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;
499 ci
->i_wrbuffer_ref
= 0;
500 ci
->i_wrbuffer_ref_head
= 0;
501 atomic_set(&ci
->i_filelock_ref
, 0);
502 atomic_set(&ci
->i_shared_gen
, 1);
503 ci
->i_rdcache_gen
= 0;
504 ci
->i_rdcache_revoking
= 0;
506 INIT_LIST_HEAD(&ci
->i_unsafe_dirops
);
507 INIT_LIST_HEAD(&ci
->i_unsafe_iops
);
508 spin_lock_init(&ci
->i_unsafe_lock
);
510 ci
->i_snap_realm
= NULL
;
511 INIT_LIST_HEAD(&ci
->i_snap_realm_item
);
512 INIT_LIST_HEAD(&ci
->i_snap_flush_item
);
514 INIT_WORK(&ci
->i_work
, ceph_inode_work
);
516 memset(&ci
->i_btime
, '\0', sizeof(ci
->i_btime
));
518 ceph_fscache_inode_init(ci
);
522 return &ci
->vfs_inode
;
525 void ceph_free_inode(struct inode
*inode
)
527 struct ceph_inode_info
*ci
= ceph_inode(inode
);
529 kfree(ci
->i_symlink
);
530 kmem_cache_free(ceph_inode_cachep
, ci
);
533 void ceph_evict_inode(struct inode
*inode
)
535 struct ceph_inode_info
*ci
= ceph_inode(inode
);
536 struct ceph_inode_frag
*frag
;
539 dout("evict_inode %p ino %llx.%llx\n", inode
, ceph_vinop(inode
));
541 truncate_inode_pages_final(&inode
->i_data
);
544 ceph_fscache_unregister_inode_cookie(ci
);
546 __ceph_remove_caps(ci
);
548 if (__ceph_has_any_quota(ci
))
549 ceph_adjust_quota_realms_count(inode
, false);
552 * we may still have a snap_realm reference if there are stray
553 * caps in i_snap_caps.
555 if (ci
->i_snap_realm
) {
556 struct ceph_mds_client
*mdsc
=
557 ceph_inode_to_client(inode
)->mdsc
;
558 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
559 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
560 dout(" dropping residual ref to snap realm %p\n",
562 spin_lock(&realm
->inodes_with_caps_lock
);
563 list_del_init(&ci
->i_snap_realm_item
);
564 ci
->i_snap_realm
= NULL
;
565 if (realm
->ino
== ci
->i_vino
.ino
)
567 spin_unlock(&realm
->inodes_with_caps_lock
);
568 ceph_put_snap_realm(mdsc
, realm
);
570 ceph_put_snapid_map(mdsc
, ci
->i_snapid_map
);
571 ci
->i_snap_realm
= NULL
;
575 while ((n
= rb_first(&ci
->i_fragtree
)) != NULL
) {
576 frag
= rb_entry(n
, struct ceph_inode_frag
, node
);
577 rb_erase(n
, &ci
->i_fragtree
);
580 ci
->i_fragtree_nsplits
= 0;
582 __ceph_destroy_xattrs(ci
);
583 if (ci
->i_xattrs
.blob
)
584 ceph_buffer_put(ci
->i_xattrs
.blob
);
585 if (ci
->i_xattrs
.prealloc_blob
)
586 ceph_buffer_put(ci
->i_xattrs
.prealloc_blob
);
588 ceph_put_string(rcu_dereference_raw(ci
->i_layout
.pool_ns
));
591 static inline blkcnt_t
calc_inode_blocks(u64 size
)
593 return (size
+ (1<<9) - 1) >> 9;
597 * Helpers to fill in size, ctime, mtime, and atime. We have to be
598 * careful because either the client or MDS may have more up to date
599 * info, depending on which capabilities are held, and whether
600 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
601 * and size are monotonically increasing, except when utimes() or
602 * truncate() increments the corresponding _seq values.)
604 int ceph_fill_file_size(struct inode
*inode
, int issued
,
605 u32 truncate_seq
, u64 truncate_size
, u64 size
)
607 struct ceph_inode_info
*ci
= ceph_inode(inode
);
610 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) > 0 ||
611 (truncate_seq
== ci
->i_truncate_seq
&& size
> inode
->i_size
)) {
612 dout("size %lld -> %llu\n", inode
->i_size
, size
);
613 if (size
> 0 && S_ISDIR(inode
->i_mode
)) {
614 pr_err("fill_file_size non-zero size for directory\n");
617 i_size_write(inode
, size
);
618 inode
->i_blocks
= calc_inode_blocks(size
);
619 ci
->i_reported_size
= size
;
620 if (truncate_seq
!= ci
->i_truncate_seq
) {
621 dout("truncate_seq %u -> %u\n",
622 ci
->i_truncate_seq
, truncate_seq
);
623 ci
->i_truncate_seq
= truncate_seq
;
625 /* the MDS should have revoked these caps */
626 WARN_ON_ONCE(issued
& (CEPH_CAP_FILE_EXCL
|
629 CEPH_CAP_FILE_LAZYIO
));
631 * If we hold relevant caps, or in the case where we're
632 * not the only client referencing this file and we
633 * don't hold those caps, then we need to check whether
634 * the file is either opened or mmaped
636 if ((issued
& (CEPH_CAP_FILE_CACHE
|
637 CEPH_CAP_FILE_BUFFER
)) ||
638 mapping_mapped(inode
->i_mapping
) ||
639 __ceph_caps_file_wanted(ci
)) {
640 ci
->i_truncate_pending
++;
645 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) >= 0 &&
646 ci
->i_truncate_size
!= truncate_size
) {
647 dout("truncate_size %lld -> %llu\n", ci
->i_truncate_size
,
649 ci
->i_truncate_size
= truncate_size
;
653 ceph_fscache_invalidate(inode
);
658 void ceph_fill_file_time(struct inode
*inode
, int issued
,
659 u64 time_warp_seq
, struct timespec64
*ctime
,
660 struct timespec64
*mtime
, struct timespec64
*atime
)
662 struct ceph_inode_info
*ci
= ceph_inode(inode
);
665 if (issued
& (CEPH_CAP_FILE_EXCL
|
667 CEPH_CAP_FILE_BUFFER
|
669 CEPH_CAP_XATTR_EXCL
)) {
670 if (ci
->i_version
== 0 ||
671 timespec64_compare(ctime
, &inode
->i_ctime
) > 0) {
672 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
673 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
674 ctime
->tv_sec
, ctime
->tv_nsec
);
675 inode
->i_ctime
= *ctime
;
677 if (ci
->i_version
== 0 ||
678 ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) > 0) {
679 /* the MDS did a utimes() */
680 dout("mtime %lld.%09ld -> %lld.%09ld "
682 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
683 mtime
->tv_sec
, mtime
->tv_nsec
,
684 ci
->i_time_warp_seq
, (int)time_warp_seq
);
686 inode
->i_mtime
= *mtime
;
687 inode
->i_atime
= *atime
;
688 ci
->i_time_warp_seq
= time_warp_seq
;
689 } else if (time_warp_seq
== ci
->i_time_warp_seq
) {
690 /* nobody did utimes(); take the max */
691 if (timespec64_compare(mtime
, &inode
->i_mtime
) > 0) {
692 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
693 inode
->i_mtime
.tv_sec
,
694 inode
->i_mtime
.tv_nsec
,
695 mtime
->tv_sec
, mtime
->tv_nsec
);
696 inode
->i_mtime
= *mtime
;
698 if (timespec64_compare(atime
, &inode
->i_atime
) > 0) {
699 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
700 inode
->i_atime
.tv_sec
,
701 inode
->i_atime
.tv_nsec
,
702 atime
->tv_sec
, atime
->tv_nsec
);
703 inode
->i_atime
= *atime
;
705 } else if (issued
& CEPH_CAP_FILE_EXCL
) {
706 /* we did a utimes(); ignore mds values */
711 /* we have no write|excl caps; whatever the MDS says is true */
712 if (ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) >= 0) {
713 inode
->i_ctime
= *ctime
;
714 inode
->i_mtime
= *mtime
;
715 inode
->i_atime
= *atime
;
716 ci
->i_time_warp_seq
= time_warp_seq
;
721 if (warn
) /* time_warp_seq shouldn't go backwards */
722 dout("%p mds time_warp_seq %llu < %u\n",
723 inode
, time_warp_seq
, ci
->i_time_warp_seq
);
727 * Populate an inode based on info from mds. May be called on new or
730 static int fill_inode(struct inode
*inode
, struct page
*locked_page
,
731 struct ceph_mds_reply_info_in
*iinfo
,
732 struct ceph_mds_reply_dirfrag
*dirinfo
,
733 struct ceph_mds_session
*session
, int cap_fmode
,
734 struct ceph_cap_reservation
*caps_reservation
)
736 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
737 struct ceph_mds_reply_inode
*info
= iinfo
->in
;
738 struct ceph_inode_info
*ci
= ceph_inode(inode
);
739 int issued
, new_issued
, info_caps
;
740 struct timespec64 mtime
, atime
, ctime
;
741 struct ceph_buffer
*xattr_blob
= NULL
;
742 struct ceph_buffer
*old_blob
= NULL
;
743 struct ceph_string
*pool_ns
= NULL
;
744 struct ceph_cap
*new_cap
= NULL
;
747 bool queue_trunc
= false;
748 bool new_version
= false;
749 bool fill_inline
= false;
751 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
752 inode
, ceph_vinop(inode
), le64_to_cpu(info
->version
),
755 info_caps
= le32_to_cpu(info
->cap
.caps
);
757 /* prealloc new cap struct */
758 if (info_caps
&& ceph_snap(inode
) == CEPH_NOSNAP
) {
759 new_cap
= ceph_get_cap(mdsc
, caps_reservation
);
765 * prealloc xattr data, if it looks like we'll need it. only
766 * if len > 4 (meaning there are actually xattrs; the first 4
767 * bytes are the xattr count).
769 if (iinfo
->xattr_len
> 4) {
770 xattr_blob
= ceph_buffer_new(iinfo
->xattr_len
, GFP_NOFS
);
772 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
776 if (iinfo
->pool_ns_len
> 0)
777 pool_ns
= ceph_find_or_create_string(iinfo
->pool_ns_data
,
780 if (ceph_snap(inode
) != CEPH_NOSNAP
&& !ci
->i_snapid_map
)
781 ci
->i_snapid_map
= ceph_get_snapid_map(mdsc
, ceph_snap(inode
));
783 spin_lock(&ci
->i_ceph_lock
);
786 * provided version will be odd if inode value is projected,
787 * even if stable. skip the update if we have newer stable
788 * info (ours>=theirs, e.g. due to racing mds replies), unless
789 * we are getting projected (unstable) info (in which case the
790 * version is odd, and we want ours>theirs).
796 if (ci
->i_version
== 0 ||
797 ((info
->cap
.flags
& CEPH_CAP_FLAG_AUTH
) &&
798 le64_to_cpu(info
->version
) > (ci
->i_version
& ~1)))
801 /* Update change_attribute */
802 inode_set_max_iversion_raw(inode
, iinfo
->change_attr
);
804 __ceph_caps_issued(ci
, &issued
);
805 issued
|= __ceph_caps_dirty(ci
);
806 new_issued
= ~issued
& info_caps
;
809 inode
->i_rdev
= le32_to_cpu(info
->rdev
);
810 /* directories have fl_stripe_unit set to zero */
811 if (le32_to_cpu(info
->layout
.fl_stripe_unit
))
813 fls(le32_to_cpu(info
->layout
.fl_stripe_unit
)) - 1;
815 inode
->i_blkbits
= CEPH_BLOCK_SHIFT
;
817 __ceph_update_quota(ci
, iinfo
->max_bytes
, iinfo
->max_files
);
819 if ((new_version
|| (new_issued
& CEPH_CAP_AUTH_SHARED
)) &&
820 (issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
821 inode
->i_mode
= le32_to_cpu(info
->mode
);
822 inode
->i_uid
= make_kuid(&init_user_ns
, le32_to_cpu(info
->uid
));
823 inode
->i_gid
= make_kgid(&init_user_ns
, le32_to_cpu(info
->gid
));
824 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
825 from_kuid(&init_user_ns
, inode
->i_uid
),
826 from_kgid(&init_user_ns
, inode
->i_gid
));
827 ceph_decode_timespec64(&ci
->i_btime
, &iinfo
->btime
);
828 ceph_decode_timespec64(&ci
->i_snap_btime
, &iinfo
->snap_btime
);
831 if ((new_version
|| (new_issued
& CEPH_CAP_LINK_SHARED
)) &&
832 (issued
& CEPH_CAP_LINK_EXCL
) == 0)
833 set_nlink(inode
, le32_to_cpu(info
->nlink
));
835 if (new_version
|| (new_issued
& CEPH_CAP_ANY_RD
)) {
836 /* be careful with mtime, atime, size */
837 ceph_decode_timespec64(&atime
, &info
->atime
);
838 ceph_decode_timespec64(&mtime
, &info
->mtime
);
839 ceph_decode_timespec64(&ctime
, &info
->ctime
);
840 ceph_fill_file_time(inode
, issued
,
841 le32_to_cpu(info
->time_warp_seq
),
842 &ctime
, &mtime
, &atime
);
845 if (new_version
|| (info_caps
& CEPH_CAP_FILE_SHARED
)) {
846 ci
->i_files
= le64_to_cpu(info
->files
);
847 ci
->i_subdirs
= le64_to_cpu(info
->subdirs
);
851 (new_issued
& (CEPH_CAP_ANY_FILE_RD
| CEPH_CAP_ANY_FILE_WR
))) {
852 s64 old_pool
= ci
->i_layout
.pool_id
;
853 struct ceph_string
*old_ns
;
855 ceph_file_layout_from_legacy(&ci
->i_layout
, &info
->layout
);
856 old_ns
= rcu_dereference_protected(ci
->i_layout
.pool_ns
,
857 lockdep_is_held(&ci
->i_ceph_lock
));
858 rcu_assign_pointer(ci
->i_layout
.pool_ns
, pool_ns
);
860 if (ci
->i_layout
.pool_id
!= old_pool
|| pool_ns
!= old_ns
)
861 ci
->i_ceph_flags
&= ~CEPH_I_POOL_PERM
;
865 queue_trunc
= ceph_fill_file_size(inode
, issued
,
866 le32_to_cpu(info
->truncate_seq
),
867 le64_to_cpu(info
->truncate_size
),
868 le64_to_cpu(info
->size
));
869 /* only update max_size on auth cap */
870 if ((info
->cap
.flags
& CEPH_CAP_FLAG_AUTH
) &&
871 ci
->i_max_size
!= le64_to_cpu(info
->max_size
)) {
872 dout("max_size %lld -> %llu\n", ci
->i_max_size
,
873 le64_to_cpu(info
->max_size
));
874 ci
->i_max_size
= le64_to_cpu(info
->max_size
);
878 /* layout and rstat are not tracked by capability, update them if
879 * the inode info is from auth mds */
880 if (new_version
|| (info
->cap
.flags
& CEPH_CAP_FLAG_AUTH
)) {
881 if (S_ISDIR(inode
->i_mode
)) {
882 ci
->i_dir_layout
= iinfo
->dir_layout
;
883 ci
->i_rbytes
= le64_to_cpu(info
->rbytes
);
884 ci
->i_rfiles
= le64_to_cpu(info
->rfiles
);
885 ci
->i_rsubdirs
= le64_to_cpu(info
->rsubdirs
);
886 ci
->i_dir_pin
= iinfo
->dir_pin
;
887 ceph_decode_timespec64(&ci
->i_rctime
, &info
->rctime
);
892 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
893 if ((ci
->i_xattrs
.version
== 0 || !(issued
& CEPH_CAP_XATTR_EXCL
)) &&
894 le64_to_cpu(info
->xattr_version
) > ci
->i_xattrs
.version
) {
895 if (ci
->i_xattrs
.blob
)
896 old_blob
= ci
->i_xattrs
.blob
;
897 ci
->i_xattrs
.blob
= xattr_blob
;
899 memcpy(ci
->i_xattrs
.blob
->vec
.iov_base
,
900 iinfo
->xattr_data
, iinfo
->xattr_len
);
901 ci
->i_xattrs
.version
= le64_to_cpu(info
->xattr_version
);
902 ceph_forget_all_cached_acls(inode
);
903 ceph_security_invalidate_secctx(inode
);
907 /* finally update i_version */
908 if (le64_to_cpu(info
->version
) > ci
->i_version
)
909 ci
->i_version
= le64_to_cpu(info
->version
);
911 inode
->i_mapping
->a_ops
= &ceph_aops
;
913 switch (inode
->i_mode
& S_IFMT
) {
918 inode
->i_blkbits
= PAGE_SHIFT
;
919 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
920 inode
->i_op
= &ceph_file_iops
;
923 inode
->i_op
= &ceph_file_iops
;
924 inode
->i_fop
= &ceph_file_fops
;
927 inode
->i_op
= &ceph_symlink_iops
;
928 if (!ci
->i_symlink
) {
929 u32 symlen
= iinfo
->symlink_len
;
932 spin_unlock(&ci
->i_ceph_lock
);
934 if (symlen
!= i_size_read(inode
)) {
935 pr_err("fill_inode %llx.%llx BAD symlink "
936 "size %lld\n", ceph_vinop(inode
),
938 i_size_write(inode
, symlen
);
939 inode
->i_blocks
= calc_inode_blocks(symlen
);
943 sym
= kstrndup(iinfo
->symlink
, symlen
, GFP_NOFS
);
947 spin_lock(&ci
->i_ceph_lock
);
951 kfree(sym
); /* lost a race */
953 inode
->i_link
= ci
->i_symlink
;
956 inode
->i_op
= &ceph_dir_iops
;
957 inode
->i_fop
= &ceph_dir_fops
;
960 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
961 ceph_vinop(inode
), inode
->i_mode
);
964 /* were we issued a capability? */
966 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
967 ceph_add_cap(inode
, session
,
968 le64_to_cpu(info
->cap
.cap_id
),
969 cap_fmode
, info_caps
,
970 le32_to_cpu(info
->cap
.wanted
),
971 le32_to_cpu(info
->cap
.seq
),
972 le32_to_cpu(info
->cap
.mseq
),
973 le64_to_cpu(info
->cap
.realm
),
974 info
->cap
.flags
, &new_cap
);
976 /* set dir completion flag? */
977 if (S_ISDIR(inode
->i_mode
) &&
978 ci
->i_files
== 0 && ci
->i_subdirs
== 0 &&
979 (info_caps
& CEPH_CAP_FILE_SHARED
) &&
980 (issued
& CEPH_CAP_FILE_EXCL
) == 0 &&
981 !__ceph_dir_is_complete(ci
)) {
982 dout(" marking %p complete (empty)\n", inode
);
983 i_size_write(inode
, 0);
984 __ceph_dir_set_complete(ci
,
985 atomic64_read(&ci
->i_release_count
),
986 atomic64_read(&ci
->i_ordered_count
));
991 dout(" %p got snap_caps %s\n", inode
,
992 ceph_cap_string(info_caps
));
993 ci
->i_snap_caps
|= info_caps
;
995 __ceph_get_fmode(ci
, cap_fmode
);
997 } else if (cap_fmode
>= 0) {
998 pr_warn("mds issued no caps on %llx.%llx\n",
1000 __ceph_get_fmode(ci
, cap_fmode
);
1003 if (iinfo
->inline_version
> 0 &&
1004 iinfo
->inline_version
>= ci
->i_inline_version
) {
1005 int cache_caps
= CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_LAZYIO
;
1006 ci
->i_inline_version
= iinfo
->inline_version
;
1007 if (ci
->i_inline_version
!= CEPH_INLINE_NONE
&&
1008 (locked_page
|| (info_caps
& cache_caps
)))
1012 spin_unlock(&ci
->i_ceph_lock
);
1015 ceph_fill_inline_data(inode
, locked_page
,
1016 iinfo
->inline_data
, iinfo
->inline_len
);
1019 wake_up_all(&ci
->i_cap_wq
);
1021 /* queue truncate if we saw i_size decrease */
1023 ceph_queue_vmtruncate(inode
);
1025 /* populate frag tree */
1026 if (S_ISDIR(inode
->i_mode
))
1027 ceph_fill_fragtree(inode
, &info
->fragtree
, dirinfo
);
1029 /* update delegation info? */
1031 ceph_fill_dirfrag(inode
, dirinfo
);
1036 ceph_put_cap(mdsc
, new_cap
);
1037 ceph_buffer_put(old_blob
);
1038 ceph_buffer_put(xattr_blob
);
1039 ceph_put_string(pool_ns
);
1044 * caller should hold session s_mutex and dentry->d_lock.
1046 static void __update_dentry_lease(struct inode
*dir
, struct dentry
*dentry
,
1047 struct ceph_mds_reply_lease
*lease
,
1048 struct ceph_mds_session
*session
,
1049 unsigned long from_time
,
1050 struct ceph_mds_session
**old_lease_session
)
1052 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
1053 long unsigned duration
= le32_to_cpu(lease
->duration_ms
);
1054 long unsigned ttl
= from_time
+ (duration
* HZ
) / 1000;
1055 long unsigned half_ttl
= from_time
+ (duration
* HZ
/ 2) / 1000;
1057 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1058 dentry
, duration
, ttl
);
1060 /* only track leases on regular dentries */
1061 if (ceph_snap(dir
) != CEPH_NOSNAP
)
1064 di
->lease_shared_gen
= atomic_read(&ceph_inode(dir
)->i_shared_gen
);
1065 if (duration
== 0) {
1066 __ceph_dentry_dir_lease_touch(di
);
1070 if (di
->lease_gen
== session
->s_cap_gen
&&
1071 time_before(ttl
, di
->time
))
1072 return; /* we already have a newer lease. */
1074 if (di
->lease_session
&& di
->lease_session
!= session
) {
1075 *old_lease_session
= di
->lease_session
;
1076 di
->lease_session
= NULL
;
1079 if (!di
->lease_session
)
1080 di
->lease_session
= ceph_get_mds_session(session
);
1081 di
->lease_gen
= session
->s_cap_gen
;
1082 di
->lease_seq
= le32_to_cpu(lease
->seq
);
1083 di
->lease_renew_after
= half_ttl
;
1084 di
->lease_renew_from
= 0;
1087 __ceph_dentry_lease_touch(di
);
1090 static inline void update_dentry_lease(struct inode
*dir
, struct dentry
*dentry
,
1091 struct ceph_mds_reply_lease
*lease
,
1092 struct ceph_mds_session
*session
,
1093 unsigned long from_time
)
1095 struct ceph_mds_session
*old_lease_session
= NULL
;
1096 spin_lock(&dentry
->d_lock
);
1097 __update_dentry_lease(dir
, dentry
, lease
, session
, from_time
,
1098 &old_lease_session
);
1099 spin_unlock(&dentry
->d_lock
);
1100 if (old_lease_session
)
1101 ceph_put_mds_session(old_lease_session
);
1105 * update dentry lease without having parent inode locked
1107 static void update_dentry_lease_careful(struct dentry
*dentry
,
1108 struct ceph_mds_reply_lease
*lease
,
1109 struct ceph_mds_session
*session
,
1110 unsigned long from_time
,
1111 char *dname
, u32 dname_len
,
1112 struct ceph_vino
*pdvino
,
1113 struct ceph_vino
*ptvino
)
1117 struct ceph_mds_session
*old_lease_session
= NULL
;
1119 spin_lock(&dentry
->d_lock
);
1120 /* make sure dentry's name matches target */
1121 if (dentry
->d_name
.len
!= dname_len
||
1122 memcmp(dentry
->d_name
.name
, dname
, dname_len
))
1125 dir
= d_inode(dentry
->d_parent
);
1126 /* make sure parent matches dvino */
1127 if (!ceph_ino_compare(dir
, pdvino
))
1130 /* make sure dentry's inode matches target. NULL ptvino means that
1131 * we expect a negative dentry */
1133 if (d_really_is_negative(dentry
))
1135 if (!ceph_ino_compare(d_inode(dentry
), ptvino
))
1138 if (d_really_is_positive(dentry
))
1142 __update_dentry_lease(dir
, dentry
, lease
, session
,
1143 from_time
, &old_lease_session
);
1145 spin_unlock(&dentry
->d_lock
);
1146 if (old_lease_session
)
1147 ceph_put_mds_session(old_lease_session
);
1151 * splice a dentry to an inode.
1152 * caller must hold directory i_mutex for this to be safe.
1154 static int splice_dentry(struct dentry
**pdn
, struct inode
*in
)
1156 struct dentry
*dn
= *pdn
;
1157 struct dentry
*realdn
;
1159 BUG_ON(d_inode(dn
));
1161 if (S_ISDIR(in
->i_mode
)) {
1162 /* If inode is directory, d_splice_alias() below will remove
1163 * 'realdn' from its origin parent. We need to ensure that
1164 * origin parent's readdir cache will not reference 'realdn'
1166 realdn
= d_find_any_alias(in
);
1168 struct ceph_dentry_info
*di
= ceph_dentry(realdn
);
1169 spin_lock(&realdn
->d_lock
);
1171 realdn
->d_op
->d_prune(realdn
);
1174 di
->lease_shared_gen
= 0;
1177 spin_unlock(&realdn
->d_lock
);
1182 /* dn must be unhashed */
1183 if (!d_unhashed(dn
))
1185 realdn
= d_splice_alias(in
, dn
);
1186 if (IS_ERR(realdn
)) {
1187 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1188 PTR_ERR(realdn
), dn
, in
, ceph_vinop(in
));
1189 return PTR_ERR(realdn
);
1193 dout("dn %p (%d) spliced with %p (%d) "
1194 "inode %p ino %llx.%llx\n",
1196 realdn
, d_count(realdn
),
1197 d_inode(realdn
), ceph_vinop(d_inode(realdn
)));
1201 BUG_ON(!ceph_dentry(dn
));
1202 dout("dn %p attached to %p ino %llx.%llx\n",
1203 dn
, d_inode(dn
), ceph_vinop(d_inode(dn
)));
1209 * Incorporate results into the local cache. This is either just
1210 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1213 * A reply may contain
1214 * a directory inode along with a dentry.
1215 * and/or a target inode
1217 * Called with snap_rwsem (read).
1219 int ceph_fill_trace(struct super_block
*sb
, struct ceph_mds_request
*req
)
1221 struct ceph_mds_session
*session
= req
->r_session
;
1222 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1223 struct inode
*in
= NULL
;
1224 struct ceph_vino tvino
, dvino
;
1225 struct ceph_fs_client
*fsc
= ceph_sb_to_client(sb
);
1228 dout("fill_trace %p is_dentry %d is_target %d\n", req
,
1229 rinfo
->head
->is_dentry
, rinfo
->head
->is_target
);
1231 if (!rinfo
->head
->is_target
&& !rinfo
->head
->is_dentry
) {
1232 dout("fill_trace reply is empty!\n");
1233 if (rinfo
->head
->result
== 0 && req
->r_parent
)
1234 ceph_invalidate_dir_request(req
);
1238 if (rinfo
->head
->is_dentry
) {
1239 struct inode
*dir
= req
->r_parent
;
1242 err
= fill_inode(dir
, NULL
,
1243 &rinfo
->diri
, rinfo
->dirfrag
,
1245 &req
->r_caps_reservation
);
1252 if (dir
&& req
->r_op
== CEPH_MDS_OP_LOOKUPNAME
&&
1253 test_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
) &&
1254 !test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
)) {
1256 struct dentry
*dn
, *parent
;
1258 BUG_ON(!rinfo
->head
->is_target
);
1259 BUG_ON(req
->r_dentry
);
1261 parent
= d_find_any_alias(dir
);
1264 dname
.name
= rinfo
->dname
;
1265 dname
.len
= rinfo
->dname_len
;
1266 dname
.hash
= full_name_hash(parent
, dname
.name
, dname
.len
);
1267 tvino
.ino
= le64_to_cpu(rinfo
->targeti
.in
->ino
);
1268 tvino
.snap
= le64_to_cpu(rinfo
->targeti
.in
->snapid
);
1270 dn
= d_lookup(parent
, &dname
);
1271 dout("d_lookup on parent=%p name=%.*s got %p\n",
1272 parent
, dname
.len
, dname
.name
, dn
);
1275 dn
= d_alloc(parent
, &dname
);
1276 dout("d_alloc %p '%.*s' = %p\n", parent
,
1277 dname
.len
, dname
.name
, dn
);
1284 } else if (d_really_is_positive(dn
) &&
1285 (ceph_ino(d_inode(dn
)) != tvino
.ino
||
1286 ceph_snap(d_inode(dn
)) != tvino
.snap
)) {
1287 dout(" dn %p points to wrong inode %p\n",
1289 ceph_dir_clear_ordered(dir
);
1300 if (rinfo
->head
->is_target
) {
1301 tvino
.ino
= le64_to_cpu(rinfo
->targeti
.in
->ino
);
1302 tvino
.snap
= le64_to_cpu(rinfo
->targeti
.in
->snapid
);
1304 in
= ceph_get_inode(sb
, tvino
);
1310 err
= fill_inode(in
, req
->r_locked_page
, &rinfo
->targeti
, NULL
,
1312 (!test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
) &&
1313 rinfo
->head
->result
== 0) ? req
->r_fmode
: -1,
1314 &req
->r_caps_reservation
);
1316 pr_err("fill_inode badness %p %llx.%llx\n",
1317 in
, ceph_vinop(in
));
1318 if (in
->i_state
& I_NEW
)
1319 discard_new_inode(in
);
1322 req
->r_target_inode
= in
;
1323 if (in
->i_state
& I_NEW
)
1324 unlock_new_inode(in
);
1328 * ignore null lease/binding on snapdir ENOENT, or else we
1329 * will have trouble splicing in the virtual snapdir later
1331 if (rinfo
->head
->is_dentry
&&
1332 !test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
) &&
1333 test_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
) &&
1334 (rinfo
->head
->is_target
|| strncmp(req
->r_dentry
->d_name
.name
,
1335 fsc
->mount_options
->snapdir_name
,
1336 req
->r_dentry
->d_name
.len
))) {
1338 * lookup link rename : null -> possibly existing inode
1339 * mknod symlink mkdir : null -> new inode
1340 * unlink : linked -> null
1342 struct inode
*dir
= req
->r_parent
;
1343 struct dentry
*dn
= req
->r_dentry
;
1344 bool have_dir_cap
, have_lease
;
1348 BUG_ON(d_inode(dn
->d_parent
) != dir
);
1350 dvino
.ino
= le64_to_cpu(rinfo
->diri
.in
->ino
);
1351 dvino
.snap
= le64_to_cpu(rinfo
->diri
.in
->snapid
);
1353 BUG_ON(ceph_ino(dir
) != dvino
.ino
);
1354 BUG_ON(ceph_snap(dir
) != dvino
.snap
);
1356 /* do we have a lease on the whole dir? */
1358 (le32_to_cpu(rinfo
->diri
.in
->cap
.caps
) &
1359 CEPH_CAP_FILE_SHARED
);
1361 /* do we have a dn lease? */
1362 have_lease
= have_dir_cap
||
1363 le32_to_cpu(rinfo
->dlease
->duration_ms
);
1365 dout("fill_trace no dentry lease or dir cap\n");
1368 if (req
->r_old_dentry
&& req
->r_op
== CEPH_MDS_OP_RENAME
) {
1369 struct inode
*olddir
= req
->r_old_dentry_dir
;
1372 dout(" src %p '%pd' dst %p '%pd'\n",
1376 dout("fill_trace doing d_move %p -> %p\n",
1377 req
->r_old_dentry
, dn
);
1379 /* d_move screws up sibling dentries' offsets */
1380 ceph_dir_clear_ordered(dir
);
1381 ceph_dir_clear_ordered(olddir
);
1383 d_move(req
->r_old_dentry
, dn
);
1384 dout(" src %p '%pd' dst %p '%pd'\n",
1389 /* ensure target dentry is invalidated, despite
1390 rehashing bug in vfs_rename_dir */
1391 ceph_invalidate_dentry_lease(dn
);
1393 dout("dn %p gets new offset %lld\n", req
->r_old_dentry
,
1394 ceph_dentry(req
->r_old_dentry
)->offset
);
1396 /* swap r_dentry and r_old_dentry in case that
1397 * splice_dentry() gets called later. This is safe
1398 * because no other place will use them */
1399 req
->r_dentry
= req
->r_old_dentry
;
1400 req
->r_old_dentry
= dn
;
1405 if (!rinfo
->head
->is_target
) {
1406 dout("fill_trace null dentry\n");
1407 if (d_really_is_positive(dn
)) {
1408 dout("d_delete %p\n", dn
);
1409 ceph_dir_clear_ordered(dir
);
1411 } else if (have_lease
) {
1414 update_dentry_lease(dir
, dn
,
1415 rinfo
->dlease
, session
,
1416 req
->r_request_started
);
1421 /* attach proper inode */
1422 if (d_really_is_negative(dn
)) {
1423 ceph_dir_clear_ordered(dir
);
1425 err
= splice_dentry(&req
->r_dentry
, in
);
1428 dn
= req
->r_dentry
; /* may have spliced */
1429 } else if (d_really_is_positive(dn
) && d_inode(dn
) != in
) {
1430 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1431 dn
, d_inode(dn
), ceph_vinop(d_inode(dn
)),
1438 update_dentry_lease(dir
, dn
,
1439 rinfo
->dlease
, session
,
1440 req
->r_request_started
);
1442 dout(" final dn %p\n", dn
);
1443 } else if ((req
->r_op
== CEPH_MDS_OP_LOOKUPSNAP
||
1444 req
->r_op
== CEPH_MDS_OP_MKSNAP
) &&
1445 test_bit(CEPH_MDS_R_PARENT_LOCKED
, &req
->r_req_flags
) &&
1446 !test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
)) {
1447 struct inode
*dir
= req
->r_parent
;
1449 /* fill out a snapdir LOOKUPSNAP dentry */
1451 BUG_ON(ceph_snap(dir
) != CEPH_SNAPDIR
);
1452 BUG_ON(!req
->r_dentry
);
1453 dout(" linking snapped dir %p to dn %p\n", in
, req
->r_dentry
);
1454 ceph_dir_clear_ordered(dir
);
1456 err
= splice_dentry(&req
->r_dentry
, in
);
1459 } else if (rinfo
->head
->is_dentry
&& req
->r_dentry
) {
1460 /* parent inode is not locked, be carefull */
1461 struct ceph_vino
*ptvino
= NULL
;
1462 dvino
.ino
= le64_to_cpu(rinfo
->diri
.in
->ino
);
1463 dvino
.snap
= le64_to_cpu(rinfo
->diri
.in
->snapid
);
1464 if (rinfo
->head
->is_target
) {
1465 tvino
.ino
= le64_to_cpu(rinfo
->targeti
.in
->ino
);
1466 tvino
.snap
= le64_to_cpu(rinfo
->targeti
.in
->snapid
);
1469 update_dentry_lease_careful(req
->r_dentry
, rinfo
->dlease
,
1470 session
, req
->r_request_started
,
1471 rinfo
->dname
, rinfo
->dname_len
,
1475 dout("fill_trace done err=%d\n", err
);
1480 * Prepopulate our cache with readdir results, leases, etc.
1482 static int readdir_prepopulate_inodes_only(struct ceph_mds_request
*req
,
1483 struct ceph_mds_session
*session
)
1485 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1488 for (i
= 0; i
< rinfo
->dir_nr
; i
++) {
1489 struct ceph_mds_reply_dir_entry
*rde
= rinfo
->dir_entries
+ i
;
1490 struct ceph_vino vino
;
1494 vino
.ino
= le64_to_cpu(rde
->inode
.in
->ino
);
1495 vino
.snap
= le64_to_cpu(rde
->inode
.in
->snapid
);
1497 in
= ceph_get_inode(req
->r_dentry
->d_sb
, vino
);
1500 dout("new_inode badness got %d\n", err
);
1503 rc
= fill_inode(in
, NULL
, &rde
->inode
, NULL
, session
,
1504 -1, &req
->r_caps_reservation
);
1506 pr_err("fill_inode badness on %p got %d\n", in
, rc
);
1508 if (in
->i_state
& I_NEW
) {
1510 discard_new_inode(in
);
1512 } else if (in
->i_state
& I_NEW
) {
1513 unlock_new_inode(in
);
1516 /* avoid calling iput_final() in mds dispatch threads */
1517 ceph_async_iput(in
);
1523 void ceph_readdir_cache_release(struct ceph_readdir_cache_control
*ctl
)
1527 put_page(ctl
->page
);
1532 static int fill_readdir_cache(struct inode
*dir
, struct dentry
*dn
,
1533 struct ceph_readdir_cache_control
*ctl
,
1534 struct ceph_mds_request
*req
)
1536 struct ceph_inode_info
*ci
= ceph_inode(dir
);
1537 unsigned nsize
= PAGE_SIZE
/ sizeof(struct dentry
*);
1538 unsigned idx
= ctl
->index
% nsize
;
1539 pgoff_t pgoff
= ctl
->index
/ nsize
;
1541 if (!ctl
->page
|| pgoff
!= page_index(ctl
->page
)) {
1542 ceph_readdir_cache_release(ctl
);
1544 ctl
->page
= grab_cache_page(&dir
->i_data
, pgoff
);
1546 ctl
->page
= find_lock_page(&dir
->i_data
, pgoff
);
1549 return idx
== 0 ? -ENOMEM
: 0;
1551 /* reading/filling the cache are serialized by
1552 * i_mutex, no need to use page lock */
1553 unlock_page(ctl
->page
);
1554 ctl
->dentries
= kmap(ctl
->page
);
1556 memset(ctl
->dentries
, 0, PAGE_SIZE
);
1559 if (req
->r_dir_release_cnt
== atomic64_read(&ci
->i_release_count
) &&
1560 req
->r_dir_ordered_cnt
== atomic64_read(&ci
->i_ordered_count
)) {
1561 dout("readdir cache dn %p idx %d\n", dn
, ctl
->index
);
1562 ctl
->dentries
[idx
] = dn
;
1565 dout("disable readdir cache\n");
1571 int ceph_readdir_prepopulate(struct ceph_mds_request
*req
,
1572 struct ceph_mds_session
*session
)
1574 struct dentry
*parent
= req
->r_dentry
;
1575 struct ceph_inode_info
*ci
= ceph_inode(d_inode(parent
));
1576 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1580 int err
= 0, skipped
= 0, ret
, i
;
1581 struct ceph_mds_request_head
*rhead
= req
->r_request
->front
.iov_base
;
1582 u32 frag
= le32_to_cpu(rhead
->args
.readdir
.frag
);
1585 struct ceph_readdir_cache_control cache_ctl
= {};
1587 if (test_bit(CEPH_MDS_R_ABORTED
, &req
->r_req_flags
))
1588 return readdir_prepopulate_inodes_only(req
, session
);
1590 if (rinfo
->hash_order
) {
1592 last_hash
= ceph_str_hash(ci
->i_dir_layout
.dl_dir_hash
,
1594 strlen(req
->r_path2
));
1595 last_hash
= ceph_frag_value(last_hash
);
1596 } else if (rinfo
->offset_hash
) {
1597 /* mds understands offset_hash */
1598 WARN_ON_ONCE(req
->r_readdir_offset
!= 2);
1599 last_hash
= le32_to_cpu(rhead
->args
.readdir
.offset_hash
);
1603 if (rinfo
->dir_dir
&&
1604 le32_to_cpu(rinfo
->dir_dir
->frag
) != frag
) {
1605 dout("readdir_prepopulate got new frag %x -> %x\n",
1606 frag
, le32_to_cpu(rinfo
->dir_dir
->frag
));
1607 frag
= le32_to_cpu(rinfo
->dir_dir
->frag
);
1608 if (!rinfo
->hash_order
)
1609 req
->r_readdir_offset
= 2;
1612 if (le32_to_cpu(rinfo
->head
->op
) == CEPH_MDS_OP_LSSNAP
) {
1613 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1614 rinfo
->dir_nr
, parent
);
1616 dout("readdir_prepopulate %d items under dn %p\n",
1617 rinfo
->dir_nr
, parent
);
1619 ceph_fill_dirfrag(d_inode(parent
), rinfo
->dir_dir
);
1621 if (ceph_frag_is_leftmost(frag
) &&
1622 req
->r_readdir_offset
== 2 &&
1623 !(rinfo
->hash_order
&& last_hash
)) {
1624 /* note dir version at start of readdir so we can
1625 * tell if any dentries get dropped */
1626 req
->r_dir_release_cnt
=
1627 atomic64_read(&ci
->i_release_count
);
1628 req
->r_dir_ordered_cnt
=
1629 atomic64_read(&ci
->i_ordered_count
);
1630 req
->r_readdir_cache_idx
= 0;
1634 cache_ctl
.index
= req
->r_readdir_cache_idx
;
1635 fpos_offset
= req
->r_readdir_offset
;
1637 /* FIXME: release caps/leases if error occurs */
1638 for (i
= 0; i
< rinfo
->dir_nr
; i
++) {
1639 struct ceph_mds_reply_dir_entry
*rde
= rinfo
->dir_entries
+ i
;
1640 struct ceph_vino tvino
;
1642 dname
.name
= rde
->name
;
1643 dname
.len
= rde
->name_len
;
1644 dname
.hash
= full_name_hash(parent
, dname
.name
, dname
.len
);
1646 tvino
.ino
= le64_to_cpu(rde
->inode
.in
->ino
);
1647 tvino
.snap
= le64_to_cpu(rde
->inode
.in
->snapid
);
1649 if (rinfo
->hash_order
) {
1650 u32 hash
= ceph_str_hash(ci
->i_dir_layout
.dl_dir_hash
,
1651 rde
->name
, rde
->name_len
);
1652 hash
= ceph_frag_value(hash
);
1653 if (hash
!= last_hash
)
1656 rde
->offset
= ceph_make_fpos(hash
, fpos_offset
++, true);
1658 rde
->offset
= ceph_make_fpos(frag
, fpos_offset
++, false);
1662 dn
= d_lookup(parent
, &dname
);
1663 dout("d_lookup on parent=%p name=%.*s got %p\n",
1664 parent
, dname
.len
, dname
.name
, dn
);
1667 dn
= d_alloc(parent
, &dname
);
1668 dout("d_alloc %p '%.*s' = %p\n", parent
,
1669 dname
.len
, dname
.name
, dn
);
1671 dout("d_alloc badness\n");
1675 } else if (d_really_is_positive(dn
) &&
1676 (ceph_ino(d_inode(dn
)) != tvino
.ino
||
1677 ceph_snap(d_inode(dn
)) != tvino
.snap
)) {
1678 struct ceph_dentry_info
*di
= ceph_dentry(dn
);
1679 dout(" dn %p points to wrong inode %p\n",
1682 spin_lock(&dn
->d_lock
);
1683 if (di
->offset
> 0 &&
1684 di
->lease_shared_gen
==
1685 atomic_read(&ci
->i_shared_gen
)) {
1686 __ceph_dir_clear_ordered(ci
);
1689 spin_unlock(&dn
->d_lock
);
1697 if (d_really_is_positive(dn
)) {
1700 in
= ceph_get_inode(parent
->d_sb
, tvino
);
1702 dout("new_inode badness\n");
1710 ret
= fill_inode(in
, NULL
, &rde
->inode
, NULL
, session
,
1711 -1, &req
->r_caps_reservation
);
1713 pr_err("fill_inode badness on %p\n", in
);
1714 if (d_really_is_negative(dn
)) {
1715 /* avoid calling iput_final() in mds
1716 * dispatch threads */
1717 if (in
->i_state
& I_NEW
) {
1719 discard_new_inode(in
);
1721 ceph_async_iput(in
);
1727 if (in
->i_state
& I_NEW
)
1728 unlock_new_inode(in
);
1730 if (d_really_is_negative(dn
)) {
1731 if (ceph_security_xattr_deadlock(in
)) {
1732 dout(" skip splicing dn %p to inode %p"
1733 " (security xattr deadlock)\n", dn
, in
);
1734 ceph_async_iput(in
);
1739 err
= splice_dentry(&dn
, in
);
1744 ceph_dentry(dn
)->offset
= rde
->offset
;
1746 update_dentry_lease(d_inode(parent
), dn
,
1747 rde
->lease
, req
->r_session
,
1748 req
->r_request_started
);
1750 if (err
== 0 && skipped
== 0 && cache_ctl
.index
>= 0) {
1751 ret
= fill_readdir_cache(d_inode(parent
), dn
,
1760 if (err
== 0 && skipped
== 0) {
1761 set_bit(CEPH_MDS_R_DID_PREPOPULATE
, &req
->r_req_flags
);
1762 req
->r_readdir_cache_idx
= cache_ctl
.index
;
1764 ceph_readdir_cache_release(&cache_ctl
);
1765 dout("readdir_prepopulate done\n");
1769 bool ceph_inode_set_size(struct inode
*inode
, loff_t size
)
1771 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1774 spin_lock(&ci
->i_ceph_lock
);
1775 dout("set_size %p %llu -> %llu\n", inode
, inode
->i_size
, size
);
1776 i_size_write(inode
, size
);
1777 inode
->i_blocks
= calc_inode_blocks(size
);
1779 ret
= __ceph_should_report_size(ci
);
1781 spin_unlock(&ci
->i_ceph_lock
);
1786 * Put reference to inode, but avoid calling iput_final() in current thread.
1787 * iput_final() may wait for reahahead pages. The wait can cause deadlock in
1790 void ceph_async_iput(struct inode
*inode
)
1795 if (atomic_add_unless(&inode
->i_count
, -1, 1))
1797 if (queue_work(ceph_inode_to_client(inode
)->inode_wq
,
1798 &ceph_inode(inode
)->i_work
))
1800 /* queue work failed, i_count must be at least 2 */
1805 * Write back inode data in a worker thread. (This can't be done
1806 * in the message handler context.)
1808 void ceph_queue_writeback(struct inode
*inode
)
1810 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1811 set_bit(CEPH_I_WORK_WRITEBACK
, &ci
->i_work_mask
);
1814 if (queue_work(ceph_inode_to_client(inode
)->inode_wq
,
1816 dout("ceph_queue_writeback %p\n", inode
);
1818 dout("ceph_queue_writeback %p already queued, mask=%lx\n",
1819 inode
, ci
->i_work_mask
);
1825 * queue an async invalidation
1827 void ceph_queue_invalidate(struct inode
*inode
)
1829 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1830 set_bit(CEPH_I_WORK_INVALIDATE_PAGES
, &ci
->i_work_mask
);
1833 if (queue_work(ceph_inode_to_client(inode
)->inode_wq
,
1834 &ceph_inode(inode
)->i_work
)) {
1835 dout("ceph_queue_invalidate %p\n", inode
);
1837 dout("ceph_queue_invalidate %p already queued, mask=%lx\n",
1838 inode
, ci
->i_work_mask
);
1844 * Queue an async vmtruncate. If we fail to queue work, we will handle
1845 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1847 void ceph_queue_vmtruncate(struct inode
*inode
)
1849 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1850 set_bit(CEPH_I_WORK_VMTRUNCATE
, &ci
->i_work_mask
);
1853 if (queue_work(ceph_inode_to_client(inode
)->inode_wq
,
1855 dout("ceph_queue_vmtruncate %p\n", inode
);
1857 dout("ceph_queue_vmtruncate %p already queued, mask=%lx\n",
1858 inode
, ci
->i_work_mask
);
1863 static void ceph_do_invalidate_pages(struct inode
*inode
)
1865 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1866 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
1870 mutex_lock(&ci
->i_truncate_mutex
);
1872 if (READ_ONCE(fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
1873 pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
1874 inode
, ceph_ino(inode
));
1875 mapping_set_error(inode
->i_mapping
, -EIO
);
1876 truncate_pagecache(inode
, 0);
1877 mutex_unlock(&ci
->i_truncate_mutex
);
1881 spin_lock(&ci
->i_ceph_lock
);
1882 dout("invalidate_pages %p gen %d revoking %d\n", inode
,
1883 ci
->i_rdcache_gen
, ci
->i_rdcache_revoking
);
1884 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
1885 if (__ceph_caps_revoking_other(ci
, NULL
, CEPH_CAP_FILE_CACHE
))
1887 spin_unlock(&ci
->i_ceph_lock
);
1888 mutex_unlock(&ci
->i_truncate_mutex
);
1891 orig_gen
= ci
->i_rdcache_gen
;
1892 spin_unlock(&ci
->i_ceph_lock
);
1894 if (invalidate_inode_pages2(inode
->i_mapping
) < 0) {
1895 pr_err("invalidate_pages %p fails\n", inode
);
1898 spin_lock(&ci
->i_ceph_lock
);
1899 if (orig_gen
== ci
->i_rdcache_gen
&&
1900 orig_gen
== ci
->i_rdcache_revoking
) {
1901 dout("invalidate_pages %p gen %d successful\n", inode
,
1903 ci
->i_rdcache_revoking
--;
1906 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1907 inode
, orig_gen
, ci
->i_rdcache_gen
,
1908 ci
->i_rdcache_revoking
);
1909 if (__ceph_caps_revoking_other(ci
, NULL
, CEPH_CAP_FILE_CACHE
))
1912 spin_unlock(&ci
->i_ceph_lock
);
1913 mutex_unlock(&ci
->i_truncate_mutex
);
1916 ceph_check_caps(ci
, 0, NULL
);
1920 * Make sure any pending truncation is applied before doing anything
1921 * that may depend on it.
1923 void __ceph_do_pending_vmtruncate(struct inode
*inode
)
1925 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1927 int wrbuffer_refs
, finish
= 0;
1929 mutex_lock(&ci
->i_truncate_mutex
);
1931 spin_lock(&ci
->i_ceph_lock
);
1932 if (ci
->i_truncate_pending
== 0) {
1933 dout("__do_pending_vmtruncate %p none pending\n", inode
);
1934 spin_unlock(&ci
->i_ceph_lock
);
1935 mutex_unlock(&ci
->i_truncate_mutex
);
1940 * make sure any dirty snapped pages are flushed before we
1941 * possibly truncate them.. so write AND block!
1943 if (ci
->i_wrbuffer_ref_head
< ci
->i_wrbuffer_ref
) {
1944 spin_unlock(&ci
->i_ceph_lock
);
1945 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1947 filemap_write_and_wait_range(&inode
->i_data
, 0,
1948 inode
->i_sb
->s_maxbytes
);
1952 /* there should be no reader or writer */
1953 WARN_ON_ONCE(ci
->i_rd_ref
|| ci
->i_wr_ref
);
1955 to
= ci
->i_truncate_size
;
1956 wrbuffer_refs
= ci
->i_wrbuffer_ref
;
1957 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode
,
1958 ci
->i_truncate_pending
, to
);
1959 spin_unlock(&ci
->i_ceph_lock
);
1961 truncate_pagecache(inode
, to
);
1963 spin_lock(&ci
->i_ceph_lock
);
1964 if (to
== ci
->i_truncate_size
) {
1965 ci
->i_truncate_pending
= 0;
1968 spin_unlock(&ci
->i_ceph_lock
);
1972 mutex_unlock(&ci
->i_truncate_mutex
);
1974 if (wrbuffer_refs
== 0)
1975 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
1977 wake_up_all(&ci
->i_cap_wq
);
1980 static void ceph_inode_work(struct work_struct
*work
)
1982 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1984 struct inode
*inode
= &ci
->vfs_inode
;
1986 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK
, &ci
->i_work_mask
)) {
1987 dout("writeback %p\n", inode
);
1988 filemap_fdatawrite(&inode
->i_data
);
1990 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES
, &ci
->i_work_mask
))
1991 ceph_do_invalidate_pages(inode
);
1993 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE
, &ci
->i_work_mask
))
1994 __ceph_do_pending_vmtruncate(inode
);
2002 static const struct inode_operations ceph_symlink_iops
= {
2003 .get_link
= simple_get_link
,
2004 .setattr
= ceph_setattr
,
2005 .getattr
= ceph_getattr
,
2006 .listxattr
= ceph_listxattr
,
2009 int __ceph_setattr(struct inode
*inode
, struct iattr
*attr
)
2011 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2012 unsigned int ia_valid
= attr
->ia_valid
;
2013 struct ceph_mds_request
*req
;
2014 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
2015 struct ceph_cap_flush
*prealloc_cf
;
2017 int release
= 0, dirtied
= 0;
2020 int inode_dirty_flags
= 0;
2021 bool lock_snap_rwsem
= false;
2023 prealloc_cf
= ceph_alloc_cap_flush();
2027 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SETATTR
,
2030 ceph_free_cap_flush(prealloc_cf
);
2031 return PTR_ERR(req
);
2034 spin_lock(&ci
->i_ceph_lock
);
2035 issued
= __ceph_caps_issued(ci
, NULL
);
2037 if (!ci
->i_head_snapc
&&
2038 (issued
& (CEPH_CAP_ANY_EXCL
| CEPH_CAP_FILE_WR
))) {
2039 lock_snap_rwsem
= true;
2040 if (!down_read_trylock(&mdsc
->snap_rwsem
)) {
2041 spin_unlock(&ci
->i_ceph_lock
);
2042 down_read(&mdsc
->snap_rwsem
);
2043 spin_lock(&ci
->i_ceph_lock
);
2044 issued
= __ceph_caps_issued(ci
, NULL
);
2048 dout("setattr %p issued %s\n", inode
, ceph_cap_string(issued
));
2050 if (ia_valid
& ATTR_UID
) {
2051 dout("setattr %p uid %d -> %d\n", inode
,
2052 from_kuid(&init_user_ns
, inode
->i_uid
),
2053 from_kuid(&init_user_ns
, attr
->ia_uid
));
2054 if (issued
& CEPH_CAP_AUTH_EXCL
) {
2055 inode
->i_uid
= attr
->ia_uid
;
2056 dirtied
|= CEPH_CAP_AUTH_EXCL
;
2057 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
2058 !uid_eq(attr
->ia_uid
, inode
->i_uid
)) {
2059 req
->r_args
.setattr
.uid
= cpu_to_le32(
2060 from_kuid(&init_user_ns
, attr
->ia_uid
));
2061 mask
|= CEPH_SETATTR_UID
;
2062 release
|= CEPH_CAP_AUTH_SHARED
;
2065 if (ia_valid
& ATTR_GID
) {
2066 dout("setattr %p gid %d -> %d\n", inode
,
2067 from_kgid(&init_user_ns
, inode
->i_gid
),
2068 from_kgid(&init_user_ns
, attr
->ia_gid
));
2069 if (issued
& CEPH_CAP_AUTH_EXCL
) {
2070 inode
->i_gid
= attr
->ia_gid
;
2071 dirtied
|= CEPH_CAP_AUTH_EXCL
;
2072 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
2073 !gid_eq(attr
->ia_gid
, inode
->i_gid
)) {
2074 req
->r_args
.setattr
.gid
= cpu_to_le32(
2075 from_kgid(&init_user_ns
, attr
->ia_gid
));
2076 mask
|= CEPH_SETATTR_GID
;
2077 release
|= CEPH_CAP_AUTH_SHARED
;
2080 if (ia_valid
& ATTR_MODE
) {
2081 dout("setattr %p mode 0%o -> 0%o\n", inode
, inode
->i_mode
,
2083 if (issued
& CEPH_CAP_AUTH_EXCL
) {
2084 inode
->i_mode
= attr
->ia_mode
;
2085 dirtied
|= CEPH_CAP_AUTH_EXCL
;
2086 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
2087 attr
->ia_mode
!= inode
->i_mode
) {
2088 inode
->i_mode
= attr
->ia_mode
;
2089 req
->r_args
.setattr
.mode
= cpu_to_le32(attr
->ia_mode
);
2090 mask
|= CEPH_SETATTR_MODE
;
2091 release
|= CEPH_CAP_AUTH_SHARED
;
2095 if (ia_valid
& ATTR_ATIME
) {
2096 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode
,
2097 inode
->i_atime
.tv_sec
, inode
->i_atime
.tv_nsec
,
2098 attr
->ia_atime
.tv_sec
, attr
->ia_atime
.tv_nsec
);
2099 if (issued
& CEPH_CAP_FILE_EXCL
) {
2100 ci
->i_time_warp_seq
++;
2101 inode
->i_atime
= attr
->ia_atime
;
2102 dirtied
|= CEPH_CAP_FILE_EXCL
;
2103 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
2104 timespec64_compare(&inode
->i_atime
,
2105 &attr
->ia_atime
) < 0) {
2106 inode
->i_atime
= attr
->ia_atime
;
2107 dirtied
|= CEPH_CAP_FILE_WR
;
2108 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
2109 !timespec64_equal(&inode
->i_atime
, &attr
->ia_atime
)) {
2110 ceph_encode_timespec64(&req
->r_args
.setattr
.atime
,
2112 mask
|= CEPH_SETATTR_ATIME
;
2113 release
|= CEPH_CAP_FILE_SHARED
|
2114 CEPH_CAP_FILE_RD
| CEPH_CAP_FILE_WR
;
2117 if (ia_valid
& ATTR_SIZE
) {
2118 dout("setattr %p size %lld -> %lld\n", inode
,
2119 inode
->i_size
, attr
->ia_size
);
2120 if ((issued
& CEPH_CAP_FILE_EXCL
) &&
2121 attr
->ia_size
> inode
->i_size
) {
2122 i_size_write(inode
, attr
->ia_size
);
2123 inode
->i_blocks
= calc_inode_blocks(attr
->ia_size
);
2124 ci
->i_reported_size
= attr
->ia_size
;
2125 dirtied
|= CEPH_CAP_FILE_EXCL
;
2126 ia_valid
|= ATTR_MTIME
;
2127 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
2128 attr
->ia_size
!= inode
->i_size
) {
2129 req
->r_args
.setattr
.size
= cpu_to_le64(attr
->ia_size
);
2130 req
->r_args
.setattr
.old_size
=
2131 cpu_to_le64(inode
->i_size
);
2132 mask
|= CEPH_SETATTR_SIZE
;
2133 release
|= CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_EXCL
|
2134 CEPH_CAP_FILE_RD
| CEPH_CAP_FILE_WR
;
2137 if (ia_valid
& ATTR_MTIME
) {
2138 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode
,
2139 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
2140 attr
->ia_mtime
.tv_sec
, attr
->ia_mtime
.tv_nsec
);
2141 if (issued
& CEPH_CAP_FILE_EXCL
) {
2142 ci
->i_time_warp_seq
++;
2143 inode
->i_mtime
= attr
->ia_mtime
;
2144 dirtied
|= CEPH_CAP_FILE_EXCL
;
2145 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
2146 timespec64_compare(&inode
->i_mtime
,
2147 &attr
->ia_mtime
) < 0) {
2148 inode
->i_mtime
= attr
->ia_mtime
;
2149 dirtied
|= CEPH_CAP_FILE_WR
;
2150 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
2151 !timespec64_equal(&inode
->i_mtime
, &attr
->ia_mtime
)) {
2152 ceph_encode_timespec64(&req
->r_args
.setattr
.mtime
,
2154 mask
|= CEPH_SETATTR_MTIME
;
2155 release
|= CEPH_CAP_FILE_SHARED
|
2156 CEPH_CAP_FILE_RD
| CEPH_CAP_FILE_WR
;
2160 /* these do nothing */
2161 if (ia_valid
& ATTR_CTIME
) {
2162 bool only
= (ia_valid
& (ATTR_SIZE
|ATTR_MTIME
|ATTR_ATIME
|
2163 ATTR_MODE
|ATTR_UID
|ATTR_GID
)) == 0;
2164 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode
,
2165 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
2166 attr
->ia_ctime
.tv_sec
, attr
->ia_ctime
.tv_nsec
,
2167 only
? "ctime only" : "ignored");
2170 * if kernel wants to dirty ctime but nothing else,
2171 * we need to choose a cap to dirty under, or do
2172 * a almost-no-op setattr
2174 if (issued
& CEPH_CAP_AUTH_EXCL
)
2175 dirtied
|= CEPH_CAP_AUTH_EXCL
;
2176 else if (issued
& CEPH_CAP_FILE_EXCL
)
2177 dirtied
|= CEPH_CAP_FILE_EXCL
;
2178 else if (issued
& CEPH_CAP_XATTR_EXCL
)
2179 dirtied
|= CEPH_CAP_XATTR_EXCL
;
2181 mask
|= CEPH_SETATTR_CTIME
;
2184 if (ia_valid
& ATTR_FILE
)
2185 dout("setattr %p ATTR_FILE ... hrm!\n", inode
);
2188 inode_dirty_flags
= __ceph_mark_dirty_caps(ci
, dirtied
,
2190 inode
->i_ctime
= attr
->ia_ctime
;
2194 spin_unlock(&ci
->i_ceph_lock
);
2195 if (lock_snap_rwsem
)
2196 up_read(&mdsc
->snap_rwsem
);
2198 if (inode_dirty_flags
)
2199 __mark_inode_dirty(inode
, inode_dirty_flags
);
2203 req
->r_inode
= inode
;
2205 req
->r_inode_drop
= release
;
2206 req
->r_args
.setattr
.mask
= cpu_to_le32(mask
);
2207 req
->r_num_caps
= 1;
2208 req
->r_stamp
= attr
->ia_ctime
;
2209 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
2211 dout("setattr %p result=%d (%s locally, %d remote)\n", inode
, err
,
2212 ceph_cap_string(dirtied
), mask
);
2214 ceph_mdsc_put_request(req
);
2215 ceph_free_cap_flush(prealloc_cf
);
2217 if (err
>= 0 && (mask
& CEPH_SETATTR_SIZE
))
2218 __ceph_do_pending_vmtruncate(inode
);
2226 int ceph_setattr(struct dentry
*dentry
, struct iattr
*attr
)
2228 struct inode
*inode
= d_inode(dentry
);
2229 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
2232 if (ceph_snap(inode
) != CEPH_NOSNAP
)
2235 err
= setattr_prepare(dentry
, attr
);
2239 if ((attr
->ia_valid
& ATTR_SIZE
) &&
2240 attr
->ia_size
> max(inode
->i_size
, fsc
->max_file_size
))
2243 if ((attr
->ia_valid
& ATTR_SIZE
) &&
2244 ceph_quota_is_max_bytes_exceeded(inode
, attr
->ia_size
))
2247 err
= __ceph_setattr(inode
, attr
);
2249 if (err
>= 0 && (attr
->ia_valid
& ATTR_MODE
))
2250 err
= posix_acl_chmod(inode
, attr
->ia_mode
);
2256 * Verify that we have a lease on the given mask. If not,
2257 * do a getattr against an mds.
2259 int __ceph_do_getattr(struct inode
*inode
, struct page
*locked_page
,
2260 int mask
, bool force
)
2262 struct ceph_fs_client
*fsc
= ceph_sb_to_client(inode
->i_sb
);
2263 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
2264 struct ceph_mds_request
*req
;
2268 if (ceph_snap(inode
) == CEPH_SNAPDIR
) {
2269 dout("do_getattr inode %p SNAPDIR\n", inode
);
2273 dout("do_getattr inode %p mask %s mode 0%o\n",
2274 inode
, ceph_cap_string(mask
), inode
->i_mode
);
2275 if (!force
&& ceph_caps_issued_mask(ceph_inode(inode
), mask
, 1))
2278 mode
= (mask
& CEPH_STAT_RSTAT
) ? USE_AUTH_MDS
: USE_ANY_MDS
;
2279 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_GETATTR
, mode
);
2281 return PTR_ERR(req
);
2282 req
->r_inode
= inode
;
2284 req
->r_num_caps
= 1;
2285 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
2286 req
->r_locked_page
= locked_page
;
2287 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
2288 if (locked_page
&& err
== 0) {
2289 u64 inline_version
= req
->r_reply_info
.targeti
.inline_version
;
2290 if (inline_version
== 0) {
2291 /* the reply is supposed to contain inline data */
2293 } else if (inline_version
== CEPH_INLINE_NONE
) {
2296 err
= req
->r_reply_info
.targeti
.inline_len
;
2299 ceph_mdsc_put_request(req
);
2300 dout("do_getattr result=%d\n", err
);
2306 * Check inode permissions. We verify we have a valid value for
2307 * the AUTH cap, then call the generic handler.
2309 int ceph_permission(struct inode
*inode
, int mask
)
2313 if (mask
& MAY_NOT_BLOCK
)
2316 err
= ceph_do_getattr(inode
, CEPH_CAP_AUTH_SHARED
, false);
2319 err
= generic_permission(inode
, mask
);
2323 /* Craft a mask of needed caps given a set of requested statx attrs. */
2324 static int statx_to_caps(u32 want
)
2328 if (want
& (STATX_MODE
|STATX_UID
|STATX_GID
|STATX_CTIME
|STATX_BTIME
))
2329 mask
|= CEPH_CAP_AUTH_SHARED
;
2331 if (want
& (STATX_NLINK
|STATX_CTIME
))
2332 mask
|= CEPH_CAP_LINK_SHARED
;
2334 if (want
& (STATX_ATIME
|STATX_MTIME
|STATX_CTIME
|STATX_SIZE
|
2336 mask
|= CEPH_CAP_FILE_SHARED
;
2338 if (want
& (STATX_CTIME
))
2339 mask
|= CEPH_CAP_XATTR_SHARED
;
2345 * Get all the attributes. If we have sufficient caps for the requested attrs,
2346 * then we can avoid talking to the MDS at all.
2348 int ceph_getattr(const struct path
*path
, struct kstat
*stat
,
2349 u32 request_mask
, unsigned int flags
)
2351 struct inode
*inode
= d_inode(path
->dentry
);
2352 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2353 u32 valid_mask
= STATX_BASIC_STATS
;
2356 /* Skip the getattr altogether if we're asked not to sync */
2357 if (!(flags
& AT_STATX_DONT_SYNC
)) {
2358 err
= ceph_do_getattr(inode
, statx_to_caps(request_mask
),
2359 flags
& AT_STATX_FORCE_SYNC
);
2364 generic_fillattr(inode
, stat
);
2365 stat
->ino
= ceph_translate_ino(inode
->i_sb
, inode
->i_ino
);
2368 * btime on newly-allocated inodes is 0, so if this is still set to
2369 * that, then assume that it's not valid.
2371 if (ci
->i_btime
.tv_sec
|| ci
->i_btime
.tv_nsec
) {
2372 stat
->btime
= ci
->i_btime
;
2373 valid_mask
|= STATX_BTIME
;
2376 if (ceph_snap(inode
) == CEPH_NOSNAP
)
2377 stat
->dev
= inode
->i_sb
->s_dev
;
2379 stat
->dev
= ci
->i_snapid_map
? ci
->i_snapid_map
->dev
: 0;
2381 if (S_ISDIR(inode
->i_mode
)) {
2382 if (ceph_test_mount_opt(ceph_sb_to_client(inode
->i_sb
),
2384 stat
->size
= ci
->i_rbytes
;
2386 stat
->size
= ci
->i_files
+ ci
->i_subdirs
;
2388 stat
->blksize
= 65536;
2390 * Some applications rely on the number of st_nlink
2391 * value on directories to be either 0 (if unlinked)
2392 * or 2 + number of subdirectories.
2394 if (stat
->nlink
== 1)
2395 /* '.' + '..' + subdirs */
2396 stat
->nlink
= 1 + 1 + ci
->i_subdirs
;
2399 stat
->result_mask
= request_mask
& valid_mask
;