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>
19 #include "mds_client.h"
21 #include <linux/ceph/decode.h>
24 * Ceph inode operations
26 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
27 * setattr, etc.), xattr helpers, and helpers for assimilating
28 * metadata returned by the MDS into our cache.
30 * Also define helpers for doing asynchronous writeback, invalidation,
31 * and truncation for the benefit of those who can't afford to block
32 * (typically because they are in the message handler path).
35 static const struct inode_operations ceph_symlink_iops;
37 static void ceph_inode_work(struct work_struct *work);
40 * find or create an inode, given the ceph ino number
42 static int ceph_set_ino_cb(struct inode *inode, void *data)
44 struct ceph_inode_info *ci = ceph_inode(inode);
45 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
47 ci->i_vino = *(struct ceph_vino *)data;
48 inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
49 inode_set_iversion_raw(inode, 0);
50 percpu_counter_inc(&mdsc->metric.total_inodes);
55 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
59 inode = iget5_locked(sb, (unsigned long)vino.ino, ceph_ino_compare,
60 ceph_set_ino_cb, &vino);
62 return ERR_PTR(-ENOMEM);
64 dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode),
65 ceph_vinop(inode), inode, !!(inode->i_state & I_NEW));
70 * get/constuct snapdir inode for a given directory
72 struct inode *ceph_get_snapdir(struct inode *parent)
74 struct ceph_vino vino = {
75 .ino = ceph_ino(parent),
78 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
79 struct ceph_inode_info *ci = ceph_inode(inode);
84 if (!S_ISDIR(parent->i_mode)) {
85 pr_warn_once("bad snapdir parent type (mode=0%o)\n",
87 return ERR_PTR(-ENOTDIR);
90 if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
91 pr_warn_once("bad snapdir inode type (mode=0%o)\n",
93 return ERR_PTR(-ENOTDIR);
96 inode->i_mode = parent->i_mode;
97 inode->i_uid = parent->i_uid;
98 inode->i_gid = parent->i_gid;
99 inode->i_mtime = parent->i_mtime;
100 inode->i_ctime = parent->i_ctime;
101 inode->i_atime = parent->i_atime;
102 inode->i_op = &ceph_snapdir_iops;
103 inode->i_fop = &ceph_snapdir_fops;
104 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
106 ci->i_btime = ceph_inode(parent)->i_btime;
108 if (inode->i_state & I_NEW)
109 unlock_new_inode(inode);
114 const struct inode_operations ceph_file_iops = {
115 .permission = ceph_permission,
116 .setattr = ceph_setattr,
117 .getattr = ceph_getattr,
118 .listxattr = ceph_listxattr,
119 .get_acl = ceph_get_acl,
120 .set_acl = ceph_set_acl,
125 * We use a 'frag tree' to keep track of the MDS's directory fragments
126 * for a given inode (usually there is just a single fragment). We
127 * need to know when a child frag is delegated to a new MDS, or when
128 * it is flagged as replicated, so we can direct our requests
133 * find/create a frag in the tree
135 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
139 struct rb_node *parent = NULL;
140 struct ceph_inode_frag *frag;
143 p = &ci->i_fragtree.rb_node;
146 frag = rb_entry(parent, struct ceph_inode_frag, node);
147 c = ceph_frag_compare(f, frag->frag);
156 frag = kmalloc(sizeof(*frag), GFP_NOFS);
158 return ERR_PTR(-ENOMEM);
165 rb_link_node(&frag->node, parent, p);
166 rb_insert_color(&frag->node, &ci->i_fragtree);
168 dout("get_or_create_frag added %llx.%llx frag %x\n",
169 ceph_vinop(&ci->vfs_inode), f);
174 * find a specific frag @f
176 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
178 struct rb_node *n = ci->i_fragtree.rb_node;
181 struct ceph_inode_frag *frag =
182 rb_entry(n, struct ceph_inode_frag, node);
183 int c = ceph_frag_compare(f, frag->frag);
195 * Choose frag containing the given value @v. If @pfrag is
196 * specified, copy the frag delegation info to the caller if
199 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
200 struct ceph_inode_frag *pfrag, int *found)
202 u32 t = ceph_frag_make(0, 0);
203 struct ceph_inode_frag *frag;
211 WARN_ON(!ceph_frag_contains_value(t, v));
212 frag = __ceph_find_frag(ci, t);
214 break; /* t is a leaf */
215 if (frag->split_by == 0) {
217 memcpy(pfrag, frag, sizeof(*pfrag));
224 nway = 1 << frag->split_by;
225 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
226 frag->split_by, nway);
227 for (i = 0; i < nway; i++) {
228 n = ceph_frag_make_child(t, frag->split_by, i);
229 if (ceph_frag_contains_value(n, v)) {
236 dout("choose_frag(%x) = %x\n", v, t);
241 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
242 struct ceph_inode_frag *pfrag, int *found)
245 mutex_lock(&ci->i_fragtree_mutex);
246 ret = __ceph_choose_frag(ci, v, pfrag, found);
247 mutex_unlock(&ci->i_fragtree_mutex);
252 * Process dirfrag (delegation) info from the mds. Include leaf
253 * fragment in tree ONLY if ndist > 0. Otherwise, only
254 * branches/splits are included in i_fragtree)
256 static int ceph_fill_dirfrag(struct inode *inode,
257 struct ceph_mds_reply_dirfrag *dirinfo)
259 struct ceph_inode_info *ci = ceph_inode(inode);
260 struct ceph_inode_frag *frag;
261 u32 id = le32_to_cpu(dirinfo->frag);
262 int mds = le32_to_cpu(dirinfo->auth);
263 int ndist = le32_to_cpu(dirinfo->ndist);
268 spin_lock(&ci->i_ceph_lock);
270 diri_auth = ci->i_auth_cap->mds;
271 spin_unlock(&ci->i_ceph_lock);
273 if (mds == -1) /* CDIR_AUTH_PARENT */
276 mutex_lock(&ci->i_fragtree_mutex);
277 if (ndist == 0 && mds == diri_auth) {
278 /* no delegation info needed. */
279 frag = __ceph_find_frag(ci, id);
282 if (frag->split_by == 0) {
283 /* tree leaf, remove */
284 dout("fill_dirfrag removed %llx.%llx frag %x"
285 " (no ref)\n", ceph_vinop(inode), id);
286 rb_erase(&frag->node, &ci->i_fragtree);
289 /* tree branch, keep and clear */
290 dout("fill_dirfrag cleared %llx.%llx frag %x"
291 " referral\n", ceph_vinop(inode), id);
299 /* find/add this frag to store mds delegation info */
300 frag = __get_or_create_frag(ci, id);
302 /* this is not the end of the world; we can continue
303 with bad/inaccurate delegation info */
304 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
305 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
311 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
312 for (i = 0; i < frag->ndist; i++)
313 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
314 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
315 ceph_vinop(inode), frag->frag, frag->ndist);
318 mutex_unlock(&ci->i_fragtree_mutex);
322 static int frag_tree_split_cmp(const void *l, const void *r)
324 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
325 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
326 return ceph_frag_compare(le32_to_cpu(ls->frag),
327 le32_to_cpu(rs->frag));
330 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
333 return f == ceph_frag_make(0, 0);
334 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
336 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
339 static int ceph_fill_fragtree(struct inode *inode,
340 struct ceph_frag_tree_head *fragtree,
341 struct ceph_mds_reply_dirfrag *dirinfo)
343 struct ceph_inode_info *ci = ceph_inode(inode);
344 struct ceph_inode_frag *frag, *prev_frag = NULL;
345 struct rb_node *rb_node;
346 unsigned i, split_by, nsplits;
350 mutex_lock(&ci->i_fragtree_mutex);
351 nsplits = le32_to_cpu(fragtree->nsplits);
352 if (nsplits != ci->i_fragtree_nsplits) {
354 } else if (nsplits) {
355 i = prandom_u32() % nsplits;
356 id = le32_to_cpu(fragtree->splits[i].frag);
357 if (!__ceph_find_frag(ci, id))
359 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
360 rb_node = rb_first(&ci->i_fragtree);
361 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
362 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
365 if (!update && dirinfo) {
366 id = le32_to_cpu(dirinfo->frag);
367 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
374 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
375 frag_tree_split_cmp, NULL);
378 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
379 rb_node = rb_first(&ci->i_fragtree);
380 for (i = 0; i < nsplits; i++) {
381 id = le32_to_cpu(fragtree->splits[i].frag);
382 split_by = le32_to_cpu(fragtree->splits[i].by);
383 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
384 pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
385 "frag %x split by %d\n", ceph_vinop(inode),
386 i, nsplits, id, split_by);
391 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
392 if (ceph_frag_compare(frag->frag, id) >= 0) {
393 if (frag->frag != id)
396 rb_node = rb_next(rb_node);
399 rb_node = rb_next(rb_node);
400 /* delete stale split/leaf node */
401 if (frag->split_by > 0 ||
402 !is_frag_child(frag->frag, prev_frag)) {
403 rb_erase(&frag->node, &ci->i_fragtree);
404 if (frag->split_by > 0)
405 ci->i_fragtree_nsplits--;
411 frag = __get_or_create_frag(ci, id);
415 if (frag->split_by == 0)
416 ci->i_fragtree_nsplits++;
417 frag->split_by = split_by;
418 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
422 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
423 rb_node = rb_next(rb_node);
424 /* delete stale split/leaf node */
425 if (frag->split_by > 0 ||
426 !is_frag_child(frag->frag, prev_frag)) {
427 rb_erase(&frag->node, &ci->i_fragtree);
428 if (frag->split_by > 0)
429 ci->i_fragtree_nsplits--;
434 mutex_unlock(&ci->i_fragtree_mutex);
439 * initialize a newly allocated inode.
441 struct inode *ceph_alloc_inode(struct super_block *sb)
443 struct ceph_inode_info *ci;
446 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
450 dout("alloc_inode %p\n", &ci->vfs_inode);
452 spin_lock_init(&ci->i_ceph_lock);
455 ci->i_inline_version = 0;
456 ci->i_time_warp_seq = 0;
457 ci->i_ceph_flags = 0;
458 atomic64_set(&ci->i_ordered_count, 1);
459 atomic64_set(&ci->i_release_count, 1);
460 atomic64_set(&ci->i_complete_seq[0], 0);
461 atomic64_set(&ci->i_complete_seq[1], 0);
462 ci->i_symlink = NULL;
467 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
468 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
469 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
471 ci->i_fragtree = RB_ROOT;
472 mutex_init(&ci->i_fragtree_mutex);
474 ci->i_xattrs.blob = NULL;
475 ci->i_xattrs.prealloc_blob = NULL;
476 ci->i_xattrs.dirty = false;
477 ci->i_xattrs.index = RB_ROOT;
478 ci->i_xattrs.count = 0;
479 ci->i_xattrs.names_size = 0;
480 ci->i_xattrs.vals_size = 0;
481 ci->i_xattrs.version = 0;
482 ci->i_xattrs.index_version = 0;
484 ci->i_caps = RB_ROOT;
485 ci->i_auth_cap = NULL;
486 ci->i_dirty_caps = 0;
487 ci->i_flushing_caps = 0;
488 INIT_LIST_HEAD(&ci->i_dirty_item);
489 INIT_LIST_HEAD(&ci->i_flushing_item);
490 ci->i_prealloc_cap_flush = NULL;
491 INIT_LIST_HEAD(&ci->i_cap_flush_list);
492 init_waitqueue_head(&ci->i_cap_wq);
493 ci->i_hold_caps_max = 0;
494 INIT_LIST_HEAD(&ci->i_cap_delay_list);
495 INIT_LIST_HEAD(&ci->i_cap_snaps);
496 ci->i_head_snapc = NULL;
499 ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
500 for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
501 ci->i_nr_by_mode[i] = 0;
503 mutex_init(&ci->i_truncate_mutex);
504 ci->i_truncate_seq = 0;
505 ci->i_truncate_size = 0;
506 ci->i_truncate_pending = 0;
509 ci->i_reported_size = 0;
510 ci->i_wanted_max_size = 0;
511 ci->i_requested_max_size = 0;
515 ci->i_rdcache_ref = 0;
519 ci->i_wrbuffer_ref = 0;
520 ci->i_wrbuffer_ref_head = 0;
521 atomic_set(&ci->i_filelock_ref, 0);
522 atomic_set(&ci->i_shared_gen, 1);
523 ci->i_rdcache_gen = 0;
524 ci->i_rdcache_revoking = 0;
526 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
527 INIT_LIST_HEAD(&ci->i_unsafe_iops);
528 spin_lock_init(&ci->i_unsafe_lock);
530 ci->i_snap_realm = NULL;
531 INIT_LIST_HEAD(&ci->i_snap_realm_item);
532 INIT_LIST_HEAD(&ci->i_snap_flush_item);
534 INIT_WORK(&ci->i_work, ceph_inode_work);
536 memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
538 ceph_fscache_inode_init(ci);
542 return &ci->vfs_inode;
545 void ceph_free_inode(struct inode *inode)
547 struct ceph_inode_info *ci = ceph_inode(inode);
549 kfree(ci->i_symlink);
550 kmem_cache_free(ceph_inode_cachep, ci);
553 void ceph_evict_inode(struct inode *inode)
555 struct ceph_inode_info *ci = ceph_inode(inode);
556 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
557 struct ceph_inode_frag *frag;
560 dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
562 percpu_counter_dec(&mdsc->metric.total_inodes);
564 truncate_inode_pages_final(&inode->i_data);
567 ceph_fscache_unregister_inode_cookie(ci);
569 __ceph_remove_caps(ci);
571 if (__ceph_has_any_quota(ci))
572 ceph_adjust_quota_realms_count(inode, false);
575 * we may still have a snap_realm reference if there are stray
576 * caps in i_snap_caps.
578 if (ci->i_snap_realm) {
579 if (ceph_snap(inode) == CEPH_NOSNAP) {
580 struct ceph_snap_realm *realm = ci->i_snap_realm;
581 dout(" dropping residual ref to snap realm %p\n",
583 spin_lock(&realm->inodes_with_caps_lock);
584 list_del_init(&ci->i_snap_realm_item);
585 ci->i_snap_realm = NULL;
586 if (realm->ino == ci->i_vino.ino)
588 spin_unlock(&realm->inodes_with_caps_lock);
589 ceph_put_snap_realm(mdsc, realm);
591 ceph_put_snapid_map(mdsc, ci->i_snapid_map);
592 ci->i_snap_realm = NULL;
596 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
597 frag = rb_entry(n, struct ceph_inode_frag, node);
598 rb_erase(n, &ci->i_fragtree);
601 ci->i_fragtree_nsplits = 0;
603 __ceph_destroy_xattrs(ci);
604 if (ci->i_xattrs.blob)
605 ceph_buffer_put(ci->i_xattrs.blob);
606 if (ci->i_xattrs.prealloc_blob)
607 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
609 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
610 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
613 static inline blkcnt_t calc_inode_blocks(u64 size)
615 return (size + (1<<9) - 1) >> 9;
619 * Helpers to fill in size, ctime, mtime, and atime. We have to be
620 * careful because either the client or MDS may have more up to date
621 * info, depending on which capabilities are held, and whether
622 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
623 * and size are monotonically increasing, except when utimes() or
624 * truncate() increments the corresponding _seq values.)
626 int ceph_fill_file_size(struct inode *inode, int issued,
627 u32 truncate_seq, u64 truncate_size, u64 size)
629 struct ceph_inode_info *ci = ceph_inode(inode);
632 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
633 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
634 dout("size %lld -> %llu\n", inode->i_size, size);
635 if (size > 0 && S_ISDIR(inode->i_mode)) {
636 pr_err("fill_file_size non-zero size for directory\n");
639 i_size_write(inode, size);
640 inode->i_blocks = calc_inode_blocks(size);
641 ci->i_reported_size = size;
642 if (truncate_seq != ci->i_truncate_seq) {
643 dout("truncate_seq %u -> %u\n",
644 ci->i_truncate_seq, truncate_seq);
645 ci->i_truncate_seq = truncate_seq;
647 /* the MDS should have revoked these caps */
648 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
651 CEPH_CAP_FILE_LAZYIO));
653 * If we hold relevant caps, or in the case where we're
654 * not the only client referencing this file and we
655 * don't hold those caps, then we need to check whether
656 * the file is either opened or mmaped
658 if ((issued & (CEPH_CAP_FILE_CACHE|
659 CEPH_CAP_FILE_BUFFER)) ||
660 mapping_mapped(inode->i_mapping) ||
661 __ceph_is_file_opened(ci)) {
662 ci->i_truncate_pending++;
667 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
668 ci->i_truncate_size != truncate_size) {
669 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
671 ci->i_truncate_size = truncate_size;
675 ceph_fscache_invalidate(inode);
680 void ceph_fill_file_time(struct inode *inode, int issued,
681 u64 time_warp_seq, struct timespec64 *ctime,
682 struct timespec64 *mtime, struct timespec64 *atime)
684 struct ceph_inode_info *ci = ceph_inode(inode);
687 if (issued & (CEPH_CAP_FILE_EXCL|
689 CEPH_CAP_FILE_BUFFER|
691 CEPH_CAP_XATTR_EXCL)) {
692 if (ci->i_version == 0 ||
693 timespec64_compare(ctime, &inode->i_ctime) > 0) {
694 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
695 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
696 ctime->tv_sec, ctime->tv_nsec);
697 inode->i_ctime = *ctime;
699 if (ci->i_version == 0 ||
700 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
701 /* the MDS did a utimes() */
702 dout("mtime %lld.%09ld -> %lld.%09ld "
704 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
705 mtime->tv_sec, mtime->tv_nsec,
706 ci->i_time_warp_seq, (int)time_warp_seq);
708 inode->i_mtime = *mtime;
709 inode->i_atime = *atime;
710 ci->i_time_warp_seq = time_warp_seq;
711 } else if (time_warp_seq == ci->i_time_warp_seq) {
712 /* nobody did utimes(); take the max */
713 if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
714 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
715 inode->i_mtime.tv_sec,
716 inode->i_mtime.tv_nsec,
717 mtime->tv_sec, mtime->tv_nsec);
718 inode->i_mtime = *mtime;
720 if (timespec64_compare(atime, &inode->i_atime) > 0) {
721 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
722 inode->i_atime.tv_sec,
723 inode->i_atime.tv_nsec,
724 atime->tv_sec, atime->tv_nsec);
725 inode->i_atime = *atime;
727 } else if (issued & CEPH_CAP_FILE_EXCL) {
728 /* we did a utimes(); ignore mds values */
733 /* we have no write|excl caps; whatever the MDS says is true */
734 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
735 inode->i_ctime = *ctime;
736 inode->i_mtime = *mtime;
737 inode->i_atime = *atime;
738 ci->i_time_warp_seq = time_warp_seq;
743 if (warn) /* time_warp_seq shouldn't go backwards */
744 dout("%p mds time_warp_seq %llu < %u\n",
745 inode, time_warp_seq, ci->i_time_warp_seq);
749 * Populate an inode based on info from mds. May be called on new or
752 int ceph_fill_inode(struct inode *inode, struct page *locked_page,
753 struct ceph_mds_reply_info_in *iinfo,
754 struct ceph_mds_reply_dirfrag *dirinfo,
755 struct ceph_mds_session *session, int cap_fmode,
756 struct ceph_cap_reservation *caps_reservation)
758 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
759 struct ceph_mds_reply_inode *info = iinfo->in;
760 struct ceph_inode_info *ci = ceph_inode(inode);
761 int issued, new_issued, info_caps;
762 struct timespec64 mtime, atime, ctime;
763 struct ceph_buffer *xattr_blob = NULL;
764 struct ceph_buffer *old_blob = NULL;
765 struct ceph_string *pool_ns = NULL;
766 struct ceph_cap *new_cap = NULL;
769 bool queue_trunc = false;
770 bool new_version = false;
771 bool fill_inline = false;
772 umode_t mode = le32_to_cpu(info->mode);
773 dev_t rdev = le32_to_cpu(info->rdev);
775 dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
776 inode, ceph_vinop(inode), le64_to_cpu(info->version),
779 /* Once I_NEW is cleared, we can't change type or dev numbers */
780 if (inode->i_state & I_NEW) {
781 inode->i_mode = mode;
783 if (inode_wrong_type(inode, mode)) {
784 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
785 ceph_vinop(inode), inode->i_mode, mode);
789 if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
790 pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
791 ceph_vinop(inode), MAJOR(inode->i_rdev),
792 MINOR(inode->i_rdev), MAJOR(rdev),
798 info_caps = le32_to_cpu(info->cap.caps);
800 /* prealloc new cap struct */
801 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
802 new_cap = ceph_get_cap(mdsc, caps_reservation);
808 * prealloc xattr data, if it looks like we'll need it. only
809 * if len > 4 (meaning there are actually xattrs; the first 4
810 * bytes are the xattr count).
812 if (iinfo->xattr_len > 4) {
813 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
815 pr_err("%s ENOMEM xattr blob %d bytes\n", __func__,
819 if (iinfo->pool_ns_len > 0)
820 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
823 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
824 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
826 spin_lock(&ci->i_ceph_lock);
829 * provided version will be odd if inode value is projected,
830 * even if stable. skip the update if we have newer stable
831 * info (ours>=theirs, e.g. due to racing mds replies), unless
832 * we are getting projected (unstable) info (in which case the
833 * version is odd, and we want ours>theirs).
839 if (ci->i_version == 0 ||
840 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
841 le64_to_cpu(info->version) > (ci->i_version & ~1)))
844 /* Update change_attribute */
845 inode_set_max_iversion_raw(inode, iinfo->change_attr);
847 __ceph_caps_issued(ci, &issued);
848 issued |= __ceph_caps_dirty(ci);
849 new_issued = ~issued & info_caps;
851 /* directories have fl_stripe_unit set to zero */
852 if (le32_to_cpu(info->layout.fl_stripe_unit))
854 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
856 inode->i_blkbits = CEPH_BLOCK_SHIFT;
858 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
860 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
861 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
862 inode->i_mode = mode;
863 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
864 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
865 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
866 from_kuid(&init_user_ns, inode->i_uid),
867 from_kgid(&init_user_ns, inode->i_gid));
868 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
869 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
872 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
873 (issued & CEPH_CAP_LINK_EXCL) == 0)
874 set_nlink(inode, le32_to_cpu(info->nlink));
876 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
877 /* be careful with mtime, atime, size */
878 ceph_decode_timespec64(&atime, &info->atime);
879 ceph_decode_timespec64(&mtime, &info->mtime);
880 ceph_decode_timespec64(&ctime, &info->ctime);
881 ceph_fill_file_time(inode, issued,
882 le32_to_cpu(info->time_warp_seq),
883 &ctime, &mtime, &atime);
886 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
887 ci->i_files = le64_to_cpu(info->files);
888 ci->i_subdirs = le64_to_cpu(info->subdirs);
892 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
893 s64 old_pool = ci->i_layout.pool_id;
894 struct ceph_string *old_ns;
896 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
897 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
898 lockdep_is_held(&ci->i_ceph_lock));
899 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
901 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
902 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
906 queue_trunc = ceph_fill_file_size(inode, issued,
907 le32_to_cpu(info->truncate_seq),
908 le64_to_cpu(info->truncate_size),
909 le64_to_cpu(info->size));
910 /* only update max_size on auth cap */
911 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
912 ci->i_max_size != le64_to_cpu(info->max_size)) {
913 dout("max_size %lld -> %llu\n", ci->i_max_size,
914 le64_to_cpu(info->max_size));
915 ci->i_max_size = le64_to_cpu(info->max_size);
919 /* layout and rstat are not tracked by capability, update them if
920 * the inode info is from auth mds */
921 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
922 if (S_ISDIR(inode->i_mode)) {
923 ci->i_dir_layout = iinfo->dir_layout;
924 ci->i_rbytes = le64_to_cpu(info->rbytes);
925 ci->i_rfiles = le64_to_cpu(info->rfiles);
926 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
927 ci->i_dir_pin = iinfo->dir_pin;
928 ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
933 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
934 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
935 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
936 if (ci->i_xattrs.blob)
937 old_blob = ci->i_xattrs.blob;
938 ci->i_xattrs.blob = xattr_blob;
940 memcpy(ci->i_xattrs.blob->vec.iov_base,
941 iinfo->xattr_data, iinfo->xattr_len);
942 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
943 ceph_forget_all_cached_acls(inode);
944 ceph_security_invalidate_secctx(inode);
948 /* finally update i_version */
949 if (le64_to_cpu(info->version) > ci->i_version)
950 ci->i_version = le64_to_cpu(info->version);
952 inode->i_mapping->a_ops = &ceph_aops;
954 switch (inode->i_mode & S_IFMT) {
959 inode->i_blkbits = PAGE_SHIFT;
960 init_special_inode(inode, inode->i_mode, rdev);
961 inode->i_op = &ceph_file_iops;
964 inode->i_op = &ceph_file_iops;
965 inode->i_fop = &ceph_file_fops;
968 inode->i_op = &ceph_symlink_iops;
969 if (!ci->i_symlink) {
970 u32 symlen = iinfo->symlink_len;
973 spin_unlock(&ci->i_ceph_lock);
975 if (symlen != i_size_read(inode)) {
976 pr_err("%s %llx.%llx BAD symlink "
977 "size %lld\n", __func__,
980 i_size_write(inode, symlen);
981 inode->i_blocks = calc_inode_blocks(symlen);
985 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
989 spin_lock(&ci->i_ceph_lock);
993 kfree(sym); /* lost a race */
995 inode->i_link = ci->i_symlink;
998 inode->i_op = &ceph_dir_iops;
999 inode->i_fop = &ceph_dir_fops;
1002 pr_err("%s %llx.%llx BAD mode 0%o\n", __func__,
1003 ceph_vinop(inode), inode->i_mode);
1006 /* were we issued a capability? */
1008 if (ceph_snap(inode) == CEPH_NOSNAP) {
1009 ceph_add_cap(inode, session,
1010 le64_to_cpu(info->cap.cap_id),
1012 le32_to_cpu(info->cap.wanted),
1013 le32_to_cpu(info->cap.seq),
1014 le32_to_cpu(info->cap.mseq),
1015 le64_to_cpu(info->cap.realm),
1016 info->cap.flags, &new_cap);
1018 /* set dir completion flag? */
1019 if (S_ISDIR(inode->i_mode) &&
1020 ci->i_files == 0 && ci->i_subdirs == 0 &&
1021 (info_caps & CEPH_CAP_FILE_SHARED) &&
1022 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1023 !__ceph_dir_is_complete(ci)) {
1024 dout(" marking %p complete (empty)\n", inode);
1025 i_size_write(inode, 0);
1026 __ceph_dir_set_complete(ci,
1027 atomic64_read(&ci->i_release_count),
1028 atomic64_read(&ci->i_ordered_count));
1033 dout(" %p got snap_caps %s\n", inode,
1034 ceph_cap_string(info_caps));
1035 ci->i_snap_caps |= info_caps;
1039 if (iinfo->inline_version > 0 &&
1040 iinfo->inline_version >= ci->i_inline_version) {
1041 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1042 ci->i_inline_version = iinfo->inline_version;
1043 if (ci->i_inline_version != CEPH_INLINE_NONE &&
1044 (locked_page || (info_caps & cache_caps)))
1048 if (cap_fmode >= 0) {
1050 pr_warn("mds issued no caps on %llx.%llx\n",
1052 __ceph_touch_fmode(ci, mdsc, cap_fmode);
1055 spin_unlock(&ci->i_ceph_lock);
1058 ceph_fill_inline_data(inode, locked_page,
1059 iinfo->inline_data, iinfo->inline_len);
1062 wake_up_all(&ci->i_cap_wq);
1064 /* queue truncate if we saw i_size decrease */
1066 ceph_queue_vmtruncate(inode);
1068 /* populate frag tree */
1069 if (S_ISDIR(inode->i_mode))
1070 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1072 /* update delegation info? */
1074 ceph_fill_dirfrag(inode, dirinfo);
1079 ceph_put_cap(mdsc, new_cap);
1080 ceph_buffer_put(old_blob);
1081 ceph_buffer_put(xattr_blob);
1082 ceph_put_string(pool_ns);
1087 * caller should hold session s_mutex and dentry->d_lock.
1089 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1090 struct ceph_mds_reply_lease *lease,
1091 struct ceph_mds_session *session,
1092 unsigned long from_time,
1093 struct ceph_mds_session **old_lease_session)
1095 struct ceph_dentry_info *di = ceph_dentry(dentry);
1096 unsigned mask = le16_to_cpu(lease->mask);
1097 long unsigned duration = le32_to_cpu(lease->duration_ms);
1098 long unsigned ttl = from_time + (duration * HZ) / 1000;
1099 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1101 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1102 dentry, duration, ttl);
1104 /* only track leases on regular dentries */
1105 if (ceph_snap(dir) != CEPH_NOSNAP)
1108 if (mask & CEPH_LEASE_PRIMARY_LINK)
1109 di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1111 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1113 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1114 if (!(mask & CEPH_LEASE_VALID)) {
1115 __ceph_dentry_dir_lease_touch(di);
1119 if (di->lease_gen == session->s_cap_gen &&
1120 time_before(ttl, di->time))
1121 return; /* we already have a newer lease. */
1123 if (di->lease_session && di->lease_session != session) {
1124 *old_lease_session = di->lease_session;
1125 di->lease_session = NULL;
1128 if (!di->lease_session)
1129 di->lease_session = ceph_get_mds_session(session);
1130 di->lease_gen = session->s_cap_gen;
1131 di->lease_seq = le32_to_cpu(lease->seq);
1132 di->lease_renew_after = half_ttl;
1133 di->lease_renew_from = 0;
1136 __ceph_dentry_lease_touch(di);
1139 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1140 struct ceph_mds_reply_lease *lease,
1141 struct ceph_mds_session *session,
1142 unsigned long from_time)
1144 struct ceph_mds_session *old_lease_session = NULL;
1145 spin_lock(&dentry->d_lock);
1146 __update_dentry_lease(dir, dentry, lease, session, from_time,
1147 &old_lease_session);
1148 spin_unlock(&dentry->d_lock);
1149 if (old_lease_session)
1150 ceph_put_mds_session(old_lease_session);
1154 * update dentry lease without having parent inode locked
1156 static void update_dentry_lease_careful(struct dentry *dentry,
1157 struct ceph_mds_reply_lease *lease,
1158 struct ceph_mds_session *session,
1159 unsigned long from_time,
1160 char *dname, u32 dname_len,
1161 struct ceph_vino *pdvino,
1162 struct ceph_vino *ptvino)
1166 struct ceph_mds_session *old_lease_session = NULL;
1168 spin_lock(&dentry->d_lock);
1169 /* make sure dentry's name matches target */
1170 if (dentry->d_name.len != dname_len ||
1171 memcmp(dentry->d_name.name, dname, dname_len))
1174 dir = d_inode(dentry->d_parent);
1175 /* make sure parent matches dvino */
1176 if (!ceph_ino_compare(dir, pdvino))
1179 /* make sure dentry's inode matches target. NULL ptvino means that
1180 * we expect a negative dentry */
1182 if (d_really_is_negative(dentry))
1184 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1187 if (d_really_is_positive(dentry))
1191 __update_dentry_lease(dir, dentry, lease, session,
1192 from_time, &old_lease_session);
1194 spin_unlock(&dentry->d_lock);
1195 if (old_lease_session)
1196 ceph_put_mds_session(old_lease_session);
1200 * splice a dentry to an inode.
1201 * caller must hold directory i_mutex for this to be safe.
1203 static int splice_dentry(struct dentry **pdn, struct inode *in)
1205 struct dentry *dn = *pdn;
1206 struct dentry *realdn;
1208 BUG_ON(d_inode(dn));
1210 if (S_ISDIR(in->i_mode)) {
1211 /* If inode is directory, d_splice_alias() below will remove
1212 * 'realdn' from its origin parent. We need to ensure that
1213 * origin parent's readdir cache will not reference 'realdn'
1215 realdn = d_find_any_alias(in);
1217 struct ceph_dentry_info *di = ceph_dentry(realdn);
1218 spin_lock(&realdn->d_lock);
1220 realdn->d_op->d_prune(realdn);
1223 di->lease_shared_gen = 0;
1226 spin_unlock(&realdn->d_lock);
1231 /* dn must be unhashed */
1232 if (!d_unhashed(dn))
1234 realdn = d_splice_alias(in, dn);
1235 if (IS_ERR(realdn)) {
1236 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1237 PTR_ERR(realdn), dn, in, ceph_vinop(in));
1238 return PTR_ERR(realdn);
1242 dout("dn %p (%d) spliced with %p (%d) "
1243 "inode %p ino %llx.%llx\n",
1245 realdn, d_count(realdn),
1246 d_inode(realdn), ceph_vinop(d_inode(realdn)));
1250 BUG_ON(!ceph_dentry(dn));
1251 dout("dn %p attached to %p ino %llx.%llx\n",
1252 dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1258 * Incorporate results into the local cache. This is either just
1259 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1262 * A reply may contain
1263 * a directory inode along with a dentry.
1264 * and/or a target inode
1266 * Called with snap_rwsem (read).
1268 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1270 struct ceph_mds_session *session = req->r_session;
1271 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1272 struct inode *in = NULL;
1273 struct ceph_vino tvino, dvino;
1274 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1277 dout("fill_trace %p is_dentry %d is_target %d\n", req,
1278 rinfo->head->is_dentry, rinfo->head->is_target);
1280 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1281 dout("fill_trace reply is empty!\n");
1282 if (rinfo->head->result == 0 && req->r_parent)
1283 ceph_invalidate_dir_request(req);
1287 if (rinfo->head->is_dentry) {
1288 struct inode *dir = req->r_parent;
1291 err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1292 rinfo->dirfrag, session, -1,
1293 &req->r_caps_reservation);
1300 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1301 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1302 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1304 struct dentry *dn, *parent;
1306 BUG_ON(!rinfo->head->is_target);
1307 BUG_ON(req->r_dentry);
1309 parent = d_find_any_alias(dir);
1312 dname.name = rinfo->dname;
1313 dname.len = rinfo->dname_len;
1314 dname.hash = full_name_hash(parent, dname.name, dname.len);
1315 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1316 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1318 dn = d_lookup(parent, &dname);
1319 dout("d_lookup on parent=%p name=%.*s got %p\n",
1320 parent, dname.len, dname.name, dn);
1323 dn = d_alloc(parent, &dname);
1324 dout("d_alloc %p '%.*s' = %p\n", parent,
1325 dname.len, dname.name, dn);
1332 } else if (d_really_is_positive(dn) &&
1333 (ceph_ino(d_inode(dn)) != tvino.ino ||
1334 ceph_snap(d_inode(dn)) != tvino.snap)) {
1335 dout(" dn %p points to wrong inode %p\n",
1337 ceph_dir_clear_ordered(dir);
1348 if (rinfo->head->is_target) {
1349 /* Should be filled in by handle_reply */
1350 BUG_ON(!req->r_target_inode);
1352 in = req->r_target_inode;
1353 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1355 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1356 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1357 rinfo->head->result == 0) ? req->r_fmode : -1,
1358 &req->r_caps_reservation);
1360 pr_err("ceph_fill_inode badness %p %llx.%llx\n",
1361 in, ceph_vinop(in));
1362 req->r_target_inode = NULL;
1363 if (in->i_state & I_NEW)
1364 discard_new_inode(in);
1369 if (in->i_state & I_NEW)
1370 unlock_new_inode(in);
1374 * ignore null lease/binding on snapdir ENOENT, or else we
1375 * will have trouble splicing in the virtual snapdir later
1377 if (rinfo->head->is_dentry &&
1378 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1379 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1380 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1381 fsc->mount_options->snapdir_name,
1382 req->r_dentry->d_name.len))) {
1384 * lookup link rename : null -> possibly existing inode
1385 * mknod symlink mkdir : null -> new inode
1386 * unlink : linked -> null
1388 struct inode *dir = req->r_parent;
1389 struct dentry *dn = req->r_dentry;
1390 bool have_dir_cap, have_lease;
1394 BUG_ON(d_inode(dn->d_parent) != dir);
1396 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1397 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1399 BUG_ON(ceph_ino(dir) != dvino.ino);
1400 BUG_ON(ceph_snap(dir) != dvino.snap);
1402 /* do we have a lease on the whole dir? */
1404 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1405 CEPH_CAP_FILE_SHARED);
1407 /* do we have a dn lease? */
1408 have_lease = have_dir_cap ||
1409 le32_to_cpu(rinfo->dlease->duration_ms);
1411 dout("fill_trace no dentry lease or dir cap\n");
1414 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1415 struct inode *olddir = req->r_old_dentry_dir;
1418 dout(" src %p '%pd' dst %p '%pd'\n",
1422 dout("fill_trace doing d_move %p -> %p\n",
1423 req->r_old_dentry, dn);
1425 /* d_move screws up sibling dentries' offsets */
1426 ceph_dir_clear_ordered(dir);
1427 ceph_dir_clear_ordered(olddir);
1429 d_move(req->r_old_dentry, dn);
1430 dout(" src %p '%pd' dst %p '%pd'\n",
1435 /* ensure target dentry is invalidated, despite
1436 rehashing bug in vfs_rename_dir */
1437 ceph_invalidate_dentry_lease(dn);
1439 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1440 ceph_dentry(req->r_old_dentry)->offset);
1442 /* swap r_dentry and r_old_dentry in case that
1443 * splice_dentry() gets called later. This is safe
1444 * because no other place will use them */
1445 req->r_dentry = req->r_old_dentry;
1446 req->r_old_dentry = dn;
1451 if (!rinfo->head->is_target) {
1452 dout("fill_trace null dentry\n");
1453 if (d_really_is_positive(dn)) {
1454 dout("d_delete %p\n", dn);
1455 ceph_dir_clear_ordered(dir);
1457 } else if (have_lease) {
1460 update_dentry_lease(dir, dn,
1461 rinfo->dlease, session,
1462 req->r_request_started);
1467 /* attach proper inode */
1468 if (d_really_is_negative(dn)) {
1469 ceph_dir_clear_ordered(dir);
1471 err = splice_dentry(&req->r_dentry, in);
1474 dn = req->r_dentry; /* may have spliced */
1475 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1476 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1477 dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1484 update_dentry_lease(dir, dn,
1485 rinfo->dlease, session,
1486 req->r_request_started);
1488 dout(" final dn %p\n", dn);
1489 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1490 req->r_op == CEPH_MDS_OP_MKSNAP) &&
1491 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1492 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1493 struct inode *dir = req->r_parent;
1495 /* fill out a snapdir LOOKUPSNAP dentry */
1497 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1498 BUG_ON(!req->r_dentry);
1499 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1500 ceph_dir_clear_ordered(dir);
1502 err = splice_dentry(&req->r_dentry, in);
1505 } else if (rinfo->head->is_dentry && req->r_dentry) {
1506 /* parent inode is not locked, be carefull */
1507 struct ceph_vino *ptvino = NULL;
1508 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1509 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1510 if (rinfo->head->is_target) {
1511 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1512 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1515 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1516 session, req->r_request_started,
1517 rinfo->dname, rinfo->dname_len,
1521 dout("fill_trace done err=%d\n", err);
1526 * Prepopulate our cache with readdir results, leases, etc.
1528 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1529 struct ceph_mds_session *session)
1531 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1534 for (i = 0; i < rinfo->dir_nr; i++) {
1535 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1536 struct ceph_vino vino;
1540 vino.ino = le64_to_cpu(rde->inode.in->ino);
1541 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1543 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1546 dout("new_inode badness got %d\n", err);
1549 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1550 -1, &req->r_caps_reservation);
1552 pr_err("ceph_fill_inode badness on %p got %d\n",
1555 if (in->i_state & I_NEW) {
1557 discard_new_inode(in);
1559 } else if (in->i_state & I_NEW) {
1560 unlock_new_inode(in);
1563 /* avoid calling iput_final() in mds dispatch threads */
1564 ceph_async_iput(in);
1570 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1574 put_page(ctl->page);
1579 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1580 struct ceph_readdir_cache_control *ctl,
1581 struct ceph_mds_request *req)
1583 struct ceph_inode_info *ci = ceph_inode(dir);
1584 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1585 unsigned idx = ctl->index % nsize;
1586 pgoff_t pgoff = ctl->index / nsize;
1588 if (!ctl->page || pgoff != page_index(ctl->page)) {
1589 ceph_readdir_cache_release(ctl);
1591 ctl->page = grab_cache_page(&dir->i_data, pgoff);
1593 ctl->page = find_lock_page(&dir->i_data, pgoff);
1596 return idx == 0 ? -ENOMEM : 0;
1598 /* reading/filling the cache are serialized by
1599 * i_mutex, no need to use page lock */
1600 unlock_page(ctl->page);
1601 ctl->dentries = kmap(ctl->page);
1603 memset(ctl->dentries, 0, PAGE_SIZE);
1606 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1607 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1608 dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1609 ctl->dentries[idx] = dn;
1612 dout("disable readdir cache\n");
1618 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1619 struct ceph_mds_session *session)
1621 struct dentry *parent = req->r_dentry;
1622 struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
1623 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1627 int err = 0, skipped = 0, ret, i;
1628 u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1631 struct ceph_readdir_cache_control cache_ctl = {};
1633 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1634 return readdir_prepopulate_inodes_only(req, session);
1636 if (rinfo->hash_order) {
1638 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1640 strlen(req->r_path2));
1641 last_hash = ceph_frag_value(last_hash);
1642 } else if (rinfo->offset_hash) {
1643 /* mds understands offset_hash */
1644 WARN_ON_ONCE(req->r_readdir_offset != 2);
1645 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
1649 if (rinfo->dir_dir &&
1650 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1651 dout("readdir_prepopulate got new frag %x -> %x\n",
1652 frag, le32_to_cpu(rinfo->dir_dir->frag));
1653 frag = le32_to_cpu(rinfo->dir_dir->frag);
1654 if (!rinfo->hash_order)
1655 req->r_readdir_offset = 2;
1658 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1659 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1660 rinfo->dir_nr, parent);
1662 dout("readdir_prepopulate %d items under dn %p\n",
1663 rinfo->dir_nr, parent);
1665 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1667 if (ceph_frag_is_leftmost(frag) &&
1668 req->r_readdir_offset == 2 &&
1669 !(rinfo->hash_order && last_hash)) {
1670 /* note dir version at start of readdir so we can
1671 * tell if any dentries get dropped */
1672 req->r_dir_release_cnt =
1673 atomic64_read(&ci->i_release_count);
1674 req->r_dir_ordered_cnt =
1675 atomic64_read(&ci->i_ordered_count);
1676 req->r_readdir_cache_idx = 0;
1680 cache_ctl.index = req->r_readdir_cache_idx;
1681 fpos_offset = req->r_readdir_offset;
1683 /* FIXME: release caps/leases if error occurs */
1684 for (i = 0; i < rinfo->dir_nr; i++) {
1685 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1686 struct ceph_vino tvino;
1688 dname.name = rde->name;
1689 dname.len = rde->name_len;
1690 dname.hash = full_name_hash(parent, dname.name, dname.len);
1692 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1693 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1695 if (rinfo->hash_order) {
1696 u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1697 rde->name, rde->name_len);
1698 hash = ceph_frag_value(hash);
1699 if (hash != last_hash)
1702 rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1704 rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1708 dn = d_lookup(parent, &dname);
1709 dout("d_lookup on parent=%p name=%.*s got %p\n",
1710 parent, dname.len, dname.name, dn);
1713 dn = d_alloc(parent, &dname);
1714 dout("d_alloc %p '%.*s' = %p\n", parent,
1715 dname.len, dname.name, dn);
1717 dout("d_alloc badness\n");
1721 } else if (d_really_is_positive(dn) &&
1722 (ceph_ino(d_inode(dn)) != tvino.ino ||
1723 ceph_snap(d_inode(dn)) != tvino.snap)) {
1724 struct ceph_dentry_info *di = ceph_dentry(dn);
1725 dout(" dn %p points to wrong inode %p\n",
1728 spin_lock(&dn->d_lock);
1729 if (di->offset > 0 &&
1730 di->lease_shared_gen ==
1731 atomic_read(&ci->i_shared_gen)) {
1732 __ceph_dir_clear_ordered(ci);
1735 spin_unlock(&dn->d_lock);
1743 if (d_really_is_positive(dn)) {
1746 in = ceph_get_inode(parent->d_sb, tvino);
1748 dout("new_inode badness\n");
1756 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1757 -1, &req->r_caps_reservation);
1759 pr_err("ceph_fill_inode badness on %p\n", in);
1760 if (d_really_is_negative(dn)) {
1761 /* avoid calling iput_final() in mds
1762 * dispatch threads */
1763 if (in->i_state & I_NEW) {
1765 discard_new_inode(in);
1767 ceph_async_iput(in);
1773 if (in->i_state & I_NEW)
1774 unlock_new_inode(in);
1776 if (d_really_is_negative(dn)) {
1777 if (ceph_security_xattr_deadlock(in)) {
1778 dout(" skip splicing dn %p to inode %p"
1779 " (security xattr deadlock)\n", dn, in);
1780 ceph_async_iput(in);
1785 err = splice_dentry(&dn, in);
1790 ceph_dentry(dn)->offset = rde->offset;
1792 update_dentry_lease(d_inode(parent), dn,
1793 rde->lease, req->r_session,
1794 req->r_request_started);
1796 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
1797 ret = fill_readdir_cache(d_inode(parent), dn,
1806 if (err == 0 && skipped == 0) {
1807 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
1808 req->r_readdir_cache_idx = cache_ctl.index;
1810 ceph_readdir_cache_release(&cache_ctl);
1811 dout("readdir_prepopulate done\n");
1815 bool ceph_inode_set_size(struct inode *inode, loff_t size)
1817 struct ceph_inode_info *ci = ceph_inode(inode);
1820 spin_lock(&ci->i_ceph_lock);
1821 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1822 i_size_write(inode, size);
1823 inode->i_blocks = calc_inode_blocks(size);
1825 ret = __ceph_should_report_size(ci);
1827 spin_unlock(&ci->i_ceph_lock);
1832 * Put reference to inode, but avoid calling iput_final() in current thread.
1833 * iput_final() may wait for reahahead pages. The wait can cause deadlock in
1836 void ceph_async_iput(struct inode *inode)
1841 if (atomic_add_unless(&inode->i_count, -1, 1))
1843 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1844 &ceph_inode(inode)->i_work))
1846 /* queue work failed, i_count must be at least 2 */
1850 void ceph_queue_inode_work(struct inode *inode, int work_bit)
1852 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1853 struct ceph_inode_info *ci = ceph_inode(inode);
1854 set_bit(work_bit, &ci->i_work_mask);
1857 if (queue_work(fsc->inode_wq, &ci->i_work)) {
1858 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask);
1860 dout("queue_inode_work %p already queued, mask=%lx\n",
1861 inode, ci->i_work_mask);
1866 static void ceph_do_invalidate_pages(struct inode *inode)
1868 struct ceph_inode_info *ci = ceph_inode(inode);
1869 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1873 mutex_lock(&ci->i_truncate_mutex);
1875 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
1876 pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
1877 inode, ceph_ino(inode));
1878 mapping_set_error(inode->i_mapping, -EIO);
1879 truncate_pagecache(inode, 0);
1880 mutex_unlock(&ci->i_truncate_mutex);
1884 spin_lock(&ci->i_ceph_lock);
1885 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1886 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1887 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1888 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1890 spin_unlock(&ci->i_ceph_lock);
1891 mutex_unlock(&ci->i_truncate_mutex);
1894 orig_gen = ci->i_rdcache_gen;
1895 spin_unlock(&ci->i_ceph_lock);
1897 if (invalidate_inode_pages2(inode->i_mapping) < 0) {
1898 pr_err("invalidate_pages %p fails\n", inode);
1901 spin_lock(&ci->i_ceph_lock);
1902 if (orig_gen == ci->i_rdcache_gen &&
1903 orig_gen == ci->i_rdcache_revoking) {
1904 dout("invalidate_pages %p gen %d successful\n", inode,
1906 ci->i_rdcache_revoking--;
1909 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1910 inode, orig_gen, ci->i_rdcache_gen,
1911 ci->i_rdcache_revoking);
1912 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1915 spin_unlock(&ci->i_ceph_lock);
1916 mutex_unlock(&ci->i_truncate_mutex);
1919 ceph_check_caps(ci, 0, NULL);
1923 * Make sure any pending truncation is applied before doing anything
1924 * that may depend on it.
1926 void __ceph_do_pending_vmtruncate(struct inode *inode)
1928 struct ceph_inode_info *ci = ceph_inode(inode);
1930 int wrbuffer_refs, finish = 0;
1932 mutex_lock(&ci->i_truncate_mutex);
1934 spin_lock(&ci->i_ceph_lock);
1935 if (ci->i_truncate_pending == 0) {
1936 dout("__do_pending_vmtruncate %p none pending\n", inode);
1937 spin_unlock(&ci->i_ceph_lock);
1938 mutex_unlock(&ci->i_truncate_mutex);
1943 * make sure any dirty snapped pages are flushed before we
1944 * possibly truncate them.. so write AND block!
1946 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1947 spin_unlock(&ci->i_ceph_lock);
1948 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1950 filemap_write_and_wait_range(&inode->i_data, 0,
1951 inode->i_sb->s_maxbytes);
1955 /* there should be no reader or writer */
1956 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1958 to = ci->i_truncate_size;
1959 wrbuffer_refs = ci->i_wrbuffer_ref;
1960 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1961 ci->i_truncate_pending, to);
1962 spin_unlock(&ci->i_ceph_lock);
1964 truncate_pagecache(inode, to);
1966 spin_lock(&ci->i_ceph_lock);
1967 if (to == ci->i_truncate_size) {
1968 ci->i_truncate_pending = 0;
1971 spin_unlock(&ci->i_ceph_lock);
1975 mutex_unlock(&ci->i_truncate_mutex);
1977 if (wrbuffer_refs == 0)
1978 ceph_check_caps(ci, 0, NULL);
1980 wake_up_all(&ci->i_cap_wq);
1983 static void ceph_inode_work(struct work_struct *work)
1985 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1987 struct inode *inode = &ci->vfs_inode;
1989 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
1990 dout("writeback %p\n", inode);
1991 filemap_fdatawrite(&inode->i_data);
1993 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
1994 ceph_do_invalidate_pages(inode);
1996 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
1997 __ceph_do_pending_vmtruncate(inode);
1999 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
2000 ceph_check_caps(ci, 0, NULL);
2002 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
2003 ceph_flush_snaps(ci, NULL);
2011 static const struct inode_operations ceph_symlink_iops = {
2012 .get_link = simple_get_link,
2013 .setattr = ceph_setattr,
2014 .getattr = ceph_getattr,
2015 .listxattr = ceph_listxattr,
2018 int __ceph_setattr(struct inode *inode, struct iattr *attr)
2020 struct ceph_inode_info *ci = ceph_inode(inode);
2021 unsigned int ia_valid = attr->ia_valid;
2022 struct ceph_mds_request *req;
2023 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2024 struct ceph_cap_flush *prealloc_cf;
2026 int release = 0, dirtied = 0;
2029 int inode_dirty_flags = 0;
2030 bool lock_snap_rwsem = false;
2032 prealloc_cf = ceph_alloc_cap_flush();
2036 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2039 ceph_free_cap_flush(prealloc_cf);
2040 return PTR_ERR(req);
2043 spin_lock(&ci->i_ceph_lock);
2044 issued = __ceph_caps_issued(ci, NULL);
2046 if (!ci->i_head_snapc &&
2047 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2048 lock_snap_rwsem = true;
2049 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2050 spin_unlock(&ci->i_ceph_lock);
2051 down_read(&mdsc->snap_rwsem);
2052 spin_lock(&ci->i_ceph_lock);
2053 issued = __ceph_caps_issued(ci, NULL);
2057 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2059 if (ia_valid & ATTR_UID) {
2060 dout("setattr %p uid %d -> %d\n", inode,
2061 from_kuid(&init_user_ns, inode->i_uid),
2062 from_kuid(&init_user_ns, attr->ia_uid));
2063 if (issued & CEPH_CAP_AUTH_EXCL) {
2064 inode->i_uid = attr->ia_uid;
2065 dirtied |= CEPH_CAP_AUTH_EXCL;
2066 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2067 !uid_eq(attr->ia_uid, inode->i_uid)) {
2068 req->r_args.setattr.uid = cpu_to_le32(
2069 from_kuid(&init_user_ns, attr->ia_uid));
2070 mask |= CEPH_SETATTR_UID;
2071 release |= CEPH_CAP_AUTH_SHARED;
2074 if (ia_valid & ATTR_GID) {
2075 dout("setattr %p gid %d -> %d\n", inode,
2076 from_kgid(&init_user_ns, inode->i_gid),
2077 from_kgid(&init_user_ns, attr->ia_gid));
2078 if (issued & CEPH_CAP_AUTH_EXCL) {
2079 inode->i_gid = attr->ia_gid;
2080 dirtied |= CEPH_CAP_AUTH_EXCL;
2081 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2082 !gid_eq(attr->ia_gid, inode->i_gid)) {
2083 req->r_args.setattr.gid = cpu_to_le32(
2084 from_kgid(&init_user_ns, attr->ia_gid));
2085 mask |= CEPH_SETATTR_GID;
2086 release |= CEPH_CAP_AUTH_SHARED;
2089 if (ia_valid & ATTR_MODE) {
2090 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2092 if (issued & CEPH_CAP_AUTH_EXCL) {
2093 inode->i_mode = attr->ia_mode;
2094 dirtied |= CEPH_CAP_AUTH_EXCL;
2095 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2096 attr->ia_mode != inode->i_mode) {
2097 inode->i_mode = attr->ia_mode;
2098 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2099 mask |= CEPH_SETATTR_MODE;
2100 release |= CEPH_CAP_AUTH_SHARED;
2104 if (ia_valid & ATTR_ATIME) {
2105 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2106 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2107 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2108 if (issued & CEPH_CAP_FILE_EXCL) {
2109 ci->i_time_warp_seq++;
2110 inode->i_atime = attr->ia_atime;
2111 dirtied |= CEPH_CAP_FILE_EXCL;
2112 } else if ((issued & CEPH_CAP_FILE_WR) &&
2113 timespec64_compare(&inode->i_atime,
2114 &attr->ia_atime) < 0) {
2115 inode->i_atime = attr->ia_atime;
2116 dirtied |= CEPH_CAP_FILE_WR;
2117 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2118 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2119 ceph_encode_timespec64(&req->r_args.setattr.atime,
2121 mask |= CEPH_SETATTR_ATIME;
2122 release |= CEPH_CAP_FILE_SHARED |
2123 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2126 if (ia_valid & ATTR_SIZE) {
2127 dout("setattr %p size %lld -> %lld\n", inode,
2128 inode->i_size, attr->ia_size);
2129 if ((issued & CEPH_CAP_FILE_EXCL) &&
2130 attr->ia_size > inode->i_size) {
2131 i_size_write(inode, attr->ia_size);
2132 inode->i_blocks = calc_inode_blocks(attr->ia_size);
2133 ci->i_reported_size = attr->ia_size;
2134 dirtied |= CEPH_CAP_FILE_EXCL;
2135 ia_valid |= ATTR_MTIME;
2136 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2137 attr->ia_size != inode->i_size) {
2138 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2139 req->r_args.setattr.old_size =
2140 cpu_to_le64(inode->i_size);
2141 mask |= CEPH_SETATTR_SIZE;
2142 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2143 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2146 if (ia_valid & ATTR_MTIME) {
2147 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2148 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2149 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2150 if (issued & CEPH_CAP_FILE_EXCL) {
2151 ci->i_time_warp_seq++;
2152 inode->i_mtime = attr->ia_mtime;
2153 dirtied |= CEPH_CAP_FILE_EXCL;
2154 } else if ((issued & CEPH_CAP_FILE_WR) &&
2155 timespec64_compare(&inode->i_mtime,
2156 &attr->ia_mtime) < 0) {
2157 inode->i_mtime = attr->ia_mtime;
2158 dirtied |= CEPH_CAP_FILE_WR;
2159 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2160 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2161 ceph_encode_timespec64(&req->r_args.setattr.mtime,
2163 mask |= CEPH_SETATTR_MTIME;
2164 release |= CEPH_CAP_FILE_SHARED |
2165 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2169 /* these do nothing */
2170 if (ia_valid & ATTR_CTIME) {
2171 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2172 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2173 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2174 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2175 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2176 only ? "ctime only" : "ignored");
2179 * if kernel wants to dirty ctime but nothing else,
2180 * we need to choose a cap to dirty under, or do
2181 * a almost-no-op setattr
2183 if (issued & CEPH_CAP_AUTH_EXCL)
2184 dirtied |= CEPH_CAP_AUTH_EXCL;
2185 else if (issued & CEPH_CAP_FILE_EXCL)
2186 dirtied |= CEPH_CAP_FILE_EXCL;
2187 else if (issued & CEPH_CAP_XATTR_EXCL)
2188 dirtied |= CEPH_CAP_XATTR_EXCL;
2190 mask |= CEPH_SETATTR_CTIME;
2193 if (ia_valid & ATTR_FILE)
2194 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2197 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2199 inode->i_ctime = attr->ia_ctime;
2203 spin_unlock(&ci->i_ceph_lock);
2204 if (lock_snap_rwsem)
2205 up_read(&mdsc->snap_rwsem);
2207 if (inode_dirty_flags)
2208 __mark_inode_dirty(inode, inode_dirty_flags);
2212 req->r_inode = inode;
2214 req->r_inode_drop = release;
2215 req->r_args.setattr.mask = cpu_to_le32(mask);
2216 req->r_num_caps = 1;
2217 req->r_stamp = attr->ia_ctime;
2218 err = ceph_mdsc_do_request(mdsc, NULL, req);
2220 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2221 ceph_cap_string(dirtied), mask);
2223 ceph_mdsc_put_request(req);
2224 ceph_free_cap_flush(prealloc_cf);
2226 if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2227 __ceph_do_pending_vmtruncate(inode);
2235 int ceph_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
2238 struct inode *inode = d_inode(dentry);
2239 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2242 if (ceph_snap(inode) != CEPH_NOSNAP)
2245 err = setattr_prepare(&init_user_ns, dentry, attr);
2249 if ((attr->ia_valid & ATTR_SIZE) &&
2250 attr->ia_size > max(inode->i_size, fsc->max_file_size))
2253 if ((attr->ia_valid & ATTR_SIZE) &&
2254 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2257 err = __ceph_setattr(inode, attr);
2259 if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2260 err = posix_acl_chmod(&init_user_ns, inode, attr->ia_mode);
2266 * Verify that we have a lease on the given mask. If not,
2267 * do a getattr against an mds.
2269 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2270 int mask, bool force)
2272 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2273 struct ceph_mds_client *mdsc = fsc->mdsc;
2274 struct ceph_mds_request *req;
2278 if (ceph_snap(inode) == CEPH_SNAPDIR) {
2279 dout("do_getattr inode %p SNAPDIR\n", inode);
2283 dout("do_getattr inode %p mask %s mode 0%o\n",
2284 inode, ceph_cap_string(mask), inode->i_mode);
2285 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2288 mode = (mask & CEPH_STAT_RSTAT) ? USE_AUTH_MDS : USE_ANY_MDS;
2289 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2291 return PTR_ERR(req);
2292 req->r_inode = inode;
2294 req->r_num_caps = 1;
2295 req->r_args.getattr.mask = cpu_to_le32(mask);
2296 req->r_locked_page = locked_page;
2297 err = ceph_mdsc_do_request(mdsc, NULL, req);
2298 if (locked_page && err == 0) {
2299 u64 inline_version = req->r_reply_info.targeti.inline_version;
2300 if (inline_version == 0) {
2301 /* the reply is supposed to contain inline data */
2303 } else if (inline_version == CEPH_INLINE_NONE) {
2306 err = req->r_reply_info.targeti.inline_len;
2309 ceph_mdsc_put_request(req);
2310 dout("do_getattr result=%d\n", err);
2316 * Check inode permissions. We verify we have a valid value for
2317 * the AUTH cap, then call the generic handler.
2319 int ceph_permission(struct user_namespace *mnt_userns, struct inode *inode,
2324 if (mask & MAY_NOT_BLOCK)
2327 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2330 err = generic_permission(&init_user_ns, inode, mask);
2334 /* Craft a mask of needed caps given a set of requested statx attrs. */
2335 static int statx_to_caps(u32 want, umode_t mode)
2339 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME))
2340 mask |= CEPH_CAP_AUTH_SHARED;
2342 if (want & (STATX_NLINK|STATX_CTIME)) {
2344 * The link count for directories depends on inode->i_subdirs,
2345 * and that is only updated when Fs caps are held.
2348 mask |= CEPH_CAP_FILE_SHARED;
2350 mask |= CEPH_CAP_LINK_SHARED;
2353 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|
2355 mask |= CEPH_CAP_FILE_SHARED;
2357 if (want & (STATX_CTIME))
2358 mask |= CEPH_CAP_XATTR_SHARED;
2364 * Get all the attributes. If we have sufficient caps for the requested attrs,
2365 * then we can avoid talking to the MDS at all.
2367 int ceph_getattr(struct user_namespace *mnt_userns, const struct path *path,
2368 struct kstat *stat, u32 request_mask, unsigned int flags)
2370 struct inode *inode = d_inode(path->dentry);
2371 struct ceph_inode_info *ci = ceph_inode(inode);
2372 u32 valid_mask = STATX_BASIC_STATS;
2375 /* Skip the getattr altogether if we're asked not to sync */
2376 if (!(flags & AT_STATX_DONT_SYNC)) {
2377 err = ceph_do_getattr(inode,
2378 statx_to_caps(request_mask, inode->i_mode),
2379 flags & AT_STATX_FORCE_SYNC);
2384 generic_fillattr(&init_user_ns, inode, stat);
2385 stat->ino = ceph_present_inode(inode);
2388 * btime on newly-allocated inodes is 0, so if this is still set to
2389 * that, then assume that it's not valid.
2391 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2392 stat->btime = ci->i_btime;
2393 valid_mask |= STATX_BTIME;
2396 if (ceph_snap(inode) == CEPH_NOSNAP)
2397 stat->dev = inode->i_sb->s_dev;
2399 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
2401 if (S_ISDIR(inode->i_mode)) {
2402 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
2404 stat->size = ci->i_rbytes;
2406 stat->size = ci->i_files + ci->i_subdirs;
2408 stat->blksize = 65536;
2410 * Some applications rely on the number of st_nlink
2411 * value on directories to be either 0 (if unlinked)
2412 * or 2 + number of subdirectories.
2414 if (stat->nlink == 1)
2415 /* '.' + '..' + subdirs */
2416 stat->nlink = 1 + 1 + ci->i_subdirs;
2419 stat->result_mask = request_mask & valid_mask;