ceph: cleanup async writeback, truncation, invalidate helpers
[safe/jmp/linux-2.6] / fs / ceph / inode.c
1 #include "ceph_debug.h"
2
3 #include <linux/module.h>
4 #include <linux/fs.h>
5 #include <linux/smp_lock.h>
6 #include <linux/slab.h>
7 #include <linux/string.h>
8 #include <linux/uaccess.h>
9 #include <linux/kernel.h>
10 #include <linux/namei.h>
11 #include <linux/writeback.h>
12 #include <linux/vmalloc.h>
13
14 #include "super.h"
15 #include "decode.h"
16
17 /*
18  * Ceph inode operations
19  *
20  * Implement basic inode helpers (get, alloc) and inode ops (getattr,
21  * setattr, etc.), xattr helpers, and helpers for assimilating
22  * metadata returned by the MDS into our cache.
23  *
24  * Also define helpers for doing asynchronous writeback, invalidation,
25  * and truncation for the benefit of those who can't afford to block
26  * (typically because they are in the message handler path).
27  */
28
29 static const struct inode_operations ceph_symlink_iops;
30
31 static void ceph_invalidate_work(struct work_struct *work);
32 static void ceph_writeback_work(struct work_struct *work);
33 static void ceph_vmtruncate_work(struct work_struct *work);
34
35 /*
36  * find or create an inode, given the ceph ino number
37  */
38 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
39 {
40         struct inode *inode;
41         ino_t t = ceph_vino_to_ino(vino);
42
43         inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
44         if (inode == NULL)
45                 return ERR_PTR(-ENOMEM);
46         if (inode->i_state & I_NEW) {
47                 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
48                      inode, ceph_vinop(inode), (u64)inode->i_ino);
49                 unlock_new_inode(inode);
50         }
51
52         dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
53              vino.snap, inode);
54         return inode;
55 }
56
57 /*
58  * get/constuct snapdir inode for a given directory
59  */
60 struct inode *ceph_get_snapdir(struct inode *parent)
61 {
62         struct ceph_vino vino = {
63                 .ino = ceph_ino(parent),
64                 .snap = CEPH_SNAPDIR,
65         };
66         struct inode *inode = ceph_get_inode(parent->i_sb, vino);
67         struct ceph_inode_info *ci = ceph_inode(inode);
68
69         BUG_ON(!S_ISDIR(parent->i_mode));
70         if (IS_ERR(inode))
71                 return ERR_PTR(PTR_ERR(inode));
72         inode->i_mode = parent->i_mode;
73         inode->i_uid = parent->i_uid;
74         inode->i_gid = parent->i_gid;
75         inode->i_op = &ceph_dir_iops;
76         inode->i_fop = &ceph_dir_fops;
77         ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
78         ci->i_rbytes = 0;
79         return inode;
80 }
81
82 const struct inode_operations ceph_file_iops = {
83         .permission = ceph_permission,
84         .setattr = ceph_setattr,
85         .getattr = ceph_getattr,
86         .setxattr = ceph_setxattr,
87         .getxattr = ceph_getxattr,
88         .listxattr = ceph_listxattr,
89         .removexattr = ceph_removexattr,
90 };
91
92
93 /*
94  * We use a 'frag tree' to keep track of the MDS's directory fragments
95  * for a given inode (usually there is just a single fragment).  We
96  * need to know when a child frag is delegated to a new MDS, or when
97  * it is flagged as replicated, so we can direct our requests
98  * accordingly.
99  */
100
101 /*
102  * find/create a frag in the tree
103  */
104 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
105                                                     u32 f)
106 {
107         struct rb_node **p;
108         struct rb_node *parent = NULL;
109         struct ceph_inode_frag *frag;
110         int c;
111
112         p = &ci->i_fragtree.rb_node;
113         while (*p) {
114                 parent = *p;
115                 frag = rb_entry(parent, struct ceph_inode_frag, node);
116                 c = ceph_frag_compare(f, frag->frag);
117                 if (c < 0)
118                         p = &(*p)->rb_left;
119                 else if (c > 0)
120                         p = &(*p)->rb_right;
121                 else
122                         return frag;
123         }
124
125         frag = kmalloc(sizeof(*frag), GFP_NOFS);
126         if (!frag) {
127                 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
128                        "frag %x\n", &ci->vfs_inode,
129                        ceph_vinop(&ci->vfs_inode), f);
130                 return ERR_PTR(-ENOMEM);
131         }
132         frag->frag = f;
133         frag->split_by = 0;
134         frag->mds = -1;
135         frag->ndist = 0;
136
137         rb_link_node(&frag->node, parent, p);
138         rb_insert_color(&frag->node, &ci->i_fragtree);
139
140         dout("get_or_create_frag added %llx.%llx frag %x\n",
141              ceph_vinop(&ci->vfs_inode), f);
142         return frag;
143 }
144
145 /*
146  * find a specific frag @f
147  */
148 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
149 {
150         struct rb_node *n = ci->i_fragtree.rb_node;
151
152         while (n) {
153                 struct ceph_inode_frag *frag =
154                         rb_entry(n, struct ceph_inode_frag, node);
155                 int c = ceph_frag_compare(f, frag->frag);
156                 if (c < 0)
157                         n = n->rb_left;
158                 else if (c > 0)
159                         n = n->rb_right;
160                 else
161                         return frag;
162         }
163         return NULL;
164 }
165
166 /*
167  * Choose frag containing the given value @v.  If @pfrag is
168  * specified, copy the frag delegation info to the caller if
169  * it is present.
170  */
171 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
172                      struct ceph_inode_frag *pfrag,
173                      int *found)
174 {
175         u32 t = ceph_frag_make(0, 0);
176         struct ceph_inode_frag *frag;
177         unsigned nway, i;
178         u32 n;
179
180         if (found)
181                 *found = 0;
182
183         mutex_lock(&ci->i_fragtree_mutex);
184         while (1) {
185                 WARN_ON(!ceph_frag_contains_value(t, v));
186                 frag = __ceph_find_frag(ci, t);
187                 if (!frag)
188                         break; /* t is a leaf */
189                 if (frag->split_by == 0) {
190                         if (pfrag)
191                                 memcpy(pfrag, frag, sizeof(*pfrag));
192                         if (found)
193                                 *found = 1;
194                         break;
195                 }
196
197                 /* choose child */
198                 nway = 1 << frag->split_by;
199                 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
200                      frag->split_by, nway);
201                 for (i = 0; i < nway; i++) {
202                         n = ceph_frag_make_child(t, frag->split_by, i);
203                         if (ceph_frag_contains_value(n, v)) {
204                                 t = n;
205                                 break;
206                         }
207                 }
208                 BUG_ON(i == nway);
209         }
210         dout("choose_frag(%x) = %x\n", v, t);
211
212         mutex_unlock(&ci->i_fragtree_mutex);
213         return t;
214 }
215
216 /*
217  * Process dirfrag (delegation) info from the mds.  Include leaf
218  * fragment in tree ONLY if ndist > 0.  Otherwise, only
219  * branches/splits are included in i_fragtree)
220  */
221 static int ceph_fill_dirfrag(struct inode *inode,
222                              struct ceph_mds_reply_dirfrag *dirinfo)
223 {
224         struct ceph_inode_info *ci = ceph_inode(inode);
225         struct ceph_inode_frag *frag;
226         u32 id = le32_to_cpu(dirinfo->frag);
227         int mds = le32_to_cpu(dirinfo->auth);
228         int ndist = le32_to_cpu(dirinfo->ndist);
229         int i;
230         int err = 0;
231
232         mutex_lock(&ci->i_fragtree_mutex);
233         if (ndist == 0) {
234                 /* no delegation info needed. */
235                 frag = __ceph_find_frag(ci, id);
236                 if (!frag)
237                         goto out;
238                 if (frag->split_by == 0) {
239                         /* tree leaf, remove */
240                         dout("fill_dirfrag removed %llx.%llx frag %x"
241                              " (no ref)\n", ceph_vinop(inode), id);
242                         rb_erase(&frag->node, &ci->i_fragtree);
243                         kfree(frag);
244                 } else {
245                         /* tree branch, keep and clear */
246                         dout("fill_dirfrag cleared %llx.%llx frag %x"
247                              " referral\n", ceph_vinop(inode), id);
248                         frag->mds = -1;
249                         frag->ndist = 0;
250                 }
251                 goto out;
252         }
253
254
255         /* find/add this frag to store mds delegation info */
256         frag = __get_or_create_frag(ci, id);
257         if (IS_ERR(frag)) {
258                 /* this is not the end of the world; we can continue
259                    with bad/inaccurate delegation info */
260                 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
261                        ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
262                 err = -ENOMEM;
263                 goto out;
264         }
265
266         frag->mds = mds;
267         frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
268         for (i = 0; i < frag->ndist; i++)
269                 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
270         dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
271              ceph_vinop(inode), frag->frag, frag->ndist);
272
273 out:
274         mutex_unlock(&ci->i_fragtree_mutex);
275         return err;
276 }
277
278
279 /*
280  * initialize a newly allocated inode.
281  */
282 struct inode *ceph_alloc_inode(struct super_block *sb)
283 {
284         struct ceph_inode_info *ci;
285         int i;
286
287         ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
288         if (!ci)
289                 return NULL;
290
291         dout("alloc_inode %p\n", &ci->vfs_inode);
292
293         ci->i_version = 0;
294         ci->i_time_warp_seq = 0;
295         ci->i_ceph_flags = 0;
296         ci->i_release_count = 0;
297         ci->i_symlink = NULL;
298
299         ci->i_fragtree = RB_ROOT;
300         mutex_init(&ci->i_fragtree_mutex);
301
302         ci->i_xattrs.blob = NULL;
303         ci->i_xattrs.prealloc_blob = NULL;
304         ci->i_xattrs.dirty = false;
305         ci->i_xattrs.index = RB_ROOT;
306         ci->i_xattrs.count = 0;
307         ci->i_xattrs.names_size = 0;
308         ci->i_xattrs.vals_size = 0;
309         ci->i_xattrs.version = 0;
310         ci->i_xattrs.index_version = 0;
311
312         ci->i_caps = RB_ROOT;
313         ci->i_auth_cap = NULL;
314         ci->i_dirty_caps = 0;
315         ci->i_flushing_caps = 0;
316         INIT_LIST_HEAD(&ci->i_dirty_item);
317         INIT_LIST_HEAD(&ci->i_flushing_item);
318         ci->i_cap_flush_seq = 0;
319         ci->i_cap_flush_last_tid = 0;
320         memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
321         init_waitqueue_head(&ci->i_cap_wq);
322         ci->i_hold_caps_min = 0;
323         ci->i_hold_caps_max = 0;
324         INIT_LIST_HEAD(&ci->i_cap_delay_list);
325         ci->i_cap_exporting_mds = 0;
326         ci->i_cap_exporting_mseq = 0;
327         ci->i_cap_exporting_issued = 0;
328         INIT_LIST_HEAD(&ci->i_cap_snaps);
329         ci->i_head_snapc = NULL;
330         ci->i_snap_caps = 0;
331
332         for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
333                 ci->i_nr_by_mode[i] = 0;
334
335         ci->i_truncate_seq = 0;
336         ci->i_truncate_size = 0;
337         ci->i_truncate_pending = 0;
338
339         ci->i_max_size = 0;
340         ci->i_reported_size = 0;
341         ci->i_wanted_max_size = 0;
342         ci->i_requested_max_size = 0;
343
344         ci->i_pin_ref = 0;
345         ci->i_rd_ref = 0;
346         ci->i_rdcache_ref = 0;
347         ci->i_wr_ref = 0;
348         ci->i_wrbuffer_ref = 0;
349         ci->i_wrbuffer_ref_head = 0;
350         ci->i_shared_gen = 0;
351         ci->i_rdcache_gen = 0;
352         ci->i_rdcache_revoking = 0;
353
354         INIT_LIST_HEAD(&ci->i_unsafe_writes);
355         INIT_LIST_HEAD(&ci->i_unsafe_dirops);
356         spin_lock_init(&ci->i_unsafe_lock);
357
358         ci->i_snap_realm = NULL;
359         INIT_LIST_HEAD(&ci->i_snap_realm_item);
360         INIT_LIST_HEAD(&ci->i_snap_flush_item);
361
362         INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
363         INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
364
365         INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
366
367         return &ci->vfs_inode;
368 }
369
370 void ceph_destroy_inode(struct inode *inode)
371 {
372         struct ceph_inode_info *ci = ceph_inode(inode);
373         struct ceph_inode_frag *frag;
374         struct rb_node *n;
375
376         dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
377
378         ceph_queue_caps_release(inode);
379
380         kfree(ci->i_symlink);
381         while ((n = rb_first(&ci->i_fragtree)) != NULL) {
382                 frag = rb_entry(n, struct ceph_inode_frag, node);
383                 rb_erase(n, &ci->i_fragtree);
384                 kfree(frag);
385         }
386
387         __ceph_destroy_xattrs(ci);
388         if (ci->i_xattrs.blob)
389                 ceph_buffer_put(ci->i_xattrs.blob);
390         if (ci->i_xattrs.prealloc_blob)
391                 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
392
393         kmem_cache_free(ceph_inode_cachep, ci);
394 }
395
396
397 /*
398  * Helpers to fill in size, ctime, mtime, and atime.  We have to be
399  * careful because either the client or MDS may have more up to date
400  * info, depending on which capabilities are held, and whether
401  * time_warp_seq or truncate_seq have increased.  (Ordinarily, mtime
402  * and size are monotonically increasing, except when utimes() or
403  * truncate() increments the corresponding _seq values.)
404  */
405 int ceph_fill_file_size(struct inode *inode, int issued,
406                         u32 truncate_seq, u64 truncate_size, u64 size)
407 {
408         struct ceph_inode_info *ci = ceph_inode(inode);
409         int queue_trunc = 0;
410
411         if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
412             (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
413                 dout("size %lld -> %llu\n", inode->i_size, size);
414                 inode->i_size = size;
415                 inode->i_blocks = (size + (1<<9) - 1) >> 9;
416                 ci->i_reported_size = size;
417                 if (truncate_seq != ci->i_truncate_seq) {
418                         dout("truncate_seq %u -> %u\n",
419                              ci->i_truncate_seq, truncate_seq);
420                         ci->i_truncate_seq = truncate_seq;
421                         /*
422                          * If we hold relevant caps, or in the case where we're
423                          * not the only client referencing this file and we
424                          * don't hold those caps, then we need to check whether
425                          * the file is either opened or mmaped
426                          */
427                         if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
428                                       CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
429                                       CEPH_CAP_FILE_EXCL)) ||
430                             mapping_mapped(inode->i_mapping) ||
431                             __ceph_caps_file_wanted(ci)) {
432                                 ci->i_truncate_pending++;
433                                 queue_trunc = 1;
434                         }
435                 }
436         }
437         if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
438             ci->i_truncate_size != truncate_size) {
439                 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
440                      truncate_size);
441                 ci->i_truncate_size = truncate_size;
442         }
443         return queue_trunc;
444 }
445
446 void ceph_fill_file_time(struct inode *inode, int issued,
447                          u64 time_warp_seq, struct timespec *ctime,
448                          struct timespec *mtime, struct timespec *atime)
449 {
450         struct ceph_inode_info *ci = ceph_inode(inode);
451         int warn = 0;
452
453         if (issued & (CEPH_CAP_FILE_EXCL|
454                       CEPH_CAP_FILE_WR|
455                       CEPH_CAP_FILE_BUFFER)) {
456                 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
457                         dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
458                              inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
459                              ctime->tv_sec, ctime->tv_nsec);
460                         inode->i_ctime = *ctime;
461                 }
462                 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
463                         /* the MDS did a utimes() */
464                         dout("mtime %ld.%09ld -> %ld.%09ld "
465                              "tw %d -> %d\n",
466                              inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
467                              mtime->tv_sec, mtime->tv_nsec,
468                              ci->i_time_warp_seq, (int)time_warp_seq);
469
470                         inode->i_mtime = *mtime;
471                         inode->i_atime = *atime;
472                         ci->i_time_warp_seq = time_warp_seq;
473                 } else if (time_warp_seq == ci->i_time_warp_seq) {
474                         /* nobody did utimes(); take the max */
475                         if (timespec_compare(mtime, &inode->i_mtime) > 0) {
476                                 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
477                                      inode->i_mtime.tv_sec,
478                                      inode->i_mtime.tv_nsec,
479                                      mtime->tv_sec, mtime->tv_nsec);
480                                 inode->i_mtime = *mtime;
481                         }
482                         if (timespec_compare(atime, &inode->i_atime) > 0) {
483                                 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
484                                      inode->i_atime.tv_sec,
485                                      inode->i_atime.tv_nsec,
486                                      atime->tv_sec, atime->tv_nsec);
487                                 inode->i_atime = *atime;
488                         }
489                 } else if (issued & CEPH_CAP_FILE_EXCL) {
490                         /* we did a utimes(); ignore mds values */
491                 } else {
492                         warn = 1;
493                 }
494         } else {
495                 /* we have no write caps; whatever the MDS says is true */
496                 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
497                         inode->i_ctime = *ctime;
498                         inode->i_mtime = *mtime;
499                         inode->i_atime = *atime;
500                         ci->i_time_warp_seq = time_warp_seq;
501                 } else {
502                         warn = 1;
503                 }
504         }
505         if (warn) /* time_warp_seq shouldn't go backwards */
506                 dout("%p mds time_warp_seq %llu < %u\n",
507                      inode, time_warp_seq, ci->i_time_warp_seq);
508 }
509
510 /*
511  * Populate an inode based on info from mds.  May be called on new or
512  * existing inodes.
513  */
514 static int fill_inode(struct inode *inode,
515                       struct ceph_mds_reply_info_in *iinfo,
516                       struct ceph_mds_reply_dirfrag *dirinfo,
517                       struct ceph_mds_session *session,
518                       unsigned long ttl_from, int cap_fmode,
519                       struct ceph_cap_reservation *caps_reservation)
520 {
521         struct ceph_mds_reply_inode *info = iinfo->in;
522         struct ceph_inode_info *ci = ceph_inode(inode);
523         int i;
524         int issued, implemented;
525         struct timespec mtime, atime, ctime;
526         u32 nsplits;
527         struct ceph_buffer *xattr_blob = NULL;
528         int err = 0;
529         int queue_trunc = 0;
530
531         dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
532              inode, ceph_vinop(inode), le64_to_cpu(info->version),
533              ci->i_version);
534
535         /*
536          * prealloc xattr data, if it looks like we'll need it.  only
537          * if len > 4 (meaning there are actually xattrs; the first 4
538          * bytes are the xattr count).
539          */
540         if (iinfo->xattr_len > 4) {
541                 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
542                 if (!xattr_blob)
543                         pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
544                                iinfo->xattr_len);
545         }
546
547         spin_lock(&inode->i_lock);
548
549         /*
550          * provided version will be odd if inode value is projected,
551          * even if stable.  skip the update if we have a newer info
552          * (e.g., due to inode info racing form multiple MDSs), or if
553          * we are getting projected (unstable) inode info.
554          */
555         if (le64_to_cpu(info->version) > 0 &&
556             (ci->i_version & ~1) > le64_to_cpu(info->version))
557                 goto no_change;
558
559         issued = __ceph_caps_issued(ci, &implemented);
560         issued |= implemented | __ceph_caps_dirty(ci);
561
562         /* update inode */
563         ci->i_version = le64_to_cpu(info->version);
564         inode->i_version++;
565         inode->i_rdev = le32_to_cpu(info->rdev);
566
567         if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
568                 inode->i_mode = le32_to_cpu(info->mode);
569                 inode->i_uid = le32_to_cpu(info->uid);
570                 inode->i_gid = le32_to_cpu(info->gid);
571                 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
572                      inode->i_uid, inode->i_gid);
573         }
574
575         if ((issued & CEPH_CAP_LINK_EXCL) == 0)
576                 inode->i_nlink = le32_to_cpu(info->nlink);
577
578         /* be careful with mtime, atime, size */
579         ceph_decode_timespec(&atime, &info->atime);
580         ceph_decode_timespec(&mtime, &info->mtime);
581         ceph_decode_timespec(&ctime, &info->ctime);
582         queue_trunc = ceph_fill_file_size(inode, issued,
583                                           le32_to_cpu(info->truncate_seq),
584                                           le64_to_cpu(info->truncate_size),
585                                           le64_to_cpu(info->size));
586         ceph_fill_file_time(inode, issued,
587                             le32_to_cpu(info->time_warp_seq),
588                             &ctime, &mtime, &atime);
589
590         ci->i_max_size = le64_to_cpu(info->max_size);
591         ci->i_layout = info->layout;
592         inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
593
594         /* xattrs */
595         /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
596         if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
597             le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
598                 if (ci->i_xattrs.blob)
599                         ceph_buffer_put(ci->i_xattrs.blob);
600                 ci->i_xattrs.blob = xattr_blob;
601                 if (xattr_blob)
602                         memcpy(ci->i_xattrs.blob->vec.iov_base,
603                                iinfo->xattr_data, iinfo->xattr_len);
604                 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
605         }
606
607         inode->i_mapping->a_ops = &ceph_aops;
608         inode->i_mapping->backing_dev_info =
609                 &ceph_client(inode->i_sb)->backing_dev_info;
610
611         switch (inode->i_mode & S_IFMT) {
612         case S_IFIFO:
613         case S_IFBLK:
614         case S_IFCHR:
615         case S_IFSOCK:
616                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
617                 inode->i_op = &ceph_file_iops;
618                 break;
619         case S_IFREG:
620                 inode->i_op = &ceph_file_iops;
621                 inode->i_fop = &ceph_file_fops;
622                 break;
623         case S_IFLNK:
624                 inode->i_op = &ceph_symlink_iops;
625                 if (!ci->i_symlink) {
626                         int symlen = iinfo->symlink_len;
627                         char *sym;
628
629                         BUG_ON(symlen != inode->i_size);
630                         spin_unlock(&inode->i_lock);
631
632                         err = -ENOMEM;
633                         sym = kmalloc(symlen+1, GFP_NOFS);
634                         if (!sym)
635                                 goto out;
636                         memcpy(sym, iinfo->symlink, symlen);
637                         sym[symlen] = 0;
638
639                         spin_lock(&inode->i_lock);
640                         if (!ci->i_symlink)
641                                 ci->i_symlink = sym;
642                         else
643                                 kfree(sym); /* lost a race */
644                 }
645                 break;
646         case S_IFDIR:
647                 inode->i_op = &ceph_dir_iops;
648                 inode->i_fop = &ceph_dir_fops;
649
650                 ci->i_files = le64_to_cpu(info->files);
651                 ci->i_subdirs = le64_to_cpu(info->subdirs);
652                 ci->i_rbytes = le64_to_cpu(info->rbytes);
653                 ci->i_rfiles = le64_to_cpu(info->rfiles);
654                 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
655                 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
656
657                 /* set dir completion flag? */
658                 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
659                     ceph_snap(inode) == CEPH_NOSNAP &&
660                     (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) {
661                         dout(" marking %p complete (empty)\n", inode);
662                         ci->i_ceph_flags |= CEPH_I_COMPLETE;
663                         ci->i_max_offset = 2;
664                 }
665
666                 /* it may be better to set st_size in getattr instead? */
667                 if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES))
668                         inode->i_size = ci->i_rbytes;
669                 break;
670         default:
671                 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
672                        ceph_vinop(inode), inode->i_mode);
673         }
674
675 no_change:
676         spin_unlock(&inode->i_lock);
677
678         /* queue truncate if we saw i_size decrease */
679         if (queue_trunc)
680                 ceph_queue_vmtruncate(inode);
681
682         /* populate frag tree */
683         /* FIXME: move me up, if/when version reflects fragtree changes */
684         nsplits = le32_to_cpu(info->fragtree.nsplits);
685         mutex_lock(&ci->i_fragtree_mutex);
686         for (i = 0; i < nsplits; i++) {
687                 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
688                 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
689
690                 if (IS_ERR(frag))
691                         continue;
692                 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
693                 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
694         }
695         mutex_unlock(&ci->i_fragtree_mutex);
696
697         /* were we issued a capability? */
698         if (info->cap.caps) {
699                 if (ceph_snap(inode) == CEPH_NOSNAP) {
700                         ceph_add_cap(inode, session,
701                                      le64_to_cpu(info->cap.cap_id),
702                                      cap_fmode,
703                                      le32_to_cpu(info->cap.caps),
704                                      le32_to_cpu(info->cap.wanted),
705                                      le32_to_cpu(info->cap.seq),
706                                      le32_to_cpu(info->cap.mseq),
707                                      le64_to_cpu(info->cap.realm),
708                                      info->cap.flags,
709                                      caps_reservation);
710                 } else {
711                         spin_lock(&inode->i_lock);
712                         dout(" %p got snap_caps %s\n", inode,
713                              ceph_cap_string(le32_to_cpu(info->cap.caps)));
714                         ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
715                         if (cap_fmode >= 0)
716                                 __ceph_get_fmode(ci, cap_fmode);
717                         spin_unlock(&inode->i_lock);
718                 }
719         }
720
721         /* update delegation info? */
722         if (dirinfo)
723                 ceph_fill_dirfrag(inode, dirinfo);
724
725         err = 0;
726
727 out:
728         if (xattr_blob)
729                 ceph_buffer_put(xattr_blob);
730         return err;
731 }
732
733 /*
734  * caller should hold session s_mutex.
735  */
736 static void update_dentry_lease(struct dentry *dentry,
737                                 struct ceph_mds_reply_lease *lease,
738                                 struct ceph_mds_session *session,
739                                 unsigned long from_time)
740 {
741         struct ceph_dentry_info *di = ceph_dentry(dentry);
742         long unsigned duration = le32_to_cpu(lease->duration_ms);
743         long unsigned ttl = from_time + (duration * HZ) / 1000;
744         long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
745         struct inode *dir;
746
747         /* only track leases on regular dentries */
748         if (dentry->d_op != &ceph_dentry_ops)
749                 return;
750
751         spin_lock(&dentry->d_lock);
752         dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
753              dentry, le16_to_cpu(lease->mask), duration, ttl);
754
755         /* make lease_rdcache_gen match directory */
756         dir = dentry->d_parent->d_inode;
757         di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
758
759         if (lease->mask == 0)
760                 goto out_unlock;
761
762         if (di->lease_gen == session->s_cap_gen &&
763             time_before(ttl, dentry->d_time))
764                 goto out_unlock;  /* we already have a newer lease. */
765
766         if (di->lease_session && di->lease_session != session)
767                 goto out_unlock;
768
769         ceph_dentry_lru_touch(dentry);
770
771         if (!di->lease_session)
772                 di->lease_session = ceph_get_mds_session(session);
773         di->lease_gen = session->s_cap_gen;
774         di->lease_seq = le32_to_cpu(lease->seq);
775         di->lease_renew_after = half_ttl;
776         di->lease_renew_from = 0;
777         dentry->d_time = ttl;
778 out_unlock:
779         spin_unlock(&dentry->d_lock);
780         return;
781 }
782
783 /*
784  * splice a dentry to an inode.
785  * caller must hold directory i_mutex for this to be safe.
786  *
787  * we will only rehash the resulting dentry if @prehash is
788  * true; @prehash will be set to false (for the benefit of
789  * the caller) if we fail.
790  */
791 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
792                                     bool *prehash)
793 {
794         struct dentry *realdn;
795
796         /* dn must be unhashed */
797         if (!d_unhashed(dn))
798                 d_drop(dn);
799         realdn = d_materialise_unique(dn, in);
800         if (IS_ERR(realdn)) {
801                 pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
802                        dn, in, ceph_vinop(in));
803                 if (prehash)
804                         *prehash = false; /* don't rehash on error */
805                 dn = realdn; /* note realdn contains the error */
806                 goto out;
807         } else if (realdn) {
808                 dout("dn %p (%d) spliced with %p (%d) "
809                      "inode %p ino %llx.%llx\n",
810                      dn, atomic_read(&dn->d_count),
811                      realdn, atomic_read(&realdn->d_count),
812                      realdn->d_inode, ceph_vinop(realdn->d_inode));
813                 dput(dn);
814                 dn = realdn;
815         } else {
816                 BUG_ON(!ceph_dentry(dn));
817
818                 dout("dn %p attached to %p ino %llx.%llx\n",
819                      dn, dn->d_inode, ceph_vinop(dn->d_inode));
820         }
821         if ((!prehash || *prehash) && d_unhashed(dn))
822                 d_rehash(dn);
823 out:
824         return dn;
825 }
826
827 /*
828  * Set dentry's directory position based on the current dir's max, and
829  * order it in d_subdirs, so that dcache_readdir behaves.
830  */
831 static void ceph_set_dentry_offset(struct dentry *dn)
832 {
833         struct dentry *dir = dn->d_parent;
834         struct inode *inode = dn->d_parent->d_inode;
835         struct ceph_dentry_info *di;
836
837         BUG_ON(!inode);
838
839         di = ceph_dentry(dn);
840
841         spin_lock(&inode->i_lock);
842         di->offset = ceph_inode(inode)->i_max_offset++;
843         spin_unlock(&inode->i_lock);
844
845         spin_lock(&dcache_lock);
846         spin_lock(&dn->d_lock);
847         list_move_tail(&dir->d_subdirs, &dn->d_u.d_child);
848         dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
849              dn->d_u.d_child.prev, dn->d_u.d_child.next);
850         spin_unlock(&dn->d_lock);
851         spin_unlock(&dcache_lock);
852 }
853
854 /*
855  * Incorporate results into the local cache.  This is either just
856  * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
857  * after a lookup).
858  *
859  * A reply may contain
860  *         a directory inode along with a dentry.
861  *  and/or a target inode
862  *
863  * Called with snap_rwsem (read).
864  */
865 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
866                     struct ceph_mds_session *session)
867 {
868         struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
869         struct inode *in = NULL;
870         struct ceph_mds_reply_inode *ininfo;
871         struct ceph_vino vino;
872         int i = 0;
873         int err = 0;
874
875         dout("fill_trace %p is_dentry %d is_target %d\n", req,
876              rinfo->head->is_dentry, rinfo->head->is_target);
877
878 #if 0
879         /*
880          * Debugging hook:
881          *
882          * If we resend completed ops to a recovering mds, we get no
883          * trace.  Since that is very rare, pretend this is the case
884          * to ensure the 'no trace' handlers in the callers behave.
885          *
886          * Fill in inodes unconditionally to avoid breaking cap
887          * invariants.
888          */
889         if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
890                 pr_info("fill_trace faking empty trace on %lld %s\n",
891                         req->r_tid, ceph_mds_op_name(rinfo->head->op));
892                 if (rinfo->head->is_dentry) {
893                         rinfo->head->is_dentry = 0;
894                         err = fill_inode(req->r_locked_dir,
895                                          &rinfo->diri, rinfo->dirfrag,
896                                          session, req->r_request_started, -1);
897                 }
898                 if (rinfo->head->is_target) {
899                         rinfo->head->is_target = 0;
900                         ininfo = rinfo->targeti.in;
901                         vino.ino = le64_to_cpu(ininfo->ino);
902                         vino.snap = le64_to_cpu(ininfo->snapid);
903                         in = ceph_get_inode(sb, vino);
904                         err = fill_inode(in, &rinfo->targeti, NULL,
905                                          session, req->r_request_started,
906                                          req->r_fmode);
907                         iput(in);
908                 }
909         }
910 #endif
911
912         if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
913                 dout("fill_trace reply is empty!\n");
914                 if (rinfo->head->result == 0 && req->r_locked_dir) {
915                         struct ceph_inode_info *ci =
916                                 ceph_inode(req->r_locked_dir);
917                         dout(" clearing %p complete (empty trace)\n",
918                              req->r_locked_dir);
919                         ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
920                         ci->i_release_count++;
921                 }
922                 return 0;
923         }
924
925         if (rinfo->head->is_dentry) {
926                 struct inode *dir = req->r_locked_dir;
927
928                 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
929                                  session, req->r_request_started, -1,
930                                  &req->r_caps_reservation);
931                 if (err < 0)
932                         return err;
933         }
934
935         if (rinfo->head->is_dentry && !req->r_aborted) {
936                 /*
937                  * lookup link rename   : null -> possibly existing inode
938                  * mknod symlink mkdir  : null -> new inode
939                  * unlink               : linked -> null
940                  */
941                 struct inode *dir = req->r_locked_dir;
942                 struct dentry *dn = req->r_dentry;
943                 bool have_dir_cap, have_lease;
944
945                 BUG_ON(!dn);
946                 BUG_ON(!dir);
947                 BUG_ON(dn->d_parent->d_inode != dir);
948                 BUG_ON(ceph_ino(dir) !=
949                        le64_to_cpu(rinfo->diri.in->ino));
950                 BUG_ON(ceph_snap(dir) !=
951                        le64_to_cpu(rinfo->diri.in->snapid));
952
953                 /* do we have a lease on the whole dir? */
954                 have_dir_cap =
955                         (le32_to_cpu(rinfo->diri.in->cap.caps) &
956                          CEPH_CAP_FILE_SHARED);
957
958                 /* do we have a dn lease? */
959                 have_lease = have_dir_cap ||
960                         (le16_to_cpu(rinfo->dlease->mask) &
961                          CEPH_LOCK_DN);
962
963                 if (!have_lease)
964                         dout("fill_trace  no dentry lease or dir cap\n");
965
966                 /* rename? */
967                 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
968                         dout(" src %p '%.*s' dst %p '%.*s'\n",
969                              req->r_old_dentry,
970                              req->r_old_dentry->d_name.len,
971                              req->r_old_dentry->d_name.name,
972                              dn, dn->d_name.len, dn->d_name.name);
973                         dout("fill_trace doing d_move %p -> %p\n",
974                              req->r_old_dentry, dn);
975                         d_move(req->r_old_dentry, dn);
976                         dout(" src %p '%.*s' dst %p '%.*s'\n",
977                              req->r_old_dentry,
978                              req->r_old_dentry->d_name.len,
979                              req->r_old_dentry->d_name.name,
980                              dn, dn->d_name.len, dn->d_name.name);
981                         /* ensure target dentry is invalidated, despite
982                            rehashing bug in vfs_rename_dir */
983                         dn->d_time = jiffies;
984                         ceph_dentry(dn)->lease_shared_gen = 0;
985                         /* take overwritten dentry's readdir offset */
986                         ceph_dentry(req->r_old_dentry)->offset =
987                                 ceph_dentry(dn)->offset;
988                         dn = req->r_old_dentry;  /* use old_dentry */
989                         in = dn->d_inode;
990                 }
991
992                 /* null dentry? */
993                 if (!rinfo->head->is_target) {
994                         dout("fill_trace null dentry\n");
995                         if (dn->d_inode) {
996                                 dout("d_delete %p\n", dn);
997                                 d_delete(dn);
998                         } else {
999                                 dout("d_instantiate %p NULL\n", dn);
1000                                 d_instantiate(dn, NULL);
1001                                 if (have_lease && d_unhashed(dn))
1002                                         d_rehash(dn);
1003                                 update_dentry_lease(dn, rinfo->dlease,
1004                                                     session,
1005                                                     req->r_request_started);
1006                         }
1007                         goto done;
1008                 }
1009
1010                 /* attach proper inode */
1011                 ininfo = rinfo->targeti.in;
1012                 vino.ino = le64_to_cpu(ininfo->ino);
1013                 vino.snap = le64_to_cpu(ininfo->snapid);
1014                 if (!dn->d_inode) {
1015                         in = ceph_get_inode(sb, vino);
1016                         if (IS_ERR(in)) {
1017                                 pr_err("fill_trace bad get_inode "
1018                                        "%llx.%llx\n", vino.ino, vino.snap);
1019                                 err = PTR_ERR(in);
1020                                 d_delete(dn);
1021                                 goto done;
1022                         }
1023                         dn = splice_dentry(dn, in, &have_lease);
1024                         if (IS_ERR(dn)) {
1025                                 err = PTR_ERR(dn);
1026                                 goto done;
1027                         }
1028                         req->r_dentry = dn;  /* may have spliced */
1029                         ceph_set_dentry_offset(dn);
1030                         igrab(in);
1031                 } else if (ceph_ino(in) == vino.ino &&
1032                            ceph_snap(in) == vino.snap) {
1033                         igrab(in);
1034                 } else {
1035                         dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1036                              dn, in, ceph_ino(in), ceph_snap(in),
1037                              vino.ino, vino.snap);
1038                         have_lease = false;
1039                         in = NULL;
1040                 }
1041
1042                 if (have_lease)
1043                         update_dentry_lease(dn, rinfo->dlease, session,
1044                                             req->r_request_started);
1045                 dout(" final dn %p\n", dn);
1046                 i++;
1047         } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1048                    req->r_op == CEPH_MDS_OP_MKSNAP) {
1049                 struct dentry *dn = req->r_dentry;
1050
1051                 /* fill out a snapdir LOOKUPSNAP dentry */
1052                 BUG_ON(!dn);
1053                 BUG_ON(!req->r_locked_dir);
1054                 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1055                 ininfo = rinfo->targeti.in;
1056                 vino.ino = le64_to_cpu(ininfo->ino);
1057                 vino.snap = le64_to_cpu(ininfo->snapid);
1058                 in = ceph_get_inode(sb, vino);
1059                 if (IS_ERR(in)) {
1060                         pr_err("fill_inode get_inode badness %llx.%llx\n",
1061                                vino.ino, vino.snap);
1062                         err = PTR_ERR(in);
1063                         d_delete(dn);
1064                         goto done;
1065                 }
1066                 dout(" linking snapped dir %p to dn %p\n", in, dn);
1067                 dn = splice_dentry(dn, in, NULL);
1068                 if (IS_ERR(dn)) {
1069                         err = PTR_ERR(dn);
1070                         goto done;
1071                 }
1072                 ceph_set_dentry_offset(dn);
1073                 req->r_dentry = dn;  /* may have spliced */
1074                 igrab(in);
1075                 rinfo->head->is_dentry = 1;  /* fool notrace handlers */
1076         }
1077
1078         if (rinfo->head->is_target) {
1079                 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1080                 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1081
1082                 if (in == NULL || ceph_ino(in) != vino.ino ||
1083                     ceph_snap(in) != vino.snap) {
1084                         in = ceph_get_inode(sb, vino);
1085                         if (IS_ERR(in)) {
1086                                 err = PTR_ERR(in);
1087                                 goto done;
1088                         }
1089                 }
1090                 req->r_target_inode = in;
1091
1092                 err = fill_inode(in,
1093                                  &rinfo->targeti, NULL,
1094                                  session, req->r_request_started,
1095                                  (le32_to_cpu(rinfo->head->result) == 0) ?
1096                                  req->r_fmode : -1,
1097                                  &req->r_caps_reservation);
1098                 if (err < 0) {
1099                         pr_err("fill_inode badness %p %llx.%llx\n",
1100                                in, ceph_vinop(in));
1101                         goto done;
1102                 }
1103         }
1104
1105 done:
1106         dout("fill_trace done err=%d\n", err);
1107         return err;
1108 }
1109
1110 /*
1111  * Prepopulate our cache with readdir results, leases, etc.
1112  */
1113 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1114                              struct ceph_mds_session *session)
1115 {
1116         struct dentry *parent = req->r_dentry;
1117         struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1118         struct qstr dname;
1119         struct dentry *dn;
1120         struct inode *in;
1121         int err = 0, i;
1122         struct inode *snapdir = NULL;
1123         struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1124         u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1125         struct ceph_dentry_info *di;
1126
1127         if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1128                 snapdir = ceph_get_snapdir(parent->d_inode);
1129                 parent = d_find_alias(snapdir);
1130                 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1131                      rinfo->dir_nr, parent);
1132         } else {
1133                 dout("readdir_prepopulate %d items under dn %p\n",
1134                      rinfo->dir_nr, parent);
1135                 if (rinfo->dir_dir)
1136                         ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1137         }
1138
1139         for (i = 0; i < rinfo->dir_nr; i++) {
1140                 struct ceph_vino vino;
1141
1142                 dname.name = rinfo->dir_dname[i];
1143                 dname.len = rinfo->dir_dname_len[i];
1144                 dname.hash = full_name_hash(dname.name, dname.len);
1145
1146                 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1147                 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1148
1149 retry_lookup:
1150                 dn = d_lookup(parent, &dname);
1151                 dout("d_lookup on parent=%p name=%.*s got %p\n",
1152                      parent, dname.len, dname.name, dn);
1153
1154                 if (!dn) {
1155                         dn = d_alloc(parent, &dname);
1156                         dout("d_alloc %p '%.*s' = %p\n", parent,
1157                              dname.len, dname.name, dn);
1158                         if (dn == NULL) {
1159                                 dout("d_alloc badness\n");
1160                                 err = -ENOMEM;
1161                                 goto out;
1162                         }
1163                         err = ceph_init_dentry(dn);
1164                         if (err < 0)
1165                                 goto out;
1166                 } else if (dn->d_inode &&
1167                            (ceph_ino(dn->d_inode) != vino.ino ||
1168                             ceph_snap(dn->d_inode) != vino.snap)) {
1169                         dout(" dn %p points to wrong inode %p\n",
1170                              dn, dn->d_inode);
1171                         d_delete(dn);
1172                         dput(dn);
1173                         goto retry_lookup;
1174                 } else {
1175                         /* reorder parent's d_subdirs */
1176                         spin_lock(&dcache_lock);
1177                         spin_lock(&dn->d_lock);
1178                         list_move(&dn->d_u.d_child, &parent->d_subdirs);
1179                         spin_unlock(&dn->d_lock);
1180                         spin_unlock(&dcache_lock);
1181                 }
1182
1183                 di = dn->d_fsdata;
1184                 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1185
1186                 /* inode */
1187                 if (dn->d_inode) {
1188                         in = dn->d_inode;
1189                 } else {
1190                         in = ceph_get_inode(parent->d_sb, vino);
1191                         if (in == NULL) {
1192                                 dout("new_inode badness\n");
1193                                 d_delete(dn);
1194                                 dput(dn);
1195                                 err = -ENOMEM;
1196                                 goto out;
1197                         }
1198                         dn = splice_dentry(dn, in, NULL);
1199                 }
1200
1201                 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1202                                req->r_request_started, -1,
1203                                &req->r_caps_reservation) < 0) {
1204                         pr_err("fill_inode badness on %p\n", in);
1205                         dput(dn);
1206                         continue;
1207                 }
1208                 update_dentry_lease(dn, rinfo->dir_dlease[i],
1209                                     req->r_session, req->r_request_started);
1210                 dput(dn);
1211         }
1212         req->r_did_prepopulate = true;
1213
1214 out:
1215         if (snapdir) {
1216                 iput(snapdir);
1217                 dput(parent);
1218         }
1219         dout("readdir_prepopulate done\n");
1220         return err;
1221 }
1222
1223 int ceph_inode_set_size(struct inode *inode, loff_t size)
1224 {
1225         struct ceph_inode_info *ci = ceph_inode(inode);
1226         int ret = 0;
1227
1228         spin_lock(&inode->i_lock);
1229         dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1230         inode->i_size = size;
1231         inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1232
1233         /* tell the MDS if we are approaching max_size */
1234         if ((size << 1) >= ci->i_max_size &&
1235             (ci->i_reported_size << 1) < ci->i_max_size)
1236                 ret = 1;
1237
1238         spin_unlock(&inode->i_lock);
1239         return ret;
1240 }
1241
1242 /*
1243  * Write back inode data in a worker thread.  (This can't be done
1244  * in the message handler context.)
1245  */
1246 void ceph_queue_writeback(struct inode *inode)
1247 {
1248         if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1249                        &ceph_inode(inode)->i_wb_work)) {
1250                 dout("ceph_queue_invalidate %p\n", inode);
1251                 igrab(inode);
1252         } else {
1253                 dout("ceph_queue_invalidate %p failed\n", inode);
1254         }
1255 }
1256
1257 static void ceph_writeback_work(struct work_struct *work)
1258 {
1259         struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1260                                                   i_wb_work);
1261         struct inode *inode = &ci->vfs_inode;
1262
1263         dout("writeback %p\n", inode);
1264         filemap_fdatawrite(&inode->i_data);
1265         iput(inode);
1266 }
1267
1268 /*
1269  * queue an async invalidation
1270  */
1271 void ceph_queue_invalidate(struct inode *inode)
1272 {
1273         if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1274                        &ceph_inode(inode)->i_pg_inv_work)) {
1275                 dout("ceph_queue_invalidate %p\n", inode);
1276                 igrab(inode);
1277         } else {
1278                 dout("ceph_queue_invalidate %p failed\n", inode);
1279         }
1280 }
1281
1282 /*
1283  * Invalidate inode pages in a worker thread.  (This can't be done
1284  * in the message handler context.)
1285  */
1286 static void ceph_invalidate_work(struct work_struct *work)
1287 {
1288         struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1289                                                   i_pg_inv_work);
1290         struct inode *inode = &ci->vfs_inode;
1291         u32 orig_gen;
1292         int check = 0;
1293
1294         spin_lock(&inode->i_lock);
1295         dout("invalidate_pages %p gen %d revoking %d\n", inode,
1296              ci->i_rdcache_gen, ci->i_rdcache_revoking);
1297         if (ci->i_rdcache_gen == 0 ||
1298             ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1299                 BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
1300                 /* nevermind! */
1301                 ci->i_rdcache_revoking = 0;
1302                 spin_unlock(&inode->i_lock);
1303                 goto out;
1304         }
1305         orig_gen = ci->i_rdcache_gen;
1306         spin_unlock(&inode->i_lock);
1307
1308         truncate_inode_pages(&inode->i_data, 0);
1309
1310         spin_lock(&inode->i_lock);
1311         if (orig_gen == ci->i_rdcache_gen) {
1312                 dout("invalidate_pages %p gen %d successful\n", inode,
1313                      ci->i_rdcache_gen);
1314                 ci->i_rdcache_gen = 0;
1315                 ci->i_rdcache_revoking = 0;
1316                 check = 1;
1317         } else {
1318                 dout("invalidate_pages %p gen %d raced, gen now %d\n",
1319                      inode, orig_gen, ci->i_rdcache_gen);
1320         }
1321         spin_unlock(&inode->i_lock);
1322
1323         if (check)
1324                 ceph_check_caps(ci, 0, NULL);
1325 out:
1326         iput(inode);
1327 }
1328
1329
1330 /*
1331  * called by trunc_wq; take i_mutex ourselves
1332  *
1333  * We also truncate in a separate thread as well.
1334  */
1335 static void ceph_vmtruncate_work(struct work_struct *work)
1336 {
1337         struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1338                                                   i_vmtruncate_work);
1339         struct inode *inode = &ci->vfs_inode;
1340
1341         dout("vmtruncate_work %p\n", inode);
1342         mutex_lock(&inode->i_mutex);
1343         __ceph_do_pending_vmtruncate(inode);
1344         mutex_unlock(&inode->i_mutex);
1345         iput(inode);
1346 }
1347
1348 /*
1349  * Queue an async vmtruncate.  If we fail to queue work, we will handle
1350  * the truncation the next time we call __ceph_do_pending_vmtruncate.
1351  */
1352 void ceph_queue_vmtruncate(struct inode *inode)
1353 {
1354         struct ceph_inode_info *ci = ceph_inode(inode);
1355
1356         if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
1357                        &ci->i_vmtruncate_work)) {
1358                 dout("ceph_queue_vmtruncate %p\n", inode);
1359                 igrab(inode);
1360         } else {
1361                 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1362                      inode, ci->i_truncate_pending);
1363         }
1364 }
1365
1366 /*
1367  * called with i_mutex held.
1368  *
1369  * Make sure any pending truncation is applied before doing anything
1370  * that may depend on it.
1371  */
1372 void __ceph_do_pending_vmtruncate(struct inode *inode)
1373 {
1374         struct ceph_inode_info *ci = ceph_inode(inode);
1375         u64 to;
1376         int wrbuffer_refs, wake = 0;
1377
1378 retry:
1379         spin_lock(&inode->i_lock);
1380         if (ci->i_truncate_pending == 0) {
1381                 dout("__do_pending_vmtruncate %p none pending\n", inode);
1382                 spin_unlock(&inode->i_lock);
1383                 return;
1384         }
1385
1386         /*
1387          * make sure any dirty snapped pages are flushed before we
1388          * possibly truncate them.. so write AND block!
1389          */
1390         if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1391                 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1392                      inode);
1393                 spin_unlock(&inode->i_lock);
1394                 filemap_write_and_wait_range(&inode->i_data, 0,
1395                                              inode->i_sb->s_maxbytes);
1396                 goto retry;
1397         }
1398
1399         to = ci->i_truncate_size;
1400         wrbuffer_refs = ci->i_wrbuffer_ref;
1401         dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1402              ci->i_truncate_pending, to);
1403         spin_unlock(&inode->i_lock);
1404
1405         truncate_inode_pages(inode->i_mapping, to);
1406
1407         spin_lock(&inode->i_lock);
1408         ci->i_truncate_pending--;
1409         if (ci->i_truncate_pending == 0)
1410                 wake = 1;
1411         spin_unlock(&inode->i_lock);
1412
1413         if (wrbuffer_refs == 0)
1414                 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1415         if (wake)
1416                 wake_up(&ci->i_cap_wq);
1417 }
1418
1419
1420 /*
1421  * symlinks
1422  */
1423 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1424 {
1425         struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1426         nd_set_link(nd, ci->i_symlink);
1427         return NULL;
1428 }
1429
1430 static const struct inode_operations ceph_symlink_iops = {
1431         .readlink = generic_readlink,
1432         .follow_link = ceph_sym_follow_link,
1433 };
1434
1435 /*
1436  * setattr
1437  */
1438 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1439 {
1440         struct inode *inode = dentry->d_inode;
1441         struct ceph_inode_info *ci = ceph_inode(inode);
1442         struct inode *parent_inode = dentry->d_parent->d_inode;
1443         const unsigned int ia_valid = attr->ia_valid;
1444         struct ceph_mds_request *req;
1445         struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
1446         int issued;
1447         int release = 0, dirtied = 0;
1448         int mask = 0;
1449         int err = 0;
1450
1451         if (ceph_snap(inode) != CEPH_NOSNAP)
1452                 return -EROFS;
1453
1454         __ceph_do_pending_vmtruncate(inode);
1455
1456         err = inode_change_ok(inode, attr);
1457         if (err != 0)
1458                 return err;
1459
1460         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1461                                        USE_AUTH_MDS);
1462         if (IS_ERR(req))
1463                 return PTR_ERR(req);
1464
1465         spin_lock(&inode->i_lock);
1466         issued = __ceph_caps_issued(ci, NULL);
1467         dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1468
1469         if (ia_valid & ATTR_UID) {
1470                 dout("setattr %p uid %d -> %d\n", inode,
1471                      inode->i_uid, attr->ia_uid);
1472                 if (issued & CEPH_CAP_AUTH_EXCL) {
1473                         inode->i_uid = attr->ia_uid;
1474                         dirtied |= CEPH_CAP_AUTH_EXCL;
1475                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1476                            attr->ia_uid != inode->i_uid) {
1477                         req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1478                         mask |= CEPH_SETATTR_UID;
1479                         release |= CEPH_CAP_AUTH_SHARED;
1480                 }
1481         }
1482         if (ia_valid & ATTR_GID) {
1483                 dout("setattr %p gid %d -> %d\n", inode,
1484                      inode->i_gid, attr->ia_gid);
1485                 if (issued & CEPH_CAP_AUTH_EXCL) {
1486                         inode->i_gid = attr->ia_gid;
1487                         dirtied |= CEPH_CAP_AUTH_EXCL;
1488                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1489                            attr->ia_gid != inode->i_gid) {
1490                         req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1491                         mask |= CEPH_SETATTR_GID;
1492                         release |= CEPH_CAP_AUTH_SHARED;
1493                 }
1494         }
1495         if (ia_valid & ATTR_MODE) {
1496                 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1497                      attr->ia_mode);
1498                 if (issued & CEPH_CAP_AUTH_EXCL) {
1499                         inode->i_mode = attr->ia_mode;
1500                         dirtied |= CEPH_CAP_AUTH_EXCL;
1501                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1502                            attr->ia_mode != inode->i_mode) {
1503                         req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1504                         mask |= CEPH_SETATTR_MODE;
1505                         release |= CEPH_CAP_AUTH_SHARED;
1506                 }
1507         }
1508
1509         if (ia_valid & ATTR_ATIME) {
1510                 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1511                      inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1512                      attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1513                 if (issued & CEPH_CAP_FILE_EXCL) {
1514                         ci->i_time_warp_seq++;
1515                         inode->i_atime = attr->ia_atime;
1516                         dirtied |= CEPH_CAP_FILE_EXCL;
1517                 } else if ((issued & CEPH_CAP_FILE_WR) &&
1518                            timespec_compare(&inode->i_atime,
1519                                             &attr->ia_atime) < 0) {
1520                         inode->i_atime = attr->ia_atime;
1521                         dirtied |= CEPH_CAP_FILE_WR;
1522                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1523                            !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1524                         ceph_encode_timespec(&req->r_args.setattr.atime,
1525                                              &attr->ia_atime);
1526                         mask |= CEPH_SETATTR_ATIME;
1527                         release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1528                                 CEPH_CAP_FILE_WR;
1529                 }
1530         }
1531         if (ia_valid & ATTR_MTIME) {
1532                 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1533                      inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1534                      attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1535                 if (issued & CEPH_CAP_FILE_EXCL) {
1536                         ci->i_time_warp_seq++;
1537                         inode->i_mtime = attr->ia_mtime;
1538                         dirtied |= CEPH_CAP_FILE_EXCL;
1539                 } else if ((issued & CEPH_CAP_FILE_WR) &&
1540                            timespec_compare(&inode->i_mtime,
1541                                             &attr->ia_mtime) < 0) {
1542                         inode->i_mtime = attr->ia_mtime;
1543                         dirtied |= CEPH_CAP_FILE_WR;
1544                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1545                            !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1546                         ceph_encode_timespec(&req->r_args.setattr.mtime,
1547                                              &attr->ia_mtime);
1548                         mask |= CEPH_SETATTR_MTIME;
1549                         release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1550                                 CEPH_CAP_FILE_WR;
1551                 }
1552         }
1553         if (ia_valid & ATTR_SIZE) {
1554                 dout("setattr %p size %lld -> %lld\n", inode,
1555                      inode->i_size, attr->ia_size);
1556                 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1557                         err = -EINVAL;
1558                         goto out;
1559                 }
1560                 if ((issued & CEPH_CAP_FILE_EXCL) &&
1561                     attr->ia_size > inode->i_size) {
1562                         inode->i_size = attr->ia_size;
1563                         inode->i_blocks =
1564                                 (attr->ia_size + (1 << 9) - 1) >> 9;
1565                         inode->i_ctime = attr->ia_ctime;
1566                         ci->i_reported_size = attr->ia_size;
1567                         dirtied |= CEPH_CAP_FILE_EXCL;
1568                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1569                            attr->ia_size != inode->i_size) {
1570                         req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1571                         req->r_args.setattr.old_size =
1572                                 cpu_to_le64(inode->i_size);
1573                         mask |= CEPH_SETATTR_SIZE;
1574                         release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1575                                 CEPH_CAP_FILE_WR;
1576                 }
1577         }
1578
1579         /* these do nothing */
1580         if (ia_valid & ATTR_CTIME) {
1581                 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1582                                          ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1583                 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1584                      inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1585                      attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1586                      only ? "ctime only" : "ignored");
1587                 inode->i_ctime = attr->ia_ctime;
1588                 if (only) {
1589                         /*
1590                          * if kernel wants to dirty ctime but nothing else,
1591                          * we need to choose a cap to dirty under, or do
1592                          * a almost-no-op setattr
1593                          */
1594                         if (issued & CEPH_CAP_AUTH_EXCL)
1595                                 dirtied |= CEPH_CAP_AUTH_EXCL;
1596                         else if (issued & CEPH_CAP_FILE_EXCL)
1597                                 dirtied |= CEPH_CAP_FILE_EXCL;
1598                         else if (issued & CEPH_CAP_XATTR_EXCL)
1599                                 dirtied |= CEPH_CAP_XATTR_EXCL;
1600                         else
1601                                 mask |= CEPH_SETATTR_CTIME;
1602                 }
1603         }
1604         if (ia_valid & ATTR_FILE)
1605                 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1606
1607         if (dirtied) {
1608                 __ceph_mark_dirty_caps(ci, dirtied);
1609                 inode->i_ctime = CURRENT_TIME;
1610         }
1611
1612         release &= issued;
1613         spin_unlock(&inode->i_lock);
1614
1615         if (mask) {
1616                 req->r_inode = igrab(inode);
1617                 req->r_inode_drop = release;
1618                 req->r_args.setattr.mask = cpu_to_le32(mask);
1619                 req->r_num_caps = 1;
1620                 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1621         }
1622         dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1623              ceph_cap_string(dirtied), mask);
1624
1625         ceph_mdsc_put_request(req);
1626         __ceph_do_pending_vmtruncate(inode);
1627         return err;
1628 out:
1629         spin_unlock(&inode->i_lock);
1630         ceph_mdsc_put_request(req);
1631         return err;
1632 }
1633
1634 /*
1635  * Verify that we have a lease on the given mask.  If not,
1636  * do a getattr against an mds.
1637  */
1638 int ceph_do_getattr(struct inode *inode, int mask)
1639 {
1640         struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
1641         struct ceph_mds_client *mdsc = &client->mdsc;
1642         struct ceph_mds_request *req;
1643         int err;
1644
1645         if (ceph_snap(inode) == CEPH_SNAPDIR) {
1646                 dout("do_getattr inode %p SNAPDIR\n", inode);
1647                 return 0;
1648         }
1649
1650         dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
1651         if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1652                 return 0;
1653
1654         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1655         if (IS_ERR(req))
1656                 return PTR_ERR(req);
1657         req->r_inode = igrab(inode);
1658         req->r_num_caps = 1;
1659         req->r_args.getattr.mask = cpu_to_le32(mask);
1660         err = ceph_mdsc_do_request(mdsc, NULL, req);
1661         ceph_mdsc_put_request(req);
1662         dout("do_getattr result=%d\n", err);
1663         return err;
1664 }
1665
1666
1667 /*
1668  * Check inode permissions.  We verify we have a valid value for
1669  * the AUTH cap, then call the generic handler.
1670  */
1671 int ceph_permission(struct inode *inode, int mask)
1672 {
1673         int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1674
1675         if (!err)
1676                 err = generic_permission(inode, mask, NULL);
1677         return err;
1678 }
1679
1680 /*
1681  * Get all attributes.  Hopefully somedata we'll have a statlite()
1682  * and can limit the fields we require to be accurate.
1683  */
1684 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1685                  struct kstat *stat)
1686 {
1687         struct inode *inode = dentry->d_inode;
1688         struct ceph_inode_info *ci = ceph_inode(inode);
1689         int err;
1690
1691         err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1692         if (!err) {
1693                 generic_fillattr(inode, stat);
1694                 stat->ino = inode->i_ino;
1695                 if (ceph_snap(inode) != CEPH_NOSNAP)
1696                         stat->dev = ceph_snap(inode);
1697                 else
1698                         stat->dev = 0;
1699                 if (S_ISDIR(inode->i_mode)) {
1700                         stat->size = ci->i_rbytes;
1701                         stat->blocks = 0;
1702                         stat->blksize = 65536;
1703                 }
1704         }
1705         return err;
1706 }