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>
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_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
,
58 umount_begin
: nfs_umount_begin
,
64 struct rpc_stat nfs_rpcstat
= { &nfs_program
};
65 static struct rpc_version
* nfs_version
[] = {
74 struct rpc_program nfs_program
= {
77 sizeof(nfs_version
) / sizeof(nfs_version
[0]),
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
96 nfs_read_inode(struct inode
* inode
)
98 inode
->i_blksize
= inode
->i_sb
->s_blocksize
;
101 NFS_FILEID(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;
112 NFS_ATTRTIMEO(inode
) = NFS_MINATTRTIMEO(inode
);
113 NFS_ATTRTIMEO_UPDATE(inode
) = jiffies
;
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
);
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
);
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
--)
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
)
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
;
220 if ((error
= server
->rpc_ops
->getroot(server
, rootfh
, &fattr
)) < 0) {
221 printk(KERN_NOTICE
"nfs_get_root: getattr error = %d\n", -error
);
225 inode
= __nfs_fhget(sb
, &fattr
);
229 extern struct nfs_fh
*nfs_fh_alloc(void);
230 extern void nfs_fh_free(struct nfs_fh
*p
);
233 * The way this works is that the mount process passes a structure
234 * in the data argument which contains the server's IP address
235 * and the root file handle obtained from the server's mount
236 * daemon. We stash these away in the private superblock fields.
239 nfs_read_super(struct super_block
*sb
, void *raw_data
, int silent
)
241 struct nfs_mount_data
*data
= (struct nfs_mount_data
*) raw_data
;
242 struct nfs_server
*server
;
243 struct rpc_xprt
*xprt
= NULL
;
244 struct rpc_clnt
*clnt
= NULL
;
245 struct nfs_fh
*root
= &data
->root
, *root_fh
, fh
;
246 struct inode
*root_inode
= NULL
;
247 unsigned int authflavor
;
248 struct sockaddr_in srvaddr
;
249 struct rpc_timeout timeparms
;
250 struct nfs_fsinfo fsinfo
;
251 int tcp
, version
, maxlen
;
253 memset(&sb
->u
.nfs_sb
, 0, sizeof(sb
->u
.nfs_sb
));
257 memset(&fh
, 0, sizeof(fh
));
258 if (data
->version
!= NFS_MOUNT_VERSION
) {
259 printk("nfs warning: mount version %s than kernel\n",
260 data
->version
< NFS_MOUNT_VERSION
? "older" : "newer");
261 if (data
->version
< 2)
263 if (data
->version
< 3)
265 if (data
->version
< 4) {
266 data
->flags
&= ~NFS_MOUNT_VER3
;
268 root
->size
= NFS2_FHSIZE
;
269 memcpy(root
->data
, data
->old_root
.data
, NFS2_FHSIZE
);
273 /* We now require that the mount process passes the remote address */
274 memcpy(&srvaddr
, &data
->addr
, sizeof(srvaddr
));
275 if (srvaddr
.sin_addr
.s_addr
== INADDR_ANY
)
278 sb
->s_flags
|= MS_ODD_RENAME
; /* This should go away */
280 sb
->s_magic
= NFS_SUPER_MAGIC
;
281 sb
->s_op
= &nfs_sops
;
282 sb
->s_blocksize_bits
= 0;
283 sb
->s_blocksize
= nfs_block_size(data
->bsize
, &sb
->s_blocksize_bits
);
284 server
= &sb
->u
.nfs_sb
.s_server
;
285 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
286 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
287 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
289 if (data
->flags
& NFS_MOUNT_NOAC
) {
290 data
->acregmin
= data
->acregmax
= 0;
291 data
->acdirmin
= data
->acdirmax
= 0;
293 server
->acregmin
= data
->acregmin
*HZ
;
294 server
->acregmax
= data
->acregmax
*HZ
;
295 server
->acdirmin
= data
->acdirmin
*HZ
;
296 server
->acdirmax
= data
->acdirmax
*HZ
;
298 server
->namelen
= data
->namlen
;
299 server
->hostname
= kmalloc(strlen(data
->hostname
) + 1, GFP_KERNEL
);
300 if (!server
->hostname
)
302 strcpy(server
->hostname
, data
->hostname
);
305 /* Check NFS protocol revision and initialize RPC op vector
306 * and file handle pool. */
307 if (data
->flags
& NFS_MOUNT_VER3
) {
309 server
->rpc_ops
= &nfs_v3_clientops
;
311 if (data
->version
< 4) {
312 printk(KERN_NOTICE
"NFS: NFSv3 not supported by mount program.\n");
316 printk(KERN_NOTICE
"NFS: NFSv3 not supported.\n");
320 server
->rpc_ops
= &nfs_v2_clientops
;
324 /* Which protocol do we use? */
325 tcp
= (data
->flags
& NFS_MOUNT_TCP
);
327 /* Initialize timeout values */
328 timeparms
.to_initval
= data
->timeo
* HZ
/ 10;
329 timeparms
.to_retries
= data
->retrans
;
330 timeparms
.to_maxval
= tcp
? RPC_MAX_TCP_TIMEOUT
: RPC_MAX_UDP_TIMEOUT
;
331 timeparms
.to_exponential
= 1;
333 if (!timeparms
.to_initval
)
334 timeparms
.to_initval
= (tcp
? 600 : 11) * HZ
/ 10;
335 if (!timeparms
.to_retries
)
336 timeparms
.to_retries
= 5;
338 /* Now create transport and client */
339 xprt
= xprt_create_proto(tcp
? IPPROTO_TCP
: IPPROTO_UDP
,
340 &srvaddr
, &timeparms
);
344 /* Choose authentication flavor */
345 authflavor
= RPC_AUTH_UNIX
;
346 if (data
->flags
& NFS_MOUNT_SECURE
)
347 authflavor
= RPC_AUTH_DES
;
348 else if (data
->flags
& NFS_MOUNT_KERBEROS
)
349 authflavor
= RPC_AUTH_KRB
;
351 clnt
= rpc_create_client(xprt
, server
->hostname
, &nfs_program
,
352 version
, authflavor
);
356 clnt
->cl_intr
= (data
->flags
& NFS_MOUNT_INTR
)? 1 : 0;
357 clnt
->cl_softrtry
= (data
->flags
& NFS_MOUNT_SOFT
)? 1 : 0;
359 server
->client
= clnt
;
361 /* Fire up rpciod if not yet running */
362 if (rpciod_up() != 0)
366 * Keep the super block locked while we try to get
367 * the root fh attributes.
369 root_fh
= nfs_fh_alloc();
372 memcpy((u8
*)root_fh
, (u8
*)root
, sizeof(*root
));
374 /* Did getting the root inode fail? */
375 if (!(root_inode
= nfs_get_root(sb
, root
))
376 && (data
->flags
& NFS_MOUNT_VER3
)) {
377 data
->flags
&= ~NFS_MOUNT_VER3
;
378 nfs_fh_free(root_fh
);
380 rpc_shutdown_client(server
->client
);
381 goto nfsv3_try_again
;
386 sb
->s_root
= d_alloc_root(root_inode
);
390 sb
->s_root
->d_op
= &nfs_dentry_operations
;
391 sb
->s_root
->d_fsdata
= root_fh
;
393 /* Get some general file system info */
394 if (server
->rpc_ops
->statfs(server
, root
, &fsinfo
) >= 0) {
395 if (server
->namelen
== 0)
396 server
->namelen
= fsinfo
.namelen
;
398 printk(KERN_NOTICE
"NFS: cannot retrieve file system info.\n");
402 /* Work out a lot of parameters */
403 if (data
->rsize
== 0)
404 server
->rsize
= nfs_block_size(fsinfo
.rtpref
, NULL
);
405 if (data
->wsize
== 0)
406 server
->wsize
= nfs_block_size(fsinfo
.wtpref
, NULL
);
407 server
->dtsize
= nfs_block_size(fsinfo
.dtpref
, NULL
);
408 if (server
->dtsize
> PAGE_CACHE_SIZE
)
409 server
->dtsize
= PAGE_CACHE_SIZE
;
410 /* NFSv3: we don't have bsize, but rather rtmult and wtmult... */
412 fsinfo
.bsize
= (fsinfo
.rtmult
>fsinfo
.wtmult
) ? fsinfo
.rtmult
: fsinfo
.wtmult
;
413 /* Also make sure we don't go below rsize/wsize since
414 * RPC calls are expensive */
415 if (fsinfo
.bsize
< server
->rsize
)
416 fsinfo
.bsize
= server
->rsize
;
417 if (fsinfo
.bsize
< server
->wsize
)
418 fsinfo
.bsize
= server
->wsize
;
420 if (data
->bsize
== 0)
421 sb
->s_blocksize
= nfs_block_bits(fsinfo
.bsize
, &sb
->s_blocksize_bits
);
422 if (server
->rsize
> fsinfo
.rtmax
)
423 server
->rsize
= fsinfo
.rtmax
;
424 if (server
->wsize
> fsinfo
.wtmax
)
425 server
->wsize
= fsinfo
.wtmax
;
427 server
->rpages
= (server
->rsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
428 if (server
->rpages
> NFS_READ_MAXIOV
) {
429 server
->rpages
= NFS_READ_MAXIOV
;
430 server
->rsize
= server
->rpages
<< PAGE_CACHE_SHIFT
;
433 server
->wpages
= (server
->wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
434 if (server
->wpages
> NFS_WRITE_MAXIOV
) {
435 server
->wpages
= NFS_WRITE_MAXIOV
;
436 server
->wsize
= server
->wpages
<< PAGE_CACHE_SHIFT
;
439 maxlen
= (version
== 2) ? NFS2_MAXNAMLEN
: NFS3_MAXNAMLEN
;
441 if (server
->namelen
== 0 || server
->namelen
> maxlen
)
442 server
->namelen
= maxlen
;
444 /* Fire up the writeback cache */
445 if (nfs_reqlist_alloc(server
) < 0) {
446 printk(KERN_NOTICE
"NFS: cannot initialize writeback cache.\n");
447 goto failure_kill_reqlist
;
452 /* Check whether to start the lockd process */
453 if (!(server
->flags
& NFS_MOUNT_NONLM
))
457 /* Yargs. It didn't work out. */
458 failure_kill_reqlist
:
459 nfs_reqlist_exit(server
);
461 printk("nfs_read_super: get root inode failed\n");
463 nfs_fh_free(root_fh
);
469 printk(KERN_WARNING
"NFS: couldn't start rpciod!\n");
471 rpc_shutdown_client(server
->client
);
475 printk(KERN_WARNING
"NFS: cannot create RPC client.\n");
480 printk(KERN_WARNING
"NFS: cannot create RPC transport.\n");
483 nfs_reqlist_free(server
);
484 kfree(server
->hostname
);
489 printk("NFS: mount program didn't pass remote address!\n");
493 printk("nfs_read_super: missing data argument\n");
500 nfs_statfs(struct super_block
*sb
, struct statfs
*buf
)
502 struct nfs_server
*server
= &sb
->u
.nfs_sb
.s_server
;
503 unsigned char blockbits
;
504 unsigned long blockres
;
505 struct nfs_fsinfo res
;
508 error
= server
->rpc_ops
->statfs(server
, NFS_FH(sb
->s_root
), &res
);
509 buf
->f_type
= NFS_SUPER_MAGIC
;
514 res
.bsize
= sb
->s_blocksize
;
515 buf
->f_bsize
= nfs_block_bits(res
.bsize
, &blockbits
);
516 blockres
= (1 << blockbits
) - 1;
517 buf
->f_blocks
= (res
.tbytes
+ blockres
) >> blockbits
;
518 buf
->f_bfree
= (res
.fbytes
+ blockres
) >> blockbits
;
519 buf
->f_bavail
= (res
.abytes
+ blockres
) >> blockbits
;
520 buf
->f_files
= res
.tfiles
;
521 buf
->f_ffree
= res
.afiles
;
522 if (res
.namelen
== 0 || res
.namelen
> server
->namelen
)
523 res
.namelen
= server
->namelen
;
524 buf
->f_namelen
= res
.namelen
;
527 printk("nfs_statfs: statfs error = %d\n", -error
);
528 buf
->f_bsize
= buf
->f_blocks
= buf
->f_bfree
= buf
->f_bavail
= -1;
533 * Free all unused dentries in an inode's alias list.
535 * Subtle note: we have to be very careful not to cause
536 * any IO operations with the stale dentries, as this
537 * could cause file corruption. But since the dentry
538 * count is 0 and all pending IO for a dentry has been
539 * flushed when the count went to 0, we're safe here.
540 * Also returns the number of unhashed dentries
543 nfs_free_dentries(struct inode
*inode
)
545 struct list_head
*tmp
, *head
= &inode
->i_dentry
;
548 if (S_ISDIR(inode
->i_mode
)) {
549 struct dentry
*dentry
= d_find_alias(inode
);
551 shrink_dcache_parent(dentry
);
555 d_prune_aliases(inode
);
558 while ((tmp
= tmp
->next
) != head
) {
559 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_alias
);
560 if (d_unhashed(dentry
))
567 * Invalidate the local caches
570 nfs_zap_caches(struct inode
*inode
)
572 NFS_ATTRTIMEO(inode
) = NFS_MINATTRTIMEO(inode
);
573 NFS_ATTRTIMEO_UPDATE(inode
) = jiffies
;
575 invalidate_inode_pages(inode
);
577 memset(NFS_COOKIEVERF(inode
), 0, sizeof(NFS_COOKIEVERF(inode
)));
582 * Invalidate, but do not unhash, the inode
585 nfs_invalidate_inode(struct inode
*inode
)
587 umode_t save_mode
= inode
->i_mode
;
589 make_bad_inode(inode
);
590 inode
->i_mode
= save_mode
;
591 nfs_zap_caches(inode
);
595 * Fill in inode information from the fattr.
598 nfs_fill_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
601 * Check whether the mode has been set, as we only want to
602 * do this once. (We don't allow inodes to change types.)
604 if (inode
->i_mode
== 0) {
605 NFS_FILEID(inode
) = fattr
->fileid
;
606 NFS_FSID(inode
) = fattr
->fsid
;
607 inode
->i_mode
= fattr
->mode
;
608 /* Why so? Because we want revalidate for devices/FIFOs, and
609 * that's precisely what we have in nfs_file_inode_operations.
611 inode
->i_op
= &nfs_file_inode_operations
;
612 if (S_ISREG(inode
->i_mode
)) {
613 inode
->i_fop
= &nfs_file_operations
;
614 inode
->i_data
.a_ops
= &nfs_file_aops
;
615 } else if (S_ISDIR(inode
->i_mode
)) {
616 inode
->i_op
= &nfs_dir_inode_operations
;
617 inode
->i_fop
= &nfs_dir_operations
;
618 } else if (S_ISLNK(inode
->i_mode
))
619 inode
->i_op
= &nfs_symlink_inode_operations
;
621 init_special_inode(inode
, inode
->i_mode
, fattr
->rdev
);
623 * Preset the size and mtime, as there's no need
624 * to invalidate the caches.
626 inode
->i_size
= nfs_size_to_loff_t(fattr
->size
);
627 inode
->i_mtime
= nfs_time_to_secs(fattr
->mtime
);
628 inode
->i_atime
= nfs_time_to_secs(fattr
->atime
);
629 inode
->i_ctime
= nfs_time_to_secs(fattr
->ctime
);
630 NFS_CACHE_CTIME(inode
) = fattr
->ctime
;
631 NFS_CACHE_MTIME(inode
) = fattr
->mtime
;
632 NFS_CACHE_ATIME(inode
) = fattr
->atime
;
633 NFS_CACHE_ISIZE(inode
) = fattr
->size
;
634 NFS_ATTRTIMEO(inode
) = NFS_MINATTRTIMEO(inode
);
635 NFS_ATTRTIMEO_UPDATE(inode
) = jiffies
;
637 nfs_refresh_inode(inode
, fattr
);
641 * In NFSv3 we can have 64bit inode numbers. In order to support
642 * this, and re-exported directories (also seen in NFSv2)
643 * we are forced to allow 2 different inodes to have the same
647 nfs_find_actor(struct inode
*inode
, unsigned long ino
, void *opaque
)
649 struct nfs_fattr
*fattr
= (struct nfs_fattr
*)opaque
;
650 if (NFS_FSID(inode
) != fattr
->fsid
)
652 if (NFS_FILEID(inode
) != fattr
->fileid
)
658 nfs_inode_is_stale(struct inode
*inode
, struct nfs_fattr
*fattr
)
664 (fattr
->mode
& S_IFMT
) != (inode
->i_mode
& S_IFMT
))
667 if (is_bad_inode(inode
))
671 * If the inode seems stale, free up cached dentries.
673 unhashed
= nfs_free_dentries(inode
);
675 /* Assume we're holding an i_count
677 * NB: sockets sometimes have volatile file handles
678 * don't invalidate their inodes even if all dentries are
681 if (unhashed
&& atomic_read(&inode
->i_count
) == unhashed
+ 1
682 && !S_ISSOCK(inode
->i_mode
) && !S_ISFIFO(inode
->i_mode
))
689 * This is our own version of iget that looks up inodes by file handle
690 * instead of inode number. We use this technique instead of using
691 * the vfs read_inode function because there is no way to pass the
692 * file handle or current attributes into the read_inode function.
694 * We provide a special check for NetApp .snapshot directories to avoid
695 * inode aliasing problems. All snapshot inodes are anonymous (unhashed).
698 nfs_fhget(struct dentry
*dentry
, struct nfs_fh
*fhandle
,
699 struct nfs_fattr
*fattr
)
701 struct super_block
*sb
= dentry
->d_sb
;
703 dprintk("NFS: nfs_fhget(%s/%s fileid=%Ld)\n",
704 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
705 (long long)fattr
->fileid
);
707 /* Install the file handle in the dentry */
708 memcpy(dentry
->d_fsdata
, fhandle
, sizeof(struct nfs_fh
));
710 #ifdef CONFIG_NFS_SNAPSHOT
712 * Check for NetApp snapshot dentries, and get an
713 * unhashed inode to avoid aliasing problems.
715 if ((dentry
->d_parent
->d_inode
->u
.nfs_i
.flags
& NFS_IS_SNAPSHOT
) ||
716 (dentry
->d_name
.len
== 9 &&
717 memcmp(dentry
->d_name
.name
, ".snapshot", 9) == 0)) {
718 struct inode
*inode
= get_empty_inode();
722 inode
->i_dev
= sb
->s_dev
;
724 inode
->i_ino
= nfs_fattr_to_ino_t(fattr
);
725 nfs_read_inode(inode
);
726 nfs_fill_inode(inode
, fattr
);
727 inode
->u
.nfs_i
.flags
|= NFS_IS_SNAPSHOT
;
728 dprintk("NFS: nfs_fhget(snapshot ino=%ld)\n", inode
->i_ino
);
733 return __nfs_fhget(sb
, fattr
);
737 * Look up the inode by super block and fattr->fileid.
739 * Note carefully the special handling of busy inodes (i_count > 1).
740 * With the kernel 2.1.xx dcache all inodes except hard links must
741 * have i_count == 1 after iget(). Otherwise, it indicates that the
742 * server has reused a fileid (i_ino) and we have a stale inode.
744 static struct inode
*
745 __nfs_fhget(struct super_block
*sb
, struct nfs_fattr
*fattr
)
747 struct inode
*inode
= NULL
;
750 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
754 printk("NFS: Buggy server - nlink == 0!\n");
758 ino
= nfs_fattr_to_ino_t(fattr
);
760 while((inode
= iget4(sb
, ino
, nfs_find_actor
, fattr
)) != NULL
) {
763 * Check for busy inodes, and attempt to get rid of any
764 * unused local references. If successful, we release the
765 * inode and try again.
767 * Note that the busy test uses the values in the fattr,
768 * as the inode may have become a different object.
769 * (We can probably handle modes changes here, too.)
771 if (!nfs_inode_is_stale(inode
,fattr
))
774 dprintk("__nfs_fhget: inode %ld still busy, i_count=%d\n",
775 inode
->i_ino
, atomic_read(&inode
->i_count
));
776 nfs_zap_caches(inode
);
777 remove_inode_hash(inode
);
784 nfs_fill_inode(inode
, fattr
);
785 dprintk("NFS: __nfs_fhget(%x/%ld ct=%d)\n",
786 inode
->i_dev
, inode
->i_ino
, atomic_read(&inode
->i_count
));
792 printk("__nfs_fhget: iget failed\n");
797 nfs_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
799 struct inode
*inode
= dentry
->d_inode
;
800 struct nfs_fattr fattr
;
804 * Make sure the inode is up-to-date.
806 error
= nfs_revalidate(dentry
);
809 printk("nfs_notify_change: revalidate failed, error=%d\n", error
);
814 if (!S_ISREG(inode
->i_mode
))
815 attr
->ia_valid
&= ~ATTR_SIZE
;
817 error
= nfs_wb_all(inode
);
821 error
= NFS_PROTO(inode
)->setattr(dentry
, &fattr
, attr
);
825 * If we changed the size or mtime, update the inode
826 * now to avoid invalidating the page cache.
828 if (attr
->ia_valid
& ATTR_SIZE
) {
829 if (attr
->ia_size
!= fattr
.size
)
830 printk("nfs_notify_change: attr=%Ld, fattr=%Ld??\n",
831 (long long) attr
->ia_size
, (long long)fattr
.size
);
832 vmtruncate(inode
, attr
->ia_size
);
836 * If we changed the size or mtime, update the inode
837 * now to avoid invalidating the page cache.
839 if (!(fattr
.valid
& NFS_ATTR_WCC
)) {
840 fattr
.pre_size
= NFS_CACHE_ISIZE(inode
);
841 fattr
.pre_mtime
= NFS_CACHE_MTIME(inode
);
842 fattr
.pre_ctime
= NFS_CACHE_CTIME(inode
);
843 fattr
.valid
|= NFS_ATTR_WCC
;
845 error
= nfs_refresh_inode(inode
, &fattr
);
851 * Wait for the inode to get unlocked.
852 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
855 nfs_wait_on_inode(struct inode
*inode
, int flag
)
857 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
859 if (!(NFS_FLAGS(inode
) & flag
))
861 atomic_inc(&inode
->i_count
);
862 error
= nfs_wait_event(clnt
, inode
->i_wait
, !(NFS_FLAGS(inode
) & flag
));
868 * Externally visible revalidation function
871 nfs_revalidate(struct dentry
*dentry
)
873 return nfs_revalidate_inode(NFS_DSERVER(dentry
), dentry
);
877 * These are probably going to contain hooks for
878 * allocating and releasing RPC credentials for
879 * the file. I'll have to think about Tronds patch
882 int nfs_open(struct inode
*inode
, struct file
*filp
)
884 struct rpc_auth
*auth
= NFS_CLIENT(inode
)->cl_auth
;
885 struct rpc_cred
*cred
= rpcauth_lookupcred(auth
, 0);
887 filp
->private_data
= cred
;
891 int nfs_release(struct inode
*inode
, struct file
*filp
)
893 struct rpc_auth
*auth
= NFS_CLIENT(inode
)->cl_auth
;
894 struct rpc_cred
*cred
= nfs_file_cred(filp
);
897 rpcauth_releasecred(auth
, cred
);
902 * This function is called whenever some part of NFS notices that
903 * the cached attributes have to be refreshed.
906 __nfs_revalidate_inode(struct nfs_server
*server
, struct dentry
*dentry
)
908 struct inode
*inode
= dentry
->d_inode
;
910 struct nfs_fattr fattr
;
912 dfprintk(PAGECACHE
, "NFS: revalidating %s/%s, ino=%ld\n",
913 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
916 if (!inode
|| is_bad_inode(inode
))
919 while (NFS_REVALIDATING(inode
)) {
920 status
= nfs_wait_on_inode(inode
, NFS_INO_REVALIDATING
);
923 if (time_before(jiffies
,NFS_READTIME(inode
)+NFS_ATTRTIMEO(inode
)))
926 NFS_FLAGS(inode
) |= NFS_INO_REVALIDATING
;
928 status
= NFS_PROTO(inode
)->getattr(dentry
, &fattr
);
930 struct dentry
*dir
= dentry
->d_parent
;
931 struct inode
*dir_i
= dir
->d_inode
;
934 struct nfs_fh fhandle
;
935 dfprintk(PAGECACHE
, "nfs_revalidate_inode: %s/%s getattr failed, ino=%ld, error=%d\n",
936 dir
->d_name
.name
, dentry
->d_name
.name
,
937 inode
->i_ino
, status
);
938 if (status
!= -ESTALE
)
941 * A "stale filehandle" error ... show the current fh
942 * and find out what the filehandle should be.
944 fh
= (u32
*) NFS_FH(dentry
)->data
;
945 dfprintk(PAGECACHE
, "NFS: bad fh %08x%08x%08x%08x%08x%08x%08x%08x\n",
946 fh
[0],fh
[1],fh
[2],fh
[3],fh
[4],fh
[5],fh
[6],fh
[7]);
947 error
= NFS_PROTO(dir_i
)->lookup(dir
, &dentry
->d_name
,
950 dfprintk(PAGECACHE
, "NFS: lookup failed, error=%d\n", error
);
953 fh
= (u32
*) fhandle
.data
;
954 dfprintk(PAGECACHE
, " %08x%08x%08x%08x%08x%08x%08x%08x\n",
955 fh
[0],fh
[1],fh
[2],fh
[3],fh
[4],fh
[5],fh
[6],fh
[7]);
959 status
= nfs_refresh_inode(inode
, &fattr
);
961 dfprintk(PAGECACHE
, "nfs_revalidate_inode: %s/%s refresh failed, ino=%ld, error=%d\n",
962 dentry
->d_parent
->d_name
.name
,
963 dentry
->d_name
.name
, inode
->i_ino
, status
);
966 dfprintk(PAGECACHE
, "NFS: %s/%s revalidation complete\n",
967 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
969 NFS_FLAGS(inode
) &= ~NFS_INO_REVALIDATING
;
970 wake_up(&inode
->i_wait
);
975 * Many nfs protocol calls return the new file attributes after
976 * an operation. Here we update the inode to reflect the state
977 * of the server's inode.
979 * This is a bit tricky because we have to make sure all dirty pages
980 * have been sent off to the server before calling invalidate_inode_pages.
981 * To make sure no other process adds more write requests while we try
982 * our best to flush them, we make them sleep during the attribute refresh.
984 * A very similar scenario holds for the dir cache.
987 nfs_refresh_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
989 __u64 new_size
, new_mtime
;
994 if (!inode
|| !fattr
) {
995 printk(KERN_ERR
"nfs_refresh_inode: inode or fattr is NULL\n");
998 if (inode
->i_mode
== 0) {
999 printk(KERN_ERR
"nfs_refresh_inode: empty inode\n");
1003 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
1006 if (is_bad_inode(inode
))
1009 dfprintk(VFS
, "NFS: refresh_inode(%x/%ld ct=%d info=0x%x)\n",
1010 inode
->i_dev
, inode
->i_ino
,
1011 atomic_read(&inode
->i_count
), fattr
->valid
);
1014 if (NFS_FSID(inode
) != fattr
->fsid
||
1015 NFS_FILEID(inode
) != fattr
->fileid
) {
1016 printk(KERN_ERR
"nfs_refresh_inode: inode number mismatch\n"
1017 "expected (0x%Lx/0x%Lx), got (0x%Lx/0x%Lx)\n",
1018 (long long)NFS_FSID(inode
), (long long)NFS_FILEID(inode
),
1019 (long long)fattr
->fsid
, (long long)fattr
->fileid
);
1024 * Make sure the inode's type hasn't changed.
1026 if ((inode
->i_mode
& S_IFMT
) != (fattr
->mode
& S_IFMT
))
1029 new_mtime
= fattr
->mtime
;
1030 new_size
= fattr
->size
;
1031 new_isize
= nfs_size_to_loff_t(fattr
->size
);
1036 * Update the read time so we don't revalidate too often.
1038 NFS_READTIME(inode
) = jiffies
;
1041 * Note: NFS_CACHE_ISIZE(inode) reflects the state of the cache.
1042 * NOT inode->i_size!!!
1044 if (NFS_CACHE_ISIZE(inode
) != new_size
) {
1045 #ifdef NFS_DEBUG_VERBOSE
1046 printk(KERN_DEBUG
"NFS: isize change on %x/%ld\n", inode
->i_dev
, inode
->i_ino
);
1052 * Note: we don't check inode->i_mtime since pipes etc.
1053 * can change this value in VFS without requiring a
1054 * cache revalidation.
1056 if (NFS_CACHE_MTIME(inode
) != new_mtime
) {
1057 #ifdef NFS_DEBUG_VERBOSE
1058 printk(KERN_DEBUG
"NFS: mtime change on %x/%ld\n", inode
->i_dev
, inode
->i_ino
);
1063 /* Check Weak Cache Consistency data.
1064 * If size and mtime match the pre-operation values, we can
1065 * assume that any attribute changes were caused by our NFS
1066 * operation, so there's no need to invalidate the caches.
1068 if ((fattr
->valid
& NFS_ATTR_WCC
)
1069 && NFS_CACHE_ISIZE(inode
) == fattr
->pre_size
1070 && NFS_CACHE_MTIME(inode
) == fattr
->pre_mtime
) {
1075 * If we have pending writebacks, things can get
1078 if (nfs_have_writebacks(inode
) && new_isize
< inode
->i_size
)
1079 new_isize
= inode
->i_size
;
1081 NFS_CACHE_CTIME(inode
) = fattr
->ctime
;
1082 inode
->i_ctime
= nfs_time_to_secs(fattr
->ctime
);
1083 /* If we've been messing around with atime, don't
1084 * update it. Save the server value in NFS_CACHE_ATIME.
1086 NFS_CACHE_ATIME(inode
) = fattr
->atime
;
1087 if (time_before(inode
->i_atime
, nfs_time_to_secs(fattr
->atime
)))
1088 inode
->i_atime
= nfs_time_to_secs(fattr
->atime
);
1090 NFS_CACHE_MTIME(inode
) = new_mtime
;
1091 inode
->i_mtime
= nfs_time_to_secs(new_mtime
);
1093 NFS_CACHE_ISIZE(inode
) = new_size
;
1094 inode
->i_size
= new_isize
;
1096 inode
->i_mode
= fattr
->mode
;
1097 inode
->i_nlink
= fattr
->nlink
;
1098 inode
->i_uid
= fattr
->uid
;
1099 inode
->i_gid
= fattr
->gid
;
1101 if (fattr
->valid
& NFS_ATTR_FATTR_V3
) {
1103 * report the blocks in 512byte units
1105 inode
->i_blocks
= nfs_calc_block_size(fattr
->du
.nfs3
.used
);
1106 inode
->i_blksize
= inode
->i_sb
->s_blocksize
;
1108 inode
->i_blocks
= fattr
->du
.nfs2
.blocks
;
1109 inode
->i_blksize
= fattr
->du
.nfs2
.blocksize
;
1112 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
1113 inode
->i_rdev
= to_kdev_t(fattr
->rdev
);
1115 /* Update attrtimeo value */
1116 if (!invalid
&& time_after(jiffies
, NFS_ATTRTIMEO_UPDATE(inode
)+NFS_ATTRTIMEO(inode
))) {
1117 if ((NFS_ATTRTIMEO(inode
) <<= 1) > NFS_MAXATTRTIMEO(inode
))
1118 NFS_ATTRTIMEO(inode
) = NFS_MAXATTRTIMEO(inode
);
1119 NFS_ATTRTIMEO_UPDATE(inode
) = jiffies
;
1123 nfs_zap_caches(inode
);
1130 * Big trouble! The inode has become a different object.
1133 printk(KERN_DEBUG
"nfs_refresh_inode: inode %ld mode changed, %07o to %07o\n",
1134 inode
->i_ino
, inode
->i_mode
, fattr
->mode
);
1137 * No need to worry about unhashing the dentry, as the
1138 * lookup validation will know that the inode is bad.
1139 * (But we fall through to invalidate the caches.)
1141 nfs_invalidate_inode(inode
);
1146 * File system information
1148 static DECLARE_FSTYPE(nfs_fs_type
, "nfs", nfs_read_super
, 0);
1150 extern int nfs_init_fhcache(void);
1151 extern void nfs_destroy_fhcache(void);
1152 extern int nfs_init_nfspagecache(void);
1153 extern void nfs_destroy_nfspagecache(void);
1154 extern int nfs_init_readpagecache(void);
1155 extern int nfs_destroy_readpagecache(void);
1165 err
= nfs_init_fhcache();
1169 err
= nfs_init_nfspagecache();
1173 err
= nfs_init_readpagecache();
1177 #ifdef CONFIG_PROC_FS
1178 rpc_proc_register(&nfs_rpcstat
);
1180 return register_filesystem(&nfs_fs_type
);
1184 * Every kernel module contains stuff like this.
1189 /* Not quite true; I just maintain it */
1190 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1195 return init_nfs_fs();
1199 cleanup_module(void)
1201 nfs_destroy_readpagecache();
1202 nfs_destroy_nfspagecache();
1203 nfs_destroy_fhcache();
1204 #ifdef CONFIG_PROC_FS
1205 rpc_proc_unregister("nfs");
1207 unregister_filesystem(&nfs_fs_type
);