blob: d31b4d6e5a5e2d4f259cd3896889594d3e410575 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/proc.c
3 *
4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
5 *
6 * OS-independent nfs remote procedure call functions
7 *
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.
11 *
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.
15 *
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
18 *
19 * Feel free to fix it and mail me the diffs if it worries you.
20 *
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
24 *
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).
28 */
29
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
48#define NFSDBG_FACILITY NFSDBG_PROC
49
50extern struct rpc_procinfo nfs_procedures[];
51
52/*
53 * Bare-bones access to getattr: this is for nfs_read_super.
54 */
55static int
56nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
57 struct nfs_fsinfo *info)
58{
59 struct nfs_fattr *fattr = info->fattr;
60 struct nfs2_fsstat fsinfo;
61 int status;
62
63 dprintk("%s: call getattr\n", __FUNCTION__);
64 fattr->valid = 0;
65 status = rpc_call(server->client_sys, NFSPROC_GETATTR, fhandle, fattr, 0);
66 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
67 if (status)
68 return status;
69 dprintk("%s: call statfs\n", __FUNCTION__);
70 status = rpc_call(server->client_sys, NFSPROC_STATFS, fhandle, &fsinfo, 0);
71 dprintk("%s: reply statfs: %d\n", __FUNCTION__, status);
72 if (status)
73 return status;
74 info->rtmax = NFS_MAXDATA;
75 info->rtpref = fsinfo.tsize;
76 info->rtmult = fsinfo.bsize;
77 info->wtmax = NFS_MAXDATA;
78 info->wtpref = fsinfo.tsize;
79 info->wtmult = fsinfo.bsize;
80 info->dtpref = fsinfo.tsize;
81 info->maxfilesize = 0x7FFFFFFF;
82 info->lease_time = 0;
83 return 0;
84}
85
86/*
87 * One function for each procedure in the NFS protocol.
88 */
89static int
90nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
91 struct nfs_fattr *fattr)
92{
93 int status;
94
95 dprintk("NFS call getattr\n");
96 fattr->valid = 0;
97 status = rpc_call(server->client, NFSPROC_GETATTR,
98 fhandle, fattr, 0);
99 dprintk("NFS reply getattr: %d\n", status);
100 return status;
101}
102
103static int
104nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
105 struct iattr *sattr)
106{
107 struct inode *inode = dentry->d_inode;
108 struct nfs_sattrargs arg = {
109 .fh = NFS_FH(inode),
110 .sattr = sattr
111 };
112 int status;
113
114 dprintk("NFS call setattr\n");
115 fattr->valid = 0;
116 status = rpc_call(NFS_CLIENT(inode), NFSPROC_SETATTR, &arg, fattr, 0);
117 dprintk("NFS reply setattr: %d\n", status);
118 return status;
119}
120
121static int
122nfs_proc_lookup(struct inode *dir, struct qstr *name,
123 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
124{
125 struct nfs_diropargs arg = {
126 .fh = NFS_FH(dir),
127 .name = name->name,
128 .len = name->len
129 };
130 struct nfs_diropok res = {
131 .fh = fhandle,
132 .fattr = fattr
133 };
134 int status;
135
136 dprintk("NFS call lookup %s\n", name->name);
137 fattr->valid = 0;
138 status = rpc_call(NFS_CLIENT(dir), NFSPROC_LOOKUP, &arg, &res, 0);
139 dprintk("NFS reply lookup: %d\n", status);
140 return status;
141}
142
143static int nfs_proc_readlink(struct inode *inode, struct page *page,
144 unsigned int pgbase, unsigned int pglen)
145{
146 struct nfs_readlinkargs args = {
147 .fh = NFS_FH(inode),
148 .pgbase = pgbase,
149 .pglen = pglen,
150 .pages = &page
151 };
152 int status;
153
154 dprintk("NFS call readlink\n");
155 status = rpc_call(NFS_CLIENT(inode), NFSPROC_READLINK, &args, NULL, 0);
156 dprintk("NFS reply readlink: %d\n", status);
157 return status;
158}
159
160static int nfs_proc_read(struct nfs_read_data *rdata)
161{
162 int flags = rdata->flags;
163 struct inode * inode = rdata->inode;
164 struct nfs_fattr * fattr = rdata->res.fattr;
165 struct rpc_message msg = {
166 .rpc_proc = &nfs_procedures[NFSPROC_READ],
167 .rpc_argp = &rdata->args,
168 .rpc_resp = &rdata->res,
169 .rpc_cred = rdata->cred,
170 };
171 int status;
172
173 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
174 (long long) rdata->args.offset);
175 fattr->valid = 0;
176 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
177 if (status >= 0) {
178 nfs_refresh_inode(inode, fattr);
179 /* Emulate the eof flag, which isn't normally needed in NFSv2
180 * as it is guaranteed to always return the file attributes
181 */
182 if (rdata->args.offset + rdata->args.count >= fattr->size)
183 rdata->res.eof = 1;
184 }
185 dprintk("NFS reply read: %d\n", status);
186 return status;
187}
188
189static int nfs_proc_write(struct nfs_write_data *wdata)
190{
191 int flags = wdata->flags;
192 struct inode * inode = wdata->inode;
193 struct nfs_fattr * fattr = wdata->res.fattr;
194 struct rpc_message msg = {
195 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
196 .rpc_argp = &wdata->args,
197 .rpc_resp = &wdata->res,
198 .rpc_cred = wdata->cred,
199 };
200 int status;
201
202 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
203 (long long) wdata->args.offset);
204 fattr->valid = 0;
205 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
206 if (status >= 0) {
207 nfs_refresh_inode(inode, fattr);
208 wdata->res.count = wdata->args.count;
209 wdata->verf.committed = NFS_FILE_SYNC;
210 }
211 dprintk("NFS reply write: %d\n", status);
212 return status < 0? status : wdata->res.count;
213}
214
215static int
216nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
217 int flags)
218{
219 struct nfs_fh fhandle;
220 struct nfs_fattr fattr;
221 struct nfs_createargs arg = {
222 .fh = NFS_FH(dir),
223 .name = dentry->d_name.name,
224 .len = dentry->d_name.len,
225 .sattr = sattr
226 };
227 struct nfs_diropok res = {
228 .fh = &fhandle,
229 .fattr = &fattr
230 };
231 int status;
232
233 fattr.valid = 0;
234 dprintk("NFS call create %s\n", dentry->d_name.name);
235 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
236 if (status == 0)
237 status = nfs_instantiate(dentry, &fhandle, &fattr);
238 dprintk("NFS reply create: %d\n", status);
239 return status;
240}
241
242/*
243 * In NFSv2, mknod is grafted onto the create call.
244 */
245static int
246nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
247 dev_t rdev)
248{
249 struct nfs_fh fhandle;
250 struct nfs_fattr fattr;
251 struct nfs_createargs arg = {
252 .fh = NFS_FH(dir),
253 .name = dentry->d_name.name,
254 .len = dentry->d_name.len,
255 .sattr = sattr
256 };
257 struct nfs_diropok res = {
258 .fh = &fhandle,
259 .fattr = &fattr
260 };
261 int status, mode;
262
263 dprintk("NFS call mknod %s\n", dentry->d_name.name);
264
265 mode = sattr->ia_mode;
266 if (S_ISFIFO(mode)) {
267 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
268 sattr->ia_valid &= ~ATTR_SIZE;
269 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
270 sattr->ia_valid |= ATTR_SIZE;
271 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
272 }
273
274 fattr.valid = 0;
275 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
276
277 if (status == -EINVAL && S_ISFIFO(mode)) {
278 sattr->ia_mode = mode;
279 fattr.valid = 0;
280 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
281 }
282 if (status == 0)
283 status = nfs_instantiate(dentry, &fhandle, &fattr);
284 dprintk("NFS reply mknod: %d\n", status);
285 return status;
286}
287
288static int
289nfs_proc_remove(struct inode *dir, struct qstr *name)
290{
291 struct nfs_diropargs arg = {
292 .fh = NFS_FH(dir),
293 .name = name->name,
294 .len = name->len
295 };
296 struct rpc_message msg = {
297 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
298 .rpc_argp = &arg,
299 .rpc_resp = NULL,
300 .rpc_cred = NULL
301 };
302 int status;
303
304 dprintk("NFS call remove %s\n", name->name);
305 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
306
307 dprintk("NFS reply remove: %d\n", status);
308 return status;
309}
310
311static int
312nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
313{
314 struct nfs_diropargs *arg;
315
316 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
317 if (!arg)
318 return -ENOMEM;
319 arg->fh = NFS_FH(dir->d_inode);
320 arg->name = name->name;
321 arg->len = name->len;
322 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
323 msg->rpc_argp = arg;
324 return 0;
325}
326
327static int
328nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
329{
330 struct rpc_message *msg = &task->tk_msg;
331
332 if (msg->rpc_argp)
333 kfree(msg->rpc_argp);
334 return 0;
335}
336
337static int
338nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
339 struct inode *new_dir, struct qstr *new_name)
340{
341 struct nfs_renameargs arg = {
342 .fromfh = NFS_FH(old_dir),
343 .fromname = old_name->name,
344 .fromlen = old_name->len,
345 .tofh = NFS_FH(new_dir),
346 .toname = new_name->name,
347 .tolen = new_name->len
348 };
349 int status;
350
351 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
352 status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
353 dprintk("NFS reply rename: %d\n", status);
354 return status;
355}
356
357static int
358nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
359{
360 struct nfs_linkargs arg = {
361 .fromfh = NFS_FH(inode),
362 .tofh = NFS_FH(dir),
363 .toname = name->name,
364 .tolen = name->len
365 };
366 int status;
367
368 dprintk("NFS call link %s\n", name->name);
369 status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
370 dprintk("NFS reply link: %d\n", status);
371 return status;
372}
373
374static int
375nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
376 struct iattr *sattr, struct nfs_fh *fhandle,
377 struct nfs_fattr *fattr)
378{
379 struct nfs_symlinkargs arg = {
380 .fromfh = NFS_FH(dir),
381 .fromname = name->name,
382 .fromlen = name->len,
383 .topath = path->name,
384 .tolen = path->len,
385 .sattr = sattr
386 };
387 int status;
388
389 if (path->len > NFS2_MAXPATHLEN)
390 return -ENAMETOOLONG;
391 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
392 fattr->valid = 0;
393 fhandle->size = 0;
394 status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
395 dprintk("NFS reply symlink: %d\n", status);
396 return status;
397}
398
399static int
400nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
401{
402 struct nfs_fh fhandle;
403 struct nfs_fattr fattr;
404 struct nfs_createargs arg = {
405 .fh = NFS_FH(dir),
406 .name = dentry->d_name.name,
407 .len = dentry->d_name.len,
408 .sattr = sattr
409 };
410 struct nfs_diropok res = {
411 .fh = &fhandle,
412 .fattr = &fattr
413 };
414 int status;
415
416 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
417 fattr.valid = 0;
418 status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
419 if (status == 0)
420 status = nfs_instantiate(dentry, &fhandle, &fattr);
421 dprintk("NFS reply mkdir: %d\n", status);
422 return status;
423}
424
425static int
426nfs_proc_rmdir(struct inode *dir, struct qstr *name)
427{
428 struct nfs_diropargs arg = {
429 .fh = NFS_FH(dir),
430 .name = name->name,
431 .len = name->len
432 };
433 int status;
434
435 dprintk("NFS call rmdir %s\n", name->name);
436 status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
437 dprintk("NFS reply rmdir: %d\n", status);
438 return status;
439}
440
441/*
442 * The READDIR implementation is somewhat hackish - we pass a temporary
443 * buffer to the encode function, which installs it in the receive
444 * the receive iovec. The decode function just parses the reply to make
445 * sure it is syntactically correct; the entries itself are decoded
446 * from nfs_readdir by calling the decode_entry function directly.
447 */
448static int
449nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
450 u64 cookie, struct page *page, unsigned int count, int plus)
451{
452 struct inode *dir = dentry->d_inode;
453 struct nfs_readdirargs arg = {
454 .fh = NFS_FH(dir),
455 .cookie = cookie,
456 .count = count,
457 .pages = &page
458 };
459 struct rpc_message msg = {
460 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
461 .rpc_argp = &arg,
462 .rpc_resp = NULL,
463 .rpc_cred = cred
464 };
465 int status;
466
467 lock_kernel();
468
469 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
470 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
471
472 dprintk("NFS reply readdir: %d\n", status);
473 unlock_kernel();
474 return status;
475}
476
477static int
478nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
479 struct nfs_fsstat *stat)
480{
481 struct nfs2_fsstat fsinfo;
482 int status;
483
484 dprintk("NFS call statfs\n");
485 stat->fattr->valid = 0;
486 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
487 dprintk("NFS reply statfs: %d\n", status);
488 if (status)
489 goto out;
490 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
491 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
492 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
493 stat->tfiles = 0;
494 stat->ffiles = 0;
495 stat->afiles = 0;
496out:
497 return status;
498}
499
500static int
501nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
502 struct nfs_fsinfo *info)
503{
504 struct nfs2_fsstat fsinfo;
505 int status;
506
507 dprintk("NFS call fsinfo\n");
508 info->fattr->valid = 0;
509 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
510 dprintk("NFS reply fsinfo: %d\n", status);
511 if (status)
512 goto out;
513 info->rtmax = NFS_MAXDATA;
514 info->rtpref = fsinfo.tsize;
515 info->rtmult = fsinfo.bsize;
516 info->wtmax = NFS_MAXDATA;
517 info->wtpref = fsinfo.tsize;
518 info->wtmult = fsinfo.bsize;
519 info->dtpref = fsinfo.tsize;
520 info->maxfilesize = 0x7FFFFFFF;
521 info->lease_time = 0;
522out:
523 return status;
524}
525
526static int
527nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
528 struct nfs_pathconf *info)
529{
530 info->max_link = 0;
531 info->max_namelen = NFS2_MAXNAMLEN;
532 return 0;
533}
534
535extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
536
537static void
538nfs_read_done(struct rpc_task *task)
539{
540 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
541
542 if (task->tk_status >= 0) {
543 nfs_refresh_inode(data->inode, data->res.fattr);
544 /* Emulate the eof flag, which isn't normally needed in NFSv2
545 * as it is guaranteed to always return the file attributes
546 */
547 if (data->args.offset + data->args.count >= data->res.fattr->size)
548 data->res.eof = 1;
549 }
550 nfs_readpage_result(task);
551}
552
553static void
554nfs_proc_read_setup(struct nfs_read_data *data)
555{
556 struct rpc_task *task = &data->task;
557 struct inode *inode = data->inode;
558 int flags;
559 struct rpc_message msg = {
560 .rpc_proc = &nfs_procedures[NFSPROC_READ],
561 .rpc_argp = &data->args,
562 .rpc_resp = &data->res,
563 .rpc_cred = data->cred,
564 };
565
566 /* N.B. Do we need to test? Never called for swapfile inode */
567 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
568
569 /* Finalize the task. */
570 rpc_init_task(task, NFS_CLIENT(inode), nfs_read_done, flags);
571 rpc_call_setup(task, &msg, 0);
572}
573
574static void
575nfs_write_done(struct rpc_task *task)
576{
577 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
578
579 if (task->tk_status >= 0)
580 nfs_refresh_inode(data->inode, data->res.fattr);
581 nfs_writeback_done(task);
582}
583
584static void
585nfs_proc_write_setup(struct nfs_write_data *data, int how)
586{
587 struct rpc_task *task = &data->task;
588 struct inode *inode = data->inode;
589 int flags;
590 struct rpc_message msg = {
591 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
592 .rpc_argp = &data->args,
593 .rpc_resp = &data->res,
594 .rpc_cred = data->cred,
595 };
596
597 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
598 data->args.stable = NFS_FILE_SYNC;
599
600 /* Set the initial flags for the task. */
601 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
602
603 /* Finalize the task. */
604 rpc_init_task(task, NFS_CLIENT(inode), nfs_write_done, flags);
605 rpc_call_setup(task, &msg, 0);
606}
607
608static void
609nfs_proc_commit_setup(struct nfs_write_data *data, int how)
610{
611 BUG();
612}
613
614static int
615nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
616{
617 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
618}
619
620
621struct nfs_rpc_ops nfs_v2_clientops = {
622 .version = 2, /* protocol version */
623 .dentry_ops = &nfs_dentry_operations,
624 .dir_inode_ops = &nfs_dir_inode_operations,
625 .getroot = nfs_proc_get_root,
626 .getattr = nfs_proc_getattr,
627 .setattr = nfs_proc_setattr,
628 .lookup = nfs_proc_lookup,
629 .access = NULL, /* access */
630 .readlink = nfs_proc_readlink,
631 .read = nfs_proc_read,
632 .write = nfs_proc_write,
633 .commit = NULL, /* commit */
634 .create = nfs_proc_create,
635 .remove = nfs_proc_remove,
636 .unlink_setup = nfs_proc_unlink_setup,
637 .unlink_done = nfs_proc_unlink_done,
638 .rename = nfs_proc_rename,
639 .link = nfs_proc_link,
640 .symlink = nfs_proc_symlink,
641 .mkdir = nfs_proc_mkdir,
642 .rmdir = nfs_proc_rmdir,
643 .readdir = nfs_proc_readdir,
644 .mknod = nfs_proc_mknod,
645 .statfs = nfs_proc_statfs,
646 .fsinfo = nfs_proc_fsinfo,
647 .pathconf = nfs_proc_pathconf,
648 .decode_dirent = nfs_decode_dirent,
649 .read_setup = nfs_proc_read_setup,
650 .write_setup = nfs_proc_write_setup,
651 .commit_setup = nfs_proc_commit_setup,
652 .file_open = nfs_open,
653 .file_release = nfs_release,
654 .lock = nfs_proc_lock,
655};