1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/fs.h>
18#include <linux/file.h>
19#include <linux/splice.h>
20#include <linux/falloc.h>
21#include <linux/fcntl.h>
22#include <linux/namei.h>
23#include <linux/delay.h>
24#include <linux/fsnotify.h>
25#include <linux/posix_acl_xattr.h>
26#include <linux/xattr.h>
27#include <linux/jhash.h>
28#include <linux/ima.h>
29#include <linux/slab.h>
30#include <linux/uaccess.h>
31#include <linux/exportfs.h>
32#include <linux/writeback.h>
33#include <linux/security.h>
34
35#ifdef CONFIG_NFSD_V3
36#include "xdr3.h"
37#endif
38
39#ifdef CONFIG_NFSD_V4
40#include "../internal.h"
41#include "acl.h"
42#include "idmap.h"
43#endif
44
45#include "nfsd.h"
46#include "vfs.h"
47#include "filecache.h"
48#include "trace.h"
49
50#define NFSDDBG_FACILITY NFSDDBG_FILEOP
51
52
53
54
55
56
57
58int
59nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
60 struct svc_export **expp)
61{
62 struct svc_export *exp = *expp, *exp2 = NULL;
63 struct dentry *dentry = *dpp;
64 struct path path = {.mnt = mntget(exp->ex_path.mnt),
65 .dentry = dget(dentry)};
66 int err = 0;
67
68 err = follow_down(&path);
69 if (err < 0)
70 goto out;
71 if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
72 nfsd_mountpoint(dentry, exp) == 2) {
73
74 path_put(&path);
75 goto out;
76 }
77
78 exp2 = rqst_exp_get_by_name(rqstp, &path);
79 if (IS_ERR(exp2)) {
80 err = PTR_ERR(exp2);
81
82
83
84
85
86
87
88 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
89 err = 0;
90 path_put(&path);
91 goto out;
92 }
93 if (nfsd_v4client(rqstp) ||
94 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
95
96
97
98
99
100
101
102 *dpp = path.dentry;
103 path.dentry = dentry;
104 *expp = exp2;
105 exp2 = exp;
106 }
107 path_put(&path);
108 exp_put(exp2);
109out:
110 return err;
111}
112
113static void follow_to_parent(struct path *path)
114{
115 struct dentry *dp;
116
117 while (path->dentry == path->mnt->mnt_root && follow_up(path))
118 ;
119 dp = dget_parent(path->dentry);
120 dput(path->dentry);
121 path->dentry = dp;
122}
123
124static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
125{
126 struct svc_export *exp2;
127 struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
128 .dentry = dget(dparent)};
129
130 follow_to_parent(&path);
131
132 exp2 = rqst_exp_parent(rqstp, &path);
133 if (PTR_ERR(exp2) == -ENOENT) {
134 *dentryp = dget(dparent);
135 } else if (IS_ERR(exp2)) {
136 path_put(&path);
137 return PTR_ERR(exp2);
138 } else {
139 *dentryp = dget(path.dentry);
140 exp_put(*exp);
141 *exp = exp2;
142 }
143 path_put(&path);
144 return 0;
145}
146
147
148
149
150
151
152
153
154
155int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
156{
157 if (!d_inode(dentry))
158 return 0;
159 if (exp->ex_flags & NFSEXP_V4ROOT)
160 return 1;
161 if (nfsd4_is_junction(dentry))
162 return 1;
163 if (d_mountpoint(dentry))
164
165
166
167
168 return 2;
169 return 0;
170}
171
172__be32
173nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
174 const char *name, unsigned int len,
175 struct svc_export **exp_ret, struct dentry **dentry_ret)
176{
177 struct svc_export *exp;
178 struct dentry *dparent;
179 struct dentry *dentry;
180 int host_err;
181
182 dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
183
184 dparent = fhp->fh_dentry;
185 exp = exp_get(fhp->fh_export);
186
187
188 if (isdotent(name, len)) {
189 if (len==1)
190 dentry = dget(dparent);
191 else if (dparent != exp->ex_path.dentry)
192 dentry = dget_parent(dparent);
193 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
194 dentry = dget(dparent);
195 else {
196
197 host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
198 if (host_err)
199 goto out_nfserr;
200 }
201 } else {
202
203
204
205
206
207 fh_lock_nested(fhp, I_MUTEX_PARENT);
208 dentry = lookup_one_len(name, dparent, len);
209 host_err = PTR_ERR(dentry);
210 if (IS_ERR(dentry))
211 goto out_nfserr;
212 if (nfsd_mountpoint(dentry, exp)) {
213
214
215
216
217
218
219
220
221 fh_unlock(fhp);
222 if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
223 dput(dentry);
224 goto out_nfserr;
225 }
226 }
227 }
228 *dentry_ret = dentry;
229 *exp_ret = exp;
230 return 0;
231
232out_nfserr:
233 exp_put(exp);
234 return nfserrno(host_err);
235}
236
237
238
239
240
241
242
243
244
245
246
247
248__be32
249nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
250 unsigned int len, struct svc_fh *resfh)
251{
252 struct svc_export *exp;
253 struct dentry *dentry;
254 __be32 err;
255
256 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
257 if (err)
258 return err;
259 err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
260 if (err)
261 return err;
262 err = check_nfsd_access(exp, rqstp);
263 if (err)
264 goto out;
265
266
267
268
269 err = fh_compose(resfh, exp, dentry, fhp);
270 if (!err && d_really_is_negative(dentry))
271 err = nfserr_noent;
272out:
273 dput(dentry);
274 exp_put(exp);
275 return err;
276}
277
278
279
280
281static int
282commit_inode_metadata(struct inode *inode)
283{
284 const struct export_operations *export_ops = inode->i_sb->s_export_op;
285
286 if (export_ops->commit_metadata)
287 return export_ops->commit_metadata(inode);
288 return sync_inode_metadata(inode, 1);
289}
290
291static int
292commit_metadata(struct svc_fh *fhp)
293{
294 struct inode *inode = d_inode(fhp->fh_dentry);
295
296 if (!EX_ISSYNC(fhp->fh_export))
297 return 0;
298 return commit_inode_metadata(inode);
299}
300
301
302
303
304
305static void
306nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
307{
308
309 if (iap->ia_valid & ATTR_MODE) {
310 iap->ia_mode &= S_IALLUGO;
311 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
312 }
313
314
315 if (!S_ISDIR(inode->i_mode) &&
316 ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
317 iap->ia_valid |= ATTR_KILL_PRIV;
318 if (iap->ia_valid & ATTR_MODE) {
319
320 iap->ia_mode &= ~S_ISUID;
321 if (iap->ia_mode & S_IXGRP)
322 iap->ia_mode &= ~S_ISGID;
323 } else {
324
325 iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
326 }
327 }
328}
329
330static __be32
331nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
332 struct iattr *iap)
333{
334 struct inode *inode = d_inode(fhp->fh_dentry);
335
336 if (iap->ia_size < inode->i_size) {
337 __be32 err;
338
339 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
340 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
341 if (err)
342 return err;
343 }
344 return nfserrno(get_write_access(inode));
345}
346
347
348
349
350__be32
351nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
352 int check_guard, time64_t guardtime)
353{
354 struct dentry *dentry;
355 struct inode *inode;
356 int accmode = NFSD_MAY_SATTR;
357 umode_t ftype = 0;
358 __be32 err;
359 int host_err;
360 bool get_write_count;
361 bool size_change = (iap->ia_valid & ATTR_SIZE);
362
363 if (iap->ia_valid & ATTR_SIZE) {
364 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
365 ftype = S_IFREG;
366 }
367
368
369
370
371
372
373
374
375 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
376 accmode |= NFSD_MAY_OWNER_OVERRIDE;
377 if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
378 accmode |= NFSD_MAY_WRITE;
379 }
380
381
382 get_write_count = !fhp->fh_dentry;
383
384
385 err = fh_verify(rqstp, fhp, ftype, accmode);
386 if (err)
387 return err;
388 if (get_write_count) {
389 host_err = fh_want_write(fhp);
390 if (host_err)
391 goto out;
392 }
393
394 dentry = fhp->fh_dentry;
395 inode = d_inode(dentry);
396
397
398 if (S_ISLNK(inode->i_mode))
399 iap->ia_valid &= ~ATTR_MODE;
400
401 if (!iap->ia_valid)
402 return 0;
403
404 nfsd_sanitize_attrs(inode, iap);
405
406 if (check_guard && guardtime != inode->i_ctime.tv_sec)
407 return nfserr_notsync;
408
409
410
411
412
413
414
415
416 if (size_change) {
417 err = nfsd_get_write_access(rqstp, fhp, iap);
418 if (err)
419 return err;
420 }
421
422 fh_lock(fhp);
423 if (size_change) {
424
425
426
427
428
429
430
431 struct iattr size_attr = {
432 .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
433 .ia_size = iap->ia_size,
434 };
435
436 host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
437 if (host_err)
438 goto out_unlock;
439 iap->ia_valid &= ~ATTR_SIZE;
440
441
442
443
444
445
446 if ((iap->ia_valid & ~ATTR_MTIME) == 0)
447 goto out_unlock;
448 }
449
450 iap->ia_valid |= ATTR_CTIME;
451 host_err = notify_change(&init_user_ns, dentry, iap, NULL);
452
453out_unlock:
454 fh_unlock(fhp);
455 if (size_change)
456 put_write_access(inode);
457out:
458 if (!host_err)
459 host_err = commit_metadata(fhp);
460 return nfserrno(host_err);
461}
462
463#if defined(CONFIG_NFSD_V4)
464
465
466
467#define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
468
469
470
471
472
473
474
475
476
477int nfsd4_is_junction(struct dentry *dentry)
478{
479 struct inode *inode = d_inode(dentry);
480
481 if (inode == NULL)
482 return 0;
483 if (inode->i_mode & S_IXUGO)
484 return 0;
485 if (!(inode->i_mode & S_ISVTX))
486 return 0;
487 if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
488 NULL, 0) <= 0)
489 return 0;
490 return 1;
491}
492#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
493__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
494 struct xdr_netobj *label)
495{
496 __be32 error;
497 int host_error;
498 struct dentry *dentry;
499
500 error = fh_verify(rqstp, fhp, 0 , NFSD_MAY_SATTR);
501 if (error)
502 return error;
503
504 dentry = fhp->fh_dentry;
505
506 inode_lock(d_inode(dentry));
507 host_error = security_inode_setsecctx(dentry, label->data, label->len);
508 inode_unlock(d_inode(dentry));
509 return nfserrno(host_error);
510}
511#else
512__be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
513 struct xdr_netobj *label)
514{
515 return nfserr_notsupp;
516}
517#endif
518
519__be32 nfsd4_clone_file_range(struct nfsd_file *nf_src, u64 src_pos,
520 struct nfsd_file *nf_dst, u64 dst_pos, u64 count, bool sync)
521{
522 struct file *src = nf_src->nf_file;
523 struct file *dst = nf_dst->nf_file;
524 loff_t cloned;
525 __be32 ret = 0;
526
527 down_write(&nf_dst->nf_rwsem);
528 cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
529 if (cloned < 0) {
530 ret = nfserrno(cloned);
531 goto out_err;
532 }
533 if (count && cloned != count) {
534 ret = nfserrno(-EINVAL);
535 goto out_err;
536 }
537 if (sync) {
538 loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
539 int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
540
541 if (!status)
542 status = commit_inode_metadata(file_inode(src));
543 if (status < 0) {
544 nfsd_reset_boot_verifier(net_generic(nf_dst->nf_net,
545 nfsd_net_id));
546 ret = nfserrno(status);
547 }
548 }
549out_err:
550 up_write(&nf_dst->nf_rwsem);
551 return ret;
552}
553
554ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
555 u64 dst_pos, u64 count)
556{
557
558
559
560
561
562
563
564
565
566 count = min_t(u64, count, 1 << 22);
567 return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
568}
569
570__be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
571 struct file *file, loff_t offset, loff_t len,
572 int flags)
573{
574 int error;
575
576 if (!S_ISREG(file_inode(file)->i_mode))
577 return nfserr_inval;
578
579 error = vfs_fallocate(file, flags, offset, len);
580 if (!error)
581 error = commit_metadata(fhp);
582
583 return nfserrno(error);
584}
585#endif
586
587#ifdef CONFIG_NFSD_V3
588
589
590
591struct accessmap {
592 u32 access;
593 int how;
594};
595static struct accessmap nfs3_regaccess[] = {
596 { NFS3_ACCESS_READ, NFSD_MAY_READ },
597 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
598 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
599 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
600
601#ifdef CONFIG_NFSD_V4
602 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
603 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
604 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
605#endif
606
607 { 0, 0 }
608};
609
610static struct accessmap nfs3_diraccess[] = {
611 { NFS3_ACCESS_READ, NFSD_MAY_READ },
612 { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
613 { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
614 { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
615 { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
616
617#ifdef CONFIG_NFSD_V4
618 { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
619 { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
620 { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
621#endif
622
623 { 0, 0 }
624};
625
626static struct accessmap nfs3_anyaccess[] = {
627
628
629
630
631
632
633
634 { NFS3_ACCESS_READ, NFSD_MAY_READ },
635 { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
636 { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
637 { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
638
639 { 0, 0 }
640};
641
642__be32
643nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
644{
645 struct accessmap *map;
646 struct svc_export *export;
647 struct dentry *dentry;
648 u32 query, result = 0, sresult = 0;
649 __be32 error;
650
651 error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
652 if (error)
653 goto out;
654
655 export = fhp->fh_export;
656 dentry = fhp->fh_dentry;
657
658 if (d_is_reg(dentry))
659 map = nfs3_regaccess;
660 else if (d_is_dir(dentry))
661 map = nfs3_diraccess;
662 else
663 map = nfs3_anyaccess;
664
665
666 query = *access;
667 for (; map->access; map++) {
668 if (map->access & query) {
669 __be32 err2;
670
671 sresult |= map->access;
672
673 err2 = nfsd_permission(rqstp, export, dentry, map->how);
674 switch (err2) {
675 case nfs_ok:
676 result |= map->access;
677 break;
678
679
680
681 case nfserr_rofs:
682 case nfserr_acces:
683 case nfserr_perm:
684
685 break;
686 default:
687 error = err2;
688 goto out;
689 }
690 }
691 }
692 *access = result;
693 if (supported)
694 *supported = sresult;
695
696 out:
697 return error;
698}
699#endif
700
701int nfsd_open_break_lease(struct inode *inode, int access)
702{
703 unsigned int mode;
704
705 if (access & NFSD_MAY_NOT_BREAK_LEASE)
706 return 0;
707 mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
708 return break_lease(inode, mode | O_NONBLOCK);
709}
710
711
712
713
714
715
716
717static __be32
718__nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
719 int may_flags, struct file **filp)
720{
721 struct path path;
722 struct inode *inode;
723 struct file *file;
724 int flags = O_RDONLY|O_LARGEFILE;
725 __be32 err;
726 int host_err = 0;
727
728 path.mnt = fhp->fh_export->ex_path.mnt;
729 path.dentry = fhp->fh_dentry;
730 inode = d_inode(path.dentry);
731
732
733
734
735 err = nfserr_perm;
736 if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
737 goto out;
738
739 if (!inode->i_fop)
740 goto out;
741
742 host_err = nfsd_open_break_lease(inode, may_flags);
743 if (host_err)
744 goto out_nfserr;
745
746 if (may_flags & NFSD_MAY_WRITE) {
747 if (may_flags & NFSD_MAY_READ)
748 flags = O_RDWR|O_LARGEFILE;
749 else
750 flags = O_WRONLY|O_LARGEFILE;
751 }
752
753 file = dentry_open(&path, flags, current_cred());
754 if (IS_ERR(file)) {
755 host_err = PTR_ERR(file);
756 goto out_nfserr;
757 }
758
759 host_err = ima_file_check(file, may_flags);
760 if (host_err) {
761 fput(file);
762 goto out_nfserr;
763 }
764
765 if (may_flags & NFSD_MAY_64BIT_COOKIE)
766 file->f_mode |= FMODE_64BITHASH;
767 else
768 file->f_mode |= FMODE_32BITHASH;
769
770 *filp = file;
771out_nfserr:
772 err = nfserrno(host_err);
773out:
774 return err;
775}
776
777__be32
778nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
779 int may_flags, struct file **filp)
780{
781 __be32 err;
782
783 validate_process_creds();
784
785
786
787
788
789
790
791
792
793
794
795 if (type == S_IFREG)
796 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
797 err = fh_verify(rqstp, fhp, type, may_flags);
798 if (!err)
799 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
800 validate_process_creds();
801 return err;
802}
803
804__be32
805nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
806 int may_flags, struct file **filp)
807{
808 __be32 err;
809
810 validate_process_creds();
811 err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
812 validate_process_creds();
813 return err;
814}
815
816
817
818
819
820
821static int
822nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
823 struct splice_desc *sd)
824{
825 struct svc_rqst *rqstp = sd->u.data;
826 struct page **pp = rqstp->rq_next_page;
827 struct page *page = buf->page;
828
829 if (rqstp->rq_res.page_len == 0) {
830 svc_rqst_replace_page(rqstp, page);
831 rqstp->rq_res.page_base = buf->offset;
832 } else if (page != pp[-1]) {
833 svc_rqst_replace_page(rqstp, page);
834 }
835 rqstp->rq_res.page_len += sd->len;
836
837 return sd->len;
838}
839
840static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
841 struct splice_desc *sd)
842{
843 return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
844}
845
846static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
847 size_t expected)
848{
849 if (expected != 0 && len == 0)
850 return 1;
851 if (offset+len >= i_size_read(file_inode(file)))
852 return 1;
853 return 0;
854}
855
856static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
857 struct file *file, loff_t offset,
858 unsigned long *count, u32 *eof, ssize_t host_err)
859{
860 if (host_err >= 0) {
861 nfsd_stats_io_read_add(fhp->fh_export, host_err);
862 *eof = nfsd_eof_on_read(file, offset, host_err, *count);
863 *count = host_err;
864 fsnotify_access(file);
865 trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
866 return 0;
867 } else {
868 trace_nfsd_read_err(rqstp, fhp, offset, host_err);
869 return nfserrno(host_err);
870 }
871}
872
873__be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
874 struct file *file, loff_t offset, unsigned long *count,
875 u32 *eof)
876{
877 struct splice_desc sd = {
878 .len = 0,
879 .total_len = *count,
880 .pos = offset,
881 .u.data = rqstp,
882 };
883 ssize_t host_err;
884
885 trace_nfsd_read_splice(rqstp, fhp, offset, *count);
886 rqstp->rq_next_page = rqstp->rq_respages + 1;
887 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
888 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
889}
890
891__be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
892 struct file *file, loff_t offset,
893 struct kvec *vec, int vlen, unsigned long *count,
894 u32 *eof)
895{
896 struct iov_iter iter;
897 loff_t ppos = offset;
898 ssize_t host_err;
899
900 trace_nfsd_read_vector(rqstp, fhp, offset, *count);
901 iov_iter_kvec(&iter, READ, vec, vlen, *count);
902 host_err = vfs_iter_read(file, &iter, &ppos, 0);
903 return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
904}
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920static int wait_for_concurrent_writes(struct file *file)
921{
922 struct inode *inode = file_inode(file);
923 static ino_t last_ino;
924 static dev_t last_dev;
925 int err = 0;
926
927 if (atomic_read(&inode->i_writecount) > 1
928 || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
929 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
930 msleep(10);
931 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
932 }
933
934 if (inode->i_state & I_DIRTY) {
935 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
936 err = vfs_fsync(file, 0);
937 }
938 last_ino = inode->i_ino;
939 last_dev = inode->i_sb->s_dev;
940 return err;
941}
942
943__be32
944nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
945 loff_t offset, struct kvec *vec, int vlen,
946 unsigned long *cnt, int stable,
947 __be32 *verf)
948{
949 struct file *file = nf->nf_file;
950 struct super_block *sb = file_inode(file)->i_sb;
951 struct svc_export *exp;
952 struct iov_iter iter;
953 __be32 nfserr;
954 int host_err;
955 int use_wgather;
956 loff_t pos = offset;
957 unsigned long exp_op_flags = 0;
958 unsigned int pflags = current->flags;
959 rwf_t flags = 0;
960 bool restore_flags = false;
961
962 trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
963
964 if (sb->s_export_op)
965 exp_op_flags = sb->s_export_op->flags;
966
967 if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
968 !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
969
970
971
972
973
974
975
976 current->flags |= PF_LOCAL_THROTTLE;
977 restore_flags = true;
978 }
979
980 exp = fhp->fh_export;
981 use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
982
983 if (!EX_ISSYNC(exp))
984 stable = NFS_UNSTABLE;
985
986 if (stable && !use_wgather)
987 flags |= RWF_SYNC;
988
989 iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt);
990 if (flags & RWF_SYNC) {
991 down_write(&nf->nf_rwsem);
992 host_err = vfs_iter_write(file, &iter, &pos, flags);
993 if (host_err < 0)
994 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
995 nfsd_net_id));
996 up_write(&nf->nf_rwsem);
997 } else {
998 down_read(&nf->nf_rwsem);
999 if (verf)
1000 nfsd_copy_boot_verifier(verf,
1001 net_generic(SVC_NET(rqstp),
1002 nfsd_net_id));
1003 host_err = vfs_iter_write(file, &iter, &pos, flags);
1004 up_read(&nf->nf_rwsem);
1005 }
1006 if (host_err < 0) {
1007 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1008 nfsd_net_id));
1009 goto out_nfserr;
1010 }
1011 *cnt = host_err;
1012 nfsd_stats_io_write_add(exp, *cnt);
1013 fsnotify_modify(file);
1014
1015 if (stable && use_wgather) {
1016 host_err = wait_for_concurrent_writes(file);
1017 if (host_err < 0)
1018 nfsd_reset_boot_verifier(net_generic(SVC_NET(rqstp),
1019 nfsd_net_id));
1020 }
1021
1022out_nfserr:
1023 if (host_err >= 0) {
1024 trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
1025 nfserr = nfs_ok;
1026 } else {
1027 trace_nfsd_write_err(rqstp, fhp, offset, host_err);
1028 nfserr = nfserrno(host_err);
1029 }
1030 if (restore_flags)
1031 current_restore_flags(pflags, PF_LOCAL_THROTTLE);
1032 return nfserr;
1033}
1034
1035
1036
1037
1038
1039
1040__be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
1041 loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
1042 u32 *eof)
1043{
1044 struct nfsd_file *nf;
1045 struct file *file;
1046 __be32 err;
1047
1048 trace_nfsd_read_start(rqstp, fhp, offset, *count);
1049 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
1050 if (err)
1051 return err;
1052
1053 file = nf->nf_file;
1054 if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
1055 err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
1056 else
1057 err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
1058
1059 nfsd_file_put(nf);
1060
1061 trace_nfsd_read_done(rqstp, fhp, offset, *count);
1062
1063 return err;
1064}
1065
1066
1067
1068
1069
1070
1071__be32
1072nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1073 struct kvec *vec, int vlen, unsigned long *cnt, int stable,
1074 __be32 *verf)
1075{
1076 struct nfsd_file *nf;
1077 __be32 err;
1078
1079 trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
1080
1081 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf);
1082 if (err)
1083 goto out;
1084
1085 err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
1086 vlen, cnt, stable, verf);
1087 nfsd_file_put(nf);
1088out:
1089 trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
1090 return err;
1091}
1092
1093#ifdef CONFIG_NFSD_V3
1094static int
1095nfsd_filemap_write_and_wait_range(struct nfsd_file *nf, loff_t offset,
1096 loff_t end)
1097{
1098 struct address_space *mapping = nf->nf_file->f_mapping;
1099 int ret = filemap_fdatawrite_range(mapping, offset, end);
1100
1101 if (ret)
1102 return ret;
1103 filemap_fdatawait_range_keep_errors(mapping, offset, end);
1104 return 0;
1105}
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116__be32
1117nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1118 loff_t offset, unsigned long count, __be32 *verf)
1119{
1120 struct nfsd_file *nf;
1121 loff_t end = LLONG_MAX;
1122 __be32 err = nfserr_inval;
1123
1124 if (offset < 0)
1125 goto out;
1126 if (count != 0) {
1127 end = offset + (loff_t)count - 1;
1128 if (end < offset)
1129 goto out;
1130 }
1131
1132 err = nfsd_file_acquire(rqstp, fhp,
1133 NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf);
1134 if (err)
1135 goto out;
1136 if (EX_ISSYNC(fhp->fh_export)) {
1137 int err2 = nfsd_filemap_write_and_wait_range(nf, offset, end);
1138
1139 down_write(&nf->nf_rwsem);
1140 if (!err2)
1141 err2 = vfs_fsync_range(nf->nf_file, offset, end, 0);
1142 switch (err2) {
1143 case 0:
1144 nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1145 nfsd_net_id));
1146 break;
1147 case -EINVAL:
1148 err = nfserr_notsupp;
1149 break;
1150 default:
1151 err = nfserrno(err2);
1152 nfsd_reset_boot_verifier(net_generic(nf->nf_net,
1153 nfsd_net_id));
1154 }
1155 up_write(&nf->nf_rwsem);
1156 } else
1157 nfsd_copy_boot_verifier(verf, net_generic(nf->nf_net,
1158 nfsd_net_id));
1159
1160 nfsd_file_put(nf);
1161out:
1162 return err;
1163}
1164#endif
1165
1166static __be32
1167nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1168 struct iattr *iap)
1169{
1170
1171
1172
1173 iap->ia_valid &= ~ATTR_MODE;
1174
1175
1176
1177
1178
1179 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1180 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1181 if (iap->ia_valid)
1182 return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0);
1183
1184 return nfserrno(commit_metadata(resfhp));
1185}
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195static void
1196nfsd_check_ignore_resizing(struct iattr *iap)
1197{
1198 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1199 iap->ia_valid &= ~ATTR_SIZE;
1200}
1201
1202
1203__be32
1204nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
1205 char *fname, int flen, struct iattr *iap,
1206 int type, dev_t rdev, struct svc_fh *resfhp)
1207{
1208 struct dentry *dentry, *dchild;
1209 struct inode *dirp;
1210 __be32 err;
1211 __be32 err2;
1212 int host_err;
1213
1214 dentry = fhp->fh_dentry;
1215 dirp = d_inode(dentry);
1216
1217 dchild = dget(resfhp->fh_dentry);
1218 if (!fhp->fh_locked) {
1219 WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
1220 dentry);
1221 err = nfserr_io;
1222 goto out;
1223 }
1224
1225 err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
1226 if (err)
1227 goto out;
1228
1229 if (!(iap->ia_valid & ATTR_MODE))
1230 iap->ia_mode = 0;
1231 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1232
1233 if (!IS_POSIXACL(dirp))
1234 iap->ia_mode &= ~current_umask();
1235
1236 err = 0;
1237 host_err = 0;
1238 switch (type) {
1239 case S_IFREG:
1240 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1241 if (!host_err)
1242 nfsd_check_ignore_resizing(iap);
1243 break;
1244 case S_IFDIR:
1245 host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
1246 if (!host_err && unlikely(d_unhashed(dchild))) {
1247 struct dentry *d;
1248 d = lookup_one_len(dchild->d_name.name,
1249 dchild->d_parent,
1250 dchild->d_name.len);
1251 if (IS_ERR(d)) {
1252 host_err = PTR_ERR(d);
1253 break;
1254 }
1255 if (unlikely(d_is_negative(d))) {
1256 dput(d);
1257 err = nfserr_serverfault;
1258 goto out;
1259 }
1260 dput(resfhp->fh_dentry);
1261 resfhp->fh_dentry = dget(d);
1262 err = fh_update(resfhp);
1263 dput(dchild);
1264 dchild = d;
1265 if (err)
1266 goto out;
1267 }
1268 break;
1269 case S_IFCHR:
1270 case S_IFBLK:
1271 case S_IFIFO:
1272 case S_IFSOCK:
1273 host_err = vfs_mknod(&init_user_ns, dirp, dchild,
1274 iap->ia_mode, rdev);
1275 break;
1276 default:
1277 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1278 type);
1279 host_err = -EINVAL;
1280 }
1281 if (host_err < 0)
1282 goto out_nfserr;
1283
1284 err = nfsd_create_setattr(rqstp, resfhp, iap);
1285
1286
1287
1288
1289
1290
1291
1292 err2 = nfserrno(commit_metadata(fhp));
1293 if (err2)
1294 err = err2;
1295
1296
1297
1298 if (!err)
1299 err = fh_update(resfhp);
1300out:
1301 dput(dchild);
1302 return err;
1303
1304out_nfserr:
1305 err = nfserrno(host_err);
1306 goto out;
1307}
1308
1309
1310
1311
1312
1313
1314
1315__be32
1316nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1317 char *fname, int flen, struct iattr *iap,
1318 int type, dev_t rdev, struct svc_fh *resfhp)
1319{
1320 struct dentry *dentry, *dchild = NULL;
1321 __be32 err;
1322 int host_err;
1323
1324 if (isdotent(fname, flen))
1325 return nfserr_exist;
1326
1327 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
1328 if (err)
1329 return err;
1330
1331 dentry = fhp->fh_dentry;
1332
1333 host_err = fh_want_write(fhp);
1334 if (host_err)
1335 return nfserrno(host_err);
1336
1337 fh_lock_nested(fhp, I_MUTEX_PARENT);
1338 dchild = lookup_one_len(fname, dentry, flen);
1339 host_err = PTR_ERR(dchild);
1340 if (IS_ERR(dchild))
1341 return nfserrno(host_err);
1342 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1343
1344
1345
1346
1347 dput(dchild);
1348 if (err)
1349 return err;
1350 return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
1351 rdev, resfhp);
1352}
1353
1354#ifdef CONFIG_NFSD_V3
1355
1356
1357
1358
1359__be32
1360do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1361 char *fname, int flen, struct iattr *iap,
1362 struct svc_fh *resfhp, int createmode, u32 *verifier,
1363 bool *truncp, bool *created)
1364{
1365 struct dentry *dentry, *dchild = NULL;
1366 struct inode *dirp;
1367 __be32 err;
1368 int host_err;
1369 __u32 v_mtime=0, v_atime=0;
1370
1371 err = nfserr_perm;
1372 if (!flen)
1373 goto out;
1374 err = nfserr_exist;
1375 if (isdotent(fname, flen))
1376 goto out;
1377 if (!(iap->ia_valid & ATTR_MODE))
1378 iap->ia_mode = 0;
1379 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1380 if (err)
1381 goto out;
1382
1383 dentry = fhp->fh_dentry;
1384 dirp = d_inode(dentry);
1385
1386 host_err = fh_want_write(fhp);
1387 if (host_err)
1388 goto out_nfserr;
1389
1390 fh_lock_nested(fhp, I_MUTEX_PARENT);
1391
1392
1393
1394
1395 dchild = lookup_one_len(fname, dentry, flen);
1396 host_err = PTR_ERR(dchild);
1397 if (IS_ERR(dchild))
1398 goto out_nfserr;
1399
1400
1401 if (d_really_is_negative(dchild)) {
1402 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1403 if (err)
1404 goto out;
1405 }
1406
1407 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1408 if (err)
1409 goto out;
1410
1411 if (nfsd_create_is_exclusive(createmode)) {
1412
1413
1414
1415
1416
1417
1418 v_mtime = verifier[0]&0x7fffffff;
1419 v_atime = verifier[1]&0x7fffffff;
1420 }
1421
1422 if (d_really_is_positive(dchild)) {
1423 err = 0;
1424
1425 switch (createmode) {
1426 case NFS3_CREATE_UNCHECKED:
1427 if (! d_is_reg(dchild))
1428 goto out;
1429 else if (truncp) {
1430
1431
1432
1433
1434
1435
1436
1437 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1438 }
1439 else {
1440 iap->ia_valid &= ATTR_SIZE;
1441 goto set_attr;
1442 }
1443 break;
1444 case NFS3_CREATE_EXCLUSIVE:
1445 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1446 && d_inode(dchild)->i_atime.tv_sec == v_atime
1447 && d_inode(dchild)->i_size == 0 ) {
1448 if (created)
1449 *created = true;
1450 break;
1451 }
1452 fallthrough;
1453 case NFS4_CREATE_EXCLUSIVE4_1:
1454 if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
1455 && d_inode(dchild)->i_atime.tv_sec == v_atime
1456 && d_inode(dchild)->i_size == 0 ) {
1457 if (created)
1458 *created = true;
1459 goto set_attr;
1460 }
1461 fallthrough;
1462 case NFS3_CREATE_GUARDED:
1463 err = nfserr_exist;
1464 }
1465 fh_drop_write(fhp);
1466 goto out;
1467 }
1468
1469 if (!IS_POSIXACL(dirp))
1470 iap->ia_mode &= ~current_umask();
1471
1472 host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
1473 if (host_err < 0) {
1474 fh_drop_write(fhp);
1475 goto out_nfserr;
1476 }
1477 if (created)
1478 *created = true;
1479
1480 nfsd_check_ignore_resizing(iap);
1481
1482 if (nfsd_create_is_exclusive(createmode)) {
1483
1484 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1485 | ATTR_MTIME_SET|ATTR_ATIME_SET;
1486
1487 iap->ia_mtime.tv_sec = v_mtime;
1488 iap->ia_atime.tv_sec = v_atime;
1489 iap->ia_mtime.tv_nsec = 0;
1490 iap->ia_atime.tv_nsec = 0;
1491 }
1492
1493 set_attr:
1494 err = nfsd_create_setattr(rqstp, resfhp, iap);
1495
1496
1497
1498
1499
1500 if (!err)
1501 err = nfserrno(commit_metadata(fhp));
1502
1503
1504
1505
1506 if (!err)
1507 err = fh_update(resfhp);
1508
1509 out:
1510 fh_unlock(fhp);
1511 if (dchild && !IS_ERR(dchild))
1512 dput(dchild);
1513 fh_drop_write(fhp);
1514 return err;
1515
1516 out_nfserr:
1517 err = nfserrno(host_err);
1518 goto out;
1519}
1520#endif
1521
1522
1523
1524
1525
1526
1527__be32
1528nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1529{
1530 __be32 err;
1531 const char *link;
1532 struct path path;
1533 DEFINE_DELAYED_CALL(done);
1534 int len;
1535
1536 err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1537 if (unlikely(err))
1538 return err;
1539
1540 path.mnt = fhp->fh_export->ex_path.mnt;
1541 path.dentry = fhp->fh_dentry;
1542
1543 if (unlikely(!d_is_symlink(path.dentry)))
1544 return nfserr_inval;
1545
1546 touch_atime(&path);
1547
1548 link = vfs_get_link(path.dentry, &done);
1549 if (IS_ERR(link))
1550 return nfserrno(PTR_ERR(link));
1551
1552 len = strlen(link);
1553 if (len < *lenp)
1554 *lenp = len;
1555 memcpy(buf, link, *lenp);
1556 do_delayed_call(&done);
1557 return 0;
1558}
1559
1560
1561
1562
1563
1564__be32
1565nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1566 char *fname, int flen,
1567 char *path,
1568 struct svc_fh *resfhp)
1569{
1570 struct dentry *dentry, *dnew;
1571 __be32 err, cerr;
1572 int host_err;
1573
1574 err = nfserr_noent;
1575 if (!flen || path[0] == '\0')
1576 goto out;
1577 err = nfserr_exist;
1578 if (isdotent(fname, flen))
1579 goto out;
1580
1581 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1582 if (err)
1583 goto out;
1584
1585 host_err = fh_want_write(fhp);
1586 if (host_err)
1587 goto out_nfserr;
1588
1589 fh_lock(fhp);
1590 dentry = fhp->fh_dentry;
1591 dnew = lookup_one_len(fname, dentry, flen);
1592 host_err = PTR_ERR(dnew);
1593 if (IS_ERR(dnew))
1594 goto out_nfserr;
1595
1596 host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
1597 err = nfserrno(host_err);
1598 fh_unlock(fhp);
1599 if (!err)
1600 err = nfserrno(commit_metadata(fhp));
1601
1602 fh_drop_write(fhp);
1603
1604 cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1605 dput(dnew);
1606 if (err==0) err = cerr;
1607out:
1608 return err;
1609
1610out_nfserr:
1611 err = nfserrno(host_err);
1612 goto out;
1613}
1614
1615
1616
1617
1618
1619__be32
1620nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1621 char *name, int len, struct svc_fh *tfhp)
1622{
1623 struct dentry *ddir, *dnew, *dold;
1624 struct inode *dirp;
1625 __be32 err;
1626 int host_err;
1627
1628 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1629 if (err)
1630 goto out;
1631 err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1632 if (err)
1633 goto out;
1634 err = nfserr_isdir;
1635 if (d_is_dir(tfhp->fh_dentry))
1636 goto out;
1637 err = nfserr_perm;
1638 if (!len)
1639 goto out;
1640 err = nfserr_exist;
1641 if (isdotent(name, len))
1642 goto out;
1643
1644 host_err = fh_want_write(tfhp);
1645 if (host_err) {
1646 err = nfserrno(host_err);
1647 goto out;
1648 }
1649
1650 fh_lock_nested(ffhp, I_MUTEX_PARENT);
1651 ddir = ffhp->fh_dentry;
1652 dirp = d_inode(ddir);
1653
1654 dnew = lookup_one_len(name, ddir, len);
1655 host_err = PTR_ERR(dnew);
1656 if (IS_ERR(dnew))
1657 goto out_nfserr;
1658
1659 dold = tfhp->fh_dentry;
1660
1661 err = nfserr_noent;
1662 if (d_really_is_negative(dold))
1663 goto out_dput;
1664 host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
1665 fh_unlock(ffhp);
1666 if (!host_err) {
1667 err = nfserrno(commit_metadata(ffhp));
1668 if (!err)
1669 err = nfserrno(commit_metadata(tfhp));
1670 } else {
1671 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1672 err = nfserr_acces;
1673 else
1674 err = nfserrno(host_err);
1675 }
1676out_dput:
1677 dput(dnew);
1678out_unlock:
1679 fh_unlock(ffhp);
1680 fh_drop_write(tfhp);
1681out:
1682 return err;
1683
1684out_nfserr:
1685 err = nfserrno(host_err);
1686 goto out_unlock;
1687}
1688
1689static void
1690nfsd_close_cached_files(struct dentry *dentry)
1691{
1692 struct inode *inode = d_inode(dentry);
1693
1694 if (inode && S_ISREG(inode->i_mode))
1695 nfsd_file_close_inode_sync(inode);
1696}
1697
1698static bool
1699nfsd_has_cached_files(struct dentry *dentry)
1700{
1701 bool ret = false;
1702 struct inode *inode = d_inode(dentry);
1703
1704 if (inode && S_ISREG(inode->i_mode))
1705 ret = nfsd_file_is_cached(inode);
1706 return ret;
1707}
1708
1709
1710
1711
1712
1713__be32
1714nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1715 struct svc_fh *tfhp, char *tname, int tlen)
1716{
1717 struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
1718 struct inode *fdir, *tdir;
1719 __be32 err;
1720 int host_err;
1721 bool close_cached = false;
1722
1723 err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1724 if (err)
1725 goto out;
1726 err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1727 if (err)
1728 goto out;
1729
1730 fdentry = ffhp->fh_dentry;
1731 fdir = d_inode(fdentry);
1732
1733 tdentry = tfhp->fh_dentry;
1734 tdir = d_inode(tdentry);
1735
1736 err = nfserr_perm;
1737 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1738 goto out;
1739
1740retry:
1741 host_err = fh_want_write(ffhp);
1742 if (host_err) {
1743 err = nfserrno(host_err);
1744 goto out;
1745 }
1746
1747
1748
1749 trap = lock_rename(tdentry, fdentry);
1750 ffhp->fh_locked = tfhp->fh_locked = true;
1751 fill_pre_wcc(ffhp);
1752 fill_pre_wcc(tfhp);
1753
1754 odentry = lookup_one_len(fname, fdentry, flen);
1755 host_err = PTR_ERR(odentry);
1756 if (IS_ERR(odentry))
1757 goto out_nfserr;
1758
1759 host_err = -ENOENT;
1760 if (d_really_is_negative(odentry))
1761 goto out_dput_old;
1762 host_err = -EINVAL;
1763 if (odentry == trap)
1764 goto out_dput_old;
1765
1766 ndentry = lookup_one_len(tname, tdentry, tlen);
1767 host_err = PTR_ERR(ndentry);
1768 if (IS_ERR(ndentry))
1769 goto out_dput_old;
1770 host_err = -ENOTEMPTY;
1771 if (ndentry == trap)
1772 goto out_dput_new;
1773
1774 host_err = -EXDEV;
1775 if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1776 goto out_dput_new;
1777 if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1778 goto out_dput_new;
1779
1780 if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
1781 nfsd_has_cached_files(ndentry)) {
1782 close_cached = true;
1783 goto out_dput_old;
1784 } else {
1785 struct renamedata rd = {
1786 .old_mnt_userns = &init_user_ns,
1787 .old_dir = fdir,
1788 .old_dentry = odentry,
1789 .new_mnt_userns = &init_user_ns,
1790 .new_dir = tdir,
1791 .new_dentry = ndentry,
1792 };
1793 host_err = vfs_rename(&rd);
1794 if (!host_err) {
1795 host_err = commit_metadata(tfhp);
1796 if (!host_err)
1797 host_err = commit_metadata(ffhp);
1798 }
1799 }
1800 out_dput_new:
1801 dput(ndentry);
1802 out_dput_old:
1803 dput(odentry);
1804 out_nfserr:
1805 err = nfserrno(host_err);
1806
1807
1808
1809
1810
1811 if (!close_cached) {
1812 fill_post_wcc(ffhp);
1813 fill_post_wcc(tfhp);
1814 }
1815 unlock_rename(tdentry, fdentry);
1816 ffhp->fh_locked = tfhp->fh_locked = false;
1817 fh_drop_write(ffhp);
1818
1819
1820
1821
1822
1823
1824
1825 if (close_cached) {
1826 close_cached = false;
1827 nfsd_close_cached_files(ndentry);
1828 dput(ndentry);
1829 goto retry;
1830 }
1831out:
1832 return err;
1833}
1834
1835
1836
1837
1838
1839__be32
1840nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1841 char *fname, int flen)
1842{
1843 struct dentry *dentry, *rdentry;
1844 struct inode *dirp;
1845 struct inode *rinode;
1846 __be32 err;
1847 int host_err;
1848
1849 err = nfserr_acces;
1850 if (!flen || isdotent(fname, flen))
1851 goto out;
1852 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1853 if (err)
1854 goto out;
1855
1856 host_err = fh_want_write(fhp);
1857 if (host_err)
1858 goto out_nfserr;
1859
1860 fh_lock_nested(fhp, I_MUTEX_PARENT);
1861 dentry = fhp->fh_dentry;
1862 dirp = d_inode(dentry);
1863
1864 rdentry = lookup_one_len(fname, dentry, flen);
1865 host_err = PTR_ERR(rdentry);
1866 if (IS_ERR(rdentry))
1867 goto out_drop_write;
1868
1869 if (d_really_is_negative(rdentry)) {
1870 dput(rdentry);
1871 host_err = -ENOENT;
1872 goto out_drop_write;
1873 }
1874 rinode = d_inode(rdentry);
1875 ihold(rinode);
1876
1877 if (!type)
1878 type = d_inode(rdentry)->i_mode & S_IFMT;
1879
1880 if (type != S_IFDIR) {
1881 if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
1882 nfsd_close_cached_files(rdentry);
1883 host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
1884 } else {
1885 host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
1886 }
1887
1888 fh_unlock(fhp);
1889 if (!host_err)
1890 host_err = commit_metadata(fhp);
1891 dput(rdentry);
1892 iput(rinode);
1893
1894out_drop_write:
1895 fh_drop_write(fhp);
1896out_nfserr:
1897 if (host_err == -EBUSY) {
1898
1899
1900
1901 if (nfsd_v4client(rqstp))
1902 err = nfserr_file_open;
1903 else
1904 err = nfserr_acces;
1905 } else {
1906 err = nfserrno(host_err);
1907 }
1908out:
1909 return err;
1910}
1911
1912
1913
1914
1915
1916
1917
1918
1919struct buffered_dirent {
1920 u64 ino;
1921 loff_t offset;
1922 int namlen;
1923 unsigned int d_type;
1924 char name[];
1925};
1926
1927struct readdir_data {
1928 struct dir_context ctx;
1929 char *dirent;
1930 size_t used;
1931 int full;
1932};
1933
1934static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1935 int namlen, loff_t offset, u64 ino,
1936 unsigned int d_type)
1937{
1938 struct readdir_data *buf =
1939 container_of(ctx, struct readdir_data, ctx);
1940 struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1941 unsigned int reclen;
1942
1943 reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1944 if (buf->used + reclen > PAGE_SIZE) {
1945 buf->full = 1;
1946 return -EINVAL;
1947 }
1948
1949 de->namlen = namlen;
1950 de->offset = offset;
1951 de->ino = ino;
1952 de->d_type = d_type;
1953 memcpy(de->name, name, namlen);
1954 buf->used += reclen;
1955
1956 return 0;
1957}
1958
1959static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
1960 nfsd_filldir_t func, struct readdir_cd *cdp,
1961 loff_t *offsetp)
1962{
1963 struct buffered_dirent *de;
1964 int host_err;
1965 int size;
1966 loff_t offset;
1967 struct readdir_data buf = {
1968 .ctx.actor = nfsd_buffered_filldir,
1969 .dirent = (void *)__get_free_page(GFP_KERNEL)
1970 };
1971
1972 if (!buf.dirent)
1973 return nfserrno(-ENOMEM);
1974
1975 offset = *offsetp;
1976
1977 while (1) {
1978 unsigned int reclen;
1979
1980 cdp->err = nfserr_eof;
1981 buf.used = 0;
1982 buf.full = 0;
1983
1984 host_err = iterate_dir(file, &buf.ctx);
1985 if (buf.full)
1986 host_err = 0;
1987
1988 if (host_err < 0)
1989 break;
1990
1991 size = buf.used;
1992
1993 if (!size)
1994 break;
1995
1996 de = (struct buffered_dirent *)buf.dirent;
1997 while (size > 0) {
1998 offset = de->offset;
1999
2000 if (func(cdp, de->name, de->namlen, de->offset,
2001 de->ino, de->d_type))
2002 break;
2003
2004 if (cdp->err != nfs_ok)
2005 break;
2006
2007 trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
2008
2009 reclen = ALIGN(sizeof(*de) + de->namlen,
2010 sizeof(u64));
2011 size -= reclen;
2012 de = (struct buffered_dirent *)((char *)de + reclen);
2013 }
2014 if (size > 0)
2015 break;
2016
2017 offset = vfs_llseek(file, 0, SEEK_CUR);
2018 }
2019
2020 free_page((unsigned long)(buf.dirent));
2021
2022 if (host_err)
2023 return nfserrno(host_err);
2024
2025 *offsetp = offset;
2026 return cdp->err;
2027}
2028
2029
2030
2031
2032
2033__be32
2034nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
2035 struct readdir_cd *cdp, nfsd_filldir_t func)
2036{
2037 __be32 err;
2038 struct file *file;
2039 loff_t offset = *offsetp;
2040 int may_flags = NFSD_MAY_READ;
2041
2042
2043 if (rqstp->rq_vers > 2)
2044 may_flags |= NFSD_MAY_64BIT_COOKIE;
2045
2046 err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
2047 if (err)
2048 goto out;
2049
2050 offset = vfs_llseek(file, offset, SEEK_SET);
2051 if (offset < 0) {
2052 err = nfserrno((int)offset);
2053 goto out_close;
2054 }
2055
2056 err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
2057
2058 if (err == nfserr_eof || err == nfserr_toosmall)
2059 err = nfs_ok;
2060out_close:
2061 fput(file);
2062out:
2063 return err;
2064}
2065
2066
2067
2068
2069
2070__be32
2071nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2072{
2073 __be32 err;
2074
2075 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2076 if (!err) {
2077 struct path path = {
2078 .mnt = fhp->fh_export->ex_path.mnt,
2079 .dentry = fhp->fh_dentry,
2080 };
2081 if (vfs_statfs(&path, stat))
2082 err = nfserr_io;
2083 }
2084 return err;
2085}
2086
2087static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2088{
2089 return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2090}
2091
2092#ifdef CONFIG_NFSD_V4
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112static __be32
2113nfsd_xattr_errno(int err)
2114{
2115 switch (err) {
2116 case -ENODATA:
2117 return nfserr_noxattr;
2118 case -E2BIG:
2119 return nfserr_xattr2big;
2120 case -ERANGE:
2121 return nfserr_toosmall;
2122 }
2123 return nfserrno(err);
2124}
2125
2126
2127
2128
2129
2130
2131
2132
2133__be32
2134nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2135 void **bufp, int *lenp)
2136{
2137 ssize_t len;
2138 __be32 err;
2139 char *buf;
2140 struct inode *inode;
2141 struct dentry *dentry;
2142
2143 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2144 if (err)
2145 return err;
2146
2147 err = nfs_ok;
2148 dentry = fhp->fh_dentry;
2149 inode = d_inode(dentry);
2150
2151 inode_lock_shared(inode);
2152
2153 len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
2154
2155
2156
2157
2158 if (len == 0) {
2159 *bufp = NULL;
2160 *lenp = 0;
2161 goto out;
2162 }
2163
2164 if (len < 0) {
2165 err = nfsd_xattr_errno(len);
2166 goto out;
2167 }
2168
2169 if (len > *lenp) {
2170 err = nfserr_toosmall;
2171 goto out;
2172 }
2173
2174 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2175 if (buf == NULL) {
2176 err = nfserr_jukebox;
2177 goto out;
2178 }
2179
2180 len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
2181 if (len <= 0) {
2182 kvfree(buf);
2183 buf = NULL;
2184 err = nfsd_xattr_errno(len);
2185 }
2186
2187 *lenp = len;
2188 *bufp = buf;
2189
2190out:
2191 inode_unlock_shared(inode);
2192
2193 return err;
2194}
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206__be32
2207nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
2208 int *lenp)
2209{
2210 ssize_t len;
2211 __be32 err;
2212 char *buf;
2213 struct inode *inode;
2214 struct dentry *dentry;
2215
2216 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
2217 if (err)
2218 return err;
2219
2220 dentry = fhp->fh_dentry;
2221 inode = d_inode(dentry);
2222 *lenp = 0;
2223
2224 inode_lock_shared(inode);
2225
2226 len = vfs_listxattr(dentry, NULL, 0);
2227 if (len <= 0) {
2228 err = nfsd_xattr_errno(len);
2229 goto out;
2230 }
2231
2232 if (len > XATTR_LIST_MAX) {
2233 err = nfserr_xattr2big;
2234 goto out;
2235 }
2236
2237
2238
2239
2240 buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
2241 if (buf == NULL) {
2242 err = nfserr_jukebox;
2243 goto out;
2244 }
2245
2246 len = vfs_listxattr(dentry, buf, len);
2247 if (len <= 0) {
2248 kvfree(buf);
2249 err = nfsd_xattr_errno(len);
2250 goto out;
2251 }
2252
2253 *lenp = len;
2254 *bufp = buf;
2255
2256 err = nfs_ok;
2257out:
2258 inode_unlock_shared(inode);
2259
2260 return err;
2261}
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271__be32
2272nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
2273{
2274 __be32 err;
2275 int ret;
2276
2277 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2278 if (err)
2279 return err;
2280
2281 ret = fh_want_write(fhp);
2282 if (ret)
2283 return nfserrno(ret);
2284
2285 fh_lock(fhp);
2286
2287 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
2288 name, NULL);
2289
2290 fh_unlock(fhp);
2291 fh_drop_write(fhp);
2292
2293 return nfsd_xattr_errno(ret);
2294}
2295
2296__be32
2297nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
2298 void *buf, u32 len, u32 flags)
2299{
2300 __be32 err;
2301 int ret;
2302
2303 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
2304 if (err)
2305 return err;
2306
2307 ret = fh_want_write(fhp);
2308 if (ret)
2309 return nfserrno(ret);
2310 fh_lock(fhp);
2311
2312 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
2313 len, flags, NULL);
2314
2315 fh_unlock(fhp);
2316 fh_drop_write(fhp);
2317
2318 return nfsd_xattr_errno(ret);
2319}
2320#endif
2321
2322
2323
2324
2325__be32
2326nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2327 struct dentry *dentry, int acc)
2328{
2329 struct inode *inode = d_inode(dentry);
2330 int err;
2331
2332 if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
2333 return 0;
2334#if 0
2335 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2336 acc,
2337 (acc & NFSD_MAY_READ)? " read" : "",
2338 (acc & NFSD_MAY_WRITE)? " write" : "",
2339 (acc & NFSD_MAY_EXEC)? " exec" : "",
2340 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2341 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2342 (acc & NFSD_MAY_LOCK)? " lock" : "",
2343 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2344 inode->i_mode,
2345 IS_IMMUTABLE(inode)? " immut" : "",
2346 IS_APPEND(inode)? " append" : "",
2347 __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
2348 dprintk(" owner %d/%d user %d/%d\n",
2349 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2350#endif
2351
2352
2353
2354
2355
2356 if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2357 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2358 if (exp_rdonly(rqstp, exp) ||
2359 __mnt_is_readonly(exp->ex_path.mnt))
2360 return nfserr_rofs;
2361 if ( IS_IMMUTABLE(inode))
2362 return nfserr_perm;
2363 }
2364 if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2365 return nfserr_perm;
2366
2367 if (acc & NFSD_MAY_LOCK) {
2368
2369
2370
2371
2372 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2373 return 0;
2374 else
2375 acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2376 }
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391 if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2392 uid_eq(inode->i_uid, current_fsuid()))
2393 return 0;
2394
2395
2396 err = inode_permission(&init_user_ns, inode,
2397 acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
2398
2399
2400 if (err == -EACCES && S_ISREG(inode->i_mode) &&
2401 (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2402 acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2403 err = inode_permission(&init_user_ns, inode, MAY_EXEC);
2404
2405 return err? nfserrno(err) : 0;
2406}
2407