ceph: fix dentry reference leak in dcache readdir
[safe/jmp/linux-2.6] / fs / ceph / dir.c
1 #include "ceph_debug.h"
2
3 #include <linux/spinlock.h>
4 #include <linux/fs_struct.h>
5 #include <linux/namei.h>
6 #include <linux/sched.h>
7
8 #include "super.h"
9
10 /*
11  * Directory operations: readdir, lookup, create, link, unlink,
12  * rename, etc.
13  */
14
15 /*
16  * Ceph MDS operations are specified in terms of a base ino and
17  * relative path.  Thus, the client can specify an operation on a
18  * specific inode (e.g., a getattr due to fstat(2)), or as a path
19  * relative to, say, the root directory.
20  *
21  * Normally, we limit ourselves to strict inode ops (no path component)
22  * or dentry operations (a single path component relative to an ino).  The
23  * exception to this is open_root_dentry(), which will open the mount
24  * point by name.
25  */
26
27 const struct inode_operations ceph_dir_iops;
28 const struct file_operations ceph_dir_fops;
29 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         if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP)
42                 dentry->d_op = &ceph_dentry_ops;
43         else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR)
44                 dentry->d_op = &ceph_snapdir_dentry_ops;
45         else
46                 dentry->d_op = &ceph_snap_dentry_ops;
47
48         di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS);
49         if (!di)
50                 return -ENOMEM;          /* oh well */
51
52         spin_lock(&dentry->d_lock);
53         if (dentry->d_fsdata) /* lost a race */
54                 goto out_unlock;
55         di->dentry = dentry;
56         di->lease_session = NULL;
57         dentry->d_fsdata = di;
58         dentry->d_time = jiffies;
59         ceph_dentry_lru_add(dentry);
60 out_unlock:
61         spin_unlock(&dentry->d_lock);
62         return 0;
63 }
64
65
66
67 /*
68  * for readdir, we encode the directory frag and offset within that
69  * frag into f_pos.
70  */
71 static unsigned fpos_frag(loff_t p)
72 {
73         return p >> 32;
74 }
75 static unsigned fpos_off(loff_t p)
76 {
77         return p & 0xffffffff;
78 }
79
80 /*
81  * When possible, we try to satisfy a readdir by peeking at the
82  * dcache.  We make this work by carefully ordering dentries on
83  * d_u.d_child when we initially get results back from the MDS, and
84  * falling back to a "normal" sync readdir if any dentries in the dir
85  * are dropped.
86  *
87  * I_COMPLETE tells indicates we have all dentries in the dir.  It is
88  * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
89  * the MDS if/when the directory is modified).
90  */
91 static int __dcache_readdir(struct file *filp,
92                             void *dirent, filldir_t filldir)
93 {
94         struct inode *inode = filp->f_dentry->d_inode;
95         struct ceph_file_info *fi = filp->private_data;
96         struct dentry *parent = filp->f_dentry;
97         struct inode *dir = parent->d_inode;
98         struct list_head *p;
99         struct dentry *dentry, *last;
100         struct ceph_dentry_info *di;
101         int err = 0;
102
103         /* claim ref on last dentry we returned */
104         last = fi->dentry;
105         fi->dentry = NULL;
106
107         dout("__dcache_readdir %p at %llu (last %p)\n", dir, filp->f_pos,
108              last);
109
110         spin_lock(&dcache_lock);
111
112         /* start at beginning? */
113         if (filp->f_pos == 2 || (last &&
114                                  filp->f_pos < ceph_dentry(last)->offset)) {
115                 if (list_empty(&parent->d_subdirs))
116                         goto out_unlock;
117                 p = parent->d_subdirs.prev;
118                 dout(" initial p %p/%p\n", p->prev, p->next);
119         } else {
120                 p = last->d_u.d_child.prev;
121         }
122
123 more:
124         dentry = list_entry(p, struct dentry, d_u.d_child);
125         di = ceph_dentry(dentry);
126         while (1) {
127                 dout(" p %p/%p d_subdirs %p/%p\n", p->prev, p->next,
128                      parent->d_subdirs.prev, parent->d_subdirs.next);
129                 if (p == &parent->d_subdirs) {
130                         fi->at_end = 1;
131                         goto out_unlock;
132                 }
133                 if (!d_unhashed(dentry) && dentry->d_inode &&
134                     ceph_snap(dentry->d_inode) != CEPH_SNAPDIR &&
135                     ceph_ino(dentry->d_inode) != CEPH_INO_CEPH &&
136                     filp->f_pos <= di->offset)
137                         break;
138                 dout(" skipping %p %.*s at %llu (%llu)%s%s\n", dentry,
139                      dentry->d_name.len, dentry->d_name.name, di->offset,
140                      filp->f_pos, d_unhashed(dentry) ? " unhashed" : "",
141                      !dentry->d_inode ? " null" : "");
142                 p = p->prev;
143                 dentry = list_entry(p, struct dentry, d_u.d_child);
144                 di = ceph_dentry(dentry);
145         }
146
147         atomic_inc(&dentry->d_count);
148         spin_unlock(&dcache_lock);
149         spin_unlock(&inode->i_lock);
150
151         dout(" %llu (%llu) dentry %p %.*s %p\n", di->offset, filp->f_pos,
152              dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
153         filp->f_pos = di->offset;
154         err = filldir(dirent, dentry->d_name.name,
155                       dentry->d_name.len, di->offset,
156                       dentry->d_inode->i_ino,
157                       dentry->d_inode->i_mode >> 12);
158
159         if (last) {
160                 if (err < 0) {
161                         /* remember our position */
162                         fi->dentry = last;
163                         fi->next_offset = di->offset;
164                 } else {
165                         dput(last);
166                 }
167                 last = NULL;
168         }
169
170         spin_lock(&inode->i_lock);
171         spin_lock(&dcache_lock);
172
173         last = dentry;
174
175         if (err < 0)
176                 goto out_unlock;
177
178         p = p->prev;
179         filp->f_pos++;
180
181         /* make sure a dentry wasn't dropped while we didn't have dcache_lock */
182         if ((ceph_inode(dir)->i_ceph_flags & CEPH_I_COMPLETE))
183                 goto more;
184         dout(" lost I_COMPLETE on %p; falling back to mds\n", dir);
185         err = -EAGAIN;
186
187 out_unlock:
188         spin_unlock(&dcache_lock);
189
190         if (last) {
191                 spin_unlock(&inode->i_lock);
192                 dput(last);
193                 spin_lock(&inode->i_lock);
194         }
195
196         return err;
197 }
198
199 /*
200  * make note of the last dentry we read, so we can
201  * continue at the same lexicographical point,
202  * regardless of what dir changes take place on the
203  * server.
204  */
205 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
206                             int len)
207 {
208         kfree(fi->last_name);
209         fi->last_name = kmalloc(len+1, GFP_NOFS);
210         if (!fi->last_name)
211                 return -ENOMEM;
212         memcpy(fi->last_name, name, len);
213         fi->last_name[len] = 0;
214         dout("note_last_dentry '%s'\n", fi->last_name);
215         return 0;
216 }
217
218 static int ceph_readdir(struct file *filp, void *dirent, filldir_t filldir)
219 {
220         struct ceph_file_info *fi = filp->private_data;
221         struct inode *inode = filp->f_dentry->d_inode;
222         struct ceph_inode_info *ci = ceph_inode(inode);
223         struct ceph_client *client = ceph_inode_to_client(inode);
224         struct ceph_mds_client *mdsc = &client->mdsc;
225         unsigned frag = fpos_frag(filp->f_pos);
226         int off = fpos_off(filp->f_pos);
227         int err;
228         u32 ftype;
229         struct ceph_mds_reply_info_parsed *rinfo;
230         const int max_entries = client->mount_args->max_readdir;
231
232         dout("readdir %p filp %p frag %u off %u\n", inode, filp, frag, off);
233         if (fi->at_end)
234                 return 0;
235
236         /* always start with . and .. */
237         if (filp->f_pos == 0) {
238                 /* note dir version at start of readdir so we can tell
239                  * if any dentries get dropped */
240                 fi->dir_release_count = ci->i_release_count;
241
242                 dout("readdir off 0 -> '.'\n");
243                 if (filldir(dirent, ".", 1, ceph_make_fpos(0, 0),
244                             inode->i_ino, inode->i_mode >> 12) < 0)
245                         return 0;
246                 filp->f_pos = 1;
247                 off = 1;
248         }
249         if (filp->f_pos == 1) {
250                 dout("readdir off 1 -> '..'\n");
251                 if (filldir(dirent, "..", 2, ceph_make_fpos(0, 1),
252                             filp->f_dentry->d_parent->d_inode->i_ino,
253                             inode->i_mode >> 12) < 0)
254                         return 0;
255                 filp->f_pos = 2;
256                 off = 2;
257         }
258
259         /* can we use the dcache? */
260         spin_lock(&inode->i_lock);
261         if ((filp->f_pos == 2 || fi->dentry) &&
262             !ceph_test_opt(client, NOASYNCREADDIR) &&
263             (ci->i_ceph_flags & CEPH_I_COMPLETE) &&
264             __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
265                 err = __dcache_readdir(filp, dirent, filldir);
266                 if (err != -EAGAIN) {
267                         spin_unlock(&inode->i_lock);
268                         return err;
269                 }
270         }
271         spin_unlock(&inode->i_lock);
272         if (fi->dentry) {
273                 err = note_last_dentry(fi, fi->dentry->d_name.name,
274                                        fi->dentry->d_name.len);
275                 if (err)
276                         return err;
277                 dput(fi->dentry);
278                 fi->dentry = NULL;
279         }
280
281         /* proceed with a normal readdir */
282
283 more:
284         /* do we have the correct frag content buffered? */
285         if (fi->frag != frag || fi->last_readdir == NULL) {
286                 struct ceph_mds_request *req;
287                 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
288                         CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
289
290                 /* discard old result, if any */
291                 if (fi->last_readdir) {
292                         ceph_mdsc_put_request(fi->last_readdir);
293                         fi->last_readdir = NULL;
294                 }
295
296                 /* requery frag tree, as the frag topology may have changed */
297                 frag = ceph_choose_frag(ceph_inode(inode), frag, NULL, NULL);
298
299                 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
300                      ceph_vinop(inode), frag, fi->last_name);
301                 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
302                 if (IS_ERR(req))
303                         return PTR_ERR(req);
304                 req->r_inode = igrab(inode);
305                 req->r_dentry = dget(filp->f_dentry);
306                 /* hints to request -> mds selection code */
307                 req->r_direct_mode = USE_AUTH_MDS;
308                 req->r_direct_hash = ceph_frag_value(frag);
309                 req->r_direct_is_hash = true;
310                 req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
311                 req->r_readdir_offset = fi->next_offset;
312                 req->r_args.readdir.frag = cpu_to_le32(frag);
313                 req->r_args.readdir.max_entries = cpu_to_le32(max_entries);
314                 req->r_num_caps = max_entries;
315                 err = ceph_mdsc_do_request(mdsc, NULL, req);
316                 if (err < 0) {
317                         ceph_mdsc_put_request(req);
318                         return err;
319                 }
320                 dout("readdir got and parsed readdir result=%d"
321                      " on frag %x, end=%d, complete=%d\n", err, frag,
322                      (int)req->r_reply_info.dir_end,
323                      (int)req->r_reply_info.dir_complete);
324
325                 if (!req->r_did_prepopulate) {
326                         dout("readdir !did_prepopulate");
327                         fi->dir_release_count--;    /* preclude I_COMPLETE */
328                 }
329
330                 /* note next offset and last dentry name */
331                 fi->offset = fi->next_offset;
332                 fi->last_readdir = req;
333
334                 if (req->r_reply_info.dir_end) {
335                         kfree(fi->last_name);
336                         fi->last_name = NULL;
337                         fi->next_offset = 0;
338                 } else {
339                         rinfo = &req->r_reply_info;
340                         err = note_last_dentry(fi,
341                                        rinfo->dir_dname[rinfo->dir_nr-1],
342                                        rinfo->dir_dname_len[rinfo->dir_nr-1]);
343                         if (err)
344                                 return err;
345                         fi->next_offset += rinfo->dir_nr;
346                 }
347         }
348
349         rinfo = &fi->last_readdir->r_reply_info;
350         dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
351              rinfo->dir_nr, off, fi->offset);
352         while (off - fi->offset >= 0 && off - fi->offset < rinfo->dir_nr) {
353                 u64 pos = ceph_make_fpos(frag, off);
354                 struct ceph_mds_reply_inode *in =
355                         rinfo->dir_in[off - fi->offset].in;
356                 dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
357                      off, off - fi->offset, rinfo->dir_nr, pos,
358                      rinfo->dir_dname_len[off - fi->offset],
359                      rinfo->dir_dname[off - fi->offset], in);
360                 BUG_ON(!in);
361                 ftype = le32_to_cpu(in->mode) >> 12;
362                 if (filldir(dirent,
363                             rinfo->dir_dname[off - fi->offset],
364                             rinfo->dir_dname_len[off - fi->offset],
365                             pos,
366                             le64_to_cpu(in->ino),
367                             ftype) < 0) {
368                         dout("filldir stopping us...\n");
369                         return 0;
370                 }
371                 off++;
372                 filp->f_pos = pos + 1;
373         }
374
375         if (fi->last_name) {
376                 ceph_mdsc_put_request(fi->last_readdir);
377                 fi->last_readdir = NULL;
378                 goto more;
379         }
380
381         /* more frags? */
382         if (!ceph_frag_is_rightmost(frag)) {
383                 frag = ceph_frag_next(frag);
384                 off = 0;
385                 filp->f_pos = ceph_make_fpos(frag, off);
386                 dout("readdir next frag is %x\n", frag);
387                 goto more;
388         }
389         fi->at_end = 1;
390
391         /*
392          * if dir_release_count still matches the dir, no dentries
393          * were released during the whole readdir, and we should have
394          * the complete dir contents in our cache.
395          */
396         spin_lock(&inode->i_lock);
397         if (ci->i_release_count == fi->dir_release_count) {
398                 dout(" marking %p complete\n", inode);
399                 ci->i_ceph_flags |= CEPH_I_COMPLETE;
400                 ci->i_max_offset = filp->f_pos;
401         }
402         spin_unlock(&inode->i_lock);
403
404         dout("readdir %p filp %p done.\n", inode, filp);
405         return 0;
406 }
407
408 static void reset_readdir(struct ceph_file_info *fi)
409 {
410         if (fi->last_readdir) {
411                 ceph_mdsc_put_request(fi->last_readdir);
412                 fi->last_readdir = NULL;
413         }
414         kfree(fi->last_name);
415         fi->next_offset = 2;  /* compensate for . and .. */
416         if (fi->dentry) {
417                 dput(fi->dentry);
418                 fi->dentry = NULL;
419         }
420         fi->at_end = 0;
421 }
422
423 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int origin)
424 {
425         struct ceph_file_info *fi = file->private_data;
426         struct inode *inode = file->f_mapping->host;
427         loff_t old_offset = offset;
428         loff_t retval;
429
430         mutex_lock(&inode->i_mutex);
431         switch (origin) {
432         case SEEK_END:
433                 offset += inode->i_size + 2;   /* FIXME */
434                 break;
435         case SEEK_CUR:
436                 offset += file->f_pos;
437         }
438         retval = -EINVAL;
439         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
440                 if (offset != file->f_pos) {
441                         file->f_pos = offset;
442                         file->f_version = 0;
443                         fi->at_end = 0;
444                 }
445                 retval = offset;
446
447                 /*
448                  * discard buffered readdir content on seekdir(0), or
449                  * seek to new frag, or seek prior to current chunk.
450                  */
451                 if (offset == 0 ||
452                     fpos_frag(offset) != fpos_frag(old_offset) ||
453                     fpos_off(offset) < fi->offset) {
454                         dout("dir_llseek dropping %p content\n", file);
455                         reset_readdir(fi);
456                 }
457
458                 /* bump dir_release_count if we did a forward seek */
459                 if (offset > old_offset)
460                         fi->dir_release_count--;
461         }
462         mutex_unlock(&inode->i_mutex);
463         return retval;
464 }
465
466 /*
467  * Process result of a lookup/open request.
468  *
469  * Mainly, make sure we return the final req->r_dentry (if it already
470  * existed) in place of the original VFS-provided dentry when they
471  * differ.
472  *
473  * Gracefully handle the case where the MDS replies with -ENOENT and
474  * no trace (which it may do, at its discretion, e.g., if it doesn't
475  * care to issue a lease on the negative dentry).
476  */
477 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
478                                   struct dentry *dentry, int err)
479 {
480         struct ceph_client *client = ceph_client(dentry->d_sb);
481         struct inode *parent = dentry->d_parent->d_inode;
482
483         /* .snap dir? */
484         if (err == -ENOENT &&
485             ceph_vino(parent).ino != CEPH_INO_ROOT && /* no .snap in root dir */
486             strcmp(dentry->d_name.name,
487                    client->mount_args->snapdir_name) == 0) {
488                 struct inode *inode = ceph_get_snapdir(parent);
489                 dout("ENOENT on snapdir %p '%.*s', linking to snapdir %p\n",
490                      dentry, dentry->d_name.len, dentry->d_name.name, inode);
491                 BUG_ON(!d_unhashed(dentry));
492                 d_add(dentry, inode);
493                 err = 0;
494         }
495
496         if (err == -ENOENT) {
497                 /* no trace? */
498                 err = 0;
499                 if (!req->r_reply_info.head->is_dentry) {
500                         dout("ENOENT and no trace, dentry %p inode %p\n",
501                              dentry, dentry->d_inode);
502                         if (dentry->d_inode) {
503                                 d_drop(dentry);
504                                 err = -ENOENT;
505                         } else {
506                                 d_add(dentry, NULL);
507                         }
508                 }
509         }
510         if (err)
511                 dentry = ERR_PTR(err);
512         else if (dentry != req->r_dentry)
513                 dentry = dget(req->r_dentry);   /* we got spliced */
514         else
515                 dentry = NULL;
516         return dentry;
517 }
518
519 static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
520 {
521         return ceph_ino(inode) == CEPH_INO_ROOT &&
522                 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
523 }
524
525 /*
526  * Look up a single dir entry.  If there is a lookup intent, inform
527  * the MDS so that it gets our 'caps wanted' value in a single op.
528  */
529 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
530                                   struct nameidata *nd)
531 {
532         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
533         struct ceph_mds_client *mdsc = &client->mdsc;
534         struct ceph_mds_request *req;
535         int op;
536         int err;
537
538         dout("lookup %p dentry %p '%.*s'\n",
539              dir, dentry, dentry->d_name.len, dentry->d_name.name);
540
541         if (dentry->d_name.len > NAME_MAX)
542                 return ERR_PTR(-ENAMETOOLONG);
543
544         err = ceph_init_dentry(dentry);
545         if (err < 0)
546                 return ERR_PTR(err);
547
548         /* open (but not create!) intent? */
549         if (nd &&
550             (nd->flags & LOOKUP_OPEN) &&
551             (nd->flags & LOOKUP_CONTINUE) == 0 && /* only open last component */
552             !(nd->intent.open.flags & O_CREAT)) {
553                 int mode = nd->intent.open.create_mode & ~current->fs->umask;
554                 return ceph_lookup_open(dir, dentry, nd, mode, 1);
555         }
556
557         /* can we conclude ENOENT locally? */
558         if (dentry->d_inode == NULL) {
559                 struct ceph_inode_info *ci = ceph_inode(dir);
560                 struct ceph_dentry_info *di = ceph_dentry(dentry);
561
562                 spin_lock(&dir->i_lock);
563                 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
564                 if (strncmp(dentry->d_name.name,
565                             client->mount_args->snapdir_name,
566                             dentry->d_name.len) &&
567                     !is_root_ceph_dentry(dir, dentry) &&
568                     (ci->i_ceph_flags & CEPH_I_COMPLETE) &&
569                     (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
570                         di->offset = ci->i_max_offset++;
571                         spin_unlock(&dir->i_lock);
572                         dout(" dir %p complete, -ENOENT\n", dir);
573                         d_add(dentry, NULL);
574                         di->lease_shared_gen = ci->i_shared_gen;
575                         return NULL;
576                 }
577                 spin_unlock(&dir->i_lock);
578         }
579
580         op = ceph_snap(dir) == CEPH_SNAPDIR ?
581                 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
582         req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
583         if (IS_ERR(req))
584                 return ERR_PTR(PTR_ERR(req));
585         req->r_dentry = dget(dentry);
586         req->r_num_caps = 2;
587         /* we only need inode linkage */
588         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
589         req->r_locked_dir = dir;
590         err = ceph_mdsc_do_request(mdsc, NULL, req);
591         dentry = ceph_finish_lookup(req, dentry, err);
592         ceph_mdsc_put_request(req);  /* will dput(dentry) */
593         dout("lookup result=%p\n", dentry);
594         return dentry;
595 }
596
597 /*
598  * If we do a create but get no trace back from the MDS, follow up with
599  * a lookup (the VFS expects us to link up the provided dentry).
600  */
601 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
602 {
603         struct dentry *result = ceph_lookup(dir, dentry, NULL);
604
605         if (result && !IS_ERR(result)) {
606                 /*
607                  * We created the item, then did a lookup, and found
608                  * it was already linked to another inode we already
609                  * had in our cache (and thus got spliced).  Link our
610                  * dentry to that inode, but don't hash it, just in
611                  * case the VFS wants to dereference it.
612                  */
613                 BUG_ON(!result->d_inode);
614                 d_instantiate(dentry, result->d_inode);
615                 return 0;
616         }
617         return PTR_ERR(result);
618 }
619
620 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
621                       int mode, dev_t rdev)
622 {
623         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
624         struct ceph_mds_client *mdsc = &client->mdsc;
625         struct ceph_mds_request *req;
626         int err;
627
628         if (ceph_snap(dir) != CEPH_NOSNAP)
629                 return -EROFS;
630
631         dout("mknod in dir %p dentry %p mode 0%o rdev %d\n",
632              dir, dentry, mode, rdev);
633         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
634         if (IS_ERR(req)) {
635                 d_drop(dentry);
636                 return PTR_ERR(req);
637         }
638         req->r_dentry = dget(dentry);
639         req->r_num_caps = 2;
640         req->r_locked_dir = dir;
641         req->r_args.mknod.mode = cpu_to_le32(mode);
642         req->r_args.mknod.rdev = cpu_to_le32(rdev);
643         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
644         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
645         err = ceph_mdsc_do_request(mdsc, dir, req);
646         if (!err && !req->r_reply_info.head->is_dentry)
647                 err = ceph_handle_notrace_create(dir, dentry);
648         ceph_mdsc_put_request(req);
649         if (err)
650                 d_drop(dentry);
651         return err;
652 }
653
654 static int ceph_create(struct inode *dir, struct dentry *dentry, int mode,
655                        struct nameidata *nd)
656 {
657         dout("create in dir %p dentry %p name '%.*s'\n",
658              dir, dentry, dentry->d_name.len, dentry->d_name.name);
659
660         if (ceph_snap(dir) != CEPH_NOSNAP)
661                 return -EROFS;
662
663         if (nd) {
664                 BUG_ON((nd->flags & LOOKUP_OPEN) == 0);
665                 dentry = ceph_lookup_open(dir, dentry, nd, mode, 0);
666                 /* hrm, what should i do here if we get aliased? */
667                 if (IS_ERR(dentry))
668                         return PTR_ERR(dentry);
669                 return 0;
670         }
671
672         /* fall back to mknod */
673         return ceph_mknod(dir, dentry, (mode & ~S_IFMT) | S_IFREG, 0);
674 }
675
676 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
677                             const char *dest)
678 {
679         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
680         struct ceph_mds_client *mdsc = &client->mdsc;
681         struct ceph_mds_request *req;
682         int err;
683
684         if (ceph_snap(dir) != CEPH_NOSNAP)
685                 return -EROFS;
686
687         dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
688         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
689         if (IS_ERR(req)) {
690                 d_drop(dentry);
691                 return PTR_ERR(req);
692         }
693         req->r_dentry = dget(dentry);
694         req->r_num_caps = 2;
695         req->r_path2 = kstrdup(dest, GFP_NOFS);
696         req->r_locked_dir = dir;
697         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
698         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
699         err = ceph_mdsc_do_request(mdsc, dir, req);
700         if (!err && !req->r_reply_info.head->is_dentry)
701                 err = ceph_handle_notrace_create(dir, dentry);
702         ceph_mdsc_put_request(req);
703         if (err)
704                 d_drop(dentry);
705         return err;
706 }
707
708 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, int mode)
709 {
710         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
711         struct ceph_mds_client *mdsc = &client->mdsc;
712         struct ceph_mds_request *req;
713         int err = -EROFS;
714         int op;
715
716         if (ceph_snap(dir) == CEPH_SNAPDIR) {
717                 /* mkdir .snap/foo is a MKSNAP */
718                 op = CEPH_MDS_OP_MKSNAP;
719                 dout("mksnap dir %p snap '%.*s' dn %p\n", dir,
720                      dentry->d_name.len, dentry->d_name.name, dentry);
721         } else if (ceph_snap(dir) == CEPH_NOSNAP) {
722                 dout("mkdir dir %p dn %p mode 0%o\n", dir, dentry, mode);
723                 op = CEPH_MDS_OP_MKDIR;
724         } else {
725                 goto out;
726         }
727         req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
728         if (IS_ERR(req)) {
729                 err = PTR_ERR(req);
730                 goto out;
731         }
732
733         req->r_dentry = dget(dentry);
734         req->r_num_caps = 2;
735         req->r_locked_dir = dir;
736         req->r_args.mkdir.mode = cpu_to_le32(mode);
737         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
738         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
739         err = ceph_mdsc_do_request(mdsc, dir, req);
740         if (!err && !req->r_reply_info.head->is_dentry)
741                 err = ceph_handle_notrace_create(dir, dentry);
742         ceph_mdsc_put_request(req);
743 out:
744         if (err < 0)
745                 d_drop(dentry);
746         return err;
747 }
748
749 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
750                      struct dentry *dentry)
751 {
752         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
753         struct ceph_mds_client *mdsc = &client->mdsc;
754         struct ceph_mds_request *req;
755         int err;
756
757         if (ceph_snap(dir) != CEPH_NOSNAP)
758                 return -EROFS;
759
760         dout("link in dir %p old_dentry %p dentry %p\n", dir,
761              old_dentry, dentry);
762         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
763         if (IS_ERR(req)) {
764                 d_drop(dentry);
765                 return PTR_ERR(req);
766         }
767         req->r_dentry = dget(dentry);
768         req->r_num_caps = 2;
769         req->r_old_dentry = dget(old_dentry); /* or inode? hrm. */
770         req->r_locked_dir = dir;
771         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
772         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
773         err = ceph_mdsc_do_request(mdsc, dir, req);
774         if (err)
775                 d_drop(dentry);
776         else if (!req->r_reply_info.head->is_dentry)
777                 d_instantiate(dentry, igrab(old_dentry->d_inode));
778         ceph_mdsc_put_request(req);
779         return err;
780 }
781
782 /*
783  * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps.  If it
784  * looks like the link count will hit 0, drop any other caps (other
785  * than PIN) we don't specifically want (due to the file still being
786  * open).
787  */
788 static int drop_caps_for_unlink(struct inode *inode)
789 {
790         struct ceph_inode_info *ci = ceph_inode(inode);
791         int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
792
793         spin_lock(&inode->i_lock);
794         if (inode->i_nlink == 1) {
795                 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
796                 ci->i_ceph_flags |= CEPH_I_NODELAY;
797         }
798         spin_unlock(&inode->i_lock);
799         return drop;
800 }
801
802 /*
803  * rmdir and unlink are differ only by the metadata op code
804  */
805 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
806 {
807         struct ceph_client *client = ceph_sb_to_client(dir->i_sb);
808         struct ceph_mds_client *mdsc = &client->mdsc;
809         struct inode *inode = dentry->d_inode;
810         struct ceph_mds_request *req;
811         int err = -EROFS;
812         int op;
813
814         if (ceph_snap(dir) == CEPH_SNAPDIR) {
815                 /* rmdir .snap/foo is RMSNAP */
816                 dout("rmsnap dir %p '%.*s' dn %p\n", dir, dentry->d_name.len,
817                      dentry->d_name.name, dentry);
818                 op = CEPH_MDS_OP_RMSNAP;
819         } else if (ceph_snap(dir) == CEPH_NOSNAP) {
820                 dout("unlink/rmdir dir %p dn %p inode %p\n",
821                      dir, dentry, inode);
822                 op = ((dentry->d_inode->i_mode & S_IFMT) == S_IFDIR) ?
823                         CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
824         } else
825                 goto out;
826         req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
827         if (IS_ERR(req)) {
828                 err = PTR_ERR(req);
829                 goto out;
830         }
831         req->r_dentry = dget(dentry);
832         req->r_num_caps = 2;
833         req->r_locked_dir = dir;
834         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
835         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
836         req->r_inode_drop = drop_caps_for_unlink(inode);
837         err = ceph_mdsc_do_request(mdsc, dir, req);
838         if (!err && !req->r_reply_info.head->is_dentry)
839                 d_delete(dentry);
840         ceph_mdsc_put_request(req);
841 out:
842         return err;
843 }
844
845 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
846                        struct inode *new_dir, struct dentry *new_dentry)
847 {
848         struct ceph_client *client = ceph_sb_to_client(old_dir->i_sb);
849         struct ceph_mds_client *mdsc = &client->mdsc;
850         struct ceph_mds_request *req;
851         int err;
852
853         if (ceph_snap(old_dir) != ceph_snap(new_dir))
854                 return -EXDEV;
855         if (ceph_snap(old_dir) != CEPH_NOSNAP ||
856             ceph_snap(new_dir) != CEPH_NOSNAP)
857                 return -EROFS;
858         dout("rename dir %p dentry %p to dir %p dentry %p\n",
859              old_dir, old_dentry, new_dir, new_dentry);
860         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS);
861         if (IS_ERR(req))
862                 return PTR_ERR(req);
863         req->r_dentry = dget(new_dentry);
864         req->r_num_caps = 2;
865         req->r_old_dentry = dget(old_dentry);
866         req->r_locked_dir = new_dir;
867         req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
868         req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
869         req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
870         req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
871         /* release LINK_RDCACHE on source inode (mds will lock it) */
872         req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
873         if (new_dentry->d_inode)
874                 req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode);
875         err = ceph_mdsc_do_request(mdsc, old_dir, req);
876         if (!err && !req->r_reply_info.head->is_dentry) {
877                 /*
878                  * Normally d_move() is done by fill_trace (called by
879                  * do_request, above).  If there is no trace, we need
880                  * to do it here.
881                  */
882                 d_move(old_dentry, new_dentry);
883         }
884         ceph_mdsc_put_request(req);
885         return err;
886 }
887
888
889 /*
890  * Check if dentry lease is valid.  If not, delete the lease.  Try to
891  * renew if the least is more than half up.
892  */
893 static int dentry_lease_is_valid(struct dentry *dentry)
894 {
895         struct ceph_dentry_info *di;
896         struct ceph_mds_session *s;
897         int valid = 0;
898         u32 gen;
899         unsigned long ttl;
900         struct ceph_mds_session *session = NULL;
901         struct inode *dir = NULL;
902         u32 seq = 0;
903
904         spin_lock(&dentry->d_lock);
905         di = ceph_dentry(dentry);
906         if (di && di->lease_session) {
907                 s = di->lease_session;
908                 spin_lock(&s->s_cap_lock);
909                 gen = s->s_cap_gen;
910                 ttl = s->s_cap_ttl;
911                 spin_unlock(&s->s_cap_lock);
912
913                 if (di->lease_gen == gen &&
914                     time_before(jiffies, dentry->d_time) &&
915                     time_before(jiffies, ttl)) {
916                         valid = 1;
917                         if (di->lease_renew_after &&
918                             time_after(jiffies, di->lease_renew_after)) {
919                                 /* we should renew */
920                                 dir = dentry->d_parent->d_inode;
921                                 session = ceph_get_mds_session(s);
922                                 seq = di->lease_seq;
923                                 di->lease_renew_after = 0;
924                                 di->lease_renew_from = jiffies;
925                         }
926                 }
927         }
928         spin_unlock(&dentry->d_lock);
929
930         if (session) {
931                 ceph_mdsc_lease_send_msg(session, dir, dentry,
932                                          CEPH_MDS_LEASE_RENEW, seq);
933                 ceph_put_mds_session(session);
934         }
935         dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
936         return valid;
937 }
938
939 /*
940  * Check if directory-wide content lease/cap is valid.
941  */
942 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
943 {
944         struct ceph_inode_info *ci = ceph_inode(dir);
945         struct ceph_dentry_info *di = ceph_dentry(dentry);
946         int valid = 0;
947
948         spin_lock(&dir->i_lock);
949         if (ci->i_shared_gen == di->lease_shared_gen)
950                 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
951         spin_unlock(&dir->i_lock);
952         dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
953              dir, (unsigned)ci->i_shared_gen, dentry,
954              (unsigned)di->lease_shared_gen, valid);
955         return valid;
956 }
957
958 /*
959  * Check if cached dentry can be trusted.
960  */
961 static int ceph_d_revalidate(struct dentry *dentry, struct nameidata *nd)
962 {
963         struct inode *dir = dentry->d_parent->d_inode;
964
965         dout("d_revalidate %p '%.*s' inode %p\n", dentry,
966              dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
967
968         /* always trust cached snapped dentries, snapdir dentry */
969         if (ceph_snap(dir) != CEPH_NOSNAP) {
970                 dout("d_revalidate %p '%.*s' inode %p is SNAPPED\n", dentry,
971                      dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
972                 goto out_touch;
973         }
974         if (dentry->d_inode && ceph_snap(dentry->d_inode) == CEPH_SNAPDIR)
975                 goto out_touch;
976
977         if (dentry_lease_is_valid(dentry) ||
978             dir_lease_is_valid(dir, dentry))
979                 goto out_touch;
980
981         dout("d_revalidate %p invalid\n", dentry);
982         d_drop(dentry);
983         return 0;
984 out_touch:
985         ceph_dentry_lru_touch(dentry);
986         return 1;
987 }
988
989 /*
990  * When a dentry is released, clear the dir I_COMPLETE if it was part
991  * of the current dir gen.
992  */
993 static void ceph_dentry_release(struct dentry *dentry)
994 {
995         struct ceph_dentry_info *di = ceph_dentry(dentry);
996         struct inode *parent_inode = dentry->d_parent->d_inode;
997
998         if (parent_inode) {
999                 struct ceph_inode_info *ci = ceph_inode(parent_inode);
1000
1001                 spin_lock(&parent_inode->i_lock);
1002                 if (ci->i_shared_gen == di->lease_shared_gen) {
1003                         dout(" clearing %p complete (d_release)\n",
1004                              parent_inode);
1005                         ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1006                         ci->i_release_count++;
1007                 }
1008                 spin_unlock(&parent_inode->i_lock);
1009         }
1010         if (di) {
1011                 ceph_dentry_lru_del(dentry);
1012                 if (di->lease_session)
1013                         ceph_put_mds_session(di->lease_session);
1014                 kmem_cache_free(ceph_dentry_cachep, di);
1015                 dentry->d_fsdata = NULL;
1016         }
1017 }
1018
1019 static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1020                                           struct nameidata *nd)
1021 {
1022         /*
1023          * Eventually, we'll want to revalidate snapped metadata
1024          * too... probably...
1025          */
1026         return 1;
1027 }
1028
1029
1030
1031 /*
1032  * read() on a dir.  This weird interface hack only works if mounted
1033  * with '-o dirstat'.
1034  */
1035 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1036                              loff_t *ppos)
1037 {
1038         struct ceph_file_info *cf = file->private_data;
1039         struct inode *inode = file->f_dentry->d_inode;
1040         struct ceph_inode_info *ci = ceph_inode(inode);
1041         int left;
1042
1043         if (!ceph_test_opt(ceph_client(inode->i_sb), DIRSTAT))
1044                 return -EISDIR;
1045
1046         if (!cf->dir_info) {
1047                 cf->dir_info = kmalloc(1024, GFP_NOFS);
1048                 if (!cf->dir_info)
1049                         return -ENOMEM;
1050                 cf->dir_info_len =
1051                         sprintf(cf->dir_info,
1052                                 "entries:   %20lld\n"
1053                                 " files:    %20lld\n"
1054                                 " subdirs:  %20lld\n"
1055                                 "rentries:  %20lld\n"
1056                                 " rfiles:   %20lld\n"
1057                                 " rsubdirs: %20lld\n"
1058                                 "rbytes:    %20lld\n"
1059                                 "rctime:    %10ld.%09ld\n",
1060                                 ci->i_files + ci->i_subdirs,
1061                                 ci->i_files,
1062                                 ci->i_subdirs,
1063                                 ci->i_rfiles + ci->i_rsubdirs,
1064                                 ci->i_rfiles,
1065                                 ci->i_rsubdirs,
1066                                 ci->i_rbytes,
1067                                 (long)ci->i_rctime.tv_sec,
1068                                 (long)ci->i_rctime.tv_nsec);
1069         }
1070
1071         if (*ppos >= cf->dir_info_len)
1072                 return 0;
1073         size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1074         left = copy_to_user(buf, cf->dir_info + *ppos, size);
1075         if (left == size)
1076                 return -EFAULT;
1077         *ppos += (size - left);
1078         return size - left;
1079 }
1080
1081 /*
1082  * an fsync() on a dir will wait for any uncommitted directory
1083  * operations to commit.
1084  */
1085 static int ceph_dir_fsync(struct file *file, struct dentry *dentry,
1086                           int datasync)
1087 {
1088         struct inode *inode = dentry->d_inode;
1089         struct ceph_inode_info *ci = ceph_inode(inode);
1090         struct list_head *head = &ci->i_unsafe_dirops;
1091         struct ceph_mds_request *req;
1092         u64 last_tid;
1093         int ret = 0;
1094
1095         dout("dir_fsync %p\n", inode);
1096         spin_lock(&ci->i_unsafe_lock);
1097         if (list_empty(head))
1098                 goto out;
1099
1100         req = list_entry(head->prev,
1101                          struct ceph_mds_request, r_unsafe_dir_item);
1102         last_tid = req->r_tid;
1103
1104         do {
1105                 ceph_mdsc_get_request(req);
1106                 spin_unlock(&ci->i_unsafe_lock);
1107                 dout("dir_fsync %p wait on tid %llu (until %llu)\n",
1108                      inode, req->r_tid, last_tid);
1109                 if (req->r_timeout) {
1110                         ret = wait_for_completion_timeout(
1111                                 &req->r_safe_completion, req->r_timeout);
1112                         if (ret > 0)
1113                                 ret = 0;
1114                         else if (ret == 0)
1115                                 ret = -EIO;  /* timed out */
1116                 } else {
1117                         wait_for_completion(&req->r_safe_completion);
1118                 }
1119                 spin_lock(&ci->i_unsafe_lock);
1120                 ceph_mdsc_put_request(req);
1121
1122                 if (ret || list_empty(head))
1123                         break;
1124                 req = list_entry(head->next,
1125                                  struct ceph_mds_request, r_unsafe_dir_item);
1126         } while (req->r_tid < last_tid);
1127 out:
1128         spin_unlock(&ci->i_unsafe_lock);
1129         return ret;
1130 }
1131
1132 /*
1133  * We maintain a private dentry LRU.
1134  *
1135  * FIXME: this needs to be changed to a per-mds lru to be useful.
1136  */
1137 void ceph_dentry_lru_add(struct dentry *dn)
1138 {
1139         struct ceph_dentry_info *di = ceph_dentry(dn);
1140         struct ceph_mds_client *mdsc;
1141
1142         dout("dentry_lru_add %p %p '%.*s'\n", di, dn,
1143              dn->d_name.len, dn->d_name.name);
1144         if (di) {
1145                 mdsc = &ceph_client(dn->d_sb)->mdsc;
1146                 spin_lock(&mdsc->dentry_lru_lock);
1147                 list_add_tail(&di->lru, &mdsc->dentry_lru);
1148                 mdsc->num_dentry++;
1149                 spin_unlock(&mdsc->dentry_lru_lock);
1150         }
1151 }
1152
1153 void ceph_dentry_lru_touch(struct dentry *dn)
1154 {
1155         struct ceph_dentry_info *di = ceph_dentry(dn);
1156         struct ceph_mds_client *mdsc;
1157
1158         dout("dentry_lru_touch %p %p '%.*s'\n", di, dn,
1159              dn->d_name.len, dn->d_name.name);
1160         if (di) {
1161                 mdsc = &ceph_client(dn->d_sb)->mdsc;
1162                 spin_lock(&mdsc->dentry_lru_lock);
1163                 list_move_tail(&di->lru, &mdsc->dentry_lru);
1164                 spin_unlock(&mdsc->dentry_lru_lock);
1165         }
1166 }
1167
1168 void ceph_dentry_lru_del(struct dentry *dn)
1169 {
1170         struct ceph_dentry_info *di = ceph_dentry(dn);
1171         struct ceph_mds_client *mdsc;
1172
1173         dout("dentry_lru_del %p %p '%.*s'\n", di, dn,
1174              dn->d_name.len, dn->d_name.name);
1175         if (di) {
1176                 mdsc = &ceph_client(dn->d_sb)->mdsc;
1177                 spin_lock(&mdsc->dentry_lru_lock);
1178                 list_del_init(&di->lru);
1179                 mdsc->num_dentry--;
1180                 spin_unlock(&mdsc->dentry_lru_lock);
1181         }
1182 }
1183
1184 const struct file_operations ceph_dir_fops = {
1185         .read = ceph_read_dir,
1186         .readdir = ceph_readdir,
1187         .llseek = ceph_dir_llseek,
1188         .open = ceph_open,
1189         .release = ceph_release,
1190         .unlocked_ioctl = ceph_ioctl,
1191         .fsync = ceph_dir_fsync,
1192 };
1193
1194 const struct inode_operations ceph_dir_iops = {
1195         .lookup = ceph_lookup,
1196         .permission = ceph_permission,
1197         .getattr = ceph_getattr,
1198         .setattr = ceph_setattr,
1199         .setxattr = ceph_setxattr,
1200         .getxattr = ceph_getxattr,
1201         .listxattr = ceph_listxattr,
1202         .removexattr = ceph_removexattr,
1203         .mknod = ceph_mknod,
1204         .symlink = ceph_symlink,
1205         .mkdir = ceph_mkdir,
1206         .link = ceph_link,
1207         .unlink = ceph_unlink,
1208         .rmdir = ceph_unlink,
1209         .rename = ceph_rename,
1210         .create = ceph_create,
1211 };
1212
1213 struct dentry_operations ceph_dentry_ops = {
1214         .d_revalidate = ceph_d_revalidate,
1215         .d_release = ceph_dentry_release,
1216 };
1217
1218 struct dentry_operations ceph_snapdir_dentry_ops = {
1219         .d_revalidate = ceph_snapdir_d_revalidate,
1220 };
1221
1222 struct dentry_operations ceph_snap_dentry_ops = {
1223 };