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 readdir, we encode the directory frag and offset within that
75 static unsigned fpos_frag(loff_t p
)
79 static unsigned fpos_off(loff_t p
)
81 return p
& 0xffffffff;
84 static int fpos_cmp(loff_t l
, loff_t r
)
86 int v
= ceph_frag_compare(fpos_frag(l
), fpos_frag(r
));
89 return (int)(fpos_off(l
) - fpos_off(r
));
93 * make note of the last dentry we read, so we can
94 * continue at the same lexicographical point,
95 * regardless of what dir changes take place on the
98 static int note_last_dentry(struct ceph_file_info
*fi
, const char *name
,
99 int len
, unsigned next_offset
)
101 char *buf
= kmalloc(len
+1, GFP_KERNEL
);
104 kfree(fi
->last_name
);
106 memcpy(fi
->last_name
, name
, len
);
107 fi
->last_name
[len
] = 0;
108 fi
->next_offset
= next_offset
;
109 dout("note_last_dentry '%s'\n", fi
->last_name
);
114 static struct dentry
*
115 __dcache_find_get_entry(struct dentry
*parent
, u64 idx
,
116 struct ceph_readdir_cache_control
*cache_ctl
)
118 struct inode
*dir
= d_inode(parent
);
119 struct dentry
*dentry
;
120 unsigned idx_mask
= (PAGE_SIZE
/ sizeof(struct dentry
*)) - 1;
121 loff_t ptr_pos
= idx
* sizeof(struct dentry
*);
122 pgoff_t ptr_pgoff
= ptr_pos
>> PAGE_SHIFT
;
124 if (ptr_pos
>= i_size_read(dir
))
127 if (!cache_ctl
->page
|| ptr_pgoff
!= page_index(cache_ctl
->page
)) {
128 ceph_readdir_cache_release(cache_ctl
);
129 cache_ctl
->page
= find_lock_page(&dir
->i_data
, ptr_pgoff
);
130 if (!cache_ctl
->page
) {
131 dout(" page %lu not found\n", ptr_pgoff
);
132 return ERR_PTR(-EAGAIN
);
134 /* reading/filling the cache are serialized by
135 i_mutex, no need to use page lock */
136 unlock_page(cache_ctl
->page
);
137 cache_ctl
->dentries
= kmap(cache_ctl
->page
);
140 cache_ctl
->index
= idx
& idx_mask
;
143 spin_lock(&parent
->d_lock
);
144 /* check i_size again here, because empty directory can be
145 * marked as complete while not holding the i_mutex. */
146 if (ceph_dir_is_complete_ordered(dir
) && ptr_pos
< i_size_read(dir
))
147 dentry
= cache_ctl
->dentries
[cache_ctl
->index
];
150 spin_unlock(&parent
->d_lock
);
151 if (dentry
&& !lockref_get_not_dead(&dentry
->d_lockref
))
154 return dentry
? : ERR_PTR(-EAGAIN
);
158 * When possible, we try to satisfy a readdir by peeking at the
159 * dcache. We make this work by carefully ordering dentries on
160 * d_child when we initially get results back from the MDS, and
161 * falling back to a "normal" sync readdir if any dentries in the dir
164 * Complete dir indicates that we have all dentries in the dir. It is
165 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
166 * the MDS if/when the directory is modified).
168 static int __dcache_readdir(struct file
*file
, struct dir_context
*ctx
,
171 struct ceph_file_info
*fi
= file
->private_data
;
172 struct dentry
*parent
= file
->f_path
.dentry
;
173 struct inode
*dir
= d_inode(parent
);
174 struct dentry
*dentry
, *last
= NULL
;
175 struct ceph_dentry_info
*di
;
176 struct ceph_readdir_cache_control cache_ctl
= {};
180 dout("__dcache_readdir %p v%u at %llu\n", dir
, shared_gen
, ctx
->pos
);
182 /* search start position */
184 u64 count
= div_u64(i_size_read(dir
), sizeof(struct dentry
*));
186 u64 step
= count
>> 1;
187 dentry
= __dcache_find_get_entry(parent
, idx
+ step
,
190 /* use linar search */
194 if (IS_ERR(dentry
)) {
195 err
= PTR_ERR(dentry
);
198 di
= ceph_dentry(dentry
);
199 spin_lock(&dentry
->d_lock
);
200 if (fpos_cmp(di
->offset
, ctx
->pos
) < 0) {
206 spin_unlock(&dentry
->d_lock
);
210 dout("__dcache_readdir %p cache idx %llu\n", dir
, idx
);
215 bool emit_dentry
= false;
216 dentry
= __dcache_find_get_entry(parent
, idx
++, &cache_ctl
);
218 fi
->flags
|= CEPH_F_ATEND
;
222 if (IS_ERR(dentry
)) {
223 err
= PTR_ERR(dentry
);
227 di
= ceph_dentry(dentry
);
228 spin_lock(&dentry
->d_lock
);
229 if (di
->lease_shared_gen
== shared_gen
&&
230 d_really_is_positive(dentry
) &&
231 fpos_cmp(ctx
->pos
, di
->offset
) <= 0) {
234 spin_unlock(&dentry
->d_lock
);
237 dout(" %llu (%llu) dentry %p %pd %p\n", di
->offset
, ctx
->pos
,
238 dentry
, dentry
, d_inode(dentry
));
239 ctx
->pos
= di
->offset
;
240 if (!dir_emit(ctx
, dentry
->d_name
.name
,
242 ceph_translate_ino(dentry
->d_sb
,
243 d_inode(dentry
)->i_ino
),
244 d_inode(dentry
)->i_mode
>> 12)) {
259 ceph_readdir_cache_release(&cache_ctl
);
262 di
= ceph_dentry(last
);
263 ret
= note_last_dentry(fi
, last
->d_name
.name
, last
->d_name
.len
,
264 fpos_off(di
->offset
) + 1);
272 static int ceph_readdir(struct file
*file
, struct dir_context
*ctx
)
274 struct ceph_file_info
*fi
= file
->private_data
;
275 struct inode
*inode
= file_inode(file
);
276 struct ceph_inode_info
*ci
= ceph_inode(inode
);
277 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
278 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
279 unsigned frag
= fpos_frag(ctx
->pos
);
280 int off
= fpos_off(ctx
->pos
);
283 struct ceph_mds_reply_info_parsed
*rinfo
;
285 dout("readdir %p file %p frag %u off %u\n", inode
, file
, frag
, off
);
286 if (fi
->flags
& CEPH_F_ATEND
)
289 /* always start with . and .. */
291 dout("readdir off 0 -> '.'\n");
292 if (!dir_emit(ctx
, ".", 1,
293 ceph_translate_ino(inode
->i_sb
, inode
->i_ino
),
294 inode
->i_mode
>> 12))
300 ino_t ino
= parent_ino(file
->f_path
.dentry
);
301 dout("readdir off 1 -> '..'\n");
302 if (!dir_emit(ctx
, "..", 2,
303 ceph_translate_ino(inode
->i_sb
, ino
),
304 inode
->i_mode
>> 12))
310 /* can we use the dcache? */
311 spin_lock(&ci
->i_ceph_lock
);
312 if (ceph_test_mount_opt(fsc
, DCACHE
) &&
313 !ceph_test_mount_opt(fsc
, NOASYNCREADDIR
) &&
314 ceph_snap(inode
) != CEPH_SNAPDIR
&&
315 __ceph_dir_is_complete_ordered(ci
) &&
316 __ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1)) {
317 u32 shared_gen
= ci
->i_shared_gen
;
318 spin_unlock(&ci
->i_ceph_lock
);
319 err
= __dcache_readdir(file
, ctx
, shared_gen
);
322 frag
= fpos_frag(ctx
->pos
);
323 off
= fpos_off(ctx
->pos
);
325 spin_unlock(&ci
->i_ceph_lock
);
328 /* proceed with a normal readdir */
330 /* do we have the correct frag content buffered? */
331 if (fi
->frag
!= frag
|| fi
->last_readdir
== NULL
) {
332 struct ceph_mds_request
*req
;
333 int op
= ceph_snap(inode
) == CEPH_SNAPDIR
?
334 CEPH_MDS_OP_LSSNAP
: CEPH_MDS_OP_READDIR
;
336 /* discard old result, if any */
337 if (fi
->last_readdir
) {
338 ceph_mdsc_put_request(fi
->last_readdir
);
339 fi
->last_readdir
= NULL
;
342 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
343 ceph_vinop(inode
), frag
, fi
->last_name
);
344 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
347 err
= ceph_alloc_readdir_reply_buffer(req
, inode
);
349 ceph_mdsc_put_request(req
);
352 /* hints to request -> mds selection code */
353 req
->r_direct_mode
= USE_AUTH_MDS
;
354 req
->r_direct_hash
= ceph_frag_value(frag
);
355 req
->r_direct_is_hash
= true;
357 req
->r_path2
= kstrdup(fi
->last_name
, GFP_KERNEL
);
359 ceph_mdsc_put_request(req
);
363 req
->r_dir_release_cnt
= fi
->dir_release_count
;
364 req
->r_dir_ordered_cnt
= fi
->dir_ordered_count
;
365 req
->r_readdir_cache_idx
= fi
->readdir_cache_idx
;
366 req
->r_readdir_offset
= fi
->next_offset
;
367 req
->r_args
.readdir
.frag
= cpu_to_le32(frag
);
369 req
->r_inode
= inode
;
371 req
->r_dentry
= dget(file
->f_path
.dentry
);
372 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
374 ceph_mdsc_put_request(req
);
377 dout("readdir got and parsed readdir result=%d"
378 " on frag %x, end=%d, complete=%d\n", err
, frag
,
379 (int)req
->r_reply_info
.dir_end
,
380 (int)req
->r_reply_info
.dir_complete
);
383 /* note next offset and last dentry name */
384 rinfo
= &req
->r_reply_info
;
385 if (le32_to_cpu(rinfo
->dir_dir
->frag
) != frag
) {
386 frag
= le32_to_cpu(rinfo
->dir_dir
->frag
);
387 off
= req
->r_readdir_offset
;
388 fi
->next_offset
= off
;
392 fi
->offset
= fi
->next_offset
;
393 fi
->last_readdir
= req
;
395 if (req
->r_did_prepopulate
) {
396 fi
->readdir_cache_idx
= req
->r_readdir_cache_idx
;
397 if (fi
->readdir_cache_idx
< 0) {
398 /* preclude from marking dir ordered */
399 fi
->dir_ordered_count
= 0;
400 } else if (ceph_frag_is_leftmost(frag
) && off
== 2) {
401 /* note dir version at start of readdir so
402 * we can tell if any dentries get dropped */
403 fi
->dir_release_count
= req
->r_dir_release_cnt
;
404 fi
->dir_ordered_count
= req
->r_dir_ordered_cnt
;
407 dout("readdir !did_prepopulate");
408 /* disable readdir cache */
409 fi
->readdir_cache_idx
= -1;
410 /* preclude from marking dir complete */
411 fi
->dir_release_count
= 0;
414 if (req
->r_reply_info
.dir_end
) {
415 kfree(fi
->last_name
);
416 fi
->last_name
= NULL
;
419 err
= note_last_dentry(fi
,
420 rinfo
->dir_dname
[rinfo
->dir_nr
-1],
421 rinfo
->dir_dname_len
[rinfo
->dir_nr
-1],
422 fi
->next_offset
+ rinfo
->dir_nr
);
428 rinfo
= &fi
->last_readdir
->r_reply_info
;
429 dout("readdir frag %x num %d off %d chunkoff %d\n", frag
,
430 rinfo
->dir_nr
, off
, fi
->offset
);
432 ctx
->pos
= ceph_make_fpos(frag
, off
);
433 while (off
>= fi
->offset
&& off
- fi
->offset
< rinfo
->dir_nr
) {
434 struct ceph_mds_reply_inode
*in
=
435 rinfo
->dir_in
[off
- fi
->offset
].in
;
436 struct ceph_vino vino
;
439 dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
440 off
, off
- fi
->offset
, rinfo
->dir_nr
, ctx
->pos
,
441 rinfo
->dir_dname_len
[off
- fi
->offset
],
442 rinfo
->dir_dname
[off
- fi
->offset
], in
);
444 ftype
= le32_to_cpu(in
->mode
) >> 12;
445 vino
.ino
= le64_to_cpu(in
->ino
);
446 vino
.snap
= le64_to_cpu(in
->snapid
);
447 ino
= ceph_vino_to_ino(vino
);
449 rinfo
->dir_dname
[off
- fi
->offset
],
450 rinfo
->dir_dname_len
[off
- fi
->offset
],
451 ceph_translate_ino(inode
->i_sb
, ino
), ftype
)) {
452 dout("filldir stopping us...\n");
460 ceph_mdsc_put_request(fi
->last_readdir
);
461 fi
->last_readdir
= NULL
;
466 if (!ceph_frag_is_rightmost(frag
)) {
467 frag
= ceph_frag_next(frag
);
469 ctx
->pos
= ceph_make_fpos(frag
, off
);
470 dout("readdir next frag is %x\n", frag
);
473 fi
->flags
|= CEPH_F_ATEND
;
476 * if dir_release_count still matches the dir, no dentries
477 * were released during the whole readdir, and we should have
478 * the complete dir contents in our cache.
480 if (atomic64_read(&ci
->i_release_count
) == fi
->dir_release_count
) {
481 spin_lock(&ci
->i_ceph_lock
);
482 if (fi
->dir_ordered_count
== atomic64_read(&ci
->i_ordered_count
)) {
483 dout(" marking %p complete and ordered\n", inode
);
484 /* use i_size to track number of entries in
486 BUG_ON(fi
->readdir_cache_idx
< 0);
487 i_size_write(inode
, fi
->readdir_cache_idx
*
488 sizeof(struct dentry
*));
490 dout(" marking %p complete\n", inode
);
492 __ceph_dir_set_complete(ci
, fi
->dir_release_count
,
493 fi
->dir_ordered_count
);
494 spin_unlock(&ci
->i_ceph_lock
);
497 dout("readdir %p file %p done.\n", inode
, file
);
501 static void reset_readdir(struct ceph_file_info
*fi
, unsigned frag
)
503 if (fi
->last_readdir
) {
504 ceph_mdsc_put_request(fi
->last_readdir
);
505 fi
->last_readdir
= NULL
;
507 kfree(fi
->last_name
);
508 fi
->last_name
= NULL
;
509 fi
->dir_release_count
= 0;
510 fi
->readdir_cache_idx
= -1;
511 fi
->next_offset
= 2; /* compensate for . and .. */
512 fi
->flags
&= ~CEPH_F_ATEND
;
515 static loff_t
ceph_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
517 struct ceph_file_info
*fi
= file
->private_data
;
518 struct inode
*inode
= file
->f_mapping
->host
;
519 loff_t old_offset
= ceph_make_fpos(fi
->frag
, fi
->next_offset
);
526 offset
+= file
->f_pos
;
530 retval
= -EOPNOTSUPP
;
536 if (offset
!= file
->f_pos
) {
537 file
->f_pos
= offset
;
539 fi
->flags
&= ~CEPH_F_ATEND
;
544 fpos_frag(offset
) != fi
->frag
||
545 fpos_off(offset
) < fi
->offset
) {
546 /* discard buffered readdir content on seekdir(0), or
547 * seek to new frag, or seek prior to current chunk */
548 dout("dir_llseek dropping %p content\n", file
);
549 reset_readdir(fi
, fpos_frag(offset
));
550 } else if (fpos_cmp(offset
, old_offset
) > 0) {
551 /* reset dir_release_count if we did a forward seek */
552 fi
->dir_release_count
= 0;
553 fi
->readdir_cache_idx
= -1;
562 * Handle lookups for the hidden .snap directory.
564 int ceph_handle_snapdir(struct ceph_mds_request
*req
,
565 struct dentry
*dentry
, int err
)
567 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dentry
->d_sb
);
568 struct inode
*parent
= d_inode(dentry
->d_parent
); /* we hold i_mutex */
571 if (err
== -ENOENT
&&
572 ceph_snap(parent
) == CEPH_NOSNAP
&&
573 strcmp(dentry
->d_name
.name
,
574 fsc
->mount_options
->snapdir_name
) == 0) {
575 struct inode
*inode
= ceph_get_snapdir(parent
);
576 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
577 dentry
, dentry
, inode
);
578 BUG_ON(!d_unhashed(dentry
));
579 d_add(dentry
, inode
);
586 * Figure out final result of a lookup/open request.
588 * Mainly, make sure we return the final req->r_dentry (if it already
589 * existed) in place of the original VFS-provided dentry when they
592 * Gracefully handle the case where the MDS replies with -ENOENT and
593 * no trace (which it may do, at its discretion, e.g., if it doesn't
594 * care to issue a lease on the negative dentry).
596 struct dentry
*ceph_finish_lookup(struct ceph_mds_request
*req
,
597 struct dentry
*dentry
, int err
)
599 if (err
== -ENOENT
) {
602 if (!req
->r_reply_info
.head
->is_dentry
) {
603 dout("ENOENT and no trace, dentry %p inode %p\n",
604 dentry
, d_inode(dentry
));
605 if (d_really_is_positive(dentry
)) {
614 dentry
= ERR_PTR(err
);
615 else if (dentry
!= req
->r_dentry
)
616 dentry
= dget(req
->r_dentry
); /* we got spliced */
622 static int is_root_ceph_dentry(struct inode
*inode
, struct dentry
*dentry
)
624 return ceph_ino(inode
) == CEPH_INO_ROOT
&&
625 strncmp(dentry
->d_name
.name
, ".ceph", 5) == 0;
629 * Look up a single dir entry. If there is a lookup intent, inform
630 * the MDS so that it gets our 'caps wanted' value in a single op.
632 static struct dentry
*ceph_lookup(struct inode
*dir
, struct dentry
*dentry
,
635 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
636 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
637 struct ceph_mds_request
*req
;
642 dout("lookup %p dentry %p '%pd'\n",
643 dir
, dentry
, dentry
);
645 if (dentry
->d_name
.len
> NAME_MAX
)
646 return ERR_PTR(-ENAMETOOLONG
);
648 err
= ceph_init_dentry(dentry
);
652 /* can we conclude ENOENT locally? */
653 if (d_really_is_negative(dentry
)) {
654 struct ceph_inode_info
*ci
= ceph_inode(dir
);
655 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
657 spin_lock(&ci
->i_ceph_lock
);
658 dout(" dir %p flags are %d\n", dir
, ci
->i_ceph_flags
);
659 if (strncmp(dentry
->d_name
.name
,
660 fsc
->mount_options
->snapdir_name
,
661 dentry
->d_name
.len
) &&
662 !is_root_ceph_dentry(dir
, dentry
) &&
663 ceph_test_mount_opt(fsc
, DCACHE
) &&
664 __ceph_dir_is_complete(ci
) &&
665 (__ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1))) {
666 spin_unlock(&ci
->i_ceph_lock
);
667 dout(" dir %p complete, -ENOENT\n", dir
);
669 di
->lease_shared_gen
= ci
->i_shared_gen
;
672 spin_unlock(&ci
->i_ceph_lock
);
675 op
= ceph_snap(dir
) == CEPH_SNAPDIR
?
676 CEPH_MDS_OP_LOOKUPSNAP
: CEPH_MDS_OP_LOOKUP
;
677 req
= ceph_mdsc_create_request(mdsc
, op
, USE_ANY_MDS
);
679 return ERR_CAST(req
);
680 req
->r_dentry
= dget(dentry
);
683 mask
= CEPH_STAT_CAP_INODE
| CEPH_CAP_AUTH_SHARED
;
684 if (ceph_security_xattr_wanted(dir
))
685 mask
|= CEPH_CAP_XATTR_SHARED
;
686 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
688 req
->r_locked_dir
= dir
;
689 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
690 err
= ceph_handle_snapdir(req
, dentry
, err
);
691 dentry
= ceph_finish_lookup(req
, dentry
, err
);
692 ceph_mdsc_put_request(req
); /* will dput(dentry) */
693 dout("lookup result=%p\n", dentry
);
698 * If we do a create but get no trace back from the MDS, follow up with
699 * a lookup (the VFS expects us to link up the provided dentry).
701 int ceph_handle_notrace_create(struct inode
*dir
, struct dentry
*dentry
)
703 struct dentry
*result
= ceph_lookup(dir
, dentry
, 0);
705 if (result
&& !IS_ERR(result
)) {
707 * We created the item, then did a lookup, and found
708 * it was already linked to another inode we already
709 * had in our cache (and thus got spliced). To not
710 * confuse VFS (especially when inode is a directory),
711 * we don't link our dentry to that inode, return an
714 * This event should be rare and it happens only when
715 * we talk to old MDS. Recent MDS does not send traceless
716 * reply for request that creates new inode.
721 return PTR_ERR(result
);
724 static int ceph_mknod(struct inode
*dir
, struct dentry
*dentry
,
725 umode_t mode
, dev_t rdev
)
727 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
728 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
729 struct ceph_mds_request
*req
;
730 struct ceph_acls_info acls
= {};
733 if (ceph_snap(dir
) != CEPH_NOSNAP
)
736 err
= ceph_pre_init_acls(dir
, &mode
, &acls
);
740 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
741 dir
, dentry
, mode
, rdev
);
742 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_MKNOD
, USE_AUTH_MDS
);
747 req
->r_dentry
= dget(dentry
);
749 req
->r_locked_dir
= dir
;
750 req
->r_args
.mknod
.mode
= cpu_to_le32(mode
);
751 req
->r_args
.mknod
.rdev
= cpu_to_le32(rdev
);
752 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
753 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
755 req
->r_pagelist
= acls
.pagelist
;
756 acls
.pagelist
= NULL
;
758 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
759 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
760 err
= ceph_handle_notrace_create(dir
, dentry
);
761 ceph_mdsc_put_request(req
);
764 ceph_init_inode_acls(d_inode(dentry
), &acls
);
767 ceph_release_acls_info(&acls
);
771 static int ceph_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
774 return ceph_mknod(dir
, dentry
, mode
, 0);
777 static int ceph_symlink(struct inode
*dir
, struct dentry
*dentry
,
780 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
781 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
782 struct ceph_mds_request
*req
;
785 if (ceph_snap(dir
) != CEPH_NOSNAP
)
788 dout("symlink in dir %p dentry %p to '%s'\n", dir
, dentry
, dest
);
789 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SYMLINK
, USE_AUTH_MDS
);
794 req
->r_path2
= kstrdup(dest
, GFP_KERNEL
);
797 ceph_mdsc_put_request(req
);
800 req
->r_locked_dir
= dir
;
801 req
->r_dentry
= dget(dentry
);
803 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
804 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
805 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
806 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
807 err
= ceph_handle_notrace_create(dir
, dentry
);
808 ceph_mdsc_put_request(req
);
815 static int ceph_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
817 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
818 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
819 struct ceph_mds_request
*req
;
820 struct ceph_acls_info acls
= {};
824 if (ceph_snap(dir
) == CEPH_SNAPDIR
) {
825 /* mkdir .snap/foo is a MKSNAP */
826 op
= CEPH_MDS_OP_MKSNAP
;
827 dout("mksnap dir %p snap '%pd' dn %p\n", dir
,
829 } else if (ceph_snap(dir
) == CEPH_NOSNAP
) {
830 dout("mkdir dir %p dn %p mode 0%ho\n", dir
, dentry
, mode
);
831 op
= CEPH_MDS_OP_MKDIR
;
837 err
= ceph_pre_init_acls(dir
, &mode
, &acls
);
841 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
847 req
->r_dentry
= dget(dentry
);
849 req
->r_locked_dir
= dir
;
850 req
->r_args
.mkdir
.mode
= cpu_to_le32(mode
);
851 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
852 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
854 req
->r_pagelist
= acls
.pagelist
;
855 acls
.pagelist
= NULL
;
857 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
859 !req
->r_reply_info
.head
->is_target
&&
860 !req
->r_reply_info
.head
->is_dentry
)
861 err
= ceph_handle_notrace_create(dir
, dentry
);
862 ceph_mdsc_put_request(req
);
865 ceph_init_inode_acls(d_inode(dentry
), &acls
);
868 ceph_release_acls_info(&acls
);
872 static int ceph_link(struct dentry
*old_dentry
, struct inode
*dir
,
873 struct dentry
*dentry
)
875 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
876 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
877 struct ceph_mds_request
*req
;
880 if (ceph_snap(dir
) != CEPH_NOSNAP
)
883 dout("link in dir %p old_dentry %p dentry %p\n", dir
,
885 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_LINK
, USE_AUTH_MDS
);
890 req
->r_dentry
= dget(dentry
);
892 req
->r_old_dentry
= dget(old_dentry
);
893 req
->r_locked_dir
= dir
;
894 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
895 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
896 /* release LINK_SHARED on source inode (mds will lock it) */
897 req
->r_old_inode_drop
= CEPH_CAP_LINK_SHARED
;
898 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
901 } else if (!req
->r_reply_info
.head
->is_dentry
) {
902 ihold(d_inode(old_dentry
));
903 d_instantiate(dentry
, d_inode(old_dentry
));
905 ceph_mdsc_put_request(req
);
910 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
911 * looks like the link count will hit 0, drop any other caps (other
912 * than PIN) we don't specifically want (due to the file still being
915 static int drop_caps_for_unlink(struct inode
*inode
)
917 struct ceph_inode_info
*ci
= ceph_inode(inode
);
918 int drop
= CEPH_CAP_LINK_SHARED
| CEPH_CAP_LINK_EXCL
;
920 spin_lock(&ci
->i_ceph_lock
);
921 if (inode
->i_nlink
== 1) {
922 drop
|= ~(__ceph_caps_wanted(ci
) | CEPH_CAP_PIN
);
923 ci
->i_ceph_flags
|= CEPH_I_NODELAY
;
925 spin_unlock(&ci
->i_ceph_lock
);
930 * rmdir and unlink are differ only by the metadata op code
932 static int ceph_unlink(struct inode
*dir
, struct dentry
*dentry
)
934 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
935 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
936 struct inode
*inode
= d_inode(dentry
);
937 struct ceph_mds_request
*req
;
941 if (ceph_snap(dir
) == CEPH_SNAPDIR
) {
942 /* rmdir .snap/foo is RMSNAP */
943 dout("rmsnap dir %p '%pd' dn %p\n", dir
, dentry
, dentry
);
944 op
= CEPH_MDS_OP_RMSNAP
;
945 } else if (ceph_snap(dir
) == CEPH_NOSNAP
) {
946 dout("unlink/rmdir dir %p dn %p inode %p\n",
948 op
= d_is_dir(dentry
) ?
949 CEPH_MDS_OP_RMDIR
: CEPH_MDS_OP_UNLINK
;
952 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
957 req
->r_dentry
= dget(dentry
);
959 req
->r_locked_dir
= dir
;
960 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
961 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
962 req
->r_inode_drop
= drop_caps_for_unlink(inode
);
963 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
964 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
966 ceph_mdsc_put_request(req
);
971 static int ceph_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
972 struct inode
*new_dir
, struct dentry
*new_dentry
)
974 struct ceph_fs_client
*fsc
= ceph_sb_to_client(old_dir
->i_sb
);
975 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
976 struct ceph_mds_request
*req
;
977 int op
= CEPH_MDS_OP_RENAME
;
980 if (ceph_snap(old_dir
) != ceph_snap(new_dir
))
982 if (ceph_snap(old_dir
) != CEPH_NOSNAP
) {
983 if (old_dir
== new_dir
&& ceph_snap(old_dir
) == CEPH_SNAPDIR
)
984 op
= CEPH_MDS_OP_RENAMESNAP
;
988 dout("rename dir %p dentry %p to dir %p dentry %p\n",
989 old_dir
, old_dentry
, new_dir
, new_dentry
);
990 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
994 req
->r_dentry
= dget(new_dentry
);
996 req
->r_old_dentry
= dget(old_dentry
);
997 req
->r_old_dentry_dir
= old_dir
;
998 req
->r_locked_dir
= new_dir
;
999 req
->r_old_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1000 req
->r_old_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1001 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1002 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1003 /* release LINK_RDCACHE on source inode (mds will lock it) */
1004 req
->r_old_inode_drop
= CEPH_CAP_LINK_SHARED
;
1005 if (d_really_is_positive(new_dentry
))
1006 req
->r_inode_drop
= drop_caps_for_unlink(d_inode(new_dentry
));
1007 err
= ceph_mdsc_do_request(mdsc
, old_dir
, req
);
1008 if (!err
&& !req
->r_reply_info
.head
->is_dentry
) {
1010 * Normally d_move() is done by fill_trace (called by
1011 * do_request, above). If there is no trace, we need
1015 /* d_move screws up sibling dentries' offsets */
1016 ceph_dir_clear_complete(old_dir
);
1017 ceph_dir_clear_complete(new_dir
);
1019 d_move(old_dentry
, new_dentry
);
1021 /* ensure target dentry is invalidated, despite
1022 rehashing bug in vfs_rename_dir */
1023 ceph_invalidate_dentry_lease(new_dentry
);
1025 ceph_mdsc_put_request(req
);
1030 * Ensure a dentry lease will no longer revalidate.
1032 void ceph_invalidate_dentry_lease(struct dentry
*dentry
)
1034 spin_lock(&dentry
->d_lock
);
1035 dentry
->d_time
= jiffies
;
1036 ceph_dentry(dentry
)->lease_shared_gen
= 0;
1037 spin_unlock(&dentry
->d_lock
);
1041 * Check if dentry lease is valid. If not, delete the lease. Try to
1042 * renew if the least is more than half up.
1044 static int dentry_lease_is_valid(struct dentry
*dentry
)
1046 struct ceph_dentry_info
*di
;
1047 struct ceph_mds_session
*s
;
1051 struct ceph_mds_session
*session
= NULL
;
1052 struct inode
*dir
= NULL
;
1055 spin_lock(&dentry
->d_lock
);
1056 di
= ceph_dentry(dentry
);
1057 if (di
->lease_session
) {
1058 s
= di
->lease_session
;
1059 spin_lock(&s
->s_gen_ttl_lock
);
1062 spin_unlock(&s
->s_gen_ttl_lock
);
1064 if (di
->lease_gen
== gen
&&
1065 time_before(jiffies
, dentry
->d_time
) &&
1066 time_before(jiffies
, ttl
)) {
1068 if (di
->lease_renew_after
&&
1069 time_after(jiffies
, di
->lease_renew_after
)) {
1070 /* we should renew */
1071 dir
= d_inode(dentry
->d_parent
);
1072 session
= ceph_get_mds_session(s
);
1073 seq
= di
->lease_seq
;
1074 di
->lease_renew_after
= 0;
1075 di
->lease_renew_from
= jiffies
;
1079 spin_unlock(&dentry
->d_lock
);
1082 ceph_mdsc_lease_send_msg(session
, dir
, dentry
,
1083 CEPH_MDS_LEASE_RENEW
, seq
);
1084 ceph_put_mds_session(session
);
1086 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry
, valid
);
1091 * Check if directory-wide content lease/cap is valid.
1093 static int dir_lease_is_valid(struct inode
*dir
, struct dentry
*dentry
)
1095 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1096 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
1099 spin_lock(&ci
->i_ceph_lock
);
1100 if (ci
->i_shared_gen
== di
->lease_shared_gen
)
1101 valid
= __ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1);
1102 spin_unlock(&ci
->i_ceph_lock
);
1103 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1104 dir
, (unsigned)ci
->i_shared_gen
, dentry
,
1105 (unsigned)di
->lease_shared_gen
, valid
);
1110 * Check if cached dentry can be trusted.
1112 static int ceph_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1115 struct dentry
*parent
;
1118 if (flags
& LOOKUP_RCU
)
1121 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry
,
1122 dentry
, d_inode(dentry
), ceph_dentry(dentry
)->offset
);
1124 parent
= dget_parent(dentry
);
1125 dir
= d_inode(parent
);
1127 /* always trust cached snapped dentries, snapdir dentry */
1128 if (ceph_snap(dir
) != CEPH_NOSNAP
) {
1129 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry
,
1130 dentry
, d_inode(dentry
));
1132 } else if (d_really_is_positive(dentry
) &&
1133 ceph_snap(d_inode(dentry
)) == CEPH_SNAPDIR
) {
1135 } else if (dentry_lease_is_valid(dentry
) ||
1136 dir_lease_is_valid(dir
, dentry
)) {
1137 if (d_really_is_positive(dentry
))
1138 valid
= ceph_is_any_caps(d_inode(dentry
));
1144 struct ceph_mds_client
*mdsc
=
1145 ceph_sb_to_client(dir
->i_sb
)->mdsc
;
1146 struct ceph_mds_request
*req
;
1149 op
= ceph_snap(dir
) == CEPH_SNAPDIR
?
1150 CEPH_MDS_OP_LOOKUPSNAP
: CEPH_MDS_OP_LOOKUP
;
1151 req
= ceph_mdsc_create_request(mdsc
, op
, USE_ANY_MDS
);
1153 req
->r_dentry
= dget(dentry
);
1154 req
->r_num_caps
= 2;
1156 mask
= CEPH_STAT_CAP_INODE
| CEPH_CAP_AUTH_SHARED
;
1157 if (ceph_security_xattr_wanted(dir
))
1158 mask
|= CEPH_CAP_XATTR_SHARED
;
1159 req
->r_args
.getattr
.mask
= mask
;
1161 req
->r_locked_dir
= dir
;
1162 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1163 if (err
== 0 || err
== -ENOENT
) {
1164 if (dentry
== req
->r_dentry
) {
1165 valid
= !d_unhashed(dentry
);
1167 d_invalidate(req
->r_dentry
);
1171 ceph_mdsc_put_request(req
);
1172 dout("d_revalidate %p lookup result=%d\n",
1177 dout("d_revalidate %p %s\n", dentry
, valid
? "valid" : "invalid");
1179 ceph_dentry_lru_touch(dentry
);
1181 ceph_dir_clear_complete(dir
);
1189 * Release our ceph_dentry_info.
1191 static void ceph_d_release(struct dentry
*dentry
)
1193 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
1195 dout("d_release %p\n", dentry
);
1196 ceph_dentry_lru_del(dentry
);
1197 if (di
->lease_session
)
1198 ceph_put_mds_session(di
->lease_session
);
1199 kmem_cache_free(ceph_dentry_cachep
, di
);
1200 dentry
->d_fsdata
= NULL
;
1203 static int ceph_snapdir_d_revalidate(struct dentry
*dentry
,
1207 * Eventually, we'll want to revalidate snapped metadata
1208 * too... probably...
1214 * When the VFS prunes a dentry from the cache, we need to clear the
1215 * complete flag on the parent directory.
1217 * Called under dentry->d_lock.
1219 static void ceph_d_prune(struct dentry
*dentry
)
1221 dout("ceph_d_prune %p\n", dentry
);
1223 /* do we have a valid parent? */
1224 if (IS_ROOT(dentry
))
1227 /* if we are not hashed, we don't affect dir's completeness */
1228 if (d_unhashed(dentry
))
1232 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1233 * cleared until d_release
1235 ceph_dir_clear_complete(d_inode(dentry
->d_parent
));
1239 * read() on a dir. This weird interface hack only works if mounted
1240 * with '-o dirstat'.
1242 static ssize_t
ceph_read_dir(struct file
*file
, char __user
*buf
, size_t size
,
1245 struct ceph_file_info
*cf
= file
->private_data
;
1246 struct inode
*inode
= file_inode(file
);
1247 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1249 const int bufsize
= 1024;
1251 if (!ceph_test_mount_opt(ceph_sb_to_client(inode
->i_sb
), DIRSTAT
))
1254 if (!cf
->dir_info
) {
1255 cf
->dir_info
= kmalloc(bufsize
, GFP_KERNEL
);
1259 snprintf(cf
->dir_info
, bufsize
,
1262 " subdirs: %20lld\n"
1263 "rentries: %20lld\n"
1265 " rsubdirs: %20lld\n"
1267 "rctime: %10ld.%09ld\n",
1268 ci
->i_files
+ ci
->i_subdirs
,
1271 ci
->i_rfiles
+ ci
->i_rsubdirs
,
1275 (long)ci
->i_rctime
.tv_sec
,
1276 (long)ci
->i_rctime
.tv_nsec
);
1279 if (*ppos
>= cf
->dir_info_len
)
1281 size
= min_t(unsigned, size
, cf
->dir_info_len
-*ppos
);
1282 left
= copy_to_user(buf
, cf
->dir_info
+ *ppos
, size
);
1285 *ppos
+= (size
- left
);
1290 * We maintain a private dentry LRU.
1292 * FIXME: this needs to be changed to a per-mds lru to be useful.
1294 void ceph_dentry_lru_add(struct dentry
*dn
)
1296 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1297 struct ceph_mds_client
*mdsc
;
1299 dout("dentry_lru_add %p %p '%pd'\n", di
, dn
, dn
);
1300 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1301 spin_lock(&mdsc
->dentry_lru_lock
);
1302 list_add_tail(&di
->lru
, &mdsc
->dentry_lru
);
1304 spin_unlock(&mdsc
->dentry_lru_lock
);
1307 void ceph_dentry_lru_touch(struct dentry
*dn
)
1309 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1310 struct ceph_mds_client
*mdsc
;
1312 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di
, dn
, dn
,
1314 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1315 spin_lock(&mdsc
->dentry_lru_lock
);
1316 list_move_tail(&di
->lru
, &mdsc
->dentry_lru
);
1317 spin_unlock(&mdsc
->dentry_lru_lock
);
1320 void ceph_dentry_lru_del(struct dentry
*dn
)
1322 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1323 struct ceph_mds_client
*mdsc
;
1325 dout("dentry_lru_del %p %p '%pd'\n", di
, dn
, dn
);
1326 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1327 spin_lock(&mdsc
->dentry_lru_lock
);
1328 list_del_init(&di
->lru
);
1330 spin_unlock(&mdsc
->dentry_lru_lock
);
1334 * Return name hash for a given dentry. This is dependent on
1335 * the parent directory's hash function.
1337 unsigned ceph_dentry_hash(struct inode
*dir
, struct dentry
*dn
)
1339 struct ceph_inode_info
*dci
= ceph_inode(dir
);
1341 switch (dci
->i_dir_layout
.dl_dir_hash
) {
1342 case 0: /* for backward compat */
1343 case CEPH_STR_HASH_LINUX
:
1344 return dn
->d_name
.hash
;
1347 return ceph_str_hash(dci
->i_dir_layout
.dl_dir_hash
,
1348 dn
->d_name
.name
, dn
->d_name
.len
);
1352 const struct file_operations ceph_dir_fops
= {
1353 .read
= ceph_read_dir
,
1354 .iterate
= ceph_readdir
,
1355 .llseek
= ceph_dir_llseek
,
1357 .release
= ceph_release
,
1358 .unlocked_ioctl
= ceph_ioctl
,
1359 .fsync
= ceph_fsync
,
1362 const struct file_operations ceph_snapdir_fops
= {
1363 .iterate
= ceph_readdir
,
1364 .llseek
= ceph_dir_llseek
,
1366 .release
= ceph_release
,
1369 const struct inode_operations ceph_dir_iops
= {
1370 .lookup
= ceph_lookup
,
1371 .permission
= ceph_permission
,
1372 .getattr
= ceph_getattr
,
1373 .setattr
= ceph_setattr
,
1374 .setxattr
= ceph_setxattr
,
1375 .getxattr
= ceph_getxattr
,
1376 .listxattr
= ceph_listxattr
,
1377 .removexattr
= ceph_removexattr
,
1378 .get_acl
= ceph_get_acl
,
1379 .set_acl
= ceph_set_acl
,
1380 .mknod
= ceph_mknod
,
1381 .symlink
= ceph_symlink
,
1382 .mkdir
= ceph_mkdir
,
1384 .unlink
= ceph_unlink
,
1385 .rmdir
= ceph_unlink
,
1386 .rename
= ceph_rename
,
1387 .create
= ceph_create
,
1388 .atomic_open
= ceph_atomic_open
,
1391 const struct inode_operations ceph_snapdir_iops
= {
1392 .lookup
= ceph_lookup
,
1393 .permission
= ceph_permission
,
1394 .getattr
= ceph_getattr
,
1395 .mkdir
= ceph_mkdir
,
1396 .rmdir
= ceph_unlink
,
1397 .rename
= ceph_rename
,
1400 const struct dentry_operations ceph_dentry_ops
= {
1401 .d_revalidate
= ceph_d_revalidate
,
1402 .d_release
= ceph_d_release
,
1403 .d_prune
= ceph_d_prune
,
1406 const struct dentry_operations ceph_snapdir_dentry_ops
= {
1407 .d_revalidate
= ceph_snapdir_d_revalidate
,
1408 .d_release
= ceph_d_release
,
1411 const struct dentry_operations ceph_snap_dentry_ops
= {
1412 .d_release
= ceph_d_release
,
1413 .d_prune
= ceph_d_prune
,