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