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/config.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/errno.h>
26 #include <linux/locks.h>
27 #include <linux/unistd.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/sunrpc/stats.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_mount.h>
32 #include <linux/nfs_flushd.h>
33 #include <linux/lockd/bind.h>
34 #include <linux/smp_lock.h>
35 #include <linux/seq_file.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
40 #define CONFIG_NFS_SNAPSHOT 1
41 #define NFSDBG_FACILITY NFSDBG_VFS
42 #define NFS_PARANOIA 1
44 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fh *, struct nfs_fattr *);
45 void nfs_zap_caches(struct inode *);
46 static void nfs_invalidate_inode(struct inode *);
48 static void nfs_read_inode(struct inode *);
49 static void nfs_write_inode(struct inode *,int);
50 static void nfs_delete_inode(struct inode *);
51 static void nfs_put_super(struct super_block *);
52 static void nfs_clear_inode(struct inode *);
53 static void nfs_umount_begin(struct super_block *);
54 static int nfs_statfs(struct super_block *, struct statfs *);
55 static int nfs_show_options(struct seq_file *, struct vfsmount *);
57 static struct super_operations nfs_sops = {
58 read_inode: nfs_read_inode,
59 write_inode: nfs_write_inode,
60 delete_inode: nfs_delete_inode,
61 put_super: nfs_put_super,
63 clear_inode: nfs_clear_inode,
64 umount_begin: nfs_umount_begin,
65 show_options: nfs_show_options,
71 struct rpc_stat nfs_rpcstat = { &nfs_program };
72 static struct rpc_version * nfs_version[] = {
81 struct rpc_program nfs_program = {
84 sizeof(nfs_version) / sizeof(nfs_version[0]),
89 static inline unsigned long
90 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
92 return nfs_fileid_to_ino_t(fattr->fileid);
96 * The "read_inode" function doesn't actually do anything:
97 * the real data is filled in later in nfs_fhget. Here we
98 * just mark the cache times invalid, and zero out i_mode
99 * (the latter makes "nfs_refresh_inode" do the right thing
103 nfs_read_inode(struct inode * inode)
105 inode->i_blksize = inode->i_sb->s_blocksize;
108 /* We can't support UPDATE_ATIME(), since the server will reset it */
109 inode->i_flags |= S_NOATIME;
110 INIT_LIST_HEAD(&inode->u.nfs_i.read);
111 INIT_LIST_HEAD(&inode->u.nfs_i.dirty);
112 INIT_LIST_HEAD(&inode->u.nfs_i.commit);
113 INIT_LIST_HEAD(&inode->u.nfs_i.writeback);
115 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
116 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
120 nfs_write_inode(struct inode *inode, int sync)
122 int flags = sync ? FLUSH_WAIT : 0;
124 nfs_sync_file(inode, 0, 0, flags);
128 nfs_delete_inode(struct inode * inode)
130 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
133 * The following can never actually happen...
135 if (nfs_have_writebacks(inode) || nfs_have_read(inode)) {
136 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
143 * For the moment, the only task for the NFS clear_inode method is to
144 * release the mmap credential
147 nfs_clear_inode(struct inode *inode)
149 struct rpc_cred *cred = NFS_I(inode)->mm_cred;
156 nfs_put_super(struct super_block *sb)
158 struct nfs_server *server = &sb->u.nfs_sb.s_server;
159 struct rpc_clnt *rpc;
162 * First get rid of the request flushing daemon.
163 * Relies on rpc_shutdown_client() waiting on all
164 * client tasks to finish.
166 nfs_reqlist_exit(server);
168 if ((rpc = server->client) != NULL)
169 rpc_shutdown_client(rpc);
171 nfs_reqlist_free(server);
173 if (!(server->flags & NFS_MOUNT_NONLM))
174 lockd_down(); /* release rpc.lockd */
175 rpciod_down(); /* release rpciod */
177 kfree(server->hostname);
181 nfs_umount_begin(struct super_block *sb)
183 struct nfs_server *server = &sb->u.nfs_sb.s_server;
184 struct rpc_clnt *rpc;
186 /* -EIO all pending I/O */
187 if ((rpc = server->client) != NULL)
188 rpc_killall_tasks(rpc);
192 static inline unsigned long
193 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
195 /* make sure blocksize is a power of two */
196 if ((bsize & (bsize - 1)) || nrbitsp) {
197 unsigned char nrbits;
199 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
210 * Calculate the number of 512byte blocks used.
212 static inline unsigned long
213 nfs_calc_block_size(u64 tsize)
215 loff_t used = (tsize + 511) >> 9;
216 return (used > ULONG_MAX) ? ULONG_MAX : used;
220 * Compute and set NFS server blocksize
222 static inline unsigned long
223 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
226 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
227 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
228 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
230 return nfs_block_bits(bsize, nrbitsp);
234 * Obtain the root inode of the file system.
236 static struct inode *
237 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh)
239 struct nfs_server *server = &sb->u.nfs_sb.s_server;
240 struct nfs_fattr fattr;
244 if ((error = server->rpc_ops->getroot(server, rootfh, &fattr)) < 0) {
245 printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error);
249 inode = __nfs_fhget(sb, rootfh, &fattr);
254 * The way this works is that the mount process passes a structure
255 * in the data argument which contains the server's IP address
256 * and the root file handle obtained from the server's mount
257 * daemon. We stash these away in the private superblock fields.
260 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
262 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
263 struct nfs_server *server;
264 struct rpc_xprt *xprt = NULL;
265 struct rpc_clnt *clnt = NULL;
266 struct nfs_fh *root = &data->root, fh;
267 struct inode *root_inode = NULL;
268 unsigned int authflavor;
269 struct sockaddr_in srvaddr;
270 struct rpc_timeout timeparms;
271 struct nfs_fsinfo fsinfo;
272 int tcp, version, maxlen;
274 memset(&sb->u.nfs_sb, 0, sizeof(sb->u.nfs_sb));
278 memset(&fh, 0, sizeof(fh));
279 if (data->version != NFS_MOUNT_VERSION) {
280 printk("nfs warning: mount version %s than kernel\n",
281 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
282 if (data->version < 2)
284 if (data->version < 3)
286 if (data->version < 4) {
287 data->flags &= ~NFS_MOUNT_VER3;
289 root->size = NFS2_FHSIZE;
290 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
294 /* We now require that the mount process passes the remote address */
295 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
296 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
299 sb->s_magic = NFS_SUPER_MAGIC;
300 sb->s_op = &nfs_sops;
301 sb->s_blocksize_bits = 0;
302 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
303 server = &sb->u.nfs_sb.s_server;
304 server->rsize = nfs_block_size(data->rsize, NULL);
305 server->wsize = nfs_block_size(data->wsize, NULL);
306 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
308 if (data->flags & NFS_MOUNT_NOAC) {
309 data->acregmin = data->acregmax = 0;
310 data->acdirmin = data->acdirmax = 0;
311 sb->s_flags |= MS_SYNCHRONOUS;
313 server->acregmin = data->acregmin*HZ;
314 server->acregmax = data->acregmax*HZ;
315 server->acdirmin = data->acdirmin*HZ;
316 server->acdirmax = data->acdirmax*HZ;
318 server->namelen = data->namlen;
319 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
320 if (!server->hostname)
322 strcpy(server->hostname, data->hostname);
323 INIT_LIST_HEAD(&server->lru_read);
324 INIT_LIST_HEAD(&server->lru_dirty);
325 INIT_LIST_HEAD(&server->lru_commit);
326 INIT_LIST_HEAD(&server->lru_busy);
329 /* Check NFS protocol revision and initialize RPC op vector
330 * and file handle pool. */
331 if (data->flags & NFS_MOUNT_VER3) {
333 server->rpc_ops = &nfs_v3_clientops;
335 if (data->version < 4) {
336 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
340 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
344 server->rpc_ops = &nfs_v2_clientops;
348 /* Which protocol do we use? */
349 tcp = (data->flags & NFS_MOUNT_TCP);
351 /* Initialize timeout values */
352 timeparms.to_initval = data->timeo * HZ / 10;
353 timeparms.to_retries = data->retrans;
354 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
355 timeparms.to_exponential = 1;
357 if (!timeparms.to_initval)
358 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
359 if (!timeparms.to_retries)
360 timeparms.to_retries = 5;
362 /* Now create transport and client */
363 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
364 &srvaddr, &timeparms);
368 /* Choose authentication flavor */
369 authflavor = RPC_AUTH_UNIX;
370 if (data->flags & NFS_MOUNT_SECURE)
371 authflavor = RPC_AUTH_DES;
372 else if (data->flags & NFS_MOUNT_KERBEROS)
373 authflavor = RPC_AUTH_KRB;
375 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
376 version, authflavor);
380 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
381 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
382 clnt->cl_droppriv = (data->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
384 server->client = clnt;
386 /* Fire up rpciod if not yet running */
387 if (rpciod_up() != 0)
391 * Keep the super block locked while we try to get
392 * the root fh attributes.
394 /* Did getting the root inode fail? */
395 if (!(root_inode = nfs_get_root(sb, root))
396 && (data->flags & NFS_MOUNT_VER3)) {
397 data->flags &= ~NFS_MOUNT_VER3;
399 rpc_shutdown_client(server->client);
400 goto nfsv3_try_again;
405 sb->s_root = d_alloc_root(root_inode);
409 sb->s_root->d_op = &nfs_dentry_operations;
411 /* Get some general file system info */
412 if (server->rpc_ops->statfs(server, root, &fsinfo) >= 0) {
413 if (server->namelen == 0)
414 server->namelen = fsinfo.namelen;
416 printk(KERN_NOTICE "NFS: cannot retrieve file system info.\n");
420 /* Work out a lot of parameters */
421 if (data->rsize == 0)
422 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
423 if (data->wsize == 0)
424 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
425 /* NFSv3: we don't have bsize, but rather rtmult and wtmult... */
427 fsinfo.bsize = (fsinfo.rtmult>fsinfo.wtmult) ? fsinfo.rtmult : fsinfo.wtmult;
428 /* Also make sure we don't go below rsize/wsize since
429 * RPC calls are expensive */
430 if (fsinfo.bsize < server->rsize)
431 fsinfo.bsize = server->rsize;
432 if (fsinfo.bsize < server->wsize)
433 fsinfo.bsize = server->wsize;
435 if (data->bsize == 0)
436 sb->s_blocksize = nfs_block_bits(fsinfo.bsize, &sb->s_blocksize_bits);
437 if (server->rsize > fsinfo.rtmax)
438 server->rsize = fsinfo.rtmax;
439 if (server->wsize > fsinfo.wtmax)
440 server->wsize = fsinfo.wtmax;
442 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
443 if (server->rpages > NFS_READ_MAXIOV) {
444 server->rpages = NFS_READ_MAXIOV;
445 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
448 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
449 if (server->wpages > NFS_WRITE_MAXIOV) {
450 server->wpages = NFS_WRITE_MAXIOV;
451 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
454 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
455 if (server->dtsize > PAGE_CACHE_SIZE)
456 server->dtsize = PAGE_CACHE_SIZE;
457 if (server->dtsize > server->rsize)
458 server->dtsize = server->rsize;
460 maxlen = (version == 2) ? NFS2_MAXNAMLEN : NFS3_MAXNAMLEN;
462 if (server->namelen == 0 || server->namelen > maxlen)
463 server->namelen = maxlen;
465 sb->s_maxbytes = fsinfo.maxfilesize;
466 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
467 sb->s_maxbytes = MAX_LFS_FILESIZE;
469 /* Fire up the writeback cache */
470 if (nfs_reqlist_alloc(server) < 0) {
471 printk(KERN_NOTICE "NFS: cannot initialize writeback cache.\n");
472 goto failure_kill_reqlist;
475 /* We're airborne Set socket buffersize */
476 rpc_setbufsize(clnt, server->wsize + 100, server->rsize + 100);
478 /* Check whether to start the lockd process */
479 if (!(server->flags & NFS_MOUNT_NONLM))
483 /* Yargs. It didn't work out. */
484 failure_kill_reqlist:
485 nfs_reqlist_exit(server);
487 printk("nfs_read_super: get root inode failed\n");
493 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
495 rpc_shutdown_client(server->client);
499 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
504 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
507 nfs_reqlist_free(server);
508 kfree(server->hostname);
513 printk("NFS: mount program didn't pass remote address!\n");
517 printk("nfs_read_super: missing data argument\n");
524 nfs_statfs(struct super_block *sb, struct statfs *buf)
526 struct nfs_server *server = &sb->u.nfs_sb.s_server;
527 unsigned char blockbits;
528 unsigned long blockres;
529 struct nfs_fsinfo res;
532 error = server->rpc_ops->statfs(server, NFS_FH(sb->s_root->d_inode), &res);
533 buf->f_type = NFS_SUPER_MAGIC;
538 res.bsize = sb->s_blocksize;
539 buf->f_bsize = nfs_block_bits(res.bsize, &blockbits);
540 blockres = (1 << blockbits) - 1;
541 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
542 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
543 buf->f_bavail = (res.abytes + blockres) >> blockbits;
544 buf->f_files = res.tfiles;
545 buf->f_ffree = res.afiles;
546 if (res.namelen == 0 || res.namelen > server->namelen)
547 res.namelen = server->namelen;
548 buf->f_namelen = res.namelen;
551 printk("nfs_statfs: statfs error = %d\n", -error);
552 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
556 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
558 static struct proc_nfs_info {
563 { NFS_MOUNT_SOFT, ",soft", ",hard" },
564 { NFS_MOUNT_INTR, ",intr", "" },
565 { NFS_MOUNT_POSIX, ",posix", "" },
566 { NFS_MOUNT_TCP, ",tcp", ",udp" },
567 { NFS_MOUNT_NOCTO, ",nocto", "" },
568 { NFS_MOUNT_NOAC, ",noac", "" },
569 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
570 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
573 struct proc_nfs_info *nfs_infop;
574 struct nfs_server *nfss = &mnt->mnt_sb->u.nfs_sb.s_server;
576 seq_printf(m, ",v%d", nfss->rpc_ops->version);
577 seq_printf(m, ",rsize=%d", nfss->rsize);
578 seq_printf(m, ",wsize=%d", nfss->wsize);
579 if (nfss->acregmin != 3*HZ)
580 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
581 if (nfss->acregmax != 60*HZ)
582 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
583 if (nfss->acdirmin != 30*HZ)
584 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
585 if (nfss->acdirmax != 60*HZ)
586 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
587 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
588 if (nfss->flags & nfs_infop->flag)
589 seq_puts(m, nfs_infop->str);
591 seq_puts(m, nfs_infop->nostr);
593 seq_puts(m, ",addr=");
594 seq_escape(m, nfss->hostname, " \t\n\\");
599 * Invalidate the local caches
602 nfs_zap_caches(struct inode *inode)
604 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
605 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
607 invalidate_inode_pages(inode);
609 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
614 * Invalidate, but do not unhash, the inode
617 nfs_invalidate_inode(struct inode *inode)
619 umode_t save_mode = inode->i_mode;
621 make_bad_inode(inode);
622 inode->i_mode = save_mode;
623 nfs_zap_caches(inode);
627 * Fill in inode information from the fattr.
630 nfs_fill_inode(struct inode *inode, struct nfs_fh *fh, struct nfs_fattr *fattr)
633 * Check whether the mode has been set, as we only want to
634 * do this once. (We don't allow inodes to change types.)
636 if (inode->i_mode == 0) {
637 NFS_FILEID(inode) = fattr->fileid;
638 inode->i_mode = fattr->mode;
639 /* Why so? Because we want revalidate for devices/FIFOs, and
640 * that's precisely what we have in nfs_file_inode_operations.
642 inode->i_op = &nfs_file_inode_operations;
643 if (S_ISREG(inode->i_mode)) {
644 inode->i_fop = &nfs_file_operations;
645 inode->i_data.a_ops = &nfs_file_aops;
646 } else if (S_ISDIR(inode->i_mode)) {
647 inode->i_op = &nfs_dir_inode_operations;
648 inode->i_fop = &nfs_dir_operations;
649 } else if (S_ISLNK(inode->i_mode))
650 inode->i_op = &nfs_symlink_inode_operations;
652 init_special_inode(inode, inode->i_mode, fattr->rdev);
653 memcpy(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh));
655 nfs_refresh_inode(inode, fattr);
658 struct nfs_find_desc {
660 struct nfs_fattr *fattr;
664 * In NFSv3 we can have 64bit inode numbers. In order to support
665 * this, and re-exported directories (also seen in NFSv2)
666 * we are forced to allow 2 different inodes to have the same
670 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
672 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
673 struct nfs_fh *fh = desc->fh;
674 struct nfs_fattr *fattr = desc->fattr;
676 if (NFS_FILEID(inode) != fattr->fileid)
678 if (memcmp(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh)) != 0)
680 if (is_bad_inode(inode))
682 /* Force an attribute cache update if inode->i_count == 0 */
683 if (!atomic_read(&inode->i_count))
689 * This is our own version of iget that looks up inodes by file handle
690 * instead of inode number. We use this technique instead of using
691 * the vfs read_inode function because there is no way to pass the
692 * file handle or current attributes into the read_inode function.
696 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
697 struct nfs_fattr *fattr)
699 struct super_block *sb = dentry->d_sb;
701 dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
702 dentry->d_parent->d_name.name, dentry->d_name.name,
703 (long long)fattr->fileid);
704 return __nfs_fhget(sb, fhandle, fattr);
708 * Look up the inode by super block and fattr->fileid.
710 static struct inode *
711 __nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
713 struct nfs_find_desc desc = { fh, fattr };
714 struct inode *inode = NULL;
717 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
721 printk("NFS: Buggy server - nlink == 0!\n");
725 ino = nfs_fattr_to_ino_t(fattr);
727 if (!(inode = iget4(sb, ino, nfs_find_actor, &desc)))
730 nfs_fill_inode(inode, fh, fattr);
731 dprintk("NFS: __nfs_fhget(%x/%Ld ct=%d)\n",
732 inode->i_dev, (long long)NFS_FILEID(inode),
733 atomic_read(&inode->i_count));
739 printk("__nfs_fhget: iget failed\n");
744 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
746 struct inode *inode = dentry->d_inode;
747 struct nfs_fattr fattr;
751 * Make sure the inode is up-to-date.
753 error = nfs_revalidate_inode(NFS_SERVER(inode),inode);
756 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
761 if (!S_ISREG(inode->i_mode))
762 attr->ia_valid &= ~ATTR_SIZE;
764 filemap_fdatasync(inode->i_mapping);
765 error = nfs_wb_all(inode);
766 filemap_fdatawait(inode->i_mapping);
770 error = NFS_PROTO(inode)->setattr(inode, &fattr, attr);
774 * If we changed the size or mtime, update the inode
775 * now to avoid invalidating the page cache.
777 if (attr->ia_valid & ATTR_SIZE) {
778 if (attr->ia_size != fattr.size)
779 printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
780 (long long) attr->ia_size, (long long)fattr.size);
781 vmtruncate(inode, attr->ia_size);
785 * If we changed the size or mtime, update the inode
786 * now to avoid invalidating the page cache.
788 if (!(fattr.valid & NFS_ATTR_WCC)) {
789 fattr.pre_size = NFS_CACHE_ISIZE(inode);
790 fattr.pre_mtime = NFS_CACHE_MTIME(inode);
791 fattr.pre_ctime = NFS_CACHE_CTIME(inode);
792 fattr.valid |= NFS_ATTR_WCC;
794 /* Force an attribute cache update */
796 error = nfs_refresh_inode(inode, &fattr);
802 * Wait for the inode to get unlocked.
803 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
806 nfs_wait_on_inode(struct inode *inode, int flag)
808 struct rpc_clnt *clnt = NFS_CLIENT(inode);
810 if (!(NFS_FLAGS(inode) & flag))
812 atomic_inc(&inode->i_count);
813 error = nfs_wait_event(clnt, inode->i_wait, !(NFS_FLAGS(inode) & flag));
819 * Externally visible revalidation function
822 nfs_revalidate(struct dentry *dentry)
824 struct inode *inode = dentry->d_inode;
825 return nfs_revalidate_inode(NFS_SERVER(inode), inode);
829 * Ensure that mmap has a recent RPC credential for use when writing out
833 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
835 struct rpc_cred **p = &NFS_I(inode)->mm_cred,
838 *p = get_rpccred(cred);
840 put_rpccred(oldcred);
844 * These are probably going to contain hooks for
845 * allocating and releasing RPC credentials for
846 * the file. I'll have to think about Tronds patch
849 int nfs_open(struct inode *inode, struct file *filp)
851 struct rpc_auth *auth;
852 struct rpc_cred *cred;
855 auth = NFS_CLIENT(inode)->cl_auth;
856 cred = rpcauth_lookupcred(auth, 0);
857 filp->private_data = cred;
858 if (filp->f_mode & FMODE_WRITE)
859 nfs_set_mmcred(inode, cred);
864 int nfs_release(struct inode *inode, struct file *filp)
866 struct rpc_cred *cred;
869 cred = nfs_file_cred(filp);
877 * This function is called whenever some part of NFS notices that
878 * the cached attributes have to be refreshed.
881 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
883 int status = -ESTALE;
884 struct nfs_fattr fattr;
886 dfprintk(PAGECACHE, "NFS: revalidating (%x/%Ld)\n",
887 inode->i_dev, (long long)NFS_FILEID(inode));
890 if (!inode || is_bad_inode(inode))
892 if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
895 while (NFS_REVALIDATING(inode)) {
896 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
899 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode))) {
900 status = NFS_STALE(inode) ? -ESTALE : 0;
904 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
906 status = NFS_PROTO(inode)->getattr(inode, &fattr);
908 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) getattr failed, error=%d\n",
909 inode->i_dev, (long long)NFS_FILEID(inode), status);
910 if (status == -ESTALE) {
911 NFS_FLAGS(inode) |= NFS_INO_STALE;
912 if (inode != inode->i_sb->s_root->d_inode)
913 remove_inode_hash(inode);
918 status = nfs_refresh_inode(inode, &fattr);
920 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) refresh failed, error=%d\n",
921 inode->i_dev, (long long)NFS_FILEID(inode), status);
924 dfprintk(PAGECACHE, "NFS: (%x/%Ld) revalidation complete\n",
925 inode->i_dev, (long long)NFS_FILEID(inode));
927 NFS_FLAGS(inode) &= ~NFS_INO_STALE;
929 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
930 wake_up(&inode->i_wait);
937 * nfs_fattr_obsolete - Test if attribute data is newer than cached data
939 * @fattr: attributes to test
941 * Avoid stuffing the attribute cache with obsolete information.
942 * We always accept updates if the attribute cache timed out, or if
943 * fattr->ctime is newer than our cached value.
944 * If fattr->ctime matches the cached value, we still accept the update
945 * if it increases the file size.
948 int nfs_fattr_obsolete(struct inode *inode, struct nfs_fattr *fattr)
952 if (time_after(jiffies, NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
954 if ((cdif = (s64)fattr->ctime - (s64)NFS_CACHE_CTIME(inode)) > 0)
957 if (cdif == 0 && fattr->size > NFS_CACHE_ISIZE(inode))
965 * Many nfs protocol calls return the new file attributes after
966 * an operation. Here we update the inode to reflect the state
967 * of the server's inode.
969 * This is a bit tricky because we have to make sure all dirty pages
970 * have been sent off to the server before calling invalidate_inode_pages.
971 * To make sure no other process adds more write requests while we try
972 * our best to flush them, we make them sleep during the attribute refresh.
974 * A very similar scenario holds for the dir cache.
977 __nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
979 __u64 new_size, new_mtime;
984 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
985 inode->i_dev, inode->i_ino,
986 atomic_read(&inode->i_count), fattr->valid);
988 if (NFS_FILEID(inode) != fattr->fileid) {
989 printk(KERN_ERR "nfs_refresh_inode: inode number mismatch\n"
990 "expected (0x%x/0x%Lx), got (0x%x/0x%Lx)\n",
991 inode->i_dev, (long long)NFS_FILEID(inode),
992 inode->i_dev, (long long)fattr->fileid);
997 * Make sure the inode's type hasn't changed.
999 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1002 new_mtime = fattr->mtime;
1003 new_size = fattr->size;
1004 new_isize = nfs_size_to_loff_t(fattr->size);
1006 new_atime = nfs_time_to_secs(fattr->atime);
1008 if (nfs_fattr_obsolete(inode, fattr))
1012 * Update the read time so we don't revalidate too often.
1014 NFS_READTIME(inode) = jiffies;
1017 * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
1018 * NOT inode->i_size!!!
1020 if (NFS_CACHE_ISIZE(inode) != new_size) {
1021 #ifdef NFS_DEBUG_VERBOSE
1022 printk(KERN_DEBUG "NFS: isize change on %x/%ld\n", inode->i_dev, inode->i_ino);
1028 * Note: we don't check inode->i_mtime since pipes etc.
1029 * can change this value in VFS without requiring a
1030 * cache revalidation.
1032 if (NFS_CACHE_MTIME(inode) != new_mtime) {
1033 #ifdef NFS_DEBUG_VERBOSE
1034 printk(KERN_DEBUG "NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
1039 /* Check Weak Cache Consistency data.
1040 * If size and mtime match the pre-operation values, we can
1041 * assume that any attribute changes were caused by our NFS
1042 * operation, so there's no need to invalidate the caches.
1044 if ((fattr->valid & NFS_ATTR_WCC)
1045 && NFS_CACHE_ISIZE(inode) == fattr->pre_size
1046 && NFS_CACHE_MTIME(inode) == fattr->pre_mtime) {
1050 /* set the invalid flag if the last attempt at invalidating
1051 * the inode didn't empty the clean_pages list */
1052 if ( NFS_FLAGS(inode) & NFS_INO_MAPPED) {
1053 NFS_FLAGS(inode) &= ~NFS_INO_MAPPED;
1058 * If we have pending writebacks, things can get
1061 if (nfs_have_writebacks(inode) && new_isize < inode->i_size)
1062 new_isize = inode->i_size;
1064 NFS_CACHE_CTIME(inode) = fattr->ctime;
1065 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
1067 inode->i_atime = new_atime;
1069 if (NFS_CACHE_MTIME(inode) != new_mtime) {
1070 NFS_MTIME_UPDATE(inode) = jiffies;
1071 NFS_CACHE_MTIME(inode) = new_mtime;
1072 inode->i_mtime = nfs_time_to_secs(new_mtime);
1075 NFS_CACHE_ISIZE(inode) = new_size;
1076 inode->i_size = new_isize;
1078 inode->i_mode = fattr->mode;
1079 inode->i_nlink = fattr->nlink;
1080 inode->i_uid = fattr->uid;
1081 inode->i_gid = fattr->gid;
1083 if (fattr->valid & NFS_ATTR_FATTR_V3) {
1085 * report the blocks in 512byte units
1087 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1088 inode->i_blksize = inode->i_sb->s_blocksize;
1090 inode->i_blocks = fattr->du.nfs2.blocks;
1091 inode->i_blksize = fattr->du.nfs2.blocksize;
1094 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1095 inode->i_rdev = to_kdev_t(fattr->rdev);
1097 /* Update attrtimeo value */
1099 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
1100 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1101 invalidate_inode_pages(inode);
1102 if (! list_empty(&inode->i_mapping->clean_pages)) {
1104 "NFS: clean_pages for %x/%ld is not empty\n",
1105 inode->i_dev, inode->i_ino);
1106 NFS_FLAGS(inode) |= NFS_INO_MAPPED;
1108 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1109 } else if (time_after(jiffies, NFS_ATTRTIMEO_UPDATE(inode)+NFS_ATTRTIMEO(inode))) {
1110 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
1111 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
1112 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1117 if (new_atime - inode->i_atime > 0)
1118 inode->i_atime = new_atime;
1122 * Big trouble! The inode has become a different object.
1125 printk(KERN_DEBUG "nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1126 inode->i_ino, inode->i_mode, fattr->mode);
1129 * No need to worry about unhashing the dentry, as the
1130 * lookup validation will know that the inode is bad.
1131 * (But we fall through to invalidate the caches.)
1133 nfs_invalidate_inode(inode);
1139 * File system information
1141 static DECLARE_FSTYPE(nfs_fs_type, "nfs", nfs_read_super, FS_ODD_RENAME);
1143 extern int nfs_init_nfspagecache(void);
1144 extern void nfs_destroy_nfspagecache(void);
1145 extern int nfs_init_readpagecache(void);
1146 extern int nfs_destroy_readpagecache(void);
1147 extern int nfs_init_writepagecache(void);
1148 extern int nfs_destroy_writepagecache(void);
1153 static int __init init_nfs_fs(void)
1157 err = nfs_init_nfspagecache();
1161 err = nfs_init_readpagecache();
1165 err = nfs_init_writepagecache();
1169 #ifdef CONFIG_PROC_FS
1170 rpc_proc_register(&nfs_rpcstat);
1172 return register_filesystem(&nfs_fs_type);
1175 static void __exit exit_nfs_fs(void)
1177 nfs_destroy_writepagecache();
1178 nfs_destroy_readpagecache();
1179 nfs_destroy_nfspagecache();
1180 #ifdef CONFIG_PROC_FS
1181 rpc_proc_unregister("nfs");
1183 unregister_filesystem(&nfs_fs_type);
1187 /* Not quite true; I just maintain it */
1188 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1189 MODULE_LICENSE("GPL");
1191 module_init(init_nfs_fs)
1192 module_exit(exit_nfs_fs)