4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <linux/time.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/errno.h>
26 #include <linux/unistd.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/sunrpc/stats.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/smp_lock.h>
34 #include <linux/seq_file.h>
35 #include <linux/mount.h>
36 #include <linux/nfs_idmap.h>
37 #include <linux/vfs.h>
38 #include <linux/vserver/xid.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
43 #define NFSDBG_FACILITY NFSDBG_VFS
44 #define NFS_PARANOIA 1
46 /* Maximum number of readahead requests
47 * FIXME: this should really be a sysctl so that users may tune it to suit
48 * their needs. People that do NFS over a slow network, might for
49 * instance want to reduce it to something closer to 1 for improved
50 * interactive response.
52 #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
54 static void nfs_invalidate_inode(struct inode *);
55 static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
57 static struct inode *nfs_alloc_inode(struct super_block *sb);
58 static void nfs_destroy_inode(struct inode *);
59 static void nfs_write_inode(struct inode *,int);
60 static void nfs_delete_inode(struct inode *);
61 static void nfs_put_super(struct super_block *);
62 static void nfs_clear_inode(struct inode *);
63 static void nfs_umount_begin(struct super_block *);
64 static int nfs_statfs(struct super_block *, struct kstatfs *);
65 static int nfs_show_options(struct seq_file *, struct vfsmount *);
67 static struct super_operations nfs_sops = {
68 .alloc_inode = nfs_alloc_inode,
69 .destroy_inode = nfs_destroy_inode,
70 .write_inode = nfs_write_inode,
71 .delete_inode = nfs_delete_inode,
72 .put_super = nfs_put_super,
74 .clear_inode = nfs_clear_inode,
75 .umount_begin = nfs_umount_begin,
76 .show_options = nfs_show_options,
82 struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
85 static struct rpc_version * nfs_version[] = {
89 #if defined(CONFIG_NFS_V3)
91 #elif defined(CONFIG_NFS_V4)
94 #if defined(CONFIG_NFS_V4)
99 struct rpc_program nfs_program = {
101 .number = NFS_PROGRAM,
102 .nrvers = sizeof(nfs_version) / sizeof(nfs_version[0]),
103 .version = nfs_version,
104 .stats = &nfs_rpcstat,
105 .pipe_dir_name = "/nfs",
108 static inline unsigned long
109 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
111 return nfs_fileid_to_ino_t(fattr->fileid);
115 nfs_write_inode(struct inode *inode, int sync)
117 int flags = sync ? FLUSH_WAIT : 0;
119 nfs_commit_inode(inode, 0, 0, flags);
123 nfs_delete_inode(struct inode * inode)
125 dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
128 * The following can never actually happen...
130 if (nfs_have_writebacks(inode)) {
131 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
138 * For the moment, the only task for the NFS clear_inode method is to
139 * release the mmap credential
142 nfs_clear_inode(struct inode *inode)
144 struct nfs_inode *nfsi = NFS_I(inode);
145 struct rpc_cred *cred = nfsi->mm_cred;
149 cred = nfsi->cache_access.cred;
152 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
156 nfs_put_super(struct super_block *sb)
158 struct nfs_server *server = NFS_SB(sb);
160 nfs4_renewd_prepare_shutdown(server);
162 if (server->client != NULL)
163 rpc_shutdown_client(server->client);
164 if (server->client_sys != NULL)
165 rpc_shutdown_client(server->client_sys);
167 if (!(server->flags & NFS_MOUNT_NONLM))
168 lockd_down(); /* release rpc.lockd */
169 rpciod_down(); /* release rpciod */
171 destroy_nfsv4_state(server);
173 kfree(server->hostname);
177 nfs_umount_begin(struct super_block *sb)
179 struct nfs_server *server = NFS_SB(sb);
180 struct rpc_clnt *rpc;
182 /* -EIO all pending I/O */
183 if ((rpc = server->client) != NULL)
184 rpc_killall_tasks(rpc);
188 static inline unsigned long
189 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
191 /* make sure blocksize is a power of two */
192 if ((bsize & (bsize - 1)) || nrbitsp) {
193 unsigned char nrbits;
195 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
206 * Calculate the number of 512byte blocks used.
208 static inline unsigned long
209 nfs_calc_block_size(u64 tsize)
211 loff_t used = (tsize + 511) >> 9;
212 return (used > ULONG_MAX) ? ULONG_MAX : used;
216 * Compute and set NFS server blocksize
218 static inline unsigned long
219 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
222 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
223 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
224 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
226 return nfs_block_bits(bsize, nrbitsp);
230 * Obtain the root inode of the file system.
232 static struct inode *
233 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
235 struct nfs_server *server = NFS_SB(sb);
239 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
241 dprintk("nfs_get_root: getattr error = %d\n", -error);
242 return ERR_PTR(error);
245 rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
247 return ERR_PTR(-ENOMEM);
252 * Do NFS version-independent mount processing, and sanity checking
255 nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
257 struct nfs_server *server;
258 struct inode *root_inode;
259 struct nfs_fattr fattr;
260 struct nfs_fsinfo fsinfo = {
263 struct nfs_pathconf pathinfo = {
266 int no_root_error = 0;
268 /* We probably want something more informative here */
269 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
273 sb->s_magic = NFS_SUPER_MAGIC;
275 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
276 /* Did getting the root inode fail? */
277 if (IS_ERR(root_inode)) {
278 no_root_error = PTR_ERR(root_inode);
281 sb->s_root = d_alloc_root(root_inode);
283 no_root_error = -ENOMEM;
286 sb->s_root->d_op = server->rpc_ops->dentry_ops;
288 /* Get some general file system info */
289 if (server->namelen == 0 &&
290 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
291 server->namelen = pathinfo.max_namelen;
292 /* Work out a lot of parameters */
293 if (server->rsize == 0)
294 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
295 if (server->wsize == 0)
296 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
297 if (sb->s_blocksize == 0) {
298 if (fsinfo.wtmult == 0) {
299 sb->s_blocksize = 512;
300 sb->s_blocksize_bits = 9;
302 sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
303 &sb->s_blocksize_bits);
306 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
307 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
308 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
309 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
311 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
312 if (server->rpages > NFS_READ_MAXIOV) {
313 server->rpages = NFS_READ_MAXIOV;
314 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
317 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
318 if (server->wpages > NFS_WRITE_MAXIOV) {
319 server->wpages = NFS_WRITE_MAXIOV;
320 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
323 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
324 if (server->dtsize > PAGE_CACHE_SIZE)
325 server->dtsize = PAGE_CACHE_SIZE;
326 if (server->dtsize > server->rsize)
327 server->dtsize = server->rsize;
329 if (server->flags & NFS_MOUNT_NOAC) {
330 server->acregmin = server->acregmax = 0;
331 server->acdirmin = server->acdirmax = 0;
332 sb->s_flags |= MS_SYNCHRONOUS;
334 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
336 if (server->flags & NFS_MOUNT_TAGXID)
337 sb->s_flags |= MS_TAGXID;
339 sb->s_maxbytes = fsinfo.maxfilesize;
340 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
341 sb->s_maxbytes = MAX_LFS_FILESIZE;
343 /* We're airborne Set socket buffersize */
344 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
346 /* Yargs. It didn't work out. */
348 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
349 if (!IS_ERR(root_inode))
351 return no_root_error;
355 * Create an RPC client handle.
357 static struct rpc_clnt *
358 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
360 struct rpc_timeout timeparms;
361 struct rpc_xprt *xprt = NULL;
362 struct rpc_clnt *clnt = NULL;
363 int tcp = (data->flags & NFS_MOUNT_TCP);
365 /* Initialize timeout values */
366 timeparms.to_initval = data->timeo * HZ / 10;
367 timeparms.to_retries = data->retrans;
368 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
369 timeparms.to_exponential = 1;
371 if (!timeparms.to_initval)
372 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
373 if (!timeparms.to_retries)
374 timeparms.to_retries = 5;
376 /* create transport and client */
377 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
378 &server->addr, &timeparms);
380 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
381 return (struct rpc_clnt *)xprt;
383 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
384 server->rpc_ops->version, data->pseudoflavor);
386 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
390 clnt->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
391 clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
392 clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
393 clnt->cl_tagxid = (server->flags & NFS_MOUNT_TAGXID) ? 1 : 0;
404 * The way this works is that the mount process passes a structure
405 * in the data argument which contains the server's IP address
406 * and the root file handle obtained from the server's mount
407 * daemon. We stash these away in the private superblock fields.
410 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
412 struct nfs_server *server;
414 rpc_authflavor_t authflavor;
417 sb->s_blocksize_bits = 0;
420 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
422 server->rsize = nfs_block_size(data->rsize, NULL);
424 server->wsize = nfs_block_size(data->wsize, NULL);
425 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
427 server->acregmin = data->acregmin*HZ;
428 server->acregmax = data->acregmax*HZ;
429 server->acdirmin = data->acdirmin*HZ;
430 server->acdirmax = data->acdirmax*HZ;
432 server->namelen = data->namlen;
433 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
434 if (!server->hostname)
436 strcpy(server->hostname, data->hostname);
438 /* Check NFS protocol revision and initialize RPC op vector
439 * and file handle pool. */
440 if (server->flags & NFS_MOUNT_VER3) {
442 server->rpc_ops = &nfs_v3_clientops;
443 server->caps |= NFS_CAP_READDIRPLUS;
444 if (data->version < 4) {
445 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
449 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
453 server->rpc_ops = &nfs_v2_clientops;
456 /* Fill in pseudoflavor for mount version < 5 */
457 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
458 data->pseudoflavor = RPC_AUTH_UNIX;
459 authflavor = data->pseudoflavor; /* save for sb_init() */
460 /* XXX maybe we want to add a server->pseudoflavor field */
462 /* Create RPC client handles */
463 server->client = nfs_create_client(server, data);
464 if (IS_ERR(server->client))
466 /* RFC 2623, sec 2.3.2 */
467 if (authflavor != RPC_AUTH_UNIX) {
468 server->client_sys = rpc_clone_client(server->client);
469 if (server->client_sys == NULL)
471 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
474 atomic_inc(&server->client->cl_count);
475 server->client_sys = server->client;
478 /* Fire up rpciod if not yet running */
479 if (rpciod_up() != 0) {
480 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
484 sb->s_op = &nfs_sops;
485 err = nfs_sb_init(sb, authflavor);
489 if (server->flags & NFS_MOUNT_VER3) {
490 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
491 server->namelen = NFS3_MAXNAMLEN;
493 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
494 server->namelen = NFS2_MAXNAMLEN;
497 /* Check whether to start the lockd process */
498 if (!(server->flags & NFS_MOUNT_NONLM))
505 rpc_shutdown_client(server->client);
506 if (server->client_sys)
507 rpc_shutdown_client(server->client_sys);
509 if (server->hostname)
510 kfree(server->hostname);
515 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
517 struct nfs_server *server = NFS_SB(sb);
518 unsigned char blockbits;
519 unsigned long blockres;
520 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
521 struct nfs_fattr fattr;
522 struct nfs_fsstat res = {
529 error = server->rpc_ops->statfs(server, rootfh, &res);
530 buf->f_type = NFS_SUPER_MAGIC;
534 buf->f_bsize = sb->s_blocksize;
535 blockbits = sb->s_blocksize_bits;
536 blockres = (1 << blockbits) - 1;
537 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
538 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
539 buf->f_bavail = (res.abytes + blockres) >> blockbits;
540 buf->f_files = res.tfiles;
541 buf->f_ffree = res.afiles;
543 buf->f_namelen = server->namelen;
550 printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
551 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
556 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
558 static struct proc_nfs_info {
563 { NFS_MOUNT_SOFT, ",soft", ",hard" },
564 { NFS_MOUNT_INTR, ",intr", "" },
565 { NFS_MOUNT_POSIX, ",posix", "" },
566 { NFS_MOUNT_TCP, ",tcp", ",udp" },
567 { NFS_MOUNT_NOCTO, ",nocto", "" },
568 { NFS_MOUNT_NOAC, ",noac", "" },
569 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
570 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
571 { NFS_MOUNT_TAGXID, ",tagxid", "" },
574 struct proc_nfs_info *nfs_infop;
575 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
577 seq_printf(m, ",v%d", nfss->rpc_ops->version);
578 seq_printf(m, ",rsize=%d", nfss->rsize);
579 seq_printf(m, ",wsize=%d", nfss->wsize);
580 if (nfss->acregmin != 3*HZ)
581 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
582 if (nfss->acregmax != 60*HZ)
583 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
584 if (nfss->acdirmin != 30*HZ)
585 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
586 if (nfss->acdirmax != 60*HZ)
587 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
588 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
589 if (nfss->flags & nfs_infop->flag)
590 seq_puts(m, nfs_infop->str);
592 seq_puts(m, nfs_infop->nostr);
594 seq_puts(m, ",addr=");
595 seq_escape(m, nfss->hostname, " \t\n\\");
600 * Invalidate the local caches
603 nfs_zap_caches(struct inode *inode)
605 struct nfs_inode *nfsi = NFS_I(inode);
606 int mode = inode->i_mode;
608 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
609 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
611 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
612 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
613 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
615 nfsi->flags |= NFS_INO_INVALID_ATTR;
619 * Invalidate, but do not unhash, the inode
622 nfs_invalidate_inode(struct inode *inode)
624 umode_t save_mode = inode->i_mode;
626 make_bad_inode(inode);
627 inode->i_mode = save_mode;
628 nfs_zap_caches(inode);
631 struct nfs_find_desc {
633 struct nfs_fattr *fattr;
637 * In NFSv3 we can have 64bit inode numbers. In order to support
638 * this, and re-exported directories (also seen in NFSv2)
639 * we are forced to allow 2 different inodes to have the same
643 nfs_find_actor(struct inode *inode, void *opaque)
645 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
646 struct nfs_fh *fh = desc->fh;
647 struct nfs_fattr *fattr = desc->fattr;
649 if (NFS_FILEID(inode) != fattr->fileid)
651 if (memcmp(NFS_FH(inode), fh, sizeof(struct nfs_fh)) != 0)
653 if (is_bad_inode(inode))
659 nfs_init_locked(struct inode *inode, void *opaque)
661 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
662 struct nfs_fh *fh = desc->fh;
663 struct nfs_fattr *fattr = desc->fattr;
665 NFS_FILEID(inode) = fattr->fileid;
666 memcpy(NFS_FH(inode), fh, sizeof(struct nfs_fh));
670 /* Don't use READDIRPLUS on directories that we believe are too large */
671 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
674 * This is our front-end to iget that looks up inodes by file handle
675 * instead of inode number.
678 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
680 struct nfs_find_desc desc = {
684 struct inode *inode = NULL;
687 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
691 printk("NFS: Buggy server - nlink == 0!\n");
695 hash = nfs_fattr_to_ino_t(fattr);
697 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
700 if (inode->i_state & I_NEW) {
701 struct nfs_inode *nfsi = NFS_I(inode);
703 /* We set i_ino for the few things that still rely on it,
707 /* We can't support update_atime(), since the server will reset it */
708 inode->i_flags |= S_NOATIME|S_NOCMTIME;
709 inode->i_mode = fattr->mode;
710 /* Why so? Because we want revalidate for devices/FIFOs, and
711 * that's precisely what we have in nfs_file_inode_operations.
713 inode->i_op = &nfs_file_inode_operations;
714 if (S_ISREG(inode->i_mode)) {
715 inode->i_fop = &nfs_file_operations;
716 inode->i_data.a_ops = &nfs_file_aops;
717 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
718 } else if (S_ISDIR(inode->i_mode)) {
719 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
720 inode->i_fop = &nfs_dir_operations;
721 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
722 && fattr->size <= NFS_LIMIT_READDIRPLUS)
723 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
724 } else if (S_ISLNK(inode->i_mode))
725 inode->i_op = &nfs_symlink_inode_operations;
727 init_special_inode(inode, inode->i_mode, fattr->rdev);
729 nfsi->read_cache_jiffies = fattr->timestamp;
730 inode->i_atime = fattr->atime;
731 inode->i_mtime = fattr->mtime;
732 inode->i_ctime = fattr->ctime;
733 if (fattr->valid & NFS_ATTR_FATTR_V4)
734 nfsi->change_attr = fattr->change_attr;
735 inode->i_size = nfs_size_to_loff_t(fattr->size);
736 inode->i_nlink = fattr->nlink;
737 inode->i_uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
738 inode->i_gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
739 inode->i_xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
740 /* maybe fattr->xid someday */
741 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
743 * report the blocks in 512byte units
745 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
746 inode->i_blksize = inode->i_sb->s_blocksize;
748 inode->i_blocks = fattr->du.nfs2.blocks;
749 inode->i_blksize = fattr->du.nfs2.blocksize;
751 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
752 nfsi->attrtimeo_timestamp = jiffies;
753 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
754 nfsi->cache_access.cred = NULL;
756 unlock_new_inode(inode);
758 nfs_refresh_inode(inode, fattr);
759 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
761 (long long)NFS_FILEID(inode),
762 atomic_read(&inode->i_count));
768 make_bad_inode(inode);
773 printk("nfs_fhget: iget failed\n");
777 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_XID|ATTR_SIZE|\
778 ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
781 nfs_setattr(struct dentry *dentry, struct iattr *attr)
783 struct inode *inode = dentry->d_inode;
784 struct nfs_fattr fattr;
787 if (attr->ia_valid & ATTR_SIZE) {
788 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
789 attr->ia_valid &= ~ATTR_SIZE;
792 /* Optimization: if the end result is no change, don't RPC */
793 attr->ia_valid &= NFS_VALID_ATTRS;
794 if (attr->ia_valid == 0)
798 nfs_begin_data_update(inode);
799 /* Write all dirty data if we're changing file permissions or size */
800 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
801 if (filemap_fdatawrite(inode->i_mapping) == 0)
802 filemap_fdatawait(inode->i_mapping);
805 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
807 nfs_refresh_inode(inode, &fattr);
808 if ((attr->ia_valid & ATTR_MODE) != 0) {
810 mode = inode->i_mode & ~S_IALLUGO;
811 mode |= attr->ia_mode & S_IALLUGO;
812 inode->i_mode = mode;
814 if ((attr->ia_valid & ATTR_UID) != 0)
815 inode->i_uid = attr->ia_uid;
816 if ((attr->ia_valid & ATTR_GID) != 0)
817 inode->i_gid = attr->ia_gid;
818 if ((attr->ia_valid & ATTR_XID) != 0)
819 inode->i_xid = attr->ia_xid;
820 if ((attr->ia_valid & ATTR_SIZE) != 0) {
821 inode->i_size = attr->ia_size;
822 vmtruncate(inode, attr->ia_size);
825 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
826 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
832 nfs_end_data_update(inode);
838 * Wait for the inode to get unlocked.
839 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
842 nfs_wait_on_inode(struct inode *inode, int flag)
844 struct rpc_clnt *clnt = NFS_CLIENT(inode);
845 struct nfs_inode *nfsi = NFS_I(inode);
848 if (!(NFS_FLAGS(inode) & flag))
850 atomic_inc(&inode->i_count);
851 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
852 !(NFS_FLAGS(inode) & flag));
857 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
859 struct inode *inode = dentry->d_inode;
860 struct nfs_inode *nfsi = NFS_I(inode);
861 int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
864 if (__IS_FLG(inode, MS_NOATIME))
866 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
868 /* We may force a getattr if the user cares about atime */
870 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
872 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
874 generic_fillattr(inode, stat);
879 * Ensure that mmap has a recent RPC credential for use when writing out
883 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
885 struct rpc_cred **p = &NFS_I(inode)->mm_cred,
888 *p = get_rpccred(cred);
890 put_rpccred(oldcred);
894 * These are probably going to contain hooks for
895 * allocating and releasing RPC credentials for
896 * the file. I'll have to think about Tronds patch
899 int nfs_open(struct inode *inode, struct file *filp)
901 struct rpc_auth *auth;
902 struct rpc_cred *cred;
904 auth = NFS_CLIENT(inode)->cl_auth;
905 cred = rpcauth_lookupcred(auth, 0);
906 filp->private_data = cred;
907 if ((filp->f_mode & FMODE_WRITE) != 0) {
908 nfs_set_mmcred(inode, cred);
909 nfs_begin_data_update(inode);
914 int nfs_release(struct inode *inode, struct file *filp)
916 struct rpc_cred *cred;
919 if ((filp->f_mode & FMODE_WRITE) != 0)
920 nfs_end_data_update(inode);
921 cred = nfs_file_cred(filp);
929 * This function is called whenever some part of NFS notices that
930 * the cached attributes have to be refreshed.
933 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
935 int status = -ESTALE;
936 struct nfs_fattr fattr;
937 struct nfs_inode *nfsi = NFS_I(inode);
938 unsigned long verifier;
941 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
942 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
945 if (!inode || is_bad_inode(inode))
947 if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
950 while (NFS_REVALIDATING(inode)) {
951 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
954 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
956 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
958 status = NFS_STALE(inode) ? -ESTALE : 0;
961 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
963 /* Protect against RPC races by saving the change attribute */
964 verifier = nfs_save_change_attribute(inode);
965 status = NFS_PROTO(inode)->getattr(inode, &fattr);
967 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
969 (long long)NFS_FILEID(inode), status);
970 if (status == -ESTALE) {
971 NFS_FLAGS(inode) |= NFS_INO_STALE;
972 if (inode != inode->i_sb->s_root->d_inode)
973 remove_inode_hash(inode);
978 status = nfs_update_inode(inode, &fattr, verifier);
980 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
982 (long long)NFS_FILEID(inode), status);
987 * We may need to keep the attributes marked as invalid if
988 * we raced with nfs_end_attr_update().
990 if (verifier == nfsi->cache_change_attribute)
991 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
992 /* Do the page cache invalidation */
993 if (flags & NFS_INO_INVALID_DATA) {
994 if (S_ISREG(inode->i_mode)) {
995 if (filemap_fdatawrite(inode->i_mapping) == 0)
996 filemap_fdatawait(inode->i_mapping);
999 nfsi->flags &= ~NFS_INO_INVALID_DATA;
1000 invalidate_inode_pages2(inode->i_mapping);
1001 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1002 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1004 (long long)NFS_FILEID(inode));
1005 /* This ensures we revalidate dentries */
1006 nfsi->cache_change_attribute++;
1008 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1010 (long long)NFS_FILEID(inode));
1012 NFS_FLAGS(inode) &= ~NFS_INO_STALE;
1014 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1015 wake_up(&nfsi->nfs_i_wait);
1022 * nfs_begin_data_update
1023 * @inode - pointer to inode
1024 * Declare that a set of operations will update file data on the server
1026 void nfs_begin_data_update(struct inode *inode)
1028 atomic_inc(&NFS_I(inode)->data_updates);
1032 * nfs_end_data_update
1033 * @inode - pointer to inode
1034 * Declare end of the operations that will update file data
1035 * This will mark the inode as immediately needing revalidation
1036 * of its attribute cache.
1038 void nfs_end_data_update(struct inode *inode)
1040 struct nfs_inode *nfsi = NFS_I(inode);
1042 /* Mark the attribute cache for revalidation */
1043 nfsi->flags |= NFS_INO_INVALID_ATTR;
1044 /* Directories and symlinks: invalidate page cache too */
1045 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1046 nfsi->flags |= NFS_INO_INVALID_DATA;
1047 nfsi->cache_change_attribute ++;
1048 atomic_dec(&nfsi->data_updates);
1052 * nfs_end_data_update_defer
1053 * @inode - pointer to inode
1054 * Declare end of the operations that will update file data
1055 * This will defer marking the inode as needing revalidation
1056 * unless there are no other pending updates.
1058 void nfs_end_data_update_defer(struct inode *inode)
1060 struct nfs_inode *nfsi = NFS_I(inode);
1062 if (atomic_dec_and_test(&nfsi->data_updates)) {
1063 /* Mark the attribute cache for revalidation */
1064 nfsi->flags |= NFS_INO_INVALID_ATTR;
1065 /* Directories and symlinks: invalidate page cache too */
1066 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1067 nfsi->flags |= NFS_INO_INVALID_DATA;
1068 nfsi->cache_change_attribute ++;
1073 * nfs_refresh_inode - verify consistency of the inode attribute cache
1074 * @inode - pointer to inode
1075 * @fattr - updated attributes
1077 * Verifies the attribute cache. If we have just changed the attributes,
1078 * so that fattr carries weak cache consistency data, then it may
1079 * also update the ctime/mtime/change_attribute.
1081 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1083 struct nfs_inode *nfsi = NFS_I(inode);
1084 loff_t cur_size, new_isize;
1090 /* Are we in the process of updating data on the server? */
1091 data_unstable = nfs_caches_unstable(inode);
1093 if (fattr->valid & NFS_ATTR_FATTR_V4) {
1094 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1095 && nfsi->change_attr == fattr->pre_change_attr)
1096 nfsi->change_attr = fattr->change_attr;
1097 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1098 nfsi->flags |= NFS_INO_INVALID_ATTR;
1101 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1104 /* Has the inode gone and changed behind our back? */
1105 if (nfsi->fileid != fattr->fileid
1106 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1109 cur_size = i_size_read(inode);
1110 new_isize = nfs_size_to_loff_t(fattr->size);
1112 /* If we have atomic WCC data, we may update some attributes */
1113 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1114 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1115 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1116 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1117 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1120 /* Verify a few of the more important attributes */
1121 if (!data_unstable) {
1122 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1123 || cur_size != new_isize)
1124 nfsi->flags |= NFS_INO_INVALID_ATTR;
1125 } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1126 nfsi->flags |= NFS_INO_INVALID_ATTR;
1128 uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
1129 gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
1130 xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
1132 /* Have any file permissions changed? */
1133 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1134 || inode->i_uid != uid
1135 || inode->i_gid != gid
1136 || inode->i_xid != xid)
1137 nfsi->flags |= NFS_INO_INVALID_ATTR;
1139 /* Has the link count changed? */
1140 if (inode->i_nlink != fattr->nlink)
1141 nfsi->flags |= NFS_INO_INVALID_ATTR;
1143 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1144 nfsi->flags |= NFS_INO_INVALID_ATIME;
1146 nfsi->read_cache_jiffies = fattr->timestamp;
1151 * Many nfs protocol calls return the new file attributes after
1152 * an operation. Here we update the inode to reflect the state
1153 * of the server's inode.
1155 * This is a bit tricky because we have to make sure all dirty pages
1156 * have been sent off to the server before calling invalidate_inode_pages.
1157 * To make sure no other process adds more write requests while we try
1158 * our best to flush them, we make them sleep during the attribute refresh.
1160 * A very similar scenario holds for the dir cache.
1162 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1164 struct nfs_inode *nfsi = NFS_I(inode);
1167 unsigned int invalid = 0;
1174 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1175 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1176 atomic_read(&inode->i_count), fattr->valid);
1178 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1181 if (nfsi->fileid != fattr->fileid) {
1182 printk(KERN_ERR "%s: inode number mismatch\n"
1183 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1185 inode->i_sb->s_id, (long long)nfsi->fileid,
1186 inode->i_sb->s_id, (long long)fattr->fileid);
1191 * Make sure the inode's type hasn't changed.
1193 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1197 * Update the read time so we don't revalidate too often.
1199 nfsi->read_cache_jiffies = fattr->timestamp;
1201 /* Are we racing with known updates of the metadata on the server? */
1202 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1204 /* Check if the file size agrees */
1205 new_size = fattr->size;
1206 new_isize = nfs_size_to_loff_t(fattr->size);
1207 cur_isize = i_size_read(inode);
1208 if (cur_isize != new_size) {
1209 #ifdef NFS_DEBUG_VERBOSE
1210 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1213 * If we have pending writebacks, things can get
1216 if (S_ISREG(inode->i_mode) && data_unstable) {
1217 if (new_isize > cur_isize) {
1218 inode->i_size = new_isize;
1219 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1222 inode->i_size = new_isize;
1223 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1228 * Note: we don't check inode->i_mtime since pipes etc.
1229 * can change this value in VFS without requiring a
1230 * cache revalidation.
1232 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1233 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1234 #ifdef NFS_DEBUG_VERBOSE
1235 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1238 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1241 if ((fattr->valid & NFS_ATTR_FATTR_V4)
1242 && nfsi->change_attr != fattr->change_attr) {
1243 #ifdef NFS_DEBUG_VERBOSE
1244 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1245 inode->i_sb->s_id, inode->i_ino);
1247 nfsi->change_attr = fattr->change_attr;
1249 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1252 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1253 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1255 uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
1256 gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
1257 xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
1259 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1260 inode->i_uid != uid ||
1261 inode->i_gid != gid ||
1262 inode->i_xid != xid) {
1263 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1268 invalid |= NFS_INO_INVALID_ATTR;
1271 inode->i_mode = fattr->mode;
1272 inode->i_nlink = fattr->nlink;
1277 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1279 * report the blocks in 512byte units
1281 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1282 inode->i_blksize = inode->i_sb->s_blocksize;
1284 inode->i_blocks = fattr->du.nfs2.blocks;
1285 inode->i_blksize = fattr->du.nfs2.blocksize;
1288 /* Update attrtimeo value if we're out of the unstable period */
1289 if (invalid & NFS_INO_INVALID_ATTR) {
1290 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1291 nfsi->attrtimeo_timestamp = jiffies;
1292 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1293 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1294 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1295 nfsi->attrtimeo_timestamp = jiffies;
1297 /* Don't invalidate the data if we were to blame */
1298 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1299 || S_ISLNK(inode->i_mode)))
1300 invalid &= ~NFS_INO_INVALID_DATA;
1301 nfsi->flags |= invalid;
1306 * Big trouble! The inode has become a different object.
1309 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1310 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1313 * No need to worry about unhashing the dentry, as the
1314 * lookup validation will know that the inode is bad.
1315 * (But we fall through to invalidate the caches.)
1317 nfs_invalidate_inode(inode);
1323 * File system information
1326 static int nfs_set_super(struct super_block *s, void *data)
1328 s->s_fs_info = data;
1329 return set_anon_super(s, data);
1332 static int nfs_compare_super(struct super_block *sb, void *data)
1334 struct nfs_server *server = data;
1335 struct nfs_server *old = NFS_SB(sb);
1337 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1339 if (old->addr.sin_port != server->addr.sin_port)
1341 return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1344 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1345 int flags, const char *dev_name, void *raw_data)
1348 struct nfs_server *server;
1349 struct super_block *s;
1350 struct nfs_fh *root;
1351 struct nfs_mount_data *data = raw_data;
1354 printk("nfs_read_super: missing data argument\n");
1355 return ERR_PTR(-EINVAL);
1358 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1360 return ERR_PTR(-ENOMEM);
1361 memset(server, 0, sizeof(struct nfs_server));
1362 /* Zero out the NFS state stuff */
1363 init_nfsv4_state(server);
1366 memcpy(root, &data->root, sizeof(*root));
1367 if (root->size < sizeof(root->data))
1368 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1370 if (data->version != NFS_MOUNT_VERSION) {
1371 printk("nfs warning: mount version %s than kernel\n",
1372 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1373 if (data->version < 2)
1375 if (data->version < 3)
1377 if (data->version < 4) {
1378 data->flags &= ~NFS_MOUNT_VER3;
1379 memset(root, 0, sizeof(*root));
1380 root->size = NFS2_FHSIZE;
1381 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1383 if (data->version < 5)
1384 data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1387 if (root->size > sizeof(root->data)) {
1388 printk("nfs_get_sb: invalid root filehandle\n");
1390 return ERR_PTR(-EINVAL);
1392 /* We now require that the mount process passes the remote address */
1393 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1394 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1395 printk("NFS: mount program didn't pass remote address!\n");
1397 return ERR_PTR(-EINVAL);
1400 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1402 if (IS_ERR(s) || s->s_root) {
1409 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1411 up_write(&s->s_umount);
1412 deactivate_super(s);
1413 return ERR_PTR(error);
1415 s->s_flags |= MS_ACTIVE;
1419 static void nfs_kill_super(struct super_block *s)
1421 struct nfs_server *server = NFS_SB(s);
1426 static struct file_system_type nfs_fs_type = {
1427 .owner = THIS_MODULE,
1429 .get_sb = nfs_get_sb,
1430 .kill_sb = nfs_kill_super,
1431 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1434 #ifdef CONFIG_NFS_V4
1436 static void nfs4_clear_inode(struct inode *);
1438 static struct super_operations nfs4_sops = {
1439 .alloc_inode = nfs_alloc_inode,
1440 .destroy_inode = nfs_destroy_inode,
1441 .write_inode = nfs_write_inode,
1442 .delete_inode = nfs_delete_inode,
1443 .put_super = nfs_put_super,
1444 .statfs = nfs_statfs,
1445 .clear_inode = nfs4_clear_inode,
1446 .umount_begin = nfs_umount_begin,
1447 .show_options = nfs_show_options,
1451 * Clean out any remaining NFSv4 state that might be left over due
1452 * to open() calls that passed nfs_atomic_lookup, but failed to call
1455 static void nfs4_clear_inode(struct inode *inode)
1457 struct nfs_inode *nfsi = NFS_I(inode);
1459 while (!list_empty(&nfsi->open_states)) {
1460 struct nfs4_state *state;
1462 state = list_entry(nfsi->open_states.next,
1465 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1468 (long long)NFS_FILEID(inode),
1470 BUG_ON(atomic_read(&state->count) != 1);
1471 nfs4_close_state(state, state->state);
1473 /* Now call standard NFS clear_inode() code */
1474 nfs_clear_inode(inode);
1478 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1480 struct nfs_server *server;
1481 struct nfs4_client *clp = NULL;
1482 struct rpc_xprt *xprt = NULL;
1483 struct rpc_clnt *clnt = NULL;
1484 struct rpc_timeout timeparms;
1485 rpc_authflavor_t authflavour;
1486 int proto, err = -EIO;
1488 sb->s_blocksize_bits = 0;
1489 sb->s_blocksize = 0;
1490 server = NFS_SB(sb);
1491 if (data->rsize != 0)
1492 server->rsize = nfs_block_size(data->rsize, NULL);
1493 if (data->wsize != 0)
1494 server->wsize = nfs_block_size(data->wsize, NULL);
1495 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1497 /* NFSv4 doesn't use NLM locking */
1498 server->flags |= NFS_MOUNT_NONLM;
1500 server->acregmin = data->acregmin*HZ;
1501 server->acregmax = data->acregmax*HZ;
1502 server->acdirmin = data->acdirmin*HZ;
1503 server->acdirmax = data->acdirmax*HZ;
1505 server->rpc_ops = &nfs_v4_clientops;
1506 /* Initialize timeout values */
1508 timeparms.to_initval = data->timeo * HZ / 10;
1509 timeparms.to_retries = data->retrans;
1510 timeparms.to_exponential = 1;
1511 if (!timeparms.to_retries)
1512 timeparms.to_retries = 5;
1514 proto = data->proto;
1515 /* Which IP protocol do we use? */
1518 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1519 if (!timeparms.to_initval)
1520 timeparms.to_initval = 600 * HZ / 10;
1523 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1524 if (!timeparms.to_initval)
1525 timeparms.to_initval = 11 * HZ / 10;
1531 clp = nfs4_get_client(&server->addr.sin_addr);
1533 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1537 /* Now create transport and client */
1538 authflavour = RPC_AUTH_UNIX;
1539 if (data->auth_flavourlen != 0) {
1540 if (data->auth_flavourlen > 1)
1541 printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1542 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1548 down_write(&clp->cl_sem);
1549 if (clp->cl_rpcclient == NULL) {
1550 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1552 up_write(&clp->cl_sem);
1553 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1554 err = PTR_ERR(xprt);
1557 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1558 server->rpc_ops->version, authflavour);
1560 up_write(&clp->cl_sem);
1561 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1563 err = PTR_ERR(clnt);
1566 clnt->cl_chatty = 1;
1567 clp->cl_rpcclient = clnt;
1568 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1569 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1572 if (list_empty(&clp->cl_superblocks))
1573 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1574 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1575 clnt = rpc_clone_client(clp->cl_rpcclient);
1577 server->nfs4_state = clp;
1578 up_write(&clp->cl_sem);
1582 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1583 err = PTR_ERR(clnt);
1584 goto out_remove_list;
1587 clnt->cl_intr = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1588 clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1589 server->client = clnt;
1592 if (server->nfs4_state->cl_idmap == NULL) {
1593 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1597 if (clnt->cl_auth->au_flavor != authflavour) {
1598 if (rpcauth_create(authflavour, clnt) == NULL) {
1599 printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1604 /* Fire up rpciod if not yet running */
1605 if (rpciod_up() != 0) {
1606 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1610 sb->s_op = &nfs4_sops;
1611 err = nfs_sb_init(sb, authflavour);
1616 rpc_shutdown_client(server->client);
1618 down_write(&server->nfs4_state->cl_sem);
1619 list_del_init(&server->nfs4_siblings);
1620 up_write(&server->nfs4_state->cl_sem);
1621 destroy_nfsv4_state(server);
1624 nfs4_put_client(clp);
1628 static int nfs4_compare_super(struct super_block *sb, void *data)
1630 struct nfs_server *server = data;
1631 struct nfs_server *old = NFS_SB(sb);
1633 if (strcmp(server->hostname, old->hostname) != 0)
1635 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1641 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1646 return ERR_PTR(-EINVAL);
1647 if (src->len < maxlen)
1650 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1652 return ERR_PTR(-ENOMEM);
1654 if (copy_from_user(dst, src->data, maxlen)) {
1657 return ERR_PTR(-EFAULT);
1663 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1664 int flags, const char *dev_name, void *raw_data)
1667 struct nfs_server *server;
1668 struct super_block *s;
1669 struct nfs4_mount_data *data = raw_data;
1673 printk("nfs_read_super: missing data argument\n");
1674 return ERR_PTR(-EINVAL);
1677 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1679 return ERR_PTR(-ENOMEM);
1680 memset(server, 0, sizeof(struct nfs_server));
1681 /* Zero out the NFS state stuff */
1682 init_nfsv4_state(server);
1684 if (data->version != NFS4_MOUNT_VERSION) {
1685 printk("nfs warning: mount version %s than kernel\n",
1686 data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1689 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1692 server->hostname = p;
1694 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1697 server->mnt_path = p;
1699 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1700 sizeof(server->ip_addr) - 1);
1704 /* We now require that the mount process passes the remote address */
1705 if (data->host_addrlen != sizeof(server->addr)) {
1706 s = ERR_PTR(-EINVAL);
1709 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1710 s = ERR_PTR(-EFAULT);
1713 if (server->addr.sin_family != AF_INET ||
1714 server->addr.sin_addr.s_addr == INADDR_ANY) {
1715 printk("NFS: mount program didn't pass remote IP address!\n");
1716 s = ERR_PTR(-EINVAL);
1720 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1722 if (IS_ERR(s) || s->s_root)
1727 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1729 up_write(&s->s_umount);
1730 deactivate_super(s);
1731 return ERR_PTR(error);
1733 s->s_flags |= MS_ACTIVE;
1736 s = (struct super_block *)p;
1738 if (server->mnt_path)
1739 kfree(server->mnt_path);
1740 if (server->hostname)
1741 kfree(server->hostname);
1746 static struct file_system_type nfs4_fs_type = {
1747 .owner = THIS_MODULE,
1749 .get_sb = nfs4_get_sb,
1750 .kill_sb = nfs_kill_super,
1751 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1754 #define nfs4_zero_state(nfsi) \
1756 INIT_LIST_HEAD(&(nfsi)->open_states); \
1758 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1759 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1761 #define nfs4_zero_state(nfsi) \
1763 #define register_nfs4fs() (0)
1764 #define unregister_nfs4fs()
1767 extern int nfs_init_nfspagecache(void);
1768 extern void nfs_destroy_nfspagecache(void);
1769 extern int nfs_init_readpagecache(void);
1770 extern int nfs_destroy_readpagecache(void);
1771 extern int nfs_init_writepagecache(void);
1772 extern int nfs_destroy_writepagecache(void);
1774 static kmem_cache_t * nfs_inode_cachep;
1776 static struct inode *nfs_alloc_inode(struct super_block *sb)
1778 struct nfs_inode *nfsi;
1779 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1783 nfsi->mm_cred = NULL;
1784 nfs4_zero_state(nfsi);
1785 return &nfsi->vfs_inode;
1788 static void nfs_destroy_inode(struct inode *inode)
1790 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1793 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1795 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1797 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1798 SLAB_CTOR_CONSTRUCTOR) {
1799 inode_init_once(&nfsi->vfs_inode);
1800 INIT_LIST_HEAD(&nfsi->dirty);
1801 INIT_LIST_HEAD(&nfsi->commit);
1802 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1803 atomic_set(&nfsi->data_updates, 0);
1807 init_waitqueue_head(&nfsi->nfs_i_wait);
1811 int nfs_init_inodecache(void)
1813 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1814 sizeof(struct nfs_inode),
1815 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1817 if (nfs_inode_cachep == NULL)
1823 void nfs_destroy_inodecache(void)
1825 if (kmem_cache_destroy(nfs_inode_cachep))
1826 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1832 static int __init init_nfs_fs(void)
1836 err = nfs_init_nfspagecache();
1840 err = nfs_init_inodecache();
1844 err = nfs_init_readpagecache();
1848 err = nfs_init_writepagecache();
1852 #ifdef CONFIG_PROC_FS
1853 rpc_proc_register(&nfs_rpcstat);
1855 err = register_filesystem(&nfs_fs_type);
1858 if ((err = register_nfs4fs()) != 0)
1862 rpc_proc_unregister("nfs");
1863 nfs_destroy_writepagecache();
1865 nfs_destroy_readpagecache();
1867 nfs_destroy_inodecache();
1869 nfs_destroy_nfspagecache();
1874 static void __exit exit_nfs_fs(void)
1876 nfs_destroy_writepagecache();
1877 nfs_destroy_readpagecache();
1878 nfs_destroy_inodecache();
1879 nfs_destroy_nfspagecache();
1880 #ifdef CONFIG_PROC_FS
1881 rpc_proc_unregister("nfs");
1883 unregister_filesystem(&nfs_fs_type);
1884 unregister_nfs4fs();
1887 /* Not quite true; I just maintain it */
1888 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1889 MODULE_LICENSE("GPL");
1891 module_init(init_nfs_fs)
1892 module_exit(exit_nfs_fs)