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>
13 #include <linux/pagevec.h>
19 * Ceph inode operations
21 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
22 * setattr, etc.), xattr helpers, and helpers for assimilating
23 * metadata returned by the MDS into our cache.
25 * Also define helpers for doing asynchronous writeback, invalidation,
26 * and truncation for the benefit of those who can't afford to block
27 * (typically because they are in the message handler path).
30 static const struct inode_operations ceph_symlink_iops
;
32 static void ceph_invalidate_work(struct work_struct
*work
);
33 static void ceph_writeback_work(struct work_struct
*work
);
34 static void ceph_vmtruncate_work(struct work_struct
*work
);
37 * find or create an inode, given the ceph ino number
39 struct inode
*ceph_get_inode(struct super_block
*sb
, struct ceph_vino vino
)
42 ino_t t
= ceph_vino_to_ino(vino
);
44 inode
= iget5_locked(sb
, t
, ceph_ino_compare
, ceph_set_ino_cb
, &vino
);
46 return ERR_PTR(-ENOMEM
);
47 if (inode
->i_state
& I_NEW
) {
48 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
49 inode
, ceph_vinop(inode
), (u64
)inode
->i_ino
);
50 unlock_new_inode(inode
);
53 dout("get_inode on %lu=%llx.%llx got %p\n", inode
->i_ino
, vino
.ino
,
59 * get/constuct snapdir inode for a given directory
61 struct inode
*ceph_get_snapdir(struct inode
*parent
)
63 struct ceph_vino vino
= {
64 .ino
= ceph_ino(parent
),
67 struct inode
*inode
= ceph_get_inode(parent
->i_sb
, vino
);
68 struct ceph_inode_info
*ci
= ceph_inode(inode
);
70 BUG_ON(!S_ISDIR(parent
->i_mode
));
72 return ERR_PTR(PTR_ERR(inode
));
73 inode
->i_mode
= parent
->i_mode
;
74 inode
->i_uid
= parent
->i_uid
;
75 inode
->i_gid
= parent
->i_gid
;
76 inode
->i_op
= &ceph_dir_iops
;
77 inode
->i_fop
= &ceph_dir_fops
;
78 ci
->i_snap_caps
= CEPH_CAP_PIN
; /* so we can open */
83 const struct inode_operations ceph_file_iops
= {
84 .permission
= ceph_permission
,
85 .setattr
= ceph_setattr
,
86 .getattr
= ceph_getattr
,
87 .setxattr
= ceph_setxattr
,
88 .getxattr
= ceph_getxattr
,
89 .listxattr
= ceph_listxattr
,
90 .removexattr
= ceph_removexattr
,
95 * We use a 'frag tree' to keep track of the MDS's directory fragments
96 * for a given inode (usually there is just a single fragment). We
97 * need to know when a child frag is delegated to a new MDS, or when
98 * it is flagged as replicated, so we can direct our requests
103 * find/create a frag in the tree
105 static struct ceph_inode_frag
*__get_or_create_frag(struct ceph_inode_info
*ci
,
109 struct rb_node
*parent
= NULL
;
110 struct ceph_inode_frag
*frag
;
113 p
= &ci
->i_fragtree
.rb_node
;
116 frag
= rb_entry(parent
, struct ceph_inode_frag
, node
);
117 c
= ceph_frag_compare(f
, frag
->frag
);
126 frag
= kmalloc(sizeof(*frag
), GFP_NOFS
);
128 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
129 "frag %x\n", &ci
->vfs_inode
,
130 ceph_vinop(&ci
->vfs_inode
), f
);
131 return ERR_PTR(-ENOMEM
);
138 rb_link_node(&frag
->node
, parent
, p
);
139 rb_insert_color(&frag
->node
, &ci
->i_fragtree
);
141 dout("get_or_create_frag added %llx.%llx frag %x\n",
142 ceph_vinop(&ci
->vfs_inode
), f
);
147 * find a specific frag @f
149 struct ceph_inode_frag
*__ceph_find_frag(struct ceph_inode_info
*ci
, u32 f
)
151 struct rb_node
*n
= ci
->i_fragtree
.rb_node
;
154 struct ceph_inode_frag
*frag
=
155 rb_entry(n
, struct ceph_inode_frag
, node
);
156 int c
= ceph_frag_compare(f
, frag
->frag
);
168 * Choose frag containing the given value @v. If @pfrag is
169 * specified, copy the frag delegation info to the caller if
172 u32
ceph_choose_frag(struct ceph_inode_info
*ci
, u32 v
,
173 struct ceph_inode_frag
*pfrag
,
176 u32 t
= ceph_frag_make(0, 0);
177 struct ceph_inode_frag
*frag
;
184 mutex_lock(&ci
->i_fragtree_mutex
);
186 WARN_ON(!ceph_frag_contains_value(t
, v
));
187 frag
= __ceph_find_frag(ci
, t
);
189 break; /* t is a leaf */
190 if (frag
->split_by
== 0) {
192 memcpy(pfrag
, frag
, sizeof(*pfrag
));
199 nway
= 1 << frag
->split_by
;
200 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v
, t
,
201 frag
->split_by
, nway
);
202 for (i
= 0; i
< nway
; i
++) {
203 n
= ceph_frag_make_child(t
, frag
->split_by
, i
);
204 if (ceph_frag_contains_value(n
, v
)) {
211 dout("choose_frag(%x) = %x\n", v
, t
);
213 mutex_unlock(&ci
->i_fragtree_mutex
);
218 * Process dirfrag (delegation) info from the mds. Include leaf
219 * fragment in tree ONLY if ndist > 0. Otherwise, only
220 * branches/splits are included in i_fragtree)
222 static int ceph_fill_dirfrag(struct inode
*inode
,
223 struct ceph_mds_reply_dirfrag
*dirinfo
)
225 struct ceph_inode_info
*ci
= ceph_inode(inode
);
226 struct ceph_inode_frag
*frag
;
227 u32 id
= le32_to_cpu(dirinfo
->frag
);
228 int mds
= le32_to_cpu(dirinfo
->auth
);
229 int ndist
= le32_to_cpu(dirinfo
->ndist
);
233 mutex_lock(&ci
->i_fragtree_mutex
);
235 /* no delegation info needed. */
236 frag
= __ceph_find_frag(ci
, id
);
239 if (frag
->split_by
== 0) {
240 /* tree leaf, remove */
241 dout("fill_dirfrag removed %llx.%llx frag %x"
242 " (no ref)\n", ceph_vinop(inode
), id
);
243 rb_erase(&frag
->node
, &ci
->i_fragtree
);
246 /* tree branch, keep and clear */
247 dout("fill_dirfrag cleared %llx.%llx frag %x"
248 " referral\n", ceph_vinop(inode
), id
);
256 /* find/add this frag to store mds delegation info */
257 frag
= __get_or_create_frag(ci
, id
);
259 /* this is not the end of the world; we can continue
260 with bad/inaccurate delegation info */
261 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
262 ceph_vinop(inode
), le32_to_cpu(dirinfo
->frag
));
268 frag
->ndist
= min_t(u32
, ndist
, CEPH_MAX_DIRFRAG_REP
);
269 for (i
= 0; i
< frag
->ndist
; i
++)
270 frag
->dist
[i
] = le32_to_cpu(dirinfo
->dist
[i
]);
271 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
272 ceph_vinop(inode
), frag
->frag
, frag
->ndist
);
275 mutex_unlock(&ci
->i_fragtree_mutex
);
281 * initialize a newly allocated inode.
283 struct inode
*ceph_alloc_inode(struct super_block
*sb
)
285 struct ceph_inode_info
*ci
;
288 ci
= kmem_cache_alloc(ceph_inode_cachep
, GFP_NOFS
);
292 dout("alloc_inode %p\n", &ci
->vfs_inode
);
295 ci
->i_time_warp_seq
= 0;
296 ci
->i_ceph_flags
= 0;
297 ci
->i_release_count
= 0;
298 ci
->i_symlink
= NULL
;
300 ci
->i_fragtree
= RB_ROOT
;
301 mutex_init(&ci
->i_fragtree_mutex
);
303 ci
->i_xattrs
.blob
= NULL
;
304 ci
->i_xattrs
.prealloc_blob
= NULL
;
305 ci
->i_xattrs
.dirty
= false;
306 ci
->i_xattrs
.index
= RB_ROOT
;
307 ci
->i_xattrs
.count
= 0;
308 ci
->i_xattrs
.names_size
= 0;
309 ci
->i_xattrs
.vals_size
= 0;
310 ci
->i_xattrs
.version
= 0;
311 ci
->i_xattrs
.index_version
= 0;
313 ci
->i_caps
= RB_ROOT
;
314 ci
->i_auth_cap
= NULL
;
315 ci
->i_dirty_caps
= 0;
316 ci
->i_flushing_caps
= 0;
317 INIT_LIST_HEAD(&ci
->i_dirty_item
);
318 INIT_LIST_HEAD(&ci
->i_flushing_item
);
319 ci
->i_cap_flush_seq
= 0;
320 ci
->i_cap_flush_last_tid
= 0;
321 memset(&ci
->i_cap_flush_tid
, 0, sizeof(ci
->i_cap_flush_tid
));
322 init_waitqueue_head(&ci
->i_cap_wq
);
323 ci
->i_hold_caps_min
= 0;
324 ci
->i_hold_caps_max
= 0;
325 INIT_LIST_HEAD(&ci
->i_cap_delay_list
);
326 ci
->i_cap_exporting_mds
= 0;
327 ci
->i_cap_exporting_mseq
= 0;
328 ci
->i_cap_exporting_issued
= 0;
329 INIT_LIST_HEAD(&ci
->i_cap_snaps
);
330 ci
->i_head_snapc
= NULL
;
333 for (i
= 0; i
< CEPH_FILE_MODE_NUM
; i
++)
334 ci
->i_nr_by_mode
[i
] = 0;
336 ci
->i_truncate_seq
= 0;
337 ci
->i_truncate_size
= 0;
338 ci
->i_truncate_pending
= 0;
341 ci
->i_reported_size
= 0;
342 ci
->i_wanted_max_size
= 0;
343 ci
->i_requested_max_size
= 0;
347 ci
->i_rdcache_ref
= 0;
349 ci
->i_wrbuffer_ref
= 0;
350 ci
->i_wrbuffer_ref_head
= 0;
351 ci
->i_shared_gen
= 0;
352 ci
->i_rdcache_gen
= 0;
353 ci
->i_rdcache_revoking
= 0;
355 INIT_LIST_HEAD(&ci
->i_unsafe_writes
);
356 INIT_LIST_HEAD(&ci
->i_unsafe_dirops
);
357 spin_lock_init(&ci
->i_unsafe_lock
);
359 ci
->i_snap_realm
= NULL
;
360 INIT_LIST_HEAD(&ci
->i_snap_realm_item
);
361 INIT_LIST_HEAD(&ci
->i_snap_flush_item
);
363 INIT_WORK(&ci
->i_wb_work
, ceph_writeback_work
);
364 INIT_WORK(&ci
->i_pg_inv_work
, ceph_invalidate_work
);
366 INIT_WORK(&ci
->i_vmtruncate_work
, ceph_vmtruncate_work
);
368 return &ci
->vfs_inode
;
371 void ceph_destroy_inode(struct inode
*inode
)
373 struct ceph_inode_info
*ci
= ceph_inode(inode
);
374 struct ceph_inode_frag
*frag
;
377 dout("destroy_inode %p ino %llx.%llx\n", inode
, ceph_vinop(inode
));
379 ceph_queue_caps_release(inode
);
381 kfree(ci
->i_symlink
);
382 while ((n
= rb_first(&ci
->i_fragtree
)) != NULL
) {
383 frag
= rb_entry(n
, struct ceph_inode_frag
, node
);
384 rb_erase(n
, &ci
->i_fragtree
);
388 __ceph_destroy_xattrs(ci
);
389 if (ci
->i_xattrs
.blob
)
390 ceph_buffer_put(ci
->i_xattrs
.blob
);
391 if (ci
->i_xattrs
.prealloc_blob
)
392 ceph_buffer_put(ci
->i_xattrs
.prealloc_blob
);
394 kmem_cache_free(ceph_inode_cachep
, ci
);
399 * Helpers to fill in size, ctime, mtime, and atime. We have to be
400 * careful because either the client or MDS may have more up to date
401 * info, depending on which capabilities are held, and whether
402 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
403 * and size are monotonically increasing, except when utimes() or
404 * truncate() increments the corresponding _seq values.)
406 int ceph_fill_file_size(struct inode
*inode
, int issued
,
407 u32 truncate_seq
, u64 truncate_size
, u64 size
)
409 struct ceph_inode_info
*ci
= ceph_inode(inode
);
412 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) > 0 ||
413 (truncate_seq
== ci
->i_truncate_seq
&& size
> inode
->i_size
)) {
414 dout("size %lld -> %llu\n", inode
->i_size
, size
);
415 inode
->i_size
= size
;
416 inode
->i_blocks
= (size
+ (1<<9) - 1) >> 9;
417 ci
->i_reported_size
= size
;
418 if (truncate_seq
!= ci
->i_truncate_seq
) {
419 dout("truncate_seq %u -> %u\n",
420 ci
->i_truncate_seq
, truncate_seq
);
421 ci
->i_truncate_seq
= truncate_seq
;
423 * If we hold relevant caps, or in the case where we're
424 * not the only client referencing this file and we
425 * don't hold those caps, then we need to check whether
426 * the file is either opened or mmaped
428 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_RD
|
429 CEPH_CAP_FILE_WR
|CEPH_CAP_FILE_BUFFER
|
430 CEPH_CAP_FILE_EXCL
)) ||
431 mapping_mapped(inode
->i_mapping
) ||
432 __ceph_caps_file_wanted(ci
)) {
433 ci
->i_truncate_pending
++;
438 if (ceph_seq_cmp(truncate_seq
, ci
->i_truncate_seq
) >= 0 &&
439 ci
->i_truncate_size
!= truncate_size
) {
440 dout("truncate_size %lld -> %llu\n", ci
->i_truncate_size
,
442 ci
->i_truncate_size
= truncate_size
;
447 void ceph_fill_file_time(struct inode
*inode
, int issued
,
448 u64 time_warp_seq
, struct timespec
*ctime
,
449 struct timespec
*mtime
, struct timespec
*atime
)
451 struct ceph_inode_info
*ci
= ceph_inode(inode
);
454 if (issued
& (CEPH_CAP_FILE_EXCL
|
456 CEPH_CAP_FILE_BUFFER
)) {
457 if (timespec_compare(ctime
, &inode
->i_ctime
) > 0) {
458 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
459 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
460 ctime
->tv_sec
, ctime
->tv_nsec
);
461 inode
->i_ctime
= *ctime
;
463 if (ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) > 0) {
464 /* the MDS did a utimes() */
465 dout("mtime %ld.%09ld -> %ld.%09ld "
467 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
468 mtime
->tv_sec
, mtime
->tv_nsec
,
469 ci
->i_time_warp_seq
, (int)time_warp_seq
);
471 inode
->i_mtime
= *mtime
;
472 inode
->i_atime
= *atime
;
473 ci
->i_time_warp_seq
= time_warp_seq
;
474 } else if (time_warp_seq
== ci
->i_time_warp_seq
) {
475 /* nobody did utimes(); take the max */
476 if (timespec_compare(mtime
, &inode
->i_mtime
) > 0) {
477 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
478 inode
->i_mtime
.tv_sec
,
479 inode
->i_mtime
.tv_nsec
,
480 mtime
->tv_sec
, mtime
->tv_nsec
);
481 inode
->i_mtime
= *mtime
;
483 if (timespec_compare(atime
, &inode
->i_atime
) > 0) {
484 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
485 inode
->i_atime
.tv_sec
,
486 inode
->i_atime
.tv_nsec
,
487 atime
->tv_sec
, atime
->tv_nsec
);
488 inode
->i_atime
= *atime
;
490 } else if (issued
& CEPH_CAP_FILE_EXCL
) {
491 /* we did a utimes(); ignore mds values */
496 /* we have no write caps; whatever the MDS says is true */
497 if (ceph_seq_cmp(time_warp_seq
, ci
->i_time_warp_seq
) >= 0) {
498 inode
->i_ctime
= *ctime
;
499 inode
->i_mtime
= *mtime
;
500 inode
->i_atime
= *atime
;
501 ci
->i_time_warp_seq
= time_warp_seq
;
506 if (warn
) /* time_warp_seq shouldn't go backwards */
507 dout("%p mds time_warp_seq %llu < %u\n",
508 inode
, time_warp_seq
, ci
->i_time_warp_seq
);
512 * Populate an inode based on info from mds. May be called on new or
515 static int fill_inode(struct inode
*inode
,
516 struct ceph_mds_reply_info_in
*iinfo
,
517 struct ceph_mds_reply_dirfrag
*dirinfo
,
518 struct ceph_mds_session
*session
,
519 unsigned long ttl_from
, int cap_fmode
,
520 struct ceph_cap_reservation
*caps_reservation
)
522 struct ceph_mds_reply_inode
*info
= iinfo
->in
;
523 struct ceph_inode_info
*ci
= ceph_inode(inode
);
525 int issued
, implemented
;
526 struct timespec mtime
, atime
, ctime
;
528 struct ceph_buffer
*xattr_blob
= NULL
;
532 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
533 inode
, ceph_vinop(inode
), le64_to_cpu(info
->version
),
537 * prealloc xattr data, if it looks like we'll need it. only
538 * if len > 4 (meaning there are actually xattrs; the first 4
539 * bytes are the xattr count).
541 if (iinfo
->xattr_len
> 4) {
542 xattr_blob
= ceph_buffer_new(iinfo
->xattr_len
, GFP_NOFS
);
544 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
548 spin_lock(&inode
->i_lock
);
551 * provided version will be odd if inode value is projected,
552 * even if stable. skip the update if we have a newer info
553 * (e.g., due to inode info racing form multiple MDSs), or if
554 * we are getting projected (unstable) inode info.
556 if (le64_to_cpu(info
->version
) > 0 &&
557 (ci
->i_version
& ~1) > le64_to_cpu(info
->version
))
560 issued
= __ceph_caps_issued(ci
, &implemented
);
561 issued
|= implemented
| __ceph_caps_dirty(ci
);
564 ci
->i_version
= le64_to_cpu(info
->version
);
566 inode
->i_rdev
= le32_to_cpu(info
->rdev
);
568 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
569 inode
->i_mode
= le32_to_cpu(info
->mode
);
570 inode
->i_uid
= le32_to_cpu(info
->uid
);
571 inode
->i_gid
= le32_to_cpu(info
->gid
);
572 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
573 inode
->i_uid
, inode
->i_gid
);
576 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
577 inode
->i_nlink
= le32_to_cpu(info
->nlink
);
579 /* be careful with mtime, atime, size */
580 ceph_decode_timespec(&atime
, &info
->atime
);
581 ceph_decode_timespec(&mtime
, &info
->mtime
);
582 ceph_decode_timespec(&ctime
, &info
->ctime
);
583 queue_trunc
= ceph_fill_file_size(inode
, issued
,
584 le32_to_cpu(info
->truncate_seq
),
585 le64_to_cpu(info
->truncate_size
),
586 le64_to_cpu(info
->size
));
587 ceph_fill_file_time(inode
, issued
,
588 le32_to_cpu(info
->time_warp_seq
),
589 &ctime
, &mtime
, &atime
);
591 ci
->i_max_size
= le64_to_cpu(info
->max_size
);
592 ci
->i_layout
= info
->layout
;
593 inode
->i_blkbits
= fls(le32_to_cpu(info
->layout
.fl_stripe_unit
)) - 1;
596 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
597 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 &&
598 le64_to_cpu(info
->xattr_version
) > ci
->i_xattrs
.version
) {
599 if (ci
->i_xattrs
.blob
)
600 ceph_buffer_put(ci
->i_xattrs
.blob
);
601 ci
->i_xattrs
.blob
= xattr_blob
;
603 memcpy(ci
->i_xattrs
.blob
->vec
.iov_base
,
604 iinfo
->xattr_data
, iinfo
->xattr_len
);
605 ci
->i_xattrs
.version
= le64_to_cpu(info
->xattr_version
);
608 inode
->i_mapping
->a_ops
= &ceph_aops
;
609 inode
->i_mapping
->backing_dev_info
=
610 &ceph_client(inode
->i_sb
)->backing_dev_info
;
612 switch (inode
->i_mode
& S_IFMT
) {
617 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
618 inode
->i_op
= &ceph_file_iops
;
621 inode
->i_op
= &ceph_file_iops
;
622 inode
->i_fop
= &ceph_file_fops
;
625 inode
->i_op
= &ceph_symlink_iops
;
626 if (!ci
->i_symlink
) {
627 int symlen
= iinfo
->symlink_len
;
630 BUG_ON(symlen
!= inode
->i_size
);
631 spin_unlock(&inode
->i_lock
);
634 sym
= kmalloc(symlen
+1, GFP_NOFS
);
637 memcpy(sym
, iinfo
->symlink
, symlen
);
640 spin_lock(&inode
->i_lock
);
644 kfree(sym
); /* lost a race */
648 inode
->i_op
= &ceph_dir_iops
;
649 inode
->i_fop
= &ceph_dir_fops
;
651 ci
->i_files
= le64_to_cpu(info
->files
);
652 ci
->i_subdirs
= le64_to_cpu(info
->subdirs
);
653 ci
->i_rbytes
= le64_to_cpu(info
->rbytes
);
654 ci
->i_rfiles
= le64_to_cpu(info
->rfiles
);
655 ci
->i_rsubdirs
= le64_to_cpu(info
->rsubdirs
);
656 ceph_decode_timespec(&ci
->i_rctime
, &info
->rctime
);
658 /* set dir completion flag? */
659 if (ci
->i_files
== 0 && ci
->i_subdirs
== 0 &&
660 ceph_snap(inode
) == CEPH_NOSNAP
&&
661 (le32_to_cpu(info
->cap
.caps
) & CEPH_CAP_FILE_SHARED
)) {
662 dout(" marking %p complete (empty)\n", inode
);
663 ci
->i_ceph_flags
|= CEPH_I_COMPLETE
;
664 ci
->i_max_offset
= 2;
667 /* it may be better to set st_size in getattr instead? */
668 if (ceph_test_opt(ceph_client(inode
->i_sb
), RBYTES
))
669 inode
->i_size
= ci
->i_rbytes
;
672 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
673 ceph_vinop(inode
), inode
->i_mode
);
677 spin_unlock(&inode
->i_lock
);
679 /* queue truncate if we saw i_size decrease */
681 ceph_queue_vmtruncate(inode
);
683 /* populate frag tree */
684 /* FIXME: move me up, if/when version reflects fragtree changes */
685 nsplits
= le32_to_cpu(info
->fragtree
.nsplits
);
686 mutex_lock(&ci
->i_fragtree_mutex
);
687 for (i
= 0; i
< nsplits
; i
++) {
688 u32 id
= le32_to_cpu(info
->fragtree
.splits
[i
].frag
);
689 struct ceph_inode_frag
*frag
= __get_or_create_frag(ci
, id
);
693 frag
->split_by
= le32_to_cpu(info
->fragtree
.splits
[i
].by
);
694 dout(" frag %x split by %d\n", frag
->frag
, frag
->split_by
);
696 mutex_unlock(&ci
->i_fragtree_mutex
);
698 /* were we issued a capability? */
699 if (info
->cap
.caps
) {
700 if (ceph_snap(inode
) == CEPH_NOSNAP
) {
701 ceph_add_cap(inode
, session
,
702 le64_to_cpu(info
->cap
.cap_id
),
704 le32_to_cpu(info
->cap
.caps
),
705 le32_to_cpu(info
->cap
.wanted
),
706 le32_to_cpu(info
->cap
.seq
),
707 le32_to_cpu(info
->cap
.mseq
),
708 le64_to_cpu(info
->cap
.realm
),
712 spin_lock(&inode
->i_lock
);
713 dout(" %p got snap_caps %s\n", inode
,
714 ceph_cap_string(le32_to_cpu(info
->cap
.caps
)));
715 ci
->i_snap_caps
|= le32_to_cpu(info
->cap
.caps
);
717 __ceph_get_fmode(ci
, cap_fmode
);
718 spin_unlock(&inode
->i_lock
);
722 /* update delegation info? */
724 ceph_fill_dirfrag(inode
, dirinfo
);
730 ceph_buffer_put(xattr_blob
);
735 * caller should hold session s_mutex.
737 static void update_dentry_lease(struct dentry
*dentry
,
738 struct ceph_mds_reply_lease
*lease
,
739 struct ceph_mds_session
*session
,
740 unsigned long from_time
)
742 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
743 long unsigned duration
= le32_to_cpu(lease
->duration_ms
);
744 long unsigned ttl
= from_time
+ (duration
* HZ
) / 1000;
745 long unsigned half_ttl
= from_time
+ (duration
* HZ
/ 2) / 1000;
748 /* only track leases on regular dentries */
749 if (dentry
->d_op
!= &ceph_dentry_ops
)
752 spin_lock(&dentry
->d_lock
);
753 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
754 dentry
, le16_to_cpu(lease
->mask
), duration
, ttl
);
756 /* make lease_rdcache_gen match directory */
757 dir
= dentry
->d_parent
->d_inode
;
758 di
->lease_shared_gen
= ceph_inode(dir
)->i_shared_gen
;
760 if (lease
->mask
== 0)
763 if (di
->lease_gen
== session
->s_cap_gen
&&
764 time_before(ttl
, dentry
->d_time
))
765 goto out_unlock
; /* we already have a newer lease. */
767 if (di
->lease_session
&& di
->lease_session
!= session
)
770 ceph_dentry_lru_touch(dentry
);
772 if (!di
->lease_session
)
773 di
->lease_session
= ceph_get_mds_session(session
);
774 di
->lease_gen
= session
->s_cap_gen
;
775 di
->lease_seq
= le32_to_cpu(lease
->seq
);
776 di
->lease_renew_after
= half_ttl
;
777 di
->lease_renew_from
= 0;
778 dentry
->d_time
= ttl
;
780 spin_unlock(&dentry
->d_lock
);
785 * splice a dentry to an inode.
786 * caller must hold directory i_mutex for this to be safe.
788 * we will only rehash the resulting dentry if @prehash is
789 * true; @prehash will be set to false (for the benefit of
790 * the caller) if we fail.
792 static struct dentry
*splice_dentry(struct dentry
*dn
, struct inode
*in
,
795 struct dentry
*realdn
;
797 /* dn must be unhashed */
800 realdn
= d_materialise_unique(dn
, in
);
801 if (IS_ERR(realdn
)) {
802 pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
803 dn
, in
, ceph_vinop(in
));
805 *prehash
= false; /* don't rehash on error */
806 dn
= realdn
; /* note realdn contains the error */
809 dout("dn %p (%d) spliced with %p (%d) "
810 "inode %p ino %llx.%llx\n",
811 dn
, atomic_read(&dn
->d_count
),
812 realdn
, atomic_read(&realdn
->d_count
),
813 realdn
->d_inode
, ceph_vinop(realdn
->d_inode
));
817 BUG_ON(!ceph_dentry(dn
));
819 dout("dn %p attached to %p ino %llx.%llx\n",
820 dn
, dn
->d_inode
, ceph_vinop(dn
->d_inode
));
822 if ((!prehash
|| *prehash
) && d_unhashed(dn
))
829 * Set dentry's directory position based on the current dir's max, and
830 * order it in d_subdirs, so that dcache_readdir behaves.
832 static void ceph_set_dentry_offset(struct dentry
*dn
)
834 struct dentry
*dir
= dn
->d_parent
;
835 struct inode
*inode
= dn
->d_parent
->d_inode
;
836 struct ceph_dentry_info
*di
;
840 di
= ceph_dentry(dn
);
842 spin_lock(&inode
->i_lock
);
843 di
->offset
= ceph_inode(inode
)->i_max_offset
++;
844 spin_unlock(&inode
->i_lock
);
846 spin_lock(&dcache_lock
);
847 spin_lock(&dn
->d_lock
);
848 list_move_tail(&dir
->d_subdirs
, &dn
->d_u
.d_child
);
849 dout("set_dentry_offset %p %lld (%p %p)\n", dn
, di
->offset
,
850 dn
->d_u
.d_child
.prev
, dn
->d_u
.d_child
.next
);
851 spin_unlock(&dn
->d_lock
);
852 spin_unlock(&dcache_lock
);
856 * Incorporate results into the local cache. This is either just
857 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
860 * A reply may contain
861 * a directory inode along with a dentry.
862 * and/or a target inode
864 * Called with snap_rwsem (read).
866 int ceph_fill_trace(struct super_block
*sb
, struct ceph_mds_request
*req
,
867 struct ceph_mds_session
*session
)
869 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
870 struct inode
*in
= NULL
;
871 struct ceph_mds_reply_inode
*ininfo
;
872 struct ceph_vino vino
;
876 dout("fill_trace %p is_dentry %d is_target %d\n", req
,
877 rinfo
->head
->is_dentry
, rinfo
->head
->is_target
);
883 * If we resend completed ops to a recovering mds, we get no
884 * trace. Since that is very rare, pretend this is the case
885 * to ensure the 'no trace' handlers in the callers behave.
887 * Fill in inodes unconditionally to avoid breaking cap
890 if (rinfo
->head
->op
& CEPH_MDS_OP_WRITE
) {
891 pr_info("fill_trace faking empty trace on %lld %s\n",
892 req
->r_tid
, ceph_mds_op_name(rinfo
->head
->op
));
893 if (rinfo
->head
->is_dentry
) {
894 rinfo
->head
->is_dentry
= 0;
895 err
= fill_inode(req
->r_locked_dir
,
896 &rinfo
->diri
, rinfo
->dirfrag
,
897 session
, req
->r_request_started
, -1);
899 if (rinfo
->head
->is_target
) {
900 rinfo
->head
->is_target
= 0;
901 ininfo
= rinfo
->targeti
.in
;
902 vino
.ino
= le64_to_cpu(ininfo
->ino
);
903 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
904 in
= ceph_get_inode(sb
, vino
);
905 err
= fill_inode(in
, &rinfo
->targeti
, NULL
,
906 session
, req
->r_request_started
,
913 if (!rinfo
->head
->is_target
&& !rinfo
->head
->is_dentry
) {
914 dout("fill_trace reply is empty!\n");
915 if (rinfo
->head
->result
== 0 && req
->r_locked_dir
) {
916 struct ceph_inode_info
*ci
=
917 ceph_inode(req
->r_locked_dir
);
918 dout(" clearing %p complete (empty trace)\n",
920 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
921 ci
->i_release_count
++;
926 if (rinfo
->head
->is_dentry
) {
927 struct inode
*dir
= req
->r_locked_dir
;
929 err
= fill_inode(dir
, &rinfo
->diri
, rinfo
->dirfrag
,
930 session
, req
->r_request_started
, -1,
931 &req
->r_caps_reservation
);
936 if (rinfo
->head
->is_dentry
&& !req
->r_aborted
) {
938 * lookup link rename : null -> possibly existing inode
939 * mknod symlink mkdir : null -> new inode
940 * unlink : linked -> null
942 struct inode
*dir
= req
->r_locked_dir
;
943 struct dentry
*dn
= req
->r_dentry
;
944 bool have_dir_cap
, have_lease
;
948 BUG_ON(dn
->d_parent
->d_inode
!= dir
);
949 BUG_ON(ceph_ino(dir
) !=
950 le64_to_cpu(rinfo
->diri
.in
->ino
));
951 BUG_ON(ceph_snap(dir
) !=
952 le64_to_cpu(rinfo
->diri
.in
->snapid
));
954 /* do we have a lease on the whole dir? */
956 (le32_to_cpu(rinfo
->diri
.in
->cap
.caps
) &
957 CEPH_CAP_FILE_SHARED
);
959 /* do we have a dn lease? */
960 have_lease
= have_dir_cap
||
961 (le16_to_cpu(rinfo
->dlease
->mask
) &
965 dout("fill_trace no dentry lease or dir cap\n");
968 if (req
->r_old_dentry
&& req
->r_op
== CEPH_MDS_OP_RENAME
) {
969 dout(" src %p '%.*s' dst %p '%.*s'\n",
971 req
->r_old_dentry
->d_name
.len
,
972 req
->r_old_dentry
->d_name
.name
,
973 dn
, dn
->d_name
.len
, dn
->d_name
.name
);
974 dout("fill_trace doing d_move %p -> %p\n",
975 req
->r_old_dentry
, dn
);
976 d_move(req
->r_old_dentry
, dn
);
977 dout(" src %p '%.*s' dst %p '%.*s'\n",
979 req
->r_old_dentry
->d_name
.len
,
980 req
->r_old_dentry
->d_name
.name
,
981 dn
, dn
->d_name
.len
, dn
->d_name
.name
);
982 /* ensure target dentry is invalidated, despite
983 rehashing bug in vfs_rename_dir */
984 dn
->d_time
= jiffies
;
985 ceph_dentry(dn
)->lease_shared_gen
= 0;
986 /* take overwritten dentry's readdir offset */
987 ceph_dentry(req
->r_old_dentry
)->offset
=
988 ceph_dentry(dn
)->offset
;
989 dn
= req
->r_old_dentry
; /* use old_dentry */
994 if (!rinfo
->head
->is_target
) {
995 dout("fill_trace null dentry\n");
997 dout("d_delete %p\n", dn
);
1000 dout("d_instantiate %p NULL\n", dn
);
1001 d_instantiate(dn
, NULL
);
1002 if (have_lease
&& d_unhashed(dn
))
1004 update_dentry_lease(dn
, rinfo
->dlease
,
1006 req
->r_request_started
);
1011 /* attach proper inode */
1012 ininfo
= rinfo
->targeti
.in
;
1013 vino
.ino
= le64_to_cpu(ininfo
->ino
);
1014 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
1016 in
= ceph_get_inode(sb
, vino
);
1018 pr_err("fill_trace bad get_inode "
1019 "%llx.%llx\n", vino
.ino
, vino
.snap
);
1024 dn
= splice_dentry(dn
, in
, &have_lease
);
1029 req
->r_dentry
= dn
; /* may have spliced */
1030 ceph_set_dentry_offset(dn
);
1032 } else if (ceph_ino(in
) == vino
.ino
&&
1033 ceph_snap(in
) == vino
.snap
) {
1036 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1037 dn
, in
, ceph_ino(in
), ceph_snap(in
),
1038 vino
.ino
, vino
.snap
);
1044 update_dentry_lease(dn
, rinfo
->dlease
, session
,
1045 req
->r_request_started
);
1046 dout(" final dn %p\n", dn
);
1048 } else if (req
->r_op
== CEPH_MDS_OP_LOOKUPSNAP
||
1049 req
->r_op
== CEPH_MDS_OP_MKSNAP
) {
1050 struct dentry
*dn
= req
->r_dentry
;
1052 /* fill out a snapdir LOOKUPSNAP dentry */
1054 BUG_ON(!req
->r_locked_dir
);
1055 BUG_ON(ceph_snap(req
->r_locked_dir
) != CEPH_SNAPDIR
);
1056 ininfo
= rinfo
->targeti
.in
;
1057 vino
.ino
= le64_to_cpu(ininfo
->ino
);
1058 vino
.snap
= le64_to_cpu(ininfo
->snapid
);
1059 in
= ceph_get_inode(sb
, vino
);
1061 pr_err("fill_inode get_inode badness %llx.%llx\n",
1062 vino
.ino
, vino
.snap
);
1067 dout(" linking snapped dir %p to dn %p\n", in
, dn
);
1068 dn
= splice_dentry(dn
, in
, NULL
);
1073 ceph_set_dentry_offset(dn
);
1074 req
->r_dentry
= dn
; /* may have spliced */
1076 rinfo
->head
->is_dentry
= 1; /* fool notrace handlers */
1079 if (rinfo
->head
->is_target
) {
1080 vino
.ino
= le64_to_cpu(rinfo
->targeti
.in
->ino
);
1081 vino
.snap
= le64_to_cpu(rinfo
->targeti
.in
->snapid
);
1083 if (in
== NULL
|| ceph_ino(in
) != vino
.ino
||
1084 ceph_snap(in
) != vino
.snap
) {
1085 in
= ceph_get_inode(sb
, vino
);
1091 req
->r_target_inode
= in
;
1093 err
= fill_inode(in
,
1094 &rinfo
->targeti
, NULL
,
1095 session
, req
->r_request_started
,
1096 (le32_to_cpu(rinfo
->head
->result
) == 0) ?
1098 &req
->r_caps_reservation
);
1100 pr_err("fill_inode badness %p %llx.%llx\n",
1101 in
, ceph_vinop(in
));
1107 dout("fill_trace done err=%d\n", err
);
1112 * Prepopulate our cache with readdir results, leases, etc.
1114 int ceph_readdir_prepopulate(struct ceph_mds_request
*req
,
1115 struct ceph_mds_session
*session
)
1117 struct dentry
*parent
= req
->r_dentry
;
1118 struct ceph_mds_reply_info_parsed
*rinfo
= &req
->r_reply_info
;
1123 struct inode
*snapdir
= NULL
;
1124 struct ceph_mds_request_head
*rhead
= req
->r_request
->front
.iov_base
;
1125 u64 frag
= le32_to_cpu(rhead
->args
.readdir
.frag
);
1126 struct ceph_dentry_info
*di
;
1128 if (le32_to_cpu(rinfo
->head
->op
) == CEPH_MDS_OP_LSSNAP
) {
1129 snapdir
= ceph_get_snapdir(parent
->d_inode
);
1130 parent
= d_find_alias(snapdir
);
1131 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1132 rinfo
->dir_nr
, parent
);
1134 dout("readdir_prepopulate %d items under dn %p\n",
1135 rinfo
->dir_nr
, parent
);
1137 ceph_fill_dirfrag(parent
->d_inode
, rinfo
->dir_dir
);
1140 for (i
= 0; i
< rinfo
->dir_nr
; i
++) {
1141 struct ceph_vino vino
;
1143 dname
.name
= rinfo
->dir_dname
[i
];
1144 dname
.len
= rinfo
->dir_dname_len
[i
];
1145 dname
.hash
= full_name_hash(dname
.name
, dname
.len
);
1147 vino
.ino
= le64_to_cpu(rinfo
->dir_in
[i
].in
->ino
);
1148 vino
.snap
= le64_to_cpu(rinfo
->dir_in
[i
].in
->snapid
);
1151 dn
= d_lookup(parent
, &dname
);
1152 dout("d_lookup on parent=%p name=%.*s got %p\n",
1153 parent
, dname
.len
, dname
.name
, dn
);
1156 dn
= d_alloc(parent
, &dname
);
1157 dout("d_alloc %p '%.*s' = %p\n", parent
,
1158 dname
.len
, dname
.name
, dn
);
1160 dout("d_alloc badness\n");
1164 err
= ceph_init_dentry(dn
);
1167 } else if (dn
->d_inode
&&
1168 (ceph_ino(dn
->d_inode
) != vino
.ino
||
1169 ceph_snap(dn
->d_inode
) != vino
.snap
)) {
1170 dout(" dn %p points to wrong inode %p\n",
1176 /* reorder parent's d_subdirs */
1177 spin_lock(&dcache_lock
);
1178 spin_lock(&dn
->d_lock
);
1179 list_move(&dn
->d_u
.d_child
, &parent
->d_subdirs
);
1180 spin_unlock(&dn
->d_lock
);
1181 spin_unlock(&dcache_lock
);
1185 di
->offset
= ceph_make_fpos(frag
, i
+ req
->r_readdir_offset
);
1191 in
= ceph_get_inode(parent
->d_sb
, vino
);
1193 dout("new_inode badness\n");
1199 dn
= splice_dentry(dn
, in
, NULL
);
1202 if (fill_inode(in
, &rinfo
->dir_in
[i
], NULL
, session
,
1203 req
->r_request_started
, -1,
1204 &req
->r_caps_reservation
) < 0) {
1205 pr_err("fill_inode badness on %p\n", in
);
1209 update_dentry_lease(dn
, rinfo
->dir_dlease
[i
],
1210 req
->r_session
, req
->r_request_started
);
1213 req
->r_did_prepopulate
= true;
1220 dout("readdir_prepopulate done\n");
1224 int ceph_inode_set_size(struct inode
*inode
, loff_t size
)
1226 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1229 spin_lock(&inode
->i_lock
);
1230 dout("set_size %p %llu -> %llu\n", inode
, inode
->i_size
, size
);
1231 inode
->i_size
= size
;
1232 inode
->i_blocks
= (size
+ (1 << 9) - 1) >> 9;
1234 /* tell the MDS if we are approaching max_size */
1235 if ((size
<< 1) >= ci
->i_max_size
&&
1236 (ci
->i_reported_size
<< 1) < ci
->i_max_size
)
1239 spin_unlock(&inode
->i_lock
);
1244 * Write back inode data in a worker thread. (This can't be done
1245 * in the message handler context.)
1247 void ceph_queue_writeback(struct inode
*inode
)
1249 if (queue_work(ceph_inode_to_client(inode
)->wb_wq
,
1250 &ceph_inode(inode
)->i_wb_work
)) {
1251 dout("ceph_queue_writeback %p\n", inode
);
1254 dout("ceph_queue_writeback %p failed\n", inode
);
1258 static void ceph_writeback_work(struct work_struct
*work
)
1260 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1262 struct inode
*inode
= &ci
->vfs_inode
;
1264 dout("writeback %p\n", inode
);
1265 filemap_fdatawrite(&inode
->i_data
);
1270 * queue an async invalidation
1272 void ceph_queue_invalidate(struct inode
*inode
)
1274 if (queue_work(ceph_inode_to_client(inode
)->pg_inv_wq
,
1275 &ceph_inode(inode
)->i_pg_inv_work
)) {
1276 dout("ceph_queue_invalidate %p\n", inode
);
1279 dout("ceph_queue_invalidate %p failed\n", inode
);
1284 * invalidate any pages that are not dirty or under writeback. this
1285 * includes pages that are clean and mapped.
1287 static void ceph_invalidate_nondirty_pages(struct address_space
*mapping
)
1289 struct pagevec pvec
;
1293 pagevec_init(&pvec
, 0);
1294 while (pagevec_lookup(&pvec
, mapping
, next
, PAGEVEC_SIZE
)) {
1295 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
1296 struct page
*page
= pvec
.pages
[i
];
1299 (PageDirty(page
) || PageWriteback(page
));
1302 skip_page
= !trylock_page(page
);
1305 * We really shouldn't be looking at the ->index of an
1306 * unlocked page. But we're not allowed to lock these
1307 * pages. So we rely upon nobody altering the ->index
1308 * of this (pinned-by-us) page.
1310 index
= page
->index
;
1318 generic_error_remove_page(mapping
, page
);
1321 pagevec_release(&pvec
);
1327 * Invalidate inode pages in a worker thread. (This can't be done
1328 * in the message handler context.)
1330 static void ceph_invalidate_work(struct work_struct
*work
)
1332 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1334 struct inode
*inode
= &ci
->vfs_inode
;
1338 spin_lock(&inode
->i_lock
);
1339 dout("invalidate_pages %p gen %d revoking %d\n", inode
,
1340 ci
->i_rdcache_gen
, ci
->i_rdcache_revoking
);
1341 if (ci
->i_rdcache_gen
== 0 ||
1342 ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
1343 BUG_ON(ci
->i_rdcache_revoking
> ci
->i_rdcache_gen
);
1345 ci
->i_rdcache_revoking
= 0;
1346 spin_unlock(&inode
->i_lock
);
1349 orig_gen
= ci
->i_rdcache_gen
;
1350 spin_unlock(&inode
->i_lock
);
1352 ceph_invalidate_nondirty_pages(inode
->i_mapping
);
1354 spin_lock(&inode
->i_lock
);
1355 if (orig_gen
== ci
->i_rdcache_gen
) {
1356 dout("invalidate_pages %p gen %d successful\n", inode
,
1358 ci
->i_rdcache_gen
= 0;
1359 ci
->i_rdcache_revoking
= 0;
1362 dout("invalidate_pages %p gen %d raced, gen now %d\n",
1363 inode
, orig_gen
, ci
->i_rdcache_gen
);
1365 spin_unlock(&inode
->i_lock
);
1368 ceph_check_caps(ci
, 0, NULL
);
1375 * called by trunc_wq; take i_mutex ourselves
1377 * We also truncate in a separate thread as well.
1379 static void ceph_vmtruncate_work(struct work_struct
*work
)
1381 struct ceph_inode_info
*ci
= container_of(work
, struct ceph_inode_info
,
1383 struct inode
*inode
= &ci
->vfs_inode
;
1385 dout("vmtruncate_work %p\n", inode
);
1386 mutex_lock(&inode
->i_mutex
);
1387 __ceph_do_pending_vmtruncate(inode
);
1388 mutex_unlock(&inode
->i_mutex
);
1393 * Queue an async vmtruncate. If we fail to queue work, we will handle
1394 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1396 void ceph_queue_vmtruncate(struct inode
*inode
)
1398 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1400 if (queue_work(ceph_client(inode
->i_sb
)->trunc_wq
,
1401 &ci
->i_vmtruncate_work
)) {
1402 dout("ceph_queue_vmtruncate %p\n", inode
);
1405 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1406 inode
, ci
->i_truncate_pending
);
1411 * called with i_mutex held.
1413 * Make sure any pending truncation is applied before doing anything
1414 * that may depend on it.
1416 void __ceph_do_pending_vmtruncate(struct inode
*inode
)
1418 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1420 int wrbuffer_refs
, wake
= 0;
1423 spin_lock(&inode
->i_lock
);
1424 if (ci
->i_truncate_pending
== 0) {
1425 dout("__do_pending_vmtruncate %p none pending\n", inode
);
1426 spin_unlock(&inode
->i_lock
);
1431 * make sure any dirty snapped pages are flushed before we
1432 * possibly truncate them.. so write AND block!
1434 if (ci
->i_wrbuffer_ref_head
< ci
->i_wrbuffer_ref
) {
1435 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1437 spin_unlock(&inode
->i_lock
);
1438 filemap_write_and_wait_range(&inode
->i_data
, 0,
1439 inode
->i_sb
->s_maxbytes
);
1443 to
= ci
->i_truncate_size
;
1444 wrbuffer_refs
= ci
->i_wrbuffer_ref
;
1445 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode
,
1446 ci
->i_truncate_pending
, to
);
1447 spin_unlock(&inode
->i_lock
);
1449 truncate_inode_pages(inode
->i_mapping
, to
);
1451 spin_lock(&inode
->i_lock
);
1452 ci
->i_truncate_pending
--;
1453 if (ci
->i_truncate_pending
== 0)
1455 spin_unlock(&inode
->i_lock
);
1457 if (wrbuffer_refs
== 0)
1458 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
1460 wake_up(&ci
->i_cap_wq
);
1467 static void *ceph_sym_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1469 struct ceph_inode_info
*ci
= ceph_inode(dentry
->d_inode
);
1470 nd_set_link(nd
, ci
->i_symlink
);
1474 static const struct inode_operations ceph_symlink_iops
= {
1475 .readlink
= generic_readlink
,
1476 .follow_link
= ceph_sym_follow_link
,
1482 int ceph_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1484 struct inode
*inode
= dentry
->d_inode
;
1485 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1486 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
1487 const unsigned int ia_valid
= attr
->ia_valid
;
1488 struct ceph_mds_request
*req
;
1489 struct ceph_mds_client
*mdsc
= &ceph_client(dentry
->d_sb
)->mdsc
;
1491 int release
= 0, dirtied
= 0;
1495 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1498 __ceph_do_pending_vmtruncate(inode
);
1500 err
= inode_change_ok(inode
, attr
);
1504 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SETATTR
,
1507 return PTR_ERR(req
);
1509 spin_lock(&inode
->i_lock
);
1510 issued
= __ceph_caps_issued(ci
, NULL
);
1511 dout("setattr %p issued %s\n", inode
, ceph_cap_string(issued
));
1513 if (ia_valid
& ATTR_UID
) {
1514 dout("setattr %p uid %d -> %d\n", inode
,
1515 inode
->i_uid
, attr
->ia_uid
);
1516 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1517 inode
->i_uid
= attr
->ia_uid
;
1518 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1519 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1520 attr
->ia_uid
!= inode
->i_uid
) {
1521 req
->r_args
.setattr
.uid
= cpu_to_le32(attr
->ia_uid
);
1522 mask
|= CEPH_SETATTR_UID
;
1523 release
|= CEPH_CAP_AUTH_SHARED
;
1526 if (ia_valid
& ATTR_GID
) {
1527 dout("setattr %p gid %d -> %d\n", inode
,
1528 inode
->i_gid
, attr
->ia_gid
);
1529 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1530 inode
->i_gid
= attr
->ia_gid
;
1531 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1532 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1533 attr
->ia_gid
!= inode
->i_gid
) {
1534 req
->r_args
.setattr
.gid
= cpu_to_le32(attr
->ia_gid
);
1535 mask
|= CEPH_SETATTR_GID
;
1536 release
|= CEPH_CAP_AUTH_SHARED
;
1539 if (ia_valid
& ATTR_MODE
) {
1540 dout("setattr %p mode 0%o -> 0%o\n", inode
, inode
->i_mode
,
1542 if (issued
& CEPH_CAP_AUTH_EXCL
) {
1543 inode
->i_mode
= attr
->ia_mode
;
1544 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1545 } else if ((issued
& CEPH_CAP_AUTH_SHARED
) == 0 ||
1546 attr
->ia_mode
!= inode
->i_mode
) {
1547 req
->r_args
.setattr
.mode
= cpu_to_le32(attr
->ia_mode
);
1548 mask
|= CEPH_SETATTR_MODE
;
1549 release
|= CEPH_CAP_AUTH_SHARED
;
1553 if (ia_valid
& ATTR_ATIME
) {
1554 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode
,
1555 inode
->i_atime
.tv_sec
, inode
->i_atime
.tv_nsec
,
1556 attr
->ia_atime
.tv_sec
, attr
->ia_atime
.tv_nsec
);
1557 if (issued
& CEPH_CAP_FILE_EXCL
) {
1558 ci
->i_time_warp_seq
++;
1559 inode
->i_atime
= attr
->ia_atime
;
1560 dirtied
|= CEPH_CAP_FILE_EXCL
;
1561 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
1562 timespec_compare(&inode
->i_atime
,
1563 &attr
->ia_atime
) < 0) {
1564 inode
->i_atime
= attr
->ia_atime
;
1565 dirtied
|= CEPH_CAP_FILE_WR
;
1566 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1567 !timespec_equal(&inode
->i_atime
, &attr
->ia_atime
)) {
1568 ceph_encode_timespec(&req
->r_args
.setattr
.atime
,
1570 mask
|= CEPH_SETATTR_ATIME
;
1571 release
|= CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_RD
|
1575 if (ia_valid
& ATTR_MTIME
) {
1576 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode
,
1577 inode
->i_mtime
.tv_sec
, inode
->i_mtime
.tv_nsec
,
1578 attr
->ia_mtime
.tv_sec
, attr
->ia_mtime
.tv_nsec
);
1579 if (issued
& CEPH_CAP_FILE_EXCL
) {
1580 ci
->i_time_warp_seq
++;
1581 inode
->i_mtime
= attr
->ia_mtime
;
1582 dirtied
|= CEPH_CAP_FILE_EXCL
;
1583 } else if ((issued
& CEPH_CAP_FILE_WR
) &&
1584 timespec_compare(&inode
->i_mtime
,
1585 &attr
->ia_mtime
) < 0) {
1586 inode
->i_mtime
= attr
->ia_mtime
;
1587 dirtied
|= CEPH_CAP_FILE_WR
;
1588 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1589 !timespec_equal(&inode
->i_mtime
, &attr
->ia_mtime
)) {
1590 ceph_encode_timespec(&req
->r_args
.setattr
.mtime
,
1592 mask
|= CEPH_SETATTR_MTIME
;
1593 release
|= CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_RD
|
1597 if (ia_valid
& ATTR_SIZE
) {
1598 dout("setattr %p size %lld -> %lld\n", inode
,
1599 inode
->i_size
, attr
->ia_size
);
1600 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
) {
1604 if ((issued
& CEPH_CAP_FILE_EXCL
) &&
1605 attr
->ia_size
> inode
->i_size
) {
1606 inode
->i_size
= attr
->ia_size
;
1608 (attr
->ia_size
+ (1 << 9) - 1) >> 9;
1609 inode
->i_ctime
= attr
->ia_ctime
;
1610 ci
->i_reported_size
= attr
->ia_size
;
1611 dirtied
|= CEPH_CAP_FILE_EXCL
;
1612 } else if ((issued
& CEPH_CAP_FILE_SHARED
) == 0 ||
1613 attr
->ia_size
!= inode
->i_size
) {
1614 req
->r_args
.setattr
.size
= cpu_to_le64(attr
->ia_size
);
1615 req
->r_args
.setattr
.old_size
=
1616 cpu_to_le64(inode
->i_size
);
1617 mask
|= CEPH_SETATTR_SIZE
;
1618 release
|= CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_RD
|
1623 /* these do nothing */
1624 if (ia_valid
& ATTR_CTIME
) {
1625 bool only
= (ia_valid
& (ATTR_SIZE
|ATTR_MTIME
|ATTR_ATIME
|
1626 ATTR_MODE
|ATTR_UID
|ATTR_GID
)) == 0;
1627 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode
,
1628 inode
->i_ctime
.tv_sec
, inode
->i_ctime
.tv_nsec
,
1629 attr
->ia_ctime
.tv_sec
, attr
->ia_ctime
.tv_nsec
,
1630 only
? "ctime only" : "ignored");
1631 inode
->i_ctime
= attr
->ia_ctime
;
1634 * if kernel wants to dirty ctime but nothing else,
1635 * we need to choose a cap to dirty under, or do
1636 * a almost-no-op setattr
1638 if (issued
& CEPH_CAP_AUTH_EXCL
)
1639 dirtied
|= CEPH_CAP_AUTH_EXCL
;
1640 else if (issued
& CEPH_CAP_FILE_EXCL
)
1641 dirtied
|= CEPH_CAP_FILE_EXCL
;
1642 else if (issued
& CEPH_CAP_XATTR_EXCL
)
1643 dirtied
|= CEPH_CAP_XATTR_EXCL
;
1645 mask
|= CEPH_SETATTR_CTIME
;
1648 if (ia_valid
& ATTR_FILE
)
1649 dout("setattr %p ATTR_FILE ... hrm!\n", inode
);
1652 __ceph_mark_dirty_caps(ci
, dirtied
);
1653 inode
->i_ctime
= CURRENT_TIME
;
1657 spin_unlock(&inode
->i_lock
);
1660 req
->r_inode
= igrab(inode
);
1661 req
->r_inode_drop
= release
;
1662 req
->r_args
.setattr
.mask
= cpu_to_le32(mask
);
1663 req
->r_num_caps
= 1;
1664 err
= ceph_mdsc_do_request(mdsc
, parent_inode
, req
);
1666 dout("setattr %p result=%d (%s locally, %d remote)\n", inode
, err
,
1667 ceph_cap_string(dirtied
), mask
);
1669 ceph_mdsc_put_request(req
);
1670 __ceph_do_pending_vmtruncate(inode
);
1673 spin_unlock(&inode
->i_lock
);
1674 ceph_mdsc_put_request(req
);
1679 * Verify that we have a lease on the given mask. If not,
1680 * do a getattr against an mds.
1682 int ceph_do_getattr(struct inode
*inode
, int mask
)
1684 struct ceph_client
*client
= ceph_sb_to_client(inode
->i_sb
);
1685 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1686 struct ceph_mds_request
*req
;
1689 if (ceph_snap(inode
) == CEPH_SNAPDIR
) {
1690 dout("do_getattr inode %p SNAPDIR\n", inode
);
1694 dout("do_getattr inode %p mask %s\n", inode
, ceph_cap_string(mask
));
1695 if (ceph_caps_issued_mask(ceph_inode(inode
), mask
, 1))
1698 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_GETATTR
, USE_ANY_MDS
);
1700 return PTR_ERR(req
);
1701 req
->r_inode
= igrab(inode
);
1702 req
->r_num_caps
= 1;
1703 req
->r_args
.getattr
.mask
= cpu_to_le32(mask
);
1704 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1705 ceph_mdsc_put_request(req
);
1706 dout("do_getattr result=%d\n", err
);
1712 * Check inode permissions. We verify we have a valid value for
1713 * the AUTH cap, then call the generic handler.
1715 int ceph_permission(struct inode
*inode
, int mask
)
1717 int err
= ceph_do_getattr(inode
, CEPH_CAP_AUTH_SHARED
);
1720 err
= generic_permission(inode
, mask
, NULL
);
1725 * Get all attributes. Hopefully somedata we'll have a statlite()
1726 * and can limit the fields we require to be accurate.
1728 int ceph_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1731 struct inode
*inode
= dentry
->d_inode
;
1732 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1735 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_INODE_ALL
);
1737 generic_fillattr(inode
, stat
);
1738 stat
->ino
= inode
->i_ino
;
1739 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1740 stat
->dev
= ceph_snap(inode
);
1743 if (S_ISDIR(inode
->i_mode
)) {
1744 stat
->size
= ci
->i_rbytes
;
1746 stat
->blksize
= 65536;