xref: /openbmc/linux/fs/ceph/inode.c (revision 5c64737d253683b7d138dde0da513a9ade16a170)
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 #include <linux/fscrypt.h>
18 
19 #include "super.h"
20 #include "mds_client.h"
21 #include "cache.h"
22 #include "crypto.h"
23 #include <linux/ceph/decode.h>
24 
25 /*
26  * Ceph inode operations
27  *
28  * Implement basic inode helpers (get, alloc) and inode ops (getattr,
29  * setattr, etc.), xattr helpers, and helpers for assimilating
30  * metadata returned by the MDS into our cache.
31  *
32  * Also define helpers for doing asynchronous writeback, invalidation,
33  * and truncation for the benefit of those who can't afford to block
34  * (typically because they are in the message handler path).
35  */
36 
37 static const struct inode_operations ceph_symlink_iops;
38 static const struct inode_operations ceph_encrypted_symlink_iops;
39 
40 static void ceph_inode_work(struct work_struct *work);
41 
42 /*
43  * find or create an inode, given the ceph ino number
44  */
45 static int ceph_set_ino_cb(struct inode *inode, void *data)
46 {
47 	struct ceph_inode_info *ci = ceph_inode(inode);
48 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
49 
50 	ci->i_vino = *(struct ceph_vino *)data;
51 	inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
52 	inode_set_iversion_raw(inode, 0);
53 	percpu_counter_inc(&mdsc->metric.total_inodes);
54 
55 	return 0;
56 }
57 
58 /**
59  * ceph_new_inode - allocate a new inode in advance of an expected create
60  * @dir: parent directory for new inode
61  * @dentry: dentry that may eventually point to new inode
62  * @mode: mode of new inode
63  * @as_ctx: pointer to inherited security context
64  *
65  * Allocate a new inode in advance of an operation to create a new inode.
66  * This allocates the inode and sets up the acl_sec_ctx with appropriate
67  * info for the new inode.
68  *
69  * Returns a pointer to the new inode or an ERR_PTR.
70  */
71 struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
72 			     umode_t *mode, struct ceph_acl_sec_ctx *as_ctx)
73 {
74 	int err;
75 	struct inode *inode;
76 
77 	inode = new_inode(dir->i_sb);
78 	if (!inode)
79 		return ERR_PTR(-ENOMEM);
80 
81 	if (!S_ISLNK(*mode)) {
82 		err = ceph_pre_init_acls(dir, mode, as_ctx);
83 		if (err < 0)
84 			goto out_err;
85 	}
86 
87 	inode->i_state = 0;
88 	inode->i_mode = *mode;
89 
90 	err = ceph_security_init_secctx(dentry, *mode, as_ctx);
91 	if (err < 0)
92 		goto out_err;
93 
94 	err = ceph_fscrypt_prepare_context(dir, inode, as_ctx);
95 	if (err)
96 		goto out_err;
97 
98 	return inode;
99 out_err:
100 	iput(inode);
101 	return ERR_PTR(err);
102 }
103 
104 void ceph_as_ctx_to_req(struct ceph_mds_request *req,
105 			struct ceph_acl_sec_ctx *as_ctx)
106 {
107 	if (as_ctx->pagelist) {
108 		req->r_pagelist = as_ctx->pagelist;
109 		as_ctx->pagelist = NULL;
110 	}
111 	ceph_fscrypt_as_ctx_to_req(req, as_ctx);
112 }
113 
114 /**
115  * ceph_get_inode - find or create/hash a new inode
116  * @sb: superblock to search and allocate in
117  * @vino: vino to search for
118  * @newino: optional new inode to insert if one isn't found (may be NULL)
119  *
120  * Search for or insert a new inode into the hash for the given vino, and
121  * return a reference to it. If new is non-NULL, its reference is consumed.
122  */
123 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
124 			     struct inode *newino)
125 {
126 	struct inode *inode;
127 
128 	if (ceph_vino_is_reserved(vino))
129 		return ERR_PTR(-EREMOTEIO);
130 
131 	if (newino) {
132 		inode = inode_insert5(newino, (unsigned long)vino.ino,
133 				      ceph_ino_compare, ceph_set_ino_cb, &vino);
134 		if (inode != newino)
135 			iput(newino);
136 	} else {
137 		inode = iget5_locked(sb, (unsigned long)vino.ino,
138 				     ceph_ino_compare, ceph_set_ino_cb, &vino);
139 	}
140 
141 	if (!inode) {
142 		dout("No inode found for %llx.%llx\n", vino.ino, vino.snap);
143 		return ERR_PTR(-ENOMEM);
144 	}
145 
146 	dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode),
147 	     ceph_vinop(inode), inode, !!(inode->i_state & I_NEW));
148 	return inode;
149 }
150 
151 /*
152  * get/constuct snapdir inode for a given directory
153  */
154 struct inode *ceph_get_snapdir(struct inode *parent)
155 {
156 	struct ceph_vino vino = {
157 		.ino = ceph_ino(parent),
158 		.snap = CEPH_SNAPDIR,
159 	};
160 	struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
161 	struct ceph_inode_info *ci = ceph_inode(inode);
162 
163 	if (IS_ERR(inode))
164 		return inode;
165 
166 	if (!S_ISDIR(parent->i_mode)) {
167 		pr_warn_once("bad snapdir parent type (mode=0%o)\n",
168 			     parent->i_mode);
169 		goto err;
170 	}
171 
172 	if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
173 		pr_warn_once("bad snapdir inode type (mode=0%o)\n",
174 			     inode->i_mode);
175 		goto err;
176 	}
177 
178 	inode->i_mode = parent->i_mode;
179 	inode->i_uid = parent->i_uid;
180 	inode->i_gid = parent->i_gid;
181 	inode->i_mtime = parent->i_mtime;
182 	inode->i_ctime = parent->i_ctime;
183 	inode->i_atime = parent->i_atime;
184 	ci->i_rbytes = 0;
185 	ci->i_btime = ceph_inode(parent)->i_btime;
186 
187 	if (inode->i_state & I_NEW) {
188 		inode->i_op = &ceph_snapdir_iops;
189 		inode->i_fop = &ceph_snapdir_fops;
190 		ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
191 		unlock_new_inode(inode);
192 	}
193 
194 	return inode;
195 err:
196 	if ((inode->i_state & I_NEW))
197 		discard_new_inode(inode);
198 	else
199 		iput(inode);
200 	return ERR_PTR(-ENOTDIR);
201 }
202 
203 const struct inode_operations ceph_file_iops = {
204 	.permission = ceph_permission,
205 	.setattr = ceph_setattr,
206 	.getattr = ceph_getattr,
207 	.listxattr = ceph_listxattr,
208 	.get_inode_acl = ceph_get_acl,
209 	.set_acl = ceph_set_acl,
210 };
211 
212 
213 /*
214  * We use a 'frag tree' to keep track of the MDS's directory fragments
215  * for a given inode (usually there is just a single fragment).  We
216  * need to know when a child frag is delegated to a new MDS, or when
217  * it is flagged as replicated, so we can direct our requests
218  * accordingly.
219  */
220 
221 /*
222  * find/create a frag in the tree
223  */
224 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
225 						    u32 f)
226 {
227 	struct rb_node **p;
228 	struct rb_node *parent = NULL;
229 	struct ceph_inode_frag *frag;
230 	int c;
231 
232 	p = &ci->i_fragtree.rb_node;
233 	while (*p) {
234 		parent = *p;
235 		frag = rb_entry(parent, struct ceph_inode_frag, node);
236 		c = ceph_frag_compare(f, frag->frag);
237 		if (c < 0)
238 			p = &(*p)->rb_left;
239 		else if (c > 0)
240 			p = &(*p)->rb_right;
241 		else
242 			return frag;
243 	}
244 
245 	frag = kmalloc(sizeof(*frag), GFP_NOFS);
246 	if (!frag)
247 		return ERR_PTR(-ENOMEM);
248 
249 	frag->frag = f;
250 	frag->split_by = 0;
251 	frag->mds = -1;
252 	frag->ndist = 0;
253 
254 	rb_link_node(&frag->node, parent, p);
255 	rb_insert_color(&frag->node, &ci->i_fragtree);
256 
257 	dout("get_or_create_frag added %llx.%llx frag %x\n",
258 	     ceph_vinop(&ci->netfs.inode), f);
259 	return frag;
260 }
261 
262 /*
263  * find a specific frag @f
264  */
265 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
266 {
267 	struct rb_node *n = ci->i_fragtree.rb_node;
268 
269 	while (n) {
270 		struct ceph_inode_frag *frag =
271 			rb_entry(n, struct ceph_inode_frag, node);
272 		int c = ceph_frag_compare(f, frag->frag);
273 		if (c < 0)
274 			n = n->rb_left;
275 		else if (c > 0)
276 			n = n->rb_right;
277 		else
278 			return frag;
279 	}
280 	return NULL;
281 }
282 
283 /*
284  * Choose frag containing the given value @v.  If @pfrag is
285  * specified, copy the frag delegation info to the caller if
286  * it is present.
287  */
288 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
289 			      struct ceph_inode_frag *pfrag, int *found)
290 {
291 	u32 t = ceph_frag_make(0, 0);
292 	struct ceph_inode_frag *frag;
293 	unsigned nway, i;
294 	u32 n;
295 
296 	if (found)
297 		*found = 0;
298 
299 	while (1) {
300 		WARN_ON(!ceph_frag_contains_value(t, v));
301 		frag = __ceph_find_frag(ci, t);
302 		if (!frag)
303 			break; /* t is a leaf */
304 		if (frag->split_by == 0) {
305 			if (pfrag)
306 				memcpy(pfrag, frag, sizeof(*pfrag));
307 			if (found)
308 				*found = 1;
309 			break;
310 		}
311 
312 		/* choose child */
313 		nway = 1 << frag->split_by;
314 		dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
315 		     frag->split_by, nway);
316 		for (i = 0; i < nway; i++) {
317 			n = ceph_frag_make_child(t, frag->split_by, i);
318 			if (ceph_frag_contains_value(n, v)) {
319 				t = n;
320 				break;
321 			}
322 		}
323 		BUG_ON(i == nway);
324 	}
325 	dout("choose_frag(%x) = %x\n", v, t);
326 
327 	return t;
328 }
329 
330 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
331 		     struct ceph_inode_frag *pfrag, int *found)
332 {
333 	u32 ret;
334 	mutex_lock(&ci->i_fragtree_mutex);
335 	ret = __ceph_choose_frag(ci, v, pfrag, found);
336 	mutex_unlock(&ci->i_fragtree_mutex);
337 	return ret;
338 }
339 
340 /*
341  * Process dirfrag (delegation) info from the mds.  Include leaf
342  * fragment in tree ONLY if ndist > 0.  Otherwise, only
343  * branches/splits are included in i_fragtree)
344  */
345 static int ceph_fill_dirfrag(struct inode *inode,
346 			     struct ceph_mds_reply_dirfrag *dirinfo)
347 {
348 	struct ceph_inode_info *ci = ceph_inode(inode);
349 	struct ceph_inode_frag *frag;
350 	u32 id = le32_to_cpu(dirinfo->frag);
351 	int mds = le32_to_cpu(dirinfo->auth);
352 	int ndist = le32_to_cpu(dirinfo->ndist);
353 	int diri_auth = -1;
354 	int i;
355 	int err = 0;
356 
357 	spin_lock(&ci->i_ceph_lock);
358 	if (ci->i_auth_cap)
359 		diri_auth = ci->i_auth_cap->mds;
360 	spin_unlock(&ci->i_ceph_lock);
361 
362 	if (mds == -1) /* CDIR_AUTH_PARENT */
363 		mds = diri_auth;
364 
365 	mutex_lock(&ci->i_fragtree_mutex);
366 	if (ndist == 0 && mds == diri_auth) {
367 		/* no delegation info needed. */
368 		frag = __ceph_find_frag(ci, id);
369 		if (!frag)
370 			goto out;
371 		if (frag->split_by == 0) {
372 			/* tree leaf, remove */
373 			dout("fill_dirfrag removed %llx.%llx frag %x"
374 			     " (no ref)\n", ceph_vinop(inode), id);
375 			rb_erase(&frag->node, &ci->i_fragtree);
376 			kfree(frag);
377 		} else {
378 			/* tree branch, keep and clear */
379 			dout("fill_dirfrag cleared %llx.%llx frag %x"
380 			     " referral\n", ceph_vinop(inode), id);
381 			frag->mds = -1;
382 			frag->ndist = 0;
383 		}
384 		goto out;
385 	}
386 
387 
388 	/* find/add this frag to store mds delegation info */
389 	frag = __get_or_create_frag(ci, id);
390 	if (IS_ERR(frag)) {
391 		/* this is not the end of the world; we can continue
392 		   with bad/inaccurate delegation info */
393 		pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
394 		       ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
395 		err = -ENOMEM;
396 		goto out;
397 	}
398 
399 	frag->mds = mds;
400 	frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
401 	for (i = 0; i < frag->ndist; i++)
402 		frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
403 	dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
404 	     ceph_vinop(inode), frag->frag, frag->ndist);
405 
406 out:
407 	mutex_unlock(&ci->i_fragtree_mutex);
408 	return err;
409 }
410 
411 static int frag_tree_split_cmp(const void *l, const void *r)
412 {
413 	struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
414 	struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
415 	return ceph_frag_compare(le32_to_cpu(ls->frag),
416 				 le32_to_cpu(rs->frag));
417 }
418 
419 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
420 {
421 	if (!frag)
422 		return f == ceph_frag_make(0, 0);
423 	if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
424 		return false;
425 	return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
426 }
427 
428 static int ceph_fill_fragtree(struct inode *inode,
429 			      struct ceph_frag_tree_head *fragtree,
430 			      struct ceph_mds_reply_dirfrag *dirinfo)
431 {
432 	struct ceph_inode_info *ci = ceph_inode(inode);
433 	struct ceph_inode_frag *frag, *prev_frag = NULL;
434 	struct rb_node *rb_node;
435 	unsigned i, split_by, nsplits;
436 	u32 id;
437 	bool update = false;
438 
439 	mutex_lock(&ci->i_fragtree_mutex);
440 	nsplits = le32_to_cpu(fragtree->nsplits);
441 	if (nsplits != ci->i_fragtree_nsplits) {
442 		update = true;
443 	} else if (nsplits) {
444 		i = get_random_u32_below(nsplits);
445 		id = le32_to_cpu(fragtree->splits[i].frag);
446 		if (!__ceph_find_frag(ci, id))
447 			update = true;
448 	} else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
449 		rb_node = rb_first(&ci->i_fragtree);
450 		frag = rb_entry(rb_node, struct ceph_inode_frag, node);
451 		if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
452 			update = true;
453 	}
454 	if (!update && dirinfo) {
455 		id = le32_to_cpu(dirinfo->frag);
456 		if (id != __ceph_choose_frag(ci, id, NULL, NULL))
457 			update = true;
458 	}
459 	if (!update)
460 		goto out_unlock;
461 
462 	if (nsplits > 1) {
463 		sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
464 		     frag_tree_split_cmp, NULL);
465 	}
466 
467 	dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
468 	rb_node = rb_first(&ci->i_fragtree);
469 	for (i = 0; i < nsplits; i++) {
470 		id = le32_to_cpu(fragtree->splits[i].frag);
471 		split_by = le32_to_cpu(fragtree->splits[i].by);
472 		if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
473 			pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
474 			       "frag %x split by %d\n", ceph_vinop(inode),
475 			       i, nsplits, id, split_by);
476 			continue;
477 		}
478 		frag = NULL;
479 		while (rb_node) {
480 			frag = rb_entry(rb_node, struct ceph_inode_frag, node);
481 			if (ceph_frag_compare(frag->frag, id) >= 0) {
482 				if (frag->frag != id)
483 					frag = NULL;
484 				else
485 					rb_node = rb_next(rb_node);
486 				break;
487 			}
488 			rb_node = rb_next(rb_node);
489 			/* delete stale split/leaf node */
490 			if (frag->split_by > 0 ||
491 			    !is_frag_child(frag->frag, prev_frag)) {
492 				rb_erase(&frag->node, &ci->i_fragtree);
493 				if (frag->split_by > 0)
494 					ci->i_fragtree_nsplits--;
495 				kfree(frag);
496 			}
497 			frag = NULL;
498 		}
499 		if (!frag) {
500 			frag = __get_or_create_frag(ci, id);
501 			if (IS_ERR(frag))
502 				continue;
503 		}
504 		if (frag->split_by == 0)
505 			ci->i_fragtree_nsplits++;
506 		frag->split_by = split_by;
507 		dout(" frag %x split by %d\n", frag->frag, frag->split_by);
508 		prev_frag = frag;
509 	}
510 	while (rb_node) {
511 		frag = rb_entry(rb_node, struct ceph_inode_frag, node);
512 		rb_node = rb_next(rb_node);
513 		/* delete stale split/leaf node */
514 		if (frag->split_by > 0 ||
515 		    !is_frag_child(frag->frag, prev_frag)) {
516 			rb_erase(&frag->node, &ci->i_fragtree);
517 			if (frag->split_by > 0)
518 				ci->i_fragtree_nsplits--;
519 			kfree(frag);
520 		}
521 	}
522 out_unlock:
523 	mutex_unlock(&ci->i_fragtree_mutex);
524 	return 0;
525 }
526 
527 /*
528  * initialize a newly allocated inode.
529  */
530 struct inode *ceph_alloc_inode(struct super_block *sb)
531 {
532 	struct ceph_inode_info *ci;
533 	int i;
534 
535 	ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
536 	if (!ci)
537 		return NULL;
538 
539 	dout("alloc_inode %p\n", &ci->netfs.inode);
540 
541 	/* Set parameters for the netfs library */
542 	netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
543 
544 	spin_lock_init(&ci->i_ceph_lock);
545 
546 	ci->i_version = 0;
547 	ci->i_inline_version = 0;
548 	ci->i_time_warp_seq = 0;
549 	ci->i_ceph_flags = 0;
550 	atomic64_set(&ci->i_ordered_count, 1);
551 	atomic64_set(&ci->i_release_count, 1);
552 	atomic64_set(&ci->i_complete_seq[0], 0);
553 	atomic64_set(&ci->i_complete_seq[1], 0);
554 	ci->i_symlink = NULL;
555 
556 	ci->i_max_bytes = 0;
557 	ci->i_max_files = 0;
558 
559 	memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
560 	memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
561 	RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
562 
563 	ci->i_fragtree = RB_ROOT;
564 	mutex_init(&ci->i_fragtree_mutex);
565 
566 	ci->i_xattrs.blob = NULL;
567 	ci->i_xattrs.prealloc_blob = NULL;
568 	ci->i_xattrs.dirty = false;
569 	ci->i_xattrs.index = RB_ROOT;
570 	ci->i_xattrs.count = 0;
571 	ci->i_xattrs.names_size = 0;
572 	ci->i_xattrs.vals_size = 0;
573 	ci->i_xattrs.version = 0;
574 	ci->i_xattrs.index_version = 0;
575 
576 	ci->i_caps = RB_ROOT;
577 	ci->i_auth_cap = NULL;
578 	ci->i_dirty_caps = 0;
579 	ci->i_flushing_caps = 0;
580 	INIT_LIST_HEAD(&ci->i_dirty_item);
581 	INIT_LIST_HEAD(&ci->i_flushing_item);
582 	ci->i_prealloc_cap_flush = NULL;
583 	INIT_LIST_HEAD(&ci->i_cap_flush_list);
584 	init_waitqueue_head(&ci->i_cap_wq);
585 	ci->i_hold_caps_max = 0;
586 	INIT_LIST_HEAD(&ci->i_cap_delay_list);
587 	INIT_LIST_HEAD(&ci->i_cap_snaps);
588 	ci->i_head_snapc = NULL;
589 	ci->i_snap_caps = 0;
590 
591 	ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
592 	for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
593 		ci->i_nr_by_mode[i] = 0;
594 
595 	mutex_init(&ci->i_truncate_mutex);
596 	ci->i_truncate_seq = 0;
597 	ci->i_truncate_size = 0;
598 	ci->i_truncate_pending = 0;
599 	ci->i_truncate_pagecache_size = 0;
600 
601 	ci->i_max_size = 0;
602 	ci->i_reported_size = 0;
603 	ci->i_wanted_max_size = 0;
604 	ci->i_requested_max_size = 0;
605 
606 	ci->i_pin_ref = 0;
607 	ci->i_rd_ref = 0;
608 	ci->i_rdcache_ref = 0;
609 	ci->i_wr_ref = 0;
610 	ci->i_wb_ref = 0;
611 	ci->i_fx_ref = 0;
612 	ci->i_wrbuffer_ref = 0;
613 	ci->i_wrbuffer_ref_head = 0;
614 	atomic_set(&ci->i_filelock_ref, 0);
615 	atomic_set(&ci->i_shared_gen, 1);
616 	ci->i_rdcache_gen = 0;
617 	ci->i_rdcache_revoking = 0;
618 
619 	INIT_LIST_HEAD(&ci->i_unsafe_dirops);
620 	INIT_LIST_HEAD(&ci->i_unsafe_iops);
621 	spin_lock_init(&ci->i_unsafe_lock);
622 
623 	ci->i_snap_realm = NULL;
624 	INIT_LIST_HEAD(&ci->i_snap_realm_item);
625 	INIT_LIST_HEAD(&ci->i_snap_flush_item);
626 
627 	INIT_WORK(&ci->i_work, ceph_inode_work);
628 	ci->i_work_mask = 0;
629 	memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
630 #ifdef CONFIG_FS_ENCRYPTION
631 	ci->fscrypt_auth = NULL;
632 	ci->fscrypt_auth_len = 0;
633 #endif
634 	return &ci->netfs.inode;
635 }
636 
637 void ceph_free_inode(struct inode *inode)
638 {
639 	struct ceph_inode_info *ci = ceph_inode(inode);
640 
641 	kfree(ci->i_symlink);
642 #ifdef CONFIG_FS_ENCRYPTION
643 	kfree(ci->fscrypt_auth);
644 #endif
645 	fscrypt_free_inode(inode);
646 	kmem_cache_free(ceph_inode_cachep, ci);
647 }
648 
649 void ceph_evict_inode(struct inode *inode)
650 {
651 	struct ceph_inode_info *ci = ceph_inode(inode);
652 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
653 	struct ceph_inode_frag *frag;
654 	struct rb_node *n;
655 
656 	dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
657 
658 	percpu_counter_dec(&mdsc->metric.total_inodes);
659 
660 	truncate_inode_pages_final(&inode->i_data);
661 	if (inode->i_state & I_PINNING_FSCACHE_WB)
662 		ceph_fscache_unuse_cookie(inode, true);
663 	clear_inode(inode);
664 
665 	ceph_fscache_unregister_inode_cookie(ci);
666 	fscrypt_put_encryption_info(inode);
667 
668 	__ceph_remove_caps(ci);
669 
670 	if (__ceph_has_quota(ci, QUOTA_GET_ANY))
671 		ceph_adjust_quota_realms_count(inode, false);
672 
673 	/*
674 	 * we may still have a snap_realm reference if there are stray
675 	 * caps in i_snap_caps.
676 	 */
677 	if (ci->i_snap_realm) {
678 		if (ceph_snap(inode) == CEPH_NOSNAP) {
679 			dout(" dropping residual ref to snap realm %p\n",
680 			     ci->i_snap_realm);
681 			ceph_change_snap_realm(inode, NULL);
682 		} else {
683 			ceph_put_snapid_map(mdsc, ci->i_snapid_map);
684 			ci->i_snap_realm = NULL;
685 		}
686 	}
687 
688 	while ((n = rb_first(&ci->i_fragtree)) != NULL) {
689 		frag = rb_entry(n, struct ceph_inode_frag, node);
690 		rb_erase(n, &ci->i_fragtree);
691 		kfree(frag);
692 	}
693 	ci->i_fragtree_nsplits = 0;
694 
695 	__ceph_destroy_xattrs(ci);
696 	if (ci->i_xattrs.blob)
697 		ceph_buffer_put(ci->i_xattrs.blob);
698 	if (ci->i_xattrs.prealloc_blob)
699 		ceph_buffer_put(ci->i_xattrs.prealloc_blob);
700 
701 	ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
702 	ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
703 }
704 
705 static inline blkcnt_t calc_inode_blocks(u64 size)
706 {
707 	return (size + (1<<9) - 1) >> 9;
708 }
709 
710 /*
711  * Helpers to fill in size, ctime, mtime, and atime.  We have to be
712  * careful because either the client or MDS may have more up to date
713  * info, depending on which capabilities are held, and whether
714  * time_warp_seq or truncate_seq have increased.  (Ordinarily, mtime
715  * and size are monotonically increasing, except when utimes() or
716  * truncate() increments the corresponding _seq values.)
717  */
718 int ceph_fill_file_size(struct inode *inode, int issued,
719 			u32 truncate_seq, u64 truncate_size, u64 size)
720 {
721 	struct ceph_inode_info *ci = ceph_inode(inode);
722 	int queue_trunc = 0;
723 	loff_t isize = i_size_read(inode);
724 
725 	if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
726 	    (truncate_seq == ci->i_truncate_seq && size > isize)) {
727 		dout("size %lld -> %llu\n", isize, size);
728 		if (size > 0 && S_ISDIR(inode->i_mode)) {
729 			pr_err("fill_file_size non-zero size for directory\n");
730 			size = 0;
731 		}
732 		i_size_write(inode, size);
733 		inode->i_blocks = calc_inode_blocks(size);
734 		/*
735 		 * If we're expanding, then we should be able to just update
736 		 * the existing cookie.
737 		 */
738 		if (size > isize)
739 			ceph_fscache_update(inode);
740 		ci->i_reported_size = size;
741 		if (truncate_seq != ci->i_truncate_seq) {
742 			dout("truncate_seq %u -> %u\n",
743 			     ci->i_truncate_seq, truncate_seq);
744 			ci->i_truncate_seq = truncate_seq;
745 
746 			/* the MDS should have revoked these caps */
747 			WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
748 					       CEPH_CAP_FILE_RD |
749 					       CEPH_CAP_FILE_WR |
750 					       CEPH_CAP_FILE_LAZYIO));
751 			/*
752 			 * If we hold relevant caps, or in the case where we're
753 			 * not the only client referencing this file and we
754 			 * don't hold those caps, then we need to check whether
755 			 * the file is either opened or mmaped
756 			 */
757 			if ((issued & (CEPH_CAP_FILE_CACHE|
758 				       CEPH_CAP_FILE_BUFFER)) ||
759 			    mapping_mapped(inode->i_mapping) ||
760 			    __ceph_is_file_opened(ci)) {
761 				ci->i_truncate_pending++;
762 				queue_trunc = 1;
763 			}
764 		}
765 	}
766 	if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
767 	    ci->i_truncate_size != truncate_size) {
768 		dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
769 		     truncate_size);
770 		ci->i_truncate_size = truncate_size;
771 		if (IS_ENCRYPTED(inode))
772 			ci->i_truncate_pagecache_size = size;
773 		else
774 			ci->i_truncate_pagecache_size = truncate_size;
775 	}
776 	return queue_trunc;
777 }
778 
779 void ceph_fill_file_time(struct inode *inode, int issued,
780 			 u64 time_warp_seq, struct timespec64 *ctime,
781 			 struct timespec64 *mtime, struct timespec64 *atime)
782 {
783 	struct ceph_inode_info *ci = ceph_inode(inode);
784 	int warn = 0;
785 
786 	if (issued & (CEPH_CAP_FILE_EXCL|
787 		      CEPH_CAP_FILE_WR|
788 		      CEPH_CAP_FILE_BUFFER|
789 		      CEPH_CAP_AUTH_EXCL|
790 		      CEPH_CAP_XATTR_EXCL)) {
791 		if (ci->i_version == 0 ||
792 		    timespec64_compare(ctime, &inode->i_ctime) > 0) {
793 			dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
794 			     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
795 			     ctime->tv_sec, ctime->tv_nsec);
796 			inode->i_ctime = *ctime;
797 		}
798 		if (ci->i_version == 0 ||
799 		    ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
800 			/* the MDS did a utimes() */
801 			dout("mtime %lld.%09ld -> %lld.%09ld "
802 			     "tw %d -> %d\n",
803 			     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
804 			     mtime->tv_sec, mtime->tv_nsec,
805 			     ci->i_time_warp_seq, (int)time_warp_seq);
806 
807 			inode->i_mtime = *mtime;
808 			inode->i_atime = *atime;
809 			ci->i_time_warp_seq = time_warp_seq;
810 		} else if (time_warp_seq == ci->i_time_warp_seq) {
811 			/* nobody did utimes(); take the max */
812 			if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
813 				dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
814 				     inode->i_mtime.tv_sec,
815 				     inode->i_mtime.tv_nsec,
816 				     mtime->tv_sec, mtime->tv_nsec);
817 				inode->i_mtime = *mtime;
818 			}
819 			if (timespec64_compare(atime, &inode->i_atime) > 0) {
820 				dout("atime %lld.%09ld -> %lld.%09ld inc\n",
821 				     inode->i_atime.tv_sec,
822 				     inode->i_atime.tv_nsec,
823 				     atime->tv_sec, atime->tv_nsec);
824 				inode->i_atime = *atime;
825 			}
826 		} else if (issued & CEPH_CAP_FILE_EXCL) {
827 			/* we did a utimes(); ignore mds values */
828 		} else {
829 			warn = 1;
830 		}
831 	} else {
832 		/* we have no write|excl caps; whatever the MDS says is true */
833 		if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
834 			inode->i_ctime = *ctime;
835 			inode->i_mtime = *mtime;
836 			inode->i_atime = *atime;
837 			ci->i_time_warp_seq = time_warp_seq;
838 		} else {
839 			warn = 1;
840 		}
841 	}
842 	if (warn) /* time_warp_seq shouldn't go backwards */
843 		dout("%p mds time_warp_seq %llu < %u\n",
844 		     inode, time_warp_seq, ci->i_time_warp_seq);
845 }
846 
847 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
848 static int decode_encrypted_symlink(const char *encsym, int enclen, u8 **decsym)
849 {
850 	int declen;
851 	u8 *sym;
852 
853 	sym = kmalloc(enclen + 1, GFP_NOFS);
854 	if (!sym)
855 		return -ENOMEM;
856 
857 	declen = ceph_base64_decode(encsym, enclen, sym);
858 	if (declen < 0) {
859 		pr_err("%s: can't decode symlink (%d). Content: %.*s\n",
860 		       __func__, declen, enclen, encsym);
861 		kfree(sym);
862 		return -EIO;
863 	}
864 	sym[declen + 1] = '\0';
865 	*decsym = sym;
866 	return declen;
867 }
868 #else
869 static int decode_encrypted_symlink(const char *encsym, int symlen, u8 **decsym)
870 {
871 	return -EOPNOTSUPP;
872 }
873 #endif
874 
875 /*
876  * Populate an inode based on info from mds.  May be called on new or
877  * existing inodes.
878  */
879 int ceph_fill_inode(struct inode *inode, struct page *locked_page,
880 		    struct ceph_mds_reply_info_in *iinfo,
881 		    struct ceph_mds_reply_dirfrag *dirinfo,
882 		    struct ceph_mds_session *session, int cap_fmode,
883 		    struct ceph_cap_reservation *caps_reservation)
884 {
885 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
886 	struct ceph_mds_reply_inode *info = iinfo->in;
887 	struct ceph_inode_info *ci = ceph_inode(inode);
888 	int issued, new_issued, info_caps;
889 	struct timespec64 mtime, atime, ctime;
890 	struct ceph_buffer *xattr_blob = NULL;
891 	struct ceph_buffer *old_blob = NULL;
892 	struct ceph_string *pool_ns = NULL;
893 	struct ceph_cap *new_cap = NULL;
894 	int err = 0;
895 	bool wake = false;
896 	bool queue_trunc = false;
897 	bool new_version = false;
898 	bool fill_inline = false;
899 	umode_t mode = le32_to_cpu(info->mode);
900 	dev_t rdev = le32_to_cpu(info->rdev);
901 
902 	lockdep_assert_held(&mdsc->snap_rwsem);
903 
904 	dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
905 	     inode, ceph_vinop(inode), le64_to_cpu(info->version),
906 	     ci->i_version);
907 
908 	/* Once I_NEW is cleared, we can't change type or dev numbers */
909 	if (inode->i_state & I_NEW) {
910 		inode->i_mode = mode;
911 	} else {
912 		if (inode_wrong_type(inode, mode)) {
913 			pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
914 				     ceph_vinop(inode), inode->i_mode, mode);
915 			return -ESTALE;
916 		}
917 
918 		if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
919 			pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
920 				     ceph_vinop(inode), MAJOR(inode->i_rdev),
921 				     MINOR(inode->i_rdev), MAJOR(rdev),
922 				     MINOR(rdev));
923 			return -ESTALE;
924 		}
925 	}
926 
927 	info_caps = le32_to_cpu(info->cap.caps);
928 
929 	/* prealloc new cap struct */
930 	if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
931 		new_cap = ceph_get_cap(mdsc, caps_reservation);
932 		if (!new_cap)
933 			return -ENOMEM;
934 	}
935 
936 	/*
937 	 * prealloc xattr data, if it looks like we'll need it.  only
938 	 * if len > 4 (meaning there are actually xattrs; the first 4
939 	 * bytes are the xattr count).
940 	 */
941 	if (iinfo->xattr_len > 4) {
942 		xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
943 		if (!xattr_blob)
944 			pr_err("%s ENOMEM xattr blob %d bytes\n", __func__,
945 			       iinfo->xattr_len);
946 	}
947 
948 	if (iinfo->pool_ns_len > 0)
949 		pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
950 						     iinfo->pool_ns_len);
951 
952 	if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
953 		ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
954 
955 	spin_lock(&ci->i_ceph_lock);
956 
957 	/*
958 	 * provided version will be odd if inode value is projected,
959 	 * even if stable.  skip the update if we have newer stable
960 	 * info (ours>=theirs, e.g. due to racing mds replies), unless
961 	 * we are getting projected (unstable) info (in which case the
962 	 * version is odd, and we want ours>theirs).
963 	 *   us   them
964 	 *   2    2     skip
965 	 *   3    2     skip
966 	 *   3    3     update
967 	 */
968 	if (ci->i_version == 0 ||
969 	    ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
970 	     le64_to_cpu(info->version) > (ci->i_version & ~1)))
971 		new_version = true;
972 
973 	/* Update change_attribute */
974 	inode_set_max_iversion_raw(inode, iinfo->change_attr);
975 
976 	__ceph_caps_issued(ci, &issued);
977 	issued |= __ceph_caps_dirty(ci);
978 	new_issued = ~issued & info_caps;
979 
980 	__ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
981 
982 #ifdef CONFIG_FS_ENCRYPTION
983 	if (iinfo->fscrypt_auth_len &&
984 	    ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) {
985 		kfree(ci->fscrypt_auth);
986 		ci->fscrypt_auth_len = iinfo->fscrypt_auth_len;
987 		ci->fscrypt_auth = iinfo->fscrypt_auth;
988 		iinfo->fscrypt_auth = NULL;
989 		iinfo->fscrypt_auth_len = 0;
990 		inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
991 	}
992 #endif
993 
994 	if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
995 	    (issued & CEPH_CAP_AUTH_EXCL) == 0) {
996 		inode->i_mode = mode;
997 		inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
998 		inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
999 		dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
1000 		     from_kuid(&init_user_ns, inode->i_uid),
1001 		     from_kgid(&init_user_ns, inode->i_gid));
1002 		ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
1003 		ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
1004 	}
1005 
1006 	/* directories have fl_stripe_unit set to zero */
1007 	if (IS_ENCRYPTED(inode))
1008 		inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
1009 	else if (le32_to_cpu(info->layout.fl_stripe_unit))
1010 		inode->i_blkbits =
1011 			fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
1012 	else
1013 		inode->i_blkbits = CEPH_BLOCK_SHIFT;
1014 
1015 	if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
1016 	    (issued & CEPH_CAP_LINK_EXCL) == 0)
1017 		set_nlink(inode, le32_to_cpu(info->nlink));
1018 
1019 	if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
1020 		/* be careful with mtime, atime, size */
1021 		ceph_decode_timespec64(&atime, &info->atime);
1022 		ceph_decode_timespec64(&mtime, &info->mtime);
1023 		ceph_decode_timespec64(&ctime, &info->ctime);
1024 		ceph_fill_file_time(inode, issued,
1025 				le32_to_cpu(info->time_warp_seq),
1026 				&ctime, &mtime, &atime);
1027 	}
1028 
1029 	if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
1030 		ci->i_files = le64_to_cpu(info->files);
1031 		ci->i_subdirs = le64_to_cpu(info->subdirs);
1032 	}
1033 
1034 	if (new_version ||
1035 	    (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
1036 		u64 size = le64_to_cpu(info->size);
1037 		s64 old_pool = ci->i_layout.pool_id;
1038 		struct ceph_string *old_ns;
1039 
1040 		ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
1041 		old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
1042 					lockdep_is_held(&ci->i_ceph_lock));
1043 		rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
1044 
1045 		if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
1046 			ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
1047 
1048 		pool_ns = old_ns;
1049 
1050 		if (IS_ENCRYPTED(inode) && size &&
1051 		    iinfo->fscrypt_file_len == sizeof(__le64)) {
1052 			u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file);
1053 
1054 			if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) {
1055 				size = fsize;
1056 			} else {
1057 				pr_warn("fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n",
1058 					info->size, size);
1059 			}
1060 		}
1061 
1062 		queue_trunc = ceph_fill_file_size(inode, issued,
1063 					le32_to_cpu(info->truncate_seq),
1064 					le64_to_cpu(info->truncate_size),
1065 					size);
1066 		/* only update max_size on auth cap */
1067 		if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1068 		    ci->i_max_size != le64_to_cpu(info->max_size)) {
1069 			dout("max_size %lld -> %llu\n", ci->i_max_size,
1070 					le64_to_cpu(info->max_size));
1071 			ci->i_max_size = le64_to_cpu(info->max_size);
1072 		}
1073 	}
1074 
1075 	/* layout and rstat are not tracked by capability, update them if
1076 	 * the inode info is from auth mds */
1077 	if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
1078 		if (S_ISDIR(inode->i_mode)) {
1079 			ci->i_dir_layout = iinfo->dir_layout;
1080 			ci->i_rbytes = le64_to_cpu(info->rbytes);
1081 			ci->i_rfiles = le64_to_cpu(info->rfiles);
1082 			ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
1083 			ci->i_dir_pin = iinfo->dir_pin;
1084 			ci->i_rsnaps = iinfo->rsnaps;
1085 			ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
1086 		}
1087 	}
1088 
1089 	/* xattrs */
1090 	/* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
1091 	if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))  &&
1092 	    le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
1093 		if (ci->i_xattrs.blob)
1094 			old_blob = ci->i_xattrs.blob;
1095 		ci->i_xattrs.blob = xattr_blob;
1096 		if (xattr_blob)
1097 			memcpy(ci->i_xattrs.blob->vec.iov_base,
1098 			       iinfo->xattr_data, iinfo->xattr_len);
1099 		ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
1100 		ceph_forget_all_cached_acls(inode);
1101 		ceph_security_invalidate_secctx(inode);
1102 		xattr_blob = NULL;
1103 	}
1104 
1105 	/* finally update i_version */
1106 	if (le64_to_cpu(info->version) > ci->i_version)
1107 		ci->i_version = le64_to_cpu(info->version);
1108 
1109 	inode->i_mapping->a_ops = &ceph_aops;
1110 
1111 	switch (inode->i_mode & S_IFMT) {
1112 	case S_IFIFO:
1113 	case S_IFBLK:
1114 	case S_IFCHR:
1115 	case S_IFSOCK:
1116 		inode->i_blkbits = PAGE_SHIFT;
1117 		init_special_inode(inode, inode->i_mode, rdev);
1118 		inode->i_op = &ceph_file_iops;
1119 		break;
1120 	case S_IFREG:
1121 		inode->i_op = &ceph_file_iops;
1122 		inode->i_fop = &ceph_file_fops;
1123 		break;
1124 	case S_IFLNK:
1125 		if (!ci->i_symlink) {
1126 			u32 symlen = iinfo->symlink_len;
1127 			char *sym;
1128 
1129 			spin_unlock(&ci->i_ceph_lock);
1130 
1131 			if (IS_ENCRYPTED(inode)) {
1132 				if (symlen != i_size_read(inode))
1133 					pr_err("%s %llx.%llx BAD symlink size %lld\n",
1134 						__func__, ceph_vinop(inode),
1135 						i_size_read(inode));
1136 
1137 				err = decode_encrypted_symlink(iinfo->symlink,
1138 							       symlen, (u8 **)&sym);
1139 				if (err < 0) {
1140 					pr_err("%s decoding encrypted symlink failed: %d\n",
1141 						__func__, err);
1142 					goto out;
1143 				}
1144 				symlen = err;
1145 				i_size_write(inode, symlen);
1146 				inode->i_blocks = calc_inode_blocks(symlen);
1147 			} else {
1148 				if (symlen != i_size_read(inode)) {
1149 					pr_err("%s %llx.%llx BAD symlink size %lld\n",
1150 						__func__, ceph_vinop(inode),
1151 						i_size_read(inode));
1152 					i_size_write(inode, symlen);
1153 					inode->i_blocks = calc_inode_blocks(symlen);
1154 				}
1155 
1156 				err = -ENOMEM;
1157 				sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
1158 				if (!sym)
1159 					goto out;
1160 			}
1161 
1162 			spin_lock(&ci->i_ceph_lock);
1163 			if (!ci->i_symlink)
1164 				ci->i_symlink = sym;
1165 			else
1166 				kfree(sym); /* lost a race */
1167 		}
1168 
1169 		if (IS_ENCRYPTED(inode)) {
1170 			/*
1171 			 * Encrypted symlinks need to be decrypted before we can
1172 			 * cache their targets in i_link. Don't touch it here.
1173 			 */
1174 			inode->i_op = &ceph_encrypted_symlink_iops;
1175 		} else {
1176 			inode->i_link = ci->i_symlink;
1177 			inode->i_op = &ceph_symlink_iops;
1178 		}
1179 		break;
1180 	case S_IFDIR:
1181 		inode->i_op = &ceph_dir_iops;
1182 		inode->i_fop = &ceph_dir_fops;
1183 		break;
1184 	default:
1185 		pr_err("%s %llx.%llx BAD mode 0%o\n", __func__,
1186 		       ceph_vinop(inode), inode->i_mode);
1187 	}
1188 
1189 	/* were we issued a capability? */
1190 	if (info_caps) {
1191 		if (ceph_snap(inode) == CEPH_NOSNAP) {
1192 			ceph_add_cap(inode, session,
1193 				     le64_to_cpu(info->cap.cap_id),
1194 				     info_caps,
1195 				     le32_to_cpu(info->cap.wanted),
1196 				     le32_to_cpu(info->cap.seq),
1197 				     le32_to_cpu(info->cap.mseq),
1198 				     le64_to_cpu(info->cap.realm),
1199 				     info->cap.flags, &new_cap);
1200 
1201 			/* set dir completion flag? */
1202 			if (S_ISDIR(inode->i_mode) &&
1203 			    ci->i_files == 0 && ci->i_subdirs == 0 &&
1204 			    (info_caps & CEPH_CAP_FILE_SHARED) &&
1205 			    (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1206 			    !__ceph_dir_is_complete(ci)) {
1207 				dout(" marking %p complete (empty)\n", inode);
1208 				i_size_write(inode, 0);
1209 				__ceph_dir_set_complete(ci,
1210 					atomic64_read(&ci->i_release_count),
1211 					atomic64_read(&ci->i_ordered_count));
1212 			}
1213 
1214 			wake = true;
1215 		} else {
1216 			dout(" %p got snap_caps %s\n", inode,
1217 			     ceph_cap_string(info_caps));
1218 			ci->i_snap_caps |= info_caps;
1219 		}
1220 	}
1221 
1222 	if (iinfo->inline_version > 0 &&
1223 	    iinfo->inline_version >= ci->i_inline_version) {
1224 		int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1225 		ci->i_inline_version = iinfo->inline_version;
1226 		if (ceph_has_inline_data(ci) &&
1227 		    (locked_page || (info_caps & cache_caps)))
1228 			fill_inline = true;
1229 	}
1230 
1231 	if (cap_fmode >= 0) {
1232 		if (!info_caps)
1233 			pr_warn("mds issued no caps on %llx.%llx\n",
1234 				ceph_vinop(inode));
1235 		__ceph_touch_fmode(ci, mdsc, cap_fmode);
1236 	}
1237 
1238 	spin_unlock(&ci->i_ceph_lock);
1239 
1240 	ceph_fscache_register_inode_cookie(inode);
1241 
1242 	if (fill_inline)
1243 		ceph_fill_inline_data(inode, locked_page,
1244 				      iinfo->inline_data, iinfo->inline_len);
1245 
1246 	if (wake)
1247 		wake_up_all(&ci->i_cap_wq);
1248 
1249 	/* queue truncate if we saw i_size decrease */
1250 	if (queue_trunc)
1251 		ceph_queue_vmtruncate(inode);
1252 
1253 	/* populate frag tree */
1254 	if (S_ISDIR(inode->i_mode))
1255 		ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1256 
1257 	/* update delegation info? */
1258 	if (dirinfo)
1259 		ceph_fill_dirfrag(inode, dirinfo);
1260 
1261 	err = 0;
1262 out:
1263 	if (new_cap)
1264 		ceph_put_cap(mdsc, new_cap);
1265 	ceph_buffer_put(old_blob);
1266 	ceph_buffer_put(xattr_blob);
1267 	ceph_put_string(pool_ns);
1268 	return err;
1269 }
1270 
1271 /*
1272  * caller should hold session s_mutex and dentry->d_lock.
1273  */
1274 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1275 				  struct ceph_mds_reply_lease *lease,
1276 				  struct ceph_mds_session *session,
1277 				  unsigned long from_time,
1278 				  struct ceph_mds_session **old_lease_session)
1279 {
1280 	struct ceph_dentry_info *di = ceph_dentry(dentry);
1281 	unsigned mask = le16_to_cpu(lease->mask);
1282 	long unsigned duration = le32_to_cpu(lease->duration_ms);
1283 	long unsigned ttl = from_time + (duration * HZ) / 1000;
1284 	long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1285 
1286 	dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1287 	     dentry, duration, ttl);
1288 
1289 	/* only track leases on regular dentries */
1290 	if (ceph_snap(dir) != CEPH_NOSNAP)
1291 		return;
1292 
1293 	if (mask & CEPH_LEASE_PRIMARY_LINK)
1294 		di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1295 	else
1296 		di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1297 
1298 	di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1299 	if (!(mask & CEPH_LEASE_VALID)) {
1300 		__ceph_dentry_dir_lease_touch(di);
1301 		return;
1302 	}
1303 
1304 	if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
1305 	    time_before(ttl, di->time))
1306 		return;  /* we already have a newer lease. */
1307 
1308 	if (di->lease_session && di->lease_session != session) {
1309 		*old_lease_session = di->lease_session;
1310 		di->lease_session = NULL;
1311 	}
1312 
1313 	if (!di->lease_session)
1314 		di->lease_session = ceph_get_mds_session(session);
1315 	di->lease_gen = atomic_read(&session->s_cap_gen);
1316 	di->lease_seq = le32_to_cpu(lease->seq);
1317 	di->lease_renew_after = half_ttl;
1318 	di->lease_renew_from = 0;
1319 	di->time = ttl;
1320 
1321 	__ceph_dentry_lease_touch(di);
1322 }
1323 
1324 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1325 					struct ceph_mds_reply_lease *lease,
1326 					struct ceph_mds_session *session,
1327 					unsigned long from_time)
1328 {
1329 	struct ceph_mds_session *old_lease_session = NULL;
1330 	spin_lock(&dentry->d_lock);
1331 	__update_dentry_lease(dir, dentry, lease, session, from_time,
1332 			      &old_lease_session);
1333 	spin_unlock(&dentry->d_lock);
1334 	ceph_put_mds_session(old_lease_session);
1335 }
1336 
1337 /*
1338  * update dentry lease without having parent inode locked
1339  */
1340 static void update_dentry_lease_careful(struct dentry *dentry,
1341 					struct ceph_mds_reply_lease *lease,
1342 					struct ceph_mds_session *session,
1343 					unsigned long from_time,
1344 					char *dname, u32 dname_len,
1345 					struct ceph_vino *pdvino,
1346 					struct ceph_vino *ptvino)
1347 
1348 {
1349 	struct inode *dir;
1350 	struct ceph_mds_session *old_lease_session = NULL;
1351 
1352 	spin_lock(&dentry->d_lock);
1353 	/* make sure dentry's name matches target */
1354 	if (dentry->d_name.len != dname_len ||
1355 	    memcmp(dentry->d_name.name, dname, dname_len))
1356 		goto out_unlock;
1357 
1358 	dir = d_inode(dentry->d_parent);
1359 	/* make sure parent matches dvino */
1360 	if (!ceph_ino_compare(dir, pdvino))
1361 		goto out_unlock;
1362 
1363 	/* make sure dentry's inode matches target. NULL ptvino means that
1364 	 * we expect a negative dentry */
1365 	if (ptvino) {
1366 		if (d_really_is_negative(dentry))
1367 			goto out_unlock;
1368 		if (!ceph_ino_compare(d_inode(dentry), ptvino))
1369 			goto out_unlock;
1370 	} else {
1371 		if (d_really_is_positive(dentry))
1372 			goto out_unlock;
1373 	}
1374 
1375 	__update_dentry_lease(dir, dentry, lease, session,
1376 			      from_time, &old_lease_session);
1377 out_unlock:
1378 	spin_unlock(&dentry->d_lock);
1379 	ceph_put_mds_session(old_lease_session);
1380 }
1381 
1382 /*
1383  * splice a dentry to an inode.
1384  * caller must hold directory i_rwsem for this to be safe.
1385  */
1386 static int splice_dentry(struct dentry **pdn, struct inode *in)
1387 {
1388 	struct dentry *dn = *pdn;
1389 	struct dentry *realdn;
1390 
1391 	BUG_ON(d_inode(dn));
1392 
1393 	if (S_ISDIR(in->i_mode)) {
1394 		/* If inode is directory, d_splice_alias() below will remove
1395 		 * 'realdn' from its origin parent. We need to ensure that
1396 		 * origin parent's readdir cache will not reference 'realdn'
1397 		 */
1398 		realdn = d_find_any_alias(in);
1399 		if (realdn) {
1400 			struct ceph_dentry_info *di = ceph_dentry(realdn);
1401 			spin_lock(&realdn->d_lock);
1402 
1403 			realdn->d_op->d_prune(realdn);
1404 
1405 			di->time = jiffies;
1406 			di->lease_shared_gen = 0;
1407 			di->offset = 0;
1408 
1409 			spin_unlock(&realdn->d_lock);
1410 			dput(realdn);
1411 		}
1412 	}
1413 
1414 	/* dn must be unhashed */
1415 	if (!d_unhashed(dn))
1416 		d_drop(dn);
1417 	realdn = d_splice_alias(in, dn);
1418 	if (IS_ERR(realdn)) {
1419 		pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1420 		       PTR_ERR(realdn), dn, in, ceph_vinop(in));
1421 		return PTR_ERR(realdn);
1422 	}
1423 
1424 	if (realdn) {
1425 		dout("dn %p (%d) spliced with %p (%d) "
1426 		     "inode %p ino %llx.%llx\n",
1427 		     dn, d_count(dn),
1428 		     realdn, d_count(realdn),
1429 		     d_inode(realdn), ceph_vinop(d_inode(realdn)));
1430 		dput(dn);
1431 		*pdn = realdn;
1432 	} else {
1433 		BUG_ON(!ceph_dentry(dn));
1434 		dout("dn %p attached to %p ino %llx.%llx\n",
1435 		     dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1436 	}
1437 	return 0;
1438 }
1439 
1440 /*
1441  * Incorporate results into the local cache.  This is either just
1442  * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1443  * after a lookup).
1444  *
1445  * A reply may contain
1446  *         a directory inode along with a dentry.
1447  *  and/or a target inode
1448  *
1449  * Called with snap_rwsem (read).
1450  */
1451 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1452 {
1453 	struct ceph_mds_session *session = req->r_session;
1454 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1455 	struct inode *in = NULL;
1456 	struct ceph_vino tvino, dvino;
1457 	struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1458 	int err = 0;
1459 
1460 	dout("fill_trace %p is_dentry %d is_target %d\n", req,
1461 	     rinfo->head->is_dentry, rinfo->head->is_target);
1462 
1463 	if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1464 		dout("fill_trace reply is empty!\n");
1465 		if (rinfo->head->result == 0 && req->r_parent)
1466 			ceph_invalidate_dir_request(req);
1467 		return 0;
1468 	}
1469 
1470 	if (rinfo->head->is_dentry) {
1471 		struct inode *dir = req->r_parent;
1472 
1473 		if (dir) {
1474 			err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1475 					      rinfo->dirfrag, session, -1,
1476 					      &req->r_caps_reservation);
1477 			if (err < 0)
1478 				goto done;
1479 		} else {
1480 			WARN_ON_ONCE(1);
1481 		}
1482 
1483 		if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1484 		    test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1485 		    !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1486 			bool is_nokey = false;
1487 			struct qstr dname;
1488 			struct dentry *dn, *parent;
1489 			struct fscrypt_str oname = FSTR_INIT(NULL, 0);
1490 			struct ceph_fname fname = { .dir	= dir,
1491 						    .name	= rinfo->dname,
1492 						    .ctext	= rinfo->altname,
1493 						    .name_len	= rinfo->dname_len,
1494 						    .ctext_len	= rinfo->altname_len };
1495 
1496 			BUG_ON(!rinfo->head->is_target);
1497 			BUG_ON(req->r_dentry);
1498 
1499 			parent = d_find_any_alias(dir);
1500 			BUG_ON(!parent);
1501 
1502 			err = ceph_fname_alloc_buffer(dir, &oname);
1503 			if (err < 0) {
1504 				dput(parent);
1505 				goto done;
1506 			}
1507 
1508 			err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey);
1509 			if (err < 0) {
1510 				dput(parent);
1511 				ceph_fname_free_buffer(dir, &oname);
1512 				goto done;
1513 			}
1514 			dname.name = oname.name;
1515 			dname.len = oname.len;
1516 			dname.hash = full_name_hash(parent, dname.name, dname.len);
1517 			tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1518 			tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1519 retry_lookup:
1520 			dn = d_lookup(parent, &dname);
1521 			dout("d_lookup on parent=%p name=%.*s got %p\n",
1522 			     parent, dname.len, dname.name, dn);
1523 
1524 			if (!dn) {
1525 				dn = d_alloc(parent, &dname);
1526 				dout("d_alloc %p '%.*s' = %p\n", parent,
1527 				     dname.len, dname.name, dn);
1528 				if (!dn) {
1529 					dput(parent);
1530 					ceph_fname_free_buffer(dir, &oname);
1531 					err = -ENOMEM;
1532 					goto done;
1533 				}
1534 				if (is_nokey) {
1535 					spin_lock(&dn->d_lock);
1536 					dn->d_flags |= DCACHE_NOKEY_NAME;
1537 					spin_unlock(&dn->d_lock);
1538 				}
1539 				err = 0;
1540 			} else if (d_really_is_positive(dn) &&
1541 				   (ceph_ino(d_inode(dn)) != tvino.ino ||
1542 				    ceph_snap(d_inode(dn)) != tvino.snap)) {
1543 				dout(" dn %p points to wrong inode %p\n",
1544 				     dn, d_inode(dn));
1545 				ceph_dir_clear_ordered(dir);
1546 				d_delete(dn);
1547 				dput(dn);
1548 				goto retry_lookup;
1549 			}
1550 			ceph_fname_free_buffer(dir, &oname);
1551 
1552 			req->r_dentry = dn;
1553 			dput(parent);
1554 		}
1555 	}
1556 
1557 	if (rinfo->head->is_target) {
1558 		/* Should be filled in by handle_reply */
1559 		BUG_ON(!req->r_target_inode);
1560 
1561 		in = req->r_target_inode;
1562 		err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1563 				NULL, session,
1564 				(!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1565 				 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1566 				 rinfo->head->result == 0) ?  req->r_fmode : -1,
1567 				&req->r_caps_reservation);
1568 		if (err < 0) {
1569 			pr_err("ceph_fill_inode badness %p %llx.%llx\n",
1570 				in, ceph_vinop(in));
1571 			req->r_target_inode = NULL;
1572 			if (in->i_state & I_NEW)
1573 				discard_new_inode(in);
1574 			else
1575 				iput(in);
1576 			goto done;
1577 		}
1578 		if (in->i_state & I_NEW)
1579 			unlock_new_inode(in);
1580 	}
1581 
1582 	/*
1583 	 * ignore null lease/binding on snapdir ENOENT, or else we
1584 	 * will have trouble splicing in the virtual snapdir later
1585 	 */
1586 	if (rinfo->head->is_dentry &&
1587             !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1588 	    test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1589 	    (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1590 					       fsc->mount_options->snapdir_name,
1591 					       req->r_dentry->d_name.len))) {
1592 		/*
1593 		 * lookup link rename   : null -> possibly existing inode
1594 		 * mknod symlink mkdir  : null -> new inode
1595 		 * unlink               : linked -> null
1596 		 */
1597 		struct inode *dir = req->r_parent;
1598 		struct dentry *dn = req->r_dentry;
1599 		bool have_dir_cap, have_lease;
1600 
1601 		BUG_ON(!dn);
1602 		BUG_ON(!dir);
1603 		BUG_ON(d_inode(dn->d_parent) != dir);
1604 
1605 		dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1606 		dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1607 
1608 		BUG_ON(ceph_ino(dir) != dvino.ino);
1609 		BUG_ON(ceph_snap(dir) != dvino.snap);
1610 
1611 		/* do we have a lease on the whole dir? */
1612 		have_dir_cap =
1613 			(le32_to_cpu(rinfo->diri.in->cap.caps) &
1614 			 CEPH_CAP_FILE_SHARED);
1615 
1616 		/* do we have a dn lease? */
1617 		have_lease = have_dir_cap ||
1618 			le32_to_cpu(rinfo->dlease->duration_ms);
1619 		if (!have_lease)
1620 			dout("fill_trace  no dentry lease or dir cap\n");
1621 
1622 		/* rename? */
1623 		if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1624 			struct inode *olddir = req->r_old_dentry_dir;
1625 			BUG_ON(!olddir);
1626 
1627 			dout(" src %p '%pd' dst %p '%pd'\n",
1628 			     req->r_old_dentry,
1629 			     req->r_old_dentry,
1630 			     dn, dn);
1631 			dout("fill_trace doing d_move %p -> %p\n",
1632 			     req->r_old_dentry, dn);
1633 
1634 			/* d_move screws up sibling dentries' offsets */
1635 			ceph_dir_clear_ordered(dir);
1636 			ceph_dir_clear_ordered(olddir);
1637 
1638 			d_move(req->r_old_dentry, dn);
1639 			dout(" src %p '%pd' dst %p '%pd'\n",
1640 			     req->r_old_dentry,
1641 			     req->r_old_dentry,
1642 			     dn, dn);
1643 
1644 			/* ensure target dentry is invalidated, despite
1645 			   rehashing bug in vfs_rename_dir */
1646 			ceph_invalidate_dentry_lease(dn);
1647 
1648 			dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1649 			     ceph_dentry(req->r_old_dentry)->offset);
1650 
1651 			/* swap r_dentry and r_old_dentry in case that
1652 			 * splice_dentry() gets called later. This is safe
1653 			 * because no other place will use them */
1654 			req->r_dentry = req->r_old_dentry;
1655 			req->r_old_dentry = dn;
1656 			dn = req->r_dentry;
1657 		}
1658 
1659 		/* null dentry? */
1660 		if (!rinfo->head->is_target) {
1661 			dout("fill_trace null dentry\n");
1662 			if (d_really_is_positive(dn)) {
1663 				dout("d_delete %p\n", dn);
1664 				ceph_dir_clear_ordered(dir);
1665 				d_delete(dn);
1666 			} else if (have_lease) {
1667 				if (d_unhashed(dn))
1668 					d_add(dn, NULL);
1669 			}
1670 
1671 			if (!d_unhashed(dn) && have_lease)
1672 				update_dentry_lease(dir, dn,
1673 						    rinfo->dlease, session,
1674 						    req->r_request_started);
1675 			goto done;
1676 		}
1677 
1678 		/* attach proper inode */
1679 		if (d_really_is_negative(dn)) {
1680 			ceph_dir_clear_ordered(dir);
1681 			ihold(in);
1682 			err = splice_dentry(&req->r_dentry, in);
1683 			if (err < 0)
1684 				goto done;
1685 			dn = req->r_dentry;  /* may have spliced */
1686 		} else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1687 			dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1688 			     dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1689 			     ceph_vinop(in));
1690 			d_invalidate(dn);
1691 			have_lease = false;
1692 		}
1693 
1694 		if (have_lease) {
1695 			update_dentry_lease(dir, dn,
1696 					    rinfo->dlease, session,
1697 					    req->r_request_started);
1698 		}
1699 		dout(" final dn %p\n", dn);
1700 	} else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1701 		    req->r_op == CEPH_MDS_OP_MKSNAP) &&
1702 	           test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1703 		   !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1704 		struct inode *dir = req->r_parent;
1705 
1706 		/* fill out a snapdir LOOKUPSNAP dentry */
1707 		BUG_ON(!dir);
1708 		BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1709 		BUG_ON(!req->r_dentry);
1710 		dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1711 		ceph_dir_clear_ordered(dir);
1712 		ihold(in);
1713 		err = splice_dentry(&req->r_dentry, in);
1714 		if (err < 0)
1715 			goto done;
1716 	} else if (rinfo->head->is_dentry && req->r_dentry) {
1717 		/* parent inode is not locked, be carefull */
1718 		struct ceph_vino *ptvino = NULL;
1719 		dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1720 		dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1721 		if (rinfo->head->is_target) {
1722 			tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1723 			tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1724 			ptvino = &tvino;
1725 		}
1726 		update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1727 					    session, req->r_request_started,
1728 					    rinfo->dname, rinfo->dname_len,
1729 					    &dvino, ptvino);
1730 	}
1731 done:
1732 	dout("fill_trace done err=%d\n", err);
1733 	return err;
1734 }
1735 
1736 /*
1737  * Prepopulate our cache with readdir results, leases, etc.
1738  */
1739 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1740 					   struct ceph_mds_session *session)
1741 {
1742 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1743 	int i, err = 0;
1744 
1745 	for (i = 0; i < rinfo->dir_nr; i++) {
1746 		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1747 		struct ceph_vino vino;
1748 		struct inode *in;
1749 		int rc;
1750 
1751 		vino.ino = le64_to_cpu(rde->inode.in->ino);
1752 		vino.snap = le64_to_cpu(rde->inode.in->snapid);
1753 
1754 		in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL);
1755 		if (IS_ERR(in)) {
1756 			err = PTR_ERR(in);
1757 			dout("new_inode badness got %d\n", err);
1758 			continue;
1759 		}
1760 		rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1761 				     -1, &req->r_caps_reservation);
1762 		if (rc < 0) {
1763 			pr_err("ceph_fill_inode badness on %p got %d\n",
1764 			       in, rc);
1765 			err = rc;
1766 			if (in->i_state & I_NEW) {
1767 				ihold(in);
1768 				discard_new_inode(in);
1769 			}
1770 		} else if (in->i_state & I_NEW) {
1771 			unlock_new_inode(in);
1772 		}
1773 
1774 		iput(in);
1775 	}
1776 
1777 	return err;
1778 }
1779 
1780 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1781 {
1782 	if (ctl->page) {
1783 		kunmap(ctl->page);
1784 		put_page(ctl->page);
1785 		ctl->page = NULL;
1786 	}
1787 }
1788 
1789 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1790 			      struct ceph_readdir_cache_control *ctl,
1791 			      struct ceph_mds_request *req)
1792 {
1793 	struct ceph_inode_info *ci = ceph_inode(dir);
1794 	unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1795 	unsigned idx = ctl->index % nsize;
1796 	pgoff_t pgoff = ctl->index / nsize;
1797 
1798 	if (!ctl->page || pgoff != page_index(ctl->page)) {
1799 		ceph_readdir_cache_release(ctl);
1800 		if (idx == 0)
1801 			ctl->page = grab_cache_page(&dir->i_data, pgoff);
1802 		else
1803 			ctl->page = find_lock_page(&dir->i_data, pgoff);
1804 		if (!ctl->page) {
1805 			ctl->index = -1;
1806 			return idx == 0 ? -ENOMEM : 0;
1807 		}
1808 		/* reading/filling the cache are serialized by
1809 		 * i_rwsem, no need to use page lock */
1810 		unlock_page(ctl->page);
1811 		ctl->dentries = kmap(ctl->page);
1812 		if (idx == 0)
1813 			memset(ctl->dentries, 0, PAGE_SIZE);
1814 	}
1815 
1816 	if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1817 	    req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1818 		dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1819 		ctl->dentries[idx] = dn;
1820 		ctl->index++;
1821 	} else {
1822 		dout("disable readdir cache\n");
1823 		ctl->index = -1;
1824 	}
1825 	return 0;
1826 }
1827 
1828 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1829 			     struct ceph_mds_session *session)
1830 {
1831 	struct dentry *parent = req->r_dentry;
1832 	struct inode *inode = d_inode(parent);
1833 	struct ceph_inode_info *ci = ceph_inode(inode);
1834 	struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1835 	struct qstr dname;
1836 	struct dentry *dn;
1837 	struct inode *in;
1838 	int err = 0, skipped = 0, ret, i;
1839 	u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1840 	u32 last_hash = 0;
1841 	u32 fpos_offset;
1842 	struct ceph_readdir_cache_control cache_ctl = {};
1843 
1844 	if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1845 		return readdir_prepopulate_inodes_only(req, session);
1846 
1847 	if (rinfo->hash_order) {
1848 		if (req->r_path2) {
1849 			last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1850 						  req->r_path2,
1851 						  strlen(req->r_path2));
1852 			last_hash = ceph_frag_value(last_hash);
1853 		} else if (rinfo->offset_hash) {
1854 			/* mds understands offset_hash */
1855 			WARN_ON_ONCE(req->r_readdir_offset != 2);
1856 			last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
1857 		}
1858 	}
1859 
1860 	if (rinfo->dir_dir &&
1861 	    le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1862 		dout("readdir_prepopulate got new frag %x -> %x\n",
1863 		     frag, le32_to_cpu(rinfo->dir_dir->frag));
1864 		frag = le32_to_cpu(rinfo->dir_dir->frag);
1865 		if (!rinfo->hash_order)
1866 			req->r_readdir_offset = 2;
1867 	}
1868 
1869 	if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1870 		dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1871 		     rinfo->dir_nr, parent);
1872 	} else {
1873 		dout("readdir_prepopulate %d items under dn %p\n",
1874 		     rinfo->dir_nr, parent);
1875 		if (rinfo->dir_dir)
1876 			ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1877 
1878 		if (ceph_frag_is_leftmost(frag) &&
1879 		    req->r_readdir_offset == 2 &&
1880 		    !(rinfo->hash_order && last_hash)) {
1881 			/* note dir version at start of readdir so we can
1882 			 * tell if any dentries get dropped */
1883 			req->r_dir_release_cnt =
1884 				atomic64_read(&ci->i_release_count);
1885 			req->r_dir_ordered_cnt =
1886 				atomic64_read(&ci->i_ordered_count);
1887 			req->r_readdir_cache_idx = 0;
1888 		}
1889 	}
1890 
1891 	cache_ctl.index = req->r_readdir_cache_idx;
1892 	fpos_offset = req->r_readdir_offset;
1893 
1894 	/* FIXME: release caps/leases if error occurs */
1895 	for (i = 0; i < rinfo->dir_nr; i++) {
1896 		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1897 		struct ceph_vino tvino;
1898 
1899 		dname.name = rde->name;
1900 		dname.len = rde->name_len;
1901 		dname.hash = full_name_hash(parent, dname.name, dname.len);
1902 
1903 		tvino.ino = le64_to_cpu(rde->inode.in->ino);
1904 		tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1905 
1906 		if (rinfo->hash_order) {
1907 			u32 hash = ceph_frag_value(rde->raw_hash);
1908 			if (hash != last_hash)
1909 				fpos_offset = 2;
1910 			last_hash = hash;
1911 			rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1912 		} else {
1913 			rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1914 		}
1915 
1916 retry_lookup:
1917 		dn = d_lookup(parent, &dname);
1918 		dout("d_lookup on parent=%p name=%.*s got %p\n",
1919 		     parent, dname.len, dname.name, dn);
1920 
1921 		if (!dn) {
1922 			dn = d_alloc(parent, &dname);
1923 			dout("d_alloc %p '%.*s' = %p\n", parent,
1924 			     dname.len, dname.name, dn);
1925 			if (!dn) {
1926 				dout("d_alloc badness\n");
1927 				err = -ENOMEM;
1928 				goto out;
1929 			}
1930 			if (rde->is_nokey) {
1931 				spin_lock(&dn->d_lock);
1932 				dn->d_flags |= DCACHE_NOKEY_NAME;
1933 				spin_unlock(&dn->d_lock);
1934 			}
1935 		} else if (d_really_is_positive(dn) &&
1936 			   (ceph_ino(d_inode(dn)) != tvino.ino ||
1937 			    ceph_snap(d_inode(dn)) != tvino.snap)) {
1938 			struct ceph_dentry_info *di = ceph_dentry(dn);
1939 			dout(" dn %p points to wrong inode %p\n",
1940 			     dn, d_inode(dn));
1941 
1942 			spin_lock(&dn->d_lock);
1943 			if (di->offset > 0 &&
1944 			    di->lease_shared_gen ==
1945 			    atomic_read(&ci->i_shared_gen)) {
1946 				__ceph_dir_clear_ordered(ci);
1947 				di->offset = 0;
1948 			}
1949 			spin_unlock(&dn->d_lock);
1950 
1951 			d_delete(dn);
1952 			dput(dn);
1953 			goto retry_lookup;
1954 		}
1955 
1956 		/* inode */
1957 		if (d_really_is_positive(dn)) {
1958 			in = d_inode(dn);
1959 		} else {
1960 			in = ceph_get_inode(parent->d_sb, tvino, NULL);
1961 			if (IS_ERR(in)) {
1962 				dout("new_inode badness\n");
1963 				d_drop(dn);
1964 				dput(dn);
1965 				err = PTR_ERR(in);
1966 				goto out;
1967 			}
1968 		}
1969 
1970 		ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1971 				      -1, &req->r_caps_reservation);
1972 		if (ret < 0) {
1973 			pr_err("ceph_fill_inode badness on %p\n", in);
1974 			if (d_really_is_negative(dn)) {
1975 				if (in->i_state & I_NEW) {
1976 					ihold(in);
1977 					discard_new_inode(in);
1978 				}
1979 				iput(in);
1980 			}
1981 			d_drop(dn);
1982 			err = ret;
1983 			goto next_item;
1984 		}
1985 		if (in->i_state & I_NEW)
1986 			unlock_new_inode(in);
1987 
1988 		if (d_really_is_negative(dn)) {
1989 			if (ceph_security_xattr_deadlock(in)) {
1990 				dout(" skip splicing dn %p to inode %p"
1991 				     " (security xattr deadlock)\n", dn, in);
1992 				iput(in);
1993 				skipped++;
1994 				goto next_item;
1995 			}
1996 
1997 			err = splice_dentry(&dn, in);
1998 			if (err < 0)
1999 				goto next_item;
2000 		}
2001 
2002 		ceph_dentry(dn)->offset = rde->offset;
2003 
2004 		update_dentry_lease(d_inode(parent), dn,
2005 				    rde->lease, req->r_session,
2006 				    req->r_request_started);
2007 
2008 		if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
2009 			ret = fill_readdir_cache(d_inode(parent), dn,
2010 						 &cache_ctl, req);
2011 			if (ret < 0)
2012 				err = ret;
2013 		}
2014 next_item:
2015 		dput(dn);
2016 	}
2017 out:
2018 	if (err == 0 && skipped == 0) {
2019 		set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
2020 		req->r_readdir_cache_idx = cache_ctl.index;
2021 	}
2022 	ceph_readdir_cache_release(&cache_ctl);
2023 	dout("readdir_prepopulate done\n");
2024 	return err;
2025 }
2026 
2027 bool ceph_inode_set_size(struct inode *inode, loff_t size)
2028 {
2029 	struct ceph_inode_info *ci = ceph_inode(inode);
2030 	bool ret;
2031 
2032 	spin_lock(&ci->i_ceph_lock);
2033 	dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
2034 	i_size_write(inode, size);
2035 	ceph_fscache_update(inode);
2036 	inode->i_blocks = calc_inode_blocks(size);
2037 
2038 	ret = __ceph_should_report_size(ci);
2039 
2040 	spin_unlock(&ci->i_ceph_lock);
2041 
2042 	return ret;
2043 }
2044 
2045 void ceph_queue_inode_work(struct inode *inode, int work_bit)
2046 {
2047 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2048 	struct ceph_inode_info *ci = ceph_inode(inode);
2049 	set_bit(work_bit, &ci->i_work_mask);
2050 
2051 	ihold(inode);
2052 	if (queue_work(fsc->inode_wq, &ci->i_work)) {
2053 		dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask);
2054 	} else {
2055 		dout("queue_inode_work %p already queued, mask=%lx\n",
2056 		     inode, ci->i_work_mask);
2057 		iput(inode);
2058 	}
2059 }
2060 
2061 static void ceph_do_invalidate_pages(struct inode *inode)
2062 {
2063 	struct ceph_inode_info *ci = ceph_inode(inode);
2064 	u32 orig_gen;
2065 	int check = 0;
2066 
2067 	ceph_fscache_invalidate(inode, false);
2068 
2069 	mutex_lock(&ci->i_truncate_mutex);
2070 
2071 	if (ceph_inode_is_shutdown(inode)) {
2072 		pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n",
2073 				    __func__, ceph_vinop(inode));
2074 		mapping_set_error(inode->i_mapping, -EIO);
2075 		truncate_pagecache(inode, 0);
2076 		mutex_unlock(&ci->i_truncate_mutex);
2077 		goto out;
2078 	}
2079 
2080 	spin_lock(&ci->i_ceph_lock);
2081 	dout("invalidate_pages %p gen %d revoking %d\n", inode,
2082 	     ci->i_rdcache_gen, ci->i_rdcache_revoking);
2083 	if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2084 		if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2085 			check = 1;
2086 		spin_unlock(&ci->i_ceph_lock);
2087 		mutex_unlock(&ci->i_truncate_mutex);
2088 		goto out;
2089 	}
2090 	orig_gen = ci->i_rdcache_gen;
2091 	spin_unlock(&ci->i_ceph_lock);
2092 
2093 	if (invalidate_inode_pages2(inode->i_mapping) < 0) {
2094 		pr_err("invalidate_inode_pages2 %llx.%llx failed\n",
2095 		       ceph_vinop(inode));
2096 	}
2097 
2098 	spin_lock(&ci->i_ceph_lock);
2099 	if (orig_gen == ci->i_rdcache_gen &&
2100 	    orig_gen == ci->i_rdcache_revoking) {
2101 		dout("invalidate_pages %p gen %d successful\n", inode,
2102 		     ci->i_rdcache_gen);
2103 		ci->i_rdcache_revoking--;
2104 		check = 1;
2105 	} else {
2106 		dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
2107 		     inode, orig_gen, ci->i_rdcache_gen,
2108 		     ci->i_rdcache_revoking);
2109 		if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2110 			check = 1;
2111 	}
2112 	spin_unlock(&ci->i_ceph_lock);
2113 	mutex_unlock(&ci->i_truncate_mutex);
2114 out:
2115 	if (check)
2116 		ceph_check_caps(ci, 0);
2117 }
2118 
2119 /*
2120  * Make sure any pending truncation is applied before doing anything
2121  * that may depend on it.
2122  */
2123 void __ceph_do_pending_vmtruncate(struct inode *inode)
2124 {
2125 	struct ceph_inode_info *ci = ceph_inode(inode);
2126 	u64 to;
2127 	int wrbuffer_refs, finish = 0;
2128 
2129 	mutex_lock(&ci->i_truncate_mutex);
2130 retry:
2131 	spin_lock(&ci->i_ceph_lock);
2132 	if (ci->i_truncate_pending == 0) {
2133 		dout("__do_pending_vmtruncate %p none pending\n", inode);
2134 		spin_unlock(&ci->i_ceph_lock);
2135 		mutex_unlock(&ci->i_truncate_mutex);
2136 		return;
2137 	}
2138 
2139 	/*
2140 	 * make sure any dirty snapped pages are flushed before we
2141 	 * possibly truncate them.. so write AND block!
2142 	 */
2143 	if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
2144 		spin_unlock(&ci->i_ceph_lock);
2145 		dout("__do_pending_vmtruncate %p flushing snaps first\n",
2146 		     inode);
2147 		filemap_write_and_wait_range(&inode->i_data, 0,
2148 					     inode->i_sb->s_maxbytes);
2149 		goto retry;
2150 	}
2151 
2152 	/* there should be no reader or writer */
2153 	WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
2154 
2155 	to = ci->i_truncate_pagecache_size;
2156 	wrbuffer_refs = ci->i_wrbuffer_ref;
2157 	dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
2158 	     ci->i_truncate_pending, to);
2159 	spin_unlock(&ci->i_ceph_lock);
2160 
2161 	ceph_fscache_resize(inode, to);
2162 	truncate_pagecache(inode, to);
2163 
2164 	spin_lock(&ci->i_ceph_lock);
2165 	if (to == ci->i_truncate_pagecache_size) {
2166 		ci->i_truncate_pending = 0;
2167 		finish = 1;
2168 	}
2169 	spin_unlock(&ci->i_ceph_lock);
2170 	if (!finish)
2171 		goto retry;
2172 
2173 	mutex_unlock(&ci->i_truncate_mutex);
2174 
2175 	if (wrbuffer_refs == 0)
2176 		ceph_check_caps(ci, 0);
2177 
2178 	wake_up_all(&ci->i_cap_wq);
2179 }
2180 
2181 static void ceph_inode_work(struct work_struct *work)
2182 {
2183 	struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
2184 						 i_work);
2185 	struct inode *inode = &ci->netfs.inode;
2186 
2187 	if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
2188 		dout("writeback %p\n", inode);
2189 		filemap_fdatawrite(&inode->i_data);
2190 	}
2191 	if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
2192 		ceph_do_invalidate_pages(inode);
2193 
2194 	if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
2195 		__ceph_do_pending_vmtruncate(inode);
2196 
2197 	if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
2198 		ceph_check_caps(ci, 0);
2199 
2200 	if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
2201 		ceph_flush_snaps(ci, NULL);
2202 
2203 	iput(inode);
2204 }
2205 
2206 static const char *ceph_encrypted_get_link(struct dentry *dentry,
2207 					   struct inode *inode,
2208 					   struct delayed_call *done)
2209 {
2210 	struct ceph_inode_info *ci = ceph_inode(inode);
2211 
2212 	if (!dentry)
2213 		return ERR_PTR(-ECHILD);
2214 
2215 	return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
2216 				   done);
2217 }
2218 
2219 static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap,
2220 					  const struct path *path,
2221 					  struct kstat *stat, u32 request_mask,
2222 					  unsigned int query_flags)
2223 {
2224 	int ret;
2225 
2226 	ret = ceph_getattr(idmap, path, stat, request_mask, query_flags);
2227 	if (ret)
2228 		return ret;
2229 	return fscrypt_symlink_getattr(path, stat);
2230 }
2231 
2232 /*
2233  * symlinks
2234  */
2235 static const struct inode_operations ceph_symlink_iops = {
2236 	.get_link = simple_get_link,
2237 	.setattr = ceph_setattr,
2238 	.getattr = ceph_getattr,
2239 	.listxattr = ceph_listxattr,
2240 };
2241 
2242 static const struct inode_operations ceph_encrypted_symlink_iops = {
2243 	.get_link = ceph_encrypted_get_link,
2244 	.setattr = ceph_setattr,
2245 	.getattr = ceph_encrypted_symlink_getattr,
2246 	.listxattr = ceph_listxattr,
2247 };
2248 
2249 /*
2250  * Transfer the encrypted last block to the MDS and the MDS
2251  * will help update it when truncating a smaller size.
2252  *
2253  * We don't support a PAGE_SIZE that is smaller than the
2254  * CEPH_FSCRYPT_BLOCK_SIZE.
2255  */
2256 static int fill_fscrypt_truncate(struct inode *inode,
2257 				 struct ceph_mds_request *req,
2258 				 struct iattr *attr)
2259 {
2260 	struct ceph_inode_info *ci = ceph_inode(inode);
2261 	int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE;
2262 	loff_t pos, orig_pos = round_down(attr->ia_size,
2263 					  CEPH_FSCRYPT_BLOCK_SIZE);
2264 	u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT;
2265 	struct ceph_pagelist *pagelist = NULL;
2266 	struct kvec iov = {0};
2267 	struct iov_iter iter;
2268 	struct page *page = NULL;
2269 	struct ceph_fscrypt_truncate_size_header header;
2270 	int retry_op = 0;
2271 	int len = CEPH_FSCRYPT_BLOCK_SIZE;
2272 	loff_t i_size = i_size_read(inode);
2273 	int got, ret, issued;
2274 	u64 objver;
2275 
2276 	ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got);
2277 	if (ret < 0)
2278 		return ret;
2279 
2280 	issued = __ceph_caps_issued(ci, NULL);
2281 
2282 	dout("%s size %lld -> %lld got cap refs on %s, issued %s\n", __func__,
2283 	     i_size, attr->ia_size, ceph_cap_string(got),
2284 	     ceph_cap_string(issued));
2285 
2286 	/* Try to writeback the dirty pagecaches */
2287 	if (issued & (CEPH_CAP_FILE_BUFFER)) {
2288 		loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SHIFT - 1;
2289 
2290 		ret = filemap_write_and_wait_range(inode->i_mapping,
2291 						   orig_pos, lend);
2292 		if (ret < 0)
2293 			goto out;
2294 	}
2295 
2296 	page = __page_cache_alloc(GFP_KERNEL);
2297 	if (page == NULL) {
2298 		ret = -ENOMEM;
2299 		goto out;
2300 	}
2301 
2302 	pagelist = ceph_pagelist_alloc(GFP_KERNEL);
2303 	if (!pagelist) {
2304 		ret = -ENOMEM;
2305 		goto out;
2306 	}
2307 
2308 	iov.iov_base = kmap_local_page(page);
2309 	iov.iov_len = len;
2310 	iov_iter_kvec(&iter, READ, &iov, 1, len);
2311 
2312 	pos = orig_pos;
2313 	ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver);
2314 	if (ret < 0)
2315 		goto out;
2316 
2317 	/* Insert the header first */
2318 	header.ver = 1;
2319 	header.compat = 1;
2320 	header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode));
2321 
2322 	/*
2323 	 * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE,
2324 	 * because in MDS it may need this to do the truncate.
2325 	 */
2326 	header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE);
2327 
2328 	/*
2329 	 * If we hit a hole here, we should just skip filling
2330 	 * the fscrypt for the request, because once the fscrypt
2331 	 * is enabled, the file will be split into many blocks
2332 	 * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there
2333 	 * has a hole, the hole size should be multiple of block
2334 	 * size.
2335 	 *
2336 	 * If the Rados object doesn't exist, it will be set to 0.
2337 	 */
2338 	if (!objver) {
2339 		dout("%s hit hole, ppos %lld < size %lld\n", __func__,
2340 		     pos, i_size);
2341 
2342 		header.data_len = cpu_to_le32(8 + 8 + 4);
2343 		header.file_offset = 0;
2344 		ret = 0;
2345 	} else {
2346 		header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE);
2347 		header.file_offset = cpu_to_le64(orig_pos);
2348 
2349 		/* truncate and zero out the extra contents for the last block */
2350 		memset(iov.iov_base + boff, 0, PAGE_SIZE - boff);
2351 
2352 		/* encrypt the last block */
2353 		ret = ceph_fscrypt_encrypt_block_inplace(inode, page,
2354 						    CEPH_FSCRYPT_BLOCK_SIZE,
2355 						    0, block,
2356 						    GFP_KERNEL);
2357 		if (ret)
2358 			goto out;
2359 	}
2360 
2361 	/* Insert the header */
2362 	ret = ceph_pagelist_append(pagelist, &header, sizeof(header));
2363 	if (ret)
2364 		goto out;
2365 
2366 	if (header.block_size) {
2367 		/* Append the last block contents to pagelist */
2368 		ret = ceph_pagelist_append(pagelist, iov.iov_base,
2369 					   CEPH_FSCRYPT_BLOCK_SIZE);
2370 		if (ret)
2371 			goto out;
2372 	}
2373 	req->r_pagelist = pagelist;
2374 out:
2375 	dout("%s %p size dropping cap refs on %s\n", __func__,
2376 	     inode, ceph_cap_string(got));
2377 	ceph_put_cap_refs(ci, got);
2378 	if (iov.iov_base)
2379 		kunmap_local(iov.iov_base);
2380 	if (page)
2381 		__free_pages(page, 0);
2382 	if (ret && pagelist)
2383 		ceph_pagelist_release(pagelist);
2384 	return ret;
2385 }
2386 
2387 int __ceph_setattr(struct inode *inode, struct iattr *attr,
2388 		   struct ceph_iattr *cia)
2389 {
2390 	struct ceph_inode_info *ci = ceph_inode(inode);
2391 	unsigned int ia_valid = attr->ia_valid;
2392 	struct ceph_mds_request *req;
2393 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2394 	struct ceph_cap_flush *prealloc_cf;
2395 	loff_t isize = i_size_read(inode);
2396 	int issued;
2397 	int release = 0, dirtied = 0;
2398 	int mask = 0;
2399 	int err = 0;
2400 	int inode_dirty_flags = 0;
2401 	bool lock_snap_rwsem = false;
2402 	bool fill_fscrypt;
2403 	int truncate_retry = 20; /* The RMW will take around 50ms */
2404 
2405 retry:
2406 	prealloc_cf = ceph_alloc_cap_flush();
2407 	if (!prealloc_cf)
2408 		return -ENOMEM;
2409 
2410 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2411 				       USE_AUTH_MDS);
2412 	if (IS_ERR(req)) {
2413 		ceph_free_cap_flush(prealloc_cf);
2414 		return PTR_ERR(req);
2415 	}
2416 
2417 	fill_fscrypt = false;
2418 	spin_lock(&ci->i_ceph_lock);
2419 	issued = __ceph_caps_issued(ci, NULL);
2420 
2421 	if (!ci->i_head_snapc &&
2422 	    (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2423 		lock_snap_rwsem = true;
2424 		if (!down_read_trylock(&mdsc->snap_rwsem)) {
2425 			spin_unlock(&ci->i_ceph_lock);
2426 			down_read(&mdsc->snap_rwsem);
2427 			spin_lock(&ci->i_ceph_lock);
2428 			issued = __ceph_caps_issued(ci, NULL);
2429 		}
2430 	}
2431 
2432 	dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2433 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
2434 	if (cia && cia->fscrypt_auth) {
2435 		u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth);
2436 
2437 		if (len > sizeof(*cia->fscrypt_auth)) {
2438 			err = -EINVAL;
2439 			spin_unlock(&ci->i_ceph_lock);
2440 			goto out;
2441 		}
2442 
2443 		dout("setattr %llx:%llx fscrypt_auth len %u to %u)\n",
2444 			ceph_vinop(inode), ci->fscrypt_auth_len, len);
2445 
2446 		/* It should never be re-set once set */
2447 		WARN_ON_ONCE(ci->fscrypt_auth);
2448 
2449 		if (issued & CEPH_CAP_AUTH_EXCL) {
2450 			dirtied |= CEPH_CAP_AUTH_EXCL;
2451 			kfree(ci->fscrypt_auth);
2452 			ci->fscrypt_auth = (u8 *)cia->fscrypt_auth;
2453 			ci->fscrypt_auth_len = len;
2454 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2455 			   ci->fscrypt_auth_len != len ||
2456 			   memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) {
2457 			req->r_fscrypt_auth = cia->fscrypt_auth;
2458 			mask |= CEPH_SETATTR_FSCRYPT_AUTH;
2459 			release |= CEPH_CAP_AUTH_SHARED;
2460 		}
2461 		cia->fscrypt_auth = NULL;
2462 	}
2463 #else
2464 	if (cia && cia->fscrypt_auth) {
2465 		err = -EINVAL;
2466 		spin_unlock(&ci->i_ceph_lock);
2467 		goto out;
2468 	}
2469 #endif /* CONFIG_FS_ENCRYPTION */
2470 
2471 	if (ia_valid & ATTR_UID) {
2472 		dout("setattr %p uid %d -> %d\n", inode,
2473 		     from_kuid(&init_user_ns, inode->i_uid),
2474 		     from_kuid(&init_user_ns, attr->ia_uid));
2475 		if (issued & CEPH_CAP_AUTH_EXCL) {
2476 			inode->i_uid = attr->ia_uid;
2477 			dirtied |= CEPH_CAP_AUTH_EXCL;
2478 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2479 			   !uid_eq(attr->ia_uid, inode->i_uid)) {
2480 			req->r_args.setattr.uid = cpu_to_le32(
2481 				from_kuid(&init_user_ns, attr->ia_uid));
2482 			mask |= CEPH_SETATTR_UID;
2483 			release |= CEPH_CAP_AUTH_SHARED;
2484 		}
2485 	}
2486 	if (ia_valid & ATTR_GID) {
2487 		dout("setattr %p gid %d -> %d\n", inode,
2488 		     from_kgid(&init_user_ns, inode->i_gid),
2489 		     from_kgid(&init_user_ns, attr->ia_gid));
2490 		if (issued & CEPH_CAP_AUTH_EXCL) {
2491 			inode->i_gid = attr->ia_gid;
2492 			dirtied |= CEPH_CAP_AUTH_EXCL;
2493 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2494 			   !gid_eq(attr->ia_gid, inode->i_gid)) {
2495 			req->r_args.setattr.gid = cpu_to_le32(
2496 				from_kgid(&init_user_ns, attr->ia_gid));
2497 			mask |= CEPH_SETATTR_GID;
2498 			release |= CEPH_CAP_AUTH_SHARED;
2499 		}
2500 	}
2501 	if (ia_valid & ATTR_MODE) {
2502 		dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2503 		     attr->ia_mode);
2504 		if (issued & CEPH_CAP_AUTH_EXCL) {
2505 			inode->i_mode = attr->ia_mode;
2506 			dirtied |= CEPH_CAP_AUTH_EXCL;
2507 		} else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2508 			   attr->ia_mode != inode->i_mode) {
2509 			inode->i_mode = attr->ia_mode;
2510 			req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2511 			mask |= CEPH_SETATTR_MODE;
2512 			release |= CEPH_CAP_AUTH_SHARED;
2513 		}
2514 	}
2515 
2516 	if (ia_valid & ATTR_ATIME) {
2517 		dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2518 		     inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2519 		     attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2520 		if (issued & CEPH_CAP_FILE_EXCL) {
2521 			ci->i_time_warp_seq++;
2522 			inode->i_atime = attr->ia_atime;
2523 			dirtied |= CEPH_CAP_FILE_EXCL;
2524 		} else if ((issued & CEPH_CAP_FILE_WR) &&
2525 			   timespec64_compare(&inode->i_atime,
2526 					    &attr->ia_atime) < 0) {
2527 			inode->i_atime = attr->ia_atime;
2528 			dirtied |= CEPH_CAP_FILE_WR;
2529 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2530 			   !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2531 			ceph_encode_timespec64(&req->r_args.setattr.atime,
2532 					       &attr->ia_atime);
2533 			mask |= CEPH_SETATTR_ATIME;
2534 			release |= CEPH_CAP_FILE_SHARED |
2535 				   CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2536 		}
2537 	}
2538 	if (ia_valid & ATTR_SIZE) {
2539 		dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size);
2540 		/*
2541 		 * Only when the new size is smaller and not aligned to
2542 		 * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed.
2543 		 */
2544 		if (IS_ENCRYPTED(inode) && attr->ia_size < isize &&
2545 		    (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) {
2546 			mask |= CEPH_SETATTR_SIZE;
2547 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2548 				   CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2549 			set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2550 			mask |= CEPH_SETATTR_FSCRYPT_FILE;
2551 			req->r_args.setattr.size =
2552 				cpu_to_le64(round_up(attr->ia_size,
2553 						     CEPH_FSCRYPT_BLOCK_SIZE));
2554 			req->r_args.setattr.old_size =
2555 				cpu_to_le64(round_up(isize,
2556 						     CEPH_FSCRYPT_BLOCK_SIZE));
2557 			req->r_fscrypt_file = attr->ia_size;
2558 			fill_fscrypt = true;
2559 		} else if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
2560 			if (attr->ia_size > isize) {
2561 				i_size_write(inode, attr->ia_size);
2562 				inode->i_blocks = calc_inode_blocks(attr->ia_size);
2563 				ci->i_reported_size = attr->ia_size;
2564 				dirtied |= CEPH_CAP_FILE_EXCL;
2565 				ia_valid |= ATTR_MTIME;
2566 			}
2567 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2568 			   attr->ia_size != isize) {
2569 			mask |= CEPH_SETATTR_SIZE;
2570 			release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2571 				   CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2572 			if (IS_ENCRYPTED(inode) && attr->ia_size) {
2573 				set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2574 				mask |= CEPH_SETATTR_FSCRYPT_FILE;
2575 				req->r_args.setattr.size =
2576 					cpu_to_le64(round_up(attr->ia_size,
2577 							     CEPH_FSCRYPT_BLOCK_SIZE));
2578 				req->r_args.setattr.old_size =
2579 					cpu_to_le64(round_up(isize,
2580 							     CEPH_FSCRYPT_BLOCK_SIZE));
2581 				req->r_fscrypt_file = attr->ia_size;
2582 			} else {
2583 				req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2584 				req->r_args.setattr.old_size = cpu_to_le64(isize);
2585 				req->r_fscrypt_file = 0;
2586 			}
2587 		}
2588 	}
2589 	if (ia_valid & ATTR_MTIME) {
2590 		dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2591 		     inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2592 		     attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2593 		if (issued & CEPH_CAP_FILE_EXCL) {
2594 			ci->i_time_warp_seq++;
2595 			inode->i_mtime = attr->ia_mtime;
2596 			dirtied |= CEPH_CAP_FILE_EXCL;
2597 		} else if ((issued & CEPH_CAP_FILE_WR) &&
2598 			   timespec64_compare(&inode->i_mtime,
2599 					    &attr->ia_mtime) < 0) {
2600 			inode->i_mtime = attr->ia_mtime;
2601 			dirtied |= CEPH_CAP_FILE_WR;
2602 		} else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2603 			   !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2604 			ceph_encode_timespec64(&req->r_args.setattr.mtime,
2605 					       &attr->ia_mtime);
2606 			mask |= CEPH_SETATTR_MTIME;
2607 			release |= CEPH_CAP_FILE_SHARED |
2608 				   CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2609 		}
2610 	}
2611 
2612 	/* these do nothing */
2613 	if (ia_valid & ATTR_CTIME) {
2614 		bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2615 					 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2616 		dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2617 		     inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2618 		     attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2619 		     only ? "ctime only" : "ignored");
2620 		if (only) {
2621 			/*
2622 			 * if kernel wants to dirty ctime but nothing else,
2623 			 * we need to choose a cap to dirty under, or do
2624 			 * a almost-no-op setattr
2625 			 */
2626 			if (issued & CEPH_CAP_AUTH_EXCL)
2627 				dirtied |= CEPH_CAP_AUTH_EXCL;
2628 			else if (issued & CEPH_CAP_FILE_EXCL)
2629 				dirtied |= CEPH_CAP_FILE_EXCL;
2630 			else if (issued & CEPH_CAP_XATTR_EXCL)
2631 				dirtied |= CEPH_CAP_XATTR_EXCL;
2632 			else
2633 				mask |= CEPH_SETATTR_CTIME;
2634 		}
2635 	}
2636 	if (ia_valid & ATTR_FILE)
2637 		dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2638 
2639 	if (dirtied) {
2640 		inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2641 							   &prealloc_cf);
2642 		inode->i_ctime = attr->ia_ctime;
2643 		inode_inc_iversion_raw(inode);
2644 	}
2645 
2646 	release &= issued;
2647 	spin_unlock(&ci->i_ceph_lock);
2648 	if (lock_snap_rwsem) {
2649 		up_read(&mdsc->snap_rwsem);
2650 		lock_snap_rwsem = false;
2651 	}
2652 
2653 	if (inode_dirty_flags)
2654 		__mark_inode_dirty(inode, inode_dirty_flags);
2655 
2656 	if (mask) {
2657 		req->r_inode = inode;
2658 		ihold(inode);
2659 		req->r_inode_drop = release;
2660 		req->r_args.setattr.mask = cpu_to_le32(mask);
2661 		req->r_num_caps = 1;
2662 		req->r_stamp = attr->ia_ctime;
2663 		if (fill_fscrypt) {
2664 			err = fill_fscrypt_truncate(inode, req, attr);
2665 			if (err)
2666 				goto out;
2667 		}
2668 
2669 		/*
2670 		 * The truncate request will return -EAGAIN when the
2671 		 * last block has been updated just before the MDS
2672 		 * successfully gets the xlock for the FILE lock. To
2673 		 * avoid corrupting the file contents we need to retry
2674 		 * it.
2675 		 */
2676 		err = ceph_mdsc_do_request(mdsc, NULL, req);
2677 		if (err == -EAGAIN && truncate_retry--) {
2678 			dout("setattr %p result=%d (%s locally, %d remote), retry it!\n",
2679 			     inode, err, ceph_cap_string(dirtied), mask);
2680 			ceph_mdsc_put_request(req);
2681 			ceph_free_cap_flush(prealloc_cf);
2682 			goto retry;
2683 		}
2684 	}
2685 out:
2686 	dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2687 	     ceph_cap_string(dirtied), mask);
2688 
2689 	ceph_mdsc_put_request(req);
2690 	ceph_free_cap_flush(prealloc_cf);
2691 
2692 	if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2693 		__ceph_do_pending_vmtruncate(inode);
2694 
2695 	return err;
2696 }
2697 
2698 /*
2699  * setattr
2700  */
2701 int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2702 		 struct iattr *attr)
2703 {
2704 	struct inode *inode = d_inode(dentry);
2705 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2706 	int err;
2707 
2708 	if (ceph_snap(inode) != CEPH_NOSNAP)
2709 		return -EROFS;
2710 
2711 	if (ceph_inode_is_shutdown(inode))
2712 		return -ESTALE;
2713 
2714 	err = fscrypt_prepare_setattr(dentry, attr);
2715 	if (err)
2716 		return err;
2717 
2718 	err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2719 	if (err != 0)
2720 		return err;
2721 
2722 	if ((attr->ia_valid & ATTR_SIZE) &&
2723 	    attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
2724 		return -EFBIG;
2725 
2726 	if ((attr->ia_valid & ATTR_SIZE) &&
2727 	    ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2728 		return -EDQUOT;
2729 
2730 	err = __ceph_setattr(inode, attr, NULL);
2731 
2732 	if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2733 		err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode);
2734 
2735 	return err;
2736 }
2737 
2738 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
2739 {
2740 	int issued = ceph_caps_issued(ceph_inode(inode));
2741 
2742 	/*
2743 	 * If any 'x' caps is issued we can just choose the auth MDS
2744 	 * instead of the random replica MDSes. Because only when the
2745 	 * Locker is in LOCK_EXEC state will the loner client could
2746 	 * get the 'x' caps. And if we send the getattr requests to
2747 	 * any replica MDS it must auth pin and tries to rdlock from
2748 	 * the auth MDS, and then the auth MDS need to do the Locker
2749 	 * state transition to LOCK_SYNC. And after that the lock state
2750 	 * will change back.
2751 	 *
2752 	 * This cost much when doing the Locker state transition and
2753 	 * usually will need to revoke caps from clients.
2754 	 *
2755 	 * And for the 'Xs' caps for getxattr we will also choose the
2756 	 * auth MDS, because the MDS side code is buggy due to setxattr
2757 	 * won't notify the replica MDSes when the values changed and
2758 	 * the replica MDS will return the old values. Though we will
2759 	 * fix it in MDS code, but this still makes sense for old ceph.
2760 	 */
2761 	if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
2762 	    || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
2763 		return USE_AUTH_MDS;
2764 	else
2765 		return USE_ANY_MDS;
2766 }
2767 
2768 /*
2769  * Verify that we have a lease on the given mask.  If not,
2770  * do a getattr against an mds.
2771  */
2772 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2773 		      int mask, bool force)
2774 {
2775 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2776 	struct ceph_mds_client *mdsc = fsc->mdsc;
2777 	struct ceph_mds_request *req;
2778 	int mode;
2779 	int err;
2780 
2781 	if (ceph_snap(inode) == CEPH_SNAPDIR) {
2782 		dout("do_getattr inode %p SNAPDIR\n", inode);
2783 		return 0;
2784 	}
2785 
2786 	dout("do_getattr inode %p mask %s mode 0%o\n",
2787 	     inode, ceph_cap_string(mask), inode->i_mode);
2788 	if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2789 			return 0;
2790 
2791 	mode = ceph_try_to_choose_auth_mds(inode, mask);
2792 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2793 	if (IS_ERR(req))
2794 		return PTR_ERR(req);
2795 	req->r_inode = inode;
2796 	ihold(inode);
2797 	req->r_num_caps = 1;
2798 	req->r_args.getattr.mask = cpu_to_le32(mask);
2799 	req->r_locked_page = locked_page;
2800 	err = ceph_mdsc_do_request(mdsc, NULL, req);
2801 	if (locked_page && err == 0) {
2802 		u64 inline_version = req->r_reply_info.targeti.inline_version;
2803 		if (inline_version == 0) {
2804 			/* the reply is supposed to contain inline data */
2805 			err = -EINVAL;
2806 		} else if (inline_version == CEPH_INLINE_NONE ||
2807 			   inline_version == 1) {
2808 			err = -ENODATA;
2809 		} else {
2810 			err = req->r_reply_info.targeti.inline_len;
2811 		}
2812 	}
2813 	ceph_mdsc_put_request(req);
2814 	dout("do_getattr result=%d\n", err);
2815 	return err;
2816 }
2817 
2818 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
2819 		      size_t size)
2820 {
2821 	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2822 	struct ceph_mds_client *mdsc = fsc->mdsc;
2823 	struct ceph_mds_request *req;
2824 	int mode = USE_AUTH_MDS;
2825 	int err;
2826 	char *xattr_value;
2827 	size_t xattr_value_len;
2828 
2829 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
2830 	if (IS_ERR(req)) {
2831 		err = -ENOMEM;
2832 		goto out;
2833 	}
2834 
2835 	req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR;
2836 	req->r_path2 = kstrdup(name, GFP_NOFS);
2837 	if (!req->r_path2) {
2838 		err = -ENOMEM;
2839 		goto put;
2840 	}
2841 
2842 	ihold(inode);
2843 	req->r_inode = inode;
2844 	err = ceph_mdsc_do_request(mdsc, NULL, req);
2845 	if (err < 0)
2846 		goto put;
2847 
2848 	xattr_value = req->r_reply_info.xattr_info.xattr_value;
2849 	xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
2850 
2851 	dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
2852 
2853 	err = (int)xattr_value_len;
2854 	if (size == 0)
2855 		goto put;
2856 
2857 	if (xattr_value_len > size) {
2858 		err = -ERANGE;
2859 		goto put;
2860 	}
2861 
2862 	memcpy(value, xattr_value, xattr_value_len);
2863 put:
2864 	ceph_mdsc_put_request(req);
2865 out:
2866 	dout("do_getvxattr result=%d\n", err);
2867 	return err;
2868 }
2869 
2870 
2871 /*
2872  * Check inode permissions.  We verify we have a valid value for
2873  * the AUTH cap, then call the generic handler.
2874  */
2875 int ceph_permission(struct mnt_idmap *idmap, struct inode *inode,
2876 		    int mask)
2877 {
2878 	int err;
2879 
2880 	if (mask & MAY_NOT_BLOCK)
2881 		return -ECHILD;
2882 
2883 	err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2884 
2885 	if (!err)
2886 		err = generic_permission(&nop_mnt_idmap, inode, mask);
2887 	return err;
2888 }
2889 
2890 /* Craft a mask of needed caps given a set of requested statx attrs. */
2891 static int statx_to_caps(u32 want, umode_t mode)
2892 {
2893 	int mask = 0;
2894 
2895 	if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE))
2896 		mask |= CEPH_CAP_AUTH_SHARED;
2897 
2898 	if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) {
2899 		/*
2900 		 * The link count for directories depends on inode->i_subdirs,
2901 		 * and that is only updated when Fs caps are held.
2902 		 */
2903 		if (S_ISDIR(mode))
2904 			mask |= CEPH_CAP_FILE_SHARED;
2905 		else
2906 			mask |= CEPH_CAP_LINK_SHARED;
2907 	}
2908 
2909 	if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE))
2910 		mask |= CEPH_CAP_FILE_SHARED;
2911 
2912 	if (want & (STATX_CTIME|STATX_CHANGE_COOKIE))
2913 		mask |= CEPH_CAP_XATTR_SHARED;
2914 
2915 	return mask;
2916 }
2917 
2918 /*
2919  * Get all the attributes. If we have sufficient caps for the requested attrs,
2920  * then we can avoid talking to the MDS at all.
2921  */
2922 int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
2923 		 struct kstat *stat, u32 request_mask, unsigned int flags)
2924 {
2925 	struct inode *inode = d_inode(path->dentry);
2926 	struct super_block *sb = inode->i_sb;
2927 	struct ceph_inode_info *ci = ceph_inode(inode);
2928 	u32 valid_mask = STATX_BASIC_STATS;
2929 	int err = 0;
2930 
2931 	if (ceph_inode_is_shutdown(inode))
2932 		return -ESTALE;
2933 
2934 	/* Skip the getattr altogether if we're asked not to sync */
2935 	if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
2936 		err = ceph_do_getattr(inode,
2937 				statx_to_caps(request_mask, inode->i_mode),
2938 				flags & AT_STATX_FORCE_SYNC);
2939 		if (err)
2940 			return err;
2941 	}
2942 
2943 	generic_fillattr(&nop_mnt_idmap, inode, stat);
2944 	stat->ino = ceph_present_inode(inode);
2945 
2946 	/*
2947 	 * btime on newly-allocated inodes is 0, so if this is still set to
2948 	 * that, then assume that it's not valid.
2949 	 */
2950 	if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2951 		stat->btime = ci->i_btime;
2952 		valid_mask |= STATX_BTIME;
2953 	}
2954 
2955 	if (request_mask & STATX_CHANGE_COOKIE) {
2956 		stat->change_cookie = inode_peek_iversion_raw(inode);
2957 		valid_mask |= STATX_CHANGE_COOKIE;
2958 	}
2959 
2960 	if (ceph_snap(inode) == CEPH_NOSNAP)
2961 		stat->dev = sb->s_dev;
2962 	else
2963 		stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
2964 
2965 	if (S_ISDIR(inode->i_mode)) {
2966 		if (ceph_test_mount_opt(ceph_sb_to_client(sb), RBYTES)) {
2967 			stat->size = ci->i_rbytes;
2968 		} else if (ceph_snap(inode) == CEPH_SNAPDIR) {
2969 			struct ceph_inode_info *pci;
2970 			struct ceph_snap_realm *realm;
2971 			struct inode *parent;
2972 
2973 			parent = ceph_lookup_inode(sb, ceph_ino(inode));
2974 			if (IS_ERR(parent))
2975 				return PTR_ERR(parent);
2976 
2977 			pci = ceph_inode(parent);
2978 			spin_lock(&pci->i_ceph_lock);
2979 			realm = pci->i_snap_realm;
2980 			if (realm)
2981 				stat->size = realm->num_snaps;
2982 			else
2983 				stat->size = 0;
2984 			spin_unlock(&pci->i_ceph_lock);
2985 			iput(parent);
2986 		} else {
2987 			stat->size = ci->i_files + ci->i_subdirs;
2988 		}
2989 		stat->blocks = 0;
2990 		stat->blksize = 65536;
2991 		/*
2992 		 * Some applications rely on the number of st_nlink
2993 		 * value on directories to be either 0 (if unlinked)
2994 		 * or 2 + number of subdirectories.
2995 		 */
2996 		if (stat->nlink == 1)
2997 			/* '.' + '..' + subdirs */
2998 			stat->nlink = 1 + 1 + ci->i_subdirs;
2999 	}
3000 
3001 	stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
3002 	if (IS_ENCRYPTED(inode))
3003 		stat->attributes |= STATX_ATTR_ENCRYPTED;
3004 	stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC |
3005 				  STATX_ATTR_ENCRYPTED);
3006 
3007 	stat->result_mask = request_mask & valid_mask;
3008 	return err;
3009 }
3010 
3011 void ceph_inode_shutdown(struct inode *inode)
3012 {
3013 	struct ceph_inode_info *ci = ceph_inode(inode);
3014 	struct rb_node *p;
3015 	int iputs = 0;
3016 	bool invalidate = false;
3017 
3018 	spin_lock(&ci->i_ceph_lock);
3019 	ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
3020 	p = rb_first(&ci->i_caps);
3021 	while (p) {
3022 		struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
3023 
3024 		p = rb_next(p);
3025 		iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
3026 	}
3027 	spin_unlock(&ci->i_ceph_lock);
3028 
3029 	if (invalidate)
3030 		ceph_queue_invalidate(inode);
3031 	while (iputs--)
3032 		iput(inode);
3033 }
3034