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 bool is_nokey = false; 1410 struct qstr dname; 1411 struct dentry *dn, *parent; 1412 struct fscrypt_str oname = FSTR_INIT(NULL, 0); 1413 struct ceph_fname fname = { .dir = dir, 1414 .name = rinfo->dname, 1415 .ctext = rinfo->altname, 1416 .name_len = rinfo->dname_len, 1417 .ctext_len = rinfo->altname_len }; 1418 1419 BUG_ON(!rinfo->head->is_target); 1420 BUG_ON(req->r_dentry); 1421 1422 parent = d_find_any_alias(dir); 1423 BUG_ON(!parent); 1424 1425 err = ceph_fname_alloc_buffer(dir, &oname); 1426 if (err < 0) { 1427 dput(parent); 1428 goto done; 1429 } 1430 1431 err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey); 1432 if (err < 0) { 1433 dput(parent); 1434 ceph_fname_free_buffer(dir, &oname); 1435 goto done; 1436 } 1437 dname.name = oname.name; 1438 dname.len = oname.len; 1439 dname.hash = full_name_hash(parent, dname.name, dname.len); 1440 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1441 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1442 retry_lookup: 1443 dn = d_lookup(parent, &dname); 1444 dout("d_lookup on parent=%p name=%.*s got %p\n", 1445 parent, dname.len, dname.name, dn); 1446 1447 if (!dn) { 1448 dn = d_alloc(parent, &dname); 1449 dout("d_alloc %p '%.*s' = %p\n", parent, 1450 dname.len, dname.name, dn); 1451 if (!dn) { 1452 dput(parent); 1453 ceph_fname_free_buffer(dir, &oname); 1454 err = -ENOMEM; 1455 goto done; 1456 } 1457 if (is_nokey) { 1458 spin_lock(&dn->d_lock); 1459 dn->d_flags |= DCACHE_NOKEY_NAME; 1460 spin_unlock(&dn->d_lock); 1461 } 1462 err = 0; 1463 } else if (d_really_is_positive(dn) && 1464 (ceph_ino(d_inode(dn)) != tvino.ino || 1465 ceph_snap(d_inode(dn)) != tvino.snap)) { 1466 dout(" dn %p points to wrong inode %p\n", 1467 dn, d_inode(dn)); 1468 ceph_dir_clear_ordered(dir); 1469 d_delete(dn); 1470 dput(dn); 1471 goto retry_lookup; 1472 } 1473 ceph_fname_free_buffer(dir, &oname); 1474 1475 req->r_dentry = dn; 1476 dput(parent); 1477 } 1478 } 1479 1480 if (rinfo->head->is_target) { 1481 /* Should be filled in by handle_reply */ 1482 BUG_ON(!req->r_target_inode); 1483 1484 in = req->r_target_inode; 1485 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, 1486 NULL, session, 1487 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1488 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && 1489 rinfo->head->result == 0) ? req->r_fmode : -1, 1490 &req->r_caps_reservation); 1491 if (err < 0) { 1492 pr_err("ceph_fill_inode badness %p %llx.%llx\n", 1493 in, ceph_vinop(in)); 1494 req->r_target_inode = NULL; 1495 if (in->i_state & I_NEW) 1496 discard_new_inode(in); 1497 else 1498 iput(in); 1499 goto done; 1500 } 1501 if (in->i_state & I_NEW) 1502 unlock_new_inode(in); 1503 } 1504 1505 /* 1506 * ignore null lease/binding on snapdir ENOENT, or else we 1507 * will have trouble splicing in the virtual snapdir later 1508 */ 1509 if (rinfo->head->is_dentry && 1510 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1511 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1512 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, 1513 fsc->mount_options->snapdir_name, 1514 req->r_dentry->d_name.len))) { 1515 /* 1516 * lookup link rename : null -> possibly existing inode 1517 * mknod symlink mkdir : null -> new inode 1518 * unlink : linked -> null 1519 */ 1520 struct inode *dir = req->r_parent; 1521 struct dentry *dn = req->r_dentry; 1522 bool have_dir_cap, have_lease; 1523 1524 BUG_ON(!dn); 1525 BUG_ON(!dir); 1526 BUG_ON(d_inode(dn->d_parent) != dir); 1527 1528 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1529 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1530 1531 BUG_ON(ceph_ino(dir) != dvino.ino); 1532 BUG_ON(ceph_snap(dir) != dvino.snap); 1533 1534 /* do we have a lease on the whole dir? */ 1535 have_dir_cap = 1536 (le32_to_cpu(rinfo->diri.in->cap.caps) & 1537 CEPH_CAP_FILE_SHARED); 1538 1539 /* do we have a dn lease? */ 1540 have_lease = have_dir_cap || 1541 le32_to_cpu(rinfo->dlease->duration_ms); 1542 if (!have_lease) 1543 dout("fill_trace no dentry lease or dir cap\n"); 1544 1545 /* rename? */ 1546 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { 1547 struct inode *olddir = req->r_old_dentry_dir; 1548 BUG_ON(!olddir); 1549 1550 dout(" src %p '%pd' dst %p '%pd'\n", 1551 req->r_old_dentry, 1552 req->r_old_dentry, 1553 dn, dn); 1554 dout("fill_trace doing d_move %p -> %p\n", 1555 req->r_old_dentry, dn); 1556 1557 /* d_move screws up sibling dentries' offsets */ 1558 ceph_dir_clear_ordered(dir); 1559 ceph_dir_clear_ordered(olddir); 1560 1561 d_move(req->r_old_dentry, dn); 1562 dout(" src %p '%pd' dst %p '%pd'\n", 1563 req->r_old_dentry, 1564 req->r_old_dentry, 1565 dn, dn); 1566 1567 /* ensure target dentry is invalidated, despite 1568 rehashing bug in vfs_rename_dir */ 1569 ceph_invalidate_dentry_lease(dn); 1570 1571 dout("dn %p gets new offset %lld\n", req->r_old_dentry, 1572 ceph_dentry(req->r_old_dentry)->offset); 1573 1574 /* swap r_dentry and r_old_dentry in case that 1575 * splice_dentry() gets called later. This is safe 1576 * because no other place will use them */ 1577 req->r_dentry = req->r_old_dentry; 1578 req->r_old_dentry = dn; 1579 dn = req->r_dentry; 1580 } 1581 1582 /* null dentry? */ 1583 if (!rinfo->head->is_target) { 1584 dout("fill_trace null dentry\n"); 1585 if (d_really_is_positive(dn)) { 1586 dout("d_delete %p\n", dn); 1587 ceph_dir_clear_ordered(dir); 1588 d_delete(dn); 1589 } else if (have_lease) { 1590 if (d_unhashed(dn)) 1591 d_add(dn, NULL); 1592 } 1593 1594 if (!d_unhashed(dn) && have_lease) 1595 update_dentry_lease(dir, dn, 1596 rinfo->dlease, session, 1597 req->r_request_started); 1598 goto done; 1599 } 1600 1601 /* attach proper inode */ 1602 if (d_really_is_negative(dn)) { 1603 ceph_dir_clear_ordered(dir); 1604 ihold(in); 1605 err = splice_dentry(&req->r_dentry, in); 1606 if (err < 0) 1607 goto done; 1608 dn = req->r_dentry; /* may have spliced */ 1609 } else if (d_really_is_positive(dn) && d_inode(dn) != in) { 1610 dout(" %p links to %p %llx.%llx, not %llx.%llx\n", 1611 dn, d_inode(dn), ceph_vinop(d_inode(dn)), 1612 ceph_vinop(in)); 1613 d_invalidate(dn); 1614 have_lease = false; 1615 } 1616 1617 if (have_lease) { 1618 update_dentry_lease(dir, dn, 1619 rinfo->dlease, session, 1620 req->r_request_started); 1621 } 1622 dout(" final dn %p\n", dn); 1623 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || 1624 req->r_op == CEPH_MDS_OP_MKSNAP) && 1625 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1626 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 1627 struct inode *dir = req->r_parent; 1628 1629 /* fill out a snapdir LOOKUPSNAP dentry */ 1630 BUG_ON(!dir); 1631 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); 1632 BUG_ON(!req->r_dentry); 1633 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry); 1634 ceph_dir_clear_ordered(dir); 1635 ihold(in); 1636 err = splice_dentry(&req->r_dentry, in); 1637 if (err < 0) 1638 goto done; 1639 } else if (rinfo->head->is_dentry && req->r_dentry) { 1640 /* parent inode is not locked, be carefull */ 1641 struct ceph_vino *ptvino = NULL; 1642 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1643 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1644 if (rinfo->head->is_target) { 1645 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1646 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1647 ptvino = &tvino; 1648 } 1649 update_dentry_lease_careful(req->r_dentry, rinfo->dlease, 1650 session, req->r_request_started, 1651 rinfo->dname, rinfo->dname_len, 1652 &dvino, ptvino); 1653 } 1654 done: 1655 dout("fill_trace done err=%d\n", err); 1656 return err; 1657 } 1658 1659 /* 1660 * Prepopulate our cache with readdir results, leases, etc. 1661 */ 1662 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, 1663 struct ceph_mds_session *session) 1664 { 1665 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1666 int i, err = 0; 1667 1668 for (i = 0; i < rinfo->dir_nr; i++) { 1669 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1670 struct ceph_vino vino; 1671 struct inode *in; 1672 int rc; 1673 1674 vino.ino = le64_to_cpu(rde->inode.in->ino); 1675 vino.snap = le64_to_cpu(rde->inode.in->snapid); 1676 1677 in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL); 1678 if (IS_ERR(in)) { 1679 err = PTR_ERR(in); 1680 dout("new_inode badness got %d\n", err); 1681 continue; 1682 } 1683 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1684 -1, &req->r_caps_reservation); 1685 if (rc < 0) { 1686 pr_err("ceph_fill_inode badness on %p got %d\n", 1687 in, rc); 1688 err = rc; 1689 if (in->i_state & I_NEW) { 1690 ihold(in); 1691 discard_new_inode(in); 1692 } 1693 } else if (in->i_state & I_NEW) { 1694 unlock_new_inode(in); 1695 } 1696 1697 iput(in); 1698 } 1699 1700 return err; 1701 } 1702 1703 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) 1704 { 1705 if (ctl->page) { 1706 kunmap(ctl->page); 1707 put_page(ctl->page); 1708 ctl->page = NULL; 1709 } 1710 } 1711 1712 static int fill_readdir_cache(struct inode *dir, struct dentry *dn, 1713 struct ceph_readdir_cache_control *ctl, 1714 struct ceph_mds_request *req) 1715 { 1716 struct ceph_inode_info *ci = ceph_inode(dir); 1717 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); 1718 unsigned idx = ctl->index % nsize; 1719 pgoff_t pgoff = ctl->index / nsize; 1720 1721 if (!ctl->page || pgoff != page_index(ctl->page)) { 1722 ceph_readdir_cache_release(ctl); 1723 if (idx == 0) 1724 ctl->page = grab_cache_page(&dir->i_data, pgoff); 1725 else 1726 ctl->page = find_lock_page(&dir->i_data, pgoff); 1727 if (!ctl->page) { 1728 ctl->index = -1; 1729 return idx == 0 ? -ENOMEM : 0; 1730 } 1731 /* reading/filling the cache are serialized by 1732 * i_rwsem, no need to use page lock */ 1733 unlock_page(ctl->page); 1734 ctl->dentries = kmap(ctl->page); 1735 if (idx == 0) 1736 memset(ctl->dentries, 0, PAGE_SIZE); 1737 } 1738 1739 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && 1740 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { 1741 dout("readdir cache dn %p idx %d\n", dn, ctl->index); 1742 ctl->dentries[idx] = dn; 1743 ctl->index++; 1744 } else { 1745 dout("disable readdir cache\n"); 1746 ctl->index = -1; 1747 } 1748 return 0; 1749 } 1750 1751 int ceph_readdir_prepopulate(struct ceph_mds_request *req, 1752 struct ceph_mds_session *session) 1753 { 1754 struct dentry *parent = req->r_dentry; 1755 struct ceph_inode_info *ci = ceph_inode(d_inode(parent)); 1756 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1757 struct qstr dname; 1758 struct dentry *dn; 1759 struct inode *in; 1760 int err = 0, skipped = 0, ret, i; 1761 u32 frag = le32_to_cpu(req->r_args.readdir.frag); 1762 u32 last_hash = 0; 1763 u32 fpos_offset; 1764 struct ceph_readdir_cache_control cache_ctl = {}; 1765 1766 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) 1767 return readdir_prepopulate_inodes_only(req, session); 1768 1769 if (rinfo->hash_order) { 1770 if (req->r_path2) { 1771 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1772 req->r_path2, 1773 strlen(req->r_path2)); 1774 last_hash = ceph_frag_value(last_hash); 1775 } else if (rinfo->offset_hash) { 1776 /* mds understands offset_hash */ 1777 WARN_ON_ONCE(req->r_readdir_offset != 2); 1778 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); 1779 } 1780 } 1781 1782 if (rinfo->dir_dir && 1783 le32_to_cpu(rinfo->dir_dir->frag) != frag) { 1784 dout("readdir_prepopulate got new frag %x -> %x\n", 1785 frag, le32_to_cpu(rinfo->dir_dir->frag)); 1786 frag = le32_to_cpu(rinfo->dir_dir->frag); 1787 if (!rinfo->hash_order) 1788 req->r_readdir_offset = 2; 1789 } 1790 1791 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { 1792 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", 1793 rinfo->dir_nr, parent); 1794 } else { 1795 dout("readdir_prepopulate %d items under dn %p\n", 1796 rinfo->dir_nr, parent); 1797 if (rinfo->dir_dir) 1798 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); 1799 1800 if (ceph_frag_is_leftmost(frag) && 1801 req->r_readdir_offset == 2 && 1802 !(rinfo->hash_order && last_hash)) { 1803 /* note dir version at start of readdir so we can 1804 * tell if any dentries get dropped */ 1805 req->r_dir_release_cnt = 1806 atomic64_read(&ci->i_release_count); 1807 req->r_dir_ordered_cnt = 1808 atomic64_read(&ci->i_ordered_count); 1809 req->r_readdir_cache_idx = 0; 1810 } 1811 } 1812 1813 cache_ctl.index = req->r_readdir_cache_idx; 1814 fpos_offset = req->r_readdir_offset; 1815 1816 /* FIXME: release caps/leases if error occurs */ 1817 for (i = 0; i < rinfo->dir_nr; i++) { 1818 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1819 struct ceph_vino tvino; 1820 1821 dname.name = rde->name; 1822 dname.len = rde->name_len; 1823 dname.hash = full_name_hash(parent, dname.name, dname.len); 1824 1825 tvino.ino = le64_to_cpu(rde->inode.in->ino); 1826 tvino.snap = le64_to_cpu(rde->inode.in->snapid); 1827 1828 if (rinfo->hash_order) { 1829 u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1830 rde->name, rde->name_len); 1831 hash = ceph_frag_value(hash); 1832 if (hash != last_hash) 1833 fpos_offset = 2; 1834 last_hash = hash; 1835 rde->offset = ceph_make_fpos(hash, fpos_offset++, true); 1836 } else { 1837 rde->offset = ceph_make_fpos(frag, fpos_offset++, false); 1838 } 1839 1840 retry_lookup: 1841 dn = d_lookup(parent, &dname); 1842 dout("d_lookup on parent=%p name=%.*s got %p\n", 1843 parent, dname.len, dname.name, dn); 1844 1845 if (!dn) { 1846 dn = d_alloc(parent, &dname); 1847 dout("d_alloc %p '%.*s' = %p\n", parent, 1848 dname.len, dname.name, dn); 1849 if (!dn) { 1850 dout("d_alloc badness\n"); 1851 err = -ENOMEM; 1852 goto out; 1853 } 1854 } else if (d_really_is_positive(dn) && 1855 (ceph_ino(d_inode(dn)) != tvino.ino || 1856 ceph_snap(d_inode(dn)) != tvino.snap)) { 1857 struct ceph_dentry_info *di = ceph_dentry(dn); 1858 dout(" dn %p points to wrong inode %p\n", 1859 dn, d_inode(dn)); 1860 1861 spin_lock(&dn->d_lock); 1862 if (di->offset > 0 && 1863 di->lease_shared_gen == 1864 atomic_read(&ci->i_shared_gen)) { 1865 __ceph_dir_clear_ordered(ci); 1866 di->offset = 0; 1867 } 1868 spin_unlock(&dn->d_lock); 1869 1870 d_delete(dn); 1871 dput(dn); 1872 goto retry_lookup; 1873 } 1874 1875 /* inode */ 1876 if (d_really_is_positive(dn)) { 1877 in = d_inode(dn); 1878 } else { 1879 in = ceph_get_inode(parent->d_sb, tvino, NULL); 1880 if (IS_ERR(in)) { 1881 dout("new_inode badness\n"); 1882 d_drop(dn); 1883 dput(dn); 1884 err = PTR_ERR(in); 1885 goto out; 1886 } 1887 } 1888 1889 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1890 -1, &req->r_caps_reservation); 1891 if (ret < 0) { 1892 pr_err("ceph_fill_inode badness on %p\n", in); 1893 if (d_really_is_negative(dn)) { 1894 if (in->i_state & I_NEW) { 1895 ihold(in); 1896 discard_new_inode(in); 1897 } 1898 iput(in); 1899 } 1900 d_drop(dn); 1901 err = ret; 1902 goto next_item; 1903 } 1904 if (in->i_state & I_NEW) 1905 unlock_new_inode(in); 1906 1907 if (d_really_is_negative(dn)) { 1908 if (ceph_security_xattr_deadlock(in)) { 1909 dout(" skip splicing dn %p to inode %p" 1910 " (security xattr deadlock)\n", dn, in); 1911 iput(in); 1912 skipped++; 1913 goto next_item; 1914 } 1915 1916 err = splice_dentry(&dn, in); 1917 if (err < 0) 1918 goto next_item; 1919 } 1920 1921 ceph_dentry(dn)->offset = rde->offset; 1922 1923 update_dentry_lease(d_inode(parent), dn, 1924 rde->lease, req->r_session, 1925 req->r_request_started); 1926 1927 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { 1928 ret = fill_readdir_cache(d_inode(parent), dn, 1929 &cache_ctl, req); 1930 if (ret < 0) 1931 err = ret; 1932 } 1933 next_item: 1934 dput(dn); 1935 } 1936 out: 1937 if (err == 0 && skipped == 0) { 1938 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); 1939 req->r_readdir_cache_idx = cache_ctl.index; 1940 } 1941 ceph_readdir_cache_release(&cache_ctl); 1942 dout("readdir_prepopulate done\n"); 1943 return err; 1944 } 1945 1946 bool ceph_inode_set_size(struct inode *inode, loff_t size) 1947 { 1948 struct ceph_inode_info *ci = ceph_inode(inode); 1949 bool ret; 1950 1951 spin_lock(&ci->i_ceph_lock); 1952 dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size); 1953 i_size_write(inode, size); 1954 ceph_fscache_update(inode); 1955 inode->i_blocks = calc_inode_blocks(size); 1956 1957 ret = __ceph_should_report_size(ci); 1958 1959 spin_unlock(&ci->i_ceph_lock); 1960 1961 return ret; 1962 } 1963 1964 void ceph_queue_inode_work(struct inode *inode, int work_bit) 1965 { 1966 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 1967 struct ceph_inode_info *ci = ceph_inode(inode); 1968 set_bit(work_bit, &ci->i_work_mask); 1969 1970 ihold(inode); 1971 if (queue_work(fsc->inode_wq, &ci->i_work)) { 1972 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask); 1973 } else { 1974 dout("queue_inode_work %p already queued, mask=%lx\n", 1975 inode, ci->i_work_mask); 1976 iput(inode); 1977 } 1978 } 1979 1980 static void ceph_do_invalidate_pages(struct inode *inode) 1981 { 1982 struct ceph_inode_info *ci = ceph_inode(inode); 1983 u32 orig_gen; 1984 int check = 0; 1985 1986 ceph_fscache_invalidate(inode, false); 1987 1988 mutex_lock(&ci->i_truncate_mutex); 1989 1990 if (ceph_inode_is_shutdown(inode)) { 1991 pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n", 1992 __func__, ceph_vinop(inode)); 1993 mapping_set_error(inode->i_mapping, -EIO); 1994 truncate_pagecache(inode, 0); 1995 mutex_unlock(&ci->i_truncate_mutex); 1996 goto out; 1997 } 1998 1999 spin_lock(&ci->i_ceph_lock); 2000 dout("invalidate_pages %p gen %d revoking %d\n", inode, 2001 ci->i_rdcache_gen, ci->i_rdcache_revoking); 2002 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 2003 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 2004 check = 1; 2005 spin_unlock(&ci->i_ceph_lock); 2006 mutex_unlock(&ci->i_truncate_mutex); 2007 goto out; 2008 } 2009 orig_gen = ci->i_rdcache_gen; 2010 spin_unlock(&ci->i_ceph_lock); 2011 2012 if (invalidate_inode_pages2(inode->i_mapping) < 0) { 2013 pr_err("invalidate_inode_pages2 %llx.%llx failed\n", 2014 ceph_vinop(inode)); 2015 } 2016 2017 spin_lock(&ci->i_ceph_lock); 2018 if (orig_gen == ci->i_rdcache_gen && 2019 orig_gen == ci->i_rdcache_revoking) { 2020 dout("invalidate_pages %p gen %d successful\n", inode, 2021 ci->i_rdcache_gen); 2022 ci->i_rdcache_revoking--; 2023 check = 1; 2024 } else { 2025 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", 2026 inode, orig_gen, ci->i_rdcache_gen, 2027 ci->i_rdcache_revoking); 2028 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 2029 check = 1; 2030 } 2031 spin_unlock(&ci->i_ceph_lock); 2032 mutex_unlock(&ci->i_truncate_mutex); 2033 out: 2034 if (check) 2035 ceph_check_caps(ci, 0); 2036 } 2037 2038 /* 2039 * Make sure any pending truncation is applied before doing anything 2040 * that may depend on it. 2041 */ 2042 void __ceph_do_pending_vmtruncate(struct inode *inode) 2043 { 2044 struct ceph_inode_info *ci = ceph_inode(inode); 2045 u64 to; 2046 int wrbuffer_refs, finish = 0; 2047 2048 mutex_lock(&ci->i_truncate_mutex); 2049 retry: 2050 spin_lock(&ci->i_ceph_lock); 2051 if (ci->i_truncate_pending == 0) { 2052 dout("__do_pending_vmtruncate %p none pending\n", inode); 2053 spin_unlock(&ci->i_ceph_lock); 2054 mutex_unlock(&ci->i_truncate_mutex); 2055 return; 2056 } 2057 2058 /* 2059 * make sure any dirty snapped pages are flushed before we 2060 * possibly truncate them.. so write AND block! 2061 */ 2062 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { 2063 spin_unlock(&ci->i_ceph_lock); 2064 dout("__do_pending_vmtruncate %p flushing snaps first\n", 2065 inode); 2066 filemap_write_and_wait_range(&inode->i_data, 0, 2067 inode->i_sb->s_maxbytes); 2068 goto retry; 2069 } 2070 2071 /* there should be no reader or writer */ 2072 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); 2073 2074 to = ci->i_truncate_size; 2075 wrbuffer_refs = ci->i_wrbuffer_ref; 2076 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, 2077 ci->i_truncate_pending, to); 2078 spin_unlock(&ci->i_ceph_lock); 2079 2080 ceph_fscache_resize(inode, to); 2081 truncate_pagecache(inode, to); 2082 2083 spin_lock(&ci->i_ceph_lock); 2084 if (to == ci->i_truncate_size) { 2085 ci->i_truncate_pending = 0; 2086 finish = 1; 2087 } 2088 spin_unlock(&ci->i_ceph_lock); 2089 if (!finish) 2090 goto retry; 2091 2092 mutex_unlock(&ci->i_truncate_mutex); 2093 2094 if (wrbuffer_refs == 0) 2095 ceph_check_caps(ci, 0); 2096 2097 wake_up_all(&ci->i_cap_wq); 2098 } 2099 2100 static void ceph_inode_work(struct work_struct *work) 2101 { 2102 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, 2103 i_work); 2104 struct inode *inode = &ci->netfs.inode; 2105 2106 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { 2107 dout("writeback %p\n", inode); 2108 filemap_fdatawrite(&inode->i_data); 2109 } 2110 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) 2111 ceph_do_invalidate_pages(inode); 2112 2113 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) 2114 __ceph_do_pending_vmtruncate(inode); 2115 2116 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) 2117 ceph_check_caps(ci, 0); 2118 2119 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) 2120 ceph_flush_snaps(ci, NULL); 2121 2122 iput(inode); 2123 } 2124 2125 /* 2126 * symlinks 2127 */ 2128 static const struct inode_operations ceph_symlink_iops = { 2129 .get_link = simple_get_link, 2130 .setattr = ceph_setattr, 2131 .getattr = ceph_getattr, 2132 .listxattr = ceph_listxattr, 2133 }; 2134 2135 int __ceph_setattr(struct inode *inode, struct iattr *attr, 2136 struct ceph_iattr *cia) 2137 { 2138 struct ceph_inode_info *ci = ceph_inode(inode); 2139 unsigned int ia_valid = attr->ia_valid; 2140 struct ceph_mds_request *req; 2141 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 2142 struct ceph_cap_flush *prealloc_cf; 2143 int issued; 2144 int release = 0, dirtied = 0; 2145 int mask = 0; 2146 int err = 0; 2147 int inode_dirty_flags = 0; 2148 bool lock_snap_rwsem = false; 2149 2150 prealloc_cf = ceph_alloc_cap_flush(); 2151 if (!prealloc_cf) 2152 return -ENOMEM; 2153 2154 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, 2155 USE_AUTH_MDS); 2156 if (IS_ERR(req)) { 2157 ceph_free_cap_flush(prealloc_cf); 2158 return PTR_ERR(req); 2159 } 2160 2161 spin_lock(&ci->i_ceph_lock); 2162 issued = __ceph_caps_issued(ci, NULL); 2163 2164 if (!ci->i_head_snapc && 2165 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { 2166 lock_snap_rwsem = true; 2167 if (!down_read_trylock(&mdsc->snap_rwsem)) { 2168 spin_unlock(&ci->i_ceph_lock); 2169 down_read(&mdsc->snap_rwsem); 2170 spin_lock(&ci->i_ceph_lock); 2171 issued = __ceph_caps_issued(ci, NULL); 2172 } 2173 } 2174 2175 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); 2176 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 2177 if (cia && cia->fscrypt_auth) { 2178 u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth); 2179 2180 if (len > sizeof(*cia->fscrypt_auth)) { 2181 err = -EINVAL; 2182 spin_unlock(&ci->i_ceph_lock); 2183 goto out; 2184 } 2185 2186 dout("setattr %llx:%llx fscrypt_auth len %u to %u)\n", 2187 ceph_vinop(inode), ci->fscrypt_auth_len, len); 2188 2189 /* It should never be re-set once set */ 2190 WARN_ON_ONCE(ci->fscrypt_auth); 2191 2192 if (issued & CEPH_CAP_AUTH_EXCL) { 2193 dirtied |= CEPH_CAP_AUTH_EXCL; 2194 kfree(ci->fscrypt_auth); 2195 ci->fscrypt_auth = (u8 *)cia->fscrypt_auth; 2196 ci->fscrypt_auth_len = len; 2197 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2198 ci->fscrypt_auth_len != len || 2199 memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) { 2200 req->r_fscrypt_auth = cia->fscrypt_auth; 2201 mask |= CEPH_SETATTR_FSCRYPT_AUTH; 2202 release |= CEPH_CAP_AUTH_SHARED; 2203 } 2204 cia->fscrypt_auth = NULL; 2205 } 2206 #else 2207 if (cia && cia->fscrypt_auth) { 2208 err = -EINVAL; 2209 spin_unlock(&ci->i_ceph_lock); 2210 goto out; 2211 } 2212 #endif /* CONFIG_FS_ENCRYPTION */ 2213 2214 if (ia_valid & ATTR_UID) { 2215 dout("setattr %p uid %d -> %d\n", inode, 2216 from_kuid(&init_user_ns, inode->i_uid), 2217 from_kuid(&init_user_ns, attr->ia_uid)); 2218 if (issued & CEPH_CAP_AUTH_EXCL) { 2219 inode->i_uid = attr->ia_uid; 2220 dirtied |= CEPH_CAP_AUTH_EXCL; 2221 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2222 !uid_eq(attr->ia_uid, inode->i_uid)) { 2223 req->r_args.setattr.uid = cpu_to_le32( 2224 from_kuid(&init_user_ns, attr->ia_uid)); 2225 mask |= CEPH_SETATTR_UID; 2226 release |= CEPH_CAP_AUTH_SHARED; 2227 } 2228 } 2229 if (ia_valid & ATTR_GID) { 2230 dout("setattr %p gid %d -> %d\n", inode, 2231 from_kgid(&init_user_ns, inode->i_gid), 2232 from_kgid(&init_user_ns, attr->ia_gid)); 2233 if (issued & CEPH_CAP_AUTH_EXCL) { 2234 inode->i_gid = attr->ia_gid; 2235 dirtied |= CEPH_CAP_AUTH_EXCL; 2236 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2237 !gid_eq(attr->ia_gid, inode->i_gid)) { 2238 req->r_args.setattr.gid = cpu_to_le32( 2239 from_kgid(&init_user_ns, attr->ia_gid)); 2240 mask |= CEPH_SETATTR_GID; 2241 release |= CEPH_CAP_AUTH_SHARED; 2242 } 2243 } 2244 if (ia_valid & ATTR_MODE) { 2245 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, 2246 attr->ia_mode); 2247 if (issued & CEPH_CAP_AUTH_EXCL) { 2248 inode->i_mode = attr->ia_mode; 2249 dirtied |= CEPH_CAP_AUTH_EXCL; 2250 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2251 attr->ia_mode != inode->i_mode) { 2252 inode->i_mode = attr->ia_mode; 2253 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); 2254 mask |= CEPH_SETATTR_MODE; 2255 release |= CEPH_CAP_AUTH_SHARED; 2256 } 2257 } 2258 2259 if (ia_valid & ATTR_ATIME) { 2260 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode, 2261 inode->i_atime.tv_sec, inode->i_atime.tv_nsec, 2262 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); 2263 if (issued & CEPH_CAP_FILE_EXCL) { 2264 ci->i_time_warp_seq++; 2265 inode->i_atime = attr->ia_atime; 2266 dirtied |= CEPH_CAP_FILE_EXCL; 2267 } else if ((issued & CEPH_CAP_FILE_WR) && 2268 timespec64_compare(&inode->i_atime, 2269 &attr->ia_atime) < 0) { 2270 inode->i_atime = attr->ia_atime; 2271 dirtied |= CEPH_CAP_FILE_WR; 2272 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2273 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) { 2274 ceph_encode_timespec64(&req->r_args.setattr.atime, 2275 &attr->ia_atime); 2276 mask |= CEPH_SETATTR_ATIME; 2277 release |= CEPH_CAP_FILE_SHARED | 2278 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2279 } 2280 } 2281 if (ia_valid & ATTR_SIZE) { 2282 loff_t isize = i_size_read(inode); 2283 2284 dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size); 2285 if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) { 2286 if (attr->ia_size > isize) { 2287 i_size_write(inode, attr->ia_size); 2288 inode->i_blocks = calc_inode_blocks(attr->ia_size); 2289 ci->i_reported_size = attr->ia_size; 2290 dirtied |= CEPH_CAP_FILE_EXCL; 2291 ia_valid |= ATTR_MTIME; 2292 } 2293 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2294 attr->ia_size != isize) { 2295 req->r_args.setattr.size = cpu_to_le64(attr->ia_size); 2296 req->r_args.setattr.old_size = cpu_to_le64(isize); 2297 mask |= CEPH_SETATTR_SIZE; 2298 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | 2299 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2300 } 2301 } 2302 if (ia_valid & ATTR_MTIME) { 2303 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode, 2304 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 2305 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); 2306 if (issued & CEPH_CAP_FILE_EXCL) { 2307 ci->i_time_warp_seq++; 2308 inode->i_mtime = attr->ia_mtime; 2309 dirtied |= CEPH_CAP_FILE_EXCL; 2310 } else if ((issued & CEPH_CAP_FILE_WR) && 2311 timespec64_compare(&inode->i_mtime, 2312 &attr->ia_mtime) < 0) { 2313 inode->i_mtime = attr->ia_mtime; 2314 dirtied |= CEPH_CAP_FILE_WR; 2315 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2316 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) { 2317 ceph_encode_timespec64(&req->r_args.setattr.mtime, 2318 &attr->ia_mtime); 2319 mask |= CEPH_SETATTR_MTIME; 2320 release |= CEPH_CAP_FILE_SHARED | 2321 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2322 } 2323 } 2324 2325 /* these do nothing */ 2326 if (ia_valid & ATTR_CTIME) { 2327 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| 2328 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; 2329 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode, 2330 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 2331 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, 2332 only ? "ctime only" : "ignored"); 2333 if (only) { 2334 /* 2335 * if kernel wants to dirty ctime but nothing else, 2336 * we need to choose a cap to dirty under, or do 2337 * a almost-no-op setattr 2338 */ 2339 if (issued & CEPH_CAP_AUTH_EXCL) 2340 dirtied |= CEPH_CAP_AUTH_EXCL; 2341 else if (issued & CEPH_CAP_FILE_EXCL) 2342 dirtied |= CEPH_CAP_FILE_EXCL; 2343 else if (issued & CEPH_CAP_XATTR_EXCL) 2344 dirtied |= CEPH_CAP_XATTR_EXCL; 2345 else 2346 mask |= CEPH_SETATTR_CTIME; 2347 } 2348 } 2349 if (ia_valid & ATTR_FILE) 2350 dout("setattr %p ATTR_FILE ... hrm!\n", inode); 2351 2352 if (dirtied) { 2353 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, 2354 &prealloc_cf); 2355 inode->i_ctime = attr->ia_ctime; 2356 inode_inc_iversion_raw(inode); 2357 } 2358 2359 release &= issued; 2360 spin_unlock(&ci->i_ceph_lock); 2361 if (lock_snap_rwsem) 2362 up_read(&mdsc->snap_rwsem); 2363 2364 if (inode_dirty_flags) 2365 __mark_inode_dirty(inode, inode_dirty_flags); 2366 2367 if (mask) { 2368 req->r_inode = inode; 2369 ihold(inode); 2370 req->r_inode_drop = release; 2371 req->r_args.setattr.mask = cpu_to_le32(mask); 2372 req->r_num_caps = 1; 2373 req->r_stamp = attr->ia_ctime; 2374 err = ceph_mdsc_do_request(mdsc, NULL, req); 2375 } 2376 out: 2377 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, 2378 ceph_cap_string(dirtied), mask); 2379 2380 ceph_mdsc_put_request(req); 2381 ceph_free_cap_flush(prealloc_cf); 2382 2383 if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) 2384 __ceph_do_pending_vmtruncate(inode); 2385 2386 return err; 2387 } 2388 2389 /* 2390 * setattr 2391 */ 2392 int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2393 struct iattr *attr) 2394 { 2395 struct inode *inode = d_inode(dentry); 2396 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 2397 int err; 2398 2399 if (ceph_snap(inode) != CEPH_NOSNAP) 2400 return -EROFS; 2401 2402 if (ceph_inode_is_shutdown(inode)) 2403 return -ESTALE; 2404 2405 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 2406 if (err != 0) 2407 return err; 2408 2409 if ((attr->ia_valid & ATTR_SIZE) && 2410 attr->ia_size > max(i_size_read(inode), fsc->max_file_size)) 2411 return -EFBIG; 2412 2413 if ((attr->ia_valid & ATTR_SIZE) && 2414 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) 2415 return -EDQUOT; 2416 2417 err = __ceph_setattr(inode, attr, NULL); 2418 2419 if (err >= 0 && (attr->ia_valid & ATTR_MODE)) 2420 err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode); 2421 2422 return err; 2423 } 2424 2425 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask) 2426 { 2427 int issued = ceph_caps_issued(ceph_inode(inode)); 2428 2429 /* 2430 * If any 'x' caps is issued we can just choose the auth MDS 2431 * instead of the random replica MDSes. Because only when the 2432 * Locker is in LOCK_EXEC state will the loner client could 2433 * get the 'x' caps. And if we send the getattr requests to 2434 * any replica MDS it must auth pin and tries to rdlock from 2435 * the auth MDS, and then the auth MDS need to do the Locker 2436 * state transition to LOCK_SYNC. And after that the lock state 2437 * will change back. 2438 * 2439 * This cost much when doing the Locker state transition and 2440 * usually will need to revoke caps from clients. 2441 * 2442 * And for the 'Xs' caps for getxattr we will also choose the 2443 * auth MDS, because the MDS side code is buggy due to setxattr 2444 * won't notify the replica MDSes when the values changed and 2445 * the replica MDS will return the old values. Though we will 2446 * fix it in MDS code, but this still makes sense for old ceph. 2447 */ 2448 if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL)) 2449 || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR))) 2450 return USE_AUTH_MDS; 2451 else 2452 return USE_ANY_MDS; 2453 } 2454 2455 /* 2456 * Verify that we have a lease on the given mask. If not, 2457 * do a getattr against an mds. 2458 */ 2459 int __ceph_do_getattr(struct inode *inode, struct page *locked_page, 2460 int mask, bool force) 2461 { 2462 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 2463 struct ceph_mds_client *mdsc = fsc->mdsc; 2464 struct ceph_mds_request *req; 2465 int mode; 2466 int err; 2467 2468 if (ceph_snap(inode) == CEPH_SNAPDIR) { 2469 dout("do_getattr inode %p SNAPDIR\n", inode); 2470 return 0; 2471 } 2472 2473 dout("do_getattr inode %p mask %s mode 0%o\n", 2474 inode, ceph_cap_string(mask), inode->i_mode); 2475 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) 2476 return 0; 2477 2478 mode = ceph_try_to_choose_auth_mds(inode, mask); 2479 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); 2480 if (IS_ERR(req)) 2481 return PTR_ERR(req); 2482 req->r_inode = inode; 2483 ihold(inode); 2484 req->r_num_caps = 1; 2485 req->r_args.getattr.mask = cpu_to_le32(mask); 2486 req->r_locked_page = locked_page; 2487 err = ceph_mdsc_do_request(mdsc, NULL, req); 2488 if (locked_page && err == 0) { 2489 u64 inline_version = req->r_reply_info.targeti.inline_version; 2490 if (inline_version == 0) { 2491 /* the reply is supposed to contain inline data */ 2492 err = -EINVAL; 2493 } else if (inline_version == CEPH_INLINE_NONE || 2494 inline_version == 1) { 2495 err = -ENODATA; 2496 } else { 2497 err = req->r_reply_info.targeti.inline_len; 2498 } 2499 } 2500 ceph_mdsc_put_request(req); 2501 dout("do_getattr result=%d\n", err); 2502 return err; 2503 } 2504 2505 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value, 2506 size_t size) 2507 { 2508 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 2509 struct ceph_mds_client *mdsc = fsc->mdsc; 2510 struct ceph_mds_request *req; 2511 int mode = USE_AUTH_MDS; 2512 int err; 2513 char *xattr_value; 2514 size_t xattr_value_len; 2515 2516 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode); 2517 if (IS_ERR(req)) { 2518 err = -ENOMEM; 2519 goto out; 2520 } 2521 2522 req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR; 2523 req->r_path2 = kstrdup(name, GFP_NOFS); 2524 if (!req->r_path2) { 2525 err = -ENOMEM; 2526 goto put; 2527 } 2528 2529 ihold(inode); 2530 req->r_inode = inode; 2531 err = ceph_mdsc_do_request(mdsc, NULL, req); 2532 if (err < 0) 2533 goto put; 2534 2535 xattr_value = req->r_reply_info.xattr_info.xattr_value; 2536 xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len; 2537 2538 dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size); 2539 2540 err = (int)xattr_value_len; 2541 if (size == 0) 2542 goto put; 2543 2544 if (xattr_value_len > size) { 2545 err = -ERANGE; 2546 goto put; 2547 } 2548 2549 memcpy(value, xattr_value, xattr_value_len); 2550 put: 2551 ceph_mdsc_put_request(req); 2552 out: 2553 dout("do_getvxattr result=%d\n", err); 2554 return err; 2555 } 2556 2557 2558 /* 2559 * Check inode permissions. We verify we have a valid value for 2560 * the AUTH cap, then call the generic handler. 2561 */ 2562 int ceph_permission(struct mnt_idmap *idmap, struct inode *inode, 2563 int mask) 2564 { 2565 int err; 2566 2567 if (mask & MAY_NOT_BLOCK) 2568 return -ECHILD; 2569 2570 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); 2571 2572 if (!err) 2573 err = generic_permission(&nop_mnt_idmap, inode, mask); 2574 return err; 2575 } 2576 2577 /* Craft a mask of needed caps given a set of requested statx attrs. */ 2578 static int statx_to_caps(u32 want, umode_t mode) 2579 { 2580 int mask = 0; 2581 2582 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE)) 2583 mask |= CEPH_CAP_AUTH_SHARED; 2584 2585 if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) { 2586 /* 2587 * The link count for directories depends on inode->i_subdirs, 2588 * and that is only updated when Fs caps are held. 2589 */ 2590 if (S_ISDIR(mode)) 2591 mask |= CEPH_CAP_FILE_SHARED; 2592 else 2593 mask |= CEPH_CAP_LINK_SHARED; 2594 } 2595 2596 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE)) 2597 mask |= CEPH_CAP_FILE_SHARED; 2598 2599 if (want & (STATX_CTIME|STATX_CHANGE_COOKIE)) 2600 mask |= CEPH_CAP_XATTR_SHARED; 2601 2602 return mask; 2603 } 2604 2605 /* 2606 * Get all the attributes. If we have sufficient caps for the requested attrs, 2607 * then we can avoid talking to the MDS at all. 2608 */ 2609 int ceph_getattr(struct mnt_idmap *idmap, const struct path *path, 2610 struct kstat *stat, u32 request_mask, unsigned int flags) 2611 { 2612 struct inode *inode = d_inode(path->dentry); 2613 struct super_block *sb = inode->i_sb; 2614 struct ceph_inode_info *ci = ceph_inode(inode); 2615 u32 valid_mask = STATX_BASIC_STATS; 2616 int err = 0; 2617 2618 if (ceph_inode_is_shutdown(inode)) 2619 return -ESTALE; 2620 2621 /* Skip the getattr altogether if we're asked not to sync */ 2622 if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) { 2623 err = ceph_do_getattr(inode, 2624 statx_to_caps(request_mask, inode->i_mode), 2625 flags & AT_STATX_FORCE_SYNC); 2626 if (err) 2627 return err; 2628 } 2629 2630 generic_fillattr(&nop_mnt_idmap, inode, stat); 2631 stat->ino = ceph_present_inode(inode); 2632 2633 /* 2634 * btime on newly-allocated inodes is 0, so if this is still set to 2635 * that, then assume that it's not valid. 2636 */ 2637 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { 2638 stat->btime = ci->i_btime; 2639 valid_mask |= STATX_BTIME; 2640 } 2641 2642 if (request_mask & STATX_CHANGE_COOKIE) { 2643 stat->change_cookie = inode_peek_iversion_raw(inode); 2644 valid_mask |= STATX_CHANGE_COOKIE; 2645 } 2646 2647 if (ceph_snap(inode) == CEPH_NOSNAP) 2648 stat->dev = sb->s_dev; 2649 else 2650 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; 2651 2652 if (S_ISDIR(inode->i_mode)) { 2653 if (ceph_test_mount_opt(ceph_sb_to_client(sb), RBYTES)) { 2654 stat->size = ci->i_rbytes; 2655 } else if (ceph_snap(inode) == CEPH_SNAPDIR) { 2656 struct ceph_inode_info *pci; 2657 struct ceph_snap_realm *realm; 2658 struct inode *parent; 2659 2660 parent = ceph_lookup_inode(sb, ceph_ino(inode)); 2661 if (IS_ERR(parent)) 2662 return PTR_ERR(parent); 2663 2664 pci = ceph_inode(parent); 2665 spin_lock(&pci->i_ceph_lock); 2666 realm = pci->i_snap_realm; 2667 if (realm) 2668 stat->size = realm->num_snaps; 2669 else 2670 stat->size = 0; 2671 spin_unlock(&pci->i_ceph_lock); 2672 iput(parent); 2673 } else { 2674 stat->size = ci->i_files + ci->i_subdirs; 2675 } 2676 stat->blocks = 0; 2677 stat->blksize = 65536; 2678 /* 2679 * Some applications rely on the number of st_nlink 2680 * value on directories to be either 0 (if unlinked) 2681 * or 2 + number of subdirectories. 2682 */ 2683 if (stat->nlink == 1) 2684 /* '.' + '..' + subdirs */ 2685 stat->nlink = 1 + 1 + ci->i_subdirs; 2686 } 2687 2688 stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC; 2689 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC; 2690 stat->result_mask = request_mask & valid_mask; 2691 return err; 2692 } 2693 2694 void ceph_inode_shutdown(struct inode *inode) 2695 { 2696 struct ceph_inode_info *ci = ceph_inode(inode); 2697 struct rb_node *p; 2698 int iputs = 0; 2699 bool invalidate = false; 2700 2701 spin_lock(&ci->i_ceph_lock); 2702 ci->i_ceph_flags |= CEPH_I_SHUTDOWN; 2703 p = rb_first(&ci->i_caps); 2704 while (p) { 2705 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 2706 2707 p = rb_next(p); 2708 iputs += ceph_purge_inode_cap(inode, cap, &invalidate); 2709 } 2710 spin_unlock(&ci->i_ceph_lock); 2711 2712 if (invalidate) 2713 ceph_queue_invalidate(inode); 2714 while (iputs--) 2715 iput(inode); 2716 } 2717