1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/spinlock.h>
5 #include <linux/fs_struct.h>
6 #include <linux/namei.h>
7 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/xattr.h>
12 #include "mds_client.h"
15 * Directory operations: readdir, lookup, create, link, unlink,
20 * Ceph MDS operations are specified in terms of a base ino and
21 * relative path. Thus, the client can specify an operation on a
22 * specific inode (e.g., a getattr due to fstat(2)), or as a path
23 * relative to, say, the root directory.
25 * Normally, we limit ourselves to strict inode ops (no path component)
26 * or dentry operations (a single path component relative to an ino). The
27 * exception to this is open_root_dentry(), which will open the mount
31 const struct dentry_operations ceph_dentry_ops
;
34 * Initialize ceph dentry state.
36 static int ceph_d_init(struct dentry
*dentry
)
38 struct ceph_dentry_info
*di
;
40 di
= kmem_cache_zalloc(ceph_dentry_cachep
, GFP_KERNEL
);
42 return -ENOMEM
; /* oh well */
45 di
->lease_session
= NULL
;
47 dentry
->d_fsdata
= di
;
48 ceph_dentry_lru_add(dentry
);
53 * for f_pos for readdir:
55 * (0xff << 52) | ((24 bits hash) << 28) |
56 * (the nth entry has hash collision);
58 * ((frag value) << 28) | (the nth entry in frag);
60 #define OFFSET_BITS 28
61 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
62 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
63 loff_t
ceph_make_fpos(unsigned high
, unsigned off
, bool hash_order
)
65 loff_t fpos
= ((loff_t
)high
<< 28) | (loff_t
)off
;
71 static bool is_hash_order(loff_t p
)
73 return (p
& HASH_ORDER
) == HASH_ORDER
;
76 static unsigned fpos_frag(loff_t p
)
78 return p
>> OFFSET_BITS
;
81 static unsigned fpos_hash(loff_t p
)
83 return ceph_frag_value(fpos_frag(p
));
86 static unsigned fpos_off(loff_t p
)
88 return p
& OFFSET_MASK
;
91 static int fpos_cmp(loff_t l
, loff_t r
)
93 int v
= ceph_frag_compare(fpos_frag(l
), fpos_frag(r
));
96 return (int)(fpos_off(l
) - fpos_off(r
));
100 * make note of the last dentry we read, so we can
101 * continue at the same lexicographical point,
102 * regardless of what dir changes take place on the
105 static int note_last_dentry(struct ceph_file_info
*fi
, const char *name
,
106 int len
, unsigned next_offset
)
108 char *buf
= kmalloc(len
+1, GFP_KERNEL
);
111 kfree(fi
->last_name
);
113 memcpy(fi
->last_name
, name
, len
);
114 fi
->last_name
[len
] = 0;
115 fi
->next_offset
= next_offset
;
116 dout("note_last_dentry '%s'\n", fi
->last_name
);
121 static struct dentry
*
122 __dcache_find_get_entry(struct dentry
*parent
, u64 idx
,
123 struct ceph_readdir_cache_control
*cache_ctl
)
125 struct inode
*dir
= d_inode(parent
);
126 struct dentry
*dentry
;
127 unsigned idx_mask
= (PAGE_SIZE
/ sizeof(struct dentry
*)) - 1;
128 loff_t ptr_pos
= idx
* sizeof(struct dentry
*);
129 pgoff_t ptr_pgoff
= ptr_pos
>> PAGE_SHIFT
;
131 if (ptr_pos
>= i_size_read(dir
))
134 if (!cache_ctl
->page
|| ptr_pgoff
!= page_index(cache_ctl
->page
)) {
135 ceph_readdir_cache_release(cache_ctl
);
136 cache_ctl
->page
= find_lock_page(&dir
->i_data
, ptr_pgoff
);
137 if (!cache_ctl
->page
) {
138 dout(" page %lu not found\n", ptr_pgoff
);
139 return ERR_PTR(-EAGAIN
);
141 /* reading/filling the cache are serialized by
142 i_mutex, no need to use page lock */
143 unlock_page(cache_ctl
->page
);
144 cache_ctl
->dentries
= kmap(cache_ctl
->page
);
147 cache_ctl
->index
= idx
& idx_mask
;
150 spin_lock(&parent
->d_lock
);
151 /* check i_size again here, because empty directory can be
152 * marked as complete while not holding the i_mutex. */
153 if (ceph_dir_is_complete_ordered(dir
) && ptr_pos
< i_size_read(dir
))
154 dentry
= cache_ctl
->dentries
[cache_ctl
->index
];
157 spin_unlock(&parent
->d_lock
);
158 if (dentry
&& !lockref_get_not_dead(&dentry
->d_lockref
))
161 return dentry
? : ERR_PTR(-EAGAIN
);
165 * When possible, we try to satisfy a readdir by peeking at the
166 * dcache. We make this work by carefully ordering dentries on
167 * d_child when we initially get results back from the MDS, and
168 * falling back to a "normal" sync readdir if any dentries in the dir
171 * Complete dir indicates that we have all dentries in the dir. It is
172 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
173 * the MDS if/when the directory is modified).
175 static int __dcache_readdir(struct file
*file
, struct dir_context
*ctx
,
178 struct ceph_file_info
*fi
= file
->private_data
;
179 struct dentry
*parent
= file
->f_path
.dentry
;
180 struct inode
*dir
= d_inode(parent
);
181 struct dentry
*dentry
, *last
= NULL
;
182 struct ceph_dentry_info
*di
;
183 struct ceph_readdir_cache_control cache_ctl
= {};
187 dout("__dcache_readdir %p v%u at %llx\n", dir
, (unsigned)shared_gen
, ctx
->pos
);
189 /* search start position */
191 u64 count
= div_u64(i_size_read(dir
), sizeof(struct dentry
*));
193 u64 step
= count
>> 1;
194 dentry
= __dcache_find_get_entry(parent
, idx
+ step
,
197 /* use linar search */
201 if (IS_ERR(dentry
)) {
202 err
= PTR_ERR(dentry
);
205 di
= ceph_dentry(dentry
);
206 spin_lock(&dentry
->d_lock
);
207 if (fpos_cmp(di
->offset
, ctx
->pos
) < 0) {
213 spin_unlock(&dentry
->d_lock
);
217 dout("__dcache_readdir %p cache idx %llu\n", dir
, idx
);
222 bool emit_dentry
= false;
223 dentry
= __dcache_find_get_entry(parent
, idx
++, &cache_ctl
);
225 fi
->flags
|= CEPH_F_ATEND
;
229 if (IS_ERR(dentry
)) {
230 err
= PTR_ERR(dentry
);
234 spin_lock(&dentry
->d_lock
);
235 di
= ceph_dentry(dentry
);
236 if (d_unhashed(dentry
) ||
237 d_really_is_negative(dentry
) ||
238 di
->lease_shared_gen
!= shared_gen
) {
239 spin_unlock(&dentry
->d_lock
);
244 if (fpos_cmp(ctx
->pos
, di
->offset
) <= 0) {
247 spin_unlock(&dentry
->d_lock
);
250 dout(" %llx dentry %p %pd %p\n", di
->offset
,
251 dentry
, dentry
, d_inode(dentry
));
252 ctx
->pos
= di
->offset
;
253 if (!dir_emit(ctx
, dentry
->d_name
.name
,
255 ceph_translate_ino(dentry
->d_sb
,
256 d_inode(dentry
)->i_ino
),
257 d_inode(dentry
)->i_mode
>> 12)) {
272 ceph_readdir_cache_release(&cache_ctl
);
275 di
= ceph_dentry(last
);
276 ret
= note_last_dentry(fi
, last
->d_name
.name
, last
->d_name
.len
,
277 fpos_off(di
->offset
) + 1);
281 /* last_name no longer match cache index */
282 if (fi
->readdir_cache_idx
>= 0) {
283 fi
->readdir_cache_idx
= -1;
284 fi
->dir_release_count
= 0;
290 static bool need_send_readdir(struct ceph_file_info
*fi
, loff_t pos
)
292 if (!fi
->last_readdir
)
294 if (is_hash_order(pos
))
295 return !ceph_frag_contains_value(fi
->frag
, fpos_hash(pos
));
297 return fi
->frag
!= fpos_frag(pos
);
300 static int ceph_readdir(struct file
*file
, struct dir_context
*ctx
)
302 struct ceph_file_info
*fi
= file
->private_data
;
303 struct inode
*inode
= file_inode(file
);
304 struct ceph_inode_info
*ci
= ceph_inode(inode
);
305 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
306 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
310 struct ceph_mds_reply_info_parsed
*rinfo
;
312 dout("readdir %p file %p pos %llx\n", inode
, file
, ctx
->pos
);
313 if (fi
->flags
& CEPH_F_ATEND
)
316 /* always start with . and .. */
318 dout("readdir off 0 -> '.'\n");
319 if (!dir_emit(ctx
, ".", 1,
320 ceph_translate_ino(inode
->i_sb
, inode
->i_ino
),
321 inode
->i_mode
>> 12))
326 ino_t ino
= parent_ino(file
->f_path
.dentry
);
327 dout("readdir off 1 -> '..'\n");
328 if (!dir_emit(ctx
, "..", 2,
329 ceph_translate_ino(inode
->i_sb
, ino
),
330 inode
->i_mode
>> 12))
335 /* can we use the dcache? */
336 spin_lock(&ci
->i_ceph_lock
);
337 if (ceph_test_mount_opt(fsc
, DCACHE
) &&
338 !ceph_test_mount_opt(fsc
, NOASYNCREADDIR
) &&
339 ceph_snap(inode
) != CEPH_SNAPDIR
&&
340 __ceph_dir_is_complete_ordered(ci
) &&
341 __ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1)) {
342 int shared_gen
= atomic_read(&ci
->i_shared_gen
);
343 spin_unlock(&ci
->i_ceph_lock
);
344 err
= __dcache_readdir(file
, ctx
, shared_gen
);
348 spin_unlock(&ci
->i_ceph_lock
);
351 /* proceed with a normal readdir */
353 /* do we have the correct frag content buffered? */
354 if (need_send_readdir(fi
, ctx
->pos
)) {
355 struct ceph_mds_request
*req
;
356 int op
= ceph_snap(inode
) == CEPH_SNAPDIR
?
357 CEPH_MDS_OP_LSSNAP
: CEPH_MDS_OP_READDIR
;
359 /* discard old result, if any */
360 if (fi
->last_readdir
) {
361 ceph_mdsc_put_request(fi
->last_readdir
);
362 fi
->last_readdir
= NULL
;
365 if (is_hash_order(ctx
->pos
)) {
366 /* fragtree isn't always accurate. choose frag
367 * based on previous reply when possible. */
368 if (frag
== (unsigned)-1)
369 frag
= ceph_choose_frag(ci
, fpos_hash(ctx
->pos
),
372 frag
= fpos_frag(ctx
->pos
);
375 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
376 ceph_vinop(inode
), frag
, fi
->last_name
);
377 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
380 err
= ceph_alloc_readdir_reply_buffer(req
, inode
);
382 ceph_mdsc_put_request(req
);
385 /* hints to request -> mds selection code */
386 req
->r_direct_mode
= USE_AUTH_MDS
;
387 if (op
== CEPH_MDS_OP_READDIR
) {
388 req
->r_direct_hash
= ceph_frag_value(frag
);
389 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH
, &req
->r_req_flags
);
392 req
->r_path2
= kstrdup(fi
->last_name
, GFP_KERNEL
);
394 ceph_mdsc_put_request(req
);
397 } else if (is_hash_order(ctx
->pos
)) {
398 req
->r_args
.readdir
.offset_hash
=
399 cpu_to_le32(fpos_hash(ctx
->pos
));
402 req
->r_dir_release_cnt
= fi
->dir_release_count
;
403 req
->r_dir_ordered_cnt
= fi
->dir_ordered_count
;
404 req
->r_readdir_cache_idx
= fi
->readdir_cache_idx
;
405 req
->r_readdir_offset
= fi
->next_offset
;
406 req
->r_args
.readdir
.frag
= cpu_to_le32(frag
);
407 req
->r_args
.readdir
.flags
=
408 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS
);
410 req
->r_inode
= inode
;
412 req
->r_dentry
= dget(file
->f_path
.dentry
);
413 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
415 ceph_mdsc_put_request(req
);
418 dout("readdir got and parsed readdir result=%d on "
419 "frag %x, end=%d, complete=%d, hash_order=%d\n",
421 (int)req
->r_reply_info
.dir_end
,
422 (int)req
->r_reply_info
.dir_complete
,
423 (int)req
->r_reply_info
.hash_order
);
425 rinfo
= &req
->r_reply_info
;
426 if (le32_to_cpu(rinfo
->dir_dir
->frag
) != frag
) {
427 frag
= le32_to_cpu(rinfo
->dir_dir
->frag
);
428 if (!rinfo
->hash_order
) {
429 fi
->next_offset
= req
->r_readdir_offset
;
430 /* adjust ctx->pos to beginning of frag */
431 ctx
->pos
= ceph_make_fpos(frag
,
438 fi
->last_readdir
= req
;
440 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE
, &req
->r_req_flags
)) {
441 fi
->readdir_cache_idx
= req
->r_readdir_cache_idx
;
442 if (fi
->readdir_cache_idx
< 0) {
443 /* preclude from marking dir ordered */
444 fi
->dir_ordered_count
= 0;
445 } else if (ceph_frag_is_leftmost(frag
) &&
446 fi
->next_offset
== 2) {
447 /* note dir version at start of readdir so
448 * we can tell if any dentries get dropped */
449 fi
->dir_release_count
= req
->r_dir_release_cnt
;
450 fi
->dir_ordered_count
= req
->r_dir_ordered_cnt
;
453 dout("readdir !did_prepopulate");
454 /* disable readdir cache */
455 fi
->readdir_cache_idx
= -1;
456 /* preclude from marking dir complete */
457 fi
->dir_release_count
= 0;
460 /* note next offset and last dentry name */
461 if (rinfo
->dir_nr
> 0) {
462 struct ceph_mds_reply_dir_entry
*rde
=
463 rinfo
->dir_entries
+ (rinfo
->dir_nr
-1);
464 unsigned next_offset
= req
->r_reply_info
.dir_end
?
465 2 : (fpos_off(rde
->offset
) + 1);
466 err
= note_last_dentry(fi
, rde
->name
, rde
->name_len
,
470 } else if (req
->r_reply_info
.dir_end
) {
476 rinfo
= &fi
->last_readdir
->r_reply_info
;
477 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
478 fi
->frag
, rinfo
->dir_nr
, ctx
->pos
,
479 rinfo
->dir_nr
? rinfo
->dir_entries
[0].offset
: 0LL);
482 /* search start position */
483 if (rinfo
->dir_nr
> 0) {
484 int step
, nr
= rinfo
->dir_nr
;
487 if (rinfo
->dir_entries
[i
+ step
].offset
< ctx
->pos
) {
495 for (; i
< rinfo
->dir_nr
; i
++) {
496 struct ceph_mds_reply_dir_entry
*rde
= rinfo
->dir_entries
+ i
;
497 struct ceph_vino vino
;
501 BUG_ON(rde
->offset
< ctx
->pos
);
503 ctx
->pos
= rde
->offset
;
504 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
505 i
, rinfo
->dir_nr
, ctx
->pos
,
506 rde
->name_len
, rde
->name
, &rde
->inode
.in
);
508 BUG_ON(!rde
->inode
.in
);
509 ftype
= le32_to_cpu(rde
->inode
.in
->mode
) >> 12;
510 vino
.ino
= le64_to_cpu(rde
->inode
.in
->ino
);
511 vino
.snap
= le64_to_cpu(rde
->inode
.in
->snapid
);
512 ino
= ceph_vino_to_ino(vino
);
514 if (!dir_emit(ctx
, rde
->name
, rde
->name_len
,
515 ceph_translate_ino(inode
->i_sb
, ino
), ftype
)) {
516 dout("filldir stopping us...\n");
522 ceph_mdsc_put_request(fi
->last_readdir
);
523 fi
->last_readdir
= NULL
;
525 if (fi
->next_offset
> 2) {
531 if (!ceph_frag_is_rightmost(fi
->frag
)) {
532 frag
= ceph_frag_next(fi
->frag
);
533 if (is_hash_order(ctx
->pos
)) {
534 loff_t new_pos
= ceph_make_fpos(ceph_frag_value(frag
),
535 fi
->next_offset
, true);
536 if (new_pos
> ctx
->pos
)
540 ctx
->pos
= ceph_make_fpos(frag
, fi
->next_offset
, false);
541 kfree(fi
->last_name
);
542 fi
->last_name
= NULL
;
544 dout("readdir next frag is %x\n", frag
);
547 fi
->flags
|= CEPH_F_ATEND
;
550 * if dir_release_count still matches the dir, no dentries
551 * were released during the whole readdir, and we should have
552 * the complete dir contents in our cache.
554 if (atomic64_read(&ci
->i_release_count
) == fi
->dir_release_count
) {
555 spin_lock(&ci
->i_ceph_lock
);
556 if (fi
->dir_ordered_count
== atomic64_read(&ci
->i_ordered_count
)) {
557 dout(" marking %p complete and ordered\n", inode
);
558 /* use i_size to track number of entries in
560 BUG_ON(fi
->readdir_cache_idx
< 0);
561 i_size_write(inode
, fi
->readdir_cache_idx
*
562 sizeof(struct dentry
*));
564 dout(" marking %p complete\n", inode
);
566 __ceph_dir_set_complete(ci
, fi
->dir_release_count
,
567 fi
->dir_ordered_count
);
568 spin_unlock(&ci
->i_ceph_lock
);
571 dout("readdir %p file %p done.\n", inode
, file
);
575 static void reset_readdir(struct ceph_file_info
*fi
)
577 if (fi
->last_readdir
) {
578 ceph_mdsc_put_request(fi
->last_readdir
);
579 fi
->last_readdir
= NULL
;
581 kfree(fi
->last_name
);
582 fi
->last_name
= NULL
;
583 fi
->dir_release_count
= 0;
584 fi
->readdir_cache_idx
= -1;
585 fi
->next_offset
= 2; /* compensate for . and .. */
586 fi
->flags
&= ~CEPH_F_ATEND
;
590 * discard buffered readdir content on seekdir(0), or seek to new frag,
591 * or seek prior to current chunk
593 static bool need_reset_readdir(struct ceph_file_info
*fi
, loff_t new_pos
)
595 struct ceph_mds_reply_info_parsed
*rinfo
;
599 if (is_hash_order(new_pos
)) {
600 /* no need to reset last_name for a forward seek when
601 * dentries are sotred in hash order */
602 } else if (fi
->frag
!= fpos_frag(new_pos
)) {
605 rinfo
= fi
->last_readdir
? &fi
->last_readdir
->r_reply_info
: NULL
;
606 if (!rinfo
|| !rinfo
->dir_nr
)
608 chunk_offset
= rinfo
->dir_entries
[0].offset
;
609 return new_pos
< chunk_offset
||
610 is_hash_order(new_pos
) != is_hash_order(chunk_offset
);
613 static loff_t
ceph_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
615 struct ceph_file_info
*fi
= file
->private_data
;
616 struct inode
*inode
= file
->f_mapping
->host
;
623 offset
+= file
->f_pos
;
627 retval
= -EOPNOTSUPP
;
633 if (need_reset_readdir(fi
, offset
)) {
634 dout("dir_llseek dropping %p content\n", file
);
636 } else if (is_hash_order(offset
) && offset
> file
->f_pos
) {
637 /* for hash offset, we don't know if a forward seek
638 * is within same frag */
639 fi
->dir_release_count
= 0;
640 fi
->readdir_cache_idx
= -1;
643 if (offset
!= file
->f_pos
) {
644 file
->f_pos
= offset
;
646 fi
->flags
&= ~CEPH_F_ATEND
;
656 * Handle lookups for the hidden .snap directory.
658 int ceph_handle_snapdir(struct ceph_mds_request
*req
,
659 struct dentry
*dentry
, int err
)
661 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dentry
->d_sb
);
662 struct inode
*parent
= d_inode(dentry
->d_parent
); /* we hold i_mutex */
665 if (err
== -ENOENT
&&
666 ceph_snap(parent
) == CEPH_NOSNAP
&&
667 strcmp(dentry
->d_name
.name
,
668 fsc
->mount_options
->snapdir_name
) == 0) {
669 struct inode
*inode
= ceph_get_snapdir(parent
);
670 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
671 dentry
, dentry
, inode
);
672 BUG_ON(!d_unhashed(dentry
));
673 d_add(dentry
, inode
);
680 * Figure out final result of a lookup/open request.
682 * Mainly, make sure we return the final req->r_dentry (if it already
683 * existed) in place of the original VFS-provided dentry when they
686 * Gracefully handle the case where the MDS replies with -ENOENT and
687 * no trace (which it may do, at its discretion, e.g., if it doesn't
688 * care to issue a lease on the negative dentry).
690 struct dentry
*ceph_finish_lookup(struct ceph_mds_request
*req
,
691 struct dentry
*dentry
, int err
)
693 if (err
== -ENOENT
) {
696 if (!req
->r_reply_info
.head
->is_dentry
) {
697 dout("ENOENT and no trace, dentry %p inode %p\n",
698 dentry
, d_inode(dentry
));
699 if (d_really_is_positive(dentry
)) {
708 dentry
= ERR_PTR(err
);
709 else if (dentry
!= req
->r_dentry
)
710 dentry
= dget(req
->r_dentry
); /* we got spliced */
716 static bool is_root_ceph_dentry(struct inode
*inode
, struct dentry
*dentry
)
718 return ceph_ino(inode
) == CEPH_INO_ROOT
&&
719 strncmp(dentry
->d_name
.name
, ".ceph", 5) == 0;
723 * Look up a single dir entry. If there is a lookup intent, inform
724 * the MDS so that it gets our 'caps wanted' value in a single op.
726 static struct dentry
*ceph_lookup(struct inode
*dir
, struct dentry
*dentry
,
729 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
730 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
731 struct ceph_mds_request
*req
;
736 dout("lookup %p dentry %p '%pd'\n",
737 dir
, dentry
, dentry
);
739 if (dentry
->d_name
.len
> NAME_MAX
)
740 return ERR_PTR(-ENAMETOOLONG
);
742 /* can we conclude ENOENT locally? */
743 if (d_really_is_negative(dentry
)) {
744 struct ceph_inode_info
*ci
= ceph_inode(dir
);
745 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
747 spin_lock(&ci
->i_ceph_lock
);
748 dout(" dir %p flags are %d\n", dir
, ci
->i_ceph_flags
);
749 if (strncmp(dentry
->d_name
.name
,
750 fsc
->mount_options
->snapdir_name
,
751 dentry
->d_name
.len
) &&
752 !is_root_ceph_dentry(dir
, dentry
) &&
753 ceph_test_mount_opt(fsc
, DCACHE
) &&
754 __ceph_dir_is_complete(ci
) &&
755 (__ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1))) {
756 spin_unlock(&ci
->i_ceph_lock
);
757 dout(" dir %p complete, -ENOENT\n", dir
);
759 di
->lease_shared_gen
= atomic_read(&ci
->i_shared_gen
);
762 spin_unlock(&ci
->i_ceph_lock
);
765 op
= ceph_snap(dir
) == CEPH_SNAPDIR
?
766 CEPH_MDS_OP_LOOKUPSNAP
: CEPH_MDS_OP_LOOKUP
;
767 req
= ceph_mdsc_create_request(mdsc
, op
, USE_ANY_MDS
);
769 return ERR_CAST(req
);
770 req
->r_dentry
= dget(dentry
);
773 mask
= CEPH_STAT_CAP_INODE
| CEPH_CAP_AUTH_SHARED
;
774 if (ceph_security_xattr_wanted(dir
))
775 mask
|= CEPH_CAP_XATTR_SHARED
;
776 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
779 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
780 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
781 err
= ceph_handle_snapdir(req
, dentry
, err
);
782 dentry
= ceph_finish_lookup(req
, dentry
, err
);
783 ceph_mdsc_put_request(req
); /* will dput(dentry) */
784 dout("lookup result=%p\n", dentry
);
789 * If we do a create but get no trace back from the MDS, follow up with
790 * a lookup (the VFS expects us to link up the provided dentry).
792 int ceph_handle_notrace_create(struct inode
*dir
, struct dentry
*dentry
)
794 struct dentry
*result
= ceph_lookup(dir
, dentry
, 0);
796 if (result
&& !IS_ERR(result
)) {
798 * We created the item, then did a lookup, and found
799 * it was already linked to another inode we already
800 * had in our cache (and thus got spliced). To not
801 * confuse VFS (especially when inode is a directory),
802 * we don't link our dentry to that inode, return an
805 * This event should be rare and it happens only when
806 * we talk to old MDS. Recent MDS does not send traceless
807 * reply for request that creates new inode.
812 return PTR_ERR(result
);
815 static int ceph_mknod(struct inode
*dir
, struct dentry
*dentry
,
816 umode_t mode
, dev_t rdev
)
818 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
819 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
820 struct ceph_mds_request
*req
;
821 struct ceph_acls_info acls
= {};
824 if (ceph_snap(dir
) != CEPH_NOSNAP
)
827 if (ceph_quota_is_max_files_exceeded(dir
))
830 err
= ceph_pre_init_acls(dir
, &mode
, &acls
);
834 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
835 dir
, dentry
, mode
, rdev
);
836 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_MKNOD
, USE_AUTH_MDS
);
841 req
->r_dentry
= dget(dentry
);
844 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
845 req
->r_args
.mknod
.mode
= cpu_to_le32(mode
);
846 req
->r_args
.mknod
.rdev
= cpu_to_le32(rdev
);
847 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
848 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
850 req
->r_pagelist
= acls
.pagelist
;
851 acls
.pagelist
= NULL
;
853 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
854 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
855 err
= ceph_handle_notrace_create(dir
, dentry
);
856 ceph_mdsc_put_request(req
);
859 ceph_init_inode_acls(d_inode(dentry
), &acls
);
862 ceph_release_acls_info(&acls
);
866 static int ceph_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
869 return ceph_mknod(dir
, dentry
, mode
, 0);
872 static int ceph_symlink(struct inode
*dir
, 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 if (ceph_quota_is_max_files_exceeded(dir
))
886 dout("symlink in dir %p dentry %p to '%s'\n", dir
, dentry
, dest
);
887 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SYMLINK
, USE_AUTH_MDS
);
892 req
->r_path2
= kstrdup(dest
, GFP_KERNEL
);
895 ceph_mdsc_put_request(req
);
899 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
900 req
->r_dentry
= dget(dentry
);
902 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
903 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
904 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
905 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
906 err
= ceph_handle_notrace_create(dir
, dentry
);
907 ceph_mdsc_put_request(req
);
914 static int ceph_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
916 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
917 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
918 struct ceph_mds_request
*req
;
919 struct ceph_acls_info acls
= {};
923 if (ceph_snap(dir
) == CEPH_SNAPDIR
) {
924 /* mkdir .snap/foo is a MKSNAP */
925 op
= CEPH_MDS_OP_MKSNAP
;
926 dout("mksnap dir %p snap '%pd' dn %p\n", dir
,
928 } else if (ceph_snap(dir
) == CEPH_NOSNAP
) {
929 dout("mkdir dir %p dn %p mode 0%ho\n", dir
, dentry
, mode
);
930 op
= CEPH_MDS_OP_MKDIR
;
935 if (ceph_quota_is_max_files_exceeded(dir
)) {
941 err
= ceph_pre_init_acls(dir
, &mode
, &acls
);
945 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
951 req
->r_dentry
= dget(dentry
);
954 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
955 req
->r_args
.mkdir
.mode
= cpu_to_le32(mode
);
956 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
957 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
959 req
->r_pagelist
= acls
.pagelist
;
960 acls
.pagelist
= NULL
;
962 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
964 !req
->r_reply_info
.head
->is_target
&&
965 !req
->r_reply_info
.head
->is_dentry
)
966 err
= ceph_handle_notrace_create(dir
, dentry
);
967 ceph_mdsc_put_request(req
);
970 ceph_init_inode_acls(d_inode(dentry
), &acls
);
973 ceph_release_acls_info(&acls
);
977 static int ceph_link(struct dentry
*old_dentry
, struct inode
*dir
,
978 struct dentry
*dentry
)
980 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
981 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
982 struct ceph_mds_request
*req
;
985 if (ceph_snap(dir
) != CEPH_NOSNAP
)
988 dout("link in dir %p old_dentry %p dentry %p\n", dir
,
990 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_LINK
, USE_AUTH_MDS
);
995 req
->r_dentry
= dget(dentry
);
997 req
->r_old_dentry
= dget(old_dentry
);
999 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
1000 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1001 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1002 /* release LINK_SHARED on source inode (mds will lock it) */
1003 req
->r_old_inode_drop
= CEPH_CAP_LINK_SHARED
;
1004 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
1007 } else if (!req
->r_reply_info
.head
->is_dentry
) {
1008 ihold(d_inode(old_dentry
));
1009 d_instantiate(dentry
, d_inode(old_dentry
));
1011 ceph_mdsc_put_request(req
);
1016 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1017 * looks like the link count will hit 0, drop any other caps (other
1018 * than PIN) we don't specifically want (due to the file still being
1021 static int drop_caps_for_unlink(struct inode
*inode
)
1023 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1024 int drop
= CEPH_CAP_LINK_SHARED
| CEPH_CAP_LINK_EXCL
;
1026 spin_lock(&ci
->i_ceph_lock
);
1027 if (inode
->i_nlink
== 1) {
1028 drop
|= ~(__ceph_caps_wanted(ci
) | CEPH_CAP_PIN
);
1029 ci
->i_ceph_flags
|= CEPH_I_NODELAY
;
1031 spin_unlock(&ci
->i_ceph_lock
);
1036 * rmdir and unlink are differ only by the metadata op code
1038 static int ceph_unlink(struct inode
*dir
, struct dentry
*dentry
)
1040 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dir
->i_sb
);
1041 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1042 struct inode
*inode
= d_inode(dentry
);
1043 struct ceph_mds_request
*req
;
1047 if (ceph_snap(dir
) == CEPH_SNAPDIR
) {
1048 /* rmdir .snap/foo is RMSNAP */
1049 dout("rmsnap dir %p '%pd' dn %p\n", dir
, dentry
, dentry
);
1050 op
= CEPH_MDS_OP_RMSNAP
;
1051 } else if (ceph_snap(dir
) == CEPH_NOSNAP
) {
1052 dout("unlink/rmdir dir %p dn %p inode %p\n",
1053 dir
, dentry
, inode
);
1054 op
= d_is_dir(dentry
) ?
1055 CEPH_MDS_OP_RMDIR
: CEPH_MDS_OP_UNLINK
;
1058 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
1063 req
->r_dentry
= dget(dentry
);
1064 req
->r_num_caps
= 2;
1065 req
->r_parent
= dir
;
1066 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
1067 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1068 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1069 req
->r_inode_drop
= drop_caps_for_unlink(inode
);
1070 err
= ceph_mdsc_do_request(mdsc
, dir
, req
);
1071 if (!err
&& !req
->r_reply_info
.head
->is_dentry
)
1073 ceph_mdsc_put_request(req
);
1078 static int ceph_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1079 struct inode
*new_dir
, struct dentry
*new_dentry
,
1082 struct ceph_fs_client
*fsc
= ceph_sb_to_client(old_dir
->i_sb
);
1083 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1084 struct ceph_mds_request
*req
;
1085 int op
= CEPH_MDS_OP_RENAME
;
1091 if (ceph_snap(old_dir
) != ceph_snap(new_dir
))
1093 if (ceph_snap(old_dir
) != CEPH_NOSNAP
) {
1094 if (old_dir
== new_dir
&& ceph_snap(old_dir
) == CEPH_SNAPDIR
)
1095 op
= CEPH_MDS_OP_RENAMESNAP
;
1099 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1100 old_dir
, old_dentry
, new_dir
, new_dentry
);
1101 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
1103 return PTR_ERR(req
);
1105 req
->r_dentry
= dget(new_dentry
);
1106 req
->r_num_caps
= 2;
1107 req
->r_old_dentry
= dget(old_dentry
);
1108 req
->r_old_dentry_dir
= old_dir
;
1109 req
->r_parent
= new_dir
;
1110 set_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
);
1111 req
->r_old_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1112 req
->r_old_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1113 req
->r_dentry_drop
= CEPH_CAP_FILE_SHARED
;
1114 req
->r_dentry_unless
= CEPH_CAP_FILE_EXCL
;
1115 /* release LINK_RDCACHE on source inode (mds will lock it) */
1116 req
->r_old_inode_drop
= CEPH_CAP_LINK_SHARED
;
1117 if (d_really_is_positive(new_dentry
))
1118 req
->r_inode_drop
= drop_caps_for_unlink(d_inode(new_dentry
));
1119 err
= ceph_mdsc_do_request(mdsc
, old_dir
, req
);
1120 if (!err
&& !req
->r_reply_info
.head
->is_dentry
) {
1122 * Normally d_move() is done by fill_trace (called by
1123 * do_request, above). If there is no trace, we need
1127 /* d_move screws up sibling dentries' offsets */
1128 ceph_dir_clear_complete(old_dir
);
1129 ceph_dir_clear_complete(new_dir
);
1131 d_move(old_dentry
, new_dentry
);
1133 /* ensure target dentry is invalidated, despite
1134 rehashing bug in vfs_rename_dir */
1135 ceph_invalidate_dentry_lease(new_dentry
);
1137 ceph_mdsc_put_request(req
);
1142 * Ensure a dentry lease will no longer revalidate.
1144 void ceph_invalidate_dentry_lease(struct dentry
*dentry
)
1146 spin_lock(&dentry
->d_lock
);
1147 ceph_dentry(dentry
)->time
= jiffies
;
1148 ceph_dentry(dentry
)->lease_shared_gen
= 0;
1149 spin_unlock(&dentry
->d_lock
);
1153 * Check if dentry lease is valid. If not, delete the lease. Try to
1154 * renew if the least is more than half up.
1156 static int dentry_lease_is_valid(struct dentry
*dentry
, unsigned int flags
,
1159 struct ceph_dentry_info
*di
;
1160 struct ceph_mds_session
*s
;
1164 struct ceph_mds_session
*session
= NULL
;
1167 spin_lock(&dentry
->d_lock
);
1168 di
= ceph_dentry(dentry
);
1169 if (di
&& di
->lease_session
) {
1170 s
= di
->lease_session
;
1171 spin_lock(&s
->s_gen_ttl_lock
);
1174 spin_unlock(&s
->s_gen_ttl_lock
);
1176 if (di
->lease_gen
== gen
&&
1177 time_before(jiffies
, di
->time
) &&
1178 time_before(jiffies
, ttl
)) {
1180 if (di
->lease_renew_after
&&
1181 time_after(jiffies
, di
->lease_renew_after
)) {
1183 * We should renew. If we're in RCU walk mode
1184 * though, we can't do that so just return
1187 if (flags
& LOOKUP_RCU
) {
1190 session
= ceph_get_mds_session(s
);
1191 seq
= di
->lease_seq
;
1192 di
->lease_renew_after
= 0;
1193 di
->lease_renew_from
= jiffies
;
1198 spin_unlock(&dentry
->d_lock
);
1201 ceph_mdsc_lease_send_msg(session
, dir
, dentry
,
1202 CEPH_MDS_LEASE_RENEW
, seq
);
1203 ceph_put_mds_session(session
);
1205 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry
, valid
);
1210 * Check if directory-wide content lease/cap is valid.
1212 static int dir_lease_is_valid(struct inode
*dir
, struct dentry
*dentry
)
1214 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1215 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
1218 spin_lock(&ci
->i_ceph_lock
);
1219 if (atomic_read(&ci
->i_shared_gen
) == di
->lease_shared_gen
)
1220 valid
= __ceph_caps_issued_mask(ci
, CEPH_CAP_FILE_SHARED
, 1);
1221 spin_unlock(&ci
->i_ceph_lock
);
1222 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1223 dir
, (unsigned)atomic_read(&ci
->i_shared_gen
),
1224 dentry
, (unsigned)di
->lease_shared_gen
, valid
);
1229 * Check if cached dentry can be trusted.
1231 static int ceph_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1234 struct dentry
*parent
;
1237 if (flags
& LOOKUP_RCU
) {
1238 parent
= READ_ONCE(dentry
->d_parent
);
1239 dir
= d_inode_rcu(parent
);
1243 parent
= dget_parent(dentry
);
1244 dir
= d_inode(parent
);
1247 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry
,
1248 dentry
, d_inode(dentry
), ceph_dentry(dentry
)->offset
);
1250 /* always trust cached snapped dentries, snapdir dentry */
1251 if (ceph_snap(dir
) != CEPH_NOSNAP
) {
1252 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry
,
1253 dentry
, d_inode(dentry
));
1255 } else if (d_really_is_positive(dentry
) &&
1256 ceph_snap(d_inode(dentry
)) == CEPH_SNAPDIR
) {
1259 valid
= dentry_lease_is_valid(dentry
, flags
, dir
);
1260 if (valid
== -ECHILD
)
1262 if (valid
|| dir_lease_is_valid(dir
, dentry
)) {
1263 if (d_really_is_positive(dentry
))
1264 valid
= ceph_is_any_caps(d_inode(dentry
));
1271 struct ceph_mds_client
*mdsc
=
1272 ceph_sb_to_client(dir
->i_sb
)->mdsc
;
1273 struct ceph_mds_request
*req
;
1277 if (flags
& LOOKUP_RCU
)
1280 op
= ceph_snap(dir
) == CEPH_SNAPDIR
?
1281 CEPH_MDS_OP_LOOKUPSNAP
: CEPH_MDS_OP_LOOKUP
;
1282 req
= ceph_mdsc_create_request(mdsc
, op
, USE_ANY_MDS
);
1284 req
->r_dentry
= dget(dentry
);
1285 req
->r_num_caps
= 2;
1286 req
->r_parent
= dir
;
1288 mask
= CEPH_STAT_CAP_INODE
| CEPH_CAP_AUTH_SHARED
;
1289 if (ceph_security_xattr_wanted(dir
))
1290 mask
|= CEPH_CAP_XATTR_SHARED
;
1291 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
1293 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1296 if (d_really_is_positive(dentry
) &&
1297 d_inode(dentry
) == req
->r_target_inode
)
1301 if (d_really_is_negative(dentry
))
1307 ceph_mdsc_put_request(req
);
1308 dout("d_revalidate %p lookup result=%d\n",
1313 dout("d_revalidate %p %s\n", dentry
, valid
? "valid" : "invalid");
1315 ceph_dentry_lru_touch(dentry
);
1317 ceph_dir_clear_complete(dir
);
1320 if (!(flags
& LOOKUP_RCU
))
1326 * Release our ceph_dentry_info.
1328 static void ceph_d_release(struct dentry
*dentry
)
1330 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
1332 dout("d_release %p\n", dentry
);
1333 ceph_dentry_lru_del(dentry
);
1335 spin_lock(&dentry
->d_lock
);
1336 dentry
->d_fsdata
= NULL
;
1337 spin_unlock(&dentry
->d_lock
);
1339 if (di
->lease_session
)
1340 ceph_put_mds_session(di
->lease_session
);
1341 kmem_cache_free(ceph_dentry_cachep
, di
);
1345 * When the VFS prunes a dentry from the cache, we need to clear the
1346 * complete flag on the parent directory.
1348 * Called under dentry->d_lock.
1350 static void ceph_d_prune(struct dentry
*dentry
)
1352 struct ceph_inode_info
*dir_ci
;
1353 struct ceph_dentry_info
*di
;
1355 dout("ceph_d_prune %pd %p\n", dentry
, dentry
);
1357 /* do we have a valid parent? */
1358 if (IS_ROOT(dentry
))
1361 /* we hold d_lock, so d_parent is stable */
1362 dir_ci
= ceph_inode(d_inode(dentry
->d_parent
));
1363 if (dir_ci
->i_vino
.snap
== CEPH_SNAPDIR
)
1366 /* who calls d_delete() should also disable dcache readdir */
1367 if (d_really_is_negative(dentry
))
1370 /* d_fsdata does not get cleared until d_release */
1371 if (!d_unhashed(dentry
)) {
1372 __ceph_dir_clear_complete(dir_ci
);
1376 /* Disable dcache readdir just in case that someone called d_drop()
1377 * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
1378 * properly (dcache readdir is still enabled) */
1379 di
= ceph_dentry(dentry
);
1380 if (di
->offset
> 0 &&
1381 di
->lease_shared_gen
== atomic_read(&dir_ci
->i_shared_gen
))
1382 __ceph_dir_clear_ordered(dir_ci
);
1386 * read() on a dir. This weird interface hack only works if mounted
1387 * with '-o dirstat'.
1389 static ssize_t
ceph_read_dir(struct file
*file
, char __user
*buf
, size_t size
,
1392 struct ceph_file_info
*cf
= file
->private_data
;
1393 struct inode
*inode
= file_inode(file
);
1394 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1396 const int bufsize
= 1024;
1398 if (!ceph_test_mount_opt(ceph_sb_to_client(inode
->i_sb
), DIRSTAT
))
1401 if (!cf
->dir_info
) {
1402 cf
->dir_info
= kmalloc(bufsize
, GFP_KERNEL
);
1406 snprintf(cf
->dir_info
, bufsize
,
1409 " subdirs: %20lld\n"
1410 "rentries: %20lld\n"
1412 " rsubdirs: %20lld\n"
1414 "rctime: %10ld.%09ld\n",
1415 ci
->i_files
+ ci
->i_subdirs
,
1418 ci
->i_rfiles
+ ci
->i_rsubdirs
,
1422 (long)ci
->i_rctime
.tv_sec
,
1423 (long)ci
->i_rctime
.tv_nsec
);
1426 if (*ppos
>= cf
->dir_info_len
)
1428 size
= min_t(unsigned, size
, cf
->dir_info_len
-*ppos
);
1429 left
= copy_to_user(buf
, cf
->dir_info
+ *ppos
, size
);
1432 *ppos
+= (size
- left
);
1437 * We maintain a private dentry LRU.
1439 * FIXME: this needs to be changed to a per-mds lru to be useful.
1441 void ceph_dentry_lru_add(struct dentry
*dn
)
1443 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1444 struct ceph_mds_client
*mdsc
;
1446 dout("dentry_lru_add %p %p '%pd'\n", di
, dn
, dn
);
1447 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1448 spin_lock(&mdsc
->dentry_lru_lock
);
1449 list_add_tail(&di
->lru
, &mdsc
->dentry_lru
);
1451 spin_unlock(&mdsc
->dentry_lru_lock
);
1454 void ceph_dentry_lru_touch(struct dentry
*dn
)
1456 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1457 struct ceph_mds_client
*mdsc
;
1459 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di
, dn
, dn
,
1461 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1462 spin_lock(&mdsc
->dentry_lru_lock
);
1463 list_move_tail(&di
->lru
, &mdsc
->dentry_lru
);
1464 spin_unlock(&mdsc
->dentry_lru_lock
);
1467 void ceph_dentry_lru_del(struct dentry
*dn
)
1469 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1470 struct ceph_mds_client
*mdsc
;
1472 dout("dentry_lru_del %p %p '%pd'\n", di
, dn
, dn
);
1473 mdsc
= ceph_sb_to_client(dn
->d_sb
)->mdsc
;
1474 spin_lock(&mdsc
->dentry_lru_lock
);
1475 list_del_init(&di
->lru
);
1477 spin_unlock(&mdsc
->dentry_lru_lock
);
1481 * Return name hash for a given dentry. This is dependent on
1482 * the parent directory's hash function.
1484 unsigned ceph_dentry_hash(struct inode
*dir
, struct dentry
*dn
)
1486 struct ceph_inode_info
*dci
= ceph_inode(dir
);
1488 switch (dci
->i_dir_layout
.dl_dir_hash
) {
1489 case 0: /* for backward compat */
1490 case CEPH_STR_HASH_LINUX
:
1491 return dn
->d_name
.hash
;
1494 return ceph_str_hash(dci
->i_dir_layout
.dl_dir_hash
,
1495 dn
->d_name
.name
, dn
->d_name
.len
);
1499 const struct file_operations ceph_dir_fops
= {
1500 .read
= ceph_read_dir
,
1501 .iterate
= ceph_readdir
,
1502 .llseek
= ceph_dir_llseek
,
1504 .release
= ceph_release
,
1505 .unlocked_ioctl
= ceph_ioctl
,
1506 .fsync
= ceph_fsync
,
1509 const struct file_operations ceph_snapdir_fops
= {
1510 .iterate
= ceph_readdir
,
1511 .llseek
= ceph_dir_llseek
,
1513 .release
= ceph_release
,
1516 const struct inode_operations ceph_dir_iops
= {
1517 .lookup
= ceph_lookup
,
1518 .permission
= ceph_permission
,
1519 .getattr
= ceph_getattr
,
1520 .setattr
= ceph_setattr
,
1521 .listxattr
= ceph_listxattr
,
1522 .get_acl
= ceph_get_acl
,
1523 .set_acl
= ceph_set_acl
,
1524 .mknod
= ceph_mknod
,
1525 .symlink
= ceph_symlink
,
1526 .mkdir
= ceph_mkdir
,
1528 .unlink
= ceph_unlink
,
1529 .rmdir
= ceph_unlink
,
1530 .rename
= ceph_rename
,
1531 .create
= ceph_create
,
1532 .atomic_open
= ceph_atomic_open
,
1535 const struct inode_operations ceph_snapdir_iops
= {
1536 .lookup
= ceph_lookup
,
1537 .permission
= ceph_permission
,
1538 .getattr
= ceph_getattr
,
1539 .mkdir
= ceph_mkdir
,
1540 .rmdir
= ceph_unlink
,
1541 .rename
= ceph_rename
,
1544 const struct dentry_operations ceph_dentry_ops
= {
1545 .d_revalidate
= ceph_d_revalidate
,
1546 .d_release
= ceph_d_release
,
1547 .d_prune
= ceph_d_prune
,
1548 .d_init
= ceph_d_init
,