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