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