1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.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>
20 #include "mds_client.h"
23 #include <linux/ceph/decode.h>
26 * Ceph inode operations
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.
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).
37 static const struct inode_operations ceph_symlink_iops;
38 static const struct inode_operations ceph_encrypted_symlink_iops;
40 static void ceph_inode_work(struct work_struct *work);
43 * find or create an inode, given the ceph ino number
45 static int ceph_set_ino_cb(struct inode *inode, void *data)
47 struct ceph_inode_info *ci = ceph_inode(inode);
48 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
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);
59 * ceph_new_inode - allocate a new inode in advance of an expected create
60 * @dir: parent directory for new inode
61 * @dentry: dentry that may eventually point to new inode
62 * @mode: mode of new inode
63 * @as_ctx: pointer to inherited security context
65 * Allocate a new inode in advance of an operation to create a new inode.
66 * This allocates the inode and sets up the acl_sec_ctx with appropriate
67 * info for the new inode.
69 * Returns a pointer to the new inode or an ERR_PTR.
71 struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
72 umode_t *mode, struct ceph_acl_sec_ctx *as_ctx)
77 inode = new_inode(dir->i_sb);
79 return ERR_PTR(-ENOMEM);
81 if (!S_ISLNK(*mode)) {
82 err = ceph_pre_init_acls(dir, mode, as_ctx);
88 inode->i_mode = *mode;
90 err = ceph_security_init_secctx(dentry, *mode, as_ctx);
95 * We'll skip setting fscrypt context for snapshots, leaving that for
98 if (ceph_snap(dir) != CEPH_SNAPDIR) {
99 err = ceph_fscrypt_prepare_context(dir, inode, as_ctx);
110 void ceph_as_ctx_to_req(struct ceph_mds_request *req,
111 struct ceph_acl_sec_ctx *as_ctx)
113 if (as_ctx->pagelist) {
114 req->r_pagelist = as_ctx->pagelist;
115 as_ctx->pagelist = NULL;
117 ceph_fscrypt_as_ctx_to_req(req, as_ctx);
121 * ceph_get_inode - find or create/hash a new inode
122 * @sb: superblock to search and allocate in
123 * @vino: vino to search for
124 * @newino: optional new inode to insert if one isn't found (may be NULL)
126 * Search for or insert a new inode into the hash for the given vino, and
127 * return a reference to it. If new is non-NULL, its reference is consumed.
129 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
130 struct inode *newino)
134 if (ceph_vino_is_reserved(vino))
135 return ERR_PTR(-EREMOTEIO);
138 inode = inode_insert5(newino, (unsigned long)vino.ino,
139 ceph_ino_compare, ceph_set_ino_cb, &vino);
143 inode = iget5_locked(sb, (unsigned long)vino.ino,
144 ceph_ino_compare, ceph_set_ino_cb, &vino);
148 dout("No inode found for %llx.%llx\n", vino.ino, vino.snap);
149 return ERR_PTR(-ENOMEM);
152 dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode),
153 ceph_vinop(inode), inode, !!(inode->i_state & I_NEW));
158 * get/constuct snapdir inode for a given directory
160 struct inode *ceph_get_snapdir(struct inode *parent)
162 struct ceph_vino vino = {
163 .ino = ceph_ino(parent),
164 .snap = CEPH_SNAPDIR,
166 struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
167 struct ceph_inode_info *ci = ceph_inode(inode);
173 if (!S_ISDIR(parent->i_mode)) {
174 pr_warn_once("bad snapdir parent type (mode=0%o)\n",
179 if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
180 pr_warn_once("bad snapdir inode type (mode=0%o)\n",
185 inode->i_mode = parent->i_mode;
186 inode->i_uid = parent->i_uid;
187 inode->i_gid = parent->i_gid;
188 inode->i_mtime = parent->i_mtime;
189 inode_set_ctime_to_ts(inode, inode_get_ctime(parent));
190 inode->i_atime = parent->i_atime;
192 ci->i_btime = ceph_inode(parent)->i_btime;
194 #ifdef CONFIG_FS_ENCRYPTION
195 /* if encrypted, just borrow fscrypt_auth from parent */
196 if (IS_ENCRYPTED(parent)) {
197 struct ceph_inode_info *pci = ceph_inode(parent);
199 ci->fscrypt_auth = kmemdup(pci->fscrypt_auth,
200 pci->fscrypt_auth_len,
202 if (ci->fscrypt_auth) {
203 inode->i_flags |= S_ENCRYPTED;
204 ci->fscrypt_auth_len = pci->fscrypt_auth_len;
206 dout("Failed to alloc snapdir fscrypt_auth\n");
212 if (inode->i_state & I_NEW) {
213 inode->i_op = &ceph_snapdir_iops;
214 inode->i_fop = &ceph_snapdir_fops;
215 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
216 unlock_new_inode(inode);
221 if ((inode->i_state & I_NEW))
222 discard_new_inode(inode);
228 const struct inode_operations ceph_file_iops = {
229 .permission = ceph_permission,
230 .setattr = ceph_setattr,
231 .getattr = ceph_getattr,
232 .listxattr = ceph_listxattr,
233 .get_inode_acl = ceph_get_acl,
234 .set_acl = ceph_set_acl,
239 * We use a 'frag tree' to keep track of the MDS's directory fragments
240 * for a given inode (usually there is just a single fragment). We
241 * need to know when a child frag is delegated to a new MDS, or when
242 * it is flagged as replicated, so we can direct our requests
247 * find/create a frag in the tree
249 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
253 struct rb_node *parent = NULL;
254 struct ceph_inode_frag *frag;
257 p = &ci->i_fragtree.rb_node;
260 frag = rb_entry(parent, struct ceph_inode_frag, node);
261 c = ceph_frag_compare(f, frag->frag);
270 frag = kmalloc(sizeof(*frag), GFP_NOFS);
272 return ERR_PTR(-ENOMEM);
279 rb_link_node(&frag->node, parent, p);
280 rb_insert_color(&frag->node, &ci->i_fragtree);
282 dout("get_or_create_frag added %llx.%llx frag %x\n",
283 ceph_vinop(&ci->netfs.inode), f);
288 * find a specific frag @f
290 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
292 struct rb_node *n = ci->i_fragtree.rb_node;
295 struct ceph_inode_frag *frag =
296 rb_entry(n, struct ceph_inode_frag, node);
297 int c = ceph_frag_compare(f, frag->frag);
309 * Choose frag containing the given value @v. If @pfrag is
310 * specified, copy the frag delegation info to the caller if
313 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
314 struct ceph_inode_frag *pfrag, int *found)
316 u32 t = ceph_frag_make(0, 0);
317 struct ceph_inode_frag *frag;
325 WARN_ON(!ceph_frag_contains_value(t, v));
326 frag = __ceph_find_frag(ci, t);
328 break; /* t is a leaf */
329 if (frag->split_by == 0) {
331 memcpy(pfrag, frag, sizeof(*pfrag));
338 nway = 1 << frag->split_by;
339 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
340 frag->split_by, nway);
341 for (i = 0; i < nway; i++) {
342 n = ceph_frag_make_child(t, frag->split_by, i);
343 if (ceph_frag_contains_value(n, v)) {
350 dout("choose_frag(%x) = %x\n", v, t);
355 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
356 struct ceph_inode_frag *pfrag, int *found)
359 mutex_lock(&ci->i_fragtree_mutex);
360 ret = __ceph_choose_frag(ci, v, pfrag, found);
361 mutex_unlock(&ci->i_fragtree_mutex);
366 * Process dirfrag (delegation) info from the mds. Include leaf
367 * fragment in tree ONLY if ndist > 0. Otherwise, only
368 * branches/splits are included in i_fragtree)
370 static int ceph_fill_dirfrag(struct inode *inode,
371 struct ceph_mds_reply_dirfrag *dirinfo)
373 struct ceph_inode_info *ci = ceph_inode(inode);
374 struct ceph_inode_frag *frag;
375 u32 id = le32_to_cpu(dirinfo->frag);
376 int mds = le32_to_cpu(dirinfo->auth);
377 int ndist = le32_to_cpu(dirinfo->ndist);
382 spin_lock(&ci->i_ceph_lock);
384 diri_auth = ci->i_auth_cap->mds;
385 spin_unlock(&ci->i_ceph_lock);
387 if (mds == -1) /* CDIR_AUTH_PARENT */
390 mutex_lock(&ci->i_fragtree_mutex);
391 if (ndist == 0 && mds == diri_auth) {
392 /* no delegation info needed. */
393 frag = __ceph_find_frag(ci, id);
396 if (frag->split_by == 0) {
397 /* tree leaf, remove */
398 dout("fill_dirfrag removed %llx.%llx frag %x"
399 " (no ref)\n", ceph_vinop(inode), id);
400 rb_erase(&frag->node, &ci->i_fragtree);
403 /* tree branch, keep and clear */
404 dout("fill_dirfrag cleared %llx.%llx frag %x"
405 " referral\n", ceph_vinop(inode), id);
413 /* find/add this frag to store mds delegation info */
414 frag = __get_or_create_frag(ci, id);
416 /* this is not the end of the world; we can continue
417 with bad/inaccurate delegation info */
418 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
419 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
425 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
426 for (i = 0; i < frag->ndist; i++)
427 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
428 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
429 ceph_vinop(inode), frag->frag, frag->ndist);
432 mutex_unlock(&ci->i_fragtree_mutex);
436 static int frag_tree_split_cmp(const void *l, const void *r)
438 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
439 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
440 return ceph_frag_compare(le32_to_cpu(ls->frag),
441 le32_to_cpu(rs->frag));
444 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
447 return f == ceph_frag_make(0, 0);
448 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
450 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
453 static int ceph_fill_fragtree(struct inode *inode,
454 struct ceph_frag_tree_head *fragtree,
455 struct ceph_mds_reply_dirfrag *dirinfo)
457 struct ceph_inode_info *ci = ceph_inode(inode);
458 struct ceph_inode_frag *frag, *prev_frag = NULL;
459 struct rb_node *rb_node;
460 unsigned i, split_by, nsplits;
464 mutex_lock(&ci->i_fragtree_mutex);
465 nsplits = le32_to_cpu(fragtree->nsplits);
466 if (nsplits != ci->i_fragtree_nsplits) {
468 } else if (nsplits) {
469 i = get_random_u32_below(nsplits);
470 id = le32_to_cpu(fragtree->splits[i].frag);
471 if (!__ceph_find_frag(ci, id))
473 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
474 rb_node = rb_first(&ci->i_fragtree);
475 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
476 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
479 if (!update && dirinfo) {
480 id = le32_to_cpu(dirinfo->frag);
481 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
488 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
489 frag_tree_split_cmp, NULL);
492 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
493 rb_node = rb_first(&ci->i_fragtree);
494 for (i = 0; i < nsplits; i++) {
495 id = le32_to_cpu(fragtree->splits[i].frag);
496 split_by = le32_to_cpu(fragtree->splits[i].by);
497 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
498 pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
499 "frag %x split by %d\n", ceph_vinop(inode),
500 i, nsplits, id, split_by);
505 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
506 if (ceph_frag_compare(frag->frag, id) >= 0) {
507 if (frag->frag != id)
510 rb_node = rb_next(rb_node);
513 rb_node = rb_next(rb_node);
514 /* delete stale split/leaf node */
515 if (frag->split_by > 0 ||
516 !is_frag_child(frag->frag, prev_frag)) {
517 rb_erase(&frag->node, &ci->i_fragtree);
518 if (frag->split_by > 0)
519 ci->i_fragtree_nsplits--;
525 frag = __get_or_create_frag(ci, id);
529 if (frag->split_by == 0)
530 ci->i_fragtree_nsplits++;
531 frag->split_by = split_by;
532 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
536 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
537 rb_node = rb_next(rb_node);
538 /* delete stale split/leaf node */
539 if (frag->split_by > 0 ||
540 !is_frag_child(frag->frag, prev_frag)) {
541 rb_erase(&frag->node, &ci->i_fragtree);
542 if (frag->split_by > 0)
543 ci->i_fragtree_nsplits--;
548 mutex_unlock(&ci->i_fragtree_mutex);
553 * initialize a newly allocated inode.
555 struct inode *ceph_alloc_inode(struct super_block *sb)
557 struct ceph_inode_info *ci;
560 ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
564 dout("alloc_inode %p\n", &ci->netfs.inode);
566 /* Set parameters for the netfs library */
567 netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
569 spin_lock_init(&ci->i_ceph_lock);
572 ci->i_inline_version = 0;
573 ci->i_time_warp_seq = 0;
574 ci->i_ceph_flags = 0;
575 atomic64_set(&ci->i_ordered_count, 1);
576 atomic64_set(&ci->i_release_count, 1);
577 atomic64_set(&ci->i_complete_seq[0], 0);
578 atomic64_set(&ci->i_complete_seq[1], 0);
579 ci->i_symlink = NULL;
584 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
585 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
586 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
588 ci->i_fragtree = RB_ROOT;
589 mutex_init(&ci->i_fragtree_mutex);
591 ci->i_xattrs.blob = NULL;
592 ci->i_xattrs.prealloc_blob = NULL;
593 ci->i_xattrs.dirty = false;
594 ci->i_xattrs.index = RB_ROOT;
595 ci->i_xattrs.count = 0;
596 ci->i_xattrs.names_size = 0;
597 ci->i_xattrs.vals_size = 0;
598 ci->i_xattrs.version = 0;
599 ci->i_xattrs.index_version = 0;
601 ci->i_caps = RB_ROOT;
602 ci->i_auth_cap = NULL;
603 ci->i_dirty_caps = 0;
604 ci->i_flushing_caps = 0;
605 INIT_LIST_HEAD(&ci->i_dirty_item);
606 INIT_LIST_HEAD(&ci->i_flushing_item);
607 ci->i_prealloc_cap_flush = NULL;
608 INIT_LIST_HEAD(&ci->i_cap_flush_list);
609 init_waitqueue_head(&ci->i_cap_wq);
610 ci->i_hold_caps_max = 0;
611 INIT_LIST_HEAD(&ci->i_cap_delay_list);
612 INIT_LIST_HEAD(&ci->i_cap_snaps);
613 ci->i_head_snapc = NULL;
616 ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
617 for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
618 ci->i_nr_by_mode[i] = 0;
620 mutex_init(&ci->i_truncate_mutex);
621 ci->i_truncate_seq = 0;
622 ci->i_truncate_size = 0;
623 ci->i_truncate_pending = 0;
624 ci->i_truncate_pagecache_size = 0;
627 ci->i_reported_size = 0;
628 ci->i_wanted_max_size = 0;
629 ci->i_requested_max_size = 0;
633 ci->i_rdcache_ref = 0;
637 ci->i_wrbuffer_ref = 0;
638 ci->i_wrbuffer_ref_head = 0;
639 atomic_set(&ci->i_filelock_ref, 0);
640 atomic_set(&ci->i_shared_gen, 1);
641 ci->i_rdcache_gen = 0;
642 ci->i_rdcache_revoking = 0;
644 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
645 INIT_LIST_HEAD(&ci->i_unsafe_iops);
646 spin_lock_init(&ci->i_unsafe_lock);
648 ci->i_snap_realm = NULL;
649 INIT_LIST_HEAD(&ci->i_snap_realm_item);
650 INIT_LIST_HEAD(&ci->i_snap_flush_item);
652 INIT_WORK(&ci->i_work, ceph_inode_work);
654 memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
655 #ifdef CONFIG_FS_ENCRYPTION
656 ci->fscrypt_auth = NULL;
657 ci->fscrypt_auth_len = 0;
659 return &ci->netfs.inode;
662 void ceph_free_inode(struct inode *inode)
664 struct ceph_inode_info *ci = ceph_inode(inode);
666 kfree(ci->i_symlink);
667 #ifdef CONFIG_FS_ENCRYPTION
668 kfree(ci->fscrypt_auth);
670 fscrypt_free_inode(inode);
671 kmem_cache_free(ceph_inode_cachep, ci);
674 void ceph_evict_inode(struct inode *inode)
676 struct ceph_inode_info *ci = ceph_inode(inode);
677 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
678 struct ceph_inode_frag *frag;
681 dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
683 percpu_counter_dec(&mdsc->metric.total_inodes);
685 truncate_inode_pages_final(&inode->i_data);
686 if (inode->i_state & I_PINNING_FSCACHE_WB)
687 ceph_fscache_unuse_cookie(inode, true);
690 ceph_fscache_unregister_inode_cookie(ci);
691 fscrypt_put_encryption_info(inode);
693 __ceph_remove_caps(ci);
695 if (__ceph_has_quota(ci, QUOTA_GET_ANY))
696 ceph_adjust_quota_realms_count(inode, false);
699 * we may still have a snap_realm reference if there are stray
700 * caps in i_snap_caps.
702 if (ci->i_snap_realm) {
703 if (ceph_snap(inode) == CEPH_NOSNAP) {
704 dout(" dropping residual ref to snap realm %p\n",
706 ceph_change_snap_realm(inode, NULL);
708 ceph_put_snapid_map(mdsc, ci->i_snapid_map);
709 ci->i_snap_realm = NULL;
713 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
714 frag = rb_entry(n, struct ceph_inode_frag, node);
715 rb_erase(n, &ci->i_fragtree);
718 ci->i_fragtree_nsplits = 0;
720 __ceph_destroy_xattrs(ci);
721 if (ci->i_xattrs.blob)
722 ceph_buffer_put(ci->i_xattrs.blob);
723 if (ci->i_xattrs.prealloc_blob)
724 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
726 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
727 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
730 static inline blkcnt_t calc_inode_blocks(u64 size)
732 return (size + (1<<9) - 1) >> 9;
736 * Helpers to fill in size, ctime, mtime, and atime. We have to be
737 * careful because either the client or MDS may have more up to date
738 * info, depending on which capabilities are held, and whether
739 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
740 * and size are monotonically increasing, except when utimes() or
741 * truncate() increments the corresponding _seq values.)
743 int ceph_fill_file_size(struct inode *inode, int issued,
744 u32 truncate_seq, u64 truncate_size, u64 size)
746 struct ceph_inode_info *ci = ceph_inode(inode);
748 loff_t isize = i_size_read(inode);
750 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
751 (truncate_seq == ci->i_truncate_seq && size > isize)) {
752 dout("size %lld -> %llu\n", isize, size);
753 if (size > 0 && S_ISDIR(inode->i_mode)) {
754 pr_err("fill_file_size non-zero size for directory\n");
757 i_size_write(inode, size);
758 inode->i_blocks = calc_inode_blocks(size);
760 * If we're expanding, then we should be able to just update
761 * the existing cookie.
764 ceph_fscache_update(inode);
765 ci->i_reported_size = size;
766 if (truncate_seq != ci->i_truncate_seq) {
767 dout("%s truncate_seq %u -> %u\n", __func__,
768 ci->i_truncate_seq, truncate_seq);
769 ci->i_truncate_seq = truncate_seq;
771 /* the MDS should have revoked these caps */
772 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
775 CEPH_CAP_FILE_LAZYIO));
777 * If we hold relevant caps, or in the case where we're
778 * not the only client referencing this file and we
779 * don't hold those caps, then we need to check whether
780 * the file is either opened or mmaped
782 if ((issued & (CEPH_CAP_FILE_CACHE|
783 CEPH_CAP_FILE_BUFFER)) ||
784 mapping_mapped(inode->i_mapping) ||
785 __ceph_is_file_opened(ci)) {
786 ci->i_truncate_pending++;
793 * It's possible that the new sizes of the two consecutive
794 * size truncations will be in the same fscrypt last block,
795 * and we need to truncate the corresponding page caches
798 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) {
799 dout("%s truncate_size %lld -> %llu, encrypted %d\n", __func__,
800 ci->i_truncate_size, truncate_size, !!IS_ENCRYPTED(inode));
802 ci->i_truncate_size = truncate_size;
804 if (IS_ENCRYPTED(inode)) {
805 dout("%s truncate_pagecache_size %lld -> %llu\n",
806 __func__, ci->i_truncate_pagecache_size, size);
807 ci->i_truncate_pagecache_size = size;
809 ci->i_truncate_pagecache_size = truncate_size;
815 void ceph_fill_file_time(struct inode *inode, int issued,
816 u64 time_warp_seq, struct timespec64 *ctime,
817 struct timespec64 *mtime, struct timespec64 *atime)
819 struct ceph_inode_info *ci = ceph_inode(inode);
820 struct timespec64 ictime = inode_get_ctime(inode);
823 if (issued & (CEPH_CAP_FILE_EXCL|
825 CEPH_CAP_FILE_BUFFER|
827 CEPH_CAP_XATTR_EXCL)) {
828 if (ci->i_version == 0 ||
829 timespec64_compare(ctime, &ictime) > 0) {
830 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
831 ictime.tv_sec, ictime.tv_nsec,
832 ctime->tv_sec, ctime->tv_nsec);
833 inode_set_ctime_to_ts(inode, *ctime);
835 if (ci->i_version == 0 ||
836 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
837 /* the MDS did a utimes() */
838 dout("mtime %lld.%09ld -> %lld.%09ld "
840 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
841 mtime->tv_sec, mtime->tv_nsec,
842 ci->i_time_warp_seq, (int)time_warp_seq);
844 inode->i_mtime = *mtime;
845 inode->i_atime = *atime;
846 ci->i_time_warp_seq = time_warp_seq;
847 } else if (time_warp_seq == ci->i_time_warp_seq) {
848 /* nobody did utimes(); take the max */
849 if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
850 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
851 inode->i_mtime.tv_sec,
852 inode->i_mtime.tv_nsec,
853 mtime->tv_sec, mtime->tv_nsec);
854 inode->i_mtime = *mtime;
856 if (timespec64_compare(atime, &inode->i_atime) > 0) {
857 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
858 inode->i_atime.tv_sec,
859 inode->i_atime.tv_nsec,
860 atime->tv_sec, atime->tv_nsec);
861 inode->i_atime = *atime;
863 } else if (issued & CEPH_CAP_FILE_EXCL) {
864 /* we did a utimes(); ignore mds values */
869 /* we have no write|excl caps; whatever the MDS says is true */
870 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
871 inode_set_ctime_to_ts(inode, *ctime);
872 inode->i_mtime = *mtime;
873 inode->i_atime = *atime;
874 ci->i_time_warp_seq = time_warp_seq;
879 if (warn) /* time_warp_seq shouldn't go backwards */
880 dout("%p mds time_warp_seq %llu < %u\n",
881 inode, time_warp_seq, ci->i_time_warp_seq);
884 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
885 static int decode_encrypted_symlink(const char *encsym, int enclen, u8 **decsym)
890 sym = kmalloc(enclen + 1, GFP_NOFS);
894 declen = ceph_base64_decode(encsym, enclen, sym);
896 pr_err("%s: can't decode symlink (%d). Content: %.*s\n",
897 __func__, declen, enclen, encsym);
901 sym[declen + 1] = '\0';
906 static int decode_encrypted_symlink(const char *encsym, int symlen, u8 **decsym)
913 * Populate an inode based on info from mds. May be called on new or
916 int ceph_fill_inode(struct inode *inode, struct page *locked_page,
917 struct ceph_mds_reply_info_in *iinfo,
918 struct ceph_mds_reply_dirfrag *dirinfo,
919 struct ceph_mds_session *session, int cap_fmode,
920 struct ceph_cap_reservation *caps_reservation)
922 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
923 struct ceph_mds_reply_inode *info = iinfo->in;
924 struct ceph_inode_info *ci = ceph_inode(inode);
925 int issued, new_issued, info_caps;
926 struct timespec64 mtime, atime, ctime;
927 struct ceph_buffer *xattr_blob = NULL;
928 struct ceph_buffer *old_blob = NULL;
929 struct ceph_string *pool_ns = NULL;
930 struct ceph_cap *new_cap = NULL;
933 bool queue_trunc = false;
934 bool new_version = false;
935 bool fill_inline = false;
936 umode_t mode = le32_to_cpu(info->mode);
937 dev_t rdev = le32_to_cpu(info->rdev);
939 lockdep_assert_held(&mdsc->snap_rwsem);
941 dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
942 inode, ceph_vinop(inode), le64_to_cpu(info->version),
945 /* Once I_NEW is cleared, we can't change type or dev numbers */
946 if (inode->i_state & I_NEW) {
947 inode->i_mode = mode;
949 if (inode_wrong_type(inode, mode)) {
950 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
951 ceph_vinop(inode), inode->i_mode, mode);
955 if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
956 pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
957 ceph_vinop(inode), MAJOR(inode->i_rdev),
958 MINOR(inode->i_rdev), MAJOR(rdev),
964 info_caps = le32_to_cpu(info->cap.caps);
966 /* prealloc new cap struct */
967 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
968 new_cap = ceph_get_cap(mdsc, caps_reservation);
974 * prealloc xattr data, if it looks like we'll need it. only
975 * if len > 4 (meaning there are actually xattrs; the first 4
976 * bytes are the xattr count).
978 if (iinfo->xattr_len > 4) {
979 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
981 pr_err("%s ENOMEM xattr blob %d bytes\n", __func__,
985 if (iinfo->pool_ns_len > 0)
986 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
989 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
990 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
992 spin_lock(&ci->i_ceph_lock);
995 * provided version will be odd if inode value is projected,
996 * even if stable. skip the update if we have newer stable
997 * info (ours>=theirs, e.g. due to racing mds replies), unless
998 * we are getting projected (unstable) info (in which case the
999 * version is odd, and we want ours>theirs).
1005 if (ci->i_version == 0 ||
1006 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1007 le64_to_cpu(info->version) > (ci->i_version & ~1)))
1010 /* Update change_attribute */
1011 inode_set_max_iversion_raw(inode, iinfo->change_attr);
1013 __ceph_caps_issued(ci, &issued);
1014 issued |= __ceph_caps_dirty(ci);
1015 new_issued = ~issued & info_caps;
1017 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
1019 #ifdef CONFIG_FS_ENCRYPTION
1020 if (iinfo->fscrypt_auth_len &&
1021 ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) {
1022 kfree(ci->fscrypt_auth);
1023 ci->fscrypt_auth_len = iinfo->fscrypt_auth_len;
1024 ci->fscrypt_auth = iinfo->fscrypt_auth;
1025 iinfo->fscrypt_auth = NULL;
1026 iinfo->fscrypt_auth_len = 0;
1027 inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
1031 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
1032 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
1033 inode->i_mode = mode;
1034 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
1035 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
1036 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
1037 from_kuid(&init_user_ns, inode->i_uid),
1038 from_kgid(&init_user_ns, inode->i_gid));
1039 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
1040 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
1043 /* directories have fl_stripe_unit set to zero */
1044 if (IS_ENCRYPTED(inode))
1045 inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
1046 else if (le32_to_cpu(info->layout.fl_stripe_unit))
1048 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
1050 inode->i_blkbits = CEPH_BLOCK_SHIFT;
1052 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
1053 (issued & CEPH_CAP_LINK_EXCL) == 0)
1054 set_nlink(inode, le32_to_cpu(info->nlink));
1056 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
1057 /* be careful with mtime, atime, size */
1058 ceph_decode_timespec64(&atime, &info->atime);
1059 ceph_decode_timespec64(&mtime, &info->mtime);
1060 ceph_decode_timespec64(&ctime, &info->ctime);
1061 ceph_fill_file_time(inode, issued,
1062 le32_to_cpu(info->time_warp_seq),
1063 &ctime, &mtime, &atime);
1066 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
1067 ci->i_files = le64_to_cpu(info->files);
1068 ci->i_subdirs = le64_to_cpu(info->subdirs);
1072 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
1073 u64 size = le64_to_cpu(info->size);
1074 s64 old_pool = ci->i_layout.pool_id;
1075 struct ceph_string *old_ns;
1077 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
1078 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
1079 lockdep_is_held(&ci->i_ceph_lock));
1080 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
1082 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
1083 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
1087 if (IS_ENCRYPTED(inode) && size &&
1088 iinfo->fscrypt_file_len == sizeof(__le64)) {
1089 u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file);
1091 if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) {
1094 pr_warn("fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n",
1099 queue_trunc = ceph_fill_file_size(inode, issued,
1100 le32_to_cpu(info->truncate_seq),
1101 le64_to_cpu(info->truncate_size),
1103 /* only update max_size on auth cap */
1104 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1105 ci->i_max_size != le64_to_cpu(info->max_size)) {
1106 dout("max_size %lld -> %llu\n", ci->i_max_size,
1107 le64_to_cpu(info->max_size));
1108 ci->i_max_size = le64_to_cpu(info->max_size);
1112 /* layout and rstat are not tracked by capability, update them if
1113 * the inode info is from auth mds */
1114 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
1115 if (S_ISDIR(inode->i_mode)) {
1116 ci->i_dir_layout = iinfo->dir_layout;
1117 ci->i_rbytes = le64_to_cpu(info->rbytes);
1118 ci->i_rfiles = le64_to_cpu(info->rfiles);
1119 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
1120 ci->i_dir_pin = iinfo->dir_pin;
1121 ci->i_rsnaps = iinfo->rsnaps;
1122 ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
1127 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
1128 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
1129 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
1130 if (ci->i_xattrs.blob)
1131 old_blob = ci->i_xattrs.blob;
1132 ci->i_xattrs.blob = xattr_blob;
1134 memcpy(ci->i_xattrs.blob->vec.iov_base,
1135 iinfo->xattr_data, iinfo->xattr_len);
1136 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
1137 ceph_forget_all_cached_acls(inode);
1138 ceph_security_invalidate_secctx(inode);
1142 /* finally update i_version */
1143 if (le64_to_cpu(info->version) > ci->i_version)
1144 ci->i_version = le64_to_cpu(info->version);
1146 inode->i_mapping->a_ops = &ceph_aops;
1148 switch (inode->i_mode & S_IFMT) {
1153 inode->i_blkbits = PAGE_SHIFT;
1154 init_special_inode(inode, inode->i_mode, rdev);
1155 inode->i_op = &ceph_file_iops;
1158 inode->i_op = &ceph_file_iops;
1159 inode->i_fop = &ceph_file_fops;
1162 if (!ci->i_symlink) {
1163 u32 symlen = iinfo->symlink_len;
1166 spin_unlock(&ci->i_ceph_lock);
1168 if (IS_ENCRYPTED(inode)) {
1169 if (symlen != i_size_read(inode))
1170 pr_err("%s %llx.%llx BAD symlink size %lld\n",
1171 __func__, ceph_vinop(inode),
1172 i_size_read(inode));
1174 err = decode_encrypted_symlink(iinfo->symlink,
1175 symlen, (u8 **)&sym);
1177 pr_err("%s decoding encrypted symlink failed: %d\n",
1182 i_size_write(inode, symlen);
1183 inode->i_blocks = calc_inode_blocks(symlen);
1185 if (symlen != i_size_read(inode)) {
1186 pr_err("%s %llx.%llx BAD symlink size %lld\n",
1187 __func__, ceph_vinop(inode),
1188 i_size_read(inode));
1189 i_size_write(inode, symlen);
1190 inode->i_blocks = calc_inode_blocks(symlen);
1194 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
1199 spin_lock(&ci->i_ceph_lock);
1201 ci->i_symlink = sym;
1203 kfree(sym); /* lost a race */
1206 if (IS_ENCRYPTED(inode)) {
1208 * Encrypted symlinks need to be decrypted before we can
1209 * cache their targets in i_link. Don't touch it here.
1211 inode->i_op = &ceph_encrypted_symlink_iops;
1213 inode->i_link = ci->i_symlink;
1214 inode->i_op = &ceph_symlink_iops;
1218 inode->i_op = &ceph_dir_iops;
1219 inode->i_fop = &ceph_dir_fops;
1222 pr_err("%s %llx.%llx BAD mode 0%o\n", __func__,
1223 ceph_vinop(inode), inode->i_mode);
1226 /* were we issued a capability? */
1228 if (ceph_snap(inode) == CEPH_NOSNAP) {
1229 ceph_add_cap(inode, session,
1230 le64_to_cpu(info->cap.cap_id),
1232 le32_to_cpu(info->cap.wanted),
1233 le32_to_cpu(info->cap.seq),
1234 le32_to_cpu(info->cap.mseq),
1235 le64_to_cpu(info->cap.realm),
1236 info->cap.flags, &new_cap);
1238 /* set dir completion flag? */
1239 if (S_ISDIR(inode->i_mode) &&
1240 ci->i_files == 0 && ci->i_subdirs == 0 &&
1241 (info_caps & CEPH_CAP_FILE_SHARED) &&
1242 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1243 !__ceph_dir_is_complete(ci)) {
1244 dout(" marking %p complete (empty)\n", inode);
1245 i_size_write(inode, 0);
1246 __ceph_dir_set_complete(ci,
1247 atomic64_read(&ci->i_release_count),
1248 atomic64_read(&ci->i_ordered_count));
1253 dout(" %p got snap_caps %s\n", inode,
1254 ceph_cap_string(info_caps));
1255 ci->i_snap_caps |= info_caps;
1259 if (iinfo->inline_version > 0 &&
1260 iinfo->inline_version >= ci->i_inline_version) {
1261 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1262 ci->i_inline_version = iinfo->inline_version;
1263 if (ceph_has_inline_data(ci) &&
1264 (locked_page || (info_caps & cache_caps)))
1268 if (cap_fmode >= 0) {
1270 pr_warn("mds issued no caps on %llx.%llx\n",
1272 __ceph_touch_fmode(ci, mdsc, cap_fmode);
1275 spin_unlock(&ci->i_ceph_lock);
1277 ceph_fscache_register_inode_cookie(inode);
1280 ceph_fill_inline_data(inode, locked_page,
1281 iinfo->inline_data, iinfo->inline_len);
1284 wake_up_all(&ci->i_cap_wq);
1286 /* queue truncate if we saw i_size decrease */
1288 ceph_queue_vmtruncate(inode);
1290 /* populate frag tree */
1291 if (S_ISDIR(inode->i_mode))
1292 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1294 /* update delegation info? */
1296 ceph_fill_dirfrag(inode, dirinfo);
1301 ceph_put_cap(mdsc, new_cap);
1302 ceph_buffer_put(old_blob);
1303 ceph_buffer_put(xattr_blob);
1304 ceph_put_string(pool_ns);
1309 * caller should hold session s_mutex and dentry->d_lock.
1311 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1312 struct ceph_mds_reply_lease *lease,
1313 struct ceph_mds_session *session,
1314 unsigned long from_time,
1315 struct ceph_mds_session **old_lease_session)
1317 struct ceph_dentry_info *di = ceph_dentry(dentry);
1318 unsigned mask = le16_to_cpu(lease->mask);
1319 long unsigned duration = le32_to_cpu(lease->duration_ms);
1320 long unsigned ttl = from_time + (duration * HZ) / 1000;
1321 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1323 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1324 dentry, duration, ttl);
1326 /* only track leases on regular dentries */
1327 if (ceph_snap(dir) != CEPH_NOSNAP)
1330 if (mask & CEPH_LEASE_PRIMARY_LINK)
1331 di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1333 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1335 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1336 if (!(mask & CEPH_LEASE_VALID)) {
1337 __ceph_dentry_dir_lease_touch(di);
1341 if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
1342 time_before(ttl, di->time))
1343 return; /* we already have a newer lease. */
1345 if (di->lease_session && di->lease_session != session) {
1346 *old_lease_session = di->lease_session;
1347 di->lease_session = NULL;
1350 if (!di->lease_session)
1351 di->lease_session = ceph_get_mds_session(session);
1352 di->lease_gen = atomic_read(&session->s_cap_gen);
1353 di->lease_seq = le32_to_cpu(lease->seq);
1354 di->lease_renew_after = half_ttl;
1355 di->lease_renew_from = 0;
1358 __ceph_dentry_lease_touch(di);
1361 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1362 struct ceph_mds_reply_lease *lease,
1363 struct ceph_mds_session *session,
1364 unsigned long from_time)
1366 struct ceph_mds_session *old_lease_session = NULL;
1367 spin_lock(&dentry->d_lock);
1368 __update_dentry_lease(dir, dentry, lease, session, from_time,
1369 &old_lease_session);
1370 spin_unlock(&dentry->d_lock);
1371 ceph_put_mds_session(old_lease_session);
1375 * update dentry lease without having parent inode locked
1377 static void update_dentry_lease_careful(struct dentry *dentry,
1378 struct ceph_mds_reply_lease *lease,
1379 struct ceph_mds_session *session,
1380 unsigned long from_time,
1381 char *dname, u32 dname_len,
1382 struct ceph_vino *pdvino,
1383 struct ceph_vino *ptvino)
1387 struct ceph_mds_session *old_lease_session = NULL;
1389 spin_lock(&dentry->d_lock);
1390 /* make sure dentry's name matches target */
1391 if (dentry->d_name.len != dname_len ||
1392 memcmp(dentry->d_name.name, dname, dname_len))
1395 dir = d_inode(dentry->d_parent);
1396 /* make sure parent matches dvino */
1397 if (!ceph_ino_compare(dir, pdvino))
1400 /* make sure dentry's inode matches target. NULL ptvino means that
1401 * we expect a negative dentry */
1403 if (d_really_is_negative(dentry))
1405 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1408 if (d_really_is_positive(dentry))
1412 __update_dentry_lease(dir, dentry, lease, session,
1413 from_time, &old_lease_session);
1415 spin_unlock(&dentry->d_lock);
1416 ceph_put_mds_session(old_lease_session);
1420 * splice a dentry to an inode.
1421 * caller must hold directory i_rwsem for this to be safe.
1423 static int splice_dentry(struct dentry **pdn, struct inode *in)
1425 struct dentry *dn = *pdn;
1426 struct dentry *realdn;
1428 BUG_ON(d_inode(dn));
1430 if (S_ISDIR(in->i_mode)) {
1431 /* If inode is directory, d_splice_alias() below will remove
1432 * 'realdn' from its origin parent. We need to ensure that
1433 * origin parent's readdir cache will not reference 'realdn'
1435 realdn = d_find_any_alias(in);
1437 struct ceph_dentry_info *di = ceph_dentry(realdn);
1438 spin_lock(&realdn->d_lock);
1440 realdn->d_op->d_prune(realdn);
1443 di->lease_shared_gen = 0;
1446 spin_unlock(&realdn->d_lock);
1451 /* dn must be unhashed */
1452 if (!d_unhashed(dn))
1454 realdn = d_splice_alias(in, dn);
1455 if (IS_ERR(realdn)) {
1456 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1457 PTR_ERR(realdn), dn, in, ceph_vinop(in));
1458 return PTR_ERR(realdn);
1462 dout("dn %p (%d) spliced with %p (%d) "
1463 "inode %p ino %llx.%llx\n",
1465 realdn, d_count(realdn),
1466 d_inode(realdn), ceph_vinop(d_inode(realdn)));
1470 BUG_ON(!ceph_dentry(dn));
1471 dout("dn %p attached to %p ino %llx.%llx\n",
1472 dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1478 * Incorporate results into the local cache. This is either just
1479 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1482 * A reply may contain
1483 * a directory inode along with a dentry.
1484 * and/or a target inode
1486 * Called with snap_rwsem (read).
1488 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1490 struct ceph_mds_session *session = req->r_session;
1491 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1492 struct inode *in = NULL;
1493 struct ceph_vino tvino, dvino;
1494 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1497 dout("fill_trace %p is_dentry %d is_target %d\n", req,
1498 rinfo->head->is_dentry, rinfo->head->is_target);
1500 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1501 dout("fill_trace reply is empty!\n");
1502 if (rinfo->head->result == 0 && req->r_parent)
1503 ceph_invalidate_dir_request(req);
1507 if (rinfo->head->is_dentry) {
1508 struct inode *dir = req->r_parent;
1511 err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1512 rinfo->dirfrag, session, -1,
1513 &req->r_caps_reservation);
1520 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1521 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1522 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1523 bool is_nokey = false;
1525 struct dentry *dn, *parent;
1526 struct fscrypt_str oname = FSTR_INIT(NULL, 0);
1527 struct ceph_fname fname = { .dir = dir,
1528 .name = rinfo->dname,
1529 .ctext = rinfo->altname,
1530 .name_len = rinfo->dname_len,
1531 .ctext_len = rinfo->altname_len };
1533 BUG_ON(!rinfo->head->is_target);
1534 BUG_ON(req->r_dentry);
1536 parent = d_find_any_alias(dir);
1539 err = ceph_fname_alloc_buffer(dir, &oname);
1545 err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey);
1548 ceph_fname_free_buffer(dir, &oname);
1551 dname.name = oname.name;
1552 dname.len = oname.len;
1553 dname.hash = full_name_hash(parent, dname.name, dname.len);
1554 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1555 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1557 dn = d_lookup(parent, &dname);
1558 dout("d_lookup on parent=%p name=%.*s got %p\n",
1559 parent, dname.len, dname.name, dn);
1562 dn = d_alloc(parent, &dname);
1563 dout("d_alloc %p '%.*s' = %p\n", parent,
1564 dname.len, dname.name, dn);
1567 ceph_fname_free_buffer(dir, &oname);
1572 spin_lock(&dn->d_lock);
1573 dn->d_flags |= DCACHE_NOKEY_NAME;
1574 spin_unlock(&dn->d_lock);
1577 } else if (d_really_is_positive(dn) &&
1578 (ceph_ino(d_inode(dn)) != tvino.ino ||
1579 ceph_snap(d_inode(dn)) != tvino.snap)) {
1580 dout(" dn %p points to wrong inode %p\n",
1582 ceph_dir_clear_ordered(dir);
1587 ceph_fname_free_buffer(dir, &oname);
1594 if (rinfo->head->is_target) {
1595 /* Should be filled in by handle_reply */
1596 BUG_ON(!req->r_target_inode);
1598 in = req->r_target_inode;
1599 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1601 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1602 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1603 rinfo->head->result == 0) ? req->r_fmode : -1,
1604 &req->r_caps_reservation);
1606 pr_err("ceph_fill_inode badness %p %llx.%llx\n",
1607 in, ceph_vinop(in));
1608 req->r_target_inode = NULL;
1609 if (in->i_state & I_NEW)
1610 discard_new_inode(in);
1615 if (in->i_state & I_NEW)
1616 unlock_new_inode(in);
1620 * ignore null lease/binding on snapdir ENOENT, or else we
1621 * will have trouble splicing in the virtual snapdir later
1623 if (rinfo->head->is_dentry &&
1624 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1625 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1626 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1627 fsc->mount_options->snapdir_name,
1628 req->r_dentry->d_name.len))) {
1630 * lookup link rename : null -> possibly existing inode
1631 * mknod symlink mkdir : null -> new inode
1632 * unlink : linked -> null
1634 struct inode *dir = req->r_parent;
1635 struct dentry *dn = req->r_dentry;
1636 bool have_dir_cap, have_lease;
1640 BUG_ON(d_inode(dn->d_parent) != dir);
1642 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1643 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1645 BUG_ON(ceph_ino(dir) != dvino.ino);
1646 BUG_ON(ceph_snap(dir) != dvino.snap);
1648 /* do we have a lease on the whole dir? */
1650 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1651 CEPH_CAP_FILE_SHARED);
1653 /* do we have a dn lease? */
1654 have_lease = have_dir_cap ||
1655 le32_to_cpu(rinfo->dlease->duration_ms);
1657 dout("fill_trace no dentry lease or dir cap\n");
1660 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1661 struct inode *olddir = req->r_old_dentry_dir;
1664 dout(" src %p '%pd' dst %p '%pd'\n",
1668 dout("fill_trace doing d_move %p -> %p\n",
1669 req->r_old_dentry, dn);
1671 /* d_move screws up sibling dentries' offsets */
1672 ceph_dir_clear_ordered(dir);
1673 ceph_dir_clear_ordered(olddir);
1675 d_move(req->r_old_dentry, dn);
1676 dout(" src %p '%pd' dst %p '%pd'\n",
1681 /* ensure target dentry is invalidated, despite
1682 rehashing bug in vfs_rename_dir */
1683 ceph_invalidate_dentry_lease(dn);
1685 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1686 ceph_dentry(req->r_old_dentry)->offset);
1688 /* swap r_dentry and r_old_dentry in case that
1689 * splice_dentry() gets called later. This is safe
1690 * because no other place will use them */
1691 req->r_dentry = req->r_old_dentry;
1692 req->r_old_dentry = dn;
1697 if (!rinfo->head->is_target) {
1698 dout("fill_trace null dentry\n");
1699 if (d_really_is_positive(dn)) {
1700 dout("d_delete %p\n", dn);
1701 ceph_dir_clear_ordered(dir);
1703 } else if (have_lease) {
1708 if (!d_unhashed(dn) && have_lease)
1709 update_dentry_lease(dir, dn,
1710 rinfo->dlease, session,
1711 req->r_request_started);
1715 /* attach proper inode */
1716 if (d_really_is_negative(dn)) {
1717 ceph_dir_clear_ordered(dir);
1719 err = splice_dentry(&req->r_dentry, in);
1722 dn = req->r_dentry; /* may have spliced */
1723 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1724 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1725 dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1732 update_dentry_lease(dir, dn,
1733 rinfo->dlease, session,
1734 req->r_request_started);
1736 dout(" final dn %p\n", dn);
1737 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1738 req->r_op == CEPH_MDS_OP_MKSNAP) &&
1739 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1740 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1741 struct inode *dir = req->r_parent;
1743 /* fill out a snapdir LOOKUPSNAP dentry */
1745 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1746 BUG_ON(!req->r_dentry);
1747 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1748 ceph_dir_clear_ordered(dir);
1750 err = splice_dentry(&req->r_dentry, in);
1753 } else if (rinfo->head->is_dentry && req->r_dentry) {
1754 /* parent inode is not locked, be carefull */
1755 struct ceph_vino *ptvino = NULL;
1756 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1757 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1758 if (rinfo->head->is_target) {
1759 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1760 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1763 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1764 session, req->r_request_started,
1765 rinfo->dname, rinfo->dname_len,
1769 dout("fill_trace done err=%d\n", err);
1774 * Prepopulate our cache with readdir results, leases, etc.
1776 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1777 struct ceph_mds_session *session)
1779 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1782 for (i = 0; i < rinfo->dir_nr; i++) {
1783 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1784 struct ceph_vino vino;
1788 vino.ino = le64_to_cpu(rde->inode.in->ino);
1789 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1791 in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL);
1794 dout("new_inode badness got %d\n", err);
1797 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1798 -1, &req->r_caps_reservation);
1800 pr_err("ceph_fill_inode badness on %p got %d\n",
1803 if (in->i_state & I_NEW) {
1805 discard_new_inode(in);
1807 } else if (in->i_state & I_NEW) {
1808 unlock_new_inode(in);
1817 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1821 put_page(ctl->page);
1826 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1827 struct ceph_readdir_cache_control *ctl,
1828 struct ceph_mds_request *req)
1830 struct ceph_inode_info *ci = ceph_inode(dir);
1831 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1832 unsigned idx = ctl->index % nsize;
1833 pgoff_t pgoff = ctl->index / nsize;
1835 if (!ctl->page || pgoff != page_index(ctl->page)) {
1836 ceph_readdir_cache_release(ctl);
1838 ctl->page = grab_cache_page(&dir->i_data, pgoff);
1840 ctl->page = find_lock_page(&dir->i_data, pgoff);
1843 return idx == 0 ? -ENOMEM : 0;
1845 /* reading/filling the cache are serialized by
1846 * i_rwsem, no need to use page lock */
1847 unlock_page(ctl->page);
1848 ctl->dentries = kmap(ctl->page);
1850 memset(ctl->dentries, 0, PAGE_SIZE);
1853 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1854 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1855 dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1856 ctl->dentries[idx] = dn;
1859 dout("disable readdir cache\n");
1865 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1866 struct ceph_mds_session *session)
1868 struct dentry *parent = req->r_dentry;
1869 struct inode *inode = d_inode(parent);
1870 struct ceph_inode_info *ci = ceph_inode(inode);
1871 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1875 int err = 0, skipped = 0, ret, i;
1876 u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1879 struct ceph_readdir_cache_control cache_ctl = {};
1881 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1882 return readdir_prepopulate_inodes_only(req, session);
1884 if (rinfo->hash_order) {
1886 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1888 strlen(req->r_path2));
1889 last_hash = ceph_frag_value(last_hash);
1890 } else if (rinfo->offset_hash) {
1891 /* mds understands offset_hash */
1892 WARN_ON_ONCE(req->r_readdir_offset != 2);
1893 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
1897 if (rinfo->dir_dir &&
1898 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1899 dout("readdir_prepopulate got new frag %x -> %x\n",
1900 frag, le32_to_cpu(rinfo->dir_dir->frag));
1901 frag = le32_to_cpu(rinfo->dir_dir->frag);
1902 if (!rinfo->hash_order)
1903 req->r_readdir_offset = 2;
1906 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1907 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1908 rinfo->dir_nr, parent);
1910 dout("readdir_prepopulate %d items under dn %p\n",
1911 rinfo->dir_nr, parent);
1913 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1915 if (ceph_frag_is_leftmost(frag) &&
1916 req->r_readdir_offset == 2 &&
1917 !(rinfo->hash_order && last_hash)) {
1918 /* note dir version at start of readdir so we can
1919 * tell if any dentries get dropped */
1920 req->r_dir_release_cnt =
1921 atomic64_read(&ci->i_release_count);
1922 req->r_dir_ordered_cnt =
1923 atomic64_read(&ci->i_ordered_count);
1924 req->r_readdir_cache_idx = 0;
1928 cache_ctl.index = req->r_readdir_cache_idx;
1929 fpos_offset = req->r_readdir_offset;
1931 /* FIXME: release caps/leases if error occurs */
1932 for (i = 0; i < rinfo->dir_nr; i++) {
1933 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1934 struct ceph_vino tvino;
1936 dname.name = rde->name;
1937 dname.len = rde->name_len;
1938 dname.hash = full_name_hash(parent, dname.name, dname.len);
1940 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1941 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1943 if (rinfo->hash_order) {
1944 u32 hash = ceph_frag_value(rde->raw_hash);
1945 if (hash != last_hash)
1948 rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1950 rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1954 dn = d_lookup(parent, &dname);
1955 dout("d_lookup on parent=%p name=%.*s got %p\n",
1956 parent, dname.len, dname.name, dn);
1959 dn = d_alloc(parent, &dname);
1960 dout("d_alloc %p '%.*s' = %p\n", parent,
1961 dname.len, dname.name, dn);
1963 dout("d_alloc badness\n");
1967 if (rde->is_nokey) {
1968 spin_lock(&dn->d_lock);
1969 dn->d_flags |= DCACHE_NOKEY_NAME;
1970 spin_unlock(&dn->d_lock);
1972 } else if (d_really_is_positive(dn) &&
1973 (ceph_ino(d_inode(dn)) != tvino.ino ||
1974 ceph_snap(d_inode(dn)) != tvino.snap)) {
1975 struct ceph_dentry_info *di = ceph_dentry(dn);
1976 dout(" dn %p points to wrong inode %p\n",
1979 spin_lock(&dn->d_lock);
1980 if (di->offset > 0 &&
1981 di->lease_shared_gen ==
1982 atomic_read(&ci->i_shared_gen)) {
1983 __ceph_dir_clear_ordered(ci);
1986 spin_unlock(&dn->d_lock);
1994 if (d_really_is_positive(dn)) {
1997 in = ceph_get_inode(parent->d_sb, tvino, NULL);
1999 dout("new_inode badness\n");
2007 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
2008 -1, &req->r_caps_reservation);
2010 pr_err("ceph_fill_inode badness on %p\n", in);
2011 if (d_really_is_negative(dn)) {
2012 if (in->i_state & I_NEW) {
2014 discard_new_inode(in);
2022 if (in->i_state & I_NEW)
2023 unlock_new_inode(in);
2025 if (d_really_is_negative(dn)) {
2026 if (ceph_security_xattr_deadlock(in)) {
2027 dout(" skip splicing dn %p to inode %p"
2028 " (security xattr deadlock)\n", dn, in);
2034 err = splice_dentry(&dn, in);
2039 ceph_dentry(dn)->offset = rde->offset;
2041 update_dentry_lease(d_inode(parent), dn,
2042 rde->lease, req->r_session,
2043 req->r_request_started);
2045 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
2046 ret = fill_readdir_cache(d_inode(parent), dn,
2055 if (err == 0 && skipped == 0) {
2056 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
2057 req->r_readdir_cache_idx = cache_ctl.index;
2059 ceph_readdir_cache_release(&cache_ctl);
2060 dout("readdir_prepopulate done\n");
2064 bool ceph_inode_set_size(struct inode *inode, loff_t size)
2066 struct ceph_inode_info *ci = ceph_inode(inode);
2069 spin_lock(&ci->i_ceph_lock);
2070 dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
2071 i_size_write(inode, size);
2072 ceph_fscache_update(inode);
2073 inode->i_blocks = calc_inode_blocks(size);
2075 ret = __ceph_should_report_size(ci);
2077 spin_unlock(&ci->i_ceph_lock);
2082 void ceph_queue_inode_work(struct inode *inode, int work_bit)
2084 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2085 struct ceph_inode_info *ci = ceph_inode(inode);
2086 set_bit(work_bit, &ci->i_work_mask);
2089 if (queue_work(fsc->inode_wq, &ci->i_work)) {
2090 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask);
2092 dout("queue_inode_work %p already queued, mask=%lx\n",
2093 inode, ci->i_work_mask);
2098 static void ceph_do_invalidate_pages(struct inode *inode)
2100 struct ceph_inode_info *ci = ceph_inode(inode);
2104 ceph_fscache_invalidate(inode, false);
2106 mutex_lock(&ci->i_truncate_mutex);
2108 if (ceph_inode_is_shutdown(inode)) {
2109 pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n",
2110 __func__, ceph_vinop(inode));
2111 mapping_set_error(inode->i_mapping, -EIO);
2112 truncate_pagecache(inode, 0);
2113 mutex_unlock(&ci->i_truncate_mutex);
2117 spin_lock(&ci->i_ceph_lock);
2118 dout("invalidate_pages %p gen %d revoking %d\n", inode,
2119 ci->i_rdcache_gen, ci->i_rdcache_revoking);
2120 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2121 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2123 spin_unlock(&ci->i_ceph_lock);
2124 mutex_unlock(&ci->i_truncate_mutex);
2127 orig_gen = ci->i_rdcache_gen;
2128 spin_unlock(&ci->i_ceph_lock);
2130 if (invalidate_inode_pages2(inode->i_mapping) < 0) {
2131 pr_err("invalidate_inode_pages2 %llx.%llx failed\n",
2135 spin_lock(&ci->i_ceph_lock);
2136 if (orig_gen == ci->i_rdcache_gen &&
2137 orig_gen == ci->i_rdcache_revoking) {
2138 dout("invalidate_pages %p gen %d successful\n", inode,
2140 ci->i_rdcache_revoking--;
2143 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
2144 inode, orig_gen, ci->i_rdcache_gen,
2145 ci->i_rdcache_revoking);
2146 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2149 spin_unlock(&ci->i_ceph_lock);
2150 mutex_unlock(&ci->i_truncate_mutex);
2153 ceph_check_caps(ci, 0);
2157 * Make sure any pending truncation is applied before doing anything
2158 * that may depend on it.
2160 void __ceph_do_pending_vmtruncate(struct inode *inode)
2162 struct ceph_inode_info *ci = ceph_inode(inode);
2164 int wrbuffer_refs, finish = 0;
2166 mutex_lock(&ci->i_truncate_mutex);
2168 spin_lock(&ci->i_ceph_lock);
2169 if (ci->i_truncate_pending == 0) {
2170 dout("%s %p none pending\n", __func__, inode);
2171 spin_unlock(&ci->i_ceph_lock);
2172 mutex_unlock(&ci->i_truncate_mutex);
2177 * make sure any dirty snapped pages are flushed before we
2178 * possibly truncate them.. so write AND block!
2180 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
2181 spin_unlock(&ci->i_ceph_lock);
2182 dout("%s %p flushing snaps first\n", __func__, inode);
2183 filemap_write_and_wait_range(&inode->i_data, 0,
2184 inode->i_sb->s_maxbytes);
2188 /* there should be no reader or writer */
2189 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
2191 to = ci->i_truncate_pagecache_size;
2192 wrbuffer_refs = ci->i_wrbuffer_ref;
2193 dout("%s %p (%d) to %lld\n", __func__, inode,
2194 ci->i_truncate_pending, to);
2195 spin_unlock(&ci->i_ceph_lock);
2197 ceph_fscache_resize(inode, to);
2198 truncate_pagecache(inode, to);
2200 spin_lock(&ci->i_ceph_lock);
2201 if (to == ci->i_truncate_pagecache_size) {
2202 ci->i_truncate_pending = 0;
2205 spin_unlock(&ci->i_ceph_lock);
2209 mutex_unlock(&ci->i_truncate_mutex);
2211 if (wrbuffer_refs == 0)
2212 ceph_check_caps(ci, 0);
2214 wake_up_all(&ci->i_cap_wq);
2217 static void ceph_inode_work(struct work_struct *work)
2219 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
2221 struct inode *inode = &ci->netfs.inode;
2223 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
2224 dout("writeback %p\n", inode);
2225 filemap_fdatawrite(&inode->i_data);
2227 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
2228 ceph_do_invalidate_pages(inode);
2230 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
2231 __ceph_do_pending_vmtruncate(inode);
2233 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
2234 ceph_check_caps(ci, 0);
2236 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
2237 ceph_flush_snaps(ci, NULL);
2242 static const char *ceph_encrypted_get_link(struct dentry *dentry,
2243 struct inode *inode,
2244 struct delayed_call *done)
2246 struct ceph_inode_info *ci = ceph_inode(inode);
2249 return ERR_PTR(-ECHILD);
2251 return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
2255 static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap,
2256 const struct path *path,
2257 struct kstat *stat, u32 request_mask,
2258 unsigned int query_flags)
2262 ret = ceph_getattr(idmap, path, stat, request_mask, query_flags);
2265 return fscrypt_symlink_getattr(path, stat);
2271 static const struct inode_operations ceph_symlink_iops = {
2272 .get_link = simple_get_link,
2273 .setattr = ceph_setattr,
2274 .getattr = ceph_getattr,
2275 .listxattr = ceph_listxattr,
2278 static const struct inode_operations ceph_encrypted_symlink_iops = {
2279 .get_link = ceph_encrypted_get_link,
2280 .setattr = ceph_setattr,
2281 .getattr = ceph_encrypted_symlink_getattr,
2282 .listxattr = ceph_listxattr,
2286 * Transfer the encrypted last block to the MDS and the MDS
2287 * will help update it when truncating a smaller size.
2289 * We don't support a PAGE_SIZE that is smaller than the
2290 * CEPH_FSCRYPT_BLOCK_SIZE.
2292 static int fill_fscrypt_truncate(struct inode *inode,
2293 struct ceph_mds_request *req,
2296 struct ceph_inode_info *ci = ceph_inode(inode);
2297 int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE;
2298 loff_t pos, orig_pos = round_down(attr->ia_size,
2299 CEPH_FSCRYPT_BLOCK_SIZE);
2300 u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT;
2301 struct ceph_pagelist *pagelist = NULL;
2302 struct kvec iov = {0};
2303 struct iov_iter iter;
2304 struct page *page = NULL;
2305 struct ceph_fscrypt_truncate_size_header header;
2307 int len = CEPH_FSCRYPT_BLOCK_SIZE;
2308 loff_t i_size = i_size_read(inode);
2309 int got, ret, issued;
2312 ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got);
2316 issued = __ceph_caps_issued(ci, NULL);
2318 dout("%s size %lld -> %lld got cap refs on %s, issued %s\n", __func__,
2319 i_size, attr->ia_size, ceph_cap_string(got),
2320 ceph_cap_string(issued));
2322 /* Try to writeback the dirty pagecaches */
2323 if (issued & (CEPH_CAP_FILE_BUFFER)) {
2324 loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SHIFT - 1;
2326 ret = filemap_write_and_wait_range(inode->i_mapping,
2332 page = __page_cache_alloc(GFP_KERNEL);
2338 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
2344 iov.iov_base = kmap_local_page(page);
2346 iov_iter_kvec(&iter, READ, &iov, 1, len);
2349 ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver);
2353 /* Insert the header first */
2356 header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode));
2359 * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE,
2360 * because in MDS it may need this to do the truncate.
2362 header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE);
2365 * If we hit a hole here, we should just skip filling
2366 * the fscrypt for the request, because once the fscrypt
2367 * is enabled, the file will be split into many blocks
2368 * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there
2369 * has a hole, the hole size should be multiple of block
2372 * If the Rados object doesn't exist, it will be set to 0.
2375 dout("%s hit hole, ppos %lld < size %lld\n", __func__,
2378 header.data_len = cpu_to_le32(8 + 8 + 4);
2379 header.file_offset = 0;
2382 header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE);
2383 header.file_offset = cpu_to_le64(orig_pos);
2385 dout("%s encrypt block boff/bsize %d/%lu\n", __func__,
2386 boff, CEPH_FSCRYPT_BLOCK_SIZE);
2388 /* truncate and zero out the extra contents for the last block */
2389 memset(iov.iov_base + boff, 0, PAGE_SIZE - boff);
2391 /* encrypt the last block */
2392 ret = ceph_fscrypt_encrypt_block_inplace(inode, page,
2393 CEPH_FSCRYPT_BLOCK_SIZE,
2400 /* Insert the header */
2401 ret = ceph_pagelist_append(pagelist, &header, sizeof(header));
2405 if (header.block_size) {
2406 /* Append the last block contents to pagelist */
2407 ret = ceph_pagelist_append(pagelist, iov.iov_base,
2408 CEPH_FSCRYPT_BLOCK_SIZE);
2412 req->r_pagelist = pagelist;
2414 dout("%s %p size dropping cap refs on %s\n", __func__,
2415 inode, ceph_cap_string(got));
2416 ceph_put_cap_refs(ci, got);
2418 kunmap_local(iov.iov_base);
2420 __free_pages(page, 0);
2421 if (ret && pagelist)
2422 ceph_pagelist_release(pagelist);
2426 int __ceph_setattr(struct inode *inode, struct iattr *attr,
2427 struct ceph_iattr *cia)
2429 struct ceph_inode_info *ci = ceph_inode(inode);
2430 unsigned int ia_valid = attr->ia_valid;
2431 struct ceph_mds_request *req;
2432 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2433 struct ceph_cap_flush *prealloc_cf;
2434 loff_t isize = i_size_read(inode);
2436 int release = 0, dirtied = 0;
2439 int inode_dirty_flags = 0;
2440 bool lock_snap_rwsem = false;
2442 int truncate_retry = 20; /* The RMW will take around 50ms */
2445 prealloc_cf = ceph_alloc_cap_flush();
2449 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2452 ceph_free_cap_flush(prealloc_cf);
2453 return PTR_ERR(req);
2456 fill_fscrypt = false;
2457 spin_lock(&ci->i_ceph_lock);
2458 issued = __ceph_caps_issued(ci, NULL);
2460 if (!ci->i_head_snapc &&
2461 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2462 lock_snap_rwsem = true;
2463 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2464 spin_unlock(&ci->i_ceph_lock);
2465 down_read(&mdsc->snap_rwsem);
2466 spin_lock(&ci->i_ceph_lock);
2467 issued = __ceph_caps_issued(ci, NULL);
2471 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2472 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
2473 if (cia && cia->fscrypt_auth) {
2474 u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth);
2476 if (len > sizeof(*cia->fscrypt_auth)) {
2478 spin_unlock(&ci->i_ceph_lock);
2482 dout("setattr %llx:%llx fscrypt_auth len %u to %u)\n",
2483 ceph_vinop(inode), ci->fscrypt_auth_len, len);
2485 /* It should never be re-set once set */
2486 WARN_ON_ONCE(ci->fscrypt_auth);
2488 if (issued & CEPH_CAP_AUTH_EXCL) {
2489 dirtied |= CEPH_CAP_AUTH_EXCL;
2490 kfree(ci->fscrypt_auth);
2491 ci->fscrypt_auth = (u8 *)cia->fscrypt_auth;
2492 ci->fscrypt_auth_len = len;
2493 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2494 ci->fscrypt_auth_len != len ||
2495 memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) {
2496 req->r_fscrypt_auth = cia->fscrypt_auth;
2497 mask |= CEPH_SETATTR_FSCRYPT_AUTH;
2498 release |= CEPH_CAP_AUTH_SHARED;
2500 cia->fscrypt_auth = NULL;
2503 if (cia && cia->fscrypt_auth) {
2505 spin_unlock(&ci->i_ceph_lock);
2508 #endif /* CONFIG_FS_ENCRYPTION */
2510 if (ia_valid & ATTR_UID) {
2511 dout("setattr %p uid %d -> %d\n", inode,
2512 from_kuid(&init_user_ns, inode->i_uid),
2513 from_kuid(&init_user_ns, attr->ia_uid));
2514 if (issued & CEPH_CAP_AUTH_EXCL) {
2515 inode->i_uid = attr->ia_uid;
2516 dirtied |= CEPH_CAP_AUTH_EXCL;
2517 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2518 !uid_eq(attr->ia_uid, inode->i_uid)) {
2519 req->r_args.setattr.uid = cpu_to_le32(
2520 from_kuid(&init_user_ns, attr->ia_uid));
2521 mask |= CEPH_SETATTR_UID;
2522 release |= CEPH_CAP_AUTH_SHARED;
2525 if (ia_valid & ATTR_GID) {
2526 dout("setattr %p gid %d -> %d\n", inode,
2527 from_kgid(&init_user_ns, inode->i_gid),
2528 from_kgid(&init_user_ns, attr->ia_gid));
2529 if (issued & CEPH_CAP_AUTH_EXCL) {
2530 inode->i_gid = attr->ia_gid;
2531 dirtied |= CEPH_CAP_AUTH_EXCL;
2532 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2533 !gid_eq(attr->ia_gid, inode->i_gid)) {
2534 req->r_args.setattr.gid = cpu_to_le32(
2535 from_kgid(&init_user_ns, attr->ia_gid));
2536 mask |= CEPH_SETATTR_GID;
2537 release |= CEPH_CAP_AUTH_SHARED;
2540 if (ia_valid & ATTR_MODE) {
2541 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2543 if (issued & CEPH_CAP_AUTH_EXCL) {
2544 inode->i_mode = attr->ia_mode;
2545 dirtied |= CEPH_CAP_AUTH_EXCL;
2546 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2547 attr->ia_mode != inode->i_mode) {
2548 inode->i_mode = attr->ia_mode;
2549 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2550 mask |= CEPH_SETATTR_MODE;
2551 release |= CEPH_CAP_AUTH_SHARED;
2555 if (ia_valid & ATTR_ATIME) {
2556 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2557 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2558 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2559 if (issued & CEPH_CAP_FILE_EXCL) {
2560 ci->i_time_warp_seq++;
2561 inode->i_atime = attr->ia_atime;
2562 dirtied |= CEPH_CAP_FILE_EXCL;
2563 } else if ((issued & CEPH_CAP_FILE_WR) &&
2564 timespec64_compare(&inode->i_atime,
2565 &attr->ia_atime) < 0) {
2566 inode->i_atime = attr->ia_atime;
2567 dirtied |= CEPH_CAP_FILE_WR;
2568 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2569 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2570 ceph_encode_timespec64(&req->r_args.setattr.atime,
2572 mask |= CEPH_SETATTR_ATIME;
2573 release |= CEPH_CAP_FILE_SHARED |
2574 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2577 if (ia_valid & ATTR_SIZE) {
2578 dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size);
2580 * Only when the new size is smaller and not aligned to
2581 * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed.
2583 if (IS_ENCRYPTED(inode) && attr->ia_size < isize &&
2584 (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) {
2585 mask |= CEPH_SETATTR_SIZE;
2586 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2587 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2588 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2589 mask |= CEPH_SETATTR_FSCRYPT_FILE;
2590 req->r_args.setattr.size =
2591 cpu_to_le64(round_up(attr->ia_size,
2592 CEPH_FSCRYPT_BLOCK_SIZE));
2593 req->r_args.setattr.old_size =
2594 cpu_to_le64(round_up(isize,
2595 CEPH_FSCRYPT_BLOCK_SIZE));
2596 req->r_fscrypt_file = attr->ia_size;
2597 fill_fscrypt = true;
2598 } else if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
2599 if (attr->ia_size > isize) {
2600 i_size_write(inode, attr->ia_size);
2601 inode->i_blocks = calc_inode_blocks(attr->ia_size);
2602 ci->i_reported_size = attr->ia_size;
2603 dirtied |= CEPH_CAP_FILE_EXCL;
2604 ia_valid |= ATTR_MTIME;
2606 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2607 attr->ia_size != isize) {
2608 mask |= CEPH_SETATTR_SIZE;
2609 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2610 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2611 if (IS_ENCRYPTED(inode) && attr->ia_size) {
2612 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2613 mask |= CEPH_SETATTR_FSCRYPT_FILE;
2614 req->r_args.setattr.size =
2615 cpu_to_le64(round_up(attr->ia_size,
2616 CEPH_FSCRYPT_BLOCK_SIZE));
2617 req->r_args.setattr.old_size =
2618 cpu_to_le64(round_up(isize,
2619 CEPH_FSCRYPT_BLOCK_SIZE));
2620 req->r_fscrypt_file = attr->ia_size;
2622 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2623 req->r_args.setattr.old_size = cpu_to_le64(isize);
2624 req->r_fscrypt_file = 0;
2628 if (ia_valid & ATTR_MTIME) {
2629 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2630 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2631 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2632 if (issued & CEPH_CAP_FILE_EXCL) {
2633 ci->i_time_warp_seq++;
2634 inode->i_mtime = attr->ia_mtime;
2635 dirtied |= CEPH_CAP_FILE_EXCL;
2636 } else if ((issued & CEPH_CAP_FILE_WR) &&
2637 timespec64_compare(&inode->i_mtime,
2638 &attr->ia_mtime) < 0) {
2639 inode->i_mtime = attr->ia_mtime;
2640 dirtied |= CEPH_CAP_FILE_WR;
2641 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2642 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2643 ceph_encode_timespec64(&req->r_args.setattr.mtime,
2645 mask |= CEPH_SETATTR_MTIME;
2646 release |= CEPH_CAP_FILE_SHARED |
2647 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2651 /* these do nothing */
2652 if (ia_valid & ATTR_CTIME) {
2653 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2654 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2655 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2656 inode_get_ctime(inode).tv_sec,
2657 inode_get_ctime(inode).tv_nsec,
2658 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2659 only ? "ctime only" : "ignored");
2662 * if kernel wants to dirty ctime but nothing else,
2663 * we need to choose a cap to dirty under, or do
2664 * a almost-no-op setattr
2666 if (issued & CEPH_CAP_AUTH_EXCL)
2667 dirtied |= CEPH_CAP_AUTH_EXCL;
2668 else if (issued & CEPH_CAP_FILE_EXCL)
2669 dirtied |= CEPH_CAP_FILE_EXCL;
2670 else if (issued & CEPH_CAP_XATTR_EXCL)
2671 dirtied |= CEPH_CAP_XATTR_EXCL;
2673 mask |= CEPH_SETATTR_CTIME;
2676 if (ia_valid & ATTR_FILE)
2677 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2680 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2682 inode_set_ctime_to_ts(inode, attr->ia_ctime);
2683 inode_inc_iversion_raw(inode);
2687 spin_unlock(&ci->i_ceph_lock);
2688 if (lock_snap_rwsem) {
2689 up_read(&mdsc->snap_rwsem);
2690 lock_snap_rwsem = false;
2693 if (inode_dirty_flags)
2694 __mark_inode_dirty(inode, inode_dirty_flags);
2697 req->r_inode = inode;
2699 req->r_inode_drop = release;
2700 req->r_args.setattr.mask = cpu_to_le32(mask);
2701 req->r_num_caps = 1;
2702 req->r_stamp = attr->ia_ctime;
2704 err = fill_fscrypt_truncate(inode, req, attr);
2710 * The truncate request will return -EAGAIN when the
2711 * last block has been updated just before the MDS
2712 * successfully gets the xlock for the FILE lock. To
2713 * avoid corrupting the file contents we need to retry
2716 err = ceph_mdsc_do_request(mdsc, NULL, req);
2717 if (err == -EAGAIN && truncate_retry--) {
2718 dout("setattr %p result=%d (%s locally, %d remote), retry it!\n",
2719 inode, err, ceph_cap_string(dirtied), mask);
2720 ceph_mdsc_put_request(req);
2721 ceph_free_cap_flush(prealloc_cf);
2726 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2727 ceph_cap_string(dirtied), mask);
2729 ceph_mdsc_put_request(req);
2730 ceph_free_cap_flush(prealloc_cf);
2732 if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2733 __ceph_do_pending_vmtruncate(inode);
2741 int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2744 struct inode *inode = d_inode(dentry);
2745 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2748 if (ceph_snap(inode) != CEPH_NOSNAP)
2751 if (ceph_inode_is_shutdown(inode))
2754 err = fscrypt_prepare_setattr(dentry, attr);
2758 err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2762 if ((attr->ia_valid & ATTR_SIZE) &&
2763 attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
2766 if ((attr->ia_valid & ATTR_SIZE) &&
2767 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2770 err = __ceph_setattr(inode, attr, NULL);
2772 if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2773 err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode);
2778 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
2780 int issued = ceph_caps_issued(ceph_inode(inode));
2783 * If any 'x' caps is issued we can just choose the auth MDS
2784 * instead of the random replica MDSes. Because only when the
2785 * Locker is in LOCK_EXEC state will the loner client could
2786 * get the 'x' caps. And if we send the getattr requests to
2787 * any replica MDS it must auth pin and tries to rdlock from
2788 * the auth MDS, and then the auth MDS need to do the Locker
2789 * state transition to LOCK_SYNC. And after that the lock state
2792 * This cost much when doing the Locker state transition and
2793 * usually will need to revoke caps from clients.
2795 * And for the 'Xs' caps for getxattr we will also choose the
2796 * auth MDS, because the MDS side code is buggy due to setxattr
2797 * won't notify the replica MDSes when the values changed and
2798 * the replica MDS will return the old values. Though we will
2799 * fix it in MDS code, but this still makes sense for old ceph.
2801 if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
2802 || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
2803 return USE_AUTH_MDS;
2809 * Verify that we have a lease on the given mask. If not,
2810 * do a getattr against an mds.
2812 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2813 int mask, bool force)
2815 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2816 struct ceph_mds_client *mdsc = fsc->mdsc;
2817 struct ceph_mds_request *req;
2821 if (ceph_snap(inode) == CEPH_SNAPDIR) {
2822 dout("do_getattr inode %p SNAPDIR\n", inode);
2826 dout("do_getattr inode %p mask %s mode 0%o\n",
2827 inode, ceph_cap_string(mask), inode->i_mode);
2828 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2831 mode = ceph_try_to_choose_auth_mds(inode, mask);
2832 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2834 return PTR_ERR(req);
2835 req->r_inode = inode;
2837 req->r_num_caps = 1;
2838 req->r_args.getattr.mask = cpu_to_le32(mask);
2839 req->r_locked_page = locked_page;
2840 err = ceph_mdsc_do_request(mdsc, NULL, req);
2841 if (locked_page && err == 0) {
2842 u64 inline_version = req->r_reply_info.targeti.inline_version;
2843 if (inline_version == 0) {
2844 /* the reply is supposed to contain inline data */
2846 } else if (inline_version == CEPH_INLINE_NONE ||
2847 inline_version == 1) {
2850 err = req->r_reply_info.targeti.inline_len;
2853 ceph_mdsc_put_request(req);
2854 dout("do_getattr result=%d\n", err);
2858 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
2861 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2862 struct ceph_mds_client *mdsc = fsc->mdsc;
2863 struct ceph_mds_request *req;
2864 int mode = USE_AUTH_MDS;
2867 size_t xattr_value_len;
2869 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
2875 req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR;
2876 req->r_path2 = kstrdup(name, GFP_NOFS);
2877 if (!req->r_path2) {
2883 req->r_inode = inode;
2884 err = ceph_mdsc_do_request(mdsc, NULL, req);
2888 xattr_value = req->r_reply_info.xattr_info.xattr_value;
2889 xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
2891 dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
2893 err = (int)xattr_value_len;
2897 if (xattr_value_len > size) {
2902 memcpy(value, xattr_value, xattr_value_len);
2904 ceph_mdsc_put_request(req);
2906 dout("do_getvxattr result=%d\n", err);
2912 * Check inode permissions. We verify we have a valid value for
2913 * the AUTH cap, then call the generic handler.
2915 int ceph_permission(struct mnt_idmap *idmap, struct inode *inode,
2920 if (mask & MAY_NOT_BLOCK)
2923 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2926 err = generic_permission(&nop_mnt_idmap, inode, mask);
2930 /* Craft a mask of needed caps given a set of requested statx attrs. */
2931 static int statx_to_caps(u32 want, umode_t mode)
2935 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE))
2936 mask |= CEPH_CAP_AUTH_SHARED;
2938 if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) {
2940 * The link count for directories depends on inode->i_subdirs,
2941 * and that is only updated when Fs caps are held.
2944 mask |= CEPH_CAP_FILE_SHARED;
2946 mask |= CEPH_CAP_LINK_SHARED;
2949 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE))
2950 mask |= CEPH_CAP_FILE_SHARED;
2952 if (want & (STATX_CTIME|STATX_CHANGE_COOKIE))
2953 mask |= CEPH_CAP_XATTR_SHARED;
2959 * Get all the attributes. If we have sufficient caps for the requested attrs,
2960 * then we can avoid talking to the MDS at all.
2962 int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
2963 struct kstat *stat, u32 request_mask, unsigned int flags)
2965 struct inode *inode = d_inode(path->dentry);
2966 struct super_block *sb = inode->i_sb;
2967 struct ceph_inode_info *ci = ceph_inode(inode);
2968 u32 valid_mask = STATX_BASIC_STATS;
2971 if (ceph_inode_is_shutdown(inode))
2974 /* Skip the getattr altogether if we're asked not to sync */
2975 if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
2976 err = ceph_do_getattr(inode,
2977 statx_to_caps(request_mask, inode->i_mode),
2978 flags & AT_STATX_FORCE_SYNC);
2983 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2984 stat->ino = ceph_present_inode(inode);
2987 * btime on newly-allocated inodes is 0, so if this is still set to
2988 * that, then assume that it's not valid.
2990 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2991 stat->btime = ci->i_btime;
2992 valid_mask |= STATX_BTIME;
2995 if (request_mask & STATX_CHANGE_COOKIE) {
2996 stat->change_cookie = inode_peek_iversion_raw(inode);
2997 valid_mask |= STATX_CHANGE_COOKIE;
3000 if (ceph_snap(inode) == CEPH_NOSNAP)
3001 stat->dev = sb->s_dev;
3003 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
3005 if (S_ISDIR(inode->i_mode)) {
3006 if (ceph_test_mount_opt(ceph_sb_to_client(sb), RBYTES)) {
3007 stat->size = ci->i_rbytes;
3008 } else if (ceph_snap(inode) == CEPH_SNAPDIR) {
3009 struct ceph_inode_info *pci;
3010 struct ceph_snap_realm *realm;
3011 struct inode *parent;
3013 parent = ceph_lookup_inode(sb, ceph_ino(inode));
3015 return PTR_ERR(parent);
3017 pci = ceph_inode(parent);
3018 spin_lock(&pci->i_ceph_lock);
3019 realm = pci->i_snap_realm;
3021 stat->size = realm->num_snaps;
3024 spin_unlock(&pci->i_ceph_lock);
3027 stat->size = ci->i_files + ci->i_subdirs;
3030 stat->blksize = 65536;
3032 * Some applications rely on the number of st_nlink
3033 * value on directories to be either 0 (if unlinked)
3034 * or 2 + number of subdirectories.
3036 if (stat->nlink == 1)
3037 /* '.' + '..' + subdirs */
3038 stat->nlink = 1 + 1 + ci->i_subdirs;
3041 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
3042 if (IS_ENCRYPTED(inode))
3043 stat->attributes |= STATX_ATTR_ENCRYPTED;
3044 stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC |
3045 STATX_ATTR_ENCRYPTED);
3047 stat->result_mask = request_mask & valid_mask;
3051 void ceph_inode_shutdown(struct inode *inode)
3053 struct ceph_inode_info *ci = ceph_inode(inode);
3056 bool invalidate = false;
3058 spin_lock(&ci->i_ceph_lock);
3059 ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
3060 p = rb_first(&ci->i_caps);
3062 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
3065 iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
3067 spin_unlock(&ci->i_ceph_lock);
3070 ceph_queue_invalidate(inode);