]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/inode.c
NFS: Allow getattr to also report readdirplus cache hits
[mirror_ubuntu-artful-kernel.git] / fs / nfs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/inode.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs inode and superblock handling functions
7 *
526719ba 8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
1da177e4
LT
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 */
15
1da177e4
LT
16#include <linux/module.h>
17#include <linux/init.h>
e8edc6e0 18#include <linux/sched.h>
1da177e4
LT
19#include <linux/time.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/unistd.h>
26#include <linux/sunrpc/clnt.h>
27#include <linux/sunrpc/stats.h>
4ece3a2d 28#include <linux/sunrpc/metrics.h>
1da177e4
LT
29#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/nfs4_mount.h>
32#include <linux/lockd/bind.h>
1da177e4
LT
33#include <linux/seq_file.h>
34#include <linux/mount.h>
1da177e4 35#include <linux/vfs.h>
9cdb3883
MN
36#include <linux/inet.h>
37#include <linux/nfs_xdr.h>
5a0e3ad6 38#include <linux/slab.h>
3fa0b4e2 39#include <linux/compat.h>
d310310c 40#include <linux/freezer.h>
1da177e4 41
1da177e4
LT
42#include <asm/uaccess.h>
43
4ce79717 44#include "nfs4_fs.h"
a72b4422 45#include "callback.h"
1da177e4 46#include "delegation.h"
d9ef5a8c 47#include "iostat.h"
f7b422b1 48#include "internal.h"
8ec442ae 49#include "fscache.h"
e5e94017 50#include "pnfs.h"
ab7017a3 51#include "nfs.h"
1b340d01 52#include "netns.h"
1da177e4 53
f4ce1299
TM
54#include "nfstrace.h"
55
1da177e4 56#define NFSDBG_FACILITY NFSDBG_VFS
1da177e4 57
f43bf0be
TM
58#define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
59
60/* Default is to see 64-bit inode numbers */
90ab5ee9 61static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
f43bf0be 62
1da177e4 63static void nfs_invalidate_inode(struct inode *);
24aa1fe6 64static int nfs_update_inode(struct inode *, struct nfs_fattr *);
1da177e4 65
e18b890b 66static struct kmem_cache * nfs_inode_cachep;
b7fa0554 67
1da177e4
LT
68static inline unsigned long
69nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
70{
71 return nfs_fileid_to_ino_t(fattr->fileid);
72}
73
210c7c17 74static int nfs_wait_killable(int mode)
72cb77f4 75{
416ad3c9 76 freezable_schedule_unsafe();
dfd01f02
PZ
77 if (signal_pending_state(mode, current))
78 return -ERESTARTSYS;
72cb77f4
TM
79 return 0;
80}
210c7c17
BC
81
82int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
83{
84 return nfs_wait_killable(mode);
85}
89d77c8f 86EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
72cb77f4 87
210c7c17
BC
88int nfs_wait_atomic_killable(atomic_t *p)
89{
90 return nfs_wait_killable(TASK_KILLABLE);
91}
92
f43bf0be
TM
93/**
94 * nfs_compat_user_ino64 - returns the user-visible inode number
95 * @fileid: 64-bit fileid
96 *
97 * This function returns a 32-bit inode number if the boot parameter
98 * nfs.enable_ino64 is zero.
99 */
100u64 nfs_compat_user_ino64(u64 fileid)
101{
3fa0b4e2
FF
102#ifdef CONFIG_COMPAT
103 compat_ulong_t ino;
104#else
105 unsigned long ino;
106#endif
f43bf0be
TM
107
108 if (enable_ino64)
109 return fileid;
110 ino = fileid;
111 if (sizeof(ino) < sizeof(fileid))
112 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
113 return ino;
114}
115
eed99357
TM
116int nfs_drop_inode(struct inode *inode)
117{
118 return NFS_STALE(inode) || generic_drop_inode(inode);
119}
120EXPORT_SYMBOL_GPL(nfs_drop_inode);
121
19d87ca3 122void nfs_clear_inode(struct inode *inode)
1da177e4 123{
da6d503a
TM
124 /*
125 * The following should never happen...
126 */
f48407dd
TM
127 WARN_ON_ONCE(nfs_have_writebacks(inode));
128 WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files));
ada70d94 129 nfs_zap_acl_cache(inode);
1c3c07e9 130 nfs_access_zap_cache(inode);
f1fe29b4 131 nfs_fscache_clear_inode(inode);
1da177e4 132}
89d77c8f 133EXPORT_SYMBOL_GPL(nfs_clear_inode);
1da177e4 134
b57922d9
AV
135void nfs_evict_inode(struct inode *inode)
136{
91b0abe3 137 truncate_inode_pages_final(&inode->i_data);
dbd5768f 138 clear_inode(inode);
b57922d9
AV
139 nfs_clear_inode(inode);
140}
141
9e1681c2 142int nfs_sync_inode(struct inode *inode)
4d346bea 143{
95d9f6c3 144 inode_dio_wait(inode);
4d346bea
TM
145 return nfs_wb_all(inode);
146}
9e1681c2 147EXPORT_SYMBOL_GPL(nfs_sync_inode);
4d346bea 148
29884df0
TM
149/**
150 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
151 */
152int nfs_sync_mapping(struct address_space *mapping)
153{
5cf95214 154 int ret = 0;
29884df0 155
5cf95214
TM
156 if (mapping->nrpages != 0) {
157 unmap_mapping_range(mapping, 0, 0, 0);
158 ret = nfs_wb_all(mapping->host);
159 }
29884df0
TM
160 return ret;
161}
162
6edf9609
TM
163static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags)
164{
165 struct nfs_inode *nfsi = NFS_I(inode);
166
167 if (inode->i_mapping->nrpages == 0)
168 flags &= ~NFS_INO_INVALID_DATA;
169 nfsi->cache_validity |= flags;
170 if (flags & NFS_INO_INVALID_DATA)
171 nfs_fscache_invalidate(inode);
172}
173
1da177e4
LT
174/*
175 * Invalidate the local caches
176 */
b37b03b7 177static void nfs_zap_caches_locked(struct inode *inode)
1da177e4
LT
178{
179 struct nfs_inode *nfsi = NFS_I(inode);
180 int mode = inode->i_mode;
181
91d5b470
CL
182 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
183
c7c20973
TM
184 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
185 nfsi->attrtimeo_timestamp = jiffies;
1da177e4 186
c3f52af3 187 memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf));
de242c0b 188 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
6edf9609 189 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
aa9c2669
DQ
190 | NFS_INO_INVALID_DATA
191 | NFS_INO_INVALID_ACCESS
192 | NFS_INO_INVALID_ACL
6edf9609 193 | NFS_INO_REVAL_PAGECACHE);
aa9c2669 194 } else
6edf9609 195 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
aa9c2669
DQ
196 | NFS_INO_INVALID_ACCESS
197 | NFS_INO_INVALID_ACL
6edf9609 198 | NFS_INO_REVAL_PAGECACHE);
fd1defc2 199 nfs_zap_label_cache_locked(nfsi);
b37b03b7 200}
dc59250c 201
b37b03b7
TM
202void nfs_zap_caches(struct inode *inode)
203{
204 spin_lock(&inode->i_lock);
205 nfs_zap_caches_locked(inode);
dc59250c 206 spin_unlock(&inode->i_lock);
ada70d94
TM
207}
208
cd9ae2b6
TM
209void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
210{
211 if (mapping->nrpages != 0) {
212 spin_lock(&inode->i_lock);
6edf9609 213 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
cd9ae2b6
TM
214 spin_unlock(&inode->i_lock);
215 }
216}
217
f41f7418 218void nfs_zap_acl_cache(struct inode *inode)
ada70d94
TM
219{
220 void (*clear_acl_cache)(struct inode *);
221
222 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
223 if (clear_acl_cache != NULL)
224 clear_acl_cache(inode);
dc59250c 225 spin_lock(&inode->i_lock);
55296809 226 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
dc59250c 227 spin_unlock(&inode->i_lock);
1da177e4 228}
1c606fb7 229EXPORT_SYMBOL_GPL(nfs_zap_acl_cache);
1da177e4 230
c4812998
TM
231void nfs_invalidate_atime(struct inode *inode)
232{
233 spin_lock(&inode->i_lock);
6edf9609 234 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME);
c4812998
TM
235 spin_unlock(&inode->i_lock);
236}
ddda8e0a 237EXPORT_SYMBOL_GPL(nfs_invalidate_atime);
c4812998 238
1da177e4 239/*
b37b03b7
TM
240 * Invalidate, but do not unhash, the inode.
241 * NB: must be called with inode->i_lock held!
1da177e4 242 */
b37b03b7 243static void nfs_invalidate_inode(struct inode *inode)
1da177e4 244{
3a10c30a 245 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
b37b03b7 246 nfs_zap_caches_locked(inode);
1da177e4
LT
247}
248
249struct nfs_find_desc {
250 struct nfs_fh *fh;
251 struct nfs_fattr *fattr;
252};
253
254/*
255 * In NFSv3 we can have 64bit inode numbers. In order to support
256 * this, and re-exported directories (also seen in NFSv2)
257 * we are forced to allow 2 different inodes to have the same
258 * i_ino.
259 */
260static int
261nfs_find_actor(struct inode *inode, void *opaque)
262{
263 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
264 struct nfs_fh *fh = desc->fh;
265 struct nfs_fattr *fattr = desc->fattr;
266
267 if (NFS_FILEID(inode) != fattr->fileid)
268 return 0;
f6488c9b
JL
269 if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode))
270 return 0;
1da177e4
LT
271 if (nfs_compare_fh(NFS_FH(inode), fh))
272 return 0;
273 if (is_bad_inode(inode) || NFS_STALE(inode))
274 return 0;
275 return 1;
276}
277
278static int
279nfs_init_locked(struct inode *inode, void *opaque)
280{
281 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
282 struct nfs_fattr *fattr = desc->fattr;
283
99fadcd7 284 set_nfs_fileid(inode, fattr->fileid);
916ec34d 285 inode->i_mode = fattr->mode;
1da177e4
LT
286 nfs_copy_fh(NFS_FH(inode), desc->fh);
287 return 0;
288}
289
e058f70b 290#ifdef CONFIG_NFS_V4_SECURITY_LABEL
fd1defc2
TM
291static void nfs_clear_label_invalid(struct inode *inode)
292{
293 spin_lock(&inode->i_lock);
294 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL;
295 spin_unlock(&inode->i_lock);
296}
297
aa9c2669
DQ
298void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
299 struct nfs4_label *label)
300{
301 int error;
302
303 if (label == NULL)
304 return;
305
aa9c2669
DQ
306 if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) {
307 error = security_inode_notifysecctx(inode, label->label,
308 label->len);
309 if (error)
310 printk(KERN_ERR "%s() %s %d "
311 "security_inode_notifysecctx() %d\n",
312 __func__,
313 (char *)label->label,
314 label->len, error);
fd1defc2 315 nfs_clear_label_invalid(inode);
aa9c2669
DQ
316 }
317}
318
e058f70b
SD
319struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags)
320{
321 struct nfs4_label *label = NULL;
322 int minor_version = server->nfs_client->cl_minorversion;
323
324 if (minor_version < 2)
325 return label;
326
327 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
328 return label;
329
330 label = kzalloc(sizeof(struct nfs4_label), flags);
331 if (label == NULL)
332 return ERR_PTR(-ENOMEM);
333
334 label->label = kzalloc(NFS4_MAXLABELLEN, flags);
335 if (label->label == NULL) {
336 kfree(label);
337 return ERR_PTR(-ENOMEM);
338 }
339 label->len = NFS4_MAXLABELLEN;
340
341 return label;
342}
343EXPORT_SYMBOL_GPL(nfs4_label_alloc);
aa9c2669 344#else
829e57d7 345void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr,
aa9c2669
DQ
346 struct nfs4_label *label)
347{
348}
e058f70b 349#endif
aa9c2669 350EXPORT_SYMBOL_GPL(nfs_setsecurity);
e058f70b 351
1da177e4
LT
352/*
353 * This is our front-end to iget that looks up inodes by file handle
354 * instead of inode number.
355 */
356struct inode *
1775fd3e 357nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label)
1da177e4
LT
358{
359 struct nfs_find_desc desc = {
360 .fh = fh,
361 .fattr = fattr
362 };
03f28e3a 363 struct inode *inode = ERR_PTR(-ENOENT);
1da177e4
LT
364 unsigned long hash;
365
7ebb9315
BS
366 nfs_attr_check_mountpoint(sb, fattr);
367
2ef47eb1
AS
368 if (nfs_attr_use_mounted_on_fileid(fattr))
369 fattr->fileid = fattr->mounted_on_fileid;
370 else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
1da177e4 371 goto out_no_inode;
9e6e70f8 372 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
1da177e4 373 goto out_no_inode;
1da177e4
LT
374
375 hash = nfs_fattr_to_ino_t(fattr);
376
03f28e3a
TM
377 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
378 if (inode == NULL) {
379 inode = ERR_PTR(-ENOMEM);
1da177e4 380 goto out_no_inode;
03f28e3a 381 }
1da177e4
LT
382
383 if (inode->i_state & I_NEW) {
384 struct nfs_inode *nfsi = NFS_I(inode);
b0c4fddc 385 unsigned long now = jiffies;
1da177e4
LT
386
387 /* We set i_ino for the few things that still rely on it,
388 * such as stat(2) */
389 inode->i_ino = hash;
390
391 /* We can't support update_atime(), since the server will reset it */
392 inode->i_flags |= S_NOATIME|S_NOCMTIME;
393 inode->i_mode = fattr->mode;
62ab460c
TM
394 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
395 && nfs_server_capable(inode, NFS_CAP_MODE))
6edf9609 396 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
1da177e4
LT
397 /* Why so? Because we want revalidate for devices/FIFOs, and
398 * that's precisely what we have in nfs_file_inode_operations.
399 */
8fa5c000 400 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
1da177e4 401 if (S_ISREG(inode->i_mode)) {
1788ea6e 402 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
1da177e4 403 inode->i_data.a_ops = &nfs_file_aops;
1da177e4 404 } else if (S_ISDIR(inode->i_mode)) {
8fa5c000 405 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
1da177e4 406 inode->i_fop = &nfs_dir_operations;
11de3b11 407 inode->i_data.a_ops = &nfs_dir_aops;
55a97593 408 /* Deal with crossing mountpoints */
7ebb9315
BS
409 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
410 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
6b97fd3d
MN
411 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
412 inode->i_op = &nfs_referral_inode_operations;
413 else
414 inode->i_op = &nfs_mountpoint_inode_operations;
55a97593 415 inode->i_fop = NULL;
36d43a43 416 inode->i_flags |= S_AUTOMOUNT;
55a97593 417 }
21fc61c7 418 } else if (S_ISLNK(inode->i_mode)) {
1da177e4 419 inode->i_op = &nfs_symlink_inode_operations;
21fc61c7
AV
420 inode_nohighmem(inode);
421 } else
1da177e4
LT
422 init_special_inode(inode, inode->i_mode, fattr->rdev);
423
9e6e70f8
TM
424 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
425 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
426 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
a9a4a87a 427 inode->i_version = 0;
9e6e70f8 428 inode->i_size = 0;
6d6b77f1 429 clear_nlink(inode);
9ff593c4
EB
430 inode->i_uid = make_kuid(&init_user_ns, -2);
431 inode->i_gid = make_kgid(&init_user_ns, -2);
9e6e70f8
TM
432 inode->i_blocks = 0;
433 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
2701d086
AA
434 nfsi->write_io = 0;
435 nfsi->read_io = 0;
9e6e70f8 436
33801147 437 nfsi->read_cache_jiffies = fattr->time_start;
4704f0e2 438 nfsi->attr_gencount = fattr->gencount;
9e6e70f8
TM
439 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
440 inode->i_atime = fattr->atime;
62ab460c 441 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
6edf9609 442 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8
TM
443 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
444 inode->i_mtime = fattr->mtime;
62ab460c 445 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
6edf9609 446 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8
TM
447 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
448 inode->i_ctime = fattr->ctime;
62ab460c 449 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
6edf9609 450 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8 451 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
a9a4a87a 452 inode->i_version = fattr->change_attr;
cd812599 453 else
5c675d64
TM
454 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
455 | NFS_INO_REVAL_PAGECACHE);
9e6e70f8
TM
456 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
457 inode->i_size = nfs_size_to_loff_t(fattr->size);
62ab460c 458 else
6edf9609
TM
459 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR
460 | NFS_INO_REVAL_PAGECACHE);
9e6e70f8 461 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
bfe86848 462 set_nlink(inode, fattr->nlink);
62ab460c 463 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
6edf9609 464 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8
TM
465 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
466 inode->i_uid = fattr->uid;
62ab460c 467 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
6edf9609 468 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8
TM
469 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
470 inode->i_gid = fattr->gid;
62ab460c 471 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
6edf9609 472 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR);
9e6e70f8
TM
473 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
474 inode->i_blocks = fattr->du.nfs2.blocks;
475 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1da177e4
LT
476 /*
477 * report the blocks in 512byte units
478 */
479 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1da177e4 480 }
aa9c2669
DQ
481
482 nfs_setsecurity(inode, fattr, label);
483
1da177e4 484 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
b0c4fddc 485 nfsi->attrtimeo_timestamp = now;
1c3c07e9 486 nfsi->access_cache = RB_ROOT;
1da177e4 487
f1fe29b4 488 nfs_fscache_init_inode(inode);
ef79c097 489
1da177e4
LT
490 unlock_new_inode(inode);
491 } else
492 nfs_refresh_inode(inode, fattr);
1e8968c5 493 dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n",
1da177e4 494 inode->i_sb->s_id,
1e8968c5 495 (unsigned long long)NFS_FILEID(inode),
4f1abd22 496 nfs_display_fhandle_hash(fh),
1da177e4
LT
497 atomic_read(&inode->i_count));
498
499out:
500 return inode;
501
502out_no_inode:
03f28e3a 503 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
1da177e4
LT
504 goto out;
505}
89d77c8f 506EXPORT_SYMBOL_GPL(nfs_fhget);
1da177e4 507
536e43d1 508#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)
1da177e4
LT
509
510int
511nfs_setattr(struct dentry *dentry, struct iattr *attr)
512{
2b0143b5 513 struct inode *inode = d_inode(dentry);
987f8dfc 514 struct nfs_fattr *fattr;
ae57ca0f 515 int error = 0;
1da177e4 516
91d5b470
CL
517 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
518
188b95dd
JL
519 /* skip mode change if it's just for clearing setuid/setgid */
520 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
521 attr->ia_valid &= ~ATTR_MODE;
522
1da177e4 523 if (attr->ia_valid & ATTR_SIZE) {
08a899d5
CH
524 BUG_ON(!S_ISREG(inode->i_mode));
525
ae57ca0f
KM
526 error = inode_newsize_ok(inode, attr->ia_size);
527 if (error)
528 return error;
529
530 if (attr->ia_size == i_size_read(inode))
1da177e4
LT
531 attr->ia_valid &= ~ATTR_SIZE;
532 }
533
534 /* Optimization: if the end result is no change, don't RPC */
535 attr->ia_valid &= NFS_VALID_ATTRS;
536e43d1 536 if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
1da177e4
LT
537 return 0;
538
f4ce1299
TM
539 trace_nfs_setattr_enter(inode);
540
755c1e20 541 /* Write all dirty data */
4d346bea
TM
542 if (S_ISREG(inode->i_mode))
543 nfs_sync_inode(inode);
987f8dfc
TM
544
545 fattr = nfs_alloc_fattr();
ae57ca0f
KM
546 if (fattr == NULL) {
547 error = -ENOMEM;
987f8dfc 548 goto out;
ae57ca0f
KM
549 }
550
642ac549
TM
551 /*
552 * Return any delegations if we're going to change ACLs
553 */
554 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
57ec14c5 555 NFS_PROTO(inode)->return_delegation(inode);
987f8dfc 556 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
65e4308d 557 if (error == 0)
aa9c2669 558 error = nfs_refresh_inode(inode, fattr);
987f8dfc
TM
559 nfs_free_fattr(fattr);
560out:
f4ce1299 561 trace_nfs_setattr_exit(inode, error);
65e4308d
TM
562 return error;
563}
ddda8e0a 564EXPORT_SYMBOL_GPL(nfs_setattr);
65e4308d 565
a3d01454
TM
566/**
567 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
568 * @inode: inode of the file used
569 * @offset: file offset to start truncating
570 *
571 * This is a copy of the common vmtruncate, but with the locking
572 * corrected to take into account the fact that NFS requires
573 * inode->i_size to be updated under the inode->i_lock.
f044636d 574 * Note: must be called with inode->i_lock held!
a3d01454
TM
575 */
576static int nfs_vmtruncate(struct inode * inode, loff_t offset)
577{
c08d3b0e 578 int err;
a3d01454 579
c08d3b0e 580 err = inode_newsize_ok(inode, offset);
581 if (err)
582 goto out;
a3d01454 583
c08d3b0e 584 i_size_write(inode, offset);
6edf9609
TM
585 /* Optimisation */
586 if (offset == 0)
587 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA;
c08d3b0e 588
f044636d 589 spin_unlock(&inode->i_lock);
7caef267 590 truncate_pagecache(inode, offset);
f044636d 591 spin_lock(&inode->i_lock);
c08d3b0e 592out:
593 return err;
a3d01454
TM
594}
595
65e4308d
TM
596/**
597 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
598 * @inode: pointer to struct inode
599 * @attr: pointer to struct iattr
600 *
601 * Note: we do this in the *proc.c in order to ensure that
602 * it works for things like exclusive creates too.
603 */
f044636d
TM
604void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
605 struct nfs_fattr *fattr)
65e4308d 606{
f044636d
TM
607 /* Barrier: bump the attribute generation count. */
608 nfs_fattr_set_barrier(fattr);
609
610 spin_lock(&inode->i_lock);
611 NFS_I(inode)->attr_gencount = fattr->gencount;
65e4308d 612 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
1da177e4 613 if ((attr->ia_valid & ATTR_MODE) != 0) {
65e4308d
TM
614 int mode = attr->ia_mode & S_IALLUGO;
615 mode |= inode->i_mode & ~S_IALLUGO;
1da177e4
LT
616 inode->i_mode = mode;
617 }
618 if ((attr->ia_valid & ATTR_UID) != 0)
619 inode->i_uid = attr->ia_uid;
620 if ((attr->ia_valid & ATTR_GID) != 0)
621 inode->i_gid = attr->ia_gid;
6edf9609
TM
622 nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS
623 | NFS_INO_INVALID_ACL);
65e4308d
TM
624 }
625 if ((attr->ia_valid & ATTR_SIZE) != 0) {
91d5b470 626 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
a3d01454 627 nfs_vmtruncate(inode, attr->ia_size);
65e4308d 628 }
616c3196
JL
629 if (fattr->valid)
630 nfs_update_inode(inode, fattr);
631 else
632 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
f044636d 633 spin_unlock(&inode->i_lock);
1da177e4 634}
ddda8e0a 635EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
1da177e4 636
1bcf4c5c 637static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
311324ad
TM
638{
639 struct dentry *parent;
640
1bcf4c5c
TM
641 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
642 return;
311324ad 643 parent = dget_parent(dentry);
2b0143b5 644 nfs_force_use_readdirplus(d_inode(parent));
311324ad
TM
645 dput(parent);
646}
647
1bcf4c5c
TM
648static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
649{
650 struct dentry *parent;
651
652 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
653 return;
654 parent = dget_parent(dentry);
655 nfs_advise_use_readdirplus(d_inode(parent));
656 dput(parent);
657}
658
311324ad
TM
659static bool nfs_need_revalidate_inode(struct inode *inode)
660{
661 if (NFS_I(inode)->cache_validity &
662 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
663 return true;
664 if (nfs_attribute_cache_expired(inode))
665 return true;
666 return false;
667}
668
1da177e4
LT
669int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
670{
2b0143b5 671 struct inode *inode = d_inode(dentry);
55296809 672 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
16caf5b6 673 int err = 0;
1da177e4 674
f4ce1299 675 trace_nfs_getattr_enter(inode);
acdc53b2 676 /* Flush out writes to the server in order to update c/mtime. */
28c494c5 677 if (S_ISREG(inode->i_mode)) {
79566ef0 678 err = filemap_write_and_wait(inode->i_mapping);
acdc53b2
TM
679 if (err)
680 goto out;
28c494c5 681 }
fc33a7bb
CH
682
683 /*
684 * We may force a getattr if the user cares about atime.
685 *
686 * Note that we only have to check the vfsmount flags here:
687 * - NFS always sets S_NOATIME by so checking it would give a
688 * bogus result
689 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
690 * no point in checking those.
691 */
692 if ((mnt->mnt_flags & MNT_NOATIME) ||
693 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
1da177e4 694 need_atime = 0;
fc33a7bb 695
311324ad
TM
696 if (need_atime || nfs_need_revalidate_inode(inode)) {
697 struct nfs_server *server = NFS_SERVER(inode);
698
1bcf4c5c 699 nfs_readdirplus_parent_cache_miss(dentry);
311324ad 700 err = __nfs_revalidate_inode(server, inode);
1bcf4c5c
TM
701 } else
702 nfs_readdirplus_parent_cache_hit(dentry);
4e769b93 703 if (!err) {
1da177e4 704 generic_fillattr(inode, stat);
f43bf0be 705 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
7ef5ca4f
N
706 if (S_ISDIR(inode->i_mode))
707 stat->blksize = NFS_SERVER(inode)->dtsize;
4e769b93 708 }
acdc53b2 709out:
f4ce1299 710 trace_nfs_getattr_exit(inode, err);
1da177e4
LT
711 return err;
712}
ddda8e0a 713EXPORT_SYMBOL_GPL(nfs_getattr);
1da177e4 714
f11ac8db
TM
715static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
716{
717 atomic_set(&l_ctx->count, 1);
d51fdb87 718 l_ctx->lockowner = current->files;
f11ac8db 719 INIT_LIST_HEAD(&l_ctx->list);
210c7c17 720 atomic_set(&l_ctx->io_count, 0);
f11ac8db
TM
721}
722
723static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
724{
8c86899f
TM
725 struct nfs_lock_context *head = &ctx->lock_context;
726 struct nfs_lock_context *pos = head;
f11ac8db 727
8c86899f 728 do {
d51fdb87 729 if (pos->lockowner != current->files)
f11ac8db 730 continue;
f11ac8db
TM
731 atomic_inc(&pos->count);
732 return pos;
8c86899f 733 } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
f11ac8db
TM
734 return NULL;
735}
736
737struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
738{
739 struct nfs_lock_context *res, *new = NULL;
2b0143b5 740 struct inode *inode = d_inode(ctx->dentry);
f11ac8db
TM
741
742 spin_lock(&inode->i_lock);
743 res = __nfs_find_lock_context(ctx);
744 if (res == NULL) {
745 spin_unlock(&inode->i_lock);
746 new = kmalloc(sizeof(*new), GFP_KERNEL);
747 if (new == NULL)
b3c54de6 748 return ERR_PTR(-ENOMEM);
f11ac8db
TM
749 nfs_init_lock_context(new);
750 spin_lock(&inode->i_lock);
751 res = __nfs_find_lock_context(ctx);
752 if (res == NULL) {
753 list_add_tail(&new->list, &ctx->lock_context.list);
754 new->open_context = ctx;
755 res = new;
756 new = NULL;
757 }
758 }
759 spin_unlock(&inode->i_lock);
760 kfree(new);
761 return res;
762}
1c6dcbe5 763EXPORT_SYMBOL_GPL(nfs_get_lock_context);
f11ac8db
TM
764
765void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
766{
767 struct nfs_open_context *ctx = l_ctx->open_context;
2b0143b5 768 struct inode *inode = d_inode(ctx->dentry);
f11ac8db
TM
769
770 if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
771 return;
772 list_del(&l_ctx->list);
773 spin_unlock(&inode->i_lock);
774 kfree(l_ctx);
775}
1c6dcbe5 776EXPORT_SYMBOL_GPL(nfs_put_lock_context);
f11ac8db 777
7fe5c398
TM
778/**
779 * nfs_close_context - Common close_context() routine NFSv2/v3
780 * @ctx: pointer to context
781 * @is_sync: is this a synchronous close
782 *
5cf9d706
TM
783 * Ensure that the attributes are up to date if we're mounted
784 * with close-to-open semantics and we have cached data that will
785 * need to be revalidated on open.
7fe5c398
TM
786 */
787void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
788{
5cf9d706 789 struct nfs_inode *nfsi;
7fe5c398
TM
790 struct inode *inode;
791 struct nfs_server *server;
792
793 if (!(ctx->mode & FMODE_WRITE))
794 return;
795 if (!is_sync)
796 return;
2b0143b5 797 inode = d_inode(ctx->dentry);
5cf9d706
TM
798 nfsi = NFS_I(inode);
799 if (inode->i_mapping->nrpages == 0)
800 return;
801 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
802 return;
803 if (!list_empty(&nfsi->open_files))
7fe5c398
TM
804 return;
805 server = NFS_SERVER(inode);
806 if (server->flags & NFS_MOUNT_NOCTO)
807 return;
808 nfs_revalidate_inode(server, inode);
809}
ddda8e0a 810EXPORT_SYMBOL_GPL(nfs_close_context);
7fe5c398 811
532d4def
N
812struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry,
813 fmode_t f_mode,
814 struct file *filp)
1da177e4
LT
815{
816 struct nfs_open_context *ctx;
5ede7b1c
AV
817 struct rpc_cred *cred = rpc_lookup_cred();
818 if (IS_ERR(cred))
819 return ERR_CAST(cred);
1da177e4 820
f52720ca 821 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
5ede7b1c
AV
822 if (!ctx) {
823 put_rpccred(cred);
824 return ERR_PTR(-ENOMEM);
1da177e4 825 }
5ede7b1c
AV
826 nfs_sb_active(dentry->d_sb);
827 ctx->dentry = dget(dentry);
828 ctx->cred = cred;
829 ctx->state = NULL;
830 ctx->mode = f_mode;
831 ctx->flags = 0;
832 ctx->error = 0;
532d4def 833 ctx->flock_owner = (fl_owner_t)filp;
5ede7b1c
AV
834 nfs_init_lock_context(&ctx->lock_context);
835 ctx->lock_context.open_context = ctx;
836 INIT_LIST_HEAD(&ctx->list);
82be417a 837 ctx->mdsthreshold = NULL;
1da177e4
LT
838 return ctx;
839}
89d77c8f 840EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
1da177e4
LT
841
842struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
843{
844 if (ctx != NULL)
f11ac8db 845 atomic_inc(&ctx->lock_context.count);
1da177e4
LT
846 return ctx;
847}
89d77c8f 848EXPORT_SYMBOL_GPL(get_nfs_open_context);
1da177e4 849
7fe5c398 850static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1da177e4 851{
2b0143b5 852 struct inode *inode = d_inode(ctx->dentry);
3d4ff43d 853 struct super_block *sb = ctx->dentry->d_sb;
3bec63db 854
5c78f58e 855 if (!list_empty(&ctx->list)) {
ef84303e
BH
856 if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
857 return;
858 list_del(&ctx->list);
859 spin_unlock(&inode->i_lock);
ef84303e 860 } else if (!atomic_dec_and_test(&ctx->lock_context.count))
5e11934d 861 return;
5c78f58e
TM
862 if (inode != NULL)
863 NFS_PROTO(inode)->close_context(ctx, is_sync);
3bec63db
TM
864 if (ctx->cred != NULL)
865 put_rpccred(ctx->cred);
3d4ff43d 866 dput(ctx->dentry);
322b2b90 867 nfs_sb_deactive(sb);
82be417a 868 kfree(ctx->mdsthreshold);
3bec63db
TM
869 kfree(ctx);
870}
871
a49c3c77
TM
872void put_nfs_open_context(struct nfs_open_context *ctx)
873{
874 __put_nfs_open_context(ctx, 0);
875}
89d77c8f 876EXPORT_SYMBOL_GPL(put_nfs_open_context);
a49c3c77 877
4eae5014
TM
878static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
879{
880 __put_nfs_open_context(ctx, 1);
881}
882
1da177e4
LT
883/*
884 * Ensure that mmap has a recent RPC credential for use when writing out
885 * shared pages
886 */
c45ffdd2 887void nfs_inode_attach_open_context(struct nfs_open_context *ctx)
1da177e4 888{
2b0143b5 889 struct inode *inode = d_inode(ctx->dentry);
1da177e4
LT
890 struct nfs_inode *nfsi = NFS_I(inode);
891
1da177e4 892 spin_lock(&inode->i_lock);
ca0daa27
TM
893 if (ctx->mode & FMODE_WRITE)
894 list_add(&ctx->list, &nfsi->open_files);
895 else
896 list_add_tail(&ctx->list, &nfsi->open_files);
1da177e4
LT
897 spin_unlock(&inode->i_lock);
898}
c45ffdd2
TM
899EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context);
900
901void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
902{
903 filp->private_data = get_nfs_open_context(ctx);
904 if (list_empty(&ctx->list))
905 nfs_inode_attach_open_context(ctx);
906}
89d77c8f 907EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
1da177e4 908
d530838b
TM
909/*
910 * Given an inode, search for an open context with the desired characteristics
911 */
dc0b027d 912struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
1da177e4
LT
913{
914 struct nfs_inode *nfsi = NFS_I(inode);
915 struct nfs_open_context *pos, *ctx = NULL;
916
917 spin_lock(&inode->i_lock);
918 list_for_each_entry(pos, &nfsi->open_files, list) {
d530838b
TM
919 if (cred != NULL && pos->cred != cred)
920 continue;
1544fa0f
TM
921 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
922 continue;
923 ctx = get_nfs_open_context(pos);
924 break;
1da177e4
LT
925 }
926 spin_unlock(&inode->i_lock);
927 return ctx;
928}
929
aff8d8dc 930void nfs_file_clear_open_context(struct file *filp)
1da177e4 931{
cd3758e3 932 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1da177e4
LT
933
934 if (ctx) {
2b0143b5 935 struct inode *inode = d_inode(ctx->dentry);
c45ffdd2 936
0bcbf039
PT
937 /*
938 * We fatal error on write before. Try to writeback
939 * every page again.
940 */
941 if (ctx->error < 0)
942 invalidate_inode_pages2(inode->i_mapping);
1da177e4
LT
943 filp->private_data = NULL;
944 spin_lock(&inode->i_lock);
945 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
946 spin_unlock(&inode->i_lock);
4eae5014 947 put_nfs_open_context_sync(ctx);
1da177e4
LT
948 }
949}
950
951/*
952 * These allocate and release file read/write context information.
953 */
954int nfs_open(struct inode *inode, struct file *filp)
955{
956 struct nfs_open_context *ctx;
1da177e4 957
532d4def 958 ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
5ede7b1c
AV
959 if (IS_ERR(ctx))
960 return PTR_ERR(ctx);
1da177e4
LT
961 nfs_file_set_open_context(filp, ctx);
962 put_nfs_open_context(ctx);
f1fe29b4 963 nfs_fscache_open_file(inode, filp);
1da177e4
LT
964 return 0;
965}
966
1da177e4
LT
967/*
968 * This function is called whenever some part of NFS notices that
969 * the cached attributes have to be refreshed.
970 */
971int
972__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
973{
974 int status = -ESTALE;
1775fd3e 975 struct nfs4_label *label = NULL;
a3cba2aa 976 struct nfs_fattr *fattr = NULL;
1da177e4 977 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4 978
1e8968c5
NV
979 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n",
980 inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode));
1da177e4 981
f4ce1299
TM
982 trace_nfs_revalidate_inode_enter(inode);
983
85233a7a 984 if (is_bad_inode(inode))
691beb13 985 goto out;
1da177e4 986 if (NFS_STALE(inode))
412d582e 987 goto out;
7fdc49c4 988
ac46bd37
TM
989 /* pNFS: Attributes aren't updated until we layoutcommit */
990 if (S_ISREG(inode->i_mode)) {
991 status = pnfs_sync_inode(inode, false);
992 if (status)
993 goto out;
994 }
995
a3cba2aa
TM
996 status = -ENOMEM;
997 fattr = nfs_alloc_fattr();
998 if (fattr == NULL)
999 goto out;
1000
691beb13 1001 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
14c43f76
DQ
1002
1003 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
1004 if (IS_ERR(label)) {
1005 status = PTR_ERR(label);
1006 goto out;
1007 }
1008
1775fd3e 1009 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, label);
1da177e4 1010 if (status != 0) {
1e8968c5 1011 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n",
1da177e4 1012 inode->i_sb->s_id,
1e8968c5 1013 (unsigned long long)NFS_FILEID(inode), status);
1da177e4
LT
1014 if (status == -ESTALE) {
1015 nfs_zap_caches(inode);
1016 if (!S_ISDIR(inode->i_mode))
3a10c30a 1017 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
1da177e4 1018 }
14c43f76 1019 goto err_out;
1da177e4
LT
1020 }
1021
a3cba2aa 1022 status = nfs_refresh_inode(inode, fattr);
1da177e4 1023 if (status) {
1e8968c5 1024 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n",
1da177e4 1025 inode->i_sb->s_id,
1e8968c5 1026 (unsigned long long)NFS_FILEID(inode), status);
14c43f76 1027 goto err_out;
1da177e4 1028 }
55296809 1029
24aa1fe6 1030 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
ada70d94 1031 nfs_zap_acl_cache(inode);
55296809 1032
3da580aa
JL
1033 nfs_setsecurity(inode, fattr, label);
1034
1e8968c5 1035 dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n",
1da177e4 1036 inode->i_sb->s_id,
1e8968c5 1037 (unsigned long long)NFS_FILEID(inode));
1da177e4 1038
14c43f76
DQ
1039err_out:
1040 nfs4_label_free(label);
1041out:
a3cba2aa 1042 nfs_free_fattr(fattr);
f4ce1299 1043 trace_nfs_revalidate_inode_exit(inode, status);
1da177e4
LT
1044 return status;
1045}
1046
1047int nfs_attribute_timeout(struct inode *inode)
1048{
1049 struct nfs_inode *nfsi = NFS_I(inode);
1050
d7cf8dd0
TM
1051 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
1052}
1053
43f291cd 1054int nfs_attribute_cache_expired(struct inode *inode)
d7cf8dd0 1055{
b4d2314b 1056 if (nfs_have_delegated_attributes(inode))
1da177e4 1057 return 0;
d7cf8dd0 1058 return nfs_attribute_timeout(inode);
1da177e4
LT
1059}
1060
1061/**
1062 * nfs_revalidate_inode - Revalidate the inode attributes
1063 * @server - pointer to nfs_server struct
1064 * @inode - pointer to inode struct
1065 *
1066 * Updates inode attribute information by retrieving the data from the server.
1067 */
1068int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1069{
311324ad 1070 if (!nfs_need_revalidate_inode(inode))
1da177e4
LT
1071 return NFS_STALE(inode) ? -ESTALE : 0;
1072 return __nfs_revalidate_inode(server, inode);
1073}
1c606fb7 1074EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
1da177e4 1075
912a108d
N
1076int nfs_revalidate_inode_rcu(struct nfs_server *server, struct inode *inode)
1077{
1078 if (!(NFS_I(inode)->cache_validity &
1079 (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL))
1080 && !nfs_attribute_cache_expired(inode))
1081 return NFS_STALE(inode) ? -ESTALE : 0;
1082 return -ECHILD;
1083}
1084
1cda707d 1085static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
717d44e8
TM
1086{
1087 struct nfs_inode *nfsi = NFS_I(inode);
f8806c84
TM
1088 int ret;
1089
717d44e8 1090 if (mapping->nrpages != 0) {
f8806c84 1091 if (S_ISREG(inode->i_mode)) {
ef070dcb 1092 unmap_mapping_range(mapping, 0, 0, 0);
f8806c84
TM
1093 ret = nfs_sync_mapping(mapping);
1094 if (ret < 0)
1095 return ret;
1096 }
1097 ret = invalidate_inode_pages2(mapping);
717d44e8
TM
1098 if (ret < 0)
1099 return ret;
1100 }
d529ef83
JL
1101 if (S_ISDIR(inode->i_mode)) {
1102 spin_lock(&inode->i_lock);
717d44e8 1103 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
d529ef83
JL
1104 spin_unlock(&inode->i_lock);
1105 }
717d44e8 1106 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
de242c0b 1107 nfs_fscache_wait_on_invalidate(inode);
f4ce1299 1108
1e8968c5
NV
1109 dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n",
1110 inode->i_sb->s_id,
1111 (unsigned long long)NFS_FILEID(inode));
717d44e8
TM
1112 return 0;
1113}
1114
b4b1eadf
TM
1115static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
1116{
1117 if (nfs_have_delegated_attributes(inode))
1118 return false;
1119 return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
1120 || nfs_attribute_timeout(inode)
1121 || NFS_STALE(inode);
1122}
1123
0d0def49
AV
1124int nfs_revalidate_mapping_rcu(struct inode *inode)
1125{
1126 struct nfs_inode *nfsi = NFS_I(inode);
1127 unsigned long *bitlock = &nfsi->flags;
1128 int ret = 0;
1129
1130 if (IS_SWAPFILE(inode))
1131 goto out;
1132 if (nfs_mapping_need_revalidate_inode(inode)) {
1133 ret = -ECHILD;
1134 goto out;
1135 }
1136 spin_lock(&inode->i_lock);
1137 if (test_bit(NFS_INO_INVALIDATING, bitlock) ||
1138 (nfsi->cache_validity & NFS_INO_INVALID_DATA))
1139 ret = -ECHILD;
1140 spin_unlock(&inode->i_lock);
1141out:
1142 return ret;
1143}
1144
717d44e8 1145/**
be527494 1146 * nfs_revalidate_mapping - Revalidate the pagecache
717d44e8
TM
1147 * @inode - pointer to host inode
1148 * @mapping - pointer to mapping
717d44e8 1149 */
be527494
TM
1150int nfs_revalidate_mapping(struct inode *inode,
1151 struct address_space *mapping)
717d44e8
TM
1152{
1153 struct nfs_inode *nfsi = NFS_I(inode);
d529ef83 1154 unsigned long *bitlock = &nfsi->flags;
717d44e8 1155 int ret = 0;
dc59250c 1156
29418aa4
MG
1157 /* swapfiles are not supposed to be shared. */
1158 if (IS_SWAPFILE(inode))
1159 goto out;
1160
b4b1eadf 1161 if (nfs_mapping_need_revalidate_inode(inode)) {
717d44e8
TM
1162 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1163 if (ret < 0)
1164 goto out;
7d52e862 1165 }
d529ef83
JL
1166
1167 /*
1168 * We must clear NFS_INO_INVALID_DATA first to ensure that
1169 * invalidations that come in while we're shooting down the mappings
1170 * are respected. But, that leaves a race window where one revalidator
1171 * can clear the flag, and then another checks it before the mapping
1172 * gets invalidated. Fix that by serializing access to this part of
1173 * the function.
1174 *
1175 * At the same time, we need to allow other tasks to see whether we
1176 * might be in the middle of invalidating the pages, so we only set
1177 * the bit lock here if it looks like we're going to be doing that.
1178 */
1179 for (;;) {
74316201
N
1180 ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
1181 nfs_wait_bit_killable, TASK_KILLABLE);
d529ef83
JL
1182 if (ret)
1183 goto out;
17dfeb91
TM
1184 spin_lock(&inode->i_lock);
1185 if (test_bit(NFS_INO_INVALIDATING, bitlock)) {
1186 spin_unlock(&inode->i_lock);
1187 continue;
1188 }
1189 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
d529ef83 1190 break;
d529ef83 1191 spin_unlock(&inode->i_lock);
17dfeb91 1192 goto out;
f4ce1299
TM
1193 }
1194
17dfeb91 1195 set_bit(NFS_INO_INVALIDATING, bitlock);
4db72b40 1196 smp_wmb();
17dfeb91
TM
1197 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
1198 spin_unlock(&inode->i_lock);
1199 trace_nfs_invalidate_mapping_enter(inode);
be527494 1200 ret = nfs_invalidate_mapping(inode, mapping);
17dfeb91
TM
1201 trace_nfs_invalidate_mapping_exit(inode, ret);
1202
d529ef83 1203 clear_bit_unlock(NFS_INO_INVALIDATING, bitlock);
4e857c58 1204 smp_mb__after_atomic();
d529ef83 1205 wake_up_bit(bitlock, NFS_INO_INVALIDATING);
cd9ae2b6 1206out:
44b11874 1207 return ret;
7d52e862
TM
1208}
1209
ca0daa27 1210static bool nfs_file_has_writers(struct nfs_inode *nfsi)
874f9463 1211{
ca0daa27
TM
1212 struct inode *inode = &nfsi->vfs_inode;
1213
1214 assert_spin_locked(&inode->i_lock);
1215
1216 if (!S_ISREG(inode->i_mode))
1217 return false;
1218 if (list_empty(&nfsi->open_files))
1219 return false;
1220 /* Note: This relies on nfsi->open_files being ordered with writers
1221 * being placed at the head of the list.
1222 * See nfs_inode_attach_open_context()
1223 */
1224 return (list_first_entry(&nfsi->open_files,
1225 struct nfs_open_context,
1226 list)->mode & FMODE_WRITE) == FMODE_WRITE;
874f9463
TM
1227}
1228
651b0e70 1229static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi)
874f9463 1230{
651b0e70 1231 return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi);
874f9463
TM
1232}
1233
27dc1cd3 1234static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
a895b4a1
TM
1235{
1236 struct nfs_inode *nfsi = NFS_I(inode);
27dc1cd3 1237 unsigned long ret = 0;
a895b4a1 1238
9e6e70f8
TM
1239 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
1240 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
a9a4a87a
TM
1241 && inode->i_version == fattr->pre_change_attr) {
1242 inode->i_version = fattr->change_attr;
70ca8852 1243 if (S_ISDIR(inode->i_mode))
6edf9609 1244 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
27dc1cd3 1245 ret |= NFS_INO_INVALID_ATTR;
70ca8852 1246 }
a895b4a1 1247 /* If we have atomic WCC data, we may update some attributes */
9e6e70f8
TM
1248 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
1249 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
27dc1cd3
TM
1250 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
1251 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1252 ret |= NFS_INO_INVALID_ATTR;
1253 }
9e6e70f8
TM
1254
1255 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
1256 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
1257 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
27dc1cd3
TM
1258 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1259 if (S_ISDIR(inode->i_mode))
6edf9609 1260 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA);
27dc1cd3 1261 ret |= NFS_INO_INVALID_ATTR;
a895b4a1 1262 }
9e6e70f8
TM
1263 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
1264 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
1265 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
cb1410c7 1266 && nfsi->nrequests == 0) {
27dc1cd3
TM
1267 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
1268 ret |= NFS_INO_INVALID_ATTR;
1269 }
de242c0b 1270
27dc1cd3 1271 return ret;
a895b4a1
TM
1272}
1273
1da177e4 1274/**
33801147 1275 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1da177e4
LT
1276 * @inode - pointer to inode
1277 * @fattr - updated attributes
1278 *
1279 * Verifies the attribute cache. If we have just changed the attributes,
1280 * so that fattr carries weak cache consistency data, then it may
1281 * also update the ctime/mtime/change_attribute.
1282 */
33801147 1283static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1da177e4
LT
1284{
1285 struct nfs_inode *nfsi = NFS_I(inode);
1286 loff_t cur_size, new_isize;
2a3f5fd4 1287 unsigned long invalid = 0;
1da177e4 1288
dc59250c 1289
01da47bd
TM
1290 if (nfs_have_delegated_attributes(inode))
1291 return 0;
ca62b9c3 1292 /* Has the inode gone and changed behind our back? */
9e6e70f8
TM
1293 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
1294 return -EIO;
1295 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
ca62b9c3 1296 return -EIO;
ca62b9c3 1297
651b0e70 1298 if (!nfs_file_has_buffered_writers(nfsi)) {
ca0daa27
TM
1299 /* Verify a few of the more important attributes */
1300 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && inode->i_version != fattr->change_attr)
1301 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_REVAL_PAGECACHE;
1da177e4 1302
ca0daa27
TM
1303 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
1304 invalid |= NFS_INO_INVALID_ATTR;
ca62b9c3 1305
ca0daa27
TM
1306 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec_equal(&inode->i_ctime, &fattr->ctime))
1307 invalid |= NFS_INO_INVALID_ATTR;
1308
1309 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1310 cur_size = i_size_read(inode);
1311 new_isize = nfs_size_to_loff_t(fattr->size);
1312 if (cur_size != new_isize)
1313 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1314 }
9e6e70f8 1315 }
1da177e4
LT
1316
1317 /* Have any file permissions changed? */
9e6e70f8
TM
1318 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
1319 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
9ff593c4 1320 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid))
9e6e70f8 1321 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
9ff593c4 1322 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid))
2a3f5fd4 1323 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
1da177e4
LT
1324
1325 /* Has the link count changed? */
9e6e70f8 1326 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
2a3f5fd4 1327 invalid |= NFS_INO_INVALID_ATTR;
1da177e4 1328
9e6e70f8 1329 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
2a3f5fd4
TM
1330 invalid |= NFS_INO_INVALID_ATIME;
1331
1332 if (invalid != 0)
1ad13dbc 1333 nfs_set_cache_invalid(inode, invalid | NFS_INO_REVAL_FORCED);
1da177e4 1334
33801147 1335 nfsi->read_cache_jiffies = fattr->time_start;
1da177e4
LT
1336 return 0;
1337}
1338
ae05f269 1339static atomic_long_t nfs_attr_generation_counter;
4704f0e2
TM
1340
1341static unsigned long nfs_read_attr_generation_counter(void)
1342{
ae05f269 1343 return atomic_long_read(&nfs_attr_generation_counter);
4704f0e2
TM
1344}
1345
1346unsigned long nfs_inc_attr_generation_counter(void)
1347{
ae05f269 1348 return atomic_long_inc_return(&nfs_attr_generation_counter);
4704f0e2 1349}
3235b403 1350EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter);
4704f0e2
TM
1351
1352void nfs_fattr_init(struct nfs_fattr *fattr)
1353{
1354 fattr->valid = 0;
1355 fattr->time_start = jiffies;
1356 fattr->gencount = nfs_inc_attr_generation_counter();
6926afd1
TM
1357 fattr->owner_name = NULL;
1358 fattr->group_name = NULL;
4704f0e2 1359}
ddda8e0a 1360EXPORT_SYMBOL_GPL(nfs_fattr_init);
4704f0e2 1361
140e049c
TM
1362/**
1363 * nfs_fattr_set_barrier
1364 * @fattr: attributes
1365 *
1366 * Used to set a barrier after an attribute was updated. This
1367 * barrier ensures that older attributes from RPC calls that may
1368 * have raced with our update cannot clobber these new values.
1369 * Note that you are still responsible for ensuring that other
1370 * operations which change the attribute on the server do not
1371 * collide.
1372 */
1373void nfs_fattr_set_barrier(struct nfs_fattr *fattr)
1374{
1375 fattr->gencount = nfs_inc_attr_generation_counter();
1376}
1377
2d36bfde
TM
1378struct nfs_fattr *nfs_alloc_fattr(void)
1379{
1380 struct nfs_fattr *fattr;
1381
1382 fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1383 if (fattr != NULL)
1384 nfs_fattr_init(fattr);
1385 return fattr;
1386}
ddda8e0a 1387EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
2d36bfde
TM
1388
1389struct nfs_fh *nfs_alloc_fhandle(void)
1390{
1391 struct nfs_fh *fh;
1392
1393 fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1394 if (fh != NULL)
1395 fh->size = 0;
1396 return fh;
1397}
ddda8e0a 1398EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
2d36bfde 1399
e27d359e 1400#ifdef NFS_DEBUG
d8e0539e
WAA
1401/*
1402 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1403 * in the same way that wireshark does
1404 *
1405 * @fh: file handle
1406 *
1407 * For debugging only.
1408 */
1409u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1410{
1411 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1412 * not on the result */
1264a2f0 1413 return nfs_fhandle_hash(fh);
d8e0539e 1414}
9e6ee76d 1415EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash);
d8e0539e
WAA
1416
1417/*
20d27e92
CL
1418 * _nfs_display_fhandle - display an NFS file handle on the console
1419 *
1420 * @fh: file handle to display
1421 * @caption: display caption
1422 *
1423 * For debugging only.
1424 */
20d27e92
CL
1425void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1426{
1427 unsigned short i;
1428
fa68a1ba 1429 if (fh == NULL || fh->size == 0) {
20d27e92
CL
1430 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1431 return;
1432 }
1433
d8e0539e
WAA
1434 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1435 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
20d27e92
CL
1436 for (i = 0; i < fh->size; i += 16) {
1437 __be32 *pos = (__be32 *)&fh->data[i];
1438
1439 switch ((fh->size - i - 1) >> 2) {
1440 case 0:
1441 printk(KERN_DEFAULT " %08x\n",
1442 be32_to_cpup(pos));
1443 break;
1444 case 1:
1445 printk(KERN_DEFAULT " %08x %08x\n",
1446 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1447 break;
1448 case 2:
1449 printk(KERN_DEFAULT " %08x %08x %08x\n",
1450 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1451 be32_to_cpup(pos + 2));
1452 break;
1453 default:
1454 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1455 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1456 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1457 }
1458 }
1459}
9e6ee76d 1460EXPORT_SYMBOL_GPL(_nfs_display_fhandle);
20d27e92
CL
1461#endif
1462
a10ad176
TM
1463/**
1464 * nfs_inode_attrs_need_update - check if the inode attributes need updating
1465 * @inode - pointer to inode
1466 * @fattr - attributes
1467 *
1468 * Attempt to divine whether or not an RPC call reply carrying stale
1469 * attributes got scheduled after another call carrying updated ones.
1470 *
1471 * To do so, the function first assumes that a more recent ctime means
1472 * that the attributes in fattr are newer, however it also attempt to
1473 * catch the case where ctime either didn't change, or went backwards
1474 * (if someone reset the clock on the server) by looking at whether
1475 * or not this RPC call was started after the inode was last updated.
4704f0e2 1476 * Note also the check for wraparound of 'attr_gencount'
a10ad176
TM
1477 *
1478 * The function returns 'true' if it thinks the attributes in 'fattr' are
1479 * more recent than the ones cached in the inode.
1480 *
1481 */
1482static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1483{
1484 const struct nfs_inode *nfsi = NFS_I(inode);
1485
4704f0e2 1486 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
4704f0e2 1487 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
a10ad176
TM
1488}
1489
1490static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1491{
f4ce1299
TM
1492 int ret;
1493
1494 trace_nfs_refresh_inode_enter(inode);
1495
a10ad176 1496 if (nfs_inode_attrs_need_update(inode, fattr))
f4ce1299
TM
1497 ret = nfs_update_inode(inode, fattr);
1498 else
1499 ret = nfs_check_inode_attributes(inode, fattr);
1500
1501 trace_nfs_refresh_inode_exit(inode, ret);
1502 return ret;
870a5be8
TM
1503}
1504
33801147
TM
1505/**
1506 * nfs_refresh_inode - try to update the inode attribute cache
1507 * @inode - pointer to inode
1508 * @fattr - updated attributes
1509 *
1510 * Check that an RPC call that returned attributes has not overlapped with
1511 * other recent updates of the inode metadata, then decide whether it is
1512 * safe to do a full update of the inode attributes, or whether just to
1513 * call nfs_check_inode_attributes.
1514 */
1515int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1516{
33801147
TM
1517 int status;
1518
1519 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1520 return 0;
1521 spin_lock(&inode->i_lock);
870a5be8 1522 status = nfs_refresh_inode_locked(inode, fattr);
33801147 1523 spin_unlock(&inode->i_lock);
ef79c097 1524
33801147
TM
1525 return status;
1526}
ddda8e0a 1527EXPORT_SYMBOL_GPL(nfs_refresh_inode);
33801147 1528
d65f557f
TM
1529static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1530{
ca0daa27 1531 unsigned long invalid = NFS_INO_INVALID_ATTR;
d65f557f 1532
aaae3f00
TM
1533 /*
1534 * Don't revalidate the pagecache if we hold a delegation, but do
1535 * force an attribute update
1536 */
1537 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1538 invalid = NFS_INO_INVALID_ATTR|NFS_INO_REVAL_FORCED;
1539
6edf9609
TM
1540 if (S_ISDIR(inode->i_mode))
1541 invalid |= NFS_INO_INVALID_DATA;
1542 nfs_set_cache_invalid(inode, invalid);
d65f557f
TM
1543 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1544 return 0;
1545 return nfs_refresh_inode_locked(inode, fattr);
1546}
1547
decf491f
TM
1548/**
1549 * nfs_post_op_update_inode - try to update the inode attribute cache
1550 * @inode - pointer to inode
1551 * @fattr - updated attributes
1552 *
1553 * After an operation that has changed the inode metadata, mark the
1554 * attribute cache as being invalid, then try to update it.
f551e44f
CL
1555 *
1556 * NB: if the server didn't return any post op attributes, this
1557 * function will force the retrieval of attributes before the next
1558 * NFS request. Thus it should be used only for operations that
1559 * are expected to change one or more attributes, to avoid
1560 * unnecessary NFS requests and trips through nfs_update_inode().
decf491f
TM
1561 */
1562int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1563{
d65f557f 1564 int status;
decf491f 1565
7668fdbe 1566 spin_lock(&inode->i_lock);
92d64e47 1567 nfs_fattr_set_barrier(fattr);
d65f557f 1568 status = nfs_post_op_update_inode_locked(inode, fattr);
7668fdbe 1569 spin_unlock(&inode->i_lock);
aa9c2669 1570
870a5be8 1571 return status;
decf491f 1572}
1c606fb7 1573EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
decf491f 1574
70ca8852 1575/**
a08a8cd3 1576 * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache
70ca8852
TM
1577 * @inode - pointer to inode
1578 * @fattr - updated attributes
1579 *
1580 * After an operation that has changed the inode metadata, mark the
1581 * attribute cache as being invalid, then try to update it. Fake up
1582 * weak cache consistency data, if none exist.
1583 *
1584 * This function is mainly designed to be used by the ->write_done() functions.
1585 */
a08a8cd3 1586int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr)
70ca8852 1587{
d65f557f
TM
1588 int status;
1589
d65f557f
TM
1590 /* Don't do a WCC update if these attributes are already stale */
1591 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1592 !nfs_inode_attrs_need_update(inode, fattr)) {
9e6e70f8
TM
1593 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1594 | NFS_ATTR_FATTR_PRESIZE
1595 | NFS_ATTR_FATTR_PREMTIME
1596 | NFS_ATTR_FATTR_PRECTIME);
d65f557f
TM
1597 goto out_noforce;
1598 }
9e6e70f8
TM
1599 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1600 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
a9a4a87a 1601 fattr->pre_change_attr = inode->i_version;
9e6e70f8 1602 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
70ca8852 1603 }
9e6e70f8
TM
1604 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1605 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
70ca8852 1606 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
9e6e70f8
TM
1607 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1608 }
1609 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1610 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
70ca8852 1611 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
9e6e70f8
TM
1612 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1613 }
1614 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1615 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
a3d01454 1616 fattr->pre_size = i_size_read(inode);
9e6e70f8 1617 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
70ca8852 1618 }
d65f557f
TM
1619out_noforce:
1620 status = nfs_post_op_update_inode_locked(inode, fattr);
a08a8cd3
TM
1621 return status;
1622}
1623
1624/**
1625 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1626 * @inode - pointer to inode
1627 * @fattr - updated attributes
1628 *
1629 * After an operation that has changed the inode metadata, mark the
1630 * attribute cache as being invalid, then try to update it. Fake up
1631 * weak cache consistency data, if none exist.
1632 *
1633 * This function is mainly designed to be used by the ->write_done() functions.
1634 */
1635int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1636{
1637 int status;
1638
1639 spin_lock(&inode->i_lock);
8f8ba1d7 1640 nfs_fattr_set_barrier(fattr);
a08a8cd3 1641 status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
d65f557f
TM
1642 spin_unlock(&inode->i_lock);
1643 return status;
70ca8852 1644}
ddda8e0a 1645EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
70ca8852 1646
ea96d1ec
AS
1647
1648static inline bool nfs_fileid_valid(struct nfs_inode *nfsi,
1649 struct nfs_fattr *fattr)
1650{
1651 bool ret1 = true, ret2 = true;
1652
1653 if (fattr->valid & NFS_ATTR_FATTR_FILEID)
1654 ret1 = (nfsi->fileid == fattr->fileid);
1655 if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
1656 ret2 = (nfsi->fileid == fattr->mounted_on_fileid);
1657 return ret1 || ret2;
1658}
1659
1da177e4
LT
1660/*
1661 * Many nfs protocol calls return the new file attributes after
1662 * an operation. Here we update the inode to reflect the state
1663 * of the server's inode.
1664 *
1665 * This is a bit tricky because we have to make sure all dirty pages
1666 * have been sent off to the server before calling invalidate_inode_pages.
1667 * To make sure no other process adds more write requests while we try
1668 * our best to flush them, we make them sleep during the attribute refresh.
1669 *
1670 * A very similar scenario holds for the dir cache.
1671 */
24aa1fe6 1672static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1da177e4 1673{
8b4bdcf8 1674 struct nfs_server *server;
1da177e4 1675 struct nfs_inode *nfsi = NFS_I(inode);
951a143b 1676 loff_t cur_isize, new_isize;
2a3f5fd4 1677 unsigned long invalid = 0;
3e7d950a 1678 unsigned long now = jiffies;
62ab460c 1679 unsigned long save_cache_validity;
651b0e70 1680 bool have_writers = nfs_file_has_buffered_writers(nfsi);
944171cb 1681 bool cache_revalidated = true;
1da177e4 1682
1e8968c5 1683 dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n",
3110ff80 1684 __func__, inode->i_sb->s_id, inode->i_ino,
4f1abd22 1685 nfs_display_fhandle_hash(NFS_FH(inode)),
1da177e4
LT
1686 atomic_read(&inode->i_count), fattr->valid);
1687
ea96d1ec 1688 if (!nfs_fileid_valid(nfsi, fattr)) {
e73e6c9e
MT
1689 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1690 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1691 NFS_SERVER(inode)->nfs_client->cl_hostname,
1692 inode->i_sb->s_id, (long long)nfsi->fileid,
1693 (long long)fattr->fileid);
1694 goto out_err;
1695 }
1da177e4
LT
1696
1697 /*
1698 * Make sure the inode's type hasn't changed.
1699 */
e73e6c9e
MT
1700 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1701 /*
1702 * Big trouble! The inode has become a different object.
1703 */
1e8968c5 1704 printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n",
e73e6c9e
MT
1705 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1706 goto out_err;
1707 }
1da177e4 1708
8b4bdcf8 1709 server = NFS_SERVER(inode);
a0356862 1710 /* Update the fsid? */
9e6e70f8 1711 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
c37dcd33 1712 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
36d43a43 1713 !IS_AUTOMOUNT(inode))
8b4bdcf8
TM
1714 server->fsid = fattr->fsid;
1715
1da177e4
LT
1716 /*
1717 * Update the read time so we don't revalidate too often.
1718 */
33801147 1719 nfsi->read_cache_jiffies = fattr->time_start;
3e7d950a 1720
62ab460c
TM
1721 save_cache_validity = nfsi->cache_validity;
1722 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1723 | NFS_INO_INVALID_ATIME
1724 | NFS_INO_REVAL_FORCED
1725 | NFS_INO_REVAL_PAGECACHE);
1da177e4 1726
a895b4a1 1727 /* Do atomic weak cache consistency updates */
27dc1cd3 1728 invalid |= nfs_wcc_update_inode(inode, fattr);
a895b4a1 1729
944171cb
BC
1730 if (pnfs_layoutcommit_outstanding(inode)) {
1731 nfsi->cache_validity |= save_cache_validity & NFS_INO_INVALID_ATTR;
1732 cache_revalidated = false;
1733 }
10b7e9ad 1734
47aabaa7 1735 /* More cache consistency checks */
9e6e70f8 1736 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
a9a4a87a 1737 if (inode->i_version != fattr->change_attr) {
9e6e70f8
TM
1738 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1739 inode->i_sb->s_id, inode->i_ino);
38512aa9 1740 /* Could it be a race with writeback? */
ca0daa27 1741 if (!have_writers) {
38512aa9
TM
1742 invalid |= NFS_INO_INVALID_ATTR
1743 | NFS_INO_INVALID_DATA
1744 | NFS_INO_INVALID_ACCESS
1745 | NFS_INO_INVALID_ACL;
1746 if (S_ISDIR(inode->i_mode))
1747 nfs_force_lookup_revalidate(inode);
1748 }
a9a4a87a 1749 inode->i_version = fattr->change_attr;
9e6e70f8 1750 }
ade14a7d 1751 } else {
43b6535e 1752 nfsi->cache_validity |= save_cache_validity;
ade14a7d
TM
1753 cache_revalidated = false;
1754 }
9e6e70f8
TM
1755
1756 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
fee7fe19 1757 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
ade14a7d 1758 } else if (server->caps & NFS_CAP_MTIME) {
43b6535e
TM
1759 nfsi->cache_validity |= save_cache_validity &
1760 (NFS_INO_INVALID_ATTR
62ab460c 1761 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1762 cache_revalidated = false;
1763 }
62ab460c 1764
9e6e70f8 1765 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
fee7fe19 1766 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
ade14a7d 1767 } else if (server->caps & NFS_CAP_CTIME) {
43b6535e
TM
1768 nfsi->cache_validity |= save_cache_validity &
1769 (NFS_INO_INVALID_ATTR
62ab460c 1770 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1771 cache_revalidated = false;
1772 }
47aabaa7 1773
951a143b 1774 /* Check if our cached file size is stale */
9e6e70f8
TM
1775 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1776 new_isize = nfs_size_to_loff_t(fattr->size);
1777 cur_isize = i_size_read(inode);
1778 if (new_isize != cur_isize) {
1779 /* Do we perhaps have any outstanding writes, or has
1780 * the file grown beyond our last write? */
ca0daa27 1781 if (nfsi->nrequests == 0 || new_isize > cur_isize) {
9e6e70f8 1782 i_size_write(inode, new_isize);
ca0daa27
TM
1783 if (!have_writers)
1784 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
9e6e70f8 1785 }
60c16ea8
HJ
1786 dprintk("NFS: isize change on server for file %s/%ld "
1787 "(%Ld to %Ld)\n",
1788 inode->i_sb->s_id,
1789 inode->i_ino,
1790 (long long)cur_isize,
1791 (long long)new_isize);
1da177e4 1792 }
ade14a7d 1793 } else {
43b6535e
TM
1794 nfsi->cache_validity |= save_cache_validity &
1795 (NFS_INO_INVALID_ATTR
62ab460c
TM
1796 | NFS_INO_REVAL_PAGECACHE
1797 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1798 cache_revalidated = false;
1799 }
1da177e4 1800
1da177e4 1801
9e6e70f8
TM
1802 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1803 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
ade14a7d 1804 else if (server->caps & NFS_CAP_ATIME) {
43b6535e
TM
1805 nfsi->cache_validity |= save_cache_validity &
1806 (NFS_INO_INVALID_ATIME
62ab460c 1807 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1808 cache_revalidated = false;
1809 }
1da177e4 1810
9e6e70f8
TM
1811 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1812 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
9b4b3513
TM
1813 umode_t newmode = inode->i_mode & S_IFMT;
1814 newmode |= fattr->mode & S_IALLUGO;
1815 inode->i_mode = newmode;
9e6e70f8 1816 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
9e6e70f8 1817 }
ade14a7d 1818 } else if (server->caps & NFS_CAP_MODE) {
43b6535e
TM
1819 nfsi->cache_validity |= save_cache_validity &
1820 (NFS_INO_INVALID_ATTR
62ab460c
TM
1821 | NFS_INO_INVALID_ACCESS
1822 | NFS_INO_INVALID_ACL
1823 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1824 cache_revalidated = false;
1825 }
62ab460c 1826
9e6e70f8 1827 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
9ff593c4 1828 if (!uid_eq(inode->i_uid, fattr->uid)) {
9e6e70f8
TM
1829 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1830 inode->i_uid = fattr->uid;
1831 }
ade14a7d 1832 } else if (server->caps & NFS_CAP_OWNER) {
43b6535e
TM
1833 nfsi->cache_validity |= save_cache_validity &
1834 (NFS_INO_INVALID_ATTR
62ab460c
TM
1835 | NFS_INO_INVALID_ACCESS
1836 | NFS_INO_INVALID_ACL
1837 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1838 cache_revalidated = false;
1839 }
62ab460c 1840
9e6e70f8 1841 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
9ff593c4 1842 if (!gid_eq(inode->i_gid, fattr->gid)) {
9e6e70f8
TM
1843 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1844 inode->i_gid = fattr->gid;
1845 }
ade14a7d 1846 } else if (server->caps & NFS_CAP_OWNER_GROUP) {
43b6535e
TM
1847 nfsi->cache_validity |= save_cache_validity &
1848 (NFS_INO_INVALID_ATTR
62ab460c
TM
1849 | NFS_INO_INVALID_ACCESS
1850 | NFS_INO_INVALID_ACL
1851 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1852 cache_revalidated = false;
1853 }
921615f1 1854
9e6e70f8
TM
1855 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1856 if (inode->i_nlink != fattr->nlink) {
1857 invalid |= NFS_INO_INVALID_ATTR;
1858 if (S_ISDIR(inode->i_mode))
1859 invalid |= NFS_INO_INVALID_DATA;
bfe86848 1860 set_nlink(inode, fattr->nlink);
9e6e70f8 1861 }
ade14a7d 1862 } else if (server->caps & NFS_CAP_NLINK) {
43b6535e
TM
1863 nfsi->cache_validity |= save_cache_validity &
1864 (NFS_INO_INVALID_ATTR
62ab460c 1865 | NFS_INO_REVAL_FORCED);
ade14a7d
TM
1866 cache_revalidated = false;
1867 }
1da177e4 1868
9e6e70f8 1869 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1da177e4
LT
1870 /*
1871 * report the blocks in 512byte units
1872 */
1873 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
ade14a7d 1874 } else if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
9e6e70f8 1875 inode->i_blocks = fattr->du.nfs2.blocks;
ade14a7d
TM
1876 else
1877 cache_revalidated = false;
1da177e4
LT
1878
1879 /* Update attrtimeo value if we're out of the unstable period */
fd1defc2 1880 if (invalid & NFS_INO_INVALID_ATTR) {
91d5b470 1881 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1da177e4 1882 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
3e7d950a 1883 nfsi->attrtimeo_timestamp = now;
f5062003 1884 /* Set barrier to be more recent than all outstanding updates */
4704f0e2 1885 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
6d2b2966 1886 } else {
ade14a7d
TM
1887 if (cache_revalidated) {
1888 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp,
1889 nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
1890 nfsi->attrtimeo <<= 1;
1891 if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode))
1892 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1893 }
6d2b2966
TM
1894 nfsi->attrtimeo_timestamp = now;
1895 }
f5062003
TM
1896 /* Set the barrier to be more recent than this fattr */
1897 if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0)
1898 nfsi->attr_gencount = fattr->gencount;
1da177e4 1899 }
c812012f
JL
1900
1901 /* Don't declare attrcache up to date if there were no attrs! */
ade14a7d 1902 if (cache_revalidated)
c812012f
JL
1903 invalid &= ~NFS_INO_INVALID_ATTR;
1904
1da177e4
LT
1905 /* Don't invalidate the data if we were to blame */
1906 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1907 || S_ISLNK(inode->i_mode)))
1908 invalid &= ~NFS_INO_INVALID_DATA;
011e2a7f 1909 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
62ab460c 1910 (save_cache_validity & NFS_INO_REVAL_FORCED))
6edf9609 1911 nfs_set_cache_invalid(inode, invalid);
de242c0b 1912
1da177e4 1913 return 0;
b37b03b7 1914 out_err:
1da177e4
LT
1915 /*
1916 * No need to worry about unhashing the dentry, as the
1917 * lookup validation will know that the inode is bad.
1918 * (But we fall through to invalidate the caches.)
1919 */
1920 nfs_invalidate_inode(inode);
1da177e4
LT
1921 return -ESTALE;
1922}
1923
f7b422b1 1924struct inode *nfs_alloc_inode(struct super_block *sb)
1da177e4
LT
1925{
1926 struct nfs_inode *nfsi;
c456aacf 1927 nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
1da177e4
LT
1928 if (!nfsi)
1929 return NULL;
55296809
CL
1930 nfsi->flags = 0UL;
1931 nfsi->cache_validity = 0UL;
89d77c8f 1932#if IS_ENABLED(CONFIG_NFS_V4)
e50a1c2e
BF
1933 nfsi->nfs4_acl = NULL;
1934#endif /* CONFIG_NFS_V4 */
1da177e4
LT
1935 return &nfsi->vfs_inode;
1936}
89d77c8f 1937EXPORT_SYMBOL_GPL(nfs_alloc_inode);
1da177e4 1938
fa0d7e3d 1939static void nfs_i_callback(struct rcu_head *head)
1da177e4 1940{
fa0d7e3d 1941 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
1942 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1943}
1944
fa0d7e3d
NP
1945void nfs_destroy_inode(struct inode *inode)
1946{
1947 call_rcu(&inode->i_rcu, nfs_i_callback);
1948}
89d77c8f 1949EXPORT_SYMBOL_GPL(nfs_destroy_inode);
fa0d7e3d 1950
d75d5414
AM
1951static inline void nfs4_init_once(struct nfs_inode *nfsi)
1952{
89d77c8f 1953#if IS_ENABLED(CONFIG_NFS_V4)
d75d5414
AM
1954 INIT_LIST_HEAD(&nfsi->open_states);
1955 nfsi->delegation = NULL;
d75d5414 1956 init_rwsem(&nfsi->rwsem);
e5e94017 1957 nfsi->layout = NULL;
d75d5414
AM
1958#endif
1959}
f7b422b1 1960
51cc5068 1961static void init_once(void *foo)
1da177e4
LT
1962{
1963 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1964
a35afb83 1965 inode_init_once(&nfsi->vfs_inode);
a35afb83
CL
1966 INIT_LIST_HEAD(&nfsi->open_files);
1967 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1968 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
ea2cf228 1969 INIT_LIST_HEAD(&nfsi->commit_info.list);
cb1410c7 1970 nfsi->nrequests = 0;
ea2cf228 1971 nfsi->commit_info.ncommit = 0;
1a0de48a 1972 atomic_set(&nfsi->commit_info.rpcs_out, 0);
884be175 1973 init_rwsem(&nfsi->rmdir_sem);
a35afb83 1974 nfs4_init_once(nfsi);
1da177e4 1975}
20c2df83 1976
f7b422b1 1977static int __init nfs_init_inodecache(void)
1da177e4
LT
1978{
1979 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1980 sizeof(struct nfs_inode),
fffb60f9 1981 0, (SLAB_RECLAIM_ACCOUNT|
5d097056 1982 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
20c2df83 1983 init_once);
1da177e4
LT
1984 if (nfs_inode_cachep == NULL)
1985 return -ENOMEM;
1986
1987 return 0;
1988}
1989
266bee88 1990static void nfs_destroy_inodecache(void)
1da177e4 1991{
8c0a8537
KS
1992 /*
1993 * Make sure all delayed rcu free inodes are flushed before we
1994 * destroy cache.
1995 */
1996 rcu_barrier();
1a1d92c1 1997 kmem_cache_destroy(nfs_inode_cachep);
1da177e4
LT
1998}
1999
5746006f 2000struct workqueue_struct *nfsiod_workqueue;
89d77c8f 2001EXPORT_SYMBOL_GPL(nfsiod_workqueue);
5746006f
TM
2002
2003/*
2004 * start up the nfsiod workqueue
2005 */
2006static int nfsiod_start(void)
2007{
2008 struct workqueue_struct *wq;
2009 dprintk("RPC: creating workqueue nfsiod\n");
ada609ee 2010 wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
5746006f
TM
2011 if (wq == NULL)
2012 return -ENOMEM;
2013 nfsiod_workqueue = wq;
2014 return 0;
2015}
2016
2017/*
2018 * Destroy the nfsiod workqueue
2019 */
2020static void nfsiod_stop(void)
2021{
2022 struct workqueue_struct *wq;
2023
2024 wq = nfsiod_workqueue;
2025 if (wq == NULL)
2026 return;
2027 nfsiod_workqueue = NULL;
2028 destroy_workqueue(wq);
2029}
2030
1b340d01 2031int nfs_net_id;
9e2e74db 2032EXPORT_SYMBOL_GPL(nfs_net_id);
1b340d01
SK
2033
2034static int nfs_net_init(struct net *net)
2035{
6b13168b 2036 nfs_clients_init(net);
65b38851 2037 return nfs_fs_proc_net_init(net);
1b340d01
SK
2038}
2039
2040static void nfs_net_exit(struct net *net)
2041{
65b38851 2042 nfs_fs_proc_net_exit(net);
28cd1b3f 2043 nfs_cleanup_cb_ident_idr(net);
1b340d01
SK
2044}
2045
2046static struct pernet_operations nfs_net_ops = {
2047 .init = nfs_net_init,
2048 .exit = nfs_net_exit,
2049 .id = &nfs_net_id,
2050 .size = sizeof(struct nfs_net),
2051};
2052
1da177e4
LT
2053/*
2054 * Initialize NFS
2055 */
2056static int __init init_nfs_fs(void)
2057{
2058 int err;
2059
1b340d01 2060 err = register_pernet_subsys(&nfs_net_ops);
e571cbf1 2061 if (err < 0)
89d77c8f 2062 goto out9;
e571cbf1 2063
8ec442ae
DH
2064 err = nfs_fscache_register();
2065 if (err < 0)
89d77c8f 2066 goto out8;
8ec442ae 2067
5746006f
TM
2068 err = nfsiod_start();
2069 if (err)
89d77c8f 2070 goto out7;
5746006f 2071
6aaca566
DH
2072 err = nfs_fs_proc_init();
2073 if (err)
89d77c8f 2074 goto out6;
6aaca566 2075
1da177e4
LT
2076 err = nfs_init_nfspagecache();
2077 if (err)
89d77c8f 2078 goto out5;
1da177e4
LT
2079
2080 err = nfs_init_inodecache();
2081 if (err)
89d77c8f 2082 goto out4;
1da177e4
LT
2083
2084 err = nfs_init_readpagecache();
2085 if (err)
89d77c8f 2086 goto out3;
1da177e4
LT
2087
2088 err = nfs_init_writepagecache();
2089 if (err)
89d77c8f 2090 goto out2;
1da177e4 2091
1da177e4
LT
2092 err = nfs_init_directcache();
2093 if (err)
89d77c8f 2094 goto out1;
1da177e4 2095
ec7652aa 2096 rpc_proc_register(&init_net, &nfs_rpcstat);
cd738ee9
KM
2097
2098 err = register_nfs_fs();
2099 if (err)
129d1977
BS
2100 goto out0;
2101
1da177e4 2102 return 0;
129d1977 2103out0:
ec7652aa 2104 rpc_proc_unregister(&init_net, "nfs");
1da177e4 2105 nfs_destroy_directcache();
89d77c8f 2106out1:
129d1977 2107 nfs_destroy_writepagecache();
89d77c8f 2108out2:
129d1977 2109 nfs_destroy_readpagecache();
89d77c8f 2110out3:
129d1977 2111 nfs_destroy_inodecache();
89d77c8f 2112out4:
129d1977 2113 nfs_destroy_nfspagecache();
89d77c8f 2114out5:
129d1977 2115 nfs_fs_proc_exit();
89d77c8f 2116out6:
129d1977 2117 nfsiod_stop();
89d77c8f 2118out7:
129d1977 2119 nfs_fscache_unregister();
89d77c8f 2120out8:
129d1977 2121 unregister_pernet_subsys(&nfs_net_ops);
89d77c8f 2122out9:
1da177e4
LT
2123 return err;
2124}
2125
2126static void __exit exit_nfs_fs(void)
2127{
1da177e4 2128 nfs_destroy_directcache();
1da177e4
LT
2129 nfs_destroy_writepagecache();
2130 nfs_destroy_readpagecache();
2131 nfs_destroy_inodecache();
2132 nfs_destroy_nfspagecache();
8ec442ae 2133 nfs_fscache_unregister();
1b340d01 2134 unregister_pernet_subsys(&nfs_net_ops);
ec7652aa 2135 rpc_proc_unregister(&init_net, "nfs");
f7b422b1 2136 unregister_nfs_fs();
6aaca566 2137 nfs_fs_proc_exit();
5746006f 2138 nfsiod_stop();
1da177e4
LT
2139}
2140
2141/* Not quite true; I just maintain it */
2142MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2143MODULE_LICENSE("GPL");
f43bf0be 2144module_param(enable_ino64, bool, 0644);
1da177e4
LT
2145
2146module_init(init_nfs_fs)
2147module_exit(exit_nfs_fs)