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