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>
12 #include <linux/pagevec.h>
15 #include "mds_client.h"
16 #include <linux/ceph/decode.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));
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 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
302 ci->i_fragtree = RB_ROOT;
303 mutex_init(&ci->i_fragtree_mutex);
305 ci->i_xattrs.blob = NULL;
306 ci->i_xattrs.prealloc_blob = NULL;
307 ci->i_xattrs.dirty = false;
308 ci->i_xattrs.index = RB_ROOT;
309 ci->i_xattrs.count = 0;
310 ci->i_xattrs.names_size = 0;
311 ci->i_xattrs.vals_size = 0;
312 ci->i_xattrs.version = 0;
313 ci->i_xattrs.index_version = 0;
315 ci->i_caps = RB_ROOT;
316 ci->i_auth_cap = NULL;
317 ci->i_dirty_caps = 0;
318 ci->i_flushing_caps = 0;
319 INIT_LIST_HEAD(&ci->i_dirty_item);
320 INIT_LIST_HEAD(&ci->i_flushing_item);
321 ci->i_cap_flush_seq = 0;
322 ci->i_cap_flush_last_tid = 0;
323 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
324 init_waitqueue_head(&ci->i_cap_wq);
325 ci->i_hold_caps_min = 0;
326 ci->i_hold_caps_max = 0;
327 INIT_LIST_HEAD(&ci->i_cap_delay_list);
328 ci->i_cap_exporting_mds = 0;
329 ci->i_cap_exporting_mseq = 0;
330 ci->i_cap_exporting_issued = 0;
331 INIT_LIST_HEAD(&ci->i_cap_snaps);
332 ci->i_head_snapc = NULL;
335 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
336 ci->i_nr_by_mode[i] = 0;
338 ci->i_truncate_seq = 0;
339 ci->i_truncate_size = 0;
340 ci->i_truncate_pending = 0;
343 ci->i_reported_size = 0;
344 ci->i_wanted_max_size = 0;
345 ci->i_requested_max_size = 0;
349 ci->i_rdcache_ref = 0;
351 ci->i_wrbuffer_ref = 0;
352 ci->i_wrbuffer_ref_head = 0;
353 ci->i_shared_gen = 0;
354 ci->i_rdcache_gen = 0;
355 ci->i_rdcache_revoking = 0;
357 INIT_LIST_HEAD(&ci->i_unsafe_writes);
358 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
359 spin_lock_init(&ci->i_unsafe_lock);
361 ci->i_snap_realm = NULL;
362 INIT_LIST_HEAD(&ci->i_snap_realm_item);
363 INIT_LIST_HEAD(&ci->i_snap_flush_item);
365 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
366 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
368 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
370 return &ci->vfs_inode;
373 void ceph_destroy_inode(struct inode *inode)
375 struct ceph_inode_info *ci = ceph_inode(inode);
376 struct ceph_inode_frag *frag;
379 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
381 ceph_queue_caps_release(inode);
384 * we may still have a snap_realm reference if there are stray
385 * caps in i_cap_exporting_issued or i_snap_caps.
387 if (ci->i_snap_realm) {
388 struct ceph_mds_client *mdsc =
389 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
390 struct ceph_snap_realm *realm = ci->i_snap_realm;
392 dout(" dropping residual ref to snap realm %p\n", realm);
393 spin_lock(&realm->inodes_with_caps_lock);
394 list_del_init(&ci->i_snap_realm_item);
395 spin_unlock(&realm->inodes_with_caps_lock);
396 ceph_put_snap_realm(mdsc, realm);
399 kfree(ci->i_symlink);
400 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
401 frag = rb_entry(n, struct ceph_inode_frag, node);
402 rb_erase(n, &ci->i_fragtree);
406 __ceph_destroy_xattrs(ci);
407 if (ci->i_xattrs.blob)
408 ceph_buffer_put(ci->i_xattrs.blob);
409 if (ci->i_xattrs.prealloc_blob)
410 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
412 kmem_cache_free(ceph_inode_cachep, ci);
417 * Helpers to fill in size, ctime, mtime, and atime. We have to be
418 * careful because either the client or MDS may have more up to date
419 * info, depending on which capabilities are held, and whether
420 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
421 * and size are monotonically increasing, except when utimes() or
422 * truncate() increments the corresponding _seq values.)
424 int ceph_fill_file_size(struct inode *inode, int issued,
425 u32 truncate_seq, u64 truncate_size, u64 size)
427 struct ceph_inode_info *ci = ceph_inode(inode);
430 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
431 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
432 dout("size %lld -> %llu\n", inode->i_size, size);
433 inode->i_size = size;
434 inode->i_blocks = (size + (1<<9) - 1) >> 9;
435 ci->i_reported_size = size;
436 if (truncate_seq != ci->i_truncate_seq) {
437 dout("truncate_seq %u -> %u\n",
438 ci->i_truncate_seq, truncate_seq);
439 ci->i_truncate_seq = truncate_seq;
441 * If we hold relevant caps, or in the case where we're
442 * not the only client referencing this file and we
443 * don't hold those caps, then we need to check whether
444 * the file is either opened or mmaped
446 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
447 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
449 CEPH_CAP_FILE_LAZYIO)) ||
450 mapping_mapped(inode->i_mapping) ||
451 __ceph_caps_file_wanted(ci)) {
452 ci->i_truncate_pending++;
457 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
458 ci->i_truncate_size != truncate_size) {
459 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
461 ci->i_truncate_size = truncate_size;
466 void ceph_fill_file_time(struct inode *inode, int issued,
467 u64 time_warp_seq, struct timespec *ctime,
468 struct timespec *mtime, struct timespec *atime)
470 struct ceph_inode_info *ci = ceph_inode(inode);
473 if (issued & (CEPH_CAP_FILE_EXCL|
475 CEPH_CAP_FILE_BUFFER|
477 CEPH_CAP_XATTR_EXCL)) {
478 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
479 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
480 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
481 ctime->tv_sec, ctime->tv_nsec);
482 inode->i_ctime = *ctime;
484 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
485 /* the MDS did a utimes() */
486 dout("mtime %ld.%09ld -> %ld.%09ld "
488 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
489 mtime->tv_sec, mtime->tv_nsec,
490 ci->i_time_warp_seq, (int)time_warp_seq);
492 inode->i_mtime = *mtime;
493 inode->i_atime = *atime;
494 ci->i_time_warp_seq = time_warp_seq;
495 } else if (time_warp_seq == ci->i_time_warp_seq) {
496 /* nobody did utimes(); take the max */
497 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
498 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
499 inode->i_mtime.tv_sec,
500 inode->i_mtime.tv_nsec,
501 mtime->tv_sec, mtime->tv_nsec);
502 inode->i_mtime = *mtime;
504 if (timespec_compare(atime, &inode->i_atime) > 0) {
505 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
506 inode->i_atime.tv_sec,
507 inode->i_atime.tv_nsec,
508 atime->tv_sec, atime->tv_nsec);
509 inode->i_atime = *atime;
511 } else if (issued & CEPH_CAP_FILE_EXCL) {
512 /* we did a utimes(); ignore mds values */
517 /* we have no write|excl caps; whatever the MDS says is true */
518 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
519 inode->i_ctime = *ctime;
520 inode->i_mtime = *mtime;
521 inode->i_atime = *atime;
522 ci->i_time_warp_seq = time_warp_seq;
527 if (warn) /* time_warp_seq shouldn't go backwards */
528 dout("%p mds time_warp_seq %llu < %u\n",
529 inode, time_warp_seq, ci->i_time_warp_seq);
533 * Populate an inode based on info from mds. May be called on new or
536 static int fill_inode(struct inode *inode,
537 struct ceph_mds_reply_info_in *iinfo,
538 struct ceph_mds_reply_dirfrag *dirinfo,
539 struct ceph_mds_session *session,
540 unsigned long ttl_from, int cap_fmode,
541 struct ceph_cap_reservation *caps_reservation)
543 struct ceph_mds_reply_inode *info = iinfo->in;
544 struct ceph_inode_info *ci = ceph_inode(inode);
546 int issued, implemented;
547 struct timespec mtime, atime, ctime;
549 struct ceph_buffer *xattr_blob = NULL;
553 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
554 inode, ceph_vinop(inode), le64_to_cpu(info->version),
558 * prealloc xattr data, if it looks like we'll need it. only
559 * if len > 4 (meaning there are actually xattrs; the first 4
560 * bytes are the xattr count).
562 if (iinfo->xattr_len > 4) {
563 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
565 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
569 spin_lock(&inode->i_lock);
572 * provided version will be odd if inode value is projected,
573 * even if stable. skip the update if we have newer stable
574 * info (ours>=theirs, e.g. due to racing mds replies), unless
575 * we are getting projected (unstable) info (in which case the
576 * version is odd, and we want ours>theirs).
582 if (le64_to_cpu(info->version) > 0 &&
583 (ci->i_version & ~1) >= le64_to_cpu(info->version))
586 issued = __ceph_caps_issued(ci, &implemented);
587 issued |= implemented | __ceph_caps_dirty(ci);
590 ci->i_version = le64_to_cpu(info->version);
592 inode->i_rdev = le32_to_cpu(info->rdev);
594 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
595 inode->i_mode = le32_to_cpu(info->mode);
596 inode->i_uid = le32_to_cpu(info->uid);
597 inode->i_gid = le32_to_cpu(info->gid);
598 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
599 inode->i_uid, inode->i_gid);
602 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
603 inode->i_nlink = le32_to_cpu(info->nlink);
605 /* be careful with mtime, atime, size */
606 ceph_decode_timespec(&atime, &info->atime);
607 ceph_decode_timespec(&mtime, &info->mtime);
608 ceph_decode_timespec(&ctime, &info->ctime);
609 queue_trunc = ceph_fill_file_size(inode, issued,
610 le32_to_cpu(info->truncate_seq),
611 le64_to_cpu(info->truncate_size),
612 le64_to_cpu(info->size));
613 ceph_fill_file_time(inode, issued,
614 le32_to_cpu(info->time_warp_seq),
615 &ctime, &mtime, &atime);
617 /* only update max_size on auth cap */
618 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
619 ci->i_max_size != le64_to_cpu(info->max_size)) {
620 dout("max_size %lld -> %llu\n", ci->i_max_size,
621 le64_to_cpu(info->max_size));
622 ci->i_max_size = le64_to_cpu(info->max_size);
625 ci->i_layout = info->layout;
626 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
629 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
630 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
631 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
632 if (ci->i_xattrs.blob)
633 ceph_buffer_put(ci->i_xattrs.blob);
634 ci->i_xattrs.blob = xattr_blob;
636 memcpy(ci->i_xattrs.blob->vec.iov_base,
637 iinfo->xattr_data, iinfo->xattr_len);
638 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
642 inode->i_mapping->a_ops = &ceph_aops;
643 inode->i_mapping->backing_dev_info =
644 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
646 switch (inode->i_mode & S_IFMT) {
651 init_special_inode(inode, inode->i_mode, inode->i_rdev);
652 inode->i_op = &ceph_file_iops;
655 inode->i_op = &ceph_file_iops;
656 inode->i_fop = &ceph_file_fops;
659 inode->i_op = &ceph_symlink_iops;
660 if (!ci->i_symlink) {
661 int symlen = iinfo->symlink_len;
664 BUG_ON(symlen != inode->i_size);
665 spin_unlock(&inode->i_lock);
668 sym = kmalloc(symlen+1, GFP_NOFS);
671 memcpy(sym, iinfo->symlink, symlen);
674 spin_lock(&inode->i_lock);
678 kfree(sym); /* lost a race */
682 inode->i_op = &ceph_dir_iops;
683 inode->i_fop = &ceph_dir_fops;
685 ci->i_dir_layout = iinfo->dir_layout;
687 ci->i_files = le64_to_cpu(info->files);
688 ci->i_subdirs = le64_to_cpu(info->subdirs);
689 ci->i_rbytes = le64_to_cpu(info->rbytes);
690 ci->i_rfiles = le64_to_cpu(info->rfiles);
691 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
692 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
694 /* set dir completion flag? */
695 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
696 ceph_snap(inode) == CEPH_NOSNAP &&
697 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
698 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
699 (ci->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
700 dout(" marking %p complete (empty)\n", inode);
701 ci->i_ceph_flags |= CEPH_I_COMPLETE;
702 ci->i_max_offset = 2;
705 /* it may be better to set st_size in getattr instead? */
706 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), RBYTES))
707 inode->i_size = ci->i_rbytes;
710 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
711 ceph_vinop(inode), inode->i_mode);
715 spin_unlock(&inode->i_lock);
717 /* queue truncate if we saw i_size decrease */
719 ceph_queue_vmtruncate(inode);
721 /* populate frag tree */
722 /* FIXME: move me up, if/when version reflects fragtree changes */
723 nsplits = le32_to_cpu(info->fragtree.nsplits);
724 mutex_lock(&ci->i_fragtree_mutex);
725 for (i = 0; i < nsplits; i++) {
726 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
727 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
731 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
732 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
734 mutex_unlock(&ci->i_fragtree_mutex);
736 /* were we issued a capability? */
737 if (info->cap.caps) {
738 if (ceph_snap(inode) == CEPH_NOSNAP) {
739 ceph_add_cap(inode, session,
740 le64_to_cpu(info->cap.cap_id),
742 le32_to_cpu(info->cap.caps),
743 le32_to_cpu(info->cap.wanted),
744 le32_to_cpu(info->cap.seq),
745 le32_to_cpu(info->cap.mseq),
746 le64_to_cpu(info->cap.realm),
750 spin_lock(&inode->i_lock);
751 dout(" %p got snap_caps %s\n", inode,
752 ceph_cap_string(le32_to_cpu(info->cap.caps)));
753 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
755 __ceph_get_fmode(ci, cap_fmode);
756 spin_unlock(&inode->i_lock);
758 } else if (cap_fmode >= 0) {
759 pr_warning("mds issued no caps on %llx.%llx\n",
761 __ceph_get_fmode(ci, cap_fmode);
764 /* update delegation info? */
766 ceph_fill_dirfrag(inode, dirinfo);
772 ceph_buffer_put(xattr_blob);
777 * caller should hold session s_mutex.
779 static void update_dentry_lease(struct dentry *dentry,
780 struct ceph_mds_reply_lease *lease,
781 struct ceph_mds_session *session,
782 unsigned long from_time)
784 struct ceph_dentry_info *di = ceph_dentry(dentry);
785 long unsigned duration = le32_to_cpu(lease->duration_ms);
786 long unsigned ttl = from_time + (duration * HZ) / 1000;
787 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
790 /* only track leases on regular dentries */
791 if (dentry->d_op != &ceph_dentry_ops)
794 spin_lock(&dentry->d_lock);
795 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
796 dentry, le16_to_cpu(lease->mask), duration, ttl);
798 /* make lease_rdcache_gen match directory */
799 dir = dentry->d_parent->d_inode;
800 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
802 if (lease->mask == 0)
805 if (di->lease_gen == session->s_cap_gen &&
806 time_before(ttl, dentry->d_time))
807 goto out_unlock; /* we already have a newer lease. */
809 if (di->lease_session && di->lease_session != session)
812 ceph_dentry_lru_touch(dentry);
814 if (!di->lease_session)
815 di->lease_session = ceph_get_mds_session(session);
816 di->lease_gen = session->s_cap_gen;
817 di->lease_seq = le32_to_cpu(lease->seq);
818 di->lease_renew_after = half_ttl;
819 di->lease_renew_from = 0;
820 dentry->d_time = ttl;
822 spin_unlock(&dentry->d_lock);
827 * Set dentry's directory position based on the current dir's max, and
828 * order it in d_subdirs, so that dcache_readdir behaves.
830 static void ceph_set_dentry_offset(struct dentry *dn)
832 struct dentry *dir = dn->d_parent;
833 struct inode *inode = dn->d_parent->d_inode;
834 struct ceph_dentry_info *di;
838 di = ceph_dentry(dn);
840 spin_lock(&inode->i_lock);
841 if ((ceph_inode(inode)->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
842 spin_unlock(&inode->i_lock);
845 di->offset = ceph_inode(inode)->i_max_offset++;
846 spin_unlock(&inode->i_lock);
848 spin_lock(&dcache_lock);
849 spin_lock(&dn->d_lock);
850 list_move(&dn->d_u.d_child, &dir->d_subdirs);
851 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
852 dn->d_u.d_child.prev, dn->d_u.d_child.next);
853 spin_unlock(&dn->d_lock);
854 spin_unlock(&dcache_lock);
858 * splice a dentry to an inode.
859 * caller must hold directory i_mutex for this to be safe.
861 * we will only rehash the resulting dentry if @prehash is
862 * true; @prehash will be set to false (for the benefit of
863 * the caller) if we fail.
865 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
866 bool *prehash, bool set_offset)
868 struct dentry *realdn;
872 /* dn must be unhashed */
875 realdn = d_materialise_unique(dn, in);
876 if (IS_ERR(realdn)) {
877 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
878 PTR_ERR(realdn), dn, in, ceph_vinop(in));
880 *prehash = false; /* don't rehash on error */
881 dn = realdn; /* note realdn contains the error */
884 dout("dn %p (%d) spliced with %p (%d) "
885 "inode %p ino %llx.%llx\n",
886 dn, atomic_read(&dn->d_count),
887 realdn, atomic_read(&realdn->d_count),
888 realdn->d_inode, ceph_vinop(realdn->d_inode));
892 BUG_ON(!ceph_dentry(dn));
893 dout("dn %p attached to %p ino %llx.%llx\n",
894 dn, dn->d_inode, ceph_vinop(dn->d_inode));
896 if ((!prehash || *prehash) && d_unhashed(dn))
899 ceph_set_dentry_offset(dn);
905 * Incorporate results into the local cache. This is either just
906 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
909 * A reply may contain
910 * a directory inode along with a dentry.
911 * and/or a target inode
913 * Called with snap_rwsem (read).
915 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
916 struct ceph_mds_session *session)
918 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
919 struct inode *in = NULL;
920 struct ceph_mds_reply_inode *ininfo;
921 struct ceph_vino vino;
922 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
926 dout("fill_trace %p is_dentry %d is_target %d\n", req,
927 rinfo->head->is_dentry, rinfo->head->is_target);
933 * If we resend completed ops to a recovering mds, we get no
934 * trace. Since that is very rare, pretend this is the case
935 * to ensure the 'no trace' handlers in the callers behave.
937 * Fill in inodes unconditionally to avoid breaking cap
940 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
941 pr_info("fill_trace faking empty trace on %lld %s\n",
942 req->r_tid, ceph_mds_op_name(rinfo->head->op));
943 if (rinfo->head->is_dentry) {
944 rinfo->head->is_dentry = 0;
945 err = fill_inode(req->r_locked_dir,
946 &rinfo->diri, rinfo->dirfrag,
947 session, req->r_request_started, -1);
949 if (rinfo->head->is_target) {
950 rinfo->head->is_target = 0;
951 ininfo = rinfo->targeti.in;
952 vino.ino = le64_to_cpu(ininfo->ino);
953 vino.snap = le64_to_cpu(ininfo->snapid);
954 in = ceph_get_inode(sb, vino);
955 err = fill_inode(in, &rinfo->targeti, NULL,
956 session, req->r_request_started,
963 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
964 dout("fill_trace reply is empty!\n");
965 if (rinfo->head->result == 0 && req->r_locked_dir)
966 ceph_invalidate_dir_request(req);
970 if (rinfo->head->is_dentry) {
971 struct inode *dir = req->r_locked_dir;
973 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
974 session, req->r_request_started, -1,
975 &req->r_caps_reservation);
981 * ignore null lease/binding on snapdir ENOENT, or else we
982 * will have trouble splicing in the virtual snapdir later
984 if (rinfo->head->is_dentry && !req->r_aborted &&
985 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
986 fsc->mount_options->snapdir_name,
987 req->r_dentry->d_name.len))) {
989 * lookup link rename : null -> possibly existing inode
990 * mknod symlink mkdir : null -> new inode
991 * unlink : linked -> null
993 struct inode *dir = req->r_locked_dir;
994 struct dentry *dn = req->r_dentry;
995 bool have_dir_cap, have_lease;
999 BUG_ON(dn->d_parent->d_inode != dir);
1000 BUG_ON(ceph_ino(dir) !=
1001 le64_to_cpu(rinfo->diri.in->ino));
1002 BUG_ON(ceph_snap(dir) !=
1003 le64_to_cpu(rinfo->diri.in->snapid));
1005 /* do we have a lease on the whole dir? */
1007 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1008 CEPH_CAP_FILE_SHARED);
1010 /* do we have a dn lease? */
1011 have_lease = have_dir_cap ||
1012 (le16_to_cpu(rinfo->dlease->mask) &
1016 dout("fill_trace no dentry lease or dir cap\n");
1019 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1020 dout(" src %p '%.*s' dst %p '%.*s'\n",
1022 req->r_old_dentry->d_name.len,
1023 req->r_old_dentry->d_name.name,
1024 dn, dn->d_name.len, dn->d_name.name);
1025 dout("fill_trace doing d_move %p -> %p\n",
1026 req->r_old_dentry, dn);
1028 /* d_move screws up d_subdirs order */
1029 ceph_i_clear(dir, CEPH_I_COMPLETE);
1031 d_move(req->r_old_dentry, dn);
1032 dout(" src %p '%.*s' dst %p '%.*s'\n",
1034 req->r_old_dentry->d_name.len,
1035 req->r_old_dentry->d_name.name,
1036 dn, dn->d_name.len, dn->d_name.name);
1038 /* ensure target dentry is invalidated, despite
1039 rehashing bug in vfs_rename_dir */
1040 ceph_invalidate_dentry_lease(dn);
1042 /* take overwritten dentry's readdir offset */
1043 dout("dn %p gets %p offset %lld (old offset %lld)\n",
1044 req->r_old_dentry, dn, ceph_dentry(dn)->offset,
1045 ceph_dentry(req->r_old_dentry)->offset);
1046 ceph_dentry(req->r_old_dentry)->offset =
1047 ceph_dentry(dn)->offset;
1049 dn = req->r_old_dentry; /* use old_dentry */
1054 if (!rinfo->head->is_target) {
1055 dout("fill_trace null dentry\n");
1057 dout("d_delete %p\n", dn);
1060 dout("d_instantiate %p NULL\n", dn);
1061 d_instantiate(dn, NULL);
1062 if (have_lease && d_unhashed(dn))
1064 update_dentry_lease(dn, rinfo->dlease,
1066 req->r_request_started);
1071 /* attach proper inode */
1072 ininfo = rinfo->targeti.in;
1073 vino.ino = le64_to_cpu(ininfo->ino);
1074 vino.snap = le64_to_cpu(ininfo->snapid);
1077 in = ceph_get_inode(sb, vino);
1079 pr_err("fill_trace bad get_inode "
1080 "%llx.%llx\n", vino.ino, vino.snap);
1085 dn = splice_dentry(dn, in, &have_lease, true);
1090 req->r_dentry = dn; /* may have spliced */
1092 } else if (ceph_ino(in) == vino.ino &&
1093 ceph_snap(in) == vino.snap) {
1096 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1097 dn, in, ceph_ino(in), ceph_snap(in),
1098 vino.ino, vino.snap);
1104 update_dentry_lease(dn, rinfo->dlease, session,
1105 req->r_request_started);
1106 dout(" final dn %p\n", dn);
1108 } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1109 req->r_op == CEPH_MDS_OP_MKSNAP) {
1110 struct dentry *dn = req->r_dentry;
1112 /* fill out a snapdir LOOKUPSNAP dentry */
1114 BUG_ON(!req->r_locked_dir);
1115 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1116 ininfo = rinfo->targeti.in;
1117 vino.ino = le64_to_cpu(ininfo->ino);
1118 vino.snap = le64_to_cpu(ininfo->snapid);
1119 in = ceph_get_inode(sb, vino);
1121 pr_err("fill_inode get_inode badness %llx.%llx\n",
1122 vino.ino, vino.snap);
1127 dout(" linking snapped dir %p to dn %p\n", in, dn);
1128 dn = splice_dentry(dn, in, NULL, true);
1133 req->r_dentry = dn; /* may have spliced */
1135 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1138 if (rinfo->head->is_target) {
1139 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1140 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1142 if (in == NULL || ceph_ino(in) != vino.ino ||
1143 ceph_snap(in) != vino.snap) {
1144 in = ceph_get_inode(sb, vino);
1150 req->r_target_inode = in;
1152 err = fill_inode(in,
1153 &rinfo->targeti, NULL,
1154 session, req->r_request_started,
1155 (le32_to_cpu(rinfo->head->result) == 0) ?
1157 &req->r_caps_reservation);
1159 pr_err("fill_inode badness %p %llx.%llx\n",
1160 in, ceph_vinop(in));
1166 dout("fill_trace done err=%d\n", err);
1171 * Prepopulate our cache with readdir results, leases, etc.
1173 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1174 struct ceph_mds_session *session)
1176 struct dentry *parent = req->r_dentry;
1177 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1182 struct inode *snapdir = NULL;
1183 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1184 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1185 struct ceph_dentry_info *di;
1187 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1188 snapdir = ceph_get_snapdir(parent->d_inode);
1189 parent = d_find_alias(snapdir);
1190 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1191 rinfo->dir_nr, parent);
1193 dout("readdir_prepopulate %d items under dn %p\n",
1194 rinfo->dir_nr, parent);
1196 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1199 for (i = 0; i < rinfo->dir_nr; i++) {
1200 struct ceph_vino vino;
1202 dname.name = rinfo->dir_dname[i];
1203 dname.len = rinfo->dir_dname_len[i];
1204 dname.hash = full_name_hash(dname.name, dname.len);
1206 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1207 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1210 dn = d_lookup(parent, &dname);
1211 dout("d_lookup on parent=%p name=%.*s got %p\n",
1212 parent, dname.len, dname.name, dn);
1215 dn = d_alloc(parent, &dname);
1216 dout("d_alloc %p '%.*s' = %p\n", parent,
1217 dname.len, dname.name, dn);
1219 dout("d_alloc badness\n");
1223 err = ceph_init_dentry(dn);
1228 } else if (dn->d_inode &&
1229 (ceph_ino(dn->d_inode) != vino.ino ||
1230 ceph_snap(dn->d_inode) != vino.snap)) {
1231 dout(" dn %p points to wrong inode %p\n",
1237 /* reorder parent's d_subdirs */
1238 spin_lock(&dcache_lock);
1239 spin_lock(&dn->d_lock);
1240 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1241 spin_unlock(&dn->d_lock);
1242 spin_unlock(&dcache_lock);
1246 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1252 in = ceph_get_inode(parent->d_sb, vino);
1254 dout("new_inode badness\n");
1260 dn = splice_dentry(dn, in, NULL, false);
1265 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1266 req->r_request_started, -1,
1267 &req->r_caps_reservation) < 0) {
1268 pr_err("fill_inode badness on %p\n", in);
1272 update_dentry_lease(dn, rinfo->dir_dlease[i],
1274 req->r_request_started);
1279 req->r_did_prepopulate = true;
1286 dout("readdir_prepopulate done\n");
1290 int ceph_inode_set_size(struct inode *inode, loff_t size)
1292 struct ceph_inode_info *ci = ceph_inode(inode);
1295 spin_lock(&inode->i_lock);
1296 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1297 inode->i_size = size;
1298 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1300 /* tell the MDS if we are approaching max_size */
1301 if ((size << 1) >= ci->i_max_size &&
1302 (ci->i_reported_size << 1) < ci->i_max_size)
1305 spin_unlock(&inode->i_lock);
1310 * Write back inode data in a worker thread. (This can't be done
1311 * in the message handler context.)
1313 void ceph_queue_writeback(struct inode *inode)
1315 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1316 &ceph_inode(inode)->i_wb_work)) {
1317 dout("ceph_queue_writeback %p\n", inode);
1320 dout("ceph_queue_writeback %p failed\n", inode);
1324 static void ceph_writeback_work(struct work_struct *work)
1326 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1328 struct inode *inode = &ci->vfs_inode;
1330 dout("writeback %p\n", inode);
1331 filemap_fdatawrite(&inode->i_data);
1336 * queue an async invalidation
1338 void ceph_queue_invalidate(struct inode *inode)
1340 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1341 &ceph_inode(inode)->i_pg_inv_work)) {
1342 dout("ceph_queue_invalidate %p\n", inode);
1345 dout("ceph_queue_invalidate %p failed\n", inode);
1350 * invalidate any pages that are not dirty or under writeback. this
1351 * includes pages that are clean and mapped.
1353 static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
1355 struct pagevec pvec;
1359 pagevec_init(&pvec, 0);
1360 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
1361 for (i = 0; i < pagevec_count(&pvec); i++) {
1362 struct page *page = pvec.pages[i];
1365 (PageDirty(page) || PageWriteback(page));
1368 skip_page = !trylock_page(page);
1371 * We really shouldn't be looking at the ->index of an
1372 * unlocked page. But we're not allowed to lock these
1373 * pages. So we rely upon nobody altering the ->index
1374 * of this (pinned-by-us) page.
1376 index = page->index;
1384 generic_error_remove_page(mapping, page);
1387 pagevec_release(&pvec);
1393 * Invalidate inode pages in a worker thread. (This can't be done
1394 * in the message handler context.)
1396 static void ceph_invalidate_work(struct work_struct *work)
1398 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1400 struct inode *inode = &ci->vfs_inode;
1404 spin_lock(&inode->i_lock);
1405 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1406 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1407 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1409 spin_unlock(&inode->i_lock);
1412 orig_gen = ci->i_rdcache_gen;
1413 spin_unlock(&inode->i_lock);
1415 ceph_invalidate_nondirty_pages(inode->i_mapping);
1417 spin_lock(&inode->i_lock);
1418 if (orig_gen == ci->i_rdcache_gen &&
1419 orig_gen == ci->i_rdcache_revoking) {
1420 dout("invalidate_pages %p gen %d successful\n", inode,
1422 ci->i_rdcache_revoking--;
1425 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1426 inode, orig_gen, ci->i_rdcache_gen,
1427 ci->i_rdcache_revoking);
1429 spin_unlock(&inode->i_lock);
1432 ceph_check_caps(ci, 0, NULL);
1439 * called by trunc_wq; take i_mutex ourselves
1441 * We also truncate in a separate thread as well.
1443 static void ceph_vmtruncate_work(struct work_struct *work)
1445 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1447 struct inode *inode = &ci->vfs_inode;
1449 dout("vmtruncate_work %p\n", inode);
1450 mutex_lock(&inode->i_mutex);
1451 __ceph_do_pending_vmtruncate(inode);
1452 mutex_unlock(&inode->i_mutex);
1457 * Queue an async vmtruncate. If we fail to queue work, we will handle
1458 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1460 void ceph_queue_vmtruncate(struct inode *inode)
1462 struct ceph_inode_info *ci = ceph_inode(inode);
1464 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
1465 &ci->i_vmtruncate_work)) {
1466 dout("ceph_queue_vmtruncate %p\n", inode);
1469 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1470 inode, ci->i_truncate_pending);
1475 * called with i_mutex held.
1477 * Make sure any pending truncation is applied before doing anything
1478 * that may depend on it.
1480 void __ceph_do_pending_vmtruncate(struct inode *inode)
1482 struct ceph_inode_info *ci = ceph_inode(inode);
1484 int wrbuffer_refs, wake = 0;
1487 spin_lock(&inode->i_lock);
1488 if (ci->i_truncate_pending == 0) {
1489 dout("__do_pending_vmtruncate %p none pending\n", inode);
1490 spin_unlock(&inode->i_lock);
1495 * make sure any dirty snapped pages are flushed before we
1496 * possibly truncate them.. so write AND block!
1498 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1499 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1501 spin_unlock(&inode->i_lock);
1502 filemap_write_and_wait_range(&inode->i_data, 0,
1503 inode->i_sb->s_maxbytes);
1507 to = ci->i_truncate_size;
1508 wrbuffer_refs = ci->i_wrbuffer_ref;
1509 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1510 ci->i_truncate_pending, to);
1511 spin_unlock(&inode->i_lock);
1513 truncate_inode_pages(inode->i_mapping, to);
1515 spin_lock(&inode->i_lock);
1516 ci->i_truncate_pending--;
1517 if (ci->i_truncate_pending == 0)
1519 spin_unlock(&inode->i_lock);
1521 if (wrbuffer_refs == 0)
1522 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1524 wake_up_all(&ci->i_cap_wq);
1531 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1533 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1534 nd_set_link(nd, ci->i_symlink);
1538 static const struct inode_operations ceph_symlink_iops = {
1539 .readlink = generic_readlink,
1540 .follow_link = ceph_sym_follow_link,
1546 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1548 struct inode *inode = dentry->d_inode;
1549 struct ceph_inode_info *ci = ceph_inode(inode);
1550 struct inode *parent_inode = dentry->d_parent->d_inode;
1551 const unsigned int ia_valid = attr->ia_valid;
1552 struct ceph_mds_request *req;
1553 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
1555 int release = 0, dirtied = 0;
1559 if (ceph_snap(inode) != CEPH_NOSNAP)
1562 __ceph_do_pending_vmtruncate(inode);
1564 err = inode_change_ok(inode, attr);
1568 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1571 return PTR_ERR(req);
1573 spin_lock(&inode->i_lock);
1574 issued = __ceph_caps_issued(ci, NULL);
1575 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1577 if (ia_valid & ATTR_UID) {
1578 dout("setattr %p uid %d -> %d\n", inode,
1579 inode->i_uid, attr->ia_uid);
1580 if (issued & CEPH_CAP_AUTH_EXCL) {
1581 inode->i_uid = attr->ia_uid;
1582 dirtied |= CEPH_CAP_AUTH_EXCL;
1583 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1584 attr->ia_uid != inode->i_uid) {
1585 req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1586 mask |= CEPH_SETATTR_UID;
1587 release |= CEPH_CAP_AUTH_SHARED;
1590 if (ia_valid & ATTR_GID) {
1591 dout("setattr %p gid %d -> %d\n", inode,
1592 inode->i_gid, attr->ia_gid);
1593 if (issued & CEPH_CAP_AUTH_EXCL) {
1594 inode->i_gid = attr->ia_gid;
1595 dirtied |= CEPH_CAP_AUTH_EXCL;
1596 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1597 attr->ia_gid != inode->i_gid) {
1598 req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1599 mask |= CEPH_SETATTR_GID;
1600 release |= CEPH_CAP_AUTH_SHARED;
1603 if (ia_valid & ATTR_MODE) {
1604 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1606 if (issued & CEPH_CAP_AUTH_EXCL) {
1607 inode->i_mode = attr->ia_mode;
1608 dirtied |= CEPH_CAP_AUTH_EXCL;
1609 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1610 attr->ia_mode != inode->i_mode) {
1611 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1612 mask |= CEPH_SETATTR_MODE;
1613 release |= CEPH_CAP_AUTH_SHARED;
1617 if (ia_valid & ATTR_ATIME) {
1618 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1619 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1620 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1621 if (issued & CEPH_CAP_FILE_EXCL) {
1622 ci->i_time_warp_seq++;
1623 inode->i_atime = attr->ia_atime;
1624 dirtied |= CEPH_CAP_FILE_EXCL;
1625 } else if ((issued & CEPH_CAP_FILE_WR) &&
1626 timespec_compare(&inode->i_atime,
1627 &attr->ia_atime) < 0) {
1628 inode->i_atime = attr->ia_atime;
1629 dirtied |= CEPH_CAP_FILE_WR;
1630 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1631 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1632 ceph_encode_timespec(&req->r_args.setattr.atime,
1634 mask |= CEPH_SETATTR_ATIME;
1635 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1639 if (ia_valid & ATTR_MTIME) {
1640 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1641 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1642 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1643 if (issued & CEPH_CAP_FILE_EXCL) {
1644 ci->i_time_warp_seq++;
1645 inode->i_mtime = attr->ia_mtime;
1646 dirtied |= CEPH_CAP_FILE_EXCL;
1647 } else if ((issued & CEPH_CAP_FILE_WR) &&
1648 timespec_compare(&inode->i_mtime,
1649 &attr->ia_mtime) < 0) {
1650 inode->i_mtime = attr->ia_mtime;
1651 dirtied |= CEPH_CAP_FILE_WR;
1652 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1653 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1654 ceph_encode_timespec(&req->r_args.setattr.mtime,
1656 mask |= CEPH_SETATTR_MTIME;
1657 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1661 if (ia_valid & ATTR_SIZE) {
1662 dout("setattr %p size %lld -> %lld\n", inode,
1663 inode->i_size, attr->ia_size);
1664 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1668 if ((issued & CEPH_CAP_FILE_EXCL) &&
1669 attr->ia_size > inode->i_size) {
1670 inode->i_size = attr->ia_size;
1672 (attr->ia_size + (1 << 9) - 1) >> 9;
1673 inode->i_ctime = attr->ia_ctime;
1674 ci->i_reported_size = attr->ia_size;
1675 dirtied |= CEPH_CAP_FILE_EXCL;
1676 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1677 attr->ia_size != inode->i_size) {
1678 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1679 req->r_args.setattr.old_size =
1680 cpu_to_le64(inode->i_size);
1681 mask |= CEPH_SETATTR_SIZE;
1682 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1687 /* these do nothing */
1688 if (ia_valid & ATTR_CTIME) {
1689 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1690 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1691 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1692 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1693 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1694 only ? "ctime only" : "ignored");
1695 inode->i_ctime = attr->ia_ctime;
1698 * if kernel wants to dirty ctime but nothing else,
1699 * we need to choose a cap to dirty under, or do
1700 * a almost-no-op setattr
1702 if (issued & CEPH_CAP_AUTH_EXCL)
1703 dirtied |= CEPH_CAP_AUTH_EXCL;
1704 else if (issued & CEPH_CAP_FILE_EXCL)
1705 dirtied |= CEPH_CAP_FILE_EXCL;
1706 else if (issued & CEPH_CAP_XATTR_EXCL)
1707 dirtied |= CEPH_CAP_XATTR_EXCL;
1709 mask |= CEPH_SETATTR_CTIME;
1712 if (ia_valid & ATTR_FILE)
1713 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1716 __ceph_mark_dirty_caps(ci, dirtied);
1717 inode->i_ctime = CURRENT_TIME;
1721 spin_unlock(&inode->i_lock);
1724 req->r_inode = igrab(inode);
1725 req->r_inode_drop = release;
1726 req->r_args.setattr.mask = cpu_to_le32(mask);
1727 req->r_num_caps = 1;
1728 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1730 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1731 ceph_cap_string(dirtied), mask);
1733 ceph_mdsc_put_request(req);
1734 __ceph_do_pending_vmtruncate(inode);
1737 spin_unlock(&inode->i_lock);
1738 ceph_mdsc_put_request(req);
1743 * Verify that we have a lease on the given mask. If not,
1744 * do a getattr against an mds.
1746 int ceph_do_getattr(struct inode *inode, int mask)
1748 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1749 struct ceph_mds_client *mdsc = fsc->mdsc;
1750 struct ceph_mds_request *req;
1753 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1754 dout("do_getattr inode %p SNAPDIR\n", inode);
1758 dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
1759 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1762 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1764 return PTR_ERR(req);
1765 req->r_inode = igrab(inode);
1766 req->r_num_caps = 1;
1767 req->r_args.getattr.mask = cpu_to_le32(mask);
1768 err = ceph_mdsc_do_request(mdsc, NULL, req);
1769 ceph_mdsc_put_request(req);
1770 dout("do_getattr result=%d\n", err);
1776 * Check inode permissions. We verify we have a valid value for
1777 * the AUTH cap, then call the generic handler.
1779 int ceph_permission(struct inode *inode, int mask)
1781 int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1784 err = generic_permission(inode, mask, NULL);
1789 * Get all attributes. Hopefully somedata we'll have a statlite()
1790 * and can limit the fields we require to be accurate.
1792 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1795 struct inode *inode = dentry->d_inode;
1796 struct ceph_inode_info *ci = ceph_inode(inode);
1799 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1801 generic_fillattr(inode, stat);
1802 stat->ino = inode->i_ino;
1803 if (ceph_snap(inode) != CEPH_NOSNAP)
1804 stat->dev = ceph_snap(inode);
1807 if (S_ISDIR(inode->i_mode)) {
1808 stat->size = ci->i_rbytes;
1810 stat->blksize = 65536;