blob: 522aa10a1a3e7cfe64f07f79786c55bd6a35d84a [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 Filz3fa0b4e2010-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
Benjamin Coddington210c7c12016-01-06 10:40:18 -050075static int nfs_wait_killable(int mode)
Trond Myklebust72cb77f2009-03-11 14:10:30 -040076{
Colin Cross416ad3c2013-05-06 23:50:06 +000077 freezable_schedule_unsafe();
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}
Benjamin Coddington210c7c12016-01-06 10:40:18 -050082
83int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
84{
85 return nfs_wait_killable(mode);
86}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040087EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
Trond Myklebust72cb77f2009-03-11 14:10:30 -040088
89/**
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040090 * nfs_compat_user_ino64 - returns the user-visible inode number
91 * @fileid: 64-bit fileid
92 *
93 * This function returns a 32-bit inode number if the boot parameter
94 * nfs.enable_ino64 is zero.
95 */
96u64 nfs_compat_user_ino64(u64 fileid)
97{
Frank Filz3fa0b4e2010-12-02 19:31:23 +000098#ifdef CONFIG_COMPAT
99 compat_ulong_t ino;
100#else
101 unsigned long ino;
102#endif
Trond Myklebustf43bf0b2007-10-09 12:01:04 -0400103
104 if (enable_ino64)
105 return fileid;
106 ino = fileid;
107 if (sizeof(ino) < sizeof(fileid))
108 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
109 return ino;
110}
111
Trond Myklebusteed99352012-12-14 14:36:36 -0500112int nfs_drop_inode(struct inode *inode)
113{
114 return NFS_STALE(inode) || generic_drop_inode(inode);
115}
116EXPORT_SYMBOL_GPL(nfs_drop_inode);
117
Bryan Schumaker19d87ca2012-07-30 16:05:21 -0400118void nfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Trond Myklebustda6d5032006-06-01 17:26:35 -0400120 /*
121 * The following should never happen...
122 */
Trond Myklebustf48407d2012-10-15 16:19:30 -0400123 WARN_ON_ONCE(nfs_have_writebacks(inode));
124 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
Trond Myklebustada70d92005-06-22 17:16:22 +0000125 nfs_zap_acl_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400126 nfs_access_zap_cache(inode);
David Howellsf1fe29b2013-09-27 11:20:03 +0100127 nfs_fscache_clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400129EXPORT_SYMBOL_GPL(nfs_clear_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Al Virob57922d2010-06-07 14:34:48 -0400131void nfs_evict_inode(struct inode *inode)
132{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700133 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200134 clear_inode(inode);
Al Virob57922d2010-06-07 14:34:48 -0400135 nfs_clear_inode(inode);
136}
137
Trond Myklebust9e1681c2015-03-25 17:23:31 -0400138int nfs_sync_inode(struct inode *inode)
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400139{
Christoph Hellwig95d9f6c2016-03-02 17:35:55 +0100140 inode_dio_wait(inode);
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400141 return nfs_wb_all(inode);
142}
Trond Myklebust9e1681c2015-03-25 17:23:31 -0400143EXPORT_SYMBOL_GPL(nfs_sync_inode);
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400144
Trond Myklebust29884df2005-12-13 16:13:54 -0500145/**
146 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
Trond Myklebust302fad72019-02-18 13:32:38 -0500147 * @mapping: pointer to struct address_space
Trond Myklebust29884df2005-12-13 16:13:54 -0500148 */
149int nfs_sync_mapping(struct address_space *mapping)
150{
Trond Myklebust5cf95212010-02-19 17:03:29 -0800151 int ret = 0;
Trond Myklebust29884df2005-12-13 16:13:54 -0500152
Trond Myklebust5cf95212010-02-19 17:03:29 -0800153 if (mapping->nrpages != 0) {
154 unmap_mapping_range(mapping, 0, 0, 0);
155 ret = nfs_wb_all(mapping->host);
156 }
Trond Myklebust29884df2005-12-13 16:13:54 -0500157 return ret;
158}
159
Trond Myklebust187e5932016-12-16 18:51:15 -0500160static int nfs_attribute_timeout(struct inode *inode)
161{
162 struct nfs_inode *nfsi = NFS_I(inode);
163
164 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
165}
166
Trond Myklebust61540bf2016-12-08 18:18:38 -0500167static bool nfs_check_cache_invalid_delegated(struct inode *inode, unsigned long flags)
168{
169 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
170
171 /* Special case for the pagecache or access cache */
172 if (flags == NFS_INO_REVAL_PAGECACHE &&
173 !(cache_validity & NFS_INO_REVAL_FORCED))
174 return false;
175 return (cache_validity & flags) != 0;
176}
177
178static bool nfs_check_cache_invalid_not_delegated(struct inode *inode, unsigned long flags)
179{
180 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
181
182 if ((cache_validity & flags) != 0)
183 return true;
184 if (nfs_attribute_timeout(inode))
185 return true;
186 return false;
187}
188
189bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags)
190{
191 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
192 return nfs_check_cache_invalid_delegated(inode, flags);
193
194 return nfs_check_cache_invalid_not_delegated(inode, flags);
195}
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000196EXPORT_SYMBOL_GPL(nfs_check_cache_invalid);
Trond Myklebust61540bf2016-12-08 18:18:38 -0500197
Trond Myklebust6edf9602014-06-20 13:11:01 -0400198static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
199{
200 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400201 bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400202
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400203 if (have_delegation) {
204 if (!(flags & NFS_INO_REVAL_FORCED))
205 flags &= ~NFS_INO_INVALID_OTHER;
206 flags &= ~(NFS_INO_INVALID_CHANGE
207 | NFS_INO_INVALID_SIZE
Frank van der Linden0f44da52020-06-23 22:39:00 +0000208 | NFS_INO_REVAL_PAGECACHE
209 | NFS_INO_INVALID_XATTR);
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400210 }
211
Trond Myklebust6edf9602014-06-20 13:11:01 -0400212 if (inode->i_mapping->nrpages == 0)
Trond Myklebust1c341b72019-05-22 08:38:57 -0400213 flags &= ~(NFS_INO_INVALID_DATA|NFS_INO_DATA_INVAL_DEFER);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400214 nfsi->cache_validity |= flags;
215 if (flags & NFS_INO_INVALID_DATA)
216 nfs_fscache_invalidate(inode);
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * Invalidate the local caches
221 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500222static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct nfs_inode *nfsi = NFS_I(inode);
225 int mode = inode->i_mode;
226
Chuck Lever91d5b472006-03-20 13:44:14 -0500227 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
228
Trond Myklebustc7c20972007-09-28 19:22:40 -0400229 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
230 nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
David Howellsde242c02012-12-20 21:52:38 +0000232 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
Trond Myklebust6edf9602014-06-20 13:11:01 -0400233 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
David Quigleyaa9c2662013-05-22 12:50:44 -0400234 | NFS_INO_INVALID_DATA
235 | NFS_INO_INVALID_ACCESS
236 | NFS_INO_INVALID_ACL
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000237 | NFS_INO_INVALID_XATTR
Trond Myklebust6edf9602014-06-20 13:11:01 -0400238 | NFS_INO_REVAL_PAGECACHE);
David Quigleyaa9c2662013-05-22 12:50:44 -0400239 } else
Trond Myklebust6edf9602014-06-20 13:11:01 -0400240 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
David Quigleyaa9c2662013-05-22 12:50:44 -0400241 | NFS_INO_INVALID_ACCESS
242 | NFS_INO_INVALID_ACL
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000243 | NFS_INO_INVALID_XATTR
Trond Myklebust6edf9602014-06-20 13:11:01 -0400244 | NFS_INO_REVAL_PAGECACHE);
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500245 nfs_zap_label_cache_locked(nfsi);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500246}
Chuck Leverdc592502005-08-18 11:24:12 -0700247
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500248void nfs_zap_caches(struct inode *inode)
249{
250 spin_lock(&inode->i_lock);
251 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700252 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000253}
254
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700255void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
256{
257 if (mapping->nrpages != 0) {
258 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400259 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700260 spin_unlock(&inode->i_lock);
261 }
262}
263
Trond Myklebustf41f7412008-06-11 17:39:04 -0400264void nfs_zap_acl_cache(struct inode *inode)
Trond Myklebustada70d92005-06-22 17:16:22 +0000265{
266 void (*clear_acl_cache)(struct inode *);
267
268 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
269 if (clear_acl_cache != NULL)
270 clear_acl_cache(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700271 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700272 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700273 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -0400275EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Trond Myklebustc4812992007-09-28 17:11:45 -0400277void nfs_invalidate_atime(struct inode *inode)
278{
279 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400280 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebustc4812992007-09-28 17:11:45 -0400281 spin_unlock(&inode->i_lock);
282}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400283EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
Trond Myklebustc4812992007-09-28 17:11:45 -0400284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500286 * Invalidate, but do not unhash, the inode.
287 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400289static void nfs_set_inode_stale_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Benny Halevy3a10c302008-01-23 08:58:59 +0200291 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500292 nfs_zap_caches_locked(inode);
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400293 trace_nfs_set_inode_stale(inode);
294}
295
296void nfs_set_inode_stale(struct inode *inode)
297{
298 spin_lock(&inode->i_lock);
299 nfs_set_inode_stale_locked(inode);
300 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303struct nfs_find_desc {
304 struct nfs_fh *fh;
305 struct nfs_fattr *fattr;
306};
307
308/*
309 * In NFSv3 we can have 64bit inode numbers. In order to support
310 * this, and re-exported directories (also seen in NFSv2)
311 * we are forced to allow 2 different inodes to have the same
312 * i_ino.
313 */
314static int
315nfs_find_actor(struct inode *inode, void *opaque)
316{
317 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
318 struct nfs_fh *fh = desc->fh;
319 struct nfs_fattr *fattr = desc->fattr;
320
321 if (NFS_FILEID(inode) != fattr->fileid)
322 return 0;
Jeff Laytonf6488c92013-02-27 20:10:34 -0500323 if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (nfs_compare_fh(NFS_FH(inode), fh))
326 return 0;
327 if (is_bad_inode(inode) || NFS_STALE(inode))
328 return 0;
329 return 1;
330}
331
332static int
333nfs_init_locked(struct inode *inode, void *opaque)
334{
335 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
336 struct nfs_fattr *fattr = desc->fattr;
337
Benny Halevy99fadcd2008-01-23 08:59:08 +0200338 set_nfs_fileid(inode, fattr->fileid);
Trond Myklebust916ec342016-06-17 16:48:28 -0400339 inode->i_mode = fattr->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 nfs_copy_fh(NFS_FH(inode), desc->fh);
341 return 0;
342}
343
Steve Dicksone058f702013-05-22 12:50:40 -0400344#ifdef CONFIG_NFS_V4_SECURITY_LABEL
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500345static void nfs_clear_label_invalid(struct inode *inode)
346{
347 spin_lock(&inode->i_lock);
348 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
349 spin_unlock(&inode->i_lock);
350}
351
David Quigleyaa9c2662013-05-22 12:50:44 -0400352void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
353 struct nfs4_label *label)
354{
355 int error;
356
357 if (label == NULL)
358 return;
359
David Quigleyaa9c2662013-05-22 12:50:44 -0400360 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
361 error = security_inode_notifysecctx(inode, label->label,
362 label->len);
363 if (error)
364 printk(KERN_ERR "%s() %s %d "
365 "security_inode_notifysecctx() %d\n",
366 __func__,
367 (char *)label->label,
368 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{
375 struct nfs4_label *label = NULL;
376 int minor_version = server->nfs_client->cl_minorversion;
377
378 if (minor_version < 2)
379 return label;
380
381 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
382 return label;
383
384 label = kzalloc(sizeof(struct nfs4_label), flags);
385 if (label == NULL)
386 return ERR_PTR(-ENOMEM);
387
388 label->label = kzalloc(NFS4_MAXLABELLEN, flags);
389 if (label->label == NULL) {
390 kfree(label);
391 return ERR_PTR(-ENOMEM);
392 }
393 label->len = NFS4_MAXLABELLEN;
394
395 return label;
396}
397EXPORT_SYMBOL_GPL(nfs4_label_alloc);
David Quigleyaa9c2662013-05-22 12:50:44 -0400398#else
Trond Myklebust829e57d2013-11-18 14:33:50 -0500399void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -0400400 struct nfs4_label *label)
401{
402}
Steve Dicksone058f702013-05-22 12:50:40 -0400403#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400404EXPORT_SYMBOL_GPL(nfs_setsecurity);
Steve Dicksone058f702013-05-22 12:50:40 -0400405
Peng Taof174ff72017-06-29 06:34:51 -0700406/* Search for inode identified by fh, fileid and i_mode in inode cache. */
407struct inode *
408nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
409{
410 struct nfs_find_desc desc = {
411 .fh = fh,
412 .fattr = fattr,
413 };
414 struct inode *inode;
415 unsigned long hash;
416
417 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
418 !(fattr->valid & NFS_ATTR_FATTR_TYPE))
419 return NULL;
420
421 hash = nfs_fattr_to_ino_t(fattr);
422 inode = ilookup5(sb, hash, nfs_find_actor, &desc);
423
424 dprintk("%s: returning %p\n", __func__, inode);
425 return inode;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
429 * This is our front-end to iget that looks up inodes by file handle
430 * instead of inode number.
431 */
432struct inode *
David Quigley1775fd32013-05-22 12:50:42 -0400433nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct nfs_find_desc desc = {
436 .fh = fh,
437 .fattr = fattr
438 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500439 struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned long hash;
441
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000442 nfs_attr_check_mountpoint(sb, fattr);
443
Anna Schumaker2ef47eb2014-12-09 16:19:16 -0500444 if (nfs_attr_use_mounted_on_fileid(fattr))
445 fattr->fileid = fattr->mounted_on_fileid;
446 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto out_no_inode;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400448 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto out_no_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 hash = nfs_fattr_to_ino_t(fattr);
452
Trond Myklebust03f28e32006-03-20 13:44:48 -0500453 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
454 if (inode == NULL) {
455 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (inode->i_state & I_NEW) {
460 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800461 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* We set i_ino for the few things that still rely on it,
464 * such as stat(2) */
465 inode->i_ino = hash;
466
467 /* We can't support update_atime(), since the server will reset it */
468 inode->i_flags |= S_NOATIME|S_NOCMTIME;
469 inode->i_mode = fattr->mode;
Trond Myklebust821a8682018-03-27 18:30:42 -0400470 nfsi->cache_validity = 0;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400471 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
472 && nfs_server_capable(inode, NFS_CAP_MODE))
Trond Myklebust16e14372018-03-20 16:53:31 -0400473 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* Why so? Because we want revalidate for devices/FIFOs, and
475 * that's precisely what we have in nfs_file_inode_operations.
476 */
David Howells8fa5c002006-08-22 20:06:12 -0400477 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (S_ISREG(inode->i_mode)) {
Jeff Layton1788ea62011-11-04 13:31:21 -0400479 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 inode->i_data.a_ops = &nfs_file_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else if (S_ISDIR(inode->i_mode)) {
David Howells8fa5c002006-08-22 20:06:12 -0400482 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 inode->i_fop = &nfs_dir_operations;
Trond Myklebust11de3b12010-12-01 14:17:06 -0500484 inode->i_data.a_ops = &nfs_dir_aops;
Trond Myklebust55a97592006-06-09 09:34:19 -0400485 /* Deal with crossing mountpoints */
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000486 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
487 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400488 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
489 inode->i_op = &nfs_referral_inode_operations;
490 else
491 inode->i_op = &nfs_mountpoint_inode_operations;
Trond Myklebust55a97592006-06-09 09:34:19 -0400492 inode->i_fop = NULL;
David Howells36d43a42011-01-14 18:45:42 +0000493 inode->i_flags |= S_AUTOMOUNT;
Trond Myklebust55a97592006-06-09 09:34:19 -0400494 }
Al Viro21fc61c2015-11-17 01:07:57 -0500495 } else if (S_ISLNK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 inode->i_op = &nfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500497 inode_nohighmem(inode);
498 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 init_special_inode(inode, inode->i_mode, fattr->rdev);
500
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400501 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
502 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
503 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
Jeff Layton1eb5d982018-01-09 08:21:17 -0500504 inode_set_iversion_raw(inode, 0);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400505 inode->i_size = 0;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200506 clear_nlink(inode);
Eric W. Biederman9ff593c2013-02-01 14:26:23 -0800507 inode->i_uid = make_kuid(&init_user_ns, -2);
508 inode->i_gid = make_kgid(&init_user_ns, -2);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400509 inode->i_blocks = 0;
510 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
Andy Adamson2701d082012-05-24 13:13:24 -0400511 nfsi->write_io = 0;
512 nfsi->read_io = 0;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400513
Trond Myklebust33801142005-10-27 22:12:39 -0400514 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400515 nfsi->attr_gencount = fattr->gencount;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400516 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400517 inode->i_atime = fattr->atime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400518 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400519 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400520 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400521 inode->i_mtime = fattr->mtime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400522 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400523 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400524 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400525 inode->i_ctime = fattr->ctime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400526 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400527 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400528 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Jeff Layton1eb5d982018-01-09 08:21:17 -0500529 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebustcd812592015-07-05 11:12:07 -0400530 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400531 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400532 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
533 inode->i_size = nfs_size_to_loff_t(fattr->size);
Trond Myklebust62ab4602009-08-09 15:06:19 -0400534 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400535 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400536 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200537 set_nlink(inode, fattr->nlink);
Trond Myklebust62ab4602009-08-09 15:06:19 -0400538 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
Trond Myklebust16e14372018-03-20 16:53:31 -0400539 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400540 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
541 inode->i_uid = fattr->uid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400542 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
Trond Myklebust16e14372018-03-20 16:53:31 -0400543 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400544 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
545 inode->i_gid = fattr->gid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400546 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
Trond Myklebust16e14372018-03-20 16:53:31 -0400547 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Frank van der Linden0f44da52020-06-23 22:39:00 +0000548 if (nfs_server_capable(inode, NFS_CAP_XATTR))
549 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400550 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
551 inode->i_blocks = fattr->du.nfs2.blocks;
552 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /*
554 * report the blocks in 512byte units
555 */
556 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
David Quigleyaa9c2662013-05-22 12:50:44 -0400558
Trond Myklebust821a8682018-03-27 18:30:42 -0400559 if (nfsi->cache_validity != 0)
560 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
561
David Quigleyaa9c2662013-05-22 12:50:44 -0400562 nfs_setsecurity(inode, fattr, label);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800565 nfsi->attrtimeo_timestamp = now;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400566 nfsi->access_cache = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
David Howellsf1fe29b2013-09-27 11:20:03 +0100568 nfs_fscache_init_inode(inode);
David Howellsef79c092009-04-03 16:42:43 +0100569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unlock_new_inode(inode);
NeilBrown26fde4d2017-07-03 15:27:26 +1000571 } else {
572 int err = nfs_refresh_inode(inode, fattr);
573 if (err < 0) {
574 iput(inode);
575 inode = ERR_PTR(err);
576 goto out_no_inode;
577 }
578 }
Niels de Vos1e8968c2013-12-17 18:20:16 +0100579 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100581 (unsigned long long)NFS_FILEID(inode),
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -0500582 nfs_display_fhandle_hash(fh),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 atomic_read(&inode->i_count));
584
585out:
586 return inode;
587
588out_no_inode:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500589 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto out;
591}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400592EXPORT_SYMBOL_GPL(nfs_fhget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Trond Myklebust536e43d2012-01-17 22:04:26 -0500594#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 -0700595
596int
597nfs_setattr(struct dentry *dentry, struct iattr *attr)
598{
David Howells2b0143b2015-03-17 22:25:59 +0000599 struct inode *inode = d_inode(dentry);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400600 struct nfs_fattr *fattr;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800601 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Chuck Lever91d5b472006-03-20 13:44:14 -0500603 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
604
Jeff Layton188b95d2007-10-18 03:05:21 -0700605 /* skip mode change if it's just for clearing setuid/setgid */
606 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
607 attr->ia_valid &= ~ATTR_MODE;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig08a899d2014-09-07 08:36:40 -0700610 BUG_ON(!S_ISREG(inode->i_mode));
611
Kinglong Meeae57ca02015-08-26 21:10:55 +0800612 error = inode_newsize_ok(inode, attr->ia_size);
613 if (error)
614 return error;
615
616 if (attr->ia_size == i_size_read(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 attr->ia_valid &= ~ATTR_SIZE;
618 }
619
620 /* Optimization: if the end result is no change, don't RPC */
621 attr->ia_valid &= NFS_VALID_ATTRS;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500622 if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400625 trace_nfs_setattr_enter(inode);
626
Trond Myklebust755c1e22006-03-20 13:44:06 -0500627 /* Write all dirty data */
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400628 if (S_ISREG(inode->i_mode))
629 nfs_sync_inode(inode);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400630
631 fattr = nfs_alloc_fattr();
Kinglong Meeae57ca02015-08-26 21:10:55 +0800632 if (fattr == NULL) {
633 error = -ENOMEM;
Trond Myklebust987f8df2010-04-16 16:22:52 -0400634 goto out;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800635 }
636
Trond Myklebust987f8df2010-04-16 16:22:52 -0400637 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400638 if (error == 0)
David Quigleyaa9c2662013-05-22 12:50:44 -0400639 error = nfs_refresh_inode(inode, fattr);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400640 nfs_free_fattr(fattr);
641out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400642 trace_nfs_setattr_exit(inode, error);
Trond Myklebust65e43082005-08-16 11:49:44 -0400643 return error;
644}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400645EXPORT_SYMBOL_GPL(nfs_setattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400646
647/**
Trond Myklebusta3d01452008-06-11 12:21:19 -0400648 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
649 * @inode: inode of the file used
650 * @offset: file offset to start truncating
651 *
652 * This is a copy of the common vmtruncate, but with the locking
653 * corrected to take into account the fact that NFS requires
654 * inode->i_size to be updated under the inode->i_lock.
Trond Myklebustf0446362015-02-26 16:09:04 -0500655 * Note: must be called with inode->i_lock held!
Trond Myklebusta3d01452008-06-11 12:21:19 -0400656 */
657static int nfs_vmtruncate(struct inode * inode, loff_t offset)
658{
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000659 int err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400660
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000661 err = inode_newsize_ok(inode, offset);
662 if (err)
663 goto out;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400664
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000665 i_size_write(inode, offset);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400666 /* Optimisation */
667 if (offset == 0)
Trond Myklebust1c341b72019-05-22 08:38:57 -0400668 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA |
669 NFS_INO_DATA_INVAL_DEFER);
Trond Myklebustf6cdfa62018-03-27 17:10:42 -0400670 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400671
Trond Myklebustf0446362015-02-26 16:09:04 -0500672 spin_unlock(&inode->i_lock);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700673 truncate_pagecache(inode, offset);
Trond Myklebustf0446362015-02-26 16:09:04 -0500674 spin_lock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000675out:
676 return err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400677}
678
679/**
Trond Myklebust65e43082005-08-16 11:49:44 -0400680 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
681 * @inode: pointer to struct inode
682 * @attr: pointer to struct iattr
Trond Myklebust16e14372018-03-20 16:53:31 -0400683 * @fattr: pointer to struct nfs_fattr
Trond Myklebust65e43082005-08-16 11:49:44 -0400684 *
685 * Note: we do this in the *proc.c in order to ensure that
686 * it works for things like exclusive creates too.
687 */
Trond Myklebustf0446362015-02-26 16:09:04 -0500688void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
689 struct nfs_fattr *fattr)
Trond Myklebust65e43082005-08-16 11:49:44 -0400690{
Trond Myklebustf0446362015-02-26 16:09:04 -0500691 /* Barrier: bump the attribute generation count. */
692 nfs_fattr_set_barrier(fattr);
693
694 spin_lock(&inode->i_lock);
695 NFS_I(inode)->attr_gencount = fattr->gencount;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400696 if ((attr->ia_valid & ATTR_SIZE) != 0) {
697 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
698 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
699 nfs_vmtruncate(inode, attr->ia_size);
700 }
Trond Myklebust65e43082005-08-16 11:49:44 -0400701 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
Trond Myklebust6a97d022018-04-08 17:51:11 -0400702 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400704 int mode = attr->ia_mode & S_IALLUGO;
705 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 inode->i_mode = mode;
707 }
708 if ((attr->ia_valid & ATTR_UID) != 0)
709 inode->i_uid = attr->ia_uid;
710 if ((attr->ia_valid & ATTR_GID) != 0)
711 inode->i_gid = attr->ia_gid;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400712 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400713 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400714 else
715 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
716 | NFS_INO_INVALID_CTIME);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400717 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
718 | NFS_INO_INVALID_ACL);
Trond Myklebust65e43082005-08-16 11:49:44 -0400719 }
Trond Myklebust6a97d022018-04-08 17:51:11 -0400720 if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
721 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
722 | NFS_INO_INVALID_CTIME);
723 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400724 inode->i_atime = fattr->atime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400725 else if (attr->ia_valid & ATTR_ATIME_SET)
726 inode->i_atime = attr->ia_atime;
727 else
728 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
729
730 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400731 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400732 else
733 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
734 | NFS_INO_INVALID_CTIME);
735 }
736 if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
737 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
738 | NFS_INO_INVALID_CTIME);
739 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400740 inode->i_mtime = fattr->mtime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400741 else if (attr->ia_valid & ATTR_MTIME_SET)
742 inode->i_mtime = attr->ia_mtime;
743 else
744 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
745
746 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400747 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400748 else
749 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
750 | NFS_INO_INVALID_CTIME);
Trond Myklebust65e43082005-08-16 11:49:44 -0400751 }
Jeff Layton616c3192015-11-25 13:50:45 -0500752 if (fattr->valid)
753 nfs_update_inode(inode, fattr);
Trond Myklebustf0446362015-02-26 16:09:04 -0500754 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400756EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500758static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
Trond Myklebust311324a2014-02-07 17:02:08 -0500759{
760 struct dentry *parent;
761
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500762 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
763 return;
Trond Myklebust311324a2014-02-07 17:02:08 -0500764 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000765 nfs_force_use_readdirplus(d_inode(parent));
Trond Myklebust311324a2014-02-07 17:02:08 -0500766 dput(parent);
767}
768
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500769static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
770{
771 struct dentry *parent;
772
773 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
774 return;
775 parent = dget_parent(dentry);
776 nfs_advise_use_readdirplus(d_inode(parent));
777 dput(parent);
778}
779
Trond Myklebust311324a2014-02-07 17:02:08 -0500780static bool nfs_need_revalidate_inode(struct inode *inode)
781{
782 if (NFS_I(inode)->cache_validity &
783 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
784 return true;
785 if (nfs_attribute_cache_expired(inode))
786 return true;
787 return false;
788}
789
David Howellsa528d352017-01-31 16:46:22 +0000790int nfs_getattr(const struct path *path, struct kstat *stat,
791 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
David Howellsa528d352017-01-31 16:46:22 +0000793 struct inode *inode = d_inode(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500794 struct nfs_server *server = NFS_SERVER(inode);
795 unsigned long cache_validity;
Jan Kara16caf5b2014-10-23 14:02:47 +0200796 int err = 0;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500797 bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
798 bool do_update = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400800 trace_nfs_getattr_enter(inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500801
Trond Myklebustac7cbb22020-06-04 16:20:06 -0400802 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
803 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500804 goto out_no_update;
Trond Myklebustac7cbb22020-06-04 16:20:06 -0400805 }
Trond Myklebust9ccee942018-01-04 17:46:09 -0500806
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800807 /* Flush out writes to the server in order to update c/mtime. */
Trond Myklebust9ccee942018-01-04 17:46:09 -0500808 if ((request_mask & (STATX_CTIME|STATX_MTIME)) &&
809 S_ISREG(inode->i_mode)) {
Trond Myklebust79566ef2016-06-25 17:45:40 -0400810 err = filemap_write_and_wait(inode->i_mapping);
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800811 if (err)
812 goto out;
Chuck Lever28c494c2007-10-26 13:32:13 -0400813 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800814
815 /*
816 * We may force a getattr if the user cares about atime.
817 *
818 * Note that we only have to check the vfsmount flags here:
819 * - NFS always sets S_NOATIME by so checking it would give a
820 * bogus result
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800821 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800822 * no point in checking those.
823 */
David Howellsa528d352017-01-31 16:46:22 +0000824 if ((path->mnt->mnt_flags & MNT_NOATIME) ||
825 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Trond Myklebust9ccee942018-01-04 17:46:09 -0500826 request_mask &= ~STATX_ATIME;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800827
Trond Myklebust9ccee942018-01-04 17:46:09 -0500828 /* Is the user requesting attributes that might need revalidation? */
829 if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
830 STATX_MTIME|STATX_UID|STATX_GID|
831 STATX_SIZE|STATX_BLOCKS)))
832 goto out_no_revalidate;
Trond Myklebust311324a2014-02-07 17:02:08 -0500833
Trond Myklebust9ccee942018-01-04 17:46:09 -0500834 /* Check whether the cached attributes are stale */
835 do_update |= force_sync || nfs_attribute_cache_expired(inode);
836 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
837 do_update |= cache_validity &
838 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
839 if (request_mask & STATX_ATIME)
840 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
841 if (request_mask & (STATX_CTIME|STATX_MTIME))
842 do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
Zheng Bin3a39e772020-05-21 17:17:21 +0800843 if (request_mask & STATX_BLOCKS)
844 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500845 if (do_update) {
846 /* Update the attribute cache */
Hou Tao59b86d82017-04-28 18:35:19 +0800847 if (!(server->flags & NFS_MOUNT_NOAC))
848 nfs_readdirplus_parent_cache_miss(path->dentry);
849 else
850 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust311324a2014-02-07 17:02:08 -0500851 err = __nfs_revalidate_inode(server, inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500852 if (err)
853 goto out;
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500854 } else
David Howellsa528d352017-01-31 16:46:22 +0000855 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500856out_no_revalidate:
857 /* Only return attributes that were revalidated. */
858 stat->result_mask &= request_mask;
859out_no_update:
860 generic_fillattr(inode, stat);
861 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
862 if (S_ISDIR(inode->i_mode))
863 stat->blksize = NFS_SERVER(inode)->dtsize;
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800864out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400865 trace_nfs_getattr_exit(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return err;
867}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400868EXPORT_SYMBOL_GPL(nfs_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400870static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
871{
Elena Reshetova2f62b5a2017-10-20 12:53:37 +0300872 refcount_set(&l_ctx->count, 1);
NeilBrownd51fdb82016-10-13 15:26:47 +1100873 l_ctx->lockowner = current->files;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400874 INIT_LIST_HEAD(&l_ctx->list);
Benjamin Coddington210c7c12016-01-06 10:40:18 -0500875 atomic_set(&l_ctx->io_count, 0);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400876}
877
878static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
879{
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400880 struct nfs_lock_context *pos;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400881
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400882 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
NeilBrownd51fdb82016-10-13 15:26:47 +1100883 if (pos->lockowner != current->files)
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400884 continue;
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400885 if (refcount_inc_not_zero(&pos->count))
886 return pos;
887 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400888 return NULL;
889}
890
891struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
892{
893 struct nfs_lock_context *res, *new = NULL;
David Howells2b0143b2015-03-17 22:25:59 +0000894 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400895
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400896 rcu_read_lock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400897 res = __nfs_find_lock_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400898 rcu_read_unlock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400899 if (res == NULL) {
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400900 new = kmalloc(sizeof(*new), GFP_KERNEL);
901 if (new == NULL)
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400902 return ERR_PTR(-ENOMEM);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400903 nfs_init_lock_context(new);
904 spin_lock(&inode->i_lock);
905 res = __nfs_find_lock_context(ctx);
906 if (res == NULL) {
Trond Myklebust15494512019-04-07 13:59:10 -0400907 new->open_context = get_nfs_open_context(ctx);
908 if (new->open_context) {
909 list_add_tail_rcu(&new->list,
910 &ctx->lock_context.list);
911 res = new;
912 new = NULL;
913 } else
914 res = ERR_PTR(-EBADF);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400915 }
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400916 spin_unlock(&inode->i_lock);
917 kfree(new);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400918 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400919 return res;
920}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400921EXPORT_SYMBOL_GPL(nfs_get_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400922
923void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
924{
925 struct nfs_open_context *ctx = l_ctx->open_context;
David Howells2b0143b2015-03-17 22:25:59 +0000926 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400927
Elena Reshetova2f62b5a2017-10-20 12:53:37 +0300928 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400929 return;
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400930 list_del_rcu(&l_ctx->list);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400931 spin_unlock(&inode->i_lock);
Trond Myklebust15494512019-04-07 13:59:10 -0400932 put_nfs_open_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400933 kfree_rcu(l_ctx, rcu_head);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400934}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400935EXPORT_SYMBOL_GPL(nfs_put_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400936
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400937/**
938 * nfs_close_context - Common close_context() routine NFSv2/v3
939 * @ctx: pointer to context
940 * @is_sync: is this a synchronous close
941 *
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400942 * Ensure that the attributes are up to date if we're mounted
943 * with close-to-open semantics and we have cached data that will
944 * need to be revalidated on open.
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400945 */
946void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
947{
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400948 struct nfs_inode *nfsi;
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400949 struct inode *inode;
950 struct nfs_server *server;
951
952 if (!(ctx->mode & FMODE_WRITE))
953 return;
954 if (!is_sync)
955 return;
David Howells2b0143b2015-03-17 22:25:59 +0000956 inode = d_inode(ctx->dentry);
Trond Myklebust58ff4182016-12-16 17:39:58 -0500957 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
958 return;
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400959 nfsi = NFS_I(inode);
960 if (inode->i_mapping->nrpages == 0)
961 return;
962 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
963 return;
964 if (!list_empty(&nfsi->open_files))
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400965 return;
966 server = NFS_SERVER(inode);
967 if (server->flags & NFS_MOUNT_NOCTO)
968 return;
969 nfs_revalidate_inode(server, inode);
970}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400971EXPORT_SYMBOL_GPL(nfs_close_context);
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400972
NeilBrown532d4de2016-10-13 15:26:47 +1100973struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
974 fmode_t f_mode,
975 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct nfs_open_context *ctx;
978
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700979 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Trond Myklebust1d179d62020-02-07 19:32:49 -0500980 if (!ctx)
Al Viro5ede7b12011-10-23 18:49:54 -0400981 return ERR_PTR(-ENOMEM);
Al Viro5ede7b12011-10-23 18:49:54 -0400982 nfs_sb_active(dentry->d_sb);
983 ctx->dentry = dget(dentry);
Trond Myklebust1d179d62020-02-07 19:32:49 -0500984 if (filp)
985 ctx->cred = get_cred(filp->f_cred);
986 else
987 ctx->cred = get_current_cred();
NeilBrownddf529e2018-12-03 11:30:30 +1100988 ctx->ll_cred = NULL;
Al Viro5ede7b12011-10-23 18:49:54 -0400989 ctx->state = NULL;
990 ctx->mode = f_mode;
991 ctx->flags = 0;
992 ctx->error = 0;
NeilBrown532d4de2016-10-13 15:26:47 +1100993 ctx->flock_owner = (fl_owner_t)filp;
Al Viro5ede7b12011-10-23 18:49:54 -0400994 nfs_init_lock_context(&ctx->lock_context);
995 ctx->lock_context.open_context = ctx;
996 INIT_LIST_HEAD(&ctx->list);
Andy Adamson82be4172012-05-23 05:02:35 -0400997 ctx->mdsthreshold = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return ctx;
999}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001000EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1003{
Trond Myklebust0de43972018-09-02 15:57:01 -04001004 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
1005 return ctx;
1006 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001008EXPORT_SYMBOL_GPL(get_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001010static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001011{
David Howells2b0143b2015-03-17 22:25:59 +00001012 struct inode *inode = d_inode(ctx->dentry);
Al Viro3d4ff432011-06-22 18:40:12 -04001013 struct super_block *sb = ctx->dentry->d_sb;
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001014
Trond Myklebust0de43972018-09-02 15:57:01 -04001015 if (!refcount_dec_and_test(&ctx->lock_context.count))
Trond Myklebust5e119342007-07-26 12:06:17 -04001016 return;
Trond Myklebust0de43972018-09-02 15:57:01 -04001017 if (!list_empty(&ctx->list)) {
1018 spin_lock(&inode->i_lock);
1019 list_del_rcu(&ctx->list);
1020 spin_unlock(&inode->i_lock);
1021 }
Trond Myklebust5c78f582010-09-27 15:51:20 -04001022 if (inode != NULL)
1023 NFS_PROTO(inode)->close_context(ctx, is_sync);
NeilBrowna52458b2018-12-03 11:30:31 +11001024 put_cred(ctx->cred);
Al Viro3d4ff432011-06-22 18:40:12 -04001025 dput(ctx->dentry);
Trond Myklebust322b2b92013-01-11 16:39:51 -05001026 nfs_sb_deactive(sb);
NeilBrownddf529e2018-12-03 11:30:30 +11001027 put_rpccred(ctx->ll_cred);
Andy Adamson82be4172012-05-23 05:02:35 -04001028 kfree(ctx->mdsthreshold);
Trond Myklebust0de43972018-09-02 15:57:01 -04001029 kfree_rcu(ctx, rcu_head);
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001030}
1031
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001032void put_nfs_open_context(struct nfs_open_context *ctx)
1033{
1034 __put_nfs_open_context(ctx, 0);
1035}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001036EXPORT_SYMBOL_GPL(put_nfs_open_context);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001037
Trond Myklebust4eae5012015-09-04 15:17:53 -04001038static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1039{
1040 __put_nfs_open_context(ctx, 1);
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * Ensure that mmap has a recent RPC credential for use when writing out
1045 * shared pages
1046 */
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001047void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
David Howells2b0143b2015-03-17 22:25:59 +00001049 struct inode *inode = d_inode(ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct nfs_inode *nfsi = NFS_I(inode);
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_lock(&inode->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -04001053 if (list_empty(&nfsi->open_files) &&
1054 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
1055 nfsi->cache_validity |= NFS_INO_INVALID_DATA |
1056 NFS_INO_REVAL_FORCED;
Trond Myklebust0de43972018-09-02 15:57:01 -04001057 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 spin_unlock(&inode->i_lock);
1059}
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001060EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1061
1062void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1063{
1064 filp->private_data = get_nfs_open_context(ctx);
1065 if (list_empty(&ctx->list))
1066 nfs_inode_attach_open_context(ctx);
1067}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001068EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Trond Myklebustd5308382005-11-04 15:33:38 -05001070/*
1071 * Given an inode, search for an open context with the desired characteristics
1072 */
NeilBrowna52458b2018-12-03 11:30:31 +11001073struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct nfs_inode *nfsi = NFS_I(inode);
1076 struct nfs_open_context *pos, *ctx = NULL;
1077
Trond Myklebust0de43972018-09-02 15:57:01 -04001078 rcu_read_lock();
1079 list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
Trond Myklebust65f51602020-01-26 17:31:15 -05001080 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
Trond Myklebustd5308382005-11-04 15:33:38 -05001081 continue;
Trond Myklebust1544fa02010-03-25 13:54:49 -04001082 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1083 continue;
1084 ctx = get_nfs_open_context(pos);
Trond Myklebust0de43972018-09-02 15:57:01 -04001085 if (ctx)
1086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001088 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return ctx;
1090}
1091
Anna Schumakeraff8d8d2015-07-13 14:01:33 -04001092void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001094 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (ctx) {
David Howells2b0143b2015-03-17 22:25:59 +00001097 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001098
Peng Tao0bcbf032015-12-05 15:57:31 +08001099 /*
1100 * We fatal error on write before. Try to writeback
1101 * every page again.
1102 */
1103 if (ctx->error < 0)
1104 invalidate_inode_pages2(inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 filp->private_data = NULL;
Trond Myklebust4eae5012015-09-04 15:17:53 -04001106 put_nfs_open_context_sync(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108}
1109
1110/*
1111 * These allocate and release file read/write context information.
1112 */
1113int nfs_open(struct inode *inode, struct file *filp)
1114{
1115 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
NeilBrown532d4de2016-10-13 15:26:47 +11001117 ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
Al Viro5ede7b12011-10-23 18:49:54 -04001118 if (IS_ERR(ctx))
1119 return PTR_ERR(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 nfs_file_set_open_context(filp, ctx);
1121 put_nfs_open_context(ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +01001122 nfs_fscache_open_file(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124}
Trond Myklebust44942b42019-06-27 06:41:45 -04001125EXPORT_SYMBOL_GPL(nfs_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * This function is called whenever some part of NFS notices that
1129 * the cached attributes have to be refreshed.
1130 */
1131int
1132__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1133{
1134 int status = -ESTALE;
David Quigley1775fd32013-05-22 12:50:42 -04001135 struct nfs4_label *label = NULL;
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001136 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Niels de Vos1e8968c2013-12-17 18:20:16 +01001139 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1140 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001142 trace_nfs_revalidate_inode_enter(inode);
1143
Chuck Lever85233a72006-10-19 23:28:42 -07001144 if (is_bad_inode(inode))
Chuck Lever412d5822005-08-18 11:24:11 -07001145 goto out;
Trond Myklebust7fdc49c2007-09-28 19:11:33 -04001146 if (NFS_STALE(inode))
1147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Trond Myklebustac46bd32016-07-05 13:46:53 -04001149 /* pNFS: Attributes aren't updated until we layoutcommit */
1150 if (S_ISREG(inode->i_mode)) {
1151 status = pnfs_sync_inode(inode, false);
1152 if (status)
1153 goto out;
1154 }
1155
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001156 status = -ENOMEM;
1157 fattr = nfs_alloc_fattr();
1158 if (fattr == NULL)
1159 goto out;
1160
Trond Myklebust691beb12008-10-05 14:48:22 -04001161 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
David Quigley14c43f72013-05-22 12:50:43 -04001162
1163 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1164 if (IS_ERR(label)) {
1165 status = PTR_ERR(label);
1166 goto out;
1167 }
1168
Trond Myklebusta841b542018-04-07 13:50:59 -04001169 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
1170 label, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (status != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001172 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001174 (unsigned long long)NFS_FILEID(inode), status);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001175 switch (status) {
1176 case -ETIMEDOUT:
1177 /* A soft timeout occurred. Use cached information? */
1178 if (server->flags & NFS_MOUNT_SOFTREVAL)
1179 status = 0;
1180 break;
1181 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04001183 nfs_set_inode_stale(inode);
1184 else
1185 nfs_zap_caches(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
David Quigley14c43f72013-05-22 12:50:43 -04001187 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001190 status = nfs_refresh_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (status) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001192 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001194 (unsigned long long)NFS_FILEID(inode), status);
David Quigley14c43f72013-05-22 12:50:43 -04001195 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Chuck Lever55296802005-08-18 11:24:09 -07001197
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001198 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +00001199 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -07001200
Jeff Layton3da580a2013-11-02 06:57:18 -04001201 nfs_setsecurity(inode, fattr, label);
1202
Niels de Vos1e8968c2013-12-17 18:20:16 +01001203 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001205 (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
David Quigley14c43f72013-05-22 12:50:43 -04001207err_out:
1208 nfs4_label_free(label);
1209out:
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001210 nfs_free_fattr(fattr);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001211 trace_nfs_revalidate_inode_exit(inode, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return status;
1213}
1214
Scott Mayhew43f291c2013-07-05 17:49:30 -04001215int nfs_attribute_cache_expired(struct inode *inode)
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001216{
Trond Myklebustb4d23142010-03-10 15:21:44 -05001217 if (nfs_have_delegated_attributes(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return 0;
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001219 return nfs_attribute_timeout(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222/**
1223 * nfs_revalidate_inode - Revalidate the inode attributes
Trond Myklebust302fad72019-02-18 13:32:38 -05001224 * @server: pointer to nfs_server struct
1225 * @inode: pointer to inode struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 *
1227 * Updates inode attribute information by retrieving the data from the server.
1228 */
1229int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1230{
Trond Myklebust311324a2014-02-07 17:02:08 -05001231 if (!nfs_need_revalidate_inode(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return NFS_STALE(inode) ? -ESTALE : 0;
1233 return __nfs_revalidate_inode(server, inode);
1234}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04001235EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Trond Myklebust1cda7072010-02-19 17:03:30 -08001237static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust717d44e2007-01-24 11:54:55 -08001238{
Trond Myklebustf8806c82013-08-05 13:26:31 -04001239 int ret;
1240
Trond Myklebust717d44e2007-01-24 11:54:55 -08001241 if (mapping->nrpages != 0) {
Trond Myklebustf8806c82013-08-05 13:26:31 -04001242 if (S_ISREG(inode->i_mode)) {
1243 ret = nfs_sync_mapping(mapping);
1244 if (ret < 0)
1245 return ret;
1246 }
1247 ret = invalidate_inode_pages2(mapping);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001248 if (ret < 0)
1249 return ret;
1250 }
Trond Myklebust717d44e2007-01-24 11:54:55 -08001251 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
David Howellsde242c02012-12-20 21:52:38 +00001252 nfs_fscache_wait_on_invalidate(inode);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001253
Niels de Vos1e8968c2013-12-17 18:20:16 +01001254 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1255 inode->i_sb->s_id,
1256 (unsigned long long)NFS_FILEID(inode));
Trond Myklebust717d44e2007-01-24 11:54:55 -08001257 return 0;
1258}
1259
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001260bool nfs_mapping_need_revalidate_inode(struct inode *inode)
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001261{
Trond Myklebust61540bf2016-12-08 18:18:38 -05001262 return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
1263 NFS_STALE(inode);
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001264}
1265
Al Viro0d0def42015-11-17 21:14:24 -05001266int nfs_revalidate_mapping_rcu(struct inode *inode)
1267{
1268 struct nfs_inode *nfsi = NFS_I(inode);
1269 unsigned long *bitlock = &nfsi->flags;
1270 int ret = 0;
1271
1272 if (IS_SWAPFILE(inode))
1273 goto out;
1274 if (nfs_mapping_need_revalidate_inode(inode)) {
1275 ret = -ECHILD;
1276 goto out;
1277 }
1278 spin_lock(&inode->i_lock);
1279 if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1280 (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1281 ret = -ECHILD;
1282 spin_unlock(&inode->i_lock);
1283out:
1284 return ret;
1285}
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/**
Trond Myklebustbe527492016-06-22 08:19:36 -04001288 * nfs_revalidate_mapping - Revalidate the pagecache
Trond Myklebust302fad72019-02-18 13:32:38 -05001289 * @inode: pointer to host inode
1290 * @mapping: pointer to mapping
Trond Myklebust7d52e862005-06-22 17:16:30 +00001291 */
Trond Myklebustbe527492016-06-22 08:19:36 -04001292int nfs_revalidate_mapping(struct inode *inode,
1293 struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +00001294{
1295 struct nfs_inode *nfsi = NFS_I(inode);
Jeff Laytond529ef82014-01-27 13:46:15 -05001296 unsigned long *bitlock = &nfsi->flags;
Trond Myklebust44b11872006-05-25 01:40:59 -04001297 int ret = 0;
1298
Mel Gorman29418aa2012-07-31 16:45:10 -07001299 /* swapfiles are not supposed to be shared. */
1300 if (IS_SWAPFILE(inode))
1301 goto out;
1302
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001303 if (nfs_mapping_need_revalidate_inode(inode)) {
Trond Myklebust44b11872006-05-25 01:40:59 -04001304 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001305 if (ret < 0)
1306 goto out;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001307 }
Jeff Laytond529ef82014-01-27 13:46:15 -05001308
1309 /*
1310 * We must clear NFS_INO_INVALID_DATA first to ensure that
1311 * invalidations that come in while we're shooting down the mappings
1312 * are respected. But, that leaves a race window where one revalidator
1313 * can clear the flag, and then another checks it before the mapping
1314 * gets invalidated. Fix that by serializing access to this part of
1315 * the function.
1316 *
1317 * At the same time, we need to allow other tasks to see whether we
1318 * might be in the middle of invalidating the pages, so we only set
1319 * the bit lock here if it looks like we're going to be doing that.
1320 */
1321 for (;;) {
NeilBrown74316202014-07-07 15:16:04 +10001322 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1323 nfs_wait_bit_killable, TASK_KILLABLE);
Jeff Laytond529ef82014-01-27 13:46:15 -05001324 if (ret)
1325 goto out;
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001326 spin_lock(&inode->i_lock);
1327 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1328 spin_unlock(&inode->i_lock);
1329 continue;
1330 }
1331 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
Jeff Laytond529ef82014-01-27 13:46:15 -05001332 break;
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001333 spin_unlock(&inode->i_lock);
1334 goto out;
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001335 }
1336
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001337 set_bit(NFS_INO_INVALIDATING, bitlock);
Jeff Layton4db72b42014-01-28 13:47:46 -05001338 smp_wmb();
Trond Myklebust1c341b72019-05-22 08:38:57 -04001339 nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA|
1340 NFS_INO_DATA_INVAL_DEFER);
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001341 spin_unlock(&inode->i_lock);
1342 trace_nfs_invalidate_mapping_enter(inode);
Trond Myklebustbe527492016-06-22 08:19:36 -04001343 ret = nfs_invalidate_mapping(inode, mapping);
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001344 trace_nfs_invalidate_mapping_exit(inode, ret);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001345
Jeff Laytond529ef82014-01-27 13:46:15 -05001346 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001347 smp_mb__after_atomic();
Jeff Laytond529ef82014-01-27 13:46:15 -05001348 wake_up_bit(bitlock, NFS_INO_INVALIDATING);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -07001349out:
Trond Myklebust44b11872006-05-25 01:40:59 -04001350 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001351}
1352
Trond Myklebustca0daa22016-06-08 17:08:28 -04001353static bool nfs_file_has_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001354{
Trond Myklebustca0daa22016-06-08 17:08:28 -04001355 struct inode *inode = &nfsi->vfs_inode;
1356
Trond Myklebustca0daa22016-06-08 17:08:28 -04001357 if (!S_ISREG(inode->i_mode))
1358 return false;
1359 if (list_empty(&nfsi->open_files))
1360 return false;
Trond Myklebust6ba0c4e2018-09-02 15:34:37 -04001361 return inode_is_open_for_write(inode);
Trond Myklebust874f9462015-03-02 23:32:08 -05001362}
1363
Trond Myklebust651b0e72016-06-25 17:24:46 -04001364static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001365{
Trond Myklebust651b0e72016-06-25 17:24:46 -04001366 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
Trond Myklebust874f9462015-03-02 23:32:08 -05001367}
1368
Trond Myklebust783b1942018-03-20 16:53:29 -04001369static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001370{
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001371 struct timespec64 ts;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001372
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001373 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1374 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001375 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05001376 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001377 if (S_ISDIR(inode->i_mode))
Trond Myklebust6edf9602014-06-20 13:11:01 -04001378 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Frank van der Linden0f44da52020-06-23 22:39:00 +00001379 else if (nfs_server_capable(inode, NFS_CAP_XATTR))
1380 nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001381 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001382 /* If we have atomic WCC data, we may update some attributes */
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001383 ts = inode->i_ctime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001384 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1385 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001386 && timespec64_equal(&ts, &fattr->pre_ctime)) {
1387 inode->i_ctime = fattr->ctime;
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001388 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001389
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001390 ts = inode->i_mtime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001391 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1392 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001393 && timespec64_equal(&ts, &fattr->pre_mtime)) {
1394 inode->i_mtime = fattr->mtime;
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001395 if (S_ISDIR(inode->i_mode))
Trond Myklebust6edf9602014-06-20 13:11:01 -04001396 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001397 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001398 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1399 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1400 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001401 && !nfs_have_writebacks(inode)) {
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001402 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001403 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/**
Trond Myklebust33801142005-10-27 22:12:39 -04001407 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001408 * @inode: pointer to inode
1409 * @fattr: updated attributes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 *
1411 * Verifies the attribute cache. If we have just changed the attributes,
1412 * so that fattr carries weak cache consistency data, then it may
1413 * also update the ctime/mtime/change_attribute.
1414 */
Trond Myklebust33801142005-10-27 22:12:39 -04001415static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct nfs_inode *nfsi = NFS_I(inode);
1418 loff_t cur_size, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001419 unsigned long invalid = 0;
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001420 struct timespec64 ts;
Chuck Leverdc592502005-08-18 11:24:12 -07001421
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001422 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
Trond Myklebust01da47b2012-04-29 12:30:19 -04001423 return 0;
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001424
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001425 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1426 /* Only a mounted-on-fileid? Just exit */
1427 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001430 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001431 /* Is this perhaps the mounted-on fileid? */
1432 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1433 nfsi->fileid == fattr->mounted_on_fileid)
1434 return 0;
NeilBrowncc896842017-07-05 12:22:20 +10001435 return -ESTALE;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001436 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001437 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
NeilBrowncc896842017-07-05 12:22:20 +10001438 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001440
Trond Myklebust651b0e72016-06-25 17:24:46 -04001441 if (!nfs_file_has_buffered_writers(nfsi)) {
Trond Myklebustca0daa22016-06-08 17:08:28 -04001442 /* Verify a few of the more important attributes */
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001443 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
Trond Myklebust16e14372018-03-20 16:53:31 -04001444 invalid |= NFS_INO_INVALID_CHANGE
1445 | NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001447 ts = inode->i_mtime;
1448 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001449 invalid |= NFS_INO_INVALID_MTIME;
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001450
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001451 ts = inode->i_ctime;
1452 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001453 invalid |= NFS_INO_INVALID_CTIME;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001454
1455 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1456 cur_size = i_size_read(inode);
1457 new_isize = nfs_size_to_loff_t(fattr->size);
1458 if (cur_size != new_isize)
Trond Myklebust16e14372018-03-20 16:53:31 -04001459 invalid |= NFS_INO_INVALID_SIZE
1460 | NFS_INO_REVAL_PAGECACHE;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001461 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* Have any file permissions changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001465 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
Trond Myklebust16e14372018-03-20 16:53:31 -04001466 invalid |= NFS_INO_INVALID_ACCESS
1467 | NFS_INO_INVALID_ACL
1468 | NFS_INO_INVALID_OTHER;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001469 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
Trond Myklebust16e14372018-03-20 16:53:31 -04001470 invalid |= NFS_INO_INVALID_ACCESS
1471 | NFS_INO_INVALID_ACL
1472 | NFS_INO_INVALID_OTHER;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001473 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
Trond Myklebust16e14372018-03-20 16:53:31 -04001474 invalid |= NFS_INO_INVALID_ACCESS
1475 | NFS_INO_INVALID_ACL
1476 | NFS_INO_INVALID_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* Has the link count changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001479 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
Trond Myklebust16e14372018-03-20 16:53:31 -04001480 invalid |= NFS_INO_INVALID_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001482 ts = inode->i_atime;
1483 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001484 invalid |= NFS_INO_INVALID_ATIME;
1485
1486 if (invalid != 0)
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001487 nfs_set_cache_invalid(inode, invalid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Trond Myklebust33801142005-10-27 22:12:39 -04001489 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491}
1492
Trond Myklebustae05f262008-10-28 15:21:40 -04001493static atomic_long_t nfs_attr_generation_counter;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001494
1495static unsigned long nfs_read_attr_generation_counter(void)
1496{
Trond Myklebustae05f262008-10-28 15:21:40 -04001497 return atomic_long_read(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001498}
1499
1500unsigned long nfs_inc_attr_generation_counter(void)
1501{
Trond Myklebustae05f262008-10-28 15:21:40 -04001502 return atomic_long_inc_return(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001503}
Trond Myklebust3235b402015-02-26 19:52:06 -05001504EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001505
1506void nfs_fattr_init(struct nfs_fattr *fattr)
1507{
1508 fattr->valid = 0;
1509 fattr->time_start = jiffies;
1510 fattr->gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6926afd12012-01-07 13:22:46 -05001511 fattr->owner_name = NULL;
1512 fattr->group_name = NULL;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001513}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001514EXPORT_SYMBOL_GPL(nfs_fattr_init);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001515
Trond Myklebust140e0492015-02-26 17:42:42 -05001516/**
1517 * nfs_fattr_set_barrier
1518 * @fattr: attributes
1519 *
1520 * Used to set a barrier after an attribute was updated. This
1521 * barrier ensures that older attributes from RPC calls that may
1522 * have raced with our update cannot clobber these new values.
1523 * Note that you are still responsible for ensuring that other
1524 * operations which change the attribute on the server do not
1525 * collide.
1526 */
1527void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1528{
1529 fattr->gencount = nfs_inc_attr_generation_counter();
1530}
1531
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001532struct nfs_fattr *nfs_alloc_fattr(void)
1533{
1534 struct nfs_fattr *fattr;
1535
1536 fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1537 if (fattr != NULL)
1538 nfs_fattr_init(fattr);
1539 return fattr;
1540}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001541EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001542
1543struct nfs_fh *nfs_alloc_fhandle(void)
1544{
1545 struct nfs_fh *fh;
1546
1547 fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1548 if (fh != NULL)
1549 fh->size = 0;
1550 return fh;
1551}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001552EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001553
Trond Myklebuste27d3592012-03-18 14:07:42 -04001554#ifdef NFS_DEBUG
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001555/*
1556 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1557 * in the same way that wireshark does
1558 *
1559 * @fh: file handle
1560 *
1561 * For debugging only.
1562 */
1563u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1564{
1565 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1566 * not on the result */
Trond Myklebust1264a2f2013-08-12 16:06:31 -04001567 return nfs_fhandle_hash(fh);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001568}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001569EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001570
1571/*
Chuck Lever20d27e92012-03-01 17:01:31 -05001572 * _nfs_display_fhandle - display an NFS file handle on the console
1573 *
1574 * @fh: file handle to display
1575 * @caption: display caption
1576 *
1577 * For debugging only.
1578 */
Chuck Lever20d27e92012-03-01 17:01:31 -05001579void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1580{
1581 unsigned short i;
1582
Trond Myklebustfa68a1b2012-03-06 10:14:35 -05001583 if (fh == NULL || fh->size == 0) {
Chuck Lever20d27e92012-03-01 17:01:31 -05001584 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1585 return;
1586 }
1587
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001588 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1589 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
Chuck Lever20d27e92012-03-01 17:01:31 -05001590 for (i = 0; i < fh->size; i += 16) {
1591 __be32 *pos = (__be32 *)&fh->data[i];
1592
1593 switch ((fh->size - i - 1) >> 2) {
1594 case 0:
1595 printk(KERN_DEFAULT " %08x\n",
1596 be32_to_cpup(pos));
1597 break;
1598 case 1:
1599 printk(KERN_DEFAULT " %08x %08x\n",
1600 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1601 break;
1602 case 2:
1603 printk(KERN_DEFAULT " %08x %08x %08x\n",
1604 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1605 be32_to_cpup(pos + 2));
1606 break;
1607 default:
1608 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1609 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1610 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1611 }
1612 }
1613}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001614EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
Chuck Lever20d27e92012-03-01 17:01:31 -05001615#endif
1616
1617/**
Trond Myklebusta10ad172008-09-23 17:28:41 -04001618 * nfs_inode_attrs_need_update - check if the inode attributes need updating
Trond Myklebust302fad72019-02-18 13:32:38 -05001619 * @inode: pointer to inode
1620 * @fattr: attributes
Trond Myklebusta10ad172008-09-23 17:28:41 -04001621 *
1622 * Attempt to divine whether or not an RPC call reply carrying stale
1623 * attributes got scheduled after another call carrying updated ones.
1624 *
1625 * To do so, the function first assumes that a more recent ctime means
1626 * that the attributes in fattr are newer, however it also attempt to
1627 * catch the case where ctime either didn't change, or went backwards
1628 * (if someone reset the clock on the server) by looking at whether
1629 * or not this RPC call was started after the inode was last updated.
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001630 * Note also the check for wraparound of 'attr_gencount'
Trond Myklebusta10ad172008-09-23 17:28:41 -04001631 *
1632 * The function returns 'true' if it thinks the attributes in 'fattr' are
1633 * more recent than the ones cached in the inode.
1634 *
1635 */
1636static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1637{
1638 const struct nfs_inode *nfsi = NFS_I(inode);
1639
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001640 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001641 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
Trond Myklebusta10ad172008-09-23 17:28:41 -04001642}
1643
Trond Myklebust870a5be2008-10-05 12:07:23 -04001644static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1645{
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001646 int ret;
1647
1648 trace_nfs_refresh_inode_enter(inode);
1649
Trond Myklebusta10ad172008-09-23 17:28:41 -04001650 if (nfs_inode_attrs_need_update(inode, fattr))
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001651 ret = nfs_update_inode(inode, fattr);
1652 else
1653 ret = nfs_check_inode_attributes(inode, fattr);
1654
1655 trace_nfs_refresh_inode_exit(inode, ret);
1656 return ret;
Trond Myklebust870a5be2008-10-05 12:07:23 -04001657}
1658
Trond Myklebust33801142005-10-27 22:12:39 -04001659/**
1660 * nfs_refresh_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001661 * @inode: pointer to inode
1662 * @fattr: updated attributes
Trond Myklebust33801142005-10-27 22:12:39 -04001663 *
1664 * Check that an RPC call that returned attributes has not overlapped with
1665 * other recent updates of the inode metadata, then decide whether it is
1666 * safe to do a full update of the inode attributes, or whether just to
1667 * call nfs_check_inode_attributes.
1668 */
1669int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1670{
Trond Myklebust33801142005-10-27 22:12:39 -04001671 int status;
1672
1673 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1674 return 0;
1675 spin_lock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001676 status = nfs_refresh_inode_locked(inode, fattr);
Trond Myklebust33801142005-10-27 22:12:39 -04001677 spin_unlock(&inode->i_lock);
David Howellsef79c092009-04-03 16:42:43 +01001678
Trond Myklebust33801142005-10-27 22:12:39 -04001679 return status;
1680}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001681EXPORT_SYMBOL_GPL(nfs_refresh_inode);
Trond Myklebust33801142005-10-27 22:12:39 -04001682
Trond Myklebust16e14372018-03-20 16:53:31 -04001683static int nfs_post_op_update_inode_locked(struct inode *inode,
1684 struct nfs_fattr *fattr, unsigned int invalid)
Trond Myklebustd65f5572008-10-05 12:27:55 -04001685{
Trond Myklebust6edf9602014-06-20 13:11:01 -04001686 if (S_ISDIR(inode->i_mode))
1687 invalid |= NFS_INO_INVALID_DATA;
1688 nfs_set_cache_invalid(inode, invalid);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001689 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1690 return 0;
1691 return nfs_refresh_inode_locked(inode, fattr);
1692}
1693
Trond Myklebustdecf4912005-10-27 22:12:39 -04001694/**
1695 * nfs_post_op_update_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001696 * @inode: pointer to inode
1697 * @fattr: updated attributes
Trond Myklebustdecf4912005-10-27 22:12:39 -04001698 *
1699 * After an operation that has changed the inode metadata, mark the
1700 * attribute cache as being invalid, then try to update it.
Chuck Leverf551e442006-09-20 14:33:04 -04001701 *
1702 * NB: if the server didn't return any post op attributes, this
1703 * function will force the retrieval of attributes before the next
1704 * NFS request. Thus it should be used only for operations that
1705 * are expected to change one or more attributes, to avoid
1706 * unnecessary NFS requests and trips through nfs_update_inode().
Trond Myklebustdecf4912005-10-27 22:12:39 -04001707 */
1708int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1709{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001710 int status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001711
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001712 spin_lock(&inode->i_lock);
Trond Myklebust92d64e42015-02-26 19:48:26 -05001713 nfs_fattr_set_barrier(fattr);
Trond Myklebust16e14372018-03-20 16:53:31 -04001714 status = nfs_post_op_update_inode_locked(inode, fattr,
1715 NFS_INO_INVALID_CHANGE
Trond Myklebustd5541682018-05-29 11:15:49 -04001716 | NFS_INO_INVALID_CTIME
1717 | NFS_INO_REVAL_FORCED);
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001718 spin_unlock(&inode->i_lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04001719
Trond Myklebust870a5be2008-10-05 12:07:23 -04001720 return status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001721}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04001722EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -04001723
Trond Myklebust70ca8852007-09-30 15:21:24 -04001724/**
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001725 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001726 * @inode: pointer to inode
1727 * @fattr: updated attributes
Trond Myklebust70ca8852007-09-30 15:21:24 -04001728 *
1729 * After an operation that has changed the inode metadata, mark the
1730 * attribute cache as being invalid, then try to update it. Fake up
1731 * weak cache consistency data, if none exist.
1732 *
1733 * This function is mainly designed to be used by the ->write_done() functions.
1734 */
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001735int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebust70ca8852007-09-30 15:21:24 -04001736{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001737 int status;
1738
Trond Myklebustd65f5572008-10-05 12:27:55 -04001739 /* Don't do a WCC update if these attributes are already stale */
1740 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1741 !nfs_inode_attrs_need_update(inode, fattr)) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001742 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1743 | NFS_ATTR_FATTR_PRESIZE
1744 | NFS_ATTR_FATTR_PREMTIME
1745 | NFS_ATTR_FATTR_PRECTIME);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001746 goto out_noforce;
1747 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001748 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1749 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05001750 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001751 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001752 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001753 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1754 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001755 fattr->pre_ctime = inode->i_ctime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001756 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1757 }
1758 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1759 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001760 fattr->pre_mtime = inode->i_mtime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001761 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1762 }
1763 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1764 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
Trond Myklebusta3d01452008-06-11 12:21:19 -04001765 fattr->pre_size = i_size_read(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001766 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001767 }
Trond Myklebustd65f5572008-10-05 12:27:55 -04001768out_noforce:
Trond Myklebust16e14372018-03-20 16:53:31 -04001769 status = nfs_post_op_update_inode_locked(inode, fattr,
1770 NFS_INO_INVALID_CHANGE
1771 | NFS_INO_INVALID_CTIME
Zheng Bin3a39e772020-05-21 17:17:21 +08001772 | NFS_INO_INVALID_MTIME
1773 | NFS_INO_INVALID_BLOCKS);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001774 return status;
1775}
1776
1777/**
1778 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001779 * @inode: pointer to inode
1780 * @fattr: updated attributes
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001781 *
1782 * After an operation that has changed the inode metadata, mark the
1783 * attribute cache as being invalid, then try to update it. Fake up
1784 * weak cache consistency data, if none exist.
1785 *
1786 * This function is mainly designed to be used by the ->write_done() functions.
1787 */
1788int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1789{
1790 int status;
1791
1792 spin_lock(&inode->i_lock);
Trond Myklebust8f8ba1d2015-02-26 17:54:58 -05001793 nfs_fattr_set_barrier(fattr);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001794 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001795 spin_unlock(&inode->i_lock);
1796 return status;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001797}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001798EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001799
Anna Schumakerea96d1e2015-04-03 14:35:59 -04001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/*
1802 * Many nfs protocol calls return the new file attributes after
1803 * an operation. Here we update the inode to reflect the state
1804 * of the server's inode.
1805 *
1806 * This is a bit tricky because we have to make sure all dirty pages
1807 * have been sent off to the server before calling invalidate_inode_pages.
1808 * To make sure no other process adds more write requests while we try
1809 * our best to flush them, we make them sleep during the attribute refresh.
1810 *
1811 * A very similar scenario holds for the dir cache.
1812 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001813static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001815 struct nfs_server *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001817 loff_t cur_isize, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001818 unsigned long invalid = 0;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001819 unsigned long now = jiffies;
Trond Myklebust62ab4602009-08-09 15:06:19 -04001820 unsigned long save_cache_validity;
Trond Myklebust651b0e72016-06-25 17:24:46 -04001821 bool have_writers = nfs_file_has_buffered_writers(nfsi);
Benjamin Coddington944171c2016-07-28 14:41:10 -04001822 bool cache_revalidated = true;
Trond Myklebust0b467262018-06-03 11:56:05 -04001823 bool attr_changed = false;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001824 bool have_delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Niels de Vos1e8968c2013-12-17 18:20:16 +01001826 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001827 __func__, inode->i_sb->s_id, inode->i_ino,
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -05001828 nfs_display_fhandle_hash(NFS_FH(inode)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 atomic_read(&inode->i_count), fattr->valid);
1830
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001831 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1832 /* Only a mounted-on-fileid? Just exit */
1833 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1834 return 0;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001835 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001836 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001837 /* Is this perhaps the mounted-on fileid? */
1838 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1839 nfsi->fileid == fattr->mounted_on_fileid)
1840 return 0;
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001841 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1842 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1843 NFS_SERVER(inode)->nfs_client->cl_hostname,
1844 inode->i_sb->s_id, (long long)nfsi->fileid,
1845 (long long)fattr->fileid);
1846 goto out_err;
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /*
1850 * Make sure the inode's type hasn't changed.
1851 */
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001852 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1853 /*
1854 * Big trouble! The inode has become a different object.
1855 */
Niels de Vos1e8968c2013-12-17 18:20:16 +01001856 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001857 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1858 goto out_err;
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001861 server = NFS_SERVER(inode);
Trond Myklebusta0356862007-06-05 13:26:15 -04001862 /* Update the fsid? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001863 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Trond Myklebustc37dcd332008-03-06 12:34:50 -05001864 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
David Howells36d43a42011-01-14 18:45:42 +00001865 !IS_AUTOMOUNT(inode))
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001866 server->fsid = fattr->fsid;
1867
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001868 /* Save the delegation state before clearing cache_validity */
1869 have_delegation = nfs_have_delegated_attributes(inode);
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /*
1872 * Update the read time so we don't revalidate too often.
1873 */
Trond Myklebust33801142005-10-27 22:12:39 -04001874 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001875
Trond Myklebust62ab4602009-08-09 15:06:19 -04001876 save_cache_validity = nfsi->cache_validity;
1877 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1878 | NFS_INO_INVALID_ATIME
1879 | NFS_INO_REVAL_FORCED
Zheng Bin3a39e772020-05-21 17:17:21 +08001880 | NFS_INO_REVAL_PAGECACHE
1881 | NFS_INO_INVALID_BLOCKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001883 /* Do atomic weak cache consistency updates */
Trond Myklebust783b1942018-03-20 16:53:29 -04001884 nfs_wcc_update_inode(inode, fattr);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001885
Benjamin Coddington944171c2016-07-28 14:41:10 -04001886 if (pnfs_layoutcommit_outstanding(inode)) {
1887 nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
1888 cache_revalidated = false;
1889 }
Trond Myklebust10b7e9a2016-07-18 00:51:01 -04001890
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001891 /* More cache consistency checks */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001892 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001893 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
Trond Myklebust38512aa982016-06-07 21:44:08 -04001894 /* Could it be a race with writeback? */
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001895 if (!(have_writers || have_delegation)) {
Trond Myklebust0b467262018-06-03 11:56:05 -04001896 invalid |= NFS_INO_INVALID_DATA
Trond Myklebust38512aa982016-06-07 21:44:08 -04001897 | NFS_INO_INVALID_ACCESS
Frank van der Linden95ad37f2020-06-23 22:39:04 +00001898 | NFS_INO_INVALID_ACL
1899 | NFS_INO_INVALID_XATTR;
Trond Myklebustcac88f92018-03-20 16:53:30 -04001900 /* Force revalidate of all attributes */
Trond Myklebust16e14372018-03-20 16:53:31 -04001901 save_cache_validity |= NFS_INO_INVALID_CTIME
1902 | NFS_INO_INVALID_MTIME
1903 | NFS_INO_INVALID_SIZE
1904 | NFS_INO_INVALID_OTHER;
Trond Myklebust38512aa982016-06-07 21:44:08 -04001905 if (S_ISDIR(inode->i_mode))
1906 nfs_force_lookup_revalidate(inode);
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001907 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1908 inode->i_sb->s_id,
1909 inode->i_ino);
Trond Myklebust1c341b72019-05-22 08:38:57 -04001910 } else if (!have_delegation)
1911 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001912 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust0b467262018-06-03 11:56:05 -04001913 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001914 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001915 } else {
Trond Myklebust8619ddd2018-03-20 16:53:28 -04001916 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001917 (NFS_INO_INVALID_CHANGE
Trond Myklebust8619ddd2018-03-20 16:53:28 -04001918 | NFS_INO_REVAL_PAGECACHE
1919 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001920 cache_revalidated = false;
1921 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001922
1923 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001924 inode->i_mtime = fattr->mtime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001925 } else if (server->caps & NFS_CAP_MTIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001926 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001927 (NFS_INO_INVALID_MTIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001928 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001929 cache_revalidated = false;
1930 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001931
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001932 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001933 inode->i_ctime = fattr->ctime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001934 } else if (server->caps & NFS_CAP_CTIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001935 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001936 (NFS_INO_INVALID_CTIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001937 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001938 cache_revalidated = false;
1939 }
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001940
Trond Myklebust951a1432005-06-22 17:16:30 +00001941 /* Check if our cached file size is stale */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001942 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1943 new_isize = nfs_size_to_loff_t(fattr->size);
1944 cur_isize = i_size_read(inode);
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001945 if (new_isize != cur_isize && !have_delegation) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001946 /* Do we perhaps have any outstanding writes, or has
1947 * the file grown beyond our last write? */
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001948 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001949 i_size_write(inode, new_isize);
Trond Myklebustca0daa22016-06-08 17:08:28 -04001950 if (!have_writers)
Trond Myklebust16e14372018-03-20 16:53:31 -04001951 invalid |= NFS_INO_INVALID_DATA;
Trond Myklebust0b467262018-06-03 11:56:05 -04001952 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001953 }
Harshula Jayasuriya60c16ea2011-05-23 22:52:11 +10001954 dprintk("NFS: isize change on server for file %s/%ld "
1955 "(%Ld to %Ld)\n",
1956 inode->i_sb->s_id,
1957 inode->i_ino,
1958 (long long)cur_isize,
1959 (long long)new_isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001961 } else {
Trond Myklebust43b65352014-04-15 10:07:57 -04001962 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001963 (NFS_INO_INVALID_SIZE
Trond Myklebust62ab4602009-08-09 15:06:19 -04001964 | NFS_INO_REVAL_PAGECACHE
1965 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001966 cache_revalidated = false;
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001970 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001971 inode->i_atime = fattr->atime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001972 else if (server->caps & NFS_CAP_ATIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001973 nfsi->cache_validity |= save_cache_validity &
1974 (NFS_INO_INVALID_ATIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001975 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001976 cache_revalidated = false;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001979 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1980 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
Trond Myklebust9b4b3512010-02-03 08:27:35 -05001981 umode_t newmode = inode->i_mode & S_IFMT;
1982 newmode |= fattr->mode & S_IALLUGO;
1983 inode->i_mode = newmode;
Trond Myklebust16e14372018-03-20 16:53:31 -04001984 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04001985 | NFS_INO_INVALID_ACL;
1986 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001987 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001988 } else if (server->caps & NFS_CAP_MODE) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001989 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04001990 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04001991 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001992 cache_revalidated = false;
1993 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001994
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001995 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001996 if (!uid_eq(inode->i_uid, fattr->uid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04001997 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04001998 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001999 inode->i_uid = fattr->uid;
Trond Myklebust0b467262018-06-03 11:56:05 -04002000 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002001 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002002 } else if (server->caps & NFS_CAP_OWNER) {
Trond Myklebust43b65352014-04-15 10:07:57 -04002003 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04002004 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002005 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002006 cache_revalidated = false;
2007 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04002008
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002009 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08002010 if (!gid_eq(inode->i_gid, fattr->gid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04002011 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04002012 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002013 inode->i_gid = fattr->gid;
Trond Myklebust0b467262018-06-03 11:56:05 -04002014 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002015 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002016 } else if (server->caps & NFS_CAP_OWNER_GROUP) {
Trond Myklebust43b65352014-04-15 10:07:57 -04002017 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04002018 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002019 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002020 cache_revalidated = false;
2021 }
Trond Myklebust921615f2008-10-14 19:23:07 -04002022
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002023 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2024 if (inode->i_nlink != fattr->nlink) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002025 if (S_ISDIR(inode->i_mode))
2026 invalid |= NFS_INO_INVALID_DATA;
Miklos Szeredibfe86842011-10-28 14:13:29 +02002027 set_nlink(inode, fattr->nlink);
Trond Myklebust0b467262018-06-03 11:56:05 -04002028 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002029 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002030 } else if (server->caps & NFS_CAP_NLINK) {
Trond Myklebust43b65352014-04-15 10:07:57 -04002031 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04002032 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002033 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002034 cache_revalidated = false;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002037 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * report the blocks in 512byte units
2040 */
2041 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Trond Myklebustade14a72015-12-29 18:55:19 -05002042 } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002043 inode->i_blocks = fattr->du.nfs2.blocks;
Zheng Bin3a39e772020-05-21 17:17:21 +08002044 else {
2045 nfsi->cache_validity |= save_cache_validity &
2046 (NFS_INO_INVALID_BLOCKS
2047 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002048 cache_revalidated = false;
Zheng Bin3a39e772020-05-21 17:17:21 +08002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /* Update attrtimeo value if we're out of the unstable period */
Trond Myklebust0b467262018-06-03 11:56:05 -04002052 if (attr_changed) {
Trond Myklebustcac88f92018-03-20 16:53:30 -04002053 invalid &= ~NFS_INO_INVALID_ATTR;
Chuck Lever91d5b472006-03-20 13:44:14 -05002054 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebust3e7d9502007-02-05 14:26:28 -08002056 nfsi->attrtimeo_timestamp = now;
Trond Myklebustf5062002015-02-26 19:34:32 -05002057 /* Set barrier to be more recent than all outstanding updates */
Trond Myklebust4704f0e2008-10-14 19:16:07 -04002058 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002059 } else {
Trond Myklebustade14a72015-12-29 18:55:19 -05002060 if (cache_revalidated) {
2061 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2062 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2063 nfsi->attrtimeo <<= 1;
2064 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2065 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2066 }
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002067 nfsi->attrtimeo_timestamp = now;
2068 }
Trond Myklebustf5062002015-02-26 19:34:32 -05002069 /* Set the barrier to be more recent than this fattr */
2070 if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
2071 nfsi->attr_gencount = fattr->gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Jeff Laytonc8120122015-11-25 13:50:11 -05002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /* Don't invalidate the data if we were to blame */
2075 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2076 || S_ISLNK(inode->i_mode)))
2077 invalid &= ~NFS_INO_INVALID_DATA;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002078 nfs_set_cache_invalid(inode, invalid);
David Howellsde242c02012-12-20 21:52:38 +00002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
Trond Myklebustb37b03b2005-11-25 17:10:06 -05002081 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * No need to worry about unhashing the dentry, as the
2084 * lookup validation will know that the inode is bad.
2085 * (But we fall through to invalidate the caches.)
2086 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002087 nfs_set_inode_stale_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return -ESTALE;
2089}
2090
David Howellsf7b422b2006-06-09 09:34:33 -04002091struct inode *nfs_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct nfs_inode *nfsi;
Firo Yangc456aac2015-04-23 17:17:51 +08002094 nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!nfsi)
2096 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07002097 nfsi->flags = 0UL;
2098 nfsi->cache_validity = 0UL;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002099#if IS_ENABLED(CONFIG_NFS_V4)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002100 nfsi->nfs4_acl = NULL;
2101#endif /* CONFIG_NFS_V4 */
Frank van der Linden95ad37f2020-06-23 22:39:04 +00002102#ifdef CONFIG_NFS_V4_2
2103 nfsi->xattr_cache = NULL;
2104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return &nfsi->vfs_inode;
2106}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002107EXPORT_SYMBOL_GPL(nfs_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Al Viroca1a1992019-04-15 20:19:40 -04002109void nfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002110{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002111 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2112}
Al Viroca1a1992019-04-15 20:19:40 -04002113EXPORT_SYMBOL_GPL(nfs_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Andrew Mortond75d5412006-06-25 02:41:26 -07002115static inline void nfs4_init_once(struct nfs_inode *nfsi)
2116{
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002117#if IS_ENABLED(CONFIG_NFS_V4)
Andrew Mortond75d5412006-06-25 02:41:26 -07002118 INIT_LIST_HEAD(&nfsi->open_states);
2119 nfsi->delegation = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002120 init_rwsem(&nfsi->rwsem);
Benny Halevye5e94012010-10-20 00:18:01 -04002121 nfsi->layout = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002122#endif
2123}
David Howellsf7b422b2006-06-09 09:34:33 -04002124
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002125static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2128
Christoph Lametera35afb82007-05-16 22:10:57 -07002129 inode_init_once(&nfsi->vfs_inode);
Christoph Lametera35afb82007-05-16 22:10:57 -07002130 INIT_LIST_HEAD(&nfsi->open_files);
2131 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2132 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
Fred Isamanea2cf222012-04-20 14:47:53 -04002133 INIT_LIST_HEAD(&nfsi->commit_info.list);
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04002134 atomic_long_set(&nfsi->nrequests, 0);
Trond Myklebust5cb953d2017-08-01 17:04:12 -04002135 atomic_long_set(&nfsi->commit_info.ncommit, 0);
Trond Myklebust1a0de482012-06-19 18:38:56 -04002136 atomic_set(&nfsi->commit_info.rpcs_out, 0);
Al Viro884be172016-04-28 23:56:31 -04002137 init_rwsem(&nfsi->rmdir_sem);
Trond Myklebuste824f992017-08-01 11:53:49 -04002138 mutex_init(&nfsi->commit_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -07002139 nfs4_init_once(nfsi);
Trond Myklebustefeda802020-02-05 09:01:54 -05002140 nfsi->cache_change_attribute = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
Paul Mundt20c2df82007-07-20 10:11:58 +09002142
David Howellsf7b422b2006-06-09 09:34:33 -04002143static int __init nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2146 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002147 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08002148 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09002149 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (nfs_inode_cachep == NULL)
2151 return -ENOMEM;
2152
2153 return 0;
2154}
2155
David Brownell266bee82006-06-27 12:59:15 -07002156static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10002158 /*
2159 * Make sure all delayed rcu free inodes are flushed before we
2160 * destroy cache.
2161 */
2162 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002163 kmem_cache_destroy(nfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Trond Myklebust57460062008-02-19 20:04:22 -05002166struct workqueue_struct *nfsiod_workqueue;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002167EXPORT_SYMBOL_GPL(nfsiod_workqueue);
Trond Myklebust57460062008-02-19 20:04:22 -05002168
2169/*
2170 * start up the nfsiod workqueue
2171 */
2172static int nfsiod_start(void)
2173{
2174 struct workqueue_struct *wq;
2175 dprintk("RPC: creating workqueue nfsiod\n");
NeilBrownbf701b762020-11-27 11:24:33 +11002176 wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
Trond Myklebust57460062008-02-19 20:04:22 -05002177 if (wq == NULL)
2178 return -ENOMEM;
2179 nfsiod_workqueue = wq;
2180 return 0;
2181}
2182
2183/*
2184 * Destroy the nfsiod workqueue
2185 */
2186static void nfsiod_stop(void)
2187{
2188 struct workqueue_struct *wq;
2189
2190 wq = nfsiod_workqueue;
2191 if (wq == NULL)
2192 return;
2193 nfsiod_workqueue = NULL;
2194 destroy_workqueue(wq);
2195}
2196
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +03002197unsigned int nfs_net_id;
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04002198EXPORT_SYMBOL_GPL(nfs_net_id);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002199
2200static int nfs_net_init(struct net *net)
2201{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00002202 nfs_clients_init(net);
Eric W. Biederman65b38852014-07-31 04:35:20 -07002203 return nfs_fs_proc_net_init(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002204}
2205
2206static void nfs_net_exit(struct net *net)
2207{
Eric W. Biederman65b38852014-07-31 04:35:20 -07002208 nfs_fs_proc_net_exit(net);
Trond Myklebust10b7a702019-02-06 09:32:50 -05002209 nfs_clients_exit(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002210}
2211
2212static struct pernet_operations nfs_net_ops = {
2213 .init = nfs_net_init,
2214 .exit = nfs_net_exit,
2215 .id = &nfs_net_id,
2216 .size = sizeof(struct nfs_net),
2217};
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219/*
2220 * Initialize NFS
2221 */
2222static int __init init_nfs_fs(void)
2223{
2224 int err;
2225
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002226 err = nfs_sysfs_init();
2227 if (err < 0)
2228 goto out10;
2229
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002230 err = register_pernet_subsys(&nfs_net_ops);
2231 if (err < 0)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002232 goto out9;
Trond Myklebuste571cbf2009-08-19 18:12:27 -04002233
David Howells8ec442a2009-04-03 16:42:42 +01002234 err = nfs_fscache_register();
2235 if (err < 0)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002236 goto out8;
David Howells8ec442a2009-04-03 16:42:42 +01002237
Trond Myklebust57460062008-02-19 20:04:22 -05002238 err = nfsiod_start();
2239 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002240 goto out7;
Trond Myklebust57460062008-02-19 20:04:22 -05002241
David Howells6aaca562006-08-22 20:06:13 -04002242 err = nfs_fs_proc_init();
2243 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002244 goto out6;
David Howells6aaca562006-08-22 20:06:13 -04002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 err = nfs_init_nfspagecache();
2247 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002248 goto out5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 err = nfs_init_inodecache();
2251 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002252 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 err = nfs_init_readpagecache();
2255 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002256 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 err = nfs_init_writepagecache();
2259 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002260 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 err = nfs_init_directcache();
2263 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002264 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002266 rpc_proc_register(&init_net, &nfs_rpcstat);
Kinglong Meecd738ee2015-07-01 11:58:31 +08002267
2268 err = register_nfs_fs();
2269 if (err)
Bryan Schumaker129d1972012-07-16 16:39:13 -04002270 goto out0;
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return 0;
Bryan Schumaker129d1972012-07-16 16:39:13 -04002273out0:
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002274 rpc_proc_unregister(&init_net, "nfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 nfs_destroy_directcache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002276out1:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002277 nfs_destroy_writepagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002278out2:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002279 nfs_destroy_readpagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002280out3:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002281 nfs_destroy_inodecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002282out4:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002283 nfs_destroy_nfspagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002284out5:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002285 nfs_fs_proc_exit();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002286out6:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002287 nfsiod_stop();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002288out7:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002289 nfs_fscache_unregister();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002290out8:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002291 unregister_pernet_subsys(&nfs_net_ops);
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002292out9:
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002293 nfs_sysfs_exit();
2294out10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return err;
2296}
2297
2298static void __exit exit_nfs_fs(void)
2299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 nfs_destroy_directcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 nfs_destroy_writepagecache();
2302 nfs_destroy_readpagecache();
2303 nfs_destroy_inodecache();
2304 nfs_destroy_nfspagecache();
David Howells8ec442a2009-04-03 16:42:42 +01002305 nfs_fscache_unregister();
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002306 unregister_pernet_subsys(&nfs_net_ops);
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002307 rpc_proc_unregister(&init_net, "nfs");
David Howellsf7b422b2006-06-09 09:34:33 -04002308 unregister_nfs_fs();
David Howells6aaca562006-08-22 20:06:13 -04002309 nfs_fs_proc_exit();
Trond Myklebust57460062008-02-19 20:04:22 -05002310 nfsiod_stop();
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002311 nfs_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314/* Not quite true; I just maintain it */
2315MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2316MODULE_LICENSE("GPL");
Trond Myklebustf43bf0b2007-10-09 12:01:04 -04002317module_param(enable_ino64, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319module_init(init_nfs_fs)
2320module_exit(exit_nfs_fs)