vserver 1.9.3
[linux-2.6.git] / fs / nfs / proc.c
1 /*
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
50 extern struct rpc_procinfo nfs_procedures[];
51
52 /*
53  * Bare-bones access to getattr: this is for nfs_read_super.
54  */
55 static int
56 nfs_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  */
89 static int
90 nfs_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\n");
100         return status;
101 }
102
103 static int
104 nfs_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\n");
118         return status;
119 }
120
121 static int
122 nfs_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
143 static 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
160 static 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
189 static 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
215 static struct inode *
216 nfs_proc_create(struct inode *dir, struct qstr *name, 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           = name->name,
224                 .len            = 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", name->name);
235         status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
236         dprintk("NFS reply create: %d\n", status);
237         if (status == 0) {
238                 struct inode *inode;
239                 inode = nfs_fhget(dir->i_sb, &fhandle, &fattr);
240                 if (inode)
241                         return inode;
242                 status = -ENOMEM;
243         }
244         return ERR_PTR(status);
245 }
246
247 /*
248  * In NFSv2, mknod is grafted onto the create call.
249  */
250 static int
251 nfs_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
252                dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
253 {
254         struct nfs_createargs   arg = {
255                 .fh             = NFS_FH(dir),
256                 .name           = name->name,
257                 .len            = name->len,
258                 .sattr          = sattr
259         };
260         struct nfs_diropok      res = {
261                 .fh             = fhandle,
262                 .fattr          = fattr
263         };
264         int                     status, mode;
265
266         dprintk("NFS call  mknod %s\n", name->name);
267
268         mode = sattr->ia_mode;
269         if (S_ISFIFO(mode)) {
270                 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
271                 sattr->ia_valid &= ~ATTR_SIZE;
272         } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
273                 sattr->ia_valid |= ATTR_SIZE;
274                 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
275         }
276
277         fattr->valid = 0;
278         status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
279
280         if (status == -EINVAL && S_ISFIFO(mode)) {
281                 sattr->ia_mode = mode;
282                 fattr->valid = 0;
283                 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
284         }
285         dprintk("NFS reply mknod: %d\n", status);
286         return status;
287 }
288   
289 static int
290 nfs_proc_remove(struct inode *dir, struct qstr *name)
291 {
292         struct nfs_diropargs    arg = {
293                 .fh             = NFS_FH(dir),
294                 .name           = name->name,
295                 .len            = name->len
296         };
297         struct rpc_message      msg = { 
298                 .rpc_proc       = &nfs_procedures[NFSPROC_REMOVE],
299                 .rpc_argp       = &arg,
300                 .rpc_resp       = NULL,
301                 .rpc_cred       = NULL
302         };
303         int                     status;
304
305         dprintk("NFS call  remove %s\n", name->name);
306         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
307
308         dprintk("NFS reply remove: %d\n", status);
309         return status;
310 }
311
312 static int
313 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
314 {
315         struct nfs_diropargs    *arg;
316
317         arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
318         if (!arg)
319                 return -ENOMEM;
320         arg->fh = NFS_FH(dir->d_inode);
321         arg->name = name->name;
322         arg->len = name->len;
323         msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
324         msg->rpc_argp = arg;
325         return 0;
326 }
327
328 static int
329 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
330 {
331         struct rpc_message *msg = &task->tk_msg;
332         
333         if (msg->rpc_argp)
334                 kfree(msg->rpc_argp);
335         return 0;
336 }
337
338 static int
339 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
340                 struct inode *new_dir, struct qstr *new_name)
341 {
342         struct nfs_renameargs   arg = {
343                 .fromfh         = NFS_FH(old_dir),
344                 .fromname       = old_name->name,
345                 .fromlen        = old_name->len,
346                 .tofh           = NFS_FH(new_dir),
347                 .toname         = new_name->name,
348                 .tolen          = new_name->len
349         };
350         int                     status;
351
352         dprintk("NFS call  rename %s -> %s\n", old_name->name, new_name->name);
353         status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
354         dprintk("NFS reply rename: %d\n", status);
355         return status;
356 }
357
358 static int
359 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
360 {
361         struct nfs_linkargs     arg = {
362                 .fromfh         = NFS_FH(inode),
363                 .tofh           = NFS_FH(dir),
364                 .toname         = name->name,
365                 .tolen          = name->len
366         };
367         int                     status;
368
369         dprintk("NFS call  link %s\n", name->name);
370         status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
371         dprintk("NFS reply link: %d\n", status);
372         return status;
373 }
374
375 static int
376 nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
377                  struct iattr *sattr, struct nfs_fh *fhandle,
378                  struct nfs_fattr *fattr)
379 {
380         struct nfs_symlinkargs  arg = {
381                 .fromfh         = NFS_FH(dir),
382                 .fromname       = name->name,
383                 .fromlen        = name->len,
384                 .topath         = path->name,
385                 .tolen          = path->len,
386                 .sattr          = sattr
387         };
388         int                     status;
389
390         if (path->len > NFS2_MAXPATHLEN)
391                 return -ENAMETOOLONG;
392         dprintk("NFS call  symlink %s -> %s\n", name->name, path->name);
393         fattr->valid = 0;
394         fhandle->size = 0;
395         status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
396         dprintk("NFS reply symlink: %d\n", status);
397         return status;
398 }
399
400 static int
401 nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
402                struct nfs_fh *fhandle, struct nfs_fattr *fattr)
403 {
404         struct nfs_createargs   arg = {
405                 .fh             = NFS_FH(dir),
406                 .name           = name->name,
407                 .len            = 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", name->name);
417         fattr->valid = 0;
418         status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
419         dprintk("NFS reply mkdir: %d\n", status);
420         return status;
421 }
422
423 static int
424 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
425 {
426         struct nfs_diropargs    arg = {
427                 .fh             = NFS_FH(dir),
428                 .name           = name->name,
429                 .len            = name->len
430         };
431         int                     status;
432
433         dprintk("NFS call  rmdir %s\n", name->name);
434         status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
435         dprintk("NFS reply rmdir: %d\n", status);
436         return status;
437 }
438
439 /*
440  * The READDIR implementation is somewhat hackish - we pass a temporary
441  * buffer to the encode function, which installs it in the receive
442  * the receive iovec. The decode function just parses the reply to make
443  * sure it is syntactically correct; the entries itself are decoded
444  * from nfs_readdir by calling the decode_entry function directly.
445  */
446 static int
447 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
448                  u64 cookie, struct page *page, unsigned int count, int plus)
449 {
450         struct inode            *dir = dentry->d_inode;
451         struct nfs_readdirargs  arg = {
452                 .fh             = NFS_FH(dir),
453                 .cookie         = cookie,
454                 .count          = count,
455                 .pages          = &page
456         };
457         struct rpc_message      msg = {
458                 .rpc_proc       = &nfs_procedures[NFSPROC_READDIR],
459                 .rpc_argp       = &arg,
460                 .rpc_resp       = NULL,
461                 .rpc_cred       = cred
462         };
463         int                     status;
464
465         lock_kernel();
466
467         dprintk("NFS call  readdir %d\n", (unsigned int)cookie);
468         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
469
470         dprintk("NFS reply readdir: %d\n", status);
471         unlock_kernel();
472         return status;
473 }
474
475 static int
476 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
477                         struct nfs_fsstat *stat)
478 {
479         struct nfs2_fsstat fsinfo;
480         int     status;
481
482         dprintk("NFS call  statfs\n");
483         stat->fattr->valid = 0;
484         status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
485         dprintk("NFS reply statfs: %d\n", status);
486         if (status)
487                 goto out;
488         stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
489         stat->fbytes = (u64)fsinfo.bfree  * fsinfo.bsize;
490         stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
491         stat->tfiles = 0;
492         stat->ffiles = 0;
493         stat->afiles = 0;
494 out:
495         return status;
496 }
497
498 static int
499 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
500                         struct nfs_fsinfo *info)
501 {
502         struct nfs2_fsstat fsinfo;
503         int     status;
504
505         dprintk("NFS call  fsinfo\n");
506         info->fattr->valid = 0;
507         status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
508         dprintk("NFS reply fsinfo: %d\n", status);
509         if (status)
510                 goto out;
511         info->rtmax  = NFS_MAXDATA;
512         info->rtpref = fsinfo.tsize;
513         info->rtmult = fsinfo.bsize;
514         info->wtmax  = NFS_MAXDATA;
515         info->wtpref = fsinfo.tsize;
516         info->wtmult = fsinfo.bsize;
517         info->dtpref = fsinfo.tsize;
518         info->maxfilesize = 0x7FFFFFFF;
519         info->lease_time = 0;
520 out:
521         return status;
522 }
523
524 static int
525 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
526                   struct nfs_pathconf *info)
527 {
528         info->max_link = 0;
529         info->max_namelen = NFS2_MAXNAMLEN;
530         return 0;
531 }
532
533 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
534
535 static void
536 nfs_read_done(struct rpc_task *task)
537 {
538         struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
539
540         if (task->tk_status >= 0) {
541                 nfs_refresh_inode(data->inode, data->res.fattr);
542                 /* Emulate the eof flag, which isn't normally needed in NFSv2
543                  * as it is guaranteed to always return the file attributes
544                  */
545                 if (data->args.offset + data->args.count >= data->res.fattr->size)
546                         data->res.eof = 1;
547         }
548         nfs_readpage_result(task);
549 }
550
551 static void
552 nfs_proc_read_setup(struct nfs_read_data *data)
553 {
554         struct rpc_task         *task = &data->task;
555         struct inode            *inode = data->inode;
556         int                     flags;
557         struct rpc_message      msg = {
558                 .rpc_proc       = &nfs_procedures[NFSPROC_READ],
559                 .rpc_argp       = &data->args,
560                 .rpc_resp       = &data->res,
561                 .rpc_cred       = data->cred,
562         };
563
564         /* N.B. Do we need to test? Never called for swapfile inode */
565         flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
566
567         /* Finalize the task. */
568         rpc_init_task(task, NFS_CLIENT(inode), nfs_read_done, flags);
569         rpc_call_setup(task, &msg, 0);
570 }
571
572 static void
573 nfs_write_done(struct rpc_task *task)
574 {
575         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
576
577         if (task->tk_status >= 0)
578                 nfs_refresh_inode(data->inode, data->res.fattr);
579         nfs_writeback_done(task);
580 }
581
582 static void
583 nfs_proc_write_setup(struct nfs_write_data *data, int how)
584 {
585         struct rpc_task         *task = &data->task;
586         struct inode            *inode = data->inode;
587         int                     flags;
588         struct rpc_message      msg = {
589                 .rpc_proc       = &nfs_procedures[NFSPROC_WRITE],
590                 .rpc_argp       = &data->args,
591                 .rpc_resp       = &data->res,
592                 .rpc_cred       = data->cred,
593         };
594
595         /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
596         data->args.stable = NFS_FILE_SYNC;
597
598         /* Set the initial flags for the task.  */
599         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
600
601         /* Finalize the task. */
602         rpc_init_task(task, NFS_CLIENT(inode), nfs_write_done, flags);
603         rpc_call_setup(task, &msg, 0);
604 }
605
606 static void
607 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
608 {
609         BUG();
610 }
611
612 static int
613 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
614 {
615         return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
616 }
617
618
619 struct nfs_rpc_ops      nfs_v2_clientops = {
620         .version        = 2,                   /* protocol version */
621         .dentry_ops     = &nfs_dentry_operations,
622         .dir_inode_ops  = &nfs_dir_inode_operations,
623         .getroot        = nfs_proc_get_root,
624         .getattr        = nfs_proc_getattr,
625         .setattr        = nfs_proc_setattr,
626         .lookup         = nfs_proc_lookup,
627         .access         = NULL,                /* access */
628         .readlink       = nfs_proc_readlink,
629         .read           = nfs_proc_read,
630         .write          = nfs_proc_write,
631         .commit         = NULL,                /* commit */
632         .create         = nfs_proc_create,
633         .remove         = nfs_proc_remove,
634         .unlink_setup   = nfs_proc_unlink_setup,
635         .unlink_done    = nfs_proc_unlink_done,
636         .rename         = nfs_proc_rename,
637         .link           = nfs_proc_link,
638         .symlink        = nfs_proc_symlink,
639         .mkdir          = nfs_proc_mkdir,
640         .rmdir          = nfs_proc_rmdir,
641         .readdir        = nfs_proc_readdir,
642         .mknod          = nfs_proc_mknod,
643         .statfs         = nfs_proc_statfs,
644         .fsinfo         = nfs_proc_fsinfo,
645         .pathconf       = nfs_proc_pathconf,
646         .decode_dirent  = nfs_decode_dirent,
647         .read_setup     = nfs_proc_read_setup,
648         .write_setup    = nfs_proc_write_setup,
649         .commit_setup   = nfs_proc_commit_setup,
650         .file_open      = nfs_open,
651         .file_release   = nfs_release,
652         .lock           = nfs_proc_lock,
653 };