[NETFILTER]: {ip,ip6,arp}_tables: fix exponential worst-case search for loops
[pv_ops_mirror.git] / fs / nfs / proc.c
blob560536ad74a49fd500077882b211655c26424b85
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 <linux/smp_lock.h>
47 #include "internal.h"
49 #define NFSDBG_FACILITY NFSDBG_PROC
52 * Bare-bones access to getattr: this is for nfs_read_super.
54 static int
55 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
56 struct nfs_fsinfo *info)
58 struct nfs_fattr *fattr = info->fattr;
59 struct nfs2_fsstat fsinfo;
60 struct rpc_message msg = {
61 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
62 .rpc_argp = fhandle,
63 .rpc_resp = fattr,
65 int status;
67 dprintk("%s: call getattr\n", __FUNCTION__);
68 nfs_fattr_init(fattr);
69 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
70 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
71 if (status)
72 return status;
73 dprintk("%s: call statfs\n", __FUNCTION__);
74 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
75 msg.rpc_resp = &fsinfo;
76 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
77 dprintk("%s: reply statfs: %d\n", __FUNCTION__, status);
78 if (status)
79 return status;
80 info->rtmax = NFS_MAXDATA;
81 info->rtpref = fsinfo.tsize;
82 info->rtmult = fsinfo.bsize;
83 info->wtmax = NFS_MAXDATA;
84 info->wtpref = fsinfo.tsize;
85 info->wtmult = fsinfo.bsize;
86 info->dtpref = fsinfo.tsize;
87 info->maxfilesize = 0x7FFFFFFF;
88 info->lease_time = 0;
89 return 0;
93 * One function for each procedure in the NFS protocol.
95 static int
96 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr)
99 struct rpc_message msg = {
100 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
101 .rpc_argp = fhandle,
102 .rpc_resp = fattr,
104 int status;
106 dprintk("NFS call getattr\n");
107 nfs_fattr_init(fattr);
108 status = rpc_call_sync(server->client, &msg, 0);
109 dprintk("NFS reply getattr: %d\n", status);
110 return status;
113 static int
114 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
115 struct iattr *sattr)
117 struct inode *inode = dentry->d_inode;
118 struct nfs_sattrargs arg = {
119 .fh = NFS_FH(inode),
120 .sattr = sattr
122 struct rpc_message msg = {
123 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
124 .rpc_argp = &arg,
125 .rpc_resp = fattr,
127 int status;
129 /* Mask out the non-modebit related stuff from attr->ia_mode */
130 sattr->ia_mode &= S_IALLUGO;
132 dprintk("NFS call setattr\n");
133 nfs_fattr_init(fattr);
134 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
135 if (status == 0)
136 nfs_setattr_update_inode(inode, sattr);
137 dprintk("NFS reply setattr: %d\n", status);
138 return status;
141 static int
142 nfs_proc_lookup(struct inode *dir, struct qstr *name,
143 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
145 struct nfs_diropargs arg = {
146 .fh = NFS_FH(dir),
147 .name = name->name,
148 .len = name->len
150 struct nfs_diropok res = {
151 .fh = fhandle,
152 .fattr = fattr
154 struct rpc_message msg = {
155 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
156 .rpc_argp = &arg,
157 .rpc_resp = &res,
159 int status;
161 dprintk("NFS call lookup %s\n", name->name);
162 nfs_fattr_init(fattr);
163 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
164 dprintk("NFS reply lookup: %d\n", status);
165 return status;
168 static int nfs_proc_readlink(struct inode *inode, struct page *page,
169 unsigned int pgbase, unsigned int pglen)
171 struct nfs_readlinkargs args = {
172 .fh = NFS_FH(inode),
173 .pgbase = pgbase,
174 .pglen = pglen,
175 .pages = &page
177 struct rpc_message msg = {
178 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
179 .rpc_argp = &args,
181 int status;
183 dprintk("NFS call readlink\n");
184 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
185 dprintk("NFS reply readlink: %d\n", status);
186 return status;
189 static int nfs_proc_read(struct nfs_read_data *rdata)
191 int flags = rdata->flags;
192 struct inode * inode = rdata->inode;
193 struct nfs_fattr * fattr = rdata->res.fattr;
194 struct rpc_message msg = {
195 .rpc_proc = &nfs_procedures[NFSPROC_READ],
196 .rpc_argp = &rdata->args,
197 .rpc_resp = &rdata->res,
198 .rpc_cred = rdata->cred,
200 int status;
202 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
203 (long long) rdata->args.offset);
204 nfs_fattr_init(fattr);
205 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
206 if (status >= 0) {
207 nfs_refresh_inode(inode, fattr);
208 /* Emulate the eof flag, which isn't normally needed in NFSv2
209 * as it is guaranteed to always return the file attributes
211 if (rdata->args.offset + rdata->args.count >= fattr->size)
212 rdata->res.eof = 1;
214 dprintk("NFS reply read: %d\n", status);
215 return status;
218 static int
219 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
220 int flags, struct nameidata *nd)
222 struct nfs_fh fhandle;
223 struct nfs_fattr fattr;
224 struct nfs_createargs arg = {
225 .fh = NFS_FH(dir),
226 .name = dentry->d_name.name,
227 .len = dentry->d_name.len,
228 .sattr = sattr
230 struct nfs_diropok res = {
231 .fh = &fhandle,
232 .fattr = &fattr
234 struct rpc_message msg = {
235 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
236 .rpc_argp = &arg,
237 .rpc_resp = &res,
239 int status;
241 nfs_fattr_init(&fattr);
242 dprintk("NFS call create %s\n", dentry->d_name.name);
243 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
244 if (status == 0)
245 status = nfs_instantiate(dentry, &fhandle, &fattr);
246 dprintk("NFS reply create: %d\n", status);
247 return status;
251 * In NFSv2, mknod is grafted onto the create call.
253 static int
254 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
255 dev_t rdev)
257 struct nfs_fh fhandle;
258 struct nfs_fattr fattr;
259 struct nfs_createargs arg = {
260 .fh = NFS_FH(dir),
261 .name = dentry->d_name.name,
262 .len = dentry->d_name.len,
263 .sattr = sattr
265 struct nfs_diropok res = {
266 .fh = &fhandle,
267 .fattr = &fattr
269 struct rpc_message msg = {
270 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
271 .rpc_argp = &arg,
272 .rpc_resp = &res,
274 int status, mode;
276 dprintk("NFS call mknod %s\n", dentry->d_name.name);
278 mode = sattr->ia_mode;
279 if (S_ISFIFO(mode)) {
280 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
281 sattr->ia_valid &= ~ATTR_SIZE;
282 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
283 sattr->ia_valid |= ATTR_SIZE;
284 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
287 nfs_fattr_init(&fattr);
288 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
289 nfs_mark_for_revalidate(dir);
291 if (status == -EINVAL && S_ISFIFO(mode)) {
292 sattr->ia_mode = mode;
293 nfs_fattr_init(&fattr);
294 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
296 if (status == 0)
297 status = nfs_instantiate(dentry, &fhandle, &fattr);
298 dprintk("NFS reply mknod: %d\n", status);
299 return status;
302 static int
303 nfs_proc_remove(struct inode *dir, struct qstr *name)
305 struct nfs_diropargs arg = {
306 .fh = NFS_FH(dir),
307 .name = name->name,
308 .len = name->len
310 struct rpc_message msg = {
311 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
312 .rpc_argp = &arg,
314 int status;
316 dprintk("NFS call remove %s\n", name->name);
317 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
318 nfs_mark_for_revalidate(dir);
320 dprintk("NFS reply remove: %d\n", status);
321 return status;
324 static int
325 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
327 struct nfs_diropargs *arg;
329 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
330 if (!arg)
331 return -ENOMEM;
332 arg->fh = NFS_FH(dir->d_inode);
333 arg->name = name->name;
334 arg->len = name->len;
335 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
336 msg->rpc_argp = arg;
337 return 0;
340 static int
341 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
343 struct rpc_message *msg = &task->tk_msg;
345 if (msg->rpc_argp) {
346 nfs_mark_for_revalidate(dir->d_inode);
347 kfree(msg->rpc_argp);
349 return 0;
352 static int
353 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
354 struct inode *new_dir, struct qstr *new_name)
356 struct nfs_renameargs arg = {
357 .fromfh = NFS_FH(old_dir),
358 .fromname = old_name->name,
359 .fromlen = old_name->len,
360 .tofh = NFS_FH(new_dir),
361 .toname = new_name->name,
362 .tolen = new_name->len
364 struct rpc_message msg = {
365 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
366 .rpc_argp = &arg,
368 int status;
370 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
371 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
372 nfs_mark_for_revalidate(old_dir);
373 nfs_mark_for_revalidate(new_dir);
374 dprintk("NFS reply rename: %d\n", status);
375 return status;
378 static int
379 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
381 struct nfs_linkargs arg = {
382 .fromfh = NFS_FH(inode),
383 .tofh = NFS_FH(dir),
384 .toname = name->name,
385 .tolen = name->len
387 struct rpc_message msg = {
388 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
389 .rpc_argp = &arg,
391 int status;
393 dprintk("NFS call link %s\n", name->name);
394 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
395 nfs_mark_for_revalidate(inode);
396 nfs_mark_for_revalidate(dir);
397 dprintk("NFS reply link: %d\n", status);
398 return status;
401 static int
402 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
403 unsigned int len, struct iattr *sattr)
405 struct nfs_fh fhandle;
406 struct nfs_fattr fattr;
407 struct nfs_symlinkargs arg = {
408 .fromfh = NFS_FH(dir),
409 .fromname = dentry->d_name.name,
410 .fromlen = dentry->d_name.len,
411 .pages = &page,
412 .pathlen = len,
413 .sattr = sattr
415 struct rpc_message msg = {
416 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
417 .rpc_argp = &arg,
419 int status;
421 if (len > NFS2_MAXPATHLEN)
422 return -ENAMETOOLONG;
424 dprintk("NFS call symlink %s\n", dentry->d_name.name);
426 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
427 nfs_mark_for_revalidate(dir);
430 * V2 SYMLINK requests don't return any attributes. Setting the
431 * filehandle size to zero indicates to nfs_instantiate that it
432 * should fill in the data with a LOOKUP call on the wire.
434 if (status == 0) {
435 nfs_fattr_init(&fattr);
436 fhandle.size = 0;
437 status = nfs_instantiate(dentry, &fhandle, &fattr);
440 dprintk("NFS reply symlink: %d\n", status);
441 return status;
444 static int
445 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
447 struct nfs_fh fhandle;
448 struct nfs_fattr fattr;
449 struct nfs_createargs arg = {
450 .fh = NFS_FH(dir),
451 .name = dentry->d_name.name,
452 .len = dentry->d_name.len,
453 .sattr = sattr
455 struct nfs_diropok res = {
456 .fh = &fhandle,
457 .fattr = &fattr
459 struct rpc_message msg = {
460 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
461 .rpc_argp = &arg,
462 .rpc_resp = &res,
464 int status;
466 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
467 nfs_fattr_init(&fattr);
468 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
469 nfs_mark_for_revalidate(dir);
470 if (status == 0)
471 status = nfs_instantiate(dentry, &fhandle, &fattr);
472 dprintk("NFS reply mkdir: %d\n", status);
473 return status;
476 static int
477 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
479 struct nfs_diropargs arg = {
480 .fh = NFS_FH(dir),
481 .name = name->name,
482 .len = name->len
484 struct rpc_message msg = {
485 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
486 .rpc_argp = &arg,
488 int status;
490 dprintk("NFS call rmdir %s\n", name->name);
491 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
492 nfs_mark_for_revalidate(dir);
493 dprintk("NFS reply rmdir: %d\n", status);
494 return status;
498 * The READDIR implementation is somewhat hackish - we pass a temporary
499 * buffer to the encode function, which installs it in the receive
500 * the receive iovec. The decode function just parses the reply to make
501 * sure it is syntactically correct; the entries itself are decoded
502 * from nfs_readdir by calling the decode_entry function directly.
504 static int
505 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
506 u64 cookie, struct page *page, unsigned int count, int plus)
508 struct inode *dir = dentry->d_inode;
509 struct nfs_readdirargs arg = {
510 .fh = NFS_FH(dir),
511 .cookie = cookie,
512 .count = count,
513 .pages = &page,
515 struct rpc_message msg = {
516 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
517 .rpc_argp = &arg,
518 .rpc_cred = cred,
520 int status;
522 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
523 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
525 dprintk("NFS reply readdir: %d\n", status);
526 return status;
529 static int
530 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
531 struct nfs_fsstat *stat)
533 struct nfs2_fsstat fsinfo;
534 struct rpc_message msg = {
535 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
536 .rpc_argp = fhandle,
537 .rpc_resp = &fsinfo,
539 int status;
541 dprintk("NFS call statfs\n");
542 nfs_fattr_init(stat->fattr);
543 status = rpc_call_sync(server->client, &msg, 0);
544 dprintk("NFS reply statfs: %d\n", status);
545 if (status)
546 goto out;
547 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
548 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
549 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
550 stat->tfiles = 0;
551 stat->ffiles = 0;
552 stat->afiles = 0;
553 out:
554 return status;
557 static int
558 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
559 struct nfs_fsinfo *info)
561 struct nfs2_fsstat fsinfo;
562 struct rpc_message msg = {
563 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
564 .rpc_argp = fhandle,
565 .rpc_resp = &fsinfo,
567 int status;
569 dprintk("NFS call fsinfo\n");
570 nfs_fattr_init(info->fattr);
571 status = rpc_call_sync(server->client, &msg, 0);
572 dprintk("NFS reply fsinfo: %d\n", status);
573 if (status)
574 goto out;
575 info->rtmax = NFS_MAXDATA;
576 info->rtpref = fsinfo.tsize;
577 info->rtmult = fsinfo.bsize;
578 info->wtmax = NFS_MAXDATA;
579 info->wtpref = fsinfo.tsize;
580 info->wtmult = fsinfo.bsize;
581 info->dtpref = fsinfo.tsize;
582 info->maxfilesize = 0x7FFFFFFF;
583 info->lease_time = 0;
584 out:
585 return status;
588 static int
589 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
590 struct nfs_pathconf *info)
592 info->max_link = 0;
593 info->max_namelen = NFS2_MAXNAMLEN;
594 return 0;
597 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
599 if (task->tk_status >= 0) {
600 nfs_refresh_inode(data->inode, data->res.fattr);
601 /* Emulate the eof flag, which isn't normally needed in NFSv2
602 * as it is guaranteed to always return the file attributes
604 if (data->args.offset + data->args.count >= data->res.fattr->size)
605 data->res.eof = 1;
607 return 0;
610 static void nfs_proc_read_setup(struct nfs_read_data *data)
612 struct rpc_message msg = {
613 .rpc_proc = &nfs_procedures[NFSPROC_READ],
614 .rpc_argp = &data->args,
615 .rpc_resp = &data->res,
616 .rpc_cred = data->cred,
619 rpc_call_setup(&data->task, &msg, 0);
622 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
624 if (task->tk_status >= 0)
625 nfs_post_op_update_inode(data->inode, data->res.fattr);
626 return 0;
629 static void nfs_proc_write_setup(struct nfs_write_data *data, int how)
631 struct rpc_message msg = {
632 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
633 .rpc_argp = &data->args,
634 .rpc_resp = &data->res,
635 .rpc_cred = data->cred,
638 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
639 data->args.stable = NFS_FILE_SYNC;
641 /* Finalize the task. */
642 rpc_call_setup(&data->task, &msg, 0);
645 static void
646 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
648 BUG();
651 static int
652 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
654 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
658 const struct nfs_rpc_ops nfs_v2_clientops = {
659 .version = 2, /* protocol version */
660 .dentry_ops = &nfs_dentry_operations,
661 .dir_inode_ops = &nfs_dir_inode_operations,
662 .file_inode_ops = &nfs_file_inode_operations,
663 .getroot = nfs_proc_get_root,
664 .getattr = nfs_proc_getattr,
665 .setattr = nfs_proc_setattr,
666 .lookup = nfs_proc_lookup,
667 .access = NULL, /* access */
668 .readlink = nfs_proc_readlink,
669 .read = nfs_proc_read,
670 .create = nfs_proc_create,
671 .remove = nfs_proc_remove,
672 .unlink_setup = nfs_proc_unlink_setup,
673 .unlink_done = nfs_proc_unlink_done,
674 .rename = nfs_proc_rename,
675 .link = nfs_proc_link,
676 .symlink = nfs_proc_symlink,
677 .mkdir = nfs_proc_mkdir,
678 .rmdir = nfs_proc_rmdir,
679 .readdir = nfs_proc_readdir,
680 .mknod = nfs_proc_mknod,
681 .statfs = nfs_proc_statfs,
682 .fsinfo = nfs_proc_fsinfo,
683 .pathconf = nfs_proc_pathconf,
684 .decode_dirent = nfs_decode_dirent,
685 .read_setup = nfs_proc_read_setup,
686 .read_done = nfs_read_done,
687 .write_setup = nfs_proc_write_setup,
688 .write_done = nfs_write_done,
689 .commit_setup = nfs_proc_commit_setup,
690 .file_open = nfs_open,
691 .file_release = nfs_release,
692 .lock = nfs_proc_lock,