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/time.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/errno.h>
26 #include <linux/unistd.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/sunrpc/stats.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/vserver/xid.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
43 #define NFSDBG_FACILITY NFSDBG_VFS
44 #define NFS_PARANOIA 1
46 /* Maximum number of readahead requests
47 * FIXME: this should really be a sysctl so that users may tune it to suit
48 * their needs. People that do NFS over a slow network, might for
49 * instance want to reduce it to something closer to 1 for improved
50 * interactive response.
52 #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
54 static void nfs_invalidate_inode(struct inode *);
55 static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
57 static struct inode *nfs_alloc_inode(struct super_block *sb);
58 static void nfs_destroy_inode(struct inode *);
59 static void nfs_write_inode(struct inode *,int);
60 static void nfs_delete_inode(struct inode *);
61 static void nfs_put_super(struct super_block *);
62 static void nfs_clear_inode(struct inode *);
63 static void nfs_umount_begin(struct super_block *);
64 static int nfs_statfs(struct super_block *, struct kstatfs *);
65 static int nfs_show_options(struct seq_file *, struct vfsmount *);
67 static struct super_operations nfs_sops = {
68 .alloc_inode = nfs_alloc_inode,
69 .destroy_inode = nfs_destroy_inode,
70 .write_inode = nfs_write_inode,
71 .delete_inode = nfs_delete_inode,
72 .put_super = nfs_put_super,
74 .clear_inode = nfs_clear_inode,
75 .umount_begin = nfs_umount_begin,
76 .show_options = nfs_show_options,
82 struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
85 static struct rpc_version * nfs_version[] = {
89 #if defined(CONFIG_NFS_V3)
91 #elif defined(CONFIG_NFS_V4)
94 #if defined(CONFIG_NFS_V4)
99 struct rpc_program nfs_program = {
101 .number = NFS_PROGRAM,
102 .nrvers = sizeof(nfs_version) / sizeof(nfs_version[0]),
103 .version = nfs_version,
104 .stats = &nfs_rpcstat,
105 .pipe_dir_name = "/nfs",
108 static inline unsigned long
109 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
111 return nfs_fileid_to_ino_t(fattr->fileid);
115 nfs_write_inode(struct inode *inode, int sync)
117 int flags = sync ? FLUSH_WAIT : 0;
119 nfs_commit_inode(inode, 0, 0, flags);
123 nfs_delete_inode(struct inode * inode)
125 dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
128 * The following can never actually happen...
130 if (nfs_have_writebacks(inode)) {
131 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
134 // DLIMIT_FREE_INODE(inode->i_sb, inode->i_xid);
139 * For the moment, the only task for the NFS clear_inode method is to
140 * release the mmap credential
143 nfs_clear_inode(struct inode *inode)
145 struct nfs_inode *nfsi = NFS_I(inode);
146 struct rpc_cred *cred = nfsi->mm_cred;
150 cred = nfsi->cache_access.cred;
153 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
157 nfs_put_super(struct super_block *sb)
159 struct nfs_server *server = NFS_SB(sb);
161 nfs4_renewd_prepare_shutdown(server);
163 if (server->client != NULL)
164 rpc_shutdown_client(server->client);
165 if (server->client_sys != NULL)
166 rpc_shutdown_client(server->client_sys);
168 if (!(server->flags & NFS_MOUNT_NONLM))
169 lockd_down(); /* release rpc.lockd */
170 rpciod_down(); /* release rpciod */
172 destroy_nfsv4_state(server);
174 kfree(server->hostname);
178 nfs_umount_begin(struct super_block *sb)
180 struct nfs_server *server = NFS_SB(sb);
181 struct rpc_clnt *rpc;
183 /* -EIO all pending I/O */
184 if ((rpc = server->client) != NULL)
185 rpc_killall_tasks(rpc);
189 static inline unsigned long
190 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
192 /* make sure blocksize is a power of two */
193 if ((bsize & (bsize - 1)) || nrbitsp) {
194 unsigned char nrbits;
196 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
207 * Calculate the number of 512byte blocks used.
209 static inline unsigned long
210 nfs_calc_block_size(u64 tsize)
212 loff_t used = (tsize + 511) >> 9;
213 return (used > ULONG_MAX) ? ULONG_MAX : used;
217 * Compute and set NFS server blocksize
219 static inline unsigned long
220 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
223 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
224 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
225 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
227 return nfs_block_bits(bsize, nrbitsp);
231 * Obtain the root inode of the file system.
233 static struct inode *
234 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
236 struct nfs_server *server = NFS_SB(sb);
240 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
242 dprintk("nfs_get_root: getattr error = %d\n", -error);
243 return ERR_PTR(error);
246 rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
248 return ERR_PTR(-ENOMEM);
253 * Do NFS version-independent mount processing, and sanity checking
256 nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
258 struct nfs_server *server;
259 struct inode *root_inode;
260 struct nfs_fattr fattr;
261 struct nfs_fsinfo fsinfo = {
264 struct nfs_pathconf pathinfo = {
267 int no_root_error = 0;
269 /* We probably want something more informative here */
270 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
274 sb->s_magic = NFS_SUPER_MAGIC;
276 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
277 /* Did getting the root inode fail? */
278 if (IS_ERR(root_inode)) {
279 no_root_error = PTR_ERR(root_inode);
282 sb->s_root = d_alloc_root(root_inode);
284 no_root_error = -ENOMEM;
287 sb->s_root->d_op = server->rpc_ops->dentry_ops;
289 /* Get some general file system info */
290 if (server->namelen == 0 &&
291 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
292 server->namelen = pathinfo.max_namelen;
293 /* Work out a lot of parameters */
294 if (server->rsize == 0)
295 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
296 if (server->wsize == 0)
297 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
298 if (sb->s_blocksize == 0) {
299 if (fsinfo.wtmult == 0) {
300 sb->s_blocksize = 512;
301 sb->s_blocksize_bits = 9;
303 sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
304 &sb->s_blocksize_bits);
307 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
308 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
309 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
310 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
312 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
313 if (server->rpages > NFS_READ_MAXIOV) {
314 server->rpages = NFS_READ_MAXIOV;
315 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
318 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
319 if (server->wpages > NFS_WRITE_MAXIOV) {
320 server->wpages = NFS_WRITE_MAXIOV;
321 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
324 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
325 if (server->dtsize > PAGE_CACHE_SIZE)
326 server->dtsize = PAGE_CACHE_SIZE;
327 if (server->dtsize > server->rsize)
328 server->dtsize = server->rsize;
330 if (server->flags & NFS_MOUNT_NOAC) {
331 server->acregmin = server->acregmax = 0;
332 server->acdirmin = server->acdirmax = 0;
333 sb->s_flags |= MS_SYNCHRONOUS;
335 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
337 if (server->flags & NFS_MOUNT_TAGXID)
338 sb->s_flags |= MS_TAGXID;
340 sb->s_maxbytes = fsinfo.maxfilesize;
341 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
342 sb->s_maxbytes = MAX_LFS_FILESIZE;
344 /* We're airborne Set socket buffersize */
345 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
347 /* Yargs. It didn't work out. */
349 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
350 if (!IS_ERR(root_inode))
352 return no_root_error;
356 * Create an RPC client handle.
358 static struct rpc_clnt *
359 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
361 struct rpc_timeout timeparms;
362 struct rpc_xprt *xprt = NULL;
363 struct rpc_clnt *clnt = NULL;
364 int tcp = (data->flags & NFS_MOUNT_TCP);
366 /* Initialize timeout values */
367 timeparms.to_initval = data->timeo * HZ / 10;
368 timeparms.to_retries = data->retrans;
369 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
370 timeparms.to_exponential = 1;
372 if (!timeparms.to_initval)
373 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
374 if (!timeparms.to_retries)
375 timeparms.to_retries = 5;
377 /* create transport and client */
378 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
379 &server->addr, &timeparms);
381 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
382 return (struct rpc_clnt *)xprt;
384 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
385 server->rpc_ops->version, data->pseudoflavor);
387 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
391 clnt->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
392 clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
393 clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
404 * The way this works is that the mount process passes a structure
405 * in the data argument which contains the server's IP address
406 * and the root file handle obtained from the server's mount
407 * daemon. We stash these away in the private superblock fields.
410 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
412 struct nfs_server *server;
414 rpc_authflavor_t authflavor;
417 sb->s_blocksize_bits = 0;
420 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
422 server->rsize = nfs_block_size(data->rsize, NULL);
424 server->wsize = nfs_block_size(data->wsize, NULL);
425 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
427 server->acregmin = data->acregmin*HZ;
428 server->acregmax = data->acregmax*HZ;
429 server->acdirmin = data->acdirmin*HZ;
430 server->acdirmax = data->acdirmax*HZ;
432 server->namelen = data->namlen;
433 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
434 if (!server->hostname)
436 strcpy(server->hostname, data->hostname);
438 /* Check NFS protocol revision and initialize RPC op vector
439 * and file handle pool. */
440 if (server->flags & NFS_MOUNT_VER3) {
442 server->rpc_ops = &nfs_v3_clientops;
443 server->caps |= NFS_CAP_READDIRPLUS;
444 if (data->version < 4) {
445 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
449 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
453 server->rpc_ops = &nfs_v2_clientops;
456 /* Fill in pseudoflavor for mount version < 5 */
457 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
458 data->pseudoflavor = RPC_AUTH_UNIX;
459 authflavor = data->pseudoflavor; /* save for sb_init() */
460 /* XXX maybe we want to add a server->pseudoflavor field */
462 /* Create RPC client handles */
463 server->client = nfs_create_client(server, data);
464 if (IS_ERR(server->client))
466 /* RFC 2623, sec 2.3.2 */
467 if (authflavor != RPC_AUTH_UNIX) {
468 server->client_sys = rpc_clone_client(server->client);
469 if (server->client_sys == NULL)
471 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
474 atomic_inc(&server->client->cl_count);
475 server->client_sys = server->client;
478 /* Fire up rpciod if not yet running */
479 if (rpciod_up() != 0) {
480 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
484 sb->s_op = &nfs_sops;
485 err = nfs_sb_init(sb, authflavor);
489 if (server->flags & NFS_MOUNT_VER3) {
490 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
491 server->namelen = NFS3_MAXNAMLEN;
493 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
494 server->namelen = NFS2_MAXNAMLEN;
497 /* Check whether to start the lockd process */
498 if (!(server->flags & NFS_MOUNT_NONLM))
505 rpc_shutdown_client(server->client);
506 if (server->client_sys)
507 rpc_shutdown_client(server->client_sys);
509 if (server->hostname)
510 kfree(server->hostname);
515 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
517 struct nfs_server *server = NFS_SB(sb);
518 unsigned char blockbits;
519 unsigned long blockres;
520 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
521 struct nfs_fattr fattr;
522 struct nfs_fsstat res = {
529 error = server->rpc_ops->statfs(server, rootfh, &res);
530 buf->f_type = NFS_SUPER_MAGIC;
534 buf->f_bsize = sb->s_blocksize;
535 blockbits = sb->s_blocksize_bits;
536 blockres = (1 << blockbits) - 1;
537 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
538 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
539 buf->f_bavail = (res.abytes + blockres) >> blockbits;
540 buf->f_files = res.tfiles;
541 buf->f_ffree = res.afiles;
543 buf->f_namelen = server->namelen;
550 printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
551 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", "" },
571 { NFS_MOUNT_TAGXID, ",tagxid", "" },
574 struct proc_nfs_info *nfs_infop;
575 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
577 seq_printf(m, ",v%d", nfss->rpc_ops->version);
578 seq_printf(m, ",rsize=%d", nfss->rsize);
579 seq_printf(m, ",wsize=%d", nfss->wsize);
580 if (nfss->acregmin != 3*HZ)
581 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
582 if (nfss->acregmax != 60*HZ)
583 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
584 if (nfss->acdirmin != 30*HZ)
585 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
586 if (nfss->acdirmax != 60*HZ)
587 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
588 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
589 if (nfss->flags & nfs_infop->flag)
590 seq_puts(m, nfs_infop->str);
592 seq_puts(m, nfs_infop->nostr);
594 seq_puts(m, ",addr=");
595 seq_escape(m, nfss->hostname, " \t\n\\");
600 * Invalidate the local caches
603 nfs_zap_caches(struct inode *inode)
605 struct nfs_inode *nfsi = NFS_I(inode);
606 int mode = inode->i_mode;
608 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
609 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
611 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
612 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
613 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
615 nfsi->flags |= NFS_INO_INVALID_ATTR;
619 * Invalidate, but do not unhash, the inode
622 nfs_invalidate_inode(struct inode *inode)
624 umode_t save_mode = inode->i_mode;
626 make_bad_inode(inode);
627 inode->i_mode = save_mode;
628 nfs_zap_caches(inode);
631 struct nfs_find_desc {
633 struct nfs_fattr *fattr;
637 * In NFSv3 we can have 64bit inode numbers. In order to support
638 * this, and re-exported directories (also seen in NFSv2)
639 * we are forced to allow 2 different inodes to have the same
643 nfs_find_actor(struct inode *inode, void *opaque)
645 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
646 struct nfs_fh *fh = desc->fh;
647 struct nfs_fattr *fattr = desc->fattr;
649 if (NFS_FILEID(inode) != fattr->fileid)
651 if (memcmp(NFS_FH(inode), fh, sizeof(struct nfs_fh)) != 0)
653 if (is_bad_inode(inode))
659 nfs_init_locked(struct inode *inode, void *opaque)
661 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
662 struct nfs_fh *fh = desc->fh;
663 struct nfs_fattr *fattr = desc->fattr;
665 NFS_FILEID(inode) = fattr->fileid;
666 memcpy(NFS_FH(inode), fh, sizeof(struct nfs_fh));
670 /* Don't use READDIRPLUS on directories that we believe are too large */
671 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
674 * This is our front-end to iget that looks up inodes by file handle
675 * instead of inode number.
678 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
680 struct nfs_find_desc desc = {
684 struct inode *inode = NULL;
687 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
691 printk("NFS: Buggy server - nlink == 0!\n");
695 hash = nfs_fattr_to_ino_t(fattr);
697 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
700 if (inode->i_state & I_NEW) {
701 struct nfs_inode *nfsi = NFS_I(inode);
703 /* if (DLIMIT_ALLOC_INODE(sb, inode->i_xid)) {
708 /* We set i_ino for the few things that still rely on it,
712 /* We can't support update_atime(), since the server will reset it */
713 inode->i_flags |= S_NOATIME|S_NOCMTIME;
714 inode->i_mode = fattr->mode;
715 /* Why so? Because we want revalidate for devices/FIFOs, and
716 * that's precisely what we have in nfs_file_inode_operations.
718 inode->i_op = &nfs_file_inode_operations;
719 if (S_ISREG(inode->i_mode)) {
720 inode->i_fop = &nfs_file_operations;
721 inode->i_data.a_ops = &nfs_file_aops;
722 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
723 } else if (S_ISDIR(inode->i_mode)) {
724 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
725 inode->i_fop = &nfs_dir_operations;
726 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
727 && fattr->size <= NFS_LIMIT_READDIRPLUS)
728 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
729 } else if (S_ISLNK(inode->i_mode))
730 inode->i_op = &nfs_symlink_inode_operations;
732 init_special_inode(inode, inode->i_mode, fattr->rdev);
734 nfsi->read_cache_jiffies = fattr->timestamp;
735 inode->i_atime = fattr->atime;
736 inode->i_mtime = fattr->mtime;
737 inode->i_ctime = fattr->ctime;
738 if (fattr->valid & NFS_ATTR_FATTR_V4)
739 nfsi->change_attr = fattr->change_attr;
740 inode->i_size = nfs_size_to_loff_t(fattr->size);
741 inode->i_nlink = fattr->nlink;
742 inode->i_uid = INOXID_UID(fattr->uid, fattr->gid);
743 inode->i_gid = INOXID_GID(fattr->uid, fattr->gid);
744 if (inode->i_sb->s_flags & MS_TAGXID)
745 inode->i_xid = INOXID_XID(fattr->uid, fattr->gid, 0);
746 /* maybe fattr->xid someday */
747 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
749 * report the blocks in 512byte units
751 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
752 inode->i_blksize = inode->i_sb->s_blocksize;
754 inode->i_blocks = fattr->du.nfs2.blocks;
755 inode->i_blksize = fattr->du.nfs2.blocksize;
757 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
758 nfsi->attrtimeo_timestamp = jiffies;
759 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
760 nfsi->cache_access.cred = NULL;
762 unlock_new_inode(inode);
764 nfs_refresh_inode(inode, fattr);
765 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
767 (long long)NFS_FILEID(inode),
768 atomic_read(&inode->i_count));
774 make_bad_inode(inode);
779 printk("nfs_fhget: iget failed\n");
783 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_XID|ATTR_SIZE|\
784 ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
787 nfs_setattr(struct dentry *dentry, struct iattr *attr)
789 struct inode *inode = dentry->d_inode;
790 struct nfs_fattr fattr;
793 if (attr->ia_valid & ATTR_SIZE) {
794 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
795 attr->ia_valid &= ~ATTR_SIZE;
798 /* Optimization: if the end result is no change, don't RPC */
799 attr->ia_valid &= NFS_VALID_ATTRS;
800 if (attr->ia_valid == 0)
804 nfs_begin_data_update(inode);
805 /* Write all dirty data if we're changing file permissions or size */
806 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
807 if (filemap_fdatawrite(inode->i_mapping) == 0)
808 filemap_fdatawait(inode->i_mapping);
811 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
813 nfs_refresh_inode(inode, &fattr);
814 if ((attr->ia_valid & ATTR_MODE) != 0) {
816 mode = inode->i_mode & ~S_IALLUGO;
817 mode |= attr->ia_mode & S_IALLUGO;
818 inode->i_mode = mode;
820 if ((attr->ia_valid & ATTR_UID) != 0)
821 inode->i_uid = attr->ia_uid;
822 if ((attr->ia_valid & ATTR_GID) != 0)
823 inode->i_gid = attr->ia_gid;
824 if ((attr->ia_valid & ATTR_XID) != 0)
825 inode->i_xid = attr->ia_xid;
826 if ((attr->ia_valid & ATTR_SIZE) != 0) {
827 inode->i_size = attr->ia_size;
828 vmtruncate(inode, attr->ia_size);
831 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
832 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
838 nfs_end_data_update(inode);
844 * Wait for the inode to get unlocked.
845 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
848 nfs_wait_on_inode(struct inode *inode, int flag)
850 struct rpc_clnt *clnt = NFS_CLIENT(inode);
851 struct nfs_inode *nfsi = NFS_I(inode);
854 if (!(NFS_FLAGS(inode) & flag))
856 atomic_inc(&inode->i_count);
857 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
858 !(NFS_FLAGS(inode) & flag));
863 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
865 struct inode *inode = dentry->d_inode;
866 struct nfs_inode *nfsi = NFS_I(inode);
867 int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
870 if (__IS_FLG(inode, MS_NOATIME))
872 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
874 /* We may force a getattr if the user cares about atime */
876 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
878 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
880 generic_fillattr(inode, stat);
885 * Ensure that mmap has a recent RPC credential for use when writing out
889 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
891 struct rpc_cred **p = &NFS_I(inode)->mm_cred,
894 *p = get_rpccred(cred);
896 put_rpccred(oldcred);
900 * These are probably going to contain hooks for
901 * allocating and releasing RPC credentials for
902 * the file. I'll have to think about Tronds patch
905 int nfs_open(struct inode *inode, struct file *filp)
907 struct rpc_auth *auth;
908 struct rpc_cred *cred;
910 auth = NFS_CLIENT(inode)->cl_auth;
911 cred = rpcauth_lookupcred(auth, 0);
912 filp->private_data = cred;
913 if ((filp->f_mode & FMODE_WRITE) != 0) {
914 nfs_set_mmcred(inode, cred);
915 nfs_begin_data_update(inode);
920 int nfs_release(struct inode *inode, struct file *filp)
922 struct rpc_cred *cred;
925 if ((filp->f_mode & FMODE_WRITE) != 0)
926 nfs_end_data_update(inode);
927 cred = nfs_file_cred(filp);
935 * This function is called whenever some part of NFS notices that
936 * the cached attributes have to be refreshed.
939 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
941 int status = -ESTALE;
942 struct nfs_fattr fattr;
943 struct nfs_inode *nfsi = NFS_I(inode);
944 unsigned long verifier;
947 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
948 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
951 if (!inode || is_bad_inode(inode))
953 if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
956 while (NFS_REVALIDATING(inode)) {
957 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
960 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
962 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
964 status = NFS_STALE(inode) ? -ESTALE : 0;
967 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
969 /* Protect against RPC races by saving the change attribute */
970 verifier = nfs_save_change_attribute(inode);
971 status = NFS_PROTO(inode)->getattr(inode, &fattr);
973 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
975 (long long)NFS_FILEID(inode), status);
976 if (status == -ESTALE) {
977 NFS_FLAGS(inode) |= NFS_INO_STALE;
978 if (inode != inode->i_sb->s_root->d_inode)
979 remove_inode_hash(inode);
984 status = nfs_update_inode(inode, &fattr, verifier);
986 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
988 (long long)NFS_FILEID(inode), status);
993 * We may need to keep the attributes marked as invalid if
994 * we raced with nfs_end_attr_update().
996 if (verifier == nfsi->cache_change_attribute)
997 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
998 /* Do the page cache invalidation */
999 if (flags & NFS_INO_INVALID_DATA) {
1000 if (S_ISREG(inode->i_mode)) {
1001 if (filemap_fdatawrite(inode->i_mapping) == 0)
1002 filemap_fdatawait(inode->i_mapping);
1005 nfsi->flags &= ~NFS_INO_INVALID_DATA;
1006 invalidate_inode_pages2(inode->i_mapping);
1007 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1008 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1010 (long long)NFS_FILEID(inode));
1011 /* This ensures we revalidate dentries */
1012 nfsi->cache_change_attribute++;
1014 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1016 (long long)NFS_FILEID(inode));
1018 NFS_FLAGS(inode) &= ~NFS_INO_STALE;
1020 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1021 wake_up(&nfsi->nfs_i_wait);
1028 * nfs_begin_data_update
1029 * @inode - pointer to inode
1030 * Declare that a set of operations will update file data on the server
1032 void nfs_begin_data_update(struct inode *inode)
1034 atomic_inc(&NFS_I(inode)->data_updates);
1038 * nfs_end_data_update
1039 * @inode - pointer to inode
1040 * Declare end of the operations that will update file data
1041 * This will mark the inode as immediately needing revalidation
1042 * of its attribute cache.
1044 void nfs_end_data_update(struct inode *inode)
1046 struct nfs_inode *nfsi = NFS_I(inode);
1048 /* Mark the attribute cache for revalidation */
1049 nfsi->flags |= NFS_INO_INVALID_ATTR;
1050 /* Directories and symlinks: invalidate page cache too */
1051 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1052 nfsi->flags |= NFS_INO_INVALID_DATA;
1053 nfsi->cache_change_attribute ++;
1054 atomic_dec(&nfsi->data_updates);
1058 * nfs_end_data_update_defer
1059 * @inode - pointer to inode
1060 * Declare end of the operations that will update file data
1061 * This will defer marking the inode as needing revalidation
1062 * unless there are no other pending updates.
1064 void nfs_end_data_update_defer(struct inode *inode)
1066 struct nfs_inode *nfsi = NFS_I(inode);
1068 if (atomic_dec_and_test(&nfsi->data_updates)) {
1069 /* Mark the attribute cache for revalidation */
1070 nfsi->flags |= NFS_INO_INVALID_ATTR;
1071 /* Directories and symlinks: invalidate page cache too */
1072 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1073 nfsi->flags |= NFS_INO_INVALID_DATA;
1074 nfsi->cache_change_attribute ++;
1079 * nfs_refresh_inode - verify consistency of the inode attribute cache
1080 * @inode - pointer to inode
1081 * @fattr - updated attributes
1083 * Verifies the attribute cache. If we have just changed the attributes,
1084 * so that fattr carries weak cache consistency data, then it may
1085 * also update the ctime/mtime/change_attribute.
1087 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1089 struct nfs_inode *nfsi = NFS_I(inode);
1090 loff_t cur_size, new_isize;
1096 /* Are we in the process of updating data on the server? */
1097 data_unstable = nfs_caches_unstable(inode);
1099 if (fattr->valid & NFS_ATTR_FATTR_V4) {
1100 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1101 && nfsi->change_attr == fattr->pre_change_attr)
1102 nfsi->change_attr = fattr->change_attr;
1103 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1104 nfsi->flags |= NFS_INO_INVALID_ATTR;
1107 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1110 /* Has the inode gone and changed behind our back? */
1111 if (nfsi->fileid != fattr->fileid
1112 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1115 cur_size = i_size_read(inode);
1116 new_isize = nfs_size_to_loff_t(fattr->size);
1118 /* If we have atomic WCC data, we may update some attributes */
1119 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1120 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1121 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1122 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1123 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1126 /* Verify a few of the more important attributes */
1127 if (!data_unstable) {
1128 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1129 || cur_size != new_isize)
1130 nfsi->flags |= NFS_INO_INVALID_ATTR;
1131 } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1132 nfsi->flags |= NFS_INO_INVALID_ATTR;
1134 uid = INOXID_UID(fattr->uid, fattr->gid);
1135 gid = INOXID_GID(fattr->uid, fattr->gid);
1136 if (inode->i_sb->s_flags & MS_TAGXID)
1137 xid = INOXID_XID(fattr->uid, fattr->gid, 0);
1139 /* Have any file permissions changed? */
1140 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1141 || inode->i_uid != uid
1142 || inode->i_gid != gid
1143 || inode->i_xid != xid)
1144 nfsi->flags |= NFS_INO_INVALID_ATTR;
1146 /* Has the link count changed? */
1147 if (inode->i_nlink != fattr->nlink)
1148 nfsi->flags |= NFS_INO_INVALID_ATTR;
1150 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1151 nfsi->flags |= NFS_INO_INVALID_ATIME;
1153 nfsi->read_cache_jiffies = fattr->timestamp;
1158 * Many nfs protocol calls return the new file attributes after
1159 * an operation. Here we update the inode to reflect the state
1160 * of the server's inode.
1162 * This is a bit tricky because we have to make sure all dirty pages
1163 * have been sent off to the server before calling invalidate_inode_pages.
1164 * To make sure no other process adds more write requests while we try
1165 * our best to flush them, we make them sleep during the attribute refresh.
1167 * A very similar scenario holds for the dir cache.
1169 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1171 struct nfs_inode *nfsi = NFS_I(inode);
1174 unsigned int invalid = 0;
1181 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1182 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1183 atomic_read(&inode->i_count), fattr->valid);
1185 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1188 if (nfsi->fileid != fattr->fileid) {
1189 printk(KERN_ERR "%s: inode number mismatch\n"
1190 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1192 inode->i_sb->s_id, (long long)nfsi->fileid,
1193 inode->i_sb->s_id, (long long)fattr->fileid);
1198 * Make sure the inode's type hasn't changed.
1200 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1204 * Update the read time so we don't revalidate too often.
1206 nfsi->read_cache_jiffies = fattr->timestamp;
1208 /* Are we racing with known updates of the metadata on the server? */
1209 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1211 /* Check if the file size agrees */
1212 new_size = fattr->size;
1213 new_isize = nfs_size_to_loff_t(fattr->size);
1214 cur_isize = i_size_read(inode);
1215 if (cur_isize != new_size) {
1216 #ifdef NFS_DEBUG_VERBOSE
1217 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1220 * If we have pending writebacks, things can get
1223 if (S_ISREG(inode->i_mode) && data_unstable) {
1224 if (new_isize > cur_isize) {
1225 inode->i_size = new_isize;
1226 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1229 inode->i_size = new_isize;
1230 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1235 * Note: we don't check inode->i_mtime since pipes etc.
1236 * can change this value in VFS without requiring a
1237 * cache revalidation.
1239 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1240 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1241 #ifdef NFS_DEBUG_VERBOSE
1242 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1245 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1248 if ((fattr->valid & NFS_ATTR_FATTR_V4)
1249 && nfsi->change_attr != fattr->change_attr) {
1250 #ifdef NFS_DEBUG_VERBOSE
1251 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1252 inode->i_sb->s_id, inode->i_ino);
1254 nfsi->change_attr = fattr->change_attr;
1256 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1259 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1260 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1262 uid = INOXID_UID(fattr->uid, fattr->gid);
1263 gid = INOXID_GID(fattr->uid, fattr->gid);
1264 if (inode->i_sb->s_flags & MS_TAGXID)
1265 xid = INOXID_XID(fattr->uid, fattr->gid, 0);
1267 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1268 inode->i_uid != uid ||
1269 inode->i_gid != gid ||
1270 inode->i_xid != xid) {
1271 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1276 invalid |= NFS_INO_INVALID_ATTR;
1279 inode->i_mode = fattr->mode;
1280 inode->i_nlink = fattr->nlink;
1285 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1287 * report the blocks in 512byte units
1289 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1290 inode->i_blksize = inode->i_sb->s_blocksize;
1292 inode->i_blocks = fattr->du.nfs2.blocks;
1293 inode->i_blksize = fattr->du.nfs2.blocksize;
1296 /* Update attrtimeo value if we're out of the unstable period */
1297 if (invalid & NFS_INO_INVALID_ATTR) {
1298 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1299 nfsi->attrtimeo_timestamp = jiffies;
1300 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1301 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1302 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1303 nfsi->attrtimeo_timestamp = jiffies;
1305 /* Don't invalidate the data if we were to blame */
1306 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1307 || S_ISLNK(inode->i_mode)))
1308 invalid &= ~NFS_INO_INVALID_DATA;
1309 nfsi->flags |= invalid;
1314 * Big trouble! The inode has become a different object.
1317 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1318 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1321 * No need to worry about unhashing the dentry, as the
1322 * lookup validation will know that the inode is bad.
1323 * (But we fall through to invalidate the caches.)
1325 nfs_invalidate_inode(inode);
1331 * File system information
1334 static int nfs_set_super(struct super_block *s, void *data)
1336 s->s_fs_info = data;
1337 return set_anon_super(s, data);
1340 static int nfs_compare_super(struct super_block *sb, void *data)
1342 struct nfs_server *server = data;
1343 struct nfs_server *old = NFS_SB(sb);
1345 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1347 if (old->addr.sin_port != server->addr.sin_port)
1349 return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1352 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1353 int flags, const char *dev_name, void *raw_data)
1356 struct nfs_server *server;
1357 struct super_block *s;
1358 struct nfs_fh *root;
1359 struct nfs_mount_data *data = raw_data;
1362 printk("nfs_read_super: missing data argument\n");
1363 return ERR_PTR(-EINVAL);
1366 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1368 return ERR_PTR(-ENOMEM);
1369 memset(server, 0, sizeof(struct nfs_server));
1370 /* Zero out the NFS state stuff */
1371 init_nfsv4_state(server);
1374 memcpy(root, &data->root, sizeof(*root));
1375 if (root->size < sizeof(root->data))
1376 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1378 if (data->version != NFS_MOUNT_VERSION) {
1379 printk("nfs warning: mount version %s than kernel\n",
1380 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1381 if (data->version < 2)
1383 if (data->version < 3)
1385 if (data->version < 4) {
1386 data->flags &= ~NFS_MOUNT_VER3;
1387 memset(root, 0, sizeof(*root));
1388 root->size = NFS2_FHSIZE;
1389 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1391 if (data->version < 5)
1392 data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1395 if (root->size > sizeof(root->data)) {
1396 printk("nfs_get_sb: invalid root filehandle\n");
1398 return ERR_PTR(-EINVAL);
1400 /* We now require that the mount process passes the remote address */
1401 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1402 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1403 printk("NFS: mount program didn't pass remote address!\n");
1405 return ERR_PTR(-EINVAL);
1408 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1410 if (IS_ERR(s) || s->s_root) {
1417 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1419 up_write(&s->s_umount);
1420 deactivate_super(s);
1421 return ERR_PTR(error);
1423 s->s_flags |= MS_ACTIVE;
1427 static void nfs_kill_super(struct super_block *s)
1429 struct nfs_server *server = NFS_SB(s);
1434 static struct file_system_type nfs_fs_type = {
1435 .owner = THIS_MODULE,
1437 .get_sb = nfs_get_sb,
1438 .kill_sb = nfs_kill_super,
1439 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1442 #ifdef CONFIG_NFS_V4
1444 static void nfs4_clear_inode(struct inode *);
1446 static struct super_operations nfs4_sops = {
1447 .alloc_inode = nfs_alloc_inode,
1448 .destroy_inode = nfs_destroy_inode,
1449 .write_inode = nfs_write_inode,
1450 .delete_inode = nfs_delete_inode,
1451 .put_super = nfs_put_super,
1452 .statfs = nfs_statfs,
1453 .clear_inode = nfs4_clear_inode,
1454 .umount_begin = nfs_umount_begin,
1455 .show_options = nfs_show_options,
1459 * Clean out any remaining NFSv4 state that might be left over due
1460 * to open() calls that passed nfs_atomic_lookup, but failed to call
1463 static void nfs4_clear_inode(struct inode *inode)
1465 struct nfs_inode *nfsi = NFS_I(inode);
1467 while (!list_empty(&nfsi->open_states)) {
1468 struct nfs4_state *state;
1470 state = list_entry(nfsi->open_states.next,
1473 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1476 (long long)NFS_FILEID(inode),
1478 BUG_ON(atomic_read(&state->count) != 1);
1479 nfs4_close_state(state, state->state);
1481 /* Now call standard NFS clear_inode() code */
1482 nfs_clear_inode(inode);
1486 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1488 struct nfs_server *server;
1489 struct nfs4_client *clp = NULL;
1490 struct rpc_xprt *xprt = NULL;
1491 struct rpc_clnt *clnt = NULL;
1492 struct rpc_timeout timeparms;
1493 rpc_authflavor_t authflavour;
1494 int proto, err = -EIO;
1496 sb->s_blocksize_bits = 0;
1497 sb->s_blocksize = 0;
1498 server = NFS_SB(sb);
1499 if (data->rsize != 0)
1500 server->rsize = nfs_block_size(data->rsize, NULL);
1501 if (data->wsize != 0)
1502 server->wsize = nfs_block_size(data->wsize, NULL);
1503 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1505 /* NFSv4 doesn't use NLM locking */
1506 server->flags |= NFS_MOUNT_NONLM;
1508 server->acregmin = data->acregmin*HZ;
1509 server->acregmax = data->acregmax*HZ;
1510 server->acdirmin = data->acdirmin*HZ;
1511 server->acdirmax = data->acdirmax*HZ;
1513 server->rpc_ops = &nfs_v4_clientops;
1514 /* Initialize timeout values */
1516 timeparms.to_initval = data->timeo * HZ / 10;
1517 timeparms.to_retries = data->retrans;
1518 timeparms.to_exponential = 1;
1519 if (!timeparms.to_retries)
1520 timeparms.to_retries = 5;
1522 proto = data->proto;
1523 /* Which IP protocol do we use? */
1526 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1527 if (!timeparms.to_initval)
1528 timeparms.to_initval = 600 * HZ / 10;
1531 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1532 if (!timeparms.to_initval)
1533 timeparms.to_initval = 11 * HZ / 10;
1539 clp = nfs4_get_client(&server->addr.sin_addr);
1541 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1545 /* Now create transport and client */
1546 authflavour = RPC_AUTH_UNIX;
1547 if (data->auth_flavourlen != 0) {
1548 if (data->auth_flavourlen > 1)
1549 printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1550 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1556 down_write(&clp->cl_sem);
1557 if (clp->cl_rpcclient == NULL) {
1558 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1560 up_write(&clp->cl_sem);
1561 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1562 err = PTR_ERR(xprt);
1565 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1566 server->rpc_ops->version, authflavour);
1568 up_write(&clp->cl_sem);
1569 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1571 err = PTR_ERR(clnt);
1574 clnt->cl_chatty = 1;
1575 clp->cl_rpcclient = clnt;
1576 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1577 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1580 if (list_empty(&clp->cl_superblocks))
1581 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1582 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1583 clnt = rpc_clone_client(clp->cl_rpcclient);
1585 server->nfs4_state = clp;
1586 up_write(&clp->cl_sem);
1590 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1591 err = PTR_ERR(clnt);
1592 goto out_remove_list;
1595 clnt->cl_intr = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1596 clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1597 server->client = clnt;
1600 if (server->nfs4_state->cl_idmap == NULL) {
1601 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1605 if (clnt->cl_auth->au_flavor != authflavour) {
1606 if (rpcauth_create(authflavour, clnt) == NULL) {
1607 printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1612 /* Fire up rpciod if not yet running */
1613 if (rpciod_up() != 0) {
1614 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1618 sb->s_op = &nfs4_sops;
1619 err = nfs_sb_init(sb, authflavour);
1624 rpc_shutdown_client(server->client);
1626 down_write(&server->nfs4_state->cl_sem);
1627 list_del_init(&server->nfs4_siblings);
1628 up_write(&server->nfs4_state->cl_sem);
1629 destroy_nfsv4_state(server);
1632 nfs4_put_client(clp);
1636 static int nfs4_compare_super(struct super_block *sb, void *data)
1638 struct nfs_server *server = data;
1639 struct nfs_server *old = NFS_SB(sb);
1641 if (strcmp(server->hostname, old->hostname) != 0)
1643 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1649 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1654 return ERR_PTR(-EINVAL);
1655 if (src->len < maxlen)
1658 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1660 return ERR_PTR(-ENOMEM);
1662 if (copy_from_user(dst, src->data, maxlen)) {
1665 return ERR_PTR(-EFAULT);
1671 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1672 int flags, const char *dev_name, void *raw_data)
1675 struct nfs_server *server;
1676 struct super_block *s;
1677 struct nfs4_mount_data *data = raw_data;
1681 printk("nfs_read_super: missing data argument\n");
1682 return ERR_PTR(-EINVAL);
1685 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1687 return ERR_PTR(-ENOMEM);
1688 memset(server, 0, sizeof(struct nfs_server));
1689 /* Zero out the NFS state stuff */
1690 init_nfsv4_state(server);
1692 if (data->version != NFS4_MOUNT_VERSION) {
1693 printk("nfs warning: mount version %s than kernel\n",
1694 data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1697 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1700 server->hostname = p;
1702 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1705 server->mnt_path = p;
1707 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1708 sizeof(server->ip_addr) - 1);
1712 /* We now require that the mount process passes the remote address */
1713 if (data->host_addrlen != sizeof(server->addr)) {
1714 s = ERR_PTR(-EINVAL);
1717 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1718 s = ERR_PTR(-EFAULT);
1721 if (server->addr.sin_family != AF_INET ||
1722 server->addr.sin_addr.s_addr == INADDR_ANY) {
1723 printk("NFS: mount program didn't pass remote IP address!\n");
1724 s = ERR_PTR(-EINVAL);
1728 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1730 if (IS_ERR(s) || s->s_root)
1735 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1737 up_write(&s->s_umount);
1738 deactivate_super(s);
1739 return ERR_PTR(error);
1741 s->s_flags |= MS_ACTIVE;
1744 s = (struct super_block *)p;
1746 if (server->mnt_path)
1747 kfree(server->mnt_path);
1748 if (server->hostname)
1749 kfree(server->hostname);
1754 static struct file_system_type nfs4_fs_type = {
1755 .owner = THIS_MODULE,
1757 .get_sb = nfs4_get_sb,
1758 .kill_sb = nfs_kill_super,
1759 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1762 #define nfs4_zero_state(nfsi) \
1764 INIT_LIST_HEAD(&(nfsi)->open_states); \
1766 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1767 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1769 #define nfs4_zero_state(nfsi) \
1771 #define register_nfs4fs() (0)
1772 #define unregister_nfs4fs()
1775 extern int nfs_init_nfspagecache(void);
1776 extern void nfs_destroy_nfspagecache(void);
1777 extern int nfs_init_readpagecache(void);
1778 extern int nfs_destroy_readpagecache(void);
1779 extern int nfs_init_writepagecache(void);
1780 extern int nfs_destroy_writepagecache(void);
1782 static kmem_cache_t * nfs_inode_cachep;
1784 static struct inode *nfs_alloc_inode(struct super_block *sb)
1786 struct nfs_inode *nfsi;
1787 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1791 nfsi->mm_cred = NULL;
1792 nfs4_zero_state(nfsi);
1793 return &nfsi->vfs_inode;
1796 static void nfs_destroy_inode(struct inode *inode)
1798 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1801 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1803 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1805 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1806 SLAB_CTOR_CONSTRUCTOR) {
1807 inode_init_once(&nfsi->vfs_inode);
1808 INIT_LIST_HEAD(&nfsi->dirty);
1809 INIT_LIST_HEAD(&nfsi->commit);
1810 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1811 atomic_set(&nfsi->data_updates, 0);
1815 init_waitqueue_head(&nfsi->nfs_i_wait);
1819 int nfs_init_inodecache(void)
1821 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1822 sizeof(struct nfs_inode),
1823 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1825 if (nfs_inode_cachep == NULL)
1831 void nfs_destroy_inodecache(void)
1833 if (kmem_cache_destroy(nfs_inode_cachep))
1834 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1840 static int __init init_nfs_fs(void)
1844 err = nfs_init_nfspagecache();
1848 err = nfs_init_inodecache();
1852 err = nfs_init_readpagecache();
1856 err = nfs_init_writepagecache();
1860 #ifdef CONFIG_PROC_FS
1861 rpc_proc_register(&nfs_rpcstat);
1863 err = register_filesystem(&nfs_fs_type);
1866 if ((err = register_nfs4fs()) != 0)
1870 rpc_proc_unregister("nfs");
1871 nfs_destroy_writepagecache();
1873 nfs_destroy_readpagecache();
1875 nfs_destroy_inodecache();
1877 nfs_destroy_nfspagecache();
1882 static void __exit exit_nfs_fs(void)
1884 nfs_destroy_writepagecache();
1885 nfs_destroy_readpagecache();
1886 nfs_destroy_inodecache();
1887 nfs_destroy_nfspagecache();
1888 #ifdef CONFIG_PROC_FS
1889 rpc_proc_unregister("nfs");
1891 unregister_filesystem(&nfs_fs_type);
1892 unregister_nfs4fs();
1895 /* Not quite true; I just maintain it */
1896 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1897 MODULE_LICENSE("GPL");
1899 module_init(init_nfs_fs)
1900 module_exit(exit_nfs_fs)