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 __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files); 976 977 #ifdef CONFIG_FS_ENCRYPTION 978 if (iinfo->fscrypt_auth_len && (inode->i_state & I_NEW)) { 979 kfree(ci->fscrypt_auth); 980 ci->fscrypt_auth_len = iinfo->fscrypt_auth_len; 981 ci->fscrypt_auth = iinfo->fscrypt_auth; 982 iinfo->fscrypt_auth = NULL; 983 iinfo->fscrypt_auth_len = 0; 984 inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED); 985 } 986 #endif 987 988 if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && 989 (issued & CEPH_CAP_AUTH_EXCL) == 0) { 990 inode->i_mode = mode; 991 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); 992 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); 993 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, 994 from_kuid(&init_user_ns, inode->i_uid), 995 from_kgid(&init_user_ns, inode->i_gid)); 996 ceph_decode_timespec64(&ci->i_btime, &iinfo->btime); 997 ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime); 998 } 999 1000 /* directories have fl_stripe_unit set to zero */ 1001 if (IS_ENCRYPTED(inode)) 1002 inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT; 1003 else if (le32_to_cpu(info->layout.fl_stripe_unit)) 1004 inode->i_blkbits = 1005 fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; 1006 else 1007 inode->i_blkbits = CEPH_BLOCK_SHIFT; 1008 1009 if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && 1010 (issued & CEPH_CAP_LINK_EXCL) == 0) 1011 set_nlink(inode, le32_to_cpu(info->nlink)); 1012 1013 if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { 1014 /* be careful with mtime, atime, size */ 1015 ceph_decode_timespec64(&atime, &info->atime); 1016 ceph_decode_timespec64(&mtime, &info->mtime); 1017 ceph_decode_timespec64(&ctime, &info->ctime); 1018 ceph_fill_file_time(inode, issued, 1019 le32_to_cpu(info->time_warp_seq), 1020 &ctime, &mtime, &atime); 1021 } 1022 1023 if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) { 1024 ci->i_files = le64_to_cpu(info->files); 1025 ci->i_subdirs = le64_to_cpu(info->subdirs); 1026 } 1027 1028 if (new_version || 1029 (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { 1030 s64 old_pool = ci->i_layout.pool_id; 1031 struct ceph_string *old_ns; 1032 1033 ceph_file_layout_from_legacy(&ci->i_layout, &info->layout); 1034 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, 1035 lockdep_is_held(&ci->i_ceph_lock)); 1036 rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns); 1037 1038 if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) 1039 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; 1040 1041 pool_ns = old_ns; 1042 1043 queue_trunc = ceph_fill_file_size(inode, issued, 1044 le32_to_cpu(info->truncate_seq), 1045 le64_to_cpu(info->truncate_size), 1046 le64_to_cpu(info->size)); 1047 /* only update max_size on auth cap */ 1048 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && 1049 ci->i_max_size != le64_to_cpu(info->max_size)) { 1050 dout("max_size %lld -> %llu\n", ci->i_max_size, 1051 le64_to_cpu(info->max_size)); 1052 ci->i_max_size = le64_to_cpu(info->max_size); 1053 } 1054 } 1055 1056 /* layout and rstat are not tracked by capability, update them if 1057 * the inode info is from auth mds */ 1058 if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) { 1059 if (S_ISDIR(inode->i_mode)) { 1060 ci->i_dir_layout = iinfo->dir_layout; 1061 ci->i_rbytes = le64_to_cpu(info->rbytes); 1062 ci->i_rfiles = le64_to_cpu(info->rfiles); 1063 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); 1064 ci->i_dir_pin = iinfo->dir_pin; 1065 ci->i_rsnaps = iinfo->rsnaps; 1066 ceph_decode_timespec64(&ci->i_rctime, &info->rctime); 1067 } 1068 } 1069 1070 /* xattrs */ 1071 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ 1072 if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && 1073 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { 1074 if (ci->i_xattrs.blob) 1075 old_blob = ci->i_xattrs.blob; 1076 ci->i_xattrs.blob = xattr_blob; 1077 if (xattr_blob) 1078 memcpy(ci->i_xattrs.blob->vec.iov_base, 1079 iinfo->xattr_data, iinfo->xattr_len); 1080 ci->i_xattrs.version = le64_to_cpu(info->xattr_version); 1081 ceph_forget_all_cached_acls(inode); 1082 ceph_security_invalidate_secctx(inode); 1083 xattr_blob = NULL; 1084 } 1085 1086 /* finally update i_version */ 1087 if (le64_to_cpu(info->version) > ci->i_version) 1088 ci->i_version = le64_to_cpu(info->version); 1089 1090 inode->i_mapping->a_ops = &ceph_aops; 1091 1092 switch (inode->i_mode & S_IFMT) { 1093 case S_IFIFO: 1094 case S_IFBLK: 1095 case S_IFCHR: 1096 case S_IFSOCK: 1097 inode->i_blkbits = PAGE_SHIFT; 1098 init_special_inode(inode, inode->i_mode, rdev); 1099 inode->i_op = &ceph_file_iops; 1100 break; 1101 case S_IFREG: 1102 inode->i_op = &ceph_file_iops; 1103 inode->i_fop = &ceph_file_fops; 1104 break; 1105 case S_IFLNK: 1106 if (!ci->i_symlink) { 1107 u32 symlen = iinfo->symlink_len; 1108 char *sym; 1109 1110 spin_unlock(&ci->i_ceph_lock); 1111 1112 if (IS_ENCRYPTED(inode)) { 1113 if (symlen != i_size_read(inode)) 1114 pr_err("%s %llx.%llx BAD symlink size %lld\n", 1115 __func__, ceph_vinop(inode), 1116 i_size_read(inode)); 1117 1118 err = decode_encrypted_symlink(iinfo->symlink, 1119 symlen, (u8 **)&sym); 1120 if (err < 0) { 1121 pr_err("%s decoding encrypted symlink failed: %d\n", 1122 __func__, err); 1123 goto out; 1124 } 1125 symlen = err; 1126 i_size_write(inode, symlen); 1127 inode->i_blocks = calc_inode_blocks(symlen); 1128 } else { 1129 if (symlen != i_size_read(inode)) { 1130 pr_err("%s %llx.%llx BAD symlink size %lld\n", 1131 __func__, ceph_vinop(inode), 1132 i_size_read(inode)); 1133 i_size_write(inode, symlen); 1134 inode->i_blocks = calc_inode_blocks(symlen); 1135 } 1136 1137 err = -ENOMEM; 1138 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); 1139 if (!sym) 1140 goto out; 1141 } 1142 1143 spin_lock(&ci->i_ceph_lock); 1144 if (!ci->i_symlink) 1145 ci->i_symlink = sym; 1146 else 1147 kfree(sym); /* lost a race */ 1148 } 1149 1150 if (IS_ENCRYPTED(inode)) { 1151 /* 1152 * Encrypted symlinks need to be decrypted before we can 1153 * cache their targets in i_link. Don't touch it here. 1154 */ 1155 inode->i_op = &ceph_encrypted_symlink_iops; 1156 } else { 1157 inode->i_link = ci->i_symlink; 1158 inode->i_op = &ceph_symlink_iops; 1159 } 1160 break; 1161 case S_IFDIR: 1162 inode->i_op = &ceph_dir_iops; 1163 inode->i_fop = &ceph_dir_fops; 1164 break; 1165 default: 1166 pr_err("%s %llx.%llx BAD mode 0%o\n", __func__, 1167 ceph_vinop(inode), inode->i_mode); 1168 } 1169 1170 /* were we issued a capability? */ 1171 if (info_caps) { 1172 if (ceph_snap(inode) == CEPH_NOSNAP) { 1173 ceph_add_cap(inode, session, 1174 le64_to_cpu(info->cap.cap_id), 1175 info_caps, 1176 le32_to_cpu(info->cap.wanted), 1177 le32_to_cpu(info->cap.seq), 1178 le32_to_cpu(info->cap.mseq), 1179 le64_to_cpu(info->cap.realm), 1180 info->cap.flags, &new_cap); 1181 1182 /* set dir completion flag? */ 1183 if (S_ISDIR(inode->i_mode) && 1184 ci->i_files == 0 && ci->i_subdirs == 0 && 1185 (info_caps & CEPH_CAP_FILE_SHARED) && 1186 (issued & CEPH_CAP_FILE_EXCL) == 0 && 1187 !__ceph_dir_is_complete(ci)) { 1188 dout(" marking %p complete (empty)\n", inode); 1189 i_size_write(inode, 0); 1190 __ceph_dir_set_complete(ci, 1191 atomic64_read(&ci->i_release_count), 1192 atomic64_read(&ci->i_ordered_count)); 1193 } 1194 1195 wake = true; 1196 } else { 1197 dout(" %p got snap_caps %s\n", inode, 1198 ceph_cap_string(info_caps)); 1199 ci->i_snap_caps |= info_caps; 1200 } 1201 } 1202 1203 if (iinfo->inline_version > 0 && 1204 iinfo->inline_version >= ci->i_inline_version) { 1205 int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; 1206 ci->i_inline_version = iinfo->inline_version; 1207 if (ceph_has_inline_data(ci) && 1208 (locked_page || (info_caps & cache_caps))) 1209 fill_inline = true; 1210 } 1211 1212 if (cap_fmode >= 0) { 1213 if (!info_caps) 1214 pr_warn("mds issued no caps on %llx.%llx\n", 1215 ceph_vinop(inode)); 1216 __ceph_touch_fmode(ci, mdsc, cap_fmode); 1217 } 1218 1219 spin_unlock(&ci->i_ceph_lock); 1220 1221 ceph_fscache_register_inode_cookie(inode); 1222 1223 if (fill_inline) 1224 ceph_fill_inline_data(inode, locked_page, 1225 iinfo->inline_data, iinfo->inline_len); 1226 1227 if (wake) 1228 wake_up_all(&ci->i_cap_wq); 1229 1230 /* queue truncate if we saw i_size decrease */ 1231 if (queue_trunc) 1232 ceph_queue_vmtruncate(inode); 1233 1234 /* populate frag tree */ 1235 if (S_ISDIR(inode->i_mode)) 1236 ceph_fill_fragtree(inode, &info->fragtree, dirinfo); 1237 1238 /* update delegation info? */ 1239 if (dirinfo) 1240 ceph_fill_dirfrag(inode, dirinfo); 1241 1242 err = 0; 1243 out: 1244 if (new_cap) 1245 ceph_put_cap(mdsc, new_cap); 1246 ceph_buffer_put(old_blob); 1247 ceph_buffer_put(xattr_blob); 1248 ceph_put_string(pool_ns); 1249 return err; 1250 } 1251 1252 /* 1253 * caller should hold session s_mutex and dentry->d_lock. 1254 */ 1255 static void __update_dentry_lease(struct inode *dir, struct dentry *dentry, 1256 struct ceph_mds_reply_lease *lease, 1257 struct ceph_mds_session *session, 1258 unsigned long from_time, 1259 struct ceph_mds_session **old_lease_session) 1260 { 1261 struct ceph_dentry_info *di = ceph_dentry(dentry); 1262 unsigned mask = le16_to_cpu(lease->mask); 1263 long unsigned duration = le32_to_cpu(lease->duration_ms); 1264 long unsigned ttl = from_time + (duration * HZ) / 1000; 1265 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; 1266 1267 dout("update_dentry_lease %p duration %lu ms ttl %lu\n", 1268 dentry, duration, ttl); 1269 1270 /* only track leases on regular dentries */ 1271 if (ceph_snap(dir) != CEPH_NOSNAP) 1272 return; 1273 1274 if (mask & CEPH_LEASE_PRIMARY_LINK) 1275 di->flags |= CEPH_DENTRY_PRIMARY_LINK; 1276 else 1277 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; 1278 1279 di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen); 1280 if (!(mask & CEPH_LEASE_VALID)) { 1281 __ceph_dentry_dir_lease_touch(di); 1282 return; 1283 } 1284 1285 if (di->lease_gen == atomic_read(&session->s_cap_gen) && 1286 time_before(ttl, di->time)) 1287 return; /* we already have a newer lease. */ 1288 1289 if (di->lease_session && di->lease_session != session) { 1290 *old_lease_session = di->lease_session; 1291 di->lease_session = NULL; 1292 } 1293 1294 if (!di->lease_session) 1295 di->lease_session = ceph_get_mds_session(session); 1296 di->lease_gen = atomic_read(&session->s_cap_gen); 1297 di->lease_seq = le32_to_cpu(lease->seq); 1298 di->lease_renew_after = half_ttl; 1299 di->lease_renew_from = 0; 1300 di->time = ttl; 1301 1302 __ceph_dentry_lease_touch(di); 1303 } 1304 1305 static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry, 1306 struct ceph_mds_reply_lease *lease, 1307 struct ceph_mds_session *session, 1308 unsigned long from_time) 1309 { 1310 struct ceph_mds_session *old_lease_session = NULL; 1311 spin_lock(&dentry->d_lock); 1312 __update_dentry_lease(dir, dentry, lease, session, from_time, 1313 &old_lease_session); 1314 spin_unlock(&dentry->d_lock); 1315 ceph_put_mds_session(old_lease_session); 1316 } 1317 1318 /* 1319 * update dentry lease without having parent inode locked 1320 */ 1321 static void update_dentry_lease_careful(struct dentry *dentry, 1322 struct ceph_mds_reply_lease *lease, 1323 struct ceph_mds_session *session, 1324 unsigned long from_time, 1325 char *dname, u32 dname_len, 1326 struct ceph_vino *pdvino, 1327 struct ceph_vino *ptvino) 1328 1329 { 1330 struct inode *dir; 1331 struct ceph_mds_session *old_lease_session = NULL; 1332 1333 spin_lock(&dentry->d_lock); 1334 /* make sure dentry's name matches target */ 1335 if (dentry->d_name.len != dname_len || 1336 memcmp(dentry->d_name.name, dname, dname_len)) 1337 goto out_unlock; 1338 1339 dir = d_inode(dentry->d_parent); 1340 /* make sure parent matches dvino */ 1341 if (!ceph_ino_compare(dir, pdvino)) 1342 goto out_unlock; 1343 1344 /* make sure dentry's inode matches target. NULL ptvino means that 1345 * we expect a negative dentry */ 1346 if (ptvino) { 1347 if (d_really_is_negative(dentry)) 1348 goto out_unlock; 1349 if (!ceph_ino_compare(d_inode(dentry), ptvino)) 1350 goto out_unlock; 1351 } else { 1352 if (d_really_is_positive(dentry)) 1353 goto out_unlock; 1354 } 1355 1356 __update_dentry_lease(dir, dentry, lease, session, 1357 from_time, &old_lease_session); 1358 out_unlock: 1359 spin_unlock(&dentry->d_lock); 1360 ceph_put_mds_session(old_lease_session); 1361 } 1362 1363 /* 1364 * splice a dentry to an inode. 1365 * caller must hold directory i_rwsem for this to be safe. 1366 */ 1367 static int splice_dentry(struct dentry **pdn, struct inode *in) 1368 { 1369 struct dentry *dn = *pdn; 1370 struct dentry *realdn; 1371 1372 BUG_ON(d_inode(dn)); 1373 1374 if (S_ISDIR(in->i_mode)) { 1375 /* If inode is directory, d_splice_alias() below will remove 1376 * 'realdn' from its origin parent. We need to ensure that 1377 * origin parent's readdir cache will not reference 'realdn' 1378 */ 1379 realdn = d_find_any_alias(in); 1380 if (realdn) { 1381 struct ceph_dentry_info *di = ceph_dentry(realdn); 1382 spin_lock(&realdn->d_lock); 1383 1384 realdn->d_op->d_prune(realdn); 1385 1386 di->time = jiffies; 1387 di->lease_shared_gen = 0; 1388 di->offset = 0; 1389 1390 spin_unlock(&realdn->d_lock); 1391 dput(realdn); 1392 } 1393 } 1394 1395 /* dn must be unhashed */ 1396 if (!d_unhashed(dn)) 1397 d_drop(dn); 1398 realdn = d_splice_alias(in, dn); 1399 if (IS_ERR(realdn)) { 1400 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n", 1401 PTR_ERR(realdn), dn, in, ceph_vinop(in)); 1402 return PTR_ERR(realdn); 1403 } 1404 1405 if (realdn) { 1406 dout("dn %p (%d) spliced with %p (%d) " 1407 "inode %p ino %llx.%llx\n", 1408 dn, d_count(dn), 1409 realdn, d_count(realdn), 1410 d_inode(realdn), ceph_vinop(d_inode(realdn))); 1411 dput(dn); 1412 *pdn = realdn; 1413 } else { 1414 BUG_ON(!ceph_dentry(dn)); 1415 dout("dn %p attached to %p ino %llx.%llx\n", 1416 dn, d_inode(dn), ceph_vinop(d_inode(dn))); 1417 } 1418 return 0; 1419 } 1420 1421 /* 1422 * Incorporate results into the local cache. This is either just 1423 * one inode, or a directory, dentry, and possibly linked-to inode (e.g., 1424 * after a lookup). 1425 * 1426 * A reply may contain 1427 * a directory inode along with a dentry. 1428 * and/or a target inode 1429 * 1430 * Called with snap_rwsem (read). 1431 */ 1432 int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) 1433 { 1434 struct ceph_mds_session *session = req->r_session; 1435 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1436 struct inode *in = NULL; 1437 struct ceph_vino tvino, dvino; 1438 struct ceph_fs_client *fsc = ceph_sb_to_client(sb); 1439 int err = 0; 1440 1441 dout("fill_trace %p is_dentry %d is_target %d\n", req, 1442 rinfo->head->is_dentry, rinfo->head->is_target); 1443 1444 if (!rinfo->head->is_target && !rinfo->head->is_dentry) { 1445 dout("fill_trace reply is empty!\n"); 1446 if (rinfo->head->result == 0 && req->r_parent) 1447 ceph_invalidate_dir_request(req); 1448 return 0; 1449 } 1450 1451 if (rinfo->head->is_dentry) { 1452 struct inode *dir = req->r_parent; 1453 1454 if (dir) { 1455 err = ceph_fill_inode(dir, NULL, &rinfo->diri, 1456 rinfo->dirfrag, session, -1, 1457 &req->r_caps_reservation); 1458 if (err < 0) 1459 goto done; 1460 } else { 1461 WARN_ON_ONCE(1); 1462 } 1463 1464 if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME && 1465 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1466 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 1467 bool is_nokey = false; 1468 struct qstr dname; 1469 struct dentry *dn, *parent; 1470 struct fscrypt_str oname = FSTR_INIT(NULL, 0); 1471 struct ceph_fname fname = { .dir = dir, 1472 .name = rinfo->dname, 1473 .ctext = rinfo->altname, 1474 .name_len = rinfo->dname_len, 1475 .ctext_len = rinfo->altname_len }; 1476 1477 BUG_ON(!rinfo->head->is_target); 1478 BUG_ON(req->r_dentry); 1479 1480 parent = d_find_any_alias(dir); 1481 BUG_ON(!parent); 1482 1483 err = ceph_fname_alloc_buffer(dir, &oname); 1484 if (err < 0) { 1485 dput(parent); 1486 goto done; 1487 } 1488 1489 err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey); 1490 if (err < 0) { 1491 dput(parent); 1492 ceph_fname_free_buffer(dir, &oname); 1493 goto done; 1494 } 1495 dname.name = oname.name; 1496 dname.len = oname.len; 1497 dname.hash = full_name_hash(parent, dname.name, dname.len); 1498 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1499 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1500 retry_lookup: 1501 dn = d_lookup(parent, &dname); 1502 dout("d_lookup on parent=%p name=%.*s got %p\n", 1503 parent, dname.len, dname.name, dn); 1504 1505 if (!dn) { 1506 dn = d_alloc(parent, &dname); 1507 dout("d_alloc %p '%.*s' = %p\n", parent, 1508 dname.len, dname.name, dn); 1509 if (!dn) { 1510 dput(parent); 1511 ceph_fname_free_buffer(dir, &oname); 1512 err = -ENOMEM; 1513 goto done; 1514 } 1515 if (is_nokey) { 1516 spin_lock(&dn->d_lock); 1517 dn->d_flags |= DCACHE_NOKEY_NAME; 1518 spin_unlock(&dn->d_lock); 1519 } 1520 err = 0; 1521 } else if (d_really_is_positive(dn) && 1522 (ceph_ino(d_inode(dn)) != tvino.ino || 1523 ceph_snap(d_inode(dn)) != tvino.snap)) { 1524 dout(" dn %p points to wrong inode %p\n", 1525 dn, d_inode(dn)); 1526 ceph_dir_clear_ordered(dir); 1527 d_delete(dn); 1528 dput(dn); 1529 goto retry_lookup; 1530 } 1531 ceph_fname_free_buffer(dir, &oname); 1532 1533 req->r_dentry = dn; 1534 dput(parent); 1535 } 1536 } 1537 1538 if (rinfo->head->is_target) { 1539 /* Should be filled in by handle_reply */ 1540 BUG_ON(!req->r_target_inode); 1541 1542 in = req->r_target_inode; 1543 err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, 1544 NULL, session, 1545 (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1546 !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && 1547 rinfo->head->result == 0) ? req->r_fmode : -1, 1548 &req->r_caps_reservation); 1549 if (err < 0) { 1550 pr_err("ceph_fill_inode badness %p %llx.%llx\n", 1551 in, ceph_vinop(in)); 1552 req->r_target_inode = NULL; 1553 if (in->i_state & I_NEW) 1554 discard_new_inode(in); 1555 else 1556 iput(in); 1557 goto done; 1558 } 1559 if (in->i_state & I_NEW) 1560 unlock_new_inode(in); 1561 } 1562 1563 /* 1564 * ignore null lease/binding on snapdir ENOENT, or else we 1565 * will have trouble splicing in the virtual snapdir later 1566 */ 1567 if (rinfo->head->is_dentry && 1568 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && 1569 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1570 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, 1571 fsc->mount_options->snapdir_name, 1572 req->r_dentry->d_name.len))) { 1573 /* 1574 * lookup link rename : null -> possibly existing inode 1575 * mknod symlink mkdir : null -> new inode 1576 * unlink : linked -> null 1577 */ 1578 struct inode *dir = req->r_parent; 1579 struct dentry *dn = req->r_dentry; 1580 bool have_dir_cap, have_lease; 1581 1582 BUG_ON(!dn); 1583 BUG_ON(!dir); 1584 BUG_ON(d_inode(dn->d_parent) != dir); 1585 1586 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1587 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1588 1589 BUG_ON(ceph_ino(dir) != dvino.ino); 1590 BUG_ON(ceph_snap(dir) != dvino.snap); 1591 1592 /* do we have a lease on the whole dir? */ 1593 have_dir_cap = 1594 (le32_to_cpu(rinfo->diri.in->cap.caps) & 1595 CEPH_CAP_FILE_SHARED); 1596 1597 /* do we have a dn lease? */ 1598 have_lease = have_dir_cap || 1599 le32_to_cpu(rinfo->dlease->duration_ms); 1600 if (!have_lease) 1601 dout("fill_trace no dentry lease or dir cap\n"); 1602 1603 /* rename? */ 1604 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { 1605 struct inode *olddir = req->r_old_dentry_dir; 1606 BUG_ON(!olddir); 1607 1608 dout(" src %p '%pd' dst %p '%pd'\n", 1609 req->r_old_dentry, 1610 req->r_old_dentry, 1611 dn, dn); 1612 dout("fill_trace doing d_move %p -> %p\n", 1613 req->r_old_dentry, dn); 1614 1615 /* d_move screws up sibling dentries' offsets */ 1616 ceph_dir_clear_ordered(dir); 1617 ceph_dir_clear_ordered(olddir); 1618 1619 d_move(req->r_old_dentry, dn); 1620 dout(" src %p '%pd' dst %p '%pd'\n", 1621 req->r_old_dentry, 1622 req->r_old_dentry, 1623 dn, dn); 1624 1625 /* ensure target dentry is invalidated, despite 1626 rehashing bug in vfs_rename_dir */ 1627 ceph_invalidate_dentry_lease(dn); 1628 1629 dout("dn %p gets new offset %lld\n", req->r_old_dentry, 1630 ceph_dentry(req->r_old_dentry)->offset); 1631 1632 /* swap r_dentry and r_old_dentry in case that 1633 * splice_dentry() gets called later. This is safe 1634 * because no other place will use them */ 1635 req->r_dentry = req->r_old_dentry; 1636 req->r_old_dentry = dn; 1637 dn = req->r_dentry; 1638 } 1639 1640 /* null dentry? */ 1641 if (!rinfo->head->is_target) { 1642 dout("fill_trace null dentry\n"); 1643 if (d_really_is_positive(dn)) { 1644 dout("d_delete %p\n", dn); 1645 ceph_dir_clear_ordered(dir); 1646 d_delete(dn); 1647 } else if (have_lease) { 1648 if (d_unhashed(dn)) 1649 d_add(dn, NULL); 1650 } 1651 1652 if (!d_unhashed(dn) && have_lease) 1653 update_dentry_lease(dir, dn, 1654 rinfo->dlease, session, 1655 req->r_request_started); 1656 goto done; 1657 } 1658 1659 /* attach proper inode */ 1660 if (d_really_is_negative(dn)) { 1661 ceph_dir_clear_ordered(dir); 1662 ihold(in); 1663 err = splice_dentry(&req->r_dentry, in); 1664 if (err < 0) 1665 goto done; 1666 dn = req->r_dentry; /* may have spliced */ 1667 } else if (d_really_is_positive(dn) && d_inode(dn) != in) { 1668 dout(" %p links to %p %llx.%llx, not %llx.%llx\n", 1669 dn, d_inode(dn), ceph_vinop(d_inode(dn)), 1670 ceph_vinop(in)); 1671 d_invalidate(dn); 1672 have_lease = false; 1673 } 1674 1675 if (have_lease) { 1676 update_dentry_lease(dir, dn, 1677 rinfo->dlease, session, 1678 req->r_request_started); 1679 } 1680 dout(" final dn %p\n", dn); 1681 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || 1682 req->r_op == CEPH_MDS_OP_MKSNAP) && 1683 test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && 1684 !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { 1685 struct inode *dir = req->r_parent; 1686 1687 /* fill out a snapdir LOOKUPSNAP dentry */ 1688 BUG_ON(!dir); 1689 BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); 1690 BUG_ON(!req->r_dentry); 1691 dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry); 1692 ceph_dir_clear_ordered(dir); 1693 ihold(in); 1694 err = splice_dentry(&req->r_dentry, in); 1695 if (err < 0) 1696 goto done; 1697 } else if (rinfo->head->is_dentry && req->r_dentry) { 1698 /* parent inode is not locked, be carefull */ 1699 struct ceph_vino *ptvino = NULL; 1700 dvino.ino = le64_to_cpu(rinfo->diri.in->ino); 1701 dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); 1702 if (rinfo->head->is_target) { 1703 tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); 1704 tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); 1705 ptvino = &tvino; 1706 } 1707 update_dentry_lease_careful(req->r_dentry, rinfo->dlease, 1708 session, req->r_request_started, 1709 rinfo->dname, rinfo->dname_len, 1710 &dvino, ptvino); 1711 } 1712 done: 1713 dout("fill_trace done err=%d\n", err); 1714 return err; 1715 } 1716 1717 /* 1718 * Prepopulate our cache with readdir results, leases, etc. 1719 */ 1720 static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, 1721 struct ceph_mds_session *session) 1722 { 1723 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1724 int i, err = 0; 1725 1726 for (i = 0; i < rinfo->dir_nr; i++) { 1727 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1728 struct ceph_vino vino; 1729 struct inode *in; 1730 int rc; 1731 1732 vino.ino = le64_to_cpu(rde->inode.in->ino); 1733 vino.snap = le64_to_cpu(rde->inode.in->snapid); 1734 1735 in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL); 1736 if (IS_ERR(in)) { 1737 err = PTR_ERR(in); 1738 dout("new_inode badness got %d\n", err); 1739 continue; 1740 } 1741 rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1742 -1, &req->r_caps_reservation); 1743 if (rc < 0) { 1744 pr_err("ceph_fill_inode badness on %p got %d\n", 1745 in, rc); 1746 err = rc; 1747 if (in->i_state & I_NEW) { 1748 ihold(in); 1749 discard_new_inode(in); 1750 } 1751 } else if (in->i_state & I_NEW) { 1752 unlock_new_inode(in); 1753 } 1754 1755 iput(in); 1756 } 1757 1758 return err; 1759 } 1760 1761 void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) 1762 { 1763 if (ctl->page) { 1764 kunmap(ctl->page); 1765 put_page(ctl->page); 1766 ctl->page = NULL; 1767 } 1768 } 1769 1770 static int fill_readdir_cache(struct inode *dir, struct dentry *dn, 1771 struct ceph_readdir_cache_control *ctl, 1772 struct ceph_mds_request *req) 1773 { 1774 struct ceph_inode_info *ci = ceph_inode(dir); 1775 unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); 1776 unsigned idx = ctl->index % nsize; 1777 pgoff_t pgoff = ctl->index / nsize; 1778 1779 if (!ctl->page || pgoff != page_index(ctl->page)) { 1780 ceph_readdir_cache_release(ctl); 1781 if (idx == 0) 1782 ctl->page = grab_cache_page(&dir->i_data, pgoff); 1783 else 1784 ctl->page = find_lock_page(&dir->i_data, pgoff); 1785 if (!ctl->page) { 1786 ctl->index = -1; 1787 return idx == 0 ? -ENOMEM : 0; 1788 } 1789 /* reading/filling the cache are serialized by 1790 * i_rwsem, no need to use page lock */ 1791 unlock_page(ctl->page); 1792 ctl->dentries = kmap(ctl->page); 1793 if (idx == 0) 1794 memset(ctl->dentries, 0, PAGE_SIZE); 1795 } 1796 1797 if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && 1798 req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { 1799 dout("readdir cache dn %p idx %d\n", dn, ctl->index); 1800 ctl->dentries[idx] = dn; 1801 ctl->index++; 1802 } else { 1803 dout("disable readdir cache\n"); 1804 ctl->index = -1; 1805 } 1806 return 0; 1807 } 1808 1809 int ceph_readdir_prepopulate(struct ceph_mds_request *req, 1810 struct ceph_mds_session *session) 1811 { 1812 struct dentry *parent = req->r_dentry; 1813 struct inode *inode = d_inode(parent); 1814 struct ceph_inode_info *ci = ceph_inode(inode); 1815 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; 1816 struct qstr dname; 1817 struct dentry *dn; 1818 struct inode *in; 1819 int err = 0, skipped = 0, ret, i; 1820 u32 frag = le32_to_cpu(req->r_args.readdir.frag); 1821 u32 last_hash = 0; 1822 u32 fpos_offset; 1823 struct ceph_readdir_cache_control cache_ctl = {}; 1824 1825 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) 1826 return readdir_prepopulate_inodes_only(req, session); 1827 1828 if (rinfo->hash_order) { 1829 if (req->r_path2) { 1830 last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, 1831 req->r_path2, 1832 strlen(req->r_path2)); 1833 last_hash = ceph_frag_value(last_hash); 1834 } else if (rinfo->offset_hash) { 1835 /* mds understands offset_hash */ 1836 WARN_ON_ONCE(req->r_readdir_offset != 2); 1837 last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); 1838 } 1839 } 1840 1841 if (rinfo->dir_dir && 1842 le32_to_cpu(rinfo->dir_dir->frag) != frag) { 1843 dout("readdir_prepopulate got new frag %x -> %x\n", 1844 frag, le32_to_cpu(rinfo->dir_dir->frag)); 1845 frag = le32_to_cpu(rinfo->dir_dir->frag); 1846 if (!rinfo->hash_order) 1847 req->r_readdir_offset = 2; 1848 } 1849 1850 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { 1851 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", 1852 rinfo->dir_nr, parent); 1853 } else { 1854 dout("readdir_prepopulate %d items under dn %p\n", 1855 rinfo->dir_nr, parent); 1856 if (rinfo->dir_dir) 1857 ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); 1858 1859 if (ceph_frag_is_leftmost(frag) && 1860 req->r_readdir_offset == 2 && 1861 !(rinfo->hash_order && last_hash)) { 1862 /* note dir version at start of readdir so we can 1863 * tell if any dentries get dropped */ 1864 req->r_dir_release_cnt = 1865 atomic64_read(&ci->i_release_count); 1866 req->r_dir_ordered_cnt = 1867 atomic64_read(&ci->i_ordered_count); 1868 req->r_readdir_cache_idx = 0; 1869 } 1870 } 1871 1872 cache_ctl.index = req->r_readdir_cache_idx; 1873 fpos_offset = req->r_readdir_offset; 1874 1875 /* FIXME: release caps/leases if error occurs */ 1876 for (i = 0; i < rinfo->dir_nr; i++) { 1877 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; 1878 struct ceph_vino tvino; 1879 1880 dname.name = rde->name; 1881 dname.len = rde->name_len; 1882 dname.hash = full_name_hash(parent, dname.name, dname.len); 1883 1884 tvino.ino = le64_to_cpu(rde->inode.in->ino); 1885 tvino.snap = le64_to_cpu(rde->inode.in->snapid); 1886 1887 if (rinfo->hash_order) { 1888 u32 hash = ceph_frag_value(rde->raw_hash); 1889 if (hash != last_hash) 1890 fpos_offset = 2; 1891 last_hash = hash; 1892 rde->offset = ceph_make_fpos(hash, fpos_offset++, true); 1893 } else { 1894 rde->offset = ceph_make_fpos(frag, fpos_offset++, false); 1895 } 1896 1897 retry_lookup: 1898 dn = d_lookup(parent, &dname); 1899 dout("d_lookup on parent=%p name=%.*s got %p\n", 1900 parent, dname.len, dname.name, dn); 1901 1902 if (!dn) { 1903 dn = d_alloc(parent, &dname); 1904 dout("d_alloc %p '%.*s' = %p\n", parent, 1905 dname.len, dname.name, dn); 1906 if (!dn) { 1907 dout("d_alloc badness\n"); 1908 err = -ENOMEM; 1909 goto out; 1910 } 1911 if (rde->is_nokey) { 1912 spin_lock(&dn->d_lock); 1913 dn->d_flags |= DCACHE_NOKEY_NAME; 1914 spin_unlock(&dn->d_lock); 1915 } 1916 } else if (d_really_is_positive(dn) && 1917 (ceph_ino(d_inode(dn)) != tvino.ino || 1918 ceph_snap(d_inode(dn)) != tvino.snap)) { 1919 struct ceph_dentry_info *di = ceph_dentry(dn); 1920 dout(" dn %p points to wrong inode %p\n", 1921 dn, d_inode(dn)); 1922 1923 spin_lock(&dn->d_lock); 1924 if (di->offset > 0 && 1925 di->lease_shared_gen == 1926 atomic_read(&ci->i_shared_gen)) { 1927 __ceph_dir_clear_ordered(ci); 1928 di->offset = 0; 1929 } 1930 spin_unlock(&dn->d_lock); 1931 1932 d_delete(dn); 1933 dput(dn); 1934 goto retry_lookup; 1935 } 1936 1937 /* inode */ 1938 if (d_really_is_positive(dn)) { 1939 in = d_inode(dn); 1940 } else { 1941 in = ceph_get_inode(parent->d_sb, tvino, NULL); 1942 if (IS_ERR(in)) { 1943 dout("new_inode badness\n"); 1944 d_drop(dn); 1945 dput(dn); 1946 err = PTR_ERR(in); 1947 goto out; 1948 } 1949 } 1950 1951 ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, 1952 -1, &req->r_caps_reservation); 1953 if (ret < 0) { 1954 pr_err("ceph_fill_inode badness on %p\n", in); 1955 if (d_really_is_negative(dn)) { 1956 if (in->i_state & I_NEW) { 1957 ihold(in); 1958 discard_new_inode(in); 1959 } 1960 iput(in); 1961 } 1962 d_drop(dn); 1963 err = ret; 1964 goto next_item; 1965 } 1966 if (in->i_state & I_NEW) 1967 unlock_new_inode(in); 1968 1969 if (d_really_is_negative(dn)) { 1970 if (ceph_security_xattr_deadlock(in)) { 1971 dout(" skip splicing dn %p to inode %p" 1972 " (security xattr deadlock)\n", dn, in); 1973 iput(in); 1974 skipped++; 1975 goto next_item; 1976 } 1977 1978 err = splice_dentry(&dn, in); 1979 if (err < 0) 1980 goto next_item; 1981 } 1982 1983 ceph_dentry(dn)->offset = rde->offset; 1984 1985 update_dentry_lease(d_inode(parent), dn, 1986 rde->lease, req->r_session, 1987 req->r_request_started); 1988 1989 if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { 1990 ret = fill_readdir_cache(d_inode(parent), dn, 1991 &cache_ctl, req); 1992 if (ret < 0) 1993 err = ret; 1994 } 1995 next_item: 1996 dput(dn); 1997 } 1998 out: 1999 if (err == 0 && skipped == 0) { 2000 set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); 2001 req->r_readdir_cache_idx = cache_ctl.index; 2002 } 2003 ceph_readdir_cache_release(&cache_ctl); 2004 dout("readdir_prepopulate done\n"); 2005 return err; 2006 } 2007 2008 bool ceph_inode_set_size(struct inode *inode, loff_t size) 2009 { 2010 struct ceph_inode_info *ci = ceph_inode(inode); 2011 bool ret; 2012 2013 spin_lock(&ci->i_ceph_lock); 2014 dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size); 2015 i_size_write(inode, size); 2016 ceph_fscache_update(inode); 2017 inode->i_blocks = calc_inode_blocks(size); 2018 2019 ret = __ceph_should_report_size(ci); 2020 2021 spin_unlock(&ci->i_ceph_lock); 2022 2023 return ret; 2024 } 2025 2026 void ceph_queue_inode_work(struct inode *inode, int work_bit) 2027 { 2028 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 2029 struct ceph_inode_info *ci = ceph_inode(inode); 2030 set_bit(work_bit, &ci->i_work_mask); 2031 2032 ihold(inode); 2033 if (queue_work(fsc->inode_wq, &ci->i_work)) { 2034 dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask); 2035 } else { 2036 dout("queue_inode_work %p already queued, mask=%lx\n", 2037 inode, ci->i_work_mask); 2038 iput(inode); 2039 } 2040 } 2041 2042 static void ceph_do_invalidate_pages(struct inode *inode) 2043 { 2044 struct ceph_inode_info *ci = ceph_inode(inode); 2045 u32 orig_gen; 2046 int check = 0; 2047 2048 ceph_fscache_invalidate(inode, false); 2049 2050 mutex_lock(&ci->i_truncate_mutex); 2051 2052 if (ceph_inode_is_shutdown(inode)) { 2053 pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n", 2054 __func__, ceph_vinop(inode)); 2055 mapping_set_error(inode->i_mapping, -EIO); 2056 truncate_pagecache(inode, 0); 2057 mutex_unlock(&ci->i_truncate_mutex); 2058 goto out; 2059 } 2060 2061 spin_lock(&ci->i_ceph_lock); 2062 dout("invalidate_pages %p gen %d revoking %d\n", inode, 2063 ci->i_rdcache_gen, ci->i_rdcache_revoking); 2064 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 2065 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 2066 check = 1; 2067 spin_unlock(&ci->i_ceph_lock); 2068 mutex_unlock(&ci->i_truncate_mutex); 2069 goto out; 2070 } 2071 orig_gen = ci->i_rdcache_gen; 2072 spin_unlock(&ci->i_ceph_lock); 2073 2074 if (invalidate_inode_pages2(inode->i_mapping) < 0) { 2075 pr_err("invalidate_inode_pages2 %llx.%llx failed\n", 2076 ceph_vinop(inode)); 2077 } 2078 2079 spin_lock(&ci->i_ceph_lock); 2080 if (orig_gen == ci->i_rdcache_gen && 2081 orig_gen == ci->i_rdcache_revoking) { 2082 dout("invalidate_pages %p gen %d successful\n", inode, 2083 ci->i_rdcache_gen); 2084 ci->i_rdcache_revoking--; 2085 check = 1; 2086 } else { 2087 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", 2088 inode, orig_gen, ci->i_rdcache_gen, 2089 ci->i_rdcache_revoking); 2090 if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) 2091 check = 1; 2092 } 2093 spin_unlock(&ci->i_ceph_lock); 2094 mutex_unlock(&ci->i_truncate_mutex); 2095 out: 2096 if (check) 2097 ceph_check_caps(ci, 0); 2098 } 2099 2100 /* 2101 * Make sure any pending truncation is applied before doing anything 2102 * that may depend on it. 2103 */ 2104 void __ceph_do_pending_vmtruncate(struct inode *inode) 2105 { 2106 struct ceph_inode_info *ci = ceph_inode(inode); 2107 u64 to; 2108 int wrbuffer_refs, finish = 0; 2109 2110 mutex_lock(&ci->i_truncate_mutex); 2111 retry: 2112 spin_lock(&ci->i_ceph_lock); 2113 if (ci->i_truncate_pending == 0) { 2114 dout("__do_pending_vmtruncate %p none pending\n", inode); 2115 spin_unlock(&ci->i_ceph_lock); 2116 mutex_unlock(&ci->i_truncate_mutex); 2117 return; 2118 } 2119 2120 /* 2121 * make sure any dirty snapped pages are flushed before we 2122 * possibly truncate them.. so write AND block! 2123 */ 2124 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { 2125 spin_unlock(&ci->i_ceph_lock); 2126 dout("__do_pending_vmtruncate %p flushing snaps first\n", 2127 inode); 2128 filemap_write_and_wait_range(&inode->i_data, 0, 2129 inode->i_sb->s_maxbytes); 2130 goto retry; 2131 } 2132 2133 /* there should be no reader or writer */ 2134 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); 2135 2136 to = ci->i_truncate_size; 2137 wrbuffer_refs = ci->i_wrbuffer_ref; 2138 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, 2139 ci->i_truncate_pending, to); 2140 spin_unlock(&ci->i_ceph_lock); 2141 2142 ceph_fscache_resize(inode, to); 2143 truncate_pagecache(inode, to); 2144 2145 spin_lock(&ci->i_ceph_lock); 2146 if (to == ci->i_truncate_size) { 2147 ci->i_truncate_pending = 0; 2148 finish = 1; 2149 } 2150 spin_unlock(&ci->i_ceph_lock); 2151 if (!finish) 2152 goto retry; 2153 2154 mutex_unlock(&ci->i_truncate_mutex); 2155 2156 if (wrbuffer_refs == 0) 2157 ceph_check_caps(ci, 0); 2158 2159 wake_up_all(&ci->i_cap_wq); 2160 } 2161 2162 static void ceph_inode_work(struct work_struct *work) 2163 { 2164 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, 2165 i_work); 2166 struct inode *inode = &ci->netfs.inode; 2167 2168 if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { 2169 dout("writeback %p\n", inode); 2170 filemap_fdatawrite(&inode->i_data); 2171 } 2172 if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) 2173 ceph_do_invalidate_pages(inode); 2174 2175 if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) 2176 __ceph_do_pending_vmtruncate(inode); 2177 2178 if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) 2179 ceph_check_caps(ci, 0); 2180 2181 if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) 2182 ceph_flush_snaps(ci, NULL); 2183 2184 iput(inode); 2185 } 2186 2187 static const char *ceph_encrypted_get_link(struct dentry *dentry, 2188 struct inode *inode, 2189 struct delayed_call *done) 2190 { 2191 struct ceph_inode_info *ci = ceph_inode(inode); 2192 2193 if (!dentry) 2194 return ERR_PTR(-ECHILD); 2195 2196 return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode), 2197 done); 2198 } 2199 2200 static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap, 2201 const struct path *path, 2202 struct kstat *stat, u32 request_mask, 2203 unsigned int query_flags) 2204 { 2205 int ret; 2206 2207 ret = ceph_getattr(idmap, path, stat, request_mask, query_flags); 2208 if (ret) 2209 return ret; 2210 return fscrypt_symlink_getattr(path, stat); 2211 } 2212 2213 /* 2214 * symlinks 2215 */ 2216 static const struct inode_operations ceph_symlink_iops = { 2217 .get_link = simple_get_link, 2218 .setattr = ceph_setattr, 2219 .getattr = ceph_getattr, 2220 .listxattr = ceph_listxattr, 2221 }; 2222 2223 static const struct inode_operations ceph_encrypted_symlink_iops = { 2224 .get_link = ceph_encrypted_get_link, 2225 .setattr = ceph_setattr, 2226 .getattr = ceph_encrypted_symlink_getattr, 2227 .listxattr = ceph_listxattr, 2228 }; 2229 2230 int __ceph_setattr(struct inode *inode, struct iattr *attr, 2231 struct ceph_iattr *cia) 2232 { 2233 struct ceph_inode_info *ci = ceph_inode(inode); 2234 unsigned int ia_valid = attr->ia_valid; 2235 struct ceph_mds_request *req; 2236 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 2237 struct ceph_cap_flush *prealloc_cf; 2238 int issued; 2239 int release = 0, dirtied = 0; 2240 int mask = 0; 2241 int err = 0; 2242 int inode_dirty_flags = 0; 2243 bool lock_snap_rwsem = false; 2244 2245 prealloc_cf = ceph_alloc_cap_flush(); 2246 if (!prealloc_cf) 2247 return -ENOMEM; 2248 2249 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, 2250 USE_AUTH_MDS); 2251 if (IS_ERR(req)) { 2252 ceph_free_cap_flush(prealloc_cf); 2253 return PTR_ERR(req); 2254 } 2255 2256 spin_lock(&ci->i_ceph_lock); 2257 issued = __ceph_caps_issued(ci, NULL); 2258 2259 if (!ci->i_head_snapc && 2260 (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { 2261 lock_snap_rwsem = true; 2262 if (!down_read_trylock(&mdsc->snap_rwsem)) { 2263 spin_unlock(&ci->i_ceph_lock); 2264 down_read(&mdsc->snap_rwsem); 2265 spin_lock(&ci->i_ceph_lock); 2266 issued = __ceph_caps_issued(ci, NULL); 2267 } 2268 } 2269 2270 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); 2271 #if IS_ENABLED(CONFIG_FS_ENCRYPTION) 2272 if (cia && cia->fscrypt_auth) { 2273 u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth); 2274 2275 if (len > sizeof(*cia->fscrypt_auth)) { 2276 err = -EINVAL; 2277 spin_unlock(&ci->i_ceph_lock); 2278 goto out; 2279 } 2280 2281 dout("setattr %llx:%llx fscrypt_auth len %u to %u)\n", 2282 ceph_vinop(inode), ci->fscrypt_auth_len, len); 2283 2284 /* It should never be re-set once set */ 2285 WARN_ON_ONCE(ci->fscrypt_auth); 2286 2287 if (issued & CEPH_CAP_AUTH_EXCL) { 2288 dirtied |= CEPH_CAP_AUTH_EXCL; 2289 kfree(ci->fscrypt_auth); 2290 ci->fscrypt_auth = (u8 *)cia->fscrypt_auth; 2291 ci->fscrypt_auth_len = len; 2292 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2293 ci->fscrypt_auth_len != len || 2294 memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) { 2295 req->r_fscrypt_auth = cia->fscrypt_auth; 2296 mask |= CEPH_SETATTR_FSCRYPT_AUTH; 2297 release |= CEPH_CAP_AUTH_SHARED; 2298 } 2299 cia->fscrypt_auth = NULL; 2300 } 2301 #else 2302 if (cia && cia->fscrypt_auth) { 2303 err = -EINVAL; 2304 spin_unlock(&ci->i_ceph_lock); 2305 goto out; 2306 } 2307 #endif /* CONFIG_FS_ENCRYPTION */ 2308 2309 if (ia_valid & ATTR_UID) { 2310 dout("setattr %p uid %d -> %d\n", inode, 2311 from_kuid(&init_user_ns, inode->i_uid), 2312 from_kuid(&init_user_ns, attr->ia_uid)); 2313 if (issued & CEPH_CAP_AUTH_EXCL) { 2314 inode->i_uid = attr->ia_uid; 2315 dirtied |= CEPH_CAP_AUTH_EXCL; 2316 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2317 !uid_eq(attr->ia_uid, inode->i_uid)) { 2318 req->r_args.setattr.uid = cpu_to_le32( 2319 from_kuid(&init_user_ns, attr->ia_uid)); 2320 mask |= CEPH_SETATTR_UID; 2321 release |= CEPH_CAP_AUTH_SHARED; 2322 } 2323 } 2324 if (ia_valid & ATTR_GID) { 2325 dout("setattr %p gid %d -> %d\n", inode, 2326 from_kgid(&init_user_ns, inode->i_gid), 2327 from_kgid(&init_user_ns, attr->ia_gid)); 2328 if (issued & CEPH_CAP_AUTH_EXCL) { 2329 inode->i_gid = attr->ia_gid; 2330 dirtied |= CEPH_CAP_AUTH_EXCL; 2331 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2332 !gid_eq(attr->ia_gid, inode->i_gid)) { 2333 req->r_args.setattr.gid = cpu_to_le32( 2334 from_kgid(&init_user_ns, attr->ia_gid)); 2335 mask |= CEPH_SETATTR_GID; 2336 release |= CEPH_CAP_AUTH_SHARED; 2337 } 2338 } 2339 if (ia_valid & ATTR_MODE) { 2340 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, 2341 attr->ia_mode); 2342 if (issued & CEPH_CAP_AUTH_EXCL) { 2343 inode->i_mode = attr->ia_mode; 2344 dirtied |= CEPH_CAP_AUTH_EXCL; 2345 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || 2346 attr->ia_mode != inode->i_mode) { 2347 inode->i_mode = attr->ia_mode; 2348 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); 2349 mask |= CEPH_SETATTR_MODE; 2350 release |= CEPH_CAP_AUTH_SHARED; 2351 } 2352 } 2353 2354 if (ia_valid & ATTR_ATIME) { 2355 dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode, 2356 inode->i_atime.tv_sec, inode->i_atime.tv_nsec, 2357 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); 2358 if (issued & CEPH_CAP_FILE_EXCL) { 2359 ci->i_time_warp_seq++; 2360 inode->i_atime = attr->ia_atime; 2361 dirtied |= CEPH_CAP_FILE_EXCL; 2362 } else if ((issued & CEPH_CAP_FILE_WR) && 2363 timespec64_compare(&inode->i_atime, 2364 &attr->ia_atime) < 0) { 2365 inode->i_atime = attr->ia_atime; 2366 dirtied |= CEPH_CAP_FILE_WR; 2367 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2368 !timespec64_equal(&inode->i_atime, &attr->ia_atime)) { 2369 ceph_encode_timespec64(&req->r_args.setattr.atime, 2370 &attr->ia_atime); 2371 mask |= CEPH_SETATTR_ATIME; 2372 release |= CEPH_CAP_FILE_SHARED | 2373 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2374 } 2375 } 2376 if (ia_valid & ATTR_SIZE) { 2377 loff_t isize = i_size_read(inode); 2378 2379 dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size); 2380 if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) { 2381 if (attr->ia_size > isize) { 2382 i_size_write(inode, attr->ia_size); 2383 inode->i_blocks = calc_inode_blocks(attr->ia_size); 2384 ci->i_reported_size = attr->ia_size; 2385 dirtied |= CEPH_CAP_FILE_EXCL; 2386 ia_valid |= ATTR_MTIME; 2387 } 2388 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2389 attr->ia_size != isize) { 2390 req->r_args.setattr.size = cpu_to_le64(attr->ia_size); 2391 req->r_args.setattr.old_size = cpu_to_le64(isize); 2392 mask |= CEPH_SETATTR_SIZE; 2393 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | 2394 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2395 } 2396 } 2397 if (ia_valid & ATTR_MTIME) { 2398 dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode, 2399 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, 2400 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); 2401 if (issued & CEPH_CAP_FILE_EXCL) { 2402 ci->i_time_warp_seq++; 2403 inode->i_mtime = attr->ia_mtime; 2404 dirtied |= CEPH_CAP_FILE_EXCL; 2405 } else if ((issued & CEPH_CAP_FILE_WR) && 2406 timespec64_compare(&inode->i_mtime, 2407 &attr->ia_mtime) < 0) { 2408 inode->i_mtime = attr->ia_mtime; 2409 dirtied |= CEPH_CAP_FILE_WR; 2410 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || 2411 !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) { 2412 ceph_encode_timespec64(&req->r_args.setattr.mtime, 2413 &attr->ia_mtime); 2414 mask |= CEPH_SETATTR_MTIME; 2415 release |= CEPH_CAP_FILE_SHARED | 2416 CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; 2417 } 2418 } 2419 2420 /* these do nothing */ 2421 if (ia_valid & ATTR_CTIME) { 2422 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| 2423 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; 2424 dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode, 2425 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, 2426 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, 2427 only ? "ctime only" : "ignored"); 2428 if (only) { 2429 /* 2430 * if kernel wants to dirty ctime but nothing else, 2431 * we need to choose a cap to dirty under, or do 2432 * a almost-no-op setattr 2433 */ 2434 if (issued & CEPH_CAP_AUTH_EXCL) 2435 dirtied |= CEPH_CAP_AUTH_EXCL; 2436 else if (issued & CEPH_CAP_FILE_EXCL) 2437 dirtied |= CEPH_CAP_FILE_EXCL; 2438 else if (issued & CEPH_CAP_XATTR_EXCL) 2439 dirtied |= CEPH_CAP_XATTR_EXCL; 2440 else 2441 mask |= CEPH_SETATTR_CTIME; 2442 } 2443 } 2444 if (ia_valid & ATTR_FILE) 2445 dout("setattr %p ATTR_FILE ... hrm!\n", inode); 2446 2447 if (dirtied) { 2448 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, 2449 &prealloc_cf); 2450 inode->i_ctime = attr->ia_ctime; 2451 inode_inc_iversion_raw(inode); 2452 } 2453 2454 release &= issued; 2455 spin_unlock(&ci->i_ceph_lock); 2456 if (lock_snap_rwsem) 2457 up_read(&mdsc->snap_rwsem); 2458 2459 if (inode_dirty_flags) 2460 __mark_inode_dirty(inode, inode_dirty_flags); 2461 2462 if (mask) { 2463 req->r_inode = inode; 2464 ihold(inode); 2465 req->r_inode_drop = release; 2466 req->r_args.setattr.mask = cpu_to_le32(mask); 2467 req->r_num_caps = 1; 2468 req->r_stamp = attr->ia_ctime; 2469 err = ceph_mdsc_do_request(mdsc, NULL, req); 2470 } 2471 out: 2472 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, 2473 ceph_cap_string(dirtied), mask); 2474 2475 ceph_mdsc_put_request(req); 2476 ceph_free_cap_flush(prealloc_cf); 2477 2478 if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) 2479 __ceph_do_pending_vmtruncate(inode); 2480 2481 return err; 2482 } 2483 2484 /* 2485 * setattr 2486 */ 2487 int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2488 struct iattr *attr) 2489 { 2490 struct inode *inode = d_inode(dentry); 2491 struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 2492 int err; 2493 2494 if (ceph_snap(inode) != CEPH_NOSNAP) 2495 return -EROFS; 2496 2497 if (ceph_inode_is_shutdown(inode)) 2498 return -ESTALE; 2499 2500 err = fscrypt_prepare_setattr(dentry, attr); 2501 if (err) 2502 return err; 2503 2504 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 2505 if (err != 0) 2506 return err; 2507 2508 if ((attr->ia_valid & ATTR_SIZE) && 2509 attr->ia_size > max(i_size_read(inode), fsc->max_file_size)) 2510 return -EFBIG; 2511 2512 if ((attr->ia_valid & ATTR_SIZE) && 2513 ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) 2514 return -EDQUOT; 2515 2516 err = __ceph_setattr(inode, attr, NULL); 2517 2518 if (err >= 0 && (attr->ia_valid & ATTR_MODE)) 2519 err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode); 2520 2521 return err; 2522 } 2523 2524 int ceph_try_to_choose_auth_mds(struct inode *inode, int mask) 2525 { 2526 int issued = ceph_caps_issued(ceph_inode(inode)); 2527 2528 /* 2529 * If any 'x' caps is issued we can just choose the auth MDS 2530 * instead of the random replica MDSes. Because only when the 2531 * Locker is in LOCK_EXEC state will the loner client could 2532 * get the 'x' caps. And if we send the getattr requests to 2533 * any replica MDS it must auth pin and tries to rdlock from 2534 * the auth MDS, and then the auth MDS need to do the Locker 2535 * state transition to LOCK_SYNC. And after that the lock state 2536 * will change back. 2537 * 2538 * This cost much when doing the Locker state transition and 2539 * usually will need to revoke caps from clients. 2540 * 2541 * And for the 'Xs' caps for getxattr we will also choose the 2542 * auth MDS, because the MDS side code is buggy due to setxattr 2543 * won't notify the replica MDSes when the values changed and 2544 * the replica MDS will return the old values. Though we will 2545 * fix it in MDS code, but this still makes sense for old ceph. 2546 */ 2547 if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL)) 2548 || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR))) 2549 return USE_AUTH_MDS; 2550 else 2551 return USE_ANY_MDS; 2552 } 2553 2554 /* 2555 * Verify that we have a lease on the given mask. If not, 2556 * do a getattr against an mds. 2557 */ 2558 int __ceph_do_getattr(struct inode *inode, struct page *locked_page, 2559 int mask, bool force) 2560 { 2561 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 2562 struct ceph_mds_client *mdsc = fsc->mdsc; 2563 struct ceph_mds_request *req; 2564 int mode; 2565 int err; 2566 2567 if (ceph_snap(inode) == CEPH_SNAPDIR) { 2568 dout("do_getattr inode %p SNAPDIR\n", inode); 2569 return 0; 2570 } 2571 2572 dout("do_getattr inode %p mask %s mode 0%o\n", 2573 inode, ceph_cap_string(mask), inode->i_mode); 2574 if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) 2575 return 0; 2576 2577 mode = ceph_try_to_choose_auth_mds(inode, mask); 2578 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); 2579 if (IS_ERR(req)) 2580 return PTR_ERR(req); 2581 req->r_inode = inode; 2582 ihold(inode); 2583 req->r_num_caps = 1; 2584 req->r_args.getattr.mask = cpu_to_le32(mask); 2585 req->r_locked_page = locked_page; 2586 err = ceph_mdsc_do_request(mdsc, NULL, req); 2587 if (locked_page && err == 0) { 2588 u64 inline_version = req->r_reply_info.targeti.inline_version; 2589 if (inline_version == 0) { 2590 /* the reply is supposed to contain inline data */ 2591 err = -EINVAL; 2592 } else if (inline_version == CEPH_INLINE_NONE || 2593 inline_version == 1) { 2594 err = -ENODATA; 2595 } else { 2596 err = req->r_reply_info.targeti.inline_len; 2597 } 2598 } 2599 ceph_mdsc_put_request(req); 2600 dout("do_getattr result=%d\n", err); 2601 return err; 2602 } 2603 2604 int ceph_do_getvxattr(struct inode *inode, const char *name, void *value, 2605 size_t size) 2606 { 2607 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 2608 struct ceph_mds_client *mdsc = fsc->mdsc; 2609 struct ceph_mds_request *req; 2610 int mode = USE_AUTH_MDS; 2611 int err; 2612 char *xattr_value; 2613 size_t xattr_value_len; 2614 2615 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode); 2616 if (IS_ERR(req)) { 2617 err = -ENOMEM; 2618 goto out; 2619 } 2620 2621 req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR; 2622 req->r_path2 = kstrdup(name, GFP_NOFS); 2623 if (!req->r_path2) { 2624 err = -ENOMEM; 2625 goto put; 2626 } 2627 2628 ihold(inode); 2629 req->r_inode = inode; 2630 err = ceph_mdsc_do_request(mdsc, NULL, req); 2631 if (err < 0) 2632 goto put; 2633 2634 xattr_value = req->r_reply_info.xattr_info.xattr_value; 2635 xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len; 2636 2637 dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size); 2638 2639 err = (int)xattr_value_len; 2640 if (size == 0) 2641 goto put; 2642 2643 if (xattr_value_len > size) { 2644 err = -ERANGE; 2645 goto put; 2646 } 2647 2648 memcpy(value, xattr_value, xattr_value_len); 2649 put: 2650 ceph_mdsc_put_request(req); 2651 out: 2652 dout("do_getvxattr result=%d\n", err); 2653 return err; 2654 } 2655 2656 2657 /* 2658 * Check inode permissions. We verify we have a valid value for 2659 * the AUTH cap, then call the generic handler. 2660 */ 2661 int ceph_permission(struct mnt_idmap *idmap, struct inode *inode, 2662 int mask) 2663 { 2664 int err; 2665 2666 if (mask & MAY_NOT_BLOCK) 2667 return -ECHILD; 2668 2669 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); 2670 2671 if (!err) 2672 err = generic_permission(&nop_mnt_idmap, inode, mask); 2673 return err; 2674 } 2675 2676 /* Craft a mask of needed caps given a set of requested statx attrs. */ 2677 static int statx_to_caps(u32 want, umode_t mode) 2678 { 2679 int mask = 0; 2680 2681 if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE)) 2682 mask |= CEPH_CAP_AUTH_SHARED; 2683 2684 if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) { 2685 /* 2686 * The link count for directories depends on inode->i_subdirs, 2687 * and that is only updated when Fs caps are held. 2688 */ 2689 if (S_ISDIR(mode)) 2690 mask |= CEPH_CAP_FILE_SHARED; 2691 else 2692 mask |= CEPH_CAP_LINK_SHARED; 2693 } 2694 2695 if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE)) 2696 mask |= CEPH_CAP_FILE_SHARED; 2697 2698 if (want & (STATX_CTIME|STATX_CHANGE_COOKIE)) 2699 mask |= CEPH_CAP_XATTR_SHARED; 2700 2701 return mask; 2702 } 2703 2704 /* 2705 * Get all the attributes. If we have sufficient caps for the requested attrs, 2706 * then we can avoid talking to the MDS at all. 2707 */ 2708 int ceph_getattr(struct mnt_idmap *idmap, const struct path *path, 2709 struct kstat *stat, u32 request_mask, unsigned int flags) 2710 { 2711 struct inode *inode = d_inode(path->dentry); 2712 struct super_block *sb = inode->i_sb; 2713 struct ceph_inode_info *ci = ceph_inode(inode); 2714 u32 valid_mask = STATX_BASIC_STATS; 2715 int err = 0; 2716 2717 if (ceph_inode_is_shutdown(inode)) 2718 return -ESTALE; 2719 2720 /* Skip the getattr altogether if we're asked not to sync */ 2721 if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) { 2722 err = ceph_do_getattr(inode, 2723 statx_to_caps(request_mask, inode->i_mode), 2724 flags & AT_STATX_FORCE_SYNC); 2725 if (err) 2726 return err; 2727 } 2728 2729 generic_fillattr(&nop_mnt_idmap, inode, stat); 2730 stat->ino = ceph_present_inode(inode); 2731 2732 /* 2733 * btime on newly-allocated inodes is 0, so if this is still set to 2734 * that, then assume that it's not valid. 2735 */ 2736 if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { 2737 stat->btime = ci->i_btime; 2738 valid_mask |= STATX_BTIME; 2739 } 2740 2741 if (request_mask & STATX_CHANGE_COOKIE) { 2742 stat->change_cookie = inode_peek_iversion_raw(inode); 2743 valid_mask |= STATX_CHANGE_COOKIE; 2744 } 2745 2746 if (ceph_snap(inode) == CEPH_NOSNAP) 2747 stat->dev = sb->s_dev; 2748 else 2749 stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; 2750 2751 if (S_ISDIR(inode->i_mode)) { 2752 if (ceph_test_mount_opt(ceph_sb_to_client(sb), RBYTES)) { 2753 stat->size = ci->i_rbytes; 2754 } else if (ceph_snap(inode) == CEPH_SNAPDIR) { 2755 struct ceph_inode_info *pci; 2756 struct ceph_snap_realm *realm; 2757 struct inode *parent; 2758 2759 parent = ceph_lookup_inode(sb, ceph_ino(inode)); 2760 if (IS_ERR(parent)) 2761 return PTR_ERR(parent); 2762 2763 pci = ceph_inode(parent); 2764 spin_lock(&pci->i_ceph_lock); 2765 realm = pci->i_snap_realm; 2766 if (realm) 2767 stat->size = realm->num_snaps; 2768 else 2769 stat->size = 0; 2770 spin_unlock(&pci->i_ceph_lock); 2771 iput(parent); 2772 } else { 2773 stat->size = ci->i_files + ci->i_subdirs; 2774 } 2775 stat->blocks = 0; 2776 stat->blksize = 65536; 2777 /* 2778 * Some applications rely on the number of st_nlink 2779 * value on directories to be either 0 (if unlinked) 2780 * or 2 + number of subdirectories. 2781 */ 2782 if (stat->nlink == 1) 2783 /* '.' + '..' + subdirs */ 2784 stat->nlink = 1 + 1 + ci->i_subdirs; 2785 } 2786 2787 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC; 2788 if (IS_ENCRYPTED(inode)) 2789 stat->attributes |= STATX_ATTR_ENCRYPTED; 2790 stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC | 2791 STATX_ATTR_ENCRYPTED); 2792 2793 stat->result_mask = request_mask & valid_mask; 2794 return err; 2795 } 2796 2797 void ceph_inode_shutdown(struct inode *inode) 2798 { 2799 struct ceph_inode_info *ci = ceph_inode(inode); 2800 struct rb_node *p; 2801 int iputs = 0; 2802 bool invalidate = false; 2803 2804 spin_lock(&ci->i_ceph_lock); 2805 ci->i_ceph_flags |= CEPH_I_SHUTDOWN; 2806 p = rb_first(&ci->i_caps); 2807 while (p) { 2808 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 2809 2810 p = rb_next(p); 2811 iputs += ceph_purge_inode_cap(inode, cap, &invalidate); 2812 } 2813 spin_unlock(&ci->i_ceph_lock); 2814 2815 if (invalidate) 2816 ceph_queue_invalidate(inode); 2817 while (iputs--) 2818 iput(inode); 2819 } 2820