- David Miller: sparc and net updates. Fix merge_segments.
[davej-history.git] / fs / nfs / inode.c
blobf741cb6ddfe2b3570a5de0b8b4f25be93d15140e
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);
597 struct nfs_find_desc {
598 struct nfs_fh *fh;
599 struct nfs_fattr *fattr;
603 * In NFSv3 we can have 64bit inode numbers. In order to support
604 * this, and re-exported directories (also seen in NFSv2)
605 * we are forced to allow 2 different inodes to have the same
606 * i_ino.
608 static int
609 nfs_find_actor(struct inode *inode, unsigned long ino, void *opaque)
611 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
612 struct nfs_fh *fh = desc->fh;
613 struct nfs_fattr *fattr = desc->fattr;
615 if (NFS_FSID(inode) != fattr->fsid)
616 return 0;
617 if (NFS_FILEID(inode) != fattr->fileid)
618 return 0;
619 if (memcmp(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh)) != 0)
620 return 0;
621 return 1;
625 nfs_inode_is_stale(struct inode *inode, struct nfs_fh *fh, struct nfs_fattr *fattr)
627 /* Empty inodes are not stale */
628 if (!inode->i_mode)
629 return 0;
631 if ((fattr->mode & S_IFMT) != (inode->i_mode & S_IFMT))
632 return 1;
634 if (is_bad_inode(inode))
635 return 1;
637 /* Has the filehandle changed? If so is the old one stale? */
638 if (memcmp(&inode->u.nfs_i.fh, fh, sizeof(inode->u.nfs_i.fh)) != 0 &&
639 __nfs_revalidate_inode(NFS_SERVER(inode),inode) == -ESTALE)
640 return 1;
642 return 0;
646 * This is our own version of iget that looks up inodes by file handle
647 * instead of inode number. We use this technique instead of using
648 * the vfs read_inode function because there is no way to pass the
649 * file handle or current attributes into the read_inode function.
652 struct inode *
653 nfs_fhget(struct dentry *dentry, struct nfs_fh *fhandle,
654 struct nfs_fattr *fattr)
656 struct super_block *sb = dentry->d_sb;
658 dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
659 dentry->d_parent->d_name.name, dentry->d_name.name,
660 (long long)fattr->fileid);
661 return __nfs_fhget(sb, fhandle, fattr);
665 * Look up the inode by super block and fattr->fileid.
667 static struct inode *
668 __nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
670 struct nfs_find_desc desc = { fh, fattr };
671 struct inode *inode = NULL;
672 unsigned long ino;
674 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
675 goto out_no_inode;
677 if (!fattr->nlink) {
678 printk("NFS: Buggy server - nlink == 0!\n");
679 goto out_no_inode;
682 ino = nfs_fattr_to_ino_t(fattr);
684 if (!(inode = iget4(sb, ino, nfs_find_actor, &desc)))
685 goto out_no_inode;
687 nfs_fill_inode(inode, fh, fattr);
688 dprintk("NFS: __nfs_fhget(%x/%Ld ct=%d)\n",
689 inode->i_dev, (long long)NFS_FILEID(inode),
690 atomic_read(&inode->i_count));
692 out:
693 return inode;
695 out_no_inode:
696 printk("__nfs_fhget: iget failed\n");
697 goto out;
701 nfs_notify_change(struct dentry *dentry, struct iattr *attr)
703 struct inode *inode = dentry->d_inode;
704 struct nfs_fattr fattr;
705 int error;
708 * Make sure the inode is up-to-date.
710 error = nfs_revalidate(dentry);
711 if (error) {
712 #ifdef NFS_PARANOIA
713 printk("nfs_notify_change: revalidate failed, error=%d\n", error);
714 #endif
715 goto out;
718 if (!S_ISREG(inode->i_mode))
719 attr->ia_valid &= ~ATTR_SIZE;
721 error = nfs_wb_all(inode);
722 if (error)
723 goto out;
725 error = NFS_PROTO(inode)->setattr(inode, &fattr, attr);
726 if (error)
727 goto out;
729 * If we changed the size or mtime, update the inode
730 * now to avoid invalidating the page cache.
732 if (attr->ia_valid & ATTR_SIZE) {
733 if (attr->ia_size != fattr.size)
734 printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
735 (long long) attr->ia_size, (long long)fattr.size);
736 vmtruncate(inode, attr->ia_size);
740 * If we changed the size or mtime, update the inode
741 * now to avoid invalidating the page cache.
743 if (!(fattr.valid & NFS_ATTR_WCC)) {
744 fattr.pre_size = NFS_CACHE_ISIZE(inode);
745 fattr.pre_mtime = NFS_CACHE_MTIME(inode);
746 fattr.pre_ctime = NFS_CACHE_CTIME(inode);
747 fattr.valid |= NFS_ATTR_WCC;
749 error = nfs_refresh_inode(inode, &fattr);
750 out:
751 return error;
755 * Wait for the inode to get unlocked.
756 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
759 nfs_wait_on_inode(struct inode *inode, int flag)
761 struct rpc_clnt *clnt = NFS_CLIENT(inode);
762 int error;
763 if (!(NFS_FLAGS(inode) & flag))
764 return 0;
765 atomic_inc(&inode->i_count);
766 error = nfs_wait_event(clnt, inode->i_wait, !(NFS_FLAGS(inode) & flag));
767 iput(inode);
768 return error;
772 * Externally visible revalidation function
775 nfs_revalidate(struct dentry *dentry)
777 struct inode *inode = dentry->d_inode;
778 return nfs_revalidate_inode(NFS_SERVER(inode), inode);
782 * These are probably going to contain hooks for
783 * allocating and releasing RPC credentials for
784 * the file. I'll have to think about Tronds patch
785 * a bit more..
787 int nfs_open(struct inode *inode, struct file *filp)
789 struct rpc_auth *auth;
790 struct rpc_cred *cred;
792 lock_kernel();
793 auth = NFS_CLIENT(inode)->cl_auth;
794 cred = rpcauth_lookupcred(auth, 0);
795 filp->private_data = cred;
796 unlock_kernel();
797 return 0;
800 int nfs_release(struct inode *inode, struct file *filp)
802 struct rpc_auth *auth;
803 struct rpc_cred *cred;
805 lock_kernel();
806 auth = NFS_CLIENT(inode)->cl_auth;
807 cred = nfs_file_cred(filp);
808 if (cred)
809 rpcauth_releasecred(auth, cred);
810 unlock_kernel();
811 return 0;
815 * This function is called whenever some part of NFS notices that
816 * the cached attributes have to be refreshed.
819 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
821 int status = 0;
822 struct nfs_fattr fattr;
824 dfprintk(PAGECACHE, "NFS: revalidating (%x/%Ld)\n",
825 inode->i_dev, (long long)NFS_FILEID(inode));
827 lock_kernel();
828 if (!inode || is_bad_inode(inode) || NFS_STALE(inode)) {
829 unlock_kernel();
830 return -ESTALE;
833 while (NFS_REVALIDATING(inode)) {
834 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
835 if (status < 0) {
836 unlock_kernel();
837 return status;
839 if (time_before(jiffies,NFS_READTIME(inode)+NFS_ATTRTIMEO(inode))) {
840 status = NFS_STALE(inode) ? -ESTALE : 0;
841 goto out_nowait;
844 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
846 status = NFS_PROTO(inode)->getattr(inode, &fattr);
847 if (status) {
848 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) getattr failed, error=%d\n",
849 inode->i_dev, (long long)NFS_FILEID(inode), status);
850 if (status == -ESTALE) {
851 NFS_FLAGS(inode) |= NFS_INO_STALE;
852 remove_inode_hash(inode);
854 goto out;
857 status = nfs_refresh_inode(inode, &fattr);
858 if (status) {
859 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%x/%Ld) refresh failed, error=%d\n",
860 inode->i_dev, (long long)NFS_FILEID(inode), status);
861 goto out;
863 dfprintk(PAGECACHE, "NFS: (%x/%Ld) revalidation complete\n",
864 inode->i_dev, (long long)NFS_FILEID(inode));
865 out:
866 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
867 wake_up(&inode->i_wait);
868 out_nowait:
869 unlock_kernel();
870 return status;
874 * Many nfs protocol calls return the new file attributes after
875 * an operation. Here we update the inode to reflect the state
876 * of the server's inode.
878 * This is a bit tricky because we have to make sure all dirty pages
879 * have been sent off to the server before calling invalidate_inode_pages.
880 * To make sure no other process adds more write requests while we try
881 * our best to flush them, we make them sleep during the attribute refresh.
883 * A very similar scenario holds for the dir cache.
886 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
888 __u64 new_size, new_mtime;
889 loff_t new_isize;
890 int invalid = 0;
891 int error = -EIO;
893 if (!inode || !fattr) {
894 printk(KERN_ERR "nfs_refresh_inode: inode or fattr is NULL\n");
895 goto out;
897 if (inode->i_mode == 0) {
898 printk(KERN_ERR "nfs_refresh_inode: empty inode\n");
899 goto out;
902 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
903 goto out;
905 if (is_bad_inode(inode))
906 goto out;
908 dfprintk(VFS, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
909 inode->i_dev, inode->i_ino,
910 atomic_read(&inode->i_count), fattr->valid);
913 if (NFS_FSID(inode) != fattr->fsid ||
914 NFS_FILEID(inode) != fattr->fileid) {
915 printk(KERN_ERR "nfs_refresh_inode: inode number mismatch\n"
916 "expected (0x%Lx/0x%Lx), got (0x%Lx/0x%Lx)\n",
917 (long long)NFS_FSID(inode), (long long)NFS_FILEID(inode),
918 (long long)fattr->fsid, (long long)fattr->fileid);
919 goto out;
923 * Make sure the inode's type hasn't changed.
925 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
926 goto out_changed;
928 new_mtime = fattr->mtime;
929 new_size = fattr->size;
930 new_isize = nfs_size_to_loff_t(fattr->size);
932 error = 0;
935 * Update the read time so we don't revalidate too often.
937 NFS_READTIME(inode) = jiffies;
940 * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
941 * NOT inode->i_size!!!
943 if (NFS_CACHE_ISIZE(inode) != new_size) {
944 #ifdef NFS_DEBUG_VERBOSE
945 printk(KERN_DEBUG "NFS: isize change on %x/%ld\n", inode->i_dev, inode->i_ino);
946 #endif
947 invalid = 1;
951 * Note: we don't check inode->i_mtime since pipes etc.
952 * can change this value in VFS without requiring a
953 * cache revalidation.
955 if (NFS_CACHE_MTIME(inode) != new_mtime) {
956 #ifdef NFS_DEBUG_VERBOSE
957 printk(KERN_DEBUG "NFS: mtime change on %x/%ld\n", inode->i_dev, inode->i_ino);
958 #endif
959 invalid = 1;
962 /* Check Weak Cache Consistency data.
963 * If size and mtime match the pre-operation values, we can
964 * assume that any attribute changes were caused by our NFS
965 * operation, so there's no need to invalidate the caches.
967 if ((fattr->valid & NFS_ATTR_WCC)
968 && NFS_CACHE_ISIZE(inode) == fattr->pre_size
969 && NFS_CACHE_MTIME(inode) == fattr->pre_mtime) {
970 invalid = 0;
974 * If we have pending writebacks, things can get
975 * messy.
977 if (nfs_have_writebacks(inode) && new_isize < inode->i_size)
978 new_isize = inode->i_size;
980 NFS_CACHE_CTIME(inode) = fattr->ctime;
981 inode->i_ctime = nfs_time_to_secs(fattr->ctime);
982 /* If we've been messing around with atime, don't
983 * update it. Save the server value in NFS_CACHE_ATIME.
985 NFS_CACHE_ATIME(inode) = fattr->atime;
986 if (time_before(inode->i_atime, nfs_time_to_secs(fattr->atime)))
987 inode->i_atime = nfs_time_to_secs(fattr->atime);
989 NFS_CACHE_MTIME(inode) = new_mtime;
990 inode->i_mtime = nfs_time_to_secs(new_mtime);
992 NFS_CACHE_ISIZE(inode) = new_size;
993 inode->i_size = new_isize;
995 inode->i_mode = fattr->mode;
996 inode->i_nlink = fattr->nlink;
997 inode->i_uid = fattr->uid;
998 inode->i_gid = fattr->gid;
1000 if (fattr->valid & NFS_ATTR_FATTR_V3) {
1002 * report the blocks in 512byte units
1004 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1005 inode->i_blksize = inode->i_sb->s_blocksize;
1006 } else {
1007 inode->i_blocks = fattr->du.nfs2.blocks;
1008 inode->i_blksize = fattr->du.nfs2.blocksize;
1010 inode->i_rdev = 0;
1011 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1012 inode->i_rdev = to_kdev_t(fattr->rdev);
1014 /* Update attrtimeo value */
1015 if (!invalid && time_after(jiffies, NFS_ATTRTIMEO_UPDATE(inode)+NFS_ATTRTIMEO(inode))) {
1016 if ((NFS_ATTRTIMEO(inode) <<= 1) > NFS_MAXATTRTIMEO(inode))
1017 NFS_ATTRTIMEO(inode) = NFS_MAXATTRTIMEO(inode);
1018 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
1021 if (invalid)
1022 nfs_zap_caches(inode);
1024 out:
1025 return error;
1027 out_changed:
1029 * Big trouble! The inode has become a different object.
1031 #ifdef NFS_PARANOIA
1032 printk(KERN_DEBUG "nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1033 inode->i_ino, inode->i_mode, fattr->mode);
1034 #endif
1036 * No need to worry about unhashing the dentry, as the
1037 * lookup validation will know that the inode is bad.
1038 * (But we fall through to invalidate the caches.)
1040 nfs_invalidate_inode(inode);
1041 goto out;
1045 * File system information
1047 static DECLARE_FSTYPE(nfs_fs_type, "nfs", nfs_read_super, FS_ODD_RENAME);
1049 extern int nfs_init_nfspagecache(void);
1050 extern void nfs_destroy_nfspagecache(void);
1051 extern int nfs_init_readpagecache(void);
1052 extern int nfs_destroy_readpagecache(void);
1055 * Initialize NFS
1058 init_nfs_fs(void)
1060 int err;
1062 err = nfs_init_nfspagecache();
1063 if (err)
1064 return err;
1066 err = nfs_init_readpagecache();
1067 if (err)
1068 return err;
1070 #ifdef CONFIG_PROC_FS
1071 rpc_proc_register(&nfs_rpcstat);
1072 #endif
1073 return register_filesystem(&nfs_fs_type);
1077 * Every kernel module contains stuff like this.
1079 #ifdef MODULE
1081 EXPORT_NO_SYMBOLS;
1082 /* Not quite true; I just maintain it */
1083 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1086 init_module(void)
1088 return init_nfs_fs();
1091 void
1092 cleanup_module(void)
1094 nfs_destroy_readpagecache();
1095 nfs_destroy_nfspagecache();
1096 #ifdef CONFIG_PROC_FS
1097 rpc_proc_unregister("nfs");
1098 #endif
1099 unregister_filesystem(&nfs_fs_type);
1101 #endif