[PATCH] NFSD: Add server support for NFSv3 ACLs.
[linux-2.6-block.git] / fs / nfs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/inode.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * nfs inode and superblock handling functions
7 *
8 * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
10 *
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
13 *
14 */
15
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/init.h>
19
20#include <linux/time.h>
21#include <linux/kernel.h>
22#include <linux/mm.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>
38
39#include <asm/system.h>
40#include <asm/uaccess.h>
41
4ce79717 42#include "nfs4_fs.h"
1da177e4
LT
43#include "delegation.h"
44
45#define NFSDBG_FACILITY NFSDBG_VFS
46#define NFS_PARANOIA 1
47
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.
53 */
54#define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
55
56static void nfs_invalidate_inode(struct inode *);
57static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
58
59static struct inode *nfs_alloc_inode(struct super_block *sb);
60static void nfs_destroy_inode(struct inode *);
61static int nfs_write_inode(struct inode *,int);
62static void nfs_delete_inode(struct inode *);
63static void nfs_clear_inode(struct inode *);
64static void nfs_umount_begin(struct super_block *);
65static int nfs_statfs(struct super_block *, struct kstatfs *);
66static int nfs_show_options(struct seq_file *, struct vfsmount *);
ada70d94 67static void nfs_zap_acl_cache(struct inode *);
1da177e4
LT
68
69static struct rpc_program nfs_program;
70
71static 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,
76 .statfs = nfs_statfs,
77 .clear_inode = nfs_clear_inode,
78 .umount_begin = nfs_umount_begin,
79 .show_options = nfs_show_options,
80};
81
82/*
83 * RPC cruft for NFS
84 */
85static struct rpc_stat nfs_rpcstat = {
86 .program = &nfs_program
87};
88static struct rpc_version * nfs_version[] = {
89 NULL,
90 NULL,
91 &nfs_version2,
92#if defined(CONFIG_NFS_V3)
93 &nfs_version3,
94#elif defined(CONFIG_NFS_V4)
95 NULL,
96#endif
97#if defined(CONFIG_NFS_V4)
98 &nfs_version4,
99#endif
100};
101
102static struct rpc_program nfs_program = {
103 .name = "nfs",
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",
109};
110
111static inline unsigned long
112nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
113{
114 return nfs_fileid_to_ino_t(fattr->fileid);
115}
116
117static int
118nfs_write_inode(struct inode *inode, int sync)
119{
120 int flags = sync ? FLUSH_WAIT : 0;
121 int ret;
122
123 ret = nfs_commit_inode(inode, 0, 0, flags);
124 if (ret < 0)
125 return ret;
126 return 0;
127}
128
129static void
130nfs_delete_inode(struct inode * inode)
131{
132 dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
133
134 nfs_wb_all(inode);
135 /*
136 * The following should never happen...
137 */
138 if (nfs_have_writebacks(inode)) {
139 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
140 }
141
142 clear_inode(inode);
143}
144
1da177e4
LT
145static void
146nfs_clear_inode(struct inode *inode)
147{
148 struct nfs_inode *nfsi = NFS_I(inode);
149 struct rpc_cred *cred;
150
151 nfs_wb_all(inode);
152 BUG_ON (!list_empty(&nfsi->open_files));
ada70d94 153 nfs_zap_acl_cache(inode);
1da177e4
LT
154 cred = nfsi->cache_access.cred;
155 if (cred)
156 put_rpccred(cred);
157 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
158}
159
160void
161nfs_umount_begin(struct super_block *sb)
162{
6a19275a 163 struct rpc_clnt *rpc = NFS_SB(sb)->client;
1da177e4
LT
164
165 /* -EIO all pending I/O */
6a19275a 166 if (!IS_ERR(rpc))
1da177e4
LT
167 rpc_killall_tasks(rpc);
168}
169
170
171static inline unsigned long
172nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
173{
174 /* make sure blocksize is a power of two */
175 if ((bsize & (bsize - 1)) || nrbitsp) {
176 unsigned char nrbits;
177
178 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
179 ;
180 bsize = 1 << nrbits;
181 if (nrbitsp)
182 *nrbitsp = nrbits;
183 }
184
185 return bsize;
186}
187
188/*
189 * Calculate the number of 512byte blocks used.
190 */
191static inline unsigned long
192nfs_calc_block_size(u64 tsize)
193{
194 loff_t used = (tsize + 511) >> 9;
195 return (used > ULONG_MAX) ? ULONG_MAX : used;
196}
197
198/*
199 * Compute and set NFS server blocksize
200 */
201static inline unsigned long
202nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
203{
204 if (bsize < 1024)
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;
208
209 return nfs_block_bits(bsize, nrbitsp);
210}
211
212/*
213 * Obtain the root inode of the file system.
214 */
215static struct inode *
216nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
217{
218 struct nfs_server *server = NFS_SB(sb);
219 struct inode *rooti;
220 int error;
221
222 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
223 if (error < 0) {
224 dprintk("nfs_get_root: getattr error = %d\n", -error);
225 return ERR_PTR(error);
226 }
227
228 rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
229 if (!rooti)
230 return ERR_PTR(-ENOMEM);
231 return rooti;
232}
233
234/*
235 * Do NFS version-independent mount processing, and sanity checking
236 */
237static int
238nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
239{
240 struct nfs_server *server;
241 struct inode *root_inode;
242 struct nfs_fattr fattr;
243 struct nfs_fsinfo fsinfo = {
244 .fattr = &fattr,
245 };
246 struct nfs_pathconf pathinfo = {
247 .fattr = &fattr,
248 };
249 int no_root_error = 0;
250 unsigned long max_rpc_payload;
251
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));
254
255 server = NFS_SB(sb);
256
257 sb->s_magic = NFS_SUPER_MAGIC;
258
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);
263 goto out_no_root;
264 }
265 sb->s_root = d_alloc_root(root_inode);
266 if (!sb->s_root) {
267 no_root_error = -ENOMEM;
268 goto out_no_root;
269 }
270 sb->s_root->d_op = server->rpc_ops->dentry_ops;
271
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);
281
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);
286
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;
292
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;
297 }
298
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;
303 }
304
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);
309
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;
315
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;
320 }
321 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
322
323 sb->s_maxbytes = fsinfo.maxfilesize;
324 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
325 sb->s_maxbytes = MAX_LFS_FILESIZE;
326
327 server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
328 server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
329
330 /* We're airborne Set socket buffersize */
331 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
332 return 0;
333 /* Yargs. It didn't work out. */
334out_no_root:
335 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
336 if (!IS_ERR(root_inode))
337 iput(root_inode);
338 return no_root_error;
339}
340
341/*
342 * Create an RPC client handle.
343 */
344static struct rpc_clnt *
345nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
346{
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);
351
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;
357
358 if (!timeparms.to_initval)
359 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
360 if (!timeparms.to_retries)
361 timeparms.to_retries = 5;
362
363 /* create transport and client */
364 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
365 &server->addr, &timeparms);
366 if (IS_ERR(xprt)) {
9085bbcb
TM
367 dprintk("%s: cannot create RPC transport. Error = %ld\n",
368 __FUNCTION__, PTR_ERR(xprt));
1da177e4
LT
369 return (struct rpc_clnt *)xprt;
370 }
371 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
372 server->rpc_ops->version, data->pseudoflavor);
373 if (IS_ERR(clnt)) {
9085bbcb
TM
374 dprintk("%s: cannot create RPC client. Error = %ld\n",
375 __FUNCTION__, PTR_ERR(xprt));
1da177e4
LT
376 goto out_fail;
377 }
378
379 clnt->cl_intr = 1;
380 clnt->cl_softrtry = 1;
381 clnt->cl_chatty = 1;
382
383 return clnt;
384
385out_fail:
1da177e4
LT
386 return clnt;
387}
388
389/*
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.
394 */
395static int
396nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
397{
398 struct nfs_server *server;
399 rpc_authflavor_t authflavor;
400
401 server = NFS_SB(sb);
402 sb->s_blocksize_bits = 0;
403 sb->s_blocksize = 0;
404 if (data->bsize)
405 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
406 if (data->rsize)
407 server->rsize = nfs_block_size(data->rsize, NULL);
408 if (data->wsize)
409 server->wsize = nfs_block_size(data->wsize, NULL);
410 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
411
412 server->acregmin = data->acregmin*HZ;
413 server->acregmax = data->acregmax*HZ;
414 server->acdirmin = data->acdirmin*HZ;
415 server->acdirmax = data->acdirmax*HZ;
416
417 /* Start lockd here, before we might error out */
418 if (!(server->flags & NFS_MOUNT_NONLM))
419 lockd_up();
420
421 server->namelen = data->namlen;
422 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
423 if (!server->hostname)
424 return -ENOMEM;
425 strcpy(server->hostname, data->hostname);
426
427 /* Check NFS protocol revision and initialize RPC op vector
428 * and file handle pool. */
1da177e4 429#ifdef CONFIG_NFS_V3
9085bbcb 430 if (server->flags & NFS_MOUNT_VER3) {
1da177e4
LT
431 server->rpc_ops = &nfs_v3_clientops;
432 server->caps |= NFS_CAP_READDIRPLUS;
1da177e4
LT
433 } else {
434 server->rpc_ops = &nfs_v2_clientops;
435 }
9085bbcb
TM
436#else
437 server->rpc_ops = &nfs_v2_clientops;
438#endif
1da177e4
LT
439
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 */
445
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) {
6a19275a
BF
452 struct rpc_auth *auth;
453
1da177e4
LT
454 server->client_sys = rpc_clone_client(server->client);
455 if (IS_ERR(server->client_sys))
456 return PTR_ERR(server->client_sys);
6a19275a
BF
457 auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
458 if (IS_ERR(auth))
459 return PTR_ERR(auth);
1da177e4
LT
460 } else {
461 atomic_inc(&server->client->cl_count);
462 server->client_sys = server->client;
463 }
464
465 if (server->flags & NFS_MOUNT_VER3) {
466 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
467 server->namelen = NFS3_MAXNAMLEN;
468 sb->s_time_gran = 1;
469 } else {
470 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
471 server->namelen = NFS2_MAXNAMLEN;
472 }
473
474 sb->s_op = &nfs_sops;
475 return nfs_sb_init(sb, authflavor);
476}
477
478static int
479nfs_statfs(struct super_block *sb, struct kstatfs *buf)
480{
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 = {
487 .fattr = &fattr,
488 };
489 int error;
490
491 lock_kernel();
492
493 error = server->rpc_ops->statfs(server, rootfh, &res);
494 buf->f_type = NFS_SUPER_MAGIC;
495 if (error < 0)
496 goto out_err;
497
498 /*
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.
502 */
503 buf->f_frsize = sb->s_blocksize;
504
505 /*
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.
511 */
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;
518
519 buf->f_files = res.tfiles;
520 buf->f_ffree = res.afiles;
521
522 buf->f_namelen = server->namelen;
523 out:
524 unlock_kernel();
525
526 return 0;
527
528 out_err:
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;
531 goto out;
532
533}
534
535static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
536{
537 static struct proc_nfs_info {
538 int flag;
539 char *str;
540 char *nostr;
541 } 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" },
549 { 0, NULL, NULL }
550 };
551 struct proc_nfs_info *nfs_infop;
552 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
553
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);
568 else
569 seq_puts(m, nfs_infop->nostr);
570 }
571 seq_puts(m, ",addr=");
572 seq_escape(m, nfss->hostname, " \t\n\\");
573 return 0;
574}
575
576/*
577 * Invalidate the local caches
578 */
579void
580nfs_zap_caches(struct inode *inode)
581{
582 struct nfs_inode *nfsi = NFS_I(inode);
583 int mode = inode->i_mode;
584
585 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
586 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
587
588 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
589 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
ada70d94 590 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1da177e4 591 else
ada70d94
TM
592 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
593}
594
595static void nfs_zap_acl_cache(struct inode *inode)
596{
597 void (*clear_acl_cache)(struct inode *);
598
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;
1da177e4
LT
603}
604
605/*
606 * Invalidate, but do not unhash, the inode
607 */
608static void
609nfs_invalidate_inode(struct inode *inode)
610{
611 umode_t save_mode = inode->i_mode;
612
613 make_bad_inode(inode);
614 inode->i_mode = save_mode;
615 nfs_zap_caches(inode);
616}
617
618struct nfs_find_desc {
619 struct nfs_fh *fh;
620 struct nfs_fattr *fattr;
621};
622
623/*
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
627 * i_ino.
628 */
629static int
630nfs_find_actor(struct inode *inode, void *opaque)
631{
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;
635
636 if (NFS_FILEID(inode) != fattr->fileid)
637 return 0;
638 if (nfs_compare_fh(NFS_FH(inode), fh))
639 return 0;
640 if (is_bad_inode(inode) || NFS_STALE(inode))
641 return 0;
642 return 1;
643}
644
645static int
646nfs_init_locked(struct inode *inode, void *opaque)
647{
648 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
649 struct nfs_fattr *fattr = desc->fattr;
650
651 NFS_FILEID(inode) = fattr->fileid;
652 nfs_copy_fh(NFS_FH(inode), desc->fh);
653 return 0;
654}
655
656/* Don't use READDIRPLUS on directories that we believe are too large */
657#define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
658
659/*
660 * This is our front-end to iget that looks up inodes by file handle
661 * instead of inode number.
662 */
663struct inode *
664nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
665{
666 struct nfs_find_desc desc = {
667 .fh = fh,
668 .fattr = fattr
669 };
670 struct inode *inode = NULL;
671 unsigned long hash;
672
673 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
674 goto out_no_inode;
675
676 if (!fattr->nlink) {
677 printk("NFS: Buggy server - nlink == 0!\n");
678 goto out_no_inode;
679 }
680
681 hash = nfs_fattr_to_ino_t(fattr);
682
683 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
684 goto out_no_inode;
685
686 if (inode->i_state & I_NEW) {
687 struct nfs_inode *nfsi = NFS_I(inode);
688
689 /* We set i_ino for the few things that still rely on it,
690 * such as stat(2) */
691 inode->i_ino = hash;
692
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.
698 */
92cfc62c 699 inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
1da177e4
LT
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;
712 else
713 init_special_inode(inode, inode->i_mode, fattr->rdev);
714
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)) {
726 /*
727 * report the blocks in 512byte units
728 */
729 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
730 inode->i_blksize = inode->i_sb->s_blocksize;
731 } else {
732 inode->i_blocks = fattr->du.nfs2.blocks;
733 inode->i_blksize = fattr->du.nfs2.blocksize;
734 }
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;
739
740 unlock_new_inode(inode);
741 } else
742 nfs_refresh_inode(inode, fattr);
743 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
744 inode->i_sb->s_id,
745 (long long)NFS_FILEID(inode),
746 atomic_read(&inode->i_count));
747
748out:
749 return inode;
750
751out_no_inode:
752 printk("nfs_fhget: iget failed\n");
753 goto out;
754}
755
756#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
757
758int
759nfs_setattr(struct dentry *dentry, struct iattr *attr)
760{
761 struct inode *inode = dentry->d_inode;
762 struct nfs_fattr fattr;
763 int error;
764
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;
768 }
769
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)
773 return 0;
774
775 lock_kernel();
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);
781 nfs_wb_all(inode);
782 }
783 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
784 if (error == 0) {
785 nfs_refresh_inode(inode, &fattr);
786 if ((attr->ia_valid & ATTR_MODE) != 0) {
787 int mode;
788 mode = inode->i_mode & ~S_IALLUGO;
789 mode |= attr->ia_mode & S_IALLUGO;
790 inode->i_mode = mode;
791 }
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);
799 }
800 }
801 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
ada70d94 802 NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1da177e4
LT
803 nfs_end_data_update(inode);
804 unlock_kernel();
805 return error;
806}
807
808/*
809 * Wait for the inode to get unlocked.
810 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
811 */
812static int
813nfs_wait_on_inode(struct inode *inode, int flag)
814{
815 struct rpc_clnt *clnt = NFS_CLIENT(inode);
816 struct nfs_inode *nfsi = NFS_I(inode);
817
818 int error;
819 if (!(NFS_FLAGS(inode) & flag))
820 return 0;
821 atomic_inc(&inode->i_count);
822 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
823 !(NFS_FLAGS(inode) & flag));
824 iput(inode);
825 return error;
826}
827
828int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
829{
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;
833 int err;
834
835 if (__IS_FLG(inode, MS_NOATIME))
836 need_atime = 0;
837 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
838 need_atime = 0;
839 /* We may force a getattr if the user cares about atime */
840 if (need_atime)
841 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
842 else
843 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
844 if (!err)
845 generic_fillattr(inode, stat);
846 return err;
847}
848
849struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, struct rpc_cred *cred)
850{
851 struct nfs_open_context *ctx;
852
853 ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
854 if (ctx != NULL) {
855 atomic_set(&ctx->count, 1);
856 ctx->dentry = dget(dentry);
857 ctx->cred = get_rpccred(cred);
858 ctx->state = NULL;
859 ctx->lockowner = current->files;
860 ctx->error = 0;
1da177e4
LT
861 }
862 return ctx;
863}
864
865struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
866{
867 if (ctx != NULL)
868 atomic_inc(&ctx->count);
869 return ctx;
870}
871
872void put_nfs_open_context(struct nfs_open_context *ctx)
873{
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);
880 }
881 if (ctx->state != NULL)
882 nfs4_close_state(ctx->state, ctx->mode);
883 if (ctx->cred != NULL)
884 put_rpccred(ctx->cred);
885 dput(ctx->dentry);
886 kfree(ctx);
887 }
888}
889
890/*
891 * Ensure that mmap has a recent RPC credential for use when writing out
892 * shared pages
893 */
894void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
895{
896 struct inode *inode = filp->f_dentry->d_inode;
897 struct nfs_inode *nfsi = NFS_I(inode);
898
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);
903}
904
905struct nfs_open_context *nfs_find_open_context(struct inode *inode, int mode)
906{
907 struct nfs_inode *nfsi = NFS_I(inode);
908 struct nfs_open_context *pos, *ctx = NULL;
909
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);
914 break;
915 }
916 }
917 spin_unlock(&inode->i_lock);
918 return ctx;
919}
920
921void nfs_file_clear_open_context(struct file *filp)
922{
923 struct inode *inode = filp->f_dentry->d_inode;
924 struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
925
926 if (ctx) {
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);
932 }
933}
934
935/*
936 * These allocate and release file read/write context information.
937 */
938int nfs_open(struct inode *inode, struct file *filp)
939{
940 struct nfs_open_context *ctx;
941 struct rpc_cred *cred;
942
943 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
944 if (IS_ERR(cred))
945 return PTR_ERR(cred);
946 ctx = alloc_nfs_open_context(filp->f_dentry, cred);
947 put_rpccred(cred);
948 if (ctx == NULL)
949 return -ENOMEM;
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);
955 return 0;
956}
957
958int nfs_release(struct inode *inode, struct file *filp)
959{
960 if ((filp->f_mode & FMODE_WRITE) != 0)
961 nfs_end_data_update(inode);
962 nfs_file_clear_open_context(filp);
963 return 0;
964}
965
966/*
967 * This function is called whenever some part of NFS notices that
968 * the cached attributes have to be refreshed.
969 */
970int
971__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
972{
973 int status = -ESTALE;
974 struct nfs_fattr fattr;
975 struct nfs_inode *nfsi = NFS_I(inode);
976 unsigned long verifier;
977 unsigned int flags;
978
979 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
980 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
981
982 lock_kernel();
983 if (!inode || is_bad_inode(inode))
984 goto out_nowait;
985 if (NFS_STALE(inode))
986 goto out_nowait;
987
988 while (NFS_REVALIDATING(inode)) {
989 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
990 if (status < 0)
991 goto out_nowait;
992 if (NFS_ATTRTIMEO(inode) == 0)
993 continue;
994 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
995 continue;
996 status = NFS_STALE(inode) ? -ESTALE : 0;
997 goto out_nowait;
998 }
999 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
1000
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);
1004 if (status != 0) {
1005 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
1006 inode->i_sb->s_id,
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;
1012 }
1013 goto out;
1014 }
1015
1016 status = nfs_update_inode(inode, &fattr, verifier);
1017 if (status) {
1018 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
1019 inode->i_sb->s_id,
1020 (long long)NFS_FILEID(inode), status);
1021 goto out;
1022 }
1023 flags = nfsi->flags;
1024 /*
1025 * We may need to keep the attributes marked as invalid if
1026 * we raced with nfs_end_attr_update().
1027 */
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);
1035 nfs_wb_all(inode);
1036 }
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",
1041 inode->i_sb->s_id,
1042 (long long)NFS_FILEID(inode));
1043 /* This ensures we revalidate dentries */
1044 nfsi->cache_change_attribute++;
1045 }
ada70d94
TM
1046 if (flags & NFS_INO_INVALID_ACL)
1047 nfs_zap_acl_cache(inode);
1da177e4
LT
1048 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1049 inode->i_sb->s_id,
1050 (long long)NFS_FILEID(inode));
1051
1052out:
1053 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1054 wake_up(&nfsi->nfs_i_wait);
1055 out_nowait:
1056 unlock_kernel();
1057 return status;
1058}
1059
1060int nfs_attribute_timeout(struct inode *inode)
1061{
1062 struct nfs_inode *nfsi = NFS_I(inode);
1063
1064 if (nfs_have_delegation(inode, FMODE_READ))
1065 return 0;
1066 return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
1067}
1068
1069/**
1070 * nfs_revalidate_inode - Revalidate the inode attributes
1071 * @server - pointer to nfs_server struct
1072 * @inode - pointer to inode struct
1073 *
1074 * Updates inode attribute information by retrieving the data from the server.
1075 */
1076int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1077{
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);
1082}
1083
1084/**
1085 * nfs_begin_data_update
1086 * @inode - pointer to inode
1087 * Declare that a set of operations will update file data on the server
1088 */
1089void nfs_begin_data_update(struct inode *inode)
1090{
1091 atomic_inc(&NFS_I(inode)->data_updates);
1092}
1093
1094/**
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.
1100 */
1101void nfs_end_data_update(struct inode *inode)
1102{
1103 struct nfs_inode *nfsi = NFS_I(inode);
1104
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;
1111 }
1112 nfsi->cache_change_attribute ++;
1113 atomic_dec(&nfsi->data_updates);
1114}
1115
1116/**
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.
1122 */
1123void nfs_end_data_update_defer(struct inode *inode)
1124{
1125 struct nfs_inode *nfsi = NFS_I(inode);
1126
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 ++;
1134 }
1135}
1136
1137/**
1138 * nfs_refresh_inode - verify consistency of the inode attribute cache
1139 * @inode - pointer to inode
1140 * @fattr - updated attributes
1141 *
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.
1145 */
1146int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1147{
1148 struct nfs_inode *nfsi = NFS_I(inode);
1149 loff_t cur_size, new_isize;
1150 int data_unstable;
1151
1152 /* Do we hold a delegation? */
1153 if (nfs_have_delegation(inode, FMODE_READ))
1154 return 0;
1155
1156 /* Are we in the process of updating data on the server? */
1157 data_unstable = nfs_caches_unstable(inode);
1158
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;
1165 }
1166
1167 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1168 return 0;
1169
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))
1173 return -EIO;
1174
1175 cur_size = i_size_read(inode);
1176 new_isize = nfs_size_to_loff_t(fattr->size);
1177
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));
1184 }
1185
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;
1193
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)
ada70d94 1198 nfsi->flags |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
1da177e4
LT
1199
1200 /* Has the link count changed? */
1201 if (inode->i_nlink != fattr->nlink)
1202 nfsi->flags |= NFS_INO_INVALID_ATTR;
1203
1204 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1205 nfsi->flags |= NFS_INO_INVALID_ATIME;
1206
1207 nfsi->read_cache_jiffies = fattr->timestamp;
1208 return 0;
1209}
1210
1211/*
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.
1215 *
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.
1220 *
1221 * A very similar scenario holds for the dir cache.
1222 */
1223static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1224{
1225 struct nfs_inode *nfsi = NFS_I(inode);
1226 __u64 new_size;
1227 loff_t new_isize;
1228 unsigned int invalid = 0;
1229 loff_t cur_isize;
1230 int data_unstable;
1231
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);
1235
1236 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1237 return 0;
1238
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",
1242 __FUNCTION__,
1243 inode->i_sb->s_id, (long long)nfsi->fileid,
1244 inode->i_sb->s_id, (long long)fattr->fileid);
1245 goto out_err;
1246 }
1247
1248 /*
1249 * Make sure the inode's type hasn't changed.
1250 */
1251 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1252 goto out_changed;
1253
1254 /*
1255 * Update the read time so we don't revalidate too often.
1256 */
1257 nfsi->read_cache_jiffies = fattr->timestamp;
1258
1259 /* Are we racing with known updates of the metadata on the server? */
1260 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1261
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);
1269#endif
1270 /*
1271 * If we have pending writebacks, things can get
1272 * messy.
1273 */
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;
1278 }
1279 } else {
1280 inode->i_size = new_isize;
1281 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1282 }
1283 }
1284
1285 /*
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.
1289 */
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);
1294#endif
1295 if (!data_unstable)
1296 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1297 }
1298
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);
1304#endif
1305 nfsi->change_attr = fattr->change_attr;
1306 if (!data_unstable)
ada70d94 1307 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1da177e4
LT
1308 }
1309
ada70d94
TM
1310 /* If ctime has changed we should definitely clear access+acl caches */
1311 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
1312 if (!data_unstable)
1313 invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1314 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1315 }
1da177e4
LT
1316 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1317
1318 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1319 inode->i_uid != fattr->uid ||
1320 inode->i_gid != fattr->gid)
ada70d94 1321 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1da177e4
LT
1322
1323 inode->i_mode = fattr->mode;
1324 inode->i_nlink = fattr->nlink;
1325 inode->i_uid = fattr->uid;
1326 inode->i_gid = fattr->gid;
1327
1328 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1329 /*
1330 * report the blocks in 512byte units
1331 */
1332 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1333 inode->i_blksize = inode->i_sb->s_blocksize;
1334 } else {
1335 inode->i_blocks = fattr->du.nfs2.blocks;
1336 inode->i_blksize = fattr->du.nfs2.blocksize;
1337 }
1338
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;
1347 }
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;
1354
1355 return 0;
1356 out_changed:
1357 /*
1358 * Big trouble! The inode has become a different object.
1359 */
1360#ifdef NFS_PARANOIA
1361 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1362 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1363#endif
1364 /*
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.)
1368 */
1369 nfs_invalidate_inode(inode);
1370 out_err:
1371 NFS_FLAGS(inode) |= NFS_INO_STALE;
1372 return -ESTALE;
1373}
1374
1375/*
1376 * File system information
1377 */
1378
1379static int nfs_set_super(struct super_block *s, void *data)
1380{
1381 s->s_fs_info = data;
1382 return set_anon_super(s, data);
1383}
1384
1385static int nfs_compare_super(struct super_block *sb, void *data)
1386{
1387 struct nfs_server *server = data;
1388 struct nfs_server *old = NFS_SB(sb);
1389
1390 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1391 return 0;
1392 if (old->addr.sin_port != server->addr.sin_port)
1393 return 0;
1394 return !nfs_compare_fh(&old->fh, &server->fh);
1395}
1396
1397static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1398 int flags, const char *dev_name, void *raw_data)
1399{
1400 int error;
9085bbcb 1401 struct nfs_server *server = NULL;
1da177e4
LT
1402 struct super_block *s;
1403 struct nfs_fh *root;
1404 struct nfs_mount_data *data = raw_data;
1405
9085bbcb
TM
1406 s = ERR_PTR(-EINVAL);
1407 if (data == NULL) {
1408 dprintk("%s: missing data argument\n", __FUNCTION__);
1409 goto out_err;
1410 }
1411 if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
1412 dprintk("%s: bad mount version\n", __FUNCTION__);
1413 goto out_err;
1da177e4 1414 }
9085bbcb
TM
1415 switch (data->version) {
1416 case 1:
1417 data->namlen = 0;
1418 case 2:
1419 data->bsize = 0;
1420 case 3:
1421 if (data->flags & NFS_MOUNT_VER3) {
1422 dprintk("%s: mount structure version %d does not support NFSv3\n",
1423 __FUNCTION__,
1424 data->version);
1425 goto out_err;
1426 }
1427 data->root.size = NFS2_FHSIZE;
1428 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1429 case 4:
1430 if (data->flags & NFS_MOUNT_SECFLAVOUR) {
1431 dprintk("%s: mount structure version %d does not support strong security\n",
1432 __FUNCTION__,
1433 data->version);
1434 goto out_err;
1435 }
1436 case 5:
1437 memset(data->context, 0, sizeof(data->context));
1438 }
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__);
1444 goto out_err;
1445 }
1446#endif /* CONFIG_NFS_V3 */
1da177e4 1447
9085bbcb 1448 s = ERR_PTR(-ENOMEM);
1da177e4
LT
1449 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1450 if (!server)
9085bbcb 1451 goto out_err;
1da177e4
LT
1452 memset(server, 0, sizeof(struct nfs_server));
1453 /* Zero out the NFS state stuff */
1454 init_nfsv4_state(server);
6a19275a 1455 server->client = server->client_sys = ERR_PTR(-EINVAL);
1da177e4 1456
1da177e4
LT
1457 root = &server->fh;
1458 if (data->flags & NFS_MOUNT_VER3)
1459 root->size = data->root.size;
1460 else
1461 root->size = NFS2_FHSIZE;
9085bbcb 1462 s = ERR_PTR(-EINVAL);
1da177e4 1463 if (root->size > sizeof(root->data)) {
9085bbcb
TM
1464 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
1465 goto out_err;
1da177e4
LT
1466 }
1467 memcpy(root->data, data->root.data, root->size);
1468
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) {
9085bbcb
TM
1472 dprintk("%s: mount program didn't pass remote address!\n",
1473 __FUNCTION__);
1474 goto out_err;
1da177e4
LT
1475 }
1476
9085bbcb
TM
1477 /* Fire up rpciod if not yet running */
1478 s = ERR_PTR(rpciod_up());
1479 if (IS_ERR(s)) {
1480 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1481 __FUNCTION__, PTR_ERR(s));
1482 goto out_err;
1da177e4
LT
1483 }
1484
9085bbcb
TM
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;
1da177e4 1488
9085bbcb 1489 s->s_flags = flags;
1da177e4
LT
1490
1491 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1492 if (error) {
1493 up_write(&s->s_umount);
1494 deactivate_super(s);
1495 return ERR_PTR(error);
1496 }
1497 s->s_flags |= MS_ACTIVE;
1498 return s;
9085bbcb
TM
1499out_rpciod_down:
1500 rpciod_down();
1501out_err:
1502 kfree(server);
1503 return s;
1da177e4
LT
1504}
1505
1506static void nfs_kill_super(struct super_block *s)
1507{
1508 struct nfs_server *server = NFS_SB(s);
1509
1510 kill_anon_super(s);
1511
6a19275a 1512 if (!IS_ERR(server->client))
1da177e4 1513 rpc_shutdown_client(server->client);
6a19275a 1514 if (!IS_ERR(server->client_sys))
1da177e4
LT
1515 rpc_shutdown_client(server->client_sys);
1516
1517 if (!(server->flags & NFS_MOUNT_NONLM))
1518 lockd_down(); /* release rpc.lockd */
1519
1520 rpciod_down(); /* release rpciod */
1521
1522 if (server->hostname != NULL)
1523 kfree(server->hostname);
1524 kfree(server);
1525}
1526
1527static struct file_system_type nfs_fs_type = {
1528 .owner = THIS_MODULE,
1529 .name = "nfs",
1530 .get_sb = nfs_get_sb,
1531 .kill_sb = nfs_kill_super,
1532 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1533};
1534
1535#ifdef CONFIG_NFS_V4
1536
1537static void nfs4_clear_inode(struct inode *);
1538
1539
1540static 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,
1549};
1550
1551/*
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
1554 * nfs_open().
1555 */
1556static void nfs4_clear_inode(struct inode *inode)
1557{
1558 struct nfs_inode *nfsi = NFS_I(inode);
1559
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;
1568
1569 state = list_entry(nfsi->open_states.next,
1570 struct nfs4_state,
1571 inode_states);
1572 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1573 __FUNCTION__,
1574 inode->i_sb->s_id,
1575 (long long)NFS_FILEID(inode),
1576 state);
1577 BUG_ON(atomic_read(&state->count) != 1);
1578 nfs4_close_state(state, state->state);
1579 }
1580}
1581
1582
1583static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1584{
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;
1592
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;
1602
1603 server->acregmin = data->acregmin*HZ;
1604 server->acregmax = data->acregmax*HZ;
1605 server->acdirmin = data->acdirmin*HZ;
1606 server->acdirmax = data->acdirmax*HZ;
1607
1608 server->rpc_ops = &nfs_v4_clientops;
1609 /* Initialize timeout values */
1610
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;
1616
1617 proto = data->proto;
1618 /* Which IP protocol do we use? */
1619 switch (proto) {
1620 case IPPROTO_TCP:
1621 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1622 if (!timeparms.to_initval)
1623 timeparms.to_initval = 600 * HZ / 10;
1624 break;
1625 case IPPROTO_UDP:
1626 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1627 if (!timeparms.to_initval)
1628 timeparms.to_initval = 11 * HZ / 10;
1629 break;
1630 default:
1631 return -EINVAL;
1632 }
1633
1634 clp = nfs4_get_client(&server->addr.sin_addr);
1635 if (!clp) {
9085bbcb 1636 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
1da177e4
LT
1637 return -EIO;
1638 }
1639
1640 /* Now create transport and client */
1641 authflavour = RPC_AUTH_UNIX;
1642 if (data->auth_flavourlen != 0) {
9085bbcb
TM
1643 if (data->auth_flavourlen != 1) {
1644 dprintk("%s: Invalid number of RPC auth flavours %d.\n",
1645 __FUNCTION__, data->auth_flavourlen);
1646 err = -EINVAL;
1647 goto out_fail;
1648 }
1da177e4
LT
1649 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1650 err = -EFAULT;
1651 goto out_fail;
1652 }
1653 }
1654
1655 down_write(&clp->cl_sem);
6a19275a 1656 if (IS_ERR(clp->cl_rpcclient)) {
1da177e4
LT
1657 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1658 if (IS_ERR(xprt)) {
1659 up_write(&clp->cl_sem);
1da177e4 1660 err = PTR_ERR(xprt);
9085bbcb
TM
1661 dprintk("%s: cannot create RPC transport. Error = %d\n",
1662 __FUNCTION__, err);
1da177e4
LT
1663 goto out_fail;
1664 }
1665 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1666 server->rpc_ops->version, authflavour);
1667 if (IS_ERR(clnt)) {
1668 up_write(&clp->cl_sem);
1da177e4 1669 err = PTR_ERR(clnt);
9085bbcb
TM
1670 dprintk("%s: cannot create RPC client. Error = %d\n",
1671 __FUNCTION__, err);
1da177e4
LT
1672 goto out_fail;
1673 }
1674 clnt->cl_intr = 1;
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;
1683 goto out_fail;
1684 }
1685 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1686 nfs_idmap_new(clp);
1687 }
1688 if (list_empty(&clp->cl_superblocks)) {
1689 err = nfs4_init_client(clp);
1690 if (err != 0) {
1691 up_write(&clp->cl_sem);
1692 goto out_fail;
1693 }
1694 }
1695 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1696 clnt = rpc_clone_client(clp->cl_rpcclient);
1697 if (!IS_ERR(clnt))
1698 server->nfs4_state = clp;
1699 up_write(&clp->cl_sem);
1700 clp = NULL;
1701
1702 if (IS_ERR(clnt)) {
9085bbcb
TM
1703 err = PTR_ERR(clnt);
1704 dprintk("%s: cannot create RPC client. Error = %d\n",
1705 __FUNCTION__, err);
1706 return err;
1da177e4
LT
1707 }
1708
1709 server->client = clnt;
1710
1711 if (server->nfs4_state->cl_idmap == NULL) {
9085bbcb 1712 dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
1da177e4
LT
1713 return -ENOMEM;
1714 }
1715
1716 if (clnt->cl_auth->au_flavor != authflavour) {
6a19275a
BF
1717 struct rpc_auth *auth;
1718
1719 auth = rpcauth_create(authflavour, clnt);
1720 if (IS_ERR(auth)) {
9085bbcb 1721 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
6a19275a 1722 return PTR_ERR(auth);
1da177e4
LT
1723 }
1724 }
1725
1726 sb->s_time_gran = 1;
1727
1728 sb->s_op = &nfs4_sops;
1729 err = nfs_sb_init(sb, authflavour);
1730 if (err == 0)
1731 return 0;
1732out_fail:
1733 if (clp)
1734 nfs4_put_client(clp);
1735 return err;
1736}
1737
1738static int nfs4_compare_super(struct super_block *sb, void *data)
1739{
1740 struct nfs_server *server = data;
1741 struct nfs_server *old = NFS_SB(sb);
1742
1743 if (strcmp(server->hostname, old->hostname) != 0)
1744 return 0;
1745 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1746 return 0;
1747 return 1;
1748}
1749
1750static void *
1751nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1752{
1753 void *p = NULL;
1754
1755 if (!src->len)
1756 return ERR_PTR(-EINVAL);
1757 if (src->len < maxlen)
1758 maxlen = src->len;
1759 if (dst == NULL) {
1760 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1761 if (p == NULL)
1762 return ERR_PTR(-ENOMEM);
1763 }
1764 if (copy_from_user(dst, src->data, maxlen)) {
1765 if (p != NULL)
1766 kfree(p);
1767 return ERR_PTR(-EFAULT);
1768 }
1769 dst[maxlen] = '\0';
1770 return dst;
1771}
1772
1773static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1774 int flags, const char *dev_name, void *raw_data)
1775{
1776 int error;
1777 struct nfs_server *server;
1778 struct super_block *s;
1779 struct nfs4_mount_data *data = raw_data;
1780 void *p;
1781
9085bbcb
TM
1782 if (data == NULL) {
1783 dprintk("%s: missing data argument\n", __FUNCTION__);
1784 return ERR_PTR(-EINVAL);
1785 }
1786 if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
1787 dprintk("%s: bad mount version\n", __FUNCTION__);
1da177e4
LT
1788 return ERR_PTR(-EINVAL);
1789 }
1790
1791 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1792 if (!server)
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);
6a19275a 1797 server->client = server->client_sys = ERR_PTR(-EINVAL);
1da177e4 1798
1da177e4
LT
1799 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1800 if (IS_ERR(p))
1801 goto out_err;
1802 server->hostname = p;
1803
1804 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1805 if (IS_ERR(p))
1806 goto out_err;
1807 server->mnt_path = p;
1808
1809 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1810 sizeof(server->ip_addr) - 1);
1811 if (IS_ERR(p))
1812 goto out_err;
1813
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);
1817 goto out_free;
1818 }
1819 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1820 s = ERR_PTR(-EFAULT);
1821 goto out_free;
1822 }
1823 if (server->addr.sin_family != AF_INET ||
1824 server->addr.sin_addr.s_addr == INADDR_ANY) {
9085bbcb
TM
1825 dprintk("%s: mount program didn't pass remote IP address!\n",
1826 __FUNCTION__);
1da177e4
LT
1827 s = ERR_PTR(-EINVAL);
1828 goto out_free;
1829 }
1830
9085bbcb
TM
1831 /* Fire up rpciod if not yet running */
1832 s = ERR_PTR(rpciod_up());
1833 if (IS_ERR(s)) {
1834 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1835 __FUNCTION__, PTR_ERR(s));
1836 goto out_free;
1837 }
1838
1da177e4
LT
1839 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1840
1841 if (IS_ERR(s) || s->s_root)
1842 goto out_free;
1843
1844 s->s_flags = flags;
1845
1da177e4
LT
1846 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1847 if (error) {
1848 up_write(&s->s_umount);
1849 deactivate_super(s);
1850 return ERR_PTR(error);
1851 }
1852 s->s_flags |= MS_ACTIVE;
1853 return s;
1854out_err:
1855 s = (struct super_block *)p;
1856out_free:
1857 if (server->mnt_path)
1858 kfree(server->mnt_path);
1859 if (server->hostname)
1860 kfree(server->hostname);
1861 kfree(server);
1862 return s;
1863}
1864
1865static void nfs4_kill_super(struct super_block *sb)
1866{
1867 struct nfs_server *server = NFS_SB(sb);
1868
1869 nfs_return_all_delegations(sb);
1870 kill_anon_super(sb);
1871
1872 nfs4_renewd_prepare_shutdown(server);
1873
1874 if (server->client != NULL && !IS_ERR(server->client))
1875 rpc_shutdown_client(server->client);
1876 rpciod_down(); /* release rpciod */
1877
1878 destroy_nfsv4_state(server);
1879
1880 if (server->hostname != NULL)
1881 kfree(server->hostname);
1882 kfree(server);
1883}
1884
1885static struct file_system_type nfs4_fs_type = {
1886 .owner = THIS_MODULE,
1887 .name = "nfs4",
1888 .get_sb = nfs4_get_sb,
1889 .kill_sb = nfs4_kill_super,
1890 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1891};
1892
1893#define nfs4_init_once(nfsi) \
1894 do { \
1895 INIT_LIST_HEAD(&(nfsi)->open_states); \
1896 nfsi->delegation = NULL; \
1897 nfsi->delegation_state = 0; \
1898 init_rwsem(&nfsi->rwsem); \
1899 } while(0)
1900#define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1901#define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1902#else
1903#define nfs4_init_once(nfsi) \
1904 do { } while (0)
1905#define register_nfs4fs() (0)
1906#define unregister_nfs4fs()
1907#endif
1908
1909extern int nfs_init_nfspagecache(void);
1910extern void nfs_destroy_nfspagecache(void);
1911extern int nfs_init_readpagecache(void);
1912extern void nfs_destroy_readpagecache(void);
1913extern int nfs_init_writepagecache(void);
1914extern void nfs_destroy_writepagecache(void);
1915#ifdef CONFIG_NFS_DIRECTIO
1916extern int nfs_init_directcache(void);
1917extern void nfs_destroy_directcache(void);
1918#endif
1919
1920static kmem_cache_t * nfs_inode_cachep;
1921
1922static struct inode *nfs_alloc_inode(struct super_block *sb)
1923{
1924 struct nfs_inode *nfsi;
1925 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1926 if (!nfsi)
1927 return NULL;
1928 nfsi->flags = 0;
e50a1c2e
BF
1929#ifdef CONFIG_NFS_V4
1930 nfsi->nfs4_acl = NULL;
1931#endif /* CONFIG_NFS_V4 */
1da177e4
LT
1932 return &nfsi->vfs_inode;
1933}
1934
1935static void nfs_destroy_inode(struct inode *inode)
1936{
1937 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1938}
1939
1940static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1941{
1942 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1943
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);
1953 nfsi->ndirty = 0;
1954 nfsi->ncommit = 0;
1955 nfsi->npages = 0;
1956 init_waitqueue_head(&nfsi->nfs_i_wait);
1957 nfs4_init_once(nfsi);
1958 }
1959}
1960
75c96f85 1961static int nfs_init_inodecache(void)
1da177e4
LT
1962{
1963 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1964 sizeof(struct nfs_inode),
1965 0, SLAB_RECLAIM_ACCOUNT,
1966 init_once, NULL);
1967 if (nfs_inode_cachep == NULL)
1968 return -ENOMEM;
1969
1970 return 0;
1971}
1972
75c96f85 1973static void nfs_destroy_inodecache(void)
1da177e4
LT
1974{
1975 if (kmem_cache_destroy(nfs_inode_cachep))
1976 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1977}
1978
1979/*
1980 * Initialize NFS
1981 */
1982static int __init init_nfs_fs(void)
1983{
1984 int err;
1985
1986 err = nfs_init_nfspagecache();
1987 if (err)
1988 goto out4;
1989
1990 err = nfs_init_inodecache();
1991 if (err)
1992 goto out3;
1993
1994 err = nfs_init_readpagecache();
1995 if (err)
1996 goto out2;
1997
1998 err = nfs_init_writepagecache();
1999 if (err)
2000 goto out1;
2001
2002#ifdef CONFIG_NFS_DIRECTIO
2003 err = nfs_init_directcache();
2004 if (err)
2005 goto out0;
2006#endif
2007
2008#ifdef CONFIG_PROC_FS
2009 rpc_proc_register(&nfs_rpcstat);
2010#endif
2011 err = register_filesystem(&nfs_fs_type);
2012 if (err)
2013 goto out;
2014 if ((err = register_nfs4fs()) != 0)
2015 goto out;
2016 return 0;
2017out:
2018#ifdef CONFIG_PROC_FS
2019 rpc_proc_unregister("nfs");
2020#endif
2021 nfs_destroy_writepagecache();
2022#ifdef CONFIG_NFS_DIRECTIO
2023out0:
2024 nfs_destroy_directcache();
2025#endif
2026out1:
2027 nfs_destroy_readpagecache();
2028out2:
2029 nfs_destroy_inodecache();
2030out3:
2031 nfs_destroy_nfspagecache();
2032out4:
2033 return err;
2034}
2035
2036static void __exit exit_nfs_fs(void)
2037{
2038#ifdef CONFIG_NFS_DIRECTIO
2039 nfs_destroy_directcache();
2040#endif
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");
2047#endif
2048 unregister_filesystem(&nfs_fs_type);
2049 unregister_nfs4fs();
2050}
2051
2052/* Not quite true; I just maintain it */
2053MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2054MODULE_LICENSE("GPL");
2055
2056module_init(init_nfs_fs)
2057module_exit(exit_nfs_fs)