1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ceph/ceph_debug.h> 3 4 #include <linux/module.h> 5 #include <linux/fs.h> 6 #include <linux/slab.h> 7 #include <linux/string.h> 8 #include <linux/uaccess.h> 9 #include <linux/kernel.h> 10 #include <linux/writeback.h> 11 #include <linux/vmalloc.h> 12 #include <linux/xattr.h> 13 #include <linux/posix_acl.h> 14 #include <linux/random.h> 15 #include <linux/sort.h> 16 #include <linux/iversion.h> 17 18 #include "super.h" 19 #include "mds_client.h" 20 #include "cache.h" 21 #include <linux/ceph/decode.h> 22 23 /* 24 * Ceph inode operations 25 * 26 * Implement basic inode helpers (get, alloc) and inode ops (getattr, 27 * setattr, etc.), xattr helpers, and helpers for assimilating 28 * metadata returned by the MDS into our cache. 29 * 30 * Also define helpers for doing asynchronous writeback, invalidation, 31 * and truncation for the benefit of those who can't afford to block 32 * (typically because they are in the message handler path). 33 */ 34 35 static const struct inode_operations ceph_symlink_iops; 36 37 static void ceph_inode_work(struct work_struct *work); 38 39 /* 40 * find or create an inode, given the ceph ino number 41 */ 42 static int ceph_set_ino_cb(struct inode *inode, void *data) 43 { 44 struct ceph_inode_info *ci = ceph_inode(inode); 45 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 46 47 ci->i_vino = *(struct ceph_vino *)data; 48 inode->i_ino = ceph_vino_to_ino_t(ci->i_vino); 49 inode_set_iversion_raw(inode, 0); 50 percpu_counter_inc(&mdsc->metric.total_inodes); 51 52 return 0; 53 } 54 55 struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino) 56 { 57 struct inode *inode; 58 59 inode = iget5_locked(sb, (unsigned long)vino.ino, ceph_ino_compare, 60 ceph_set_ino_cb, &vino); 61 if (!inode) 62 return ERR_PTR(-ENOMEM); 63 64 dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode), 65 ceph_vinop(inode), inode, !!(inode->i_state & I_NEW)); 66 return inode; 67 } 68 69 /* 70 * get/constuct snapdir inode for a given directory 71 */ 72 struct inode *ceph_get_snapdir(struct inode *parent) 73 { 74 struct ceph_vino vino = { 75 .ino = ceph_ino(parent), 76 .snap = CEPH_SNAPDIR, 77 }; 78 struct inode *inode = ceph_get_inode(parent->i_sb, vino); 79 struct ceph_inode_info *ci = ceph_inode(inode); 80 81 if (IS_ERR(inode)) 82 return inode; 83 84 if (!S_ISDIR(parent->i_mode)) { 85 pr_warn_once("bad snapdir parent type (mode=0%o)\n", 86 parent->i_mode); 87 return ERR_PTR(-ENOTDIR); 88 } 89 90 if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) { 91 pr_warn_once("bad snapdir inode type (mode=0%o)\n", 92 inode->i_mode); 93 return ERR_PTR(-ENOTDIR); 94 } 95 96 inode->i_mode = parent->i_mode; 97 inode->i_uid = parent->i_uid; 98 inode->i_gid = parent->i_gid; 99 inode->i_mtime = parent->i_mtime; 100 inode->i_ctime = parent->i_ctime; 101 inode->i_atime = parent->i_atime; 102 inode->i_op = &ceph_snapdir_iops; 103 inode->i_fop = &ceph_snapdir_fops; 104 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ 105 ci->i_rbytes = 0; 106 ci->i_btime = ceph_inode(parent)->i_btime; 107 108 if (inode->i_state & I_NEW) 109 unlock_new_inode(inode); 110 111 return inode; 112 } 113 114 const struct inode_operations ceph_file_iops = { 115 .permission = ceph_permission, 116 .setattr = ceph_setattr, 117 .getattr = ceph_getattr, 118 .listxattr = ceph_listxattr, 119 .get_acl = ceph_get_acl, 120 .set_acl = ceph_set_acl, 121 }; 122 123 124 /* 125 * We use a 'frag tree' to keep track of the MDS's directory fragments 126 * for a given inode (usually there is just a single fragment). We 127 * need to know when a child frag is delegated to a new MDS, or when 128 * it is flagged as replicated, so we can direct our requests 129 * accordingly. 130 */ 131 132 /* 133 * find/create a frag in the tree 134 */ 135 static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, 136 u32 f) 137 { 138 struct rb_node **p; 139 struct rb_node *parent = NULL; 140 struct ceph_inode_frag *frag; 141 int c; 142 143 p = &ci->i_fragtree.rb_node; 144 while (*p) { 145 parent = *p; 146 frag = rb_entry(parent, struct ceph_inode_frag, node); 147 c = ceph_frag_compare(f, frag->frag); 148 if (c < 0) 149 p = &(*p)->rb_left; 150 else if (c > 0) 151 p = &(*p)->rb_right; 152 else 153 return frag; 154 } 155 156 frag = kmalloc(sizeof(*frag), GFP_NOFS); 157 if (!frag) 158 return ERR_PTR(-ENOMEM); 159 160 frag->frag = f; 161 frag->split_by = 0; 162 frag->mds = -1; 163 frag->ndist = 0; 164 165 rb_link_node(&frag->node, parent, p); 166 rb_insert_color(&frag->node, &ci->i_fragtree); 167 168 dout("get_or_create_frag added %llx.%llx frag %x\n", 169 ceph_vinop(&ci->vfs_inode), f); 170 return frag; 171 } 172 173 /* 174 * find a specific frag @f 175 */ 176 struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) 177 { 178 struct rb_node *n = ci->i_fragtree.rb_node; 179 180 while (n) { 181 struct ceph_inode_frag *frag = 182 rb_entry(n, struct ceph_inode_frag, node); 183 int c = ceph_frag_compare(f, frag->frag); 184 if (c < 0) 185 n = n->rb_left; 186 else if (c > 0) 187 n = n->rb_right; 188 else 189 return frag; 190 } 191 return NULL; 192 } 193 194 /* 195 * Choose frag containing the given value @v. If @pfrag is 196 * specified, copy the frag delegation info to the caller if 197 * it is present. 198 */ 199 static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v, 200 struct ceph_inode_frag *pfrag, int *found) 201 { 202 u32 t = ceph_frag_make(0, 0); 203 struct ceph_inode_frag *frag; 204 unsigned nway, i; 205 u32 n; 206 207 if (found) 208 *found = 0; 209 210 while (1) { 211 WARN_ON(!ceph_frag_contains_value(t, v)); 212 frag = __ceph_find_frag(ci, t); 213 if (!frag) 214 break; /* t is a leaf */ 215 if (frag->split_by == 0) { 216 if (pfrag) 217 memcpy(pfrag, frag, sizeof(*pfrag)); 218 if (found) 219 *found = 1; 220 break; 221 } 222 223 /* choose child */ 224 nway = 1 << frag->split_by; 225 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t, 226 frag->split_by, nway); 227 for (i = 0; i < nway; i++) { 228 n = ceph_frag_make_child(t, frag->split_by, i); 229 if (ceph_frag_contains_value(n, v)) { 230 t = n; 231 break; 232 } 233 } 234 BUG_ON(i == nway); 235 } 236 dout("choose_frag(%x) = %x\n", v, t); 237 238 return t; 239 } 240 241 u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, 242 struct ceph_inode_frag *pfrag, int *found) 243 { 244 u32 ret; 245 mutex_lock(&ci->i_fragtree_mutex); 246 ret = __ceph_choose_frag(ci, v, pfrag, found); 247 mutex_unlock(&ci->i_fragtree_mutex); 248 return ret; 249 } 250 251 /* 252 * Process dirfrag (delegation) info from the mds. Include leaf 253 * fragment in tree ONLY if ndist > 0. Otherwise, only 254 * branches/splits are included in i_fragtree) 255 */ 256 static int ceph_fill_dirfrag(struct inode *inode, 257 struct ceph_mds_reply_dirfrag *dirinfo) 258 { 259 struct ceph_inode_info *ci = ceph_inode(inode); 260 struct ceph_inode_frag *frag; 261 u32 id = le32_to_cpu(dirinfo->frag); 262 int mds = le32_to_cpu(dirinfo->auth); 263 int ndist = le32_to_cpu(dirinfo->ndist); 264 int diri_auth = -1; 265 int i; 266 int err = 0; 267 268 spin_lock(&ci->i_ceph_lock); 269 if (ci->i_auth_cap) 270 diri_auth = ci->i_auth_cap->mds; 271 spin_unlock(&ci->i_ceph_lock); 272 273 if (mds == -1) /* CDIR_AUTH_PARENT */ 274 mds = diri_auth; 275 276 mutex_lock(&ci->i_fragtree_mutex); 277 if (ndist == 0 && mds == diri_auth) { 278 /* no delegation info needed. */ 279 frag = __ceph_find_frag(ci, id); 280 if (!frag) 281 goto out; 282 if (frag->split_by == 0) { 283 /* tree leaf, remove */ 284 dout("fill_dirfrag removed %llx.%llx frag %x" 285 " (no ref)\n", ceph_vinop(inode), id); 286 rb_erase(&frag->node, &ci->i_fragtree); 287 kfree(frag); 288 } else { 289 /* tree branch, keep and clear */ 290 dout("fill_dirfrag cleared %llx.%llx frag %x" 291 " referral\n", ceph_vinop(inode), id); 292 frag->mds = -1; 293 frag->ndist = 0; 294 } 295 goto out; 296 } 297 298 299 /* find/add this frag to store mds delegation info */ 300 frag = __get_or_create_frag(ci, id); 301 if (IS_ERR(frag)) { 302 /* this is not the end of the world; we can continue 303 with bad/inaccurate delegation info */ 304 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n", 305 ceph_vinop(inode), le32_to_cpu(dirinfo->frag)); 306 err = -ENOMEM; 307 goto out; 308 } 309 310 frag->mds = mds; 311 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); 312 for (i = 0; i < frag->ndist; i++) 313 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); 314 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n", 315 ceph_vinop(inode), frag->frag, frag->ndist); 316 317 out: 318 mutex_unlock(&ci->i_fragtree_mutex); 319 return err; 320 } 321 322 static int frag_tree_split_cmp(const void *l, const void *r) 323 { 324 struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l; 325 struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r; 326 return ceph_frag_compare(le32_to_cpu(ls->frag), 327 le32_to_cpu(rs->frag)); 328 } 329 330 static bool is_frag_child(u32 f, struct ceph_inode_frag *frag) 331 { 332 if (!frag) 333 return f == ceph_frag_make(0, 0); 334 if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by) 335 return false; 336 return ceph_frag_contains_value(frag->frag, ceph_frag_value(f)); 337 } 338 339 static int ceph_fill_fragtree(struct inode *inode, 340 struct ceph_frag_tree_head *fragtree, 341 struct ceph_mds_reply_dirfrag *dirinfo) 342 { 343 struct ceph_inode_info *ci = ceph_inode(inode); 344 struct ceph_inode_frag *frag, *prev_frag = NULL; 345 struct rb_node *rb_node; 346 unsigned i, split_by, nsplits; 347 u32 id; 348 bool update = false; 349 350 mutex_lock(&ci->i_fragtree_mutex); 351 nsplits = le32_to_cpu(fragtree->nsplits); 352 if (nsplits != ci->i_fragtree_nsplits) { 353 update = true; 354 } else if (nsplits) { 355 i = prandom_u32() % nsplits; 356 id = le32_to_cpu(fragtree->splits[i].frag); 357 if (!__ceph_find_frag(ci, id)) 358 update = true; 359 } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) { 360 rb_node = rb_first(&ci->i_fragtree); 361 frag = rb_entry(rb_node, struct ceph_inode_frag, node); 362 if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node)) 363 update = true; 364 } 365 if (!update && dirinfo) { 366 id = le32_to_cpu(dirinfo->frag); 367 if (id != __ceph_choose_frag(ci, id, NULL, NULL)) 368 update = true; 369 } 370 if (!update) 371 goto out_unlock; 372 373 if (nsplits > 1) { 374 sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]), 375 frag_tree_split_cmp, NULL); 376 } 377 378 dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode)); 379 rb_node = rb_first(&ci->i_fragtree); 380 for (i = 0; i < nsplits; i++) { 381 id = le32_to_cpu(fragtree->splits[i].frag); 382 split_by = le32_to_cpu(fragtree->splits[i].by); 383 if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) { 384 pr_err("fill_fragtree %llx.%llx invalid split %d/%u, " 385 "frag %x split by %d\n", ceph_vinop(inode), 386 i, nsplits, id, split_by); 387 continue; 388 } 389 frag = NULL; 390 while (rb_node) { 391 frag = rb_entry(rb_node, struct ceph_inode_frag, node); 392 if (ceph_frag_compare(frag->frag, id) >= 0) { 393 if (frag->frag != id) 394 frag = NULL; 395 else 396 rb_node = rb_next(rb_node); 397 break; 398 } 399 rb_node = rb_next(rb_node); 400 /* delete stale split/leaf node */ 401 if (frag->split_by > 0 || 402 !is_frag_child(frag->frag, prev_frag)) { 403 rb_erase(&frag->node, &ci->i_fragtree); 404 if (frag->split_by > 0) 405 ci->i_fragtree_nsplits--; 406 kfree(frag); 407 } 408 frag = NULL; 409 } 410 if (!frag) { 411 frag = __get_or_create_frag(ci, id); 412 if (IS_ERR(frag)) 413 continue; 414 } 415 if (frag->split_by == 0) 416 ci->i_fragtree_nsplits++; 417 frag->split_by = split_by; 418 dout(" frag %x split by %d\n", frag->frag, frag->split_by); 419 prev_frag = frag; 420 } 421 while (rb_node) { 422 frag = rb_entry(rb_node, struct ceph_inode_frag, node); 423 rb_node = rb_next(rb_node); 424 /* delete stale split/leaf node */ 425 if (frag->split_by > 0 || 426 !is_frag_child(frag->frag, prev_frag)) { 427 rb_erase(&frag->node, &ci->i_fragtree); 428 if (frag->split_by > 0) 429 ci->i_fragtree_nsplits--; 430 kfree(frag); 431 } 432 } 433 out_unlock: 434 mutex_unlock(&ci->i_fragtree_mutex); 435 return 0; 436 } 437 438 /* 439 * initialize a newly allocated inode. 440 */ 441 struct inode *ceph_alloc_inode(struct super_block *sb) 442 { 443 struct ceph_inode_info *ci; 444 int i; 445 446 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS); 447 if (!ci) 448 return NULL; 449 450 dout("alloc_inode %p\n", &ci->vfs_inode); 451 452 spin_lock_init(&ci->i_ceph_lock); 453 454 ci->i_version = 0; 455 ci->i_inline_version = 0; 456 ci->i_time_warp_seq = 0; 457 ci->i_ceph_flags = 0; 458 atomic64_set(&ci->i_ordered_count, 1); 459 atomic64_set(&ci->i_release_count, 1); 460 atomic64_set(&ci->i_complete_seq[0], 0); 461 atomic64_set(&ci->i_complete_seq[1], 0); 462 ci->i_symlink = NULL; 463 464 ci->i_max_bytes = 0; 465 ci->i_max_files = 0; 466 467 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout)); 468 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); 469 RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL); 470 471 ci->i_fragtree = RB_ROOT; 472 mutex_init(&ci->i_fragtree_mutex); 473 474 ci->i_xattrs.blob = NULL; 475 ci->i_xattrs.prealloc_blob = NULL; 476 ci->i_xattrs.dirty = false; 477 ci->i_xattrs.index = RB_ROOT; 478 ci->i_xattrs.count = 0; 479 ci->i_xattrs.names_size = 0; 480 ci->i_xattrs.vals_size = 0; 481 ci->i_xattrs.version = 0; 482 ci->i_xattrs.index_version = 0; 483 484 ci->i_caps = RB_ROOT; 485 ci->i_auth_cap = NULL; 486 ci->i_dirty_caps = 0; 487 ci->i_flushing_caps = 0; 488 INIT_LIST_HEAD(&ci->i_dirty_item); 489 INIT_LIST_HEAD(&ci->i_flushing_item); 490 ci->i_prealloc_cap_flush = NULL; 491 INIT_LIST_HEAD(&ci->i_cap_flush_list); 492 init_waitqueue_head(&ci->i_cap_wq); 493 ci->i_hold_caps_max = 0; 494 INIT_LIST_HEAD(&ci->i_cap_delay_list); 495 INIT_LIST_HEAD(&ci->i_cap_snaps); 496 ci->i_head_snapc = NULL; 497 ci->i_snap_caps = 0; 498 499 ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ; 500 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) 501 ci->i_nr_by_mode[i] = 0; 502 503 mutex_init(&ci->i_truncate_mutex); 504 ci->i_truncate_seq = 0; 505 ci->i_truncate_size = 0; 506 ci->i_truncate_pending = 0; 507 508 ci->i_max_size = 0; 509 ci->i_reported_size = 0; 510 ci->i_wanted_max_size = 0; 511 ci->i_requested_max_size = 0; 512 513 ci->i_pin_ref = 0; 514 ci->i_rd_ref = 0; 515 ci->i_rdcache_ref = 0; 516 ci->i_wr_ref = 0; 517 ci->i_wb_ref = 0; 518 ci->i_fx_ref = 0; 519 ci->i_wrbuffer_ref = 0; 520 ci->i_wrbuffer_ref_head = 0; 521 atomic_set(&ci->i_filelock_ref, 0); 522 atomic_set(&ci->i_shared_gen, 1); 523 ci->i_rdcache_gen = 0; 524 ci->i_rdcache_revoking = 0; 525 526 INIT_LIST_HEAD(&ci->i_unsafe_dirops); 527 INIT_LIST_HEAD(&ci->i_unsafe_iops); 528 spin_lock_init(&ci->i_unsafe_lock); 529 530 ci->i_snap_realm = NULL; 531 INIT_LIST_HEAD(&ci->i_snap_realm_item); 532 INIT_LIST_HEAD(&ci->i_snap_flush_item); 533 534 INIT_WORK(&ci->i_work, ceph_inode_work); 535 ci->i_work_mask = 0; 536 memset(&ci->i_btime, '\0', sizeof(ci->i_btime)); 537 538 ceph_fscache_inode_init(ci); 539 540 ci->i_meta_err = 0; 541 542 return &ci->vfs_inode; 543 } 544 545 void ceph_free_inode(struct inode *inode) 546 { 547 struct ceph_inode_info *ci = ceph_inode(inode); 548 549 kfree(ci->i_symlink); 550 kmem_cache_free(ceph_inode_cachep, ci); 551 } 552 553 void ceph_evict_inode(struct inode *inode) 554 { 555 struct ceph_inode_info *ci = ceph_inode(inode); 556 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 557 struct ceph_inode_frag *frag; 558 struct rb_node *n; 559 560 dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode)); 561 562 percpu_counter_dec(&mdsc->metric.total_inodes); 563 564 truncate_inode_pages_final(&inode->i_data); 565 clear_inode(inode); 566 567 ceph_fscache_unregister_inode_cookie(ci); 568 569 __ceph_remove_caps(ci); 570 571 if (__ceph_has_any_quota(ci)) 572 ceph_adjust_quota_realms_count(inode, false); 573 574 /* 575 * we may still have a snap_realm reference if there are stray 576 * caps in i_snap_caps. 577 */ 578 if (ci->i_snap_realm) { 579 if (ceph_snap(inode) == CEPH_NOSNAP) { 580 struct ceph_snap_realm *realm = ci->i_snap_realm; 581 dout(" dropping residual ref to snap realm %p\n", 582 realm); 583 spin_lock(&realm->inodes_with_caps_lock); 584 list_del_init(&ci->i_snap_realm_item); 585 ci->i_snap_realm = NULL; 586 if (realm->ino == ci->i_vino.ino) 587 realm->inode = NULL; 588 spin_unlock(&realm->inodes_with_caps_lock); 589 ceph_put_snap_realm(mdsc, realm); 590 } else { 591 ceph_put_snapid_map(mdsc, ci->i_snapid_map); 592 ci->i_snap_realm = NULL; 593 } 594 } 595 596 while ((n = rb_first(&ci->i_fragtree)) != NULL) { 597 frag = rb_entry(n, struct ceph_inode_frag, node); 598 rb_erase(n, &ci->i_fragtree); 599 kfree(frag); 600 } 601 ci->i_fragtree_nsplits = 0; 602 603 __ceph_destroy_xattrs(ci); 604 if (ci->i_xattrs.blob) 605 ceph_buffer_put(ci->i_xattrs.blob); 606 if (ci->i_xattrs.prealloc_blob) 607 ceph_buffer_put(ci->i_xattrs.prealloc_blob); 608 609 ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns)); 610 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); 611 } 612 613 static inline blkcnt_t calc_inode_blocks(u64 size) 614 { 615 return (size + (1<<9) - 1) >> 9; 616 } 617 618 /* 619 * Helpers to fill in size, ctime, mtime, and atime. We have to be 620 * careful because either the client or MDS may have more up to date 621 * info, depending on which capabilities are held, and whether 622 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime 623 * and size are monotonically increasing, except when utimes() or 624 * truncate() increments the corresponding _seq values.) 625 */ 626 int ceph_fill_file_size(struct inode *inode, int issued, 627 u32 truncate_seq, u64 truncate_size, u64 size) 628 { 629 struct ceph_inode_info *ci = ceph_inode(inode); 630 int queue_trunc = 0; 631 632 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || 633 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) { 634 dout("size %lld -> %llu\n", inode->i_size, size); 635 if (size > 0 && S_ISDIR(inode->i_mode)) { 636 pr_err("fill_file_size non-zero size for directory\n"); 637 size = 0; 638 } 639 i_size_write(inode, size); 640 inode->i_blocks = calc_inode_blocks(size); 641 ci->i_reported_size = size; 642 if (truncate_seq != ci->i_truncate_seq) { 643 dout("truncate_seq %u -> %u\n", 644 ci->i_truncate_seq, truncate_seq); 645 ci->i_truncate_seq = truncate_seq; 646 647 /* the MDS should have revoked these caps */ 648 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL | 649 CEPH_CAP_FILE_RD | 650 CEPH_CAP_FILE_WR | 651 CEPH_CAP_FILE_LAZYIO)); 652 /* 653 * If we hold relevant caps, or in the case where we're 654 * not the only client referencing this file and we 655 * don't hold those caps, then we need to check whether 656 * the file is either opened or mmaped 657 */ 658 if ((issued & (CEPH_CAP_FILE_CACHE| 659 CEPH_CAP_FILE_BUFFER)) || 660 mapping_mapped(inode->i_mapping) || 661 __ceph_is_file_opened(ci)) { 662 ci->i_truncate_pending++; 663 queue_trunc = 1; 664 } 665 } 666 } 667 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 && 668 ci->i_truncate_size != truncate_size) { 669 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size, 670 truncate_size); 671 ci->i_truncate_size = truncate_size; 672 } 673 674 if (queue_trunc) 675 ceph_fscache_invalidate(inode); 676 677 return queue_trunc; 678 } 679 680 void ceph_fill_file_time(struct inode *inode, int issued, 681 u64 time_warp_seq, struct timespec64 *ctime, 682 struct timespec64 *mtime, struct timespec64 *atime) 683 { 684 struct ceph_inode_info *ci = ceph_inode(inode); 685 int warn = 0; 686 687 if (issued & (CEPH_CAP_FILE_EXCL| 688 CEPH_CAP_FILE_WR| 689 CEPH_CAP_FILE_BUFFER| 690 CEPH_CAP_AUTH_EXCL| 691 CEPH_CAP_XATTR_EXCL)) { 692 if (ci->i_version == 0 || 693 timespec64_compare(ctime, &inode->i_ctime) > 0) { 694 dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n", 695 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 696 ctime->tv_sec, ctime->tv_nsec); 697 inode->i_ctime = *ctime; 698 } 699 if (ci->i_version == 0 || 700 ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { 701 /* the MDS did a utimes() */ 702 dout("mtime %lld.%09ld -> %lld.%09ld " 703 "tw %d -> %d\n", 704 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 705 mtime->tv_sec, mtime->tv_nsec, 706 ci->i_time_warp_seq, (int)time_warp_seq); 707 708 inode->i_mtime = *mtime; 709 inode->i_atime = *atime; 710 ci->i_time_warp_seq = time_warp_seq; 711 } else if (time_warp_seq == ci->i_time_warp_seq) { 712 /* nobody did utimes(); take the max */ 713 if (timespec64_compare(mtime, &inode->i_mtime) > 0) { 714 dout("mtime %lld.%09ld -> %lld.%09ld inc\n", 715 inode->i_mtime.tv_sec, 716 inode->i_mtime.tv_nsec, 717 mtime->tv_sec, mtime->tv_nsec); 718 inode->i_mtime = *mtime; 719 } 720 if (timespec64_compare(atime, &inode->i_atime) > 0) { 721 dout("atime %lld.%09ld -> %lld.%09ld inc\n", 722 inode->i_atime.tv_sec, 723 inode->i_atime.tv_nsec, 724 atime->tv_sec, atime->tv_nsec); 725 inode->i_atime = *atime; 726 } 727 } else if (issued & CEPH_CAP_FILE_EXCL) { 728 /* we did a utimes(); ignore mds values */ 729 } else { 730 warn = 1; 731 } 732 } else { 733 /* we have no write|excl caps; whatever the MDS says is true */ 734 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { 735 inode->i_ctime = *ctime; 736 inode->i_mtime = *mtime; 737 inode->i_atime = *atime; 738 ci->i_time_warp_seq = time_warp_seq; 739 } else { 740 warn = 1; 741 } 742 } 743 if (warn) /* time_warp_seq shouldn't go backwards */ 744 dout("%p mds time_warp_seq %llu < %u\n", 745 inode, time_warp_seq, ci->i_time_warp_seq); 746 } 747 748 /* 749 * Populate an inode based on info from mds. May be called on new or 750 * existing inodes. 751 */ 752 int ceph_fill_inode(struct inode *inode, struct page *locked_page, 753 struct ceph_mds_reply_info_in *iinfo, 754 struct ceph_mds_reply_dirfrag *dirinfo, 755 struct ceph_mds_session *session, int cap_fmode, 756 struct ceph_cap_reservation *caps_reservation) 757 { 758 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); 759 struct ceph_mds_reply_inode *info = iinfo->in; 760 struct ceph_inode_info *ci = ceph_inode(inode); 761 int issued, new_issued, info_caps; 762 struct timespec64 mtime, atime, ctime; 763 struct ceph_buffer *xattr_blob = NULL; 764 struct ceph_buffer *old_blob = NULL; 765 struct ceph_string *pool_ns = NULL; 766 struct ceph_cap *new_cap = NULL; 767 int err = 0; 768 bool wake = false; 769 bool queue_trunc = false; 770 bool new_version = false; 771 bool fill_inline = false; 772 773 dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__, 774 inode, ceph_vinop(inode), le64_to_cpu(info->version), 775 ci->i_version); 776 777 info_caps = le32_to_cpu(info->cap.caps); 778 779 /* prealloc new cap struct */ 780 if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) { 781 new_cap = ceph_get_cap(mdsc, caps_reservation); 782 if (!new_cap) 783 return -ENOMEM; 784 } 785 786 /* 787 * prealloc xattr data, if it looks like we'll need it. only 788 * if len > 4 (meaning there are actually xattrs; the first 4 789 * bytes are the xattr count). 790 */ 791 if (iinfo->xattr_len > 4) { 792 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); 793 if (!xattr_blob) 794 pr_err("%s ENOMEM xattr blob %d bytes\n", __func__, 795 iinfo->xattr_len); 796 } 797 798 if (iinfo->pool_ns_len > 0) 799 pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data, 800 iinfo->pool_ns_len); 801 802 if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map) 803 ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode)); 804 805 spin_lock(&ci->i_ceph_lock); 806 807 /* 808 * provided version will be odd if inode value is projected, 809 * even if stable. skip the update if we have newer stable 810 * info (ours>=theirs, e.g. due to racing mds replies), unless 811 * we are getting projected (unstable) info (in which case the 812 * version is odd, and we want ours>theirs). 813 * us them 814 * 2 2 skip 815 * 3 2 skip 816 * 3 3 update 817 */ 818 if (ci->i_version == 0 || 819 ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && 820 le64_to_cpu(info->version) > (ci->i_version & ~1))) 821 new_version = true; 822 823 /* Update change_attribute */ 824 inode_set_max_iversion_raw(inode, iinfo->change_attr); 825 826 __ceph_caps_issued(ci, &issued); 827 issued |= __ceph_caps_dirty(ci); 828 new_issued = ~issued & info_caps; 829 830 /* update inode */ 831 inode->i_rdev = le32_to_cpu(info->rdev); 832 /* directories have fl_stripe_unit set to zero */ 833 if (le32_to_cpu(info->layout.fl_stripe_unit)) 834 inode->i_blkbits = 835 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; 836 else 837 inode->i_blkbits = CEPH_BLOCK_SHIFT; 838 839 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files); 840 841 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && 842 (issued & CEPH_CAP_AUTH_EXCL) == 0) { 843 inode->i_mode = le32_to_cpu(info->mode); 844 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); 845 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); 846 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, 847 from_kuid(&init_user_ns, inode->i_uid), 848 from_kgid(&init_user_ns, inode->i_gid)); 849 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime); 850 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime); 851 } 852 853 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && 854 (issued & CEPH_CAP_LINK_EXCL) == 0) 855 set_nlink(inode, le32_to_cpu(info->nlink)); 856 857 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { 858 /* be careful with mtime, atime, size */ 859 ceph_decode_timespec64(&atime, &info->atime); 860 ceph_decode_timespec64(&mtime, &info->mtime); 861 ceph_decode_timespec64(&ctime, &info->ctime); 862 ceph_fill_file_time(inode, issued, 863 le32_to_cpu(info->time_warp_seq), 864 &ctime, &mtime, &atime); 865 } 866 867 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) { 868 ci->i_files = le64_to_cpu(info->files); 869 ci->i_subdirs = le64_to_cpu(info->subdirs); 870 } 871 872 if (new_version || 873 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { 874 s64 old_pool = ci->i_layout.pool_id; 875 struct ceph_string *old_ns; 876 877 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout); 878 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, 879 lockdep_is_held(&ci->i_ceph_lock)); 880 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns); 881 882 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) 883 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; 884 885 pool_ns = old_ns; 886 887 queue_trunc = ceph_fill_file_size(inode, issued, 888 le32_to_cpu(info->truncate_seq), 889 le64_to_cpu(info->truncate_size), 890 le64_to_cpu(info->size)); 891 /* only update max_size on auth cap */ 892 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && 893 ci->i_max_size != le64_to_cpu(info->max_size)) { 894 dout("max_size %lld -> %llu\n", ci->i_max_size, 895 le64_to_cpu(info->max_size)); 896 ci->i_max_size = le64_to_cpu(info->max_size); 897 } 898 } 899 900 /* layout and rstat are not tracked by capability, update them if 901 * the inode info is from auth mds */ 902 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) { 903 if (S_ISDIR(inode->i_mode)) { 904 ci->i_dir_layout = iinfo->dir_layout; 905 ci->i_rbytes = le64_to_cpu(info->rbytes); 906 ci->i_rfiles = le64_to_cpu(info->rfiles); 907 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); 908 ci->i_dir_pin = iinfo->dir_pin; 909 ceph_decode_timespec64(&ci->i_rctime, &info->rctime); 910 } 911 } 912 913 /* xattrs */ 914 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ 915 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && 916 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { 917 if (ci->i_xattrs.blob) 918 old_blob = ci->i_xattrs.blob; 919 ci->i_xattrs.blob = xattr_blob; 920 if (xattr_blob) 921 memcpy(ci->i_xattrs.blob->vec.iov_base, 922 iinfo->xattr_data, iinfo->xattr_len); 923 ci->i_xattrs.version = le64_to_cpu(info->xattr_version); 924 ceph_forget_all_cached_acls(inode); 925 ceph_security_invalidate_secctx(inode); 926 xattr_blob = NULL; 927 } 928 929 /* finally update i_version */ 930 if (le64_to_cpu(info->version) > ci->i_version) 931 ci->i_version = le64_to_cpu(info->version); 932 933 inode->i_mapping->a_ops = &ceph_aops; 934 935 switch (inode->i_mode & S_IFMT) { 936 case S_IFIFO: 937 case S_IFBLK: 938 case S_IFCHR: 939 case S_IFSOCK: 940 inode->i_blkbits = PAGE_SHIFT; 941 init_special_inode(inode, inode->i_mode, inode->i_rdev); 942 inode->i_op = &ceph_file_iops; 943 break; 944 case S_IFREG: 945 inode->i_op = &ceph_file_iops; 946 inode->i_fop = &ceph_file_fops; 947 break; 948 case S_IFLNK: 949 inode->i_op = &ceph_symlink_iops; 950 if (!ci->i_symlink) { 951 u32 symlen = iinfo->symlink_len; 952 char *sym; 953 954 spin_unlock(&ci->i_ceph_lock); 955 956 if (symlen != i_size_read(inode)) { 957 pr_err("%s %llx.%llx BAD symlink " 958 "size %lld\n", __func__, 959 ceph_vinop(inode), 960 i_size_read(inode)); 961 i_size_write(inode, symlen); 962 inode->i_blocks = calc_inode_blocks(symlen); 963 } 964 965 err = -ENOMEM; 966 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); 967 if (!sym) 968 goto out; 969 970 spin_lock(&ci->i_ceph_lock); 971 if (!ci->i_symlink) 972 ci->i_symlink = sym; 973 else 974 kfree(sym); /* lost a race */ 975 } 976 inode->i_link = ci->i_symlink; 977 break; 978 case S_IFDIR: 979 inode->i_op = &ceph_dir_iops; 980 inode->i_fop = &ceph_dir_fops; 981 break; 982 default: 983 pr_err("%s %llx.%llx BAD mode 0%o\n", __func__, 984 ceph_vinop(inode), inode->i_mode); 985 } 986 987 /* were we issued a capability? */ 988 if (info_caps) { 989 if (ceph_snap(inode) == CEPH_NOSNAP) { 990 ceph_add_cap(inode, session, 991 le64_to_cpu(info->cap.cap_id), 992 info_caps, 993 le32_to_cpu(info->cap.wanted), 994 le32_to_cpu(info->cap.seq), 995 le32_to_cpu(info->cap.mseq), 996 le64_to_cpu(info->cap.realm), 997 info->cap.flags, &new_cap); 998 999 /* set dir completion flag? */ 1000 if (S_ISDIR(inode->i_mode) && 1001 ci->i_files == 0 && ci->i_subdirs == 0 && 1002 (info_caps & CEPH_CAP_FILE_SHARED) && 1003 (issued & CEPH_CAP_FILE_EXCL) == 0 && 1004 !__ceph_dir_is_complete(ci)) { 1005 dout(" marking %p complete (empty)\n", inode); 1006 i_size_write(inode, 0); 1007 __ceph_dir_set_complete(ci, 1008 atomic64_read(&ci->i_release_count), 1009 atomic64_read(&ci->i_ordered_count)); 1010 } 1011 1012 wake = true; 1013 } else { 1014 dout(" %p got snap_caps %s\n", inode, 1015 ceph_cap_string(info_caps)); 1016 ci->i_snap_caps |= info_caps; 1017 } 1018 } 1019 1020 if (iinfo->inline_version > 0 && 1021 iinfo->inline_version >= ci->i_inline_version) { 1022 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 1023 ci->i_inline_version = iinfo->inline_version; 1024 if (ci->i_inline_version != CEPH_INLINE_NONE && 1025 (locked_page || (info_caps & cache_caps))) 1026 fill_inline = true; 1027 } 1028 1029 if (cap_fmode >= 0) { 1030 if (!info_caps) 1031 pr_warn("mds issued no caps on %llx.%llx\n", 1032 ceph_vinop(inode)); 1033 __ceph_touch_fmode(ci, mdsc, cap_fmode); 1034 } 1035 1036 spin_unlock(&ci->i_ceph_lock); 1037 1038 if (fill_inline) 1039 ceph_fill_inline_data(inode, locked_page, 1040 iinfo->inline_data, iinfo->inline_len); 1041 1042 if (wake) 1043 wake_up_all(&ci->i_cap_wq); 1044 1045 /* queue truncate if we saw i_size decrease */ 1046 if (queue_trunc) 1047 ceph_queue_vmtruncate(inode); 1048 1049 /* populate frag tree */ 1050 if (S_ISDIR(inode->i_mode)) 1051 ceph_fill_fragtree(inode, &info->fragtree, dirinfo); 1052 1053 /* update delegation info? */ 1054 if (dirinfo) 1055 ceph_fill_dirfrag(inode, dirinfo); 1056 1057 err = 0; 1058 out: 1059 if (new_cap) 1060 ceph_put_cap(mdsc, new_cap); 1061 ceph_buffer_put(old_blob); 1062 ceph_buffer_put(xattr_blob); 1063 ceph_put_string(pool_ns); 1064 return err; 1065 } 1066 1067 /* 1068 * caller should hold session s_mutex and dentry->d_lock. 1069 */ 1070 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry, 1071 struct ceph_mds_reply_lease *lease, 1072 struct ceph_mds_session *session, 1073 unsigned long from_time, 1074 struct ceph_mds_session **old_lease_session) 1075 { 1076 struct ceph_dentry_info *di = ceph_dentry(dentry); 1077 unsigned mask = le16_to_cpu(lease->mask); 1078 long unsigned duration = le32_to_cpu(lease->duration_ms); 1079 long unsigned ttl = from_time + (duration * HZ) / 1000; 1080 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; 1081 1082 dout("update_dentry_lease %p duration %lu ms ttl %lu\n", 1083 dentry, duration, ttl); 1084 1085 /* only track leases on regular dentries */ 1086 if (ceph_snap(dir) != CEPH_NOSNAP) 1087 return; 1088 1089 if (mask & CEPH_LEASE_PRIMARY_LINK) 1090 di->flags |= CEPH_DENTRY_PRIMARY_LINK; 1091 else 1092 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; 1093 1094 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen); 1095 if (!(mask & CEPH_LEASE_VALID)) { 1096 __ceph_dentry_dir_lease_touch(di); 1097 return; 1098 } 1099 1100 if (di->lease_gen == session->s_cap_gen && 1101 time_before(ttl, di->time)) 1102 return; /* we already have a newer lease. */ 1103 1104 if (di->lease_session && di->lease_session != session) { 1105 *old_lease_session = di->lease_session; 1106 di->lease_session = NULL; 1107 } 1108 1109 if (!di->lease_session) 1110 di->lease_session = ceph_get_mds_session(session); 1111 di->lease_gen = session->s_cap_gen; 1112 di->lease_seq = le32_to_cpu(lease->seq); 1113 di->lease_renew_after = half_ttl; 1114 di->lease_renew_from = 0; 1115 di->time = ttl; 1116 1117 __ceph_dentry_lease_touch(di); 1118 } 1119 1120 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry, 1121 struct ceph_mds_reply_lease *lease, 1122 struct ceph_mds_session *session, 1123 unsigned long from_time) 1124 { 1125 struct ceph_mds_session *old_lease_session = NULL; 1126 spin_lock(&dentry->d_lock); 1127 __update_dentry_lease(dir, dentry, lease, session, from_time, 1128 &old_lease_session); 1129 spin_unlock(&dentry->d_lock); 1130 if (old_lease_session) 1131 ceph_put_mds_session(old_lease_session); 1132 } 1133 1134 /* 1135 * update dentry lease without having parent inode locked 1136 */ 1137 static void update_dentry_lease_careful(struct dentry *dentry, 1138 struct ceph_mds_reply_lease *lease, 1139 struct ceph_mds_session *session, 1140 unsigned long from_time, 1141 char *dname, u32 dname_len, 1142 struct ceph_vino *pdvino, 1143 struct ceph_vino *ptvino) 1144 1145 { 1146 struct inode *dir; 1147 struct ceph_mds_session *old_lease_session = NULL; 1148 1149 spin_lock(&dentry->d_lock); 1150 /* make sure dentry's name matches target */ 1151 if (dentry->d_name.len != dname_len || 1152 memcmp(dentry->d_name.name, dname, dname_len)) 1153 goto out_unlock; 1154 1155 dir = d_inode(dentry->d_parent); 1156 /* make sure parent matches dvino */ 1157 if (!ceph_ino_compare(dir, pdvino)) 1158 goto out_unlock; 1159 1160 /* make sure dentry's inode matches target. NULL ptvino means that 1161 * we expect a negative dentry */ 1162 if (ptvino) { 1163 if (d_really_is_negative(dentry)) 1164 goto out_unlock; 1165 if (!ceph_ino_compare(d_inode(dentry), ptvino)) 1166 goto out_unlock; 1167 } else { 1168 if (d_really_is_positive(dentry)) 1169 goto out_unlock; 1170 } 1171 1172 __update_dentry_lease(dir, dentry, lease, session, 1173 from_time, &old_lease_session); 1174 out_unlock: 1175 spin_unlock(&dentry->d_lock); 1176 if (old_lease_session) 1177 ceph_put_mds_session(old_lease_session); 1178 } 1179 1180 /* 1181 * splice a dentry to an inode. 1182 * caller must hold directory i_mutex for this to be safe. 1183 */ 1184 static int splice_dentry(struct dentry **pdn, struct inode *in) 1185 { 1186 struct dentry *dn = *pdn; 1187 struct dentry *realdn; 1188 1189 BUG_ON(d_inode(dn)); 1190 1191 if (S_ISDIR(in->i_mode)) { 1192 /* If inode is directory, d_splice_alias() below will remove 1193 * 'realdn' from its origin parent. We need to ensure that 1194 * origin parent's readdir cache will not reference 'realdn' 1195 */ 1196 realdn = d_find_any_alias(in); 1197 if (realdn) { 1198 struct ceph_dentry_info *di = ceph_dentry(realdn); 1199 spin_lock(&realdn->d_lock); 1200 1201 realdn->d_op->d_prune(realdn); 1202 1203 di->time = jiffies; 1204 di->lease_shared_gen = 0; 1205 di->offset = 0; 1206 1207 spin_unlock(&realdn->d_lock); 1208 dput(realdn); 1209 } 1210 } 1211 1212 /* dn must be unhashed */ 1213 if (!d_unhashed(dn)) 1214 d_drop(dn); 1215 realdn = d_splice_alias(in, dn); 1216 if (IS_ERR(realdn)) { 1217 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n", 1218 PTR_ERR(realdn), dn, in, ceph_vinop(in)); 1219 return PTR_ERR(realdn); 1220 } 1221 1222 if (realdn) { 1223 dout("dn %p (%d) spliced with %p (%d) " 1224 "inode %p ino %llx.%llx\n", 1225 dn, d_count(dn), 1226 realdn, d_count(realdn), 1227 d_inode(realdn), ceph_vinop(d_inode(realdn))); 1228 dput(dn); 1229 *pdn = realdn; 1230 } else { 1231 BUG_ON(!ceph_dentry(dn)); 1232 dout("dn %p attached to %p ino %llx.%llx\n", 1233 dn, d_inode(dn), ceph_vinop(d_inode(dn))); 1234 } 1235 return 0; 1236 } 1237 1238 /* 1239 * Incorporate results into the local cache. This is either just 1240 * one inode, or a directory, dentry, and possibly linked-to inode (e.g., 1241 * after a lookup). 1242 * 1243 * A reply may contain 1244 * a directory inode along with a dentry. 1245 * and/or a target inode 1246 * 1247 * Called with snap_rwsem (read). 1248 */ 1249 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) 1250 { 1251 struct ceph_mds_session *session = req->r_session; 1252 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1253 struct inode *in = NULL; 1254 struct ceph_vino tvino, dvino; 1255 struct ceph_fs_client *fsc = ceph_sb_to_client(sb); 1256 int err = 0; 1257 1258 dout("fill_trace %p is_dentry %d is_target %d\n", req, 1259 rinfo->head->is_dentry, rinfo->head->is_target); 1260 1261 if (!rinfo->head->is_target && !rinfo->head->is_dentry) { 1262 dout("fill_trace reply is empty!\n"); 1263 if (rinfo->head->result == 0 && req->r_parent) 1264 ceph_invalidate_dir_request(req); 1265 return 0; 1266 } 1267 1268 if (rinfo->head->is_dentry) { 1269 struct inode *dir = req->r_parent; 1270 1271 if (dir) { 1272 err = ceph_fill_inode(dir, NULL, &rinfo->diri, 1273 rinfo->dirfrag, session, -1, 1274 &req->r_caps_reservation); 1275 if (err < 0) 1276 goto done; 1277 } else { 1278 WARN_ON_ONCE(1); 1279 } 1280 1281 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME && 1282 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1283 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 1284 struct qstr dname; 1285 struct dentry *dn, *parent; 1286 1287 BUG_ON(!rinfo->head->is_target); 1288 BUG_ON(req->r_dentry); 1289 1290 parent = d_find_any_alias(dir); 1291 BUG_ON(!parent); 1292 1293 dname.name = rinfo->dname; 1294 dname.len = rinfo->dname_len; 1295 dname.hash = full_name_hash(parent, dname.name, dname.len); 1296 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1297 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1298 retry_lookup: 1299 dn = d_lookup(parent, &dname); 1300 dout("d_lookup on parent=%p name=%.*s got %p\n", 1301 parent, dname.len, dname.name, dn); 1302 1303 if (!dn) { 1304 dn = d_alloc(parent, &dname); 1305 dout("d_alloc %p '%.*s' = %p\n", parent, 1306 dname.len, dname.name, dn); 1307 if (!dn) { 1308 dput(parent); 1309 err = -ENOMEM; 1310 goto done; 1311 } 1312 err = 0; 1313 } else if (d_really_is_positive(dn) && 1314 (ceph_ino(d_inode(dn)) != tvino.ino || 1315 ceph_snap(d_inode(dn)) != tvino.snap)) { 1316 dout(" dn %p points to wrong inode %p\n", 1317 dn, d_inode(dn)); 1318 ceph_dir_clear_ordered(dir); 1319 d_delete(dn); 1320 dput(dn); 1321 goto retry_lookup; 1322 } 1323 1324 req->r_dentry = dn; 1325 dput(parent); 1326 } 1327 } 1328 1329 if (rinfo->head->is_target) { 1330 /* Should be filled in by handle_reply */ 1331 BUG_ON(!req->r_target_inode); 1332 1333 in = req->r_target_inode; 1334 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, 1335 NULL, session, 1336 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1337 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && 1338 rinfo->head->result == 0) ? req->r_fmode : -1, 1339 &req->r_caps_reservation); 1340 if (err < 0) { 1341 pr_err("ceph_fill_inode badness %p %llx.%llx\n", 1342 in, ceph_vinop(in)); 1343 req->r_target_inode = NULL; 1344 if (in->i_state & I_NEW) 1345 discard_new_inode(in); 1346 else 1347 iput(in); 1348 goto done; 1349 } 1350 if (in->i_state & I_NEW) 1351 unlock_new_inode(in); 1352 } 1353 1354 /* 1355 * ignore null lease/binding on snapdir ENOENT, or else we 1356 * will have trouble splicing in the virtual snapdir later 1357 */ 1358 if (rinfo->head->is_dentry && 1359 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1360 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1361 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, 1362 fsc->mount_options->snapdir_name, 1363 req->r_dentry->d_name.len))) { 1364 /* 1365 * lookup link rename : null -> possibly existing inode 1366 * mknod symlink mkdir : null -> new inode 1367 * unlink : linked -> null 1368 */ 1369 struct inode *dir = req->r_parent; 1370 struct dentry *dn = req->r_dentry; 1371 bool have_dir_cap, have_lease; 1372 1373 BUG_ON(!dn); 1374 BUG_ON(!dir); 1375 BUG_ON(d_inode(dn->d_parent) != dir); 1376 1377 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1378 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1379 1380 BUG_ON(ceph_ino(dir) != dvino.ino); 1381 BUG_ON(ceph_snap(dir) != dvino.snap); 1382 1383 /* do we have a lease on the whole dir? */ 1384 have_dir_cap = 1385 (le32_to_cpu(rinfo->diri.in->cap.caps) & 1386 CEPH_CAP_FILE_SHARED); 1387 1388 /* do we have a dn lease? */ 1389 have_lease = have_dir_cap || 1390 le32_to_cpu(rinfo->dlease->duration_ms); 1391 if (!have_lease) 1392 dout("fill_trace no dentry lease or dir cap\n"); 1393 1394 /* rename? */ 1395 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { 1396 struct inode *olddir = req->r_old_dentry_dir; 1397 BUG_ON(!olddir); 1398 1399 dout(" src %p '%pd' dst %p '%pd'\n", 1400 req->r_old_dentry, 1401 req->r_old_dentry, 1402 dn, dn); 1403 dout("fill_trace doing d_move %p -> %p\n", 1404 req->r_old_dentry, dn); 1405 1406 /* d_move screws up sibling dentries' offsets */ 1407 ceph_dir_clear_ordered(dir); 1408 ceph_dir_clear_ordered(olddir); 1409 1410 d_move(req->r_old_dentry, dn); 1411 dout(" src %p '%pd' dst %p '%pd'\n", 1412 req->r_old_dentry, 1413 req->r_old_dentry, 1414 dn, dn); 1415 1416 /* ensure target dentry is invalidated, despite 1417 rehashing bug in vfs_rename_dir */ 1418 ceph_invalidate_dentry_lease(dn); 1419 1420 dout("dn %p gets new offset %lld\n", req->r_old_dentry, 1421 ceph_dentry(req->r_old_dentry)->offset); 1422 1423 /* swap r_dentry and r_old_dentry in case that 1424 * splice_dentry() gets called later. This is safe 1425 * because no other place will use them */ 1426 req->r_dentry = req->r_old_dentry; 1427 req->r_old_dentry = dn; 1428 dn = req->r_dentry; 1429 } 1430 1431 /* null dentry? */ 1432 if (!rinfo->head->is_target) { 1433 dout("fill_trace null dentry\n"); 1434 if (d_really_is_positive(dn)) { 1435 dout("d_delete %p\n", dn); 1436 ceph_dir_clear_ordered(dir); 1437 d_delete(dn); 1438 } else if (have_lease) { 1439 if (d_unhashed(dn)) 1440 d_add(dn, NULL); 1441 update_dentry_lease(dir, dn, 1442 rinfo->dlease, session, 1443 req->r_request_started); 1444 } 1445 goto done; 1446 } 1447 1448 /* attach proper inode */ 1449 if (d_really_is_negative(dn)) { 1450 ceph_dir_clear_ordered(dir); 1451 ihold(in); 1452 err = splice_dentry(&req->r_dentry, in); 1453 if (err < 0) 1454 goto done; 1455 dn = req->r_dentry; /* may have spliced */ 1456 } else if (d_really_is_positive(dn) && d_inode(dn) != in) { 1457 dout(" %p links to %p %llx.%llx, not %llx.%llx\n", 1458 dn, d_inode(dn), ceph_vinop(d_inode(dn)), 1459 ceph_vinop(in)); 1460 d_invalidate(dn); 1461 have_lease = false; 1462 } 1463 1464 if (have_lease) { 1465 update_dentry_lease(dir, dn, 1466 rinfo->dlease, session, 1467 req->r_request_started); 1468 } 1469 dout(" final dn %p\n", dn); 1470 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || 1471 req->r_op == CEPH_MDS_OP_MKSNAP) && 1472 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1473 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 1474 struct inode *dir = req->r_parent; 1475 1476 /* fill out a snapdir LOOKUPSNAP dentry */ 1477 BUG_ON(!dir); 1478 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); 1479 BUG_ON(!req->r_dentry); 1480 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry); 1481 ceph_dir_clear_ordered(dir); 1482 ihold(in); 1483 err = splice_dentry(&req->r_dentry, in); 1484 if (err < 0) 1485 goto done; 1486 } else if (rinfo->head->is_dentry && req->r_dentry) { 1487 /* parent inode is not locked, be carefull */ 1488 struct ceph_vino *ptvino = NULL; 1489 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1490 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1491 if (rinfo->head->is_target) { 1492 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1493 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1494 ptvino = &tvino; 1495 } 1496 update_dentry_lease_careful(req->r_dentry, rinfo->dlease, 1497 session, req->r_request_started, 1498 rinfo->dname, rinfo->dname_len, 1499 &dvino, ptvino); 1500 } 1501 done: 1502 dout("fill_trace done err=%d\n", err); 1503 return err; 1504 } 1505 1506 /* 1507 * Prepopulate our cache with readdir results, leases, etc. 1508 */ 1509 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, 1510 struct ceph_mds_session *session) 1511 { 1512 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1513 int i, err = 0; 1514 1515 for (i = 0; i < rinfo->dir_nr; i++) { 1516 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1517 struct ceph_vino vino; 1518 struct inode *in; 1519 int rc; 1520 1521 vino.ino = le64_to_cpu(rde->inode.in->ino); 1522 vino.snap = le64_to_cpu(rde->inode.in->snapid); 1523 1524 in = ceph_get_inode(req->r_dentry->d_sb, vino); 1525 if (IS_ERR(in)) { 1526 err = PTR_ERR(in); 1527 dout("new_inode badness got %d\n", err); 1528 continue; 1529 } 1530 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1531 -1, &req->r_caps_reservation); 1532 if (rc < 0) { 1533 pr_err("ceph_fill_inode badness on %p got %d\n", 1534 in, rc); 1535 err = rc; 1536 if (in->i_state & I_NEW) { 1537 ihold(in); 1538 discard_new_inode(in); 1539 } 1540 } else if (in->i_state & I_NEW) { 1541 unlock_new_inode(in); 1542 } 1543 1544 /* avoid calling iput_final() in mds dispatch threads */ 1545 ceph_async_iput(in); 1546 } 1547 1548 return err; 1549 } 1550 1551 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) 1552 { 1553 if (ctl->page) { 1554 kunmap(ctl->page); 1555 put_page(ctl->page); 1556 ctl->page = NULL; 1557 } 1558 } 1559 1560 static int fill_readdir_cache(struct inode *dir, struct dentry *dn, 1561 struct ceph_readdir_cache_control *ctl, 1562 struct ceph_mds_request *req) 1563 { 1564 struct ceph_inode_info *ci = ceph_inode(dir); 1565 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); 1566 unsigned idx = ctl->index % nsize; 1567 pgoff_t pgoff = ctl->index / nsize; 1568 1569 if (!ctl->page || pgoff != page_index(ctl->page)) { 1570 ceph_readdir_cache_release(ctl); 1571 if (idx == 0) 1572 ctl->page = grab_cache_page(&dir->i_data, pgoff); 1573 else 1574 ctl->page = find_lock_page(&dir->i_data, pgoff); 1575 if (!ctl->page) { 1576 ctl->index = -1; 1577 return idx == 0 ? -ENOMEM : 0; 1578 } 1579 /* reading/filling the cache are serialized by 1580 * i_mutex, no need to use page lock */ 1581 unlock_page(ctl->page); 1582 ctl->dentries = kmap(ctl->page); 1583 if (idx == 0) 1584 memset(ctl->dentries, 0, PAGE_SIZE); 1585 } 1586 1587 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && 1588 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { 1589 dout("readdir cache dn %p idx %d\n", dn, ctl->index); 1590 ctl->dentries[idx] = dn; 1591 ctl->index++; 1592 } else { 1593 dout("disable readdir cache\n"); 1594 ctl->index = -1; 1595 } 1596 return 0; 1597 } 1598 1599 int ceph_readdir_prepopulate(struct ceph_mds_request *req, 1600 struct ceph_mds_session *session) 1601 { 1602 struct dentry *parent = req->r_dentry; 1603 struct ceph_inode_info *ci = ceph_inode(d_inode(parent)); 1604 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1605 struct qstr dname; 1606 struct dentry *dn; 1607 struct inode *in; 1608 int err = 0, skipped = 0, ret, i; 1609 u32 frag = le32_to_cpu(req->r_args.readdir.frag); 1610 u32 last_hash = 0; 1611 u32 fpos_offset; 1612 struct ceph_readdir_cache_control cache_ctl = {}; 1613 1614 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) 1615 return readdir_prepopulate_inodes_only(req, session); 1616 1617 if (rinfo->hash_order) { 1618 if (req->r_path2) { 1619 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1620 req->r_path2, 1621 strlen(req->r_path2)); 1622 last_hash = ceph_frag_value(last_hash); 1623 } else if (rinfo->offset_hash) { 1624 /* mds understands offset_hash */ 1625 WARN_ON_ONCE(req->r_readdir_offset != 2); 1626 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); 1627 } 1628 } 1629 1630 if (rinfo->dir_dir && 1631 le32_to_cpu(rinfo->dir_dir->frag) != frag) { 1632 dout("readdir_prepopulate got new frag %x -> %x\n", 1633 frag, le32_to_cpu(rinfo->dir_dir->frag)); 1634 frag = le32_to_cpu(rinfo->dir_dir->frag); 1635 if (!rinfo->hash_order) 1636 req->r_readdir_offset = 2; 1637 } 1638 1639 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { 1640 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", 1641 rinfo->dir_nr, parent); 1642 } else { 1643 dout("readdir_prepopulate %d items under dn %p\n", 1644 rinfo->dir_nr, parent); 1645 if (rinfo->dir_dir) 1646 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); 1647 1648 if (ceph_frag_is_leftmost(frag) && 1649 req->r_readdir_offset == 2 && 1650 !(rinfo->hash_order && last_hash)) { 1651 /* note dir version at start of readdir so we can 1652 * tell if any dentries get dropped */ 1653 req->r_dir_release_cnt = 1654 atomic64_read(&ci->i_release_count); 1655 req->r_dir_ordered_cnt = 1656 atomic64_read(&ci->i_ordered_count); 1657 req->r_readdir_cache_idx = 0; 1658 } 1659 } 1660 1661 cache_ctl.index = req->r_readdir_cache_idx; 1662 fpos_offset = req->r_readdir_offset; 1663 1664 /* FIXME: release caps/leases if error occurs */ 1665 for (i = 0; i < rinfo->dir_nr; i++) { 1666 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1667 struct ceph_vino tvino; 1668 1669 dname.name = rde->name; 1670 dname.len = rde->name_len; 1671 dname.hash = full_name_hash(parent, dname.name, dname.len); 1672 1673 tvino.ino = le64_to_cpu(rde->inode.in->ino); 1674 tvino.snap = le64_to_cpu(rde->inode.in->snapid); 1675 1676 if (rinfo->hash_order) { 1677 u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1678 rde->name, rde->name_len); 1679 hash = ceph_frag_value(hash); 1680 if (hash != last_hash) 1681 fpos_offset = 2; 1682 last_hash = hash; 1683 rde->offset = ceph_make_fpos(hash, fpos_offset++, true); 1684 } else { 1685 rde->offset = ceph_make_fpos(frag, fpos_offset++, false); 1686 } 1687 1688 retry_lookup: 1689 dn = d_lookup(parent, &dname); 1690 dout("d_lookup on parent=%p name=%.*s got %p\n", 1691 parent, dname.len, dname.name, dn); 1692 1693 if (!dn) { 1694 dn = d_alloc(parent, &dname); 1695 dout("d_alloc %p '%.*s' = %p\n", parent, 1696 dname.len, dname.name, dn); 1697 if (!dn) { 1698 dout("d_alloc badness\n"); 1699 err = -ENOMEM; 1700 goto out; 1701 } 1702 } else if (d_really_is_positive(dn) && 1703 (ceph_ino(d_inode(dn)) != tvino.ino || 1704 ceph_snap(d_inode(dn)) != tvino.snap)) { 1705 struct ceph_dentry_info *di = ceph_dentry(dn); 1706 dout(" dn %p points to wrong inode %p\n", 1707 dn, d_inode(dn)); 1708 1709 spin_lock(&dn->d_lock); 1710 if (di->offset > 0 && 1711 di->lease_shared_gen == 1712 atomic_read(&ci->i_shared_gen)) { 1713 __ceph_dir_clear_ordered(ci); 1714 di->offset = 0; 1715 } 1716 spin_unlock(&dn->d_lock); 1717 1718 d_delete(dn); 1719 dput(dn); 1720 goto retry_lookup; 1721 } 1722 1723 /* inode */ 1724 if (d_really_is_positive(dn)) { 1725 in = d_inode(dn); 1726 } else { 1727 in = ceph_get_inode(parent->d_sb, tvino); 1728 if (IS_ERR(in)) { 1729 dout("new_inode badness\n"); 1730 d_drop(dn); 1731 dput(dn); 1732 err = PTR_ERR(in); 1733 goto out; 1734 } 1735 } 1736 1737 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1738 -1, &req->r_caps_reservation); 1739 if (ret < 0) { 1740 pr_err("ceph_fill_inode badness on %p\n", in); 1741 if (d_really_is_negative(dn)) { 1742 /* avoid calling iput_final() in mds 1743 * dispatch threads */ 1744 if (in->i_state & I_NEW) { 1745 ihold(in); 1746 discard_new_inode(in); 1747 } 1748 ceph_async_iput(in); 1749 } 1750 d_drop(dn); 1751 err = ret; 1752 goto next_item; 1753 } 1754 if (in->i_state & I_NEW) 1755 unlock_new_inode(in); 1756 1757 if (d_really_is_negative(dn)) { 1758 if (ceph_security_xattr_deadlock(in)) { 1759 dout(" skip splicing dn %p to inode %p" 1760 " (security xattr deadlock)\n", dn, in); 1761 ceph_async_iput(in); 1762 skipped++; 1763 goto next_item; 1764 } 1765 1766 err = splice_dentry(&dn, in); 1767 if (err < 0) 1768 goto next_item; 1769 } 1770 1771 ceph_dentry(dn)->offset = rde->offset; 1772 1773 update_dentry_lease(d_inode(parent), dn, 1774 rde->lease, req->r_session, 1775 req->r_request_started); 1776 1777 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { 1778 ret = fill_readdir_cache(d_inode(parent), dn, 1779 &cache_ctl, req); 1780 if (ret < 0) 1781 err = ret; 1782 } 1783 next_item: 1784 dput(dn); 1785 } 1786 out: 1787 if (err == 0 && skipped == 0) { 1788 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); 1789 req->r_readdir_cache_idx = cache_ctl.index; 1790 } 1791 ceph_readdir_cache_release(&cache_ctl); 1792 dout("readdir_prepopulate done\n"); 1793 return err; 1794 } 1795 1796 bool ceph_inode_set_size(struct inode *inode, loff_t size) 1797 { 1798 struct ceph_inode_info *ci = ceph_inode(inode); 1799 bool ret; 1800 1801 spin_lock(&ci->i_ceph_lock); 1802 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size); 1803 i_size_write(inode, size); 1804 inode->i_blocks = calc_inode_blocks(size); 1805 1806 ret = __ceph_should_report_size(ci); 1807 1808 spin_unlock(&ci->i_ceph_lock); 1809 return ret; 1810 } 1811 1812 /* 1813 * Put reference to inode, but avoid calling iput_final() in current thread. 1814 * iput_final() may wait for reahahead pages. The wait can cause deadlock in 1815 * some contexts. 1816 */ 1817 void ceph_async_iput(struct inode *inode) 1818 { 1819 if (!inode) 1820 return; 1821 for (;;) { 1822 if (atomic_add_unless(&inode->i_count, -1, 1)) 1823 break; 1824 if (queue_work(ceph_inode_to_client(inode)->inode_wq, 1825 &ceph_inode(inode)->i_work)) 1826 break; 1827 /* queue work failed, i_count must be at least 2 */ 1828 } 1829 } 1830 1831 void ceph_queue_inode_work(struct inode *inode, int work_bit) 1832 { 1833 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 1834 struct ceph_inode_info *ci = ceph_inode(inode); 1835 set_bit(work_bit, &ci->i_work_mask); 1836 1837 ihold(inode); 1838 if (queue_work(fsc->inode_wq, &ci->i_work)) { 1839 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask); 1840 } else { 1841 dout("queue_inode_work %p already queued, mask=%lx\n", 1842 inode, ci->i_work_mask); 1843 iput(inode); 1844 } 1845 } 1846 1847 static void ceph_do_invalidate_pages(struct inode *inode) 1848 { 1849 struct ceph_inode_info *ci = ceph_inode(inode); 1850 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 1851 u32 orig_gen; 1852 int check = 0; 1853 1854 mutex_lock(&ci->i_truncate_mutex); 1855 1856 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) { 1857 pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n", 1858 inode, ceph_ino(inode)); 1859 mapping_set_error(inode->i_mapping, -EIO); 1860 truncate_pagecache(inode, 0); 1861 mutex_unlock(&ci->i_truncate_mutex); 1862 goto out; 1863 } 1864 1865 spin_lock(&ci->i_ceph_lock); 1866 dout("invalidate_pages %p gen %d revoking %d\n", inode, 1867 ci->i_rdcache_gen, ci->i_rdcache_revoking); 1868 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 1869 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 1870 check = 1; 1871 spin_unlock(&ci->i_ceph_lock); 1872 mutex_unlock(&ci->i_truncate_mutex); 1873 goto out; 1874 } 1875 orig_gen = ci->i_rdcache_gen; 1876 spin_unlock(&ci->i_ceph_lock); 1877 1878 if (invalidate_inode_pages2(inode->i_mapping) < 0) { 1879 pr_err("invalidate_pages %p fails\n", inode); 1880 } 1881 1882 spin_lock(&ci->i_ceph_lock); 1883 if (orig_gen == ci->i_rdcache_gen && 1884 orig_gen == ci->i_rdcache_revoking) { 1885 dout("invalidate_pages %p gen %d successful\n", inode, 1886 ci->i_rdcache_gen); 1887 ci->i_rdcache_revoking--; 1888 check = 1; 1889 } else { 1890 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", 1891 inode, orig_gen, ci->i_rdcache_gen, 1892 ci->i_rdcache_revoking); 1893 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 1894 check = 1; 1895 } 1896 spin_unlock(&ci->i_ceph_lock); 1897 mutex_unlock(&ci->i_truncate_mutex); 1898 out: 1899 if (check) 1900 ceph_check_caps(ci, 0, NULL); 1901 } 1902 1903 /* 1904 * Make sure any pending truncation is applied before doing anything 1905 * that may depend on it. 1906 */ 1907 void __ceph_do_pending_vmtruncate(struct inode *inode) 1908 { 1909 struct ceph_inode_info *ci = ceph_inode(inode); 1910 u64 to; 1911 int wrbuffer_refs, finish = 0; 1912 1913 mutex_lock(&ci->i_truncate_mutex); 1914 retry: 1915 spin_lock(&ci->i_ceph_lock); 1916 if (ci->i_truncate_pending == 0) { 1917 dout("__do_pending_vmtruncate %p none pending\n", inode); 1918 spin_unlock(&ci->i_ceph_lock); 1919 mutex_unlock(&ci->i_truncate_mutex); 1920 return; 1921 } 1922 1923 /* 1924 * make sure any dirty snapped pages are flushed before we 1925 * possibly truncate them.. so write AND block! 1926 */ 1927 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { 1928 spin_unlock(&ci->i_ceph_lock); 1929 dout("__do_pending_vmtruncate %p flushing snaps first\n", 1930 inode); 1931 filemap_write_and_wait_range(&inode->i_data, 0, 1932 inode->i_sb->s_maxbytes); 1933 goto retry; 1934 } 1935 1936 /* there should be no reader or writer */ 1937 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); 1938 1939 to = ci->i_truncate_size; 1940 wrbuffer_refs = ci->i_wrbuffer_ref; 1941 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, 1942 ci->i_truncate_pending, to); 1943 spin_unlock(&ci->i_ceph_lock); 1944 1945 truncate_pagecache(inode, to); 1946 1947 spin_lock(&ci->i_ceph_lock); 1948 if (to == ci->i_truncate_size) { 1949 ci->i_truncate_pending = 0; 1950 finish = 1; 1951 } 1952 spin_unlock(&ci->i_ceph_lock); 1953 if (!finish) 1954 goto retry; 1955 1956 mutex_unlock(&ci->i_truncate_mutex); 1957 1958 if (wrbuffer_refs == 0) 1959 ceph_check_caps(ci, 0, NULL); 1960 1961 wake_up_all(&ci->i_cap_wq); 1962 } 1963 1964 static void ceph_inode_work(struct work_struct *work) 1965 { 1966 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, 1967 i_work); 1968 struct inode *inode = &ci->vfs_inode; 1969 1970 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { 1971 dout("writeback %p\n", inode); 1972 filemap_fdatawrite(&inode->i_data); 1973 } 1974 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) 1975 ceph_do_invalidate_pages(inode); 1976 1977 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) 1978 __ceph_do_pending_vmtruncate(inode); 1979 1980 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) 1981 ceph_check_caps(ci, 0, NULL); 1982 1983 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) 1984 ceph_flush_snaps(ci, NULL); 1985 1986 iput(inode); 1987 } 1988 1989 /* 1990 * symlinks 1991 */ 1992 static const struct inode_operations ceph_symlink_iops = { 1993 .get_link = simple_get_link, 1994 .setattr = ceph_setattr, 1995 .getattr = ceph_getattr, 1996 .listxattr = ceph_listxattr, 1997 }; 1998 1999 int __ceph_setattr(struct inode *inode, struct iattr *attr) 2000 { 2001 struct ceph_inode_info *ci = ceph_inode(inode); 2002 unsigned int ia_valid = attr->ia_valid; 2003 struct ceph_mds_request *req; 2004 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 2005 struct ceph_cap_flush *prealloc_cf; 2006 int issued; 2007 int release = 0, dirtied = 0; 2008 int mask = 0; 2009 int err = 0; 2010 int inode_dirty_flags = 0; 2011 bool lock_snap_rwsem = false; 2012 2013 prealloc_cf = ceph_alloc_cap_flush(); 2014 if (!prealloc_cf) 2015 return -ENOMEM; 2016 2017 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, 2018 USE_AUTH_MDS); 2019 if (IS_ERR(req)) { 2020 ceph_free_cap_flush(prealloc_cf); 2021 return PTR_ERR(req); 2022 } 2023 2024 spin_lock(&ci->i_ceph_lock); 2025 issued = __ceph_caps_issued(ci, NULL); 2026 2027 if (!ci->i_head_snapc && 2028 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { 2029 lock_snap_rwsem = true; 2030 if (!down_read_trylock(&mdsc->snap_rwsem)) { 2031 spin_unlock(&ci->i_ceph_lock); 2032 down_read(&mdsc->snap_rwsem); 2033 spin_lock(&ci->i_ceph_lock); 2034 issued = __ceph_caps_issued(ci, NULL); 2035 } 2036 } 2037 2038 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); 2039 2040 if (ia_valid & ATTR_UID) { 2041 dout("setattr %p uid %d -> %d\n", inode, 2042 from_kuid(&init_user_ns, inode->i_uid), 2043 from_kuid(&init_user_ns, attr->ia_uid)); 2044 if (issued & CEPH_CAP_AUTH_EXCL) { 2045 inode->i_uid = attr->ia_uid; 2046 dirtied |= CEPH_CAP_AUTH_EXCL; 2047 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2048 !uid_eq(attr->ia_uid, inode->i_uid)) { 2049 req->r_args.setattr.uid = cpu_to_le32( 2050 from_kuid(&init_user_ns, attr->ia_uid)); 2051 mask |= CEPH_SETATTR_UID; 2052 release |= CEPH_CAP_AUTH_SHARED; 2053 } 2054 } 2055 if (ia_valid & ATTR_GID) { 2056 dout("setattr %p gid %d -> %d\n", inode, 2057 from_kgid(&init_user_ns, inode->i_gid), 2058 from_kgid(&init_user_ns, attr->ia_gid)); 2059 if (issued & CEPH_CAP_AUTH_EXCL) { 2060 inode->i_gid = attr->ia_gid; 2061 dirtied |= CEPH_CAP_AUTH_EXCL; 2062 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2063 !gid_eq(attr->ia_gid, inode->i_gid)) { 2064 req->r_args.setattr.gid = cpu_to_le32( 2065 from_kgid(&init_user_ns, attr->ia_gid)); 2066 mask |= CEPH_SETATTR_GID; 2067 release |= CEPH_CAP_AUTH_SHARED; 2068 } 2069 } 2070 if (ia_valid & ATTR_MODE) { 2071 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, 2072 attr->ia_mode); 2073 if (issued & CEPH_CAP_AUTH_EXCL) { 2074 inode->i_mode = attr->ia_mode; 2075 dirtied |= CEPH_CAP_AUTH_EXCL; 2076 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2077 attr->ia_mode != inode->i_mode) { 2078 inode->i_mode = attr->ia_mode; 2079 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); 2080 mask |= CEPH_SETATTR_MODE; 2081 release |= CEPH_CAP_AUTH_SHARED; 2082 } 2083 } 2084 2085 if (ia_valid & ATTR_ATIME) { 2086 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode, 2087 inode->i_atime.tv_sec, inode->i_atime.tv_nsec, 2088 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); 2089 if (issued & CEPH_CAP_FILE_EXCL) { 2090 ci->i_time_warp_seq++; 2091 inode->i_atime = attr->ia_atime; 2092 dirtied |= CEPH_CAP_FILE_EXCL; 2093 } else if ((issued & CEPH_CAP_FILE_WR) && 2094 timespec64_compare(&inode->i_atime, 2095 &attr->ia_atime) < 0) { 2096 inode->i_atime = attr->ia_atime; 2097 dirtied |= CEPH_CAP_FILE_WR; 2098 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2099 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) { 2100 ceph_encode_timespec64(&req->r_args.setattr.atime, 2101 &attr->ia_atime); 2102 mask |= CEPH_SETATTR_ATIME; 2103 release |= CEPH_CAP_FILE_SHARED | 2104 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2105 } 2106 } 2107 if (ia_valid & ATTR_SIZE) { 2108 dout("setattr %p size %lld -> %lld\n", inode, 2109 inode->i_size, attr->ia_size); 2110 if ((issued & CEPH_CAP_FILE_EXCL) && 2111 attr->ia_size > inode->i_size) { 2112 i_size_write(inode, attr->ia_size); 2113 inode->i_blocks = calc_inode_blocks(attr->ia_size); 2114 ci->i_reported_size = attr->ia_size; 2115 dirtied |= CEPH_CAP_FILE_EXCL; 2116 ia_valid |= ATTR_MTIME; 2117 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2118 attr->ia_size != inode->i_size) { 2119 req->r_args.setattr.size = cpu_to_le64(attr->ia_size); 2120 req->r_args.setattr.old_size = 2121 cpu_to_le64(inode->i_size); 2122 mask |= CEPH_SETATTR_SIZE; 2123 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | 2124 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2125 } 2126 } 2127 if (ia_valid & ATTR_MTIME) { 2128 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode, 2129 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 2130 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); 2131 if (issued & CEPH_CAP_FILE_EXCL) { 2132 ci->i_time_warp_seq++; 2133 inode->i_mtime = attr->ia_mtime; 2134 dirtied |= CEPH_CAP_FILE_EXCL; 2135 } else if ((issued & CEPH_CAP_FILE_WR) && 2136 timespec64_compare(&inode->i_mtime, 2137 &attr->ia_mtime) < 0) { 2138 inode->i_mtime = attr->ia_mtime; 2139 dirtied |= CEPH_CAP_FILE_WR; 2140 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2141 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) { 2142 ceph_encode_timespec64(&req->r_args.setattr.mtime, 2143 &attr->ia_mtime); 2144 mask |= CEPH_SETATTR_MTIME; 2145 release |= CEPH_CAP_FILE_SHARED | 2146 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2147 } 2148 } 2149 2150 /* these do nothing */ 2151 if (ia_valid & ATTR_CTIME) { 2152 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| 2153 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; 2154 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode, 2155 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 2156 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, 2157 only ? "ctime only" : "ignored"); 2158 if (only) { 2159 /* 2160 * if kernel wants to dirty ctime but nothing else, 2161 * we need to choose a cap to dirty under, or do 2162 * a almost-no-op setattr 2163 */ 2164 if (issued & CEPH_CAP_AUTH_EXCL) 2165 dirtied |= CEPH_CAP_AUTH_EXCL; 2166 else if (issued & CEPH_CAP_FILE_EXCL) 2167 dirtied |= CEPH_CAP_FILE_EXCL; 2168 else if (issued & CEPH_CAP_XATTR_EXCL) 2169 dirtied |= CEPH_CAP_XATTR_EXCL; 2170 else 2171 mask |= CEPH_SETATTR_CTIME; 2172 } 2173 } 2174 if (ia_valid & ATTR_FILE) 2175 dout("setattr %p ATTR_FILE ... hrm!\n", inode); 2176 2177 if (dirtied) { 2178 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, 2179 &prealloc_cf); 2180 inode->i_ctime = attr->ia_ctime; 2181 } 2182 2183 release &= issued; 2184 spin_unlock(&ci->i_ceph_lock); 2185 if (lock_snap_rwsem) 2186 up_read(&mdsc->snap_rwsem); 2187 2188 if (inode_dirty_flags) 2189 __mark_inode_dirty(inode, inode_dirty_flags); 2190 2191 2192 if (mask) { 2193 req->r_inode = inode; 2194 ihold(inode); 2195 req->r_inode_drop = release; 2196 req->r_args.setattr.mask = cpu_to_le32(mask); 2197 req->r_num_caps = 1; 2198 req->r_stamp = attr->ia_ctime; 2199 err = ceph_mdsc_do_request(mdsc, NULL, req); 2200 } 2201 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, 2202 ceph_cap_string(dirtied), mask); 2203 2204 ceph_mdsc_put_request(req); 2205 ceph_free_cap_flush(prealloc_cf); 2206 2207 if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) 2208 __ceph_do_pending_vmtruncate(inode); 2209 2210 return err; 2211 } 2212 2213 /* 2214 * setattr 2215 */ 2216 int ceph_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 2217 struct iattr *attr) 2218 { 2219 struct inode *inode = d_inode(dentry); 2220 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 2221 int err; 2222 2223 if (ceph_snap(inode) != CEPH_NOSNAP) 2224 return -EROFS; 2225 2226 err = setattr_prepare(&init_user_ns, dentry, attr); 2227 if (err != 0) 2228 return err; 2229 2230 if ((attr->ia_valid & ATTR_SIZE) && 2231 attr->ia_size > max(inode->i_size, fsc->max_file_size)) 2232 return -EFBIG; 2233 2234 if ((attr->ia_valid & ATTR_SIZE) && 2235 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) 2236 return -EDQUOT; 2237 2238 err = __ceph_setattr(inode, attr); 2239 2240 if (err >= 0 && (attr->ia_valid & ATTR_MODE)) 2241 err = posix_acl_chmod(&init_user_ns, inode, attr->ia_mode); 2242 2243 return err; 2244 } 2245 2246 /* 2247 * Verify that we have a lease on the given mask. If not, 2248 * do a getattr against an mds. 2249 */ 2250 int __ceph_do_getattr(struct inode *inode, struct page *locked_page, 2251 int mask, bool force) 2252 { 2253 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 2254 struct ceph_mds_client *mdsc = fsc->mdsc; 2255 struct ceph_mds_request *req; 2256 int mode; 2257 int err; 2258 2259 if (ceph_snap(inode) == CEPH_SNAPDIR) { 2260 dout("do_getattr inode %p SNAPDIR\n", inode); 2261 return 0; 2262 } 2263 2264 dout("do_getattr inode %p mask %s mode 0%o\n", 2265 inode, ceph_cap_string(mask), inode->i_mode); 2266 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) 2267 return 0; 2268 2269 mode = (mask & CEPH_STAT_RSTAT) ? USE_AUTH_MDS : USE_ANY_MDS; 2270 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); 2271 if (IS_ERR(req)) 2272 return PTR_ERR(req); 2273 req->r_inode = inode; 2274 ihold(inode); 2275 req->r_num_caps = 1; 2276 req->r_args.getattr.mask = cpu_to_le32(mask); 2277 req->r_locked_page = locked_page; 2278 err = ceph_mdsc_do_request(mdsc, NULL, req); 2279 if (locked_page && err == 0) { 2280 u64 inline_version = req->r_reply_info.targeti.inline_version; 2281 if (inline_version == 0) { 2282 /* the reply is supposed to contain inline data */ 2283 err = -EINVAL; 2284 } else if (inline_version == CEPH_INLINE_NONE) { 2285 err = -ENODATA; 2286 } else { 2287 err = req->r_reply_info.targeti.inline_len; 2288 } 2289 } 2290 ceph_mdsc_put_request(req); 2291 dout("do_getattr result=%d\n", err); 2292 return err; 2293 } 2294 2295 2296 /* 2297 * Check inode permissions. We verify we have a valid value for 2298 * the AUTH cap, then call the generic handler. 2299 */ 2300 int ceph_permission(struct user_namespace *mnt_userns, struct inode *inode, 2301 int mask) 2302 { 2303 int err; 2304 2305 if (mask & MAY_NOT_BLOCK) 2306 return -ECHILD; 2307 2308 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); 2309 2310 if (!err) 2311 err = generic_permission(&init_user_ns, inode, mask); 2312 return err; 2313 } 2314 2315 /* Craft a mask of needed caps given a set of requested statx attrs. */ 2316 static int statx_to_caps(u32 want, umode_t mode) 2317 { 2318 int mask = 0; 2319 2320 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME)) 2321 mask |= CEPH_CAP_AUTH_SHARED; 2322 2323 if (want & (STATX_NLINK|STATX_CTIME)) { 2324 /* 2325 * The link count for directories depends on inode->i_subdirs, 2326 * and that is only updated when Fs caps are held. 2327 */ 2328 if (S_ISDIR(mode)) 2329 mask |= CEPH_CAP_FILE_SHARED; 2330 else 2331 mask |= CEPH_CAP_LINK_SHARED; 2332 } 2333 2334 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE| 2335 STATX_BLOCKS)) 2336 mask |= CEPH_CAP_FILE_SHARED; 2337 2338 if (want & (STATX_CTIME)) 2339 mask |= CEPH_CAP_XATTR_SHARED; 2340 2341 return mask; 2342 } 2343 2344 /* 2345 * Get all the attributes. If we have sufficient caps for the requested attrs, 2346 * then we can avoid talking to the MDS at all. 2347 */ 2348 int ceph_getattr(struct user_namespace *mnt_userns, const struct path *path, 2349 struct kstat *stat, u32 request_mask, unsigned int flags) 2350 { 2351 struct inode *inode = d_inode(path->dentry); 2352 struct ceph_inode_info *ci = ceph_inode(inode); 2353 u32 valid_mask = STATX_BASIC_STATS; 2354 int err = 0; 2355 2356 /* Skip the getattr altogether if we're asked not to sync */ 2357 if (!(flags & AT_STATX_DONT_SYNC)) { 2358 err = ceph_do_getattr(inode, 2359 statx_to_caps(request_mask, inode->i_mode), 2360 flags & AT_STATX_FORCE_SYNC); 2361 if (err) 2362 return err; 2363 } 2364 2365 generic_fillattr(&init_user_ns, inode, stat); 2366 stat->ino = ceph_present_inode(inode); 2367 2368 /* 2369 * btime on newly-allocated inodes is 0, so if this is still set to 2370 * that, then assume that it's not valid. 2371 */ 2372 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { 2373 stat->btime = ci->i_btime; 2374 valid_mask |= STATX_BTIME; 2375 } 2376 2377 if (ceph_snap(inode) == CEPH_NOSNAP) 2378 stat->dev = inode->i_sb->s_dev; 2379 else 2380 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; 2381 2382 if (S_ISDIR(inode->i_mode)) { 2383 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), 2384 RBYTES)) 2385 stat->size = ci->i_rbytes; 2386 else 2387 stat->size = ci->i_files + ci->i_subdirs; 2388 stat->blocks = 0; 2389 stat->blksize = 65536; 2390 /* 2391 * Some applications rely on the number of st_nlink 2392 * value on directories to be either 0 (if unlinked) 2393 * or 2 + number of subdirectories. 2394 */ 2395 if (stat->nlink == 1) 2396 /* '.' + '..' + subdirs */ 2397 stat->nlink = 1 + 1 + ci->i_subdirs; 2398 } 2399 2400 stat->result_mask = request_mask & valid_mask; 2401 return err; 2402 } 2403