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