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