acbdf7496d312fe5a95d7d23a485d96ac6437148
[sfrench/cifs-2.6.git] / fs / nfs / nfs3proc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/nfs/nfs3proc.c
4  *
5  *  Client-side NFSv3 procedures stubs.
6  *
7  *  Copyright (C) 1997, Olaf Kirch
8  */
9
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/slab.h>
15 #include <linux/nfs.h>
16 #include <linux/nfs3.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_page.h>
19 #include <linux/lockd/bind.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/freezer.h>
22 #include <linux/xattr.h>
23
24 #include "iostat.h"
25 #include "internal.h"
26 #include "nfs3_fs.h"
27
28 #define NFSDBG_FACILITY         NFSDBG_PROC
29
30 /* A wrapper to handle the EJUKEBOX error messages */
31 static int
32 nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
33 {
34         int res;
35         do {
36                 res = rpc_call_sync(clnt, msg, flags);
37                 if (res != -EJUKEBOX)
38                         break;
39                 freezable_schedule_timeout_killable_unsafe(NFS_JUKEBOX_RETRY_TIME);
40                 res = -ERESTARTSYS;
41         } while (!fatal_signal_pending(current));
42         return res;
43 }
44
45 #define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
46
47 static int
48 nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
49 {
50         if (task->tk_status != -EJUKEBOX)
51                 return 0;
52         if (task->tk_status == -EJUKEBOX)
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", __func__);
72         nfs_fattr_init(info->fattr);
73         status = rpc_call_sync(client, &msg, 0);
74         dprintk("%s: reply fsinfo: %d\n", __func__, status);
75         if (status == 0 && !(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", __func__, status);
80         }
81         return status;
82 }
83
84 /*
85  * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
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->nfs_client->cl_rpcclient != server->client)
95                 status = do_proc_get_root(server->nfs_client->cl_rpcclient, 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, struct nfs4_label *label,
105                 struct inode *inode)
106 {
107         struct rpc_message msg = {
108                 .rpc_proc       = &nfs3_procedures[NFS3PROC_GETATTR],
109                 .rpc_argp       = fhandle,
110                 .rpc_resp       = fattr,
111         };
112         int     status;
113         unsigned short task_flags = 0;
114
115         /* Is this is an attribute revalidation, subject to softreval? */
116         if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
117                 task_flags |= RPC_TASK_TIMEOUT;
118
119         dprintk("NFS call  getattr\n");
120         nfs_fattr_init(fattr);
121         status = rpc_call_sync(server->client, &msg, task_flags);
122         dprintk("NFS reply getattr: %d\n", status);
123         return status;
124 }
125
126 static int
127 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
128                         struct iattr *sattr)
129 {
130         struct inode *inode = d_inode(dentry);
131         struct nfs3_sattrargs   arg = {
132                 .fh             = NFS_FH(inode),
133                 .sattr          = sattr,
134         };
135         struct rpc_message msg = {
136                 .rpc_proc       = &nfs3_procedures[NFS3PROC_SETATTR],
137                 .rpc_argp       = &arg,
138                 .rpc_resp       = fattr,
139         };
140         int     status;
141
142         dprintk("NFS call  setattr\n");
143         if (sattr->ia_valid & ATTR_FILE)
144                 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
145         nfs_fattr_init(fattr);
146         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
147         if (status == 0) {
148                 nfs_setattr_update_inode(inode, sattr, fattr);
149                 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
150                         nfs_zap_acl_cache(inode);
151         }
152         dprintk("NFS reply setattr: %d\n", status);
153         return status;
154 }
155
156 static int
157 __nfs3_proc_lookup(struct inode *dir, const char *name, size_t len,
158                    struct nfs_fh *fhandle, struct nfs_fattr *fattr,
159                    unsigned short task_flags)
160 {
161         struct nfs3_diropargs   arg = {
162                 .fh             = NFS_FH(dir),
163                 .name           = name,
164                 .len            = len
165         };
166         struct nfs3_diropres    res = {
167                 .fh             = fhandle,
168                 .fattr          = fattr
169         };
170         struct rpc_message msg = {
171                 .rpc_proc       = &nfs3_procedures[NFS3PROC_LOOKUP],
172                 .rpc_argp       = &arg,
173                 .rpc_resp       = &res,
174         };
175         int                     status;
176
177         res.dir_attr = nfs_alloc_fattr();
178         if (res.dir_attr == NULL)
179                 return -ENOMEM;
180
181         nfs_fattr_init(fattr);
182         status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
183         nfs_refresh_inode(dir, res.dir_attr);
184         if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
185                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
186                 msg.rpc_argp = fhandle;
187                 msg.rpc_resp = fattr;
188                 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
189         }
190         nfs_free_fattr(res.dir_attr);
191         dprintk("NFS reply lookup: %d\n", status);
192         return status;
193 }
194
195 static int
196 nfs3_proc_lookup(struct inode *dir, struct dentry *dentry,
197                  struct nfs_fh *fhandle, struct nfs_fattr *fattr,
198                  struct nfs4_label *label)
199 {
200         unsigned short task_flags = 0;
201
202         /* Is this is an attribute revalidation, subject to softreval? */
203         if (nfs_lookup_is_soft_revalidate(dentry))
204                 task_flags |= RPC_TASK_TIMEOUT;
205
206         dprintk("NFS call  lookup %pd2\n", dentry);
207         return __nfs3_proc_lookup(dir, dentry->d_name.name,
208                                   dentry->d_name.len, fhandle, fattr,
209                                   task_flags);
210 }
211
212 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
213 {
214         struct nfs3_accessargs  arg = {
215                 .fh             = NFS_FH(inode),
216                 .access         = entry->mask,
217         };
218         struct nfs3_accessres   res;
219         struct rpc_message msg = {
220                 .rpc_proc       = &nfs3_procedures[NFS3PROC_ACCESS],
221                 .rpc_argp       = &arg,
222                 .rpc_resp       = &res,
223                 .rpc_cred       = entry->cred,
224         };
225         int status = -ENOMEM;
226
227         dprintk("NFS call  access\n");
228         res.fattr = nfs_alloc_fattr();
229         if (res.fattr == NULL)
230                 goto out;
231
232         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
233         nfs_refresh_inode(inode, res.fattr);
234         if (status == 0)
235                 nfs_access_set_mask(entry, res.access);
236         nfs_free_fattr(res.fattr);
237 out:
238         dprintk("NFS reply access: %d\n", status);
239         return status;
240 }
241
242 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
243                 unsigned int pgbase, unsigned int pglen)
244 {
245         struct nfs_fattr        *fattr;
246         struct nfs3_readlinkargs args = {
247                 .fh             = NFS_FH(inode),
248                 .pgbase         = pgbase,
249                 .pglen          = pglen,
250                 .pages          = &page
251         };
252         struct rpc_message msg = {
253                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READLINK],
254                 .rpc_argp       = &args,
255         };
256         int status = -ENOMEM;
257
258         dprintk("NFS call  readlink\n");
259         fattr = nfs_alloc_fattr();
260         if (fattr == NULL)
261                 goto out;
262         msg.rpc_resp = fattr;
263
264         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
265         nfs_refresh_inode(inode, fattr);
266         nfs_free_fattr(fattr);
267 out:
268         dprintk("NFS reply readlink: %d\n", status);
269         return status;
270 }
271
272 struct nfs3_createdata {
273         struct rpc_message msg;
274         union {
275                 struct nfs3_createargs create;
276                 struct nfs3_mkdirargs mkdir;
277                 struct nfs3_symlinkargs symlink;
278                 struct nfs3_mknodargs mknod;
279         } arg;
280         struct nfs3_diropres res;
281         struct nfs_fh fh;
282         struct nfs_fattr fattr;
283         struct nfs_fattr dir_attr;
284 };
285
286 static struct nfs3_createdata *nfs3_alloc_createdata(void)
287 {
288         struct nfs3_createdata *data;
289
290         data = kzalloc(sizeof(*data), GFP_KERNEL);
291         if (data != NULL) {
292                 data->msg.rpc_argp = &data->arg;
293                 data->msg.rpc_resp = &data->res;
294                 data->res.fh = &data->fh;
295                 data->res.fattr = &data->fattr;
296                 data->res.dir_attr = &data->dir_attr;
297                 nfs_fattr_init(data->res.fattr);
298                 nfs_fattr_init(data->res.dir_attr);
299         }
300         return data;
301 }
302
303 static struct dentry *
304 nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
305 {
306         int status;
307
308         status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
309         nfs_post_op_update_inode(dir, data->res.dir_attr);
310         if (status != 0)
311                 return ERR_PTR(status);
312
313         return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr, NULL);
314 }
315
316 static void nfs3_free_createdata(struct nfs3_createdata *data)
317 {
318         kfree(data);
319 }
320
321 /*
322  * Create a regular file.
323  */
324 static int
325 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
326                  int flags)
327 {
328         struct posix_acl *default_acl, *acl;
329         struct nfs3_createdata *data;
330         struct dentry *d_alias;
331         int status = -ENOMEM;
332
333         dprintk("NFS call  create %pd\n", dentry);
334
335         data = nfs3_alloc_createdata();
336         if (data == NULL)
337                 goto out;
338
339         data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
340         data->arg.create.fh = NFS_FH(dir);
341         data->arg.create.name = dentry->d_name.name;
342         data->arg.create.len = dentry->d_name.len;
343         data->arg.create.sattr = sattr;
344
345         data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
346         if (flags & O_EXCL) {
347                 data->arg.create.createmode  = NFS3_CREATE_EXCLUSIVE;
348                 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
349                 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
350         }
351
352         status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
353         if (status)
354                 goto out;
355
356         for (;;) {
357                 d_alias = nfs3_do_create(dir, dentry, data);
358                 status = PTR_ERR_OR_ZERO(d_alias);
359
360                 if (status != -ENOTSUPP)
361                         break;
362                 /* If the server doesn't support the exclusive creation
363                  * semantics, try again with simple 'guarded' mode. */
364                 switch (data->arg.create.createmode) {
365                         case NFS3_CREATE_EXCLUSIVE:
366                                 data->arg.create.createmode = NFS3_CREATE_GUARDED;
367                                 break;
368
369                         case NFS3_CREATE_GUARDED:
370                                 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
371                                 break;
372
373                         case NFS3_CREATE_UNCHECKED:
374                                 goto out;
375                 }
376                 nfs_fattr_init(data->res.dir_attr);
377                 nfs_fattr_init(data->res.fattr);
378         }
379
380         if (status != 0)
381                 goto out_release_acls;
382
383         if (d_alias)
384                 dentry = d_alias;
385
386         /* When we created the file with exclusive semantics, make
387          * sure we set the attributes afterwards. */
388         if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
389                 dprintk("NFS call  setattr (post-create)\n");
390
391                 if (!(sattr->ia_valid & ATTR_ATIME_SET))
392                         sattr->ia_valid |= ATTR_ATIME;
393                 if (!(sattr->ia_valid & ATTR_MTIME_SET))
394                         sattr->ia_valid |= ATTR_MTIME;
395
396                 /* Note: we could use a guarded setattr here, but I'm
397                  * not sure this buys us anything (and I'd have
398                  * to revamp the NFSv3 XDR code) */
399                 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
400                 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
401                 dprintk("NFS reply setattr (post-create): %d\n", status);
402                 if (status != 0)
403                         goto out_dput;
404         }
405
406         status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
407
408 out_dput:
409         dput(d_alias);
410 out_release_acls:
411         posix_acl_release(acl);
412         posix_acl_release(default_acl);
413 out:
414         nfs3_free_createdata(data);
415         dprintk("NFS reply create: %d\n", status);
416         return status;
417 }
418
419 static int
420 nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
421 {
422         struct nfs_removeargs arg = {
423                 .fh = NFS_FH(dir),
424                 .name = dentry->d_name,
425         };
426         struct nfs_removeres res;
427         struct rpc_message msg = {
428                 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
429                 .rpc_argp = &arg,
430                 .rpc_resp = &res,
431         };
432         int status = -ENOMEM;
433
434         dprintk("NFS call  remove %pd2\n", dentry);
435         res.dir_attr = nfs_alloc_fattr();
436         if (res.dir_attr == NULL)
437                 goto out;
438
439         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
440         nfs_post_op_update_inode(dir, res.dir_attr);
441         nfs_free_fattr(res.dir_attr);
442 out:
443         dprintk("NFS reply remove: %d\n", status);
444         return status;
445 }
446
447 static void
448 nfs3_proc_unlink_setup(struct rpc_message *msg,
449                 struct dentry *dentry,
450                 struct inode *inode)
451 {
452         msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
453 }
454
455 static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
456 {
457         rpc_call_start(task);
458 }
459
460 static int
461 nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
462 {
463         struct nfs_removeres *res;
464         if (nfs3_async_handle_jukebox(task, dir))
465                 return 0;
466         res = task->tk_msg.rpc_resp;
467         nfs_post_op_update_inode(dir, res->dir_attr);
468         return 1;
469 }
470
471 static void
472 nfs3_proc_rename_setup(struct rpc_message *msg,
473                 struct dentry *old_dentry,
474                 struct dentry *new_dentry)
475 {
476         msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
477 }
478
479 static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
480 {
481         rpc_call_start(task);
482 }
483
484 static int
485 nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
486                       struct inode *new_dir)
487 {
488         struct nfs_renameres *res;
489
490         if (nfs3_async_handle_jukebox(task, old_dir))
491                 return 0;
492         res = task->tk_msg.rpc_resp;
493
494         nfs_post_op_update_inode(old_dir, res->old_fattr);
495         nfs_post_op_update_inode(new_dir, res->new_fattr);
496         return 1;
497 }
498
499 static int
500 nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
501 {
502         struct nfs3_linkargs    arg = {
503                 .fromfh         = NFS_FH(inode),
504                 .tofh           = NFS_FH(dir),
505                 .toname         = name->name,
506                 .tolen          = name->len
507         };
508         struct nfs3_linkres     res;
509         struct rpc_message msg = {
510                 .rpc_proc       = &nfs3_procedures[NFS3PROC_LINK],
511                 .rpc_argp       = &arg,
512                 .rpc_resp       = &res,
513         };
514         int status = -ENOMEM;
515
516         dprintk("NFS call  link %s\n", name->name);
517         res.fattr = nfs_alloc_fattr();
518         res.dir_attr = nfs_alloc_fattr();
519         if (res.fattr == NULL || res.dir_attr == NULL)
520                 goto out;
521
522         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
523         nfs_post_op_update_inode(dir, res.dir_attr);
524         nfs_post_op_update_inode(inode, res.fattr);
525 out:
526         nfs_free_fattr(res.dir_attr);
527         nfs_free_fattr(res.fattr);
528         dprintk("NFS reply link: %d\n", status);
529         return status;
530 }
531
532 static int
533 nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
534                   unsigned int len, struct iattr *sattr)
535 {
536         struct nfs3_createdata *data;
537         struct dentry *d_alias;
538         int status = -ENOMEM;
539
540         if (len > NFS3_MAXPATHLEN)
541                 return -ENAMETOOLONG;
542
543         dprintk("NFS call  symlink %pd\n", dentry);
544
545         data = nfs3_alloc_createdata();
546         if (data == NULL)
547                 goto out;
548         data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
549         data->arg.symlink.fromfh = NFS_FH(dir);
550         data->arg.symlink.fromname = dentry->d_name.name;
551         data->arg.symlink.fromlen = dentry->d_name.len;
552         data->arg.symlink.pages = &page;
553         data->arg.symlink.pathlen = len;
554         data->arg.symlink.sattr = sattr;
555
556         d_alias = nfs3_do_create(dir, dentry, data);
557         status = PTR_ERR_OR_ZERO(d_alias);
558
559         if (status == 0)
560                 dput(d_alias);
561
562         nfs3_free_createdata(data);
563 out:
564         dprintk("NFS reply symlink: %d\n", status);
565         return status;
566 }
567
568 static int
569 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
570 {
571         struct posix_acl *default_acl, *acl;
572         struct nfs3_createdata *data;
573         struct dentry *d_alias;
574         int status = -ENOMEM;
575
576         dprintk("NFS call  mkdir %pd\n", dentry);
577
578         data = nfs3_alloc_createdata();
579         if (data == NULL)
580                 goto out;
581
582         status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
583         if (status)
584                 goto out;
585
586         data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
587         data->arg.mkdir.fh = NFS_FH(dir);
588         data->arg.mkdir.name = dentry->d_name.name;
589         data->arg.mkdir.len = dentry->d_name.len;
590         data->arg.mkdir.sattr = sattr;
591
592         d_alias = nfs3_do_create(dir, dentry, data);
593         status = PTR_ERR_OR_ZERO(d_alias);
594
595         if (status != 0)
596                 goto out_release_acls;
597
598         if (d_alias)
599                 dentry = d_alias;
600
601         status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
602
603         dput(d_alias);
604 out_release_acls:
605         posix_acl_release(acl);
606         posix_acl_release(default_acl);
607 out:
608         nfs3_free_createdata(data);
609         dprintk("NFS reply mkdir: %d\n", status);
610         return status;
611 }
612
613 static int
614 nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
615 {
616         struct nfs_fattr        *dir_attr;
617         struct nfs3_diropargs   arg = {
618                 .fh             = NFS_FH(dir),
619                 .name           = name->name,
620                 .len            = name->len
621         };
622         struct rpc_message msg = {
623                 .rpc_proc       = &nfs3_procedures[NFS3PROC_RMDIR],
624                 .rpc_argp       = &arg,
625         };
626         int status = -ENOMEM;
627
628         dprintk("NFS call  rmdir %s\n", name->name);
629         dir_attr = nfs_alloc_fattr();
630         if (dir_attr == NULL)
631                 goto out;
632
633         msg.rpc_resp = dir_attr;
634         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
635         nfs_post_op_update_inode(dir, dir_attr);
636         nfs_free_fattr(dir_attr);
637 out:
638         dprintk("NFS reply rmdir: %d\n", status);
639         return status;
640 }
641
642 /*
643  * The READDIR implementation is somewhat hackish - we pass the user buffer
644  * to the encode function, which installs it in the receive iovec.
645  * The decode function itself doesn't perform any decoding, it just makes
646  * sure the reply is syntactically correct.
647  *
648  * Also note that this implementation handles both plain readdir and
649  * readdirplus.
650  */
651 static int
652 nfs3_proc_readdir(struct dentry *dentry, const struct cred *cred,
653                   u64 cookie, struct page **pages, unsigned int count, bool plus)
654 {
655         struct inode            *dir = d_inode(dentry);
656         __be32                  *verf = NFS_I(dir)->cookieverf;
657         struct nfs3_readdirargs arg = {
658                 .fh             = NFS_FH(dir),
659                 .cookie         = cookie,
660                 .verf           = {verf[0], verf[1]},
661                 .plus           = plus,
662                 .count          = count,
663                 .pages          = pages
664         };
665         struct nfs3_readdirres  res = {
666                 .verf           = verf,
667                 .plus           = plus
668         };
669         struct rpc_message      msg = {
670                 .rpc_proc       = &nfs3_procedures[NFS3PROC_READDIR],
671                 .rpc_argp       = &arg,
672                 .rpc_resp       = &res,
673                 .rpc_cred       = cred,
674         };
675         int status = -ENOMEM;
676
677         if (plus)
678                 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
679
680         dprintk("NFS call  readdir%s %d\n",
681                         plus? "plus" : "", (unsigned int) cookie);
682
683         res.dir_attr = nfs_alloc_fattr();
684         if (res.dir_attr == NULL)
685                 goto out;
686
687         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
688
689         nfs_invalidate_atime(dir);
690         nfs_refresh_inode(dir, res.dir_attr);
691
692         nfs_free_fattr(res.dir_attr);
693 out:
694         dprintk("NFS reply readdir%s: %d\n",
695                         plus? "plus" : "", status);
696         return status;
697 }
698
699 static int
700 nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
701                 dev_t rdev)
702 {
703         struct posix_acl *default_acl, *acl;
704         struct nfs3_createdata *data;
705         struct dentry *d_alias;
706         int status = -ENOMEM;
707
708         dprintk("NFS call  mknod %pd %u:%u\n", dentry,
709                         MAJOR(rdev), MINOR(rdev));
710
711         data = nfs3_alloc_createdata();
712         if (data == NULL)
713                 goto out;
714
715         status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
716         if (status)
717                 goto out;
718
719         data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
720         data->arg.mknod.fh = NFS_FH(dir);
721         data->arg.mknod.name = dentry->d_name.name;
722         data->arg.mknod.len = dentry->d_name.len;
723         data->arg.mknod.sattr = sattr;
724         data->arg.mknod.rdev = rdev;
725
726         switch (sattr->ia_mode & S_IFMT) {
727         case S_IFBLK:
728                 data->arg.mknod.type = NF3BLK;
729                 break;
730         case S_IFCHR:
731                 data->arg.mknod.type = NF3CHR;
732                 break;
733         case S_IFIFO:
734                 data->arg.mknod.type = NF3FIFO;
735                 break;
736         case S_IFSOCK:
737                 data->arg.mknod.type = NF3SOCK;
738                 break;
739         default:
740                 status = -EINVAL;
741                 goto out;
742         }
743
744         d_alias = nfs3_do_create(dir, dentry, data);
745         status = PTR_ERR_OR_ZERO(d_alias);
746         if (status != 0)
747                 goto out_release_acls;
748
749         if (d_alias)
750                 dentry = d_alias;
751
752         status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
753
754         dput(d_alias);
755 out_release_acls:
756         posix_acl_release(acl);
757         posix_acl_release(default_acl);
758 out:
759         nfs3_free_createdata(data);
760         dprintk("NFS reply mknod: %d\n", status);
761         return status;
762 }
763
764 static int
765 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
766                  struct nfs_fsstat *stat)
767 {
768         struct rpc_message msg = {
769                 .rpc_proc       = &nfs3_procedures[NFS3PROC_FSSTAT],
770                 .rpc_argp       = fhandle,
771                 .rpc_resp       = stat,
772         };
773         int     status;
774
775         dprintk("NFS call  fsstat\n");
776         nfs_fattr_init(stat->fattr);
777         status = rpc_call_sync(server->client, &msg, 0);
778         dprintk("NFS reply fsstat: %d\n", status);
779         return status;
780 }
781
782 static int
783 do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
784                  struct nfs_fsinfo *info)
785 {
786         struct rpc_message msg = {
787                 .rpc_proc       = &nfs3_procedures[NFS3PROC_FSINFO],
788                 .rpc_argp       = fhandle,
789                 .rpc_resp       = info,
790         };
791         int     status;
792
793         dprintk("NFS call  fsinfo\n");
794         nfs_fattr_init(info->fattr);
795         status = rpc_call_sync(client, &msg, 0);
796         dprintk("NFS reply fsinfo: %d\n", status);
797         return status;
798 }
799
800 /*
801  * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
802  * nfs_create_server
803  */
804 static int
805 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
806                    struct nfs_fsinfo *info)
807 {
808         int     status;
809
810         status = do_proc_fsinfo(server->client, fhandle, info);
811         if (status && server->nfs_client->cl_rpcclient != server->client)
812                 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
813         return status;
814 }
815
816 static int
817 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
818                    struct nfs_pathconf *info)
819 {
820         struct rpc_message msg = {
821                 .rpc_proc       = &nfs3_procedures[NFS3PROC_PATHCONF],
822                 .rpc_argp       = fhandle,
823                 .rpc_resp       = info,
824         };
825         int     status;
826
827         dprintk("NFS call  pathconf\n");
828         nfs_fattr_init(info->fattr);
829         status = rpc_call_sync(server->client, &msg, 0);
830         dprintk("NFS reply pathconf: %d\n", status);
831         return status;
832 }
833
834 static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
835 {
836         struct inode *inode = hdr->inode;
837         struct nfs_server *server = NFS_SERVER(inode);
838
839         if (hdr->pgio_done_cb != NULL)
840                 return hdr->pgio_done_cb(task, hdr);
841
842         if (nfs3_async_handle_jukebox(task, inode))
843                 return -EAGAIN;
844
845         if (task->tk_status >= 0 && !server->read_hdrsize)
846                 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
847
848         nfs_invalidate_atime(inode);
849         nfs_refresh_inode(inode, &hdr->fattr);
850         return 0;
851 }
852
853 static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
854                                  struct rpc_message *msg)
855 {
856         msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
857         hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
858 }
859
860 static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
861                                       struct nfs_pgio_header *hdr)
862 {
863         rpc_call_start(task);
864         return 0;
865 }
866
867 static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
868 {
869         struct inode *inode = hdr->inode;
870
871         if (hdr->pgio_done_cb != NULL)
872                 return hdr->pgio_done_cb(task, hdr);
873
874         if (nfs3_async_handle_jukebox(task, inode))
875                 return -EAGAIN;
876         if (task->tk_status >= 0)
877                 nfs_writeback_update_inode(hdr);
878         return 0;
879 }
880
881 static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
882                                   struct rpc_message *msg,
883                                   struct rpc_clnt **clnt)
884 {
885         msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
886 }
887
888 static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
889 {
890         rpc_call_start(task);
891 }
892
893 static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
894 {
895         if (data->commit_done_cb != NULL)
896                 return data->commit_done_cb(task, data);
897
898         if (nfs3_async_handle_jukebox(task, data->inode))
899                 return -EAGAIN;
900         nfs_refresh_inode(data->inode, data->res.fattr);
901         return 0;
902 }
903
904 static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
905                                    struct rpc_clnt **clnt)
906 {
907         msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
908 }
909
910 static void nfs3_nlm_alloc_call(void *data)
911 {
912         struct nfs_lock_context *l_ctx = data;
913         if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
914                 get_nfs_open_context(l_ctx->open_context);
915                 nfs_get_lock_context(l_ctx->open_context);
916         }
917 }
918
919 static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
920 {
921         struct nfs_lock_context *l_ctx = data;
922         if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
923                 return nfs_async_iocounter_wait(task, l_ctx);
924         return false;
925
926 }
927
928 static void nfs3_nlm_release_call(void *data)
929 {
930         struct nfs_lock_context *l_ctx = data;
931         struct nfs_open_context *ctx;
932         if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
933                 ctx = l_ctx->open_context;
934                 nfs_put_lock_context(l_ctx);
935                 put_nfs_open_context(ctx);
936         }
937 }
938
939 static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
940         .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
941         .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
942         .nlmclnt_release_call = nfs3_nlm_release_call,
943 };
944
945 static int
946 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
947 {
948         struct inode *inode = file_inode(filp);
949         struct nfs_lock_context *l_ctx = NULL;
950         struct nfs_open_context *ctx = nfs_file_open_context(filp);
951         int status;
952
953         if (fl->fl_flags & FL_CLOSE) {
954                 l_ctx = nfs_get_lock_context(ctx);
955                 if (IS_ERR(l_ctx))
956                         l_ctx = NULL;
957                 else
958                         set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
959         }
960
961         status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
962
963         if (l_ctx)
964                 nfs_put_lock_context(l_ctx);
965
966         return status;
967 }
968
969 static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
970 {
971         return 0;
972 }
973
974 static const struct inode_operations nfs3_dir_inode_operations = {
975         .create         = nfs_create,
976         .lookup         = nfs_lookup,
977         .link           = nfs_link,
978         .unlink         = nfs_unlink,
979         .symlink        = nfs_symlink,
980         .mkdir          = nfs_mkdir,
981         .rmdir          = nfs_rmdir,
982         .mknod          = nfs_mknod,
983         .rename         = nfs_rename,
984         .permission     = nfs_permission,
985         .getattr        = nfs_getattr,
986         .setattr        = nfs_setattr,
987 #ifdef CONFIG_NFS_V3_ACL
988         .listxattr      = nfs3_listxattr,
989         .get_acl        = nfs3_get_acl,
990         .set_acl        = nfs3_set_acl,
991 #endif
992 };
993
994 static const struct inode_operations nfs3_file_inode_operations = {
995         .permission     = nfs_permission,
996         .getattr        = nfs_getattr,
997         .setattr        = nfs_setattr,
998 #ifdef CONFIG_NFS_V3_ACL
999         .listxattr      = nfs3_listxattr,
1000         .get_acl        = nfs3_get_acl,
1001         .set_acl        = nfs3_set_acl,
1002 #endif
1003 };
1004
1005 const struct nfs_rpc_ops nfs_v3_clientops = {
1006         .version        = 3,                    /* protocol version */
1007         .dentry_ops     = &nfs_dentry_operations,
1008         .dir_inode_ops  = &nfs3_dir_inode_operations,
1009         .file_inode_ops = &nfs3_file_inode_operations,
1010         .file_ops       = &nfs_file_operations,
1011         .nlmclnt_ops    = &nlmclnt_fl_close_lock_ops,
1012         .getroot        = nfs3_proc_get_root,
1013         .submount       = nfs_submount,
1014         .try_get_tree   = nfs_try_get_tree,
1015         .getattr        = nfs3_proc_getattr,
1016         .setattr        = nfs3_proc_setattr,
1017         .lookup         = nfs3_proc_lookup,
1018         .access         = nfs3_proc_access,
1019         .readlink       = nfs3_proc_readlink,
1020         .create         = nfs3_proc_create,
1021         .remove         = nfs3_proc_remove,
1022         .unlink_setup   = nfs3_proc_unlink_setup,
1023         .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
1024         .unlink_done    = nfs3_proc_unlink_done,
1025         .rename_setup   = nfs3_proc_rename_setup,
1026         .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
1027         .rename_done    = nfs3_proc_rename_done,
1028         .link           = nfs3_proc_link,
1029         .symlink        = nfs3_proc_symlink,
1030         .mkdir          = nfs3_proc_mkdir,
1031         .rmdir          = nfs3_proc_rmdir,
1032         .readdir        = nfs3_proc_readdir,
1033         .mknod          = nfs3_proc_mknod,
1034         .statfs         = nfs3_proc_statfs,
1035         .fsinfo         = nfs3_proc_fsinfo,
1036         .pathconf       = nfs3_proc_pathconf,
1037         .decode_dirent  = nfs3_decode_dirent,
1038         .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
1039         .read_setup     = nfs3_proc_read_setup,
1040         .read_done      = nfs3_read_done,
1041         .write_setup    = nfs3_proc_write_setup,
1042         .write_done     = nfs3_write_done,
1043         .commit_setup   = nfs3_proc_commit_setup,
1044         .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
1045         .commit_done    = nfs3_commit_done,
1046         .lock           = nfs3_proc_lock,
1047         .clear_acl_cache = forget_all_cached_acls,
1048         .close_context  = nfs_close_context,
1049         .have_delegation = nfs3_have_delegation,
1050         .alloc_client   = nfs_alloc_client,
1051         .init_client    = nfs_init_client,
1052         .free_client    = nfs_free_client,
1053         .create_server  = nfs3_create_server,
1054         .clone_server   = nfs3_clone_server,
1055 };