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