netfilter: tcp conntrack: fix unacknowledged data detection with NAT
[linux/fpc-iii.git] / fs / nfs / proc.c
blob7be72d90d49de3191899c8be4626d7cfc385d148
1 /*
2 * linux/fs/nfs/proc.c
4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 * OS-independent nfs remote procedure call functions
8 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
9 * so at last we can have decent(ish) throughput off a
10 * Sun server.
12 * Coding optimized and cleaned up by Florian La Roche.
13 * Note: Error returns are optimized for NFS_OK, which isn't translated via
14 * nfs_stat_to_errno(), but happens to be already the right return code.
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
19 * Feel free to fix it and mail me the diffs if it worries you.
21 * Completely rewritten to support the new RPC call interface;
22 * rewrote and moved the entire XDR stuff to xdr.c
23 * --Olaf Kirch June 1996
25 * The code below initializes all auto variables explicitly, otherwise
26 * it will fail to work as a module (gcc generates a memset call for an
27 * incomplete struct).
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/mm.h>
35 #include <linux/utsname.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 #include <linux/in.h>
39 #include <linux/pagemap.h>
40 #include <linux/sunrpc/clnt.h>
41 #include <linux/nfs.h>
42 #include <linux/nfs2.h>
43 #include <linux/nfs_fs.h>
44 #include <linux/nfs_page.h>
45 #include <linux/lockd/bind.h>
46 #include "internal.h"
48 #define NFSDBG_FACILITY NFSDBG_PROC
51 * Bare-bones access to getattr: this is for nfs_read_super.
53 static int
54 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
55 struct nfs_fsinfo *info)
57 struct nfs_fattr *fattr = info->fattr;
58 struct nfs2_fsstat fsinfo;
59 struct rpc_message msg = {
60 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
61 .rpc_argp = fhandle,
62 .rpc_resp = fattr,
64 int status;
66 dprintk("%s: call getattr\n", __func__);
67 nfs_fattr_init(fattr);
68 status = rpc_call_sync(server->client, &msg, 0);
69 /* Retry with default authentication if different */
70 if (status && server->nfs_client->cl_rpcclient != server->client)
71 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
72 dprintk("%s: reply getattr: %d\n", __func__, status);
73 if (status)
74 return status;
75 dprintk("%s: call statfs\n", __func__);
76 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
77 msg.rpc_resp = &fsinfo;
78 status = rpc_call_sync(server->client, &msg, 0);
79 /* Retry with default authentication if different */
80 if (status && server->nfs_client->cl_rpcclient != server->client)
81 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
82 dprintk("%s: reply statfs: %d\n", __func__, status);
83 if (status)
84 return status;
85 info->rtmax = NFS_MAXDATA;
86 info->rtpref = fsinfo.tsize;
87 info->rtmult = fsinfo.bsize;
88 info->wtmax = NFS_MAXDATA;
89 info->wtpref = fsinfo.tsize;
90 info->wtmult = fsinfo.bsize;
91 info->dtpref = fsinfo.tsize;
92 info->maxfilesize = 0x7FFFFFFF;
93 info->lease_time = 0;
94 return 0;
98 * One function for each procedure in the NFS protocol.
100 static int
101 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
104 struct rpc_message msg = {
105 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
106 .rpc_argp = fhandle,
107 .rpc_resp = fattr,
109 int status;
111 dprintk("NFS call getattr\n");
112 nfs_fattr_init(fattr);
113 status = rpc_call_sync(server->client, &msg, 0);
114 dprintk("NFS reply getattr: %d\n", status);
115 return status;
118 static int
119 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
120 struct iattr *sattr)
122 struct inode *inode = dentry->d_inode;
123 struct nfs_sattrargs arg = {
124 .fh = NFS_FH(inode),
125 .sattr = sattr
127 struct rpc_message msg = {
128 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
129 .rpc_argp = &arg,
130 .rpc_resp = fattr,
132 int status;
134 /* Mask out the non-modebit related stuff from attr->ia_mode */
135 sattr->ia_mode &= S_IALLUGO;
137 dprintk("NFS call setattr\n");
138 if (sattr->ia_valid & ATTR_FILE)
139 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
140 nfs_fattr_init(fattr);
141 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
142 if (status == 0)
143 nfs_setattr_update_inode(inode, sattr);
144 dprintk("NFS reply setattr: %d\n", status);
145 return status;
148 static int
149 nfs_proc_lookup(struct inode *dir, struct qstr *name,
150 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
152 struct nfs_diropargs arg = {
153 .fh = NFS_FH(dir),
154 .name = name->name,
155 .len = name->len
157 struct nfs_diropok res = {
158 .fh = fhandle,
159 .fattr = fattr
161 struct rpc_message msg = {
162 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
163 .rpc_argp = &arg,
164 .rpc_resp = &res,
166 int status;
168 dprintk("NFS call lookup %s\n", name->name);
169 nfs_fattr_init(fattr);
170 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
171 dprintk("NFS reply lookup: %d\n", status);
172 return status;
175 static int nfs_proc_readlink(struct inode *inode, struct page *page,
176 unsigned int pgbase, unsigned int pglen)
178 struct nfs_readlinkargs args = {
179 .fh = NFS_FH(inode),
180 .pgbase = pgbase,
181 .pglen = pglen,
182 .pages = &page
184 struct rpc_message msg = {
185 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
186 .rpc_argp = &args,
188 int status;
190 dprintk("NFS call readlink\n");
191 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
192 dprintk("NFS reply readlink: %d\n", status);
193 return status;
196 static int
197 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
198 int flags, struct nameidata *nd)
200 struct nfs_fh fhandle;
201 struct nfs_fattr fattr;
202 struct nfs_createargs arg = {
203 .fh = NFS_FH(dir),
204 .name = dentry->d_name.name,
205 .len = dentry->d_name.len,
206 .sattr = sattr
208 struct nfs_diropok res = {
209 .fh = &fhandle,
210 .fattr = &fattr
212 struct rpc_message msg = {
213 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
214 .rpc_argp = &arg,
215 .rpc_resp = &res,
217 int status;
219 nfs_fattr_init(&fattr);
220 dprintk("NFS call create %s\n", dentry->d_name.name);
221 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
222 nfs_mark_for_revalidate(dir);
223 if (status == 0)
224 status = nfs_instantiate(dentry, &fhandle, &fattr);
225 dprintk("NFS reply create: %d\n", status);
226 return status;
230 * In NFSv2, mknod is grafted onto the create call.
232 static int
233 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
234 dev_t rdev)
236 struct nfs_fh fhandle;
237 struct nfs_fattr fattr;
238 struct nfs_createargs arg = {
239 .fh = NFS_FH(dir),
240 .name = dentry->d_name.name,
241 .len = dentry->d_name.len,
242 .sattr = sattr
244 struct nfs_diropok res = {
245 .fh = &fhandle,
246 .fattr = &fattr
248 struct rpc_message msg = {
249 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
250 .rpc_argp = &arg,
251 .rpc_resp = &res,
253 int status, mode;
255 dprintk("NFS call mknod %s\n", dentry->d_name.name);
257 mode = sattr->ia_mode;
258 if (S_ISFIFO(mode)) {
259 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
260 sattr->ia_valid &= ~ATTR_SIZE;
261 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
262 sattr->ia_valid |= ATTR_SIZE;
263 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
266 nfs_fattr_init(&fattr);
267 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
268 nfs_mark_for_revalidate(dir);
270 if (status == -EINVAL && S_ISFIFO(mode)) {
271 sattr->ia_mode = mode;
272 nfs_fattr_init(&fattr);
273 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
275 if (status == 0)
276 status = nfs_instantiate(dentry, &fhandle, &fattr);
277 dprintk("NFS reply mknod: %d\n", status);
278 return status;
281 static int
282 nfs_proc_remove(struct inode *dir, struct qstr *name)
284 struct nfs_removeargs arg = {
285 .fh = NFS_FH(dir),
286 .name.len = name->len,
287 .name.name = name->name,
289 struct rpc_message msg = {
290 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
291 .rpc_argp = &arg,
293 int status;
295 dprintk("NFS call remove %s\n", name->name);
296 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
297 nfs_mark_for_revalidate(dir);
299 dprintk("NFS reply remove: %d\n", status);
300 return status;
303 static void
304 nfs_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
306 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
309 static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
311 nfs_mark_for_revalidate(dir);
312 return 1;
315 static int
316 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
317 struct inode *new_dir, struct qstr *new_name)
319 struct nfs_renameargs arg = {
320 .fromfh = NFS_FH(old_dir),
321 .fromname = old_name->name,
322 .fromlen = old_name->len,
323 .tofh = NFS_FH(new_dir),
324 .toname = new_name->name,
325 .tolen = new_name->len
327 struct rpc_message msg = {
328 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
329 .rpc_argp = &arg,
331 int status;
333 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
334 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
335 nfs_mark_for_revalidate(old_dir);
336 nfs_mark_for_revalidate(new_dir);
337 dprintk("NFS reply rename: %d\n", status);
338 return status;
341 static int
342 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
344 struct nfs_linkargs arg = {
345 .fromfh = NFS_FH(inode),
346 .tofh = NFS_FH(dir),
347 .toname = name->name,
348 .tolen = name->len
350 struct rpc_message msg = {
351 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
352 .rpc_argp = &arg,
354 int status;
356 dprintk("NFS call link %s\n", name->name);
357 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
358 nfs_mark_for_revalidate(inode);
359 nfs_mark_for_revalidate(dir);
360 dprintk("NFS reply link: %d\n", status);
361 return status;
364 static int
365 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
366 unsigned int len, struct iattr *sattr)
368 struct nfs_fh fhandle;
369 struct nfs_fattr fattr;
370 struct nfs_symlinkargs arg = {
371 .fromfh = NFS_FH(dir),
372 .fromname = dentry->d_name.name,
373 .fromlen = dentry->d_name.len,
374 .pages = &page,
375 .pathlen = len,
376 .sattr = sattr
378 struct rpc_message msg = {
379 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
380 .rpc_argp = &arg,
382 int status;
384 if (len > NFS2_MAXPATHLEN)
385 return -ENAMETOOLONG;
387 dprintk("NFS call symlink %s\n", dentry->d_name.name);
389 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
390 nfs_mark_for_revalidate(dir);
393 * V2 SYMLINK requests don't return any attributes. Setting the
394 * filehandle size to zero indicates to nfs_instantiate that it
395 * should fill in the data with a LOOKUP call on the wire.
397 if (status == 0) {
398 nfs_fattr_init(&fattr);
399 fhandle.size = 0;
400 status = nfs_instantiate(dentry, &fhandle, &fattr);
403 dprintk("NFS reply symlink: %d\n", status);
404 return status;
407 static int
408 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
410 struct nfs_fh fhandle;
411 struct nfs_fattr fattr;
412 struct nfs_createargs arg = {
413 .fh = NFS_FH(dir),
414 .name = dentry->d_name.name,
415 .len = dentry->d_name.len,
416 .sattr = sattr
418 struct nfs_diropok res = {
419 .fh = &fhandle,
420 .fattr = &fattr
422 struct rpc_message msg = {
423 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
424 .rpc_argp = &arg,
425 .rpc_resp = &res,
427 int status;
429 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
430 nfs_fattr_init(&fattr);
431 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
432 nfs_mark_for_revalidate(dir);
433 if (status == 0)
434 status = nfs_instantiate(dentry, &fhandle, &fattr);
435 dprintk("NFS reply mkdir: %d\n", status);
436 return status;
439 static int
440 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
442 struct nfs_diropargs arg = {
443 .fh = NFS_FH(dir),
444 .name = name->name,
445 .len = name->len
447 struct rpc_message msg = {
448 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
449 .rpc_argp = &arg,
451 int status;
453 dprintk("NFS call rmdir %s\n", name->name);
454 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
455 nfs_mark_for_revalidate(dir);
456 dprintk("NFS reply rmdir: %d\n", status);
457 return status;
461 * The READDIR implementation is somewhat hackish - we pass a temporary
462 * buffer to the encode function, which installs it in the receive
463 * the receive iovec. The decode function just parses the reply to make
464 * sure it is syntactically correct; the entries itself are decoded
465 * from nfs_readdir by calling the decode_entry function directly.
467 static int
468 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
469 u64 cookie, struct page *page, unsigned int count, int plus)
471 struct inode *dir = dentry->d_inode;
472 struct nfs_readdirargs arg = {
473 .fh = NFS_FH(dir),
474 .cookie = cookie,
475 .count = count,
476 .pages = &page,
478 struct rpc_message msg = {
479 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
480 .rpc_argp = &arg,
481 .rpc_cred = cred,
483 int status;
485 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
486 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
488 nfs_invalidate_atime(dir);
490 dprintk("NFS reply readdir: %d\n", status);
491 return status;
494 static int
495 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
496 struct nfs_fsstat *stat)
498 struct nfs2_fsstat fsinfo;
499 struct rpc_message msg = {
500 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
501 .rpc_argp = fhandle,
502 .rpc_resp = &fsinfo,
504 int status;
506 dprintk("NFS call statfs\n");
507 nfs_fattr_init(stat->fattr);
508 status = rpc_call_sync(server->client, &msg, 0);
509 dprintk("NFS reply statfs: %d\n", status);
510 if (status)
511 goto out;
512 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
513 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
514 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
515 stat->tfiles = 0;
516 stat->ffiles = 0;
517 stat->afiles = 0;
518 out:
519 return status;
522 static int
523 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
524 struct nfs_fsinfo *info)
526 struct nfs2_fsstat fsinfo;
527 struct rpc_message msg = {
528 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
529 .rpc_argp = fhandle,
530 .rpc_resp = &fsinfo,
532 int status;
534 dprintk("NFS call fsinfo\n");
535 nfs_fattr_init(info->fattr);
536 status = rpc_call_sync(server->client, &msg, 0);
537 dprintk("NFS reply fsinfo: %d\n", status);
538 if (status)
539 goto out;
540 info->rtmax = NFS_MAXDATA;
541 info->rtpref = fsinfo.tsize;
542 info->rtmult = fsinfo.bsize;
543 info->wtmax = NFS_MAXDATA;
544 info->wtpref = fsinfo.tsize;
545 info->wtmult = fsinfo.bsize;
546 info->dtpref = fsinfo.tsize;
547 info->maxfilesize = 0x7FFFFFFF;
548 info->lease_time = 0;
549 out:
550 return status;
553 static int
554 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
555 struct nfs_pathconf *info)
557 info->max_link = 0;
558 info->max_namelen = NFS2_MAXNAMLEN;
559 return 0;
562 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
564 nfs_invalidate_atime(data->inode);
565 if (task->tk_status >= 0) {
566 nfs_refresh_inode(data->inode, data->res.fattr);
567 /* Emulate the eof flag, which isn't normally needed in NFSv2
568 * as it is guaranteed to always return the file attributes
570 if (data->args.offset + data->args.count >= data->res.fattr->size)
571 data->res.eof = 1;
573 return 0;
576 static void nfs_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
578 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
581 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
583 if (task->tk_status >= 0)
584 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
585 return 0;
588 static void nfs_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
590 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
591 data->args.stable = NFS_FILE_SYNC;
592 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
595 static void
596 nfs_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
598 BUG();
601 static int
602 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
604 struct inode *inode = filp->f_path.dentry->d_inode;
606 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
609 /* Helper functions for NFS lock bounds checking */
610 #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
611 static int nfs_lock_check_bounds(const struct file_lock *fl)
613 __s32 start, end;
615 start = (__s32)fl->fl_start;
616 if ((loff_t)start != fl->fl_start)
617 goto out_einval;
619 if (fl->fl_end != OFFSET_MAX) {
620 end = (__s32)fl->fl_end;
621 if ((loff_t)end != fl->fl_end)
622 goto out_einval;
623 } else
624 end = NFS_LOCK32_OFFSET_MAX;
626 if (start < 0 || start > end)
627 goto out_einval;
628 return 0;
629 out_einval:
630 return -EINVAL;
633 const struct nfs_rpc_ops nfs_v2_clientops = {
634 .version = 2, /* protocol version */
635 .dentry_ops = &nfs_dentry_operations,
636 .dir_inode_ops = &nfs_dir_inode_operations,
637 .file_inode_ops = &nfs_file_inode_operations,
638 .getroot = nfs_proc_get_root,
639 .getattr = nfs_proc_getattr,
640 .setattr = nfs_proc_setattr,
641 .lookup = nfs_proc_lookup,
642 .access = NULL, /* access */
643 .readlink = nfs_proc_readlink,
644 .create = nfs_proc_create,
645 .remove = nfs_proc_remove,
646 .unlink_setup = nfs_proc_unlink_setup,
647 .unlink_done = nfs_proc_unlink_done,
648 .rename = nfs_proc_rename,
649 .link = nfs_proc_link,
650 .symlink = nfs_proc_symlink,
651 .mkdir = nfs_proc_mkdir,
652 .rmdir = nfs_proc_rmdir,
653 .readdir = nfs_proc_readdir,
654 .mknod = nfs_proc_mknod,
655 .statfs = nfs_proc_statfs,
656 .fsinfo = nfs_proc_fsinfo,
657 .pathconf = nfs_proc_pathconf,
658 .decode_dirent = nfs_decode_dirent,
659 .read_setup = nfs_proc_read_setup,
660 .read_done = nfs_read_done,
661 .write_setup = nfs_proc_write_setup,
662 .write_done = nfs_write_done,
663 .commit_setup = nfs_proc_commit_setup,
664 .lock = nfs_proc_lock,
665 .lock_check_bounds = nfs_lock_check_bounds,
666 .close_context = nfs_close_context,