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>
39 #include <linux/slab.h>
40 #include <linux/compat.h>
41 #include <linux/freezer.h>
42 #include <linux/crc32.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
49 #include "delegation.h"
53 #include "dns_resolve.h"
57 #define NFSDBG_FACILITY NFSDBG_VFS
59 #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
61 /* Default is to see 64-bit inode numbers */
62 static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
64 static void nfs_invalidate_inode(struct inode *);
65 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
67 static struct kmem_cache * nfs_inode_cachep;
69 static inline unsigned long
70 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
72 return nfs_fileid_to_ino_t(fattr->fileid);
76 * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
77 * @word: long word containing the bit lock
79 int nfs_wait_bit_killable(void *word)
81 if (fatal_signal_pending(current))
88 * nfs_compat_user_ino64 - returns the user-visible inode number
89 * @fileid: 64-bit fileid
91 * This function returns a 32-bit inode number if the boot parameter
92 * nfs.enable_ino64 is zero.
94 u64 nfs_compat_user_ino64(u64 fileid)
105 if (sizeof(ino) < sizeof(fileid))
106 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
110 static void nfs_clear_inode(struct inode *inode)
113 * The following should never happen...
115 BUG_ON(nfs_have_writebacks(inode));
116 BUG_ON(!list_empty(&NFS_I(inode)->open_files));
117 nfs_zap_acl_cache(inode);
118 nfs_access_zap_cache(inode);
119 nfs_fscache_release_inode_cookie(inode);
122 void nfs_evict_inode(struct inode *inode)
124 truncate_inode_pages(&inode->i_data, 0);
125 end_writeback(inode);
126 nfs_clear_inode(inode);
130 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
132 int nfs_sync_mapping(struct address_space *mapping)
136 if (mapping->nrpages != 0) {
137 unmap_mapping_range(mapping, 0, 0, 0);
138 ret = nfs_wb_all(mapping->host);
144 * Invalidate the local caches
146 static void nfs_zap_caches_locked(struct inode *inode)
148 struct nfs_inode *nfsi = NFS_I(inode);
149 int mode = inode->i_mode;
151 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
153 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
154 nfsi->attrtimeo_timestamp = jiffies;
156 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
157 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
158 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
160 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
163 void nfs_zap_caches(struct inode *inode)
165 spin_lock(&inode->i_lock);
166 nfs_zap_caches_locked(inode);
167 spin_unlock(&inode->i_lock);
170 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
172 if (mapping->nrpages != 0) {
173 spin_lock(&inode->i_lock);
174 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
175 spin_unlock(&inode->i_lock);
179 void nfs_zap_acl_cache(struct inode *inode)
181 void (*clear_acl_cache)(struct inode *);
183 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
184 if (clear_acl_cache != NULL)
185 clear_acl_cache(inode);
186 spin_lock(&inode->i_lock);
187 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
188 spin_unlock(&inode->i_lock);
191 void nfs_invalidate_atime(struct inode *inode)
193 spin_lock(&inode->i_lock);
194 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
195 spin_unlock(&inode->i_lock);
199 * Invalidate, but do not unhash, the inode.
200 * NB: must be called with inode->i_lock held!
202 static void nfs_invalidate_inode(struct inode *inode)
204 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
205 nfs_zap_caches_locked(inode);
208 struct nfs_find_desc {
210 struct nfs_fattr *fattr;
214 * In NFSv3 we can have 64bit inode numbers. In order to support
215 * this, and re-exported directories (also seen in NFSv2)
216 * we are forced to allow 2 different inodes to have the same
220 nfs_find_actor(struct inode *inode, void *opaque)
222 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
223 struct nfs_fh *fh = desc->fh;
224 struct nfs_fattr *fattr = desc->fattr;
226 if (NFS_FILEID(inode) != fattr->fileid)
228 if (nfs_compare_fh(NFS_FH(inode), fh))
230 if (is_bad_inode(inode) || NFS_STALE(inode))
236 nfs_init_locked(struct inode *inode, void *opaque)
238 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
239 struct nfs_fattr *fattr = desc->fattr;
241 set_nfs_fileid(inode, fattr->fileid);
242 nfs_copy_fh(NFS_FH(inode), desc->fh);
247 * This is our front-end to iget that looks up inodes by file handle
248 * instead of inode number.
251 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
253 struct nfs_find_desc desc = {
257 struct inode *inode = ERR_PTR(-ENOENT);
260 nfs_attr_check_mountpoint(sb, fattr);
262 if (((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) &&
263 !nfs_attr_use_mounted_on_fileid(fattr))
265 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
268 hash = nfs_fattr_to_ino_t(fattr);
270 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
272 inode = ERR_PTR(-ENOMEM);
276 if (inode->i_state & I_NEW) {
277 struct nfs_inode *nfsi = NFS_I(inode);
278 unsigned long now = jiffies;
280 /* We set i_ino for the few things that still rely on it,
284 /* We can't support update_atime(), since the server will reset it */
285 inode->i_flags |= S_NOATIME|S_NOCMTIME;
286 inode->i_mode = fattr->mode;
287 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
288 && nfs_server_capable(inode, NFS_CAP_MODE))
289 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
290 | NFS_INO_INVALID_ACCESS
291 | NFS_INO_INVALID_ACL;
292 /* Why so? Because we want revalidate for devices/FIFOs, and
293 * that's precisely what we have in nfs_file_inode_operations.
295 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
296 if (S_ISREG(inode->i_mode)) {
297 inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
298 inode->i_data.a_ops = &nfs_file_aops;
299 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
300 } else if (S_ISDIR(inode->i_mode)) {
301 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
302 inode->i_fop = &nfs_dir_operations;
303 inode->i_data.a_ops = &nfs_dir_aops;
304 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS))
305 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
306 /* Deal with crossing mountpoints */
307 if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
308 fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
309 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
310 inode->i_op = &nfs_referral_inode_operations;
312 inode->i_op = &nfs_mountpoint_inode_operations;
314 inode->i_flags |= S_AUTOMOUNT;
316 } else if (S_ISLNK(inode->i_mode))
317 inode->i_op = &nfs_symlink_inode_operations;
319 init_special_inode(inode, inode->i_mode, fattr->rdev);
321 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
322 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
323 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
324 inode->i_version = 0;
330 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
332 nfsi->read_cache_jiffies = fattr->time_start;
333 nfsi->attr_gencount = fattr->gencount;
334 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
335 inode->i_atime = fattr->atime;
336 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
337 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
338 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
339 inode->i_mtime = fattr->mtime;
340 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
341 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
342 | NFS_INO_INVALID_DATA;
343 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
344 inode->i_ctime = fattr->ctime;
345 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
346 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
347 | NFS_INO_INVALID_ACCESS
348 | NFS_INO_INVALID_ACL;
349 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
350 inode->i_version = fattr->change_attr;
351 else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
352 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
353 | NFS_INO_INVALID_DATA;
354 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
355 inode->i_size = nfs_size_to_loff_t(fattr->size);
357 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
358 | NFS_INO_INVALID_DATA
359 | NFS_INO_REVAL_PAGECACHE;
360 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
361 set_nlink(inode, fattr->nlink);
362 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
363 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
364 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
365 inode->i_uid = fattr->uid;
366 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
367 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
368 | NFS_INO_INVALID_ACCESS
369 | NFS_INO_INVALID_ACL;
370 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
371 inode->i_gid = fattr->gid;
372 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
373 nfsi->cache_validity |= NFS_INO_INVALID_ATTR
374 | NFS_INO_INVALID_ACCESS
375 | NFS_INO_INVALID_ACL;
376 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
377 inode->i_blocks = fattr->du.nfs2.blocks;
378 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
380 * report the blocks in 512byte units
382 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
384 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
385 nfsi->attrtimeo_timestamp = now;
386 nfsi->access_cache = RB_ROOT;
388 nfs_fscache_init_inode_cookie(inode);
390 unlock_new_inode(inode);
392 nfs_refresh_inode(inode, fattr);
393 dprintk("NFS: nfs_fhget(%s/%Ld fh_crc=0x%08x ct=%d)\n",
395 (long long)NFS_FILEID(inode),
396 nfs_display_fhandle_hash(fh),
397 atomic_read(&inode->i_count));
403 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
407 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
410 nfs_setattr(struct dentry *dentry, struct iattr *attr)
412 struct inode *inode = dentry->d_inode;
413 struct nfs_fattr *fattr;
416 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
418 /* skip mode change if it's just for clearing setuid/setgid */
419 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
420 attr->ia_valid &= ~ATTR_MODE;
422 if (attr->ia_valid & ATTR_SIZE) {
423 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
424 attr->ia_valid &= ~ATTR_SIZE;
427 /* Optimization: if the end result is no change, don't RPC */
428 attr->ia_valid &= NFS_VALID_ATTRS;
429 if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
432 /* Write all dirty data */
433 if (S_ISREG(inode->i_mode))
436 fattr = nfs_alloc_fattr();
440 * Return any delegations if we're going to change ACLs
442 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
443 nfs_inode_return_delegation(inode);
444 error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
446 nfs_refresh_inode(inode, fattr);
447 nfs_free_fattr(fattr);
453 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
454 * @inode: inode of the file used
455 * @offset: file offset to start truncating
457 * This is a copy of the common vmtruncate, but with the locking
458 * corrected to take into account the fact that NFS requires
459 * inode->i_size to be updated under the inode->i_lock.
461 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
466 err = inode_newsize_ok(inode, offset);
470 spin_lock(&inode->i_lock);
471 oldsize = inode->i_size;
472 i_size_write(inode, offset);
473 spin_unlock(&inode->i_lock);
475 truncate_pagecache(inode, oldsize, offset);
481 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
482 * @inode: pointer to struct inode
483 * @attr: pointer to struct iattr
485 * Note: we do this in the *proc.c in order to ensure that
486 * it works for things like exclusive creates too.
488 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
490 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
491 spin_lock(&inode->i_lock);
492 if ((attr->ia_valid & ATTR_MODE) != 0) {
493 int mode = attr->ia_mode & S_IALLUGO;
494 mode |= inode->i_mode & ~S_IALLUGO;
495 inode->i_mode = mode;
497 if ((attr->ia_valid & ATTR_UID) != 0)
498 inode->i_uid = attr->ia_uid;
499 if ((attr->ia_valid & ATTR_GID) != 0)
500 inode->i_gid = attr->ia_gid;
501 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
502 spin_unlock(&inode->i_lock);
504 if ((attr->ia_valid & ATTR_SIZE) != 0) {
505 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
506 nfs_vmtruncate(inode, attr->ia_size);
510 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
512 struct inode *inode = dentry->d_inode;
513 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
516 /* Flush out writes to the server in order to update c/mtime. */
517 if (S_ISREG(inode->i_mode)) {
518 err = filemap_write_and_wait(inode->i_mapping);
524 * We may force a getattr if the user cares about atime.
526 * Note that we only have to check the vfsmount flags here:
527 * - NFS always sets S_NOATIME by so checking it would give a
529 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
530 * no point in checking those.
532 if ((mnt->mnt_flags & MNT_NOATIME) ||
533 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
537 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
539 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
541 generic_fillattr(inode, stat);
542 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
548 static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
550 atomic_set(&l_ctx->count, 1);
551 l_ctx->lockowner = current->files;
552 l_ctx->pid = current->tgid;
553 INIT_LIST_HEAD(&l_ctx->list);
556 static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
558 struct nfs_lock_context *pos;
560 list_for_each_entry(pos, &ctx->lock_context.list, list) {
561 if (pos->lockowner != current->files)
563 if (pos->pid != current->tgid)
565 atomic_inc(&pos->count);
571 struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
573 struct nfs_lock_context *res, *new = NULL;
574 struct inode *inode = ctx->dentry->d_inode;
576 spin_lock(&inode->i_lock);
577 res = __nfs_find_lock_context(ctx);
579 spin_unlock(&inode->i_lock);
580 new = kmalloc(sizeof(*new), GFP_KERNEL);
583 nfs_init_lock_context(new);
584 spin_lock(&inode->i_lock);
585 res = __nfs_find_lock_context(ctx);
587 list_add_tail(&new->list, &ctx->lock_context.list);
588 new->open_context = ctx;
593 spin_unlock(&inode->i_lock);
598 void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
600 struct nfs_open_context *ctx = l_ctx->open_context;
601 struct inode *inode = ctx->dentry->d_inode;
603 if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
605 list_del(&l_ctx->list);
606 spin_unlock(&inode->i_lock);
611 * nfs_close_context - Common close_context() routine NFSv2/v3
612 * @ctx: pointer to context
613 * @is_sync: is this a synchronous close
615 * always ensure that the attributes are up to date if we're mounted
616 * with close-to-open semantics
618 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
621 struct nfs_server *server;
623 if (!(ctx->mode & FMODE_WRITE))
627 inode = ctx->dentry->d_inode;
628 if (!list_empty(&NFS_I(inode)->open_files))
630 server = NFS_SERVER(inode);
631 if (server->flags & NFS_MOUNT_NOCTO)
633 nfs_revalidate_inode(server, inode);
636 struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode)
638 struct nfs_open_context *ctx;
639 struct rpc_cred *cred = rpc_lookup_cred();
641 return ERR_CAST(cred);
643 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
646 return ERR_PTR(-ENOMEM);
648 nfs_sb_active(dentry->d_sb);
649 ctx->dentry = dget(dentry);
655 nfs_init_lock_context(&ctx->lock_context);
656 ctx->lock_context.open_context = ctx;
657 INIT_LIST_HEAD(&ctx->list);
661 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
664 atomic_inc(&ctx->lock_context.count);
668 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
670 struct inode *inode = ctx->dentry->d_inode;
671 struct super_block *sb = ctx->dentry->d_sb;
673 if (!list_empty(&ctx->list)) {
674 if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
676 list_del(&ctx->list);
677 spin_unlock(&inode->i_lock);
678 } else if (!atomic_dec_and_test(&ctx->lock_context.count))
681 NFS_PROTO(inode)->close_context(ctx, is_sync);
682 if (ctx->cred != NULL)
683 put_rpccred(ctx->cred);
689 void put_nfs_open_context(struct nfs_open_context *ctx)
691 __put_nfs_open_context(ctx, 0);
695 * Ensure that mmap has a recent RPC credential for use when writing out
698 void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
700 struct inode *inode = filp->f_path.dentry->d_inode;
701 struct nfs_inode *nfsi = NFS_I(inode);
703 filp->private_data = get_nfs_open_context(ctx);
704 spin_lock(&inode->i_lock);
705 list_add(&ctx->list, &nfsi->open_files);
706 spin_unlock(&inode->i_lock);
710 * Given an inode, search for an open context with the desired characteristics
712 struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
714 struct nfs_inode *nfsi = NFS_I(inode);
715 struct nfs_open_context *pos, *ctx = NULL;
717 spin_lock(&inode->i_lock);
718 list_for_each_entry(pos, &nfsi->open_files, list) {
719 if (cred != NULL && pos->cred != cred)
721 if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
723 ctx = get_nfs_open_context(pos);
726 spin_unlock(&inode->i_lock);
730 static void nfs_file_clear_open_context(struct file *filp)
732 struct inode *inode = filp->f_path.dentry->d_inode;
733 struct nfs_open_context *ctx = nfs_file_open_context(filp);
736 filp->private_data = NULL;
737 spin_lock(&inode->i_lock);
738 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
739 spin_unlock(&inode->i_lock);
740 __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
745 * These allocate and release file read/write context information.
747 int nfs_open(struct inode *inode, struct file *filp)
749 struct nfs_open_context *ctx;
751 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
754 nfs_file_set_open_context(filp, ctx);
755 put_nfs_open_context(ctx);
756 nfs_fscache_set_inode_cookie(inode, filp);
760 int nfs_release(struct inode *inode, struct file *filp)
762 nfs_file_clear_open_context(filp);
767 * This function is called whenever some part of NFS notices that
768 * the cached attributes have to be refreshed.
771 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
773 int status = -ESTALE;
774 struct nfs_fattr *fattr = NULL;
775 struct nfs_inode *nfsi = NFS_I(inode);
777 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
778 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
780 if (is_bad_inode(inode))
782 if (NFS_STALE(inode))
786 fattr = nfs_alloc_fattr();
790 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
791 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
793 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
795 (long long)NFS_FILEID(inode), status);
796 if (status == -ESTALE) {
797 nfs_zap_caches(inode);
798 if (!S_ISDIR(inode->i_mode))
799 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
804 status = nfs_refresh_inode(inode, fattr);
806 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
808 (long long)NFS_FILEID(inode), status);
812 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
813 nfs_zap_acl_cache(inode);
815 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
817 (long long)NFS_FILEID(inode));
820 nfs_free_fattr(fattr);
824 int nfs_attribute_timeout(struct inode *inode)
826 struct nfs_inode *nfsi = NFS_I(inode);
828 return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
831 static int nfs_attribute_cache_expired(struct inode *inode)
833 if (nfs_have_delegated_attributes(inode))
835 return nfs_attribute_timeout(inode);
839 * nfs_revalidate_inode - Revalidate the inode attributes
840 * @server - pointer to nfs_server struct
841 * @inode - pointer to inode struct
843 * Updates inode attribute information by retrieving the data from the server.
845 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
847 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
848 && !nfs_attribute_cache_expired(inode))
849 return NFS_STALE(inode) ? -ESTALE : 0;
850 return __nfs_revalidate_inode(server, inode);
853 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
855 struct nfs_inode *nfsi = NFS_I(inode);
857 if (mapping->nrpages != 0) {
858 int ret = invalidate_inode_pages2(mapping);
862 spin_lock(&inode->i_lock);
863 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
864 if (S_ISDIR(inode->i_mode))
865 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
866 spin_unlock(&inode->i_lock);
867 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
868 nfs_fscache_reset_inode_cookie(inode);
869 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
870 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
875 * nfs_revalidate_mapping - Revalidate the pagecache
876 * @inode - pointer to host inode
877 * @mapping - pointer to mapping
879 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
881 struct nfs_inode *nfsi = NFS_I(inode);
884 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
885 || nfs_attribute_cache_expired(inode)
886 || NFS_STALE(inode)) {
887 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
891 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
892 ret = nfs_invalidate_mapping(inode, mapping);
897 static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
899 struct nfs_inode *nfsi = NFS_I(inode);
900 unsigned long ret = 0;
902 if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
903 && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
904 && inode->i_version == fattr->pre_change_attr) {
905 inode->i_version = fattr->change_attr;
906 if (S_ISDIR(inode->i_mode))
907 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
908 ret |= NFS_INO_INVALID_ATTR;
910 /* If we have atomic WCC data, we may update some attributes */
911 if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
912 && (fattr->valid & NFS_ATTR_FATTR_CTIME)
913 && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
914 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
915 ret |= NFS_INO_INVALID_ATTR;
918 if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
919 && (fattr->valid & NFS_ATTR_FATTR_MTIME)
920 && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
921 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
922 if (S_ISDIR(inode->i_mode))
923 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
924 ret |= NFS_INO_INVALID_ATTR;
926 if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
927 && (fattr->valid & NFS_ATTR_FATTR_SIZE)
928 && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
929 && nfsi->npages == 0) {
930 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
931 ret |= NFS_INO_INVALID_ATTR;
937 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
938 * @inode - pointer to inode
939 * @fattr - updated attributes
941 * Verifies the attribute cache. If we have just changed the attributes,
942 * so that fattr carries weak cache consistency data, then it may
943 * also update the ctime/mtime/change_attribute.
945 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
947 struct nfs_inode *nfsi = NFS_I(inode);
948 loff_t cur_size, new_isize;
949 unsigned long invalid = 0;
952 /* Has the inode gone and changed behind our back? */
953 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
955 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
958 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
959 inode->i_version != fattr->change_attr)
960 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
962 /* Verify a few of the more important attributes */
963 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
964 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
966 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
967 cur_size = i_size_read(inode);
968 new_isize = nfs_size_to_loff_t(fattr->size);
969 if (cur_size != new_isize && nfsi->npages == 0)
970 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
973 /* Have any file permissions changed? */
974 if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
975 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
976 if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
977 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
978 if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
979 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
981 /* Has the link count changed? */
982 if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
983 invalid |= NFS_INO_INVALID_ATTR;
985 if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
986 invalid |= NFS_INO_INVALID_ATIME;
989 nfsi->cache_validity |= invalid;
991 nfsi->read_cache_jiffies = fattr->time_start;
995 static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
997 if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
999 return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
1002 static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1004 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
1006 return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
1009 static atomic_long_t nfs_attr_generation_counter;
1011 static unsigned long nfs_read_attr_generation_counter(void)
1013 return atomic_long_read(&nfs_attr_generation_counter);
1016 unsigned long nfs_inc_attr_generation_counter(void)
1018 return atomic_long_inc_return(&nfs_attr_generation_counter);
1021 void nfs_fattr_init(struct nfs_fattr *fattr)
1024 fattr->time_start = jiffies;
1025 fattr->gencount = nfs_inc_attr_generation_counter();
1026 fattr->owner_name = NULL;
1027 fattr->group_name = NULL;
1030 struct nfs_fattr *nfs_alloc_fattr(void)
1032 struct nfs_fattr *fattr;
1034 fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
1036 nfs_fattr_init(fattr);
1040 struct nfs_fh *nfs_alloc_fhandle(void)
1044 fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
1052 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1053 * in the same way that wireshark does
1057 * For debugging only.
1059 u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
1061 /* wireshark uses 32-bit AUTODIN crc and does a bitwise
1062 * not on the result */
1063 return ~crc32(0xFFFFFFFF, &fh->data[0], fh->size);
1067 * _nfs_display_fhandle - display an NFS file handle on the console
1069 * @fh: file handle to display
1070 * @caption: display caption
1072 * For debugging only.
1074 void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
1078 if (fh == NULL || fh->size == 0) {
1079 printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
1083 printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
1084 caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
1085 for (i = 0; i < fh->size; i += 16) {
1086 __be32 *pos = (__be32 *)&fh->data[i];
1088 switch ((fh->size - i - 1) >> 2) {
1090 printk(KERN_DEFAULT " %08x\n",
1094 printk(KERN_DEFAULT " %08x %08x\n",
1095 be32_to_cpup(pos), be32_to_cpup(pos + 1));
1098 printk(KERN_DEFAULT " %08x %08x %08x\n",
1099 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1100 be32_to_cpup(pos + 2));
1103 printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
1104 be32_to_cpup(pos), be32_to_cpup(pos + 1),
1105 be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
1112 * nfs_inode_attrs_need_update - check if the inode attributes need updating
1113 * @inode - pointer to inode
1114 * @fattr - attributes
1116 * Attempt to divine whether or not an RPC call reply carrying stale
1117 * attributes got scheduled after another call carrying updated ones.
1119 * To do so, the function first assumes that a more recent ctime means
1120 * that the attributes in fattr are newer, however it also attempt to
1121 * catch the case where ctime either didn't change, or went backwards
1122 * (if someone reset the clock on the server) by looking at whether
1123 * or not this RPC call was started after the inode was last updated.
1124 * Note also the check for wraparound of 'attr_gencount'
1126 * The function returns 'true' if it thinks the attributes in 'fattr' are
1127 * more recent than the ones cached in the inode.
1130 static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1132 const struct nfs_inode *nfsi = NFS_I(inode);
1134 return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
1135 nfs_ctime_need_update(inode, fattr) ||
1136 nfs_size_need_update(inode, fattr) ||
1137 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
1140 static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1142 if (nfs_inode_attrs_need_update(inode, fattr))
1143 return nfs_update_inode(inode, fattr);
1144 return nfs_check_inode_attributes(inode, fattr);
1148 * nfs_refresh_inode - try to update the inode attribute cache
1149 * @inode - pointer to inode
1150 * @fattr - updated attributes
1152 * Check that an RPC call that returned attributes has not overlapped with
1153 * other recent updates of the inode metadata, then decide whether it is
1154 * safe to do a full update of the inode attributes, or whether just to
1155 * call nfs_check_inode_attributes.
1157 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1161 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1163 spin_lock(&inode->i_lock);
1164 status = nfs_refresh_inode_locked(inode, fattr);
1165 spin_unlock(&inode->i_lock);
1170 static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1172 struct nfs_inode *nfsi = NFS_I(inode);
1174 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1175 if (S_ISDIR(inode->i_mode))
1176 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1177 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1179 return nfs_refresh_inode_locked(inode, fattr);
1183 * nfs_post_op_update_inode - try to update the inode attribute cache
1184 * @inode - pointer to inode
1185 * @fattr - updated attributes
1187 * After an operation that has changed the inode metadata, mark the
1188 * attribute cache as being invalid, then try to update it.
1190 * NB: if the server didn't return any post op attributes, this
1191 * function will force the retrieval of attributes before the next
1192 * NFS request. Thus it should be used only for operations that
1193 * are expected to change one or more attributes, to avoid
1194 * unnecessary NFS requests and trips through nfs_update_inode().
1196 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1200 spin_lock(&inode->i_lock);
1201 status = nfs_post_op_update_inode_locked(inode, fattr);
1202 spin_unlock(&inode->i_lock);
1207 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1208 * @inode - pointer to inode
1209 * @fattr - updated attributes
1211 * After an operation that has changed the inode metadata, mark the
1212 * attribute cache as being invalid, then try to update it. Fake up
1213 * weak cache consistency data, if none exist.
1215 * This function is mainly designed to be used by the ->write_done() functions.
1217 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1221 spin_lock(&inode->i_lock);
1222 /* Don't do a WCC update if these attributes are already stale */
1223 if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1224 !nfs_inode_attrs_need_update(inode, fattr)) {
1225 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1226 | NFS_ATTR_FATTR_PRESIZE
1227 | NFS_ATTR_FATTR_PREMTIME
1228 | NFS_ATTR_FATTR_PRECTIME);
1231 if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1232 (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1233 fattr->pre_change_attr = inode->i_version;
1234 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1236 if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1237 (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
1238 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
1239 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1241 if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1242 (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
1243 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
1244 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1246 if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1247 (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1248 fattr->pre_size = i_size_read(inode);
1249 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1252 status = nfs_post_op_update_inode_locked(inode, fattr);
1253 spin_unlock(&inode->i_lock);
1258 * Many nfs protocol calls return the new file attributes after
1259 * an operation. Here we update the inode to reflect the state
1260 * of the server's inode.
1262 * This is a bit tricky because we have to make sure all dirty pages
1263 * have been sent off to the server before calling invalidate_inode_pages.
1264 * To make sure no other process adds more write requests while we try
1265 * our best to flush them, we make them sleep during the attribute refresh.
1267 * A very similar scenario holds for the dir cache.
1269 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1271 struct nfs_server *server;
1272 struct nfs_inode *nfsi = NFS_I(inode);
1273 loff_t cur_isize, new_isize;
1274 unsigned long invalid = 0;
1275 unsigned long now = jiffies;
1276 unsigned long save_cache_validity;
1278 dfprintk(VFS, "NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
1279 __func__, inode->i_sb->s_id, inode->i_ino,
1280 nfs_display_fhandle_hash(NFS_FH(inode)),
1281 atomic_read(&inode->i_count), fattr->valid);
1283 if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
1287 * Make sure the inode's type hasn't changed.
1289 if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1292 server = NFS_SERVER(inode);
1293 /* Update the fsid? */
1294 if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
1295 !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1296 !IS_AUTOMOUNT(inode))
1297 server->fsid = fattr->fsid;
1300 * Update the read time so we don't revalidate too often.
1302 nfsi->read_cache_jiffies = fattr->time_start;
1304 save_cache_validity = nfsi->cache_validity;
1305 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1306 | NFS_INO_INVALID_ATIME
1307 | NFS_INO_REVAL_FORCED
1308 | NFS_INO_REVAL_PAGECACHE);
1310 /* Do atomic weak cache consistency updates */
1311 invalid |= nfs_wcc_update_inode(inode, fattr);
1313 /* More cache consistency checks */
1314 if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1315 if (inode->i_version != fattr->change_attr) {
1316 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1317 inode->i_sb->s_id, inode->i_ino);
1318 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1319 if (S_ISDIR(inode->i_mode))
1320 nfs_force_lookup_revalidate(inode);
1321 inode->i_version = fattr->change_attr;
1323 } else if (server->caps & NFS_CAP_CHANGE_ATTR)
1324 invalid |= save_cache_validity;
1326 if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
1327 /* NFSv2/v3: Check if the mtime agrees */
1328 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1329 dprintk("NFS: mtime change on server for file %s/%ld\n",
1330 inode->i_sb->s_id, inode->i_ino);
1331 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1332 if (S_ISDIR(inode->i_mode))
1333 nfs_force_lookup_revalidate(inode);
1334 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1336 } else if (server->caps & NFS_CAP_MTIME)
1337 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1338 | NFS_INO_INVALID_DATA
1339 | NFS_INO_REVAL_PAGECACHE
1340 | NFS_INO_REVAL_FORCED);
1342 if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
1343 /* If ctime has changed we should definitely clear access+acl caches */
1344 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
1345 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1346 /* and probably clear data for a directory too as utimes can cause
1347 * havoc with our cache.
1349 if (S_ISDIR(inode->i_mode)) {
1350 invalid |= NFS_INO_INVALID_DATA;
1351 nfs_force_lookup_revalidate(inode);
1353 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1355 } else if (server->caps & NFS_CAP_CTIME)
1356 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1357 | NFS_INO_INVALID_ACCESS
1358 | NFS_INO_INVALID_ACL
1359 | NFS_INO_REVAL_FORCED);
1361 /* Check if our cached file size is stale */
1362 if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1363 new_isize = nfs_size_to_loff_t(fattr->size);
1364 cur_isize = i_size_read(inode);
1365 if (new_isize != cur_isize) {
1366 /* Do we perhaps have any outstanding writes, or has
1367 * the file grown beyond our last write? */
1368 if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
1369 new_isize > cur_isize) {
1370 i_size_write(inode, new_isize);
1371 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1373 dprintk("NFS: isize change on server for file %s/%ld "
1377 (long long)cur_isize,
1378 (long long)new_isize);
1381 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1382 | NFS_INO_REVAL_PAGECACHE
1383 | NFS_INO_REVAL_FORCED);
1386 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1387 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1388 else if (server->caps & NFS_CAP_ATIME)
1389 invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
1390 | NFS_INO_REVAL_FORCED);
1392 if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1393 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1394 umode_t newmode = inode->i_mode & S_IFMT;
1395 newmode |= fattr->mode & S_IALLUGO;
1396 inode->i_mode = newmode;
1397 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1399 } else if (server->caps & NFS_CAP_MODE)
1400 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1401 | NFS_INO_INVALID_ACCESS
1402 | NFS_INO_INVALID_ACL
1403 | NFS_INO_REVAL_FORCED);
1405 if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
1406 if (inode->i_uid != fattr->uid) {
1407 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1408 inode->i_uid = fattr->uid;
1410 } else if (server->caps & NFS_CAP_OWNER)
1411 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1412 | NFS_INO_INVALID_ACCESS
1413 | NFS_INO_INVALID_ACL
1414 | NFS_INO_REVAL_FORCED);
1416 if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
1417 if (inode->i_gid != fattr->gid) {
1418 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1419 inode->i_gid = fattr->gid;
1421 } else if (server->caps & NFS_CAP_OWNER_GROUP)
1422 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1423 | NFS_INO_INVALID_ACCESS
1424 | NFS_INO_INVALID_ACL
1425 | NFS_INO_REVAL_FORCED);
1427 if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1428 if (inode->i_nlink != fattr->nlink) {
1429 invalid |= NFS_INO_INVALID_ATTR;
1430 if (S_ISDIR(inode->i_mode))
1431 invalid |= NFS_INO_INVALID_DATA;
1432 set_nlink(inode, fattr->nlink);
1434 } else if (server->caps & NFS_CAP_NLINK)
1435 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1436 | NFS_INO_REVAL_FORCED);
1438 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1440 * report the blocks in 512byte units
1442 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1444 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1445 inode->i_blocks = fattr->du.nfs2.blocks;
1447 /* Update attrtimeo value if we're out of the unstable period */
1448 if (invalid & NFS_INO_INVALID_ATTR) {
1449 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1450 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1451 nfsi->attrtimeo_timestamp = now;
1452 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1454 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
1455 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1456 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1457 nfsi->attrtimeo_timestamp = now;
1460 invalid &= ~NFS_INO_INVALID_ATTR;
1461 /* Don't invalidate the data if we were to blame */
1462 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1463 || S_ISLNK(inode->i_mode)))
1464 invalid &= ~NFS_INO_INVALID_DATA;
1465 if (!nfs_have_delegation(inode, FMODE_READ) ||
1466 (save_cache_validity & NFS_INO_REVAL_FORCED))
1467 nfsi->cache_validity |= invalid;
1472 * Big trouble! The inode has become a different object.
1474 printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
1475 __func__, inode->i_ino, inode->i_mode, fattr->mode);
1478 * No need to worry about unhashing the dentry, as the
1479 * lookup validation will know that the inode is bad.
1480 * (But we fall through to invalidate the caches.)
1482 nfs_invalidate_inode(inode);
1486 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1487 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1488 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
1489 (long long)nfsi->fileid, (long long)fattr->fileid);
1494 #ifdef CONFIG_NFS_V4
1497 * Clean out any remaining NFSv4 state that might be left over due
1498 * to open() calls that passed nfs_atomic_lookup, but failed to call
1501 void nfs4_evict_inode(struct inode *inode)
1503 truncate_inode_pages(&inode->i_data, 0);
1504 end_writeback(inode);
1505 pnfs_return_layout(inode);
1506 pnfs_destroy_layout(NFS_I(inode));
1507 /* If we are holding a delegation, return it! */
1508 nfs_inode_return_delegation_noreclaim(inode);
1509 /* First call standard NFS clear_inode() code */
1510 nfs_clear_inode(inode);
1514 struct inode *nfs_alloc_inode(struct super_block *sb)
1516 struct nfs_inode *nfsi;
1517 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
1521 nfsi->cache_validity = 0UL;
1522 #ifdef CONFIG_NFS_V3_ACL
1523 nfsi->acl_access = ERR_PTR(-EAGAIN);
1524 nfsi->acl_default = ERR_PTR(-EAGAIN);
1526 #ifdef CONFIG_NFS_V4
1527 nfsi->nfs4_acl = NULL;
1528 #endif /* CONFIG_NFS_V4 */
1529 return &nfsi->vfs_inode;
1532 static void nfs_i_callback(struct rcu_head *head)
1534 struct inode *inode = container_of(head, struct inode, i_rcu);
1535 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1538 void nfs_destroy_inode(struct inode *inode)
1540 call_rcu(&inode->i_rcu, nfs_i_callback);
1543 static inline void nfs4_init_once(struct nfs_inode *nfsi)
1545 #ifdef CONFIG_NFS_V4
1546 INIT_LIST_HEAD(&nfsi->open_states);
1547 nfsi->delegation = NULL;
1548 nfsi->delegation_state = 0;
1549 init_rwsem(&nfsi->rwsem);
1550 nfsi->layout = NULL;
1551 atomic_set(&nfsi->commits_outstanding, 0);
1555 static void init_once(void *foo)
1557 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1559 inode_init_once(&nfsi->vfs_inode);
1560 INIT_LIST_HEAD(&nfsi->open_files);
1561 INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1562 INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
1563 INIT_LIST_HEAD(&nfsi->commit_list);
1566 atomic_set(&nfsi->silly_count, 1);
1567 INIT_HLIST_HEAD(&nfsi->silly_list);
1568 init_waitqueue_head(&nfsi->waitqueue);
1569 nfs4_init_once(nfsi);
1572 static int __init nfs_init_inodecache(void)
1574 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1575 sizeof(struct nfs_inode),
1576 0, (SLAB_RECLAIM_ACCOUNT|
1579 if (nfs_inode_cachep == NULL)
1585 static void nfs_destroy_inodecache(void)
1587 kmem_cache_destroy(nfs_inode_cachep);
1590 struct workqueue_struct *nfsiod_workqueue;
1593 * start up the nfsiod workqueue
1595 static int nfsiod_start(void)
1597 struct workqueue_struct *wq;
1598 dprintk("RPC: creating workqueue nfsiod\n");
1599 wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
1602 nfsiod_workqueue = wq;
1607 * Destroy the nfsiod workqueue
1609 static void nfsiod_stop(void)
1611 struct workqueue_struct *wq;
1613 wq = nfsiod_workqueue;
1616 nfsiod_workqueue = NULL;
1617 destroy_workqueue(wq);
1621 EXPORT_SYMBOL_GPL(nfs_net_id);
1623 static int nfs_net_init(struct net *net)
1625 nfs_clients_init(net);
1626 return nfs_dns_resolver_cache_init(net);
1629 static void nfs_net_exit(struct net *net)
1631 nfs_dns_resolver_cache_destroy(net);
1632 nfs_cleanup_cb_ident_idr(net);
1635 static struct pernet_operations nfs_net_ops = {
1636 .init = nfs_net_init,
1637 .exit = nfs_net_exit,
1639 .size = sizeof(struct nfs_net),
1645 static int __init init_nfs_fs(void)
1649 err = nfs_idmap_init();
1653 err = nfs_dns_resolver_init();
1657 err = register_pernet_subsys(&nfs_net_ops);
1661 err = nfs_fscache_register();
1665 err = nfsiod_start();
1669 err = nfs_fs_proc_init();
1673 err = nfs_init_nfspagecache();
1677 err = nfs_init_inodecache();
1681 err = nfs_init_readpagecache();
1685 err = nfs_init_writepagecache();
1689 err = nfs_init_directcache();
1693 #ifdef CONFIG_PROC_FS
1694 rpc_proc_register(&init_net, &nfs_rpcstat);
1696 if ((err = register_nfs_fs()) != 0)
1700 #ifdef CONFIG_PROC_FS
1701 rpc_proc_unregister(&init_net, "nfs");
1703 nfs_destroy_directcache();
1705 nfs_destroy_writepagecache();
1707 nfs_destroy_readpagecache();
1709 nfs_destroy_inodecache();
1711 nfs_destroy_nfspagecache();
1717 nfs_fscache_unregister();
1719 unregister_pernet_subsys(&nfs_net_ops);
1721 nfs_dns_resolver_destroy();
1728 static void __exit exit_nfs_fs(void)
1730 nfs_destroy_directcache();
1731 nfs_destroy_writepagecache();
1732 nfs_destroy_readpagecache();
1733 nfs_destroy_inodecache();
1734 nfs_destroy_nfspagecache();
1735 nfs_fscache_unregister();
1736 unregister_pernet_subsys(&nfs_net_ops);
1737 nfs_dns_resolver_destroy();
1739 #ifdef CONFIG_PROC_FS
1740 rpc_proc_unregister(&init_net, "nfs");
1742 unregister_nfs_fs();
1747 /* Not quite true; I just maintain it */
1748 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1749 MODULE_LICENSE("GPL");
1750 module_param(enable_ino64, bool, 0644);
1752 module_init(init_nfs_fs)
1753 module_exit(exit_nfs_fs)