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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/fs/nfs/nfs3proc.c