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 int ceph_init_dentry(struct dentry *dentry)
37 struct ceph_dentry_info *di;
42 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
44 return -ENOMEM; /* oh well */
46 spin_lock(&dentry->d_lock);
47 if (dentry->d_fsdata) {
49 kmem_cache_free(ceph_dentry_cachep, di);
53 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
54 d_set_d_op(dentry, &ceph_dentry_ops);
55 else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
56 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
58 d_set_d_op(dentry, &ceph_snap_dentry_ops);
61 di->lease_session = NULL;
63 /* avoid reordering d_fsdata setup so that the check above is safe */
65 dentry->d_fsdata = di;
66 ceph_dentry_lru_add(dentry);
68 spin_unlock(&dentry->d_lock);
73 * for f_pos for readdir:
75 * (0xff << 52) | ((24 bits hash) << 28) |
76 * (the nth entry has hash collision);
78 * ((frag value) << 28) | (the nth entry in frag);
80 #define OFFSET_BITS 28
81 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
82 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
83 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
85 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
91 static bool is_hash_order(loff_t p)
93 return (p & HASH_ORDER) == HASH_ORDER;
96 static unsigned fpos_frag(loff_t p)
98 return p >> OFFSET_BITS;
101 static unsigned fpos_hash(loff_t p)
103 return ceph_frag_value(fpos_frag(p));
106 static unsigned fpos_off(loff_t p)
108 return p & OFFSET_MASK;
111 static int fpos_cmp(loff_t l, loff_t r)
113 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
116 return (int)(fpos_off(l) - fpos_off(r));
120 * make note of the last dentry we read, so we can
121 * continue at the same lexicographical point,
122 * regardless of what dir changes take place on the
125 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
126 int len, unsigned next_offset)
128 char *buf = kmalloc(len+1, GFP_KERNEL);
131 kfree(fi->last_name);
133 memcpy(fi->last_name, name, len);
134 fi->last_name[len] = 0;
135 fi->next_offset = next_offset;
136 dout("note_last_dentry '%s'\n", fi->last_name);
141 static struct dentry *
142 __dcache_find_get_entry(struct dentry *parent, u64 idx,
143 struct ceph_readdir_cache_control *cache_ctl)
145 struct inode *dir = d_inode(parent);
146 struct dentry *dentry;
147 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
148 loff_t ptr_pos = idx * sizeof(struct dentry *);
149 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
151 if (ptr_pos >= i_size_read(dir))
154 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
155 ceph_readdir_cache_release(cache_ctl);
156 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
157 if (!cache_ctl->page) {
158 dout(" page %lu not found\n", ptr_pgoff);
159 return ERR_PTR(-EAGAIN);
161 /* reading/filling the cache are serialized by
162 i_mutex, no need to use page lock */
163 unlock_page(cache_ctl->page);
164 cache_ctl->dentries = kmap(cache_ctl->page);
167 cache_ctl->index = idx & idx_mask;
170 spin_lock(&parent->d_lock);
171 /* check i_size again here, because empty directory can be
172 * marked as complete while not holding the i_mutex. */
173 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
174 dentry = cache_ctl->dentries[cache_ctl->index];
177 spin_unlock(&parent->d_lock);
178 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
181 return dentry ? : ERR_PTR(-EAGAIN);
185 * When possible, we try to satisfy a readdir by peeking at the
186 * dcache. We make this work by carefully ordering dentries on
187 * d_child when we initially get results back from the MDS, and
188 * falling back to a "normal" sync readdir if any dentries in the dir
191 * Complete dir indicates that we have all dentries in the dir. It is
192 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
193 * the MDS if/when the directory is modified).
195 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
198 struct ceph_file_info *fi = file->private_data;
199 struct dentry *parent = file->f_path.dentry;
200 struct inode *dir = d_inode(parent);
201 struct dentry *dentry, *last = NULL;
202 struct ceph_dentry_info *di;
203 struct ceph_readdir_cache_control cache_ctl = {};
207 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
209 /* search start position */
211 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
213 u64 step = count >> 1;
214 dentry = __dcache_find_get_entry(parent, idx + step,
217 /* use linar search */
221 if (IS_ERR(dentry)) {
222 err = PTR_ERR(dentry);
225 di = ceph_dentry(dentry);
226 spin_lock(&dentry->d_lock);
227 if (fpos_cmp(di->offset, ctx->pos) < 0) {
233 spin_unlock(&dentry->d_lock);
237 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
242 bool emit_dentry = false;
243 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
245 fi->flags |= CEPH_F_ATEND;
249 if (IS_ERR(dentry)) {
250 err = PTR_ERR(dentry);
254 di = ceph_dentry(dentry);
255 spin_lock(&dentry->d_lock);
256 if (di->lease_shared_gen == shared_gen &&
257 d_really_is_positive(dentry) &&
258 fpos_cmp(ctx->pos, di->offset) <= 0) {
261 spin_unlock(&dentry->d_lock);
264 dout(" %llx dentry %p %pd %p\n", di->offset,
265 dentry, dentry, d_inode(dentry));
266 ctx->pos = di->offset;
267 if (!dir_emit(ctx, dentry->d_name.name,
269 ceph_translate_ino(dentry->d_sb,
270 d_inode(dentry)->i_ino),
271 d_inode(dentry)->i_mode >> 12)) {
286 ceph_readdir_cache_release(&cache_ctl);
289 di = ceph_dentry(last);
290 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
291 fpos_off(di->offset) + 1);
295 /* last_name no longer match cache index */
296 if (fi->readdir_cache_idx >= 0) {
297 fi->readdir_cache_idx = -1;
298 fi->dir_release_count = 0;
304 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
306 if (!fi->last_readdir)
308 if (is_hash_order(pos))
309 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
311 return fi->frag != fpos_frag(pos);
314 static int ceph_readdir(struct file *file, struct dir_context *ctx)
316 struct ceph_file_info *fi = file->private_data;
317 struct inode *inode = file_inode(file);
318 struct ceph_inode_info *ci = ceph_inode(inode);
319 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
320 struct ceph_mds_client *mdsc = fsc->mdsc;
324 struct ceph_mds_reply_info_parsed *rinfo;
326 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
327 if (fi->flags & CEPH_F_ATEND)
330 /* always start with . and .. */
332 dout("readdir off 0 -> '.'\n");
333 if (!dir_emit(ctx, ".", 1,
334 ceph_translate_ino(inode->i_sb, inode->i_ino),
335 inode->i_mode >> 12))
340 ino_t ino = parent_ino(file->f_path.dentry);
341 dout("readdir off 1 -> '..'\n");
342 if (!dir_emit(ctx, "..", 2,
343 ceph_translate_ino(inode->i_sb, ino),
344 inode->i_mode >> 12))
349 /* can we use the dcache? */
350 spin_lock(&ci->i_ceph_lock);
351 if (ceph_test_mount_opt(fsc, DCACHE) &&
352 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
353 ceph_snap(inode) != CEPH_SNAPDIR &&
354 __ceph_dir_is_complete_ordered(ci) &&
355 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
356 u32 shared_gen = ci->i_shared_gen;
357 spin_unlock(&ci->i_ceph_lock);
358 err = __dcache_readdir(file, ctx, shared_gen);
362 spin_unlock(&ci->i_ceph_lock);
365 /* proceed with a normal readdir */
367 /* do we have the correct frag content buffered? */
368 if (need_send_readdir(fi, ctx->pos)) {
369 struct ceph_mds_request *req;
370 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
371 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
373 /* discard old result, if any */
374 if (fi->last_readdir) {
375 ceph_mdsc_put_request(fi->last_readdir);
376 fi->last_readdir = NULL;
379 if (is_hash_order(ctx->pos)) {
380 /* fragtree isn't always accurate. choose frag
381 * based on previous reply when possible. */
382 if (frag == (unsigned)-1)
383 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
386 frag = fpos_frag(ctx->pos);
389 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
390 ceph_vinop(inode), frag, fi->last_name);
391 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
394 err = ceph_alloc_readdir_reply_buffer(req, inode);
396 ceph_mdsc_put_request(req);
399 /* hints to request -> mds selection code */
400 req->r_direct_mode = USE_AUTH_MDS;
401 req->r_direct_hash = ceph_frag_value(frag);
402 req->r_direct_is_hash = true;
404 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
406 ceph_mdsc_put_request(req);
410 req->r_dir_release_cnt = fi->dir_release_count;
411 req->r_dir_ordered_cnt = fi->dir_ordered_count;
412 req->r_readdir_cache_idx = fi->readdir_cache_idx;
413 req->r_readdir_offset = fi->next_offset;
414 req->r_args.readdir.frag = cpu_to_le32(frag);
415 req->r_args.readdir.flags =
416 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
418 req->r_inode = inode;
420 req->r_dentry = dget(file->f_path.dentry);
421 err = ceph_mdsc_do_request(mdsc, NULL, req);
423 ceph_mdsc_put_request(req);
426 dout("readdir got and parsed readdir result=%d on "
427 "frag %x, end=%d, complete=%d, hash_order=%d\n",
429 (int)req->r_reply_info.dir_end,
430 (int)req->r_reply_info.dir_complete,
431 (int)req->r_reply_info.hash_order);
433 rinfo = &req->r_reply_info;
434 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
435 frag = le32_to_cpu(rinfo->dir_dir->frag);
436 if (!rinfo->hash_order) {
437 fi->next_offset = req->r_readdir_offset;
438 /* adjust ctx->pos to beginning of frag */
439 ctx->pos = ceph_make_fpos(frag,
446 fi->last_readdir = req;
448 if (req->r_did_prepopulate) {
449 fi->readdir_cache_idx = req->r_readdir_cache_idx;
450 if (fi->readdir_cache_idx < 0) {
451 /* preclude from marking dir ordered */
452 fi->dir_ordered_count = 0;
453 } else if (ceph_frag_is_leftmost(frag) &&
454 fi->next_offset == 2) {
455 /* note dir version at start of readdir so
456 * we can tell if any dentries get dropped */
457 fi->dir_release_count = req->r_dir_release_cnt;
458 fi->dir_ordered_count = req->r_dir_ordered_cnt;
461 dout("readdir !did_prepopulate");
462 /* disable readdir cache */
463 fi->readdir_cache_idx = -1;
464 /* preclude from marking dir complete */
465 fi->dir_release_count = 0;
468 /* note next offset and last dentry name */
469 if (rinfo->dir_nr > 0) {
470 struct ceph_mds_reply_dir_entry *rde =
471 rinfo->dir_entries + (rinfo->dir_nr-1);
472 unsigned next_offset = req->r_reply_info.dir_end ?
473 2 : (fpos_off(rde->offset) + 1);
474 err = note_last_dentry(fi, rde->name, rde->name_len,
478 } else if (req->r_reply_info.dir_end) {
484 rinfo = &fi->last_readdir->r_reply_info;
485 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
486 fi->frag, rinfo->dir_nr, ctx->pos,
487 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
490 /* search start position */
491 if (rinfo->dir_nr > 0) {
492 int step, nr = rinfo->dir_nr;
495 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
503 for (; i < rinfo->dir_nr; i++) {
504 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
505 struct ceph_vino vino;
509 BUG_ON(rde->offset < ctx->pos);
511 ctx->pos = rde->offset;
512 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
513 i, rinfo->dir_nr, ctx->pos,
514 rde->name_len, rde->name, &rde->inode.in);
516 BUG_ON(!rde->inode.in);
517 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
518 vino.ino = le64_to_cpu(rde->inode.in->ino);
519 vino.snap = le64_to_cpu(rde->inode.in->snapid);
520 ino = ceph_vino_to_ino(vino);
522 if (!dir_emit(ctx, rde->name, rde->name_len,
523 ceph_translate_ino(inode->i_sb, ino), ftype)) {
524 dout("filldir stopping us...\n");
530 ceph_mdsc_put_request(fi->last_readdir);
531 fi->last_readdir = NULL;
533 if (fi->next_offset > 2) {
539 if (!ceph_frag_is_rightmost(fi->frag)) {
540 frag = ceph_frag_next(fi->frag);
541 if (is_hash_order(ctx->pos)) {
542 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
543 fi->next_offset, true);
544 if (new_pos > ctx->pos)
548 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
549 kfree(fi->last_name);
550 fi->last_name = NULL;
552 dout("readdir next frag is %x\n", frag);
555 fi->flags |= CEPH_F_ATEND;
558 * if dir_release_count still matches the dir, no dentries
559 * were released during the whole readdir, and we should have
560 * the complete dir contents in our cache.
562 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
563 spin_lock(&ci->i_ceph_lock);
564 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
565 dout(" marking %p complete and ordered\n", inode);
566 /* use i_size to track number of entries in
568 BUG_ON(fi->readdir_cache_idx < 0);
569 i_size_write(inode, fi->readdir_cache_idx *
570 sizeof(struct dentry*));
572 dout(" marking %p complete\n", inode);
574 __ceph_dir_set_complete(ci, fi->dir_release_count,
575 fi->dir_ordered_count);
576 spin_unlock(&ci->i_ceph_lock);
579 dout("readdir %p file %p done.\n", inode, file);
583 static void reset_readdir(struct ceph_file_info *fi)
585 if (fi->last_readdir) {
586 ceph_mdsc_put_request(fi->last_readdir);
587 fi->last_readdir = NULL;
589 kfree(fi->last_name);
590 fi->last_name = NULL;
591 fi->dir_release_count = 0;
592 fi->readdir_cache_idx = -1;
593 fi->next_offset = 2; /* compensate for . and .. */
594 fi->flags &= ~CEPH_F_ATEND;
598 * discard buffered readdir content on seekdir(0), or seek to new frag,
599 * or seek prior to current chunk
601 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
603 struct ceph_mds_reply_info_parsed *rinfo;
607 if (is_hash_order(new_pos)) {
608 /* no need to reset last_name for a forward seek when
609 * dentries are sotred in hash order */
610 } else if (fi->frag != fpos_frag(new_pos)) {
613 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
614 if (!rinfo || !rinfo->dir_nr)
616 chunk_offset = rinfo->dir_entries[0].offset;
617 return new_pos < chunk_offset ||
618 is_hash_order(new_pos) != is_hash_order(chunk_offset);
621 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
623 struct ceph_file_info *fi = file->private_data;
624 struct inode *inode = file->f_mapping->host;
631 offset += file->f_pos;
635 retval = -EOPNOTSUPP;
641 if (need_reset_readdir(fi, offset)) {
642 dout("dir_llseek dropping %p content\n", file);
644 } else if (is_hash_order(offset) && offset > file->f_pos) {
645 /* for hash offset, we don't know if a forward seek
646 * is within same frag */
647 fi->dir_release_count = 0;
648 fi->readdir_cache_idx = -1;
651 if (offset != file->f_pos) {
652 file->f_pos = offset;
654 fi->flags &= ~CEPH_F_ATEND;
664 * Handle lookups for the hidden .snap directory.
666 int ceph_handle_snapdir(struct ceph_mds_request *req,
667 struct dentry *dentry, int err)
669 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
670 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
673 if (err == -ENOENT &&
674 ceph_snap(parent) == CEPH_NOSNAP &&
675 strcmp(dentry->d_name.name,
676 fsc->mount_options->snapdir_name) == 0) {
677 struct inode *inode = ceph_get_snapdir(parent);
678 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
679 dentry, dentry, inode);
680 BUG_ON(!d_unhashed(dentry));
681 d_add(dentry, inode);
688 * Figure out final result of a lookup/open request.
690 * Mainly, make sure we return the final req->r_dentry (if it already
691 * existed) in place of the original VFS-provided dentry when they
694 * Gracefully handle the case where the MDS replies with -ENOENT and
695 * no trace (which it may do, at its discretion, e.g., if it doesn't
696 * care to issue a lease on the negative dentry).
698 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
699 struct dentry *dentry, int err)
701 if (err == -ENOENT) {
704 if (!req->r_reply_info.head->is_dentry) {
705 dout("ENOENT and no trace, dentry %p inode %p\n",
706 dentry, d_inode(dentry));
707 if (d_really_is_positive(dentry)) {
716 dentry = ERR_PTR(err);
717 else if (dentry != req->r_dentry)
718 dentry = dget(req->r_dentry); /* we got spliced */
724 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
726 return ceph_ino(inode) == CEPH_INO_ROOT &&
727 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
731 * Look up a single dir entry. If there is a lookup intent, inform
732 * the MDS so that it gets our 'caps wanted' value in a single op.
734 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
737 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
738 struct ceph_mds_client *mdsc = fsc->mdsc;
739 struct ceph_mds_request *req;
744 dout("lookup %p dentry %p '%pd'\n",
745 dir, dentry, dentry);
747 if (dentry->d_name.len > NAME_MAX)
748 return ERR_PTR(-ENAMETOOLONG);
750 err = ceph_init_dentry(dentry);
754 /* can we conclude ENOENT locally? */
755 if (d_really_is_negative(dentry)) {
756 struct ceph_inode_info *ci = ceph_inode(dir);
757 struct ceph_dentry_info *di = ceph_dentry(dentry);
759 spin_lock(&ci->i_ceph_lock);
760 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
761 if (strncmp(dentry->d_name.name,
762 fsc->mount_options->snapdir_name,
763 dentry->d_name.len) &&
764 !is_root_ceph_dentry(dir, dentry) &&
765 ceph_test_mount_opt(fsc, DCACHE) &&
766 __ceph_dir_is_complete(ci) &&
767 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
768 spin_unlock(&ci->i_ceph_lock);
769 dout(" dir %p complete, -ENOENT\n", dir);
771 di->lease_shared_gen = ci->i_shared_gen;
774 spin_unlock(&ci->i_ceph_lock);
777 op = ceph_snap(dir) == CEPH_SNAPDIR ?
778 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
779 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
781 return ERR_CAST(req);
782 req->r_dentry = dget(dentry);
785 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
786 if (ceph_security_xattr_wanted(dir))
787 mask |= CEPH_CAP_XATTR_SHARED;
788 req->r_args.getattr.mask = cpu_to_le32(mask);
790 req->r_locked_dir = dir;
791 err = ceph_mdsc_do_request(mdsc, NULL, req);
792 err = ceph_handle_snapdir(req, dentry, err);
793 dentry = ceph_finish_lookup(req, dentry, err);
794 ceph_mdsc_put_request(req); /* will dput(dentry) */
795 dout("lookup result=%p\n", dentry);
800 * If we do a create but get no trace back from the MDS, follow up with
801 * a lookup (the VFS expects us to link up the provided dentry).
803 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
805 struct dentry *result = ceph_lookup(dir, dentry, 0);
807 if (result && !IS_ERR(result)) {
809 * We created the item, then did a lookup, and found
810 * it was already linked to another inode we already
811 * had in our cache (and thus got spliced). To not
812 * confuse VFS (especially when inode is a directory),
813 * we don't link our dentry to that inode, return an
816 * This event should be rare and it happens only when
817 * we talk to old MDS. Recent MDS does not send traceless
818 * reply for request that creates new inode.
823 return PTR_ERR(result);
826 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
827 umode_t mode, dev_t rdev)
829 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
830 struct ceph_mds_client *mdsc = fsc->mdsc;
831 struct ceph_mds_request *req;
832 struct ceph_acls_info acls = {};
835 if (ceph_snap(dir) != CEPH_NOSNAP)
838 err = ceph_pre_init_acls(dir, &mode, &acls);
842 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
843 dir, dentry, mode, rdev);
844 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
849 req->r_dentry = dget(dentry);
851 req->r_locked_dir = dir;
852 req->r_args.mknod.mode = cpu_to_le32(mode);
853 req->r_args.mknod.rdev = cpu_to_le32(rdev);
854 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
855 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
857 req->r_pagelist = acls.pagelist;
858 acls.pagelist = NULL;
860 err = ceph_mdsc_do_request(mdsc, dir, req);
861 if (!err && !req->r_reply_info.head->is_dentry)
862 err = ceph_handle_notrace_create(dir, dentry);
863 ceph_mdsc_put_request(req);
866 ceph_init_inode_acls(d_inode(dentry), &acls);
869 ceph_release_acls_info(&acls);
873 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
876 return ceph_mknod(dir, dentry, mode, 0);
879 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
882 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
883 struct ceph_mds_client *mdsc = fsc->mdsc;
884 struct ceph_mds_request *req;
887 if (ceph_snap(dir) != CEPH_NOSNAP)
890 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
891 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
896 req->r_path2 = kstrdup(dest, GFP_KERNEL);
899 ceph_mdsc_put_request(req);
902 req->r_locked_dir = dir;
903 req->r_dentry = dget(dentry);
905 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
906 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
907 err = ceph_mdsc_do_request(mdsc, dir, req);
908 if (!err && !req->r_reply_info.head->is_dentry)
909 err = ceph_handle_notrace_create(dir, dentry);
910 ceph_mdsc_put_request(req);
917 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
919 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
920 struct ceph_mds_client *mdsc = fsc->mdsc;
921 struct ceph_mds_request *req;
922 struct ceph_acls_info acls = {};
926 if (ceph_snap(dir) == CEPH_SNAPDIR) {
927 /* mkdir .snap/foo is a MKSNAP */
928 op = CEPH_MDS_OP_MKSNAP;
929 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
931 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
932 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
933 op = CEPH_MDS_OP_MKDIR;
939 err = ceph_pre_init_acls(dir, &mode, &acls);
943 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
949 req->r_dentry = dget(dentry);
951 req->r_locked_dir = dir;
952 req->r_args.mkdir.mode = cpu_to_le32(mode);
953 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
954 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
956 req->r_pagelist = acls.pagelist;
957 acls.pagelist = NULL;
959 err = ceph_mdsc_do_request(mdsc, dir, req);
961 !req->r_reply_info.head->is_target &&
962 !req->r_reply_info.head->is_dentry)
963 err = ceph_handle_notrace_create(dir, dentry);
964 ceph_mdsc_put_request(req);
967 ceph_init_inode_acls(d_inode(dentry), &acls);
970 ceph_release_acls_info(&acls);
974 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
975 struct dentry *dentry)
977 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
978 struct ceph_mds_client *mdsc = fsc->mdsc;
979 struct ceph_mds_request *req;
982 if (ceph_snap(dir) != CEPH_NOSNAP)
985 dout("link in dir %p old_dentry %p dentry %p\n", dir,
987 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
992 req->r_dentry = dget(dentry);
994 req->r_old_dentry = dget(old_dentry);
995 req->r_locked_dir = dir;
996 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
997 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
998 /* release LINK_SHARED on source inode (mds will lock it) */
999 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1000 err = ceph_mdsc_do_request(mdsc, dir, req);
1003 } else if (!req->r_reply_info.head->is_dentry) {
1004 ihold(d_inode(old_dentry));
1005 d_instantiate(dentry, d_inode(old_dentry));
1007 ceph_mdsc_put_request(req);
1012 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1013 * looks like the link count will hit 0, drop any other caps (other
1014 * than PIN) we don't specifically want (due to the file still being
1017 static int drop_caps_for_unlink(struct inode *inode)
1019 struct ceph_inode_info *ci = ceph_inode(inode);
1020 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1022 spin_lock(&ci->i_ceph_lock);
1023 if (inode->i_nlink == 1) {
1024 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1025 ci->i_ceph_flags |= CEPH_I_NODELAY;
1027 spin_unlock(&ci->i_ceph_lock);
1032 * rmdir and unlink are differ only by the metadata op code
1034 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1036 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1037 struct ceph_mds_client *mdsc = fsc->mdsc;
1038 struct inode *inode = d_inode(dentry);
1039 struct ceph_mds_request *req;
1043 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1044 /* rmdir .snap/foo is RMSNAP */
1045 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1046 op = CEPH_MDS_OP_RMSNAP;
1047 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1048 dout("unlink/rmdir dir %p dn %p inode %p\n",
1049 dir, dentry, inode);
1050 op = d_is_dir(dentry) ?
1051 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1054 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1059 req->r_dentry = dget(dentry);
1060 req->r_num_caps = 2;
1061 req->r_locked_dir = dir;
1062 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1063 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1064 req->r_inode_drop = drop_caps_for_unlink(inode);
1065 err = ceph_mdsc_do_request(mdsc, dir, req);
1066 if (!err && !req->r_reply_info.head->is_dentry)
1068 ceph_mdsc_put_request(req);
1073 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1074 struct inode *new_dir, struct dentry *new_dentry,
1077 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1078 struct ceph_mds_client *mdsc = fsc->mdsc;
1079 struct ceph_mds_request *req;
1080 int op = CEPH_MDS_OP_RENAME;
1086 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1088 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1089 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1090 op = CEPH_MDS_OP_RENAMESNAP;
1094 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1095 old_dir, old_dentry, new_dir, new_dentry);
1096 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1098 return PTR_ERR(req);
1100 req->r_dentry = dget(new_dentry);
1101 req->r_num_caps = 2;
1102 req->r_old_dentry = dget(old_dentry);
1103 req->r_old_dentry_dir = old_dir;
1104 req->r_locked_dir = new_dir;
1105 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1106 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1107 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1108 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1109 /* release LINK_RDCACHE on source inode (mds will lock it) */
1110 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1111 if (d_really_is_positive(new_dentry))
1112 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
1113 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1114 if (!err && !req->r_reply_info.head->is_dentry) {
1116 * Normally d_move() is done by fill_trace (called by
1117 * do_request, above). If there is no trace, we need
1121 /* d_move screws up sibling dentries' offsets */
1122 ceph_dir_clear_complete(old_dir);
1123 ceph_dir_clear_complete(new_dir);
1125 d_move(old_dentry, new_dentry);
1127 /* ensure target dentry is invalidated, despite
1128 rehashing bug in vfs_rename_dir */
1129 ceph_invalidate_dentry_lease(new_dentry);
1131 ceph_mdsc_put_request(req);
1136 * Ensure a dentry lease will no longer revalidate.
1138 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1140 spin_lock(&dentry->d_lock);
1141 ceph_dentry(dentry)->time = jiffies;
1142 ceph_dentry(dentry)->lease_shared_gen = 0;
1143 spin_unlock(&dentry->d_lock);
1147 * Check if dentry lease is valid. If not, delete the lease. Try to
1148 * renew if the least is more than half up.
1150 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1153 struct ceph_dentry_info *di;
1154 struct ceph_mds_session *s;
1158 struct ceph_mds_session *session = NULL;
1161 spin_lock(&dentry->d_lock);
1162 di = ceph_dentry(dentry);
1163 if (di && di->lease_session) {
1164 s = di->lease_session;
1165 spin_lock(&s->s_gen_ttl_lock);
1168 spin_unlock(&s->s_gen_ttl_lock);
1170 if (di->lease_gen == gen &&
1171 time_before(jiffies, di->time) &&
1172 time_before(jiffies, ttl)) {
1174 if (di->lease_renew_after &&
1175 time_after(jiffies, di->lease_renew_after)) {
1177 * We should renew. If we're in RCU walk mode
1178 * though, we can't do that so just return
1181 if (flags & LOOKUP_RCU) {
1184 session = ceph_get_mds_session(s);
1185 seq = di->lease_seq;
1186 di->lease_renew_after = 0;
1187 di->lease_renew_from = jiffies;
1192 spin_unlock(&dentry->d_lock);
1195 ceph_mdsc_lease_send_msg(session, dir, dentry,
1196 CEPH_MDS_LEASE_RENEW, seq);
1197 ceph_put_mds_session(session);
1199 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1204 * Check if directory-wide content lease/cap is valid.
1206 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1208 struct ceph_inode_info *ci = ceph_inode(dir);
1209 struct ceph_dentry_info *di = ceph_dentry(dentry);
1212 spin_lock(&ci->i_ceph_lock);
1213 if (ci->i_shared_gen == di->lease_shared_gen)
1214 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1215 spin_unlock(&ci->i_ceph_lock);
1216 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1217 dir, (unsigned)ci->i_shared_gen, dentry,
1218 (unsigned)di->lease_shared_gen, valid);
1223 * Check if cached dentry can be trusted.
1225 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1228 struct dentry *parent;
1231 if (flags & LOOKUP_RCU) {
1232 parent = ACCESS_ONCE(dentry->d_parent);
1233 dir = d_inode_rcu(parent);
1237 parent = dget_parent(dentry);
1238 dir = d_inode(parent);
1241 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1242 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1244 /* always trust cached snapped dentries, snapdir dentry */
1245 if (ceph_snap(dir) != CEPH_NOSNAP) {
1246 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1247 dentry, d_inode(dentry));
1249 } else if (d_really_is_positive(dentry) &&
1250 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1253 valid = dentry_lease_is_valid(dentry, flags, dir);
1254 if (valid == -ECHILD)
1256 if (valid || dir_lease_is_valid(dir, dentry)) {
1257 if (d_really_is_positive(dentry))
1258 valid = ceph_is_any_caps(d_inode(dentry));
1265 struct ceph_mds_client *mdsc =
1266 ceph_sb_to_client(dir->i_sb)->mdsc;
1267 struct ceph_mds_request *req;
1271 if (flags & LOOKUP_RCU)
1274 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1275 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_GETATTR;
1276 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1278 req->r_dentry = dget(dentry);
1279 req->r_num_caps = op == CEPH_MDS_OP_GETATTR ? 1 : 2;
1281 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1282 if (ceph_security_xattr_wanted(dir))
1283 mask |= CEPH_CAP_XATTR_SHARED;
1284 req->r_args.getattr.mask = cpu_to_le32(mask);
1286 err = ceph_mdsc_do_request(mdsc, NULL, req);
1289 if (d_really_is_positive(dentry) &&
1290 d_inode(dentry) == req->r_target_inode)
1294 if (d_really_is_negative(dentry))
1300 ceph_mdsc_put_request(req);
1301 dout("d_revalidate %p lookup result=%d\n",
1306 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1308 ceph_dentry_lru_touch(dentry);
1310 ceph_dir_clear_complete(dir);
1313 if (!(flags & LOOKUP_RCU))
1319 * Release our ceph_dentry_info.
1321 static void ceph_d_release(struct dentry *dentry)
1323 struct ceph_dentry_info *di = ceph_dentry(dentry);
1325 dout("d_release %p\n", dentry);
1326 ceph_dentry_lru_del(dentry);
1328 spin_lock(&dentry->d_lock);
1329 dentry->d_fsdata = NULL;
1330 spin_unlock(&dentry->d_lock);
1332 if (di->lease_session)
1333 ceph_put_mds_session(di->lease_session);
1334 kmem_cache_free(ceph_dentry_cachep, di);
1337 static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1341 * Eventually, we'll want to revalidate snapped metadata
1342 * too... probably...
1348 * When the VFS prunes a dentry from the cache, we need to clear the
1349 * complete flag on the parent directory.
1351 * Called under dentry->d_lock.
1353 static void ceph_d_prune(struct dentry *dentry)
1355 dout("ceph_d_prune %p\n", dentry);
1357 /* do we have a valid parent? */
1358 if (IS_ROOT(dentry))
1361 /* if we are not hashed, we don't affect dir's completeness */
1362 if (d_unhashed(dentry))
1366 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1367 * cleared until d_release
1369 ceph_dir_clear_complete(d_inode(dentry->d_parent));
1373 * read() on a dir. This weird interface hack only works if mounted
1374 * with '-o dirstat'.
1376 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1379 struct ceph_file_info *cf = file->private_data;
1380 struct inode *inode = file_inode(file);
1381 struct ceph_inode_info *ci = ceph_inode(inode);
1383 const int bufsize = 1024;
1385 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1388 if (!cf->dir_info) {
1389 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1393 snprintf(cf->dir_info, bufsize,
1396 " subdirs: %20lld\n"
1397 "rentries: %20lld\n"
1399 " rsubdirs: %20lld\n"
1401 "rctime: %10ld.%09ld\n",
1402 ci->i_files + ci->i_subdirs,
1405 ci->i_rfiles + ci->i_rsubdirs,
1409 (long)ci->i_rctime.tv_sec,
1410 (long)ci->i_rctime.tv_nsec);
1413 if (*ppos >= cf->dir_info_len)
1415 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1416 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1419 *ppos += (size - left);
1424 * We maintain a private dentry LRU.
1426 * FIXME: this needs to be changed to a per-mds lru to be useful.
1428 void ceph_dentry_lru_add(struct dentry *dn)
1430 struct ceph_dentry_info *di = ceph_dentry(dn);
1431 struct ceph_mds_client *mdsc;
1433 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1434 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1435 spin_lock(&mdsc->dentry_lru_lock);
1436 list_add_tail(&di->lru, &mdsc->dentry_lru);
1438 spin_unlock(&mdsc->dentry_lru_lock);
1441 void ceph_dentry_lru_touch(struct dentry *dn)
1443 struct ceph_dentry_info *di = ceph_dentry(dn);
1444 struct ceph_mds_client *mdsc;
1446 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1448 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1449 spin_lock(&mdsc->dentry_lru_lock);
1450 list_move_tail(&di->lru, &mdsc->dentry_lru);
1451 spin_unlock(&mdsc->dentry_lru_lock);
1454 void ceph_dentry_lru_del(struct dentry *dn)
1456 struct ceph_dentry_info *di = ceph_dentry(dn);
1457 struct ceph_mds_client *mdsc;
1459 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1460 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1461 spin_lock(&mdsc->dentry_lru_lock);
1462 list_del_init(&di->lru);
1464 spin_unlock(&mdsc->dentry_lru_lock);
1468 * Return name hash for a given dentry. This is dependent on
1469 * the parent directory's hash function.
1471 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1473 struct ceph_inode_info *dci = ceph_inode(dir);
1476 switch (dci->i_dir_layout.dl_dir_hash) {
1477 case 0: /* for backward compat */
1478 case CEPH_STR_HASH_LINUX:
1479 return dn->d_name.hash;
1482 spin_lock(&dn->d_lock);
1483 hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1484 dn->d_name.name, dn->d_name.len);
1485 spin_unlock(&dn->d_lock);
1490 const struct file_operations ceph_dir_fops = {
1491 .read = ceph_read_dir,
1492 .iterate = ceph_readdir,
1493 .llseek = ceph_dir_llseek,
1495 .release = ceph_release,
1496 .unlocked_ioctl = ceph_ioctl,
1497 .fsync = ceph_fsync,
1500 const struct file_operations ceph_snapdir_fops = {
1501 .iterate = ceph_readdir,
1502 .llseek = ceph_dir_llseek,
1504 .release = ceph_release,
1507 const struct inode_operations ceph_dir_iops = {
1508 .lookup = ceph_lookup,
1509 .permission = ceph_permission,
1510 .getattr = ceph_getattr,
1511 .setattr = ceph_setattr,
1512 .listxattr = ceph_listxattr,
1513 .get_acl = ceph_get_acl,
1514 .set_acl = ceph_set_acl,
1515 .mknod = ceph_mknod,
1516 .symlink = ceph_symlink,
1517 .mkdir = ceph_mkdir,
1519 .unlink = ceph_unlink,
1520 .rmdir = ceph_unlink,
1521 .rename = ceph_rename,
1522 .create = ceph_create,
1523 .atomic_open = ceph_atomic_open,
1526 const struct inode_operations ceph_snapdir_iops = {
1527 .lookup = ceph_lookup,
1528 .permission = ceph_permission,
1529 .getattr = ceph_getattr,
1530 .mkdir = ceph_mkdir,
1531 .rmdir = ceph_unlink,
1532 .rename = ceph_rename,
1535 const struct dentry_operations ceph_dentry_ops = {
1536 .d_revalidate = ceph_d_revalidate,
1537 .d_release = ceph_d_release,
1538 .d_prune = ceph_d_prune,
1541 const struct dentry_operations ceph_snapdir_dentry_ops = {
1542 .d_revalidate = ceph_snapdir_d_revalidate,
1543 .d_release = ceph_d_release,
1546 const struct dentry_operations ceph_snap_dentry_ops = {
1547 .d_release = ceph_d_release,
1548 .d_prune = ceph_d_prune,