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