SUNRPC: Fix a typo in cache_pipefs_files
[linux-2.6-block.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>
35#include <linux/nfs_idmap.h>
36#include <linux/vfs.h>
9cdb3883
MN
37#include <linux/inet.h>
38#include <linux/nfs_xdr.h>
1da177e4
LT
39
40#include <asm/system.h>
41#include <asm/uaccess.h>
42
4ce79717 43#include "nfs4_fs.h"
a72b4422 44#include "callback.h"
1da177e4 45#include "delegation.h"
d9ef5a8c 46#include "iostat.h"
f7b422b1 47#include "internal.h"
8ec442ae 48#include "fscache.h"
1da177e4
LT
49
50#define NFSDBG_FACILITY NFSDBG_VFS
1da177e4 51
f43bf0be
TM
52#define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
53
54/* Default is to see 64-bit inode numbers */
55static int enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
56
1da177e4 57static void nfs_invalidate_inode(struct inode *);
24aa1fe6 58static int nfs_update_inode(struct inode *, struct nfs_fattr *);
1da177e4 59
e18b890b 60static struct kmem_cache * nfs_inode_cachep;
b7fa0554 61
1da177e4
LT
62static inline unsigned long
63nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
64{
65 return nfs_fileid_to_ino_t(fattr->fileid);
66}
67
72cb77f4
TM
68/**
69 * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
70 * @word: long word containing the bit lock
71 */
72int nfs_wait_bit_killable(void *word)
73{
74 if (fatal_signal_pending(current))
75 return -ERESTARTSYS;
76 schedule();
77 return 0;
78}
79
f43bf0be
TM
80/**
81 * nfs_compat_user_ino64 - returns the user-visible inode number
82 * @fileid: 64-bit fileid
83 *
84 * This function returns a 32-bit inode number if the boot parameter
85 * nfs.enable_ino64 is zero.
86 */
87u64 nfs_compat_user_ino64(u64 fileid)
88{
89 int ino;
90
91 if (enable_ino64)
92 return fileid;
93 ino = fileid;
94 if (sizeof(ino) < sizeof(fileid))
95 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
96 return ino;
97}
98
f7b422b1 99int nfs_write_inode(struct inode *inode, int sync)
1da177e4 100{
1da177e4
LT
101 int ret;
102
d30c8348
TM
103 if (sync) {
104 ret = filemap_fdatawait(inode->i_mapping);
105 if (ret == 0)
106 ret = nfs_commit_inode(inode, FLUSH_SYNC);
107 } else
108 ret = nfs_commit_inode(inode, 0);
109 if (ret >= 0)
110 return 0;
111 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
112 return ret;
1da177e4
LT
113}
114
f7b422b1 115void nfs_clear_inode(struct inode *inode)
1da177e4 116{
da6d503a
TM
117 /*
118 * The following should never happen...
119 */
120 BUG_ON(nfs_have_writebacks(inode));
1c3c07e9 121 BUG_ON(!list_empty(&NFS_I(inode)->open_files));
ada70d94 122 nfs_zap_acl_cache(inode);
1c3c07e9 123 nfs_access_zap_cache(inode);
ef79c097 124 nfs_fscache_release_inode_cookie(inode);
1da177e4
LT
125}
126
29884df0
TM
127/**
128 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
129 */
130int nfs_sync_mapping(struct address_space *mapping)
131{
132 int ret;
133
134 if (mapping->nrpages == 0)
135 return 0;
136 unmap_mapping_range(mapping, 0, 0, 0);
28fd1298 137 ret = filemap_write_and_wait(mapping);
29884df0
TM
138 if (ret != 0)
139 goto out;
140 ret = nfs_wb_all(mapping->host);
141out:
142 return ret;
143}
144
1da177e4
LT
145/*
146 * Invalidate the local caches
147 */
b37b03b7 148static void nfs_zap_caches_locked(struct inode *inode)
1da177e4
LT
149{
150 struct nfs_inode *nfsi = NFS_I(inode);
151 int mode = inode->i_mode;
152
91d5b470
CL
153 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
154
c7c20973
TM
155 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
156 nfsi->attrtimeo_timestamp = jiffies;
1da177e4
LT
157
158 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
159 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
55296809 160 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
1da177e4 161 else
55296809 162 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
b37b03b7 163}
dc59250c 164
b37b03b7
TM
165void nfs_zap_caches(struct inode *inode)
166{
167 spin_lock(&inode->i_lock);
168 nfs_zap_caches_locked(inode);
dc59250c 169 spin_unlock(&inode->i_lock);
ada70d94
TM
170}
171
cd9ae2b6
TM
172void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
173{
174 if (mapping->nrpages != 0) {
175 spin_lock(&inode->i_lock);
176 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
177 spin_unlock(&inode->i_lock);
178 }
179}
180
f41f7418 181void nfs_zap_acl_cache(struct inode *inode)
ada70d94
TM
182{
183 void (*clear_acl_cache)(struct inode *);
184
185 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
186 if (clear_acl_cache != NULL)
187 clear_acl_cache(inode);
dc59250c 188 spin_lock(&inode->i_lock);
55296809 189 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
dc59250c 190 spin_unlock(&inode->i_lock);
1da177e4
LT
191}
192
c4812998
TM
193void nfs_invalidate_atime(struct inode *inode)
194{
195 spin_lock(&inode->i_lock);
196 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
197 spin_unlock(&inode->i_lock);
198}
199
1da177e4 200/*
b37b03b7
TM
201 * Invalidate, but do not unhash, the inode.
202 * NB: must be called with inode->i_lock held!
1da177e4 203 */
b37b03b7 204static void nfs_invalidate_inode(struct inode *inode)
1da177e4 205{
3a10c30a 206 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
b37b03b7 207 nfs_zap_caches_locked(inode);
1da177e4
LT
208}
209
210struct nfs_find_desc {
211 struct nfs_fh *fh;
212 struct nfs_fattr *fattr;
213};
214
215/*
216 * In NFSv3 we can have 64bit inode numbers. In order to support
217 * this, and re-exported directories (also seen in NFSv2)
218 * we are forced to allow 2 different inodes to have the same
219 * i_ino.
220 */
221static int
222nfs_find_actor(struct inode *inode, void *opaque)
223{
224 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
225 struct nfs_fh *fh = desc->fh;
226 struct nfs_fattr *fattr = desc->fattr;
227
228 if (NFS_FILEID(inode) != fattr->fileid)
229 return 0;
230 if (nfs_compare_fh(NFS_FH(inode), fh))
231 return 0;
232 if (is_bad_inode(inode) || NFS_STALE(inode))
233 return 0;
234 return 1;
235}
236
237static int
238nfs_init_locked(struct inode *inode, void *opaque)
239{
240 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
241 struct nfs_fattr *fattr = desc->fattr;
242
99fadcd7 243 set_nfs_fileid(inode, fattr->fileid);
1da177e4
LT
244 nfs_copy_fh(NFS_FH(inode), desc->fh);
245 return 0;
246}
247
248/* Don't use READDIRPLUS on directories that we believe are too large */
249#define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
250
251/*
252 * This is our front-end to iget that looks up inodes by file handle
253 * instead of inode number.
254 */
255struct inode *
256nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
257{
258 struct nfs_find_desc desc = {
259 .fh = fh,
260 .fattr = fattr
261 };
03f28e3a 262 struct inode *inode = ERR_PTR(-ENOENT);
1da177e4
LT
263 unsigned long hash;
264
9e6e70f8 265 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
1da177e4 266 goto out_no_inode;
9e6e70f8 267 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
1da177e4 268 goto out_no_inode;
1da177e4
LT
269
270 hash = nfs_fattr_to_ino_t(fattr);
271
03f28e3a
TM
272 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
273 if (inode == NULL) {
274 inode = ERR_PTR(-ENOMEM);
1da177e4 275 goto out_no_inode;
03f28e3a 276 }
1da177e4
LT
277
278 if (inode->i_state & I_NEW) {
279 struct nfs_inode *nfsi = NFS_I(inode);
b0c4fddc 280 unsigned long now = jiffies;
1da177e4
LT
281
282 /* We set i_ino for the few things that still rely on it,
283 * such as stat(2) */
284 inode->i_ino = hash;
285
286 /* We can't support update_atime(), since the server will reset it */
287 inode->i_flags |= S_NOATIME|S_NOCMTIME;
288 inode->i_mode = fattr->mode;
62ab460c
TM
289 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
290 && nfs_server_capable(inode, NFS_CAP_MODE))
291 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
292 | NFS_INO_INVALID_ACCESS
293 | NFS_INO_INVALID_ACL;
1da177e4
LT
294 /* Why so? Because we want revalidate for devices/FIFOs, and
295 * that's precisely what we have in nfs_file_inode_operations.
296 */
8fa5c000 297 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
1da177e4
LT
298 if (S_ISREG(inode->i_mode)) {
299 inode->i_fop = &nfs_file_operations;
300 inode->i_data.a_ops = &nfs_file_aops;
301 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
302 } else if (S_ISDIR(inode->i_mode)) {
8fa5c000 303 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
1da177e4
LT
304 inode->i_fop = &nfs_dir_operations;
305 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
306 && fattr->size <= NFS_LIMIT_READDIRPLUS)
3a10c30a 307 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
55a97593 308 /* Deal with crossing mountpoints */
9e6e70f8
TM
309 if ((fattr->valid & NFS_ATTR_FATTR_FSID)
310 && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
6b97fd3d
MN
311 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
312 inode->i_op = &nfs_referral_inode_operations;
313 else
314 inode->i_op = &nfs_mountpoint_inode_operations;
55a97593 315 inode->i_fop = NULL;
c37dcd33 316 set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
55a97593 317 }
1da177e4
LT
318 } else if (S_ISLNK(inode->i_mode))
319 inode->i_op = &nfs_symlink_inode_operations;
320 else
321 init_special_inode(inode, inode->i_mode, fattr->rdev);
322
9e6e70f8
TM
323 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
324 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
325 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
326 nfsi->change_attr = 0;
327 inode->i_size = 0;
328 inode->i_nlink = 0;
329 inode->i_uid = -2;
330 inode->i_gid = -2;
331 inode->i_blocks = 0;
332 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
333
33801147 334 nfsi->read_cache_jiffies = fattr->time_start;
4704f0e2 335 nfsi->attr_gencount = fattr->gencount;
9e6e70f8
TM
336 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
337 inode->i_atime = fattr->atime;
62ab460c
TM
338 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
339 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
9e6e70f8
TM
340 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
341 inode->i_mtime = fattr->mtime;
62ab460c
TM
342 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
343 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
344 | NFS_INO_INVALID_DATA;
9e6e70f8
TM
345 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
346 inode->i_ctime = fattr->ctime;
62ab460c
TM
347 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
348 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
349 | NFS_INO_INVALID_ACCESS
350 | NFS_INO_INVALID_ACL;
9e6e70f8 351 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
1da177e4 352 nfsi->change_attr = fattr->change_attr;
62ab460c
TM
353 else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
354 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
355 | NFS_INO_INVALID_DATA;
9e6e70f8
TM
356 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
357 inode->i_size = nfs_size_to_loff_t(fattr->size);
62ab460c
TM
358 else
359 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
360 | NFS_INO_INVALID_DATA
361 | NFS_INO_REVAL_PAGECACHE;
9e6e70f8
TM
362 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
363 inode->i_nlink = fattr->nlink;
62ab460c
TM
364 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
365 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
9e6e70f8
TM
366 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
367 inode->i_uid = fattr->uid;
62ab460c
TM
368 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
369 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
370 | NFS_INO_INVALID_ACCESS
371 | NFS_INO_INVALID_ACL;
9e6e70f8
TM
372 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
373 inode->i_gid = fattr->gid;
62ab460c
TM
374 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
375 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
376 | NFS_INO_INVALID_ACCESS
377 | NFS_INO_INVALID_ACL;
9e6e70f8
TM
378 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
379 inode->i_blocks = fattr->du.nfs2.blocks;
380 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1da177e4
LT
381 /*
382 * report the blocks in 512byte units
383 */
384 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1da177e4
LT
385 }
386 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
b0c4fddc 387 nfsi->attrtimeo_timestamp = now;
1c3c07e9 388 nfsi->access_cache = RB_ROOT;
1da177e4 389
ef79c097
DH
390 nfs_fscache_init_inode_cookie(inode);
391
1da177e4
LT
392 unlock_new_inode(inode);
393 } else
394 nfs_refresh_inode(inode, fattr);
395 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
396 inode->i_sb->s_id,
397 (long long)NFS_FILEID(inode),
398 atomic_read(&inode->i_count));
399
400out:
401 return inode;
402
403out_no_inode:
03f28e3a 404 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
1da177e4
LT
405 goto out;
406}
407
659bfcd6 408#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
1da177e4
LT
409
410int
411nfs_setattr(struct dentry *dentry, struct iattr *attr)
412{
413 struct inode *inode = dentry->d_inode;
414 struct nfs_fattr fattr;
415 int error;
416
91d5b470
CL
417 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
418
188b95dd
JL
419 /* skip mode change if it's just for clearing setuid/setgid */
420 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
421 attr->ia_valid &= ~ATTR_MODE;
422
1da177e4
LT
423 if (attr->ia_valid & ATTR_SIZE) {
424 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
425 attr->ia_valid &= ~ATTR_SIZE;
426 }
427
428 /* Optimization: if the end result is no change, don't RPC */
429 attr->ia_valid &= NFS_VALID_ATTRS;
659bfcd6 430 if ((attr->ia_valid & ~ATTR_FILE) == 0)
1da177e4
LT
431 return 0;
432
755c1e20 433 /* Write all dirty data */
e1552e19
TM
434 if (S_ISREG(inode->i_mode)) {
435 filemap_write_and_wait(inode->i_mapping);
436 nfs_wb_all(inode);
437 }
642ac549
TM
438 /*
439 * Return any delegations if we're going to change ACLs
440 */
441 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
442 nfs_inode_return_delegation(inode);
1da177e4 443 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
65e4308d 444 if (error == 0)
1da177e4 445 nfs_refresh_inode(inode, &fattr);
65e4308d
TM
446 return error;
447}
448
a3d01454
TM
449/**
450 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
451 * @inode: inode of the file used
452 * @offset: file offset to start truncating
453 *
454 * This is a copy of the common vmtruncate, but with the locking
455 * corrected to take into account the fact that NFS requires
456 * inode->i_size to be updated under the inode->i_lock.
457 */
458static int nfs_vmtruncate(struct inode * inode, loff_t offset)
459{
460 if (i_size_read(inode) < offset) {
461 unsigned long limit;
462
463 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
464 if (limit != RLIM_INFINITY && offset > limit)
465 goto out_sig;
466 if (offset > inode->i_sb->s_maxbytes)
467 goto out_big;
468 spin_lock(&inode->i_lock);
469 i_size_write(inode, offset);
470 spin_unlock(&inode->i_lock);
471 } else {
472 struct address_space *mapping = inode->i_mapping;
473
474 /*
475 * truncation of in-use swapfiles is disallowed - it would
476 * cause subsequent swapout to scribble on the now-freed
477 * blocks.
478 */
479 if (IS_SWAPFILE(inode))
480 return -ETXTBSY;
481 spin_lock(&inode->i_lock);
482 i_size_write(inode, offset);
483 spin_unlock(&inode->i_lock);
484
485 /*
486 * unmap_mapping_range is called twice, first simply for
487 * efficiency so that truncate_inode_pages does fewer
488 * single-page unmaps. However after this first call, and
489 * before truncate_inode_pages finishes, it is possible for
490 * private pages to be COWed, which remain after
491 * truncate_inode_pages finishes, hence the second
492 * unmap_mapping_range call must be made for correctness.
493 */
494 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
495 truncate_inode_pages(mapping, offset);
496 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
497 }
498 return 0;
499out_sig:
500 send_sig(SIGXFSZ, current, 0);
501out_big:
502 return -EFBIG;
503}
504
65e4308d
TM
505/**
506 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
507 * @inode: pointer to struct inode
508 * @attr: pointer to struct iattr
509 *
510 * Note: we do this in the *proc.c in order to ensure that
511 * it works for things like exclusive creates too.
512 */
513void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
514{
515 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
2f28ea61 516 spin_lock(&inode->i_lock);
1da177e4 517 if ((attr->ia_valid & ATTR_MODE) != 0) {
65e4308d
TM
518 int mode = attr->ia_mode & S_IALLUGO;
519 mode |= inode->i_mode & ~S_IALLUGO;
1da177e4
LT
520 inode->i_mode = mode;
521 }
522 if ((attr->ia_valid & ATTR_UID) != 0)
523 inode->i_uid = attr->ia_uid;
524 if ((attr->ia_valid & ATTR_GID) != 0)
525 inode->i_gid = attr->ia_gid;
55296809 526 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
dc59250c 527 spin_unlock(&inode->i_lock);
65e4308d
TM
528 }
529 if ((attr->ia_valid & ATTR_SIZE) != 0) {
91d5b470 530 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
a3d01454 531 nfs_vmtruncate(inode, attr->ia_size);
65e4308d 532 }
1da177e4
LT
533}
534
1da177e4
LT
535int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
536{
537 struct inode *inode = dentry->d_inode;
55296809 538 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
1da177e4
LT
539 int err;
540
28c494c5
CL
541 /*
542 * Flush out writes to the server in order to update c/mtime.
543 *
544 * Hold the i_mutex to suspend application writes temporarily;
545 * this prevents long-running writing applications from blocking
546 * nfs_wb_nocommit.
547 */
548 if (S_ISREG(inode->i_mode)) {
549 mutex_lock(&inode->i_mutex);
ed90ef51 550 nfs_wb_nocommit(inode);
28c494c5
CL
551 mutex_unlock(&inode->i_mutex);
552 }
fc33a7bb
CH
553
554 /*
555 * We may force a getattr if the user cares about atime.
556 *
557 * Note that we only have to check the vfsmount flags here:
558 * - NFS always sets S_NOATIME by so checking it would give a
559 * bogus result
560 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
561 * no point in checking those.
562 */
563 if ((mnt->mnt_flags & MNT_NOATIME) ||
564 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
1da177e4 565 need_atime = 0;
fc33a7bb 566
1da177e4
LT
567 if (need_atime)
568 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
569 else
570 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
4e769b93 571 if (!err) {
1da177e4 572 generic_fillattr(inode, stat);
f43bf0be 573 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
4e769b93 574 }
1da177e4
LT
575 return err;
576}
577
7fe5c398
TM
578/**
579 * nfs_close_context - Common close_context() routine NFSv2/v3
580 * @ctx: pointer to context
581 * @is_sync: is this a synchronous close
582 *
583 * always ensure that the attributes are up to date if we're mounted
584 * with close-to-open semantics
585 */
586void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
587{
588 struct inode *inode;
589 struct nfs_server *server;
590
591 if (!(ctx->mode & FMODE_WRITE))
592 return;
593 if (!is_sync)
594 return;
595 inode = ctx->path.dentry->d_inode;
596 if (!list_empty(&NFS_I(inode)->open_files))
597 return;
598 server = NFS_SERVER(inode);
599 if (server->flags & NFS_MOUNT_NOCTO)
600 return;
601 nfs_revalidate_inode(server, inode);
602}
603
b92dccf6 604static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
1da177e4
LT
605{
606 struct nfs_open_context *ctx;
607
f52720ca 608 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
1da177e4 609 if (ctx != NULL) {
88be9f99
TM
610 ctx->path.dentry = dget(dentry);
611 ctx->path.mnt = mntget(mnt);
1da177e4
LT
612 ctx->cred = get_rpccred(cred);
613 ctx->state = NULL;
614 ctx->lockowner = current->files;
66d3aac0 615 ctx->flags = 0;
1da177e4 616 ctx->error = 0;
00a92642 617 ctx->dir_cookie = 0;
5e11934d 618 atomic_set(&ctx->count, 1);
1da177e4
LT
619 }
620 return ctx;
621}
622
623struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
624{
625 if (ctx != NULL)
5e11934d 626 atomic_inc(&ctx->count);
1da177e4
LT
627 return ctx;
628}
629
7fe5c398 630static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
1da177e4 631{
7fe5c398 632 struct inode *inode = ctx->path.dentry->d_inode;
3bec63db 633
5e11934d
TM
634 if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
635 return;
636 list_del(&ctx->list);
637 spin_unlock(&inode->i_lock);
7fe5c398 638 NFS_PROTO(inode)->close_context(ctx, is_sync);
3bec63db
TM
639 if (ctx->cred != NULL)
640 put_rpccred(ctx->cred);
31f31db1 641 path_put(&ctx->path);
3bec63db
TM
642 kfree(ctx);
643}
644
a49c3c77
TM
645void put_nfs_open_context(struct nfs_open_context *ctx)
646{
647 __put_nfs_open_context(ctx, 0);
648}
649
650static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
651{
652 __put_nfs_open_context(ctx, 1);
653}
654
1da177e4
LT
655/*
656 * Ensure that mmap has a recent RPC credential for use when writing out
657 * shared pages
658 */
b92dccf6 659static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1da177e4 660{
01cce933 661 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
662 struct nfs_inode *nfsi = NFS_I(inode);
663
664 filp->private_data = get_nfs_open_context(ctx);
665 spin_lock(&inode->i_lock);
666 list_add(&ctx->list, &nfsi->open_files);
667 spin_unlock(&inode->i_lock);
668}
669
d530838b
TM
670/*
671 * Given an inode, search for an open context with the desired characteristics
672 */
dc0b027d 673struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
1da177e4
LT
674{
675 struct nfs_inode *nfsi = NFS_I(inode);
676 struct nfs_open_context *pos, *ctx = NULL;
677
678 spin_lock(&inode->i_lock);
679 list_for_each_entry(pos, &nfsi->open_files, list) {
d530838b
TM
680 if (cred != NULL && pos->cred != cred)
681 continue;
1da177e4
LT
682 if ((pos->mode & mode) == mode) {
683 ctx = get_nfs_open_context(pos);
684 break;
685 }
686 }
687 spin_unlock(&inode->i_lock);
688 return ctx;
689}
690
b92dccf6 691static void nfs_file_clear_open_context(struct file *filp)
1da177e4 692{
01cce933 693 struct inode *inode = filp->f_path.dentry->d_inode;
cd3758e3 694 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1da177e4
LT
695
696 if (ctx) {
697 filp->private_data = NULL;
698 spin_lock(&inode->i_lock);
699 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
700 spin_unlock(&inode->i_lock);
a49c3c77 701 put_nfs_open_context_sync(ctx);
1da177e4
LT
702 }
703}
704
705/*
706 * These allocate and release file read/write context information.
707 */
708int nfs_open(struct inode *inode, struct file *filp)
709{
710 struct nfs_open_context *ctx;
711 struct rpc_cred *cred;
712
98a8e323 713 cred = rpc_lookup_cred();
1da177e4
LT
714 if (IS_ERR(cred))
715 return PTR_ERR(cred);
01cce933 716 ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
1da177e4
LT
717 put_rpccred(cred);
718 if (ctx == NULL)
719 return -ENOMEM;
720 ctx->mode = filp->f_mode;
721 nfs_file_set_open_context(filp, ctx);
722 put_nfs_open_context(ctx);
ef79c097 723 nfs_fscache_set_inode_cookie(inode, filp);
1da177e4
LT
724 return 0;
725}
726
727int nfs_release(struct inode *inode, struct file *filp)
728{
1da177e4
LT
729 nfs_file_clear_open_context(filp);
730 return 0;
731}
732
733/*
734 * This function is called whenever some part of NFS notices that
735 * the cached attributes have to be refreshed.
736 */
737int
738__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
739{
740 int status = -ESTALE;
741 struct nfs_fattr fattr;
742 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
743
744 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
745 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
746
85233a7a 747 if (is_bad_inode(inode))
691beb13 748 goto out;
1da177e4 749 if (NFS_STALE(inode))
412d582e 750 goto out;
7fdc49c4 751
691beb13 752 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1da177e4
LT
753 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
754 if (status != 0) {
755 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
756 inode->i_sb->s_id,
757 (long long)NFS_FILEID(inode), status);
758 if (status == -ESTALE) {
759 nfs_zap_caches(inode);
760 if (!S_ISDIR(inode->i_mode))
3a10c30a 761 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
1da177e4
LT
762 }
763 goto out;
764 }
765
4dc05efb 766 status = nfs_refresh_inode(inode, &fattr);
1da177e4
LT
767 if (status) {
768 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
769 inode->i_sb->s_id,
770 (long long)NFS_FILEID(inode), status);
771 goto out;
772 }
55296809 773
24aa1fe6 774 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
ada70d94 775 nfs_zap_acl_cache(inode);
55296809 776
1da177e4
LT
777 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
778 inode->i_sb->s_id,
779 (long long)NFS_FILEID(inode));
780
412d582e 781 out:
1da177e4
LT
782 return status;
783}
784
785int nfs_attribute_timeout(struct inode *inode)
786{
787 struct nfs_inode *nfsi = NFS_I(inode);
788
789 if (nfs_have_delegation(inode, FMODE_READ))
790 return 0;
64672d55 791 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
1da177e4
LT
792}
793
794/**
795 * nfs_revalidate_inode - Revalidate the inode attributes
796 * @server - pointer to nfs_server struct
797 * @inode - pointer to inode struct
798 *
799 * Updates inode attribute information by retrieving the data from the server.
800 */
801int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
802{
44b11874 803 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
1da177e4
LT
804 && !nfs_attribute_timeout(inode))
805 return NFS_STALE(inode) ? -ESTALE : 0;
806 return __nfs_revalidate_inode(server, inode);
807}
808
717d44e8
TM
809static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
810{
811 struct nfs_inode *nfsi = NFS_I(inode);
812
813 if (mapping->nrpages != 0) {
814 int ret = invalidate_inode_pages2(mapping);
815 if (ret < 0)
816 return ret;
817 }
818 spin_lock(&inode->i_lock);
819 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
2f78e431 820 if (S_ISDIR(inode->i_mode))
717d44e8 821 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
717d44e8
TM
822 spin_unlock(&inode->i_lock);
823 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
ef79c097 824 nfs_fscache_reset_inode_cookie(inode);
717d44e8
TM
825 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
826 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
827 return 0;
828}
829
830static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
831{
832 int ret = 0;
833
834 mutex_lock(&inode->i_mutex);
835 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
836 ret = nfs_sync_mapping(mapping);
837 if (ret == 0)
838 ret = nfs_invalidate_mapping_nolock(inode, mapping);
839 }
840 mutex_unlock(&inode->i_mutex);
841 return ret;
842}
843
7d52e862 844/**
717d44e8 845 * nfs_revalidate_mapping_nolock - Revalidate the pagecache
7d52e862
TM
846 * @inode - pointer to host inode
847 * @mapping - pointer to mapping
848 */
717d44e8 849int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
7d52e862
TM
850{
851 struct nfs_inode *nfsi = NFS_I(inode);
44b11874
TM
852 int ret = 0;
853
44b11874 854 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
717d44e8 855 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
44b11874 856 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
717d44e8
TM
857 if (ret < 0)
858 goto out;
859 }
860 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
861 ret = nfs_invalidate_mapping_nolock(inode, mapping);
862out:
863 return ret;
864}
7d52e862 865
717d44e8
TM
866/**
867 * nfs_revalidate_mapping - Revalidate the pagecache
868 * @inode - pointer to host inode
869 * @mapping - pointer to mapping
870 *
871 * This version of the function will take the inode->i_mutex and attempt to
872 * flush out all dirty data if it needs to invalidate the page cache.
873 */
874int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
875{
876 struct nfs_inode *nfsi = NFS_I(inode);
877 int ret = 0;
dc59250c 878
717d44e8
TM
879 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
880 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
881 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
882 if (ret < 0)
883 goto out;
7d52e862 884 }
717d44e8
TM
885 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
886 ret = nfs_invalidate_mapping(inode, mapping);
cd9ae2b6 887out:
44b11874 888 return ret;
7d52e862
TM
889}
890
a895b4a1
TM
891static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
892{
893 struct nfs_inode *nfsi = NFS_I(inode);
894
9e6e70f8
TM
895 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
896 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
897 && nfsi->change_attr == fattr->pre_change_attr) {
70ca8852
TM
898 nfsi->change_attr = fattr->change_attr;
899 if (S_ISDIR(inode->i_mode))
900 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
901 }
a895b4a1 902 /* If we have atomic WCC data, we may update some attributes */
9e6e70f8
TM
903 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
904 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
905 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
a895b4a1 906 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
9e6e70f8
TM
907
908 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
909 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
910 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
a895b4a1 911 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
e323ea46
TM
912 if (S_ISDIR(inode->i_mode))
913 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
a895b4a1 914 }
9e6e70f8
TM
915 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
916 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
917 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
918 && nfsi->npages == 0)
919 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
a895b4a1
TM
920}
921
1da177e4 922/**
33801147 923 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1da177e4
LT
924 * @inode - pointer to inode
925 * @fattr - updated attributes
926 *
927 * Verifies the attribute cache. If we have just changed the attributes,
928 * so that fattr carries weak cache consistency data, then it may
929 * also update the ctime/mtime/change_attribute.
930 */
33801147 931static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1da177e4
LT
932{
933 struct nfs_inode *nfsi = NFS_I(inode);
934 loff_t cur_size, new_isize;
2a3f5fd4 935 unsigned long invalid = 0;
1da177e4 936
dc59250c 937
ca62b9c3 938 /* Has the inode gone and changed behind our back? */
9e6e70f8
TM
939 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
940 return -EIO;
941 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
ca62b9c3 942 return -EIO;
ca62b9c3 943
9e6e70f8 944 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
f1bb0b92 945 nfsi->change_attr != fattr->change_attr)
2a3f5fd4 946 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1da177e4 947
1da177e4 948 /* Verify a few of the more important attributes */
9e6e70f8 949 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
2a3f5fd4 950 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
ca62b9c3 951
9e6e70f8
TM
952 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
953 cur_size = i_size_read(inode);
954 new_isize = nfs_size_to_loff_t(fattr->size);
955 if (cur_size != new_isize && nfsi->npages == 0)
956 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
957 }
1da177e4
LT
958
959 /* Have any file permissions changed? */
9e6e70f8
TM
960 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
961 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
962 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
963 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
964 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
2a3f5fd4 965 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
1da177e4
LT
966
967 /* Has the link count changed? */
9e6e70f8 968 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
2a3f5fd4 969 invalid |= NFS_INO_INVALID_ATTR;
1da177e4 970
9e6e70f8 971 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
2a3f5fd4
TM
972 invalid |= NFS_INO_INVALID_ATIME;
973
974 if (invalid != 0)
975 nfsi->cache_validity |= invalid;
1da177e4 976
33801147 977 nfsi->read_cache_jiffies = fattr->time_start;
1da177e4
LT
978 return 0;
979}
980
a10ad176 981static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
870a5be8 982{
9e6e70f8
TM
983 if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
984 return 0;
a10ad176
TM
985 return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
986}
987
988static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
989{
9e6e70f8
TM
990 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
991 return 0;
a10ad176
TM
992 return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
993}
870a5be8 994
ae05f269 995static atomic_long_t nfs_attr_generation_counter;
4704f0e2
TM
996
997static unsigned long nfs_read_attr_generation_counter(void)
998{
ae05f269 999 return atomic_long_read(&nfs_attr_generation_counter);
4704f0e2
TM
1000}
1001
1002unsigned long nfs_inc_attr_generation_counter(void)
1003{
ae05f269 1004 return atomic_long_inc_return(&nfs_attr_generation_counter);
4704f0e2
TM
1005}
1006
1007void nfs_fattr_init(struct nfs_fattr *fattr)
1008{
1009 fattr->valid = 0;
1010 fattr->time_start = jiffies;
1011 fattr->gencount = nfs_inc_attr_generation_counter();
1012}
1013
a10ad176
TM
1014/**
1015 * nfs_inode_attrs_need_update - check if the inode attributes need updating
1016 * @inode - pointer to inode
1017 * @fattr - attributes
1018 *
1019 * Attempt to divine whether or not an RPC call reply carrying stale
1020 * attributes got scheduled after another call carrying updated ones.
1021 *
1022 * To do so, the function first assumes that a more recent ctime means
1023 * that the attributes in fattr are newer, however it also attempt to
1024 * catch the case where ctime either didn't change, or went backwards
1025 * (if someone reset the clock on the server) by looking at whether
1026 * or not this RPC call was started after the inode was last updated.
4704f0e2 1027 * Note also the check for wraparound of 'attr_gencount'
a10ad176
TM
1028 *
1029 * The function returns 'true' if it thinks the attributes in 'fattr' are
1030 * more recent than the ones cached in the inode.
1031 *
1032 */
1033static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1034{
1035 const struct nfs_inode *nfsi = NFS_I(inode);
1036
4704f0e2 1037 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
03254e65
TM
1038 nfs_ctime_need_update(inode, fattr) ||
1039 nfs_size_need_update(inode, fattr) ||
4704f0e2 1040 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
a10ad176
TM
1041}
1042
1043static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1044{
1045 if (nfs_inode_attrs_need_update(inode, fattr))
870a5be8
TM
1046 return nfs_update_inode(inode, fattr);
1047 return nfs_check_inode_attributes(inode, fattr);
1048}
1049
33801147
TM
1050/**
1051 * nfs_refresh_inode - try to update the inode attribute cache
1052 * @inode - pointer to inode
1053 * @fattr - updated attributes
1054 *
1055 * Check that an RPC call that returned attributes has not overlapped with
1056 * other recent updates of the inode metadata, then decide whether it is
1057 * safe to do a full update of the inode attributes, or whether just to
1058 * call nfs_check_inode_attributes.
1059 */
1060int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1061{
33801147
TM
1062 int status;
1063
1064 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1065 return 0;
1066 spin_lock(&inode->i_lock);
870a5be8 1067 status = nfs_refresh_inode_locked(inode, fattr);
33801147 1068 spin_unlock(&inode->i_lock);
ef79c097 1069
33801147
TM
1070 return status;
1071}
1072
d65f557f
TM
1073static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1074{
1075 struct nfs_inode *nfsi = NFS_I(inode);
1076
1077 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1078 if (S_ISDIR(inode->i_mode))
1079 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1080 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1081 return 0;
1082 return nfs_refresh_inode_locked(inode, fattr);
1083}
1084
decf491f
TM
1085/**
1086 * nfs_post_op_update_inode - try to update the inode attribute cache
1087 * @inode - pointer to inode
1088 * @fattr - updated attributes
1089 *
1090 * After an operation that has changed the inode metadata, mark the
1091 * attribute cache as being invalid, then try to update it.
f551e44f
CL
1092 *
1093 * NB: if the server didn't return any post op attributes, this
1094 * function will force the retrieval of attributes before the next
1095 * NFS request. Thus it should be used only for operations that
1096 * are expected to change one or more attributes, to avoid
1097 * unnecessary NFS requests and trips through nfs_update_inode().
decf491f
TM
1098 */
1099int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1100{
d65f557f 1101 int status;
decf491f 1102
7668fdbe 1103 spin_lock(&inode->i_lock);
d65f557f 1104 status = nfs_post_op_update_inode_locked(inode, fattr);
7668fdbe 1105 spin_unlock(&inode->i_lock);
870a5be8 1106 return status;
decf491f
TM
1107}
1108
70ca8852
TM
1109/**
1110 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1111 * @inode - pointer to inode
1112 * @fattr - updated attributes
1113 *
1114 * After an operation that has changed the inode metadata, mark the
1115 * attribute cache as being invalid, then try to update it. Fake up
1116 * weak cache consistency data, if none exist.
1117 *
1118 * This function is mainly designed to be used by the ->write_done() functions.
1119 */
1120int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1121{
d65f557f
TM
1122 int status;
1123
1124 spin_lock(&inode->i_lock);
1125 /* Don't do a WCC update if these attributes are already stale */
1126 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1127 !nfs_inode_attrs_need_update(inode, fattr)) {
9e6e70f8
TM
1128 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1129 | NFS_ATTR_FATTR_PRESIZE
1130 | NFS_ATTR_FATTR_PREMTIME
1131 | NFS_ATTR_FATTR_PRECTIME);
d65f557f
TM
1132 goto out_noforce;
1133 }
9e6e70f8
TM
1134 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1135 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
70ca8852 1136 fattr->pre_change_attr = NFS_I(inode)->change_attr;
9e6e70f8 1137 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
70ca8852 1138 }
9e6e70f8
TM
1139 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1140 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
70ca8852 1141 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
9e6e70f8
TM
1142 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1143 }
1144 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1145 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
70ca8852 1146 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
9e6e70f8
TM
1147 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1148 }
1149 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1150 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
a3d01454 1151 fattr->pre_size = i_size_read(inode);
9e6e70f8 1152 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
70ca8852 1153 }
d65f557f
TM
1154out_noforce:
1155 status = nfs_post_op_update_inode_locked(inode, fattr);
1156 spin_unlock(&inode->i_lock);
1157 return status;
70ca8852
TM
1158}
1159
1da177e4
LT
1160/*
1161 * Many nfs protocol calls return the new file attributes after
1162 * an operation. Here we update the inode to reflect the state
1163 * of the server's inode.
1164 *
1165 * This is a bit tricky because we have to make sure all dirty pages
1166 * have been sent off to the server before calling invalidate_inode_pages.
1167 * To make sure no other process adds more write requests while we try
1168 * our best to flush them, we make them sleep during the attribute refresh.
1169 *
1170 * A very similar scenario holds for the dir cache.
1171 */
24aa1fe6 1172static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1da177e4 1173{
8b4bdcf8 1174 struct nfs_server *server;
1da177e4 1175 struct nfs_inode *nfsi = NFS_I(inode);
951a143b 1176 loff_t cur_isize, new_isize;
2a3f5fd4 1177 unsigned long invalid = 0;
3e7d950a 1178 unsigned long now = jiffies;
62ab460c 1179 unsigned long save_cache_validity;
1da177e4
LT
1180
1181 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
3110ff80 1182 __func__, inode->i_sb->s_id, inode->i_ino,
1da177e4
LT
1183 atomic_read(&inode->i_count), fattr->valid);
1184
9e6e70f8 1185 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
325cfed9 1186 goto out_fileid;
1da177e4
LT
1187
1188 /*
1189 * Make sure the inode's type hasn't changed.
1190 */
9e6e70f8 1191 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1da177e4
LT
1192 goto out_changed;
1193
8b4bdcf8 1194 server = NFS_SERVER(inode);
a0356862 1195 /* Update the fsid? */
9e6e70f8 1196 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
c37dcd33
TM
1197 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1198 !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
8b4bdcf8
TM
1199 server->fsid = fattr->fsid;
1200
1da177e4
LT
1201 /*
1202 * Update the read time so we don't revalidate too often.
1203 */
33801147 1204 nfsi->read_cache_jiffies = fattr->time_start;
3e7d950a 1205
62ab460c
TM
1206 save_cache_validity = nfsi->cache_validity;
1207 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1208 | NFS_INO_INVALID_ATIME
1209 | NFS_INO_REVAL_FORCED
1210 | NFS_INO_REVAL_PAGECACHE);
1da177e4 1211
a895b4a1
TM
1212 /* Do atomic weak cache consistency updates */
1213 nfs_wcc_update_inode(inode, fattr);
1214
47aabaa7 1215 /* More cache consistency checks */
9e6e70f8
TM
1216 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1217 if (nfsi->change_attr != fattr->change_attr) {
1218 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1219 inode->i_sb->s_id, inode->i_ino);
1220 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1221 if (S_ISDIR(inode->i_mode))
1222 nfs_force_lookup_revalidate(inode);
1223 nfsi->change_attr = fattr->change_attr;
1224 }
62ab460c
TM
1225 } else if (server->caps & NFS_CAP_CHANGE_ATTR)
1226 invalid |= save_cache_validity;
9e6e70f8
TM
1227
1228 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
47aabaa7
TM
1229 /* NFSv2/v3: Check if the mtime agrees */
1230 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1231 dprintk("NFS: mtime change on server for file %s/%ld\n",
1232 inode->i_sb->s_id, inode->i_ino);
1233 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
bfc69a45
TM
1234 if (S_ISDIR(inode->i_mode))
1235 nfs_force_lookup_revalidate(inode);
9e6e70f8 1236 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
47aabaa7 1237 }
62ab460c
TM
1238 } else if (server->caps & NFS_CAP_MTIME)
1239 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1240 | NFS_INO_INVALID_DATA
1241 | NFS_INO_REVAL_PAGECACHE
1242 | NFS_INO_REVAL_FORCED);
1243
9e6e70f8 1244 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
47aabaa7 1245 /* If ctime has changed we should definitely clear access+acl caches */
37d9d76d 1246 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
4704f0e2 1247 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
37d9d76d
N
1248 /* and probably clear data for a directory too as utimes can cause
1249 * havoc with our cache.
1250 */
1251 if (S_ISDIR(inode->i_mode)) {
1252 invalid |= NFS_INO_INVALID_DATA;
1253 nfs_force_lookup_revalidate(inode);
1254 }
9e6e70f8 1255 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
37d9d76d 1256 }
62ab460c
TM
1257 } else if (server->caps & NFS_CAP_CTIME)
1258 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1259 | NFS_INO_INVALID_ACCESS
1260 | NFS_INO_INVALID_ACL
1261 | NFS_INO_REVAL_FORCED);
47aabaa7 1262
951a143b 1263 /* Check if our cached file size is stale */
9e6e70f8
TM
1264 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1265 new_isize = nfs_size_to_loff_t(fattr->size);
1266 cur_isize = i_size_read(inode);
1267 if (new_isize != cur_isize) {
1268 /* Do we perhaps have any outstanding writes, or has
1269 * the file grown beyond our last write? */
1270 if (nfsi->npages == 0 || new_isize > cur_isize) {
1271 i_size_write(inode, new_isize);
1272 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1273 }
1274 dprintk("NFS: isize change on server for file %s/%ld\n",
1275 inode->i_sb->s_id, inode->i_ino);
1da177e4 1276 }
62ab460c
TM
1277 } else
1278 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1279 | NFS_INO_REVAL_PAGECACHE
1280 | NFS_INO_REVAL_FORCED);
1da177e4 1281
1da177e4 1282
9e6e70f8
TM
1283 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1284 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
62ab460c
TM
1285 else if (server->caps & NFS_CAP_ATIME)
1286 invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
1287 | NFS_INO_REVAL_FORCED);
1da177e4 1288
9e6e70f8
TM
1289 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1290 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1291 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1292 inode->i_mode = fattr->mode;
1293 }
62ab460c
TM
1294 } else if (server->caps & NFS_CAP_MODE)
1295 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1296 | NFS_INO_INVALID_ACCESS
1297 | NFS_INO_INVALID_ACL
1298 | NFS_INO_REVAL_FORCED);
1299
9e6e70f8
TM
1300 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
1301 if (inode->i_uid != fattr->uid) {
1302 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1303 inode->i_uid = fattr->uid;
1304 }
62ab460c
TM
1305 } else if (server->caps & NFS_CAP_OWNER)
1306 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1307 | NFS_INO_INVALID_ACCESS
1308 | NFS_INO_INVALID_ACL
1309 | NFS_INO_REVAL_FORCED);
1310
9e6e70f8
TM
1311 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
1312 if (inode->i_gid != fattr->gid) {
1313 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1314 inode->i_gid = fattr->gid;
1315 }
62ab460c
TM
1316 } else if (server->caps & NFS_CAP_OWNER_GROUP)
1317 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1318 | NFS_INO_INVALID_ACCESS
1319 | NFS_INO_INVALID_ACL
1320 | NFS_INO_REVAL_FORCED);
921615f1 1321
9e6e70f8
TM
1322 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1323 if (inode->i_nlink != fattr->nlink) {
1324 invalid |= NFS_INO_INVALID_ATTR;
1325 if (S_ISDIR(inode->i_mode))
1326 invalid |= NFS_INO_INVALID_DATA;
1327 inode->i_nlink = fattr->nlink;
1328 }
62ab460c
TM
1329 } else if (server->caps & NFS_CAP_NLINK)
1330 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1331 | NFS_INO_REVAL_FORCED);
1da177e4 1332
9e6e70f8 1333 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1da177e4
LT
1334 /*
1335 * report the blocks in 512byte units
1336 */
1337 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1da177e4 1338 }
9e6e70f8
TM
1339 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1340 inode->i_blocks = fattr->du.nfs2.blocks;
1da177e4
LT
1341
1342 /* Update attrtimeo value if we're out of the unstable period */
1343 if (invalid & NFS_INO_INVALID_ATTR) {
91d5b470 1344 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1da177e4 1345 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
3e7d950a 1346 nfsi->attrtimeo_timestamp = now;
4704f0e2 1347 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
6d2b2966 1348 } else {
64672d55 1349 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
6d2b2966
TM
1350 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1351 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1352 nfsi->attrtimeo_timestamp = now;
1353 }
1da177e4 1354 }
f2115dc9 1355 invalid &= ~NFS_INO_INVALID_ATTR;
1da177e4
LT
1356 /* Don't invalidate the data if we were to blame */
1357 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1358 || S_ISLNK(inode->i_mode)))
1359 invalid &= ~NFS_INO_INVALID_DATA;
412c77ce 1360 if (!nfs_have_delegation(inode, FMODE_READ) ||
62ab460c 1361 (save_cache_validity & NFS_INO_REVAL_FORCED))
55296809 1362 nfsi->cache_validity |= invalid;
1da177e4
LT
1363
1364 return 0;
1365 out_changed:
1366 /*
1367 * Big trouble! The inode has become a different object.
1368 */
1da177e4 1369 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
3110ff80 1370 __func__, inode->i_ino, inode->i_mode, fattr->mode);
b37b03b7 1371 out_err:
1da177e4
LT
1372 /*
1373 * No need to worry about unhashing the dentry, as the
1374 * lookup validation will know that the inode is bad.
1375 * (But we fall through to invalidate the caches.)
1376 */
1377 nfs_invalidate_inode(inode);
1da177e4 1378 return -ESTALE;
325cfed9
CL
1379
1380 out_fileid:
1381 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1382 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
54ceac45 1383 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
325cfed9
CL
1384 (long long)nfsi->fileid, (long long)fattr->fileid);
1385 goto out_err;
1da177e4
LT
1386}
1387
55a97593 1388
1da177e4
LT
1389#ifdef CONFIG_NFS_V4
1390
1da177e4
LT
1391/*
1392 * Clean out any remaining NFSv4 state that might be left over due
1393 * to open() calls that passed nfs_atomic_lookup, but failed to call
1394 * nfs_open().
1395 */
f7b422b1 1396void nfs4_clear_inode(struct inode *inode)
1da177e4 1397{
1da177e4 1398 /* If we are holding a delegation, return it! */
e6f81075 1399 nfs_inode_return_delegation_noreclaim(inode);
1da177e4
LT
1400 /* First call standard NFS clear_inode() code */
1401 nfs_clear_inode(inode);
1da177e4 1402}
1da177e4
LT
1403#endif
1404
f7b422b1 1405struct inode *nfs_alloc_inode(struct super_block *sb)
1da177e4
LT
1406{
1407 struct nfs_inode *nfsi;
e94b1766 1408 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
1da177e4
LT
1409 if (!nfsi)
1410 return NULL;
55296809
CL
1411 nfsi->flags = 0UL;
1412 nfsi->cache_validity = 0UL;
458818ed
TM
1413#ifdef CONFIG_NFS_V3_ACL
1414 nfsi->acl_access = ERR_PTR(-EAGAIN);
1415 nfsi->acl_default = ERR_PTR(-EAGAIN);
1416#endif
e50a1c2e
BF
1417#ifdef CONFIG_NFS_V4
1418 nfsi->nfs4_acl = NULL;
1419#endif /* CONFIG_NFS_V4 */
1da177e4
LT
1420 return &nfsi->vfs_inode;
1421}
1422
f7b422b1 1423void nfs_destroy_inode(struct inode *inode)
1da177e4
LT
1424{
1425 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1426}
1427
d75d5414
AM
1428static inline void nfs4_init_once(struct nfs_inode *nfsi)
1429{
1430#ifdef CONFIG_NFS_V4
1431 INIT_LIST_HEAD(&nfsi->open_states);
1432 nfsi->delegation = NULL;
1433 nfsi->delegation_state = 0;
1434 init_rwsem(&nfsi->rwsem);
1435#endif
1436}
f7b422b1 1437
51cc5068 1438static void init_once(void *foo)
1da177e4
LT
1439{
1440 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1441
a35afb83 1442 inode_init_once(&nfsi->vfs_inode);
a35afb83
CL
1443 INIT_LIST_HEAD(&nfsi->open_files);
1444 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1445 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
1446 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
a35afb83 1447 nfsi->npages = 0;
565277f6
TM
1448 atomic_set(&nfsi->silly_count, 1);
1449 INIT_HLIST_HEAD(&nfsi->silly_list);
1450 init_waitqueue_head(&nfsi->waitqueue);
a35afb83 1451 nfs4_init_once(nfsi);
1da177e4 1452}
20c2df83 1453
f7b422b1 1454static int __init nfs_init_inodecache(void)
1da177e4
LT
1455{
1456 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1457 sizeof(struct nfs_inode),
fffb60f9
PJ
1458 0, (SLAB_RECLAIM_ACCOUNT|
1459 SLAB_MEM_SPREAD),
20c2df83 1460 init_once);
1da177e4
LT
1461 if (nfs_inode_cachep == NULL)
1462 return -ENOMEM;
1463
1464 return 0;
1465}
1466
266bee88 1467static void nfs_destroy_inodecache(void)
1da177e4 1468{
1a1d92c1 1469 kmem_cache_destroy(nfs_inode_cachep);
1da177e4
LT
1470}
1471
5746006f
TM
1472struct workqueue_struct *nfsiod_workqueue;
1473
1474/*
1475 * start up the nfsiod workqueue
1476 */
1477static int nfsiod_start(void)
1478{
1479 struct workqueue_struct *wq;
1480 dprintk("RPC: creating workqueue nfsiod\n");
1481 wq = create_singlethread_workqueue("nfsiod");
1482 if (wq == NULL)
1483 return -ENOMEM;
1484 nfsiod_workqueue = wq;
1485 return 0;
1486}
1487
1488/*
1489 * Destroy the nfsiod workqueue
1490 */
1491static void nfsiod_stop(void)
1492{
1493 struct workqueue_struct *wq;
1494
1495 wq = nfsiod_workqueue;
1496 if (wq == NULL)
1497 return;
1498 nfsiod_workqueue = NULL;
1499 destroy_workqueue(wq);
1500}
1501
1da177e4
LT
1502/*
1503 * Initialize NFS
1504 */
1505static int __init init_nfs_fs(void)
1506{
1507 int err;
1508
8ec442ae
DH
1509 err = nfs_fscache_register();
1510 if (err < 0)
1511 goto out7;
1512
5746006f
TM
1513 err = nfsiod_start();
1514 if (err)
1515 goto out6;
1516
6aaca566
DH
1517 err = nfs_fs_proc_init();
1518 if (err)
1519 goto out5;
1520
1da177e4
LT
1521 err = nfs_init_nfspagecache();
1522 if (err)
1523 goto out4;
1524
1525 err = nfs_init_inodecache();
1526 if (err)
1527 goto out3;
1528
1529 err = nfs_init_readpagecache();
1530 if (err)
1531 goto out2;
1532
1533 err = nfs_init_writepagecache();
1534 if (err)
1535 goto out1;
1536
1da177e4
LT
1537 err = nfs_init_directcache();
1538 if (err)
1539 goto out0;
1da177e4
LT
1540
1541#ifdef CONFIG_PROC_FS
1542 rpc_proc_register(&nfs_rpcstat);
1543#endif
f7b422b1 1544 if ((err = register_nfs_fs()) != 0)
1da177e4
LT
1545 goto out;
1546 return 0;
1547out:
1548#ifdef CONFIG_PROC_FS
1549 rpc_proc_unregister("nfs");
1550#endif
1da177e4 1551 nfs_destroy_directcache();
6b59a754 1552out0:
6b59a754 1553 nfs_destroy_writepagecache();
1da177e4
LT
1554out1:
1555 nfs_destroy_readpagecache();
1556out2:
1557 nfs_destroy_inodecache();
1558out3:
1559 nfs_destroy_nfspagecache();
1560out4:
6aaca566
DH
1561 nfs_fs_proc_exit();
1562out5:
5746006f
TM
1563 nfsiod_stop();
1564out6:
8ec442ae
DH
1565 nfs_fscache_unregister();
1566out7:
1da177e4
LT
1567 return err;
1568}
1569
1570static void __exit exit_nfs_fs(void)
1571{
1da177e4 1572 nfs_destroy_directcache();
1da177e4
LT
1573 nfs_destroy_writepagecache();
1574 nfs_destroy_readpagecache();
1575 nfs_destroy_inodecache();
1576 nfs_destroy_nfspagecache();
8ec442ae 1577 nfs_fscache_unregister();
1da177e4
LT
1578#ifdef CONFIG_PROC_FS
1579 rpc_proc_unregister("nfs");
1580#endif
f7b422b1 1581 unregister_nfs_fs();
6aaca566 1582 nfs_fs_proc_exit();
5746006f 1583 nfsiod_stop();
1da177e4
LT
1584}
1585
1586/* Not quite true; I just maintain it */
1587MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1588MODULE_LICENSE("GPL");
f43bf0be 1589module_param(enable_ino64, bool, 0644);
1da177e4
LT
1590
1591module_init(init_nfs_fs)
1592module_exit(exit_nfs_fs)