094b8fc377878c6c8066fa7d34ff754d6095946b
[linux-2.6-microblaze.git] / fs / ceph / inode.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/module.h>
5 #include <linux/fs.h>
6 #include <linux/slab.h>
7 #include <linux/string.h>
8 #include <linux/uaccess.h>
9 #include <linux/kernel.h>
10 #include <linux/writeback.h>
11 #include <linux/vmalloc.h>
12 #include <linux/xattr.h>
13 #include <linux/posix_acl.h>
14 #include <linux/random.h>
15 #include <linux/sort.h>
16 #include <linux/iversion.h>
17
18 #include "super.h"
19 #include "mds_client.h"
20 #include "cache.h"
21 #include <linux/ceph/decode.h>
22
23 /*
24  * Ceph inode operations
25  *
26  * Implement basic inode helpers (get, alloc) and inode ops (getattr,
27  * setattr, etc.), xattr helpers, and helpers for assimilating
28  * metadata returned by the MDS into our cache.
29  *
30  * Also define helpers for doing asynchronous writeback, invalidation,
31  * and truncation for the benefit of those who can't afford to block
32  * (typically because they are in the message handler path).
33  */
34
35 static const struct inode_operations ceph_symlink_iops;
36
37 static void ceph_inode_work(struct work_struct *work);
38
39 /*
40  * find or create an inode, given the ceph ino number
41  */
42 static int ceph_set_ino_cb(struct inode *inode, void *data)
43 {
44         ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
45         inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
46         inode_set_iversion_raw(inode, 0);
47         return 0;
48 }
49
50 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
51 {
52         struct inode *inode;
53         ino_t t = ceph_vino_to_ino(vino);
54
55         inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
56         if (!inode)
57                 return ERR_PTR(-ENOMEM);
58         if (inode->i_state & I_NEW)
59                 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
60                      inode, ceph_vinop(inode), (u64)inode->i_ino);
61
62         dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
63              vino.snap, inode);
64         return inode;
65 }
66
67 /*
68  * get/constuct snapdir inode for a given directory
69  */
70 struct inode *ceph_get_snapdir(struct inode *parent)
71 {
72         struct ceph_vino vino = {
73                 .ino = ceph_ino(parent),
74                 .snap = CEPH_SNAPDIR,
75         };
76         struct inode *inode = ceph_get_inode(parent->i_sb, vino);
77         struct ceph_inode_info *ci = ceph_inode(inode);
78
79         BUG_ON(!S_ISDIR(parent->i_mode));
80         if (IS_ERR(inode))
81                 return inode;
82         inode->i_mode = parent->i_mode;
83         inode->i_uid = parent->i_uid;
84         inode->i_gid = parent->i_gid;
85         inode->i_op = &ceph_snapdir_iops;
86         inode->i_fop = &ceph_snapdir_fops;
87         ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
88         ci->i_rbytes = 0;
89
90         if (inode->i_state & I_NEW)
91                 unlock_new_inode(inode);
92
93         return inode;
94 }
95
96 const struct inode_operations ceph_file_iops = {
97         .permission = ceph_permission,
98         .setattr = ceph_setattr,
99         .getattr = ceph_getattr,
100         .listxattr = ceph_listxattr,
101         .get_acl = ceph_get_acl,
102         .set_acl = ceph_set_acl,
103 };
104
105
106 /*
107  * We use a 'frag tree' to keep track of the MDS's directory fragments
108  * for a given inode (usually there is just a single fragment).  We
109  * need to know when a child frag is delegated to a new MDS, or when
110  * it is flagged as replicated, so we can direct our requests
111  * accordingly.
112  */
113
114 /*
115  * find/create a frag in the tree
116  */
117 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
118                                                     u32 f)
119 {
120         struct rb_node **p;
121         struct rb_node *parent = NULL;
122         struct ceph_inode_frag *frag;
123         int c;
124
125         p = &ci->i_fragtree.rb_node;
126         while (*p) {
127                 parent = *p;
128                 frag = rb_entry(parent, struct ceph_inode_frag, node);
129                 c = ceph_frag_compare(f, frag->frag);
130                 if (c < 0)
131                         p = &(*p)->rb_left;
132                 else if (c > 0)
133                         p = &(*p)->rb_right;
134                 else
135                         return frag;
136         }
137
138         frag = kmalloc(sizeof(*frag), GFP_NOFS);
139         if (!frag)
140                 return ERR_PTR(-ENOMEM);
141
142         frag->frag = f;
143         frag->split_by = 0;
144         frag->mds = -1;
145         frag->ndist = 0;
146
147         rb_link_node(&frag->node, parent, p);
148         rb_insert_color(&frag->node, &ci->i_fragtree);
149
150         dout("get_or_create_frag added %llx.%llx frag %x\n",
151              ceph_vinop(&ci->vfs_inode), f);
152         return frag;
153 }
154
155 /*
156  * find a specific frag @f
157  */
158 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
159 {
160         struct rb_node *n = ci->i_fragtree.rb_node;
161
162         while (n) {
163                 struct ceph_inode_frag *frag =
164                         rb_entry(n, struct ceph_inode_frag, node);
165                 int c = ceph_frag_compare(f, frag->frag);
166                 if (c < 0)
167                         n = n->rb_left;
168                 else if (c > 0)
169                         n = n->rb_right;
170                 else
171                         return frag;
172         }
173         return NULL;
174 }
175
176 /*
177  * Choose frag containing the given value @v.  If @pfrag is
178  * specified, copy the frag delegation info to the caller if
179  * it is present.
180  */
181 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
182                               struct ceph_inode_frag *pfrag, int *found)
183 {
184         u32 t = ceph_frag_make(0, 0);
185         struct ceph_inode_frag *frag;
186         unsigned nway, i;
187         u32 n;
188
189         if (found)
190                 *found = 0;
191
192         while (1) {
193                 WARN_ON(!ceph_frag_contains_value(t, v));
194                 frag = __ceph_find_frag(ci, t);
195                 if (!frag)
196                         break; /* t is a leaf */
197                 if (frag->split_by == 0) {
198                         if (pfrag)
199                                 memcpy(pfrag, frag, sizeof(*pfrag));
200                         if (found)
201                                 *found = 1;
202                         break;
203                 }
204
205                 /* choose child */
206                 nway = 1 << frag->split_by;
207                 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
208                      frag->split_by, nway);
209                 for (i = 0; i < nway; i++) {
210                         n = ceph_frag_make_child(t, frag->split_by, i);
211                         if (ceph_frag_contains_value(n, v)) {
212                                 t = n;
213                                 break;
214                         }
215                 }
216                 BUG_ON(i == nway);
217         }
218         dout("choose_frag(%x) = %x\n", v, t);
219
220         return t;
221 }
222
223 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
224                      struct ceph_inode_frag *pfrag, int *found)
225 {
226         u32 ret;
227         mutex_lock(&ci->i_fragtree_mutex);
228         ret = __ceph_choose_frag(ci, v, pfrag, found);
229         mutex_unlock(&ci->i_fragtree_mutex);
230         return ret;
231 }
232
233 /*
234  * Process dirfrag (delegation) info from the mds.  Include leaf
235  * fragment in tree ONLY if ndist > 0.  Otherwise, only
236  * branches/splits are included in i_fragtree)
237  */
238 static int ceph_fill_dirfrag(struct inode *inode,
239                              struct ceph_mds_reply_dirfrag *dirinfo)
240 {
241         struct ceph_inode_info *ci = ceph_inode(inode);
242         struct ceph_inode_frag *frag;
243         u32 id = le32_to_cpu(dirinfo->frag);
244         int mds = le32_to_cpu(dirinfo->auth);
245         int ndist = le32_to_cpu(dirinfo->ndist);
246         int diri_auth = -1;
247         int i;
248         int err = 0;
249
250         spin_lock(&ci->i_ceph_lock);
251         if (ci->i_auth_cap)
252                 diri_auth = ci->i_auth_cap->mds;
253         spin_unlock(&ci->i_ceph_lock);
254
255         if (mds == -1) /* CDIR_AUTH_PARENT */
256                 mds = diri_auth;
257
258         mutex_lock(&ci->i_fragtree_mutex);
259         if (ndist == 0 && mds == diri_auth) {
260                 /* no delegation info needed. */
261                 frag = __ceph_find_frag(ci, id);
262                 if (!frag)
263                         goto out;
264                 if (frag->split_by == 0) {
265                         /* tree leaf, remove */
266                         dout("fill_dirfrag removed %llx.%llx frag %x"
267                              " (no ref)\n", ceph_vinop(inode), id);
268                         rb_erase(&frag->node, &ci->i_fragtree);
269                         kfree(frag);
270                 } else {
271                         /* tree branch, keep and clear */
272                         dout("fill_dirfrag cleared %llx.%llx frag %x"
273                              " referral\n", ceph_vinop(inode), id);
274                         frag->mds = -1;
275                         frag->ndist = 0;
276                 }
277                 goto out;
278         }
279
280
281         /* find/add this frag to store mds delegation info */
282         frag = __get_or_create_frag(ci, id);
283         if (IS_ERR(frag)) {
284                 /* this is not the end of the world; we can continue
285                    with bad/inaccurate delegation info */
286                 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
287                        ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
288                 err = -ENOMEM;
289                 goto out;
290         }
291
292         frag->mds = mds;
293         frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
294         for (i = 0; i < frag->ndist; i++)
295                 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
296         dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
297              ceph_vinop(inode), frag->frag, frag->ndist);
298
299 out:
300         mutex_unlock(&ci->i_fragtree_mutex);
301         return err;
302 }
303
304 static int frag_tree_split_cmp(const void *l, const void *r)
305 {
306         struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
307         struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
308         return ceph_frag_compare(le32_to_cpu(ls->frag),
309                                  le32_to_cpu(rs->frag));
310 }
311
312 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
313 {
314         if (!frag)
315                 return f == ceph_frag_make(0, 0);
316         if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
317                 return false;
318         return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
319 }
320
321 static int ceph_fill_fragtree(struct inode *inode,
322                               struct ceph_frag_tree_head *fragtree,
323                               struct ceph_mds_reply_dirfrag *dirinfo)
324 {
325         struct ceph_inode_info *ci = ceph_inode(inode);
326         struct ceph_inode_frag *frag, *prev_frag = NULL;
327         struct rb_node *rb_node;
328         unsigned i, split_by, nsplits;
329         u32 id;
330         bool update = false;
331
332         mutex_lock(&ci->i_fragtree_mutex);
333         nsplits = le32_to_cpu(fragtree->nsplits);
334         if (nsplits != ci->i_fragtree_nsplits) {
335                 update = true;
336         } else if (nsplits) {
337                 i = prandom_u32() % nsplits;
338                 id = le32_to_cpu(fragtree->splits[i].frag);
339                 if (!__ceph_find_frag(ci, id))
340                         update = true;
341         } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
342                 rb_node = rb_first(&ci->i_fragtree);
343                 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
344                 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
345                         update = true;
346         }
347         if (!update && dirinfo) {
348                 id = le32_to_cpu(dirinfo->frag);
349                 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
350                         update = true;
351         }
352         if (!update)
353                 goto out_unlock;
354
355         if (nsplits > 1) {
356                 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
357                      frag_tree_split_cmp, NULL);
358         }
359
360         dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
361         rb_node = rb_first(&ci->i_fragtree);
362         for (i = 0; i < nsplits; i++) {
363                 id = le32_to_cpu(fragtree->splits[i].frag);
364                 split_by = le32_to_cpu(fragtree->splits[i].by);
365                 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
366                         pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
367                                "frag %x split by %d\n", ceph_vinop(inode),
368                                i, nsplits, id, split_by);
369                         continue;
370                 }
371                 frag = NULL;
372                 while (rb_node) {
373                         frag = rb_entry(rb_node, struct ceph_inode_frag, node);
374                         if (ceph_frag_compare(frag->frag, id) >= 0) {
375                                 if (frag->frag != id)
376                                         frag = NULL;
377                                 else
378                                         rb_node = rb_next(rb_node);
379                                 break;
380                         }
381                         rb_node = rb_next(rb_node);
382                         /* delete stale split/leaf node */
383                         if (frag->split_by > 0 ||
384                             !is_frag_child(frag->frag, prev_frag)) {
385                                 rb_erase(&frag->node, &ci->i_fragtree);
386                                 if (frag->split_by > 0)
387                                         ci->i_fragtree_nsplits--;
388                                 kfree(frag);
389                         }
390                         frag = NULL;
391                 }
392                 if (!frag) {
393                         frag = __get_or_create_frag(ci, id);
394                         if (IS_ERR(frag))
395                                 continue;
396                 }
397                 if (frag->split_by == 0)
398                         ci->i_fragtree_nsplits++;
399                 frag->split_by = split_by;
400                 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
401                 prev_frag = frag;
402         }
403         while (rb_node) {
404                 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
405                 rb_node = rb_next(rb_node);
406                 /* delete stale split/leaf node */
407                 if (frag->split_by > 0 ||
408                     !is_frag_child(frag->frag, prev_frag)) {
409                         rb_erase(&frag->node, &ci->i_fragtree);
410                         if (frag->split_by > 0)
411                                 ci->i_fragtree_nsplits--;
412                         kfree(frag);
413                 }
414         }
415 out_unlock:
416         mutex_unlock(&ci->i_fragtree_mutex);
417         return 0;
418 }
419
420 /*
421  * initialize a newly allocated inode.
422  */
423 struct inode *ceph_alloc_inode(struct super_block *sb)
424 {
425         struct ceph_inode_info *ci;
426         int i;
427
428         ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
429         if (!ci)
430                 return NULL;
431
432         dout("alloc_inode %p\n", &ci->vfs_inode);
433
434         spin_lock_init(&ci->i_ceph_lock);
435
436         ci->i_version = 0;
437         ci->i_inline_version = 0;
438         ci->i_time_warp_seq = 0;
439         ci->i_ceph_flags = 0;
440         atomic64_set(&ci->i_ordered_count, 1);
441         atomic64_set(&ci->i_release_count, 1);
442         atomic64_set(&ci->i_complete_seq[0], 0);
443         atomic64_set(&ci->i_complete_seq[1], 0);
444         ci->i_symlink = NULL;
445
446         ci->i_max_bytes = 0;
447         ci->i_max_files = 0;
448
449         memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
450         RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
451
452         ci->i_fragtree = RB_ROOT;
453         mutex_init(&ci->i_fragtree_mutex);
454
455         ci->i_xattrs.blob = NULL;
456         ci->i_xattrs.prealloc_blob = NULL;
457         ci->i_xattrs.dirty = false;
458         ci->i_xattrs.index = RB_ROOT;
459         ci->i_xattrs.count = 0;
460         ci->i_xattrs.names_size = 0;
461         ci->i_xattrs.vals_size = 0;
462         ci->i_xattrs.version = 0;
463         ci->i_xattrs.index_version = 0;
464
465         ci->i_caps = RB_ROOT;
466         ci->i_auth_cap = NULL;
467         ci->i_dirty_caps = 0;
468         ci->i_flushing_caps = 0;
469         INIT_LIST_HEAD(&ci->i_dirty_item);
470         INIT_LIST_HEAD(&ci->i_flushing_item);
471         ci->i_prealloc_cap_flush = NULL;
472         INIT_LIST_HEAD(&ci->i_cap_flush_list);
473         init_waitqueue_head(&ci->i_cap_wq);
474         ci->i_hold_caps_min = 0;
475         ci->i_hold_caps_max = 0;
476         INIT_LIST_HEAD(&ci->i_cap_delay_list);
477         INIT_LIST_HEAD(&ci->i_cap_snaps);
478         ci->i_head_snapc = NULL;
479         ci->i_snap_caps = 0;
480
481         for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
482                 ci->i_nr_by_mode[i] = 0;
483
484         mutex_init(&ci->i_truncate_mutex);
485         ci->i_truncate_seq = 0;
486         ci->i_truncate_size = 0;
487         ci->i_truncate_pending = 0;
488
489         ci->i_max_size = 0;
490         ci->i_reported_size = 0;
491         ci->i_wanted_max_size = 0;
492         ci->i_requested_max_size = 0;
493
494         ci->i_pin_ref = 0;
495         ci->i_rd_ref = 0;
496         ci->i_rdcache_ref = 0;
497         ci->i_wr_ref = 0;
498         ci->i_wb_ref = 0;
499         ci->i_fx_ref = 0;
500         ci->i_wrbuffer_ref = 0;
501         ci->i_wrbuffer_ref_head = 0;
502         atomic_set(&ci->i_filelock_ref, 0);
503         atomic_set(&ci->i_shared_gen, 1);
504         ci->i_rdcache_gen = 0;
505         ci->i_rdcache_revoking = 0;
506
507         INIT_LIST_HEAD(&ci->i_unsafe_dirops);
508         INIT_LIST_HEAD(&ci->i_unsafe_iops);
509         spin_lock_init(&ci->i_unsafe_lock);
510
511         ci->i_snap_realm = NULL;
512         INIT_LIST_HEAD(&ci->i_snap_realm_item);
513         INIT_LIST_HEAD(&ci->i_snap_flush_item);
514
515         INIT_WORK(&ci->i_work, ceph_inode_work);
516         ci->i_work_mask = 0;
517         memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
518
519         ceph_fscache_inode_init(ci);
520
521         ci->i_meta_err = 0;
522
523         return &ci->vfs_inode;
524 }
525
526 void ceph_free_inode(struct inode *inode)
527 {
528         struct ceph_inode_info *ci = ceph_inode(inode);
529
530         kfree(ci->i_symlink);
531         kmem_cache_free(ceph_inode_cachep, ci);
532 }
533
534 void ceph_evict_inode(struct inode *inode)
535 {
536         struct ceph_inode_info *ci = ceph_inode(inode);
537         struct ceph_inode_frag *frag;
538         struct rb_node *n;
539
540         dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
541
542         truncate_inode_pages_final(&inode->i_data);
543         clear_inode(inode);
544
545         ceph_fscache_unregister_inode_cookie(ci);
546
547         __ceph_remove_caps(ci);
548
549         if (__ceph_has_any_quota(ci))
550                 ceph_adjust_quota_realms_count(inode, false);
551
552         /*
553          * we may still have a snap_realm reference if there are stray
554          * caps in i_snap_caps.
555          */
556         if (ci->i_snap_realm) {
557                 struct ceph_mds_client *mdsc =
558                                         ceph_inode_to_client(inode)->mdsc;
559                 if (ceph_snap(inode) == CEPH_NOSNAP) {
560                         struct ceph_snap_realm *realm = ci->i_snap_realm;
561                         dout(" dropping residual ref to snap realm %p\n",
562                              realm);
563                         spin_lock(&realm->inodes_with_caps_lock);
564                         list_del_init(&ci->i_snap_realm_item);
565                         ci->i_snap_realm = NULL;
566                         if (realm->ino == ci->i_vino.ino)
567                                 realm->inode = NULL;
568                         spin_unlock(&realm->inodes_with_caps_lock);
569                         ceph_put_snap_realm(mdsc, realm);
570                 } else {
571                         ceph_put_snapid_map(mdsc, ci->i_snapid_map);
572                         ci->i_snap_realm = NULL;
573                 }
574         }
575
576         while ((n = rb_first(&ci->i_fragtree)) != NULL) {
577                 frag = rb_entry(n, struct ceph_inode_frag, node);
578                 rb_erase(n, &ci->i_fragtree);
579                 kfree(frag);
580         }
581         ci->i_fragtree_nsplits = 0;
582
583         __ceph_destroy_xattrs(ci);
584         if (ci->i_xattrs.blob)
585                 ceph_buffer_put(ci->i_xattrs.blob);
586         if (ci->i_xattrs.prealloc_blob)
587                 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
588
589         ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
590 }
591
592 static inline blkcnt_t calc_inode_blocks(u64 size)
593 {
594         return (size + (1<<9) - 1) >> 9;
595 }
596
597 /*
598  * Helpers to fill in size, ctime, mtime, and atime.  We have to be
599  * careful because either the client or MDS may have more up to date
600  * info, depending on which capabilities are held, and whether
601  * time_warp_seq or truncate_seq have increased.  (Ordinarily, mtime
602  * and size are monotonically increasing, except when utimes() or
603  * truncate() increments the corresponding _seq values.)
604  */
605 int ceph_fill_file_size(struct inode *inode, int issued,
606                         u32 truncate_seq, u64 truncate_size, u64 size)
607 {
608         struct ceph_inode_info *ci = ceph_inode(inode);
609         int queue_trunc = 0;
610
611         if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
612             (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
613                 dout("size %lld -> %llu\n", inode->i_size, size);
614                 if (size > 0 && S_ISDIR(inode->i_mode)) {
615                         pr_err("fill_file_size non-zero size for directory\n");
616                         size = 0;
617                 }
618                 i_size_write(inode, size);
619                 inode->i_blocks = calc_inode_blocks(size);
620                 ci->i_reported_size = size;
621                 if (truncate_seq != ci->i_truncate_seq) {
622                         dout("truncate_seq %u -> %u\n",
623                              ci->i_truncate_seq, truncate_seq);
624                         ci->i_truncate_seq = truncate_seq;
625
626                         /* the MDS should have revoked these caps */
627                         WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
628                                                CEPH_CAP_FILE_RD |
629                                                CEPH_CAP_FILE_WR |
630                                                CEPH_CAP_FILE_LAZYIO));
631                         /*
632                          * If we hold relevant caps, or in the case where we're
633                          * not the only client referencing this file and we
634                          * don't hold those caps, then we need to check whether
635                          * the file is either opened or mmaped
636                          */
637                         if ((issued & (CEPH_CAP_FILE_CACHE|
638                                        CEPH_CAP_FILE_BUFFER)) ||
639                             mapping_mapped(inode->i_mapping) ||
640                             __ceph_caps_file_wanted(ci)) {
641                                 ci->i_truncate_pending++;
642                                 queue_trunc = 1;
643                         }
644                 }
645         }
646         if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
647             ci->i_truncate_size != truncate_size) {
648                 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
649                      truncate_size);
650                 ci->i_truncate_size = truncate_size;
651         }
652
653         if (queue_trunc)
654                 ceph_fscache_invalidate(inode);
655
656         return queue_trunc;
657 }
658
659 void ceph_fill_file_time(struct inode *inode, int issued,
660                          u64 time_warp_seq, struct timespec64 *ctime,
661                          struct timespec64 *mtime, struct timespec64 *atime)
662 {
663         struct ceph_inode_info *ci = ceph_inode(inode);
664         int warn = 0;
665
666         if (issued & (CEPH_CAP_FILE_EXCL|
667                       CEPH_CAP_FILE_WR|
668                       CEPH_CAP_FILE_BUFFER|
669                       CEPH_CAP_AUTH_EXCL|
670                       CEPH_CAP_XATTR_EXCL)) {
671                 if (ci->i_version == 0 ||
672                     timespec64_compare(ctime, &inode->i_ctime) > 0) {
673                         dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
674                              inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
675                              ctime->tv_sec, ctime->tv_nsec);
676                         inode->i_ctime = *ctime;
677                 }
678                 if (ci->i_version == 0 ||
679                     ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
680                         /* the MDS did a utimes() */
681                         dout("mtime %lld.%09ld -> %lld.%09ld "
682                              "tw %d -> %d\n",
683                              inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
684                              mtime->tv_sec, mtime->tv_nsec,
685                              ci->i_time_warp_seq, (int)time_warp_seq);
686
687                         inode->i_mtime = *mtime;
688                         inode->i_atime = *atime;
689                         ci->i_time_warp_seq = time_warp_seq;
690                 } else if (time_warp_seq == ci->i_time_warp_seq) {
691                         /* nobody did utimes(); take the max */
692                         if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
693                                 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
694                                      inode->i_mtime.tv_sec,
695                                      inode->i_mtime.tv_nsec,
696                                      mtime->tv_sec, mtime->tv_nsec);
697                                 inode->i_mtime = *mtime;
698                         }
699                         if (timespec64_compare(atime, &inode->i_atime) > 0) {
700                                 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
701                                      inode->i_atime.tv_sec,
702                                      inode->i_atime.tv_nsec,
703                                      atime->tv_sec, atime->tv_nsec);
704                                 inode->i_atime = *atime;
705                         }
706                 } else if (issued & CEPH_CAP_FILE_EXCL) {
707                         /* we did a utimes(); ignore mds values */
708                 } else {
709                         warn = 1;
710                 }
711         } else {
712                 /* we have no write|excl caps; whatever the MDS says is true */
713                 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
714                         inode->i_ctime = *ctime;
715                         inode->i_mtime = *mtime;
716                         inode->i_atime = *atime;
717                         ci->i_time_warp_seq = time_warp_seq;
718                 } else {
719                         warn = 1;
720                 }
721         }
722         if (warn) /* time_warp_seq shouldn't go backwards */
723                 dout("%p mds time_warp_seq %llu < %u\n",
724                      inode, time_warp_seq, ci->i_time_warp_seq);
725 }
726
727 /*
728  * Populate an inode based on info from mds.  May be called on new or
729  * existing inodes.
730  */
731 static int fill_inode(struct inode *inode, struct page *locked_page,
732                       struct ceph_mds_reply_info_in *iinfo,
733                       struct ceph_mds_reply_dirfrag *dirinfo,
734                       struct ceph_mds_session *session, int cap_fmode,
735                       struct ceph_cap_reservation *caps_reservation)
736 {
737         struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
738         struct ceph_mds_reply_inode *info = iinfo->in;
739         struct ceph_inode_info *ci = ceph_inode(inode);
740         int issued, new_issued, info_caps;
741         struct timespec64 mtime, atime, ctime;
742         struct ceph_buffer *xattr_blob = NULL;
743         struct ceph_buffer *old_blob = NULL;
744         struct ceph_string *pool_ns = NULL;
745         struct ceph_cap *new_cap = NULL;
746         int err = 0;
747         bool wake = false;
748         bool queue_trunc = false;
749         bool new_version = false;
750         bool fill_inline = false;
751
752         dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
753              inode, ceph_vinop(inode), le64_to_cpu(info->version),
754              ci->i_version);
755
756         info_caps = le32_to_cpu(info->cap.caps);
757
758         /* prealloc new cap struct */
759         if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
760                 new_cap = ceph_get_cap(mdsc, caps_reservation);
761                 if (!new_cap)
762                         return -ENOMEM;
763         }
764
765         /*
766          * prealloc xattr data, if it looks like we'll need it.  only
767          * if len > 4 (meaning there are actually xattrs; the first 4
768          * bytes are the xattr count).
769          */
770         if (iinfo->xattr_len > 4) {
771                 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
772                 if (!xattr_blob)
773                         pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
774                                iinfo->xattr_len);
775         }
776
777         if (iinfo->pool_ns_len > 0)
778                 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
779                                                      iinfo->pool_ns_len);
780
781         if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
782                 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
783
784         spin_lock(&ci->i_ceph_lock);
785
786         /*
787          * provided version will be odd if inode value is projected,
788          * even if stable.  skip the update if we have newer stable
789          * info (ours>=theirs, e.g. due to racing mds replies), unless
790          * we are getting projected (unstable) info (in which case the
791          * version is odd, and we want ours>theirs).
792          *   us   them
793          *   2    2     skip
794          *   3    2     skip
795          *   3    3     update
796          */
797         if (ci->i_version == 0 ||
798             ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
799              le64_to_cpu(info->version) > (ci->i_version & ~1)))
800                 new_version = true;
801
802         /* Update change_attribute */
803         inode_set_max_iversion_raw(inode, iinfo->change_attr);
804
805         __ceph_caps_issued(ci, &issued);
806         issued |= __ceph_caps_dirty(ci);
807         new_issued = ~issued & info_caps;
808
809         /* update inode */
810         inode->i_rdev = le32_to_cpu(info->rdev);
811         /* directories have fl_stripe_unit set to zero */
812         if (le32_to_cpu(info->layout.fl_stripe_unit))
813                 inode->i_blkbits =
814                         fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
815         else
816                 inode->i_blkbits = CEPH_BLOCK_SHIFT;
817
818         __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
819
820         if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
821             (issued & CEPH_CAP_AUTH_EXCL) == 0) {
822                 inode->i_mode = le32_to_cpu(info->mode);
823                 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
824                 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
825                 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
826                      from_kuid(&init_user_ns, inode->i_uid),
827                      from_kgid(&init_user_ns, inode->i_gid));
828                 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
829                 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
830         }
831
832         if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
833             (issued & CEPH_CAP_LINK_EXCL) == 0)
834                 set_nlink(inode, le32_to_cpu(info->nlink));
835
836         if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
837                 /* be careful with mtime, atime, size */
838                 ceph_decode_timespec64(&atime, &info->atime);
839                 ceph_decode_timespec64(&mtime, &info->mtime);
840                 ceph_decode_timespec64(&ctime, &info->ctime);
841                 ceph_fill_file_time(inode, issued,
842                                 le32_to_cpu(info->time_warp_seq),
843                                 &ctime, &mtime, &atime);
844         }
845
846         if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
847                 ci->i_files = le64_to_cpu(info->files);
848                 ci->i_subdirs = le64_to_cpu(info->subdirs);
849         }
850
851         if (new_version ||
852             (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
853                 s64 old_pool = ci->i_layout.pool_id;
854                 struct ceph_string *old_ns;
855
856                 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
857                 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
858                                         lockdep_is_held(&ci->i_ceph_lock));
859                 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
860
861                 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
862                         ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
863
864                 pool_ns = old_ns;
865
866                 queue_trunc = ceph_fill_file_size(inode, issued,
867                                         le32_to_cpu(info->truncate_seq),
868                                         le64_to_cpu(info->truncate_size),
869                                         le64_to_cpu(info->size));
870                 /* only update max_size on auth cap */
871                 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
872                     ci->i_max_size != le64_to_cpu(info->max_size)) {
873                         dout("max_size %lld -> %llu\n", ci->i_max_size,
874                                         le64_to_cpu(info->max_size));
875                         ci->i_max_size = le64_to_cpu(info->max_size);
876                 }
877         }
878
879         /* layout and rstat are not tracked by capability, update them if
880          * the inode info is from auth mds */
881         if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
882                 if (S_ISDIR(inode->i_mode)) {
883                         ci->i_dir_layout = iinfo->dir_layout;
884                         ci->i_rbytes = le64_to_cpu(info->rbytes);
885                         ci->i_rfiles = le64_to_cpu(info->rfiles);
886                         ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
887                         ci->i_dir_pin = iinfo->dir_pin;
888                         ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
889                 }
890         }
891
892         /* xattrs */
893         /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
894         if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))  &&
895             le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
896                 if (ci->i_xattrs.blob)
897                         old_blob = ci->i_xattrs.blob;
898                 ci->i_xattrs.blob = xattr_blob;
899                 if (xattr_blob)
900                         memcpy(ci->i_xattrs.blob->vec.iov_base,
901                                iinfo->xattr_data, iinfo->xattr_len);
902                 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
903                 ceph_forget_all_cached_acls(inode);
904                 ceph_security_invalidate_secctx(inode);
905                 xattr_blob = NULL;
906         }
907
908         /* finally update i_version */
909         if (le64_to_cpu(info->version) > ci->i_version)
910                 ci->i_version = le64_to_cpu(info->version);
911
912         inode->i_mapping->a_ops = &ceph_aops;
913
914         switch (inode->i_mode & S_IFMT) {
915         case S_IFIFO:
916         case S_IFBLK:
917         case S_IFCHR:
918         case S_IFSOCK:
919                 inode->i_blkbits = PAGE_SHIFT;
920                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
921                 inode->i_op = &ceph_file_iops;
922                 break;
923         case S_IFREG:
924                 inode->i_op = &ceph_file_iops;
925                 inode->i_fop = &ceph_file_fops;
926                 break;
927         case S_IFLNK:
928                 inode->i_op = &ceph_symlink_iops;
929                 if (!ci->i_symlink) {
930                         u32 symlen = iinfo->symlink_len;
931                         char *sym;
932
933                         spin_unlock(&ci->i_ceph_lock);
934
935                         if (symlen != i_size_read(inode)) {
936                                 pr_err("fill_inode %llx.%llx BAD symlink "
937                                         "size %lld\n", ceph_vinop(inode),
938                                         i_size_read(inode));
939                                 i_size_write(inode, symlen);
940                                 inode->i_blocks = calc_inode_blocks(symlen);
941                         }
942
943                         err = -ENOMEM;
944                         sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
945                         if (!sym)
946                                 goto out;
947
948                         spin_lock(&ci->i_ceph_lock);
949                         if (!ci->i_symlink)
950                                 ci->i_symlink = sym;
951                         else
952                                 kfree(sym); /* lost a race */
953                 }
954                 inode->i_link = ci->i_symlink;
955                 break;
956         case S_IFDIR:
957                 inode->i_op = &ceph_dir_iops;
958                 inode->i_fop = &ceph_dir_fops;
959                 break;
960         default:
961                 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
962                        ceph_vinop(inode), inode->i_mode);
963         }
964
965         /* were we issued a capability? */
966         if (info_caps) {
967                 if (ceph_snap(inode) == CEPH_NOSNAP) {
968                         ceph_add_cap(inode, session,
969                                      le64_to_cpu(info->cap.cap_id),
970                                      cap_fmode, info_caps,
971                                      le32_to_cpu(info->cap.wanted),
972                                      le32_to_cpu(info->cap.seq),
973                                      le32_to_cpu(info->cap.mseq),
974                                      le64_to_cpu(info->cap.realm),
975                                      info->cap.flags, &new_cap);
976
977                         /* set dir completion flag? */
978                         if (S_ISDIR(inode->i_mode) &&
979                             ci->i_files == 0 && ci->i_subdirs == 0 &&
980                             (info_caps & CEPH_CAP_FILE_SHARED) &&
981                             (issued & CEPH_CAP_FILE_EXCL) == 0 &&
982                             !__ceph_dir_is_complete(ci)) {
983                                 dout(" marking %p complete (empty)\n", inode);
984                                 i_size_write(inode, 0);
985                                 __ceph_dir_set_complete(ci,
986                                         atomic64_read(&ci->i_release_count),
987                                         atomic64_read(&ci->i_ordered_count));
988                         }
989
990                         wake = true;
991                 } else {
992                         dout(" %p got snap_caps %s\n", inode,
993                              ceph_cap_string(info_caps));
994                         ci->i_snap_caps |= info_caps;
995                         if (cap_fmode >= 0)
996                                 __ceph_get_fmode(ci, cap_fmode);
997                 }
998         } else if (cap_fmode >= 0) {
999                 pr_warn("mds issued no caps on %llx.%llx\n",
1000                            ceph_vinop(inode));
1001                 __ceph_get_fmode(ci, cap_fmode);
1002         }
1003
1004         if (iinfo->inline_version > 0 &&
1005             iinfo->inline_version >= ci->i_inline_version) {
1006                 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1007                 ci->i_inline_version = iinfo->inline_version;
1008                 if (ci->i_inline_version != CEPH_INLINE_NONE &&
1009                     (locked_page || (info_caps & cache_caps)))
1010                         fill_inline = true;
1011         }
1012
1013         spin_unlock(&ci->i_ceph_lock);
1014
1015         if (fill_inline)
1016                 ceph_fill_inline_data(inode, locked_page,
1017                                       iinfo->inline_data, iinfo->inline_len);
1018
1019         if (wake)
1020                 wake_up_all(&ci->i_cap_wq);
1021
1022         /* queue truncate if we saw i_size decrease */
1023         if (queue_trunc)
1024                 ceph_queue_vmtruncate(inode);
1025
1026         /* populate frag tree */
1027         if (S_ISDIR(inode->i_mode))
1028                 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1029
1030         /* update delegation info? */
1031         if (dirinfo)
1032                 ceph_fill_dirfrag(inode, dirinfo);
1033
1034         err = 0;
1035 out:
1036         if (new_cap)
1037                 ceph_put_cap(mdsc, new_cap);
1038         ceph_buffer_put(old_blob);
1039         ceph_buffer_put(xattr_blob);
1040         ceph_put_string(pool_ns);
1041         return err;
1042 }
1043
1044 /*
1045  * caller should hold session s_mutex and dentry->d_lock.
1046  */
1047 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1048                                   struct ceph_mds_reply_lease *lease,
1049                                   struct ceph_mds_session *session,
1050                                   unsigned long from_time,
1051                                   struct ceph_mds_session **old_lease_session)
1052 {
1053         struct ceph_dentry_info *di = ceph_dentry(dentry);
1054         long unsigned duration = le32_to_cpu(lease->duration_ms);
1055         long unsigned ttl = from_time + (duration * HZ) / 1000;
1056         long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1057
1058         dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1059              dentry, duration, ttl);
1060
1061         /* only track leases on regular dentries */
1062         if (ceph_snap(dir) != CEPH_NOSNAP)
1063                 return;
1064
1065         di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1066         if (duration == 0) {
1067                 __ceph_dentry_dir_lease_touch(di);
1068                 return;
1069         }
1070
1071         if (di->lease_gen == session->s_cap_gen &&
1072             time_before(ttl, di->time))
1073                 return;  /* we already have a newer lease. */
1074
1075         if (di->lease_session && di->lease_session != session) {
1076                 *old_lease_session = di->lease_session;
1077                 di->lease_session = NULL;
1078         }
1079
1080         if (!di->lease_session)
1081                 di->lease_session = ceph_get_mds_session(session);
1082         di->lease_gen = session->s_cap_gen;
1083         di->lease_seq = le32_to_cpu(lease->seq);
1084         di->lease_renew_after = half_ttl;
1085         di->lease_renew_from = 0;
1086         di->time = ttl;
1087
1088         __ceph_dentry_lease_touch(di);
1089 }
1090
1091 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1092                                         struct ceph_mds_reply_lease *lease,
1093                                         struct ceph_mds_session *session,
1094                                         unsigned long from_time)
1095 {
1096         struct ceph_mds_session *old_lease_session = NULL;
1097         spin_lock(&dentry->d_lock);
1098         __update_dentry_lease(dir, dentry, lease, session, from_time,
1099                               &old_lease_session);
1100         spin_unlock(&dentry->d_lock);
1101         if (old_lease_session)
1102                 ceph_put_mds_session(old_lease_session);
1103 }
1104
1105 /*
1106  * update dentry lease without having parent inode locked
1107  */
1108 static void update_dentry_lease_careful(struct dentry *dentry,
1109                                         struct ceph_mds_reply_lease *lease,
1110                                         struct ceph_mds_session *session,
1111                                         unsigned long from_time,
1112                                         char *dname, u32 dname_len,
1113                                         struct ceph_vino *pdvino,
1114                                         struct ceph_vino *ptvino)
1115
1116 {
1117         struct inode *dir;
1118         struct ceph_mds_session *old_lease_session = NULL;
1119
1120         spin_lock(&dentry->d_lock);
1121         /* make sure dentry's name matches target */
1122         if (dentry->d_name.len != dname_len ||
1123             memcmp(dentry->d_name.name, dname, dname_len))
1124                 goto out_unlock;
1125
1126         dir = d_inode(dentry->d_parent);
1127         /* make sure parent matches dvino */
1128         if (!ceph_ino_compare(dir, pdvino))
1129                 goto out_unlock;
1130
1131         /* make sure dentry's inode matches target. NULL ptvino means that
1132          * we expect a negative dentry */
1133         if (ptvino) {
1134                 if (d_really_is_negative(dentry))
1135                         goto out_unlock;
1136                 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1137                         goto out_unlock;
1138         } else {
1139                 if (d_really_is_positive(dentry))
1140                         goto out_unlock;
1141         }
1142
1143         __update_dentry_lease(dir, dentry, lease, session,
1144                               from_time, &old_lease_session);
1145 out_unlock:
1146         spin_unlock(&dentry->d_lock);
1147         if (old_lease_session)
1148                 ceph_put_mds_session(old_lease_session);
1149 }
1150
1151 /*
1152  * splice a dentry to an inode.
1153  * caller must hold directory i_mutex for this to be safe.
1154  */
1155 static int splice_dentry(struct dentry **pdn, struct inode *in)
1156 {
1157         struct dentry *dn = *pdn;
1158         struct dentry *realdn;
1159
1160         BUG_ON(d_inode(dn));
1161
1162         if (S_ISDIR(in->i_mode)) {
1163                 /* If inode is directory, d_splice_alias() below will remove
1164                  * 'realdn' from its origin parent. We need to ensure that
1165                  * origin parent's readdir cache will not reference 'realdn'
1166                  */
1167                 realdn = d_find_any_alias(in);
1168                 if (realdn) {
1169                         struct ceph_dentry_info *di = ceph_dentry(realdn);
1170                         spin_lock(&realdn->d_lock);
1171
1172                         realdn->d_op->d_prune(realdn);
1173
1174                         di->time = jiffies;
1175                         di->lease_shared_gen = 0;
1176                         di->offset = 0;
1177
1178                         spin_unlock(&realdn->d_lock);
1179                         dput(realdn);
1180                 }
1181         }
1182
1183         /* dn must be unhashed */
1184         if (!d_unhashed(dn))
1185                 d_drop(dn);
1186         realdn = d_splice_alias(in, dn);
1187         if (IS_ERR(realdn)) {
1188                 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1189                        PTR_ERR(realdn), dn, in, ceph_vinop(in));
1190                 return PTR_ERR(realdn);
1191         }
1192
1193         if (realdn) {
1194                 dout("dn %p (%d) spliced with %p (%d) "
1195                      "inode %p ino %llx.%llx\n",
1196                      dn, d_count(dn),
1197                      realdn, d_count(realdn),
1198                      d_inode(realdn), ceph_vinop(d_inode(realdn)));
1199                 dput(dn);
1200                 *pdn = realdn;
1201         } else {
1202                 BUG_ON(!ceph_dentry(dn));
1203                 dout("dn %p attached to %p ino %llx.%llx\n",
1204                      dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1205         }
1206         return 0;
1207 }
1208
1209 /*
1210  * Incorporate results into the local cache.  This is either just
1211  * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1212  * after a lookup).
1213  *
1214  * A reply may contain
1215  *         a directory inode along with a dentry.
1216  *  and/or a target inode
1217  *
1218  * Called with snap_rwsem (read).
1219  */
1220 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1221 {
1222         struct ceph_mds_session *session = req->r_session;
1223         struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1224         struct inode *in = NULL;
1225         struct ceph_vino tvino, dvino;
1226         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1227         int err = 0;
1228
1229         dout("fill_trace %p is_dentry %d is_target %d\n", req,
1230              rinfo->head->is_dentry, rinfo->head->is_target);
1231
1232         if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1233                 dout("fill_trace reply is empty!\n");
1234                 if (rinfo->head->result == 0 && req->r_parent)
1235                         ceph_invalidate_dir_request(req);
1236                 return 0;
1237         }
1238
1239         if (rinfo->head->is_dentry) {
1240                 struct inode *dir = req->r_parent;
1241
1242                 if (dir) {
1243                         err = fill_inode(dir, NULL,
1244                                          &rinfo->diri, rinfo->dirfrag,
1245                                          session, -1,
1246                                          &req->r_caps_reservation);
1247                         if (err < 0)
1248                                 goto done;
1249                 } else {
1250                         WARN_ON_ONCE(1);
1251                 }
1252
1253                 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1254                     test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1255                     !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1256                         struct qstr dname;
1257                         struct dentry *dn, *parent;
1258
1259                         BUG_ON(!rinfo->head->is_target);
1260                         BUG_ON(req->r_dentry);
1261
1262                         parent = d_find_any_alias(dir);
1263                         BUG_ON(!parent);
1264
1265                         dname.name = rinfo->dname;
1266                         dname.len = rinfo->dname_len;
1267                         dname.hash = full_name_hash(parent, dname.name, dname.len);
1268                         tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1269                         tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1270 retry_lookup:
1271                         dn = d_lookup(parent, &dname);
1272                         dout("d_lookup on parent=%p name=%.*s got %p\n",
1273                              parent, dname.len, dname.name, dn);
1274
1275                         if (!dn) {
1276                                 dn = d_alloc(parent, &dname);
1277                                 dout("d_alloc %p '%.*s' = %p\n", parent,
1278                                      dname.len, dname.name, dn);
1279                                 if (!dn) {
1280                                         dput(parent);
1281                                         err = -ENOMEM;
1282                                         goto done;
1283                                 }
1284                                 err = 0;
1285                         } else if (d_really_is_positive(dn) &&
1286                                    (ceph_ino(d_inode(dn)) != tvino.ino ||
1287                                     ceph_snap(d_inode(dn)) != tvino.snap)) {
1288                                 dout(" dn %p points to wrong inode %p\n",
1289                                      dn, d_inode(dn));
1290                                 ceph_dir_clear_ordered(dir);
1291                                 d_delete(dn);
1292                                 dput(dn);
1293                                 goto retry_lookup;
1294                         }
1295
1296                         req->r_dentry = dn;
1297                         dput(parent);
1298                 }
1299         }
1300
1301         if (rinfo->head->is_target) {
1302                 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1303                 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1304
1305                 in = ceph_get_inode(sb, tvino);
1306                 if (IS_ERR(in)) {
1307                         err = PTR_ERR(in);
1308                         goto done;
1309                 }
1310
1311                 err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL,
1312                                 session,
1313                                 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1314                                  rinfo->head->result == 0) ?  req->r_fmode : -1,
1315                                 &req->r_caps_reservation);
1316                 if (err < 0) {
1317                         pr_err("fill_inode badness %p %llx.%llx\n",
1318                                 in, ceph_vinop(in));
1319                         if (in->i_state & I_NEW)
1320                                 discard_new_inode(in);
1321                         goto done;
1322                 }
1323                 req->r_target_inode = in;
1324                 if (in->i_state & I_NEW)
1325                         unlock_new_inode(in);
1326         }
1327
1328         /*
1329          * ignore null lease/binding on snapdir ENOENT, or else we
1330          * will have trouble splicing in the virtual snapdir later
1331          */
1332         if (rinfo->head->is_dentry &&
1333             !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1334             test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1335             (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1336                                                fsc->mount_options->snapdir_name,
1337                                                req->r_dentry->d_name.len))) {
1338                 /*
1339                  * lookup link rename   : null -> possibly existing inode
1340                  * mknod symlink mkdir  : null -> new inode
1341                  * unlink               : linked -> null
1342                  */
1343                 struct inode *dir = req->r_parent;
1344                 struct dentry *dn = req->r_dentry;
1345                 bool have_dir_cap, have_lease;
1346
1347                 BUG_ON(!dn);
1348                 BUG_ON(!dir);
1349                 BUG_ON(d_inode(dn->d_parent) != dir);
1350
1351                 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1352                 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1353
1354                 BUG_ON(ceph_ino(dir) != dvino.ino);
1355                 BUG_ON(ceph_snap(dir) != dvino.snap);
1356
1357                 /* do we have a lease on the whole dir? */
1358                 have_dir_cap =
1359                         (le32_to_cpu(rinfo->diri.in->cap.caps) &
1360                          CEPH_CAP_FILE_SHARED);
1361
1362                 /* do we have a dn lease? */
1363                 have_lease = have_dir_cap ||
1364                         le32_to_cpu(rinfo->dlease->duration_ms);
1365                 if (!have_lease)
1366                         dout("fill_trace  no dentry lease or dir cap\n");
1367
1368                 /* rename? */
1369                 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1370                         struct inode *olddir = req->r_old_dentry_dir;
1371                         BUG_ON(!olddir);
1372
1373                         dout(" src %p '%pd' dst %p '%pd'\n",
1374                              req->r_old_dentry,
1375                              req->r_old_dentry,
1376                              dn, dn);
1377                         dout("fill_trace doing d_move %p -> %p\n",
1378                              req->r_old_dentry, dn);
1379
1380                         /* d_move screws up sibling dentries' offsets */
1381                         ceph_dir_clear_ordered(dir);
1382                         ceph_dir_clear_ordered(olddir);
1383
1384                         d_move(req->r_old_dentry, dn);
1385                         dout(" src %p '%pd' dst %p '%pd'\n",
1386                              req->r_old_dentry,
1387                              req->r_old_dentry,
1388                              dn, dn);
1389
1390                         /* ensure target dentry is invalidated, despite
1391                            rehashing bug in vfs_rename_dir */
1392                         ceph_invalidate_dentry_lease(dn);
1393
1394                         dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1395                              ceph_dentry(req->r_old_dentry)->offset);
1396
1397                         /* swap r_dentry and r_old_dentry in case that
1398                          * splice_dentry() gets called later. This is safe
1399                          * because no other place will use them */
1400                         req->r_dentry = req->r_old_dentry;
1401                         req->r_old_dentry = dn;
1402                         dn = req->r_dentry;
1403                 }
1404
1405                 /* null dentry? */
1406                 if (!rinfo->head->is_target) {
1407                         dout("fill_trace null dentry\n");
1408                         if (d_really_is_positive(dn)) {
1409                                 dout("d_delete %p\n", dn);
1410                                 ceph_dir_clear_ordered(dir);
1411                                 d_delete(dn);
1412                         } else if (have_lease) {
1413                                 if (d_unhashed(dn))
1414                                         d_add(dn, NULL);
1415                                 update_dentry_lease(dir, dn,
1416                                                     rinfo->dlease, session,
1417                                                     req->r_request_started);
1418                         }
1419                         goto done;
1420                 }
1421
1422                 /* attach proper inode */
1423                 if (d_really_is_negative(dn)) {
1424                         ceph_dir_clear_ordered(dir);
1425                         ihold(in);
1426                         err = splice_dentry(&req->r_dentry, in);
1427                         if (err < 0)
1428                                 goto done;
1429                         dn = req->r_dentry;  /* may have spliced */
1430                 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1431                         dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1432                              dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1433                              ceph_vinop(in));
1434                         d_invalidate(dn);
1435                         have_lease = false;
1436                 }
1437
1438                 if (have_lease) {
1439                         update_dentry_lease(dir, dn,
1440                                             rinfo->dlease, session,
1441                                             req->r_request_started);
1442                 }
1443                 dout(" final dn %p\n", dn);
1444         } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1445                     req->r_op == CEPH_MDS_OP_MKSNAP) &&
1446                    test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1447                    !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1448                 struct inode *dir = req->r_parent;
1449
1450                 /* fill out a snapdir LOOKUPSNAP dentry */
1451                 BUG_ON(!dir);
1452                 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1453                 BUG_ON(!req->r_dentry);
1454                 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1455                 ceph_dir_clear_ordered(dir);
1456                 ihold(in);
1457                 err = splice_dentry(&req->r_dentry, in);
1458                 if (err < 0)
1459                         goto done;
1460         } else if (rinfo->head->is_dentry && req->r_dentry) {
1461                 /* parent inode is not locked, be carefull */
1462                 struct ceph_vino *ptvino = NULL;
1463                 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1464                 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1465                 if (rinfo->head->is_target) {
1466                         tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1467                         tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1468                         ptvino = &tvino;
1469                 }
1470                 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1471                                             session, req->r_request_started,
1472                                             rinfo->dname, rinfo->dname_len,
1473                                             &dvino, ptvino);
1474         }
1475 done:
1476         dout("fill_trace done err=%d\n", err);
1477         return err;
1478 }
1479
1480 /*
1481  * Prepopulate our cache with readdir results, leases, etc.
1482  */
1483 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1484                                            struct ceph_mds_session *session)
1485 {
1486         struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1487         int i, err = 0;
1488
1489         for (i = 0; i < rinfo->dir_nr; i++) {
1490                 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1491                 struct ceph_vino vino;
1492                 struct inode *in;
1493                 int rc;
1494
1495                 vino.ino = le64_to_cpu(rde->inode.in->ino);
1496                 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1497
1498                 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1499                 if (IS_ERR(in)) {
1500                         err = PTR_ERR(in);
1501                         dout("new_inode badness got %d\n", err);
1502                         continue;
1503                 }
1504                 rc = fill_inode(in, NULL, &rde->inode, NULL, session,
1505                                 -1, &req->r_caps_reservation);
1506                 if (rc < 0) {
1507                         pr_err("fill_inode badness on %p got %d\n", in, rc);
1508                         err = rc;
1509                         if (in->i_state & I_NEW) {
1510                                 ihold(in);
1511                                 discard_new_inode(in);
1512                         }
1513                 } else if (in->i_state & I_NEW) {
1514                         unlock_new_inode(in);
1515                 }
1516
1517                 /* avoid calling iput_final() in mds dispatch threads */
1518                 ceph_async_iput(in);
1519         }
1520
1521         return err;
1522 }
1523
1524 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1525 {
1526         if (ctl->page) {
1527                 kunmap(ctl->page);
1528                 put_page(ctl->page);
1529                 ctl->page = NULL;
1530         }
1531 }
1532
1533 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1534                               struct ceph_readdir_cache_control *ctl,
1535                               struct ceph_mds_request *req)
1536 {
1537         struct ceph_inode_info *ci = ceph_inode(dir);
1538         unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1539         unsigned idx = ctl->index % nsize;
1540         pgoff_t pgoff = ctl->index / nsize;
1541
1542         if (!ctl->page || pgoff != page_index(ctl->page)) {
1543                 ceph_readdir_cache_release(ctl);
1544                 if (idx == 0)
1545                         ctl->page = grab_cache_page(&dir->i_data, pgoff);
1546                 else
1547                         ctl->page = find_lock_page(&dir->i_data, pgoff);
1548                 if (!ctl->page) {
1549                         ctl->index = -1;
1550                         return idx == 0 ? -ENOMEM : 0;
1551                 }
1552                 /* reading/filling the cache are serialized by
1553                  * i_mutex, no need to use page lock */
1554                 unlock_page(ctl->page);
1555                 ctl->dentries = kmap(ctl->page);
1556                 if (idx == 0)
1557                         memset(ctl->dentries, 0, PAGE_SIZE);
1558         }
1559
1560         if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1561             req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1562                 dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1563                 ctl->dentries[idx] = dn;
1564                 ctl->index++;
1565         } else {
1566                 dout("disable readdir cache\n");
1567                 ctl->index = -1;
1568         }
1569         return 0;
1570 }
1571
1572 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1573                              struct ceph_mds_session *session)
1574 {
1575         struct dentry *parent = req->r_dentry;
1576         struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
1577         struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1578         struct qstr dname;
1579         struct dentry *dn;
1580         struct inode *in;
1581         int err = 0, skipped = 0, ret, i;
1582         struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1583         u32 frag = le32_to_cpu(rhead->args.readdir.frag);
1584         u32 last_hash = 0;
1585         u32 fpos_offset;
1586         struct ceph_readdir_cache_control cache_ctl = {};
1587
1588         if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1589                 return readdir_prepopulate_inodes_only(req, session);
1590
1591         if (rinfo->hash_order) {
1592                 if (req->r_path2) {
1593                         last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1594                                                   req->r_path2,
1595                                                   strlen(req->r_path2));
1596                         last_hash = ceph_frag_value(last_hash);
1597                 } else if (rinfo->offset_hash) {
1598                         /* mds understands offset_hash */
1599                         WARN_ON_ONCE(req->r_readdir_offset != 2);
1600                         last_hash = le32_to_cpu(rhead->args.readdir.offset_hash);
1601                 }
1602         }
1603
1604         if (rinfo->dir_dir &&
1605             le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1606                 dout("readdir_prepopulate got new frag %x -> %x\n",
1607                      frag, le32_to_cpu(rinfo->dir_dir->frag));
1608                 frag = le32_to_cpu(rinfo->dir_dir->frag);
1609                 if (!rinfo->hash_order)
1610                         req->r_readdir_offset = 2;
1611         }
1612
1613         if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1614                 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1615                      rinfo->dir_nr, parent);
1616         } else {
1617                 dout("readdir_prepopulate %d items under dn %p\n",
1618                      rinfo->dir_nr, parent);
1619                 if (rinfo->dir_dir)
1620                         ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1621
1622                 if (ceph_frag_is_leftmost(frag) &&
1623                     req->r_readdir_offset == 2 &&
1624                     !(rinfo->hash_order && last_hash)) {
1625                         /* note dir version at start of readdir so we can
1626                          * tell if any dentries get dropped */
1627                         req->r_dir_release_cnt =
1628                                 atomic64_read(&ci->i_release_count);
1629                         req->r_dir_ordered_cnt =
1630                                 atomic64_read(&ci->i_ordered_count);
1631                         req->r_readdir_cache_idx = 0;
1632                 }
1633         }
1634
1635         cache_ctl.index = req->r_readdir_cache_idx;
1636         fpos_offset = req->r_readdir_offset;
1637
1638         /* FIXME: release caps/leases if error occurs */
1639         for (i = 0; i < rinfo->dir_nr; i++) {
1640                 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1641                 struct ceph_vino tvino;
1642
1643                 dname.name = rde->name;
1644                 dname.len = rde->name_len;
1645                 dname.hash = full_name_hash(parent, dname.name, dname.len);
1646
1647                 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1648                 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1649
1650                 if (rinfo->hash_order) {
1651                         u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1652                                                  rde->name, rde->name_len);
1653                         hash = ceph_frag_value(hash);
1654                         if (hash != last_hash)
1655                                 fpos_offset = 2;
1656                         last_hash = hash;
1657                         rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1658                 } else {
1659                         rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1660                 }
1661
1662 retry_lookup:
1663                 dn = d_lookup(parent, &dname);
1664                 dout("d_lookup on parent=%p name=%.*s got %p\n",
1665                      parent, dname.len, dname.name, dn);
1666
1667                 if (!dn) {
1668                         dn = d_alloc(parent, &dname);
1669                         dout("d_alloc %p '%.*s' = %p\n", parent,
1670                              dname.len, dname.name, dn);
1671                         if (!dn) {
1672                                 dout("d_alloc badness\n");
1673                                 err = -ENOMEM;
1674                                 goto out;
1675                         }
1676                 } else if (d_really_is_positive(dn) &&
1677                            (ceph_ino(d_inode(dn)) != tvino.ino ||
1678                             ceph_snap(d_inode(dn)) != tvino.snap)) {
1679                         struct ceph_dentry_info *di = ceph_dentry(dn);
1680                         dout(" dn %p points to wrong inode %p\n",
1681                              dn, d_inode(dn));
1682
1683                         spin_lock(&dn->d_lock);
1684                         if (di->offset > 0 &&
1685                             di->lease_shared_gen ==
1686                             atomic_read(&ci->i_shared_gen)) {
1687                                 __ceph_dir_clear_ordered(ci);
1688                                 di->offset = 0;
1689                         }
1690                         spin_unlock(&dn->d_lock);
1691
1692                         d_delete(dn);
1693                         dput(dn);
1694                         goto retry_lookup;
1695                 }
1696
1697                 /* inode */
1698                 if (d_really_is_positive(dn)) {
1699                         in = d_inode(dn);
1700                 } else {
1701                         in = ceph_get_inode(parent->d_sb, tvino);
1702                         if (IS_ERR(in)) {
1703                                 dout("new_inode badness\n");
1704                                 d_drop(dn);
1705                                 dput(dn);
1706                                 err = PTR_ERR(in);
1707                                 goto out;
1708                         }
1709                 }
1710
1711                 ret = fill_inode(in, NULL, &rde->inode, NULL, session,
1712                                  -1, &req->r_caps_reservation);
1713                 if (ret < 0) {
1714                         pr_err("fill_inode badness on %p\n", in);
1715                         if (d_really_is_negative(dn)) {
1716                                 /* avoid calling iput_final() in mds
1717                                  * dispatch threads */
1718                                 if (in->i_state & I_NEW) {
1719                                         ihold(in);
1720                                         discard_new_inode(in);
1721                                 }
1722                                 ceph_async_iput(in);
1723                         }
1724                         d_drop(dn);
1725                         err = ret;
1726                         goto next_item;
1727                 }
1728                 if (in->i_state & I_NEW)
1729                         unlock_new_inode(in);
1730
1731                 if (d_really_is_negative(dn)) {
1732                         if (ceph_security_xattr_deadlock(in)) {
1733                                 dout(" skip splicing dn %p to inode %p"
1734                                      " (security xattr deadlock)\n", dn, in);
1735                                 ceph_async_iput(in);
1736                                 skipped++;
1737                                 goto next_item;
1738                         }
1739
1740                         err = splice_dentry(&dn, in);
1741                         if (err < 0)
1742                                 goto next_item;
1743                 }
1744
1745                 ceph_dentry(dn)->offset = rde->offset;
1746
1747                 update_dentry_lease(d_inode(parent), dn,
1748                                     rde->lease, req->r_session,
1749                                     req->r_request_started);
1750
1751                 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
1752                         ret = fill_readdir_cache(d_inode(parent), dn,
1753                                                  &cache_ctl, req);
1754                         if (ret < 0)
1755                                 err = ret;
1756                 }
1757 next_item:
1758                 dput(dn);
1759         }
1760 out:
1761         if (err == 0 && skipped == 0) {
1762                 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
1763                 req->r_readdir_cache_idx = cache_ctl.index;
1764         }
1765         ceph_readdir_cache_release(&cache_ctl);
1766         dout("readdir_prepopulate done\n");
1767         return err;
1768 }
1769
1770 bool ceph_inode_set_size(struct inode *inode, loff_t size)
1771 {
1772         struct ceph_inode_info *ci = ceph_inode(inode);
1773         bool ret;
1774
1775         spin_lock(&ci->i_ceph_lock);
1776         dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1777         i_size_write(inode, size);
1778         inode->i_blocks = calc_inode_blocks(size);
1779
1780         ret = __ceph_should_report_size(ci);
1781
1782         spin_unlock(&ci->i_ceph_lock);
1783         return ret;
1784 }
1785
1786 /*
1787  * Put reference to inode, but avoid calling iput_final() in current thread.
1788  * iput_final() may wait for reahahead pages. The wait can cause deadlock in
1789  * some contexts.
1790  */
1791 void ceph_async_iput(struct inode *inode)
1792 {
1793         if (!inode)
1794                 return;
1795         for (;;) {
1796                 if (atomic_add_unless(&inode->i_count, -1, 1))
1797                         break;
1798                 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1799                                &ceph_inode(inode)->i_work))
1800                         break;
1801                 /* queue work failed, i_count must be at least 2 */
1802         }
1803 }
1804
1805 /*
1806  * Write back inode data in a worker thread.  (This can't be done
1807  * in the message handler context.)
1808  */
1809 void ceph_queue_writeback(struct inode *inode)
1810 {
1811         struct ceph_inode_info *ci = ceph_inode(inode);
1812         set_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask);
1813
1814         ihold(inode);
1815         if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1816                        &ci->i_work)) {
1817                 dout("ceph_queue_writeback %p\n", inode);
1818         } else {
1819                 dout("ceph_queue_writeback %p already queued, mask=%lx\n",
1820                      inode, ci->i_work_mask);
1821                 iput(inode);
1822         }
1823 }
1824
1825 /*
1826  * queue an async invalidation
1827  */
1828 void ceph_queue_invalidate(struct inode *inode)
1829 {
1830         struct ceph_inode_info *ci = ceph_inode(inode);
1831         set_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask);
1832
1833         ihold(inode);
1834         if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1835                        &ceph_inode(inode)->i_work)) {
1836                 dout("ceph_queue_invalidate %p\n", inode);
1837         } else {
1838                 dout("ceph_queue_invalidate %p already queued, mask=%lx\n",
1839                      inode, ci->i_work_mask);
1840                 iput(inode);
1841         }
1842 }
1843
1844 /*
1845  * Queue an async vmtruncate.  If we fail to queue work, we will handle
1846  * the truncation the next time we call __ceph_do_pending_vmtruncate.
1847  */
1848 void ceph_queue_vmtruncate(struct inode *inode)
1849 {
1850         struct ceph_inode_info *ci = ceph_inode(inode);
1851         set_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask);
1852
1853         ihold(inode);
1854         if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1855                        &ci->i_work)) {
1856                 dout("ceph_queue_vmtruncate %p\n", inode);
1857         } else {
1858                 dout("ceph_queue_vmtruncate %p already queued, mask=%lx\n",
1859                      inode, ci->i_work_mask);
1860                 iput(inode);
1861         }
1862 }
1863
1864 static void ceph_do_invalidate_pages(struct inode *inode)
1865 {
1866         struct ceph_inode_info *ci = ceph_inode(inode);
1867         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1868         u32 orig_gen;
1869         int check = 0;
1870
1871         mutex_lock(&ci->i_truncate_mutex);
1872
1873         if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1874                 pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
1875                                     inode, ceph_ino(inode));
1876                 mapping_set_error(inode->i_mapping, -EIO);
1877                 truncate_pagecache(inode, 0);
1878                 mutex_unlock(&ci->i_truncate_mutex);
1879                 goto out;
1880         }
1881
1882         spin_lock(&ci->i_ceph_lock);
1883         dout("invalidate_pages %p gen %d revoking %d\n", inode,
1884              ci->i_rdcache_gen, ci->i_rdcache_revoking);
1885         if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1886                 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1887                         check = 1;
1888                 spin_unlock(&ci->i_ceph_lock);
1889                 mutex_unlock(&ci->i_truncate_mutex);
1890                 goto out;
1891         }
1892         orig_gen = ci->i_rdcache_gen;
1893         spin_unlock(&ci->i_ceph_lock);
1894
1895         if (invalidate_inode_pages2(inode->i_mapping) < 0) {
1896                 pr_err("invalidate_pages %p fails\n", inode);
1897         }
1898
1899         spin_lock(&ci->i_ceph_lock);
1900         if (orig_gen == ci->i_rdcache_gen &&
1901             orig_gen == ci->i_rdcache_revoking) {
1902                 dout("invalidate_pages %p gen %d successful\n", inode,
1903                      ci->i_rdcache_gen);
1904                 ci->i_rdcache_revoking--;
1905                 check = 1;
1906         } else {
1907                 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1908                      inode, orig_gen, ci->i_rdcache_gen,
1909                      ci->i_rdcache_revoking);
1910                 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1911                         check = 1;
1912         }
1913         spin_unlock(&ci->i_ceph_lock);
1914         mutex_unlock(&ci->i_truncate_mutex);
1915 out:
1916         if (check)
1917                 ceph_check_caps(ci, 0, NULL);
1918 }
1919
1920 /*
1921  * Make sure any pending truncation is applied before doing anything
1922  * that may depend on it.
1923  */
1924 void __ceph_do_pending_vmtruncate(struct inode *inode)
1925 {
1926         struct ceph_inode_info *ci = ceph_inode(inode);
1927         u64 to;
1928         int wrbuffer_refs, finish = 0;
1929
1930         mutex_lock(&ci->i_truncate_mutex);
1931 retry:
1932         spin_lock(&ci->i_ceph_lock);
1933         if (ci->i_truncate_pending == 0) {
1934                 dout("__do_pending_vmtruncate %p none pending\n", inode);
1935                 spin_unlock(&ci->i_ceph_lock);
1936                 mutex_unlock(&ci->i_truncate_mutex);
1937                 return;
1938         }
1939
1940         /*
1941          * make sure any dirty snapped pages are flushed before we
1942          * possibly truncate them.. so write AND block!
1943          */
1944         if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1945                 spin_unlock(&ci->i_ceph_lock);
1946                 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1947                      inode);
1948                 filemap_write_and_wait_range(&inode->i_data, 0,
1949                                              inode->i_sb->s_maxbytes);
1950                 goto retry;
1951         }
1952
1953         /* there should be no reader or writer */
1954         WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1955
1956         to = ci->i_truncate_size;
1957         wrbuffer_refs = ci->i_wrbuffer_ref;
1958         dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1959              ci->i_truncate_pending, to);
1960         spin_unlock(&ci->i_ceph_lock);
1961
1962         truncate_pagecache(inode, to);
1963
1964         spin_lock(&ci->i_ceph_lock);
1965         if (to == ci->i_truncate_size) {
1966                 ci->i_truncate_pending = 0;
1967                 finish = 1;
1968         }
1969         spin_unlock(&ci->i_ceph_lock);
1970         if (!finish)
1971                 goto retry;
1972
1973         mutex_unlock(&ci->i_truncate_mutex);
1974
1975         if (wrbuffer_refs == 0)
1976                 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1977
1978         wake_up_all(&ci->i_cap_wq);
1979 }
1980
1981 static void ceph_inode_work(struct work_struct *work)
1982 {
1983         struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1984                                                  i_work);
1985         struct inode *inode = &ci->vfs_inode;
1986
1987         if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
1988                 dout("writeback %p\n", inode);
1989                 filemap_fdatawrite(&inode->i_data);
1990         }
1991         if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
1992                 ceph_do_invalidate_pages(inode);
1993
1994         if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
1995                 __ceph_do_pending_vmtruncate(inode);
1996
1997         iput(inode);
1998 }
1999
2000 /*
2001  * symlinks
2002  */
2003 static const struct inode_operations ceph_symlink_iops = {
2004         .get_link = simple_get_link,
2005         .setattr = ceph_setattr,
2006         .getattr = ceph_getattr,
2007         .listxattr = ceph_listxattr,
2008 };
2009
2010 int __ceph_setattr(struct inode *inode, struct iattr *attr)
2011 {
2012         struct ceph_inode_info *ci = ceph_inode(inode);
2013         unsigned int ia_valid = attr->ia_valid;
2014         struct ceph_mds_request *req;
2015         struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2016         struct ceph_cap_flush *prealloc_cf;
2017         int issued;
2018         int release = 0, dirtied = 0;
2019         int mask = 0;
2020         int err = 0;
2021         int inode_dirty_flags = 0;
2022         bool lock_snap_rwsem = false;
2023
2024         prealloc_cf = ceph_alloc_cap_flush();
2025         if (!prealloc_cf)
2026                 return -ENOMEM;
2027
2028         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2029                                        USE_AUTH_MDS);
2030         if (IS_ERR(req)) {
2031                 ceph_free_cap_flush(prealloc_cf);
2032                 return PTR_ERR(req);
2033         }
2034
2035         spin_lock(&ci->i_ceph_lock);
2036         issued = __ceph_caps_issued(ci, NULL);
2037
2038         if (!ci->i_head_snapc &&
2039             (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2040                 lock_snap_rwsem = true;
2041                 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2042                         spin_unlock(&ci->i_ceph_lock);
2043                         down_read(&mdsc->snap_rwsem);
2044                         spin_lock(&ci->i_ceph_lock);
2045                         issued = __ceph_caps_issued(ci, NULL);
2046                 }
2047         }
2048
2049         dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2050
2051         if (ia_valid & ATTR_UID) {
2052                 dout("setattr %p uid %d -> %d\n", inode,
2053                      from_kuid(&init_user_ns, inode->i_uid),
2054                      from_kuid(&init_user_ns, attr->ia_uid));
2055                 if (issued & CEPH_CAP_AUTH_EXCL) {
2056                         inode->i_uid = attr->ia_uid;
2057                         dirtied |= CEPH_CAP_AUTH_EXCL;
2058                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2059                            !uid_eq(attr->ia_uid, inode->i_uid)) {
2060                         req->r_args.setattr.uid = cpu_to_le32(
2061                                 from_kuid(&init_user_ns, attr->ia_uid));
2062                         mask |= CEPH_SETATTR_UID;
2063                         release |= CEPH_CAP_AUTH_SHARED;
2064                 }
2065         }
2066         if (ia_valid & ATTR_GID) {
2067                 dout("setattr %p gid %d -> %d\n", inode,
2068                      from_kgid(&init_user_ns, inode->i_gid),
2069                      from_kgid(&init_user_ns, attr->ia_gid));
2070                 if (issued & CEPH_CAP_AUTH_EXCL) {
2071                         inode->i_gid = attr->ia_gid;
2072                         dirtied |= CEPH_CAP_AUTH_EXCL;
2073                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2074                            !gid_eq(attr->ia_gid, inode->i_gid)) {
2075                         req->r_args.setattr.gid = cpu_to_le32(
2076                                 from_kgid(&init_user_ns, attr->ia_gid));
2077                         mask |= CEPH_SETATTR_GID;
2078                         release |= CEPH_CAP_AUTH_SHARED;
2079                 }
2080         }
2081         if (ia_valid & ATTR_MODE) {
2082                 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2083                      attr->ia_mode);
2084                 if (issued & CEPH_CAP_AUTH_EXCL) {
2085                         inode->i_mode = attr->ia_mode;
2086                         dirtied |= CEPH_CAP_AUTH_EXCL;
2087                 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2088                            attr->ia_mode != inode->i_mode) {
2089                         inode->i_mode = attr->ia_mode;
2090                         req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2091                         mask |= CEPH_SETATTR_MODE;
2092                         release |= CEPH_CAP_AUTH_SHARED;
2093                 }
2094         }
2095
2096         if (ia_valid & ATTR_ATIME) {
2097                 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2098                      inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2099                      attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2100                 if (issued & CEPH_CAP_FILE_EXCL) {
2101                         ci->i_time_warp_seq++;
2102                         inode->i_atime = attr->ia_atime;
2103                         dirtied |= CEPH_CAP_FILE_EXCL;
2104                 } else if ((issued & CEPH_CAP_FILE_WR) &&
2105                            timespec64_compare(&inode->i_atime,
2106                                             &attr->ia_atime) < 0) {
2107                         inode->i_atime = attr->ia_atime;
2108                         dirtied |= CEPH_CAP_FILE_WR;
2109                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2110                            !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2111                         ceph_encode_timespec64(&req->r_args.setattr.atime,
2112                                                &attr->ia_atime);
2113                         mask |= CEPH_SETATTR_ATIME;
2114                         release |= CEPH_CAP_FILE_SHARED |
2115                                    CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2116                 }
2117         }
2118         if (ia_valid & ATTR_SIZE) {
2119                 dout("setattr %p size %lld -> %lld\n", inode,
2120                      inode->i_size, attr->ia_size);
2121                 if ((issued & CEPH_CAP_FILE_EXCL) &&
2122                     attr->ia_size > inode->i_size) {
2123                         i_size_write(inode, attr->ia_size);
2124                         inode->i_blocks = calc_inode_blocks(attr->ia_size);
2125                         ci->i_reported_size = attr->ia_size;
2126                         dirtied |= CEPH_CAP_FILE_EXCL;
2127                         ia_valid |= ATTR_MTIME;
2128                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2129                            attr->ia_size != inode->i_size) {
2130                         req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2131                         req->r_args.setattr.old_size =
2132                                 cpu_to_le64(inode->i_size);
2133                         mask |= CEPH_SETATTR_SIZE;
2134                         release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2135                                    CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2136                 }
2137         }
2138         if (ia_valid & ATTR_MTIME) {
2139                 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2140                      inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2141                      attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2142                 if (issued & CEPH_CAP_FILE_EXCL) {
2143                         ci->i_time_warp_seq++;
2144                         inode->i_mtime = attr->ia_mtime;
2145                         dirtied |= CEPH_CAP_FILE_EXCL;
2146                 } else if ((issued & CEPH_CAP_FILE_WR) &&
2147                            timespec64_compare(&inode->i_mtime,
2148                                             &attr->ia_mtime) < 0) {
2149                         inode->i_mtime = attr->ia_mtime;
2150                         dirtied |= CEPH_CAP_FILE_WR;
2151                 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2152                            !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2153                         ceph_encode_timespec64(&req->r_args.setattr.mtime,
2154                                                &attr->ia_mtime);
2155                         mask |= CEPH_SETATTR_MTIME;
2156                         release |= CEPH_CAP_FILE_SHARED |
2157                                    CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2158                 }
2159         }
2160
2161         /* these do nothing */
2162         if (ia_valid & ATTR_CTIME) {
2163                 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2164                                          ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2165                 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2166                      inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2167                      attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2168                      only ? "ctime only" : "ignored");
2169                 if (only) {
2170                         /*
2171                          * if kernel wants to dirty ctime but nothing else,
2172                          * we need to choose a cap to dirty under, or do
2173                          * a almost-no-op setattr
2174                          */
2175                         if (issued & CEPH_CAP_AUTH_EXCL)
2176                                 dirtied |= CEPH_CAP_AUTH_EXCL;
2177                         else if (issued & CEPH_CAP_FILE_EXCL)
2178                                 dirtied |= CEPH_CAP_FILE_EXCL;
2179                         else if (issued & CEPH_CAP_XATTR_EXCL)
2180                                 dirtied |= CEPH_CAP_XATTR_EXCL;
2181                         else
2182                                 mask |= CEPH_SETATTR_CTIME;
2183                 }
2184         }
2185         if (ia_valid & ATTR_FILE)
2186                 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2187
2188         if (dirtied) {
2189                 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2190                                                            &prealloc_cf);
2191                 inode->i_ctime = attr->ia_ctime;
2192         }
2193
2194         release &= issued;
2195         spin_unlock(&ci->i_ceph_lock);
2196         if (lock_snap_rwsem)
2197                 up_read(&mdsc->snap_rwsem);
2198
2199         if (inode_dirty_flags)
2200                 __mark_inode_dirty(inode, inode_dirty_flags);
2201
2202
2203         if (mask) {
2204                 req->r_inode = inode;
2205                 ihold(inode);
2206                 req->r_inode_drop = release;
2207                 req->r_args.setattr.mask = cpu_to_le32(mask);
2208                 req->r_num_caps = 1;
2209                 req->r_stamp = attr->ia_ctime;
2210                 err = ceph_mdsc_do_request(mdsc, NULL, req);
2211         }
2212         dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2213              ceph_cap_string(dirtied), mask);
2214
2215         ceph_mdsc_put_request(req);
2216         ceph_free_cap_flush(prealloc_cf);
2217
2218         if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2219                 __ceph_do_pending_vmtruncate(inode);
2220
2221         return err;
2222 }
2223
2224 /*
2225  * setattr
2226  */
2227 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
2228 {
2229         struct inode *inode = d_inode(dentry);
2230         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2231         int err;
2232
2233         if (ceph_snap(inode) != CEPH_NOSNAP)
2234                 return -EROFS;
2235
2236         err = setattr_prepare(dentry, attr);
2237         if (err != 0)
2238                 return err;
2239
2240         if ((attr->ia_valid & ATTR_SIZE) &&
2241             attr->ia_size > max(inode->i_size, fsc->max_file_size))
2242                 return -EFBIG;
2243
2244         if ((attr->ia_valid & ATTR_SIZE) &&
2245             ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2246                 return -EDQUOT;
2247
2248         err = __ceph_setattr(inode, attr);
2249
2250         if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2251                 err = posix_acl_chmod(inode, attr->ia_mode);
2252
2253         return err;
2254 }
2255
2256 /*
2257  * Verify that we have a lease on the given mask.  If not,
2258  * do a getattr against an mds.
2259  */
2260 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2261                       int mask, bool force)
2262 {
2263         struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2264         struct ceph_mds_client *mdsc = fsc->mdsc;
2265         struct ceph_mds_request *req;
2266         int mode;
2267         int err;
2268
2269         if (ceph_snap(inode) == CEPH_SNAPDIR) {
2270                 dout("do_getattr inode %p SNAPDIR\n", inode);
2271                 return 0;
2272         }
2273
2274         dout("do_getattr inode %p mask %s mode 0%o\n",
2275              inode, ceph_cap_string(mask), inode->i_mode);
2276         if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
2277                 return 0;
2278
2279         mode = (mask & CEPH_STAT_RSTAT) ? USE_AUTH_MDS : USE_ANY_MDS;
2280         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2281         if (IS_ERR(req))
2282                 return PTR_ERR(req);
2283         req->r_inode = inode;
2284         ihold(inode);
2285         req->r_num_caps = 1;
2286         req->r_args.getattr.mask = cpu_to_le32(mask);
2287         req->r_locked_page = locked_page;
2288         err = ceph_mdsc_do_request(mdsc, NULL, req);
2289         if (locked_page && err == 0) {
2290                 u64 inline_version = req->r_reply_info.targeti.inline_version;
2291                 if (inline_version == 0) {
2292                         /* the reply is supposed to contain inline data */
2293                         err = -EINVAL;
2294                 } else if (inline_version == CEPH_INLINE_NONE) {
2295                         err = -ENODATA;
2296                 } else {
2297                         err = req->r_reply_info.targeti.inline_len;
2298                 }
2299         }
2300         ceph_mdsc_put_request(req);
2301         dout("do_getattr result=%d\n", err);
2302         return err;
2303 }
2304
2305
2306 /*
2307  * Check inode permissions.  We verify we have a valid value for
2308  * the AUTH cap, then call the generic handler.
2309  */
2310 int ceph_permission(struct inode *inode, int mask)
2311 {
2312         int err;
2313
2314         if (mask & MAY_NOT_BLOCK)
2315                 return -ECHILD;
2316
2317         err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2318
2319         if (!err)
2320                 err = generic_permission(inode, mask);
2321         return err;
2322 }
2323
2324 /* Craft a mask of needed caps given a set of requested statx attrs. */
2325 static int statx_to_caps(u32 want)
2326 {
2327         int mask = 0;
2328
2329         if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME))
2330                 mask |= CEPH_CAP_AUTH_SHARED;
2331
2332         if (want & (STATX_NLINK|STATX_CTIME))
2333                 mask |= CEPH_CAP_LINK_SHARED;
2334
2335         if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|
2336                     STATX_BLOCKS))
2337                 mask |= CEPH_CAP_FILE_SHARED;
2338
2339         if (want & (STATX_CTIME))
2340                 mask |= CEPH_CAP_XATTR_SHARED;
2341
2342         return mask;
2343 }
2344
2345 /*
2346  * Get all the attributes. If we have sufficient caps for the requested attrs,
2347  * then we can avoid talking to the MDS at all.
2348  */
2349 int ceph_getattr(const struct path *path, struct kstat *stat,
2350                  u32 request_mask, unsigned int flags)
2351 {
2352         struct inode *inode = d_inode(path->dentry);
2353         struct ceph_inode_info *ci = ceph_inode(inode);
2354         u32 valid_mask = STATX_BASIC_STATS;
2355         int err = 0;
2356
2357         /* Skip the getattr altogether if we're asked not to sync */
2358         if (!(flags & AT_STATX_DONT_SYNC)) {
2359                 err = ceph_do_getattr(inode, statx_to_caps(request_mask),
2360                                       flags & AT_STATX_FORCE_SYNC);
2361                 if (err)
2362                         return err;
2363         }
2364
2365         generic_fillattr(inode, stat);
2366         stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
2367
2368         /*
2369          * btime on newly-allocated inodes is 0, so if this is still set to
2370          * that, then assume that it's not valid.
2371          */
2372         if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2373                 stat->btime = ci->i_btime;
2374                 valid_mask |= STATX_BTIME;
2375         }
2376
2377         if (ceph_snap(inode) == CEPH_NOSNAP)
2378                 stat->dev = inode->i_sb->s_dev;
2379         else
2380                 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
2381
2382         if (S_ISDIR(inode->i_mode)) {
2383                 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
2384                                         RBYTES))
2385                         stat->size = ci->i_rbytes;
2386                 else
2387                         stat->size = ci->i_files + ci->i_subdirs;
2388                 stat->blocks = 0;
2389                 stat->blksize = 65536;
2390                 /*
2391                  * Some applications rely on the number of st_nlink
2392                  * value on directories to be either 0 (if unlinked)
2393                  * or 2 + number of subdirectories.
2394                  */
2395                 if (stat->nlink == 1)
2396                         /* '.' + '..' + subdirs */
2397                         stat->nlink = 1 + 1 + ci->i_subdirs;
2398         }
2399
2400         stat->result_mask = request_mask & valid_mask;
2401         return err;
2402 }