Import 2.3.48
[davej-history.git] / fs / nfs / inode.c
blob7d2a6c146e7bbd51a19a05a740ade6f47faa9379
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/lockd/bind.h>
31 #include <linux/smp_lock.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
36 #define CONFIG_NFS_SNAPSHOT 1
37 #define NFSDBG_FACILITY NFSDBG_VFS
38 #define NFS_PARANOIA 1
40 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fattr *);
41 void nfs_zap_caches(struct inode *);
42 static void nfs_invalidate_inode(struct inode *);
44 static void nfs_read_inode(struct inode *);
45 static void nfs_put_inode(struct inode *);
46 static void nfs_delete_inode(struct inode *);
47 static void nfs_put_super(struct super_block *);
48 static void nfs_umount_begin(struct super_block *);
49 static int nfs_statfs(struct super_block *, struct statfs *, int);
51 static struct super_operations nfs_sops = {
52 read_inode: nfs_read_inode,
53 put_inode: nfs_put_inode,
54 delete_inode: nfs_delete_inode,
55 put_super: nfs_put_super,
56 statfs: nfs_statfs,
57 umount_begin: nfs_umount_begin,
60 struct rpc_stat nfs_rpcstat = { &nfs_program };
63 * The "read_inode" function doesn't actually do anything:
64 * the real data is filled in later in nfs_fhget. Here we
65 * just mark the cache times invalid, and zero out i_mode
66 * (the latter makes "nfs_refresh_inode" do the right thing
67 * wrt pipe inodes)
69 static void
70 nfs_read_inode(struct inode * inode)
72 inode->i_blksize = inode->i_sb->s_blocksize;
73 inode->i_mode = 0;
74 inode->i_rdev = 0;
75 NFS_FILEID(inode) = 0;
76 NFS_FSID(inode) = 0;
77 NFS_CACHEINV(inode);
78 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
81 static void
82 nfs_put_inode(struct inode * inode)
84 dprintk("NFS: put_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
86 * We want to get rid of unused inodes ...
88 if (inode->i_count == 1)
89 inode->i_nlink = 0;
92 static void
93 nfs_delete_inode(struct inode * inode)
95 int failed;
97 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
99 lock_kernel();
100 if (S_ISDIR(inode->i_mode)) {
101 nfs_free_dircache(inode);
102 } else {
104 * Flush out any pending write requests ...
106 if (NFS_WRITEBACK(inode) != NULL) {
107 unsigned long timeout = jiffies + 5*HZ;
108 #ifdef NFS_DEBUG_VERBOSE
109 printk("nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
110 #endif
111 nfs_inval(inode);
112 while (NFS_WRITEBACK(inode) != NULL &&
113 time_before(jiffies, timeout)) {
114 current->state = TASK_INTERRUPTIBLE;
115 schedule_timeout(HZ/10);
117 current->state = TASK_RUNNING;
118 if (NFS_WRITEBACK(inode) != NULL)
119 printk("NFS: Arghhh, stuck RPC requests!\n");
123 failed = nfs_check_failed_request(inode);
124 if (failed)
125 printk("NFS: inode %ld had %d failed requests\n",
126 inode->i_ino, failed);
127 unlock_kernel();
129 clear_inode(inode);
132 void
133 nfs_put_super(struct super_block *sb)
135 struct nfs_server *server = &sb->u.nfs_sb.s_server;
136 struct rpc_clnt *rpc;
138 if ((rpc = server->client) != NULL)
139 rpc_shutdown_client(rpc);
141 if (!(server->flags & NFS_MOUNT_NONLM))
142 lockd_down(); /* release rpc.lockd */
143 rpciod_down(); /* release rpciod */
145 kfree(server->hostname);
147 MOD_DEC_USE_COUNT;
150 void
151 nfs_umount_begin(struct super_block *sb)
153 struct nfs_server *server = &sb->u.nfs_sb.s_server;
154 struct rpc_clnt *rpc;
156 /* -EIO all pending I/O */
157 if ((rpc = server->client) != NULL)
158 rpc_killall_tasks(rpc);
162 * Compute and set NFS server blocksize
164 static unsigned int
165 nfs_block_size(unsigned int bsize, unsigned char *nrbitsp)
167 if (bsize < 1024)
168 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
169 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
170 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
172 /* make sure blocksize is a power of two */
173 if ((bsize & (bsize - 1)) || nrbitsp) {
174 unsigned int nrbits;
176 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
178 bsize = 1 << nrbits;
179 if (nrbitsp)
180 *nrbitsp = nrbits;
181 if (bsize < NFS_DEF_FILE_IO_BUFFER_SIZE)
182 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
185 return bsize;
188 extern struct nfs_fh *nfs_fh_alloc(void);
189 extern void nfs_fh_free(struct nfs_fh *p);
192 * The way this works is that the mount process passes a structure
193 * in the data argument which contains the server's IP address
194 * and the root file handle obtained from the server's mount
195 * daemon. We stash these away in the private superblock fields.
197 struct super_block *
198 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
200 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
201 struct nfs_server *server;
202 struct rpc_xprt *xprt;
203 struct rpc_clnt *clnt;
204 struct nfs_fh *root_fh;
205 struct inode *root_inode;
206 unsigned int authflavor;
207 int tcp;
208 struct sockaddr_in srvaddr;
209 struct rpc_timeout timeparms;
210 struct nfs_fattr fattr;
212 MOD_INC_USE_COUNT;
213 if (!data)
214 goto out_miss_args;
216 if (data->version != NFS_MOUNT_VERSION) {
217 printk("nfs warning: mount version %s than kernel\n",
218 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
219 if (data->version < 2)
220 data->namlen = 0;
221 if (data->version < 3)
222 data->bsize = 0;
225 /* We now require that the mount process passes the remote address */
226 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
227 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
228 goto out_no_remote;
230 lock_super(sb);
232 sb->s_flags |= MS_ODD_RENAME; /* This should go away */
234 sb->s_magic = NFS_SUPER_MAGIC;
235 sb->s_op = &nfs_sops;
236 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
237 sb->u.nfs_sb.s_root = data->root;
238 server = &sb->u.nfs_sb.s_server;
239 server->rsize = nfs_block_size(data->rsize, NULL);
240 server->wsize = nfs_block_size(data->wsize, NULL);
241 server->flags = data->flags;
243 if (data->flags & NFS_MOUNT_NOAC) {
244 data->acregmin = data->acregmax = 0;
245 data->acdirmin = data->acdirmax = 0;
247 server->acregmin = data->acregmin*HZ;
248 server->acregmax = data->acregmax*HZ;
249 server->acdirmin = data->acdirmin*HZ;
250 server->acdirmax = data->acdirmax*HZ;
252 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
253 if (!server->hostname)
254 goto out_unlock;
255 strcpy(server->hostname, data->hostname);
257 /* Which protocol do we use? */
258 tcp = (data->flags & NFS_MOUNT_TCP);
260 /* Initialize timeout values */
261 timeparms.to_initval = data->timeo * HZ / 10;
262 timeparms.to_retries = data->retrans;
263 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
264 timeparms.to_exponential = 1;
266 /* Now create transport and client */
267 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
268 &srvaddr, &timeparms);
269 if (xprt == NULL)
270 goto out_no_xprt;
272 /* Choose authentication flavor */
273 authflavor = RPC_AUTH_UNIX;
274 if (data->flags & NFS_MOUNT_SECURE)
275 authflavor = RPC_AUTH_DES;
276 else if (data->flags & NFS_MOUNT_KERBEROS)
277 authflavor = RPC_AUTH_KRB;
279 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
280 NFS_VERSION, authflavor);
281 if (clnt == NULL)
282 goto out_no_client;
284 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
285 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
286 clnt->cl_chatty = 1;
287 server->client = clnt;
289 /* Fire up rpciod if not yet running */
290 if (rpciod_up() != 0)
291 goto out_no_iod;
294 * Keep the super block locked while we try to get
295 * the root fh attributes.
297 root_fh = nfs_fh_alloc();
298 if (!root_fh)
299 goto out_no_fh;
300 *root_fh = data->root;
302 if (nfs_proc_getattr(server, root_fh, &fattr) != 0)
303 goto out_no_fattr;
305 root_inode = __nfs_fhget(sb, &fattr);
306 if (!root_inode)
307 goto out_no_root;
308 sb->s_root = d_alloc_root(root_inode);
309 if (!sb->s_root)
310 goto out_no_root;
311 sb->s_root->d_op = &nfs_dentry_operations;
312 sb->s_root->d_fsdata = root_fh;
314 /* We're airborne */
315 unlock_super(sb);
317 /* Check whether to start the lockd process */
318 if (!(server->flags & NFS_MOUNT_NONLM))
319 lockd_up();
320 return sb;
322 /* Yargs. It didn't work out. */
323 out_no_root:
324 printk("nfs_read_super: get root inode failed\n");
325 iput(root_inode);
326 goto out_free_fh;
328 out_no_fattr:
329 printk("nfs_read_super: get root fattr failed\n");
330 out_free_fh:
331 nfs_fh_free(root_fh);
332 out_no_fh:
333 rpciod_down();
334 goto out_shutdown;
336 out_no_iod:
337 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
338 out_shutdown:
339 rpc_shutdown_client(server->client);
340 goto out_free_host;
342 out_no_client:
343 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
344 xprt_destroy(xprt);
345 goto out_free_host;
347 out_no_xprt:
348 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
350 out_free_host:
351 kfree(server->hostname);
352 out_unlock:
353 unlock_super(sb);
354 goto out_fail;
356 out_no_remote:
357 printk("NFS: mount program didn't pass remote address!\n");
358 goto out_fail;
360 out_miss_args:
361 printk("nfs_read_super: missing data argument\n");
363 out_fail:
364 sb->s_dev = 0;
365 MOD_DEC_USE_COUNT;
366 return NULL;
369 static int
370 nfs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
372 int error;
373 struct nfs_fsinfo res;
374 struct statfs tmp;
376 error = nfs_proc_statfs(&sb->u.nfs_sb.s_server, &sb->u.nfs_sb.s_root,
377 &res);
378 if (error) {
379 printk("nfs_statfs: statfs error = %d\n", -error);
380 res.bsize = res.blocks = res.bfree = res.bavail = 0;
382 tmp.f_type = NFS_SUPER_MAGIC;
383 tmp.f_bsize = res.bsize;
384 tmp.f_blocks = res.blocks;
385 tmp.f_bfree = res.bfree;
386 tmp.f_bavail = res.bavail;
387 tmp.f_files = 0;
388 tmp.f_ffree = 0;
389 tmp.f_namelen = NAME_MAX;
390 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
394 * Free all unused dentries in an inode's alias list.
396 * Subtle note: we have to be very careful not to cause
397 * any IO operations with the stale dentries, as this
398 * could cause file corruption. But since the dentry
399 * count is 0 and all pending IO for a dentry has been
400 * flushed when the count went to 0, we're safe here.
401 * Also returns the number of unhashed dentries
403 static int
404 nfs_free_dentries(struct inode *inode)
406 struct list_head *tmp, *head = &inode->i_dentry;
407 int unhashed;
409 restart:
410 tmp = head;
411 unhashed = 0;
412 while ((tmp = tmp->next) != head) {
413 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
414 dprintk("nfs_free_dentries: found %s/%s, d_count=%d, hashed=%d\n",
415 dentry->d_parent->d_name.name, dentry->d_name.name,
416 dentry->d_count, !list_empty(&dentry->d_hash));
417 if (!list_empty(&dentry->d_subdirs))
418 shrink_dcache_parent(dentry);
419 if (!dentry->d_count) {
420 dget(dentry);
421 d_drop(dentry);
422 dput(dentry);
423 goto restart;
425 if (list_empty(&dentry->d_hash))
426 unhashed++;
428 return unhashed;
432 * Invalidate the local caches
434 void
435 nfs_zap_caches(struct inode *inode)
437 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
438 NFS_CACHEINV(inode);
440 invalidate_inode_pages(inode);
441 if (S_ISDIR(inode->i_mode))
442 nfs_flush_dircache(inode);
446 * Invalidate, but do not unhash, the inode
448 static void
449 nfs_invalidate_inode(struct inode *inode)
451 umode_t save_mode = inode->i_mode;
453 make_bad_inode(inode);
454 inode->i_mode = save_mode;
455 nfs_inval(inode);
456 nfs_zap_caches(inode);
460 * Fill in inode information from the fattr.
462 static void
463 nfs_fill_inode(struct inode *inode, struct nfs_fattr *fattr)
466 * Check whether the mode has been set, as we only want to
467 * do this once. (We don't allow inodes to change types.)
469 if (inode->i_mode == 0) {
470 NFS_FILEID(inode) = fattr->fileid;
471 NFS_FSID(inode) = fattr->fsid;
472 inode->i_mode = fattr->mode;
473 /* Why so? Because we want revalidate for devices/FIFOs, and
474 * that's precisely what we have in nfs_file_inode_operations.
476 inode->i_op = &nfs_file_inode_operations;
477 if (S_ISREG(inode->i_mode)) {
478 inode->i_fop = &nfs_file_operations;
479 inode->i_data.a_ops = &nfs_file_aops;
480 } else if (S_ISDIR(inode->i_mode)) {
481 inode->i_op = &nfs_dir_inode_operations;
482 inode->i_fop = &nfs_dir_operations;
483 } else if (S_ISLNK(inode->i_mode))
484 inode->i_op = &nfs_symlink_inode_operations;
485 else
486 init_special_inode(inode, inode->i_mode, fattr->rdev);
488 * Preset the size and mtime, as there's no need
489 * to invalidate the caches.
491 inode->i_size = fattr->size;
492 inode->i_mtime = fattr->mtime.seconds;
493 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
495 nfs_refresh_inode(inode, fattr);
499 * In NFSv3 we can have 64bit inode numbers. In order to support
500 * this, and re-exported directories (also seen in NFSv2)
501 * we are forced to allow 2 different inodes to have the same
502 * i_ino.
504 static int
505 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
507 struct nfs_fattr *fattr = (struct nfs_fattr *)opaque;
508 if (NFS_FSID(inode) != fattr->fsid)
509 return 0;
510 if (NFS_FILEID(inode) != fattr->fileid)
511 return 0;
512 return 1;
515 static int
516 nfs_inode_is_stale(struct inode *inode, struct nfs_fattr *fattr)
518 int unhashed;
519 int is_stale = 0;
521 if (inode->i_mode &&
522 (fattr->mode & S_IFMT) != (inode->i_mode & S_IFMT))
523 is_stale = 1;
525 if (is_bad_inode(inode))
526 is_stale = 1;
529 * If the inode seems stale, free up cached dentries.
531 unhashed = nfs_free_dentries(inode);
533 /* Assume we're holding an i_count
535 * NB: sockets sometimes have volatile file handles
536 * don't invalidate their inodes even if all dentries are
537 * unhashed.
539 if (unhashed && inode->i_count == unhashed + 1
540 && !S_ISSOCK(inode->i_mode) && !S_ISFIFO(inode->i_mode))
541 is_stale = 1;
543 return is_stale;
547 * This is our own version of iget that looks up inodes by file handle
548 * instead of inode number. We use this technique instead of using
549 * the vfs read_inode function because there is no way to pass the
550 * file handle or current attributes into the read_inode function.
552 * We provide a special check for NetApp .snapshot directories to avoid
553 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
555 struct inode *
556 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
557 struct nfs_fattr *fattr)
559 struct super_block *sb = dentry->d_sb;
561 dprintk("NFS: nfs_fhget(%s/%s fileid=%d)\n",
562 dentry->d_parent->d_name.name, dentry->d_name.name,
563 fattr->fileid);
565 /* Install the file handle in the dentry */
566 *((struct nfs_fh *) dentry->d_fsdata) = *fhandle;
568 #ifdef CONFIG_NFS_SNAPSHOT
570 * Check for NetApp snapshot dentries, and get an
571 * unhashed inode to avoid aliasing problems.
573 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
574 (dentry->d_name.len == 9 &&
575 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
576 struct inode *inode = get_empty_inode();
577 if (!inode)
578 goto out;
579 inode->i_sb = sb;
580 inode->i_dev = sb->s_dev;
581 inode->i_flags = 0;
582 inode->i_ino = fattr->fileid;
583 nfs_read_inode(inode);
584 nfs_fill_inode(inode, fattr);
585 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
586 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
587 out:
588 return inode;
590 #endif
591 return __nfs_fhget(sb, fattr);
595 * Look up the inode by super block and fattr->fileid.
597 * Note carefully the special handling of busy inodes (i_count > 1).
598 * With the kernel 2.1.xx dcache all inodes except hard links must
599 * have i_count == 1 after iget(). Otherwise, it indicates that the
600 * server has reused a fileid (i_ino) and we have a stale inode.
602 static struct inode *
603 __nfs_fhget(struct super_block *sb, struct nfs_fattr *fattr)
605 struct inode *inode = NULL;
606 unsigned long ino;
608 if (!fattr->nlink) {
609 printk("NFS: Buggy server - nlink == 0!\n");
610 goto out_no_inode;
613 ino = fattr->fileid;
615 while((inode = iget4(sb, ino, nfs_find_actor, fattr)) != NULL) {
618 * Check for busy inodes, and attempt to get rid of any
619 * unused local references. If successful, we release the
620 * inode and try again.
622 * Note that the busy test uses the values in the fattr,
623 * as the inode may have become a different object.
624 * (We can probably handle modes changes here, too.)
626 if (!nfs_inode_is_stale(inode,fattr))
627 break;
629 dprintk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
630 inode->i_ino, inode->i_count);
631 nfs_zap_caches(inode);
632 remove_inode_hash(inode);
633 iput(inode);
636 if (!inode)
637 goto out_no_inode;
639 nfs_fill_inode(inode, fattr);
640 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
641 inode->i_dev, inode->i_ino, inode->i_count);
643 out:
644 return inode;
646 out_no_inode:
647 printk("__nfs_fhget: iget failed\n");
648 goto out;
652 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
654 struct inode *inode = dentry->d_inode;
655 struct nfs_fattr fattr;
656 int error;
659 * Make sure the inode is up-to-date.
661 error = nfs_revalidate(dentry);
662 if (error) {
663 #ifdef NFS_PARANOIA
664 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
665 #endif
666 goto out;
669 if (!S_ISREG(inode->i_mode))
670 attr->ia_valid &= ~ATTR_SIZE;
672 error = nfs_wb_all(inode);
673 if (error)
674 goto out;
676 error = nfs_proc_setattr(NFS_DSERVER(dentry), NFS_FH(dentry),
677 &fattr, attr);
678 if (error)
679 goto out;
681 * If we changed the size or mtime, update the inode
682 * now to avoid invalidating the page cache.
684 if (attr->ia_valid & ATTR_SIZE) {
685 if (attr->ia_size != fattr.size)
686 printk("nfs_notify_change: attr=%Ld, fattr=%d??\n",
687 (long long) attr->ia_size, fattr.size);
688 inode->i_mtime = fattr.mtime.seconds;
689 vmtruncate(inode, attr->ia_size);
691 if (attr->ia_valid & ATTR_MTIME)
692 inode->i_mtime = fattr.mtime.seconds;
693 error = nfs_refresh_inode(inode, &fattr);
694 out:
695 return error;
699 * Wait for the inode to get unlocked.
700 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
703 nfs_wait_on_inode(struct inode *inode, int flag)
705 struct task_struct *tsk = current;
706 DECLARE_WAITQUEUE(wait, tsk);
707 int intr, error = 0;
709 intr = NFS_SERVER(inode)->flags & NFS_MOUNT_INTR;
710 add_wait_queue(&inode->i_wait, &wait);
711 for (;;) {
712 set_task_state(tsk, (intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE));
713 error = 0;
714 if (!(NFS_FLAGS(inode) & flag))
715 break;
716 error = -ERESTARTSYS;
717 if (intr && signalled())
718 break;
719 schedule();
721 set_task_state(tsk, TASK_RUNNING);
722 remove_wait_queue(&inode->i_wait, &wait);
723 return error;
727 * Externally visible revalidation function
730 nfs_revalidate(struct dentry *dentry)
732 return nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
736 * These are probably going to contain hooks for
737 * allocating and releasing RPC credentials for
738 * the file. I'll have to think about Tronds patch
739 * a bit more..
741 int nfs_open(struct inode *inode, struct file *filp)
743 return 0;
746 int nfs_release(struct inode *inode, struct file *filp)
748 return 0;
752 * This function is called whenever some part of NFS notices that
753 * the cached attributes have to be refreshed.
756 __nfs_revalidate_inode(struct nfs_server *server, struct dentry *dentry)
758 struct inode *inode = dentry->d_inode;
759 int status = 0;
760 struct nfs_fattr fattr;
762 dfprintk(PAGECACHE, "NFS: revalidating %s/%s, ino=%ld\n",
763 dentry->d_parent->d_name.name, dentry->d_name.name,
764 inode->i_ino);
766 if (!inode || is_bad_inode(inode))
767 return -ESTALE;
769 while (NFS_REVALIDATING(inode)) {
770 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
771 if (status < 0)
772 return status;
773 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
774 return 0;
776 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
778 status = nfs_proc_getattr(server, NFS_FH(dentry), &fattr);
779 if (status) {
780 int error;
781 u32 *fh;
782 struct nfs_fh fhandle;
783 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
784 dentry->d_parent->d_name.name,
785 dentry->d_name.name, inode->i_ino, status);
786 if (status != -ESTALE)
787 goto out;
789 * A "stale filehandle" error ... show the current fh
790 * and find out what the filehandle should be.
792 fh = (u32 *) NFS_FH(dentry);
793 dfprintk(PAGECACHE, "NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
794 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
795 error = nfs_proc_lookup(server, NFS_FH(dentry->d_parent),
796 dentry->d_name.name, &fhandle, &fattr);
797 if (error) {
798 dfprintk(PAGECACHE, "NFS: lookup failed, error=%d\n", error);
799 goto out;
801 fh = (u32 *) &fhandle;
802 dfprintk(PAGECACHE, " %08x%08x%08x%08x%08x%08x%08x%08x\n",
803 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
804 goto out;
807 status = nfs_refresh_inode(inode, &fattr);
808 if (status) {
809 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
810 dentry->d_parent->d_name.name,
811 dentry->d_name.name, inode->i_ino, status);
812 goto out;
814 dfprintk(PAGECACHE, "NFS: %s/%s revalidation complete\n",
815 dentry->d_parent->d_name.name, dentry->d_name.name);
816 out:
817 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
818 wake_up(&inode->i_wait);
819 return status;
823 * Many nfs protocol calls return the new file attributes after
824 * an operation. Here we update the inode to reflect the state
825 * of the server's inode.
827 * This is a bit tricky because we have to make sure all dirty pages
828 * have been sent off to the server before calling invalidate_inode_pages.
829 * To make sure no other process adds more write requests while we try
830 * our best to flush them, we make them sleep during the attribute refresh.
832 * A very similar scenario holds for the dir cache.
835 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
837 int invalid = 0;
838 int error = -EIO;
840 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d)\n",
841 inode->i_dev, inode->i_ino, inode->i_count);
843 if (!inode || !fattr) {
844 printk("nfs_refresh_inode: inode or fattr is NULL\n");
845 goto out;
847 if (inode->i_ino != fattr->fileid) {
848 printk("nfs_refresh_inode: mismatch, ino=%ld, fattr=%d\n",
849 inode->i_ino, fattr->fileid);
850 goto out;
854 * Make sure the inode's type hasn't changed.
856 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
857 goto out_changed;
859 inode->i_mode = fattr->mode;
860 inode->i_nlink = fattr->nlink;
861 inode->i_uid = fattr->uid;
862 inode->i_gid = fattr->gid;
864 inode->i_blocks = fattr->blocks;
865 inode->i_atime = fattr->atime.seconds;
866 inode->i_ctime = fattr->ctime.seconds;
869 * Update the read time so we don't revalidate too often.
871 NFS_READTIME(inode) = jiffies;
872 error = 0;
875 * If we have pending write-back entries, we don't want
876 * to look at the size or the mtime the server sends us
877 * too closely, as we're in the middle of modifying them.
879 if (NFS_WRITEBACK(inode))
880 goto out;
882 if (inode->i_size != fattr->size) {
883 #ifdef NFS_DEBUG_VERBOSE
884 printk("NFS: size change on %x/%ld\n", inode->i_dev, inode->i_ino);
885 #endif
886 inode->i_size = fattr->size;
887 invalid = 1;
890 if (inode->i_mtime != fattr->mtime.seconds) {
891 #ifdef NFS_DEBUG_VERBOSE
892 printk("NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
893 #endif
894 inode->i_mtime = fattr->mtime.seconds;
895 invalid = 1;
898 if (invalid)
899 goto out_invalid;
901 /* Update attrtimeo value */
902 if (fattr->mtime.seconds == NFS_OLDMTIME(inode)) {
903 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
904 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
906 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
908 out:
909 return error;
911 out_changed:
913 * Big trouble! The inode has become a different object.
915 #ifdef NFS_PARANOIA
916 printk("nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
917 inode->i_ino, inode->i_mode, fattr->mode);
918 #endif
920 * No need to worry about unhashing the dentry, as the
921 * lookup validation will know that the inode is bad.
923 nfs_invalidate_inode(inode);
924 goto out;
926 out_invalid:
927 #ifdef NFS_DEBUG_VERBOSE
928 printk("nfs_refresh_inode: invalidating %ld pages\n", inode->i_nrpages);
929 #endif
930 nfs_zap_caches(inode);
931 goto out;
935 * File system information
937 static struct file_system_type nfs_fs_type = {
938 "nfs",
939 0 /* FS_NO_DCACHE - this doesn't work right now*/,
940 nfs_read_super,
941 NULL
944 extern int nfs_init_fhcache(void);
945 extern int nfs_init_wreqcache(void);
948 * Initialize NFS
951 init_nfs_fs(void)
953 int err;
955 err = nfs_init_fhcache();
956 if (err)
957 return err;
959 err = nfs_init_wreqcache();
960 if (err)
961 return err;
963 #ifdef CONFIG_PROC_FS
964 rpc_proc_register(&nfs_rpcstat);
965 #endif
966 return register_filesystem(&nfs_fs_type);
970 * Every kernel module contains stuff like this.
972 #ifdef MODULE
974 EXPORT_NO_SYMBOLS;
975 /* Not quite true; I just maintain it */
976 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
979 init_module(void)
981 return init_nfs_fs();
984 void
985 cleanup_module(void)
987 #ifdef CONFIG_PROC_FS
988 rpc_proc_unregister("nfs");
989 #endif
990 unregister_filesystem(&nfs_fs_type);
992 #endif