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