- Andries Brouwer: final isofs pieces.
[davej-history.git] / fs / nfs / inode.c
bloba205c3ad4a281230971a333275d32e6a8ad94d80
1 /*
2 * linux/fs/nfs/inode.c
4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
16 #include <linux/config.h>
17 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
25 #include <linux/locks.h>
26 #include <linux/unistd.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/sunrpc/stats.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs_flushd.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/smp_lock.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
38 #define CONFIG_NFS_SNAPSHOT 1
39 #define NFSDBG_FACILITY NFSDBG_VFS
40 #define NFS_PARANOIA 1
42 static struct inode * __nfs_fhget(struct super_block *, struct nfs_fh *, struct nfs_fattr *);
43 void nfs_zap_caches(struct inode *);
44 static void nfs_invalidate_inode(struct inode *);
46 static void nfs_read_inode(struct inode *);
47 static void nfs_delete_inode(struct inode *);
48 static void nfs_put_super(struct super_block *);
49 static void nfs_umount_begin(struct super_block *);
50 static int nfs_statfs(struct super_block *, struct statfs *);
52 static struct super_operations nfs_sops = {
53 read_inode: nfs_read_inode,
54 put_inode: force_delete,
55 delete_inode: nfs_delete_inode,
56 put_super: nfs_put_super,
57 statfs: nfs_statfs,
58 umount_begin: nfs_umount_begin,
62 * RPC cruft for NFS
64 struct rpc_stat nfs_rpcstat = { &nfs_program };
65 static struct rpc_version * nfs_version[] = {
66 NULL,
67 NULL,
68 &nfs_version2,
69 #ifdef CONFIG_NFS_V3
70 &nfs_version3,
71 #endif
74 struct rpc_program nfs_program = {
75 "nfs",
76 NFS_PROGRAM,
77 sizeof(nfs_version) / sizeof(nfs_version[0]),
78 nfs_version,
79 &nfs_rpcstat,
82 static inline unsigned long
83 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
85 return nfs_fileid_to_ino_t(fattr->fileid);
89 * The "read_inode" function doesn't actually do anything:
90 * the real data is filled in later in nfs_fhget. Here we
91 * just mark the cache times invalid, and zero out i_mode
92 * (the latter makes "nfs_refresh_inode" do the right thing
93 * wrt pipe inodes)
95 static void
96 nfs_read_inode(struct inode * inode)
98 inode->i_blksize = inode->i_sb->s_blocksize;
99 inode->i_mode = 0;
100 inode->i_rdev = 0;
101 NFS_FILEID(inode) = 0;
102 NFS_FSID(inode) = 0;
103 INIT_LIST_HEAD(&inode->u.nfs_i.read);
104 INIT_LIST_HEAD(&inode->u.nfs_i.dirty);
105 INIT_LIST_HEAD(&inode->u.nfs_i.commit);
106 INIT_LIST_HEAD(&inode->u.nfs_i.writeback);
107 inode->u.nfs_i.nread = 0;
108 inode->u.nfs_i.ndirty = 0;
109 inode->u.nfs_i.ncommit = 0;
110 inode->u.nfs_i.npages = 0;
111 NFS_CACHEINV(inode);
112 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
113 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
116 static void
117 nfs_delete_inode(struct inode * inode)
119 dprintk("NFS: delete_inode(%x/%ld)\n", inode->i_dev, inode->i_ino);
122 * The following can never actually happen...
124 if (nfs_have_writebacks(inode) || nfs_have_read(inode)) {
125 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
128 clear_inode(inode);
131 void
132 nfs_put_super(struct super_block *sb)
134 struct nfs_server *server = &sb->u.nfs_sb.s_server;
135 struct rpc_clnt *rpc;
138 * First get rid of the request flushing daemon.
139 * Relies on rpc_shutdown_client() waiting on all
140 * client tasks to finish.
142 nfs_reqlist_exit(server);
144 if ((rpc = server->client) != NULL)
145 rpc_shutdown_client(rpc);
147 nfs_reqlist_free(server);
149 if (!(server->flags & NFS_MOUNT_NONLM))
150 lockd_down(); /* release rpc.lockd */
151 rpciod_down(); /* release rpciod */
153 kfree(server->hostname);
156 void
157 nfs_umount_begin(struct super_block *sb)
159 struct nfs_server *server = &sb->u.nfs_sb.s_server;
160 struct rpc_clnt *rpc;
162 /* -EIO all pending I/O */
163 if ((rpc = server->client) != NULL)
164 rpc_killall_tasks(rpc);
168 static inline unsigned long
169 nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
171 /* make sure blocksize is a power of two */
172 if ((bsize & (bsize - 1)) || nrbitsp) {
173 unsigned char nrbits;
175 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
177 bsize = 1 << nrbits;
178 if (nrbitsp)
179 *nrbitsp = nrbits;
182 return bsize;
186 * Calculate the number of 512byte blocks used.
188 static inline unsigned long
189 nfs_calc_block_size(u64 tsize)
191 loff_t used = (tsize + 511) >> 9;
192 return (used > ULONG_MAX) ? ULONG_MAX : used;
196 * Compute and set NFS server blocksize
198 static inline unsigned long
199 nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
201 if (bsize < 1024)
202 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
203 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
204 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
206 return nfs_block_bits(bsize, nrbitsp);
210 * Obtain the root inode of the file system.
212 static struct inode *
213 nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh)
215 struct nfs_server *server = &sb->u.nfs_sb.s_server;
216 struct nfs_fattr fattr;
217 struct inode *inode;
218 int error;
220 if ((error = server->rpc_ops->getroot(server, rootfh, &fattr)) < 0) {
221 printk(KERN_NOTICE "nfs_get_root: getattr error = %d\n", -error);
222 return NULL;
225 inode = __nfs_fhget(sb, rootfh, &fattr);
226 return inode;
230 * The way this works is that the mount process passes a structure
231 * in the data argument which contains the server's IP address
232 * and the root file handle obtained from the server's mount
233 * daemon. We stash these away in the private superblock fields.
235 struct super_block *
236 nfs_read_super(struct super_block *sb, void *raw_data, int silent)
238 struct nfs_mount_data *data = (struct nfs_mount_data *) raw_data;
239 struct nfs_server *server;
240 struct rpc_xprt *xprt = NULL;
241 struct rpc_clnt *clnt = NULL;
242 struct nfs_fh *root = &data->root, fh;
243 struct inode *root_inode = NULL;
244 unsigned int authflavor;
245 struct sockaddr_in srvaddr;
246 struct rpc_timeout timeparms;
247 struct nfs_fsinfo fsinfo;
248 int tcp, version, maxlen;
250 memset(&sb->u.nfs_sb, 0, sizeof(sb->u.nfs_sb));
251 if (!data)
252 goto out_miss_args;
254 memset(&fh, 0, sizeof(fh));
255 if (data->version != NFS_MOUNT_VERSION) {
256 printk("nfs warning: mount version %s than kernel\n",
257 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
258 if (data->version < 2)
259 data->namlen = 0;
260 if (data->version < 3)
261 data->bsize = 0;
262 if (data->version < 4) {
263 data->flags &= ~NFS_MOUNT_VER3;
264 root = &fh;
265 root->size = NFS2_FHSIZE;
266 memcpy(root->data, data->old_root.data, NFS2_FHSIZE);
270 /* We now require that the mount process passes the remote address */
271 memcpy(&srvaddr, &data->addr, sizeof(srvaddr));
272 if (srvaddr.sin_addr.s_addr == INADDR_ANY)
273 goto out_no_remote;
275 sb->s_magic = NFS_SUPER_MAGIC;
276 sb->s_op = &nfs_sops;
277 sb->s_blocksize_bits = 0;
278 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
279 server = &sb->u.nfs_sb.s_server;
280 server->rsize = nfs_block_size(data->rsize, NULL);
281 server->wsize = nfs_block_size(data->wsize, NULL);
282 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
284 if (data->flags & NFS_MOUNT_NOAC) {
285 data->acregmin = data->acregmax = 0;
286 data->acdirmin = data->acdirmax = 0;
288 server->acregmin = data->acregmin*HZ;
289 server->acregmax = data->acregmax*HZ;
290 server->acdirmin = data->acdirmin*HZ;
291 server->acdirmax = data->acdirmax*HZ;
293 server->namelen = data->namlen;
294 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
295 if (!server->hostname)
296 goto out_unlock;
297 strcpy(server->hostname, data->hostname);
299 nfsv3_try_again:
300 /* Check NFS protocol revision and initialize RPC op vector
301 * and file handle pool. */
302 if (data->flags & NFS_MOUNT_VER3) {
303 #ifdef CONFIG_NFS_V3
304 server->rpc_ops = &nfs_v3_clientops;
305 version = 3;
306 if (data->version < 4) {
307 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
308 goto out_unlock;
310 #else
311 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
312 goto out_unlock;
313 #endif
314 } else {
315 server->rpc_ops = &nfs_v2_clientops;
316 version = 2;
319 /* Which protocol do we use? */
320 tcp = (data->flags & NFS_MOUNT_TCP);
322 /* Initialize timeout values */
323 timeparms.to_initval = data->timeo * HZ / 10;
324 timeparms.to_retries = data->retrans;
325 timeparms.to_maxval = tcp? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
326 timeparms.to_exponential = 1;
328 if (!timeparms.to_initval)
329 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
330 if (!timeparms.to_retries)
331 timeparms.to_retries = 5;
333 /* Now create transport and client */
334 xprt = xprt_create_proto(tcp? IPPROTO_TCP : IPPROTO_UDP,
335 &srvaddr, &timeparms);
336 if (xprt == NULL)
337 goto out_no_xprt;
339 /* Choose authentication flavor */
340 authflavor = RPC_AUTH_UNIX;
341 if (data->flags & NFS_MOUNT_SECURE)
342 authflavor = RPC_AUTH_DES;
343 else if (data->flags & NFS_MOUNT_KERBEROS)
344 authflavor = RPC_AUTH_KRB;
346 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
347 version, authflavor);
348 if (clnt == NULL)
349 goto out_no_client;
351 clnt->cl_intr = (data->flags & NFS_MOUNT_INTR)? 1 : 0;
352 clnt->cl_softrtry = (data->flags & NFS_MOUNT_SOFT)? 1 : 0;
353 clnt->cl_droppriv = (data->flags & NFS_MOUNT_BROKEN_SUID) ? 1 : 0;
354 clnt->cl_chatty = 1;
355 server->client = clnt;
357 /* Fire up rpciod if not yet running */
358 if (rpciod_up() != 0)
359 goto out_no_iod;
362 * Keep the super block locked while we try to get
363 * the root fh attributes.
365 /* Did getting the root inode fail? */
366 if (!(root_inode = nfs_get_root(sb, root))
367 && (data->flags & NFS_MOUNT_VER3)) {
368 data->flags &= ~NFS_MOUNT_VER3;
369 rpciod_down();
370 rpc_shutdown_client(server->client);
371 goto nfsv3_try_again;
374 if (!root_inode)
375 goto out_no_root;
376 sb->s_root = d_alloc_root(root_inode);
377 if (!sb->s_root)
378 goto out_no_root;
380 sb->s_root->d_op = &nfs_dentry_operations;
382 /* Get some general file system info */
383 if (server->rpc_ops->statfs(server, root, &fsinfo) >= 0) {
384 if (server->namelen == 0)
385 server->namelen = fsinfo.namelen;
386 } else {
387 printk(KERN_NOTICE "NFS: cannot retrieve file system info.\n");
388 goto out_no_root;
391 /* Work out a lot of parameters */
392 if (data->rsize == 0)
393 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
394 if (data->wsize == 0)
395 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
396 /* NFSv3: we don't have bsize, but rather rtmult and wtmult... */
397 if (!fsinfo.bsize)
398 fsinfo.bsize = (fsinfo.rtmult>fsinfo.wtmult) ? fsinfo.rtmult : fsinfo.wtmult;
399 /* Also make sure we don't go below rsize/wsize since
400 * RPC calls are expensive */
401 if (fsinfo.bsize < server->rsize)
402 fsinfo.bsize = server->rsize;
403 if (fsinfo.bsize < server->wsize)
404 fsinfo.bsize = server->wsize;
406 if (data->bsize == 0)
407 sb->s_blocksize = nfs_block_bits(fsinfo.bsize, &sb->s_blocksize_bits);
408 if (server->rsize > fsinfo.rtmax)
409 server->rsize = fsinfo.rtmax;
410 if (server->wsize > fsinfo.wtmax)
411 server->wsize = fsinfo.wtmax;
413 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
414 if (server->rpages > NFS_READ_MAXIOV) {
415 server->rpages = NFS_READ_MAXIOV;
416 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
419 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
420 if (server->wpages > NFS_WRITE_MAXIOV) {
421 server->wpages = NFS_WRITE_MAXIOV;
422 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
425 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
426 if (server->dtsize > PAGE_CACHE_SIZE)
427 server->dtsize = PAGE_CACHE_SIZE;
428 if (server->dtsize > server->rsize)
429 server->dtsize = server->rsize;
431 maxlen = (version == 2) ? NFS2_MAXNAMLEN : NFS3_MAXNAMLEN;
433 if (server->namelen == 0 || server->namelen > maxlen)
434 server->namelen = maxlen;
436 /* Fire up the writeback cache */
437 if (nfs_reqlist_alloc(server) < 0) {
438 printk(KERN_NOTICE "NFS: cannot initialize writeback cache.\n");
439 goto failure_kill_reqlist;
442 /* We're airborne */
444 /* Check whether to start the lockd process */
445 if (!(server->flags & NFS_MOUNT_NONLM))
446 lockd_up();
447 return sb;
449 /* Yargs. It didn't work out. */
450 failure_kill_reqlist:
451 nfs_reqlist_exit(server);
452 out_no_root:
453 printk("nfs_read_super: get root inode failed\n");
454 iput(root_inode);
455 rpciod_down();
456 goto out_shutdown;
458 out_no_iod:
459 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
460 out_shutdown:
461 rpc_shutdown_client(server->client);
462 goto out_free_host;
464 out_no_client:
465 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
466 xprt_destroy(xprt);
467 goto out_free_host;
469 out_no_xprt:
470 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
472 out_free_host:
473 nfs_reqlist_free(server);
474 kfree(server->hostname);
475 out_unlock:
476 goto out_fail;
478 out_no_remote:
479 printk("NFS: mount program didn't pass remote address!\n");
480 goto out_fail;
482 out_miss_args:
483 printk("nfs_read_super: missing data argument\n");
485 out_fail:
486 return NULL;
489 static int
490 nfs_statfs(struct super_block *sb, struct statfs *buf)
492 struct nfs_server *server = &sb->u.nfs_sb.s_server;
493 unsigned char blockbits;
494 unsigned long blockres;
495 struct nfs_fsinfo res;
496 int error;
498 error = server->rpc_ops->statfs(server, NFS_FH(sb->s_root->d_inode), &res);
499 buf->f_type = NFS_SUPER_MAGIC;
500 if (error < 0)
501 goto out_err;
503 if (res.bsize == 0)
504 res.bsize = sb->s_blocksize;
505 buf->f_bsize = nfs_block_bits(res.bsize, &blockbits);
506 blockres = (1 << blockbits) - 1;
507 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
508 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
509 buf->f_bavail = (res.abytes + blockres) >> blockbits;
510 buf->f_files = res.tfiles;
511 buf->f_ffree = res.afiles;
512 if (res.namelen == 0 || res.namelen > server->namelen)
513 res.namelen = server->namelen;
514 buf->f_namelen = res.namelen;
515 return 0;
516 out_err:
517 printk("nfs_statfs: statfs error = %d\n", -error);
518 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
519 return 0;
523 * Invalidate the local caches
525 void
526 nfs_zap_caches(struct inode *inode)
528 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
529 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
531 invalidate_inode_pages(inode);
533 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
534 NFS_CACHEINV(inode);
538 * Invalidate, but do not unhash, the inode
540 static void
541 nfs_invalidate_inode(struct inode *inode)
543 umode_t save_mode = inode->i_mode;
545 make_bad_inode(inode);
546 inode->i_mode = save_mode;
547 nfs_zap_caches(inode);
551 * Fill in inode information from the fattr.
553 static void
554 nfs_fill_inode(struct inode *inode, struct nfs_fh *fh, struct nfs_fattr *fattr)
557 * Check whether the mode has been set, as we only want to
558 * do this once. (We don't allow inodes to change types.)
560 if (inode->i_mode == 0) {
561 NFS_FILEID(inode) = fattr->fileid;
562 NFS_FSID(inode) = fattr->fsid;
563 inode->i_mode = fattr->mode;
564 /* Why so? Because we want revalidate for devices/FIFOs, and
565 * that's precisely what we have in nfs_file_inode_operations.
567 inode->i_op = &nfs_file_inode_operations;
568 if (S_ISREG(inode->i_mode)) {
569 inode->i_fop = &nfs_file_operations;
570 inode->i_data.a_ops = &nfs_file_aops;
571 } else if (S_ISDIR(inode->i_mode)) {
572 inode->i_op = &nfs_dir_inode_operations;
573 inode->i_fop = &nfs_dir_operations;
574 } else if (S_ISLNK(inode->i_mode))
575 inode->i_op = &nfs_symlink_inode_operations;
576 else
577 init_special_inode(inode, inode->i_mode, fattr->rdev);
579 * Preset the size and mtime, as there's no need
580 * to invalidate the caches.
582 inode->i_size = nfs_size_to_loff_t(fattr->size);
583 inode->i_mtime = nfs_time_to_secs(fattr->mtime);
584 inode->i_atime = nfs_time_to_secs(fattr->atime);
585 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
586 NFS_CACHE_CTIME(inode) = fattr->ctime;
587 NFS_CACHE_MTIME(inode) = fattr->mtime;
588 NFS_CACHE_ATIME(inode) = fattr->atime;
589 NFS_CACHE_ISIZE(inode) = fattr->size;
590 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
591 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
592 memcpy(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh));
594 nfs_refresh_inode(inode, fattr);
598 * In NFSv3 we can have 64bit inode numbers. In order to support
599 * this, and re-exported directories (also seen in NFSv2)
600 * we are forced to allow 2 different inodes to have the same
601 * i_ino.
603 static int
604 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
606 struct nfs_fattr *fattr = (struct nfs_fattr *)opaque;
608 if (NFS_FSID(inode) != fattr->fsid)
609 return 0;
610 if (NFS_FILEID(inode) != fattr->fileid)
611 return 0;
613 return 1;
616 static int
617 nfs_inode_is_stale(struct inode *inode, struct nfs_fh *fh, struct nfs_fattr *fattr)
619 /* Empty inodes are not stale */
620 if (!inode->i_mode)
621 return 0;
623 if ((fattr->mode & S_IFMT) != (inode->i_mode & S_IFMT))
624 return 1;
626 if (is_bad_inode(inode))
627 return 1;
629 /* Has the filehandle changed? If so is the old one stale? */
630 if (memcmp(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh)) != 0 &&
631 __nfs_revalidate_inode(NFS_SERVER(inode),inode) < 0)
632 return 1;
634 return 0;
638 * This is our own version of iget that looks up inodes by file handle
639 * instead of inode number. We use this technique instead of using
640 * the vfs read_inode function because there is no way to pass the
641 * file handle or current attributes into the read_inode function.
643 * We provide a special check for NetApp .snapshot directories to avoid
644 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
646 struct inode *
647 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
648 struct nfs_fattr *fattr)
650 struct super_block *sb = dentry->d_sb;
652 dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
653 dentry->d_parent->d_name.name, dentry->d_name.name,
654 (long long)fattr->fileid);
656 #ifdef CONFIG_NFS_SNAPSHOT
658 * Check for NetApp snapshot dentries, and get an
659 * unhashed inode to avoid aliasing problems.
661 if ((dentry->d_parent->d_inode->u.nfs_i.flags & NFS_IS_SNAPSHOT) ||
662 (dentry->d_name.len == 9 &&
663 memcmp(dentry->d_name.name, ".snapshot", 9) == 0)) {
664 struct inode *inode = new_inode(sb);
665 if (!inode)
666 goto out;
667 inode->i_ino = nfs_fattr_to_ino_t(fattr);
668 nfs_read_inode(inode);
669 nfs_fill_inode(inode, fhandle, fattr);
670 inode->u.nfs_i.flags |= NFS_IS_SNAPSHOT;
671 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode->i_ino);
672 out:
673 return inode;
675 #endif
676 return __nfs_fhget(sb, fhandle, fattr);
680 * Look up the inode by super block and fattr->fileid.
682 * Note carefully the special handling of busy inodes (i_count > 1).
683 * With the kernel 2.1.xx dcache all inodes except hard links must
684 * have i_count == 1 after iget(). Otherwise, it indicates that the
685 * server has reused a fileid (i_ino) and we have a stale inode.
687 static struct inode *
688 __nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
690 struct inode *inode = NULL;
691 unsigned long ino;
693 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
694 goto out_no_inode;
696 if (!fattr->nlink) {
697 printk("NFS: Buggy server - nlink == 0!\n");
698 goto out_no_inode;
701 ino = nfs_fattr_to_ino_t(fattr);
703 while((inode = iget4(sb, ino, nfs_find_actor, fattr)) != NULL) {
706 * Check for busy inodes, and attempt to get rid of any
707 * unused local references. If successful, we release the
708 * inode and try again.
710 * Note that the busy test uses the values in the fattr,
711 * as the inode may have become a different object.
712 * (We can probably handle modes changes here, too.)
714 if (!nfs_inode_is_stale(inode, fh, fattr))
715 break;
717 dprintk("__nfs_fhget: inode (%x/%Ld) still busy, i_count=%d\n",
718 inode->i_dev, (long long)NFS_FILEID(inode),
719 atomic_read(&inode->i_count));
720 nfs_zap_caches(inode);
721 remove_inode_hash(inode);
722 iput(inode);
725 if (!inode)
726 goto out_no_inode;
728 nfs_fill_inode(inode, fh, fattr);
729 dprintk("NFS: __nfs_fhget(%x/%Ld ct=%d)\n",
730 inode->i_dev, (long long)NFS_FILEID(inode),
731 atomic_read(&inode->i_count));
733 out:
734 return inode;
736 out_no_inode:
737 printk("__nfs_fhget: iget failed\n");
738 goto out;
742 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
744 struct inode *inode = dentry->d_inode;
745 struct nfs_fattr fattr;
746 int error;
749 * Make sure the inode is up-to-date.
751 error = nfs_revalidate(dentry);
752 if (error) {
753 #ifdef NFS_PARANOIA
754 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
755 #endif
756 goto out;
759 if (!S_ISREG(inode->i_mode))
760 attr->ia_valid &= ~ATTR_SIZE;
762 error = nfs_wb_all(inode);
763 if (error)
764 goto out;
766 error = NFS_PROTO(inode)->setattr(inode, &fattr, attr);
767 if (error)
768 goto out;
770 * If we changed the size or mtime, update the inode
771 * now to avoid invalidating the page cache.
773 if (attr->ia_valid & ATTR_SIZE) {
774 if (attr->ia_size != fattr.size)
775 printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
776 (long long) attr->ia_size, (long long)fattr.size);
777 vmtruncate(inode, attr->ia_size);
781 * If we changed the size or mtime, update the inode
782 * now to avoid invalidating the page cache.
784 if (!(fattr.valid & NFS_ATTR_WCC)) {
785 fattr.pre_size = NFS_CACHE_ISIZE(inode);
786 fattr.pre_mtime = NFS_CACHE_MTIME(inode);
787 fattr.pre_ctime = NFS_CACHE_CTIME(inode);
788 fattr.valid |= NFS_ATTR_WCC;
790 error = nfs_refresh_inode(inode, &fattr);
791 out:
792 return error;
796 * Wait for the inode to get unlocked.
797 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
800 nfs_wait_on_inode(struct inode *inode, int flag)
802 struct rpc_clnt *clnt = NFS_CLIENT(inode);
803 int error;
804 if (!(NFS_FLAGS(inode) & flag))
805 return 0;
806 atomic_inc(&inode->i_count);
807 error = nfs_wait_event(clnt, inode->i_wait, !(NFS_FLAGS(inode) & flag));
808 iput(inode);
809 return error;
813 * Externally visible revalidation function
816 nfs_revalidate(struct dentry *dentry)
818 struct inode *inode = dentry->d_inode;
819 return nfs_revalidate_inode(NFS_SERVER(inode), inode);
823 * These are probably going to contain hooks for
824 * allocating and releasing RPC credentials for
825 * the file. I'll have to think about Tronds patch
826 * a bit more..
828 int nfs_open(struct inode *inode, struct file *filp)
830 struct rpc_auth *auth;
831 struct rpc_cred *cred;
833 lock_kernel();
834 auth = NFS_CLIENT(inode)->cl_auth;
835 cred = rpcauth_lookupcred(auth, 0);
836 filp->private_data = cred;
837 unlock_kernel();
838 return 0;
841 int nfs_release(struct inode *inode, struct file *filp)
843 struct rpc_auth *auth;
844 struct rpc_cred *cred;
846 lock_kernel();
847 auth = NFS_CLIENT(inode)->cl_auth;
848 cred = nfs_file_cred(filp);
849 if (cred)
850 rpcauth_releasecred(auth, cred);
851 unlock_kernel();
852 return 0;
856 * This function is called whenever some part of NFS notices that
857 * the cached attributes have to be refreshed.
860 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
862 int status = 0;
863 struct nfs_fattr fattr;
865 dfprintk(PAGECACHE, "NFS: revalidating (%x/%Ld)\n",
866 inode->i_dev, (long long)NFS_FILEID(inode));
868 lock_kernel();
869 if (!inode || is_bad_inode(inode)) {
870 unlock_kernel();
871 return -ESTALE;
874 while (NFS_REVALIDATING(inode)) {
875 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
876 if (status < 0) {
877 unlock_kernel();
878 return status;
880 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode))) {
881 unlock_kernel();
882 return 0;
885 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
887 status = NFS_PROTO(inode)->getattr(inode, &fattr);
888 if (status) {
889 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) getattr failed, error=%d\n",
890 inode->i_dev, (long long)NFS_FILEID(inode), status);
891 goto out;
894 status = nfs_refresh_inode(inode, &fattr);
895 if (status) {
896 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) refresh failed, error=%d\n",
897 inode->i_dev, (long long)NFS_FILEID(inode), status);
898 goto out;
900 dfprintk(PAGECACHE, "NFS: (%x/%Ld) revalidation complete\n",
901 inode->i_dev, (long long)NFS_FILEID(inode));
902 out:
903 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
904 wake_up(&inode->i_wait);
905 unlock_kernel();
906 return status;
910 * Many nfs protocol calls return the new file attributes after
911 * an operation. Here we update the inode to reflect the state
912 * of the server's inode.
914 * This is a bit tricky because we have to make sure all dirty pages
915 * have been sent off to the server before calling invalidate_inode_pages.
916 * To make sure no other process adds more write requests while we try
917 * our best to flush them, we make them sleep during the attribute refresh.
919 * A very similar scenario holds for the dir cache.
922 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
924 __u64 new_size, new_mtime;
925 loff_t new_isize;
926 int invalid = 0;
927 int error = -EIO;
929 if (!inode || !fattr) {
930 printk(KERN_ERR "nfs_refresh_inode: inode or fattr is NULL\n");
931 goto out;
933 if (inode->i_mode == 0) {
934 printk(KERN_ERR "nfs_refresh_inode: empty inode\n");
935 goto out;
938 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
939 goto out;
941 if (is_bad_inode(inode))
942 goto out;
944 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
945 inode->i_dev, inode->i_ino,
946 atomic_read(&inode->i_count), fattr->valid);
949 if (NFS_FSID(inode) != fattr->fsid ||
950 NFS_FILEID(inode) != fattr->fileid) {
951 printk(KERN_ERR "nfs_refresh_inode: inode number mismatch\n"
952 "expected (0x%Lx/0x%Lx), got (0x%Lx/0x%Lx)\n",
953 (long long)NFS_FSID(inode), (long long)NFS_FILEID(inode),
954 (long long)fattr->fsid, (long long)fattr->fileid);
955 goto out;
959 * Make sure the inode's type hasn't changed.
961 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
962 goto out_changed;
964 new_mtime = fattr->mtime;
965 new_size = fattr->size;
966 new_isize = nfs_size_to_loff_t(fattr->size);
968 error = 0;
971 * Update the read time so we don't revalidate too often.
973 NFS_READTIME(inode) = jiffies;
976 * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
977 * NOT inode->i_size!!!
979 if (NFS_CACHE_ISIZE(inode) != new_size) {
980 #ifdef NFS_DEBUG_VERBOSE
981 printk(KERN_DEBUG "NFS: isize change on %x/%ld\n", inode->i_dev, inode->i_ino);
982 #endif
983 invalid = 1;
987 * Note: we don't check inode->i_mtime since pipes etc.
988 * can change this value in VFS without requiring a
989 * cache revalidation.
991 if (NFS_CACHE_MTIME(inode) != new_mtime) {
992 #ifdef NFS_DEBUG_VERBOSE
993 printk(KERN_DEBUG "NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
994 #endif
995 invalid = 1;
998 /* Check Weak Cache Consistency data.
999 * If size and mtime match the pre-operation values, we can
1000 * assume that any attribute changes were caused by our NFS
1001 * operation, so there's no need to invalidate the caches.
1003 if ((fattr->valid & NFS_ATTR_WCC)
1004 && NFS_CACHE_ISIZE(inode) == fattr->pre_size
1005 && NFS_CACHE_MTIME(inode) == fattr->pre_mtime) {
1006 invalid = 0;
1010 * If we have pending writebacks, things can get
1011 * messy.
1013 if (nfs_have_writebacks(inode) && new_isize < inode->i_size)
1014 new_isize = inode->i_size;
1016 NFS_CACHE_CTIME(inode) = fattr->ctime;
1017 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
1018 /* If we've been messing around with atime, don't
1019 * update it. Save the server value in NFS_CACHE_ATIME.
1021 NFS_CACHE_ATIME(inode) = fattr->atime;
1022 if (time_before(inode->i_atime, nfs_time_to_secs(fattr->atime)))
1023 inode->i_atime = nfs_time_to_secs(fattr->atime);
1025 NFS_CACHE_MTIME(inode) = new_mtime;
1026 inode->i_mtime = nfs_time_to_secs(new_mtime);
1028 NFS_CACHE_ISIZE(inode) = new_size;
1029 inode->i_size = new_isize;
1031 inode->i_mode = fattr->mode;
1032 inode->i_nlink = fattr->nlink;
1033 inode->i_uid = fattr->uid;
1034 inode->i_gid = fattr->gid;
1036 if (fattr->valid & NFS_ATTR_FATTR_V3) {
1038 * report the blocks in 512byte units
1040 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1041 inode->i_blksize = inode->i_sb->s_blocksize;
1042 } else {
1043 inode->i_blocks = fattr->du.nfs2.blocks;
1044 inode->i_blksize = fattr->du.nfs2.blocksize;
1046 inode->i_rdev = 0;
1047 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1048 inode->i_rdev = to_kdev_t(fattr->rdev);
1050 /* Update attrtimeo value */
1051 if (!invalid && time_after(jiffies, NFS_ATTRTIMEO_UPDATE(inode)+NFS_ATTRTIMEO(inode))) {
1052 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
1053 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
1054 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1057 if (invalid)
1058 nfs_zap_caches(inode);
1060 out:
1061 return error;
1063 out_changed:
1065 * Big trouble! The inode has become a different object.
1067 #ifdef NFS_PARANOIA
1068 printk(KERN_DEBUG "nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1069 inode->i_ino, inode->i_mode, fattr->mode);
1070 #endif
1072 * No need to worry about unhashing the dentry, as the
1073 * lookup validation will know that the inode is bad.
1074 * (But we fall through to invalidate the caches.)
1076 nfs_invalidate_inode(inode);
1077 goto out;
1081 * File system information
1083 static DECLARE_FSTYPE(nfs_fs_type, "nfs", nfs_read_super, FS_ODD_RENAME);
1085 extern int nfs_init_nfspagecache(void);
1086 extern void nfs_destroy_nfspagecache(void);
1087 extern int nfs_init_readpagecache(void);
1088 extern int nfs_destroy_readpagecache(void);
1091 * Initialize NFS
1094 init_nfs_fs(void)
1096 int err;
1098 err = nfs_init_nfspagecache();
1099 if (err)
1100 return err;
1102 err = nfs_init_readpagecache();
1103 if (err)
1104 return err;
1106 #ifdef CONFIG_PROC_FS
1107 rpc_proc_register(&nfs_rpcstat);
1108 #endif
1109 return register_filesystem(&nfs_fs_type);
1113 * Every kernel module contains stuff like this.
1115 #ifdef MODULE
1117 EXPORT_NO_SYMBOLS;
1118 /* Not quite true; I just maintain it */
1119 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1122 init_module(void)
1124 return init_nfs_fs();
1127 void
1128 cleanup_module(void)
1130 nfs_destroy_readpagecache();
1131 nfs_destroy_nfspagecache();
1132 #ifdef CONFIG_PROC_FS
1133 rpc_proc_unregister("nfs");
1134 #endif
1135 unregister_filesystem(&nfs_fs_type);
1137 #endif