* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / nfs / inode.c
blob5421cebf99a8079cf0bceda607c9f615b41e61f4
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 if (S_ISDIR(inode->i_mode)) {
104 nfs_free_dircache(inode);
105 } else {
107 * Flush out any pending write requests ...
109 if (NFS_WRITEBACK(inode) != NULL) {
110 unsigned long timeout = jiffies + 5*HZ;
111 #ifdef NFS_DEBUG_VERBOSE
112 printk("nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
113 #endif
114 nfs_inval(inode);
115 while (NFS_WRITEBACK(inode) != NULL &&
116 time_before(jiffies, timeout)) {
117 current->state = TASK_INTERRUPTIBLE;
118 schedule_timeout(HZ/10);
120 current->state = TASK_RUNNING;
121 if (NFS_WRITEBACK(inode) != NULL)
122 printk("NFS: Arghhh, stuck RPC requests!\n");
126 failed = nfs_check_failed_request(inode);
127 if (failed)
128 printk("NFS: inode %ld had %d failed requests\n",
129 inode->i_ino, failed);
130 clear_inode(inode);
133 void
134 nfs_put_super(struct super_block *sb)
136 struct nfs_server *server = &sb->u.nfs_sb.s_server;
137 struct rpc_clnt *rpc;
139 if ((rpc = server->client) != NULL)
140 rpc_shutdown_client(rpc);
142 if (!(server->flags & NFS_MOUNT_NONLM))
143 lockd_down(); /* release rpc.lockd */
144 rpciod_down(); /* release rpciod */
146 kfree(server->hostname);
148 MOD_DEC_USE_COUNT;
151 void
152 nfs_umount_begin(struct super_block *sb)
154 struct nfs_server *server = &sb->u.nfs_sb.s_server;
155 struct rpc_clnt *rpc;
157 /* -EIO all pending I/O */
158 if ((rpc = server->client) != NULL)
159 rpc_killall_tasks(rpc);
163 * Compute and set NFS server blocksize
165 static unsigned int
166 nfs_block_size(unsigned int bsize, unsigned char *nrbitsp)
168 if (bsize < 1024)
169 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
170 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
171 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
173 /* make sure blocksize is a power of two */
174 if ((bsize & (bsize - 1)) || nrbitsp) {
175 unsigned int nrbits;
177 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
179 bsize = 1 << nrbits;
180 if (nrbitsp)
181 *nrbitsp = nrbits;
182 if (bsize < NFS_DEF_FILE_IO_BUFFER_SIZE)
183 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
186 return bsize;
189 extern struct nfs_fh *nfs_fh_alloc(void);
190 extern void nfs_fh_free(struct nfs_fh *p);
193 * The way this works is that the mount process passes a structure
194 * in the data argument which contains the server's IP address
195 * and the root file handle obtained from the server's mount
196 * daemon. We stash these away in the private superblock fields.
198 struct super_block *
199 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
201 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
202 struct nfs_server *server;
203 struct rpc_xprt *xprt;
204 struct rpc_clnt *clnt;
205 struct nfs_fh *root_fh;
206 struct inode *root_inode;
207 unsigned int authflavor;
208 int tcp;
209 struct sockaddr_in srvaddr;
210 struct rpc_timeout timeparms;
211 struct nfs_fattr fattr;
213 MOD_INC_USE_COUNT;
214 if (!data)
215 goto out_miss_args;
217 if (data->version != NFS_MOUNT_VERSION) {
218 printk("nfs warning: mount version %s than kernel\n",
219 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
220 if (data->version < 2)
221 data->namlen = 0;
222 if (data->version < 3)
223 data->bsize = 0;
226 /* We now require that the mount process passes the remote address */
227 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
228 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
229 goto out_no_remote;
231 lock_super(sb);
233 sb->s_flags |= MS_ODD_RENAME; /* This should go away */
235 sb->s_magic = NFS_SUPER_MAGIC;
236 sb->s_op = &nfs_sops;
237 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
238 sb->u.nfs_sb.s_root = data->root;
239 server = &sb->u.nfs_sb.s_server;
240 server->rsize = nfs_block_size(data->rsize, NULL);
241 server->wsize = nfs_block_size(data->wsize, NULL);
242 server->flags = data->flags;
244 if (data->flags & NFS_MOUNT_NOAC) {
245 data->acregmin = data->acregmax = 0;
246 data->acdirmin = data->acdirmax = 0;
248 server->acregmin = data->acregmin*HZ;
249 server->acregmax = data->acregmax*HZ;
250 server->acdirmin = data->acdirmin*HZ;
251 server->acdirmax = data->acdirmax*HZ;
253 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
254 if (!server->hostname)
255 goto out_unlock;
256 strcpy(server->hostname, data->hostname);
258 /* Which protocol do we use? */
259 tcp = (data->flags & NFS_MOUNT_TCP);
261 /* Initialize timeout values */
262 timeparms.to_initval = data->timeo * HZ / 10;
263 timeparms.to_retries = data->retrans;
264 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
265 timeparms.to_exponential = 1;
267 /* Now create transport and client */
268 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
269 &srvaddr, &timeparms);
270 if (xprt == NULL)
271 goto out_no_xprt;
273 /* Choose authentication flavor */
274 authflavor = RPC_AUTH_UNIX;
275 if (data->flags & NFS_MOUNT_SECURE)
276 authflavor = RPC_AUTH_DES;
277 else if (data->flags & NFS_MOUNT_KERBEROS)
278 authflavor = RPC_AUTH_KRB;
280 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
281 NFS_VERSION, authflavor);
282 if (clnt == NULL)
283 goto out_no_client;
285 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
286 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
287 clnt->cl_chatty = 1;
288 server->client = clnt;
290 /* Fire up rpciod if not yet running */
291 if (rpciod_up() != 0)
292 goto out_no_iod;
295 * Keep the super block locked while we try to get
296 * the root fh attributes.
298 root_fh = nfs_fh_alloc();
299 if (!root_fh)
300 goto out_no_fh;
301 *root_fh = data->root;
303 if (nfs_proc_getattr(server, root_fh, &fattr) != 0)
304 goto out_no_fattr;
306 root_inode = __nfs_fhget(sb, &fattr);
307 if (!root_inode)
308 goto out_no_root;
309 sb->s_root = d_alloc_root(root_inode);
310 if (!sb->s_root)
311 goto out_no_root;
312 sb->s_root->d_op = &nfs_dentry_operations;
313 sb->s_root->d_fsdata = root_fh;
315 /* We're airborne */
316 unlock_super(sb);
318 /* Check whether to start the lockd process */
319 if (!(server->flags & NFS_MOUNT_NONLM))
320 lockd_up();
321 return sb;
323 /* Yargs. It didn't work out. */
324 out_no_root:
325 printk("nfs_read_super: get root inode failed\n");
326 iput(root_inode);
327 goto out_free_fh;
329 out_no_fattr:
330 printk("nfs_read_super: get root fattr failed\n");
331 out_free_fh:
332 nfs_fh_free(root_fh);
333 out_no_fh:
334 rpciod_down();
335 goto out_shutdown;
337 out_no_iod:
338 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
339 out_shutdown:
340 rpc_shutdown_client(server->client);
341 goto out_free_host;
343 out_no_client:
344 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
345 xprt_destroy(xprt);
346 goto out_free_host;
348 out_no_xprt:
349 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
351 out_free_host:
352 kfree(server->hostname);
353 out_unlock:
354 unlock_super(sb);
355 goto out_fail;
357 out_no_remote:
358 printk("NFS: mount program didn't pass remote address!\n");
359 goto out_fail;
361 out_miss_args:
362 printk("nfs_read_super: missing data argument\n");
364 out_fail:
365 sb->s_dev = 0;
366 MOD_DEC_USE_COUNT;
367 return NULL;
370 static int
371 nfs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
373 int error;
374 struct nfs_fsinfo res;
375 struct statfs tmp;
377 error = nfs_proc_statfs(&sb->u.nfs_sb.s_server, &sb->u.nfs_sb.s_root,
378 &res);
379 if (error) {
380 printk("nfs_statfs: statfs error = %d\n", -error);
381 res.bsize = res.blocks = res.bfree = res.bavail = 0;
383 tmp.f_type = NFS_SUPER_MAGIC;
384 tmp.f_bsize = res.bsize;
385 tmp.f_blocks = res.blocks;
386 tmp.f_bfree = res.bfree;
387 tmp.f_bavail = res.bavail;
388 tmp.f_files = 0;
389 tmp.f_ffree = 0;
390 tmp.f_namelen = NAME_MAX;
391 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
395 * Free all unused dentries in an inode's alias list.
397 * Subtle note: we have to be very careful not to cause
398 * any IO operations with the stale dentries, as this
399 * could cause file corruption. But since the dentry
400 * count is 0 and all pending IO for a dentry has been
401 * flushed when the count went to 0, we're safe here.
402 * Also returns the number of unhashed dentries
404 static int
405 nfs_free_dentries(struct inode *inode)
407 struct list_head *tmp, *head = &inode->i_dentry;
408 int unhashed;
410 restart:
411 tmp = head;
412 unhashed = 0;
413 while ((tmp = tmp->next) != head) {
414 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
415 dprintk("nfs_free_dentries: found %s/%s, d_count=%d, hashed=%d\n",
416 dentry->d_parent->d_name.name, dentry->d_name.name,
417 dentry->d_count, !list_empty(&dentry->d_hash));
418 if (!dentry->d_count) {
419 dget(dentry);
420 d_drop(dentry);
421 dput(dentry);
422 goto restart;
424 if (!list_empty(&dentry->d_hash))
425 unhashed++;
427 return unhashed;
431 * Invalidate the local caches
433 static void
434 nfs_zap_caches(struct inode *inode)
436 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
437 NFS_CACHEINV(inode);
439 invalidate_inode_pages(inode);
440 if (S_ISDIR(inode->i_mode))
441 nfs_flush_dircache(inode);
445 * Invalidate, but do not unhash, the inode
447 static void
448 nfs_invalidate_inode(struct inode *inode)
450 umode_t save_mode = inode->i_mode;
452 make_bad_inode(inode);
453 inode->i_mode = save_mode;
454 nfs_inval(inode);
455 nfs_zap_caches(inode);
459 * Fill in inode information from the fattr.
461 static void
462 nfs_fill_inode(struct inode *inode, struct nfs_fattr *fattr)
465 * Check whether the mode has been set, as we only want to
466 * do this once. (We don't allow inodes to change types.)
468 if (inode->i_mode == 0) {
469 inode->i_mode = fattr->mode;
470 if (S_ISREG(inode->i_mode))
471 inode->i_op = &nfs_file_inode_operations;
472 else if (S_ISDIR(inode->i_mode))
473 inode->i_op = &nfs_dir_inode_operations;
474 else if (S_ISLNK(inode->i_mode))
475 inode->i_op = &nfs_symlink_inode_operations;
476 else
477 init_special_inode(inode, inode->i_mode, fattr->rdev);
479 * Preset the size and mtime, as there's no need
480 * to invalidate the caches.
482 inode->i_size = fattr->size;
483 inode->i_mtime = fattr->mtime.seconds;
484 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
486 nfs_refresh_inode(inode, fattr);
490 * This is our own version of iget that looks up inodes by file handle
491 * instead of inode number. We use this technique instead of using
492 * the vfs read_inode function because there is no way to pass the
493 * file handle or current attributes into the read_inode function.
495 * We provide a special check for NetApp .snapshot directories to avoid
496 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
498 struct inode *
499 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
500 struct nfs_fattr *fattr)
502 struct super_block *sb = dentry->d_sb;
504 dprintk("NFS: nfs_fhget(%s/%s fileid=%d)\n",
505 dentry->d_parent->d_name.name, dentry->d_name.name,
506 fattr->fileid);
508 /* Install the file handle in the dentry */
509 *((struct nfs_fh *) dentry->d_fsdata) = *fhandle;
511 #ifdef CONFIG_NFS_SNAPSHOT
513 * Check for NetApp snapshot dentries, and get an
514 * unhashed inode to avoid aliasing problems.
516 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
517 (dentry->d_name.len == 9 &&
518 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
519 struct inode *inode = get_empty_inode();
520 if (!inode)
521 goto out;
522 inode->i_sb = sb;
523 inode->i_dev = sb->s_dev;
524 inode->i_flags = 0;
525 inode->i_ino = fattr->fileid;
526 nfs_read_inode(inode);
527 nfs_fill_inode(inode, fattr);
528 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
529 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
530 out:
531 return inode;
533 #endif
534 return __nfs_fhget(sb, fattr);
538 * Look up the inode by super block and fattr->fileid.
540 * Note carefully the special handling of busy inodes (i_count > 1).
541 * With the kernel 2.1.xx dcache all inodes except hard links must
542 * have i_count == 1 after iget(). Otherwise, it indicates that the
543 * server has reused a fileid (i_ino) and we have a stale inode.
545 static struct inode *
546 __nfs_fhget(struct super_block *sb, struct nfs_fattr *fattr)
548 struct inode *inode;
549 int max_count, stale_inode, unhashed = 0;
551 retry:
552 inode = iget(sb, fattr->fileid);
553 if (!inode)
554 goto out_no_inode;
555 /* N.B. This should be impossible ... */
556 if (inode->i_ino != fattr->fileid)
557 goto out_bad_id;
560 * Check for busy inodes, and attempt to get rid of any
561 * unused local references. If successful, we release the
562 * inode and try again.
564 * Note that the busy test uses the values in the fattr,
565 * as the inode may have become a different object.
566 * (We can probably handle modes changes here, too.)
568 stale_inode = inode->i_mode &&
569 ((fattr->mode ^ inode->i_mode) & S_IFMT);
570 stale_inode |= inode->i_count && inode->i_count == unhashed;
571 max_count = S_ISDIR(fattr->mode) ? 1 : fattr->nlink;
572 if (stale_inode || inode->i_count > max_count + unhashed) {
573 dprintk("__nfs_fhget: inode %ld busy, i_count=%d, i_nlink=%d\n",
574 inode->i_ino, inode->i_count, inode->i_nlink);
575 unhashed = nfs_free_dentries(inode);
576 if (stale_inode || inode->i_count > max_count + unhashed) {
577 printk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
578 inode->i_ino, inode->i_count);
579 if (!list_empty(&inode->i_dentry)) {
580 struct dentry *dentry;
581 dentry = list_entry(inode->i_dentry.next,
582 struct dentry, d_alias);
583 printk("__nfs_fhget: killing %s/%s filehandle\n",
584 dentry->d_parent->d_name.name,
585 dentry->d_name.name);
586 memset(dentry->d_fsdata, 0,
587 sizeof(struct nfs_fh));
589 remove_inode_hash(inode);
590 nfs_invalidate_inode(inode);
591 unhashed = 0;
593 iput(inode);
594 goto retry;
596 nfs_fill_inode(inode, fattr);
597 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
598 inode->i_dev, inode->i_ino, inode->i_count);
600 out:
601 return inode;
603 out_no_inode:
604 printk("__nfs_fhget: iget failed\n");
605 goto out;
606 out_bad_id:
607 printk("__nfs_fhget: unexpected inode from iget\n");
608 goto out;
612 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
614 struct inode *inode = dentry->d_inode;
615 int error;
616 struct nfs_sattr sattr;
617 struct nfs_fattr fattr;
620 * Make sure the inode is up-to-date.
622 error = nfs_revalidate(dentry);
623 if (error) {
624 #ifdef NFS_PARANOIA
625 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
626 #endif
627 goto out;
630 sattr.mode = (u32) -1;
631 if (attr->ia_valid & ATTR_MODE)
632 sattr.mode = attr->ia_mode;
634 sattr.uid = (u32) -1;
635 if (attr->ia_valid & ATTR_UID)
636 sattr.uid = attr->ia_uid;
638 sattr.gid = (u32) -1;
639 if (attr->ia_valid & ATTR_GID)
640 sattr.gid = attr->ia_gid;
642 sattr.size = (u32) -1;
643 if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode))
644 sattr.size = attr->ia_size;
646 sattr.mtime.seconds = sattr.mtime.useconds = (u32) -1;
647 if (attr->ia_valid & ATTR_MTIME) {
648 sattr.mtime.seconds = attr->ia_mtime;
649 sattr.mtime.useconds = 0;
652 sattr.atime.seconds = sattr.atime.useconds = (u32) -1;
653 if (attr->ia_valid & ATTR_ATIME) {
654 sattr.atime.seconds = attr->ia_atime;
655 sattr.atime.useconds = 0;
658 error = nfs_wb_all(inode);
659 if (error)
660 goto out;
662 error = nfs_proc_setattr(NFS_DSERVER(dentry), NFS_FH(dentry),
663 &sattr, &fattr);
664 if (error)
665 goto out;
667 * If we changed the size or mtime, update the inode
668 * now to avoid invalidating the page cache.
670 if (sattr.size != (u32) -1) {
671 if (sattr.size != fattr.size)
672 printk("nfs_notify_change: sattr=%d, fattr=%d??\n",
673 sattr.size, fattr.size);
674 inode->i_size = sattr.size;
675 inode->i_mtime = fattr.mtime.seconds;
677 if (sattr.mtime.seconds != (u32) -1)
678 inode->i_mtime = fattr.mtime.seconds;
679 error = nfs_refresh_inode(inode, &fattr);
680 out:
681 return error;
685 * Externally visible revalidation function
688 nfs_revalidate(struct dentry *dentry)
690 return nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
694 * These are probably going to contain hooks for
695 * allocating and releasing RPC credentials for
696 * the file. I'll have to think about Tronds patch
697 * a bit more..
699 int nfs_open(struct inode *inode, struct file *filp)
701 return 0;
704 int nfs_release(struct inode *inode, struct file *filp)
706 return 0;
710 * This function is called whenever some part of NFS notices that
711 * the cached attributes have to be refreshed.
714 _nfs_revalidate_inode(struct nfs_server *server, struct dentry *dentry)
716 struct inode *inode = dentry->d_inode;
717 int status = 0;
718 struct nfs_fattr fattr;
720 dfprintk(PAGECACHE, "NFS: revalidating %s/%s, ino=%ld\n",
721 dentry->d_parent->d_name.name, dentry->d_name.name,
722 inode->i_ino);
723 status = nfs_proc_getattr(server, NFS_FH(dentry), &fattr);
724 if (status) {
725 int error;
726 u32 *fh;
727 struct nfs_fh fhandle;
728 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
729 dentry->d_parent->d_name.name,
730 dentry->d_name.name, inode->i_ino, status);
731 if (status != -ESTALE)
732 goto out;
734 * A "stale filehandle" error ... show the current fh
735 * and find out what the filehandle should be.
737 fh = (u32 *) NFS_FH(dentry);
738 dfprintk(PAGECACHE, "NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
739 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
740 error = nfs_proc_lookup(server, NFS_FH(dentry->d_parent),
741 dentry->d_name.name, &fhandle, &fattr);
742 if (error) {
743 dfprintk(PAGECACHE, "NFS: lookup failed, error=%d\n", error);
744 goto out;
746 fh = (u32 *) &fhandle;
747 dfprintk(PAGECACHE, " %08x%08x%08x%08x%08x%08x%08x%08x\n",
748 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
749 goto out;
752 status = nfs_refresh_inode(inode, &fattr);
753 if (status) {
754 dfprintk(PAGECACHE, "nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
755 dentry->d_parent->d_name.name,
756 dentry->d_name.name, inode->i_ino, status);
757 goto out;
759 dfprintk(PAGECACHE, "NFS: %s/%s revalidation complete\n",
760 dentry->d_parent->d_name.name, dentry->d_name.name);
761 out:
762 return status;
766 * Many nfs protocol calls return the new file attributes after
767 * an operation. Here we update the inode to reflect the state
768 * of the server's inode.
770 * This is a bit tricky because we have to make sure all dirty pages
771 * have been sent off to the server before calling invalidate_inode_pages.
772 * To make sure no other process adds more write requests while we try
773 * our best to flush them, we make them sleep during the attribute refresh.
775 * A very similar scenario holds for the dir cache.
778 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
780 int invalid = 0;
781 int error = -EIO;
783 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d)\n",
784 inode->i_dev, inode->i_ino, inode->i_count);
786 if (!inode || !fattr) {
787 printk("nfs_refresh_inode: inode or fattr is NULL\n");
788 goto out;
790 if (inode->i_ino != fattr->fileid) {
791 printk("nfs_refresh_inode: mismatch, ino=%ld, fattr=%d\n",
792 inode->i_ino, fattr->fileid);
793 goto out;
797 * Make sure the inode's type hasn't changed.
799 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
800 goto out_changed;
802 inode->i_mode = fattr->mode;
803 inode->i_nlink = fattr->nlink;
804 inode->i_uid = fattr->uid;
805 inode->i_gid = fattr->gid;
807 inode->i_blocks = fattr->blocks;
808 inode->i_atime = fattr->atime.seconds;
809 inode->i_ctime = fattr->ctime.seconds;
812 * Update the read time so we don't revalidate too often.
814 NFS_READTIME(inode) = jiffies;
815 error = 0;
818 * If we have pending write-back entries, we don't want
819 * to look at the size or the mtime the server sends us
820 * too closely, as we're in the middle of modifying them.
822 if (NFS_WRITEBACK(inode))
823 goto out;
825 if (inode->i_size != fattr->size) {
826 #ifdef NFS_DEBUG_VERBOSE
827 printk("NFS: size change on %x/%ld\n", inode->i_dev, inode->i_ino);
828 #endif
829 inode->i_size = fattr->size;
830 invalid = 1;
833 if (inode->i_mtime != fattr->mtime.seconds) {
834 #ifdef NFS_DEBUG_VERBOSE
835 printk("NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
836 #endif
837 inode->i_mtime = fattr->mtime.seconds;
838 invalid = 1;
841 if (invalid)
842 goto out_invalid;
844 /* Update attrtimeo value */
845 if (fattr->mtime.seconds == NFS_OLDMTIME(inode)) {
846 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
847 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
849 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
851 out:
852 return error;
854 out_changed:
856 * Big trouble! The inode has become a different object.
858 #ifdef NFS_PARANOIA
859 printk("nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
860 inode->i_ino, inode->i_mode, fattr->mode);
861 #endif
863 * No need to worry about unhashing the dentry, as the
864 * lookup validation will know that the inode is bad.
866 nfs_invalidate_inode(inode);
867 goto out;
869 out_invalid:
870 #ifdef NFS_DEBUG_VERBOSE
871 printk("nfs_refresh_inode: invalidating %ld pages\n", inode->i_nrpages);
872 #endif
873 nfs_zap_caches(inode);
874 goto out;
878 * File system information
880 static struct file_system_type nfs_fs_type = {
881 "nfs",
882 0 /* FS_NO_DCACHE - this doesn't work right now*/,
883 nfs_read_super,
884 NULL
887 extern int nfs_init_fhcache(void);
888 extern int nfs_init_wreqcache(void);
891 * Initialize NFS
894 init_nfs_fs(void)
896 int err;
898 err = nfs_init_fhcache();
899 if (err)
900 return err;
902 err = nfs_init_wreqcache();
903 if (err)
904 return err;
906 #ifdef CONFIG_PROC_FS
907 rpc_register_sysctl();
908 rpc_proc_init();
909 rpc_proc_register(&nfs_rpcstat);
910 #endif
911 return register_filesystem(&nfs_fs_type);
915 * Every kernel module contains stuff like this.
917 #ifdef MODULE
919 EXPORT_NO_SYMBOLS;
920 /* Not quite true; I just maintain it */
921 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
924 init_module(void)
926 return init_nfs_fs();
929 void
930 cleanup_module(void)
932 #ifdef CONFIG_PROC_FS
933 rpc_proc_unregister("nfs");
934 #endif
935 unregister_filesystem(&nfs_fs_type);
937 #endif