blob: 0bf1f835de01410446be110a4c161029037f3973 [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}
196
Trond Myklebust6edf9602014-06-20 13:11:01 -0400197static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
198{
199 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400200 bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400201
Trond Myklebust3f0b3cf2018-06-03 13:05:21 -0400202 if (have_delegation) {
203 if (!(flags & NFS_INO_REVAL_FORCED))
204 flags &= ~NFS_INO_INVALID_OTHER;
205 flags &= ~(NFS_INO_INVALID_CHANGE
206 | NFS_INO_INVALID_SIZE
207 | NFS_INO_REVAL_PAGECACHE);
208 }
209
Trond Myklebust6edf9602014-06-20 13:11:01 -0400210 if (inode->i_mapping->nrpages == 0)
Trond Myklebust1c341b72019-05-22 08:38:57 -0400211 flags &= ~(NFS_INO_INVALID_DATA|NFS_INO_DATA_INVAL_DEFER);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400212 nfsi->cache_validity |= flags;
213 if (flags & NFS_INO_INVALID_DATA)
214 nfs_fscache_invalidate(inode);
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * Invalidate the local caches
219 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500220static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct nfs_inode *nfsi = NFS_I(inode);
223 int mode = inode->i_mode;
224
Chuck Lever91d5b472006-03-20 13:44:14 -0500225 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
226
Trond Myklebustc7c20972007-09-28 19:22:40 -0400227 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
228 nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Trond Myklebustc3f52af2012-09-03 14:56:02 -0400230 memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
David Howellsde242c02012-12-20 21:52:38 +0000231 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
Trond Myklebust6edf9602014-06-20 13:11:01 -0400232 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
David Quigleyaa9c2662013-05-22 12:50:44 -0400233 | NFS_INO_INVALID_DATA
234 | NFS_INO_INVALID_ACCESS
235 | NFS_INO_INVALID_ACL
Trond Myklebust6edf9602014-06-20 13:11:01 -0400236 | NFS_INO_REVAL_PAGECACHE);
David Quigleyaa9c2662013-05-22 12:50:44 -0400237 } else
Trond Myklebust6edf9602014-06-20 13:11:01 -0400238 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
David Quigleyaa9c2662013-05-22 12:50:44 -0400239 | NFS_INO_INVALID_ACCESS
240 | NFS_INO_INVALID_ACL
Trond Myklebust6edf9602014-06-20 13:11:01 -0400241 | NFS_INO_REVAL_PAGECACHE);
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500242 nfs_zap_label_cache_locked(nfsi);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500243}
Chuck Leverdc592502005-08-18 11:24:12 -0700244
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500245void nfs_zap_caches(struct inode *inode)
246{
247 spin_lock(&inode->i_lock);
248 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700249 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000250}
251
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700252void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
253{
254 if (mapping->nrpages != 0) {
255 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400256 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700257 spin_unlock(&inode->i_lock);
258 }
259}
260
Trond Myklebustf41f7412008-06-11 17:39:04 -0400261void nfs_zap_acl_cache(struct inode *inode)
Trond Myklebustada70d92005-06-22 17:16:22 +0000262{
263 void (*clear_acl_cache)(struct inode *);
264
265 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
266 if (clear_acl_cache != NULL)
267 clear_acl_cache(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700268 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700269 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700270 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -0400272EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Trond Myklebustc4812992007-09-28 17:11:45 -0400274void nfs_invalidate_atime(struct inode *inode)
275{
276 spin_lock(&inode->i_lock);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400277 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebustc4812992007-09-28 17:11:45 -0400278 spin_unlock(&inode->i_lock);
279}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400280EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
Trond Myklebustc4812992007-09-28 17:11:45 -0400281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500283 * Invalidate, but do not unhash, the inode.
284 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400286static void nfs_set_inode_stale_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Benny Halevy3a10c302008-01-23 08:58:59 +0200288 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500289 nfs_zap_caches_locked(inode);
Trond Myklebust93ce4af2020-04-06 13:39:29 -0400290 trace_nfs_set_inode_stale(inode);
291}
292
293void nfs_set_inode_stale(struct inode *inode)
294{
295 spin_lock(&inode->i_lock);
296 nfs_set_inode_stale_locked(inode);
297 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300struct nfs_find_desc {
301 struct nfs_fh *fh;
302 struct nfs_fattr *fattr;
303};
304
305/*
306 * In NFSv3 we can have 64bit inode numbers. In order to support
307 * this, and re-exported directories (also seen in NFSv2)
308 * we are forced to allow 2 different inodes to have the same
309 * i_ino.
310 */
311static int
312nfs_find_actor(struct inode *inode, void *opaque)
313{
314 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
315 struct nfs_fh *fh = desc->fh;
316 struct nfs_fattr *fattr = desc->fattr;
317
318 if (NFS_FILEID(inode) != fattr->fileid)
319 return 0;
Jeff Laytonf6488c92013-02-27 20:10:34 -0500320 if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (nfs_compare_fh(NFS_FH(inode), fh))
323 return 0;
324 if (is_bad_inode(inode) || NFS_STALE(inode))
325 return 0;
326 return 1;
327}
328
329static int
330nfs_init_locked(struct inode *inode, void *opaque)
331{
332 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
333 struct nfs_fattr *fattr = desc->fattr;
334
Benny Halevy99fadcd2008-01-23 08:59:08 +0200335 set_nfs_fileid(inode, fattr->fileid);
Trond Myklebust916ec342016-06-17 16:48:28 -0400336 inode->i_mode = fattr->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 nfs_copy_fh(NFS_FH(inode), desc->fh);
338 return 0;
339}
340
Steve Dicksone058f702013-05-22 12:50:40 -0400341#ifdef CONFIG_NFS_V4_SECURITY_LABEL
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500342static void nfs_clear_label_invalid(struct inode *inode)
343{
344 spin_lock(&inode->i_lock);
345 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
346 spin_unlock(&inode->i_lock);
347}
348
David Quigleyaa9c2662013-05-22 12:50:44 -0400349void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
350 struct nfs4_label *label)
351{
352 int error;
353
354 if (label == NULL)
355 return;
356
David Quigleyaa9c2662013-05-22 12:50:44 -0400357 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
358 error = security_inode_notifysecctx(inode, label->label,
359 label->len);
360 if (error)
361 printk(KERN_ERR "%s() %s %d "
362 "security_inode_notifysecctx() %d\n",
363 __func__,
364 (char *)label->label,
365 label->len, error);
Trond Myklebustfd1defc2014-02-06 14:38:53 -0500366 nfs_clear_label_invalid(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -0400367 }
368}
369
Steve Dicksone058f702013-05-22 12:50:40 -0400370struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
371{
372 struct nfs4_label *label = NULL;
373 int minor_version = server->nfs_client->cl_minorversion;
374
375 if (minor_version < 2)
376 return label;
377
378 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
379 return label;
380
381 label = kzalloc(sizeof(struct nfs4_label), flags);
382 if (label == NULL)
383 return ERR_PTR(-ENOMEM);
384
385 label->label = kzalloc(NFS4_MAXLABELLEN, flags);
386 if (label->label == NULL) {
387 kfree(label);
388 return ERR_PTR(-ENOMEM);
389 }
390 label->len = NFS4_MAXLABELLEN;
391
392 return label;
393}
394EXPORT_SYMBOL_GPL(nfs4_label_alloc);
David Quigleyaa9c2662013-05-22 12:50:44 -0400395#else
Trond Myklebust829e57d2013-11-18 14:33:50 -0500396void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -0400397 struct nfs4_label *label)
398{
399}
Steve Dicksone058f702013-05-22 12:50:40 -0400400#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400401EXPORT_SYMBOL_GPL(nfs_setsecurity);
Steve Dicksone058f702013-05-22 12:50:40 -0400402
Peng Taof174ff72017-06-29 06:34:51 -0700403/* Search for inode identified by fh, fileid and i_mode in inode cache. */
404struct inode *
405nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh)
406{
407 struct nfs_find_desc desc = {
408 .fh = fh,
409 .fattr = fattr,
410 };
411 struct inode *inode;
412 unsigned long hash;
413
414 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) ||
415 !(fattr->valid & NFS_ATTR_FATTR_TYPE))
416 return NULL;
417
418 hash = nfs_fattr_to_ino_t(fattr);
419 inode = ilookup5(sb, hash, nfs_find_actor, &desc);
420
421 dprintk("%s: returning %p\n", __func__, inode);
422 return inode;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * This is our front-end to iget that looks up inodes by file handle
427 * instead of inode number.
428 */
429struct inode *
David Quigley1775fd32013-05-22 12:50:42 -0400430nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct nfs_find_desc desc = {
433 .fh = fh,
434 .fattr = fattr
435 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500436 struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long hash;
438
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000439 nfs_attr_check_mountpoint(sb, fattr);
440
Anna Schumaker2ef47eb2014-12-09 16:19:16 -0500441 if (nfs_attr_use_mounted_on_fileid(fattr))
442 fattr->fileid = fattr->mounted_on_fileid;
443 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out_no_inode;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400445 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto out_no_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 hash = nfs_fattr_to_ino_t(fattr);
449
Trond Myklebust03f28e32006-03-20 13:44:48 -0500450 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
451 if (inode == NULL) {
452 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (inode->i_state & I_NEW) {
457 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800458 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* We set i_ino for the few things that still rely on it,
461 * such as stat(2) */
462 inode->i_ino = hash;
463
464 /* We can't support update_atime(), since the server will reset it */
465 inode->i_flags |= S_NOATIME|S_NOCMTIME;
466 inode->i_mode = fattr->mode;
Trond Myklebust821a8682018-03-27 18:30:42 -0400467 nfsi->cache_validity = 0;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400468 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
469 && nfs_server_capable(inode, NFS_CAP_MODE))
Trond Myklebust16e14372018-03-20 16:53:31 -0400470 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Why so? Because we want revalidate for devices/FIFOs, and
472 * that's precisely what we have in nfs_file_inode_operations.
473 */
David Howells8fa5c002006-08-22 20:06:12 -0400474 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (S_ISREG(inode->i_mode)) {
Jeff Layton1788ea62011-11-04 13:31:21 -0400476 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 inode->i_data.a_ops = &nfs_file_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else if (S_ISDIR(inode->i_mode)) {
David Howells8fa5c002006-08-22 20:06:12 -0400479 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 inode->i_fop = &nfs_dir_operations;
Trond Myklebust11de3b12010-12-01 14:17:06 -0500481 inode->i_data.a_ops = &nfs_dir_aops;
Trond Myklebust55a97592006-06-09 09:34:19 -0400482 /* Deal with crossing mountpoints */
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000483 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
484 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400485 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
486 inode->i_op = &nfs_referral_inode_operations;
487 else
488 inode->i_op = &nfs_mountpoint_inode_operations;
Trond Myklebust55a97592006-06-09 09:34:19 -0400489 inode->i_fop = NULL;
David Howells36d43a42011-01-14 18:45:42 +0000490 inode->i_flags |= S_AUTOMOUNT;
Trond Myklebust55a97592006-06-09 09:34:19 -0400491 }
Al Viro21fc61c2015-11-17 01:07:57 -0500492 } else if (S_ISLNK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 inode->i_op = &nfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500494 inode_nohighmem(inode);
495 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 init_special_inode(inode, inode->i_mode, fattr->rdev);
497
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400498 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
499 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
500 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
Jeff Layton1eb5d982018-01-09 08:21:17 -0500501 inode_set_iversion_raw(inode, 0);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400502 inode->i_size = 0;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200503 clear_nlink(inode);
Eric W. Biederman9ff593c2013-02-01 14:26:23 -0800504 inode->i_uid = make_kuid(&init_user_ns, -2);
505 inode->i_gid = make_kgid(&init_user_ns, -2);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400506 inode->i_blocks = 0;
507 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
Andy Adamson2701d082012-05-24 13:13:24 -0400508 nfsi->write_io = 0;
509 nfsi->read_io = 0;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400510
Trond Myklebust33801142005-10-27 22:12:39 -0400511 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400512 nfsi->attr_gencount = fattr->gencount;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400513 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400514 inode->i_atime = fattr->atime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400515 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400516 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400517 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400518 inode->i_mtime = fattr->mtime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400519 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400520 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400521 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400522 inode->i_ctime = fattr->ctime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400523 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
Trond Myklebust16e14372018-03-20 16:53:31 -0400524 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400525 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Jeff Layton1eb5d982018-01-09 08:21:17 -0500526 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebustcd812592015-07-05 11:12:07 -0400527 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400528 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400529 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
530 inode->i_size = nfs_size_to_loff_t(fattr->size);
Trond Myklebust62ab4602009-08-09 15:06:19 -0400531 else
Trond Myklebust16e14372018-03-20 16:53:31 -0400532 nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400533 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200534 set_nlink(inode, fattr->nlink);
Trond Myklebust62ab4602009-08-09 15:06:19 -0400535 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
Trond Myklebust16e14372018-03-20 16:53:31 -0400536 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400537 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
538 inode->i_uid = fattr->uid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400539 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
Trond Myklebust16e14372018-03-20 16:53:31 -0400540 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400541 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
542 inode->i_gid = fattr->gid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400543 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
Trond Myklebust16e14372018-03-20 16:53:31 -0400544 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400545 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
546 inode->i_blocks = fattr->du.nfs2.blocks;
547 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * report the blocks in 512byte units
550 */
551 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
David Quigleyaa9c2662013-05-22 12:50:44 -0400553
Trond Myklebust821a8682018-03-27 18:30:42 -0400554 if (nfsi->cache_validity != 0)
555 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
556
David Quigleyaa9c2662013-05-22 12:50:44 -0400557 nfs_setsecurity(inode, fattr, label);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800560 nfsi->attrtimeo_timestamp = now;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400561 nfsi->access_cache = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
David Howellsf1fe29b2013-09-27 11:20:03 +0100563 nfs_fscache_init_inode(inode);
David Howellsef79c092009-04-03 16:42:43 +0100564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unlock_new_inode(inode);
NeilBrown26fde4d2017-07-03 15:27:26 +1000566 } else {
567 int err = nfs_refresh_inode(inode, fattr);
568 if (err < 0) {
569 iput(inode);
570 inode = ERR_PTR(err);
571 goto out_no_inode;
572 }
573 }
Niels de Vos1e8968c2013-12-17 18:20:16 +0100574 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100576 (unsigned long long)NFS_FILEID(inode),
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -0500577 nfs_display_fhandle_hash(fh),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 atomic_read(&inode->i_count));
579
580out:
581 return inode;
582
583out_no_inode:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500584 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto out;
586}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400587EXPORT_SYMBOL_GPL(nfs_fhget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Trond Myklebust536e43d2012-01-17 22:04:26 -0500589#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 -0700590
591int
592nfs_setattr(struct dentry *dentry, struct iattr *attr)
593{
David Howells2b0143b2015-03-17 22:25:59 +0000594 struct inode *inode = d_inode(dentry);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400595 struct nfs_fattr *fattr;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800596 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Chuck Lever91d5b472006-03-20 13:44:14 -0500598 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
599
Jeff Layton188b95d2007-10-18 03:05:21 -0700600 /* skip mode change if it's just for clearing setuid/setgid */
601 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
602 attr->ia_valid &= ~ATTR_MODE;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig08a899d2014-09-07 08:36:40 -0700605 BUG_ON(!S_ISREG(inode->i_mode));
606
Kinglong Meeae57ca02015-08-26 21:10:55 +0800607 error = inode_newsize_ok(inode, attr->ia_size);
608 if (error)
609 return error;
610
611 if (attr->ia_size == i_size_read(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 attr->ia_valid &= ~ATTR_SIZE;
613 }
614
615 /* Optimization: if the end result is no change, don't RPC */
616 attr->ia_valid &= NFS_VALID_ATTRS;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500617 if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400620 trace_nfs_setattr_enter(inode);
621
Trond Myklebust755c1e22006-03-20 13:44:06 -0500622 /* Write all dirty data */
Trond Myklebust4d346bea2015-03-25 16:38:33 -0400623 if (S_ISREG(inode->i_mode))
624 nfs_sync_inode(inode);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400625
626 fattr = nfs_alloc_fattr();
Kinglong Meeae57ca02015-08-26 21:10:55 +0800627 if (fattr == NULL) {
628 error = -ENOMEM;
Trond Myklebust987f8df2010-04-16 16:22:52 -0400629 goto out;
Kinglong Meeae57ca02015-08-26 21:10:55 +0800630 }
631
Trond Myklebust987f8df2010-04-16 16:22:52 -0400632 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400633 if (error == 0)
David Quigleyaa9c2662013-05-22 12:50:44 -0400634 error = nfs_refresh_inode(inode, fattr);
Trond Myklebust987f8df2010-04-16 16:22:52 -0400635 nfs_free_fattr(fattr);
636out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400637 trace_nfs_setattr_exit(inode, error);
Trond Myklebust65e43082005-08-16 11:49:44 -0400638 return error;
639}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400640EXPORT_SYMBOL_GPL(nfs_setattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400641
642/**
Trond Myklebusta3d01452008-06-11 12:21:19 -0400643 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
644 * @inode: inode of the file used
645 * @offset: file offset to start truncating
646 *
647 * This is a copy of the common vmtruncate, but with the locking
648 * corrected to take into account the fact that NFS requires
649 * inode->i_size to be updated under the inode->i_lock.
Trond Myklebustf0446362015-02-26 16:09:04 -0500650 * Note: must be called with inode->i_lock held!
Trond Myklebusta3d01452008-06-11 12:21:19 -0400651 */
652static int nfs_vmtruncate(struct inode * inode, loff_t offset)
653{
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000654 int err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400655
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000656 err = inode_newsize_ok(inode, offset);
657 if (err)
658 goto out;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400659
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000660 i_size_write(inode, offset);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400661 /* Optimisation */
662 if (offset == 0)
Trond Myklebust1c341b72019-05-22 08:38:57 -0400663 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA |
664 NFS_INO_DATA_INVAL_DEFER);
Trond Myklebustf6cdfa62018-03-27 17:10:42 -0400665 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400666
Trond Myklebustf0446362015-02-26 16:09:04 -0500667 spin_unlock(&inode->i_lock);
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700668 truncate_pagecache(inode, offset);
Trond Myklebustf0446362015-02-26 16:09:04 -0500669 spin_lock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +1000670out:
671 return err;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400672}
673
674/**
Trond Myklebust65e43082005-08-16 11:49:44 -0400675 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
676 * @inode: pointer to struct inode
677 * @attr: pointer to struct iattr
Trond Myklebust16e14372018-03-20 16:53:31 -0400678 * @fattr: pointer to struct nfs_fattr
Trond Myklebust65e43082005-08-16 11:49:44 -0400679 *
680 * Note: we do this in the *proc.c in order to ensure that
681 * it works for things like exclusive creates too.
682 */
Trond Myklebustf0446362015-02-26 16:09:04 -0500683void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
684 struct nfs_fattr *fattr)
Trond Myklebust65e43082005-08-16 11:49:44 -0400685{
Trond Myklebustf0446362015-02-26 16:09:04 -0500686 /* Barrier: bump the attribute generation count. */
687 nfs_fattr_set_barrier(fattr);
688
689 spin_lock(&inode->i_lock);
690 NFS_I(inode)->attr_gencount = fattr->gencount;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400691 if ((attr->ia_valid & ATTR_SIZE) != 0) {
692 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
693 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
694 nfs_vmtruncate(inode, attr->ia_size);
695 }
Trond Myklebust65e43082005-08-16 11:49:44 -0400696 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
Trond Myklebust6a97d022018-04-08 17:51:11 -0400697 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400699 int mode = attr->ia_mode & S_IALLUGO;
700 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 inode->i_mode = mode;
702 }
703 if ((attr->ia_valid & ATTR_UID) != 0)
704 inode->i_uid = attr->ia_uid;
705 if ((attr->ia_valid & ATTR_GID) != 0)
706 inode->i_gid = attr->ia_gid;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400707 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400708 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400709 else
710 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
711 | NFS_INO_INVALID_CTIME);
Trond Myklebust6edf9602014-06-20 13:11:01 -0400712 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
713 | NFS_INO_INVALID_ACL);
Trond Myklebust65e43082005-08-16 11:49:44 -0400714 }
Trond Myklebust6a97d022018-04-08 17:51:11 -0400715 if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) {
716 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME
717 | NFS_INO_INVALID_CTIME);
718 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400719 inode->i_atime = fattr->atime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400720 else if (attr->ia_valid & ATTR_ATIME_SET)
721 inode->i_atime = attr->ia_atime;
722 else
723 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
724
725 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400726 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400727 else
728 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
729 | NFS_INO_INVALID_CTIME);
730 }
731 if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) {
732 NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME
733 | NFS_INO_INVALID_CTIME);
734 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400735 inode->i_mtime = fattr->mtime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400736 else if (attr->ia_valid & ATTR_MTIME_SET)
737 inode->i_mtime = attr->ia_mtime;
738 else
739 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
740
741 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -0400742 inode->i_ctime = fattr->ctime;
Trond Myklebust6a97d022018-04-08 17:51:11 -0400743 else
744 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE
745 | NFS_INO_INVALID_CTIME);
Trond Myklebust65e43082005-08-16 11:49:44 -0400746 }
Jeff Layton616c3192015-11-25 13:50:45 -0500747 if (fattr->valid)
748 nfs_update_inode(inode, fattr);
Trond Myklebustf0446362015-02-26 16:09:04 -0500749 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400751EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500753static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
Trond Myklebust311324a2014-02-07 17:02:08 -0500754{
755 struct dentry *parent;
756
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500757 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
758 return;
Trond Myklebust311324a2014-02-07 17:02:08 -0500759 parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000760 nfs_force_use_readdirplus(d_inode(parent));
Trond Myklebust311324a2014-02-07 17:02:08 -0500761 dput(parent);
762}
763
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500764static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
765{
766 struct dentry *parent;
767
768 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
769 return;
770 parent = dget_parent(dentry);
771 nfs_advise_use_readdirplus(d_inode(parent));
772 dput(parent);
773}
774
Trond Myklebust311324a2014-02-07 17:02:08 -0500775static bool nfs_need_revalidate_inode(struct inode *inode)
776{
777 if (NFS_I(inode)->cache_validity &
778 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
779 return true;
780 if (nfs_attribute_cache_expired(inode))
781 return true;
782 return false;
783}
784
David Howellsa528d352017-01-31 16:46:22 +0000785int nfs_getattr(const struct path *path, struct kstat *stat,
786 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
David Howellsa528d352017-01-31 16:46:22 +0000788 struct inode *inode = d_inode(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500789 struct nfs_server *server = NFS_SERVER(inode);
790 unsigned long cache_validity;
Jan Kara16caf5b2014-10-23 14:02:47 +0200791 int err = 0;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500792 bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
793 bool do_update = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400795 trace_nfs_getattr_enter(inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500796
797 if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync)
798 goto out_no_update;
799
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800800 /* Flush out writes to the server in order to update c/mtime. */
Trond Myklebust9ccee942018-01-04 17:46:09 -0500801 if ((request_mask & (STATX_CTIME|STATX_MTIME)) &&
802 S_ISREG(inode->i_mode)) {
Trond Myklebust79566ef2016-06-25 17:45:40 -0400803 err = filemap_write_and_wait(inode->i_mapping);
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800804 if (err)
805 goto out;
Chuck Lever28c494c2007-10-26 13:32:13 -0400806 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800807
808 /*
809 * We may force a getattr if the user cares about atime.
810 *
811 * Note that we only have to check the vfsmount flags here:
812 * - NFS always sets S_NOATIME by so checking it would give a
813 * bogus result
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800814 * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800815 * no point in checking those.
816 */
David Howellsa528d352017-01-31 16:46:22 +0000817 if ((path->mnt->mnt_flags & MNT_NOATIME) ||
818 ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Trond Myklebust9ccee942018-01-04 17:46:09 -0500819 request_mask &= ~STATX_ATIME;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800820
Trond Myklebust9ccee942018-01-04 17:46:09 -0500821 /* Is the user requesting attributes that might need revalidation? */
822 if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME|
823 STATX_MTIME|STATX_UID|STATX_GID|
824 STATX_SIZE|STATX_BLOCKS)))
825 goto out_no_revalidate;
Trond Myklebust311324a2014-02-07 17:02:08 -0500826
Trond Myklebust9ccee942018-01-04 17:46:09 -0500827 /* Check whether the cached attributes are stale */
828 do_update |= force_sync || nfs_attribute_cache_expired(inode);
829 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
830 do_update |= cache_validity &
831 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL);
832 if (request_mask & STATX_ATIME)
833 do_update |= cache_validity & NFS_INO_INVALID_ATIME;
834 if (request_mask & (STATX_CTIME|STATX_MTIME))
835 do_update |= cache_validity & NFS_INO_REVAL_PAGECACHE;
Zheng Bin3a39e772020-05-21 17:17:21 +0800836 if (request_mask & STATX_BLOCKS)
837 do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
Trond Myklebust9ccee942018-01-04 17:46:09 -0500838 if (do_update) {
839 /* Update the attribute cache */
Hou Tao59b86d82017-04-28 18:35:19 +0800840 if (!(server->flags & NFS_MOUNT_NOAC))
841 nfs_readdirplus_parent_cache_miss(path->dentry);
842 else
843 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust311324a2014-02-07 17:02:08 -0500844 err = __nfs_revalidate_inode(server, inode);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500845 if (err)
846 goto out;
Trond Myklebust1bcf4c52016-12-02 09:15:37 -0500847 } else
David Howellsa528d352017-01-31 16:46:22 +0000848 nfs_readdirplus_parent_cache_hit(path->dentry);
Trond Myklebust9ccee942018-01-04 17:46:09 -0500849out_no_revalidate:
850 /* Only return attributes that were revalidated. */
851 stat->result_mask &= request_mask;
852out_no_update:
853 generic_fillattr(inode, stat);
854 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
855 if (S_ISDIR(inode->i_mode))
856 stat->blksize = NFS_SERVER(inode)->dtsize;
Trond Myklebustacdc53b2010-02-19 17:03:26 -0800857out:
Trond Myklebustf4ce1292013-08-19 18:59:33 -0400858 trace_nfs_getattr_exit(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return err;
860}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400861EXPORT_SYMBOL_GPL(nfs_getattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400863static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
864{
Elena Reshetova2f62b5a2017-10-20 12:53:37 +0300865 refcount_set(&l_ctx->count, 1);
NeilBrownd51fdb82016-10-13 15:26:47 +1100866 l_ctx->lockowner = current->files;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400867 INIT_LIST_HEAD(&l_ctx->list);
Benjamin Coddington210c7c12016-01-06 10:40:18 -0500868 atomic_set(&l_ctx->io_count, 0);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400869}
870
871static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
872{
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400873 struct nfs_lock_context *pos;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400874
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400875 list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) {
NeilBrownd51fdb82016-10-13 15:26:47 +1100876 if (pos->lockowner != current->files)
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400877 continue;
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400878 if (refcount_inc_not_zero(&pos->count))
879 return pos;
880 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400881 return NULL;
882}
883
884struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
885{
886 struct nfs_lock_context *res, *new = NULL;
David Howells2b0143b2015-03-17 22:25:59 +0000887 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400888
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400889 rcu_read_lock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400890 res = __nfs_find_lock_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400891 rcu_read_unlock();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400892 if (res == NULL) {
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400893 new = kmalloc(sizeof(*new), GFP_KERNEL);
894 if (new == NULL)
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400895 return ERR_PTR(-ENOMEM);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400896 nfs_init_lock_context(new);
897 spin_lock(&inode->i_lock);
898 res = __nfs_find_lock_context(ctx);
899 if (res == NULL) {
Trond Myklebust15494512019-04-07 13:59:10 -0400900 new->open_context = get_nfs_open_context(ctx);
901 if (new->open_context) {
902 list_add_tail_rcu(&new->list,
903 &ctx->lock_context.list);
904 res = new;
905 new = NULL;
906 } else
907 res = ERR_PTR(-EBADF);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400908 }
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400909 spin_unlock(&inode->i_lock);
910 kfree(new);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400911 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400912 return res;
913}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400914EXPORT_SYMBOL_GPL(nfs_get_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400915
916void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
917{
918 struct nfs_open_context *ctx = l_ctx->open_context;
David Howells2b0143b2015-03-17 22:25:59 +0000919 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400920
Elena Reshetova2f62b5a2017-10-20 12:53:37 +0300921 if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400922 return;
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400923 list_del_rcu(&l_ctx->list);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400924 spin_unlock(&inode->i_lock);
Trond Myklebust15494512019-04-07 13:59:10 -0400925 put_nfs_open_context(ctx);
Trond Myklebust1db97ea2018-09-02 15:11:57 -0400926 kfree_rcu(l_ctx, rcu_head);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400927}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400928EXPORT_SYMBOL_GPL(nfs_put_lock_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400929
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400930/**
931 * nfs_close_context - Common close_context() routine NFSv2/v3
932 * @ctx: pointer to context
933 * @is_sync: is this a synchronous close
934 *
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400935 * Ensure that the attributes are up to date if we're mounted
936 * with close-to-open semantics and we have cached data that will
937 * need to be revalidated on open.
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400938 */
939void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
940{
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400941 struct nfs_inode *nfsi;
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400942 struct inode *inode;
943 struct nfs_server *server;
944
945 if (!(ctx->mode & FMODE_WRITE))
946 return;
947 if (!is_sync)
948 return;
David Howells2b0143b2015-03-17 22:25:59 +0000949 inode = d_inode(ctx->dentry);
Trond Myklebust58ff4182016-12-16 17:39:58 -0500950 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
951 return;
Trond Myklebust5cf9d702015-09-04 15:07:37 -0400952 nfsi = NFS_I(inode);
953 if (inode->i_mapping->nrpages == 0)
954 return;
955 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
956 return;
957 if (!list_empty(&nfsi->open_files))
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400958 return;
959 server = NFS_SERVER(inode);
960 if (server->flags & NFS_MOUNT_NOCTO)
961 return;
962 nfs_revalidate_inode(server, inode);
963}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400964EXPORT_SYMBOL_GPL(nfs_close_context);
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400965
NeilBrown532d4de2016-10-13 15:26:47 +1100966struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
967 fmode_t f_mode,
968 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct nfs_open_context *ctx;
971
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700972 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Trond Myklebust1d179d62020-02-07 19:32:49 -0500973 if (!ctx)
Al Viro5ede7b12011-10-23 18:49:54 -0400974 return ERR_PTR(-ENOMEM);
Al Viro5ede7b12011-10-23 18:49:54 -0400975 nfs_sb_active(dentry->d_sb);
976 ctx->dentry = dget(dentry);
Trond Myklebust1d179d62020-02-07 19:32:49 -0500977 if (filp)
978 ctx->cred = get_cred(filp->f_cred);
979 else
980 ctx->cred = get_current_cred();
NeilBrownddf529e2018-12-03 11:30:30 +1100981 ctx->ll_cred = NULL;
Al Viro5ede7b12011-10-23 18:49:54 -0400982 ctx->state = NULL;
983 ctx->mode = f_mode;
984 ctx->flags = 0;
985 ctx->error = 0;
NeilBrown532d4de2016-10-13 15:26:47 +1100986 ctx->flock_owner = (fl_owner_t)filp;
Al Viro5ede7b12011-10-23 18:49:54 -0400987 nfs_init_lock_context(&ctx->lock_context);
988 ctx->lock_context.open_context = ctx;
989 INIT_LIST_HEAD(&ctx->list);
Andy Adamson82be4172012-05-23 05:02:35 -0400990 ctx->mdsthreshold = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ctx;
992}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400993EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
996{
Trond Myklebust0de43972018-09-02 15:57:01 -0400997 if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count))
998 return ctx;
999 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001001EXPORT_SYMBOL_GPL(get_nfs_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001003static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001004{
David Howells2b0143b2015-03-17 22:25:59 +00001005 struct inode *inode = d_inode(ctx->dentry);
Al Viro3d4ff432011-06-22 18:40:12 -04001006 struct super_block *sb = ctx->dentry->d_sb;
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001007
Trond Myklebust0de43972018-09-02 15:57:01 -04001008 if (!refcount_dec_and_test(&ctx->lock_context.count))
Trond Myklebust5e119342007-07-26 12:06:17 -04001009 return;
Trond Myklebust0de43972018-09-02 15:57:01 -04001010 if (!list_empty(&ctx->list)) {
1011 spin_lock(&inode->i_lock);
1012 list_del_rcu(&ctx->list);
1013 spin_unlock(&inode->i_lock);
1014 }
Trond Myklebust5c78f582010-09-27 15:51:20 -04001015 if (inode != NULL)
1016 NFS_PROTO(inode)->close_context(ctx, is_sync);
NeilBrowna52458b2018-12-03 11:30:31 +11001017 put_cred(ctx->cred);
Al Viro3d4ff432011-06-22 18:40:12 -04001018 dput(ctx->dentry);
Trond Myklebust322b2b92013-01-11 16:39:51 -05001019 nfs_sb_deactive(sb);
NeilBrownddf529e2018-12-03 11:30:30 +11001020 put_rpccred(ctx->ll_cred);
Andy Adamson82be4172012-05-23 05:02:35 -04001021 kfree(ctx->mdsthreshold);
Trond Myklebust0de43972018-09-02 15:57:01 -04001022 kfree_rcu(ctx, rcu_head);
Trond Myklebust3bec63d2007-06-17 16:02:44 -04001023}
1024
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001025void put_nfs_open_context(struct nfs_open_context *ctx)
1026{
1027 __put_nfs_open_context(ctx, 0);
1028}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001029EXPORT_SYMBOL_GPL(put_nfs_open_context);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001030
Trond Myklebust4eae5012015-09-04 15:17:53 -04001031static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
1032{
1033 __put_nfs_open_context(ctx, 1);
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/*
1037 * Ensure that mmap has a recent RPC credential for use when writing out
1038 * shared pages
1039 */
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001040void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
David Howells2b0143b2015-03-17 22:25:59 +00001042 struct inode *inode = d_inode(ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct nfs_inode *nfsi = NFS_I(inode);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_lock(&inode->i_lock);
Trond Myklebust1c341b72019-05-22 08:38:57 -04001046 if (list_empty(&nfsi->open_files) &&
1047 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
1048 nfsi->cache_validity |= NFS_INO_INVALID_DATA |
1049 NFS_INO_REVAL_FORCED;
Trond Myklebust0de43972018-09-02 15:57:01 -04001050 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_unlock(&inode->i_lock);
1052}
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001053EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
1054
1055void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1056{
1057 filp->private_data = get_nfs_open_context(ctx);
1058 if (list_empty(&ctx->list))
1059 nfs_inode_attach_open_context(ctx);
1060}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001061EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Trond Myklebustd5308382005-11-04 15:33:38 -05001063/*
1064 * Given an inode, search for an open context with the desired characteristics
1065 */
NeilBrowna52458b2018-12-03 11:30:31 +11001066struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct nfs_inode *nfsi = NFS_I(inode);
1069 struct nfs_open_context *pos, *ctx = NULL;
1070
Trond Myklebust0de43972018-09-02 15:57:01 -04001071 rcu_read_lock();
1072 list_for_each_entry_rcu(pos, &nfsi->open_files, list) {
Trond Myklebust65f51602020-01-26 17:31:15 -05001073 if (cred != NULL && cred_fscmp(pos->cred, cred) != 0)
Trond Myklebustd5308382005-11-04 15:33:38 -05001074 continue;
Trond Myklebust1544fa02010-03-25 13:54:49 -04001075 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
1076 continue;
1077 ctx = get_nfs_open_context(pos);
Trond Myklebust0de43972018-09-02 15:57:01 -04001078 if (ctx)
1079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001081 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ctx;
1083}
1084
Anna Schumakeraff8d8d2015-07-13 14:01:33 -04001085void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001087 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (ctx) {
David Howells2b0143b2015-03-17 22:25:59 +00001090 struct inode *inode = d_inode(ctx->dentry);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04001091
Peng Tao0bcbf032015-12-05 15:57:31 +08001092 /*
1093 * We fatal error on write before. Try to writeback
1094 * every page again.
1095 */
1096 if (ctx->error < 0)
1097 invalidate_inode_pages2(inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 filp->private_data = NULL;
Trond Myklebust4eae5012015-09-04 15:17:53 -04001099 put_nfs_open_context_sync(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101}
1102
1103/*
1104 * These allocate and release file read/write context information.
1105 */
1106int nfs_open(struct inode *inode, struct file *filp)
1107{
1108 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown532d4de2016-10-13 15:26:47 +11001110 ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
Al Viro5ede7b12011-10-23 18:49:54 -04001111 if (IS_ERR(ctx))
1112 return PTR_ERR(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 nfs_file_set_open_context(filp, ctx);
1114 put_nfs_open_context(ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +01001115 nfs_fscache_open_file(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117}
Trond Myklebust44942b42019-06-27 06:41:45 -04001118EXPORT_SYMBOL_GPL(nfs_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
1121 * This function is called whenever some part of NFS notices that
1122 * the cached attributes have to be refreshed.
1123 */
1124int
1125__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1126{
1127 int status = -ESTALE;
David Quigley1775fd32013-05-22 12:50:42 -04001128 struct nfs4_label *label = NULL;
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001129 struct nfs_fattr *fattr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Niels de Vos1e8968c2013-12-17 18:20:16 +01001132 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
1133 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001135 trace_nfs_revalidate_inode_enter(inode);
1136
Chuck Lever85233a72006-10-19 23:28:42 -07001137 if (is_bad_inode(inode))
Chuck Lever412d5822005-08-18 11:24:11 -07001138 goto out;
Trond Myklebust7fdc49c2007-09-28 19:11:33 -04001139 if (NFS_STALE(inode))
1140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Trond Myklebustac46bd32016-07-05 13:46:53 -04001142 /* pNFS: Attributes aren't updated until we layoutcommit */
1143 if (S_ISREG(inode->i_mode)) {
1144 status = pnfs_sync_inode(inode, false);
1145 if (status)
1146 goto out;
1147 }
1148
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001149 status = -ENOMEM;
1150 fattr = nfs_alloc_fattr();
1151 if (fattr == NULL)
1152 goto out;
1153
Trond Myklebust691beb12008-10-05 14:48:22 -04001154 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
David Quigley14c43f72013-05-22 12:50:43 -04001155
1156 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1157 if (IS_ERR(label)) {
1158 status = PTR_ERR(label);
1159 goto out;
1160 }
1161
Trond Myklebusta841b542018-04-07 13:50:59 -04001162 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr,
1163 label, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (status != 0) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001165 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001167 (unsigned long long)NFS_FILEID(inode), status);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001168 switch (status) {
1169 case -ETIMEDOUT:
1170 /* A soft timeout occurred. Use cached information? */
1171 if (server->flags & NFS_MOUNT_SOFTREVAL)
1172 status = 0;
1173 break;
1174 case -ESTALE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!S_ISDIR(inode->i_mode))
Trond Myklebust93ce4af2020-04-06 13:39:29 -04001176 nfs_set_inode_stale(inode);
1177 else
1178 nfs_zap_caches(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
David Quigley14c43f72013-05-22 12:50:43 -04001180 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001183 status = nfs_refresh_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (status) {
Niels de Vos1e8968c2013-12-17 18:20:16 +01001185 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001187 (unsigned long long)NFS_FILEID(inode), status);
David Quigley14c43f72013-05-22 12:50:43 -04001188 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Chuck Lever55296802005-08-18 11:24:09 -07001190
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001191 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +00001192 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -07001193
Jeff Layton3da580a2013-11-02 06:57:18 -04001194 nfs_setsecurity(inode, fattr, label);
1195
Niels de Vos1e8968c2013-12-17 18:20:16 +01001196 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +01001198 (unsigned long long)NFS_FILEID(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
David Quigley14c43f72013-05-22 12:50:43 -04001200err_out:
1201 nfs4_label_free(label);
1202out:
Trond Myklebusta3cba2a2010-04-16 16:22:49 -04001203 nfs_free_fattr(fattr);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001204 trace_nfs_revalidate_inode_exit(inode, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return status;
1206}
1207
Scott Mayhew43f291c2013-07-05 17:49:30 -04001208int nfs_attribute_cache_expired(struct inode *inode)
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001209{
Trond Myklebustb4d23142010-03-10 15:21:44 -05001210 if (nfs_have_delegated_attributes(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return 0;
Trond Myklebustd7cf8dd2010-04-16 16:42:46 -04001212 return nfs_attribute_timeout(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215/**
1216 * nfs_revalidate_inode - Revalidate the inode attributes
Trond Myklebust302fad72019-02-18 13:32:38 -05001217 * @server: pointer to nfs_server struct
1218 * @inode: pointer to inode struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *
1220 * Updates inode attribute information by retrieving the data from the server.
1221 */
1222int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1223{
Trond Myklebust311324a2014-02-07 17:02:08 -05001224 if (!nfs_need_revalidate_inode(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return NFS_STALE(inode) ? -ESTALE : 0;
1226 return __nfs_revalidate_inode(server, inode);
1227}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04001228EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Trond Myklebust1cda7072010-02-19 17:03:30 -08001230static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust717d44e2007-01-24 11:54:55 -08001231{
1232 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustf8806c82013-08-05 13:26:31 -04001233 int ret;
1234
Trond Myklebust717d44e2007-01-24 11:54:55 -08001235 if (mapping->nrpages != 0) {
Trond Myklebustf8806c82013-08-05 13:26:31 -04001236 if (S_ISREG(inode->i_mode)) {
1237 ret = nfs_sync_mapping(mapping);
1238 if (ret < 0)
1239 return ret;
1240 }
1241 ret = invalidate_inode_pages2(mapping);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001242 if (ret < 0)
1243 return ret;
1244 }
Jeff Laytond529ef82014-01-27 13:46:15 -05001245 if (S_ISDIR(inode->i_mode)) {
1246 spin_lock(&inode->i_lock);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001247 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
Jeff Laytond529ef82014-01-27 13:46:15 -05001248 spin_unlock(&inode->i_lock);
1249 }
Trond Myklebust717d44e2007-01-24 11:54:55 -08001250 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
David Howellsde242c02012-12-20 21:52:38 +00001251 nfs_fscache_wait_on_invalidate(inode);
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001252
Niels de Vos1e8968c2013-12-17 18:20:16 +01001253 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1254 inode->i_sb->s_id,
1255 (unsigned long long)NFS_FILEID(inode));
Trond Myklebust717d44e2007-01-24 11:54:55 -08001256 return 0;
1257}
1258
Trond Myklebust1cd9cb02016-12-04 18:34:34 -05001259bool nfs_mapping_need_revalidate_inode(struct inode *inode)
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001260{
Trond Myklebust61540bf2016-12-08 18:18:38 -05001261 return nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE) ||
1262 NFS_STALE(inode);
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001263}
1264
Al Viro0d0def42015-11-17 21:14:24 -05001265int nfs_revalidate_mapping_rcu(struct inode *inode)
1266{
1267 struct nfs_inode *nfsi = NFS_I(inode);
1268 unsigned long *bitlock = &nfsi->flags;
1269 int ret = 0;
1270
1271 if (IS_SWAPFILE(inode))
1272 goto out;
1273 if (nfs_mapping_need_revalidate_inode(inode)) {
1274 ret = -ECHILD;
1275 goto out;
1276 }
1277 spin_lock(&inode->i_lock);
1278 if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1279 (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1280 ret = -ECHILD;
1281 spin_unlock(&inode->i_lock);
1282out:
1283 return ret;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/**
Trond Myklebustbe527492016-06-22 08:19:36 -04001287 * nfs_revalidate_mapping - Revalidate the pagecache
Trond Myklebust302fad72019-02-18 13:32:38 -05001288 * @inode: pointer to host inode
1289 * @mapping: pointer to mapping
Trond Myklebust7d52e862005-06-22 17:16:30 +00001290 */
Trond Myklebustbe527492016-06-22 08:19:36 -04001291int nfs_revalidate_mapping(struct inode *inode,
1292 struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +00001293{
1294 struct nfs_inode *nfsi = NFS_I(inode);
Jeff Laytond529ef82014-01-27 13:46:15 -05001295 unsigned long *bitlock = &nfsi->flags;
Trond Myklebust44b11872006-05-25 01:40:59 -04001296 int ret = 0;
1297
Mel Gorman29418aa2012-07-31 16:45:10 -07001298 /* swapfiles are not supposed to be shared. */
1299 if (IS_SWAPFILE(inode))
1300 goto out;
1301
Trond Myklebustb4b1ead2012-04-29 11:23:50 -04001302 if (nfs_mapping_need_revalidate_inode(inode)) {
Trond Myklebust44b11872006-05-25 01:40:59 -04001303 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001304 if (ret < 0)
1305 goto out;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001306 }
Jeff Laytond529ef82014-01-27 13:46:15 -05001307
1308 /*
1309 * We must clear NFS_INO_INVALID_DATA first to ensure that
1310 * invalidations that come in while we're shooting down the mappings
1311 * are respected. But, that leaves a race window where one revalidator
1312 * can clear the flag, and then another checks it before the mapping
1313 * gets invalidated. Fix that by serializing access to this part of
1314 * the function.
1315 *
1316 * At the same time, we need to allow other tasks to see whether we
1317 * might be in the middle of invalidating the pages, so we only set
1318 * the bit lock here if it looks like we're going to be doing that.
1319 */
1320 for (;;) {
NeilBrown74316202014-07-07 15:16:04 +10001321 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1322 nfs_wait_bit_killable, TASK_KILLABLE);
Jeff Laytond529ef82014-01-27 13:46:15 -05001323 if (ret)
1324 goto out;
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001325 spin_lock(&inode->i_lock);
1326 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1327 spin_unlock(&inode->i_lock);
1328 continue;
1329 }
1330 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
Jeff Laytond529ef82014-01-27 13:46:15 -05001331 break;
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001332 spin_unlock(&inode->i_lock);
1333 goto out;
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001334 }
1335
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001336 set_bit(NFS_INO_INVALIDATING, bitlock);
Jeff Layton4db72b42014-01-28 13:47:46 -05001337 smp_wmb();
Trond Myklebust1c341b72019-05-22 08:38:57 -04001338 nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA|
1339 NFS_INO_DATA_INVAL_DEFER);
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001340 spin_unlock(&inode->i_lock);
1341 trace_nfs_invalidate_mapping_enter(inode);
Trond Myklebustbe527492016-06-22 08:19:36 -04001342 ret = nfs_invalidate_mapping(inode, mapping);
Trond Myklebust17dfeb92014-01-28 09:37:16 -05001343 trace_nfs_invalidate_mapping_exit(inode, ret);
Trond Myklebust717d44e2007-01-24 11:54:55 -08001344
Jeff Laytond529ef82014-01-27 13:46:15 -05001345 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001346 smp_mb__after_atomic();
Jeff Laytond529ef82014-01-27 13:46:15 -05001347 wake_up_bit(bitlock, NFS_INO_INVALIDATING);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -07001348out:
Trond Myklebust44b11872006-05-25 01:40:59 -04001349 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001350}
1351
Trond Myklebustca0daa22016-06-08 17:08:28 -04001352static bool nfs_file_has_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001353{
Trond Myklebustca0daa22016-06-08 17:08:28 -04001354 struct inode *inode = &nfsi->vfs_inode;
1355
Trond Myklebustca0daa22016-06-08 17:08:28 -04001356 if (!S_ISREG(inode->i_mode))
1357 return false;
1358 if (list_empty(&nfsi->open_files))
1359 return false;
Trond Myklebust6ba0c4e2018-09-02 15:34:37 -04001360 return inode_is_open_for_write(inode);
Trond Myklebust874f9462015-03-02 23:32:08 -05001361}
1362
Trond Myklebust651b0e72016-06-25 17:24:46 -04001363static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
Trond Myklebust874f9462015-03-02 23:32:08 -05001364{
Trond Myklebust651b0e72016-06-25 17:24:46 -04001365 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
Trond Myklebust874f9462015-03-02 23:32:08 -05001366}
1367
Trond Myklebust783b1942018-03-20 16:53:29 -04001368static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001369{
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001370 struct timespec64 ts;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001371
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001372 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1373 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001374 && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05001375 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001376 if (S_ISDIR(inode->i_mode))
Trond Myklebust6edf9602014-06-20 13:11:01 -04001377 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001378 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001379 /* If we have atomic WCC data, we may update some attributes */
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001380 ts = inode->i_ctime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001381 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1382 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001383 && timespec64_equal(&ts, &fattr->pre_ctime)) {
1384 inode->i_ctime = fattr->ctime;
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001385 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001386
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001387 ts = inode->i_mtime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001388 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1389 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001390 && timespec64_equal(&ts, &fattr->pre_mtime)) {
1391 inode->i_mtime = fattr->mtime;
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001392 if (S_ISDIR(inode->i_mode))
Trond Myklebust6edf9602014-06-20 13:11:01 -04001393 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001394 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001395 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1396 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1397 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001398 && !nfs_have_writebacks(inode)) {
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001399 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
Trond Myklebust27dc1cd2011-01-25 15:28:21 -05001400 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/**
Trond Myklebust33801142005-10-27 22:12:39 -04001404 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001405 * @inode: pointer to inode
1406 * @fattr: updated attributes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 *
1408 * Verifies the attribute cache. If we have just changed the attributes,
1409 * so that fattr carries weak cache consistency data, then it may
1410 * also update the ctime/mtime/change_attribute.
1411 */
Trond Myklebust33801142005-10-27 22:12:39 -04001412static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct nfs_inode *nfsi = NFS_I(inode);
1415 loff_t cur_size, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001416 unsigned long invalid = 0;
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001417 struct timespec64 ts;
Chuck Leverdc592502005-08-18 11:24:12 -07001418
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001419 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
Trond Myklebust01da47b2012-04-29 12:30:19 -04001420 return 0;
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001421
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001422 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1423 /* Only a mounted-on-fileid? Just exit */
1424 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001427 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001428 /* Is this perhaps the mounted-on fileid? */
1429 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1430 nfsi->fileid == fattr->mounted_on_fileid)
1431 return 0;
NeilBrowncc896842017-07-05 12:22:20 +10001432 return -ESTALE;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001433 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001434 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
NeilBrowncc896842017-07-05 12:22:20 +10001435 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001437
Trond Myklebust651b0e72016-06-25 17:24:46 -04001438 if (!nfs_file_has_buffered_writers(nfsi)) {
Trond Myklebustca0daa22016-06-08 17:08:28 -04001439 /* Verify a few of the more important attributes */
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001440 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr))
Trond Myklebust16e14372018-03-20 16:53:31 -04001441 invalid |= NFS_INO_INVALID_CHANGE
1442 | NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001444 ts = inode->i_mtime;
1445 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001446 invalid |= NFS_INO_INVALID_MTIME;
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001447
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001448 ts = inode->i_ctime;
1449 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime))
Trond Myklebust16e14372018-03-20 16:53:31 -04001450 invalid |= NFS_INO_INVALID_CTIME;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001451
1452 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1453 cur_size = i_size_read(inode);
1454 new_isize = nfs_size_to_loff_t(fattr->size);
1455 if (cur_size != new_isize)
Trond Myklebust16e14372018-03-20 16:53:31 -04001456 invalid |= NFS_INO_INVALID_SIZE
1457 | NFS_INO_REVAL_PAGECACHE;
Trond Myklebustca0daa22016-06-08 17:08:28 -04001458 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Have any file permissions changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001462 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
Trond Myklebust16e14372018-03-20 16:53:31 -04001463 invalid |= NFS_INO_INVALID_ACCESS
1464 | NFS_INO_INVALID_ACL
1465 | NFS_INO_INVALID_OTHER;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001466 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
Trond Myklebust16e14372018-03-20 16:53:31 -04001467 invalid |= NFS_INO_INVALID_ACCESS
1468 | NFS_INO_INVALID_ACL
1469 | NFS_INO_INVALID_OTHER;
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001470 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
Trond Myklebust16e14372018-03-20 16:53:31 -04001471 invalid |= NFS_INO_INVALID_ACCESS
1472 | NFS_INO_INVALID_ACL
1473 | NFS_INO_INVALID_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /* Has the link count changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001476 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
Trond Myklebust16e14372018-03-20 16:53:31 -04001477 invalid |= NFS_INO_INVALID_OTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001479 ts = inode->i_atime;
1480 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001481 invalid |= NFS_INO_INVALID_ATIME;
1482
1483 if (invalid != 0)
Trond Myklebust4ebe83a2018-06-03 12:12:52 -04001484 nfs_set_cache_invalid(inode, invalid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Trond Myklebust33801142005-10-27 22:12:39 -04001486 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488}
1489
Trond Myklebustae05f262008-10-28 15:21:40 -04001490static atomic_long_t nfs_attr_generation_counter;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001491
1492static unsigned long nfs_read_attr_generation_counter(void)
1493{
Trond Myklebustae05f262008-10-28 15:21:40 -04001494 return atomic_long_read(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001495}
1496
1497unsigned long nfs_inc_attr_generation_counter(void)
1498{
Trond Myklebustae05f262008-10-28 15:21:40 -04001499 return atomic_long_inc_return(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001500}
Trond Myklebust3235b402015-02-26 19:52:06 -05001501EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001502
1503void nfs_fattr_init(struct nfs_fattr *fattr)
1504{
1505 fattr->valid = 0;
1506 fattr->time_start = jiffies;
1507 fattr->gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6926afd2012-01-07 13:22:46 -05001508 fattr->owner_name = NULL;
1509 fattr->group_name = NULL;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001510}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001511EXPORT_SYMBOL_GPL(nfs_fattr_init);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001512
Trond Myklebust140e0492015-02-26 17:42:42 -05001513/**
1514 * nfs_fattr_set_barrier
1515 * @fattr: attributes
1516 *
1517 * Used to set a barrier after an attribute was updated. This
1518 * barrier ensures that older attributes from RPC calls that may
1519 * have raced with our update cannot clobber these new values.
1520 * Note that you are still responsible for ensuring that other
1521 * operations which change the attribute on the server do not
1522 * collide.
1523 */
1524void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1525{
1526 fattr->gencount = nfs_inc_attr_generation_counter();
1527}
1528
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001529struct nfs_fattr *nfs_alloc_fattr(void)
1530{
1531 struct nfs_fattr *fattr;
1532
1533 fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1534 if (fattr != NULL)
1535 nfs_fattr_init(fattr);
1536 return fattr;
1537}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001538EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001539
1540struct nfs_fh *nfs_alloc_fhandle(void)
1541{
1542 struct nfs_fh *fh;
1543
1544 fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1545 if (fh != NULL)
1546 fh->size = 0;
1547 return fh;
1548}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001549EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
Trond Myklebust2d36bfde2010-04-16 16:22:45 -04001550
Trond Myklebuste27d3592012-03-18 14:07:42 -04001551#ifdef NFS_DEBUG
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001552/*
1553 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1554 * in the same way that wireshark does
1555 *
1556 * @fh: file handle
1557 *
1558 * For debugging only.
1559 */
1560u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1561{
1562 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1563 * not on the result */
Trond Myklebust1264a2f2013-08-12 16:06:31 -04001564 return nfs_fhandle_hash(fh);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001565}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001566EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001567
1568/*
Chuck Lever20d27e92012-03-01 17:01:31 -05001569 * _nfs_display_fhandle - display an NFS file handle on the console
1570 *
1571 * @fh: file handle to display
1572 * @caption: display caption
1573 *
1574 * For debugging only.
1575 */
Chuck Lever20d27e92012-03-01 17:01:31 -05001576void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1577{
1578 unsigned short i;
1579
Trond Myklebustfa68a1b2012-03-06 10:14:35 -05001580 if (fh == NULL || fh->size == 0) {
Chuck Lever20d27e92012-03-01 17:01:31 -05001581 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1582 return;
1583 }
1584
Weston Andros Adamsond8e05392012-03-06 20:46:43 -05001585 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1586 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
Chuck Lever20d27e92012-03-01 17:01:31 -05001587 for (i = 0; i < fh->size; i += 16) {
1588 __be32 *pos = (__be32 *)&fh->data[i];
1589
1590 switch ((fh->size - i - 1) >> 2) {
1591 case 0:
1592 printk(KERN_DEFAULT " %08x\n",
1593 be32_to_cpup(pos));
1594 break;
1595 case 1:
1596 printk(KERN_DEFAULT " %08x %08x\n",
1597 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1598 break;
1599 case 2:
1600 printk(KERN_DEFAULT " %08x %08x %08x\n",
1601 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1602 be32_to_cpup(pos + 2));
1603 break;
1604 default:
1605 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1606 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1607 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1608 }
1609 }
1610}
Chuck Lever9e6ee762013-10-17 14:12:45 -04001611EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
Chuck Lever20d27e92012-03-01 17:01:31 -05001612#endif
1613
1614/**
Trond Myklebusta10ad172008-09-23 17:28:41 -04001615 * nfs_inode_attrs_need_update - check if the inode attributes need updating
Trond Myklebust302fad72019-02-18 13:32:38 -05001616 * @inode: pointer to inode
1617 * @fattr: attributes
Trond Myklebusta10ad172008-09-23 17:28:41 -04001618 *
1619 * Attempt to divine whether or not an RPC call reply carrying stale
1620 * attributes got scheduled after another call carrying updated ones.
1621 *
1622 * To do so, the function first assumes that a more recent ctime means
1623 * that the attributes in fattr are newer, however it also attempt to
1624 * catch the case where ctime either didn't change, or went backwards
1625 * (if someone reset the clock on the server) by looking at whether
1626 * or not this RPC call was started after the inode was last updated.
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001627 * Note also the check for wraparound of 'attr_gencount'
Trond Myklebusta10ad172008-09-23 17:28:41 -04001628 *
1629 * The function returns 'true' if it thinks the attributes in 'fattr' are
1630 * more recent than the ones cached in the inode.
1631 *
1632 */
1633static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1634{
1635 const struct nfs_inode *nfsi = NFS_I(inode);
1636
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001637 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001638 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
Trond Myklebusta10ad172008-09-23 17:28:41 -04001639}
1640
Trond Myklebust870a5be2008-10-05 12:07:23 -04001641static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1642{
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001643 int ret;
1644
1645 trace_nfs_refresh_inode_enter(inode);
1646
Trond Myklebusta10ad172008-09-23 17:28:41 -04001647 if (nfs_inode_attrs_need_update(inode, fattr))
Trond Myklebustf4ce1292013-08-19 18:59:33 -04001648 ret = nfs_update_inode(inode, fattr);
1649 else
1650 ret = nfs_check_inode_attributes(inode, fattr);
1651
1652 trace_nfs_refresh_inode_exit(inode, ret);
1653 return ret;
Trond Myklebust870a5be2008-10-05 12:07:23 -04001654}
1655
Trond Myklebust33801142005-10-27 22:12:39 -04001656/**
1657 * nfs_refresh_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001658 * @inode: pointer to inode
1659 * @fattr: updated attributes
Trond Myklebust33801142005-10-27 22:12:39 -04001660 *
1661 * Check that an RPC call that returned attributes has not overlapped with
1662 * other recent updates of the inode metadata, then decide whether it is
1663 * safe to do a full update of the inode attributes, or whether just to
1664 * call nfs_check_inode_attributes.
1665 */
1666int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1667{
Trond Myklebust33801142005-10-27 22:12:39 -04001668 int status;
1669
1670 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1671 return 0;
1672 spin_lock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001673 status = nfs_refresh_inode_locked(inode, fattr);
Trond Myklebust33801142005-10-27 22:12:39 -04001674 spin_unlock(&inode->i_lock);
David Howellsef79c092009-04-03 16:42:43 +01001675
Trond Myklebust33801142005-10-27 22:12:39 -04001676 return status;
1677}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001678EXPORT_SYMBOL_GPL(nfs_refresh_inode);
Trond Myklebust33801142005-10-27 22:12:39 -04001679
Trond Myklebust16e14372018-03-20 16:53:31 -04001680static int nfs_post_op_update_inode_locked(struct inode *inode,
1681 struct nfs_fattr *fattr, unsigned int invalid)
Trond Myklebustd65f5572008-10-05 12:27:55 -04001682{
Trond Myklebust6edf9602014-06-20 13:11:01 -04001683 if (S_ISDIR(inode->i_mode))
1684 invalid |= NFS_INO_INVALID_DATA;
1685 nfs_set_cache_invalid(inode, invalid);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001686 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1687 return 0;
1688 return nfs_refresh_inode_locked(inode, fattr);
1689}
1690
Trond Myklebustdecf4912005-10-27 22:12:39 -04001691/**
1692 * nfs_post_op_update_inode - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001693 * @inode: pointer to inode
1694 * @fattr: updated attributes
Trond Myklebustdecf4912005-10-27 22:12:39 -04001695 *
1696 * After an operation that has changed the inode metadata, mark the
1697 * attribute cache as being invalid, then try to update it.
Chuck Leverf551e442006-09-20 14:33:04 -04001698 *
1699 * NB: if the server didn't return any post op attributes, this
1700 * function will force the retrieval of attributes before the next
1701 * NFS request. Thus it should be used only for operations that
1702 * are expected to change one or more attributes, to avoid
1703 * unnecessary NFS requests and trips through nfs_update_inode().
Trond Myklebustdecf4912005-10-27 22:12:39 -04001704 */
1705int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1706{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001707 int status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001708
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001709 spin_lock(&inode->i_lock);
Trond Myklebust92d64e42015-02-26 19:48:26 -05001710 nfs_fattr_set_barrier(fattr);
Trond Myklebust16e14372018-03-20 16:53:31 -04001711 status = nfs_post_op_update_inode_locked(inode, fattr,
1712 NFS_INO_INVALID_CHANGE
Trond Myklebustd5541682018-05-29 11:15:49 -04001713 | NFS_INO_INVALID_CTIME
1714 | NFS_INO_REVAL_FORCED);
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001715 spin_unlock(&inode->i_lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04001716
Trond Myklebust870a5be2008-10-05 12:07:23 -04001717 return status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001718}
Bryan Schumaker1c606fb2012-07-30 16:05:24 -04001719EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -04001720
Trond Myklebust70ca8852007-09-30 15:21:24 -04001721/**
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001722 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001723 * @inode: pointer to inode
1724 * @fattr: updated attributes
Trond Myklebust70ca8852007-09-30 15:21:24 -04001725 *
1726 * After an operation that has changed the inode metadata, mark the
1727 * attribute cache as being invalid, then try to update it. Fake up
1728 * weak cache consistency data, if none exist.
1729 *
1730 * This function is mainly designed to be used by the ->write_done() functions.
1731 */
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001732int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
Trond Myklebust70ca8852007-09-30 15:21:24 -04001733{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001734 int status;
1735
Trond Myklebustd65f5572008-10-05 12:27:55 -04001736 /* Don't do a WCC update if these attributes are already stale */
1737 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1738 !nfs_inode_attrs_need_update(inode, fattr)) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001739 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1740 | NFS_ATTR_FATTR_PRESIZE
1741 | NFS_ATTR_FATTR_PREMTIME
1742 | NFS_ATTR_FATTR_PRECTIME);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001743 goto out_noforce;
1744 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001745 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1746 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
Jeff Layton1eb5d982018-01-09 08:21:17 -05001747 fattr->pre_change_attr = inode_peek_iversion_raw(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001748 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001749 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001750 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1751 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001752 fattr->pre_ctime = inode->i_ctime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001753 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1754 }
1755 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1756 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001757 fattr->pre_mtime = inode->i_mtime;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001758 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1759 }
1760 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1761 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
Trond Myklebusta3d01452008-06-11 12:21:19 -04001762 fattr->pre_size = i_size_read(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001763 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001764 }
Trond Myklebustd65f5572008-10-05 12:27:55 -04001765out_noforce:
Trond Myklebust16e14372018-03-20 16:53:31 -04001766 status = nfs_post_op_update_inode_locked(inode, fattr,
1767 NFS_INO_INVALID_CHANGE
1768 | NFS_INO_INVALID_CTIME
Zheng Bin3a39e772020-05-21 17:17:21 +08001769 | NFS_INO_INVALID_MTIME
1770 | NFS_INO_INVALID_BLOCKS);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001771 return status;
1772}
1773
1774/**
1775 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
Trond Myklebust302fad72019-02-18 13:32:38 -05001776 * @inode: pointer to inode
1777 * @fattr: updated attributes
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001778 *
1779 * After an operation that has changed the inode metadata, mark the
1780 * attribute cache as being invalid, then try to update it. Fake up
1781 * weak cache consistency data, if none exist.
1782 *
1783 * This function is mainly designed to be used by the ->write_done() functions.
1784 */
1785int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1786{
1787 int status;
1788
1789 spin_lock(&inode->i_lock);
Trond Myklebust8f8ba1d2015-02-26 17:54:58 -05001790 nfs_fattr_set_barrier(fattr);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05001791 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001792 spin_unlock(&inode->i_lock);
1793 return status;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001794}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001795EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
Trond Myklebust70ca8852007-09-30 15:21:24 -04001796
Anna Schumakerea96d1e2015-04-03 14:35:59 -04001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/*
1799 * Many nfs protocol calls return the new file attributes after
1800 * an operation. Here we update the inode to reflect the state
1801 * of the server's inode.
1802 *
1803 * This is a bit tricky because we have to make sure all dirty pages
1804 * have been sent off to the server before calling invalidate_inode_pages.
1805 * To make sure no other process adds more write requests while we try
1806 * our best to flush them, we make them sleep during the attribute refresh.
1807 *
1808 * A very similar scenario holds for the dir cache.
1809 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001810static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001812 struct nfs_server *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001814 loff_t cur_isize, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001815 unsigned long invalid = 0;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001816 unsigned long now = jiffies;
Trond Myklebust62ab4602009-08-09 15:06:19 -04001817 unsigned long save_cache_validity;
Trond Myklebust651b0e72016-06-25 17:24:46 -04001818 bool have_writers = nfs_file_has_buffered_writers(nfsi);
Benjamin Coddington944171c2016-07-28 14:41:10 -04001819 bool cache_revalidated = true;
Trond Myklebust0b467262018-06-03 11:56:05 -04001820 bool attr_changed = false;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001821 bool have_delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Niels de Vos1e8968c2013-12-17 18:20:16 +01001823 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001824 __func__, inode->i_sb->s_id, inode->i_ino,
Weston Andros Adamson4f1abd22012-03-06 21:58:20 -05001825 nfs_display_fhandle_hash(NFS_FH(inode)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 atomic_read(&inode->i_count), fattr->valid);
1827
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001828 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) {
1829 /* Only a mounted-on-fileid? Just exit */
1830 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1831 return 0;
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001832 /* Has the inode gone and changed behind our back? */
Trond Myklebusteb3d8f42019-08-28 11:26:13 -04001833 } else if (nfsi->fileid != fattr->fileid) {
Trond Myklebust7e10cc22019-08-09 12:06:43 -04001834 /* Is this perhaps the mounted-on fileid? */
1835 if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) &&
1836 nfsi->fileid == fattr->mounted_on_fileid)
1837 return 0;
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001838 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1839 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1840 NFS_SERVER(inode)->nfs_client->cl_hostname,
1841 inode->i_sb->s_id, (long long)nfsi->fileid,
1842 (long long)fattr->fileid);
1843 goto out_err;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 /*
1847 * Make sure the inode's type hasn't changed.
1848 */
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001849 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1850 /*
1851 * Big trouble! The inode has become a different object.
1852 */
Niels de Vos1e8968c2013-12-17 18:20:16 +01001853 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
Matthew Treinishe73e6c92012-04-30 11:32:57 -04001854 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1855 goto out_err;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001858 server = NFS_SERVER(inode);
Trond Myklebusta0356862007-06-05 13:26:15 -04001859 /* Update the fsid? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001860 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Trond Myklebustc37dcd332008-03-06 12:34:50 -05001861 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
David Howells36d43a42011-01-14 18:45:42 +00001862 !IS_AUTOMOUNT(inode))
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001863 server->fsid = fattr->fsid;
1864
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001865 /* Save the delegation state before clearing cache_validity */
1866 have_delegation = nfs_have_delegated_attributes(inode);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Update the read time so we don't revalidate too often.
1870 */
Trond Myklebust33801142005-10-27 22:12:39 -04001871 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001872
Trond Myklebust62ab4602009-08-09 15:06:19 -04001873 save_cache_validity = nfsi->cache_validity;
1874 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1875 | NFS_INO_INVALID_ATIME
1876 | NFS_INO_REVAL_FORCED
Zheng Bin3a39e772020-05-21 17:17:21 +08001877 | NFS_INO_REVAL_PAGECACHE
1878 | NFS_INO_INVALID_BLOCKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001880 /* Do atomic weak cache consistency updates */
Trond Myklebust783b1942018-03-20 16:53:29 -04001881 nfs_wcc_update_inode(inode, fattr);
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001882
Benjamin Coddington944171c2016-07-28 14:41:10 -04001883 if (pnfs_layoutcommit_outstanding(inode)) {
1884 nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
1885 cache_revalidated = false;
1886 }
Trond Myklebust10b7e9a2016-07-18 00:51:01 -04001887
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001888 /* More cache consistency checks */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001889 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
Goffredo Baroncellic472c072018-02-01 08:15:25 -05001890 if (!inode_eq_iversion_raw(inode, fattr->change_attr)) {
Trond Myklebust38512aa982016-06-07 21:44:08 -04001891 /* Could it be a race with writeback? */
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001892 if (!(have_writers || have_delegation)) {
Trond Myklebust0b467262018-06-03 11:56:05 -04001893 invalid |= NFS_INO_INVALID_DATA
Trond Myklebust38512aa982016-06-07 21:44:08 -04001894 | NFS_INO_INVALID_ACCESS
1895 | NFS_INO_INVALID_ACL;
Trond Myklebustcac88f92018-03-20 16:53:30 -04001896 /* Force revalidate of all attributes */
Trond Myklebust16e14372018-03-20 16:53:31 -04001897 save_cache_validity |= NFS_INO_INVALID_CTIME
1898 | NFS_INO_INVALID_MTIME
1899 | NFS_INO_INVALID_SIZE
1900 | NFS_INO_INVALID_OTHER;
Trond Myklebust38512aa982016-06-07 21:44:08 -04001901 if (S_ISDIR(inode->i_mode))
1902 nfs_force_lookup_revalidate(inode);
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001903 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1904 inode->i_sb->s_id,
1905 inode->i_ino);
Trond Myklebust1c341b72019-05-22 08:38:57 -04001906 } else if (!have_delegation)
1907 nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001908 inode_set_iversion_raw(inode, fattr->change_attr);
Trond Myklebust0b467262018-06-03 11:56:05 -04001909 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001910 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001911 } else {
Trond Myklebust8619ddd2018-03-20 16:53:28 -04001912 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001913 (NFS_INO_INVALID_CHANGE
Trond Myklebust8619ddd2018-03-20 16:53:28 -04001914 | NFS_INO_REVAL_PAGECACHE
1915 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001916 cache_revalidated = false;
1917 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001918
1919 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001920 inode->i_mtime = fattr->mtime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001921 } else if (server->caps & NFS_CAP_MTIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001922 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001923 (NFS_INO_INVALID_MTIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001924 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001925 cache_revalidated = false;
1926 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001927
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001928 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001929 inode->i_ctime = fattr->ctime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001930 } else if (server->caps & NFS_CAP_CTIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001931 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001932 (NFS_INO_INVALID_CTIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001933 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001934 cache_revalidated = false;
1935 }
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001936
Trond Myklebust951a1432005-06-22 17:16:30 +00001937 /* Check if our cached file size is stale */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001938 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1939 new_isize = nfs_size_to_loff_t(fattr->size);
1940 cur_isize = i_size_read(inode);
Trond Myklebustc80d17c2018-06-03 11:38:38 -04001941 if (new_isize != cur_isize && !have_delegation) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001942 /* Do we perhaps have any outstanding writes, or has
1943 * the file grown beyond our last write? */
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001944 if (!nfs_have_writebacks(inode) || new_isize > cur_isize) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001945 i_size_write(inode, new_isize);
Trond Myklebustca0daa22016-06-08 17:08:28 -04001946 if (!have_writers)
Trond Myklebust16e14372018-03-20 16:53:31 -04001947 invalid |= NFS_INO_INVALID_DATA;
Trond Myklebust0b467262018-06-03 11:56:05 -04001948 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001949 }
Harshula Jayasuriya60c16ea2011-05-23 22:52:11 +10001950 dprintk("NFS: isize change on server for file %s/%ld "
1951 "(%Ld to %Ld)\n",
1952 inode->i_sb->s_id,
1953 inode->i_ino,
1954 (long long)cur_isize,
1955 (long long)new_isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001957 } else {
Trond Myklebust43b65352014-04-15 10:07:57 -04001958 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04001959 (NFS_INO_INVALID_SIZE
Trond Myklebust62ab4602009-08-09 15:06:19 -04001960 | NFS_INO_REVAL_PAGECACHE
1961 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001962 cache_revalidated = false;
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001966 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
Trond Myklebuste86d5a02019-10-04 16:38:56 -04001967 inode->i_atime = fattr->atime;
Trond Myklebustade14a72015-12-29 18:55:19 -05001968 else if (server->caps & NFS_CAP_ATIME) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001969 nfsi->cache_validity |= save_cache_validity &
1970 (NFS_INO_INVALID_ATIME
Trond Myklebust62ab4602009-08-09 15:06:19 -04001971 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001972 cache_revalidated = false;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001975 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1976 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
Trond Myklebust9b4b3512010-02-03 08:27:35 -05001977 umode_t newmode = inode->i_mode & S_IFMT;
1978 newmode |= fattr->mode & S_IALLUGO;
1979 inode->i_mode = newmode;
Trond Myklebust16e14372018-03-20 16:53:31 -04001980 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04001981 | NFS_INO_INVALID_ACL;
1982 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001983 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001984 } else if (server->caps & NFS_CAP_MODE) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001985 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04001986 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04001987 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05001988 cache_revalidated = false;
1989 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001990
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001991 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08001992 if (!uid_eq(inode->i_uid, fattr->uid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04001993 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04001994 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001995 inode->i_uid = fattr->uid;
Trond Myklebust0b467262018-06-03 11:56:05 -04001996 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001997 }
Trond Myklebustade14a72015-12-29 18:55:19 -05001998 } else if (server->caps & NFS_CAP_OWNER) {
Trond Myklebust43b65352014-04-15 10:07:57 -04001999 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04002000 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002001 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002002 cache_revalidated = false;
2003 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04002004
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002005 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
Eric W. Biederman9ff593c2013-02-01 14:26:23 -08002006 if (!gid_eq(inode->i_gid, fattr->gid)) {
Trond Myklebust16e14372018-03-20 16:53:31 -04002007 invalid |= NFS_INO_INVALID_ACCESS
Trond Myklebust0b467262018-06-03 11:56:05 -04002008 | NFS_INO_INVALID_ACL;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002009 inode->i_gid = fattr->gid;
Trond Myklebust0b467262018-06-03 11:56:05 -04002010 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002011 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002012 } else if (server->caps & NFS_CAP_OWNER_GROUP) {
Trond Myklebust43b65352014-04-15 10:07:57 -04002013 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust0b467262018-06-03 11:56:05 -04002014 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002015 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002016 cache_revalidated = false;
2017 }
Trond Myklebust921615f2008-10-14 19:23:07 -04002018
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002019 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
2020 if (inode->i_nlink != fattr->nlink) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002021 if (S_ISDIR(inode->i_mode))
2022 invalid |= NFS_INO_INVALID_DATA;
Miklos Szeredibfe86842011-10-28 14:13:29 +02002023 set_nlink(inode, fattr->nlink);
Trond Myklebust0b467262018-06-03 11:56:05 -04002024 attr_changed = true;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002025 }
Trond Myklebustade14a72015-12-29 18:55:19 -05002026 } else if (server->caps & NFS_CAP_NLINK) {
Trond Myklebust43b65352014-04-15 10:07:57 -04002027 nfsi->cache_validity |= save_cache_validity &
Trond Myklebust16e14372018-03-20 16:53:31 -04002028 (NFS_INO_INVALID_OTHER
Trond Myklebust62ab4602009-08-09 15:06:19 -04002029 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002030 cache_revalidated = false;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002033 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /*
2035 * report the blocks in 512byte units
2036 */
2037 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Trond Myklebustade14a72015-12-29 18:55:19 -05002038 } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04002039 inode->i_blocks = fattr->du.nfs2.blocks;
Zheng Bin3a39e772020-05-21 17:17:21 +08002040 else {
2041 nfsi->cache_validity |= save_cache_validity &
2042 (NFS_INO_INVALID_BLOCKS
2043 | NFS_INO_REVAL_FORCED);
Trond Myklebustade14a72015-12-29 18:55:19 -05002044 cache_revalidated = false;
Zheng Bin3a39e772020-05-21 17:17:21 +08002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* Update attrtimeo value if we're out of the unstable period */
Trond Myklebust0b467262018-06-03 11:56:05 -04002048 if (attr_changed) {
Trond Myklebustcac88f92018-03-20 16:53:30 -04002049 invalid &= ~NFS_INO_INVALID_ATTR;
Chuck Lever91d5b472006-03-20 13:44:14 -05002050 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebust3e7d9502007-02-05 14:26:28 -08002052 nfsi->attrtimeo_timestamp = now;
Trond Myklebustf5062002015-02-26 19:34:32 -05002053 /* Set barrier to be more recent than all outstanding updates */
Trond Myklebust4704f0e2008-10-14 19:16:07 -04002054 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002055 } else {
Trond Myklebustade14a72015-12-29 18:55:19 -05002056 if (cache_revalidated) {
2057 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
2058 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
2059 nfsi->attrtimeo <<= 1;
2060 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
2061 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
2062 }
Trond Myklebust6d2b2962007-10-01 18:57:50 -04002063 nfsi->attrtimeo_timestamp = now;
2064 }
Trond Myklebustf5062002015-02-26 19:34:32 -05002065 /* Set the barrier to be more recent than this fattr */
2066 if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
2067 nfsi->attr_gencount = fattr->gencount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Jeff Laytonc8120122015-11-25 13:50:11 -05002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* Don't invalidate the data if we were to blame */
2071 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
2072 || S_ISLNK(inode->i_mode)))
2073 invalid &= ~NFS_INO_INVALID_DATA;
Trond Myklebustc80d17c2018-06-03 11:38:38 -04002074 nfs_set_cache_invalid(inode, invalid);
David Howellsde242c02012-12-20 21:52:38 +00002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
Trond Myklebustb37b03b2005-11-25 17:10:06 -05002077 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /*
2079 * No need to worry about unhashing the dentry, as the
2080 * lookup validation will know that the inode is bad.
2081 * (But we fall through to invalidate the caches.)
2082 */
Trond Myklebust93ce4af2020-04-06 13:39:29 -04002083 nfs_set_inode_stale_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return -ESTALE;
2085}
2086
David Howellsf7b422b2006-06-09 09:34:33 -04002087struct inode *nfs_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct nfs_inode *nfsi;
Firo Yangc456aac2015-04-23 17:17:51 +08002090 nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!nfsi)
2092 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07002093 nfsi->flags = 0UL;
2094 nfsi->cache_validity = 0UL;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002095#if IS_ENABLED(CONFIG_NFS_V4)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002096 nfsi->nfs4_acl = NULL;
2097#endif /* CONFIG_NFS_V4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return &nfsi->vfs_inode;
2099}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002100EXPORT_SYMBOL_GPL(nfs_alloc_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Al Viroca1a1992019-04-15 20:19:40 -04002102void nfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002103{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002104 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2105}
Al Viroca1a1992019-04-15 20:19:40 -04002106EXPORT_SYMBOL_GPL(nfs_free_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Andrew Mortond75d5412006-06-25 02:41:26 -07002108static inline void nfs4_init_once(struct nfs_inode *nfsi)
2109{
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002110#if IS_ENABLED(CONFIG_NFS_V4)
Andrew Mortond75d5412006-06-25 02:41:26 -07002111 INIT_LIST_HEAD(&nfsi->open_states);
2112 nfsi->delegation = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002113 init_rwsem(&nfsi->rwsem);
Benny Halevye5e94012010-10-20 00:18:01 -04002114 nfsi->layout = NULL;
Andrew Mortond75d5412006-06-25 02:41:26 -07002115#endif
2116}
David Howellsf7b422b2006-06-09 09:34:33 -04002117
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002118static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2121
Christoph Lametera35afb82007-05-16 22:10:57 -07002122 inode_init_once(&nfsi->vfs_inode);
Christoph Lametera35afb82007-05-16 22:10:57 -07002123 INIT_LIST_HEAD(&nfsi->open_files);
2124 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
2125 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
Fred Isamanea2cf222012-04-20 14:47:53 -04002126 INIT_LIST_HEAD(&nfsi->commit_info.list);
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04002127 atomic_long_set(&nfsi->nrequests, 0);
Trond Myklebust5cb953d2017-08-01 17:04:12 -04002128 atomic_long_set(&nfsi->commit_info.ncommit, 0);
Trond Myklebust1a0de482012-06-19 18:38:56 -04002129 atomic_set(&nfsi->commit_info.rpcs_out, 0);
Al Viro884be172016-04-28 23:56:31 -04002130 init_rwsem(&nfsi->rmdir_sem);
Trond Myklebuste824f992017-08-01 11:53:49 -04002131 mutex_init(&nfsi->commit_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -07002132 nfs4_init_once(nfsi);
Trond Myklebustefeda802020-02-05 09:01:54 -05002133 nfsi->cache_change_attribute = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
Paul Mundt20c2df82007-07-20 10:11:58 +09002135
David Howellsf7b422b2006-06-09 09:34:33 -04002136static int __init nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2139 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002140 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08002141 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09002142 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (nfs_inode_cachep == NULL)
2144 return -ENOMEM;
2145
2146 return 0;
2147}
2148
David Brownell266bee82006-06-27 12:59:15 -07002149static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10002151 /*
2152 * Make sure all delayed rcu free inodes are flushed before we
2153 * destroy cache.
2154 */
2155 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002156 kmem_cache_destroy(nfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Trond Myklebust57460062008-02-19 20:04:22 -05002159struct workqueue_struct *nfsiod_workqueue;
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002160EXPORT_SYMBOL_GPL(nfsiod_workqueue);
Trond Myklebust57460062008-02-19 20:04:22 -05002161
2162/*
2163 * start up the nfsiod workqueue
2164 */
2165static int nfsiod_start(void)
2166{
2167 struct workqueue_struct *wq;
2168 dprintk("RPC: creating workqueue nfsiod\n");
Tejun Heoada609e2011-01-25 14:35:54 +01002169 wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
Trond Myklebust57460062008-02-19 20:04:22 -05002170 if (wq == NULL)
2171 return -ENOMEM;
2172 nfsiod_workqueue = wq;
2173 return 0;
2174}
2175
2176/*
2177 * Destroy the nfsiod workqueue
2178 */
2179static void nfsiod_stop(void)
2180{
2181 struct workqueue_struct *wq;
2182
2183 wq = nfsiod_workqueue;
2184 if (wq == NULL)
2185 return;
2186 nfsiod_workqueue = NULL;
2187 destroy_workqueue(wq);
2188}
2189
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +03002190unsigned int nfs_net_id;
Stanislav Kinsbursky9e2e74d2012-01-10 17:04:24 +04002191EXPORT_SYMBOL_GPL(nfs_net_id);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002192
2193static int nfs_net_init(struct net *net)
2194{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00002195 nfs_clients_init(net);
Eric W. Biederman65b38852014-07-31 04:35:20 -07002196 return nfs_fs_proc_net_init(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002197}
2198
2199static void nfs_net_exit(struct net *net)
2200{
Eric W. Biederman65b38852014-07-31 04:35:20 -07002201 nfs_fs_proc_net_exit(net);
Trond Myklebust10b7a702019-02-06 09:32:50 -05002202 nfs_clients_exit(net);
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002203}
2204
2205static struct pernet_operations nfs_net_ops = {
2206 .init = nfs_net_init,
2207 .exit = nfs_net_exit,
2208 .id = &nfs_net_id,
2209 .size = sizeof(struct nfs_net),
2210};
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/*
2213 * Initialize NFS
2214 */
2215static int __init init_nfs_fs(void)
2216{
2217 int err;
2218
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002219 err = nfs_sysfs_init();
2220 if (err < 0)
2221 goto out10;
2222
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002223 err = register_pernet_subsys(&nfs_net_ops);
2224 if (err < 0)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002225 goto out9;
Trond Myklebuste571cbf2009-08-19 18:12:27 -04002226
David Howells8ec442a2009-04-03 16:42:42 +01002227 err = nfs_fscache_register();
2228 if (err < 0)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002229 goto out8;
David Howells8ec442a2009-04-03 16:42:42 +01002230
Trond Myklebust57460062008-02-19 20:04:22 -05002231 err = nfsiod_start();
2232 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002233 goto out7;
Trond Myklebust57460062008-02-19 20:04:22 -05002234
David Howells6aaca562006-08-22 20:06:13 -04002235 err = nfs_fs_proc_init();
2236 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002237 goto out6;
David Howells6aaca562006-08-22 20:06:13 -04002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 err = nfs_init_nfspagecache();
2240 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002241 goto out5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 err = nfs_init_inodecache();
2244 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002245 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 err = nfs_init_readpagecache();
2248 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002249 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 err = nfs_init_writepagecache();
2252 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002253 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 err = nfs_init_directcache();
2256 if (err)
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002257 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002259 rpc_proc_register(&init_net, &nfs_rpcstat);
Kinglong Meecd738ee2015-07-01 11:58:31 +08002260
2261 err = register_nfs_fs();
2262 if (err)
Bryan Schumaker129d1972012-07-16 16:39:13 -04002263 goto out0;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
Bryan Schumaker129d1972012-07-16 16:39:13 -04002266out0:
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002267 rpc_proc_unregister(&init_net, "nfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 nfs_destroy_directcache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002269out1:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002270 nfs_destroy_writepagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002271out2:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002272 nfs_destroy_readpagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002273out3:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002274 nfs_destroy_inodecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002275out4:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002276 nfs_destroy_nfspagecache();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002277out5:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002278 nfs_fs_proc_exit();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002279out6:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002280 nfsiod_stop();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002281out7:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002282 nfs_fscache_unregister();
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002283out8:
Bryan Schumaker129d1972012-07-16 16:39:13 -04002284 unregister_pernet_subsys(&nfs_net_ops);
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002285out9:
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002286 nfs_sysfs_exit();
2287out10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return err;
2289}
2290
2291static void __exit exit_nfs_fs(void)
2292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 nfs_destroy_directcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 nfs_destroy_writepagecache();
2295 nfs_destroy_readpagecache();
2296 nfs_destroy_inodecache();
2297 nfs_destroy_nfspagecache();
David Howells8ec442a2009-04-03 16:42:42 +01002298 nfs_fscache_unregister();
Stanislav Kinsbursky1b340d02011-11-25 17:13:04 +03002299 unregister_pernet_subsys(&nfs_net_ops);
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +03002300 rpc_proc_unregister(&init_net, "nfs");
David Howellsf7b422b2006-06-09 09:34:33 -04002301 unregister_nfs_fs();
David Howells6aaca562006-08-22 20:06:13 -04002302 nfs_fs_proc_exit();
Trond Myklebust57460062008-02-19 20:04:22 -05002303 nfsiod_stop();
Trond Myklebust996bc4f2019-01-24 16:10:46 -05002304 nfs_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307/* Not quite true; I just maintain it */
2308MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2309MODULE_LICENSE("GPL");
Trond Myklebustf43bf0b2007-10-09 12:01:04 -04002310module_param(enable_ino64, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312module_init(init_nfs_fs)
2313module_exit(exit_nfs_fs)