1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/spinlock.h>
5 #include <linux/fs_struct.h>
6 #include <linux/namei.h>
7 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/xattr.h>
12 #include "mds_client.h"
15 * Directory operations: readdir, lookup, create, link, unlink,
20 * Ceph MDS operations are specified in terms of a base ino and
21 * relative path. Thus, the client can specify an operation on a
22 * specific inode (e.g., a getattr due to fstat(2)), or as a path
23 * relative to, say, the root directory.
25 * Normally, we limit ourselves to strict inode ops (no path component)
26 * or dentry operations (a single path component relative to an ino). The
27 * exception to this is open_root_dentry(), which will open the mount
31 const struct dentry_operations ceph_dentry_ops;
34 * Initialize ceph dentry state.
36 static int ceph_d_init(struct dentry *dentry)
38 struct ceph_dentry_info *di;
40 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
42 return -ENOMEM; /* oh well */
45 di->lease_session = NULL;
47 dentry->d_fsdata = di;
48 ceph_dentry_lru_add(dentry);
53 * for f_pos for readdir:
55 * (0xff << 52) | ((24 bits hash) << 28) |
56 * (the nth entry has hash collision);
58 * ((frag value) << 28) | (the nth entry in frag);
60 #define OFFSET_BITS 28
61 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
62 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
63 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
65 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
71 static bool is_hash_order(loff_t p)
73 return (p & HASH_ORDER) == HASH_ORDER;
76 static unsigned fpos_frag(loff_t p)
78 return p >> OFFSET_BITS;
81 static unsigned fpos_hash(loff_t p)
83 return ceph_frag_value(fpos_frag(p));
86 static unsigned fpos_off(loff_t p)
88 return p & OFFSET_MASK;
91 static int fpos_cmp(loff_t l, loff_t r)
93 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
96 return (int)(fpos_off(l) - fpos_off(r));
100 * make note of the last dentry we read, so we can
101 * continue at the same lexicographical point,
102 * regardless of what dir changes take place on the
105 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
106 int len, unsigned next_offset)
108 char *buf = kmalloc(len+1, GFP_KERNEL);
111 kfree(fi->last_name);
113 memcpy(fi->last_name, name, len);
114 fi->last_name[len] = 0;
115 fi->next_offset = next_offset;
116 dout("note_last_dentry '%s'\n", fi->last_name);
121 static struct dentry *
122 __dcache_find_get_entry(struct dentry *parent, u64 idx,
123 struct ceph_readdir_cache_control *cache_ctl)
125 struct inode *dir = d_inode(parent);
126 struct dentry *dentry;
127 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
128 loff_t ptr_pos = idx * sizeof(struct dentry *);
129 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
131 if (ptr_pos >= i_size_read(dir))
134 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
135 ceph_readdir_cache_release(cache_ctl);
136 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
137 if (!cache_ctl->page) {
138 dout(" page %lu not found\n", ptr_pgoff);
139 return ERR_PTR(-EAGAIN);
141 /* reading/filling the cache are serialized by
142 i_mutex, no need to use page lock */
143 unlock_page(cache_ctl->page);
144 cache_ctl->dentries = kmap(cache_ctl->page);
147 cache_ctl->index = idx & idx_mask;
150 spin_lock(&parent->d_lock);
151 /* check i_size again here, because empty directory can be
152 * marked as complete while not holding the i_mutex. */
153 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
154 dentry = cache_ctl->dentries[cache_ctl->index];
157 spin_unlock(&parent->d_lock);
158 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
161 return dentry ? : ERR_PTR(-EAGAIN);
165 * When possible, we try to satisfy a readdir by peeking at the
166 * dcache. We make this work by carefully ordering dentries on
167 * d_child when we initially get results back from the MDS, and
168 * falling back to a "normal" sync readdir if any dentries in the dir
171 * Complete dir indicates that we have all dentries in the dir. It is
172 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
173 * the MDS if/when the directory is modified).
175 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
178 struct ceph_file_info *fi = file->private_data;
179 struct dentry *parent = file->f_path.dentry;
180 struct inode *dir = d_inode(parent);
181 struct dentry *dentry, *last = NULL;
182 struct ceph_dentry_info *di;
183 struct ceph_readdir_cache_control cache_ctl = {};
187 dout("__dcache_readdir %p v%u at %llx\n", dir, (unsigned)shared_gen, ctx->pos);
189 /* search start position */
191 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
193 u64 step = count >> 1;
194 dentry = __dcache_find_get_entry(parent, idx + step,
197 /* use linar search */
201 if (IS_ERR(dentry)) {
202 err = PTR_ERR(dentry);
205 di = ceph_dentry(dentry);
206 spin_lock(&dentry->d_lock);
207 if (fpos_cmp(di->offset, ctx->pos) < 0) {
213 spin_unlock(&dentry->d_lock);
217 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
222 bool emit_dentry = false;
223 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
225 fi->flags |= CEPH_F_ATEND;
229 if (IS_ERR(dentry)) {
230 err = PTR_ERR(dentry);
234 di = ceph_dentry(dentry);
235 spin_lock(&dentry->d_lock);
236 if (di->lease_shared_gen == shared_gen &&
237 d_really_is_positive(dentry) &&
238 fpos_cmp(ctx->pos, di->offset) <= 0) {
241 spin_unlock(&dentry->d_lock);
244 dout(" %llx dentry %p %pd %p\n", di->offset,
245 dentry, dentry, d_inode(dentry));
246 ctx->pos = di->offset;
247 if (!dir_emit(ctx, dentry->d_name.name,
249 ceph_translate_ino(dentry->d_sb,
250 d_inode(dentry)->i_ino),
251 d_inode(dentry)->i_mode >> 12)) {
266 ceph_readdir_cache_release(&cache_ctl);
269 di = ceph_dentry(last);
270 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
271 fpos_off(di->offset) + 1);
275 /* last_name no longer match cache index */
276 if (fi->readdir_cache_idx >= 0) {
277 fi->readdir_cache_idx = -1;
278 fi->dir_release_count = 0;
284 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
286 if (!fi->last_readdir)
288 if (is_hash_order(pos))
289 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
291 return fi->frag != fpos_frag(pos);
294 static int ceph_readdir(struct file *file, struct dir_context *ctx)
296 struct ceph_file_info *fi = file->private_data;
297 struct inode *inode = file_inode(file);
298 struct ceph_inode_info *ci = ceph_inode(inode);
299 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
300 struct ceph_mds_client *mdsc = fsc->mdsc;
304 struct ceph_mds_reply_info_parsed *rinfo;
306 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
307 if (fi->flags & CEPH_F_ATEND)
310 /* always start with . and .. */
312 dout("readdir off 0 -> '.'\n");
313 if (!dir_emit(ctx, ".", 1,
314 ceph_translate_ino(inode->i_sb, inode->i_ino),
315 inode->i_mode >> 12))
320 ino_t ino = parent_ino(file->f_path.dentry);
321 dout("readdir off 1 -> '..'\n");
322 if (!dir_emit(ctx, "..", 2,
323 ceph_translate_ino(inode->i_sb, ino),
324 inode->i_mode >> 12))
329 /* can we use the dcache? */
330 spin_lock(&ci->i_ceph_lock);
331 if (ceph_test_mount_opt(fsc, DCACHE) &&
332 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
333 ceph_snap(inode) != CEPH_SNAPDIR &&
334 __ceph_dir_is_complete_ordered(ci) &&
335 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
336 int shared_gen = atomic_read(&ci->i_shared_gen);
337 spin_unlock(&ci->i_ceph_lock);
338 err = __dcache_readdir(file, ctx, shared_gen);
342 spin_unlock(&ci->i_ceph_lock);
345 /* proceed with a normal readdir */
347 /* do we have the correct frag content buffered? */
348 if (need_send_readdir(fi, ctx->pos)) {
349 struct ceph_mds_request *req;
350 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
351 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
353 /* discard old result, if any */
354 if (fi->last_readdir) {
355 ceph_mdsc_put_request(fi->last_readdir);
356 fi->last_readdir = NULL;
359 if (is_hash_order(ctx->pos)) {
360 /* fragtree isn't always accurate. choose frag
361 * based on previous reply when possible. */
362 if (frag == (unsigned)-1)
363 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
366 frag = fpos_frag(ctx->pos);
369 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
370 ceph_vinop(inode), frag, fi->last_name);
371 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
374 err = ceph_alloc_readdir_reply_buffer(req, inode);
376 ceph_mdsc_put_request(req);
379 /* hints to request -> mds selection code */
380 req->r_direct_mode = USE_AUTH_MDS;
381 if (op == CEPH_MDS_OP_READDIR) {
382 req->r_direct_hash = ceph_frag_value(frag);
383 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
384 req->r_inode_drop = CEPH_CAP_FILE_EXCL;
387 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
389 ceph_mdsc_put_request(req);
392 } else if (is_hash_order(ctx->pos)) {
393 req->r_args.readdir.offset_hash =
394 cpu_to_le32(fpos_hash(ctx->pos));
397 req->r_dir_release_cnt = fi->dir_release_count;
398 req->r_dir_ordered_cnt = fi->dir_ordered_count;
399 req->r_readdir_cache_idx = fi->readdir_cache_idx;
400 req->r_readdir_offset = fi->next_offset;
401 req->r_args.readdir.frag = cpu_to_le32(frag);
402 req->r_args.readdir.flags =
403 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
405 req->r_inode = inode;
407 req->r_dentry = dget(file->f_path.dentry);
408 err = ceph_mdsc_do_request(mdsc, NULL, req);
410 ceph_mdsc_put_request(req);
413 dout("readdir got and parsed readdir result=%d on "
414 "frag %x, end=%d, complete=%d, hash_order=%d\n",
416 (int)req->r_reply_info.dir_end,
417 (int)req->r_reply_info.dir_complete,
418 (int)req->r_reply_info.hash_order);
420 rinfo = &req->r_reply_info;
421 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
422 frag = le32_to_cpu(rinfo->dir_dir->frag);
423 if (!rinfo->hash_order) {
424 fi->next_offset = req->r_readdir_offset;
425 /* adjust ctx->pos to beginning of frag */
426 ctx->pos = ceph_make_fpos(frag,
433 fi->last_readdir = req;
435 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
436 fi->readdir_cache_idx = req->r_readdir_cache_idx;
437 if (fi->readdir_cache_idx < 0) {
438 /* preclude from marking dir ordered */
439 fi->dir_ordered_count = 0;
440 } else if (ceph_frag_is_leftmost(frag) &&
441 fi->next_offset == 2) {
442 /* note dir version at start of readdir so
443 * we can tell if any dentries get dropped */
444 fi->dir_release_count = req->r_dir_release_cnt;
445 fi->dir_ordered_count = req->r_dir_ordered_cnt;
448 dout("readdir !did_prepopulate");
449 /* disable readdir cache */
450 fi->readdir_cache_idx = -1;
451 /* preclude from marking dir complete */
452 fi->dir_release_count = 0;
455 /* note next offset and last dentry name */
456 if (rinfo->dir_nr > 0) {
457 struct ceph_mds_reply_dir_entry *rde =
458 rinfo->dir_entries + (rinfo->dir_nr-1);
459 unsigned next_offset = req->r_reply_info.dir_end ?
460 2 : (fpos_off(rde->offset) + 1);
461 err = note_last_dentry(fi, rde->name, rde->name_len,
465 } else if (req->r_reply_info.dir_end) {
471 rinfo = &fi->last_readdir->r_reply_info;
472 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
473 fi->frag, rinfo->dir_nr, ctx->pos,
474 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
477 /* search start position */
478 if (rinfo->dir_nr > 0) {
479 int step, nr = rinfo->dir_nr;
482 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
490 for (; i < rinfo->dir_nr; i++) {
491 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
492 struct ceph_vino vino;
496 BUG_ON(rde->offset < ctx->pos);
498 ctx->pos = rde->offset;
499 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
500 i, rinfo->dir_nr, ctx->pos,
501 rde->name_len, rde->name, &rde->inode.in);
503 BUG_ON(!rde->inode.in);
504 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
505 vino.ino = le64_to_cpu(rde->inode.in->ino);
506 vino.snap = le64_to_cpu(rde->inode.in->snapid);
507 ino = ceph_vino_to_ino(vino);
509 if (!dir_emit(ctx, rde->name, rde->name_len,
510 ceph_translate_ino(inode->i_sb, ino), ftype)) {
511 dout("filldir stopping us...\n");
517 ceph_mdsc_put_request(fi->last_readdir);
518 fi->last_readdir = NULL;
520 if (fi->next_offset > 2) {
526 if (!ceph_frag_is_rightmost(fi->frag)) {
527 frag = ceph_frag_next(fi->frag);
528 if (is_hash_order(ctx->pos)) {
529 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
530 fi->next_offset, true);
531 if (new_pos > ctx->pos)
535 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
536 kfree(fi->last_name);
537 fi->last_name = NULL;
539 dout("readdir next frag is %x\n", frag);
542 fi->flags |= CEPH_F_ATEND;
545 * if dir_release_count still matches the dir, no dentries
546 * were released during the whole readdir, and we should have
547 * the complete dir contents in our cache.
549 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
550 spin_lock(&ci->i_ceph_lock);
551 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
552 dout(" marking %p complete and ordered\n", inode);
553 /* use i_size to track number of entries in
555 BUG_ON(fi->readdir_cache_idx < 0);
556 i_size_write(inode, fi->readdir_cache_idx *
557 sizeof(struct dentry*));
559 dout(" marking %p complete\n", inode);
561 __ceph_dir_set_complete(ci, fi->dir_release_count,
562 fi->dir_ordered_count);
563 spin_unlock(&ci->i_ceph_lock);
566 dout("readdir %p file %p done.\n", inode, file);
570 static void reset_readdir(struct ceph_file_info *fi)
572 if (fi->last_readdir) {
573 ceph_mdsc_put_request(fi->last_readdir);
574 fi->last_readdir = NULL;
576 kfree(fi->last_name);
577 fi->last_name = NULL;
578 fi->dir_release_count = 0;
579 fi->readdir_cache_idx = -1;
580 fi->next_offset = 2; /* compensate for . and .. */
581 fi->flags &= ~CEPH_F_ATEND;
585 * discard buffered readdir content on seekdir(0), or seek to new frag,
586 * or seek prior to current chunk
588 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
590 struct ceph_mds_reply_info_parsed *rinfo;
594 if (is_hash_order(new_pos)) {
595 /* no need to reset last_name for a forward seek when
596 * dentries are sotred in hash order */
597 } else if (fi->frag != fpos_frag(new_pos)) {
600 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
601 if (!rinfo || !rinfo->dir_nr)
603 chunk_offset = rinfo->dir_entries[0].offset;
604 return new_pos < chunk_offset ||
605 is_hash_order(new_pos) != is_hash_order(chunk_offset);
608 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
610 struct ceph_file_info *fi = file->private_data;
611 struct inode *inode = file->f_mapping->host;
618 offset += file->f_pos;
622 retval = -EOPNOTSUPP;
628 if (need_reset_readdir(fi, offset)) {
629 dout("dir_llseek dropping %p content\n", file);
631 } else if (is_hash_order(offset) && offset > file->f_pos) {
632 /* for hash offset, we don't know if a forward seek
633 * is within same frag */
634 fi->dir_release_count = 0;
635 fi->readdir_cache_idx = -1;
638 if (offset != file->f_pos) {
639 file->f_pos = offset;
641 fi->flags &= ~CEPH_F_ATEND;
651 * Handle lookups for the hidden .snap directory.
653 int ceph_handle_snapdir(struct ceph_mds_request *req,
654 struct dentry *dentry, int err)
656 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
657 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
660 if (err == -ENOENT &&
661 ceph_snap(parent) == CEPH_NOSNAP &&
662 strcmp(dentry->d_name.name,
663 fsc->mount_options->snapdir_name) == 0) {
664 struct inode *inode = ceph_get_snapdir(parent);
665 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
666 dentry, dentry, inode);
667 BUG_ON(!d_unhashed(dentry));
668 d_add(dentry, inode);
675 * Figure out final result of a lookup/open request.
677 * Mainly, make sure we return the final req->r_dentry (if it already
678 * existed) in place of the original VFS-provided dentry when they
681 * Gracefully handle the case where the MDS replies with -ENOENT and
682 * no trace (which it may do, at its discretion, e.g., if it doesn't
683 * care to issue a lease on the negative dentry).
685 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
686 struct dentry *dentry, int err)
688 if (err == -ENOENT) {
691 if (!req->r_reply_info.head->is_dentry) {
692 dout("ENOENT and no trace, dentry %p inode %p\n",
693 dentry, d_inode(dentry));
694 if (d_really_is_positive(dentry)) {
703 dentry = ERR_PTR(err);
704 else if (dentry != req->r_dentry)
705 dentry = dget(req->r_dentry); /* we got spliced */
711 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
713 return ceph_ino(inode) == CEPH_INO_ROOT &&
714 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
718 * Look up a single dir entry. If there is a lookup intent, inform
719 * the MDS so that it gets our 'caps wanted' value in a single op.
721 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
724 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
725 struct ceph_mds_client *mdsc = fsc->mdsc;
726 struct ceph_mds_request *req;
731 dout("lookup %p dentry %p '%pd'\n",
732 dir, dentry, dentry);
734 if (dentry->d_name.len > NAME_MAX)
735 return ERR_PTR(-ENAMETOOLONG);
737 /* can we conclude ENOENT locally? */
738 if (d_really_is_negative(dentry)) {
739 struct ceph_inode_info *ci = ceph_inode(dir);
740 struct ceph_dentry_info *di = ceph_dentry(dentry);
742 spin_lock(&ci->i_ceph_lock);
743 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
744 if (strncmp(dentry->d_name.name,
745 fsc->mount_options->snapdir_name,
746 dentry->d_name.len) &&
747 !is_root_ceph_dentry(dir, dentry) &&
748 ceph_test_mount_opt(fsc, DCACHE) &&
749 __ceph_dir_is_complete(ci) &&
750 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
751 spin_unlock(&ci->i_ceph_lock);
752 dout(" dir %p complete, -ENOENT\n", dir);
754 di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
757 spin_unlock(&ci->i_ceph_lock);
760 op = ceph_snap(dir) == CEPH_SNAPDIR ?
761 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
762 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
764 return ERR_CAST(req);
765 req->r_dentry = dget(dentry);
768 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
769 if (ceph_security_xattr_wanted(dir))
770 mask |= CEPH_CAP_XATTR_SHARED;
771 req->r_args.getattr.mask = cpu_to_le32(mask);
774 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
775 err = ceph_mdsc_do_request(mdsc, NULL, req);
776 err = ceph_handle_snapdir(req, dentry, err);
777 dentry = ceph_finish_lookup(req, dentry, err);
778 ceph_mdsc_put_request(req); /* will dput(dentry) */
779 dout("lookup result=%p\n", dentry);
784 * If we do a create but get no trace back from the MDS, follow up with
785 * a lookup (the VFS expects us to link up the provided dentry).
787 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
789 struct dentry *result = ceph_lookup(dir, dentry, 0);
791 if (result && !IS_ERR(result)) {
793 * We created the item, then did a lookup, and found
794 * it was already linked to another inode we already
795 * had in our cache (and thus got spliced). To not
796 * confuse VFS (especially when inode is a directory),
797 * we don't link our dentry to that inode, return an
800 * This event should be rare and it happens only when
801 * we talk to old MDS. Recent MDS does not send traceless
802 * reply for request that creates new inode.
807 return PTR_ERR(result);
810 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
811 umode_t mode, dev_t rdev)
813 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
814 struct ceph_mds_client *mdsc = fsc->mdsc;
815 struct ceph_mds_request *req;
816 struct ceph_acls_info acls = {};
819 if (ceph_snap(dir) != CEPH_NOSNAP)
822 err = ceph_pre_init_acls(dir, &mode, &acls);
826 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
827 dir, dentry, mode, rdev);
828 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
833 req->r_dentry = dget(dentry);
836 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
837 req->r_args.mknod.mode = cpu_to_le32(mode);
838 req->r_args.mknod.rdev = cpu_to_le32(rdev);
839 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
840 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
842 req->r_pagelist = acls.pagelist;
843 acls.pagelist = NULL;
845 err = ceph_mdsc_do_request(mdsc, dir, req);
846 if (!err && !req->r_reply_info.head->is_dentry)
847 err = ceph_handle_notrace_create(dir, dentry);
848 ceph_mdsc_put_request(req);
851 ceph_init_inode_acls(d_inode(dentry), &acls);
854 ceph_release_acls_info(&acls);
858 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
861 return ceph_mknod(dir, dentry, mode, 0);
864 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
867 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
868 struct ceph_mds_client *mdsc = fsc->mdsc;
869 struct ceph_mds_request *req;
872 if (ceph_snap(dir) != CEPH_NOSNAP)
875 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
876 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
881 req->r_path2 = kstrdup(dest, GFP_KERNEL);
884 ceph_mdsc_put_request(req);
888 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
889 req->r_dentry = dget(dentry);
891 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
892 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
893 err = ceph_mdsc_do_request(mdsc, dir, req);
894 if (!err && !req->r_reply_info.head->is_dentry)
895 err = ceph_handle_notrace_create(dir, dentry);
896 ceph_mdsc_put_request(req);
903 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
905 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
906 struct ceph_mds_client *mdsc = fsc->mdsc;
907 struct ceph_mds_request *req;
908 struct ceph_acls_info acls = {};
912 if (ceph_snap(dir) == CEPH_SNAPDIR) {
913 /* mkdir .snap/foo is a MKSNAP */
914 op = CEPH_MDS_OP_MKSNAP;
915 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
917 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
918 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
919 op = CEPH_MDS_OP_MKDIR;
925 err = ceph_pre_init_acls(dir, &mode, &acls);
929 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
935 req->r_dentry = dget(dentry);
938 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
939 req->r_args.mkdir.mode = cpu_to_le32(mode);
940 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
941 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
943 req->r_pagelist = acls.pagelist;
944 acls.pagelist = NULL;
946 err = ceph_mdsc_do_request(mdsc, dir, req);
948 !req->r_reply_info.head->is_target &&
949 !req->r_reply_info.head->is_dentry)
950 err = ceph_handle_notrace_create(dir, dentry);
951 ceph_mdsc_put_request(req);
954 ceph_init_inode_acls(d_inode(dentry), &acls);
957 ceph_release_acls_info(&acls);
961 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
962 struct dentry *dentry)
964 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
965 struct ceph_mds_client *mdsc = fsc->mdsc;
966 struct ceph_mds_request *req;
969 if (ceph_snap(dir) != CEPH_NOSNAP)
972 dout("link in dir %p old_dentry %p dentry %p\n", dir,
974 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
979 req->r_dentry = dget(dentry);
981 req->r_old_dentry = dget(old_dentry);
983 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
984 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
985 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
986 /* release LINK_SHARED on source inode (mds will lock it) */
987 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
988 err = ceph_mdsc_do_request(mdsc, dir, req);
991 } else if (!req->r_reply_info.head->is_dentry) {
992 ihold(d_inode(old_dentry));
993 d_instantiate(dentry, d_inode(old_dentry));
995 ceph_mdsc_put_request(req);
1000 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1001 * looks like the link count will hit 0, drop any other caps (other
1002 * than PIN) we don't specifically want (due to the file still being
1005 static int drop_caps_for_unlink(struct inode *inode)
1007 struct ceph_inode_info *ci = ceph_inode(inode);
1008 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1010 spin_lock(&ci->i_ceph_lock);
1011 if (inode->i_nlink == 1) {
1012 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1013 ci->i_ceph_flags |= CEPH_I_NODELAY;
1015 spin_unlock(&ci->i_ceph_lock);
1020 * rmdir and unlink are differ only by the metadata op code
1022 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1024 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1025 struct ceph_mds_client *mdsc = fsc->mdsc;
1026 struct inode *inode = d_inode(dentry);
1027 struct ceph_mds_request *req;
1031 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1032 /* rmdir .snap/foo is RMSNAP */
1033 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1034 op = CEPH_MDS_OP_RMSNAP;
1035 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1036 dout("unlink/rmdir dir %p dn %p inode %p\n",
1037 dir, dentry, inode);
1038 op = d_is_dir(dentry) ?
1039 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1042 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1047 req->r_dentry = dget(dentry);
1048 req->r_num_caps = 2;
1049 req->r_parent = dir;
1050 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1051 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1052 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1053 req->r_inode_drop = drop_caps_for_unlink(inode);
1054 err = ceph_mdsc_do_request(mdsc, dir, req);
1055 if (!err && !req->r_reply_info.head->is_dentry)
1057 ceph_mdsc_put_request(req);
1062 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1063 struct inode *new_dir, struct dentry *new_dentry,
1066 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1067 struct ceph_mds_client *mdsc = fsc->mdsc;
1068 struct ceph_mds_request *req;
1069 int op = CEPH_MDS_OP_RENAME;
1075 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1077 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1078 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1079 op = CEPH_MDS_OP_RENAMESNAP;
1083 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1084 old_dir, old_dentry, new_dir, new_dentry);
1085 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1087 return PTR_ERR(req);
1089 req->r_dentry = dget(new_dentry);
1090 req->r_num_caps = 2;
1091 req->r_old_dentry = dget(old_dentry);
1092 req->r_old_dentry_dir = old_dir;
1093 req->r_parent = new_dir;
1094 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1095 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1096 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1097 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1098 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1099 /* release LINK_RDCACHE on source inode (mds will lock it) */
1100 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1101 if (d_really_is_positive(new_dentry))
1102 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
1103 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1104 if (!err && !req->r_reply_info.head->is_dentry) {
1106 * Normally d_move() is done by fill_trace (called by
1107 * do_request, above). If there is no trace, we need
1110 d_move(old_dentry, new_dentry);
1112 ceph_mdsc_put_request(req);
1117 * Ensure a dentry lease will no longer revalidate.
1119 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1121 spin_lock(&dentry->d_lock);
1122 ceph_dentry(dentry)->time = jiffies;
1123 ceph_dentry(dentry)->lease_shared_gen = 0;
1124 spin_unlock(&dentry->d_lock);
1128 * Check if dentry lease is valid. If not, delete the lease. Try to
1129 * renew if the least is more than half up.
1131 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1134 struct ceph_dentry_info *di;
1135 struct ceph_mds_session *s;
1139 struct ceph_mds_session *session = NULL;
1142 spin_lock(&dentry->d_lock);
1143 di = ceph_dentry(dentry);
1144 if (di && di->lease_session) {
1145 s = di->lease_session;
1146 spin_lock(&s->s_gen_ttl_lock);
1149 spin_unlock(&s->s_gen_ttl_lock);
1151 if (di->lease_gen == gen &&
1152 time_before(jiffies, di->time) &&
1153 time_before(jiffies, ttl)) {
1155 if (di->lease_renew_after &&
1156 time_after(jiffies, di->lease_renew_after)) {
1158 * We should renew. If we're in RCU walk mode
1159 * though, we can't do that so just return
1162 if (flags & LOOKUP_RCU) {
1165 session = ceph_get_mds_session(s);
1166 seq = di->lease_seq;
1167 di->lease_renew_after = 0;
1168 di->lease_renew_from = jiffies;
1173 spin_unlock(&dentry->d_lock);
1176 ceph_mdsc_lease_send_msg(session, dir, dentry,
1177 CEPH_MDS_LEASE_RENEW, seq);
1178 ceph_put_mds_session(session);
1180 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1185 * Check if directory-wide content lease/cap is valid.
1187 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1189 struct ceph_inode_info *ci = ceph_inode(dir);
1190 struct ceph_dentry_info *di = ceph_dentry(dentry);
1193 spin_lock(&ci->i_ceph_lock);
1194 if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen)
1195 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1196 spin_unlock(&ci->i_ceph_lock);
1197 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1198 dir, (unsigned)atomic_read(&ci->i_shared_gen),
1199 dentry, (unsigned)di->lease_shared_gen, valid);
1204 * Check if cached dentry can be trusted.
1206 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1209 struct dentry *parent;
1212 if (flags & LOOKUP_RCU) {
1213 parent = READ_ONCE(dentry->d_parent);
1214 dir = d_inode_rcu(parent);
1218 parent = dget_parent(dentry);
1219 dir = d_inode(parent);
1222 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1223 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1225 /* always trust cached snapped dentries, snapdir dentry */
1226 if (ceph_snap(dir) != CEPH_NOSNAP) {
1227 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1228 dentry, d_inode(dentry));
1230 } else if (d_really_is_positive(dentry) &&
1231 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1234 valid = dentry_lease_is_valid(dentry, flags, dir);
1235 if (valid == -ECHILD)
1237 if (valid || dir_lease_is_valid(dir, dentry)) {
1238 if (d_really_is_positive(dentry))
1239 valid = ceph_is_any_caps(d_inode(dentry));
1246 struct ceph_mds_client *mdsc =
1247 ceph_sb_to_client(dir->i_sb)->mdsc;
1248 struct ceph_mds_request *req;
1252 if (flags & LOOKUP_RCU)
1255 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1256 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1257 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1259 req->r_dentry = dget(dentry);
1260 req->r_num_caps = 2;
1261 req->r_parent = dir;
1263 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1264 if (ceph_security_xattr_wanted(dir))
1265 mask |= CEPH_CAP_XATTR_SHARED;
1266 req->r_args.getattr.mask = cpu_to_le32(mask);
1268 err = ceph_mdsc_do_request(mdsc, NULL, req);
1271 if (d_really_is_positive(dentry) &&
1272 d_inode(dentry) == req->r_target_inode)
1276 if (d_really_is_negative(dentry))
1282 ceph_mdsc_put_request(req);
1283 dout("d_revalidate %p lookup result=%d\n",
1288 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1290 ceph_dentry_lru_touch(dentry);
1292 ceph_dir_clear_complete(dir);
1295 if (!(flags & LOOKUP_RCU))
1301 * Release our ceph_dentry_info.
1303 static void ceph_d_release(struct dentry *dentry)
1305 struct ceph_dentry_info *di = ceph_dentry(dentry);
1307 dout("d_release %p\n", dentry);
1308 ceph_dentry_lru_del(dentry);
1310 spin_lock(&dentry->d_lock);
1311 dentry->d_fsdata = NULL;
1312 spin_unlock(&dentry->d_lock);
1314 if (di->lease_session)
1315 ceph_put_mds_session(di->lease_session);
1316 kmem_cache_free(ceph_dentry_cachep, di);
1320 * When the VFS prunes a dentry from the cache, we need to clear the
1321 * complete flag on the parent directory.
1323 * Called under dentry->d_lock.
1325 static void ceph_d_prune(struct dentry *dentry)
1327 dout("ceph_d_prune %p\n", dentry);
1329 /* do we have a valid parent? */
1330 if (IS_ROOT(dentry))
1333 /* if we are not hashed, we don't affect dir's completeness */
1334 if (d_unhashed(dentry))
1337 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
1341 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1342 * cleared until d_release
1344 ceph_dir_clear_complete(d_inode(dentry->d_parent));
1348 * read() on a dir. This weird interface hack only works if mounted
1349 * with '-o dirstat'.
1351 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1354 struct ceph_file_info *cf = file->private_data;
1355 struct inode *inode = file_inode(file);
1356 struct ceph_inode_info *ci = ceph_inode(inode);
1358 const int bufsize = 1024;
1360 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1363 if (!cf->dir_info) {
1364 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1368 snprintf(cf->dir_info, bufsize,
1371 " subdirs: %20lld\n"
1372 "rentries: %20lld\n"
1374 " rsubdirs: %20lld\n"
1376 "rctime: %10ld.%09ld\n",
1377 ci->i_files + ci->i_subdirs,
1380 ci->i_rfiles + ci->i_rsubdirs,
1384 (long)ci->i_rctime.tv_sec,
1385 (long)ci->i_rctime.tv_nsec);
1388 if (*ppos >= cf->dir_info_len)
1390 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1391 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1394 *ppos += (size - left);
1399 * We maintain a private dentry LRU.
1401 * FIXME: this needs to be changed to a per-mds lru to be useful.
1403 void ceph_dentry_lru_add(struct dentry *dn)
1405 struct ceph_dentry_info *di = ceph_dentry(dn);
1406 struct ceph_mds_client *mdsc;
1408 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1409 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1410 spin_lock(&mdsc->dentry_lru_lock);
1411 list_add_tail(&di->lru, &mdsc->dentry_lru);
1413 spin_unlock(&mdsc->dentry_lru_lock);
1416 void ceph_dentry_lru_touch(struct dentry *dn)
1418 struct ceph_dentry_info *di = ceph_dentry(dn);
1419 struct ceph_mds_client *mdsc;
1421 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1423 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1424 spin_lock(&mdsc->dentry_lru_lock);
1425 list_move_tail(&di->lru, &mdsc->dentry_lru);
1426 spin_unlock(&mdsc->dentry_lru_lock);
1429 void ceph_dentry_lru_del(struct dentry *dn)
1431 struct ceph_dentry_info *di = ceph_dentry(dn);
1432 struct ceph_mds_client *mdsc;
1434 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1435 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1436 spin_lock(&mdsc->dentry_lru_lock);
1437 list_del_init(&di->lru);
1439 spin_unlock(&mdsc->dentry_lru_lock);
1443 * Return name hash for a given dentry. This is dependent on
1444 * the parent directory's hash function.
1446 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1448 struct ceph_inode_info *dci = ceph_inode(dir);
1450 switch (dci->i_dir_layout.dl_dir_hash) {
1451 case 0: /* for backward compat */
1452 case CEPH_STR_HASH_LINUX:
1453 return dn->d_name.hash;
1456 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1457 dn->d_name.name, dn->d_name.len);
1461 const struct file_operations ceph_dir_fops = {
1462 .read = ceph_read_dir,
1463 .iterate = ceph_readdir,
1464 .llseek = ceph_dir_llseek,
1466 .release = ceph_release,
1467 .unlocked_ioctl = ceph_ioctl,
1468 .fsync = ceph_fsync,
1471 const struct file_operations ceph_snapdir_fops = {
1472 .iterate = ceph_readdir,
1473 .llseek = ceph_dir_llseek,
1475 .release = ceph_release,
1478 const struct inode_operations ceph_dir_iops = {
1479 .lookup = ceph_lookup,
1480 .permission = ceph_permission,
1481 .getattr = ceph_getattr,
1482 .setattr = ceph_setattr,
1483 .listxattr = ceph_listxattr,
1484 .get_acl = ceph_get_acl,
1485 .set_acl = ceph_set_acl,
1486 .mknod = ceph_mknod,
1487 .symlink = ceph_symlink,
1488 .mkdir = ceph_mkdir,
1490 .unlink = ceph_unlink,
1491 .rmdir = ceph_unlink,
1492 .rename = ceph_rename,
1493 .create = ceph_create,
1494 .atomic_open = ceph_atomic_open,
1497 const struct inode_operations ceph_snapdir_iops = {
1498 .lookup = ceph_lookup,
1499 .permission = ceph_permission,
1500 .getattr = ceph_getattr,
1501 .mkdir = ceph_mkdir,
1502 .rmdir = ceph_unlink,
1503 .rename = ceph_rename,
1506 const struct dentry_operations ceph_dentry_ops = {
1507 .d_revalidate = ceph_d_revalidate,
1508 .d_release = ceph_d_release,
1509 .d_prune = ceph_d_prune,
1510 .d_init = ceph_d_init,