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>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
42 #define NFSDBG_FACILITY NFSDBG_VFS
43 #define NFS_PARANOIA 1
45 /* Maximum number of readahead requests
46 * FIXME: this should really be a sysctl so that users may tune it to suit
47 * their needs. People that do NFS over a slow network, might for
48 * instance want to reduce it to something closer to 1 for improved
49 * interactive response.
51 #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
53 static void nfs_invalidate_inode(struct inode *);
54 static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
56 static struct inode *nfs_alloc_inode(struct super_block *sb);
57 static void nfs_destroy_inode(struct inode *);
58 static void nfs_write_inode(struct inode *,int);
59 static void nfs_delete_inode(struct inode *);
60 static void nfs_put_super(struct super_block *);
61 static void nfs_clear_inode(struct inode *);
62 static void nfs_umount_begin(struct super_block *);
63 static int nfs_statfs(struct super_block *, struct kstatfs *);
64 static int nfs_show_options(struct seq_file *, struct vfsmount *);
66 static struct super_operations nfs_sops = {
67 .alloc_inode = nfs_alloc_inode,
68 .destroy_inode = nfs_destroy_inode,
69 .write_inode = nfs_write_inode,
70 .delete_inode = nfs_delete_inode,
71 .put_super = nfs_put_super,
73 .clear_inode = nfs_clear_inode,
74 .umount_begin = nfs_umount_begin,
75 .show_options = nfs_show_options,
81 struct rpc_stat nfs_rpcstat = {
82 .program = &nfs_program
84 static struct rpc_version * nfs_version[] = {
88 #if defined(CONFIG_NFS_V3)
90 #elif defined(CONFIG_NFS_V4)
93 #if defined(CONFIG_NFS_V4)
98 struct rpc_program nfs_program = {
100 .number = NFS_PROGRAM,
101 .nrvers = sizeof(nfs_version) / sizeof(nfs_version[0]),
102 .version = nfs_version,
103 .stats = &nfs_rpcstat,
104 .pipe_dir_name = "/nfs",
107 static inline unsigned long
108 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
110 return nfs_fileid_to_ino_t(fattr->fileid);
114 nfs_write_inode(struct inode *inode, int sync)
116 int flags = sync ? FLUSH_WAIT : 0;
118 nfs_commit_inode(inode, 0, 0, flags);
122 nfs_delete_inode(struct inode * inode)
124 dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
127 * The following can never actually happen...
129 if (nfs_have_writebacks(inode)) {
130 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
137 * For the moment, the only task for the NFS clear_inode method is to
138 * release the mmap credential
141 nfs_clear_inode(struct inode *inode)
143 struct nfs_inode *nfsi = NFS_I(inode);
144 struct rpc_cred *cred = nfsi->mm_cred;
148 cred = nfsi->cache_access.cred;
151 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
155 nfs_put_super(struct super_block *sb)
157 struct nfs_server *server = NFS_SB(sb);
159 nfs4_renewd_prepare_shutdown(server);
161 if (server->client != NULL)
162 rpc_shutdown_client(server->client);
163 if (server->client_sys != NULL)
164 rpc_shutdown_client(server->client_sys);
166 if (!(server->flags & NFS_MOUNT_NONLM))
167 lockd_down(); /* release rpc.lockd */
168 rpciod_down(); /* release rpciod */
170 destroy_nfsv4_state(server);
172 kfree(server->hostname);
176 nfs_umount_begin(struct super_block *sb)
178 struct nfs_server *server = NFS_SB(sb);
179 struct rpc_clnt *rpc;
181 /* -EIO all pending I/O */
182 if ((rpc = server->client) != NULL)
183 rpc_killall_tasks(rpc);
187 static inline unsigned long
188 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
190 /* make sure blocksize is a power of two */
191 if ((bsize & (bsize - 1)) || nrbitsp) {
192 unsigned char nrbits;
194 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
205 * Calculate the number of 512byte blocks used.
207 static inline unsigned long
208 nfs_calc_block_size(u64 tsize)
210 loff_t used = (tsize + 511) >> 9;
211 return (used > ULONG_MAX) ? ULONG_MAX : used;
215 * Compute and set NFS server blocksize
217 static inline unsigned long
218 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
221 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
222 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
223 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
225 return nfs_block_bits(bsize, nrbitsp);
229 * Obtain the root inode of the file system.
231 static struct inode *
232 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
234 struct nfs_server *server = NFS_SB(sb);
238 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
240 printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error);
241 return ERR_PTR(error);
244 rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
246 return ERR_PTR(-ENOMEM);
251 * Do NFS version-independent mount processing, and sanity checking
254 nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
256 struct nfs_server *server;
257 struct inode *root_inode;
258 struct nfs_fattr fattr;
259 struct nfs_fsinfo fsinfo = {
262 struct nfs_pathconf pathinfo = {
266 /* We probably want something more informative here */
267 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
271 sb->s_magic = NFS_SUPER_MAGIC;
273 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
274 /* Did getting the root inode fail? */
275 if (IS_ERR(root_inode))
277 sb->s_root = d_alloc_root(root_inode);
281 sb->s_root->d_op = server->rpc_ops->dentry_ops;
283 /* Get some general file system info */
284 if (server->namelen == 0 &&
285 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
286 server->namelen = pathinfo.max_namelen;
287 /* Work out a lot of parameters */
288 if (server->rsize == 0)
289 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
290 if (server->wsize == 0)
291 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
292 if (sb->s_blocksize == 0) {
293 if (fsinfo.wtmult == 0) {
294 sb->s_blocksize = 512;
295 sb->s_blocksize_bits = 9;
297 sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
298 &sb->s_blocksize_bits);
301 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
302 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
303 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
304 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
306 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
307 if (server->rpages > NFS_READ_MAXIOV) {
308 server->rpages = NFS_READ_MAXIOV;
309 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
312 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
313 if (server->wpages > NFS_WRITE_MAXIOV) {
314 server->wpages = NFS_WRITE_MAXIOV;
315 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
318 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
319 if (server->dtsize > PAGE_CACHE_SIZE)
320 server->dtsize = PAGE_CACHE_SIZE;
321 if (server->dtsize > server->rsize)
322 server->dtsize = server->rsize;
324 if (server->flags & NFS_MOUNT_NOAC) {
325 server->acregmin = server->acregmax = 0;
326 server->acdirmin = server->acdirmax = 0;
327 sb->s_flags |= MS_SYNCHRONOUS;
329 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
331 sb->s_maxbytes = fsinfo.maxfilesize;
332 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
333 sb->s_maxbytes = MAX_LFS_FILESIZE;
335 /* We're airborne Set socket buffersize */
336 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
338 /* Yargs. It didn't work out. */
340 printk("nfs_read_super: get root inode failed\n");
341 if (!IS_ERR(root_inode))
347 * Create an RPC client handle.
349 static struct rpc_clnt *
350 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
352 struct rpc_timeout timeparms;
353 struct rpc_xprt *xprt = NULL;
354 struct rpc_clnt *clnt = NULL;
355 int tcp = (data->flags & NFS_MOUNT_TCP);
357 /* Initialize timeout values */
358 timeparms.to_initval = data->timeo * HZ / 10;
359 timeparms.to_retries = data->retrans;
360 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
361 timeparms.to_exponential = 1;
363 if (!timeparms.to_initval)
364 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
365 if (!timeparms.to_retries)
366 timeparms.to_retries = 5;
368 /* create transport and client */
369 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
370 &server->addr, &timeparms);
372 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
373 return (struct rpc_clnt *)xprt;
375 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
376 server->rpc_ops->version, data->pseudoflavor);
378 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
382 clnt->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
383 clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
384 clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
395 * The way this works is that the mount process passes a structure
396 * in the data argument which contains the server's IP address
397 * and the root file handle obtained from the server's mount
398 * daemon. We stash these away in the private superblock fields.
401 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
403 struct nfs_server *server;
405 rpc_authflavor_t authflavor;
408 sb->s_blocksize_bits = 0;
411 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
413 server->rsize = nfs_block_size(data->rsize, NULL);
415 server->wsize = nfs_block_size(data->wsize, NULL);
416 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
418 server->acregmin = data->acregmin*HZ;
419 server->acregmax = data->acregmax*HZ;
420 server->acdirmin = data->acdirmin*HZ;
421 server->acdirmax = data->acdirmax*HZ;
423 server->namelen = data->namlen;
424 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
425 if (!server->hostname)
427 strcpy(server->hostname, data->hostname);
429 /* Check NFS protocol revision and initialize RPC op vector
430 * and file handle pool. */
431 if (server->flags & NFS_MOUNT_VER3) {
433 server->rpc_ops = &nfs_v3_clientops;
434 server->caps |= NFS_CAP_READDIRPLUS;
435 if (data->version < 4) {
436 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
440 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
444 server->rpc_ops = &nfs_v2_clientops;
447 /* Fill in pseudoflavor for mount version < 5 */
448 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
449 data->pseudoflavor = RPC_AUTH_UNIX;
450 authflavor = data->pseudoflavor; /* save for sb_init() */
451 /* XXX maybe we want to add a server->pseudoflavor field */
453 /* Create RPC client handles */
454 server->client = nfs_create_client(server, data);
455 if (server->client == NULL)
457 /* RFC 2623, sec 2.3.2 */
458 if (authflavor != RPC_AUTH_UNIX) {
459 server->client_sys = rpc_clone_client(server->client);
460 if (server->client_sys == NULL)
462 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
465 atomic_inc(&server->client->cl_count);
466 server->client_sys = server->client;
469 /* Fire up rpciod if not yet running */
470 if (rpciod_up() != 0) {
471 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
475 sb->s_op = &nfs_sops;
476 err = nfs_sb_init(sb, authflavor);
480 if (server->flags & NFS_MOUNT_VER3) {
481 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
482 server->namelen = NFS3_MAXNAMLEN;
484 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
485 server->namelen = NFS2_MAXNAMLEN;
488 /* Check whether to start the lockd process */
489 if (!(server->flags & NFS_MOUNT_NONLM))
496 rpc_shutdown_client(server->client);
497 if (server->client_sys)
498 rpc_shutdown_client(server->client_sys);
500 if (server->hostname)
501 kfree(server->hostname);
506 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
508 struct nfs_server *server = NFS_SB(sb);
509 unsigned char blockbits;
510 unsigned long blockres;
511 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
512 struct nfs_fattr fattr;
513 struct nfs_fsstat res = {
520 error = server->rpc_ops->statfs(server, rootfh, &res);
521 buf->f_type = NFS_SUPER_MAGIC;
525 buf->f_bsize = sb->s_blocksize;
526 blockbits = sb->s_blocksize_bits;
527 blockres = (1 << blockbits) - 1;
528 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
529 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
530 buf->f_bavail = (res.abytes + blockres) >> blockbits;
531 buf->f_files = res.tfiles;
532 buf->f_ffree = res.afiles;
534 buf->f_namelen = server->namelen;
541 printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
542 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
547 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
549 static struct proc_nfs_info {
554 { NFS_MOUNT_SOFT, ",soft", ",hard" },
555 { NFS_MOUNT_INTR, ",intr", "" },
556 { NFS_MOUNT_POSIX, ",posix", "" },
557 { NFS_MOUNT_TCP, ",tcp", ",udp" },
558 { NFS_MOUNT_NOCTO, ",nocto", "" },
559 { NFS_MOUNT_NOAC, ",noac", "" },
560 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
561 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
564 struct proc_nfs_info *nfs_infop;
565 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
567 seq_printf(m, ",v%d", nfss->rpc_ops->version);
568 seq_printf(m, ",rsize=%d", nfss->rsize);
569 seq_printf(m, ",wsize=%d", nfss->wsize);
570 if (nfss->acregmin != 3*HZ)
571 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
572 if (nfss->acregmax != 60*HZ)
573 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
574 if (nfss->acdirmin != 30*HZ)
575 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
576 if (nfss->acdirmax != 60*HZ)
577 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
578 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
579 if (nfss->flags & nfs_infop->flag)
580 seq_puts(m, nfs_infop->str);
582 seq_puts(m, nfs_infop->nostr);
584 seq_puts(m, ",addr=");
585 seq_escape(m, nfss->hostname, " \t\n\\");
590 * Invalidate the local caches
593 nfs_zap_caches(struct inode *inode)
595 struct nfs_inode *nfsi = NFS_I(inode);
596 int mode = inode->i_mode;
598 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
599 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
601 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
602 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
603 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
605 nfsi->flags |= NFS_INO_INVALID_ATTR;
609 * Invalidate, but do not unhash, the inode
612 nfs_invalidate_inode(struct inode *inode)
614 umode_t save_mode = inode->i_mode;
616 make_bad_inode(inode);
617 inode->i_mode = save_mode;
618 nfs_zap_caches(inode);
621 struct nfs_find_desc {
623 struct nfs_fattr *fattr;
627 * In NFSv3 we can have 64bit inode numbers. In order to support
628 * this, and re-exported directories (also seen in NFSv2)
629 * we are forced to allow 2 different inodes to have the same
633 nfs_find_actor(struct inode *inode, void *opaque)
635 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
636 struct nfs_fh *fh = desc->fh;
637 struct nfs_fattr *fattr = desc->fattr;
639 if (NFS_FILEID(inode) != fattr->fileid)
641 if (memcmp(NFS_FH(inode), fh, sizeof(struct nfs_fh)) != 0)
643 if (is_bad_inode(inode))
649 nfs_init_locked(struct inode *inode, void *opaque)
651 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
652 struct nfs_fh *fh = desc->fh;
653 struct nfs_fattr *fattr = desc->fattr;
655 NFS_FILEID(inode) = fattr->fileid;
656 memcpy(NFS_FH(inode), fh, sizeof(struct nfs_fh));
660 /* Don't use READDIRPLUS on directories that we believe are too large */
661 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
664 * This is our front-end to iget that looks up inodes by file handle
665 * instead of inode number.
668 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
670 struct nfs_find_desc desc = {
674 struct inode *inode = NULL;
677 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
681 printk("NFS: Buggy server - nlink == 0!\n");
685 hash = nfs_fattr_to_ino_t(fattr);
687 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
690 if (inode->i_state & I_NEW) {
691 struct nfs_inode *nfsi = NFS_I(inode);
693 /* We set i_ino for the few things that still rely on it,
697 /* We can't support update_atime(), since the server will reset it */
698 inode->i_flags |= S_NOATIME|S_NOCMTIME;
699 inode->i_mode = fattr->mode;
700 /* Why so? Because we want revalidate for devices/FIFOs, and
701 * that's precisely what we have in nfs_file_inode_operations.
703 inode->i_op = &nfs_file_inode_operations;
704 if (S_ISREG(inode->i_mode)) {
705 inode->i_fop = &nfs_file_operations;
706 inode->i_data.a_ops = &nfs_file_aops;
707 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
708 } else if (S_ISDIR(inode->i_mode)) {
709 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
710 inode->i_fop = &nfs_dir_operations;
711 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
712 && fattr->size <= NFS_LIMIT_READDIRPLUS)
713 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
714 } else if (S_ISLNK(inode->i_mode))
715 inode->i_op = &nfs_symlink_inode_operations;
717 init_special_inode(inode, inode->i_mode, fattr->rdev);
719 nfsi->read_cache_jiffies = fattr->timestamp;
720 inode->i_atime = fattr->atime;
721 inode->i_mtime = fattr->mtime;
722 inode->i_ctime = fattr->ctime;
723 if (fattr->valid & NFS_ATTR_FATTR_V4)
724 nfsi->change_attr = fattr->change_attr;
725 inode->i_size = nfs_size_to_loff_t(fattr->size);
726 inode->i_nlink = fattr->nlink;
727 inode->i_uid = fattr->uid;
728 inode->i_gid = fattr->gid;
729 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
731 * report the blocks in 512byte units
733 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
734 inode->i_blksize = inode->i_sb->s_blocksize;
736 inode->i_blocks = fattr->du.nfs2.blocks;
737 inode->i_blksize = fattr->du.nfs2.blocksize;
739 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
740 nfsi->attrtimeo_timestamp = jiffies;
741 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
742 nfsi->cache_access.cred = NULL;
744 unlock_new_inode(inode);
746 nfs_refresh_inode(inode, fattr);
747 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
749 (long long)NFS_FILEID(inode),
750 atomic_read(&inode->i_count));
756 printk("nfs_fhget: iget failed\n");
760 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
763 nfs_setattr(struct dentry *dentry, struct iattr *attr)
765 struct inode *inode = dentry->d_inode;
766 struct nfs_fattr fattr;
769 if (attr->ia_valid & ATTR_SIZE) {
770 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
771 attr->ia_valid &= ~ATTR_SIZE;
774 /* Optimization: if the end result is no change, don't RPC */
775 attr->ia_valid &= NFS_VALID_ATTRS;
776 if (attr->ia_valid == 0)
780 nfs_begin_data_update(inode);
781 /* Write all dirty data if we're changing file permissions or size */
782 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
783 if (filemap_fdatawrite(inode->i_mapping) == 0)
784 filemap_fdatawait(inode->i_mapping);
787 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
789 nfs_refresh_inode(inode, &fattr);
790 if ((attr->ia_valid & ATTR_MODE) != 0) {
792 mode = inode->i_mode & ~S_IALLUGO;
793 mode |= attr->ia_mode & S_IALLUGO;
794 inode->i_mode = mode;
796 if ((attr->ia_valid & ATTR_UID) != 0)
797 inode->i_uid = attr->ia_uid;
798 if ((attr->ia_valid & ATTR_GID) != 0)
799 inode->i_gid = attr->ia_gid;
800 if ((attr->ia_valid & ATTR_SIZE) != 0) {
801 inode->i_size = attr->ia_size;
802 vmtruncate(inode, attr->ia_size);
805 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
806 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
812 nfs_end_data_update(inode);
818 * Wait for the inode to get unlocked.
819 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
822 nfs_wait_on_inode(struct inode *inode, int flag)
824 struct rpc_clnt *clnt = NFS_CLIENT(inode);
825 struct nfs_inode *nfsi = NFS_I(inode);
828 if (!(NFS_FLAGS(inode) & flag))
830 atomic_inc(&inode->i_count);
831 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
832 !(NFS_FLAGS(inode) & flag));
837 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
839 struct inode *inode = dentry->d_inode;
840 struct nfs_inode *nfsi = NFS_I(inode);
841 int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
844 if (__IS_FLG(inode, MS_NOATIME))
846 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
848 /* We may force a getattr if the user cares about atime */
850 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
852 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
854 generic_fillattr(inode, stat);
859 * Ensure that mmap has a recent RPC credential for use when writing out
863 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
865 struct rpc_cred **p = &NFS_I(inode)->mm_cred,
868 *p = get_rpccred(cred);
870 put_rpccred(oldcred);
874 * These are probably going to contain hooks for
875 * allocating and releasing RPC credentials for
876 * the file. I'll have to think about Tronds patch
879 int nfs_open(struct inode *inode, struct file *filp)
881 struct rpc_auth *auth;
882 struct rpc_cred *cred;
884 auth = NFS_CLIENT(inode)->cl_auth;
885 cred = rpcauth_lookupcred(auth, 0);
886 filp->private_data = cred;
887 if ((filp->f_mode & FMODE_WRITE) != 0) {
888 nfs_set_mmcred(inode, cred);
889 nfs_begin_data_update(inode);
894 int nfs_release(struct inode *inode, struct file *filp)
896 struct rpc_cred *cred;
899 if ((filp->f_mode & FMODE_WRITE) != 0)
900 nfs_end_data_update(inode);
901 cred = nfs_file_cred(filp);
909 * This function is called whenever some part of NFS notices that
910 * the cached attributes have to be refreshed.
913 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
915 int status = -ESTALE;
916 struct nfs_fattr fattr;
917 struct nfs_inode *nfsi = NFS_I(inode);
918 unsigned long verifier;
921 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
922 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
925 if (!inode || is_bad_inode(inode))
927 if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
930 while (NFS_REVALIDATING(inode)) {
931 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
934 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
936 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
938 status = NFS_STALE(inode) ? -ESTALE : 0;
941 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
943 /* Protect against RPC races by saving the change attribute */
944 verifier = nfs_save_change_attribute(inode);
945 status = NFS_PROTO(inode)->getattr(inode, &fattr);
947 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
949 (long long)NFS_FILEID(inode), status);
950 if (status == -ESTALE) {
951 NFS_FLAGS(inode) |= NFS_INO_STALE;
952 if (inode != inode->i_sb->s_root->d_inode)
953 remove_inode_hash(inode);
958 status = nfs_update_inode(inode, &fattr, verifier);
960 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
962 (long long)NFS_FILEID(inode), status);
967 * We may need to keep the attributes marked as invalid if
968 * we raced with nfs_end_attr_update().
970 if (verifier == nfsi->cache_change_attribute)
971 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
972 /* Do the page cache invalidation */
973 if (flags & NFS_INO_INVALID_DATA) {
974 if (S_ISREG(inode->i_mode)) {
975 if (filemap_fdatawrite(inode->i_mapping) == 0)
976 filemap_fdatawait(inode->i_mapping);
979 nfsi->flags &= ~NFS_INO_INVALID_DATA;
980 invalidate_inode_pages2(inode->i_mapping);
981 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
982 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
984 (long long)NFS_FILEID(inode));
985 /* This ensures we revalidate dentries */
986 nfsi->cache_change_attribute++;
988 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
990 (long long)NFS_FILEID(inode));
992 NFS_FLAGS(inode) &= ~NFS_INO_STALE;
994 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
995 wake_up(&nfsi->nfs_i_wait);
1002 * nfs_begin_data_update
1003 * @inode - pointer to inode
1004 * Declare that a set of operations will update file data on the server
1006 void nfs_begin_data_update(struct inode *inode)
1008 atomic_inc(&NFS_I(inode)->data_updates);
1012 * nfs_end_data_update
1013 * @inode - pointer to inode
1014 * Declare end of the operations that will update file data
1015 * This will mark the inode as immediately needing revalidation
1016 * of its attribute cache.
1018 void nfs_end_data_update(struct inode *inode)
1020 struct nfs_inode *nfsi = NFS_I(inode);
1022 /* Mark the attribute cache for revalidation */
1023 nfsi->flags |= NFS_INO_INVALID_ATTR;
1024 /* Directories and symlinks: invalidate page cache too */
1025 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1026 nfsi->flags |= NFS_INO_INVALID_DATA;
1027 nfsi->cache_change_attribute ++;
1028 atomic_dec(&nfsi->data_updates);
1032 * nfs_end_data_update_defer
1033 * @inode - pointer to inode
1034 * Declare end of the operations that will update file data
1035 * This will defer marking the inode as needing revalidation
1036 * unless there are no other pending updates.
1038 void nfs_end_data_update_defer(struct inode *inode)
1040 struct nfs_inode *nfsi = NFS_I(inode);
1042 if (atomic_dec_and_test(&nfsi->data_updates)) {
1043 /* Mark the attribute cache for revalidation */
1044 nfsi->flags |= NFS_INO_INVALID_ATTR;
1045 /* Directories and symlinks: invalidate page cache too */
1046 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1047 nfsi->flags |= NFS_INO_INVALID_DATA;
1048 nfsi->cache_change_attribute ++;
1053 * nfs_refresh_inode - verify consistency of the inode attribute cache
1054 * @inode - pointer to inode
1055 * @fattr - updated attributes
1057 * Verifies the attribute cache. If we have just changed the attributes,
1058 * so that fattr carries weak cache consistency data, then it may
1059 * also update the ctime/mtime/change_attribute.
1061 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1063 struct nfs_inode *nfsi = NFS_I(inode);
1064 loff_t cur_size, new_isize;
1067 /* Are we in the process of updating data on the server? */
1068 data_unstable = nfs_caches_unstable(inode);
1070 if (fattr->valid & NFS_ATTR_FATTR_V4) {
1071 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1072 && nfsi->change_attr == fattr->pre_change_attr)
1073 nfsi->change_attr = fattr->change_attr;
1074 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1075 nfsi->flags |= NFS_INO_INVALID_ATTR;
1078 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1081 /* Has the inode gone and changed behind our back? */
1082 if (nfsi->fileid != fattr->fileid
1083 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1086 cur_size = i_size_read(inode);
1087 new_isize = nfs_size_to_loff_t(fattr->size);
1089 /* If we have atomic WCC data, we may update some attributes */
1090 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1091 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1092 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1093 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1094 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1097 /* Verify a few of the more important attributes */
1098 if (!data_unstable) {
1099 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1100 || cur_size != new_isize)
1101 nfsi->flags |= NFS_INO_INVALID_ATTR;
1102 } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1103 nfsi->flags |= NFS_INO_INVALID_ATTR;
1105 /* Have any file permissions changed? */
1106 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1107 || inode->i_uid != fattr->uid
1108 || inode->i_gid != fattr->gid)
1109 nfsi->flags |= NFS_INO_INVALID_ATTR;
1111 /* Has the link count changed? */
1112 if (inode->i_nlink != fattr->nlink)
1113 nfsi->flags |= NFS_INO_INVALID_ATTR;
1115 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1116 nfsi->flags |= NFS_INO_INVALID_ATIME;
1118 nfsi->read_cache_jiffies = fattr->timestamp;
1123 * Many nfs protocol calls return the new file attributes after
1124 * an operation. Here we update the inode to reflect the state
1125 * of the server's inode.
1127 * This is a bit tricky because we have to make sure all dirty pages
1128 * have been sent off to the server before calling invalidate_inode_pages.
1129 * To make sure no other process adds more write requests while we try
1130 * our best to flush them, we make them sleep during the attribute refresh.
1132 * A very similar scenario holds for the dir cache.
1134 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1136 struct nfs_inode *nfsi = NFS_I(inode);
1139 unsigned int invalid = 0;
1143 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1144 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1145 atomic_read(&inode->i_count), fattr->valid);
1147 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1150 if (nfsi->fileid != fattr->fileid) {
1151 printk(KERN_ERR "%s: inode number mismatch\n"
1152 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1154 inode->i_sb->s_id, (long long)nfsi->fileid,
1155 inode->i_sb->s_id, (long long)fattr->fileid);
1160 * Make sure the inode's type hasn't changed.
1162 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1166 * Update the read time so we don't revalidate too often.
1168 nfsi->read_cache_jiffies = fattr->timestamp;
1170 /* Are we racing with known updates of the metadata on the server? */
1171 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1173 /* Check if the file size agrees */
1174 new_size = fattr->size;
1175 new_isize = nfs_size_to_loff_t(fattr->size);
1176 cur_isize = i_size_read(inode);
1177 if (cur_isize != new_size) {
1178 #ifdef NFS_DEBUG_VERBOSE
1179 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1182 * If we have pending writebacks, things can get
1185 if (S_ISREG(inode->i_mode) && data_unstable) {
1186 if (new_isize > cur_isize) {
1187 inode->i_size = new_isize;
1188 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1191 inode->i_size = new_isize;
1192 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1197 * Note: we don't check inode->i_mtime since pipes etc.
1198 * can change this value in VFS without requiring a
1199 * cache revalidation.
1201 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1202 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1203 #ifdef NFS_DEBUG_VERBOSE
1204 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1207 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1210 if ((fattr->valid & NFS_ATTR_FATTR_V4)
1211 && nfsi->change_attr != fattr->change_attr) {
1212 #ifdef NFS_DEBUG_VERBOSE
1213 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1214 inode->i_sb->s_id, inode->i_ino);
1216 nfsi->change_attr = fattr->change_attr;
1218 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1221 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1222 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1224 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1225 inode->i_uid != fattr->uid ||
1226 inode->i_gid != fattr->gid) {
1227 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1232 invalid |= NFS_INO_INVALID_ATTR;
1235 inode->i_mode = fattr->mode;
1236 inode->i_nlink = fattr->nlink;
1237 inode->i_uid = fattr->uid;
1238 inode->i_gid = fattr->gid;
1240 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1242 * report the blocks in 512byte units
1244 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1245 inode->i_blksize = inode->i_sb->s_blocksize;
1247 inode->i_blocks = fattr->du.nfs2.blocks;
1248 inode->i_blksize = fattr->du.nfs2.blocksize;
1251 /* Update attrtimeo value if we're out of the unstable period */
1252 if (invalid & NFS_INO_INVALID_ATTR) {
1253 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1254 nfsi->attrtimeo_timestamp = jiffies;
1255 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1256 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1257 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1258 nfsi->attrtimeo_timestamp = jiffies;
1260 /* Don't invalidate the data if we were to blame */
1261 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1262 || S_ISLNK(inode->i_mode)))
1263 invalid &= ~NFS_INO_INVALID_DATA;
1264 nfsi->flags |= invalid;
1269 * Big trouble! The inode has become a different object.
1272 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1273 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1276 * No need to worry about unhashing the dentry, as the
1277 * lookup validation will know that the inode is bad.
1278 * (But we fall through to invalidate the caches.)
1280 nfs_invalidate_inode(inode);
1286 * File system information
1289 static int nfs_set_super(struct super_block *s, void *data)
1291 s->s_fs_info = data;
1292 return set_anon_super(s, data);
1295 static int nfs_compare_super(struct super_block *sb, void *data)
1297 struct nfs_server *server = data;
1298 struct nfs_server *old = NFS_SB(sb);
1300 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1302 if (old->addr.sin_port != server->addr.sin_port)
1304 return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1307 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1308 int flags, const char *dev_name, void *raw_data)
1311 struct nfs_server *server;
1312 struct super_block *s;
1313 struct nfs_fh *root;
1314 struct nfs_mount_data *data = raw_data;
1317 printk("nfs_read_super: missing data argument\n");
1318 return ERR_PTR(-EINVAL);
1321 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1323 return ERR_PTR(-ENOMEM);
1324 memset(server, 0, sizeof(struct nfs_server));
1325 /* Zero out the NFS state stuff */
1326 init_nfsv4_state(server);
1329 memcpy(root, &data->root, sizeof(*root));
1330 if (root->size < sizeof(root->data))
1331 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1333 if (data->version != NFS_MOUNT_VERSION) {
1334 printk("nfs warning: mount version %s than kernel\n",
1335 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1336 if (data->version < 2)
1338 if (data->version < 3)
1340 if (data->version < 4) {
1341 data->flags &= ~NFS_MOUNT_VER3;
1342 memset(root, 0, sizeof(*root));
1343 root->size = NFS2_FHSIZE;
1344 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1346 if (data->version < 5)
1347 data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1350 if (root->size > sizeof(root->data)) {
1351 printk("nfs_get_sb: invalid root filehandle\n");
1353 return ERR_PTR(-EINVAL);
1355 /* We now require that the mount process passes the remote address */
1356 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1357 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1358 printk("NFS: mount program didn't pass remote address!\n");
1360 return ERR_PTR(-EINVAL);
1363 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1365 if (IS_ERR(s) || s->s_root) {
1372 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1374 up_write(&s->s_umount);
1375 deactivate_super(s);
1376 return ERR_PTR(error);
1378 s->s_flags |= MS_ACTIVE;
1382 static void nfs_kill_super(struct super_block *s)
1384 struct nfs_server *server = NFS_SB(s);
1389 static struct file_system_type nfs_fs_type = {
1390 .owner = THIS_MODULE,
1392 .get_sb = nfs_get_sb,
1393 .kill_sb = nfs_kill_super,
1394 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1397 #ifdef CONFIG_NFS_V4
1399 static void nfs4_clear_inode(struct inode *);
1401 static struct super_operations nfs4_sops = {
1402 .alloc_inode = nfs_alloc_inode,
1403 .destroy_inode = nfs_destroy_inode,
1404 .write_inode = nfs_write_inode,
1405 .delete_inode = nfs_delete_inode,
1406 .put_super = nfs_put_super,
1407 .statfs = nfs_statfs,
1408 .clear_inode = nfs4_clear_inode,
1409 .umount_begin = nfs_umount_begin,
1410 .show_options = nfs_show_options,
1414 * Clean out any remaining NFSv4 state that might be left over due
1415 * to open() calls that passed nfs_atomic_lookup, but failed to call
1418 static void nfs4_clear_inode(struct inode *inode)
1420 struct nfs_inode *nfsi = NFS_I(inode);
1422 while (!list_empty(&nfsi->open_states)) {
1423 struct nfs4_state *state;
1425 state = list_entry(nfsi->open_states.next,
1428 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1431 (long long)NFS_FILEID(inode),
1433 BUG_ON(atomic_read(&state->count) != 1);
1434 nfs4_close_state(state, state->state);
1436 /* Now call standard NFS clear_inode() code */
1437 nfs_clear_inode(inode);
1441 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1443 struct nfs_server *server;
1444 struct nfs4_client *clp = NULL;
1445 struct rpc_xprt *xprt = NULL;
1446 struct rpc_clnt *clnt = NULL;
1447 struct rpc_timeout timeparms;
1448 rpc_authflavor_t authflavour;
1449 int proto, err = -EIO;
1451 sb->s_blocksize_bits = 0;
1452 sb->s_blocksize = 0;
1453 server = NFS_SB(sb);
1454 if (data->rsize != 0)
1455 server->rsize = nfs_block_size(data->rsize, NULL);
1456 if (data->wsize != 0)
1457 server->wsize = nfs_block_size(data->wsize, NULL);
1458 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1460 /* NFSv4 doesn't use NLM locking */
1461 server->flags |= NFS_MOUNT_NONLM;
1463 server->acregmin = data->acregmin*HZ;
1464 server->acregmax = data->acregmax*HZ;
1465 server->acdirmin = data->acdirmin*HZ;
1466 server->acdirmax = data->acdirmax*HZ;
1468 server->rpc_ops = &nfs_v4_clientops;
1469 /* Initialize timeout values */
1471 timeparms.to_initval = data->timeo * HZ / 10;
1472 timeparms.to_retries = data->retrans;
1473 timeparms.to_exponential = 1;
1474 if (!timeparms.to_retries)
1475 timeparms.to_retries = 5;
1477 proto = data->proto;
1478 /* Which IP protocol do we use? */
1481 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1482 if (!timeparms.to_initval)
1483 timeparms.to_initval = 600 * HZ / 10;
1486 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1487 if (!timeparms.to_initval)
1488 timeparms.to_initval = 11 * HZ / 10;
1494 clp = nfs4_get_client(&server->addr.sin_addr);
1496 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1500 /* Now create transport and client */
1501 authflavour = RPC_AUTH_UNIX;
1502 if (data->auth_flavourlen != 0) {
1503 if (data->auth_flavourlen > 1)
1504 printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1505 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1511 down_write(&clp->cl_sem);
1512 if (clp->cl_rpcclient == NULL) {
1513 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1515 up_write(&clp->cl_sem);
1516 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1517 err = PTR_ERR(xprt);
1520 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1521 server->rpc_ops->version, authflavour);
1523 up_write(&clp->cl_sem);
1524 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1526 err = PTR_ERR(clnt);
1529 clnt->cl_chatty = 1;
1530 clp->cl_rpcclient = clnt;
1531 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1532 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1535 if (list_empty(&clp->cl_superblocks))
1536 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1537 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1538 clnt = rpc_clone_client(clp->cl_rpcclient);
1540 server->nfs4_state = clp;
1541 up_write(&clp->cl_sem);
1545 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1546 err = PTR_ERR(clnt);
1547 goto out_remove_list;
1550 if (server->nfs4_state->cl_idmap == NULL) {
1551 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1555 clnt->cl_intr = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1556 clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1557 server->client = clnt;
1559 if (clnt->cl_auth->au_flavor != authflavour) {
1560 if (rpcauth_create(authflavour, clnt) == NULL) {
1561 printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1566 /* Fire up rpciod if not yet running */
1567 if (rpciod_up() != 0) {
1568 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1572 sb->s_op = &nfs4_sops;
1573 err = nfs_sb_init(sb, authflavour);
1578 rpc_shutdown_client(server->client);
1580 down_write(&server->nfs4_state->cl_sem);
1581 list_del_init(&server->nfs4_siblings);
1582 up_write(&server->nfs4_state->cl_sem);
1583 destroy_nfsv4_state(server);
1586 nfs4_put_client(clp);
1590 static int nfs4_compare_super(struct super_block *sb, void *data)
1592 struct nfs_server *server = data;
1593 struct nfs_server *old = NFS_SB(sb);
1595 if (strcmp(server->hostname, old->hostname) != 0)
1597 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1603 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1608 return ERR_PTR(-EINVAL);
1609 if (src->len < maxlen)
1612 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1614 return ERR_PTR(-ENOMEM);
1616 if (copy_from_user(dst, src->data, maxlen)) {
1619 return ERR_PTR(-EFAULT);
1625 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1626 int flags, const char *dev_name, void *raw_data)
1629 struct nfs_server *server;
1630 struct super_block *s;
1631 struct nfs4_mount_data *data = raw_data;
1635 printk("nfs_read_super: missing data argument\n");
1636 return ERR_PTR(-EINVAL);
1639 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1641 return ERR_PTR(-ENOMEM);
1642 memset(server, 0, sizeof(struct nfs_server));
1643 /* Zero out the NFS state stuff */
1644 init_nfsv4_state(server);
1646 if (data->version != NFS4_MOUNT_VERSION) {
1647 printk("nfs warning: mount version %s than kernel\n",
1648 data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1651 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1654 server->hostname = p;
1656 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1659 server->mnt_path = p;
1661 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1662 sizeof(server->ip_addr) - 1);
1666 /* We now require that the mount process passes the remote address */
1667 if (data->host_addrlen != sizeof(server->addr)) {
1668 s = ERR_PTR(-EINVAL);
1671 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1672 s = ERR_PTR(-EFAULT);
1675 if (server->addr.sin_family != AF_INET ||
1676 server->addr.sin_addr.s_addr == INADDR_ANY) {
1677 printk("NFS: mount program didn't pass remote IP address!\n");
1678 s = ERR_PTR(-EINVAL);
1682 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1684 if (IS_ERR(s) || s->s_root)
1689 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1691 up_write(&s->s_umount);
1692 deactivate_super(s);
1693 return ERR_PTR(error);
1695 s->s_flags |= MS_ACTIVE;
1698 s = (struct super_block *)p;
1700 if (server->mnt_path)
1701 kfree(server->mnt_path);
1702 if (server->hostname)
1703 kfree(server->hostname);
1708 static struct file_system_type nfs4_fs_type = {
1709 .owner = THIS_MODULE,
1711 .get_sb = nfs4_get_sb,
1712 .kill_sb = nfs_kill_super,
1713 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1716 #define nfs4_zero_state(nfsi) \
1718 INIT_LIST_HEAD(&(nfsi)->open_states); \
1720 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1721 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1723 #define nfs4_zero_state(nfsi) \
1725 #define register_nfs4fs() (0)
1726 #define unregister_nfs4fs()
1729 extern int nfs_init_nfspagecache(void);
1730 extern void nfs_destroy_nfspagecache(void);
1731 extern int nfs_init_readpagecache(void);
1732 extern int nfs_destroy_readpagecache(void);
1733 extern int nfs_init_writepagecache(void);
1734 extern int nfs_destroy_writepagecache(void);
1736 static kmem_cache_t * nfs_inode_cachep;
1738 static struct inode *nfs_alloc_inode(struct super_block *sb)
1740 struct nfs_inode *nfsi;
1741 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1745 nfsi->mm_cred = NULL;
1746 nfs4_zero_state(nfsi);
1747 return &nfsi->vfs_inode;
1750 static void nfs_destroy_inode(struct inode *inode)
1752 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1755 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1757 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1759 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1760 SLAB_CTOR_CONSTRUCTOR) {
1761 inode_init_once(&nfsi->vfs_inode);
1762 INIT_LIST_HEAD(&nfsi->dirty);
1763 INIT_LIST_HEAD(&nfsi->commit);
1764 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1765 atomic_set(&nfsi->data_updates, 0);
1769 init_waitqueue_head(&nfsi->nfs_i_wait);
1773 int nfs_init_inodecache(void)
1775 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1776 sizeof(struct nfs_inode),
1777 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1779 if (nfs_inode_cachep == NULL)
1785 void nfs_destroy_inodecache(void)
1787 if (kmem_cache_destroy(nfs_inode_cachep))
1788 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1794 static int __init init_nfs_fs(void)
1798 err = nfs_init_nfspagecache();
1802 err = nfs_init_inodecache();
1806 err = nfs_init_readpagecache();
1810 err = nfs_init_writepagecache();
1814 #ifdef CONFIG_PROC_FS
1815 rpc_proc_register(&nfs_rpcstat);
1817 err = register_filesystem(&nfs_fs_type);
1820 if ((err = register_nfs4fs()) != 0)
1824 rpc_proc_unregister("nfs");
1825 nfs_destroy_writepagecache();
1827 nfs_destroy_readpagecache();
1829 nfs_destroy_inodecache();
1831 nfs_destroy_nfspagecache();
1836 static void __exit exit_nfs_fs(void)
1838 nfs_destroy_writepagecache();
1839 nfs_destroy_readpagecache();
1840 nfs_destroy_inodecache();
1841 nfs_destroy_nfspagecache();
1842 #ifdef CONFIG_PROC_FS
1843 rpc_proc_unregister("nfs");
1845 unregister_filesystem(&nfs_fs_type);
1846 unregister_nfs4fs();
1849 /* Not quite true; I just maintain it */
1850 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1851 MODULE_LICENSE("GPL");
1853 module_init(init_nfs_fs)
1854 module_exit(exit_nfs_fs)