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>
18 #include <linux/init.h>
20 #include <linux/time.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/stat.h>
25 #include <linux/errno.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/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/smp_lock.h>
34 #include <linux/seq_file.h>
35 #include <linux/mount.h>
36 #include <linux/nfs_idmap.h>
37 #include <linux/vfs.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
43 #include "delegation.h"
45 #define NFSDBG_FACILITY NFSDBG_VFS
46 #define NFS_PARANOIA 1
48 /* Maximum number of readahead requests
49 * FIXME: this should really be a sysctl so that users may tune it to suit
50 * their needs. People that do NFS over a slow network, might for
51 * instance want to reduce it to something closer to 1 for improved
52 * interactive response.
54 #define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
56 static void nfs_invalidate_inode(struct inode
*);
57 static int nfs_update_inode(struct inode
*, struct nfs_fattr
*, unsigned long);
59 static struct inode
*nfs_alloc_inode(struct super_block
*sb
);
60 static void nfs_destroy_inode(struct inode
*);
61 static int nfs_write_inode(struct inode
*,int);
62 static void nfs_delete_inode(struct inode
*);
63 static void nfs_clear_inode(struct inode
*);
64 static void nfs_umount_begin(struct super_block
*);
65 static int nfs_statfs(struct super_block
*, struct kstatfs
*);
66 static int nfs_show_options(struct seq_file
*, struct vfsmount
*);
67 static void nfs_zap_acl_cache(struct inode
*);
69 static struct rpc_program nfs_program
;
71 static struct super_operations nfs_sops
= {
72 .alloc_inode
= nfs_alloc_inode
,
73 .destroy_inode
= nfs_destroy_inode
,
74 .write_inode
= nfs_write_inode
,
75 .delete_inode
= nfs_delete_inode
,
77 .clear_inode
= nfs_clear_inode
,
78 .umount_begin
= nfs_umount_begin
,
79 .show_options
= nfs_show_options
,
85 static struct rpc_stat nfs_rpcstat
= {
86 .program
= &nfs_program
88 static struct rpc_version
* nfs_version
[] = {
92 #if defined(CONFIG_NFS_V3)
94 #elif defined(CONFIG_NFS_V4)
97 #if defined(CONFIG_NFS_V4)
102 static struct rpc_program nfs_program
= {
104 .number
= NFS_PROGRAM
,
105 .nrvers
= sizeof(nfs_version
) / sizeof(nfs_version
[0]),
106 .version
= nfs_version
,
107 .stats
= &nfs_rpcstat
,
108 .pipe_dir_name
= "/nfs",
111 static inline unsigned long
112 nfs_fattr_to_ino_t(struct nfs_fattr
*fattr
)
114 return nfs_fileid_to_ino_t(fattr
->fileid
);
118 nfs_write_inode(struct inode
*inode
, int sync
)
120 int flags
= sync
? FLUSH_WAIT
: 0;
123 ret
= nfs_commit_inode(inode
, 0, 0, flags
);
130 nfs_delete_inode(struct inode
* inode
)
132 dprintk("NFS: delete_inode(%s/%ld)\n", inode
->i_sb
->s_id
, inode
->i_ino
);
136 * The following should never happen...
138 if (nfs_have_writebacks(inode
)) {
139 printk(KERN_ERR
"nfs_delete_inode: inode %ld has pending RPC requests\n", inode
->i_ino
);
146 nfs_clear_inode(struct inode
*inode
)
148 struct nfs_inode
*nfsi
= NFS_I(inode
);
149 struct rpc_cred
*cred
;
152 BUG_ON (!list_empty(&nfsi
->open_files
));
153 nfs_zap_acl_cache(inode
);
154 cred
= nfsi
->cache_access
.cred
;
157 BUG_ON(atomic_read(&nfsi
->data_updates
) != 0);
161 nfs_umount_begin(struct super_block
*sb
)
163 struct rpc_clnt
*rpc
= NFS_SB(sb
)->client
;
165 /* -EIO all pending I/O */
167 rpc_killall_tasks(rpc
);
171 static inline unsigned long
172 nfs_block_bits(unsigned long bsize
, unsigned char *nrbitsp
)
174 /* make sure blocksize is a power of two */
175 if ((bsize
& (bsize
- 1)) || nrbitsp
) {
176 unsigned char nrbits
;
178 for (nrbits
= 31; nrbits
&& !(bsize
& (1 << nrbits
)); nrbits
--)
189 * Calculate the number of 512byte blocks used.
191 static inline unsigned long
192 nfs_calc_block_size(u64 tsize
)
194 loff_t used
= (tsize
+ 511) >> 9;
195 return (used
> ULONG_MAX
) ? ULONG_MAX
: used
;
199 * Compute and set NFS server blocksize
201 static inline unsigned long
202 nfs_block_size(unsigned long bsize
, unsigned char *nrbitsp
)
205 bsize
= NFS_DEF_FILE_IO_BUFFER_SIZE
;
206 else if (bsize
>= NFS_MAX_FILE_IO_BUFFER_SIZE
)
207 bsize
= NFS_MAX_FILE_IO_BUFFER_SIZE
;
209 return nfs_block_bits(bsize
, nrbitsp
);
213 * Obtain the root inode of the file system.
215 static struct inode
*
216 nfs_get_root(struct super_block
*sb
, struct nfs_fh
*rootfh
, struct nfs_fsinfo
*fsinfo
)
218 struct nfs_server
*server
= NFS_SB(sb
);
222 error
= server
->rpc_ops
->getroot(server
, rootfh
, fsinfo
);
224 dprintk("nfs_get_root: getattr error = %d\n", -error
);
225 return ERR_PTR(error
);
228 rooti
= nfs_fhget(sb
, rootfh
, fsinfo
->fattr
);
230 return ERR_PTR(-ENOMEM
);
235 * Do NFS version-independent mount processing, and sanity checking
238 nfs_sb_init(struct super_block
*sb
, rpc_authflavor_t authflavor
)
240 struct nfs_server
*server
;
241 struct inode
*root_inode
;
242 struct nfs_fattr fattr
;
243 struct nfs_fsinfo fsinfo
= {
246 struct nfs_pathconf pathinfo
= {
249 int no_root_error
= 0;
250 unsigned long max_rpc_payload
;
252 /* We probably want something more informative here */
253 snprintf(sb
->s_id
, sizeof(sb
->s_id
), "%x:%x", MAJOR(sb
->s_dev
), MINOR(sb
->s_dev
));
257 sb
->s_magic
= NFS_SUPER_MAGIC
;
259 root_inode
= nfs_get_root(sb
, &server
->fh
, &fsinfo
);
260 /* Did getting the root inode fail? */
261 if (IS_ERR(root_inode
)) {
262 no_root_error
= PTR_ERR(root_inode
);
265 sb
->s_root
= d_alloc_root(root_inode
);
267 no_root_error
= -ENOMEM
;
270 sb
->s_root
->d_op
= server
->rpc_ops
->dentry_ops
;
272 /* Get some general file system info */
273 if (server
->namelen
== 0 &&
274 server
->rpc_ops
->pathconf(server
, &server
->fh
, &pathinfo
) >= 0)
275 server
->namelen
= pathinfo
.max_namelen
;
276 /* Work out a lot of parameters */
277 if (server
->rsize
== 0)
278 server
->rsize
= nfs_block_size(fsinfo
.rtpref
, NULL
);
279 if (server
->wsize
== 0)
280 server
->wsize
= nfs_block_size(fsinfo
.wtpref
, NULL
);
282 if (fsinfo
.rtmax
>= 512 && server
->rsize
> fsinfo
.rtmax
)
283 server
->rsize
= nfs_block_size(fsinfo
.rtmax
, NULL
);
284 if (fsinfo
.wtmax
>= 512 && server
->wsize
> fsinfo
.wtmax
)
285 server
->wsize
= nfs_block_size(fsinfo
.wtmax
, NULL
);
287 max_rpc_payload
= nfs_block_size(rpc_max_payload(server
->client
), NULL
);
288 if (server
->rsize
> max_rpc_payload
)
289 server
->rsize
= max_rpc_payload
;
290 if (server
->wsize
> max_rpc_payload
)
291 server
->wsize
= max_rpc_payload
;
293 server
->rpages
= (server
->rsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
294 if (server
->rpages
> NFS_READ_MAXIOV
) {
295 server
->rpages
= NFS_READ_MAXIOV
;
296 server
->rsize
= server
->rpages
<< PAGE_CACHE_SHIFT
;
299 server
->wpages
= (server
->wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
300 if (server
->wpages
> NFS_WRITE_MAXIOV
) {
301 server
->wpages
= NFS_WRITE_MAXIOV
;
302 server
->wsize
= server
->wpages
<< PAGE_CACHE_SHIFT
;
305 if (sb
->s_blocksize
== 0)
306 sb
->s_blocksize
= nfs_block_bits(server
->wsize
,
307 &sb
->s_blocksize_bits
);
308 server
->wtmult
= nfs_block_bits(fsinfo
.wtmult
, NULL
);
310 server
->dtsize
= nfs_block_size(fsinfo
.dtpref
, NULL
);
311 if (server
->dtsize
> PAGE_CACHE_SIZE
)
312 server
->dtsize
= PAGE_CACHE_SIZE
;
313 if (server
->dtsize
> server
->rsize
)
314 server
->dtsize
= server
->rsize
;
316 if (server
->flags
& NFS_MOUNT_NOAC
) {
317 server
->acregmin
= server
->acregmax
= 0;
318 server
->acdirmin
= server
->acdirmax
= 0;
319 sb
->s_flags
|= MS_SYNCHRONOUS
;
321 server
->backing_dev_info
.ra_pages
= server
->rpages
* NFS_MAX_READAHEAD
;
323 sb
->s_maxbytes
= fsinfo
.maxfilesize
;
324 if (sb
->s_maxbytes
> MAX_LFS_FILESIZE
)
325 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
327 server
->client
->cl_intr
= (server
->flags
& NFS_MOUNT_INTR
) ? 1 : 0;
328 server
->client
->cl_softrtry
= (server
->flags
& NFS_MOUNT_SOFT
) ? 1 : 0;
330 /* We're airborne Set socket buffersize */
331 rpc_setbufsize(server
->client
, server
->wsize
+ 100, server
->rsize
+ 100);
333 /* Yargs. It didn't work out. */
335 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error
);
336 if (!IS_ERR(root_inode
))
338 return no_root_error
;
342 * Create an RPC client handle.
344 static struct rpc_clnt
*
345 nfs_create_client(struct nfs_server
*server
, const struct nfs_mount_data
*data
)
347 struct rpc_timeout timeparms
;
348 struct rpc_xprt
*xprt
= NULL
;
349 struct rpc_clnt
*clnt
= NULL
;
350 int tcp
= (data
->flags
& NFS_MOUNT_TCP
);
352 /* Initialize timeout values */
353 timeparms
.to_initval
= data
->timeo
* HZ
/ 10;
354 timeparms
.to_retries
= data
->retrans
;
355 timeparms
.to_maxval
= tcp
? RPC_MAX_TCP_TIMEOUT
: RPC_MAX_UDP_TIMEOUT
;
356 timeparms
.to_exponential
= 1;
358 if (!timeparms
.to_initval
)
359 timeparms
.to_initval
= (tcp
? 600 : 11) * HZ
/ 10;
360 if (!timeparms
.to_retries
)
361 timeparms
.to_retries
= 5;
363 /* create transport and client */
364 xprt
= xprt_create_proto(tcp
? IPPROTO_TCP
: IPPROTO_UDP
,
365 &server
->addr
, &timeparms
);
367 dprintk("%s: cannot create RPC transport. Error = %ld\n",
368 __FUNCTION__
, PTR_ERR(xprt
));
369 return (struct rpc_clnt
*)xprt
;
371 clnt
= rpc_create_client(xprt
, server
->hostname
, &nfs_program
,
372 server
->rpc_ops
->version
, data
->pseudoflavor
);
374 dprintk("%s: cannot create RPC client. Error = %ld\n",
375 __FUNCTION__
, PTR_ERR(xprt
));
380 clnt
->cl_softrtry
= 1;
390 * The way this works is that the mount process passes a structure
391 * in the data argument which contains the server's IP address
392 * and the root file handle obtained from the server's mount
393 * daemon. We stash these away in the private superblock fields.
396 nfs_fill_super(struct super_block
*sb
, struct nfs_mount_data
*data
, int silent
)
398 struct nfs_server
*server
;
399 rpc_authflavor_t authflavor
;
402 sb
->s_blocksize_bits
= 0;
405 sb
->s_blocksize
= nfs_block_size(data
->bsize
, &sb
->s_blocksize_bits
);
407 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
409 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
410 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
412 server
->acregmin
= data
->acregmin
*HZ
;
413 server
->acregmax
= data
->acregmax
*HZ
;
414 server
->acdirmin
= data
->acdirmin
*HZ
;
415 server
->acdirmax
= data
->acdirmax
*HZ
;
417 /* Start lockd here, before we might error out */
418 if (!(server
->flags
& NFS_MOUNT_NONLM
))
421 server
->namelen
= data
->namlen
;
422 server
->hostname
= kmalloc(strlen(data
->hostname
) + 1, GFP_KERNEL
);
423 if (!server
->hostname
)
425 strcpy(server
->hostname
, data
->hostname
);
427 /* Check NFS protocol revision and initialize RPC op vector
428 * and file handle pool. */
430 if (server
->flags
& NFS_MOUNT_VER3
) {
431 server
->rpc_ops
= &nfs_v3_clientops
;
432 server
->caps
|= NFS_CAP_READDIRPLUS
;
434 server
->rpc_ops
= &nfs_v2_clientops
;
437 server
->rpc_ops
= &nfs_v2_clientops
;
440 /* Fill in pseudoflavor for mount version < 5 */
441 if (!(data
->flags
& NFS_MOUNT_SECFLAVOUR
))
442 data
->pseudoflavor
= RPC_AUTH_UNIX
;
443 authflavor
= data
->pseudoflavor
; /* save for sb_init() */
444 /* XXX maybe we want to add a server->pseudoflavor field */
446 /* Create RPC client handles */
447 server
->client
= nfs_create_client(server
, data
);
448 if (IS_ERR(server
->client
))
449 return PTR_ERR(server
->client
);
450 /* RFC 2623, sec 2.3.2 */
451 if (authflavor
!= RPC_AUTH_UNIX
) {
452 struct rpc_auth
*auth
;
454 server
->client_sys
= rpc_clone_client(server
->client
);
455 if (IS_ERR(server
->client_sys
))
456 return PTR_ERR(server
->client_sys
);
457 auth
= rpcauth_create(RPC_AUTH_UNIX
, server
->client_sys
);
459 return PTR_ERR(auth
);
461 atomic_inc(&server
->client
->cl_count
);
462 server
->client_sys
= server
->client
;
465 if (server
->flags
& NFS_MOUNT_VER3
) {
466 if (server
->namelen
== 0 || server
->namelen
> NFS3_MAXNAMLEN
)
467 server
->namelen
= NFS3_MAXNAMLEN
;
470 if (server
->namelen
== 0 || server
->namelen
> NFS2_MAXNAMLEN
)
471 server
->namelen
= NFS2_MAXNAMLEN
;
474 sb
->s_op
= &nfs_sops
;
475 return nfs_sb_init(sb
, authflavor
);
479 nfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
481 struct nfs_server
*server
= NFS_SB(sb
);
482 unsigned char blockbits
;
483 unsigned long blockres
;
484 struct nfs_fh
*rootfh
= NFS_FH(sb
->s_root
->d_inode
);
485 struct nfs_fattr fattr
;
486 struct nfs_fsstat res
= {
493 error
= server
->rpc_ops
->statfs(server
, rootfh
, &res
);
494 buf
->f_type
= NFS_SUPER_MAGIC
;
499 * Current versions of glibc do not correctly handle the
500 * case where f_frsize != f_bsize. Eventually we want to
501 * report the value of wtmult in this field.
503 buf
->f_frsize
= sb
->s_blocksize
;
506 * On most *nix systems, f_blocks, f_bfree, and f_bavail
507 * are reported in units of f_frsize. Linux hasn't had
508 * an f_frsize field in its statfs struct until recently,
509 * thus historically Linux's sys_statfs reports these
510 * fields in units of f_bsize.
512 buf
->f_bsize
= sb
->s_blocksize
;
513 blockbits
= sb
->s_blocksize_bits
;
514 blockres
= (1 << blockbits
) - 1;
515 buf
->f_blocks
= (res
.tbytes
+ blockres
) >> blockbits
;
516 buf
->f_bfree
= (res
.fbytes
+ blockres
) >> blockbits
;
517 buf
->f_bavail
= (res
.abytes
+ blockres
) >> blockbits
;
519 buf
->f_files
= res
.tfiles
;
520 buf
->f_ffree
= res
.afiles
;
522 buf
->f_namelen
= server
->namelen
;
529 printk(KERN_WARNING
"nfs_statfs: statfs error = %d\n", -error
);
530 buf
->f_bsize
= buf
->f_blocks
= buf
->f_bfree
= buf
->f_bavail
= -1;
535 static int nfs_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
537 static struct proc_nfs_info
{
542 { NFS_MOUNT_SOFT
, ",soft", ",hard" },
543 { NFS_MOUNT_INTR
, ",intr", "" },
544 { NFS_MOUNT_POSIX
, ",posix", "" },
545 { NFS_MOUNT_TCP
, ",tcp", ",udp" },
546 { NFS_MOUNT_NOCTO
, ",nocto", "" },
547 { NFS_MOUNT_NOAC
, ",noac", "" },
548 { NFS_MOUNT_NONLM
, ",nolock", ",lock" },
551 struct proc_nfs_info
*nfs_infop
;
552 struct nfs_server
*nfss
= NFS_SB(mnt
->mnt_sb
);
554 seq_printf(m
, ",v%d", nfss
->rpc_ops
->version
);
555 seq_printf(m
, ",rsize=%d", nfss
->rsize
);
556 seq_printf(m
, ",wsize=%d", nfss
->wsize
);
557 if (nfss
->acregmin
!= 3*HZ
)
558 seq_printf(m
, ",acregmin=%d", nfss
->acregmin
/HZ
);
559 if (nfss
->acregmax
!= 60*HZ
)
560 seq_printf(m
, ",acregmax=%d", nfss
->acregmax
/HZ
);
561 if (nfss
->acdirmin
!= 30*HZ
)
562 seq_printf(m
, ",acdirmin=%d", nfss
->acdirmin
/HZ
);
563 if (nfss
->acdirmax
!= 60*HZ
)
564 seq_printf(m
, ",acdirmax=%d", nfss
->acdirmax
/HZ
);
565 for (nfs_infop
= nfs_info
; nfs_infop
->flag
; nfs_infop
++) {
566 if (nfss
->flags
& nfs_infop
->flag
)
567 seq_puts(m
, nfs_infop
->str
);
569 seq_puts(m
, nfs_infop
->nostr
);
571 seq_puts(m
, ",addr=");
572 seq_escape(m
, nfss
->hostname
, " \t\n\\");
577 * Invalidate the local caches
580 nfs_zap_caches(struct inode
*inode
)
582 struct nfs_inode
*nfsi
= NFS_I(inode
);
583 int mode
= inode
->i_mode
;
585 NFS_ATTRTIMEO(inode
) = NFS_MINATTRTIMEO(inode
);
586 NFS_ATTRTIMEO_UPDATE(inode
) = jiffies
;
588 memset(NFS_COOKIEVERF(inode
), 0, sizeof(NFS_COOKIEVERF(inode
)));
589 if (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))
590 nfsi
->flags
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
|NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
592 nfsi
->flags
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
595 static void nfs_zap_acl_cache(struct inode
*inode
)
597 void (*clear_acl_cache
)(struct inode
*);
599 clear_acl_cache
= NFS_PROTO(inode
)->clear_acl_cache
;
600 if (clear_acl_cache
!= NULL
)
601 clear_acl_cache(inode
);
602 NFS_I(inode
)->flags
&= ~NFS_INO_INVALID_ACL
;
606 * Invalidate, but do not unhash, the inode
609 nfs_invalidate_inode(struct inode
*inode
)
611 umode_t save_mode
= inode
->i_mode
;
613 make_bad_inode(inode
);
614 inode
->i_mode
= save_mode
;
615 nfs_zap_caches(inode
);
618 struct nfs_find_desc
{
620 struct nfs_fattr
*fattr
;
624 * In NFSv3 we can have 64bit inode numbers. In order to support
625 * this, and re-exported directories (also seen in NFSv2)
626 * we are forced to allow 2 different inodes to have the same
630 nfs_find_actor(struct inode
*inode
, void *opaque
)
632 struct nfs_find_desc
*desc
= (struct nfs_find_desc
*)opaque
;
633 struct nfs_fh
*fh
= desc
->fh
;
634 struct nfs_fattr
*fattr
= desc
->fattr
;
636 if (NFS_FILEID(inode
) != fattr
->fileid
)
638 if (nfs_compare_fh(NFS_FH(inode
), fh
))
640 if (is_bad_inode(inode
) || NFS_STALE(inode
))
646 nfs_init_locked(struct inode
*inode
, void *opaque
)
648 struct nfs_find_desc
*desc
= (struct nfs_find_desc
*)opaque
;
649 struct nfs_fattr
*fattr
= desc
->fattr
;
651 NFS_FILEID(inode
) = fattr
->fileid
;
652 nfs_copy_fh(NFS_FH(inode
), desc
->fh
);
656 /* Don't use READDIRPLUS on directories that we believe are too large */
657 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
660 * This is our front-end to iget that looks up inodes by file handle
661 * instead of inode number.
664 nfs_fhget(struct super_block
*sb
, struct nfs_fh
*fh
, struct nfs_fattr
*fattr
)
666 struct nfs_find_desc desc
= {
670 struct inode
*inode
= NULL
;
673 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
677 printk("NFS: Buggy server - nlink == 0!\n");
681 hash
= nfs_fattr_to_ino_t(fattr
);
683 if (!(inode
= iget5_locked(sb
, hash
, nfs_find_actor
, nfs_init_locked
, &desc
)))
686 if (inode
->i_state
& I_NEW
) {
687 struct nfs_inode
*nfsi
= NFS_I(inode
);
689 /* We set i_ino for the few things that still rely on it,
693 /* We can't support update_atime(), since the server will reset it */
694 inode
->i_flags
|= S_NOATIME
|S_NOCMTIME
;
695 inode
->i_mode
= fattr
->mode
;
696 /* Why so? Because we want revalidate for devices/FIFOs, and
697 * that's precisely what we have in nfs_file_inode_operations.
699 inode
->i_op
= NFS_SB(sb
)->rpc_ops
->file_inode_ops
;
700 if (S_ISREG(inode
->i_mode
)) {
701 inode
->i_fop
= &nfs_file_operations
;
702 inode
->i_data
.a_ops
= &nfs_file_aops
;
703 inode
->i_data
.backing_dev_info
= &NFS_SB(sb
)->backing_dev_info
;
704 } else if (S_ISDIR(inode
->i_mode
)) {
705 inode
->i_op
= NFS_SB(sb
)->rpc_ops
->dir_inode_ops
;
706 inode
->i_fop
= &nfs_dir_operations
;
707 if (nfs_server_capable(inode
, NFS_CAP_READDIRPLUS
)
708 && fattr
->size
<= NFS_LIMIT_READDIRPLUS
)
709 NFS_FLAGS(inode
) |= NFS_INO_ADVISE_RDPLUS
;
710 } else if (S_ISLNK(inode
->i_mode
))
711 inode
->i_op
= &nfs_symlink_inode_operations
;
713 init_special_inode(inode
, inode
->i_mode
, fattr
->rdev
);
715 nfsi
->read_cache_jiffies
= fattr
->timestamp
;
716 inode
->i_atime
= fattr
->atime
;
717 inode
->i_mtime
= fattr
->mtime
;
718 inode
->i_ctime
= fattr
->ctime
;
719 if (fattr
->valid
& NFS_ATTR_FATTR_V4
)
720 nfsi
->change_attr
= fattr
->change_attr
;
721 inode
->i_size
= nfs_size_to_loff_t(fattr
->size
);
722 inode
->i_nlink
= fattr
->nlink
;
723 inode
->i_uid
= fattr
->uid
;
724 inode
->i_gid
= fattr
->gid
;
725 if (fattr
->valid
& (NFS_ATTR_FATTR_V3
| NFS_ATTR_FATTR_V4
)) {
727 * report the blocks in 512byte units
729 inode
->i_blocks
= nfs_calc_block_size(fattr
->du
.nfs3
.used
);
730 inode
->i_blksize
= inode
->i_sb
->s_blocksize
;
732 inode
->i_blocks
= fattr
->du
.nfs2
.blocks
;
733 inode
->i_blksize
= fattr
->du
.nfs2
.blocksize
;
735 nfsi
->attrtimeo
= NFS_MINATTRTIMEO(inode
);
736 nfsi
->attrtimeo_timestamp
= jiffies
;
737 memset(nfsi
->cookieverf
, 0, sizeof(nfsi
->cookieverf
));
738 nfsi
->cache_access
.cred
= NULL
;
740 unlock_new_inode(inode
);
742 nfs_refresh_inode(inode
, fattr
);
743 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
745 (long long)NFS_FILEID(inode
),
746 atomic_read(&inode
->i_count
));
752 printk("nfs_fhget: iget failed\n");
756 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
759 nfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
761 struct inode
*inode
= dentry
->d_inode
;
762 struct nfs_fattr fattr
;
765 if (attr
->ia_valid
& ATTR_SIZE
) {
766 if (!S_ISREG(inode
->i_mode
) || attr
->ia_size
== i_size_read(inode
))
767 attr
->ia_valid
&= ~ATTR_SIZE
;
770 /* Optimization: if the end result is no change, don't RPC */
771 attr
->ia_valid
&= NFS_VALID_ATTRS
;
772 if (attr
->ia_valid
== 0)
776 nfs_begin_data_update(inode
);
777 /* Write all dirty data if we're changing file permissions or size */
778 if ((attr
->ia_valid
& (ATTR_MODE
|ATTR_UID
|ATTR_GID
|ATTR_SIZE
)) != 0) {
779 if (filemap_fdatawrite(inode
->i_mapping
) == 0)
780 filemap_fdatawait(inode
->i_mapping
);
783 error
= NFS_PROTO(inode
)->setattr(dentry
, &fattr
, attr
);
785 nfs_refresh_inode(inode
, &fattr
);
786 if ((attr
->ia_valid
& ATTR_MODE
) != 0) {
788 mode
= inode
->i_mode
& ~S_IALLUGO
;
789 mode
|= attr
->ia_mode
& S_IALLUGO
;
790 inode
->i_mode
= mode
;
792 if ((attr
->ia_valid
& ATTR_UID
) != 0)
793 inode
->i_uid
= attr
->ia_uid
;
794 if ((attr
->ia_valid
& ATTR_GID
) != 0)
795 inode
->i_gid
= attr
->ia_gid
;
796 if ((attr
->ia_valid
& ATTR_SIZE
) != 0) {
797 inode
->i_size
= attr
->ia_size
;
798 vmtruncate(inode
, attr
->ia_size
);
801 if ((attr
->ia_valid
& (ATTR_MODE
|ATTR_UID
|ATTR_GID
)) != 0)
802 NFS_FLAGS(inode
) |= NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
803 nfs_end_data_update(inode
);
809 * Wait for the inode to get unlocked.
810 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
813 nfs_wait_on_inode(struct inode
*inode
, int flag
)
815 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
816 struct nfs_inode
*nfsi
= NFS_I(inode
);
819 if (!(NFS_FLAGS(inode
) & flag
))
821 atomic_inc(&inode
->i_count
);
822 error
= nfs_wait_event(clnt
, nfsi
->nfs_i_wait
,
823 !(NFS_FLAGS(inode
) & flag
));
828 int nfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
830 struct inode
*inode
= dentry
->d_inode
;
831 struct nfs_inode
*nfsi
= NFS_I(inode
);
832 int need_atime
= nfsi
->flags
& NFS_INO_INVALID_ATIME
;
835 if (__IS_FLG(inode
, MS_NOATIME
))
837 else if (__IS_FLG(inode
, MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
839 /* We may force a getattr if the user cares about atime */
841 err
= __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
843 err
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
845 generic_fillattr(inode
, stat
);
849 struct nfs_open_context
*alloc_nfs_open_context(struct dentry
*dentry
, struct rpc_cred
*cred
)
851 struct nfs_open_context
*ctx
;
853 ctx
= (struct nfs_open_context
*)kmalloc(sizeof(*ctx
), GFP_KERNEL
);
855 atomic_set(&ctx
->count
, 1);
856 ctx
->dentry
= dget(dentry
);
857 ctx
->cred
= get_rpccred(cred
);
859 ctx
->lockowner
= current
->files
;
865 struct nfs_open_context
*get_nfs_open_context(struct nfs_open_context
*ctx
)
868 atomic_inc(&ctx
->count
);
872 void put_nfs_open_context(struct nfs_open_context
*ctx
)
874 if (atomic_dec_and_test(&ctx
->count
)) {
875 if (!list_empty(&ctx
->list
)) {
876 struct inode
*inode
= ctx
->dentry
->d_inode
;
877 spin_lock(&inode
->i_lock
);
878 list_del(&ctx
->list
);
879 spin_unlock(&inode
->i_lock
);
881 if (ctx
->state
!= NULL
)
882 nfs4_close_state(ctx
->state
, ctx
->mode
);
883 if (ctx
->cred
!= NULL
)
884 put_rpccred(ctx
->cred
);
891 * Ensure that mmap has a recent RPC credential for use when writing out
894 void nfs_file_set_open_context(struct file
*filp
, struct nfs_open_context
*ctx
)
896 struct inode
*inode
= filp
->f_dentry
->d_inode
;
897 struct nfs_inode
*nfsi
= NFS_I(inode
);
899 filp
->private_data
= get_nfs_open_context(ctx
);
900 spin_lock(&inode
->i_lock
);
901 list_add(&ctx
->list
, &nfsi
->open_files
);
902 spin_unlock(&inode
->i_lock
);
905 struct nfs_open_context
*nfs_find_open_context(struct inode
*inode
, int mode
)
907 struct nfs_inode
*nfsi
= NFS_I(inode
);
908 struct nfs_open_context
*pos
, *ctx
= NULL
;
910 spin_lock(&inode
->i_lock
);
911 list_for_each_entry(pos
, &nfsi
->open_files
, list
) {
912 if ((pos
->mode
& mode
) == mode
) {
913 ctx
= get_nfs_open_context(pos
);
917 spin_unlock(&inode
->i_lock
);
921 void nfs_file_clear_open_context(struct file
*filp
)
923 struct inode
*inode
= filp
->f_dentry
->d_inode
;
924 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)filp
->private_data
;
927 filp
->private_data
= NULL
;
928 spin_lock(&inode
->i_lock
);
929 list_move_tail(&ctx
->list
, &NFS_I(inode
)->open_files
);
930 spin_unlock(&inode
->i_lock
);
931 put_nfs_open_context(ctx
);
936 * These allocate and release file read/write context information.
938 int nfs_open(struct inode
*inode
, struct file
*filp
)
940 struct nfs_open_context
*ctx
;
941 struct rpc_cred
*cred
;
943 cred
= rpcauth_lookupcred(NFS_CLIENT(inode
)->cl_auth
, 0);
945 return PTR_ERR(cred
);
946 ctx
= alloc_nfs_open_context(filp
->f_dentry
, cred
);
950 ctx
->mode
= filp
->f_mode
;
951 nfs_file_set_open_context(filp
, ctx
);
952 put_nfs_open_context(ctx
);
953 if ((filp
->f_mode
& FMODE_WRITE
) != 0)
954 nfs_begin_data_update(inode
);
958 int nfs_release(struct inode
*inode
, struct file
*filp
)
960 if ((filp
->f_mode
& FMODE_WRITE
) != 0)
961 nfs_end_data_update(inode
);
962 nfs_file_clear_open_context(filp
);
967 * This function is called whenever some part of NFS notices that
968 * the cached attributes have to be refreshed.
971 __nfs_revalidate_inode(struct nfs_server
*server
, struct inode
*inode
)
973 int status
= -ESTALE
;
974 struct nfs_fattr fattr
;
975 struct nfs_inode
*nfsi
= NFS_I(inode
);
976 unsigned long verifier
;
979 dfprintk(PAGECACHE
, "NFS: revalidating (%s/%Ld)\n",
980 inode
->i_sb
->s_id
, (long long)NFS_FILEID(inode
));
983 if (!inode
|| is_bad_inode(inode
))
985 if (NFS_STALE(inode
))
988 while (NFS_REVALIDATING(inode
)) {
989 status
= nfs_wait_on_inode(inode
, NFS_INO_REVALIDATING
);
992 if (NFS_ATTRTIMEO(inode
) == 0)
994 if (NFS_FLAGS(inode
) & (NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
|NFS_INO_INVALID_ATIME
))
996 status
= NFS_STALE(inode
) ? -ESTALE
: 0;
999 NFS_FLAGS(inode
) |= NFS_INO_REVALIDATING
;
1001 /* Protect against RPC races by saving the change attribute */
1002 verifier
= nfs_save_change_attribute(inode
);
1003 status
= NFS_PROTO(inode
)->getattr(server
, NFS_FH(inode
), &fattr
);
1005 dfprintk(PAGECACHE
, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
1007 (long long)NFS_FILEID(inode
), status
);
1008 if (status
== -ESTALE
) {
1009 nfs_zap_caches(inode
);
1010 if (!S_ISDIR(inode
->i_mode
))
1011 NFS_FLAGS(inode
) |= NFS_INO_STALE
;
1016 status
= nfs_update_inode(inode
, &fattr
, verifier
);
1018 dfprintk(PAGECACHE
, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
1020 (long long)NFS_FILEID(inode
), status
);
1023 flags
= nfsi
->flags
;
1025 * We may need to keep the attributes marked as invalid if
1026 * we raced with nfs_end_attr_update().
1028 if (verifier
== nfsi
->cache_change_attribute
)
1029 nfsi
->flags
&= ~(NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ATIME
);
1030 /* Do the page cache invalidation */
1031 if (flags
& NFS_INO_INVALID_DATA
) {
1032 if (S_ISREG(inode
->i_mode
)) {
1033 if (filemap_fdatawrite(inode
->i_mapping
) == 0)
1034 filemap_fdatawait(inode
->i_mapping
);
1037 nfsi
->flags
&= ~NFS_INO_INVALID_DATA
;
1038 invalidate_inode_pages2(inode
->i_mapping
);
1039 memset(NFS_COOKIEVERF(inode
), 0, sizeof(NFS_COOKIEVERF(inode
)));
1040 dfprintk(PAGECACHE
, "NFS: (%s/%Ld) data cache invalidated\n",
1042 (long long)NFS_FILEID(inode
));
1043 /* This ensures we revalidate dentries */
1044 nfsi
->cache_change_attribute
++;
1046 if (flags
& NFS_INO_INVALID_ACL
)
1047 nfs_zap_acl_cache(inode
);
1048 dfprintk(PAGECACHE
, "NFS: (%s/%Ld) revalidation complete\n",
1050 (long long)NFS_FILEID(inode
));
1053 NFS_FLAGS(inode
) &= ~NFS_INO_REVALIDATING
;
1054 wake_up(&nfsi
->nfs_i_wait
);
1060 int nfs_attribute_timeout(struct inode
*inode
)
1062 struct nfs_inode
*nfsi
= NFS_I(inode
);
1064 if (nfs_have_delegation(inode
, FMODE_READ
))
1066 return time_after(jiffies
, nfsi
->read_cache_jiffies
+nfsi
->attrtimeo
);
1070 * nfs_revalidate_inode - Revalidate the inode attributes
1071 * @server - pointer to nfs_server struct
1072 * @inode - pointer to inode struct
1074 * Updates inode attribute information by retrieving the data from the server.
1076 int nfs_revalidate_inode(struct nfs_server
*server
, struct inode
*inode
)
1078 if (!(NFS_FLAGS(inode
) & (NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
))
1079 && !nfs_attribute_timeout(inode
))
1080 return NFS_STALE(inode
) ? -ESTALE
: 0;
1081 return __nfs_revalidate_inode(server
, inode
);
1085 * nfs_begin_data_update
1086 * @inode - pointer to inode
1087 * Declare that a set of operations will update file data on the server
1089 void nfs_begin_data_update(struct inode
*inode
)
1091 atomic_inc(&NFS_I(inode
)->data_updates
);
1095 * nfs_end_data_update
1096 * @inode - pointer to inode
1097 * Declare end of the operations that will update file data
1098 * This will mark the inode as immediately needing revalidation
1099 * of its attribute cache.
1101 void nfs_end_data_update(struct inode
*inode
)
1103 struct nfs_inode
*nfsi
= NFS_I(inode
);
1105 if (!nfs_have_delegation(inode
, FMODE_READ
)) {
1106 /* Mark the attribute cache for revalidation */
1107 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1108 /* Directories and symlinks: invalidate page cache too */
1109 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
1110 nfsi
->flags
|= NFS_INO_INVALID_DATA
;
1112 nfsi
->cache_change_attribute
++;
1113 atomic_dec(&nfsi
->data_updates
);
1117 * nfs_end_data_update_defer
1118 * @inode - pointer to inode
1119 * Declare end of the operations that will update file data
1120 * This will defer marking the inode as needing revalidation
1121 * unless there are no other pending updates.
1123 void nfs_end_data_update_defer(struct inode
*inode
)
1125 struct nfs_inode
*nfsi
= NFS_I(inode
);
1127 if (atomic_dec_and_test(&nfsi
->data_updates
)) {
1128 /* Mark the attribute cache for revalidation */
1129 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1130 /* Directories and symlinks: invalidate page cache too */
1131 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
1132 nfsi
->flags
|= NFS_INO_INVALID_DATA
;
1133 nfsi
->cache_change_attribute
++;
1138 * nfs_refresh_inode - verify consistency of the inode attribute cache
1139 * @inode - pointer to inode
1140 * @fattr - updated attributes
1142 * Verifies the attribute cache. If we have just changed the attributes,
1143 * so that fattr carries weak cache consistency data, then it may
1144 * also update the ctime/mtime/change_attribute.
1146 int nfs_refresh_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
1148 struct nfs_inode
*nfsi
= NFS_I(inode
);
1149 loff_t cur_size
, new_isize
;
1152 /* Do we hold a delegation? */
1153 if (nfs_have_delegation(inode
, FMODE_READ
))
1156 /* Are we in the process of updating data on the server? */
1157 data_unstable
= nfs_caches_unstable(inode
);
1159 if (fattr
->valid
& NFS_ATTR_FATTR_V4
) {
1160 if ((fattr
->valid
& NFS_ATTR_PRE_CHANGE
) != 0
1161 && nfsi
->change_attr
== fattr
->pre_change_attr
)
1162 nfsi
->change_attr
= fattr
->change_attr
;
1163 if (!data_unstable
&& nfsi
->change_attr
!= fattr
->change_attr
)
1164 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1167 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
1170 /* Has the inode gone and changed behind our back? */
1171 if (nfsi
->fileid
!= fattr
->fileid
1172 || (inode
->i_mode
& S_IFMT
) != (fattr
->mode
& S_IFMT
))
1175 cur_size
= i_size_read(inode
);
1176 new_isize
= nfs_size_to_loff_t(fattr
->size
);
1178 /* If we have atomic WCC data, we may update some attributes */
1179 if ((fattr
->valid
& NFS_ATTR_WCC
) != 0) {
1180 if (timespec_equal(&inode
->i_ctime
, &fattr
->pre_ctime
))
1181 memcpy(&inode
->i_ctime
, &fattr
->ctime
, sizeof(inode
->i_ctime
));
1182 if (timespec_equal(&inode
->i_mtime
, &fattr
->pre_mtime
))
1183 memcpy(&inode
->i_mtime
, &fattr
->mtime
, sizeof(inode
->i_mtime
));
1186 /* Verify a few of the more important attributes */
1187 if (!data_unstable
) {
1188 if (!timespec_equal(&inode
->i_mtime
, &fattr
->mtime
)
1189 || cur_size
!= new_isize
)
1190 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1191 } else if (S_ISREG(inode
->i_mode
) && new_isize
> cur_size
)
1192 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1194 /* Have any file permissions changed? */
1195 if ((inode
->i_mode
& S_IALLUGO
) != (fattr
->mode
& S_IALLUGO
)
1196 || inode
->i_uid
!= fattr
->uid
1197 || inode
->i_gid
!= fattr
->gid
)
1198 nfsi
->flags
|= NFS_INO_INVALID_ATTR
| NFS_INO_INVALID_ACCESS
| NFS_INO_INVALID_ACL
;
1200 /* Has the link count changed? */
1201 if (inode
->i_nlink
!= fattr
->nlink
)
1202 nfsi
->flags
|= NFS_INO_INVALID_ATTR
;
1204 if (!timespec_equal(&inode
->i_atime
, &fattr
->atime
))
1205 nfsi
->flags
|= NFS_INO_INVALID_ATIME
;
1207 nfsi
->read_cache_jiffies
= fattr
->timestamp
;
1212 * Many nfs protocol calls return the new file attributes after
1213 * an operation. Here we update the inode to reflect the state
1214 * of the server's inode.
1216 * This is a bit tricky because we have to make sure all dirty pages
1217 * have been sent off to the server before calling invalidate_inode_pages.
1218 * To make sure no other process adds more write requests while we try
1219 * our best to flush them, we make them sleep during the attribute refresh.
1221 * A very similar scenario holds for the dir cache.
1223 static int nfs_update_inode(struct inode
*inode
, struct nfs_fattr
*fattr
, unsigned long verifier
)
1225 struct nfs_inode
*nfsi
= NFS_I(inode
);
1228 unsigned int invalid
= 0;
1232 dfprintk(VFS
, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1233 __FUNCTION__
, inode
->i_sb
->s_id
, inode
->i_ino
,
1234 atomic_read(&inode
->i_count
), fattr
->valid
);
1236 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
1239 if (nfsi
->fileid
!= fattr
->fileid
) {
1240 printk(KERN_ERR
"%s: inode number mismatch\n"
1241 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1243 inode
->i_sb
->s_id
, (long long)nfsi
->fileid
,
1244 inode
->i_sb
->s_id
, (long long)fattr
->fileid
);
1249 * Make sure the inode's type hasn't changed.
1251 if ((inode
->i_mode
& S_IFMT
) != (fattr
->mode
& S_IFMT
))
1255 * Update the read time so we don't revalidate too often.
1257 nfsi
->read_cache_jiffies
= fattr
->timestamp
;
1259 /* Are we racing with known updates of the metadata on the server? */
1260 data_unstable
= ! nfs_verify_change_attribute(inode
, verifier
);
1262 /* Check if the file size agrees */
1263 new_size
= fattr
->size
;
1264 new_isize
= nfs_size_to_loff_t(fattr
->size
);
1265 cur_isize
= i_size_read(inode
);
1266 if (cur_isize
!= new_size
) {
1267 #ifdef NFS_DEBUG_VERBOSE
1268 printk(KERN_DEBUG
"NFS: isize change on %s/%ld\n", inode
->i_sb
->s_id
, inode
->i_ino
);
1271 * If we have pending writebacks, things can get
1274 if (S_ISREG(inode
->i_mode
) && data_unstable
) {
1275 if (new_isize
> cur_isize
) {
1276 inode
->i_size
= new_isize
;
1277 invalid
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1280 inode
->i_size
= new_isize
;
1281 invalid
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1286 * Note: we don't check inode->i_mtime since pipes etc.
1287 * can change this value in VFS without requiring a
1288 * cache revalidation.
1290 if (!timespec_equal(&inode
->i_mtime
, &fattr
->mtime
)) {
1291 memcpy(&inode
->i_mtime
, &fattr
->mtime
, sizeof(inode
->i_mtime
));
1292 #ifdef NFS_DEBUG_VERBOSE
1293 printk(KERN_DEBUG
"NFS: mtime change on %s/%ld\n", inode
->i_sb
->s_id
, inode
->i_ino
);
1296 invalid
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1299 if ((fattr
->valid
& NFS_ATTR_FATTR_V4
)
1300 && nfsi
->change_attr
!= fattr
->change_attr
) {
1301 #ifdef NFS_DEBUG_VERBOSE
1302 printk(KERN_DEBUG
"NFS: change_attr change on %s/%ld\n",
1303 inode
->i_sb
->s_id
, inode
->i_ino
);
1305 nfsi
->change_attr
= fattr
->change_attr
;
1307 invalid
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
|NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
1310 /* If ctime has changed we should definitely clear access+acl caches */
1311 if (!timespec_equal(&inode
->i_ctime
, &fattr
->ctime
)) {
1313 invalid
|= NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
1314 memcpy(&inode
->i_ctime
, &fattr
->ctime
, sizeof(inode
->i_ctime
));
1316 memcpy(&inode
->i_atime
, &fattr
->atime
, sizeof(inode
->i_atime
));
1318 if ((inode
->i_mode
& S_IALLUGO
) != (fattr
->mode
& S_IALLUGO
) ||
1319 inode
->i_uid
!= fattr
->uid
||
1320 inode
->i_gid
!= fattr
->gid
)
1321 invalid
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
;
1323 inode
->i_mode
= fattr
->mode
;
1324 inode
->i_nlink
= fattr
->nlink
;
1325 inode
->i_uid
= fattr
->uid
;
1326 inode
->i_gid
= fattr
->gid
;
1328 if (fattr
->valid
& (NFS_ATTR_FATTR_V3
| NFS_ATTR_FATTR_V4
)) {
1330 * report the blocks in 512byte units
1332 inode
->i_blocks
= nfs_calc_block_size(fattr
->du
.nfs3
.used
);
1333 inode
->i_blksize
= inode
->i_sb
->s_blocksize
;
1335 inode
->i_blocks
= fattr
->du
.nfs2
.blocks
;
1336 inode
->i_blksize
= fattr
->du
.nfs2
.blocksize
;
1339 /* Update attrtimeo value if we're out of the unstable period */
1340 if (invalid
& NFS_INO_INVALID_ATTR
) {
1341 nfsi
->attrtimeo
= NFS_MINATTRTIMEO(inode
);
1342 nfsi
->attrtimeo_timestamp
= jiffies
;
1343 } else if (time_after(jiffies
, nfsi
->attrtimeo_timestamp
+nfsi
->attrtimeo
)) {
1344 if ((nfsi
->attrtimeo
<<= 1) > NFS_MAXATTRTIMEO(inode
))
1345 nfsi
->attrtimeo
= NFS_MAXATTRTIMEO(inode
);
1346 nfsi
->attrtimeo_timestamp
= jiffies
;
1348 /* Don't invalidate the data if we were to blame */
1349 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
)
1350 || S_ISLNK(inode
->i_mode
)))
1351 invalid
&= ~NFS_INO_INVALID_DATA
;
1352 if (!nfs_have_delegation(inode
, FMODE_READ
))
1353 nfsi
->flags
|= invalid
;
1358 * Big trouble! The inode has become a different object.
1361 printk(KERN_DEBUG
"%s: inode %ld mode changed, %07o to %07o\n",
1362 __FUNCTION__
, inode
->i_ino
, inode
->i_mode
, fattr
->mode
);
1365 * No need to worry about unhashing the dentry, as the
1366 * lookup validation will know that the inode is bad.
1367 * (But we fall through to invalidate the caches.)
1369 nfs_invalidate_inode(inode
);
1371 NFS_FLAGS(inode
) |= NFS_INO_STALE
;
1376 * File system information
1379 static int nfs_set_super(struct super_block
*s
, void *data
)
1381 s
->s_fs_info
= data
;
1382 return set_anon_super(s
, data
);
1385 static int nfs_compare_super(struct super_block
*sb
, void *data
)
1387 struct nfs_server
*server
= data
;
1388 struct nfs_server
*old
= NFS_SB(sb
);
1390 if (old
->addr
.sin_addr
.s_addr
!= server
->addr
.sin_addr
.s_addr
)
1392 if (old
->addr
.sin_port
!= server
->addr
.sin_port
)
1394 return !nfs_compare_fh(&old
->fh
, &server
->fh
);
1397 static struct super_block
*nfs_get_sb(struct file_system_type
*fs_type
,
1398 int flags
, const char *dev_name
, void *raw_data
)
1401 struct nfs_server
*server
= NULL
;
1402 struct super_block
*s
;
1403 struct nfs_fh
*root
;
1404 struct nfs_mount_data
*data
= raw_data
;
1406 s
= ERR_PTR(-EINVAL
);
1408 dprintk("%s: missing data argument\n", __FUNCTION__
);
1411 if (data
->version
<= 0 || data
->version
> NFS_MOUNT_VERSION
) {
1412 dprintk("%s: bad mount version\n", __FUNCTION__
);
1415 switch (data
->version
) {
1421 if (data
->flags
& NFS_MOUNT_VER3
) {
1422 dprintk("%s: mount structure version %d does not support NFSv3\n",
1427 data
->root
.size
= NFS2_FHSIZE
;
1428 memcpy(data
->root
.data
, data
->old_root
.data
, NFS2_FHSIZE
);
1430 if (data
->flags
& NFS_MOUNT_SECFLAVOUR
) {
1431 dprintk("%s: mount structure version %d does not support strong security\n",
1437 memset(data
->context
, 0, sizeof(data
->context
));
1439 #ifndef CONFIG_NFS_V3
1440 /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
1441 s
= ERR_PTR(-EPROTONOSUPPORT
);
1442 if (data
->flags
& NFS_MOUNT_VER3
) {
1443 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__
);
1446 #endif /* CONFIG_NFS_V3 */
1448 s
= ERR_PTR(-ENOMEM
);
1449 server
= kmalloc(sizeof(struct nfs_server
), GFP_KERNEL
);
1452 memset(server
, 0, sizeof(struct nfs_server
));
1453 /* Zero out the NFS state stuff */
1454 init_nfsv4_state(server
);
1455 server
->client
= server
->client_sys
= ERR_PTR(-EINVAL
);
1458 if (data
->flags
& NFS_MOUNT_VER3
)
1459 root
->size
= data
->root
.size
;
1461 root
->size
= NFS2_FHSIZE
;
1462 s
= ERR_PTR(-EINVAL
);
1463 if (root
->size
> sizeof(root
->data
)) {
1464 dprintk("%s: invalid root filehandle\n", __FUNCTION__
);
1467 memcpy(root
->data
, data
->root
.data
, root
->size
);
1469 /* We now require that the mount process passes the remote address */
1470 memcpy(&server
->addr
, &data
->addr
, sizeof(server
->addr
));
1471 if (server
->addr
.sin_addr
.s_addr
== INADDR_ANY
) {
1472 dprintk("%s: mount program didn't pass remote address!\n",
1477 /* Fire up rpciod if not yet running */
1478 s
= ERR_PTR(rpciod_up());
1480 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1481 __FUNCTION__
, PTR_ERR(s
));
1485 s
= sget(fs_type
, nfs_compare_super
, nfs_set_super
, server
);
1486 if (IS_ERR(s
) || s
->s_root
)
1487 goto out_rpciod_down
;
1491 error
= nfs_fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
1493 up_write(&s
->s_umount
);
1494 deactivate_super(s
);
1495 return ERR_PTR(error
);
1497 s
->s_flags
|= MS_ACTIVE
;
1506 static void nfs_kill_super(struct super_block
*s
)
1508 struct nfs_server
*server
= NFS_SB(s
);
1512 if (!IS_ERR(server
->client
))
1513 rpc_shutdown_client(server
->client
);
1514 if (!IS_ERR(server
->client_sys
))
1515 rpc_shutdown_client(server
->client_sys
);
1517 if (!(server
->flags
& NFS_MOUNT_NONLM
))
1518 lockd_down(); /* release rpc.lockd */
1520 rpciod_down(); /* release rpciod */
1522 if (server
->hostname
!= NULL
)
1523 kfree(server
->hostname
);
1527 static struct file_system_type nfs_fs_type
= {
1528 .owner
= THIS_MODULE
,
1530 .get_sb
= nfs_get_sb
,
1531 .kill_sb
= nfs_kill_super
,
1532 .fs_flags
= FS_ODD_RENAME
|FS_REVAL_DOT
|FS_BINARY_MOUNTDATA
,
1535 #ifdef CONFIG_NFS_V4
1537 static void nfs4_clear_inode(struct inode
*);
1540 static struct super_operations nfs4_sops
= {
1541 .alloc_inode
= nfs_alloc_inode
,
1542 .destroy_inode
= nfs_destroy_inode
,
1543 .write_inode
= nfs_write_inode
,
1544 .delete_inode
= nfs_delete_inode
,
1545 .statfs
= nfs_statfs
,
1546 .clear_inode
= nfs4_clear_inode
,
1547 .umount_begin
= nfs_umount_begin
,
1548 .show_options
= nfs_show_options
,
1552 * Clean out any remaining NFSv4 state that might be left over due
1553 * to open() calls that passed nfs_atomic_lookup, but failed to call
1556 static void nfs4_clear_inode(struct inode
*inode
)
1558 struct nfs_inode
*nfsi
= NFS_I(inode
);
1560 /* If we are holding a delegation, return it! */
1561 if (nfsi
->delegation
!= NULL
)
1562 nfs_inode_return_delegation(inode
);
1563 /* First call standard NFS clear_inode() code */
1564 nfs_clear_inode(inode
);
1565 /* Now clear out any remaining state */
1566 while (!list_empty(&nfsi
->open_states
)) {
1567 struct nfs4_state
*state
;
1569 state
= list_entry(nfsi
->open_states
.next
,
1572 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1575 (long long)NFS_FILEID(inode
),
1577 BUG_ON(atomic_read(&state
->count
) != 1);
1578 nfs4_close_state(state
, state
->state
);
1583 static int nfs4_fill_super(struct super_block
*sb
, struct nfs4_mount_data
*data
, int silent
)
1585 struct nfs_server
*server
;
1586 struct nfs4_client
*clp
= NULL
;
1587 struct rpc_xprt
*xprt
= NULL
;
1588 struct rpc_clnt
*clnt
= NULL
;
1589 struct rpc_timeout timeparms
;
1590 rpc_authflavor_t authflavour
;
1591 int proto
, err
= -EIO
;
1593 sb
->s_blocksize_bits
= 0;
1594 sb
->s_blocksize
= 0;
1595 server
= NFS_SB(sb
);
1596 if (data
->rsize
!= 0)
1597 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
1598 if (data
->wsize
!= 0)
1599 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
1600 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
1601 server
->caps
= NFS_CAP_ATOMIC_OPEN
;
1603 server
->acregmin
= data
->acregmin
*HZ
;
1604 server
->acregmax
= data
->acregmax
*HZ
;
1605 server
->acdirmin
= data
->acdirmin
*HZ
;
1606 server
->acdirmax
= data
->acdirmax
*HZ
;
1608 server
->rpc_ops
= &nfs_v4_clientops
;
1609 /* Initialize timeout values */
1611 timeparms
.to_initval
= data
->timeo
* HZ
/ 10;
1612 timeparms
.to_retries
= data
->retrans
;
1613 timeparms
.to_exponential
= 1;
1614 if (!timeparms
.to_retries
)
1615 timeparms
.to_retries
= 5;
1617 proto
= data
->proto
;
1618 /* Which IP protocol do we use? */
1621 timeparms
.to_maxval
= RPC_MAX_TCP_TIMEOUT
;
1622 if (!timeparms
.to_initval
)
1623 timeparms
.to_initval
= 600 * HZ
/ 10;
1626 timeparms
.to_maxval
= RPC_MAX_UDP_TIMEOUT
;
1627 if (!timeparms
.to_initval
)
1628 timeparms
.to_initval
= 11 * HZ
/ 10;
1634 clp
= nfs4_get_client(&server
->addr
.sin_addr
);
1636 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__
);
1640 /* Now create transport and client */
1641 authflavour
= RPC_AUTH_UNIX
;
1642 if (data
->auth_flavourlen
!= 0) {
1643 if (data
->auth_flavourlen
!= 1) {
1644 dprintk("%s: Invalid number of RPC auth flavours %d.\n",
1645 __FUNCTION__
, data
->auth_flavourlen
);
1649 if (copy_from_user(&authflavour
, data
->auth_flavours
, sizeof(authflavour
))) {
1655 down_write(&clp
->cl_sem
);
1656 if (IS_ERR(clp
->cl_rpcclient
)) {
1657 xprt
= xprt_create_proto(proto
, &server
->addr
, &timeparms
);
1659 up_write(&clp
->cl_sem
);
1660 err
= PTR_ERR(xprt
);
1661 dprintk("%s: cannot create RPC transport. Error = %d\n",
1665 clnt
= rpc_create_client(xprt
, server
->hostname
, &nfs_program
,
1666 server
->rpc_ops
->version
, authflavour
);
1668 up_write(&clp
->cl_sem
);
1669 err
= PTR_ERR(clnt
);
1670 dprintk("%s: cannot create RPC client. Error = %d\n",
1675 clnt
->cl_softrtry
= 1;
1676 clnt
->cl_chatty
= 1;
1677 clp
->cl_rpcclient
= clnt
;
1678 clp
->cl_cred
= rpcauth_lookupcred(clnt
->cl_auth
, 0);
1679 if (IS_ERR(clp
->cl_cred
)) {
1680 up_write(&clp
->cl_sem
);
1681 err
= PTR_ERR(clp
->cl_cred
);
1682 clp
->cl_cred
= NULL
;
1685 memcpy(clp
->cl_ipaddr
, server
->ip_addr
, sizeof(clp
->cl_ipaddr
));
1688 if (list_empty(&clp
->cl_superblocks
)) {
1689 err
= nfs4_init_client(clp
);
1691 up_write(&clp
->cl_sem
);
1695 list_add_tail(&server
->nfs4_siblings
, &clp
->cl_superblocks
);
1696 clnt
= rpc_clone_client(clp
->cl_rpcclient
);
1698 server
->nfs4_state
= clp
;
1699 up_write(&clp
->cl_sem
);
1703 err
= PTR_ERR(clnt
);
1704 dprintk("%s: cannot create RPC client. Error = %d\n",
1709 server
->client
= clnt
;
1711 if (server
->nfs4_state
->cl_idmap
== NULL
) {
1712 dprintk("%s: failed to create idmapper.\n", __FUNCTION__
);
1716 if (clnt
->cl_auth
->au_flavor
!= authflavour
) {
1717 struct rpc_auth
*auth
;
1719 auth
= rpcauth_create(authflavour
, clnt
);
1721 dprintk("%s: couldn't create credcache!\n", __FUNCTION__
);
1722 return PTR_ERR(auth
);
1726 sb
->s_time_gran
= 1;
1728 sb
->s_op
= &nfs4_sops
;
1729 err
= nfs_sb_init(sb
, authflavour
);
1734 nfs4_put_client(clp
);
1738 static int nfs4_compare_super(struct super_block
*sb
, void *data
)
1740 struct nfs_server
*server
= data
;
1741 struct nfs_server
*old
= NFS_SB(sb
);
1743 if (strcmp(server
->hostname
, old
->hostname
) != 0)
1745 if (strcmp(server
->mnt_path
, old
->mnt_path
) != 0)
1751 nfs_copy_user_string(char *dst
, struct nfs_string
*src
, int maxlen
)
1756 return ERR_PTR(-EINVAL
);
1757 if (src
->len
< maxlen
)
1760 p
= dst
= kmalloc(maxlen
+ 1, GFP_KERNEL
);
1762 return ERR_PTR(-ENOMEM
);
1764 if (copy_from_user(dst
, src
->data
, maxlen
)) {
1767 return ERR_PTR(-EFAULT
);
1773 static struct super_block
*nfs4_get_sb(struct file_system_type
*fs_type
,
1774 int flags
, const char *dev_name
, void *raw_data
)
1777 struct nfs_server
*server
;
1778 struct super_block
*s
;
1779 struct nfs4_mount_data
*data
= raw_data
;
1783 dprintk("%s: missing data argument\n", __FUNCTION__
);
1784 return ERR_PTR(-EINVAL
);
1786 if (data
->version
<= 0 || data
->version
> NFS4_MOUNT_VERSION
) {
1787 dprintk("%s: bad mount version\n", __FUNCTION__
);
1788 return ERR_PTR(-EINVAL
);
1791 server
= kmalloc(sizeof(struct nfs_server
), GFP_KERNEL
);
1793 return ERR_PTR(-ENOMEM
);
1794 memset(server
, 0, sizeof(struct nfs_server
));
1795 /* Zero out the NFS state stuff */
1796 init_nfsv4_state(server
);
1797 server
->client
= server
->client_sys
= ERR_PTR(-EINVAL
);
1799 p
= nfs_copy_user_string(NULL
, &data
->hostname
, 256);
1802 server
->hostname
= p
;
1804 p
= nfs_copy_user_string(NULL
, &data
->mnt_path
, 1024);
1807 server
->mnt_path
= p
;
1809 p
= nfs_copy_user_string(server
->ip_addr
, &data
->client_addr
,
1810 sizeof(server
->ip_addr
) - 1);
1814 /* We now require that the mount process passes the remote address */
1815 if (data
->host_addrlen
!= sizeof(server
->addr
)) {
1816 s
= ERR_PTR(-EINVAL
);
1819 if (copy_from_user(&server
->addr
, data
->host_addr
, sizeof(server
->addr
))) {
1820 s
= ERR_PTR(-EFAULT
);
1823 if (server
->addr
.sin_family
!= AF_INET
||
1824 server
->addr
.sin_addr
.s_addr
== INADDR_ANY
) {
1825 dprintk("%s: mount program didn't pass remote IP address!\n",
1827 s
= ERR_PTR(-EINVAL
);
1831 /* Fire up rpciod if not yet running */
1832 s
= ERR_PTR(rpciod_up());
1834 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1835 __FUNCTION__
, PTR_ERR(s
));
1839 s
= sget(fs_type
, nfs4_compare_super
, nfs_set_super
, server
);
1841 if (IS_ERR(s
) || s
->s_root
)
1846 error
= nfs4_fill_super(s
, data
, flags
& MS_VERBOSE
? 1 : 0);
1848 up_write(&s
->s_umount
);
1849 deactivate_super(s
);
1850 return ERR_PTR(error
);
1852 s
->s_flags
|= MS_ACTIVE
;
1855 s
= (struct super_block
*)p
;
1857 if (server
->mnt_path
)
1858 kfree(server
->mnt_path
);
1859 if (server
->hostname
)
1860 kfree(server
->hostname
);
1865 static void nfs4_kill_super(struct super_block
*sb
)
1867 struct nfs_server
*server
= NFS_SB(sb
);
1869 nfs_return_all_delegations(sb
);
1870 kill_anon_super(sb
);
1872 nfs4_renewd_prepare_shutdown(server
);
1874 if (server
->client
!= NULL
&& !IS_ERR(server
->client
))
1875 rpc_shutdown_client(server
->client
);
1876 rpciod_down(); /* release rpciod */
1878 destroy_nfsv4_state(server
);
1880 if (server
->hostname
!= NULL
)
1881 kfree(server
->hostname
);
1885 static struct file_system_type nfs4_fs_type
= {
1886 .owner
= THIS_MODULE
,
1888 .get_sb
= nfs4_get_sb
,
1889 .kill_sb
= nfs4_kill_super
,
1890 .fs_flags
= FS_ODD_RENAME
|FS_REVAL_DOT
|FS_BINARY_MOUNTDATA
,
1893 #define nfs4_init_once(nfsi) \
1895 INIT_LIST_HEAD(&(nfsi)->open_states); \
1896 nfsi->delegation = NULL; \
1897 nfsi->delegation_state = 0; \
1898 init_rwsem(&nfsi->rwsem); \
1900 #define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1901 #define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1903 #define nfs4_init_once(nfsi) \
1905 #define register_nfs4fs() (0)
1906 #define unregister_nfs4fs()
1909 extern int nfs_init_nfspagecache(void);
1910 extern void nfs_destroy_nfspagecache(void);
1911 extern int nfs_init_readpagecache(void);
1912 extern void nfs_destroy_readpagecache(void);
1913 extern int nfs_init_writepagecache(void);
1914 extern void nfs_destroy_writepagecache(void);
1915 #ifdef CONFIG_NFS_DIRECTIO
1916 extern int nfs_init_directcache(void);
1917 extern void nfs_destroy_directcache(void);
1920 static kmem_cache_t
* nfs_inode_cachep
;
1922 static struct inode
*nfs_alloc_inode(struct super_block
*sb
)
1924 struct nfs_inode
*nfsi
;
1925 nfsi
= (struct nfs_inode
*)kmem_cache_alloc(nfs_inode_cachep
, SLAB_KERNEL
);
1929 #ifdef CONFIG_NFS_V4
1930 nfsi
->nfs4_acl
= NULL
;
1931 #endif /* CONFIG_NFS_V4 */
1932 return &nfsi
->vfs_inode
;
1935 static void nfs_destroy_inode(struct inode
*inode
)
1937 kmem_cache_free(nfs_inode_cachep
, NFS_I(inode
));
1940 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
1942 struct nfs_inode
*nfsi
= (struct nfs_inode
*) foo
;
1944 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
1945 SLAB_CTOR_CONSTRUCTOR
) {
1946 inode_init_once(&nfsi
->vfs_inode
);
1947 spin_lock_init(&nfsi
->req_lock
);
1948 INIT_LIST_HEAD(&nfsi
->dirty
);
1949 INIT_LIST_HEAD(&nfsi
->commit
);
1950 INIT_LIST_HEAD(&nfsi
->open_files
);
1951 INIT_RADIX_TREE(&nfsi
->nfs_page_tree
, GFP_ATOMIC
);
1952 atomic_set(&nfsi
->data_updates
, 0);
1956 init_waitqueue_head(&nfsi
->nfs_i_wait
);
1957 nfs4_init_once(nfsi
);
1961 static int nfs_init_inodecache(void)
1963 nfs_inode_cachep
= kmem_cache_create("nfs_inode_cache",
1964 sizeof(struct nfs_inode
),
1965 0, SLAB_RECLAIM_ACCOUNT
,
1967 if (nfs_inode_cachep
== NULL
)
1973 static void nfs_destroy_inodecache(void)
1975 if (kmem_cache_destroy(nfs_inode_cachep
))
1976 printk(KERN_INFO
"nfs_inode_cache: not all structures were freed\n");
1982 static int __init
init_nfs_fs(void)
1986 err
= nfs_init_nfspagecache();
1990 err
= nfs_init_inodecache();
1994 err
= nfs_init_readpagecache();
1998 err
= nfs_init_writepagecache();
2002 #ifdef CONFIG_NFS_DIRECTIO
2003 err
= nfs_init_directcache();
2008 #ifdef CONFIG_PROC_FS
2009 rpc_proc_register(&nfs_rpcstat
);
2011 err
= register_filesystem(&nfs_fs_type
);
2014 if ((err
= register_nfs4fs()) != 0)
2018 #ifdef CONFIG_PROC_FS
2019 rpc_proc_unregister("nfs");
2021 nfs_destroy_writepagecache();
2022 #ifdef CONFIG_NFS_DIRECTIO
2024 nfs_destroy_directcache();
2027 nfs_destroy_readpagecache();
2029 nfs_destroy_inodecache();
2031 nfs_destroy_nfspagecache();
2036 static void __exit
exit_nfs_fs(void)
2038 #ifdef CONFIG_NFS_DIRECTIO
2039 nfs_destroy_directcache();
2041 nfs_destroy_writepagecache();
2042 nfs_destroy_readpagecache();
2043 nfs_destroy_inodecache();
2044 nfs_destroy_nfspagecache();
2045 #ifdef CONFIG_PROC_FS
2046 rpc_proc_unregister("nfs");
2048 unregister_filesystem(&nfs_fs_type
);
2049 unregister_nfs4fs();
2052 /* Not quite true; I just maintain it */
2053 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2054 MODULE_LICENSE("GPL");
2056 module_init(init_nfs_fs
)
2057 module_exit(exit_nfs_fs
)