import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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/sched.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/locks.h>
27 #include <linux/unistd.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/sunrpc/stats.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_mount.h>
32 #include <linux/nfs_flushd.h>
33 #include <linux/lockd/bind.h>
34 #include <linux/smp_lock.h>
35 #include <linux/seq_file.h>
36
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39
40 #define CONFIG_NFS_SNAPSHOT 1
41 #define NFSDBG_FACILITY         NFSDBG_VFS
42 #define NFS_PARANOIA 1
43
44 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fh *, struct nfs_fattr *);
45 void nfs_zap_caches(struct inode *);
46 static void nfs_invalidate_inode(struct inode *);
47
48 static void nfs_read_inode(struct inode *);
49 static void nfs_write_inode(struct inode *,int);
50 static void nfs_delete_inode(struct inode *);
51 static void nfs_put_super(struct super_block *);
52 static void nfs_clear_inode(struct inode *);
53 static void nfs_umount_begin(struct super_block *);
54 static int  nfs_statfs(struct super_block *, struct statfs *);
55 static int  nfs_show_options(struct seq_file *, struct vfsmount *);
56
57 static struct super_operations nfs_sops = { 
58         read_inode:     nfs_read_inode,
59         write_inode:    nfs_write_inode,
60         delete_inode:   nfs_delete_inode,
61         put_super:      nfs_put_super,
62         statfs:         nfs_statfs,
63         clear_inode:    nfs_clear_inode,
64         umount_begin:   nfs_umount_begin,
65         show_options:   nfs_show_options,
66 };
67
68 /*
69  * RPC cruft for NFS
70  */
71 struct rpc_stat                 nfs_rpcstat = { &nfs_program };
72 static struct rpc_version *     nfs_version[] = {
73         NULL,
74         NULL,
75         &nfs_version2,
76 #ifdef CONFIG_NFS_V3
77         &nfs_version3,
78 #endif
79 };
80
81 struct rpc_program              nfs_program = {
82         "nfs",
83         NFS_PROGRAM,
84         sizeof(nfs_version) / sizeof(nfs_version[0]),
85         nfs_version,
86         &nfs_rpcstat,
87 };
88
89 static inline unsigned long
90 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
91 {
92         return nfs_fileid_to_ino_t(fattr->fileid);
93 }
94
95 /*
96  * The "read_inode" function doesn't actually do anything:
97  * the real data is filled in later in nfs_fhget. Here we
98  * just mark the cache times invalid, and zero out i_mode
99  * (the latter makes "nfs_refresh_inode" do the right thing
100  * wrt pipe inodes)
101  */
102 static void
103 nfs_read_inode(struct inode * inode)
104 {
105         inode->i_blksize = inode->i_sb->s_blocksize;
106         inode->i_mode = 0;
107         inode->i_rdev = 0;
108         /* We can't support UPDATE_ATIME(), since the server will reset it */
109         inode->i_flags |= S_NOATIME;
110         INIT_LIST_HEAD(&inode->u.nfs_i.read);
111         INIT_LIST_HEAD(&inode->u.nfs_i.dirty);
112         INIT_LIST_HEAD(&inode->u.nfs_i.commit);
113         INIT_LIST_HEAD(&inode->u.nfs_i.writeback);
114         NFS_CACHEINV(inode);
115         NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
116         NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
117 }
118
119 static void
120 nfs_write_inode(struct inode *inode, int sync)
121 {
122         int flags = sync ? FLUSH_WAIT : 0;
123
124         nfs_sync_file(inode, 0, 0, flags);
125 }
126
127 static void
128 nfs_delete_inode(struct inode * inode)
129 {
130         dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
131
132         /*
133          * The following can never actually happen...
134          */
135         if (nfs_have_writebacks(inode) || nfs_have_read(inode)) {
136                 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
137         }
138
139         clear_inode(inode);
140 }
141
142 /*
143  * For the moment, the only task for the NFS clear_inode method is to
144  * release the mmap credential
145  */
146 static void
147 nfs_clear_inode(struct inode *inode)
148 {
149         struct rpc_cred *cred = NFS_I(inode)->mm_cred;
150
151         if (cred)
152                 put_rpccred(cred);
153 }
154
155 void
156 nfs_put_super(struct super_block *sb)
157 {
158         struct nfs_server *server = &sb->u.nfs_sb.s_server;
159         struct rpc_clnt *rpc;
160
161         /*
162          * First get rid of the request flushing daemon.
163          * Relies on rpc_shutdown_client() waiting on all
164          * client tasks to finish.
165          */
166         nfs_reqlist_exit(server);
167
168         if ((rpc = server->client) != NULL)
169                 rpc_shutdown_client(rpc);
170
171         nfs_reqlist_free(server);
172
173         if (!(server->flags & NFS_MOUNT_NONLM))
174                 lockd_down();   /* release rpc.lockd */
175         rpciod_down();          /* release rpciod */
176
177         kfree(server->hostname);
178 }
179
180 void
181 nfs_umount_begin(struct super_block *sb)
182 {
183         struct nfs_server *server = &sb->u.nfs_sb.s_server;
184         struct rpc_clnt *rpc;
185
186         /* -EIO all pending I/O */
187         if ((rpc = server->client) != NULL)
188                 rpc_killall_tasks(rpc);
189 }
190
191
192 static inline unsigned long
193 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
194 {
195         /* make sure blocksize is a power of two */
196         if ((bsize & (bsize - 1)) || nrbitsp) {
197                 unsigned char   nrbits;
198
199                 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
200                         ;
201                 bsize = 1 << nrbits;
202                 if (nrbitsp)
203                         *nrbitsp = nrbits;
204         }
205
206         return bsize;
207 }
208
209 /*
210  * Calculate the number of 512byte blocks used.
211  */
212 static inline unsigned long
213 nfs_calc_block_size(u64 tsize)
214 {
215         loff_t used = (tsize + 511) >> 9;
216         return (used > ULONG_MAX) ? ULONG_MAX : used;
217 }
218
219 /*
220  * Compute and set NFS server blocksize
221  */
222 static inline unsigned long
223 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
224 {
225         if (bsize < 1024)
226                 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
227         else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
228                 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
229
230         return nfs_block_bits(bsize, nrbitsp);
231 }
232
233 /*
234  * Obtain the root inode of the file system.
235  */
236 static struct inode *
237 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh)
238 {
239         struct nfs_server       *server = &sb->u.nfs_sb.s_server;
240         struct nfs_fattr        fattr;
241         struct inode            *inode;
242         int                     error;
243
244         if ((error = server->rpc_ops->getroot(server, rootfh, &fattr)) < 0) {
245                 printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error);
246                 return NULL;
247         }
248
249         inode = __nfs_fhget(sb, rootfh, &fattr);
250         return inode;
251 }
252
253 /*
254  * The way this works is that the mount process passes a structure
255  * in the data argument which contains the server's IP address
256  * and the root file handle obtained from the server's mount
257  * daemon. We stash these away in the private superblock fields.
258  */
259 struct super_block *
260 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
261 {
262         struct nfs_mount_data   *data = (struct nfs_mount_data *) raw_data;
263         struct nfs_server       *server;
264         struct rpc_xprt         *xprt = NULL;
265         struct rpc_clnt         *clnt = NULL;
266         struct nfs_fh           *root = &data->root, fh;
267         struct inode            *root_inode = NULL;
268         unsigned int            authflavor;
269         struct sockaddr_in      srvaddr;
270         struct rpc_timeout      timeparms;
271         struct nfs_fsinfo       fsinfo;
272         int                     tcp, version, maxlen;
273
274         memset(&sb->u.nfs_sb, 0, sizeof(sb->u.nfs_sb));
275         if (!data)
276                 goto out_miss_args;
277
278         memset(&fh, 0, sizeof(fh));
279         if (data->version != NFS_MOUNT_VERSION) {
280                 printk("nfs warning: mount version %s than kernel\n",
281                         data->version < NFS_MOUNT_VERSION ? "older" : "newer");
282                 if (data->version < 2)
283                         data->namlen = 0;
284                 if (data->version < 3)
285                         data->bsize  = 0;
286                 if (data->version < 4) {
287                         data->flags &= ~NFS_MOUNT_VER3;
288                         root = &fh;
289                         root->size = NFS2_FHSIZE;
290                         memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
291                 }
292         }
293
294         /* We now require that the mount process passes the remote address */
295         memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
296         if (srvaddr.sin_addr.s_addr == INADDR_ANY)
297                 goto out_no_remote;
298
299         sb->s_magic      = NFS_SUPER_MAGIC;
300         sb->s_op         = &nfs_sops;
301         sb->s_blocksize_bits = 0;
302         sb->s_blocksize  = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
303         server           = &sb->u.nfs_sb.s_server;
304         server->rsize    = nfs_block_size(data->rsize, NULL);
305         server->wsize    = nfs_block_size(data->wsize, NULL);
306         server->flags    = data->flags & NFS_MOUNT_FLAGMASK;
307
308         if (data->flags & NFS_MOUNT_NOAC) {
309                 data->acregmin = data->acregmax = 0;
310                 data->acdirmin = data->acdirmax = 0;
311                 sb->s_flags |= MS_SYNCHRONOUS;
312         }
313         server->acregmin = data->acregmin*HZ;
314         server->acregmax = data->acregmax*HZ;
315         server->acdirmin = data->acdirmin*HZ;
316         server->acdirmax = data->acdirmax*HZ;
317
318         server->namelen  = data->namlen;
319         server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
320         if (!server->hostname)
321                 goto out_unlock;
322         strcpy(server->hostname, data->hostname);
323         INIT_LIST_HEAD(&server->lru_read);
324         INIT_LIST_HEAD(&server->lru_dirty);
325         INIT_LIST_HEAD(&server->lru_commit);
326         INIT_LIST_HEAD(&server->lru_busy);
327
328  nfsv3_try_again:
329         /* Check NFS protocol revision and initialize RPC op vector
330          * and file handle pool. */
331         if (data->flags & NFS_MOUNT_VER3) {
332 #ifdef CONFIG_NFS_V3
333                 server->rpc_ops = &nfs_v3_clientops;
334                 version = 3;
335                 if (data->version < 4) {
336                         printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
337                         goto out_unlock;
338                 }
339 #else
340                 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
341                 goto out_unlock;
342 #endif
343         } else {
344                 server->rpc_ops = &nfs_v2_clientops;
345                 version = 2;
346         }
347
348         /* Which protocol do we use? */
349         tcp   = (data->flags & NFS_MOUNT_TCP);
350
351         /* Initialize timeout values */
352         timeparms.to_initval = data->timeo * HZ / 10;
353         timeparms.to_retries = data->retrans;
354         timeparms.to_maxval  = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
355         timeparms.to_exponential = 1;
356
357         if (!timeparms.to_initval)
358                 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
359         if (!timeparms.to_retries)
360                 timeparms.to_retries = 5;
361
362         /* Now create transport and client */
363         xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
364                                                 &srvaddr, &timeparms);
365         if (xprt == NULL)
366                 goto out_no_xprt;
367
368         /* Choose authentication flavor */
369         authflavor = RPC_AUTH_UNIX;
370         if (data->flags & NFS_MOUNT_SECURE)
371                 authflavor = RPC_AUTH_DES;
372         else if (data->flags & NFS_MOUNT_KERBEROS)
373                 authflavor = RPC_AUTH_KRB;
374
375         clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
376                                  version, authflavor);
377         if (clnt == NULL)
378                 goto out_no_client;
379
380         clnt->cl_intr     = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
381         clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
382         clnt->cl_droppriv = (data->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
383         clnt->cl_chatty   = 1;
384         server->client    = clnt;
385
386         /* Fire up rpciod if not yet running */
387         if (rpciod_up() != 0)
388                 goto out_no_iod;
389
390         /*
391          * Keep the super block locked while we try to get 
392          * the root fh attributes.
393          */
394         /* Did getting the root inode fail? */
395         if (!(root_inode = nfs_get_root(sb, root))
396             && (data->flags & NFS_MOUNT_VER3)) {
397                 data->flags &= ~NFS_MOUNT_VER3;
398                 rpciod_down();
399                 rpc_shutdown_client(server->client);
400                 goto nfsv3_try_again;
401         }
402
403         if (!root_inode)
404                 goto out_no_root;
405         sb->s_root = d_alloc_root(root_inode);
406         if (!sb->s_root)
407                 goto out_no_root;
408
409         sb->s_root->d_op = &nfs_dentry_operations;
410
411         /* Get some general file system info */
412         if (server->rpc_ops->statfs(server, root, &fsinfo) >= 0) {
413                 if (server->namelen == 0)
414                         server->namelen = fsinfo.namelen;
415         } else {
416                 printk(KERN_NOTICE "NFS: cannot retrieve file system info.\n");
417                 goto out_no_root;
418         }
419
420         /* Work out a lot of parameters */
421         if (data->rsize == 0)
422                 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
423         if (data->wsize == 0)
424                 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
425         /* NFSv3: we don't have bsize, but rather rtmult and wtmult... */
426         if (!fsinfo.bsize)
427                 fsinfo.bsize = (fsinfo.rtmult>fsinfo.wtmult) ? fsinfo.rtmult : fsinfo.wtmult;
428         /* Also make sure we don't go below rsize/wsize since
429          * RPC calls are expensive */
430         if (fsinfo.bsize < server->rsize)
431                 fsinfo.bsize = server->rsize;
432         if (fsinfo.bsize < server->wsize)
433                 fsinfo.bsize = server->wsize;
434
435         if (data->bsize == 0)
436                 sb->s_blocksize = nfs_block_bits(fsinfo.bsize, &sb->s_blocksize_bits);
437         if (server->rsize > fsinfo.rtmax)
438                 server->rsize = fsinfo.rtmax;
439         if (server->wsize > fsinfo.wtmax)
440                 server->wsize = fsinfo.wtmax;
441
442         server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
443         if (server->rpages > NFS_READ_MAXIOV) {
444                 server->rpages = NFS_READ_MAXIOV;
445                 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
446         }
447
448         server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
449         if (server->wpages > NFS_WRITE_MAXIOV) {
450                 server->wpages = NFS_WRITE_MAXIOV;
451                 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
452         }
453
454         server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
455         if (server->dtsize > PAGE_CACHE_SIZE)
456                 server->dtsize = PAGE_CACHE_SIZE;
457         if (server->dtsize > server->rsize)
458                 server->dtsize = server->rsize;
459
460         maxlen = (version == 2) ? NFS2_MAXNAMLEN : NFS3_MAXNAMLEN;
461
462         if (server->namelen == 0 || server->namelen > maxlen)
463                 server->namelen = maxlen;
464
465         sb->s_maxbytes = fsinfo.maxfilesize;
466         if (sb->s_maxbytes > MAX_LFS_FILESIZE) 
467                 sb->s_maxbytes = MAX_LFS_FILESIZE; 
468
469         /* Fire up the writeback cache */
470         if (nfs_reqlist_alloc(server) < 0) {
471                 printk(KERN_NOTICE "NFS: cannot initialize writeback cache.\n");
472                 goto failure_kill_reqlist;
473         }
474
475         /* We're airborne Set socket buffersize */
476         rpc_setbufsize(clnt, server->wsize + 100, server->rsize + 100);
477
478         /* Check whether to start the lockd process */
479         if (!(server->flags & NFS_MOUNT_NONLM))
480                 lockd_up();
481         return sb;
482
483         /* Yargs. It didn't work out. */
484  failure_kill_reqlist:
485         nfs_reqlist_exit(server);
486 out_no_root:
487         printk("nfs_read_super: get root inode failed\n");
488         iput(root_inode);
489         rpciod_down();
490         goto out_shutdown;
491
492 out_no_iod:
493         printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
494 out_shutdown:
495         rpc_shutdown_client(server->client);
496         goto out_free_host;
497
498 out_no_client:
499         printk(KERN_WARNING "NFS: cannot create RPC client.\n");
500         xprt_destroy(xprt);
501         goto out_free_host;
502
503 out_no_xprt:
504         printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
505
506 out_free_host:
507         nfs_reqlist_free(server);
508         kfree(server->hostname);
509 out_unlock:
510         goto out_fail;
511
512 out_no_remote:
513         printk("NFS: mount program didn't pass remote address!\n");
514         goto out_fail;
515
516 out_miss_args:
517         printk("nfs_read_super: missing data argument\n");
518
519 out_fail:
520         return NULL;
521 }
522
523 static int
524 nfs_statfs(struct super_block *sb, struct statfs *buf)
525 {
526         struct nfs_server *server = &sb->u.nfs_sb.s_server;
527         unsigned char blockbits;
528         unsigned long blockres;
529         struct nfs_fsinfo res;
530         int error;
531
532         error = server->rpc_ops->statfs(server, NFS_FH(sb->s_root->d_inode), &res);
533         buf->f_type = NFS_SUPER_MAGIC;
534         if (error < 0)
535                 goto out_err;
536
537         if (res.bsize == 0)
538                 res.bsize = sb->s_blocksize;
539         buf->f_bsize = nfs_block_bits(res.bsize, &blockbits);
540         blockres = (1 << blockbits) - 1;
541         buf->f_blocks = (res.tbytes + blockres) >> blockbits;
542         buf->f_bfree = (res.fbytes + blockres) >> blockbits;
543         buf->f_bavail = (res.abytes + blockres) >> blockbits;
544         buf->f_files = res.tfiles;
545         buf->f_ffree = res.afiles;
546         if (res.namelen == 0 || res.namelen > server->namelen)
547                 res.namelen = server->namelen;
548         buf->f_namelen = res.namelen;
549         return 0;
550  out_err:
551         printk("nfs_statfs: statfs error = %d\n", -error);
552         buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
553         return 0;
554 }
555
556 static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
557 {
558         static struct proc_nfs_info {
559                 int flag;
560                 char *str;
561                 char *nostr;
562         } nfs_info[] = {
563                 { NFS_MOUNT_SOFT, ",soft", ",hard" },
564                 { NFS_MOUNT_INTR, ",intr", "" },
565                 { NFS_MOUNT_POSIX, ",posix", "" },
566                 { NFS_MOUNT_TCP, ",tcp", ",udp" },
567                 { NFS_MOUNT_NOCTO, ",nocto", "" },
568                 { NFS_MOUNT_NOAC, ",noac", "" },
569                 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
570                 { NFS_MOUNT_BROKEN_SUID, ",broken_suid", "" },
571                 { 0, NULL, NULL }
572         };
573         struct proc_nfs_info *nfs_infop;
574         struct nfs_server *nfss = &mnt->mnt_sb->u.nfs_sb.s_server;
575
576         seq_printf(m, ",v%d", nfss->rpc_ops->version);
577         seq_printf(m, ",rsize=%d", nfss->rsize);
578         seq_printf(m, ",wsize=%d", nfss->wsize);
579         if (nfss->acregmin != 3*HZ)
580                 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
581         if (nfss->acregmax != 60*HZ)
582                 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
583         if (nfss->acdirmin != 30*HZ)
584                 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
585         if (nfss->acdirmax != 60*HZ)
586                 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
587         for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
588                 if (nfss->flags & nfs_infop->flag)
589                         seq_puts(m, nfs_infop->str);
590                 else
591                         seq_puts(m, nfs_infop->nostr);
592         }
593         seq_puts(m, ",addr=");
594         seq_escape(m, nfss->hostname, " \t\n\\");
595         return 0;
596 }
597
598 /*
599  * Invalidate the local caches
600  */
601 void
602 nfs_zap_caches(struct inode *inode)
603 {
604         NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
605         NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
606
607         invalidate_inode_pages(inode);
608
609         memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
610         NFS_CACHEINV(inode);
611 }
612
613 /*
614  * Invalidate, but do not unhash, the inode
615  */
616 static void
617 nfs_invalidate_inode(struct inode *inode)
618 {
619         umode_t save_mode = inode->i_mode;
620
621         make_bad_inode(inode);
622         inode->i_mode = save_mode;
623         nfs_zap_caches(inode);
624 }
625
626 /*
627  * Fill in inode information from the fattr.
628  */
629 static void
630 nfs_fill_inode(struct inode *inode, struct nfs_fh *fh, struct nfs_fattr *fattr)
631 {
632         /*
633          * Check whether the mode has been set, as we only want to
634          * do this once. (We don't allow inodes to change types.)
635          */
636         if (inode->i_mode == 0) {
637                 NFS_FILEID(inode) = fattr->fileid;
638                 inode->i_mode = fattr->mode;
639                 /* Why so? Because we want revalidate for devices/FIFOs, and
640                  * that's precisely what we have in nfs_file_inode_operations.
641                  */
642                 inode->i_op = &nfs_file_inode_operations;
643                 if (S_ISREG(inode->i_mode)) {
644                         inode->i_fop = &nfs_file_operations;
645                         inode->i_data.a_ops = &nfs_file_aops;
646                 } else if (S_ISDIR(inode->i_mode)) {
647                         inode->i_op = &nfs_dir_inode_operations;
648                         inode->i_fop = &nfs_dir_operations;
649                 } else if (S_ISLNK(inode->i_mode))
650                         inode->i_op = &nfs_symlink_inode_operations;
651                 else
652                         init_special_inode(inode, inode->i_mode, fattr->rdev);
653                 memcpy(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh));
654         }
655         nfs_refresh_inode(inode, fattr);
656 }
657
658 struct nfs_find_desc {
659         struct nfs_fh           *fh;
660         struct nfs_fattr        *fattr;
661 };
662
663 /*
664  * In NFSv3 we can have 64bit inode numbers. In order to support
665  * this, and re-exported directories (also seen in NFSv2)
666  * we are forced to allow 2 different inodes to have the same
667  * i_ino.
668  */
669 static int
670 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
671 {
672         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
673         struct nfs_fh           *fh = desc->fh;
674         struct nfs_fattr        *fattr = desc->fattr;
675
676         if (NFS_FILEID(inode) != fattr->fileid)
677                 return 0;
678         if (memcmp(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh)) != 0)
679                 return 0;
680         if (is_bad_inode(inode))
681                 return 0;
682         /* Force an attribute cache update if inode->i_count == 0 */
683         if (!atomic_read(&inode->i_count))
684                 NFS_CACHEINV(inode);
685         return 1;
686 }
687
688 /*
689  * This is our own version of iget that looks up inodes by file handle
690  * instead of inode number.  We use this technique instead of using
691  * the vfs read_inode function because there is no way to pass the
692  * file handle or current attributes into the read_inode function.
693  *
694  */
695 struct inode *
696 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
697                                  struct nfs_fattr *fattr)
698 {
699         struct super_block *sb = dentry->d_sb;
700
701         dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
702                 dentry->d_parent->d_name.name, dentry->d_name.name,
703                 (long long)fattr->fileid);
704         return __nfs_fhget(sb, fhandle, fattr);
705 }
706
707 /*
708  * Look up the inode by super block and fattr->fileid.
709  */
710 static struct inode *
711 __nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
712 {
713         struct nfs_find_desc desc = { fh, fattr };
714         struct inode *inode = NULL;
715         unsigned long ino;
716
717         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
718                 goto out_no_inode;
719
720         if (!fattr->nlink) {
721                 printk("NFS: Buggy server - nlink == 0!\n");
722                 goto out_no_inode;
723         }
724
725         ino = nfs_fattr_to_ino_t(fattr);
726
727         if (!(inode = iget4(sb, ino, nfs_find_actor, &desc)))
728                 goto out_no_inode;
729
730         nfs_fill_inode(inode, fh, fattr);
731         dprintk("NFS: __nfs_fhget(%x/%Ld ct=%d)\n",
732                 inode->i_dev, (long long)NFS_FILEID(inode),
733                 atomic_read(&inode->i_count));
734
735 out:
736         return inode;
737
738 out_no_inode:
739         printk("__nfs_fhget: iget failed\n");
740         goto out;
741 }
742
743 int
744 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
745 {
746         struct inode *inode = dentry->d_inode;
747         struct nfs_fattr fattr;
748         int error;
749
750         /*
751          * Make sure the inode is up-to-date.
752          */
753         error = nfs_revalidate_inode(NFS_SERVER(inode),inode);
754         if (error) {
755 #ifdef NFS_PARANOIA
756 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
757 #endif
758                 goto out;
759         }
760
761         if (!S_ISREG(inode->i_mode))
762                 attr->ia_valid &= ~ATTR_SIZE;
763
764         filemap_fdatasync(inode->i_mapping);
765         error = nfs_wb_all(inode);
766         filemap_fdatawait(inode->i_mapping);
767         if (error)
768                 goto out;
769
770         error = NFS_PROTO(inode)->setattr(inode, &fattr, attr);
771         if (error)
772                 goto out;
773         /*
774          * If we changed the size or mtime, update the inode
775          * now to avoid invalidating the page cache.
776          */
777         if (attr->ia_valid & ATTR_SIZE) {
778                 if (attr->ia_size != fattr.size)
779                         printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
780                                (long long) attr->ia_size, (long long)fattr.size);
781                 vmtruncate(inode, attr->ia_size);
782         }
783
784         /*
785          * If we changed the size or mtime, update the inode
786          * now to avoid invalidating the page cache.
787          */
788         if (!(fattr.valid & NFS_ATTR_WCC)) {
789                 fattr.pre_size = NFS_CACHE_ISIZE(inode);
790                 fattr.pre_mtime = NFS_CACHE_MTIME(inode);
791                 fattr.pre_ctime = NFS_CACHE_CTIME(inode);
792                 fattr.valid |= NFS_ATTR_WCC;
793         }
794         /* Force an attribute cache update */
795         NFS_CACHEINV(inode);
796         error = nfs_refresh_inode(inode, &fattr);
797 out:
798         return error;
799 }
800
801 /*
802  * Wait for the inode to get unlocked.
803  * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
804  */
805 int
806 nfs_wait_on_inode(struct inode *inode, int flag)
807 {
808         struct rpc_clnt *clnt = NFS_CLIENT(inode);
809         int error;
810         if (!(NFS_FLAGS(inode) & flag))
811                 return 0;
812         atomic_inc(&inode->i_count);
813         error = nfs_wait_event(clnt, inode->i_wait, !(NFS_FLAGS(inode) & flag));
814         iput(inode);
815         return error;
816 }
817
818 /*
819  * Externally visible revalidation function
820  */
821 int
822 nfs_revalidate(struct dentry *dentry)
823 {
824         struct inode *inode = dentry->d_inode;
825         return nfs_revalidate_inode(NFS_SERVER(inode), inode);
826 }
827
828 /*
829  * Ensure that mmap has a recent RPC credential for use when writing out
830  * shared pages
831  */
832 static inline void
833 nfs_set_mmcred(struct inode *inode, struct rpc_cred *cred)
834 {
835         struct rpc_cred **p = &NFS_I(inode)->mm_cred,
836                         *oldcred = *p;
837
838         *p = get_rpccred(cred);
839         if (oldcred)
840                 put_rpccred(oldcred);
841 }
842
843 /*
844  * These are probably going to contain hooks for
845  * allocating and releasing RPC credentials for
846  * the file. I'll have to think about Tronds patch
847  * a bit more..
848  */
849 int nfs_open(struct inode *inode, struct file *filp)
850 {
851         struct rpc_auth *auth;
852         struct rpc_cred *cred;
853
854         lock_kernel();
855         auth = NFS_CLIENT(inode)->cl_auth;
856         cred = rpcauth_lookupcred(auth, 0);
857         filp->private_data = cred;
858         if (filp->f_mode & FMODE_WRITE)
859                 nfs_set_mmcred(inode, cred);
860         unlock_kernel();
861         return 0;
862 }
863
864 int nfs_release(struct inode *inode, struct file *filp)
865 {
866         struct rpc_cred *cred;
867
868         lock_kernel();
869         cred = nfs_file_cred(filp);
870         if (cred)
871                 put_rpccred(cred);
872         unlock_kernel();
873         return 0;
874 }
875
876 /*
877  * This function is called whenever some part of NFS notices that
878  * the cached attributes have to be refreshed.
879  */
880 int
881 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
882 {
883         int              status = -ESTALE;
884         struct nfs_fattr fattr;
885
886         dfprintk(PAGECACHE, "NFS: revalidating (%x/%Ld)\n",
887                 inode->i_dev, (long long)NFS_FILEID(inode));
888
889         lock_kernel();
890         if (!inode || is_bad_inode(inode))
891                 goto out_nowait;
892         if (NFS_STALE(inode) && inode != inode->i_sb->s_root->d_inode)
893                 goto out_nowait;
894
895         while (NFS_REVALIDATING(inode)) {
896                 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
897                 if (status < 0)
898                         goto out_nowait;
899                 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode))) {
900                         status = NFS_STALE(inode) ? -ESTALE : 0;
901                         goto out_nowait;
902                 }
903         }
904         NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
905
906         status = NFS_PROTO(inode)->getattr(inode, &fattr);
907         if (status) {
908                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) getattr failed, error=%d\n",
909                          inode->i_dev, (long long)NFS_FILEID(inode), status);
910                 if (status == -ESTALE) {
911                         NFS_FLAGS(inode) |= NFS_INO_STALE;
912                         if (inode != inode->i_sb->s_root->d_inode)
913                                 remove_inode_hash(inode);
914                 }
915                 goto out;
916         }
917
918         status = nfs_refresh_inode(inode, &fattr);
919         if (status) {
920                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) refresh failed, error=%d\n",
921                          inode->i_dev, (long long)NFS_FILEID(inode), status);
922                 goto out;
923         }
924         dfprintk(PAGECACHE, "NFS: (%x/%Ld) revalidation complete\n",
925                 inode->i_dev, (long long)NFS_FILEID(inode));
926
927         NFS_FLAGS(inode) &= ~NFS_INO_STALE;
928 out:
929         NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
930         wake_up(&inode->i_wait);
931  out_nowait:
932         unlock_kernel();
933         return status;
934 }
935
936 /*
937  * nfs_fattr_obsolete - Test if attribute data is newer than cached data
938  * @inode: inode
939  * @fattr: attributes to test
940  *
941  * Avoid stuffing the attribute cache with obsolete information.
942  * We always accept updates if the attribute cache timed out, or if
943  * fattr->ctime is newer than our cached value.
944  * If fattr->ctime matches the cached value, we still accept the update
945  * if it increases the file size.
946  */
947 static inline
948 int nfs_fattr_obsolete(struct inode *inode, struct nfs_fattr *fattr)
949 {
950         s64 cdif;
951
952         if (time_after(jiffies, NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
953                 goto out_valid;
954         if ((cdif = (s64)fattr->ctime - (s64)NFS_CACHE_CTIME(inode)) > 0)
955                 goto out_valid;
956         /* Ugh... */
957         if (cdif == 0 && fattr->size > NFS_CACHE_ISIZE(inode))
958                 goto out_valid;
959         return -1;
960  out_valid:
961         return 0;
962 }
963
964 /*
965  * Many nfs protocol calls return the new file attributes after
966  * an operation.  Here we update the inode to reflect the state
967  * of the server's inode.
968  *
969  * This is a bit tricky because we have to make sure all dirty pages
970  * have been sent off to the server before calling invalidate_inode_pages.
971  * To make sure no other process adds more write requests while we try
972  * our best to flush them, we make them sleep during the attribute refresh.
973  *
974  * A very similar scenario holds for the dir cache.
975  */
976 int
977 __nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
978 {
979         __u64           new_size, new_mtime;
980         loff_t          new_isize;
981         time_t          new_atime;
982         int             invalid = 0;
983
984         dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
985                         inode->i_dev, inode->i_ino,
986                         atomic_read(&inode->i_count), fattr->valid);
987
988         if (NFS_FILEID(inode) != fattr->fileid) {
989                 printk(KERN_ERR "nfs_refresh_inode: inode number mismatch\n"
990                        "expected (0x%x/0x%Lx), got (0x%x/0x%Lx)\n",
991                        inode->i_dev, (long long)NFS_FILEID(inode),
992                        inode->i_dev, (long long)fattr->fileid);
993                 goto out_err;
994         }
995
996         /*
997          * Make sure the inode's type hasn't changed.
998          */
999         if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1000                 goto out_changed;
1001
1002         new_mtime = fattr->mtime;
1003         new_size = fattr->size;
1004         new_isize = nfs_size_to_loff_t(fattr->size);
1005
1006         new_atime = nfs_time_to_secs(fattr->atime);
1007         /* Avoid races */
1008         if (nfs_fattr_obsolete(inode, fattr))
1009                 goto out_nochange;
1010
1011         /*
1012          * Update the read time so we don't revalidate too often.
1013          */
1014         NFS_READTIME(inode) = jiffies;
1015
1016         /*
1017          * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
1018          *       NOT inode->i_size!!!
1019          */
1020         if (NFS_CACHE_ISIZE(inode) != new_size) {
1021 #ifdef NFS_DEBUG_VERBOSE
1022                 printk(KERN_DEBUG "NFS: isize change on %x/%ld\n", inode->i_dev, inode->i_ino);
1023 #endif
1024                 invalid = 1;
1025         }
1026
1027         /*
1028          * Note: we don't check inode->i_mtime since pipes etc.
1029          *       can change this value in VFS without requiring a
1030          *       cache revalidation.
1031          */
1032         if (NFS_CACHE_MTIME(inode) != new_mtime) {
1033 #ifdef NFS_DEBUG_VERBOSE
1034                 printk(KERN_DEBUG "NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
1035 #endif
1036                 invalid = 1;
1037         }
1038
1039         /* Check Weak Cache Consistency data.
1040          * If size and mtime match the pre-operation values, we can
1041          * assume that any attribute changes were caused by our NFS
1042          * operation, so there's no need to invalidate the caches.
1043          */
1044         if ((fattr->valid & NFS_ATTR_WCC)
1045             && NFS_CACHE_ISIZE(inode) == fattr->pre_size
1046             && NFS_CACHE_MTIME(inode) == fattr->pre_mtime) {
1047                 invalid = 0;
1048         }
1049
1050         /* set the invalid flag if the last attempt at invalidating
1051          * the inode didn't empty the clean_pages list */
1052         if ( NFS_FLAGS(inode) & NFS_INO_MAPPED) {
1053                 NFS_FLAGS(inode) &= ~NFS_INO_MAPPED;
1054                 invalid = 1;
1055         }
1056
1057         /*
1058          * If we have pending writebacks, things can get
1059          * messy.
1060          */
1061         if (nfs_have_writebacks(inode) && new_isize < inode->i_size)
1062                 new_isize = inode->i_size;
1063
1064         NFS_CACHE_CTIME(inode) = fattr->ctime;
1065         inode->i_ctime = nfs_time_to_secs(fattr->ctime);
1066
1067         inode->i_atime = new_atime;
1068
1069         if (NFS_CACHE_MTIME(inode) != new_mtime) {
1070                 NFS_MTIME_UPDATE(inode) = jiffies;
1071                 NFS_CACHE_MTIME(inode) = new_mtime;
1072                 inode->i_mtime = nfs_time_to_secs(new_mtime);
1073         }
1074
1075         NFS_CACHE_ISIZE(inode) = new_size;
1076         inode->i_size = new_isize;
1077
1078         inode->i_mode = fattr->mode;
1079         inode->i_nlink = fattr->nlink;
1080         inode->i_uid = fattr->uid;
1081         inode->i_gid = fattr->gid;
1082
1083         if (fattr->valid & NFS_ATTR_FATTR_V3) {
1084                 /*
1085                  * report the blocks in 512byte units
1086                  */
1087                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1088                 inode->i_blksize = inode->i_sb->s_blocksize;
1089         } else {
1090                 inode->i_blocks = fattr->du.nfs2.blocks;
1091                 inode->i_blksize = fattr->du.nfs2.blocksize;
1092         }
1093         inode->i_rdev = 0;
1094         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1095                 inode->i_rdev = to_kdev_t(fattr->rdev);
1096  
1097         /* Update attrtimeo value */
1098         if (invalid) {
1099                 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
1100                 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1101                 invalidate_inode_pages(inode);
1102                 if (! list_empty(&inode->i_mapping->clean_pages)) {
1103                         dfprintk(PAGECACHE,
1104                                  "NFS: clean_pages for %x/%ld is not empty\n",
1105                                  inode->i_dev, inode->i_ino);
1106                         NFS_FLAGS(inode) |= NFS_INO_MAPPED;
1107                 }
1108                 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1109         } else if (time_after(jiffies, NFS_ATTRTIMEO_UPDATE(inode)+NFS_ATTRTIMEO(inode))) {
1110                 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
1111                         NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
1112                 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1113         }
1114
1115         return 0;
1116  out_nochange:
1117         if (new_atime - inode->i_atime > 0)
1118                 inode->i_atime = new_atime;
1119         return 0;
1120  out_changed:
1121         /*
1122          * Big trouble! The inode has become a different object.
1123          */
1124 #ifdef NFS_PARANOIA
1125         printk(KERN_DEBUG "nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1126                inode->i_ino, inode->i_mode, fattr->mode);
1127 #endif
1128         /*
1129          * No need to worry about unhashing the dentry, as the
1130          * lookup validation will know that the inode is bad.
1131          * (But we fall through to invalidate the caches.)
1132          */
1133         nfs_invalidate_inode(inode);
1134  out_err:
1135         return -EIO;
1136 }
1137
1138 /*
1139  * File system information
1140  */
1141 static DECLARE_FSTYPE(nfs_fs_type, "nfs", nfs_read_super, FS_ODD_RENAME);
1142
1143 extern int nfs_init_nfspagecache(void);
1144 extern void nfs_destroy_nfspagecache(void);
1145 extern int nfs_init_readpagecache(void);
1146 extern int nfs_destroy_readpagecache(void);
1147 extern int nfs_init_writepagecache(void);
1148 extern int nfs_destroy_writepagecache(void);
1149
1150 /*
1151  * Initialize NFS
1152  */
1153 static int __init init_nfs_fs(void)
1154 {
1155         int err;
1156
1157         err = nfs_init_nfspagecache();
1158         if (err)
1159                 return err;
1160
1161         err = nfs_init_readpagecache();
1162         if (err)
1163                 return err;
1164
1165         err = nfs_init_writepagecache();
1166         if (err)
1167                 return err;
1168
1169 #ifdef CONFIG_PROC_FS
1170         rpc_proc_register(&nfs_rpcstat);
1171 #endif
1172         return register_filesystem(&nfs_fs_type);
1173 }
1174
1175 static void __exit exit_nfs_fs(void)
1176 {
1177         nfs_destroy_writepagecache();
1178         nfs_destroy_readpagecache();
1179         nfs_destroy_nfspagecache();
1180 #ifdef CONFIG_PROC_FS
1181         rpc_proc_unregister("nfs");
1182 #endif
1183         unregister_filesystem(&nfs_fs_type);
1184 }
1185
1186 EXPORT_NO_SYMBOLS;
1187 /* Not quite true; I just maintain it */
1188 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1189 MODULE_LICENSE("GPL");
1190
1191 module_init(init_nfs_fs)
1192 module_exit(exit_nfs_fs)