[PATCH] swsusp: Fix SNAPSHOT_S2RAM ioctl
[linux-2.6/sactl.git] / fs / nfs / proc.c
blob1dcf56de948276c5bef2c62ff91303c30c652611
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
190 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
191 int flags, struct nameidata *nd)
193 struct nfs_fh fhandle;
194 struct nfs_fattr fattr;
195 struct nfs_createargs arg = {
196 .fh = NFS_FH(dir),
197 .name = dentry->d_name.name,
198 .len = dentry->d_name.len,
199 .sattr = sattr
201 struct nfs_diropok res = {
202 .fh = &fhandle,
203 .fattr = &fattr
205 struct rpc_message msg = {
206 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
207 .rpc_argp = &arg,
208 .rpc_resp = &res,
210 int status;
212 nfs_fattr_init(&fattr);
213 dprintk("NFS call create %s\n", dentry->d_name.name);
214 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
215 if (status == 0)
216 status = nfs_instantiate(dentry, &fhandle, &fattr);
217 dprintk("NFS reply create: %d\n", status);
218 return status;
222 * In NFSv2, mknod is grafted onto the create call.
224 static int
225 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
226 dev_t rdev)
228 struct nfs_fh fhandle;
229 struct nfs_fattr fattr;
230 struct nfs_createargs arg = {
231 .fh = NFS_FH(dir),
232 .name = dentry->d_name.name,
233 .len = dentry->d_name.len,
234 .sattr = sattr
236 struct nfs_diropok res = {
237 .fh = &fhandle,
238 .fattr = &fattr
240 struct rpc_message msg = {
241 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
242 .rpc_argp = &arg,
243 .rpc_resp = &res,
245 int status, mode;
247 dprintk("NFS call mknod %s\n", dentry->d_name.name);
249 mode = sattr->ia_mode;
250 if (S_ISFIFO(mode)) {
251 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
252 sattr->ia_valid &= ~ATTR_SIZE;
253 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
254 sattr->ia_valid |= ATTR_SIZE;
255 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
258 nfs_fattr_init(&fattr);
259 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
260 nfs_mark_for_revalidate(dir);
262 if (status == -EINVAL && S_ISFIFO(mode)) {
263 sattr->ia_mode = mode;
264 nfs_fattr_init(&fattr);
265 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
267 if (status == 0)
268 status = nfs_instantiate(dentry, &fhandle, &fattr);
269 dprintk("NFS reply mknod: %d\n", status);
270 return status;
273 static int
274 nfs_proc_remove(struct inode *dir, struct qstr *name)
276 struct nfs_diropargs arg = {
277 .fh = NFS_FH(dir),
278 .name = name->name,
279 .len = name->len
281 struct rpc_message msg = {
282 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
283 .rpc_argp = &arg,
285 int status;
287 dprintk("NFS call remove %s\n", name->name);
288 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
289 nfs_mark_for_revalidate(dir);
291 dprintk("NFS reply remove: %d\n", status);
292 return status;
295 static int
296 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
298 struct nfs_diropargs *arg;
300 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
301 if (!arg)
302 return -ENOMEM;
303 arg->fh = NFS_FH(dir->d_inode);
304 arg->name = name->name;
305 arg->len = name->len;
306 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
307 msg->rpc_argp = arg;
308 return 0;
311 static int
312 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
314 struct rpc_message *msg = &task->tk_msg;
316 if (msg->rpc_argp) {
317 nfs_mark_for_revalidate(dir->d_inode);
318 kfree(msg->rpc_argp);
320 return 0;
323 static int
324 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
325 struct inode *new_dir, struct qstr *new_name)
327 struct nfs_renameargs arg = {
328 .fromfh = NFS_FH(old_dir),
329 .fromname = old_name->name,
330 .fromlen = old_name->len,
331 .tofh = NFS_FH(new_dir),
332 .toname = new_name->name,
333 .tolen = new_name->len
335 struct rpc_message msg = {
336 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
337 .rpc_argp = &arg,
339 int status;
341 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
342 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
343 nfs_mark_for_revalidate(old_dir);
344 nfs_mark_for_revalidate(new_dir);
345 dprintk("NFS reply rename: %d\n", status);
346 return status;
349 static int
350 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
352 struct nfs_linkargs arg = {
353 .fromfh = NFS_FH(inode),
354 .tofh = NFS_FH(dir),
355 .toname = name->name,
356 .tolen = name->len
358 struct rpc_message msg = {
359 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
360 .rpc_argp = &arg,
362 int status;
364 dprintk("NFS call link %s\n", name->name);
365 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
366 nfs_mark_for_revalidate(inode);
367 nfs_mark_for_revalidate(dir);
368 dprintk("NFS reply link: %d\n", status);
369 return status;
372 static int
373 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
374 unsigned int len, struct iattr *sattr)
376 struct nfs_fh fhandle;
377 struct nfs_fattr fattr;
378 struct nfs_symlinkargs arg = {
379 .fromfh = NFS_FH(dir),
380 .fromname = dentry->d_name.name,
381 .fromlen = dentry->d_name.len,
382 .pages = &page,
383 .pathlen = len,
384 .sattr = sattr
386 struct rpc_message msg = {
387 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
388 .rpc_argp = &arg,
390 int status;
392 if (len > NFS2_MAXPATHLEN)
393 return -ENAMETOOLONG;
395 dprintk("NFS call symlink %s\n", dentry->d_name.name);
397 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
398 nfs_mark_for_revalidate(dir);
401 * V2 SYMLINK requests don't return any attributes. Setting the
402 * filehandle size to zero indicates to nfs_instantiate that it
403 * should fill in the data with a LOOKUP call on the wire.
405 if (status == 0) {
406 nfs_fattr_init(&fattr);
407 fhandle.size = 0;
408 status = nfs_instantiate(dentry, &fhandle, &fattr);
411 dprintk("NFS reply symlink: %d\n", status);
412 return status;
415 static int
416 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
418 struct nfs_fh fhandle;
419 struct nfs_fattr fattr;
420 struct nfs_createargs arg = {
421 .fh = NFS_FH(dir),
422 .name = dentry->d_name.name,
423 .len = dentry->d_name.len,
424 .sattr = sattr
426 struct nfs_diropok res = {
427 .fh = &fhandle,
428 .fattr = &fattr
430 struct rpc_message msg = {
431 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
432 .rpc_argp = &arg,
433 .rpc_resp = &res,
435 int status;
437 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
438 nfs_fattr_init(&fattr);
439 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
440 nfs_mark_for_revalidate(dir);
441 if (status == 0)
442 status = nfs_instantiate(dentry, &fhandle, &fattr);
443 dprintk("NFS reply mkdir: %d\n", status);
444 return status;
447 static int
448 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
450 struct nfs_diropargs arg = {
451 .fh = NFS_FH(dir),
452 .name = name->name,
453 .len = name->len
455 struct rpc_message msg = {
456 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
457 .rpc_argp = &arg,
459 int status;
461 dprintk("NFS call rmdir %s\n", name->name);
462 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
463 nfs_mark_for_revalidate(dir);
464 dprintk("NFS reply rmdir: %d\n", status);
465 return status;
469 * The READDIR implementation is somewhat hackish - we pass a temporary
470 * buffer to the encode function, which installs it in the receive
471 * the receive iovec. The decode function just parses the reply to make
472 * sure it is syntactically correct; the entries itself are decoded
473 * from nfs_readdir by calling the decode_entry function directly.
475 static int
476 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
477 u64 cookie, struct page *page, unsigned int count, int plus)
479 struct inode *dir = dentry->d_inode;
480 struct nfs_readdirargs arg = {
481 .fh = NFS_FH(dir),
482 .cookie = cookie,
483 .count = count,
484 .pages = &page,
486 struct rpc_message msg = {
487 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
488 .rpc_argp = &arg,
489 .rpc_cred = cred,
491 int status;
493 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
494 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
496 dprintk("NFS reply readdir: %d\n", status);
497 return status;
500 static int
501 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
502 struct nfs_fsstat *stat)
504 struct nfs2_fsstat fsinfo;
505 struct rpc_message msg = {
506 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
507 .rpc_argp = fhandle,
508 .rpc_resp = &fsinfo,
510 int status;
512 dprintk("NFS call statfs\n");
513 nfs_fattr_init(stat->fattr);
514 status = rpc_call_sync(server->client, &msg, 0);
515 dprintk("NFS reply statfs: %d\n", status);
516 if (status)
517 goto out;
518 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
519 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
520 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
521 stat->tfiles = 0;
522 stat->ffiles = 0;
523 stat->afiles = 0;
524 out:
525 return status;
528 static int
529 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
530 struct nfs_fsinfo *info)
532 struct nfs2_fsstat fsinfo;
533 struct rpc_message msg = {
534 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
535 .rpc_argp = fhandle,
536 .rpc_resp = &fsinfo,
538 int status;
540 dprintk("NFS call fsinfo\n");
541 nfs_fattr_init(info->fattr);
542 status = rpc_call_sync(server->client, &msg, 0);
543 dprintk("NFS reply fsinfo: %d\n", status);
544 if (status)
545 goto out;
546 info->rtmax = NFS_MAXDATA;
547 info->rtpref = fsinfo.tsize;
548 info->rtmult = fsinfo.bsize;
549 info->wtmax = NFS_MAXDATA;
550 info->wtpref = fsinfo.tsize;
551 info->wtmult = fsinfo.bsize;
552 info->dtpref = fsinfo.tsize;
553 info->maxfilesize = 0x7FFFFFFF;
554 info->lease_time = 0;
555 out:
556 return status;
559 static int
560 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
561 struct nfs_pathconf *info)
563 info->max_link = 0;
564 info->max_namelen = NFS2_MAXNAMLEN;
565 return 0;
568 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
570 if (task->tk_status >= 0) {
571 nfs_refresh_inode(data->inode, data->res.fattr);
572 /* Emulate the eof flag, which isn't normally needed in NFSv2
573 * as it is guaranteed to always return the file attributes
575 if (data->args.offset + data->args.count >= data->res.fattr->size)
576 data->res.eof = 1;
578 return 0;
581 static void nfs_proc_read_setup(struct nfs_read_data *data)
583 struct rpc_message msg = {
584 .rpc_proc = &nfs_procedures[NFSPROC_READ],
585 .rpc_argp = &data->args,
586 .rpc_resp = &data->res,
587 .rpc_cred = data->cred,
590 rpc_call_setup(&data->task, &msg, 0);
593 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
595 if (task->tk_status >= 0)
596 nfs_post_op_update_inode(data->inode, data->res.fattr);
597 return 0;
600 static void nfs_proc_write_setup(struct nfs_write_data *data, int how)
602 struct rpc_message msg = {
603 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
604 .rpc_argp = &data->args,
605 .rpc_resp = &data->res,
606 .rpc_cred = data->cred,
609 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
610 data->args.stable = NFS_FILE_SYNC;
612 /* Finalize the task. */
613 rpc_call_setup(&data->task, &msg, 0);
616 static void
617 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
619 BUG();
622 static int
623 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
625 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
629 const struct nfs_rpc_ops nfs_v2_clientops = {
630 .version = 2, /* protocol version */
631 .dentry_ops = &nfs_dentry_operations,
632 .dir_inode_ops = &nfs_dir_inode_operations,
633 .file_inode_ops = &nfs_file_inode_operations,
634 .getroot = nfs_proc_get_root,
635 .getattr = nfs_proc_getattr,
636 .setattr = nfs_proc_setattr,
637 .lookup = nfs_proc_lookup,
638 .access = NULL, /* access */
639 .readlink = nfs_proc_readlink,
640 .create = nfs_proc_create,
641 .remove = nfs_proc_remove,
642 .unlink_setup = nfs_proc_unlink_setup,
643 .unlink_done = nfs_proc_unlink_done,
644 .rename = nfs_proc_rename,
645 .link = nfs_proc_link,
646 .symlink = nfs_proc_symlink,
647 .mkdir = nfs_proc_mkdir,
648 .rmdir = nfs_proc_rmdir,
649 .readdir = nfs_proc_readdir,
650 .mknod = nfs_proc_mknod,
651 .statfs = nfs_proc_statfs,
652 .fsinfo = nfs_proc_fsinfo,
653 .pathconf = nfs_proc_pathconf,
654 .decode_dirent = nfs_decode_dirent,
655 .read_setup = nfs_proc_read_setup,
656 .read_done = nfs_read_done,
657 .write_setup = nfs_proc_write_setup,
658 .write_done = nfs_write_done,
659 .commit_setup = nfs_proc_commit_setup,
660 .file_open = nfs_open,
661 .file_release = nfs_release,
662 .lock = nfs_proc_lock,