4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/time.h>
20 #include <linux/kernel.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>
28 #include <linux/sunrpc/metrics.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/seq_file.h>
34 #include <linux/mount.h>
35 #include <linux/nfs_idmap.h>
36 #include <linux/vfs.h>
37 #include <linux/inet.h>
38 #include <linux/nfs_xdr.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
45 #include "delegation.h"
50 #define NFSDBG_FACILITY NFSDBG_VFS
52 #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
54 /* Default is to see 64-bit inode numbers */
55 static int enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
57 static void nfs_invalidate_inode(struct inode *);
58 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
60 static struct kmem_cache * nfs_inode_cachep;
62 static inline unsigned long
63 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
65 return nfs_fileid_to_ino_t(fattr->fileid);
69 * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
70 * @word: long word containing the bit lock
72 int nfs_wait_bit_killable(void *word)
74 if (fatal_signal_pending(current))
81 * nfs_compat_user_ino64 - returns the user-visible inode number
82 * @fileid: 64-bit fileid
84 * This function returns a 32-bit inode number if the boot parameter
85 * nfs.enable_ino64 is zero.
87 u64 nfs_compat_user_ino64(u64 fileid)
94 if (sizeof(ino) < sizeof(fileid))
95 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
99 int nfs_write_inode(struct inode *inode, int sync)
104 ret = filemap_fdatawait(inode->i_mapping);
106 ret = nfs_commit_inode(inode, FLUSH_SYNC);
108 ret = nfs_commit_inode(inode, 0);
111 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
115 void nfs_clear_inode(struct inode *inode)
118 * The following should never happen...
120 BUG_ON(nfs_have_writebacks(inode));
121 BUG_ON(!list_empty(&NFS_I(inode)->open_files));
122 nfs_zap_acl_cache(inode);
123 nfs_access_zap_cache(inode);
124 nfs_fscache_release_inode_cookie(inode);
128 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
130 int nfs_sync_mapping(struct address_space *mapping)
134 if (mapping->nrpages == 0)
136 unmap_mapping_range(mapping, 0, 0, 0);
137 ret = filemap_write_and_wait(mapping);
140 ret = nfs_wb_all(mapping->host);
146 * Invalidate the local caches
148 static void nfs_zap_caches_locked(struct inode *inode)
150 struct nfs_inode *nfsi = NFS_I(inode);
151 int mode = inode->i_mode;
153 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
155 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
156 nfsi->attrtimeo_timestamp = jiffies;
158 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
159 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
160 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
162 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
165 void nfs_zap_caches(struct inode *inode)
167 spin_lock(&inode->i_lock);
168 nfs_zap_caches_locked(inode);
169 spin_unlock(&inode->i_lock);
172 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
174 if (mapping->nrpages != 0) {
175 spin_lock(&inode->i_lock);
176 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
177 spin_unlock(&inode->i_lock);
181 void nfs_zap_acl_cache(struct inode *inode)
183 void (*clear_acl_cache)(struct inode *);
185 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
186 if (clear_acl_cache != NULL)
187 clear_acl_cache(inode);
188 spin_lock(&inode->i_lock);
189 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
190 spin_unlock(&inode->i_lock);
193 void nfs_invalidate_atime(struct inode *inode)
195 spin_lock(&inode->i_lock);
196 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
197 spin_unlock(&inode->i_lock);
201 * Invalidate, but do not unhash, the inode.
202 * NB: must be called with inode->i_lock held!
204 static void nfs_invalidate_inode(struct inode *inode)
206 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
207 nfs_zap_caches_locked(inode);
210 struct nfs_find_desc {
212 struct nfs_fattr *fattr;
216 * In NFSv3 we can have 64bit inode numbers. In order to support
217 * this, and re-exported directories (also seen in NFSv2)
218 * we are forced to allow 2 different inodes to have the same
222 nfs_find_actor(struct inode *inode, void *opaque)
224 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
225 struct nfs_fh *fh = desc->fh;
226 struct nfs_fattr *fattr = desc->fattr;
228 if (NFS_FILEID(inode) != fattr->fileid)
230 if (nfs_compare_fh(NFS_FH(inode), fh))
232 if (is_bad_inode(inode) || NFS_STALE(inode))
238 nfs_init_locked(struct inode *inode, void *opaque)
240 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
241 struct nfs_fattr *fattr = desc->fattr;
243 set_nfs_fileid(inode, fattr->fileid);
244 nfs_copy_fh(NFS_FH(inode), desc->fh);
248 /* Don't use READDIRPLUS on directories that we believe are too large */
249 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
252 * This is our front-end to iget that looks up inodes by file handle
253 * instead of inode number.
256 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
258 struct nfs_find_desc desc = {
262 struct inode *inode = ERR_PTR(-ENOENT);
265 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
267 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
270 hash = nfs_fattr_to_ino_t(fattr);
272 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
274 inode = ERR_PTR(-ENOMEM);
278 if (inode->i_state & I_NEW) {
279 struct nfs_inode *nfsi = NFS_I(inode);
280 unsigned long now = jiffies;
282 /* We set i_ino for the few things that still rely on it,
286 /* We can't support update_atime(), since the server will reset it */
287 inode->i_flags |= S_NOATIME|S_NOCMTIME;
288 inode->i_mode = fattr->mode;
289 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
290 && nfs_server_capable(inode, NFS_CAP_MODE))
291 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
292 | NFS_INO_INVALID_ACCESS
293 | NFS_INO_INVALID_ACL;
294 /* Why so? Because we want revalidate for devices/FIFOs, and
295 * that's precisely what we have in nfs_file_inode_operations.
297 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
298 if (S_ISREG(inode->i_mode)) {
299 inode->i_fop = &nfs_file_operations;
300 inode->i_data.a_ops = &nfs_file_aops;
301 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
302 } else if (S_ISDIR(inode->i_mode)) {
303 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
304 inode->i_fop = &nfs_dir_operations;
305 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
306 && fattr->size <= NFS_LIMIT_READDIRPLUS)
307 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
308 /* Deal with crossing mountpoints */
309 if ((fattr->valid & NFS_ATTR_FATTR_FSID)
310 && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
311 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
312 inode->i_op = &nfs_referral_inode_operations;
314 inode->i_op = &nfs_mountpoint_inode_operations;
316 set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
318 } else if (S_ISLNK(inode->i_mode))
319 inode->i_op = &nfs_symlink_inode_operations;
321 init_special_inode(inode, inode->i_mode, fattr->rdev);
323 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
324 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
325 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
326 nfsi->change_attr = 0;
332 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
334 nfsi->read_cache_jiffies = fattr->time_start;
335 nfsi->attr_gencount = fattr->gencount;
336 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
337 inode->i_atime = fattr->atime;
338 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
339 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
340 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
341 inode->i_mtime = fattr->mtime;
342 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
343 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
344 | NFS_INO_INVALID_DATA;
345 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
346 inode->i_ctime = fattr->ctime;
347 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
348 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
349 | NFS_INO_INVALID_ACCESS
350 | NFS_INO_INVALID_ACL;
351 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
352 nfsi->change_attr = fattr->change_attr;
353 else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
354 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
355 | NFS_INO_INVALID_DATA;
356 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
357 inode->i_size = nfs_size_to_loff_t(fattr->size);
359 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
360 | NFS_INO_INVALID_DATA
361 | NFS_INO_REVAL_PAGECACHE;
362 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
363 inode->i_nlink = fattr->nlink;
364 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
365 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
366 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
367 inode->i_uid = fattr->uid;
368 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
369 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
370 | NFS_INO_INVALID_ACCESS
371 | NFS_INO_INVALID_ACL;
372 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
373 inode->i_gid = fattr->gid;
374 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
375 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
376 | NFS_INO_INVALID_ACCESS
377 | NFS_INO_INVALID_ACL;
378 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
379 inode->i_blocks = fattr->du.nfs2.blocks;
380 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
382 * report the blocks in 512byte units
384 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
386 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
387 nfsi->attrtimeo_timestamp = now;
388 nfsi->access_cache = RB_ROOT;
390 nfs_fscache_init_inode_cookie(inode);
392 unlock_new_inode(inode);
394 nfs_refresh_inode(inode, fattr);
395 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
397 (long long)NFS_FILEID(inode),
398 atomic_read(&inode->i_count));
404 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
408 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
411 nfs_setattr(struct dentry *dentry, struct iattr *attr)
413 struct inode *inode = dentry->d_inode;
414 struct nfs_fattr fattr;
417 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
419 /* skip mode change if it's just for clearing setuid/setgid */
420 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
421 attr->ia_valid &= ~ATTR_MODE;
423 if (attr->ia_valid & ATTR_SIZE) {
424 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
425 attr->ia_valid &= ~ATTR_SIZE;
428 /* Optimization: if the end result is no change, don't RPC */
429 attr->ia_valid &= NFS_VALID_ATTRS;
430 if ((attr->ia_valid & ~ATTR_FILE) == 0)
433 /* Write all dirty data */
434 if (S_ISREG(inode->i_mode)) {
435 filemap_write_and_wait(inode->i_mapping);
439 * Return any delegations if we're going to change ACLs
441 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
442 nfs_inode_return_delegation(inode);
443 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
445 nfs_refresh_inode(inode, &fattr);
450 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
451 * @inode: inode of the file used
452 * @offset: file offset to start truncating
454 * This is a copy of the common vmtruncate, but with the locking
455 * corrected to take into account the fact that NFS requires
456 * inode->i_size to be updated under the inode->i_lock.
458 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
460 if (i_size_read(inode) < offset) {
463 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
464 if (limit != RLIM_INFINITY && offset > limit)
466 if (offset > inode->i_sb->s_maxbytes)
468 spin_lock(&inode->i_lock);
469 i_size_write(inode, offset);
470 spin_unlock(&inode->i_lock);
472 struct address_space *mapping = inode->i_mapping;
475 * truncation of in-use swapfiles is disallowed - it would
476 * cause subsequent swapout to scribble on the now-freed
479 if (IS_SWAPFILE(inode))
481 spin_lock(&inode->i_lock);
482 i_size_write(inode, offset);
483 spin_unlock(&inode->i_lock);
486 * unmap_mapping_range is called twice, first simply for
487 * efficiency so that truncate_inode_pages does fewer
488 * single-page unmaps. However after this first call, and
489 * before truncate_inode_pages finishes, it is possible for
490 * private pages to be COWed, which remain after
491 * truncate_inode_pages finishes, hence the second
492 * unmap_mapping_range call must be made for correctness.
494 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
495 truncate_inode_pages(mapping, offset);
496 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
500 send_sig(SIGXFSZ, current, 0);
506 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
507 * @inode: pointer to struct inode
508 * @attr: pointer to struct iattr
510 * Note: we do this in the *proc.c in order to ensure that
511 * it works for things like exclusive creates too.
513 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
515 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
516 spin_lock(&inode->i_lock);
517 if ((attr->ia_valid & ATTR_MODE) != 0) {
518 int mode = attr->ia_mode & S_IALLUGO;
519 mode |= inode->i_mode & ~S_IALLUGO;
520 inode->i_mode = mode;
522 if ((attr->ia_valid & ATTR_UID) != 0)
523 inode->i_uid = attr->ia_uid;
524 if ((attr->ia_valid & ATTR_GID) != 0)
525 inode->i_gid = attr->ia_gid;
526 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
527 spin_unlock(&inode->i_lock);
529 if ((attr->ia_valid & ATTR_SIZE) != 0) {
530 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
531 nfs_vmtruncate(inode, attr->ia_size);
535 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
537 struct inode *inode = dentry->d_inode;
538 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
542 * Flush out writes to the server in order to update c/mtime.
544 * Hold the i_mutex to suspend application writes temporarily;
545 * this prevents long-running writing applications from blocking
548 if (S_ISREG(inode->i_mode)) {
549 mutex_lock(&inode->i_mutex);
550 nfs_wb_nocommit(inode);
551 mutex_unlock(&inode->i_mutex);
555 * We may force a getattr if the user cares about atime.
557 * Note that we only have to check the vfsmount flags here:
558 * - NFS always sets S_NOATIME by so checking it would give a
560 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
561 * no point in checking those.
563 if ((mnt->mnt_flags & MNT_NOATIME) ||
564 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
568 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
570 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
572 generic_fillattr(inode, stat);
573 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
579 * nfs_close_context - Common close_context() routine NFSv2/v3
580 * @ctx: pointer to context
581 * @is_sync: is this a synchronous close
583 * always ensure that the attributes are up to date if we're mounted
584 * with close-to-open semantics
586 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
589 struct nfs_server *server;
591 if (!(ctx->mode & FMODE_WRITE))
595 inode = ctx->path.dentry->d_inode;
596 if (!list_empty(&NFS_I(inode)->open_files))
598 server = NFS_SERVER(inode);
599 if (server->flags & NFS_MOUNT_NOCTO)
601 nfs_revalidate_inode(server, inode);
604 static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
606 struct nfs_open_context *ctx;
608 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
610 ctx->path.dentry = dget(dentry);
611 ctx->path.mnt = mntget(mnt);
612 ctx->cred = get_rpccred(cred);
614 ctx->lockowner = current->files;
618 atomic_set(&ctx->count, 1);
623 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
626 atomic_inc(&ctx->count);
630 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
632 struct inode *inode = ctx->path.dentry->d_inode;
634 if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
636 list_del(&ctx->list);
637 spin_unlock(&inode->i_lock);
638 NFS_PROTO(inode)->close_context(ctx, is_sync);
639 if (ctx->cred != NULL)
640 put_rpccred(ctx->cred);
641 path_put(&ctx->path);
645 void put_nfs_open_context(struct nfs_open_context *ctx)
647 __put_nfs_open_context(ctx, 0);
650 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
652 __put_nfs_open_context(ctx, 1);
656 * Ensure that mmap has a recent RPC credential for use when writing out
659 static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
661 struct inode *inode = filp->f_path.dentry->d_inode;
662 struct nfs_inode *nfsi = NFS_I(inode);
664 filp->private_data = get_nfs_open_context(ctx);
665 spin_lock(&inode->i_lock);
666 list_add(&ctx->list, &nfsi->open_files);
667 spin_unlock(&inode->i_lock);
671 * Given an inode, search for an open context with the desired characteristics
673 struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
675 struct nfs_inode *nfsi = NFS_I(inode);
676 struct nfs_open_context *pos, *ctx = NULL;
678 spin_lock(&inode->i_lock);
679 list_for_each_entry(pos, &nfsi->open_files, list) {
680 if (cred != NULL && pos->cred != cred)
682 if ((pos->mode & mode) == mode) {
683 ctx = get_nfs_open_context(pos);
687 spin_unlock(&inode->i_lock);
691 static void nfs_file_clear_open_context(struct file *filp)
693 struct inode *inode = filp->f_path.dentry->d_inode;
694 struct nfs_open_context *ctx = nfs_file_open_context(filp);
697 filp->private_data = NULL;
698 spin_lock(&inode->i_lock);
699 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
700 spin_unlock(&inode->i_lock);
701 put_nfs_open_context_sync(ctx);
706 * These allocate and release file read/write context information.
708 int nfs_open(struct inode *inode, struct file *filp)
710 struct nfs_open_context *ctx;
711 struct rpc_cred *cred;
713 cred = rpc_lookup_cred();
715 return PTR_ERR(cred);
716 ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
720 ctx->mode = filp->f_mode;
721 nfs_file_set_open_context(filp, ctx);
722 put_nfs_open_context(ctx);
723 nfs_fscache_set_inode_cookie(inode, filp);
727 int nfs_release(struct inode *inode, struct file *filp)
729 nfs_file_clear_open_context(filp);
734 * This function is called whenever some part of NFS notices that
735 * the cached attributes have to be refreshed.
738 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
740 int status = -ESTALE;
741 struct nfs_fattr fattr;
742 struct nfs_inode *nfsi = NFS_I(inode);
744 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
745 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
747 if (is_bad_inode(inode))
749 if (NFS_STALE(inode))
752 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
753 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
755 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
757 (long long)NFS_FILEID(inode), status);
758 if (status == -ESTALE) {
759 nfs_zap_caches(inode);
760 if (!S_ISDIR(inode->i_mode))
761 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
766 status = nfs_refresh_inode(inode, &fattr);
768 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
770 (long long)NFS_FILEID(inode), status);
774 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
775 nfs_zap_acl_cache(inode);
777 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
779 (long long)NFS_FILEID(inode));
785 int nfs_attribute_timeout(struct inode *inode)
787 struct nfs_inode *nfsi = NFS_I(inode);
789 if (nfs_have_delegation(inode, FMODE_READ))
791 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
795 * nfs_revalidate_inode - Revalidate the inode attributes
796 * @server - pointer to nfs_server struct
797 * @inode - pointer to inode struct
799 * Updates inode attribute information by retrieving the data from the server.
801 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
803 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
804 && !nfs_attribute_timeout(inode))
805 return NFS_STALE(inode) ? -ESTALE : 0;
806 return __nfs_revalidate_inode(server, inode);
809 static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
811 struct nfs_inode *nfsi = NFS_I(inode);
813 if (mapping->nrpages != 0) {
814 int ret = invalidate_inode_pages2(mapping);
818 spin_lock(&inode->i_lock);
819 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
820 if (S_ISDIR(inode->i_mode))
821 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
822 spin_unlock(&inode->i_lock);
823 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
824 nfs_fscache_reset_inode_cookie(inode);
825 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
826 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
830 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
834 mutex_lock(&inode->i_mutex);
835 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
836 ret = nfs_sync_mapping(mapping);
838 ret = nfs_invalidate_mapping_nolock(inode, mapping);
840 mutex_unlock(&inode->i_mutex);
845 * nfs_revalidate_mapping_nolock - Revalidate the pagecache
846 * @inode - pointer to host inode
847 * @mapping - pointer to mapping
849 int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
851 struct nfs_inode *nfsi = NFS_I(inode);
854 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
855 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
856 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
860 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
861 ret = nfs_invalidate_mapping_nolock(inode, mapping);
867 * nfs_revalidate_mapping - Revalidate the pagecache
868 * @inode - pointer to host inode
869 * @mapping - pointer to mapping
871 * This version of the function will take the inode->i_mutex and attempt to
872 * flush out all dirty data if it needs to invalidate the page cache.
874 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
876 struct nfs_inode *nfsi = NFS_I(inode);
879 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
880 || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
881 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
885 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
886 ret = nfs_invalidate_mapping(inode, mapping);
891 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
893 struct nfs_inode *nfsi = NFS_I(inode);
895 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
896 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
897 && nfsi->change_attr == fattr->pre_change_attr) {
898 nfsi->change_attr = fattr->change_attr;
899 if (S_ISDIR(inode->i_mode))
900 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
902 /* If we have atomic WCC data, we may update some attributes */
903 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
904 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
905 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
906 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
908 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
909 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
910 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
911 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
912 if (S_ISDIR(inode->i_mode))
913 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
915 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
916 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
917 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
918 && nfsi->npages == 0)
919 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
923 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
924 * @inode - pointer to inode
925 * @fattr - updated attributes
927 * Verifies the attribute cache. If we have just changed the attributes,
928 * so that fattr carries weak cache consistency data, then it may
929 * also update the ctime/mtime/change_attribute.
931 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
933 struct nfs_inode *nfsi = NFS_I(inode);
934 loff_t cur_size, new_isize;
935 unsigned long invalid = 0;
938 /* Has the inode gone and changed behind our back? */
939 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
941 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
944 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
945 nfsi->change_attr != fattr->change_attr)
946 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
948 /* Verify a few of the more important attributes */
949 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
950 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
952 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
953 cur_size = i_size_read(inode);
954 new_isize = nfs_size_to_loff_t(fattr->size);
955 if (cur_size != new_isize && nfsi->npages == 0)
956 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
959 /* Have any file permissions changed? */
960 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
961 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
962 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
963 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
964 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
965 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
967 /* Has the link count changed? */
968 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
969 invalid |= NFS_INO_INVALID_ATTR;
971 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
972 invalid |= NFS_INO_INVALID_ATIME;
975 nfsi->cache_validity |= invalid;
977 nfsi->read_cache_jiffies = fattr->time_start;
981 static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
983 if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
985 return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
988 static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
990 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
992 return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
995 static atomic_long_t nfs_attr_generation_counter;
997 static unsigned long nfs_read_attr_generation_counter(void)
999 return atomic_long_read(&nfs_attr_generation_counter);
1002 unsigned long nfs_inc_attr_generation_counter(void)
1004 return atomic_long_inc_return(&nfs_attr_generation_counter);
1007 void nfs_fattr_init(struct nfs_fattr *fattr)
1010 fattr->time_start = jiffies;
1011 fattr->gencount = nfs_inc_attr_generation_counter();
1015 * nfs_inode_attrs_need_update - check if the inode attributes need updating
1016 * @inode - pointer to inode
1017 * @fattr - attributes
1019 * Attempt to divine whether or not an RPC call reply carrying stale
1020 * attributes got scheduled after another call carrying updated ones.
1022 * To do so, the function first assumes that a more recent ctime means
1023 * that the attributes in fattr are newer, however it also attempt to
1024 * catch the case where ctime either didn't change, or went backwards
1025 * (if someone reset the clock on the server) by looking at whether
1026 * or not this RPC call was started after the inode was last updated.
1027 * Note also the check for wraparound of 'attr_gencount'
1029 * The function returns 'true' if it thinks the attributes in 'fattr' are
1030 * more recent than the ones cached in the inode.
1033 static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1035 const struct nfs_inode *nfsi = NFS_I(inode);
1037 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
1038 nfs_ctime_need_update(inode, fattr) ||
1039 nfs_size_need_update(inode, fattr) ||
1040 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
1043 static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1045 if (nfs_inode_attrs_need_update(inode, fattr))
1046 return nfs_update_inode(inode, fattr);
1047 return nfs_check_inode_attributes(inode, fattr);
1051 * nfs_refresh_inode - try to update the inode attribute cache
1052 * @inode - pointer to inode
1053 * @fattr - updated attributes
1055 * Check that an RPC call that returned attributes has not overlapped with
1056 * other recent updates of the inode metadata, then decide whether it is
1057 * safe to do a full update of the inode attributes, or whether just to
1058 * call nfs_check_inode_attributes.
1060 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1064 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1066 spin_lock(&inode->i_lock);
1067 status = nfs_refresh_inode_locked(inode, fattr);
1068 spin_unlock(&inode->i_lock);
1073 static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1075 struct nfs_inode *nfsi = NFS_I(inode);
1077 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1078 if (S_ISDIR(inode->i_mode))
1079 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1080 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1082 return nfs_refresh_inode_locked(inode, fattr);
1086 * nfs_post_op_update_inode - try to update the inode attribute cache
1087 * @inode - pointer to inode
1088 * @fattr - updated attributes
1090 * After an operation that has changed the inode metadata, mark the
1091 * attribute cache as being invalid, then try to update it.
1093 * NB: if the server didn't return any post op attributes, this
1094 * function will force the retrieval of attributes before the next
1095 * NFS request. Thus it should be used only for operations that
1096 * are expected to change one or more attributes, to avoid
1097 * unnecessary NFS requests and trips through nfs_update_inode().
1099 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1103 spin_lock(&inode->i_lock);
1104 status = nfs_post_op_update_inode_locked(inode, fattr);
1105 spin_unlock(&inode->i_lock);
1110 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1111 * @inode - pointer to inode
1112 * @fattr - updated attributes
1114 * After an operation that has changed the inode metadata, mark the
1115 * attribute cache as being invalid, then try to update it. Fake up
1116 * weak cache consistency data, if none exist.
1118 * This function is mainly designed to be used by the ->write_done() functions.
1120 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1124 spin_lock(&inode->i_lock);
1125 /* Don't do a WCC update if these attributes are already stale */
1126 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1127 !nfs_inode_attrs_need_update(inode, fattr)) {
1128 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1129 | NFS_ATTR_FATTR_PRESIZE
1130 | NFS_ATTR_FATTR_PREMTIME
1131 | NFS_ATTR_FATTR_PRECTIME);
1134 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1135 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1136 fattr->pre_change_attr = NFS_I(inode)->change_attr;
1137 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1139 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1140 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
1141 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
1142 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1144 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1145 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
1146 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
1147 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1149 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1150 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1151 fattr->pre_size = i_size_read(inode);
1152 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1155 status = nfs_post_op_update_inode_locked(inode, fattr);
1156 spin_unlock(&inode->i_lock);
1161 * Many nfs protocol calls return the new file attributes after
1162 * an operation. Here we update the inode to reflect the state
1163 * of the server's inode.
1165 * This is a bit tricky because we have to make sure all dirty pages
1166 * have been sent off to the server before calling invalidate_inode_pages.
1167 * To make sure no other process adds more write requests while we try
1168 * our best to flush them, we make them sleep during the attribute refresh.
1170 * A very similar scenario holds for the dir cache.
1172 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1174 struct nfs_server *server;
1175 struct nfs_inode *nfsi = NFS_I(inode);
1176 loff_t cur_isize, new_isize;
1177 unsigned long invalid = 0;
1178 unsigned long now = jiffies;
1179 unsigned long save_cache_validity;
1181 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1182 __func__, inode->i_sb->s_id, inode->i_ino,
1183 atomic_read(&inode->i_count), fattr->valid);
1185 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
1189 * Make sure the inode's type hasn't changed.
1191 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1194 server = NFS_SERVER(inode);
1195 /* Update the fsid? */
1196 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
1197 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1198 !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
1199 server->fsid = fattr->fsid;
1202 * Update the read time so we don't revalidate too often.
1204 nfsi->read_cache_jiffies = fattr->time_start;
1206 save_cache_validity = nfsi->cache_validity;
1207 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1208 | NFS_INO_INVALID_ATIME
1209 | NFS_INO_REVAL_FORCED
1210 | NFS_INO_REVAL_PAGECACHE);
1212 /* Do atomic weak cache consistency updates */
1213 nfs_wcc_update_inode(inode, fattr);
1215 /* More cache consistency checks */
1216 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1217 if (nfsi->change_attr != fattr->change_attr) {
1218 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1219 inode->i_sb->s_id, inode->i_ino);
1220 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1221 if (S_ISDIR(inode->i_mode))
1222 nfs_force_lookup_revalidate(inode);
1223 nfsi->change_attr = fattr->change_attr;
1225 } else if (server->caps & NFS_CAP_CHANGE_ATTR)
1226 invalid |= save_cache_validity;
1228 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
1229 /* NFSv2/v3: Check if the mtime agrees */
1230 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1231 dprintk("NFS: mtime change on server for file %s/%ld\n",
1232 inode->i_sb->s_id, inode->i_ino);
1233 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1234 if (S_ISDIR(inode->i_mode))
1235 nfs_force_lookup_revalidate(inode);
1236 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1238 } else if (server->caps & NFS_CAP_MTIME)
1239 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1240 | NFS_INO_INVALID_DATA
1241 | NFS_INO_REVAL_PAGECACHE
1242 | NFS_INO_REVAL_FORCED);
1244 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
1245 /* If ctime has changed we should definitely clear access+acl caches */
1246 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
1247 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1248 /* and probably clear data for a directory too as utimes can cause
1249 * havoc with our cache.
1251 if (S_ISDIR(inode->i_mode)) {
1252 invalid |= NFS_INO_INVALID_DATA;
1253 nfs_force_lookup_revalidate(inode);
1255 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1257 } else if (server->caps & NFS_CAP_CTIME)
1258 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1259 | NFS_INO_INVALID_ACCESS
1260 | NFS_INO_INVALID_ACL
1261 | NFS_INO_REVAL_FORCED);
1263 /* Check if our cached file size is stale */
1264 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1265 new_isize = nfs_size_to_loff_t(fattr->size);
1266 cur_isize = i_size_read(inode);
1267 if (new_isize != cur_isize) {
1268 /* Do we perhaps have any outstanding writes, or has
1269 * the file grown beyond our last write? */
1270 if (nfsi->npages == 0 || new_isize > cur_isize) {
1271 i_size_write(inode, new_isize);
1272 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1274 dprintk("NFS: isize change on server for file %s/%ld\n",
1275 inode->i_sb->s_id, inode->i_ino);
1278 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1279 | NFS_INO_REVAL_PAGECACHE
1280 | NFS_INO_REVAL_FORCED);
1283 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1284 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1285 else if (server->caps & NFS_CAP_ATIME)
1286 invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
1287 | NFS_INO_REVAL_FORCED);
1289 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1290 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1291 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1292 inode->i_mode = fattr->mode;
1294 } else if (server->caps & NFS_CAP_MODE)
1295 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1296 | NFS_INO_INVALID_ACCESS
1297 | NFS_INO_INVALID_ACL
1298 | NFS_INO_REVAL_FORCED);
1300 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
1301 if (inode->i_uid != fattr->uid) {
1302 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1303 inode->i_uid = fattr->uid;
1305 } else if (server->caps & NFS_CAP_OWNER)
1306 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1307 | NFS_INO_INVALID_ACCESS
1308 | NFS_INO_INVALID_ACL
1309 | NFS_INO_REVAL_FORCED);
1311 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
1312 if (inode->i_gid != fattr->gid) {
1313 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1314 inode->i_gid = fattr->gid;
1316 } else if (server->caps & NFS_CAP_OWNER_GROUP)
1317 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1318 | NFS_INO_INVALID_ACCESS
1319 | NFS_INO_INVALID_ACL
1320 | NFS_INO_REVAL_FORCED);
1322 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1323 if (inode->i_nlink != fattr->nlink) {
1324 invalid |= NFS_INO_INVALID_ATTR;
1325 if (S_ISDIR(inode->i_mode))
1326 invalid |= NFS_INO_INVALID_DATA;
1327 inode->i_nlink = fattr->nlink;
1329 } else if (server->caps & NFS_CAP_NLINK)
1330 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1331 | NFS_INO_REVAL_FORCED);
1333 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1335 * report the blocks in 512byte units
1337 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1339 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1340 inode->i_blocks = fattr->du.nfs2.blocks;
1342 /* Update attrtimeo value if we're out of the unstable period */
1343 if (invalid & NFS_INO_INVALID_ATTR) {
1344 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1345 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1346 nfsi->attrtimeo_timestamp = now;
1347 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1349 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
1350 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1351 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1352 nfsi->attrtimeo_timestamp = now;
1355 invalid &= ~NFS_INO_INVALID_ATTR;
1356 /* Don't invalidate the data if we were to blame */
1357 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1358 || S_ISLNK(inode->i_mode)))
1359 invalid &= ~NFS_INO_INVALID_DATA;
1360 if (!nfs_have_delegation(inode, FMODE_READ) ||
1361 (save_cache_validity & NFS_INO_REVAL_FORCED))
1362 nfsi->cache_validity |= invalid;
1367 * Big trouble! The inode has become a different object.
1369 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1370 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1373 * No need to worry about unhashing the dentry, as the
1374 * lookup validation will know that the inode is bad.
1375 * (But we fall through to invalidate the caches.)
1377 nfs_invalidate_inode(inode);
1381 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1382 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1383 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
1384 (long long)nfsi->fileid, (long long)fattr->fileid);
1389 #ifdef CONFIG_NFS_V4
1392 * Clean out any remaining NFSv4 state that might be left over due
1393 * to open() calls that passed nfs_atomic_lookup, but failed to call
1396 void nfs4_clear_inode(struct inode *inode)
1398 /* If we are holding a delegation, return it! */
1399 nfs_inode_return_delegation_noreclaim(inode);
1400 /* First call standard NFS clear_inode() code */
1401 nfs_clear_inode(inode);
1405 struct inode *nfs_alloc_inode(struct super_block *sb)
1407 struct nfs_inode *nfsi;
1408 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
1412 nfsi->cache_validity = 0UL;
1413 #ifdef CONFIG_NFS_V3_ACL
1414 nfsi->acl_access = ERR_PTR(-EAGAIN);
1415 nfsi->acl_default = ERR_PTR(-EAGAIN);
1417 #ifdef CONFIG_NFS_V4
1418 nfsi->nfs4_acl = NULL;
1419 #endif /* CONFIG_NFS_V4 */
1420 return &nfsi->vfs_inode;
1423 void nfs_destroy_inode(struct inode *inode)
1425 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1428 static inline void nfs4_init_once(struct nfs_inode *nfsi)
1430 #ifdef CONFIG_NFS_V4
1431 INIT_LIST_HEAD(&nfsi->open_states);
1432 nfsi->delegation = NULL;
1433 nfsi->delegation_state = 0;
1434 init_rwsem(&nfsi->rwsem);
1438 static void init_once(void *foo)
1440 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1442 inode_init_once(&nfsi->vfs_inode);
1443 INIT_LIST_HEAD(&nfsi->open_files);
1444 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1445 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
1446 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1448 atomic_set(&nfsi->silly_count, 1);
1449 INIT_HLIST_HEAD(&nfsi->silly_list);
1450 init_waitqueue_head(&nfsi->waitqueue);
1451 nfs4_init_once(nfsi);
1454 static int __init nfs_init_inodecache(void)
1456 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1457 sizeof(struct nfs_inode),
1458 0, (SLAB_RECLAIM_ACCOUNT|
1461 if (nfs_inode_cachep == NULL)
1467 static void nfs_destroy_inodecache(void)
1469 kmem_cache_destroy(nfs_inode_cachep);
1472 struct workqueue_struct *nfsiod_workqueue;
1475 * start up the nfsiod workqueue
1477 static int nfsiod_start(void)
1479 struct workqueue_struct *wq;
1480 dprintk("RPC: creating workqueue nfsiod\n");
1481 wq = create_singlethread_workqueue("nfsiod");
1484 nfsiod_workqueue = wq;
1489 * Destroy the nfsiod workqueue
1491 static void nfsiod_stop(void)
1493 struct workqueue_struct *wq;
1495 wq = nfsiod_workqueue;
1498 nfsiod_workqueue = NULL;
1499 destroy_workqueue(wq);
1505 static int __init init_nfs_fs(void)
1509 err = nfs_fscache_register();
1513 err = nfsiod_start();
1517 err = nfs_fs_proc_init();
1521 err = nfs_init_nfspagecache();
1525 err = nfs_init_inodecache();
1529 err = nfs_init_readpagecache();
1533 err = nfs_init_writepagecache();
1537 err = nfs_init_directcache();
1541 #ifdef CONFIG_PROC_FS
1542 rpc_proc_register(&nfs_rpcstat);
1544 if ((err = register_nfs_fs()) != 0)
1548 #ifdef CONFIG_PROC_FS
1549 rpc_proc_unregister("nfs");
1551 nfs_destroy_directcache();
1553 nfs_destroy_writepagecache();
1555 nfs_destroy_readpagecache();
1557 nfs_destroy_inodecache();
1559 nfs_destroy_nfspagecache();
1565 nfs_fscache_unregister();
1570 static void __exit exit_nfs_fs(void)
1572 nfs_destroy_directcache();
1573 nfs_destroy_writepagecache();
1574 nfs_destroy_readpagecache();
1575 nfs_destroy_inodecache();
1576 nfs_destroy_nfspagecache();
1577 nfs_fscache_unregister();
1578 #ifdef CONFIG_PROC_FS
1579 rpc_proc_unregister("nfs");
1581 unregister_nfs_fs();
1586 /* Not quite true; I just maintain it */
1587 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1588 MODULE_LICENSE("GPL");
1589 module_param(enable_ino64, bool, 0644);
1591 module_init(init_nfs_fs)
1592 module_exit(exit_nfs_fs)