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