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