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