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