a7e1ee653f75546b6a0e47232b1d509783f2bbd3
[linux-flexiantxendom0-3.2.10.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 #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 static struct rpc_cred *
73 nfs_cred(struct inode *inode, struct file *filp)
74 {
75         struct rpc_cred *cred = NULL;
76
77         if (filp)
78                 cred = (struct rpc_cred *)filp->private_data;
79         if (!cred)
80                 cred = NFS_I(inode)->mm_cred;
81         return cred;
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         dprintk("%s: call  fsinfo\n", __FUNCTION__);
94         info->fattr->valid = 0;
95         status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
96         dprintk("%s: reply fsinfo %d\n", __FUNCTION__, status);
97         if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
98                 status = rpc_call(server->client_sys, NFS3PROC_GETATTR, fhandle, info->fattr, 0);
99                 dprintk("%s: reply getattr %d\n", __FUNCTION__, status);
100         }
101         return status;
102 }
103
104 /*
105  * One function for each procedure in the NFS protocol.
106  */
107 static int
108 nfs3_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
109 {
110         int     status;
111
112         dprintk("NFS call  getattr\n");
113         fattr->valid = 0;
114         status = rpc_call(NFS_CLIENT(inode), NFS3PROC_GETATTR,
115                           NFS_FH(inode), fattr, 0);
116         dprintk("NFS reply getattr\n");
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         int     status;
130
131         dprintk("NFS call  setattr\n");
132         fattr->valid = 0;
133         status = rpc_call(NFS_CLIENT(inode), NFS3PROC_SETATTR, &arg, fattr, 0);
134         dprintk("NFS reply setattr\n");
135         return status;
136 }
137
138 static int
139 nfs3_proc_lookup(struct inode *dir, struct qstr *name,
140                  struct nfs_fh *fhandle, struct nfs_fattr *fattr)
141 {
142         struct nfs_fattr        dir_attr;
143         struct nfs3_diropargs   arg = {
144                 .fh             = NFS_FH(dir),
145                 .name           = name->name,
146                 .len            = name->len
147         };
148         struct nfs3_diropres    res = {
149                 .dir_attr       = &dir_attr,
150                 .fh             = fhandle,
151                 .fattr          = fattr
152         };
153         int                     status;
154
155         dprintk("NFS call  lookup %s\n", name->name);
156         dir_attr.valid = 0;
157         fattr->valid = 0;
158         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_LOOKUP, &arg, &res, 0);
159         if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
160                 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_GETATTR,
161                          fhandle, fattr, 0);
162         dprintk("NFS reply lookup: %d\n", status);
163         if (status >= 0)
164                 status = nfs_refresh_inode(dir, &dir_attr);
165         return status;
166 }
167
168 static int
169 nfs3_proc_access(struct inode *inode, struct rpc_cred *cred, int mode)
170 {
171         struct nfs_fattr        fattr;
172         struct nfs3_accessargs  arg = {
173                 .fh             = NFS_FH(inode),
174         };
175         struct nfs3_accessres   res = {
176                 .fattr          = &fattr,
177         };
178         struct rpc_message msg = {
179                 .rpc_proc       = &nfs3_procedures[NFS3PROC_ACCESS],
180                 .rpc_argp       = &arg,
181                 .rpc_resp       = &res,
182                 .rpc_cred       = cred
183         };
184         int     status;
185
186         dprintk("NFS call  access\n");
187         fattr.valid = 0;
188
189         if (mode & MAY_READ)
190                 arg.access |= NFS3_ACCESS_READ;
191         if (S_ISDIR(inode->i_mode)) {
192                 if (mode & MAY_WRITE)
193                         arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
194                 if (mode & MAY_EXEC)
195                         arg.access |= NFS3_ACCESS_LOOKUP;
196         } else {
197                 if (mode & MAY_WRITE)
198                         arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
199                 if (mode & MAY_EXEC)
200                         arg.access |= NFS3_ACCESS_EXECUTE;
201         }
202         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
203         nfs_refresh_inode(inode, &fattr);
204         dprintk("NFS reply access\n");
205
206         if (status == 0 && (arg.access & res.access) != arg.access)
207                 status = -EACCES;
208         return status;
209 }
210
211 static int
212 nfs3_proc_readlink(struct inode *inode, struct page *page)
213 {
214         struct nfs_fattr        fattr;
215         struct nfs3_readlinkargs args = {
216                 .fh             = NFS_FH(inode),
217                 .count          = PAGE_CACHE_SIZE,
218                 .pages          = &page
219         };
220         int                     status;
221
222         dprintk("NFS call  readlink\n");
223         fattr.valid = 0;
224         status = rpc_call(NFS_CLIENT(inode), NFS3PROC_READLINK,
225                           &args, &fattr, 0);
226         nfs_refresh_inode(inode, &fattr);
227         dprintk("NFS reply readlink: %d\n", status);
228         return status;
229 }
230
231 static int
232 nfs3_proc_read(struct nfs_read_data *rdata, struct file *filp)
233 {
234         int                     flags = rdata->flags;
235         struct inode *          inode = rdata->inode;
236         struct nfs_fattr *      fattr = rdata->res.fattr;
237         struct rpc_message      msg = {
238                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READ],
239                 .rpc_argp       = &rdata->args,
240                 .rpc_resp       = &rdata->res,
241         };
242         int                     status;
243
244         dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
245                         (long long) rdata->args.offset);
246         fattr->valid = 0;
247         msg.rpc_cred = nfs_cred(inode, filp);
248         status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
249         if (status >= 0)
250                 nfs_refresh_inode(inode, fattr);
251         dprintk("NFS reply read: %d\n", status);
252         return status;
253 }
254
255 static int
256 nfs3_proc_write(struct nfs_write_data *wdata, struct file *filp)
257 {
258         int                     rpcflags = wdata->flags;
259         struct inode *          inode = wdata->inode;
260         struct nfs_fattr *      fattr = wdata->res.fattr;
261         struct rpc_message      msg = {
262                 .rpc_proc       = &nfs3_procedures[NFS3PROC_WRITE],
263                 .rpc_argp       = &wdata->args,
264                 .rpc_resp       = &wdata->res,
265         };
266         int                     status;
267
268         dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
269                         (long long) wdata->args.offset);
270         fattr->valid = 0;
271         msg.rpc_cred = nfs_cred(inode, filp);
272         status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
273         if (status >= 0)
274                 nfs_refresh_inode(inode, fattr);
275         dprintk("NFS reply write: %d\n", status);
276         return status < 0? status : wdata->res.count;
277 }
278
279 static int
280 nfs3_proc_commit(struct nfs_write_data *cdata, struct file *filp)
281 {
282         struct inode *          inode = cdata->inode;
283         struct nfs_fattr *      fattr = cdata->res.fattr;
284         struct rpc_message      msg = {
285                 .rpc_proc       = &nfs3_procedures[NFS3PROC_COMMIT],
286                 .rpc_argp       = &cdata->args,
287                 .rpc_resp       = &cdata->res,
288         };
289         int                     status;
290
291         dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
292                         (long long) cdata->args.offset);
293         fattr->valid = 0;
294         msg.rpc_cred = nfs_cred(inode, filp);
295         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
296         if (status >= 0)
297                 nfs_refresh_inode(inode, fattr);
298         dprintk("NFS reply commit: %d\n", status);
299         return status;
300 }
301
302 /*
303  * Create a regular file.
304  * For now, we don't implement O_EXCL.
305  */
306 static struct inode *
307 nfs3_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
308                  int flags)
309 {
310         struct nfs_fh           fhandle;
311         struct nfs_fattr        fattr;
312         struct nfs_fattr        dir_attr;
313         struct nfs3_createargs  arg = {
314                 .fh             = NFS_FH(dir),
315                 .name           = name->name,
316                 .len            = name->len,
317                 .sattr          = sattr,
318         };
319         struct nfs3_diropres    res = {
320                 .dir_attr       = &dir_attr,
321                 .fh             = &fhandle,
322                 .fattr          = &fattr
323         };
324         int                     status;
325
326         dprintk("NFS call  create %s\n", name->name);
327         arg.createmode = NFS3_CREATE_UNCHECKED;
328         if (flags & O_EXCL) {
329                 arg.createmode  = NFS3_CREATE_EXCLUSIVE;
330                 arg.verifier[0] = jiffies;
331                 arg.verifier[1] = current->pid;
332         }
333
334 again:
335         dir_attr.valid = 0;
336         fattr.valid = 0;
337         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_CREATE, &arg, &res, 0);
338         nfs_refresh_inode(dir, &dir_attr);
339
340         /* If the server doesn't support the exclusive creation semantics,
341          * try again with simple 'guarded' mode. */
342         if (status == NFSERR_NOTSUPP) {
343                 switch (arg.createmode) {
344                         case NFS3_CREATE_EXCLUSIVE:
345                                 arg.createmode = NFS3_CREATE_GUARDED;
346                                 break;
347
348                         case NFS3_CREATE_GUARDED:
349                                 arg.createmode = NFS3_CREATE_UNCHECKED;
350                                 break;
351
352                         case NFS3_CREATE_UNCHECKED:
353                                 goto exit;
354                 }
355                 goto again;
356         }
357
358 exit:
359         dprintk("NFS reply create: %d\n", status);
360
361         if (status != 0)
362                 goto out;
363         if (fhandle.size == 0 || !(fattr.valid & NFS_ATTR_FATTR)) {
364                 status = nfs3_proc_lookup(dir, name, &fhandle, &fattr);
365                 if (status != 0)
366                         goto out;
367         }
368
369         /* When we created the file with exclusive semantics, make
370          * sure we set the attributes afterwards. */
371         if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
372                 struct nfs3_sattrargs   arg = {
373                         .fh             = &fhandle,
374                         .sattr          = sattr,
375                 };
376                 dprintk("NFS call  setattr (post-create)\n");
377
378                 if (!(sattr->ia_valid & ATTR_ATIME_SET))
379                         sattr->ia_valid |= ATTR_ATIME;
380                 if (!(sattr->ia_valid & ATTR_MTIME_SET))
381                         sattr->ia_valid |= ATTR_MTIME;
382
383                 /* Note: we could use a guarded setattr here, but I'm
384                  * not sure this buys us anything (and I'd have
385                  * to revamp the NFSv3 XDR code) */
386                 fattr.valid = 0;
387                 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SETATTR,
388                                                 &arg, &fattr, 0);
389                 dprintk("NFS reply setattr (post-create): %d\n", status);
390         }
391         if (status == 0) {
392                 struct inode *inode;
393                 inode = nfs_fhget(dir->i_sb, &fhandle, &fattr);
394                 if (inode)
395                         return inode;
396                 status = -ENOMEM;
397         }
398 out:
399         return ERR_PTR(status);
400 }
401
402 static int
403 nfs3_proc_remove(struct inode *dir, struct qstr *name)
404 {
405         struct nfs_fattr        dir_attr;
406         struct nfs3_diropargs   arg = {
407                 .fh             = NFS_FH(dir),
408                 .name           = name->name,
409                 .len            = name->len
410         };
411         struct rpc_message      msg = {
412                 .rpc_proc       = &nfs3_procedures[NFS3PROC_REMOVE],
413                 .rpc_argp       = &arg,
414                 .rpc_resp       = &dir_attr,
415         };
416         int                     status;
417
418         dprintk("NFS call  remove %s\n", name->name);
419         dir_attr.valid = 0;
420         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
421         nfs_refresh_inode(dir, &dir_attr);
422         dprintk("NFS reply remove: %d\n", status);
423         return status;
424 }
425
426 static int
427 nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
428 {
429         struct nfs3_diropargs   *arg;
430         struct nfs_fattr        *res;
431
432         arg = (struct nfs3_diropargs *)kmalloc(sizeof(*arg)+sizeof(*res), GFP_KERNEL);
433         if (!arg)
434                 return -ENOMEM;
435         res = (struct nfs_fattr*)(arg + 1);
436         arg->fh = NFS_FH(dir->d_inode);
437         arg->name = name->name;
438         arg->len = name->len;
439         res->valid = 0;
440         msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
441         msg->rpc_argp = arg;
442         msg->rpc_resp = res;
443         return 0;
444 }
445
446 static int
447 nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
448 {
449         struct rpc_message *msg = &task->tk_msg;
450         struct nfs_fattr        *dir_attr;
451
452         if (nfs3_async_handle_jukebox(task))
453                 return 1;
454         if (msg->rpc_argp) {
455                 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
456                 nfs_refresh_inode(dir->d_inode, dir_attr);
457                 kfree(msg->rpc_argp);
458         }
459         return 0;
460 }
461
462 static int
463 nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
464                  struct inode *new_dir, struct qstr *new_name)
465 {
466         struct nfs_fattr        old_dir_attr, new_dir_attr;
467         struct nfs3_renameargs  arg = {
468                 .fromfh         = NFS_FH(old_dir),
469                 .fromname       = old_name->name,
470                 .fromlen        = old_name->len,
471                 .tofh           = NFS_FH(new_dir),
472                 .toname         = new_name->name,
473                 .tolen          = new_name->len
474         };
475         struct nfs3_renameres   res = {
476                 .fromattr       = &old_dir_attr,
477                 .toattr         = &new_dir_attr
478         };
479         int                     status;
480
481         dprintk("NFS call  rename %s -> %s\n", old_name->name, new_name->name);
482         old_dir_attr.valid = 0;
483         new_dir_attr.valid = 0;
484         status = rpc_call(NFS_CLIENT(old_dir), NFS3PROC_RENAME, &arg, &res, 0);
485         nfs_refresh_inode(old_dir, &old_dir_attr);
486         nfs_refresh_inode(new_dir, &new_dir_attr);
487         dprintk("NFS reply rename: %d\n", status);
488         return status;
489 }
490
491 static int
492 nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
493 {
494         struct nfs_fattr        dir_attr, fattr;
495         struct nfs3_linkargs    arg = {
496                 .fromfh         = NFS_FH(inode),
497                 .tofh           = NFS_FH(dir),
498                 .toname         = name->name,
499                 .tolen          = name->len
500         };
501         struct nfs3_linkres     res = {
502                 .dir_attr       = &dir_attr,
503                 .fattr          = &fattr
504         };
505         int                     status;
506
507         dprintk("NFS call  link %s\n", name->name);
508         dir_attr.valid = 0;
509         fattr.valid = 0;
510         status = rpc_call(NFS_CLIENT(inode), NFS3PROC_LINK, &arg, &res, 0);
511         nfs_refresh_inode(dir, &dir_attr);
512         nfs_refresh_inode(inode, &fattr);
513         dprintk("NFS reply link: %d\n", status);
514         return status;
515 }
516
517 static int
518 nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
519                   struct iattr *sattr, struct nfs_fh *fhandle,
520                   struct nfs_fattr *fattr)
521 {
522         struct nfs_fattr        dir_attr;
523         struct nfs3_symlinkargs arg = {
524                 .fromfh         = NFS_FH(dir),
525                 .fromname       = name->name,
526                 .fromlen        = name->len,
527                 .topath         = path->name,
528                 .tolen          = path->len,
529                 .sattr          = sattr
530         };
531         struct nfs3_diropres    res = {
532                 .dir_attr       = &dir_attr,
533                 .fh             = fhandle,
534                 .fattr          = fattr
535         };
536         int                     status;
537
538         dprintk("NFS call  symlink %s -> %s\n", name->name, path->name);
539         dir_attr.valid = 0;
540         fattr->valid = 0;
541         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SYMLINK, &arg, &res, 0);
542         nfs_refresh_inode(dir, &dir_attr);
543         dprintk("NFS reply symlink: %d\n", status);
544         return status;
545 }
546
547 static int
548 nfs3_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
549                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
550 {
551         struct nfs_fattr        dir_attr;
552         struct nfs3_mkdirargs   arg = {
553                 .fh             = NFS_FH(dir),
554                 .name           = name->name,
555                 .len            = name->len,
556                 .sattr          = sattr
557         };
558         struct nfs3_diropres    res = {
559                 .dir_attr       = &dir_attr,
560                 .fh             = fhandle,
561                 .fattr          = fattr
562         };
563         int                     status;
564
565         dprintk("NFS call  mkdir %s\n", name->name);
566         dir_attr.valid = 0;
567         fattr->valid = 0;
568         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKDIR, &arg, &res, 0);
569         nfs_refresh_inode(dir, &dir_attr);
570         dprintk("NFS reply mkdir: %d\n", status);
571         return status;
572 }
573
574 static int
575 nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
576 {
577         struct nfs_fattr        dir_attr;
578         struct nfs3_diropargs   arg = {
579                 .fh             = NFS_FH(dir),
580                 .name           = name->name,
581                 .len            = name->len
582         };
583         int                     status;
584
585         dprintk("NFS call  rmdir %s\n", name->name);
586         dir_attr.valid = 0;
587         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
588         nfs_refresh_inode(dir, &dir_attr);
589         dprintk("NFS reply rmdir: %d\n", status);
590         return status;
591 }
592
593 /*
594  * The READDIR implementation is somewhat hackish - we pass the user buffer
595  * to the encode function, which installs it in the receive iovec.
596  * The decode function itself doesn't perform any decoding, it just makes
597  * sure the reply is syntactically correct.
598  *
599  * Also note that this implementation handles both plain readdir and
600  * readdirplus.
601  */
602 static int
603 nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
604                   u64 cookie, struct page *page, unsigned int count, int plus)
605 {
606         struct inode            *dir = dentry->d_inode;
607         struct nfs_fattr        dir_attr;
608         u32                     *verf = NFS_COOKIEVERF(dir);
609         struct nfs3_readdirargs arg = {
610                 .fh             = NFS_FH(dir),
611                 .cookie         = cookie,
612                 .verf           = {verf[0], verf[1]},
613                 .plus           = plus,
614                 .count          = count,
615                 .pages          = &page
616         };
617         struct nfs3_readdirres  res = {
618                 .dir_attr       = &dir_attr,
619                 .verf           = verf,
620                 .plus           = plus
621         };
622         struct rpc_message      msg = {
623                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READDIR],
624                 .rpc_argp       = &arg,
625                 .rpc_resp       = &res,
626                 .rpc_cred       = cred
627         };
628         int                     status;
629
630         lock_kernel();
631
632         if (plus)
633                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
634
635         dprintk("NFS call  readdir%s %d\n",
636                         plus? "plus" : "", (unsigned int) cookie);
637
638         dir_attr.valid = 0;
639         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
640         nfs_refresh_inode(dir, &dir_attr);
641         dprintk("NFS reply readdir: %d\n", status);
642         unlock_kernel();
643         return status;
644 }
645
646 static int
647 nfs3_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
648                 dev_t rdev, struct nfs_fh *fh, struct nfs_fattr *fattr)
649 {
650         struct nfs_fattr        dir_attr;
651         struct nfs3_mknodargs   arg = {
652                 .fh             = NFS_FH(dir),
653                 .name           = name->name,
654                 .len            = name->len,
655                 .sattr          = sattr,
656                 .rdev           = rdev
657         };
658         struct nfs3_diropres    res = {
659                 .dir_attr       = &dir_attr,
660                 .fh             = fh,
661                 .fattr          = fattr
662         };
663         int                     status;
664
665         switch (sattr->ia_mode & S_IFMT) {
666         case S_IFBLK:   arg.type = NF3BLK;  break;
667         case S_IFCHR:   arg.type = NF3CHR;  break;
668         case S_IFIFO:   arg.type = NF3FIFO; break;
669         case S_IFSOCK:  arg.type = NF3SOCK; break;
670         default:        return -EINVAL;
671         }
672
673         dprintk("NFS call  mknod %s %u:%u\n", name->name,
674                         MAJOR(rdev), MINOR(rdev));
675         dir_attr.valid = 0;
676         fattr->valid = 0;
677         status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKNOD, &arg, &res, 0);
678         nfs_refresh_inode(dir, &dir_attr);
679         dprintk("NFS reply mknod: %d\n", status);
680         return status;
681 }
682
683 static int
684 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
685                  struct nfs_fsstat *stat)
686 {
687         int     status;
688
689         dprintk("NFS call  fsstat\n");
690         stat->fattr->valid = 0;
691         status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, stat, 0);
692         dprintk("NFS reply statfs: %d\n", status);
693         return status;
694 }
695
696 static int
697 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
698                  struct nfs_fsinfo *info)
699 {
700         int     status;
701
702         dprintk("NFS call  fsinfo\n");
703         info->fattr->valid = 0;
704         status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
705         dprintk("NFS reply fsinfo: %d\n", status);
706         return status;
707 }
708
709 static int
710 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
711                    struct nfs_pathconf *info)
712 {
713         int     status;
714
715         dprintk("NFS call  pathconf\n");
716         info->fattr->valid = 0;
717         status = rpc_call(server->client, NFS3PROC_PATHCONF, fhandle, info, 0);
718         dprintk("NFS reply pathconf: %d\n", status);
719         return status;
720 }
721
722 #ifdef CONFIG_NFS_ACL
723 static int
724 nfs3_proc_checkacls(struct inode *inode)
725 {
726         struct nfs_server *server = NFS_SERVER(inode);
727         struct nfs_fattr fattr;
728         struct nfs3_getaclargs args = { };
729         struct nfs3_getaclres res = { &fattr };
730         int status;
731
732         if (!(server->flags & NFSACL) || (server->flags & NFS_MOUNT_NOACL))
733                 return -EOPNOTSUPP;
734
735         args.fh = NFS_FH(inode);
736         args.mask = NFS3_ACLCNT | NFS3_DFACLCNT;
737
738         dprintk("NFS call getacl\n");
739         status = rpc_call(server->acl_client, NFS3PROC_GETACL,
740                           &args, &res, 0);
741         dprintk("NFS reply getacl: %d\n", status);
742
743         if (status) {
744                 if (status == -ENOSYS) {
745                         dprintk("NFS_ACL extension not supported; disabling\n");
746                         server->flags &= ~NFSACL;
747                         status = -EOPNOTSUPP;
748                 } else if (status == -ENOTSUPP)
749                         status = -EOPNOTSUPP;
750                 goto getout;
751         }
752         if ((args.mask & res.mask) != args.mask) {
753                 status = -EIO;
754                 goto getout;
755         }
756
757         status = nfs_refresh_inode(inode, &fattr);
758
759 getout:
760         posix_acl_release(res.acl_access);
761         posix_acl_release(res.acl_default);
762
763         if (!status) {
764                 /* The (count > 4) test will exclude ACL entries from the list
765                    of names even if their ACL_GROUP_ENTRY and ACL_MASK have
766                    different permissions. Getacl still returns these as
767                    four-entry ACLs, instead of minimal (three-entry) ACLs. */
768                    
769                 if ((args.mask & NFS3_ACLCNT) && res.acl_access_count > 4)
770                         status |= ACL_TYPE_ACCESS;
771                 if ((args.mask & NFS3_DFACLCNT) && res.acl_default_count > 0)
772                         status |= ACL_TYPE_DEFAULT;
773         }
774         return status;
775 }
776 #endif  /* CONFIG_NFS_ACL */
777
778 #ifdef CONFIG_NFS_ACL
779 static struct posix_acl *
780 nfs3_proc_getacl(struct inode *inode, int type)
781 {
782         struct nfs_server *server = NFS_SERVER(inode);
783         struct nfs_fattr fattr;
784         struct page *pages[NFSACL_MAXPAGES] = { };
785         struct nfs3_getaclargs args = {
786                 /* The xdr layer may allocate pages here. */
787                 .pages =        pages,
788         };
789         struct nfs3_getaclres res = {
790                 .fattr =        &fattr,
791         };
792         struct posix_acl *acl = NULL;
793         int status, count;
794
795         if (!(server->flags & NFSACL) || (server->flags & NFS_MOUNT_NOACL))
796                 return ERR_PTR(-EOPNOTSUPP);
797
798         args.fh = NFS_FH(inode);
799         switch(type) {
800                 case ACL_TYPE_ACCESS:
801                         args.mask = NFS3_ACLCNT|NFS3_ACL;
802                         break;
803
804                 case ACL_TYPE_DEFAULT:
805                         if (!S_ISDIR(inode->i_mode))
806                                 return NULL;
807                         args.mask = NFS3_DFACLCNT|NFS3_DFACL;
808                         break;
809
810                 default:
811                         return ERR_PTR(-EINVAL);
812         }
813
814         dprintk("NFS call getacl\n");
815         status = rpc_call(server->acl_client, NFS3PROC_GETACL,
816                           &args, &res, 0);
817         dprintk("NFS reply getacl: %d\n", status);
818
819         /* pages may have been allocated at the xdr layer. */
820         for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
821                 __free_page(args.pages[count]);
822
823         if (status) {
824                 if (status == -ENOSYS) {
825                         dprintk("NFS_ACL extension not supported; disabling\n");
826                         server->flags &= ~NFSACL;
827                         status = -EOPNOTSUPP;
828                 } else if (status == -ENOTSUPP)
829                         status = -EOPNOTSUPP;
830                 goto getout;
831         }
832         if ((args.mask & res.mask) != args.mask) {
833                 status = -EIO;
834                 goto getout;
835         }
836
837         if (type == ACL_TYPE_ACCESS) {
838                 if (res.acl_access) {
839                         mode_t mode = inode->i_mode;
840                         if (!posix_acl_equiv_mode(res.acl_access, &mode) &&
841                             inode->i_mode == mode) {
842                                 posix_acl_release(res.acl_access);
843                                 res.acl_access = NULL;
844                         }
845                 }
846                 acl = res.acl_access;
847                 res.acl_access = NULL;
848         } else {
849                 acl = res.acl_default;
850                 res.acl_default = NULL;
851         }
852
853         status = nfs_refresh_inode(inode, &fattr);
854
855 getout:
856         posix_acl_release(res.acl_access);
857         posix_acl_release(res.acl_default);
858
859         if (status) {
860                 posix_acl_release(acl);
861                 acl = ERR_PTR(status);
862         }
863         return acl;
864 }
865 #endif  /* CONFIG_NFS_ACL */
866
867 #ifdef CONFIG_NFS_ACL
868 static int
869 nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
870 {
871         struct nfs_server *server = NFS_SERVER(inode);
872         struct nfs_fattr fattr;
873         struct page *pages[NFSACL_MAXPAGES] = { };
874         struct nfs3_setaclargs args = {
875                 .pages = pages,
876         };
877         int status, count;
878
879         if (!(server->flags & NFSACL) || (server->flags & NFS_MOUNT_NOACL))
880                 return -EOPNOTSUPP;
881
882         /* We are doing this here, because XDR marshalling can only
883            return -ENOMEM. */
884         if (acl && acl->a_count > NFS3_ACL_MAX_ENTRIES)
885                 return -ENOSPC;
886         args.inode = inode;
887         args.mask = NFS3_ACL|NFS3_DFACL;
888         if (S_ISDIR(inode->i_mode)) {
889                 switch(type) {
890                         case ACL_TYPE_ACCESS:
891                                 args.acl_access = acl;
892                                 args.acl_default = NFS_PROTO(inode)->getacl(
893                                         inode, ACL_TYPE_DEFAULT);
894                                 status = PTR_ERR(args.acl_default);
895                                 if (IS_ERR(args.acl_default)) {
896                                         args.acl_default = NULL;
897                                         goto cleanup;
898                                 }
899                                 break;
900
901                         case ACL_TYPE_DEFAULT:
902                                 args.acl_access = NFS_PROTO(inode)->getacl(
903                                         inode, ACL_TYPE_ACCESS);
904                                 status = PTR_ERR(args.acl_access);
905                                 if (IS_ERR(args.acl_access)) {
906                                         args.acl_access = NULL;
907                                         goto cleanup;
908                                 }
909                                 args.acl_default = acl;
910                                 break;
911
912                         default:
913                                 status = -EINVAL;
914                                 goto cleanup;
915                 }
916         } else {
917                 status = -EINVAL;
918                 if (type != ACL_TYPE_ACCESS)
919                         goto cleanup;
920                 args.mask = NFS3_ACL;
921                 args.acl_access = acl;
922         }
923         if (args.acl_access == NULL) {
924                 args.acl_access = posix_acl_from_mode(inode->i_mode,
925                                                       GFP_KERNEL);
926                 status = PTR_ERR(args.acl_access);
927                 if (IS_ERR(args.acl_access)) {
928                         args.acl_access = NULL;
929                         goto cleanup;
930                 }
931         }
932         args.mask = NFS3_ACL | (args.acl_default ? NFS3_DFACL : 0);
933
934         dprintk("NFS call setacl\n");
935         status = rpc_call(server->acl_client, NFS3PROC_SETACL,
936                           &args, &fattr, 0);
937         dprintk("NFS reply setacl: %d\n", status);
938
939         /* pages may have been allocated at the xdr layer. */
940         for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
941                 __free_page(args.pages[count]);
942
943         if (status) {
944                 if (status == -ENOSYS) {
945                         dprintk("NFS_ACL SETACL RPC not supported"
946                                 "(will not retry)\n");
947                         server->flags &= ~NFSACL;
948                         status = -EOPNOTSUPP;
949                 } else if (status == -ENOTSUPP)
950                         status = -EOPNOTSUPP;
951         } else {
952                 /* Force an attribute cache update if the file mode
953                  * has changed. */
954                 if (inode->i_mode != fattr.mode)
955                         NFS_CACHEINV(inode);
956                 status = nfs_refresh_inode(inode, &fattr);
957         }
958
959 cleanup:
960         if (args.acl_access != acl)
961                 posix_acl_release(args.acl_access);
962         if (args.acl_default != acl)
963                 posix_acl_release(args.acl_default);
964         return status;
965 }
966 #endif  /* CONFIG_NFS_ACL */
967
968 extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
969
970 static void
971 nfs3_read_done(struct rpc_task *task)
972 {
973         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
974
975         if (nfs3_async_handle_jukebox(task))
976                 return;
977         /* Call back common NFS readpage processing */
978         if (task->tk_status >= 0)
979                 nfs_refresh_inode(data->inode, &data->fattr);
980         nfs_readpage_result(task);
981 }
982
983 static void
984 nfs3_proc_read_setup(struct nfs_read_data *data)
985 {
986         struct rpc_task         *task = &data->task;
987         struct inode            *inode = data->inode;
988         int                     flags;
989         struct rpc_message      msg = {
990                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READ],
991                 .rpc_argp       = &data->args,
992                 .rpc_resp       = &data->res,
993                 .rpc_cred       = data->cred,
994         };
995
996         /* N.B. Do we need to test? Never called for swapfile inode */
997         flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
998
999         /* Finalize the task. */
1000         rpc_init_task(task, NFS_CLIENT(inode), nfs3_read_done, flags);
1001         rpc_call_setup(task, &msg, 0);
1002 }
1003
1004 static void
1005 nfs3_write_done(struct rpc_task *task)
1006 {
1007         struct nfs_write_data *data;
1008
1009         if (nfs3_async_handle_jukebox(task))
1010                 return;
1011         data = (struct nfs_write_data *)task->tk_calldata;
1012         if (task->tk_status >= 0)
1013                 nfs_refresh_inode(data->inode, data->res.fattr);
1014         nfs_writeback_done(task);
1015 }
1016
1017 static void
1018 nfs3_proc_write_setup(struct nfs_write_data *data, int how)
1019 {
1020         struct rpc_task         *task = &data->task;
1021         struct inode            *inode = data->inode;
1022         int                     stable;
1023         int                     flags;
1024         struct rpc_message      msg = {
1025                 .rpc_proc       = &nfs3_procedures[NFS3PROC_WRITE],
1026                 .rpc_argp       = &data->args,
1027                 .rpc_resp       = &data->res,
1028                 .rpc_cred       = data->cred,
1029         };
1030
1031         if (how & FLUSH_STABLE) {
1032                 if (!NFS_I(inode)->ncommit)
1033                         stable = NFS_FILE_SYNC;
1034                 else
1035                         stable = NFS_DATA_SYNC;
1036         } else
1037                 stable = NFS_UNSTABLE;
1038         data->args.stable = stable;
1039
1040         /* Set the initial flags for the task.  */
1041         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1042
1043         /* Finalize the task. */
1044         rpc_init_task(task, NFS_CLIENT(inode), nfs3_write_done, flags);
1045         rpc_call_setup(task, &msg, 0);
1046 }
1047
1048 static void
1049 nfs3_commit_done(struct rpc_task *task)
1050 {
1051         struct nfs_write_data *data;
1052
1053         if (nfs3_async_handle_jukebox(task))
1054                 return;
1055         data = (struct nfs_write_data *)task->tk_calldata;
1056         if (task->tk_status >= 0)
1057                 nfs_refresh_inode(data->inode, data->res.fattr);
1058         nfs_commit_done(task);
1059 }
1060
1061 static void
1062 nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
1063 {
1064         struct rpc_task         *task = &data->task;
1065         struct inode            *inode = data->inode;
1066         int                     flags;
1067         struct rpc_message      msg = {
1068                 .rpc_proc       = &nfs3_procedures[NFS3PROC_COMMIT],
1069                 .rpc_argp       = &data->args,
1070                 .rpc_resp       = &data->res,
1071                 .rpc_cred       = data->cred,
1072         };
1073
1074         /* Set the initial flags for the task.  */
1075         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1076
1077         /* Finalize the task. */
1078         rpc_init_task(task, NFS_CLIENT(inode), nfs3_commit_done, flags);
1079         rpc_call_setup(task, &msg, 0);
1080 }
1081
1082 /*
1083  * Set up the nfspage struct with the right credentials
1084  */
1085 void
1086 nfs3_request_init(struct nfs_page *req, struct file *filp)
1087 {
1088         req->wb_cred = get_rpccred(nfs_cred(req->wb_inode, filp));
1089 }
1090
1091 static int
1092 nfs3_request_compatible(struct nfs_page *req, struct file *filp, struct page *page)
1093 {
1094         if (req->wb_file != filp)
1095                 return 0;
1096         if (req->wb_page != page)
1097                 return 0;
1098         if (req->wb_cred != nfs_file_cred(filp))
1099                 return 0;
1100         return 1;
1101 }
1102
1103 static int
1104 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
1105 {
1106         return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
1107 }
1108
1109 struct nfs_rpc_ops      nfs_v3_clientops = {
1110         .version        = 3,                    /* protocol version */
1111         .dentry_ops     = &nfs_dentry_operations,
1112         .dir_inode_ops  = &nfs_dir_inode_operations,
1113         .getroot        = nfs3_proc_get_root,
1114         .getattr        = nfs3_proc_getattr,
1115         .setattr        = nfs3_proc_setattr,
1116         .lookup         = nfs3_proc_lookup,
1117         .access         = nfs3_proc_access,
1118         .readlink       = nfs3_proc_readlink,
1119         .read           = nfs3_proc_read,
1120         .write          = nfs3_proc_write,
1121         .commit         = nfs3_proc_commit,
1122         .create         = nfs3_proc_create,
1123         .remove         = nfs3_proc_remove,
1124         .unlink_setup   = nfs3_proc_unlink_setup,
1125         .unlink_done    = nfs3_proc_unlink_done,
1126         .rename         = nfs3_proc_rename,
1127         .link           = nfs3_proc_link,
1128         .symlink        = nfs3_proc_symlink,
1129         .mkdir          = nfs3_proc_mkdir,
1130         .rmdir          = nfs3_proc_rmdir,
1131         .readdir        = nfs3_proc_readdir,
1132         .mknod          = nfs3_proc_mknod,
1133         .statfs         = nfs3_proc_statfs,
1134         .fsinfo         = nfs3_proc_fsinfo,
1135         .pathconf       = nfs3_proc_pathconf,
1136         .decode_dirent  = nfs3_decode_dirent,
1137         .read_setup     = nfs3_proc_read_setup,
1138         .write_setup    = nfs3_proc_write_setup,
1139         .commit_setup   = nfs3_proc_commit_setup,
1140         .file_open      = nfs_open,
1141         .file_release   = nfs_release,
1142         .request_init   = nfs3_request_init,
1143         .request_compatible = nfs3_request_compatible,
1144         .lock           = nfs3_proc_lock,
1145 #ifdef CONFIG_NFS_ACL
1146         .getacl         = nfs3_proc_getacl,
1147         .setacl         = nfs3_proc_setacl,
1148         .checkacls      = nfs3_proc_checkacls,
1149 #endif  /* CONFIG_NFS_ACL */
1150 };