blob: 47167ab64f5ba4595320118420125e4fe1968f9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Chuck Lever4ece3a22006-03-20 13:44:22 -050029#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/nfs_fs.h>
31#include <linux/nfs_mount.h>
32#include <linux/nfs4_mount.h>
33#include <linux/lockd/bind.h>
34#include <linux/smp_lock.h>
35#include <linux/seq_file.h>
36#include <linux/mount.h>
37#include <linux/nfs_idmap.h>
38#include <linux/vfs.h>
39
40#include <asm/system.h>
41#include <asm/uaccess.h>
42
Trond Myklebust4ce79712005-06-22 17:16:21 +000043#include "nfs4_fs.h"
Trond Myklebusta72b4422006-01-03 09:55:41 +010044#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "delegation.h"
Chuck Leverd9ef5a82006-03-20 13:44:13 -050046#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define NFSDBG_FACILITY NFSDBG_VFS
49#define NFS_PARANOIA 1
50
51/* Maximum number of readahead requests
52 * FIXME: this should really be a sysctl so that users may tune it to suit
53 * their needs. People that do NFS over a slow network, might for
54 * instance want to reduce it to something closer to 1 for improved
55 * interactive response.
56 */
57#define NFS_MAX_READAHEAD (RPC_DEF_SLOT_TABLE - 1)
58
59static void nfs_invalidate_inode(struct inode *);
Trond Myklebust24aa1fe2005-12-03 15:20:07 -050060static int nfs_update_inode(struct inode *, struct nfs_fattr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static struct inode *nfs_alloc_inode(struct super_block *sb);
63static void nfs_destroy_inode(struct inode *);
64static int nfs_write_inode(struct inode *,int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void nfs_clear_inode(struct inode *);
Trond Myklebust8b512d92006-06-09 09:34:18 -040066static void nfs_umount_begin(struct vfsmount *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int nfs_statfs(struct super_block *, struct kstatfs *);
68static int nfs_show_options(struct seq_file *, struct vfsmount *);
Chuck Leverd9ef5a82006-03-20 13:44:13 -050069static int nfs_show_stats(struct seq_file *, struct vfsmount *);
Trond Myklebustada70d92005-06-22 17:16:22 +000070static void nfs_zap_acl_cache(struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static struct rpc_program nfs_program;
73
74static struct super_operations nfs_sops = {
75 .alloc_inode = nfs_alloc_inode,
76 .destroy_inode = nfs_destroy_inode,
77 .write_inode = nfs_write_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .statfs = nfs_statfs,
79 .clear_inode = nfs_clear_inode,
80 .umount_begin = nfs_umount_begin,
81 .show_options = nfs_show_options,
Chuck Leverd9ef5a82006-03-20 13:44:13 -050082 .show_stats = nfs_show_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85/*
86 * RPC cruft for NFS
87 */
88static struct rpc_stat nfs_rpcstat = {
89 .program = &nfs_program
90};
91static struct rpc_version * nfs_version[] = {
92 NULL,
93 NULL,
94 &nfs_version2,
95#if defined(CONFIG_NFS_V3)
96 &nfs_version3,
97#elif defined(CONFIG_NFS_V4)
98 NULL,
99#endif
100#if defined(CONFIG_NFS_V4)
101 &nfs_version4,
102#endif
103};
104
105static struct rpc_program nfs_program = {
106 .name = "nfs",
107 .number = NFS_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800108 .nrvers = ARRAY_SIZE(nfs_version),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .version = nfs_version,
110 .stats = &nfs_rpcstat,
111 .pipe_dir_name = "/nfs",
112};
113
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000114#ifdef CONFIG_NFS_V3_ACL
115static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
116static struct rpc_version * nfsacl_version[] = {
117 [3] = &nfsacl_version3,
118};
119
120struct rpc_program nfsacl_program = {
121 .name = "nfsacl",
122 .number = NFS_ACL_PROGRAM,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800123 .nrvers = ARRAY_SIZE(nfsacl_version),
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000124 .version = nfsacl_version,
125 .stats = &nfsacl_rpcstat,
126};
127#endif /* CONFIG_NFS_V3_ACL */
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static inline unsigned long
130nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
131{
132 return nfs_fileid_to_ino_t(fattr->fileid);
133}
134
135static int
136nfs_write_inode(struct inode *inode, int sync)
137{
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500138 int flags = sync ? FLUSH_SYNC : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int ret;
140
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000141 ret = nfs_commit_inode(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (ret < 0)
143 return ret;
144 return 0;
145}
146
147static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148nfs_clear_inode(struct inode *inode)
149{
150 struct nfs_inode *nfsi = NFS_I(inode);
151 struct rpc_cred *cred;
152
Trond Myklebustda6d5032006-06-01 17:26:35 -0400153 /*
154 * The following should never happen...
155 */
156 BUG_ON(nfs_have_writebacks(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 BUG_ON (!list_empty(&nfsi->open_files));
Trond Myklebustada70d92005-06-22 17:16:22 +0000158 nfs_zap_acl_cache(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 cred = nfsi->cache_access.cred;
160 if (cred)
161 put_rpccred(cred);
162 BUG_ON(atomic_read(&nfsi->data_updates) != 0);
163}
164
Trond Myklebust8b512d92006-06-09 09:34:18 -0400165static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Trond Myklebust8b512d92006-06-09 09:34:18 -0400167 struct nfs_server *server;
168 struct rpc_clnt *rpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Trond Myklebust8b512d92006-06-09 09:34:18 -0400170 if (!(flags & MNT_FORCE))
171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* -EIO all pending I/O */
Trond Myklebust8b512d92006-06-09 09:34:18 -0400173 server = NFS_SB(vfsmnt->mnt_sb);
174 rpc = server->client;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000175 if (!IS_ERR(rpc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 rpc_killall_tasks(rpc);
Trond Myklebust8b512d92006-06-09 09:34:18 -0400177 rpc = server->client_acl;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000178 if (!IS_ERR(rpc))
179 rpc_killall_tasks(rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182
183static inline unsigned long
184nfs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
185{
186 /* make sure blocksize is a power of two */
187 if ((bsize & (bsize - 1)) || nrbitsp) {
188 unsigned char nrbits;
189
190 for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
191 ;
192 bsize = 1 << nrbits;
193 if (nrbitsp)
194 *nrbitsp = nrbits;
195 }
196
197 return bsize;
198}
199
200/*
201 * Calculate the number of 512byte blocks used.
202 */
203static inline unsigned long
204nfs_calc_block_size(u64 tsize)
205{
206 loff_t used = (tsize + 511) >> 9;
207 return (used > ULONG_MAX) ? ULONG_MAX : used;
208}
209
210/*
211 * Compute and set NFS server blocksize
212 */
213static inline unsigned long
214nfs_block_size(unsigned long bsize, unsigned char *nrbitsp)
215{
Chuck Lever40859d72005-11-30 18:09:02 -0500216 if (bsize < NFS_MIN_FILE_IO_SIZE)
217 bsize = NFS_DEF_FILE_IO_SIZE;
218 else if (bsize >= NFS_MAX_FILE_IO_SIZE)
219 bsize = NFS_MAX_FILE_IO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return nfs_block_bits(bsize, nrbitsp);
222}
223
Trond Myklebust55a97592006-06-09 09:34:19 -0400224static inline void
225nfs_super_set_maxbytes(struct super_block *sb, __u64 maxfilesize)
226{
227 sb->s_maxbytes = (loff_t)maxfilesize;
228 if (sb->s_maxbytes > MAX_LFS_FILESIZE || sb->s_maxbytes <= 0)
229 sb->s_maxbytes = MAX_LFS_FILESIZE;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * Obtain the root inode of the file system.
234 */
235static struct inode *
236nfs_get_root(struct super_block *sb, struct nfs_fh *rootfh, struct nfs_fsinfo *fsinfo)
237{
238 struct nfs_server *server = NFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int error;
240
241 error = server->rpc_ops->getroot(server, rootfh, fsinfo);
242 if (error < 0) {
243 dprintk("nfs_get_root: getattr error = %d\n", -error);
244 return ERR_PTR(error);
245 }
246
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -0400247 server->fsid = fsinfo->fattr->fsid;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500248 return nfs_fhget(sb, rootfh, fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * Do NFS version-independent mount processing, and sanity checking
253 */
254static int
255nfs_sb_init(struct super_block *sb, rpc_authflavor_t authflavor)
256{
257 struct nfs_server *server;
258 struct inode *root_inode;
259 struct nfs_fattr fattr;
260 struct nfs_fsinfo fsinfo = {
261 .fattr = &fattr,
262 };
263 struct nfs_pathconf pathinfo = {
264 .fattr = &fattr,
265 };
266 int no_root_error = 0;
267 unsigned long max_rpc_payload;
268
269 /* We probably want something more informative here */
270 snprintf(sb->s_id, sizeof(sb->s_id), "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
271
272 server = NFS_SB(sb);
273
274 sb->s_magic = NFS_SUPER_MAGIC;
275
Trond Myklebust01d0ae82006-03-20 13:44:48 -0500276 server->io_stats = nfs_alloc_iostats();
277 if (server->io_stats == NULL)
278 return -ENOMEM;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 root_inode = nfs_get_root(sb, &server->fh, &fsinfo);
281 /* Did getting the root inode fail? */
282 if (IS_ERR(root_inode)) {
283 no_root_error = PTR_ERR(root_inode);
284 goto out_no_root;
285 }
286 sb->s_root = d_alloc_root(root_inode);
287 if (!sb->s_root) {
288 no_root_error = -ENOMEM;
289 goto out_no_root;
290 }
291 sb->s_root->d_op = server->rpc_ops->dentry_ops;
292
Chuck Lever67ec9f42006-03-20 13:44:15 -0500293 /* mount time stamp, in seconds */
294 server->mount_time = jiffies;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Get some general file system info */
297 if (server->namelen == 0 &&
298 server->rpc_ops->pathconf(server, &server->fh, &pathinfo) >= 0)
299 server->namelen = pathinfo.max_namelen;
300 /* Work out a lot of parameters */
301 if (server->rsize == 0)
302 server->rsize = nfs_block_size(fsinfo.rtpref, NULL);
303 if (server->wsize == 0)
304 server->wsize = nfs_block_size(fsinfo.wtpref, NULL);
305
306 if (fsinfo.rtmax >= 512 && server->rsize > fsinfo.rtmax)
307 server->rsize = nfs_block_size(fsinfo.rtmax, NULL);
308 if (fsinfo.wtmax >= 512 && server->wsize > fsinfo.wtmax)
309 server->wsize = nfs_block_size(fsinfo.wtmax, NULL);
310
311 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
312 if (server->rsize > max_rpc_payload)
313 server->rsize = max_rpc_payload;
Chuck Lever40859d72005-11-30 18:09:02 -0500314 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
315 server->rsize = NFS_MAX_FILE_IO_SIZE;
316 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (server->wsize > max_rpc_payload)
319 server->wsize = max_rpc_payload;
Chuck Lever40859d72005-11-30 18:09:02 -0500320 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
321 server->wsize = NFS_MAX_FILE_IO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (sb->s_blocksize == 0)
325 sb->s_blocksize = nfs_block_bits(server->wsize,
326 &sb->s_blocksize_bits);
327 server->wtmult = nfs_block_bits(fsinfo.wtmult, NULL);
328
329 server->dtsize = nfs_block_size(fsinfo.dtpref, NULL);
330 if (server->dtsize > PAGE_CACHE_SIZE)
331 server->dtsize = PAGE_CACHE_SIZE;
332 if (server->dtsize > server->rsize)
333 server->dtsize = server->rsize;
334
335 if (server->flags & NFS_MOUNT_NOAC) {
336 server->acregmin = server->acregmax = 0;
337 server->acdirmin = server->acdirmax = 0;
338 sb->s_flags |= MS_SYNCHRONOUS;
339 }
340 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
341
Trond Myklebust55a97592006-06-09 09:34:19 -0400342 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 server->client->cl_intr = (server->flags & NFS_MOUNT_INTR) ? 1 : 0;
345 server->client->cl_softrtry = (server->flags & NFS_MOUNT_SOFT) ? 1 : 0;
346
347 /* We're airborne Set socket buffersize */
348 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
349 return 0;
350 /* Yargs. It didn't work out. */
351out_no_root:
352 dprintk("nfs_sb_init: get root inode failed: errno %d\n", -no_root_error);
353 if (!IS_ERR(root_inode))
354 iput(root_inode);
355 return no_root_error;
356}
357
Chuck Levereab5c082005-08-11 16:25:14 -0400358static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
359{
360 to->to_initval = timeo * HZ / 10;
361 to->to_retries = retrans;
362 if (!to->to_retries)
363 to->to_retries = 2;
364
365 switch (proto) {
366 case IPPROTO_TCP:
367 if (!to->to_initval)
368 to->to_initval = 60 * HZ;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700369 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
370 to->to_initval = NFS_MAX_TCP_TIMEOUT;
Chuck Levereab5c082005-08-11 16:25:14 -0400371 to->to_increment = to->to_initval;
372 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
373 to->to_exponential = 0;
374 break;
375 case IPPROTO_UDP:
376 default:
377 if (!to->to_initval)
378 to->to_initval = 11 * HZ / 10;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700379 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
380 to->to_initval = NFS_MAX_UDP_TIMEOUT;
381 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
Chuck Levereab5c082005-08-11 16:25:14 -0400382 to->to_exponential = 1;
383 break;
384 }
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * Create an RPC client handle.
389 */
390static struct rpc_clnt *
391nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
392{
393 struct rpc_timeout timeparms;
394 struct rpc_xprt *xprt = NULL;
395 struct rpc_clnt *clnt = NULL;
Chuck Levereab5c082005-08-11 16:25:14 -0400396 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Chuck Levereab5c082005-08-11 16:25:14 -0400398 nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Chuck Lever7a480e22006-03-20 13:44:12 -0500400 server->retrans_timeo = timeparms.to_initval;
401 server->retrans_count = timeparms.to_retries;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* create transport and client */
Chuck Levereab5c082005-08-11 16:25:14 -0400404 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (IS_ERR(xprt)) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +0000406 dprintk("%s: cannot create RPC transport. Error = %ld\n",
407 __FUNCTION__, PTR_ERR(xprt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return (struct rpc_clnt *)xprt;
409 }
410 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
411 server->rpc_ops->version, data->pseudoflavor);
412 if (IS_ERR(clnt)) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +0000413 dprintk("%s: cannot create RPC client. Error = %ld\n",
414 __FUNCTION__, PTR_ERR(xprt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto out_fail;
416 }
417
418 clnt->cl_intr = 1;
419 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 return clnt;
422
423out_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return clnt;
425}
426
427/*
428 * The way this works is that the mount process passes a structure
429 * in the data argument which contains the server's IP address
430 * and the root file handle obtained from the server's mount
431 * daemon. We stash these away in the private superblock fields.
432 */
433static int
434nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data, int silent)
435{
436 struct nfs_server *server;
437 rpc_authflavor_t authflavor;
438
439 server = NFS_SB(sb);
440 sb->s_blocksize_bits = 0;
441 sb->s_blocksize = 0;
442 if (data->bsize)
443 sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
444 if (data->rsize)
445 server->rsize = nfs_block_size(data->rsize, NULL);
446 if (data->wsize)
447 server->wsize = nfs_block_size(data->wsize, NULL);
448 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
449
450 server->acregmin = data->acregmin*HZ;
451 server->acregmax = data->acregmax*HZ;
452 server->acdirmin = data->acdirmin*HZ;
453 server->acdirmax = data->acdirmax*HZ;
454
455 /* Start lockd here, before we might error out */
456 if (!(server->flags & NFS_MOUNT_NONLM))
457 lockd_up();
458
459 server->namelen = data->namlen;
460 server->hostname = kmalloc(strlen(data->hostname) + 1, GFP_KERNEL);
461 if (!server->hostname)
462 return -ENOMEM;
463 strcpy(server->hostname, data->hostname);
464
465 /* Check NFS protocol revision and initialize RPC op vector
466 * and file handle pool. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#ifdef CONFIG_NFS_V3
Trond Myklebust9085bbc2005-06-22 17:16:20 +0000468 if (server->flags & NFS_MOUNT_VER3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 server->rpc_ops = &nfs_v3_clientops;
470 server->caps |= NFS_CAP_READDIRPLUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
472 server->rpc_ops = &nfs_v2_clientops;
473 }
Trond Myklebust9085bbc2005-06-22 17:16:20 +0000474#else
475 server->rpc_ops = &nfs_v2_clientops;
476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* Fill in pseudoflavor for mount version < 5 */
479 if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
480 data->pseudoflavor = RPC_AUTH_UNIX;
481 authflavor = data->pseudoflavor; /* save for sb_init() */
482 /* XXX maybe we want to add a server->pseudoflavor field */
483
484 /* Create RPC client handles */
485 server->client = nfs_create_client(server, data);
486 if (IS_ERR(server->client))
487 return PTR_ERR(server->client);
488 /* RFC 2623, sec 2.3.2 */
489 if (authflavor != RPC_AUTH_UNIX) {
J. Bruce Fields6a192752005-06-22 17:16:23 +0000490 struct rpc_auth *auth;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 server->client_sys = rpc_clone_client(server->client);
493 if (IS_ERR(server->client_sys))
494 return PTR_ERR(server->client_sys);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000495 auth = rpcauth_create(RPC_AUTH_UNIX, server->client_sys);
496 if (IS_ERR(auth))
497 return PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else {
499 atomic_inc(&server->client->cl_count);
500 server->client_sys = server->client;
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (server->flags & NFS_MOUNT_VER3) {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000503#ifdef CONFIG_NFS_V3_ACL
504 if (!(server->flags & NFS_MOUNT_NOACL)) {
505 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
506 /* No errors! Assume that Sun nfsacls are supported */
507 if (!IS_ERR(server->client_acl))
508 server->caps |= NFS_CAP_ACLS;
509 }
510#else
511 server->flags &= ~NFS_MOUNT_NOACL;
512#endif /* CONFIG_NFS_V3_ACL */
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000513 /*
514 * The VFS shouldn't apply the umask to mode bits. We will
515 * do so ourselves when necessary.
516 */
517 sb->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
519 server->namelen = NFS3_MAXNAMLEN;
520 sb->s_time_gran = 1;
521 } else {
522 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
523 server->namelen = NFS2_MAXNAMLEN;
524 }
525
526 sb->s_op = &nfs_sops;
527 return nfs_sb_init(sb, authflavor);
528}
529
530static int
531nfs_statfs(struct super_block *sb, struct kstatfs *buf)
532{
533 struct nfs_server *server = NFS_SB(sb);
534 unsigned char blockbits;
535 unsigned long blockres;
536 struct nfs_fh *rootfh = NFS_FH(sb->s_root->d_inode);
537 struct nfs_fattr fattr;
538 struct nfs_fsstat res = {
539 .fattr = &fattr,
540 };
541 int error;
542
543 lock_kernel();
544
545 error = server->rpc_ops->statfs(server, rootfh, &res);
546 buf->f_type = NFS_SUPER_MAGIC;
547 if (error < 0)
548 goto out_err;
549
550 /*
551 * Current versions of glibc do not correctly handle the
552 * case where f_frsize != f_bsize. Eventually we want to
553 * report the value of wtmult in this field.
554 */
555 buf->f_frsize = sb->s_blocksize;
556
557 /*
558 * On most *nix systems, f_blocks, f_bfree, and f_bavail
559 * are reported in units of f_frsize. Linux hasn't had
560 * an f_frsize field in its statfs struct until recently,
561 * thus historically Linux's sys_statfs reports these
562 * fields in units of f_bsize.
563 */
564 buf->f_bsize = sb->s_blocksize;
565 blockbits = sb->s_blocksize_bits;
566 blockres = (1 << blockbits) - 1;
567 buf->f_blocks = (res.tbytes + blockres) >> blockbits;
568 buf->f_bfree = (res.fbytes + blockres) >> blockbits;
569 buf->f_bavail = (res.abytes + blockres) >> blockbits;
570
571 buf->f_files = res.tfiles;
572 buf->f_ffree = res.afiles;
573
574 buf->f_namelen = server->namelen;
575 out:
576 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578
579 out_err:
Chuck Leverdc20f802005-11-30 18:08:57 -0500580 dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1;
582 goto out;
583
584}
585
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500586static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 static struct proc_nfs_info {
589 int flag;
590 char *str;
591 char *nostr;
592 } nfs_info[] = {
593 { NFS_MOUNT_SOFT, ",soft", ",hard" },
594 { NFS_MOUNT_INTR, ",intr", "" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 { NFS_MOUNT_NOCTO, ",nocto", "" },
596 { NFS_MOUNT_NOAC, ",noac", "" },
Chuck Leverc8bded92006-03-20 13:44:13 -0500597 { NFS_MOUNT_NONLM, ",nolock", "" },
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000598 { NFS_MOUNT_NOACL, ",noacl", "" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 { 0, NULL, NULL }
600 };
601 struct proc_nfs_info *nfs_infop;
Trond Myklebust3063d8a2005-08-25 16:25:57 -0700602 char buf[12];
603 char *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Chuck Leverc8bded92006-03-20 13:44:13 -0500605 seq_printf(m, ",vers=%d", nfss->rpc_ops->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 seq_printf(m, ",rsize=%d", nfss->rsize);
607 seq_printf(m, ",wsize=%d", nfss->wsize);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500608 if (nfss->acregmin != 3*HZ || showdefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500610 if (nfss->acregmax != 60*HZ || showdefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500612 if (nfss->acdirmin != 30*HZ || showdefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500614 if (nfss->acdirmax != 60*HZ || showdefaults)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
616 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
617 if (nfss->flags & nfs_infop->flag)
618 seq_puts(m, nfs_infop->str);
619 else
620 seq_puts(m, nfs_infop->nostr);
621 }
Trond Myklebust3063d8a2005-08-25 16:25:57 -0700622 switch (nfss->client->cl_xprt->prot) {
623 case IPPROTO_TCP:
624 proto = "tcp";
625 break;
626 case IPPROTO_UDP:
627 proto = "udp";
628 break;
629 default:
630 snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
631 proto = buf;
632 }
633 seq_printf(m, ",proto=%s", proto);
Chuck Lever7a480e22006-03-20 13:44:12 -0500634 seq_printf(m, ",timeo=%lu", 10U * nfss->retrans_timeo / HZ);
635 seq_printf(m, ",retrans=%u", nfss->retrans_count);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500636}
637
638static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
639{
640 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
641
642 nfs_show_mount_options(m, nfss, 0);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 seq_puts(m, ",addr=");
645 seq_escape(m, nfss->hostname, " \t\n\\");
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500646
647 return 0;
648}
649
650static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
651{
652 int i, cpu;
653 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
654 struct rpc_auth *auth = nfss->client->cl_auth;
655 struct nfs_iostats totals = { };
656
657 seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
658
659 /*
660 * Display all mount option settings
661 */
662 seq_printf(m, "\n\topts:\t");
663 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
664 seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
665 seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
666 seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
667 nfs_show_mount_options(m, nfss, 1);
668
Chuck Lever67ec9f42006-03-20 13:44:15 -0500669 seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
670
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500671 seq_printf(m, "\n\tcaps:\t");
672 seq_printf(m, "caps=0x%x", nfss->caps);
673 seq_printf(m, ",wtmult=%d", nfss->wtmult);
674 seq_printf(m, ",dtsize=%d", nfss->dtsize);
675 seq_printf(m, ",bsize=%d", nfss->bsize);
676 seq_printf(m, ",namelen=%d", nfss->namelen);
677
678#ifdef CONFIG_NFS_V4
679 if (nfss->rpc_ops->version == 4) {
680 seq_printf(m, "\n\tnfsv4:\t");
681 seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
682 seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
683 seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
684 }
685#endif
686
687 /*
688 * Display security flavor in effect for this mount
689 */
690 seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
691 if (auth->au_flavor)
692 seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
693
694 /*
695 * Display superblock I/O counters
696 */
John Hawkesb9d95062006-04-19 13:06:20 -0400697 for_each_possible_cpu(cpu) {
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500698 struct nfs_iostats *stats;
699
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500700 preempt_disable();
701 stats = per_cpu_ptr(nfss->io_stats, cpu);
702
703 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
704 totals.events[i] += stats->events[i];
705 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
706 totals.bytes[i] += stats->bytes[i];
707
708 preempt_enable();
709 }
710
711 seq_printf(m, "\n\tevents:\t");
712 for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
713 seq_printf(m, "%lu ", totals.events[i]);
714 seq_printf(m, "\n\tbytes:\t");
715 for (i = 0; i < __NFSIOS_BYTESMAX; i++)
716 seq_printf(m, "%Lu ", totals.bytes[i]);
Chuck Lever4ece3a22006-03-20 13:44:22 -0500717 seq_printf(m, "\n");
718
719 rpc_print_iostats(m, nfss->client);
Chuck Leverd9ef5a82006-03-20 13:44:13 -0500720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
723
Trond Myklebust29884df2005-12-13 16:13:54 -0500724/**
725 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
726 */
727int nfs_sync_mapping(struct address_space *mapping)
728{
729 int ret;
730
731 if (mapping->nrpages == 0)
732 return 0;
733 unmap_mapping_range(mapping, 0, 0, 0);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800734 ret = filemap_write_and_wait(mapping);
Trond Myklebust29884df2005-12-13 16:13:54 -0500735 if (ret != 0)
736 goto out;
737 ret = nfs_wb_all(mapping->host);
738out:
739 return ret;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/*
743 * Invalidate the local caches
744 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500745static void nfs_zap_caches_locked(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct nfs_inode *nfsi = NFS_I(inode);
748 int mode = inode->i_mode;
749
Chuck Lever91d5b472006-03-20 13:44:14 -0500750 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
753 NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
754
755 memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
756 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
Chuck Lever55296802005-08-18 11:24:09 -0700757 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 else
Chuck Lever55296802005-08-18 11:24:09 -0700759 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500760}
Chuck Leverdc592502005-08-18 11:24:12 -0700761
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500762void nfs_zap_caches(struct inode *inode)
763{
764 spin_lock(&inode->i_lock);
765 nfs_zap_caches_locked(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700766 spin_unlock(&inode->i_lock);
Trond Myklebustada70d92005-06-22 17:16:22 +0000767}
768
769static void nfs_zap_acl_cache(struct inode *inode)
770{
771 void (*clear_acl_cache)(struct inode *);
772
773 clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
774 if (clear_acl_cache != NULL)
775 clear_acl_cache(inode);
Chuck Leverdc592502005-08-18 11:24:12 -0700776 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700777 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700778 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500782 * Invalidate, but do not unhash, the inode.
783 * NB: must be called with inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500785static void nfs_invalidate_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Trond Myklebustb37b03b2005-11-25 17:10:06 -0500787 set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
788 nfs_zap_caches_locked(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791struct nfs_find_desc {
792 struct nfs_fh *fh;
793 struct nfs_fattr *fattr;
794};
795
796/*
797 * In NFSv3 we can have 64bit inode numbers. In order to support
798 * this, and re-exported directories (also seen in NFSv2)
799 * we are forced to allow 2 different inodes to have the same
800 * i_ino.
801 */
802static int
803nfs_find_actor(struct inode *inode, void *opaque)
804{
805 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
806 struct nfs_fh *fh = desc->fh;
807 struct nfs_fattr *fattr = desc->fattr;
808
809 if (NFS_FILEID(inode) != fattr->fileid)
810 return 0;
811 if (nfs_compare_fh(NFS_FH(inode), fh))
812 return 0;
813 if (is_bad_inode(inode) || NFS_STALE(inode))
814 return 0;
815 return 1;
816}
817
818static int
819nfs_init_locked(struct inode *inode, void *opaque)
820{
821 struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
822 struct nfs_fattr *fattr = desc->fattr;
823
824 NFS_FILEID(inode) = fattr->fileid;
825 nfs_copy_fh(NFS_FH(inode), desc->fh);
826 return 0;
827}
828
829/* Don't use READDIRPLUS on directories that we believe are too large */
830#define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
831
832/*
833 * This is our front-end to iget that looks up inodes by file handle
834 * instead of inode number.
835 */
836struct inode *
837nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
838{
839 struct nfs_find_desc desc = {
840 .fh = fh,
841 .fattr = fattr
842 };
Trond Myklebust03f28e32006-03-20 13:44:48 -0500843 struct inode *inode = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned long hash;
845
846 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
847 goto out_no_inode;
848
849 if (!fattr->nlink) {
850 printk("NFS: Buggy server - nlink == 0!\n");
851 goto out_no_inode;
852 }
853
854 hash = nfs_fattr_to_ino_t(fattr);
855
Trond Myklebust03f28e32006-03-20 13:44:48 -0500856 inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
857 if (inode == NULL) {
858 inode = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto out_no_inode;
Trond Myklebust03f28e32006-03-20 13:44:48 -0500860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (inode->i_state & I_NEW) {
863 struct nfs_inode *nfsi = NFS_I(inode);
864
865 /* We set i_ino for the few things that still rely on it,
866 * such as stat(2) */
867 inode->i_ino = hash;
868
869 /* We can't support update_atime(), since the server will reset it */
870 inode->i_flags |= S_NOATIME|S_NOCMTIME;
871 inode->i_mode = fattr->mode;
872 /* Why so? Because we want revalidate for devices/FIFOs, and
873 * that's precisely what we have in nfs_file_inode_operations.
874 */
J. Bruce Fields92cfc622005-06-22 17:16:22 +0000875 inode->i_op = NFS_SB(sb)->rpc_ops->file_inode_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (S_ISREG(inode->i_mode)) {
877 inode->i_fop = &nfs_file_operations;
878 inode->i_data.a_ops = &nfs_file_aops;
879 inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
880 } else if (S_ISDIR(inode->i_mode)) {
881 inode->i_op = NFS_SB(sb)->rpc_ops->dir_inode_ops;
882 inode->i_fop = &nfs_dir_operations;
883 if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
884 && fattr->size <= NFS_LIMIT_READDIRPLUS)
Chuck Lever412d5822005-08-18 11:24:11 -0700885 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
Trond Myklebust55a97592006-06-09 09:34:19 -0400886 /* Deal with crossing mountpoints */
887 if (!nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
888 inode->i_op = &nfs_mountpoint_inode_operations;
889 inode->i_fop = NULL;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } else if (S_ISLNK(inode->i_mode))
892 inode->i_op = &nfs_symlink_inode_operations;
893 else
894 init_special_inode(inode, inode->i_mode, fattr->rdev);
895
Trond Myklebust33801142005-10-27 22:12:39 -0400896 nfsi->read_cache_jiffies = fattr->time_start;
897 nfsi->last_updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 inode->i_atime = fattr->atime;
899 inode->i_mtime = fattr->mtime;
900 inode->i_ctime = fattr->ctime;
901 if (fattr->valid & NFS_ATTR_FATTR_V4)
902 nfsi->change_attr = fattr->change_attr;
903 inode->i_size = nfs_size_to_loff_t(fattr->size);
904 inode->i_nlink = fattr->nlink;
905 inode->i_uid = fattr->uid;
906 inode->i_gid = fattr->gid;
907 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
908 /*
909 * report the blocks in 512byte units
910 */
911 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
912 inode->i_blksize = inode->i_sb->s_blocksize;
913 } else {
914 inode->i_blocks = fattr->du.nfs2.blocks;
915 inode->i_blksize = fattr->du.nfs2.blocksize;
916 }
917 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
918 nfsi->attrtimeo_timestamp = jiffies;
919 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
920 nfsi->cache_access.cred = NULL;
921
922 unlock_new_inode(inode);
923 } else
924 nfs_refresh_inode(inode, fattr);
925 dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
926 inode->i_sb->s_id,
927 (long long)NFS_FILEID(inode),
928 atomic_read(&inode->i_count));
929
930out:
931 return inode;
932
933out_no_inode:
Trond Myklebust03f28e32006-03-20 13:44:48 -0500934 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto out;
936}
937
938#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET)
939
940int
941nfs_setattr(struct dentry *dentry, struct iattr *attr)
942{
943 struct inode *inode = dentry->d_inode;
944 struct nfs_fattr fattr;
945 int error;
946
Chuck Lever91d5b472006-03-20 13:44:14 -0500947 nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (attr->ia_valid & ATTR_SIZE) {
950 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
951 attr->ia_valid &= ~ATTR_SIZE;
952 }
953
954 /* Optimization: if the end result is no change, don't RPC */
955 attr->ia_valid &= NFS_VALID_ATTRS;
956 if (attr->ia_valid == 0)
957 return 0;
958
959 lock_kernel();
960 nfs_begin_data_update(inode);
Trond Myklebust755c1e22006-03-20 13:44:06 -0500961 /* Write all dirty data */
962 filemap_write_and_wait(inode->i_mapping);
963 nfs_wb_all(inode);
Trond Myklebust642ac542005-10-18 14:20:19 -0700964 /*
965 * Return any delegations if we're going to change ACLs
966 */
967 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
968 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400970 if (error == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 nfs_refresh_inode(inode, &fattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400972 nfs_end_data_update(inode);
973 unlock_kernel();
974 return error;
975}
976
977/**
978 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
979 * @inode: pointer to struct inode
980 * @attr: pointer to struct iattr
981 *
982 * Note: we do this in the *proc.c in order to ensure that
983 * it works for things like exclusive creates too.
984 */
985void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
986{
987 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if ((attr->ia_valid & ATTR_MODE) != 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -0400989 int mode = attr->ia_mode & S_IALLUGO;
990 mode |= inode->i_mode & ~S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 inode->i_mode = mode;
992 }
993 if ((attr->ia_valid & ATTR_UID) != 0)
994 inode->i_uid = attr->ia_uid;
995 if ((attr->ia_valid & ATTR_GID) != 0)
996 inode->i_gid = attr->ia_gid;
Chuck Leverdc592502005-08-18 11:24:12 -0700997 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -0700998 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
Chuck Leverdc592502005-08-18 11:24:12 -0700999 spin_unlock(&inode->i_lock);
Trond Myklebust65e43082005-08-16 11:49:44 -04001000 }
1001 if ((attr->ia_valid & ATTR_SIZE) != 0) {
Chuck Lever91d5b472006-03-20 13:44:14 -05001002 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
Trond Myklebust65e43082005-08-16 11:49:44 -04001003 inode->i_size = attr->ia_size;
1004 vmtruncate(inode, attr->ia_size);
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Chuck Lever412d5822005-08-18 11:24:11 -07001008static int nfs_wait_schedule(void *word)
1009{
1010 if (signal_pending(current))
1011 return -ERESTARTSYS;
1012 schedule();
1013 return 0;
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
1017 * Wait for the inode to get unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
Chuck Lever412d5822005-08-18 11:24:11 -07001019static int nfs_wait_on_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct rpc_clnt *clnt = NFS_CLIENT(inode);
1022 struct nfs_inode *nfsi = NFS_I(inode);
Chuck Lever412d5822005-08-18 11:24:11 -07001023 sigset_t oldmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int error;
Chuck Lever412d5822005-08-18 11:24:11 -07001025
Chuck Lever412d5822005-08-18 11:24:11 -07001026 rpc_clnt_sigmask(clnt, &oldmask);
1027 error = wait_on_bit_lock(&nfsi->flags, NFS_INO_REVALIDATING,
1028 nfs_wait_schedule, TASK_INTERRUPTIBLE);
1029 rpc_clnt_sigunmask(clnt, &oldmask);
Chuck Lever412d5822005-08-18 11:24:11 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return error;
1032}
1033
Chuck Lever412d5822005-08-18 11:24:11 -07001034static void nfs_wake_up_inode(struct inode *inode)
1035{
1036 struct nfs_inode *nfsi = NFS_I(inode);
1037
1038 clear_bit(NFS_INO_REVALIDATING, &nfsi->flags);
1039 smp_mb__after_clear_bit();
1040 wake_up_bit(&nfsi->flags, NFS_INO_REVALIDATING);
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1044{
1045 struct inode *inode = dentry->d_inode;
Chuck Lever55296802005-08-18 11:24:09 -07001046 int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int err;
1048
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001049 /* Flush out writes to the server in order to update c/mtime */
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001050 nfs_sync_inode_wait(inode, 0, 0, FLUSH_NOCOMMIT);
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001051
1052 /*
1053 * We may force a getattr if the user cares about atime.
1054 *
1055 * Note that we only have to check the vfsmount flags here:
1056 * - NFS always sets S_NOATIME by so checking it would give a
1057 * bogus result
1058 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
1059 * no point in checking those.
1060 */
1061 if ((mnt->mnt_flags & MNT_NOATIME) ||
1062 ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 need_atime = 0;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (need_atime)
1066 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
1067 else
1068 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1069 if (!err)
1070 generic_fillattr(inode, stat);
1071 return err;
1072}
1073
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001074static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct nfs_open_context *ctx;
1077
1078 ctx = (struct nfs_open_context *)kmalloc(sizeof(*ctx), GFP_KERNEL);
1079 if (ctx != NULL) {
1080 atomic_set(&ctx->count, 1);
1081 ctx->dentry = dget(dentry);
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001082 ctx->vfsmnt = mntget(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ctx->cred = get_rpccred(cred);
1084 ctx->state = NULL;
1085 ctx->lockowner = current->files;
1086 ctx->error = 0;
Olivier Galibert00a92642005-06-22 17:16:29 +00001087 ctx->dir_cookie = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 return ctx;
1090}
1091
1092struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
1093{
1094 if (ctx != NULL)
1095 atomic_inc(&ctx->count);
1096 return ctx;
1097}
1098
1099void put_nfs_open_context(struct nfs_open_context *ctx)
1100{
1101 if (atomic_dec_and_test(&ctx->count)) {
1102 if (!list_empty(&ctx->list)) {
1103 struct inode *inode = ctx->dentry->d_inode;
1104 spin_lock(&inode->i_lock);
1105 list_del(&ctx->list);
1106 spin_unlock(&inode->i_lock);
1107 }
1108 if (ctx->state != NULL)
1109 nfs4_close_state(ctx->state, ctx->mode);
1110 if (ctx->cred != NULL)
1111 put_rpccred(ctx->cred);
1112 dput(ctx->dentry);
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001113 mntput(ctx->vfsmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 kfree(ctx);
1115 }
1116}
1117
1118/*
1119 * Ensure that mmap has a recent RPC credential for use when writing out
1120 * shared pages
1121 */
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001122static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct inode *inode = filp->f_dentry->d_inode;
1125 struct nfs_inode *nfsi = NFS_I(inode);
1126
1127 filp->private_data = get_nfs_open_context(ctx);
1128 spin_lock(&inode->i_lock);
1129 list_add(&ctx->list, &nfsi->open_files);
1130 spin_unlock(&inode->i_lock);
1131}
1132
Trond Myklebustd5308382005-11-04 15:33:38 -05001133/*
1134 * Given an inode, search for an open context with the desired characteristics
1135 */
1136struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct nfs_inode *nfsi = NFS_I(inode);
1139 struct nfs_open_context *pos, *ctx = NULL;
1140
1141 spin_lock(&inode->i_lock);
1142 list_for_each_entry(pos, &nfsi->open_files, list) {
Trond Myklebustd5308382005-11-04 15:33:38 -05001143 if (cred != NULL && pos->cred != cred)
1144 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if ((pos->mode & mode) == mode) {
1146 ctx = get_nfs_open_context(pos);
1147 break;
1148 }
1149 }
1150 spin_unlock(&inode->i_lock);
1151 return ctx;
1152}
1153
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001154static void nfs_file_clear_open_context(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 struct inode *inode = filp->f_dentry->d_inode;
1157 struct nfs_open_context *ctx = (struct nfs_open_context *)filp->private_data;
1158
1159 if (ctx) {
1160 filp->private_data = NULL;
1161 spin_lock(&inode->i_lock);
1162 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
1163 spin_unlock(&inode->i_lock);
1164 put_nfs_open_context(ctx);
1165 }
1166}
1167
1168/*
1169 * These allocate and release file read/write context information.
1170 */
1171int nfs_open(struct inode *inode, struct file *filp)
1172{
1173 struct nfs_open_context *ctx;
1174 struct rpc_cred *cred;
1175
1176 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1177 if (IS_ERR(cred))
1178 return PTR_ERR(cred);
Trond Myklebustb92dccf2006-03-20 13:44:03 -05001179 ctx = alloc_nfs_open_context(filp->f_vfsmnt, filp->f_dentry, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 put_rpccred(cred);
1181 if (ctx == NULL)
1182 return -ENOMEM;
1183 ctx->mode = filp->f_mode;
1184 nfs_file_set_open_context(filp, ctx);
1185 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187}
1188
1189int nfs_release(struct inode *inode, struct file *filp)
1190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 nfs_file_clear_open_context(filp);
1192 return 0;
1193}
1194
1195/*
1196 * This function is called whenever some part of NFS notices that
1197 * the cached attributes have to be refreshed.
1198 */
1199int
1200__nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1201{
1202 int status = -ESTALE;
1203 struct nfs_fattr fattr;
1204 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
1207 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
1208
Trond Myklebust1842bfb2006-05-25 01:41:01 -04001209 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 lock_kernel();
1211 if (!inode || is_bad_inode(inode))
1212 goto out_nowait;
1213 if (NFS_STALE(inode))
1214 goto out_nowait;
1215
Chuck Lever412d5822005-08-18 11:24:11 -07001216 status = nfs_wait_on_inode(inode);
1217 if (status < 0)
1218 goto out;
1219 if (NFS_STALE(inode)) {
1220 status = -ESTALE;
1221 /* Do we trust the cached ESTALE? */
1222 if (NFS_ATTRTIMEO(inode) != 0) {
Trond Myklebust44b11872006-05-25 01:40:59 -04001223 if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) {
Chuck Lever412d5822005-08-18 11:24:11 -07001224 /* no */
1225 } else
1226 goto out;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
1231 if (status != 0) {
1232 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
1233 inode->i_sb->s_id,
1234 (long long)NFS_FILEID(inode), status);
1235 if (status == -ESTALE) {
1236 nfs_zap_caches(inode);
1237 if (!S_ISDIR(inode->i_mode))
Chuck Lever412d5822005-08-18 11:24:11 -07001238 set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 goto out;
1241 }
1242
Trond Myklebust33801142005-10-27 22:12:39 -04001243 spin_lock(&inode->i_lock);
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001244 status = nfs_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (status) {
Trond Myklebust33801142005-10-27 22:12:39 -04001246 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
1248 inode->i_sb->s_id,
1249 (long long)NFS_FILEID(inode), status);
1250 goto out;
1251 }
Chuck Leverdc592502005-08-18 11:24:12 -07001252 spin_unlock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -07001253
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001254 if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
Trond Myklebustada70d92005-06-22 17:16:22 +00001255 nfs_zap_acl_cache(inode);
Chuck Lever55296802005-08-18 11:24:09 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
1258 inode->i_sb->s_id,
1259 (long long)NFS_FILEID(inode));
1260
Chuck Lever412d5822005-08-18 11:24:11 -07001261 out:
1262 nfs_wake_up_inode(inode);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 out_nowait:
1265 unlock_kernel();
1266 return status;
1267}
1268
1269int nfs_attribute_timeout(struct inode *inode)
1270{
1271 struct nfs_inode *nfsi = NFS_I(inode);
1272
1273 if (nfs_have_delegation(inode, FMODE_READ))
1274 return 0;
1275 return time_after(jiffies, nfsi->read_cache_jiffies+nfsi->attrtimeo);
1276}
1277
1278/**
1279 * nfs_revalidate_inode - Revalidate the inode attributes
1280 * @server - pointer to nfs_server struct
1281 * @inode - pointer to inode struct
1282 *
1283 * Updates inode attribute information by retrieving the data from the server.
1284 */
1285int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
1286{
Trond Myklebust44b11872006-05-25 01:40:59 -04001287 if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 && !nfs_attribute_timeout(inode))
1289 return NFS_STALE(inode) ? -ESTALE : 0;
1290 return __nfs_revalidate_inode(server, inode);
1291}
1292
1293/**
Trond Myklebust7d52e862005-06-22 17:16:30 +00001294 * nfs_revalidate_mapping - Revalidate the pagecache
1295 * @inode - pointer to host inode
1296 * @mapping - pointer to mapping
1297 */
Trond Myklebust44b11872006-05-25 01:40:59 -04001298int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
Trond Myklebust7d52e862005-06-22 17:16:30 +00001299{
1300 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust44b11872006-05-25 01:40:59 -04001301 int ret = 0;
1302
1303 if (NFS_STALE(inode))
1304 ret = -ESTALE;
1305 if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1306 || nfs_attribute_timeout(inode))
1307 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
Trond Myklebust7d52e862005-06-22 17:16:30 +00001308
Chuck Lever55296802005-08-18 11:24:09 -07001309 if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
Chuck Lever91d5b472006-03-20 13:44:14 -05001310 nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
Trond Myklebust29884df2005-12-13 16:13:54 -05001311 if (S_ISREG(inode->i_mode))
1312 nfs_sync_mapping(mapping);
Trond Myklebust7d52e862005-06-22 17:16:30 +00001313 invalidate_inode_pages2(mapping);
Chuck Leverdc592502005-08-18 11:24:12 -07001314
1315 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -07001316 nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001317 if (S_ISDIR(inode->i_mode)) {
1318 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
1319 /* This ensures we revalidate child dentries */
Trond Myklebust913a70f2005-10-27 22:12:38 -04001320 nfsi->cache_change_attribute = jiffies;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001321 }
Chuck Leverdc592502005-08-18 11:24:12 -07001322 spin_unlock(&inode->i_lock);
1323
Trond Myklebust7d52e862005-06-22 17:16:30 +00001324 dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
1325 inode->i_sb->s_id,
1326 (long long)NFS_FILEID(inode));
1327 }
Trond Myklebust44b11872006-05-25 01:40:59 -04001328 return ret;
Trond Myklebust7d52e862005-06-22 17:16:30 +00001329}
1330
1331/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * nfs_begin_data_update
1333 * @inode - pointer to inode
1334 * Declare that a set of operations will update file data on the server
1335 */
1336void nfs_begin_data_update(struct inode *inode)
1337{
1338 atomic_inc(&NFS_I(inode)->data_updates);
1339}
1340
1341/**
1342 * nfs_end_data_update
1343 * @inode - pointer to inode
1344 * Declare end of the operations that will update file data
1345 * This will mark the inode as immediately needing revalidation
1346 * of its attribute cache.
1347 */
1348void nfs_end_data_update(struct inode *inode)
1349{
1350 struct nfs_inode *nfsi = NFS_I(inode);
1351
1352 if (!nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebustdecf4912005-10-27 22:12:39 -04001353 /* Directories and symlinks: invalidate page cache */
1354 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
1355 spin_lock(&inode->i_lock);
Chuck Lever55296802005-08-18 11:24:09 -07001356 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001357 spin_unlock(&inode->i_lock);
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Trond Myklebust913a70f2005-10-27 22:12:38 -04001360 nfsi->cache_change_attribute = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 atomic_dec(&nfsi->data_updates);
1362}
1363
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001364static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1365{
1366 struct nfs_inode *nfsi = NFS_I(inode);
1367
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001368 /* If we have atomic WCC data, we may update some attributes */
1369 if ((fattr->valid & NFS_ATTR_WCC) != 0) {
1370 if (timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
1371 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1372 nfsi->cache_change_attribute = jiffies;
1373 }
1374 if (timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
1375 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1376 nfsi->cache_change_attribute = jiffies;
1377 }
1378 if (inode->i_size == fattr->pre_size && nfsi->npages == 0) {
1379 inode->i_size = fattr->size;
1380 nfsi->cache_change_attribute = jiffies;
1381 }
1382 }
1383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/**
Trond Myklebust33801142005-10-27 22:12:39 -04001386 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * @inode - pointer to inode
1388 * @fattr - updated attributes
1389 *
1390 * Verifies the attribute cache. If we have just changed the attributes,
1391 * so that fattr carries weak cache consistency data, then it may
1392 * also update the ctime/mtime/change_attribute.
1393 */
Trond Myklebust33801142005-10-27 22:12:39 -04001394static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct nfs_inode *nfsi = NFS_I(inode);
1397 loff_t cur_size, new_isize;
1398 int data_unstable;
1399
Chuck Leverdc592502005-08-18 11:24:12 -07001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Has the inode gone and changed behind our back? */
1402 if (nfsi->fileid != fattr->fileid
Chuck Leverdc592502005-08-18 11:24:12 -07001403 || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EIO;
Chuck Leverdc592502005-08-18 11:24:12 -07001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001407 /* Are we in the process of updating data on the server? */
1408 data_unstable = nfs_caches_unstable(inode);
1409
1410 /* Do atomic weak cache consistency updates */
1411 nfs_wcc_update_inode(inode, fattr);
1412
Trond Myklebust9d1e9232006-05-25 01:40:46 -04001413 if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
Trond Myklebustf1bb0b92006-05-25 01:40:55 -04001414 nfsi->change_attr != fattr->change_attr)
1415 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Verify a few of the more important attributes */
Trond Myklebustf1bb0b92006-05-25 01:40:55 -04001418 if (!timespec_equal(&inode->i_mtime, &fattr->mtime))
1419 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001420
1421 cur_size = i_size_read(inode);
1422 new_isize = nfs_size_to_loff_t(fattr->size);
Trond Myklebustfb374d22006-03-20 13:44:08 -05001423 if (cur_size != new_isize && nfsi->npages == 0)
1424 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* Have any file permissions changed? */
1427 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)
1428 || inode->i_uid != fattr->uid
1429 || inode->i_gid != fattr->gid)
Chuck Lever55296802005-08-18 11:24:09 -07001430 nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /* Has the link count changed? */
1433 if (inode->i_nlink != fattr->nlink)
Chuck Lever55296802005-08-18 11:24:09 -07001434 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!timespec_equal(&inode->i_atime, &fattr->atime))
Chuck Lever55296802005-08-18 11:24:09 -07001437 nfsi->cache_validity |= NFS_INO_INVALID_ATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Trond Myklebust33801142005-10-27 22:12:39 -04001439 nfsi->read_cache_jiffies = fattr->time_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
1441}
1442
Trond Myklebust33801142005-10-27 22:12:39 -04001443/**
1444 * nfs_refresh_inode - try to update the inode attribute cache
1445 * @inode - pointer to inode
1446 * @fattr - updated attributes
1447 *
1448 * Check that an RPC call that returned attributes has not overlapped with
1449 * other recent updates of the inode metadata, then decide whether it is
1450 * safe to do a full update of the inode attributes, or whether just to
1451 * call nfs_check_inode_attributes.
1452 */
1453int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1454{
1455 struct nfs_inode *nfsi = NFS_I(inode);
1456 int status;
1457
1458 if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1459 return 0;
1460 spin_lock(&inode->i_lock);
Trond Myklebust33801142005-10-27 22:12:39 -04001461 if (time_after(fattr->time_start, nfsi->last_updated))
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001462 status = nfs_update_inode(inode, fattr);
Trond Myklebust33801142005-10-27 22:12:39 -04001463 else
1464 status = nfs_check_inode_attributes(inode, fattr);
1465
1466 spin_unlock(&inode->i_lock);
1467 return status;
1468}
1469
Trond Myklebustdecf4912005-10-27 22:12:39 -04001470/**
1471 * nfs_post_op_update_inode - try to update the inode attribute cache
1472 * @inode - pointer to inode
1473 * @fattr - updated attributes
1474 *
1475 * After an operation that has changed the inode metadata, mark the
1476 * attribute cache as being invalid, then try to update it.
1477 */
1478int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1479{
1480 struct nfs_inode *nfsi = NFS_I(inode);
1481 int status = 0;
1482
1483 spin_lock(&inode->i_lock);
1484 if (unlikely((fattr->valid & NFS_ATTR_FATTR) == 0)) {
Trond Myklebustf1bb0b92006-05-25 01:40:55 -04001485 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001486 goto out;
1487 }
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001488 status = nfs_update_inode(inode, fattr);
Trond Myklebustdecf4912005-10-27 22:12:39 -04001489out:
1490 spin_unlock(&inode->i_lock);
1491 return status;
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/*
1495 * Many nfs protocol calls return the new file attributes after
1496 * an operation. Here we update the inode to reflect the state
1497 * of the server's inode.
1498 *
1499 * This is a bit tricky because we have to make sure all dirty pages
1500 * have been sent off to the server before calling invalidate_inode_pages.
1501 * To make sure no other process adds more write requests while we try
1502 * our best to flush them, we make them sleep during the attribute refresh.
1503 *
1504 * A very similar scenario holds for the dir cache.
1505 */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001506static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001508 struct nfs_server *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001510 loff_t cur_isize, new_isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned int invalid = 0;
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001512 int data_stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1515 __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
1516 atomic_read(&inode->i_count), fattr->valid);
1517
Chuck Lever325cfed2005-11-30 18:08:55 -05001518 if (nfsi->fileid != fattr->fileid)
1519 goto out_fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /*
1522 * Make sure the inode's type hasn't changed.
1523 */
Trond Myklebust33801142005-10-27 22:12:39 -04001524 if ((inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 goto out_changed;
1526
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04001527 server = NFS_SERVER(inode);
1528 /* Update the fsid if and only if this is the root directory */
1529 if (inode == inode->i_sb->s_root->d_inode
1530 && !nfs_fsid_equal(&server->fsid, &fattr->fsid))
1531 server->fsid = fattr->fsid;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * Update the read time so we don't revalidate too often.
1535 */
Trond Myklebust33801142005-10-27 22:12:39 -04001536 nfsi->read_cache_jiffies = fattr->time_start;
1537 nfsi->last_updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* Are we racing with known updates of the metadata on the server? */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001540 data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
1541 if (data_stable)
Trond Myklebustf1bb0b92006-05-25 01:40:55 -04001542 nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Trond Myklebusta895b4a2006-01-03 09:55:40 +01001544 /* Do atomic weak cache consistency updates */
1545 nfs_wcc_update_inode(inode, fattr);
1546
Trond Myklebust951a1432005-06-22 17:16:30 +00001547 /* Check if our cached file size is stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 new_isize = nfs_size_to_loff_t(fattr->size);
1549 cur_isize = i_size_read(inode);
Trond Myklebust951a1432005-06-22 17:16:30 +00001550 if (new_isize != cur_isize) {
1551 /* Do we perhaps have any outstanding writes? */
1552 if (nfsi->npages == 0) {
1553 /* No, but did we race with nfs_end_data_update()? */
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001554 if (data_stable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 inode->i_size = new_isize;
Trond Myklebust951a1432005-06-22 17:16:30 +00001556 invalid |= NFS_INO_INVALID_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Trond Myklebust951a1432005-06-22 17:16:30 +00001558 invalid |= NFS_INO_INVALID_ATTR;
1559 } else if (new_isize > cur_isize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 inode->i_size = new_isize;
1561 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1562 }
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001563 nfsi->cache_change_attribute = jiffies;
Trond Myklebust951a1432005-06-22 17:16:30 +00001564 dprintk("NFS: isize change on server for file %s/%ld\n",
1565 inode->i_sb->s_id, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
Trond Myklebust951a1432005-06-22 17:16:30 +00001568 /* Check if the mtime agrees */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1570 memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
Trond Myklebust951a1432005-06-22 17:16:30 +00001571 dprintk("NFS: mtime change on server for file %s/%ld\n",
1572 inode->i_sb->s_id, inode->i_ino);
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001573 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1574 nfsi->cache_change_attribute = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
Trond Myklebustada70d92005-06-22 17:16:22 +00001577 /* If ctime has changed we should definitely clear access+acl caches */
1578 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001579 invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
Trond Myklebustada70d92005-06-22 17:16:22 +00001580 memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001581 nfsi->cache_change_attribute = jiffies;
Trond Myklebustada70d92005-06-22 17:16:22 +00001582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1584
1585 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO) ||
1586 inode->i_uid != fattr->uid ||
1587 inode->i_gid != fattr->gid)
Trond Myklebustada70d92005-06-22 17:16:22 +00001588 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 inode->i_mode = fattr->mode;
1591 inode->i_nlink = fattr->nlink;
1592 inode->i_uid = fattr->uid;
1593 inode->i_gid = fattr->gid;
1594
1595 if (fattr->valid & (NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4)) {
1596 /*
1597 * report the blocks in 512byte units
1598 */
1599 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1600 inode->i_blksize = inode->i_sb->s_blocksize;
1601 } else {
1602 inode->i_blocks = fattr->du.nfs2.blocks;
1603 inode->i_blksize = fattr->du.nfs2.blocksize;
1604 }
1605
Trond Myklebust9d1e9232006-05-25 01:40:46 -04001606 if ((fattr->valid & NFS_ATTR_FATTR_V4) != 0 &&
1607 nfsi->change_attr != fattr->change_attr) {
1608 dprintk("NFS: change_attr change on server for file %s/%ld\n",
1609 inode->i_sb->s_id, inode->i_ino);
1610 nfsi->change_attr = fattr->change_attr;
1611 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1612 nfsi->cache_change_attribute = jiffies;
Trond Myklebustca62b9c2006-03-20 13:44:07 -05001613 }
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /* Update attrtimeo value if we're out of the unstable period */
1616 if (invalid & NFS_INO_INVALID_ATTR) {
Chuck Lever91d5b472006-03-20 13:44:14 -05001617 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1619 nfsi->attrtimeo_timestamp = jiffies;
1620 } else if (time_after(jiffies, nfsi->attrtimeo_timestamp+nfsi->attrtimeo)) {
1621 if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1622 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1623 nfsi->attrtimeo_timestamp = jiffies;
1624 }
1625 /* Don't invalidate the data if we were to blame */
1626 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1627 || S_ISLNK(inode->i_mode)))
1628 invalid &= ~NFS_INO_INVALID_DATA;
Trond Myklebust24aa1fe2005-12-03 15:20:07 -05001629 if (data_stable)
1630 invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!nfs_have_delegation(inode, FMODE_READ))
Chuck Lever55296802005-08-18 11:24:09 -07001632 nfsi->cache_validity |= invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 return 0;
1635 out_changed:
1636 /*
1637 * Big trouble! The inode has become a different object.
1638 */
1639#ifdef NFS_PARANOIA
1640 printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1641 __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
1642#endif
Trond Myklebustb37b03b2005-11-25 17:10:06 -05001643 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * No need to worry about unhashing the dentry, as the
1646 * lookup validation will know that the inode is bad.
1647 * (But we fall through to invalidate the caches.)
1648 */
1649 nfs_invalidate_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -ESTALE;
Chuck Lever325cfed2005-11-30 18:08:55 -05001651
1652 out_fileid:
1653 printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1654 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1655 NFS_SERVER(inode)->hostname, inode->i_sb->s_id,
1656 (long long)nfsi->fileid, (long long)fattr->fileid);
1657 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660/*
1661 * File system information
1662 */
1663
Trond Myklebust55a97592006-06-09 09:34:19 -04001664/*
1665 * nfs_path - reconstruct the path given an arbitrary dentry
1666 * @base - arbitrary string to prepend to the path
1667 * @dentry - pointer to dentry
1668 * @buffer - result buffer
1669 * @buflen - length of buffer
1670 *
1671 * Helper function for constructing the path from the
1672 * root dentry to an arbitrary hashed dentry.
1673 *
1674 * This is mainly for use in figuring out the path on the
1675 * server side when automounting on top of an existing partition.
1676 */
1677static char *nfs_path(const char *base, const struct dentry *dentry,
1678 char *buffer, ssize_t buflen)
1679{
1680 char *end = buffer+buflen;
1681 int namelen;
1682
1683 *--end = '\0';
1684 buflen--;
1685 spin_lock(&dcache_lock);
1686 while (!IS_ROOT(dentry)) {
1687 namelen = dentry->d_name.len;
1688 buflen -= namelen + 1;
1689 if (buflen < 0)
1690 goto Elong;
1691 end -= namelen;
1692 memcpy(end, dentry->d_name.name, namelen);
1693 *--end = '/';
1694 dentry = dentry->d_parent;
1695 }
1696 spin_unlock(&dcache_lock);
1697 namelen = strlen(base);
1698 /* Strip off excess slashes in base string */
1699 while (namelen > 0 && base[namelen - 1] == '/')
1700 namelen--;
1701 buflen -= namelen;
1702 if (buflen < 0)
1703 goto Elong;
1704 end -= namelen;
1705 memcpy(end, base, namelen);
1706 return end;
1707Elong:
1708 return ERR_PTR(-ENAMETOOLONG);
1709}
1710
1711struct nfs_clone_mount {
1712 const struct super_block *sb;
1713 const struct dentry *dentry;
1714 struct nfs_fh *fh;
1715 struct nfs_fattr *fattr;
1716};
1717
1718static struct super_block *nfs_clone_generic_sb(struct nfs_clone_mount *data,
1719 struct super_block *(*clone_client)(struct nfs_server *, struct nfs_clone_mount *))
1720{
1721 struct nfs_server *server;
1722 struct nfs_server *parent = NFS_SB(data->sb);
1723 struct super_block *sb = ERR_PTR(-EINVAL);
1724 void *err = ERR_PTR(-ENOMEM);
1725 struct inode *root_inode;
1726 struct nfs_fsinfo fsinfo;
1727 int len;
1728
1729 server = kmalloc(sizeof(struct nfs_server), GFP_KERNEL);
1730 if (server == NULL)
1731 goto out_err;
1732 memcpy(server, parent, sizeof(*server));
1733 len = strlen(parent->hostname) + 1;
1734 server->hostname = kmalloc(len, GFP_KERNEL);
1735 if (server->hostname == NULL)
1736 goto free_server;
1737 memcpy(server->hostname, parent->hostname, len);
1738 server->fsid = data->fattr->fsid;
1739 nfs_copy_fh(&server->fh, data->fh);
1740 if (rpciod_up() != 0)
1741 goto free_hostname;
1742
1743 sb = clone_client(server, data);
1744 if (IS_ERR((err = sb)) || sb->s_root)
1745 goto kill_rpciod;
1746
1747 sb->s_op = data->sb->s_op;
1748 sb->s_blocksize = data->sb->s_blocksize;
1749 sb->s_blocksize_bits = data->sb->s_blocksize_bits;
1750 sb->s_maxbytes = data->sb->s_maxbytes;
1751
1752 server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
1753 err = ERR_PTR(-ENOMEM);
1754 server->io_stats = nfs_alloc_iostats();
1755 if (server->io_stats == NULL)
1756 goto out_deactivate;
1757
1758 server->client = rpc_clone_client(parent->client);
1759 if (IS_ERR((err = server->client)))
1760 goto out_deactivate;
1761 if (!IS_ERR(parent->client_sys)) {
1762 server->client_sys = rpc_clone_client(parent->client_sys);
1763 if (IS_ERR((err = server->client_sys)))
1764 goto out_deactivate;
1765 }
1766 if (!IS_ERR(parent->client_acl)) {
1767 server->client_acl = rpc_clone_client(parent->client_acl);
1768 if (IS_ERR((err = server->client_acl)))
1769 goto out_deactivate;
1770 }
1771 root_inode = nfs_fhget(sb, data->fh, data->fattr);
1772 if (!root_inode)
1773 goto out_deactivate;
1774 sb->s_root = d_alloc_root(root_inode);
1775 if (!sb->s_root)
1776 goto out_put_root;
1777 fsinfo.fattr = data->fattr;
1778 if (NFS_PROTO(root_inode)->fsinfo(server, data->fh, &fsinfo) == 0)
1779 nfs_super_set_maxbytes(sb, fsinfo.maxfilesize);
1780 sb->s_root->d_op = server->rpc_ops->dentry_ops;
1781 sb->s_flags |= MS_ACTIVE;
1782 return sb;
1783out_put_root:
1784 iput(root_inode);
1785out_deactivate:
1786 up_write(&sb->s_umount);
1787 deactivate_super(sb);
1788 return (struct super_block *)err;
1789kill_rpciod:
1790 rpciod_down();
1791free_hostname:
1792 kfree(server->hostname);
1793free_server:
1794 kfree(server);
1795out_err:
1796 return (struct super_block *)err;
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799static int nfs_set_super(struct super_block *s, void *data)
1800{
1801 s->s_fs_info = data;
1802 return set_anon_super(s, data);
1803}
1804
1805static int nfs_compare_super(struct super_block *sb, void *data)
1806{
1807 struct nfs_server *server = data;
1808 struct nfs_server *old = NFS_SB(sb);
1809
1810 if (old->addr.sin_addr.s_addr != server->addr.sin_addr.s_addr)
1811 return 0;
1812 if (old->addr.sin_port != server->addr.sin_port)
1813 return 0;
1814 return !nfs_compare_fh(&old->fh, &server->fh);
1815}
1816
1817static struct super_block *nfs_get_sb(struct file_system_type *fs_type,
1818 int flags, const char *dev_name, void *raw_data)
1819{
1820 int error;
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001821 struct nfs_server *server = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct super_block *s;
1823 struct nfs_fh *root;
1824 struct nfs_mount_data *data = raw_data;
1825
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001826 s = ERR_PTR(-EINVAL);
1827 if (data == NULL) {
1828 dprintk("%s: missing data argument\n", __FUNCTION__);
1829 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001831 if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
1832 dprintk("%s: bad mount version\n", __FUNCTION__);
1833 goto out_err;
1834 }
1835 switch (data->version) {
1836 case 1:
1837 data->namlen = 0;
1838 case 2:
1839 data->bsize = 0;
1840 case 3:
1841 if (data->flags & NFS_MOUNT_VER3) {
1842 dprintk("%s: mount structure version %d does not support NFSv3\n",
1843 __FUNCTION__,
1844 data->version);
1845 goto out_err;
1846 }
1847 data->root.size = NFS2_FHSIZE;
1848 memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1849 case 4:
1850 if (data->flags & NFS_MOUNT_SECFLAVOUR) {
1851 dprintk("%s: mount structure version %d does not support strong security\n",
1852 __FUNCTION__,
1853 data->version);
1854 goto out_err;
1855 }
1856 case 5:
1857 memset(data->context, 0, sizeof(data->context));
1858 }
1859#ifndef CONFIG_NFS_V3
1860 /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
1861 s = ERR_PTR(-EPROTONOSUPPORT);
1862 if (data->flags & NFS_MOUNT_VER3) {
1863 dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
1864 goto out_err;
1865 }
1866#endif /* CONFIG_NFS_V3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001868 s = ERR_PTR(-ENOMEM);
Eric Sesterhennbd647542006-03-20 13:44:10 -05001869 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (!server)
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001871 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Zero out the NFS state stuff */
1873 init_nfsv4_state(server);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001874 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 root = &server->fh;
1877 if (data->flags & NFS_MOUNT_VER3)
1878 root->size = data->root.size;
1879 else
1880 root->size = NFS2_FHSIZE;
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001881 s = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (root->size > sizeof(root->data)) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001883 dprintk("%s: invalid root filehandle\n", __FUNCTION__);
1884 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 memcpy(root->data, data->root.data, root->size);
1887
1888 /* We now require that the mount process passes the remote address */
1889 memcpy(&server->addr, &data->addr, sizeof(server->addr));
1890 if (server->addr.sin_addr.s_addr == INADDR_ANY) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001891 dprintk("%s: mount program didn't pass remote address!\n",
1892 __FUNCTION__);
1893 goto out_err;
1894 }
1895
1896 /* Fire up rpciod if not yet running */
1897 s = ERR_PTR(rpciod_up());
1898 if (IS_ERR(s)) {
1899 dprintk("%s: couldn't start rpciod! Error = %ld\n",
1900 __FUNCTION__, PTR_ERR(s));
1901 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
1904 s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001905 if (IS_ERR(s) || s->s_root)
1906 goto out_rpciod_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 s->s_flags = flags;
1909
Theodore Ts'o9b04c992006-03-24 03:15:10 -08001910 error = nfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (error) {
1912 up_write(&s->s_umount);
1913 deactivate_super(s);
1914 return ERR_PTR(error);
1915 }
1916 s->s_flags |= MS_ACTIVE;
1917 return s;
Trond Myklebust9085bbc2005-06-22 17:16:20 +00001918out_rpciod_down:
1919 rpciod_down();
1920out_err:
1921 kfree(server);
1922 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925static void nfs_kill_super(struct super_block *s)
1926{
1927 struct nfs_server *server = NFS_SB(s);
1928
1929 kill_anon_super(s);
1930
J. Bruce Fields6a192752005-06-22 17:16:23 +00001931 if (!IS_ERR(server->client))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 rpc_shutdown_client(server->client);
J. Bruce Fields6a192752005-06-22 17:16:23 +00001933 if (!IS_ERR(server->client_sys))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 rpc_shutdown_client(server->client_sys);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001935 if (!IS_ERR(server->client_acl))
1936 rpc_shutdown_client(server->client_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 if (!(server->flags & NFS_MOUNT_NONLM))
1939 lockd_down(); /* release rpc.lockd */
1940
1941 rpciod_down(); /* release rpciod */
1942
Trond Myklebust01d0ae82006-03-20 13:44:48 -05001943 nfs_free_iostats(server->io_stats);
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001944 kfree(server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 kfree(server);
1946}
1947
1948static struct file_system_type nfs_fs_type = {
1949 .owner = THIS_MODULE,
1950 .name = "nfs",
1951 .get_sb = nfs_get_sb,
1952 .kill_sb = nfs_kill_super,
1953 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1954};
1955
Trond Myklebust55a97592006-06-09 09:34:19 -04001956static struct super_block *nfs_clone_client(struct nfs_server *server, struct nfs_clone_mount *data)
1957{
1958 struct super_block *sb;
1959
1960 sb = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
1961 if (!IS_ERR(sb) && sb->s_root == NULL && !(server->flags & NFS_MOUNT_NONLM))
1962 lockd_up();
1963 return sb;
1964}
1965
1966static struct super_block *nfs_clone_nfs_sb(struct file_system_type *fs_type,
1967 int flags, const char *dev_name, void *raw_data)
1968{
1969 struct nfs_clone_mount *data = raw_data;
1970 return nfs_clone_generic_sb(data, nfs_clone_client);
1971}
1972
1973static struct file_system_type clone_nfs_fs_type = {
1974 .owner = THIS_MODULE,
1975 .name = "nfs",
1976 .get_sb = nfs_clone_nfs_sb,
1977 .kill_sb = nfs_kill_super,
1978 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
1979};
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981#ifdef CONFIG_NFS_V4
1982
1983static void nfs4_clear_inode(struct inode *);
1984
1985
1986static struct super_operations nfs4_sops = {
1987 .alloc_inode = nfs_alloc_inode,
1988 .destroy_inode = nfs_destroy_inode,
1989 .write_inode = nfs_write_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 .statfs = nfs_statfs,
1991 .clear_inode = nfs4_clear_inode,
1992 .umount_begin = nfs_umount_begin,
1993 .show_options = nfs_show_options,
Chuck Leverd9ef5a82006-03-20 13:44:13 -05001994 .show_stats = nfs_show_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995};
1996
1997/*
1998 * Clean out any remaining NFSv4 state that might be left over due
1999 * to open() calls that passed nfs_atomic_lookup, but failed to call
2000 * nfs_open().
2001 */
2002static void nfs4_clear_inode(struct inode *inode)
2003{
2004 struct nfs_inode *nfsi = NFS_I(inode);
2005
2006 /* If we are holding a delegation, return it! */
Trond Myklebustcae7a072005-10-18 14:20:19 -07002007 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* First call standard NFS clear_inode() code */
2009 nfs_clear_inode(inode);
2010 /* Now clear out any remaining state */
2011 while (!list_empty(&nfsi->open_states)) {
2012 struct nfs4_state *state;
2013
2014 state = list_entry(nfsi->open_states.next,
2015 struct nfs4_state,
2016 inode_states);
2017 dprintk("%s(%s/%Ld): found unclaimed NFSv4 state %p\n",
2018 __FUNCTION__,
2019 inode->i_sb->s_id,
2020 (long long)NFS_FILEID(inode),
2021 state);
2022 BUG_ON(atomic_read(&state->count) != 1);
2023 nfs4_close_state(state, state->state);
2024 }
2025}
2026
2027
2028static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data, int silent)
2029{
2030 struct nfs_server *server;
2031 struct nfs4_client *clp = NULL;
2032 struct rpc_xprt *xprt = NULL;
2033 struct rpc_clnt *clnt = NULL;
2034 struct rpc_timeout timeparms;
2035 rpc_authflavor_t authflavour;
Chuck Levereab5c082005-08-11 16:25:14 -04002036 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 sb->s_blocksize_bits = 0;
2039 sb->s_blocksize = 0;
2040 server = NFS_SB(sb);
2041 if (data->rsize != 0)
2042 server->rsize = nfs_block_size(data->rsize, NULL);
2043 if (data->wsize != 0)
2044 server->wsize = nfs_block_size(data->wsize, NULL);
2045 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
2046 server->caps = NFS_CAP_ATOMIC_OPEN;
2047
2048 server->acregmin = data->acregmin*HZ;
2049 server->acregmax = data->acregmax*HZ;
2050 server->acdirmin = data->acdirmin*HZ;
2051 server->acdirmax = data->acdirmax*HZ;
2052
2053 server->rpc_ops = &nfs_v4_clientops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Chuck Levereab5c082005-08-11 16:25:14 -04002055 nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Chuck Lever7a480e22006-03-20 13:44:12 -05002057 server->retrans_timeo = timeparms.to_initval;
2058 server->retrans_count = timeparms.to_retries;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 clp = nfs4_get_client(&server->addr.sin_addr);
2061 if (!clp) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002062 dprintk("%s: failed to create NFS4 client.\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EIO;
2064 }
2065
2066 /* Now create transport and client */
2067 authflavour = RPC_AUTH_UNIX;
2068 if (data->auth_flavourlen != 0) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002069 if (data->auth_flavourlen != 1) {
2070 dprintk("%s: Invalid number of RPC auth flavours %d.\n",
2071 __FUNCTION__, data->auth_flavourlen);
2072 err = -EINVAL;
2073 goto out_fail;
2074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (copy_from_user(&authflavour, data->auth_flavours, sizeof(authflavour))) {
2076 err = -EFAULT;
2077 goto out_fail;
2078 }
2079 }
2080
2081 down_write(&clp->cl_sem);
J. Bruce Fields6a192752005-06-22 17:16:23 +00002082 if (IS_ERR(clp->cl_rpcclient)) {
Chuck Levereab5c082005-08-11 16:25:14 -04002083 xprt = xprt_create_proto(data->proto, &server->addr, &timeparms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (IS_ERR(xprt)) {
2085 up_write(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 err = PTR_ERR(xprt);
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002087 dprintk("%s: cannot create RPC transport. Error = %d\n",
2088 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 goto out_fail;
2090 }
2091 clnt = rpc_create_client(xprt, server->hostname, &nfs_program,
2092 server->rpc_ops->version, authflavour);
2093 if (IS_ERR(clnt)) {
2094 up_write(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 err = PTR_ERR(clnt);
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002096 dprintk("%s: cannot create RPC client. Error = %d\n",
2097 __FUNCTION__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out_fail;
2099 }
2100 clnt->cl_intr = 1;
2101 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 clp->cl_rpcclient = clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 memcpy(clp->cl_ipaddr, server->ip_addr, sizeof(clp->cl_ipaddr));
2104 nfs_idmap_new(clp);
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
2107 clnt = rpc_clone_client(clp->cl_rpcclient);
2108 if (!IS_ERR(clnt))
2109 server->nfs4_state = clp;
2110 up_write(&clp->cl_sem);
2111 clp = NULL;
2112
2113 if (IS_ERR(clnt)) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002114 err = PTR_ERR(clnt);
2115 dprintk("%s: cannot create RPC client. Error = %d\n",
2116 __FUNCTION__, err);
2117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
2120 server->client = clnt;
2121
2122 if (server->nfs4_state->cl_idmap == NULL) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002123 dprintk("%s: failed to create idmapper.\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return -ENOMEM;
2125 }
2126
2127 if (clnt->cl_auth->au_flavor != authflavour) {
J. Bruce Fields6a192752005-06-22 17:16:23 +00002128 struct rpc_auth *auth;
2129
2130 auth = rpcauth_create(authflavour, clnt);
2131 if (IS_ERR(auth)) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002132 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
J. Bruce Fields6a192752005-06-22 17:16:23 +00002133 return PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135 }
2136
2137 sb->s_time_gran = 1;
2138
2139 sb->s_op = &nfs4_sops;
2140 err = nfs_sb_init(sb, authflavour);
2141 if (err == 0)
2142 return 0;
2143out_fail:
2144 if (clp)
2145 nfs4_put_client(clp);
2146 return err;
2147}
2148
2149static int nfs4_compare_super(struct super_block *sb, void *data)
2150{
2151 struct nfs_server *server = data;
2152 struct nfs_server *old = NFS_SB(sb);
2153
2154 if (strcmp(server->hostname, old->hostname) != 0)
2155 return 0;
2156 if (strcmp(server->mnt_path, old->mnt_path) != 0)
2157 return 0;
2158 return 1;
2159}
2160
2161static void *
2162nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
2163{
2164 void *p = NULL;
2165
2166 if (!src->len)
2167 return ERR_PTR(-EINVAL);
2168 if (src->len < maxlen)
2169 maxlen = src->len;
2170 if (dst == NULL) {
2171 p = dst = kmalloc(maxlen + 1, GFP_KERNEL);
2172 if (p == NULL)
2173 return ERR_PTR(-ENOMEM);
2174 }
2175 if (copy_from_user(dst, src->data, maxlen)) {
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002176 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return ERR_PTR(-EFAULT);
2178 }
2179 dst[maxlen] = '\0';
2180 return dst;
2181}
2182
2183static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
2184 int flags, const char *dev_name, void *raw_data)
2185{
2186 int error;
2187 struct nfs_server *server;
2188 struct super_block *s;
2189 struct nfs4_mount_data *data = raw_data;
2190 void *p;
2191
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002192 if (data == NULL) {
2193 dprintk("%s: missing data argument\n", __FUNCTION__);
2194 return ERR_PTR(-EINVAL);
2195 }
2196 if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
2197 dprintk("%s: bad mount version\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return ERR_PTR(-EINVAL);
2199 }
2200
Eric Sesterhennbd647542006-03-20 13:44:10 -05002201 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!server)
2203 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* Zero out the NFS state stuff */
2205 init_nfsv4_state(server);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002206 server->client = server->client_sys = server->client_acl = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 p = nfs_copy_user_string(NULL, &data->hostname, 256);
2209 if (IS_ERR(p))
2210 goto out_err;
2211 server->hostname = p;
2212
2213 p = nfs_copy_user_string(NULL, &data->mnt_path, 1024);
2214 if (IS_ERR(p))
2215 goto out_err;
2216 server->mnt_path = p;
2217
2218 p = nfs_copy_user_string(server->ip_addr, &data->client_addr,
2219 sizeof(server->ip_addr) - 1);
2220 if (IS_ERR(p))
2221 goto out_err;
2222
2223 /* We now require that the mount process passes the remote address */
2224 if (data->host_addrlen != sizeof(server->addr)) {
2225 s = ERR_PTR(-EINVAL);
2226 goto out_free;
2227 }
2228 if (copy_from_user(&server->addr, data->host_addr, sizeof(server->addr))) {
2229 s = ERR_PTR(-EFAULT);
2230 goto out_free;
2231 }
2232 if (server->addr.sin_family != AF_INET ||
2233 server->addr.sin_addr.s_addr == INADDR_ANY) {
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002234 dprintk("%s: mount program didn't pass remote IP address!\n",
2235 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 s = ERR_PTR(-EINVAL);
2237 goto out_free;
2238 }
2239
Trond Myklebust9085bbc2005-06-22 17:16:20 +00002240 /* Fire up rpciod if not yet running */
2241 s = ERR_PTR(rpciod_up());
2242 if (IS_ERR(s)) {
2243 dprintk("%s: couldn't start rpciod! Error = %ld\n",
2244 __FUNCTION__, PTR_ERR(s));
2245 goto out_free;
2246 }
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 s = sget(fs_type, nfs4_compare_super, nfs_set_super, server);
2249
2250 if (IS_ERR(s) || s->s_root)
2251 goto out_free;
2252
2253 s->s_flags = flags;
2254
Theodore Ts'o9b04c992006-03-24 03:15:10 -08002255 error = nfs4_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (error) {
2257 up_write(&s->s_umount);
2258 deactivate_super(s);
2259 return ERR_PTR(error);
2260 }
2261 s->s_flags |= MS_ACTIVE;
2262 return s;
2263out_err:
2264 s = (struct super_block *)p;
2265out_free:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002266 kfree(server->mnt_path);
2267 kfree(server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 kfree(server);
2269 return s;
2270}
2271
2272static void nfs4_kill_super(struct super_block *sb)
2273{
2274 struct nfs_server *server = NFS_SB(sb);
2275
2276 nfs_return_all_delegations(sb);
2277 kill_anon_super(sb);
2278
2279 nfs4_renewd_prepare_shutdown(server);
2280
2281 if (server->client != NULL && !IS_ERR(server->client))
2282 rpc_shutdown_client(server->client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 destroy_nfsv4_state(server);
2285
Trond Myklebust967b9282006-03-20 13:44:09 -05002286 rpciod_down();
2287
Trond Myklebust01d0ae82006-03-20 13:44:48 -05002288 nfs_free_iostats(server->io_stats);
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002289 kfree(server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 kfree(server);
2291}
2292
2293static struct file_system_type nfs4_fs_type = {
2294 .owner = THIS_MODULE,
2295 .name = "nfs4",
2296 .get_sb = nfs4_get_sb,
2297 .kill_sb = nfs4_kill_super,
2298 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
2299};
2300
Trond Myklebusta72b4422006-01-03 09:55:41 +01002301static const int nfs_set_port_min = 0;
2302static const int nfs_set_port_max = 65535;
2303static int param_set_port(const char *val, struct kernel_param *kp)
2304{
2305 char *endp;
2306 int num = simple_strtol(val, &endp, 0);
2307 if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
2308 return -EINVAL;
2309 *((int *)kp->arg) = num;
2310 return 0;
2311}
2312
2313module_param_call(callback_tcpport, param_set_port, param_get_int,
2314 &nfs_callback_set_tcpport, 0644);
2315
Trond Myklebust58df0952006-01-03 09:55:57 +01002316static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
2317{
2318 char *endp;
2319 int num = simple_strtol(val, &endp, 0);
2320 int jif = num * HZ;
2321 if (endp == val || *endp || num < 0 || jif < num)
2322 return -EINVAL;
2323 *((int *)kp->arg) = jif;
2324 return 0;
2325}
2326
2327module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
2328 &nfs_idmap_cache_timeout, 0644);
2329
Trond Myklebust55a97592006-06-09 09:34:19 -04002330/* Constructs the SERVER-side path */
2331static inline char *nfs4_path(const struct dentry *dentry, char *buffer, ssize_t buflen)
2332{
2333 return nfs_path(NFS_SB(dentry->d_sb)->mnt_path, dentry, buffer, buflen);
2334}
2335
2336static inline char *nfs4_dup_path(const struct dentry *dentry)
2337{
2338 char *page = (char *) __get_free_page(GFP_USER);
2339 char *path;
2340
2341 path = nfs4_path(dentry, page, PAGE_SIZE);
2342 if (!IS_ERR(path)) {
2343 int len = PAGE_SIZE + page - path;
2344 char *tmp = path;
2345
2346 path = kmalloc(len, GFP_KERNEL);
2347 if (path)
2348 memcpy(path, tmp, len);
2349 else
2350 path = ERR_PTR(-ENOMEM);
2351 }
2352 free_page((unsigned long)page);
2353 return path;
2354}
2355
2356static struct super_block *nfs4_clone_client(struct nfs_server *server, struct nfs_clone_mount *data)
2357{
2358 const struct dentry *dentry = data->dentry;
2359 struct nfs4_client *clp = server->nfs4_state;
2360 struct super_block *sb;
2361
2362 server->mnt_path = nfs4_dup_path(dentry);
2363 if (IS_ERR(server->mnt_path)) {
2364 sb = (struct super_block *)server->mnt_path;
2365 goto err;
2366 }
2367 sb = sget(&nfs4_fs_type, nfs4_compare_super, nfs_set_super, server);
2368 if (IS_ERR(sb) || sb->s_root)
2369 goto free_path;
2370 nfs4_server_capabilities(server, &server->fh);
2371
2372 down_write(&clp->cl_sem);
2373 atomic_inc(&clp->cl_count);
2374 list_add_tail(&server->nfs4_siblings, &clp->cl_superblocks);
2375 up_write(&clp->cl_sem);
2376 return sb;
2377free_path:
2378 kfree(server->mnt_path);
2379err:
2380 server->mnt_path = NULL;
2381 return sb;
2382}
2383
2384static struct super_block *nfs_clone_nfs4_sb(struct file_system_type *fs_type,
2385 int flags, const char *dev_name, void *raw_data)
2386{
2387 struct nfs_clone_mount *data = raw_data;
2388 return nfs_clone_generic_sb(data, nfs4_clone_client);
2389}
2390
2391static struct file_system_type clone_nfs4_fs_type = {
2392 .owner = THIS_MODULE,
2393 .name = "nfs",
2394 .get_sb = nfs_clone_nfs4_sb,
2395 .kill_sb = nfs4_kill_super,
2396 .fs_flags = FS_ODD_RENAME|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
2397};
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399#define nfs4_init_once(nfsi) \
2400 do { \
2401 INIT_LIST_HEAD(&(nfsi)->open_states); \
2402 nfsi->delegation = NULL; \
2403 nfsi->delegation_state = 0; \
2404 init_rwsem(&nfsi->rwsem); \
2405 } while(0)
Trond Myklebusta72b4422006-01-03 09:55:41 +01002406
2407static inline int register_nfs4fs(void)
2408{
2409 int ret;
2410
2411 ret = nfs_register_sysctl();
2412 if (ret != 0)
2413 return ret;
2414 ret = register_filesystem(&nfs4_fs_type);
2415 if (ret != 0)
2416 nfs_unregister_sysctl();
2417 return ret;
2418}
2419
2420static inline void unregister_nfs4fs(void)
2421{
2422 unregister_filesystem(&nfs4_fs_type);
2423 nfs_unregister_sysctl();
2424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425#else
Trond Myklebust55a97592006-06-09 09:34:19 -04002426#define nfs4_clone_client(a,b) ERR_PTR(-EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427#define nfs4_init_once(nfsi) \
2428 do { } while (0)
2429#define register_nfs4fs() (0)
2430#define unregister_nfs4fs()
2431#endif
2432
Trond Myklebust55a97592006-06-09 09:34:19 -04002433static inline char *nfs_devname(const struct vfsmount *mnt_parent,
2434 const struct dentry *dentry,
2435 char *buffer, ssize_t buflen)
2436{
2437 return nfs_path(mnt_parent->mnt_devname, dentry, buffer, buflen);
2438}
2439
2440/**
2441 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
2442 * @mnt_parent - mountpoint of parent directory
2443 * @dentry - parent directory
2444 * @fh - filehandle for new root dentry
2445 * @fattr - attributes for new root inode
2446 *
2447 */
2448struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
2449 const struct dentry *dentry, struct nfs_fh *fh,
2450 struct nfs_fattr *fattr)
2451{
2452 struct nfs_clone_mount mountdata = {
2453 .sb = mnt_parent->mnt_sb,
2454 .dentry = dentry,
2455 .fh = fh,
2456 .fattr = fattr,
2457 };
2458 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
2459 char *page = (char *) __get_free_page(GFP_USER);
2460 char *devname;
2461
2462 dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
2463 dentry->d_parent->d_name.name,
2464 dentry->d_name.name);
2465 if (page == NULL)
2466 goto out;
2467 devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
2468 mnt = (struct vfsmount *)devname;
2469 if (IS_ERR(devname))
2470 goto free_page;
2471 switch (NFS_SB(mnt_parent->mnt_sb)->rpc_ops->version) {
2472 case 2:
2473 case 3:
2474 mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, &mountdata);
2475 break;
2476 case 4:
2477 mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, &mountdata);
2478 break;
2479 default:
2480 BUG();
2481 }
2482free_page:
2483 free_page((unsigned long)page);
2484out:
2485 dprintk("%s: done\n", __FUNCTION__);
2486 return mnt;
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489extern int nfs_init_nfspagecache(void);
2490extern void nfs_destroy_nfspagecache(void);
2491extern int nfs_init_readpagecache(void);
2492extern void nfs_destroy_readpagecache(void);
2493extern int nfs_init_writepagecache(void);
2494extern void nfs_destroy_writepagecache(void);
2495#ifdef CONFIG_NFS_DIRECTIO
2496extern int nfs_init_directcache(void);
2497extern void nfs_destroy_directcache(void);
2498#endif
2499
2500static kmem_cache_t * nfs_inode_cachep;
2501
2502static struct inode *nfs_alloc_inode(struct super_block *sb)
2503{
2504 struct nfs_inode *nfsi;
2505 nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, SLAB_KERNEL);
2506 if (!nfsi)
2507 return NULL;
Chuck Lever55296802005-08-18 11:24:09 -07002508 nfsi->flags = 0UL;
2509 nfsi->cache_validity = 0UL;
Steve Dickson223db122005-11-30 09:25:33 -05002510 nfsi->cache_change_attribute = jiffies;
Trond Myklebust458818e2005-06-22 17:16:27 +00002511#ifdef CONFIG_NFS_V3_ACL
2512 nfsi->acl_access = ERR_PTR(-EAGAIN);
2513 nfsi->acl_default = ERR_PTR(-EAGAIN);
2514#endif
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002515#ifdef CONFIG_NFS_V4
2516 nfsi->nfs4_acl = NULL;
2517#endif /* CONFIG_NFS_V4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return &nfsi->vfs_inode;
2519}
2520
2521static void nfs_destroy_inode(struct inode *inode)
2522{
2523 kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
2524}
2525
2526static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
2527{
2528 struct nfs_inode *nfsi = (struct nfs_inode *) foo;
2529
2530 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2531 SLAB_CTOR_CONSTRUCTOR) {
2532 inode_init_once(&nfsi->vfs_inode);
2533 spin_lock_init(&nfsi->req_lock);
2534 INIT_LIST_HEAD(&nfsi->dirty);
2535 INIT_LIST_HEAD(&nfsi->commit);
2536 INIT_LIST_HEAD(&nfsi->open_files);
2537 INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
2538 atomic_set(&nfsi->data_updates, 0);
2539 nfsi->ndirty = 0;
2540 nfsi->ncommit = 0;
2541 nfsi->npages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 nfs4_init_once(nfsi);
2543 }
2544}
2545
Adrian Bunk75c96f82005-05-05 16:16:09 -07002546static int nfs_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
2549 sizeof(struct nfs_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08002550 0, (SLAB_RECLAIM_ACCOUNT|
2551 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 init_once, NULL);
2553 if (nfs_inode_cachep == NULL)
2554 return -ENOMEM;
2555
2556 return 0;
2557}
2558
Adrian Bunk75c96f82005-05-05 16:16:09 -07002559static void nfs_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 if (kmem_cache_destroy(nfs_inode_cachep))
2562 printk(KERN_INFO "nfs_inode_cache: not all structures were freed\n");
2563}
2564
2565/*
2566 * Initialize NFS
2567 */
2568static int __init init_nfs_fs(void)
2569{
2570 int err;
2571
2572 err = nfs_init_nfspagecache();
2573 if (err)
2574 goto out4;
2575
2576 err = nfs_init_inodecache();
2577 if (err)
2578 goto out3;
2579
2580 err = nfs_init_readpagecache();
2581 if (err)
2582 goto out2;
2583
2584 err = nfs_init_writepagecache();
2585 if (err)
2586 goto out1;
2587
2588#ifdef CONFIG_NFS_DIRECTIO
2589 err = nfs_init_directcache();
2590 if (err)
2591 goto out0;
2592#endif
2593
2594#ifdef CONFIG_PROC_FS
2595 rpc_proc_register(&nfs_rpcstat);
2596#endif
2597 err = register_filesystem(&nfs_fs_type);
2598 if (err)
2599 goto out;
2600 if ((err = register_nfs4fs()) != 0)
2601 goto out;
2602 return 0;
2603out:
2604#ifdef CONFIG_PROC_FS
2605 rpc_proc_unregister("nfs");
2606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607#ifdef CONFIG_NFS_DIRECTIO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 nfs_destroy_directcache();
Chuck Lever6b59a752005-11-30 18:08:19 -05002609out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610#endif
Chuck Lever6b59a752005-11-30 18:08:19 -05002611 nfs_destroy_writepagecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612out1:
2613 nfs_destroy_readpagecache();
2614out2:
2615 nfs_destroy_inodecache();
2616out3:
2617 nfs_destroy_nfspagecache();
2618out4:
2619 return err;
2620}
2621
2622static void __exit exit_nfs_fs(void)
2623{
2624#ifdef CONFIG_NFS_DIRECTIO
2625 nfs_destroy_directcache();
2626#endif
2627 nfs_destroy_writepagecache();
2628 nfs_destroy_readpagecache();
2629 nfs_destroy_inodecache();
2630 nfs_destroy_nfspagecache();
2631#ifdef CONFIG_PROC_FS
2632 rpc_proc_unregister("nfs");
2633#endif
2634 unregister_filesystem(&nfs_fs_type);
2635 unregister_nfs4fs();
2636}
2637
2638/* Not quite true; I just maintain it */
2639MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2640MODULE_LICENSE("GPL");
2641
2642module_init(init_nfs_fs)
2643module_exit(exit_nfs_fs)