VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / 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/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/smp_lock.h>
34 #include <linux/seq_file.h>
35 #include <linux/mount.h>
36 #include <linux/nfs_idmap.h>
37 #include <linux/vfs.h>
38 #include <linux/vserver/xid.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42
43 #define NFSDBG_FACILITY         NFSDBG_VFS
44 #define NFS_PARANOIA 1
45
46 /* Maximum number of readahead requests
47  * FIXME: this should really be a sysctl so that users may tune it to suit
48  *        their needs. People that do NFS over a slow network, might for
49  *        instance want to reduce it to something closer to 1 for improved
50  *        interactive response.
51  */
52 #define NFS_MAX_READAHEAD       (RPC_DEF_SLOT_TABLE - 1)
53
54 static void nfs_invalidate_inode(struct inode *);
55 static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
56
57 static struct inode *nfs_alloc_inode(struct super_block *sb);
58 static void nfs_destroy_inode(struct inode *);
59 static void nfs_write_inode(struct inode *,int);
60 static void nfs_delete_inode(struct inode *);
61 static void nfs_put_super(struct super_block *);
62 static void nfs_clear_inode(struct inode *);
63 static void nfs_umount_begin(struct super_block *);
64 static int  nfs_statfs(struct super_block *, struct kstatfs *);
65 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
66
67 static struct super_operations nfs_sops = { 
68         .alloc_inode    = nfs_alloc_inode,
69         .destroy_inode  = nfs_destroy_inode,
70         .write_inode    = nfs_write_inode,
71         .delete_inode   = nfs_delete_inode,
72         .put_super      = nfs_put_super,
73         .statfs         = nfs_statfs,
74         .clear_inode    = nfs_clear_inode,
75         .umount_begin   = nfs_umount_begin,
76         .show_options   = nfs_show_options,
77 };
78
79 /*
80  * RPC cruft for NFS
81  */
82 struct rpc_stat                 nfs_rpcstat = {
83         .program                = &nfs_program
84 };
85 static struct rpc_version *     nfs_version[] = {
86         NULL,
87         NULL,
88         &nfs_version2,
89 #if defined(CONFIG_NFS_V3)
90         &nfs_version3,
91 #elif defined(CONFIG_NFS_V4)
92         NULL,
93 #endif
94 #if defined(CONFIG_NFS_V4)
95         &nfs_version4,
96 #endif
97 };
98
99 struct rpc_program              nfs_program = {
100         .name                   = "nfs",
101         .number                 = NFS_PROGRAM,
102         .nrvers                 = sizeof(nfs_version) / sizeof(nfs_version[0]),
103         .version                = nfs_version,
104         .stats                  = &nfs_rpcstat,
105         .pipe_dir_name          = "/nfs",
106 };
107
108 static inline unsigned long
109 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
110 {
111         return nfs_fileid_to_ino_t(fattr->fileid);
112 }
113
114 static void
115 nfs_write_inode(struct inode *inode, int sync)
116 {
117         int flags = sync ? FLUSH_WAIT : 0;
118
119         nfs_commit_inode(inode, 0, 0, flags);
120 }
121
122 static void
123 nfs_delete_inode(struct inode * inode)
124 {
125         dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
126
127         /*
128          * The following can never actually happen...
129          */
130         if (nfs_have_writebacks(inode)) {
131                 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
132         }
133
134         clear_inode(inode);
135 }
136
137 /*
138  * For the moment, the only task for the NFS clear_inode method is to
139  * release the mmap credential
140  */
141 static void
142 nfs_clear_inode(struct inode *inode)
143 {
144         struct nfs_inode *nfsi = NFS_I(inode);
145         struct rpc_cred *cred = nfsi->mm_cred;
146
147         if (cred)
148                 put_rpccred(cred);
149         cred = nfsi->cache_access.cred;
150         if (cred)
151                 put_rpccred(cred);
152         BUG_ON(atomic_read(&nfsi->data_updates) != 0);
153 }
154
155 void
156 nfs_put_super(struct super_block *sb)
157 {
158         struct nfs_server *server = NFS_SB(sb);
159
160         nfs4_renewd_prepare_shutdown(server);
161
162         if (server->client != NULL)
163                 rpc_shutdown_client(server->client);
164         if (server->client_sys != NULL)
165                 rpc_shutdown_client(server->client_sys);
166
167         if (!(server->flags & NFS_MOUNT_NONLM))
168                 lockd_down();   /* release rpc.lockd */
169         rpciod_down();          /* release rpciod */
170
171         destroy_nfsv4_state(server);
172
173         kfree(server->hostname);
174 }
175
176 void
177 nfs_umount_begin(struct super_block *sb)
178 {
179         struct nfs_server *server = NFS_SB(sb);
180         struct rpc_clnt *rpc;
181
182         /* -EIO all pending I/O */
183         if ((rpc = server->client) != NULL)
184                 rpc_killall_tasks(rpc);
185 }
186
187
188 static inline unsigned long
189 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
190 {
191         /* make sure blocksize is a power of two */
192         if ((bsize & (bsize - 1)) || nrbitsp) {
193                 unsigned char   nrbits;
194
195                 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
196                         ;
197                 bsize = 1 << nrbits;
198                 if (nrbitsp)
199                         *nrbitsp = nrbits;
200         }
201
202         return bsize;
203 }
204
205 /*
206  * Calculate the number of 512byte blocks used.
207  */
208 static inline unsigned long
209 nfs_calc_block_size(u64 tsize)
210 {
211         loff_t used = (tsize + 511) >> 9;
212         return (used > ULONG_MAX) ? ULONG_MAX : used;
213 }
214
215 /*
216  * Compute and set NFS server blocksize
217  */
218 static inline unsigned long
219 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
220 {
221         if (bsize < 1024)
222                 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
223         else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
224                 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
225
226         return nfs_block_bits(bsize, nrbitsp);
227 }
228
229 /*
230  * Obtain the root inode of the file system.
231  */
232 static struct inode *
233 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
234 {
235         struct nfs_server       *server = NFS_SB(sb);
236         struct inode *rooti;
237         int                     error;
238
239         error = server->rpc_ops->getroot(server, rootfh, fsinfo);
240         if (error < 0) {
241                 dprintk("nfs_get_root: getattr error = %d\n", -error);
242                 return ERR_PTR(error);
243         }
244
245         rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
246         if (!rooti)
247                 return ERR_PTR(-ENOMEM);
248         return rooti;
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
268         /* We probably want something more informative here */
269         snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
270
271         server = NFS_SB(sb);
272
273         sb->s_magic      = NFS_SUPER_MAGIC;
274
275         root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
276         /* Did getting the root inode fail? */
277         if (IS_ERR(root_inode)) {
278                 no_root_error = PTR_ERR(root_inode);
279                 goto out_no_root;
280         }
281         sb->s_root = d_alloc_root(root_inode);
282         if (!sb->s_root) {
283                 no_root_error = -ENOMEM;
284                 goto out_no_root;
285         }
286         sb->s_root->d_op = server->rpc_ops->dentry_ops;
287
288         /* Get some general file system info */
289         if (server->namelen == 0 &&
290             server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
291                 server->namelen = pathinfo.max_namelen;
292         /* Work out a lot of parameters */
293         if (server->rsize == 0)
294                 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
295         if (server->wsize == 0)
296                 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
297         if (sb->s_blocksize == 0) {
298                 if (fsinfo.wtmult == 0) {
299                         sb->s_blocksize = 512;
300                         sb->s_blocksize_bits = 9;
301                 } else
302                         sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
303                                                          &sb->s_blocksize_bits);
304         }
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         server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
312         if (server->rpages > NFS_READ_MAXIOV) {
313                 server->rpages = NFS_READ_MAXIOV;
314                 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
315         }
316
317         server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
318         if (server->wpages > NFS_WRITE_MAXIOV) {
319                 server->wpages = NFS_WRITE_MAXIOV;
320                 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
321         }
322
323         server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
324         if (server->dtsize > PAGE_CACHE_SIZE)
325                 server->dtsize = PAGE_CACHE_SIZE;
326         if (server->dtsize > server->rsize)
327                 server->dtsize = server->rsize;
328
329         if (server->flags & NFS_MOUNT_NOAC) {
330                 server->acregmin = server->acregmax = 0;
331                 server->acdirmin = server->acdirmax = 0;
332                 sb->s_flags |= MS_SYNCHRONOUS;
333         }
334         server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
335
336         if (server->flags & NFS_MOUNT_TAGXID)
337                 sb->s_flags |= MS_TAGXID;
338
339         sb->s_maxbytes = fsinfo.maxfilesize;
340         if (sb->s_maxbytes > MAX_LFS_FILESIZE) 
341                 sb->s_maxbytes = MAX_LFS_FILESIZE; 
342
343         /* We're airborne Set socket buffersize */
344         rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
345         return 0;
346         /* Yargs. It didn't work out. */
347 out_no_root:
348         dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
349         if (!IS_ERR(root_inode))
350                 iput(root_inode);
351         return no_root_error;
352 }
353
354 /*
355  * Create an RPC client handle.
356  */
357 static struct rpc_clnt *
358 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
359 {
360         struct rpc_timeout      timeparms;
361         struct rpc_xprt         *xprt = NULL;
362         struct rpc_clnt         *clnt = NULL;
363         int                     tcp   = (data->flags & NFS_MOUNT_TCP);
364
365         /* Initialize timeout values */
366         timeparms.to_initval = data->timeo * HZ / 10;
367         timeparms.to_retries = data->retrans;
368         timeparms.to_maxval  = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
369         timeparms.to_exponential = 1;
370
371         if (!timeparms.to_initval)
372                 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
373         if (!timeparms.to_retries)
374                 timeparms.to_retries = 5;
375
376         /* create transport and client */
377         xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
378                                  &server->addr, &timeparms);
379         if (IS_ERR(xprt)) {
380                 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
381                 return (struct rpc_clnt *)xprt;
382         }
383         clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
384                                  server->rpc_ops->version, data->pseudoflavor);
385         if (IS_ERR(clnt)) {
386                 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
387                 goto out_fail;
388         }
389
390         clnt->cl_intr     = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
391         clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
392         clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
393         clnt->cl_tagxid   = (server->flags & NFS_MOUNT_TAGXID) ? 1 : 0;
394         clnt->cl_chatty   = 1;
395
396         return clnt;
397
398 out_fail:
399         xprt_destroy(xprt);
400         return clnt;
401 }
402
403 /*
404  * The way this works is that the mount process passes a structure
405  * in the data argument which contains the server's IP address
406  * and the root file handle obtained from the server's mount
407  * daemon. We stash these away in the private superblock fields.
408  */
409 static int
410 nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
411 {
412         struct nfs_server       *server;
413         int                     err = -EIO;
414         rpc_authflavor_t        authflavor;
415
416         server           = NFS_SB(sb);
417         sb->s_blocksize_bits = 0;
418         sb->s_blocksize = 0;
419         if (data->bsize)
420                 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
421         if (data->rsize)
422                 server->rsize = nfs_block_size(data->rsize, NULL);
423         if (data->wsize)
424                 server->wsize = nfs_block_size(data->wsize, NULL);
425         server->flags    = data->flags & NFS_MOUNT_FLAGMASK;
426
427         server->acregmin = data->acregmin*HZ;
428         server->acregmax = data->acregmax*HZ;
429         server->acdirmin = data->acdirmin*HZ;
430         server->acdirmax = data->acdirmax*HZ;
431
432         server->namelen  = data->namlen;
433         server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
434         if (!server->hostname)
435                 goto out_fail;
436         strcpy(server->hostname, data->hostname);
437
438         /* Check NFS protocol revision and initialize RPC op vector
439          * and file handle pool. */
440         if (server->flags & NFS_MOUNT_VER3) {
441 #ifdef CONFIG_NFS_V3
442                 server->rpc_ops = &nfs_v3_clientops;
443                 server->caps |= NFS_CAP_READDIRPLUS;
444                 if (data->version < 4) {
445                         printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
446                         goto out_fail;
447                 }
448 #else
449                 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
450                 goto out_fail;
451 #endif
452         } else {
453                 server->rpc_ops = &nfs_v2_clientops;
454         }
455
456         /* Fill in pseudoflavor for mount version < 5 */
457         if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
458                 data->pseudoflavor = RPC_AUTH_UNIX;
459         authflavor = data->pseudoflavor;        /* save for sb_init() */
460         /* XXX maybe we want to add a server->pseudoflavor field */
461
462         /* Create RPC client handles */
463         server->client = nfs_create_client(server, data);
464         if (IS_ERR(server->client))
465                 goto out_fail;
466         /* RFC 2623, sec 2.3.2 */
467         if (authflavor != RPC_AUTH_UNIX) {
468                 server->client_sys = rpc_clone_client(server->client);
469                 if (server->client_sys == NULL)
470                         goto out_shutdown;
471                 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
472                         goto out_shutdown;
473         } else {
474                 atomic_inc(&server->client->cl_count);
475                 server->client_sys = server->client;
476         }
477
478         /* Fire up rpciod if not yet running */
479         if (rpciod_up() != 0) {
480                 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
481                 goto out_shutdown;
482         }
483
484         sb->s_op = &nfs_sops;
485         err = nfs_sb_init(sb, authflavor);
486         if (err != 0)
487                 goto out_noinit;
488
489         if (server->flags & NFS_MOUNT_VER3) {
490                 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
491                         server->namelen = NFS3_MAXNAMLEN;
492         } else {
493                 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
494                         server->namelen = NFS2_MAXNAMLEN;
495         }
496
497         /* Check whether to start the lockd process */
498         if (!(server->flags & NFS_MOUNT_NONLM))
499                 lockd_up();
500         return 0;
501 out_noinit:
502         rpciod_down();
503 out_shutdown:
504         if (server->client)
505                 rpc_shutdown_client(server->client);
506         if (server->client_sys)
507                 rpc_shutdown_client(server->client_sys);
508 out_fail:
509         if (server->hostname)
510                 kfree(server->hostname);
511         return err;
512 }
513
514 static int
515 nfs_statfs(struct super_block *sb, struct kstatfs *buf)
516 {
517         struct nfs_server *server = NFS_SB(sb);
518         unsigned char blockbits;
519         unsigned long blockres;
520         struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
521         struct nfs_fattr fattr;
522         struct nfs_fsstat res = {
523                         .fattr = &fattr,
524         };
525         int error;
526
527         lock_kernel();
528
529         error = server->rpc_ops->statfs(server, rootfh, &res);
530         buf->f_type = NFS_SUPER_MAGIC;
531         if (error < 0)
532                 goto out_err;
533
534         buf->f_bsize = sb->s_blocksize;
535         blockbits = sb->s_blocksize_bits;
536         blockres = (1 << blockbits) - 1;
537         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
538         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
539         buf->f_bavail = (res.abytes + blockres) >> blockbits;
540         buf->f_files = res.tfiles;
541         buf->f_ffree = res.afiles;
542
543         buf->f_namelen = server->namelen;
544  out:
545         unlock_kernel();
546
547         return 0;
548
549  out_err:
550         printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
551         buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
552         goto out;
553
554 }
555
556 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
557 {
558         static struct proc_nfs_info {
559                 int flag;
560                 char *str;
561                 char *nostr;
562         } nfs_info[] = {
563                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
564                 { NFS_MOUNT_INTR, ",intr", "" },
565                 { NFS_MOUNT_POSIX, ",posix", "" },
566                 { NFS_MOUNT_TCP, ",tcp", ",udp" },
567                 { NFS_MOUNT_NOCTO, ",nocto", "" },
568                 { NFS_MOUNT_NOAC, ",noac", "" },
569                 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
570                 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
571                 { NFS_MOUNT_TAGXID, ",tagxid", "" },
572                 { 0, NULL, NULL }
573         };
574         struct proc_nfs_info *nfs_infop;
575         struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
576
577         seq_printf(m, ",v%d", nfss->rpc_ops->version);
578         seq_printf(m, ",rsize=%d", nfss->rsize);
579         seq_printf(m, ",wsize=%d", nfss->wsize);
580         if (nfss->acregmin != 3*HZ)
581                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
582         if (nfss->acregmax != 60*HZ)
583                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
584         if (nfss->acdirmin != 30*HZ)
585                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
586         if (nfss->acdirmax != 60*HZ)
587                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
588         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
589                 if (nfss->flags & nfs_infop->flag)
590                         seq_puts(m, nfs_infop->str);
591                 else
592                         seq_puts(m, nfs_infop->nostr);
593         }
594         seq_puts(m, ",addr=");
595         seq_escape(m, nfss->hostname, " \t\n\\");
596         return 0;
597 }
598
599 /*
600  * Invalidate the local caches
601  */
602 void
603 nfs_zap_caches(struct inode *inode)
604 {
605         struct nfs_inode *nfsi = NFS_I(inode);
606         int mode = inode->i_mode;
607
608         NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
609         NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
610
611         memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
612         if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
613                 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
614         else
615                 nfsi->flags |= NFS_INO_INVALID_ATTR;
616 }
617
618 /*
619  * Invalidate, but do not unhash, the inode
620  */
621 static void
622 nfs_invalidate_inode(struct inode *inode)
623 {
624         umode_t save_mode = inode->i_mode;
625
626         make_bad_inode(inode);
627         inode->i_mode = save_mode;
628         nfs_zap_caches(inode);
629 }
630
631 struct nfs_find_desc {
632         struct nfs_fh           *fh;
633         struct nfs_fattr        *fattr;
634 };
635
636 /*
637  * In NFSv3 we can have 64bit inode numbers. In order to support
638  * this, and re-exported directories (also seen in NFSv2)
639  * we are forced to allow 2 different inodes to have the same
640  * i_ino.
641  */
642 static int
643 nfs_find_actor(struct inode *inode, void *opaque)
644 {
645         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
646         struct nfs_fh           *fh = desc->fh;
647         struct nfs_fattr        *fattr = desc->fattr;
648
649         if (NFS_FILEID(inode) != fattr->fileid)
650                 return 0;
651         if (memcmp(NFS_FH(inode), fh, sizeof(struct nfs_fh)) != 0)
652                 return 0;
653         if (is_bad_inode(inode))
654                 return 0;
655         return 1;
656 }
657
658 static int
659 nfs_init_locked(struct inode *inode, void *opaque)
660 {
661         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
662         struct nfs_fh           *fh = desc->fh;
663         struct nfs_fattr        *fattr = desc->fattr;
664
665         NFS_FILEID(inode) = fattr->fileid;
666         memcpy(NFS_FH(inode), fh, sizeof(struct nfs_fh));
667         return 0;
668 }
669
670 /* Don't use READDIRPLUS on directories that we believe are too large */
671 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
672
673 /*
674  * This is our front-end to iget that looks up inodes by file handle
675  * instead of inode number.
676  */
677 struct inode *
678 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
679 {
680         struct nfs_find_desc desc = {
681                 .fh     = fh,
682                 .fattr  = fattr
683         };
684         struct inode *inode = NULL;
685         unsigned long hash;
686
687         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
688                 goto out_no_inode;
689
690         if (!fattr->nlink) {
691                 printk("NFS: Buggy server - nlink == 0!\n");
692                 goto out_no_inode;
693         }
694
695         hash = nfs_fattr_to_ino_t(fattr);
696
697         if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
698                 goto out_no_inode;
699
700         if (inode->i_state & I_NEW) {
701                 struct nfs_inode *nfsi = NFS_I(inode);
702
703                 /* We set i_ino for the few things that still rely on it,
704                  * such as stat(2) */
705                 inode->i_ino = hash;
706
707                 /* We can't support update_atime(), since the server will reset it */
708                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
709                 inode->i_mode = fattr->mode;
710                 /* Why so? Because we want revalidate for devices/FIFOs, and
711                  * that's precisely what we have in nfs_file_inode_operations.
712                  */
713                 inode->i_op = &nfs_file_inode_operations;
714                 if (S_ISREG(inode->i_mode)) {
715                         inode->i_fop = &nfs_file_operations;
716                         inode->i_data.a_ops = &nfs_file_aops;
717                         inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
718                 } else if (S_ISDIR(inode->i_mode)) {
719                         inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
720                         inode->i_fop = &nfs_dir_operations;
721                         if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
722                             && fattr->size <= NFS_LIMIT_READDIRPLUS)
723                                 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
724                 } else if (S_ISLNK(inode->i_mode))
725                         inode->i_op = &nfs_symlink_inode_operations;
726                 else
727                         init_special_inode(inode, inode->i_mode, fattr->rdev);
728
729                 nfsi->read_cache_jiffies = fattr->timestamp;
730                 inode->i_atime = fattr->atime;
731                 inode->i_mtime = fattr->mtime;
732                 inode->i_ctime = fattr->ctime;
733                 if (fattr->valid & NFS_ATTR_FATTR_V4)
734                         nfsi->change_attr = fattr->change_attr;
735                 inode->i_size = nfs_size_to_loff_t(fattr->size);
736                 inode->i_nlink = fattr->nlink;
737                 inode->i_uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
738                 inode->i_gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
739                 inode->i_xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
740                                          /* maybe fattr->xid someday */
741                 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
742                         /*
743                          * report the blocks in 512byte units
744                          */
745                         inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
746                         inode->i_blksize = inode->i_sb->s_blocksize;
747                 } else {
748                         inode->i_blocks = fattr->du.nfs2.blocks;
749                         inode->i_blksize = fattr->du.nfs2.blocksize;
750                 }
751                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
752                 nfsi->attrtimeo_timestamp = jiffies;
753                 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
754                 nfsi->cache_access.cred = NULL;
755
756                 unlock_new_inode(inode);
757         } else
758                 nfs_refresh_inode(inode, fattr);
759         dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
760                 inode->i_sb->s_id,
761                 (long long)NFS_FILEID(inode),
762                 atomic_read(&inode->i_count));
763
764 out:
765         return inode;
766 /*
767 fail_dlim:
768         make_bad_inode(inode);
769         iput(inode);
770         inode = NULL;
771 */
772 out_no_inode:
773         printk("nfs_fhget: iget failed\n");
774         goto out;
775 }
776
777 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_XID|ATTR_SIZE|\
778                          ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
779
780 int
781 nfs_setattr(struct dentry *dentry, struct iattr *attr)
782 {
783         struct inode *inode = dentry->d_inode;
784         struct nfs_fattr fattr;
785         int error;
786
787         if (attr->ia_valid & ATTR_SIZE) {
788                 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
789                         attr->ia_valid &= ~ATTR_SIZE;
790         }
791
792         /* Optimization: if the end result is no change, don't RPC */
793         attr->ia_valid &= NFS_VALID_ATTRS;
794         if (attr->ia_valid == 0)
795                 return 0;
796
797         lock_kernel();
798         nfs_begin_data_update(inode);
799         /* Write all dirty data if we're changing file permissions or size */
800         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
801                 if (filemap_fdatawrite(inode->i_mapping) == 0)
802                         filemap_fdatawait(inode->i_mapping);
803                 nfs_wb_all(inode);
804         }
805         error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
806         if (error == 0) {
807                 nfs_refresh_inode(inode, &fattr);
808                 if ((attr->ia_valid & ATTR_MODE) != 0) {
809                         int mode;
810                         mode = inode->i_mode & ~S_IALLUGO;
811                         mode |= attr->ia_mode & S_IALLUGO;
812                         inode->i_mode = mode;
813                 }
814                 if ((attr->ia_valid & ATTR_UID) != 0)
815                         inode->i_uid = attr->ia_uid;
816                 if ((attr->ia_valid & ATTR_GID) != 0)
817                         inode->i_gid = attr->ia_gid;
818                 if ((attr->ia_valid & ATTR_XID) != 0)
819                         inode->i_xid = attr->ia_xid;
820                 if ((attr->ia_valid & ATTR_SIZE) != 0) {
821                         inode->i_size = attr->ia_size;
822                         vmtruncate(inode, attr->ia_size);
823                 }
824         }
825         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
826                 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
827                 if (*cred) {
828                         put_rpccred(*cred);
829                         *cred = NULL;
830                 }
831         }
832         nfs_end_data_update(inode);
833         unlock_kernel();
834         return error;
835 }
836
837 /*
838  * Wait for the inode to get unlocked.
839  * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
840  */
841 int
842 nfs_wait_on_inode(struct inode *inode, int flag)
843 {
844         struct rpc_clnt *clnt = NFS_CLIENT(inode);
845         struct nfs_inode *nfsi = NFS_I(inode);
846
847         int error;
848         if (!(NFS_FLAGS(inode) & flag))
849                 return 0;
850         atomic_inc(&inode->i_count);
851         error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
852                                 !(NFS_FLAGS(inode) & flag));
853         iput(inode);
854         return error;
855 }
856
857 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
858 {
859         struct inode *inode = dentry->d_inode;
860         struct nfs_inode *nfsi = NFS_I(inode);
861         int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
862         int err;
863
864         if (__IS_FLG(inode, MS_NOATIME))
865                 need_atime = 0;
866         else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
867                 need_atime = 0;
868         /* We may force a getattr if the user cares about atime */
869         if (need_atime)
870                 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
871         else
872                 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
873         if (!err)
874                 generic_fillattr(inode, stat);
875         return err;
876 }
877
878 /*
879  * Ensure that mmap has a recent RPC credential for use when writing out
880  * shared pages
881  */
882 void
883 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
884 {
885         struct rpc_cred **p = &NFS_I(inode)->mm_cred,
886                         *oldcred = *p;
887
888         *p = get_rpccred(cred);
889         if (oldcred)
890                 put_rpccred(oldcred);
891 }
892
893 /*
894  * These are probably going to contain hooks for
895  * allocating and releasing RPC credentials for
896  * the file. I'll have to think about Tronds patch
897  * a bit more..
898  */
899 int nfs_open(struct inode *inode, struct file *filp)
900 {
901         struct rpc_auth *auth;
902         struct rpc_cred *cred;
903
904         auth = NFS_CLIENT(inode)->cl_auth;
905         cred = rpcauth_lookupcred(auth, 0);
906         filp->private_data = cred;
907         if ((filp->f_mode & FMODE_WRITE) != 0) {
908                 nfs_set_mmcred(inode, cred);
909                 nfs_begin_data_update(inode);
910         }
911         return 0;
912 }
913
914 int nfs_release(struct inode *inode, struct file *filp)
915 {
916         struct rpc_cred *cred;
917
918         lock_kernel();
919         if ((filp->f_mode & FMODE_WRITE) != 0)
920                 nfs_end_data_update(inode);
921         cred = nfs_file_cred(filp);
922         if (cred)
923                 put_rpccred(cred);
924         unlock_kernel();
925         return 0;
926 }
927
928 /*
929  * This function is called whenever some part of NFS notices that
930  * the cached attributes have to be refreshed.
931  */
932 int
933 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
934 {
935         int              status = -ESTALE;
936         struct nfs_fattr fattr;
937         struct nfs_inode *nfsi = NFS_I(inode);
938         unsigned long verifier;
939         unsigned int flags;
940
941         dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
942                 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
943
944         lock_kernel();
945         if (!inode || is_bad_inode(inode))
946                 goto out_nowait;
947         if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
948                 goto out_nowait;
949
950         while (NFS_REVALIDATING(inode)) {
951                 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
952                 if (status < 0)
953                         goto out_nowait;
954                 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
955                         continue;
956                 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
957                         continue;
958                 status = NFS_STALE(inode) ? -ESTALE : 0;
959                 goto out_nowait;
960         }
961         NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
962
963         /* Protect against RPC races by saving the change attribute */
964         verifier = nfs_save_change_attribute(inode);
965         status = NFS_PROTO(inode)->getattr(inode, &fattr);
966         if (status) {
967                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
968                          inode->i_sb->s_id,
969                          (long long)NFS_FILEID(inode), status);
970                 if (status == -ESTALE) {
971                         NFS_FLAGS(inode) |= NFS_INO_STALE;
972                         if (inode != inode->i_sb->s_root->d_inode)
973                                 remove_inode_hash(inode);
974                 }
975                 goto out;
976         }
977
978         status = nfs_update_inode(inode, &fattr, verifier);
979         if (status) {
980                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
981                          inode->i_sb->s_id,
982                          (long long)NFS_FILEID(inode), status);
983                 goto out;
984         }
985         flags = nfsi->flags;
986         /*
987          * We may need to keep the attributes marked as invalid if
988          * we raced with nfs_end_attr_update().
989          */
990         if (verifier == nfsi->cache_change_attribute)
991                 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
992         /* Do the page cache invalidation */
993         if (flags & NFS_INO_INVALID_DATA) {
994                 if (S_ISREG(inode->i_mode)) {
995                         if (filemap_fdatawrite(inode->i_mapping) == 0)
996                                 filemap_fdatawait(inode->i_mapping);
997                         nfs_wb_all(inode);
998                 }
999                 nfsi->flags &= ~NFS_INO_INVALID_DATA;
1000                 invalidate_inode_pages2(inode->i_mapping);
1001                 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1002                 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1003                                 inode->i_sb->s_id,
1004                                 (long long)NFS_FILEID(inode));
1005                 /* This ensures we revalidate dentries */
1006                 nfsi->cache_change_attribute++;
1007         }
1008         dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1009                 inode->i_sb->s_id,
1010                 (long long)NFS_FILEID(inode));
1011
1012         NFS_FLAGS(inode) &= ~NFS_INO_STALE;
1013 out:
1014         NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1015         wake_up(&nfsi->nfs_i_wait);
1016  out_nowait:
1017         unlock_kernel();
1018         return status;
1019 }
1020
1021 /**
1022  * nfs_begin_data_update
1023  * @inode - pointer to inode
1024  * Declare that a set of operations will update file data on the server
1025  */
1026 void nfs_begin_data_update(struct inode *inode)
1027 {
1028         atomic_inc(&NFS_I(inode)->data_updates);
1029 }
1030
1031 /**
1032  * nfs_end_data_update
1033  * @inode - pointer to inode
1034  * Declare end of the operations that will update file data
1035  * This will mark the inode as immediately needing revalidation
1036  * of its attribute cache.
1037  */
1038 void nfs_end_data_update(struct inode *inode)
1039 {
1040         struct nfs_inode *nfsi = NFS_I(inode);
1041
1042         /* Mark the attribute cache for revalidation */
1043         nfsi->flags |= NFS_INO_INVALID_ATTR;
1044         /* Directories and symlinks: invalidate page cache too */
1045         if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1046                 nfsi->flags |= NFS_INO_INVALID_DATA;
1047         nfsi->cache_change_attribute ++;
1048         atomic_dec(&nfsi->data_updates);
1049 }
1050
1051 /**
1052  * nfs_end_data_update_defer
1053  * @inode - pointer to inode
1054  * Declare end of the operations that will update file data
1055  * This will defer marking the inode as needing revalidation
1056  * unless there are no other pending updates.
1057  */
1058 void nfs_end_data_update_defer(struct inode *inode)
1059 {
1060         struct nfs_inode *nfsi = NFS_I(inode);
1061
1062         if (atomic_dec_and_test(&nfsi->data_updates)) {
1063                 /* Mark the attribute cache for revalidation */
1064                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1065                 /* Directories and symlinks: invalidate page cache too */
1066                 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1067                         nfsi->flags |= NFS_INO_INVALID_DATA;
1068                 nfsi->cache_change_attribute ++;
1069         }
1070 }
1071
1072 /**
1073  * nfs_refresh_inode - verify consistency of the inode attribute cache
1074  * @inode - pointer to inode
1075  * @fattr - updated attributes
1076  *
1077  * Verifies the attribute cache. If we have just changed the attributes,
1078  * so that fattr carries weak cache consistency data, then it may
1079  * also update the ctime/mtime/change_attribute.
1080  */
1081 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1082 {
1083         struct nfs_inode *nfsi = NFS_I(inode);
1084         loff_t cur_size, new_isize;
1085         int data_unstable;
1086         uid_t uid;
1087         gid_t gid;
1088         xid_t xid = 0;
1089
1090         /* Are we in the process of updating data on the server? */
1091         data_unstable = nfs_caches_unstable(inode);
1092
1093         if (fattr->valid & NFS_ATTR_FATTR_V4) {
1094                 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1095                                 && nfsi->change_attr == fattr->pre_change_attr)
1096                         nfsi->change_attr = fattr->change_attr;
1097                 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1098                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1099         }
1100
1101         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1102                 return 0;
1103
1104         /* Has the inode gone and changed behind our back? */
1105         if (nfsi->fileid != fattr->fileid
1106                         || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1107                 return -EIO;
1108
1109         cur_size = i_size_read(inode);
1110         new_isize = nfs_size_to_loff_t(fattr->size);
1111
1112         /* If we have atomic WCC data, we may update some attributes */
1113         if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1114                 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1115                         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1116                 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1117                         memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1118         }
1119
1120         /* Verify a few of the more important attributes */
1121         if (!data_unstable) {
1122                 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1123                                 || cur_size != new_isize)
1124                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1125         } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1126                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1127
1128         uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
1129         gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
1130         xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
1131
1132         /* Have any file permissions changed? */
1133         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1134                         || inode->i_uid != uid
1135                         || inode->i_gid != gid
1136                         || inode->i_xid != xid)
1137                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1138
1139         /* Has the link count changed? */
1140         if (inode->i_nlink != fattr->nlink)
1141                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1142
1143         if (!timespec_equal(&inode->i_atime, &fattr->atime))
1144                 nfsi->flags |= NFS_INO_INVALID_ATIME;
1145
1146         nfsi->read_cache_jiffies = fattr->timestamp;
1147         return 0;
1148 }
1149
1150 /*
1151  * Many nfs protocol calls return the new file attributes after
1152  * an operation.  Here we update the inode to reflect the state
1153  * of the server's inode.
1154  *
1155  * This is a bit tricky because we have to make sure all dirty pages
1156  * have been sent off to the server before calling invalidate_inode_pages.
1157  * To make sure no other process adds more write requests while we try
1158  * our best to flush them, we make them sleep during the attribute refresh.
1159  *
1160  * A very similar scenario holds for the dir cache.
1161  */
1162 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1163 {
1164         struct nfs_inode *nfsi = NFS_I(inode);
1165         __u64           new_size;
1166         loff_t          new_isize;
1167         unsigned int    invalid = 0;
1168         loff_t          cur_isize;
1169         int data_unstable;
1170         uid_t           uid;
1171         gid_t           gid;
1172         xid_t           xid = 0;
1173
1174         dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1175                         __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1176                         atomic_read(&inode->i_count), fattr->valid);
1177
1178         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1179                 return 0;
1180
1181         if (nfsi->fileid != fattr->fileid) {
1182                 printk(KERN_ERR "%s: inode number mismatch\n"
1183                        "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1184                        __FUNCTION__,
1185                        inode->i_sb->s_id, (long long)nfsi->fileid,
1186                        inode->i_sb->s_id, (long long)fattr->fileid);
1187                 goto out_err;
1188         }
1189
1190         /*
1191          * Make sure the inode's type hasn't changed.
1192          */
1193         if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1194                 goto out_changed;
1195
1196         /*
1197          * Update the read time so we don't revalidate too often.
1198          */
1199         nfsi->read_cache_jiffies = fattr->timestamp;
1200
1201         /* Are we racing with known updates of the metadata on the server? */
1202         data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1203
1204         /* Check if the file size agrees */
1205         new_size = fattr->size;
1206         new_isize = nfs_size_to_loff_t(fattr->size);
1207         cur_isize = i_size_read(inode);
1208         if (cur_isize != new_size) {
1209 #ifdef NFS_DEBUG_VERBOSE
1210                 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1211 #endif
1212                 /*
1213                  * If we have pending writebacks, things can get
1214                  * messy.
1215                  */
1216                 if (S_ISREG(inode->i_mode) && data_unstable) {
1217                         if (new_isize > cur_isize) {
1218                                 inode->i_size = new_isize;
1219                                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1220                         }
1221                 } else {
1222                         inode->i_size = new_isize;
1223                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1224                 }
1225         }
1226
1227         /*
1228          * Note: we don't check inode->i_mtime since pipes etc.
1229          *       can change this value in VFS without requiring a
1230          *       cache revalidation.
1231          */
1232         if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1233                 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1234 #ifdef NFS_DEBUG_VERBOSE
1235                 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1236 #endif
1237                 if (!data_unstable)
1238                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1239         }
1240
1241         if ((fattr->valid & NFS_ATTR_FATTR_V4)
1242             && nfsi->change_attr != fattr->change_attr) {
1243 #ifdef NFS_DEBUG_VERBOSE
1244                 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1245                        inode->i_sb->s_id, inode->i_ino);
1246 #endif
1247                 nfsi->change_attr = fattr->change_attr;
1248                 if (!data_unstable)
1249                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1250         }
1251
1252         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1253         memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1254
1255         uid = INOXID_UID(XID_TAG(inode), fattr->uid, fattr->gid);
1256         gid = INOXID_GID(XID_TAG(inode), fattr->uid, fattr->gid);
1257         xid = INOXID_XID(XID_TAG(inode), fattr->uid, fattr->gid, 0);
1258
1259         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1260             inode->i_uid != uid ||
1261             inode->i_gid != gid ||
1262             inode->i_xid != xid) {
1263                 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1264                 if (*cred) {
1265                         put_rpccred(*cred);
1266                         *cred = NULL;
1267                 }
1268                 invalid |= NFS_INO_INVALID_ATTR;
1269         }
1270
1271         inode->i_mode = fattr->mode;
1272         inode->i_nlink = fattr->nlink;
1273         inode->i_uid = uid;
1274         inode->i_gid = gid;
1275         inode->i_xid = xid;
1276
1277         if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1278                 /*
1279                  * report the blocks in 512byte units
1280                  */
1281                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1282                 inode->i_blksize = inode->i_sb->s_blocksize;
1283         } else {
1284                 inode->i_blocks = fattr->du.nfs2.blocks;
1285                 inode->i_blksize = fattr->du.nfs2.blocksize;
1286         }
1287
1288         /* Update attrtimeo value if we're out of the unstable period */
1289         if (invalid & NFS_INO_INVALID_ATTR) {
1290                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1291                 nfsi->attrtimeo_timestamp = jiffies;
1292         } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1293                 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1294                         nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1295                 nfsi->attrtimeo_timestamp = jiffies;
1296         }
1297         /* Don't invalidate the data if we were to blame */
1298         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1299                                 || S_ISLNK(inode->i_mode)))
1300                 invalid &= ~NFS_INO_INVALID_DATA;
1301         nfsi->flags |= invalid;
1302
1303         return 0;
1304  out_changed:
1305         /*
1306          * Big trouble! The inode has become a different object.
1307          */
1308 #ifdef NFS_PARANOIA
1309         printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1310                         __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1311 #endif
1312         /*
1313          * No need to worry about unhashing the dentry, as the
1314          * lookup validation will know that the inode is bad.
1315          * (But we fall through to invalidate the caches.)
1316          */
1317         nfs_invalidate_inode(inode);
1318  out_err:
1319         return -EIO;
1320 }
1321
1322 /*
1323  * File system information
1324  */
1325
1326 static int nfs_set_super(struct super_block *s, void *data)
1327 {
1328         s->s_fs_info = data;
1329         return set_anon_super(s, data);
1330 }
1331  
1332 static int nfs_compare_super(struct super_block *sb, void *data)
1333 {
1334         struct nfs_server *server = data;
1335         struct nfs_server *old = NFS_SB(sb);
1336
1337         if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1338                 return 0;
1339         if (old->addr.sin_port != server->addr.sin_port)
1340                 return 0;
1341         return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1342 }
1343
1344 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1345         int flags, const char *dev_name, void *raw_data)
1346 {
1347         int error;
1348         struct nfs_server *server;
1349         struct super_block *s;
1350         struct nfs_fh *root;
1351         struct nfs_mount_data *data = raw_data;
1352
1353         if (!data) {
1354                 printk("nfs_read_super: missing data argument\n");
1355                 return ERR_PTR(-EINVAL);
1356         }
1357
1358         server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1359         if (!server)
1360                 return ERR_PTR(-ENOMEM);
1361         memset(server, 0, sizeof(struct nfs_server));
1362         /* Zero out the NFS state stuff */
1363         init_nfsv4_state(server);
1364
1365         root = &server->fh;
1366         memcpy(root, &data->root, sizeof(*root));
1367         if (root->size < sizeof(root->data))
1368                 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1369
1370         if (data->version != NFS_MOUNT_VERSION) {
1371                 printk("nfs warning: mount version %s than kernel\n",
1372                         data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1373                 if (data->version < 2)
1374                         data->namlen = 0;
1375                 if (data->version < 3)
1376                         data->bsize  = 0;
1377                 if (data->version < 4) {
1378                         data->flags &= ~NFS_MOUNT_VER3;
1379                         memset(root, 0, sizeof(*root));
1380                         root->size = NFS2_FHSIZE;
1381                         memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1382                 }
1383                 if (data->version < 5)
1384                         data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1385         }
1386
1387         if (root->size > sizeof(root->data)) {
1388                 printk("nfs_get_sb: invalid root filehandle\n");
1389                 kfree(server);
1390                 return ERR_PTR(-EINVAL);
1391         }
1392         /* We now require that the mount process passes the remote address */
1393         memcpy(&server->addr, &data->addr, sizeof(server->addr));
1394         if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1395                 printk("NFS: mount program didn't pass remote address!\n");
1396                 kfree(server);
1397                 return ERR_PTR(-EINVAL);
1398         }
1399
1400         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1401
1402         if (IS_ERR(s) || s->s_root) {
1403                 kfree(server);
1404                 return s;
1405         }
1406
1407         s->s_flags = flags;
1408
1409         error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1410         if (error) {
1411                 up_write(&s->s_umount);
1412                 deactivate_super(s);
1413                 return ERR_PTR(error);
1414         }
1415         s->s_flags |= MS_ACTIVE;
1416         return s;
1417 }
1418
1419 static void nfs_kill_super(struct super_block *s)
1420 {
1421         struct nfs_server *server = NFS_SB(s);
1422         kill_anon_super(s);
1423         kfree(server);
1424 }
1425
1426 static struct file_system_type nfs_fs_type = {
1427         .owner          = THIS_MODULE,
1428         .name           = "nfs",
1429         .get_sb         = nfs_get_sb,
1430         .kill_sb        = nfs_kill_super,
1431         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1432 };
1433
1434 #ifdef CONFIG_NFS_V4
1435
1436 static void nfs4_clear_inode(struct inode *);
1437
1438 static struct super_operations nfs4_sops = { 
1439         .alloc_inode    = nfs_alloc_inode,
1440         .destroy_inode  = nfs_destroy_inode,
1441         .write_inode    = nfs_write_inode,
1442         .delete_inode   = nfs_delete_inode,
1443         .put_super      = nfs_put_super,
1444         .statfs         = nfs_statfs,
1445         .clear_inode    = nfs4_clear_inode,
1446         .umount_begin   = nfs_umount_begin,
1447         .show_options   = nfs_show_options,
1448 };
1449
1450 /*
1451  * Clean out any remaining NFSv4 state that might be left over due
1452  * to open() calls that passed nfs_atomic_lookup, but failed to call
1453  * nfs_open().
1454  */
1455 static void nfs4_clear_inode(struct inode *inode)
1456 {
1457         struct nfs_inode *nfsi = NFS_I(inode);
1458
1459         while (!list_empty(&nfsi->open_states)) {
1460                 struct nfs4_state *state;
1461                 
1462                 state = list_entry(nfsi->open_states.next,
1463                                 struct nfs4_state,
1464                                 inode_states);
1465                 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1466                                 __FUNCTION__,
1467                                 inode->i_sb->s_id,
1468                                 (long long)NFS_FILEID(inode),
1469                                 state);
1470                 BUG_ON(atomic_read(&state->count) != 1);
1471                 nfs4_close_state(state, state->state);
1472         }
1473         /* Now call standard NFS clear_inode() code */
1474         nfs_clear_inode(inode);
1475 }
1476
1477
1478 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1479 {
1480         struct nfs_server *server;
1481         struct nfs4_client *clp = NULL;
1482         struct rpc_xprt *xprt = NULL;
1483         struct rpc_clnt *clnt = NULL;
1484         struct rpc_timeout timeparms;
1485         rpc_authflavor_t authflavour;
1486         int proto, err = -EIO;
1487
1488         sb->s_blocksize_bits = 0;
1489         sb->s_blocksize = 0;
1490         server = NFS_SB(sb);
1491         if (data->rsize != 0)
1492                 server->rsize = nfs_block_size(data->rsize, NULL);
1493         if (data->wsize != 0)
1494                 server->wsize = nfs_block_size(data->wsize, NULL);
1495         server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1496
1497         /* NFSv4 doesn't use NLM locking */
1498         server->flags |= NFS_MOUNT_NONLM;
1499
1500         server->acregmin = data->acregmin*HZ;
1501         server->acregmax = data->acregmax*HZ;
1502         server->acdirmin = data->acdirmin*HZ;
1503         server->acdirmax = data->acdirmax*HZ;
1504
1505         server->rpc_ops = &nfs_v4_clientops;
1506         /* Initialize timeout values */
1507
1508         timeparms.to_initval = data->timeo * HZ / 10;
1509         timeparms.to_retries = data->retrans;
1510         timeparms.to_exponential = 1;
1511         if (!timeparms.to_retries)
1512                 timeparms.to_retries = 5;
1513
1514         proto = data->proto;
1515         /* Which IP protocol do we use? */
1516         switch (proto) {
1517         case IPPROTO_TCP:
1518                 timeparms.to_maxval  = RPC_MAX_TCP_TIMEOUT;
1519                 if (!timeparms.to_initval)
1520                         timeparms.to_initval = 600 * HZ / 10;
1521                 break;
1522         case IPPROTO_UDP:
1523                 timeparms.to_maxval  = RPC_MAX_UDP_TIMEOUT;
1524                 if (!timeparms.to_initval)
1525                         timeparms.to_initval = 11 * HZ / 10;
1526                 break;
1527         default:
1528                 return -EINVAL;
1529         }
1530
1531         clp = nfs4_get_client(&server->addr.sin_addr);
1532         if (!clp) {
1533                 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1534                 goto out_fail;
1535         }
1536
1537         /* Now create transport and client */
1538         authflavour = RPC_AUTH_UNIX;
1539         if (data->auth_flavourlen != 0) {
1540                 if (data->auth_flavourlen > 1)
1541                         printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1542                 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1543                         err = -EFAULT;
1544                         goto out_fail;
1545                 }
1546         }
1547
1548         down_write(&clp->cl_sem);
1549         if (clp->cl_rpcclient == NULL) {
1550                 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1551                 if (IS_ERR(xprt)) {
1552                         up_write(&clp->cl_sem);
1553                         printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1554                         err = PTR_ERR(xprt);
1555                         goto out_fail;
1556                 }
1557                 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1558                                 server->rpc_ops->version, authflavour);
1559                 if (IS_ERR(clnt)) {
1560                         up_write(&clp->cl_sem);
1561                         printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1562                         xprt_destroy(xprt);
1563                         err = PTR_ERR(clnt);
1564                         goto out_fail;
1565                 }
1566                 clnt->cl_chatty   = 1;
1567                 clp->cl_rpcclient = clnt;
1568                 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1569                 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1570                 nfs_idmap_new(clp);
1571         }
1572         if (list_empty(&clp->cl_superblocks))
1573                 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1574         list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1575         clnt = rpc_clone_client(clp->cl_rpcclient);
1576         if (!IS_ERR(clnt))
1577                         server->nfs4_state = clp;
1578         up_write(&clp->cl_sem);
1579         clp = NULL;
1580
1581         if (IS_ERR(clnt)) {
1582                 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1583                 err = PTR_ERR(clnt);
1584                 goto out_remove_list;
1585         }
1586
1587         clnt->cl_intr     = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1588         clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1589         server->client    = clnt;
1590
1591         err = -ENOMEM;
1592         if (server->nfs4_state->cl_idmap == NULL) {
1593                 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1594                 goto out_shutdown;
1595         }
1596
1597         if (clnt->cl_auth->au_flavor != authflavour) {
1598                 if (rpcauth_create(authflavour, clnt) == NULL) {
1599                         printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1600                         goto out_shutdown;
1601                 }
1602         }
1603
1604         /* Fire up rpciod if not yet running */
1605         if (rpciod_up() != 0) {
1606                 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1607                 goto out_shutdown;
1608         }
1609
1610         sb->s_op = &nfs4_sops;
1611         err = nfs_sb_init(sb, authflavour);
1612         if (err == 0)
1613                 return 0;
1614         rpciod_down();
1615 out_shutdown:
1616         rpc_shutdown_client(server->client);
1617 out_remove_list:
1618         down_write(&server->nfs4_state->cl_sem);
1619         list_del_init(&server->nfs4_siblings);
1620         up_write(&server->nfs4_state->cl_sem);
1621         destroy_nfsv4_state(server);
1622 out_fail:
1623         if (clp)
1624                 nfs4_put_client(clp);
1625         return err;
1626 }
1627
1628 static int nfs4_compare_super(struct super_block *sb, void *data)
1629 {
1630         struct nfs_server *server = data;
1631         struct nfs_server *old = NFS_SB(sb);
1632
1633         if (strcmp(server->hostname, old->hostname) != 0)
1634                 return 0;
1635         if (strcmp(server->mnt_path, old->mnt_path) != 0)
1636                 return 0;
1637         return 1;
1638 }
1639
1640 static void *
1641 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1642 {
1643         void *p = NULL;
1644
1645         if (!src->len)
1646                 return ERR_PTR(-EINVAL);
1647         if (src->len < maxlen)
1648                 maxlen = src->len;
1649         if (dst == NULL) {
1650                 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1651                 if (p == NULL)
1652                         return ERR_PTR(-ENOMEM);
1653         }
1654         if (copy_from_user(dst, src->data, maxlen)) {
1655                 if (p != NULL)
1656                         kfree(p);
1657                 return ERR_PTR(-EFAULT);
1658         }
1659         dst[maxlen] = '\0';
1660         return dst;
1661 }
1662
1663 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1664         int flags, const char *dev_name, void *raw_data)
1665 {
1666         int error;
1667         struct nfs_server *server;
1668         struct super_block *s;
1669         struct nfs4_mount_data *data = raw_data;
1670         void *p;
1671
1672         if (!data) {
1673                 printk("nfs_read_super: missing data argument\n");
1674                 return ERR_PTR(-EINVAL);
1675         }
1676
1677         server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1678         if (!server)
1679                 return ERR_PTR(-ENOMEM);
1680         memset(server, 0, sizeof(struct nfs_server));
1681         /* Zero out the NFS state stuff */
1682         init_nfsv4_state(server);
1683
1684         if (data->version != NFS4_MOUNT_VERSION) {
1685                 printk("nfs warning: mount version %s than kernel\n",
1686                         data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1687         }
1688
1689         p = nfs_copy_user_string(NULL, &data->hostname, 256);
1690         if (IS_ERR(p))
1691                 goto out_err;
1692         server->hostname = p;
1693
1694         p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1695         if (IS_ERR(p))
1696                 goto out_err;
1697         server->mnt_path = p;
1698
1699         p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1700                         sizeof(server->ip_addr) - 1);
1701         if (IS_ERR(p))
1702                 goto out_err;
1703
1704         /* We now require that the mount process passes the remote address */
1705         if (data->host_addrlen != sizeof(server->addr)) {
1706                 s = ERR_PTR(-EINVAL);
1707                 goto out_free;
1708         }
1709         if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1710                 s = ERR_PTR(-EFAULT);
1711                 goto out_free;
1712         }
1713         if (server->addr.sin_family != AF_INET ||
1714             server->addr.sin_addr.s_addr == INADDR_ANY) {
1715                 printk("NFS: mount program didn't pass remote IP address!\n");
1716                 s = ERR_PTR(-EINVAL);
1717                 goto out_free;
1718         }
1719
1720         s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1721
1722         if (IS_ERR(s) || s->s_root)
1723                 goto out_free;
1724
1725         s->s_flags = flags;
1726
1727         error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1728         if (error) {
1729                 up_write(&s->s_umount);
1730                 deactivate_super(s);
1731                 return ERR_PTR(error);
1732         }
1733         s->s_flags |= MS_ACTIVE;
1734         return s;
1735 out_err:
1736         s = (struct super_block *)p;
1737 out_free:
1738         if (server->mnt_path)
1739                 kfree(server->mnt_path);
1740         if (server->hostname)
1741                 kfree(server->hostname);
1742         kfree(server);
1743         return s;
1744 }
1745
1746 static struct file_system_type nfs4_fs_type = {
1747         .owner          = THIS_MODULE,
1748         .name           = "nfs4",
1749         .get_sb         = nfs4_get_sb,
1750         .kill_sb        = nfs_kill_super,
1751         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1752 };
1753
1754 #define nfs4_zero_state(nfsi) \
1755         do { \
1756                 INIT_LIST_HEAD(&(nfsi)->open_states); \
1757         } while(0)
1758 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1759 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1760 #else
1761 #define nfs4_zero_state(nfsi) \
1762         do { } while (0)
1763 #define register_nfs4fs() (0)
1764 #define unregister_nfs4fs()
1765 #endif
1766
1767 extern int nfs_init_nfspagecache(void);
1768 extern void nfs_destroy_nfspagecache(void);
1769 extern int nfs_init_readpagecache(void);
1770 extern int nfs_destroy_readpagecache(void);
1771 extern int nfs_init_writepagecache(void);
1772 extern int nfs_destroy_writepagecache(void);
1773
1774 static kmem_cache_t * nfs_inode_cachep;
1775
1776 static struct inode *nfs_alloc_inode(struct super_block *sb)
1777 {
1778         struct nfs_inode *nfsi;
1779         nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1780         if (!nfsi)
1781                 return NULL;
1782         nfsi->flags = 0;
1783         nfsi->mm_cred = NULL;
1784         nfs4_zero_state(nfsi);
1785         return &nfsi->vfs_inode;
1786 }
1787
1788 static void nfs_destroy_inode(struct inode *inode)
1789 {
1790         kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1791 }
1792
1793 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1794 {
1795         struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1796
1797         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1798             SLAB_CTOR_CONSTRUCTOR) {
1799                 inode_init_once(&nfsi->vfs_inode);
1800                 INIT_LIST_HEAD(&nfsi->dirty);
1801                 INIT_LIST_HEAD(&nfsi->commit);
1802                 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1803                 atomic_set(&nfsi->data_updates, 0);
1804                 nfsi->ndirty = 0;
1805                 nfsi->ncommit = 0;
1806                 nfsi->npages = 0;
1807                 init_waitqueue_head(&nfsi->nfs_i_wait);
1808         }
1809 }
1810  
1811 int nfs_init_inodecache(void)
1812 {
1813         nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1814                                              sizeof(struct nfs_inode),
1815                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1816                                              init_once, NULL);
1817         if (nfs_inode_cachep == NULL)
1818                 return -ENOMEM;
1819
1820         return 0;
1821 }
1822
1823 void nfs_destroy_inodecache(void)
1824 {
1825         if (kmem_cache_destroy(nfs_inode_cachep))
1826                 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1827 }
1828
1829 /*
1830  * Initialize NFS
1831  */
1832 static int __init init_nfs_fs(void)
1833 {
1834         int err;
1835
1836         err = nfs_init_nfspagecache();
1837         if (err)
1838                 goto out4;
1839
1840         err = nfs_init_inodecache();
1841         if (err)
1842                 goto out3;
1843
1844         err = nfs_init_readpagecache();
1845         if (err)
1846                 goto out2;
1847
1848         err = nfs_init_writepagecache();
1849         if (err)
1850                 goto out1;
1851
1852 #ifdef CONFIG_PROC_FS
1853         rpc_proc_register(&nfs_rpcstat);
1854 #endif
1855         err = register_filesystem(&nfs_fs_type);
1856         if (err)
1857                 goto out;
1858         if ((err = register_nfs4fs()) != 0)
1859                 goto out;
1860         return 0;
1861 out:
1862         rpc_proc_unregister("nfs");
1863         nfs_destroy_writepagecache();
1864 out1:
1865         nfs_destroy_readpagecache();
1866 out2:
1867         nfs_destroy_inodecache();
1868 out3:
1869         nfs_destroy_nfspagecache();
1870 out4:
1871         return err;
1872 }
1873
1874 static void __exit exit_nfs_fs(void)
1875 {
1876         nfs_destroy_writepagecache();
1877         nfs_destroy_readpagecache();
1878         nfs_destroy_inodecache();
1879         nfs_destroy_nfspagecache();
1880 #ifdef CONFIG_PROC_FS
1881         rpc_proc_unregister("nfs");
1882 #endif
1883         unregister_filesystem(&nfs_fs_type);
1884         unregister_nfs4fs();
1885 }
1886
1887 /* Not quite true; I just maintain it */
1888 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1889 MODULE_LICENSE("GPL");
1890
1891 module_init(init_nfs_fs)
1892 module_exit(exit_nfs_fs)