1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/spinlock.h>
4 #include <linux/fs_struct.h>
5 #include <linux/namei.h>
6 #include <linux/slab.h>
7 #include <linux/sched.h>
10 #include "mds_client.h"
13 * Directory operations: readdir, lookup, create, link, unlink,
18 * Ceph MDS operations are specified in terms of a base ino and
19 * relative path. Thus, the client can specify an operation on a
20 * specific inode (e.g., a getattr due to fstat(2)), or as a path
21 * relative to, say, the root directory.
23 * Normally, we limit ourselves to strict inode ops (no path component)
24 * or dentry operations (a single path component relative to an ino). The
25 * exception to this is open_root_dentry(), which will open the mount
29 const struct dentry_operations ceph_dentry_ops;
32 * Initialize ceph dentry state.
34 int ceph_init_dentry(struct dentry *dentry)
36 struct ceph_dentry_info *di;
41 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
43 return -ENOMEM; /* oh well */
45 spin_lock(&dentry->d_lock);
46 if (dentry->d_fsdata) {
48 kmem_cache_free(ceph_dentry_cachep, di);
52 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
53 d_set_d_op(dentry, &ceph_dentry_ops);
54 else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
55 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
57 d_set_d_op(dentry, &ceph_snap_dentry_ops);
60 di->lease_session = NULL;
61 dentry->d_time = jiffies;
62 /* avoid reordering d_fsdata setup so that the check above is safe */
64 dentry->d_fsdata = di;
65 ceph_dentry_lru_add(dentry);
67 spin_unlock(&dentry->d_lock);
72 * for f_pos for readdir:
74 * (0xff << 52) | ((24 bits hash) << 28) |
75 * (the nth entry has hash collision);
77 * ((frag value) << 28) | (the nth entry in frag);
79 #define OFFSET_BITS 28
80 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
81 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
82 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
84 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
90 static bool is_hash_order(loff_t p)
92 return (p & HASH_ORDER) == HASH_ORDER;
95 static unsigned fpos_frag(loff_t p)
97 return p >> OFFSET_BITS;
100 static unsigned fpos_hash(loff_t p)
102 return ceph_frag_value(fpos_frag(p));
105 static unsigned fpos_off(loff_t p)
107 return p & OFFSET_MASK;
110 static int fpos_cmp(loff_t l, loff_t r)
112 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
115 return (int)(fpos_off(l) - fpos_off(r));
119 * make note of the last dentry we read, so we can
120 * continue at the same lexicographical point,
121 * regardless of what dir changes take place on the
124 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
125 int len, unsigned next_offset)
127 char *buf = kmalloc(len+1, GFP_KERNEL);
130 kfree(fi->last_name);
132 memcpy(fi->last_name, name, len);
133 fi->last_name[len] = 0;
134 fi->next_offset = next_offset;
135 dout("note_last_dentry '%s'\n", fi->last_name);
140 static struct dentry *
141 __dcache_find_get_entry(struct dentry *parent, u64 idx,
142 struct ceph_readdir_cache_control *cache_ctl)
144 struct inode *dir = d_inode(parent);
145 struct dentry *dentry;
146 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
147 loff_t ptr_pos = idx * sizeof(struct dentry *);
148 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
150 if (ptr_pos >= i_size_read(dir))
153 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
154 ceph_readdir_cache_release(cache_ctl);
155 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
156 if (!cache_ctl->page) {
157 dout(" page %lu not found\n", ptr_pgoff);
158 return ERR_PTR(-EAGAIN);
160 /* reading/filling the cache are serialized by
161 i_mutex, no need to use page lock */
162 unlock_page(cache_ctl->page);
163 cache_ctl->dentries = kmap(cache_ctl->page);
166 cache_ctl->index = idx & idx_mask;
169 spin_lock(&parent->d_lock);
170 /* check i_size again here, because empty directory can be
171 * marked as complete while not holding the i_mutex. */
172 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
173 dentry = cache_ctl->dentries[cache_ctl->index];
176 spin_unlock(&parent->d_lock);
177 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
180 return dentry ? : ERR_PTR(-EAGAIN);
184 * When possible, we try to satisfy a readdir by peeking at the
185 * dcache. We make this work by carefully ordering dentries on
186 * d_child when we initially get results back from the MDS, and
187 * falling back to a "normal" sync readdir if any dentries in the dir
190 * Complete dir indicates that we have all dentries in the dir. It is
191 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
192 * the MDS if/when the directory is modified).
194 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
197 struct ceph_file_info *fi = file->private_data;
198 struct dentry *parent = file->f_path.dentry;
199 struct inode *dir = d_inode(parent);
200 struct dentry *dentry, *last = NULL;
201 struct ceph_dentry_info *di;
202 struct ceph_readdir_cache_control cache_ctl = {};
206 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
208 /* search start position */
210 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
212 u64 step = count >> 1;
213 dentry = __dcache_find_get_entry(parent, idx + step,
216 /* use linar search */
220 if (IS_ERR(dentry)) {
221 err = PTR_ERR(dentry);
224 di = ceph_dentry(dentry);
225 spin_lock(&dentry->d_lock);
226 if (fpos_cmp(di->offset, ctx->pos) < 0) {
232 spin_unlock(&dentry->d_lock);
236 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
241 bool emit_dentry = false;
242 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
244 fi->flags |= CEPH_F_ATEND;
248 if (IS_ERR(dentry)) {
249 err = PTR_ERR(dentry);
253 di = ceph_dentry(dentry);
254 spin_lock(&dentry->d_lock);
255 if (di->lease_shared_gen == shared_gen &&
256 d_really_is_positive(dentry) &&
257 fpos_cmp(ctx->pos, di->offset) <= 0) {
260 spin_unlock(&dentry->d_lock);
263 dout(" %llx dentry %p %pd %p\n", di->offset,
264 dentry, dentry, d_inode(dentry));
265 ctx->pos = di->offset;
266 if (!dir_emit(ctx, dentry->d_name.name,
268 ceph_translate_ino(dentry->d_sb,
269 d_inode(dentry)->i_ino),
270 d_inode(dentry)->i_mode >> 12)) {
285 ceph_readdir_cache_release(&cache_ctl);
288 di = ceph_dentry(last);
289 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
290 fpos_off(di->offset) + 1);
298 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
300 if (!fi->last_readdir)
302 if (is_hash_order(pos))
303 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
305 return fi->frag != fpos_frag(pos);
308 static int ceph_readdir(struct file *file, struct dir_context *ctx)
310 struct ceph_file_info *fi = file->private_data;
311 struct inode *inode = file_inode(file);
312 struct ceph_inode_info *ci = ceph_inode(inode);
313 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
314 struct ceph_mds_client *mdsc = fsc->mdsc;
318 struct ceph_mds_reply_info_parsed *rinfo;
320 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
321 if (fi->flags & CEPH_F_ATEND)
324 /* always start with . and .. */
326 dout("readdir off 0 -> '.'\n");
327 if (!dir_emit(ctx, ".", 1,
328 ceph_translate_ino(inode->i_sb, inode->i_ino),
329 inode->i_mode >> 12))
334 ino_t ino = parent_ino(file->f_path.dentry);
335 dout("readdir off 1 -> '..'\n");
336 if (!dir_emit(ctx, "..", 2,
337 ceph_translate_ino(inode->i_sb, ino),
338 inode->i_mode >> 12))
343 /* can we use the dcache? */
344 spin_lock(&ci->i_ceph_lock);
345 if (ceph_test_mount_opt(fsc, DCACHE) &&
346 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
347 ceph_snap(inode) != CEPH_SNAPDIR &&
348 __ceph_dir_is_complete_ordered(ci) &&
349 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
350 u32 shared_gen = ci->i_shared_gen;
351 spin_unlock(&ci->i_ceph_lock);
352 err = __dcache_readdir(file, ctx, shared_gen);
356 spin_unlock(&ci->i_ceph_lock);
359 /* proceed with a normal readdir */
361 /* do we have the correct frag content buffered? */
362 if (need_send_readdir(fi, ctx->pos)) {
363 struct ceph_mds_request *req;
365 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
366 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
368 /* discard old result, if any */
369 if (fi->last_readdir) {
370 ceph_mdsc_put_request(fi->last_readdir);
371 fi->last_readdir = NULL;
374 if (is_hash_order(ctx->pos)) {
375 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
378 frag = fpos_frag(ctx->pos);
381 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
382 ceph_vinop(inode), frag, fi->last_name);
383 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
386 err = ceph_alloc_readdir_reply_buffer(req, inode);
388 ceph_mdsc_put_request(req);
391 /* hints to request -> mds selection code */
392 req->r_direct_mode = USE_AUTH_MDS;
393 req->r_direct_hash = ceph_frag_value(frag);
394 req->r_direct_is_hash = true;
396 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
398 ceph_mdsc_put_request(req);
402 req->r_dir_release_cnt = fi->dir_release_count;
403 req->r_dir_ordered_cnt = fi->dir_ordered_count;
404 req->r_readdir_cache_idx = fi->readdir_cache_idx;
405 req->r_readdir_offset = fi->next_offset;
406 req->r_args.readdir.frag = cpu_to_le32(frag);
407 req->r_args.readdir.flags =
408 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
410 req->r_inode = inode;
412 req->r_dentry = dget(file->f_path.dentry);
413 err = ceph_mdsc_do_request(mdsc, NULL, req);
415 ceph_mdsc_put_request(req);
418 dout("readdir got and parsed readdir result=%d on "
419 "frag %x, end=%d, complete=%d, hash_order=%d\n",
421 (int)req->r_reply_info.dir_end,
422 (int)req->r_reply_info.dir_complete,
423 (int)req->r_reply_info.hash_order);
425 rinfo = &req->r_reply_info;
426 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
427 frag = le32_to_cpu(rinfo->dir_dir->frag);
428 if (!rinfo->hash_order) {
429 fi->next_offset = req->r_readdir_offset;
430 /* adjust ctx->pos to beginning of frag */
431 ctx->pos = ceph_make_fpos(frag,
438 fi->last_readdir = req;
440 if (req->r_did_prepopulate) {
441 fi->readdir_cache_idx = req->r_readdir_cache_idx;
442 if (fi->readdir_cache_idx < 0) {
443 /* preclude from marking dir ordered */
444 fi->dir_ordered_count = 0;
445 } else if (ceph_frag_is_leftmost(frag) &&
446 fi->next_offset == 2) {
447 /* note dir version at start of readdir so
448 * we can tell if any dentries get dropped */
449 fi->dir_release_count = req->r_dir_release_cnt;
450 fi->dir_ordered_count = req->r_dir_ordered_cnt;
453 dout("readdir !did_prepopulate");
454 /* disable readdir cache */
455 fi->readdir_cache_idx = -1;
456 /* preclude from marking dir complete */
457 fi->dir_release_count = 0;
460 /* note next offset and last dentry name */
461 if (rinfo->dir_nr > 0) {
462 struct ceph_mds_reply_dir_entry *rde =
463 rinfo->dir_entries + (rinfo->dir_nr-1);
464 unsigned next_offset = req->r_reply_info.dir_end ?
465 2 : (fpos_off(rde->offset) + 1);
466 err = note_last_dentry(fi, rde->name, rde->name_len,
470 } else if (req->r_reply_info.dir_end) {
476 rinfo = &fi->last_readdir->r_reply_info;
477 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
478 fi->frag, rinfo->dir_nr, ctx->pos,
479 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
482 /* search start position */
483 if (rinfo->dir_nr > 0) {
484 int step, nr = rinfo->dir_nr;
487 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
495 for (; i < rinfo->dir_nr; i++) {
496 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
497 struct ceph_vino vino;
500 BUG_ON(rde->offset < ctx->pos);
502 ctx->pos = rde->offset;
503 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
504 i, rinfo->dir_nr, ctx->pos,
505 rde->name_len, rde->name, &rde->inode.in);
507 BUG_ON(!rde->inode.in);
508 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
509 vino.ino = le64_to_cpu(rde->inode.in->ino);
510 vino.snap = le64_to_cpu(rde->inode.in->snapid);
511 ino = ceph_vino_to_ino(vino);
513 if (!dir_emit(ctx, rde->name, rde->name_len,
514 ceph_translate_ino(inode->i_sb, ino), ftype)) {
515 dout("filldir stopping us...\n");
521 if (fi->next_offset > 2) {
522 ceph_mdsc_put_request(fi->last_readdir);
523 fi->last_readdir = NULL;
528 if (!ceph_frag_is_rightmost(fi->frag)) {
529 unsigned frag = ceph_frag_next(fi->frag);
530 if (is_hash_order(ctx->pos)) {
531 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
532 fi->next_offset, true);
533 if (new_pos > ctx->pos)
537 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
538 kfree(fi->last_name);
539 fi->last_name = NULL;
541 dout("readdir next frag is %x\n", frag);
544 fi->flags |= CEPH_F_ATEND;
547 * if dir_release_count still matches the dir, no dentries
548 * were released during the whole readdir, and we should have
549 * the complete dir contents in our cache.
551 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
552 spin_lock(&ci->i_ceph_lock);
553 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
554 dout(" marking %p complete and ordered\n", inode);
555 /* use i_size to track number of entries in
557 BUG_ON(fi->readdir_cache_idx < 0);
558 i_size_write(inode, fi->readdir_cache_idx *
559 sizeof(struct dentry*));
561 dout(" marking %p complete\n", inode);
563 __ceph_dir_set_complete(ci, fi->dir_release_count,
564 fi->dir_ordered_count);
565 spin_unlock(&ci->i_ceph_lock);
568 dout("readdir %p file %p done.\n", inode, file);
572 static void reset_readdir(struct ceph_file_info *fi)
574 if (fi->last_readdir) {
575 ceph_mdsc_put_request(fi->last_readdir);
576 fi->last_readdir = NULL;
578 kfree(fi->last_name);
579 fi->last_name = NULL;
580 fi->dir_release_count = 0;
581 fi->readdir_cache_idx = -1;
582 fi->next_offset = 2; /* compensate for . and .. */
583 fi->flags &= ~CEPH_F_ATEND;
587 * discard buffered readdir content on seekdir(0), or seek to new frag,
588 * or seek prior to current chunk
590 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
592 struct ceph_mds_reply_info_parsed *rinfo;
596 if (is_hash_order(new_pos)) {
597 /* no need to reset last_name for a forward seek when
598 * dentries are sotred in hash order */
599 } else if (fi->frag |= fpos_frag(new_pos)) {
602 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
603 if (!rinfo || !rinfo->dir_nr)
605 chunk_offset = rinfo->dir_entries[0].offset;
606 return new_pos < chunk_offset ||
607 is_hash_order(new_pos) != is_hash_order(chunk_offset);
610 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
612 struct ceph_file_info *fi = file->private_data;
613 struct inode *inode = file->f_mapping->host;
620 offset += file->f_pos;
624 retval = -EOPNOTSUPP;
630 if (need_reset_readdir(fi, offset)) {
631 dout("dir_llseek dropping %p content\n", file);
633 } else if (is_hash_order(offset) && offset > file->f_pos) {
634 /* for hash offset, we don't know if a forward seek
635 * is within same frag */
636 fi->dir_release_count = 0;
637 fi->readdir_cache_idx = -1;
640 if (offset != file->f_pos) {
641 file->f_pos = offset;
643 fi->flags &= ~CEPH_F_ATEND;
653 * Handle lookups for the hidden .snap directory.
655 int ceph_handle_snapdir(struct ceph_mds_request *req,
656 struct dentry *dentry, int err)
658 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
659 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
662 if (err == -ENOENT &&
663 ceph_snap(parent) == CEPH_NOSNAP &&
664 strcmp(dentry->d_name.name,
665 fsc->mount_options->snapdir_name) == 0) {
666 struct inode *inode = ceph_get_snapdir(parent);
667 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
668 dentry, dentry, inode);
669 BUG_ON(!d_unhashed(dentry));
670 d_add(dentry, inode);
677 * Figure out final result of a lookup/open request.
679 * Mainly, make sure we return the final req->r_dentry (if it already
680 * existed) in place of the original VFS-provided dentry when they
683 * Gracefully handle the case where the MDS replies with -ENOENT and
684 * no trace (which it may do, at its discretion, e.g., if it doesn't
685 * care to issue a lease on the negative dentry).
687 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
688 struct dentry *dentry, int err)
690 if (err == -ENOENT) {
693 if (!req->r_reply_info.head->is_dentry) {
694 dout("ENOENT and no trace, dentry %p inode %p\n",
695 dentry, d_inode(dentry));
696 if (d_really_is_positive(dentry)) {
705 dentry = ERR_PTR(err);
706 else if (dentry != req->r_dentry)
707 dentry = dget(req->r_dentry); /* we got spliced */
713 static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
715 return ceph_ino(inode) == CEPH_INO_ROOT &&
716 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
720 * Look up a single dir entry. If there is a lookup intent, inform
721 * the MDS so that it gets our 'caps wanted' value in a single op.
723 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
726 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
727 struct ceph_mds_client *mdsc = fsc->mdsc;
728 struct ceph_mds_request *req;
733 dout("lookup %p dentry %p '%pd'\n",
734 dir, dentry, dentry);
736 if (dentry->d_name.len > NAME_MAX)
737 return ERR_PTR(-ENAMETOOLONG);
739 err = ceph_init_dentry(dentry);
743 /* can we conclude ENOENT locally? */
744 if (d_really_is_negative(dentry)) {
745 struct ceph_inode_info *ci = ceph_inode(dir);
746 struct ceph_dentry_info *di = ceph_dentry(dentry);
748 spin_lock(&ci->i_ceph_lock);
749 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
750 if (strncmp(dentry->d_name.name,
751 fsc->mount_options->snapdir_name,
752 dentry->d_name.len) &&
753 !is_root_ceph_dentry(dir, dentry) &&
754 ceph_test_mount_opt(fsc, DCACHE) &&
755 __ceph_dir_is_complete(ci) &&
756 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
757 spin_unlock(&ci->i_ceph_lock);
758 dout(" dir %p complete, -ENOENT\n", dir);
760 di->lease_shared_gen = ci->i_shared_gen;
763 spin_unlock(&ci->i_ceph_lock);
766 op = ceph_snap(dir) == CEPH_SNAPDIR ?
767 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
768 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
770 return ERR_CAST(req);
771 req->r_dentry = dget(dentry);
774 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
775 if (ceph_security_xattr_wanted(dir))
776 mask |= CEPH_CAP_XATTR_SHARED;
777 req->r_args.getattr.mask = cpu_to_le32(mask);
779 req->r_locked_dir = dir;
780 err = ceph_mdsc_do_request(mdsc, NULL, req);
781 err = ceph_handle_snapdir(req, dentry, err);
782 dentry = ceph_finish_lookup(req, dentry, err);
783 ceph_mdsc_put_request(req); /* will dput(dentry) */
784 dout("lookup result=%p\n", dentry);
789 * If we do a create but get no trace back from the MDS, follow up with
790 * a lookup (the VFS expects us to link up the provided dentry).
792 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
794 struct dentry *result = ceph_lookup(dir, dentry, 0);
796 if (result && !IS_ERR(result)) {
798 * We created the item, then did a lookup, and found
799 * it was already linked to another inode we already
800 * had in our cache (and thus got spliced). To not
801 * confuse VFS (especially when inode is a directory),
802 * we don't link our dentry to that inode, return an
805 * This event should be rare and it happens only when
806 * we talk to old MDS. Recent MDS does not send traceless
807 * reply for request that creates new inode.
812 return PTR_ERR(result);
815 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
816 umode_t mode, dev_t rdev)
818 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
819 struct ceph_mds_client *mdsc = fsc->mdsc;
820 struct ceph_mds_request *req;
821 struct ceph_acls_info acls = {};
824 if (ceph_snap(dir) != CEPH_NOSNAP)
827 err = ceph_pre_init_acls(dir, &mode, &acls);
831 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
832 dir, dentry, mode, rdev);
833 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
838 req->r_dentry = dget(dentry);
840 req->r_locked_dir = dir;
841 req->r_args.mknod.mode = cpu_to_le32(mode);
842 req->r_args.mknod.rdev = cpu_to_le32(rdev);
843 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
844 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
846 req->r_pagelist = acls.pagelist;
847 acls.pagelist = NULL;
849 err = ceph_mdsc_do_request(mdsc, dir, req);
850 if (!err && !req->r_reply_info.head->is_dentry)
851 err = ceph_handle_notrace_create(dir, dentry);
852 ceph_mdsc_put_request(req);
855 ceph_init_inode_acls(d_inode(dentry), &acls);
858 ceph_release_acls_info(&acls);
862 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
865 return ceph_mknod(dir, dentry, mode, 0);
868 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
871 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
872 struct ceph_mds_client *mdsc = fsc->mdsc;
873 struct ceph_mds_request *req;
876 if (ceph_snap(dir) != CEPH_NOSNAP)
879 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
880 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
885 req->r_path2 = kstrdup(dest, GFP_KERNEL);
888 ceph_mdsc_put_request(req);
891 req->r_locked_dir = dir;
892 req->r_dentry = dget(dentry);
894 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
895 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
896 err = ceph_mdsc_do_request(mdsc, dir, req);
897 if (!err && !req->r_reply_info.head->is_dentry)
898 err = ceph_handle_notrace_create(dir, dentry);
899 ceph_mdsc_put_request(req);
906 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
908 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
909 struct ceph_mds_client *mdsc = fsc->mdsc;
910 struct ceph_mds_request *req;
911 struct ceph_acls_info acls = {};
915 if (ceph_snap(dir) == CEPH_SNAPDIR) {
916 /* mkdir .snap/foo is a MKSNAP */
917 op = CEPH_MDS_OP_MKSNAP;
918 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
920 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
921 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
922 op = CEPH_MDS_OP_MKDIR;
928 err = ceph_pre_init_acls(dir, &mode, &acls);
932 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
938 req->r_dentry = dget(dentry);
940 req->r_locked_dir = dir;
941 req->r_args.mkdir.mode = cpu_to_le32(mode);
942 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
943 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
945 req->r_pagelist = acls.pagelist;
946 acls.pagelist = NULL;
948 err = ceph_mdsc_do_request(mdsc, dir, req);
950 !req->r_reply_info.head->is_target &&
951 !req->r_reply_info.head->is_dentry)
952 err = ceph_handle_notrace_create(dir, dentry);
953 ceph_mdsc_put_request(req);
956 ceph_init_inode_acls(d_inode(dentry), &acls);
959 ceph_release_acls_info(&acls);
963 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
964 struct dentry *dentry)
966 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
967 struct ceph_mds_client *mdsc = fsc->mdsc;
968 struct ceph_mds_request *req;
971 if (ceph_snap(dir) != CEPH_NOSNAP)
974 dout("link in dir %p old_dentry %p dentry %p\n", dir,
976 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
981 req->r_dentry = dget(dentry);
983 req->r_old_dentry = dget(old_dentry);
984 req->r_locked_dir = dir;
985 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
986 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
987 /* release LINK_SHARED on source inode (mds will lock it) */
988 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
989 err = ceph_mdsc_do_request(mdsc, dir, req);
992 } else if (!req->r_reply_info.head->is_dentry) {
993 ihold(d_inode(old_dentry));
994 d_instantiate(dentry, d_inode(old_dentry));
996 ceph_mdsc_put_request(req);
1001 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1002 * looks like the link count will hit 0, drop any other caps (other
1003 * than PIN) we don't specifically want (due to the file still being
1006 static int drop_caps_for_unlink(struct inode *inode)
1008 struct ceph_inode_info *ci = ceph_inode(inode);
1009 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1011 spin_lock(&ci->i_ceph_lock);
1012 if (inode->i_nlink == 1) {
1013 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1014 ci->i_ceph_flags |= CEPH_I_NODELAY;
1016 spin_unlock(&ci->i_ceph_lock);
1021 * rmdir and unlink are differ only by the metadata op code
1023 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1025 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1026 struct ceph_mds_client *mdsc = fsc->mdsc;
1027 struct inode *inode = d_inode(dentry);
1028 struct ceph_mds_request *req;
1032 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1033 /* rmdir .snap/foo is RMSNAP */
1034 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1035 op = CEPH_MDS_OP_RMSNAP;
1036 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1037 dout("unlink/rmdir dir %p dn %p inode %p\n",
1038 dir, dentry, inode);
1039 op = d_is_dir(dentry) ?
1040 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1043 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1048 req->r_dentry = dget(dentry);
1049 req->r_num_caps = 2;
1050 req->r_locked_dir = dir;
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)
1065 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1066 struct ceph_mds_client *mdsc = fsc->mdsc;
1067 struct ceph_mds_request *req;
1068 int op = CEPH_MDS_OP_RENAME;
1071 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1073 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1074 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1075 op = CEPH_MDS_OP_RENAMESNAP;
1079 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1080 old_dir, old_dentry, new_dir, new_dentry);
1081 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1083 return PTR_ERR(req);
1085 req->r_dentry = dget(new_dentry);
1086 req->r_num_caps = 2;
1087 req->r_old_dentry = dget(old_dentry);
1088 req->r_old_dentry_dir = old_dir;
1089 req->r_locked_dir = new_dir;
1090 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1091 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1092 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1093 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1094 /* release LINK_RDCACHE on source inode (mds will lock it) */
1095 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1096 if (d_really_is_positive(new_dentry))
1097 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
1098 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1099 if (!err && !req->r_reply_info.head->is_dentry) {
1101 * Normally d_move() is done by fill_trace (called by
1102 * do_request, above). If there is no trace, we need
1106 /* d_move screws up sibling dentries' offsets */
1107 ceph_dir_clear_complete(old_dir);
1108 ceph_dir_clear_complete(new_dir);
1110 d_move(old_dentry, new_dentry);
1112 /* ensure target dentry is invalidated, despite
1113 rehashing bug in vfs_rename_dir */
1114 ceph_invalidate_dentry_lease(new_dentry);
1116 ceph_mdsc_put_request(req);
1121 * Ensure a dentry lease will no longer revalidate.
1123 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1125 spin_lock(&dentry->d_lock);
1126 dentry->d_time = jiffies;
1127 ceph_dentry(dentry)->lease_shared_gen = 0;
1128 spin_unlock(&dentry->d_lock);
1132 * Check if dentry lease is valid. If not, delete the lease. Try to
1133 * renew if the least is more than half up.
1135 static int dentry_lease_is_valid(struct dentry *dentry)
1137 struct ceph_dentry_info *di;
1138 struct ceph_mds_session *s;
1142 struct ceph_mds_session *session = NULL;
1143 struct inode *dir = NULL;
1146 spin_lock(&dentry->d_lock);
1147 di = ceph_dentry(dentry);
1148 if (di->lease_session) {
1149 s = di->lease_session;
1150 spin_lock(&s->s_gen_ttl_lock);
1153 spin_unlock(&s->s_gen_ttl_lock);
1155 if (di->lease_gen == gen &&
1156 time_before(jiffies, dentry->d_time) &&
1157 time_before(jiffies, ttl)) {
1159 if (di->lease_renew_after &&
1160 time_after(jiffies, di->lease_renew_after)) {
1161 /* we should renew */
1162 dir = d_inode(dentry->d_parent);
1163 session = ceph_get_mds_session(s);
1164 seq = di->lease_seq;
1165 di->lease_renew_after = 0;
1166 di->lease_renew_from = jiffies;
1170 spin_unlock(&dentry->d_lock);
1173 ceph_mdsc_lease_send_msg(session, dir, dentry,
1174 CEPH_MDS_LEASE_RENEW, seq);
1175 ceph_put_mds_session(session);
1177 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1182 * Check if directory-wide content lease/cap is valid.
1184 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1186 struct ceph_inode_info *ci = ceph_inode(dir);
1187 struct ceph_dentry_info *di = ceph_dentry(dentry);
1190 spin_lock(&ci->i_ceph_lock);
1191 if (ci->i_shared_gen == di->lease_shared_gen)
1192 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1193 spin_unlock(&ci->i_ceph_lock);
1194 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1195 dir, (unsigned)ci->i_shared_gen, dentry,
1196 (unsigned)di->lease_shared_gen, valid);
1201 * Check if cached dentry can be trusted.
1203 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1206 struct dentry *parent;
1209 if (flags & LOOKUP_RCU)
1212 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1213 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1215 parent = dget_parent(dentry);
1216 dir = d_inode(parent);
1218 /* always trust cached snapped dentries, snapdir dentry */
1219 if (ceph_snap(dir) != CEPH_NOSNAP) {
1220 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1221 dentry, d_inode(dentry));
1223 } else if (d_really_is_positive(dentry) &&
1224 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1226 } else if (dentry_lease_is_valid(dentry) ||
1227 dir_lease_is_valid(dir, dentry)) {
1228 if (d_really_is_positive(dentry))
1229 valid = ceph_is_any_caps(d_inode(dentry));
1235 struct ceph_mds_client *mdsc =
1236 ceph_sb_to_client(dir->i_sb)->mdsc;
1237 struct ceph_mds_request *req;
1240 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1241 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1242 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1244 req->r_dentry = dget(dentry);
1245 req->r_num_caps = 2;
1247 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1248 if (ceph_security_xattr_wanted(dir))
1249 mask |= CEPH_CAP_XATTR_SHARED;
1250 req->r_args.getattr.mask = mask;
1252 req->r_locked_dir = dir;
1253 err = ceph_mdsc_do_request(mdsc, NULL, req);
1254 if (err == 0 || err == -ENOENT) {
1255 if (dentry == req->r_dentry) {
1256 valid = !d_unhashed(dentry);
1258 d_invalidate(req->r_dentry);
1262 ceph_mdsc_put_request(req);
1263 dout("d_revalidate %p lookup result=%d\n",
1268 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1270 ceph_dentry_lru_touch(dentry);
1272 ceph_dir_clear_complete(dir);
1280 * Release our ceph_dentry_info.
1282 static void ceph_d_release(struct dentry *dentry)
1284 struct ceph_dentry_info *di = ceph_dentry(dentry);
1286 dout("d_release %p\n", dentry);
1287 ceph_dentry_lru_del(dentry);
1288 if (di->lease_session)
1289 ceph_put_mds_session(di->lease_session);
1290 kmem_cache_free(ceph_dentry_cachep, di);
1291 dentry->d_fsdata = NULL;
1294 static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1298 * Eventually, we'll want to revalidate snapped metadata
1299 * too... probably...
1305 * When the VFS prunes a dentry from the cache, we need to clear the
1306 * complete flag on the parent directory.
1308 * Called under dentry->d_lock.
1310 static void ceph_d_prune(struct dentry *dentry)
1312 dout("ceph_d_prune %p\n", dentry);
1314 /* do we have a valid parent? */
1315 if (IS_ROOT(dentry))
1318 /* if we are not hashed, we don't affect dir's completeness */
1319 if (d_unhashed(dentry))
1323 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1324 * cleared until d_release
1326 ceph_dir_clear_complete(d_inode(dentry->d_parent));
1330 * read() on a dir. This weird interface hack only works if mounted
1331 * with '-o dirstat'.
1333 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1336 struct ceph_file_info *cf = file->private_data;
1337 struct inode *inode = file_inode(file);
1338 struct ceph_inode_info *ci = ceph_inode(inode);
1340 const int bufsize = 1024;
1342 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1345 if (!cf->dir_info) {
1346 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1350 snprintf(cf->dir_info, bufsize,
1353 " subdirs: %20lld\n"
1354 "rentries: %20lld\n"
1356 " rsubdirs: %20lld\n"
1358 "rctime: %10ld.%09ld\n",
1359 ci->i_files + ci->i_subdirs,
1362 ci->i_rfiles + ci->i_rsubdirs,
1366 (long)ci->i_rctime.tv_sec,
1367 (long)ci->i_rctime.tv_nsec);
1370 if (*ppos >= cf->dir_info_len)
1372 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1373 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1376 *ppos += (size - left);
1381 * We maintain a private dentry LRU.
1383 * FIXME: this needs to be changed to a per-mds lru to be useful.
1385 void ceph_dentry_lru_add(struct dentry *dn)
1387 struct ceph_dentry_info *di = ceph_dentry(dn);
1388 struct ceph_mds_client *mdsc;
1390 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1391 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1392 spin_lock(&mdsc->dentry_lru_lock);
1393 list_add_tail(&di->lru, &mdsc->dentry_lru);
1395 spin_unlock(&mdsc->dentry_lru_lock);
1398 void ceph_dentry_lru_touch(struct dentry *dn)
1400 struct ceph_dentry_info *di = ceph_dentry(dn);
1401 struct ceph_mds_client *mdsc;
1403 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1405 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1406 spin_lock(&mdsc->dentry_lru_lock);
1407 list_move_tail(&di->lru, &mdsc->dentry_lru);
1408 spin_unlock(&mdsc->dentry_lru_lock);
1411 void ceph_dentry_lru_del(struct dentry *dn)
1413 struct ceph_dentry_info *di = ceph_dentry(dn);
1414 struct ceph_mds_client *mdsc;
1416 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1417 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1418 spin_lock(&mdsc->dentry_lru_lock);
1419 list_del_init(&di->lru);
1421 spin_unlock(&mdsc->dentry_lru_lock);
1425 * Return name hash for a given dentry. This is dependent on
1426 * the parent directory's hash function.
1428 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1430 struct ceph_inode_info *dci = ceph_inode(dir);
1432 switch (dci->i_dir_layout.dl_dir_hash) {
1433 case 0: /* for backward compat */
1434 case CEPH_STR_HASH_LINUX:
1435 return dn->d_name.hash;
1438 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1439 dn->d_name.name, dn->d_name.len);
1443 const struct file_operations ceph_dir_fops = {
1444 .read = ceph_read_dir,
1445 .iterate = ceph_readdir,
1446 .llseek = ceph_dir_llseek,
1448 .release = ceph_release,
1449 .unlocked_ioctl = ceph_ioctl,
1450 .fsync = ceph_fsync,
1453 const struct file_operations ceph_snapdir_fops = {
1454 .iterate = ceph_readdir,
1455 .llseek = ceph_dir_llseek,
1457 .release = ceph_release,
1460 const struct inode_operations ceph_dir_iops = {
1461 .lookup = ceph_lookup,
1462 .permission = ceph_permission,
1463 .getattr = ceph_getattr,
1464 .setattr = ceph_setattr,
1465 .setxattr = ceph_setxattr,
1466 .getxattr = ceph_getxattr,
1467 .listxattr = ceph_listxattr,
1468 .removexattr = ceph_removexattr,
1469 .get_acl = ceph_get_acl,
1470 .set_acl = ceph_set_acl,
1471 .mknod = ceph_mknod,
1472 .symlink = ceph_symlink,
1473 .mkdir = ceph_mkdir,
1475 .unlink = ceph_unlink,
1476 .rmdir = ceph_unlink,
1477 .rename = ceph_rename,
1478 .create = ceph_create,
1479 .atomic_open = ceph_atomic_open,
1482 const struct inode_operations ceph_snapdir_iops = {
1483 .lookup = ceph_lookup,
1484 .permission = ceph_permission,
1485 .getattr = ceph_getattr,
1486 .mkdir = ceph_mkdir,
1487 .rmdir = ceph_unlink,
1488 .rename = ceph_rename,
1491 const struct dentry_operations ceph_dentry_ops = {
1492 .d_revalidate = ceph_d_revalidate,
1493 .d_release = ceph_d_release,
1494 .d_prune = ceph_d_prune,
1497 const struct dentry_operations ceph_snapdir_dentry_ops = {
1498 .d_revalidate = ceph_snapdir_d_revalidate,
1499 .d_release = ceph_d_release,
1502 const struct dentry_operations ceph_snap_dentry_ops = {
1503 .d_release = ceph_d_release,
1504 .d_prune = ceph_d_prune,