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