Import 2.1.118
[davej-history.git] / fs / nfs / inode.c
blob59782127092e1ec5c8d2d674d361bc610d3181e0
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 *);
41 static void nfs_read_inode(struct inode *);
42 static void nfs_put_inode(struct inode *);
43 static void nfs_delete_inode(struct inode *);
44 static int nfs_notify_change(struct dentry *, struct iattr *);
45 static void nfs_put_super(struct super_block *);
46 static void nfs_umount_begin(struct super_block *);
47 static int nfs_statfs(struct super_block *, struct statfs *, int);
49 static struct super_operations nfs_sops = {
50 nfs_read_inode, /* read inode */
51 NULL, /* write inode */
52 nfs_put_inode, /* put inode */
53 nfs_delete_inode, /* delete inode */
54 nfs_notify_change, /* notify change */
55 nfs_put_super, /* put superblock */
56 NULL, /* write superblock */
57 nfs_statfs, /* stat filesystem */
58 NULL, /* no remount */
59 NULL, /* no clear inode */
60 nfs_umount_begin /* umount attempt begin */
63 struct rpc_stat nfs_rpcstat = { &nfs_program };
66 * The "read_inode" function doesn't actually do anything:
67 * the real data is filled in later in nfs_fhget. Here we
68 * just mark the cache times invalid, and zero out i_mode
69 * (the latter makes "nfs_refresh_inode" do the right thing
70 * wrt pipe inodes)
72 static void
73 nfs_read_inode(struct inode * inode)
75 inode->i_blksize = inode->i_sb->s_blocksize;
76 inode->i_mode = 0;
77 inode->i_rdev = 0;
78 inode->i_op = NULL;
79 NFS_CACHEINV(inode);
80 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
83 static void
84 nfs_put_inode(struct inode * inode)
86 dprintk("NFS: put_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
88 * We want to get rid of unused inodes ...
90 if (inode->i_count == 1)
91 inode->i_nlink = 0;
94 static void
95 nfs_delete_inode(struct inode * inode)
97 int failed;
99 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
101 * Flush out any pending write requests ...
103 if (NFS_WRITEBACK(inode) != NULL) {
104 unsigned long timeout = jiffies + 5*HZ;
105 #ifdef NFS_DEBUG_VERBOSE
106 printk("nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
107 #endif
108 nfs_invalidate_pages(inode);
109 while (NFS_WRITEBACK(inode) != NULL && jiffies < timeout) {
110 current->state = TASK_INTERRUPTIBLE;
111 current->timeout = jiffies + HZ/10;
112 schedule();
114 current->state = TASK_RUNNING;
115 if (NFS_WRITEBACK(inode) != NULL)
116 printk("NFS: Arghhh, stuck RPC requests!\n");
119 failed = nfs_check_failed_request(inode);
120 if (failed)
121 printk("NFS: inode %ld had %d failed requests\n",
122 inode->i_ino, failed);
123 clear_inode(inode);
126 void
127 nfs_put_super(struct super_block *sb)
129 struct nfs_server *server = &sb->u.nfs_sb.s_server;
130 struct rpc_clnt *rpc;
132 if ((rpc = server->client) != NULL)
133 rpc_shutdown_client(rpc);
135 if (!(server->flags & NFS_MOUNT_NONLM))
136 lockd_down(); /* release rpc.lockd */
137 rpciod_down(); /* release rpciod */
139 * Invalidate the dircache for this superblock.
141 nfs_invalidate_dircache_sb(sb);
143 kfree(server->hostname);
145 MOD_DEC_USE_COUNT;
148 void
149 nfs_umount_begin(struct super_block *sb)
151 struct nfs_server *server = &sb->u.nfs_sb.s_server;
152 struct rpc_clnt *rpc;
154 /* -EIO all pending I/O */
155 if ((rpc = server->client) != NULL)
156 rpc_killall_tasks(rpc);
160 * Compute and set NFS server blocksize
162 static unsigned int
163 nfs_block_size(unsigned int bsize, unsigned char *nrbitsp)
165 if (bsize < 1024)
166 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
167 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
168 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
170 /* make sure blocksize is a power of two */
171 if ((bsize & (bsize - 1)) || nrbitsp) {
172 unsigned int nrbits;
174 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
176 bsize = 1 << nrbits;
177 if (nrbitsp)
178 *nrbitsp = nrbits;
179 if (bsize < NFS_DEF_FILE_IO_BUFFER_SIZE)
180 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
183 return bsize;
187 * The way this works is that the mount process passes a structure
188 * in the data argument which contains the server's IP address
189 * and the root file handle obtained from the server's mount
190 * daemon. We stash these away in the private superblock fields.
192 struct super_block *
193 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
195 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
196 struct nfs_server *server;
197 struct rpc_xprt *xprt;
198 struct rpc_clnt *clnt;
199 struct nfs_fh *root_fh;
200 struct inode *root_inode;
201 unsigned int authflavor;
202 int tcp;
203 struct sockaddr_in srvaddr;
204 struct rpc_timeout timeparms;
205 struct nfs_fattr fattr;
207 MOD_INC_USE_COUNT;
208 if (!data)
209 goto out_miss_args;
211 if (data->version != NFS_MOUNT_VERSION) {
212 printk("nfs warning: mount version %s than kernel\n",
213 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
214 if (data->version < 2)
215 data->namlen = 0;
216 if (data->version < 3)
217 data->bsize = 0;
220 /* We now require that the mount process passes the remote address */
221 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
222 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
223 goto out_no_remote;
225 lock_super(sb);
227 sb->s_magic = NFS_SUPER_MAGIC;
228 sb->s_op = &nfs_sops;
229 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
230 sb->u.nfs_sb.s_root = data->root;
231 server = &sb->u.nfs_sb.s_server;
232 server->rsize = nfs_block_size(data->rsize, NULL);
233 server->wsize = nfs_block_size(data->wsize, NULL);
234 server->flags = data->flags;
235 server->acregmin = data->acregmin*HZ;
236 server->acregmax = data->acregmax*HZ;
237 server->acdirmin = data->acdirmin*HZ;
238 server->acdirmax = data->acdirmax*HZ;
240 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
241 if (!server->hostname)
242 goto out_unlock;
243 strcpy(server->hostname, data->hostname);
245 /* Which protocol do we use? */
246 tcp = (data->flags & NFS_MOUNT_TCP);
248 /* Initialize timeout values */
249 timeparms.to_initval = data->timeo * HZ / 10;
250 timeparms.to_retries = data->retrans;
251 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
252 timeparms.to_exponential = 1;
254 /* Now create transport and client */
255 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
256 &srvaddr, &timeparms);
257 if (xprt == NULL)
258 goto out_no_xprt;
260 /* Choose authentication flavor */
261 authflavor = RPC_AUTH_UNIX;
262 if (data->flags & NFS_MOUNT_SECURE)
263 authflavor = RPC_AUTH_DES;
264 else if (data->flags & NFS_MOUNT_KERBEROS)
265 authflavor = RPC_AUTH_KRB;
267 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
268 NFS_VERSION, authflavor);
269 if (clnt == NULL)
270 goto out_no_client;
272 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
273 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
274 clnt->cl_chatty = 1;
275 server->client = clnt;
277 /* Fire up rpciod if not yet running */
278 if (rpciod_up() != 0)
279 goto out_no_iod;
282 * Keep the super block locked while we try to get
283 * the root fh attributes.
285 root_fh = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
286 if (!root_fh)
287 goto out_no_fh;
288 *root_fh = data->root;
290 if (nfs_proc_getattr(server, root_fh, &fattr) != 0)
291 goto out_no_fattr;
293 root_inode = __nfs_fhget(sb, &fattr);
294 if (!root_inode)
295 goto out_no_root;
296 sb->s_root = d_alloc_root(root_inode, NULL);
297 if (!sb->s_root)
298 goto out_no_root;
299 sb->s_root->d_op = &nfs_dentry_operations;
300 sb->s_root->d_fsdata = root_fh;
302 /* We're airborne */
303 unlock_super(sb);
305 /* Check whether to start the lockd process */
306 if (!(server->flags & NFS_MOUNT_NONLM))
307 lockd_up();
308 return sb;
310 /* Yargs. It didn't work out. */
311 out_no_root:
312 printk("nfs_read_super: get root inode failed\n");
313 iput(root_inode);
314 goto out_free_fh;
316 out_no_fattr:
317 printk("nfs_read_super: get root fattr failed\n");
318 out_free_fh:
319 kfree(root_fh);
320 out_no_fh:
321 rpciod_down();
322 goto out_shutdown;
324 out_no_iod:
325 printk("NFS: couldn't start rpciod!\n");
326 out_shutdown:
327 rpc_shutdown_client(server->client);
328 goto out_unlock;
330 out_no_client:
331 printk("NFS: cannot create RPC client.\n");
332 xprt_destroy(xprt);
333 goto out_unlock;
335 out_no_xprt:
336 printk("NFS: cannot create RPC transport.\n");
337 kfree(server->hostname);
338 out_unlock:
339 unlock_super(sb);
340 goto out_fail;
342 out_no_remote:
343 printk("NFS: mount program didn't pass remote address!\n");
344 goto out_fail;
346 out_miss_args:
347 printk("nfs_read_super: missing data argument\n");
349 out_fail:
350 sb->s_dev = 0;
351 MOD_DEC_USE_COUNT;
352 return NULL;
355 static int
356 nfs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
358 int error;
359 struct nfs_fsinfo res;
360 struct statfs tmp;
362 error = nfs_proc_statfs(&sb->u.nfs_sb.s_server, &sb->u.nfs_sb.s_root,
363 &res);
364 if (error) {
365 printk("nfs_statfs: statfs error = %d\n", -error);
366 res.bsize = res.blocks = res.bfree = res.bavail = 0;
368 tmp.f_type = NFS_SUPER_MAGIC;
369 tmp.f_bsize = res.bsize;
370 tmp.f_blocks = res.blocks;
371 tmp.f_bfree = res.bfree;
372 tmp.f_bavail = res.bavail;
373 tmp.f_files = 0;
374 tmp.f_ffree = 0;
375 tmp.f_namelen = NAME_MAX;
376 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
380 * Free all unused dentries in an inode's alias list.
382 * Subtle note: we have to be very careful not to cause
383 * any IO operations with the stale dentries, as this
384 * could cause file corruption. But since the dentry
385 * count is 0 and all pending IO for a dentry has been
386 * flushed when the count went to 0, we're safe here.
388 void nfs_free_dentries(struct inode *inode)
390 struct list_head *tmp, *head = &inode->i_dentry;
392 restart:
393 tmp = head;
394 while ((tmp = tmp->next) != head) {
395 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
396 if (!dentry->d_count) {
397 printk("nfs_free_dentries: freeing %s/%s, i_count=%d\n",
398 dentry->d_parent->d_name.name, dentry->d_name.name, inode->i_count);
399 dget(dentry);
400 d_drop(dentry);
401 dput(dentry);
402 goto restart;
408 * Fill in inode information from the fattr.
410 static void
411 nfs_fill_inode(struct inode *inode, struct nfs_fattr *fattr)
414 * Check whether the mode has been set, as we only want to
415 * do this once. (We don't allow inodes to change types.)
417 if (inode->i_mode == 0) {
418 inode->i_mode = fattr->mode;
419 if (S_ISREG(inode->i_mode))
420 inode->i_op = &nfs_file_inode_operations;
421 else if (S_ISDIR(inode->i_mode))
422 inode->i_op = &nfs_dir_inode_operations;
423 else if (S_ISLNK(inode->i_mode))
424 inode->i_op = &nfs_symlink_inode_operations;
425 else if (S_ISCHR(inode->i_mode)) {
426 inode->i_op = &chrdev_inode_operations;
427 inode->i_rdev = to_kdev_t(fattr->rdev);
428 } else if (S_ISBLK(inode->i_mode)) {
429 inode->i_op = &blkdev_inode_operations;
430 inode->i_rdev = to_kdev_t(fattr->rdev);
431 } else if (S_ISFIFO(inode->i_mode))
432 init_fifo(inode);
433 else
434 inode->i_op = NULL;
436 * Preset the size and mtime, as there's no need
437 * to invalidate the caches.
439 inode->i_size = fattr->size;
440 inode->i_mtime = fattr->mtime.seconds;
441 NFS_OLDMTIME(inode) = fattr->mtime.seconds;
443 nfs_refresh_inode(inode, fattr);
447 * This is our own version of iget that looks up inodes by file handle
448 * instead of inode number. We use this technique instead of using
449 * the vfs read_inode function because there is no way to pass the
450 * file handle or current attributes into the read_inode function.
452 * We provide a special check for NetApp .snapshot directories to avoid
453 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
455 struct inode *
456 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
457 struct nfs_fattr *fattr)
459 struct super_block *sb = dentry->d_sb;
461 dprintk("NFS: nfs_fhget(%s/%s fileid=%d)\n",
462 dentry->d_parent->d_name.name, dentry->d_name.name,
463 fattr->fileid);
465 /* Install the file handle in the dentry */
466 *((struct nfs_fh *) dentry->d_fsdata) = *fhandle;
468 #ifdef CONFIG_NFS_SNAPSHOT
470 * Check for NetApp snapshot dentries, and get an
471 * unhashed inode to avoid aliasing problems.
473 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
474 (IS_ROOT(dentry->d_parent) && dentry->d_name.len == 9 &&
475 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
476 struct inode *inode = get_empty_inode();
477 if (!inode)
478 goto out;
479 inode->i_sb = sb;
480 inode->i_dev = sb->s_dev;
481 inode->i_flags = sb->s_flags;
482 inode->i_ino = fattr->fileid;
483 nfs_read_inode(inode);
484 nfs_fill_inode(inode, fattr);
485 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
486 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
487 out:
488 return inode;
490 #endif
491 return __nfs_fhget(sb, fattr);
495 * Look up the inode by super block and fattr->fileid.
497 * Note carefully the special handling of busy inodes (i_count > 1).
498 * With the kernel 2.1.xx dcache all inodes except hard links must
499 * have i_count == 1 after iget(). Otherwise, it indicates that the
500 * server has reused a fileid (i_ino) and we have a stale inode.
502 static struct inode *
503 __nfs_fhget(struct super_block *sb, struct nfs_fattr *fattr)
505 struct inode *inode;
506 int max_count;
508 retry:
509 inode = iget(sb, fattr->fileid);
510 if (!inode)
511 goto out_no_inode;
512 /* N.B. This should be impossible ... */
513 if (inode->i_ino != fattr->fileid)
514 goto out_bad_id;
517 * Check for busy inodes, and attempt to get rid of any
518 * unused local references. If successful, we release the
519 * inode and try again.
521 * Note that the busy test uses the values in the fattr,
522 * as the inode may have become a different object.
523 * (We can probably handle modes changes here, too.)
525 max_count = S_ISDIR(fattr->mode) ? 1 : fattr->nlink;
526 if (inode->i_count > max_count) {
527 printk("__nfs_fhget: inode %ld busy, i_count=%d, i_nlink=%d\n",
528 inode->i_ino, inode->i_count, inode->i_nlink);
529 nfs_free_dentries(inode);
530 if (inode->i_count > max_count) {
531 printk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
532 inode->i_ino, inode->i_count);
533 if (!list_empty(&inode->i_dentry)) {
534 struct dentry *dentry;
535 dentry = list_entry(inode->i_dentry.next,
536 struct dentry, d_alias);
537 printk("__nfs_fhget: killing %s/%s filehandle\n",
538 dentry->d_parent->d_name.name, dentry->d_name.name);
539 memset(dentry->d_fsdata, 0,
540 sizeof(struct nfs_fh));
541 } else
542 printk("NFS: inode %ld busy, no aliases?\n",
543 inode->i_ino);
544 make_bad_inode(inode);
545 remove_inode_hash(inode);
547 iput(inode);
548 goto retry;
550 nfs_fill_inode(inode, fattr);
551 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
552 inode->i_dev, inode->i_ino, inode->i_count);
554 out:
555 return inode;
557 out_no_inode:
558 printk("__nfs_fhget: iget failed\n");
559 goto out;
560 out_bad_id:
561 printk("__nfs_fhget: unexpected inode from iget\n");
562 goto out;
566 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
568 struct inode *inode = dentry->d_inode;
569 int error;
570 struct nfs_sattr sattr;
571 struct nfs_fattr fattr;
574 * Make sure the inode is up-to-date.
576 error = nfs_revalidate(dentry);
577 if (error) {
578 #ifdef NFS_PARANOIA
579 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
580 #endif
581 goto out;
584 sattr.mode = (u32) -1;
585 if (attr->ia_valid & ATTR_MODE)
586 sattr.mode = attr->ia_mode;
588 sattr.uid = (u32) -1;
589 if (attr->ia_valid & ATTR_UID)
590 sattr.uid = attr->ia_uid;
592 sattr.gid = (u32) -1;
593 if (attr->ia_valid & ATTR_GID)
594 sattr.gid = attr->ia_gid;
596 sattr.size = (u32) -1;
597 if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode))
598 sattr.size = attr->ia_size;
600 sattr.mtime.seconds = sattr.mtime.useconds = (u32) -1;
601 if (attr->ia_valid & ATTR_MTIME) {
602 sattr.mtime.seconds = attr->ia_mtime;
603 sattr.mtime.useconds = 0;
606 sattr.atime.seconds = sattr.atime.useconds = (u32) -1;
607 if (attr->ia_valid & ATTR_ATIME) {
608 sattr.atime.seconds = attr->ia_atime;
609 sattr.atime.useconds = 0;
612 error = nfs_proc_setattr(NFS_DSERVER(dentry), NFS_FH(dentry),
613 &sattr, &fattr);
614 if (error)
615 goto out;
617 * If we changed the size or mtime, update the inode
618 * now to avoid invalidating the page cache.
620 if (sattr.size != (u32) -1) {
621 if (sattr.size != fattr.size)
622 printk("nfs_notify_change: sattr=%d, fattr=%d??\n",
623 sattr.size, fattr.size);
624 nfs_truncate_dirty_pages(inode, sattr.size);
625 inode->i_size = sattr.size;
626 inode->i_mtime = fattr.mtime.seconds;
628 if (sattr.mtime.seconds != (u32) -1)
629 inode->i_mtime = fattr.mtime.seconds;
630 error = nfs_refresh_inode(inode, &fattr);
631 out:
632 return error;
636 * Externally visible revalidation function
639 nfs_revalidate(struct dentry *dentry)
641 return nfs_revalidate_inode(NFS_DSERVER(dentry), dentry);
645 * This function is called whenever some part of NFS notices that
646 * the cached attributes have to be refreshed.
649 _nfs_revalidate_inode(struct nfs_server *server, struct dentry *dentry)
651 struct inode *inode = dentry->d_inode;
652 int status = 0;
653 struct nfs_fattr fattr;
655 /* Don't bother revalidating if we've done it recently */
656 if (jiffies - NFS_READTIME(inode) < NFS_ATTRTIMEO(inode))
657 goto out;
659 dfprintk(PAGECACHE, "NFS: revalidating %s/%s, ino=%ld\n",
660 dentry->d_parent->d_name.name, dentry->d_name.name,
661 inode->i_ino);
662 status = nfs_proc_getattr(server, NFS_FH(dentry), &fattr);
663 if (status) {
664 int error;
665 u32 *fh;
666 struct nfs_fh fhandle;
667 #ifdef NFS_PARANOIA
668 printk("nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
669 dentry->d_parent->d_name.name, dentry->d_name.name, inode->i_ino, status);
670 #endif
671 if (status != -ESTALE)
672 goto out;
674 * A "stale filehandle" error ... show the current fh
675 * and find out what the filehandle should be.
677 fh = (u32 *) NFS_FH(dentry);
678 printk("NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
679 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
680 error = nfs_proc_lookup(server, NFS_FH(dentry->d_parent),
681 dentry->d_name.name, &fhandle, &fattr);
682 if (error) {
683 printk("NFS: lookup failed, error=%d\n", error);
684 goto out;
686 fh = (u32 *) &fhandle;
687 printk(" %08x%08x%08x%08x%08x%08x%08x%08x\n",
688 fh[0],fh[1],fh[2],fh[3],fh[4],fh[5],fh[6],fh[7]);
689 goto out;
692 status = nfs_refresh_inode(inode, &fattr);
693 if (status) {
694 #ifdef NFS_PARANOIA
695 printk("nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
696 dentry->d_parent->d_name.name, dentry->d_name.name, inode->i_ino, status);
697 #endif
698 goto out;
700 if (fattr.mtime.seconds == NFS_OLDMTIME(inode)) {
701 /* Update attrtimeo value */
702 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
703 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
705 NFS_OLDMTIME(inode) = fattr.mtime.seconds;
706 dfprintk(PAGECACHE, "NFS: %s/%s revalidation complete\n",
707 dentry->d_parent->d_name.name, dentry->d_name.name);
708 out:
709 return status;
713 * Many nfs protocol calls return the new file attributes after
714 * an operation. Here we update the inode to reflect the state
715 * of the server's inode.
717 * This is a bit tricky because we have to make sure all dirty pages
718 * have been sent off to the server before calling invalidate_inode_pages.
719 * To make sure no other process adds more write requests while we try
720 * our best to flush them, we make them sleep during the attribute refresh.
722 * A very similar scenario holds for the dir cache.
725 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
727 int invalid = 0;
728 int error = -EIO;
730 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d)\n",
731 inode->i_dev, inode->i_ino, inode->i_count);
733 if (!inode || !fattr) {
734 printk("nfs_refresh_inode: inode or fattr is NULL\n");
735 goto out;
737 if (inode->i_ino != fattr->fileid) {
738 printk("nfs_refresh_inode: mismatch, ino=%ld, fattr=%d\n",
739 inode->i_ino, fattr->fileid);
740 goto out;
744 * Make sure the inode's type hasn't changed.
746 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
747 goto out_changed;
750 * If we have pending write-back entries, we don't want
751 * to look at the size the server sends us too closely..
752 * In particular, ignore the server if it tells us that
753 * the file is smaller or older than we locally think it
754 * is..
756 if (NFS_WRITEBACK(inode)) {
757 if (inode->i_size > fattr->size)
758 fattr->size = inode->i_size;
759 if (inode->i_mtime > fattr->mtime.seconds)
760 fattr->mtime.seconds = inode->i_mtime;
764 * If the size or mtime changed from outside, we want
765 * to invalidate the local caches immediately.
767 if (inode->i_size != fattr->size) {
768 #ifdef NFS_DEBUG_VERBOSE
769 printk("NFS: size change on %x/%ld\n", inode->i_dev, inode->i_ino);
770 #endif
771 invalid = 1;
773 if (inode->i_mtime != fattr->mtime.seconds) {
774 #ifdef NFS_DEBUG_VERBOSE
775 printk("NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
776 #endif
777 invalid = 1;
780 inode->i_mode = fattr->mode;
781 inode->i_nlink = fattr->nlink;
782 inode->i_uid = fattr->uid;
783 inode->i_gid = fattr->gid;
785 inode->i_size = fattr->size;
786 inode->i_blocks = fattr->blocks;
787 inode->i_atime = fattr->atime.seconds;
788 inode->i_mtime = fattr->mtime.seconds;
789 inode->i_ctime = fattr->ctime.seconds;
791 * Update the read time so we don't revalidate too often.
793 NFS_READTIME(inode) = jiffies;
794 error = 0;
795 if (invalid)
796 goto out_invalid;
797 out:
798 return error;
800 out_changed:
802 * Big trouble! The inode has become a different object.
804 #ifdef NFS_PARANOIA
805 printk("nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
806 inode->i_ino, inode->i_mode, fattr->mode);
807 #endif
808 fattr->mode = inode->i_mode; /* save mode */
809 make_bad_inode(inode);
810 inode->i_mode = fattr->mode; /* restore mode */
812 * No need to worry about unhashing the dentry, as the
813 * lookup validation will know that the inode is bad.
814 * (But we fall through to invalidate the caches.)
817 out_invalid:
819 * Invalidate the local caches
821 #ifdef NFS_DEBUG_VERBOSE
822 printk("nfs_refresh_inode: invalidating %ld pages\n", inode->i_nrpages);
823 #endif
824 if (!S_ISDIR(inode->i_mode)) {
825 /* This sends off all dirty pages off to the server.
826 * Note that this function must not sleep. */
827 nfs_invalidate_pages(inode);
828 invalidate_inode_pages(inode);
829 } else
830 nfs_invalidate_dircache(inode);
831 NFS_CACHEINV(inode);
832 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
833 goto out;
837 * File system information
839 static struct file_system_type nfs_fs_type = {
840 "nfs",
841 0 /* FS_NO_DCACHE - this doesn't work right now*/,
842 nfs_read_super,
843 NULL
847 * Initialize NFS
850 init_nfs_fs(void)
852 #ifdef CONFIG_PROC_FS
853 rpc_register_sysctl();
854 rpc_proc_init();
855 rpc_proc_register(&nfs_rpcstat);
856 #endif
857 return register_filesystem(&nfs_fs_type);
861 * Every kernel module contains stuff like this.
863 #ifdef MODULE
865 EXPORT_NO_SYMBOLS;
866 /* Not quite true; I just maintain it */
867 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
870 init_module(void)
872 return init_nfs_fs();
875 void
876 cleanup_module(void)
878 #ifdef CONFIG_PROC_FS
879 rpc_proc_unregister("nfs");
880 #endif
881 unregister_filesystem(&nfs_fs_type);
882 nfs_free_dircache();
884 #endif