1 #include "ceph_debug.h"
3 #include <linux/module.h>
5 #include <linux/smp_lock.h>
6 #include <linux/slab.h>
7 #include <linux/string.h>
8 #include <linux/uaccess.h>
9 #include <linux/kernel.h>
10 #include <linux/namei.h>
11 #include <linux/writeback.h>
12 #include <linux/vmalloc.h>
18 * Ceph inode operations
20 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
21 * setattr, etc.), xattr helpers, and helpers for assimilating
22 * metadata returned by the MDS into our cache.
24 * Also define helpers for doing asynchronous writeback, invalidation,
25 * and truncation for the benefit of those who can't afford to block
26 * (typically because they are in the message handler path).
29 static const struct inode_operations ceph_symlink_iops
;
31 static void ceph_invalidate_work(struct work_struct
*work
);
32 static void ceph_writeback_work(struct work_struct
*work
);
33 static void ceph_vmtruncate_work(struct work_struct
*work
);
36 * find or create an inode, given the ceph ino number
38 struct inode
*ceph_get_inode(struct super_block
*sb
, struct ceph_vino vino
)
41 ino_t t
= ceph_vino_to_ino(vino
);
43 inode
= iget5_locked(sb
, t
, ceph_ino_compare
, ceph_set_ino_cb
, &vino
);
45 return ERR_PTR(-ENOMEM
);
46 if (inode
->i_state
& I_NEW
) {
47 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
48 inode
, ceph_vinop(inode
), (u64
)inode
->i_ino
);
49 unlock_new_inode(inode
);
52 dout("get_inode on %lu=%llx.%llx got %p\n", inode
->i_ino
, vino
.ino
,
58 * get/constuct snapdir inode for a given directory
60 struct inode
*ceph_get_snapdir(struct inode
*parent
)
62 struct ceph_vino vino
= {
63 .ino
= ceph_ino(parent
),
66 struct inode
*inode
= ceph_get_inode(parent
->i_sb
, vino
);
67 struct ceph_inode_info
*ci
= ceph_inode(inode
);
69 BUG_ON(!S_ISDIR(parent
->i_mode
));
71 return ERR_PTR(PTR_ERR(inode
));
72 inode
->i_mode
= parent
->i_mode
;
73 inode
->i_uid
= parent
->i_uid
;
74 inode
->i_gid
= parent
->i_gid
;
75 inode
->i_op
= &ceph_dir_iops
;
76 inode
->i_fop
= &ceph_dir_fops
;
77 ci
->i_snap_caps
= CEPH_CAP_PIN
; /* so we can open */
82 const struct inode_operations ceph_file_iops
= {
83 .permission
= ceph_permission
,
84 .setattr
= ceph_setattr
,
85 .getattr
= ceph_getattr
,
86 .setxattr
= ceph_setxattr
,
87 .getxattr
= ceph_getxattr
,
88 .listxattr
= ceph_listxattr
,
89 .removexattr
= ceph_removexattr
,
94 * We use a 'frag tree' to keep track of the MDS's directory fragments
95 * for a given inode (usually there is just a single fragment). We
96 * need to know when a child frag is delegated to a new MDS, or when
97 * it is flagged as replicated, so we can direct our requests
102 * find/create a frag in the tree
104 static struct ceph_inode_frag
*__get_or_create_frag(struct ceph_inode_info
*ci
,
108 struct rb_node
*parent
= NULL
;
109 struct ceph_inode_frag
*frag
;
112 p
= &ci
->i_fragtree
.rb_node
;
115 frag
= rb_entry(parent
, struct ceph_inode_frag
, node
);
116 c
= ceph_frag_compare(f
, frag
->frag
);
125 frag
= kmalloc(sizeof(*frag
), GFP_NOFS
);
127 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
128 "frag %x\n", &ci
->vfs_inode
,
129 ceph_vinop(&ci
->vfs_inode
), f
);
130 return ERR_PTR(-ENOMEM
);
137 rb_link_node(&frag
->node
, parent
, p
);
138 rb_insert_color(&frag
->node
, &ci
->i_fragtree
);
140 dout("get_or_create_frag added %llx.%llx frag %x\n",
141 ceph_vinop(&ci
->vfs_inode
), f
);
146 * find a specific frag @f
148 struct ceph_inode_frag
*__ceph_find_frag(struct ceph_inode_info
*ci
, u32 f
)
150 struct rb_node
*n
= ci
->i_fragtree
.rb_node
;
153 struct ceph_inode_frag
*frag
=
154 rb_entry(n
, struct ceph_inode_frag
, node
);
155 int c
= ceph_frag_compare(f
, frag
->frag
);
167 * Choose frag containing the given value @v. If @pfrag is
168 * specified, copy the frag delegation info to the caller if
171 u32
ceph_choose_frag(struct ceph_inode_info
*ci
, u32 v
,
172 struct ceph_inode_frag
*pfrag
,
175 u32 t
= ceph_frag_make(0, 0);
176 struct ceph_inode_frag
*frag
;
183 mutex_lock(&ci
->i_fragtree_mutex
);
185 WARN_ON(!ceph_frag_contains_value(t
, v
));
186 frag
= __ceph_find_frag(ci
, t
);
188 break; /* t is a leaf */
189 if (frag
->split_by
== 0) {
191 memcpy(pfrag
, frag
, sizeof(*pfrag
));
198 nway
= 1 << frag
->split_by
;
199 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v
, t
,
200 frag
->split_by
, nway
);
201 for (i
= 0; i
< nway
; i
++) {
202 n
= ceph_frag_make_child(t
, frag
->split_by
, i
);
203 if (ceph_frag_contains_value(n
, v
)) {
210 dout("choose_frag(%x) = %x\n", v
, t
);
212 mutex_unlock(&ci
->i_fragtree_mutex
);
217 * Process dirfrag (delegation) info from the mds. Include leaf
218 * fragment in tree ONLY if ndist > 0. Otherwise, only
219 * branches/splits are included in i_fragtree)
221 static int ceph_fill_dirfrag(struct inode
*inode
,
222 struct ceph_mds_reply_dirfrag
*dirinfo
)
224 struct ceph_inode_info
*ci
= ceph_inode(inode
);
225 struct ceph_inode_frag
*frag
;
226 u32 id
= le32_to_cpu(dirinfo
->frag
);
227 int mds
= le32_to_cpu(dirinfo
->auth
);
228 int ndist
= le32_to_cpu(dirinfo
->ndist
);
232 mutex_lock(&ci
->i_fragtree_mutex
);
234 /* no delegation info needed. */
235 frag
= __ceph_find_frag(ci
, id
);
238 if (frag
->split_by
== 0) {
239 /* tree leaf, remove */
240 dout("fill_dirfrag removed %llx.%llx frag %x"
241 " (no ref)\n", ceph_vinop(inode
), id
);
242 rb_erase(&frag
->node
, &ci
->i_fragtree
);
245 /* tree branch, keep and clear */
246 dout("fill_dirfrag cleared %llx.%llx frag %x"
247 " referral\n", ceph_vinop(inode
), id
);
255 /* find/add this frag to store mds delegation info */
256 frag
= __get_or_create_frag(ci
, id
);
258 /* this is not the end of the world; we can continue
259 with bad/inaccurate delegation info */
260 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
261 ceph_vinop(inode
), le32_to_cpu(dirinfo
->frag
));
267 frag
->ndist
= min_t(u32
, ndist
, CEPH_MAX_DIRFRAG_REP
);
268 for (i
= 0; i
< frag
->ndist
; i
++)
269 frag
->dist
[i
] = le32_to_cpu(dirinfo
->dist
[i
]);
270 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
271 ceph_vinop(inode
), frag
->frag
, frag
->ndist
);
274 mutex_unlock(&ci
->i_fragtree_mutex
);
280 * initialize a newly allocated inode.
282 struct inode
*ceph_alloc_inode(struct super_block
*sb
)
284 struct ceph_inode_info
*ci
;
287 ci
= kmem_cache_alloc(ceph_inode_cachep
, GFP_NOFS
);
291 dout("alloc_inode %p\n", &ci
->vfs_inode
);
294 ci
->i_time_warp_seq
= 0;
295 ci
->i_ceph_flags
= 0;
296 ci
->i_release_count
= 0;
297 ci
->i_symlink
= NULL
;
299 ci
->i_fragtree
= RB_ROOT
;
300 mutex_init(&ci
->i_fragtree_mutex
);
302 ci
->i_xattrs
.blob
= NULL
;
303 ci
->i_xattrs
.prealloc_blob
= NULL
;
304 ci
->i_xattrs
.dirty
= false;
305 ci
->i_xattrs
.index
= RB_ROOT
;
306 ci
->i_xattrs
.count
= 0;
307 ci
->i_xattrs
.names_size
= 0;
308 ci
->i_xattrs
.vals_size
= 0;
309 ci
->i_xattrs
.version
= 0;
310 ci
->i_xattrs
.index_version
= 0;
312 ci
->i_caps
= RB_ROOT
;
313 ci
->i_auth_cap
= NULL
;
314 ci
->i_dirty_caps
= 0;
315 ci
->i_flushing_caps
= 0;
316 INIT_LIST_HEAD(&ci
->i_dirty_item
);
317 INIT_LIST_HEAD(&ci
->i_flushing_item
);
318 ci
->i_cap_flush_seq
= 0;
319 ci
->i_cap_flush_last_tid
= 0;
320 memset(&ci
->i_cap_flush_tid
, 0, sizeof(ci
->i_cap_flush_tid
));
321 init_waitqueue_head(&ci
->i_cap_wq
);
322 ci
->i_hold_caps_min
= 0;
323 ci
->i_hold_caps_max
= 0;
324 INIT_LIST_HEAD(&ci
->i_cap_delay_list
);
325 ci
->i_cap_exporting_mds
= 0;
326 ci
->i_cap_exporting_mseq
= 0;
327 ci
->i_cap_exporting_issued
= 0;
328 INIT_LIST_HEAD(&ci
->i_cap_snaps
);
329 ci
->i_head_snapc
= NULL
;
332 for (i
= 0; i
< CEPH_FILE_MODE_NUM
; i
++)
333 ci
->i_nr_by_mode
[i
] = 0;
335 ci
->i_truncate_seq
= 0;
336 ci
->i_truncate_size
= 0;
337 ci
->i_truncate_pending
= 0;
340 ci
->i_reported_size
= 0;
341 ci
->i_wanted_max_size
= 0;
342 ci
->i_requested_max_size
= 0;
346 ci
->i_rdcache_ref
= 0;
348 ci
->i_wrbuffer_ref
= 0;
349 ci
->i_wrbuffer_ref_head
= 0;
350 ci
->i_shared_gen
= 0;
351 ci
->i_rdcache_gen
= 0;
352 ci
->i_rdcache_revoking
= 0;
354 INIT_LIST_HEAD(&ci
->i_unsafe_writes
);
355 INIT_LIST_HEAD(&ci
->i_unsafe_dirops
);
356 spin_lock_init(&ci
->i_unsafe_lock
);
358 ci
->i_snap_realm
= NULL
;
359 INIT_LIST_HEAD(&ci
->i_snap_realm_item
);
360 INIT_LIST_HEAD(&ci
->i_snap_flush_item
);
362 INIT_WORK(&ci
->i_wb_work
, ceph_writeback_work
);
363 INIT_WORK(&ci
->i_pg_inv_work
, ceph_invalidate_work
);
365 INIT_WORK(&ci
->i_vmtruncate_work
, ceph_vmtruncate_work
);
367 return &ci
->vfs_inode
;
370 void ceph_destroy_inode(struct inode
*inode
)
372 struct ceph_inode_info
*ci
= ceph_inode(inode
);
373 struct ceph_inode_frag
*frag
;
376 dout("destroy_inode %p ino %llx.%llx\n", inode
, ceph_vinop(inode
));
378 ceph_queue_caps_release(inode
);
380 kfree(ci
->i_symlink
);
381 while ((n
= rb_first(&ci
->i_fragtree
)) != NULL
) {
382 frag
= rb_entry(n
, struct ceph_inode_frag
, node
);
383 rb_erase(n
, &ci
->i_fragtree
);
387 __ceph_destroy_xattrs(ci
);
388 if (ci
->i_xattrs
.blob
)
389 ceph_buffer_put(ci
->i_xattrs
.blob
);
390 if (ci
->i_xattrs
.prealloc_blob
)
391 ceph_buffer_put(ci
->i_xattrs
.prealloc_blob
);
393 kmem_cache_free(ceph_inode_cachep
, ci
);
398 * Helpers to fill in size, ctime, mtime, and atime. We have to be
399 * careful because either the client or MDS may have more up to date
400 * info, depending on which capabilities are held, and whether
401 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
402 * and size are monotonically increasing, except when utimes() or
403 * truncate() increments the corresponding _seq values.)
405 int ceph_fill_file_size(struct inode
*inode
, int issued
,
406 u32 truncate_seq
, u64 truncate_size
, u64 size
)
408 struct ceph_inode_info
*ci
= ceph_inode(inode
);
411 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) > 0 ||
412 (truncate_seq
== ci
->i_truncate_seq
&& size
> inode
->i_size
)) {
413 dout("size %lld -> %llu\n", inode
->i_size
, size
);
414 inode
->i_size
= size
;
415 inode
->i_blocks
= (size
+ (1<<9) - 1) >> 9;
416 ci
->i_reported_size
= size
;
417 if (truncate_seq
!= ci
->i_truncate_seq
) {
418 dout("truncate_seq %u -> %u\n",
419 ci
->i_truncate_seq
, truncate_seq
);
420 ci
->i_truncate_seq
= truncate_seq
;
422 * If we hold relevant caps, or in the case where we're
423 * not the only client referencing this file and we
424 * don't hold those caps, then we need to check whether
425 * the file is either opened or mmaped
427 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_RD
|
428 CEPH_CAP_FILE_WR
|CEPH_CAP_FILE_BUFFER
|
429 CEPH_CAP_FILE_EXCL
)) ||
430 mapping_mapped(inode
->i_mapping
) ||
431 __ceph_caps_file_wanted(ci
)) {
432 ci
->i_truncate_pending
++;
437 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) >= 0 &&
438 ci
->i_truncate_size
!= truncate_size
) {
439 dout("truncate_size %lld -> %llu\n", ci
->i_truncate_size
,
441 ci
->i_truncate_size
= truncate_size
;
446 void ceph_fill_file_time(struct inode
*inode
, int issued
,
447 u64 time_warp_seq
, struct timespec
*ctime
,
448 struct timespec
*mtime
, struct timespec
*atime
)
450 struct ceph_inode_info
*ci
= ceph_inode(inode
);
453 if (issued
& (CEPH_CAP_FILE_EXCL
|
455 CEPH_CAP_FILE_BUFFER
)) {
456 if (timespec_compare(ctime
, &inode
->i_ctime
) > 0) {
457 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
458 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
459 ctime
->tv_sec
, ctime
->tv_nsec
);
460 inode
->i_ctime
= *ctime
;
462 if (ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) > 0) {
463 /* the MDS did a utimes() */
464 dout("mtime %ld.%09ld -> %ld.%09ld "
466 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
467 mtime
->tv_sec
, mtime
->tv_nsec
,
468 ci
->i_time_warp_seq
, (int)time_warp_seq
);
470 inode
->i_mtime
= *mtime
;
471 inode
->i_atime
= *atime
;
472 ci
->i_time_warp_seq
= time_warp_seq
;
473 } else if (time_warp_seq
== ci
->i_time_warp_seq
) {
474 /* nobody did utimes(); take the max */
475 if (timespec_compare(mtime
, &inode
->i_mtime
) > 0) {
476 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
477 inode
->i_mtime
.tv_sec
,
478 inode
->i_mtime
.tv_nsec
,
479 mtime
->tv_sec
, mtime
->tv_nsec
);
480 inode
->i_mtime
= *mtime
;
482 if (timespec_compare(atime
, &inode
->i_atime
) > 0) {
483 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
484 inode
->i_atime
.tv_sec
,
485 inode
->i_atime
.tv_nsec
,
486 atime
->tv_sec
, atime
->tv_nsec
);
487 inode
->i_atime
= *atime
;
489 } else if (issued
& CEPH_CAP_FILE_EXCL
) {
490 /* we did a utimes(); ignore mds values */
495 /* we have no write caps; whatever the MDS says is true */
496 if (ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) >= 0) {
497 inode
->i_ctime
= *ctime
;
498 inode
->i_mtime
= *mtime
;
499 inode
->i_atime
= *atime
;
500 ci
->i_time_warp_seq
= time_warp_seq
;
505 if (warn
) /* time_warp_seq shouldn't go backwards */
506 dout("%p mds time_warp_seq %llu < %u\n",
507 inode
, time_warp_seq
, ci
->i_time_warp_seq
);
511 * Populate an inode based on info from mds. May be called on new or
514 static int fill_inode(struct inode
*inode
,
515 struct ceph_mds_reply_info_in
*iinfo
,
516 struct ceph_mds_reply_dirfrag
*dirinfo
,
517 struct ceph_mds_session
*session
,
518 unsigned long ttl_from
, int cap_fmode
,
519 struct ceph_cap_reservation
*caps_reservation
)
521 struct ceph_mds_reply_inode
*info
= iinfo
->in
;
522 struct ceph_inode_info
*ci
= ceph_inode(inode
);
524 int issued
, implemented
;
525 struct timespec mtime
, atime
, ctime
;
527 struct ceph_buffer
*xattr_blob
= NULL
;
531 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
532 inode
, ceph_vinop(inode
), le64_to_cpu(info
->version
),
536 * prealloc xattr data, if it looks like we'll need it. only
537 * if len > 4 (meaning there are actually xattrs; the first 4
538 * bytes are the xattr count).
540 if (iinfo
->xattr_len
> 4) {
541 xattr_blob
= ceph_buffer_new(iinfo
->xattr_len
, GFP_NOFS
);
543 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
547 spin_lock(&inode
->i_lock
);
550 * provided version will be odd if inode value is projected,
551 * even if stable. skip the update if we have a newer info
552 * (e.g., due to inode info racing form multiple MDSs), or if
553 * we are getting projected (unstable) inode info.
555 if (le64_to_cpu(info
->version
) > 0 &&
556 (ci
->i_version
& ~1) > le64_to_cpu(info
->version
))
559 issued
= __ceph_caps_issued(ci
, &implemented
);
560 issued
|= implemented
| __ceph_caps_dirty(ci
);
563 ci
->i_version
= le64_to_cpu(info
->version
);
565 inode
->i_rdev
= le32_to_cpu(info
->rdev
);
567 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
568 inode
->i_mode
= le32_to_cpu(info
->mode
);
569 inode
->i_uid
= le32_to_cpu(info
->uid
);
570 inode
->i_gid
= le32_to_cpu(info
->gid
);
571 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
572 inode
->i_uid
, inode
->i_gid
);
575 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
576 inode
->i_nlink
= le32_to_cpu(info
->nlink
);
578 /* be careful with mtime, atime, size */
579 ceph_decode_timespec(&atime
, &info
->atime
);
580 ceph_decode_timespec(&mtime
, &info
->mtime
);
581 ceph_decode_timespec(&ctime
, &info
->ctime
);
582 queue_trunc
= ceph_fill_file_size(inode
, issued
,
583 le32_to_cpu(info
->truncate_seq
),
584 le64_to_cpu(info
->truncate_size
),
585 le64_to_cpu(info
->size
));
586 ceph_fill_file_time(inode
, issued
,
587 le32_to_cpu(info
->time_warp_seq
),
588 &ctime
, &mtime
, &atime
);
590 ci
->i_max_size
= le64_to_cpu(info
->max_size
);
591 ci
->i_layout
= info
->layout
;
592 inode
->i_blkbits
= fls(le32_to_cpu(info
->layout
.fl_stripe_unit
)) - 1;
595 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
596 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 &&
597 le64_to_cpu(info
->xattr_version
) > ci
->i_xattrs
.version
) {
598 if (ci
->i_xattrs
.blob
)
599 ceph_buffer_put(ci
->i_xattrs
.blob
);
600 ci
->i_xattrs
.blob
= xattr_blob
;
602 memcpy(ci
->i_xattrs
.blob
->vec
.iov_base
,
603 iinfo
->xattr_data
, iinfo
->xattr_len
);
604 ci
->i_xattrs
.version
= le64_to_cpu(info
->xattr_version
);
607 inode
->i_mapping
->a_ops
= &ceph_aops
;
608 inode
->i_mapping
->backing_dev_info
=
609 &ceph_client(inode
->i_sb
)->backing_dev_info
;
611 switch (inode
->i_mode
& S_IFMT
) {
616 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
617 inode
->i_op
= &ceph_file_iops
;
620 inode
->i_op
= &ceph_file_iops
;
621 inode
->i_fop
= &ceph_file_fops
;
624 inode
->i_op
= &ceph_symlink_iops
;
625 if (!ci
->i_symlink
) {
626 int symlen
= iinfo
->symlink_len
;
629 BUG_ON(symlen
!= inode
->i_size
);
630 spin_unlock(&inode
->i_lock
);
633 sym
= kmalloc(symlen
+1, GFP_NOFS
);
636 memcpy(sym
, iinfo
->symlink
, symlen
);
639 spin_lock(&inode
->i_lock
);
643 kfree(sym
); /* lost a race */
647 inode
->i_op
= &ceph_dir_iops
;
648 inode
->i_fop
= &ceph_dir_fops
;
650 ci
->i_files
= le64_to_cpu(info
->files
);
651 ci
->i_subdirs
= le64_to_cpu(info
->subdirs
);
652 ci
->i_rbytes
= le64_to_cpu(info
->rbytes
);
653 ci
->i_rfiles
= le64_to_cpu(info
->rfiles
);
654 ci
->i_rsubdirs
= le64_to_cpu(info
->rsubdirs
);
655 ceph_decode_timespec(&ci
->i_rctime
, &info
->rctime
);
657 /* set dir completion flag? */
658 if (ci
->i_files
== 0 && ci
->i_subdirs
== 0 &&
659 ceph_snap(inode
) == CEPH_NOSNAP
&&
660 (le32_to_cpu(info
->cap
.caps
) & CEPH_CAP_FILE_SHARED
)) {
661 dout(" marking %p complete (empty)\n", inode
);
662 ci
->i_ceph_flags
|= CEPH_I_COMPLETE
;
663 ci
->i_max_offset
= 2;
666 /* it may be better to set st_size in getattr instead? */
667 if (ceph_test_opt(ceph_client(inode
->i_sb
), RBYTES
))
668 inode
->i_size
= ci
->i_rbytes
;
671 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
672 ceph_vinop(inode
), inode
->i_mode
);
676 spin_unlock(&inode
->i_lock
);
678 /* queue truncate if we saw i_size decrease */
680 ceph_queue_vmtruncate(inode
);
682 /* populate frag tree */
683 /* FIXME: move me up, if/when version reflects fragtree changes */
684 nsplits
= le32_to_cpu(info
->fragtree
.nsplits
);
685 mutex_lock(&ci
->i_fragtree_mutex
);
686 for (i
= 0; i
< nsplits
; i
++) {
687 u32 id
= le32_to_cpu(info
->fragtree
.splits
[i
].frag
);
688 struct ceph_inode_frag
*frag
= __get_or_create_frag(ci
, id
);
692 frag
->split_by
= le32_to_cpu(info
->fragtree
.splits
[i
].by
);
693 dout(" frag %x split by %d\n", frag
->frag
, frag
->split_by
);
695 mutex_unlock(&ci
->i_fragtree_mutex
);
697 /* were we issued a capability? */
698 if (info
->cap
.caps
) {
699 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
700 ceph_add_cap(inode
, session
,
701 le64_to_cpu(info
->cap
.cap_id
),
703 le32_to_cpu(info
->cap
.caps
),
704 le32_to_cpu(info
->cap
.wanted
),
705 le32_to_cpu(info
->cap
.seq
),
706 le32_to_cpu(info
->cap
.mseq
),
707 le64_to_cpu(info
->cap
.realm
),
711 spin_lock(&inode
->i_lock
);
712 dout(" %p got snap_caps %s\n", inode
,
713 ceph_cap_string(le32_to_cpu(info
->cap
.caps
)));
714 ci
->i_snap_caps
|= le32_to_cpu(info
->cap
.caps
);
716 __ceph_get_fmode(ci
, cap_fmode
);
717 spin_unlock(&inode
->i_lock
);
721 /* update delegation info? */
723 ceph_fill_dirfrag(inode
, dirinfo
);
729 ceph_buffer_put(xattr_blob
);
734 * caller should hold session s_mutex.
736 static void update_dentry_lease(struct dentry
*dentry
,
737 struct ceph_mds_reply_lease
*lease
,
738 struct ceph_mds_session
*session
,
739 unsigned long from_time
)
741 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
742 long unsigned duration
= le32_to_cpu(lease
->duration_ms
);
743 long unsigned ttl
= from_time
+ (duration
* HZ
) / 1000;
744 long unsigned half_ttl
= from_time
+ (duration
* HZ
/ 2) / 1000;
747 /* only track leases on regular dentries */
748 if (dentry
->d_op
!= &ceph_dentry_ops
)
751 spin_lock(&dentry
->d_lock
);
752 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
753 dentry
, le16_to_cpu(lease
->mask
), duration
, ttl
);
755 /* make lease_rdcache_gen match directory */
756 dir
= dentry
->d_parent
->d_inode
;
757 di
->lease_shared_gen
= ceph_inode(dir
)->i_shared_gen
;
759 if (lease
->mask
== 0)
762 if (di
->lease_gen
== session
->s_cap_gen
&&
763 time_before(ttl
, dentry
->d_time
))
764 goto out_unlock
; /* we already have a newer lease. */
766 if (di
->lease_session
&& di
->lease_session
!= session
)
769 ceph_dentry_lru_touch(dentry
);
771 if (!di
->lease_session
)
772 di
->lease_session
= ceph_get_mds_session(session
);
773 di
->lease_gen
= session
->s_cap_gen
;
774 di
->lease_seq
= le32_to_cpu(lease
->seq
);
775 di
->lease_renew_after
= half_ttl
;
776 di
->lease_renew_from
= 0;
777 dentry
->d_time
= ttl
;
779 spin_unlock(&dentry
->d_lock
);
784 * splice a dentry to an inode.
785 * caller must hold directory i_mutex for this to be safe.
787 * we will only rehash the resulting dentry if @prehash is
788 * true; @prehash will be set to false (for the benefit of
789 * the caller) if we fail.
791 static struct dentry
*splice_dentry(struct dentry
*dn
, struct inode
*in
,
794 struct dentry
*realdn
;
796 /* dn must be unhashed */
799 realdn
= d_materialise_unique(dn
, in
);
800 if (IS_ERR(realdn
)) {
801 pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
802 dn
, in
, ceph_vinop(in
));
804 *prehash
= false; /* don't rehash on error */
805 dn
= realdn
; /* note realdn contains the error */
808 dout("dn %p (%d) spliced with %p (%d) "
809 "inode %p ino %llx.%llx\n",
810 dn
, atomic_read(&dn
->d_count
),
811 realdn
, atomic_read(&realdn
->d_count
),
812 realdn
->d_inode
, ceph_vinop(realdn
->d_inode
));
816 BUG_ON(!ceph_dentry(dn
));
818 dout("dn %p attached to %p ino %llx.%llx\n",
819 dn
, dn
->d_inode
, ceph_vinop(dn
->d_inode
));
821 if ((!prehash
|| *prehash
) && d_unhashed(dn
))
828 * Set dentry's directory position based on the current dir's max, and
829 * order it in d_subdirs, so that dcache_readdir behaves.
831 static void ceph_set_dentry_offset(struct dentry
*dn
)
833 struct dentry
*dir
= dn
->d_parent
;
834 struct inode
*inode
= dn
->d_parent
->d_inode
;
835 struct ceph_dentry_info
*di
;
839 di
= ceph_dentry(dn
);
841 spin_lock(&inode
->i_lock
);
842 di
->offset
= ceph_inode(inode
)->i_max_offset
++;
843 spin_unlock(&inode
->i_lock
);
845 spin_lock(&dcache_lock
);
846 spin_lock(&dn
->d_lock
);
847 list_move_tail(&dir
->d_subdirs
, &dn
->d_u
.d_child
);
848 dout("set_dentry_offset %p %lld (%p %p)\n", dn
, di
->offset
,
849 dn
->d_u
.d_child
.prev
, dn
->d_u
.d_child
.next
);
850 spin_unlock(&dn
->d_lock
);
851 spin_unlock(&dcache_lock
);
855 * Incorporate results into the local cache. This is either just
856 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
859 * A reply may contain
860 * a directory inode along with a dentry.
861 * and/or a target inode
863 * Called with snap_rwsem (read).
865 int ceph_fill_trace(struct super_block
*sb
, struct ceph_mds_request
*req
,
866 struct ceph_mds_session
*session
)
868 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
869 struct inode
*in
= NULL
;
870 struct ceph_mds_reply_inode
*ininfo
;
871 struct ceph_vino vino
;
875 dout("fill_trace %p is_dentry %d is_target %d\n", req
,
876 rinfo
->head
->is_dentry
, rinfo
->head
->is_target
);
882 * If we resend completed ops to a recovering mds, we get no
883 * trace. Since that is very rare, pretend this is the case
884 * to ensure the 'no trace' handlers in the callers behave.
886 * Fill in inodes unconditionally to avoid breaking cap
889 if (rinfo
->head
->op
& CEPH_MDS_OP_WRITE
) {
890 pr_info("fill_trace faking empty trace on %lld %s\n",
891 req
->r_tid
, ceph_mds_op_name(rinfo
->head
->op
));
892 if (rinfo
->head
->is_dentry
) {
893 rinfo
->head
->is_dentry
= 0;
894 err
= fill_inode(req
->r_locked_dir
,
895 &rinfo
->diri
, rinfo
->dirfrag
,
896 session
, req
->r_request_started
, -1);
898 if (rinfo
->head
->is_target
) {
899 rinfo
->head
->is_target
= 0;
900 ininfo
= rinfo
->targeti
.in
;
901 vino
.ino
= le64_to_cpu(ininfo
->ino
);
902 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
903 in
= ceph_get_inode(sb
, vino
);
904 err
= fill_inode(in
, &rinfo
->targeti
, NULL
,
905 session
, req
->r_request_started
,
912 if (!rinfo
->head
->is_target
&& !rinfo
->head
->is_dentry
) {
913 dout("fill_trace reply is empty!\n");
914 if (rinfo
->head
->result
== 0 && req
->r_locked_dir
) {
915 struct ceph_inode_info
*ci
=
916 ceph_inode(req
->r_locked_dir
);
917 dout(" clearing %p complete (empty trace)\n",
919 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
920 ci
->i_release_count
++;
925 if (rinfo
->head
->is_dentry
) {
926 struct inode
*dir
= req
->r_locked_dir
;
928 err
= fill_inode(dir
, &rinfo
->diri
, rinfo
->dirfrag
,
929 session
, req
->r_request_started
, -1,
930 &req
->r_caps_reservation
);
935 if (rinfo
->head
->is_dentry
&& !req
->r_aborted
) {
937 * lookup link rename : null -> possibly existing inode
938 * mknod symlink mkdir : null -> new inode
939 * unlink : linked -> null
941 struct inode
*dir
= req
->r_locked_dir
;
942 struct dentry
*dn
= req
->r_dentry
;
943 bool have_dir_cap
, have_lease
;
947 BUG_ON(dn
->d_parent
->d_inode
!= dir
);
948 BUG_ON(ceph_ino(dir
) !=
949 le64_to_cpu(rinfo
->diri
.in
->ino
));
950 BUG_ON(ceph_snap(dir
) !=
951 le64_to_cpu(rinfo
->diri
.in
->snapid
));
953 /* do we have a lease on the whole dir? */
955 (le32_to_cpu(rinfo
->diri
.in
->cap
.caps
) &
956 CEPH_CAP_FILE_SHARED
);
958 /* do we have a dn lease? */
959 have_lease
= have_dir_cap
||
960 (le16_to_cpu(rinfo
->dlease
->mask
) &
964 dout("fill_trace no dentry lease or dir cap\n");
967 if (req
->r_old_dentry
&& req
->r_op
== CEPH_MDS_OP_RENAME
) {
968 dout(" src %p '%.*s' dst %p '%.*s'\n",
970 req
->r_old_dentry
->d_name
.len
,
971 req
->r_old_dentry
->d_name
.name
,
972 dn
, dn
->d_name
.len
, dn
->d_name
.name
);
973 dout("fill_trace doing d_move %p -> %p\n",
974 req
->r_old_dentry
, dn
);
975 d_move(req
->r_old_dentry
, dn
);
976 dout(" src %p '%.*s' dst %p '%.*s'\n",
978 req
->r_old_dentry
->d_name
.len
,
979 req
->r_old_dentry
->d_name
.name
,
980 dn
, dn
->d_name
.len
, dn
->d_name
.name
);
981 /* ensure target dentry is invalidated, despite
982 rehashing bug in vfs_rename_dir */
983 dn
->d_time
= jiffies
;
984 ceph_dentry(dn
)->lease_shared_gen
= 0;
985 /* take overwritten dentry's readdir offset */
986 ceph_dentry(req
->r_old_dentry
)->offset
=
987 ceph_dentry(dn
)->offset
;
988 dn
= req
->r_old_dentry
; /* use old_dentry */
993 if (!rinfo
->head
->is_target
) {
994 dout("fill_trace null dentry\n");
996 dout("d_delete %p\n", dn
);
999 dout("d_instantiate %p NULL\n", dn
);
1000 d_instantiate(dn
, NULL
);
1001 if (have_lease
&& d_unhashed(dn
))
1003 update_dentry_lease(dn
, rinfo
->dlease
,
1005 req
->r_request_started
);
1010 /* attach proper inode */
1011 ininfo
= rinfo
->targeti
.in
;
1012 vino
.ino
= le64_to_cpu(ininfo
->ino
);
1013 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
1015 in
= ceph_get_inode(sb
, vino
);
1017 pr_err("fill_trace bad get_inode "
1018 "%llx.%llx\n", vino
.ino
, vino
.snap
);
1023 dn
= splice_dentry(dn
, in
, &have_lease
);
1028 req
->r_dentry
= dn
; /* may have spliced */
1029 ceph_set_dentry_offset(dn
);
1031 } else if (ceph_ino(in
) == vino
.ino
&&
1032 ceph_snap(in
) == vino
.snap
) {
1035 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1036 dn
, in
, ceph_ino(in
), ceph_snap(in
),
1037 vino
.ino
, vino
.snap
);
1043 update_dentry_lease(dn
, rinfo
->dlease
, session
,
1044 req
->r_request_started
);
1045 dout(" final dn %p\n", dn
);
1047 } else if (req
->r_op
== CEPH_MDS_OP_LOOKUPSNAP
||
1048 req
->r_op
== CEPH_MDS_OP_MKSNAP
) {
1049 struct dentry
*dn
= req
->r_dentry
;
1051 /* fill out a snapdir LOOKUPSNAP dentry */
1053 BUG_ON(!req
->r_locked_dir
);
1054 BUG_ON(ceph_snap(req
->r_locked_dir
) != CEPH_SNAPDIR
);
1055 ininfo
= rinfo
->targeti
.in
;
1056 vino
.ino
= le64_to_cpu(ininfo
->ino
);
1057 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
1058 in
= ceph_get_inode(sb
, vino
);
1060 pr_err("fill_inode get_inode badness %llx.%llx\n",
1061 vino
.ino
, vino
.snap
);
1066 dout(" linking snapped dir %p to dn %p\n", in
, dn
);
1067 dn
= splice_dentry(dn
, in
, NULL
);
1072 ceph_set_dentry_offset(dn
);
1073 req
->r_dentry
= dn
; /* may have spliced */
1075 rinfo
->head
->is_dentry
= 1; /* fool notrace handlers */
1078 if (rinfo
->head
->is_target
) {
1079 vino
.ino
= le64_to_cpu(rinfo
->targeti
.in
->ino
);
1080 vino
.snap
= le64_to_cpu(rinfo
->targeti
.in
->snapid
);
1082 if (in
== NULL
|| ceph_ino(in
) != vino
.ino
||
1083 ceph_snap(in
) != vino
.snap
) {
1084 in
= ceph_get_inode(sb
, vino
);
1090 req
->r_target_inode
= in
;
1092 err
= fill_inode(in
,
1093 &rinfo
->targeti
, NULL
,
1094 session
, req
->r_request_started
,
1095 (le32_to_cpu(rinfo
->head
->result
) == 0) ?
1097 &req
->r_caps_reservation
);
1099 pr_err("fill_inode badness %p %llx.%llx\n",
1100 in
, ceph_vinop(in
));
1106 dout("fill_trace done err=%d\n", err
);
1111 * Prepopulate our cache with readdir results, leases, etc.
1113 int ceph_readdir_prepopulate(struct ceph_mds_request
*req
,
1114 struct ceph_mds_session
*session
)
1116 struct dentry
*parent
= req
->r_dentry
;
1117 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1122 struct inode
*snapdir
= NULL
;
1123 struct ceph_mds_request_head
*rhead
= req
->r_request
->front
.iov_base
;
1124 u64 frag
= le32_to_cpu(rhead
->args
.readdir
.frag
);
1125 struct ceph_dentry_info
*di
;
1127 if (le32_to_cpu(rinfo
->head
->op
) == CEPH_MDS_OP_LSSNAP
) {
1128 snapdir
= ceph_get_snapdir(parent
->d_inode
);
1129 parent
= d_find_alias(snapdir
);
1130 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1131 rinfo
->dir_nr
, parent
);
1133 dout("readdir_prepopulate %d items under dn %p\n",
1134 rinfo
->dir_nr
, parent
);
1136 ceph_fill_dirfrag(parent
->d_inode
, rinfo
->dir_dir
);
1139 for (i
= 0; i
< rinfo
->dir_nr
; i
++) {
1140 struct ceph_vino vino
;
1142 dname
.name
= rinfo
->dir_dname
[i
];
1143 dname
.len
= rinfo
->dir_dname_len
[i
];
1144 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
1146 vino
.ino
= le64_to_cpu(rinfo
->dir_in
[i
].in
->ino
);
1147 vino
.snap
= le64_to_cpu(rinfo
->dir_in
[i
].in
->snapid
);
1150 dn
= d_lookup(parent
, &dname
);
1151 dout("d_lookup on parent=%p name=%.*s got %p\n",
1152 parent
, dname
.len
, dname
.name
, dn
);
1155 dn
= d_alloc(parent
, &dname
);
1156 dout("d_alloc %p '%.*s' = %p\n", parent
,
1157 dname
.len
, dname
.name
, dn
);
1159 dout("d_alloc badness\n");
1163 err
= ceph_init_dentry(dn
);
1166 } else if (dn
->d_inode
&&
1167 (ceph_ino(dn
->d_inode
) != vino
.ino
||
1168 ceph_snap(dn
->d_inode
) != vino
.snap
)) {
1169 dout(" dn %p points to wrong inode %p\n",
1175 /* reorder parent's d_subdirs */
1176 spin_lock(&dcache_lock
);
1177 spin_lock(&dn
->d_lock
);
1178 list_move(&dn
->d_u
.d_child
, &parent
->d_subdirs
);
1179 spin_unlock(&dn
->d_lock
);
1180 spin_unlock(&dcache_lock
);
1184 di
->offset
= ceph_make_fpos(frag
, i
+ req
->r_readdir_offset
);
1190 in
= ceph_get_inode(parent
->d_sb
, vino
);
1192 dout("new_inode badness\n");
1198 dn
= splice_dentry(dn
, in
, NULL
);
1201 if (fill_inode(in
, &rinfo
->dir_in
[i
], NULL
, session
,
1202 req
->r_request_started
, -1,
1203 &req
->r_caps_reservation
) < 0) {
1204 pr_err("fill_inode badness on %p\n", in
);
1208 update_dentry_lease(dn
, rinfo
->dir_dlease
[i
],
1209 req
->r_session
, req
->r_request_started
);
1212 req
->r_did_prepopulate
= true;
1219 dout("readdir_prepopulate done\n");
1223 int ceph_inode_set_size(struct inode
*inode
, loff_t size
)
1225 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1228 spin_lock(&inode
->i_lock
);
1229 dout("set_size %p %llu -> %llu\n", inode
, inode
->i_size
, size
);
1230 inode
->i_size
= size
;
1231 inode
->i_blocks
= (size
+ (1 << 9) - 1) >> 9;
1233 /* tell the MDS if we are approaching max_size */
1234 if ((size
<< 1) >= ci
->i_max_size
&&
1235 (ci
->i_reported_size
<< 1) < ci
->i_max_size
)
1238 spin_unlock(&inode
->i_lock
);
1243 * Write back inode data in a worker thread. (This can't be done
1244 * in the message handler context.)
1246 void ceph_queue_writeback(struct inode
*inode
)
1248 if (queue_work(ceph_inode_to_client(inode
)->wb_wq
,
1249 &ceph_inode(inode
)->i_wb_work
)) {
1250 dout("ceph_queue_invalidate %p\n", inode
);
1253 dout("ceph_queue_invalidate %p failed\n", inode
);
1257 static void ceph_writeback_work(struct work_struct
*work
)
1259 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1261 struct inode
*inode
= &ci
->vfs_inode
;
1263 dout("writeback %p\n", inode
);
1264 filemap_fdatawrite(&inode
->i_data
);
1269 * queue an async invalidation
1271 void ceph_queue_invalidate(struct inode
*inode
)
1273 if (queue_work(ceph_inode_to_client(inode
)->pg_inv_wq
,
1274 &ceph_inode(inode
)->i_pg_inv_work
)) {
1275 dout("ceph_queue_invalidate %p\n", inode
);
1278 dout("ceph_queue_invalidate %p failed\n", inode
);
1283 * Invalidate inode pages in a worker thread. (This can't be done
1284 * in the message handler context.)
1286 static void ceph_invalidate_work(struct work_struct
*work
)
1288 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1290 struct inode
*inode
= &ci
->vfs_inode
;
1294 spin_lock(&inode
->i_lock
);
1295 dout("invalidate_pages %p gen %d revoking %d\n", inode
,
1296 ci
->i_rdcache_gen
, ci
->i_rdcache_revoking
);
1297 if (ci
->i_rdcache_gen
== 0 ||
1298 ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
1299 BUG_ON(ci
->i_rdcache_revoking
> ci
->i_rdcache_gen
);
1301 ci
->i_rdcache_revoking
= 0;
1302 spin_unlock(&inode
->i_lock
);
1305 orig_gen
= ci
->i_rdcache_gen
;
1306 spin_unlock(&inode
->i_lock
);
1308 truncate_inode_pages(&inode
->i_data
, 0);
1310 spin_lock(&inode
->i_lock
);
1311 if (orig_gen
== ci
->i_rdcache_gen
) {
1312 dout("invalidate_pages %p gen %d successful\n", inode
,
1314 ci
->i_rdcache_gen
= 0;
1315 ci
->i_rdcache_revoking
= 0;
1318 dout("invalidate_pages %p gen %d raced, gen now %d\n",
1319 inode
, orig_gen
, ci
->i_rdcache_gen
);
1321 spin_unlock(&inode
->i_lock
);
1324 ceph_check_caps(ci
, 0, NULL
);
1331 * called by trunc_wq; take i_mutex ourselves
1333 * We also truncate in a separate thread as well.
1335 static void ceph_vmtruncate_work(struct work_struct
*work
)
1337 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1339 struct inode
*inode
= &ci
->vfs_inode
;
1341 dout("vmtruncate_work %p\n", inode
);
1342 mutex_lock(&inode
->i_mutex
);
1343 __ceph_do_pending_vmtruncate(inode
);
1344 mutex_unlock(&inode
->i_mutex
);
1349 * Queue an async vmtruncate. If we fail to queue work, we will handle
1350 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1352 void ceph_queue_vmtruncate(struct inode
*inode
)
1354 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1356 if (queue_work(ceph_client(inode
->i_sb
)->trunc_wq
,
1357 &ci
->i_vmtruncate_work
)) {
1358 dout("ceph_queue_vmtruncate %p\n", inode
);
1361 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1362 inode
, ci
->i_truncate_pending
);
1367 * called with i_mutex held.
1369 * Make sure any pending truncation is applied before doing anything
1370 * that may depend on it.
1372 void __ceph_do_pending_vmtruncate(struct inode
*inode
)
1374 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1376 int wrbuffer_refs
, wake
= 0;
1379 spin_lock(&inode
->i_lock
);
1380 if (ci
->i_truncate_pending
== 0) {
1381 dout("__do_pending_vmtruncate %p none pending\n", inode
);
1382 spin_unlock(&inode
->i_lock
);
1387 * make sure any dirty snapped pages are flushed before we
1388 * possibly truncate them.. so write AND block!
1390 if (ci
->i_wrbuffer_ref_head
< ci
->i_wrbuffer_ref
) {
1391 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1393 spin_unlock(&inode
->i_lock
);
1394 filemap_write_and_wait_range(&inode
->i_data
, 0,
1395 inode
->i_sb
->s_maxbytes
);
1399 to
= ci
->i_truncate_size
;
1400 wrbuffer_refs
= ci
->i_wrbuffer_ref
;
1401 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode
,
1402 ci
->i_truncate_pending
, to
);
1403 spin_unlock(&inode
->i_lock
);
1405 truncate_inode_pages(inode
->i_mapping
, to
);
1407 spin_lock(&inode
->i_lock
);
1408 ci
->i_truncate_pending
--;
1409 if (ci
->i_truncate_pending
== 0)
1411 spin_unlock(&inode
->i_lock
);
1413 if (wrbuffer_refs
== 0)
1414 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
1416 wake_up(&ci
->i_cap_wq
);
1423 static void *ceph_sym_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1425 struct ceph_inode_info
*ci
= ceph_inode(dentry
->d_inode
);
1426 nd_set_link(nd
, ci
->i_symlink
);
1430 static const struct inode_operations ceph_symlink_iops
= {
1431 .readlink
= generic_readlink
,
1432 .follow_link
= ceph_sym_follow_link
,
1438 int ceph_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1440 struct inode
*inode
= dentry
->d_inode
;
1441 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1442 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
1443 const unsigned int ia_valid
= attr
->ia_valid
;
1444 struct ceph_mds_request
*req
;
1445 struct ceph_mds_client
*mdsc
= &ceph_client(dentry
->d_sb
)->mdsc
;
1447 int release
= 0, dirtied
= 0;
1451 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1454 __ceph_do_pending_vmtruncate(inode
);
1456 err
= inode_change_ok(inode
, attr
);
1460 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SETATTR
,
1463 return PTR_ERR(req
);
1465 spin_lock(&inode
->i_lock
);
1466 issued
= __ceph_caps_issued(ci
, NULL
);
1467 dout("setattr %p issued %s\n", inode
, ceph_cap_string(issued
));
1469 if (ia_valid
& ATTR_UID
) {
1470 dout("setattr %p uid %d -> %d\n", inode
,
1471 inode
->i_uid
, attr
->ia_uid
);
1472 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1473 inode
->i_uid
= attr
->ia_uid
;
1474 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1475 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1476 attr
->ia_uid
!= inode
->i_uid
) {
1477 req
->r_args
.setattr
.uid
= cpu_to_le32(attr
->ia_uid
);
1478 mask
|= CEPH_SETATTR_UID
;
1479 release
|= CEPH_CAP_AUTH_SHARED
;
1482 if (ia_valid
& ATTR_GID
) {
1483 dout("setattr %p gid %d -> %d\n", inode
,
1484 inode
->i_gid
, attr
->ia_gid
);
1485 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1486 inode
->i_gid
= attr
->ia_gid
;
1487 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1488 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1489 attr
->ia_gid
!= inode
->i_gid
) {
1490 req
->r_args
.setattr
.gid
= cpu_to_le32(attr
->ia_gid
);
1491 mask
|= CEPH_SETATTR_GID
;
1492 release
|= CEPH_CAP_AUTH_SHARED
;
1495 if (ia_valid
& ATTR_MODE
) {
1496 dout("setattr %p mode 0%o -> 0%o\n", inode
, inode
->i_mode
,
1498 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1499 inode
->i_mode
= attr
->ia_mode
;
1500 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1501 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1502 attr
->ia_mode
!= inode
->i_mode
) {
1503 req
->r_args
.setattr
.mode
= cpu_to_le32(attr
->ia_mode
);
1504 mask
|= CEPH_SETATTR_MODE
;
1505 release
|= CEPH_CAP_AUTH_SHARED
;
1509 if (ia_valid
& ATTR_ATIME
) {
1510 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode
,
1511 inode
->i_atime
.tv_sec
, inode
->i_atime
.tv_nsec
,
1512 attr
->ia_atime
.tv_sec
, attr
->ia_atime
.tv_nsec
);
1513 if (issued
& CEPH_CAP_FILE_EXCL
) {
1514 ci
->i_time_warp_seq
++;
1515 inode
->i_atime
= attr
->ia_atime
;
1516 dirtied
|= CEPH_CAP_FILE_EXCL
;
1517 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
1518 timespec_compare(&inode
->i_atime
,
1519 &attr
->ia_atime
) < 0) {
1520 inode
->i_atime
= attr
->ia_atime
;
1521 dirtied
|= CEPH_CAP_FILE_WR
;
1522 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1523 !timespec_equal(&inode
->i_atime
, &attr
->ia_atime
)) {
1524 ceph_encode_timespec(&req
->r_args
.setattr
.atime
,
1526 mask
|= CEPH_SETATTR_ATIME
;
1527 release
|= CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_RD
|
1531 if (ia_valid
& ATTR_MTIME
) {
1532 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode
,
1533 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
1534 attr
->ia_mtime
.tv_sec
, attr
->ia_mtime
.tv_nsec
);
1535 if (issued
& CEPH_CAP_FILE_EXCL
) {
1536 ci
->i_time_warp_seq
++;
1537 inode
->i_mtime
= attr
->ia_mtime
;
1538 dirtied
|= CEPH_CAP_FILE_EXCL
;
1539 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
1540 timespec_compare(&inode
->i_mtime
,
1541 &attr
->ia_mtime
) < 0) {
1542 inode
->i_mtime
= attr
->ia_mtime
;
1543 dirtied
|= CEPH_CAP_FILE_WR
;
1544 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1545 !timespec_equal(&inode
->i_mtime
, &attr
->ia_mtime
)) {
1546 ceph_encode_timespec(&req
->r_args
.setattr
.mtime
,
1548 mask
|= CEPH_SETATTR_MTIME
;
1549 release
|= CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_RD
|
1553 if (ia_valid
& ATTR_SIZE
) {
1554 dout("setattr %p size %lld -> %lld\n", inode
,
1555 inode
->i_size
, attr
->ia_size
);
1556 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
) {
1560 if ((issued
& CEPH_CAP_FILE_EXCL
) &&
1561 attr
->ia_size
> inode
->i_size
) {
1562 inode
->i_size
= attr
->ia_size
;
1564 (attr
->ia_size
+ (1 << 9) - 1) >> 9;
1565 inode
->i_ctime
= attr
->ia_ctime
;
1566 ci
->i_reported_size
= attr
->ia_size
;
1567 dirtied
|= CEPH_CAP_FILE_EXCL
;
1568 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1569 attr
->ia_size
!= inode
->i_size
) {
1570 req
->r_args
.setattr
.size
= cpu_to_le64(attr
->ia_size
);
1571 req
->r_args
.setattr
.old_size
=
1572 cpu_to_le64(inode
->i_size
);
1573 mask
|= CEPH_SETATTR_SIZE
;
1574 release
|= CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_RD
|
1579 /* these do nothing */
1580 if (ia_valid
& ATTR_CTIME
) {
1581 bool only
= (ia_valid
& (ATTR_SIZE
|ATTR_MTIME
|ATTR_ATIME
|
1582 ATTR_MODE
|ATTR_UID
|ATTR_GID
)) == 0;
1583 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode
,
1584 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
1585 attr
->ia_ctime
.tv_sec
, attr
->ia_ctime
.tv_nsec
,
1586 only
? "ctime only" : "ignored");
1587 inode
->i_ctime
= attr
->ia_ctime
;
1590 * if kernel wants to dirty ctime but nothing else,
1591 * we need to choose a cap to dirty under, or do
1592 * a almost-no-op setattr
1594 if (issued
& CEPH_CAP_AUTH_EXCL
)
1595 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1596 else if (issued
& CEPH_CAP_FILE_EXCL
)
1597 dirtied
|= CEPH_CAP_FILE_EXCL
;
1598 else if (issued
& CEPH_CAP_XATTR_EXCL
)
1599 dirtied
|= CEPH_CAP_XATTR_EXCL
;
1601 mask
|= CEPH_SETATTR_CTIME
;
1604 if (ia_valid
& ATTR_FILE
)
1605 dout("setattr %p ATTR_FILE ... hrm!\n", inode
);
1608 __ceph_mark_dirty_caps(ci
, dirtied
);
1609 inode
->i_ctime
= CURRENT_TIME
;
1613 spin_unlock(&inode
->i_lock
);
1616 req
->r_inode
= igrab(inode
);
1617 req
->r_inode_drop
= release
;
1618 req
->r_args
.setattr
.mask
= cpu_to_le32(mask
);
1619 req
->r_num_caps
= 1;
1620 err
= ceph_mdsc_do_request(mdsc
, parent_inode
, req
);
1622 dout("setattr %p result=%d (%s locally, %d remote)\n", inode
, err
,
1623 ceph_cap_string(dirtied
), mask
);
1625 ceph_mdsc_put_request(req
);
1626 __ceph_do_pending_vmtruncate(inode
);
1629 spin_unlock(&inode
->i_lock
);
1630 ceph_mdsc_put_request(req
);
1635 * Verify that we have a lease on the given mask. If not,
1636 * do a getattr against an mds.
1638 int ceph_do_getattr(struct inode
*inode
, int mask
)
1640 struct ceph_client
*client
= ceph_sb_to_client(inode
->i_sb
);
1641 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1642 struct ceph_mds_request
*req
;
1645 if (ceph_snap(inode
) == CEPH_SNAPDIR
) {
1646 dout("do_getattr inode %p SNAPDIR\n", inode
);
1650 dout("do_getattr inode %p mask %s\n", inode
, ceph_cap_string(mask
));
1651 if (ceph_caps_issued_mask(ceph_inode(inode
), mask
, 1))
1654 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_GETATTR
, USE_ANY_MDS
);
1656 return PTR_ERR(req
);
1657 req
->r_inode
= igrab(inode
);
1658 req
->r_num_caps
= 1;
1659 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
1660 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1661 ceph_mdsc_put_request(req
);
1662 dout("do_getattr result=%d\n", err
);
1668 * Check inode permissions. We verify we have a valid value for
1669 * the AUTH cap, then call the generic handler.
1671 int ceph_permission(struct inode
*inode
, int mask
)
1673 int err
= ceph_do_getattr(inode
, CEPH_CAP_AUTH_SHARED
);
1676 err
= generic_permission(inode
, mask
, NULL
);
1681 * Get all attributes. Hopefully somedata we'll have a statlite()
1682 * and can limit the fields we require to be accurate.
1684 int ceph_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1687 struct inode
*inode
= dentry
->d_inode
;
1688 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1691 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_INODE_ALL
);
1693 generic_fillattr(inode
, stat
);
1694 stat
->ino
= inode
->i_ino
;
1695 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1696 stat
->dev
= ceph_snap(inode
);
1699 if (S_ISDIR(inode
->i_mode
)) {
1700 stat
->size
= ci
->i_rbytes
;
1702 stat
->blksize
= 65536;