blob: fe5a8b45d867d1ec8dcc6388d83463d63ac700b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/inode.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs inode and superblock handling functions
7 *
Alan Cox526719b2008-10-27 15:19:48 +00008 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Chuck Lever4ece3a22006-03-20 13:44:22 -050028#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/nfs4_mount.h>
32#include <linux/lockd/bind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/seq_file.h>
34#include <linux/mount.h>
35#include <linux/nfs_idmap.h>
36#include <linux/vfs.h>
Manoj Naik9cdb3882006-06-09 09:34:28 -040037#include <linux/inet.h>
38#include <linux/nfs_xdr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/system.h>
41#include <asm/uaccess.h>
42
Trond Myklebust4ce79712005-06-22 17:16:21 +000043#include "nfs4_fs.h"
Trond Myklebusta72b4422006-01-03 09:55:41 +010044#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "delegation.h"
Chuck Leverd9ef5a82006-03-20 13:44:13 -050046#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040047#include "internal.h"
David Howells8ec442a2009-04-03 16:42:42 +010048#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040052#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void nfs_invalidate_inode(struct inode *);
Trond Myklebust24aa1fe2005-12-03 15:20:07 -050058static int nfs_update_inode(struct inode *, struct nfs_fattr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Christoph Lametere18b8902006-12-06 20:33:20 -080060static struct kmem_cache * nfs_inode_cachep;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static inline unsigned long
63nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
64{
65 return nfs_fileid_to_ino_t(fattr->fileid);
66}
67
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040068/**
Trond Myklebust72cb77f2009-03-11 14:10:30 -040069 * 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
80/**
Trond Myklebustf43bf0b2007-10-09 12:01:04 -040081 * 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
David Howellsf7b422b2006-06-09 09:34:33 -040099int nfs_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int ret;
102
Trond Myklebustd30c8342006-12-13 15:23:47 -0500103 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
David Howellsf7b422b2006-06-09 09:34:33 -0400115void nfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Trond Myklebustda6d5032006-06-01 17:26:35 -0400117 /*
118 * The following should never happen...
119 */
120 BUG_ON(nfs_have_writebacks(inode));
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400121 BUG_ON(!list_empty(&NFS_I(inode)->open_files));
Trond Myklebustada70d92005-06-22 17:16:22 +0000122 nfs_zap_acl_cache(inode);
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400123 nfs_access_zap_cache(inode);
David Howellsef79c092009-04-03 16:42:43 +0100124 nfs_fscache_release_inode_cookie(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Trond Myklebust29884df2005-12-13 16:13:54 -0500127/**
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);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800137 ret = filemap_write_and_wait(mapping);
Trond Myklebust29884df2005-12-13 16:13:54 -0500138 if (ret != 0)
139 goto out;
140 ret = nfs_wb_all(mapping->host);
141out:
142 return ret;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * Invalidate the local caches
147 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500148static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct nfs_inode *nfsi = NFS_I(inode);
151 int mode = inode->i_mode;
152
Chuck Lever91d5b472006-03-20 13:44:14 -0500153 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
154
Trond Myklebustc7c20972007-09-28 19:22:40 -0400155 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
156 nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
159 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
Chuck Lever55296802005-08-18 11:24:09 -0700160 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else
Chuck Lever55296802005-08-18 11:24:09 -0700162 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500163}
Chuck Leverdc592502005-08-18 11:24:12 -0700164
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500165void nfs_zap_caches(struct inode *inode)
166{
167 spin_lock(&inode->i_lock);
168 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700169 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000170}
171
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700172void 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
Trond Myklebustf41f7412008-06-11 17:39:04 -0400181void nfs_zap_acl_cache(struct inode *inode)
Trond Myklebustada70d92005-06-22 17:16:22 +0000182{
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);
Chuck Leverdc592502005-08-18 11:24:12 -0700188 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700189 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700190 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Trond Myklebustc4812992007-09-28 17:11:45 -0400193void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500201 * Invalidate, but do not unhash, the inode.
202 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500204static void nfs_invalidate_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Benny Halevy3a10c302008-01-23 08:58:59 +0200206 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500207 nfs_zap_caches_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
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
Benny Halevy99fadcd2008-01-23 08:59:08 +0200243 set_nfs_fileid(inode, fattr->fileid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 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 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500262 struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned long hash;
264
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400265 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto out_no_inode;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400267 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out_no_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 hash = nfs_fattr_to_ino_t(fattr);
271
Trond Myklebust03f28e32006-03-20 13:44:48 -0500272 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
273 if (inode == NULL) {
274 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (inode->i_state & I_NEW) {
279 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800280 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
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;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400289 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Why so? Because we want revalidate for devices/FIFOs, and
295 * that's precisely what we have in nfs_file_inode_operations.
296 */
David Howells8fa5c002006-08-22 20:06:12 -0400297 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 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)) {
David Howells8fa5c002006-08-22 20:06:12 -0400303 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 inode->i_fop = &nfs_dir_operations;
305 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
306 && fattr->size <= NFS_LIMIT_READDIRPLUS)
Benny Halevy3a10c302008-01-23 08:58:59 +0200307 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Trond Myklebust55a97592006-06-09 09:34:19 -0400308 /* Deal with crossing mountpoints */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400309 if ((fattr->valid & NFS_ATTR_FATTR_FSID)
310 && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400311 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;
Trond Myklebust55a97592006-06-09 09:34:19 -0400315 inode->i_fop = NULL;
Trond Myklebustc37dcd332008-03-06 12:34:50 -0500316 set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
Trond Myklebust55a97592006-06-09 09:34:19 -0400317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } 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
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400323 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
Trond Myklebust33801142005-10-27 22:12:39 -0400334 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400335 nfsi->attr_gencount = fattr->gencount;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400336 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
337 inode->i_atime = fattr->atime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400338 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
339 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400340 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
341 inode->i_mtime = fattr->mtime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400342 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
343 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
344 | NFS_INO_INVALID_DATA;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400345 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
346 inode->i_ctime = fattr->ctime;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400347 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;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400351 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 nfsi->change_attr = fattr->change_attr;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400353 else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
354 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
355 | NFS_INO_INVALID_DATA;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400356 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
357 inode->i_size = nfs_size_to_loff_t(fattr->size);
Trond Myklebust62ab4602009-08-09 15:06:19 -0400358 else
359 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
360 | NFS_INO_INVALID_DATA
361 | NFS_INO_REVAL_PAGECACHE;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400362 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
363 inode->i_nlink = fattr->nlink;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400364 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
365 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400366 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
367 inode->i_uid = fattr->uid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400368 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;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400372 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
373 inode->i_gid = fattr->gid;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400374 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;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400378 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * report the blocks in 512byte units
383 */
384 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800387 nfsi->attrtimeo_timestamp = now;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400388 nfsi->access_cache = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
David Howellsef79c092009-04-03 16:42:43 +0100390 nfs_fscache_init_inode_cookie(inode);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 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:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500404 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto out;
406}
407
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400408#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
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
Chuck Lever91d5b472006-03-20 13:44:14 -0500417 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
418
Jeff Layton188b95d2007-10-18 03:05:21 -0700419 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 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;
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400430 if ((attr->ia_valid & ~ATTR_FILE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432
Trond Myklebust755c1e22006-03-20 13:44:06 -0500433 /* Write all dirty data */
Trond Myklebuste1552e12007-04-14 19:07:28 -0400434 if (S_ISREG(inode->i_mode)) {
435 filemap_write_and_wait(inode->i_mapping);
436 nfs_wb_all(inode);
437 }
Trond Myklebust642ac542005-10-18 14:20:19 -0700438 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400444 if (error == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 nfs_refresh_inode(inode, &fattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400446 return error;
447}
448
449/**
Trond Myklebusta3d01452008-06-11 12:21:19 -0400450 * 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
505/**
Trond Myklebust65e43082005-08-16 11:49:44 -0400506 * 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) {
Trond Myklebust2f28ea62008-10-05 14:26:11 -0400516 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400518 int mode = attr->ia_mode & S_IALLUGO;
519 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 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;
Chuck Lever55296802005-08-18 11:24:09 -0700526 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700527 spin_unlock(&inode->i_lock);
Trond Myklebust65e43082005-08-16 11:49:44 -0400528 }
529 if ((attr->ia_valid & ATTR_SIZE) != 0) {
Chuck Lever91d5b472006-03-20 13:44:14 -0500530 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
Trond Myklebusta3d01452008-06-11 12:21:19 -0400531 nfs_vmtruncate(inode, attr->ia_size);
Trond Myklebust65e43082005-08-16 11:49:44 -0400532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
536{
537 struct inode *inode = dentry->d_inode;
Chuck Lever55296802005-08-18 11:24:09 -0700538 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int err;
540
Chuck Lever28c494c2007-10-26 13:32:13 -0400541 /*
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);
Trond Myklebusted90ef52007-07-20 13:13:28 -0400550 nfs_wb_nocommit(inode);
Chuck Lever28c494c2007-10-26 13:32:13 -0400551 mutex_unlock(&inode->i_mutex);
552 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800553
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)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 need_atime = 0;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (need_atime)
568 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
569 else
570 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
Peter Staubach4e769b92007-08-03 15:07:10 -0400571 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 generic_fillattr(inode, stat);
Trond Myklebustf43bf0b2007-10-09 12:01:04 -0400573 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
Peter Staubach4e769b92007-08-03 15:07:10 -0400574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return err;
576}
577
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400578/**
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
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500604static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct nfs_open_context *ctx;
607
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700608 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (ctx != NULL) {
Trond Myklebust88be9f92007-06-05 10:42:27 -0400610 ctx->path.dentry = dget(dentry);
611 ctx->path.mnt = mntget(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ctx->cred = get_rpccred(cred);
613 ctx->state = NULL;
614 ctx->lockowner = current->files;
Jeff Layton66d3aac2008-03-31 15:01:58 -0400615 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ctx->error = 0;
Olivier Galibert00a92642005-06-22 17:16:29 +0000617 ctx->dir_cookie = 0;
Trond Myklebust5e119342007-07-26 12:06:17 -0400618 atomic_set(&ctx->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 return ctx;
621}
622
623struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
624{
625 if (ctx != NULL)
Trond Myklebust5e119342007-07-26 12:06:17 -0400626 atomic_inc(&ctx->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return ctx;
628}
629
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400630static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400631{
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400632 struct inode *inode = ctx->path.dentry->d_inode;
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400633
Trond Myklebust5e119342007-07-26 12:06:17 -0400634 if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
635 return;
636 list_del(&ctx->list);
637 spin_unlock(&inode->i_lock);
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400638 NFS_PROTO(inode)->close_context(ctx, is_sync);
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400639 if (ctx->cred != NULL)
640 put_rpccred(ctx->cred);
Jan Blunck31f31db12008-05-02 13:42:45 -0700641 path_put(&ctx->path);
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400642 kfree(ctx);
643}
644
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400645void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * Ensure that mmap has a recent RPC credential for use when writing out
657 * shared pages
658 */
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500659static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800661 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 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
Trond Myklebustd5308382005-11-04 15:33:38 -0500670/*
671 * Given an inode, search for an open context with the desired characteristics
672 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500673struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
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) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500680 if (cred != NULL && pos->cred != cred)
681 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 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
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500691static void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800693 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400694 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
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);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400701 put_nfs_open_context_sync(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
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
Trond Myklebust98a8e322008-03-12 12:25:28 -0400713 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (IS_ERR(cred))
715 return PTR_ERR(cred);
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800716 ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 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);
David Howellsef79c092009-04-03 16:42:43 +0100723 nfs_fscache_set_inode_cookie(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
727int nfs_release(struct inode *inode, struct file *filp)
728{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
745 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
746
Chuck Lever85233a72006-10-19 23:28:42 -0700747 if (is_bad_inode(inode))
Chuck Lever412d5822005-08-18 11:24:11 -0700748 goto out;
Trond Myklebust7fdc49c2007-09-28 19:11:33 -0400749 if (NFS_STALE(inode))
750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Trond Myklebust691beb12008-10-05 14:48:22 -0400752 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 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))
Benny Halevy3a10c302008-01-23 08:58:59 +0200761 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 goto out;
764 }
765
Trond Myklebust4dc05ef2008-09-23 17:28:42 -0400766 status = nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 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 }
Chuck Lever55296802005-08-18 11:24:09 -0700773
Trond Myklebust24aa1fe2005-12-03 15:20:07 -0500774 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +0000775 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
778 inode->i_sb->s_id,
779 (long long)NFS_FILEID(inode));
780
Chuck Lever412d5822005-08-18 11:24:11 -0700781 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 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;
Peter Staubach64672d52008-12-23 15:21:56 -0500791 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
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{
Trond Myklebust44b11872006-05-25 01:40:59 -0400803 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 && !nfs_attribute_timeout(inode))
805 return NFS_STALE(inode) ? -ESTALE : 0;
806 return __nfs_revalidate_inode(server, inode);
807}
808
Trond Myklebust717d44e2007-01-24 11:54:55 -0800809static 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;
Trond Myklebust2f78e432007-09-30 15:31:19 -0400820 if (S_ISDIR(inode->i_mode))
Trond Myklebust717d44e2007-01-24 11:54:55 -0800821 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
Trond Myklebust717d44e2007-01-24 11:54:55 -0800822 spin_unlock(&inode->i_lock);
823 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
David Howellsef79c092009-04-03 16:42:43 +0100824 nfs_fscache_reset_inode_cookie(inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -0800825 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
844/**
845 * nfs_revalidate_mapping_nolock - Revalidate the pagecache
846 * @inode - pointer to host inode
847 * @mapping - pointer to mapping
848 */
849int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
850{
851 struct nfs_inode *nfsi = NFS_I(inode);
852 int ret = 0;
853
854 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
855 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
856 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
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}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/**
Trond Myklebust7d52e862005-06-22 17:16:30 +0000867 * nfs_revalidate_mapping - Revalidate the pagecache
868 * @inode - pointer to host inode
869 * @mapping - pointer to mapping
Trond Myklebust717d44e2007-01-24 11:54:55 -0800870 *
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.
Trond Myklebust7d52e862005-06-22 17:16:30 +0000873 */
Trond Myklebust44b11872006-05-25 01:40:59 -0400874int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +0000875{
876 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust44b11872006-05-25 01:40:59 -0400877 int ret = 0;
878
Trond Myklebust44b11872006-05-25 01:40:59 -0400879 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
Trond Myklebust717d44e2007-01-24 11:54:55 -0800880 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
Trond Myklebust44b11872006-05-25 01:40:59 -0400881 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -0800882 if (ret < 0)
883 goto out;
Trond Myklebust7d52e862005-06-22 17:16:30 +0000884 }
Trond Myklebust717d44e2007-01-24 11:54:55 -0800885 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
886 ret = nfs_invalidate_mapping(inode, mapping);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700887out:
Trond Myklebust44b11872006-05-25 01:40:59 -0400888 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +0000889}
890
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100891static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
892{
893 struct nfs_inode *nfsi = NFS_I(inode);
894
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400895 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
896 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
897 && nfsi->change_attr == fattr->pre_change_attr) {
Trond Myklebust70ca8852007-09-30 15:21:24 -0400898 nfsi->change_attr = fattr->change_attr;
899 if (S_ISDIR(inode->i_mode))
900 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
901 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100902 /* If we have atomic WCC data, we may update some attributes */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400903 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
904 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
905 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100906 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400907
908 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
909 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
910 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100911 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
Trond Myklebuste323ea42007-09-30 17:03:25 -0400912 if (S_ISDIR(inode->i_mode))
913 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100914 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400915 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));
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/**
Trond Myklebust33801142005-10-27 22:12:39 -0400923 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * @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 */
Trond Myklebust33801142005-10-27 22:12:39 -0400931static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct nfs_inode *nfsi = NFS_I(inode);
934 loff_t cur_size, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400935 unsigned long invalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Chuck Leverdc592502005-08-18 11:24:12 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* Has the inode gone and changed behind our back? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400939 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EIO;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400941 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
942 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400944 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
Trond Myklebustf1bb0b92006-05-25 01:40:55 -0400945 nfsi->change_attr != fattr->change_attr)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400946 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* Verify a few of the more important attributes */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400949 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400950 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustca62b9c2006-03-20 13:44:07 -0500951
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400952 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* Have any file permissions changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400960 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)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400965 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* Has the link count changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400968 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400969 invalid |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400971 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400972 invalid |= NFS_INO_INVALID_ATIME;
973
974 if (invalid != 0)
975 nfsi->cache_validity |= invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Trond Myklebust33801142005-10-27 22:12:39 -0400977 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979}
980
Trond Myklebusta10ad172008-09-23 17:28:41 -0400981static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
982{
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400983 if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
984 return 0;
Trond Myklebusta10ad172008-09-23 17:28:41 -0400985 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{
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400990 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
991 return 0;
Trond Myklebusta10ad172008-09-23 17:28:41 -0400992 return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
993}
994
Trond Myklebustae05f262008-10-28 15:21:40 -0400995static atomic_long_t nfs_attr_generation_counter;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400996
997static unsigned long nfs_read_attr_generation_counter(void)
998{
Trond Myklebustae05f262008-10-28 15:21:40 -0400999 return atomic_long_read(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001000}
1001
1002unsigned long nfs_inc_attr_generation_counter(void)
1003{
Trond Myklebustae05f262008-10-28 15:21:40 -04001004 return atomic_long_inc_return(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001005}
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
Trond Myklebusta10ad172008-09-23 17:28:41 -04001014/**
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.
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001027 * Note also the check for wraparound of 'attr_gencount'
Trond Myklebusta10ad172008-09-23 17:28:41 -04001028 *
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
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001037 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
Trond Myklebust03254e62008-10-09 13:27:55 -04001038 nfs_ctime_need_update(inode, fattr) ||
1039 nfs_size_need_update(inode, fattr) ||
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001040 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
Trond Myklebusta10ad172008-09-23 17:28:41 -04001041}
1042
Trond Myklebust870a5be2008-10-05 12:07:23 -04001043static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1044{
Trond Myklebusta10ad172008-09-23 17:28:41 -04001045 if (nfs_inode_attrs_need_update(inode, fattr))
Trond Myklebust870a5be2008-10-05 12:07:23 -04001046 return nfs_update_inode(inode, fattr);
1047 return nfs_check_inode_attributes(inode, fattr);
1048}
1049
Trond Myklebust33801142005-10-27 22:12:39 -04001050/**
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{
Trond Myklebust33801142005-10-27 22:12:39 -04001062 int status;
1063
1064 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1065 return 0;
1066 spin_lock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001067 status = nfs_refresh_inode_locked(inode, fattr);
Trond Myklebust33801142005-10-27 22:12:39 -04001068 spin_unlock(&inode->i_lock);
David Howellsef79c092009-04-03 16:42:43 +01001069
Trond Myklebust33801142005-10-27 22:12:39 -04001070 return status;
1071}
1072
Trond Myklebustd65f5572008-10-05 12:27:55 -04001073static 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
Trond Myklebustdecf4912005-10-27 22:12:39 -04001085/**
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.
Chuck Leverf551e442006-09-20 14:33:04 -04001092 *
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().
Trond Myklebustdecf4912005-10-27 22:12:39 -04001098 */
1099int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1100{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001101 int status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001102
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001103 spin_lock(&inode->i_lock);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001104 status = nfs_post_op_update_inode_locked(inode, fattr);
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001105 spin_unlock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001106 return status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001107}
1108
Trond Myklebust70ca8852007-09-30 15:21:24 -04001109/**
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{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001122 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)) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001128 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1129 | NFS_ATTR_FATTR_PRESIZE
1130 | NFS_ATTR_FATTR_PREMTIME
1131 | NFS_ATTR_FATTR_PRECTIME);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001132 goto out_noforce;
1133 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001134 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1135 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001136 fattr->pre_change_attr = NFS_I(inode)->change_attr;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001137 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001138 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001139 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1140 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001141 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001142 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1143 }
1144 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1145 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001146 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001147 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1148 }
1149 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1150 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
Trond Myklebusta3d01452008-06-11 12:21:19 -04001151 fattr->pre_size = i_size_read(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001152 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001153 }
Trond Myklebustd65f5572008-10-05 12:27:55 -04001154out_noforce:
1155 status = nfs_post_op_update_inode_locked(inode, fattr);
1156 spin_unlock(&inode->i_lock);
1157 return status;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
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 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001172static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001174 struct nfs_server *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001176 loff_t cur_isize, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001177 unsigned long invalid = 0;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001178 unsigned long now = jiffies;
Trond Myklebust62ab4602009-08-09 15:06:19 -04001179 unsigned long save_cache_validity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001182 __func__, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 atomic_read(&inode->i_count), fattr->valid);
1184
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001185 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
Chuck Lever325cfed2005-11-30 18:08:55 -05001186 goto out_fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /*
1189 * Make sure the inode's type hasn't changed.
1190 */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001191 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out_changed;
1193
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001194 server = NFS_SERVER(inode);
Trond Myklebusta0356862007-06-05 13:26:15 -04001195 /* Update the fsid? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001196 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Trond Myklebustc37dcd332008-03-06 12:34:50 -05001197 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1198 !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001199 server->fsid = fattr->fsid;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /*
1202 * Update the read time so we don't revalidate too often.
1203 */
Trond Myklebust33801142005-10-27 22:12:39 -04001204 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001205
Trond Myklebust62ab4602009-08-09 15:06:19 -04001206 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001212 /* Do atomic weak cache consistency updates */
1213 nfs_wcc_update_inode(inode, fattr);
1214
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001215 /* More cache consistency checks */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001216 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 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001225 } else if (server->caps & NFS_CAP_CHANGE_ATTR)
1226 invalid |= save_cache_validity;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001227
1228 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001229 /* 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;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001234 if (S_ISDIR(inode->i_mode))
1235 nfs_force_lookup_revalidate(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001236 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001237 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001238 } 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
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001244 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001245 /* If ctime has changed we should definitely clear access+acl caches */
NeilBrown37d9d762009-03-11 14:10:23 -04001246 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001247 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
NeilBrown37d9d762009-03-11 14:10:23 -04001248 /* 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 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001255 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
NeilBrown37d9d762009-03-11 14:10:23 -04001256 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001257 } 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);
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001262
Trond Myklebust951a1432005-06-22 17:16:30 +00001263 /* Check if our cached file size is stale */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001264 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001277 } else
1278 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1279 | NFS_INO_REVAL_PAGECACHE
1280 | NFS_INO_REVAL_FORCED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001283 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1284 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
Trond Myklebust62ab4602009-08-09 15:06:19 -04001285 else if (server->caps & NFS_CAP_ATIME)
1286 invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
1287 | NFS_INO_REVAL_FORCED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001289 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 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001294 } 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
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001300 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 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001305 } 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
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001311 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 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001316 } 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);
Trond Myklebust921615f2008-10-14 19:23:07 -04001321
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001322 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 }
Trond Myklebust62ab4602009-08-09 15:06:19 -04001329 } else if (server->caps & NFS_CAP_NLINK)
1330 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1331 | NFS_INO_REVAL_FORCED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001333 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * report the blocks in 512byte units
1336 */
1337 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001339 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1340 inode->i_blocks = fattr->du.nfs2.blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* Update attrtimeo value if we're out of the unstable period */
1343 if (invalid & NFS_INO_INVALID_ATTR) {
Chuck Lever91d5b472006-03-20 13:44:14 -05001344 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001346 nfsi->attrtimeo_timestamp = now;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001347 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6d2b2962007-10-01 18:57:50 -04001348 } else {
Peter Staubach64672d52008-12-23 15:21:56 -05001349 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
Trond Myklebust6d2b2962007-10-01 18:57:50 -04001350 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1351 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1352 nfsi->attrtimeo_timestamp = now;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Trond Myklebustf2115dc2007-08-15 12:49:17 -04001355 invalid &= ~NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* 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;
Trond Myklebust412c77c2007-07-03 16:10:55 -04001360 if (!nfs_have_delegation(inode, FMODE_READ) ||
Trond Myklebust62ab4602009-08-09 15:06:19 -04001361 (save_cache_validity & NFS_INO_REVAL_FORCED))
Chuck Lever55296802005-08-18 11:24:09 -07001362 nfsi->cache_validity |= invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 return 0;
1365 out_changed:
1366 /*
1367 * Big trouble! The inode has become a different object.
1368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001370 __func__, inode->i_ino, inode->i_mode, fattr->mode);
Trond Myklebustb37b03b2005-11-25 17:10:06 -05001371 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -ESTALE;
Chuck Lever325cfed2005-11-30 18:08:55 -05001379
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",
David Howells54ceac42006-08-22 20:06:13 -04001383 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
Chuck Lever325cfed2005-11-30 18:08:55 -05001384 (long long)nfsi->fileid, (long long)fattr->fileid);
1385 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Trond Myklebust55a97592006-06-09 09:34:19 -04001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#ifdef CONFIG_NFS_V4
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
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 */
David Howellsf7b422b2006-06-09 09:34:33 -04001396void nfs4_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* If we are holding a delegation, return it! */
Trond Myklebuste6f81072008-01-24 18:14:34 -05001399 nfs_inode_return_delegation_noreclaim(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* First call standard NFS clear_inode() code */
1401 nfs_clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif
1404
David Howellsf7b422b2006-06-09 09:34:33 -04001405struct inode *nfs_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct nfs_inode *nfsi;
Christoph Lametere94b1762006-12-06 20:33:17 -08001408 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!nfsi)
1410 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07001411 nfsi->flags = 0UL;
1412 nfsi->cache_validity = 0UL;
Trond Myklebust458818e2005-06-22 17:16:27 +00001413#ifdef CONFIG_NFS_V3_ACL
1414 nfsi->acl_access = ERR_PTR(-EAGAIN);
1415 nfsi->acl_default = ERR_PTR(-EAGAIN);
1416#endif
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00001417#ifdef CONFIG_NFS_V4
1418 nfsi->nfs4_acl = NULL;
1419#endif /* CONFIG_NFS_V4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return &nfsi->vfs_inode;
1421}
1422
David Howellsf7b422b2006-06-09 09:34:33 -04001423void nfs_destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1426}
1427
Andrew Mortond75d5412006-06-25 02:41:26 -07001428static 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}
David Howellsf7b422b2006-06-09 09:34:33 -04001437
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001438static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1441
Christoph Lametera35afb82007-05-16 22:10:57 -07001442 inode_init_once(&nfsi->vfs_inode);
Christoph Lametera35afb82007-05-16 22:10:57 -07001443 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);
Christoph Lametera35afb82007-05-16 22:10:57 -07001447 nfsi->npages = 0;
Trond Myklebust565277f2007-10-15 18:17:53 -04001448 atomic_set(&nfsi->silly_count, 1);
1449 INIT_HLIST_HEAD(&nfsi->silly_list);
1450 init_waitqueue_head(&nfsi->waitqueue);
Christoph Lametera35afb82007-05-16 22:10:57 -07001451 nfs4_init_once(nfsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
Paul Mundt20c2df82007-07-20 10:11:58 +09001453
David Howellsf7b422b2006-06-09 09:34:33 -04001454static int __init nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1457 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001458 0, (SLAB_RECLAIM_ACCOUNT|
1459 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001460 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (nfs_inode_cachep == NULL)
1462 return -ENOMEM;
1463
1464 return 0;
1465}
1466
David Brownell266bee82006-06-27 12:59:15 -07001467static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001469 kmem_cache_destroy(nfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Trond Myklebust57460062008-02-19 20:04:22 -05001472struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
1503 * Initialize NFS
1504 */
1505static int __init init_nfs_fs(void)
1506{
1507 int err;
1508
David Howells8ec442a2009-04-03 16:42:42 +01001509 err = nfs_fscache_register();
1510 if (err < 0)
1511 goto out7;
1512
Trond Myklebust57460062008-02-19 20:04:22 -05001513 err = nfsiod_start();
1514 if (err)
1515 goto out6;
1516
David Howells6aaca562006-08-22 20:06:13 -04001517 err = nfs_fs_proc_init();
1518 if (err)
1519 goto out5;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 err = nfs_init_directcache();
1538 if (err)
1539 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541#ifdef CONFIG_PROC_FS
1542 rpc_proc_register(&nfs_rpcstat);
1543#endif
David Howellsf7b422b2006-06-09 09:34:33 -04001544 if ((err = register_nfs_fs()) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto out;
1546 return 0;
1547out:
1548#ifdef CONFIG_PROC_FS
1549 rpc_proc_unregister("nfs");
1550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 nfs_destroy_directcache();
Chuck Lever6b59a752005-11-30 18:08:19 -05001552out0:
Chuck Lever6b59a752005-11-30 18:08:19 -05001553 nfs_destroy_writepagecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554out1:
1555 nfs_destroy_readpagecache();
1556out2:
1557 nfs_destroy_inodecache();
1558out3:
1559 nfs_destroy_nfspagecache();
1560out4:
David Howells6aaca562006-08-22 20:06:13 -04001561 nfs_fs_proc_exit();
1562out5:
Trond Myklebust57460062008-02-19 20:04:22 -05001563 nfsiod_stop();
1564out6:
David Howells8ec442a2009-04-03 16:42:42 +01001565 nfs_fscache_unregister();
1566out7:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return err;
1568}
1569
1570static void __exit exit_nfs_fs(void)
1571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 nfs_destroy_directcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 nfs_destroy_writepagecache();
1574 nfs_destroy_readpagecache();
1575 nfs_destroy_inodecache();
1576 nfs_destroy_nfspagecache();
David Howells8ec442a2009-04-03 16:42:42 +01001577 nfs_fscache_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578#ifdef CONFIG_PROC_FS
1579 rpc_proc_unregister("nfs");
1580#endif
David Howellsf7b422b2006-06-09 09:34:33 -04001581 unregister_nfs_fs();
David Howells6aaca562006-08-22 20:06:13 -04001582 nfs_fs_proc_exit();
Trond Myklebust57460062008-02-19 20:04:22 -05001583 nfsiod_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/* Not quite true; I just maintain it */
1587MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1588MODULE_LICENSE("GPL");
Trond Myklebustf43bf0b2007-10-09 12:01:04 -04001589module_param(enable_ino64, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591module_init(init_nfs_fs)
1592module_exit(exit_nfs_fs)