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