blob: a834d1d850b78caf0df89e3f293b9f4b9efb88e7 [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>
33#include <linux/smp_lock.h>
34#include <linux/seq_file.h>
35#include <linux/mount.h>
36#include <linux/nfs_idmap.h>
37#include <linux/vfs.h>
Manoj Naik9cdb3882006-06-09 09:34:28 -040038#include <linux/inet.h>
39#include <linux/nfs_xdr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/system.h>
42#include <asm/uaccess.h>
43
Trond Myklebust4ce79712005-06-22 17:16:21 +000044#include "nfs4_fs.h"
Trond Myklebusta72b4422006-01-03 09:55:41 +010045#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "delegation.h"
Chuck Leverd9ef5a82006-03-20 13:44:13 -050047#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040048#include "internal.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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Trond Myklebust29884df2005-12-13 16:13:54 -0500126/**
127 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
128 */
129int nfs_sync_mapping(struct address_space *mapping)
130{
131 int ret;
132
133 if (mapping->nrpages == 0)
134 return 0;
135 unmap_mapping_range(mapping, 0, 0, 0);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800136 ret = filemap_write_and_wait(mapping);
Trond Myklebust29884df2005-12-13 16:13:54 -0500137 if (ret != 0)
138 goto out;
139 ret = nfs_wb_all(mapping->host);
140out:
141 return ret;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Invalidate the local caches
146 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500147static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct nfs_inode *nfsi = NFS_I(inode);
150 int mode = inode->i_mode;
151
Chuck Lever91d5b472006-03-20 13:44:14 -0500152 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
153
Trond Myklebustc7c20972007-09-28 19:22:40 -0400154 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
155 nfsi->attrtimeo_timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
158 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
Chuck Lever55296802005-08-18 11:24:09 -0700159 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 -0700160 else
Chuck Lever55296802005-08-18 11:24:09 -0700161 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 -0500162}
Chuck Leverdc592502005-08-18 11:24:12 -0700163
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500164void nfs_zap_caches(struct inode *inode)
165{
166 spin_lock(&inode->i_lock);
167 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700168 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000169}
170
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700171void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
172{
173 if (mapping->nrpages != 0) {
174 spin_lock(&inode->i_lock);
175 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
176 spin_unlock(&inode->i_lock);
177 }
178}
179
Trond Myklebustf41f7412008-06-11 17:39:04 -0400180void nfs_zap_acl_cache(struct inode *inode)
Trond Myklebustada70d92005-06-22 17:16:22 +0000181{
182 void (*clear_acl_cache)(struct inode *);
183
184 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
185 if (clear_acl_cache != NULL)
186 clear_acl_cache(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700187 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700188 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700189 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Trond Myklebustc4812992007-09-28 17:11:45 -0400192void nfs_invalidate_atime(struct inode *inode)
193{
194 spin_lock(&inode->i_lock);
195 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
196 spin_unlock(&inode->i_lock);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500200 * Invalidate, but do not unhash, the inode.
201 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500203static void nfs_invalidate_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Benny Halevy3a10c302008-01-23 08:58:59 +0200205 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500206 nfs_zap_caches_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209struct nfs_find_desc {
210 struct nfs_fh *fh;
211 struct nfs_fattr *fattr;
212};
213
214/*
215 * In NFSv3 we can have 64bit inode numbers. In order to support
216 * this, and re-exported directories (also seen in NFSv2)
217 * we are forced to allow 2 different inodes to have the same
218 * i_ino.
219 */
220static int
221nfs_find_actor(struct inode *inode, void *opaque)
222{
223 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
224 struct nfs_fh *fh = desc->fh;
225 struct nfs_fattr *fattr = desc->fattr;
226
227 if (NFS_FILEID(inode) != fattr->fileid)
228 return 0;
229 if (nfs_compare_fh(NFS_FH(inode), fh))
230 return 0;
231 if (is_bad_inode(inode) || NFS_STALE(inode))
232 return 0;
233 return 1;
234}
235
236static int
237nfs_init_locked(struct inode *inode, void *opaque)
238{
239 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
240 struct nfs_fattr *fattr = desc->fattr;
241
Benny Halevy99fadcd2008-01-23 08:59:08 +0200242 set_nfs_fileid(inode, fattr->fileid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 nfs_copy_fh(NFS_FH(inode), desc->fh);
244 return 0;
245}
246
247/* Don't use READDIRPLUS on directories that we believe are too large */
248#define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
249
250/*
251 * This is our front-end to iget that looks up inodes by file handle
252 * instead of inode number.
253 */
254struct inode *
255nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
256{
257 struct nfs_find_desc desc = {
258 .fh = fh,
259 .fattr = fattr
260 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500261 struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long hash;
263
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400264 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto out_no_inode;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400266 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto out_no_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 hash = nfs_fattr_to_ino_t(fattr);
270
Trond Myklebust03f28e32006-03-20 13:44:48 -0500271 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
272 if (inode == NULL) {
273 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if (inode->i_state & I_NEW) {
278 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800279 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* We set i_ino for the few things that still rely on it,
282 * such as stat(2) */
283 inode->i_ino = hash;
284
285 /* We can't support update_atime(), since the server will reset it */
286 inode->i_flags |= S_NOATIME|S_NOCMTIME;
287 inode->i_mode = fattr->mode;
288 /* Why so? Because we want revalidate for devices/FIFOs, and
289 * that's precisely what we have in nfs_file_inode_operations.
290 */
David Howells8fa5c002006-08-22 20:06:12 -0400291 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (S_ISREG(inode->i_mode)) {
293 inode->i_fop = &nfs_file_operations;
294 inode->i_data.a_ops = &nfs_file_aops;
295 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
296 } else if (S_ISDIR(inode->i_mode)) {
David Howells8fa5c002006-08-22 20:06:12 -0400297 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 inode->i_fop = &nfs_dir_operations;
299 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
300 && fattr->size <= NFS_LIMIT_READDIRPLUS)
Benny Halevy3a10c302008-01-23 08:58:59 +0200301 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
Trond Myklebust55a97592006-06-09 09:34:19 -0400302 /* Deal with crossing mountpoints */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400303 if ((fattr->valid & NFS_ATTR_FATTR_FSID)
304 && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400305 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
306 inode->i_op = &nfs_referral_inode_operations;
307 else
308 inode->i_op = &nfs_mountpoint_inode_operations;
Trond Myklebust55a97592006-06-09 09:34:19 -0400309 inode->i_fop = NULL;
Trond Myklebustc37dcd332008-03-06 12:34:50 -0500310 set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
Trond Myklebust55a97592006-06-09 09:34:19 -0400311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else if (S_ISLNK(inode->i_mode))
313 inode->i_op = &nfs_symlink_inode_operations;
314 else
315 init_special_inode(inode, inode->i_mode, fattr->rdev);
316
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400317 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
318 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
319 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
320 nfsi->change_attr = 0;
321 inode->i_size = 0;
322 inode->i_nlink = 0;
323 inode->i_uid = -2;
324 inode->i_gid = -2;
325 inode->i_blocks = 0;
326 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
327
Trond Myklebust33801142005-10-27 22:12:39 -0400328 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400329 nfsi->attr_gencount = fattr->gencount;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400330 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
331 inode->i_atime = fattr->atime;
332 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
333 inode->i_mtime = fattr->mtime;
334 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
335 inode->i_ctime = fattr->ctime;
336 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 nfsi->change_attr = fattr->change_attr;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400338 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
339 inode->i_size = nfs_size_to_loff_t(fattr->size);
340 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
341 inode->i_nlink = fattr->nlink;
342 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
343 inode->i_uid = fattr->uid;
344 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
345 inode->i_gid = fattr->gid;
346 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
347 inode->i_blocks = fattr->du.nfs2.blocks;
348 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * report the blocks in 512byte units
351 */
352 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebustb0c4fdd2007-02-05 14:44:22 -0800355 nfsi->attrtimeo_timestamp = now;
Trond Myklebust1c3c07e2006-07-25 11:28:18 -0400356 nfsi->access_cache = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 unlock_new_inode(inode);
359 } else
360 nfs_refresh_inode(inode, fattr);
361 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
362 inode->i_sb->s_id,
363 (long long)NFS_FILEID(inode),
364 atomic_read(&inode->i_count));
365
366out:
367 return inode;
368
369out_no_inode:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500370 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto out;
372}
373
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400374#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 -0700375
376int
377nfs_setattr(struct dentry *dentry, struct iattr *attr)
378{
379 struct inode *inode = dentry->d_inode;
380 struct nfs_fattr fattr;
381 int error;
382
Chuck Lever91d5b472006-03-20 13:44:14 -0500383 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
384
Jeff Layton188b95d2007-10-18 03:05:21 -0700385 /* skip mode change if it's just for clearing setuid/setgid */
386 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
387 attr->ia_valid &= ~ATTR_MODE;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (attr->ia_valid & ATTR_SIZE) {
390 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
391 attr->ia_valid &= ~ATTR_SIZE;
392 }
393
394 /* Optimization: if the end result is no change, don't RPC */
395 attr->ia_valid &= NFS_VALID_ATTRS;
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400396 if ((attr->ia_valid & ~ATTR_FILE) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398
Trond Myklebust755c1e22006-03-20 13:44:06 -0500399 /* Write all dirty data */
Trond Myklebuste1552e12007-04-14 19:07:28 -0400400 if (S_ISREG(inode->i_mode)) {
401 filemap_write_and_wait(inode->i_mapping);
402 nfs_wb_all(inode);
403 }
Trond Myklebust642ac542005-10-18 14:20:19 -0700404 /*
405 * Return any delegations if we're going to change ACLs
406 */
407 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
408 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400410 if (error == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 nfs_refresh_inode(inode, &fattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400412 return error;
413}
414
415/**
Trond Myklebusta3d01452008-06-11 12:21:19 -0400416 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
417 * @inode: inode of the file used
418 * @offset: file offset to start truncating
419 *
420 * This is a copy of the common vmtruncate, but with the locking
421 * corrected to take into account the fact that NFS requires
422 * inode->i_size to be updated under the inode->i_lock.
423 */
424static int nfs_vmtruncate(struct inode * inode, loff_t offset)
425{
426 if (i_size_read(inode) < offset) {
427 unsigned long limit;
428
429 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
430 if (limit != RLIM_INFINITY && offset > limit)
431 goto out_sig;
432 if (offset > inode->i_sb->s_maxbytes)
433 goto out_big;
434 spin_lock(&inode->i_lock);
435 i_size_write(inode, offset);
436 spin_unlock(&inode->i_lock);
437 } else {
438 struct address_space *mapping = inode->i_mapping;
439
440 /*
441 * truncation of in-use swapfiles is disallowed - it would
442 * cause subsequent swapout to scribble on the now-freed
443 * blocks.
444 */
445 if (IS_SWAPFILE(inode))
446 return -ETXTBSY;
447 spin_lock(&inode->i_lock);
448 i_size_write(inode, offset);
449 spin_unlock(&inode->i_lock);
450
451 /*
452 * unmap_mapping_range is called twice, first simply for
453 * efficiency so that truncate_inode_pages does fewer
454 * single-page unmaps. However after this first call, and
455 * before truncate_inode_pages finishes, it is possible for
456 * private pages to be COWed, which remain after
457 * truncate_inode_pages finishes, hence the second
458 * unmap_mapping_range call must be made for correctness.
459 */
460 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
461 truncate_inode_pages(mapping, offset);
462 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
463 }
464 return 0;
465out_sig:
466 send_sig(SIGXFSZ, current, 0);
467out_big:
468 return -EFBIG;
469}
470
471/**
Trond Myklebust65e43082005-08-16 11:49:44 -0400472 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
473 * @inode: pointer to struct inode
474 * @attr: pointer to struct iattr
475 *
476 * Note: we do this in the *proc.c in order to ensure that
477 * it works for things like exclusive creates too.
478 */
479void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
480{
481 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
Trond Myklebust2f28ea62008-10-05 14:26:11 -0400482 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400484 int mode = attr->ia_mode & S_IALLUGO;
485 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 inode->i_mode = mode;
487 }
488 if ((attr->ia_valid & ATTR_UID) != 0)
489 inode->i_uid = attr->ia_uid;
490 if ((attr->ia_valid & ATTR_GID) != 0)
491 inode->i_gid = attr->ia_gid;
Chuck Lever55296802005-08-18 11:24:09 -0700492 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700493 spin_unlock(&inode->i_lock);
Trond Myklebust65e43082005-08-16 11:49:44 -0400494 }
495 if ((attr->ia_valid & ATTR_SIZE) != 0) {
Chuck Lever91d5b472006-03-20 13:44:14 -0500496 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
Trond Myklebusta3d01452008-06-11 12:21:19 -0400497 nfs_vmtruncate(inode, attr->ia_size);
Trond Myklebust65e43082005-08-16 11:49:44 -0400498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
502{
503 struct inode *inode = dentry->d_inode;
Chuck Lever55296802005-08-18 11:24:09 -0700504 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int err;
506
Chuck Lever28c494c2007-10-26 13:32:13 -0400507 /*
508 * Flush out writes to the server in order to update c/mtime.
509 *
510 * Hold the i_mutex to suspend application writes temporarily;
511 * this prevents long-running writing applications from blocking
512 * nfs_wb_nocommit.
513 */
514 if (S_ISREG(inode->i_mode)) {
515 mutex_lock(&inode->i_mutex);
Trond Myklebusted90ef52007-07-20 13:13:28 -0400516 nfs_wb_nocommit(inode);
Chuck Lever28c494c2007-10-26 13:32:13 -0400517 mutex_unlock(&inode->i_mutex);
518 }
Christoph Hellwigfc33a7bb2006-01-09 20:52:17 -0800519
520 /*
521 * We may force a getattr if the user cares about atime.
522 *
523 * Note that we only have to check the vfsmount flags here:
524 * - NFS always sets S_NOATIME by so checking it would give a
525 * bogus result
526 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
527 * no point in checking those.
528 */
529 if ((mnt->mnt_flags & MNT_NOATIME) ||
530 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 need_atime = 0;
Christoph Hellwigfc33a7bb2006-01-09 20:52:17 -0800532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (need_atime)
534 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
535 else
536 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
Peter Staubach4e769b92007-08-03 15:07:10 -0400537 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 generic_fillattr(inode, stat);
Trond Myklebustf43bf0b2007-10-09 12:01:04 -0400539 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
Peter Staubach4e769b92007-08-03 15:07:10 -0400540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return err;
542}
543
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400544/**
545 * nfs_close_context - Common close_context() routine NFSv2/v3
546 * @ctx: pointer to context
547 * @is_sync: is this a synchronous close
548 *
549 * always ensure that the attributes are up to date if we're mounted
550 * with close-to-open semantics
551 */
552void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
553{
554 struct inode *inode;
555 struct nfs_server *server;
556
557 if (!(ctx->mode & FMODE_WRITE))
558 return;
559 if (!is_sync)
560 return;
561 inode = ctx->path.dentry->d_inode;
562 if (!list_empty(&NFS_I(inode)->open_files))
563 return;
564 server = NFS_SERVER(inode);
565 if (server->flags & NFS_MOUNT_NOCTO)
566 return;
567 nfs_revalidate_inode(server, inode);
568}
569
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500570static 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 -0700571{
572 struct nfs_open_context *ctx;
573
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700574 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (ctx != NULL) {
Trond Myklebust88be9f92007-06-05 10:42:27 -0400576 ctx->path.dentry = dget(dentry);
577 ctx->path.mnt = mntget(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ctx->cred = get_rpccred(cred);
579 ctx->state = NULL;
580 ctx->lockowner = current->files;
Jeff Layton66d3aac2008-03-31 15:01:58 -0400581 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ctx->error = 0;
Olivier Galibert00a92642005-06-22 17:16:29 +0000583 ctx->dir_cookie = 0;
Trond Myklebust5e119342007-07-26 12:06:17 -0400584 atomic_set(&ctx->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 return ctx;
587}
588
589struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
590{
591 if (ctx != NULL)
Trond Myklebust5e119342007-07-26 12:06:17 -0400592 atomic_inc(&ctx->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return ctx;
594}
595
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400596static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400597{
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400598 struct inode *inode = ctx->path.dentry->d_inode;
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400599
Trond Myklebust5e119342007-07-26 12:06:17 -0400600 if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
601 return;
602 list_del(&ctx->list);
603 spin_unlock(&inode->i_lock);
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400604 NFS_PROTO(inode)->close_context(ctx, is_sync);
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400605 if (ctx->cred != NULL)
606 put_rpccred(ctx->cred);
Jan Blunck31f31db12008-05-02 13:42:45 -0700607 path_put(&ctx->path);
Trond Myklebust3bec63d2007-06-17 16:02:44 -0400608 kfree(ctx);
609}
610
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400611void put_nfs_open_context(struct nfs_open_context *ctx)
612{
613 __put_nfs_open_context(ctx, 0);
614}
615
616static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
617{
618 __put_nfs_open_context(ctx, 1);
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * Ensure that mmap has a recent RPC credential for use when writing out
623 * shared pages
624 */
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500625static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800627 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct nfs_inode *nfsi = NFS_I(inode);
629
630 filp->private_data = get_nfs_open_context(ctx);
631 spin_lock(&inode->i_lock);
632 list_add(&ctx->list, &nfsi->open_files);
633 spin_unlock(&inode->i_lock);
634}
635
Trond Myklebustd5308382005-11-04 15:33:38 -0500636/*
637 * Given an inode, search for an open context with the desired characteristics
638 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500639struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct nfs_inode *nfsi = NFS_I(inode);
642 struct nfs_open_context *pos, *ctx = NULL;
643
644 spin_lock(&inode->i_lock);
645 list_for_each_entry(pos, &nfsi->open_files, list) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500646 if (cred != NULL && pos->cred != cred)
647 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if ((pos->mode & mode) == mode) {
649 ctx = get_nfs_open_context(pos);
650 break;
651 }
652 }
653 spin_unlock(&inode->i_lock);
654 return ctx;
655}
656
Trond Myklebustb92dccf2006-03-20 13:44:03 -0500657static void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800659 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400660 struct nfs_open_context *ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (ctx) {
663 filp->private_data = NULL;
664 spin_lock(&inode->i_lock);
665 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
666 spin_unlock(&inode->i_lock);
Trond Myklebusta49c3c72007-10-18 18:03:27 -0400667 put_nfs_open_context_sync(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669}
670
671/*
672 * These allocate and release file read/write context information.
673 */
674int nfs_open(struct inode *inode, struct file *filp)
675{
676 struct nfs_open_context *ctx;
677 struct rpc_cred *cred;
678
Trond Myklebust98a8e322008-03-12 12:25:28 -0400679 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (IS_ERR(cred))
681 return PTR_ERR(cred);
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800682 ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 put_rpccred(cred);
684 if (ctx == NULL)
685 return -ENOMEM;
686 ctx->mode = filp->f_mode;
687 nfs_file_set_open_context(filp, ctx);
688 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
692int nfs_release(struct inode *inode, struct file *filp)
693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 nfs_file_clear_open_context(filp);
695 return 0;
696}
697
698/*
699 * This function is called whenever some part of NFS notices that
700 * the cached attributes have to be refreshed.
701 */
702int
703__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
704{
705 int status = -ESTALE;
706 struct nfs_fattr fattr;
707 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
710 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
711
Chuck Lever85233a72006-10-19 23:28:42 -0700712 if (is_bad_inode(inode))
Chuck Lever412d5822005-08-18 11:24:11 -0700713 goto out;
Trond Myklebust7fdc49c2007-09-28 19:11:33 -0400714 if (NFS_STALE(inode))
715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Trond Myklebust691beb12008-10-05 14:48:22 -0400717 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
719 if (status != 0) {
720 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
721 inode->i_sb->s_id,
722 (long long)NFS_FILEID(inode), status);
723 if (status == -ESTALE) {
724 nfs_zap_caches(inode);
725 if (!S_ISDIR(inode->i_mode))
Benny Halevy3a10c302008-01-23 08:58:59 +0200726 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 goto out;
729 }
730
Trond Myklebust4dc05ef2008-09-23 17:28:42 -0400731 status = nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (status) {
733 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
734 inode->i_sb->s_id,
735 (long long)NFS_FILEID(inode), status);
736 goto out;
737 }
Chuck Lever55296802005-08-18 11:24:09 -0700738
Trond Myklebust24aa1fe2005-12-03 15:20:07 -0500739 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +0000740 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
743 inode->i_sb->s_id,
744 (long long)NFS_FILEID(inode));
745
Chuck Lever412d5822005-08-18 11:24:11 -0700746 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return status;
748}
749
750int nfs_attribute_timeout(struct inode *inode)
751{
752 struct nfs_inode *nfsi = NFS_I(inode);
753
754 if (nfs_have_delegation(inode, FMODE_READ))
755 return 0;
Peter Staubach64672d52008-12-23 15:21:56 -0500756 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/**
760 * nfs_revalidate_inode - Revalidate the inode attributes
761 * @server - pointer to nfs_server struct
762 * @inode - pointer to inode struct
763 *
764 * Updates inode attribute information by retrieving the data from the server.
765 */
766int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
767{
Trond Myklebust44b11872006-05-25 01:40:59 -0400768 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 && !nfs_attribute_timeout(inode))
770 return NFS_STALE(inode) ? -ESTALE : 0;
771 return __nfs_revalidate_inode(server, inode);
772}
773
Trond Myklebust717d44e2007-01-24 11:54:55 -0800774static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
775{
776 struct nfs_inode *nfsi = NFS_I(inode);
777
778 if (mapping->nrpages != 0) {
779 int ret = invalidate_inode_pages2(mapping);
780 if (ret < 0)
781 return ret;
782 }
783 spin_lock(&inode->i_lock);
784 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
Trond Myklebust2f78e432007-09-30 15:31:19 -0400785 if (S_ISDIR(inode->i_mode))
Trond Myklebust717d44e2007-01-24 11:54:55 -0800786 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
Trond Myklebust717d44e2007-01-24 11:54:55 -0800787 spin_unlock(&inode->i_lock);
788 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
789 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
790 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
791 return 0;
792}
793
794static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
795{
796 int ret = 0;
797
798 mutex_lock(&inode->i_mutex);
799 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
800 ret = nfs_sync_mapping(mapping);
801 if (ret == 0)
802 ret = nfs_invalidate_mapping_nolock(inode, mapping);
803 }
804 mutex_unlock(&inode->i_mutex);
805 return ret;
806}
807
808/**
809 * nfs_revalidate_mapping_nolock - Revalidate the pagecache
810 * @inode - pointer to host inode
811 * @mapping - pointer to mapping
812 */
813int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
814{
815 struct nfs_inode *nfsi = NFS_I(inode);
816 int ret = 0;
817
818 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
819 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
820 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
821 if (ret < 0)
822 goto out;
823 }
824 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
825 ret = nfs_invalidate_mapping_nolock(inode, mapping);
826out:
827 return ret;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/**
Trond Myklebust7d52e862005-06-22 17:16:30 +0000831 * nfs_revalidate_mapping - Revalidate the pagecache
832 * @inode - pointer to host inode
833 * @mapping - pointer to mapping
Trond Myklebust717d44e2007-01-24 11:54:55 -0800834 *
835 * This version of the function will take the inode->i_mutex and attempt to
836 * flush out all dirty data if it needs to invalidate the page cache.
Trond Myklebust7d52e862005-06-22 17:16:30 +0000837 */
Trond Myklebust44b11872006-05-25 01:40:59 -0400838int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +0000839{
840 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust44b11872006-05-25 01:40:59 -0400841 int ret = 0;
842
Trond Myklebust44b11872006-05-25 01:40:59 -0400843 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
Trond Myklebust717d44e2007-01-24 11:54:55 -0800844 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
Trond Myklebust44b11872006-05-25 01:40:59 -0400845 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust717d44e2007-01-24 11:54:55 -0800846 if (ret < 0)
847 goto out;
Trond Myklebust7d52e862005-06-22 17:16:30 +0000848 }
Trond Myklebust717d44e2007-01-24 11:54:55 -0800849 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
850 ret = nfs_invalidate_mapping(inode, mapping);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700851out:
Trond Myklebust44b11872006-05-25 01:40:59 -0400852 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +0000853}
854
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100855static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
856{
857 struct nfs_inode *nfsi = NFS_I(inode);
858
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400859 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
860 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
861 && nfsi->change_attr == fattr->pre_change_attr) {
Trond Myklebust70ca8852007-09-30 15:21:24 -0400862 nfsi->change_attr = fattr->change_attr;
863 if (S_ISDIR(inode->i_mode))
864 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
865 }
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100866 /* If we have atomic WCC data, we may update some attributes */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400867 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
868 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
869 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100870 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400871
872 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
873 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
874 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100875 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
Trond Myklebuste323ea42007-09-30 17:03:25 -0400876 if (S_ISDIR(inode->i_mode))
877 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100878 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400879 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
880 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
881 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
882 && nfsi->npages == 0)
883 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
Trond Myklebusta895b4a2006-01-03 09:55:40 +0100884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/**
Trond Myklebust33801142005-10-27 22:12:39 -0400887 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * @inode - pointer to inode
889 * @fattr - updated attributes
890 *
891 * Verifies the attribute cache. If we have just changed the attributes,
892 * so that fattr carries weak cache consistency data, then it may
893 * also update the ctime/mtime/change_attribute.
894 */
Trond Myklebust33801142005-10-27 22:12:39 -0400895static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct nfs_inode *nfsi = NFS_I(inode);
898 loff_t cur_size, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400899 unsigned long invalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Chuck Leverdc592502005-08-18 11:24:12 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* Has the inode gone and changed behind our back? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400903 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -EIO;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400905 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
906 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400908 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
Trond Myklebustf1bb0b92006-05-25 01:40:55 -0400909 nfsi->change_attr != fattr->change_attr)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400910 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /* Verify a few of the more important attributes */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400913 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400914 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustca62b9c2006-03-20 13:44:07 -0500915
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400916 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
917 cur_size = i_size_read(inode);
918 new_isize = nfs_size_to_loff_t(fattr->size);
919 if (cur_size != new_isize && nfsi->npages == 0)
920 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Have any file permissions changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400924 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
925 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
926 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
927 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
928 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400929 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Has the link count changed? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400932 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400933 invalid |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400935 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -0400936 invalid |= NFS_INO_INVALID_ATIME;
937
938 if (invalid != 0)
939 nfsi->cache_validity |= invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebust33801142005-10-27 22:12:39 -0400941 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}
944
Trond Myklebusta10ad172008-09-23 17:28:41 -0400945static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
946{
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400947 if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
948 return 0;
Trond Myklebusta10ad172008-09-23 17:28:41 -0400949 return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
950}
951
952static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
953{
Trond Myklebust9e6e70f2009-03-11 14:10:24 -0400954 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
955 return 0;
Trond Myklebusta10ad172008-09-23 17:28:41 -0400956 return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
957}
958
Trond Myklebustae05f262008-10-28 15:21:40 -0400959static atomic_long_t nfs_attr_generation_counter;
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400960
961static unsigned long nfs_read_attr_generation_counter(void)
962{
Trond Myklebustae05f262008-10-28 15:21:40 -0400963 return atomic_long_read(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400964}
965
966unsigned long nfs_inc_attr_generation_counter(void)
967{
Trond Myklebustae05f262008-10-28 15:21:40 -0400968 return atomic_long_inc_return(&nfs_attr_generation_counter);
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400969}
970
971void nfs_fattr_init(struct nfs_fattr *fattr)
972{
973 fattr->valid = 0;
974 fattr->time_start = jiffies;
975 fattr->gencount = nfs_inc_attr_generation_counter();
976}
977
Trond Myklebusta10ad172008-09-23 17:28:41 -0400978/**
979 * nfs_inode_attrs_need_update - check if the inode attributes need updating
980 * @inode - pointer to inode
981 * @fattr - attributes
982 *
983 * Attempt to divine whether or not an RPC call reply carrying stale
984 * attributes got scheduled after another call carrying updated ones.
985 *
986 * To do so, the function first assumes that a more recent ctime means
987 * that the attributes in fattr are newer, however it also attempt to
988 * catch the case where ctime either didn't change, or went backwards
989 * (if someone reset the clock on the server) by looking at whether
990 * or not this RPC call was started after the inode was last updated.
Trond Myklebust4704f0e2008-10-14 19:16:07 -0400991 * Note also the check for wraparound of 'attr_gencount'
Trond Myklebusta10ad172008-09-23 17:28:41 -0400992 *
993 * The function returns 'true' if it thinks the attributes in 'fattr' are
994 * more recent than the ones cached in the inode.
995 *
996 */
997static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
998{
999 const struct nfs_inode *nfsi = NFS_I(inode);
1000
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001001 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
Trond Myklebust03254e62008-10-09 13:27:55 -04001002 nfs_ctime_need_update(inode, fattr) ||
1003 nfs_size_need_update(inode, fattr) ||
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001004 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
Trond Myklebusta10ad172008-09-23 17:28:41 -04001005}
1006
Trond Myklebust870a5be2008-10-05 12:07:23 -04001007static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1008{
Trond Myklebusta10ad172008-09-23 17:28:41 -04001009 if (nfs_inode_attrs_need_update(inode, fattr))
Trond Myklebust870a5be2008-10-05 12:07:23 -04001010 return nfs_update_inode(inode, fattr);
1011 return nfs_check_inode_attributes(inode, fattr);
1012}
1013
Trond Myklebust33801142005-10-27 22:12:39 -04001014/**
1015 * nfs_refresh_inode - try to update the inode attribute cache
1016 * @inode - pointer to inode
1017 * @fattr - updated attributes
1018 *
1019 * Check that an RPC call that returned attributes has not overlapped with
1020 * other recent updates of the inode metadata, then decide whether it is
1021 * safe to do a full update of the inode attributes, or whether just to
1022 * call nfs_check_inode_attributes.
1023 */
1024int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1025{
Trond Myklebust33801142005-10-27 22:12:39 -04001026 int status;
1027
1028 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1029 return 0;
1030 spin_lock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001031 status = nfs_refresh_inode_locked(inode, fattr);
Trond Myklebust33801142005-10-27 22:12:39 -04001032 spin_unlock(&inode->i_lock);
1033 return status;
1034}
1035
Trond Myklebustd65f5572008-10-05 12:27:55 -04001036static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1037{
1038 struct nfs_inode *nfsi = NFS_I(inode);
1039
1040 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1041 if (S_ISDIR(inode->i_mode))
1042 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1043 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1044 return 0;
1045 return nfs_refresh_inode_locked(inode, fattr);
1046}
1047
Trond Myklebustdecf4912005-10-27 22:12:39 -04001048/**
1049 * nfs_post_op_update_inode - try to update the inode attribute cache
1050 * @inode - pointer to inode
1051 * @fattr - updated attributes
1052 *
1053 * After an operation that has changed the inode metadata, mark the
1054 * attribute cache as being invalid, then try to update it.
Chuck Leverf551e442006-09-20 14:33:04 -04001055 *
1056 * NB: if the server didn't return any post op attributes, this
1057 * function will force the retrieval of attributes before the next
1058 * NFS request. Thus it should be used only for operations that
1059 * are expected to change one or more attributes, to avoid
1060 * unnecessary NFS requests and trips through nfs_update_inode().
Trond Myklebustdecf4912005-10-27 22:12:39 -04001061 */
1062int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1063{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001064 int status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001065
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001066 spin_lock(&inode->i_lock);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001067 status = nfs_post_op_update_inode_locked(inode, fattr);
Trond Myklebust7668fdb2007-10-01 09:59:15 -04001068 spin_unlock(&inode->i_lock);
Trond Myklebust870a5be2008-10-05 12:07:23 -04001069 return status;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001070}
1071
Trond Myklebust70ca8852007-09-30 15:21:24 -04001072/**
1073 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1074 * @inode - pointer to inode
1075 * @fattr - updated attributes
1076 *
1077 * After an operation that has changed the inode metadata, mark the
1078 * attribute cache as being invalid, then try to update it. Fake up
1079 * weak cache consistency data, if none exist.
1080 *
1081 * This function is mainly designed to be used by the ->write_done() functions.
1082 */
1083int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1084{
Trond Myklebustd65f5572008-10-05 12:27:55 -04001085 int status;
1086
1087 spin_lock(&inode->i_lock);
1088 /* Don't do a WCC update if these attributes are already stale */
1089 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1090 !nfs_inode_attrs_need_update(inode, fattr)) {
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001091 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1092 | NFS_ATTR_FATTR_PRESIZE
1093 | NFS_ATTR_FATTR_PREMTIME
1094 | NFS_ATTR_FATTR_PRECTIME);
Trond Myklebustd65f5572008-10-05 12:27:55 -04001095 goto out_noforce;
1096 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001097 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1098 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001099 fattr->pre_change_attr = NFS_I(inode)->change_attr;
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001100 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001101 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001102 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1103 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001104 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001105 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1106 }
1107 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1108 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
Trond Myklebust70ca8852007-09-30 15:21:24 -04001109 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001110 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1111 }
1112 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1113 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
Trond Myklebusta3d01452008-06-11 12:21:19 -04001114 fattr->pre_size = i_size_read(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001115 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001116 }
Trond Myklebustd65f5572008-10-05 12:27:55 -04001117out_noforce:
1118 status = nfs_post_op_update_inode_locked(inode, fattr);
1119 spin_unlock(&inode->i_lock);
1120 return status;
Trond Myklebust70ca8852007-09-30 15:21:24 -04001121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
1124 * Many nfs protocol calls return the new file attributes after
1125 * an operation. Here we update the inode to reflect the state
1126 * of the server's inode.
1127 *
1128 * This is a bit tricky because we have to make sure all dirty pages
1129 * have been sent off to the server before calling invalidate_inode_pages.
1130 * To make sure no other process adds more write requests while we try
1131 * our best to flush them, we make them sleep during the attribute refresh.
1132 *
1133 * A very similar scenario holds for the dir cache.
1134 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001135static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001137 struct nfs_server *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001139 loff_t cur_isize, new_isize;
Trond Myklebust2a3f5fd2007-10-08 14:26:13 -04001140 unsigned long invalid = 0;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001141 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001144 __func__, inode->i_sb->s_id, inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 atomic_read(&inode->i_count), fattr->valid);
1146
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001147 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
Chuck Lever325cfed2005-11-30 18:08:55 -05001148 goto out_fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /*
1151 * Make sure the inode's type hasn't changed.
1152 */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001153 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto out_changed;
1155
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001156 server = NFS_SERVER(inode);
Trond Myklebusta0356862007-06-05 13:26:15 -04001157 /* Update the fsid? */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001158 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Trond Myklebustc37dcd332008-03-06 12:34:50 -05001159 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1160 !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001161 server->fsid = fattr->fsid;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * Update the read time so we don't revalidate too often.
1165 */
Trond Myklebust33801142005-10-27 22:12:39 -04001166 nfsi->read_cache_jiffies = fattr->time_start;
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001167
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001168 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) || (fattr->valid & (NFS_ATTR_FATTR_MTIME|NFS_ATTR_FATTR_CTIME)))
1169 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1170 | NFS_INO_INVALID_ATIME
1171 | NFS_INO_REVAL_PAGECACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001173 /* Do atomic weak cache consistency updates */
1174 nfs_wcc_update_inode(inode, fattr);
1175
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001176 /* More cache consistency checks */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001177 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1178 if (nfsi->change_attr != fattr->change_attr) {
1179 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1180 inode->i_sb->s_id, inode->i_ino);
1181 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1182 if (S_ISDIR(inode->i_mode))
1183 nfs_force_lookup_revalidate(inode);
1184 nfsi->change_attr = fattr->change_attr;
1185 }
1186 }
1187
1188 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001189 /* NFSv2/v3: Check if the mtime agrees */
1190 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1191 dprintk("NFS: mtime change on server for file %s/%ld\n",
1192 inode->i_sb->s_id, inode->i_ino);
1193 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebustbfc69a42007-10-15 18:18:29 -04001194 if (S_ISDIR(inode->i_mode))
1195 nfs_force_lookup_revalidate(inode);
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001196 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001197 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001198 }
1199 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001200 /* If ctime has changed we should definitely clear access+acl caches */
NeilBrown37d9d762009-03-11 14:10:23 -04001201 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001202 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
NeilBrown37d9d762009-03-11 14:10:23 -04001203 /* and probably clear data for a directory too as utimes can cause
1204 * havoc with our cache.
1205 */
1206 if (S_ISDIR(inode->i_mode)) {
1207 invalid |= NFS_INO_INVALID_DATA;
1208 nfs_force_lookup_revalidate(inode);
1209 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001210 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
NeilBrown37d9d762009-03-11 14:10:23 -04001211 }
Trond Myklebust47aabaa2007-09-27 15:57:24 -04001212 }
1213
Trond Myklebust951a1432005-06-22 17:16:30 +00001214 /* Check if our cached file size is stale */
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001215 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1216 new_isize = nfs_size_to_loff_t(fattr->size);
1217 cur_isize = i_size_read(inode);
1218 if (new_isize != cur_isize) {
1219 /* Do we perhaps have any outstanding writes, or has
1220 * the file grown beyond our last write? */
1221 if (nfsi->npages == 0 || new_isize > cur_isize) {
1222 i_size_write(inode, new_isize);
1223 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1224 }
1225 dprintk("NFS: isize change on server for file %s/%ld\n",
1226 inode->i_sb->s_id, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001231 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1232 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001234 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1235 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1236 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1237 inode->i_mode = fattr->mode;
1238 }
1239 }
1240 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
1241 if (inode->i_uid != fattr->uid) {
1242 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1243 inode->i_uid = fattr->uid;
1244 }
1245 }
1246 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
1247 if (inode->i_gid != fattr->gid) {
1248 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1249 inode->i_gid = fattr->gid;
1250 }
NeilBrown37d9d762009-03-11 14:10:23 -04001251 }
Trond Myklebust921615f2008-10-14 19:23:07 -04001252
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001253 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1254 if (inode->i_nlink != fattr->nlink) {
1255 invalid |= NFS_INO_INVALID_ATTR;
1256 if (S_ISDIR(inode->i_mode))
1257 invalid |= NFS_INO_INVALID_DATA;
1258 inode->i_nlink = fattr->nlink;
1259 }
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001262 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * report the blocks in 512byte units
1265 */
1266 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Trond Myklebust9e6e70f2009-03-11 14:10:24 -04001268 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1269 inode->i_blocks = fattr->du.nfs2.blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Update attrtimeo value if we're out of the unstable period */
1272 if (invalid & NFS_INO_INVALID_ATTR) {
Chuck Lever91d5b472006-03-20 13:44:14 -05001273 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
Trond Myklebust3e7d9502007-02-05 14:26:28 -08001275 nfsi->attrtimeo_timestamp = now;
Trond Myklebust4704f0e2008-10-14 19:16:07 -04001276 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebust6d2b2962007-10-01 18:57:50 -04001277 } else {
Peter Staubach64672d52008-12-23 15:21:56 -05001278 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
Trond Myklebust6d2b2962007-10-01 18:57:50 -04001279 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1280 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1281 nfsi->attrtimeo_timestamp = now;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Trond Myklebustf2115dc2007-08-15 12:49:17 -04001284 invalid &= ~NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* Don't invalidate the data if we were to blame */
1286 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1287 || S_ISLNK(inode->i_mode)))
1288 invalid &= ~NFS_INO_INVALID_DATA;
Trond Myklebust412c77c2007-07-03 16:10:55 -04001289 if (!nfs_have_delegation(inode, FMODE_READ) ||
1290 (nfsi->cache_validity & NFS_INO_REVAL_FORCED))
Chuck Lever55296802005-08-18 11:24:09 -07001291 nfsi->cache_validity |= invalid;
Trond Myklebust412c77c2007-07-03 16:10:55 -04001292 nfsi->cache_validity &= ~NFS_INO_REVAL_FORCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 return 0;
1295 out_changed:
1296 /*
1297 * Big trouble! The inode has become a different object.
1298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001300 __func__, inode->i_ino, inode->i_mode, fattr->mode);
Trond Myklebustb37b03b2005-11-25 17:10:06 -05001301 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * No need to worry about unhashing the dentry, as the
1304 * lookup validation will know that the inode is bad.
1305 * (But we fall through to invalidate the caches.)
1306 */
1307 nfs_invalidate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -ESTALE;
Chuck Lever325cfed2005-11-30 18:08:55 -05001309
1310 out_fileid:
1311 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1312 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
David Howells54ceac42006-08-22 20:06:13 -04001313 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
Chuck Lever325cfed2005-11-30 18:08:55 -05001314 (long long)nfsi->fileid, (long long)fattr->fileid);
1315 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust55a97592006-06-09 09:34:19 -04001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#ifdef CONFIG_NFS_V4
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/*
1322 * Clean out any remaining NFSv4 state that might be left over due
1323 * to open() calls that passed nfs_atomic_lookup, but failed to call
1324 * nfs_open().
1325 */
David Howellsf7b422b2006-06-09 09:34:33 -04001326void nfs4_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* If we are holding a delegation, return it! */
Trond Myklebuste6f81072008-01-24 18:14:34 -05001329 nfs_inode_return_delegation_noreclaim(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* First call standard NFS clear_inode() code */
1331 nfs_clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333#endif
1334
David Howellsf7b422b2006-06-09 09:34:33 -04001335struct inode *nfs_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct nfs_inode *nfsi;
Christoph Lametere94b1762006-12-06 20:33:17 -08001338 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (!nfsi)
1340 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07001341 nfsi->flags = 0UL;
1342 nfsi->cache_validity = 0UL;
Trond Myklebust458818e2005-06-22 17:16:27 +00001343#ifdef CONFIG_NFS_V3_ACL
1344 nfsi->acl_access = ERR_PTR(-EAGAIN);
1345 nfsi->acl_default = ERR_PTR(-EAGAIN);
1346#endif
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00001347#ifdef CONFIG_NFS_V4
1348 nfsi->nfs4_acl = NULL;
1349#endif /* CONFIG_NFS_V4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return &nfsi->vfs_inode;
1351}
1352
David Howellsf7b422b2006-06-09 09:34:33 -04001353void nfs_destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1356}
1357
Andrew Mortond75d5412006-06-25 02:41:26 -07001358static inline void nfs4_init_once(struct nfs_inode *nfsi)
1359{
1360#ifdef CONFIG_NFS_V4
1361 INIT_LIST_HEAD(&nfsi->open_states);
1362 nfsi->delegation = NULL;
1363 nfsi->delegation_state = 0;
1364 init_rwsem(&nfsi->rwsem);
1365#endif
1366}
David Howellsf7b422b2006-06-09 09:34:33 -04001367
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001368static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1371
Christoph Lametera35afb82007-05-16 22:10:57 -07001372 inode_init_once(&nfsi->vfs_inode);
Christoph Lametera35afb82007-05-16 22:10:57 -07001373 INIT_LIST_HEAD(&nfsi->open_files);
1374 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1375 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
1376 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
Christoph Lametera35afb82007-05-16 22:10:57 -07001377 nfsi->npages = 0;
Trond Myklebust565277f2007-10-15 18:17:53 -04001378 atomic_set(&nfsi->silly_count, 1);
1379 INIT_HLIST_HEAD(&nfsi->silly_list);
1380 init_waitqueue_head(&nfsi->waitqueue);
Christoph Lametera35afb82007-05-16 22:10:57 -07001381 nfs4_init_once(nfsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Paul Mundt20c2df82007-07-20 10:11:58 +09001383
David Howellsf7b422b2006-06-09 09:34:33 -04001384static int __init nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1387 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001388 0, (SLAB_RECLAIM_ACCOUNT|
1389 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001390 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (nfs_inode_cachep == NULL)
1392 return -ENOMEM;
1393
1394 return 0;
1395}
1396
David Brownell266bee82006-06-27 12:59:15 -07001397static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001399 kmem_cache_destroy(nfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Trond Myklebust57460062008-02-19 20:04:22 -05001402struct workqueue_struct *nfsiod_workqueue;
1403
1404/*
1405 * start up the nfsiod workqueue
1406 */
1407static int nfsiod_start(void)
1408{
1409 struct workqueue_struct *wq;
1410 dprintk("RPC: creating workqueue nfsiod\n");
1411 wq = create_singlethread_workqueue("nfsiod");
1412 if (wq == NULL)
1413 return -ENOMEM;
1414 nfsiod_workqueue = wq;
1415 return 0;
1416}
1417
1418/*
1419 * Destroy the nfsiod workqueue
1420 */
1421static void nfsiod_stop(void)
1422{
1423 struct workqueue_struct *wq;
1424
1425 wq = nfsiod_workqueue;
1426 if (wq == NULL)
1427 return;
1428 nfsiod_workqueue = NULL;
1429 destroy_workqueue(wq);
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/*
1433 * Initialize NFS
1434 */
1435static int __init init_nfs_fs(void)
1436{
1437 int err;
1438
Trond Myklebust57460062008-02-19 20:04:22 -05001439 err = nfsiod_start();
1440 if (err)
1441 goto out6;
1442
David Howells6aaca562006-08-22 20:06:13 -04001443 err = nfs_fs_proc_init();
1444 if (err)
1445 goto out5;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 err = nfs_init_nfspagecache();
1448 if (err)
1449 goto out4;
1450
1451 err = nfs_init_inodecache();
1452 if (err)
1453 goto out3;
1454
1455 err = nfs_init_readpagecache();
1456 if (err)
1457 goto out2;
1458
1459 err = nfs_init_writepagecache();
1460 if (err)
1461 goto out1;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 err = nfs_init_directcache();
1464 if (err)
1465 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467#ifdef CONFIG_PROC_FS
1468 rpc_proc_register(&nfs_rpcstat);
1469#endif
David Howellsf7b422b2006-06-09 09:34:33 -04001470 if ((err = register_nfs_fs()) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out;
1472 return 0;
1473out:
1474#ifdef CONFIG_PROC_FS
1475 rpc_proc_unregister("nfs");
1476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 nfs_destroy_directcache();
Chuck Lever6b59a752005-11-30 18:08:19 -05001478out0:
Chuck Lever6b59a752005-11-30 18:08:19 -05001479 nfs_destroy_writepagecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480out1:
1481 nfs_destroy_readpagecache();
1482out2:
1483 nfs_destroy_inodecache();
1484out3:
1485 nfs_destroy_nfspagecache();
1486out4:
David Howells6aaca562006-08-22 20:06:13 -04001487 nfs_fs_proc_exit();
1488out5:
Trond Myklebust57460062008-02-19 20:04:22 -05001489 nfsiod_stop();
1490out6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return err;
1492}
1493
1494static void __exit exit_nfs_fs(void)
1495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 nfs_destroy_directcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 nfs_destroy_writepagecache();
1498 nfs_destroy_readpagecache();
1499 nfs_destroy_inodecache();
1500 nfs_destroy_nfspagecache();
1501#ifdef CONFIG_PROC_FS
1502 rpc_proc_unregister("nfs");
1503#endif
David Howellsf7b422b2006-06-09 09:34:33 -04001504 unregister_nfs_fs();
David Howells6aaca562006-08-22 20:06:13 -04001505 nfs_fs_proc_exit();
Trond Myklebust57460062008-02-19 20:04:22 -05001506 nfsiod_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509/* Not quite true; I just maintain it */
1510MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1511MODULE_LICENSE("GPL");
Trond Myklebustf43bf0b2007-10-09 12:01:04 -04001512module_param(enable_ino64, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514module_init(init_nfs_fs)
1515module_exit(exit_nfs_fs)