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