]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/inode.c
[PATCH] RPC: Make rpc_create_client() probe server for RPC program+version support
[mirror_ubuntu-bionic-kernel.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
42#include "delegation.h"
43
44#define NFSDBG_FACILITY NFSDBG_VFS
45#define NFS_PARANOIA 1
46
47/* Maximum number of readahead requests
48 * FIXME: this should really be a sysctl so that users may tune it to suit
49 * their needs. People that do NFS over a slow network, might for
50 * instance want to reduce it to something closer to 1 for improved
51 * interactive response.
52 */
53#define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
54
55static void nfs_invalidate_inode(struct inode *);
56static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
57
58static struct inode *nfs_alloc_inode(struct super_block *sb);
59static void nfs_destroy_inode(struct inode *);
60static int nfs_write_inode(struct inode *,int);
61static void nfs_delete_inode(struct inode *);
62static void nfs_clear_inode(struct inode *);
63static void nfs_umount_begin(struct super_block *);
64static int nfs_statfs(struct super_block *, struct kstatfs *);
65static int nfs_show_options(struct seq_file *, struct vfsmount *);
66
67static struct rpc_program nfs_program;
68
69static struct super_operations nfs_sops = {
70 .alloc_inode = nfs_alloc_inode,
71 .destroy_inode = nfs_destroy_inode,
72 .write_inode = nfs_write_inode,
73 .delete_inode = nfs_delete_inode,
74 .statfs = nfs_statfs,
75 .clear_inode = nfs_clear_inode,
76 .umount_begin = nfs_umount_begin,
77 .show_options = nfs_show_options,
78};
79
80/*
81 * RPC cruft for NFS
82 */
83static struct rpc_stat nfs_rpcstat = {
84 .program = &nfs_program
85};
86static struct rpc_version * nfs_version[] = {
87 NULL,
88 NULL,
89 &nfs_version2,
90#if defined(CONFIG_NFS_V3)
91 &nfs_version3,
92#elif defined(CONFIG_NFS_V4)
93 NULL,
94#endif
95#if defined(CONFIG_NFS_V4)
96 &nfs_version4,
97#endif
98};
99
100static struct rpc_program nfs_program = {
101 .name = "nfs",
102 .number = NFS_PROGRAM,
103 .nrvers = sizeof(nfs_version) / sizeof(nfs_version[0]),
104 .version = nfs_version,
105 .stats = &nfs_rpcstat,
106 .pipe_dir_name = "/nfs",
107};
108
109static inline unsigned long
110nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
111{
112 return nfs_fileid_to_ino_t(fattr->fileid);
113}
114
115static int
116nfs_write_inode(struct inode *inode, int sync)
117{
118 int flags = sync ? FLUSH_WAIT : 0;
119 int ret;
120
121 ret = nfs_commit_inode(inode, 0, 0, flags);
122 if (ret < 0)
123 return ret;
124 return 0;
125}
126
127static void
128nfs_delete_inode(struct inode * inode)
129{
130 dprintk("NFS: delete_inode(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
131
132 nfs_wb_all(inode);
133 /*
134 * The following should never happen...
135 */
136 if (nfs_have_writebacks(inode)) {
137 printk(KERN_ERR "nfs_delete_inode: inode %ld has pending RPC requests\n", inode->i_ino);
138 }
139
140 clear_inode(inode);
141}
142
143/*
144 * For the moment, the only task for the NFS clear_inode method is to
145 * release the mmap credential
146 */
147static void
148nfs_clear_inode(struct inode *inode)
149{
150 struct nfs_inode *nfsi = NFS_I(inode);
151 struct rpc_cred *cred;
152
153 nfs_wb_all(inode);
154 BUG_ON (!list_empty(&nfsi->open_files));
155 cred = nfsi->cache_access.cred;
156 if (cred)
157 put_rpccred(cred);
158 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
159}
160
161void
162nfs_umount_begin(struct super_block *sb)
163{
164 struct nfs_server *server = NFS_SB(sb);
165 struct rpc_clnt *rpc;
166
167 /* -EIO all pending I/O */
168 if ((rpc = server->client) != NULL)
169 rpc_killall_tasks(rpc);
170}
171
172
173static inline unsigned long
174nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
175{
176 /* make sure blocksize is a power of two */
177 if ((bsize & (bsize - 1)) || nrbitsp) {
178 unsigned char nrbits;
179
180 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
181 ;
182 bsize = 1 << nrbits;
183 if (nrbitsp)
184 *nrbitsp = nrbits;
185 }
186
187 return bsize;
188}
189
190/*
191 * Calculate the number of 512byte blocks used.
192 */
193static inline unsigned long
194nfs_calc_block_size(u64 tsize)
195{
196 loff_t used = (tsize + 511) >> 9;
197 return (used > ULONG_MAX) ? ULONG_MAX : used;
198}
199
200/*
201 * Compute and set NFS server blocksize
202 */
203static inline unsigned long
204nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
205{
206 if (bsize < 1024)
207 bsize = NFS_DEF_FILE_IO_BUFFER_SIZE;
208 else if (bsize >= NFS_MAX_FILE_IO_BUFFER_SIZE)
209 bsize = NFS_MAX_FILE_IO_BUFFER_SIZE;
210
211 return nfs_block_bits(bsize, nrbitsp);
212}
213
214/*
215 * Obtain the root inode of the file system.
216 */
217static struct inode *
218nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
219{
220 struct nfs_server *server = NFS_SB(sb);
221 struct inode *rooti;
222 int error;
223
224 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
225 if (error < 0) {
226 dprintk("nfs_get_root: getattr error = %d\n", -error);
227 return ERR_PTR(error);
228 }
229
230 rooti = nfs_fhget(sb, rootfh, fsinfo->fattr);
231 if (!rooti)
232 return ERR_PTR(-ENOMEM);
233 return rooti;
234}
235
236/*
237 * Do NFS version-independent mount processing, and sanity checking
238 */
239static int
240nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
241{
242 struct nfs_server *server;
243 struct inode *root_inode;
244 struct nfs_fattr fattr;
245 struct nfs_fsinfo fsinfo = {
246 .fattr = &fattr,
247 };
248 struct nfs_pathconf pathinfo = {
249 .fattr = &fattr,
250 };
251 int no_root_error = 0;
252 unsigned long max_rpc_payload;
253
254 /* We probably want something more informative here */
255 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
256
257 server = NFS_SB(sb);
258
259 sb->s_magic = NFS_SUPER_MAGIC;
260
261 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
262 /* Did getting the root inode fail? */
263 if (IS_ERR(root_inode)) {
264 no_root_error = PTR_ERR(root_inode);
265 goto out_no_root;
266 }
267 sb->s_root = d_alloc_root(root_inode);
268 if (!sb->s_root) {
269 no_root_error = -ENOMEM;
270 goto out_no_root;
271 }
272 sb->s_root->d_op = server->rpc_ops->dentry_ops;
273
274 /* Get some general file system info */
275 if (server->namelen == 0 &&
276 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
277 server->namelen = pathinfo.max_namelen;
278 /* Work out a lot of parameters */
279 if (server->rsize == 0)
280 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
281 if (server->wsize == 0)
282 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
283
284 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
285 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
286 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
287 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
288
289 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
290 if (server->rsize > max_rpc_payload)
291 server->rsize = max_rpc_payload;
292 if (server->wsize > max_rpc_payload)
293 server->wsize = max_rpc_payload;
294
295 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
296 if (server->rpages > NFS_READ_MAXIOV) {
297 server->rpages = NFS_READ_MAXIOV;
298 server->rsize = server->rpages << PAGE_CACHE_SHIFT;
299 }
300
301 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
302 if (server->wpages > NFS_WRITE_MAXIOV) {
303 server->wpages = NFS_WRITE_MAXIOV;
304 server->wsize = server->wpages << PAGE_CACHE_SHIFT;
305 }
306
307 if (sb->s_blocksize == 0)
308 sb->s_blocksize = nfs_block_bits(server->wsize,
309 &sb->s_blocksize_bits);
310 server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
311
312 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
313 if (server->dtsize > PAGE_CACHE_SIZE)
314 server->dtsize = PAGE_CACHE_SIZE;
315 if (server->dtsize > server->rsize)
316 server->dtsize = server->rsize;
317
318 if (server->flags & NFS_MOUNT_NOAC) {
319 server->acregmin = server->acregmax = 0;
320 server->acdirmin = server->acdirmax = 0;
321 sb->s_flags |= MS_SYNCHRONOUS;
322 }
323 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
324
325 sb->s_maxbytes = fsinfo.maxfilesize;
326 if (sb->s_maxbytes > MAX_LFS_FILESIZE)
327 sb->s_maxbytes = MAX_LFS_FILESIZE;
328
329 server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
330 server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
331
332 /* We're airborne Set socket buffersize */
333 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
334 return 0;
335 /* Yargs. It didn't work out. */
336out_no_root:
337 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
338 if (!IS_ERR(root_inode))
339 iput(root_inode);
340 return no_root_error;
341}
342
343/*
344 * Create an RPC client handle.
345 */
346static struct rpc_clnt *
347nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
348{
349 struct rpc_timeout timeparms;
350 struct rpc_xprt *xprt = NULL;
351 struct rpc_clnt *clnt = NULL;
352 int tcp = (data->flags & NFS_MOUNT_TCP);
353
354 /* Initialize timeout values */
355 timeparms.to_initval = data->timeo * HZ / 10;
356 timeparms.to_retries = data->retrans;
357 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
358 timeparms.to_exponential = 1;
359
360 if (!timeparms.to_initval)
361 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
362 if (!timeparms.to_retries)
363 timeparms.to_retries = 5;
364
365 /* create transport and client */
366 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP,
367 &server->addr, &timeparms);
368 if (IS_ERR(xprt)) {
369 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
370 return (struct rpc_clnt *)xprt;
371 }
372 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
373 server->rpc_ops->version, data->pseudoflavor);
374 if (IS_ERR(clnt)) {
375 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
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. */
429 if (server->flags & NFS_MOUNT_VER3) {
430#ifdef CONFIG_NFS_V3
431 server->rpc_ops = &nfs_v3_clientops;
432 server->caps |= NFS_CAP_READDIRPLUS;
433 if (data->version < 4) {
434 printk(KERN_NOTICE "NFS: NFSv3 not supported by mount program.\n");
435 return -EIO;
436 }
437#else
438 printk(KERN_NOTICE "NFS: NFSv3 not supported.\n");
439 return -EIO;
440#endif
441 } else {
442 server->rpc_ops = &nfs_v2_clientops;
443 }
444
445 /* Fill in pseudoflavor for mount version < 5 */
446 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
447 data->pseudoflavor = RPC_AUTH_UNIX;
448 authflavor = data->pseudoflavor; /* save for sb_init() */
449 /* XXX maybe we want to add a server->pseudoflavor field */
450
451 /* Create RPC client handles */
452 server->client = nfs_create_client(server, data);
453 if (IS_ERR(server->client))
454 return PTR_ERR(server->client);
455 /* RFC 2623, sec 2.3.2 */
456 if (authflavor != RPC_AUTH_UNIX) {
457 server->client_sys = rpc_clone_client(server->client);
458 if (IS_ERR(server->client_sys))
459 return PTR_ERR(server->client_sys);
460 if (!rpcauth_create(RPC_AUTH_UNIX, server->client_sys))
461 return -ENOMEM;
462 } else {
463 atomic_inc(&server->client->cl_count);
464 server->client_sys = server->client;
465 }
466
467 if (server->flags & NFS_MOUNT_VER3) {
468 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
469 server->namelen = NFS3_MAXNAMLEN;
470 sb->s_time_gran = 1;
471 } else {
472 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
473 server->namelen = NFS2_MAXNAMLEN;
474 }
475
476 sb->s_op = &nfs_sops;
477 return nfs_sb_init(sb, authflavor);
478}
479
480static int
481nfs_statfs(struct super_block *sb, struct kstatfs *buf)
482{
483 struct nfs_server *server = NFS_SB(sb);
484 unsigned char blockbits;
485 unsigned long blockres;
486 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
487 struct nfs_fattr fattr;
488 struct nfs_fsstat res = {
489 .fattr = &fattr,
490 };
491 int error;
492
493 lock_kernel();
494
495 error = server->rpc_ops->statfs(server, rootfh, &res);
496 buf->f_type = NFS_SUPER_MAGIC;
497 if (error < 0)
498 goto out_err;
499
500 /*
501 * Current versions of glibc do not correctly handle the
502 * case where f_frsize != f_bsize. Eventually we want to
503 * report the value of wtmult in this field.
504 */
505 buf->f_frsize = sb->s_blocksize;
506
507 /*
508 * On most *nix systems, f_blocks, f_bfree, and f_bavail
509 * are reported in units of f_frsize. Linux hasn't had
510 * an f_frsize field in its statfs struct until recently,
511 * thus historically Linux's sys_statfs reports these
512 * fields in units of f_bsize.
513 */
514 buf->f_bsize = sb->s_blocksize;
515 blockbits = sb->s_blocksize_bits;
516 blockres = (1 << blockbits) - 1;
517 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
518 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
519 buf->f_bavail = (res.abytes + blockres) >> blockbits;
520
521 buf->f_files = res.tfiles;
522 buf->f_ffree = res.afiles;
523
524 buf->f_namelen = server->namelen;
525 out:
526 unlock_kernel();
527
528 return 0;
529
530 out_err:
531 printk(KERN_WARNING "nfs_statfs: statfs error = %d\n", -error);
532 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
533 goto out;
534
535}
536
537static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
538{
539 static struct proc_nfs_info {
540 int flag;
541 char *str;
542 char *nostr;
543 } nfs_info[] = {
544 { NFS_MOUNT_SOFT, ",soft", ",hard" },
545 { NFS_MOUNT_INTR, ",intr", "" },
546 { NFS_MOUNT_POSIX, ",posix", "" },
547 { NFS_MOUNT_TCP, ",tcp", ",udp" },
548 { NFS_MOUNT_NOCTO, ",nocto", "" },
549 { NFS_MOUNT_NOAC, ",noac", "" },
550 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
551 { 0, NULL, NULL }
552 };
553 struct proc_nfs_info *nfs_infop;
554 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
555
556 seq_printf(m, ",v%d", nfss->rpc_ops->version);
557 seq_printf(m, ",rsize=%d", nfss->rsize);
558 seq_printf(m, ",wsize=%d", nfss->wsize);
559 if (nfss->acregmin != 3*HZ)
560 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
561 if (nfss->acregmax != 60*HZ)
562 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
563 if (nfss->acdirmin != 30*HZ)
564 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
565 if (nfss->acdirmax != 60*HZ)
566 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
567 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
568 if (nfss->flags & nfs_infop->flag)
569 seq_puts(m, nfs_infop->str);
570 else
571 seq_puts(m, nfs_infop->nostr);
572 }
573 seq_puts(m, ",addr=");
574 seq_escape(m, nfss->hostname, " \t\n\\");
575 return 0;
576}
577
578/*
579 * Invalidate the local caches
580 */
581void
582nfs_zap_caches(struct inode *inode)
583{
584 struct nfs_inode *nfsi = NFS_I(inode);
585 int mode = inode->i_mode;
586
587 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
588 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
589
590 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
591 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
592 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS;
593 else
594 nfsi->flags |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS;
595}
596
597/*
598 * Invalidate, but do not unhash, the inode
599 */
600static void
601nfs_invalidate_inode(struct inode *inode)
602{
603 umode_t save_mode = inode->i_mode;
604
605 make_bad_inode(inode);
606 inode->i_mode = save_mode;
607 nfs_zap_caches(inode);
608}
609
610struct nfs_find_desc {
611 struct nfs_fh *fh;
612 struct nfs_fattr *fattr;
613};
614
615/*
616 * In NFSv3 we can have 64bit inode numbers. In order to support
617 * this, and re-exported directories (also seen in NFSv2)
618 * we are forced to allow 2 different inodes to have the same
619 * i_ino.
620 */
621static int
622nfs_find_actor(struct inode *inode, void *opaque)
623{
624 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
625 struct nfs_fh *fh = desc->fh;
626 struct nfs_fattr *fattr = desc->fattr;
627
628 if (NFS_FILEID(inode) != fattr->fileid)
629 return 0;
630 if (nfs_compare_fh(NFS_FH(inode), fh))
631 return 0;
632 if (is_bad_inode(inode) || NFS_STALE(inode))
633 return 0;
634 return 1;
635}
636
637static int
638nfs_init_locked(struct inode *inode, void *opaque)
639{
640 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
641 struct nfs_fattr *fattr = desc->fattr;
642
643 NFS_FILEID(inode) = fattr->fileid;
644 nfs_copy_fh(NFS_FH(inode), desc->fh);
645 return 0;
646}
647
648/* Don't use READDIRPLUS on directories that we believe are too large */
649#define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
650
651/*
652 * This is our front-end to iget that looks up inodes by file handle
653 * instead of inode number.
654 */
655struct inode *
656nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
657{
658 struct nfs_find_desc desc = {
659 .fh = fh,
660 .fattr = fattr
661 };
662 struct inode *inode = NULL;
663 unsigned long hash;
664
665 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
666 goto out_no_inode;
667
668 if (!fattr->nlink) {
669 printk("NFS: Buggy server - nlink == 0!\n");
670 goto out_no_inode;
671 }
672
673 hash = nfs_fattr_to_ino_t(fattr);
674
675 if (!(inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc)))
676 goto out_no_inode;
677
678 if (inode->i_state & I_NEW) {
679 struct nfs_inode *nfsi = NFS_I(inode);
680
681 /* We set i_ino for the few things that still rely on it,
682 * such as stat(2) */
683 inode->i_ino = hash;
684
685 /* We can't support update_atime(), since the server will reset it */
686 inode->i_flags |= S_NOATIME|S_NOCMTIME;
687 inode->i_mode = fattr->mode;
688 /* Why so? Because we want revalidate for devices/FIFOs, and
689 * that's precisely what we have in nfs_file_inode_operations.
690 */
691 inode->i_op = &nfs_file_inode_operations;
692 if (S_ISREG(inode->i_mode)) {
693 inode->i_fop = &nfs_file_operations;
694 inode->i_data.a_ops = &nfs_file_aops;
695 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
696 } else if (S_ISDIR(inode->i_mode)) {
697 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
698 inode->i_fop = &nfs_dir_operations;
699 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
700 && fattr->size <= NFS_LIMIT_READDIRPLUS)
701 NFS_FLAGS(inode) |= NFS_INO_ADVISE_RDPLUS;
702 } else if (S_ISLNK(inode->i_mode))
703 inode->i_op = &nfs_symlink_inode_operations;
704 else
705 init_special_inode(inode, inode->i_mode, fattr->rdev);
706
707 nfsi->read_cache_jiffies = fattr->timestamp;
708 inode->i_atime = fattr->atime;
709 inode->i_mtime = fattr->mtime;
710 inode->i_ctime = fattr->ctime;
711 if (fattr->valid & NFS_ATTR_FATTR_V4)
712 nfsi->change_attr = fattr->change_attr;
713 inode->i_size = nfs_size_to_loff_t(fattr->size);
714 inode->i_nlink = fattr->nlink;
715 inode->i_uid = fattr->uid;
716 inode->i_gid = fattr->gid;
717 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
718 /*
719 * report the blocks in 512byte units
720 */
721 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
722 inode->i_blksize = inode->i_sb->s_blocksize;
723 } else {
724 inode->i_blocks = fattr->du.nfs2.blocks;
725 inode->i_blksize = fattr->du.nfs2.blocksize;
726 }
727 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
728 nfsi->attrtimeo_timestamp = jiffies;
729 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
730 nfsi->cache_access.cred = NULL;
731
732 unlock_new_inode(inode);
733 } else
734 nfs_refresh_inode(inode, fattr);
735 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
736 inode->i_sb->s_id,
737 (long long)NFS_FILEID(inode),
738 atomic_read(&inode->i_count));
739
740out:
741 return inode;
742
743out_no_inode:
744 printk("nfs_fhget: iget failed\n");
745 goto out;
746}
747
748#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
749
750int
751nfs_setattr(struct dentry *dentry, struct iattr *attr)
752{
753 struct inode *inode = dentry->d_inode;
754 struct nfs_fattr fattr;
755 int error;
756
757 if (attr->ia_valid & ATTR_SIZE) {
758 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
759 attr->ia_valid &= ~ATTR_SIZE;
760 }
761
762 /* Optimization: if the end result is no change, don't RPC */
763 attr->ia_valid &= NFS_VALID_ATTRS;
764 if (attr->ia_valid == 0)
765 return 0;
766
767 lock_kernel();
768 nfs_begin_data_update(inode);
769 /* Write all dirty data if we're changing file permissions or size */
770 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE)) != 0) {
771 if (filemap_fdatawrite(inode->i_mapping) == 0)
772 filemap_fdatawait(inode->i_mapping);
773 nfs_wb_all(inode);
774 }
775 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
776 if (error == 0) {
777 nfs_refresh_inode(inode, &fattr);
778 if ((attr->ia_valid & ATTR_MODE) != 0) {
779 int mode;
780 mode = inode->i_mode & ~S_IALLUGO;
781 mode |= attr->ia_mode & S_IALLUGO;
782 inode->i_mode = mode;
783 }
784 if ((attr->ia_valid & ATTR_UID) != 0)
785 inode->i_uid = attr->ia_uid;
786 if ((attr->ia_valid & ATTR_GID) != 0)
787 inode->i_gid = attr->ia_gid;
788 if ((attr->ia_valid & ATTR_SIZE) != 0) {
789 inode->i_size = attr->ia_size;
790 vmtruncate(inode, attr->ia_size);
791 }
792 }
793 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
794 NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS;
795 nfs_end_data_update(inode);
796 unlock_kernel();
797 return error;
798}
799
800/*
801 * Wait for the inode to get unlocked.
802 * (Used for NFS_INO_LOCKED and NFS_INO_REVALIDATING).
803 */
804static int
805nfs_wait_on_inode(struct inode *inode, int flag)
806{
807 struct rpc_clnt *clnt = NFS_CLIENT(inode);
808 struct nfs_inode *nfsi = NFS_I(inode);
809
810 int error;
811 if (!(NFS_FLAGS(inode) & flag))
812 return 0;
813 atomic_inc(&inode->i_count);
814 error = nfs_wait_event(clnt, nfsi->nfs_i_wait,
815 !(NFS_FLAGS(inode) & flag));
816 iput(inode);
817 return error;
818}
819
820int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
821{
822 struct inode *inode = dentry->d_inode;
823 struct nfs_inode *nfsi = NFS_I(inode);
824 int need_atime = nfsi->flags & NFS_INO_INVALID_ATIME;
825 int err;
826
827 if (__IS_FLG(inode, MS_NOATIME))
828 need_atime = 0;
829 else if (__IS_FLG(inode, MS_NODIRATIME) && S_ISDIR(inode->i_mode))
830 need_atime = 0;
831 /* We may force a getattr if the user cares about atime */
832 if (need_atime)
833 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
834 else
835 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
836 if (!err)
837 generic_fillattr(inode, stat);
838 return err;
839}
840
841struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, struct rpc_cred *cred)
842{
843 struct nfs_open_context *ctx;
844
845 ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
846 if (ctx != NULL) {
847 atomic_set(&ctx->count, 1);
848 ctx->dentry = dget(dentry);
849 ctx->cred = get_rpccred(cred);
850 ctx->state = NULL;
851 ctx->lockowner = current->files;
852 ctx->error = 0;
853 init_waitqueue_head(&ctx->waitq);
854 }
855 return ctx;
856}
857
858struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
859{
860 if (ctx != NULL)
861 atomic_inc(&ctx->count);
862 return ctx;
863}
864
865void put_nfs_open_context(struct nfs_open_context *ctx)
866{
867 if (atomic_dec_and_test(&ctx->count)) {
868 if (!list_empty(&ctx->list)) {
869 struct inode *inode = ctx->dentry->d_inode;
870 spin_lock(&inode->i_lock);
871 list_del(&ctx->list);
872 spin_unlock(&inode->i_lock);
873 }
874 if (ctx->state != NULL)
875 nfs4_close_state(ctx->state, ctx->mode);
876 if (ctx->cred != NULL)
877 put_rpccred(ctx->cred);
878 dput(ctx->dentry);
879 kfree(ctx);
880 }
881}
882
883/*
884 * Ensure that mmap has a recent RPC credential for use when writing out
885 * shared pages
886 */
887void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
888{
889 struct inode *inode = filp->f_dentry->d_inode;
890 struct nfs_inode *nfsi = NFS_I(inode);
891
892 filp->private_data = get_nfs_open_context(ctx);
893 spin_lock(&inode->i_lock);
894 list_add(&ctx->list, &nfsi->open_files);
895 spin_unlock(&inode->i_lock);
896}
897
898struct nfs_open_context *nfs_find_open_context(struct inode *inode, int mode)
899{
900 struct nfs_inode *nfsi = NFS_I(inode);
901 struct nfs_open_context *pos, *ctx = NULL;
902
903 spin_lock(&inode->i_lock);
904 list_for_each_entry(pos, &nfsi->open_files, list) {
905 if ((pos->mode & mode) == mode) {
906 ctx = get_nfs_open_context(pos);
907 break;
908 }
909 }
910 spin_unlock(&inode->i_lock);
911 return ctx;
912}
913
914void nfs_file_clear_open_context(struct file *filp)
915{
916 struct inode *inode = filp->f_dentry->d_inode;
917 struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
918
919 if (ctx) {
920 filp->private_data = NULL;
921 spin_lock(&inode->i_lock);
922 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
923 spin_unlock(&inode->i_lock);
924 put_nfs_open_context(ctx);
925 }
926}
927
928/*
929 * These allocate and release file read/write context information.
930 */
931int nfs_open(struct inode *inode, struct file *filp)
932{
933 struct nfs_open_context *ctx;
934 struct rpc_cred *cred;
935
936 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
937 if (IS_ERR(cred))
938 return PTR_ERR(cred);
939 ctx = alloc_nfs_open_context(filp->f_dentry, cred);
940 put_rpccred(cred);
941 if (ctx == NULL)
942 return -ENOMEM;
943 ctx->mode = filp->f_mode;
944 nfs_file_set_open_context(filp, ctx);
945 put_nfs_open_context(ctx);
946 if ((filp->f_mode & FMODE_WRITE) != 0)
947 nfs_begin_data_update(inode);
948 return 0;
949}
950
951int nfs_release(struct inode *inode, struct file *filp)
952{
953 if ((filp->f_mode & FMODE_WRITE) != 0)
954 nfs_end_data_update(inode);
955 nfs_file_clear_open_context(filp);
956 return 0;
957}
958
959/*
960 * This function is called whenever some part of NFS notices that
961 * the cached attributes have to be refreshed.
962 */
963int
964__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
965{
966 int status = -ESTALE;
967 struct nfs_fattr fattr;
968 struct nfs_inode *nfsi = NFS_I(inode);
969 unsigned long verifier;
970 unsigned int flags;
971
972 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
973 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
974
975 lock_kernel();
976 if (!inode || is_bad_inode(inode))
977 goto out_nowait;
978 if (NFS_STALE(inode))
979 goto out_nowait;
980
981 while (NFS_REVALIDATING(inode)) {
982 status = nfs_wait_on_inode(inode, NFS_INO_REVALIDATING);
983 if (status < 0)
984 goto out_nowait;
985 if (NFS_ATTRTIMEO(inode) == 0)
986 continue;
987 if (NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ATIME))
988 continue;
989 status = NFS_STALE(inode) ? -ESTALE : 0;
990 goto out_nowait;
991 }
992 NFS_FLAGS(inode) |= NFS_INO_REVALIDATING;
993
994 /* Protect against RPC races by saving the change attribute */
995 verifier = nfs_save_change_attribute(inode);
996 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
997 if (status != 0) {
998 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
999 inode->i_sb->s_id,
1000 (long long)NFS_FILEID(inode), status);
1001 if (status == -ESTALE) {
1002 nfs_zap_caches(inode);
1003 if (!S_ISDIR(inode->i_mode))
1004 NFS_FLAGS(inode) |= NFS_INO_STALE;
1005 }
1006 goto out;
1007 }
1008
1009 status = nfs_update_inode(inode, &fattr, verifier);
1010 if (status) {
1011 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
1012 inode->i_sb->s_id,
1013 (long long)NFS_FILEID(inode), status);
1014 goto out;
1015 }
1016 flags = nfsi->flags;
1017 /*
1018 * We may need to keep the attributes marked as invalid if
1019 * we raced with nfs_end_attr_update().
1020 */
1021 if (verifier == nfsi->cache_change_attribute)
1022 nfsi->flags &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
1023 /* Do the page cache invalidation */
1024 if (flags & NFS_INO_INVALID_DATA) {
1025 if (S_ISREG(inode->i_mode)) {
1026 if (filemap_fdatawrite(inode->i_mapping) == 0)
1027 filemap_fdatawait(inode->i_mapping);
1028 nfs_wb_all(inode);
1029 }
1030 nfsi->flags &= ~NFS_INO_INVALID_DATA;
1031 invalidate_inode_pages2(inode->i_mapping);
1032 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
1033 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1034 inode->i_sb->s_id,
1035 (long long)NFS_FILEID(inode));
1036 /* This ensures we revalidate dentries */
1037 nfsi->cache_change_attribute++;
1038 }
1039 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1040 inode->i_sb->s_id,
1041 (long long)NFS_FILEID(inode));
1042
1043out:
1044 NFS_FLAGS(inode) &= ~NFS_INO_REVALIDATING;
1045 wake_up(&nfsi->nfs_i_wait);
1046 out_nowait:
1047 unlock_kernel();
1048 return status;
1049}
1050
1051int nfs_attribute_timeout(struct inode *inode)
1052{
1053 struct nfs_inode *nfsi = NFS_I(inode);
1054
1055 if (nfs_have_delegation(inode, FMODE_READ))
1056 return 0;
1057 return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
1058}
1059
1060/**
1061 * nfs_revalidate_inode - Revalidate the inode attributes
1062 * @server - pointer to nfs_server struct
1063 * @inode - pointer to inode struct
1064 *
1065 * Updates inode attribute information by retrieving the data from the server.
1066 */
1067int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1068{
1069 if (!(NFS_FLAGS(inode) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
1070 && !nfs_attribute_timeout(inode))
1071 return NFS_STALE(inode) ? -ESTALE : 0;
1072 return __nfs_revalidate_inode(server, inode);
1073}
1074
1075/**
1076 * nfs_begin_data_update
1077 * @inode - pointer to inode
1078 * Declare that a set of operations will update file data on the server
1079 */
1080void nfs_begin_data_update(struct inode *inode)
1081{
1082 atomic_inc(&NFS_I(inode)->data_updates);
1083}
1084
1085/**
1086 * nfs_end_data_update
1087 * @inode - pointer to inode
1088 * Declare end of the operations that will update file data
1089 * This will mark the inode as immediately needing revalidation
1090 * of its attribute cache.
1091 */
1092void nfs_end_data_update(struct inode *inode)
1093{
1094 struct nfs_inode *nfsi = NFS_I(inode);
1095
1096 if (!nfs_have_delegation(inode, FMODE_READ)) {
1097 /* Mark the attribute cache for revalidation */
1098 nfsi->flags |= NFS_INO_INVALID_ATTR;
1099 /* Directories and symlinks: invalidate page cache too */
1100 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1101 nfsi->flags |= NFS_INO_INVALID_DATA;
1102 }
1103 nfsi->cache_change_attribute ++;
1104 atomic_dec(&nfsi->data_updates);
1105}
1106
1107/**
1108 * nfs_end_data_update_defer
1109 * @inode - pointer to inode
1110 * Declare end of the operations that will update file data
1111 * This will defer marking the inode as needing revalidation
1112 * unless there are no other pending updates.
1113 */
1114void nfs_end_data_update_defer(struct inode *inode)
1115{
1116 struct nfs_inode *nfsi = NFS_I(inode);
1117
1118 if (atomic_dec_and_test(&nfsi->data_updates)) {
1119 /* Mark the attribute cache for revalidation */
1120 nfsi->flags |= NFS_INO_INVALID_ATTR;
1121 /* Directories and symlinks: invalidate page cache too */
1122 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1123 nfsi->flags |= NFS_INO_INVALID_DATA;
1124 nfsi->cache_change_attribute ++;
1125 }
1126}
1127
1128/**
1129 * nfs_refresh_inode - verify consistency of the inode attribute cache
1130 * @inode - pointer to inode
1131 * @fattr - updated attributes
1132 *
1133 * Verifies the attribute cache. If we have just changed the attributes,
1134 * so that fattr carries weak cache consistency data, then it may
1135 * also update the ctime/mtime/change_attribute.
1136 */
1137int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1138{
1139 struct nfs_inode *nfsi = NFS_I(inode);
1140 loff_t cur_size, new_isize;
1141 int data_unstable;
1142
1143 /* Do we hold a delegation? */
1144 if (nfs_have_delegation(inode, FMODE_READ))
1145 return 0;
1146
1147 /* Are we in the process of updating data on the server? */
1148 data_unstable = nfs_caches_unstable(inode);
1149
1150 if (fattr->valid & NFS_ATTR_FATTR_V4) {
1151 if ((fattr->valid & NFS_ATTR_PRE_CHANGE) != 0
1152 && nfsi->change_attr == fattr->pre_change_attr)
1153 nfsi->change_attr = fattr->change_attr;
1154 if (!data_unstable && nfsi->change_attr != fattr->change_attr)
1155 nfsi->flags |= NFS_INO_INVALID_ATTR;
1156 }
1157
1158 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1159 return 0;
1160
1161 /* Has the inode gone and changed behind our back? */
1162 if (nfsi->fileid != fattr->fileid
1163 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1164 return -EIO;
1165
1166 cur_size = i_size_read(inode);
1167 new_isize = nfs_size_to_loff_t(fattr->size);
1168
1169 /* If we have atomic WCC data, we may update some attributes */
1170 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1171 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
1172 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1173 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime))
1174 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1175 }
1176
1177 /* Verify a few of the more important attributes */
1178 if (!data_unstable) {
1179 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)
1180 || cur_size != new_isize)
1181 nfsi->flags |= NFS_INO_INVALID_ATTR;
1182 } else if (S_ISREG(inode->i_mode) && new_isize > cur_size)
1183 nfsi->flags |= NFS_INO_INVALID_ATTR;
1184
1185 /* Have any file permissions changed? */
1186 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1187 || inode->i_uid != fattr->uid
1188 || inode->i_gid != fattr->gid)
1189 nfsi->flags |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS;
1190
1191 /* Has the link count changed? */
1192 if (inode->i_nlink != fattr->nlink)
1193 nfsi->flags |= NFS_INO_INVALID_ATTR;
1194
1195 if (!timespec_equal(&inode->i_atime, &fattr->atime))
1196 nfsi->flags |= NFS_INO_INVALID_ATIME;
1197
1198 nfsi->read_cache_jiffies = fattr->timestamp;
1199 return 0;
1200}
1201
1202/*
1203 * Many nfs protocol calls return the new file attributes after
1204 * an operation. Here we update the inode to reflect the state
1205 * of the server's inode.
1206 *
1207 * This is a bit tricky because we have to make sure all dirty pages
1208 * have been sent off to the server before calling invalidate_inode_pages.
1209 * To make sure no other process adds more write requests while we try
1210 * our best to flush them, we make them sleep during the attribute refresh.
1211 *
1212 * A very similar scenario holds for the dir cache.
1213 */
1214static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
1215{
1216 struct nfs_inode *nfsi = NFS_I(inode);
1217 __u64 new_size;
1218 loff_t new_isize;
1219 unsigned int invalid = 0;
1220 loff_t cur_isize;
1221 int data_unstable;
1222
1223 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1224 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1225 atomic_read(&inode->i_count), fattr->valid);
1226
1227 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1228 return 0;
1229
1230 if (nfsi->fileid != fattr->fileid) {
1231 printk(KERN_ERR "%s: inode number mismatch\n"
1232 "expected (%s/0x%Lx), got (%s/0x%Lx)\n",
1233 __FUNCTION__,
1234 inode->i_sb->s_id, (long long)nfsi->fileid,
1235 inode->i_sb->s_id, (long long)fattr->fileid);
1236 goto out_err;
1237 }
1238
1239 /*
1240 * Make sure the inode's type hasn't changed.
1241 */
1242 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1243 goto out_changed;
1244
1245 /*
1246 * Update the read time so we don't revalidate too often.
1247 */
1248 nfsi->read_cache_jiffies = fattr->timestamp;
1249
1250 /* Are we racing with known updates of the metadata on the server? */
1251 data_unstable = ! nfs_verify_change_attribute(inode, verifier);
1252
1253 /* Check if the file size agrees */
1254 new_size = fattr->size;
1255 new_isize = nfs_size_to_loff_t(fattr->size);
1256 cur_isize = i_size_read(inode);
1257 if (cur_isize != new_size) {
1258#ifdef NFS_DEBUG_VERBOSE
1259 printk(KERN_DEBUG "NFS: isize change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1260#endif
1261 /*
1262 * If we have pending writebacks, things can get
1263 * messy.
1264 */
1265 if (S_ISREG(inode->i_mode) && data_unstable) {
1266 if (new_isize > cur_isize) {
1267 inode->i_size = new_isize;
1268 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1269 }
1270 } else {
1271 inode->i_size = new_isize;
1272 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1273 }
1274 }
1275
1276 /*
1277 * Note: we don't check inode->i_mtime since pipes etc.
1278 * can change this value in VFS without requiring a
1279 * cache revalidation.
1280 */
1281 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1282 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1283#ifdef NFS_DEBUG_VERBOSE
1284 printk(KERN_DEBUG "NFS: mtime change on %s/%ld\n", inode->i_sb->s_id, inode->i_ino);
1285#endif
1286 if (!data_unstable)
1287 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1288 }
1289
1290 if ((fattr->valid & NFS_ATTR_FATTR_V4)
1291 && nfsi->change_attr != fattr->change_attr) {
1292#ifdef NFS_DEBUG_VERBOSE
1293 printk(KERN_DEBUG "NFS: change_attr change on %s/%ld\n",
1294 inode->i_sb->s_id, inode->i_ino);
1295#endif
1296 nfsi->change_attr = fattr->change_attr;
1297 if (!data_unstable)
1298 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS;
1299 }
1300
1301 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1302 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1303
1304 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1305 inode->i_uid != fattr->uid ||
1306 inode->i_gid != fattr->gid)
1307 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS;
1308
1309 inode->i_mode = fattr->mode;
1310 inode->i_nlink = fattr->nlink;
1311 inode->i_uid = fattr->uid;
1312 inode->i_gid = fattr->gid;
1313
1314 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1315 /*
1316 * report the blocks in 512byte units
1317 */
1318 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1319 inode->i_blksize = inode->i_sb->s_blocksize;
1320 } else {
1321 inode->i_blocks = fattr->du.nfs2.blocks;
1322 inode->i_blksize = fattr->du.nfs2.blocksize;
1323 }
1324
1325 /* Update attrtimeo value if we're out of the unstable period */
1326 if (invalid & NFS_INO_INVALID_ATTR) {
1327 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1328 nfsi->attrtimeo_timestamp = jiffies;
1329 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1330 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1331 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1332 nfsi->attrtimeo_timestamp = jiffies;
1333 }
1334 /* Don't invalidate the data if we were to blame */
1335 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1336 || S_ISLNK(inode->i_mode)))
1337 invalid &= ~NFS_INO_INVALID_DATA;
1338 if (!nfs_have_delegation(inode, FMODE_READ))
1339 nfsi->flags |= invalid;
1340
1341 return 0;
1342 out_changed:
1343 /*
1344 * Big trouble! The inode has become a different object.
1345 */
1346#ifdef NFS_PARANOIA
1347 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1348 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1349#endif
1350 /*
1351 * No need to worry about unhashing the dentry, as the
1352 * lookup validation will know that the inode is bad.
1353 * (But we fall through to invalidate the caches.)
1354 */
1355 nfs_invalidate_inode(inode);
1356 out_err:
1357 NFS_FLAGS(inode) |= NFS_INO_STALE;
1358 return -ESTALE;
1359}
1360
1361/*
1362 * File system information
1363 */
1364
1365static int nfs_set_super(struct super_block *s, void *data)
1366{
1367 s->s_fs_info = data;
1368 return set_anon_super(s, data);
1369}
1370
1371static int nfs_compare_super(struct super_block *sb, void *data)
1372{
1373 struct nfs_server *server = data;
1374 struct nfs_server *old = NFS_SB(sb);
1375
1376 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1377 return 0;
1378 if (old->addr.sin_port != server->addr.sin_port)
1379 return 0;
1380 return !nfs_compare_fh(&old->fh, &server->fh);
1381}
1382
1383static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1384 int flags, const char *dev_name, void *raw_data)
1385{
1386 int error;
1387 struct nfs_server *server;
1388 struct super_block *s;
1389 struct nfs_fh *root;
1390 struct nfs_mount_data *data = raw_data;
1391
1392 if (!data) {
1393 printk("nfs_read_super: missing data argument\n");
1394 return ERR_PTR(-EINVAL);
1395 }
1396
1397 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1398 if (!server)
1399 return ERR_PTR(-ENOMEM);
1400 memset(server, 0, sizeof(struct nfs_server));
1401 /* Zero out the NFS state stuff */
1402 init_nfsv4_state(server);
1403
1404 if (data->version != NFS_MOUNT_VERSION) {
1405 printk("nfs warning: mount version %s than kernel\n",
1406 data->version < NFS_MOUNT_VERSION ? "older" : "newer");
1407 if (data->version < 2)
1408 data->namlen = 0;
1409 if (data->version < 3)
1410 data->bsize = 0;
1411 if (data->version < 4) {
1412 data->flags &= ~NFS_MOUNT_VER3;
1413 data->root.size = NFS2_FHSIZE;
1414 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1415 }
1416 if (data->version < 5)
1417 data->flags &= ~NFS_MOUNT_SECFLAVOUR;
1418 }
1419
1420 root = &server->fh;
1421 if (data->flags & NFS_MOUNT_VER3)
1422 root->size = data->root.size;
1423 else
1424 root->size = NFS2_FHSIZE;
1425 if (root->size > sizeof(root->data)) {
1426 printk("nfs_get_sb: invalid root filehandle\n");
1427 kfree(server);
1428 return ERR_PTR(-EINVAL);
1429 }
1430 memcpy(root->data, data->root.data, root->size);
1431
1432 /* We now require that the mount process passes the remote address */
1433 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1434 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
1435 printk("NFS: mount program didn't pass remote address!\n");
1436 kfree(server);
1437 return ERR_PTR(-EINVAL);
1438 }
1439
1440 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
1441
1442 if (IS_ERR(s) || s->s_root) {
1443 kfree(server);
1444 return s;
1445 }
1446
1447 s->s_flags = flags;
1448
1449 /* Fire up rpciod if not yet running */
1450 if (rpciod_up() != 0) {
1451 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1452 kfree(server);
1453 return ERR_PTR(-EIO);
1454 }
1455
1456 error = nfs_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1457 if (error) {
1458 up_write(&s->s_umount);
1459 deactivate_super(s);
1460 return ERR_PTR(error);
1461 }
1462 s->s_flags |= MS_ACTIVE;
1463 return s;
1464}
1465
1466static void nfs_kill_super(struct super_block *s)
1467{
1468 struct nfs_server *server = NFS_SB(s);
1469
1470 kill_anon_super(s);
1471
1472 if (server->client != NULL && !IS_ERR(server->client))
1473 rpc_shutdown_client(server->client);
1474 if (server->client_sys != NULL && !IS_ERR(server->client_sys))
1475 rpc_shutdown_client(server->client_sys);
1476
1477 if (!(server->flags & NFS_MOUNT_NONLM))
1478 lockd_down(); /* release rpc.lockd */
1479
1480 rpciod_down(); /* release rpciod */
1481
1482 if (server->hostname != NULL)
1483 kfree(server->hostname);
1484 kfree(server);
1485}
1486
1487static struct file_system_type nfs_fs_type = {
1488 .owner = THIS_MODULE,
1489 .name = "nfs",
1490 .get_sb = nfs_get_sb,
1491 .kill_sb = nfs_kill_super,
1492 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1493};
1494
1495#ifdef CONFIG_NFS_V4
1496
1497static void nfs4_clear_inode(struct inode *);
1498
1499
1500static struct super_operations nfs4_sops = {
1501 .alloc_inode = nfs_alloc_inode,
1502 .destroy_inode = nfs_destroy_inode,
1503 .write_inode = nfs_write_inode,
1504 .delete_inode = nfs_delete_inode,
1505 .statfs = nfs_statfs,
1506 .clear_inode = nfs4_clear_inode,
1507 .umount_begin = nfs_umount_begin,
1508 .show_options = nfs_show_options,
1509};
1510
1511/*
1512 * Clean out any remaining NFSv4 state that might be left over due
1513 * to open() calls that passed nfs_atomic_lookup, but failed to call
1514 * nfs_open().
1515 */
1516static void nfs4_clear_inode(struct inode *inode)
1517{
1518 struct nfs_inode *nfsi = NFS_I(inode);
1519
1520 /* If we are holding a delegation, return it! */
1521 if (nfsi->delegation != NULL)
1522 nfs_inode_return_delegation(inode);
1523 /* First call standard NFS clear_inode() code */
1524 nfs_clear_inode(inode);
1525 /* Now clear out any remaining state */
1526 while (!list_empty(&nfsi->open_states)) {
1527 struct nfs4_state *state;
1528
1529 state = list_entry(nfsi->open_states.next,
1530 struct nfs4_state,
1531 inode_states);
1532 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
1533 __FUNCTION__,
1534 inode->i_sb->s_id,
1535 (long long)NFS_FILEID(inode),
1536 state);
1537 BUG_ON(atomic_read(&state->count) != 1);
1538 nfs4_close_state(state, state->state);
1539 }
1540}
1541
1542
1543static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
1544{
1545 struct nfs_server *server;
1546 struct nfs4_client *clp = NULL;
1547 struct rpc_xprt *xprt = NULL;
1548 struct rpc_clnt *clnt = NULL;
1549 struct rpc_timeout timeparms;
1550 rpc_authflavor_t authflavour;
1551 int proto, err = -EIO;
1552
1553 sb->s_blocksize_bits = 0;
1554 sb->s_blocksize = 0;
1555 server = NFS_SB(sb);
1556 if (data->rsize != 0)
1557 server->rsize = nfs_block_size(data->rsize, NULL);
1558 if (data->wsize != 0)
1559 server->wsize = nfs_block_size(data->wsize, NULL);
1560 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1561 server->caps = NFS_CAP_ATOMIC_OPEN;
1562
1563 server->acregmin = data->acregmin*HZ;
1564 server->acregmax = data->acregmax*HZ;
1565 server->acdirmin = data->acdirmin*HZ;
1566 server->acdirmax = data->acdirmax*HZ;
1567
1568 server->rpc_ops = &nfs_v4_clientops;
1569 /* Initialize timeout values */
1570
1571 timeparms.to_initval = data->timeo * HZ / 10;
1572 timeparms.to_retries = data->retrans;
1573 timeparms.to_exponential = 1;
1574 if (!timeparms.to_retries)
1575 timeparms.to_retries = 5;
1576
1577 proto = data->proto;
1578 /* Which IP protocol do we use? */
1579 switch (proto) {
1580 case IPPROTO_TCP:
1581 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1582 if (!timeparms.to_initval)
1583 timeparms.to_initval = 600 * HZ / 10;
1584 break;
1585 case IPPROTO_UDP:
1586 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1587 if (!timeparms.to_initval)
1588 timeparms.to_initval = 11 * HZ / 10;
1589 break;
1590 default:
1591 return -EINVAL;
1592 }
1593
1594 clp = nfs4_get_client(&server->addr.sin_addr);
1595 if (!clp) {
1596 printk(KERN_WARNING "NFS: failed to create NFS4 client.\n");
1597 return -EIO;
1598 }
1599
1600 /* Now create transport and client */
1601 authflavour = RPC_AUTH_UNIX;
1602 if (data->auth_flavourlen != 0) {
1603 if (data->auth_flavourlen > 1)
1604 printk(KERN_INFO "NFS: cannot yet deal with multiple auth flavours.\n");
1605 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
1606 err = -EFAULT;
1607 goto out_fail;
1608 }
1609 }
1610
1611 down_write(&clp->cl_sem);
1612 if (clp->cl_rpcclient == NULL) {
1613 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
1614 if (IS_ERR(xprt)) {
1615 up_write(&clp->cl_sem);
1616 printk(KERN_WARNING "NFS: cannot create RPC transport.\n");
1617 err = PTR_ERR(xprt);
1618 goto out_fail;
1619 }
1620 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
1621 server->rpc_ops->version, authflavour);
1622 if (IS_ERR(clnt)) {
1623 up_write(&clp->cl_sem);
1624 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1da177e4
LT
1625 err = PTR_ERR(clnt);
1626 goto out_fail;
1627 }
1628 clnt->cl_intr = 1;
1629 clnt->cl_softrtry = 1;
1630 clnt->cl_chatty = 1;
1631 clp->cl_rpcclient = clnt;
1632 clp->cl_cred = rpcauth_lookupcred(clnt->cl_auth, 0);
1633 if (IS_ERR(clp->cl_cred)) {
1634 up_write(&clp->cl_sem);
1635 err = PTR_ERR(clp->cl_cred);
1636 clp->cl_cred = NULL;
1637 goto out_fail;
1638 }
1639 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
1640 nfs_idmap_new(clp);
1641 }
1642 if (list_empty(&clp->cl_superblocks)) {
1643 err = nfs4_init_client(clp);
1644 if (err != 0) {
1645 up_write(&clp->cl_sem);
1646 goto out_fail;
1647 }
1648 }
1649 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
1650 clnt = rpc_clone_client(clp->cl_rpcclient);
1651 if (!IS_ERR(clnt))
1652 server->nfs4_state = clp;
1653 up_write(&clp->cl_sem);
1654 clp = NULL;
1655
1656 if (IS_ERR(clnt)) {
1657 printk(KERN_WARNING "NFS: cannot create RPC client.\n");
1658 return PTR_ERR(clnt);
1659 }
1660
1661 server->client = clnt;
1662
1663 if (server->nfs4_state->cl_idmap == NULL) {
1664 printk(KERN_WARNING "NFS: failed to create idmapper.\n");
1665 return -ENOMEM;
1666 }
1667
1668 if (clnt->cl_auth->au_flavor != authflavour) {
1669 if (rpcauth_create(authflavour, clnt) == NULL) {
1670 printk(KERN_WARNING "NFS: couldn't create credcache!\n");
1671 return -ENOMEM;
1672 }
1673 }
1674
1675 sb->s_time_gran = 1;
1676
1677 sb->s_op = &nfs4_sops;
1678 err = nfs_sb_init(sb, authflavour);
1679 if (err == 0)
1680 return 0;
1681out_fail:
1682 if (clp)
1683 nfs4_put_client(clp);
1684 return err;
1685}
1686
1687static int nfs4_compare_super(struct super_block *sb, void *data)
1688{
1689 struct nfs_server *server = data;
1690 struct nfs_server *old = NFS_SB(sb);
1691
1692 if (strcmp(server->hostname, old->hostname) != 0)
1693 return 0;
1694 if (strcmp(server->mnt_path, old->mnt_path) != 0)
1695 return 0;
1696 return 1;
1697}
1698
1699static void *
1700nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
1701{
1702 void *p = NULL;
1703
1704 if (!src->len)
1705 return ERR_PTR(-EINVAL);
1706 if (src->len < maxlen)
1707 maxlen = src->len;
1708 if (dst == NULL) {
1709 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
1710 if (p == NULL)
1711 return ERR_PTR(-ENOMEM);
1712 }
1713 if (copy_from_user(dst, src->data, maxlen)) {
1714 if (p != NULL)
1715 kfree(p);
1716 return ERR_PTR(-EFAULT);
1717 }
1718 dst[maxlen] = '\0';
1719 return dst;
1720}
1721
1722static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
1723 int flags, const char *dev_name, void *raw_data)
1724{
1725 int error;
1726 struct nfs_server *server;
1727 struct super_block *s;
1728 struct nfs4_mount_data *data = raw_data;
1729 void *p;
1730
1731 if (!data) {
1732 printk("nfs_read_super: missing data argument\n");
1733 return ERR_PTR(-EINVAL);
1734 }
1735
1736 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1737 if (!server)
1738 return ERR_PTR(-ENOMEM);
1739 memset(server, 0, sizeof(struct nfs_server));
1740 /* Zero out the NFS state stuff */
1741 init_nfsv4_state(server);
1742
1743 if (data->version != NFS4_MOUNT_VERSION) {
1744 printk("nfs warning: mount version %s than kernel\n",
1745 data->version < NFS4_MOUNT_VERSION ? "older" : "newer");
1746 }
1747
1748 p = nfs_copy_user_string(NULL, &data->hostname, 256);
1749 if (IS_ERR(p))
1750 goto out_err;
1751 server->hostname = p;
1752
1753 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
1754 if (IS_ERR(p))
1755 goto out_err;
1756 server->mnt_path = p;
1757
1758 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
1759 sizeof(server->ip_addr) - 1);
1760 if (IS_ERR(p))
1761 goto out_err;
1762
1763 /* We now require that the mount process passes the remote address */
1764 if (data->host_addrlen != sizeof(server->addr)) {
1765 s = ERR_PTR(-EINVAL);
1766 goto out_free;
1767 }
1768 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
1769 s = ERR_PTR(-EFAULT);
1770 goto out_free;
1771 }
1772 if (server->addr.sin_family != AF_INET ||
1773 server->addr.sin_addr.s_addr == INADDR_ANY) {
1774 printk("NFS: mount program didn't pass remote IP address!\n");
1775 s = ERR_PTR(-EINVAL);
1776 goto out_free;
1777 }
1778
1779 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
1780
1781 if (IS_ERR(s) || s->s_root)
1782 goto out_free;
1783
1784 s->s_flags = flags;
1785
1786 /* Fire up rpciod if not yet running */
1787 if (rpciod_up() != 0) {
1788 printk(KERN_WARNING "NFS: couldn't start rpciod!\n");
1789 s = ERR_PTR(-EIO);
1790 goto out_free;
1791 }
1792
1793 error = nfs4_fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
1794 if (error) {
1795 up_write(&s->s_umount);
1796 deactivate_super(s);
1797 return ERR_PTR(error);
1798 }
1799 s->s_flags |= MS_ACTIVE;
1800 return s;
1801out_err:
1802 s = (struct super_block *)p;
1803out_free:
1804 if (server->mnt_path)
1805 kfree(server->mnt_path);
1806 if (server->hostname)
1807 kfree(server->hostname);
1808 kfree(server);
1809 return s;
1810}
1811
1812static void nfs4_kill_super(struct super_block *sb)
1813{
1814 struct nfs_server *server = NFS_SB(sb);
1815
1816 nfs_return_all_delegations(sb);
1817 kill_anon_super(sb);
1818
1819 nfs4_renewd_prepare_shutdown(server);
1820
1821 if (server->client != NULL && !IS_ERR(server->client))
1822 rpc_shutdown_client(server->client);
1823 rpciod_down(); /* release rpciod */
1824
1825 destroy_nfsv4_state(server);
1826
1827 if (server->hostname != NULL)
1828 kfree(server->hostname);
1829 kfree(server);
1830}
1831
1832static struct file_system_type nfs4_fs_type = {
1833 .owner = THIS_MODULE,
1834 .name = "nfs4",
1835 .get_sb = nfs4_get_sb,
1836 .kill_sb = nfs4_kill_super,
1837 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1838};
1839
1840#define nfs4_init_once(nfsi) \
1841 do { \
1842 INIT_LIST_HEAD(&(nfsi)->open_states); \
1843 nfsi->delegation = NULL; \
1844 nfsi->delegation_state = 0; \
1845 init_rwsem(&nfsi->rwsem); \
1846 } while(0)
1847#define register_nfs4fs() register_filesystem(&nfs4_fs_type)
1848#define unregister_nfs4fs() unregister_filesystem(&nfs4_fs_type)
1849#else
1850#define nfs4_init_once(nfsi) \
1851 do { } while (0)
1852#define register_nfs4fs() (0)
1853#define unregister_nfs4fs()
1854#endif
1855
1856extern int nfs_init_nfspagecache(void);
1857extern void nfs_destroy_nfspagecache(void);
1858extern int nfs_init_readpagecache(void);
1859extern void nfs_destroy_readpagecache(void);
1860extern int nfs_init_writepagecache(void);
1861extern void nfs_destroy_writepagecache(void);
1862#ifdef CONFIG_NFS_DIRECTIO
1863extern int nfs_init_directcache(void);
1864extern void nfs_destroy_directcache(void);
1865#endif
1866
1867static kmem_cache_t * nfs_inode_cachep;
1868
1869static struct inode *nfs_alloc_inode(struct super_block *sb)
1870{
1871 struct nfs_inode *nfsi;
1872 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
1873 if (!nfsi)
1874 return NULL;
1875 nfsi->flags = 0;
1876 return &nfsi->vfs_inode;
1877}
1878
1879static void nfs_destroy_inode(struct inode *inode)
1880{
1881 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1882}
1883
1884static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1885{
1886 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1887
1888 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1889 SLAB_CTOR_CONSTRUCTOR) {
1890 inode_init_once(&nfsi->vfs_inode);
1891 spin_lock_init(&nfsi->req_lock);
1892 INIT_LIST_HEAD(&nfsi->dirty);
1893 INIT_LIST_HEAD(&nfsi->commit);
1894 INIT_LIST_HEAD(&nfsi->open_files);
1895 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1896 atomic_set(&nfsi->data_updates, 0);
1897 nfsi->ndirty = 0;
1898 nfsi->ncommit = 0;
1899 nfsi->npages = 0;
1900 init_waitqueue_head(&nfsi->nfs_i_wait);
1901 nfs4_init_once(nfsi);
1902 }
1903}
1904
75c96f85 1905static int nfs_init_inodecache(void)
1da177e4
LT
1906{
1907 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1908 sizeof(struct nfs_inode),
1909 0, SLAB_RECLAIM_ACCOUNT,
1910 init_once, NULL);
1911 if (nfs_inode_cachep == NULL)
1912 return -ENOMEM;
1913
1914 return 0;
1915}
1916
75c96f85 1917static void nfs_destroy_inodecache(void)
1da177e4
LT
1918{
1919 if (kmem_cache_destroy(nfs_inode_cachep))
1920 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
1921}
1922
1923/*
1924 * Initialize NFS
1925 */
1926static int __init init_nfs_fs(void)
1927{
1928 int err;
1929
1930 err = nfs_init_nfspagecache();
1931 if (err)
1932 goto out4;
1933
1934 err = nfs_init_inodecache();
1935 if (err)
1936 goto out3;
1937
1938 err = nfs_init_readpagecache();
1939 if (err)
1940 goto out2;
1941
1942 err = nfs_init_writepagecache();
1943 if (err)
1944 goto out1;
1945
1946#ifdef CONFIG_NFS_DIRECTIO
1947 err = nfs_init_directcache();
1948 if (err)
1949 goto out0;
1950#endif
1951
1952#ifdef CONFIG_PROC_FS
1953 rpc_proc_register(&nfs_rpcstat);
1954#endif
1955 err = register_filesystem(&nfs_fs_type);
1956 if (err)
1957 goto out;
1958 if ((err = register_nfs4fs()) != 0)
1959 goto out;
1960 return 0;
1961out:
1962#ifdef CONFIG_PROC_FS
1963 rpc_proc_unregister("nfs");
1964#endif
1965 nfs_destroy_writepagecache();
1966#ifdef CONFIG_NFS_DIRECTIO
1967out0:
1968 nfs_destroy_directcache();
1969#endif
1970out1:
1971 nfs_destroy_readpagecache();
1972out2:
1973 nfs_destroy_inodecache();
1974out3:
1975 nfs_destroy_nfspagecache();
1976out4:
1977 return err;
1978}
1979
1980static void __exit exit_nfs_fs(void)
1981{
1982#ifdef CONFIG_NFS_DIRECTIO
1983 nfs_destroy_directcache();
1984#endif
1985 nfs_destroy_writepagecache();
1986 nfs_destroy_readpagecache();
1987 nfs_destroy_inodecache();
1988 nfs_destroy_nfspagecache();
1989#ifdef CONFIG_PROC_FS
1990 rpc_proc_unregister("nfs");
1991#endif
1992 unregister_filesystem(&nfs_fs_type);
1993 unregister_nfs4fs();
1994}
1995
1996/* Not quite true; I just maintain it */
1997MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1998MODULE_LICENSE("GPL");
1999
2000module_init(init_nfs_fs)
2001module_exit(exit_nfs_fs)