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