1 #include "ceph_debug.h"
3 #include <linux/module.h>
5 #include <linux/smp_lock.h>
6 #include <linux/slab.h>
7 #include <linux/string.h>
8 #include <linux/uaccess.h>
9 #include <linux/kernel.h>
10 #include <linux/namei.h>
11 #include <linux/writeback.h>
12 #include <linux/vmalloc.h>
18 * Ceph inode operations
20 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
21 * setattr, etc.), xattr helpers, and helpers for assimilating
22 * metadata returned by the MDS into our cache.
24 * Also define helpers for doing asynchronous writeback, invalidation,
25 * and truncation for the benefit of those who can't afford to block
26 * (typically because they are in the message handler path).
29 static const struct inode_operations ceph_symlink_iops;
31 static void ceph_inode_invalidate_pages(struct work_struct *work);
34 * find or create an inode, given the ceph ino number
36 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
39 ino_t t = ceph_vino_to_ino(vino);
41 inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
43 return ERR_PTR(-ENOMEM);
44 if (inode->i_state & I_NEW) {
45 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
46 inode, ceph_vinop(inode), (u64)inode->i_ino);
47 unlock_new_inode(inode);
50 dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
56 * get/constuct snapdir inode for a given directory
58 struct inode *ceph_get_snapdir(struct inode *parent)
60 struct ceph_vino vino = {
61 .ino = ceph_ino(parent),
64 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
65 struct ceph_inode_info *ci = ceph_inode(inode);
67 BUG_ON(!S_ISDIR(parent->i_mode));
69 return ERR_PTR(PTR_ERR(inode));
70 inode->i_mode = parent->i_mode;
71 inode->i_uid = parent->i_uid;
72 inode->i_gid = parent->i_gid;
73 inode->i_op = &ceph_dir_iops;
74 inode->i_fop = &ceph_dir_fops;
75 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
80 const struct inode_operations ceph_file_iops = {
81 .permission = ceph_permission,
82 .setattr = ceph_setattr,
83 .getattr = ceph_getattr,
84 .setxattr = ceph_setxattr,
85 .getxattr = ceph_getxattr,
86 .listxattr = ceph_listxattr,
87 .removexattr = ceph_removexattr,
92 * We use a 'frag tree' to keep track of the MDS's directory fragments
93 * for a given inode (usually there is just a single fragment). We
94 * need to know when a child frag is delegated to a new MDS, or when
95 * it is flagged as replicated, so we can direct our requests
100 * find/create a frag in the tree
102 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
106 struct rb_node *parent = NULL;
107 struct ceph_inode_frag *frag;
110 p = &ci->i_fragtree.rb_node;
113 frag = rb_entry(parent, struct ceph_inode_frag, node);
114 c = ceph_frag_compare(f, frag->frag);
123 frag = kmalloc(sizeof(*frag), GFP_NOFS);
125 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
126 "frag %x\n", &ci->vfs_inode,
127 ceph_vinop(&ci->vfs_inode), f);
128 return ERR_PTR(-ENOMEM);
135 rb_link_node(&frag->node, parent, p);
136 rb_insert_color(&frag->node, &ci->i_fragtree);
138 dout("get_or_create_frag added %llx.%llx frag %x\n",
139 ceph_vinop(&ci->vfs_inode), f);
144 * find a specific frag @f
146 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
148 struct rb_node *n = ci->i_fragtree.rb_node;
151 struct ceph_inode_frag *frag =
152 rb_entry(n, struct ceph_inode_frag, node);
153 int c = ceph_frag_compare(f, frag->frag);
165 * Choose frag containing the given value @v. If @pfrag is
166 * specified, copy the frag delegation info to the caller if
169 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
170 struct ceph_inode_frag *pfrag,
173 u32 t = ceph_frag_make(0, 0);
174 struct ceph_inode_frag *frag;
181 mutex_lock(&ci->i_fragtree_mutex);
183 WARN_ON(!ceph_frag_contains_value(t, v));
184 frag = __ceph_find_frag(ci, t);
186 break; /* t is a leaf */
187 if (frag->split_by == 0) {
189 memcpy(pfrag, frag, sizeof(*pfrag));
196 nway = 1 << frag->split_by;
197 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
198 frag->split_by, nway);
199 for (i = 0; i < nway; i++) {
200 n = ceph_frag_make_child(t, frag->split_by, i);
201 if (ceph_frag_contains_value(n, v)) {
208 dout("choose_frag(%x) = %x\n", v, t);
210 mutex_unlock(&ci->i_fragtree_mutex);
215 * Process dirfrag (delegation) info from the mds. Include leaf
216 * fragment in tree ONLY if ndist > 0. Otherwise, only
217 * branches/splits are included in i_fragtree)
219 static int ceph_fill_dirfrag(struct inode *inode,
220 struct ceph_mds_reply_dirfrag *dirinfo)
222 struct ceph_inode_info *ci = ceph_inode(inode);
223 struct ceph_inode_frag *frag;
224 u32 id = le32_to_cpu(dirinfo->frag);
225 int mds = le32_to_cpu(dirinfo->auth);
226 int ndist = le32_to_cpu(dirinfo->ndist);
230 mutex_lock(&ci->i_fragtree_mutex);
232 /* no delegation info needed. */
233 frag = __ceph_find_frag(ci, id);
236 if (frag->split_by == 0) {
237 /* tree leaf, remove */
238 dout("fill_dirfrag removed %llx.%llx frag %x"
239 " (no ref)\n", ceph_vinop(inode), id);
240 rb_erase(&frag->node, &ci->i_fragtree);
243 /* tree branch, keep and clear */
244 dout("fill_dirfrag cleared %llx.%llx frag %x"
245 " referral\n", ceph_vinop(inode), id);
253 /* find/add this frag to store mds delegation info */
254 frag = __get_or_create_frag(ci, id);
256 /* this is not the end of the world; we can continue
257 with bad/inaccurate delegation info */
258 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
259 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
265 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
266 for (i = 0; i < frag->ndist; i++)
267 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
268 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
269 ceph_vinop(inode), frag->frag, frag->ndist);
272 mutex_unlock(&ci->i_fragtree_mutex);
278 * initialize a newly allocated inode.
280 struct inode *ceph_alloc_inode(struct super_block *sb)
282 struct ceph_inode_info *ci;
285 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
289 dout("alloc_inode %p\n", &ci->vfs_inode);
292 ci->i_time_warp_seq = 0;
293 ci->i_ceph_flags = 0;
294 ci->i_release_count = 0;
295 ci->i_symlink = NULL;
297 ci->i_fragtree = RB_ROOT;
298 mutex_init(&ci->i_fragtree_mutex);
300 ci->i_xattrs.blob = NULL;
301 ci->i_xattrs.prealloc_blob = NULL;
302 ci->i_xattrs.dirty = false;
303 ci->i_xattrs.index = RB_ROOT;
304 ci->i_xattrs.count = 0;
305 ci->i_xattrs.names_size = 0;
306 ci->i_xattrs.vals_size = 0;
307 ci->i_xattrs.version = 0;
308 ci->i_xattrs.index_version = 0;
310 ci->i_caps = RB_ROOT;
311 ci->i_auth_cap = NULL;
312 ci->i_dirty_caps = 0;
313 ci->i_flushing_caps = 0;
314 INIT_LIST_HEAD(&ci->i_dirty_item);
315 INIT_LIST_HEAD(&ci->i_flushing_item);
316 ci->i_cap_flush_seq = 0;
317 ci->i_cap_flush_last_tid = 0;
318 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
319 init_waitqueue_head(&ci->i_cap_wq);
320 ci->i_hold_caps_min = 0;
321 ci->i_hold_caps_max = 0;
322 INIT_LIST_HEAD(&ci->i_cap_delay_list);
323 ci->i_cap_exporting_mds = 0;
324 ci->i_cap_exporting_mseq = 0;
325 ci->i_cap_exporting_issued = 0;
326 INIT_LIST_HEAD(&ci->i_cap_snaps);
327 ci->i_head_snapc = NULL;
330 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
331 ci->i_nr_by_mode[i] = 0;
333 ci->i_truncate_seq = 0;
334 ci->i_truncate_size = 0;
335 ci->i_truncate_pending = 0;
338 ci->i_reported_size = 0;
339 ci->i_wanted_max_size = 0;
340 ci->i_requested_max_size = 0;
344 ci->i_rdcache_ref = 0;
346 ci->i_wrbuffer_ref = 0;
347 ci->i_wrbuffer_ref_head = 0;
348 ci->i_shared_gen = 0;
349 ci->i_rdcache_gen = 0;
350 ci->i_rdcache_revoking = 0;
352 INIT_LIST_HEAD(&ci->i_unsafe_writes);
353 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
354 spin_lock_init(&ci->i_unsafe_lock);
356 ci->i_snap_realm = NULL;
357 INIT_LIST_HEAD(&ci->i_snap_realm_item);
358 INIT_LIST_HEAD(&ci->i_snap_flush_item);
360 INIT_WORK(&ci->i_wb_work, ceph_inode_writeback);
361 INIT_WORK(&ci->i_pg_inv_work, ceph_inode_invalidate_pages);
363 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
365 return &ci->vfs_inode;
368 void ceph_destroy_inode(struct inode *inode)
370 struct ceph_inode_info *ci = ceph_inode(inode);
371 struct ceph_inode_frag *frag;
374 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
376 ceph_queue_caps_release(inode);
378 kfree(ci->i_symlink);
379 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
380 frag = rb_entry(n, struct ceph_inode_frag, node);
381 rb_erase(n, &ci->i_fragtree);
385 __ceph_destroy_xattrs(ci);
386 if (ci->i_xattrs.blob)
387 ceph_buffer_put(ci->i_xattrs.blob);
388 if (ci->i_xattrs.prealloc_blob)
389 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
391 kmem_cache_free(ceph_inode_cachep, ci);
396 * Helpers to fill in size, ctime, mtime, and atime. We have to be
397 * careful because either the client or MDS may have more up to date
398 * info, depending on which capabilities are held, and whether
399 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
400 * and size are monotonically increasing, except when utimes() or
401 * truncate() increments the corresponding _seq values.)
403 int ceph_fill_file_size(struct inode *inode, int issued,
404 u32 truncate_seq, u64 truncate_size, u64 size)
406 struct ceph_inode_info *ci = ceph_inode(inode);
409 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
410 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
411 dout("size %lld -> %llu\n", inode->i_size, size);
412 inode->i_size = size;
413 inode->i_blocks = (size + (1<<9) - 1) >> 9;
414 ci->i_reported_size = size;
415 if (truncate_seq != ci->i_truncate_seq) {
416 dout("truncate_seq %u -> %u\n",
417 ci->i_truncate_seq, truncate_seq);
418 ci->i_truncate_seq = truncate_seq;
419 if (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
420 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
421 CEPH_CAP_FILE_EXCL)) {
422 ci->i_truncate_pending++;
427 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
428 ci->i_truncate_size != truncate_size) {
429 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
431 ci->i_truncate_size = truncate_size;
436 void ceph_fill_file_time(struct inode *inode, int issued,
437 u64 time_warp_seq, struct timespec *ctime,
438 struct timespec *mtime, struct timespec *atime)
440 struct ceph_inode_info *ci = ceph_inode(inode);
443 if (issued & (CEPH_CAP_FILE_EXCL|
445 CEPH_CAP_FILE_BUFFER)) {
446 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
447 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
448 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
449 ctime->tv_sec, ctime->tv_nsec);
450 inode->i_ctime = *ctime;
452 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
453 /* the MDS did a utimes() */
454 dout("mtime %ld.%09ld -> %ld.%09ld "
456 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
457 mtime->tv_sec, mtime->tv_nsec,
458 ci->i_time_warp_seq, (int)time_warp_seq);
460 inode->i_mtime = *mtime;
461 inode->i_atime = *atime;
462 ci->i_time_warp_seq = time_warp_seq;
463 } else if (time_warp_seq == ci->i_time_warp_seq) {
464 /* nobody did utimes(); take the max */
465 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
466 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
467 inode->i_mtime.tv_sec,
468 inode->i_mtime.tv_nsec,
469 mtime->tv_sec, mtime->tv_nsec);
470 inode->i_mtime = *mtime;
472 if (timespec_compare(atime, &inode->i_atime) > 0) {
473 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
474 inode->i_atime.tv_sec,
475 inode->i_atime.tv_nsec,
476 atime->tv_sec, atime->tv_nsec);
477 inode->i_atime = *atime;
479 } else if (issued & CEPH_CAP_FILE_EXCL) {
480 /* we did a utimes(); ignore mds values */
485 /* we have no write caps; whatever the MDS says is true */
486 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
487 inode->i_ctime = *ctime;
488 inode->i_mtime = *mtime;
489 inode->i_atime = *atime;
490 ci->i_time_warp_seq = time_warp_seq;
495 if (warn) /* time_warp_seq shouldn't go backwards */
496 dout("%p mds time_warp_seq %llu < %u\n",
497 inode, time_warp_seq, ci->i_time_warp_seq);
501 * Populate an inode based on info from mds. May be called on new or
504 static int fill_inode(struct inode *inode,
505 struct ceph_mds_reply_info_in *iinfo,
506 struct ceph_mds_reply_dirfrag *dirinfo,
507 struct ceph_mds_session *session,
508 unsigned long ttl_from, int cap_fmode,
509 struct ceph_cap_reservation *caps_reservation)
511 struct ceph_mds_reply_inode *info = iinfo->in;
512 struct ceph_inode_info *ci = ceph_inode(inode);
514 int issued, implemented;
515 struct timespec mtime, atime, ctime;
517 struct ceph_buffer *xattr_blob = NULL;
521 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
522 inode, ceph_vinop(inode), le64_to_cpu(info->version),
526 * prealloc xattr data, if it looks like we'll need it. only
527 * if len > 4 (meaning there are actually xattrs; the first 4
528 * bytes are the xattr count).
530 if (iinfo->xattr_len > 4) {
531 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
533 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
537 spin_lock(&inode->i_lock);
540 * provided version will be odd if inode value is projected,
541 * even if stable. skip the update if we have a newer info
542 * (e.g., due to inode info racing form multiple MDSs), or if
543 * we are getting projected (unstable) inode info.
545 if (le64_to_cpu(info->version) > 0 &&
546 (ci->i_version & ~1) > le64_to_cpu(info->version))
549 issued = __ceph_caps_issued(ci, &implemented);
550 issued |= implemented | __ceph_caps_dirty(ci);
553 ci->i_version = le64_to_cpu(info->version);
555 inode->i_rdev = le32_to_cpu(info->rdev);
557 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
558 inode->i_mode = le32_to_cpu(info->mode);
559 inode->i_uid = le32_to_cpu(info->uid);
560 inode->i_gid = le32_to_cpu(info->gid);
561 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
562 inode->i_uid, inode->i_gid);
565 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
566 inode->i_nlink = le32_to_cpu(info->nlink);
568 /* be careful with mtime, atime, size */
569 ceph_decode_timespec(&atime, &info->atime);
570 ceph_decode_timespec(&mtime, &info->mtime);
571 ceph_decode_timespec(&ctime, &info->ctime);
572 queue_trunc = ceph_fill_file_size(inode, issued,
573 le32_to_cpu(info->truncate_seq),
574 le64_to_cpu(info->truncate_size),
575 le64_to_cpu(info->size));
576 ceph_fill_file_time(inode, issued,
577 le32_to_cpu(info->time_warp_seq),
578 &ctime, &mtime, &atime);
580 ci->i_max_size = le64_to_cpu(info->max_size);
581 ci->i_layout = info->layout;
582 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
585 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
586 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
587 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
588 if (ci->i_xattrs.blob)
589 ceph_buffer_put(ci->i_xattrs.blob);
590 ci->i_xattrs.blob = xattr_blob;
592 memcpy(ci->i_xattrs.blob->vec.iov_base,
593 iinfo->xattr_data, iinfo->xattr_len);
594 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
597 inode->i_mapping->a_ops = &ceph_aops;
598 inode->i_mapping->backing_dev_info =
599 &ceph_client(inode->i_sb)->backing_dev_info;
601 switch (inode->i_mode & S_IFMT) {
606 init_special_inode(inode, inode->i_mode, inode->i_rdev);
607 inode->i_op = &ceph_file_iops;
610 inode->i_op = &ceph_file_iops;
611 inode->i_fop = &ceph_file_fops;
614 inode->i_op = &ceph_symlink_iops;
615 if (!ci->i_symlink) {
616 int symlen = iinfo->symlink_len;
619 BUG_ON(symlen != inode->i_size);
620 spin_unlock(&inode->i_lock);
623 sym = kmalloc(symlen+1, GFP_NOFS);
626 memcpy(sym, iinfo->symlink, symlen);
629 spin_lock(&inode->i_lock);
633 kfree(sym); /* lost a race */
637 inode->i_op = &ceph_dir_iops;
638 inode->i_fop = &ceph_dir_fops;
640 ci->i_files = le64_to_cpu(info->files);
641 ci->i_subdirs = le64_to_cpu(info->subdirs);
642 ci->i_rbytes = le64_to_cpu(info->rbytes);
643 ci->i_rfiles = le64_to_cpu(info->rfiles);
644 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
645 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
647 /* set dir completion flag? */
648 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
649 ceph_snap(inode) == CEPH_NOSNAP &&
650 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) {
651 dout(" marking %p complete (empty)\n", inode);
652 ci->i_ceph_flags |= CEPH_I_COMPLETE;
653 ci->i_max_offset = 2;
656 /* it may be better to set st_size in getattr instead? */
657 if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES))
658 inode->i_size = ci->i_rbytes;
661 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
662 ceph_vinop(inode), inode->i_mode);
666 spin_unlock(&inode->i_lock);
668 /* queue truncate if we saw i_size decrease */
670 if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
671 &ci->i_vmtruncate_work))
674 /* populate frag tree */
675 /* FIXME: move me up, if/when version reflects fragtree changes */
676 nsplits = le32_to_cpu(info->fragtree.nsplits);
677 mutex_lock(&ci->i_fragtree_mutex);
678 for (i = 0; i < nsplits; i++) {
679 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
680 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
684 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
685 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
687 mutex_unlock(&ci->i_fragtree_mutex);
689 /* were we issued a capability? */
690 if (info->cap.caps) {
691 if (ceph_snap(inode) == CEPH_NOSNAP) {
692 ceph_add_cap(inode, session,
693 le64_to_cpu(info->cap.cap_id),
695 le32_to_cpu(info->cap.caps),
696 le32_to_cpu(info->cap.wanted),
697 le32_to_cpu(info->cap.seq),
698 le32_to_cpu(info->cap.mseq),
699 le64_to_cpu(info->cap.realm),
703 spin_lock(&inode->i_lock);
704 dout(" %p got snap_caps %s\n", inode,
705 ceph_cap_string(le32_to_cpu(info->cap.caps)));
706 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
708 __ceph_get_fmode(ci, cap_fmode);
709 spin_unlock(&inode->i_lock);
713 /* update delegation info? */
715 ceph_fill_dirfrag(inode, dirinfo);
721 ceph_buffer_put(xattr_blob);
726 * caller should hold session s_mutex.
728 static void update_dentry_lease(struct dentry *dentry,
729 struct ceph_mds_reply_lease *lease,
730 struct ceph_mds_session *session,
731 unsigned long from_time)
733 struct ceph_dentry_info *di = ceph_dentry(dentry);
734 long unsigned duration = le32_to_cpu(lease->duration_ms);
735 long unsigned ttl = from_time + (duration * HZ) / 1000;
736 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
739 /* only track leases on regular dentries */
740 if (dentry->d_op != &ceph_dentry_ops)
743 spin_lock(&dentry->d_lock);
744 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
745 dentry, le16_to_cpu(lease->mask), duration, ttl);
747 /* make lease_rdcache_gen match directory */
748 dir = dentry->d_parent->d_inode;
749 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
751 if (lease->mask == 0)
754 if (di->lease_gen == session->s_cap_gen &&
755 time_before(ttl, dentry->d_time))
756 goto out_unlock; /* we already have a newer lease. */
758 if (di->lease_session && di->lease_session != session)
761 ceph_dentry_lru_touch(dentry);
763 if (!di->lease_session)
764 di->lease_session = ceph_get_mds_session(session);
765 di->lease_gen = session->s_cap_gen;
766 di->lease_seq = le32_to_cpu(lease->seq);
767 di->lease_renew_after = half_ttl;
768 di->lease_renew_from = 0;
769 dentry->d_time = ttl;
771 spin_unlock(&dentry->d_lock);
776 * splice a dentry to an inode.
777 * caller must hold directory i_mutex for this to be safe.
779 * we will only rehash the resulting dentry if @prehash is
780 * true; @prehash will be set to false (for the benefit of
781 * the caller) if we fail.
783 static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
786 struct dentry *realdn;
788 /* dn must be unhashed */
791 realdn = d_materialise_unique(dn, in);
792 if (IS_ERR(realdn)) {
793 pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
794 dn, in, ceph_vinop(in));
796 *prehash = false; /* don't rehash on error */
797 dn = realdn; /* note realdn contains the error */
800 dout("dn %p (%d) spliced with %p (%d) "
801 "inode %p ino %llx.%llx\n",
802 dn, atomic_read(&dn->d_count),
803 realdn, atomic_read(&realdn->d_count),
804 realdn->d_inode, ceph_vinop(realdn->d_inode));
808 BUG_ON(!ceph_dentry(dn));
810 dout("dn %p attached to %p ino %llx.%llx\n",
811 dn, dn->d_inode, ceph_vinop(dn->d_inode));
813 if ((!prehash || *prehash) && d_unhashed(dn))
820 * Incorporate results into the local cache. This is either just
821 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
824 * A reply may contain
825 * a directory inode along with a dentry.
826 * and/or a target inode
828 * Called with snap_rwsem (read).
830 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
831 struct ceph_mds_session *session)
833 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
834 struct inode *in = NULL;
835 struct ceph_mds_reply_inode *ininfo;
836 struct ceph_vino vino;
840 dout("fill_trace %p is_dentry %d is_target %d\n", req,
841 rinfo->head->is_dentry, rinfo->head->is_target);
847 * If we resend completed ops to a recovering mds, we get no
848 * trace. Since that is very rare, pretend this is the case
849 * to ensure the 'no trace' handlers in the callers behave.
851 * Fill in inodes unconditionally to avoid breaking cap
854 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
855 pr_info("fill_trace faking empty trace on %lld %s\n",
856 req->r_tid, ceph_mds_op_name(rinfo->head->op));
857 if (rinfo->head->is_dentry) {
858 rinfo->head->is_dentry = 0;
859 err = fill_inode(req->r_locked_dir,
860 &rinfo->diri, rinfo->dirfrag,
861 session, req->r_request_started, -1);
863 if (rinfo->head->is_target) {
864 rinfo->head->is_target = 0;
865 ininfo = rinfo->targeti.in;
866 vino.ino = le64_to_cpu(ininfo->ino);
867 vino.snap = le64_to_cpu(ininfo->snapid);
868 in = ceph_get_inode(sb, vino);
869 err = fill_inode(in, &rinfo->targeti, NULL,
870 session, req->r_request_started,
877 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
878 dout("fill_trace reply is empty!\n");
879 if (rinfo->head->result == 0 && req->r_locked_dir) {
880 struct ceph_inode_info *ci =
881 ceph_inode(req->r_locked_dir);
882 dout(" clearing %p complete (empty trace)\n",
884 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
885 ci->i_release_count++;
890 if (rinfo->head->is_dentry) {
892 * lookup link rename : null -> possibly existing inode
893 * mknod symlink mkdir : null -> new inode
894 * unlink : linked -> null
896 struct inode *dir = req->r_locked_dir;
897 struct dentry *dn = req->r_dentry;
898 bool have_dir_cap, have_lease;
902 BUG_ON(dn->d_parent->d_inode != dir);
903 BUG_ON(ceph_ino(dir) !=
904 le64_to_cpu(rinfo->diri.in->ino));
905 BUG_ON(ceph_snap(dir) !=
906 le64_to_cpu(rinfo->diri.in->snapid));
908 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
909 session, req->r_request_started, -1,
910 &req->r_caps_reservation);
914 /* do we have a lease on the whole dir? */
916 (le32_to_cpu(rinfo->diri.in->cap.caps) &
917 CEPH_CAP_FILE_SHARED);
919 /* do we have a dn lease? */
920 have_lease = have_dir_cap ||
921 (le16_to_cpu(rinfo->dlease->mask) &
925 dout("fill_trace no dentry lease or dir cap\n");
928 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
929 dout(" src %p '%.*s' dst %p '%.*s'\n",
931 req->r_old_dentry->d_name.len,
932 req->r_old_dentry->d_name.name,
933 dn, dn->d_name.len, dn->d_name.name);
934 dout("fill_trace doing d_move %p -> %p\n",
935 req->r_old_dentry, dn);
936 d_move(req->r_old_dentry, dn);
937 dout(" src %p '%.*s' dst %p '%.*s'\n",
939 req->r_old_dentry->d_name.len,
940 req->r_old_dentry->d_name.name,
941 dn, dn->d_name.len, dn->d_name.name);
942 /* ensure target dentry is invalidated, despite
943 rehashing bug in vfs_rename_dir */
944 dn->d_time = jiffies;
945 ceph_dentry(dn)->lease_shared_gen = 0;
946 /* take overwritten dentry's readdir offset */
947 ceph_dentry(req->r_old_dentry)->offset =
948 ceph_dentry(dn)->offset;
949 dn = req->r_old_dentry; /* use old_dentry */
954 if (!rinfo->head->is_target) {
955 dout("fill_trace null dentry\n");
957 dout("d_delete %p\n", dn);
960 dout("d_instantiate %p NULL\n", dn);
961 d_instantiate(dn, NULL);
962 if (have_lease && d_unhashed(dn))
964 update_dentry_lease(dn, rinfo->dlease,
966 req->r_request_started);
971 /* attach proper inode */
972 ininfo = rinfo->targeti.in;
973 vino.ino = le64_to_cpu(ininfo->ino);
974 vino.snap = le64_to_cpu(ininfo->snapid);
976 in = ceph_get_inode(sb, vino);
978 pr_err("fill_trace bad get_inode "
979 "%llx.%llx\n", vino.ino, vino.snap);
984 dn = splice_dentry(dn, in, &have_lease);
989 req->r_dentry = dn; /* may have spliced */
991 } else if (ceph_ino(in) == vino.ino &&
992 ceph_snap(in) == vino.snap) {
995 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
996 dn, in, ceph_ino(in), ceph_snap(in),
997 vino.ino, vino.snap);
1003 update_dentry_lease(dn, rinfo->dlease, session,
1004 req->r_request_started);
1005 dout(" final dn %p\n", dn);
1007 } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1008 req->r_op == CEPH_MDS_OP_MKSNAP) {
1009 struct dentry *dn = req->r_dentry;
1011 /* fill out a snapdir LOOKUPSNAP dentry */
1013 BUG_ON(!req->r_locked_dir);
1014 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1015 ininfo = rinfo->targeti.in;
1016 vino.ino = le64_to_cpu(ininfo->ino);
1017 vino.snap = le64_to_cpu(ininfo->snapid);
1018 in = ceph_get_inode(sb, vino);
1020 pr_err("fill_inode get_inode badness %llx.%llx\n",
1021 vino.ino, vino.snap);
1026 dout(" linking snapped dir %p to dn %p\n", in, dn);
1027 dn = splice_dentry(dn, in, NULL);
1032 req->r_dentry = dn; /* may have spliced */
1034 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1037 if (rinfo->head->is_target) {
1038 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1039 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1041 if (in == NULL || ceph_ino(in) != vino.ino ||
1042 ceph_snap(in) != vino.snap) {
1043 in = ceph_get_inode(sb, vino);
1049 req->r_target_inode = in;
1051 err = fill_inode(in,
1052 &rinfo->targeti, NULL,
1053 session, req->r_request_started,
1054 (le32_to_cpu(rinfo->head->result) == 0) ?
1056 &req->r_caps_reservation);
1058 pr_err("fill_inode badness %p %llx.%llx\n",
1059 in, ceph_vinop(in));
1065 dout("fill_trace done err=%d\n", err);
1070 * Prepopulate our cache with readdir results, leases, etc.
1072 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1073 struct ceph_mds_session *session)
1075 struct dentry *parent = req->r_dentry;
1076 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1081 struct inode *snapdir = NULL;
1082 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1083 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1084 struct ceph_dentry_info *di;
1086 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1087 snapdir = ceph_get_snapdir(parent->d_inode);
1088 parent = d_find_alias(snapdir);
1089 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1090 rinfo->dir_nr, parent);
1092 dout("readdir_prepopulate %d items under dn %p\n",
1093 rinfo->dir_nr, parent);
1095 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1098 for (i = 0; i < rinfo->dir_nr; i++) {
1099 struct ceph_vino vino;
1101 dname.name = rinfo->dir_dname[i];
1102 dname.len = rinfo->dir_dname_len[i];
1103 dname.hash = full_name_hash(dname.name, dname.len);
1105 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1106 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1109 dn = d_lookup(parent, &dname);
1110 dout("d_lookup on parent=%p name=%.*s got %p\n",
1111 parent, dname.len, dname.name, dn);
1114 dn = d_alloc(parent, &dname);
1115 dout("d_alloc %p '%.*s' = %p\n", parent,
1116 dname.len, dname.name, dn);
1118 dout("d_alloc badness\n");
1122 err = ceph_init_dentry(dn);
1125 } else if (dn->d_inode &&
1126 (ceph_ino(dn->d_inode) != vino.ino ||
1127 ceph_snap(dn->d_inode) != vino.snap)) {
1128 dout(" dn %p points to wrong inode %p\n",
1134 /* reorder parent's d_subdirs */
1135 spin_lock(&dcache_lock);
1136 spin_lock(&dn->d_lock);
1137 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1138 spin_unlock(&dn->d_lock);
1139 spin_unlock(&dcache_lock);
1143 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1149 in = ceph_get_inode(parent->d_sb, vino);
1151 dout("new_inode badness\n");
1157 dn = splice_dentry(dn, in, NULL);
1160 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1161 req->r_request_started, -1,
1162 &req->r_caps_reservation) < 0) {
1163 pr_err("fill_inode badness on %p\n", in);
1167 update_dentry_lease(dn, rinfo->dir_dlease[i],
1168 req->r_session, req->r_request_started);
1171 req->r_did_prepopulate = true;
1178 dout("readdir_prepopulate done\n");
1182 int ceph_inode_set_size(struct inode *inode, loff_t size)
1184 struct ceph_inode_info *ci = ceph_inode(inode);
1187 spin_lock(&inode->i_lock);
1188 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1189 inode->i_size = size;
1190 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1192 /* tell the MDS if we are approaching max_size */
1193 if ((size << 1) >= ci->i_max_size &&
1194 (ci->i_reported_size << 1) < ci->i_max_size)
1197 spin_unlock(&inode->i_lock);
1202 * Write back inode data in a worker thread. (This can't be done
1203 * in the message handler context.)
1205 void ceph_inode_writeback(struct work_struct *work)
1207 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1209 struct inode *inode = &ci->vfs_inode;
1211 dout("writeback %p\n", inode);
1212 filemap_fdatawrite(&inode->i_data);
1217 * Invalidate inode pages in a worker thread. (This can't be done
1218 * in the message handler context.)
1220 static void ceph_inode_invalidate_pages(struct work_struct *work)
1222 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1224 struct inode *inode = &ci->vfs_inode;
1228 spin_lock(&inode->i_lock);
1229 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1230 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1231 if (ci->i_rdcache_gen == 0 ||
1232 ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1233 BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
1235 ci->i_rdcache_revoking = 0;
1236 spin_unlock(&inode->i_lock);
1239 orig_gen = ci->i_rdcache_gen;
1240 spin_unlock(&inode->i_lock);
1242 truncate_inode_pages(&inode->i_data, 0);
1244 spin_lock(&inode->i_lock);
1245 if (orig_gen == ci->i_rdcache_gen) {
1246 dout("invalidate_pages %p gen %d successful\n", inode,
1248 ci->i_rdcache_gen = 0;
1249 ci->i_rdcache_revoking = 0;
1252 dout("invalidate_pages %p gen %d raced, gen now %d\n",
1253 inode, orig_gen, ci->i_rdcache_gen);
1255 spin_unlock(&inode->i_lock);
1258 ceph_check_caps(ci, 0, NULL);
1265 * called by trunc_wq; take i_mutex ourselves
1267 * We also truncate in a separate thread as well.
1269 void ceph_vmtruncate_work(struct work_struct *work)
1271 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1273 struct inode *inode = &ci->vfs_inode;
1275 dout("vmtruncate_work %p\n", inode);
1276 mutex_lock(&inode->i_mutex);
1277 __ceph_do_pending_vmtruncate(inode);
1278 mutex_unlock(&inode->i_mutex);
1283 * called with i_mutex held.
1285 * Make sure any pending truncation is applied before doing anything
1286 * that may depend on it.
1288 void __ceph_do_pending_vmtruncate(struct inode *inode)
1290 struct ceph_inode_info *ci = ceph_inode(inode);
1292 int wrbuffer_refs, wake = 0;
1295 spin_lock(&inode->i_lock);
1296 if (ci->i_truncate_pending == 0) {
1297 dout("__do_pending_vmtruncate %p none pending\n", inode);
1298 spin_unlock(&inode->i_lock);
1303 * make sure any dirty snapped pages are flushed before we
1304 * possibly truncate them.. so write AND block!
1306 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1307 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1309 spin_unlock(&inode->i_lock);
1310 filemap_write_and_wait_range(&inode->i_data, 0,
1311 inode->i_sb->s_maxbytes);
1315 to = ci->i_truncate_size;
1316 wrbuffer_refs = ci->i_wrbuffer_ref;
1317 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1318 ci->i_truncate_pending, to);
1319 spin_unlock(&inode->i_lock);
1321 truncate_inode_pages(inode->i_mapping, to);
1323 spin_lock(&inode->i_lock);
1324 ci->i_truncate_pending--;
1325 if (ci->i_truncate_pending == 0)
1327 spin_unlock(&inode->i_lock);
1329 if (wrbuffer_refs == 0)
1330 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1332 wake_up(&ci->i_cap_wq);
1339 static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1341 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1342 nd_set_link(nd, ci->i_symlink);
1346 static const struct inode_operations ceph_symlink_iops = {
1347 .readlink = generic_readlink,
1348 .follow_link = ceph_sym_follow_link,
1354 int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1356 struct inode *inode = dentry->d_inode;
1357 struct ceph_inode_info *ci = ceph_inode(inode);
1358 struct inode *parent_inode = dentry->d_parent->d_inode;
1359 const unsigned int ia_valid = attr->ia_valid;
1360 struct ceph_mds_request *req;
1361 struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
1363 int release = 0, dirtied = 0;
1366 int queue_trunc = 0;
1368 if (ceph_snap(inode) != CEPH_NOSNAP)
1371 __ceph_do_pending_vmtruncate(inode);
1373 err = inode_change_ok(inode, attr);
1377 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1380 return PTR_ERR(req);
1382 spin_lock(&inode->i_lock);
1383 issued = __ceph_caps_issued(ci, NULL);
1384 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1386 if (ia_valid & ATTR_UID) {
1387 dout("setattr %p uid %d -> %d\n", inode,
1388 inode->i_uid, attr->ia_uid);
1389 if (issued & CEPH_CAP_AUTH_EXCL) {
1390 inode->i_uid = attr->ia_uid;
1391 dirtied |= CEPH_CAP_AUTH_EXCL;
1392 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1393 attr->ia_uid != inode->i_uid) {
1394 req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1395 mask |= CEPH_SETATTR_UID;
1396 release |= CEPH_CAP_AUTH_SHARED;
1399 if (ia_valid & ATTR_GID) {
1400 dout("setattr %p gid %d -> %d\n", inode,
1401 inode->i_gid, attr->ia_gid);
1402 if (issued & CEPH_CAP_AUTH_EXCL) {
1403 inode->i_gid = attr->ia_gid;
1404 dirtied |= CEPH_CAP_AUTH_EXCL;
1405 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1406 attr->ia_gid != inode->i_gid) {
1407 req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1408 mask |= CEPH_SETATTR_GID;
1409 release |= CEPH_CAP_AUTH_SHARED;
1412 if (ia_valid & ATTR_MODE) {
1413 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1415 if (issued & CEPH_CAP_AUTH_EXCL) {
1416 inode->i_mode = attr->ia_mode;
1417 dirtied |= CEPH_CAP_AUTH_EXCL;
1418 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1419 attr->ia_mode != inode->i_mode) {
1420 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1421 mask |= CEPH_SETATTR_MODE;
1422 release |= CEPH_CAP_AUTH_SHARED;
1426 if (ia_valid & ATTR_ATIME) {
1427 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1428 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1429 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1430 if (issued & CEPH_CAP_FILE_EXCL) {
1431 ci->i_time_warp_seq++;
1432 inode->i_atime = attr->ia_atime;
1433 dirtied |= CEPH_CAP_FILE_EXCL;
1434 } else if ((issued & CEPH_CAP_FILE_WR) &&
1435 timespec_compare(&inode->i_atime,
1436 &attr->ia_atime) < 0) {
1437 inode->i_atime = attr->ia_atime;
1438 dirtied |= CEPH_CAP_FILE_WR;
1439 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1440 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1441 ceph_encode_timespec(&req->r_args.setattr.atime,
1443 mask |= CEPH_SETATTR_ATIME;
1444 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1448 if (ia_valid & ATTR_MTIME) {
1449 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1450 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1451 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1452 if (issued & CEPH_CAP_FILE_EXCL) {
1453 ci->i_time_warp_seq++;
1454 inode->i_mtime = attr->ia_mtime;
1455 dirtied |= CEPH_CAP_FILE_EXCL;
1456 } else if ((issued & CEPH_CAP_FILE_WR) &&
1457 timespec_compare(&inode->i_mtime,
1458 &attr->ia_mtime) < 0) {
1459 inode->i_mtime = attr->ia_mtime;
1460 dirtied |= CEPH_CAP_FILE_WR;
1461 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1462 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1463 ceph_encode_timespec(&req->r_args.setattr.mtime,
1465 mask |= CEPH_SETATTR_MTIME;
1466 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1470 if (ia_valid & ATTR_SIZE) {
1471 dout("setattr %p size %lld -> %lld\n", inode,
1472 inode->i_size, attr->ia_size);
1473 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1477 if ((issued & CEPH_CAP_FILE_EXCL) &&
1478 attr->ia_size > inode->i_size) {
1479 inode->i_size = attr->ia_size;
1480 if (attr->ia_size < inode->i_size) {
1481 ci->i_truncate_size = attr->ia_size;
1482 ci->i_truncate_pending++;
1486 (attr->ia_size + (1 << 9) - 1) >> 9;
1487 inode->i_ctime = attr->ia_ctime;
1488 ci->i_reported_size = attr->ia_size;
1489 dirtied |= CEPH_CAP_FILE_EXCL;
1490 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1491 attr->ia_size != inode->i_size) {
1492 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1493 req->r_args.setattr.old_size =
1494 cpu_to_le64(inode->i_size);
1495 mask |= CEPH_SETATTR_SIZE;
1496 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1501 /* these do nothing */
1502 if (ia_valid & ATTR_CTIME) {
1503 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1504 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1505 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1506 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1507 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1508 only ? "ctime only" : "ignored");
1509 inode->i_ctime = attr->ia_ctime;
1512 * if kernel wants to dirty ctime but nothing else,
1513 * we need to choose a cap to dirty under, or do
1514 * a almost-no-op setattr
1516 if (issued & CEPH_CAP_AUTH_EXCL)
1517 dirtied |= CEPH_CAP_AUTH_EXCL;
1518 else if (issued & CEPH_CAP_FILE_EXCL)
1519 dirtied |= CEPH_CAP_FILE_EXCL;
1520 else if (issued & CEPH_CAP_XATTR_EXCL)
1521 dirtied |= CEPH_CAP_XATTR_EXCL;
1523 mask |= CEPH_SETATTR_CTIME;
1526 if (ia_valid & ATTR_FILE)
1527 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1530 __ceph_mark_dirty_caps(ci, dirtied);
1531 inode->i_ctime = CURRENT_TIME;
1535 spin_unlock(&inode->i_lock);
1538 __ceph_do_pending_vmtruncate(inode);
1541 req->r_inode = igrab(inode);
1542 req->r_inode_drop = release;
1543 req->r_args.setattr.mask = cpu_to_le32(mask);
1544 req->r_num_caps = 1;
1545 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1547 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1548 ceph_cap_string(dirtied), mask);
1550 ceph_mdsc_put_request(req);
1551 __ceph_do_pending_vmtruncate(inode);
1554 spin_unlock(&inode->i_lock);
1555 ceph_mdsc_put_request(req);
1560 * Verify that we have a lease on the given mask. If not,
1561 * do a getattr against an mds.
1563 int ceph_do_getattr(struct inode *inode, int mask)
1565 struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
1566 struct ceph_mds_client *mdsc = &client->mdsc;
1567 struct ceph_mds_request *req;
1570 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1571 dout("do_getattr inode %p SNAPDIR\n", inode);
1575 dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
1576 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1579 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1581 return PTR_ERR(req);
1582 req->r_inode = igrab(inode);
1583 req->r_num_caps = 1;
1584 req->r_args.getattr.mask = cpu_to_le32(mask);
1585 err = ceph_mdsc_do_request(mdsc, NULL, req);
1586 ceph_mdsc_put_request(req);
1587 dout("do_getattr result=%d\n", err);
1593 * Check inode permissions. We verify we have a valid value for
1594 * the AUTH cap, then call the generic handler.
1596 int ceph_permission(struct inode *inode, int mask)
1598 int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1601 err = generic_permission(inode, mask, NULL);
1606 * Get all attributes. Hopefully somedata we'll have a statlite()
1607 * and can limit the fields we require to be accurate.
1609 int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1612 struct inode *inode = dentry->d_inode;
1613 struct ceph_inode_info *ci = ceph_inode(inode);
1616 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1618 generic_fillattr(inode, stat);
1619 stat->ino = inode->i_ino;
1620 if (ceph_snap(inode) != CEPH_NOSNAP)
1621 stat->dev = ceph_snap(inode);
1624 if (S_ISDIR(inode->i_mode)) {
1625 stat->size = ci->i_rbytes;
1627 stat->blksize = 65536;