Merge master.kernel.org:/home/rmk/linux-2.6-serial
[sfrench/cifs-2.6.git] / fs / nfs / nfs3proc.c
1 /*
2  *  linux/fs/nfs/nfs3proc.c
3  *
4  *  Client-side NFSv3 procedures stubs.
5  *
6  *  Copyright (C) 1997, Olaf Kirch
7  */
8
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/nfs.h>
15 #include <linux/nfs3.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_page.h>
18 #include <linux/lockd/bind.h>
19 #include <linux/smp_lock.h>
20 #include <linux/nfs_mount.h>
21
22 #include "iostat.h"
23
24 #define NFSDBG_FACILITY         NFSDBG_PROC
25
26 extern struct rpc_procinfo nfs3_procedures[];
27
28 /* A wrapper to handle the EJUKEBOX error message */
29 static int
30 nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
31 {
32         sigset_t oldset;
33         int res;
34         rpc_clnt_sigmask(clnt, &oldset);
35         do {
36                 res = rpc_call_sync(clnt, msg, flags);
37                 if (res != -EJUKEBOX)
38                         break;
39                 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
40                 res = -ERESTARTSYS;
41         } while (!signalled());
42         rpc_clnt_sigunmask(clnt, &oldset);
43         return res;
44 }
45
46 #define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
47
48 static int
49 nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
50 {
51         if (task->tk_status != -EJUKEBOX)
52                 return 0;
53         nfs_inc_stats(inode, NFSIOS_DELAY);
54         task->tk_status = 0;
55         rpc_restart_call(task);
56         rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
57         return 1;
58 }
59
60 static int
61 do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
62                  struct nfs_fsinfo *info)
63 {
64         struct rpc_message msg = {
65                 .rpc_proc       = &nfs3_procedures[NFS3PROC_FSINFO],
66                 .rpc_argp       = fhandle,
67                 .rpc_resp       = info,
68         };
69         int     status;
70
71         dprintk("%s: call  fsinfo\n", __FUNCTION__);
72         nfs_fattr_init(info->fattr);
73         status = rpc_call_sync(client, &msg, 0);
74         dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
75         if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
76                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
77                 msg.rpc_resp = info->fattr;
78                 status = rpc_call_sync(client, &msg, 0);
79                 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
80         }
81         return status;
82 }
83
84 /*
85  * Bare-bones access to getattr: this is for nfs_read_super.
86  */
87 static int
88 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
89                    struct nfs_fsinfo *info)
90 {
91         int     status;
92
93         status = do_proc_get_root(server->client, fhandle, info);
94         if (status && server->client_sys != server->client)
95                 status = do_proc_get_root(server->client_sys, fhandle, info);
96         return status;
97 }
98
99 /*
100  * One function for each procedure in the NFS protocol.
101  */
102 static int
103 nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
104                 struct nfs_fattr *fattr)
105 {
106         struct rpc_message msg = {
107                 .rpc_proc       = &nfs3_procedures[NFS3PROC_GETATTR],
108                 .rpc_argp       = fhandle,
109                 .rpc_resp       = fattr,
110         };
111         int     status;
112
113         dprintk("NFS call  getattr\n");
114         nfs_fattr_init(fattr);
115         status = rpc_call_sync(server->client, &msg, 0);
116         dprintk("NFS reply getattr: %d\n", status);
117         return status;
118 }
119
120 static int
121 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
122                         struct iattr *sattr)
123 {
124         struct inode *inode = dentry->d_inode;
125         struct nfs3_sattrargs   arg = {
126                 .fh             = NFS_FH(inode),
127                 .sattr          = sattr,
128         };
129         struct rpc_message msg = {
130                 .rpc_proc       = &nfs3_procedures[NFS3PROC_SETATTR],
131                 .rpc_argp       = &arg,
132                 .rpc_resp       = fattr,
133         };
134         int     status;
135
136         dprintk("NFS call  setattr\n");
137         nfs_fattr_init(fattr);
138         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
139         if (status == 0)
140                 nfs_setattr_update_inode(inode, sattr);
141         dprintk("NFS reply setattr: %d\n", status);
142         return status;
143 }
144
145 static int
146 nfs3_proc_lookup(struct inode *dir, struct qstr *name,
147                  struct nfs_fh *fhandle, struct nfs_fattr *fattr)
148 {
149         struct nfs_fattr        dir_attr;
150         struct nfs3_diropargs   arg = {
151                 .fh             = NFS_FH(dir),
152                 .name           = name->name,
153                 .len            = name->len
154         };
155         struct nfs3_diropres    res = {
156                 .dir_attr       = &dir_attr,
157                 .fh             = fhandle,
158                 .fattr          = fattr
159         };
160         struct rpc_message msg = {
161                 .rpc_proc       = &nfs3_procedures[NFS3PROC_LOOKUP],
162                 .rpc_argp       = &arg,
163                 .rpc_resp       = &res,
164         };
165         int                     status;
166
167         dprintk("NFS call  lookup %s\n", name->name);
168         nfs_fattr_init(&dir_attr);
169         nfs_fattr_init(fattr);
170         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
171         if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
172                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
173                 msg.rpc_argp = fhandle;
174                 msg.rpc_resp = fattr;
175                 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
176         }
177         dprintk("NFS reply lookup: %d\n", status);
178         if (status >= 0)
179                 status = nfs_refresh_inode(dir, &dir_attr);
180         return status;
181 }
182
183 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
184 {
185         struct nfs_fattr        fattr;
186         struct nfs3_accessargs  arg = {
187                 .fh             = NFS_FH(inode),
188         };
189         struct nfs3_accessres   res = {
190                 .fattr          = &fattr,
191         };
192         struct rpc_message msg = {
193                 .rpc_proc       = &nfs3_procedures[NFS3PROC_ACCESS],
194                 .rpc_argp       = &arg,
195                 .rpc_resp       = &res,
196                 .rpc_cred       = entry->cred,
197         };
198         int mode = entry->mask;
199         int status;
200
201         dprintk("NFS call  access\n");
202
203         if (mode & MAY_READ)
204                 arg.access |= NFS3_ACCESS_READ;
205         if (S_ISDIR(inode->i_mode)) {
206                 if (mode & MAY_WRITE)
207                         arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
208                 if (mode & MAY_EXEC)
209                         arg.access |= NFS3_ACCESS_LOOKUP;
210         } else {
211                 if (mode & MAY_WRITE)
212                         arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
213                 if (mode & MAY_EXEC)
214                         arg.access |= NFS3_ACCESS_EXECUTE;
215         }
216         nfs_fattr_init(&fattr);
217         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
218         nfs_refresh_inode(inode, &fattr);
219         if (status == 0) {
220                 entry->mask = 0;
221                 if (res.access & NFS3_ACCESS_READ)
222                         entry->mask |= MAY_READ;
223                 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
224                         entry->mask |= MAY_WRITE;
225                 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
226                         entry->mask |= MAY_EXEC;
227         }
228         dprintk("NFS reply access: %d\n", status);
229         return status;
230 }
231
232 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
233                 unsigned int pgbase, unsigned int pglen)
234 {
235         struct nfs_fattr        fattr;
236         struct nfs3_readlinkargs args = {
237                 .fh             = NFS_FH(inode),
238                 .pgbase         = pgbase,
239                 .pglen          = pglen,
240                 .pages          = &page
241         };
242         struct rpc_message msg = {
243                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READLINK],
244                 .rpc_argp       = &args,
245                 .rpc_resp       = &fattr,
246         };
247         int                     status;
248
249         dprintk("NFS call  readlink\n");
250         nfs_fattr_init(&fattr);
251         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
252         nfs_refresh_inode(inode, &fattr);
253         dprintk("NFS reply readlink: %d\n", status);
254         return status;
255 }
256
257 static int nfs3_proc_read(struct nfs_read_data *rdata)
258 {
259         int                     flags = rdata->flags;
260         struct inode *          inode = rdata->inode;
261         struct nfs_fattr *      fattr = rdata->res.fattr;
262         struct rpc_message      msg = {
263                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READ],
264                 .rpc_argp       = &rdata->args,
265                 .rpc_resp       = &rdata->res,
266                 .rpc_cred       = rdata->cred,
267         };
268         int                     status;
269
270         dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
271                         (long long) rdata->args.offset);
272         nfs_fattr_init(fattr);
273         status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
274         if (status >= 0)
275                 nfs_refresh_inode(inode, fattr);
276         dprintk("NFS reply read: %d\n", status);
277         return status;
278 }
279
280 static int nfs3_proc_write(struct nfs_write_data *wdata)
281 {
282         int                     rpcflags = wdata->flags;
283         struct inode *          inode = wdata->inode;
284         struct nfs_fattr *      fattr = wdata->res.fattr;
285         struct rpc_message      msg = {
286                 .rpc_proc       = &nfs3_procedures[NFS3PROC_WRITE],
287                 .rpc_argp       = &wdata->args,
288                 .rpc_resp       = &wdata->res,
289                 .rpc_cred       = wdata->cred,
290         };
291         int                     status;
292
293         dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
294                         (long long) wdata->args.offset);
295         nfs_fattr_init(fattr);
296         status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
297         if (status >= 0)
298                 nfs_post_op_update_inode(inode, fattr);
299         dprintk("NFS reply write: %d\n", status);
300         return status < 0? status : wdata->res.count;
301 }
302
303 static int nfs3_proc_commit(struct nfs_write_data *cdata)
304 {
305         struct inode *          inode = cdata->inode;
306         struct nfs_fattr *      fattr = cdata->res.fattr;
307         struct rpc_message      msg = {
308                 .rpc_proc       = &nfs3_procedures[NFS3PROC_COMMIT],
309                 .rpc_argp       = &cdata->args,
310                 .rpc_resp       = &cdata->res,
311                 .rpc_cred       = cdata->cred,
312         };
313         int                     status;
314
315         dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
316                         (long long) cdata->args.offset);
317         nfs_fattr_init(fattr);
318         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
319         if (status >= 0)
320                 nfs_post_op_update_inode(inode, fattr);
321         dprintk("NFS reply commit: %d\n", status);
322         return status;
323 }
324
325 /*
326  * Create a regular file.
327  * For now, we don't implement O_EXCL.
328  */
329 static int
330 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
331                  int flags, struct nameidata *nd)
332 {
333         struct nfs_fh           fhandle;
334         struct nfs_fattr        fattr;
335         struct nfs_fattr        dir_attr;
336         struct nfs3_createargs  arg = {
337                 .fh             = NFS_FH(dir),
338                 .name           = dentry->d_name.name,
339                 .len            = dentry->d_name.len,
340                 .sattr          = sattr,
341         };
342         struct nfs3_diropres    res = {
343                 .dir_attr       = &dir_attr,
344                 .fh             = &fhandle,
345                 .fattr          = &fattr
346         };
347         struct rpc_message msg = {
348                 .rpc_proc       = &nfs3_procedures[NFS3PROC_CREATE],
349                 .rpc_argp       = &arg,
350                 .rpc_resp       = &res,
351         };
352         mode_t mode = sattr->ia_mode;
353         int status;
354
355         dprintk("NFS call  create %s\n", dentry->d_name.name);
356         arg.createmode = NFS3_CREATE_UNCHECKED;
357         if (flags & O_EXCL) {
358                 arg.createmode  = NFS3_CREATE_EXCLUSIVE;
359                 arg.verifier[0] = jiffies;
360                 arg.verifier[1] = current->pid;
361         }
362
363         sattr->ia_mode &= ~current->fs->umask;
364
365 again:
366         nfs_fattr_init(&dir_attr);
367         nfs_fattr_init(&fattr);
368         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
369         nfs_refresh_inode(dir, &dir_attr);
370
371         /* If the server doesn't support the exclusive creation semantics,
372          * try again with simple 'guarded' mode. */
373         if (status == NFSERR_NOTSUPP) {
374                 switch (arg.createmode) {
375                         case NFS3_CREATE_EXCLUSIVE:
376                                 arg.createmode = NFS3_CREATE_GUARDED;
377                                 break;
378
379                         case NFS3_CREATE_GUARDED:
380                                 arg.createmode = NFS3_CREATE_UNCHECKED;
381                                 break;
382
383                         case NFS3_CREATE_UNCHECKED:
384                                 goto out;
385                 }
386                 goto again;
387         }
388
389         if (status == 0)
390                 status = nfs_instantiate(dentry, &fhandle, &fattr);
391         if (status != 0)
392                 goto out;
393
394         /* When we created the file with exclusive semantics, make
395          * sure we set the attributes afterwards. */
396         if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
397                 dprintk("NFS call  setattr (post-create)\n");
398
399                 if (!(sattr->ia_valid & ATTR_ATIME_SET))
400                         sattr->ia_valid |= ATTR_ATIME;
401                 if (!(sattr->ia_valid & ATTR_MTIME_SET))
402                         sattr->ia_valid |= ATTR_MTIME;
403
404                 /* Note: we could use a guarded setattr here, but I'm
405                  * not sure this buys us anything (and I'd have
406                  * to revamp the NFSv3 XDR code) */
407                 status = nfs3_proc_setattr(dentry, &fattr, sattr);
408                 if (status == 0)
409                         nfs_setattr_update_inode(dentry->d_inode, sattr);
410                 nfs_refresh_inode(dentry->d_inode, &fattr);
411                 dprintk("NFS reply setattr (post-create): %d\n", status);
412         }
413         if (status != 0)
414                 goto out;
415         status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
416 out:
417         dprintk("NFS reply create: %d\n", status);
418         return status;
419 }
420
421 static int
422 nfs3_proc_remove(struct inode *dir, struct qstr *name)
423 {
424         struct nfs_fattr        dir_attr;
425         struct nfs3_diropargs   arg = {
426                 .fh             = NFS_FH(dir),
427                 .name           = name->name,
428                 .len            = name->len
429         };
430         struct rpc_message      msg = {
431                 .rpc_proc       = &nfs3_procedures[NFS3PROC_REMOVE],
432                 .rpc_argp       = &arg,
433                 .rpc_resp       = &dir_attr,
434         };
435         int                     status;
436
437         dprintk("NFS call  remove %s\n", name->name);
438         nfs_fattr_init(&dir_attr);
439         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
440         nfs_post_op_update_inode(dir, &dir_attr);
441         dprintk("NFS reply remove: %d\n", status);
442         return status;
443 }
444
445 static int
446 nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
447 {
448         struct unlinkxdr {
449                 struct nfs3_diropargs arg;
450                 struct nfs_fattr res;
451         } *ptr;
452
453         ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
454         if (!ptr)
455                 return -ENOMEM;
456         ptr->arg.fh = NFS_FH(dir->d_inode);
457         ptr->arg.name = name->name;
458         ptr->arg.len = name->len;
459         nfs_fattr_init(&ptr->res);
460         msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
461         msg->rpc_argp = &ptr->arg;
462         msg->rpc_resp = &ptr->res;
463         return 0;
464 }
465
466 static int
467 nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
468 {
469         struct rpc_message *msg = &task->tk_msg;
470         struct nfs_fattr        *dir_attr;
471
472         if (nfs3_async_handle_jukebox(task, dir->d_inode))
473                 return 1;
474         if (msg->rpc_argp) {
475                 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
476                 nfs_post_op_update_inode(dir->d_inode, dir_attr);
477                 kfree(msg->rpc_argp);
478         }
479         return 0;
480 }
481
482 static int
483 nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
484                  struct inode *new_dir, struct qstr *new_name)
485 {
486         struct nfs_fattr        old_dir_attr, new_dir_attr;
487         struct nfs3_renameargs  arg = {
488                 .fromfh         = NFS_FH(old_dir),
489                 .fromname       = old_name->name,
490                 .fromlen        = old_name->len,
491                 .tofh           = NFS_FH(new_dir),
492                 .toname         = new_name->name,
493                 .tolen          = new_name->len
494         };
495         struct nfs3_renameres   res = {
496                 .fromattr       = &old_dir_attr,
497                 .toattr         = &new_dir_attr
498         };
499         struct rpc_message msg = {
500                 .rpc_proc       = &nfs3_procedures[NFS3PROC_RENAME],
501                 .rpc_argp       = &arg,
502                 .rpc_resp       = &res,
503         };
504         int                     status;
505
506         dprintk("NFS call  rename %s -> %s\n", old_name->name, new_name->name);
507         nfs_fattr_init(&old_dir_attr);
508         nfs_fattr_init(&new_dir_attr);
509         status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
510         nfs_post_op_update_inode(old_dir, &old_dir_attr);
511         nfs_post_op_update_inode(new_dir, &new_dir_attr);
512         dprintk("NFS reply rename: %d\n", status);
513         return status;
514 }
515
516 static int
517 nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
518 {
519         struct nfs_fattr        dir_attr, fattr;
520         struct nfs3_linkargs    arg = {
521                 .fromfh         = NFS_FH(inode),
522                 .tofh           = NFS_FH(dir),
523                 .toname         = name->name,
524                 .tolen          = name->len
525         };
526         struct nfs3_linkres     res = {
527                 .dir_attr       = &dir_attr,
528                 .fattr          = &fattr
529         };
530         struct rpc_message msg = {
531                 .rpc_proc       = &nfs3_procedures[NFS3PROC_LINK],
532                 .rpc_argp       = &arg,
533                 .rpc_resp       = &res,
534         };
535         int                     status;
536
537         dprintk("NFS call  link %s\n", name->name);
538         nfs_fattr_init(&dir_attr);
539         nfs_fattr_init(&fattr);
540         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
541         nfs_post_op_update_inode(dir, &dir_attr);
542         nfs_post_op_update_inode(inode, &fattr);
543         dprintk("NFS reply link: %d\n", status);
544         return status;
545 }
546
547 static int
548 nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
549                   struct iattr *sattr, struct nfs_fh *fhandle,
550                   struct nfs_fattr *fattr)
551 {
552         struct nfs_fattr        dir_attr;
553         struct nfs3_symlinkargs arg = {
554                 .fromfh         = NFS_FH(dir),
555                 .fromname       = name->name,
556                 .fromlen        = name->len,
557                 .topath         = path->name,
558                 .tolen          = path->len,
559                 .sattr          = sattr
560         };
561         struct nfs3_diropres    res = {
562                 .dir_attr       = &dir_attr,
563                 .fh             = fhandle,
564                 .fattr          = fattr
565         };
566         struct rpc_message msg = {
567                 .rpc_proc       = &nfs3_procedures[NFS3PROC_SYMLINK],
568                 .rpc_argp       = &arg,
569                 .rpc_resp       = &res,
570         };
571         int                     status;
572
573         if (path->len > NFS3_MAXPATHLEN)
574                 return -ENAMETOOLONG;
575         dprintk("NFS call  symlink %s -> %s\n", name->name, path->name);
576         nfs_fattr_init(&dir_attr);
577         nfs_fattr_init(fattr);
578         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
579         nfs_post_op_update_inode(dir, &dir_attr);
580         dprintk("NFS reply symlink: %d\n", status);
581         return status;
582 }
583
584 static int
585 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
586 {
587         struct nfs_fh fhandle;
588         struct nfs_fattr fattr, dir_attr;
589         struct nfs3_mkdirargs   arg = {
590                 .fh             = NFS_FH(dir),
591                 .name           = dentry->d_name.name,
592                 .len            = dentry->d_name.len,
593                 .sattr          = sattr
594         };
595         struct nfs3_diropres    res = {
596                 .dir_attr       = &dir_attr,
597                 .fh             = &fhandle,
598                 .fattr          = &fattr
599         };
600         struct rpc_message msg = {
601                 .rpc_proc       = &nfs3_procedures[NFS3PROC_MKDIR],
602                 .rpc_argp       = &arg,
603                 .rpc_resp       = &res,
604         };
605         int mode = sattr->ia_mode;
606         int status;
607
608         dprintk("NFS call  mkdir %s\n", dentry->d_name.name);
609
610         sattr->ia_mode &= ~current->fs->umask;
611
612         nfs_fattr_init(&dir_attr);
613         nfs_fattr_init(&fattr);
614         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
615         nfs_post_op_update_inode(dir, &dir_attr);
616         if (status != 0)
617                 goto out;
618         status = nfs_instantiate(dentry, &fhandle, &fattr);
619         if (status != 0)
620                 goto out;
621         status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
622 out:
623         dprintk("NFS reply mkdir: %d\n", status);
624         return status;
625 }
626
627 static int
628 nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
629 {
630         struct nfs_fattr        dir_attr;
631         struct nfs3_diropargs   arg = {
632                 .fh             = NFS_FH(dir),
633                 .name           = name->name,
634                 .len            = name->len
635         };
636         struct rpc_message msg = {
637                 .rpc_proc       = &nfs3_procedures[NFS3PROC_RMDIR],
638                 .rpc_argp       = &arg,
639                 .rpc_resp       = &dir_attr,
640         };
641         int                     status;
642
643         dprintk("NFS call  rmdir %s\n", name->name);
644         nfs_fattr_init(&dir_attr);
645         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
646         nfs_post_op_update_inode(dir, &dir_attr);
647         dprintk("NFS reply rmdir: %d\n", status);
648         return status;
649 }
650
651 /*
652  * The READDIR implementation is somewhat hackish - we pass the user buffer
653  * to the encode function, which installs it in the receive iovec.
654  * The decode function itself doesn't perform any decoding, it just makes
655  * sure the reply is syntactically correct.
656  *
657  * Also note that this implementation handles both plain readdir and
658  * readdirplus.
659  */
660 static int
661 nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
662                   u64 cookie, struct page *page, unsigned int count, int plus)
663 {
664         struct inode            *dir = dentry->d_inode;
665         struct nfs_fattr        dir_attr;
666         u32                     *verf = NFS_COOKIEVERF(dir);
667         struct nfs3_readdirargs arg = {
668                 .fh             = NFS_FH(dir),
669                 .cookie         = cookie,
670                 .verf           = {verf[0], verf[1]},
671                 .plus           = plus,
672                 .count          = count,
673                 .pages          = &page
674         };
675         struct nfs3_readdirres  res = {
676                 .dir_attr       = &dir_attr,
677                 .verf           = verf,
678                 .plus           = plus
679         };
680         struct rpc_message      msg = {
681                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READDIR],
682                 .rpc_argp       = &arg,
683                 .rpc_resp       = &res,
684                 .rpc_cred       = cred
685         };
686         int                     status;
687
688         lock_kernel();
689
690         if (plus)
691                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
692
693         dprintk("NFS call  readdir%s %d\n",
694                         plus? "plus" : "", (unsigned int) cookie);
695
696         nfs_fattr_init(&dir_attr);
697         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
698         nfs_refresh_inode(dir, &dir_attr);
699         dprintk("NFS reply readdir: %d\n", status);
700         unlock_kernel();
701         return status;
702 }
703
704 static int
705 nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
706                 dev_t rdev)
707 {
708         struct nfs_fh fh;
709         struct nfs_fattr fattr, dir_attr;
710         struct nfs3_mknodargs   arg = {
711                 .fh             = NFS_FH(dir),
712                 .name           = dentry->d_name.name,
713                 .len            = dentry->d_name.len,
714                 .sattr          = sattr,
715                 .rdev           = rdev
716         };
717         struct nfs3_diropres    res = {
718                 .dir_attr       = &dir_attr,
719                 .fh             = &fh,
720                 .fattr          = &fattr
721         };
722         struct rpc_message msg = {
723                 .rpc_proc       = &nfs3_procedures[NFS3PROC_MKNOD],
724                 .rpc_argp       = &arg,
725                 .rpc_resp       = &res,
726         };
727         mode_t mode = sattr->ia_mode;
728         int status;
729
730         switch (sattr->ia_mode & S_IFMT) {
731         case S_IFBLK:   arg.type = NF3BLK;  break;
732         case S_IFCHR:   arg.type = NF3CHR;  break;
733         case S_IFIFO:   arg.type = NF3FIFO; break;
734         case S_IFSOCK:  arg.type = NF3SOCK; break;
735         default:        return -EINVAL;
736         }
737
738         dprintk("NFS call  mknod %s %u:%u\n", dentry->d_name.name,
739                         MAJOR(rdev), MINOR(rdev));
740
741         sattr->ia_mode &= ~current->fs->umask;
742
743         nfs_fattr_init(&dir_attr);
744         nfs_fattr_init(&fattr);
745         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
746         nfs_post_op_update_inode(dir, &dir_attr);
747         if (status != 0)
748                 goto out;
749         status = nfs_instantiate(dentry, &fh, &fattr);
750         if (status != 0)
751                 goto out;
752         status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
753 out:
754         dprintk("NFS reply mknod: %d\n", status);
755         return status;
756 }
757
758 static int
759 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
760                  struct nfs_fsstat *stat)
761 {
762         struct rpc_message msg = {
763                 .rpc_proc       = &nfs3_procedures[NFS3PROC_FSSTAT],
764                 .rpc_argp       = fhandle,
765                 .rpc_resp       = stat,
766         };
767         int     status;
768
769         dprintk("NFS call  fsstat\n");
770         nfs_fattr_init(stat->fattr);
771         status = rpc_call_sync(server->client, &msg, 0);
772         dprintk("NFS reply statfs: %d\n", status);
773         return status;
774 }
775
776 static int
777 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
778                  struct nfs_fsinfo *info)
779 {
780         struct rpc_message msg = {
781                 .rpc_proc       = &nfs3_procedures[NFS3PROC_FSINFO],
782                 .rpc_argp       = fhandle,
783                 .rpc_resp       = info,
784         };
785         int     status;
786
787         dprintk("NFS call  fsinfo\n");
788         nfs_fattr_init(info->fattr);
789         status = rpc_call_sync(server->client_sys, &msg, 0);
790         dprintk("NFS reply fsinfo: %d\n", status);
791         return status;
792 }
793
794 static int
795 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
796                    struct nfs_pathconf *info)
797 {
798         struct rpc_message msg = {
799                 .rpc_proc       = &nfs3_procedures[NFS3PROC_PATHCONF],
800                 .rpc_argp       = fhandle,
801                 .rpc_resp       = info,
802         };
803         int     status;
804
805         dprintk("NFS call  pathconf\n");
806         nfs_fattr_init(info->fattr);
807         status = rpc_call_sync(server->client, &msg, 0);
808         dprintk("NFS reply pathconf: %d\n", status);
809         return status;
810 }
811
812 extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
813
814 static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
815 {
816         if (nfs3_async_handle_jukebox(task, data->inode))
817                 return -EAGAIN;
818         /* Call back common NFS readpage processing */
819         if (task->tk_status >= 0)
820                 nfs_refresh_inode(data->inode, &data->fattr);
821         return 0;
822 }
823
824 static void nfs3_proc_read_setup(struct nfs_read_data *data)
825 {
826         struct rpc_message      msg = {
827                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READ],
828                 .rpc_argp       = &data->args,
829                 .rpc_resp       = &data->res,
830                 .rpc_cred       = data->cred,
831         };
832
833         rpc_call_setup(&data->task, &msg, 0);
834 }
835
836 static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
837 {
838         if (nfs3_async_handle_jukebox(task, data->inode))
839                 return -EAGAIN;
840         if (task->tk_status >= 0)
841                 nfs_post_op_update_inode(data->inode, data->res.fattr);
842         return 0;
843 }
844
845 static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
846 {
847         struct rpc_message      msg = {
848                 .rpc_proc       = &nfs3_procedures[NFS3PROC_WRITE],
849                 .rpc_argp       = &data->args,
850                 .rpc_resp       = &data->res,
851                 .rpc_cred       = data->cred,
852         };
853
854         data->args.stable = NFS_UNSTABLE;
855         if (how & FLUSH_STABLE) {
856                 data->args.stable = NFS_FILE_SYNC;
857                 if (NFS_I(data->inode)->ncommit)
858                         data->args.stable = NFS_DATA_SYNC;
859         }
860
861         /* Finalize the task. */
862         rpc_call_setup(&data->task, &msg, 0);
863 }
864
865 static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
866 {
867         if (nfs3_async_handle_jukebox(task, data->inode))
868                 return -EAGAIN;
869         if (task->tk_status >= 0)
870                 nfs_post_op_update_inode(data->inode, data->res.fattr);
871         return 0;
872 }
873
874 static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
875 {
876         struct rpc_message      msg = {
877                 .rpc_proc       = &nfs3_procedures[NFS3PROC_COMMIT],
878                 .rpc_argp       = &data->args,
879                 .rpc_resp       = &data->res,
880                 .rpc_cred       = data->cred,
881         };
882
883         rpc_call_setup(&data->task, &msg, 0);
884 }
885
886 static int
887 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
888 {
889         return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
890 }
891
892 struct nfs_rpc_ops      nfs_v3_clientops = {
893         .version        = 3,                    /* protocol version */
894         .dentry_ops     = &nfs_dentry_operations,
895         .dir_inode_ops  = &nfs3_dir_inode_operations,
896         .file_inode_ops = &nfs3_file_inode_operations,
897         .getroot        = nfs3_proc_get_root,
898         .getattr        = nfs3_proc_getattr,
899         .setattr        = nfs3_proc_setattr,
900         .lookup         = nfs3_proc_lookup,
901         .access         = nfs3_proc_access,
902         .readlink       = nfs3_proc_readlink,
903         .read           = nfs3_proc_read,
904         .write          = nfs3_proc_write,
905         .commit         = nfs3_proc_commit,
906         .create         = nfs3_proc_create,
907         .remove         = nfs3_proc_remove,
908         .unlink_setup   = nfs3_proc_unlink_setup,
909         .unlink_done    = nfs3_proc_unlink_done,
910         .rename         = nfs3_proc_rename,
911         .link           = nfs3_proc_link,
912         .symlink        = nfs3_proc_symlink,
913         .mkdir          = nfs3_proc_mkdir,
914         .rmdir          = nfs3_proc_rmdir,
915         .readdir        = nfs3_proc_readdir,
916         .mknod          = nfs3_proc_mknod,
917         .statfs         = nfs3_proc_statfs,
918         .fsinfo         = nfs3_proc_fsinfo,
919         .pathconf       = nfs3_proc_pathconf,
920         .decode_dirent  = nfs3_decode_dirent,
921         .read_setup     = nfs3_proc_read_setup,
922         .read_done      = nfs3_read_done,
923         .write_setup    = nfs3_proc_write_setup,
924         .write_done     = nfs3_write_done,
925         .commit_setup   = nfs3_proc_commit_setup,
926         .commit_done    = nfs3_commit_done,
927         .file_open      = nfs_open,
928         .file_release   = nfs_release,
929         .lock           = nfs3_proc_lock,
930         .clear_acl_cache = nfs3_forget_cached_acls,
931 };