vfs_vnops.c revision 1.156.2.4 1 /* $NetBSD: vfs_vnops.c,v 1.156.2.4 2010/08/11 22:54:44 yamt 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.156.2.4 2010/08/11 22:54:44 yamt 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
93 #include <miscfs/specfs/specdev.h>
94 #include <miscfs/fifofs/fifo.h>
95
96 #include <uvm/uvm_extern.h>
97 #include <uvm/uvm_readahead.h>
98
99 #ifdef UNION
100 #include <fs/union/union.h>
101 #endif
102
103 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
104
105 #include <sys/verified_exec.h>
106
107 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
108 kauth_cred_t cred, int flags);
109 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
110 kauth_cred_t cred, int flags);
111 static int vn_closefile(file_t *fp);
112 static int vn_poll(file_t *fp, int events);
113 static int vn_fcntl(file_t *fp, u_int com, void *data);
114 static int vn_statfile(file_t *fp, struct stat *sb);
115 static int vn_ioctl(file_t *fp, u_long com, void *data);
116
117 const struct fileops vnops = {
118 .fo_read = vn_read,
119 .fo_write = vn_write,
120 .fo_ioctl = vn_ioctl,
121 .fo_fcntl = vn_fcntl,
122 .fo_poll = vn_poll,
123 .fo_stat = vn_statfile,
124 .fo_close = vn_closefile,
125 .fo_kqfilter = vn_kqfilter,
126 .fo_restart = fnullop_restart,
127 };
128
129 /*
130 * Common code for vnode open operations.
131 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
132 */
133 int
134 vn_open(struct nameidata *ndp, int fmode, int cmode)
135 {
136 struct vnode *vp;
137 struct lwp *l = curlwp;
138 kauth_cred_t cred = l->l_cred;
139 struct vattr va;
140 int error;
141 char *path;
142
143 ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
144
145 if (fmode & O_CREAT) {
146 ndp->ni_cnd.cn_nameiop = CREATE;
147 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
148 if ((fmode & O_EXCL) == 0 &&
149 ((fmode & O_NOFOLLOW) == 0))
150 ndp->ni_cnd.cn_flags |= FOLLOW;
151 } else {
152 ndp->ni_cnd.cn_nameiop = LOOKUP;
153 ndp->ni_cnd.cn_flags |= LOCKLEAF;
154 if ((fmode & O_NOFOLLOW) == 0)
155 ndp->ni_cnd.cn_flags |= FOLLOW;
156 }
157
158 VERIEXEC_PATH_GET(ndp->ni_dirp, ndp->ni_segflg, ndp->ni_dirp, path);
159
160 error = namei(ndp);
161 if (error)
162 goto out;
163
164 vp = ndp->ni_vp;
165
166 #if NVERIEXEC > 0
167 error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode);
168 if (error)
169 goto bad;
170 #endif /* NVERIEXEC > 0 */
171
172 if (fmode & O_CREAT) {
173 if (ndp->ni_vp == NULL) {
174 vattr_null(&va);
175 va.va_type = VREG;
176 va.va_mode = cmode;
177 if (fmode & O_EXCL)
178 va.va_vaflags |= VA_EXCLUSIVE;
179 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
180 &ndp->ni_cnd, &va);
181 if (error)
182 goto out;
183 fmode &= ~O_TRUNC;
184 vp = ndp->ni_vp;
185 } else {
186 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
187 if (ndp->ni_dvp == ndp->ni_vp)
188 vrele(ndp->ni_dvp);
189 else
190 vput(ndp->ni_dvp);
191 ndp->ni_dvp = NULL;
192 vp = ndp->ni_vp;
193 if (fmode & O_EXCL) {
194 error = EEXIST;
195 goto bad;
196 }
197 fmode &= ~O_CREAT;
198 }
199 } else {
200 vp = ndp->ni_vp;
201 }
202 if (vp->v_type == VSOCK) {
203 error = EOPNOTSUPP;
204 goto bad;
205 }
206 if (ndp->ni_vp->v_type == VLNK) {
207 error = EFTYPE;
208 goto bad;
209 }
210
211 if ((fmode & O_CREAT) == 0) {
212 error = vn_openchk(vp, cred, fmode);
213 if (error != 0)
214 goto bad;
215 }
216
217 if (fmode & O_TRUNC) {
218 VOP_UNLOCK(vp); /* XXX */
219
220 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
221 vattr_null(&va);
222 va.va_size = 0;
223 error = VOP_SETATTR(vp, &va, cred);
224 if (error != 0)
225 goto bad;
226 }
227 if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
228 goto bad;
229 if (fmode & FWRITE) {
230 mutex_enter(&vp->v_interlock);
231 vp->v_writecount++;
232 mutex_exit(&vp->v_interlock);
233 }
234
235 bad:
236 if (error)
237 vput(vp);
238 out:
239 VERIEXEC_PATH_PUT(path);
240 return (error);
241 }
242
243 /*
244 * Check for write permissions on the specified vnode.
245 * Prototype text segments cannot be written.
246 */
247 int
248 vn_writechk(struct vnode *vp)
249 {
250
251 /*
252 * If the vnode is in use as a process's text,
253 * we can't allow writing.
254 */
255 if (vp->v_iflag & VI_TEXT)
256 return (ETXTBSY);
257 return (0);
258 }
259
260 int
261 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
262 {
263 int permbits = 0;
264 int error;
265
266 if ((fflags & FREAD) != 0) {
267 permbits = VREAD;
268 }
269 if ((fflags & (FWRITE | O_TRUNC)) != 0) {
270 permbits |= VWRITE;
271 if (vp->v_type == VDIR) {
272 error = EISDIR;
273 goto bad;
274 }
275 error = vn_writechk(vp);
276 if (error != 0)
277 goto bad;
278 }
279 error = VOP_ACCESS(vp, permbits, cred);
280 bad:
281 return error;
282 }
283
284 /*
285 * Mark a vnode as having executable mappings.
286 */
287 void
288 vn_markexec(struct vnode *vp)
289 {
290
291 if ((vp->v_iflag & VI_EXECMAP) != 0) {
292 /* Safe unlocked, as long as caller holds a reference. */
293 return;
294 }
295
296 mutex_enter(&vp->v_interlock);
297 if ((vp->v_iflag & VI_EXECMAP) == 0) {
298 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
299 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
300 vp->v_iflag |= VI_EXECMAP;
301 }
302 mutex_exit(&vp->v_interlock);
303 }
304
305 /*
306 * Mark a vnode as being the text of a process.
307 * Fail if the vnode is currently writable.
308 */
309 int
310 vn_marktext(struct vnode *vp)
311 {
312
313 if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
314 /* Safe unlocked, as long as caller holds a reference. */
315 return (0);
316 }
317
318 mutex_enter(&vp->v_interlock);
319 if (vp->v_writecount != 0) {
320 KASSERT((vp->v_iflag & VI_TEXT) == 0);
321 mutex_exit(&vp->v_interlock);
322 return (ETXTBSY);
323 }
324 if ((vp->v_iflag & VI_EXECMAP) == 0) {
325 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
326 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
327 }
328 vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
329 mutex_exit(&vp->v_interlock);
330 return (0);
331 }
332
333 /*
334 * Vnode close call
335 *
336 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
337 */
338 int
339 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
340 {
341 int error;
342
343 if (flags & FWRITE) {
344 mutex_enter(&vp->v_interlock);
345 vp->v_writecount--;
346 mutex_exit(&vp->v_interlock);
347 }
348 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
349 error = VOP_CLOSE(vp, flags, cred);
350 vput(vp);
351 return (error);
352 }
353
354 static int
355 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
356 {
357 struct lwp *l = curlwp;
358 off_t testoff;
359
360 if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
361 return 0;
362
363 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
364 if (ioflag & IO_APPEND)
365 testoff = vp->v_size;
366 else
367 testoff = uio->uio_offset;
368
369 if (testoff + uio->uio_resid >
370 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
371 mutex_enter(proc_lock);
372 psignal(l->l_proc, SIGXFSZ);
373 mutex_exit(proc_lock);
374 return EFBIG;
375 }
376
377 return 0;
378 }
379
380 /*
381 * Package up an I/O request on a vnode into a uio and do it.
382 */
383 int
384 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
385 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
386 struct lwp *l)
387 {
388 struct uio auio;
389 struct iovec aiov;
390 int error;
391
392 if ((ioflg & IO_NODELOCKED) == 0) {
393 if (rw == UIO_READ) {
394 vn_lock(vp, LK_SHARED | LK_RETRY);
395 } else /* UIO_WRITE */ {
396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
397 }
398 }
399 auio.uio_iov = &aiov;
400 auio.uio_iovcnt = 1;
401 aiov.iov_base = base;
402 aiov.iov_len = len;
403 auio.uio_resid = len;
404 auio.uio_offset = offset;
405 auio.uio_rw = rw;
406 if (segflg == UIO_SYSSPACE) {
407 UIO_SETUP_SYSSPACE(&auio);
408 } else {
409 auio.uio_vmspace = l->l_proc->p_vmspace;
410 }
411
412 if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
413 goto out;
414
415 if (rw == UIO_READ) {
416 error = VOP_READ(vp, &auio, ioflg, cred);
417 } else {
418 error = VOP_WRITE(vp, &auio, ioflg, cred);
419 }
420
421 if (aresid)
422 *aresid = auio.uio_resid;
423 else
424 if (auio.uio_resid && error == 0)
425 error = EIO;
426
427 out:
428 if ((ioflg & IO_NODELOCKED) == 0) {
429 VOP_UNLOCK(vp);
430 }
431 return (error);
432 }
433
434 int
435 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
436 struct lwp *l, off_t **cookies, int *ncookies)
437 {
438 struct vnode *vp = (struct vnode *)fp->f_data;
439 struct iovec aiov;
440 struct uio auio;
441 int error, eofflag;
442
443 /* Limit the size on any kernel buffers used by VOP_READDIR */
444 count = min(MAXBSIZE, count);
445
446 unionread:
447 if (vp->v_type != VDIR)
448 return (EINVAL);
449 aiov.iov_base = bf;
450 aiov.iov_len = count;
451 auio.uio_iov = &aiov;
452 auio.uio_iovcnt = 1;
453 auio.uio_rw = UIO_READ;
454 if (segflg == UIO_SYSSPACE) {
455 UIO_SETUP_SYSSPACE(&auio);
456 } else {
457 KASSERT(l == curlwp);
458 auio.uio_vmspace = l->l_proc->p_vmspace;
459 }
460 auio.uio_resid = count;
461 vn_lock(vp, LK_SHARED | LK_RETRY);
462 auio.uio_offset = fp->f_offset;
463 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
464 ncookies);
465 mutex_enter(&fp->f_lock);
466 fp->f_offset = auio.uio_offset;
467 mutex_exit(&fp->f_lock);
468 VOP_UNLOCK(vp);
469 if (error)
470 return (error);
471
472 if (count == auio.uio_resid && vn_union_readdir_hook) {
473 struct vnode *ovp = vp;
474
475 error = (*vn_union_readdir_hook)(&vp, fp, l);
476 if (error)
477 return (error);
478 if (vp != ovp)
479 goto unionread;
480 }
481
482 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
483 (vp->v_mount->mnt_flag & MNT_UNION)) {
484 struct vnode *tvp = vp;
485 vp = vp->v_mount->mnt_vnodecovered;
486 vref(vp);
487 mutex_enter(&fp->f_lock);
488 fp->f_data = vp;
489 fp->f_offset = 0;
490 mutex_exit(&fp->f_lock);
491 vrele(tvp);
492 goto unionread;
493 }
494 *done = count - auio.uio_resid;
495 return error;
496 }
497
498 /*
499 * File table vnode read routine.
500 */
501 static int
502 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
503 int flags)
504 {
505 struct vnode *vp = (struct vnode *)fp->f_data;
506 int count, error, ioflag, fflag;
507
508 ioflag = IO_ADV_ENCODE(fp->f_advice);
509 fflag = fp->f_flag;
510 if (fflag & FNONBLOCK)
511 ioflag |= IO_NDELAY;
512 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
513 ioflag |= IO_SYNC;
514 if (fflag & FALTIO)
515 ioflag |= IO_ALTSEMANTICS;
516 if (fflag & FDIRECT)
517 ioflag |= IO_DIRECT;
518 vn_lock(vp, LK_SHARED | LK_RETRY);
519 uio->uio_offset = *offset;
520 count = uio->uio_resid;
521 error = VOP_READ(vp, uio, ioflag, cred);
522 if (flags & FOF_UPDATE_OFFSET)
523 *offset += count - uio->uio_resid;
524 VOP_UNLOCK(vp);
525 return (error);
526 }
527
528 /*
529 * File table vnode write routine.
530 */
531 static int
532 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
533 int flags)
534 {
535 struct vnode *vp = (struct vnode *)fp->f_data;
536 int count, error, ioflag, fflag;
537
538 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
539 fflag = fp->f_flag;
540 if (vp->v_type == VREG && (fflag & O_APPEND))
541 ioflag |= IO_APPEND;
542 if (fflag & FNONBLOCK)
543 ioflag |= IO_NDELAY;
544 if (fflag & FFSYNC ||
545 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
546 ioflag |= IO_SYNC;
547 else if (fflag & FDSYNC)
548 ioflag |= IO_DSYNC;
549 if (fflag & FALTIO)
550 ioflag |= IO_ALTSEMANTICS;
551 if (fflag & FDIRECT)
552 ioflag |= IO_DIRECT;
553 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
554 uio->uio_offset = *offset;
555 count = uio->uio_resid;
556
557 if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
558 goto out;
559
560 error = VOP_WRITE(vp, uio, ioflag, cred);
561
562 if (flags & FOF_UPDATE_OFFSET) {
563 if (ioflag & IO_APPEND) {
564 /*
565 * SUSv3 describes behaviour for count = 0 as following:
566 * "Before any action ... is taken, and if nbyte is zero
567 * and the file is a regular file, the write() function
568 * ... in the absence of errors ... shall return zero
569 * and have no other results."
570 */
571 if (count)
572 *offset = uio->uio_offset;
573 } else
574 *offset += count - uio->uio_resid;
575 }
576
577 out:
578 VOP_UNLOCK(vp);
579 return (error);
580 }
581
582 /*
583 * File table vnode stat routine.
584 */
585 static int
586 vn_statfile(file_t *fp, struct stat *sb)
587 {
588 struct vnode *vp = fp->f_data;
589 int error;
590
591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
592 error = vn_stat(vp, sb);
593 VOP_UNLOCK(vp);
594 return error;
595 }
596
597 int
598 vn_stat(struct vnode *vp, struct stat *sb)
599 {
600 struct vattr va;
601 int error;
602 mode_t mode;
603
604 memset(&va, 0, sizeof(va));
605 error = VOP_GETATTR(vp, &va, kauth_cred_get());
606 if (error)
607 return (error);
608 /*
609 * Copy from vattr table
610 */
611 sb->st_dev = va.va_fsid;
612 sb->st_ino = va.va_fileid;
613 mode = va.va_mode;
614 switch (vp->v_type) {
615 case VREG:
616 mode |= S_IFREG;
617 break;
618 case VDIR:
619 mode |= S_IFDIR;
620 break;
621 case VBLK:
622 mode |= S_IFBLK;
623 break;
624 case VCHR:
625 mode |= S_IFCHR;
626 break;
627 case VLNK:
628 mode |= S_IFLNK;
629 break;
630 case VSOCK:
631 mode |= S_IFSOCK;
632 break;
633 case VFIFO:
634 mode |= S_IFIFO;
635 break;
636 default:
637 return (EBADF);
638 };
639 sb->st_mode = mode;
640 sb->st_nlink = va.va_nlink;
641 sb->st_uid = va.va_uid;
642 sb->st_gid = va.va_gid;
643 sb->st_rdev = va.va_rdev;
644 sb->st_size = va.va_size;
645 sb->st_atimespec = va.va_atime;
646 sb->st_mtimespec = va.va_mtime;
647 sb->st_ctimespec = va.va_ctime;
648 sb->st_birthtimespec = va.va_birthtime;
649 sb->st_blksize = va.va_blocksize;
650 sb->st_flags = va.va_flags;
651 sb->st_gen = 0;
652 sb->st_blocks = va.va_bytes / S_BLKSIZE;
653 memset(sb->st_spare, 0, sizeof(sb->st_spare));
654 return (0);
655 }
656
657 /*
658 * File table vnode fcntl routine.
659 */
660 static int
661 vn_fcntl(file_t *fp, u_int com, void *data)
662 {
663 struct vnode *vp = fp->f_data;
664 int error;
665
666 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
667 return (error);
668 }
669
670 /*
671 * File table vnode ioctl routine.
672 */
673 static int
674 vn_ioctl(file_t *fp, u_long com, void *data)
675 {
676 struct vnode *vp = fp->f_data, *ovp;
677 struct vattr vattr;
678 int error;
679
680 switch (vp->v_type) {
681
682 case VREG:
683 case VDIR:
684 if (com == FIONREAD) {
685 error = VOP_GETATTR(vp, &vattr,
686 kauth_cred_get());
687 if (error)
688 return (error);
689 *(int *)data = vattr.va_size - fp->f_offset;
690 return (0);
691 }
692 if ((com == FIONWRITE) || (com == FIONSPACE)) {
693 /*
694 * Files don't have send queues, so there never
695 * are any bytes in them, nor is there any
696 * open space in them.
697 */
698 *(int *)data = 0;
699 return (0);
700 }
701 if (com == FIOGETBMAP) {
702 daddr_t *block;
703
704 if (*(daddr_t *)data < 0)
705 return (EINVAL);
706 block = (daddr_t *)data;
707 return (VOP_BMAP(vp, *block, NULL, block, NULL));
708 }
709 if (com == OFIOGETBMAP) {
710 daddr_t ibn, obn;
711
712 if (*(int32_t *)data < 0)
713 return (EINVAL);
714 ibn = (daddr_t)*(int32_t *)data;
715 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
716 *(int32_t *)data = (int32_t)obn;
717 return error;
718 }
719 if (com == FIONBIO || com == FIOASYNC) /* XXX */
720 return (0); /* XXX */
721 /* fall into ... */
722 case VFIFO:
723 case VCHR:
724 case VBLK:
725 error = VOP_IOCTL(vp, com, data, fp->f_flag,
726 kauth_cred_get());
727 if (error == 0 && com == TIOCSCTTY) {
728 vref(vp);
729 mutex_enter(proc_lock);
730 ovp = curproc->p_session->s_ttyvp;
731 curproc->p_session->s_ttyvp = vp;
732 mutex_exit(proc_lock);
733 if (ovp != NULL)
734 vrele(ovp);
735 }
736 return (error);
737
738 default:
739 return (EPASSTHROUGH);
740 }
741 }
742
743 /*
744 * File table vnode poll routine.
745 */
746 static int
747 vn_poll(file_t *fp, int events)
748 {
749
750 return (VOP_POLL(fp->f_data, events));
751 }
752
753 /*
754 * File table vnode kqfilter routine.
755 */
756 int
757 vn_kqfilter(file_t *fp, struct knote *kn)
758 {
759
760 return (VOP_KQFILTER(fp->f_data, kn));
761 }
762
763 /*
764 * Check that the vnode is still valid, and if so
765 * acquire requested lock.
766 */
767 int
768 vn_lock(struct vnode *vp, int flags)
769 {
770 int error;
771
772 #if 0
773 KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
774 #endif
775 KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) == 0);
776 KASSERT(!mutex_owned(&vp->v_interlock));
777
778 #ifdef DIAGNOSTIC
779 if (wapbl_vphaswapbl(vp))
780 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
781 #endif
782
783 do {
784 /*
785 * XXX PR 37706 forced unmount of file systems is unsafe.
786 * Race between vclean() and this the remaining problem.
787 */
788 mutex_enter(&vp->v_interlock);
789 if (vp->v_iflag & VI_XLOCK) {
790 if (flags & LK_NOWAIT) {
791 mutex_exit(&vp->v_interlock);
792 return EBUSY;
793 }
794 vwait(vp, VI_XLOCK);
795 mutex_exit(&vp->v_interlock);
796 error = ENOENT;
797 } else {
798 mutex_exit(&vp->v_interlock);
799 error = VOP_LOCK(vp, (flags & ~LK_RETRY));
800 if (error == 0 || error == EDEADLK || error == EBUSY)
801 return (error);
802 }
803 } while (flags & LK_RETRY);
804 return (error);
805 }
806
807 /*
808 * File table vnode close routine.
809 */
810 static int
811 vn_closefile(file_t *fp)
812 {
813
814 return vn_close(fp->f_data, fp->f_flag, fp->f_cred);
815 }
816
817 /*
818 * Simplified in-kernel wrapper calls for extended attribute access.
819 * Both calls pass in a NULL credential, authorizing a "kernel" access.
820 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
821 */
822 int
823 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
824 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
825 {
826 struct uio auio;
827 struct iovec aiov;
828 int error;
829
830 aiov.iov_len = *buflen;
831 aiov.iov_base = bf;
832
833 auio.uio_iov = &aiov;
834 auio.uio_iovcnt = 1;
835 auio.uio_rw = UIO_READ;
836 auio.uio_offset = 0;
837 auio.uio_resid = *buflen;
838 UIO_SETUP_SYSSPACE(&auio);
839
840 if ((ioflg & IO_NODELOCKED) == 0)
841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
842
843 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
844
845 if ((ioflg & IO_NODELOCKED) == 0)
846 VOP_UNLOCK(vp);
847
848 if (error == 0)
849 *buflen = *buflen - auio.uio_resid;
850
851 return (error);
852 }
853
854 /*
855 * XXX Failure mode if partially written?
856 */
857 int
858 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
859 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
860 {
861 struct uio auio;
862 struct iovec aiov;
863 int error;
864
865 aiov.iov_len = buflen;
866 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
867
868 auio.uio_iov = &aiov;
869 auio.uio_iovcnt = 1;
870 auio.uio_rw = UIO_WRITE;
871 auio.uio_offset = 0;
872 auio.uio_resid = buflen;
873 UIO_SETUP_SYSSPACE(&auio);
874
875 if ((ioflg & IO_NODELOCKED) == 0) {
876 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
877 }
878
879 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
880
881 if ((ioflg & IO_NODELOCKED) == 0) {
882 VOP_UNLOCK(vp);
883 }
884
885 return (error);
886 }
887
888 int
889 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
890 const char *attrname, struct lwp *l)
891 {
892 int error;
893
894 if ((ioflg & IO_NODELOCKED) == 0) {
895 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
896 }
897
898 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
899 if (error == EOPNOTSUPP)
900 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
901
902 if ((ioflg & IO_NODELOCKED) == 0) {
903 VOP_UNLOCK(vp);
904 }
905
906 return (error);
907 }
908
909 void
910 vn_ra_allocctx(struct vnode *vp)
911 {
912 struct uvm_ractx *ra = NULL;
913
914 KASSERT(mutex_owned(&vp->v_interlock));
915
916 if (vp->v_type != VREG) {
917 return;
918 }
919 if (vp->v_ractx != NULL) {
920 return;
921 }
922 if (vp->v_ractx == NULL) {
923 mutex_exit(&vp->v_interlock);
924 ra = uvm_ra_allocctx();
925 mutex_enter(&vp->v_interlock);
926 if (ra != NULL && vp->v_ractx == NULL) {
927 vp->v_ractx = ra;
928 ra = NULL;
929 }
930 }
931 if (ra != NULL) {
932 uvm_ra_freectx(ra);
933 }
934 }
935
936 int
937 vn_fifo_bypass(void *v)
938 {
939 struct vop_generic_args *ap = v;
940
941 return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
942 }
943