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