vfs_vnops.c revision 1.221 1 /* $NetBSD: vfs_vnops.c,v 1.221 2021/07/18 09:30:36 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.221 2021/07/18 09:30:36 dholland Exp $");
70
71 #include "veriexec.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/file.h>
77 #include <sys/stat.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/mount.h>
81 #include <sys/namei.h>
82 #include <sys/vnode.h>
83 #include <sys/ioctl.h>
84 #include <sys/tty.h>
85 #include <sys/poll.h>
86 #include <sys/kauth.h>
87 #include <sys/syslog.h>
88 #include <sys/fstrans.h>
89 #include <sys/atomic.h>
90 #include <sys/filedesc.h>
91 #include <sys/wapbl.h>
92 #include <sys/mman.h>
93
94 #include <miscfs/specfs/specdev.h>
95 #include <miscfs/fifofs/fifo.h>
96
97 #include <uvm/uvm_extern.h>
98 #include <uvm/uvm_readahead.h>
99 #include <uvm/uvm_device.h>
100
101 #ifdef UNION
102 #include <fs/union/union.h>
103 #endif
104
105 #ifndef COMPAT_ZERODEV
106 #define COMPAT_ZERODEV(dev) (0)
107 #endif
108
109 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
110
111 #include <sys/verified_exec.h>
112
113 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
114 kauth_cred_t cred, int flags);
115 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
116 kauth_cred_t cred, int flags);
117 static int vn_closefile(file_t *fp);
118 static int vn_poll(file_t *fp, int events);
119 static int vn_fcntl(file_t *fp, u_int com, void *data);
120 static int vn_statfile(file_t *fp, struct stat *sb);
121 static int vn_ioctl(file_t *fp, u_long com, void *data);
122 static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *,
123 struct uvm_object **, int *);
124
125 const struct fileops vnops = {
126 .fo_name = "vn",
127 .fo_read = vn_read,
128 .fo_write = vn_write,
129 .fo_ioctl = vn_ioctl,
130 .fo_fcntl = vn_fcntl,
131 .fo_poll = vn_poll,
132 .fo_stat = vn_statfile,
133 .fo_close = vn_closefile,
134 .fo_kqfilter = vn_kqfilter,
135 .fo_restart = fnullop_restart,
136 .fo_mmap = vn_mmap,
137 };
138
139 /*
140 * Common code for vnode open operations.
141 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
142 *
143 * at_dvp is the directory for openat(), if any.
144 * pb is the path.
145 * nmode is additional namei flags, restricted to TRYEMULROOT and NOCHROOT.
146 * fmode is the open flags, converted from O_* to F*
147 * cmode is the creation file permissions.
148 *
149 * XXX shouldn't cmode be mode_t?
150 *
151 * On success produces either a vnode in *ret_vp, or if that is NULL,
152 * a file descriptor number in ret_fd.
153 *
154 * The caller may pass NULL for ret_fd (and ret_domove), in which case
155 * EOPNOTSUPP will be produced in the cases that would otherwise return
156 * a file descriptor.
157 *
158 * Note that callers that want no-follow behavior should pass
159 * O_NOFOLLOW in fmode. Neither FOLLOW nor NOFOLLOW in nmode is
160 * honored.
161 */
162 int
163 vn_open(struct vnode *at_dvp, struct pathbuf *pb,
164 int nmode, int fmode, int cmode,
165 struct vnode **ret_vp, bool *ret_domove, int *ret_fd)
166 {
167 struct nameidata nd;
168 struct vnode *vp = NULL;
169 struct lwp *l = curlwp;
170 kauth_cred_t cred = l->l_cred;
171 struct vattr va;
172 int error;
173 const char *pathstring;
174
175 KASSERT((nmode & (TRYEMULROOT | NOCHROOT)) == nmode);
176
177 KASSERT(ret_vp != NULL);
178 KASSERT((ret_domove == NULL) == (ret_fd == NULL));
179
180 if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
181 return EINVAL;
182
183 NDINIT(&nd, LOOKUP, nmode, pb);
184 if (at_dvp != NULL)
185 NDAT(&nd, at_dvp);
186
187 nd.ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
188
189 if (fmode & O_CREAT) {
190 nd.ni_cnd.cn_nameiop = CREATE;
191 nd.ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
192 if ((fmode & O_EXCL) == 0 &&
193 ((fmode & O_NOFOLLOW) == 0))
194 nd.ni_cnd.cn_flags |= FOLLOW;
195 if ((fmode & O_EXCL) == 0)
196 nd.ni_cnd.cn_flags |= NONEXCLHACK;
197 } else {
198 nd.ni_cnd.cn_nameiop = LOOKUP;
199 nd.ni_cnd.cn_flags |= LOCKLEAF;
200 if ((fmode & O_NOFOLLOW) == 0)
201 nd.ni_cnd.cn_flags |= FOLLOW;
202 }
203
204 pathstring = pathbuf_stringcopy_get(nd.ni_pathbuf);
205 if (pathstring == NULL) {
206 return ENOMEM;
207 }
208
209 /*
210 * When this "interface" was exposed to do_open() it used
211 * to initialize l_dupfd to -newfd-1 (thus passing in the
212 * new file handle number to use)... but nothing in the
213 * kernel uses that value. So just send 0.
214 */
215 l->l_dupfd = 0;
216
217 error = namei(&nd);
218 if (error)
219 goto out;
220
221 vp = nd.ni_vp;
222
223 #if NVERIEXEC > 0
224 error = veriexec_openchk(l, nd.ni_vp, pathstring, fmode);
225 if (error) {
226 /* We have to release the locks ourselves */
227 /*
228 * 20210604 dholland passing NONEXCLHACK means we can
229 * get ni_dvp == NULL back if ni_vp exists, and we should
230 * treat that like the non-O_CREAT case.
231 */
232 if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
233 if (vp == NULL) {
234 vput(nd.ni_dvp);
235 } else {
236 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
237 if (nd.ni_dvp == nd.ni_vp)
238 vrele(nd.ni_dvp);
239 else
240 vput(nd.ni_dvp);
241 nd.ni_dvp = NULL;
242 vput(vp);
243 }
244 } else {
245 vput(vp);
246 }
247 goto out;
248 }
249 #endif /* NVERIEXEC > 0 */
250
251 /*
252 * 20210604 dholland ditto
253 */
254 if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
255 if (nd.ni_vp == NULL) {
256 vattr_null(&va);
257 va.va_type = VREG;
258 va.va_mode = cmode;
259 if (fmode & O_EXCL)
260 va.va_vaflags |= VA_EXCLUSIVE;
261 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
262 &nd.ni_cnd, &va);
263 if (error) {
264 vput(nd.ni_dvp);
265 goto out;
266 }
267 fmode &= ~O_TRUNC;
268 vp = nd.ni_vp;
269 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
270 vput(nd.ni_dvp);
271 } else {
272 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
273 if (nd.ni_dvp == nd.ni_vp)
274 vrele(nd.ni_dvp);
275 else
276 vput(nd.ni_dvp);
277 nd.ni_dvp = NULL;
278 vp = nd.ni_vp;
279 if (fmode & O_EXCL) {
280 error = EEXIST;
281 goto bad;
282 }
283 fmode &= ~O_CREAT;
284 }
285 } else if ((fmode & O_CREAT) != 0) {
286 /*
287 * 20210606 dholland passing NONEXCLHACK means this
288 * case exists; it is the same as the following one
289 * but also needs to do things in the second (exists)
290 * half of the following block. (Besides handle
291 * ni_dvp, anyway.)
292 */
293 vp = nd.ni_vp;
294 KASSERT((fmode & O_EXCL) == 0);
295 fmode &= ~O_CREAT;
296 } else {
297 vp = nd.ni_vp;
298 }
299 if (vp->v_type == VSOCK) {
300 error = EOPNOTSUPP;
301 goto bad;
302 }
303 if (nd.ni_vp->v_type == VLNK) {
304 error = EFTYPE;
305 goto bad;
306 }
307
308 if ((fmode & O_CREAT) == 0) {
309 error = vn_openchk(vp, cred, fmode);
310 if (error != 0)
311 goto bad;
312 }
313
314 if (fmode & O_TRUNC) {
315 vattr_null(&va);
316 va.va_size = 0;
317 error = VOP_SETATTR(vp, &va, cred);
318 if (error != 0)
319 goto bad;
320 }
321 if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
322 goto bad;
323 if (fmode & FWRITE) {
324 mutex_enter(vp->v_interlock);
325 vp->v_writecount++;
326 mutex_exit(vp->v_interlock);
327 }
328
329 bad:
330 if (error)
331 vput(vp);
332 out:
333 pathbuf_stringcopy_put(nd.ni_pathbuf, pathstring);
334
335 switch (error) {
336 case EDUPFD:
337 case EMOVEFD:
338 /* if the caller isn't prepared to handle fds, fail for them */
339 if (ret_fd == NULL) {
340 error = EOPNOTSUPP;
341 break;
342 }
343 *ret_vp = NULL;
344 *ret_domove = error == EMOVEFD;
345 *ret_fd = l->l_dupfd;
346 error = 0;
347 break;
348 case 0:
349 *ret_vp = vp;
350 break;
351 }
352 l->l_dupfd = 0;
353 return error;
354 }
355
356 /*
357 * Check for write permissions on the specified vnode.
358 * Prototype text segments cannot be written.
359 */
360 int
361 vn_writechk(struct vnode *vp)
362 {
363
364 /*
365 * If the vnode is in use as a process's text,
366 * we can't allow writing.
367 */
368 if (vp->v_iflag & VI_TEXT)
369 return (ETXTBSY);
370 return (0);
371 }
372
373 int
374 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
375 {
376 int permbits = 0;
377 int error;
378
379 if (vp->v_type == VNON || vp->v_type == VBAD)
380 return ENXIO;
381
382 if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
383 return ENOTDIR;
384
385 if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
386 return EFTYPE;
387
388 if ((fflags & FREAD) != 0) {
389 permbits = VREAD;
390 }
391 if ((fflags & FEXEC) != 0) {
392 permbits |= VEXEC;
393 }
394 if ((fflags & (FWRITE | O_TRUNC)) != 0) {
395 permbits |= VWRITE;
396 if (vp->v_type == VDIR) {
397 error = EISDIR;
398 goto bad;
399 }
400 error = vn_writechk(vp);
401 if (error != 0)
402 goto bad;
403 }
404 error = VOP_ACCESS(vp, permbits, cred);
405 bad:
406 return error;
407 }
408
409 /*
410 * Mark a vnode as having executable mappings.
411 */
412 void
413 vn_markexec(struct vnode *vp)
414 {
415
416 if ((vp->v_iflag & VI_EXECMAP) != 0) {
417 /* Safe unlocked, as long as caller holds a reference. */
418 return;
419 }
420
421 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
422 mutex_enter(vp->v_interlock);
423 if ((vp->v_iflag & VI_EXECMAP) == 0) {
424 cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
425 vp->v_iflag |= VI_EXECMAP;
426 }
427 mutex_exit(vp->v_interlock);
428 rw_exit(vp->v_uobj.vmobjlock);
429 }
430
431 /*
432 * Mark a vnode as being the text of a process.
433 * Fail if the vnode is currently writable.
434 */
435 int
436 vn_marktext(struct vnode *vp)
437 {
438
439 if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
440 /* Safe unlocked, as long as caller holds a reference. */
441 return (0);
442 }
443
444 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
445 mutex_enter(vp->v_interlock);
446 if (vp->v_writecount != 0) {
447 KASSERT((vp->v_iflag & VI_TEXT) == 0);
448 mutex_exit(vp->v_interlock);
449 rw_exit(vp->v_uobj.vmobjlock);
450 return (ETXTBSY);
451 }
452 if ((vp->v_iflag & VI_EXECMAP) == 0) {
453 cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
454 }
455 vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
456 mutex_exit(vp->v_interlock);
457 rw_exit(vp->v_uobj.vmobjlock);
458 return (0);
459 }
460
461 /*
462 * Vnode close call
463 *
464 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
465 */
466 int
467 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
468 {
469 int error;
470
471 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
472 if (flags & FWRITE) {
473 mutex_enter(vp->v_interlock);
474 KASSERT(vp->v_writecount > 0);
475 vp->v_writecount--;
476 mutex_exit(vp->v_interlock);
477 }
478 error = VOP_CLOSE(vp, flags, cred);
479 vput(vp);
480 return (error);
481 }
482
483 static int
484 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
485 {
486 struct lwp *l = curlwp;
487 off_t testoff;
488
489 if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
490 return 0;
491
492 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
493 if (ioflag & IO_APPEND)
494 testoff = vp->v_size;
495 else
496 testoff = uio->uio_offset;
497
498 if (testoff + uio->uio_resid >
499 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
500 mutex_enter(&proc_lock);
501 psignal(l->l_proc, SIGXFSZ);
502 mutex_exit(&proc_lock);
503 return EFBIG;
504 }
505
506 return 0;
507 }
508
509 /*
510 * Package up an I/O request on a vnode into a uio and do it.
511 */
512 int
513 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
514 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
515 struct lwp *l)
516 {
517 struct uio auio;
518 struct iovec aiov;
519 int error;
520
521 if ((ioflg & IO_NODELOCKED) == 0) {
522 if (rw == UIO_READ) {
523 vn_lock(vp, LK_SHARED | LK_RETRY);
524 } else /* UIO_WRITE */ {
525 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
526 }
527 }
528 auio.uio_iov = &aiov;
529 auio.uio_iovcnt = 1;
530 aiov.iov_base = base;
531 aiov.iov_len = len;
532 auio.uio_resid = len;
533 auio.uio_offset = offset;
534 auio.uio_rw = rw;
535 if (segflg == UIO_SYSSPACE) {
536 UIO_SETUP_SYSSPACE(&auio);
537 } else {
538 auio.uio_vmspace = l->l_proc->p_vmspace;
539 }
540
541 if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
542 goto out;
543
544 if (rw == UIO_READ) {
545 error = VOP_READ(vp, &auio, ioflg, cred);
546 } else {
547 error = VOP_WRITE(vp, &auio, ioflg, cred);
548 }
549
550 if (aresid)
551 *aresid = auio.uio_resid;
552 else
553 if (auio.uio_resid && error == 0)
554 error = EIO;
555
556 out:
557 if ((ioflg & IO_NODELOCKED) == 0) {
558 VOP_UNLOCK(vp);
559 }
560 return (error);
561 }
562
563 int
564 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
565 struct lwp *l, off_t **cookies, int *ncookies)
566 {
567 struct vnode *vp = fp->f_vnode;
568 struct iovec aiov;
569 struct uio auio;
570 int error, eofflag;
571
572 /* Limit the size on any kernel buffers used by VOP_READDIR */
573 count = uimin(MAXBSIZE, count);
574
575 unionread:
576 if (vp->v_type != VDIR)
577 return (EINVAL);
578 aiov.iov_base = bf;
579 aiov.iov_len = count;
580 auio.uio_iov = &aiov;
581 auio.uio_iovcnt = 1;
582 auio.uio_rw = UIO_READ;
583 if (segflg == UIO_SYSSPACE) {
584 UIO_SETUP_SYSSPACE(&auio);
585 } else {
586 KASSERT(l == curlwp);
587 auio.uio_vmspace = l->l_proc->p_vmspace;
588 }
589 auio.uio_resid = count;
590 vn_lock(vp, LK_SHARED | LK_RETRY);
591 auio.uio_offset = fp->f_offset;
592 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
593 ncookies);
594 mutex_enter(&fp->f_lock);
595 fp->f_offset = auio.uio_offset;
596 mutex_exit(&fp->f_lock);
597 VOP_UNLOCK(vp);
598 if (error)
599 return (error);
600
601 if (count == auio.uio_resid && vn_union_readdir_hook) {
602 struct vnode *ovp = vp;
603
604 error = (*vn_union_readdir_hook)(&vp, fp, l);
605 if (error)
606 return (error);
607 if (vp != ovp)
608 goto unionread;
609 }
610
611 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
612 (vp->v_mount->mnt_flag & MNT_UNION)) {
613 struct vnode *tvp = vp;
614 vp = vp->v_mount->mnt_vnodecovered;
615 vref(vp);
616 mutex_enter(&fp->f_lock);
617 fp->f_vnode = vp;
618 fp->f_offset = 0;
619 mutex_exit(&fp->f_lock);
620 vrele(tvp);
621 goto unionread;
622 }
623 *done = count - auio.uio_resid;
624 return error;
625 }
626
627 /*
628 * File table vnode read routine.
629 */
630 static int
631 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
632 int flags)
633 {
634 struct vnode *vp = fp->f_vnode;
635 int error, ioflag, fflag;
636 size_t count;
637
638 ioflag = IO_ADV_ENCODE(fp->f_advice);
639 fflag = fp->f_flag;
640 if (fflag & FNONBLOCK)
641 ioflag |= IO_NDELAY;
642 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
643 ioflag |= IO_SYNC;
644 if (fflag & FALTIO)
645 ioflag |= IO_ALTSEMANTICS;
646 if (fflag & FDIRECT)
647 ioflag |= IO_DIRECT;
648 vn_lock(vp, LK_SHARED | LK_RETRY);
649 uio->uio_offset = *offset;
650 count = uio->uio_resid;
651 error = VOP_READ(vp, uio, ioflag, cred);
652 if (flags & FOF_UPDATE_OFFSET)
653 *offset += count - uio->uio_resid;
654 VOP_UNLOCK(vp);
655 return (error);
656 }
657
658 /*
659 * File table vnode write routine.
660 */
661 static int
662 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
663 int flags)
664 {
665 struct vnode *vp = fp->f_vnode;
666 int error, ioflag, fflag;
667 size_t count;
668
669 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
670 fflag = fp->f_flag;
671 if (vp->v_type == VREG && (fflag & O_APPEND))
672 ioflag |= IO_APPEND;
673 if (fflag & FNONBLOCK)
674 ioflag |= IO_NDELAY;
675 if (fflag & FFSYNC ||
676 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
677 ioflag |= IO_SYNC;
678 else if (fflag & FDSYNC)
679 ioflag |= IO_DSYNC;
680 if (fflag & FALTIO)
681 ioflag |= IO_ALTSEMANTICS;
682 if (fflag & FDIRECT)
683 ioflag |= IO_DIRECT;
684 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
685 uio->uio_offset = *offset;
686 count = uio->uio_resid;
687
688 if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
689 goto out;
690
691 error = VOP_WRITE(vp, uio, ioflag, cred);
692
693 if (flags & FOF_UPDATE_OFFSET) {
694 if (ioflag & IO_APPEND) {
695 /*
696 * SUSv3 describes behaviour for count = 0 as following:
697 * "Before any action ... is taken, and if nbyte is zero
698 * and the file is a regular file, the write() function
699 * ... in the absence of errors ... shall return zero
700 * and have no other results."
701 */
702 if (count)
703 *offset = uio->uio_offset;
704 } else
705 *offset += count - uio->uio_resid;
706 }
707
708 out:
709 VOP_UNLOCK(vp);
710 return (error);
711 }
712
713 /*
714 * File table vnode stat routine.
715 */
716 static int
717 vn_statfile(file_t *fp, struct stat *sb)
718 {
719 struct vnode *vp = fp->f_vnode;
720 int error;
721
722 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
723 error = vn_stat(vp, sb);
724 VOP_UNLOCK(vp);
725 return error;
726 }
727
728 int
729 vn_stat(struct vnode *vp, struct stat *sb)
730 {
731 struct vattr va;
732 int error;
733 mode_t mode;
734
735 memset(&va, 0, sizeof(va));
736 error = VOP_GETATTR(vp, &va, kauth_cred_get());
737 if (error)
738 return (error);
739 /*
740 * Copy from vattr table
741 */
742 memset(sb, 0, sizeof(*sb));
743 sb->st_dev = va.va_fsid;
744 sb->st_ino = va.va_fileid;
745 mode = va.va_mode;
746 switch (vp->v_type) {
747 case VREG:
748 mode |= S_IFREG;
749 break;
750 case VDIR:
751 mode |= S_IFDIR;
752 break;
753 case VBLK:
754 mode |= S_IFBLK;
755 break;
756 case VCHR:
757 mode |= S_IFCHR;
758 break;
759 case VLNK:
760 mode |= S_IFLNK;
761 break;
762 case VSOCK:
763 mode |= S_IFSOCK;
764 break;
765 case VFIFO:
766 mode |= S_IFIFO;
767 break;
768 default:
769 return (EBADF);
770 }
771 sb->st_mode = mode;
772 sb->st_nlink = va.va_nlink;
773 sb->st_uid = va.va_uid;
774 sb->st_gid = va.va_gid;
775 sb->st_rdev = va.va_rdev;
776 sb->st_size = va.va_size;
777 sb->st_atimespec = va.va_atime;
778 sb->st_mtimespec = va.va_mtime;
779 sb->st_ctimespec = va.va_ctime;
780 sb->st_birthtimespec = va.va_birthtime;
781 sb->st_blksize = va.va_blocksize;
782 sb->st_flags = va.va_flags;
783 sb->st_gen = 0;
784 sb->st_blocks = va.va_bytes / S_BLKSIZE;
785 return (0);
786 }
787
788 /*
789 * File table vnode fcntl routine.
790 */
791 static int
792 vn_fcntl(file_t *fp, u_int com, void *data)
793 {
794 struct vnode *vp = fp->f_vnode;
795 int error;
796
797 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
798 return (error);
799 }
800
801 /*
802 * File table vnode ioctl routine.
803 */
804 static int
805 vn_ioctl(file_t *fp, u_long com, void *data)
806 {
807 struct vnode *vp = fp->f_vnode, *ovp;
808 struct vattr vattr;
809 int error;
810
811 switch (vp->v_type) {
812
813 case VREG:
814 case VDIR:
815 if (com == FIONREAD) {
816 vn_lock(vp, LK_SHARED | LK_RETRY);
817 error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
818 VOP_UNLOCK(vp);
819 if (error)
820 return (error);
821 *(int *)data = vattr.va_size - fp->f_offset;
822 return (0);
823 }
824 if ((com == FIONWRITE) || (com == FIONSPACE)) {
825 /*
826 * Files don't have send queues, so there never
827 * are any bytes in them, nor is there any
828 * open space in them.
829 */
830 *(int *)data = 0;
831 return (0);
832 }
833 if (com == FIOGETBMAP) {
834 daddr_t *block;
835
836 if (*(daddr_t *)data < 0)
837 return (EINVAL);
838 block = (daddr_t *)data;
839 vn_lock(vp, LK_SHARED | LK_RETRY);
840 error = VOP_BMAP(vp, *block, NULL, block, NULL);
841 VOP_UNLOCK(vp);
842 return error;
843 }
844 if (com == OFIOGETBMAP) {
845 daddr_t ibn, obn;
846
847 if (*(int32_t *)data < 0)
848 return (EINVAL);
849 ibn = (daddr_t)*(int32_t *)data;
850 vn_lock(vp, LK_SHARED | LK_RETRY);
851 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
852 VOP_UNLOCK(vp);
853 *(int32_t *)data = (int32_t)obn;
854 return error;
855 }
856 if (com == FIONBIO || com == FIOASYNC) /* XXX */
857 return (0); /* XXX */
858 /* FALLTHROUGH */
859 case VFIFO:
860 case VCHR:
861 case VBLK:
862 error = VOP_IOCTL(vp, com, data, fp->f_flag,
863 kauth_cred_get());
864 if (error == 0 && com == TIOCSCTTY) {
865 vref(vp);
866 mutex_enter(&proc_lock);
867 ovp = curproc->p_session->s_ttyvp;
868 curproc->p_session->s_ttyvp = vp;
869 mutex_exit(&proc_lock);
870 if (ovp != NULL)
871 vrele(ovp);
872 }
873 return (error);
874
875 default:
876 return (EPASSTHROUGH);
877 }
878 }
879
880 /*
881 * File table vnode poll routine.
882 */
883 static int
884 vn_poll(file_t *fp, int events)
885 {
886
887 return (VOP_POLL(fp->f_vnode, events));
888 }
889
890 /*
891 * File table vnode kqfilter routine.
892 */
893 int
894 vn_kqfilter(file_t *fp, struct knote *kn)
895 {
896
897 return (VOP_KQFILTER(fp->f_vnode, kn));
898 }
899
900 static int
901 vn_mmap(struct file *fp, off_t *offp, size_t size, int prot, int *flagsp,
902 int *advicep, struct uvm_object **uobjp, int *maxprotp)
903 {
904 struct uvm_object *uobj;
905 struct vnode *vp;
906 struct vattr va;
907 struct lwp *l;
908 vm_prot_t maxprot;
909 off_t off;
910 int error, flags;
911 bool needwritemap;
912
913 l = curlwp;
914
915 off = *offp;
916 flags = *flagsp;
917 maxprot = VM_PROT_EXECUTE;
918
919 vp = fp->f_vnode;
920 if (vp->v_type != VREG && vp->v_type != VCHR &&
921 vp->v_type != VBLK) {
922 /* only REG/CHR/BLK support mmap */
923 return ENODEV;
924 }
925 if (vp->v_type != VCHR && off < 0) {
926 return EINVAL;
927 }
928 if (vp->v_type != VCHR && (off_t)(off + size) < off) {
929 /* no offset wrapping */
930 return EOVERFLOW;
931 }
932
933 /* special case: catch SunOS style /dev/zero */
934 if (vp->v_type == VCHR &&
935 (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
936 *uobjp = NULL;
937 *maxprotp = VM_PROT_ALL;
938 return 0;
939 }
940
941 /*
942 * Old programs may not select a specific sharing type, so
943 * default to an appropriate one.
944 *
945 * XXX: how does MAP_ANON fit in the picture?
946 */
947 if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
948 #if defined(DEBUG)
949 struct proc *p = l->l_proc;
950 printf("WARNING: defaulted mmap() share type to "
951 "%s (pid %d command %s)\n", vp->v_type == VCHR ?
952 "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
953 p->p_comm);
954 #endif
955 if (vp->v_type == VCHR)
956 flags |= MAP_SHARED; /* for a device */
957 else
958 flags |= MAP_PRIVATE; /* for a file */
959 }
960
961 /*
962 * MAP_PRIVATE device mappings don't make sense (and aren't
963 * supported anyway). However, some programs rely on this,
964 * so just change it to MAP_SHARED.
965 */
966 if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
967 flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
968 }
969
970 /*
971 * now check protection
972 */
973
974 /* check read access */
975 if (fp->f_flag & FREAD)
976 maxprot |= VM_PROT_READ;
977 else if (prot & PROT_READ) {
978 return EACCES;
979 }
980
981 /* check write access, shared case first */
982 if (flags & MAP_SHARED) {
983 /*
984 * if the file is writable, only add PROT_WRITE to
985 * maxprot if the file is not immutable, append-only.
986 * otherwise, if we have asked for PROT_WRITE, return
987 * EPERM.
988 */
989 if (fp->f_flag & FWRITE) {
990 vn_lock(vp, LK_SHARED | LK_RETRY);
991 error = VOP_GETATTR(vp, &va, l->l_cred);
992 VOP_UNLOCK(vp);
993 if (error) {
994 return error;
995 }
996 if ((va.va_flags &
997 (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
998 maxprot |= VM_PROT_WRITE;
999 else if (prot & PROT_WRITE) {
1000 return EPERM;
1001 }
1002 } else if (prot & PROT_WRITE) {
1003 return EACCES;
1004 }
1005 } else {
1006 /* MAP_PRIVATE mappings can always write to */
1007 maxprot |= VM_PROT_WRITE;
1008 }
1009
1010 /*
1011 * Don't allow mmap for EXEC if the file system
1012 * is mounted NOEXEC.
1013 */
1014 if ((prot & PROT_EXEC) != 0 &&
1015 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
1016 return EACCES;
1017 }
1018
1019 if (vp->v_type != VCHR) {
1020 error = VOP_MMAP(vp, prot, curlwp->l_cred);
1021 if (error) {
1022 return error;
1023 }
1024 vref(vp);
1025 uobj = &vp->v_uobj;
1026
1027 /*
1028 * If the vnode is being mapped with PROT_EXEC,
1029 * then mark it as text.
1030 */
1031 if (prot & PROT_EXEC) {
1032 vn_markexec(vp);
1033 }
1034 } else {
1035 int i = maxprot;
1036
1037 /*
1038 * XXX Some devices don't like to be mapped with
1039 * XXX PROT_EXEC or PROT_WRITE, but we don't really
1040 * XXX have a better way of handling this, right now
1041 */
1042 do {
1043 uobj = udv_attach(vp->v_rdev,
1044 (flags & MAP_SHARED) ? i :
1045 (i & ~VM_PROT_WRITE), off, size);
1046 i--;
1047 } while ((uobj == NULL) && (i > 0));
1048 if (uobj == NULL) {
1049 return EINVAL;
1050 }
1051 *advicep = UVM_ADV_RANDOM;
1052 }
1053
1054 /*
1055 * Set vnode flags to indicate the new kinds of mapping.
1056 * We take the vnode lock in exclusive mode here to serialize
1057 * with direct I/O.
1058 *
1059 * Safe to check for these flag values without a lock, as
1060 * long as a reference to the vnode is held.
1061 */
1062 needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
1063 (flags & MAP_SHARED) != 0 &&
1064 (maxprot & VM_PROT_WRITE) != 0;
1065 if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
1066 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1067 vp->v_vflag |= VV_MAPPED;
1068 if (needwritemap) {
1069 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
1070 mutex_enter(vp->v_interlock);
1071 vp->v_iflag |= VI_WRMAP;
1072 mutex_exit(vp->v_interlock);
1073 rw_exit(vp->v_uobj.vmobjlock);
1074 }
1075 VOP_UNLOCK(vp);
1076 }
1077
1078 #if NVERIEXEC > 0
1079
1080 /*
1081 * Check if the file can be executed indirectly.
1082 *
1083 * XXX: This gives false warnings about "Incorrect access type"
1084 * XXX: if the mapping is not executable. Harmless, but will be
1085 * XXX: fixed as part of other changes.
1086 */
1087 if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
1088 NULL)) {
1089
1090 /*
1091 * Don't allow executable mappings if we can't
1092 * indirectly execute the file.
1093 */
1094 if (prot & VM_PROT_EXECUTE) {
1095 return EPERM;
1096 }
1097
1098 /*
1099 * Strip the executable bit from 'maxprot' to make sure
1100 * it can't be made executable later.
1101 */
1102 maxprot &= ~VM_PROT_EXECUTE;
1103 }
1104 #endif /* NVERIEXEC > 0 */
1105
1106 *uobjp = uobj;
1107 *maxprotp = maxprot;
1108 *flagsp = flags;
1109
1110 return 0;
1111 }
1112
1113
1114
1115 /*
1116 * Check that the vnode is still valid, and if so
1117 * acquire requested lock.
1118 */
1119 int
1120 vn_lock(struct vnode *vp, int flags)
1121 {
1122 struct lwp *l;
1123 int error;
1124
1125 #if 0
1126 KASSERT(vrefcnt(vp) > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
1127 #endif
1128 KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
1129 LK_UPGRADE|LK_DOWNGRADE)) == 0);
1130 KASSERT((flags & LK_NOWAIT) != 0 || !mutex_owned(vp->v_interlock));
1131
1132 #ifdef DIAGNOSTIC
1133 if (wapbl_vphaswapbl(vp))
1134 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
1135 #endif
1136
1137 /* Get a more useful report for lockstat. */
1138 l = curlwp;
1139 KASSERT(l->l_rwcallsite == 0);
1140 l->l_rwcallsite = (uintptr_t)__builtin_return_address(0);
1141
1142 error = VOP_LOCK(vp, flags);
1143 if ((flags & LK_RETRY) != 0 && error == ENOENT)
1144 error = VOP_LOCK(vp, flags);
1145
1146 l->l_rwcallsite = 0;
1147
1148 KASSERT((flags & LK_RETRY) == 0 || (flags & LK_NOWAIT) != 0 ||
1149 error == 0);
1150
1151 return error;
1152 }
1153
1154 /*
1155 * File table vnode close routine.
1156 */
1157 static int
1158 vn_closefile(file_t *fp)
1159 {
1160
1161 return vn_close(fp->f_vnode, fp->f_flag, fp->f_cred);
1162 }
1163
1164 /*
1165 * Simplified in-kernel wrapper calls for extended attribute access.
1166 * Both calls pass in a NULL credential, authorizing a "kernel" access.
1167 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1168 */
1169 int
1170 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1171 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
1172 {
1173 struct uio auio;
1174 struct iovec aiov;
1175 int error;
1176
1177 aiov.iov_len = *buflen;
1178 aiov.iov_base = bf;
1179
1180 auio.uio_iov = &aiov;
1181 auio.uio_iovcnt = 1;
1182 auio.uio_rw = UIO_READ;
1183 auio.uio_offset = 0;
1184 auio.uio_resid = *buflen;
1185 UIO_SETUP_SYSSPACE(&auio);
1186
1187 if ((ioflg & IO_NODELOCKED) == 0)
1188 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1189
1190 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL,
1191 NOCRED);
1192
1193 if ((ioflg & IO_NODELOCKED) == 0)
1194 VOP_UNLOCK(vp);
1195
1196 if (error == 0)
1197 *buflen = *buflen - auio.uio_resid;
1198
1199 return (error);
1200 }
1201
1202 /*
1203 * XXX Failure mode if partially written?
1204 */
1205 int
1206 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1207 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
1208 {
1209 struct uio auio;
1210 struct iovec aiov;
1211 int error;
1212
1213 aiov.iov_len = buflen;
1214 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
1215
1216 auio.uio_iov = &aiov;
1217 auio.uio_iovcnt = 1;
1218 auio.uio_rw = UIO_WRITE;
1219 auio.uio_offset = 0;
1220 auio.uio_resid = buflen;
1221 UIO_SETUP_SYSSPACE(&auio);
1222
1223 if ((ioflg & IO_NODELOCKED) == 0) {
1224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1225 }
1226
1227 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NOCRED);
1228
1229 if ((ioflg & IO_NODELOCKED) == 0) {
1230 VOP_UNLOCK(vp);
1231 }
1232
1233 return (error);
1234 }
1235
1236 int
1237 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1238 const char *attrname, struct lwp *l)
1239 {
1240 int error;
1241
1242 if ((ioflg & IO_NODELOCKED) == 0) {
1243 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1244 }
1245
1246 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NOCRED);
1247 if (error == EOPNOTSUPP)
1248 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1249 NOCRED);
1250
1251 if ((ioflg & IO_NODELOCKED) == 0) {
1252 VOP_UNLOCK(vp);
1253 }
1254
1255 return (error);
1256 }
1257
1258 int
1259 vn_fifo_bypass(void *v)
1260 {
1261 struct vop_generic_args *ap = v;
1262
1263 return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
1264 }
1265
1266 /*
1267 * Open block device by device number
1268 */
1269 int
1270 vn_bdev_open(dev_t dev, struct vnode **vpp, struct lwp *l)
1271 {
1272 int error;
1273
1274 if ((error = bdevvp(dev, vpp)) != 0)
1275 return error;
1276
1277 if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
1278 vrele(*vpp);
1279 return error;
1280 }
1281 mutex_enter((*vpp)->v_interlock);
1282 (*vpp)->v_writecount++;
1283 mutex_exit((*vpp)->v_interlock);
1284
1285 return 0;
1286 }
1287
1288 /*
1289 * Lookup the provided name in the filesystem. If the file exists,
1290 * is a valid block device, and isn't being used by anyone else,
1291 * set *vpp to the file's vnode.
1292 */
1293 int
1294 vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l)
1295 {
1296 struct vnode *vp;
1297 dev_t dev;
1298 enum vtype vt;
1299 int error;
1300
1301 error = vn_open(NULL, pb, 0, FREAD | FWRITE, 0, &vp, NULL, NULL);
1302 if (error != 0)
1303 return error;
1304
1305 dev = vp->v_rdev;
1306 vt = vp->v_type;
1307
1308 VOP_UNLOCK(vp);
1309 (void) vn_close(vp, FREAD | FWRITE, l->l_cred);
1310
1311 if (vt != VBLK)
1312 return ENOTBLK;
1313
1314 return vn_bdev_open(dev, vpp, l);
1315 }
1316