Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/ceph/ceph_debug.h>
3
4#include <linux/spinlock.h>
5#include <linux/namei.h>
6#include <linux/slab.h>
7#include <linux/sched.h>
8#include <linux/xattr.h>
9
10#include "super.h"
11#include "mds_client.h"
12
13/*
14 * Directory operations: readdir, lookup, create, link, unlink,
15 * rename, etc.
16 */
17
18/*
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.
23 *
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
27 * point by name.
28 */
29
30const struct dentry_operations ceph_dentry_ops;
31
32static bool __dentry_lease_is_valid(struct ceph_dentry_info *di);
33static int __dir_lease_try_check(const struct dentry *dentry);
34
35/*
36 * Initialize ceph dentry state.
37 */
38static int ceph_d_init(struct dentry *dentry)
39{
40 struct ceph_dentry_info *di;
41 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
42 struct ceph_mds_client *mdsc = fsc->mdsc;
43
44 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
45 if (!di)
46 return -ENOMEM; /* oh well */
47
48 di->dentry = dentry;
49 di->lease_session = NULL;
50 di->time = jiffies;
51 dentry->d_fsdata = di;
52 INIT_LIST_HEAD(&di->lease_list);
53
54 atomic64_inc(&mdsc->metric.total_dentries);
55
56 return 0;
57}
58
59/*
60 * for f_pos for readdir:
61 * - hash order:
62 * (0xff << 52) | ((24 bits hash) << 28) |
63 * (the nth entry has hash collision);
64 * - frag+name order;
65 * ((frag value) << 28) | (the nth entry in frag);
66 */
67#define OFFSET_BITS 28
68#define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
69#define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
70loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
71{
72 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
73 if (hash_order)
74 fpos |= HASH_ORDER;
75 return fpos;
76}
77
78static bool is_hash_order(loff_t p)
79{
80 return (p & HASH_ORDER) == HASH_ORDER;
81}
82
83static unsigned fpos_frag(loff_t p)
84{
85 return p >> OFFSET_BITS;
86}
87
88static unsigned fpos_hash(loff_t p)
89{
90 return ceph_frag_value(fpos_frag(p));
91}
92
93static unsigned fpos_off(loff_t p)
94{
95 return p & OFFSET_MASK;
96}
97
98static int fpos_cmp(loff_t l, loff_t r)
99{
100 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
101 if (v)
102 return v;
103 return (int)(fpos_off(l) - fpos_off(r));
104}
105
106/*
107 * make note of the last dentry we read, so we can
108 * continue at the same lexicographical point,
109 * regardless of what dir changes take place on the
110 * server.
111 */
112static int note_last_dentry(struct ceph_dir_file_info *dfi, const char *name,
113 int len, unsigned next_offset)
114{
115 char *buf = kmalloc(len+1, GFP_KERNEL);
116 if (!buf)
117 return -ENOMEM;
118 kfree(dfi->last_name);
119 dfi->last_name = buf;
120 memcpy(dfi->last_name, name, len);
121 dfi->last_name[len] = 0;
122 dfi->next_offset = next_offset;
123 dout("note_last_dentry '%s'\n", dfi->last_name);
124 return 0;
125}
126
127
128static struct dentry *
129__dcache_find_get_entry(struct dentry *parent, u64 idx,
130 struct ceph_readdir_cache_control *cache_ctl)
131{
132 struct inode *dir = d_inode(parent);
133 struct dentry *dentry;
134 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
135 loff_t ptr_pos = idx * sizeof(struct dentry *);
136 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
137
138 if (ptr_pos >= i_size_read(dir))
139 return NULL;
140
141 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
142 ceph_readdir_cache_release(cache_ctl);
143 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
144 if (!cache_ctl->page) {
145 dout(" page %lu not found\n", ptr_pgoff);
146 return ERR_PTR(-EAGAIN);
147 }
148 /* reading/filling the cache are serialized by
149 i_mutex, no need to use page lock */
150 unlock_page(cache_ctl->page);
151 cache_ctl->dentries = kmap(cache_ctl->page);
152 }
153
154 cache_ctl->index = idx & idx_mask;
155
156 rcu_read_lock();
157 spin_lock(&parent->d_lock);
158 /* check i_size again here, because empty directory can be
159 * marked as complete while not holding the i_mutex. */
160 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
161 dentry = cache_ctl->dentries[cache_ctl->index];
162 else
163 dentry = NULL;
164 spin_unlock(&parent->d_lock);
165 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
166 dentry = NULL;
167 rcu_read_unlock();
168 return dentry ? : ERR_PTR(-EAGAIN);
169}
170
171/*
172 * When possible, we try to satisfy a readdir by peeking at the
173 * dcache. We make this work by carefully ordering dentries on
174 * d_child when we initially get results back from the MDS, and
175 * falling back to a "normal" sync readdir if any dentries in the dir
176 * are dropped.
177 *
178 * Complete dir indicates that we have all dentries in the dir. It is
179 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
180 * the MDS if/when the directory is modified).
181 */
182static int __dcache_readdir(struct file *file, struct dir_context *ctx,
183 int shared_gen)
184{
185 struct ceph_dir_file_info *dfi = file->private_data;
186 struct dentry *parent = file->f_path.dentry;
187 struct inode *dir = d_inode(parent);
188 struct dentry *dentry, *last = NULL;
189 struct ceph_dentry_info *di;
190 struct ceph_readdir_cache_control cache_ctl = {};
191 u64 idx = 0;
192 int err = 0;
193
194 dout("__dcache_readdir %p v%u at %llx\n", dir, (unsigned)shared_gen, ctx->pos);
195
196 /* search start position */
197 if (ctx->pos > 2) {
198 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
199 while (count > 0) {
200 u64 step = count >> 1;
201 dentry = __dcache_find_get_entry(parent, idx + step,
202 &cache_ctl);
203 if (!dentry) {
204 /* use linar search */
205 idx = 0;
206 break;
207 }
208 if (IS_ERR(dentry)) {
209 err = PTR_ERR(dentry);
210 goto out;
211 }
212 di = ceph_dentry(dentry);
213 spin_lock(&dentry->d_lock);
214 if (fpos_cmp(di->offset, ctx->pos) < 0) {
215 idx += step + 1;
216 count -= step + 1;
217 } else {
218 count = step;
219 }
220 spin_unlock(&dentry->d_lock);
221 dput(dentry);
222 }
223
224 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
225 }
226
227
228 for (;;) {
229 bool emit_dentry = false;
230 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
231 if (!dentry) {
232 dfi->file_info.flags |= CEPH_F_ATEND;
233 err = 0;
234 break;
235 }
236 if (IS_ERR(dentry)) {
237 err = PTR_ERR(dentry);
238 goto out;
239 }
240
241 spin_lock(&dentry->d_lock);
242 di = ceph_dentry(dentry);
243 if (d_unhashed(dentry) ||
244 d_really_is_negative(dentry) ||
245 di->lease_shared_gen != shared_gen) {
246 spin_unlock(&dentry->d_lock);
247 dput(dentry);
248 err = -EAGAIN;
249 goto out;
250 }
251 if (fpos_cmp(ctx->pos, di->offset) <= 0) {
252 __ceph_dentry_dir_lease_touch(di);
253 emit_dentry = true;
254 }
255 spin_unlock(&dentry->d_lock);
256
257 if (emit_dentry) {
258 dout(" %llx dentry %p %pd %p\n", di->offset,
259 dentry, dentry, d_inode(dentry));
260 ctx->pos = di->offset;
261 if (!dir_emit(ctx, dentry->d_name.name,
262 dentry->d_name.len, ceph_present_inode(d_inode(dentry)),
263 d_inode(dentry)->i_mode >> 12)) {
264 dput(dentry);
265 err = 0;
266 break;
267 }
268 ctx->pos++;
269
270 if (last)
271 dput(last);
272 last = dentry;
273 } else {
274 dput(dentry);
275 }
276 }
277out:
278 ceph_readdir_cache_release(&cache_ctl);
279 if (last) {
280 int ret;
281 di = ceph_dentry(last);
282 ret = note_last_dentry(dfi, last->d_name.name, last->d_name.len,
283 fpos_off(di->offset) + 1);
284 if (ret < 0)
285 err = ret;
286 dput(last);
287 /* last_name no longer match cache index */
288 if (dfi->readdir_cache_idx >= 0) {
289 dfi->readdir_cache_idx = -1;
290 dfi->dir_release_count = 0;
291 }
292 }
293 return err;
294}
295
296static bool need_send_readdir(struct ceph_dir_file_info *dfi, loff_t pos)
297{
298 if (!dfi->last_readdir)
299 return true;
300 if (is_hash_order(pos))
301 return !ceph_frag_contains_value(dfi->frag, fpos_hash(pos));
302 else
303 return dfi->frag != fpos_frag(pos);
304}
305
306static int ceph_readdir(struct file *file, struct dir_context *ctx)
307{
308 struct ceph_dir_file_info *dfi = file->private_data;
309 struct inode *inode = file_inode(file);
310 struct ceph_inode_info *ci = ceph_inode(inode);
311 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
312 struct ceph_mds_client *mdsc = fsc->mdsc;
313 int i;
314 int err;
315 unsigned frag = -1;
316 struct ceph_mds_reply_info_parsed *rinfo;
317
318 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
319 if (dfi->file_info.flags & CEPH_F_ATEND)
320 return 0;
321
322 /* always start with . and .. */
323 if (ctx->pos == 0) {
324 dout("readdir off 0 -> '.'\n");
325 if (!dir_emit(ctx, ".", 1, ceph_present_inode(inode),
326 inode->i_mode >> 12))
327 return 0;
328 ctx->pos = 1;
329 }
330 if (ctx->pos == 1) {
331 u64 ino;
332 struct dentry *dentry = file->f_path.dentry;
333
334 spin_lock(&dentry->d_lock);
335 ino = ceph_present_inode(dentry->d_parent->d_inode);
336 spin_unlock(&dentry->d_lock);
337
338 dout("readdir off 1 -> '..'\n");
339 if (!dir_emit(ctx, "..", 2, ino, inode->i_mode >> 12))
340 return 0;
341 ctx->pos = 2;
342 }
343
344 spin_lock(&ci->i_ceph_lock);
345 /* request Fx cap. if have Fx, we don't need to release Fs cap
346 * for later create/unlink. */
347 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_WR);
348 /* can we use the dcache? */
349 if (ceph_test_mount_opt(fsc, DCACHE) &&
350 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
351 ceph_snap(inode) != CEPH_SNAPDIR &&
352 __ceph_dir_is_complete_ordered(ci) &&
353 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
354 int shared_gen = atomic_read(&ci->i_shared_gen);
355
356 spin_unlock(&ci->i_ceph_lock);
357 err = __dcache_readdir(file, ctx, shared_gen);
358 if (err != -EAGAIN)
359 return err;
360 } else {
361 spin_unlock(&ci->i_ceph_lock);
362 }
363
364 /* proceed with a normal readdir */
365more:
366 /* do we have the correct frag content buffered? */
367 if (need_send_readdir(dfi, ctx->pos)) {
368 struct ceph_mds_request *req;
369 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
370 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
371
372 /* discard old result, if any */
373 if (dfi->last_readdir) {
374 ceph_mdsc_put_request(dfi->last_readdir);
375 dfi->last_readdir = NULL;
376 }
377
378 if (is_hash_order(ctx->pos)) {
379 /* fragtree isn't always accurate. choose frag
380 * based on previous reply when possible. */
381 if (frag == (unsigned)-1)
382 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
383 NULL, NULL);
384 } else {
385 frag = fpos_frag(ctx->pos);
386 }
387
388 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
389 ceph_vinop(inode), frag, dfi->last_name);
390 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
391 if (IS_ERR(req))
392 return PTR_ERR(req);
393 err = ceph_alloc_readdir_reply_buffer(req, inode);
394 if (err) {
395 ceph_mdsc_put_request(req);
396 return err;
397 }
398 /* hints to request -> mds selection code */
399 req->r_direct_mode = USE_AUTH_MDS;
400 if (op == CEPH_MDS_OP_READDIR) {
401 req->r_direct_hash = ceph_frag_value(frag);
402 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
403 req->r_inode_drop = CEPH_CAP_FILE_EXCL;
404 }
405 if (dfi->last_name) {
406 req->r_path2 = kstrdup(dfi->last_name, GFP_KERNEL);
407 if (!req->r_path2) {
408 ceph_mdsc_put_request(req);
409 return -ENOMEM;
410 }
411 } else if (is_hash_order(ctx->pos)) {
412 req->r_args.readdir.offset_hash =
413 cpu_to_le32(fpos_hash(ctx->pos));
414 }
415
416 req->r_dir_release_cnt = dfi->dir_release_count;
417 req->r_dir_ordered_cnt = dfi->dir_ordered_count;
418 req->r_readdir_cache_idx = dfi->readdir_cache_idx;
419 req->r_readdir_offset = dfi->next_offset;
420 req->r_args.readdir.frag = cpu_to_le32(frag);
421 req->r_args.readdir.flags =
422 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
423
424 req->r_inode = inode;
425 ihold(inode);
426 req->r_dentry = dget(file->f_path.dentry);
427 err = ceph_mdsc_do_request(mdsc, NULL, req);
428 if (err < 0) {
429 ceph_mdsc_put_request(req);
430 return err;
431 }
432 dout("readdir got and parsed readdir result=%d on "
433 "frag %x, end=%d, complete=%d, hash_order=%d\n",
434 err, frag,
435 (int)req->r_reply_info.dir_end,
436 (int)req->r_reply_info.dir_complete,
437 (int)req->r_reply_info.hash_order);
438
439 rinfo = &req->r_reply_info;
440 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
441 frag = le32_to_cpu(rinfo->dir_dir->frag);
442 if (!rinfo->hash_order) {
443 dfi->next_offset = req->r_readdir_offset;
444 /* adjust ctx->pos to beginning of frag */
445 ctx->pos = ceph_make_fpos(frag,
446 dfi->next_offset,
447 false);
448 }
449 }
450
451 dfi->frag = frag;
452 dfi->last_readdir = req;
453
454 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
455 dfi->readdir_cache_idx = req->r_readdir_cache_idx;
456 if (dfi->readdir_cache_idx < 0) {
457 /* preclude from marking dir ordered */
458 dfi->dir_ordered_count = 0;
459 } else if (ceph_frag_is_leftmost(frag) &&
460 dfi->next_offset == 2) {
461 /* note dir version at start of readdir so
462 * we can tell if any dentries get dropped */
463 dfi->dir_release_count = req->r_dir_release_cnt;
464 dfi->dir_ordered_count = req->r_dir_ordered_cnt;
465 }
466 } else {
467 dout("readdir !did_prepopulate\n");
468 /* disable readdir cache */
469 dfi->readdir_cache_idx = -1;
470 /* preclude from marking dir complete */
471 dfi->dir_release_count = 0;
472 }
473
474 /* note next offset and last dentry name */
475 if (rinfo->dir_nr > 0) {
476 struct ceph_mds_reply_dir_entry *rde =
477 rinfo->dir_entries + (rinfo->dir_nr-1);
478 unsigned next_offset = req->r_reply_info.dir_end ?
479 2 : (fpos_off(rde->offset) + 1);
480 err = note_last_dentry(dfi, rde->name, rde->name_len,
481 next_offset);
482 if (err)
483 return err;
484 } else if (req->r_reply_info.dir_end) {
485 dfi->next_offset = 2;
486 /* keep last name */
487 }
488 }
489
490 rinfo = &dfi->last_readdir->r_reply_info;
491 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
492 dfi->frag, rinfo->dir_nr, ctx->pos,
493 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
494
495 i = 0;
496 /* search start position */
497 if (rinfo->dir_nr > 0) {
498 int step, nr = rinfo->dir_nr;
499 while (nr > 0) {
500 step = nr >> 1;
501 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
502 i += step + 1;
503 nr -= step + 1;
504 } else {
505 nr = step;
506 }
507 }
508 }
509 for (; i < rinfo->dir_nr; i++) {
510 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
511
512 BUG_ON(rde->offset < ctx->pos);
513
514 ctx->pos = rde->offset;
515 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
516 i, rinfo->dir_nr, ctx->pos,
517 rde->name_len, rde->name, &rde->inode.in);
518
519 BUG_ON(!rde->inode.in);
520
521 if (!dir_emit(ctx, rde->name, rde->name_len,
522 ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
523 le32_to_cpu(rde->inode.in->mode) >> 12)) {
524 dout("filldir stopping us...\n");
525 return 0;
526 }
527 ctx->pos++;
528 }
529
530 ceph_mdsc_put_request(dfi->last_readdir);
531 dfi->last_readdir = NULL;
532
533 if (dfi->next_offset > 2) {
534 frag = dfi->frag;
535 goto more;
536 }
537
538 /* more frags? */
539 if (!ceph_frag_is_rightmost(dfi->frag)) {
540 frag = ceph_frag_next(dfi->frag);
541 if (is_hash_order(ctx->pos)) {
542 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
543 dfi->next_offset, true);
544 if (new_pos > ctx->pos)
545 ctx->pos = new_pos;
546 /* keep last_name */
547 } else {
548 ctx->pos = ceph_make_fpos(frag, dfi->next_offset,
549 false);
550 kfree(dfi->last_name);
551 dfi->last_name = NULL;
552 }
553 dout("readdir next frag is %x\n", frag);
554 goto more;
555 }
556 dfi->file_info.flags |= CEPH_F_ATEND;
557
558 /*
559 * if dir_release_count still matches the dir, no dentries
560 * were released during the whole readdir, and we should have
561 * the complete dir contents in our cache.
562 */
563 if (atomic64_read(&ci->i_release_count) ==
564 dfi->dir_release_count) {
565 spin_lock(&ci->i_ceph_lock);
566 if (dfi->dir_ordered_count ==
567 atomic64_read(&ci->i_ordered_count)) {
568 dout(" marking %p complete and ordered\n", inode);
569 /* use i_size to track number of entries in
570 * readdir cache */
571 BUG_ON(dfi->readdir_cache_idx < 0);
572 i_size_write(inode, dfi->readdir_cache_idx *
573 sizeof(struct dentry*));
574 } else {
575 dout(" marking %p complete\n", inode);
576 }
577 __ceph_dir_set_complete(ci, dfi->dir_release_count,
578 dfi->dir_ordered_count);
579 spin_unlock(&ci->i_ceph_lock);
580 }
581
582 dout("readdir %p file %p done.\n", inode, file);
583 return 0;
584}
585
586static void reset_readdir(struct ceph_dir_file_info *dfi)
587{
588 if (dfi->last_readdir) {
589 ceph_mdsc_put_request(dfi->last_readdir);
590 dfi->last_readdir = NULL;
591 }
592 kfree(dfi->last_name);
593 dfi->last_name = NULL;
594 dfi->dir_release_count = 0;
595 dfi->readdir_cache_idx = -1;
596 dfi->next_offset = 2; /* compensate for . and .. */
597 dfi->file_info.flags &= ~CEPH_F_ATEND;
598}
599
600/*
601 * discard buffered readdir content on seekdir(0), or seek to new frag,
602 * or seek prior to current chunk
603 */
604static bool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos)
605{
606 struct ceph_mds_reply_info_parsed *rinfo;
607 loff_t chunk_offset;
608 if (new_pos == 0)
609 return true;
610 if (is_hash_order(new_pos)) {
611 /* no need to reset last_name for a forward seek when
612 * dentries are sotred in hash order */
613 } else if (dfi->frag != fpos_frag(new_pos)) {
614 return true;
615 }
616 rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL;
617 if (!rinfo || !rinfo->dir_nr)
618 return true;
619 chunk_offset = rinfo->dir_entries[0].offset;
620 return new_pos < chunk_offset ||
621 is_hash_order(new_pos) != is_hash_order(chunk_offset);
622}
623
624static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
625{
626 struct ceph_dir_file_info *dfi = file->private_data;
627 struct inode *inode = file->f_mapping->host;
628 loff_t retval;
629
630 inode_lock(inode);
631 retval = -EINVAL;
632 switch (whence) {
633 case SEEK_CUR:
634 offset += file->f_pos;
635 case SEEK_SET:
636 break;
637 case SEEK_END:
638 retval = -EOPNOTSUPP;
639 default:
640 goto out;
641 }
642
643 if (offset >= 0) {
644 if (need_reset_readdir(dfi, offset)) {
645 dout("dir_llseek dropping %p content\n", file);
646 reset_readdir(dfi);
647 } else if (is_hash_order(offset) && offset > file->f_pos) {
648 /* for hash offset, we don't know if a forward seek
649 * is within same frag */
650 dfi->dir_release_count = 0;
651 dfi->readdir_cache_idx = -1;
652 }
653
654 if (offset != file->f_pos) {
655 file->f_pos = offset;
656 file->f_version = 0;
657 dfi->file_info.flags &= ~CEPH_F_ATEND;
658 }
659 retval = offset;
660 }
661out:
662 inode_unlock(inode);
663 return retval;
664}
665
666/*
667 * Handle lookups for the hidden .snap directory.
668 */
669int ceph_handle_snapdir(struct ceph_mds_request *req,
670 struct dentry *dentry, int err)
671{
672 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
673 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
674
675 /* .snap dir? */
676 if (err == -ENOENT &&
677 ceph_snap(parent) == CEPH_NOSNAP &&
678 strcmp(dentry->d_name.name,
679 fsc->mount_options->snapdir_name) == 0) {
680 struct inode *inode = ceph_get_snapdir(parent);
681 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
682 dentry, dentry, inode);
683 BUG_ON(!d_unhashed(dentry));
684 d_add(dentry, inode);
685 err = 0;
686 }
687 return err;
688}
689
690/*
691 * Figure out final result of a lookup/open request.
692 *
693 * Mainly, make sure we return the final req->r_dentry (if it already
694 * existed) in place of the original VFS-provided dentry when they
695 * differ.
696 *
697 * Gracefully handle the case where the MDS replies with -ENOENT and
698 * no trace (which it may do, at its discretion, e.g., if it doesn't
699 * care to issue a lease on the negative dentry).
700 */
701struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
702 struct dentry *dentry, int err)
703{
704 if (err == -ENOENT) {
705 /* no trace? */
706 err = 0;
707 if (!req->r_reply_info.head->is_dentry) {
708 dout("ENOENT and no trace, dentry %p inode %p\n",
709 dentry, d_inode(dentry));
710 if (d_really_is_positive(dentry)) {
711 d_drop(dentry);
712 err = -ENOENT;
713 } else {
714 d_add(dentry, NULL);
715 }
716 }
717 }
718 if (err)
719 dentry = ERR_PTR(err);
720 else if (dentry != req->r_dentry)
721 dentry = dget(req->r_dentry); /* we got spliced */
722 else
723 dentry = NULL;
724 return dentry;
725}
726
727static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
728{
729 return ceph_ino(inode) == CEPH_INO_ROOT &&
730 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
731}
732
733/*
734 * Look up a single dir entry. If there is a lookup intent, inform
735 * the MDS so that it gets our 'caps wanted' value in a single op.
736 */
737static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
738 unsigned int flags)
739{
740 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
741 struct ceph_mds_client *mdsc = fsc->mdsc;
742 struct ceph_mds_request *req;
743 int op;
744 int mask;
745 int err;
746
747 dout("lookup %p dentry %p '%pd'\n",
748 dir, dentry, dentry);
749
750 if (dentry->d_name.len > NAME_MAX)
751 return ERR_PTR(-ENAMETOOLONG);
752
753 /* can we conclude ENOENT locally? */
754 if (d_really_is_negative(dentry)) {
755 struct ceph_inode_info *ci = ceph_inode(dir);
756 struct ceph_dentry_info *di = ceph_dentry(dentry);
757
758 spin_lock(&ci->i_ceph_lock);
759 dout(" dir %p flags are 0x%lx\n", dir, ci->i_ceph_flags);
760 if (strncmp(dentry->d_name.name,
761 fsc->mount_options->snapdir_name,
762 dentry->d_name.len) &&
763 !is_root_ceph_dentry(dir, dentry) &&
764 ceph_test_mount_opt(fsc, DCACHE) &&
765 __ceph_dir_is_complete(ci) &&
766 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
767 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
768 spin_unlock(&ci->i_ceph_lock);
769 dout(" dir %p complete, -ENOENT\n", dir);
770 d_add(dentry, NULL);
771 di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
772 return NULL;
773 }
774 spin_unlock(&ci->i_ceph_lock);
775 }
776
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);
780 if (IS_ERR(req))
781 return ERR_CAST(req);
782 req->r_dentry = dget(dentry);
783 req->r_num_caps = 2;
784
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);
789
790 req->r_parent = dir;
791 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
792 err = ceph_mdsc_do_request(mdsc, NULL, req);
793 err = ceph_handle_snapdir(req, dentry, err);
794 dentry = ceph_finish_lookup(req, dentry, err);
795 ceph_mdsc_put_request(req); /* will dput(dentry) */
796 dout("lookup result=%p\n", dentry);
797 return dentry;
798}
799
800/*
801 * If we do a create but get no trace back from the MDS, follow up with
802 * a lookup (the VFS expects us to link up the provided dentry).
803 */
804int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
805{
806 struct dentry *result = ceph_lookup(dir, dentry, 0);
807
808 if (result && !IS_ERR(result)) {
809 /*
810 * We created the item, then did a lookup, and found
811 * it was already linked to another inode we already
812 * had in our cache (and thus got spliced). To not
813 * confuse VFS (especially when inode is a directory),
814 * we don't link our dentry to that inode, return an
815 * error instead.
816 *
817 * This event should be rare and it happens only when
818 * we talk to old MDS. Recent MDS does not send traceless
819 * reply for request that creates new inode.
820 */
821 d_drop(result);
822 return -ESTALE;
823 }
824 return PTR_ERR(result);
825}
826
827static int ceph_mknod(struct inode *dir, struct dentry *dentry,
828 umode_t mode, dev_t rdev)
829{
830 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
831 struct ceph_mds_client *mdsc = fsc->mdsc;
832 struct ceph_mds_request *req;
833 struct ceph_acl_sec_ctx as_ctx = {};
834 int err;
835
836 if (ceph_snap(dir) != CEPH_NOSNAP)
837 return -EROFS;
838
839 if (ceph_quota_is_max_files_exceeded(dir)) {
840 err = -EDQUOT;
841 goto out;
842 }
843
844 err = ceph_pre_init_acls(dir, &mode, &as_ctx);
845 if (err < 0)
846 goto out;
847 err = ceph_security_init_secctx(dentry, mode, &as_ctx);
848 if (err < 0)
849 goto out;
850
851 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
852 dir, dentry, mode, rdev);
853 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
854 if (IS_ERR(req)) {
855 err = PTR_ERR(req);
856 goto out;
857 }
858 req->r_dentry = dget(dentry);
859 req->r_num_caps = 2;
860 req->r_parent = dir;
861 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
862 req->r_args.mknod.mode = cpu_to_le32(mode);
863 req->r_args.mknod.rdev = cpu_to_le32(rdev);
864 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
865 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
866 if (as_ctx.pagelist) {
867 req->r_pagelist = as_ctx.pagelist;
868 as_ctx.pagelist = NULL;
869 }
870 err = ceph_mdsc_do_request(mdsc, dir, req);
871 if (!err && !req->r_reply_info.head->is_dentry)
872 err = ceph_handle_notrace_create(dir, dentry);
873 ceph_mdsc_put_request(req);
874out:
875 if (!err)
876 ceph_init_inode_acls(d_inode(dentry), &as_ctx);
877 else
878 d_drop(dentry);
879 ceph_release_acl_sec_ctx(&as_ctx);
880 return err;
881}
882
883static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
884 bool excl)
885{
886 return ceph_mknod(dir, dentry, mode, 0);
887}
888
889static int ceph_symlink(struct inode *dir, struct dentry *dentry,
890 const char *dest)
891{
892 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
893 struct ceph_mds_client *mdsc = fsc->mdsc;
894 struct ceph_mds_request *req;
895 struct ceph_acl_sec_ctx as_ctx = {};
896 int err;
897
898 if (ceph_snap(dir) != CEPH_NOSNAP)
899 return -EROFS;
900
901 if (ceph_quota_is_max_files_exceeded(dir)) {
902 err = -EDQUOT;
903 goto out;
904 }
905
906 err = ceph_security_init_secctx(dentry, S_IFLNK | 0777, &as_ctx);
907 if (err < 0)
908 goto out;
909
910 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
911 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
912 if (IS_ERR(req)) {
913 err = PTR_ERR(req);
914 goto out;
915 }
916 req->r_path2 = kstrdup(dest, GFP_KERNEL);
917 if (!req->r_path2) {
918 err = -ENOMEM;
919 ceph_mdsc_put_request(req);
920 goto out;
921 }
922 req->r_parent = dir;
923 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
924 req->r_dentry = dget(dentry);
925 req->r_num_caps = 2;
926 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
927 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
928 if (as_ctx.pagelist) {
929 req->r_pagelist = as_ctx.pagelist;
930 as_ctx.pagelist = NULL;
931 }
932 err = ceph_mdsc_do_request(mdsc, dir, req);
933 if (!err && !req->r_reply_info.head->is_dentry)
934 err = ceph_handle_notrace_create(dir, dentry);
935 ceph_mdsc_put_request(req);
936out:
937 if (err)
938 d_drop(dentry);
939 ceph_release_acl_sec_ctx(&as_ctx);
940 return err;
941}
942
943static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
944{
945 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
946 struct ceph_mds_client *mdsc = fsc->mdsc;
947 struct ceph_mds_request *req;
948 struct ceph_acl_sec_ctx as_ctx = {};
949 int err = -EROFS;
950 int op;
951
952 if (ceph_snap(dir) == CEPH_SNAPDIR) {
953 /* mkdir .snap/foo is a MKSNAP */
954 op = CEPH_MDS_OP_MKSNAP;
955 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
956 dentry, dentry);
957 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
958 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
959 op = CEPH_MDS_OP_MKDIR;
960 } else {
961 goto out;
962 }
963
964 if (op == CEPH_MDS_OP_MKDIR &&
965 ceph_quota_is_max_files_exceeded(dir)) {
966 err = -EDQUOT;
967 goto out;
968 }
969
970 mode |= S_IFDIR;
971 err = ceph_pre_init_acls(dir, &mode, &as_ctx);
972 if (err < 0)
973 goto out;
974 err = ceph_security_init_secctx(dentry, mode, &as_ctx);
975 if (err < 0)
976 goto out;
977
978 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
979 if (IS_ERR(req)) {
980 err = PTR_ERR(req);
981 goto out;
982 }
983
984 req->r_dentry = dget(dentry);
985 req->r_num_caps = 2;
986 req->r_parent = dir;
987 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
988 req->r_args.mkdir.mode = cpu_to_le32(mode);
989 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
990 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
991 if (as_ctx.pagelist) {
992 req->r_pagelist = as_ctx.pagelist;
993 as_ctx.pagelist = NULL;
994 }
995 err = ceph_mdsc_do_request(mdsc, dir, req);
996 if (!err &&
997 !req->r_reply_info.head->is_target &&
998 !req->r_reply_info.head->is_dentry)
999 err = ceph_handle_notrace_create(dir, dentry);
1000 ceph_mdsc_put_request(req);
1001out:
1002 if (!err)
1003 ceph_init_inode_acls(d_inode(dentry), &as_ctx);
1004 else
1005 d_drop(dentry);
1006 ceph_release_acl_sec_ctx(&as_ctx);
1007 return err;
1008}
1009
1010static int ceph_link(struct dentry *old_dentry, struct inode *dir,
1011 struct dentry *dentry)
1012{
1013 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1014 struct ceph_mds_client *mdsc = fsc->mdsc;
1015 struct ceph_mds_request *req;
1016 int err;
1017
1018 if (ceph_snap(dir) != CEPH_NOSNAP)
1019 return -EROFS;
1020
1021 dout("link in dir %p old_dentry %p dentry %p\n", dir,
1022 old_dentry, dentry);
1023 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
1024 if (IS_ERR(req)) {
1025 d_drop(dentry);
1026 return PTR_ERR(req);
1027 }
1028 req->r_dentry = dget(dentry);
1029 req->r_num_caps = 2;
1030 req->r_old_dentry = dget(old_dentry);
1031 req->r_parent = dir;
1032 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1033 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1034 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1035 /* release LINK_SHARED on source inode (mds will lock it) */
1036 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1037 err = ceph_mdsc_do_request(mdsc, dir, req);
1038 if (err) {
1039 d_drop(dentry);
1040 } else if (!req->r_reply_info.head->is_dentry) {
1041 ihold(d_inode(old_dentry));
1042 d_instantiate(dentry, d_inode(old_dentry));
1043 }
1044 ceph_mdsc_put_request(req);
1045 return err;
1046}
1047
1048static void ceph_async_unlink_cb(struct ceph_mds_client *mdsc,
1049 struct ceph_mds_request *req)
1050{
1051 int result = req->r_err ? req->r_err :
1052 le32_to_cpu(req->r_reply_info.head->result);
1053
1054 if (result == -EJUKEBOX)
1055 goto out;
1056
1057 /* If op failed, mark everyone involved for errors */
1058 if (result) {
1059 int pathlen = 0;
1060 u64 base = 0;
1061 char *path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
1062 &base, 0);
1063
1064 /* mark error on parent + clear complete */
1065 mapping_set_error(req->r_parent->i_mapping, result);
1066 ceph_dir_clear_complete(req->r_parent);
1067
1068 /* drop the dentry -- we don't know its status */
1069 if (!d_unhashed(req->r_dentry))
1070 d_drop(req->r_dentry);
1071
1072 /* mark inode itself for an error (since metadata is bogus) */
1073 mapping_set_error(req->r_old_inode->i_mapping, result);
1074
1075 pr_warn("ceph: async unlink failure path=(%llx)%s result=%d!\n",
1076 base, IS_ERR(path) ? "<<bad>>" : path, result);
1077 ceph_mdsc_free_path(path, pathlen);
1078 }
1079out:
1080 iput(req->r_old_inode);
1081 ceph_mdsc_release_dir_caps(req);
1082}
1083
1084static int get_caps_for_async_unlink(struct inode *dir, struct dentry *dentry)
1085{
1086 struct ceph_inode_info *ci = ceph_inode(dir);
1087 struct ceph_dentry_info *di;
1088 int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_UNLINK;
1089
1090 spin_lock(&ci->i_ceph_lock);
1091 if ((__ceph_caps_issued(ci, NULL) & want) == want) {
1092 ceph_take_cap_refs(ci, want, false);
1093 got = want;
1094 }
1095 spin_unlock(&ci->i_ceph_lock);
1096
1097 /* If we didn't get anything, return 0 */
1098 if (!got)
1099 return 0;
1100
1101 spin_lock(&dentry->d_lock);
1102 di = ceph_dentry(dentry);
1103 /*
1104 * - We are holding Fx, which implies Fs caps.
1105 * - Only support async unlink for primary linkage
1106 */
1107 if (atomic_read(&ci->i_shared_gen) != di->lease_shared_gen ||
1108 !(di->flags & CEPH_DENTRY_PRIMARY_LINK))
1109 want = 0;
1110 spin_unlock(&dentry->d_lock);
1111
1112 /* Do we still want what we've got? */
1113 if (want == got)
1114 return got;
1115
1116 ceph_put_cap_refs(ci, got);
1117 return 0;
1118}
1119
1120/*
1121 * rmdir and unlink are differ only by the metadata op code
1122 */
1123static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1124{
1125 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1126 struct ceph_mds_client *mdsc = fsc->mdsc;
1127 struct inode *inode = d_inode(dentry);
1128 struct ceph_mds_request *req;
1129 bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
1130 int err = -EROFS;
1131 int op;
1132
1133 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1134 /* rmdir .snap/foo is RMSNAP */
1135 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1136 op = CEPH_MDS_OP_RMSNAP;
1137 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1138 dout("unlink/rmdir dir %p dn %p inode %p\n",
1139 dir, dentry, inode);
1140 op = d_is_dir(dentry) ?
1141 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1142 } else
1143 goto out;
1144retry:
1145 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1146 if (IS_ERR(req)) {
1147 err = PTR_ERR(req);
1148 goto out;
1149 }
1150 req->r_dentry = dget(dentry);
1151 req->r_num_caps = 2;
1152 req->r_parent = dir;
1153 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1154 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1155 req->r_inode_drop = ceph_drop_caps_for_unlink(inode);
1156
1157 if (try_async && op == CEPH_MDS_OP_UNLINK &&
1158 (req->r_dir_caps = get_caps_for_async_unlink(dir, dentry))) {
1159 dout("async unlink on %llu/%.*s caps=%s", ceph_ino(dir),
1160 dentry->d_name.len, dentry->d_name.name,
1161 ceph_cap_string(req->r_dir_caps));
1162 set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
1163 req->r_callback = ceph_async_unlink_cb;
1164 req->r_old_inode = d_inode(dentry);
1165 ihold(req->r_old_inode);
1166 err = ceph_mdsc_submit_request(mdsc, dir, req);
1167 if (!err) {
1168 /*
1169 * We have enough caps, so we assume that the unlink
1170 * will succeed. Fix up the target inode and dcache.
1171 */
1172 drop_nlink(inode);
1173 d_delete(dentry);
1174 } else if (err == -EJUKEBOX) {
1175 try_async = false;
1176 ceph_mdsc_put_request(req);
1177 goto retry;
1178 }
1179 } else {
1180 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1181 err = ceph_mdsc_do_request(mdsc, dir, req);
1182 if (!err && !req->r_reply_info.head->is_dentry)
1183 d_delete(dentry);
1184 }
1185
1186 ceph_mdsc_put_request(req);
1187out:
1188 return err;
1189}
1190
1191static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1192 struct inode *new_dir, struct dentry *new_dentry,
1193 unsigned int flags)
1194{
1195 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1196 struct ceph_mds_client *mdsc = fsc->mdsc;
1197 struct ceph_mds_request *req;
1198 int op = CEPH_MDS_OP_RENAME;
1199 int err;
1200
1201 if (flags)
1202 return -EINVAL;
1203
1204 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1205 return -EXDEV;
1206 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1207 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1208 op = CEPH_MDS_OP_RENAMESNAP;
1209 else
1210 return -EROFS;
1211 } else if (old_dir != new_dir) {
1212 err = ceph_quota_check_rename(mdsc, d_inode(old_dentry),
1213 new_dir);
1214 if (err)
1215 return err;
1216 }
1217
1218 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1219 old_dir, old_dentry, new_dir, new_dentry);
1220 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1221 if (IS_ERR(req))
1222 return PTR_ERR(req);
1223 ihold(old_dir);
1224 req->r_dentry = dget(new_dentry);
1225 req->r_num_caps = 2;
1226 req->r_old_dentry = dget(old_dentry);
1227 req->r_old_dentry_dir = old_dir;
1228 req->r_parent = new_dir;
1229 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1230 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1231 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1232 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1233 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1234 /* release LINK_RDCACHE on source inode (mds will lock it) */
1235 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1236 if (d_really_is_positive(new_dentry)) {
1237 req->r_inode_drop =
1238 ceph_drop_caps_for_unlink(d_inode(new_dentry));
1239 }
1240 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1241 if (!err && !req->r_reply_info.head->is_dentry) {
1242 /*
1243 * Normally d_move() is done by fill_trace (called by
1244 * do_request, above). If there is no trace, we need
1245 * to do it here.
1246 */
1247 d_move(old_dentry, new_dentry);
1248 }
1249 ceph_mdsc_put_request(req);
1250 return err;
1251}
1252
1253/*
1254 * Move dentry to tail of mdsc->dentry_leases list when lease is updated.
1255 * Leases at front of the list will expire first. (Assume all leases have
1256 * similar duration)
1257 *
1258 * Called under dentry->d_lock.
1259 */
1260void __ceph_dentry_lease_touch(struct ceph_dentry_info *di)
1261{
1262 struct dentry *dn = di->dentry;
1263 struct ceph_mds_client *mdsc;
1264
1265 dout("dentry_lease_touch %p %p '%pd'\n", di, dn, dn);
1266
1267 di->flags |= CEPH_DENTRY_LEASE_LIST;
1268 if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1269 di->flags |= CEPH_DENTRY_REFERENCED;
1270 return;
1271 }
1272
1273 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1274 spin_lock(&mdsc->dentry_list_lock);
1275 list_move_tail(&di->lease_list, &mdsc->dentry_leases);
1276 spin_unlock(&mdsc->dentry_list_lock);
1277}
1278
1279static void __dentry_dir_lease_touch(struct ceph_mds_client* mdsc,
1280 struct ceph_dentry_info *di)
1281{
1282 di->flags &= ~(CEPH_DENTRY_LEASE_LIST | CEPH_DENTRY_REFERENCED);
1283 di->lease_gen = 0;
1284 di->time = jiffies;
1285 list_move_tail(&di->lease_list, &mdsc->dentry_dir_leases);
1286}
1287
1288/*
1289 * When dir lease is used, add dentry to tail of mdsc->dentry_dir_leases
1290 * list if it's not in the list, otherwise set 'referenced' flag.
1291 *
1292 * Called under dentry->d_lock.
1293 */
1294void __ceph_dentry_dir_lease_touch(struct ceph_dentry_info *di)
1295{
1296 struct dentry *dn = di->dentry;
1297 struct ceph_mds_client *mdsc;
1298
1299 dout("dentry_dir_lease_touch %p %p '%pd' (offset 0x%llx)\n",
1300 di, dn, dn, di->offset);
1301
1302 if (!list_empty(&di->lease_list)) {
1303 if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1304 /* don't remove dentry from dentry lease list
1305 * if its lease is valid */
1306 if (__dentry_lease_is_valid(di))
1307 return;
1308 } else {
1309 di->flags |= CEPH_DENTRY_REFERENCED;
1310 return;
1311 }
1312 }
1313
1314 if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1315 di->flags |= CEPH_DENTRY_REFERENCED;
1316 di->flags &= ~CEPH_DENTRY_LEASE_LIST;
1317 return;
1318 }
1319
1320 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1321 spin_lock(&mdsc->dentry_list_lock);
1322 __dentry_dir_lease_touch(mdsc, di),
1323 spin_unlock(&mdsc->dentry_list_lock);
1324}
1325
1326static void __dentry_lease_unlist(struct ceph_dentry_info *di)
1327{
1328 struct ceph_mds_client *mdsc;
1329 if (di->flags & CEPH_DENTRY_SHRINK_LIST)
1330 return;
1331 if (list_empty(&di->lease_list))
1332 return;
1333
1334 mdsc = ceph_sb_to_client(di->dentry->d_sb)->mdsc;
1335 spin_lock(&mdsc->dentry_list_lock);
1336 list_del_init(&di->lease_list);
1337 spin_unlock(&mdsc->dentry_list_lock);
1338}
1339
1340enum {
1341 KEEP = 0,
1342 DELETE = 1,
1343 TOUCH = 2,
1344 STOP = 4,
1345};
1346
1347struct ceph_lease_walk_control {
1348 bool dir_lease;
1349 bool expire_dir_lease;
1350 unsigned long nr_to_scan;
1351 unsigned long dir_lease_ttl;
1352};
1353
1354static unsigned long
1355__dentry_leases_walk(struct ceph_mds_client *mdsc,
1356 struct ceph_lease_walk_control *lwc,
1357 int (*check)(struct dentry*, void*))
1358{
1359 struct ceph_dentry_info *di, *tmp;
1360 struct dentry *dentry, *last = NULL;
1361 struct list_head* list;
1362 LIST_HEAD(dispose);
1363 unsigned long freed = 0;
1364 int ret = 0;
1365
1366 list = lwc->dir_lease ? &mdsc->dentry_dir_leases : &mdsc->dentry_leases;
1367 spin_lock(&mdsc->dentry_list_lock);
1368 list_for_each_entry_safe(di, tmp, list, lease_list) {
1369 if (!lwc->nr_to_scan)
1370 break;
1371 --lwc->nr_to_scan;
1372
1373 dentry = di->dentry;
1374 if (last == dentry)
1375 break;
1376
1377 if (!spin_trylock(&dentry->d_lock))
1378 continue;
1379
1380 if (__lockref_is_dead(&dentry->d_lockref)) {
1381 list_del_init(&di->lease_list);
1382 goto next;
1383 }
1384
1385 ret = check(dentry, lwc);
1386 if (ret & TOUCH) {
1387 /* move it into tail of dir lease list */
1388 __dentry_dir_lease_touch(mdsc, di);
1389 if (!last)
1390 last = dentry;
1391 }
1392 if (ret & DELETE) {
1393 /* stale lease */
1394 di->flags &= ~CEPH_DENTRY_REFERENCED;
1395 if (dentry->d_lockref.count > 0) {
1396 /* update_dentry_lease() will re-add
1397 * it to lease list, or
1398 * ceph_d_delete() will return 1 when
1399 * last reference is dropped */
1400 list_del_init(&di->lease_list);
1401 } else {
1402 di->flags |= CEPH_DENTRY_SHRINK_LIST;
1403 list_move_tail(&di->lease_list, &dispose);
1404 dget_dlock(dentry);
1405 }
1406 }
1407next:
1408 spin_unlock(&dentry->d_lock);
1409 if (ret & STOP)
1410 break;
1411 }
1412 spin_unlock(&mdsc->dentry_list_lock);
1413
1414 while (!list_empty(&dispose)) {
1415 di = list_first_entry(&dispose, struct ceph_dentry_info,
1416 lease_list);
1417 dentry = di->dentry;
1418 spin_lock(&dentry->d_lock);
1419
1420 list_del_init(&di->lease_list);
1421 di->flags &= ~CEPH_DENTRY_SHRINK_LIST;
1422 if (di->flags & CEPH_DENTRY_REFERENCED) {
1423 spin_lock(&mdsc->dentry_list_lock);
1424 if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1425 list_add_tail(&di->lease_list,
1426 &mdsc->dentry_leases);
1427 } else {
1428 __dentry_dir_lease_touch(mdsc, di);
1429 }
1430 spin_unlock(&mdsc->dentry_list_lock);
1431 } else {
1432 freed++;
1433 }
1434
1435 spin_unlock(&dentry->d_lock);
1436 /* ceph_d_delete() does the trick */
1437 dput(dentry);
1438 }
1439 return freed;
1440}
1441
1442static int __dentry_lease_check(struct dentry *dentry, void *arg)
1443{
1444 struct ceph_dentry_info *di = ceph_dentry(dentry);
1445 int ret;
1446
1447 if (__dentry_lease_is_valid(di))
1448 return STOP;
1449 ret = __dir_lease_try_check(dentry);
1450 if (ret == -EBUSY)
1451 return KEEP;
1452 if (ret > 0)
1453 return TOUCH;
1454 return DELETE;
1455}
1456
1457static int __dir_lease_check(struct dentry *dentry, void *arg)
1458{
1459 struct ceph_lease_walk_control *lwc = arg;
1460 struct ceph_dentry_info *di = ceph_dentry(dentry);
1461
1462 int ret = __dir_lease_try_check(dentry);
1463 if (ret == -EBUSY)
1464 return KEEP;
1465 if (ret > 0) {
1466 if (time_before(jiffies, di->time + lwc->dir_lease_ttl))
1467 return STOP;
1468 /* Move dentry to tail of dir lease list if we don't want
1469 * to delete it. So dentries in the list are checked in a
1470 * round robin manner */
1471 if (!lwc->expire_dir_lease)
1472 return TOUCH;
1473 if (dentry->d_lockref.count > 0 ||
1474 (di->flags & CEPH_DENTRY_REFERENCED))
1475 return TOUCH;
1476 /* invalidate dir lease */
1477 di->lease_shared_gen = 0;
1478 }
1479 return DELETE;
1480}
1481
1482int ceph_trim_dentries(struct ceph_mds_client *mdsc)
1483{
1484 struct ceph_lease_walk_control lwc;
1485 unsigned long count;
1486 unsigned long freed;
1487
1488 spin_lock(&mdsc->caps_list_lock);
1489 if (mdsc->caps_use_max > 0 &&
1490 mdsc->caps_use_count > mdsc->caps_use_max)
1491 count = mdsc->caps_use_count - mdsc->caps_use_max;
1492 else
1493 count = 0;
1494 spin_unlock(&mdsc->caps_list_lock);
1495
1496 lwc.dir_lease = false;
1497 lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE * 2;
1498 freed = __dentry_leases_walk(mdsc, &lwc, __dentry_lease_check);
1499 if (!lwc.nr_to_scan) /* more invalid leases */
1500 return -EAGAIN;
1501
1502 if (lwc.nr_to_scan < CEPH_CAPS_PER_RELEASE)
1503 lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE;
1504
1505 lwc.dir_lease = true;
1506 lwc.expire_dir_lease = freed < count;
1507 lwc.dir_lease_ttl = mdsc->fsc->mount_options->caps_wanted_delay_max * HZ;
1508 freed +=__dentry_leases_walk(mdsc, &lwc, __dir_lease_check);
1509 if (!lwc.nr_to_scan) /* more to check */
1510 return -EAGAIN;
1511
1512 return freed > 0 ? 1 : 0;
1513}
1514
1515/*
1516 * Ensure a dentry lease will no longer revalidate.
1517 */
1518void ceph_invalidate_dentry_lease(struct dentry *dentry)
1519{
1520 struct ceph_dentry_info *di = ceph_dentry(dentry);
1521 spin_lock(&dentry->d_lock);
1522 di->time = jiffies;
1523 di->lease_shared_gen = 0;
1524 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1525 __dentry_lease_unlist(di);
1526 spin_unlock(&dentry->d_lock);
1527}
1528
1529/*
1530 * Check if dentry lease is valid. If not, delete the lease. Try to
1531 * renew if the least is more than half up.
1532 */
1533static bool __dentry_lease_is_valid(struct ceph_dentry_info *di)
1534{
1535 struct ceph_mds_session *session;
1536
1537 if (!di->lease_gen)
1538 return false;
1539
1540 session = di->lease_session;
1541 if (session) {
1542 u32 gen;
1543 unsigned long ttl;
1544
1545 spin_lock(&session->s_gen_ttl_lock);
1546 gen = session->s_cap_gen;
1547 ttl = session->s_cap_ttl;
1548 spin_unlock(&session->s_gen_ttl_lock);
1549
1550 if (di->lease_gen == gen &&
1551 time_before(jiffies, ttl) &&
1552 time_before(jiffies, di->time))
1553 return true;
1554 }
1555 di->lease_gen = 0;
1556 return false;
1557}
1558
1559static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags)
1560{
1561 struct ceph_dentry_info *di;
1562 struct ceph_mds_session *session = NULL;
1563 u32 seq = 0;
1564 int valid = 0;
1565
1566 spin_lock(&dentry->d_lock);
1567 di = ceph_dentry(dentry);
1568 if (di && __dentry_lease_is_valid(di)) {
1569 valid = 1;
1570
1571 if (di->lease_renew_after &&
1572 time_after(jiffies, di->lease_renew_after)) {
1573 /*
1574 * We should renew. If we're in RCU walk mode
1575 * though, we can't do that so just return
1576 * -ECHILD.
1577 */
1578 if (flags & LOOKUP_RCU) {
1579 valid = -ECHILD;
1580 } else {
1581 session = ceph_get_mds_session(di->lease_session);
1582 seq = di->lease_seq;
1583 di->lease_renew_after = 0;
1584 di->lease_renew_from = jiffies;
1585 }
1586 }
1587 }
1588 spin_unlock(&dentry->d_lock);
1589
1590 if (session) {
1591 ceph_mdsc_lease_send_msg(session, dentry,
1592 CEPH_MDS_LEASE_RENEW, seq);
1593 ceph_put_mds_session(session);
1594 }
1595 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1596 return valid;
1597}
1598
1599/*
1600 * Called under dentry->d_lock.
1601 */
1602static int __dir_lease_try_check(const struct dentry *dentry)
1603{
1604 struct ceph_dentry_info *di = ceph_dentry(dentry);
1605 struct inode *dir;
1606 struct ceph_inode_info *ci;
1607 int valid = 0;
1608
1609 if (!di->lease_shared_gen)
1610 return 0;
1611 if (IS_ROOT(dentry))
1612 return 0;
1613
1614 dir = d_inode(dentry->d_parent);
1615 ci = ceph_inode(dir);
1616
1617 if (spin_trylock(&ci->i_ceph_lock)) {
1618 if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen &&
1619 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 0))
1620 valid = 1;
1621 spin_unlock(&ci->i_ceph_lock);
1622 } else {
1623 valid = -EBUSY;
1624 }
1625
1626 if (!valid)
1627 di->lease_shared_gen = 0;
1628 return valid;
1629}
1630
1631/*
1632 * Check if directory-wide content lease/cap is valid.
1633 */
1634static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry,
1635 struct ceph_mds_client *mdsc)
1636{
1637 struct ceph_inode_info *ci = ceph_inode(dir);
1638 int valid;
1639 int shared_gen;
1640
1641 spin_lock(&ci->i_ceph_lock);
1642 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1643 if (valid) {
1644 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
1645 shared_gen = atomic_read(&ci->i_shared_gen);
1646 }
1647 spin_unlock(&ci->i_ceph_lock);
1648 if (valid) {
1649 struct ceph_dentry_info *di;
1650 spin_lock(&dentry->d_lock);
1651 di = ceph_dentry(dentry);
1652 if (dir == d_inode(dentry->d_parent) &&
1653 di && di->lease_shared_gen == shared_gen)
1654 __ceph_dentry_dir_lease_touch(di);
1655 else
1656 valid = 0;
1657 spin_unlock(&dentry->d_lock);
1658 }
1659 dout("dir_lease_is_valid dir %p v%u dentry %p = %d\n",
1660 dir, (unsigned)atomic_read(&ci->i_shared_gen), dentry, valid);
1661 return valid;
1662}
1663
1664/*
1665 * Check if cached dentry can be trusted.
1666 */
1667static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1668{
1669 int valid = 0;
1670 struct dentry *parent;
1671 struct inode *dir, *inode;
1672 struct ceph_mds_client *mdsc;
1673
1674 if (flags & LOOKUP_RCU) {
1675 parent = READ_ONCE(dentry->d_parent);
1676 dir = d_inode_rcu(parent);
1677 if (!dir)
1678 return -ECHILD;
1679 inode = d_inode_rcu(dentry);
1680 } else {
1681 parent = dget_parent(dentry);
1682 dir = d_inode(parent);
1683 inode = d_inode(dentry);
1684 }
1685
1686 dout("d_revalidate %p '%pd' inode %p offset 0x%llx\n", dentry,
1687 dentry, inode, ceph_dentry(dentry)->offset);
1688
1689 mdsc = ceph_sb_to_client(dir->i_sb)->mdsc;
1690
1691 /* always trust cached snapped dentries, snapdir dentry */
1692 if (ceph_snap(dir) != CEPH_NOSNAP) {
1693 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1694 dentry, inode);
1695 valid = 1;
1696 } else if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1697 valid = 1;
1698 } else {
1699 valid = dentry_lease_is_valid(dentry, flags);
1700 if (valid == -ECHILD)
1701 return valid;
1702 if (valid || dir_lease_is_valid(dir, dentry, mdsc)) {
1703 if (inode)
1704 valid = ceph_is_any_caps(inode);
1705 else
1706 valid = 1;
1707 }
1708 }
1709
1710 if (!valid) {
1711 struct ceph_mds_request *req;
1712 int op, err;
1713 u32 mask;
1714
1715 if (flags & LOOKUP_RCU)
1716 return -ECHILD;
1717
1718 percpu_counter_inc(&mdsc->metric.d_lease_mis);
1719
1720 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1721 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1722 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1723 if (!IS_ERR(req)) {
1724 req->r_dentry = dget(dentry);
1725 req->r_num_caps = 2;
1726 req->r_parent = dir;
1727
1728 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1729 if (ceph_security_xattr_wanted(dir))
1730 mask |= CEPH_CAP_XATTR_SHARED;
1731 req->r_args.getattr.mask = cpu_to_le32(mask);
1732
1733 err = ceph_mdsc_do_request(mdsc, NULL, req);
1734 switch (err) {
1735 case 0:
1736 if (d_really_is_positive(dentry) &&
1737 d_inode(dentry) == req->r_target_inode)
1738 valid = 1;
1739 break;
1740 case -ENOENT:
1741 if (d_really_is_negative(dentry))
1742 valid = 1;
1743 fallthrough;
1744 default:
1745 break;
1746 }
1747 ceph_mdsc_put_request(req);
1748 dout("d_revalidate %p lookup result=%d\n",
1749 dentry, err);
1750 }
1751 } else {
1752 percpu_counter_inc(&mdsc->metric.d_lease_hit);
1753 }
1754
1755 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1756 if (!valid)
1757 ceph_dir_clear_complete(dir);
1758
1759 if (!(flags & LOOKUP_RCU))
1760 dput(parent);
1761 return valid;
1762}
1763
1764/*
1765 * Delete unused dentry that doesn't have valid lease
1766 *
1767 * Called under dentry->d_lock.
1768 */
1769static int ceph_d_delete(const struct dentry *dentry)
1770{
1771 struct ceph_dentry_info *di;
1772
1773 /* won't release caps */
1774 if (d_really_is_negative(dentry))
1775 return 0;
1776 if (ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
1777 return 0;
1778 /* vaild lease? */
1779 di = ceph_dentry(dentry);
1780 if (di) {
1781 if (__dentry_lease_is_valid(di))
1782 return 0;
1783 if (__dir_lease_try_check(dentry))
1784 return 0;
1785 }
1786 return 1;
1787}
1788
1789/*
1790 * Release our ceph_dentry_info.
1791 */
1792static void ceph_d_release(struct dentry *dentry)
1793{
1794 struct ceph_dentry_info *di = ceph_dentry(dentry);
1795 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1796
1797 dout("d_release %p\n", dentry);
1798
1799 atomic64_dec(&fsc->mdsc->metric.total_dentries);
1800
1801 spin_lock(&dentry->d_lock);
1802 __dentry_lease_unlist(di);
1803 dentry->d_fsdata = NULL;
1804 spin_unlock(&dentry->d_lock);
1805
1806 if (di->lease_session)
1807 ceph_put_mds_session(di->lease_session);
1808 kmem_cache_free(ceph_dentry_cachep, di);
1809}
1810
1811/*
1812 * When the VFS prunes a dentry from the cache, we need to clear the
1813 * complete flag on the parent directory.
1814 *
1815 * Called under dentry->d_lock.
1816 */
1817static void ceph_d_prune(struct dentry *dentry)
1818{
1819 struct ceph_inode_info *dir_ci;
1820 struct ceph_dentry_info *di;
1821
1822 dout("ceph_d_prune %pd %p\n", dentry, dentry);
1823
1824 /* do we have a valid parent? */
1825 if (IS_ROOT(dentry))
1826 return;
1827
1828 /* we hold d_lock, so d_parent is stable */
1829 dir_ci = ceph_inode(d_inode(dentry->d_parent));
1830 if (dir_ci->i_vino.snap == CEPH_SNAPDIR)
1831 return;
1832
1833 /* who calls d_delete() should also disable dcache readdir */
1834 if (d_really_is_negative(dentry))
1835 return;
1836
1837 /* d_fsdata does not get cleared until d_release */
1838 if (!d_unhashed(dentry)) {
1839 __ceph_dir_clear_complete(dir_ci);
1840 return;
1841 }
1842
1843 /* Disable dcache readdir just in case that someone called d_drop()
1844 * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
1845 * properly (dcache readdir is still enabled) */
1846 di = ceph_dentry(dentry);
1847 if (di->offset > 0 &&
1848 di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen))
1849 __ceph_dir_clear_ordered(dir_ci);
1850}
1851
1852/*
1853 * read() on a dir. This weird interface hack only works if mounted
1854 * with '-o dirstat'.
1855 */
1856static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1857 loff_t *ppos)
1858{
1859 struct ceph_dir_file_info *dfi = file->private_data;
1860 struct inode *inode = file_inode(file);
1861 struct ceph_inode_info *ci = ceph_inode(inode);
1862 int left;
1863 const int bufsize = 1024;
1864
1865 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1866 return -EISDIR;
1867
1868 if (!dfi->dir_info) {
1869 dfi->dir_info = kmalloc(bufsize, GFP_KERNEL);
1870 if (!dfi->dir_info)
1871 return -ENOMEM;
1872 dfi->dir_info_len =
1873 snprintf(dfi->dir_info, bufsize,
1874 "entries: %20lld\n"
1875 " files: %20lld\n"
1876 " subdirs: %20lld\n"
1877 "rentries: %20lld\n"
1878 " rfiles: %20lld\n"
1879 " rsubdirs: %20lld\n"
1880 "rbytes: %20lld\n"
1881 "rctime: %10lld.%09ld\n",
1882 ci->i_files + ci->i_subdirs,
1883 ci->i_files,
1884 ci->i_subdirs,
1885 ci->i_rfiles + ci->i_rsubdirs,
1886 ci->i_rfiles,
1887 ci->i_rsubdirs,
1888 ci->i_rbytes,
1889 ci->i_rctime.tv_sec,
1890 ci->i_rctime.tv_nsec);
1891 }
1892
1893 if (*ppos >= dfi->dir_info_len)
1894 return 0;
1895 size = min_t(unsigned, size, dfi->dir_info_len-*ppos);
1896 left = copy_to_user(buf, dfi->dir_info + *ppos, size);
1897 if (left == size)
1898 return -EFAULT;
1899 *ppos += (size - left);
1900 return size - left;
1901}
1902
1903
1904
1905/*
1906 * Return name hash for a given dentry. This is dependent on
1907 * the parent directory's hash function.
1908 */
1909unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1910{
1911 struct ceph_inode_info *dci = ceph_inode(dir);
1912 unsigned hash;
1913
1914 switch (dci->i_dir_layout.dl_dir_hash) {
1915 case 0: /* for backward compat */
1916 case CEPH_STR_HASH_LINUX:
1917 return dn->d_name.hash;
1918
1919 default:
1920 spin_lock(&dn->d_lock);
1921 hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1922 dn->d_name.name, dn->d_name.len);
1923 spin_unlock(&dn->d_lock);
1924 return hash;
1925 }
1926}
1927
1928const struct file_operations ceph_dir_fops = {
1929 .read = ceph_read_dir,
1930 .iterate = ceph_readdir,
1931 .llseek = ceph_dir_llseek,
1932 .open = ceph_open,
1933 .release = ceph_release,
1934 .unlocked_ioctl = ceph_ioctl,
1935 .compat_ioctl = compat_ptr_ioctl,
1936 .fsync = ceph_fsync,
1937 .lock = ceph_lock,
1938 .flock = ceph_flock,
1939};
1940
1941const struct file_operations ceph_snapdir_fops = {
1942 .iterate = ceph_readdir,
1943 .llseek = ceph_dir_llseek,
1944 .open = ceph_open,
1945 .release = ceph_release,
1946};
1947
1948const struct inode_operations ceph_dir_iops = {
1949 .lookup = ceph_lookup,
1950 .permission = ceph_permission,
1951 .getattr = ceph_getattr,
1952 .setattr = ceph_setattr,
1953 .listxattr = ceph_listxattr,
1954 .get_acl = ceph_get_acl,
1955 .set_acl = ceph_set_acl,
1956 .mknod = ceph_mknod,
1957 .symlink = ceph_symlink,
1958 .mkdir = ceph_mkdir,
1959 .link = ceph_link,
1960 .unlink = ceph_unlink,
1961 .rmdir = ceph_unlink,
1962 .rename = ceph_rename,
1963 .create = ceph_create,
1964 .atomic_open = ceph_atomic_open,
1965};
1966
1967const struct inode_operations ceph_snapdir_iops = {
1968 .lookup = ceph_lookup,
1969 .permission = ceph_permission,
1970 .getattr = ceph_getattr,
1971 .mkdir = ceph_mkdir,
1972 .rmdir = ceph_unlink,
1973 .rename = ceph_rename,
1974};
1975
1976const struct dentry_operations ceph_dentry_ops = {
1977 .d_revalidate = ceph_d_revalidate,
1978 .d_delete = ceph_d_delete,
1979 .d_release = ceph_d_release,
1980 .d_prune = ceph_d_prune,
1981 .d_init = ceph_d_init,
1982};
1#include <linux/ceph/ceph_debug.h>
2
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
9#include "super.h"
10#include "mds_client.h"
11
12/*
13 * Directory operations: readdir, lookup, create, link, unlink,
14 * rename, etc.
15 */
16
17/*
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.
22 *
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
26 * point by name.
27 */
28
29const struct inode_operations ceph_dir_iops;
30const struct file_operations ceph_dir_fops;
31const struct dentry_operations ceph_dentry_ops;
32
33/*
34 * Initialize ceph dentry state.
35 */
36int ceph_init_dentry(struct dentry *dentry)
37{
38 struct ceph_dentry_info *di;
39
40 if (dentry->d_fsdata)
41 return 0;
42
43 di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS | __GFP_ZERO);
44 if (!di)
45 return -ENOMEM; /* oh well */
46
47 spin_lock(&dentry->d_lock);
48 if (dentry->d_fsdata) {
49 /* lost a race */
50 kmem_cache_free(ceph_dentry_cachep, di);
51 goto out_unlock;
52 }
53
54 if (dentry->d_parent == NULL || /* nfs fh_to_dentry */
55 ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP)
56 d_set_d_op(dentry, &ceph_dentry_ops);
57 else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR)
58 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
59 else
60 d_set_d_op(dentry, &ceph_snap_dentry_ops);
61
62 di->dentry = dentry;
63 di->lease_session = NULL;
64 dentry->d_time = jiffies;
65 /* avoid reordering d_fsdata setup so that the check above is safe */
66 smp_mb();
67 dentry->d_fsdata = di;
68 ceph_dentry_lru_add(dentry);
69out_unlock:
70 spin_unlock(&dentry->d_lock);
71 return 0;
72}
73
74struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry)
75{
76 struct inode *inode = NULL;
77
78 if (!dentry)
79 return NULL;
80
81 spin_lock(&dentry->d_lock);
82 if (dentry->d_parent) {
83 inode = dentry->d_parent->d_inode;
84 ihold(inode);
85 }
86 spin_unlock(&dentry->d_lock);
87 return inode;
88}
89
90
91/*
92 * for readdir, we encode the directory frag and offset within that
93 * frag into f_pos.
94 */
95static unsigned fpos_frag(loff_t p)
96{
97 return p >> 32;
98}
99static unsigned fpos_off(loff_t p)
100{
101 return p & 0xffffffff;
102}
103
104/*
105 * When possible, we try to satisfy a readdir by peeking at the
106 * dcache. We make this work by carefully ordering dentries on
107 * d_u.d_child when we initially get results back from the MDS, and
108 * falling back to a "normal" sync readdir if any dentries in the dir
109 * are dropped.
110 *
111 * I_COMPLETE tells indicates we have all dentries in the dir. It is
112 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
113 * the MDS if/when the directory is modified).
114 */
115static int __dcache_readdir(struct file *filp,
116 void *dirent, filldir_t filldir)
117{
118 struct ceph_file_info *fi = filp->private_data;
119 struct dentry *parent = filp->f_dentry;
120 struct inode *dir = parent->d_inode;
121 struct list_head *p;
122 struct dentry *dentry, *last;
123 struct ceph_dentry_info *di;
124 int err = 0;
125
126 /* claim ref on last dentry we returned */
127 last = fi->dentry;
128 fi->dentry = NULL;
129
130 dout("__dcache_readdir %p at %llu (last %p)\n", dir, filp->f_pos,
131 last);
132
133 spin_lock(&parent->d_lock);
134
135 /* start at beginning? */
136 if (filp->f_pos == 2 || last == NULL ||
137 filp->f_pos < ceph_dentry(last)->offset) {
138 if (list_empty(&parent->d_subdirs))
139 goto out_unlock;
140 p = parent->d_subdirs.prev;
141 dout(" initial p %p/%p\n", p->prev, p->next);
142 } else {
143 p = last->d_u.d_child.prev;
144 }
145
146more:
147 dentry = list_entry(p, struct dentry, d_u.d_child);
148 di = ceph_dentry(dentry);
149 while (1) {
150 dout(" p %p/%p %s d_subdirs %p/%p\n", p->prev, p->next,
151 d_unhashed(dentry) ? "!hashed" : "hashed",
152 parent->d_subdirs.prev, parent->d_subdirs.next);
153 if (p == &parent->d_subdirs) {
154 fi->flags |= CEPH_F_ATEND;
155 goto out_unlock;
156 }
157 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
158 if (!d_unhashed(dentry) && dentry->d_inode &&
159 ceph_snap(dentry->d_inode) != CEPH_SNAPDIR &&
160 ceph_ino(dentry->d_inode) != CEPH_INO_CEPH &&
161 filp->f_pos <= di->offset)
162 break;
163 dout(" skipping %p %.*s at %llu (%llu)%s%s\n", dentry,
164 dentry->d_name.len, dentry->d_name.name, di->offset,
165 filp->f_pos, d_unhashed(dentry) ? " unhashed" : "",
166 !dentry->d_inode ? " null" : "");
167 spin_unlock(&dentry->d_lock);
168 p = p->prev;
169 dentry = list_entry(p, struct dentry, d_u.d_child);
170 di = ceph_dentry(dentry);
171 }
172
173 dget_dlock(dentry);
174 spin_unlock(&dentry->d_lock);
175 spin_unlock(&parent->d_lock);
176
177 dout(" %llu (%llu) dentry %p %.*s %p\n", di->offset, filp->f_pos,
178 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
179 filp->f_pos = di->offset;
180 err = filldir(dirent, dentry->d_name.name,
181 dentry->d_name.len, di->offset,
182 ceph_translate_ino(dentry->d_sb, dentry->d_inode->i_ino),
183 dentry->d_inode->i_mode >> 12);
184
185 if (last) {
186 if (err < 0) {
187 /* remember our position */
188 fi->dentry = last;
189 fi->next_offset = di->offset;
190 } else {
191 dput(last);
192 }
193 }
194 last = dentry;
195
196 if (err < 0)
197 goto out;
198
199 filp->f_pos++;
200
201 /* make sure a dentry wasn't dropped while we didn't have parent lock */
202 if (!ceph_i_test(dir, CEPH_I_COMPLETE)) {
203 dout(" lost I_COMPLETE on %p; falling back to mds\n", dir);
204 err = -EAGAIN;
205 goto out;
206 }
207
208 spin_lock(&parent->d_lock);
209 p = p->prev; /* advance to next dentry */
210 goto more;
211
212out_unlock:
213 spin_unlock(&parent->d_lock);
214out:
215 if (last)
216 dput(last);
217 return err;
218}
219
220/*
221 * make note of the last dentry we read, so we can
222 * continue at the same lexicographical point,
223 * regardless of what dir changes take place on the
224 * server.
225 */
226static int note_last_dentry(struct ceph_file_info *fi, const char *name,
227 int len)
228{
229 kfree(fi->last_name);
230 fi->last_name = kmalloc(len+1, GFP_NOFS);
231 if (!fi->last_name)
232 return -ENOMEM;
233 memcpy(fi->last_name, name, len);
234 fi->last_name[len] = 0;
235 dout("note_last_dentry '%s'\n", fi->last_name);
236 return 0;
237}
238
239static int ceph_readdir(struct file *filp, void *dirent, filldir_t filldir)
240{
241 struct ceph_file_info *fi = filp->private_data;
242 struct inode *inode = filp->f_dentry->d_inode;
243 struct ceph_inode_info *ci = ceph_inode(inode);
244 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
245 struct ceph_mds_client *mdsc = fsc->mdsc;
246 unsigned frag = fpos_frag(filp->f_pos);
247 int off = fpos_off(filp->f_pos);
248 int err;
249 u32 ftype;
250 struct ceph_mds_reply_info_parsed *rinfo;
251 const int max_entries = fsc->mount_options->max_readdir;
252 const int max_bytes = fsc->mount_options->max_readdir_bytes;
253
254 dout("readdir %p filp %p frag %u off %u\n", inode, filp, frag, off);
255 if (fi->flags & CEPH_F_ATEND)
256 return 0;
257
258 /* always start with . and .. */
259 if (filp->f_pos == 0) {
260 /* note dir version at start of readdir so we can tell
261 * if any dentries get dropped */
262 fi->dir_release_count = ci->i_release_count;
263
264 dout("readdir off 0 -> '.'\n");
265 if (filldir(dirent, ".", 1, ceph_make_fpos(0, 0),
266 ceph_translate_ino(inode->i_sb, inode->i_ino),
267 inode->i_mode >> 12) < 0)
268 return 0;
269 filp->f_pos = 1;
270 off = 1;
271 }
272 if (filp->f_pos == 1) {
273 ino_t ino = parent_ino(filp->f_dentry);
274 dout("readdir off 1 -> '..'\n");
275 if (filldir(dirent, "..", 2, ceph_make_fpos(0, 1),
276 ceph_translate_ino(inode->i_sb, ino),
277 inode->i_mode >> 12) < 0)
278 return 0;
279 filp->f_pos = 2;
280 off = 2;
281 }
282
283 /* can we use the dcache? */
284 spin_lock(&inode->i_lock);
285 if ((filp->f_pos == 2 || fi->dentry) &&
286 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
287 ceph_snap(inode) != CEPH_SNAPDIR &&
288 (ci->i_ceph_flags & CEPH_I_COMPLETE) &&
289 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
290 spin_unlock(&inode->i_lock);
291 err = __dcache_readdir(filp, dirent, filldir);
292 if (err != -EAGAIN)
293 return err;
294 } else {
295 spin_unlock(&inode->i_lock);
296 }
297 if (fi->dentry) {
298 err = note_last_dentry(fi, fi->dentry->d_name.name,
299 fi->dentry->d_name.len);
300 if (err)
301 return err;
302 dput(fi->dentry);
303 fi->dentry = NULL;
304 }
305
306 /* proceed with a normal readdir */
307
308more:
309 /* do we have the correct frag content buffered? */
310 if (fi->frag != frag || fi->last_readdir == NULL) {
311 struct ceph_mds_request *req;
312 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
313 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
314
315 /* discard old result, if any */
316 if (fi->last_readdir) {
317 ceph_mdsc_put_request(fi->last_readdir);
318 fi->last_readdir = NULL;
319 }
320
321 /* requery frag tree, as the frag topology may have changed */
322 frag = ceph_choose_frag(ceph_inode(inode), frag, NULL, NULL);
323
324 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
325 ceph_vinop(inode), frag, fi->last_name);
326 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
327 if (IS_ERR(req))
328 return PTR_ERR(req);
329 req->r_inode = inode;
330 ihold(inode);
331 req->r_dentry = dget(filp->f_dentry);
332 /* hints to request -> mds selection code */
333 req->r_direct_mode = USE_AUTH_MDS;
334 req->r_direct_hash = ceph_frag_value(frag);
335 req->r_direct_is_hash = true;
336 req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
337 req->r_readdir_offset = fi->next_offset;
338 req->r_args.readdir.frag = cpu_to_le32(frag);
339 req->r_args.readdir.max_entries = cpu_to_le32(max_entries);
340 req->r_args.readdir.max_bytes = cpu_to_le32(max_bytes);
341 req->r_num_caps = max_entries + 1;
342 err = ceph_mdsc_do_request(mdsc, NULL, req);
343 if (err < 0) {
344 ceph_mdsc_put_request(req);
345 return err;
346 }
347 dout("readdir got and parsed readdir result=%d"
348 " on frag %x, end=%d, complete=%d\n", err, frag,
349 (int)req->r_reply_info.dir_end,
350 (int)req->r_reply_info.dir_complete);
351
352 if (!req->r_did_prepopulate) {
353 dout("readdir !did_prepopulate");
354 fi->dir_release_count--; /* preclude I_COMPLETE */
355 }
356
357 /* note next offset and last dentry name */
358 fi->offset = fi->next_offset;
359 fi->last_readdir = req;
360
361 if (req->r_reply_info.dir_end) {
362 kfree(fi->last_name);
363 fi->last_name = NULL;
364 if (ceph_frag_is_rightmost(frag))
365 fi->next_offset = 2;
366 else
367 fi->next_offset = 0;
368 } else {
369 rinfo = &req->r_reply_info;
370 err = note_last_dentry(fi,
371 rinfo->dir_dname[rinfo->dir_nr-1],
372 rinfo->dir_dname_len[rinfo->dir_nr-1]);
373 if (err)
374 return err;
375 fi->next_offset += rinfo->dir_nr;
376 }
377 }
378
379 rinfo = &fi->last_readdir->r_reply_info;
380 dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
381 rinfo->dir_nr, off, fi->offset);
382 while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
383 u64 pos = ceph_make_fpos(frag, off);
384 struct ceph_mds_reply_inode *in =
385 rinfo->dir_in[off - fi->offset].in;
386 struct ceph_vino vino;
387 ino_t ino;
388
389 dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
390 off, off - fi->offset, rinfo->dir_nr, pos,
391 rinfo->dir_dname_len[off - fi->offset],
392 rinfo->dir_dname[off - fi->offset], in);
393 BUG_ON(!in);
394 ftype = le32_to_cpu(in->mode) >> 12;
395 vino.ino = le64_to_cpu(in->ino);
396 vino.snap = le64_to_cpu(in->snapid);
397 ino = ceph_vino_to_ino(vino);
398 if (filldir(dirent,
399 rinfo->dir_dname[off - fi->offset],
400 rinfo->dir_dname_len[off - fi->offset],
401 pos,
402 ceph_translate_ino(inode->i_sb, ino), ftype) < 0) {
403 dout("filldir stopping us...\n");
404 return 0;
405 }
406 off++;
407 filp->f_pos = pos + 1;
408 }
409
410 if (fi->last_name) {
411 ceph_mdsc_put_request(fi->last_readdir);
412 fi->last_readdir = NULL;
413 goto more;
414 }
415
416 /* more frags? */
417 if (!ceph_frag_is_rightmost(frag)) {
418 frag = ceph_frag_next(frag);
419 off = 0;
420 filp->f_pos = ceph_make_fpos(frag, off);
421 dout("readdir next frag is %x\n", frag);
422 goto more;
423 }
424 fi->flags |= CEPH_F_ATEND;
425
426 /*
427 * if dir_release_count still matches the dir, no dentries
428 * were released during the whole readdir, and we should have
429 * the complete dir contents in our cache.
430 */
431 spin_lock(&inode->i_lock);
432 if (ci->i_release_count == fi->dir_release_count) {
433 dout(" marking %p complete\n", inode);
434 /* ci->i_ceph_flags |= CEPH_I_COMPLETE; */
435 ci->i_max_offset = filp->f_pos;
436 }
437 spin_unlock(&inode->i_lock);
438
439 dout("readdir %p filp %p done.\n", inode, filp);
440 return 0;
441}
442
443static void reset_readdir(struct ceph_file_info *fi)
444{
445 if (fi->last_readdir) {
446 ceph_mdsc_put_request(fi->last_readdir);
447 fi->last_readdir = NULL;
448 }
449 kfree(fi->last_name);
450 fi->last_name = NULL;
451 fi->next_offset = 2; /* compensate for . and .. */
452 if (fi->dentry) {
453 dput(fi->dentry);
454 fi->dentry = NULL;
455 }
456 fi->flags &= ~CEPH_F_ATEND;
457}
458
459static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int origin)
460{
461 struct ceph_file_info *fi = file->private_data;
462 struct inode *inode = file->f_mapping->host;
463 loff_t old_offset = offset;
464 loff_t retval;
465
466 mutex_lock(&inode->i_mutex);
467 retval = -EINVAL;
468 switch (origin) {
469 case SEEK_END:
470 offset += inode->i_size + 2; /* FIXME */
471 break;
472 case SEEK_CUR:
473 offset += file->f_pos;
474 case SEEK_SET:
475 break;
476 default:
477 goto out;
478 }
479
480 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
481 if (offset != file->f_pos) {
482 file->f_pos = offset;
483 file->f_version = 0;
484 fi->flags &= ~CEPH_F_ATEND;
485 }
486 retval = offset;
487
488 /*
489 * discard buffered readdir content on seekdir(0), or
490 * seek to new frag, or seek prior to current chunk.
491 */
492 if (offset == 0 ||
493 fpos_frag(offset) != fpos_frag(old_offset) ||
494 fpos_off(offset) < fi->offset) {
495 dout("dir_llseek dropping %p content\n", file);
496 reset_readdir(fi);
497 }
498
499 /* bump dir_release_count if we did a forward seek */
500 if (offset > old_offset)
501 fi->dir_release_count--;
502 }
503out:
504 mutex_unlock(&inode->i_mutex);
505 return retval;
506}
507
508/*
509 * Handle lookups for the hidden .snap directory.
510 */
511int ceph_handle_snapdir(struct ceph_mds_request *req,
512 struct dentry *dentry, int err)
513{
514 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
515 struct inode *parent = dentry->d_parent->d_inode; /* we hold i_mutex */
516
517 /* .snap dir? */
518 if (err == -ENOENT &&
519 ceph_snap(parent) == CEPH_NOSNAP &&
520 strcmp(dentry->d_name.name,
521 fsc->mount_options->snapdir_name) == 0) {
522 struct inode *inode = ceph_get_snapdir(parent);
523 dout("ENOENT on snapdir %p '%.*s', linking to snapdir %p\n",
524 dentry, dentry->d_name.len, dentry->d_name.name, inode);
525 BUG_ON(!d_unhashed(dentry));
526 d_add(dentry, inode);
527 err = 0;
528 }
529 return err;
530}
531
532/*
533 * Figure out final result of a lookup/open request.
534 *
535 * Mainly, make sure we return the final req->r_dentry (if it already
536 * existed) in place of the original VFS-provided dentry when they
537 * differ.
538 *
539 * Gracefully handle the case where the MDS replies with -ENOENT and
540 * no trace (which it may do, at its discretion, e.g., if it doesn't
541 * care to issue a lease on the negative dentry).
542 */
543struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
544 struct dentry *dentry, int err)
545{
546 if (err == -ENOENT) {
547 /* no trace? */
548 err = 0;
549 if (!req->r_reply_info.head->is_dentry) {
550 dout("ENOENT and no trace, dentry %p inode %p\n",
551 dentry, dentry->d_inode);
552 if (dentry->d_inode) {
553 d_drop(dentry);
554 err = -ENOENT;
555 } else {
556 d_add(dentry, NULL);
557 }
558 }
559 }
560 if (err)
561 dentry = ERR_PTR(err);
562 else if (dentry != req->r_dentry)
563 dentry = dget(req->r_dentry); /* we got spliced */
564 else
565 dentry = NULL;
566 return dentry;
567}
568
569static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
570{
571 return ceph_ino(inode) == CEPH_INO_ROOT &&
572 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
573}
574
575/*
576 * Look up a single dir entry. If there is a lookup intent, inform
577 * the MDS so that it gets our 'caps wanted' value in a single op.
578 */
579static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
580 struct nameidata *nd)
581{
582 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
583 struct ceph_mds_client *mdsc = fsc->mdsc;
584 struct ceph_mds_request *req;
585 int op;
586 int err;
587
588 dout("lookup %p dentry %p '%.*s'\n",
589 dir, dentry, dentry->d_name.len, dentry->d_name.name);
590
591 if (dentry->d_name.len > NAME_MAX)
592 return ERR_PTR(-ENAMETOOLONG);
593
594 err = ceph_init_dentry(dentry);
595 if (err < 0)
596 return ERR_PTR(err);
597
598 /* open (but not create!) intent? */
599 if (nd &&
600 (nd->flags & LOOKUP_OPEN) &&
601 !(nd->intent.open.flags & O_CREAT)) {
602 int mode = nd->intent.open.create_mode & ~current->fs->umask;
603 return ceph_lookup_open(dir, dentry, nd, mode, 1);
604 }
605
606 /* can we conclude ENOENT locally? */
607 if (dentry->d_inode == NULL) {
608 struct ceph_inode_info *ci = ceph_inode(dir);
609 struct ceph_dentry_info *di = ceph_dentry(dentry);
610
611 spin_lock(&dir->i_lock);
612 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
613 if (strncmp(dentry->d_name.name,
614 fsc->mount_options->snapdir_name,
615 dentry->d_name.len) &&
616 !is_root_ceph_dentry(dir, dentry) &&
617 (ci->i_ceph_flags & CEPH_I_COMPLETE) &&
618 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
619 spin_unlock(&dir->i_lock);
620 dout(" dir %p complete, -ENOENT\n", dir);
621 d_add(dentry, NULL);
622 di->lease_shared_gen = ci->i_shared_gen;
623 return NULL;
624 }
625 spin_unlock(&dir->i_lock);
626 }
627
628 op = ceph_snap(dir) == CEPH_SNAPDIR ?
629 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
630 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
631 if (IS_ERR(req))
632 return ERR_CAST(req);
633 req->r_dentry = dget(dentry);
634 req->r_num_caps = 2;
635 /* we only need inode linkage */
636 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
637 req->r_locked_dir = dir;
638 err = ceph_mdsc_do_request(mdsc, NULL, req);
639 err = ceph_handle_snapdir(req, dentry, err);
640 dentry = ceph_finish_lookup(req, dentry, err);
641 ceph_mdsc_put_request(req); /* will dput(dentry) */
642 dout("lookup result=%p\n", dentry);
643 return dentry;
644}
645
646/*
647 * If we do a create but get no trace back from the MDS, follow up with
648 * a lookup (the VFS expects us to link up the provided dentry).
649 */
650int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
651{
652 struct dentry *result = ceph_lookup(dir, dentry, NULL);
653
654 if (result && !IS_ERR(result)) {
655 /*
656 * We created the item, then did a lookup, and found
657 * it was already linked to another inode we already
658 * had in our cache (and thus got spliced). Link our
659 * dentry to that inode, but don't hash it, just in
660 * case the VFS wants to dereference it.
661 */
662 BUG_ON(!result->d_inode);
663 d_instantiate(dentry, result->d_inode);
664 return 0;
665 }
666 return PTR_ERR(result);
667}
668
669static int ceph_mknod(struct inode *dir, struct dentry *dentry,
670 int mode, dev_t rdev)
671{
672 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
673 struct ceph_mds_client *mdsc = fsc->mdsc;
674 struct ceph_mds_request *req;
675 int err;
676
677 if (ceph_snap(dir) != CEPH_NOSNAP)
678 return -EROFS;
679
680 dout("mknod in dir %p dentry %p mode 0%o rdev %d\n",
681 dir, dentry, mode, rdev);
682 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
683 if (IS_ERR(req)) {
684 d_drop(dentry);
685 return PTR_ERR(req);
686 }
687 req->r_dentry = dget(dentry);
688 req->r_num_caps = 2;
689 req->r_locked_dir = dir;
690 req->r_args.mknod.mode = cpu_to_le32(mode);
691 req->r_args.mknod.rdev = cpu_to_le32(rdev);
692 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
693 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
694 err = ceph_mdsc_do_request(mdsc, dir, req);
695 if (!err && !req->r_reply_info.head->is_dentry)
696 err = ceph_handle_notrace_create(dir, dentry);
697 ceph_mdsc_put_request(req);
698 if (err)
699 d_drop(dentry);
700 return err;
701}
702
703static int ceph_create(struct inode *dir, struct dentry *dentry, int mode,
704 struct nameidata *nd)
705{
706 dout("create in dir %p dentry %p name '%.*s'\n",
707 dir, dentry, dentry->d_name.len, dentry->d_name.name);
708
709 if (ceph_snap(dir) != CEPH_NOSNAP)
710 return -EROFS;
711
712 if (nd) {
713 BUG_ON((nd->flags & LOOKUP_OPEN) == 0);
714 dentry = ceph_lookup_open(dir, dentry, nd, mode, 0);
715 /* hrm, what should i do here if we get aliased? */
716 if (IS_ERR(dentry))
717 return PTR_ERR(dentry);
718 return 0;
719 }
720
721 /* fall back to mknod */
722 return ceph_mknod(dir, dentry, (mode & ~S_IFMT) | S_IFREG, 0);
723}
724
725static int ceph_symlink(struct inode *dir, struct dentry *dentry,
726 const char *dest)
727{
728 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
729 struct ceph_mds_client *mdsc = fsc->mdsc;
730 struct ceph_mds_request *req;
731 int err;
732
733 if (ceph_snap(dir) != CEPH_NOSNAP)
734 return -EROFS;
735
736 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
737 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
738 if (IS_ERR(req)) {
739 d_drop(dentry);
740 return PTR_ERR(req);
741 }
742 req->r_dentry = dget(dentry);
743 req->r_num_caps = 2;
744 req->r_path2 = kstrdup(dest, GFP_NOFS);
745 req->r_locked_dir = dir;
746 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
747 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
748 err = ceph_mdsc_do_request(mdsc, dir, req);
749 if (!err && !req->r_reply_info.head->is_dentry)
750 err = ceph_handle_notrace_create(dir, dentry);
751 ceph_mdsc_put_request(req);
752 if (err)
753 d_drop(dentry);
754 return err;
755}
756
757static int ceph_mkdir(struct inode *dir, struct dentry *dentry, int mode)
758{
759 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
760 struct ceph_mds_client *mdsc = fsc->mdsc;
761 struct ceph_mds_request *req;
762 int err = -EROFS;
763 int op;
764
765 if (ceph_snap(dir) == CEPH_SNAPDIR) {
766 /* mkdir .snap/foo is a MKSNAP */
767 op = CEPH_MDS_OP_MKSNAP;
768 dout("mksnap dir %p snap '%.*s' dn %p\n", dir,
769 dentry->d_name.len, dentry->d_name.name, dentry);
770 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
771 dout("mkdir dir %p dn %p mode 0%o\n", dir, dentry, mode);
772 op = CEPH_MDS_OP_MKDIR;
773 } else {
774 goto out;
775 }
776 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
777 if (IS_ERR(req)) {
778 err = PTR_ERR(req);
779 goto out;
780 }
781
782 req->r_dentry = dget(dentry);
783 req->r_num_caps = 2;
784 req->r_locked_dir = dir;
785 req->r_args.mkdir.mode = cpu_to_le32(mode);
786 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
787 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
788 err = ceph_mdsc_do_request(mdsc, dir, req);
789 if (!err && !req->r_reply_info.head->is_dentry)
790 err = ceph_handle_notrace_create(dir, dentry);
791 ceph_mdsc_put_request(req);
792out:
793 if (err < 0)
794 d_drop(dentry);
795 return err;
796}
797
798static int ceph_link(struct dentry *old_dentry, struct inode *dir,
799 struct dentry *dentry)
800{
801 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
802 struct ceph_mds_client *mdsc = fsc->mdsc;
803 struct ceph_mds_request *req;
804 int err;
805
806 if (ceph_snap(dir) != CEPH_NOSNAP)
807 return -EROFS;
808
809 dout("link in dir %p old_dentry %p dentry %p\n", dir,
810 old_dentry, dentry);
811 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
812 if (IS_ERR(req)) {
813 d_drop(dentry);
814 return PTR_ERR(req);
815 }
816 req->r_dentry = dget(dentry);
817 req->r_num_caps = 2;
818 req->r_old_dentry = dget(old_dentry); /* or inode? hrm. */
819 req->r_old_dentry_dir = ceph_get_dentry_parent_inode(old_dentry);
820 req->r_locked_dir = dir;
821 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
822 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
823 err = ceph_mdsc_do_request(mdsc, dir, req);
824 if (err) {
825 d_drop(dentry);
826 } else if (!req->r_reply_info.head->is_dentry) {
827 ihold(old_dentry->d_inode);
828 d_instantiate(dentry, old_dentry->d_inode);
829 }
830 ceph_mdsc_put_request(req);
831 return err;
832}
833
834/*
835 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
836 * looks like the link count will hit 0, drop any other caps (other
837 * than PIN) we don't specifically want (due to the file still being
838 * open).
839 */
840static int drop_caps_for_unlink(struct inode *inode)
841{
842 struct ceph_inode_info *ci = ceph_inode(inode);
843 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
844
845 spin_lock(&inode->i_lock);
846 if (inode->i_nlink == 1) {
847 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
848 ci->i_ceph_flags |= CEPH_I_NODELAY;
849 }
850 spin_unlock(&inode->i_lock);
851 return drop;
852}
853
854/*
855 * rmdir and unlink are differ only by the metadata op code
856 */
857static int ceph_unlink(struct inode *dir, struct dentry *dentry)
858{
859 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
860 struct ceph_mds_client *mdsc = fsc->mdsc;
861 struct inode *inode = dentry->d_inode;
862 struct ceph_mds_request *req;
863 int err = -EROFS;
864 int op;
865
866 if (ceph_snap(dir) == CEPH_SNAPDIR) {
867 /* rmdir .snap/foo is RMSNAP */
868 dout("rmsnap dir %p '%.*s' dn %p\n", dir, dentry->d_name.len,
869 dentry->d_name.name, dentry);
870 op = CEPH_MDS_OP_RMSNAP;
871 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
872 dout("unlink/rmdir dir %p dn %p inode %p\n",
873 dir, dentry, inode);
874 op = ((dentry->d_inode->i_mode & S_IFMT) == S_IFDIR) ?
875 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
876 } else
877 goto out;
878 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
879 if (IS_ERR(req)) {
880 err = PTR_ERR(req);
881 goto out;
882 }
883 req->r_dentry = dget(dentry);
884 req->r_num_caps = 2;
885 req->r_locked_dir = dir;
886 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
887 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
888 req->r_inode_drop = drop_caps_for_unlink(inode);
889 err = ceph_mdsc_do_request(mdsc, dir, req);
890 if (!err && !req->r_reply_info.head->is_dentry)
891 d_delete(dentry);
892 ceph_mdsc_put_request(req);
893out:
894 return err;
895}
896
897static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
898 struct inode *new_dir, struct dentry *new_dentry)
899{
900 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
901 struct ceph_mds_client *mdsc = fsc->mdsc;
902 struct ceph_mds_request *req;
903 int err;
904
905 if (ceph_snap(old_dir) != ceph_snap(new_dir))
906 return -EXDEV;
907 if (ceph_snap(old_dir) != CEPH_NOSNAP ||
908 ceph_snap(new_dir) != CEPH_NOSNAP)
909 return -EROFS;
910 dout("rename dir %p dentry %p to dir %p dentry %p\n",
911 old_dir, old_dentry, new_dir, new_dentry);
912 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS);
913 if (IS_ERR(req))
914 return PTR_ERR(req);
915 req->r_dentry = dget(new_dentry);
916 req->r_num_caps = 2;
917 req->r_old_dentry = dget(old_dentry);
918 req->r_old_dentry_dir = ceph_get_dentry_parent_inode(old_dentry);
919 req->r_locked_dir = new_dir;
920 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
921 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
922 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
923 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
924 /* release LINK_RDCACHE on source inode (mds will lock it) */
925 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
926 if (new_dentry->d_inode)
927 req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode);
928 err = ceph_mdsc_do_request(mdsc, old_dir, req);
929 if (!err && !req->r_reply_info.head->is_dentry) {
930 /*
931 * Normally d_move() is done by fill_trace (called by
932 * do_request, above). If there is no trace, we need
933 * to do it here.
934 */
935
936 /* d_move screws up d_subdirs order */
937 ceph_i_clear(new_dir, CEPH_I_COMPLETE);
938
939 d_move(old_dentry, new_dentry);
940
941 /* ensure target dentry is invalidated, despite
942 rehashing bug in vfs_rename_dir */
943 ceph_invalidate_dentry_lease(new_dentry);
944 }
945 ceph_mdsc_put_request(req);
946 return err;
947}
948
949/*
950 * Ensure a dentry lease will no longer revalidate.
951 */
952void ceph_invalidate_dentry_lease(struct dentry *dentry)
953{
954 spin_lock(&dentry->d_lock);
955 dentry->d_time = jiffies;
956 ceph_dentry(dentry)->lease_shared_gen = 0;
957 spin_unlock(&dentry->d_lock);
958}
959
960/*
961 * Check if dentry lease is valid. If not, delete the lease. Try to
962 * renew if the least is more than half up.
963 */
964static int dentry_lease_is_valid(struct dentry *dentry)
965{
966 struct ceph_dentry_info *di;
967 struct ceph_mds_session *s;
968 int valid = 0;
969 u32 gen;
970 unsigned long ttl;
971 struct ceph_mds_session *session = NULL;
972 struct inode *dir = NULL;
973 u32 seq = 0;
974
975 spin_lock(&dentry->d_lock);
976 di = ceph_dentry(dentry);
977 if (di && di->lease_session) {
978 s = di->lease_session;
979 spin_lock(&s->s_cap_lock);
980 gen = s->s_cap_gen;
981 ttl = s->s_cap_ttl;
982 spin_unlock(&s->s_cap_lock);
983
984 if (di->lease_gen == gen &&
985 time_before(jiffies, dentry->d_time) &&
986 time_before(jiffies, ttl)) {
987 valid = 1;
988 if (di->lease_renew_after &&
989 time_after(jiffies, di->lease_renew_after)) {
990 /* we should renew */
991 dir = dentry->d_parent->d_inode;
992 session = ceph_get_mds_session(s);
993 seq = di->lease_seq;
994 di->lease_renew_after = 0;
995 di->lease_renew_from = jiffies;
996 }
997 }
998 }
999 spin_unlock(&dentry->d_lock);
1000
1001 if (session) {
1002 ceph_mdsc_lease_send_msg(session, dir, dentry,
1003 CEPH_MDS_LEASE_RENEW, seq);
1004 ceph_put_mds_session(session);
1005 }
1006 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1007 return valid;
1008}
1009
1010/*
1011 * Check if directory-wide content lease/cap is valid.
1012 */
1013static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1014{
1015 struct ceph_inode_info *ci = ceph_inode(dir);
1016 struct ceph_dentry_info *di = ceph_dentry(dentry);
1017 int valid = 0;
1018
1019 spin_lock(&dir->i_lock);
1020 if (ci->i_shared_gen == di->lease_shared_gen)
1021 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1022 spin_unlock(&dir->i_lock);
1023 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1024 dir, (unsigned)ci->i_shared_gen, dentry,
1025 (unsigned)di->lease_shared_gen, valid);
1026 return valid;
1027}
1028
1029/*
1030 * Check if cached dentry can be trusted.
1031 */
1032static int ceph_d_revalidate(struct dentry *dentry, struct nameidata *nd)
1033{
1034 int valid = 0;
1035 struct inode *dir;
1036
1037 if (nd && nd->flags & LOOKUP_RCU)
1038 return -ECHILD;
1039
1040 dout("d_revalidate %p '%.*s' inode %p offset %lld\n", dentry,
1041 dentry->d_name.len, dentry->d_name.name, dentry->d_inode,
1042 ceph_dentry(dentry)->offset);
1043
1044 dir = ceph_get_dentry_parent_inode(dentry);
1045
1046 /* always trust cached snapped dentries, snapdir dentry */
1047 if (ceph_snap(dir) != CEPH_NOSNAP) {
1048 dout("d_revalidate %p '%.*s' inode %p is SNAPPED\n", dentry,
1049 dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
1050 valid = 1;
1051 } else if (dentry->d_inode &&
1052 ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) {
1053 valid = 1;
1054 } else if (dentry_lease_is_valid(dentry) ||
1055 dir_lease_is_valid(dir, dentry)) {
1056 valid = 1;
1057 }
1058
1059 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1060 if (valid)
1061 ceph_dentry_lru_touch(dentry);
1062 else
1063 d_drop(dentry);
1064 iput(dir);
1065 return valid;
1066}
1067
1068/*
1069 * Release our ceph_dentry_info.
1070 */
1071static void ceph_d_release(struct dentry *dentry)
1072{
1073 struct ceph_dentry_info *di = ceph_dentry(dentry);
1074
1075 dout("d_release %p\n", dentry);
1076 if (di) {
1077 ceph_dentry_lru_del(dentry);
1078 if (di->lease_session)
1079 ceph_put_mds_session(di->lease_session);
1080 kmem_cache_free(ceph_dentry_cachep, di);
1081 dentry->d_fsdata = NULL;
1082 }
1083}
1084
1085static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1086 struct nameidata *nd)
1087{
1088 /*
1089 * Eventually, we'll want to revalidate snapped metadata
1090 * too... probably...
1091 */
1092 return 1;
1093}
1094
1095
1096
1097/*
1098 * read() on a dir. This weird interface hack only works if mounted
1099 * with '-o dirstat'.
1100 */
1101static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1102 loff_t *ppos)
1103{
1104 struct ceph_file_info *cf = file->private_data;
1105 struct inode *inode = file->f_dentry->d_inode;
1106 struct ceph_inode_info *ci = ceph_inode(inode);
1107 int left;
1108 const int bufsize = 1024;
1109
1110 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1111 return -EISDIR;
1112
1113 if (!cf->dir_info) {
1114 cf->dir_info = kmalloc(bufsize, GFP_NOFS);
1115 if (!cf->dir_info)
1116 return -ENOMEM;
1117 cf->dir_info_len =
1118 snprintf(cf->dir_info, bufsize,
1119 "entries: %20lld\n"
1120 " files: %20lld\n"
1121 " subdirs: %20lld\n"
1122 "rentries: %20lld\n"
1123 " rfiles: %20lld\n"
1124 " rsubdirs: %20lld\n"
1125 "rbytes: %20lld\n"
1126 "rctime: %10ld.%09ld\n",
1127 ci->i_files + ci->i_subdirs,
1128 ci->i_files,
1129 ci->i_subdirs,
1130 ci->i_rfiles + ci->i_rsubdirs,
1131 ci->i_rfiles,
1132 ci->i_rsubdirs,
1133 ci->i_rbytes,
1134 (long)ci->i_rctime.tv_sec,
1135 (long)ci->i_rctime.tv_nsec);
1136 }
1137
1138 if (*ppos >= cf->dir_info_len)
1139 return 0;
1140 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1141 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1142 if (left == size)
1143 return -EFAULT;
1144 *ppos += (size - left);
1145 return size - left;
1146}
1147
1148/*
1149 * an fsync() on a dir will wait for any uncommitted directory
1150 * operations to commit.
1151 */
1152static int ceph_dir_fsync(struct file *file, loff_t start, loff_t end,
1153 int datasync)
1154{
1155 struct inode *inode = file->f_path.dentry->d_inode;
1156 struct ceph_inode_info *ci = ceph_inode(inode);
1157 struct list_head *head = &ci->i_unsafe_dirops;
1158 struct ceph_mds_request *req;
1159 u64 last_tid;
1160 int ret = 0;
1161
1162 dout("dir_fsync %p\n", inode);
1163 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1164 if (ret)
1165 return ret;
1166 mutex_lock(&inode->i_mutex);
1167
1168 spin_lock(&ci->i_unsafe_lock);
1169 if (list_empty(head))
1170 goto out;
1171
1172 req = list_entry(head->prev,
1173 struct ceph_mds_request, r_unsafe_dir_item);
1174 last_tid = req->r_tid;
1175
1176 do {
1177 ceph_mdsc_get_request(req);
1178 spin_unlock(&ci->i_unsafe_lock);
1179 dout("dir_fsync %p wait on tid %llu (until %llu)\n",
1180 inode, req->r_tid, last_tid);
1181 if (req->r_timeout) {
1182 ret = wait_for_completion_timeout(
1183 &req->r_safe_completion, req->r_timeout);
1184 if (ret > 0)
1185 ret = 0;
1186 else if (ret == 0)
1187 ret = -EIO; /* timed out */
1188 } else {
1189 wait_for_completion(&req->r_safe_completion);
1190 }
1191 spin_lock(&ci->i_unsafe_lock);
1192 ceph_mdsc_put_request(req);
1193
1194 if (ret || list_empty(head))
1195 break;
1196 req = list_entry(head->next,
1197 struct ceph_mds_request, r_unsafe_dir_item);
1198 } while (req->r_tid < last_tid);
1199out:
1200 spin_unlock(&ci->i_unsafe_lock);
1201 mutex_unlock(&inode->i_mutex);
1202
1203 return ret;
1204}
1205
1206/*
1207 * We maintain a private dentry LRU.
1208 *
1209 * FIXME: this needs to be changed to a per-mds lru to be useful.
1210 */
1211void ceph_dentry_lru_add(struct dentry *dn)
1212{
1213 struct ceph_dentry_info *di = ceph_dentry(dn);
1214 struct ceph_mds_client *mdsc;
1215
1216 dout("dentry_lru_add %p %p '%.*s'\n", di, dn,
1217 dn->d_name.len, dn->d_name.name);
1218 if (di) {
1219 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1220 spin_lock(&mdsc->dentry_lru_lock);
1221 list_add_tail(&di->lru, &mdsc->dentry_lru);
1222 mdsc->num_dentry++;
1223 spin_unlock(&mdsc->dentry_lru_lock);
1224 }
1225}
1226
1227void ceph_dentry_lru_touch(struct dentry *dn)
1228{
1229 struct ceph_dentry_info *di = ceph_dentry(dn);
1230 struct ceph_mds_client *mdsc;
1231
1232 dout("dentry_lru_touch %p %p '%.*s' (offset %lld)\n", di, dn,
1233 dn->d_name.len, dn->d_name.name, di->offset);
1234 if (di) {
1235 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1236 spin_lock(&mdsc->dentry_lru_lock);
1237 list_move_tail(&di->lru, &mdsc->dentry_lru);
1238 spin_unlock(&mdsc->dentry_lru_lock);
1239 }
1240}
1241
1242void ceph_dentry_lru_del(struct dentry *dn)
1243{
1244 struct ceph_dentry_info *di = ceph_dentry(dn);
1245 struct ceph_mds_client *mdsc;
1246
1247 dout("dentry_lru_del %p %p '%.*s'\n", di, dn,
1248 dn->d_name.len, dn->d_name.name);
1249 if (di) {
1250 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1251 spin_lock(&mdsc->dentry_lru_lock);
1252 list_del_init(&di->lru);
1253 mdsc->num_dentry--;
1254 spin_unlock(&mdsc->dentry_lru_lock);
1255 }
1256}
1257
1258/*
1259 * Return name hash for a given dentry. This is dependent on
1260 * the parent directory's hash function.
1261 */
1262unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1263{
1264 struct ceph_inode_info *dci = ceph_inode(dir);
1265
1266 switch (dci->i_dir_layout.dl_dir_hash) {
1267 case 0: /* for backward compat */
1268 case CEPH_STR_HASH_LINUX:
1269 return dn->d_name.hash;
1270
1271 default:
1272 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1273 dn->d_name.name, dn->d_name.len);
1274 }
1275}
1276
1277const struct file_operations ceph_dir_fops = {
1278 .read = ceph_read_dir,
1279 .readdir = ceph_readdir,
1280 .llseek = ceph_dir_llseek,
1281 .open = ceph_open,
1282 .release = ceph_release,
1283 .unlocked_ioctl = ceph_ioctl,
1284 .fsync = ceph_dir_fsync,
1285};
1286
1287const struct inode_operations ceph_dir_iops = {
1288 .lookup = ceph_lookup,
1289 .permission = ceph_permission,
1290 .getattr = ceph_getattr,
1291 .setattr = ceph_setattr,
1292 .setxattr = ceph_setxattr,
1293 .getxattr = ceph_getxattr,
1294 .listxattr = ceph_listxattr,
1295 .removexattr = ceph_removexattr,
1296 .mknod = ceph_mknod,
1297 .symlink = ceph_symlink,
1298 .mkdir = ceph_mkdir,
1299 .link = ceph_link,
1300 .unlink = ceph_unlink,
1301 .rmdir = ceph_unlink,
1302 .rename = ceph_rename,
1303 .create = ceph_create,
1304};
1305
1306const struct dentry_operations ceph_dentry_ops = {
1307 .d_revalidate = ceph_d_revalidate,
1308 .d_release = ceph_d_release,
1309};
1310
1311const struct dentry_operations ceph_snapdir_dentry_ops = {
1312 .d_revalidate = ceph_snapdir_d_revalidate,
1313 .d_release = ceph_d_release,
1314};
1315
1316const struct dentry_operations ceph_snap_dentry_ops = {
1317 .d_release = ceph_d_release,
1318};