4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, 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>
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/smp_lock.h>
34 #include <linux/seq_file.h>
35 #include <linux/mount.h>
36 #include <linux/nfs_idmap.h>
37 #include <linux/vfs.h>
38 #include <linux/inet.h>
39 #include <linux/nfs_xdr.h>
40 #include <linux/vserver/xid.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
47 #include "delegation.h"
51 #define NFSDBG_FACILITY NFSDBG_VFS
52 #define NFS_PARANOIA 1
54 static void nfs_invalidate_inode(struct inode *);
55 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
57 static void nfs_zap_acl_cache(struct inode *);
59 static kmem_cache_t * nfs_inode_cachep;
61 int nfs_write_inode(struct inode *inode, int sync)
63 int flags = sync ? FLUSH_SYNC : 0;
66 ret = nfs_commit_inode(inode, flags);
72 void nfs_clear_inode(struct inode *inode)
74 struct nfs_inode *nfsi = NFS_I(inode);
75 struct rpc_cred *cred;
78 * The following should never happen...
80 BUG_ON(nfs_have_writebacks(inode));
81 BUG_ON (!list_empty(&nfsi->open_files));
82 nfs_zap_acl_cache(inode);
83 cred = nfsi->cache_access.cred;
87 nfs_fscache_release_fh_cookie(NFS_SERVER(inode), nfsi);
88 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
92 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
94 int nfs_sync_mapping(struct address_space *mapping)
98 if (mapping->nrpages == 0)
100 unmap_mapping_range(mapping, 0, 0, 0);
101 ret = filemap_write_and_wait(mapping);
104 ret = nfs_wb_all(mapping->host);
110 * Invalidate the local caches
112 static void nfs_zap_caches_locked(struct inode *inode)
114 struct nfs_inode *nfsi = NFS_I(inode);
115 int mode = inode->i_mode;
117 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
119 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
120 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
122 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
123 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
124 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
126 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
129 void nfs_zap_caches(struct inode *inode)
131 spin_lock(&inode->i_lock);
132 nfs_zap_caches_locked(inode);
133 spin_unlock(&inode->i_lock);
135 nfs_fscache_zap_fh_cookie(NFS_SERVER(inode), NFS_I(inode));
138 static void nfs_zap_acl_cache(struct inode *inode)
140 void (*clear_acl_cache)(struct inode *);
142 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
143 if (clear_acl_cache != NULL)
144 clear_acl_cache(inode);
145 spin_lock(&inode->i_lock);
146 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
147 spin_unlock(&inode->i_lock);
151 * Invalidate, but do not unhash, the inode.
152 * NB: must be called with inode->i_lock held!
154 static void nfs_invalidate_inode(struct inode *inode)
156 set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
157 nfs_zap_caches_locked(inode);
160 struct nfs_find_desc {
162 struct nfs_fattr *fattr;
166 * In NFSv3 we can have 64bit inode numbers. In order to support
167 * this, and re-exported directories (also seen in NFSv2)
168 * we are forced to allow 2 different inodes to have the same
172 nfs_find_actor(struct inode *inode, void *opaque)
174 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
175 struct nfs_fh *fh = desc->fh;
176 struct nfs_fattr *fattr = desc->fattr;
178 if (NFS_FILEID(inode) != fattr->fileid)
180 if (nfs_compare_fh(NFS_FH(inode), fh))
182 if (is_bad_inode(inode) || NFS_STALE(inode))
188 nfs_init_locked(struct inode *inode, void *opaque)
190 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
191 struct nfs_fattr *fattr = desc->fattr;
193 NFS_FILEID(inode) = fattr->fileid;
194 nfs_copy_fh(NFS_FH(inode), desc->fh);
198 /* Don't use READDIRPLUS on directories that we believe are too large */
199 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
202 * This is our front-end to iget that looks up inodes by file handle
203 * instead of inode number.
206 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
208 struct nfs_find_desc desc = {
212 struct inode *inode = ERR_PTR(-ENOENT);
216 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
220 printk("NFS: Buggy server - nlink == 0!\n");
224 hash = fattr->fileid;
225 if (sizeof(hash) < sizeof(u64))
226 hash ^= fattr->fileid >> (sizeof(u64) - sizeof(ino_t)) * 8;
228 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
230 inode = ERR_PTR(-ENOMEM);
234 if (inode->i_state & I_NEW) {
235 struct nfs_inode *nfsi = NFS_I(inode);
237 /* We set i_ino for the few things that still rely on it, such
238 * as printing messages; stat and filldir use the fileid
239 * directly since i_ino may not be large enough */
240 inode->i_ino = fattr->fileid;
242 /* We can't support update_atime(), since the server will reset it */
243 inode->i_flags |= S_NOATIME|S_NOCMTIME;
244 inode->i_mode = fattr->mode;
245 /* Why so? Because we want revalidate for devices/FIFOs, and
246 * that's precisely what we have in nfs_file_inode_operations.
248 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
249 if (S_ISREG(inode->i_mode)) {
250 inode->i_fop = &nfs_file_operations;
251 inode->i_data.a_ops = &nfs_file_aops;
252 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
253 } else if (S_ISDIR(inode->i_mode)) {
254 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
255 inode->i_fop = &nfs_dir_operations;
256 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
257 && fattr->size <= NFS_LIMIT_READDIRPLUS)
258 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
259 /* Deal with crossing mountpoints */
260 if (!nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
261 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
262 inode->i_op = &nfs_referral_inode_operations;
264 inode->i_op = &nfs_mountpoint_inode_operations;
268 } else if (S_ISLNK(inode->i_mode))
269 inode->i_op = &nfs_symlink_inode_operations;
271 init_special_inode(inode, inode->i_mode, fattr->rdev);
273 nfsi->read_cache_jiffies = fattr->time_start;
274 nfsi->last_updated = jiffies;
275 inode->i_atime = fattr->atime;
276 inode->i_mtime = fattr->mtime;
277 inode->i_ctime = fattr->ctime;
278 if (fattr->valid & NFS_ATTR_FATTR_V4)
279 nfsi->change_attr = fattr->change_attr;
280 inode->i_size = nfs_size_to_loff_t(fattr->size);
281 inode->i_nlink = fattr->nlink;
282 inode->i_uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
283 inode->i_gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
284 inode->i_xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
285 /* maybe fattr->xid someday */
286 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
288 * report the blocks in 512byte units
290 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
292 inode->i_blocks = fattr->du.nfs2.blocks;
294 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
295 nfsi->attrtimeo_timestamp = jiffies;
296 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
297 nfsi->cache_access.cred = NULL;
299 nfs_fscache_get_fh_cookie(sb, nfsi, maycache);
301 unlock_new_inode(inode);
303 nfs_refresh_inode(inode, fattr);
304 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
306 (long long)NFS_FILEID(inode),
307 atomic_read(&inode->i_count));
313 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
317 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
320 nfs_setattr(struct dentry *dentry, struct iattr *attr)
322 struct inode *inode = dentry->d_inode;
323 struct nfs_fattr fattr;
326 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
328 if (attr->ia_valid & ATTR_SIZE) {
329 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
330 attr->ia_valid &= ~ATTR_SIZE;
333 /* Optimization: if the end result is no change, don't RPC */
334 attr->ia_valid &= NFS_VALID_ATTRS;
335 if (attr->ia_valid == 0)
339 nfs_begin_data_update(inode);
340 /* Write all dirty data */
341 filemap_write_and_wait(inode->i_mapping);
344 * Return any delegations if we're going to change ACLs
346 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
347 nfs_inode_return_delegation(inode);
348 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
350 nfs_refresh_inode(inode, &fattr);
351 nfs_end_data_update(inode);
357 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
358 * @inode: pointer to struct inode
359 * @attr: pointer to struct iattr
361 * Note: we do this in the *proc.c in order to ensure that
362 * it works for things like exclusive creates too.
364 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
366 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
367 if ((attr->ia_valid & ATTR_MODE) != 0) {
368 int mode = attr->ia_mode & S_IALLUGO;
369 mode |= inode->i_mode & ~S_IALLUGO;
370 inode->i_mode = mode;
372 if ((attr->ia_valid & ATTR_UID) != 0)
373 inode->i_uid = attr->ia_uid;
374 if ((attr->ia_valid & ATTR_GID) != 0)
375 inode->i_gid = attr->ia_gid;
376 if ((attr->ia_valid & ATTR_XID) && IS_TAGXID(inode))
377 inode->i_xid = attr->ia_xid;
378 spin_lock(&inode->i_lock);
379 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
380 spin_unlock(&inode->i_lock);
382 if ((attr->ia_valid & ATTR_SIZE) != 0) {
383 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
384 inode->i_size = attr->ia_size;
385 nfs_fscache_set_size(NFS_SERVER(inode), NFS_I(inode), inode->i_size);
386 vmtruncate(inode, attr->ia_size);
390 static int nfs_wait_schedule(void *word)
392 if (signal_pending(current))
399 * Wait for the inode to get unlocked.
401 static int nfs_wait_on_inode(struct inode *inode)
403 struct rpc_clnt *clnt = NFS_CLIENT(inode);
404 struct nfs_inode *nfsi = NFS_I(inode);
408 rpc_clnt_sigmask(clnt, &oldmask);
409 error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
410 nfs_wait_schedule, TASK_INTERRUPTIBLE);
411 rpc_clnt_sigunmask(clnt, &oldmask);
416 static void nfs_wake_up_inode(struct inode *inode)
418 struct nfs_inode *nfsi = NFS_I(inode);
420 clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
421 smp_mb__after_clear_bit();
422 wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
425 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
427 struct inode *inode = dentry->d_inode;
428 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
431 /* Flush out writes to the server in order to update c/mtime */
432 nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
435 * We may force a getattr if the user cares about atime.
437 * Note that we only have to check the vfsmount flags here:
438 * - NFS always sets S_NOATIME by so checking it would give a
440 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
441 * no point in checking those.
443 if ((mnt->mnt_flags & MNT_NOATIME) ||
444 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
448 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
450 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
452 generic_fillattr(inode, stat);
453 stat->ino = NFS_FILEID(inode);
458 static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
460 struct nfs_open_context *ctx;
462 ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
464 atomic_set(&ctx->count, 1);
465 ctx->dentry = dget(dentry);
466 ctx->vfsmnt = mntget(mnt);
467 ctx->cred = get_rpccred(cred);
469 ctx->lockowner = current->files;
476 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
479 atomic_inc(&ctx->count);
483 void put_nfs_open_context(struct nfs_open_context *ctx)
485 if (atomic_dec_and_test(&ctx->count)) {
486 if (!list_empty(&ctx->list)) {
487 struct inode *inode = ctx->dentry->d_inode;
488 spin_lock(&inode->i_lock);
489 list_del(&ctx->list);
490 spin_unlock(&inode->i_lock);
492 if (ctx->state != NULL)
493 nfs4_close_state(ctx->state, ctx->mode);
494 if (ctx->cred != NULL)
495 put_rpccred(ctx->cred);
503 * Ensure that mmap has a recent RPC credential for use when writing out
506 static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
508 struct inode *inode = filp->f_dentry->d_inode;
509 struct nfs_inode *nfsi = NFS_I(inode);
511 filp->private_data = get_nfs_open_context(ctx);
512 spin_lock(&inode->i_lock);
513 list_add(&ctx->list, &nfsi->open_files);
514 spin_unlock(&inode->i_lock);
518 * Given an inode, search for an open context with the desired characteristics
520 struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
522 struct nfs_inode *nfsi = NFS_I(inode);
523 struct nfs_open_context *pos, *ctx = NULL;
525 spin_lock(&inode->i_lock);
526 list_for_each_entry(pos, &nfsi->open_files, list) {
527 if (cred != NULL && pos->cred != cred)
529 if ((pos->mode & mode) == mode) {
530 ctx = get_nfs_open_context(pos);
534 spin_unlock(&inode->i_lock);
538 static void nfs_file_clear_open_context(struct file *filp)
540 struct inode *inode = filp->f_dentry->d_inode;
541 struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
544 filp->private_data = NULL;
545 spin_lock(&inode->i_lock);
546 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
547 spin_unlock(&inode->i_lock);
548 put_nfs_open_context(ctx);
553 * These allocate and release file read/write context information.
555 int nfs_open(struct inode *inode, struct file *filp)
557 struct nfs_open_context *ctx;
558 struct rpc_cred *cred;
560 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
562 return PTR_ERR(cred);
563 ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
567 ctx->mode = filp->f_mode;
568 nfs_file_set_open_context(filp, ctx);
569 put_nfs_open_context(ctx);
570 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
571 nfs_fscache_disable_fh_cookie(inode);
575 int nfs_release(struct inode *inode, struct file *filp)
577 nfs_file_clear_open_context(filp);
582 * This function is called whenever some part of NFS notices that
583 * the cached attributes have to be refreshed.
586 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
588 int status = -ESTALE;
589 struct nfs_fattr fattr;
590 struct nfs_inode *nfsi = NFS_I(inode);
592 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
593 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
595 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
597 if (!inode || is_bad_inode(inode))
599 if (NFS_STALE(inode))
602 status = nfs_wait_on_inode(inode);
605 if (NFS_STALE(inode)) {
607 /* Do we trust the cached ESTALE? */
608 if (NFS_ATTRTIMEO(inode) != 0) {
609 if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) {
616 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
618 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
620 (long long)NFS_FILEID(inode), status);
621 if (status == -ESTALE) {
622 nfs_zap_caches(inode);
623 if (!S_ISDIR(inode->i_mode))
624 set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
629 spin_lock(&inode->i_lock);
630 status = nfs_update_inode(inode, &fattr);
632 spin_unlock(&inode->i_lock);
633 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
635 (long long)NFS_FILEID(inode), status);
638 spin_unlock(&inode->i_lock);
640 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
641 nfs_zap_acl_cache(inode);
643 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
645 (long long)NFS_FILEID(inode));
648 nfs_wake_up_inode(inode);
655 int nfs_attribute_timeout(struct inode *inode)
657 struct nfs_inode *nfsi = NFS_I(inode);
659 if (nfs_have_delegation(inode, FMODE_READ))
661 return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
665 * nfs_revalidate_inode - Revalidate the inode attributes
666 * @server - pointer to nfs_server struct
667 * @inode - pointer to inode struct
669 * Updates inode attribute information by retrieving the data from the server.
671 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
673 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
674 && !nfs_attribute_timeout(inode))
675 return NFS_STALE(inode) ? -ESTALE : 0;
676 return __nfs_revalidate_inode(server, inode);
680 * nfs_revalidate_mapping - Revalidate the pagecache
681 * @inode - pointer to host inode
682 * @mapping - pointer to mapping
684 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
686 struct nfs_inode *nfsi = NFS_I(inode);
689 if (NFS_STALE(inode))
691 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
692 || nfs_attribute_timeout(inode))
693 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
695 if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
696 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
697 if (S_ISREG(inode->i_mode))
698 nfs_sync_mapping(mapping);
699 invalidate_inode_pages2(mapping);
701 spin_lock(&inode->i_lock);
702 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
703 if (S_ISDIR(inode->i_mode)) {
704 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
705 /* This ensures we revalidate child dentries */
706 nfsi->cache_change_attribute = jiffies;
708 spin_unlock(&inode->i_lock);
710 nfs_fscache_renew_fh_cookie(NFS_SERVER(inode), nfsi);
712 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
714 (long long)NFS_FILEID(inode));
720 * nfs_begin_data_update
721 * @inode - pointer to inode
722 * Declare that a set of operations will update file data on the server
724 void nfs_begin_data_update(struct inode *inode)
726 atomic_inc(&NFS_I(inode)->data_updates);
730 * nfs_end_data_update
731 * @inode - pointer to inode
732 * Declare end of the operations that will update file data
733 * This will mark the inode as immediately needing revalidation
734 * of its attribute cache.
736 void nfs_end_data_update(struct inode *inode)
738 struct nfs_inode *nfsi = NFS_I(inode);
740 if (!nfs_have_delegation(inode, FMODE_READ)) {
741 /* Directories and symlinks: invalidate page cache */
742 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
743 spin_lock(&inode->i_lock);
744 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
745 spin_unlock(&inode->i_lock);
748 nfsi->cache_change_attribute = jiffies;
749 atomic_dec(&nfsi->data_updates);
752 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
754 struct nfs_inode *nfsi = NFS_I(inode);
756 /* If we have atomic WCC data, we may update some attributes */
757 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
758 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
759 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
760 nfsi->cache_change_attribute = jiffies;
762 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
763 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
764 nfsi->cache_change_attribute = jiffies;
766 if (inode->i_size == fattr->pre_size && nfsi->npages == 0) {
767 inode->i_size = fattr->size;
768 nfsi->cache_change_attribute = jiffies;
774 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
775 * @inode - pointer to inode
776 * @fattr - updated attributes
778 * Verifies the attribute cache. If we have just changed the attributes,
779 * so that fattr carries weak cache consistency data, then it may
780 * also update the ctime/mtime/change_attribute.
782 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
784 struct nfs_inode *nfsi = NFS_I(inode);
785 loff_t cur_size, new_isize;
792 /* Has the inode gone and changed behind our back? */
793 if (nfsi->fileid != fattr->fileid
794 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
798 /* Are we in the process of updating data on the server? */
799 data_unstable = nfs_caches_unstable(inode);
801 /* Do atomic weak cache consistency updates */
802 nfs_wcc_update_inode(inode, fattr);
804 if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
805 nfsi->change_attr != fattr->change_attr)
806 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
808 /* Verify a few of the more important attributes */
809 if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
810 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
812 cur_size = i_size_read(inode);
813 new_isize = nfs_size_to_loff_t(fattr->size);
814 if (cur_size != new_isize && nfsi->npages == 0)
815 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
817 uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
818 gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
819 xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
821 /* Have any file permissions changed? */
822 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
823 || inode->i_uid != uid
824 || inode->i_gid != gid
825 || inode->i_xid != xid)
826 nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
828 /* Has the link count changed? */
829 if (inode->i_nlink != fattr->nlink)
830 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
832 if (!timespec_equal(&inode->i_atime, &fattr->atime))
833 nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
835 nfsi->read_cache_jiffies = fattr->time_start;
840 * nfs_refresh_inode - try to update the inode attribute cache
841 * @inode - pointer to inode
842 * @fattr - updated attributes
844 * Check that an RPC call that returned attributes has not overlapped with
845 * other recent updates of the inode metadata, then decide whether it is
846 * safe to do a full update of the inode attributes, or whether just to
847 * call nfs_check_inode_attributes.
849 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
851 struct nfs_inode *nfsi = NFS_I(inode);
854 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
856 spin_lock(&inode->i_lock);
857 if (time_after(fattr->time_start, nfsi->last_updated))
858 status = nfs_update_inode(inode, fattr);
860 status = nfs_check_inode_attributes(inode, fattr);
862 spin_unlock(&inode->i_lock);
867 * nfs_post_op_update_inode - try to update the inode attribute cache
868 * @inode - pointer to inode
869 * @fattr - updated attributes
871 * After an operation that has changed the inode metadata, mark the
872 * attribute cache as being invalid, then try to update it.
874 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
876 struct nfs_inode *nfsi = NFS_I(inode);
879 spin_lock(&inode->i_lock);
880 if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
881 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
884 status = nfs_update_inode(inode, fattr);
886 spin_unlock(&inode->i_lock);
891 * Many nfs protocol calls return the new file attributes after
892 * an operation. Here we update the inode to reflect the state
893 * of the server's inode.
895 * This is a bit tricky because we have to make sure all dirty pages
896 * have been sent off to the server before calling invalidate_inode_pages.
897 * To make sure no other process adds more write requests while we try
898 * our best to flush them, we make them sleep during the attribute refresh.
900 * A very similar scenario holds for the dir cache.
902 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
904 struct nfs_server *server;
905 struct nfs_inode *nfsi = NFS_I(inode);
906 loff_t cur_isize, new_isize;
907 unsigned int invalid = 0;
913 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
914 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
915 atomic_read(&inode->i_count), fattr->valid);
917 if (nfsi->fileid != fattr->fileid)
921 * Make sure the inode's type hasn't changed.
923 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
926 server = NFS_SERVER(inode);
927 /* Update the fsid if and only if this is the root directory */
928 if (inode == inode->i_sb->s_root->d_inode
929 && !nfs_fsid_equal(&server->fsid, &fattr->fsid))
930 server->fsid = fattr->fsid;
933 * Update the read time so we don't revalidate too often.
935 nfsi->read_cache_jiffies = fattr->time_start;
936 nfsi->last_updated = jiffies;
938 /* Are we racing with known updates of the metadata on the server? */
939 data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
941 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
943 /* Do atomic weak cache consistency updates */
944 nfs_wcc_update_inode(inode, fattr);
946 /* Check if our cached file size is stale */
947 new_isize = nfs_size_to_loff_t(fattr->size);
948 cur_isize = i_size_read(inode);
949 if (new_isize != cur_isize) {
950 /* Do we perhaps have any outstanding writes? */
951 if (nfsi->npages == 0) {
952 /* No, but did we race with nfs_end_data_update()? */
954 inode->i_size = new_isize;
955 invalid |= NFS_INO_INVALID_DATA;
956 nfs_fscache_set_size(NFS_SERVER(inode), nfsi, inode->i_size);
958 invalid |= NFS_INO_INVALID_ATTR;
959 } else if (new_isize > cur_isize) {
960 inode->i_size = new_isize;
961 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
962 nfs_fscache_set_size(NFS_SERVER(inode), nfsi, inode->i_size);
964 nfsi->cache_change_attribute = jiffies;
965 dprintk("NFS: isize change on server for file %s/%ld\n",
966 inode->i_sb->s_id, inode->i_ino);
969 /* Check if the mtime agrees */
970 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
971 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
972 dprintk("NFS: mtime change on server for file %s/%ld\n",
973 inode->i_sb->s_id, inode->i_ino);
974 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
975 nfsi->cache_change_attribute = jiffies;
978 /* If ctime has changed we should definitely clear access+acl caches */
979 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
980 invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
981 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
982 nfsi->cache_change_attribute = jiffies;
984 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
986 uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
987 gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
988 xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
990 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
991 inode->i_uid != uid ||
992 inode->i_gid != gid ||
994 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
996 inode->i_mode = fattr->mode;
997 inode->i_nlink = fattr->nlink;
1002 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1004 * report the blocks in 512byte units
1006 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1008 inode->i_blocks = fattr->du.nfs2.blocks;
1011 if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
1012 nfsi->change_attr != fattr->change_attr) {
1013 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1014 inode->i_sb->s_id, inode->i_ino);
1015 nfsi->change_attr = fattr->change_attr;
1016 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1017 nfsi->cache_change_attribute = jiffies;
1020 /* Update attrtimeo value if we're out of the unstable period */
1021 if (invalid & NFS_INO_INVALID_ATTR) {
1022 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1023 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1024 nfsi->attrtimeo_timestamp = jiffies;
1025 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1026 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1027 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1028 nfsi->attrtimeo_timestamp = jiffies;
1030 /* Don't invalidate the data if we were to blame */
1031 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1032 || S_ISLNK(inode->i_mode)))
1033 invalid &= ~NFS_INO_INVALID_DATA;
1035 invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
1036 if (!nfs_have_delegation(inode, FMODE_READ))
1037 nfsi->cache_validity |= invalid;
1042 * Big trouble! The inode has become a different object.
1045 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1046 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1050 * No need to worry about unhashing the dentry, as the
1051 * lookup validation will know that the inode is bad.
1052 * (But we fall through to invalidate the caches.)
1054 nfs_invalidate_inode(inode);
1058 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1059 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1060 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
1061 (long long)nfsi->fileid, (long long)fattr->fileid);
1066 #ifdef CONFIG_NFS_V4
1069 * Clean out any remaining NFSv4 state that might be left over due
1070 * to open() calls that passed nfs_atomic_lookup, but failed to call
1073 void nfs4_clear_inode(struct inode *inode)
1075 struct nfs_inode *nfsi = NFS_I(inode);
1077 /* If we are holding a delegation, return it! */
1078 nfs_inode_return_delegation(inode);
1079 /* First call standard NFS clear_inode() code */
1080 nfs_clear_inode(inode);
1081 /* Now clear out any remaining state */
1082 while (!list_empty(&nfsi->open_states)) {
1083 struct nfs4_state *state;
1085 state = list_entry(nfsi->open_states.next,
1088 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1091 (long long)NFS_FILEID(inode),
1093 BUG_ON(atomic_read(&state->count) != 1);
1094 nfs4_close_state(state, state->state);
1099 struct inode *nfs_alloc_inode(struct super_block *sb)
1101 struct nfs_inode *nfsi;
1102 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1106 nfsi->cache_validity = 0UL;
1107 nfsi->cache_change_attribute = jiffies;
1108 #ifdef CONFIG_NFS_V3_ACL
1109 nfsi->acl_access = ERR_PTR(-EAGAIN);
1110 nfsi->acl_default = ERR_PTR(-EAGAIN);
1112 #ifdef CONFIG_NFS_V4
1113 nfsi->nfs4_acl = NULL;
1114 #endif /* CONFIG_NFS_V4 */
1115 return &nfsi->vfs_inode;
1118 void nfs_destroy_inode(struct inode *inode)
1120 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1123 static inline void nfs4_init_once(struct nfs_inode *nfsi)
1125 #ifdef CONFIG_NFS_V4
1126 INIT_LIST_HEAD(&nfsi->open_states);
1127 nfsi->delegation = NULL;
1128 nfsi->delegation_state = 0;
1129 init_rwsem(&nfsi->rwsem);
1133 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1135 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1137 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1138 SLAB_CTOR_CONSTRUCTOR) {
1139 inode_init_once(&nfsi->vfs_inode);
1140 spin_lock_init(&nfsi->req_lock);
1141 INIT_LIST_HEAD(&nfsi->dirty);
1142 INIT_LIST_HEAD(&nfsi->commit);
1143 INIT_LIST_HEAD(&nfsi->open_files);
1144 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1145 atomic_set(&nfsi->data_updates, 0);
1149 nfs4_init_once(nfsi);
1153 static int __init nfs_init_inodecache(void)
1155 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1156 sizeof(struct nfs_inode),
1157 0, (SLAB_RECLAIM_ACCOUNT|
1160 if (nfs_inode_cachep == NULL)
1166 static void nfs_destroy_inodecache(void)
1168 if (kmem_cache_destroy(nfs_inode_cachep))
1169 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1175 static int __init init_nfs_fs(void)
1179 err = nfs_fscache_register();
1183 err = nfs_fs_proc_init();
1187 err = nfs_init_nfspagecache();
1191 err = nfs_init_inodecache();
1195 err = nfs_init_readpagecache();
1199 err = nfs_init_writepagecache();
1203 err = nfs_init_directcache();
1207 #ifdef CONFIG_PROC_FS
1208 rpc_proc_register(&nfs_rpcstat);
1210 if ((err = register_nfs_fs()) != 0)
1214 #ifdef CONFIG_PROC_FS
1215 rpc_proc_unregister("nfs");
1217 nfs_destroy_directcache();
1219 nfs_destroy_writepagecache();
1221 nfs_destroy_readpagecache();
1223 nfs_destroy_inodecache();
1225 nfs_destroy_nfspagecache();
1229 nfs_fscache_unregister();
1234 static void __exit exit_nfs_fs(void)
1236 nfs_destroy_directcache();
1237 nfs_destroy_writepagecache();
1238 nfs_destroy_readpagecache();
1239 nfs_destroy_inodecache();
1240 nfs_destroy_nfspagecache();
1241 nfs_fscache_unregister();
1242 #ifdef CONFIG_PROC_FS
1243 rpc_proc_unregister("nfs");
1245 unregister_nfs_fs();
1249 /* Not quite true; I just maintain it */
1250 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1251 MODULE_LICENSE("GPL");
1253 module_init(init_nfs_fs)
1254 module_exit(exit_nfs_fs)