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