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)
132 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
133 struct ceph_client *cl = mdsc->fsc->client;
136 if (ceph_vino_is_reserved(vino))
137 return ERR_PTR(-EREMOTEIO);
140 inode = inode_insert5(newino, (unsigned long)vino.ino,
141 ceph_ino_compare, ceph_set_ino_cb, &vino);
145 inode = iget5_locked(sb, (unsigned long)vino.ino,
146 ceph_ino_compare, ceph_set_ino_cb, &vino);
150 doutc(cl, "no inode found for %llx.%llx\n", vino.ino, vino.snap);
151 return ERR_PTR(-ENOMEM);
154 doutc(cl, "on %llx=%llx.%llx got %p new %d\n",
155 ceph_present_inode(inode), ceph_vinop(inode), inode,
156 !!(inode->i_state & I_NEW));
161 * get/constuct snapdir inode for a given directory
163 struct inode *ceph_get_snapdir(struct inode *parent)
165 struct ceph_client *cl = ceph_inode_to_client(parent);
166 struct ceph_vino vino = {
167 .ino = ceph_ino(parent),
168 .snap = CEPH_SNAPDIR,
170 struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
171 struct ceph_inode_info *ci = ceph_inode(inode);
177 if (!S_ISDIR(parent->i_mode)) {
178 pr_warn_once_client(cl, "bad snapdir parent type (mode=0%o)\n",
183 if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
184 pr_warn_once_client(cl, "bad snapdir inode type (mode=0%o)\n",
189 inode->i_mode = parent->i_mode;
190 inode->i_uid = parent->i_uid;
191 inode->i_gid = parent->i_gid;
192 inode_set_mtime_to_ts(inode, inode_get_mtime(parent));
193 inode_set_ctime_to_ts(inode, inode_get_ctime(parent));
194 inode_set_atime_to_ts(inode, inode_get_atime(parent));
196 ci->i_btime = ceph_inode(parent)->i_btime;
198 #ifdef CONFIG_FS_ENCRYPTION
199 /* if encrypted, just borrow fscrypt_auth from parent */
200 if (IS_ENCRYPTED(parent)) {
201 struct ceph_inode_info *pci = ceph_inode(parent);
203 ci->fscrypt_auth = kmemdup(pci->fscrypt_auth,
204 pci->fscrypt_auth_len,
206 if (ci->fscrypt_auth) {
207 inode->i_flags |= S_ENCRYPTED;
208 ci->fscrypt_auth_len = pci->fscrypt_auth_len;
210 doutc(cl, "Failed to alloc snapdir fscrypt_auth\n");
216 if (inode->i_state & I_NEW) {
217 inode->i_op = &ceph_snapdir_iops;
218 inode->i_fop = &ceph_snapdir_fops;
219 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
220 unlock_new_inode(inode);
225 if ((inode->i_state & I_NEW))
226 discard_new_inode(inode);
232 const struct inode_operations ceph_file_iops = {
233 .permission = ceph_permission,
234 .setattr = ceph_setattr,
235 .getattr = ceph_getattr,
236 .listxattr = ceph_listxattr,
237 .get_inode_acl = ceph_get_acl,
238 .set_acl = ceph_set_acl,
243 * We use a 'frag tree' to keep track of the MDS's directory fragments
244 * for a given inode (usually there is just a single fragment). We
245 * need to know when a child frag is delegated to a new MDS, or when
246 * it is flagged as replicated, so we can direct our requests
251 * find/create a frag in the tree
253 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
256 struct inode *inode = &ci->netfs.inode;
257 struct ceph_client *cl = ceph_inode_to_client(inode);
259 struct rb_node *parent = NULL;
260 struct ceph_inode_frag *frag;
263 p = &ci->i_fragtree.rb_node;
266 frag = rb_entry(parent, struct ceph_inode_frag, node);
267 c = ceph_frag_compare(f, frag->frag);
276 frag = kmalloc(sizeof(*frag), GFP_NOFS);
278 return ERR_PTR(-ENOMEM);
285 rb_link_node(&frag->node, parent, p);
286 rb_insert_color(&frag->node, &ci->i_fragtree);
288 doutc(cl, "added %p %llx.%llx frag %x\n", inode, ceph_vinop(inode), f);
293 * find a specific frag @f
295 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
297 struct rb_node *n = ci->i_fragtree.rb_node;
300 struct ceph_inode_frag *frag =
301 rb_entry(n, struct ceph_inode_frag, node);
302 int c = ceph_frag_compare(f, frag->frag);
314 * Choose frag containing the given value @v. If @pfrag is
315 * specified, copy the frag delegation info to the caller if
318 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
319 struct ceph_inode_frag *pfrag, int *found)
321 struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
322 u32 t = ceph_frag_make(0, 0);
323 struct ceph_inode_frag *frag;
331 WARN_ON(!ceph_frag_contains_value(t, v));
332 frag = __ceph_find_frag(ci, t);
334 break; /* t is a leaf */
335 if (frag->split_by == 0) {
337 memcpy(pfrag, frag, sizeof(*pfrag));
344 nway = 1 << frag->split_by;
345 doutc(cl, "frag(%x) %x splits by %d (%d ways)\n", v, t,
346 frag->split_by, nway);
347 for (i = 0; i < nway; i++) {
348 n = ceph_frag_make_child(t, frag->split_by, i);
349 if (ceph_frag_contains_value(n, v)) {
356 doutc(cl, "frag(%x) = %x\n", v, t);
361 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
362 struct ceph_inode_frag *pfrag, int *found)
365 mutex_lock(&ci->i_fragtree_mutex);
366 ret = __ceph_choose_frag(ci, v, pfrag, found);
367 mutex_unlock(&ci->i_fragtree_mutex);
372 * Process dirfrag (delegation) info from the mds. Include leaf
373 * fragment in tree ONLY if ndist > 0. Otherwise, only
374 * branches/splits are included in i_fragtree)
376 static int ceph_fill_dirfrag(struct inode *inode,
377 struct ceph_mds_reply_dirfrag *dirinfo)
379 struct ceph_inode_info *ci = ceph_inode(inode);
380 struct ceph_client *cl = ceph_inode_to_client(inode);
381 struct ceph_inode_frag *frag;
382 u32 id = le32_to_cpu(dirinfo->frag);
383 int mds = le32_to_cpu(dirinfo->auth);
384 int ndist = le32_to_cpu(dirinfo->ndist);
389 spin_lock(&ci->i_ceph_lock);
391 diri_auth = ci->i_auth_cap->mds;
392 spin_unlock(&ci->i_ceph_lock);
394 if (mds == -1) /* CDIR_AUTH_PARENT */
397 mutex_lock(&ci->i_fragtree_mutex);
398 if (ndist == 0 && mds == diri_auth) {
399 /* no delegation info needed. */
400 frag = __ceph_find_frag(ci, id);
403 if (frag->split_by == 0) {
404 /* tree leaf, remove */
405 doutc(cl, "removed %p %llx.%llx frag %x (no ref)\n",
406 inode, ceph_vinop(inode), id);
407 rb_erase(&frag->node, &ci->i_fragtree);
410 /* tree branch, keep and clear */
411 doutc(cl, "cleared %p %llx.%llx frag %x referral\n",
412 inode, ceph_vinop(inode), id);
420 /* find/add this frag to store mds delegation info */
421 frag = __get_or_create_frag(ci, id);
423 /* this is not the end of the world; we can continue
424 with bad/inaccurate delegation info */
425 pr_err_client(cl, "ENOMEM on mds ref %p %llx.%llx fg %x\n",
426 inode, ceph_vinop(inode),
427 le32_to_cpu(dirinfo->frag));
433 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
434 for (i = 0; i < frag->ndist; i++)
435 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
436 doutc(cl, "%p %llx.%llx frag %x ndist=%d\n", inode,
437 ceph_vinop(inode), frag->frag, frag->ndist);
440 mutex_unlock(&ci->i_fragtree_mutex);
444 static int frag_tree_split_cmp(const void *l, const void *r)
446 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
447 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
448 return ceph_frag_compare(le32_to_cpu(ls->frag),
449 le32_to_cpu(rs->frag));
452 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
455 return f == ceph_frag_make(0, 0);
456 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
458 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
461 static int ceph_fill_fragtree(struct inode *inode,
462 struct ceph_frag_tree_head *fragtree,
463 struct ceph_mds_reply_dirfrag *dirinfo)
465 struct ceph_client *cl = ceph_inode_to_client(inode);
466 struct ceph_inode_info *ci = ceph_inode(inode);
467 struct ceph_inode_frag *frag, *prev_frag = NULL;
468 struct rb_node *rb_node;
469 unsigned i, split_by, nsplits;
473 mutex_lock(&ci->i_fragtree_mutex);
474 nsplits = le32_to_cpu(fragtree->nsplits);
475 if (nsplits != ci->i_fragtree_nsplits) {
477 } else if (nsplits) {
478 i = get_random_u32_below(nsplits);
479 id = le32_to_cpu(fragtree->splits[i].frag);
480 if (!__ceph_find_frag(ci, id))
482 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
483 rb_node = rb_first(&ci->i_fragtree);
484 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
485 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
488 if (!update && dirinfo) {
489 id = le32_to_cpu(dirinfo->frag);
490 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
497 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
498 frag_tree_split_cmp, NULL);
501 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
502 rb_node = rb_first(&ci->i_fragtree);
503 for (i = 0; i < nsplits; i++) {
504 id = le32_to_cpu(fragtree->splits[i].frag);
505 split_by = le32_to_cpu(fragtree->splits[i].by);
506 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
507 pr_err_client(cl, "%p %llx.%llx invalid split %d/%u, "
508 "frag %x split by %d\n", inode,
509 ceph_vinop(inode), i, nsplits, id, split_by);
514 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
515 if (ceph_frag_compare(frag->frag, id) >= 0) {
516 if (frag->frag != id)
519 rb_node = rb_next(rb_node);
522 rb_node = rb_next(rb_node);
523 /* delete stale split/leaf node */
524 if (frag->split_by > 0 ||
525 !is_frag_child(frag->frag, prev_frag)) {
526 rb_erase(&frag->node, &ci->i_fragtree);
527 if (frag->split_by > 0)
528 ci->i_fragtree_nsplits--;
534 frag = __get_or_create_frag(ci, id);
538 if (frag->split_by == 0)
539 ci->i_fragtree_nsplits++;
540 frag->split_by = split_by;
541 doutc(cl, " frag %x split by %d\n", frag->frag, frag->split_by);
545 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
546 rb_node = rb_next(rb_node);
547 /* delete stale split/leaf node */
548 if (frag->split_by > 0 ||
549 !is_frag_child(frag->frag, prev_frag)) {
550 rb_erase(&frag->node, &ci->i_fragtree);
551 if (frag->split_by > 0)
552 ci->i_fragtree_nsplits--;
557 mutex_unlock(&ci->i_fragtree_mutex);
562 * initialize a newly allocated inode.
564 struct inode *ceph_alloc_inode(struct super_block *sb)
566 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
567 struct ceph_inode_info *ci;
570 ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
574 doutc(fsc->client, "%p\n", &ci->netfs.inode);
576 /* Set parameters for the netfs library */
577 netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
579 spin_lock_init(&ci->i_ceph_lock);
582 ci->i_inline_version = 0;
583 ci->i_time_warp_seq = 0;
584 ci->i_ceph_flags = 0;
585 atomic64_set(&ci->i_ordered_count, 1);
586 atomic64_set(&ci->i_release_count, 1);
587 atomic64_set(&ci->i_complete_seq[0], 0);
588 atomic64_set(&ci->i_complete_seq[1], 0);
589 ci->i_symlink = NULL;
594 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
595 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
596 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
598 ci->i_fragtree = RB_ROOT;
599 mutex_init(&ci->i_fragtree_mutex);
601 ci->i_xattrs.blob = NULL;
602 ci->i_xattrs.prealloc_blob = NULL;
603 ci->i_xattrs.dirty = false;
604 ci->i_xattrs.index = RB_ROOT;
605 ci->i_xattrs.count = 0;
606 ci->i_xattrs.names_size = 0;
607 ci->i_xattrs.vals_size = 0;
608 ci->i_xattrs.version = 0;
609 ci->i_xattrs.index_version = 0;
611 ci->i_caps = RB_ROOT;
612 ci->i_auth_cap = NULL;
613 ci->i_dirty_caps = 0;
614 ci->i_flushing_caps = 0;
615 INIT_LIST_HEAD(&ci->i_dirty_item);
616 INIT_LIST_HEAD(&ci->i_flushing_item);
617 ci->i_prealloc_cap_flush = NULL;
618 INIT_LIST_HEAD(&ci->i_cap_flush_list);
619 init_waitqueue_head(&ci->i_cap_wq);
620 ci->i_hold_caps_max = 0;
621 INIT_LIST_HEAD(&ci->i_cap_delay_list);
622 INIT_LIST_HEAD(&ci->i_cap_snaps);
623 ci->i_head_snapc = NULL;
626 ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
627 for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
628 ci->i_nr_by_mode[i] = 0;
630 mutex_init(&ci->i_truncate_mutex);
631 ci->i_truncate_seq = 0;
632 ci->i_truncate_size = 0;
633 ci->i_truncate_pending = 0;
634 ci->i_truncate_pagecache_size = 0;
637 ci->i_reported_size = 0;
638 ci->i_wanted_max_size = 0;
639 ci->i_requested_max_size = 0;
643 ci->i_rdcache_ref = 0;
647 ci->i_wrbuffer_ref = 0;
648 ci->i_wrbuffer_ref_head = 0;
649 atomic_set(&ci->i_filelock_ref, 0);
650 atomic_set(&ci->i_shared_gen, 1);
651 ci->i_rdcache_gen = 0;
652 ci->i_rdcache_revoking = 0;
654 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
655 INIT_LIST_HEAD(&ci->i_unsafe_iops);
656 spin_lock_init(&ci->i_unsafe_lock);
658 ci->i_snap_realm = NULL;
659 INIT_LIST_HEAD(&ci->i_snap_realm_item);
660 INIT_LIST_HEAD(&ci->i_snap_flush_item);
662 INIT_WORK(&ci->i_work, ceph_inode_work);
664 memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
665 #ifdef CONFIG_FS_ENCRYPTION
666 ci->fscrypt_auth = NULL;
667 ci->fscrypt_auth_len = 0;
669 return &ci->netfs.inode;
672 void ceph_free_inode(struct inode *inode)
674 struct ceph_inode_info *ci = ceph_inode(inode);
676 kfree(ci->i_symlink);
677 #ifdef CONFIG_FS_ENCRYPTION
678 kfree(ci->fscrypt_auth);
680 fscrypt_free_inode(inode);
681 kmem_cache_free(ceph_inode_cachep, ci);
684 void ceph_evict_inode(struct inode *inode)
686 struct ceph_inode_info *ci = ceph_inode(inode);
687 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
688 struct ceph_client *cl = ceph_inode_to_client(inode);
689 struct ceph_inode_frag *frag;
692 doutc(cl, "%p ino %llx.%llx\n", inode, ceph_vinop(inode));
694 percpu_counter_dec(&mdsc->metric.total_inodes);
696 truncate_inode_pages_final(&inode->i_data);
697 if (inode->i_state & I_PINNING_FSCACHE_WB)
698 ceph_fscache_unuse_cookie(inode, true);
701 ceph_fscache_unregister_inode_cookie(ci);
702 fscrypt_put_encryption_info(inode);
704 __ceph_remove_caps(ci);
706 if (__ceph_has_quota(ci, QUOTA_GET_ANY))
707 ceph_adjust_quota_realms_count(inode, false);
710 * we may still have a snap_realm reference if there are stray
711 * caps in i_snap_caps.
713 if (ci->i_snap_realm) {
714 if (ceph_snap(inode) == CEPH_NOSNAP) {
715 doutc(cl, " dropping residual ref to snap realm %p\n",
717 ceph_change_snap_realm(inode, NULL);
719 ceph_put_snapid_map(mdsc, ci->i_snapid_map);
720 ci->i_snap_realm = NULL;
724 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
725 frag = rb_entry(n, struct ceph_inode_frag, node);
726 rb_erase(n, &ci->i_fragtree);
729 ci->i_fragtree_nsplits = 0;
731 __ceph_destroy_xattrs(ci);
732 if (ci->i_xattrs.blob)
733 ceph_buffer_put(ci->i_xattrs.blob);
734 if (ci->i_xattrs.prealloc_blob)
735 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
737 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
738 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
741 static inline blkcnt_t calc_inode_blocks(u64 size)
743 return (size + (1<<9) - 1) >> 9;
747 * Helpers to fill in size, ctime, mtime, and atime. We have to be
748 * careful because either the client or MDS may have more up to date
749 * info, depending on which capabilities are held, and whether
750 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
751 * and size are monotonically increasing, except when utimes() or
752 * truncate() increments the corresponding _seq values.)
754 int ceph_fill_file_size(struct inode *inode, int issued,
755 u32 truncate_seq, u64 truncate_size, u64 size)
757 struct ceph_client *cl = ceph_inode_to_client(inode);
758 struct ceph_inode_info *ci = ceph_inode(inode);
760 loff_t isize = i_size_read(inode);
762 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
763 (truncate_seq == ci->i_truncate_seq && size > isize)) {
764 doutc(cl, "size %lld -> %llu\n", isize, size);
765 if (size > 0 && S_ISDIR(inode->i_mode)) {
766 pr_err_client(cl, "non-zero size for directory\n");
769 i_size_write(inode, size);
770 inode->i_blocks = calc_inode_blocks(size);
772 * If we're expanding, then we should be able to just update
773 * the existing cookie.
776 ceph_fscache_update(inode);
777 ci->i_reported_size = size;
778 if (truncate_seq != ci->i_truncate_seq) {
779 doutc(cl, "truncate_seq %u -> %u\n",
780 ci->i_truncate_seq, truncate_seq);
781 ci->i_truncate_seq = truncate_seq;
783 /* the MDS should have revoked these caps */
784 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_RD |
785 CEPH_CAP_FILE_LAZYIO));
787 * If we hold relevant caps, or in the case where we're
788 * not the only client referencing this file and we
789 * don't hold those caps, then we need to check whether
790 * the file is either opened or mmaped
792 if ((issued & (CEPH_CAP_FILE_CACHE|
793 CEPH_CAP_FILE_BUFFER)) ||
794 mapping_mapped(inode->i_mapping) ||
795 __ceph_is_file_opened(ci)) {
796 ci->i_truncate_pending++;
803 * It's possible that the new sizes of the two consecutive
804 * size truncations will be in the same fscrypt last block,
805 * and we need to truncate the corresponding page caches
808 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) {
809 doutc(cl, "truncate_size %lld -> %llu, encrypted %d\n",
810 ci->i_truncate_size, truncate_size,
811 !!IS_ENCRYPTED(inode));
813 ci->i_truncate_size = truncate_size;
815 if (IS_ENCRYPTED(inode)) {
816 doutc(cl, "truncate_pagecache_size %lld -> %llu\n",
817 ci->i_truncate_pagecache_size, size);
818 ci->i_truncate_pagecache_size = size;
820 ci->i_truncate_pagecache_size = truncate_size;
826 void ceph_fill_file_time(struct inode *inode, int issued,
827 u64 time_warp_seq, struct timespec64 *ctime,
828 struct timespec64 *mtime, struct timespec64 *atime)
830 struct ceph_client *cl = ceph_inode_to_client(inode);
831 struct ceph_inode_info *ci = ceph_inode(inode);
832 struct timespec64 ictime = inode_get_ctime(inode);
835 if (issued & (CEPH_CAP_FILE_EXCL|
837 CEPH_CAP_FILE_BUFFER|
839 CEPH_CAP_XATTR_EXCL)) {
840 if (ci->i_version == 0 ||
841 timespec64_compare(ctime, &ictime) > 0) {
842 doutc(cl, "ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
843 ictime.tv_sec, ictime.tv_nsec,
844 ctime->tv_sec, ctime->tv_nsec);
845 inode_set_ctime_to_ts(inode, *ctime);
847 if (ci->i_version == 0 ||
848 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
849 /* the MDS did a utimes() */
850 doutc(cl, "mtime %lld.%09ld -> %lld.%09ld tw %d -> %d\n",
851 inode_get_mtime_sec(inode),
852 inode_get_mtime_nsec(inode),
853 mtime->tv_sec, mtime->tv_nsec,
854 ci->i_time_warp_seq, (int)time_warp_seq);
856 inode_set_mtime_to_ts(inode, *mtime);
857 inode_set_atime_to_ts(inode, *atime);
858 ci->i_time_warp_seq = time_warp_seq;
859 } else if (time_warp_seq == ci->i_time_warp_seq) {
860 struct timespec64 ts;
862 /* nobody did utimes(); take the max */
863 ts = inode_get_mtime(inode);
864 if (timespec64_compare(mtime, &ts) > 0) {
865 doutc(cl, "mtime %lld.%09ld -> %lld.%09ld inc\n",
866 ts.tv_sec, ts.tv_nsec,
867 mtime->tv_sec, mtime->tv_nsec);
868 inode_set_mtime_to_ts(inode, *mtime);
870 ts = inode_get_atime(inode);
871 if (timespec64_compare(atime, &ts) > 0) {
872 doutc(cl, "atime %lld.%09ld -> %lld.%09ld inc\n",
873 ts.tv_sec, ts.tv_nsec,
874 atime->tv_sec, atime->tv_nsec);
875 inode_set_atime_to_ts(inode, *atime);
877 } else if (issued & CEPH_CAP_FILE_EXCL) {
878 /* we did a utimes(); ignore mds values */
883 /* we have no write|excl caps; whatever the MDS says is true */
884 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
885 inode_set_ctime_to_ts(inode, *ctime);
886 inode_set_mtime_to_ts(inode, *mtime);
887 inode_set_atime_to_ts(inode, *atime);
888 ci->i_time_warp_seq = time_warp_seq;
893 if (warn) /* time_warp_seq shouldn't go backwards */
894 doutc(cl, "%p mds time_warp_seq %llu < %u\n", inode,
895 time_warp_seq, ci->i_time_warp_seq);
898 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
899 static int decode_encrypted_symlink(struct ceph_mds_client *mdsc,
901 int enclen, u8 **decsym)
903 struct ceph_client *cl = mdsc->fsc->client;
907 sym = kmalloc(enclen + 1, GFP_NOFS);
911 declen = ceph_base64_decode(encsym, enclen, sym);
914 "can't decode symlink (%d). Content: %.*s\n",
915 declen, enclen, encsym);
919 sym[declen + 1] = '\0';
924 static int decode_encrypted_symlink(struct ceph_mds_client *mdsc,
926 int symlen, u8 **decsym)
933 * Populate an inode based on info from mds. May be called on new or
936 int ceph_fill_inode(struct inode *inode, struct page *locked_page,
937 struct ceph_mds_reply_info_in *iinfo,
938 struct ceph_mds_reply_dirfrag *dirinfo,
939 struct ceph_mds_session *session, int cap_fmode,
940 struct ceph_cap_reservation *caps_reservation)
942 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
943 struct ceph_client *cl = mdsc->fsc->client;
944 struct ceph_mds_reply_inode *info = iinfo->in;
945 struct ceph_inode_info *ci = ceph_inode(inode);
946 int issued, new_issued, info_caps;
947 struct timespec64 mtime, atime, ctime;
948 struct ceph_buffer *xattr_blob = NULL;
949 struct ceph_buffer *old_blob = NULL;
950 struct ceph_string *pool_ns = NULL;
951 struct ceph_cap *new_cap = NULL;
954 bool queue_trunc = false;
955 bool new_version = false;
956 bool fill_inline = false;
957 umode_t mode = le32_to_cpu(info->mode);
958 dev_t rdev = le32_to_cpu(info->rdev);
960 lockdep_assert_held(&mdsc->snap_rwsem);
962 doutc(cl, "%p ino %llx.%llx v %llu had %llu\n", inode, ceph_vinop(inode),
963 le64_to_cpu(info->version), ci->i_version);
965 /* Once I_NEW is cleared, we can't change type or dev numbers */
966 if (inode->i_state & I_NEW) {
967 inode->i_mode = mode;
969 if (inode_wrong_type(inode, mode)) {
970 pr_warn_once_client(cl,
971 "inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
972 ceph_vinop(inode), inode->i_mode, mode);
976 if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
977 pr_warn_once_client(cl,
978 "dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
979 ceph_vinop(inode), MAJOR(inode->i_rdev),
980 MINOR(inode->i_rdev), MAJOR(rdev),
986 info_caps = le32_to_cpu(info->cap.caps);
988 /* prealloc new cap struct */
989 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
990 new_cap = ceph_get_cap(mdsc, caps_reservation);
996 * prealloc xattr data, if it looks like we'll need it. only
997 * if len > 4 (meaning there are actually xattrs; the first 4
998 * bytes are the xattr count).
1000 if (iinfo->xattr_len > 4) {
1001 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
1003 pr_err_client(cl, "ENOMEM xattr blob %d bytes\n",
1007 if (iinfo->pool_ns_len > 0)
1008 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
1009 iinfo->pool_ns_len);
1011 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
1012 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
1014 spin_lock(&ci->i_ceph_lock);
1017 * provided version will be odd if inode value is projected,
1018 * even if stable. skip the update if we have newer stable
1019 * info (ours>=theirs, e.g. due to racing mds replies), unless
1020 * we are getting projected (unstable) info (in which case the
1021 * version is odd, and we want ours>theirs).
1027 if (ci->i_version == 0 ||
1028 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1029 le64_to_cpu(info->version) > (ci->i_version & ~1)))
1032 /* Update change_attribute */
1033 inode_set_max_iversion_raw(inode, iinfo->change_attr);
1035 __ceph_caps_issued(ci, &issued);
1036 issued |= __ceph_caps_dirty(ci);
1037 new_issued = ~issued & info_caps;
1039 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
1041 #ifdef CONFIG_FS_ENCRYPTION
1042 if (iinfo->fscrypt_auth_len &&
1043 ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) {
1044 kfree(ci->fscrypt_auth);
1045 ci->fscrypt_auth_len = iinfo->fscrypt_auth_len;
1046 ci->fscrypt_auth = iinfo->fscrypt_auth;
1047 iinfo->fscrypt_auth = NULL;
1048 iinfo->fscrypt_auth_len = 0;
1049 inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
1053 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
1054 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
1055 inode->i_mode = mode;
1056 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
1057 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
1058 doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode,
1059 ceph_vinop(inode), inode->i_mode,
1060 from_kuid(&init_user_ns, inode->i_uid),
1061 from_kgid(&init_user_ns, inode->i_gid));
1062 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
1063 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
1066 /* directories have fl_stripe_unit set to zero */
1067 if (IS_ENCRYPTED(inode))
1068 inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
1069 else if (le32_to_cpu(info->layout.fl_stripe_unit))
1071 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
1073 inode->i_blkbits = CEPH_BLOCK_SHIFT;
1075 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
1076 (issued & CEPH_CAP_LINK_EXCL) == 0)
1077 set_nlink(inode, le32_to_cpu(info->nlink));
1079 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
1080 /* be careful with mtime, atime, size */
1081 ceph_decode_timespec64(&atime, &info->atime);
1082 ceph_decode_timespec64(&mtime, &info->mtime);
1083 ceph_decode_timespec64(&ctime, &info->ctime);
1084 ceph_fill_file_time(inode, issued,
1085 le32_to_cpu(info->time_warp_seq),
1086 &ctime, &mtime, &atime);
1089 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
1090 ci->i_files = le64_to_cpu(info->files);
1091 ci->i_subdirs = le64_to_cpu(info->subdirs);
1095 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
1096 u64 size = le64_to_cpu(info->size);
1097 s64 old_pool = ci->i_layout.pool_id;
1098 struct ceph_string *old_ns;
1100 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
1101 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
1102 lockdep_is_held(&ci->i_ceph_lock));
1103 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
1105 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
1106 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
1110 if (IS_ENCRYPTED(inode) && size &&
1111 iinfo->fscrypt_file_len == sizeof(__le64)) {
1112 u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file);
1114 if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) {
1118 "fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n",
1123 queue_trunc = ceph_fill_file_size(inode, issued,
1124 le32_to_cpu(info->truncate_seq),
1125 le64_to_cpu(info->truncate_size),
1127 /* only update max_size on auth cap */
1128 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1129 ci->i_max_size != le64_to_cpu(info->max_size)) {
1130 doutc(cl, "max_size %lld -> %llu\n",
1131 ci->i_max_size, le64_to_cpu(info->max_size));
1132 ci->i_max_size = le64_to_cpu(info->max_size);
1136 /* layout and rstat are not tracked by capability, update them if
1137 * the inode info is from auth mds */
1138 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
1139 if (S_ISDIR(inode->i_mode)) {
1140 ci->i_dir_layout = iinfo->dir_layout;
1141 ci->i_rbytes = le64_to_cpu(info->rbytes);
1142 ci->i_rfiles = le64_to_cpu(info->rfiles);
1143 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
1144 ci->i_dir_pin = iinfo->dir_pin;
1145 ci->i_rsnaps = iinfo->rsnaps;
1146 ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
1151 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
1152 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
1153 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
1154 if (ci->i_xattrs.blob)
1155 old_blob = ci->i_xattrs.blob;
1156 ci->i_xattrs.blob = xattr_blob;
1158 memcpy(ci->i_xattrs.blob->vec.iov_base,
1159 iinfo->xattr_data, iinfo->xattr_len);
1160 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
1161 ceph_forget_all_cached_acls(inode);
1162 ceph_security_invalidate_secctx(inode);
1166 /* finally update i_version */
1167 if (le64_to_cpu(info->version) > ci->i_version)
1168 ci->i_version = le64_to_cpu(info->version);
1170 inode->i_mapping->a_ops = &ceph_aops;
1172 switch (inode->i_mode & S_IFMT) {
1177 inode->i_blkbits = PAGE_SHIFT;
1178 init_special_inode(inode, inode->i_mode, rdev);
1179 inode->i_op = &ceph_file_iops;
1182 inode->i_op = &ceph_file_iops;
1183 inode->i_fop = &ceph_file_fops;
1186 if (!ci->i_symlink) {
1187 u32 symlen = iinfo->symlink_len;
1190 spin_unlock(&ci->i_ceph_lock);
1192 if (IS_ENCRYPTED(inode)) {
1193 if (symlen != i_size_read(inode))
1195 "%p %llx.%llx BAD symlink size %lld\n",
1196 inode, ceph_vinop(inode),
1197 i_size_read(inode));
1199 err = decode_encrypted_symlink(mdsc, iinfo->symlink,
1200 symlen, (u8 **)&sym);
1203 "decoding encrypted symlink failed: %d\n",
1208 i_size_write(inode, symlen);
1209 inode->i_blocks = calc_inode_blocks(symlen);
1211 if (symlen != i_size_read(inode)) {
1213 "%p %llx.%llx BAD symlink size %lld\n",
1214 inode, ceph_vinop(inode),
1215 i_size_read(inode));
1216 i_size_write(inode, symlen);
1217 inode->i_blocks = calc_inode_blocks(symlen);
1221 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
1226 spin_lock(&ci->i_ceph_lock);
1228 ci->i_symlink = sym;
1230 kfree(sym); /* lost a race */
1233 if (IS_ENCRYPTED(inode)) {
1235 * Encrypted symlinks need to be decrypted before we can
1236 * cache their targets in i_link. Don't touch it here.
1238 inode->i_op = &ceph_encrypted_symlink_iops;
1240 inode->i_link = ci->i_symlink;
1241 inode->i_op = &ceph_symlink_iops;
1245 inode->i_op = &ceph_dir_iops;
1246 inode->i_fop = &ceph_dir_fops;
1249 pr_err_client(cl, "%p %llx.%llx BAD mode 0%o\n", inode,
1250 ceph_vinop(inode), inode->i_mode);
1253 /* were we issued a capability? */
1255 if (ceph_snap(inode) == CEPH_NOSNAP) {
1256 ceph_add_cap(inode, session,
1257 le64_to_cpu(info->cap.cap_id),
1259 le32_to_cpu(info->cap.wanted),
1260 le32_to_cpu(info->cap.seq),
1261 le32_to_cpu(info->cap.mseq),
1262 le64_to_cpu(info->cap.realm),
1263 info->cap.flags, &new_cap);
1265 /* set dir completion flag? */
1266 if (S_ISDIR(inode->i_mode) &&
1267 ci->i_files == 0 && ci->i_subdirs == 0 &&
1268 (info_caps & CEPH_CAP_FILE_SHARED) &&
1269 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1270 !__ceph_dir_is_complete(ci)) {
1271 doutc(cl, " marking %p complete (empty)\n",
1273 i_size_write(inode, 0);
1274 __ceph_dir_set_complete(ci,
1275 atomic64_read(&ci->i_release_count),
1276 atomic64_read(&ci->i_ordered_count));
1281 doutc(cl, " %p got snap_caps %s\n", inode,
1282 ceph_cap_string(info_caps));
1283 ci->i_snap_caps |= info_caps;
1287 if (iinfo->inline_version > 0 &&
1288 iinfo->inline_version >= ci->i_inline_version) {
1289 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1290 ci->i_inline_version = iinfo->inline_version;
1291 if (ceph_has_inline_data(ci) &&
1292 (locked_page || (info_caps & cache_caps)))
1296 if (cap_fmode >= 0) {
1298 pr_warn_client(cl, "mds issued no caps on %llx.%llx\n",
1300 __ceph_touch_fmode(ci, mdsc, cap_fmode);
1303 spin_unlock(&ci->i_ceph_lock);
1305 ceph_fscache_register_inode_cookie(inode);
1308 ceph_fill_inline_data(inode, locked_page,
1309 iinfo->inline_data, iinfo->inline_len);
1312 wake_up_all(&ci->i_cap_wq);
1314 /* queue truncate if we saw i_size decrease */
1316 ceph_queue_vmtruncate(inode);
1318 /* populate frag tree */
1319 if (S_ISDIR(inode->i_mode))
1320 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1322 /* update delegation info? */
1324 ceph_fill_dirfrag(inode, dirinfo);
1329 ceph_put_cap(mdsc, new_cap);
1330 ceph_buffer_put(old_blob);
1331 ceph_buffer_put(xattr_blob);
1332 ceph_put_string(pool_ns);
1337 * caller should hold session s_mutex and dentry->d_lock.
1339 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1340 struct ceph_mds_reply_lease *lease,
1341 struct ceph_mds_session *session,
1342 unsigned long from_time,
1343 struct ceph_mds_session **old_lease_session)
1345 struct ceph_client *cl = ceph_inode_to_client(dir);
1346 struct ceph_dentry_info *di = ceph_dentry(dentry);
1347 unsigned mask = le16_to_cpu(lease->mask);
1348 long unsigned duration = le32_to_cpu(lease->duration_ms);
1349 long unsigned ttl = from_time + (duration * HZ) / 1000;
1350 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1352 doutc(cl, "%p duration %lu ms ttl %lu\n", dentry, duration, ttl);
1354 /* only track leases on regular dentries */
1355 if (ceph_snap(dir) != CEPH_NOSNAP)
1358 if (mask & CEPH_LEASE_PRIMARY_LINK)
1359 di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1361 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1363 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1364 if (!(mask & CEPH_LEASE_VALID)) {
1365 __ceph_dentry_dir_lease_touch(di);
1369 if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
1370 time_before(ttl, di->time))
1371 return; /* we already have a newer lease. */
1373 if (di->lease_session && di->lease_session != session) {
1374 *old_lease_session = di->lease_session;
1375 di->lease_session = NULL;
1378 if (!di->lease_session)
1379 di->lease_session = ceph_get_mds_session(session);
1380 di->lease_gen = atomic_read(&session->s_cap_gen);
1381 di->lease_seq = le32_to_cpu(lease->seq);
1382 di->lease_renew_after = half_ttl;
1383 di->lease_renew_from = 0;
1386 __ceph_dentry_lease_touch(di);
1389 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1390 struct ceph_mds_reply_lease *lease,
1391 struct ceph_mds_session *session,
1392 unsigned long from_time)
1394 struct ceph_mds_session *old_lease_session = NULL;
1395 spin_lock(&dentry->d_lock);
1396 __update_dentry_lease(dir, dentry, lease, session, from_time,
1397 &old_lease_session);
1398 spin_unlock(&dentry->d_lock);
1399 ceph_put_mds_session(old_lease_session);
1403 * update dentry lease without having parent inode locked
1405 static void update_dentry_lease_careful(struct dentry *dentry,
1406 struct ceph_mds_reply_lease *lease,
1407 struct ceph_mds_session *session,
1408 unsigned long from_time,
1409 char *dname, u32 dname_len,
1410 struct ceph_vino *pdvino,
1411 struct ceph_vino *ptvino)
1415 struct ceph_mds_session *old_lease_session = NULL;
1417 spin_lock(&dentry->d_lock);
1418 /* make sure dentry's name matches target */
1419 if (dentry->d_name.len != dname_len ||
1420 memcmp(dentry->d_name.name, dname, dname_len))
1423 dir = d_inode(dentry->d_parent);
1424 /* make sure parent matches dvino */
1425 if (!ceph_ino_compare(dir, pdvino))
1428 /* make sure dentry's inode matches target. NULL ptvino means that
1429 * we expect a negative dentry */
1431 if (d_really_is_negative(dentry))
1433 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1436 if (d_really_is_positive(dentry))
1440 __update_dentry_lease(dir, dentry, lease, session,
1441 from_time, &old_lease_session);
1443 spin_unlock(&dentry->d_lock);
1444 ceph_put_mds_session(old_lease_session);
1448 * splice a dentry to an inode.
1449 * caller must hold directory i_rwsem for this to be safe.
1451 static int splice_dentry(struct dentry **pdn, struct inode *in)
1453 struct ceph_client *cl = ceph_inode_to_client(in);
1454 struct dentry *dn = *pdn;
1455 struct dentry *realdn;
1457 BUG_ON(d_inode(dn));
1459 if (S_ISDIR(in->i_mode)) {
1460 /* If inode is directory, d_splice_alias() below will remove
1461 * 'realdn' from its origin parent. We need to ensure that
1462 * origin parent's readdir cache will not reference 'realdn'
1464 realdn = d_find_any_alias(in);
1466 struct ceph_dentry_info *di = ceph_dentry(realdn);
1467 spin_lock(&realdn->d_lock);
1469 realdn->d_op->d_prune(realdn);
1472 di->lease_shared_gen = 0;
1475 spin_unlock(&realdn->d_lock);
1480 /* dn must be unhashed */
1481 if (!d_unhashed(dn))
1483 realdn = d_splice_alias(in, dn);
1484 if (IS_ERR(realdn)) {
1485 pr_err_client(cl, "error %ld %p inode %p ino %llx.%llx\n",
1486 PTR_ERR(realdn), dn, in, ceph_vinop(in));
1487 return PTR_ERR(realdn);
1491 doutc(cl, "dn %p (%d) spliced with %p (%d) inode %p ino %llx.%llx\n",
1492 dn, d_count(dn), realdn, d_count(realdn),
1493 d_inode(realdn), ceph_vinop(d_inode(realdn)));
1497 BUG_ON(!ceph_dentry(dn));
1498 doutc(cl, "dn %p attached to %p ino %llx.%llx\n", dn,
1499 d_inode(dn), ceph_vinop(d_inode(dn)));
1505 * Incorporate results into the local cache. This is either just
1506 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1509 * A reply may contain
1510 * a directory inode along with a dentry.
1511 * and/or a target inode
1513 * Called with snap_rwsem (read).
1515 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1517 struct ceph_mds_session *session = req->r_session;
1518 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1519 struct inode *in = NULL;
1520 struct ceph_vino tvino, dvino;
1521 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
1522 struct ceph_client *cl = fsc->client;
1525 doutc(cl, "%p is_dentry %d is_target %d\n", req,
1526 rinfo->head->is_dentry, rinfo->head->is_target);
1528 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1529 doutc(cl, "reply is empty!\n");
1530 if (rinfo->head->result == 0 && req->r_parent)
1531 ceph_invalidate_dir_request(req);
1535 if (rinfo->head->is_dentry) {
1536 struct inode *dir = req->r_parent;
1539 err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1540 rinfo->dirfrag, session, -1,
1541 &req->r_caps_reservation);
1548 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1549 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1550 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1551 bool is_nokey = false;
1553 struct dentry *dn, *parent;
1554 struct fscrypt_str oname = FSTR_INIT(NULL, 0);
1555 struct ceph_fname fname = { .dir = dir,
1556 .name = rinfo->dname,
1557 .ctext = rinfo->altname,
1558 .name_len = rinfo->dname_len,
1559 .ctext_len = rinfo->altname_len };
1561 BUG_ON(!rinfo->head->is_target);
1562 BUG_ON(req->r_dentry);
1564 parent = d_find_any_alias(dir);
1567 err = ceph_fname_alloc_buffer(dir, &oname);
1573 err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey);
1576 ceph_fname_free_buffer(dir, &oname);
1579 dname.name = oname.name;
1580 dname.len = oname.len;
1581 dname.hash = full_name_hash(parent, dname.name, dname.len);
1582 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1583 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1585 dn = d_lookup(parent, &dname);
1586 doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n",
1587 parent, dname.len, dname.name, dn);
1590 dn = d_alloc(parent, &dname);
1591 doutc(cl, "d_alloc %p '%.*s' = %p\n", parent,
1592 dname.len, dname.name, dn);
1595 ceph_fname_free_buffer(dir, &oname);
1600 spin_lock(&dn->d_lock);
1601 dn->d_flags |= DCACHE_NOKEY_NAME;
1602 spin_unlock(&dn->d_lock);
1605 } else if (d_really_is_positive(dn) &&
1606 (ceph_ino(d_inode(dn)) != tvino.ino ||
1607 ceph_snap(d_inode(dn)) != tvino.snap)) {
1608 doutc(cl, " dn %p points to wrong inode %p\n",
1610 ceph_dir_clear_ordered(dir);
1615 ceph_fname_free_buffer(dir, &oname);
1622 if (rinfo->head->is_target) {
1623 /* Should be filled in by handle_reply */
1624 BUG_ON(!req->r_target_inode);
1626 in = req->r_target_inode;
1627 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1629 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1630 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1631 rinfo->head->result == 0) ? req->r_fmode : -1,
1632 &req->r_caps_reservation);
1634 pr_err_client(cl, "badness %p %llx.%llx\n", in,
1636 req->r_target_inode = NULL;
1637 if (in->i_state & I_NEW)
1638 discard_new_inode(in);
1643 if (in->i_state & I_NEW)
1644 unlock_new_inode(in);
1648 * ignore null lease/binding on snapdir ENOENT, or else we
1649 * will have trouble splicing in the virtual snapdir later
1651 if (rinfo->head->is_dentry &&
1652 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1653 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1654 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1655 fsc->mount_options->snapdir_name,
1656 req->r_dentry->d_name.len))) {
1658 * lookup link rename : null -> possibly existing inode
1659 * mknod symlink mkdir : null -> new inode
1660 * unlink : linked -> null
1662 struct inode *dir = req->r_parent;
1663 struct dentry *dn = req->r_dentry;
1664 bool have_dir_cap, have_lease;
1668 BUG_ON(d_inode(dn->d_parent) != dir);
1670 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1671 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1673 BUG_ON(ceph_ino(dir) != dvino.ino);
1674 BUG_ON(ceph_snap(dir) != dvino.snap);
1676 /* do we have a lease on the whole dir? */
1678 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1679 CEPH_CAP_FILE_SHARED);
1681 /* do we have a dn lease? */
1682 have_lease = have_dir_cap ||
1683 le32_to_cpu(rinfo->dlease->duration_ms);
1685 doutc(cl, "no dentry lease or dir cap\n");
1688 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1689 struct inode *olddir = req->r_old_dentry_dir;
1692 doutc(cl, " src %p '%pd' dst %p '%pd'\n",
1693 req->r_old_dentry, req->r_old_dentry, dn, dn);
1694 doutc(cl, "doing d_move %p -> %p\n", req->r_old_dentry, dn);
1696 /* d_move screws up sibling dentries' offsets */
1697 ceph_dir_clear_ordered(dir);
1698 ceph_dir_clear_ordered(olddir);
1700 d_move(req->r_old_dentry, dn);
1701 doutc(cl, " src %p '%pd' dst %p '%pd'\n",
1702 req->r_old_dentry, req->r_old_dentry, dn, dn);
1704 /* ensure target dentry is invalidated, despite
1705 rehashing bug in vfs_rename_dir */
1706 ceph_invalidate_dentry_lease(dn);
1708 doutc(cl, "dn %p gets new offset %lld\n",
1710 ceph_dentry(req->r_old_dentry)->offset);
1712 /* swap r_dentry and r_old_dentry in case that
1713 * splice_dentry() gets called later. This is safe
1714 * because no other place will use them */
1715 req->r_dentry = req->r_old_dentry;
1716 req->r_old_dentry = dn;
1721 if (!rinfo->head->is_target) {
1722 doutc(cl, "null dentry\n");
1723 if (d_really_is_positive(dn)) {
1724 doutc(cl, "d_delete %p\n", dn);
1725 ceph_dir_clear_ordered(dir);
1727 } else if (have_lease) {
1732 if (!d_unhashed(dn) && have_lease)
1733 update_dentry_lease(dir, dn,
1734 rinfo->dlease, session,
1735 req->r_request_started);
1739 /* attach proper inode */
1740 if (d_really_is_negative(dn)) {
1741 ceph_dir_clear_ordered(dir);
1743 err = splice_dentry(&req->r_dentry, in);
1746 dn = req->r_dentry; /* may have spliced */
1747 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1748 doutc(cl, " %p links to %p %llx.%llx, not %llx.%llx\n",
1749 dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1756 update_dentry_lease(dir, dn,
1757 rinfo->dlease, session,
1758 req->r_request_started);
1760 doutc(cl, " final dn %p\n", dn);
1761 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1762 req->r_op == CEPH_MDS_OP_MKSNAP) &&
1763 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1764 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1765 struct inode *dir = req->r_parent;
1767 /* fill out a snapdir LOOKUPSNAP dentry */
1769 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1770 BUG_ON(!req->r_dentry);
1771 doutc(cl, " linking snapped dir %p to dn %p\n", in,
1773 ceph_dir_clear_ordered(dir);
1775 err = splice_dentry(&req->r_dentry, in);
1778 } else if (rinfo->head->is_dentry && req->r_dentry) {
1779 /* parent inode is not locked, be carefull */
1780 struct ceph_vino *ptvino = NULL;
1781 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1782 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1783 if (rinfo->head->is_target) {
1784 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1785 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1788 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1789 session, req->r_request_started,
1790 rinfo->dname, rinfo->dname_len,
1794 doutc(cl, "done err=%d\n", err);
1799 * Prepopulate our cache with readdir results, leases, etc.
1801 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1802 struct ceph_mds_session *session)
1804 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1805 struct ceph_client *cl = session->s_mdsc->fsc->client;
1808 for (i = 0; i < rinfo->dir_nr; i++) {
1809 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1810 struct ceph_vino vino;
1814 vino.ino = le64_to_cpu(rde->inode.in->ino);
1815 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1817 in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL);
1820 doutc(cl, "badness got %d\n", err);
1823 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1824 -1, &req->r_caps_reservation);
1826 pr_err_client(cl, "inode badness on %p got %d\n", in,
1829 if (in->i_state & I_NEW) {
1831 discard_new_inode(in);
1833 } else if (in->i_state & I_NEW) {
1834 unlock_new_inode(in);
1843 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1847 put_page(ctl->page);
1852 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1853 struct ceph_readdir_cache_control *ctl,
1854 struct ceph_mds_request *req)
1856 struct ceph_client *cl = ceph_inode_to_client(dir);
1857 struct ceph_inode_info *ci = ceph_inode(dir);
1858 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1859 unsigned idx = ctl->index % nsize;
1860 pgoff_t pgoff = ctl->index / nsize;
1862 if (!ctl->page || pgoff != page_index(ctl->page)) {
1863 ceph_readdir_cache_release(ctl);
1865 ctl->page = grab_cache_page(&dir->i_data, pgoff);
1867 ctl->page = find_lock_page(&dir->i_data, pgoff);
1870 return idx == 0 ? -ENOMEM : 0;
1872 /* reading/filling the cache are serialized by
1873 * i_rwsem, no need to use page lock */
1874 unlock_page(ctl->page);
1875 ctl->dentries = kmap(ctl->page);
1877 memset(ctl->dentries, 0, PAGE_SIZE);
1880 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1881 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1882 doutc(cl, "dn %p idx %d\n", dn, ctl->index);
1883 ctl->dentries[idx] = dn;
1886 doutc(cl, "disable readdir cache\n");
1892 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1893 struct ceph_mds_session *session)
1895 struct dentry *parent = req->r_dentry;
1896 struct inode *inode = d_inode(parent);
1897 struct ceph_inode_info *ci = ceph_inode(inode);
1898 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1899 struct ceph_client *cl = session->s_mdsc->fsc->client;
1903 int err = 0, skipped = 0, ret, i;
1904 u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1907 struct ceph_readdir_cache_control cache_ctl = {};
1909 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1910 return readdir_prepopulate_inodes_only(req, session);
1912 if (rinfo->hash_order) {
1914 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1916 strlen(req->r_path2));
1917 last_hash = ceph_frag_value(last_hash);
1918 } else if (rinfo->offset_hash) {
1919 /* mds understands offset_hash */
1920 WARN_ON_ONCE(req->r_readdir_offset != 2);
1921 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
1925 if (rinfo->dir_dir &&
1926 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1927 doutc(cl, "got new frag %x -> %x\n", frag,
1928 le32_to_cpu(rinfo->dir_dir->frag));
1929 frag = le32_to_cpu(rinfo->dir_dir->frag);
1930 if (!rinfo->hash_order)
1931 req->r_readdir_offset = 2;
1934 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1935 doutc(cl, "%d items under SNAPDIR dn %p\n",
1936 rinfo->dir_nr, parent);
1938 doutc(cl, "%d items under dn %p\n", rinfo->dir_nr, parent);
1940 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1942 if (ceph_frag_is_leftmost(frag) &&
1943 req->r_readdir_offset == 2 &&
1944 !(rinfo->hash_order && last_hash)) {
1945 /* note dir version at start of readdir so we can
1946 * tell if any dentries get dropped */
1947 req->r_dir_release_cnt =
1948 atomic64_read(&ci->i_release_count);
1949 req->r_dir_ordered_cnt =
1950 atomic64_read(&ci->i_ordered_count);
1951 req->r_readdir_cache_idx = 0;
1955 cache_ctl.index = req->r_readdir_cache_idx;
1956 fpos_offset = req->r_readdir_offset;
1958 /* FIXME: release caps/leases if error occurs */
1959 for (i = 0; i < rinfo->dir_nr; i++) {
1960 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1961 struct ceph_vino tvino;
1963 dname.name = rde->name;
1964 dname.len = rde->name_len;
1965 dname.hash = full_name_hash(parent, dname.name, dname.len);
1967 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1968 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1970 if (rinfo->hash_order) {
1971 u32 hash = ceph_frag_value(rde->raw_hash);
1972 if (hash != last_hash)
1975 rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1977 rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1981 dn = d_lookup(parent, &dname);
1982 doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n",
1983 parent, dname.len, dname.name, dn);
1986 dn = d_alloc(parent, &dname);
1987 doutc(cl, "d_alloc %p '%.*s' = %p\n", parent,
1988 dname.len, dname.name, dn);
1990 doutc(cl, "d_alloc badness\n");
1994 if (rde->is_nokey) {
1995 spin_lock(&dn->d_lock);
1996 dn->d_flags |= DCACHE_NOKEY_NAME;
1997 spin_unlock(&dn->d_lock);
1999 } else if (d_really_is_positive(dn) &&
2000 (ceph_ino(d_inode(dn)) != tvino.ino ||
2001 ceph_snap(d_inode(dn)) != tvino.snap)) {
2002 struct ceph_dentry_info *di = ceph_dentry(dn);
2003 doutc(cl, " dn %p points to wrong inode %p\n",
2006 spin_lock(&dn->d_lock);
2007 if (di->offset > 0 &&
2008 di->lease_shared_gen ==
2009 atomic_read(&ci->i_shared_gen)) {
2010 __ceph_dir_clear_ordered(ci);
2013 spin_unlock(&dn->d_lock);
2021 if (d_really_is_positive(dn)) {
2024 in = ceph_get_inode(parent->d_sb, tvino, NULL);
2026 doutc(cl, "new_inode badness\n");
2034 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
2035 -1, &req->r_caps_reservation);
2037 pr_err_client(cl, "badness on %p %llx.%llx\n", in,
2039 if (d_really_is_negative(dn)) {
2040 if (in->i_state & I_NEW) {
2042 discard_new_inode(in);
2050 if (in->i_state & I_NEW)
2051 unlock_new_inode(in);
2053 if (d_really_is_negative(dn)) {
2054 if (ceph_security_xattr_deadlock(in)) {
2055 doutc(cl, " skip splicing dn %p to inode %p"
2056 " (security xattr deadlock)\n", dn, in);
2062 err = splice_dentry(&dn, in);
2067 ceph_dentry(dn)->offset = rde->offset;
2069 update_dentry_lease(d_inode(parent), dn,
2070 rde->lease, req->r_session,
2071 req->r_request_started);
2073 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
2074 ret = fill_readdir_cache(d_inode(parent), dn,
2083 if (err == 0 && skipped == 0) {
2084 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
2085 req->r_readdir_cache_idx = cache_ctl.index;
2087 ceph_readdir_cache_release(&cache_ctl);
2088 doutc(cl, "done\n");
2092 bool ceph_inode_set_size(struct inode *inode, loff_t size)
2094 struct ceph_client *cl = ceph_inode_to_client(inode);
2095 struct ceph_inode_info *ci = ceph_inode(inode);
2098 spin_lock(&ci->i_ceph_lock);
2099 doutc(cl, "set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
2100 i_size_write(inode, size);
2101 ceph_fscache_update(inode);
2102 inode->i_blocks = calc_inode_blocks(size);
2104 ret = __ceph_should_report_size(ci);
2106 spin_unlock(&ci->i_ceph_lock);
2111 void ceph_queue_inode_work(struct inode *inode, int work_bit)
2113 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2114 struct ceph_client *cl = fsc->client;
2115 struct ceph_inode_info *ci = ceph_inode(inode);
2116 set_bit(work_bit, &ci->i_work_mask);
2119 if (queue_work(fsc->inode_wq, &ci->i_work)) {
2120 doutc(cl, "%p %llx.%llx mask=%lx\n", inode,
2121 ceph_vinop(inode), ci->i_work_mask);
2123 doutc(cl, "%p %llx.%llx already queued, mask=%lx\n",
2124 inode, ceph_vinop(inode), ci->i_work_mask);
2129 static void ceph_do_invalidate_pages(struct inode *inode)
2131 struct ceph_client *cl = ceph_inode_to_client(inode);
2132 struct ceph_inode_info *ci = ceph_inode(inode);
2136 ceph_fscache_invalidate(inode, false);
2138 mutex_lock(&ci->i_truncate_mutex);
2140 if (ceph_inode_is_shutdown(inode)) {
2141 pr_warn_ratelimited_client(cl,
2142 "%p %llx.%llx is shut down\n", inode,
2144 mapping_set_error(inode->i_mapping, -EIO);
2145 truncate_pagecache(inode, 0);
2146 mutex_unlock(&ci->i_truncate_mutex);
2150 spin_lock(&ci->i_ceph_lock);
2151 doutc(cl, "%p %llx.%llx gen %d revoking %d\n", inode,
2152 ceph_vinop(inode), ci->i_rdcache_gen, ci->i_rdcache_revoking);
2153 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2154 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2156 spin_unlock(&ci->i_ceph_lock);
2157 mutex_unlock(&ci->i_truncate_mutex);
2160 orig_gen = ci->i_rdcache_gen;
2161 spin_unlock(&ci->i_ceph_lock);
2163 if (invalidate_inode_pages2(inode->i_mapping) < 0) {
2164 pr_err_client(cl, "invalidate_inode_pages2 %llx.%llx failed\n",
2168 spin_lock(&ci->i_ceph_lock);
2169 if (orig_gen == ci->i_rdcache_gen &&
2170 orig_gen == ci->i_rdcache_revoking) {
2171 doutc(cl, "%p %llx.%llx gen %d successful\n", inode,
2172 ceph_vinop(inode), ci->i_rdcache_gen);
2173 ci->i_rdcache_revoking--;
2176 doutc(cl, "%p %llx.%llx gen %d raced, now %d revoking %d\n",
2177 inode, ceph_vinop(inode), orig_gen, ci->i_rdcache_gen,
2178 ci->i_rdcache_revoking);
2179 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
2182 spin_unlock(&ci->i_ceph_lock);
2183 mutex_unlock(&ci->i_truncate_mutex);
2186 ceph_check_caps(ci, 0);
2190 * Make sure any pending truncation is applied before doing anything
2191 * that may depend on it.
2193 void __ceph_do_pending_vmtruncate(struct inode *inode)
2195 struct ceph_client *cl = ceph_inode_to_client(inode);
2196 struct ceph_inode_info *ci = ceph_inode(inode);
2198 int wrbuffer_refs, finish = 0;
2200 mutex_lock(&ci->i_truncate_mutex);
2202 spin_lock(&ci->i_ceph_lock);
2203 if (ci->i_truncate_pending == 0) {
2204 doutc(cl, "%p %llx.%llx none pending\n", inode,
2206 spin_unlock(&ci->i_ceph_lock);
2207 mutex_unlock(&ci->i_truncate_mutex);
2212 * make sure any dirty snapped pages are flushed before we
2213 * possibly truncate them.. so write AND block!
2215 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
2216 spin_unlock(&ci->i_ceph_lock);
2217 doutc(cl, "%p %llx.%llx flushing snaps first\n", inode,
2219 filemap_write_and_wait_range(&inode->i_data, 0,
2220 inode->i_sb->s_maxbytes);
2224 /* there should be no reader or writer */
2225 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
2227 to = ci->i_truncate_pagecache_size;
2228 wrbuffer_refs = ci->i_wrbuffer_ref;
2229 doutc(cl, "%p %llx.%llx (%d) to %lld\n", inode, ceph_vinop(inode),
2230 ci->i_truncate_pending, to);
2231 spin_unlock(&ci->i_ceph_lock);
2233 ceph_fscache_resize(inode, to);
2234 truncate_pagecache(inode, to);
2236 spin_lock(&ci->i_ceph_lock);
2237 if (to == ci->i_truncate_pagecache_size) {
2238 ci->i_truncate_pending = 0;
2241 spin_unlock(&ci->i_ceph_lock);
2245 mutex_unlock(&ci->i_truncate_mutex);
2247 if (wrbuffer_refs == 0)
2248 ceph_check_caps(ci, 0);
2250 wake_up_all(&ci->i_cap_wq);
2253 static void ceph_inode_work(struct work_struct *work)
2255 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
2257 struct inode *inode = &ci->netfs.inode;
2258 struct ceph_client *cl = ceph_inode_to_client(inode);
2260 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
2261 doutc(cl, "writeback %p %llx.%llx\n", inode, ceph_vinop(inode));
2262 filemap_fdatawrite(&inode->i_data);
2264 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
2265 ceph_do_invalidate_pages(inode);
2267 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
2268 __ceph_do_pending_vmtruncate(inode);
2270 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
2271 ceph_check_caps(ci, 0);
2273 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
2274 ceph_flush_snaps(ci, NULL);
2279 static const char *ceph_encrypted_get_link(struct dentry *dentry,
2280 struct inode *inode,
2281 struct delayed_call *done)
2283 struct ceph_inode_info *ci = ceph_inode(inode);
2286 return ERR_PTR(-ECHILD);
2288 return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
2292 static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap,
2293 const struct path *path,
2294 struct kstat *stat, u32 request_mask,
2295 unsigned int query_flags)
2299 ret = ceph_getattr(idmap, path, stat, request_mask, query_flags);
2302 return fscrypt_symlink_getattr(path, stat);
2308 static const struct inode_operations ceph_symlink_iops = {
2309 .get_link = simple_get_link,
2310 .setattr = ceph_setattr,
2311 .getattr = ceph_getattr,
2312 .listxattr = ceph_listxattr,
2315 static const struct inode_operations ceph_encrypted_symlink_iops = {
2316 .get_link = ceph_encrypted_get_link,
2317 .setattr = ceph_setattr,
2318 .getattr = ceph_encrypted_symlink_getattr,
2319 .listxattr = ceph_listxattr,
2323 * Transfer the encrypted last block to the MDS and the MDS
2324 * will help update it when truncating a smaller size.
2326 * We don't support a PAGE_SIZE that is smaller than the
2327 * CEPH_FSCRYPT_BLOCK_SIZE.
2329 static int fill_fscrypt_truncate(struct inode *inode,
2330 struct ceph_mds_request *req,
2333 struct ceph_client *cl = ceph_inode_to_client(inode);
2334 struct ceph_inode_info *ci = ceph_inode(inode);
2335 int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE;
2336 loff_t pos, orig_pos = round_down(attr->ia_size,
2337 CEPH_FSCRYPT_BLOCK_SIZE);
2338 u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT;
2339 struct ceph_pagelist *pagelist = NULL;
2340 struct kvec iov = {0};
2341 struct iov_iter iter;
2342 struct page *page = NULL;
2343 struct ceph_fscrypt_truncate_size_header header;
2345 int len = CEPH_FSCRYPT_BLOCK_SIZE;
2346 loff_t i_size = i_size_read(inode);
2347 int got, ret, issued;
2350 ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got);
2354 issued = __ceph_caps_issued(ci, NULL);
2356 doutc(cl, "size %lld -> %lld got cap refs on %s, issued %s\n",
2357 i_size, attr->ia_size, ceph_cap_string(got),
2358 ceph_cap_string(issued));
2360 /* Try to writeback the dirty pagecaches */
2361 if (issued & (CEPH_CAP_FILE_BUFFER)) {
2362 loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SHIFT - 1;
2364 ret = filemap_write_and_wait_range(inode->i_mapping,
2370 page = __page_cache_alloc(GFP_KERNEL);
2376 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
2382 iov.iov_base = kmap_local_page(page);
2384 iov_iter_kvec(&iter, READ, &iov, 1, len);
2387 ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver);
2391 /* Insert the header first */
2394 header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode));
2397 * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE,
2398 * because in MDS it may need this to do the truncate.
2400 header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE);
2403 * If we hit a hole here, we should just skip filling
2404 * the fscrypt for the request, because once the fscrypt
2405 * is enabled, the file will be split into many blocks
2406 * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there
2407 * has a hole, the hole size should be multiple of block
2410 * If the Rados object doesn't exist, it will be set to 0.
2413 doutc(cl, "hit hole, ppos %lld < size %lld\n", pos, i_size);
2415 header.data_len = cpu_to_le32(8 + 8 + 4);
2416 header.file_offset = 0;
2419 header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE);
2420 header.file_offset = cpu_to_le64(orig_pos);
2422 doutc(cl, "encrypt block boff/bsize %d/%lu\n", boff,
2423 CEPH_FSCRYPT_BLOCK_SIZE);
2425 /* truncate and zero out the extra contents for the last block */
2426 memset(iov.iov_base + boff, 0, PAGE_SIZE - boff);
2428 /* encrypt the last block */
2429 ret = ceph_fscrypt_encrypt_block_inplace(inode, page,
2430 CEPH_FSCRYPT_BLOCK_SIZE,
2437 /* Insert the header */
2438 ret = ceph_pagelist_append(pagelist, &header, sizeof(header));
2442 if (header.block_size) {
2443 /* Append the last block contents to pagelist */
2444 ret = ceph_pagelist_append(pagelist, iov.iov_base,
2445 CEPH_FSCRYPT_BLOCK_SIZE);
2449 req->r_pagelist = pagelist;
2451 doutc(cl, "%p %llx.%llx size dropping cap refs on %s\n", inode,
2452 ceph_vinop(inode), ceph_cap_string(got));
2453 ceph_put_cap_refs(ci, got);
2455 kunmap_local(iov.iov_base);
2457 __free_pages(page, 0);
2458 if (ret && pagelist)
2459 ceph_pagelist_release(pagelist);
2463 int __ceph_setattr(struct mnt_idmap *idmap, struct inode *inode,
2464 struct iattr *attr, struct ceph_iattr *cia)
2466 struct ceph_inode_info *ci = ceph_inode(inode);
2467 unsigned int ia_valid = attr->ia_valid;
2468 struct ceph_mds_request *req;
2469 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
2470 struct ceph_client *cl = ceph_inode_to_client(inode);
2471 struct ceph_cap_flush *prealloc_cf;
2472 loff_t isize = i_size_read(inode);
2474 int release = 0, dirtied = 0;
2477 int inode_dirty_flags = 0;
2478 bool lock_snap_rwsem = false;
2480 int truncate_retry = 20; /* The RMW will take around 50ms */
2483 prealloc_cf = ceph_alloc_cap_flush();
2487 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2490 ceph_free_cap_flush(prealloc_cf);
2491 return PTR_ERR(req);
2494 fill_fscrypt = false;
2495 spin_lock(&ci->i_ceph_lock);
2496 issued = __ceph_caps_issued(ci, NULL);
2498 if (!ci->i_head_snapc &&
2499 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2500 lock_snap_rwsem = true;
2501 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2502 spin_unlock(&ci->i_ceph_lock);
2503 down_read(&mdsc->snap_rwsem);
2504 spin_lock(&ci->i_ceph_lock);
2505 issued = __ceph_caps_issued(ci, NULL);
2509 doutc(cl, "%p %llx.%llx issued %s\n", inode, ceph_vinop(inode),
2510 ceph_cap_string(issued));
2511 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
2512 if (cia && cia->fscrypt_auth) {
2513 u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth);
2515 if (len > sizeof(*cia->fscrypt_auth)) {
2517 spin_unlock(&ci->i_ceph_lock);
2521 doutc(cl, "%p %llx.%llx fscrypt_auth len %u to %u)\n", inode,
2522 ceph_vinop(inode), ci->fscrypt_auth_len, len);
2524 /* It should never be re-set once set */
2525 WARN_ON_ONCE(ci->fscrypt_auth);
2527 if (issued & CEPH_CAP_AUTH_EXCL) {
2528 dirtied |= CEPH_CAP_AUTH_EXCL;
2529 kfree(ci->fscrypt_auth);
2530 ci->fscrypt_auth = (u8 *)cia->fscrypt_auth;
2531 ci->fscrypt_auth_len = len;
2532 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2533 ci->fscrypt_auth_len != len ||
2534 memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) {
2535 req->r_fscrypt_auth = cia->fscrypt_auth;
2536 mask |= CEPH_SETATTR_FSCRYPT_AUTH;
2537 release |= CEPH_CAP_AUTH_SHARED;
2539 cia->fscrypt_auth = NULL;
2542 if (cia && cia->fscrypt_auth) {
2544 spin_unlock(&ci->i_ceph_lock);
2547 #endif /* CONFIG_FS_ENCRYPTION */
2549 if (ia_valid & ATTR_UID) {
2550 kuid_t fsuid = from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid);
2552 doutc(cl, "%p %llx.%llx uid %d -> %d\n", inode,
2554 from_kuid(&init_user_ns, inode->i_uid),
2555 from_kuid(&init_user_ns, attr->ia_uid));
2556 if (issued & CEPH_CAP_AUTH_EXCL) {
2557 inode->i_uid = fsuid;
2558 dirtied |= CEPH_CAP_AUTH_EXCL;
2559 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2560 !uid_eq(fsuid, inode->i_uid)) {
2561 req->r_args.setattr.uid = cpu_to_le32(
2562 from_kuid(&init_user_ns, fsuid));
2563 mask |= CEPH_SETATTR_UID;
2564 release |= CEPH_CAP_AUTH_SHARED;
2567 if (ia_valid & ATTR_GID) {
2568 kgid_t fsgid = from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid);
2570 doutc(cl, "%p %llx.%llx gid %d -> %d\n", inode,
2572 from_kgid(&init_user_ns, inode->i_gid),
2573 from_kgid(&init_user_ns, attr->ia_gid));
2574 if (issued & CEPH_CAP_AUTH_EXCL) {
2575 inode->i_gid = fsgid;
2576 dirtied |= CEPH_CAP_AUTH_EXCL;
2577 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2578 !gid_eq(fsgid, inode->i_gid)) {
2579 req->r_args.setattr.gid = cpu_to_le32(
2580 from_kgid(&init_user_ns, fsgid));
2581 mask |= CEPH_SETATTR_GID;
2582 release |= CEPH_CAP_AUTH_SHARED;
2585 if (ia_valid & ATTR_MODE) {
2586 doutc(cl, "%p %llx.%llx mode 0%o -> 0%o\n", inode,
2587 ceph_vinop(inode), inode->i_mode, attr->ia_mode);
2588 if (issued & CEPH_CAP_AUTH_EXCL) {
2589 inode->i_mode = attr->ia_mode;
2590 dirtied |= CEPH_CAP_AUTH_EXCL;
2591 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2592 attr->ia_mode != inode->i_mode) {
2593 inode->i_mode = attr->ia_mode;
2594 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2595 mask |= CEPH_SETATTR_MODE;
2596 release |= CEPH_CAP_AUTH_SHARED;
2600 if (ia_valid & ATTR_ATIME) {
2601 struct timespec64 atime = inode_get_atime(inode);
2603 doutc(cl, "%p %llx.%llx atime %lld.%09ld -> %lld.%09ld\n",
2604 inode, ceph_vinop(inode),
2605 atime.tv_sec, atime.tv_nsec,
2606 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2607 if (issued & CEPH_CAP_FILE_EXCL) {
2608 ci->i_time_warp_seq++;
2609 inode_set_atime_to_ts(inode, attr->ia_atime);
2610 dirtied |= CEPH_CAP_FILE_EXCL;
2611 } else if ((issued & CEPH_CAP_FILE_WR) &&
2612 timespec64_compare(&atime,
2613 &attr->ia_atime) < 0) {
2614 inode_set_atime_to_ts(inode, attr->ia_atime);
2615 dirtied |= CEPH_CAP_FILE_WR;
2616 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2617 !timespec64_equal(&atime, &attr->ia_atime)) {
2618 ceph_encode_timespec64(&req->r_args.setattr.atime,
2620 mask |= CEPH_SETATTR_ATIME;
2621 release |= CEPH_CAP_FILE_SHARED |
2622 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2625 if (ia_valid & ATTR_SIZE) {
2626 doutc(cl, "%p %llx.%llx size %lld -> %lld\n", inode,
2627 ceph_vinop(inode), isize, attr->ia_size);
2629 * Only when the new size is smaller and not aligned to
2630 * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed.
2632 if (IS_ENCRYPTED(inode) && attr->ia_size < isize &&
2633 (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) {
2634 mask |= CEPH_SETATTR_SIZE;
2635 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2636 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2637 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2638 mask |= CEPH_SETATTR_FSCRYPT_FILE;
2639 req->r_args.setattr.size =
2640 cpu_to_le64(round_up(attr->ia_size,
2641 CEPH_FSCRYPT_BLOCK_SIZE));
2642 req->r_args.setattr.old_size =
2643 cpu_to_le64(round_up(isize,
2644 CEPH_FSCRYPT_BLOCK_SIZE));
2645 req->r_fscrypt_file = attr->ia_size;
2646 fill_fscrypt = true;
2647 } else if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
2648 if (attr->ia_size > isize) {
2649 i_size_write(inode, attr->ia_size);
2650 inode->i_blocks = calc_inode_blocks(attr->ia_size);
2651 ci->i_reported_size = attr->ia_size;
2652 dirtied |= CEPH_CAP_FILE_EXCL;
2653 ia_valid |= ATTR_MTIME;
2655 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2656 attr->ia_size != isize) {
2657 mask |= CEPH_SETATTR_SIZE;
2658 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2659 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2660 if (IS_ENCRYPTED(inode) && attr->ia_size) {
2661 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
2662 mask |= CEPH_SETATTR_FSCRYPT_FILE;
2663 req->r_args.setattr.size =
2664 cpu_to_le64(round_up(attr->ia_size,
2665 CEPH_FSCRYPT_BLOCK_SIZE));
2666 req->r_args.setattr.old_size =
2667 cpu_to_le64(round_up(isize,
2668 CEPH_FSCRYPT_BLOCK_SIZE));
2669 req->r_fscrypt_file = attr->ia_size;
2671 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2672 req->r_args.setattr.old_size = cpu_to_le64(isize);
2673 req->r_fscrypt_file = 0;
2677 if (ia_valid & ATTR_MTIME) {
2678 struct timespec64 mtime = inode_get_mtime(inode);
2680 doutc(cl, "%p %llx.%llx mtime %lld.%09ld -> %lld.%09ld\n",
2681 inode, ceph_vinop(inode),
2682 mtime.tv_sec, mtime.tv_nsec,
2683 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2684 if (issued & CEPH_CAP_FILE_EXCL) {
2685 ci->i_time_warp_seq++;
2686 inode_set_mtime_to_ts(inode, attr->ia_mtime);
2687 dirtied |= CEPH_CAP_FILE_EXCL;
2688 } else if ((issued & CEPH_CAP_FILE_WR) &&
2689 timespec64_compare(&mtime, &attr->ia_mtime) < 0) {
2690 inode_set_mtime_to_ts(inode, attr->ia_mtime);
2691 dirtied |= CEPH_CAP_FILE_WR;
2692 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2693 !timespec64_equal(&mtime, &attr->ia_mtime)) {
2694 ceph_encode_timespec64(&req->r_args.setattr.mtime,
2696 mask |= CEPH_SETATTR_MTIME;
2697 release |= CEPH_CAP_FILE_SHARED |
2698 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2702 /* these do nothing */
2703 if (ia_valid & ATTR_CTIME) {
2704 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2705 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2706 doutc(cl, "%p %llx.%llx ctime %lld.%09ld -> %lld.%09ld (%s)\n",
2707 inode, ceph_vinop(inode),
2708 inode_get_ctime_sec(inode),
2709 inode_get_ctime_nsec(inode),
2710 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2711 only ? "ctime only" : "ignored");
2714 * if kernel wants to dirty ctime but nothing else,
2715 * we need to choose a cap to dirty under, or do
2716 * a almost-no-op setattr
2718 if (issued & CEPH_CAP_AUTH_EXCL)
2719 dirtied |= CEPH_CAP_AUTH_EXCL;
2720 else if (issued & CEPH_CAP_FILE_EXCL)
2721 dirtied |= CEPH_CAP_FILE_EXCL;
2722 else if (issued & CEPH_CAP_XATTR_EXCL)
2723 dirtied |= CEPH_CAP_XATTR_EXCL;
2725 mask |= CEPH_SETATTR_CTIME;
2728 if (ia_valid & ATTR_FILE)
2729 doutc(cl, "%p %llx.%llx ATTR_FILE ... hrm!\n", inode,
2733 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2735 inode_set_ctime_to_ts(inode, attr->ia_ctime);
2736 inode_inc_iversion_raw(inode);
2740 spin_unlock(&ci->i_ceph_lock);
2741 if (lock_snap_rwsem) {
2742 up_read(&mdsc->snap_rwsem);
2743 lock_snap_rwsem = false;
2746 if (inode_dirty_flags)
2747 __mark_inode_dirty(inode, inode_dirty_flags);
2750 req->r_inode = inode;
2752 req->r_inode_drop = release;
2753 req->r_args.setattr.mask = cpu_to_le32(mask);
2754 req->r_num_caps = 1;
2755 req->r_stamp = attr->ia_ctime;
2757 err = fill_fscrypt_truncate(inode, req, attr);
2763 * The truncate request will return -EAGAIN when the
2764 * last block has been updated just before the MDS
2765 * successfully gets the xlock for the FILE lock. To
2766 * avoid corrupting the file contents we need to retry
2769 err = ceph_mdsc_do_request(mdsc, NULL, req);
2770 if (err == -EAGAIN && truncate_retry--) {
2771 doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote), retry it!\n",
2772 inode, ceph_vinop(inode), err,
2773 ceph_cap_string(dirtied), mask);
2774 ceph_mdsc_put_request(req);
2775 ceph_free_cap_flush(prealloc_cf);
2780 doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote)\n", inode,
2781 ceph_vinop(inode), err, ceph_cap_string(dirtied), mask);
2783 ceph_mdsc_put_request(req);
2784 ceph_free_cap_flush(prealloc_cf);
2786 if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2787 __ceph_do_pending_vmtruncate(inode);
2795 int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2798 struct inode *inode = d_inode(dentry);
2799 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2802 if (ceph_snap(inode) != CEPH_NOSNAP)
2805 if (ceph_inode_is_shutdown(inode))
2808 err = fscrypt_prepare_setattr(dentry, attr);
2812 err = setattr_prepare(idmap, dentry, attr);
2816 if ((attr->ia_valid & ATTR_SIZE) &&
2817 attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
2820 if ((attr->ia_valid & ATTR_SIZE) &&
2821 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2824 err = __ceph_setattr(idmap, inode, attr, NULL);
2826 if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2827 err = posix_acl_chmod(idmap, dentry, attr->ia_mode);
2832 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
2834 int issued = ceph_caps_issued(ceph_inode(inode));
2837 * If any 'x' caps is issued we can just choose the auth MDS
2838 * instead of the random replica MDSes. Because only when the
2839 * Locker is in LOCK_EXEC state will the loner client could
2840 * get the 'x' caps. And if we send the getattr requests to
2841 * any replica MDS it must auth pin and tries to rdlock from
2842 * the auth MDS, and then the auth MDS need to do the Locker
2843 * state transition to LOCK_SYNC. And after that the lock state
2846 * This cost much when doing the Locker state transition and
2847 * usually will need to revoke caps from clients.
2849 * And for the 'Xs' caps for getxattr we will also choose the
2850 * auth MDS, because the MDS side code is buggy due to setxattr
2851 * won't notify the replica MDSes when the values changed and
2852 * the replica MDS will return the old values. Though we will
2853 * fix it in MDS code, but this still makes sense for old ceph.
2855 if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
2856 || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
2857 return USE_AUTH_MDS;
2863 * Verify that we have a lease on the given mask. If not,
2864 * do a getattr against an mds.
2866 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2867 int mask, bool force)
2869 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
2870 struct ceph_client *cl = fsc->client;
2871 struct ceph_mds_client *mdsc = fsc->mdsc;
2872 struct ceph_mds_request *req;
2876 if (ceph_snap(inode) == CEPH_SNAPDIR) {
2877 doutc(cl, "inode %p %llx.%llx SNAPDIR\n", inode,
2882 doutc(cl, "inode %p %llx.%llx mask %s mode 0%o\n", inode,
2883 ceph_vinop(inode), ceph_cap_string(mask), inode->i_mode);
2884 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2887 mode = ceph_try_to_choose_auth_mds(inode, mask);
2888 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2890 return PTR_ERR(req);
2891 req->r_inode = inode;
2893 req->r_num_caps = 1;
2894 req->r_args.getattr.mask = cpu_to_le32(mask);
2895 req->r_locked_page = locked_page;
2896 err = ceph_mdsc_do_request(mdsc, NULL, req);
2897 if (locked_page && err == 0) {
2898 u64 inline_version = req->r_reply_info.targeti.inline_version;
2899 if (inline_version == 0) {
2900 /* the reply is supposed to contain inline data */
2902 } else if (inline_version == CEPH_INLINE_NONE ||
2903 inline_version == 1) {
2906 err = req->r_reply_info.targeti.inline_len;
2909 ceph_mdsc_put_request(req);
2910 doutc(cl, "result=%d\n", err);
2914 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
2917 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
2918 struct ceph_client *cl = fsc->client;
2919 struct ceph_mds_client *mdsc = fsc->mdsc;
2920 struct ceph_mds_request *req;
2921 int mode = USE_AUTH_MDS;
2924 size_t xattr_value_len;
2926 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
2932 req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR;
2933 req->r_path2 = kstrdup(name, GFP_NOFS);
2934 if (!req->r_path2) {
2940 req->r_inode = inode;
2941 err = ceph_mdsc_do_request(mdsc, NULL, req);
2945 xattr_value = req->r_reply_info.xattr_info.xattr_value;
2946 xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
2948 doutc(cl, "xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
2950 err = (int)xattr_value_len;
2954 if (xattr_value_len > size) {
2959 memcpy(value, xattr_value, xattr_value_len);
2961 ceph_mdsc_put_request(req);
2963 doutc(cl, "result=%d\n", err);
2969 * Check inode permissions. We verify we have a valid value for
2970 * the AUTH cap, then call the generic handler.
2972 int ceph_permission(struct mnt_idmap *idmap, struct inode *inode,
2977 if (mask & MAY_NOT_BLOCK)
2980 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2983 err = generic_permission(idmap, inode, mask);
2987 /* Craft a mask of needed caps given a set of requested statx attrs. */
2988 static int statx_to_caps(u32 want, umode_t mode)
2992 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE))
2993 mask |= CEPH_CAP_AUTH_SHARED;
2995 if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) {
2997 * The link count for directories depends on inode->i_subdirs,
2998 * and that is only updated when Fs caps are held.
3001 mask |= CEPH_CAP_FILE_SHARED;
3003 mask |= CEPH_CAP_LINK_SHARED;
3006 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE))
3007 mask |= CEPH_CAP_FILE_SHARED;
3009 if (want & (STATX_CTIME|STATX_CHANGE_COOKIE))
3010 mask |= CEPH_CAP_XATTR_SHARED;
3016 * Get all the attributes. If we have sufficient caps for the requested attrs,
3017 * then we can avoid talking to the MDS at all.
3019 int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
3020 struct kstat *stat, u32 request_mask, unsigned int flags)
3022 struct inode *inode = d_inode(path->dentry);
3023 struct super_block *sb = inode->i_sb;
3024 struct ceph_inode_info *ci = ceph_inode(inode);
3025 u32 valid_mask = STATX_BASIC_STATS;
3028 if (ceph_inode_is_shutdown(inode))
3031 /* Skip the getattr altogether if we're asked not to sync */
3032 if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
3033 err = ceph_do_getattr(inode,
3034 statx_to_caps(request_mask, inode->i_mode),
3035 flags & AT_STATX_FORCE_SYNC);
3040 generic_fillattr(idmap, request_mask, inode, stat);
3041 stat->ino = ceph_present_inode(inode);
3044 * btime on newly-allocated inodes is 0, so if this is still set to
3045 * that, then assume that it's not valid.
3047 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
3048 stat->btime = ci->i_btime;
3049 valid_mask |= STATX_BTIME;
3052 if (request_mask & STATX_CHANGE_COOKIE) {
3053 stat->change_cookie = inode_peek_iversion_raw(inode);
3054 valid_mask |= STATX_CHANGE_COOKIE;
3057 if (ceph_snap(inode) == CEPH_NOSNAP)
3058 stat->dev = sb->s_dev;
3060 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
3062 if (S_ISDIR(inode->i_mode)) {
3063 if (ceph_test_mount_opt(ceph_sb_to_fs_client(sb), RBYTES)) {
3064 stat->size = ci->i_rbytes;
3065 } else if (ceph_snap(inode) == CEPH_SNAPDIR) {
3066 struct ceph_inode_info *pci;
3067 struct ceph_snap_realm *realm;
3068 struct inode *parent;
3070 parent = ceph_lookup_inode(sb, ceph_ino(inode));
3072 return PTR_ERR(parent);
3074 pci = ceph_inode(parent);
3075 spin_lock(&pci->i_ceph_lock);
3076 realm = pci->i_snap_realm;
3078 stat->size = realm->num_snaps;
3081 spin_unlock(&pci->i_ceph_lock);
3084 stat->size = ci->i_files + ci->i_subdirs;
3087 stat->blksize = 65536;
3089 * Some applications rely on the number of st_nlink
3090 * value on directories to be either 0 (if unlinked)
3091 * or 2 + number of subdirectories.
3093 if (stat->nlink == 1)
3094 /* '.' + '..' + subdirs */
3095 stat->nlink = 1 + 1 + ci->i_subdirs;
3098 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
3099 if (IS_ENCRYPTED(inode))
3100 stat->attributes |= STATX_ATTR_ENCRYPTED;
3101 stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC |
3102 STATX_ATTR_ENCRYPTED);
3104 stat->result_mask = request_mask & valid_mask;
3108 void ceph_inode_shutdown(struct inode *inode)
3110 struct ceph_inode_info *ci = ceph_inode(inode);
3113 bool invalidate = false;
3115 spin_lock(&ci->i_ceph_lock);
3116 ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
3117 p = rb_first(&ci->i_caps);
3119 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
3122 iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
3124 spin_unlock(&ci->i_ceph_lock);
3127 ceph_queue_invalidate(inode);