1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/inode.c 4 * 5 * Copyright (C) 1992 Rick Sladkey 6 * 7 * nfs inode and superblock handling functions 8 * 9 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some 10 * experimental NFS changes. Modularisation taken straight from SYS5 fs. 11 * 12 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts. 13 * J.S.Peatfield@damtp.cam.ac.uk 14 * 15 */ 16 17 #include <linux/module.h> 18 #include <linux/init.h> 19 #include <linux/sched/signal.h> 20 #include <linux/time.h> 21 #include <linux/kernel.h> 22 #include <linux/mm.h> 23 #include <linux/string.h> 24 #include <linux/stat.h> 25 #include <linux/errno.h> 26 #include <linux/unistd.h> 27 #include <linux/sunrpc/clnt.h> 28 #include <linux/sunrpc/stats.h> 29 #include <linux/sunrpc/metrics.h> 30 #include <linux/nfs_fs.h> 31 #include <linux/nfs_mount.h> 32 #include <linux/nfs4_mount.h> 33 #include <linux/lockd/bind.h> 34 #include <linux/seq_file.h> 35 #include <linux/mount.h> 36 #include <linux/vfs.h> 37 #include <linux/inet.h> 38 #include <linux/nfs_xdr.h> 39 #include <linux/slab.h> 40 #include <linux/compat.h> 41 #include <linux/freezer.h> 42 #include <linux/uaccess.h> 43 #include <linux/iversion.h> 44 #include <linux/fileattr.h> 45 46 #include "nfs4_fs.h" 47 #include "callback.h" 48 #include "delegation.h" 49 #include "iostat.h" 50 #include "internal.h" 51 #include "fscache.h" 52 #include "pnfs.h" 53 #include "nfs.h" 54 #include "netns.h" 55 #include "sysfs.h" 56 57 #include "nfstrace.h" 58 59 #define NFSDBG_FACILITY NFSDBG_VFS 60 61 static bool enable_ino64; 62 63 static int param_set_enable_ino64(const char *val, const struct kernel_param *kp) 64 { 65 pr_notice("enable_ino64 is deprecated and has no effect\n"); 66 return 0; 67 } 68 69 static const struct kernel_param_ops param_ops_enable_ino64 = { 70 .set = param_set_enable_ino64, 71 .get = param_get_bool, 72 }; 73 74 static int nfs_update_inode(struct inode *, struct nfs_fattr *); 75 76 static struct kmem_cache * nfs_inode_cachep; 77 78 int nfs_wait_bit_killable(struct wait_bit_key *key, int mode) 79 { 80 if (unlikely(nfs_current_task_exiting())) 81 return -EINTR; 82 schedule(); 83 if (signal_pending_state(mode, current)) 84 return -ERESTARTSYS; 85 return 0; 86 } 87 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable); 88 89 int nfs_drop_inode(struct inode *inode) 90 { 91 return NFS_STALE(inode) || inode_generic_drop(inode); 92 } 93 EXPORT_SYMBOL_GPL(nfs_drop_inode); 94 95 void nfs_clear_inode(struct inode *inode) 96 { 97 /* 98 * The following should never happen... 99 */ 100 WARN_ON_ONCE(nfs_have_writebacks(inode)); 101 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files)); 102 nfs_zap_acl_cache(inode); 103 nfs_access_zap_cache(inode); 104 nfs_fscache_clear_inode(inode); 105 } 106 EXPORT_SYMBOL_GPL(nfs_clear_inode); 107 108 void nfs_evict_inode(struct inode *inode) 109 { 110 truncate_inode_pages_final(&inode->i_data); 111 clear_inode(inode); 112 nfs_clear_inode(inode); 113 } 114 115 int nfs_sync_inode(struct inode *inode) 116 { 117 inode_dio_wait(inode); 118 return nfs_wb_all(inode); 119 } 120 EXPORT_SYMBOL_GPL(nfs_sync_inode); 121 122 /** 123 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk 124 * @mapping: pointer to struct address_space 125 */ 126 int nfs_sync_mapping(struct address_space *mapping) 127 { 128 int ret = 0; 129 130 if (mapping->nrpages != 0) { 131 unmap_mapping_range(mapping, 0, 0, 0); 132 ret = nfs_wb_all(mapping->host); 133 } 134 return ret; 135 } 136 137 static int nfs_attribute_timeout(struct inode *inode) 138 { 139 struct nfs_inode *nfsi = NFS_I(inode); 140 141 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo); 142 } 143 144 static bool nfs_check_cache_flags_invalid(struct inode *inode, 145 unsigned long flags) 146 { 147 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); 148 149 return (cache_validity & flags) != 0; 150 } 151 152 bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags) 153 { 154 if (nfs_check_cache_flags_invalid(inode, flags)) 155 return true; 156 return nfs_attribute_cache_expired(inode); 157 } 158 EXPORT_SYMBOL_GPL(nfs_check_cache_invalid); 159 160 #ifdef CONFIG_NFS_V4_2 161 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) 162 { 163 return nfsi->xattr_cache != NULL; 164 } 165 #else 166 static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) 167 { 168 return false; 169 } 170 #endif 171 172 void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) 173 { 174 struct nfs_inode *nfsi = NFS_I(inode); 175 176 if (nfs_have_delegated_attributes(inode)) { 177 if (!(flags & NFS_INO_REVAL_FORCED)) 178 flags &= ~(NFS_INO_INVALID_MODE | 179 NFS_INO_INVALID_OTHER | 180 NFS_INO_INVALID_BTIME | 181 NFS_INO_INVALID_XATTR); 182 flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); 183 } 184 185 if (!nfs_has_xattr_cache(nfsi)) 186 flags &= ~NFS_INO_INVALID_XATTR; 187 if (flags & NFS_INO_INVALID_DATA) 188 nfs_fscache_invalidate(inode, 0); 189 flags &= ~NFS_INO_REVAL_FORCED; 190 191 flags |= nfsi->cache_validity; 192 if (inode->i_mapping->nrpages == 0) 193 flags &= ~NFS_INO_INVALID_DATA; 194 195 /* pairs with nfs_clear_invalid_mapping()'s smp_load_acquire() */ 196 smp_store_release(&nfsi->cache_validity, flags); 197 198 if (inode->i_mapping->nrpages == 0 || 199 nfsi->cache_validity & NFS_INO_INVALID_DATA) { 200 nfs_ooo_clear(nfsi); 201 } 202 trace_nfs_set_cache_invalid(inode, 0); 203 } 204 EXPORT_SYMBOL_GPL(nfs_set_cache_invalid); 205 206 /* 207 * Invalidate the local caches 208 */ 209 static void nfs_zap_caches_locked(struct inode *inode) 210 { 211 struct nfs_inode *nfsi = NFS_I(inode); 212 int mode = inode->i_mode; 213 214 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); 215 216 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); 217 nfsi->attrtimeo_timestamp = jiffies; 218 219 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) 220 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR | 221 NFS_INO_INVALID_DATA | 222 NFS_INO_INVALID_ACCESS | 223 NFS_INO_INVALID_ACL | 224 NFS_INO_INVALID_XATTR); 225 else 226 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR | 227 NFS_INO_INVALID_ACCESS | 228 NFS_INO_INVALID_ACL | 229 NFS_INO_INVALID_XATTR); 230 nfs_zap_label_cache_locked(nfsi); 231 } 232 233 void nfs_zap_caches(struct inode *inode) 234 { 235 spin_lock(&inode->i_lock); 236 nfs_zap_caches_locked(inode); 237 spin_unlock(&inode->i_lock); 238 } 239 240 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping) 241 { 242 if (mapping->nrpages != 0) { 243 spin_lock(&inode->i_lock); 244 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); 245 spin_unlock(&inode->i_lock); 246 } 247 } 248 249 void nfs_zap_acl_cache(struct inode *inode) 250 { 251 void (*clear_acl_cache)(struct inode *); 252 253 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache; 254 if (clear_acl_cache != NULL) 255 clear_acl_cache(inode); 256 spin_lock(&inode->i_lock); 257 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL; 258 spin_unlock(&inode->i_lock); 259 } 260 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache); 261 262 void nfs_invalidate_atime(struct inode *inode) 263 { 264 if (nfs_have_delegated_atime(inode)) 265 return; 266 spin_lock(&inode->i_lock); 267 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); 268 spin_unlock(&inode->i_lock); 269 } 270 EXPORT_SYMBOL_GPL(nfs_invalidate_atime); 271 272 /* 273 * Invalidate, but do not unhash, the inode. 274 * NB: must be called with inode->i_lock held! 275 */ 276 static void nfs_set_inode_stale_locked(struct inode *inode) 277 { 278 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); 279 nfs_zap_caches_locked(inode); 280 trace_nfs_set_inode_stale(inode); 281 } 282 283 void nfs_set_inode_stale(struct inode *inode) 284 { 285 spin_lock(&inode->i_lock); 286 nfs_set_inode_stale_locked(inode); 287 spin_unlock(&inode->i_lock); 288 } 289 290 struct nfs_find_desc { 291 struct nfs_fh *fh; 292 struct nfs_fattr *fattr; 293 }; 294 295 /* 296 * For re-exported directories (also seen in NFSv2) 297 * we are forced to allow 2 different inodes to have the same 298 * i_ino. 299 */ 300 static int 301 nfs_find_actor(struct inode *inode, void *opaque) 302 { 303 struct nfs_find_desc *desc = opaque; 304 struct nfs_fh *fh = desc->fh; 305 struct nfs_fattr *fattr = desc->fattr; 306 307 if (inode->i_ino != fattr->fileid) 308 return 0; 309 if (inode_wrong_type(inode, fattr->mode)) 310 return 0; 311 if (nfs_compare_fh(NFS_FH(inode), fh)) 312 return 0; 313 if (is_bad_inode(inode) || NFS_STALE(inode)) 314 return 0; 315 return 1; 316 } 317 318 static int 319 nfs_init_locked(struct inode *inode, void *opaque) 320 { 321 struct nfs_find_desc *desc = opaque; 322 struct nfs_fattr *fattr = desc->fattr; 323 324 inode->i_ino = fattr->fileid; 325 inode->i_mode = fattr->mode; 326 nfs_copy_fh(NFS_FH(inode), desc->fh); 327 return 0; 328 } 329 330 #ifdef CONFIG_NFS_V4_SECURITY_LABEL 331 static void nfs_clear_label_invalid(struct inode *inode) 332 { 333 spin_lock(&inode->i_lock); 334 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL; 335 spin_unlock(&inode->i_lock); 336 } 337 338 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) 339 { 340 int error; 341 342 if (fattr->label == NULL) 343 return; 344 345 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) { 346 error = security_inode_notifysecctx(inode, fattr->label->label, 347 fattr->label->len); 348 if (error) 349 printk(KERN_ERR "%s() %s %d " 350 "security_inode_notifysecctx() %d\n", 351 __func__, 352 (char *)fattr->label->label, 353 fattr->label->len, error); 354 nfs_clear_label_invalid(inode); 355 } 356 } 357 358 struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) 359 { 360 struct nfs4_label *label; 361 362 if (!(server->caps & NFS_CAP_SECURITY_LABEL)) 363 return NULL; 364 365 label = kzalloc_obj(struct nfs4_label, flags); 366 if (label == NULL) 367 return ERR_PTR(-ENOMEM); 368 369 label->label = kzalloc(NFS4_MAXLABELLEN, flags); 370 if (label->label == NULL) { 371 kfree(label); 372 return ERR_PTR(-ENOMEM); 373 } 374 label->len = NFS4_MAXLABELLEN; 375 376 return label; 377 } 378 EXPORT_SYMBOL_GPL(nfs4_label_alloc); 379 #else 380 void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) 381 { 382 } 383 #endif 384 EXPORT_SYMBOL_GPL(nfs_setsecurity); 385 386 /* Search for inode identified by fh, fileid and i_mode in inode cache. */ 387 struct inode * 388 nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh) 389 { 390 struct nfs_find_desc desc = { 391 .fh = fh, 392 .fattr = fattr, 393 }; 394 struct inode *inode; 395 u64 hash; 396 397 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) || 398 !(fattr->valid & NFS_ATTR_FATTR_TYPE)) 399 return NULL; 400 401 hash = fattr->fileid; 402 inode = ilookup5(sb, hash, nfs_find_actor, &desc); 403 404 dprintk("%s: returning %p\n", __func__, inode); 405 return inode; 406 } 407 408 static void nfs_inode_init_regular(struct nfs_inode *nfsi) 409 { 410 atomic_long_set(&nfsi->nrequests, 0); 411 atomic_long_set(&nfsi->redirtied_pages, 0); 412 INIT_LIST_HEAD(&nfsi->commit_info.list); 413 atomic_long_set(&nfsi->commit_info.ncommit, 0); 414 atomic_set(&nfsi->commit_info.rpcs_out, 0); 415 mutex_init(&nfsi->commit_mutex); 416 } 417 418 static void nfs_inode_init_dir(struct nfs_inode *nfsi) 419 { 420 nfsi->cache_change_attribute = 0; 421 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf)); 422 init_rwsem(&nfsi->rmdir_sem); 423 } 424 425 /* 426 * This is our front-end to iget that looks up inodes by file handle 427 * instead of inode number. 428 */ 429 struct inode * 430 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr) 431 { 432 struct nfs_find_desc desc = { 433 .fh = fh, 434 .fattr = fattr 435 }; 436 struct inode *inode = ERR_PTR(-ENOENT); 437 u64 fattr_supported = NFS_SB(sb)->fattr_valid; 438 u64 hash; 439 440 nfs_attr_check_mountpoint(sb, fattr); 441 442 if (nfs_attr_use_mounted_on_fileid(fattr)) 443 fattr->fileid = fattr->mounted_on_fileid; 444 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) 445 goto out_no_inode; 446 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0) 447 goto out_no_inode; 448 449 hash = fattr->fileid; 450 451 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc); 452 if (inode == NULL) { 453 inode = ERR_PTR(-ENOMEM); 454 goto out_no_inode; 455 } 456 457 if (inode_state_read_once(inode) & I_NEW) { 458 struct nfs_inode *nfsi = NFS_I(inode); 459 unsigned long now = jiffies; 460 461 /* We can't support update_atime(), since the server will reset it */ 462 inode->i_flags |= S_NOATIME|S_NOCMTIME; 463 inode->i_mode = fattr->mode; 464 nfsi->cache_validity = 0; 465 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0 466 && (fattr_supported & NFS_ATTR_FATTR_MODE)) 467 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE); 468 /* Why so? Because we want revalidate for devices/FIFOs, and 469 * that's precisely what we have in nfs_file_inode_operations. 470 */ 471 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops; 472 if (S_ISREG(inode->i_mode)) { 473 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops; 474 inode->i_data.a_ops = &nfs_file_aops; 475 nfs_inode_init_regular(nfsi); 476 mapping_set_large_folios(inode->i_mapping); 477 } else if (S_ISDIR(inode->i_mode)) { 478 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops; 479 inode->i_fop = &nfs_dir_operations; 480 inode->i_data.a_ops = &nfs_dir_aops; 481 nfs_inode_init_dir(nfsi); 482 /* Deal with crossing mountpoints */ 483 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT || 484 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) { 485 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) 486 inode->i_op = &nfs_referral_inode_operations; 487 else 488 inode->i_op = &nfs_mountpoint_inode_operations; 489 inode->i_fop = NULL; 490 inode->i_flags |= S_AUTOMOUNT; 491 } 492 } else if (S_ISLNK(inode->i_mode)) { 493 inode->i_op = &nfs_symlink_inode_operations; 494 inode_nohighmem(inode); 495 } else 496 init_special_inode(inode, inode->i_mode, fattr->rdev); 497 498 inode_set_atime(inode, 0, 0); 499 inode_set_mtime(inode, 0, 0); 500 inode_set_ctime(inode, 0, 0); 501 memset(&nfsi->btime, 0, sizeof(nfsi->btime)); 502 inode_set_iversion_raw(inode, 0); 503 inode->i_size = 0; 504 clear_nlink(inode); 505 inode->i_uid = make_kuid(&init_user_ns, -2); 506 inode->i_gid = make_kgid(&init_user_ns, -2); 507 inode->i_blocks = 0; 508 nfsi->write_io = 0; 509 nfsi->read_io = 0; 510 511 nfsi->read_cache_jiffies = fattr->time_start; 512 nfsi->attr_gencount = fattr->gencount; 513 if (fattr->valid & NFS_ATTR_FATTR_ATIME) 514 inode_set_atime_to_ts(inode, fattr->atime); 515 else if (fattr_supported & NFS_ATTR_FATTR_ATIME) 516 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); 517 if (fattr->valid & NFS_ATTR_FATTR_MTIME) 518 inode_set_mtime_to_ts(inode, fattr->mtime); 519 else if (fattr_supported & NFS_ATTR_FATTR_MTIME) 520 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); 521 if (fattr->valid & NFS_ATTR_FATTR_CTIME) 522 inode_set_ctime_to_ts(inode, fattr->ctime); 523 else if (fattr_supported & NFS_ATTR_FATTR_CTIME) 524 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME); 525 if (fattr->valid & NFS_ATTR_FATTR_BTIME) 526 nfsi->btime = fattr->btime; 527 else if (fattr_supported & NFS_ATTR_FATTR_BTIME) 528 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BTIME); 529 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) 530 inode_set_iversion_raw(inode, fattr->change_attr); 531 else 532 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 533 if (fattr->valid & NFS_ATTR_FATTR_SIZE) 534 inode->i_size = nfs_size_to_loff_t(fattr->size); 535 else 536 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE); 537 if (fattr->valid & NFS_ATTR_FATTR_NLINK) 538 set_nlink(inode, fattr->nlink); 539 else if (fattr_supported & NFS_ATTR_FATTR_NLINK) 540 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK); 541 else 542 set_nlink(inode, 1); 543 if (fattr->valid & NFS_ATTR_FATTR_OWNER) 544 inode->i_uid = fattr->uid; 545 else if (fattr_supported & NFS_ATTR_FATTR_OWNER) 546 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); 547 if (fattr->valid & NFS_ATTR_FATTR_GROUP) 548 inode->i_gid = fattr->gid; 549 else if (fattr_supported & NFS_ATTR_FATTR_GROUP) 550 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); 551 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) 552 inode->i_blocks = fattr->du.nfs2.blocks; 553 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED && 554 fattr->size != 0) 555 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); 556 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { 557 /* 558 * report the blocks in 512byte units 559 */ 560 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); 561 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED && 562 fattr->size != 0) 563 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); 564 565 nfs_setsecurity(inode, fattr); 566 567 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); 568 nfsi->attrtimeo_timestamp = now; 569 nfsi->access_cache = RB_ROOT; 570 571 nfs_fscache_init_inode(inode); 572 573 unlock_new_inode(inode); 574 } else { 575 int err = nfs_refresh_inode(inode, fattr); 576 if (err < 0) { 577 iput(inode); 578 inode = ERR_PTR(err); 579 goto out_no_inode; 580 } 581 } 582 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n", 583 inode->i_sb->s_id, 584 (unsigned long long)inode->i_ino, 585 nfs_display_fhandle_hash(fh), 586 icount_read_once(inode)); 587 588 out: 589 return inode; 590 591 out_no_inode: 592 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode)); 593 goto out; 594 } 595 EXPORT_SYMBOL_GPL(nfs_fhget); 596 597 static void 598 nfs_fattr_fixup_delegated(struct inode *inode, struct nfs_fattr *fattr) 599 { 600 unsigned long cache_validity = NFS_I(inode)->cache_validity; 601 602 if (nfs_have_delegated_mtime(inode)) { 603 if (!(cache_validity & NFS_INO_INVALID_CTIME)) 604 fattr->valid &= ~(NFS_ATTR_FATTR_PRECTIME | 605 NFS_ATTR_FATTR_CTIME); 606 607 if (!(cache_validity & NFS_INO_INVALID_MTIME)) 608 fattr->valid &= ~(NFS_ATTR_FATTR_PREMTIME | 609 NFS_ATTR_FATTR_MTIME); 610 611 if (!(cache_validity & NFS_INO_INVALID_ATIME)) 612 fattr->valid &= ~NFS_ATTR_FATTR_ATIME; 613 } else if (nfs_have_delegated_atime(inode)) { 614 if (!(cache_validity & NFS_INO_INVALID_ATIME)) 615 fattr->valid &= ~NFS_ATTR_FATTR_ATIME; 616 } 617 } 618 619 static void nfs_set_timestamps_to_ts(struct inode *inode, struct iattr *attr) 620 { 621 unsigned int cache_flags = 0; 622 623 if (attr->ia_valid & ATTR_MTIME_SET) { 624 struct timespec64 ctime = inode_get_ctime(inode); 625 struct timespec64 mtime = inode_get_mtime(inode); 626 struct timespec64 now; 627 bool updated = false; 628 629 now = inode_set_ctime_current(inode); 630 if (!timespec64_equal(&now, &ctime)) 631 updated = true; 632 633 inode_set_mtime_to_ts(inode, attr->ia_mtime); 634 if (!timespec64_equal(&now, &mtime)) 635 updated = true; 636 637 inode_maybe_inc_iversion(inode, updated); 638 cache_flags |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME; 639 } 640 if (attr->ia_valid & ATTR_ATIME_SET) { 641 inode_set_atime_to_ts(inode, attr->ia_atime); 642 cache_flags |= NFS_INO_INVALID_ATIME; 643 } 644 NFS_I(inode)->cache_validity &= ~cache_flags; 645 } 646 647 static void nfs_update_atime(struct inode *inode) 648 { 649 inode_update_time(inode, FS_UPD_ATIME, 0); 650 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ATIME; 651 } 652 653 static void nfs_update_mtime(struct inode *inode) 654 { 655 inode_update_time(inode, FS_UPD_CMTIME, 0); 656 NFS_I(inode)->cache_validity &= 657 ~(NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME); 658 } 659 660 void nfs_update_delegated_atime(struct inode *inode) 661 { 662 spin_lock(&inode->i_lock); 663 if (nfs_have_delegated_atime(inode)) 664 nfs_update_atime(inode); 665 spin_unlock(&inode->i_lock); 666 } 667 668 void nfs_update_delegated_mtime_locked(struct inode *inode) 669 { 670 if (nfs_have_delegated_mtime(inode) || 671 nfs_have_directory_delegation(inode)) 672 nfs_update_mtime(inode); 673 } 674 675 void nfs_update_delegated_mtime(struct inode *inode) 676 { 677 spin_lock(&inode->i_lock); 678 nfs_update_delegated_mtime_locked(inode); 679 spin_unlock(&inode->i_lock); 680 } 681 EXPORT_SYMBOL_GPL(nfs_update_delegated_mtime); 682 683 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN) 684 685 int 686 nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 687 struct iattr *attr) 688 { 689 struct inode *inode = d_inode(dentry); 690 struct nfs_fattr *fattr; 691 loff_t oldsize; 692 int error = 0; 693 kuid_t task_uid = current_fsuid(); 694 kuid_t owner_uid = inode->i_uid; 695 696 nfs_inc_stats(inode, NFSIOS_VFSSETATTR); 697 698 /* skip mode change if it's just for clearing setuid/setgid */ 699 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) 700 attr->ia_valid &= ~ATTR_MODE; 701 702 if (S_ISREG(inode->i_mode)) 703 nfs_file_block_o_direct(NFS_I(inode)); 704 705 oldsize = i_size_read(inode); 706 if (attr->ia_valid & ATTR_SIZE) { 707 BUG_ON(!S_ISREG(inode->i_mode)); 708 709 error = inode_newsize_ok(inode, attr->ia_size); 710 if (error) 711 return error; 712 713 if (attr->ia_size == oldsize) 714 attr->ia_valid &= ~ATTR_SIZE; 715 } 716 717 if (nfs_have_delegated_mtime(inode) && attr->ia_valid & ATTR_MTIME) { 718 spin_lock(&inode->i_lock); 719 if (attr->ia_valid & ATTR_MTIME_SET) { 720 if (uid_eq(task_uid, owner_uid)) { 721 nfs_set_timestamps_to_ts(inode, attr); 722 attr->ia_valid &= ~(ATTR_MTIME|ATTR_MTIME_SET| 723 ATTR_ATIME|ATTR_ATIME_SET); 724 } 725 } else { 726 if (attr->ia_valid & ATTR_MTIME) 727 nfs_update_mtime(inode); 728 if (attr->ia_valid & ATTR_ATIME) 729 nfs_update_atime(inode); 730 attr->ia_valid &= ~(ATTR_MTIME|ATTR_ATIME); 731 } 732 spin_unlock(&inode->i_lock); 733 } else if (nfs_have_delegated_atime(inode) && 734 attr->ia_valid & ATTR_ATIME && 735 !(attr->ia_valid & ATTR_MTIME)) { 736 if (!(attr->ia_valid & ATTR_ATIME_SET)) { 737 nfs_update_delegated_atime(inode); 738 attr->ia_valid &= ~ATTR_ATIME; 739 } 740 } 741 742 /* Optimization: if the end result is no change, don't RPC */ 743 if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0) 744 return 0; 745 746 trace_nfs_setattr_enter(inode); 747 748 /* Write all dirty data */ 749 if (S_ISREG(inode->i_mode)) 750 nfs_sync_inode(inode); 751 752 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 753 if (fattr == NULL) { 754 error = -ENOMEM; 755 goto out; 756 } 757 758 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr); 759 if (error == 0) { 760 if (attr->ia_valid & ATTR_SIZE) 761 nfs_truncate_last_folio(inode->i_mapping, oldsize, 762 attr->ia_size); 763 error = nfs_refresh_inode(inode, fattr); 764 } 765 nfs_free_fattr(fattr); 766 out: 767 trace_nfs_setattr_exit(inode, error); 768 return error; 769 } 770 EXPORT_SYMBOL_GPL(nfs_setattr); 771 772 /** 773 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall 774 * @inode: inode of the file used 775 * @offset: file offset to start truncating 776 * 777 * This is a copy of the common vmtruncate, but with the locking 778 * corrected to take into account the fact that NFS requires 779 * inode->i_size to be updated under the inode->i_lock. 780 * Note: must be called with inode->i_lock held! 781 */ 782 static int nfs_vmtruncate(struct inode * inode, loff_t offset) 783 { 784 int err; 785 786 err = inode_newsize_ok(inode, offset); 787 if (err) 788 goto out; 789 790 trace_nfs_size_truncate(inode, offset); 791 i_size_write(inode, offset); 792 /* Optimisation */ 793 if (offset == 0) { 794 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA; 795 nfs_ooo_clear(NFS_I(inode)); 796 } 797 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; 798 799 spin_unlock(&inode->i_lock); 800 truncate_pagecache(inode, offset); 801 nfs_update_delegated_mtime_locked(inode); 802 spin_lock(&inode->i_lock); 803 out: 804 return err; 805 } 806 807 /** 808 * nfs_setattr_update_inode - Update inode metadata after a setattr call. 809 * @inode: pointer to struct inode 810 * @attr: pointer to struct iattr 811 * @fattr: pointer to struct nfs_fattr 812 * 813 * Note: we do this in the *proc.c in order to ensure that 814 * it works for things like exclusive creates too. 815 */ 816 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr, 817 struct nfs_fattr *fattr) 818 { 819 /* Barrier: bump the attribute generation count. */ 820 nfs_fattr_set_barrier(fattr); 821 822 spin_lock(&inode->i_lock); 823 NFS_I(inode)->attr_gencount = fattr->gencount; 824 if ((attr->ia_valid & ATTR_SIZE) != 0) { 825 if (!nfs_have_delegated_mtime(inode)) 826 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); 827 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); 828 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC); 829 nfs_vmtruncate(inode, attr->ia_size); 830 } 831 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) { 832 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME; 833 if ((attr->ia_valid & ATTR_KILL_SUID) != 0 && 834 inode->i_mode & S_ISUID) 835 inode->i_mode &= ~S_ISUID; 836 if (setattr_should_drop_sgid(&nop_mnt_idmap, inode)) 837 inode->i_mode &= ~S_ISGID; 838 if ((attr->ia_valid & ATTR_MODE) != 0) { 839 int mode = attr->ia_mode & S_IALLUGO; 840 mode |= inode->i_mode & ~S_IALLUGO; 841 inode->i_mode = mode; 842 } 843 if ((attr->ia_valid & ATTR_UID) != 0) 844 inode->i_uid = attr->ia_uid; 845 if ((attr->ia_valid & ATTR_GID) != 0) 846 inode->i_gid = attr->ia_gid; 847 if (fattr->valid & NFS_ATTR_FATTR_CTIME) 848 inode_set_ctime_to_ts(inode, fattr->ctime); 849 else 850 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE 851 | NFS_INO_INVALID_CTIME); 852 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS 853 | NFS_INO_INVALID_ACL); 854 } 855 if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) { 856 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME 857 | NFS_INO_INVALID_CTIME); 858 if (fattr->valid & NFS_ATTR_FATTR_ATIME) 859 inode_set_atime_to_ts(inode, fattr->atime); 860 else if (attr->ia_valid & ATTR_ATIME_SET) 861 inode_set_atime_to_ts(inode, attr->ia_atime); 862 else 863 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); 864 865 if (fattr->valid & NFS_ATTR_FATTR_CTIME) 866 inode_set_ctime_to_ts(inode, fattr->ctime); 867 else 868 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE 869 | NFS_INO_INVALID_CTIME); 870 } 871 if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) { 872 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME 873 | NFS_INO_INVALID_CTIME); 874 if (fattr->valid & NFS_ATTR_FATTR_MTIME) 875 inode_set_mtime_to_ts(inode, fattr->mtime); 876 else if (attr->ia_valid & ATTR_MTIME_SET) 877 inode_set_mtime_to_ts(inode, attr->ia_mtime); 878 else 879 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); 880 881 if (fattr->valid & NFS_ATTR_FATTR_CTIME) 882 inode_set_ctime_to_ts(inode, fattr->ctime); 883 else 884 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE 885 | NFS_INO_INVALID_CTIME); 886 } 887 if (fattr->valid) 888 nfs_update_inode(inode, fattr); 889 spin_unlock(&inode->i_lock); 890 } 891 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode); 892 893 /* 894 * Don't request help from readdirplus if the file is being written to, 895 * or if attribute caching is turned off 896 */ 897 static bool nfs_getattr_readdirplus_enable(const struct inode *inode) 898 { 899 return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) && 900 !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ; 901 } 902 903 static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry) 904 { 905 if (!IS_ROOT(dentry)) { 906 struct dentry *parent = dget_parent(dentry); 907 nfs_readdir_record_entry_cache_miss(d_inode(parent)); 908 dput(parent); 909 } 910 } 911 912 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry) 913 { 914 if (!IS_ROOT(dentry)) { 915 struct dentry *parent = dget_parent(dentry); 916 nfs_readdir_record_entry_cache_hit(d_inode(parent)); 917 dput(parent); 918 } 919 } 920 921 static u32 nfs_get_valid_attrmask(struct inode *inode) 922 { 923 u64 fattr_valid = NFS_SERVER(inode)->fattr_valid; 924 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); 925 u32 reply_mask = STATX_INO | STATX_TYPE; 926 927 if (!(cache_validity & NFS_INO_INVALID_ATIME)) 928 reply_mask |= STATX_ATIME; 929 if (!(cache_validity & NFS_INO_INVALID_CTIME)) 930 reply_mask |= STATX_CTIME; 931 if (!(cache_validity & NFS_INO_INVALID_MTIME)) 932 reply_mask |= STATX_MTIME; 933 if (!(cache_validity & NFS_INO_INVALID_SIZE)) 934 reply_mask |= STATX_SIZE; 935 if (!(cache_validity & NFS_INO_INVALID_NLINK)) 936 reply_mask |= STATX_NLINK; 937 if (!(cache_validity & NFS_INO_INVALID_MODE)) 938 reply_mask |= STATX_MODE; 939 if (!(cache_validity & NFS_INO_INVALID_OTHER)) 940 reply_mask |= STATX_UID | STATX_GID; 941 if (!(cache_validity & NFS_INO_INVALID_BLOCKS)) 942 reply_mask |= STATX_BLOCKS; 943 if (!(cache_validity & NFS_INO_INVALID_BTIME) && 944 (fattr_valid & NFS_ATTR_FATTR_BTIME)) 945 reply_mask |= STATX_BTIME; 946 if (!(cache_validity & NFS_INO_INVALID_CHANGE)) 947 reply_mask |= STATX_CHANGE_COOKIE; 948 return reply_mask; 949 } 950 951 int nfs_getattr(struct mnt_idmap *idmap, const struct path *path, 952 struct kstat *stat, u32 request_mask, unsigned int query_flags) 953 { 954 struct inode *inode = d_inode(path->dentry); 955 struct nfs_server *server = NFS_SERVER(inode); 956 u64 fattr_valid = server->fattr_valid; 957 unsigned long cache_validity; 958 int err = 0; 959 bool force_sync = query_flags & AT_STATX_FORCE_SYNC; 960 bool do_update = false; 961 bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode); 962 963 trace_nfs_getattr_enter(inode); 964 965 request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID | 966 STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME | 967 STATX_INO | STATX_SIZE | STATX_BLOCKS | STATX_BTIME | 968 STATX_CHANGE_COOKIE; 969 970 if (!(fattr_valid & NFS_ATTR_FATTR_BTIME)) 971 request_mask &= ~STATX_BTIME; 972 973 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) { 974 if (readdirplus_enabled) 975 nfs_readdirplus_parent_cache_hit(path->dentry); 976 goto out_no_revalidate; 977 } 978 979 /* Flush out writes to the server in order to update c/mtime/version. */ 980 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) && 981 S_ISREG(inode->i_mode)) { 982 if (nfs_have_delegated_mtime(inode)) 983 filemap_fdatawrite(inode->i_mapping); 984 else 985 filemap_write_and_wait(inode->i_mapping); 986 } 987 988 /* 989 * We may force a getattr if the user cares about atime. 990 * 991 * Note that we only have to check the vfsmount flags here: 992 * - NFS always sets S_NOATIME by so checking it would give a 993 * bogus result 994 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is 995 * no point in checking those. 996 */ 997 if ((path->mnt->mnt_flags & MNT_NOATIME) || 998 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) 999 request_mask &= ~STATX_ATIME; 1000 1001 /* Is the user requesting attributes that might need revalidation? */ 1002 if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME| 1003 STATX_MTIME|STATX_UID|STATX_GID| 1004 STATX_SIZE|STATX_BLOCKS|STATX_BTIME| 1005 STATX_CHANGE_COOKIE))) 1006 goto out_no_revalidate; 1007 1008 /* Check whether the cached attributes are stale */ 1009 do_update |= force_sync || nfs_attribute_cache_expired(inode); 1010 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); 1011 do_update |= cache_validity & NFS_INO_INVALID_CHANGE; 1012 if (request_mask & STATX_ATIME) 1013 do_update |= cache_validity & NFS_INO_INVALID_ATIME; 1014 if (request_mask & STATX_CTIME) 1015 do_update |= cache_validity & NFS_INO_INVALID_CTIME; 1016 if (request_mask & STATX_MTIME) 1017 do_update |= cache_validity & NFS_INO_INVALID_MTIME; 1018 if (request_mask & STATX_SIZE) 1019 do_update |= cache_validity & NFS_INO_INVALID_SIZE; 1020 if (request_mask & STATX_NLINK) 1021 do_update |= cache_validity & NFS_INO_INVALID_NLINK; 1022 if (request_mask & STATX_MODE) 1023 do_update |= cache_validity & NFS_INO_INVALID_MODE; 1024 if (request_mask & (STATX_UID | STATX_GID)) 1025 do_update |= cache_validity & NFS_INO_INVALID_OTHER; 1026 if (request_mask & STATX_BLOCKS) 1027 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS; 1028 if (request_mask & STATX_BTIME) 1029 do_update |= cache_validity & NFS_INO_INVALID_BTIME; 1030 1031 if (do_update) { 1032 if (readdirplus_enabled) 1033 nfs_readdirplus_parent_cache_miss(path->dentry); 1034 err = __nfs_revalidate_inode(server, inode); 1035 if (err) 1036 goto out; 1037 } else if (readdirplus_enabled) 1038 nfs_readdirplus_parent_cache_hit(path->dentry); 1039 out_no_revalidate: 1040 /* Only return attributes that were revalidated. */ 1041 stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask; 1042 1043 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 1044 stat->change_cookie = inode_peek_iversion_raw(inode); 1045 stat->attributes_mask |= STATX_ATTR_CHANGE_MONOTONIC; 1046 if (server->change_attr_type != NFS4_CHANGE_TYPE_IS_UNDEFINED) 1047 stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC; 1048 if (S_ISDIR(inode->i_mode)) 1049 stat->blksize = NFS_SERVER(inode)->dtsize; 1050 stat->btime = NFS_I(inode)->btime; 1051 1052 /* Special handling for STATX_DIOALIGN and STATX_DIO_READ_ALIGN 1053 * - NFS doesn't have DIO alignment constraints, avoid getting 1054 * these DIO attrs from remote and just respond with most 1055 * accommodating limits (so client will issue supported DIO). 1056 * - this is unintuitive, but the most coarse-grained 1057 * dio_offset_align is the most accommodating. 1058 */ 1059 if ((request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN)) && 1060 S_ISREG(inode->i_mode)) { 1061 stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN; 1062 stat->dio_mem_align = 4; /* 4-byte alignment */ 1063 stat->dio_offset_align = PAGE_SIZE; 1064 stat->dio_read_offset_align = stat->dio_offset_align; 1065 } 1066 out: 1067 trace_nfs_getattr_exit(inode, err); 1068 return err; 1069 } 1070 EXPORT_SYMBOL_GPL(nfs_getattr); 1071 1072 int nfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 1073 { 1074 struct inode *inode = d_inode(dentry); 1075 1076 if (nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE)) { 1077 fa->fsx_xflags |= FS_XFLAG_CASEFOLD; 1078 fa->flags |= FS_CASEFOLD_FL; 1079 } 1080 if (nfs_server_capable(inode, NFS_CAP_CASE_NONPRESERVING)) 1081 fa->fsx_xflags |= FS_XFLAG_CASENONPRESERVING; 1082 return 0; 1083 } 1084 EXPORT_SYMBOL_GPL(nfs_fileattr_get); 1085 1086 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx) 1087 { 1088 refcount_set(&l_ctx->count, 1); 1089 l_ctx->lockowner = current->files; 1090 INIT_LIST_HEAD(&l_ctx->list); 1091 atomic_set(&l_ctx->io_count, 0); 1092 } 1093 1094 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx) 1095 { 1096 struct nfs_lock_context *pos; 1097 1098 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) { 1099 if (pos->lockowner != current->files) 1100 continue; 1101 if (refcount_inc_not_zero(&pos->count)) 1102 return pos; 1103 } 1104 return NULL; 1105 } 1106 1107 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx) 1108 { 1109 struct nfs_lock_context *res, *new = NULL; 1110 struct inode *inode = d_inode(ctx->dentry); 1111 1112 rcu_read_lock(); 1113 res = __nfs_find_lock_context(ctx); 1114 rcu_read_unlock(); 1115 if (res == NULL) { 1116 new = kmalloc_obj(*new, GFP_KERNEL_ACCOUNT); 1117 if (new == NULL) 1118 return ERR_PTR(-ENOMEM); 1119 nfs_init_lock_context(new); 1120 spin_lock(&inode->i_lock); 1121 res = __nfs_find_lock_context(ctx); 1122 if (res == NULL) { 1123 new->open_context = get_nfs_open_context(ctx); 1124 if (new->open_context) { 1125 list_add_tail_rcu(&new->list, 1126 &ctx->lock_context.list); 1127 res = new; 1128 new = NULL; 1129 } else 1130 res = ERR_PTR(-EBADF); 1131 } 1132 spin_unlock(&inode->i_lock); 1133 kfree(new); 1134 } 1135 return res; 1136 } 1137 EXPORT_SYMBOL_GPL(nfs_get_lock_context); 1138 1139 void nfs_put_lock_context(struct nfs_lock_context *l_ctx) 1140 { 1141 struct nfs_open_context *ctx = l_ctx->open_context; 1142 struct inode *inode = d_inode(ctx->dentry); 1143 1144 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock)) 1145 return; 1146 list_del_rcu(&l_ctx->list); 1147 spin_unlock(&inode->i_lock); 1148 put_nfs_open_context(ctx); 1149 kfree_rcu(l_ctx, rcu_head); 1150 } 1151 EXPORT_SYMBOL_GPL(nfs_put_lock_context); 1152 1153 /** 1154 * nfs_close_context - Common close_context() routine NFSv2/v3 1155 * @ctx: pointer to context 1156 * @is_sync: is this a synchronous close 1157 * 1158 * Ensure that the attributes are up to date if we're mounted 1159 * with close-to-open semantics and we have cached data that will 1160 * need to be revalidated on open. 1161 */ 1162 void nfs_close_context(struct nfs_open_context *ctx, int is_sync) 1163 { 1164 struct nfs_inode *nfsi; 1165 struct inode *inode; 1166 1167 if (!(ctx->mode & FMODE_WRITE)) 1168 return; 1169 if (!is_sync) 1170 return; 1171 inode = d_inode(ctx->dentry); 1172 if (nfs_have_read_or_write_delegation(inode)) 1173 return; 1174 nfsi = NFS_I(inode); 1175 if (inode->i_mapping->nrpages == 0) 1176 return; 1177 if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 1178 return; 1179 if (!list_empty(&nfsi->open_files)) 1180 return; 1181 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO) 1182 return; 1183 nfs_revalidate_inode(inode, 1184 NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); 1185 } 1186 EXPORT_SYMBOL_GPL(nfs_close_context); 1187 1188 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, 1189 fmode_t f_mode, 1190 struct file *filp) 1191 { 1192 struct nfs_open_context *ctx; 1193 1194 ctx = kmalloc_obj(*ctx, GFP_KERNEL_ACCOUNT); 1195 if (!ctx) 1196 return ERR_PTR(-ENOMEM); 1197 nfs_sb_active(dentry->d_sb); 1198 ctx->dentry = dget(dentry); 1199 if (filp) 1200 ctx->cred = get_cred(filp->f_cred); 1201 else 1202 ctx->cred = get_current_cred(); 1203 rcu_assign_pointer(ctx->ll_cred, NULL); 1204 ctx->state = NULL; 1205 ctx->mode = f_mode; 1206 ctx->flags = 0; 1207 ctx->error = 0; 1208 ctx->flock_owner = (fl_owner_t)filp; 1209 nfs_init_lock_context(&ctx->lock_context); 1210 ctx->lock_context.open_context = ctx; 1211 INIT_LIST_HEAD(&ctx->list); 1212 ctx->mdsthreshold = NULL; 1213 nfs_localio_file_init(&ctx->nfl); 1214 1215 return ctx; 1216 } 1217 EXPORT_SYMBOL_GPL(alloc_nfs_open_context); 1218 1219 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) 1220 { 1221 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count)) 1222 return ctx; 1223 return NULL; 1224 } 1225 EXPORT_SYMBOL_GPL(get_nfs_open_context); 1226 1227 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) 1228 { 1229 struct inode *inode = d_inode(ctx->dentry); 1230 struct super_block *sb = ctx->dentry->d_sb; 1231 1232 if (!refcount_dec_and_test(&ctx->lock_context.count)) 1233 return; 1234 if (!list_empty(&ctx->list)) { 1235 spin_lock(&inode->i_lock); 1236 list_del_rcu(&ctx->list); 1237 spin_unlock(&inode->i_lock); 1238 } 1239 if (inode != NULL) 1240 NFS_PROTO(inode)->close_context(ctx, is_sync); 1241 put_cred(ctx->cred); 1242 dput(ctx->dentry); 1243 nfs_sb_deactive(sb); 1244 put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1)); 1245 kfree(ctx->mdsthreshold); 1246 nfs_close_local_fh(&ctx->nfl); 1247 kfree_rcu(ctx, rcu_head); 1248 } 1249 1250 void put_nfs_open_context(struct nfs_open_context *ctx) 1251 { 1252 __put_nfs_open_context(ctx, 0); 1253 } 1254 EXPORT_SYMBOL_GPL(put_nfs_open_context); 1255 1256 static void put_nfs_open_context_sync(struct nfs_open_context *ctx) 1257 { 1258 __put_nfs_open_context(ctx, 1); 1259 } 1260 1261 /* 1262 * Ensure that mmap has a recent RPC credential for use when writing out 1263 * shared pages 1264 */ 1265 void nfs_inode_attach_open_context(struct nfs_open_context *ctx) 1266 { 1267 struct inode *inode = d_inode(ctx->dentry); 1268 struct nfs_inode *nfsi = NFS_I(inode); 1269 1270 spin_lock(&inode->i_lock); 1271 if (list_empty(&nfsi->open_files) && 1272 nfs_ooo_test(nfsi)) 1273 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA | 1274 NFS_INO_REVAL_FORCED); 1275 list_add_tail_rcu(&ctx->list, &nfsi->open_files); 1276 spin_unlock(&inode->i_lock); 1277 } 1278 EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context); 1279 1280 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx) 1281 { 1282 filp->private_data = get_nfs_open_context(ctx); 1283 set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); 1284 if (list_empty(&ctx->list)) 1285 nfs_inode_attach_open_context(ctx); 1286 } 1287 EXPORT_SYMBOL_GPL(nfs_file_set_open_context); 1288 1289 /* 1290 * Given an inode, search for an open context with the desired characteristics 1291 */ 1292 struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode) 1293 { 1294 struct nfs_inode *nfsi = NFS_I(inode); 1295 struct nfs_open_context *pos, *ctx = NULL; 1296 1297 rcu_read_lock(); 1298 list_for_each_entry_rcu(pos, &nfsi->open_files, list) { 1299 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0) 1300 continue; 1301 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode) 1302 continue; 1303 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags)) 1304 continue; 1305 ctx = get_nfs_open_context(pos); 1306 if (ctx) 1307 break; 1308 } 1309 rcu_read_unlock(); 1310 return ctx; 1311 } 1312 1313 void nfs_file_clear_open_context(struct file *filp) 1314 { 1315 struct nfs_open_context *ctx = nfs_file_open_context(filp); 1316 1317 if (ctx) { 1318 struct inode *inode = d_inode(ctx->dentry); 1319 1320 clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); 1321 /* 1322 * We fatal error on write before. Try to writeback 1323 * every page again. 1324 */ 1325 if (ctx->error < 0) 1326 invalidate_inode_pages2(inode->i_mapping); 1327 filp->private_data = NULL; 1328 put_nfs_open_context_sync(ctx); 1329 } 1330 } 1331 1332 /* 1333 * These allocate and release file read/write context information. 1334 */ 1335 int nfs_open(struct inode *inode, struct file *filp) 1336 { 1337 struct nfs_open_context *ctx; 1338 1339 ctx = alloc_nfs_open_context(file_dentry(filp), 1340 flags_to_mode(filp->f_flags), filp); 1341 if (IS_ERR(ctx)) 1342 return PTR_ERR(ctx); 1343 nfs_file_set_open_context(filp, ctx); 1344 put_nfs_open_context(ctx); 1345 nfs_fscache_open_file(inode, filp); 1346 return 0; 1347 } 1348 1349 /* 1350 * This function is called whenever some part of NFS notices that 1351 * the cached attributes have to be refreshed. 1352 */ 1353 int 1354 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode) 1355 { 1356 int status = -ESTALE; 1357 struct nfs_fattr *fattr = NULL; 1358 struct nfs_inode *nfsi = NFS_I(inode); 1359 1360 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n", 1361 inode->i_sb->s_id, (unsigned long long)inode->i_ino); 1362 1363 trace_nfs_revalidate_inode_enter(inode); 1364 1365 if (is_bad_inode(inode)) 1366 goto out; 1367 if (NFS_STALE(inode)) 1368 goto out; 1369 1370 /* pNFS: Attributes aren't updated until we layoutcommit */ 1371 if (S_ISREG(inode->i_mode)) { 1372 status = pnfs_sync_inode(inode, false); 1373 if (status) 1374 goto out; 1375 } else if (nfs_have_directory_delegation(inode) && 1376 !(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)) { 1377 status = 0; 1378 goto out; 1379 } 1380 1381 status = -ENOMEM; 1382 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); 1383 if (fattr == NULL) 1384 goto out; 1385 1386 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE); 1387 1388 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode); 1389 if (status != 0) { 1390 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n", 1391 inode->i_sb->s_id, 1392 (unsigned long long)inode->i_ino, status); 1393 switch (status) { 1394 case -ETIMEDOUT: 1395 /* A soft timeout occurred. Use cached information? */ 1396 if (server->flags & NFS_MOUNT_SOFTREVAL) 1397 status = 0; 1398 break; 1399 case -ESTALE: 1400 if (!S_ISDIR(inode->i_mode)) 1401 nfs_set_inode_stale(inode); 1402 else 1403 nfs_zap_caches(inode); 1404 } 1405 goto out; 1406 } 1407 1408 status = nfs_refresh_inode(inode, fattr); 1409 if (status) { 1410 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n", 1411 inode->i_sb->s_id, 1412 (unsigned long long)inode->i_ino, status); 1413 goto out; 1414 } 1415 1416 if (nfsi->cache_validity & NFS_INO_INVALID_ACL) 1417 nfs_zap_acl_cache(inode); 1418 1419 nfs_setsecurity(inode, fattr); 1420 1421 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n", 1422 inode->i_sb->s_id, 1423 (unsigned long long)inode->i_ino); 1424 1425 out: 1426 nfs_free_fattr(fattr); 1427 trace_nfs_revalidate_inode_exit(inode, status); 1428 return status; 1429 } 1430 1431 int nfs_attribute_cache_expired(struct inode *inode) 1432 { 1433 if (nfs_have_delegated_attributes(inode)) 1434 return 0; 1435 return nfs_attribute_timeout(inode); 1436 } 1437 1438 /** 1439 * nfs_revalidate_inode - Revalidate the inode attributes 1440 * @inode: pointer to inode struct 1441 * @flags: cache flags to check 1442 * 1443 * Updates inode attribute information by retrieving the data from the server. 1444 */ 1445 int nfs_revalidate_inode(struct inode *inode, unsigned long flags) 1446 { 1447 if (!nfs_check_cache_invalid(inode, flags)) 1448 return NFS_STALE(inode) ? -ESTALE : 0; 1449 return __nfs_revalidate_inode(NFS_SERVER(inode), inode); 1450 } 1451 EXPORT_SYMBOL_GPL(nfs_revalidate_inode); 1452 1453 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping) 1454 { 1455 int ret; 1456 1457 nfs_fscache_invalidate(inode, 0); 1458 if (mapping->nrpages != 0) { 1459 if (S_ISREG(inode->i_mode)) { 1460 ret = nfs_sync_mapping(mapping); 1461 if (ret < 0) 1462 return ret; 1463 } 1464 ret = invalidate_inode_pages2(mapping); 1465 if (ret < 0) 1466 return ret; 1467 } 1468 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); 1469 1470 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n", 1471 inode->i_sb->s_id, 1472 (unsigned long long)inode->i_ino); 1473 return 0; 1474 } 1475 1476 /** 1477 * nfs_clear_invalid_mapping - Conditionally clear a mapping 1478 * @mapping: pointer to mapping 1479 * 1480 * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping. 1481 */ 1482 int nfs_clear_invalid_mapping(struct address_space *mapping) 1483 { 1484 struct inode *inode = mapping->host; 1485 struct nfs_inode *nfsi = NFS_I(inode); 1486 unsigned long *bitlock = &nfsi->flags; 1487 int ret = 0; 1488 1489 /* 1490 * We must clear NFS_INO_INVALID_DATA first to ensure that 1491 * invalidations that come in while we're shooting down the mappings 1492 * are respected. But, that leaves a race window where one revalidator 1493 * can clear the flag, and then another checks it before the mapping 1494 * gets invalidated. Fix that by serializing access to this part of 1495 * the function. 1496 * 1497 * At the same time, we need to allow other tasks to see whether we 1498 * might be in the middle of invalidating the pages, so we only set 1499 * the bit lock here if it looks like we're going to be doing that. 1500 */ 1501 for (;;) { 1502 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING, 1503 nfs_wait_bit_killable, 1504 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); 1505 if (ret) 1506 goto out; 1507 smp_rmb(); /* pairs with smp_wmb() below */ 1508 if (test_bit(NFS_INO_INVALIDATING, bitlock)) 1509 continue; 1510 /* pairs with nfs_set_cache_invalid()'s smp_store_release() */ 1511 if (!(smp_load_acquire(&nfsi->cache_validity) & NFS_INO_INVALID_DATA)) 1512 goto out; 1513 /* Slow-path that double-checks with spinlock held */ 1514 spin_lock(&inode->i_lock); 1515 if (test_bit(NFS_INO_INVALIDATING, bitlock)) { 1516 spin_unlock(&inode->i_lock); 1517 continue; 1518 } 1519 if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 1520 break; 1521 spin_unlock(&inode->i_lock); 1522 goto out; 1523 } 1524 1525 set_bit(NFS_INO_INVALIDATING, bitlock); 1526 smp_wmb(); 1527 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA; 1528 nfs_ooo_clear(nfsi); 1529 spin_unlock(&inode->i_lock); 1530 trace_nfs_invalidate_mapping_enter(inode); 1531 ret = nfs_invalidate_mapping(inode, mapping); 1532 trace_nfs_invalidate_mapping_exit(inode, ret); 1533 1534 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock); 1535 smp_mb__after_atomic(); 1536 wake_up_bit(bitlock, NFS_INO_INVALIDATING); 1537 out: 1538 return ret; 1539 } 1540 1541 bool nfs_mapping_need_revalidate_inode(struct inode *inode) 1542 { 1543 return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) || 1544 NFS_STALE(inode); 1545 } 1546 1547 int nfs_revalidate_mapping_rcu(struct inode *inode) 1548 { 1549 struct nfs_inode *nfsi = NFS_I(inode); 1550 unsigned long *bitlock = &nfsi->flags; 1551 int ret = 0; 1552 1553 if (IS_SWAPFILE(inode)) 1554 goto out; 1555 if (nfs_mapping_need_revalidate_inode(inode)) { 1556 ret = -ECHILD; 1557 goto out; 1558 } 1559 spin_lock(&inode->i_lock); 1560 if (test_bit(NFS_INO_INVALIDATING, bitlock) || 1561 (nfsi->cache_validity & NFS_INO_INVALID_DATA)) 1562 ret = -ECHILD; 1563 spin_unlock(&inode->i_lock); 1564 out: 1565 return ret; 1566 } 1567 1568 /** 1569 * nfs_revalidate_mapping - Revalidate the pagecache 1570 * @inode: pointer to host inode 1571 * @mapping: pointer to mapping 1572 */ 1573 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping) 1574 { 1575 /* swapfiles are not supposed to be shared. */ 1576 if (IS_SWAPFILE(inode)) 1577 return 0; 1578 1579 if (nfs_mapping_need_revalidate_inode(inode)) { 1580 int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 1581 if (ret < 0) 1582 return ret; 1583 } 1584 1585 return nfs_clear_invalid_mapping(mapping); 1586 } 1587 1588 static bool nfs_file_has_writers(struct nfs_inode *nfsi) 1589 { 1590 struct inode *inode = &nfsi->vfs_inode; 1591 1592 if (!S_ISREG(inode->i_mode)) 1593 return false; 1594 if (list_empty(&nfsi->open_files)) 1595 return false; 1596 return inode_is_open_for_write(inode); 1597 } 1598 1599 static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi) 1600 { 1601 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi); 1602 } 1603 1604 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr) 1605 { 1606 struct timespec64 ts; 1607 1608 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE) 1609 && (fattr->valid & NFS_ATTR_FATTR_CHANGE) 1610 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) { 1611 inode_set_iversion_raw(inode, fattr->change_attr); 1612 if (S_ISDIR(inode->i_mode)) 1613 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); 1614 else if (nfs_server_capable(inode, NFS_CAP_XATTR)) 1615 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR); 1616 } 1617 /* If we have atomic WCC data, we may update some attributes */ 1618 ts = inode_get_ctime(inode); 1619 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME) 1620 && (fattr->valid & NFS_ATTR_FATTR_CTIME) 1621 && timespec64_equal(&ts, &fattr->pre_ctime)) { 1622 inode_set_ctime_to_ts(inode, fattr->ctime); 1623 } 1624 1625 ts = inode_get_mtime(inode); 1626 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME) 1627 && (fattr->valid & NFS_ATTR_FATTR_MTIME) 1628 && timespec64_equal(&ts, &fattr->pre_mtime)) { 1629 inode_set_mtime_to_ts(inode, fattr->mtime); 1630 } 1631 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE) 1632 && (fattr->valid & NFS_ATTR_FATTR_SIZE) 1633 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size) 1634 && !nfs_have_writebacks(inode)) { 1635 trace_nfs_size_wcc(inode, fattr->size); 1636 i_size_write(inode, nfs_size_to_loff_t(fattr->size)); 1637 } 1638 } 1639 1640 /** 1641 * nfs_check_inode_attributes - verify consistency of the inode attribute cache 1642 * @inode: pointer to inode 1643 * @fattr: updated attributes 1644 * 1645 * Verifies the attribute cache. If we have just changed the attributes, 1646 * so that fattr carries weak cache consistency data, then it may 1647 * also update the ctime/mtime/change_attribute. 1648 */ 1649 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr) 1650 { 1651 struct nfs_inode *nfsi = NFS_I(inode); 1652 loff_t cur_size, new_isize; 1653 unsigned long invalid = 0; 1654 struct timespec64 ts; 1655 1656 if (nfs_have_delegated_attributes(inode)) 1657 return 0; 1658 1659 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { 1660 /* Only a mounted-on-fileid? Just exit */ 1661 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) 1662 return 0; 1663 /* Has the inode gone and changed behind our back? */ 1664 } else if (inode->i_ino != fattr->fileid) { 1665 /* Is this perhaps the mounted-on fileid? */ 1666 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && 1667 inode->i_ino == fattr->mounted_on_fileid) 1668 return 0; 1669 return -ESTALE; 1670 } 1671 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) 1672 return -ESTALE; 1673 1674 1675 if (!nfs_file_has_buffered_writers(nfsi)) { 1676 /* Verify a few of the more important attributes */ 1677 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr)) 1678 invalid |= NFS_INO_INVALID_CHANGE; 1679 1680 ts = inode_get_mtime(inode); 1681 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime)) 1682 invalid |= NFS_INO_INVALID_MTIME; 1683 1684 ts = inode_get_ctime(inode); 1685 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime)) 1686 invalid |= NFS_INO_INVALID_CTIME; 1687 1688 if (fattr->valid & NFS_ATTR_FATTR_SIZE) { 1689 cur_size = i_size_read(inode); 1690 new_isize = nfs_size_to_loff_t(fattr->size); 1691 if (cur_size != new_isize) 1692 invalid |= NFS_INO_INVALID_SIZE; 1693 } 1694 } 1695 1696 /* Have any file permissions changed? */ 1697 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) 1698 invalid |= NFS_INO_INVALID_MODE; 1699 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid)) 1700 invalid |= NFS_INO_INVALID_OTHER; 1701 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid)) 1702 invalid |= NFS_INO_INVALID_OTHER; 1703 1704 /* Has the link count changed? */ 1705 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink) 1706 invalid |= NFS_INO_INVALID_NLINK; 1707 1708 ts = inode_get_atime(inode); 1709 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime)) 1710 invalid |= NFS_INO_INVALID_ATIME; 1711 1712 if (invalid != 0) 1713 nfs_set_cache_invalid(inode, invalid); 1714 1715 nfsi->read_cache_jiffies = fattr->time_start; 1716 return 0; 1717 } 1718 1719 static atomic_long_t nfs_attr_generation_counter; 1720 1721 static unsigned long nfs_read_attr_generation_counter(void) 1722 { 1723 return atomic_long_read(&nfs_attr_generation_counter); 1724 } 1725 1726 unsigned long nfs_inc_attr_generation_counter(void) 1727 { 1728 return atomic_long_inc_return(&nfs_attr_generation_counter); 1729 } 1730 EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter); 1731 1732 void nfs_fattr_init(struct nfs_fattr *fattr) 1733 { 1734 fattr->valid = 0; 1735 fattr->time_start = jiffies; 1736 fattr->gencount = nfs_inc_attr_generation_counter(); 1737 fattr->owner_name = NULL; 1738 fattr->group_name = NULL; 1739 fattr->mdsthreshold = NULL; 1740 } 1741 EXPORT_SYMBOL_GPL(nfs_fattr_init); 1742 1743 /** 1744 * nfs_fattr_set_barrier 1745 * @fattr: attributes 1746 * 1747 * Used to set a barrier after an attribute was updated. This 1748 * barrier ensures that older attributes from RPC calls that may 1749 * have raced with our update cannot clobber these new values. 1750 * Note that you are still responsible for ensuring that other 1751 * operations which change the attribute on the server do not 1752 * collide. 1753 */ 1754 void nfs_fattr_set_barrier(struct nfs_fattr *fattr) 1755 { 1756 fattr->gencount = nfs_inc_attr_generation_counter(); 1757 } 1758 1759 struct nfs_fattr *nfs_alloc_fattr(void) 1760 { 1761 struct nfs_fattr *fattr; 1762 1763 fattr = kmalloc_obj(*fattr); 1764 if (fattr != NULL) { 1765 nfs_fattr_init(fattr); 1766 fattr->label = NULL; 1767 } 1768 return fattr; 1769 } 1770 EXPORT_SYMBOL_GPL(nfs_alloc_fattr); 1771 1772 struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server) 1773 { 1774 struct nfs_fattr *fattr = nfs_alloc_fattr(); 1775 1776 if (!fattr) 1777 return NULL; 1778 1779 fattr->label = nfs4_label_alloc(server, GFP_KERNEL); 1780 if (IS_ERR(fattr->label)) { 1781 kfree(fattr); 1782 return NULL; 1783 } 1784 1785 return fattr; 1786 } 1787 EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label); 1788 1789 struct nfs_fh *nfs_alloc_fhandle(void) 1790 { 1791 struct nfs_fh *fh; 1792 1793 fh = kmalloc_obj(struct nfs_fh); 1794 if (fh != NULL) 1795 fh->size = 0; 1796 return fh; 1797 } 1798 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle); 1799 1800 #ifdef NFS_DEBUG 1801 /* 1802 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle 1803 * in the same way that wireshark does 1804 * 1805 * @fh: file handle 1806 * 1807 * For debugging only. 1808 */ 1809 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh) 1810 { 1811 /* wireshark uses 32-bit AUTODIN crc and does a bitwise 1812 * not on the result */ 1813 return nfs_fhandle_hash(fh); 1814 } 1815 EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash); 1816 1817 /* 1818 * _nfs_display_fhandle - display an NFS file handle on the console 1819 * 1820 * @fh: file handle to display 1821 * @caption: display caption 1822 * 1823 * For debugging only. 1824 */ 1825 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption) 1826 { 1827 unsigned short i; 1828 1829 if (fh == NULL || fh->size == 0) { 1830 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh); 1831 return; 1832 } 1833 1834 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n", 1835 caption, fh, fh->size, _nfs_display_fhandle_hash(fh)); 1836 for (i = 0; i < fh->size; i += 16) { 1837 __be32 *pos = (__be32 *)&fh->data[i]; 1838 1839 switch ((fh->size - i - 1) >> 2) { 1840 case 0: 1841 printk(KERN_DEFAULT " %08x\n", 1842 be32_to_cpup(pos)); 1843 break; 1844 case 1: 1845 printk(KERN_DEFAULT " %08x %08x\n", 1846 be32_to_cpup(pos), be32_to_cpup(pos + 1)); 1847 break; 1848 case 2: 1849 printk(KERN_DEFAULT " %08x %08x %08x\n", 1850 be32_to_cpup(pos), be32_to_cpup(pos + 1), 1851 be32_to_cpup(pos + 2)); 1852 break; 1853 default: 1854 printk(KERN_DEFAULT " %08x %08x %08x %08x\n", 1855 be32_to_cpup(pos), be32_to_cpup(pos + 1), 1856 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3)); 1857 } 1858 } 1859 } 1860 EXPORT_SYMBOL_GPL(_nfs_display_fhandle); 1861 #endif 1862 1863 /** 1864 * nfs_inode_attrs_cmp_generic - compare attributes 1865 * @fattr: attributes 1866 * @inode: pointer to inode 1867 * 1868 * Attempt to divine whether or not an RPC call reply carrying stale 1869 * attributes got scheduled after another call carrying updated ones. 1870 * Note also the check for wraparound of 'attr_gencount' 1871 * 1872 * The function returns '1' if it thinks the attributes in @fattr are 1873 * more recent than the ones cached in @inode. Otherwise it returns 1874 * the value '0'. 1875 */ 1876 static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr, 1877 const struct inode *inode) 1878 { 1879 unsigned long attr_gencount = NFS_I(inode)->attr_gencount; 1880 1881 return (long)(fattr->gencount - attr_gencount) > 0 || 1882 (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0; 1883 } 1884 1885 /** 1886 * nfs_inode_attrs_cmp_monotonic - compare attributes 1887 * @fattr: attributes 1888 * @inode: pointer to inode 1889 * 1890 * Attempt to divine whether or not an RPC call reply carrying stale 1891 * attributes got scheduled after another call carrying updated ones. 1892 * 1893 * We assume that the server observes monotonic semantics for 1894 * the change attribute, so a larger value means that the attributes in 1895 * @fattr are more recent, in which case the function returns the 1896 * value '1'. 1897 * A return value of '0' indicates no measurable change 1898 * A return value of '-1' means that the attributes in @inode are 1899 * more recent. 1900 */ 1901 static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr, 1902 const struct inode *inode) 1903 { 1904 s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode); 1905 if (diff > 0) 1906 return 1; 1907 return diff == 0 ? 0 : -1; 1908 } 1909 1910 /** 1911 * nfs_inode_attrs_cmp_strict_monotonic - compare attributes 1912 * @fattr: attributes 1913 * @inode: pointer to inode 1914 * 1915 * Attempt to divine whether or not an RPC call reply carrying stale 1916 * attributes got scheduled after another call carrying updated ones. 1917 * 1918 * We assume that the server observes strictly monotonic semantics for 1919 * the change attribute, so a larger value means that the attributes in 1920 * @fattr are more recent, in which case the function returns the 1921 * value '1'. 1922 * A return value of '-1' means that the attributes in @inode are 1923 * more recent or unchanged. 1924 */ 1925 static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr, 1926 const struct inode *inode) 1927 { 1928 return nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1; 1929 } 1930 1931 /** 1932 * nfs_inode_attrs_cmp - compare attributes 1933 * @fattr: attributes 1934 * @inode: pointer to inode 1935 * 1936 * This function returns '1' if it thinks the attributes in @fattr are 1937 * more recent than the ones cached in @inode. It returns '-1' if 1938 * the attributes in @inode are more recent than the ones in @fattr, 1939 * and it returns 0 if not sure. 1940 */ 1941 static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr, 1942 const struct inode *inode) 1943 { 1944 if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0) 1945 return 1; 1946 switch (NFS_SERVER(inode)->change_attr_type) { 1947 case NFS4_CHANGE_TYPE_IS_UNDEFINED: 1948 break; 1949 case NFS4_CHANGE_TYPE_IS_TIME_METADATA: 1950 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) 1951 break; 1952 return nfs_inode_attrs_cmp_monotonic(fattr, inode); 1953 default: 1954 if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) 1955 break; 1956 return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode); 1957 } 1958 return 0; 1959 } 1960 1961 /** 1962 * nfs_inode_finish_partial_attr_update - complete a previous inode update 1963 * @fattr: attributes 1964 * @inode: pointer to inode 1965 * 1966 * Returns '1' if the last attribute update left the inode cached 1967 * attributes in a partially unrevalidated state, and @fattr 1968 * matches the change attribute of that partial update. 1969 * Otherwise returns '0'. 1970 */ 1971 static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr, 1972 const struct inode *inode) 1973 { 1974 const unsigned long check_valid = 1975 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME | 1976 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | 1977 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER | 1978 NFS_INO_INVALID_NLINK | NFS_INO_INVALID_BTIME; 1979 unsigned long cache_validity = NFS_I(inode)->cache_validity; 1980 enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type; 1981 1982 if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED && 1983 !(cache_validity & NFS_INO_INVALID_CHANGE) && 1984 (cache_validity & check_valid) != 0 && 1985 (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && 1986 nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0) 1987 return 1; 1988 return 0; 1989 } 1990 1991 static void nfs_ooo_merge(struct nfs_inode *nfsi, 1992 u64 start, u64 end) 1993 { 1994 int i, cnt; 1995 1996 if (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER) 1997 /* No point merging anything */ 1998 return; 1999 2000 if (!nfsi->ooo) { 2001 nfsi->ooo = kmalloc_obj(*nfsi->ooo, GFP_ATOMIC); 2002 if (!nfsi->ooo) { 2003 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER; 2004 return; 2005 } 2006 nfsi->ooo->cnt = 0; 2007 } 2008 2009 /* add this range, merging if possible */ 2010 cnt = nfsi->ooo->cnt; 2011 for (i = 0; i < cnt; i++) { 2012 if (end == nfsi->ooo->gap[i].start) 2013 end = nfsi->ooo->gap[i].end; 2014 else if (start == nfsi->ooo->gap[i].end) 2015 start = nfsi->ooo->gap[i].start; 2016 else 2017 continue; 2018 /* Remove 'i' from table and loop to insert the new range */ 2019 cnt -= 1; 2020 nfsi->ooo->gap[i] = nfsi->ooo->gap[cnt]; 2021 i = -1; 2022 } 2023 if (start != end) { 2024 if (cnt >= ARRAY_SIZE(nfsi->ooo->gap)) { 2025 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER; 2026 kfree(nfsi->ooo); 2027 nfsi->ooo = NULL; 2028 return; 2029 } 2030 nfsi->ooo->gap[cnt].start = start; 2031 nfsi->ooo->gap[cnt].end = end; 2032 cnt += 1; 2033 } 2034 nfsi->ooo->cnt = cnt; 2035 } 2036 2037 static void nfs_ooo_record(struct nfs_inode *nfsi, 2038 struct nfs_fattr *fattr) 2039 { 2040 /* This reply was out-of-order, so record in the 2041 * pre/post change id, possibly cancelling 2042 * gaps created when iversion was jumpped forward. 2043 */ 2044 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) && 2045 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) 2046 nfs_ooo_merge(nfsi, 2047 fattr->change_attr, 2048 fattr->pre_change_attr); 2049 } 2050 2051 static int nfs_refresh_inode_locked(struct inode *inode, 2052 struct nfs_fattr *fattr) 2053 { 2054 int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); 2055 int ret = 0; 2056 2057 trace_nfs_refresh_inode_enter(inode); 2058 2059 if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode)) 2060 ret = nfs_update_inode(inode, fattr); 2061 else { 2062 nfs_ooo_record(NFS_I(inode), fattr); 2063 2064 if (attr_cmp == 0) 2065 ret = nfs_check_inode_attributes(inode, fattr); 2066 } 2067 2068 trace_nfs_refresh_inode_exit(inode, ret); 2069 return ret; 2070 } 2071 2072 /** 2073 * nfs_refresh_inode - try to update the inode attribute cache 2074 * @inode: pointer to inode 2075 * @fattr: updated attributes 2076 * 2077 * Check that an RPC call that returned attributes has not overlapped with 2078 * other recent updates of the inode metadata, then decide whether it is 2079 * safe to do a full update of the inode attributes, or whether just to 2080 * call nfs_check_inode_attributes. 2081 */ 2082 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr) 2083 { 2084 int status; 2085 2086 if ((fattr->valid & NFS_ATTR_FATTR) == 0) 2087 return 0; 2088 spin_lock(&inode->i_lock); 2089 status = nfs_refresh_inode_locked(inode, fattr); 2090 spin_unlock(&inode->i_lock); 2091 2092 return status; 2093 } 2094 EXPORT_SYMBOL_GPL(nfs_refresh_inode); 2095 2096 static int nfs_post_op_update_inode_locked(struct inode *inode, 2097 struct nfs_fattr *fattr, unsigned int invalid) 2098 { 2099 if (S_ISDIR(inode->i_mode)) 2100 invalid |= NFS_INO_INVALID_DATA; 2101 nfs_set_cache_invalid(inode, invalid); 2102 if ((fattr->valid & NFS_ATTR_FATTR) == 0) 2103 return 0; 2104 return nfs_refresh_inode_locked(inode, fattr); 2105 } 2106 2107 /** 2108 * nfs_post_op_update_inode - try to update the inode attribute cache 2109 * @inode: pointer to inode 2110 * @fattr: updated attributes 2111 * 2112 * After an operation that has changed the inode metadata, mark the 2113 * attribute cache as being invalid, then try to update it. 2114 * 2115 * NB: if the server didn't return any post op attributes, this 2116 * function will force the retrieval of attributes before the next 2117 * NFS request. Thus it should be used only for operations that 2118 * are expected to change one or more attributes, to avoid 2119 * unnecessary NFS requests and trips through nfs_update_inode(). 2120 */ 2121 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr) 2122 { 2123 int status; 2124 2125 spin_lock(&inode->i_lock); 2126 nfs_fattr_set_barrier(fattr); 2127 status = nfs_post_op_update_inode_locked(inode, fattr, 2128 NFS_INO_INVALID_CHANGE 2129 | NFS_INO_INVALID_CTIME 2130 | NFS_INO_REVAL_FORCED); 2131 spin_unlock(&inode->i_lock); 2132 2133 return status; 2134 } 2135 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode); 2136 2137 /** 2138 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache 2139 * @inode: pointer to inode 2140 * @fattr: updated attributes 2141 * 2142 * After an operation that has changed the inode metadata, mark the 2143 * attribute cache as being invalid, then try to update it. Fake up 2144 * weak cache consistency data, if none exist. 2145 * 2146 * This function is mainly designed to be used by the ->write_done() functions. 2147 */ 2148 int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr) 2149 { 2150 int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); 2151 int status; 2152 2153 /* Don't do a WCC update if these attributes are already stale */ 2154 if (attr_cmp < 0) 2155 return 0; 2156 if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) { 2157 /* Record the pre/post change info before clearing PRECHANGE */ 2158 nfs_ooo_record(NFS_I(inode), fattr); 2159 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE 2160 | NFS_ATTR_FATTR_PRESIZE 2161 | NFS_ATTR_FATTR_PREMTIME 2162 | NFS_ATTR_FATTR_PRECTIME); 2163 goto out_noforce; 2164 } 2165 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && 2166 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) { 2167 fattr->pre_change_attr = inode_peek_iversion_raw(inode); 2168 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE; 2169 } 2170 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 && 2171 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) { 2172 fattr->pre_ctime = inode_get_ctime(inode); 2173 fattr->valid |= NFS_ATTR_FATTR_PRECTIME; 2174 } 2175 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 && 2176 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) { 2177 fattr->pre_mtime = inode_get_mtime(inode); 2178 fattr->valid |= NFS_ATTR_FATTR_PREMTIME; 2179 } 2180 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 && 2181 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) { 2182 fattr->pre_size = i_size_read(inode); 2183 fattr->valid |= NFS_ATTR_FATTR_PRESIZE; 2184 } 2185 out_noforce: 2186 status = nfs_post_op_update_inode_locked(inode, fattr, 2187 NFS_INO_INVALID_CHANGE 2188 | NFS_INO_INVALID_CTIME 2189 | NFS_INO_INVALID_MTIME 2190 | NFS_INO_INVALID_BLOCKS); 2191 return status; 2192 } 2193 2194 /** 2195 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache 2196 * @inode: pointer to inode 2197 * @fattr: updated attributes 2198 * 2199 * After an operation that has changed the inode metadata, mark the 2200 * attribute cache as being invalid, then try to update it. Fake up 2201 * weak cache consistency data, if none exist. 2202 * 2203 * This function is mainly designed to be used by the ->write_done() functions. 2204 */ 2205 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr) 2206 { 2207 int status; 2208 2209 spin_lock(&inode->i_lock); 2210 nfs_fattr_set_barrier(fattr); 2211 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr); 2212 spin_unlock(&inode->i_lock); 2213 return status; 2214 } 2215 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc); 2216 2217 2218 /* 2219 * Many nfs protocol calls return the new file attributes after 2220 * an operation. Here we update the inode to reflect the state 2221 * of the server's inode. 2222 * 2223 * This is a bit tricky because we have to make sure all dirty pages 2224 * have been sent off to the server before calling invalidate_inode_pages. 2225 * To make sure no other process adds more write requests while we try 2226 * our best to flush them, we make them sleep during the attribute refresh. 2227 * 2228 * A very similar scenario holds for the dir cache. 2229 */ 2230 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr) 2231 { 2232 struct nfs_server *server = NFS_SERVER(inode); 2233 struct nfs_inode *nfsi = NFS_I(inode); 2234 loff_t cur_isize, new_isize; 2235 u64 fattr_supported = server->fattr_valid; 2236 unsigned long invalid = 0; 2237 unsigned long now = jiffies; 2238 unsigned long save_cache_validity; 2239 bool have_writers = nfs_file_has_buffered_writers(nfsi); 2240 bool cache_revalidated = true; 2241 bool attr_changed = false; 2242 bool have_delegation; 2243 2244 dfprintk(VFS, "NFS: %s(%s/%llu fh_crc=0x%08x ct=%d info=0x%llx)\n", 2245 __func__, inode->i_sb->s_id, inode->i_ino, 2246 nfs_display_fhandle_hash(NFS_FH(inode)), 2247 icount_read_once(inode), fattr->valid); 2248 2249 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { 2250 /* Only a mounted-on-fileid? Just exit */ 2251 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) 2252 return 0; 2253 /* Has the inode gone and changed behind our back? */ 2254 } else if (inode->i_ino != fattr->fileid) { 2255 /* Is this perhaps the mounted-on fileid? */ 2256 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && 2257 inode->i_ino == fattr->mounted_on_fileid) 2258 return 0; 2259 printk(KERN_ERR "NFS: server %s error: fileid changed\n" 2260 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n", 2261 NFS_SERVER(inode)->nfs_client->cl_hostname, 2262 inode->i_sb->s_id, (long long)inode->i_ino, 2263 (long long)fattr->fileid); 2264 goto out_err; 2265 } 2266 2267 /* 2268 * Make sure the inode's type hasn't changed. 2269 */ 2270 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) { 2271 /* 2272 * Big trouble! The inode has become a different object. 2273 */ 2274 printk(KERN_DEBUG "NFS: %s: inode %llu mode changed, %07o to %07o\n", 2275 __func__, inode->i_ino, inode->i_mode, fattr->mode); 2276 goto out_err; 2277 } 2278 2279 /* Update the fsid? */ 2280 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) && 2281 !nfs_fsid_equal(&server->fsid, &fattr->fsid) && 2282 !IS_AUTOMOUNT(inode)) 2283 server->fsid = fattr->fsid; 2284 2285 /* Save the delegation state before clearing cache_validity */ 2286 have_delegation = nfs_have_delegated_attributes(inode); 2287 2288 /* 2289 * Update the read time so we don't revalidate too often. 2290 */ 2291 nfsi->read_cache_jiffies = fattr->time_start; 2292 2293 /* Fix up any delegated attributes in the struct nfs_fattr */ 2294 nfs_fattr_fixup_delegated(inode, fattr); 2295 2296 save_cache_validity = nfsi->cache_validity; 2297 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR 2298 | NFS_INO_INVALID_ATIME 2299 | NFS_INO_REVAL_FORCED 2300 | NFS_INO_INVALID_BLOCKS); 2301 2302 /* Do atomic weak cache consistency updates */ 2303 nfs_wcc_update_inode(inode, fattr); 2304 2305 if (pnfs_layoutcommit_outstanding(inode)) { 2306 nfsi->cache_validity |= 2307 save_cache_validity & 2308 (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 2309 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | 2310 NFS_INO_INVALID_BLOCKS); 2311 cache_revalidated = false; 2312 } 2313 2314 /* More cache consistency checks */ 2315 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) { 2316 if (!have_writers && nfsi->ooo && nfsi->ooo->cnt == 1 && 2317 nfsi->ooo->gap[0].end == inode_peek_iversion_raw(inode)) { 2318 /* There is one remaining gap that hasn't been 2319 * merged into iversion - do that now. 2320 */ 2321 inode_set_iversion_raw(inode, nfsi->ooo->gap[0].start); 2322 kfree(nfsi->ooo); 2323 nfsi->ooo = NULL; 2324 } 2325 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) { 2326 /* Could it be a race with writeback? */ 2327 if (!(have_writers || have_delegation)) { 2328 invalid |= NFS_INO_INVALID_DATA 2329 | NFS_INO_INVALID_ACCESS 2330 | NFS_INO_INVALID_ACL 2331 | NFS_INO_INVALID_XATTR; 2332 /* Force revalidate of all attributes */ 2333 save_cache_validity |= NFS_INO_INVALID_CTIME 2334 | NFS_INO_INVALID_MTIME 2335 | NFS_INO_INVALID_SIZE 2336 | NFS_INO_INVALID_BLOCKS 2337 | NFS_INO_INVALID_NLINK 2338 | NFS_INO_INVALID_MODE 2339 | NFS_INO_INVALID_OTHER 2340 | NFS_INO_INVALID_BTIME; 2341 if (S_ISDIR(inode->i_mode)) 2342 nfs_force_lookup_revalidate(inode); 2343 attr_changed = true; 2344 dprintk("NFS: change_attr change on server for file %s/%llu\n", 2345 inode->i_sb->s_id, 2346 inode->i_ino); 2347 } else if (!have_delegation) { 2348 nfs_ooo_record(nfsi, fattr); 2349 nfs_ooo_merge(nfsi, inode_peek_iversion_raw(inode), 2350 fattr->change_attr); 2351 } 2352 inode_set_iversion_raw(inode, fattr->change_attr); 2353 } 2354 } else { 2355 nfsi->cache_validity |= 2356 save_cache_validity & NFS_INO_INVALID_CHANGE; 2357 if (!have_delegation || 2358 (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0) 2359 cache_revalidated = false; 2360 } 2361 2362 if (fattr->valid & NFS_ATTR_FATTR_MTIME) 2363 inode_set_mtime_to_ts(inode, fattr->mtime); 2364 else if (fattr_supported & NFS_ATTR_FATTR_MTIME) 2365 nfsi->cache_validity |= 2366 save_cache_validity & NFS_INO_INVALID_MTIME; 2367 2368 if (fattr->valid & NFS_ATTR_FATTR_CTIME) 2369 inode_set_ctime_to_ts(inode, fattr->ctime); 2370 else if (fattr_supported & NFS_ATTR_FATTR_CTIME) 2371 nfsi->cache_validity |= 2372 save_cache_validity & NFS_INO_INVALID_CTIME; 2373 2374 if (fattr->valid & NFS_ATTR_FATTR_BTIME) 2375 nfsi->btime = fattr->btime; 2376 else if (fattr_supported & NFS_ATTR_FATTR_BTIME) 2377 nfsi->cache_validity |= 2378 save_cache_validity & NFS_INO_INVALID_BTIME; 2379 2380 /* Check if our cached file size is stale */ 2381 if (fattr->valid & NFS_ATTR_FATTR_SIZE) { 2382 new_isize = nfs_size_to_loff_t(fattr->size); 2383 cur_isize = i_size_read(inode); 2384 if (new_isize != cur_isize && !have_delegation) { 2385 /* Do we perhaps have any outstanding writes, or has 2386 * the file grown beyond our last write? */ 2387 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) { 2388 trace_nfs_size_update(inode, new_isize); 2389 i_size_write(inode, new_isize); 2390 if (!have_writers) 2391 invalid |= NFS_INO_INVALID_DATA; 2392 } 2393 } 2394 if (new_isize == 0 && 2395 !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED | 2396 NFS_ATTR_FATTR_BLOCKS_USED))) { 2397 fattr->du.nfs3.used = 0; 2398 fattr->valid |= NFS_ATTR_FATTR_SPACE_USED; 2399 } 2400 } else 2401 nfsi->cache_validity |= 2402 save_cache_validity & NFS_INO_INVALID_SIZE; 2403 2404 if (fattr->valid & NFS_ATTR_FATTR_ATIME) 2405 inode_set_atime_to_ts(inode, fattr->atime); 2406 else if (fattr_supported & NFS_ATTR_FATTR_ATIME) 2407 nfsi->cache_validity |= 2408 save_cache_validity & NFS_INO_INVALID_ATIME; 2409 2410 if (fattr->valid & NFS_ATTR_FATTR_MODE) { 2411 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) { 2412 umode_t newmode = inode->i_mode & S_IFMT; 2413 newmode |= fattr->mode & S_IALLUGO; 2414 inode->i_mode = newmode; 2415 invalid |= NFS_INO_INVALID_ACCESS 2416 | NFS_INO_INVALID_ACL; 2417 } 2418 } else if (fattr_supported & NFS_ATTR_FATTR_MODE) 2419 nfsi->cache_validity |= 2420 save_cache_validity & NFS_INO_INVALID_MODE; 2421 2422 if (fattr->valid & NFS_ATTR_FATTR_OWNER) { 2423 if (!uid_eq(inode->i_uid, fattr->uid)) { 2424 invalid |= NFS_INO_INVALID_ACCESS 2425 | NFS_INO_INVALID_ACL; 2426 inode->i_uid = fattr->uid; 2427 } 2428 } else if (fattr_supported & NFS_ATTR_FATTR_OWNER) 2429 nfsi->cache_validity |= 2430 save_cache_validity & NFS_INO_INVALID_OTHER; 2431 2432 if (fattr->valid & NFS_ATTR_FATTR_GROUP) { 2433 if (!gid_eq(inode->i_gid, fattr->gid)) { 2434 invalid |= NFS_INO_INVALID_ACCESS 2435 | NFS_INO_INVALID_ACL; 2436 inode->i_gid = fattr->gid; 2437 } 2438 } else if (fattr_supported & NFS_ATTR_FATTR_GROUP) 2439 nfsi->cache_validity |= 2440 save_cache_validity & NFS_INO_INVALID_OTHER; 2441 2442 if (fattr->valid & NFS_ATTR_FATTR_NLINK) { 2443 if (inode->i_nlink != fattr->nlink) 2444 set_nlink(inode, fattr->nlink); 2445 } else if (fattr_supported & NFS_ATTR_FATTR_NLINK) 2446 nfsi->cache_validity |= 2447 save_cache_validity & NFS_INO_INVALID_NLINK; 2448 2449 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { 2450 /* 2451 * report the blocks in 512byte units 2452 */ 2453 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); 2454 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED) 2455 nfsi->cache_validity |= 2456 save_cache_validity & NFS_INO_INVALID_BLOCKS; 2457 2458 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) 2459 inode->i_blocks = fattr->du.nfs2.blocks; 2460 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED) 2461 nfsi->cache_validity |= 2462 save_cache_validity & NFS_INO_INVALID_BLOCKS; 2463 2464 /* Update attrtimeo value if we're out of the unstable period */ 2465 if (attr_changed) { 2466 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); 2467 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); 2468 nfsi->attrtimeo_timestamp = now; 2469 /* Set barrier to be more recent than all outstanding updates */ 2470 nfsi->attr_gencount = nfs_inc_attr_generation_counter(); 2471 } else { 2472 if (cache_revalidated) { 2473 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, 2474 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) { 2475 nfsi->attrtimeo <<= 1; 2476 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode)) 2477 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode); 2478 } 2479 nfsi->attrtimeo_timestamp = now; 2480 } 2481 /* Set the barrier to be more recent than this fattr */ 2482 if ((long)(fattr->gencount - nfsi->attr_gencount) > 0) 2483 nfsi->attr_gencount = fattr->gencount; 2484 } 2485 2486 /* Don't invalidate the data if we were to blame */ 2487 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) 2488 || S_ISLNK(inode->i_mode))) 2489 invalid &= ~NFS_INO_INVALID_DATA; 2490 nfs_set_cache_invalid(inode, invalid); 2491 2492 return 0; 2493 out_err: 2494 /* 2495 * No need to worry about unhashing the dentry, as the 2496 * lookup validation will know that the inode is bad. 2497 * (But we fall through to invalidate the caches.) 2498 */ 2499 nfs_set_inode_stale_locked(inode); 2500 return -ESTALE; 2501 } 2502 2503 struct inode *nfs_alloc_inode(struct super_block *sb) 2504 { 2505 struct nfs_inode *nfsi; 2506 nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL); 2507 if (!nfsi) 2508 return NULL; 2509 nfsi->flags = 0UL; 2510 nfsi->cache_validity = 0UL; 2511 nfsi->ooo = NULL; 2512 #if IS_ENABLED(CONFIG_NFS_V4) 2513 nfsi->nfs4_acl = NULL; 2514 #endif /* CONFIG_NFS_V4 */ 2515 #ifdef CONFIG_NFS_V4_2 2516 nfsi->xattr_cache = NULL; 2517 #endif 2518 nfs_netfs_inode_init(nfsi); 2519 2520 return &nfsi->vfs_inode; 2521 } 2522 EXPORT_SYMBOL_GPL(nfs_alloc_inode); 2523 2524 void nfs_free_inode(struct inode *inode) 2525 { 2526 kfree(NFS_I(inode)->ooo); 2527 kmem_cache_free(nfs_inode_cachep, NFS_I(inode)); 2528 } 2529 EXPORT_SYMBOL_GPL(nfs_free_inode); 2530 2531 static inline void nfs4_init_once(struct nfs_inode *nfsi) 2532 { 2533 #if IS_ENABLED(CONFIG_NFS_V4) 2534 INIT_LIST_HEAD(&nfsi->open_states); 2535 nfsi->delegation = NULL; 2536 init_rwsem(&nfsi->rwsem); 2537 nfsi->layout = NULL; 2538 #endif 2539 } 2540 2541 static void init_once(void *foo) 2542 { 2543 struct nfs_inode *nfsi = foo; 2544 2545 inode_init_once(&nfsi->vfs_inode); 2546 INIT_LIST_HEAD(&nfsi->open_files); 2547 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru); 2548 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru); 2549 nfs4_init_once(nfsi); 2550 } 2551 2552 static int __init nfs_init_inodecache(void) 2553 { 2554 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache", 2555 sizeof(struct nfs_inode), 2556 0, (SLAB_RECLAIM_ACCOUNT| 2557 SLAB_ACCOUNT), 2558 init_once); 2559 if (nfs_inode_cachep == NULL) 2560 return -ENOMEM; 2561 2562 return 0; 2563 } 2564 2565 static void nfs_destroy_inodecache(void) 2566 { 2567 /* 2568 * Make sure all delayed rcu free inodes are flushed before we 2569 * destroy cache. 2570 */ 2571 rcu_barrier(); 2572 kmem_cache_destroy(nfs_inode_cachep); 2573 } 2574 2575 struct workqueue_struct *nfslocaliod_workqueue; 2576 struct workqueue_struct *nfsiod_workqueue; 2577 EXPORT_SYMBOL_GPL(nfsiod_workqueue); 2578 2579 /* 2580 * Destroy the nfsiod workqueues 2581 */ 2582 static void nfsiod_stop(void) 2583 { 2584 struct workqueue_struct *wq; 2585 2586 wq = nfsiod_workqueue; 2587 if (wq != NULL) { 2588 nfsiod_workqueue = NULL; 2589 destroy_workqueue(wq); 2590 } 2591 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 2592 wq = nfslocaliod_workqueue; 2593 if (wq != NULL) { 2594 nfslocaliod_workqueue = NULL; 2595 destroy_workqueue(wq); 2596 } 2597 #endif /* CONFIG_NFS_LOCALIO */ 2598 } 2599 2600 /* 2601 * Start the nfsiod workqueues 2602 */ 2603 static int nfsiod_start(void) 2604 { 2605 dprintk("RPC: creating workqueue nfsiod\n"); 2606 nfsiod_workqueue = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 2607 if (nfsiod_workqueue == NULL) 2608 return -ENOMEM; 2609 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 2610 /* 2611 * localio writes need to use a normal (non-memreclaim) workqueue. 2612 * When we start getting low on space, XFS goes and calls flush_work() on 2613 * a non-memreclaim work queue, which causes a priority inversion problem. 2614 */ 2615 dprintk("RPC: creating workqueue nfslocaliod\n"); 2616 nfslocaliod_workqueue = alloc_workqueue("nfslocaliod", WQ_UNBOUND, 0); 2617 if (unlikely(nfslocaliod_workqueue == NULL)) { 2618 nfsiod_stop(); 2619 return -ENOMEM; 2620 } 2621 #endif /* CONFIG_NFS_LOCALIO */ 2622 return 0; 2623 } 2624 2625 unsigned int nfs_net_id; 2626 EXPORT_SYMBOL_GPL(nfs_net_id); 2627 2628 static int nfs_net_init(struct net *net) 2629 { 2630 struct nfs_net *nn = net_generic(net, nfs_net_id); 2631 int err; 2632 2633 nfs_clients_init(net); 2634 2635 if (!rpc_proc_register(net, &nn->rpcstats)) { 2636 err = -ENOMEM; 2637 goto err_proc_rpc; 2638 } 2639 2640 err = nfs_fs_proc_net_init(net); 2641 if (err) 2642 goto err_proc_nfs; 2643 2644 return 0; 2645 2646 err_proc_nfs: 2647 rpc_proc_unregister(net, "nfs"); 2648 err_proc_rpc: 2649 nfs_clients_exit(net); 2650 return err; 2651 } 2652 2653 static void nfs_net_exit(struct net *net) 2654 { 2655 rpc_proc_unregister(net, "nfs"); 2656 nfs_fs_proc_net_exit(net); 2657 nfs_clients_exit(net); 2658 } 2659 2660 static struct pernet_operations nfs_net_ops = { 2661 .init = nfs_net_init, 2662 .exit = nfs_net_exit, 2663 .id = &nfs_net_id, 2664 .size = sizeof(struct nfs_net), 2665 }; 2666 2667 #ifdef CONFIG_KEYS 2668 static struct key *nfs_keyring; 2669 2670 static int __init nfs_init_keyring(void) 2671 { 2672 nfs_keyring = keyring_alloc(".nfs", 2673 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 2674 current_cred(), 2675 (KEY_POS_ALL & ~KEY_POS_SETATTR) | 2676 (KEY_USR_ALL & ~KEY_USR_SETATTR), 2677 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); 2678 return PTR_ERR_OR_ZERO(nfs_keyring); 2679 } 2680 2681 static void nfs_exit_keyring(void) 2682 { 2683 key_put(nfs_keyring); 2684 } 2685 #else 2686 static inline int nfs_init_keyring(void) 2687 { 2688 return 0; 2689 } 2690 2691 static inline void nfs_exit_keyring(void) 2692 { 2693 } 2694 #endif /* CONFIG_KEYS */ 2695 2696 /* 2697 * Initialize NFS 2698 */ 2699 static int __init init_nfs_fs(void) 2700 { 2701 int err; 2702 2703 err = nfs_init_keyring(); 2704 if (err) 2705 return err; 2706 2707 err = nfs_sysfs_init(); 2708 if (err < 0) 2709 goto out10; 2710 2711 err = register_pernet_subsys(&nfs_net_ops); 2712 if (err < 0) 2713 goto out9; 2714 2715 err = nfsiod_start(); 2716 if (err) 2717 goto out7; 2718 2719 err = nfs_fs_proc_init(); 2720 if (err) 2721 goto out6; 2722 2723 err = nfs_init_nfspagecache(); 2724 if (err) 2725 goto out5; 2726 2727 err = nfs_init_inodecache(); 2728 if (err) 2729 goto out4; 2730 2731 err = nfs_init_readpagecache(); 2732 if (err) 2733 goto out3; 2734 2735 err = nfs_init_writepagecache(); 2736 if (err) 2737 goto out2; 2738 2739 err = nfs_init_directcache(); 2740 if (err) 2741 goto out1; 2742 2743 err = register_nfs_fs(); 2744 if (err) 2745 goto out0; 2746 2747 return 0; 2748 out0: 2749 nfs_destroy_directcache(); 2750 out1: 2751 nfs_destroy_writepagecache(); 2752 out2: 2753 nfs_destroy_readpagecache(); 2754 out3: 2755 nfs_destroy_inodecache(); 2756 out4: 2757 nfs_destroy_nfspagecache(); 2758 out5: 2759 nfs_fs_proc_exit(); 2760 out6: 2761 nfsiod_stop(); 2762 out7: 2763 unregister_pernet_subsys(&nfs_net_ops); 2764 out9: 2765 nfs_sysfs_exit(); 2766 out10: 2767 nfs_exit_keyring(); 2768 return err; 2769 } 2770 2771 static void __exit exit_nfs_fs(void) 2772 { 2773 nfs_destroy_directcache(); 2774 nfs_destroy_writepagecache(); 2775 nfs_destroy_readpagecache(); 2776 nfs_destroy_inodecache(); 2777 nfs_destroy_nfspagecache(); 2778 unregister_pernet_subsys(&nfs_net_ops); 2779 unregister_nfs_fs(); 2780 nfs_fs_proc_exit(); 2781 nfsiod_stop(); 2782 nfs_sysfs_exit(); 2783 nfs_exit_keyring(); 2784 } 2785 2786 /* Not quite true; I just maintain it */ 2787 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 2788 MODULE_DESCRIPTION("NFS client support"); 2789 MODULE_LICENSE("GPL"); 2790 module_param_cb(enable_ino64, ¶m_ops_enable_ino64, &enable_ino64, 0644); 2791 2792 module_init(init_nfs_fs) 2793 module_exit(exit_nfs_fs) 2794