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