blob: 542c7d97b2353dbb2746a2e035950e65ac1898d5 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/inode.c
4 *
5 * Copyright (C) 1992 Rick Sladkey
6 *
7 * nfs inode and superblock handling functions
8 *
Alan Cox526719b2008-10-27 15:19:48 +00009 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/init.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Chuck Lever4ece3a22006-03-20 13:44:22 -050029#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/nfs_fs.h>
31#include <linux/nfs_mount.h>
32#include <linux/nfs4_mount.h>
33#include <linux/lockd/bind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/seq_file.h>
35#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/vfs.h>
Manoj Naik9cdb3882006-06-09 09:34:28 -040037#include <linux/inet.h>
38#include <linux/nfs_xdr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Frank Filz3fa0b4e22010-12-02 19:31:23 +000040#include <linux/compat.h>
Jeff Laytond3103102011-12-01 22:44:39 +010041#include <linux/freezer.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080042#include <linux/uaccess.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050043#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Trond Myklebust4ce79712005-06-22 17:16:21 +000045#include "nfs4_fs.h"
Trond Myklebusta72b4422006-01-03 09:55:41 +010046#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "delegation.h"
Chuck Leverd9ef5a82006-03-20 13:44:13 -050048#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040049#include "internal.h"
David Howells8ec442a2009-04-03 16:42:42 +010050#include "fscache.h"
Benny Halevye5e94012010-10-20 00:18:01 -040051#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040052#include "nfs.h"
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +030053#include "netns.h"
Trond Myklebust996bc4f2019-01-24 16:10:46 -050054#include "sysfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Trond Myklebustf4ce1292013-08-19 18:59:33 -040056#include "nfstrace.h"
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040060#define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
61
62/* Default is to see 64-bit inode numbers */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103063static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040064
Trond Myklebust24aa1fe2005-12-03 15:20:07 -050065static int nfs_update_inode(struct inode *, struct nfs_fattr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christoph Lametere18b8902006-12-06 20:33:20 -080067static struct kmem_cache * nfs_inode_cachep;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static inline unsigned long
70nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
71{
72 return nfs_fileid_to_ino_t(fattr->fileid);
73}
74
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020075int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
Trond Myklebust72cb77f2009-03-11 14:10:30 -040076{
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020077 schedule();
Peter Zijlstradfd01f02015-12-13 22:11:16 +010078 if (signal_pending_state(mode, current))
79 return -ERESTARTSYS;
Trond Myklebust72cb77f2009-03-11 14:10:30 -040080 return 0;
81}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040082EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
Trond Myklebust72cb77f2009-03-11 14:10:30 -040083
84/**
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040085 * 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 */
91u64 nfs_compat_user_ino64(u64 fileid)
92{
Frank Filz3fa0b4e22010-12-02 19:31:23 +000093#ifdef CONFIG_COMPAT
94 compat_ulong_t ino;
95#else
96 unsigned long ino;
97#endif
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040098
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
Trond Myklebusteed99352012-12-14 14:36:36 -0500107int nfs_drop_inode(struct inode *inode)
108{
109 return NFS_STALE(inode) || generic_drop_inode(inode);
110}
111EXPORT_SYMBOL_GPL(nfs_drop_inode);
112
Bryan Schumaker19d87ca2012-07-30 16:05:21 -0400113void nfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Trond Myklebustda6d5032006-06-01 17:26:35 -0400115 /*
116 * The following should never happen...
117 */
Trond Myklebustf48407d2012-10-15 16:19:30 -0400118 WARN_ON_ONCE(nfs_have_writebacks(inode));
119 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
Trond Myklebustada70d92005-06-22 17:16:22 +0000120 nfs_zap_acl_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400121 nfs_access_zap_cache(inode);
David Howellsf1fe29b2013-09-27 11:20:03 +0100122 nfs_fscache_clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400124EXPORT_SYMBOL_GPL(nfs_clear_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Al Virob57922d2010-06-07 14:34:48 -0400126void nfs_evict_inode(struct inode *inode)
127{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700128 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200129 clear_inode(inode);
Al Virob57922d2010-06-07 14:34:48 -0400130 nfs_clear_inode(inode);
131}
132
Trond Myklebust9e1681c2015-03-25 17:23:31 -0400133int nfs_sync_inode(struct inode *inode)
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400134{
Christoph Hellwig95d9f6c2016-03-02 17:35:55 +0100135 inode_dio_wait(inode);
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400136 return nfs_wb_all(inode);
137}
Trond Myklebust9e1681c2015-03-25 17:23:31 -0400138EXPORT_SYMBOL_GPL(nfs_sync_inode);
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400139
Trond Myklebust29884df2005-12-13 16:13:54 -0500140/**
141 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
Trond Myklebust302fad72019-02-18 13:32:38 -0500142 * @mapping: pointer to struct address_space
Trond Myklebust29884df2005-12-13 16:13:54 -0500143 */
144int nfs_sync_mapping(struct address_space *mapping)
145{
Trond Myklebust5cf95212010-02-19 17:03:29 -0800146 int ret = 0;
Trond Myklebust29884df2005-12-13 16:13:54 -0500147
Trond Myklebust5cf95212010-02-19 17:03:29 -0800148 if (mapping->nrpages != 0) {
149 unmap_mapping_range(mapping, 0, 0, 0);
150 ret = nfs_wb_all(mapping->host);
151 }
Trond Myklebust29884df2005-12-13 16:13:54 -0500152 return ret;
153}
154
Trond Myklebust187e5932016-12-16 18:51:15 -0500155static 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
Trond Myklebust13c0b082021-03-25 21:07:21 -0400162static bool nfs_check_cache_flags_invalid(struct inode *inode,
163 unsigned long flags)
Trond Myklebust61540bf2016-12-08 18:18:38 -0500164{
165 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
166
Trond Myklebust61540bf2016-12-08 18:18:38 -0500167 return (cache_validity & flags) != 0;
168}
169
Trond Myklebust61540bf2016-12-08 18:18:38 -0500170bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
171{
Trond Myklebust13c0b082021-03-25 21:07:21 -0400172 if (nfs_check_cache_flags_invalid(inode, flags))
173 return true;
174 return nfs_attribute_cache_expired(inode);
Trond Myklebust61540bf2016-12-08 18:18:38 -0500175}
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000176EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
Trond Myklebust61540bf2016-12-08 18:18:38 -0500177
Trond Myklebust848fdd62021-02-08 16:45:49 -0500178#ifdef CONFIG_NFS_V4_2
179static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
180{
181 return nfsi->xattr_cache != NULL;
182}
183#else
184static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi)
185{
186 return false;
187}
188#endif
189
Trond Myklebustfd6d3fe2021-03-08 14:42:53 -0500190void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
Trond Myklebust6edf9602014-06-20 13:11:01 -0400191{
192 struct nfs_inode *nfsi = NFS_I(inode);
193
Trond Myklebust4201916f2024-06-16 21:21:24 -0400194 if (nfs_have_delegated_attributes(inode)) {
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400195 if (!(flags & NFS_INO_REVAL_FORCED))
Trond Myklebust720869e2021-04-13 09:41:16 -0400196 flags &= ~(NFS_INO_INVALID_MODE |
Trond Myklebustcc7f2da2021-04-11 14:31:24 -0400197 NFS_INO_INVALID_OTHER |
198 NFS_INO_INVALID_XATTR);
199 flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
Trond Myklebust41e97b72022-02-09 13:26:19 -0500200 }
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400201
Trond Myklebust848fdd62021-02-08 16:45:49 -0500202 if (!nfs_has_xattr_cache(nfsi))
203 flags &= ~NFS_INO_INVALID_XATTR;
Trond Myklebust6edf9602014-06-20 13:11:01 -0400204 if (flags & NFS_INO_INVALID_DATA)
Dave Wysochanskia6b5a282020-11-14 13:43:54 -0500205 nfs_fscache_invalidate(inode, 0);
Trond Myklebust41e97b72022-02-09 13:26:19 -0500206 flags &= ~NFS_INO_REVAL_FORCED;
Trond Myklebust488796e2021-09-28 11:15:53 -0400207
Trond Myklebust6edf9602014-06-20 13:11:01 -0400208 nfsi->cache_validity |= flags;
Trond Myklebust488796e2021-09-28 11:15:53 -0400209
NeilBrown3db63da2023-03-22 09:27:04 +1100210 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 }
Benjamin Coddington93c2e5e2021-11-16 08:49:24 -0500216 trace_nfs_set_cache_invalid(inode, 0);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400217}
Trond Myklebustb6f80a22021-03-08 14:42:55 -0500218EXPORT_SYMBOL_GPL(nfs_set_cache_invalid);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * Invalidate the local caches
222 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500223static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct nfs_inode *nfsi = NFS_I(inode);
226 int mode = inode->i_mode;
227
Chuck Lever91d5b472006-03-20 13:44:14 -0500228 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
229
Trond Myklebustc7c20972007-09-28 19:22:40 -0400230 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
231 nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Trond Myklebust88a60992022-02-09 13:22:48 -0500233 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);
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500244 nfs_zap_label_cache_locked(nfsi);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500245}
Chuck Leverdc592502005-08-18 11:24:12 -0700246
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500247void nfs_zap_caches(struct inode *inode)
248{
249 spin_lock(&inode->i_lock);
250 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700251 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000252}
253
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700254void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
255{
256 if (mapping->nrpages != 0) {
257 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400258 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700259 spin_unlock(&inode->i_lock);
260 }
261}
262
Trond Myklebustf41f7412008-06-11 17:39:04 -0400263void nfs_zap_acl_cache(struct inode *inode)
Trond Myklebustada70d92005-06-22 17:16:22 +0000264{
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);
Chuck Leverdc592502005-08-18 11:24:12 -0700270 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700271 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700272 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -0400274EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Trond Myklebustc4812992007-09-28 17:11:45 -0400276void nfs_invalidate_atime(struct inode *inode)
277{
Trond Myklebuste12912d2024-06-16 21:21:25 -0400278 if (nfs_have_delegated_atime(inode))
279 return;
Trond Myklebustc4812992007-09-28 17:11:45 -0400280 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400281 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebustc4812992007-09-28 17:11:45 -0400282 spin_unlock(&inode->i_lock);
283}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400284EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
Trond Myklebustc4812992007-09-28 17:11:45 -0400285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500287 * Invalidate, but do not unhash, the inode.
288 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400290static void nfs_set_inode_stale_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Benny Halevy3a10c302008-01-23 08:58:59 +0200292 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500293 nfs_zap_caches_locked(inode);
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400294 trace_nfs_set_inode_stale(inode);
295}
296
297void 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304struct 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 */
315static int
316nfs_find_actor(struct inode *inode, void *opaque)
317{
yuzhe7e7ce2c2022-06-15 13:39:24 +0800318 struct nfs_find_desc *desc = opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct nfs_fh *fh = desc->fh;
320 struct nfs_fattr *fattr = desc->fattr;
321
322 if (NFS_FILEID(inode) != fattr->fileid)
323 return 0;
Al Viro6e3e2c42021-03-01 20:37:10 -0500324 if (inode_wrong_type(inode, fattr->mode))
Jeff Laytonf6488c92013-02-27 20:10:34 -0500325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 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
333static int
334nfs_init_locked(struct inode *inode, void *opaque)
335{
yuzhe7e7ce2c2022-06-15 13:39:24 +0800336 struct nfs_find_desc *desc = opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct nfs_fattr *fattr = desc->fattr;
338
Benny Halevy99fadcd2008-01-23 08:59:08 +0200339 set_nfs_fileid(inode, fattr->fileid);
Trond Myklebust916ec342016-06-17 16:48:28 -0400340 inode->i_mode = fattr->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 nfs_copy_fh(NFS_FH(inode), desc->fh);
342 return 0;
343}
344
Steve Dicksone058f702013-05-22 12:50:40 -0400345#ifdef CONFIG_NFS_V4_SECURITY_LABEL
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500346static 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
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400353void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
David Quigleyaa9c2662013-05-22 12:50:44 -0400354{
355 int error;
356
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400357 if (fattr->label == NULL)
David Quigleyaa9c2662013-05-22 12:50:44 -0400358 return;
359
David Quigleyaa9c2662013-05-22 12:50:44 -0400360 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400361 error = security_inode_notifysecctx(inode, fattr->label->label,
362 fattr->label->len);
David Quigleyaa9c2662013-05-22 12:50:44 -0400363 if (error)
364 printk(KERN_ERR "%s() %s %d "
365 "security_inode_notifysecctx() %d\n",
366 __func__,
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400367 (char *)fattr->label->label,
368 fattr->label->len, error);
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500369 nfs_clear_label_invalid(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -0400370 }
371}
372
Steve Dicksone058f702013-05-22 12:50:40 -0400373struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
374{
Trond Myklebuste48c81b2021-11-05 13:40:11 -0400375 struct nfs4_label *label;
Steve Dicksone058f702013-05-22 12:50:40 -0400376
377 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
Trond Myklebuste48c81b2021-11-05 13:40:11 -0400378 return NULL;
Steve Dicksone058f702013-05-22 12:50:40 -0400379
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}
393EXPORT_SYMBOL_GPL(nfs4_label_alloc);
David Quigleyaa9c2662013-05-22 12:50:44 -0400394#else
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400395void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr)
David Quigleyaa9c2662013-05-22 12:50:44 -0400396{
397}
Steve Dicksone058f702013-05-22 12:50:40 -0400398#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400399EXPORT_SYMBOL_GPL(nfs_setsecurity);
Steve Dicksone058f702013-05-22 12:50:40 -0400400
Peng Taof174ff72017-06-29 06:34:51 -0700401/* Search for inode identified by fh, fileid and i_mode in inode cache. */
402struct inode *
403nfs_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
Trond Myklebuste591b292021-09-28 17:41:41 -0400423static void nfs_inode_init_regular(struct nfs_inode *nfsi)
424{
425 atomic_long_set(&nfsi->nrequests, 0);
Trond Myklebust67f4b5d2022-08-13 08:22:25 -0400426 atomic_long_set(&nfsi->redirtied_pages, 0);
Trond Myklebuste591b292021-09-28 17:41:41 -0400427 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
433static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * This is our front-end to iget that looks up inodes by file handle
442 * instead of inode number.
443 */
444struct inode *
Anna Schumakercf7ab002021-10-22 13:11:11 -0400445nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct nfs_find_desc desc = {
448 .fh = fh,
449 .fattr = fattr
450 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500451 struct inode *inode = ERR_PTR(-ENOENT);
Trond Myklebustce62b112018-03-05 15:01:18 -0500452 u64 fattr_supported = NFS_SB(sb)->fattr_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 unsigned long hash;
454
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000455 nfs_attr_check_mountpoint(sb, fattr);
456
Anna Schumaker2ef47eb2014-12-09 16:19:16 -0500457 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out_no_inode;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400461 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out_no_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 hash = nfs_fattr_to_ino_t(fattr);
465
Trond Myklebust03f28e32006-03-20 13:44:48 -0500466 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
467 if (inode == NULL) {
468 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (inode->i_state & I_NEW) {
473 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800474 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
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;
Trond Myklebust821a8682018-03-27 18:30:42 -0400483 nfsi->cache_validity = 0;
Trond Myklebust62ab460c2009-08-09 15:06:19 -0400484 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
Trond Myklebustce62b112018-03-05 15:01:18 -0500485 && (fattr_supported & NFS_ATTR_FATTR_MODE))
Trond Myklebust720869e2021-04-13 09:41:16 -0400486 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Why so? Because we want revalidate for devices/FIFOs, and
488 * that's precisely what we have in nfs_file_inode_operations.
489 */
David Howells8fa5c002006-08-22 20:06:12 -0400490 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (S_ISREG(inode->i_mode)) {
Jeff Layton1788ea62011-11-04 13:31:21 -0400492 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 inode->i_data.a_ops = &nfs_file_aops;
Trond Myklebuste591b292021-09-28 17:41:41 -0400494 nfs_inode_init_regular(nfsi);
Christoph Hellwig49b29a52024-05-27 18:36:09 +0200495 mapping_set_large_folios(inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else if (S_ISDIR(inode->i_mode)) {
David Howells8fa5c002006-08-22 20:06:12 -0400497 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 inode->i_fop = &nfs_dir_operations;
Trond Myklebust11de3b12010-12-01 14:17:06 -0500499 inode->i_data.a_ops = &nfs_dir_aops;
Trond Myklebuste591b292021-09-28 17:41:41 -0400500 nfs_inode_init_dir(nfsi);
Trond Myklebust55a97592006-06-09 09:34:19 -0400501 /* Deal with crossing mountpoints */
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000502 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
503 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400504 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;
Trond Myklebust55a97592006-06-09 09:34:19 -0400508 inode->i_fop = NULL;
David Howells36d43a42011-01-14 18:45:42 +0000509 inode->i_flags |= S_AUTOMOUNT;
Trond Myklebust55a97592006-06-09 09:34:19 -0400510 }
Al Viro21fc61c2015-11-17 01:07:57 -0500511 } else if (S_ISLNK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 inode->i_op = &nfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500513 inode_nohighmem(inode);
514 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 init_special_inode(inode, inode->i_mode, fattr->rdev);
516
Jeff Layton41d581a92023-10-04 14:52:36 -0400517 inode_set_atime(inode, 0, 0);
518 inode_set_mtime(inode, 0, 0);
Jeff Layton55e04e92023-07-05 15:01:22 -0400519 inode_set_ctime(inode, 0, 0);
Jeff Layton1eb5d982018-01-09 08:21:17 -0500520 inode_set_iversion_raw(inode, 0);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400521 inode->i_size = 0;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200522 clear_nlink(inode);
Eric W. Biederman9ff593c2013-02-01 14:26:23 -0800523 inode->i_uid = make_kuid(&init_user_ns, -2);
524 inode->i_gid = make_kgid(&init_user_ns, -2);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400525 inode->i_blocks = 0;
Andy Adamson2701d082012-05-24 13:13:24 -0400526 nfsi->write_io = 0;
527 nfsi->read_io = 0;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400528
Trond Myklebust33801142005-10-27 22:12:39 -0400529 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400530 nfsi->attr_gencount = fattr->gencount;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400531 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Jeff Layton41d581a92023-10-04 14:52:36 -0400532 inode_set_atime_to_ts(inode, fattr->atime);
Trond Myklebustce62b112018-03-05 15:01:18 -0500533 else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
Trond Myklebust16e14372018-03-20 16:53:31 -0400534 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400535 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Jeff Layton41d581a92023-10-04 14:52:36 -0400536 inode_set_mtime_to_ts(inode, fattr->mtime);
Trond Myklebustce62b112018-03-05 15:01:18 -0500537 else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
Trond Myklebust16e14372018-03-20 16:53:31 -0400538 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400539 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Jeff Layton55e04e92023-07-05 15:01:22 -0400540 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebustce62b112018-03-05 15:01:18 -0500541 else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
Trond Myklebust16e14372018-03-20 16:53:31 -0400542 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400543 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Jeff Layton1eb5d982018-01-09 08:21:17 -0500544 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebustcd812592015-07-05 11:12:07 -0400545 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400546 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400547 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
548 inode->i_size = nfs_size_to_loff_t(fattr->size);
Trond Myklebust62ab460c2009-08-09 15:06:19 -0400549 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400550 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400551 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200552 set_nlink(inode, fattr->nlink);
Trond Myklebustce62b112018-03-05 15:01:18 -0500553 else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
Trond Myklebustfabf2b32021-03-25 13:14:42 -0400554 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400555 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
556 inode->i_uid = fattr->uid;
Trond Myklebustce62b112018-03-05 15:01:18 -0500557 else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
Trond Myklebust16e14372018-03-20 16:53:31 -0400558 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400559 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
560 inode->i_gid = fattr->gid;
Trond Myklebustce62b112018-03-05 15:01:18 -0500561 else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
Trond Myklebust16e14372018-03-20 16:53:31 -0400562 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400563 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
564 inode->i_blocks = fattr->du.nfs2.blocks;
Trond Myklebustce62b112018-03-05 15:01:18 -0500565 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED &&
566 fattr->size != 0)
567 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400568 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * report the blocks in 512byte units
571 */
572 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Trond Myklebustce62b112018-03-05 15:01:18 -0500573 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED &&
574 fattr->size != 0)
Trond Myklebust4cdfeb62021-03-24 12:29:41 -0400575 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
Trond Myklebust821a8682018-03-27 18:30:42 -0400576
Anna Schumakerdd225cb2021-10-22 13:11:12 -0400577 nfs_setsecurity(inode, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -0400578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800580 nfsi->attrtimeo_timestamp = now;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400581 nfsi->access_cache = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Howellsf1fe29b2013-09-27 11:20:03 +0100583 nfs_fscache_init_inode(inode);
David Howellsef79c092009-04-03 16:42:43 +0100584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unlock_new_inode(inode);
NeilBrown26fde4d2017-07-03 15:27:26 +1000586 } 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 }
Niels de Vos1e8968c2013-12-17 18:20:16 +0100594 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100596 (unsigned long long)NFS_FILEID(inode),
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -0500597 nfs_display_fhandle_hash(fh),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 atomic_read(&inode->i_count));
599
600out:
601 return inode;
602
603out_no_inode:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500604 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto out;
606}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400607EXPORT_SYMBOL_GPL(nfs_fhget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Trond Myklebust0a741f52024-06-16 21:21:30 -0400609static void
610nfs_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
Trond Myklebuste12912d2024-06-16 21:21:25 -0400631void 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
641void 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
650void 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}
656EXPORT_SYMBOL_GPL(nfs_update_delegated_mtime);
657
Trond Myklebust536e43d2012-01-17 22:04:26 -0500658#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660int
Christian Braunerc1632a02023-01-13 12:49:11 +0100661nfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
Christian Brauner549c7292021-01-21 14:19:43 +0100662 struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
David Howells2b0143b2015-03-17 22:25:59 +0000664 struct inode *inode = d_inode(dentry);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400665 struct nfs_fattr *fattr;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800666 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Chuck Lever91d5b472006-03-20 13:44:14 -0500668 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
669
Jeff Layton188b95d2007-10-18 03:05:21 -0700670 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig08a899d2014-09-07 08:36:40 -0700675 BUG_ON(!S_ISREG(inode->i_mode));
676
Kinglong Meeae57ca02015-08-26 21:10:55 +0800677 error = inode_newsize_ok(inode, attr->ia_size);
678 if (error)
679 return error;
680
681 if (attr->ia_size == i_size_read(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 attr->ia_valid &= ~ATTR_SIZE;
683 }
684
Trond Myklebuste12912d2024-06-16 21:21:25 -0400685 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Optimization: if the end result is no change, don't RPC */
Trond Myklebust1f9f4322021-04-12 20:08:00 -0400697 if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400700 trace_nfs_setattr_enter(inode);
701
Trond Myklebust755c1e22006-03-20 13:44:06 -0500702 /* Write all dirty data */
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400703 if (S_ISREG(inode->i_mode))
704 nfs_sync_inode(inode);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400705
Anna Schumaker1b00ad62021-10-22 13:11:08 -0400706 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
Kinglong Meeae57ca02015-08-26 21:10:55 +0800707 if (fattr == NULL) {
708 error = -ENOMEM;
Trond Myklebust987f8df2010-04-16 16:22:52 -0400709 goto out;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800710 }
711
Trond Myklebust987f8df2010-04-16 16:22:52 -0400712 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400713 if (error == 0)
David Quigleyaa9c2662013-05-22 12:50:44 -0400714 error = nfs_refresh_inode(inode, fattr);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400715 nfs_free_fattr(fattr);
716out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400717 trace_nfs_setattr_exit(inode, error);
Trond Myklebust65e43082005-08-16 11:49:44 -0400718 return error;
719}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400720EXPORT_SYMBOL_GPL(nfs_setattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400721
722/**
Trond Myklebusta3d01452008-06-11 12:21:19 -0400723 * 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.
Trond Myklebustf0446362015-02-26 16:09:04 -0500730 * Note: must be called with inode->i_lock held!
Trond Myklebusta3d01452008-06-11 12:21:19 -0400731 */
732static int nfs_vmtruncate(struct inode * inode, loff_t offset)
733{
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000734 int err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400735
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000736 err = inode_newsize_ok(inode, offset);
737 if (err)
738 goto out;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400739
Chuck Lever110cb2d2021-10-04 10:10:16 -0400740 trace_nfs_size_truncate(inode, offset);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000741 i_size_write(inode, offset);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400742 /* Optimisation */
NeilBrown3db63da2023-03-22 09:27:04 +1100743 if (offset == 0) {
744 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
745 nfs_ooo_clear(NFS_I(inode));
746 }
Trond Myklebustf6cdfa62018-03-27 17:10:42 -0400747 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400748
Trond Myklebustf0446362015-02-26 16:09:04 -0500749 spin_unlock(&inode->i_lock);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700750 truncate_pagecache(inode, offset);
Trond Myklebuste12912d2024-06-16 21:21:25 -0400751 nfs_update_delegated_mtime_locked(inode);
Trond Myklebustf0446362015-02-26 16:09:04 -0500752 spin_lock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000753out:
754 return err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400755}
756
757/**
Trond Myklebust65e43082005-08-16 11:49:44 -0400758 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
759 * @inode: pointer to struct inode
760 * @attr: pointer to struct iattr
Trond Myklebust16e14372018-03-20 16:53:31 -0400761 * @fattr: pointer to struct nfs_fattr
Trond Myklebust65e43082005-08-16 11:49:44 -0400762 *
763 * Note: we do this in the *proc.c in order to ensure that
764 * it works for things like exclusive creates too.
765 */
Trond Myklebustf0446362015-02-26 16:09:04 -0500766void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
767 struct nfs_fattr *fattr)
Trond Myklebust65e43082005-08-16 11:49:44 -0400768{
Trond Myklebustf0446362015-02-26 16:09:04 -0500769 /* 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;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400774 if ((attr->ia_valid & ATTR_SIZE) != 0) {
Trond Myklebuste12912d2024-06-16 21:21:25 -0400775 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);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400778 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
779 nfs_vmtruncate(inode, attr->ia_size);
780 }
Trond Myklebust65e43082005-08-16 11:49:44 -0400781 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
Trond Myklebust6a97d022018-04-08 17:51:11 -0400782 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
Trond Myklebust1f9f4322021-04-12 20:08:00 -0400783 if ((attr->ia_valid & ATTR_KILL_SUID) != 0 &&
784 inode->i_mode & S_ISUID)
785 inode->i_mode &= ~S_ISUID;
Christian Brauner4f704d92023-03-14 12:51:10 +0100786 if (setattr_should_drop_sgid(&nop_mnt_idmap, inode))
Trond Myklebust1f9f4322021-04-12 20:08:00 -0400787 inode->i_mode &= ~S_ISGID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400789 int mode = attr->ia_mode & S_IALLUGO;
790 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 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;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400797 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Jeff Layton55e04e92023-07-05 15:01:22 -0400798 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400799 else
800 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
801 | NFS_INO_INVALID_CTIME);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400802 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
803 | NFS_INO_INVALID_ACL);
Trond Myklebust65e43082005-08-16 11:49:44 -0400804 }
Trond Myklebust6a97d022018-04-08 17:51:11 -0400805 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)
Jeff Layton41d581a92023-10-04 14:52:36 -0400809 inode_set_atime_to_ts(inode, fattr->atime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400810 else if (attr->ia_valid & ATTR_ATIME_SET)
Jeff Layton41d581a92023-10-04 14:52:36 -0400811 inode_set_atime_to_ts(inode, attr->ia_atime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400812 else
813 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
814
815 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Jeff Layton55e04e92023-07-05 15:01:22 -0400816 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400817 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)
Jeff Layton41d581a92023-10-04 14:52:36 -0400825 inode_set_mtime_to_ts(inode, fattr->mtime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400826 else if (attr->ia_valid & ATTR_MTIME_SET)
Jeff Layton41d581a92023-10-04 14:52:36 -0400827 inode_set_mtime_to_ts(inode, attr->ia_mtime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400828 else
829 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
830
831 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Jeff Layton55e04e92023-07-05 15:01:22 -0400832 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebust6a97d022018-04-08 17:51:11 -0400833 else
834 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
835 | NFS_INO_INVALID_CTIME);
Trond Myklebust65e43082005-08-16 11:49:44 -0400836 }
Jeff Layton616c3192015-11-25 13:50:45 -0500837 if (fattr->valid)
838 nfs_update_inode(inode, fattr);
Trond Myklebustf0446362015-02-26 16:09:04 -0500839 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400841EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500843/*
844 * Don't request help from readdirplus if the file is being written to,
845 * or if attribute caching is turned off
846 */
847static 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
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500853static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
Trond Myklebust311324a2014-02-07 17:02:08 -0500854{
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500855 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 }
Trond Myklebust311324a2014-02-07 17:02:08 -0500860}
861
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500862static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
863{
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500864 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 }
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500869}
870
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400871static u32 nfs_get_valid_attrmask(struct inode *inode)
Trond Myklebust311324a2014-02-07 17:02:08 -0500872{
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400873 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;
Trond Myklebust13c0b082021-03-25 21:07:21 -0400878 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;
Trond Myklebustfabf2b32021-03-25 13:14:42 -0400884 if (!(cache_validity & NFS_INO_INVALID_NLINK))
885 reply_mask |= STATX_NLINK;
Trond Myklebust720869e2021-04-13 09:41:16 -0400886 if (!(cache_validity & NFS_INO_INVALID_MODE))
887 reply_mask |= STATX_MODE;
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400888 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400889 reply_mask |= STATX_UID | STATX_GID;
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400890 if (!(cache_validity & NFS_INO_INVALID_BLOCKS))
891 reply_mask |= STATX_BLOCKS;
Jeff Layton61a968b2022-08-05 13:47:00 -0400892 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
893 reply_mask |= STATX_CHANGE_COOKIE;
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400894 return reply_mask;
Trond Myklebust311324a2014-02-07 17:02:08 -0500895}
896
Christian Braunerb74d24f2023-01-13 12:49:12 +0100897int nfs_getattr(struct mnt_idmap *idmap, const struct path *path,
Christian Brauner549c7292021-01-21 14:19:43 +0100898 struct kstat *stat, u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
David Howellsa528d352017-01-31 16:46:22 +0000900 struct inode *inode = d_inode(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500901 struct nfs_server *server = NFS_SERVER(inode);
902 unsigned long cache_validity;
Jan Kara16caf5b2014-10-23 14:02:47 +0200903 int err = 0;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500904 bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
905 bool do_update = false;
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500906 bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400908 trace_nfs_getattr_enter(inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500909
Trond Myklebust4eb6a822021-03-25 15:59:53 -0400910 request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID |
911 STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME |
Jeff Laytoncded49b2023-06-12 09:34:04 -0400912 STATX_INO | STATX_SIZE | STATX_BLOCKS |
Jeff Layton61a968b2022-08-05 13:47:00 -0400913 STATX_CHANGE_COOKIE;
Trond Myklebust4eb6a822021-03-25 15:59:53 -0400914
Trond Myklebustac7cbb22020-06-04 16:20:06 -0400915 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500916 if (readdirplus_enabled)
917 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400918 goto out_no_revalidate;
Trond Myklebustac7cbb22020-06-04 16:20:06 -0400919 }
Trond Myklebust9ccee942018-01-04 17:46:09 -0500920
Jeff Layton61a968b2022-08-05 13:47:00 -0400921 /* Flush out writes to the server in order to update c/mtime/version. */
922 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_CHANGE_COOKIE)) &&
Trond Myklebuste12912d2024-06-16 21:21:25 -0400923 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 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800929
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
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800936 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800937 * no point in checking those.
938 */
David Howellsa528d352017-01-31 16:46:22 +0000939 if ((path->mnt->mnt_flags & MNT_NOATIME) ||
940 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Trond Myklebust9ccee942018-01-04 17:46:09 -0500941 request_mask &= ~STATX_ATIME;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800942
Trond Myklebust9ccee942018-01-04 17:46:09 -0500943 /* 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|
Jeff Layton61a968b2022-08-05 13:47:00 -0400946 STATX_SIZE|STATX_BLOCKS|
947 STATX_CHANGE_COOKIE)))
Trond Myklebust9ccee942018-01-04 17:46:09 -0500948 goto out_no_revalidate;
Trond Myklebust311324a2014-02-07 17:02:08 -0500949
Trond Myklebust9ccee942018-01-04 17:46:09 -0500950 /* 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);
Trond Myklebuste8764a62021-03-24 13:55:09 -0400953 do_update |= cache_validity & NFS_INO_INVALID_CHANGE;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500954 if (request_mask & STATX_ATIME)
955 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
Trond Myklebuste8764a62021-03-24 13:55:09 -0400956 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;
Trond Myklebustfabf2b32021-03-25 13:14:42 -0400962 if (request_mask & STATX_NLINK)
963 do_update |= cache_validity & NFS_INO_INVALID_NLINK;
Trond Myklebust720869e2021-04-13 09:41:16 -0400964 if (request_mask & STATX_MODE)
965 do_update |= cache_validity & NFS_INO_INVALID_MODE;
966 if (request_mask & (STATX_UID | STATX_GID))
Trond Myklebuste8764a62021-03-24 13:55:09 -0400967 do_update |= cache_validity & NFS_INO_INVALID_OTHER;
Zheng Bin3a39e772020-05-21 17:17:21 +0800968 if (request_mask & STATX_BLOCKS)
969 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400970
Trond Myklebust9ccee942018-01-04 17:46:09 -0500971 if (do_update) {
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500972 if (readdirplus_enabled)
Hou Tao59b86d82017-04-28 18:35:19 +0800973 nfs_readdirplus_parent_cache_miss(path->dentry);
Trond Myklebust311324a2014-02-07 17:02:08 -0500974 err = __nfs_revalidate_inode(server, inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500975 if (err)
976 goto out;
Trond Myklebustad1e109a2022-02-17 15:46:23 -0500977 } else if (readdirplus_enabled)
David Howellsa528d352017-01-31 16:46:22 +0000978 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500979out_no_revalidate:
980 /* Only return attributes that were revalidated. */
Trond Myklebust63cdd7e2021-03-24 14:29:12 -0400981 stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask;
982
Jeff Layton0d72b9282023-08-07 15:38:33 -0400983 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500984 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
Jeff Layton61a968b2022-08-05 13:47:00 -0400985 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;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500989 if (S_ISDIR(inode->i_mode))
990 stat->blksize = NFS_SERVER(inode)->dtsize;
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800991out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400992 trace_nfs_getattr_exit(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return err;
994}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400995EXPORT_SYMBOL_GPL(nfs_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400997static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
998{
Elena Reshetova2f62b5a2017-10-20 12:53:37 +0300999 refcount_set(&l_ctx->count, 1);
NeilBrownd51fdb82016-10-13 15:26:47 +11001000 l_ctx->lockowner = current->files;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001001 INIT_LIST_HEAD(&l_ctx->list);
Benjamin Coddington210c7c12016-01-06 10:40:18 -05001002 atomic_set(&l_ctx->io_count, 0);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001003}
1004
1005static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
1006{
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001007 struct nfs_lock_context *pos;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001008
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001009 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
NeilBrownd51fdb82016-10-13 15:26:47 +11001010 if (pos->lockowner != current->files)
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001011 continue;
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001012 if (refcount_inc_not_zero(&pos->count))
1013 return pos;
1014 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001015 return NULL;
1016}
1017
1018struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
1019{
1020 struct nfs_lock_context *res, *new = NULL;
David Howells2b0143b2015-03-17 22:25:59 +00001021 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001022
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001023 rcu_read_lock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001024 res = __nfs_find_lock_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001025 rcu_read_unlock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001026 if (res == NULL) {
Trond Myklebustd7867712022-01-29 13:04:10 -05001027 new = kmalloc(sizeof(*new), GFP_KERNEL_ACCOUNT);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001028 if (new == NULL)
Trond Myklebustb3c54de2012-08-13 17:15:50 -04001029 return ERR_PTR(-ENOMEM);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001030 nfs_init_lock_context(new);
1031 spin_lock(&inode->i_lock);
1032 res = __nfs_find_lock_context(ctx);
1033 if (res == NULL) {
Trond Myklebust15494512019-04-07 13:59:10 -04001034 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);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001042 }
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001043 spin_unlock(&inode->i_lock);
1044 kfree(new);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001045 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001046 return res;
1047}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04001048EXPORT_SYMBOL_GPL(nfs_get_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001049
1050void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
1051{
1052 struct nfs_open_context *ctx = l_ctx->open_context;
David Howells2b0143b2015-03-17 22:25:59 +00001053 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001054
Elena Reshetova2f62b5a2017-10-20 12:53:37 +03001055 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001056 return;
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001057 list_del_rcu(&l_ctx->list);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001058 spin_unlock(&inode->i_lock);
Trond Myklebust15494512019-04-07 13:59:10 -04001059 put_nfs_open_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -04001060 kfree_rcu(l_ctx, rcu_head);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001061}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04001062EXPORT_SYMBOL_GPL(nfs_put_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -04001063
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001064/**
1065 * nfs_close_context - Common close_context() routine NFSv2/v3
1066 * @ctx: pointer to context
1067 * @is_sync: is this a synchronous close
1068 *
Trond Myklebust5cf9d702015-09-04 15:07:37 -04001069 * 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.
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001072 */
1073void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
1074{
Trond Myklebust5cf9d702015-09-04 15:07:37 -04001075 struct nfs_inode *nfsi;
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001076 struct inode *inode;
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001077
1078 if (!(ctx->mode & FMODE_WRITE))
1079 return;
1080 if (!is_sync)
1081 return;
David Howells2b0143b2015-03-17 22:25:59 +00001082 inode = d_inode(ctx->dentry);
Trond Myklebust4201916f2024-06-16 21:21:24 -04001083 if (nfs_have_read_or_write_delegation(inode))
Trond Myklebust58ff4182016-12-16 17:39:58 -05001084 return;
Trond Myklebust5cf9d702015-09-04 15:07:37 -04001085 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))
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001091 return;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001092 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001093 return;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001094 nfs_revalidate_inode(inode,
1095 NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001096}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001097EXPORT_SYMBOL_GPL(nfs_close_context);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001098
NeilBrown532d4de2016-10-13 15:26:47 +11001099struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
1100 fmode_t f_mode,
1101 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct nfs_open_context *ctx;
1104
Trond Myklebustd7867712022-01-29 13:04:10 -05001105 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
Trond Myklebust1d179d62020-02-07 19:32:49 -05001106 if (!ctx)
Al Viro5ede7b12011-10-23 18:49:54 -04001107 return ERR_PTR(-ENOMEM);
Al Viro5ede7b12011-10-23 18:49:54 -04001108 nfs_sb_active(dentry->d_sb);
1109 ctx->dentry = dget(dentry);
Trond Myklebust1d179d62020-02-07 19:32:49 -05001110 if (filp)
1111 ctx->cred = get_cred(filp->f_cred);
1112 else
1113 ctx->cred = get_current_cred();
Trond Myklebustca05cba2021-07-10 18:07:14 -04001114 rcu_assign_pointer(ctx->ll_cred, NULL);
Al Viro5ede7b12011-10-23 18:49:54 -04001115 ctx->state = NULL;
1116 ctx->mode = f_mode;
1117 ctx->flags = 0;
1118 ctx->error = 0;
NeilBrown532d4de2016-10-13 15:26:47 +11001119 ctx->flock_owner = (fl_owner_t)filp;
Al Viro5ede7b12011-10-23 18:49:54 -04001120 nfs_init_lock_context(&ctx->lock_context);
1121 ctx->lock_context.open_context = ctx;
1122 INIT_LIST_HEAD(&ctx->list);
Andy Adamson82be4172012-05-23 05:02:35 -04001123 ctx->mdsthreshold = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return ctx;
1125}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001126EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1129{
Trond Myklebust0de43972018-09-02 15:57:01 -04001130 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1131 return ctx;
1132 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001134EXPORT_SYMBOL_GPL(get_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001136static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001137{
David Howells2b0143b2015-03-17 22:25:59 +00001138 struct inode *inode = d_inode(ctx->dentry);
Al Viro3d4ff432011-06-22 18:40:12 -04001139 struct super_block *sb = ctx->dentry->d_sb;
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001140
Trond Myklebust0de43972018-09-02 15:57:01 -04001141 if (!refcount_dec_and_test(&ctx->lock_context.count))
Trond Myklebust5e119342007-07-26 12:06:17 -04001142 return;
Trond Myklebust0de43972018-09-02 15:57:01 -04001143 if (!list_empty(&ctx->list)) {
1144 spin_lock(&inode->i_lock);
1145 list_del_rcu(&ctx->list);
1146 spin_unlock(&inode->i_lock);
1147 }
Trond Myklebust5c78f582010-09-27 15:51:20 -04001148 if (inode != NULL)
1149 NFS_PROTO(inode)->close_context(ctx, is_sync);
NeilBrowna52458b2018-12-03 11:30:31 +11001150 put_cred(ctx->cred);
Al Viro3d4ff432011-06-22 18:40:12 -04001151 dput(ctx->dentry);
Trond Myklebust322b2b92013-01-11 16:39:51 -05001152 nfs_sb_deactive(sb);
Trond Myklebustca05cba2021-07-10 18:07:14 -04001153 put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1));
Andy Adamson82be4172012-05-23 05:02:35 -04001154 kfree(ctx->mdsthreshold);
Trond Myklebust0de43972018-09-02 15:57:01 -04001155 kfree_rcu(ctx, rcu_head);
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001156}
1157
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001158void put_nfs_open_context(struct nfs_open_context *ctx)
1159{
1160 __put_nfs_open_context(ctx, 0);
1161}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001162EXPORT_SYMBOL_GPL(put_nfs_open_context);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001163
Trond Myklebust4eae5012015-09-04 15:17:53 -04001164static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1165{
1166 __put_nfs_open_context(ctx, 1);
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
1170 * Ensure that mmap has a recent RPC credential for use when writing out
1171 * shared pages
1172 */
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001173void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
David Howells2b0143b2015-03-17 22:25:59 +00001175 struct inode *inode = d_inode(ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct nfs_inode *nfsi = NFS_I(inode);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock(&inode->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -04001179 if (list_empty(&nfsi->open_files) &&
NeilBrown3db63da2023-03-22 09:27:04 +11001180 nfs_ooo_test(nfsi))
Trond Myklebustac46b3d2021-03-08 14:42:54 -05001181 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA |
1182 NFS_INO_REVAL_FORCED);
Trond Myklebust0de43972018-09-02 15:57:01 -04001183 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 spin_unlock(&inode->i_lock);
1185}
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001186EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1187
1188void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1189{
1190 filp->private_data = get_nfs_open_context(ctx);
Trond Myklebuste97bc662021-05-11 23:41:10 -04001191 set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001192 if (list_empty(&ctx->list))
1193 nfs_inode_attach_open_context(ctx);
1194}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001195EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Trond Myklebustd5308382005-11-04 15:33:38 -05001197/*
1198 * Given an inode, search for an open context with the desired characteristics
1199 */
NeilBrowna52458b2018-12-03 11:30:31 +11001200struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct nfs_inode *nfsi = NFS_I(inode);
1203 struct nfs_open_context *pos, *ctx = NULL;
1204
Trond Myklebust0de43972018-09-02 15:57:01 -04001205 rcu_read_lock();
1206 list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
Trond Myklebust65f51602020-01-26 17:31:15 -05001207 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
Trond Myklebustd5308382005-11-04 15:33:38 -05001208 continue;
Trond Myklebust1544fa02010-03-25 13:54:49 -04001209 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1210 continue;
Trond Myklebuste97bc662021-05-11 23:41:10 -04001211 if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags))
1212 continue;
Trond Myklebust1544fa02010-03-25 13:54:49 -04001213 ctx = get_nfs_open_context(pos);
Trond Myklebust0de43972018-09-02 15:57:01 -04001214 if (ctx)
1215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001217 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return ctx;
1219}
1220
Anna Schumakeraff8d8d2015-07-13 14:01:33 -04001221void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001223 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (ctx) {
David Howells2b0143b2015-03-17 22:25:59 +00001226 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001227
Trond Myklebuste97bc662021-05-11 23:41:10 -04001228 clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags);
Peng Tao0bcbf032015-12-05 15:57:31 +08001229 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 filp->private_data = NULL;
Trond Myklebust4eae5012015-09-04 15:17:53 -04001236 put_nfs_open_context_sync(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
1240/*
1241 * These allocate and release file read/write context information.
1242 */
1243int nfs_open(struct inode *inode, struct file *filp)
1244{
1245 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
ChenXiaoSong6f1c1d92022-09-23 13:40:14 +08001247 ctx = alloc_nfs_open_context(file_dentry(filp),
1248 flags_to_mode(filp->f_flags), filp);
Al Viro5ede7b12011-10-23 18:49:54 -04001249 if (IS_ERR(ctx))
1250 return PTR_ERR(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 nfs_file_set_open_context(filp, ctx);
1252 put_nfs_open_context(ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +01001253 nfs_fscache_open_file(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/*
1258 * This function is called whenever some part of NFS notices that
1259 * the cached attributes have to be refreshed.
1260 */
1261int
1262__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1263{
1264 int status = -ESTALE;
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001265 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Niels de Vos1e8968c2013-12-17 18:20:16 +01001268 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1269 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001271 trace_nfs_revalidate_inode_enter(inode);
1272
Chuck Lever85233a72006-10-19 23:28:42 -07001273 if (is_bad_inode(inode))
Chuck Lever412d5822005-08-18 11:24:11 -07001274 goto out;
Trond Myklebust7fdc49c2007-09-28 19:11:33 -04001275 if (NFS_STALE(inode))
1276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Trond Myklebustac46bd32016-07-05 13:46:53 -04001278 /* 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
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001285 status = -ENOMEM;
Anna Schumaker2ef61e02021-10-22 13:11:07 -04001286 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001287 if (fattr == NULL)
1288 goto out;
1289
Trond Myklebust691beb12008-10-05 14:48:22 -04001290 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
David Quigley14c43f72013-05-22 12:50:43 -04001291
Anna Schumaker2ef61e02021-10-22 13:11:07 -04001292 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (status != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001294 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001296 (unsigned long long)NFS_FILEID(inode), status);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001297 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04001305 nfs_set_inode_stale(inode);
1306 else
1307 nfs_zap_caches(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Anna Schumaker2ef61e02021-10-22 13:11:07 -04001309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001312 status = nfs_refresh_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (status) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001314 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001316 (unsigned long long)NFS_FILEID(inode), status);
Anna Schumaker2ef61e02021-10-22 13:11:07 -04001317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Chuck Lever55296802005-08-18 11:24:09 -07001319
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001320 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +00001321 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -07001322
Anna Schumakerdd225cb2021-10-22 13:11:12 -04001323 nfs_setsecurity(inode, fattr);
Jeff Layton3da580a2013-11-02 06:57:18 -04001324
Niels de Vos1e8968c2013-12-17 18:20:16 +01001325 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001327 (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
David Quigley14c43f72013-05-22 12:50:43 -04001329out:
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001330 nfs_free_fattr(fattr);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001331 trace_nfs_revalidate_inode_exit(inode, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return status;
1333}
1334
Scott Mayhew43f291c2013-07-05 17:49:30 -04001335int nfs_attribute_cache_expired(struct inode *inode)
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001336{
Trond Myklebustb4d23142010-03-10 15:21:44 -05001337 if (nfs_have_delegated_attributes(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001339 return nfs_attribute_timeout(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342/**
1343 * nfs_revalidate_inode - Revalidate the inode attributes
Trond Myklebust302fad72019-02-18 13:32:38 -05001344 * @inode: pointer to inode struct
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001345 * @flags: cache flags to check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
1347 * Updates inode attribute information by retrieving the data from the server.
1348 */
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001349int nfs_revalidate_inode(struct inode *inode, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001351 if (!nfs_check_cache_invalid(inode, flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return NFS_STALE(inode) ? -ESTALE : 0;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04001353 return __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04001355EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebust1cda7072010-02-19 17:03:30 -08001357static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust717d44e2007-01-24 11:54:55 -08001358{
Trond Myklebustf8806c82013-08-05 13:26:31 -04001359 int ret;
1360
Dave Wysochanskia6b5a282020-11-14 13:43:54 -05001361 nfs_fscache_invalidate(inode, 0);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001362 if (mapping->nrpages != 0) {
Trond Myklebustf8806c82013-08-05 13:26:31 -04001363 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);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001369 if (ret < 0)
1370 return ret;
1371 }
Trond Myklebust717d44e2007-01-24 11:54:55 -08001372 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001373
Niels de Vos1e8968c2013-12-17 18:20:16 +01001374 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1375 inode->i_sb->s_id,
1376 (unsigned long long)NFS_FILEID(inode));
Trond Myklebust717d44e2007-01-24 11:54:55 -08001377 return 0;
1378}
1379
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001380/**
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 */
1386int 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,
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02001407 nfs_wait_bit_killable,
1408 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001409 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();
NeilBrown3db63da2023-03-22 09:27:04 +11001424 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
1425 nfs_ooo_clear(nfsi);
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001426 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);
1434out:
1435 return ret;
1436}
1437
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001438bool nfs_mapping_need_revalidate_inode(struct inode *inode)
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001439{
Trond Myklebust13c0b082021-03-25 21:07:21 -04001440 return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) ||
Trond Myklebust61540bf2016-12-08 18:18:38 -05001441 NFS_STALE(inode);
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001442}
1443
Al Viro0d0def42015-11-17 21:14:24 -05001444int 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);
1461out:
1462 return ret;
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/**
Trond Myklebustbe527492016-06-22 08:19:36 -04001466 * nfs_revalidate_mapping - Revalidate the pagecache
Trond Myklebust302fad72019-02-18 13:32:38 -05001467 * @inode: pointer to host inode
1468 * @mapping: pointer to mapping
Trond Myklebust7d52e862005-06-22 17:16:30 +00001469 */
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001470int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +00001471{
Mel Gorman29418aa2012-07-31 16:45:10 -07001472 /* swapfiles are not supposed to be shared. */
1473 if (IS_SWAPFILE(inode))
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001474 return 0;
Mel Gorman29418aa2012-07-31 16:45:10 -07001475
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001476 if (nfs_mapping_need_revalidate_inode(inode)) {
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001477 int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001478 if (ret < 0)
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001479 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001480 }
Jeff Laytond529ef82014-01-27 13:46:15 -05001481
Trond Myklebust28aa2f92021-02-08 08:55:47 -05001482 return nfs_clear_invalid_mapping(mapping);
Trond Myklebust7d52e862005-06-22 17:16:30 +00001483}
1484
Trond Myklebustca0daa22016-06-08 17:08:28 -04001485static bool nfs_file_has_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001486{
Trond Myklebustca0daa22016-06-08 17:08:28 -04001487 struct inode *inode = &nfsi->vfs_inode;
1488
Trond Myklebustca0daa22016-06-08 17:08:28 -04001489 if (!S_ISREG(inode->i_mode))
1490 return false;
1491 if (list_empty(&nfsi->open_files))
1492 return false;
Trond Myklebust6ba0c4e2018-09-02 15:34:37 -04001493 return inode_is_open_for_write(inode);
Trond Myklebust874f9462015-03-02 23:32:08 -05001494}
1495
Trond Myklebust651b0e72016-06-25 17:24:46 -04001496static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001497{
Trond Myklebust651b0e72016-06-25 17:24:46 -04001498 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
Trond Myklebust874f9462015-03-02 23:32:08 -05001499}
1500
Trond Myklebust783b1942018-03-20 16:53:29 -04001501static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001502{
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001503 struct timespec64 ts;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001504
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001505 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1506 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001507 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05001508 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001509 if (S_ISDIR(inode->i_mode))
Trond Myklebust6edf9602014-06-20 13:11:01 -04001510 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Frank van der Linden0f44da52020-06-23 22:39:00 +00001511 else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1512 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001513 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001514 /* If we have atomic WCC data, we may update some attributes */
Jeff Layton55e04e92023-07-05 15:01:22 -04001515 ts = inode_get_ctime(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001516 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1517 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001518 && timespec64_equal(&ts, &fattr->pre_ctime)) {
Jeff Layton55e04e92023-07-05 15:01:22 -04001519 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001520 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001521
Jeff Layton41d581a92023-10-04 14:52:36 -04001522 ts = inode_get_mtime(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001523 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1524 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001525 && timespec64_equal(&ts, &fattr->pre_mtime)) {
Jeff Layton41d581a92023-10-04 14:52:36 -04001526 inode_set_mtime_to_ts(inode, fattr->mtime);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001527 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001528 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)
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001531 && !nfs_have_writebacks(inode)) {
Chuck Lever110cb2d2021-10-04 10:10:16 -04001532 trace_nfs_size_wcc(inode, fattr->size);
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001533 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001534 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
Trond Myklebust33801142005-10-27 22:12:39 -04001538 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001539 * @inode: pointer to inode
1540 * @fattr: updated attributes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
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 */
Trond Myklebust33801142005-10-27 22:12:39 -04001546static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct nfs_inode *nfsi = NFS_I(inode);
1549 loff_t cur_size, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001550 unsigned long invalid = 0;
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001551 struct timespec64 ts;
Chuck Leverdc592502005-08-18 11:24:12 -07001552
Trond Myklebust4201916f2024-06-16 21:21:24 -04001553 if (nfs_have_delegated_attributes(inode))
Trond Myklebust01da47b2012-04-29 12:30:19 -04001554 return 0;
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001555
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001556 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001561 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001562 /* 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;
NeilBrowncc896842017-07-05 12:22:20 +10001566 return -ESTALE;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001567 }
Al Viro6e3e2c42021-03-01 20:37:10 -05001568 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode))
NeilBrowncc896842017-07-05 12:22:20 +10001569 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001571
Trond Myklebust651b0e72016-06-25 17:24:46 -04001572 if (!nfs_file_has_buffered_writers(nfsi)) {
Trond Myklebustca0daa22016-06-08 17:08:28 -04001573 /* Verify a few of the more important attributes */
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001574 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
Trond Myklebust04c63492021-03-25 18:13:36 -04001575 invalid |= NFS_INO_INVALID_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jeff Layton41d581a92023-10-04 14:52:36 -04001577 ts = inode_get_mtime(inode);
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001578 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001579 invalid |= NFS_INO_INVALID_MTIME;
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001580
Jeff Layton55e04e92023-07-05 15:01:22 -04001581 ts = inode_get_ctime(inode);
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001582 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001583 invalid |= NFS_INO_INVALID_CTIME;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001584
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)
Trond Myklebust04c63492021-03-25 18:13:36 -04001589 invalid |= NFS_INO_INVALID_SIZE;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001590 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /* Have any file permissions changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001594 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
Trond Myklebust04c63492021-03-25 18:13:36 -04001595 invalid |= NFS_INO_INVALID_MODE;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001596 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
Trond Myklebust04c63492021-03-25 18:13:36 -04001597 invalid |= NFS_INO_INVALID_OTHER;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001598 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
Trond Myklebust04c63492021-03-25 18:13:36 -04001599 invalid |= NFS_INO_INVALID_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* Has the link count changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001602 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
Trond Myklebustfabf2b32021-03-25 13:14:42 -04001603 invalid |= NFS_INO_INVALID_NLINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Jeff Layton41d581a92023-10-04 14:52:36 -04001605 ts = inode_get_atime(inode);
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001606 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001607 invalid |= NFS_INO_INVALID_ATIME;
1608
1609 if (invalid != 0)
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001610 nfs_set_cache_invalid(inode, invalid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Trond Myklebust33801142005-10-27 22:12:39 -04001612 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614}
1615
Trond Myklebustae05f262008-10-28 15:21:40 -04001616static atomic_long_t nfs_attr_generation_counter;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001617
1618static unsigned long nfs_read_attr_generation_counter(void)
1619{
Trond Myklebustae05f262008-10-28 15:21:40 -04001620 return atomic_long_read(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001621}
1622
1623unsigned long nfs_inc_attr_generation_counter(void)
1624{
Trond Myklebustae05f262008-10-28 15:21:40 -04001625 return atomic_long_inc_return(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001626}
Trond Myklebust3235b402015-02-26 19:52:06 -05001627EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001628
1629void 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();
Trond Myklebust6926afd12012-01-07 13:22:46 -05001634 fattr->owner_name = NULL;
1635 fattr->group_name = NULL;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001636}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001637EXPORT_SYMBOL_GPL(nfs_fattr_init);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001638
Trond Myklebust140e0492015-02-26 17:42:42 -05001639/**
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 */
1650void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1651{
1652 fattr->gencount = nfs_inc_attr_generation_counter();
1653}
1654
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001655struct nfs_fattr *nfs_alloc_fattr(void)
1656{
1657 struct nfs_fattr *fattr;
1658
Trond Myklebustda48f262022-01-29 13:57:38 -05001659 fattr = kmalloc(sizeof(*fattr), GFP_KERNEL);
Trond Myklebustd4a95a72021-11-04 18:03:26 -04001660 if (fattr != NULL) {
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001661 nfs_fattr_init(fattr);
Trond Myklebustd4a95a72021-11-04 18:03:26 -04001662 fattr->label = NULL;
1663 }
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001664 return fattr;
1665}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001666EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001667
Anna Schumakerd755ad82021-10-22 13:11:00 -04001668struct 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
Trond Myklebustda48f262022-01-29 13:57:38 -05001675 fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
Anna Schumakerd755ad82021-10-22 13:11:00 -04001676 if (IS_ERR(fattr->label)) {
1677 kfree(fattr);
1678 return NULL;
1679 }
1680
1681 return fattr;
1682}
1683EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label);
1684
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001685struct nfs_fh *nfs_alloc_fhandle(void)
1686{
1687 struct nfs_fh *fh;
1688
Trond Myklebustda48f262022-01-29 13:57:38 -05001689 fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001690 if (fh != NULL)
1691 fh->size = 0;
1692 return fh;
1693}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001694EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001695
Trond Myklebuste27d3592012-03-18 14:07:42 -04001696#ifdef NFS_DEBUG
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001697/*
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 */
1705u32 _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 */
Trond Myklebust1264a2f2013-08-12 16:06:31 -04001709 return nfs_fhandle_hash(fh);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001710}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001711EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001712
1713/*
Chuck Lever20d27e92012-03-01 17:01:31 -05001714 * _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 */
Chuck Lever20d27e92012-03-01 17:01:31 -05001721void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1722{
1723 unsigned short i;
1724
Trond Myklebustfa68a1b2012-03-06 10:14:35 -05001725 if (fh == NULL || fh->size == 0) {
Chuck Lever20d27e92012-03-01 17:01:31 -05001726 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1727 return;
1728 }
1729
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001730 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1731 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
Chuck Lever20d27e92012-03-01 17:01:31 -05001732 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}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001756EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
Chuck Lever20d27e92012-03-01 17:01:31 -05001757#endif
1758
1759/**
Trond Myklebust6f9be832021-03-26 11:01:19 -04001760 * nfs_inode_attrs_cmp_generic - compare attributes
Trond Myklebust302fad72019-02-18 13:32:38 -05001761 * @fattr: attributes
Trond Myklebust6f9be832021-03-26 11:01:19 -04001762 * @inode: pointer to inode
Trond Myklebusta10ad172008-09-23 17:28:41 -04001763 *
1764 * Attempt to divine whether or not an RPC call reply carrying stale
1765 * attributes got scheduled after another call carrying updated ones.
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001766 * Note also the check for wraparound of 'attr_gencount'
Trond Myklebusta10ad172008-09-23 17:28:41 -04001767 *
Trond Myklebust6f9be832021-03-26 11:01:19 -04001768 * 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'.
Trond Myklebusta10ad172008-09-23 17:28:41 -04001771 */
Trond Myklebust6f9be832021-03-26 11:01:19 -04001772static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr,
1773 const struct inode *inode)
Trond Myklebusta10ad172008-09-23 17:28:41 -04001774{
Trond Myklebust9fdbfad2021-03-29 16:46:05 -04001775 unsigned long attr_gencount = NFS_I(inode)->attr_gencount;
Trond Myklebusta10ad172008-09-23 17:28:41 -04001776
Trond Myklebust9fdbfad2021-03-29 16:46:05 -04001777 return (long)(fattr->gencount - attr_gencount) > 0 ||
1778 (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0;
Trond Myklebusta10ad172008-09-23 17:28:41 -04001779}
1780
Trond Myklebust6f9be832021-03-26 11:01:19 -04001781/**
1782 * nfs_inode_attrs_cmp_monotonic - compare attributes
1783 * @fattr: attributes
1784 * @inode: pointer to inode
Trond Myklebust33801142005-10-27 22:12:39 -04001785 *
1786 * Attempt to divine whether or not an RPC call reply carrying stale
1787 * attributes got scheduled after another call carrying updated ones.
1788 *
Trond Myklebust6f9be832021-03-26 11:01:19 -04001789 * 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.
Trond Myklebust33801142005-10-27 22:12:39 -04001796 */
Trond Myklebust6f9be832021-03-26 11:01:19 -04001797static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr,
1798 const struct inode *inode)
Trond Myklebust33801142005-10-27 22:12:39 -04001799{
Trond Myklebust6f9be832021-03-26 11:01:19 -04001800 s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode);
1801 if (diff > 0)
1802 return 1;
1803 return diff == 0 ? 0 : -1;
Chuck Leverf551e442006-09-20 14:33:04 -04001804}
1805
Trond Myklebust6f9be832021-03-26 11:01:19 -04001806/**
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 */
1821static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr,
1822 const struct inode *inode)
Chuck Leverf551e442006-09-20 14:33:04 -04001823{
Trond Myklebust6f9be832021-03-26 11:01:19 -04001824 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 */
1837static 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
Trond Myklebust7b24dacf2021-04-09 09:30:21 -04001857/**
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 */
1867static 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;
Trond Myklebusteea41332021-09-26 14:05:04 -04001876 enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type;
Trond Myklebust7b24dacf2021-04-09 09:30:21 -04001877
Trond Myklebusteea41332021-09-26 14:05:04 -04001878 if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED &&
1879 !(cache_validity & NFS_INO_INVALID_CHANGE) &&
Trond Myklebust7b24dacf2021-04-09 09:30:21 -04001880 (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
NeilBrown3db63da2023-03-22 09:27:04 +11001887static 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
1933static 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
Trond Myklebust6f9be832021-03-26 11:01:19 -04001947static 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;
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001952
1953 trace_nfs_refresh_inode_enter(inode);
1954
Trond Myklebust7b24dacf2021-04-09 09:30:21 -04001955 if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode))
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001956 ret = nfs_update_inode(inode, fattr);
NeilBrown3db63da2023-03-22 09:27:04 +11001957 else {
1958 nfs_ooo_record(NFS_I(inode), fattr);
1959
1960 if (attr_cmp == 0)
1961 ret = nfs_check_inode_attributes(inode, fattr);
1962 }
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001963
1964 trace_nfs_refresh_inode_exit(inode, ret);
1965 return ret;
Trond Myklebust33801142005-10-27 22:12:39 -04001966}
1967
1968/**
1969 * nfs_refresh_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001970 * @inode: pointer to inode
1971 * @fattr: updated attributes
Trond Myklebust33801142005-10-27 22:12:39 -04001972 *
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 */
1978int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1979{
1980 int status;
Trond Myklebust33801142005-10-27 22:12:39 -04001981
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001982 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
Trond Myklebust33801142005-10-27 22:12:39 -04001983 return 0;
1984 spin_lock(&inode->i_lock);
1985 status = nfs_refresh_inode_locked(inode, fattr);
1986 spin_unlock(&inode->i_lock);
David Howellsef79c092009-04-03 16:42:43 +01001987
Trond Myklebust33801142005-10-27 22:12:39 -04001988 return status;
1989}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001990EXPORT_SYMBOL_GPL(nfs_refresh_inode);
Trond Myklebust33801142005-10-27 22:12:39 -04001991
Trond Myklebust16e14372018-03-20 16:53:31 -04001992static int nfs_post_op_update_inode_locked(struct inode *inode,
1993 struct nfs_fattr *fattr, unsigned int invalid)
Trond Myklebustd65f5572008-10-05 12:27:55 -04001994{
Trond Myklebust6edf9602014-06-20 13:11:01 -04001995 if (S_ISDIR(inode->i_mode))
1996 invalid |= NFS_INO_INVALID_DATA;
1997 nfs_set_cache_invalid(inode, invalid);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001998 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1999 return 0;
2000 return nfs_refresh_inode_locked(inode, fattr);
2001}
2002
Trond Myklebustdecf4912005-10-27 22:12:39 -04002003/**
2004 * nfs_post_op_update_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05002005 * @inode: pointer to inode
2006 * @fattr: updated attributes
Trond Myklebustdecf4912005-10-27 22:12:39 -04002007 *
2008 * After an operation that has changed the inode metadata, mark the
2009 * attribute cache as being invalid, then try to update it.
Chuck Leverf551e442006-09-20 14:33:04 -04002010 *
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().
Trond Myklebustdecf4912005-10-27 22:12:39 -04002016 */
2017int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
2018{
Trond Myklebustd65f5572008-10-05 12:27:55 -04002019 int status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04002020
Trond Myklebust7668fdb2007-10-01 09:59:15 -04002021 spin_lock(&inode->i_lock);
Trond Myklebust92d64e42015-02-26 19:48:26 -05002022 nfs_fattr_set_barrier(fattr);
Trond Myklebust16e14372018-03-20 16:53:31 -04002023 status = nfs_post_op_update_inode_locked(inode, fattr,
2024 NFS_INO_INVALID_CHANGE
Trond Myklebustd5541682018-05-29 11:15:49 -04002025 | NFS_INO_INVALID_CTIME
2026 | NFS_INO_REVAL_FORCED);
Trond Myklebust7668fdb2007-10-01 09:59:15 -04002027 spin_unlock(&inode->i_lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04002028
Trond Myklebust870a5be2008-10-05 12:07:23 -04002029 return status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04002030}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04002031EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -04002032
Trond Myklebust70ca8852007-09-30 15:21:24 -04002033/**
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05002034 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05002035 * @inode: pointer to inode
2036 * @fattr: updated attributes
Trond Myklebust70ca8852007-09-30 15:21:24 -04002037 *
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 */
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05002044int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebust70ca8852007-09-30 15:21:24 -04002045{
Trond Myklebust6f9be832021-03-26 11:01:19 -04002046 int attr_cmp = nfs_inode_attrs_cmp(fattr, inode);
Trond Myklebustd65f5572008-10-05 12:27:55 -04002047 int status;
2048
Trond Myklebustd65f5572008-10-05 12:27:55 -04002049 /* Don't do a WCC update if these attributes are already stale */
Trond Myklebust6f9be832021-03-26 11:01:19 -04002050 if (attr_cmp < 0)
2051 return 0;
2052 if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) {
NeilBrown3db63da2023-03-22 09:27:04 +11002053 /* Record the pre/post change info before clearing PRECHANGE */
2054 nfs_ooo_record(NFS_I(inode), fattr);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002055 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
2056 | NFS_ATTR_FATTR_PRESIZE
2057 | NFS_ATTR_FATTR_PREMTIME
2058 | NFS_ATTR_FATTR_PRECTIME);
Trond Myklebustd65f5572008-10-05 12:27:55 -04002059 goto out_noforce;
2060 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002061 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
2062 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05002063 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002064 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04002065 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002066 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
2067 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Jeff Layton55e04e92023-07-05 15:01:22 -04002068 fattr->pre_ctime = inode_get_ctime(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002069 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
2070 }
2071 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
2072 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Jeff Layton41d581a92023-10-04 14:52:36 -04002073 fattr->pre_mtime = inode_get_mtime(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002074 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
2075 }
2076 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
2077 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
Trond Myklebusta3d01452008-06-11 12:21:19 -04002078 fattr->pre_size = i_size_read(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002079 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04002080 }
Trond Myklebustd65f5572008-10-05 12:27:55 -04002081out_noforce:
Trond Myklebust16e14372018-03-20 16:53:31 -04002082 status = nfs_post_op_update_inode_locked(inode, fattr,
2083 NFS_INO_INVALID_CHANGE
2084 | NFS_INO_INVALID_CTIME
Zheng Bin3a39e772020-05-21 17:17:21 +08002085 | NFS_INO_INVALID_MTIME
2086 | NFS_INO_INVALID_BLOCKS);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05002087 return status;
2088}
2089
2090/**
2091 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05002092 * @inode: pointer to inode
2093 * @fattr: updated attributes
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05002094 *
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 */
2101int 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);
Trond Myklebust8f8ba1d2015-02-26 17:54:58 -05002106 nfs_fattr_set_barrier(fattr);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05002107 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
Trond Myklebustd65f5572008-10-05 12:27:55 -04002108 spin_unlock(&inode->i_lock);
2109 return status;
Trond Myklebust70ca8852007-09-30 15:21:24 -04002110}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04002111EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002112
Anna Schumakerea96d1e2015-04-03 14:35:59 -04002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/*
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 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05002126static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebustce62b112018-03-05 15:01:18 -05002128 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00002130 loff_t cur_isize, new_isize;
Trond Myklebustce62b112018-03-05 15:01:18 -05002131 u64 fattr_supported = server->fattr_valid;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04002132 unsigned long invalid = 0;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08002133 unsigned long now = jiffies;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002134 unsigned long save_cache_validity;
Trond Myklebust651b0e72016-06-25 17:24:46 -04002135 bool have_writers = nfs_file_has_buffered_writers(nfsi);
Benjamin Coddington944171c2016-07-28 14:41:10 -04002136 bool cache_revalidated = true;
Trond Myklebust0b467262018-06-03 11:56:05 -04002137 bool attr_changed = false;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002138 bool have_delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Niels de Vos1e8968c2013-12-17 18:20:16 +01002140 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07002141 __func__, inode->i_sb->s_id, inode->i_ino,
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -05002142 nfs_display_fhandle_hash(NFS_FH(inode)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 atomic_read(&inode->i_count), fattr->valid);
2144
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04002145 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;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04002149 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04002150 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04002151 /* 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;
Matthew Treinishe73e6c92012-04-30 11:32:57 -04002155 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /*
2164 * Make sure the inode's type hasn't changed.
2165 */
Al Viro6e3e2c42021-03-01 20:37:10 -05002166 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) {
Matthew Treinishe73e6c92012-04-30 11:32:57 -04002167 /*
2168 * Big trouble! The inode has become a different object.
2169 */
Niels de Vos1e8968c2013-12-17 18:20:16 +01002170 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
Matthew Treinishe73e6c92012-04-30 11:32:57 -04002171 __func__, inode->i_ino, inode->i_mode, fattr->mode);
2172 goto out_err;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Trond Myklebusta0356862007-06-05 13:26:15 -04002175 /* Update the fsid? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002176 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Trond Myklebustc37dcd332008-03-06 12:34:50 -05002177 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
David Howells36d43a42011-01-14 18:45:42 +00002178 !IS_AUTOMOUNT(inode))
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04002179 server->fsid = fattr->fsid;
2180
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002181 /* Save the delegation state before clearing cache_validity */
2182 have_delegation = nfs_have_delegated_attributes(inode);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
2185 * Update the read time so we don't revalidate too often.
2186 */
Trond Myklebust33801142005-10-27 22:12:39 -04002187 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08002188
Trond Myklebust0a741f52024-06-16 21:21:30 -04002189 /* Fix up any delegated attributes in the struct nfs_fattr */
2190 nfs_fattr_fixup_delegated(inode, fattr);
2191
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002192 save_cache_validity = nfsi->cache_validity;
2193 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
2194 | NFS_INO_INVALID_ATIME
2195 | NFS_INO_REVAL_FORCED
Zheng Bin3a39e772020-05-21 17:17:21 +08002196 | NFS_INO_INVALID_BLOCKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Trond Myklebusta895b4a2006-01-03 09:55:40 +01002198 /* Do atomic weak cache consistency updates */
Trond Myklebust783b1942018-03-20 16:53:29 -04002199 nfs_wcc_update_inode(inode, fattr);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01002200
Benjamin Coddington944171c2016-07-28 14:41:10 -04002201 if (pnfs_layoutcommit_outstanding(inode)) {
Trond Myklebust709fa572021-03-25 14:51:36 -04002202 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 |
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002206 NFS_INO_INVALID_BLOCKS);
Benjamin Coddington944171c2016-07-28 14:41:10 -04002207 cache_revalidated = false;
2208 }
Trond Myklebust10b7e9a2016-07-18 00:51:01 -04002209
Trond Myklebust47aabaa2007-09-27 15:57:24 -04002210 /* More cache consistency checks */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002211 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
NeilBrown3db63da2023-03-22 09:27:04 +11002212 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 }
Goffredo Baroncellic472c072018-02-01 08:15:25 -05002221 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
Trond Myklebust38512aa982016-06-07 21:44:08 -04002222 /* Could it be a race with writeback? */
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002223 if (!(have_writers || have_delegation)) {
Trond Myklebust0b467262018-06-03 11:56:05 -04002224 invalid |= NFS_INO_INVALID_DATA
Trond Myklebust38512aa982016-06-07 21:44:08 -04002225 | NFS_INO_INVALID_ACCESS
Frank van der Linden95ad37f2020-06-23 22:39:04 +00002226 | NFS_INO_INVALID_ACL
2227 | NFS_INO_INVALID_XATTR;
Trond Myklebustcac88f92018-03-20 16:53:30 -04002228 /* Force revalidate of all attributes */
Trond Myklebust16e14372018-03-20 16:53:31 -04002229 save_cache_validity |= NFS_INO_INVALID_CTIME
2230 | NFS_INO_INVALID_MTIME
2231 | NFS_INO_INVALID_SIZE
Trond Myklebust4cdfeb62021-03-24 12:29:41 -04002232 | NFS_INO_INVALID_BLOCKS
Trond Myklebustfabf2b32021-03-25 13:14:42 -04002233 | NFS_INO_INVALID_NLINK
Trond Myklebust720869e2021-04-13 09:41:16 -04002234 | NFS_INO_INVALID_MODE
Trond Myklebust16e14372018-03-20 16:53:31 -04002235 | NFS_INO_INVALID_OTHER;
Trond Myklebust38512aa982016-06-07 21:44:08 -04002236 if (S_ISDIR(inode->i_mode))
2237 nfs_force_lookup_revalidate(inode);
Trond Myklebusta9601ac2021-06-26 11:07:59 -04002238 attr_changed = true;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002239 dprintk("NFS: change_attr change on server for file %s/%ld\n",
2240 inode->i_sb->s_id,
2241 inode->i_ino);
NeilBrown3db63da2023-03-22 09:27:04 +11002242 } 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 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05002247 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002248 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002249 } else {
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002250 nfsi->cache_validity |=
2251 save_cache_validity & NFS_INO_INVALID_CHANGE;
Trond Myklebust213bb582021-06-26 09:23:51 -04002252 if (!have_delegation ||
2253 (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0)
2254 cache_revalidated = false;
Trond Myklebustade14a72015-12-29 18:55:19 -05002255 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002256
Trond Myklebust213bb582021-06-26 09:23:51 -04002257 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Jeff Layton41d581a92023-10-04 14:52:36 -04002258 inode_set_mtime_to_ts(inode, fattr->mtime);
Trond Myklebust213bb582021-06-26 09:23:51 -04002259 else if (fattr_supported & NFS_ATTR_FATTR_MTIME)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002260 nfsi->cache_validity |=
2261 save_cache_validity & NFS_INO_INVALID_MTIME;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002262
Trond Myklebust213bb582021-06-26 09:23:51 -04002263 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Jeff Layton55e04e92023-07-05 15:01:22 -04002264 inode_set_ctime_to_ts(inode, fattr->ctime);
Trond Myklebust213bb582021-06-26 09:23:51 -04002265 else if (fattr_supported & NFS_ATTR_FATTR_CTIME)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002266 nfsi->cache_validity |=
2267 save_cache_validity & NFS_INO_INVALID_CTIME;
Trond Myklebust47aabaa2007-09-27 15:57:24 -04002268
Trond Myklebust951a1432005-06-22 17:16:30 +00002269 /* Check if our cached file size is stale */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002270 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
2271 new_isize = nfs_size_to_loff_t(fattr->size);
2272 cur_isize = i_size_read(inode);
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002273 if (new_isize != cur_isize && !have_delegation) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002274 /* Do we perhaps have any outstanding writes, or has
2275 * the file grown beyond our last write? */
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04002276 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
Chuck Lever110cb2d2021-10-04 10:10:16 -04002277 trace_nfs_size_update(inode, new_isize);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002278 i_size_write(inode, new_isize);
Trond Myklebustca0daa22016-06-08 17:08:28 -04002279 if (!have_writers)
Trond Myklebust16e14372018-03-20 16:53:31 -04002280 invalid |= NFS_INO_INVALID_DATA;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Trond Myklebust4cdfeb62021-03-24 12:29:41 -04002283 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 }
Trond Myklebust213bb582021-06-26 09:23:51 -04002289 } else
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002290 nfsi->cache_validity |=
2291 save_cache_validity & NFS_INO_INVALID_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002293 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Jeff Layton41d581a92023-10-04 14:52:36 -04002294 inode_set_atime_to_ts(inode, fattr->atime);
Trond Myklebust213bb582021-06-26 09:23:51 -04002295 else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002296 nfsi->cache_validity |=
2297 save_cache_validity & NFS_INO_INVALID_ATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002299 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2300 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
Trond Myklebust9b4b3512010-02-03 08:27:35 -05002301 umode_t newmode = inode->i_mode & S_IFMT;
2302 newmode |= fattr->mode & S_IALLUGO;
2303 inode->i_mode = newmode;
Trond Myklebust16e14372018-03-20 16:53:31 -04002304 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04002305 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002306 }
Trond Myklebust213bb582021-06-26 09:23:51 -04002307 } else if (fattr_supported & NFS_ATTR_FATTR_MODE)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002308 nfsi->cache_validity |=
2309 save_cache_validity & NFS_INO_INVALID_MODE;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002310
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002311 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08002312 if (!uid_eq(inode->i_uid, fattr->uid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04002313 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04002314 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002315 inode->i_uid = fattr->uid;
2316 }
Trond Myklebust213bb582021-06-26 09:23:51 -04002317 } else if (fattr_supported & NFS_ATTR_FATTR_OWNER)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002318 nfsi->cache_validity |=
2319 save_cache_validity & NFS_INO_INVALID_OTHER;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002320
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002321 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08002322 if (!gid_eq(inode->i_gid, fattr->gid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04002323 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04002324 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002325 inode->i_gid = fattr->gid;
2326 }
Trond Myklebust213bb582021-06-26 09:23:51 -04002327 } else if (fattr_supported & NFS_ATTR_FATTR_GROUP)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002328 nfsi->cache_validity |=
2329 save_cache_validity & NFS_INO_INVALID_OTHER;
Trond Myklebust921615f2008-10-14 19:23:07 -04002330
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002331 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
Trond Myklebust36a10a32021-10-02 19:21:49 -04002332 if (inode->i_nlink != fattr->nlink)
Miklos Szeredibfe86842011-10-28 14:13:29 +02002333 set_nlink(inode, fattr->nlink);
Trond Myklebust213bb582021-06-26 09:23:51 -04002334 } else if (fattr_supported & NFS_ATTR_FATTR_NLINK)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002335 nfsi->cache_validity |=
2336 save_cache_validity & NFS_INO_INVALID_NLINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002338 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * report the blocks in 512byte units
2341 */
2342 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Trond Myklebust213bb582021-06-26 09:23:51 -04002343 } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED)
Trond Myklebustce62b112018-03-05 15:01:18 -05002344 nfsi->cache_validity |=
2345 save_cache_validity & NFS_INO_INVALID_BLOCKS;
Trond Myklebustce62b112018-03-05 15:01:18 -05002346
Trond Myklebust213bb582021-06-26 09:23:51 -04002347 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002348 inode->i_blocks = fattr->du.nfs2.blocks;
Trond Myklebust213bb582021-06-26 09:23:51 -04002349 else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED)
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04002350 nfsi->cache_validity |=
2351 save_cache_validity & NFS_INO_INVALID_BLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* Update attrtimeo value if we're out of the unstable period */
Trond Myklebust0b467262018-06-03 11:56:05 -04002354 if (attr_changed) {
Chuck Lever91d5b472006-03-20 13:44:14 -05002355 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebust3e7d9502007-02-05 14:26:28 -08002357 nfsi->attrtimeo_timestamp = now;
Trond Myklebustf5062002015-02-26 19:34:32 -05002358 /* Set barrier to be more recent than all outstanding updates */
Trond Myklebust4704f0e2008-10-14 19:16:07 -04002359 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002360 } else {
Trond Myklebustade14a72015-12-29 18:55:19 -05002361 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 }
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002368 nfsi->attrtimeo_timestamp = now;
2369 }
Trond Myklebustf5062002015-02-26 19:34:32 -05002370 /* Set the barrier to be more recent than this fattr */
Trond Myklebust9fdbfad2021-03-29 16:46:05 -04002371 if ((long)(fattr->gencount - nfsi->attr_gencount) > 0)
Trond Myklebustf5062002015-02-26 19:34:32 -05002372 nfsi->attr_gencount = fattr->gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Jeff Laytonc8120122015-11-25 13:50:11 -05002374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* 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;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002379 nfs_set_cache_invalid(inode, invalid);
David Howellsde242c02012-12-20 21:52:38 +00002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
Trond Myklebustb37b03b2005-11-25 17:10:06 -05002382 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
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 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002388 nfs_set_inode_stale_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -ESTALE;
2390}
2391
David Howellsf7b422b2006-06-09 09:34:33 -04002392struct inode *nfs_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 struct nfs_inode *nfsi;
Muchun Songfd60b282022-03-22 14:41:03 -07002395 nfsi = alloc_inode_sb(sb, nfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (!nfsi)
2397 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07002398 nfsi->flags = 0UL;
2399 nfsi->cache_validity = 0UL;
NeilBrown3db63da2023-03-22 09:27:04 +11002400 nfsi->ooo = NULL;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002401#if IS_ENABLED(CONFIG_NFS_V4)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002402 nfsi->nfs4_acl = NULL;
2403#endif /* CONFIG_NFS_V4 */
Frank van der Linden95ad37f2020-06-23 22:39:04 +00002404#ifdef CONFIG_NFS_V4_2
2405 nfsi->xattr_cache = NULL;
2406#endif
Dave Wysochanski000dbe02023-02-20 08:43:06 -05002407 nfs_netfs_inode_init(nfsi);
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return &nfsi->vfs_inode;
2410}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002411EXPORT_SYMBOL_GPL(nfs_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Al Viroca1a1992019-04-15 20:19:40 -04002413void nfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002414{
NeilBrown3db63da2023-03-22 09:27:04 +11002415 kfree(NFS_I(inode)->ooo);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002416 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2417}
Al Viroca1a1992019-04-15 20:19:40 -04002418EXPORT_SYMBOL_GPL(nfs_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Andrew Mortond75d5412006-06-25 02:41:26 -07002420static inline void nfs4_init_once(struct nfs_inode *nfsi)
2421{
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002422#if IS_ENABLED(CONFIG_NFS_V4)
Andrew Mortond75d5412006-06-25 02:41:26 -07002423 INIT_LIST_HEAD(&nfsi->open_states);
2424 nfsi->delegation = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002425 init_rwsem(&nfsi->rwsem);
Benny Halevye5e94012010-10-20 00:18:01 -04002426 nfsi->layout = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002427#endif
2428}
David Howellsf7b422b2006-06-09 09:34:33 -04002429
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002430static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
yuzhe7e7ce2c2022-06-15 13:39:24 +08002432 struct nfs_inode *nfsi = foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Christoph Lametera35afb82007-05-16 22:10:57 -07002434 inode_init_once(&nfsi->vfs_inode);
Christoph Lametera35afb82007-05-16 22:10:57 -07002435 INIT_LIST_HEAD(&nfsi->open_files);
2436 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2437 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
Christoph Lametera35afb82007-05-16 22:10:57 -07002438 nfs4_init_once(nfsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
Paul Mundt20c2df82007-07-20 10:11:58 +09002440
David Howellsf7b422b2006-06-09 09:34:33 -04002441static int __init nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2444 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002445 0, (SLAB_RECLAIM_ACCOUNT|
Linus Torvaldsf88c3fb2024-03-12 20:32:19 -07002446 SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09002447 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (nfs_inode_cachep == NULL)
2449 return -ENOMEM;
2450
2451 return 0;
2452}
2453
David Brownell266bee82006-06-27 12:59:15 -07002454static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10002456 /*
2457 * Make sure all delayed rcu free inodes are flushed before we
2458 * destroy cache.
2459 */
2460 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002461 kmem_cache_destroy(nfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Trond Myklebustb9f5dd52024-09-05 15:09:56 -04002464struct workqueue_struct *nfslocaliod_workqueue;
Trond Myklebust57460062008-02-19 20:04:22 -05002465struct workqueue_struct *nfsiod_workqueue;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002466EXPORT_SYMBOL_GPL(nfsiod_workqueue);
Trond Myklebust57460062008-02-19 20:04:22 -05002467
2468/*
Trond Myklebustb9f5dd52024-09-05 15:09:56 -04002469 * Destroy the nfsiod workqueues
Trond Myklebust57460062008-02-19 20:04:22 -05002470 */
2471static void nfsiod_stop(void)
2472{
2473 struct workqueue_struct *wq;
2474
2475 wq = nfsiod_workqueue;
Trond Myklebustb9f5dd52024-09-05 15:09:56 -04002476 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 */
2492static 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;
Trond Myklebust57460062008-02-19 20:04:22 -05002512}
2513
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +03002514unsigned int nfs_net_id;
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04002515EXPORT_SYMBOL_GPL(nfs_net_id);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002516
2517static int nfs_net_init(struct net *net)
2518{
Josef Bacik15480362024-02-15 14:57:32 -05002519 struct nfs_net *nn = net_generic(net, nfs_net_id);
2520
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00002521 nfs_clients_init(net);
Kuniyuki Iwashima24457f12024-04-04 15:12:00 -07002522
2523 if (!rpc_proc_register(net, &nn->rpcstats)) {
2524 nfs_clients_exit(net);
2525 return -ENOMEM;
2526 }
2527
Eric W. Biederman65b38852014-07-31 04:35:20 -07002528 return nfs_fs_proc_net_init(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002529}
2530
2531static void nfs_net_exit(struct net *net)
2532{
Josef Bacikd47151b2024-02-15 14:57:31 -05002533 rpc_proc_unregister(net, "nfs");
Eric W. Biederman65b38852014-07-31 04:35:20 -07002534 nfs_fs_proc_net_exit(net);
Trond Myklebust10b7a702019-02-06 09:32:50 -05002535 nfs_clients_exit(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002536}
2537
2538static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545/*
2546 * Initialize NFS
2547 */
2548static int __init init_nfs_fs(void)
2549{
2550 int err;
2551
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002552 err = nfs_sysfs_init();
2553 if (err < 0)
2554 goto out10;
2555
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002556 err = register_pernet_subsys(&nfs_net_ops);
2557 if (err < 0)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002558 goto out9;
Trond Myklebuste571cbf2009-08-19 18:12:27 -04002559
Trond Myklebust57460062008-02-19 20:04:22 -05002560 err = nfsiod_start();
2561 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002562 goto out7;
Trond Myklebust57460062008-02-19 20:04:22 -05002563
David Howells6aaca562006-08-22 20:06:13 -04002564 err = nfs_fs_proc_init();
2565 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002566 goto out6;
David Howells6aaca562006-08-22 20:06:13 -04002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 err = nfs_init_nfspagecache();
2569 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002570 goto out5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 err = nfs_init_inodecache();
2573 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002574 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 err = nfs_init_readpagecache();
2577 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002578 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 err = nfs_init_writepagecache();
2581 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002582 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 err = nfs_init_directcache();
2585 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002586 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Kinglong Meecd738ee2015-07-01 11:58:31 +08002588 err = register_nfs_fs();
2589 if (err)
Bryan Schumaker129d1972012-07-16 16:39:13 -04002590 goto out0;
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return 0;
Bryan Schumaker129d1972012-07-16 16:39:13 -04002593out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 nfs_destroy_directcache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002595out1:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002596 nfs_destroy_writepagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002597out2:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002598 nfs_destroy_readpagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002599out3:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002600 nfs_destroy_inodecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002601out4:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002602 nfs_destroy_nfspagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002603out5:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002604 nfs_fs_proc_exit();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002605out6:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002606 nfsiod_stop();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002607out7:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002608 unregister_pernet_subsys(&nfs_net_ops);
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002609out9:
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002610 nfs_sysfs_exit();
2611out10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return err;
2613}
2614
2615static void __exit exit_nfs_fs(void)
2616{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 nfs_destroy_directcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 nfs_destroy_writepagecache();
2619 nfs_destroy_readpagecache();
2620 nfs_destroy_inodecache();
2621 nfs_destroy_nfspagecache();
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002622 unregister_pernet_subsys(&nfs_net_ops);
David Howellsf7b422b2006-06-09 09:34:33 -04002623 unregister_nfs_fs();
David Howells6aaca562006-08-22 20:06:13 -04002624 nfs_fs_proc_exit();
Trond Myklebust57460062008-02-19 20:04:22 -05002625 nfsiod_stop();
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002626 nfs_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629/* Not quite true; I just maintain it */
2630MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
Jeff Johnsond3318992024-06-25 09:42:49 -07002631MODULE_DESCRIPTION("NFS client support");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632MODULE_LICENSE("GPL");
Trond Myklebustf43bf0b2007-10-09 12:01:04 -04002633module_param(enable_ino64, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635module_init(init_nfs_fs)
2636module_exit(exit_nfs_fs)