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