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