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