Linux-2.4.0-test2
[davej-history.git] / fs / nfs / inode.c
blobaedda1e90343e00f64a676430fda55e822a451d2
1 /*
2 * linux/fs/nfs/inode.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
16 #include <linux/config.h>
17 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
25 #include <linux/locks.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/nfs_flushd.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/smp_lock.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
38 #define CONFIG_NFS_SNAPSHOT 1
39 #define NFSDBG_FACILITY NFSDBG_VFS
40 #define NFS_PARANOIA 1
42 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fattr *);
43 void nfs_zap_caches(struct inode *);
44 static void nfs_invalidate_inode(struct inode *);
46 static void nfs_read_inode(struct inode *);
47 static void nfs_delete_inode(struct inode *);
48 static void nfs_put_super(struct super_block *);
49 static void nfs_umount_begin(struct super_block *);
50 static int nfs_statfs(struct super_block *, struct statfs *);
52 static struct super_operations nfs_sops = {
53 read_inode: nfs_read_inode,
54 put_inode: force_delete,
55 delete_inode: nfs_delete_inode,
56 put_super: nfs_put_super,
57 statfs: nfs_statfs,
58 umount_begin: nfs_umount_begin,
62 * RPC cruft for NFS
64 struct rpc_stat nfs_rpcstat = { &nfs_program };
65 static struct rpc_version * nfs_version[] = {
66 NULL,
67 NULL,
68 &nfs_version2,
69 #ifdef CONFIG_NFS_V3
70 &nfs_version3,
71 #endif
74 struct rpc_program nfs_program = {
75 "nfs",
76 NFS_PROGRAM,
77 sizeof(nfs_version) / sizeof(nfs_version[0]),
78 nfs_version,
79 &nfs_rpcstat,
82 static inline unsigned long
83 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
85 return nfs_fileid_to_ino_t(fattr->fileid);
89 * The "read_inode" function doesn't actually do anything:
90 * the real data is filled in later in nfs_fhget. Here we
91 * just mark the cache times invalid, and zero out i_mode
92 * (the latter makes "nfs_refresh_inode" do the right thing
93 * wrt pipe inodes)
95 static void
96 nfs_read_inode(struct inode * inode)
98 inode->i_blksize = inode->i_sb->s_blocksize;
99 inode->i_mode = 0;
100 inode->i_rdev = 0;
101 NFS_FILEID(inode) = 0;
102 NFS_FSID(inode) = 0;
103 INIT_LIST_HEAD(&inode->u.nfs_i.read);
104 INIT_LIST_HEAD(&inode->u.nfs_i.dirty);
105 INIT_LIST_HEAD(&inode->u.nfs_i.commit);
106 INIT_LIST_HEAD(&inode->u.nfs_i.writeback);
107 inode->u.nfs_i.nread = 0;
108 inode->u.nfs_i.ndirty = 0;
109 inode->u.nfs_i.ncommit = 0;
110 inode->u.nfs_i.npages = 0;
111 NFS_CACHEINV(inode);
112 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
113 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
116 static void
117 nfs_delete_inode(struct inode * inode)
119 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
122 * The following can never actually happen...
124 if (nfs_have_writebacks(inode) || nfs_have_read(inode)) {
125 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
128 clear_inode(inode);
131 void
132 nfs_put_super(struct super_block *sb)
134 struct nfs_server *server = &sb->u.nfs_sb.s_server;
135 struct rpc_clnt *rpc;
138 * First get rid of the request flushing daemon.
139 * Relies on rpc_shutdown_client() waiting on all
140 * client tasks to finish.
142 nfs_reqlist_exit(server);
144 if ((rpc = server->client) != NULL)
145 rpc_shutdown_client(rpc);
147 nfs_reqlist_free(server);
149 if (!(server->flags & NFS_MOUNT_NONLM))
150 lockd_down(); /* release rpc.lockd */
151 rpciod_down(); /* release rpciod */
153 kfree(server->hostname);
156 void
157 nfs_umount_begin(struct super_block *sb)
159 struct nfs_server *server = &sb->u.nfs_sb.s_server;
160 struct rpc_clnt *rpc;
162 /* -EIO all pending I/O */
163 if ((rpc = server->client) != NULL)
164 rpc_killall_tasks(rpc);
168 static inline unsigned long
169 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
171 /* make sure blocksize is a power of two */
172 if ((bsize & (bsize - 1)) || nrbitsp) {
173 unsigned char nrbits;
175 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
177 bsize = 1 << nrbits;
178 if (nrbitsp)
179 *nrbitsp = nrbits;
182 return bsize;
186 * Calculate the number of 512byte blocks used.
188 static inline unsigned long
189 nfs_calc_block_size(u64 tsize)
191 loff_t used = (tsize + 511) >> 9;
192 return (used > ULONG_MAX) ? ULONG_MAX : used;
196 * Compute and set NFS server blocksize
198 static inline unsigned long
199 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
201 if (bsize < 1024)
202 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
203 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
204 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
206 return nfs_block_bits(bsize, nrbitsp);
210 * Obtain the root inode of the file system.
212 static struct inode *
213 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh)
215 struct nfs_server *server = &sb->u.nfs_sb.s_server;
216 struct nfs_fattr fattr;
217 struct inode *inode;
218 int error;
220 if ((error = server->rpc_ops->getroot(server, rootfh, &fattr)) < 0) {
221 printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error);
222 return NULL;
225 inode = __nfs_fhget(sb, &fattr);
226 return inode;
229 extern struct nfs_fh *nfs_fh_alloc(void);
230 extern void nfs_fh_free(struct nfs_fh *p);
233 * The way this works is that the mount process passes a structure
234 * in the data argument which contains the server's IP address
235 * and the root file handle obtained from the server's mount
236 * daemon. We stash these away in the private superblock fields.
238 struct super_block *
239 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
241 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
242 struct nfs_server *server;
243 struct rpc_xprt *xprt = NULL;
244 struct rpc_clnt *clnt = NULL;
245 struct nfs_fh *root = &data->root, *root_fh, fh;
246 struct inode *root_inode = NULL;
247 unsigned int authflavor;
248 struct sockaddr_in srvaddr;
249 struct rpc_timeout timeparms;
250 struct nfs_fsinfo fsinfo;
251 int tcp, version, maxlen;
253 memset(&sb->u.nfs_sb, 0, sizeof(sb->u.nfs_sb));
254 if (!data)
255 goto out_miss_args;
257 memset(&fh, 0, sizeof(fh));
258 if (data->version != NFS_MOUNT_VERSION) {
259 printk("nfs warning: mount version %s than kernel\n",
260 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
261 if (data->version < 2)
262 data->namlen = 0;
263 if (data->version < 3)
264 data->bsize = 0;
265 if (data->version < 4) {
266 data->flags &= ~NFS_MOUNT_VER3;
267 root = &fh;
268 root->size = NFS2_FHSIZE;
269 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
273 /* We now require that the mount process passes the remote address */
274 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
275 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
276 goto out_no_remote;
278 sb->s_flags |= MS_ODD_RENAME; /* This should go away */
280 sb->s_magic = NFS_SUPER_MAGIC;
281 sb->s_op = &nfs_sops;
282 sb->s_blocksize_bits = 0;
283 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
284 server = &sb->u.nfs_sb.s_server;
285 server->rsize = nfs_block_size(data->rsize, NULL);
286 server->wsize = nfs_block_size(data->wsize, NULL);
287 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
289 if (data->flags & NFS_MOUNT_NOAC) {
290 data->acregmin = data->acregmax = 0;
291 data->acdirmin = data->acdirmax = 0;
293 server->acregmin = data->acregmin*HZ;
294 server->acregmax = data->acregmax*HZ;
295 server->acdirmin = data->acdirmin*HZ;
296 server->acdirmax = data->acdirmax*HZ;
298 server->namelen = data->namlen;
299 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
300 if (!server->hostname)
301 goto out_unlock;
302 strcpy(server->hostname, data->hostname);
304 nfsv3_try_again:
305 /* Check NFS protocol revision and initialize RPC op vector
306 * and file handle pool. */
307 if (data->flags & NFS_MOUNT_VER3) {
308 #ifdef CONFIG_NFS_V3
309 server->rpc_ops = &nfs_v3_clientops;
310 version = 3;
311 if (data->version < 4) {
312 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
313 goto out_unlock;
315 #else
316 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
317 goto out_unlock;
318 #endif
319 } else {
320 server->rpc_ops = &nfs_v2_clientops;
321 version = 2;
324 /* Which protocol do we use? */
325 tcp = (data->flags & NFS_MOUNT_TCP);
327 /* Initialize timeout values */
328 timeparms.to_initval = data->timeo * HZ / 10;
329 timeparms.to_retries = data->retrans;
330 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
331 timeparms.to_exponential = 1;
333 if (!timeparms.to_initval)
334 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
335 if (!timeparms.to_retries)
336 timeparms.to_retries = 5;
338 /* Now create transport and client */
339 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
340 &srvaddr, &timeparms);
341 if (xprt == NULL)
342 goto out_no_xprt;
344 /* Choose authentication flavor */
345 authflavor = RPC_AUTH_UNIX;
346 if (data->flags & NFS_MOUNT_SECURE)
347 authflavor = RPC_AUTH_DES;
348 else if (data->flags & NFS_MOUNT_KERBEROS)
349 authflavor = RPC_AUTH_KRB;
351 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
352 version, authflavor);
353 if (clnt == NULL)
354 goto out_no_client;
356 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
357 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
358 clnt->cl_chatty = 1;
359 server->client = clnt;
361 /* Fire up rpciod if not yet running */
362 if (rpciod_up() != 0)
363 goto out_no_iod;
366 * Keep the super block locked while we try to get
367 * the root fh attributes.
369 root_fh = nfs_fh_alloc();
370 if (!root_fh)
371 goto out_no_fh;
372 memcpy((u8*)root_fh, (u8*)root, sizeof(*root));
374 /* Did getting the root inode fail? */
375 if (!(root_inode = nfs_get_root(sb, root))
376 && (data->flags & NFS_MOUNT_VER3)) {
377 data->flags &= ~NFS_MOUNT_VER3;
378 nfs_fh_free(root_fh);
379 rpciod_down();
380 rpc_shutdown_client(server->client);
381 goto nfsv3_try_again;
384 if (!root_inode)
385 goto out_no_root;
386 sb->s_root = d_alloc_root(root_inode);
387 if (!sb->s_root)
388 goto out_no_root;
390 sb->s_root->d_op = &nfs_dentry_operations;
391 sb->s_root->d_fsdata = root_fh;
393 /* Get some general file system info */
394 if (server->rpc_ops->statfs(server, root, &fsinfo) >= 0) {
395 if (server->namelen == 0)
396 server->namelen = fsinfo.namelen;
397 } else {
398 printk(KERN_NOTICE "NFS: cannot retrieve file system info.\n");
399 goto out_no_root;
402 /* Work out a lot of parameters */
403 if (data->rsize == 0)
404 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
405 if (data->wsize == 0)
406 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
407 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
408 if (server->dtsize > PAGE_CACHE_SIZE)
409 server->dtsize = PAGE_CACHE_SIZE;
410 /* NFSv3: we don't have bsize, but rather rtmult and wtmult... */
411 if (!fsinfo.bsize)
412 fsinfo.bsize = (fsinfo.rtmult>fsinfo.wtmult) ? fsinfo.rtmult : fsinfo.wtmult;
413 /* Also make sure we don't go below rsize/wsize since
414 * RPC calls are expensive */
415 if (fsinfo.bsize < server->rsize)
416 fsinfo.bsize = server->rsize;
417 if (fsinfo.bsize < server->wsize)
418 fsinfo.bsize = server->wsize;
420 if (data->bsize == 0)
421 sb->s_blocksize = nfs_block_bits(fsinfo.bsize, &sb->s_blocksize_bits);
422 if (server->rsize > fsinfo.rtmax)
423 server->rsize = fsinfo.rtmax;
424 if (server->wsize > fsinfo.wtmax)
425 server->wsize = fsinfo.wtmax;
427 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
428 if (server->rpages > NFS_READ_MAXIOV) {
429 server->rpages = NFS_READ_MAXIOV;
430 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
433 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
434 if (server->wpages > NFS_WRITE_MAXIOV) {
435 server->wpages = NFS_WRITE_MAXIOV;
436 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
439 maxlen = (version == 2) ? NFS2_MAXNAMLEN : NFS3_MAXNAMLEN;
441 if (server->namelen == 0 || server->namelen > maxlen)
442 server->namelen = maxlen;
444 /* Fire up the writeback cache */
445 if (nfs_reqlist_alloc(server) < 0) {
446 printk(KERN_NOTICE "NFS: cannot initialize writeback cache.\n");
447 goto failure_kill_reqlist;
450 /* We're airborne */
452 /* Check whether to start the lockd process */
453 if (!(server->flags & NFS_MOUNT_NONLM))
454 lockd_up();
455 return sb;
457 /* Yargs. It didn't work out. */
458 failure_kill_reqlist:
459 nfs_reqlist_exit(server);
460 out_no_root:
461 printk("nfs_read_super: get root inode failed\n");
462 iput(root_inode);
463 nfs_fh_free(root_fh);
464 out_no_fh:
465 rpciod_down();
466 goto out_shutdown;
468 out_no_iod:
469 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
470 out_shutdown:
471 rpc_shutdown_client(server->client);
472 goto out_free_host;
474 out_no_client:
475 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
476 xprt_destroy(xprt);
477 goto out_free_host;
479 out_no_xprt:
480 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
482 out_free_host:
483 nfs_reqlist_free(server);
484 kfree(server->hostname);
485 out_unlock:
486 goto out_fail;
488 out_no_remote:
489 printk("NFS: mount program didn't pass remote address!\n");
490 goto out_fail;
492 out_miss_args:
493 printk("nfs_read_super: missing data argument\n");
495 out_fail:
496 return NULL;
499 static int
500 nfs_statfs(struct super_block *sb, struct statfs *buf)
502 struct nfs_server *server = &sb->u.nfs_sb.s_server;
503 unsigned char blockbits;
504 unsigned long blockres;
505 struct nfs_fsinfo res;
506 int error;
508 error = server->rpc_ops->statfs(server, NFS_FH(sb->s_root), &res);
509 buf->f_type = NFS_SUPER_MAGIC;
510 if (error < 0)
511 goto out_err;
513 if (res.bsize == 0)
514 res.bsize = sb->s_blocksize;
515 buf->f_bsize = nfs_block_bits(res.bsize, &blockbits);
516 blockres = (1 << blockbits) - 1;
517 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
518 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
519 buf->f_bavail = (res.abytes + blockres) >> blockbits;
520 buf->f_files = res.tfiles;
521 buf->f_ffree = res.afiles;
522 if (res.namelen == 0 || res.namelen > server->namelen)
523 res.namelen = server->namelen;
524 buf->f_namelen = res.namelen;
525 return 0;
526 out_err:
527 printk("nfs_statfs: statfs error = %d\n", -error);
528 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
529 return 0;
533 * Free all unused dentries in an inode's alias list.
535 * Subtle note: we have to be very careful not to cause
536 * any IO operations with the stale dentries, as this
537 * could cause file corruption. But since the dentry
538 * count is 0 and all pending IO for a dentry has been
539 * flushed when the count went to 0, we're safe here.
540 * Also returns the number of unhashed dentries
542 static int
543 nfs_free_dentries(struct inode *inode)
545 struct list_head *tmp, *head = &inode->i_dentry;
546 int unhashed;
548 if (S_ISDIR(inode->i_mode)) {
549 struct dentry *dentry = d_find_alias(inode);
550 if (dentry) {
551 shrink_dcache_parent(dentry);
552 dput(dentry);
555 d_prune_aliases(inode);
556 tmp = head;
557 unhashed = 0;
558 while ((tmp = tmp->next) != head) {
559 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
560 if (d_unhashed(dentry))
561 unhashed++;
563 return unhashed;
567 * Invalidate the local caches
569 void
570 nfs_zap_caches(struct inode *inode)
572 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
573 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
575 invalidate_inode_pages(inode);
577 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
578 NFS_CACHEINV(inode);
582 * Invalidate, but do not unhash, the inode
584 static void
585 nfs_invalidate_inode(struct inode *inode)
587 umode_t save_mode = inode->i_mode;
589 make_bad_inode(inode);
590 inode->i_mode = save_mode;
591 nfs_zap_caches(inode);
595 * Fill in inode information from the fattr.
597 static void
598 nfs_fill_inode(struct inode *inode, struct nfs_fattr *fattr)
601 * Check whether the mode has been set, as we only want to
602 * do this once. (We don't allow inodes to change types.)
604 if (inode->i_mode == 0) {
605 NFS_FILEID(inode) = fattr->fileid;
606 NFS_FSID(inode) = fattr->fsid;
607 inode->i_mode = fattr->mode;
608 /* Why so? Because we want revalidate for devices/FIFOs, and
609 * that's precisely what we have in nfs_file_inode_operations.
611 inode->i_op = &nfs_file_inode_operations;
612 if (S_ISREG(inode->i_mode)) {
613 inode->i_fop = &nfs_file_operations;
614 inode->i_data.a_ops = &nfs_file_aops;
615 } else if (S_ISDIR(inode->i_mode)) {
616 inode->i_op = &nfs_dir_inode_operations;
617 inode->i_fop = &nfs_dir_operations;
618 } else if (S_ISLNK(inode->i_mode))
619 inode->i_op = &nfs_symlink_inode_operations;
620 else
621 init_special_inode(inode, inode->i_mode, fattr->rdev);
623 * Preset the size and mtime, as there's no need
624 * to invalidate the caches.
626 inode->i_size = nfs_size_to_loff_t(fattr->size);
627 inode->i_mtime = nfs_time_to_secs(fattr->mtime);
628 inode->i_atime = nfs_time_to_secs(fattr->atime);
629 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
630 NFS_CACHE_CTIME(inode) = fattr->ctime;
631 NFS_CACHE_MTIME(inode) = fattr->mtime;
632 NFS_CACHE_ATIME(inode) = fattr->atime;
633 NFS_CACHE_ISIZE(inode) = fattr->size;
634 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
635 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
637 nfs_refresh_inode(inode, fattr);
641 * In NFSv3 we can have 64bit inode numbers. In order to support
642 * this, and re-exported directories (also seen in NFSv2)
643 * we are forced to allow 2 different inodes to have the same
644 * i_ino.
646 static int
647 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
649 struct nfs_fattr *fattr = (struct nfs_fattr *)opaque;
650 if (NFS_FSID(inode) != fattr->fsid)
651 return 0;
652 if (NFS_FILEID(inode) != fattr->fileid)
653 return 0;
654 return 1;
657 static int
658 nfs_inode_is_stale(struct inode *inode, struct nfs_fattr *fattr)
660 int unhashed;
661 int is_stale = 0;
663 if (inode->i_mode &&
664 (fattr->mode & S_IFMT) != (inode->i_mode & S_IFMT))
665 is_stale = 1;
667 if (is_bad_inode(inode))
668 is_stale = 1;
671 * If the inode seems stale, free up cached dentries.
673 unhashed = nfs_free_dentries(inode);
675 /* Assume we're holding an i_count
677 * NB: sockets sometimes have volatile file handles
678 * don't invalidate their inodes even if all dentries are
679 * unhashed.
681 if (unhashed && atomic_read(&inode->i_count) == unhashed + 1
682 && !S_ISSOCK(inode->i_mode) && !S_ISFIFO(inode->i_mode))
683 is_stale = 1;
685 return is_stale;
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.
694 * We provide a special check for NetApp .snapshot directories to avoid
695 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
697 struct inode *
698 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
699 struct nfs_fattr *fattr)
701 struct super_block *sb = dentry->d_sb;
703 dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
704 dentry->d_parent->d_name.name, dentry->d_name.name,
705 (long long)fattr->fileid);
707 /* Install the file handle in the dentry */
708 memcpy(dentry->d_fsdata, fhandle, sizeof(struct nfs_fh));
710 #ifdef CONFIG_NFS_SNAPSHOT
712 * Check for NetApp snapshot dentries, and get an
713 * unhashed inode to avoid aliasing problems.
715 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
716 (dentry->d_name.len == 9 &&
717 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
718 struct inode *inode = get_empty_inode();
719 if (!inode)
720 goto out;
721 inode->i_sb = sb;
722 inode->i_dev = sb->s_dev;
723 inode->i_flags = 0;
724 inode->i_ino = nfs_fattr_to_ino_t(fattr);
725 nfs_read_inode(inode);
726 nfs_fill_inode(inode, fattr);
727 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
728 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
729 out:
730 return inode;
732 #endif
733 return __nfs_fhget(sb, fattr);
737 * Look up the inode by super block and fattr->fileid.
739 * Note carefully the special handling of busy inodes (i_count > 1).
740 * With the kernel 2.1.xx dcache all inodes except hard links must
741 * have i_count == 1 after iget(). Otherwise, it indicates that the
742 * server has reused a fileid (i_ino) and we have a stale inode.
744 static struct inode *
745 __nfs_fhget(struct super_block *sb, struct nfs_fattr *fattr)
747 struct inode *inode = NULL;
748 unsigned long ino;
750 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
751 goto out_no_inode;
753 if (!fattr->nlink) {
754 printk("NFS: Buggy server - nlink == 0!\n");
755 goto out_no_inode;
758 ino = nfs_fattr_to_ino_t(fattr);
760 while((inode = iget4(sb, ino, nfs_find_actor, fattr)) != NULL) {
763 * Check for busy inodes, and attempt to get rid of any
764 * unused local references. If successful, we release the
765 * inode and try again.
767 * Note that the busy test uses the values in the fattr,
768 * as the inode may have become a different object.
769 * (We can probably handle modes changes here, too.)
771 if (!nfs_inode_is_stale(inode,fattr))
772 break;
774 dprintk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
775 inode->i_ino, atomic_read(&inode->i_count));
776 nfs_zap_caches(inode);
777 remove_inode_hash(inode);
778 iput(inode);
781 if (!inode)
782 goto out_no_inode;
784 nfs_fill_inode(inode, fattr);
785 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
786 inode->i_dev, inode->i_ino, atomic_read(&inode->i_count));
788 out:
789 return inode;
791 out_no_inode:
792 printk("__nfs_fhget: iget failed\n");
793 goto out;
797 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
799 struct inode *inode = dentry->d_inode;
800 struct nfs_fattr fattr;
801 int error;
804 * Make sure the inode is up-to-date.
806 error = nfs_revalidate(dentry);
807 if (error) {
808 #ifdef NFS_PARANOIA
809 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
810 #endif
811 goto out;
814 if (!S_ISREG(inode->i_mode))
815 attr->ia_valid &= ~ATTR_SIZE;
817 error = nfs_wb_all(inode);
818 if (error)
819 goto out;
821 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
822 if (error)
823 goto out;
825 * If we changed the size or mtime, update the inode
826 * now to avoid invalidating the page cache.
828 if (attr->ia_valid & ATTR_SIZE) {
829 if (attr->ia_size != fattr.size)
830 printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
831 (long long) attr->ia_size, (long long)fattr.size);
832 vmtruncate(inode, attr->ia_size);
836 * If we changed the size or mtime, update the inode
837 * now to avoid invalidating the page cache.
839 if (!(fattr.valid & NFS_ATTR_WCC)) {
840 fattr.pre_size = NFS_CACHE_ISIZE(inode);
841 fattr.pre_mtime = NFS_CACHE_MTIME(inode);
842 fattr.pre_ctime = NFS_CACHE_CTIME(inode);
843 fattr.valid |= NFS_ATTR_WCC;
845 error = nfs_refresh_inode(inode, &fattr);
846 out:
847 return error;
851 * Wait for the inode to get unlocked.
852 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
855 nfs_wait_on_inode(struct inode *inode, int flag)
857 struct rpc_clnt *clnt = NFS_CLIENT(inode);
858 int error;
859 if (!(NFS_FLAGS(inode) & flag))
860 return 0;
861 atomic_inc(&inode->i_count);
862 error = nfs_wait_event(clnt, inode->i_wait, !(NFS_FLAGS(inode) & flag));
863 iput(inode);
864 return error;
868 * Externally visible revalidation function
871 nfs_revalidate(struct dentry *dentry)
873 return nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
877 * These are probably going to contain hooks for
878 * allocating and releasing RPC credentials for
879 * the file. I'll have to think about Tronds patch
880 * a bit more..
882 int nfs_open(struct inode *inode, struct file *filp)
884 struct rpc_auth *auth = NFS_CLIENT(inode)->cl_auth;
885 struct rpc_cred *cred = rpcauth_lookupcred(auth, 0);
887 filp->private_data = cred;
888 return 0;
891 int nfs_release(struct inode *inode, struct file *filp)
893 struct rpc_auth *auth = NFS_CLIENT(inode)->cl_auth;
894 struct rpc_cred *cred = nfs_file_cred(filp);
896 if (cred)
897 rpcauth_releasecred(auth, cred);
898 return 0;
902 * This function is called whenever some part of NFS notices that
903 * the cached attributes have to be refreshed.
906 __nfs_revalidate_inode(struct nfs_server *server, struct dentry *dentry)
908 struct inode *inode = dentry->d_inode;
909 int status = 0;
910 struct nfs_fattr fattr;
912 dfprintk(PAGECACHE, "NFS: revalidating %s/%s, ino=%ld\n",
913 dentry->d_parent->d_name.name, dentry->d_name.name,
914 inode->i_ino);
916 if (!inode || is_bad_inode(inode))
917 return -ESTALE;
919 while (NFS_REVALIDATING(inode)) {
920 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
921 if (status < 0)
922 return status;
923 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
924 return 0;
926 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
928 status = NFS_PROTO(inode)->getattr(dentry, &fattr);
929 if (status) {
930 struct dentry *dir = dentry->d_parent;
931 struct inode *dir_i = dir->d_inode;
932 int error;
933 u32 *fh;
934 struct nfs_fh fhandle;
935 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
936 dir->d_name.name, dentry->d_name.name,
937 inode->i_ino, status);
938 if (status != -ESTALE)
939 goto out;
941 * A "stale filehandle" error ... show the current fh
942 * and find out what the filehandle should be.
944 fh = (u32 *) NFS_FH(dentry)->data;
945 dfprintk(PAGECACHE, "NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
946 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
947 error = NFS_PROTO(dir_i)->lookup(dir, &dentry->d_name,
948 &fhandle, &fattr);
949 if (error) {
950 dfprintk(PAGECACHE, "NFS: lookup failed, error=%d\n", error);
951 goto out;
953 fh = (u32 *) fhandle.data;
954 dfprintk(PAGECACHE, " %08x%08x%08x%08x%08x%08x%08x%08x\n",
955 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
956 goto out;
959 status = nfs_refresh_inode(inode, &fattr);
960 if (status) {
961 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
962 dentry->d_parent->d_name.name,
963 dentry->d_name.name, inode->i_ino, status);
964 goto out;
966 dfprintk(PAGECACHE, "NFS: %s/%s revalidation complete\n",
967 dentry->d_parent->d_name.name, dentry->d_name.name);
968 out:
969 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
970 wake_up(&inode->i_wait);
971 return status;
975 * Many nfs protocol calls return the new file attributes after
976 * an operation. Here we update the inode to reflect the state
977 * of the server's inode.
979 * This is a bit tricky because we have to make sure all dirty pages
980 * have been sent off to the server before calling invalidate_inode_pages.
981 * To make sure no other process adds more write requests while we try
982 * our best to flush them, we make them sleep during the attribute refresh.
984 * A very similar scenario holds for the dir cache.
987 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
989 __u64 new_size, new_mtime;
990 loff_t new_isize;
991 int invalid = 0;
992 int error = -EIO;
994 if (!inode || !fattr) {
995 printk(KERN_ERR "nfs_refresh_inode: inode or fattr is NULL\n");
996 goto out;
998 if (inode->i_mode == 0) {
999 printk(KERN_ERR "nfs_refresh_inode: empty inode\n");
1000 goto out;
1003 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1004 goto out;
1006 if (is_bad_inode(inode))
1007 goto out;
1009 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
1010 inode->i_dev, inode->i_ino,
1011 atomic_read(&inode->i_count), fattr->valid);
1014 if (NFS_FSID(inode) != fattr->fsid ||
1015 NFS_FILEID(inode) != fattr->fileid) {
1016 printk(KERN_ERR "nfs_refresh_inode: inode number mismatch\n"
1017 "expected (0x%Lx/0x%Lx), got (0x%Lx/0x%Lx)\n",
1018 (long long)NFS_FSID(inode), (long long)NFS_FILEID(inode),
1019 (long long)fattr->fsid, (long long)fattr->fileid);
1020 goto out;
1024 * Make sure the inode's type hasn't changed.
1026 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1027 goto out_changed;
1029 new_mtime = fattr->mtime;
1030 new_size = fattr->size;
1031 new_isize = nfs_size_to_loff_t(fattr->size);
1033 error = 0;
1036 * Update the read time so we don't revalidate too often.
1038 NFS_READTIME(inode) = jiffies;
1041 * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
1042 * NOT inode->i_size!!!
1044 if (NFS_CACHE_ISIZE(inode) != new_size) {
1045 #ifdef NFS_DEBUG_VERBOSE
1046 printk(KERN_DEBUG "NFS: isize change on %x/%ld\n", inode->i_dev, inode->i_ino);
1047 #endif
1048 invalid = 1;
1052 * Note: we don't check inode->i_mtime since pipes etc.
1053 * can change this value in VFS without requiring a
1054 * cache revalidation.
1056 if (NFS_CACHE_MTIME(inode) != new_mtime) {
1057 #ifdef NFS_DEBUG_VERBOSE
1058 printk(KERN_DEBUG "NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
1059 #endif
1060 invalid = 1;
1063 /* Check Weak Cache Consistency data.
1064 * If size and mtime match the pre-operation values, we can
1065 * assume that any attribute changes were caused by our NFS
1066 * operation, so there's no need to invalidate the caches.
1068 if ((fattr->valid & NFS_ATTR_WCC)
1069 && NFS_CACHE_ISIZE(inode) == fattr->pre_size
1070 && NFS_CACHE_MTIME(inode) == fattr->pre_mtime) {
1071 invalid = 0;
1075 * If we have pending writebacks, things can get
1076 * messy.
1078 if (nfs_have_writebacks(inode) && new_isize < inode->i_size)
1079 new_isize = inode->i_size;
1081 NFS_CACHE_CTIME(inode) = fattr->ctime;
1082 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
1083 /* If we've been messing around with atime, don't
1084 * update it. Save the server value in NFS_CACHE_ATIME.
1086 NFS_CACHE_ATIME(inode) = fattr->atime;
1087 if (time_before(inode->i_atime, nfs_time_to_secs(fattr->atime)))
1088 inode->i_atime = nfs_time_to_secs(fattr->atime);
1090 NFS_CACHE_MTIME(inode) = new_mtime;
1091 inode->i_mtime = nfs_time_to_secs(new_mtime);
1093 NFS_CACHE_ISIZE(inode) = new_size;
1094 inode->i_size = new_isize;
1096 inode->i_mode = fattr->mode;
1097 inode->i_nlink = fattr->nlink;
1098 inode->i_uid = fattr->uid;
1099 inode->i_gid = fattr->gid;
1101 if (fattr->valid & NFS_ATTR_FATTR_V3) {
1103 * report the blocks in 512byte units
1105 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1106 inode->i_blksize = inode->i_sb->s_blocksize;
1107 } else {
1108 inode->i_blocks = fattr->du.nfs2.blocks;
1109 inode->i_blksize = fattr->du.nfs2.blocksize;
1111 inode->i_rdev = 0;
1112 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1113 inode->i_rdev = to_kdev_t(fattr->rdev);
1115 /* Update attrtimeo value */
1116 if (!invalid && time_after(jiffies, NFS_ATTRTIMEO_UPDATE(inode)+NFS_ATTRTIMEO(inode))) {
1117 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
1118 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
1119 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1122 if (invalid)
1123 nfs_zap_caches(inode);
1125 out:
1126 return error;
1128 out_changed:
1130 * Big trouble! The inode has become a different object.
1132 #ifdef NFS_PARANOIA
1133 printk(KERN_DEBUG "nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1134 inode->i_ino, inode->i_mode, fattr->mode);
1135 #endif
1137 * No need to worry about unhashing the dentry, as the
1138 * lookup validation will know that the inode is bad.
1139 * (But we fall through to invalidate the caches.)
1141 nfs_invalidate_inode(inode);
1142 goto out;
1146 * File system information
1148 static DECLARE_FSTYPE(nfs_fs_type, "nfs", nfs_read_super, 0);
1150 extern int nfs_init_fhcache(void);
1151 extern void nfs_destroy_fhcache(void);
1152 extern int nfs_init_nfspagecache(void);
1153 extern void nfs_destroy_nfspagecache(void);
1154 extern int nfs_init_readpagecache(void);
1155 extern int nfs_destroy_readpagecache(void);
1158 * Initialize NFS
1161 init_nfs_fs(void)
1163 int err;
1165 err = nfs_init_fhcache();
1166 if (err)
1167 return err;
1169 err = nfs_init_nfspagecache();
1170 if (err)
1171 return err;
1173 err = nfs_init_readpagecache();
1174 if (err)
1175 return err;
1177 #ifdef CONFIG_PROC_FS
1178 rpc_proc_register(&nfs_rpcstat);
1179 #endif
1180 return register_filesystem(&nfs_fs_type);
1184 * Every kernel module contains stuff like this.
1186 #ifdef MODULE
1188 EXPORT_NO_SYMBOLS;
1189 /* Not quite true; I just maintain it */
1190 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1193 init_module(void)
1195 return init_nfs_fs();
1198 void
1199 cleanup_module(void)
1201 nfs_destroy_readpagecache();
1202 nfs_destroy_nfspagecache();
1203 nfs_destroy_fhcache();
1204 #ifdef CONFIG_PROC_FS
1205 rpc_proc_unregister("nfs");
1206 #endif
1207 unregister_filesystem(&nfs_fs_type);
1209 #endif