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