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