Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[muen/linux.git] / fs / ceph / dir.c
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
30 const struct dentry_operations ceph_dentry_ops;
31
32 /*
33  * Initialize ceph dentry state.
34  */
35 static int ceph_d_init(struct dentry *dentry)
36 {
37         struct ceph_dentry_info *di;
38
39         di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
40         if (!di)
41                 return -ENOMEM;          /* oh well */
42
43         di->dentry = dentry;
44         di->lease_session = NULL;
45         di->time = jiffies;
46         dentry->d_fsdata = di;
47         ceph_dentry_lru_add(dentry);
48         return 0;
49 }
50
51 /*
52  * for f_pos for readdir:
53  * - hash order:
54  *      (0xff << 52) | ((24 bits hash) << 28) |
55  *      (the nth entry has hash collision);
56  * - frag+name order;
57  *      ((frag value) << 28) | (the nth entry in frag);
58  */
59 #define OFFSET_BITS     28
60 #define OFFSET_MASK     ((1 << OFFSET_BITS) - 1)
61 #define HASH_ORDER      (0xffull << (OFFSET_BITS + 24))
62 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
63 {
64         loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
65         if (hash_order)
66                 fpos |= HASH_ORDER;
67         return fpos;
68 }
69
70 static bool is_hash_order(loff_t p)
71 {
72         return (p & HASH_ORDER) == HASH_ORDER;
73 }
74
75 static unsigned fpos_frag(loff_t p)
76 {
77         return p >> OFFSET_BITS;
78 }
79
80 static unsigned fpos_hash(loff_t p)
81 {
82         return ceph_frag_value(fpos_frag(p));
83 }
84
85 static unsigned fpos_off(loff_t p)
86 {
87         return p & OFFSET_MASK;
88 }
89
90 static int fpos_cmp(loff_t l, loff_t r)
91 {
92         int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
93         if (v)
94                 return v;
95         return (int)(fpos_off(l) - fpos_off(r));
96 }
97
98 /*
99  * make note of the last dentry we read, so we can
100  * continue at the same lexicographical point,
101  * regardless of what dir changes take place on the
102  * server.
103  */
104 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
105                             int len, unsigned next_offset)
106 {
107         char *buf = kmalloc(len+1, GFP_KERNEL);
108         if (!buf)
109                 return -ENOMEM;
110         kfree(fi->last_name);
111         fi->last_name = buf;
112         memcpy(fi->last_name, name, len);
113         fi->last_name[len] = 0;
114         fi->next_offset = next_offset;
115         dout("note_last_dentry '%s'\n", fi->last_name);
116         return 0;
117 }
118
119
120 static struct dentry *
121 __dcache_find_get_entry(struct dentry *parent, u64 idx,
122                         struct ceph_readdir_cache_control *cache_ctl)
123 {
124         struct inode *dir = d_inode(parent);
125         struct dentry *dentry;
126         unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
127         loff_t ptr_pos = idx * sizeof(struct dentry *);
128         pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
129
130         if (ptr_pos >= i_size_read(dir))
131                 return NULL;
132
133         if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
134                 ceph_readdir_cache_release(cache_ctl);
135                 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
136                 if (!cache_ctl->page) {
137                         dout(" page %lu not found\n", ptr_pgoff);
138                         return ERR_PTR(-EAGAIN);
139                 }
140                 /* reading/filling the cache are serialized by
141                    i_mutex, no need to use page lock */
142                 unlock_page(cache_ctl->page);
143                 cache_ctl->dentries = kmap(cache_ctl->page);
144         }
145
146         cache_ctl->index = idx & idx_mask;
147
148         rcu_read_lock();
149         spin_lock(&parent->d_lock);
150         /* check i_size again here, because empty directory can be
151          * marked as complete while not holding the i_mutex. */
152         if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
153                 dentry = cache_ctl->dentries[cache_ctl->index];
154         else
155                 dentry = NULL;
156         spin_unlock(&parent->d_lock);
157         if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
158                 dentry = NULL;
159         rcu_read_unlock();
160         return dentry ? : ERR_PTR(-EAGAIN);
161 }
162
163 /*
164  * When possible, we try to satisfy a readdir by peeking at the
165  * dcache.  We make this work by carefully ordering dentries on
166  * d_child when we initially get results back from the MDS, and
167  * falling back to a "normal" sync readdir if any dentries in the dir
168  * are dropped.
169  *
170  * Complete dir indicates that we have all dentries in the dir.  It is
171  * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
172  * the MDS if/when the directory is modified).
173  */
174 static int __dcache_readdir(struct file *file,  struct dir_context *ctx,
175                             int shared_gen)
176 {
177         struct ceph_file_info *fi = file->private_data;
178         struct dentry *parent = file->f_path.dentry;
179         struct inode *dir = d_inode(parent);
180         struct dentry *dentry, *last = NULL;
181         struct ceph_dentry_info *di;
182         struct ceph_readdir_cache_control cache_ctl = {};
183         u64 idx = 0;
184         int err = 0;
185
186         dout("__dcache_readdir %p v%u at %llx\n", dir, (unsigned)shared_gen, ctx->pos);
187
188         /* search start position */
189         if (ctx->pos > 2) {
190                 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
191                 while (count > 0) {
192                         u64 step = count >> 1;
193                         dentry = __dcache_find_get_entry(parent, idx + step,
194                                                          &cache_ctl);
195                         if (!dentry) {
196                                 /* use linar search */
197                                 idx = 0;
198                                 break;
199                         }
200                         if (IS_ERR(dentry)) {
201                                 err = PTR_ERR(dentry);
202                                 goto out;
203                         }
204                         di = ceph_dentry(dentry);
205                         spin_lock(&dentry->d_lock);
206                         if (fpos_cmp(di->offset, ctx->pos) < 0) {
207                                 idx += step + 1;
208                                 count -= step + 1;
209                         } else {
210                                 count = step;
211                         }
212                         spin_unlock(&dentry->d_lock);
213                         dput(dentry);
214                 }
215
216                 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
217         }
218
219
220         for (;;) {
221                 bool emit_dentry = false;
222                 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
223                 if (!dentry) {
224                         fi->flags |= CEPH_F_ATEND;
225                         err = 0;
226                         break;
227                 }
228                 if (IS_ERR(dentry)) {
229                         err = PTR_ERR(dentry);
230                         goto out;
231                 }
232
233                 spin_lock(&dentry->d_lock);
234                 di = ceph_dentry(dentry);
235                 if (d_unhashed(dentry) ||
236                     d_really_is_negative(dentry) ||
237                     di->lease_shared_gen != shared_gen) {
238                         spin_unlock(&dentry->d_lock);
239                         dput(dentry);
240                         err = -EAGAIN;
241                         goto out;
242                 }
243                 if (fpos_cmp(ctx->pos, di->offset) <= 0) {
244                         emit_dentry = true;
245                 }
246                 spin_unlock(&dentry->d_lock);
247
248                 if (emit_dentry) {
249                         dout(" %llx dentry %p %pd %p\n", di->offset,
250                              dentry, dentry, d_inode(dentry));
251                         ctx->pos = di->offset;
252                         if (!dir_emit(ctx, dentry->d_name.name,
253                                       dentry->d_name.len,
254                                       ceph_translate_ino(dentry->d_sb,
255                                                          d_inode(dentry)->i_ino),
256                                       d_inode(dentry)->i_mode >> 12)) {
257                                 dput(dentry);
258                                 err = 0;
259                                 break;
260                         }
261                         ctx->pos++;
262
263                         if (last)
264                                 dput(last);
265                         last = dentry;
266                 } else {
267                         dput(dentry);
268                 }
269         }
270 out:
271         ceph_readdir_cache_release(&cache_ctl);
272         if (last) {
273                 int ret;
274                 di = ceph_dentry(last);
275                 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
276                                        fpos_off(di->offset) + 1);
277                 if (ret < 0)
278                         err = ret;
279                 dput(last);
280                 /* last_name no longer match cache index */
281                 if (fi->readdir_cache_idx >= 0) {
282                         fi->readdir_cache_idx = -1;
283                         fi->dir_release_count = 0;
284                 }
285         }
286         return err;
287 }
288
289 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
290 {
291         if (!fi->last_readdir)
292                 return true;
293         if (is_hash_order(pos))
294                 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
295         else
296                 return fi->frag != fpos_frag(pos);
297 }
298
299 static int ceph_readdir(struct file *file, struct dir_context *ctx)
300 {
301         struct ceph_file_info *fi = file->private_data;
302         struct inode *inode = file_inode(file);
303         struct ceph_inode_info *ci = ceph_inode(inode);
304         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
305         struct ceph_mds_client *mdsc = fsc->mdsc;
306         int i;
307         int err;
308         unsigned frag = -1;
309         struct ceph_mds_reply_info_parsed *rinfo;
310
311         dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
312         if (fi->flags & CEPH_F_ATEND)
313                 return 0;
314
315         /* always start with . and .. */
316         if (ctx->pos == 0) {
317                 dout("readdir off 0 -> '.'\n");
318                 if (!dir_emit(ctx, ".", 1, 
319                             ceph_translate_ino(inode->i_sb, inode->i_ino),
320                             inode->i_mode >> 12))
321                         return 0;
322                 ctx->pos = 1;
323         }
324         if (ctx->pos == 1) {
325                 ino_t ino = parent_ino(file->f_path.dentry);
326                 dout("readdir off 1 -> '..'\n");
327                 if (!dir_emit(ctx, "..", 2,
328                             ceph_translate_ino(inode->i_sb, ino),
329                             inode->i_mode >> 12))
330                         return 0;
331                 ctx->pos = 2;
332         }
333
334         /* can we use the dcache? */
335         spin_lock(&ci->i_ceph_lock);
336         if (ceph_test_mount_opt(fsc, DCACHE) &&
337             !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
338             ceph_snap(inode) != CEPH_SNAPDIR &&
339             __ceph_dir_is_complete_ordered(ci) &&
340             __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
341                 int shared_gen = atomic_read(&ci->i_shared_gen);
342                 spin_unlock(&ci->i_ceph_lock);
343                 err = __dcache_readdir(file, ctx, shared_gen);
344                 if (err != -EAGAIN)
345                         return err;
346         } else {
347                 spin_unlock(&ci->i_ceph_lock);
348         }
349
350         /* proceed with a normal readdir */
351 more:
352         /* do we have the correct frag content buffered? */
353         if (need_send_readdir(fi, ctx->pos)) {
354                 struct ceph_mds_request *req;
355                 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
356                         CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
357
358                 /* discard old result, if any */
359                 if (fi->last_readdir) {
360                         ceph_mdsc_put_request(fi->last_readdir);
361                         fi->last_readdir = NULL;
362                 }
363
364                 if (is_hash_order(ctx->pos)) {
365                         /* fragtree isn't always accurate. choose frag
366                          * based on previous reply when possible. */
367                         if (frag == (unsigned)-1)
368                                 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
369                                                         NULL, NULL);
370                 } else {
371                         frag = fpos_frag(ctx->pos);
372                 }
373
374                 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
375                      ceph_vinop(inode), frag, fi->last_name);
376                 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
377                 if (IS_ERR(req))
378                         return PTR_ERR(req);
379                 err = ceph_alloc_readdir_reply_buffer(req, inode);
380                 if (err) {
381                         ceph_mdsc_put_request(req);
382                         return err;
383                 }
384                 /* hints to request -> mds selection code */
385                 req->r_direct_mode = USE_AUTH_MDS;
386                 if (op == CEPH_MDS_OP_READDIR) {
387                         req->r_direct_hash = ceph_frag_value(frag);
388                         __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
389                         req->r_inode_drop = CEPH_CAP_FILE_EXCL;
390                 }
391                 if (fi->last_name) {
392                         req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
393                         if (!req->r_path2) {
394                                 ceph_mdsc_put_request(req);
395                                 return -ENOMEM;
396                         }
397                 } else if (is_hash_order(ctx->pos)) {
398                         req->r_args.readdir.offset_hash =
399                                 cpu_to_le32(fpos_hash(ctx->pos));
400                 }
401
402                 req->r_dir_release_cnt = fi->dir_release_count;
403                 req->r_dir_ordered_cnt = fi->dir_ordered_count;
404                 req->r_readdir_cache_idx = fi->readdir_cache_idx;
405                 req->r_readdir_offset = fi->next_offset;
406                 req->r_args.readdir.frag = cpu_to_le32(frag);
407                 req->r_args.readdir.flags =
408                                 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
409
410                 req->r_inode = inode;
411                 ihold(inode);
412                 req->r_dentry = dget(file->f_path.dentry);
413                 err = ceph_mdsc_do_request(mdsc, NULL, req);
414                 if (err < 0) {
415                         ceph_mdsc_put_request(req);
416                         return err;
417                 }
418                 dout("readdir got and parsed readdir result=%d on "
419                      "frag %x, end=%d, complete=%d, hash_order=%d\n",
420                      err, frag,
421                      (int)req->r_reply_info.dir_end,
422                      (int)req->r_reply_info.dir_complete,
423                      (int)req->r_reply_info.hash_order);
424
425                 rinfo = &req->r_reply_info;
426                 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
427                         frag = le32_to_cpu(rinfo->dir_dir->frag);
428                         if (!rinfo->hash_order) {
429                                 fi->next_offset = req->r_readdir_offset;
430                                 /* adjust ctx->pos to beginning of frag */
431                                 ctx->pos = ceph_make_fpos(frag,
432                                                           fi->next_offset,
433                                                           false);
434                         }
435                 }
436
437                 fi->frag = frag;
438                 fi->last_readdir = req;
439
440                 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
441                         fi->readdir_cache_idx = req->r_readdir_cache_idx;
442                         if (fi->readdir_cache_idx < 0) {
443                                 /* preclude from marking dir ordered */
444                                 fi->dir_ordered_count = 0;
445                         } else if (ceph_frag_is_leftmost(frag) &&
446                                    fi->next_offset == 2) {
447                                 /* note dir version at start of readdir so
448                                  * we can tell if any dentries get dropped */
449                                 fi->dir_release_count = req->r_dir_release_cnt;
450                                 fi->dir_ordered_count = req->r_dir_ordered_cnt;
451                         }
452                 } else {
453                         dout("readdir !did_prepopulate");
454                         /* disable readdir cache */
455                         fi->readdir_cache_idx = -1;
456                         /* preclude from marking dir complete */
457                         fi->dir_release_count = 0;
458                 }
459
460                 /* note next offset and last dentry name */
461                 if (rinfo->dir_nr > 0) {
462                         struct ceph_mds_reply_dir_entry *rde =
463                                         rinfo->dir_entries + (rinfo->dir_nr-1);
464                         unsigned next_offset = req->r_reply_info.dir_end ?
465                                         2 : (fpos_off(rde->offset) + 1);
466                         err = note_last_dentry(fi, rde->name, rde->name_len,
467                                                next_offset);
468                         if (err)
469                                 return err;
470                 } else if (req->r_reply_info.dir_end) {
471                         fi->next_offset = 2;
472                         /* keep last name */
473                 }
474         }
475
476         rinfo = &fi->last_readdir->r_reply_info;
477         dout("readdir frag %x num %d pos %llx chunk first %llx\n",
478              fi->frag, rinfo->dir_nr, ctx->pos,
479              rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
480
481         i = 0;
482         /* search start position */
483         if (rinfo->dir_nr > 0) {
484                 int step, nr = rinfo->dir_nr;
485                 while (nr > 0) {
486                         step = nr >> 1;
487                         if (rinfo->dir_entries[i + step].offset < ctx->pos) {
488                                 i +=  step + 1;
489                                 nr -= step + 1;
490                         } else {
491                                 nr = step;
492                         }
493                 }
494         }
495         for (; i < rinfo->dir_nr; i++) {
496                 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
497                 struct ceph_vino vino;
498                 ino_t ino;
499                 u32 ftype;
500
501                 BUG_ON(rde->offset < ctx->pos);
502
503                 ctx->pos = rde->offset;
504                 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
505                      i, rinfo->dir_nr, ctx->pos,
506                      rde->name_len, rde->name, &rde->inode.in);
507
508                 BUG_ON(!rde->inode.in);
509                 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
510                 vino.ino = le64_to_cpu(rde->inode.in->ino);
511                 vino.snap = le64_to_cpu(rde->inode.in->snapid);
512                 ino = ceph_vino_to_ino(vino);
513
514                 if (!dir_emit(ctx, rde->name, rde->name_len,
515                               ceph_translate_ino(inode->i_sb, ino), ftype)) {
516                         dout("filldir stopping us...\n");
517                         return 0;
518                 }
519                 ctx->pos++;
520         }
521
522         ceph_mdsc_put_request(fi->last_readdir);
523         fi->last_readdir = NULL;
524
525         if (fi->next_offset > 2) {
526                 frag = fi->frag;
527                 goto more;
528         }
529
530         /* more frags? */
531         if (!ceph_frag_is_rightmost(fi->frag)) {
532                 frag = ceph_frag_next(fi->frag);
533                 if (is_hash_order(ctx->pos)) {
534                         loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
535                                                         fi->next_offset, true);
536                         if (new_pos > ctx->pos)
537                                 ctx->pos = new_pos;
538                         /* keep last_name */
539                 } else {
540                         ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
541                         kfree(fi->last_name);
542                         fi->last_name = NULL;
543                 }
544                 dout("readdir next frag is %x\n", frag);
545                 goto more;
546         }
547         fi->flags |= CEPH_F_ATEND;
548
549         /*
550          * if dir_release_count still matches the dir, no dentries
551          * were released during the whole readdir, and we should have
552          * the complete dir contents in our cache.
553          */
554         if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
555                 spin_lock(&ci->i_ceph_lock);
556                 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
557                         dout(" marking %p complete and ordered\n", inode);
558                         /* use i_size to track number of entries in
559                          * readdir cache */
560                         BUG_ON(fi->readdir_cache_idx < 0);
561                         i_size_write(inode, fi->readdir_cache_idx *
562                                      sizeof(struct dentry*));
563                 } else {
564                         dout(" marking %p complete\n", inode);
565                 }
566                 __ceph_dir_set_complete(ci, fi->dir_release_count,
567                                         fi->dir_ordered_count);
568                 spin_unlock(&ci->i_ceph_lock);
569         }
570
571         dout("readdir %p file %p done.\n", inode, file);
572         return 0;
573 }
574
575 static void reset_readdir(struct ceph_file_info *fi)
576 {
577         if (fi->last_readdir) {
578                 ceph_mdsc_put_request(fi->last_readdir);
579                 fi->last_readdir = NULL;
580         }
581         kfree(fi->last_name);
582         fi->last_name = NULL;
583         fi->dir_release_count = 0;
584         fi->readdir_cache_idx = -1;
585         fi->next_offset = 2;  /* compensate for . and .. */
586         fi->flags &= ~CEPH_F_ATEND;
587 }
588
589 /*
590  * discard buffered readdir content on seekdir(0), or seek to new frag,
591  * or seek prior to current chunk
592  */
593 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
594 {
595         struct ceph_mds_reply_info_parsed *rinfo;
596         loff_t chunk_offset;
597         if (new_pos == 0)
598                 return true;
599         if (is_hash_order(new_pos)) {
600                 /* no need to reset last_name for a forward seek when
601                  * dentries are sotred in hash order */
602         } else if (fi->frag != fpos_frag(new_pos)) {
603                 return true;
604         }
605         rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
606         if (!rinfo || !rinfo->dir_nr)
607                 return true;
608         chunk_offset = rinfo->dir_entries[0].offset;
609         return new_pos < chunk_offset ||
610                is_hash_order(new_pos) != is_hash_order(chunk_offset);
611 }
612
613 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
614 {
615         struct ceph_file_info *fi = file->private_data;
616         struct inode *inode = file->f_mapping->host;
617         loff_t retval;
618
619         inode_lock(inode);
620         retval = -EINVAL;
621         switch (whence) {
622         case SEEK_CUR:
623                 offset += file->f_pos;
624         case SEEK_SET:
625                 break;
626         case SEEK_END:
627                 retval = -EOPNOTSUPP;
628         default:
629                 goto out;
630         }
631
632         if (offset >= 0) {
633                 if (need_reset_readdir(fi, offset)) {
634                         dout("dir_llseek dropping %p content\n", file);
635                         reset_readdir(fi);
636                 } else if (is_hash_order(offset) && offset > file->f_pos) {
637                         /* for hash offset, we don't know if a forward seek
638                          * is within same frag */
639                         fi->dir_release_count = 0;
640                         fi->readdir_cache_idx = -1;
641                 }
642
643                 if (offset != file->f_pos) {
644                         file->f_pos = offset;
645                         file->f_version = 0;
646                         fi->flags &= ~CEPH_F_ATEND;
647                 }
648                 retval = offset;
649         }
650 out:
651         inode_unlock(inode);
652         return retval;
653 }
654
655 /*
656  * Handle lookups for the hidden .snap directory.
657  */
658 int ceph_handle_snapdir(struct ceph_mds_request *req,
659                         struct dentry *dentry, int err)
660 {
661         struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
662         struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
663
664         /* .snap dir? */
665         if (err == -ENOENT &&
666             ceph_snap(parent) == CEPH_NOSNAP &&
667             strcmp(dentry->d_name.name,
668                    fsc->mount_options->snapdir_name) == 0) {
669                 struct inode *inode = ceph_get_snapdir(parent);
670                 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
671                      dentry, dentry, inode);
672                 BUG_ON(!d_unhashed(dentry));
673                 d_add(dentry, inode);
674                 err = 0;
675         }
676         return err;
677 }
678
679 /*
680  * Figure out final result of a lookup/open request.
681  *
682  * Mainly, make sure we return the final req->r_dentry (if it already
683  * existed) in place of the original VFS-provided dentry when they
684  * differ.
685  *
686  * Gracefully handle the case where the MDS replies with -ENOENT and
687  * no trace (which it may do, at its discretion, e.g., if it doesn't
688  * care to issue a lease on the negative dentry).
689  */
690 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
691                                   struct dentry *dentry, int err)
692 {
693         if (err == -ENOENT) {
694                 /* no trace? */
695                 err = 0;
696                 if (!req->r_reply_info.head->is_dentry) {
697                         dout("ENOENT and no trace, dentry %p inode %p\n",
698                              dentry, d_inode(dentry));
699                         if (d_really_is_positive(dentry)) {
700                                 d_drop(dentry);
701                                 err = -ENOENT;
702                         } else {
703                                 d_add(dentry, NULL);
704                         }
705                 }
706         }
707         if (err)
708                 dentry = ERR_PTR(err);
709         else if (dentry != req->r_dentry)
710                 dentry = dget(req->r_dentry);   /* we got spliced */
711         else
712                 dentry = NULL;
713         return dentry;
714 }
715
716 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
717 {
718         return ceph_ino(inode) == CEPH_INO_ROOT &&
719                 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
720 }
721
722 /*
723  * Look up a single dir entry.  If there is a lookup intent, inform
724  * the MDS so that it gets our 'caps wanted' value in a single op.
725  */
726 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
727                                   unsigned int flags)
728 {
729         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
730         struct ceph_mds_client *mdsc = fsc->mdsc;
731         struct ceph_mds_request *req;
732         int op;
733         int mask;
734         int err;
735
736         dout("lookup %p dentry %p '%pd'\n",
737              dir, dentry, dentry);
738
739         if (dentry->d_name.len > NAME_MAX)
740                 return ERR_PTR(-ENAMETOOLONG);
741
742         /* can we conclude ENOENT locally? */
743         if (d_really_is_negative(dentry)) {
744                 struct ceph_inode_info *ci = ceph_inode(dir);
745                 struct ceph_dentry_info *di = ceph_dentry(dentry);
746
747                 spin_lock(&ci->i_ceph_lock);
748                 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
749                 if (strncmp(dentry->d_name.name,
750                             fsc->mount_options->snapdir_name,
751                             dentry->d_name.len) &&
752                     !is_root_ceph_dentry(dir, dentry) &&
753                     ceph_test_mount_opt(fsc, DCACHE) &&
754                     __ceph_dir_is_complete(ci) &&
755                     (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
756                         spin_unlock(&ci->i_ceph_lock);
757                         dout(" dir %p complete, -ENOENT\n", dir);
758                         d_add(dentry, NULL);
759                         di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
760                         return NULL;
761                 }
762                 spin_unlock(&ci->i_ceph_lock);
763         }
764
765         op = ceph_snap(dir) == CEPH_SNAPDIR ?
766                 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
767         req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
768         if (IS_ERR(req))
769                 return ERR_CAST(req);
770         req->r_dentry = dget(dentry);
771         req->r_num_caps = 2;
772
773         mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
774         if (ceph_security_xattr_wanted(dir))
775                 mask |= CEPH_CAP_XATTR_SHARED;
776         req->r_args.getattr.mask = cpu_to_le32(mask);
777
778         req->r_parent = dir;
779         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
780         err = ceph_mdsc_do_request(mdsc, NULL, req);
781         err = ceph_handle_snapdir(req, dentry, err);
782         dentry = ceph_finish_lookup(req, dentry, err);
783         ceph_mdsc_put_request(req);  /* will dput(dentry) */
784         dout("lookup result=%p\n", dentry);
785         return dentry;
786 }
787
788 /*
789  * If we do a create but get no trace back from the MDS, follow up with
790  * a lookup (the VFS expects us to link up the provided dentry).
791  */
792 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
793 {
794         struct dentry *result = ceph_lookup(dir, dentry, 0);
795
796         if (result && !IS_ERR(result)) {
797                 /*
798                  * We created the item, then did a lookup, and found
799                  * it was already linked to another inode we already
800                  * had in our cache (and thus got spliced). To not
801                  * confuse VFS (especially when inode is a directory),
802                  * we don't link our dentry to that inode, return an
803                  * error instead.
804                  *
805                  * This event should be rare and it happens only when
806                  * we talk to old MDS. Recent MDS does not send traceless
807                  * reply for request that creates new inode.
808                  */
809                 d_drop(result);
810                 return -ESTALE;
811         }
812         return PTR_ERR(result);
813 }
814
815 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
816                       umode_t mode, dev_t rdev)
817 {
818         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
819         struct ceph_mds_client *mdsc = fsc->mdsc;
820         struct ceph_mds_request *req;
821         struct ceph_acls_info acls = {};
822         int err;
823
824         if (ceph_snap(dir) != CEPH_NOSNAP)
825                 return -EROFS;
826
827         err = ceph_pre_init_acls(dir, &mode, &acls);
828         if (err < 0)
829                 return err;
830
831         dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
832              dir, dentry, mode, rdev);
833         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
834         if (IS_ERR(req)) {
835                 err = PTR_ERR(req);
836                 goto out;
837         }
838         req->r_dentry = dget(dentry);
839         req->r_num_caps = 2;
840         req->r_parent = dir;
841         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
842         req->r_args.mknod.mode = cpu_to_le32(mode);
843         req->r_args.mknod.rdev = cpu_to_le32(rdev);
844         req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
845         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
846         if (acls.pagelist) {
847                 req->r_pagelist = acls.pagelist;
848                 acls.pagelist = NULL;
849         }
850         err = ceph_mdsc_do_request(mdsc, dir, req);
851         if (!err && !req->r_reply_info.head->is_dentry)
852                 err = ceph_handle_notrace_create(dir, dentry);
853         ceph_mdsc_put_request(req);
854 out:
855         if (!err)
856                 ceph_init_inode_acls(d_inode(dentry), &acls);
857         else
858                 d_drop(dentry);
859         ceph_release_acls_info(&acls);
860         return err;
861 }
862
863 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
864                        bool excl)
865 {
866         return ceph_mknod(dir, dentry, mode, 0);
867 }
868
869 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
870                             const char *dest)
871 {
872         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
873         struct ceph_mds_client *mdsc = fsc->mdsc;
874         struct ceph_mds_request *req;
875         int err;
876
877         if (ceph_snap(dir) != CEPH_NOSNAP)
878                 return -EROFS;
879
880         dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
881         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
882         if (IS_ERR(req)) {
883                 err = PTR_ERR(req);
884                 goto out;
885         }
886         req->r_path2 = kstrdup(dest, GFP_KERNEL);
887         if (!req->r_path2) {
888                 err = -ENOMEM;
889                 ceph_mdsc_put_request(req);
890                 goto out;
891         }
892         req->r_parent = dir;
893         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
894         req->r_dentry = dget(dentry);
895         req->r_num_caps = 2;
896         req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
897         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
898         err = ceph_mdsc_do_request(mdsc, dir, req);
899         if (!err && !req->r_reply_info.head->is_dentry)
900                 err = ceph_handle_notrace_create(dir, dentry);
901         ceph_mdsc_put_request(req);
902 out:
903         if (err)
904                 d_drop(dentry);
905         return err;
906 }
907
908 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
909 {
910         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
911         struct ceph_mds_client *mdsc = fsc->mdsc;
912         struct ceph_mds_request *req;
913         struct ceph_acls_info acls = {};
914         int err = -EROFS;
915         int op;
916
917         if (ceph_snap(dir) == CEPH_SNAPDIR) {
918                 /* mkdir .snap/foo is a MKSNAP */
919                 op = CEPH_MDS_OP_MKSNAP;
920                 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
921                      dentry, dentry);
922         } else if (ceph_snap(dir) == CEPH_NOSNAP) {
923                 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
924                 op = CEPH_MDS_OP_MKDIR;
925         } else {
926                 goto out;
927         }
928
929         mode |= S_IFDIR;
930         err = ceph_pre_init_acls(dir, &mode, &acls);
931         if (err < 0)
932                 goto out;
933
934         req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
935         if (IS_ERR(req)) {
936                 err = PTR_ERR(req);
937                 goto out;
938         }
939
940         req->r_dentry = dget(dentry);
941         req->r_num_caps = 2;
942         req->r_parent = dir;
943         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
944         req->r_args.mkdir.mode = cpu_to_le32(mode);
945         req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
946         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
947         if (acls.pagelist) {
948                 req->r_pagelist = acls.pagelist;
949                 acls.pagelist = NULL;
950         }
951         err = ceph_mdsc_do_request(mdsc, dir, req);
952         if (!err &&
953             !req->r_reply_info.head->is_target &&
954             !req->r_reply_info.head->is_dentry)
955                 err = ceph_handle_notrace_create(dir, dentry);
956         ceph_mdsc_put_request(req);
957 out:
958         if (!err)
959                 ceph_init_inode_acls(d_inode(dentry), &acls);
960         else
961                 d_drop(dentry);
962         ceph_release_acls_info(&acls);
963         return err;
964 }
965
966 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
967                      struct dentry *dentry)
968 {
969         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
970         struct ceph_mds_client *mdsc = fsc->mdsc;
971         struct ceph_mds_request *req;
972         int err;
973
974         if (ceph_snap(dir) != CEPH_NOSNAP)
975                 return -EROFS;
976
977         dout("link in dir %p old_dentry %p dentry %p\n", dir,
978              old_dentry, dentry);
979         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
980         if (IS_ERR(req)) {
981                 d_drop(dentry);
982                 return PTR_ERR(req);
983         }
984         req->r_dentry = dget(dentry);
985         req->r_num_caps = 2;
986         req->r_old_dentry = dget(old_dentry);
987         req->r_parent = dir;
988         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
989         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
990         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
991         /* release LINK_SHARED on source inode (mds will lock it) */
992         req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
993         err = ceph_mdsc_do_request(mdsc, dir, req);
994         if (err) {
995                 d_drop(dentry);
996         } else if (!req->r_reply_info.head->is_dentry) {
997                 ihold(d_inode(old_dentry));
998                 d_instantiate(dentry, d_inode(old_dentry));
999         }
1000         ceph_mdsc_put_request(req);
1001         return err;
1002 }
1003
1004 /*
1005  * rmdir and unlink are differ only by the metadata op code
1006  */
1007 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1008 {
1009         struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1010         struct ceph_mds_client *mdsc = fsc->mdsc;
1011         struct inode *inode = d_inode(dentry);
1012         struct ceph_mds_request *req;
1013         int err = -EROFS;
1014         int op;
1015
1016         if (ceph_snap(dir) == CEPH_SNAPDIR) {
1017                 /* rmdir .snap/foo is RMSNAP */
1018                 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1019                 op = CEPH_MDS_OP_RMSNAP;
1020         } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1021                 dout("unlink/rmdir dir %p dn %p inode %p\n",
1022                      dir, dentry, inode);
1023                 op = d_is_dir(dentry) ?
1024                         CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1025         } else
1026                 goto out;
1027         req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1028         if (IS_ERR(req)) {
1029                 err = PTR_ERR(req);
1030                 goto out;
1031         }
1032         req->r_dentry = dget(dentry);
1033         req->r_num_caps = 2;
1034         req->r_parent = dir;
1035         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1036         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1037         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1038         req->r_inode_drop = ceph_drop_caps_for_unlink(inode);
1039         err = ceph_mdsc_do_request(mdsc, dir, req);
1040         if (!err && !req->r_reply_info.head->is_dentry)
1041                 d_delete(dentry);
1042         ceph_mdsc_put_request(req);
1043 out:
1044         return err;
1045 }
1046
1047 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1048                        struct inode *new_dir, struct dentry *new_dentry,
1049                        unsigned int flags)
1050 {
1051         struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1052         struct ceph_mds_client *mdsc = fsc->mdsc;
1053         struct ceph_mds_request *req;
1054         int op = CEPH_MDS_OP_RENAME;
1055         int err;
1056
1057         if (flags)
1058                 return -EINVAL;
1059
1060         if (ceph_snap(old_dir) != ceph_snap(new_dir))
1061                 return -EXDEV;
1062         if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1063                 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1064                         op = CEPH_MDS_OP_RENAMESNAP;
1065                 else
1066                         return -EROFS;
1067         }
1068         dout("rename dir %p dentry %p to dir %p dentry %p\n",
1069              old_dir, old_dentry, new_dir, new_dentry);
1070         req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1071         if (IS_ERR(req))
1072                 return PTR_ERR(req);
1073         ihold(old_dir);
1074         req->r_dentry = dget(new_dentry);
1075         req->r_num_caps = 2;
1076         req->r_old_dentry = dget(old_dentry);
1077         req->r_old_dentry_dir = old_dir;
1078         req->r_parent = new_dir;
1079         set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1080         req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1081         req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1082         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1083         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1084         /* release LINK_RDCACHE on source inode (mds will lock it) */
1085         req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1086         if (d_really_is_positive(new_dentry)) {
1087                 req->r_inode_drop =
1088                         ceph_drop_caps_for_unlink(d_inode(new_dentry));
1089         }
1090         err = ceph_mdsc_do_request(mdsc, old_dir, req);
1091         if (!err && !req->r_reply_info.head->is_dentry) {
1092                 /*
1093                  * Normally d_move() is done by fill_trace (called by
1094                  * do_request, above).  If there is no trace, we need
1095                  * to do it here.
1096                  */
1097                 d_move(old_dentry, new_dentry);
1098         }
1099         ceph_mdsc_put_request(req);
1100         return err;
1101 }
1102
1103 /*
1104  * Ensure a dentry lease will no longer revalidate.
1105  */
1106 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1107 {
1108         spin_lock(&dentry->d_lock);
1109         ceph_dentry(dentry)->time = jiffies;
1110         ceph_dentry(dentry)->lease_shared_gen = 0;
1111         spin_unlock(&dentry->d_lock);
1112 }
1113
1114 /*
1115  * Check if dentry lease is valid.  If not, delete the lease.  Try to
1116  * renew if the least is more than half up.
1117  */
1118 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1119                                  struct inode *dir)
1120 {
1121         struct ceph_dentry_info *di;
1122         struct ceph_mds_session *s;
1123         int valid = 0;
1124         u32 gen;
1125         unsigned long ttl;
1126         struct ceph_mds_session *session = NULL;
1127         u32 seq = 0;
1128
1129         spin_lock(&dentry->d_lock);
1130         di = ceph_dentry(dentry);
1131         if (di && di->lease_session) {
1132                 s = di->lease_session;
1133                 spin_lock(&s->s_gen_ttl_lock);
1134                 gen = s->s_cap_gen;
1135                 ttl = s->s_cap_ttl;
1136                 spin_unlock(&s->s_gen_ttl_lock);
1137
1138                 if (di->lease_gen == gen &&
1139                     time_before(jiffies, di->time) &&
1140                     time_before(jiffies, ttl)) {
1141                         valid = 1;
1142                         if (di->lease_renew_after &&
1143                             time_after(jiffies, di->lease_renew_after)) {
1144                                 /*
1145                                  * We should renew. If we're in RCU walk mode
1146                                  * though, we can't do that so just return
1147                                  * -ECHILD.
1148                                  */
1149                                 if (flags & LOOKUP_RCU) {
1150                                         valid = -ECHILD;
1151                                 } else {
1152                                         session = ceph_get_mds_session(s);
1153                                         seq = di->lease_seq;
1154                                         di->lease_renew_after = 0;
1155                                         di->lease_renew_from = jiffies;
1156                                 }
1157                         }
1158                 }
1159         }
1160         spin_unlock(&dentry->d_lock);
1161
1162         if (session) {
1163                 ceph_mdsc_lease_send_msg(session, dir, dentry,
1164                                          CEPH_MDS_LEASE_RENEW, seq);
1165                 ceph_put_mds_session(session);
1166         }
1167         dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1168         return valid;
1169 }
1170
1171 /*
1172  * Check if directory-wide content lease/cap is valid.
1173  */
1174 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1175 {
1176         struct ceph_inode_info *ci = ceph_inode(dir);
1177         struct ceph_dentry_info *di = ceph_dentry(dentry);
1178         int valid = 0;
1179
1180         spin_lock(&ci->i_ceph_lock);
1181         if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen)
1182                 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1183         spin_unlock(&ci->i_ceph_lock);
1184         dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1185              dir, (unsigned)atomic_read(&ci->i_shared_gen),
1186              dentry, (unsigned)di->lease_shared_gen, valid);
1187         return valid;
1188 }
1189
1190 /*
1191  * Check if cached dentry can be trusted.
1192  */
1193 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1194 {
1195         int valid = 0;
1196         struct dentry *parent;
1197         struct inode *dir;
1198
1199         if (flags & LOOKUP_RCU) {
1200                 parent = READ_ONCE(dentry->d_parent);
1201                 dir = d_inode_rcu(parent);
1202                 if (!dir)
1203                         return -ECHILD;
1204         } else {
1205                 parent = dget_parent(dentry);
1206                 dir = d_inode(parent);
1207         }
1208
1209         dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1210              dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1211
1212         /* always trust cached snapped dentries, snapdir dentry */
1213         if (ceph_snap(dir) != CEPH_NOSNAP) {
1214                 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1215                      dentry, d_inode(dentry));
1216                 valid = 1;
1217         } else if (d_really_is_positive(dentry) &&
1218                    ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1219                 valid = 1;
1220         } else {
1221                 valid = dentry_lease_is_valid(dentry, flags, dir);
1222                 if (valid == -ECHILD)
1223                         return valid;
1224                 if (valid || dir_lease_is_valid(dir, dentry)) {
1225                         if (d_really_is_positive(dentry))
1226                                 valid = ceph_is_any_caps(d_inode(dentry));
1227                         else
1228                                 valid = 1;
1229                 }
1230         }
1231
1232         if (!valid) {
1233                 struct ceph_mds_client *mdsc =
1234                         ceph_sb_to_client(dir->i_sb)->mdsc;
1235                 struct ceph_mds_request *req;
1236                 int op, err;
1237                 u32 mask;
1238
1239                 if (flags & LOOKUP_RCU)
1240                         return -ECHILD;
1241
1242                 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1243                         CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1244                 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1245                 if (!IS_ERR(req)) {
1246                         req->r_dentry = dget(dentry);
1247                         req->r_num_caps = 2;
1248                         req->r_parent = dir;
1249
1250                         mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1251                         if (ceph_security_xattr_wanted(dir))
1252                                 mask |= CEPH_CAP_XATTR_SHARED;
1253                         req->r_args.getattr.mask = cpu_to_le32(mask);
1254
1255                         err = ceph_mdsc_do_request(mdsc, NULL, req);
1256                         switch (err) {
1257                         case 0:
1258                                 if (d_really_is_positive(dentry) &&
1259                                     d_inode(dentry) == req->r_target_inode)
1260                                         valid = 1;
1261                                 break;
1262                         case -ENOENT:
1263                                 if (d_really_is_negative(dentry))
1264                                         valid = 1;
1265                                 /* Fallthrough */
1266                         default:
1267                                 break;
1268                         }
1269                         ceph_mdsc_put_request(req);
1270                         dout("d_revalidate %p lookup result=%d\n",
1271                              dentry, err);
1272                 }
1273         }
1274
1275         dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1276         if (valid) {
1277                 ceph_dentry_lru_touch(dentry);
1278         } else {
1279                 ceph_dir_clear_complete(dir);
1280         }
1281
1282         if (!(flags & LOOKUP_RCU))
1283                 dput(parent);
1284         return valid;
1285 }
1286
1287 /*
1288  * Release our ceph_dentry_info.
1289  */
1290 static void ceph_d_release(struct dentry *dentry)
1291 {
1292         struct ceph_dentry_info *di = ceph_dentry(dentry);
1293
1294         dout("d_release %p\n", dentry);
1295         ceph_dentry_lru_del(dentry);
1296
1297         spin_lock(&dentry->d_lock);
1298         dentry->d_fsdata = NULL;
1299         spin_unlock(&dentry->d_lock);
1300
1301         if (di->lease_session)
1302                 ceph_put_mds_session(di->lease_session);
1303         kmem_cache_free(ceph_dentry_cachep, di);
1304 }
1305
1306 /*
1307  * When the VFS prunes a dentry from the cache, we need to clear the
1308  * complete flag on the parent directory.
1309  *
1310  * Called under dentry->d_lock.
1311  */
1312 static void ceph_d_prune(struct dentry *dentry)
1313 {
1314         struct ceph_inode_info *dir_ci;
1315         struct ceph_dentry_info *di;
1316
1317         dout("ceph_d_prune %pd %p\n", dentry, dentry);
1318
1319         /* do we have a valid parent? */
1320         if (IS_ROOT(dentry))
1321                 return;
1322
1323         /* we hold d_lock, so d_parent is stable */
1324         dir_ci = ceph_inode(d_inode(dentry->d_parent));
1325         if (dir_ci->i_vino.snap == CEPH_SNAPDIR)
1326                 return;
1327
1328         /* who calls d_delete() should also disable dcache readdir */
1329         if (d_really_is_negative(dentry))
1330                 return;
1331
1332         /* d_fsdata does not get cleared until d_release */
1333         if (!d_unhashed(dentry)) {
1334                 __ceph_dir_clear_complete(dir_ci);
1335                 return;
1336         }
1337
1338         /* Disable dcache readdir just in case that someone called d_drop()
1339          * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
1340          * properly (dcache readdir is still enabled) */
1341         di = ceph_dentry(dentry);
1342         if (di->offset > 0 &&
1343             di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen))
1344                 __ceph_dir_clear_ordered(dir_ci);
1345 }
1346
1347 /*
1348  * read() on a dir.  This weird interface hack only works if mounted
1349  * with '-o dirstat'.
1350  */
1351 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1352                              loff_t *ppos)
1353 {
1354         struct ceph_file_info *cf = file->private_data;
1355         struct inode *inode = file_inode(file);
1356         struct ceph_inode_info *ci = ceph_inode(inode);
1357         int left;
1358         const int bufsize = 1024;
1359
1360         if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1361                 return -EISDIR;
1362
1363         if (!cf->dir_info) {
1364                 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1365                 if (!cf->dir_info)
1366                         return -ENOMEM;
1367                 cf->dir_info_len =
1368                         snprintf(cf->dir_info, bufsize,
1369                                 "entries:   %20lld\n"
1370                                 " files:    %20lld\n"
1371                                 " subdirs:  %20lld\n"
1372                                 "rentries:  %20lld\n"
1373                                 " rfiles:   %20lld\n"
1374                                 " rsubdirs: %20lld\n"
1375                                 "rbytes:    %20lld\n"
1376                                 "rctime:    %10ld.%09ld\n",
1377                                 ci->i_files + ci->i_subdirs,
1378                                 ci->i_files,
1379                                 ci->i_subdirs,
1380                                 ci->i_rfiles + ci->i_rsubdirs,
1381                                 ci->i_rfiles,
1382                                 ci->i_rsubdirs,
1383                                 ci->i_rbytes,
1384                                 (long)ci->i_rctime.tv_sec,
1385                                 (long)ci->i_rctime.tv_nsec);
1386         }
1387
1388         if (*ppos >= cf->dir_info_len)
1389                 return 0;
1390         size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1391         left = copy_to_user(buf, cf->dir_info + *ppos, size);
1392         if (left == size)
1393                 return -EFAULT;
1394         *ppos += (size - left);
1395         return size - left;
1396 }
1397
1398 /*
1399  * We maintain a private dentry LRU.
1400  *
1401  * FIXME: this needs to be changed to a per-mds lru to be useful.
1402  */
1403 void ceph_dentry_lru_add(struct dentry *dn)
1404 {
1405         struct ceph_dentry_info *di = ceph_dentry(dn);
1406         struct ceph_mds_client *mdsc;
1407
1408         dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1409         mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1410         spin_lock(&mdsc->dentry_lru_lock);
1411         list_add_tail(&di->lru, &mdsc->dentry_lru);
1412         mdsc->num_dentry++;
1413         spin_unlock(&mdsc->dentry_lru_lock);
1414 }
1415
1416 void ceph_dentry_lru_touch(struct dentry *dn)
1417 {
1418         struct ceph_dentry_info *di = ceph_dentry(dn);
1419         struct ceph_mds_client *mdsc;
1420
1421         dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1422              di->offset);
1423         mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1424         spin_lock(&mdsc->dentry_lru_lock);
1425         list_move_tail(&di->lru, &mdsc->dentry_lru);
1426         spin_unlock(&mdsc->dentry_lru_lock);
1427 }
1428
1429 void ceph_dentry_lru_del(struct dentry *dn)
1430 {
1431         struct ceph_dentry_info *di = ceph_dentry(dn);
1432         struct ceph_mds_client *mdsc;
1433
1434         dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1435         mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1436         spin_lock(&mdsc->dentry_lru_lock);
1437         list_del_init(&di->lru);
1438         mdsc->num_dentry--;
1439         spin_unlock(&mdsc->dentry_lru_lock);
1440 }
1441
1442 /*
1443  * Return name hash for a given dentry.  This is dependent on
1444  * the parent directory's hash function.
1445  */
1446 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1447 {
1448         struct ceph_inode_info *dci = ceph_inode(dir);
1449
1450         switch (dci->i_dir_layout.dl_dir_hash) {
1451         case 0: /* for backward compat */
1452         case CEPH_STR_HASH_LINUX:
1453                 return dn->d_name.hash;
1454
1455         default:
1456                 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1457                                      dn->d_name.name, dn->d_name.len);
1458         }
1459 }
1460
1461 const struct file_operations ceph_dir_fops = {
1462         .read = ceph_read_dir,
1463         .iterate = ceph_readdir,
1464         .llseek = ceph_dir_llseek,
1465         .open = ceph_open,
1466         .release = ceph_release,
1467         .unlocked_ioctl = ceph_ioctl,
1468         .fsync = ceph_fsync,
1469 };
1470
1471 const struct file_operations ceph_snapdir_fops = {
1472         .iterate = ceph_readdir,
1473         .llseek = ceph_dir_llseek,
1474         .open = ceph_open,
1475         .release = ceph_release,
1476 };
1477
1478 const struct inode_operations ceph_dir_iops = {
1479         .lookup = ceph_lookup,
1480         .permission = ceph_permission,
1481         .getattr = ceph_getattr,
1482         .setattr = ceph_setattr,
1483         .listxattr = ceph_listxattr,
1484         .get_acl = ceph_get_acl,
1485         .set_acl = ceph_set_acl,
1486         .mknod = ceph_mknod,
1487         .symlink = ceph_symlink,
1488         .mkdir = ceph_mkdir,
1489         .link = ceph_link,
1490         .unlink = ceph_unlink,
1491         .rmdir = ceph_unlink,
1492         .rename = ceph_rename,
1493         .create = ceph_create,
1494         .atomic_open = ceph_atomic_open,
1495 };
1496
1497 const struct inode_operations ceph_snapdir_iops = {
1498         .lookup = ceph_lookup,
1499         .permission = ceph_permission,
1500         .getattr = ceph_getattr,
1501         .mkdir = ceph_mkdir,
1502         .rmdir = ceph_unlink,
1503         .rename = ceph_rename,
1504 };
1505
1506 const struct dentry_operations ceph_dentry_ops = {
1507         .d_revalidate = ceph_d_revalidate,
1508         .d_release = ceph_d_release,
1509         .d_prune = ceph_d_prune,
1510         .d_init = ceph_d_init,
1511 };