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