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