]> Pileus Git - ~andy/linux/blob - fs/nfs/inode.c
47167ab64f5ba4595320118420125e4fe1968f9b
[~andy/linux] / fs / nfs / inode.c
1 /*
2  *  linux/fs/nfs/inode.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs inode and superblock handling functions
7  *
8  *  Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19
20 #include <linux/time.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.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/sunrpc/metrics.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_mount.h>
32 #include <linux/nfs4_mount.h>
33 #include <linux/lockd/bind.h>
34 #include <linux/smp_lock.h>
35 #include <linux/seq_file.h>
36 #include <linux/mount.h>
37 #include <linux/nfs_idmap.h>
38 #include <linux/vfs.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47
48 #define NFSDBG_FACILITY         NFSDBG_VFS
49 #define NFS_PARANOIA 1
50
51 /* Maximum number of readahead requests
52  * FIXME: this should really be a sysctl so that users may tune it to suit
53  *        their needs. People that do NFS over a slow network, might for
54  *        instance want to reduce it to something closer to 1 for improved
55  *        interactive response.
56  */
57 #define NFS_MAX_READAHEAD       (RPC_DEF_SLOT_TABLE - 1)
58
59 static void nfs_invalidate_inode(struct inode *);
60 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
61
62 static struct inode *nfs_alloc_inode(struct super_block *sb);
63 static void nfs_destroy_inode(struct inode *);
64 static int nfs_write_inode(struct inode *,int);
65 static void nfs_clear_inode(struct inode *);
66 static void nfs_umount_begin(struct vfsmount *, int);
67 static int  nfs_statfs(struct super_block *, struct kstatfs *);
68 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
69 static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
70 static void nfs_zap_acl_cache(struct inode *);
71
72 static struct rpc_program       nfs_program;
73
74 static struct super_operations nfs_sops = { 
75         .alloc_inode    = nfs_alloc_inode,
76         .destroy_inode  = nfs_destroy_inode,
77         .write_inode    = nfs_write_inode,
78         .statfs         = nfs_statfs,
79         .clear_inode    = nfs_clear_inode,
80         .umount_begin   = nfs_umount_begin,
81         .show_options   = nfs_show_options,
82         .show_stats     = nfs_show_stats,
83 };
84
85 /*
86  * RPC cruft for NFS
87  */
88 static struct rpc_stat          nfs_rpcstat = {
89         .program                = &nfs_program
90 };
91 static struct rpc_version *     nfs_version[] = {
92         NULL,
93         NULL,
94         &nfs_version2,
95 #if defined(CONFIG_NFS_V3)
96         &nfs_version3,
97 #elif defined(CONFIG_NFS_V4)
98         NULL,
99 #endif
100 #if defined(CONFIG_NFS_V4)
101         &nfs_version4,
102 #endif
103 };
104
105 static struct rpc_program       nfs_program = {
106         .name                   = "nfs",
107         .number                 = NFS_PROGRAM,
108         .nrvers                 = ARRAY_SIZE(nfs_version),
109         .version                = nfs_version,
110         .stats                  = &nfs_rpcstat,
111         .pipe_dir_name          = "/nfs",
112 };
113
114 #ifdef CONFIG_NFS_V3_ACL
115 static struct rpc_stat          nfsacl_rpcstat = { &nfsacl_program };
116 static struct rpc_version *     nfsacl_version[] = {
117         [3]                     = &nfsacl_version3,
118 };
119
120 struct rpc_program              nfsacl_program = {
121         .name =                 "nfsacl",
122         .number =               NFS_ACL_PROGRAM,
123         .nrvers =               ARRAY_SIZE(nfsacl_version),
124         .version =              nfsacl_version,
125         .stats =                &nfsacl_rpcstat,
126 };
127 #endif  /* CONFIG_NFS_V3_ACL */
128
129 static inline unsigned long
130 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
131 {
132         return nfs_fileid_to_ino_t(fattr->fileid);
133 }
134
135 static int
136 nfs_write_inode(struct inode *inode, int sync)
137 {
138         int flags = sync ? FLUSH_SYNC : 0;
139         int ret;
140
141         ret = nfs_commit_inode(inode, flags);
142         if (ret < 0)
143                 return ret;
144         return 0;
145 }
146
147 static void
148 nfs_clear_inode(struct inode *inode)
149 {
150         struct nfs_inode *nfsi = NFS_I(inode);
151         struct rpc_cred *cred;
152
153         /*
154          * The following should never happen...
155          */
156         BUG_ON(nfs_have_writebacks(inode));
157         BUG_ON (!list_empty(&nfsi->open_files));
158         nfs_zap_acl_cache(inode);
159         cred = nfsi->cache_access.cred;
160         if (cred)
161                 put_rpccred(cred);
162         BUG_ON(atomic_read(&nfsi->data_updates) != 0);
163 }
164
165 static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
166 {
167         struct nfs_server *server;
168         struct rpc_clnt *rpc;
169
170         if (!(flags & MNT_FORCE))
171                 return;
172         /* -EIO all pending I/O */
173         server = NFS_SB(vfsmnt->mnt_sb);
174         rpc = server->client;
175         if (!IS_ERR(rpc))
176                 rpc_killall_tasks(rpc);
177         rpc = server->client_acl;
178         if (!IS_ERR(rpc))
179                 rpc_killall_tasks(rpc);
180 }
181
182
183 static inline unsigned long
184 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
185 {
186         /* make sure blocksize is a power of two */
187         if ((bsize & (bsize - 1)) || nrbitsp) {
188                 unsigned char   nrbits;
189
190                 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
191                         ;
192                 bsize = 1 << nrbits;
193                 if (nrbitsp)
194                         *nrbitsp = nrbits;
195         }
196
197         return bsize;
198 }
199
200 /*
201  * Calculate the number of 512byte blocks used.
202  */
203 static inline unsigned long
204 nfs_calc_block_size(u64 tsize)
205 {
206         loff_t used = (tsize + 511) >> 9;
207         return (used > ULONG_MAX) ? ULONG_MAX : used;
208 }
209
210 /*
211  * Compute and set NFS server blocksize
212  */
213 static inline unsigned long
214 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
215 {
216         if (bsize < NFS_MIN_FILE_IO_SIZE)
217                 bsize = NFS_DEF_FILE_IO_SIZE;
218         else if (bsize >= NFS_MAX_FILE_IO_SIZE)
219                 bsize = NFS_MAX_FILE_IO_SIZE;
220
221         return nfs_block_bits(bsize, nrbitsp);
222 }
223
224 static inline void
225 nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
226 {
227         sb->s_maxbytes = (loff_t)maxfilesize;
228         if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
229                 sb->s_maxbytes = MAX_LFS_FILESIZE;
230 }
231
232 /*
233  * Obtain the root inode of the file system.
234  */
235 static struct inode *
236 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
237 {
238         struct nfs_server       *server = NFS_SB(sb);
239         int                     error;
240
241         error = server->rpc_ops->getroot(server, rootfh, fsinfo);
242         if (error < 0) {
243                 dprintk("nfs_get_root: getattr error = %d\n", -error);
244                 return ERR_PTR(error);
245         }
246
247         server->fsid = fsinfo->fattr->fsid;
248         return nfs_fhget(sb, rootfh, fsinfo->fattr);
249 }
250
251 /*
252  * Do NFS version-independent mount processing, and sanity checking
253  */
254 static int
255 nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
256 {
257         struct nfs_server       *server;
258         struct inode            *root_inode;
259         struct nfs_fattr        fattr;
260         struct nfs_fsinfo       fsinfo = {
261                                         .fattr = &fattr,
262                                 };
263         struct nfs_pathconf pathinfo = {
264                         .fattr = &fattr,
265         };
266         int no_root_error = 0;
267         unsigned long max_rpc_payload;
268
269         /* We probably want something more informative here */
270         snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
271
272         server = NFS_SB(sb);
273
274         sb->s_magic      = NFS_SUPER_MAGIC;
275
276         server->io_stats = nfs_alloc_iostats();
277         if (server->io_stats == NULL)
278                 return -ENOMEM;
279
280         root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
281         /* Did getting the root inode fail? */
282         if (IS_ERR(root_inode)) {
283                 no_root_error = PTR_ERR(root_inode);
284                 goto out_no_root;
285         }
286         sb->s_root = d_alloc_root(root_inode);
287         if (!sb->s_root) {
288                 no_root_error = -ENOMEM;
289                 goto out_no_root;
290         }
291         sb->s_root->d_op = server->rpc_ops->dentry_ops;
292
293         /* mount time stamp, in seconds */
294         server->mount_time = jiffies;
295
296         /* Get some general file system info */
297         if (server->namelen == 0 &&
298             server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
299                 server->namelen = pathinfo.max_namelen;
300         /* Work out a lot of parameters */
301         if (server->rsize == 0)
302                 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
303         if (server->wsize == 0)
304                 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
305
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);
310
311         max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
312         if (server->rsize > max_rpc_payload)
313                 server->rsize = max_rpc_payload;
314         if (server->rsize > NFS_MAX_FILE_IO_SIZE)
315                 server->rsize = NFS_MAX_FILE_IO_SIZE;
316         server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
317
318         if (server->wsize > max_rpc_payload)
319                 server->wsize = max_rpc_payload;
320         if (server->wsize > NFS_MAX_FILE_IO_SIZE)
321                 server->wsize = NFS_MAX_FILE_IO_SIZE;
322         server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
323
324         if (sb->s_blocksize == 0)
325                 sb->s_blocksize = nfs_block_bits(server->wsize,
326                                                          &sb->s_blocksize_bits);
327         server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
328
329         server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
330         if (server->dtsize > PAGE_CACHE_SIZE)
331                 server->dtsize = PAGE_CACHE_SIZE;
332         if (server->dtsize > server->rsize)
333                 server->dtsize = server->rsize;
334
335         if (server->flags & NFS_MOUNT_NOAC) {
336                 server->acregmin = server->acregmax = 0;
337                 server->acdirmin = server->acdirmax = 0;
338                 sb->s_flags |= MS_SYNCHRONOUS;
339         }
340         server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
341
342         nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
343
344         server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
345         server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
346
347         /* We're airborne Set socket buffersize */
348         rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
349         return 0;
350         /* Yargs. It didn't work out. */
351 out_no_root:
352         dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
353         if (!IS_ERR(root_inode))
354                 iput(root_inode);
355         return no_root_error;
356 }
357
358 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
359 {
360         to->to_initval = timeo * HZ / 10;
361         to->to_retries = retrans;
362         if (!to->to_retries)
363                 to->to_retries = 2;
364
365         switch (proto) {
366         case IPPROTO_TCP:
367                 if (!to->to_initval)
368                         to->to_initval = 60 * HZ;
369                 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
370                         to->to_initval = NFS_MAX_TCP_TIMEOUT;
371                 to->to_increment = to->to_initval;
372                 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
373                 to->to_exponential = 0;
374                 break;
375         case IPPROTO_UDP:
376         default:
377                 if (!to->to_initval)
378                         to->to_initval = 11 * HZ / 10;
379                 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
380                         to->to_initval = NFS_MAX_UDP_TIMEOUT;
381                 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
382                 to->to_exponential = 1;
383                 break;
384         }
385 }
386
387 /*
388  * Create an RPC client handle.
389  */
390 static struct rpc_clnt *
391 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
392 {
393         struct rpc_timeout      timeparms;
394         struct rpc_xprt         *xprt = NULL;
395         struct rpc_clnt         *clnt = NULL;
396         int                     proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
397
398         nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
399
400         server->retrans_timeo = timeparms.to_initval;
401         server->retrans_count = timeparms.to_retries;
402
403         /* create transport and client */
404         xprt = xprt_create_proto(proto, &server->addr, &timeparms);
405         if (IS_ERR(xprt)) {
406                 dprintk("%s: cannot create RPC transport. Error = %ld\n",
407                                 __FUNCTION__, PTR_ERR(xprt));
408                 return (struct rpc_clnt *)xprt;
409         }
410         clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
411                                  server->rpc_ops->version, data->pseudoflavor);
412         if (IS_ERR(clnt)) {
413                 dprintk("%s: cannot create RPC client. Error = %ld\n",
414                                 __FUNCTION__, PTR_ERR(xprt));
415                 goto out_fail;
416         }
417
418         clnt->cl_intr     = 1;
419         clnt->cl_softrtry = 1;
420
421         return clnt;
422
423 out_fail:
424         return clnt;
425 }
426
427 /*
428  * The way this works is that the mount process passes a structure
429  * in the data argument which contains the server's IP address
430  * and the root file handle obtained from the server's mount
431  * daemon. We stash these away in the private superblock fields.
432  */
433 static int
434 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
435 {
436         struct nfs_server       *server;
437         rpc_authflavor_t        authflavor;
438
439         server           = NFS_SB(sb);
440         sb->s_blocksize_bits = 0;
441         sb->s_blocksize = 0;
442         if (data->bsize)
443                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
444         if (data->rsize)
445                 server->rsize = nfs_block_size(data->rsize, NULL);
446         if (data->wsize)
447                 server->wsize = nfs_block_size(data->wsize, NULL);
448         server->flags    = data->flags & NFS_MOUNT_FLAGMASK;
449
450         server->acregmin = data->acregmin*HZ;
451         server->acregmax = data->acregmax*HZ;
452         server->acdirmin = data->acdirmin*HZ;
453         server->acdirmax = data->acdirmax*HZ;
454
455         /* Start lockd here, before we might error out */
456         if (!(server->flags & NFS_MOUNT_NONLM))
457                 lockd_up();
458
459         server->namelen  = data->namlen;
460         server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
461         if (!server->hostname)
462                 return -ENOMEM;
463         strcpy(server->hostname, data->hostname);
464
465         /* Check NFS protocol revision and initialize RPC op vector
466          * and file handle pool. */
467 #ifdef CONFIG_NFS_V3
468         if (server->flags & NFS_MOUNT_VER3) {
469                 server->rpc_ops = &nfs_v3_clientops;
470                 server->caps |= NFS_CAP_READDIRPLUS;
471         } else {
472                 server->rpc_ops = &nfs_v2_clientops;
473         }
474 #else
475         server->rpc_ops = &nfs_v2_clientops;
476 #endif
477
478         /* Fill in pseudoflavor for mount version < 5 */
479         if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
480                 data->pseudoflavor = RPC_AUTH_UNIX;
481         authflavor = data->pseudoflavor;        /* save for sb_init() */
482         /* XXX maybe we want to add a server->pseudoflavor field */
483
484         /* Create RPC client handles */
485         server->client = nfs_create_client(server, data);
486         if (IS_ERR(server->client))
487                 return PTR_ERR(server->client);
488         /* RFC 2623, sec 2.3.2 */
489         if (authflavor != RPC_AUTH_UNIX) {
490                 struct rpc_auth *auth;
491
492                 server->client_sys = rpc_clone_client(server->client);
493                 if (IS_ERR(server->client_sys))
494                         return PTR_ERR(server->client_sys);
495                 auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
496                 if (IS_ERR(auth))
497                         return PTR_ERR(auth);
498         } else {
499                 atomic_inc(&server->client->cl_count);
500                 server->client_sys = server->client;
501         }
502         if (server->flags & NFS_MOUNT_VER3) {
503 #ifdef CONFIG_NFS_V3_ACL
504                 if (!(server->flags & NFS_MOUNT_NOACL)) {
505                         server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
506                         /* No errors! Assume that Sun nfsacls are supported */
507                         if (!IS_ERR(server->client_acl))
508                                 server->caps |= NFS_CAP_ACLS;
509                 }
510 #else
511                 server->flags &= ~NFS_MOUNT_NOACL;
512 #endif /* CONFIG_NFS_V3_ACL */
513                 /*
514                  * The VFS shouldn't apply the umask to mode bits. We will
515                  * do so ourselves when necessary.
516                  */
517                 sb->s_flags |= MS_POSIXACL;
518                 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
519                         server->namelen = NFS3_MAXNAMLEN;
520                 sb->s_time_gran = 1;
521         } else {
522                 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
523                         server->namelen = NFS2_MAXNAMLEN;
524         }
525
526         sb->s_op = &nfs_sops;
527         return nfs_sb_init(sb, authflavor);
528 }
529
530 static int
531 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
532 {
533         struct nfs_server *server = NFS_SB(sb);
534         unsigned char blockbits;
535         unsigned long blockres;
536         struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
537         struct nfs_fattr fattr;
538         struct nfs_fsstat res = {
539                         .fattr = &fattr,
540         };
541         int error;
542
543         lock_kernel();
544
545         error = server->rpc_ops->statfs(server, rootfh, &res);
546         buf->f_type = NFS_SUPER_MAGIC;
547         if (error < 0)
548                 goto out_err;
549
550         /*
551          * Current versions of glibc do not correctly handle the
552          * case where f_frsize != f_bsize.  Eventually we want to
553          * report the value of wtmult in this field.
554          */
555         buf->f_frsize = sb->s_blocksize;
556
557         /*
558          * On most *nix systems, f_blocks, f_bfree, and f_bavail
559          * are reported in units of f_frsize.  Linux hasn't had
560          * an f_frsize field in its statfs struct until recently,
561          * thus historically Linux's sys_statfs reports these
562          * fields in units of f_bsize.
563          */
564         buf->f_bsize = sb->s_blocksize;
565         blockbits = sb->s_blocksize_bits;
566         blockres = (1 << blockbits) - 1;
567         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
568         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
569         buf->f_bavail = (res.abytes + blockres) >> blockbits;
570
571         buf->f_files = res.tfiles;
572         buf->f_ffree = res.afiles;
573
574         buf->f_namelen = server->namelen;
575  out:
576         unlock_kernel();
577         return 0;
578
579  out_err:
580         dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
581         buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
582         goto out;
583
584 }
585
586 static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
587 {
588         static struct proc_nfs_info {
589                 int flag;
590                 char *str;
591                 char *nostr;
592         } nfs_info[] = {
593                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
594                 { NFS_MOUNT_INTR, ",intr", "" },
595                 { NFS_MOUNT_NOCTO, ",nocto", "" },
596                 { NFS_MOUNT_NOAC, ",noac", "" },
597                 { NFS_MOUNT_NONLM, ",nolock", "" },
598                 { NFS_MOUNT_NOACL, ",noacl", "" },
599                 { 0, NULL, NULL }
600         };
601         struct proc_nfs_info *nfs_infop;
602         char buf[12];
603         char *proto;
604
605         seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
606         seq_printf(m, ",rsize=%d", nfss->rsize);
607         seq_printf(m, ",wsize=%d", nfss->wsize);
608         if (nfss->acregmin != 3*HZ || showdefaults)
609                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
610         if (nfss->acregmax != 60*HZ || showdefaults)
611                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
612         if (nfss->acdirmin != 30*HZ || showdefaults)
613                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
614         if (nfss->acdirmax != 60*HZ || showdefaults)
615                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
616         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
617                 if (nfss->flags & nfs_infop->flag)
618                         seq_puts(m, nfs_infop->str);
619                 else
620                         seq_puts(m, nfs_infop->nostr);
621         }
622         switch (nfss->client->cl_xprt->prot) {
623                 case IPPROTO_TCP:
624                         proto = "tcp";
625                         break;
626                 case IPPROTO_UDP:
627                         proto = "udp";
628                         break;
629                 default:
630                         snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
631                         proto = buf;
632         }
633         seq_printf(m, ",proto=%s", proto);
634         seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
635         seq_printf(m, ",retrans=%u", nfss->retrans_count);
636 }
637
638 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
639 {
640         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
641
642         nfs_show_mount_options(m, nfss, 0);
643
644         seq_puts(m, ",addr=");
645         seq_escape(m, nfss->hostname, " \t\n\\");
646
647         return 0;
648 }
649
650 static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
651 {
652         int i, cpu;
653         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
654         struct rpc_auth *auth = nfss->client->cl_auth;
655         struct nfs_iostats totals = { };
656
657         seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
658
659         /*
660          * Display all mount option settings
661          */
662         seq_printf(m, "\n\topts:\t");
663         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
664         seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
665         seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
666         seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
667         nfs_show_mount_options(m, nfss, 1);
668
669         seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
670
671         seq_printf(m, "\n\tcaps:\t");
672         seq_printf(m, "caps=0x%x", nfss->caps);
673         seq_printf(m, ",wtmult=%d", nfss->wtmult);
674         seq_printf(m, ",dtsize=%d", nfss->dtsize);
675         seq_printf(m, ",bsize=%d", nfss->bsize);
676         seq_printf(m, ",namelen=%d", nfss->namelen);
677
678 #ifdef CONFIG_NFS_V4
679         if (nfss->rpc_ops->version == 4) {
680                 seq_printf(m, "\n\tnfsv4:\t");
681                 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
682                 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
683                 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
684         }
685 #endif
686
687         /*
688          * Display security flavor in effect for this mount
689          */
690         seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
691         if (auth->au_flavor)
692                 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
693
694         /*
695          * Display superblock I/O counters
696          */
697         for_each_possible_cpu(cpu) {
698                 struct nfs_iostats *stats;
699
700                 preempt_disable();
701                 stats = per_cpu_ptr(nfss->io_stats, cpu);
702
703                 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
704                         totals.events[i] += stats->events[i];
705                 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
706                         totals.bytes[i] += stats->bytes[i];
707
708                 preempt_enable();
709         }
710
711         seq_printf(m, "\n\tevents:\t");
712         for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
713                 seq_printf(m, "%lu ", totals.events[i]);
714         seq_printf(m, "\n\tbytes:\t");
715         for (i = 0; i < __NFSIOS_BYTESMAX; i++)
716                 seq_printf(m, "%Lu ", totals.bytes[i]);
717         seq_printf(m, "\n");
718
719         rpc_print_iostats(m, nfss->client);
720
721         return 0;
722 }
723
724 /**
725  * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
726  */
727 int nfs_sync_mapping(struct address_space *mapping)
728 {
729         int ret;
730
731         if (mapping->nrpages == 0)
732                 return 0;
733         unmap_mapping_range(mapping, 0, 0, 0);
734         ret = filemap_write_and_wait(mapping);
735         if (ret != 0)
736                 goto out;
737         ret = nfs_wb_all(mapping->host);
738 out:
739         return ret;
740 }
741
742 /*
743  * Invalidate the local caches
744  */
745 static void nfs_zap_caches_locked(struct inode *inode)
746 {
747         struct nfs_inode *nfsi = NFS_I(inode);
748         int mode = inode->i_mode;
749
750         nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
751
752         NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
753         NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
754
755         memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
756         if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
757                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
758         else
759                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
760 }
761
762 void nfs_zap_caches(struct inode *inode)
763 {
764         spin_lock(&inode->i_lock);
765         nfs_zap_caches_locked(inode);
766         spin_unlock(&inode->i_lock);
767 }
768
769 static void nfs_zap_acl_cache(struct inode *inode)
770 {
771         void (*clear_acl_cache)(struct inode *);
772
773         clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
774         if (clear_acl_cache != NULL)
775                 clear_acl_cache(inode);
776         spin_lock(&inode->i_lock);
777         NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
778         spin_unlock(&inode->i_lock);
779 }
780
781 /*
782  * Invalidate, but do not unhash, the inode.
783  * NB: must be called with inode->i_lock held!
784  */
785 static void nfs_invalidate_inode(struct inode *inode)
786 {
787         set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
788         nfs_zap_caches_locked(inode);
789 }
790
791 struct nfs_find_desc {
792         struct nfs_fh           *fh;
793         struct nfs_fattr        *fattr;
794 };
795
796 /*
797  * In NFSv3 we can have 64bit inode numbers. In order to support
798  * this, and re-exported directories (also seen in NFSv2)
799  * we are forced to allow 2 different inodes to have the same
800  * i_ino.
801  */
802 static int
803 nfs_find_actor(struct inode *inode, void *opaque)
804 {
805         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
806         struct nfs_fh           *fh = desc->fh;
807         struct nfs_fattr        *fattr = desc->fattr;
808
809         if (NFS_FILEID(inode) != fattr->fileid)
810                 return 0;
811         if (nfs_compare_fh(NFS_FH(inode), fh))
812                 return 0;
813         if (is_bad_inode(inode) || NFS_STALE(inode))
814                 return 0;
815         return 1;
816 }
817
818 static int
819 nfs_init_locked(struct inode *inode, void *opaque)
820 {
821         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
822         struct nfs_fattr        *fattr = desc->fattr;
823
824         NFS_FILEID(inode) = fattr->fileid;
825         nfs_copy_fh(NFS_FH(inode), desc->fh);
826         return 0;
827 }
828
829 /* Don't use READDIRPLUS on directories that we believe are too large */
830 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
831
832 /*
833  * This is our front-end to iget that looks up inodes by file handle
834  * instead of inode number.
835  */
836 struct inode *
837 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
838 {
839         struct nfs_find_desc desc = {
840                 .fh     = fh,
841                 .fattr  = fattr
842         };
843         struct inode *inode = ERR_PTR(-ENOENT);
844         unsigned long hash;
845
846         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
847                 goto out_no_inode;
848
849         if (!fattr->nlink) {
850                 printk("NFS: Buggy server - nlink == 0!\n");
851                 goto out_no_inode;
852         }
853
854         hash = nfs_fattr_to_ino_t(fattr);
855
856         inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
857         if (inode == NULL) {
858                 inode = ERR_PTR(-ENOMEM);
859                 goto out_no_inode;
860         }
861
862         if (inode->i_state & I_NEW) {
863                 struct nfs_inode *nfsi = NFS_I(inode);
864
865                 /* We set i_ino for the few things that still rely on it,
866                  * such as stat(2) */
867                 inode->i_ino = hash;
868
869                 /* We can't support update_atime(), since the server will reset it */
870                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
871                 inode->i_mode = fattr->mode;
872                 /* Why so? Because we want revalidate for devices/FIFOs, and
873                  * that's precisely what we have in nfs_file_inode_operations.
874                  */
875                 inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
876                 if (S_ISREG(inode->i_mode)) {
877                         inode->i_fop = &nfs_file_operations;
878                         inode->i_data.a_ops = &nfs_file_aops;
879                         inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
880                 } else if (S_ISDIR(inode->i_mode)) {
881                         inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
882                         inode->i_fop = &nfs_dir_operations;
883                         if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
884                             && fattr->size <= NFS_LIMIT_READDIRPLUS)
885                                 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
886                         /* Deal with crossing mountpoints */
887                         if (!nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
888                                 inode->i_op = &nfs_mountpoint_inode_operations;
889                                 inode->i_fop = NULL;
890                         }
891                 } else if (S_ISLNK(inode->i_mode))
892                         inode->i_op = &nfs_symlink_inode_operations;
893                 else
894                         init_special_inode(inode, inode->i_mode, fattr->rdev);
895
896                 nfsi->read_cache_jiffies = fattr->time_start;
897                 nfsi->last_updated = jiffies;
898                 inode->i_atime = fattr->atime;
899                 inode->i_mtime = fattr->mtime;
900                 inode->i_ctime = fattr->ctime;
901                 if (fattr->valid & NFS_ATTR_FATTR_V4)
902                         nfsi->change_attr = fattr->change_attr;
903                 inode->i_size = nfs_size_to_loff_t(fattr->size);
904                 inode->i_nlink = fattr->nlink;
905                 inode->i_uid = fattr->uid;
906                 inode->i_gid = fattr->gid;
907                 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
908                         /*
909                          * report the blocks in 512byte units
910                          */
911                         inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
912                         inode->i_blksize = inode->i_sb->s_blocksize;
913                 } else {
914                         inode->i_blocks = fattr->du.nfs2.blocks;
915                         inode->i_blksize = fattr->du.nfs2.blocksize;
916                 }
917                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
918                 nfsi->attrtimeo_timestamp = jiffies;
919                 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
920                 nfsi->cache_access.cred = NULL;
921
922                 unlock_new_inode(inode);
923         } else
924                 nfs_refresh_inode(inode, fattr);
925         dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
926                 inode->i_sb->s_id,
927                 (long long)NFS_FILEID(inode),
928                 atomic_read(&inode->i_count));
929
930 out:
931         return inode;
932
933 out_no_inode:
934         dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
935         goto out;
936 }
937
938 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
939
940 int
941 nfs_setattr(struct dentry *dentry, struct iattr *attr)
942 {
943         struct inode *inode = dentry->d_inode;
944         struct nfs_fattr fattr;
945         int error;
946
947         nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
948
949         if (attr->ia_valid & ATTR_SIZE) {
950                 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
951                         attr->ia_valid &= ~ATTR_SIZE;
952         }
953
954         /* Optimization: if the end result is no change, don't RPC */
955         attr->ia_valid &= NFS_VALID_ATTRS;
956         if (attr->ia_valid == 0)
957                 return 0;
958
959         lock_kernel();
960         nfs_begin_data_update(inode);
961         /* Write all dirty data */
962         filemap_write_and_wait(inode->i_mapping);
963         nfs_wb_all(inode);
964         /*
965          * Return any delegations if we're going to change ACLs
966          */
967         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
968                 nfs_inode_return_delegation(inode);
969         error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
970         if (error == 0)
971                 nfs_refresh_inode(inode, &fattr);
972         nfs_end_data_update(inode);
973         unlock_kernel();
974         return error;
975 }
976
977 /**
978  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
979  * @inode: pointer to struct inode
980  * @attr: pointer to struct iattr
981  *
982  * Note: we do this in the *proc.c in order to ensure that
983  *       it works for things like exclusive creates too.
984  */
985 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
986 {
987         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
988                 if ((attr->ia_valid & ATTR_MODE) != 0) {
989                         int mode = attr->ia_mode & S_IALLUGO;
990                         mode |= inode->i_mode & ~S_IALLUGO;
991                         inode->i_mode = mode;
992                 }
993                 if ((attr->ia_valid & ATTR_UID) != 0)
994                         inode->i_uid = attr->ia_uid;
995                 if ((attr->ia_valid & ATTR_GID) != 0)
996                         inode->i_gid = attr->ia_gid;
997                 spin_lock(&inode->i_lock);
998                 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
999                 spin_unlock(&inode->i_lock);
1000         }
1001         if ((attr->ia_valid & ATTR_SIZE) != 0) {
1002                 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
1003                 inode->i_size = attr->ia_size;
1004                 vmtruncate(inode, attr->ia_size);
1005         }
1006 }
1007
1008 static int nfs_wait_schedule(void *word)
1009 {
1010         if (signal_pending(current))
1011                 return -ERESTARTSYS;
1012         schedule();
1013         return 0;
1014 }
1015
1016 /*
1017  * Wait for the inode to get unlocked.
1018  */
1019 static int nfs_wait_on_inode(struct inode *inode)
1020 {
1021         struct rpc_clnt *clnt = NFS_CLIENT(inode);
1022         struct nfs_inode *nfsi = NFS_I(inode);
1023         sigset_t oldmask;
1024         int error;
1025
1026         rpc_clnt_sigmask(clnt, &oldmask);
1027         error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
1028                                         nfs_wait_schedule, TASK_INTERRUPTIBLE);
1029         rpc_clnt_sigunmask(clnt, &oldmask);
1030
1031         return error;
1032 }
1033
1034 static void nfs_wake_up_inode(struct inode *inode)
1035 {
1036         struct nfs_inode *nfsi = NFS_I(inode);
1037
1038         clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
1039         smp_mb__after_clear_bit();
1040         wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
1041 }
1042
1043 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1044 {
1045         struct inode *inode = dentry->d_inode;
1046         int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
1047         int err;
1048
1049         /* Flush out writes to the server in order to update c/mtime */
1050         nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
1051
1052         /*
1053          * We may force a getattr if the user cares about atime.
1054          *
1055          * Note that we only have to check the vfsmount flags here:
1056          *  - NFS always sets S_NOATIME by so checking it would give a
1057          *    bogus result
1058          *  - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
1059          *    no point in checking those.
1060          */
1061         if ((mnt->mnt_flags & MNT_NOATIME) ||
1062             ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
1063                 need_atime = 0;
1064
1065         if (need_atime)
1066                 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1067         else
1068                 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1069         if (!err)
1070                 generic_fillattr(inode, stat);
1071         return err;
1072 }
1073
1074 static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
1075 {
1076         struct nfs_open_context *ctx;
1077
1078         ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
1079         if (ctx != NULL) {
1080                 atomic_set(&ctx->count, 1);
1081                 ctx->dentry = dget(dentry);
1082                 ctx->vfsmnt = mntget(mnt);
1083                 ctx->cred = get_rpccred(cred);
1084                 ctx->state = NULL;
1085                 ctx->lockowner = current->files;
1086                 ctx->error = 0;
1087                 ctx->dir_cookie = 0;
1088         }
1089         return ctx;
1090 }
1091
1092 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1093 {
1094         if (ctx != NULL)
1095                 atomic_inc(&ctx->count);
1096         return ctx;
1097 }
1098
1099 void put_nfs_open_context(struct nfs_open_context *ctx)
1100 {
1101         if (atomic_dec_and_test(&ctx->count)) {
1102                 if (!list_empty(&ctx->list)) {
1103                         struct inode *inode = ctx->dentry->d_inode;
1104                         spin_lock(&inode->i_lock);
1105                         list_del(&ctx->list);
1106                         spin_unlock(&inode->i_lock);
1107                 }
1108                 if (ctx->state != NULL)
1109                         nfs4_close_state(ctx->state, ctx->mode);
1110                 if (ctx->cred != NULL)
1111                         put_rpccred(ctx->cred);
1112                 dput(ctx->dentry);
1113                 mntput(ctx->vfsmnt);
1114                 kfree(ctx);
1115         }
1116 }
1117
1118 /*
1119  * Ensure that mmap has a recent RPC credential for use when writing out
1120  * shared pages
1121  */
1122 static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
1123 {
1124         struct inode *inode = filp->f_dentry->d_inode;
1125         struct nfs_inode *nfsi = NFS_I(inode);
1126
1127         filp->private_data = get_nfs_open_context(ctx);
1128         spin_lock(&inode->i_lock);
1129         list_add(&ctx->list, &nfsi->open_files);
1130         spin_unlock(&inode->i_lock);
1131 }
1132
1133 /*
1134  * Given an inode, search for an open context with the desired characteristics
1135  */
1136 struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
1137 {
1138         struct nfs_inode *nfsi = NFS_I(inode);
1139         struct nfs_open_context *pos, *ctx = NULL;
1140
1141         spin_lock(&inode->i_lock);
1142         list_for_each_entry(pos, &nfsi->open_files, list) {
1143                 if (cred != NULL && pos->cred != cred)
1144                         continue;
1145                 if ((pos->mode & mode) == mode) {
1146                         ctx = get_nfs_open_context(pos);
1147                         break;
1148                 }
1149         }
1150         spin_unlock(&inode->i_lock);
1151         return ctx;
1152 }
1153
1154 static void nfs_file_clear_open_context(struct file *filp)
1155 {
1156         struct inode *inode = filp->f_dentry->d_inode;
1157         struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
1158
1159         if (ctx) {
1160                 filp->private_data = NULL;
1161                 spin_lock(&inode->i_lock);
1162                 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
1163                 spin_unlock(&inode->i_lock);
1164                 put_nfs_open_context(ctx);
1165         }
1166 }
1167
1168 /*
1169  * These allocate and release file read/write context information.
1170  */
1171 int nfs_open(struct inode *inode, struct file *filp)
1172 {
1173         struct nfs_open_context *ctx;
1174         struct rpc_cred *cred;
1175
1176         cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1177         if (IS_ERR(cred))
1178                 return PTR_ERR(cred);
1179         ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
1180         put_rpccred(cred);
1181         if (ctx == NULL)
1182                 return -ENOMEM;
1183         ctx->mode = filp->f_mode;
1184         nfs_file_set_open_context(filp, ctx);
1185         put_nfs_open_context(ctx);
1186         return 0;
1187 }
1188
1189 int nfs_release(struct inode *inode, struct file *filp)
1190 {
1191         nfs_file_clear_open_context(filp);
1192         return 0;
1193 }
1194
1195 /*
1196  * This function is called whenever some part of NFS notices that
1197  * the cached attributes have to be refreshed.
1198  */
1199 int
1200 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1201 {
1202         int              status = -ESTALE;
1203         struct nfs_fattr fattr;
1204         struct nfs_inode *nfsi = NFS_I(inode);
1205
1206         dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
1207                 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
1208
1209         nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
1210         lock_kernel();
1211         if (!inode || is_bad_inode(inode))
1212                 goto out_nowait;
1213         if (NFS_STALE(inode))
1214                 goto out_nowait;
1215
1216         status = nfs_wait_on_inode(inode);
1217         if (status < 0)
1218                 goto out;
1219         if (NFS_STALE(inode)) {
1220                 status = -ESTALE;
1221                 /* Do we trust the cached ESTALE? */
1222                 if (NFS_ATTRTIMEO(inode) != 0) {
1223                         if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) {
1224                                 /* no */
1225                         } else
1226                                 goto out;
1227                 }
1228         }
1229
1230         status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
1231         if (status != 0) {
1232                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
1233                          inode->i_sb->s_id,
1234                          (long long)NFS_FILEID(inode), status);
1235                 if (status == -ESTALE) {
1236                         nfs_zap_caches(inode);
1237                         if (!S_ISDIR(inode->i_mode))
1238                                 set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
1239                 }
1240                 goto out;
1241         }
1242
1243         spin_lock(&inode->i_lock);
1244         status = nfs_update_inode(inode, &fattr);
1245         if (status) {
1246                 spin_unlock(&inode->i_lock);
1247                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
1248                          inode->i_sb->s_id,
1249                          (long long)NFS_FILEID(inode), status);
1250                 goto out;
1251         }
1252         spin_unlock(&inode->i_lock);
1253
1254         if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
1255                 nfs_zap_acl_cache(inode);
1256
1257         dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1258                 inode->i_sb->s_id,
1259                 (long long)NFS_FILEID(inode));
1260
1261  out:
1262         nfs_wake_up_inode(inode);
1263
1264  out_nowait:
1265         unlock_kernel();
1266         return status;
1267 }
1268
1269 int nfs_attribute_timeout(struct inode *inode)
1270 {
1271         struct nfs_inode *nfsi = NFS_I(inode);
1272
1273         if (nfs_have_delegation(inode, FMODE_READ))
1274                 return 0;
1275         return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
1276 }
1277
1278 /**
1279  * nfs_revalidate_inode - Revalidate the inode attributes
1280  * @server - pointer to nfs_server struct
1281  * @inode - pointer to inode struct
1282  *
1283  * Updates inode attribute information by retrieving the data from the server.
1284  */
1285 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1286 {
1287         if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
1288                         && !nfs_attribute_timeout(inode))
1289                 return NFS_STALE(inode) ? -ESTALE : 0;
1290         return __nfs_revalidate_inode(server, inode);
1291 }
1292
1293 /**
1294  * nfs_revalidate_mapping - Revalidate the pagecache
1295  * @inode - pointer to host inode
1296  * @mapping - pointer to mapping
1297  */
1298 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
1299 {
1300         struct nfs_inode *nfsi = NFS_I(inode);
1301         int ret = 0;
1302
1303         if (NFS_STALE(inode))
1304                 ret = -ESTALE;
1305         if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1306                         || nfs_attribute_timeout(inode))
1307                 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1308
1309         if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
1310                 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
1311                 if (S_ISREG(inode->i_mode))
1312                         nfs_sync_mapping(mapping);
1313                 invalidate_inode_pages2(mapping);
1314
1315                 spin_lock(&inode->i_lock);
1316                 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
1317                 if (S_ISDIR(inode->i_mode)) {
1318                         memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
1319                         /* This ensures we revalidate child dentries */
1320                         nfsi->cache_change_attribute = jiffies;
1321                 }
1322                 spin_unlock(&inode->i_lock);
1323
1324                 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1325                                 inode->i_sb->s_id,
1326                                 (long long)NFS_FILEID(inode));
1327         }
1328         return ret;
1329 }
1330
1331 /**
1332  * nfs_begin_data_update
1333  * @inode - pointer to inode
1334  * Declare that a set of operations will update file data on the server
1335  */
1336 void nfs_begin_data_update(struct inode *inode)
1337 {
1338         atomic_inc(&NFS_I(inode)->data_updates);
1339 }
1340
1341 /**
1342  * nfs_end_data_update
1343  * @inode - pointer to inode
1344  * Declare end of the operations that will update file data
1345  * This will mark the inode as immediately needing revalidation
1346  * of its attribute cache.
1347  */
1348 void nfs_end_data_update(struct inode *inode)
1349 {
1350         struct nfs_inode *nfsi = NFS_I(inode);
1351
1352         if (!nfs_have_delegation(inode, FMODE_READ)) {
1353                 /* Directories and symlinks: invalidate page cache */
1354                 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
1355                         spin_lock(&inode->i_lock);
1356                         nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1357                         spin_unlock(&inode->i_lock);
1358                 }
1359         }
1360         nfsi->cache_change_attribute = jiffies;
1361         atomic_dec(&nfsi->data_updates);
1362 }
1363
1364 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1365 {
1366         struct nfs_inode *nfsi = NFS_I(inode);
1367
1368         /* If we have atomic WCC data, we may update some attributes */
1369         if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1370                 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
1371                         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1372                         nfsi->cache_change_attribute = jiffies;
1373                 }
1374                 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
1375                         memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1376                         nfsi->cache_change_attribute = jiffies;
1377                 }
1378                 if (inode->i_size == fattr->pre_size && nfsi->npages == 0) {
1379                         inode->i_size = fattr->size;
1380                         nfsi->cache_change_attribute = jiffies;
1381                 }
1382         }
1383 }
1384
1385 /**
1386  * nfs_check_inode_attributes - verify consistency of the inode attribute cache
1387  * @inode - pointer to inode
1388  * @fattr - updated attributes
1389  *
1390  * Verifies the attribute cache. If we have just changed the attributes,
1391  * so that fattr carries weak cache consistency data, then it may
1392  * also update the ctime/mtime/change_attribute.
1393  */
1394 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
1395 {
1396         struct nfs_inode *nfsi = NFS_I(inode);
1397         loff_t cur_size, new_isize;
1398         int data_unstable;
1399
1400
1401         /* Has the inode gone and changed behind our back? */
1402         if (nfsi->fileid != fattr->fileid
1403                         || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
1404                 return -EIO;
1405         }
1406
1407         /* Are we in the process of updating data on the server? */
1408         data_unstable = nfs_caches_unstable(inode);
1409
1410         /* Do atomic weak cache consistency updates */
1411         nfs_wcc_update_inode(inode, fattr);
1412
1413         if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
1414                         nfsi->change_attr != fattr->change_attr)
1415                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1416
1417         /* Verify a few of the more important attributes */
1418         if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
1419                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1420
1421         cur_size = i_size_read(inode);
1422         new_isize = nfs_size_to_loff_t(fattr->size);
1423         if (cur_size != new_isize && nfsi->npages == 0)
1424                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1425
1426         /* Have any file permissions changed? */
1427         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1428                         || inode->i_uid != fattr->uid
1429                         || inode->i_gid != fattr->gid)
1430                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
1431
1432         /* Has the link count changed? */
1433         if (inode->i_nlink != fattr->nlink)
1434                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
1435
1436         if (!timespec_equal(&inode->i_atime, &fattr->atime))
1437                 nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
1438
1439         nfsi->read_cache_jiffies = fattr->time_start;
1440         return 0;
1441 }
1442
1443 /**
1444  * nfs_refresh_inode - try to update the inode attribute cache
1445  * @inode - pointer to inode
1446  * @fattr - updated attributes
1447  *
1448  * Check that an RPC call that returned attributes has not overlapped with
1449  * other recent updates of the inode metadata, then decide whether it is
1450  * safe to do a full update of the inode attributes, or whether just to
1451  * call nfs_check_inode_attributes.
1452  */
1453 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1454 {
1455         struct nfs_inode *nfsi = NFS_I(inode);
1456         int status;
1457
1458         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1459                 return 0;
1460         spin_lock(&inode->i_lock);
1461         if (time_after(fattr->time_start, nfsi->last_updated))
1462                 status = nfs_update_inode(inode, fattr);
1463         else
1464                 status = nfs_check_inode_attributes(inode, fattr);
1465
1466         spin_unlock(&inode->i_lock);
1467         return status;
1468 }
1469
1470 /**
1471  * nfs_post_op_update_inode - try to update the inode attribute cache
1472  * @inode - pointer to inode
1473  * @fattr - updated attributes
1474  *
1475  * After an operation that has changed the inode metadata, mark the
1476  * attribute cache as being invalid, then try to update it.
1477  */
1478 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1479 {
1480         struct nfs_inode *nfsi = NFS_I(inode);
1481         int status = 0;
1482
1483         spin_lock(&inode->i_lock);
1484         if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
1485                 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1486                 goto out;
1487         }
1488         status = nfs_update_inode(inode, fattr);
1489 out:
1490         spin_unlock(&inode->i_lock);
1491         return status;
1492 }
1493
1494 /*
1495  * Many nfs protocol calls return the new file attributes after
1496  * an operation.  Here we update the inode to reflect the state
1497  * of the server's inode.
1498  *
1499  * This is a bit tricky because we have to make sure all dirty pages
1500  * have been sent off to the server before calling invalidate_inode_pages.
1501  * To make sure no other process adds more write requests while we try
1502  * our best to flush them, we make them sleep during the attribute refresh.
1503  *
1504  * A very similar scenario holds for the dir cache.
1505  */
1506 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1507 {
1508         struct nfs_server *server;
1509         struct nfs_inode *nfsi = NFS_I(inode);
1510         loff_t cur_isize, new_isize;
1511         unsigned int    invalid = 0;
1512         int data_stable;
1513
1514         dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1515                         __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1516                         atomic_read(&inode->i_count), fattr->valid);
1517
1518         if (nfsi->fileid != fattr->fileid)
1519                 goto out_fileid;
1520
1521         /*
1522          * Make sure the inode's type hasn't changed.
1523          */
1524         if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1525                 goto out_changed;
1526
1527         server = NFS_SERVER(inode);
1528         /* Update the fsid if and only if this is the root directory */
1529         if (inode == inode->i_sb->s_root->d_inode
1530                         && !nfs_fsid_equal(&server->fsid, &fattr->fsid))
1531                 server->fsid = fattr->fsid;
1532
1533         /*
1534          * Update the read time so we don't revalidate too often.
1535          */
1536         nfsi->read_cache_jiffies = fattr->time_start;
1537         nfsi->last_updated = jiffies;
1538
1539         /* Are we racing with known updates of the metadata on the server? */
1540         data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
1541         if (data_stable)
1542                 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
1543
1544         /* Do atomic weak cache consistency updates */
1545         nfs_wcc_update_inode(inode, fattr);
1546
1547         /* Check if our cached file size is stale */
1548         new_isize = nfs_size_to_loff_t(fattr->size);
1549         cur_isize = i_size_read(inode);
1550         if (new_isize != cur_isize) {
1551                 /* Do we perhaps have any outstanding writes? */
1552                 if (nfsi->npages == 0) {
1553                         /* No, but did we race with nfs_end_data_update()? */
1554                         if (data_stable) {
1555                                 inode->i_size = new_isize;
1556                                 invalid |= NFS_INO_INVALID_DATA;
1557                         }
1558                         invalid |= NFS_INO_INVALID_ATTR;
1559                 } else if (new_isize > cur_isize) {
1560                         inode->i_size = new_isize;
1561                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1562                 }
1563                 nfsi->cache_change_attribute = jiffies;
1564                 dprintk("NFS: isize change on server for file %s/%ld\n",
1565                                 inode->i_sb->s_id, inode->i_ino);
1566         }
1567
1568         /* Check if the mtime agrees */
1569         if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1570                 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1571                 dprintk("NFS: mtime change on server for file %s/%ld\n",
1572                                 inode->i_sb->s_id, inode->i_ino);
1573                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1574                 nfsi->cache_change_attribute = jiffies;
1575         }
1576
1577         /* If ctime has changed we should definitely clear access+acl caches */
1578         if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
1579                 invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1580                 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1581                 nfsi->cache_change_attribute = jiffies;
1582         }
1583         memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1584
1585         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1586             inode->i_uid != fattr->uid ||
1587             inode->i_gid != fattr->gid)
1588                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1589
1590         inode->i_mode = fattr->mode;
1591         inode->i_nlink = fattr->nlink;
1592         inode->i_uid = fattr->uid;
1593         inode->i_gid = fattr->gid;
1594
1595         if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1596                 /*
1597                  * report the blocks in 512byte units
1598                  */
1599                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1600                 inode->i_blksize = inode->i_sb->s_blocksize;
1601         } else {
1602                 inode->i_blocks = fattr->du.nfs2.blocks;
1603                 inode->i_blksize = fattr->du.nfs2.blocksize;
1604         }
1605
1606         if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
1607                         nfsi->change_attr != fattr->change_attr) {
1608                 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1609                                 inode->i_sb->s_id, inode->i_ino);
1610                 nfsi->change_attr = fattr->change_attr;
1611                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1612                 nfsi->cache_change_attribute = jiffies;
1613         }
1614
1615         /* Update attrtimeo value if we're out of the unstable period */
1616         if (invalid & NFS_INO_INVALID_ATTR) {
1617                 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1618                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1619                 nfsi->attrtimeo_timestamp = jiffies;
1620         } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1621                 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1622                         nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1623                 nfsi->attrtimeo_timestamp = jiffies;
1624         }
1625         /* Don't invalidate the data if we were to blame */
1626         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1627                                 || S_ISLNK(inode->i_mode)))
1628                 invalid &= ~NFS_INO_INVALID_DATA;
1629         if (data_stable)
1630                 invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
1631         if (!nfs_have_delegation(inode, FMODE_READ))
1632                 nfsi->cache_validity |= invalid;
1633
1634         return 0;
1635  out_changed:
1636         /*
1637          * Big trouble! The inode has become a different object.
1638          */
1639 #ifdef NFS_PARANOIA
1640         printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1641                         __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1642 #endif
1643  out_err:
1644         /*
1645          * No need to worry about unhashing the dentry, as the
1646          * lookup validation will know that the inode is bad.
1647          * (But we fall through to invalidate the caches.)
1648          */
1649         nfs_invalidate_inode(inode);
1650         return -ESTALE;
1651
1652  out_fileid:
1653         printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1654                 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1655                 NFS_SERVER(inode)->hostname, inode->i_sb->s_id,
1656                 (long long)nfsi->fileid, (long long)fattr->fileid);
1657         goto out_err;
1658 }
1659
1660 /*
1661  * File system information
1662  */
1663
1664 /*
1665  * nfs_path - reconstruct the path given an arbitrary dentry
1666  * @base - arbitrary string to prepend to the path
1667  * @dentry - pointer to dentry
1668  * @buffer - result buffer
1669  * @buflen - length of buffer
1670  *
1671  * Helper function for constructing the path from the
1672  * root dentry to an arbitrary hashed dentry.
1673  *
1674  * This is mainly for use in figuring out the path on the
1675  * server side when automounting on top of an existing partition.
1676  */
1677 static char *nfs_path(const char *base, const struct dentry *dentry,
1678                       char *buffer, ssize_t buflen)
1679 {
1680         char *end = buffer+buflen;
1681         int namelen;
1682
1683         *--end = '\0';
1684         buflen--;
1685         spin_lock(&dcache_lock);
1686         while (!IS_ROOT(dentry)) {
1687                 namelen = dentry->d_name.len;
1688                 buflen -= namelen + 1;
1689                 if (buflen < 0)
1690                         goto Elong;
1691                 end -= namelen;
1692                 memcpy(end, dentry->d_name.name, namelen);
1693                 *--end = '/';
1694                 dentry = dentry->d_parent;
1695         }
1696         spin_unlock(&dcache_lock);
1697         namelen = strlen(base);
1698         /* Strip off excess slashes in base string */
1699         while (namelen > 0 && base[namelen - 1] == '/')
1700                 namelen--;
1701         buflen -= namelen;
1702         if (buflen < 0)
1703                 goto Elong;
1704         end -= namelen;
1705         memcpy(end, base, namelen);
1706         return end;
1707 Elong:
1708         return ERR_PTR(-ENAMETOOLONG);
1709 }
1710
1711 struct nfs_clone_mount {
1712         const struct super_block *sb;
1713         const struct dentry *dentry;
1714         struct nfs_fh *fh;
1715         struct nfs_fattr *fattr;
1716 };
1717
1718 static struct super_block *nfs_clone_generic_sb(struct nfs_clone_mount *data,
1719                 struct super_block *(*clone_client)(struct nfs_server *, struct nfs_clone_mount *))
1720 {
1721         struct nfs_server *server;
1722         struct nfs_server *parent = NFS_SB(data->sb);
1723         struct super_block *sb = ERR_PTR(-EINVAL);
1724         void *err = ERR_PTR(-ENOMEM);
1725         struct inode *root_inode;
1726         struct nfs_fsinfo fsinfo;
1727         int len;
1728
1729         server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1730         if (server == NULL)
1731                 goto out_err;
1732         memcpy(server, parent, sizeof(*server));
1733         len = strlen(parent->hostname) + 1;
1734         server->hostname = kmalloc(len, GFP_KERNEL);
1735         if (server->hostname == NULL)
1736                 goto free_server;
1737         memcpy(server->hostname, parent->hostname, len);
1738         server->fsid = data->fattr->fsid;
1739         nfs_copy_fh(&server->fh, data->fh);
1740         if (rpciod_up() != 0)
1741                 goto free_hostname;
1742
1743         sb = clone_client(server, data);
1744         if (IS_ERR((err = sb)) || sb->s_root)
1745                 goto kill_rpciod;
1746
1747         sb->s_op = data->sb->s_op;
1748         sb->s_blocksize = data->sb->s_blocksize;
1749         sb->s_blocksize_bits = data->sb->s_blocksize_bits;
1750         sb->s_maxbytes = data->sb->s_maxbytes;
1751
1752         server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1753         err = ERR_PTR(-ENOMEM);
1754         server->io_stats = nfs_alloc_iostats();
1755         if (server->io_stats == NULL)
1756                 goto out_deactivate;
1757
1758         server->client = rpc_clone_client(parent->client);
1759         if (IS_ERR((err = server->client)))
1760                 goto out_deactivate;
1761         if (!IS_ERR(parent->client_sys)) {
1762                 server->client_sys = rpc_clone_client(parent->client_sys);
1763                 if (IS_ERR((err = server->client_sys)))
1764                         goto out_deactivate;
1765         }
1766         if (!IS_ERR(parent->client_acl)) {
1767                 server->client_acl = rpc_clone_client(parent->client_acl);
1768                 if (IS_ERR((err = server->client_acl)))
1769                         goto out_deactivate;
1770         }
1771         root_inode = nfs_fhget(sb, data->fh, data->fattr);
1772         if (!root_inode)
1773                 goto out_deactivate;
1774         sb->s_root = d_alloc_root(root_inode);
1775         if (!sb->s_root)
1776                 goto out_put_root;
1777         fsinfo.fattr = data->fattr;
1778         if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
1779                 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
1780         sb->s_root->d_op = server->rpc_ops->dentry_ops;
1781         sb->s_flags |= MS_ACTIVE;
1782         return sb;
1783 out_put_root:
1784         iput(root_inode);
1785 out_deactivate:
1786         up_write(&sb->s_umount);
1787         deactivate_super(sb);
1788         return (struct super_block *)err;
1789 kill_rpciod:
1790         rpciod_down();
1791 free_hostname:
1792         kfree(server->hostname);
1793 free_server:
1794         kfree(server);
1795 out_err:
1796         return (struct super_block *)err;
1797 }
1798
1799 static int nfs_set_super(struct super_block *s, void *data)
1800 {
1801         s->s_fs_info = data;
1802         return set_anon_super(s, data);
1803 }
1804  
1805 static int nfs_compare_super(struct super_block *sb, void *data)
1806 {
1807         struct nfs_server *server = data;
1808         struct nfs_server *old = NFS_SB(sb);
1809
1810         if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1811                 return 0;
1812         if (old->addr.sin_port != server->addr.sin_port)
1813                 return 0;
1814         return !nfs_compare_fh(&old->fh, &server->fh);
1815 }
1816
1817 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1818         int flags, const char *dev_name, void *raw_data)
1819 {
1820         int error;
1821         struct nfs_server *server = NULL;
1822         struct super_block *s;
1823         struct nfs_fh *root;
1824         struct nfs_mount_data *data = raw_data;
1825
1826         s = ERR_PTR(-EINVAL);
1827         if (data == NULL) {
1828                 dprintk("%s: missing data argument\n", __FUNCTION__);
1829                 goto out_err;
1830         }
1831         if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
1832                 dprintk("%s: bad mount version\n", __FUNCTION__);
1833                 goto out_err;
1834         }
1835         switch (data->version) {
1836                 case 1:
1837                         data->namlen = 0;
1838                 case 2:
1839                         data->bsize  = 0;
1840                 case 3:
1841                         if (data->flags & NFS_MOUNT_VER3) {
1842                                 dprintk("%s: mount structure version %d does not support NFSv3\n",
1843                                                 __FUNCTION__,
1844                                                 data->version);
1845                                 goto out_err;
1846                         }
1847                         data->root.size = NFS2_FHSIZE;
1848                         memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1849                 case 4:
1850                         if (data->flags & NFS_MOUNT_SECFLAVOUR) {
1851                                 dprintk("%s: mount structure version %d does not support strong security\n",
1852                                                 __FUNCTION__,
1853                                                 data->version);
1854                                 goto out_err;
1855                         }
1856                 case 5:
1857                         memset(data->context, 0, sizeof(data->context));
1858         }
1859 #ifndef CONFIG_NFS_V3
1860         /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
1861         s = ERR_PTR(-EPROTONOSUPPORT);
1862         if (data->flags & NFS_MOUNT_VER3) {
1863                 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
1864                 goto out_err;
1865         }
1866 #endif /* CONFIG_NFS_V3 */
1867
1868         s = ERR_PTR(-ENOMEM);
1869         server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1870         if (!server)
1871                 goto out_err;
1872         /* Zero out the NFS state stuff */
1873         init_nfsv4_state(server);
1874         server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1875
1876         root = &server->fh;
1877         if (data->flags & NFS_MOUNT_VER3)
1878                 root->size = data->root.size;
1879         else
1880                 root->size = NFS2_FHSIZE;
1881         s = ERR_PTR(-EINVAL);
1882         if (root->size > sizeof(root->data)) {
1883                 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
1884                 goto out_err;
1885         }
1886         memcpy(root->data, data->root.data, root->size);
1887
1888         /* We now require that the mount process passes the remote address */
1889         memcpy(&server->addr, &data->addr, sizeof(server->addr));
1890         if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1891                 dprintk("%s: mount program didn't pass remote address!\n",
1892                                 __FUNCTION__);
1893                 goto out_err;
1894         }
1895
1896         /* Fire up rpciod if not yet running */
1897         s = ERR_PTR(rpciod_up());
1898         if (IS_ERR(s)) {
1899                 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1900                                 __FUNCTION__, PTR_ERR(s));
1901                 goto out_err;
1902         }
1903
1904         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1905         if (IS_ERR(s) || s->s_root)
1906                 goto out_rpciod_down;
1907
1908         s->s_flags = flags;
1909
1910         error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1911         if (error) {
1912                 up_write(&s->s_umount);
1913                 deactivate_super(s);
1914                 return ERR_PTR(error);
1915         }
1916         s->s_flags |= MS_ACTIVE;
1917         return s;
1918 out_rpciod_down:
1919         rpciod_down();
1920 out_err:
1921         kfree(server);
1922         return s;
1923 }
1924
1925 static void nfs_kill_super(struct super_block *s)
1926 {
1927         struct nfs_server *server = NFS_SB(s);
1928
1929         kill_anon_super(s);
1930
1931         if (!IS_ERR(server->client))
1932                 rpc_shutdown_client(server->client);
1933         if (!IS_ERR(server->client_sys))
1934                 rpc_shutdown_client(server->client_sys);
1935         if (!IS_ERR(server->client_acl))
1936                 rpc_shutdown_client(server->client_acl);
1937
1938         if (!(server->flags & NFS_MOUNT_NONLM))
1939                 lockd_down();   /* release rpc.lockd */
1940
1941         rpciod_down();          /* release rpciod */
1942
1943         nfs_free_iostats(server->io_stats);
1944         kfree(server->hostname);
1945         kfree(server);
1946 }
1947
1948 static struct file_system_type nfs_fs_type = {
1949         .owner          = THIS_MODULE,
1950         .name           = "nfs",
1951         .get_sb         = nfs_get_sb,
1952         .kill_sb        = nfs_kill_super,
1953         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1954 };
1955
1956 static struct super_block *nfs_clone_client(struct nfs_server *server, struct nfs_clone_mount *data)
1957 {
1958         struct super_block *sb;
1959
1960         sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1961         if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
1962                 lockd_up();
1963         return sb;
1964 }
1965
1966 static struct super_block *nfs_clone_nfs_sb(struct file_system_type *fs_type,
1967                 int flags, const char *dev_name, void *raw_data)
1968 {
1969         struct nfs_clone_mount *data = raw_data;
1970         return nfs_clone_generic_sb(data, nfs_clone_client);
1971 }
1972
1973 static struct file_system_type clone_nfs_fs_type = {
1974         .owner          = THIS_MODULE,
1975         .name           = "nfs",
1976         .get_sb         = nfs_clone_nfs_sb,
1977         .kill_sb        = nfs_kill_super,
1978         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1979 };
1980
1981 #ifdef CONFIG_NFS_V4
1982
1983 static void nfs4_clear_inode(struct inode *);
1984
1985
1986 static struct super_operations nfs4_sops = { 
1987         .alloc_inode    = nfs_alloc_inode,
1988         .destroy_inode  = nfs_destroy_inode,
1989         .write_inode    = nfs_write_inode,
1990         .statfs         = nfs_statfs,
1991         .clear_inode    = nfs4_clear_inode,
1992         .umount_begin   = nfs_umount_begin,
1993         .show_options   = nfs_show_options,
1994         .show_stats     = nfs_show_stats,
1995 };
1996
1997 /*
1998  * Clean out any remaining NFSv4 state that might be left over due
1999  * to open() calls that passed nfs_atomic_lookup, but failed to call
2000  * nfs_open().
2001  */
2002 static void nfs4_clear_inode(struct inode *inode)
2003 {
2004         struct nfs_inode *nfsi = NFS_I(inode);
2005
2006         /* If we are holding a delegation, return it! */
2007         nfs_inode_return_delegation(inode);
2008         /* First call standard NFS clear_inode() code */
2009         nfs_clear_inode(inode);
2010         /* Now clear out any remaining state */
2011         while (!list_empty(&nfsi->open_states)) {
2012                 struct nfs4_state *state;
2013                 
2014                 state = list_entry(nfsi->open_states.next,
2015                                 struct nfs4_state,
2016                                 inode_states);
2017                 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
2018                                 __FUNCTION__,
2019                                 inode->i_sb->s_id,
2020                                 (long long)NFS_FILEID(inode),
2021                                 state);
2022                 BUG_ON(atomic_read(&state->count) != 1);
2023                 nfs4_close_state(state, state->state);
2024         }
2025 }
2026
2027
2028 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
2029 {
2030         struct nfs_server *server;
2031         struct nfs4_client *clp = NULL;
2032         struct rpc_xprt *xprt = NULL;
2033         struct rpc_clnt *clnt = NULL;
2034         struct rpc_timeout timeparms;
2035         rpc_authflavor_t authflavour;
2036         int err = -EIO;
2037
2038         sb->s_blocksize_bits = 0;
2039         sb->s_blocksize = 0;
2040         server = NFS_SB(sb);
2041         if (data->rsize != 0)
2042                 server->rsize = nfs_block_size(data->rsize, NULL);
2043         if (data->wsize != 0)
2044                 server->wsize = nfs_block_size(data->wsize, NULL);
2045         server->flags = data->flags & NFS_MOUNT_FLAGMASK;
2046         server->caps = NFS_CAP_ATOMIC_OPEN;
2047
2048         server->acregmin = data->acregmin*HZ;
2049         server->acregmax = data->acregmax*HZ;
2050         server->acdirmin = data->acdirmin*HZ;
2051         server->acdirmax = data->acdirmax*HZ;
2052
2053         server->rpc_ops = &nfs_v4_clientops;
2054
2055         nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
2056
2057         server->retrans_timeo = timeparms.to_initval;
2058         server->retrans_count = timeparms.to_retries;
2059
2060         clp = nfs4_get_client(&server->addr.sin_addr);
2061         if (!clp) {
2062                 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
2063                 return -EIO;
2064         }
2065
2066         /* Now create transport and client */
2067         authflavour = RPC_AUTH_UNIX;
2068         if (data->auth_flavourlen != 0) {
2069                 if (data->auth_flavourlen != 1) {
2070                         dprintk("%s: Invalid number of RPC auth flavours %d.\n",
2071                                         __FUNCTION__, data->auth_flavourlen);
2072                         err = -EINVAL;
2073                         goto out_fail;
2074                 }
2075                 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
2076                         err = -EFAULT;
2077                         goto out_fail;
2078                 }
2079         }
2080
2081         down_write(&clp->cl_sem);
2082         if (IS_ERR(clp->cl_rpcclient)) {
2083                 xprt = xprt_create_proto(data->proto, &server->addr, &timeparms);
2084                 if (IS_ERR(xprt)) {
2085                         up_write(&clp->cl_sem);
2086                         err = PTR_ERR(xprt);
2087                         dprintk("%s: cannot create RPC transport. Error = %d\n",
2088                                         __FUNCTION__, err);
2089                         goto out_fail;
2090                 }
2091                 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
2092                                 server->rpc_ops->version, authflavour);
2093                 if (IS_ERR(clnt)) {
2094                         up_write(&clp->cl_sem);
2095                         err = PTR_ERR(clnt);
2096                         dprintk("%s: cannot create RPC client. Error = %d\n",
2097                                         __FUNCTION__, err);
2098                         goto out_fail;
2099                 }
2100                 clnt->cl_intr     = 1;
2101                 clnt->cl_softrtry = 1;
2102                 clp->cl_rpcclient = clnt;
2103                 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
2104                 nfs_idmap_new(clp);
2105         }
2106         list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
2107         clnt = rpc_clone_client(clp->cl_rpcclient);
2108         if (!IS_ERR(clnt))
2109                         server->nfs4_state = clp;
2110         up_write(&clp->cl_sem);
2111         clp = NULL;
2112
2113         if (IS_ERR(clnt)) {
2114                 err = PTR_ERR(clnt);
2115                 dprintk("%s: cannot create RPC client. Error = %d\n",
2116                                 __FUNCTION__, err);
2117                 return err;
2118         }
2119
2120         server->client    = clnt;
2121
2122         if (server->nfs4_state->cl_idmap == NULL) {
2123                 dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
2124                 return -ENOMEM;
2125         }
2126
2127         if (clnt->cl_auth->au_flavor != authflavour) {
2128                 struct rpc_auth *auth;
2129
2130                 auth = rpcauth_create(authflavour, clnt);
2131                 if (IS_ERR(auth)) {
2132                         dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
2133                         return PTR_ERR(auth);
2134                 }
2135         }
2136
2137         sb->s_time_gran = 1;
2138
2139         sb->s_op = &nfs4_sops;
2140         err = nfs_sb_init(sb, authflavour);
2141         if (err == 0)
2142                 return 0;
2143 out_fail:
2144         if (clp)
2145                 nfs4_put_client(clp);
2146         return err;
2147 }
2148
2149 static int nfs4_compare_super(struct super_block *sb, void *data)
2150 {
2151         struct nfs_server *server = data;
2152         struct nfs_server *old = NFS_SB(sb);
2153
2154         if (strcmp(server->hostname, old->hostname) != 0)
2155                 return 0;
2156         if (strcmp(server->mnt_path, old->mnt_path) != 0)
2157                 return 0;
2158         return 1;
2159 }
2160
2161 static void *
2162 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
2163 {
2164         void *p = NULL;
2165
2166         if (!src->len)
2167                 return ERR_PTR(-EINVAL);
2168         if (src->len < maxlen)
2169                 maxlen = src->len;
2170         if (dst == NULL) {
2171                 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
2172                 if (p == NULL)
2173                         return ERR_PTR(-ENOMEM);
2174         }
2175         if (copy_from_user(dst, src->data, maxlen)) {
2176                 kfree(p);
2177                 return ERR_PTR(-EFAULT);
2178         }
2179         dst[maxlen] = '\0';
2180         return dst;
2181 }
2182
2183 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
2184         int flags, const char *dev_name, void *raw_data)
2185 {
2186         int error;
2187         struct nfs_server *server;
2188         struct super_block *s;
2189         struct nfs4_mount_data *data = raw_data;
2190         void *p;
2191
2192         if (data == NULL) {
2193                 dprintk("%s: missing data argument\n", __FUNCTION__);
2194                 return ERR_PTR(-EINVAL);
2195         }
2196         if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
2197                 dprintk("%s: bad mount version\n", __FUNCTION__);
2198                 return ERR_PTR(-EINVAL);
2199         }
2200
2201         server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
2202         if (!server)
2203                 return ERR_PTR(-ENOMEM);
2204         /* Zero out the NFS state stuff */
2205         init_nfsv4_state(server);
2206         server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
2207
2208         p = nfs_copy_user_string(NULL, &data->hostname, 256);
2209         if (IS_ERR(p))
2210                 goto out_err;
2211         server->hostname = p;
2212
2213         p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
2214         if (IS_ERR(p))
2215                 goto out_err;
2216         server->mnt_path = p;
2217
2218         p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
2219                         sizeof(server->ip_addr) - 1);
2220         if (IS_ERR(p))
2221                 goto out_err;
2222
2223         /* We now require that the mount process passes the remote address */
2224         if (data->host_addrlen != sizeof(server->addr)) {
2225                 s = ERR_PTR(-EINVAL);
2226                 goto out_free;
2227         }
2228         if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
2229                 s = ERR_PTR(-EFAULT);
2230                 goto out_free;
2231         }
2232         if (server->addr.sin_family != AF_INET ||
2233             server->addr.sin_addr.s_addr == INADDR_ANY) {
2234                 dprintk("%s: mount program didn't pass remote IP address!\n",
2235                                 __FUNCTION__);
2236                 s = ERR_PTR(-EINVAL);
2237                 goto out_free;
2238         }
2239
2240         /* Fire up rpciod if not yet running */
2241         s = ERR_PTR(rpciod_up());
2242         if (IS_ERR(s)) {
2243                 dprintk("%s: couldn't start rpciod! Error = %ld\n",
2244                                 __FUNCTION__, PTR_ERR(s));
2245                 goto out_free;
2246         }
2247
2248         s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
2249
2250         if (IS_ERR(s) || s->s_root)
2251                 goto out_free;
2252
2253         s->s_flags = flags;
2254
2255         error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
2256         if (error) {
2257                 up_write(&s->s_umount);
2258                 deactivate_super(s);
2259                 return ERR_PTR(error);
2260         }
2261         s->s_flags |= MS_ACTIVE;
2262         return s;
2263 out_err:
2264         s = (struct super_block *)p;
2265 out_free:
2266         kfree(server->mnt_path);
2267         kfree(server->hostname);
2268         kfree(server);
2269         return s;
2270 }
2271
2272 static void nfs4_kill_super(struct super_block *sb)
2273 {
2274         struct nfs_server *server = NFS_SB(sb);
2275
2276         nfs_return_all_delegations(sb);
2277         kill_anon_super(sb);
2278
2279         nfs4_renewd_prepare_shutdown(server);
2280
2281         if (server->client != NULL && !IS_ERR(server->client))
2282                 rpc_shutdown_client(server->client);
2283
2284         destroy_nfsv4_state(server);
2285
2286         rpciod_down();
2287
2288         nfs_free_iostats(server->io_stats);
2289         kfree(server->hostname);
2290         kfree(server);
2291 }
2292
2293 static struct file_system_type nfs4_fs_type = {
2294         .owner          = THIS_MODULE,
2295         .name           = "nfs4",
2296         .get_sb         = nfs4_get_sb,
2297         .kill_sb        = nfs4_kill_super,
2298         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
2299 };
2300
2301 static const int nfs_set_port_min = 0;
2302 static const int nfs_set_port_max = 65535;
2303 static int param_set_port(const char *val, struct kernel_param *kp)
2304 {
2305         char *endp;
2306         int num = simple_strtol(val, &endp, 0);
2307         if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
2308                 return -EINVAL;
2309         *((int *)kp->arg) = num;
2310         return 0;
2311 }
2312
2313 module_param_call(callback_tcpport, param_set_port, param_get_int,
2314                  &nfs_callback_set_tcpport, 0644);
2315
2316 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
2317 {
2318         char *endp;
2319         int num = simple_strtol(val, &endp, 0);
2320         int jif = num * HZ;
2321         if (endp == val || *endp || num < 0 || jif < num)
2322                 return -EINVAL;
2323         *((int *)kp->arg) = jif;
2324         return 0;
2325 }
2326
2327 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
2328                  &nfs_idmap_cache_timeout, 0644);
2329
2330 /* Constructs the SERVER-side path */
2331 static inline char *nfs4_path(const struct dentry *dentry, char *buffer, ssize_t buflen)
2332 {
2333         return nfs_path(NFS_SB(dentry->d_sb)->mnt_path, dentry, buffer, buflen);
2334 }
2335
2336 static inline char *nfs4_dup_path(const struct dentry *dentry)
2337 {
2338         char *page = (char *) __get_free_page(GFP_USER);
2339         char *path;
2340
2341         path = nfs4_path(dentry, page, PAGE_SIZE);
2342         if (!IS_ERR(path)) {
2343                 int len = PAGE_SIZE + page - path;
2344                 char *tmp = path;
2345
2346                 path = kmalloc(len, GFP_KERNEL);
2347                 if (path)
2348                         memcpy(path, tmp, len);
2349                 else
2350                         path = ERR_PTR(-ENOMEM);
2351         }
2352         free_page((unsigned long)page);
2353         return path;
2354 }
2355
2356 static struct super_block *nfs4_clone_client(struct nfs_server *server, struct nfs_clone_mount *data)
2357 {
2358         const struct dentry *dentry = data->dentry;
2359         struct nfs4_client *clp = server->nfs4_state;
2360         struct super_block *sb;
2361
2362         server->mnt_path = nfs4_dup_path(dentry);
2363         if (IS_ERR(server->mnt_path)) {
2364                 sb = (struct super_block *)server->mnt_path;
2365                 goto err;
2366         }
2367         sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
2368         if (IS_ERR(sb) || sb->s_root)
2369                 goto free_path;
2370         nfs4_server_capabilities(server, &server->fh);
2371
2372         down_write(&clp->cl_sem);
2373         atomic_inc(&clp->cl_count);
2374         list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
2375         up_write(&clp->cl_sem);
2376         return sb;
2377 free_path:
2378         kfree(server->mnt_path);
2379 err:
2380         server->mnt_path = NULL;
2381         return sb;
2382 }
2383
2384 static struct super_block *nfs_clone_nfs4_sb(struct file_system_type *fs_type,
2385                 int flags, const char *dev_name, void *raw_data)
2386 {
2387         struct nfs_clone_mount *data = raw_data;
2388         return nfs_clone_generic_sb(data, nfs4_clone_client);
2389 }
2390
2391 static struct file_system_type clone_nfs4_fs_type = {
2392         .owner          = THIS_MODULE,
2393         .name           = "nfs",
2394         .get_sb         = nfs_clone_nfs4_sb,
2395         .kill_sb        = nfs4_kill_super,
2396         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
2397 };
2398
2399 #define nfs4_init_once(nfsi) \
2400         do { \
2401                 INIT_LIST_HEAD(&(nfsi)->open_states); \
2402                 nfsi->delegation = NULL; \
2403                 nfsi->delegation_state = 0; \
2404                 init_rwsem(&nfsi->rwsem); \
2405         } while(0)
2406
2407 static inline int register_nfs4fs(void)
2408 {
2409         int ret;
2410
2411         ret = nfs_register_sysctl();
2412         if (ret != 0)
2413                 return ret;
2414         ret = register_filesystem(&nfs4_fs_type);
2415         if (ret != 0)
2416                 nfs_unregister_sysctl();
2417         return ret;
2418 }
2419
2420 static inline void unregister_nfs4fs(void)
2421 {
2422         unregister_filesystem(&nfs4_fs_type);
2423         nfs_unregister_sysctl();
2424 }
2425 #else
2426 #define nfs4_clone_client(a,b) ERR_PTR(-EINVAL)
2427 #define nfs4_init_once(nfsi) \
2428         do { } while (0)
2429 #define register_nfs4fs() (0)
2430 #define unregister_nfs4fs()
2431 #endif
2432
2433 static inline char *nfs_devname(const struct vfsmount *mnt_parent,
2434                          const struct dentry *dentry,
2435                          char *buffer, ssize_t buflen)
2436 {
2437         return nfs_path(mnt_parent->mnt_devname, dentry, buffer, buflen);
2438 }
2439
2440 /**
2441  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
2442  * @mnt_parent - mountpoint of parent directory
2443  * @dentry - parent directory
2444  * @fh - filehandle for new root dentry
2445  * @fattr - attributes for new root inode
2446  *
2447  */
2448 struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
2449                 const struct dentry *dentry, struct nfs_fh *fh,
2450                 struct nfs_fattr *fattr)
2451 {
2452         struct nfs_clone_mount mountdata = {
2453                 .sb = mnt_parent->mnt_sb,
2454                 .dentry = dentry,
2455                 .fh = fh,
2456                 .fattr = fattr,
2457         };
2458         struct vfsmount *mnt = ERR_PTR(-ENOMEM);
2459         char *page = (char *) __get_free_page(GFP_USER);
2460         char *devname;
2461
2462         dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
2463                         dentry->d_parent->d_name.name,
2464                         dentry->d_name.name);
2465         if (page == NULL)
2466                 goto out;
2467         devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
2468         mnt = (struct vfsmount *)devname;
2469         if (IS_ERR(devname))
2470                 goto free_page;
2471         switch (NFS_SB(mnt_parent->mnt_sb)->rpc_ops->version) {
2472                 case 2:
2473                 case 3:
2474                         mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, &mountdata);
2475                         break;
2476                 case 4:
2477                         mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, &mountdata);
2478                         break;
2479                 default:
2480                         BUG();
2481         }
2482 free_page:
2483         free_page((unsigned long)page);
2484 out:
2485         dprintk("%s: done\n", __FUNCTION__);
2486         return mnt;
2487 }
2488
2489 extern int nfs_init_nfspagecache(void);
2490 extern void nfs_destroy_nfspagecache(void);
2491 extern int nfs_init_readpagecache(void);
2492 extern void nfs_destroy_readpagecache(void);
2493 extern int nfs_init_writepagecache(void);
2494 extern void nfs_destroy_writepagecache(void);
2495 #ifdef CONFIG_NFS_DIRECTIO
2496 extern int nfs_init_directcache(void);
2497 extern void nfs_destroy_directcache(void);
2498 #endif
2499
2500 static kmem_cache_t * nfs_inode_cachep;
2501
2502 static struct inode *nfs_alloc_inode(struct super_block *sb)
2503 {
2504         struct nfs_inode *nfsi;
2505         nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
2506         if (!nfsi)
2507                 return NULL;
2508         nfsi->flags = 0UL;
2509         nfsi->cache_validity = 0UL;
2510         nfsi->cache_change_attribute = jiffies;
2511 #ifdef CONFIG_NFS_V3_ACL
2512         nfsi->acl_access = ERR_PTR(-EAGAIN);
2513         nfsi->acl_default = ERR_PTR(-EAGAIN);
2514 #endif
2515 #ifdef CONFIG_NFS_V4
2516         nfsi->nfs4_acl = NULL;
2517 #endif /* CONFIG_NFS_V4 */
2518         return &nfsi->vfs_inode;
2519 }
2520
2521 static void nfs_destroy_inode(struct inode *inode)
2522 {
2523         kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2524 }
2525
2526 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
2527 {
2528         struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2529
2530         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2531             SLAB_CTOR_CONSTRUCTOR) {
2532                 inode_init_once(&nfsi->vfs_inode);
2533                 spin_lock_init(&nfsi->req_lock);
2534                 INIT_LIST_HEAD(&nfsi->dirty);
2535                 INIT_LIST_HEAD(&nfsi->commit);
2536                 INIT_LIST_HEAD(&nfsi->open_files);
2537                 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
2538                 atomic_set(&nfsi->data_updates, 0);
2539                 nfsi->ndirty = 0;
2540                 nfsi->ncommit = 0;
2541                 nfsi->npages = 0;
2542                 nfs4_init_once(nfsi);
2543         }
2544 }
2545  
2546 static int nfs_init_inodecache(void)
2547 {
2548         nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2549                                              sizeof(struct nfs_inode),
2550                                              0, (SLAB_RECLAIM_ACCOUNT|
2551                                                 SLAB_MEM_SPREAD),
2552                                              init_once, NULL);
2553         if (nfs_inode_cachep == NULL)
2554                 return -ENOMEM;
2555
2556         return 0;
2557 }
2558
2559 static void nfs_destroy_inodecache(void)
2560 {
2561         if (kmem_cache_destroy(nfs_inode_cachep))
2562                 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
2563 }
2564
2565 /*
2566  * Initialize NFS
2567  */
2568 static int __init init_nfs_fs(void)
2569 {
2570         int err;
2571
2572         err = nfs_init_nfspagecache();
2573         if (err)
2574                 goto out4;
2575
2576         err = nfs_init_inodecache();
2577         if (err)
2578                 goto out3;
2579
2580         err = nfs_init_readpagecache();
2581         if (err)
2582                 goto out2;
2583
2584         err = nfs_init_writepagecache();
2585         if (err)
2586                 goto out1;
2587
2588 #ifdef CONFIG_NFS_DIRECTIO
2589         err = nfs_init_directcache();
2590         if (err)
2591                 goto out0;
2592 #endif
2593
2594 #ifdef CONFIG_PROC_FS
2595         rpc_proc_register(&nfs_rpcstat);
2596 #endif
2597         err = register_filesystem(&nfs_fs_type);
2598         if (err)
2599                 goto out;
2600         if ((err = register_nfs4fs()) != 0)
2601                 goto out;
2602         return 0;
2603 out:
2604 #ifdef CONFIG_PROC_FS
2605         rpc_proc_unregister("nfs");
2606 #endif
2607 #ifdef CONFIG_NFS_DIRECTIO
2608         nfs_destroy_directcache();
2609 out0:
2610 #endif
2611         nfs_destroy_writepagecache();
2612 out1:
2613         nfs_destroy_readpagecache();
2614 out2:
2615         nfs_destroy_inodecache();
2616 out3:
2617         nfs_destroy_nfspagecache();
2618 out4:
2619         return err;
2620 }
2621
2622 static void __exit exit_nfs_fs(void)
2623 {
2624 #ifdef CONFIG_NFS_DIRECTIO
2625         nfs_destroy_directcache();
2626 #endif
2627         nfs_destroy_writepagecache();
2628         nfs_destroy_readpagecache();
2629         nfs_destroy_inodecache();
2630         nfs_destroy_nfspagecache();
2631 #ifdef CONFIG_PROC_FS
2632         rpc_proc_unregister("nfs");
2633 #endif
2634         unregister_filesystem(&nfs_fs_type);
2635         unregister_nfs4fs();
2636 }
2637
2638 /* Not quite true; I just maintain it */
2639 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2640 MODULE_LICENSE("GPL");
2641
2642 module_init(init_nfs_fs)
2643 module_exit(exit_nfs_fs)