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 dprintk("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 = {
265 int no_root_error = 0;
267 /* We probably want something more informative here */
268 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
272 sb->s_magic = NFS_SUPER_MAGIC;
274 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
275 /* Did getting the root inode fail? */
276 if (IS_ERR(root_inode)) {
277 no_root_error = PTR_ERR(root_inode);
280 sb->s_root = d_alloc_root(root_inode);
282 no_root_error = -ENOMEM;
285 sb->s_root->d_op = server->rpc_ops->dentry_ops;
287 /* Get some general file system info */
288 if (server->namelen == 0 &&
289 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
290 server->namelen = pathinfo.max_namelen;
291 /* Work out a lot of parameters */
292 if (server->rsize == 0)
293 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
294 if (server->wsize == 0)
295 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
296 if (sb->s_blocksize == 0) {
297 if (fsinfo.wtmult == 0) {
298 sb->s_blocksize = 512;
299 sb->s_blocksize_bits = 9;
301 sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
302 &sb->s_blocksize_bits);
305 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
306 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
307 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
308 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
310 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
311 if (server->rpages > NFS_READ_MAXIOV) {
312 server->rpages = NFS_READ_MAXIOV;
313 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
316 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
317 if (server->wpages > NFS_WRITE_MAXIOV) {
318 server->wpages = NFS_WRITE_MAXIOV;
319 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
322 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
323 if (server->dtsize > PAGE_CACHE_SIZE)
324 server->dtsize = PAGE_CACHE_SIZE;
325 if (server->dtsize > server->rsize)
326 server->dtsize = server->rsize;
328 if (server->flags & NFS_MOUNT_NOAC) {
329 server->acregmin = server->acregmax = 0;
330 server->acdirmin = server->acdirmax = 0;
331 sb->s_flags |= MS_SYNCHRONOUS;
333 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
335 sb->s_maxbytes = fsinfo.maxfilesize;
336 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
337 sb->s_maxbytes = MAX_LFS_FILESIZE;
339 /* We're airborne Set socket buffersize */
340 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
342 /* Yargs. It didn't work out. */
344 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
345 if (!IS_ERR(root_inode))
347 return no_root_error;
351 * Create an RPC client handle.
353 static struct rpc_clnt *
354 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
356 struct rpc_timeout timeparms;
357 struct rpc_xprt *xprt = NULL;
358 struct rpc_clnt *clnt = NULL;
359 int tcp = (data->flags & NFS_MOUNT_TCP);
361 /* Initialize timeout values */
362 timeparms.to_initval = data->timeo * HZ / 10;
363 timeparms.to_retries = data->retrans;
364 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
365 timeparms.to_exponential = 1;
367 if (!timeparms.to_initval)
368 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
369 if (!timeparms.to_retries)
370 timeparms.to_retries = 5;
372 /* create transport and client */
373 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
374 &server->addr, &timeparms);
376 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
377 return (struct rpc_clnt *)xprt;
379 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
380 server->rpc_ops->version, data->pseudoflavor);
382 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
386 clnt->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
387 clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
388 clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
399 * The way this works is that the mount process passes a structure
400 * in the data argument which contains the server's IP address
401 * and the root file handle obtained from the server's mount
402 * daemon. We stash these away in the private superblock fields.
405 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
407 struct nfs_server *server;
409 rpc_authflavor_t authflavor;
412 sb->s_blocksize_bits = 0;
415 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
417 server->rsize = nfs_block_size(data->rsize, NULL);
419 server->wsize = nfs_block_size(data->wsize, NULL);
420 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
422 server->acregmin = data->acregmin*HZ;
423 server->acregmax = data->acregmax*HZ;
424 server->acdirmin = data->acdirmin*HZ;
425 server->acdirmax = data->acdirmax*HZ;
427 server->namelen = data->namlen;
428 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
429 if (!server->hostname)
431 strcpy(server->hostname, data->hostname);
433 /* Check NFS protocol revision and initialize RPC op vector
434 * and file handle pool. */
435 if (server->flags & NFS_MOUNT_VER3) {
437 server->rpc_ops = &nfs_v3_clientops;
438 server->caps |= NFS_CAP_READDIRPLUS;
439 if (data->version < 4) {
440 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
444 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
448 server->rpc_ops = &nfs_v2_clientops;
451 /* Fill in pseudoflavor for mount version < 5 */
452 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
453 data->pseudoflavor = RPC_AUTH_UNIX;
454 authflavor = data->pseudoflavor; /* save for sb_init() */
455 /* XXX maybe we want to add a server->pseudoflavor field */
457 /* Create RPC client handles */
458 server->client = nfs_create_client(server, data);
459 if (server->client == NULL)
461 /* RFC 2623, sec 2.3.2 */
462 if (authflavor != RPC_AUTH_UNIX) {
463 server->client_sys = rpc_clone_client(server->client);
464 if (server->client_sys == NULL)
466 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
469 atomic_inc(&server->client->cl_count);
470 server->client_sys = server->client;
473 /* Fire up rpciod if not yet running */
474 if (rpciod_up() != 0) {
475 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
479 sb->s_op = &nfs_sops;
480 err = nfs_sb_init(sb, authflavor);
484 if (server->flags & NFS_MOUNT_VER3) {
485 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
486 server->namelen = NFS3_MAXNAMLEN;
488 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
489 server->namelen = NFS2_MAXNAMLEN;
492 /* Check whether to start the lockd process */
493 if (!(server->flags & NFS_MOUNT_NONLM))
500 rpc_shutdown_client(server->client);
501 if (server->client_sys)
502 rpc_shutdown_client(server->client_sys);
504 if (server->hostname)
505 kfree(server->hostname);
510 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
512 struct nfs_server *server = NFS_SB(sb);
513 unsigned char blockbits;
514 unsigned long blockres;
515 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
516 struct nfs_fattr fattr;
517 struct nfs_fsstat res = {
524 error = server->rpc_ops->statfs(server, rootfh, &res);
525 buf->f_type = NFS_SUPER_MAGIC;
529 buf->f_bsize = sb->s_blocksize;
530 blockbits = sb->s_blocksize_bits;
531 blockres = (1 << blockbits) - 1;
532 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
533 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
534 buf->f_bavail = (res.abytes + blockres) >> blockbits;
535 buf->f_files = res.tfiles;
536 buf->f_ffree = res.afiles;
538 buf->f_namelen = server->namelen;
545 printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
546 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
551 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
553 static struct proc_nfs_info {
558 { NFS_MOUNT_SOFT, ",soft", ",hard" },
559 { NFS_MOUNT_INTR, ",intr", "" },
560 { NFS_MOUNT_POSIX, ",posix", "" },
561 { NFS_MOUNT_TCP, ",tcp", ",udp" },
562 { NFS_MOUNT_NOCTO, ",nocto", "" },
563 { NFS_MOUNT_NOAC, ",noac", "" },
564 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
565 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
568 struct proc_nfs_info *nfs_infop;
569 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
571 seq_printf(m, ",v%d", nfss->rpc_ops->version);
572 seq_printf(m, ",rsize=%d", nfss->rsize);
573 seq_printf(m, ",wsize=%d", nfss->wsize);
574 if (nfss->acregmin != 3*HZ)
575 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
576 if (nfss->acregmax != 60*HZ)
577 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
578 if (nfss->acdirmin != 30*HZ)
579 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
580 if (nfss->acdirmax != 60*HZ)
581 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
582 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
583 if (nfss->flags & nfs_infop->flag)
584 seq_puts(m, nfs_infop->str);
586 seq_puts(m, nfs_infop->nostr);
588 seq_puts(m, ",addr=");
589 seq_escape(m, nfss->hostname, " \t\n\\");
594 * Invalidate the local caches
597 nfs_zap_caches(struct inode *inode)
599 struct nfs_inode *nfsi = NFS_I(inode);
600 int mode = inode->i_mode;
602 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
603 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
605 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
606 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
607 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
609 nfsi->flags |= NFS_INO_INVALID_ATTR;
613 * Invalidate, but do not unhash, the inode
616 nfs_invalidate_inode(struct inode *inode)
618 umode_t save_mode = inode->i_mode;
620 make_bad_inode(inode);
621 inode->i_mode = save_mode;
622 nfs_zap_caches(inode);
625 struct nfs_find_desc {
627 struct nfs_fattr *fattr;
631 * In NFSv3 we can have 64bit inode numbers. In order to support
632 * this, and re-exported directories (also seen in NFSv2)
633 * we are forced to allow 2 different inodes to have the same
637 nfs_find_actor(struct inode *inode, void *opaque)
639 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
640 struct nfs_fh *fh = desc->fh;
641 struct nfs_fattr *fattr = desc->fattr;
643 if (NFS_FILEID(inode) != fattr->fileid)
645 if (memcmp(NFS_FH(inode), fh, sizeof(struct nfs_fh)) != 0)
647 if (is_bad_inode(inode))
653 nfs_init_locked(struct inode *inode, void *opaque)
655 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
656 struct nfs_fh *fh = desc->fh;
657 struct nfs_fattr *fattr = desc->fattr;
659 NFS_FILEID(inode) = fattr->fileid;
660 memcpy(NFS_FH(inode), fh, sizeof(struct nfs_fh));
664 /* Don't use READDIRPLUS on directories that we believe are too large */
665 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
668 * This is our front-end to iget that looks up inodes by file handle
669 * instead of inode number.
672 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
674 struct nfs_find_desc desc = {
678 struct inode *inode = NULL;
681 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
685 printk("NFS: Buggy server - nlink == 0!\n");
689 hash = nfs_fattr_to_ino_t(fattr);
691 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
694 if (inode->i_state & I_NEW) {
695 struct nfs_inode *nfsi = NFS_I(inode);
697 /* We set i_ino for the few things that still rely on it,
701 /* We can't support update_atime(), since the server will reset it */
702 inode->i_flags |= S_NOATIME|S_NOCMTIME;
703 inode->i_mode = fattr->mode;
704 /* Why so? Because we want revalidate for devices/FIFOs, and
705 * that's precisely what we have in nfs_file_inode_operations.
707 inode->i_op = &nfs_file_inode_operations;
708 if (S_ISREG(inode->i_mode)) {
709 inode->i_fop = &nfs_file_operations;
710 inode->i_data.a_ops = &nfs_file_aops;
711 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
712 } else if (S_ISDIR(inode->i_mode)) {
713 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
714 inode->i_fop = &nfs_dir_operations;
715 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
716 && fattr->size <= NFS_LIMIT_READDIRPLUS)
717 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
718 } else if (S_ISLNK(inode->i_mode))
719 inode->i_op = &nfs_symlink_inode_operations;
721 init_special_inode(inode, inode->i_mode, fattr->rdev);
723 nfsi->read_cache_jiffies = fattr->timestamp;
724 inode->i_atime = fattr->atime;
725 inode->i_mtime = fattr->mtime;
726 inode->i_ctime = fattr->ctime;
727 if (fattr->valid & NFS_ATTR_FATTR_V4)
728 nfsi->change_attr = fattr->change_attr;
729 inode->i_size = nfs_size_to_loff_t(fattr->size);
730 inode->i_nlink = fattr->nlink;
731 inode->i_uid = fattr->uid;
732 inode->i_gid = fattr->gid;
733 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
735 * report the blocks in 512byte units
737 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
738 inode->i_blksize = inode->i_sb->s_blocksize;
740 inode->i_blocks = fattr->du.nfs2.blocks;
741 inode->i_blksize = fattr->du.nfs2.blocksize;
743 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
744 nfsi->attrtimeo_timestamp = jiffies;
745 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
746 nfsi->cache_access.cred = NULL;
748 unlock_new_inode(inode);
750 nfs_refresh_inode(inode, fattr);
751 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
753 (long long)NFS_FILEID(inode),
754 atomic_read(&inode->i_count));
760 printk("nfs_fhget: iget failed\n");
764 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
767 nfs_setattr(struct dentry *dentry, struct iattr *attr)
769 struct inode *inode = dentry->d_inode;
770 struct nfs_fattr fattr;
773 if (attr->ia_valid & ATTR_SIZE) {
774 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
775 attr->ia_valid &= ~ATTR_SIZE;
778 /* Optimization: if the end result is no change, don't RPC */
779 attr->ia_valid &= NFS_VALID_ATTRS;
780 if (attr->ia_valid == 0)
784 nfs_begin_data_update(inode);
785 /* Write all dirty data if we're changing file permissions or size */
786 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
787 if (filemap_fdatawrite(inode->i_mapping) == 0)
788 filemap_fdatawait(inode->i_mapping);
791 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
793 nfs_refresh_inode(inode, &fattr);
794 if ((attr->ia_valid & ATTR_MODE) != 0) {
796 mode = inode->i_mode & ~S_IALLUGO;
797 mode |= attr->ia_mode & S_IALLUGO;
798 inode->i_mode = mode;
800 if ((attr->ia_valid & ATTR_UID) != 0)
801 inode->i_uid = attr->ia_uid;
802 if ((attr->ia_valid & ATTR_GID) != 0)
803 inode->i_gid = attr->ia_gid;
804 if ((attr->ia_valid & ATTR_SIZE) != 0) {
805 inode->i_size = attr->ia_size;
806 vmtruncate(inode, attr->ia_size);
809 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
810 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
816 nfs_end_data_update(inode);
822 * Wait for the inode to get unlocked.
823 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
826 nfs_wait_on_inode(struct inode *inode, int flag)
828 struct rpc_clnt *clnt = NFS_CLIENT(inode);
829 struct nfs_inode *nfsi = NFS_I(inode);
832 if (!(NFS_FLAGS(inode) & flag))
834 atomic_inc(&inode->i_count);
835 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
836 !(NFS_FLAGS(inode) & flag));
841 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
843 struct inode *inode = dentry->d_inode;
844 struct nfs_inode *nfsi = NFS_I(inode);
845 int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
848 if (__IS_FLG(inode, MS_NOATIME))
850 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
852 /* We may force a getattr if the user cares about atime */
854 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
856 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
858 generic_fillattr(inode, stat);
863 * Ensure that mmap has a recent RPC credential for use when writing out
867 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
869 struct rpc_cred **p = &NFS_I(inode)->mm_cred,
872 *p = get_rpccred(cred);
874 put_rpccred(oldcred);
878 * These are probably going to contain hooks for
879 * allocating and releasing RPC credentials for
880 * the file. I'll have to think about Tronds patch
883 int nfs_open(struct inode *inode, struct file *filp)
885 struct rpc_auth *auth;
886 struct rpc_cred *cred;
888 auth = NFS_CLIENT(inode)->cl_auth;
889 cred = rpcauth_lookupcred(auth, 0);
890 filp->private_data = cred;
891 if ((filp->f_mode & FMODE_WRITE) != 0) {
892 nfs_set_mmcred(inode, cred);
893 nfs_begin_data_update(inode);
898 int nfs_release(struct inode *inode, struct file *filp)
900 struct rpc_cred *cred;
903 if ((filp->f_mode & FMODE_WRITE) != 0)
904 nfs_end_data_update(inode);
905 cred = nfs_file_cred(filp);
913 * This function is called whenever some part of NFS notices that
914 * the cached attributes have to be refreshed.
917 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
919 int status = -ESTALE;
920 struct nfs_fattr fattr;
921 struct nfs_inode *nfsi = NFS_I(inode);
922 unsigned long verifier;
925 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
926 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
929 if (!inode || is_bad_inode(inode))
931 if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
934 while (NFS_REVALIDATING(inode)) {
935 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
938 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
940 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
942 status = NFS_STALE(inode) ? -ESTALE : 0;
945 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
947 /* Protect against RPC races by saving the change attribute */
948 verifier = nfs_save_change_attribute(inode);
949 status = NFS_PROTO(inode)->getattr(inode, &fattr);
951 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
953 (long long)NFS_FILEID(inode), status);
954 if (status == -ESTALE) {
955 NFS_FLAGS(inode) |= NFS_INO_STALE;
956 if (inode != inode->i_sb->s_root->d_inode)
957 remove_inode_hash(inode);
962 status = nfs_update_inode(inode, &fattr, verifier);
964 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
966 (long long)NFS_FILEID(inode), status);
971 * We may need to keep the attributes marked as invalid if
972 * we raced with nfs_end_attr_update().
974 if (verifier == nfsi->cache_change_attribute)
975 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
976 /* Do the page cache invalidation */
977 if (flags & NFS_INO_INVALID_DATA) {
978 if (S_ISREG(inode->i_mode)) {
979 if (filemap_fdatawrite(inode->i_mapping) == 0)
980 filemap_fdatawait(inode->i_mapping);
983 nfsi->flags &= ~NFS_INO_INVALID_DATA;
984 invalidate_inode_pages2(inode->i_mapping);
985 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
986 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
988 (long long)NFS_FILEID(inode));
989 /* This ensures we revalidate dentries */
990 nfsi->cache_change_attribute++;
992 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
994 (long long)NFS_FILEID(inode));
996 NFS_FLAGS(inode) &= ~NFS_INO_STALE;
998 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
999 wake_up(&nfsi->nfs_i_wait);
1006 * nfs_begin_data_update
1007 * @inode - pointer to inode
1008 * Declare that a set of operations will update file data on the server
1010 void nfs_begin_data_update(struct inode *inode)
1012 atomic_inc(&NFS_I(inode)->data_updates);
1016 * nfs_end_data_update
1017 * @inode - pointer to inode
1018 * Declare end of the operations that will update file data
1019 * This will mark the inode as immediately needing revalidation
1020 * of its attribute cache.
1022 void nfs_end_data_update(struct inode *inode)
1024 struct nfs_inode *nfsi = NFS_I(inode);
1026 /* Mark the attribute cache for revalidation */
1027 nfsi->flags |= NFS_INO_INVALID_ATTR;
1028 /* Directories and symlinks: invalidate page cache too */
1029 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1030 nfsi->flags |= NFS_INO_INVALID_DATA;
1031 nfsi->cache_change_attribute ++;
1032 atomic_dec(&nfsi->data_updates);
1036 * nfs_end_data_update_defer
1037 * @inode - pointer to inode
1038 * Declare end of the operations that will update file data
1039 * This will defer marking the inode as needing revalidation
1040 * unless there are no other pending updates.
1042 void nfs_end_data_update_defer(struct inode *inode)
1044 struct nfs_inode *nfsi = NFS_I(inode);
1046 if (atomic_dec_and_test(&nfsi->data_updates)) {
1047 /* Mark the attribute cache for revalidation */
1048 nfsi->flags |= NFS_INO_INVALID_ATTR;
1049 /* Directories and symlinks: invalidate page cache too */
1050 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1051 nfsi->flags |= NFS_INO_INVALID_DATA;
1052 nfsi->cache_change_attribute ++;
1057 * nfs_refresh_inode - verify consistency of the inode attribute cache
1058 * @inode - pointer to inode
1059 * @fattr - updated attributes
1061 * Verifies the attribute cache. If we have just changed the attributes,
1062 * so that fattr carries weak cache consistency data, then it may
1063 * also update the ctime/mtime/change_attribute.
1065 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1067 struct nfs_inode *nfsi = NFS_I(inode);
1068 loff_t cur_size, new_isize;
1071 /* Are we in the process of updating data on the server? */
1072 data_unstable = nfs_caches_unstable(inode);
1074 if (fattr->valid & NFS_ATTR_FATTR_V4) {
1075 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1076 && nfsi->change_attr == fattr->pre_change_attr)
1077 nfsi->change_attr = fattr->change_attr;
1078 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1079 nfsi->flags |= NFS_INO_INVALID_ATTR;
1082 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1085 /* Has the inode gone and changed behind our back? */
1086 if (nfsi->fileid != fattr->fileid
1087 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1090 cur_size = i_size_read(inode);
1091 new_isize = nfs_size_to_loff_t(fattr->size);
1093 /* If we have atomic WCC data, we may update some attributes */
1094 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1095 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1096 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1097 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1098 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1101 /* Verify a few of the more important attributes */
1102 if (!data_unstable) {
1103 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1104 || cur_size != new_isize)
1105 nfsi->flags |= NFS_INO_INVALID_ATTR;
1106 } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1107 nfsi->flags |= NFS_INO_INVALID_ATTR;
1109 /* Have any file permissions changed? */
1110 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1111 || inode->i_uid != fattr->uid
1112 || inode->i_gid != fattr->gid)
1113 nfsi->flags |= NFS_INO_INVALID_ATTR;
1115 /* Has the link count changed? */
1116 if (inode->i_nlink != fattr->nlink)
1117 nfsi->flags |= NFS_INO_INVALID_ATTR;
1119 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1120 nfsi->flags |= NFS_INO_INVALID_ATIME;
1122 nfsi->read_cache_jiffies = fattr->timestamp;
1127 * Many nfs protocol calls return the new file attributes after
1128 * an operation. Here we update the inode to reflect the state
1129 * of the server's inode.
1131 * This is a bit tricky because we have to make sure all dirty pages
1132 * have been sent off to the server before calling invalidate_inode_pages.
1133 * To make sure no other process adds more write requests while we try
1134 * our best to flush them, we make them sleep during the attribute refresh.
1136 * A very similar scenario holds for the dir cache.
1138 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1140 struct nfs_inode *nfsi = NFS_I(inode);
1143 unsigned int invalid = 0;
1147 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1148 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1149 atomic_read(&inode->i_count), fattr->valid);
1151 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1154 if (nfsi->fileid != fattr->fileid) {
1155 printk(KERN_ERR "%s: inode number mismatch\n"
1156 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1158 inode->i_sb->s_id, (long long)nfsi->fileid,
1159 inode->i_sb->s_id, (long long)fattr->fileid);
1164 * Make sure the inode's type hasn't changed.
1166 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1170 * Update the read time so we don't revalidate too often.
1172 nfsi->read_cache_jiffies = fattr->timestamp;
1174 /* Are we racing with known updates of the metadata on the server? */
1175 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1177 /* Check if the file size agrees */
1178 new_size = fattr->size;
1179 new_isize = nfs_size_to_loff_t(fattr->size);
1180 cur_isize = i_size_read(inode);
1181 if (cur_isize != new_size) {
1182 #ifdef NFS_DEBUG_VERBOSE
1183 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1186 * If we have pending writebacks, things can get
1189 if (S_ISREG(inode->i_mode) && data_unstable) {
1190 if (new_isize > cur_isize) {
1191 inode->i_size = new_isize;
1192 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1195 inode->i_size = new_isize;
1196 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1201 * Note: we don't check inode->i_mtime since pipes etc.
1202 * can change this value in VFS without requiring a
1203 * cache revalidation.
1205 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1206 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1207 #ifdef NFS_DEBUG_VERBOSE
1208 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1211 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1214 if ((fattr->valid & NFS_ATTR_FATTR_V4)
1215 && nfsi->change_attr != fattr->change_attr) {
1216 #ifdef NFS_DEBUG_VERBOSE
1217 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1218 inode->i_sb->s_id, inode->i_ino);
1220 nfsi->change_attr = fattr->change_attr;
1222 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1225 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1226 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1228 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1229 inode->i_uid != fattr->uid ||
1230 inode->i_gid != fattr->gid) {
1231 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1236 invalid |= NFS_INO_INVALID_ATTR;
1239 inode->i_mode = fattr->mode;
1240 inode->i_nlink = fattr->nlink;
1241 inode->i_uid = fattr->uid;
1242 inode->i_gid = fattr->gid;
1244 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1246 * report the blocks in 512byte units
1248 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1249 inode->i_blksize = inode->i_sb->s_blocksize;
1251 inode->i_blocks = fattr->du.nfs2.blocks;
1252 inode->i_blksize = fattr->du.nfs2.blocksize;
1255 /* Update attrtimeo value if we're out of the unstable period */
1256 if (invalid & NFS_INO_INVALID_ATTR) {
1257 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1258 nfsi->attrtimeo_timestamp = jiffies;
1259 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1260 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1261 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1262 nfsi->attrtimeo_timestamp = jiffies;
1264 /* Don't invalidate the data if we were to blame */
1265 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1266 || S_ISLNK(inode->i_mode)))
1267 invalid &= ~NFS_INO_INVALID_DATA;
1268 nfsi->flags |= invalid;
1273 * Big trouble! The inode has become a different object.
1276 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1277 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1280 * No need to worry about unhashing the dentry, as the
1281 * lookup validation will know that the inode is bad.
1282 * (But we fall through to invalidate the caches.)
1284 nfs_invalidate_inode(inode);
1290 * File system information
1293 static int nfs_set_super(struct super_block *s, void *data)
1295 s->s_fs_info = data;
1296 return set_anon_super(s, data);
1299 static int nfs_compare_super(struct super_block *sb, void *data)
1301 struct nfs_server *server = data;
1302 struct nfs_server *old = NFS_SB(sb);
1304 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1306 if (old->addr.sin_port != server->addr.sin_port)
1308 return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1311 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1312 int flags, const char *dev_name, void *raw_data)
1315 struct nfs_server *server;
1316 struct super_block *s;
1317 struct nfs_fh *root;
1318 struct nfs_mount_data *data = raw_data;
1321 printk("nfs_read_super: missing data argument\n");
1322 return ERR_PTR(-EINVAL);
1325 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1327 return ERR_PTR(-ENOMEM);
1328 memset(server, 0, sizeof(struct nfs_server));
1329 /* Zero out the NFS state stuff */
1330 init_nfsv4_state(server);
1333 memcpy(root, &data->root, sizeof(*root));
1334 if (root->size < sizeof(root->data))
1335 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1337 if (data->version != NFS_MOUNT_VERSION) {
1338 printk("nfs warning: mount version %s than kernel\n",
1339 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1340 if (data->version < 2)
1342 if (data->version < 3)
1344 if (data->version < 4) {
1345 data->flags &= ~NFS_MOUNT_VER3;
1346 memset(root, 0, sizeof(*root));
1347 root->size = NFS2_FHSIZE;
1348 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1350 if (data->version < 5)
1351 data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1354 if (root->size > sizeof(root->data)) {
1355 printk("nfs_get_sb: invalid root filehandle\n");
1357 return ERR_PTR(-EINVAL);
1359 /* We now require that the mount process passes the remote address */
1360 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1361 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1362 printk("NFS: mount program didn't pass remote address!\n");
1364 return ERR_PTR(-EINVAL);
1367 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1369 if (IS_ERR(s) || s->s_root) {
1376 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1378 up_write(&s->s_umount);
1379 deactivate_super(s);
1380 return ERR_PTR(error);
1382 s->s_flags |= MS_ACTIVE;
1386 static void nfs_kill_super(struct super_block *s)
1388 struct nfs_server *server = NFS_SB(s);
1393 static struct file_system_type nfs_fs_type = {
1394 .owner = THIS_MODULE,
1396 .get_sb = nfs_get_sb,
1397 .kill_sb = nfs_kill_super,
1398 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1401 #ifdef CONFIG_NFS_V4
1403 static void nfs4_clear_inode(struct inode *);
1405 static struct super_operations nfs4_sops = {
1406 .alloc_inode = nfs_alloc_inode,
1407 .destroy_inode = nfs_destroy_inode,
1408 .write_inode = nfs_write_inode,
1409 .delete_inode = nfs_delete_inode,
1410 .put_super = nfs_put_super,
1411 .statfs = nfs_statfs,
1412 .clear_inode = nfs4_clear_inode,
1413 .umount_begin = nfs_umount_begin,
1414 .show_options = nfs_show_options,
1418 * Clean out any remaining NFSv4 state that might be left over due
1419 * to open() calls that passed nfs_atomic_lookup, but failed to call
1422 static void nfs4_clear_inode(struct inode *inode)
1424 struct nfs_inode *nfsi = NFS_I(inode);
1426 while (!list_empty(&nfsi->open_states)) {
1427 struct nfs4_state *state;
1429 state = list_entry(nfsi->open_states.next,
1432 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1435 (long long)NFS_FILEID(inode),
1437 BUG_ON(atomic_read(&state->count) != 1);
1438 nfs4_close_state(state, state->state);
1440 /* Now call standard NFS clear_inode() code */
1441 nfs_clear_inode(inode);
1445 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1447 struct nfs_server *server;
1448 struct nfs4_client *clp = NULL;
1449 struct rpc_xprt *xprt = NULL;
1450 struct rpc_clnt *clnt = NULL;
1451 struct rpc_timeout timeparms;
1452 rpc_authflavor_t authflavour;
1453 int proto, err = -EIO;
1455 sb->s_blocksize_bits = 0;
1456 sb->s_blocksize = 0;
1457 server = NFS_SB(sb);
1458 if (data->rsize != 0)
1459 server->rsize = nfs_block_size(data->rsize, NULL);
1460 if (data->wsize != 0)
1461 server->wsize = nfs_block_size(data->wsize, NULL);
1462 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1464 /* NFSv4 doesn't use NLM locking */
1465 server->flags |= NFS_MOUNT_NONLM;
1467 server->acregmin = data->acregmin*HZ;
1468 server->acregmax = data->acregmax*HZ;
1469 server->acdirmin = data->acdirmin*HZ;
1470 server->acdirmax = data->acdirmax*HZ;
1472 server->rpc_ops = &nfs_v4_clientops;
1473 /* Initialize timeout values */
1475 timeparms.to_initval = data->timeo * HZ / 10;
1476 timeparms.to_retries = data->retrans;
1477 timeparms.to_exponential = 1;
1478 if (!timeparms.to_retries)
1479 timeparms.to_retries = 5;
1481 proto = data->proto;
1482 /* Which IP protocol do we use? */
1485 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1486 if (!timeparms.to_initval)
1487 timeparms.to_initval = 600 * HZ / 10;
1490 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1491 if (!timeparms.to_initval)
1492 timeparms.to_initval = 11 * HZ / 10;
1498 clp = nfs4_get_client(&server->addr.sin_addr);
1500 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1504 /* Now create transport and client */
1505 authflavour = RPC_AUTH_UNIX;
1506 if (data->auth_flavourlen != 0) {
1507 if (data->auth_flavourlen > 1)
1508 printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1509 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1515 down_write(&clp->cl_sem);
1516 if (clp->cl_rpcclient == NULL) {
1517 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1519 up_write(&clp->cl_sem);
1520 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1521 err = PTR_ERR(xprt);
1524 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1525 server->rpc_ops->version, authflavour);
1527 up_write(&clp->cl_sem);
1528 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1530 err = PTR_ERR(clnt);
1533 clnt->cl_chatty = 1;
1534 clp->cl_rpcclient = clnt;
1535 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1536 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1539 if (list_empty(&clp->cl_superblocks))
1540 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1541 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1542 clnt = rpc_clone_client(clp->cl_rpcclient);
1544 server->nfs4_state = clp;
1545 up_write(&clp->cl_sem);
1549 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1550 err = PTR_ERR(clnt);
1551 goto out_remove_list;
1554 if (server->nfs4_state->cl_idmap == NULL) {
1555 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1559 clnt->cl_intr = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1560 clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1561 server->client = clnt;
1563 if (clnt->cl_auth->au_flavor != authflavour) {
1564 if (rpcauth_create(authflavour, clnt) == NULL) {
1565 printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1570 /* Fire up rpciod if not yet running */
1571 if (rpciod_up() != 0) {
1572 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1576 sb->s_op = &nfs4_sops;
1577 err = nfs_sb_init(sb, authflavour);
1582 rpc_shutdown_client(server->client);
1584 down_write(&server->nfs4_state->cl_sem);
1585 list_del_init(&server->nfs4_siblings);
1586 up_write(&server->nfs4_state->cl_sem);
1587 destroy_nfsv4_state(server);
1590 nfs4_put_client(clp);
1594 static int nfs4_compare_super(struct super_block *sb, void *data)
1596 struct nfs_server *server = data;
1597 struct nfs_server *old = NFS_SB(sb);
1599 if (strcmp(server->hostname, old->hostname) != 0)
1601 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1607 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1612 return ERR_PTR(-EINVAL);
1613 if (src->len < maxlen)
1616 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1618 return ERR_PTR(-ENOMEM);
1620 if (copy_from_user(dst, src->data, maxlen)) {
1623 return ERR_PTR(-EFAULT);
1629 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1630 int flags, const char *dev_name, void *raw_data)
1633 struct nfs_server *server;
1634 struct super_block *s;
1635 struct nfs4_mount_data *data = raw_data;
1639 printk("nfs_read_super: missing data argument\n");
1640 return ERR_PTR(-EINVAL);
1643 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1645 return ERR_PTR(-ENOMEM);
1646 memset(server, 0, sizeof(struct nfs_server));
1647 /* Zero out the NFS state stuff */
1648 init_nfsv4_state(server);
1650 if (data->version != NFS4_MOUNT_VERSION) {
1651 printk("nfs warning: mount version %s than kernel\n",
1652 data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1655 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1658 server->hostname = p;
1660 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1663 server->mnt_path = p;
1665 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1666 sizeof(server->ip_addr) - 1);
1670 /* We now require that the mount process passes the remote address */
1671 if (data->host_addrlen != sizeof(server->addr)) {
1672 s = ERR_PTR(-EINVAL);
1675 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1676 s = ERR_PTR(-EFAULT);
1679 if (server->addr.sin_family != AF_INET ||
1680 server->addr.sin_addr.s_addr == INADDR_ANY) {
1681 printk("NFS: mount program didn't pass remote IP address!\n");
1682 s = ERR_PTR(-EINVAL);
1686 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1688 if (IS_ERR(s) || s->s_root)
1693 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1695 up_write(&s->s_umount);
1696 deactivate_super(s);
1697 return ERR_PTR(error);
1699 s->s_flags |= MS_ACTIVE;
1702 s = (struct super_block *)p;
1704 if (server->mnt_path)
1705 kfree(server->mnt_path);
1706 if (server->hostname)
1707 kfree(server->hostname);
1712 static struct file_system_type nfs4_fs_type = {
1713 .owner = THIS_MODULE,
1715 .get_sb = nfs4_get_sb,
1716 .kill_sb = nfs_kill_super,
1717 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1720 #define nfs4_zero_state(nfsi) \
1722 INIT_LIST_HEAD(&(nfsi)->open_states); \
1724 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1725 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1727 #define nfs4_zero_state(nfsi) \
1729 #define register_nfs4fs() (0)
1730 #define unregister_nfs4fs()
1733 extern int nfs_init_nfspagecache(void);
1734 extern void nfs_destroy_nfspagecache(void);
1735 extern int nfs_init_readpagecache(void);
1736 extern int nfs_destroy_readpagecache(void);
1737 extern int nfs_init_writepagecache(void);
1738 extern int nfs_destroy_writepagecache(void);
1740 static kmem_cache_t * nfs_inode_cachep;
1742 static struct inode *nfs_alloc_inode(struct super_block *sb)
1744 struct nfs_inode *nfsi;
1745 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1749 nfsi->mm_cred = NULL;
1750 nfs4_zero_state(nfsi);
1751 return &nfsi->vfs_inode;
1754 static void nfs_destroy_inode(struct inode *inode)
1756 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1759 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1761 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1763 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1764 SLAB_CTOR_CONSTRUCTOR) {
1765 inode_init_once(&nfsi->vfs_inode);
1766 INIT_LIST_HEAD(&nfsi->dirty);
1767 INIT_LIST_HEAD(&nfsi->commit);
1768 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1769 atomic_set(&nfsi->data_updates, 0);
1773 init_waitqueue_head(&nfsi->nfs_i_wait);
1777 int nfs_init_inodecache(void)
1779 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1780 sizeof(struct nfs_inode),
1781 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1783 if (nfs_inode_cachep == NULL)
1789 void nfs_destroy_inodecache(void)
1791 if (kmem_cache_destroy(nfs_inode_cachep))
1792 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1798 static int __init init_nfs_fs(void)
1802 err = nfs_init_nfspagecache();
1806 err = nfs_init_inodecache();
1810 err = nfs_init_readpagecache();
1814 err = nfs_init_writepagecache();
1818 #ifdef CONFIG_PROC_FS
1819 rpc_proc_register(&nfs_rpcstat);
1821 err = register_filesystem(&nfs_fs_type);
1824 if ((err = register_nfs4fs()) != 0)
1828 rpc_proc_unregister("nfs");
1829 nfs_destroy_writepagecache();
1831 nfs_destroy_readpagecache();
1833 nfs_destroy_inodecache();
1835 nfs_destroy_nfspagecache();
1840 static void __exit exit_nfs_fs(void)
1842 nfs_destroy_writepagecache();
1843 nfs_destroy_readpagecache();
1844 nfs_destroy_inodecache();
1845 nfs_destroy_nfspagecache();
1846 #ifdef CONFIG_PROC_FS
1847 rpc_proc_unregister("nfs");
1849 unregister_filesystem(&nfs_fs_type);
1850 unregister_nfs4fs();
1853 /* Not quite true; I just maintain it */
1854 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1855 MODULE_LICENSE("GPL");
1857 module_init(init_nfs_fs)
1858 module_exit(exit_nfs_fs)