patch-2_6_7-vs1_9_1_12
[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 //      DLIMIT_FREE_INODE(inode->i_sb, inode->i_xid);
135         clear_inode(inode);
136 }
137
138 /*
139  * For the moment, the only task for the NFS clear_inode method is to
140  * release the mmap credential
141  */
142 static void
143 nfs_clear_inode(struct inode *inode)
144 {
145         struct nfs_inode *nfsi = NFS_I(inode);
146         struct rpc_cred *cred = nfsi->mm_cred;
147
148         if (cred)
149                 put_rpccred(cred);
150         cred = nfsi->cache_access.cred;
151         if (cred)
152                 put_rpccred(cred);
153         BUG_ON(atomic_read(&nfsi->data_updates) != 0);
154 }
155
156 void
157 nfs_put_super(struct super_block *sb)
158 {
159         struct nfs_server *server = NFS_SB(sb);
160
161         nfs4_renewd_prepare_shutdown(server);
162
163         if (server->client != NULL)
164                 rpc_shutdown_client(server->client);
165         if (server->client_sys != NULL)
166                 rpc_shutdown_client(server->client_sys);
167
168         if (!(server->flags & NFS_MOUNT_NONLM))
169                 lockd_down();   /* release rpc.lockd */
170         rpciod_down();          /* release rpciod */
171
172         destroy_nfsv4_state(server);
173
174         kfree(server->hostname);
175 }
176
177 void
178 nfs_umount_begin(struct super_block *sb)
179 {
180         struct nfs_server *server = NFS_SB(sb);
181         struct rpc_clnt *rpc;
182
183         /* -EIO all pending I/O */
184         if ((rpc = server->client) != NULL)
185                 rpc_killall_tasks(rpc);
186 }
187
188
189 static inline unsigned long
190 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
191 {
192         /* make sure blocksize is a power of two */
193         if ((bsize & (bsize - 1)) || nrbitsp) {
194                 unsigned char   nrbits;
195
196                 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
197                         ;
198                 bsize = 1 << nrbits;
199                 if (nrbitsp)
200                         *nrbitsp = nrbits;
201         }
202
203         return bsize;
204 }
205
206 /*
207  * Calculate the number of 512byte blocks used.
208  */
209 static inline unsigned long
210 nfs_calc_block_size(u64 tsize)
211 {
212         loff_t used = (tsize + 511) >> 9;
213         return (used > ULONG_MAX) ? ULONG_MAX : used;
214 }
215
216 /*
217  * Compute and set NFS server blocksize
218  */
219 static inline unsigned long
220 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
221 {
222         if (bsize < 1024)
223                 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
224         else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
225                 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
226
227         return nfs_block_bits(bsize, nrbitsp);
228 }
229
230 /*
231  * Obtain the root inode of the file system.
232  */
233 static struct inode *
234 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
235 {
236         struct nfs_server       *server = NFS_SB(sb);
237         struct inode *rooti;
238         int                     error;
239
240         error = server->rpc_ops->getroot(server, rootfh, fsinfo);
241         if (error < 0) {
242                 dprintk("nfs_get_root: getattr error = %d\n", -error);
243                 return ERR_PTR(error);
244         }
245
246         rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
247         if (!rooti)
248                 return ERR_PTR(-ENOMEM);
249         return rooti;
250 }
251
252 /*
253  * Do NFS version-independent mount processing, and sanity checking
254  */
255 static int
256 nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
257 {
258         struct nfs_server       *server;
259         struct inode            *root_inode;
260         struct nfs_fattr        fattr;
261         struct nfs_fsinfo       fsinfo = {
262                                         .fattr = &fattr,
263                                 };
264         struct nfs_pathconf pathinfo = {
265                         .fattr = &fattr,
266         };
267         int no_root_error = 0;
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         root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
277         /* Did getting the root inode fail? */
278         if (IS_ERR(root_inode)) {
279                 no_root_error = PTR_ERR(root_inode);
280                 goto out_no_root;
281         }
282         sb->s_root = d_alloc_root(root_inode);
283         if (!sb->s_root) {
284                 no_root_error = -ENOMEM;
285                 goto out_no_root;
286         }
287         sb->s_root->d_op = server->rpc_ops->dentry_ops;
288
289         /* Get some general file system info */
290         if (server->namelen == 0 &&
291             server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
292                 server->namelen = pathinfo.max_namelen;
293         /* Work out a lot of parameters */
294         if (server->rsize == 0)
295                 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
296         if (server->wsize == 0)
297                 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
298         if (sb->s_blocksize == 0) {
299                 if (fsinfo.wtmult == 0) {
300                         sb->s_blocksize = 512;
301                         sb->s_blocksize_bits = 9;
302                 } else
303                         sb->s_blocksize = nfs_block_bits(fsinfo.wtmult,
304                                                          &sb->s_blocksize_bits);
305         }
306
307         if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
308                 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
309         if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
310                 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
311
312         server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
313         if (server->rpages > NFS_READ_MAXIOV) {
314                 server->rpages = NFS_READ_MAXIOV;
315                 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
316         }
317
318         server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
319         if (server->wpages > NFS_WRITE_MAXIOV) {
320                 server->wpages = NFS_WRITE_MAXIOV;
321                 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
322         }
323
324         server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
325         if (server->dtsize > PAGE_CACHE_SIZE)
326                 server->dtsize = PAGE_CACHE_SIZE;
327         if (server->dtsize > server->rsize)
328                 server->dtsize = server->rsize;
329
330         if (server->flags & NFS_MOUNT_NOAC) {
331                 server->acregmin = server->acregmax = 0;
332                 server->acdirmin = server->acdirmax = 0;
333                 sb->s_flags |= MS_SYNCHRONOUS;
334         }
335         server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
336
337         if (server->flags & NFS_MOUNT_TAGXID)
338                 sb->s_flags |= MS_TAGXID;
339
340         sb->s_maxbytes = fsinfo.maxfilesize;
341         if (sb->s_maxbytes > MAX_LFS_FILESIZE) 
342                 sb->s_maxbytes = MAX_LFS_FILESIZE; 
343
344         /* We're airborne Set socket buffersize */
345         rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
346         return 0;
347         /* Yargs. It didn't work out. */
348 out_no_root:
349         dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
350         if (!IS_ERR(root_inode))
351                 iput(root_inode);
352         return no_root_error;
353 }
354
355 /*
356  * Create an RPC client handle.
357  */
358 static struct rpc_clnt *
359 nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
360 {
361         struct rpc_timeout      timeparms;
362         struct rpc_xprt         *xprt = NULL;
363         struct rpc_clnt         *clnt = NULL;
364         int                     tcp   = (data->flags & NFS_MOUNT_TCP);
365
366         /* Initialize timeout values */
367         timeparms.to_initval = data->timeo * HZ / 10;
368         timeparms.to_retries = data->retrans;
369         timeparms.to_maxval  = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
370         timeparms.to_exponential = 1;
371
372         if (!timeparms.to_initval)
373                 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
374         if (!timeparms.to_retries)
375                 timeparms.to_retries = 5;
376
377         /* create transport and client */
378         xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
379                                  &server->addr, &timeparms);
380         if (IS_ERR(xprt)) {
381                 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
382                 return (struct rpc_clnt *)xprt;
383         }
384         clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
385                                  server->rpc_ops->version, data->pseudoflavor);
386         if (IS_ERR(clnt)) {
387                 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
388                 goto out_fail;
389         }
390
391         clnt->cl_intr     = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
392         clnt->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
393         clnt->cl_droppriv = (server->flags & NFS_MOUNT_BROKEN_SUID) ? 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 (server->client == NULL)
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 /*              if (DLIMIT_ALLOC_INODE(sb, inode->i_xid)) {
704                         err = -ENOSPC;
705                         goto fail_dlim;
706                 }
707 */
708                 /* We set i_ino for the few things that still rely on it,
709                  * such as stat(2) */
710                 inode->i_ino = hash;
711
712                 /* We can't support update_atime(), since the server will reset it */
713                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
714                 inode->i_mode = fattr->mode;
715                 /* Why so? Because we want revalidate for devices/FIFOs, and
716                  * that's precisely what we have in nfs_file_inode_operations.
717                  */
718                 inode->i_op = &nfs_file_inode_operations;
719                 if (S_ISREG(inode->i_mode)) {
720                         inode->i_fop = &nfs_file_operations;
721                         inode->i_data.a_ops = &nfs_file_aops;
722                         inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
723                 } else if (S_ISDIR(inode->i_mode)) {
724                         inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
725                         inode->i_fop = &nfs_dir_operations;
726                         if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
727                             && fattr->size <= NFS_LIMIT_READDIRPLUS)
728                                 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
729                 } else if (S_ISLNK(inode->i_mode))
730                         inode->i_op = &nfs_symlink_inode_operations;
731                 else
732                         init_special_inode(inode, inode->i_mode, fattr->rdev);
733
734                 nfsi->read_cache_jiffies = fattr->timestamp;
735                 inode->i_atime = fattr->atime;
736                 inode->i_mtime = fattr->mtime;
737                 inode->i_ctime = fattr->ctime;
738                 if (fattr->valid & NFS_ATTR_FATTR_V4)
739                         nfsi->change_attr = fattr->change_attr;
740                 inode->i_size = nfs_size_to_loff_t(fattr->size);
741                 inode->i_nlink = fattr->nlink;
742                 inode->i_uid = INOXID_UID(fattr->uid, fattr->gid);
743                 inode->i_gid = INOXID_GID(fattr->uid, fattr->gid);
744                 if (inode->i_sb->s_flags & MS_TAGXID)
745                         inode->i_xid = INOXID_XID(fattr->uid, fattr->gid, 0);
746                                          /* maybe fattr->xid someday */
747                 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
748                         /*
749                          * report the blocks in 512byte units
750                          */
751                         inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
752                         inode->i_blksize = inode->i_sb->s_blocksize;
753                 } else {
754                         inode->i_blocks = fattr->du.nfs2.blocks;
755                         inode->i_blksize = fattr->du.nfs2.blocksize;
756                 }
757                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
758                 nfsi->attrtimeo_timestamp = jiffies;
759                 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
760                 nfsi->cache_access.cred = NULL;
761
762                 unlock_new_inode(inode);
763         } else
764                 nfs_refresh_inode(inode, fattr);
765         dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
766                 inode->i_sb->s_id,
767                 (long long)NFS_FILEID(inode),
768                 atomic_read(&inode->i_count));
769
770 out:
771         return inode;
772 /*
773 fail_dlim:
774         make_bad_inode(inode);
775         iput(inode);
776         inode = NULL;
777 */
778 out_no_inode:
779         printk("nfs_fhget: iget failed\n");
780         goto out;
781 }
782
783 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_XID|ATTR_SIZE|\
784                          ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
785
786 int
787 nfs_setattr(struct dentry *dentry, struct iattr *attr)
788 {
789         struct inode *inode = dentry->d_inode;
790         struct nfs_fattr fattr;
791         int error;
792
793         if (attr->ia_valid & ATTR_SIZE) {
794                 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
795                         attr->ia_valid &= ~ATTR_SIZE;
796         }
797
798         /* Optimization: if the end result is no change, don't RPC */
799         attr->ia_valid &= NFS_VALID_ATTRS;
800         if (attr->ia_valid == 0)
801                 return 0;
802
803         lock_kernel();
804         nfs_begin_data_update(inode);
805         /* Write all dirty data if we're changing file permissions or size */
806         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
807                 if (filemap_fdatawrite(inode->i_mapping) == 0)
808                         filemap_fdatawait(inode->i_mapping);
809                 nfs_wb_all(inode);
810         }
811         error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
812         if (error == 0) {
813                 nfs_refresh_inode(inode, &fattr);
814                 if ((attr->ia_valid & ATTR_MODE) != 0) {
815                         int mode;
816                         mode = inode->i_mode & ~S_IALLUGO;
817                         mode |= attr->ia_mode & S_IALLUGO;
818                         inode->i_mode = mode;
819                 }
820                 if ((attr->ia_valid & ATTR_UID) != 0)
821                         inode->i_uid = attr->ia_uid;
822                 if ((attr->ia_valid & ATTR_GID) != 0)
823                         inode->i_gid = attr->ia_gid;
824                 if ((attr->ia_valid & ATTR_XID) != 0)
825                         inode->i_xid = attr->ia_xid;
826                 if ((attr->ia_valid & ATTR_SIZE) != 0) {
827                         inode->i_size = attr->ia_size;
828                         vmtruncate(inode, attr->ia_size);
829                 }
830         }
831         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
832                 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
833                 if (*cred) {
834                         put_rpccred(*cred);
835                         *cred = NULL;
836                 }
837         }
838         nfs_end_data_update(inode);
839         unlock_kernel();
840         return error;
841 }
842
843 /*
844  * Wait for the inode to get unlocked.
845  * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
846  */
847 int
848 nfs_wait_on_inode(struct inode *inode, int flag)
849 {
850         struct rpc_clnt *clnt = NFS_CLIENT(inode);
851         struct nfs_inode *nfsi = NFS_I(inode);
852
853         int error;
854         if (!(NFS_FLAGS(inode) & flag))
855                 return 0;
856         atomic_inc(&inode->i_count);
857         error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
858                                 !(NFS_FLAGS(inode) & flag));
859         iput(inode);
860         return error;
861 }
862
863 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
864 {
865         struct inode *inode = dentry->d_inode;
866         struct nfs_inode *nfsi = NFS_I(inode);
867         int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
868         int err;
869
870         if (__IS_FLG(inode, MS_NOATIME))
871                 need_atime = 0;
872         else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
873                 need_atime = 0;
874         /* We may force a getattr if the user cares about atime */
875         if (need_atime)
876                 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
877         else
878                 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
879         if (!err)
880                 generic_fillattr(inode, stat);
881         return err;
882 }
883
884 /*
885  * Ensure that mmap has a recent RPC credential for use when writing out
886  * shared pages
887  */
888 void
889 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
890 {
891         struct rpc_cred **p = &NFS_I(inode)->mm_cred,
892                         *oldcred = *p;
893
894         *p = get_rpccred(cred);
895         if (oldcred)
896                 put_rpccred(oldcred);
897 }
898
899 /*
900  * These are probably going to contain hooks for
901  * allocating and releasing RPC credentials for
902  * the file. I'll have to think about Tronds patch
903  * a bit more..
904  */
905 int nfs_open(struct inode *inode, struct file *filp)
906 {
907         struct rpc_auth *auth;
908         struct rpc_cred *cred;
909
910         auth = NFS_CLIENT(inode)->cl_auth;
911         cred = rpcauth_lookupcred(auth, 0);
912         filp->private_data = cred;
913         if ((filp->f_mode & FMODE_WRITE) != 0) {
914                 nfs_set_mmcred(inode, cred);
915                 nfs_begin_data_update(inode);
916         }
917         return 0;
918 }
919
920 int nfs_release(struct inode *inode, struct file *filp)
921 {
922         struct rpc_cred *cred;
923
924         lock_kernel();
925         if ((filp->f_mode & FMODE_WRITE) != 0)
926                 nfs_end_data_update(inode);
927         cred = nfs_file_cred(filp);
928         if (cred)
929                 put_rpccred(cred);
930         unlock_kernel();
931         return 0;
932 }
933
934 /*
935  * This function is called whenever some part of NFS notices that
936  * the cached attributes have to be refreshed.
937  */
938 int
939 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
940 {
941         int              status = -ESTALE;
942         struct nfs_fattr fattr;
943         struct nfs_inode *nfsi = NFS_I(inode);
944         unsigned long verifier;
945         unsigned int flags;
946
947         dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
948                 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
949
950         lock_kernel();
951         if (!inode || is_bad_inode(inode))
952                 goto out_nowait;
953         if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
954                 goto out_nowait;
955
956         while (NFS_REVALIDATING(inode)) {
957                 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
958                 if (status < 0)
959                         goto out_nowait;
960                 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOAC)
961                         continue;
962                 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
963                         continue;
964                 status = NFS_STALE(inode) ? -ESTALE : 0;
965                 goto out_nowait;
966         }
967         NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
968
969         /* Protect against RPC races by saving the change attribute */
970         verifier = nfs_save_change_attribute(inode);
971         status = NFS_PROTO(inode)->getattr(inode, &fattr);
972         if (status) {
973                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
974                          inode->i_sb->s_id,
975                          (long long)NFS_FILEID(inode), status);
976                 if (status == -ESTALE) {
977                         NFS_FLAGS(inode) |= NFS_INO_STALE;
978                         if (inode != inode->i_sb->s_root->d_inode)
979                                 remove_inode_hash(inode);
980                 }
981                 goto out;
982         }
983
984         status = nfs_update_inode(inode, &fattr, verifier);
985         if (status) {
986                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
987                          inode->i_sb->s_id,
988                          (long long)NFS_FILEID(inode), status);
989                 goto out;
990         }
991         flags = nfsi->flags;
992         /*
993          * We may need to keep the attributes marked as invalid if
994          * we raced with nfs_end_attr_update().
995          */
996         if (verifier == nfsi->cache_change_attribute)
997                 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
998         /* Do the page cache invalidation */
999         if (flags & NFS_INO_INVALID_DATA) {
1000                 if (S_ISREG(inode->i_mode)) {
1001                         if (filemap_fdatawrite(inode->i_mapping) == 0)
1002                                 filemap_fdatawait(inode->i_mapping);
1003                         nfs_wb_all(inode);
1004                 }
1005                 nfsi->flags &= ~NFS_INO_INVALID_DATA;
1006                 invalidate_inode_pages2(inode->i_mapping);
1007                 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1008                 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1009                                 inode->i_sb->s_id,
1010                                 (long long)NFS_FILEID(inode));
1011                 /* This ensures we revalidate dentries */
1012                 nfsi->cache_change_attribute++;
1013         }
1014         dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1015                 inode->i_sb->s_id,
1016                 (long long)NFS_FILEID(inode));
1017
1018         NFS_FLAGS(inode) &= ~NFS_INO_STALE;
1019 out:
1020         NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1021         wake_up(&nfsi->nfs_i_wait);
1022  out_nowait:
1023         unlock_kernel();
1024         return status;
1025 }
1026
1027 /**
1028  * nfs_begin_data_update
1029  * @inode - pointer to inode
1030  * Declare that a set of operations will update file data on the server
1031  */
1032 void nfs_begin_data_update(struct inode *inode)
1033 {
1034         atomic_inc(&NFS_I(inode)->data_updates);
1035 }
1036
1037 /**
1038  * nfs_end_data_update
1039  * @inode - pointer to inode
1040  * Declare end of the operations that will update file data
1041  * This will mark the inode as immediately needing revalidation
1042  * of its attribute cache.
1043  */
1044 void nfs_end_data_update(struct inode *inode)
1045 {
1046         struct nfs_inode *nfsi = NFS_I(inode);
1047
1048         /* Mark the attribute cache for revalidation */
1049         nfsi->flags |= NFS_INO_INVALID_ATTR;
1050         /* Directories and symlinks: invalidate page cache too */
1051         if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1052                 nfsi->flags |= NFS_INO_INVALID_DATA;
1053         nfsi->cache_change_attribute ++;
1054         atomic_dec(&nfsi->data_updates);
1055 }
1056
1057 /**
1058  * nfs_end_data_update_defer
1059  * @inode - pointer to inode
1060  * Declare end of the operations that will update file data
1061  * This will defer marking the inode as needing revalidation
1062  * unless there are no other pending updates.
1063  */
1064 void nfs_end_data_update_defer(struct inode *inode)
1065 {
1066         struct nfs_inode *nfsi = NFS_I(inode);
1067
1068         if (atomic_dec_and_test(&nfsi->data_updates)) {
1069                 /* Mark the attribute cache for revalidation */
1070                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1071                 /* Directories and symlinks: invalidate page cache too */
1072                 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1073                         nfsi->flags |= NFS_INO_INVALID_DATA;
1074                 nfsi->cache_change_attribute ++;
1075         }
1076 }
1077
1078 /**
1079  * nfs_refresh_inode - verify consistency of the inode attribute cache
1080  * @inode - pointer to inode
1081  * @fattr - updated attributes
1082  *
1083  * Verifies the attribute cache. If we have just changed the attributes,
1084  * so that fattr carries weak cache consistency data, then it may
1085  * also update the ctime/mtime/change_attribute.
1086  */
1087 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1088 {
1089         struct nfs_inode *nfsi = NFS_I(inode);
1090         loff_t cur_size, new_isize;
1091         int data_unstable;
1092         uid_t uid;
1093         gid_t gid;
1094         xid_t xid = 0;
1095
1096         /* Are we in the process of updating data on the server? */
1097         data_unstable = nfs_caches_unstable(inode);
1098
1099         if (fattr->valid & NFS_ATTR_FATTR_V4) {
1100                 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1101                                 && nfsi->change_attr == fattr->pre_change_attr)
1102                         nfsi->change_attr = fattr->change_attr;
1103                 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1104                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1105         }
1106
1107         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1108                 return 0;
1109
1110         /* Has the inode gone and changed behind our back? */
1111         if (nfsi->fileid != fattr->fileid
1112                         || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1113                 return -EIO;
1114
1115         cur_size = i_size_read(inode);
1116         new_isize = nfs_size_to_loff_t(fattr->size);
1117
1118         /* If we have atomic WCC data, we may update some attributes */
1119         if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1120                 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1121                         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1122                 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1123                         memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1124         }
1125
1126         /* Verify a few of the more important attributes */
1127         if (!data_unstable) {
1128                 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1129                                 || cur_size != new_isize)
1130                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1131         } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1132                         nfsi->flags |= NFS_INO_INVALID_ATTR;
1133
1134         uid = INOXID_UID(fattr->uid, fattr->gid);
1135         gid = INOXID_GID(fattr->uid, fattr->gid);
1136         if (inode->i_sb->s_flags & MS_TAGXID)
1137                 xid = INOXID_XID(fattr->uid, fattr->gid, 0);
1138
1139         /* Have any file permissions changed? */
1140         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1141                         || inode->i_uid != uid
1142                         || inode->i_gid != gid
1143                         || inode->i_xid != xid)
1144                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1145
1146         /* Has the link count changed? */
1147         if (inode->i_nlink != fattr->nlink)
1148                 nfsi->flags |= NFS_INO_INVALID_ATTR;
1149
1150         if (!timespec_equal(&inode->i_atime, &fattr->atime))
1151                 nfsi->flags |= NFS_INO_INVALID_ATIME;
1152
1153         nfsi->read_cache_jiffies = fattr->timestamp;
1154         return 0;
1155 }
1156
1157 /*
1158  * Many nfs protocol calls return the new file attributes after
1159  * an operation.  Here we update the inode to reflect the state
1160  * of the server's inode.
1161  *
1162  * This is a bit tricky because we have to make sure all dirty pages
1163  * have been sent off to the server before calling invalidate_inode_pages.
1164  * To make sure no other process adds more write requests while we try
1165  * our best to flush them, we make them sleep during the attribute refresh.
1166  *
1167  * A very similar scenario holds for the dir cache.
1168  */
1169 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1170 {
1171         struct nfs_inode *nfsi = NFS_I(inode);
1172         __u64           new_size;
1173         loff_t          new_isize;
1174         unsigned int    invalid = 0;
1175         loff_t          cur_isize;
1176         int data_unstable;
1177         uid_t           uid;
1178         gid_t           gid;
1179         xid_t           xid = 0;
1180
1181         dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1182                         __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1183                         atomic_read(&inode->i_count), fattr->valid);
1184
1185         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1186                 return 0;
1187
1188         if (nfsi->fileid != fattr->fileid) {
1189                 printk(KERN_ERR "%s: inode number mismatch\n"
1190                        "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1191                        __FUNCTION__,
1192                        inode->i_sb->s_id, (long long)nfsi->fileid,
1193                        inode->i_sb->s_id, (long long)fattr->fileid);
1194                 goto out_err;
1195         }
1196
1197         /*
1198          * Make sure the inode's type hasn't changed.
1199          */
1200         if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1201                 goto out_changed;
1202
1203         /*
1204          * Update the read time so we don't revalidate too often.
1205          */
1206         nfsi->read_cache_jiffies = fattr->timestamp;
1207
1208         /* Are we racing with known updates of the metadata on the server? */
1209         data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1210
1211         /* Check if the file size agrees */
1212         new_size = fattr->size;
1213         new_isize = nfs_size_to_loff_t(fattr->size);
1214         cur_isize = i_size_read(inode);
1215         if (cur_isize != new_size) {
1216 #ifdef NFS_DEBUG_VERBOSE
1217                 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1218 #endif
1219                 /*
1220                  * If we have pending writebacks, things can get
1221                  * messy.
1222                  */
1223                 if (S_ISREG(inode->i_mode) && data_unstable) {
1224                         if (new_isize > cur_isize) {
1225                                 inode->i_size = new_isize;
1226                                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1227                         }
1228                 } else {
1229                         inode->i_size = new_isize;
1230                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1231                 }
1232         }
1233
1234         /*
1235          * Note: we don't check inode->i_mtime since pipes etc.
1236          *       can change this value in VFS without requiring a
1237          *       cache revalidation.
1238          */
1239         if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1240                 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1241 #ifdef NFS_DEBUG_VERBOSE
1242                 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1243 #endif
1244                 if (!data_unstable)
1245                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1246         }
1247
1248         if ((fattr->valid & NFS_ATTR_FATTR_V4)
1249             && nfsi->change_attr != fattr->change_attr) {
1250 #ifdef NFS_DEBUG_VERBOSE
1251                 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1252                        inode->i_sb->s_id, inode->i_ino);
1253 #endif
1254                 nfsi->change_attr = fattr->change_attr;
1255                 if (!data_unstable)
1256                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1257         }
1258
1259         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1260         memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1261
1262         uid = INOXID_UID(fattr->uid, fattr->gid);
1263         gid = INOXID_GID(fattr->uid, fattr->gid);
1264         if (inode->i_sb->s_flags & MS_TAGXID)
1265                 xid = INOXID_XID(fattr->uid, fattr->gid, 0);
1266
1267         if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1268             inode->i_uid != uid ||
1269             inode->i_gid != gid ||
1270             inode->i_xid != xid) {
1271                 struct rpc_cred **cred = &NFS_I(inode)->cache_access.cred;
1272                 if (*cred) {
1273                         put_rpccred(*cred);
1274                         *cred = NULL;
1275                 }
1276                 invalid |= NFS_INO_INVALID_ATTR;
1277         }
1278
1279         inode->i_mode = fattr->mode;
1280         inode->i_nlink = fattr->nlink;
1281         inode->i_uid = uid;
1282         inode->i_gid = gid;
1283         inode->i_xid = xid;
1284
1285         if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1286                 /*
1287                  * report the blocks in 512byte units
1288                  */
1289                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1290                 inode->i_blksize = inode->i_sb->s_blocksize;
1291         } else {
1292                 inode->i_blocks = fattr->du.nfs2.blocks;
1293                 inode->i_blksize = fattr->du.nfs2.blocksize;
1294         }
1295
1296         /* Update attrtimeo value if we're out of the unstable period */
1297         if (invalid & NFS_INO_INVALID_ATTR) {
1298                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1299                 nfsi->attrtimeo_timestamp = jiffies;
1300         } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1301                 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1302                         nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1303                 nfsi->attrtimeo_timestamp = jiffies;
1304         }
1305         /* Don't invalidate the data if we were to blame */
1306         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1307                                 || S_ISLNK(inode->i_mode)))
1308                 invalid &= ~NFS_INO_INVALID_DATA;
1309         nfsi->flags |= invalid;
1310
1311         return 0;
1312  out_changed:
1313         /*
1314          * Big trouble! The inode has become a different object.
1315          */
1316 #ifdef NFS_PARANOIA
1317         printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1318                         __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1319 #endif
1320         /*
1321          * No need to worry about unhashing the dentry, as the
1322          * lookup validation will know that the inode is bad.
1323          * (But we fall through to invalidate the caches.)
1324          */
1325         nfs_invalidate_inode(inode);
1326  out_err:
1327         return -EIO;
1328 }
1329
1330 /*
1331  * File system information
1332  */
1333
1334 static int nfs_set_super(struct super_block *s, void *data)
1335 {
1336         s->s_fs_info = data;
1337         return set_anon_super(s, data);
1338 }
1339  
1340 static int nfs_compare_super(struct super_block *sb, void *data)
1341 {
1342         struct nfs_server *server = data;
1343         struct nfs_server *old = NFS_SB(sb);
1344
1345         if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1346                 return 0;
1347         if (old->addr.sin_port != server->addr.sin_port)
1348                 return 0;
1349         return !memcmp(&old->fh, &server->fh, sizeof(struct nfs_fh));
1350 }
1351
1352 static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1353         int flags, const char *dev_name, void *raw_data)
1354 {
1355         int error;
1356         struct nfs_server *server;
1357         struct super_block *s;
1358         struct nfs_fh *root;
1359         struct nfs_mount_data *data = raw_data;
1360
1361         if (!data) {
1362                 printk("nfs_read_super: missing data argument\n");
1363                 return ERR_PTR(-EINVAL);
1364         }
1365
1366         server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1367         if (!server)
1368                 return ERR_PTR(-ENOMEM);
1369         memset(server, 0, sizeof(struct nfs_server));
1370         /* Zero out the NFS state stuff */
1371         init_nfsv4_state(server);
1372
1373         root = &server->fh;
1374         memcpy(root, &data->root, sizeof(*root));
1375         if (root->size < sizeof(root->data))
1376                 memset(root->data+root->size, 0, sizeof(root->data)-root->size);
1377
1378         if (data->version != NFS_MOUNT_VERSION) {
1379                 printk("nfs warning: mount version %s than kernel\n",
1380                         data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1381                 if (data->version < 2)
1382                         data->namlen = 0;
1383                 if (data->version < 3)
1384                         data->bsize  = 0;
1385                 if (data->version < 4) {
1386                         data->flags &= ~NFS_MOUNT_VER3;
1387                         memset(root, 0, sizeof(*root));
1388                         root->size = NFS2_FHSIZE;
1389                         memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
1390                 }
1391                 if (data->version < 5)
1392                         data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1393         }
1394
1395         if (root->size > sizeof(root->data)) {
1396                 printk("nfs_get_sb: invalid root filehandle\n");
1397                 kfree(server);
1398                 return ERR_PTR(-EINVAL);
1399         }
1400         /* We now require that the mount process passes the remote address */
1401         memcpy(&server->addr, &data->addr, sizeof(server->addr));
1402         if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1403                 printk("NFS: mount program didn't pass remote address!\n");
1404                 kfree(server);
1405                 return ERR_PTR(-EINVAL);
1406         }
1407
1408         s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1409
1410         if (IS_ERR(s) || s->s_root) {
1411                 kfree(server);
1412                 return s;
1413         }
1414
1415         s->s_flags = flags;
1416
1417         error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1418         if (error) {
1419                 up_write(&s->s_umount);
1420                 deactivate_super(s);
1421                 return ERR_PTR(error);
1422         }
1423         s->s_flags |= MS_ACTIVE;
1424         return s;
1425 }
1426
1427 static void nfs_kill_super(struct super_block *s)
1428 {
1429         struct nfs_server *server = NFS_SB(s);
1430         kill_anon_super(s);
1431         kfree(server);
1432 }
1433
1434 static struct file_system_type nfs_fs_type = {
1435         .owner          = THIS_MODULE,
1436         .name           = "nfs",
1437         .get_sb         = nfs_get_sb,
1438         .kill_sb        = nfs_kill_super,
1439         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1440 };
1441
1442 #ifdef CONFIG_NFS_V4
1443
1444 static void nfs4_clear_inode(struct inode *);
1445
1446 static struct super_operations nfs4_sops = { 
1447         .alloc_inode    = nfs_alloc_inode,
1448         .destroy_inode  = nfs_destroy_inode,
1449         .write_inode    = nfs_write_inode,
1450         .delete_inode   = nfs_delete_inode,
1451         .put_super      = nfs_put_super,
1452         .statfs         = nfs_statfs,
1453         .clear_inode    = nfs4_clear_inode,
1454         .umount_begin   = nfs_umount_begin,
1455         .show_options   = nfs_show_options,
1456 };
1457
1458 /*
1459  * Clean out any remaining NFSv4 state that might be left over due
1460  * to open() calls that passed nfs_atomic_lookup, but failed to call
1461  * nfs_open().
1462  */
1463 static void nfs4_clear_inode(struct inode *inode)
1464 {
1465         struct nfs_inode *nfsi = NFS_I(inode);
1466
1467         while (!list_empty(&nfsi->open_states)) {
1468                 struct nfs4_state *state;
1469                 
1470                 state = list_entry(nfsi->open_states.next,
1471                                 struct nfs4_state,
1472                                 inode_states);
1473                 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1474                                 __FUNCTION__,
1475                                 inode->i_sb->s_id,
1476                                 (long long)NFS_FILEID(inode),
1477                                 state);
1478                 BUG_ON(atomic_read(&state->count) != 1);
1479                 nfs4_close_state(state, state->state);
1480         }
1481         /* Now call standard NFS clear_inode() code */
1482         nfs_clear_inode(inode);
1483 }
1484
1485
1486 static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1487 {
1488         struct nfs_server *server;
1489         struct nfs4_client *clp = NULL;
1490         struct rpc_xprt *xprt = NULL;
1491         struct rpc_clnt *clnt = NULL;
1492         struct rpc_timeout timeparms;
1493         rpc_authflavor_t authflavour;
1494         int proto, err = -EIO;
1495
1496         sb->s_blocksize_bits = 0;
1497         sb->s_blocksize = 0;
1498         server = NFS_SB(sb);
1499         if (data->rsize != 0)
1500                 server->rsize = nfs_block_size(data->rsize, NULL);
1501         if (data->wsize != 0)
1502                 server->wsize = nfs_block_size(data->wsize, NULL);
1503         server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1504
1505         /* NFSv4 doesn't use NLM locking */
1506         server->flags |= NFS_MOUNT_NONLM;
1507
1508         server->acregmin = data->acregmin*HZ;
1509         server->acregmax = data->acregmax*HZ;
1510         server->acdirmin = data->acdirmin*HZ;
1511         server->acdirmax = data->acdirmax*HZ;
1512
1513         server->rpc_ops = &nfs_v4_clientops;
1514         /* Initialize timeout values */
1515
1516         timeparms.to_initval = data->timeo * HZ / 10;
1517         timeparms.to_retries = data->retrans;
1518         timeparms.to_exponential = 1;
1519         if (!timeparms.to_retries)
1520                 timeparms.to_retries = 5;
1521
1522         proto = data->proto;
1523         /* Which IP protocol do we use? */
1524         switch (proto) {
1525         case IPPROTO_TCP:
1526                 timeparms.to_maxval  = RPC_MAX_TCP_TIMEOUT;
1527                 if (!timeparms.to_initval)
1528                         timeparms.to_initval = 600 * HZ / 10;
1529                 break;
1530         case IPPROTO_UDP:
1531                 timeparms.to_maxval  = RPC_MAX_UDP_TIMEOUT;
1532                 if (!timeparms.to_initval)
1533                         timeparms.to_initval = 11 * HZ / 10;
1534                 break;
1535         default:
1536                 return -EINVAL;
1537         }
1538
1539         clp = nfs4_get_client(&server->addr.sin_addr);
1540         if (!clp) {
1541                 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1542                 goto out_fail;
1543         }
1544
1545         /* Now create transport and client */
1546         authflavour = RPC_AUTH_UNIX;
1547         if (data->auth_flavourlen != 0) {
1548                 if (data->auth_flavourlen > 1)
1549                         printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1550                 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1551                         err = -EFAULT;
1552                         goto out_fail;
1553                 }
1554         }
1555
1556         down_write(&clp->cl_sem);
1557         if (clp->cl_rpcclient == NULL) {
1558                 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1559                 if (IS_ERR(xprt)) {
1560                         up_write(&clp->cl_sem);
1561                         printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1562                         err = PTR_ERR(xprt);
1563                         goto out_fail;
1564                 }
1565                 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1566                                 server->rpc_ops->version, authflavour);
1567                 if (IS_ERR(clnt)) {
1568                         up_write(&clp->cl_sem);
1569                         printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1570                         xprt_destroy(xprt);
1571                         err = PTR_ERR(clnt);
1572                         goto out_fail;
1573                 }
1574                 clnt->cl_chatty   = 1;
1575                 clp->cl_rpcclient = clnt;
1576                 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1577                 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1578                 nfs_idmap_new(clp);
1579         }
1580         if (list_empty(&clp->cl_superblocks))
1581                 clear_bit(NFS4CLNT_OK, &clp->cl_state);
1582         list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1583         clnt = rpc_clone_client(clp->cl_rpcclient);
1584         if (!IS_ERR(clnt))
1585                         server->nfs4_state = clp;
1586         up_write(&clp->cl_sem);
1587         clp = NULL;
1588
1589         if (IS_ERR(clnt)) {
1590                 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1591                 err = PTR_ERR(clnt);
1592                 goto out_remove_list;
1593         }
1594         err = -ENOMEM;
1595         if (server->nfs4_state->cl_idmap == NULL) {
1596                 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1597                 goto out_shutdown;
1598         }
1599
1600         clnt->cl_intr     = (server->flags & NFS4_MOUNT_INTR) ? 1 : 0;
1601         clnt->cl_softrtry = (server->flags & NFS4_MOUNT_SOFT) ? 1 : 0;
1602         server->client    = clnt;
1603
1604         if (clnt->cl_auth->au_flavor != authflavour) {
1605                 if (rpcauth_create(authflavour, clnt) == NULL) {
1606                         printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1607                         goto out_shutdown;
1608                 }
1609         }
1610
1611         /* Fire up rpciod if not yet running */
1612         if (rpciod_up() != 0) {
1613                 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1614                 goto out_shutdown;
1615         }
1616
1617         sb->s_op = &nfs4_sops;
1618         err = nfs_sb_init(sb, authflavour);
1619         if (err == 0)
1620                 return 0;
1621         rpciod_down();
1622 out_shutdown:
1623         rpc_shutdown_client(server->client);
1624 out_remove_list:
1625         down_write(&server->nfs4_state->cl_sem);
1626         list_del_init(&server->nfs4_siblings);
1627         up_write(&server->nfs4_state->cl_sem);
1628         destroy_nfsv4_state(server);
1629 out_fail:
1630         if (clp)
1631                 nfs4_put_client(clp);
1632         return err;
1633 }
1634
1635 static int nfs4_compare_super(struct super_block *sb, void *data)
1636 {
1637         struct nfs_server *server = data;
1638         struct nfs_server *old = NFS_SB(sb);
1639
1640         if (strcmp(server->hostname, old->hostname) != 0)
1641                 return 0;
1642         if (strcmp(server->mnt_path, old->mnt_path) != 0)
1643                 return 0;
1644         return 1;
1645 }
1646
1647 static void *
1648 nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1649 {
1650         void *p = NULL;
1651
1652         if (!src->len)
1653                 return ERR_PTR(-EINVAL);
1654         if (src->len < maxlen)
1655                 maxlen = src->len;
1656         if (dst == NULL) {
1657                 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1658                 if (p == NULL)
1659                         return ERR_PTR(-ENOMEM);
1660         }
1661         if (copy_from_user(dst, src->data, maxlen)) {
1662                 if (p != NULL)
1663                         kfree(p);
1664                 return ERR_PTR(-EFAULT);
1665         }
1666         dst[maxlen] = '\0';
1667         return dst;
1668 }
1669
1670 static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1671         int flags, const char *dev_name, void *raw_data)
1672 {
1673         int error;
1674         struct nfs_server *server;
1675         struct super_block *s;
1676         struct nfs4_mount_data *data = raw_data;
1677         void *p;
1678
1679         if (!data) {
1680                 printk("nfs_read_super: missing data argument\n");
1681                 return ERR_PTR(-EINVAL);
1682         }
1683
1684         server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1685         if (!server)
1686                 return ERR_PTR(-ENOMEM);
1687         memset(server, 0, sizeof(struct nfs_server));
1688         /* Zero out the NFS state stuff */
1689         init_nfsv4_state(server);
1690
1691         if (data->version != NFS4_MOUNT_VERSION) {
1692                 printk("nfs warning: mount version %s than kernel\n",
1693                         data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1694         }
1695
1696         p = nfs_copy_user_string(NULL, &data->hostname, 256);
1697         if (IS_ERR(p))
1698                 goto out_err;
1699         server->hostname = p;
1700
1701         p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1702         if (IS_ERR(p))
1703                 goto out_err;
1704         server->mnt_path = p;
1705
1706         p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1707                         sizeof(server->ip_addr) - 1);
1708         if (IS_ERR(p))
1709                 goto out_err;
1710
1711         /* We now require that the mount process passes the remote address */
1712         if (data->host_addrlen != sizeof(server->addr)) {
1713                 s = ERR_PTR(-EINVAL);
1714                 goto out_free;
1715         }
1716         if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1717                 s = ERR_PTR(-EFAULT);
1718                 goto out_free;
1719         }
1720         if (server->addr.sin_family != AF_INET ||
1721             server->addr.sin_addr.s_addr == INADDR_ANY) {
1722                 printk("NFS: mount program didn't pass remote IP address!\n");
1723                 s = ERR_PTR(-EINVAL);
1724                 goto out_free;
1725         }
1726
1727         s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1728
1729         if (IS_ERR(s) || s->s_root)
1730                 goto out_free;
1731
1732         s->s_flags = flags;
1733
1734         error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1735         if (error) {
1736                 up_write(&s->s_umount);
1737                 deactivate_super(s);
1738                 return ERR_PTR(error);
1739         }
1740         s->s_flags |= MS_ACTIVE;
1741         return s;
1742 out_err:
1743         s = (struct super_block *)p;
1744 out_free:
1745         if (server->mnt_path)
1746                 kfree(server->mnt_path);
1747         if (server->hostname)
1748                 kfree(server->hostname);
1749         kfree(server);
1750         return s;
1751 }
1752
1753 static struct file_system_type nfs4_fs_type = {
1754         .owner          = THIS_MODULE,
1755         .name           = "nfs4",
1756         .get_sb         = nfs4_get_sb,
1757         .kill_sb        = nfs_kill_super,
1758         .fs_flags       = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1759 };
1760
1761 #define nfs4_zero_state(nfsi) \
1762         do { \
1763                 INIT_LIST_HEAD(&(nfsi)->open_states); \
1764         } while(0)
1765 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1766 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1767 #else
1768 #define nfs4_zero_state(nfsi) \
1769         do { } while (0)
1770 #define register_nfs4fs() (0)
1771 #define unregister_nfs4fs()
1772 #endif
1773
1774 extern int nfs_init_nfspagecache(void);
1775 extern void nfs_destroy_nfspagecache(void);
1776 extern int nfs_init_readpagecache(void);
1777 extern int nfs_destroy_readpagecache(void);
1778 extern int nfs_init_writepagecache(void);
1779 extern int nfs_destroy_writepagecache(void);
1780
1781 static kmem_cache_t * nfs_inode_cachep;
1782
1783 static struct inode *nfs_alloc_inode(struct super_block *sb)
1784 {
1785         struct nfs_inode *nfsi;
1786         nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1787         if (!nfsi)
1788                 return NULL;
1789         nfsi->flags = 0;
1790         nfsi->mm_cred = NULL;
1791         nfs4_zero_state(nfsi);
1792         return &nfsi->vfs_inode;
1793 }
1794
1795 static void nfs_destroy_inode(struct inode *inode)
1796 {
1797         kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1798 }
1799
1800 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1801 {
1802         struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1803
1804         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1805             SLAB_CTOR_CONSTRUCTOR) {
1806                 inode_init_once(&nfsi->vfs_inode);
1807                 INIT_LIST_HEAD(&nfsi->dirty);
1808                 INIT_LIST_HEAD(&nfsi->commit);
1809                 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1810                 atomic_set(&nfsi->data_updates, 0);
1811                 nfsi->ndirty = 0;
1812                 nfsi->ncommit = 0;
1813                 nfsi->npages = 0;
1814                 init_waitqueue_head(&nfsi->nfs_i_wait);
1815         }
1816 }
1817  
1818 int nfs_init_inodecache(void)
1819 {
1820         nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1821                                              sizeof(struct nfs_inode),
1822                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1823                                              init_once, NULL);
1824         if (nfs_inode_cachep == NULL)
1825                 return -ENOMEM;
1826
1827         return 0;
1828 }
1829
1830 void nfs_destroy_inodecache(void)
1831 {
1832         if (kmem_cache_destroy(nfs_inode_cachep))
1833                 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1834 }
1835
1836 /*
1837  * Initialize NFS
1838  */
1839 static int __init init_nfs_fs(void)
1840 {
1841         int err;
1842
1843         err = nfs_init_nfspagecache();
1844         if (err)
1845                 goto out4;
1846
1847         err = nfs_init_inodecache();
1848         if (err)
1849                 goto out3;
1850
1851         err = nfs_init_readpagecache();
1852         if (err)
1853                 goto out2;
1854
1855         err = nfs_init_writepagecache();
1856         if (err)
1857                 goto out1;
1858
1859 #ifdef CONFIG_PROC_FS
1860         rpc_proc_register(&nfs_rpcstat);
1861 #endif
1862         err = register_filesystem(&nfs_fs_type);
1863         if (err)
1864                 goto out;
1865         if ((err = register_nfs4fs()) != 0)
1866                 goto out;
1867         return 0;
1868 out:
1869         rpc_proc_unregister("nfs");
1870         nfs_destroy_writepagecache();
1871 out1:
1872         nfs_destroy_readpagecache();
1873 out2:
1874         nfs_destroy_inodecache();
1875 out3:
1876         nfs_destroy_nfspagecache();
1877 out4:
1878         return err;
1879 }
1880
1881 static void __exit exit_nfs_fs(void)
1882 {
1883         nfs_destroy_writepagecache();
1884         nfs_destroy_readpagecache();
1885         nfs_destroy_inodecache();
1886         nfs_destroy_nfspagecache();
1887 #ifdef CONFIG_PROC_FS
1888         rpc_proc_unregister("nfs");
1889 #endif
1890         unregister_filesystem(&nfs_fs_type);
1891         unregister_nfs4fs();
1892 }
1893
1894 /* Not quite true; I just maintain it */
1895 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1896 MODULE_LICENSE("GPL");
1897
1898 module_init(init_nfs_fs)
1899 module_exit(exit_nfs_fs)