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