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 ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
45 inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
46 inode_set_iversion_raw(inode, 0);
50 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
53 ino_t t = ceph_vino_to_ino(vino);
55 inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
57 return ERR_PTR(-ENOMEM);
58 if (inode->i_state & I_NEW)
59 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
60 inode, ceph_vinop(inode), (u64)inode->i_ino);
62 dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
68 * get/constuct snapdir inode for a given directory
70 struct inode *ceph_get_snapdir(struct inode *parent)
72 struct ceph_vino vino = {
73 .ino = ceph_ino(parent),
76 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
77 struct ceph_inode_info *ci = ceph_inode(inode);
79 BUG_ON(!S_ISDIR(parent->i_mode));
82 inode->i_mode = parent->i_mode;
83 inode->i_uid = parent->i_uid;
84 inode->i_gid = parent->i_gid;
85 inode->i_op = &ceph_snapdir_iops;
86 inode->i_fop = &ceph_snapdir_fops;
87 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
90 if (inode->i_state & I_NEW)
91 unlock_new_inode(inode);
96 const struct inode_operations ceph_file_iops = {
97 .permission = ceph_permission,
98 .setattr = ceph_setattr,
99 .getattr = ceph_getattr,
100 .listxattr = ceph_listxattr,
101 .get_acl = ceph_get_acl,
102 .set_acl = ceph_set_acl,
107 * We use a 'frag tree' to keep track of the MDS's directory fragments
108 * for a given inode (usually there is just a single fragment). We
109 * need to know when a child frag is delegated to a new MDS, or when
110 * it is flagged as replicated, so we can direct our requests
115 * find/create a frag in the tree
117 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
121 struct rb_node *parent = NULL;
122 struct ceph_inode_frag *frag;
125 p = &ci->i_fragtree.rb_node;
128 frag = rb_entry(parent, struct ceph_inode_frag, node);
129 c = ceph_frag_compare(f, frag->frag);
138 frag = kmalloc(sizeof(*frag), GFP_NOFS);
140 return ERR_PTR(-ENOMEM);
147 rb_link_node(&frag->node, parent, p);
148 rb_insert_color(&frag->node, &ci->i_fragtree);
150 dout("get_or_create_frag added %llx.%llx frag %x\n",
151 ceph_vinop(&ci->vfs_inode), f);
156 * find a specific frag @f
158 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
160 struct rb_node *n = ci->i_fragtree.rb_node;
163 struct ceph_inode_frag *frag =
164 rb_entry(n, struct ceph_inode_frag, node);
165 int c = ceph_frag_compare(f, frag->frag);
177 * Choose frag containing the given value @v. If @pfrag is
178 * specified, copy the frag delegation info to the caller if
181 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
182 struct ceph_inode_frag *pfrag, int *found)
184 u32 t = ceph_frag_make(0, 0);
185 struct ceph_inode_frag *frag;
193 WARN_ON(!ceph_frag_contains_value(t, v));
194 frag = __ceph_find_frag(ci, t);
196 break; /* t is a leaf */
197 if (frag->split_by == 0) {
199 memcpy(pfrag, frag, sizeof(*pfrag));
206 nway = 1 << frag->split_by;
207 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
208 frag->split_by, nway);
209 for (i = 0; i < nway; i++) {
210 n = ceph_frag_make_child(t, frag->split_by, i);
211 if (ceph_frag_contains_value(n, v)) {
218 dout("choose_frag(%x) = %x\n", v, t);
223 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
224 struct ceph_inode_frag *pfrag, int *found)
227 mutex_lock(&ci->i_fragtree_mutex);
228 ret = __ceph_choose_frag(ci, v, pfrag, found);
229 mutex_unlock(&ci->i_fragtree_mutex);
234 * Process dirfrag (delegation) info from the mds. Include leaf
235 * fragment in tree ONLY if ndist > 0. Otherwise, only
236 * branches/splits are included in i_fragtree)
238 static int ceph_fill_dirfrag(struct inode *inode,
239 struct ceph_mds_reply_dirfrag *dirinfo)
241 struct ceph_inode_info *ci = ceph_inode(inode);
242 struct ceph_inode_frag *frag;
243 u32 id = le32_to_cpu(dirinfo->frag);
244 int mds = le32_to_cpu(dirinfo->auth);
245 int ndist = le32_to_cpu(dirinfo->ndist);
250 spin_lock(&ci->i_ceph_lock);
252 diri_auth = ci->i_auth_cap->mds;
253 spin_unlock(&ci->i_ceph_lock);
255 if (mds == -1) /* CDIR_AUTH_PARENT */
258 mutex_lock(&ci->i_fragtree_mutex);
259 if (ndist == 0 && mds == diri_auth) {
260 /* no delegation info needed. */
261 frag = __ceph_find_frag(ci, id);
264 if (frag->split_by == 0) {
265 /* tree leaf, remove */
266 dout("fill_dirfrag removed %llx.%llx frag %x"
267 " (no ref)\n", ceph_vinop(inode), id);
268 rb_erase(&frag->node, &ci->i_fragtree);
271 /* tree branch, keep and clear */
272 dout("fill_dirfrag cleared %llx.%llx frag %x"
273 " referral\n", ceph_vinop(inode), id);
281 /* find/add this frag to store mds delegation info */
282 frag = __get_or_create_frag(ci, id);
284 /* this is not the end of the world; we can continue
285 with bad/inaccurate delegation info */
286 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
287 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
293 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
294 for (i = 0; i < frag->ndist; i++)
295 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
296 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
297 ceph_vinop(inode), frag->frag, frag->ndist);
300 mutex_unlock(&ci->i_fragtree_mutex);
304 static int frag_tree_split_cmp(const void *l, const void *r)
306 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
307 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
308 return ceph_frag_compare(le32_to_cpu(ls->frag),
309 le32_to_cpu(rs->frag));
312 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
315 return f == ceph_frag_make(0, 0);
316 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
318 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
321 static int ceph_fill_fragtree(struct inode *inode,
322 struct ceph_frag_tree_head *fragtree,
323 struct ceph_mds_reply_dirfrag *dirinfo)
325 struct ceph_inode_info *ci = ceph_inode(inode);
326 struct ceph_inode_frag *frag, *prev_frag = NULL;
327 struct rb_node *rb_node;
328 unsigned i, split_by, nsplits;
332 mutex_lock(&ci->i_fragtree_mutex);
333 nsplits = le32_to_cpu(fragtree->nsplits);
334 if (nsplits != ci->i_fragtree_nsplits) {
336 } else if (nsplits) {
337 i = prandom_u32() % nsplits;
338 id = le32_to_cpu(fragtree->splits[i].frag);
339 if (!__ceph_find_frag(ci, id))
341 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
342 rb_node = rb_first(&ci->i_fragtree);
343 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
344 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
347 if (!update && dirinfo) {
348 id = le32_to_cpu(dirinfo->frag);
349 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
356 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
357 frag_tree_split_cmp, NULL);
360 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
361 rb_node = rb_first(&ci->i_fragtree);
362 for (i = 0; i < nsplits; i++) {
363 id = le32_to_cpu(fragtree->splits[i].frag);
364 split_by = le32_to_cpu(fragtree->splits[i].by);
365 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
366 pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
367 "frag %x split by %d\n", ceph_vinop(inode),
368 i, nsplits, id, split_by);
373 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
374 if (ceph_frag_compare(frag->frag, id) >= 0) {
375 if (frag->frag != id)
378 rb_node = rb_next(rb_node);
381 rb_node = rb_next(rb_node);
382 /* delete stale split/leaf node */
383 if (frag->split_by > 0 ||
384 !is_frag_child(frag->frag, prev_frag)) {
385 rb_erase(&frag->node, &ci->i_fragtree);
386 if (frag->split_by > 0)
387 ci->i_fragtree_nsplits--;
393 frag = __get_or_create_frag(ci, id);
397 if (frag->split_by == 0)
398 ci->i_fragtree_nsplits++;
399 frag->split_by = split_by;
400 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
404 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
405 rb_node = rb_next(rb_node);
406 /* delete stale split/leaf node */
407 if (frag->split_by > 0 ||
408 !is_frag_child(frag->frag, prev_frag)) {
409 rb_erase(&frag->node, &ci->i_fragtree);
410 if (frag->split_by > 0)
411 ci->i_fragtree_nsplits--;
416 mutex_unlock(&ci->i_fragtree_mutex);
421 * initialize a newly allocated inode.
423 struct inode *ceph_alloc_inode(struct super_block *sb)
425 struct ceph_inode_info *ci;
428 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
432 dout("alloc_inode %p\n", &ci->vfs_inode);
434 spin_lock_init(&ci->i_ceph_lock);
437 ci->i_inline_version = 0;
438 ci->i_time_warp_seq = 0;
439 ci->i_ceph_flags = 0;
440 atomic64_set(&ci->i_ordered_count, 1);
441 atomic64_set(&ci->i_release_count, 1);
442 atomic64_set(&ci->i_complete_seq[0], 0);
443 atomic64_set(&ci->i_complete_seq[1], 0);
444 ci->i_symlink = NULL;
449 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
450 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
452 ci->i_fragtree = RB_ROOT;
453 mutex_init(&ci->i_fragtree_mutex);
455 ci->i_xattrs.blob = NULL;
456 ci->i_xattrs.prealloc_blob = NULL;
457 ci->i_xattrs.dirty = false;
458 ci->i_xattrs.index = RB_ROOT;
459 ci->i_xattrs.count = 0;
460 ci->i_xattrs.names_size = 0;
461 ci->i_xattrs.vals_size = 0;
462 ci->i_xattrs.version = 0;
463 ci->i_xattrs.index_version = 0;
465 ci->i_caps = RB_ROOT;
466 ci->i_auth_cap = NULL;
467 ci->i_dirty_caps = 0;
468 ci->i_flushing_caps = 0;
469 INIT_LIST_HEAD(&ci->i_dirty_item);
470 INIT_LIST_HEAD(&ci->i_flushing_item);
471 ci->i_prealloc_cap_flush = NULL;
472 INIT_LIST_HEAD(&ci->i_cap_flush_list);
473 init_waitqueue_head(&ci->i_cap_wq);
474 ci->i_hold_caps_min = 0;
475 ci->i_hold_caps_max = 0;
476 INIT_LIST_HEAD(&ci->i_cap_delay_list);
477 INIT_LIST_HEAD(&ci->i_cap_snaps);
478 ci->i_head_snapc = NULL;
481 for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
482 ci->i_nr_by_mode[i] = 0;
484 mutex_init(&ci->i_truncate_mutex);
485 ci->i_truncate_seq = 0;
486 ci->i_truncate_size = 0;
487 ci->i_truncate_pending = 0;
490 ci->i_reported_size = 0;
491 ci->i_wanted_max_size = 0;
492 ci->i_requested_max_size = 0;
496 ci->i_rdcache_ref = 0;
500 ci->i_wrbuffer_ref = 0;
501 ci->i_wrbuffer_ref_head = 0;
502 atomic_set(&ci->i_filelock_ref, 0);
503 atomic_set(&ci->i_shared_gen, 1);
504 ci->i_rdcache_gen = 0;
505 ci->i_rdcache_revoking = 0;
507 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
508 INIT_LIST_HEAD(&ci->i_unsafe_iops);
509 spin_lock_init(&ci->i_unsafe_lock);
511 ci->i_snap_realm = NULL;
512 INIT_LIST_HEAD(&ci->i_snap_realm_item);
513 INIT_LIST_HEAD(&ci->i_snap_flush_item);
515 INIT_WORK(&ci->i_work, ceph_inode_work);
517 memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
519 ceph_fscache_inode_init(ci);
523 return &ci->vfs_inode;
526 void ceph_free_inode(struct inode *inode)
528 struct ceph_inode_info *ci = ceph_inode(inode);
530 kfree(ci->i_symlink);
531 kmem_cache_free(ceph_inode_cachep, ci);
534 void ceph_evict_inode(struct inode *inode)
536 struct ceph_inode_info *ci = ceph_inode(inode);
537 struct ceph_inode_frag *frag;
540 dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
542 truncate_inode_pages_final(&inode->i_data);
545 ceph_fscache_unregister_inode_cookie(ci);
547 __ceph_remove_caps(ci);
549 if (__ceph_has_any_quota(ci))
550 ceph_adjust_quota_realms_count(inode, false);
553 * we may still have a snap_realm reference if there are stray
554 * caps in i_snap_caps.
556 if (ci->i_snap_realm) {
557 struct ceph_mds_client *mdsc =
558 ceph_inode_to_client(inode)->mdsc;
559 if (ceph_snap(inode) == CEPH_NOSNAP) {
560 struct ceph_snap_realm *realm = ci->i_snap_realm;
561 dout(" dropping residual ref to snap realm %p\n",
563 spin_lock(&realm->inodes_with_caps_lock);
564 list_del_init(&ci->i_snap_realm_item);
565 ci->i_snap_realm = NULL;
566 if (realm->ino == ci->i_vino.ino)
568 spin_unlock(&realm->inodes_with_caps_lock);
569 ceph_put_snap_realm(mdsc, realm);
571 ceph_put_snapid_map(mdsc, ci->i_snapid_map);
572 ci->i_snap_realm = NULL;
576 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
577 frag = rb_entry(n, struct ceph_inode_frag, node);
578 rb_erase(n, &ci->i_fragtree);
581 ci->i_fragtree_nsplits = 0;
583 __ceph_destroy_xattrs(ci);
584 if (ci->i_xattrs.blob)
585 ceph_buffer_put(ci->i_xattrs.blob);
586 if (ci->i_xattrs.prealloc_blob)
587 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
589 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
592 static inline blkcnt_t calc_inode_blocks(u64 size)
594 return (size + (1<<9) - 1) >> 9;
598 * Helpers to fill in size, ctime, mtime, and atime. We have to be
599 * careful because either the client or MDS may have more up to date
600 * info, depending on which capabilities are held, and whether
601 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
602 * and size are monotonically increasing, except when utimes() or
603 * truncate() increments the corresponding _seq values.)
605 int ceph_fill_file_size(struct inode *inode, int issued,
606 u32 truncate_seq, u64 truncate_size, u64 size)
608 struct ceph_inode_info *ci = ceph_inode(inode);
611 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
612 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
613 dout("size %lld -> %llu\n", inode->i_size, size);
614 if (size > 0 && S_ISDIR(inode->i_mode)) {
615 pr_err("fill_file_size non-zero size for directory\n");
618 i_size_write(inode, size);
619 inode->i_blocks = calc_inode_blocks(size);
620 ci->i_reported_size = size;
621 if (truncate_seq != ci->i_truncate_seq) {
622 dout("truncate_seq %u -> %u\n",
623 ci->i_truncate_seq, truncate_seq);
624 ci->i_truncate_seq = truncate_seq;
626 /* the MDS should have revoked these caps */
627 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
630 CEPH_CAP_FILE_LAZYIO));
632 * If we hold relevant caps, or in the case where we're
633 * not the only client referencing this file and we
634 * don't hold those caps, then we need to check whether
635 * the file is either opened or mmaped
637 if ((issued & (CEPH_CAP_FILE_CACHE|
638 CEPH_CAP_FILE_BUFFER)) ||
639 mapping_mapped(inode->i_mapping) ||
640 __ceph_caps_file_wanted(ci)) {
641 ci->i_truncate_pending++;
646 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
647 ci->i_truncate_size != truncate_size) {
648 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
650 ci->i_truncate_size = truncate_size;
654 ceph_fscache_invalidate(inode);
659 void ceph_fill_file_time(struct inode *inode, int issued,
660 u64 time_warp_seq, struct timespec64 *ctime,
661 struct timespec64 *mtime, struct timespec64 *atime)
663 struct ceph_inode_info *ci = ceph_inode(inode);
666 if (issued & (CEPH_CAP_FILE_EXCL|
668 CEPH_CAP_FILE_BUFFER|
670 CEPH_CAP_XATTR_EXCL)) {
671 if (ci->i_version == 0 ||
672 timespec64_compare(ctime, &inode->i_ctime) > 0) {
673 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
674 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
675 ctime->tv_sec, ctime->tv_nsec);
676 inode->i_ctime = *ctime;
678 if (ci->i_version == 0 ||
679 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
680 /* the MDS did a utimes() */
681 dout("mtime %lld.%09ld -> %lld.%09ld "
683 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
684 mtime->tv_sec, mtime->tv_nsec,
685 ci->i_time_warp_seq, (int)time_warp_seq);
687 inode->i_mtime = *mtime;
688 inode->i_atime = *atime;
689 ci->i_time_warp_seq = time_warp_seq;
690 } else if (time_warp_seq == ci->i_time_warp_seq) {
691 /* nobody did utimes(); take the max */
692 if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
693 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
694 inode->i_mtime.tv_sec,
695 inode->i_mtime.tv_nsec,
696 mtime->tv_sec, mtime->tv_nsec);
697 inode->i_mtime = *mtime;
699 if (timespec64_compare(atime, &inode->i_atime) > 0) {
700 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
701 inode->i_atime.tv_sec,
702 inode->i_atime.tv_nsec,
703 atime->tv_sec, atime->tv_nsec);
704 inode->i_atime = *atime;
706 } else if (issued & CEPH_CAP_FILE_EXCL) {
707 /* we did a utimes(); ignore mds values */
712 /* we have no write|excl caps; whatever the MDS says is true */
713 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
714 inode->i_ctime = *ctime;
715 inode->i_mtime = *mtime;
716 inode->i_atime = *atime;
717 ci->i_time_warp_seq = time_warp_seq;
722 if (warn) /* time_warp_seq shouldn't go backwards */
723 dout("%p mds time_warp_seq %llu < %u\n",
724 inode, time_warp_seq, ci->i_time_warp_seq);
728 * Populate an inode based on info from mds. May be called on new or
731 static int fill_inode(struct inode *inode, struct page *locked_page,
732 struct ceph_mds_reply_info_in *iinfo,
733 struct ceph_mds_reply_dirfrag *dirinfo,
734 struct ceph_mds_session *session, int cap_fmode,
735 struct ceph_cap_reservation *caps_reservation)
737 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
738 struct ceph_mds_reply_inode *info = iinfo->in;
739 struct ceph_inode_info *ci = ceph_inode(inode);
740 int issued, new_issued, info_caps;
741 struct timespec64 mtime, atime, ctime;
742 struct ceph_buffer *xattr_blob = NULL;
743 struct ceph_buffer *old_blob = NULL;
744 struct ceph_string *pool_ns = NULL;
745 struct ceph_cap *new_cap = NULL;
748 bool queue_trunc = false;
749 bool new_version = false;
750 bool fill_inline = false;
752 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
753 inode, ceph_vinop(inode), le64_to_cpu(info->version),
756 info_caps = le32_to_cpu(info->cap.caps);
758 /* prealloc new cap struct */
759 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
760 new_cap = ceph_get_cap(mdsc, caps_reservation);
766 * prealloc xattr data, if it looks like we'll need it. only
767 * if len > 4 (meaning there are actually xattrs; the first 4
768 * bytes are the xattr count).
770 if (iinfo->xattr_len > 4) {
771 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
773 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
777 if (iinfo->pool_ns_len > 0)
778 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
781 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
782 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
784 spin_lock(&ci->i_ceph_lock);
787 * provided version will be odd if inode value is projected,
788 * even if stable. skip the update if we have newer stable
789 * info (ours>=theirs, e.g. due to racing mds replies), unless
790 * we are getting projected (unstable) info (in which case the
791 * version is odd, and we want ours>theirs).
797 if (ci->i_version == 0 ||
798 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
799 le64_to_cpu(info->version) > (ci->i_version & ~1)))
802 /* Update change_attribute */
803 inode_set_max_iversion_raw(inode, iinfo->change_attr);
805 __ceph_caps_issued(ci, &issued);
806 issued |= __ceph_caps_dirty(ci);
807 new_issued = ~issued & info_caps;
810 inode->i_rdev = le32_to_cpu(info->rdev);
811 /* directories have fl_stripe_unit set to zero */
812 if (le32_to_cpu(info->layout.fl_stripe_unit))
814 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
816 inode->i_blkbits = CEPH_BLOCK_SHIFT;
818 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
820 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
821 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
822 inode->i_mode = le32_to_cpu(info->mode);
823 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
824 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
825 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
826 from_kuid(&init_user_ns, inode->i_uid),
827 from_kgid(&init_user_ns, inode->i_gid));
828 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
829 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
832 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
833 (issued & CEPH_CAP_LINK_EXCL) == 0)
834 set_nlink(inode, le32_to_cpu(info->nlink));
836 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
837 /* be careful with mtime, atime, size */
838 ceph_decode_timespec64(&atime, &info->atime);
839 ceph_decode_timespec64(&mtime, &info->mtime);
840 ceph_decode_timespec64(&ctime, &info->ctime);
841 ceph_fill_file_time(inode, issued,
842 le32_to_cpu(info->time_warp_seq),
843 &ctime, &mtime, &atime);
846 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
847 ci->i_files = le64_to_cpu(info->files);
848 ci->i_subdirs = le64_to_cpu(info->subdirs);
852 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
853 s64 old_pool = ci->i_layout.pool_id;
854 struct ceph_string *old_ns;
856 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
857 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
858 lockdep_is_held(&ci->i_ceph_lock));
859 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
861 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
862 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
866 queue_trunc = ceph_fill_file_size(inode, issued,
867 le32_to_cpu(info->truncate_seq),
868 le64_to_cpu(info->truncate_size),
869 le64_to_cpu(info->size));
870 /* only update max_size on auth cap */
871 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
872 ci->i_max_size != le64_to_cpu(info->max_size)) {
873 dout("max_size %lld -> %llu\n", ci->i_max_size,
874 le64_to_cpu(info->max_size));
875 ci->i_max_size = le64_to_cpu(info->max_size);
879 /* layout and rstat are not tracked by capability, update them if
880 * the inode info is from auth mds */
881 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
882 if (S_ISDIR(inode->i_mode)) {
883 ci->i_dir_layout = iinfo->dir_layout;
884 ci->i_rbytes = le64_to_cpu(info->rbytes);
885 ci->i_rfiles = le64_to_cpu(info->rfiles);
886 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
887 ci->i_dir_pin = iinfo->dir_pin;
888 ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
893 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
894 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
895 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
896 if (ci->i_xattrs.blob)
897 old_blob = ci->i_xattrs.blob;
898 ci->i_xattrs.blob = xattr_blob;
900 memcpy(ci->i_xattrs.blob->vec.iov_base,
901 iinfo->xattr_data, iinfo->xattr_len);
902 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
903 ceph_forget_all_cached_acls(inode);
904 ceph_security_invalidate_secctx(inode);
908 /* finally update i_version */
909 if (le64_to_cpu(info->version) > ci->i_version)
910 ci->i_version = le64_to_cpu(info->version);
912 inode->i_mapping->a_ops = &ceph_aops;
914 switch (inode->i_mode & S_IFMT) {
919 inode->i_blkbits = PAGE_SHIFT;
920 init_special_inode(inode, inode->i_mode, inode->i_rdev);
921 inode->i_op = &ceph_file_iops;
924 inode->i_op = &ceph_file_iops;
925 inode->i_fop = &ceph_file_fops;
928 inode->i_op = &ceph_symlink_iops;
929 if (!ci->i_symlink) {
930 u32 symlen = iinfo->symlink_len;
933 spin_unlock(&ci->i_ceph_lock);
935 if (symlen != i_size_read(inode)) {
936 pr_err("fill_inode %llx.%llx BAD symlink "
937 "size %lld\n", ceph_vinop(inode),
939 i_size_write(inode, symlen);
940 inode->i_blocks = calc_inode_blocks(symlen);
944 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
948 spin_lock(&ci->i_ceph_lock);
952 kfree(sym); /* lost a race */
954 inode->i_link = ci->i_symlink;
957 inode->i_op = &ceph_dir_iops;
958 inode->i_fop = &ceph_dir_fops;
961 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
962 ceph_vinop(inode), inode->i_mode);
965 /* were we issued a capability? */
967 if (ceph_snap(inode) == CEPH_NOSNAP) {
968 ceph_add_cap(inode, session,
969 le64_to_cpu(info->cap.cap_id),
970 cap_fmode, info_caps,
971 le32_to_cpu(info->cap.wanted),
972 le32_to_cpu(info->cap.seq),
973 le32_to_cpu(info->cap.mseq),
974 le64_to_cpu(info->cap.realm),
975 info->cap.flags, &new_cap);
977 /* set dir completion flag? */
978 if (S_ISDIR(inode->i_mode) &&
979 ci->i_files == 0 && ci->i_subdirs == 0 &&
980 (info_caps & CEPH_CAP_FILE_SHARED) &&
981 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
982 !__ceph_dir_is_complete(ci)) {
983 dout(" marking %p complete (empty)\n", inode);
984 i_size_write(inode, 0);
985 __ceph_dir_set_complete(ci,
986 atomic64_read(&ci->i_release_count),
987 atomic64_read(&ci->i_ordered_count));
992 dout(" %p got snap_caps %s\n", inode,
993 ceph_cap_string(info_caps));
994 ci->i_snap_caps |= info_caps;
996 __ceph_get_fmode(ci, cap_fmode);
998 } else if (cap_fmode >= 0) {
999 pr_warn("mds issued no caps on %llx.%llx\n",
1001 __ceph_get_fmode(ci, cap_fmode);
1004 if (iinfo->inline_version > 0 &&
1005 iinfo->inline_version >= ci->i_inline_version) {
1006 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1007 ci->i_inline_version = iinfo->inline_version;
1008 if (ci->i_inline_version != CEPH_INLINE_NONE &&
1009 (locked_page || (info_caps & cache_caps)))
1013 spin_unlock(&ci->i_ceph_lock);
1016 ceph_fill_inline_data(inode, locked_page,
1017 iinfo->inline_data, iinfo->inline_len);
1020 wake_up_all(&ci->i_cap_wq);
1022 /* queue truncate if we saw i_size decrease */
1024 ceph_queue_vmtruncate(inode);
1026 /* populate frag tree */
1027 if (S_ISDIR(inode->i_mode))
1028 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1030 /* update delegation info? */
1032 ceph_fill_dirfrag(inode, dirinfo);
1037 ceph_put_cap(mdsc, new_cap);
1038 ceph_buffer_put(old_blob);
1039 ceph_buffer_put(xattr_blob);
1040 ceph_put_string(pool_ns);
1045 * caller should hold session s_mutex and dentry->d_lock.
1047 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1048 struct ceph_mds_reply_lease *lease,
1049 struct ceph_mds_session *session,
1050 unsigned long from_time,
1051 struct ceph_mds_session **old_lease_session)
1053 struct ceph_dentry_info *di = ceph_dentry(dentry);
1054 long unsigned duration = le32_to_cpu(lease->duration_ms);
1055 long unsigned ttl = from_time + (duration * HZ) / 1000;
1056 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1058 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1059 dentry, duration, ttl);
1061 /* only track leases on regular dentries */
1062 if (ceph_snap(dir) != CEPH_NOSNAP)
1065 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1066 if (duration == 0) {
1067 __ceph_dentry_dir_lease_touch(di);
1071 if (di->lease_gen == session->s_cap_gen &&
1072 time_before(ttl, di->time))
1073 return; /* we already have a newer lease. */
1075 if (di->lease_session && di->lease_session != session) {
1076 *old_lease_session = di->lease_session;
1077 di->lease_session = NULL;
1080 if (!di->lease_session)
1081 di->lease_session = ceph_get_mds_session(session);
1082 di->lease_gen = session->s_cap_gen;
1083 di->lease_seq = le32_to_cpu(lease->seq);
1084 di->lease_renew_after = half_ttl;
1085 di->lease_renew_from = 0;
1088 __ceph_dentry_lease_touch(di);
1091 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1092 struct ceph_mds_reply_lease *lease,
1093 struct ceph_mds_session *session,
1094 unsigned long from_time)
1096 struct ceph_mds_session *old_lease_session = NULL;
1097 spin_lock(&dentry->d_lock);
1098 __update_dentry_lease(dir, dentry, lease, session, from_time,
1099 &old_lease_session);
1100 spin_unlock(&dentry->d_lock);
1101 if (old_lease_session)
1102 ceph_put_mds_session(old_lease_session);
1106 * update dentry lease without having parent inode locked
1108 static void update_dentry_lease_careful(struct dentry *dentry,
1109 struct ceph_mds_reply_lease *lease,
1110 struct ceph_mds_session *session,
1111 unsigned long from_time,
1112 char *dname, u32 dname_len,
1113 struct ceph_vino *pdvino,
1114 struct ceph_vino *ptvino)
1118 struct ceph_mds_session *old_lease_session = NULL;
1120 spin_lock(&dentry->d_lock);
1121 /* make sure dentry's name matches target */
1122 if (dentry->d_name.len != dname_len ||
1123 memcmp(dentry->d_name.name, dname, dname_len))
1126 dir = d_inode(dentry->d_parent);
1127 /* make sure parent matches dvino */
1128 if (!ceph_ino_compare(dir, pdvino))
1131 /* make sure dentry's inode matches target. NULL ptvino means that
1132 * we expect a negative dentry */
1134 if (d_really_is_negative(dentry))
1136 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1139 if (d_really_is_positive(dentry))
1143 __update_dentry_lease(dir, dentry, lease, session,
1144 from_time, &old_lease_session);
1146 spin_unlock(&dentry->d_lock);
1147 if (old_lease_session)
1148 ceph_put_mds_session(old_lease_session);
1152 * splice a dentry to an inode.
1153 * caller must hold directory i_mutex for this to be safe.
1155 static int splice_dentry(struct dentry **pdn, struct inode *in)
1157 struct dentry *dn = *pdn;
1158 struct dentry *realdn;
1160 BUG_ON(d_inode(dn));
1162 if (S_ISDIR(in->i_mode)) {
1163 /* If inode is directory, d_splice_alias() below will remove
1164 * 'realdn' from its origin parent. We need to ensure that
1165 * origin parent's readdir cache will not reference 'realdn'
1167 realdn = d_find_any_alias(in);
1169 struct ceph_dentry_info *di = ceph_dentry(realdn);
1170 spin_lock(&realdn->d_lock);
1172 realdn->d_op->d_prune(realdn);
1175 di->lease_shared_gen = 0;
1178 spin_unlock(&realdn->d_lock);
1183 /* dn must be unhashed */
1184 if (!d_unhashed(dn))
1186 realdn = d_splice_alias(in, dn);
1187 if (IS_ERR(realdn)) {
1188 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1189 PTR_ERR(realdn), dn, in, ceph_vinop(in));
1190 return PTR_ERR(realdn);
1194 dout("dn %p (%d) spliced with %p (%d) "
1195 "inode %p ino %llx.%llx\n",
1197 realdn, d_count(realdn),
1198 d_inode(realdn), ceph_vinop(d_inode(realdn)));
1202 BUG_ON(!ceph_dentry(dn));
1203 dout("dn %p attached to %p ino %llx.%llx\n",
1204 dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1210 * Incorporate results into the local cache. This is either just
1211 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1214 * A reply may contain
1215 * a directory inode along with a dentry.
1216 * and/or a target inode
1218 * Called with snap_rwsem (read).
1220 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1222 struct ceph_mds_session *session = req->r_session;
1223 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1224 struct inode *in = NULL;
1225 struct ceph_vino tvino, dvino;
1226 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1229 dout("fill_trace %p is_dentry %d is_target %d\n", req,
1230 rinfo->head->is_dentry, rinfo->head->is_target);
1232 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1233 dout("fill_trace reply is empty!\n");
1234 if (rinfo->head->result == 0 && req->r_parent)
1235 ceph_invalidate_dir_request(req);
1239 if (rinfo->head->is_dentry) {
1240 struct inode *dir = req->r_parent;
1243 err = fill_inode(dir, NULL,
1244 &rinfo->diri, rinfo->dirfrag,
1246 &req->r_caps_reservation);
1253 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1254 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1255 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1257 struct dentry *dn, *parent;
1259 BUG_ON(!rinfo->head->is_target);
1260 BUG_ON(req->r_dentry);
1262 parent = d_find_any_alias(dir);
1265 dname.name = rinfo->dname;
1266 dname.len = rinfo->dname_len;
1267 dname.hash = full_name_hash(parent, dname.name, dname.len);
1268 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1269 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1271 dn = d_lookup(parent, &dname);
1272 dout("d_lookup on parent=%p name=%.*s got %p\n",
1273 parent, dname.len, dname.name, dn);
1276 dn = d_alloc(parent, &dname);
1277 dout("d_alloc %p '%.*s' = %p\n", parent,
1278 dname.len, dname.name, dn);
1285 } else if (d_really_is_positive(dn) &&
1286 (ceph_ino(d_inode(dn)) != tvino.ino ||
1287 ceph_snap(d_inode(dn)) != tvino.snap)) {
1288 dout(" dn %p points to wrong inode %p\n",
1290 ceph_dir_clear_ordered(dir);
1301 if (rinfo->head->is_target) {
1302 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1303 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1305 in = ceph_get_inode(sb, tvino);
1311 err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL,
1313 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1314 rinfo->head->result == 0) ? req->r_fmode : -1,
1315 &req->r_caps_reservation);
1317 pr_err("fill_inode badness %p %llx.%llx\n",
1318 in, ceph_vinop(in));
1319 if (in->i_state & I_NEW)
1320 discard_new_inode(in);
1323 req->r_target_inode = in;
1324 if (in->i_state & I_NEW)
1325 unlock_new_inode(in);
1329 * ignore null lease/binding on snapdir ENOENT, or else we
1330 * will have trouble splicing in the virtual snapdir later
1332 if (rinfo->head->is_dentry &&
1333 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1334 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1335 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1336 fsc->mount_options->snapdir_name,
1337 req->r_dentry->d_name.len))) {
1339 * lookup link rename : null -> possibly existing inode
1340 * mknod symlink mkdir : null -> new inode
1341 * unlink : linked -> null
1343 struct inode *dir = req->r_parent;
1344 struct dentry *dn = req->r_dentry;
1345 bool have_dir_cap, have_lease;
1349 BUG_ON(d_inode(dn->d_parent) != dir);
1351 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1352 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1354 BUG_ON(ceph_ino(dir) != dvino.ino);
1355 BUG_ON(ceph_snap(dir) != dvino.snap);
1357 /* do we have a lease on the whole dir? */
1359 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1360 CEPH_CAP_FILE_SHARED);
1362 /* do we have a dn lease? */
1363 have_lease = have_dir_cap ||
1364 le32_to_cpu(rinfo->dlease->duration_ms);
1366 dout("fill_trace no dentry lease or dir cap\n");
1369 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1370 struct inode *olddir = req->r_old_dentry_dir;
1373 dout(" src %p '%pd' dst %p '%pd'\n",
1377 dout("fill_trace doing d_move %p -> %p\n",
1378 req->r_old_dentry, dn);
1380 /* d_move screws up sibling dentries' offsets */
1381 ceph_dir_clear_ordered(dir);
1382 ceph_dir_clear_ordered(olddir);
1384 d_move(req->r_old_dentry, dn);
1385 dout(" src %p '%pd' dst %p '%pd'\n",
1390 /* ensure target dentry is invalidated, despite
1391 rehashing bug in vfs_rename_dir */
1392 ceph_invalidate_dentry_lease(dn);
1394 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1395 ceph_dentry(req->r_old_dentry)->offset);
1397 /* swap r_dentry and r_old_dentry in case that
1398 * splice_dentry() gets called later. This is safe
1399 * because no other place will use them */
1400 req->r_dentry = req->r_old_dentry;
1401 req->r_old_dentry = dn;
1406 if (!rinfo->head->is_target) {
1407 dout("fill_trace null dentry\n");
1408 if (d_really_is_positive(dn)) {
1409 dout("d_delete %p\n", dn);
1410 ceph_dir_clear_ordered(dir);
1412 } else if (have_lease) {
1415 update_dentry_lease(dir, dn,
1416 rinfo->dlease, session,
1417 req->r_request_started);
1422 /* attach proper inode */
1423 if (d_really_is_negative(dn)) {
1424 ceph_dir_clear_ordered(dir);
1426 err = splice_dentry(&req->r_dentry, in);
1429 dn = req->r_dentry; /* may have spliced */
1430 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1431 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1432 dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1439 update_dentry_lease(dir, dn,
1440 rinfo->dlease, session,
1441 req->r_request_started);
1443 dout(" final dn %p\n", dn);
1444 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1445 req->r_op == CEPH_MDS_OP_MKSNAP) &&
1446 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1447 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1448 struct inode *dir = req->r_parent;
1450 /* fill out a snapdir LOOKUPSNAP dentry */
1452 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1453 BUG_ON(!req->r_dentry);
1454 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1455 ceph_dir_clear_ordered(dir);
1457 err = splice_dentry(&req->r_dentry, in);
1460 } else if (rinfo->head->is_dentry && req->r_dentry) {
1461 /* parent inode is not locked, be carefull */
1462 struct ceph_vino *ptvino = NULL;
1463 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1464 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1465 if (rinfo->head->is_target) {
1466 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1467 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1470 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1471 session, req->r_request_started,
1472 rinfo->dname, rinfo->dname_len,
1476 dout("fill_trace done err=%d\n", err);
1481 * Prepopulate our cache with readdir results, leases, etc.
1483 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1484 struct ceph_mds_session *session)
1486 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1489 for (i = 0; i < rinfo->dir_nr; i++) {
1490 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1491 struct ceph_vino vino;
1495 vino.ino = le64_to_cpu(rde->inode.in->ino);
1496 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1498 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1501 dout("new_inode badness got %d\n", err);
1504 rc = fill_inode(in, NULL, &rde->inode, NULL, session,
1505 -1, &req->r_caps_reservation);
1507 pr_err("fill_inode badness on %p got %d\n", in, rc);
1509 if (in->i_state & I_NEW) {
1511 discard_new_inode(in);
1513 } else if (in->i_state & I_NEW) {
1514 unlock_new_inode(in);
1517 /* avoid calling iput_final() in mds dispatch threads */
1518 ceph_async_iput(in);
1524 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1528 put_page(ctl->page);
1533 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1534 struct ceph_readdir_cache_control *ctl,
1535 struct ceph_mds_request *req)
1537 struct ceph_inode_info *ci = ceph_inode(dir);
1538 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1539 unsigned idx = ctl->index % nsize;
1540 pgoff_t pgoff = ctl->index / nsize;
1542 if (!ctl->page || pgoff != page_index(ctl->page)) {
1543 ceph_readdir_cache_release(ctl);
1545 ctl->page = grab_cache_page(&dir->i_data, pgoff);
1547 ctl->page = find_lock_page(&dir->i_data, pgoff);
1550 return idx == 0 ? -ENOMEM : 0;
1552 /* reading/filling the cache are serialized by
1553 * i_mutex, no need to use page lock */
1554 unlock_page(ctl->page);
1555 ctl->dentries = kmap(ctl->page);
1557 memset(ctl->dentries, 0, PAGE_SIZE);
1560 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1561 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1562 dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1563 ctl->dentries[idx] = dn;
1566 dout("disable readdir cache\n");
1572 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1573 struct ceph_mds_session *session)
1575 struct dentry *parent = req->r_dentry;
1576 struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
1577 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1581 int err = 0, skipped = 0, ret, i;
1582 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1583 u32 frag = le32_to_cpu(rhead->args.readdir.frag);
1586 struct ceph_readdir_cache_control cache_ctl = {};
1588 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1589 return readdir_prepopulate_inodes_only(req, session);
1591 if (rinfo->hash_order) {
1593 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1595 strlen(req->r_path2));
1596 last_hash = ceph_frag_value(last_hash);
1597 } else if (rinfo->offset_hash) {
1598 /* mds understands offset_hash */
1599 WARN_ON_ONCE(req->r_readdir_offset != 2);
1600 last_hash = le32_to_cpu(rhead->args.readdir.offset_hash);
1604 if (rinfo->dir_dir &&
1605 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1606 dout("readdir_prepopulate got new frag %x -> %x\n",
1607 frag, le32_to_cpu(rinfo->dir_dir->frag));
1608 frag = le32_to_cpu(rinfo->dir_dir->frag);
1609 if (!rinfo->hash_order)
1610 req->r_readdir_offset = 2;
1613 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1614 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1615 rinfo->dir_nr, parent);
1617 dout("readdir_prepopulate %d items under dn %p\n",
1618 rinfo->dir_nr, parent);
1620 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1622 if (ceph_frag_is_leftmost(frag) &&
1623 req->r_readdir_offset == 2 &&
1624 !(rinfo->hash_order && last_hash)) {
1625 /* note dir version at start of readdir so we can
1626 * tell if any dentries get dropped */
1627 req->r_dir_release_cnt =
1628 atomic64_read(&ci->i_release_count);
1629 req->r_dir_ordered_cnt =
1630 atomic64_read(&ci->i_ordered_count);
1631 req->r_readdir_cache_idx = 0;
1635 cache_ctl.index = req->r_readdir_cache_idx;
1636 fpos_offset = req->r_readdir_offset;
1638 /* FIXME: release caps/leases if error occurs */
1639 for (i = 0; i < rinfo->dir_nr; i++) {
1640 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1641 struct ceph_vino tvino;
1643 dname.name = rde->name;
1644 dname.len = rde->name_len;
1645 dname.hash = full_name_hash(parent, dname.name, dname.len);
1647 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1648 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1650 if (rinfo->hash_order) {
1651 u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1652 rde->name, rde->name_len);
1653 hash = ceph_frag_value(hash);
1654 if (hash != last_hash)
1657 rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1659 rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1663 dn = d_lookup(parent, &dname);
1664 dout("d_lookup on parent=%p name=%.*s got %p\n",
1665 parent, dname.len, dname.name, dn);
1668 dn = d_alloc(parent, &dname);
1669 dout("d_alloc %p '%.*s' = %p\n", parent,
1670 dname.len, dname.name, dn);
1672 dout("d_alloc badness\n");
1676 } else if (d_really_is_positive(dn) &&
1677 (ceph_ino(d_inode(dn)) != tvino.ino ||
1678 ceph_snap(d_inode(dn)) != tvino.snap)) {
1679 struct ceph_dentry_info *di = ceph_dentry(dn);
1680 dout(" dn %p points to wrong inode %p\n",
1683 spin_lock(&dn->d_lock);
1684 if (di->offset > 0 &&
1685 di->lease_shared_gen ==
1686 atomic_read(&ci->i_shared_gen)) {
1687 __ceph_dir_clear_ordered(ci);
1690 spin_unlock(&dn->d_lock);
1698 if (d_really_is_positive(dn)) {
1701 in = ceph_get_inode(parent->d_sb, tvino);
1703 dout("new_inode badness\n");
1711 ret = fill_inode(in, NULL, &rde->inode, NULL, session,
1712 -1, &req->r_caps_reservation);
1714 pr_err("fill_inode badness on %p\n", in);
1715 if (d_really_is_negative(dn)) {
1716 /* avoid calling iput_final() in mds
1717 * dispatch threads */
1718 if (in->i_state & I_NEW) {
1720 discard_new_inode(in);
1722 ceph_async_iput(in);
1728 if (in->i_state & I_NEW)
1729 unlock_new_inode(in);
1731 if (d_really_is_negative(dn)) {
1732 if (ceph_security_xattr_deadlock(in)) {
1733 dout(" skip splicing dn %p to inode %p"
1734 " (security xattr deadlock)\n", dn, in);
1735 ceph_async_iput(in);
1740 err = splice_dentry(&dn, in);
1745 ceph_dentry(dn)->offset = rde->offset;
1747 update_dentry_lease(d_inode(parent), dn,
1748 rde->lease, req->r_session,
1749 req->r_request_started);
1751 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
1752 ret = fill_readdir_cache(d_inode(parent), dn,
1761 if (err == 0 && skipped == 0) {
1762 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
1763 req->r_readdir_cache_idx = cache_ctl.index;
1765 ceph_readdir_cache_release(&cache_ctl);
1766 dout("readdir_prepopulate done\n");
1770 bool ceph_inode_set_size(struct inode *inode, loff_t size)
1772 struct ceph_inode_info *ci = ceph_inode(inode);
1775 spin_lock(&ci->i_ceph_lock);
1776 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1777 i_size_write(inode, size);
1778 inode->i_blocks = calc_inode_blocks(size);
1780 ret = __ceph_should_report_size(ci);
1782 spin_unlock(&ci->i_ceph_lock);
1787 * Put reference to inode, but avoid calling iput_final() in current thread.
1788 * iput_final() may wait for reahahead pages. The wait can cause deadlock in
1791 void ceph_async_iput(struct inode *inode)
1796 if (atomic_add_unless(&inode->i_count, -1, 1))
1798 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1799 &ceph_inode(inode)->i_work))
1801 /* queue work failed, i_count must be at least 2 */
1806 * Write back inode data in a worker thread. (This can't be done
1807 * in the message handler context.)
1809 void ceph_queue_writeback(struct inode *inode)
1811 struct ceph_inode_info *ci = ceph_inode(inode);
1812 set_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask);
1815 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1817 dout("ceph_queue_writeback %p\n", inode);
1819 dout("ceph_queue_writeback %p already queued, mask=%lx\n",
1820 inode, ci->i_work_mask);
1826 * queue an async invalidation
1828 void ceph_queue_invalidate(struct inode *inode)
1830 struct ceph_inode_info *ci = ceph_inode(inode);
1831 set_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask);
1834 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1835 &ceph_inode(inode)->i_work)) {
1836 dout("ceph_queue_invalidate %p\n", inode);
1838 dout("ceph_queue_invalidate %p already queued, mask=%lx\n",
1839 inode, ci->i_work_mask);
1845 * Queue an async vmtruncate. If we fail to queue work, we will handle
1846 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1848 void ceph_queue_vmtruncate(struct inode *inode)
1850 struct ceph_inode_info *ci = ceph_inode(inode);
1851 set_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask);
1854 if (queue_work(ceph_inode_to_client(inode)->inode_wq,
1856 dout("ceph_queue_vmtruncate %p\n", inode);
1858 dout("ceph_queue_vmtruncate %p already queued, mask=%lx\n",
1859 inode, ci->i_work_mask);
1864 static void ceph_do_invalidate_pages(struct inode *inode)
1866 struct ceph_inode_info *ci = ceph_inode(inode);
1867 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1871 mutex_lock(&ci->i_truncate_mutex);
1873 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1874 pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
1875 inode, ceph_ino(inode));
1876 mapping_set_error(inode->i_mapping, -EIO);
1877 truncate_pagecache(inode, 0);
1878 mutex_unlock(&ci->i_truncate_mutex);
1882 spin_lock(&ci->i_ceph_lock);
1883 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1884 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1885 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1886 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1888 spin_unlock(&ci->i_ceph_lock);
1889 mutex_unlock(&ci->i_truncate_mutex);
1892 orig_gen = ci->i_rdcache_gen;
1893 spin_unlock(&ci->i_ceph_lock);
1895 if (invalidate_inode_pages2(inode->i_mapping) < 0) {
1896 pr_err("invalidate_pages %p fails\n", inode);
1899 spin_lock(&ci->i_ceph_lock);
1900 if (orig_gen == ci->i_rdcache_gen &&
1901 orig_gen == ci->i_rdcache_revoking) {
1902 dout("invalidate_pages %p gen %d successful\n", inode,
1904 ci->i_rdcache_revoking--;
1907 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1908 inode, orig_gen, ci->i_rdcache_gen,
1909 ci->i_rdcache_revoking);
1910 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1913 spin_unlock(&ci->i_ceph_lock);
1914 mutex_unlock(&ci->i_truncate_mutex);
1917 ceph_check_caps(ci, 0, NULL);
1921 * Make sure any pending truncation is applied before doing anything
1922 * that may depend on it.
1924 void __ceph_do_pending_vmtruncate(struct inode *inode)
1926 struct ceph_inode_info *ci = ceph_inode(inode);
1928 int wrbuffer_refs, finish = 0;
1930 mutex_lock(&ci->i_truncate_mutex);
1932 spin_lock(&ci->i_ceph_lock);
1933 if (ci->i_truncate_pending == 0) {
1934 dout("__do_pending_vmtruncate %p none pending\n", inode);
1935 spin_unlock(&ci->i_ceph_lock);
1936 mutex_unlock(&ci->i_truncate_mutex);
1941 * make sure any dirty snapped pages are flushed before we
1942 * possibly truncate them.. so write AND block!
1944 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1945 spin_unlock(&ci->i_ceph_lock);
1946 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1948 filemap_write_and_wait_range(&inode->i_data, 0,
1949 inode->i_sb->s_maxbytes);
1953 /* there should be no reader or writer */
1954 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1956 to = ci->i_truncate_size;
1957 wrbuffer_refs = ci->i_wrbuffer_ref;
1958 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1959 ci->i_truncate_pending, to);
1960 spin_unlock(&ci->i_ceph_lock);
1962 truncate_pagecache(inode, to);
1964 spin_lock(&ci->i_ceph_lock);
1965 if (to == ci->i_truncate_size) {
1966 ci->i_truncate_pending = 0;
1969 spin_unlock(&ci->i_ceph_lock);
1973 mutex_unlock(&ci->i_truncate_mutex);
1975 if (wrbuffer_refs == 0)
1976 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1978 wake_up_all(&ci->i_cap_wq);
1981 static void ceph_inode_work(struct work_struct *work)
1983 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1985 struct inode *inode = &ci->vfs_inode;
1987 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
1988 dout("writeback %p\n", inode);
1989 filemap_fdatawrite(&inode->i_data);
1991 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
1992 ceph_do_invalidate_pages(inode);
1994 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
1995 __ceph_do_pending_vmtruncate(inode);
2003 static const struct inode_operations ceph_symlink_iops = {
2004 .get_link = simple_get_link,
2005 .setattr = ceph_setattr,
2006 .getattr = ceph_getattr,
2007 .listxattr = ceph_listxattr,
2010 int __ceph_setattr(struct inode *inode, struct iattr *attr)
2012 struct ceph_inode_info *ci = ceph_inode(inode);
2013 unsigned int ia_valid = attr->ia_valid;
2014 struct ceph_mds_request *req;
2015 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2016 struct ceph_cap_flush *prealloc_cf;
2018 int release = 0, dirtied = 0;
2021 int inode_dirty_flags = 0;
2022 bool lock_snap_rwsem = false;
2024 prealloc_cf = ceph_alloc_cap_flush();
2028 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2031 ceph_free_cap_flush(prealloc_cf);
2032 return PTR_ERR(req);
2035 spin_lock(&ci->i_ceph_lock);
2036 issued = __ceph_caps_issued(ci, NULL);
2038 if (!ci->i_head_snapc &&
2039 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2040 lock_snap_rwsem = true;
2041 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2042 spin_unlock(&ci->i_ceph_lock);
2043 down_read(&mdsc->snap_rwsem);
2044 spin_lock(&ci->i_ceph_lock);
2045 issued = __ceph_caps_issued(ci, NULL);
2049 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2051 if (ia_valid & ATTR_UID) {
2052 dout("setattr %p uid %d -> %d\n", inode,
2053 from_kuid(&init_user_ns, inode->i_uid),
2054 from_kuid(&init_user_ns, attr->ia_uid));
2055 if (issued & CEPH_CAP_AUTH_EXCL) {
2056 inode->i_uid = attr->ia_uid;
2057 dirtied |= CEPH_CAP_AUTH_EXCL;
2058 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2059 !uid_eq(attr->ia_uid, inode->i_uid)) {
2060 req->r_args.setattr.uid = cpu_to_le32(
2061 from_kuid(&init_user_ns, attr->ia_uid));
2062 mask |= CEPH_SETATTR_UID;
2063 release |= CEPH_CAP_AUTH_SHARED;
2066 if (ia_valid & ATTR_GID) {
2067 dout("setattr %p gid %d -> %d\n", inode,
2068 from_kgid(&init_user_ns, inode->i_gid),
2069 from_kgid(&init_user_ns, attr->ia_gid));
2070 if (issued & CEPH_CAP_AUTH_EXCL) {
2071 inode->i_gid = attr->ia_gid;
2072 dirtied |= CEPH_CAP_AUTH_EXCL;
2073 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2074 !gid_eq(attr->ia_gid, inode->i_gid)) {
2075 req->r_args.setattr.gid = cpu_to_le32(
2076 from_kgid(&init_user_ns, attr->ia_gid));
2077 mask |= CEPH_SETATTR_GID;
2078 release |= CEPH_CAP_AUTH_SHARED;
2081 if (ia_valid & ATTR_MODE) {
2082 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2084 if (issued & CEPH_CAP_AUTH_EXCL) {
2085 inode->i_mode = attr->ia_mode;
2086 dirtied |= CEPH_CAP_AUTH_EXCL;
2087 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2088 attr->ia_mode != inode->i_mode) {
2089 inode->i_mode = attr->ia_mode;
2090 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2091 mask |= CEPH_SETATTR_MODE;
2092 release |= CEPH_CAP_AUTH_SHARED;
2096 if (ia_valid & ATTR_ATIME) {
2097 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2098 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2099 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2100 if (issued & CEPH_CAP_FILE_EXCL) {
2101 ci->i_time_warp_seq++;
2102 inode->i_atime = attr->ia_atime;
2103 dirtied |= CEPH_CAP_FILE_EXCL;
2104 } else if ((issued & CEPH_CAP_FILE_WR) &&
2105 timespec64_compare(&inode->i_atime,
2106 &attr->ia_atime) < 0) {
2107 inode->i_atime = attr->ia_atime;
2108 dirtied |= CEPH_CAP_FILE_WR;
2109 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2110 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2111 ceph_encode_timespec64(&req->r_args.setattr.atime,
2113 mask |= CEPH_SETATTR_ATIME;
2114 release |= CEPH_CAP_FILE_SHARED |
2115 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2118 if (ia_valid & ATTR_SIZE) {
2119 dout("setattr %p size %lld -> %lld\n", inode,
2120 inode->i_size, attr->ia_size);
2121 if ((issued & CEPH_CAP_FILE_EXCL) &&
2122 attr->ia_size > inode->i_size) {
2123 i_size_write(inode, attr->ia_size);
2124 inode->i_blocks = calc_inode_blocks(attr->ia_size);
2125 ci->i_reported_size = attr->ia_size;
2126 dirtied |= CEPH_CAP_FILE_EXCL;
2127 ia_valid |= ATTR_MTIME;
2128 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2129 attr->ia_size != inode->i_size) {
2130 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2131 req->r_args.setattr.old_size =
2132 cpu_to_le64(inode->i_size);
2133 mask |= CEPH_SETATTR_SIZE;
2134 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2135 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2138 if (ia_valid & ATTR_MTIME) {
2139 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2140 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2141 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2142 if (issued & CEPH_CAP_FILE_EXCL) {
2143 ci->i_time_warp_seq++;
2144 inode->i_mtime = attr->ia_mtime;
2145 dirtied |= CEPH_CAP_FILE_EXCL;
2146 } else if ((issued & CEPH_CAP_FILE_WR) &&
2147 timespec64_compare(&inode->i_mtime,
2148 &attr->ia_mtime) < 0) {
2149 inode->i_mtime = attr->ia_mtime;
2150 dirtied |= CEPH_CAP_FILE_WR;
2151 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2152 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2153 ceph_encode_timespec64(&req->r_args.setattr.mtime,
2155 mask |= CEPH_SETATTR_MTIME;
2156 release |= CEPH_CAP_FILE_SHARED |
2157 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2161 /* these do nothing */
2162 if (ia_valid & ATTR_CTIME) {
2163 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2164 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2165 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2166 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2167 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2168 only ? "ctime only" : "ignored");
2171 * if kernel wants to dirty ctime but nothing else,
2172 * we need to choose a cap to dirty under, or do
2173 * a almost-no-op setattr
2175 if (issued & CEPH_CAP_AUTH_EXCL)
2176 dirtied |= CEPH_CAP_AUTH_EXCL;
2177 else if (issued & CEPH_CAP_FILE_EXCL)
2178 dirtied |= CEPH_CAP_FILE_EXCL;
2179 else if (issued & CEPH_CAP_XATTR_EXCL)
2180 dirtied |= CEPH_CAP_XATTR_EXCL;
2182 mask |= CEPH_SETATTR_CTIME;
2185 if (ia_valid & ATTR_FILE)
2186 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2189 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2191 inode->i_ctime = attr->ia_ctime;
2195 spin_unlock(&ci->i_ceph_lock);
2196 if (lock_snap_rwsem)
2197 up_read(&mdsc->snap_rwsem);
2199 if (inode_dirty_flags)
2200 __mark_inode_dirty(inode, inode_dirty_flags);
2204 req->r_inode = inode;
2206 req->r_inode_drop = release;
2207 req->r_args.setattr.mask = cpu_to_le32(mask);
2208 req->r_num_caps = 1;
2209 req->r_stamp = attr->ia_ctime;
2210 err = ceph_mdsc_do_request(mdsc, NULL, req);
2212 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2213 ceph_cap_string(dirtied), mask);
2215 ceph_mdsc_put_request(req);
2216 ceph_free_cap_flush(prealloc_cf);
2218 if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2219 __ceph_do_pending_vmtruncate(inode);
2227 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
2229 struct inode *inode = d_inode(dentry);
2230 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2233 if (ceph_snap(inode) != CEPH_NOSNAP)
2236 err = setattr_prepare(dentry, attr);
2240 if ((attr->ia_valid & ATTR_SIZE) &&
2241 attr->ia_size > max(inode->i_size, fsc->max_file_size))
2244 if ((attr->ia_valid & ATTR_SIZE) &&
2245 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2248 err = __ceph_setattr(inode, attr);
2250 if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2251 err = posix_acl_chmod(inode, attr->ia_mode);
2257 * Verify that we have a lease on the given mask. If not,
2258 * do a getattr against an mds.
2260 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2261 int mask, bool force)
2263 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2264 struct ceph_mds_client *mdsc = fsc->mdsc;
2265 struct ceph_mds_request *req;
2269 if (ceph_snap(inode) == CEPH_SNAPDIR) {
2270 dout("do_getattr inode %p SNAPDIR\n", inode);
2274 dout("do_getattr inode %p mask %s mode 0%o\n",
2275 inode, ceph_cap_string(mask), inode->i_mode);
2276 if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
2279 mode = (mask & CEPH_STAT_RSTAT) ? USE_AUTH_MDS : USE_ANY_MDS;
2280 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2282 return PTR_ERR(req);
2283 req->r_inode = inode;
2285 req->r_num_caps = 1;
2286 req->r_args.getattr.mask = cpu_to_le32(mask);
2287 req->r_locked_page = locked_page;
2288 err = ceph_mdsc_do_request(mdsc, NULL, req);
2289 if (locked_page && err == 0) {
2290 u64 inline_version = req->r_reply_info.targeti.inline_version;
2291 if (inline_version == 0) {
2292 /* the reply is supposed to contain inline data */
2294 } else if (inline_version == CEPH_INLINE_NONE) {
2297 err = req->r_reply_info.targeti.inline_len;
2300 ceph_mdsc_put_request(req);
2301 dout("do_getattr result=%d\n", err);
2307 * Check inode permissions. We verify we have a valid value for
2308 * the AUTH cap, then call the generic handler.
2310 int ceph_permission(struct inode *inode, int mask)
2314 if (mask & MAY_NOT_BLOCK)
2317 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2320 err = generic_permission(inode, mask);
2324 /* Craft a mask of needed caps given a set of requested statx attrs. */
2325 static int statx_to_caps(u32 want)
2329 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME))
2330 mask |= CEPH_CAP_AUTH_SHARED;
2332 if (want & (STATX_NLINK|STATX_CTIME))
2333 mask |= CEPH_CAP_LINK_SHARED;
2335 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|
2337 mask |= CEPH_CAP_FILE_SHARED;
2339 if (want & (STATX_CTIME))
2340 mask |= CEPH_CAP_XATTR_SHARED;
2346 * Get all the attributes. If we have sufficient caps for the requested attrs,
2347 * then we can avoid talking to the MDS at all.
2349 int ceph_getattr(const struct path *path, struct kstat *stat,
2350 u32 request_mask, unsigned int flags)
2352 struct inode *inode = d_inode(path->dentry);
2353 struct ceph_inode_info *ci = ceph_inode(inode);
2354 u32 valid_mask = STATX_BASIC_STATS;
2357 /* Skip the getattr altogether if we're asked not to sync */
2358 if (!(flags & AT_STATX_DONT_SYNC)) {
2359 err = ceph_do_getattr(inode, statx_to_caps(request_mask),
2360 flags & AT_STATX_FORCE_SYNC);
2365 generic_fillattr(inode, stat);
2366 stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
2369 * btime on newly-allocated inodes is 0, so if this is still set to
2370 * that, then assume that it's not valid.
2372 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2373 stat->btime = ci->i_btime;
2374 valid_mask |= STATX_BTIME;
2377 if (ceph_snap(inode) == CEPH_NOSNAP)
2378 stat->dev = inode->i_sb->s_dev;
2380 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
2382 if (S_ISDIR(inode->i_mode)) {
2383 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
2385 stat->size = ci->i_rbytes;
2387 stat->size = ci->i_files + ci->i_subdirs;
2389 stat->blksize = 65536;
2391 * Some applications rely on the number of st_nlink
2392 * value on directories to be either 0 (if unlinked)
2393 * or 2 + number of subdirectories.
2395 if (stat->nlink == 1)
2396 /* '.' + '..' + subdirs */
2397 stat->nlink = 1 + 1 + ci->i_subdirs;
2400 stat->result_mask = request_mask & valid_mask;