Import 2.3.1pre2
[davej-history.git] / fs / nfs / inode.c
blob3b5931eccbacbb06fd1b2d91f462ef222d5a54e4
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>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
35 #define CONFIG_NFS_SNAPSHOT 1
36 #define NFSDBG_FACILITY NFSDBG_VFS
37 #define NFS_PARANOIA 1
39 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fattr *);
40 static void nfs_zap_caches(struct inode *);
41 static void nfs_invalidate_inode(struct inode *);
43 static void nfs_read_inode(struct inode *);
44 static void nfs_put_inode(struct inode *);
45 static void nfs_delete_inode(struct inode *);
46 static int nfs_notify_change(struct dentry *, struct iattr *);
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 nfs_read_inode, /* read inode */
53 NULL, /* write inode */
54 nfs_put_inode, /* put inode */
55 nfs_delete_inode, /* delete inode */
56 nfs_notify_change, /* notify change */
57 nfs_put_super, /* put superblock */
58 NULL, /* write superblock */
59 nfs_statfs, /* stat filesystem */
60 NULL, /* no remount */
61 NULL, /* no clear inode */
62 nfs_umount_begin /* umount attempt begin */
65 struct rpc_stat nfs_rpcstat = { &nfs_program };
68 * The "read_inode" function doesn't actually do anything:
69 * the real data is filled in later in nfs_fhget. Here we
70 * just mark the cache times invalid, and zero out i_mode
71 * (the latter makes "nfs_refresh_inode" do the right thing
72 * wrt pipe inodes)
74 static void
75 nfs_read_inode(struct inode * inode)
77 inode->i_blksize = inode->i_sb->s_blocksize;
78 inode->i_mode = 0;
79 inode->i_rdev = 0;
80 inode->i_op = NULL;
81 NFS_CACHEINV(inode);
82 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
85 static void
86 nfs_put_inode(struct inode * inode)
88 dprintk("NFS: put_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
90 * We want to get rid of unused inodes ...
92 if (inode->i_count == 1)
93 inode->i_nlink = 0;
96 static void
97 nfs_delete_inode(struct inode * inode)
99 int failed;
101 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
103 * Flush out any pending write requests ...
105 if (NFS_WRITEBACK(inode) != NULL) {
106 unsigned long timeout = jiffies + 5*HZ;
107 #ifdef NFS_DEBUG_VERBOSE
108 printk("nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
109 #endif
110 nfs_inval(inode);
111 while (NFS_WRITEBACK(inode) != NULL &&
112 time_before(jiffies, timeout)) {
113 current->state = TASK_INTERRUPTIBLE;
114 schedule_timeout(HZ/10);
116 current->state = TASK_RUNNING;
117 if (NFS_WRITEBACK(inode) != NULL)
118 printk("NFS: Arghhh, stuck RPC requests!\n");
121 failed = nfs_check_failed_request(inode);
122 if (failed)
123 printk("NFS: inode %ld had %d failed requests\n",
124 inode->i_ino, failed);
125 clear_inode(inode);
128 void
129 nfs_put_super(struct super_block *sb)
131 struct nfs_server *server = &sb->u.nfs_sb.s_server;
132 struct rpc_clnt *rpc;
134 if ((rpc = server->client) != NULL)
135 rpc_shutdown_client(rpc);
137 if (!(server->flags & NFS_MOUNT_NONLM))
138 lockd_down(); /* release rpc.lockd */
139 rpciod_down(); /* release rpciod */
141 * Invalidate the dircache for this superblock.
143 nfs_invalidate_dircache_sb(sb);
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;
189 * The way this works is that the mount process passes a structure
190 * in the data argument which contains the server's IP address
191 * and the root file handle obtained from the server's mount
192 * daemon. We stash these away in the private superblock fields.
194 struct super_block *
195 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
197 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
198 struct nfs_server *server;
199 struct rpc_xprt *xprt;
200 struct rpc_clnt *clnt;
201 struct nfs_fh *root_fh;
202 struct inode *root_inode;
203 unsigned int authflavor;
204 int tcp;
205 struct sockaddr_in srvaddr;
206 struct rpc_timeout timeparms;
207 struct nfs_fattr fattr;
209 MOD_INC_USE_COUNT;
210 if (!data)
211 goto out_miss_args;
213 if (data->version != NFS_MOUNT_VERSION) {
214 printk("nfs warning: mount version %s than kernel\n",
215 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
216 if (data->version < 2)
217 data->namlen = 0;
218 if (data->version < 3)
219 data->bsize = 0;
222 /* We now require that the mount process passes the remote address */
223 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
224 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
225 goto out_no_remote;
227 lock_super(sb);
229 sb->s_flags |= MS_ODD_RENAME; /* This should go away */
231 sb->s_magic = NFS_SUPER_MAGIC;
232 sb->s_op = &nfs_sops;
233 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
234 sb->u.nfs_sb.s_root = data->root;
235 server = &sb->u.nfs_sb.s_server;
236 server->rsize = nfs_block_size(data->rsize, NULL);
237 server->wsize = nfs_block_size(data->wsize, NULL);
238 server->flags = data->flags;
240 if (data->flags & NFS_MOUNT_NOAC) {
241 data->acregmin = data->acregmax = 0;
242 data->acdirmin = data->acdirmax = 0;
244 server->acregmin = data->acregmin*HZ;
245 server->acregmax = data->acregmax*HZ;
246 server->acdirmin = data->acdirmin*HZ;
247 server->acdirmax = data->acdirmax*HZ;
249 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
250 if (!server->hostname)
251 goto out_unlock;
252 strcpy(server->hostname, data->hostname);
254 /* Which protocol do we use? */
255 tcp = (data->flags & NFS_MOUNT_TCP);
257 /* Initialize timeout values */
258 timeparms.to_initval = data->timeo * HZ / 10;
259 timeparms.to_retries = data->retrans;
260 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
261 timeparms.to_exponential = 1;
263 /* Now create transport and client */
264 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
265 &srvaddr, &timeparms);
266 if (xprt == NULL)
267 goto out_no_xprt;
269 /* Choose authentication flavor */
270 authflavor = RPC_AUTH_UNIX;
271 if (data->flags & NFS_MOUNT_SECURE)
272 authflavor = RPC_AUTH_DES;
273 else if (data->flags & NFS_MOUNT_KERBEROS)
274 authflavor = RPC_AUTH_KRB;
276 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
277 NFS_VERSION, authflavor);
278 if (clnt == NULL)
279 goto out_no_client;
281 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
282 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
283 clnt->cl_chatty = 1;
284 server->client = clnt;
286 /* Fire up rpciod if not yet running */
287 if (rpciod_up() != 0)
288 goto out_no_iod;
291 * Keep the super block locked while we try to get
292 * the root fh attributes.
294 root_fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
295 if (!root_fh)
296 goto out_no_fh;
297 *root_fh = data->root;
299 if (nfs_proc_getattr(server, root_fh, &fattr) != 0)
300 goto out_no_fattr;
302 root_inode = __nfs_fhget(sb, &fattr);
303 if (!root_inode)
304 goto out_no_root;
305 sb->s_root = d_alloc_root(root_inode, NULL);
306 if (!sb->s_root)
307 goto out_no_root;
308 sb->s_root->d_op = &nfs_dentry_operations;
309 sb->s_root->d_fsdata = root_fh;
311 /* We're airborne */
312 unlock_super(sb);
314 /* Check whether to start the lockd process */
315 if (!(server->flags & NFS_MOUNT_NONLM))
316 lockd_up();
317 return sb;
319 /* Yargs. It didn't work out. */
320 out_no_root:
321 printk("nfs_read_super: get root inode failed\n");
322 iput(root_inode);
323 goto out_free_fh;
325 out_no_fattr:
326 printk("nfs_read_super: get root fattr failed\n");
327 out_free_fh:
328 kfree(root_fh);
329 out_no_fh:
330 rpciod_down();
331 goto out_shutdown;
333 out_no_iod:
334 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
335 out_shutdown:
336 rpc_shutdown_client(server->client);
337 goto out_free_host;
339 out_no_client:
340 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
341 xprt_destroy(xprt);
342 goto out_free_host;
344 out_no_xprt:
345 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
347 out_free_host:
348 kfree(server->hostname);
349 out_unlock:
350 unlock_super(sb);
351 goto out_fail;
353 out_no_remote:
354 printk("NFS: mount program didn't pass remote address!\n");
355 goto out_fail;
357 out_miss_args:
358 printk("nfs_read_super: missing data argument\n");
360 out_fail:
361 sb->s_dev = 0;
362 MOD_DEC_USE_COUNT;
363 return NULL;
366 static int
367 nfs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
369 int error;
370 struct nfs_fsinfo res;
371 struct statfs tmp;
373 error = nfs_proc_statfs(&sb->u.nfs_sb.s_server, &sb->u.nfs_sb.s_root,
374 &res);
375 if (error) {
376 printk("nfs_statfs: statfs error = %d\n", -error);
377 res.bsize = res.blocks = res.bfree = res.bavail = 0;
379 tmp.f_type = NFS_SUPER_MAGIC;
380 tmp.f_bsize = res.bsize;
381 tmp.f_blocks = res.blocks;
382 tmp.f_bfree = res.bfree;
383 tmp.f_bavail = res.bavail;
384 tmp.f_files = 0;
385 tmp.f_ffree = 0;
386 tmp.f_namelen = NAME_MAX;
387 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
391 * Free all unused dentries in an inode's alias list.
393 * Subtle note: we have to be very careful not to cause
394 * any IO operations with the stale dentries, as this
395 * could cause file corruption. But since the dentry
396 * count is 0 and all pending IO for a dentry has been
397 * flushed when the count went to 0, we're safe here.
398 * Also returns the number of unhashed dentries
400 static int
401 nfs_free_dentries(struct inode *inode)
403 struct list_head *tmp, *head = &inode->i_dentry;
404 int unhashed;
406 restart:
407 tmp = head;
408 unhashed = 0;
409 while ((tmp = tmp->next) != head) {
410 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
411 dprintk("nfs_free_dentries: found %s/%s, d_count=%d, hashed=%d\n",
412 dentry->d_parent->d_name.name, dentry->d_name.name,
413 dentry->d_count, !list_empty(&dentry->d_hash));
414 if (!dentry->d_count) {
415 dget(dentry);
416 d_drop(dentry);
417 dput(dentry);
418 goto restart;
420 if (!list_empty(&dentry->d_hash))
421 unhashed++;
423 return unhashed;
427 * Invalidate the local caches
429 static void
430 nfs_zap_caches(struct inode *inode)
432 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
433 NFS_CACHEINV(inode);
435 if (S_ISDIR(inode->i_mode))
436 nfs_invalidate_dircache(inode);
437 else
438 invalidate_inode_pages(inode);
442 * Invalidate, but do not unhash, the inode
444 static void
445 nfs_invalidate_inode(struct inode *inode)
447 umode_t save_mode = inode->i_mode;
449 make_bad_inode(inode);
450 inode->i_mode = save_mode;
451 nfs_inval(inode);
452 nfs_zap_caches(inode);
456 * Fill in inode information from the fattr.
458 static void
459 nfs_fill_inode(struct inode *inode, struct nfs_fattr *fattr)
462 * Check whether the mode has been set, as we only want to
463 * do this once. (We don't allow inodes to change types.)
465 if (inode->i_mode == 0) {
466 inode->i_mode = fattr->mode;
467 if (S_ISREG(inode->i_mode))
468 inode->i_op = &nfs_file_inode_operations;
469 else if (S_ISDIR(inode->i_mode))
470 inode->i_op = &nfs_dir_inode_operations;
471 else if (S_ISLNK(inode->i_mode))
472 inode->i_op = &nfs_symlink_inode_operations;
473 else
474 init_special_inode(inode, inode->i_mode, fattr->rdev);
476 * Preset the size and mtime, as there's no need
477 * to invalidate the caches.
479 inode->i_size = fattr->size;
480 inode->i_mtime = fattr->mtime.seconds;
481 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
483 nfs_refresh_inode(inode, fattr);
487 * This is our own version of iget that looks up inodes by file handle
488 * instead of inode number. We use this technique instead of using
489 * the vfs read_inode function because there is no way to pass the
490 * file handle or current attributes into the read_inode function.
492 * We provide a special check for NetApp .snapshot directories to avoid
493 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
495 struct inode *
496 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
497 struct nfs_fattr *fattr)
499 struct super_block *sb = dentry->d_sb;
501 dprintk("NFS: nfs_fhget(%s/%s fileid=%d)\n",
502 dentry->d_parent->d_name.name, dentry->d_name.name,
503 fattr->fileid);
505 /* Install the file handle in the dentry */
506 *((struct nfs_fh *) dentry->d_fsdata) = *fhandle;
508 #ifdef CONFIG_NFS_SNAPSHOT
510 * Check for NetApp snapshot dentries, and get an
511 * unhashed inode to avoid aliasing problems.
513 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
514 (dentry->d_name.len == 9 &&
515 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
516 struct inode *inode = get_empty_inode();
517 if (!inode)
518 goto out;
519 inode->i_sb = sb;
520 inode->i_dev = sb->s_dev;
521 inode->i_flags = 0;
522 inode->i_ino = fattr->fileid;
523 nfs_read_inode(inode);
524 nfs_fill_inode(inode, fattr);
525 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
526 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
527 out:
528 return inode;
530 #endif
531 return __nfs_fhget(sb, fattr);
535 * Look up the inode by super block and fattr->fileid.
537 * Note carefully the special handling of busy inodes (i_count > 1).
538 * With the kernel 2.1.xx dcache all inodes except hard links must
539 * have i_count == 1 after iget(). Otherwise, it indicates that the
540 * server has reused a fileid (i_ino) and we have a stale inode.
542 static struct inode *
543 __nfs_fhget(struct super_block *sb, struct nfs_fattr *fattr)
545 struct inode *inode;
546 int max_count, stale_inode, unhashed = 0;
548 retry:
549 inode = iget(sb, fattr->fileid);
550 if (!inode)
551 goto out_no_inode;
552 /* N.B. This should be impossible ... */
553 if (inode->i_ino != fattr->fileid)
554 goto out_bad_id;
557 * Check for busy inodes, and attempt to get rid of any
558 * unused local references. If successful, we release the
559 * inode and try again.
561 * Note that the busy test uses the values in the fattr,
562 * as the inode may have become a different object.
563 * (We can probably handle modes changes here, too.)
565 stale_inode = inode->i_mode &&
566 ((fattr->mode ^ inode->i_mode) & S_IFMT);
567 stale_inode |= inode->i_count && inode->i_count == unhashed;
568 max_count = S_ISDIR(fattr->mode) ? 1 : fattr->nlink;
569 if (stale_inode || inode->i_count > max_count + unhashed) {
570 dprintk("__nfs_fhget: inode %ld busy, i_count=%d, i_nlink=%d\n",
571 inode->i_ino, inode->i_count, inode->i_nlink);
572 unhashed = nfs_free_dentries(inode);
573 if (stale_inode || inode->i_count > max_count + unhashed) {
574 printk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
575 inode->i_ino, inode->i_count);
576 if (!list_empty(&inode->i_dentry)) {
577 struct dentry *dentry;
578 dentry = list_entry(inode->i_dentry.next,
579 struct dentry, d_alias);
580 printk("__nfs_fhget: killing %s/%s filehandle\n",
581 dentry->d_parent->d_name.name,
582 dentry->d_name.name);
583 memset(dentry->d_fsdata, 0,
584 sizeof(struct nfs_fh));
586 remove_inode_hash(inode);
587 nfs_invalidate_inode(inode);
588 unhashed = 0;
590 iput(inode);
591 goto retry;
593 nfs_fill_inode(inode, fattr);
594 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
595 inode->i_dev, inode->i_ino, inode->i_count);
597 out:
598 return inode;
600 out_no_inode:
601 printk("__nfs_fhget: iget failed\n");
602 goto out;
603 out_bad_id:
604 printk("__nfs_fhget: unexpected inode from iget\n");
605 goto out;
609 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
611 struct inode *inode = dentry->d_inode;
612 int error;
613 struct nfs_sattr sattr;
614 struct nfs_fattr fattr;
617 * Make sure the inode is up-to-date.
619 error = nfs_revalidate(dentry);
620 if (error) {
621 #ifdef NFS_PARANOIA
622 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
623 #endif
624 goto out;
627 sattr.mode = (u32) -1;
628 if (attr->ia_valid & ATTR_MODE)
629 sattr.mode = attr->ia_mode;
631 sattr.uid = (u32) -1;
632 if (attr->ia_valid & ATTR_UID)
633 sattr.uid = attr->ia_uid;
635 sattr.gid = (u32) -1;
636 if (attr->ia_valid & ATTR_GID)
637 sattr.gid = attr->ia_gid;
639 sattr.size = (u32) -1;
640 if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode))
641 sattr.size = attr->ia_size;
643 sattr.mtime.seconds = sattr.mtime.useconds = (u32) -1;
644 if (attr->ia_valid & ATTR_MTIME) {
645 sattr.mtime.seconds = attr->ia_mtime;
646 sattr.mtime.useconds = 0;
649 sattr.atime.seconds = sattr.atime.useconds = (u32) -1;
650 if (attr->ia_valid & ATTR_ATIME) {
651 sattr.atime.seconds = attr->ia_atime;
652 sattr.atime.useconds = 0;
655 error = nfs_wb_all(inode);
656 if (error)
657 goto out;
659 error = nfs_proc_setattr(NFS_DSERVER(dentry), NFS_FH(dentry),
660 &sattr, &fattr);
661 if (error)
662 goto out;
664 * If we changed the size or mtime, update the inode
665 * now to avoid invalidating the page cache.
667 if (sattr.size != (u32) -1) {
668 if (sattr.size != fattr.size)
669 printk("nfs_notify_change: sattr=%d, fattr=%d??\n",
670 sattr.size, fattr.size);
671 inode->i_size = sattr.size;
672 inode->i_mtime = fattr.mtime.seconds;
674 if (sattr.mtime.seconds != (u32) -1)
675 inode->i_mtime = fattr.mtime.seconds;
676 error = nfs_refresh_inode(inode, &fattr);
677 out:
678 return error;
682 * Externally visible revalidation function
685 nfs_revalidate(struct dentry *dentry)
687 return nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
691 * These are probably going to contain hooks for
692 * allocating and releasing RPC credentials for
693 * the file. I'll have to think about Tronds patch
694 * a bit more..
696 int nfs_open(struct inode *inode, struct file *filp)
698 return 0;
701 int nfs_release(struct inode *inode, struct file *filp)
703 return 0;
707 * This function is called whenever some part of NFS notices that
708 * the cached attributes have to be refreshed.
711 _nfs_revalidate_inode(struct nfs_server *server, struct dentry *dentry)
713 struct inode *inode = dentry->d_inode;
714 int status = 0;
715 struct nfs_fattr fattr;
717 dfprintk(PAGECACHE, "NFS: revalidating %s/%s, ino=%ld\n",
718 dentry->d_parent->d_name.name, dentry->d_name.name,
719 inode->i_ino);
720 status = nfs_proc_getattr(server, NFS_FH(dentry), &fattr);
721 if (status) {
722 int error;
723 u32 *fh;
724 struct nfs_fh fhandle;
725 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
726 dentry->d_parent->d_name.name,
727 dentry->d_name.name, inode->i_ino, status);
728 if (status != -ESTALE)
729 goto out;
731 * A "stale filehandle" error ... show the current fh
732 * and find out what the filehandle should be.
734 fh = (u32 *) NFS_FH(dentry);
735 dfprintk(PAGECACHE, "NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
736 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
737 error = nfs_proc_lookup(server, NFS_FH(dentry->d_parent),
738 dentry->d_name.name, &fhandle, &fattr);
739 if (error) {
740 dfprintk(PAGECACHE, "NFS: lookup failed, error=%d\n", error);
741 goto out;
743 fh = (u32 *) &fhandle;
744 dfprintk(PAGECACHE, " %08x%08x%08x%08x%08x%08x%08x%08x\n",
745 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
746 goto out;
749 status = nfs_refresh_inode(inode, &fattr);
750 if (status) {
751 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
752 dentry->d_parent->d_name.name,
753 dentry->d_name.name, inode->i_ino, status);
754 goto out;
756 dfprintk(PAGECACHE, "NFS: %s/%s revalidation complete\n",
757 dentry->d_parent->d_name.name, dentry->d_name.name);
758 out:
759 return status;
763 * Many nfs protocol calls return the new file attributes after
764 * an operation. Here we update the inode to reflect the state
765 * of the server's inode.
767 * This is a bit tricky because we have to make sure all dirty pages
768 * have been sent off to the server before calling invalidate_inode_pages.
769 * To make sure no other process adds more write requests while we try
770 * our best to flush them, we make them sleep during the attribute refresh.
772 * A very similar scenario holds for the dir cache.
775 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
777 int invalid = 0;
778 int error = -EIO;
780 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d)\n",
781 inode->i_dev, inode->i_ino, inode->i_count);
783 if (!inode || !fattr) {
784 printk("nfs_refresh_inode: inode or fattr is NULL\n");
785 goto out;
787 if (inode->i_ino != fattr->fileid) {
788 printk("nfs_refresh_inode: mismatch, ino=%ld, fattr=%d\n",
789 inode->i_ino, fattr->fileid);
790 goto out;
794 * Make sure the inode's type hasn't changed.
796 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
797 goto out_changed;
799 inode->i_mode = fattr->mode;
800 inode->i_nlink = fattr->nlink;
801 inode->i_uid = fattr->uid;
802 inode->i_gid = fattr->gid;
804 inode->i_blocks = fattr->blocks;
805 inode->i_atime = fattr->atime.seconds;
806 inode->i_ctime = fattr->ctime.seconds;
809 * Update the read time so we don't revalidate too often.
811 NFS_READTIME(inode) = jiffies;
812 error = 0;
815 * If we have pending write-back entries, we don't want
816 * to look at the size or the mtime the server sends us
817 * too closely, as we're in the middle of modifying them.
819 if (NFS_WRITEBACK(inode))
820 goto out;
822 if (inode->i_size != fattr->size) {
823 #ifdef NFS_DEBUG_VERBOSE
824 printk("NFS: size change on %x/%ld\n", inode->i_dev, inode->i_ino);
825 #endif
826 inode->i_size = fattr->size;
827 invalid = 1;
830 if (inode->i_mtime != fattr->mtime.seconds) {
831 #ifdef NFS_DEBUG_VERBOSE
832 printk("NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
833 #endif
834 inode->i_mtime = fattr->mtime.seconds;
835 invalid = 1;
838 if (invalid)
839 goto out_invalid;
841 /* Update attrtimeo value */
842 if (fattr->mtime.seconds == NFS_OLDMTIME(inode)) {
843 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
844 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
846 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
848 out:
849 return error;
851 out_changed:
853 * Big trouble! The inode has become a different object.
855 #ifdef NFS_PARANOIA
856 printk("nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
857 inode->i_ino, inode->i_mode, fattr->mode);
858 #endif
860 * No need to worry about unhashing the dentry, as the
861 * lookup validation will know that the inode is bad.
863 nfs_invalidate_inode(inode);
864 goto out;
866 out_invalid:
867 #ifdef NFS_DEBUG_VERBOSE
868 printk("nfs_refresh_inode: invalidating %ld pages\n", inode->i_nrpages);
869 #endif
870 nfs_zap_caches(inode);
871 goto out;
875 * File system information
877 static struct file_system_type nfs_fs_type = {
878 "nfs",
879 0 /* FS_NO_DCACHE - this doesn't work right now*/,
880 nfs_read_super,
881 NULL
885 * Initialize NFS
888 init_nfs_fs(void)
890 #ifdef CONFIG_PROC_FS
891 rpc_register_sysctl();
892 rpc_proc_init();
893 rpc_proc_register(&nfs_rpcstat);
894 #endif
895 return register_filesystem(&nfs_fs_type);
899 * Every kernel module contains stuff like this.
901 #ifdef MODULE
903 EXPORT_NO_SYMBOLS;
904 /* Not quite true; I just maintain it */
905 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
908 init_module(void)
910 return init_nfs_fs();
913 void
914 cleanup_module(void)
916 #ifdef CONFIG_PROC_FS
917 rpc_proc_unregister("nfs");
918 #endif
919 unregister_filesystem(&nfs_fs_type);
920 nfs_free_dircache();
922 #endif