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