0001
0002 #include <linux/ceph/ceph_debug.h>
0003
0004 #include <linux/module.h>
0005 #include <linux/fs.h>
0006 #include <linux/slab.h>
0007 #include <linux/string.h>
0008 #include <linux/uaccess.h>
0009 #include <linux/kernel.h>
0010 #include <linux/writeback.h>
0011 #include <linux/vmalloc.h>
0012 #include <linux/xattr.h>
0013 #include <linux/posix_acl.h>
0014 #include <linux/random.h>
0015 #include <linux/sort.h>
0016 #include <linux/iversion.h>
0017
0018 #include "super.h"
0019 #include "mds_client.h"
0020 #include "cache.h"
0021 #include <linux/ceph/decode.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 static const struct inode_operations ceph_symlink_iops;
0036
0037 static void ceph_inode_work(struct work_struct *work);
0038
0039
0040
0041
0042 static int ceph_set_ino_cb(struct inode *inode, void *data)
0043 {
0044 struct ceph_inode_info *ci = ceph_inode(inode);
0045 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
0046
0047 ci->i_vino = *(struct ceph_vino *)data;
0048 inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
0049 inode_set_iversion_raw(inode, 0);
0050 percpu_counter_inc(&mdsc->metric.total_inodes);
0051
0052 return 0;
0053 }
0054
0055 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
0056 {
0057 struct inode *inode;
0058
0059 if (ceph_vino_is_reserved(vino))
0060 return ERR_PTR(-EREMOTEIO);
0061
0062 inode = iget5_locked(sb, (unsigned long)vino.ino, ceph_ino_compare,
0063 ceph_set_ino_cb, &vino);
0064 if (!inode)
0065 return ERR_PTR(-ENOMEM);
0066
0067 dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode),
0068 ceph_vinop(inode), inode, !!(inode->i_state & I_NEW));
0069 return inode;
0070 }
0071
0072
0073
0074
0075 struct inode *ceph_get_snapdir(struct inode *parent)
0076 {
0077 struct ceph_vino vino = {
0078 .ino = ceph_ino(parent),
0079 .snap = CEPH_SNAPDIR,
0080 };
0081 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
0082 struct ceph_inode_info *ci = ceph_inode(inode);
0083
0084 if (IS_ERR(inode))
0085 return inode;
0086
0087 if (!S_ISDIR(parent->i_mode)) {
0088 pr_warn_once("bad snapdir parent type (mode=0%o)\n",
0089 parent->i_mode);
0090 goto err;
0091 }
0092
0093 if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
0094 pr_warn_once("bad snapdir inode type (mode=0%o)\n",
0095 inode->i_mode);
0096 goto err;
0097 }
0098
0099 inode->i_mode = parent->i_mode;
0100 inode->i_uid = parent->i_uid;
0101 inode->i_gid = parent->i_gid;
0102 inode->i_mtime = parent->i_mtime;
0103 inode->i_ctime = parent->i_ctime;
0104 inode->i_atime = parent->i_atime;
0105 ci->i_rbytes = 0;
0106 ci->i_btime = ceph_inode(parent)->i_btime;
0107
0108 if (inode->i_state & I_NEW) {
0109 inode->i_op = &ceph_snapdir_iops;
0110 inode->i_fop = &ceph_snapdir_fops;
0111 ci->i_snap_caps = CEPH_CAP_PIN;
0112 unlock_new_inode(inode);
0113 }
0114
0115 return inode;
0116 err:
0117 if ((inode->i_state & I_NEW))
0118 discard_new_inode(inode);
0119 else
0120 iput(inode);
0121 return ERR_PTR(-ENOTDIR);
0122 }
0123
0124 const struct inode_operations ceph_file_iops = {
0125 .permission = ceph_permission,
0126 .setattr = ceph_setattr,
0127 .getattr = ceph_getattr,
0128 .listxattr = ceph_listxattr,
0129 .get_acl = ceph_get_acl,
0130 .set_acl = ceph_set_acl,
0131 };
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
0146 u32 f)
0147 {
0148 struct rb_node **p;
0149 struct rb_node *parent = NULL;
0150 struct ceph_inode_frag *frag;
0151 int c;
0152
0153 p = &ci->i_fragtree.rb_node;
0154 while (*p) {
0155 parent = *p;
0156 frag = rb_entry(parent, struct ceph_inode_frag, node);
0157 c = ceph_frag_compare(f, frag->frag);
0158 if (c < 0)
0159 p = &(*p)->rb_left;
0160 else if (c > 0)
0161 p = &(*p)->rb_right;
0162 else
0163 return frag;
0164 }
0165
0166 frag = kmalloc(sizeof(*frag), GFP_NOFS);
0167 if (!frag)
0168 return ERR_PTR(-ENOMEM);
0169
0170 frag->frag = f;
0171 frag->split_by = 0;
0172 frag->mds = -1;
0173 frag->ndist = 0;
0174
0175 rb_link_node(&frag->node, parent, p);
0176 rb_insert_color(&frag->node, &ci->i_fragtree);
0177
0178 dout("get_or_create_frag added %llx.%llx frag %x\n",
0179 ceph_vinop(&ci->netfs.inode), f);
0180 return frag;
0181 }
0182
0183
0184
0185
0186 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
0187 {
0188 struct rb_node *n = ci->i_fragtree.rb_node;
0189
0190 while (n) {
0191 struct ceph_inode_frag *frag =
0192 rb_entry(n, struct ceph_inode_frag, node);
0193 int c = ceph_frag_compare(f, frag->frag);
0194 if (c < 0)
0195 n = n->rb_left;
0196 else if (c > 0)
0197 n = n->rb_right;
0198 else
0199 return frag;
0200 }
0201 return NULL;
0202 }
0203
0204
0205
0206
0207
0208
0209 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
0210 struct ceph_inode_frag *pfrag, int *found)
0211 {
0212 u32 t = ceph_frag_make(0, 0);
0213 struct ceph_inode_frag *frag;
0214 unsigned nway, i;
0215 u32 n;
0216
0217 if (found)
0218 *found = 0;
0219
0220 while (1) {
0221 WARN_ON(!ceph_frag_contains_value(t, v));
0222 frag = __ceph_find_frag(ci, t);
0223 if (!frag)
0224 break;
0225 if (frag->split_by == 0) {
0226 if (pfrag)
0227 memcpy(pfrag, frag, sizeof(*pfrag));
0228 if (found)
0229 *found = 1;
0230 break;
0231 }
0232
0233
0234 nway = 1 << frag->split_by;
0235 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
0236 frag->split_by, nway);
0237 for (i = 0; i < nway; i++) {
0238 n = ceph_frag_make_child(t, frag->split_by, i);
0239 if (ceph_frag_contains_value(n, v)) {
0240 t = n;
0241 break;
0242 }
0243 }
0244 BUG_ON(i == nway);
0245 }
0246 dout("choose_frag(%x) = %x\n", v, t);
0247
0248 return t;
0249 }
0250
0251 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
0252 struct ceph_inode_frag *pfrag, int *found)
0253 {
0254 u32 ret;
0255 mutex_lock(&ci->i_fragtree_mutex);
0256 ret = __ceph_choose_frag(ci, v, pfrag, found);
0257 mutex_unlock(&ci->i_fragtree_mutex);
0258 return ret;
0259 }
0260
0261
0262
0263
0264
0265
0266 static int ceph_fill_dirfrag(struct inode *inode,
0267 struct ceph_mds_reply_dirfrag *dirinfo)
0268 {
0269 struct ceph_inode_info *ci = ceph_inode(inode);
0270 struct ceph_inode_frag *frag;
0271 u32 id = le32_to_cpu(dirinfo->frag);
0272 int mds = le32_to_cpu(dirinfo->auth);
0273 int ndist = le32_to_cpu(dirinfo->ndist);
0274 int diri_auth = -1;
0275 int i;
0276 int err = 0;
0277
0278 spin_lock(&ci->i_ceph_lock);
0279 if (ci->i_auth_cap)
0280 diri_auth = ci->i_auth_cap->mds;
0281 spin_unlock(&ci->i_ceph_lock);
0282
0283 if (mds == -1)
0284 mds = diri_auth;
0285
0286 mutex_lock(&ci->i_fragtree_mutex);
0287 if (ndist == 0 && mds == diri_auth) {
0288
0289 frag = __ceph_find_frag(ci, id);
0290 if (!frag)
0291 goto out;
0292 if (frag->split_by == 0) {
0293
0294 dout("fill_dirfrag removed %llx.%llx frag %x"
0295 " (no ref)\n", ceph_vinop(inode), id);
0296 rb_erase(&frag->node, &ci->i_fragtree);
0297 kfree(frag);
0298 } else {
0299
0300 dout("fill_dirfrag cleared %llx.%llx frag %x"
0301 " referral\n", ceph_vinop(inode), id);
0302 frag->mds = -1;
0303 frag->ndist = 0;
0304 }
0305 goto out;
0306 }
0307
0308
0309
0310 frag = __get_or_create_frag(ci, id);
0311 if (IS_ERR(frag)) {
0312
0313
0314 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
0315 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
0316 err = -ENOMEM;
0317 goto out;
0318 }
0319
0320 frag->mds = mds;
0321 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
0322 for (i = 0; i < frag->ndist; i++)
0323 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
0324 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
0325 ceph_vinop(inode), frag->frag, frag->ndist);
0326
0327 out:
0328 mutex_unlock(&ci->i_fragtree_mutex);
0329 return err;
0330 }
0331
0332 static int frag_tree_split_cmp(const void *l, const void *r)
0333 {
0334 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
0335 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
0336 return ceph_frag_compare(le32_to_cpu(ls->frag),
0337 le32_to_cpu(rs->frag));
0338 }
0339
0340 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
0341 {
0342 if (!frag)
0343 return f == ceph_frag_make(0, 0);
0344 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
0345 return false;
0346 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
0347 }
0348
0349 static int ceph_fill_fragtree(struct inode *inode,
0350 struct ceph_frag_tree_head *fragtree,
0351 struct ceph_mds_reply_dirfrag *dirinfo)
0352 {
0353 struct ceph_inode_info *ci = ceph_inode(inode);
0354 struct ceph_inode_frag *frag, *prev_frag = NULL;
0355 struct rb_node *rb_node;
0356 unsigned i, split_by, nsplits;
0357 u32 id;
0358 bool update = false;
0359
0360 mutex_lock(&ci->i_fragtree_mutex);
0361 nsplits = le32_to_cpu(fragtree->nsplits);
0362 if (nsplits != ci->i_fragtree_nsplits) {
0363 update = true;
0364 } else if (nsplits) {
0365 i = prandom_u32() % nsplits;
0366 id = le32_to_cpu(fragtree->splits[i].frag);
0367 if (!__ceph_find_frag(ci, id))
0368 update = true;
0369 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
0370 rb_node = rb_first(&ci->i_fragtree);
0371 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
0372 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
0373 update = true;
0374 }
0375 if (!update && dirinfo) {
0376 id = le32_to_cpu(dirinfo->frag);
0377 if (id != __ceph_choose_frag(ci, id, NULL, NULL))
0378 update = true;
0379 }
0380 if (!update)
0381 goto out_unlock;
0382
0383 if (nsplits > 1) {
0384 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
0385 frag_tree_split_cmp, NULL);
0386 }
0387
0388 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
0389 rb_node = rb_first(&ci->i_fragtree);
0390 for (i = 0; i < nsplits; i++) {
0391 id = le32_to_cpu(fragtree->splits[i].frag);
0392 split_by = le32_to_cpu(fragtree->splits[i].by);
0393 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
0394 pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
0395 "frag %x split by %d\n", ceph_vinop(inode),
0396 i, nsplits, id, split_by);
0397 continue;
0398 }
0399 frag = NULL;
0400 while (rb_node) {
0401 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
0402 if (ceph_frag_compare(frag->frag, id) >= 0) {
0403 if (frag->frag != id)
0404 frag = NULL;
0405 else
0406 rb_node = rb_next(rb_node);
0407 break;
0408 }
0409 rb_node = rb_next(rb_node);
0410
0411 if (frag->split_by > 0 ||
0412 !is_frag_child(frag->frag, prev_frag)) {
0413 rb_erase(&frag->node, &ci->i_fragtree);
0414 if (frag->split_by > 0)
0415 ci->i_fragtree_nsplits--;
0416 kfree(frag);
0417 }
0418 frag = NULL;
0419 }
0420 if (!frag) {
0421 frag = __get_or_create_frag(ci, id);
0422 if (IS_ERR(frag))
0423 continue;
0424 }
0425 if (frag->split_by == 0)
0426 ci->i_fragtree_nsplits++;
0427 frag->split_by = split_by;
0428 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
0429 prev_frag = frag;
0430 }
0431 while (rb_node) {
0432 frag = rb_entry(rb_node, struct ceph_inode_frag, node);
0433 rb_node = rb_next(rb_node);
0434
0435 if (frag->split_by > 0 ||
0436 !is_frag_child(frag->frag, prev_frag)) {
0437 rb_erase(&frag->node, &ci->i_fragtree);
0438 if (frag->split_by > 0)
0439 ci->i_fragtree_nsplits--;
0440 kfree(frag);
0441 }
0442 }
0443 out_unlock:
0444 mutex_unlock(&ci->i_fragtree_mutex);
0445 return 0;
0446 }
0447
0448
0449
0450
0451 struct inode *ceph_alloc_inode(struct super_block *sb)
0452 {
0453 struct ceph_inode_info *ci;
0454 int i;
0455
0456 ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
0457 if (!ci)
0458 return NULL;
0459
0460 dout("alloc_inode %p\n", &ci->netfs.inode);
0461
0462
0463 netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
0464
0465 spin_lock_init(&ci->i_ceph_lock);
0466
0467 ci->i_version = 0;
0468 ci->i_inline_version = 0;
0469 ci->i_time_warp_seq = 0;
0470 ci->i_ceph_flags = 0;
0471 atomic64_set(&ci->i_ordered_count, 1);
0472 atomic64_set(&ci->i_release_count, 1);
0473 atomic64_set(&ci->i_complete_seq[0], 0);
0474 atomic64_set(&ci->i_complete_seq[1], 0);
0475 ci->i_symlink = NULL;
0476
0477 ci->i_max_bytes = 0;
0478 ci->i_max_files = 0;
0479
0480 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
0481 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
0482 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
0483
0484 ci->i_fragtree = RB_ROOT;
0485 mutex_init(&ci->i_fragtree_mutex);
0486
0487 ci->i_xattrs.blob = NULL;
0488 ci->i_xattrs.prealloc_blob = NULL;
0489 ci->i_xattrs.dirty = false;
0490 ci->i_xattrs.index = RB_ROOT;
0491 ci->i_xattrs.count = 0;
0492 ci->i_xattrs.names_size = 0;
0493 ci->i_xattrs.vals_size = 0;
0494 ci->i_xattrs.version = 0;
0495 ci->i_xattrs.index_version = 0;
0496
0497 ci->i_caps = RB_ROOT;
0498 ci->i_auth_cap = NULL;
0499 ci->i_dirty_caps = 0;
0500 ci->i_flushing_caps = 0;
0501 INIT_LIST_HEAD(&ci->i_dirty_item);
0502 INIT_LIST_HEAD(&ci->i_flushing_item);
0503 ci->i_prealloc_cap_flush = NULL;
0504 INIT_LIST_HEAD(&ci->i_cap_flush_list);
0505 init_waitqueue_head(&ci->i_cap_wq);
0506 ci->i_hold_caps_max = 0;
0507 INIT_LIST_HEAD(&ci->i_cap_delay_list);
0508 INIT_LIST_HEAD(&ci->i_cap_snaps);
0509 ci->i_head_snapc = NULL;
0510 ci->i_snap_caps = 0;
0511
0512 ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
0513 for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
0514 ci->i_nr_by_mode[i] = 0;
0515
0516 mutex_init(&ci->i_truncate_mutex);
0517 ci->i_truncate_seq = 0;
0518 ci->i_truncate_size = 0;
0519 ci->i_truncate_pending = 0;
0520
0521 ci->i_max_size = 0;
0522 ci->i_reported_size = 0;
0523 ci->i_wanted_max_size = 0;
0524 ci->i_requested_max_size = 0;
0525
0526 ci->i_pin_ref = 0;
0527 ci->i_rd_ref = 0;
0528 ci->i_rdcache_ref = 0;
0529 ci->i_wr_ref = 0;
0530 ci->i_wb_ref = 0;
0531 ci->i_fx_ref = 0;
0532 ci->i_wrbuffer_ref = 0;
0533 ci->i_wrbuffer_ref_head = 0;
0534 atomic_set(&ci->i_filelock_ref, 0);
0535 atomic_set(&ci->i_shared_gen, 1);
0536 ci->i_rdcache_gen = 0;
0537 ci->i_rdcache_revoking = 0;
0538
0539 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
0540 INIT_LIST_HEAD(&ci->i_unsafe_iops);
0541 spin_lock_init(&ci->i_unsafe_lock);
0542
0543 ci->i_snap_realm = NULL;
0544 INIT_LIST_HEAD(&ci->i_snap_realm_item);
0545 INIT_LIST_HEAD(&ci->i_snap_flush_item);
0546
0547 INIT_WORK(&ci->i_work, ceph_inode_work);
0548 ci->i_work_mask = 0;
0549 memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
0550 return &ci->netfs.inode;
0551 }
0552
0553 void ceph_free_inode(struct inode *inode)
0554 {
0555 struct ceph_inode_info *ci = ceph_inode(inode);
0556
0557 kfree(ci->i_symlink);
0558 kmem_cache_free(ceph_inode_cachep, ci);
0559 }
0560
0561 void ceph_evict_inode(struct inode *inode)
0562 {
0563 struct ceph_inode_info *ci = ceph_inode(inode);
0564 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
0565 struct ceph_inode_frag *frag;
0566 struct rb_node *n;
0567
0568 dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
0569
0570 percpu_counter_dec(&mdsc->metric.total_inodes);
0571
0572 truncate_inode_pages_final(&inode->i_data);
0573 if (inode->i_state & I_PINNING_FSCACHE_WB)
0574 ceph_fscache_unuse_cookie(inode, true);
0575 clear_inode(inode);
0576
0577 ceph_fscache_unregister_inode_cookie(ci);
0578
0579 __ceph_remove_caps(ci);
0580
0581 if (__ceph_has_quota(ci, QUOTA_GET_ANY))
0582 ceph_adjust_quota_realms_count(inode, false);
0583
0584
0585
0586
0587
0588 if (ci->i_snap_realm) {
0589 if (ceph_snap(inode) == CEPH_NOSNAP) {
0590 dout(" dropping residual ref to snap realm %p\n",
0591 ci->i_snap_realm);
0592 ceph_change_snap_realm(inode, NULL);
0593 } else {
0594 ceph_put_snapid_map(mdsc, ci->i_snapid_map);
0595 ci->i_snap_realm = NULL;
0596 }
0597 }
0598
0599 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
0600 frag = rb_entry(n, struct ceph_inode_frag, node);
0601 rb_erase(n, &ci->i_fragtree);
0602 kfree(frag);
0603 }
0604 ci->i_fragtree_nsplits = 0;
0605
0606 __ceph_destroy_xattrs(ci);
0607 if (ci->i_xattrs.blob)
0608 ceph_buffer_put(ci->i_xattrs.blob);
0609 if (ci->i_xattrs.prealloc_blob)
0610 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
0611
0612 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
0613 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
0614 }
0615
0616 static inline blkcnt_t calc_inode_blocks(u64 size)
0617 {
0618 return (size + (1<<9) - 1) >> 9;
0619 }
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629 int ceph_fill_file_size(struct inode *inode, int issued,
0630 u32 truncate_seq, u64 truncate_size, u64 size)
0631 {
0632 struct ceph_inode_info *ci = ceph_inode(inode);
0633 int queue_trunc = 0;
0634 loff_t isize = i_size_read(inode);
0635
0636 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
0637 (truncate_seq == ci->i_truncate_seq && size > isize)) {
0638 dout("size %lld -> %llu\n", isize, size);
0639 if (size > 0 && S_ISDIR(inode->i_mode)) {
0640 pr_err("fill_file_size non-zero size for directory\n");
0641 size = 0;
0642 }
0643 i_size_write(inode, size);
0644 inode->i_blocks = calc_inode_blocks(size);
0645
0646
0647
0648
0649 if (size > isize)
0650 ceph_fscache_update(inode);
0651 ci->i_reported_size = size;
0652 if (truncate_seq != ci->i_truncate_seq) {
0653 dout("truncate_seq %u -> %u\n",
0654 ci->i_truncate_seq, truncate_seq);
0655 ci->i_truncate_seq = truncate_seq;
0656
0657
0658 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
0659 CEPH_CAP_FILE_RD |
0660 CEPH_CAP_FILE_WR |
0661 CEPH_CAP_FILE_LAZYIO));
0662
0663
0664
0665
0666
0667
0668 if ((issued & (CEPH_CAP_FILE_CACHE|
0669 CEPH_CAP_FILE_BUFFER)) ||
0670 mapping_mapped(inode->i_mapping) ||
0671 __ceph_is_file_opened(ci)) {
0672 ci->i_truncate_pending++;
0673 queue_trunc = 1;
0674 }
0675 }
0676 }
0677 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
0678 ci->i_truncate_size != truncate_size) {
0679 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
0680 truncate_size);
0681 ci->i_truncate_size = truncate_size;
0682 }
0683 return queue_trunc;
0684 }
0685
0686 void ceph_fill_file_time(struct inode *inode, int issued,
0687 u64 time_warp_seq, struct timespec64 *ctime,
0688 struct timespec64 *mtime, struct timespec64 *atime)
0689 {
0690 struct ceph_inode_info *ci = ceph_inode(inode);
0691 int warn = 0;
0692
0693 if (issued & (CEPH_CAP_FILE_EXCL|
0694 CEPH_CAP_FILE_WR|
0695 CEPH_CAP_FILE_BUFFER|
0696 CEPH_CAP_AUTH_EXCL|
0697 CEPH_CAP_XATTR_EXCL)) {
0698 if (ci->i_version == 0 ||
0699 timespec64_compare(ctime, &inode->i_ctime) > 0) {
0700 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
0701 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
0702 ctime->tv_sec, ctime->tv_nsec);
0703 inode->i_ctime = *ctime;
0704 }
0705 if (ci->i_version == 0 ||
0706 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
0707
0708 dout("mtime %lld.%09ld -> %lld.%09ld "
0709 "tw %d -> %d\n",
0710 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
0711 mtime->tv_sec, mtime->tv_nsec,
0712 ci->i_time_warp_seq, (int)time_warp_seq);
0713
0714 inode->i_mtime = *mtime;
0715 inode->i_atime = *atime;
0716 ci->i_time_warp_seq = time_warp_seq;
0717 } else if (time_warp_seq == ci->i_time_warp_seq) {
0718
0719 if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
0720 dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
0721 inode->i_mtime.tv_sec,
0722 inode->i_mtime.tv_nsec,
0723 mtime->tv_sec, mtime->tv_nsec);
0724 inode->i_mtime = *mtime;
0725 }
0726 if (timespec64_compare(atime, &inode->i_atime) > 0) {
0727 dout("atime %lld.%09ld -> %lld.%09ld inc\n",
0728 inode->i_atime.tv_sec,
0729 inode->i_atime.tv_nsec,
0730 atime->tv_sec, atime->tv_nsec);
0731 inode->i_atime = *atime;
0732 }
0733 } else if (issued & CEPH_CAP_FILE_EXCL) {
0734
0735 } else {
0736 warn = 1;
0737 }
0738 } else {
0739
0740 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
0741 inode->i_ctime = *ctime;
0742 inode->i_mtime = *mtime;
0743 inode->i_atime = *atime;
0744 ci->i_time_warp_seq = time_warp_seq;
0745 } else {
0746 warn = 1;
0747 }
0748 }
0749 if (warn)
0750 dout("%p mds time_warp_seq %llu < %u\n",
0751 inode, time_warp_seq, ci->i_time_warp_seq);
0752 }
0753
0754
0755
0756
0757
0758 int ceph_fill_inode(struct inode *inode, struct page *locked_page,
0759 struct ceph_mds_reply_info_in *iinfo,
0760 struct ceph_mds_reply_dirfrag *dirinfo,
0761 struct ceph_mds_session *session, int cap_fmode,
0762 struct ceph_cap_reservation *caps_reservation)
0763 {
0764 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
0765 struct ceph_mds_reply_inode *info = iinfo->in;
0766 struct ceph_inode_info *ci = ceph_inode(inode);
0767 int issued, new_issued, info_caps;
0768 struct timespec64 mtime, atime, ctime;
0769 struct ceph_buffer *xattr_blob = NULL;
0770 struct ceph_buffer *old_blob = NULL;
0771 struct ceph_string *pool_ns = NULL;
0772 struct ceph_cap *new_cap = NULL;
0773 int err = 0;
0774 bool wake = false;
0775 bool queue_trunc = false;
0776 bool new_version = false;
0777 bool fill_inline = false;
0778 umode_t mode = le32_to_cpu(info->mode);
0779 dev_t rdev = le32_to_cpu(info->rdev);
0780
0781 lockdep_assert_held(&mdsc->snap_rwsem);
0782
0783 dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
0784 inode, ceph_vinop(inode), le64_to_cpu(info->version),
0785 ci->i_version);
0786
0787
0788 if (inode->i_state & I_NEW) {
0789 inode->i_mode = mode;
0790 } else {
0791 if (inode_wrong_type(inode, mode)) {
0792 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
0793 ceph_vinop(inode), inode->i_mode, mode);
0794 return -ESTALE;
0795 }
0796
0797 if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
0798 pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
0799 ceph_vinop(inode), MAJOR(inode->i_rdev),
0800 MINOR(inode->i_rdev), MAJOR(rdev),
0801 MINOR(rdev));
0802 return -ESTALE;
0803 }
0804 }
0805
0806 info_caps = le32_to_cpu(info->cap.caps);
0807
0808
0809 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
0810 new_cap = ceph_get_cap(mdsc, caps_reservation);
0811 if (!new_cap)
0812 return -ENOMEM;
0813 }
0814
0815
0816
0817
0818
0819
0820 if (iinfo->xattr_len > 4) {
0821 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
0822 if (!xattr_blob)
0823 pr_err("%s ENOMEM xattr blob %d bytes\n", __func__,
0824 iinfo->xattr_len);
0825 }
0826
0827 if (iinfo->pool_ns_len > 0)
0828 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
0829 iinfo->pool_ns_len);
0830
0831 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
0832 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
0833
0834 spin_lock(&ci->i_ceph_lock);
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847 if (ci->i_version == 0 ||
0848 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
0849 le64_to_cpu(info->version) > (ci->i_version & ~1)))
0850 new_version = true;
0851
0852
0853 inode_set_max_iversion_raw(inode, iinfo->change_attr);
0854
0855 __ceph_caps_issued(ci, &issued);
0856 issued |= __ceph_caps_dirty(ci);
0857 new_issued = ~issued & info_caps;
0858
0859
0860 if (le32_to_cpu(info->layout.fl_stripe_unit))
0861 inode->i_blkbits =
0862 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
0863 else
0864 inode->i_blkbits = CEPH_BLOCK_SHIFT;
0865
0866 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
0867
0868 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
0869 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
0870 inode->i_mode = mode;
0871 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
0872 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
0873 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
0874 from_kuid(&init_user_ns, inode->i_uid),
0875 from_kgid(&init_user_ns, inode->i_gid));
0876 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
0877 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
0878 }
0879
0880 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
0881 (issued & CEPH_CAP_LINK_EXCL) == 0)
0882 set_nlink(inode, le32_to_cpu(info->nlink));
0883
0884 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
0885
0886 ceph_decode_timespec64(&atime, &info->atime);
0887 ceph_decode_timespec64(&mtime, &info->mtime);
0888 ceph_decode_timespec64(&ctime, &info->ctime);
0889 ceph_fill_file_time(inode, issued,
0890 le32_to_cpu(info->time_warp_seq),
0891 &ctime, &mtime, &atime);
0892 }
0893
0894 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
0895 ci->i_files = le64_to_cpu(info->files);
0896 ci->i_subdirs = le64_to_cpu(info->subdirs);
0897 }
0898
0899 if (new_version ||
0900 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
0901 s64 old_pool = ci->i_layout.pool_id;
0902 struct ceph_string *old_ns;
0903
0904 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
0905 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
0906 lockdep_is_held(&ci->i_ceph_lock));
0907 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
0908
0909 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
0910 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
0911
0912 pool_ns = old_ns;
0913
0914 queue_trunc = ceph_fill_file_size(inode, issued,
0915 le32_to_cpu(info->truncate_seq),
0916 le64_to_cpu(info->truncate_size),
0917 le64_to_cpu(info->size));
0918
0919 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
0920 ci->i_max_size != le64_to_cpu(info->max_size)) {
0921 dout("max_size %lld -> %llu\n", ci->i_max_size,
0922 le64_to_cpu(info->max_size));
0923 ci->i_max_size = le64_to_cpu(info->max_size);
0924 }
0925 }
0926
0927
0928
0929 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
0930 if (S_ISDIR(inode->i_mode)) {
0931 ci->i_dir_layout = iinfo->dir_layout;
0932 ci->i_rbytes = le64_to_cpu(info->rbytes);
0933 ci->i_rfiles = le64_to_cpu(info->rfiles);
0934 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
0935 ci->i_dir_pin = iinfo->dir_pin;
0936 ci->i_rsnaps = iinfo->rsnaps;
0937 ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
0938 }
0939 }
0940
0941
0942
0943 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
0944 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
0945 if (ci->i_xattrs.blob)
0946 old_blob = ci->i_xattrs.blob;
0947 ci->i_xattrs.blob = xattr_blob;
0948 if (xattr_blob)
0949 memcpy(ci->i_xattrs.blob->vec.iov_base,
0950 iinfo->xattr_data, iinfo->xattr_len);
0951 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
0952 ceph_forget_all_cached_acls(inode);
0953 ceph_security_invalidate_secctx(inode);
0954 xattr_blob = NULL;
0955 }
0956
0957
0958 if (le64_to_cpu(info->version) > ci->i_version)
0959 ci->i_version = le64_to_cpu(info->version);
0960
0961 inode->i_mapping->a_ops = &ceph_aops;
0962
0963 switch (inode->i_mode & S_IFMT) {
0964 case S_IFIFO:
0965 case S_IFBLK:
0966 case S_IFCHR:
0967 case S_IFSOCK:
0968 inode->i_blkbits = PAGE_SHIFT;
0969 init_special_inode(inode, inode->i_mode, rdev);
0970 inode->i_op = &ceph_file_iops;
0971 break;
0972 case S_IFREG:
0973 inode->i_op = &ceph_file_iops;
0974 inode->i_fop = &ceph_file_fops;
0975 break;
0976 case S_IFLNK:
0977 inode->i_op = &ceph_symlink_iops;
0978 if (!ci->i_symlink) {
0979 u32 symlen = iinfo->symlink_len;
0980 char *sym;
0981
0982 spin_unlock(&ci->i_ceph_lock);
0983
0984 if (symlen != i_size_read(inode)) {
0985 pr_err("%s %llx.%llx BAD symlink "
0986 "size %lld\n", __func__,
0987 ceph_vinop(inode),
0988 i_size_read(inode));
0989 i_size_write(inode, symlen);
0990 inode->i_blocks = calc_inode_blocks(symlen);
0991 }
0992
0993 err = -ENOMEM;
0994 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
0995 if (!sym)
0996 goto out;
0997
0998 spin_lock(&ci->i_ceph_lock);
0999 if (!ci->i_symlink)
1000 ci->i_symlink = sym;
1001 else
1002 kfree(sym);
1003 }
1004 inode->i_link = ci->i_symlink;
1005 break;
1006 case S_IFDIR:
1007 inode->i_op = &ceph_dir_iops;
1008 inode->i_fop = &ceph_dir_fops;
1009 break;
1010 default:
1011 pr_err("%s %llx.%llx BAD mode 0%o\n", __func__,
1012 ceph_vinop(inode), inode->i_mode);
1013 }
1014
1015
1016 if (info_caps) {
1017 if (ceph_snap(inode) == CEPH_NOSNAP) {
1018 ceph_add_cap(inode, session,
1019 le64_to_cpu(info->cap.cap_id),
1020 info_caps,
1021 le32_to_cpu(info->cap.wanted),
1022 le32_to_cpu(info->cap.seq),
1023 le32_to_cpu(info->cap.mseq),
1024 le64_to_cpu(info->cap.realm),
1025 info->cap.flags, &new_cap);
1026
1027
1028 if (S_ISDIR(inode->i_mode) &&
1029 ci->i_files == 0 && ci->i_subdirs == 0 &&
1030 (info_caps & CEPH_CAP_FILE_SHARED) &&
1031 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1032 !__ceph_dir_is_complete(ci)) {
1033 dout(" marking %p complete (empty)\n", inode);
1034 i_size_write(inode, 0);
1035 __ceph_dir_set_complete(ci,
1036 atomic64_read(&ci->i_release_count),
1037 atomic64_read(&ci->i_ordered_count));
1038 }
1039
1040 wake = true;
1041 } else {
1042 dout(" %p got snap_caps %s\n", inode,
1043 ceph_cap_string(info_caps));
1044 ci->i_snap_caps |= info_caps;
1045 }
1046 }
1047
1048 if (iinfo->inline_version > 0 &&
1049 iinfo->inline_version >= ci->i_inline_version) {
1050 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1051 ci->i_inline_version = iinfo->inline_version;
1052 if (ceph_has_inline_data(ci) &&
1053 (locked_page || (info_caps & cache_caps)))
1054 fill_inline = true;
1055 }
1056
1057 if (cap_fmode >= 0) {
1058 if (!info_caps)
1059 pr_warn("mds issued no caps on %llx.%llx\n",
1060 ceph_vinop(inode));
1061 __ceph_touch_fmode(ci, mdsc, cap_fmode);
1062 }
1063
1064 spin_unlock(&ci->i_ceph_lock);
1065
1066 ceph_fscache_register_inode_cookie(inode);
1067
1068 if (fill_inline)
1069 ceph_fill_inline_data(inode, locked_page,
1070 iinfo->inline_data, iinfo->inline_len);
1071
1072 if (wake)
1073 wake_up_all(&ci->i_cap_wq);
1074
1075
1076 if (queue_trunc)
1077 ceph_queue_vmtruncate(inode);
1078
1079
1080 if (S_ISDIR(inode->i_mode))
1081 ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1082
1083
1084 if (dirinfo)
1085 ceph_fill_dirfrag(inode, dirinfo);
1086
1087 err = 0;
1088 out:
1089 if (new_cap)
1090 ceph_put_cap(mdsc, new_cap);
1091 ceph_buffer_put(old_blob);
1092 ceph_buffer_put(xattr_blob);
1093 ceph_put_string(pool_ns);
1094 return err;
1095 }
1096
1097
1098
1099
1100 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1101 struct ceph_mds_reply_lease *lease,
1102 struct ceph_mds_session *session,
1103 unsigned long from_time,
1104 struct ceph_mds_session **old_lease_session)
1105 {
1106 struct ceph_dentry_info *di = ceph_dentry(dentry);
1107 unsigned mask = le16_to_cpu(lease->mask);
1108 long unsigned duration = le32_to_cpu(lease->duration_ms);
1109 long unsigned ttl = from_time + (duration * HZ) / 1000;
1110 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1111
1112 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
1113 dentry, duration, ttl);
1114
1115
1116 if (ceph_snap(dir) != CEPH_NOSNAP)
1117 return;
1118
1119 if (mask & CEPH_LEASE_PRIMARY_LINK)
1120 di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1121 else
1122 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1123
1124 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1125 if (!(mask & CEPH_LEASE_VALID)) {
1126 __ceph_dentry_dir_lease_touch(di);
1127 return;
1128 }
1129
1130 if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
1131 time_before(ttl, di->time))
1132 return;
1133
1134 if (di->lease_session && di->lease_session != session) {
1135 *old_lease_session = di->lease_session;
1136 di->lease_session = NULL;
1137 }
1138
1139 if (!di->lease_session)
1140 di->lease_session = ceph_get_mds_session(session);
1141 di->lease_gen = atomic_read(&session->s_cap_gen);
1142 di->lease_seq = le32_to_cpu(lease->seq);
1143 di->lease_renew_after = half_ttl;
1144 di->lease_renew_from = 0;
1145 di->time = ttl;
1146
1147 __ceph_dentry_lease_touch(di);
1148 }
1149
1150 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1151 struct ceph_mds_reply_lease *lease,
1152 struct ceph_mds_session *session,
1153 unsigned long from_time)
1154 {
1155 struct ceph_mds_session *old_lease_session = NULL;
1156 spin_lock(&dentry->d_lock);
1157 __update_dentry_lease(dir, dentry, lease, session, from_time,
1158 &old_lease_session);
1159 spin_unlock(&dentry->d_lock);
1160 ceph_put_mds_session(old_lease_session);
1161 }
1162
1163
1164
1165
1166 static void update_dentry_lease_careful(struct dentry *dentry,
1167 struct ceph_mds_reply_lease *lease,
1168 struct ceph_mds_session *session,
1169 unsigned long from_time,
1170 char *dname, u32 dname_len,
1171 struct ceph_vino *pdvino,
1172 struct ceph_vino *ptvino)
1173
1174 {
1175 struct inode *dir;
1176 struct ceph_mds_session *old_lease_session = NULL;
1177
1178 spin_lock(&dentry->d_lock);
1179
1180 if (dentry->d_name.len != dname_len ||
1181 memcmp(dentry->d_name.name, dname, dname_len))
1182 goto out_unlock;
1183
1184 dir = d_inode(dentry->d_parent);
1185
1186 if (!ceph_ino_compare(dir, pdvino))
1187 goto out_unlock;
1188
1189
1190
1191 if (ptvino) {
1192 if (d_really_is_negative(dentry))
1193 goto out_unlock;
1194 if (!ceph_ino_compare(d_inode(dentry), ptvino))
1195 goto out_unlock;
1196 } else {
1197 if (d_really_is_positive(dentry))
1198 goto out_unlock;
1199 }
1200
1201 __update_dentry_lease(dir, dentry, lease, session,
1202 from_time, &old_lease_session);
1203 out_unlock:
1204 spin_unlock(&dentry->d_lock);
1205 ceph_put_mds_session(old_lease_session);
1206 }
1207
1208
1209
1210
1211
1212 static int splice_dentry(struct dentry **pdn, struct inode *in)
1213 {
1214 struct dentry *dn = *pdn;
1215 struct dentry *realdn;
1216
1217 BUG_ON(d_inode(dn));
1218
1219 if (S_ISDIR(in->i_mode)) {
1220
1221
1222
1223
1224 realdn = d_find_any_alias(in);
1225 if (realdn) {
1226 struct ceph_dentry_info *di = ceph_dentry(realdn);
1227 spin_lock(&realdn->d_lock);
1228
1229 realdn->d_op->d_prune(realdn);
1230
1231 di->time = jiffies;
1232 di->lease_shared_gen = 0;
1233 di->offset = 0;
1234
1235 spin_unlock(&realdn->d_lock);
1236 dput(realdn);
1237 }
1238 }
1239
1240
1241 if (!d_unhashed(dn))
1242 d_drop(dn);
1243 realdn = d_splice_alias(in, dn);
1244 if (IS_ERR(realdn)) {
1245 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1246 PTR_ERR(realdn), dn, in, ceph_vinop(in));
1247 return PTR_ERR(realdn);
1248 }
1249
1250 if (realdn) {
1251 dout("dn %p (%d) spliced with %p (%d) "
1252 "inode %p ino %llx.%llx\n",
1253 dn, d_count(dn),
1254 realdn, d_count(realdn),
1255 d_inode(realdn), ceph_vinop(d_inode(realdn)));
1256 dput(dn);
1257 *pdn = realdn;
1258 } else {
1259 BUG_ON(!ceph_dentry(dn));
1260 dout("dn %p attached to %p ino %llx.%llx\n",
1261 dn, d_inode(dn), ceph_vinop(d_inode(dn)));
1262 }
1263 return 0;
1264 }
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1278 {
1279 struct ceph_mds_session *session = req->r_session;
1280 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1281 struct inode *in = NULL;
1282 struct ceph_vino tvino, dvino;
1283 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1284 int err = 0;
1285
1286 dout("fill_trace %p is_dentry %d is_target %d\n", req,
1287 rinfo->head->is_dentry, rinfo->head->is_target);
1288
1289 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1290 dout("fill_trace reply is empty!\n");
1291 if (rinfo->head->result == 0 && req->r_parent)
1292 ceph_invalidate_dir_request(req);
1293 return 0;
1294 }
1295
1296 if (rinfo->head->is_dentry) {
1297 struct inode *dir = req->r_parent;
1298
1299 if (dir) {
1300 err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1301 rinfo->dirfrag, session, -1,
1302 &req->r_caps_reservation);
1303 if (err < 0)
1304 goto done;
1305 } else {
1306 WARN_ON_ONCE(1);
1307 }
1308
1309 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
1310 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1311 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1312 struct qstr dname;
1313 struct dentry *dn, *parent;
1314
1315 BUG_ON(!rinfo->head->is_target);
1316 BUG_ON(req->r_dentry);
1317
1318 parent = d_find_any_alias(dir);
1319 BUG_ON(!parent);
1320
1321 dname.name = rinfo->dname;
1322 dname.len = rinfo->dname_len;
1323 dname.hash = full_name_hash(parent, dname.name, dname.len);
1324 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1325 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1326 retry_lookup:
1327 dn = d_lookup(parent, &dname);
1328 dout("d_lookup on parent=%p name=%.*s got %p\n",
1329 parent, dname.len, dname.name, dn);
1330
1331 if (!dn) {
1332 dn = d_alloc(parent, &dname);
1333 dout("d_alloc %p '%.*s' = %p\n", parent,
1334 dname.len, dname.name, dn);
1335 if (!dn) {
1336 dput(parent);
1337 err = -ENOMEM;
1338 goto done;
1339 }
1340 err = 0;
1341 } else if (d_really_is_positive(dn) &&
1342 (ceph_ino(d_inode(dn)) != tvino.ino ||
1343 ceph_snap(d_inode(dn)) != tvino.snap)) {
1344 dout(" dn %p points to wrong inode %p\n",
1345 dn, d_inode(dn));
1346 ceph_dir_clear_ordered(dir);
1347 d_delete(dn);
1348 dput(dn);
1349 goto retry_lookup;
1350 }
1351
1352 req->r_dentry = dn;
1353 dput(parent);
1354 }
1355 }
1356
1357 if (rinfo->head->is_target) {
1358
1359 BUG_ON(!req->r_target_inode);
1360
1361 in = req->r_target_inode;
1362 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1363 NULL, session,
1364 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1365 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1366 rinfo->head->result == 0) ? req->r_fmode : -1,
1367 &req->r_caps_reservation);
1368 if (err < 0) {
1369 pr_err("ceph_fill_inode badness %p %llx.%llx\n",
1370 in, ceph_vinop(in));
1371 req->r_target_inode = NULL;
1372 if (in->i_state & I_NEW)
1373 discard_new_inode(in);
1374 else
1375 iput(in);
1376 goto done;
1377 }
1378 if (in->i_state & I_NEW)
1379 unlock_new_inode(in);
1380 }
1381
1382
1383
1384
1385
1386 if (rinfo->head->is_dentry &&
1387 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
1388 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1389 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
1390 fsc->mount_options->snapdir_name,
1391 req->r_dentry->d_name.len))) {
1392
1393
1394
1395
1396
1397 struct inode *dir = req->r_parent;
1398 struct dentry *dn = req->r_dentry;
1399 bool have_dir_cap, have_lease;
1400
1401 BUG_ON(!dn);
1402 BUG_ON(!dir);
1403 BUG_ON(d_inode(dn->d_parent) != dir);
1404
1405 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1406 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1407
1408 BUG_ON(ceph_ino(dir) != dvino.ino);
1409 BUG_ON(ceph_snap(dir) != dvino.snap);
1410
1411
1412 have_dir_cap =
1413 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1414 CEPH_CAP_FILE_SHARED);
1415
1416
1417 have_lease = have_dir_cap ||
1418 le32_to_cpu(rinfo->dlease->duration_ms);
1419 if (!have_lease)
1420 dout("fill_trace no dentry lease or dir cap\n");
1421
1422
1423 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1424 struct inode *olddir = req->r_old_dentry_dir;
1425 BUG_ON(!olddir);
1426
1427 dout(" src %p '%pd' dst %p '%pd'\n",
1428 req->r_old_dentry,
1429 req->r_old_dentry,
1430 dn, dn);
1431 dout("fill_trace doing d_move %p -> %p\n",
1432 req->r_old_dentry, dn);
1433
1434
1435 ceph_dir_clear_ordered(dir);
1436 ceph_dir_clear_ordered(olddir);
1437
1438 d_move(req->r_old_dentry, dn);
1439 dout(" src %p '%pd' dst %p '%pd'\n",
1440 req->r_old_dentry,
1441 req->r_old_dentry,
1442 dn, dn);
1443
1444
1445
1446 ceph_invalidate_dentry_lease(dn);
1447
1448 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
1449 ceph_dentry(req->r_old_dentry)->offset);
1450
1451
1452
1453
1454 req->r_dentry = req->r_old_dentry;
1455 req->r_old_dentry = dn;
1456 dn = req->r_dentry;
1457 }
1458
1459
1460 if (!rinfo->head->is_target) {
1461 dout("fill_trace null dentry\n");
1462 if (d_really_is_positive(dn)) {
1463 dout("d_delete %p\n", dn);
1464 ceph_dir_clear_ordered(dir);
1465 d_delete(dn);
1466 } else if (have_lease) {
1467 if (d_unhashed(dn))
1468 d_add(dn, NULL);
1469 }
1470
1471 if (!d_unhashed(dn) && have_lease)
1472 update_dentry_lease(dir, dn,
1473 rinfo->dlease, session,
1474 req->r_request_started);
1475 goto done;
1476 }
1477
1478
1479 if (d_really_is_negative(dn)) {
1480 ceph_dir_clear_ordered(dir);
1481 ihold(in);
1482 err = splice_dentry(&req->r_dentry, in);
1483 if (err < 0)
1484 goto done;
1485 dn = req->r_dentry;
1486 } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1487 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1488 dn, d_inode(dn), ceph_vinop(d_inode(dn)),
1489 ceph_vinop(in));
1490 d_invalidate(dn);
1491 have_lease = false;
1492 }
1493
1494 if (have_lease) {
1495 update_dentry_lease(dir, dn,
1496 rinfo->dlease, session,
1497 req->r_request_started);
1498 }
1499 dout(" final dn %p\n", dn);
1500 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1501 req->r_op == CEPH_MDS_OP_MKSNAP) &&
1502 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1503 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
1504 struct inode *dir = req->r_parent;
1505
1506
1507 BUG_ON(!dir);
1508 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
1509 BUG_ON(!req->r_dentry);
1510 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
1511 ceph_dir_clear_ordered(dir);
1512 ihold(in);
1513 err = splice_dentry(&req->r_dentry, in);
1514 if (err < 0)
1515 goto done;
1516 } else if (rinfo->head->is_dentry && req->r_dentry) {
1517
1518 struct ceph_vino *ptvino = NULL;
1519 dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1520 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1521 if (rinfo->head->is_target) {
1522 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1523 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1524 ptvino = &tvino;
1525 }
1526 update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1527 session, req->r_request_started,
1528 rinfo->dname, rinfo->dname_len,
1529 &dvino, ptvino);
1530 }
1531 done:
1532 dout("fill_trace done err=%d\n", err);
1533 return err;
1534 }
1535
1536
1537
1538
1539 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1540 struct ceph_mds_session *session)
1541 {
1542 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1543 int i, err = 0;
1544
1545 for (i = 0; i < rinfo->dir_nr; i++) {
1546 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1547 struct ceph_vino vino;
1548 struct inode *in;
1549 int rc;
1550
1551 vino.ino = le64_to_cpu(rde->inode.in->ino);
1552 vino.snap = le64_to_cpu(rde->inode.in->snapid);
1553
1554 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1555 if (IS_ERR(in)) {
1556 err = PTR_ERR(in);
1557 dout("new_inode badness got %d\n", err);
1558 continue;
1559 }
1560 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1561 -1, &req->r_caps_reservation);
1562 if (rc < 0) {
1563 pr_err("ceph_fill_inode badness on %p got %d\n",
1564 in, rc);
1565 err = rc;
1566 if (in->i_state & I_NEW) {
1567 ihold(in);
1568 discard_new_inode(in);
1569 }
1570 } else if (in->i_state & I_NEW) {
1571 unlock_new_inode(in);
1572 }
1573
1574 iput(in);
1575 }
1576
1577 return err;
1578 }
1579
1580 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1581 {
1582 if (ctl->page) {
1583 kunmap(ctl->page);
1584 put_page(ctl->page);
1585 ctl->page = NULL;
1586 }
1587 }
1588
1589 static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1590 struct ceph_readdir_cache_control *ctl,
1591 struct ceph_mds_request *req)
1592 {
1593 struct ceph_inode_info *ci = ceph_inode(dir);
1594 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1595 unsigned idx = ctl->index % nsize;
1596 pgoff_t pgoff = ctl->index / nsize;
1597
1598 if (!ctl->page || pgoff != page_index(ctl->page)) {
1599 ceph_readdir_cache_release(ctl);
1600 if (idx == 0)
1601 ctl->page = grab_cache_page(&dir->i_data, pgoff);
1602 else
1603 ctl->page = find_lock_page(&dir->i_data, pgoff);
1604 if (!ctl->page) {
1605 ctl->index = -1;
1606 return idx == 0 ? -ENOMEM : 0;
1607 }
1608
1609
1610 unlock_page(ctl->page);
1611 ctl->dentries = kmap(ctl->page);
1612 if (idx == 0)
1613 memset(ctl->dentries, 0, PAGE_SIZE);
1614 }
1615
1616 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1617 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1618 dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1619 ctl->dentries[idx] = dn;
1620 ctl->index++;
1621 } else {
1622 dout("disable readdir cache\n");
1623 ctl->index = -1;
1624 }
1625 return 0;
1626 }
1627
1628 int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1629 struct ceph_mds_session *session)
1630 {
1631 struct dentry *parent = req->r_dentry;
1632 struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
1633 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1634 struct qstr dname;
1635 struct dentry *dn;
1636 struct inode *in;
1637 int err = 0, skipped = 0, ret, i;
1638 u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1639 u32 last_hash = 0;
1640 u32 fpos_offset;
1641 struct ceph_readdir_cache_control cache_ctl = {};
1642
1643 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1644 return readdir_prepopulate_inodes_only(req, session);
1645
1646 if (rinfo->hash_order) {
1647 if (req->r_path2) {
1648 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1649 req->r_path2,
1650 strlen(req->r_path2));
1651 last_hash = ceph_frag_value(last_hash);
1652 } else if (rinfo->offset_hash) {
1653
1654 WARN_ON_ONCE(req->r_readdir_offset != 2);
1655 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
1656 }
1657 }
1658
1659 if (rinfo->dir_dir &&
1660 le32_to_cpu(rinfo->dir_dir->frag) != frag) {
1661 dout("readdir_prepopulate got new frag %x -> %x\n",
1662 frag, le32_to_cpu(rinfo->dir_dir->frag));
1663 frag = le32_to_cpu(rinfo->dir_dir->frag);
1664 if (!rinfo->hash_order)
1665 req->r_readdir_offset = 2;
1666 }
1667
1668 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1669 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1670 rinfo->dir_nr, parent);
1671 } else {
1672 dout("readdir_prepopulate %d items under dn %p\n",
1673 rinfo->dir_nr, parent);
1674 if (rinfo->dir_dir)
1675 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1676
1677 if (ceph_frag_is_leftmost(frag) &&
1678 req->r_readdir_offset == 2 &&
1679 !(rinfo->hash_order && last_hash)) {
1680
1681
1682 req->r_dir_release_cnt =
1683 atomic64_read(&ci->i_release_count);
1684 req->r_dir_ordered_cnt =
1685 atomic64_read(&ci->i_ordered_count);
1686 req->r_readdir_cache_idx = 0;
1687 }
1688 }
1689
1690 cache_ctl.index = req->r_readdir_cache_idx;
1691 fpos_offset = req->r_readdir_offset;
1692
1693
1694 for (i = 0; i < rinfo->dir_nr; i++) {
1695 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1696 struct ceph_vino tvino;
1697
1698 dname.name = rde->name;
1699 dname.len = rde->name_len;
1700 dname.hash = full_name_hash(parent, dname.name, dname.len);
1701
1702 tvino.ino = le64_to_cpu(rde->inode.in->ino);
1703 tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1704
1705 if (rinfo->hash_order) {
1706 u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
1707 rde->name, rde->name_len);
1708 hash = ceph_frag_value(hash);
1709 if (hash != last_hash)
1710 fpos_offset = 2;
1711 last_hash = hash;
1712 rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1713 } else {
1714 rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1715 }
1716
1717 retry_lookup:
1718 dn = d_lookup(parent, &dname);
1719 dout("d_lookup on parent=%p name=%.*s got %p\n",
1720 parent, dname.len, dname.name, dn);
1721
1722 if (!dn) {
1723 dn = d_alloc(parent, &dname);
1724 dout("d_alloc %p '%.*s' = %p\n", parent,
1725 dname.len, dname.name, dn);
1726 if (!dn) {
1727 dout("d_alloc badness\n");
1728 err = -ENOMEM;
1729 goto out;
1730 }
1731 } else if (d_really_is_positive(dn) &&
1732 (ceph_ino(d_inode(dn)) != tvino.ino ||
1733 ceph_snap(d_inode(dn)) != tvino.snap)) {
1734 struct ceph_dentry_info *di = ceph_dentry(dn);
1735 dout(" dn %p points to wrong inode %p\n",
1736 dn, d_inode(dn));
1737
1738 spin_lock(&dn->d_lock);
1739 if (di->offset > 0 &&
1740 di->lease_shared_gen ==
1741 atomic_read(&ci->i_shared_gen)) {
1742 __ceph_dir_clear_ordered(ci);
1743 di->offset = 0;
1744 }
1745 spin_unlock(&dn->d_lock);
1746
1747 d_delete(dn);
1748 dput(dn);
1749 goto retry_lookup;
1750 }
1751
1752
1753 if (d_really_is_positive(dn)) {
1754 in = d_inode(dn);
1755 } else {
1756 in = ceph_get_inode(parent->d_sb, tvino);
1757 if (IS_ERR(in)) {
1758 dout("new_inode badness\n");
1759 d_drop(dn);
1760 dput(dn);
1761 err = PTR_ERR(in);
1762 goto out;
1763 }
1764 }
1765
1766 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
1767 -1, &req->r_caps_reservation);
1768 if (ret < 0) {
1769 pr_err("ceph_fill_inode badness on %p\n", in);
1770 if (d_really_is_negative(dn)) {
1771 if (in->i_state & I_NEW) {
1772 ihold(in);
1773 discard_new_inode(in);
1774 }
1775 iput(in);
1776 }
1777 d_drop(dn);
1778 err = ret;
1779 goto next_item;
1780 }
1781 if (in->i_state & I_NEW)
1782 unlock_new_inode(in);
1783
1784 if (d_really_is_negative(dn)) {
1785 if (ceph_security_xattr_deadlock(in)) {
1786 dout(" skip splicing dn %p to inode %p"
1787 " (security xattr deadlock)\n", dn, in);
1788 iput(in);
1789 skipped++;
1790 goto next_item;
1791 }
1792
1793 err = splice_dentry(&dn, in);
1794 if (err < 0)
1795 goto next_item;
1796 }
1797
1798 ceph_dentry(dn)->offset = rde->offset;
1799
1800 update_dentry_lease(d_inode(parent), dn,
1801 rde->lease, req->r_session,
1802 req->r_request_started);
1803
1804 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
1805 ret = fill_readdir_cache(d_inode(parent), dn,
1806 &cache_ctl, req);
1807 if (ret < 0)
1808 err = ret;
1809 }
1810 next_item:
1811 dput(dn);
1812 }
1813 out:
1814 if (err == 0 && skipped == 0) {
1815 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
1816 req->r_readdir_cache_idx = cache_ctl.index;
1817 }
1818 ceph_readdir_cache_release(&cache_ctl);
1819 dout("readdir_prepopulate done\n");
1820 return err;
1821 }
1822
1823 bool ceph_inode_set_size(struct inode *inode, loff_t size)
1824 {
1825 struct ceph_inode_info *ci = ceph_inode(inode);
1826 bool ret;
1827
1828 spin_lock(&ci->i_ceph_lock);
1829 dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
1830 i_size_write(inode, size);
1831 ceph_fscache_update(inode);
1832 inode->i_blocks = calc_inode_blocks(size);
1833
1834 ret = __ceph_should_report_size(ci);
1835
1836 spin_unlock(&ci->i_ceph_lock);
1837
1838 return ret;
1839 }
1840
1841 void ceph_queue_inode_work(struct inode *inode, int work_bit)
1842 {
1843 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1844 struct ceph_inode_info *ci = ceph_inode(inode);
1845 set_bit(work_bit, &ci->i_work_mask);
1846
1847 ihold(inode);
1848 if (queue_work(fsc->inode_wq, &ci->i_work)) {
1849 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask);
1850 } else {
1851 dout("queue_inode_work %p already queued, mask=%lx\n",
1852 inode, ci->i_work_mask);
1853 iput(inode);
1854 }
1855 }
1856
1857 static void ceph_do_invalidate_pages(struct inode *inode)
1858 {
1859 struct ceph_inode_info *ci = ceph_inode(inode);
1860 u32 orig_gen;
1861 int check = 0;
1862
1863 ceph_fscache_invalidate(inode, false);
1864
1865 mutex_lock(&ci->i_truncate_mutex);
1866
1867 if (ceph_inode_is_shutdown(inode)) {
1868 pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n",
1869 __func__, ceph_vinop(inode));
1870 mapping_set_error(inode->i_mapping, -EIO);
1871 truncate_pagecache(inode, 0);
1872 mutex_unlock(&ci->i_truncate_mutex);
1873 goto out;
1874 }
1875
1876 spin_lock(&ci->i_ceph_lock);
1877 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1878 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1879 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1880 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1881 check = 1;
1882 spin_unlock(&ci->i_ceph_lock);
1883 mutex_unlock(&ci->i_truncate_mutex);
1884 goto out;
1885 }
1886 orig_gen = ci->i_rdcache_gen;
1887 spin_unlock(&ci->i_ceph_lock);
1888
1889 if (invalidate_inode_pages2(inode->i_mapping) < 0) {
1890 pr_err("invalidate_inode_pages2 %llx.%llx failed\n",
1891 ceph_vinop(inode));
1892 }
1893
1894 spin_lock(&ci->i_ceph_lock);
1895 if (orig_gen == ci->i_rdcache_gen &&
1896 orig_gen == ci->i_rdcache_revoking) {
1897 dout("invalidate_pages %p gen %d successful\n", inode,
1898 ci->i_rdcache_gen);
1899 ci->i_rdcache_revoking--;
1900 check = 1;
1901 } else {
1902 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1903 inode, orig_gen, ci->i_rdcache_gen,
1904 ci->i_rdcache_revoking);
1905 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
1906 check = 1;
1907 }
1908 spin_unlock(&ci->i_ceph_lock);
1909 mutex_unlock(&ci->i_truncate_mutex);
1910 out:
1911 if (check)
1912 ceph_check_caps(ci, 0, NULL);
1913 }
1914
1915
1916
1917
1918
1919 void __ceph_do_pending_vmtruncate(struct inode *inode)
1920 {
1921 struct ceph_inode_info *ci = ceph_inode(inode);
1922 u64 to;
1923 int wrbuffer_refs, finish = 0;
1924
1925 mutex_lock(&ci->i_truncate_mutex);
1926 retry:
1927 spin_lock(&ci->i_ceph_lock);
1928 if (ci->i_truncate_pending == 0) {
1929 dout("__do_pending_vmtruncate %p none pending\n", inode);
1930 spin_unlock(&ci->i_ceph_lock);
1931 mutex_unlock(&ci->i_truncate_mutex);
1932 return;
1933 }
1934
1935
1936
1937
1938
1939 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1940 spin_unlock(&ci->i_ceph_lock);
1941 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1942 inode);
1943 filemap_write_and_wait_range(&inode->i_data, 0,
1944 inode->i_sb->s_maxbytes);
1945 goto retry;
1946 }
1947
1948
1949 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1950
1951 to = ci->i_truncate_size;
1952 wrbuffer_refs = ci->i_wrbuffer_ref;
1953 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1954 ci->i_truncate_pending, to);
1955 spin_unlock(&ci->i_ceph_lock);
1956
1957 ceph_fscache_resize(inode, to);
1958 truncate_pagecache(inode, to);
1959
1960 spin_lock(&ci->i_ceph_lock);
1961 if (to == ci->i_truncate_size) {
1962 ci->i_truncate_pending = 0;
1963 finish = 1;
1964 }
1965 spin_unlock(&ci->i_ceph_lock);
1966 if (!finish)
1967 goto retry;
1968
1969 mutex_unlock(&ci->i_truncate_mutex);
1970
1971 if (wrbuffer_refs == 0)
1972 ceph_check_caps(ci, 0, NULL);
1973
1974 wake_up_all(&ci->i_cap_wq);
1975 }
1976
1977 static void ceph_inode_work(struct work_struct *work)
1978 {
1979 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1980 i_work);
1981 struct inode *inode = &ci->netfs.inode;
1982
1983 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
1984 dout("writeback %p\n", inode);
1985 filemap_fdatawrite(&inode->i_data);
1986 }
1987 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
1988 ceph_do_invalidate_pages(inode);
1989
1990 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
1991 __ceph_do_pending_vmtruncate(inode);
1992
1993 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
1994 ceph_check_caps(ci, 0, NULL);
1995
1996 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
1997 ceph_flush_snaps(ci, NULL);
1998
1999 iput(inode);
2000 }
2001
2002
2003
2004
2005 static const struct inode_operations ceph_symlink_iops = {
2006 .get_link = simple_get_link,
2007 .setattr = ceph_setattr,
2008 .getattr = ceph_getattr,
2009 .listxattr = ceph_listxattr,
2010 };
2011
2012 int __ceph_setattr(struct inode *inode, struct iattr *attr)
2013 {
2014 struct ceph_inode_info *ci = ceph_inode(inode);
2015 unsigned int ia_valid = attr->ia_valid;
2016 struct ceph_mds_request *req;
2017 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2018 struct ceph_cap_flush *prealloc_cf;
2019 int issued;
2020 int release = 0, dirtied = 0;
2021 int mask = 0;
2022 int err = 0;
2023 int inode_dirty_flags = 0;
2024 bool lock_snap_rwsem = false;
2025
2026 prealloc_cf = ceph_alloc_cap_flush();
2027 if (!prealloc_cf)
2028 return -ENOMEM;
2029
2030 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2031 USE_AUTH_MDS);
2032 if (IS_ERR(req)) {
2033 ceph_free_cap_flush(prealloc_cf);
2034 return PTR_ERR(req);
2035 }
2036
2037 spin_lock(&ci->i_ceph_lock);
2038 issued = __ceph_caps_issued(ci, NULL);
2039
2040 if (!ci->i_head_snapc &&
2041 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2042 lock_snap_rwsem = true;
2043 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2044 spin_unlock(&ci->i_ceph_lock);
2045 down_read(&mdsc->snap_rwsem);
2046 spin_lock(&ci->i_ceph_lock);
2047 issued = __ceph_caps_issued(ci, NULL);
2048 }
2049 }
2050
2051 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
2052
2053 if (ia_valid & ATTR_UID) {
2054 dout("setattr %p uid %d -> %d\n", inode,
2055 from_kuid(&init_user_ns, inode->i_uid),
2056 from_kuid(&init_user_ns, attr->ia_uid));
2057 if (issued & CEPH_CAP_AUTH_EXCL) {
2058 inode->i_uid = attr->ia_uid;
2059 dirtied |= CEPH_CAP_AUTH_EXCL;
2060 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2061 !uid_eq(attr->ia_uid, inode->i_uid)) {
2062 req->r_args.setattr.uid = cpu_to_le32(
2063 from_kuid(&init_user_ns, attr->ia_uid));
2064 mask |= CEPH_SETATTR_UID;
2065 release |= CEPH_CAP_AUTH_SHARED;
2066 }
2067 }
2068 if (ia_valid & ATTR_GID) {
2069 dout("setattr %p gid %d -> %d\n", inode,
2070 from_kgid(&init_user_ns, inode->i_gid),
2071 from_kgid(&init_user_ns, attr->ia_gid));
2072 if (issued & CEPH_CAP_AUTH_EXCL) {
2073 inode->i_gid = attr->ia_gid;
2074 dirtied |= CEPH_CAP_AUTH_EXCL;
2075 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2076 !gid_eq(attr->ia_gid, inode->i_gid)) {
2077 req->r_args.setattr.gid = cpu_to_le32(
2078 from_kgid(&init_user_ns, attr->ia_gid));
2079 mask |= CEPH_SETATTR_GID;
2080 release |= CEPH_CAP_AUTH_SHARED;
2081 }
2082 }
2083 if (ia_valid & ATTR_MODE) {
2084 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2085 attr->ia_mode);
2086 if (issued & CEPH_CAP_AUTH_EXCL) {
2087 inode->i_mode = attr->ia_mode;
2088 dirtied |= CEPH_CAP_AUTH_EXCL;
2089 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2090 attr->ia_mode != inode->i_mode) {
2091 inode->i_mode = attr->ia_mode;
2092 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2093 mask |= CEPH_SETATTR_MODE;
2094 release |= CEPH_CAP_AUTH_SHARED;
2095 }
2096 }
2097
2098 if (ia_valid & ATTR_ATIME) {
2099 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
2100 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
2101 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2102 if (issued & CEPH_CAP_FILE_EXCL) {
2103 ci->i_time_warp_seq++;
2104 inode->i_atime = attr->ia_atime;
2105 dirtied |= CEPH_CAP_FILE_EXCL;
2106 } else if ((issued & CEPH_CAP_FILE_WR) &&
2107 timespec64_compare(&inode->i_atime,
2108 &attr->ia_atime) < 0) {
2109 inode->i_atime = attr->ia_atime;
2110 dirtied |= CEPH_CAP_FILE_WR;
2111 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2112 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
2113 ceph_encode_timespec64(&req->r_args.setattr.atime,
2114 &attr->ia_atime);
2115 mask |= CEPH_SETATTR_ATIME;
2116 release |= CEPH_CAP_FILE_SHARED |
2117 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2118 }
2119 }
2120 if (ia_valid & ATTR_SIZE) {
2121 loff_t isize = i_size_read(inode);
2122
2123 dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size);
2124 if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
2125 if (attr->ia_size > isize) {
2126 i_size_write(inode, attr->ia_size);
2127 inode->i_blocks = calc_inode_blocks(attr->ia_size);
2128 ci->i_reported_size = attr->ia_size;
2129 dirtied |= CEPH_CAP_FILE_EXCL;
2130 ia_valid |= ATTR_MTIME;
2131 }
2132 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2133 attr->ia_size != isize) {
2134 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
2135 req->r_args.setattr.old_size = cpu_to_le64(isize);
2136 mask |= CEPH_SETATTR_SIZE;
2137 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2138 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2139 }
2140 }
2141 if (ia_valid & ATTR_MTIME) {
2142 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
2143 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
2144 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2145 if (issued & CEPH_CAP_FILE_EXCL) {
2146 ci->i_time_warp_seq++;
2147 inode->i_mtime = attr->ia_mtime;
2148 dirtied |= CEPH_CAP_FILE_EXCL;
2149 } else if ((issued & CEPH_CAP_FILE_WR) &&
2150 timespec64_compare(&inode->i_mtime,
2151 &attr->ia_mtime) < 0) {
2152 inode->i_mtime = attr->ia_mtime;
2153 dirtied |= CEPH_CAP_FILE_WR;
2154 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
2155 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
2156 ceph_encode_timespec64(&req->r_args.setattr.mtime,
2157 &attr->ia_mtime);
2158 mask |= CEPH_SETATTR_MTIME;
2159 release |= CEPH_CAP_FILE_SHARED |
2160 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2161 }
2162 }
2163
2164
2165 if (ia_valid & ATTR_CTIME) {
2166 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2167 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
2168 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
2169 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
2170 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2171 only ? "ctime only" : "ignored");
2172 if (only) {
2173
2174
2175
2176
2177
2178 if (issued & CEPH_CAP_AUTH_EXCL)
2179 dirtied |= CEPH_CAP_AUTH_EXCL;
2180 else if (issued & CEPH_CAP_FILE_EXCL)
2181 dirtied |= CEPH_CAP_FILE_EXCL;
2182 else if (issued & CEPH_CAP_XATTR_EXCL)
2183 dirtied |= CEPH_CAP_XATTR_EXCL;
2184 else
2185 mask |= CEPH_SETATTR_CTIME;
2186 }
2187 }
2188 if (ia_valid & ATTR_FILE)
2189 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2190
2191 if (dirtied) {
2192 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2193 &prealloc_cf);
2194 inode->i_ctime = attr->ia_ctime;
2195 }
2196
2197 release &= issued;
2198 spin_unlock(&ci->i_ceph_lock);
2199 if (lock_snap_rwsem)
2200 up_read(&mdsc->snap_rwsem);
2201
2202 if (inode_dirty_flags)
2203 __mark_inode_dirty(inode, inode_dirty_flags);
2204
2205 if (mask) {
2206 req->r_inode = inode;
2207 ihold(inode);
2208 req->r_inode_drop = release;
2209 req->r_args.setattr.mask = cpu_to_le32(mask);
2210 req->r_num_caps = 1;
2211 req->r_stamp = attr->ia_ctime;
2212 err = ceph_mdsc_do_request(mdsc, NULL, req);
2213 }
2214 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2215 ceph_cap_string(dirtied), mask);
2216
2217 ceph_mdsc_put_request(req);
2218 ceph_free_cap_flush(prealloc_cf);
2219
2220 if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2221 __ceph_do_pending_vmtruncate(inode);
2222
2223 return err;
2224 }
2225
2226
2227
2228
2229 int ceph_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
2230 struct iattr *attr)
2231 {
2232 struct inode *inode = d_inode(dentry);
2233 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2234 int err;
2235
2236 if (ceph_snap(inode) != CEPH_NOSNAP)
2237 return -EROFS;
2238
2239 if (ceph_inode_is_shutdown(inode))
2240 return -ESTALE;
2241
2242 err = setattr_prepare(&init_user_ns, dentry, attr);
2243 if (err != 0)
2244 return err;
2245
2246 if ((attr->ia_valid & ATTR_SIZE) &&
2247 attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
2248 return -EFBIG;
2249
2250 if ((attr->ia_valid & ATTR_SIZE) &&
2251 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
2252 return -EDQUOT;
2253
2254 err = __ceph_setattr(inode, attr);
2255
2256 if (err >= 0 && (attr->ia_valid & ATTR_MODE))
2257 err = posix_acl_chmod(&init_user_ns, inode, attr->ia_mode);
2258
2259 return err;
2260 }
2261
2262 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
2263 {
2264 int issued = ceph_caps_issued(ceph_inode(inode));
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285 if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
2286 || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
2287 return USE_AUTH_MDS;
2288 else
2289 return USE_ANY_MDS;
2290 }
2291
2292
2293
2294
2295
2296 int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
2297 int mask, bool force)
2298 {
2299 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2300 struct ceph_mds_client *mdsc = fsc->mdsc;
2301 struct ceph_mds_request *req;
2302 int mode;
2303 int err;
2304
2305 if (ceph_snap(inode) == CEPH_SNAPDIR) {
2306 dout("do_getattr inode %p SNAPDIR\n", inode);
2307 return 0;
2308 }
2309
2310 dout("do_getattr inode %p mask %s mode 0%o\n",
2311 inode, ceph_cap_string(mask), inode->i_mode);
2312 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2313 return 0;
2314
2315 mode = ceph_try_to_choose_auth_mds(inode, mask);
2316 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2317 if (IS_ERR(req))
2318 return PTR_ERR(req);
2319 req->r_inode = inode;
2320 ihold(inode);
2321 req->r_num_caps = 1;
2322 req->r_args.getattr.mask = cpu_to_le32(mask);
2323 req->r_locked_page = locked_page;
2324 err = ceph_mdsc_do_request(mdsc, NULL, req);
2325 if (locked_page && err == 0) {
2326 u64 inline_version = req->r_reply_info.targeti.inline_version;
2327 if (inline_version == 0) {
2328
2329 err = -EINVAL;
2330 } else if (inline_version == CEPH_INLINE_NONE ||
2331 inline_version == 1) {
2332 err = -ENODATA;
2333 } else {
2334 err = req->r_reply_info.targeti.inline_len;
2335 }
2336 }
2337 ceph_mdsc_put_request(req);
2338 dout("do_getattr result=%d\n", err);
2339 return err;
2340 }
2341
2342 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
2343 size_t size)
2344 {
2345 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
2346 struct ceph_mds_client *mdsc = fsc->mdsc;
2347 struct ceph_mds_request *req;
2348 int mode = USE_AUTH_MDS;
2349 int err;
2350 char *xattr_value;
2351 size_t xattr_value_len;
2352
2353 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
2354 if (IS_ERR(req)) {
2355 err = -ENOMEM;
2356 goto out;
2357 }
2358
2359 req->r_path2 = kstrdup(name, GFP_NOFS);
2360 if (!req->r_path2) {
2361 err = -ENOMEM;
2362 goto put;
2363 }
2364
2365 ihold(inode);
2366 req->r_inode = inode;
2367 err = ceph_mdsc_do_request(mdsc, NULL, req);
2368 if (err < 0)
2369 goto put;
2370
2371 xattr_value = req->r_reply_info.xattr_info.xattr_value;
2372 xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
2373
2374 dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
2375
2376 err = (int)xattr_value_len;
2377 if (size == 0)
2378 goto put;
2379
2380 if (xattr_value_len > size) {
2381 err = -ERANGE;
2382 goto put;
2383 }
2384
2385 memcpy(value, xattr_value, xattr_value_len);
2386 put:
2387 ceph_mdsc_put_request(req);
2388 out:
2389 dout("do_getvxattr result=%d\n", err);
2390 return err;
2391 }
2392
2393
2394
2395
2396
2397
2398 int ceph_permission(struct user_namespace *mnt_userns, struct inode *inode,
2399 int mask)
2400 {
2401 int err;
2402
2403 if (mask & MAY_NOT_BLOCK)
2404 return -ECHILD;
2405
2406 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2407
2408 if (!err)
2409 err = generic_permission(&init_user_ns, inode, mask);
2410 return err;
2411 }
2412
2413
2414 static int statx_to_caps(u32 want, umode_t mode)
2415 {
2416 int mask = 0;
2417
2418 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME))
2419 mask |= CEPH_CAP_AUTH_SHARED;
2420
2421 if (want & (STATX_NLINK|STATX_CTIME)) {
2422
2423
2424
2425
2426 if (S_ISDIR(mode))
2427 mask |= CEPH_CAP_FILE_SHARED;
2428 else
2429 mask |= CEPH_CAP_LINK_SHARED;
2430 }
2431
2432 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|
2433 STATX_BLOCKS))
2434 mask |= CEPH_CAP_FILE_SHARED;
2435
2436 if (want & (STATX_CTIME))
2437 mask |= CEPH_CAP_XATTR_SHARED;
2438
2439 return mask;
2440 }
2441
2442
2443
2444
2445
2446 int ceph_getattr(struct user_namespace *mnt_userns, const struct path *path,
2447 struct kstat *stat, u32 request_mask, unsigned int flags)
2448 {
2449 struct inode *inode = d_inode(path->dentry);
2450 struct ceph_inode_info *ci = ceph_inode(inode);
2451 u32 valid_mask = STATX_BASIC_STATS;
2452 int err = 0;
2453
2454 if (ceph_inode_is_shutdown(inode))
2455 return -ESTALE;
2456
2457
2458 if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
2459 err = ceph_do_getattr(inode,
2460 statx_to_caps(request_mask, inode->i_mode),
2461 flags & AT_STATX_FORCE_SYNC);
2462 if (err)
2463 return err;
2464 }
2465
2466 generic_fillattr(&init_user_ns, inode, stat);
2467 stat->ino = ceph_present_inode(inode);
2468
2469
2470
2471
2472
2473 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
2474 stat->btime = ci->i_btime;
2475 valid_mask |= STATX_BTIME;
2476 }
2477
2478 if (ceph_snap(inode) == CEPH_NOSNAP)
2479 stat->dev = inode->i_sb->s_dev;
2480 else
2481 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
2482
2483 if (S_ISDIR(inode->i_mode)) {
2484 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
2485 RBYTES))
2486 stat->size = ci->i_rbytes;
2487 else
2488 stat->size = ci->i_files + ci->i_subdirs;
2489 stat->blocks = 0;
2490 stat->blksize = 65536;
2491
2492
2493
2494
2495
2496 if (stat->nlink == 1)
2497
2498 stat->nlink = 1 + 1 + ci->i_subdirs;
2499 }
2500
2501 stat->result_mask = request_mask & valid_mask;
2502 return err;
2503 }
2504
2505 void ceph_inode_shutdown(struct inode *inode)
2506 {
2507 struct ceph_inode_info *ci = ceph_inode(inode);
2508 struct rb_node *p;
2509 int iputs = 0;
2510 bool invalidate = false;
2511
2512 spin_lock(&ci->i_ceph_lock);
2513 ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
2514 p = rb_first(&ci->i_caps);
2515 while (p) {
2516 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
2517
2518 p = rb_next(p);
2519 iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
2520 }
2521 spin_unlock(&ci->i_ceph_lock);
2522
2523 if (invalidate)
2524 ceph_queue_invalidate(inode);
2525 while (iputs--)
2526 iput(inode);
2527 }