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>
8 #include <linux/xattr.h>
11 #include "mds_client.h"
14 * Directory operations: readdir, lookup, create, link, unlink,
19 * Ceph MDS operations are specified in terms of a base ino and
20 * relative path. Thus, the client can specify an operation on a
21 * specific inode (e.g., a getattr due to fstat(2)), or as a path
22 * relative to, say, the root directory.
24 * Normally, we limit ourselves to strict inode ops (no path component)
25 * or dentry operations (a single path component relative to an ino). The
26 * exception to this is open_root_dentry(), which will open the mount
30 const struct dentry_operations ceph_dentry_ops;
33 * Initialize ceph dentry state.
35 static int ceph_d_init(struct dentry *dentry)
37 struct ceph_dentry_info *di;
39 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
41 return -ENOMEM; /* oh well */
44 di->lease_session = NULL;
46 dentry->d_fsdata = di;
47 ceph_dentry_lru_add(dentry);
52 * for f_pos for readdir:
54 * (0xff << 52) | ((24 bits hash) << 28) |
55 * (the nth entry has hash collision);
57 * ((frag value) << 28) | (the nth entry in frag);
59 #define OFFSET_BITS 28
60 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
61 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
62 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
64 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
70 static bool is_hash_order(loff_t p)
72 return (p & HASH_ORDER) == HASH_ORDER;
75 static unsigned fpos_frag(loff_t p)
77 return p >> OFFSET_BITS;
80 static unsigned fpos_hash(loff_t p)
82 return ceph_frag_value(fpos_frag(p));
85 static unsigned fpos_off(loff_t p)
87 return p & OFFSET_MASK;
90 static int fpos_cmp(loff_t l, loff_t r)
92 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
95 return (int)(fpos_off(l) - fpos_off(r));
99 * make note of the last dentry we read, so we can
100 * continue at the same lexicographical point,
101 * regardless of what dir changes take place on the
104 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
105 int len, unsigned next_offset)
107 char *buf = kmalloc(len+1, GFP_KERNEL);
110 kfree(fi->last_name);
112 memcpy(fi->last_name, name, len);
113 fi->last_name[len] = 0;
114 fi->next_offset = next_offset;
115 dout("note_last_dentry '%s'\n", fi->last_name);
120 static struct dentry *
121 __dcache_find_get_entry(struct dentry *parent, u64 idx,
122 struct ceph_readdir_cache_control *cache_ctl)
124 struct inode *dir = d_inode(parent);
125 struct dentry *dentry;
126 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
127 loff_t ptr_pos = idx * sizeof(struct dentry *);
128 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
130 if (ptr_pos >= i_size_read(dir))
133 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
134 ceph_readdir_cache_release(cache_ctl);
135 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
136 if (!cache_ctl->page) {
137 dout(" page %lu not found\n", ptr_pgoff);
138 return ERR_PTR(-EAGAIN);
140 /* reading/filling the cache are serialized by
141 i_mutex, no need to use page lock */
142 unlock_page(cache_ctl->page);
143 cache_ctl->dentries = kmap(cache_ctl->page);
146 cache_ctl->index = idx & idx_mask;
149 spin_lock(&parent->d_lock);
150 /* check i_size again here, because empty directory can be
151 * marked as complete while not holding the i_mutex. */
152 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
153 dentry = cache_ctl->dentries[cache_ctl->index];
156 spin_unlock(&parent->d_lock);
157 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
160 return dentry ? : ERR_PTR(-EAGAIN);
164 * When possible, we try to satisfy a readdir by peeking at the
165 * dcache. We make this work by carefully ordering dentries on
166 * d_child when we initially get results back from the MDS, and
167 * falling back to a "normal" sync readdir if any dentries in the dir
170 * Complete dir indicates that we have all dentries in the dir. It is
171 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
172 * the MDS if/when the directory is modified).
174 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
177 struct ceph_file_info *fi = file->private_data;
178 struct dentry *parent = file->f_path.dentry;
179 struct inode *dir = d_inode(parent);
180 struct dentry *dentry, *last = NULL;
181 struct ceph_dentry_info *di;
182 struct ceph_readdir_cache_control cache_ctl = {};
186 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
188 /* search start position */
190 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
192 u64 step = count >> 1;
193 dentry = __dcache_find_get_entry(parent, idx + step,
196 /* use linar search */
200 if (IS_ERR(dentry)) {
201 err = PTR_ERR(dentry);
204 di = ceph_dentry(dentry);
205 spin_lock(&dentry->d_lock);
206 if (fpos_cmp(di->offset, ctx->pos) < 0) {
212 spin_unlock(&dentry->d_lock);
216 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
221 bool emit_dentry = false;
222 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
224 fi->flags |= CEPH_F_ATEND;
228 if (IS_ERR(dentry)) {
229 err = PTR_ERR(dentry);
233 di = ceph_dentry(dentry);
234 spin_lock(&dentry->d_lock);
235 if (di->lease_shared_gen == shared_gen &&
236 d_really_is_positive(dentry) &&
237 fpos_cmp(ctx->pos, di->offset) <= 0) {
240 spin_unlock(&dentry->d_lock);
243 dout(" %llx dentry %p %pd %p\n", di->offset,
244 dentry, dentry, d_inode(dentry));
245 ctx->pos = di->offset;
246 if (!dir_emit(ctx, dentry->d_name.name,
248 ceph_translate_ino(dentry->d_sb,
249 d_inode(dentry)->i_ino),
250 d_inode(dentry)->i_mode >> 12)) {
265 ceph_readdir_cache_release(&cache_ctl);
268 di = ceph_dentry(last);
269 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
270 fpos_off(di->offset) + 1);
274 /* last_name no longer match cache index */
275 if (fi->readdir_cache_idx >= 0) {
276 fi->readdir_cache_idx = -1;
277 fi->dir_release_count = 0;
283 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
285 if (!fi->last_readdir)
287 if (is_hash_order(pos))
288 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
290 return fi->frag != fpos_frag(pos);
293 static int ceph_readdir(struct file *file, struct dir_context *ctx)
295 struct ceph_file_info *fi = file->private_data;
296 struct inode *inode = file_inode(file);
297 struct ceph_inode_info *ci = ceph_inode(inode);
298 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
299 struct ceph_mds_client *mdsc = fsc->mdsc;
303 struct ceph_mds_reply_info_parsed *rinfo;
305 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
306 if (fi->flags & CEPH_F_ATEND)
309 /* always start with . and .. */
311 dout("readdir off 0 -> '.'\n");
312 if (!dir_emit(ctx, ".", 1,
313 ceph_translate_ino(inode->i_sb, inode->i_ino),
314 inode->i_mode >> 12))
319 ino_t ino = parent_ino(file->f_path.dentry);
320 dout("readdir off 1 -> '..'\n");
321 if (!dir_emit(ctx, "..", 2,
322 ceph_translate_ino(inode->i_sb, ino),
323 inode->i_mode >> 12))
328 /* can we use the dcache? */
329 spin_lock(&ci->i_ceph_lock);
330 if (ceph_test_mount_opt(fsc, DCACHE) &&
331 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
332 ceph_snap(inode) != CEPH_SNAPDIR &&
333 __ceph_dir_is_complete_ordered(ci) &&
334 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
335 u32 shared_gen = ci->i_shared_gen;
336 spin_unlock(&ci->i_ceph_lock);
337 err = __dcache_readdir(file, ctx, shared_gen);
341 spin_unlock(&ci->i_ceph_lock);
344 /* proceed with a normal readdir */
346 /* do we have the correct frag content buffered? */
347 if (need_send_readdir(fi, ctx->pos)) {
348 struct ceph_mds_request *req;
349 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
350 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
352 /* discard old result, if any */
353 if (fi->last_readdir) {
354 ceph_mdsc_put_request(fi->last_readdir);
355 fi->last_readdir = NULL;
358 if (is_hash_order(ctx->pos)) {
359 /* fragtree isn't always accurate. choose frag
360 * based on previous reply when possible. */
361 if (frag == (unsigned)-1)
362 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
365 frag = fpos_frag(ctx->pos);
368 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
369 ceph_vinop(inode), frag, fi->last_name);
370 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
373 err = ceph_alloc_readdir_reply_buffer(req, inode);
375 ceph_mdsc_put_request(req);
378 /* hints to request -> mds selection code */
379 req->r_direct_mode = USE_AUTH_MDS;
380 if (op == CEPH_MDS_OP_READDIR) {
381 req->r_direct_hash = ceph_frag_value(frag);
382 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
385 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
387 ceph_mdsc_put_request(req);
390 } else if (is_hash_order(ctx->pos)) {
391 req->r_args.readdir.offset_hash =
392 cpu_to_le32(fpos_hash(ctx->pos));
395 req->r_dir_release_cnt = fi->dir_release_count;
396 req->r_dir_ordered_cnt = fi->dir_ordered_count;
397 req->r_readdir_cache_idx = fi->readdir_cache_idx;
398 req->r_readdir_offset = fi->next_offset;
399 req->r_args.readdir.frag = cpu_to_le32(frag);
400 req->r_args.readdir.flags =
401 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
403 req->r_inode = inode;
405 req->r_dentry = dget(file->f_path.dentry);
406 err = ceph_mdsc_do_request(mdsc, NULL, req);
408 ceph_mdsc_put_request(req);
411 dout("readdir got and parsed readdir result=%d on "
412 "frag %x, end=%d, complete=%d, hash_order=%d\n",
414 (int)req->r_reply_info.dir_end,
415 (int)req->r_reply_info.dir_complete,
416 (int)req->r_reply_info.hash_order);
418 rinfo = &req->r_reply_info;
419 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
420 frag = le32_to_cpu(rinfo->dir_dir->frag);
421 if (!rinfo->hash_order) {
422 fi->next_offset = req->r_readdir_offset;
423 /* adjust ctx->pos to beginning of frag */
424 ctx->pos = ceph_make_fpos(frag,
431 fi->last_readdir = req;
433 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
434 fi->readdir_cache_idx = req->r_readdir_cache_idx;
435 if (fi->readdir_cache_idx < 0) {
436 /* preclude from marking dir ordered */
437 fi->dir_ordered_count = 0;
438 } else if (ceph_frag_is_leftmost(frag) &&
439 fi->next_offset == 2) {
440 /* note dir version at start of readdir so
441 * we can tell if any dentries get dropped */
442 fi->dir_release_count = req->r_dir_release_cnt;
443 fi->dir_ordered_count = req->r_dir_ordered_cnt;
446 dout("readdir !did_prepopulate");
447 /* disable readdir cache */
448 fi->readdir_cache_idx = -1;
449 /* preclude from marking dir complete */
450 fi->dir_release_count = 0;
453 /* note next offset and last dentry name */
454 if (rinfo->dir_nr > 0) {
455 struct ceph_mds_reply_dir_entry *rde =
456 rinfo->dir_entries + (rinfo->dir_nr-1);
457 unsigned next_offset = req->r_reply_info.dir_end ?
458 2 : (fpos_off(rde->offset) + 1);
459 err = note_last_dentry(fi, rde->name, rde->name_len,
463 } else if (req->r_reply_info.dir_end) {
469 rinfo = &fi->last_readdir->r_reply_info;
470 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
471 fi->frag, rinfo->dir_nr, ctx->pos,
472 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
475 /* search start position */
476 if (rinfo->dir_nr > 0) {
477 int step, nr = rinfo->dir_nr;
480 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
488 for (; i < rinfo->dir_nr; i++) {
489 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
490 struct ceph_vino vino;
494 BUG_ON(rde->offset < ctx->pos);
496 ctx->pos = rde->offset;
497 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
498 i, rinfo->dir_nr, ctx->pos,
499 rde->name_len, rde->name, &rde->inode.in);
501 BUG_ON(!rde->inode.in);
502 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
503 vino.ino = le64_to_cpu(rde->inode.in->ino);
504 vino.snap = le64_to_cpu(rde->inode.in->snapid);
505 ino = ceph_vino_to_ino(vino);
507 if (!dir_emit(ctx, rde->name, rde->name_len,
508 ceph_translate_ino(inode->i_sb, ino), ftype)) {
509 dout("filldir stopping us...\n");
515 ceph_mdsc_put_request(fi->last_readdir);
516 fi->last_readdir = NULL;
518 if (fi->next_offset > 2) {
524 if (!ceph_frag_is_rightmost(fi->frag)) {
525 frag = ceph_frag_next(fi->frag);
526 if (is_hash_order(ctx->pos)) {
527 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
528 fi->next_offset, true);
529 if (new_pos > ctx->pos)
533 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
534 kfree(fi->last_name);
535 fi->last_name = NULL;
537 dout("readdir next frag is %x\n", frag);
540 fi->flags |= CEPH_F_ATEND;
543 * if dir_release_count still matches the dir, no dentries
544 * were released during the whole readdir, and we should have
545 * the complete dir contents in our cache.
547 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
548 spin_lock(&ci->i_ceph_lock);
549 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
550 dout(" marking %p complete and ordered\n", inode);
551 /* use i_size to track number of entries in
553 BUG_ON(fi->readdir_cache_idx < 0);
554 i_size_write(inode, fi->readdir_cache_idx *
555 sizeof(struct dentry*));
557 dout(" marking %p complete\n", inode);
559 __ceph_dir_set_complete(ci, fi->dir_release_count,
560 fi->dir_ordered_count);
561 spin_unlock(&ci->i_ceph_lock);
564 dout("readdir %p file %p done.\n", inode, file);
568 static void reset_readdir(struct ceph_file_info *fi)
570 if (fi->last_readdir) {
571 ceph_mdsc_put_request(fi->last_readdir);
572 fi->last_readdir = NULL;
574 kfree(fi->last_name);
575 fi->last_name = NULL;
576 fi->dir_release_count = 0;
577 fi->readdir_cache_idx = -1;
578 fi->next_offset = 2; /* compensate for . and .. */
579 fi->flags &= ~CEPH_F_ATEND;
583 * discard buffered readdir content on seekdir(0), or seek to new frag,
584 * or seek prior to current chunk
586 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
588 struct ceph_mds_reply_info_parsed *rinfo;
592 if (is_hash_order(new_pos)) {
593 /* no need to reset last_name for a forward seek when
594 * dentries are sotred in hash order */
595 } else if (fi->frag != fpos_frag(new_pos)) {
598 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
599 if (!rinfo || !rinfo->dir_nr)
601 chunk_offset = rinfo->dir_entries[0].offset;
602 return new_pos < chunk_offset ||
603 is_hash_order(new_pos) != is_hash_order(chunk_offset);
606 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
608 struct ceph_file_info *fi = file->private_data;
609 struct inode *inode = file->f_mapping->host;
616 offset += file->f_pos;
620 retval = -EOPNOTSUPP;
626 if (need_reset_readdir(fi, offset)) {
627 dout("dir_llseek dropping %p content\n", file);
629 } else if (is_hash_order(offset) && offset > file->f_pos) {
630 /* for hash offset, we don't know if a forward seek
631 * is within same frag */
632 fi->dir_release_count = 0;
633 fi->readdir_cache_idx = -1;
636 if (offset != file->f_pos) {
637 file->f_pos = offset;
639 fi->flags &= ~CEPH_F_ATEND;
649 * Handle lookups for the hidden .snap directory.
651 int ceph_handle_snapdir(struct ceph_mds_request *req,
652 struct dentry *dentry, int err)
654 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
655 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
658 if (err == -ENOENT &&
659 ceph_snap(parent) == CEPH_NOSNAP &&
660 strcmp(dentry->d_name.name,
661 fsc->mount_options->snapdir_name) == 0) {
662 struct inode *inode = ceph_get_snapdir(parent);
663 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
664 dentry, dentry, inode);
665 BUG_ON(!d_unhashed(dentry));
666 d_add(dentry, inode);
673 * Figure out final result of a lookup/open request.
675 * Mainly, make sure we return the final req->r_dentry (if it already
676 * existed) in place of the original VFS-provided dentry when they
679 * Gracefully handle the case where the MDS replies with -ENOENT and
680 * no trace (which it may do, at its discretion, e.g., if it doesn't
681 * care to issue a lease on the negative dentry).
683 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
684 struct dentry *dentry, int err)
686 if (err == -ENOENT) {
689 if (!req->r_reply_info.head->is_dentry) {
690 dout("ENOENT and no trace, dentry %p inode %p\n",
691 dentry, d_inode(dentry));
692 if (d_really_is_positive(dentry)) {
701 dentry = ERR_PTR(err);
702 else if (dentry != req->r_dentry)
703 dentry = dget(req->r_dentry); /* we got spliced */
709 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
711 return ceph_ino(inode) == CEPH_INO_ROOT &&
712 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
716 * Look up a single dir entry. If there is a lookup intent, inform
717 * the MDS so that it gets our 'caps wanted' value in a single op.
719 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
722 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
723 struct ceph_mds_client *mdsc = fsc->mdsc;
724 struct ceph_mds_request *req;
729 dout("lookup %p dentry %p '%pd'\n",
730 dir, dentry, dentry);
732 if (dentry->d_name.len > NAME_MAX)
733 return ERR_PTR(-ENAMETOOLONG);
735 /* can we conclude ENOENT locally? */
736 if (d_really_is_negative(dentry)) {
737 struct ceph_inode_info *ci = ceph_inode(dir);
738 struct ceph_dentry_info *di = ceph_dentry(dentry);
740 spin_lock(&ci->i_ceph_lock);
741 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
742 if (strncmp(dentry->d_name.name,
743 fsc->mount_options->snapdir_name,
744 dentry->d_name.len) &&
745 !is_root_ceph_dentry(dir, dentry) &&
746 ceph_test_mount_opt(fsc, DCACHE) &&
747 __ceph_dir_is_complete(ci) &&
748 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
749 spin_unlock(&ci->i_ceph_lock);
750 dout(" dir %p complete, -ENOENT\n", dir);
752 di->lease_shared_gen = ci->i_shared_gen;
755 spin_unlock(&ci->i_ceph_lock);
758 op = ceph_snap(dir) == CEPH_SNAPDIR ?
759 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
760 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
762 return ERR_CAST(req);
763 req->r_dentry = dget(dentry);
766 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
767 if (ceph_security_xattr_wanted(dir))
768 mask |= CEPH_CAP_XATTR_SHARED;
769 req->r_args.getattr.mask = cpu_to_le32(mask);
772 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
773 err = ceph_mdsc_do_request(mdsc, NULL, req);
774 err = ceph_handle_snapdir(req, dentry, err);
775 dentry = ceph_finish_lookup(req, dentry, err);
776 ceph_mdsc_put_request(req); /* will dput(dentry) */
777 dout("lookup result=%p\n", dentry);
782 * If we do a create but get no trace back from the MDS, follow up with
783 * a lookup (the VFS expects us to link up the provided dentry).
785 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
787 struct dentry *result = ceph_lookup(dir, dentry, 0);
789 if (result && !IS_ERR(result)) {
791 * We created the item, then did a lookup, and found
792 * it was already linked to another inode we already
793 * had in our cache (and thus got spliced). To not
794 * confuse VFS (especially when inode is a directory),
795 * we don't link our dentry to that inode, return an
798 * This event should be rare and it happens only when
799 * we talk to old MDS. Recent MDS does not send traceless
800 * reply for request that creates new inode.
805 return PTR_ERR(result);
808 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
809 umode_t mode, dev_t rdev)
811 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
812 struct ceph_mds_client *mdsc = fsc->mdsc;
813 struct ceph_mds_request *req;
814 struct ceph_acls_info acls = {};
817 if (ceph_snap(dir) != CEPH_NOSNAP)
820 err = ceph_pre_init_acls(dir, &mode, &acls);
824 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
825 dir, dentry, mode, rdev);
826 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
831 req->r_dentry = dget(dentry);
834 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
835 req->r_args.mknod.mode = cpu_to_le32(mode);
836 req->r_args.mknod.rdev = cpu_to_le32(rdev);
837 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
838 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
840 req->r_pagelist = acls.pagelist;
841 acls.pagelist = NULL;
843 err = ceph_mdsc_do_request(mdsc, dir, req);
844 if (!err && !req->r_reply_info.head->is_dentry)
845 err = ceph_handle_notrace_create(dir, dentry);
846 ceph_mdsc_put_request(req);
849 ceph_init_inode_acls(d_inode(dentry), &acls);
852 ceph_release_acls_info(&acls);
856 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
859 return ceph_mknod(dir, dentry, mode, 0);
862 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
865 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
866 struct ceph_mds_client *mdsc = fsc->mdsc;
867 struct ceph_mds_request *req;
870 if (ceph_snap(dir) != CEPH_NOSNAP)
873 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
874 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
879 req->r_path2 = kstrdup(dest, GFP_KERNEL);
882 ceph_mdsc_put_request(req);
886 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
887 req->r_dentry = dget(dentry);
889 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
890 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
891 err = ceph_mdsc_do_request(mdsc, dir, req);
892 if (!err && !req->r_reply_info.head->is_dentry)
893 err = ceph_handle_notrace_create(dir, dentry);
894 ceph_mdsc_put_request(req);
901 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
903 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
904 struct ceph_mds_client *mdsc = fsc->mdsc;
905 struct ceph_mds_request *req;
906 struct ceph_acls_info acls = {};
910 if (ceph_snap(dir) == CEPH_SNAPDIR) {
911 /* mkdir .snap/foo is a MKSNAP */
912 op = CEPH_MDS_OP_MKSNAP;
913 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
915 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
916 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
917 op = CEPH_MDS_OP_MKDIR;
923 err = ceph_pre_init_acls(dir, &mode, &acls);
927 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
933 req->r_dentry = dget(dentry);
936 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
937 req->r_args.mkdir.mode = cpu_to_le32(mode);
938 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
939 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
941 req->r_pagelist = acls.pagelist;
942 acls.pagelist = NULL;
944 err = ceph_mdsc_do_request(mdsc, dir, req);
946 !req->r_reply_info.head->is_target &&
947 !req->r_reply_info.head->is_dentry)
948 err = ceph_handle_notrace_create(dir, dentry);
949 ceph_mdsc_put_request(req);
952 ceph_init_inode_acls(d_inode(dentry), &acls);
955 ceph_release_acls_info(&acls);
959 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
960 struct dentry *dentry)
962 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
963 struct ceph_mds_client *mdsc = fsc->mdsc;
964 struct ceph_mds_request *req;
967 if (ceph_snap(dir) != CEPH_NOSNAP)
970 dout("link in dir %p old_dentry %p dentry %p\n", dir,
972 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
977 req->r_dentry = dget(dentry);
979 req->r_old_dentry = dget(old_dentry);
981 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
982 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
983 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
984 /* release LINK_SHARED on source inode (mds will lock it) */
985 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
986 err = ceph_mdsc_do_request(mdsc, dir, req);
989 } else if (!req->r_reply_info.head->is_dentry) {
990 ihold(d_inode(old_dentry));
991 d_instantiate(dentry, d_inode(old_dentry));
993 ceph_mdsc_put_request(req);
998 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
999 * looks like the link count will hit 0, drop any other caps (other
1000 * than PIN) we don't specifically want (due to the file still being
1003 static int drop_caps_for_unlink(struct inode *inode)
1005 struct ceph_inode_info *ci = ceph_inode(inode);
1006 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1008 spin_lock(&ci->i_ceph_lock);
1009 if (inode->i_nlink == 1) {
1010 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1011 ci->i_ceph_flags |= CEPH_I_NODELAY;
1013 spin_unlock(&ci->i_ceph_lock);
1018 * rmdir and unlink are differ only by the metadata op code
1020 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1022 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1023 struct ceph_mds_client *mdsc = fsc->mdsc;
1024 struct inode *inode = d_inode(dentry);
1025 struct ceph_mds_request *req;
1029 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1030 /* rmdir .snap/foo is RMSNAP */
1031 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1032 op = CEPH_MDS_OP_RMSNAP;
1033 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1034 dout("unlink/rmdir dir %p dn %p inode %p\n",
1035 dir, dentry, inode);
1036 op = d_is_dir(dentry) ?
1037 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1040 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1045 req->r_dentry = dget(dentry);
1046 req->r_num_caps = 2;
1047 req->r_parent = dir;
1048 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1049 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1050 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1051 req->r_inode_drop = drop_caps_for_unlink(inode);
1052 err = ceph_mdsc_do_request(mdsc, dir, req);
1053 if (!err && !req->r_reply_info.head->is_dentry)
1055 ceph_mdsc_put_request(req);
1060 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1061 struct inode *new_dir, struct dentry *new_dentry,
1064 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1065 struct ceph_mds_client *mdsc = fsc->mdsc;
1066 struct ceph_mds_request *req;
1067 int op = CEPH_MDS_OP_RENAME;
1073 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1075 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1076 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1077 op = CEPH_MDS_OP_RENAMESNAP;
1081 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1082 old_dir, old_dentry, new_dir, new_dentry);
1083 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1085 return PTR_ERR(req);
1087 req->r_dentry = dget(new_dentry);
1088 req->r_num_caps = 2;
1089 req->r_old_dentry = dget(old_dentry);
1090 req->r_old_dentry_dir = old_dir;
1091 req->r_parent = new_dir;
1092 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1093 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1094 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1095 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1096 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1097 /* release LINK_RDCACHE on source inode (mds will lock it) */
1098 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1099 if (d_really_is_positive(new_dentry))
1100 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
1101 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1102 if (!err && !req->r_reply_info.head->is_dentry) {
1104 * Normally d_move() is done by fill_trace (called by
1105 * do_request, above). If there is no trace, we need
1109 /* d_move screws up sibling dentries' offsets */
1110 ceph_dir_clear_complete(old_dir);
1111 ceph_dir_clear_complete(new_dir);
1113 d_move(old_dentry, new_dentry);
1115 /* ensure target dentry is invalidated, despite
1116 rehashing bug in vfs_rename_dir */
1117 ceph_invalidate_dentry_lease(new_dentry);
1119 ceph_mdsc_put_request(req);
1124 * Ensure a dentry lease will no longer revalidate.
1126 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1128 spin_lock(&dentry->d_lock);
1129 ceph_dentry(dentry)->time = jiffies;
1130 ceph_dentry(dentry)->lease_shared_gen = 0;
1131 spin_unlock(&dentry->d_lock);
1135 * Check if dentry lease is valid. If not, delete the lease. Try to
1136 * renew if the least is more than half up.
1138 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1141 struct ceph_dentry_info *di;
1142 struct ceph_mds_session *s;
1146 struct ceph_mds_session *session = NULL;
1149 spin_lock(&dentry->d_lock);
1150 di = ceph_dentry(dentry);
1151 if (di && di->lease_session) {
1152 s = di->lease_session;
1153 spin_lock(&s->s_gen_ttl_lock);
1156 spin_unlock(&s->s_gen_ttl_lock);
1158 if (di->lease_gen == gen &&
1159 time_before(jiffies, di->time) &&
1160 time_before(jiffies, ttl)) {
1162 if (di->lease_renew_after &&
1163 time_after(jiffies, di->lease_renew_after)) {
1165 * We should renew. If we're in RCU walk mode
1166 * though, we can't do that so just return
1169 if (flags & LOOKUP_RCU) {
1172 session = ceph_get_mds_session(s);
1173 seq = di->lease_seq;
1174 di->lease_renew_after = 0;
1175 di->lease_renew_from = jiffies;
1180 spin_unlock(&dentry->d_lock);
1183 ceph_mdsc_lease_send_msg(session, dir, dentry,
1184 CEPH_MDS_LEASE_RENEW, seq);
1185 ceph_put_mds_session(session);
1187 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1192 * Check if directory-wide content lease/cap is valid.
1194 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1196 struct ceph_inode_info *ci = ceph_inode(dir);
1197 struct ceph_dentry_info *di = ceph_dentry(dentry);
1200 spin_lock(&ci->i_ceph_lock);
1201 if (ci->i_shared_gen == di->lease_shared_gen)
1202 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1203 spin_unlock(&ci->i_ceph_lock);
1204 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1205 dir, (unsigned)ci->i_shared_gen, dentry,
1206 (unsigned)di->lease_shared_gen, valid);
1211 * Check if cached dentry can be trusted.
1213 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1216 struct dentry *parent;
1219 if (flags & LOOKUP_RCU) {
1220 parent = READ_ONCE(dentry->d_parent);
1221 dir = d_inode_rcu(parent);
1225 parent = dget_parent(dentry);
1226 dir = d_inode(parent);
1229 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1230 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1232 /* always trust cached snapped dentries, snapdir dentry */
1233 if (ceph_snap(dir) != CEPH_NOSNAP) {
1234 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1235 dentry, d_inode(dentry));
1237 } else if (d_really_is_positive(dentry) &&
1238 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1241 valid = dentry_lease_is_valid(dentry, flags, dir);
1242 if (valid == -ECHILD)
1244 if (valid || dir_lease_is_valid(dir, dentry)) {
1245 if (d_really_is_positive(dentry))
1246 valid = ceph_is_any_caps(d_inode(dentry));
1253 struct ceph_mds_client *mdsc =
1254 ceph_sb_to_client(dir->i_sb)->mdsc;
1255 struct ceph_mds_request *req;
1259 if (flags & LOOKUP_RCU)
1262 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1263 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1264 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1266 req->r_dentry = dget(dentry);
1267 req->r_num_caps = 2;
1268 req->r_parent = dir;
1270 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1271 if (ceph_security_xattr_wanted(dir))
1272 mask |= CEPH_CAP_XATTR_SHARED;
1273 req->r_args.getattr.mask = cpu_to_le32(mask);
1275 err = ceph_mdsc_do_request(mdsc, NULL, req);
1278 if (d_really_is_positive(dentry) &&
1279 d_inode(dentry) == req->r_target_inode)
1283 if (d_really_is_negative(dentry))
1289 ceph_mdsc_put_request(req);
1290 dout("d_revalidate %p lookup result=%d\n",
1295 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1297 ceph_dentry_lru_touch(dentry);
1299 ceph_dir_clear_complete(dir);
1302 if (!(flags & LOOKUP_RCU))
1308 * Release our ceph_dentry_info.
1310 static void ceph_d_release(struct dentry *dentry)
1312 struct ceph_dentry_info *di = ceph_dentry(dentry);
1314 dout("d_release %p\n", dentry);
1315 ceph_dentry_lru_del(dentry);
1317 spin_lock(&dentry->d_lock);
1318 dentry->d_fsdata = NULL;
1319 spin_unlock(&dentry->d_lock);
1321 if (di->lease_session)
1322 ceph_put_mds_session(di->lease_session);
1323 kmem_cache_free(ceph_dentry_cachep, di);
1327 * When the VFS prunes a dentry from the cache, we need to clear the
1328 * complete flag on the parent directory.
1330 * Called under dentry->d_lock.
1332 static void ceph_d_prune(struct dentry *dentry)
1334 dout("ceph_d_prune %p\n", dentry);
1336 /* do we have a valid parent? */
1337 if (IS_ROOT(dentry))
1340 /* if we are not hashed, we don't affect dir's completeness */
1341 if (d_unhashed(dentry))
1344 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
1348 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1349 * cleared until d_release
1351 ceph_dir_clear_complete(d_inode(dentry->d_parent));
1355 * read() on a dir. This weird interface hack only works if mounted
1356 * with '-o dirstat'.
1358 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1361 struct ceph_file_info *cf = file->private_data;
1362 struct inode *inode = file_inode(file);
1363 struct ceph_inode_info *ci = ceph_inode(inode);
1365 const int bufsize = 1024;
1367 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1370 if (!cf->dir_info) {
1371 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1375 snprintf(cf->dir_info, bufsize,
1378 " subdirs: %20lld\n"
1379 "rentries: %20lld\n"
1381 " rsubdirs: %20lld\n"
1383 "rctime: %10ld.%09ld\n",
1384 ci->i_files + ci->i_subdirs,
1387 ci->i_rfiles + ci->i_rsubdirs,
1391 (long)ci->i_rctime.tv_sec,
1392 (long)ci->i_rctime.tv_nsec);
1395 if (*ppos >= cf->dir_info_len)
1397 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1398 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1401 *ppos += (size - left);
1406 * We maintain a private dentry LRU.
1408 * FIXME: this needs to be changed to a per-mds lru to be useful.
1410 void ceph_dentry_lru_add(struct dentry *dn)
1412 struct ceph_dentry_info *di = ceph_dentry(dn);
1413 struct ceph_mds_client *mdsc;
1415 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1416 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1417 spin_lock(&mdsc->dentry_lru_lock);
1418 list_add_tail(&di->lru, &mdsc->dentry_lru);
1420 spin_unlock(&mdsc->dentry_lru_lock);
1423 void ceph_dentry_lru_touch(struct dentry *dn)
1425 struct ceph_dentry_info *di = ceph_dentry(dn);
1426 struct ceph_mds_client *mdsc;
1428 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1430 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1431 spin_lock(&mdsc->dentry_lru_lock);
1432 list_move_tail(&di->lru, &mdsc->dentry_lru);
1433 spin_unlock(&mdsc->dentry_lru_lock);
1436 void ceph_dentry_lru_del(struct dentry *dn)
1438 struct ceph_dentry_info *di = ceph_dentry(dn);
1439 struct ceph_mds_client *mdsc;
1441 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1442 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1443 spin_lock(&mdsc->dentry_lru_lock);
1444 list_del_init(&di->lru);
1446 spin_unlock(&mdsc->dentry_lru_lock);
1450 * Return name hash for a given dentry. This is dependent on
1451 * the parent directory's hash function.
1453 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1455 struct ceph_inode_info *dci = ceph_inode(dir);
1457 switch (dci->i_dir_layout.dl_dir_hash) {
1458 case 0: /* for backward compat */
1459 case CEPH_STR_HASH_LINUX:
1460 return dn->d_name.hash;
1463 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1464 dn->d_name.name, dn->d_name.len);
1468 const struct file_operations ceph_dir_fops = {
1469 .read = ceph_read_dir,
1470 .iterate = ceph_readdir,
1471 .llseek = ceph_dir_llseek,
1473 .release = ceph_release,
1474 .unlocked_ioctl = ceph_ioctl,
1475 .fsync = ceph_fsync,
1478 const struct file_operations ceph_snapdir_fops = {
1479 .iterate = ceph_readdir,
1480 .llseek = ceph_dir_llseek,
1482 .release = ceph_release,
1485 const struct inode_operations ceph_dir_iops = {
1486 .lookup = ceph_lookup,
1487 .permission = ceph_permission,
1488 .getattr = ceph_getattr,
1489 .setattr = ceph_setattr,
1490 .listxattr = ceph_listxattr,
1491 .get_acl = ceph_get_acl,
1492 .set_acl = ceph_set_acl,
1493 .mknod = ceph_mknod,
1494 .symlink = ceph_symlink,
1495 .mkdir = ceph_mkdir,
1497 .unlink = ceph_unlink,
1498 .rmdir = ceph_unlink,
1499 .rename = ceph_rename,
1500 .create = ceph_create,
1501 .atomic_open = ceph_atomic_open,
1504 const struct inode_operations ceph_snapdir_iops = {
1505 .lookup = ceph_lookup,
1506 .permission = ceph_permission,
1507 .getattr = ceph_getattr,
1508 .mkdir = ceph_mkdir,
1509 .rmdir = ceph_unlink,
1510 .rename = ceph_rename,
1513 const struct dentry_operations ceph_dentry_ops = {
1514 .d_revalidate = ceph_d_revalidate,
1515 .d_release = ceph_d_release,
1516 .d_prune = ceph_d_prune,
1517 .d_init = ceph_d_init,