1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
5 #include <linux/slab.h>
6 #include <linux/string.h>
7 #include <linux/uaccess.h>
8 #include <linux/kernel.h>
9 #include <linux/namei.h>
10 #include <linux/writeback.h>
11 #include <linux/vmalloc.h>
14 #include "mds_client.h"
15 #include <linux/ceph/decode.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 static int ceph_set_ino_cb(struct inode *inode, void *data)
40 ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
41 inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
45 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
48 ino_t t = ceph_vino_to_ino(vino);
50 inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
52 return ERR_PTR(-ENOMEM);
53 if (inode->i_state & I_NEW) {
54 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
55 inode, ceph_vinop(inode), (u64)inode->i_ino);
56 unlock_new_inode(inode);
59 dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
64 struct inode *ceph_lookup_inode(struct super_block *sb, struct ceph_vino vino)
67 ino_t t = ceph_vino_to_ino(vino);
68 inode = ilookup5_nowait(sb, t, ceph_ino_compare, &vino);
73 * get/constuct snapdir inode for a given directory
75 struct inode *ceph_get_snapdir(struct inode *parent)
77 struct ceph_vino vino = {
78 .ino = ceph_ino(parent),
81 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
82 struct ceph_inode_info *ci = ceph_inode(inode);
84 BUG_ON(!S_ISDIR(parent->i_mode));
87 inode->i_mode = parent->i_mode;
88 inode->i_uid = parent->i_uid;
89 inode->i_gid = parent->i_gid;
90 inode->i_op = &ceph_dir_iops;
91 inode->i_fop = &ceph_dir_fops;
92 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
97 const struct inode_operations ceph_file_iops = {
98 .permission = ceph_permission,
99 .setattr = ceph_setattr,
100 .getattr = ceph_getattr,
101 .setxattr = ceph_setxattr,
102 .getxattr = ceph_getxattr,
103 .listxattr = ceph_listxattr,
104 .removexattr = ceph_removexattr,
109 * We use a 'frag tree' to keep track of the MDS's directory fragments
110 * for a given inode (usually there is just a single fragment). We
111 * need to know when a child frag is delegated to a new MDS, or when
112 * it is flagged as replicated, so we can direct our requests
117 * find/create a frag in the tree
119 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
123 struct rb_node *parent = NULL;
124 struct ceph_inode_frag *frag;
127 p = &ci->i_fragtree.rb_node;
130 frag = rb_entry(parent, struct ceph_inode_frag, node);
131 c = ceph_frag_compare(f, frag->frag);
140 frag = kmalloc(sizeof(*frag), GFP_NOFS);
142 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
143 "frag %x\n", &ci->vfs_inode,
144 ceph_vinop(&ci->vfs_inode), f);
145 return ERR_PTR(-ENOMEM);
152 rb_link_node(&frag->node, parent, p);
153 rb_insert_color(&frag->node, &ci->i_fragtree);
155 dout("get_or_create_frag added %llx.%llx frag %x\n",
156 ceph_vinop(&ci->vfs_inode), f);
161 * find a specific frag @f
163 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
165 struct rb_node *n = ci->i_fragtree.rb_node;
168 struct ceph_inode_frag *frag =
169 rb_entry(n, struct ceph_inode_frag, node);
170 int c = ceph_frag_compare(f, frag->frag);
182 * Choose frag containing the given value @v. If @pfrag is
183 * specified, copy the frag delegation info to the caller if
186 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
187 struct ceph_inode_frag *pfrag,
190 u32 t = ceph_frag_make(0, 0);
191 struct ceph_inode_frag *frag;
198 mutex_lock(&ci->i_fragtree_mutex);
200 WARN_ON(!ceph_frag_contains_value(t, v));
201 frag = __ceph_find_frag(ci, t);
203 break; /* t is a leaf */
204 if (frag->split_by == 0) {
206 memcpy(pfrag, frag, sizeof(*pfrag));
213 nway = 1 << frag->split_by;
214 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
215 frag->split_by, nway);
216 for (i = 0; i < nway; i++) {
217 n = ceph_frag_make_child(t, frag->split_by, i);
218 if (ceph_frag_contains_value(n, v)) {
225 dout("choose_frag(%x) = %x\n", v, t);
227 mutex_unlock(&ci->i_fragtree_mutex);
232 * Process dirfrag (delegation) info from the mds. Include leaf
233 * fragment in tree ONLY if ndist > 0. Otherwise, only
234 * branches/splits are included in i_fragtree)
236 static int ceph_fill_dirfrag(struct inode *inode,
237 struct ceph_mds_reply_dirfrag *dirinfo)
239 struct ceph_inode_info *ci = ceph_inode(inode);
240 struct ceph_inode_frag *frag;
241 u32 id = le32_to_cpu(dirinfo->frag);
242 int mds = le32_to_cpu(dirinfo->auth);
243 int ndist = le32_to_cpu(dirinfo->ndist);
247 mutex_lock(&ci->i_fragtree_mutex);
249 /* no delegation info needed. */
250 frag = __ceph_find_frag(ci, id);
253 if (frag->split_by == 0) {
254 /* tree leaf, remove */
255 dout("fill_dirfrag removed %llx.%llx frag %x"
256 " (no ref)\n", ceph_vinop(inode), id);
257 rb_erase(&frag->node, &ci->i_fragtree);
260 /* tree branch, keep and clear */
261 dout("fill_dirfrag cleared %llx.%llx frag %x"
262 " referral\n", ceph_vinop(inode), id);
270 /* find/add this frag to store mds delegation info */
271 frag = __get_or_create_frag(ci, id);
273 /* this is not the end of the world; we can continue
274 with bad/inaccurate delegation info */
275 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
276 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
282 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
283 for (i = 0; i < frag->ndist; i++)
284 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
285 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
286 ceph_vinop(inode), frag->frag, frag->ndist);
289 mutex_unlock(&ci->i_fragtree_mutex);
295 * initialize a newly allocated inode.
297 struct inode *ceph_alloc_inode(struct super_block *sb)
299 struct ceph_inode_info *ci;
302 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
306 dout("alloc_inode %p\n", &ci->vfs_inode);
308 spin_lock_init(&ci->i_ceph_lock);
311 ci->i_time_warp_seq = 0;
312 ci->i_ceph_flags = 0;
313 atomic_set(&ci->i_release_count, 1);
314 atomic_set(&ci->i_complete_count, 0);
315 ci->i_symlink = NULL;
317 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
319 ci->i_fragtree = RB_ROOT;
320 mutex_init(&ci->i_fragtree_mutex);
322 ci->i_xattrs.blob = NULL;
323 ci->i_xattrs.prealloc_blob = NULL;
324 ci->i_xattrs.dirty = false;
325 ci->i_xattrs.index = RB_ROOT;
326 ci->i_xattrs.count = 0;
327 ci->i_xattrs.names_size = 0;
328 ci->i_xattrs.vals_size = 0;
329 ci->i_xattrs.version = 0;
330 ci->i_xattrs.index_version = 0;
332 ci->i_caps = RB_ROOT;
333 ci->i_auth_cap = NULL;
334 ci->i_dirty_caps = 0;
335 ci->i_flushing_caps = 0;
336 INIT_LIST_HEAD(&ci->i_dirty_item);
337 INIT_LIST_HEAD(&ci->i_flushing_item);
338 ci->i_cap_flush_seq = 0;
339 ci->i_cap_flush_last_tid = 0;
340 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
341 init_waitqueue_head(&ci->i_cap_wq);
342 ci->i_hold_caps_min = 0;
343 ci->i_hold_caps_max = 0;
344 INIT_LIST_HEAD(&ci->i_cap_delay_list);
345 ci->i_cap_exporting_mds = 0;
346 ci->i_cap_exporting_mseq = 0;
347 ci->i_cap_exporting_issued = 0;
348 INIT_LIST_HEAD(&ci->i_cap_snaps);
349 ci->i_head_snapc = NULL;
352 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
353 ci->i_nr_by_mode[i] = 0;
355 ci->i_truncate_seq = 0;
356 ci->i_truncate_size = 0;
357 ci->i_truncate_pending = 0;
360 ci->i_reported_size = 0;
361 ci->i_wanted_max_size = 0;
362 ci->i_requested_max_size = 0;
366 ci->i_rdcache_ref = 0;
369 ci->i_wrbuffer_ref = 0;
370 ci->i_wrbuffer_ref_head = 0;
371 ci->i_shared_gen = 0;
372 ci->i_rdcache_gen = 0;
373 ci->i_rdcache_revoking = 0;
375 INIT_LIST_HEAD(&ci->i_unsafe_writes);
376 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
377 spin_lock_init(&ci->i_unsafe_lock);
379 ci->i_snap_realm = NULL;
380 INIT_LIST_HEAD(&ci->i_snap_realm_item);
381 INIT_LIST_HEAD(&ci->i_snap_flush_item);
383 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
384 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
386 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
388 return &ci->vfs_inode;
391 static void ceph_i_callback(struct rcu_head *head)
393 struct inode *inode = container_of(head, struct inode, i_rcu);
394 struct ceph_inode_info *ci = ceph_inode(inode);
396 kmem_cache_free(ceph_inode_cachep, ci);
399 void ceph_destroy_inode(struct inode *inode)
401 struct ceph_inode_info *ci = ceph_inode(inode);
402 struct ceph_inode_frag *frag;
405 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
407 ceph_queue_caps_release(inode);
410 * we may still have a snap_realm reference if there are stray
411 * caps in i_cap_exporting_issued or i_snap_caps.
413 if (ci->i_snap_realm) {
414 struct ceph_mds_client *mdsc =
415 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
416 struct ceph_snap_realm *realm = ci->i_snap_realm;
418 dout(" dropping residual ref to snap realm %p\n", realm);
419 spin_lock(&realm->inodes_with_caps_lock);
420 list_del_init(&ci->i_snap_realm_item);
421 spin_unlock(&realm->inodes_with_caps_lock);
422 ceph_put_snap_realm(mdsc, realm);
425 kfree(ci->i_symlink);
426 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
427 frag = rb_entry(n, struct ceph_inode_frag, node);
428 rb_erase(n, &ci->i_fragtree);
432 __ceph_destroy_xattrs(ci);
433 if (ci->i_xattrs.blob)
434 ceph_buffer_put(ci->i_xattrs.blob);
435 if (ci->i_xattrs.prealloc_blob)
436 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
438 call_rcu(&inode->i_rcu, ceph_i_callback);
443 * Helpers to fill in size, ctime, mtime, and atime. We have to be
444 * careful because either the client or MDS may have more up to date
445 * info, depending on which capabilities are held, and whether
446 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
447 * and size are monotonically increasing, except when utimes() or
448 * truncate() increments the corresponding _seq values.)
450 int ceph_fill_file_size(struct inode *inode, int issued,
451 u32 truncate_seq, u64 truncate_size, u64 size)
453 struct ceph_inode_info *ci = ceph_inode(inode);
456 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
457 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
458 dout("size %lld -> %llu\n", inode->i_size, size);
459 inode->i_size = size;
460 inode->i_blocks = (size + (1<<9) - 1) >> 9;
461 ci->i_reported_size = size;
462 if (truncate_seq != ci->i_truncate_seq) {
463 dout("truncate_seq %u -> %u\n",
464 ci->i_truncate_seq, truncate_seq);
465 ci->i_truncate_seq = truncate_seq;
467 * If we hold relevant caps, or in the case where we're
468 * not the only client referencing this file and we
469 * don't hold those caps, then we need to check whether
470 * the file is either opened or mmaped
472 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
473 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
475 CEPH_CAP_FILE_LAZYIO)) ||
476 mapping_mapped(inode->i_mapping) ||
477 __ceph_caps_file_wanted(ci)) {
478 ci->i_truncate_pending++;
483 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
484 ci->i_truncate_size != truncate_size) {
485 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
487 ci->i_truncate_size = truncate_size;
492 void ceph_fill_file_time(struct inode *inode, int issued,
493 u64 time_warp_seq, struct timespec *ctime,
494 struct timespec *mtime, struct timespec *atime)
496 struct ceph_inode_info *ci = ceph_inode(inode);
499 if (issued & (CEPH_CAP_FILE_EXCL|
501 CEPH_CAP_FILE_BUFFER|
503 CEPH_CAP_XATTR_EXCL)) {
504 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
505 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
506 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
507 ctime->tv_sec, ctime->tv_nsec);
508 inode->i_ctime = *ctime;
510 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
511 /* the MDS did a utimes() */
512 dout("mtime %ld.%09ld -> %ld.%09ld "
514 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
515 mtime->tv_sec, mtime->tv_nsec,
516 ci->i_time_warp_seq, (int)time_warp_seq);
518 inode->i_mtime = *mtime;
519 inode->i_atime = *atime;
520 ci->i_time_warp_seq = time_warp_seq;
521 } else if (time_warp_seq == ci->i_time_warp_seq) {
522 /* nobody did utimes(); take the max */
523 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
524 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
525 inode->i_mtime.tv_sec,
526 inode->i_mtime.tv_nsec,
527 mtime->tv_sec, mtime->tv_nsec);
528 inode->i_mtime = *mtime;
530 if (timespec_compare(atime, &inode->i_atime) > 0) {
531 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
532 inode->i_atime.tv_sec,
533 inode->i_atime.tv_nsec,
534 atime->tv_sec, atime->tv_nsec);
535 inode->i_atime = *atime;
537 } else if (issued & CEPH_CAP_FILE_EXCL) {
538 /* we did a utimes(); ignore mds values */
543 /* we have no write|excl caps; whatever the MDS says is true */
544 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
545 inode->i_ctime = *ctime;
546 inode->i_mtime = *mtime;
547 inode->i_atime = *atime;
548 ci->i_time_warp_seq = time_warp_seq;
553 if (warn) /* time_warp_seq shouldn't go backwards */
554 dout("%p mds time_warp_seq %llu < %u\n",
555 inode, time_warp_seq, ci->i_time_warp_seq);
559 * Populate an inode based on info from mds. May be called on new or
562 static int fill_inode(struct inode *inode,
563 struct ceph_mds_reply_info_in *iinfo,
564 struct ceph_mds_reply_dirfrag *dirinfo,
565 struct ceph_mds_session *session,
566 unsigned long ttl_from, int cap_fmode,
567 struct ceph_cap_reservation *caps_reservation)
569 struct ceph_mds_reply_inode *info = iinfo->in;
570 struct ceph_inode_info *ci = ceph_inode(inode);
572 int issued = 0, implemented;
573 struct timespec mtime, atime, ctime;
575 struct ceph_buffer *xattr_blob = NULL;
579 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
580 inode, ceph_vinop(inode), le64_to_cpu(info->version),
584 * prealloc xattr data, if it looks like we'll need it. only
585 * if len > 4 (meaning there are actually xattrs; the first 4
586 * bytes are the xattr count).
588 if (iinfo->xattr_len > 4) {
589 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
591 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
595 spin_lock(&ci->i_ceph_lock);
598 * provided version will be odd if inode value is projected,
599 * even if stable. skip the update if we have newer stable
600 * info (ours>=theirs, e.g. due to racing mds replies), unless
601 * we are getting projected (unstable) info (in which case the
602 * version is odd, and we want ours>theirs).
608 if (le64_to_cpu(info->version) > 0 &&
609 (ci->i_version & ~1) >= le64_to_cpu(info->version))
612 issued = __ceph_caps_issued(ci, &implemented);
613 issued |= implemented | __ceph_caps_dirty(ci);
616 ci->i_version = le64_to_cpu(info->version);
618 inode->i_rdev = le32_to_cpu(info->rdev);
620 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
621 inode->i_mode = le32_to_cpu(info->mode);
622 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
623 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
624 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
625 from_kuid(&init_user_ns, inode->i_uid),
626 from_kgid(&init_user_ns, inode->i_gid));
629 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
630 set_nlink(inode, le32_to_cpu(info->nlink));
632 /* be careful with mtime, atime, size */
633 ceph_decode_timespec(&atime, &info->atime);
634 ceph_decode_timespec(&mtime, &info->mtime);
635 ceph_decode_timespec(&ctime, &info->ctime);
636 queue_trunc = ceph_fill_file_size(inode, issued,
637 le32_to_cpu(info->truncate_seq),
638 le64_to_cpu(info->truncate_size),
639 le64_to_cpu(info->size));
640 ceph_fill_file_time(inode, issued,
641 le32_to_cpu(info->time_warp_seq),
642 &ctime, &mtime, &atime);
644 /* only update max_size on auth cap */
645 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
646 ci->i_max_size != le64_to_cpu(info->max_size)) {
647 dout("max_size %lld -> %llu\n", ci->i_max_size,
648 le64_to_cpu(info->max_size));
649 ci->i_max_size = le64_to_cpu(info->max_size);
652 ci->i_layout = info->layout;
653 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
656 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
657 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
658 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
659 if (ci->i_xattrs.blob)
660 ceph_buffer_put(ci->i_xattrs.blob);
661 ci->i_xattrs.blob = xattr_blob;
663 memcpy(ci->i_xattrs.blob->vec.iov_base,
664 iinfo->xattr_data, iinfo->xattr_len);
665 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
669 inode->i_mapping->a_ops = &ceph_aops;
670 inode->i_mapping->backing_dev_info =
671 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
673 switch (inode->i_mode & S_IFMT) {
678 init_special_inode(inode, inode->i_mode, inode->i_rdev);
679 inode->i_op = &ceph_file_iops;
682 inode->i_op = &ceph_file_iops;
683 inode->i_fop = &ceph_file_fops;
686 inode->i_op = &ceph_symlink_iops;
687 if (!ci->i_symlink) {
688 u32 symlen = iinfo->symlink_len;
691 spin_unlock(&ci->i_ceph_lock);
694 if (WARN_ON(symlen != inode->i_size))
698 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
702 spin_lock(&ci->i_ceph_lock);
706 kfree(sym); /* lost a race */
710 inode->i_op = &ceph_dir_iops;
711 inode->i_fop = &ceph_dir_fops;
713 ci->i_dir_layout = iinfo->dir_layout;
715 ci->i_files = le64_to_cpu(info->files);
716 ci->i_subdirs = le64_to_cpu(info->subdirs);
717 ci->i_rbytes = le64_to_cpu(info->rbytes);
718 ci->i_rfiles = le64_to_cpu(info->rfiles);
719 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
720 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
723 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
724 ceph_vinop(inode), inode->i_mode);
727 /* set dir completion flag? */
728 if (S_ISDIR(inode->i_mode) &&
729 ci->i_files == 0 && ci->i_subdirs == 0 &&
730 ceph_snap(inode) == CEPH_NOSNAP &&
731 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
732 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
733 !__ceph_dir_is_complete(ci)) {
734 dout(" marking %p complete (empty)\n", inode);
735 __ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count));
736 ci->i_max_offset = 2;
739 spin_unlock(&ci->i_ceph_lock);
741 /* queue truncate if we saw i_size decrease */
743 ceph_queue_vmtruncate(inode);
745 /* populate frag tree */
746 /* FIXME: move me up, if/when version reflects fragtree changes */
747 nsplits = le32_to_cpu(info->fragtree.nsplits);
748 mutex_lock(&ci->i_fragtree_mutex);
749 for (i = 0; i < nsplits; i++) {
750 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
751 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
755 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
756 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
758 mutex_unlock(&ci->i_fragtree_mutex);
760 /* were we issued a capability? */
761 if (info->cap.caps) {
762 if (ceph_snap(inode) == CEPH_NOSNAP) {
763 ceph_add_cap(inode, session,
764 le64_to_cpu(info->cap.cap_id),
766 le32_to_cpu(info->cap.caps),
767 le32_to_cpu(info->cap.wanted),
768 le32_to_cpu(info->cap.seq),
769 le32_to_cpu(info->cap.mseq),
770 le64_to_cpu(info->cap.realm),
774 spin_lock(&ci->i_ceph_lock);
775 dout(" %p got snap_caps %s\n", inode,
776 ceph_cap_string(le32_to_cpu(info->cap.caps)));
777 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
779 __ceph_get_fmode(ci, cap_fmode);
780 spin_unlock(&ci->i_ceph_lock);
782 } else if (cap_fmode >= 0) {
783 pr_warning("mds issued no caps on %llx.%llx\n",
785 __ceph_get_fmode(ci, cap_fmode);
788 /* update delegation info? */
790 ceph_fill_dirfrag(inode, dirinfo);
796 ceph_buffer_put(xattr_blob);
801 * caller should hold session s_mutex.
803 static void update_dentry_lease(struct dentry *dentry,
804 struct ceph_mds_reply_lease *lease,
805 struct ceph_mds_session *session,
806 unsigned long from_time)
808 struct ceph_dentry_info *di = ceph_dentry(dentry);
809 long unsigned duration = le32_to_cpu(lease->duration_ms);
810 long unsigned ttl = from_time + (duration * HZ) / 1000;
811 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
814 /* only track leases on regular dentries */
815 if (dentry->d_op != &ceph_dentry_ops)
818 spin_lock(&dentry->d_lock);
819 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
820 dentry, duration, ttl);
822 /* make lease_rdcache_gen match directory */
823 dir = dentry->d_parent->d_inode;
824 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
829 if (di->lease_gen == session->s_cap_gen &&
830 time_before(ttl, dentry->d_time))
831 goto out_unlock; /* we already have a newer lease. */
833 if (di->lease_session && di->lease_session != session)
836 ceph_dentry_lru_touch(dentry);
838 if (!di->lease_session)
839 di->lease_session = ceph_get_mds_session(session);
840 di->lease_gen = session->s_cap_gen;
841 di->lease_seq = le32_to_cpu(lease->seq);
842 di->lease_renew_after = half_ttl;
843 di->lease_renew_from = 0;
844 dentry->d_time = ttl;
846 spin_unlock(&dentry->d_lock);
851 * Set dentry's directory position based on the current dir's max, and
852 * order it in d_subdirs, so that dcache_readdir behaves.
854 * Always called under directory's i_mutex.
856 static void ceph_set_dentry_offset(struct dentry *dn)
858 struct dentry *dir = dn->d_parent;
859 struct inode *inode = dir->d_inode;
860 struct ceph_inode_info *ci;
861 struct ceph_dentry_info *di;
865 ci = ceph_inode(inode);
866 di = ceph_dentry(dn);
868 spin_lock(&ci->i_ceph_lock);
869 if (!__ceph_dir_is_complete(ci)) {
870 spin_unlock(&ci->i_ceph_lock);
873 di->offset = ceph_inode(inode)->i_max_offset++;
874 spin_unlock(&ci->i_ceph_lock);
876 spin_lock(&dir->d_lock);
877 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
878 list_move(&dn->d_u.d_child, &dir->d_subdirs);
879 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
880 dn->d_u.d_child.prev, dn->d_u.d_child.next);
881 spin_unlock(&dn->d_lock);
882 spin_unlock(&dir->d_lock);
886 * splice a dentry to an inode.
887 * caller must hold directory i_mutex for this to be safe.
889 * we will only rehash the resulting dentry if @prehash is
890 * true; @prehash will be set to false (for the benefit of
891 * the caller) if we fail.
893 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
894 bool *prehash, bool set_offset)
896 struct dentry *realdn;
900 /* dn must be unhashed */
903 realdn = d_materialise_unique(dn, in);
904 if (IS_ERR(realdn)) {
905 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
906 PTR_ERR(realdn), dn, in, ceph_vinop(in));
908 *prehash = false; /* don't rehash on error */
909 dn = realdn; /* note realdn contains the error */
912 dout("dn %p (%d) spliced with %p (%d) "
913 "inode %p ino %llx.%llx\n",
915 realdn, d_count(realdn),
916 realdn->d_inode, ceph_vinop(realdn->d_inode));
920 BUG_ON(!ceph_dentry(dn));
921 dout("dn %p attached to %p ino %llx.%llx\n",
922 dn, dn->d_inode, ceph_vinop(dn->d_inode));
924 if ((!prehash || *prehash) && d_unhashed(dn))
927 ceph_set_dentry_offset(dn);
933 * Incorporate results into the local cache. This is either just
934 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
937 * A reply may contain
938 * a directory inode along with a dentry.
939 * and/or a target inode
941 * Called with snap_rwsem (read).
943 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
944 struct ceph_mds_session *session)
946 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
947 struct inode *in = NULL;
948 struct ceph_mds_reply_inode *ininfo;
949 struct ceph_vino vino;
950 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
954 dout("fill_trace %p is_dentry %d is_target %d\n", req,
955 rinfo->head->is_dentry, rinfo->head->is_target);
961 * If we resend completed ops to a recovering mds, we get no
962 * trace. Since that is very rare, pretend this is the case
963 * to ensure the 'no trace' handlers in the callers behave.
965 * Fill in inodes unconditionally to avoid breaking cap
968 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
969 pr_info("fill_trace faking empty trace on %lld %s\n",
970 req->r_tid, ceph_mds_op_name(rinfo->head->op));
971 if (rinfo->head->is_dentry) {
972 rinfo->head->is_dentry = 0;
973 err = fill_inode(req->r_locked_dir,
974 &rinfo->diri, rinfo->dirfrag,
975 session, req->r_request_started, -1);
977 if (rinfo->head->is_target) {
978 rinfo->head->is_target = 0;
979 ininfo = rinfo->targeti.in;
980 vino.ino = le64_to_cpu(ininfo->ino);
981 vino.snap = le64_to_cpu(ininfo->snapid);
982 in = ceph_get_inode(sb, vino);
983 err = fill_inode(in, &rinfo->targeti, NULL,
984 session, req->r_request_started,
991 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
992 dout("fill_trace reply is empty!\n");
993 if (rinfo->head->result == 0 && req->r_locked_dir)
994 ceph_invalidate_dir_request(req);
998 if (rinfo->head->is_dentry) {
999 struct inode *dir = req->r_locked_dir;
1002 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
1003 session, req->r_request_started, -1,
1004 &req->r_caps_reservation);
1013 * ignore null lease/binding on snapdir ENOENT, or else we
1014 * will have trouble splicing in the virtual snapdir later
1016 if (rinfo->head->is_dentry && !req->r_aborted &&
1017 req->r_locked_dir &&
1018 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1019 fsc->mount_options->snapdir_name,
1020 req->r_dentry->d_name.len))) {
1022 * lookup link rename : null -> possibly existing inode
1023 * mknod symlink mkdir : null -> new inode
1024 * unlink : linked -> null
1026 struct inode *dir = req->r_locked_dir;
1027 struct dentry *dn = req->r_dentry;
1028 bool have_dir_cap, have_lease;
1032 BUG_ON(dn->d_parent->d_inode != dir);
1033 BUG_ON(ceph_ino(dir) !=
1034 le64_to_cpu(rinfo->diri.in->ino));
1035 BUG_ON(ceph_snap(dir) !=
1036 le64_to_cpu(rinfo->diri.in->snapid));
1038 /* do we have a lease on the whole dir? */
1040 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1041 CEPH_CAP_FILE_SHARED);
1043 /* do we have a dn lease? */
1044 have_lease = have_dir_cap ||
1045 le32_to_cpu(rinfo->dlease->duration_ms);
1047 dout("fill_trace no dentry lease or dir cap\n");
1050 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1051 dout(" src %p '%.*s' dst %p '%.*s'\n",
1053 req->r_old_dentry->d_name.len,
1054 req->r_old_dentry->d_name.name,
1055 dn, dn->d_name.len, dn->d_name.name);
1056 dout("fill_trace doing d_move %p -> %p\n",
1057 req->r_old_dentry, dn);
1059 d_move(req->r_old_dentry, dn);
1060 dout(" src %p '%.*s' dst %p '%.*s'\n",
1062 req->r_old_dentry->d_name.len,
1063 req->r_old_dentry->d_name.name,
1064 dn, dn->d_name.len, dn->d_name.name);
1066 /* ensure target dentry is invalidated, despite
1067 rehashing bug in vfs_rename_dir */
1068 ceph_invalidate_dentry_lease(dn);
1071 * d_move() puts the renamed dentry at the end of
1072 * d_subdirs. We need to assign it an appropriate
1073 * directory offset so we can behave when dir is
1076 ceph_set_dentry_offset(req->r_old_dentry);
1077 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1078 ceph_dentry(req->r_old_dentry)->offset);
1080 dn = req->r_old_dentry; /* use old_dentry */
1085 if (!rinfo->head->is_target) {
1086 dout("fill_trace null dentry\n");
1088 dout("d_delete %p\n", dn);
1091 dout("d_instantiate %p NULL\n", dn);
1092 d_instantiate(dn, NULL);
1093 if (have_lease && d_unhashed(dn))
1095 update_dentry_lease(dn, rinfo->dlease,
1097 req->r_request_started);
1102 /* attach proper inode */
1103 ininfo = rinfo->targeti.in;
1104 vino.ino = le64_to_cpu(ininfo->ino);
1105 vino.snap = le64_to_cpu(ininfo->snapid);
1108 in = ceph_get_inode(sb, vino);
1110 pr_err("fill_trace bad get_inode "
1111 "%llx.%llx\n", vino.ino, vino.snap);
1116 dn = splice_dentry(dn, in, &have_lease, true);
1121 req->r_dentry = dn; /* may have spliced */
1123 } else if (ceph_ino(in) == vino.ino &&
1124 ceph_snap(in) == vino.snap) {
1127 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1128 dn, in, ceph_ino(in), ceph_snap(in),
1129 vino.ino, vino.snap);
1135 update_dentry_lease(dn, rinfo->dlease, session,
1136 req->r_request_started);
1137 dout(" final dn %p\n", dn);
1139 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1140 req->r_op == CEPH_MDS_OP_MKSNAP) && !req->r_aborted) {
1141 struct dentry *dn = req->r_dentry;
1143 /* fill out a snapdir LOOKUPSNAP dentry */
1145 BUG_ON(!req->r_locked_dir);
1146 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1147 ininfo = rinfo->targeti.in;
1148 vino.ino = le64_to_cpu(ininfo->ino);
1149 vino.snap = le64_to_cpu(ininfo->snapid);
1150 in = ceph_get_inode(sb, vino);
1152 pr_err("fill_inode get_inode badness %llx.%llx\n",
1153 vino.ino, vino.snap);
1158 dout(" linking snapped dir %p to dn %p\n", in, dn);
1159 dn = splice_dentry(dn, in, NULL, true);
1164 req->r_dentry = dn; /* may have spliced */
1166 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1169 if (rinfo->head->is_target) {
1170 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1171 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1173 if (in == NULL || ceph_ino(in) != vino.ino ||
1174 ceph_snap(in) != vino.snap) {
1175 in = ceph_get_inode(sb, vino);
1181 req->r_target_inode = in;
1183 err = fill_inode(in,
1184 &rinfo->targeti, NULL,
1185 session, req->r_request_started,
1186 (le32_to_cpu(rinfo->head->result) == 0) ?
1188 &req->r_caps_reservation);
1190 pr_err("fill_inode badness %p %llx.%llx\n",
1191 in, ceph_vinop(in));
1197 dout("fill_trace done err=%d\n", err);
1202 * Prepopulate our cache with readdir results, leases, etc.
1204 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1205 struct ceph_mds_session *session)
1207 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1210 for (i = 0; i < rinfo->dir_nr; i++) {
1211 struct ceph_vino vino;
1215 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1216 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1218 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1221 dout("new_inode badness got %d\n", err);
1224 rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
1225 req->r_request_started, -1,
1226 &req->r_caps_reservation);
1228 pr_err("fill_inode badness on %p got %d\n", in, rc);
1237 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1238 struct ceph_mds_session *session)
1240 struct dentry *parent = req->r_dentry;
1241 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1246 struct inode *snapdir = NULL;
1247 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1248 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1249 struct ceph_dentry_info *di;
1252 return readdir_prepopulate_inodes_only(req, session);
1254 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1255 snapdir = ceph_get_snapdir(parent->d_inode);
1256 parent = d_find_alias(snapdir);
1257 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1258 rinfo->dir_nr, parent);
1260 dout("readdir_prepopulate %d items under dn %p\n",
1261 rinfo->dir_nr, parent);
1263 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1266 for (i = 0; i < rinfo->dir_nr; i++) {
1267 struct ceph_vino vino;
1269 dname.name = rinfo->dir_dname[i];
1270 dname.len = rinfo->dir_dname_len[i];
1271 dname.hash = full_name_hash(dname.name, dname.len);
1273 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1274 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1277 dn = d_lookup(parent, &dname);
1278 dout("d_lookup on parent=%p name=%.*s got %p\n",
1279 parent, dname.len, dname.name, dn);
1282 dn = d_alloc(parent, &dname);
1283 dout("d_alloc %p '%.*s' = %p\n", parent,
1284 dname.len, dname.name, dn);
1286 dout("d_alloc badness\n");
1290 err = ceph_init_dentry(dn);
1295 } else if (dn->d_inode &&
1296 (ceph_ino(dn->d_inode) != vino.ino ||
1297 ceph_snap(dn->d_inode) != vino.snap)) {
1298 dout(" dn %p points to wrong inode %p\n",
1304 /* reorder parent's d_subdirs */
1305 spin_lock(&parent->d_lock);
1306 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
1307 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1308 spin_unlock(&dn->d_lock);
1309 spin_unlock(&parent->d_lock);
1313 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1319 in = ceph_get_inode(parent->d_sb, vino);
1321 dout("new_inode badness\n");
1327 dn = splice_dentry(dn, in, NULL, false);
1332 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1333 req->r_request_started, -1,
1334 &req->r_caps_reservation) < 0) {
1335 pr_err("fill_inode badness on %p\n", in);
1339 update_dentry_lease(dn, rinfo->dir_dlease[i],
1341 req->r_request_started);
1346 req->r_did_prepopulate = true;
1353 dout("readdir_prepopulate done\n");
1357 int ceph_inode_set_size(struct inode *inode, loff_t size)
1359 struct ceph_inode_info *ci = ceph_inode(inode);
1362 spin_lock(&ci->i_ceph_lock);
1363 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1364 inode->i_size = size;
1365 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1367 /* tell the MDS if we are approaching max_size */
1368 if ((size << 1) >= ci->i_max_size &&
1369 (ci->i_reported_size << 1) < ci->i_max_size)
1372 spin_unlock(&ci->i_ceph_lock);
1377 * Write back inode data in a worker thread. (This can't be done
1378 * in the message handler context.)
1380 void ceph_queue_writeback(struct inode *inode)
1383 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1384 &ceph_inode(inode)->i_wb_work)) {
1385 dout("ceph_queue_writeback %p\n", inode);
1387 dout("ceph_queue_writeback %p failed\n", inode);
1392 static void ceph_writeback_work(struct work_struct *work)
1394 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1396 struct inode *inode = &ci->vfs_inode;
1398 dout("writeback %p\n", inode);
1399 filemap_fdatawrite(&inode->i_data);
1404 * queue an async invalidation
1406 void ceph_queue_invalidate(struct inode *inode)
1409 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1410 &ceph_inode(inode)->i_pg_inv_work)) {
1411 dout("ceph_queue_invalidate %p\n", inode);
1413 dout("ceph_queue_invalidate %p failed\n", inode);
1419 * Invalidate inode pages in a worker thread. (This can't be done
1420 * in the message handler context.)
1422 static void ceph_invalidate_work(struct work_struct *work)
1424 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1426 struct inode *inode = &ci->vfs_inode;
1430 spin_lock(&ci->i_ceph_lock);
1431 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1432 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1433 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1435 spin_unlock(&ci->i_ceph_lock);
1438 orig_gen = ci->i_rdcache_gen;
1439 spin_unlock(&ci->i_ceph_lock);
1441 truncate_inode_pages(&inode->i_data, 0);
1443 spin_lock(&ci->i_ceph_lock);
1444 if (orig_gen == ci->i_rdcache_gen &&
1445 orig_gen == ci->i_rdcache_revoking) {
1446 dout("invalidate_pages %p gen %d successful\n", inode,
1448 ci->i_rdcache_revoking--;
1451 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1452 inode, orig_gen, ci->i_rdcache_gen,
1453 ci->i_rdcache_revoking);
1455 spin_unlock(&ci->i_ceph_lock);
1458 ceph_check_caps(ci, 0, NULL);
1465 * called by trunc_wq;
1467 * We also truncate in a separate thread as well.
1469 static void ceph_vmtruncate_work(struct work_struct *work)
1471 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1473 struct inode *inode = &ci->vfs_inode;
1475 dout("vmtruncate_work %p\n", inode);
1476 if (!mutex_trylock(&inode->i_mutex)) {
1478 * the i_mutex can be hold by a writer who is waiting for
1479 * caps. wake up waiters, they will do pending vmtruncate.
1481 wake_up_all(&ci->i_cap_wq);
1482 mutex_lock(&inode->i_mutex);
1484 __ceph_do_pending_vmtruncate(inode);
1485 mutex_unlock(&inode->i_mutex);
1490 * Queue an async vmtruncate. If we fail to queue work, we will handle
1491 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1493 void ceph_queue_vmtruncate(struct inode *inode)
1495 struct ceph_inode_info *ci = ceph_inode(inode);
1498 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
1499 &ci->i_vmtruncate_work)) {
1500 dout("ceph_queue_vmtruncate %p\n", inode);
1502 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1503 inode, ci->i_truncate_pending);
1509 * Make sure any pending truncation is applied before doing anything
1510 * that may depend on it.
1512 void __ceph_do_pending_vmtruncate(struct inode *inode)
1514 struct ceph_inode_info *ci = ceph_inode(inode);
1516 int wrbuffer_refs, finish = 0;
1519 spin_lock(&ci->i_ceph_lock);
1520 if (ci->i_truncate_pending == 0) {
1521 dout("__do_pending_vmtruncate %p none pending\n", inode);
1522 spin_unlock(&ci->i_ceph_lock);
1527 * make sure any dirty snapped pages are flushed before we
1528 * possibly truncate them.. so write AND block!
1530 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1531 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1533 spin_unlock(&ci->i_ceph_lock);
1534 filemap_write_and_wait_range(&inode->i_data, 0,
1535 inode->i_sb->s_maxbytes);
1539 to = ci->i_truncate_size;
1540 wrbuffer_refs = ci->i_wrbuffer_ref;
1541 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1542 ci->i_truncate_pending, to);
1543 spin_unlock(&ci->i_ceph_lock);
1545 truncate_inode_pages(inode->i_mapping, to);
1547 spin_lock(&ci->i_ceph_lock);
1548 if (to == ci->i_truncate_size) {
1549 ci->i_truncate_pending = 0;
1552 spin_unlock(&ci->i_ceph_lock);
1556 if (wrbuffer_refs == 0)
1557 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1559 wake_up_all(&ci->i_cap_wq);
1566 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1568 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1569 nd_set_link(nd, ci->i_symlink);
1573 static const struct inode_operations ceph_symlink_iops = {
1574 .readlink = generic_readlink,
1575 .follow_link = ceph_sym_follow_link,
1576 .setattr = ceph_setattr,
1577 .getattr = ceph_getattr,
1578 .setxattr = ceph_setxattr,
1579 .getxattr = ceph_getxattr,
1580 .listxattr = ceph_listxattr,
1581 .removexattr = ceph_removexattr,
1587 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1589 struct inode *inode = dentry->d_inode;
1590 struct ceph_inode_info *ci = ceph_inode(inode);
1591 struct inode *parent_inode;
1592 const unsigned int ia_valid = attr->ia_valid;
1593 struct ceph_mds_request *req;
1594 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
1596 int release = 0, dirtied = 0;
1599 int inode_dirty_flags = 0;
1601 if (ceph_snap(inode) != CEPH_NOSNAP)
1604 __ceph_do_pending_vmtruncate(inode);
1606 err = inode_change_ok(inode, attr);
1610 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1613 return PTR_ERR(req);
1615 spin_lock(&ci->i_ceph_lock);
1616 issued = __ceph_caps_issued(ci, NULL);
1617 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1619 if (ia_valid & ATTR_UID) {
1620 dout("setattr %p uid %d -> %d\n", inode,
1621 from_kuid(&init_user_ns, inode->i_uid),
1622 from_kuid(&init_user_ns, attr->ia_uid));
1623 if (issued & CEPH_CAP_AUTH_EXCL) {
1624 inode->i_uid = attr->ia_uid;
1625 dirtied |= CEPH_CAP_AUTH_EXCL;
1626 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1627 !uid_eq(attr->ia_uid, inode->i_uid)) {
1628 req->r_args.setattr.uid = cpu_to_le32(
1629 from_kuid(&init_user_ns, attr->ia_uid));
1630 mask |= CEPH_SETATTR_UID;
1631 release |= CEPH_CAP_AUTH_SHARED;
1634 if (ia_valid & ATTR_GID) {
1635 dout("setattr %p gid %d -> %d\n", inode,
1636 from_kgid(&init_user_ns, inode->i_gid),
1637 from_kgid(&init_user_ns, attr->ia_gid));
1638 if (issued & CEPH_CAP_AUTH_EXCL) {
1639 inode->i_gid = attr->ia_gid;
1640 dirtied |= CEPH_CAP_AUTH_EXCL;
1641 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1642 !gid_eq(attr->ia_gid, inode->i_gid)) {
1643 req->r_args.setattr.gid = cpu_to_le32(
1644 from_kgid(&init_user_ns, attr->ia_gid));
1645 mask |= CEPH_SETATTR_GID;
1646 release |= CEPH_CAP_AUTH_SHARED;
1649 if (ia_valid & ATTR_MODE) {
1650 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1652 if (issued & CEPH_CAP_AUTH_EXCL) {
1653 inode->i_mode = attr->ia_mode;
1654 dirtied |= CEPH_CAP_AUTH_EXCL;
1655 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1656 attr->ia_mode != inode->i_mode) {
1657 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1658 mask |= CEPH_SETATTR_MODE;
1659 release |= CEPH_CAP_AUTH_SHARED;
1663 if (ia_valid & ATTR_ATIME) {
1664 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1665 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1666 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1667 if (issued & CEPH_CAP_FILE_EXCL) {
1668 ci->i_time_warp_seq++;
1669 inode->i_atime = attr->ia_atime;
1670 dirtied |= CEPH_CAP_FILE_EXCL;
1671 } else if ((issued & CEPH_CAP_FILE_WR) &&
1672 timespec_compare(&inode->i_atime,
1673 &attr->ia_atime) < 0) {
1674 inode->i_atime = attr->ia_atime;
1675 dirtied |= CEPH_CAP_FILE_WR;
1676 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1677 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1678 ceph_encode_timespec(&req->r_args.setattr.atime,
1680 mask |= CEPH_SETATTR_ATIME;
1681 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1685 if (ia_valid & ATTR_MTIME) {
1686 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1687 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1688 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1689 if (issued & CEPH_CAP_FILE_EXCL) {
1690 ci->i_time_warp_seq++;
1691 inode->i_mtime = attr->ia_mtime;
1692 dirtied |= CEPH_CAP_FILE_EXCL;
1693 } else if ((issued & CEPH_CAP_FILE_WR) &&
1694 timespec_compare(&inode->i_mtime,
1695 &attr->ia_mtime) < 0) {
1696 inode->i_mtime = attr->ia_mtime;
1697 dirtied |= CEPH_CAP_FILE_WR;
1698 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1699 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1700 ceph_encode_timespec(&req->r_args.setattr.mtime,
1702 mask |= CEPH_SETATTR_MTIME;
1703 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1707 if (ia_valid & ATTR_SIZE) {
1708 dout("setattr %p size %lld -> %lld\n", inode,
1709 inode->i_size, attr->ia_size);
1710 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1714 if ((issued & CEPH_CAP_FILE_EXCL) &&
1715 attr->ia_size > inode->i_size) {
1716 inode->i_size = attr->ia_size;
1718 (attr->ia_size + (1 << 9) - 1) >> 9;
1719 inode->i_ctime = attr->ia_ctime;
1720 ci->i_reported_size = attr->ia_size;
1721 dirtied |= CEPH_CAP_FILE_EXCL;
1722 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1723 attr->ia_size != inode->i_size) {
1724 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1725 req->r_args.setattr.old_size =
1726 cpu_to_le64(inode->i_size);
1727 mask |= CEPH_SETATTR_SIZE;
1728 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1733 /* these do nothing */
1734 if (ia_valid & ATTR_CTIME) {
1735 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1736 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1737 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1738 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1739 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1740 only ? "ctime only" : "ignored");
1741 inode->i_ctime = attr->ia_ctime;
1744 * if kernel wants to dirty ctime but nothing else,
1745 * we need to choose a cap to dirty under, or do
1746 * a almost-no-op setattr
1748 if (issued & CEPH_CAP_AUTH_EXCL)
1749 dirtied |= CEPH_CAP_AUTH_EXCL;
1750 else if (issued & CEPH_CAP_FILE_EXCL)
1751 dirtied |= CEPH_CAP_FILE_EXCL;
1752 else if (issued & CEPH_CAP_XATTR_EXCL)
1753 dirtied |= CEPH_CAP_XATTR_EXCL;
1755 mask |= CEPH_SETATTR_CTIME;
1758 if (ia_valid & ATTR_FILE)
1759 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1762 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
1763 inode->i_ctime = CURRENT_TIME;
1767 spin_unlock(&ci->i_ceph_lock);
1769 if (inode_dirty_flags)
1770 __mark_inode_dirty(inode, inode_dirty_flags);
1773 req->r_inode = inode;
1775 req->r_inode_drop = release;
1776 req->r_args.setattr.mask = cpu_to_le32(mask);
1777 req->r_num_caps = 1;
1778 parent_inode = ceph_get_dentry_parent_inode(dentry);
1779 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1782 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1783 ceph_cap_string(dirtied), mask);
1785 ceph_mdsc_put_request(req);
1786 __ceph_do_pending_vmtruncate(inode);
1789 spin_unlock(&ci->i_ceph_lock);
1790 ceph_mdsc_put_request(req);
1795 * Verify that we have a lease on the given mask. If not,
1796 * do a getattr against an mds.
1798 int ceph_do_getattr(struct inode *inode, int mask)
1800 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1801 struct ceph_mds_client *mdsc = fsc->mdsc;
1802 struct ceph_mds_request *req;
1805 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1806 dout("do_getattr inode %p SNAPDIR\n", inode);
1810 dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
1811 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1814 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1816 return PTR_ERR(req);
1817 req->r_inode = inode;
1819 req->r_num_caps = 1;
1820 req->r_args.getattr.mask = cpu_to_le32(mask);
1821 err = ceph_mdsc_do_request(mdsc, NULL, req);
1822 ceph_mdsc_put_request(req);
1823 dout("do_getattr result=%d\n", err);
1829 * Check inode permissions. We verify we have a valid value for
1830 * the AUTH cap, then call the generic handler.
1832 int ceph_permission(struct inode *inode, int mask)
1836 if (mask & MAY_NOT_BLOCK)
1839 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1842 err = generic_permission(inode, mask);
1847 * Get all attributes. Hopefully somedata we'll have a statlite()
1848 * and can limit the fields we require to be accurate.
1850 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1853 struct inode *inode = dentry->d_inode;
1854 struct ceph_inode_info *ci = ceph_inode(inode);
1857 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1859 generic_fillattr(inode, stat);
1860 stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
1861 if (ceph_snap(inode) != CEPH_NOSNAP)
1862 stat->dev = ceph_snap(inode);
1865 if (S_ISDIR(inode->i_mode)) {
1866 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
1868 stat->size = ci->i_rbytes;
1870 stat->size = ci->i_files + ci->i_subdirs;
1872 stat->blksize = 65536;