vfs_vnops.c revision 1.106.4.3 1 /* $NetBSD: vfs_vnops.c,v 1.106.4.3 2006/05/06 23:31:31 christos Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.106.4.3 2006/05/06 23:31:31 christos Exp $");
41
42 #include "opt_verified_exec.h"
43
44 #include "fs_union.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/buf.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/vnode.h>
57 #include <sys/ioctl.h>
58 #include <sys/tty.h>
59 #include <sys/poll.h>
60 #include <sys/kauth.h>
61
62 #include <miscfs/specfs/specdev.h>
63
64 #include <uvm/uvm_extern.h>
65 #include <uvm/uvm_readahead.h>
66
67 #ifdef UNION
68 #include <fs/union/union.h>
69 #endif
70
71 #if defined(LKM) || defined(UNION)
72 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
73 #endif
74
75 #ifdef VERIFIED_EXEC
76 #include <sys/verified_exec.h>
77 #endif
78
79 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
80 kauth_cred_t cred, int flags);
81 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
82 kauth_cred_t cred, int flags);
83 static int vn_closefile(struct file *fp, struct lwp *l);
84 static int vn_poll(struct file *fp, int events, struct lwp *l);
85 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
86 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
87 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
88
89 const struct fileops vnops = {
90 vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
91 vn_statfile, vn_closefile, vn_kqfilter
92 };
93
94 /*
95 * Common code for vnode open operations.
96 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
97 */
98 int
99 vn_open(struct nameidata *ndp, int fmode, int cmode)
100 {
101 struct vnode *vp;
102 struct mount *mp;
103 struct lwp *l = ndp->ni_cnd.cn_lwp;
104 kauth_cred_t cred = l->l_proc->p_cred;
105 struct vattr va;
106 int error;
107 #ifdef VERIFIED_EXEC
108 struct veriexec_hash_entry *vhe = NULL;
109 char pathbuf[MAXPATHLEN];
110 size_t pathlen;
111 int (*copyfun)(const void *, void *, size_t, size_t *) =
112 ndp->ni_segflg == UIO_SYSSPACE ? copystr : copyinstr;
113 #endif /* VERIFIED_EXEC */
114
115 #ifdef VERIFIED_EXEC
116 error = (*copyfun)(ndp->ni_dirp, pathbuf, sizeof(pathbuf), &pathlen);
117 if (error) {
118 if (veriexec_verbose >= 1)
119 printf("veriexec: Can't copy path. (error=%d)\n",
120 error);
121
122 return (error);
123 }
124 #endif /* VERIFIED_EXEC */
125
126 restart:
127 if (fmode & O_CREAT) {
128 ndp->ni_cnd.cn_nameiop = CREATE;
129 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
130 if ((fmode & O_EXCL) == 0 &&
131 ((fmode & O_NOFOLLOW) == 0))
132 ndp->ni_cnd.cn_flags |= FOLLOW;
133 if ((error = namei(ndp)) != 0)
134 return (error);
135 if (ndp->ni_vp == NULL) {
136 #ifdef VERIFIED_EXEC
137 /* Lockdown mode: Prevent creation of new files. */
138 if (veriexec_strict >= 3) {
139 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
140
141 printf("Veriexec: vn_open: Preventing "
142 "new file creation in %s.\n",
143 pathbuf);
144
145 vp = ndp->ni_dvp;
146 error = EPERM;
147 goto bad;
148 }
149 #endif /* VERIFIED_EXEC */
150
151 VATTR_NULL(&va);
152 va.va_type = VREG;
153 va.va_mode = cmode;
154 if (fmode & O_EXCL)
155 va.va_vaflags |= VA_EXCLUSIVE;
156 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
157 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
158 vput(ndp->ni_dvp);
159 if ((error = vn_start_write(NULL, &mp,
160 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
161 return (error);
162 goto restart;
163 }
164 VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
165 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
166 &ndp->ni_cnd, &va);
167 vn_finished_write(mp, 0);
168 if (error)
169 return (error);
170 fmode &= ~O_TRUNC;
171 vp = ndp->ni_vp;
172 } else {
173 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
174 if (ndp->ni_dvp == ndp->ni_vp)
175 vrele(ndp->ni_dvp);
176 else
177 vput(ndp->ni_dvp);
178 ndp->ni_dvp = NULL;
179 vp = ndp->ni_vp;
180 if (fmode & O_EXCL) {
181 error = EEXIST;
182 goto bad;
183 }
184 fmode &= ~O_CREAT;
185 }
186 } else {
187 ndp->ni_cnd.cn_nameiop = LOOKUP;
188 ndp->ni_cnd.cn_flags = LOCKLEAF;
189 if ((fmode & O_NOFOLLOW) == 0)
190 ndp->ni_cnd.cn_flags |= FOLLOW;
191 if ((error = namei(ndp)) != 0)
192 return (error);
193 vp = ndp->ni_vp;
194 }
195 if (vp->v_type == VSOCK) {
196 error = EOPNOTSUPP;
197 goto bad;
198 }
199 if (ndp->ni_vp->v_type == VLNK) {
200 error = EFTYPE;
201 goto bad;
202 }
203
204 #ifdef VERIFIED_EXEC
205 if ((error = VOP_GETATTR(vp, &va, cred, l)) != 0)
206 goto bad;
207 #endif
208
209 if ((fmode & O_CREAT) == 0) {
210 #ifdef VERIFIED_EXEC
211 if ((error = veriexec_verify(l, vp, &va, pathbuf,
212 VERIEXEC_FILE, &vhe)) != 0)
213 goto bad;
214 #endif
215
216 if (fmode & FREAD) {
217 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
218 goto bad;
219 }
220
221 if (fmode & (FWRITE | O_TRUNC)) {
222 if (vp->v_type == VDIR) {
223 error = EISDIR;
224 goto bad;
225 }
226 if ((error = vn_writechk(vp)) != 0 ||
227 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
228 goto bad;
229 #ifdef VERIFIED_EXEC
230 if (vhe != NULL) {
231 veriexec_report("Write access request.",
232 pathbuf, &va, l,
233 REPORT_NOVERBOSE,
234 REPORT_ALARM,
235 REPORT_NOPANIC);
236
237 /* IPS mode: Deny writing to monitored files. */
238 if (veriexec_strict >= 2) {
239 error = EPERM;
240 goto bad;
241 } else {
242 vhe->status = FINGERPRINT_NOTEVAL;
243 }
244 }
245 #endif
246 }
247 }
248
249 if (fmode & O_TRUNC) {
250 VOP_UNLOCK(vp, 0); /* XXX */
251 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
252 vrele(vp);
253 return (error);
254 }
255 VOP_LEASE(vp, l, cred, LEASE_WRITE);
256 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
257 VATTR_NULL(&va);
258 va.va_size = 0;
259 error = VOP_SETATTR(vp, &va, cred, l);
260 vn_finished_write(mp, 0);
261 if (error != 0)
262 goto bad;
263 }
264 if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
265 goto bad;
266 if (vp->v_type == VREG &&
267 uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
268 error = EIO;
269 goto bad;
270 }
271 if (fmode & FWRITE)
272 vp->v_writecount++;
273
274 return (0);
275 bad:
276 vput(vp);
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_flag & VTEXT)
293 return (ETXTBSY);
294 return (0);
295 }
296
297 /*
298 * Mark a vnode as having executable mappings.
299 */
300 void
301 vn_markexec(struct vnode *vp)
302 {
303 if ((vp->v_flag & VEXECMAP) == 0) {
304 uvmexp.filepages -= vp->v_uobj.uo_npages;
305 uvmexp.execpages += vp->v_uobj.uo_npages;
306 }
307 vp->v_flag |= VEXECMAP;
308 }
309
310 /*
311 * Mark a vnode as being the text of a process.
312 * Fail if the vnode is currently writable.
313 */
314 int
315 vn_marktext(struct vnode *vp)
316 {
317
318 if (vp->v_writecount != 0) {
319 KASSERT((vp->v_flag & VTEXT) == 0);
320 return (ETXTBSY);
321 }
322 vp->v_flag |= VTEXT;
323 vn_markexec(vp);
324 return (0);
325 }
326
327 /*
328 * Vnode close call
329 *
330 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
331 */
332 int
333 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
334 {
335 int error;
336
337 if (flags & FWRITE)
338 vp->v_writecount--;
339 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
340 error = VOP_CLOSE(vp, flags, cred, l);
341 vput(vp);
342 return (error);
343 }
344
345 /*
346 * Package up an I/O request on a vnode into a uio and do it.
347 */
348 int
349 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset,
350 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
351 struct lwp *l)
352 {
353 struct uio auio;
354 struct iovec aiov;
355 struct mount *mp = NULL;
356 int error;
357
358 if ((ioflg & IO_NODELOCKED) == 0) {
359 if (rw == UIO_READ) {
360 vn_lock(vp, LK_SHARED | LK_RETRY);
361 } else /* UIO_WRITE */ {
362 if (vp->v_type != VCHR &&
363 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
364 != 0)
365 return (error);
366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
367 }
368 }
369 auio.uio_iov = &aiov;
370 auio.uio_iovcnt = 1;
371 aiov.iov_base = base;
372 aiov.iov_len = len;
373 auio.uio_resid = len;
374 auio.uio_offset = offset;
375 auio.uio_rw = rw;
376 if (segflg == UIO_SYSSPACE) {
377 UIO_SETUP_SYSSPACE(&auio);
378 } else {
379 auio.uio_vmspace = l->l_proc->p_vmspace;
380 }
381 if (rw == UIO_READ) {
382 error = VOP_READ(vp, &auio, ioflg, cred);
383 } else {
384 error = VOP_WRITE(vp, &auio, ioflg, cred);
385 }
386 if (aresid)
387 *aresid = auio.uio_resid;
388 else
389 if (auio.uio_resid && error == 0)
390 error = EIO;
391 if ((ioflg & IO_NODELOCKED) == 0) {
392 if (rw == UIO_WRITE)
393 vn_finished_write(mp, 0);
394 VOP_UNLOCK(vp, 0);
395 }
396 return (error);
397 }
398
399 int
400 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
401 struct lwp *l, off_t **cookies, int *ncookies)
402 {
403 struct vnode *vp = (struct vnode *)fp->f_data;
404 struct iovec aiov;
405 struct uio auio;
406 int error, eofflag;
407
408 unionread:
409 if (vp->v_type != VDIR)
410 return (EINVAL);
411 aiov.iov_base = bf;
412 aiov.iov_len = count;
413 auio.uio_iov = &aiov;
414 auio.uio_iovcnt = 1;
415 auio.uio_rw = UIO_READ;
416 if (segflg == UIO_SYSSPACE) {
417 UIO_SETUP_SYSSPACE(&auio);
418 } else {
419 KASSERT(l == curlwp);
420 auio.uio_vmspace = l->l_proc->p_vmspace;
421 }
422 auio.uio_resid = count;
423 vn_lock(vp, LK_SHARED | LK_RETRY);
424 auio.uio_offset = fp->f_offset;
425 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
426 ncookies);
427 fp->f_offset = auio.uio_offset;
428 VOP_UNLOCK(vp, 0);
429 if (error)
430 return (error);
431
432 #if defined(UNION) || defined(LKM)
433 if (count == auio.uio_resid && vn_union_readdir_hook) {
434 struct vnode *ovp = vp;
435
436 error = (*vn_union_readdir_hook)(&vp, fp, l);
437 if (error)
438 return (error);
439 if (vp != ovp)
440 goto unionread;
441 }
442 #endif /* UNION || LKM */
443
444 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
445 (vp->v_mount->mnt_flag & MNT_UNION)) {
446 struct vnode *tvp = vp;
447 vp = vp->v_mount->mnt_vnodecovered;
448 VREF(vp);
449 fp->f_data = vp;
450 fp->f_offset = 0;
451 vrele(tvp);
452 goto unionread;
453 }
454 *done = count - auio.uio_resid;
455 return error;
456 }
457
458 /*
459 * File table vnode read routine.
460 */
461 static int
462 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
463 int flags)
464 {
465 struct vnode *vp = (struct vnode *)fp->f_data;
466 int count, error, ioflag;
467 struct lwp *l = curlwp;
468
469 VOP_LEASE(vp, l, cred, LEASE_READ);
470 ioflag = IO_ADV_ENCODE(fp->f_advice);
471 if (fp->f_flag & FNONBLOCK)
472 ioflag |= IO_NDELAY;
473 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
474 ioflag |= IO_SYNC;
475 if (fp->f_flag & FALTIO)
476 ioflag |= IO_ALTSEMANTICS;
477 vn_lock(vp, LK_SHARED | LK_RETRY);
478 uio->uio_offset = *offset;
479 count = uio->uio_resid;
480 error = VOP_READ(vp, uio, ioflag, cred);
481 if (flags & FOF_UPDATE_OFFSET)
482 *offset += count - uio->uio_resid;
483 VOP_UNLOCK(vp, 0);
484 return (error);
485 }
486
487 /*
488 * File table vnode write routine.
489 */
490 static int
491 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
492 int flags)
493 {
494 struct vnode *vp = (struct vnode *)fp->f_data;
495 struct mount *mp;
496 int count, error, ioflag = IO_UNIT;
497 struct lwp *l = curlwp;
498
499 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
500 ioflag |= IO_APPEND;
501 if (fp->f_flag & FNONBLOCK)
502 ioflag |= IO_NDELAY;
503 if (fp->f_flag & FFSYNC ||
504 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
505 ioflag |= IO_SYNC;
506 else if (fp->f_flag & FDSYNC)
507 ioflag |= IO_DSYNC;
508 if (fp->f_flag & FALTIO)
509 ioflag |= IO_ALTSEMANTICS;
510 mp = NULL;
511 if (vp->v_type != VCHR &&
512 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
513 return (error);
514 VOP_LEASE(vp, l, cred, LEASE_WRITE);
515 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
516 uio->uio_offset = *offset;
517 count = uio->uio_resid;
518 error = VOP_WRITE(vp, uio, ioflag, cred);
519 if (flags & FOF_UPDATE_OFFSET) {
520 if (ioflag & IO_APPEND)
521 *offset = uio->uio_offset;
522 else
523 *offset += count - uio->uio_resid;
524 }
525 VOP_UNLOCK(vp, 0);
526 vn_finished_write(mp, 0);
527 return (error);
528 }
529
530 /*
531 * File table vnode stat routine.
532 */
533 static int
534 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
535 {
536 struct vnode *vp = (struct vnode *)fp->f_data;
537
538 return vn_stat(vp, sb, l);
539 }
540
541 int
542 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
543 {
544 struct vattr va;
545 int error;
546 mode_t mode;
547
548 error = VOP_GETATTR(vp, &va, l->l_proc->p_cred, l);
549 if (error)
550 return (error);
551 /*
552 * Copy from vattr table
553 */
554 sb->st_dev = va.va_fsid;
555 sb->st_ino = va.va_fileid;
556 mode = va.va_mode;
557 switch (vp->v_type) {
558 case VREG:
559 mode |= S_IFREG;
560 break;
561 case VDIR:
562 mode |= S_IFDIR;
563 break;
564 case VBLK:
565 mode |= S_IFBLK;
566 break;
567 case VCHR:
568 mode |= S_IFCHR;
569 break;
570 case VLNK:
571 mode |= S_IFLNK;
572 break;
573 case VSOCK:
574 mode |= S_IFSOCK;
575 break;
576 case VFIFO:
577 mode |= S_IFIFO;
578 break;
579 default:
580 return (EBADF);
581 };
582 sb->st_mode = mode;
583 sb->st_nlink = va.va_nlink;
584 sb->st_uid = va.va_uid;
585 sb->st_gid = va.va_gid;
586 sb->st_rdev = va.va_rdev;
587 sb->st_size = va.va_size;
588 sb->st_atimespec = va.va_atime;
589 sb->st_mtimespec = va.va_mtime;
590 sb->st_ctimespec = va.va_ctime;
591 sb->st_birthtimespec = va.va_birthtime;
592 sb->st_blksize = va.va_blocksize;
593 sb->st_flags = va.va_flags;
594 sb->st_gen = 0;
595 sb->st_blocks = va.va_bytes / S_BLKSIZE;
596 return (0);
597 }
598
599 /*
600 * File table vnode fcntl routine.
601 */
602 static int
603 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
604 {
605 struct vnode *vp = ((struct vnode *)fp->f_data);
606 int error;
607
608 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
609 error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_proc->p_cred, l);
610 VOP_UNLOCK(vp, 0);
611 return (error);
612 }
613
614 /*
615 * File table vnode ioctl routine.
616 */
617 static int
618 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
619 {
620 struct vnode *vp = ((struct vnode *)fp->f_data);
621 struct proc *p = l->l_proc;
622 struct vattr vattr;
623 int error;
624
625 switch (vp->v_type) {
626
627 case VREG:
628 case VDIR:
629 if (com == FIONREAD) {
630 error = VOP_GETATTR(vp, &vattr, l->l_proc->p_cred, l);
631 if (error)
632 return (error);
633 *(int *)data = vattr.va_size - fp->f_offset;
634 return (0);
635 }
636 if ((com == FIONWRITE) || (com == FIONSPACE)) {
637 /*
638 * Files don't have send queues, so there never
639 * are any bytes in them, nor is there any
640 * open space in them.
641 */
642 *(int *)data = 0;
643 return (0);
644 }
645 if (com == FIOGETBMAP) {
646 daddr_t *block;
647
648 if (*(daddr_t *)data < 0)
649 return (EINVAL);
650 block = (daddr_t *)data;
651 return (VOP_BMAP(vp, *block, NULL, block, NULL));
652 }
653 if (com == OFIOGETBMAP) {
654 daddr_t ibn, obn;
655
656 if (*(int32_t *)data < 0)
657 return (EINVAL);
658 ibn = (daddr_t)*(int32_t *)data;
659 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
660 *(int32_t *)data = (int32_t)obn;
661 return error;
662 }
663 if (com == FIONBIO || com == FIOASYNC) /* XXX */
664 return (0); /* XXX */
665 /* fall into ... */
666 case VFIFO:
667 case VCHR:
668 case VBLK:
669 error = VOP_IOCTL(vp, com, data, fp->f_flag,
670 l->l_proc->p_cred, l);
671 if (error == 0 && com == TIOCSCTTY) {
672 if (p->p_session->s_ttyvp)
673 vrele(p->p_session->s_ttyvp);
674 p->p_session->s_ttyvp = vp;
675 VREF(vp);
676 }
677 return (error);
678
679 default:
680 return (EPASSTHROUGH);
681 }
682 }
683
684 /*
685 * File table vnode poll routine.
686 */
687 static int
688 vn_poll(struct file *fp, int events, struct lwp *l)
689 {
690
691 return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
692 }
693
694 /*
695 * File table vnode kqfilter routine.
696 */
697 int
698 vn_kqfilter(struct file *fp, struct knote *kn)
699 {
700
701 return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
702 }
703
704 /*
705 * Check that the vnode is still valid, and if so
706 * acquire requested lock.
707 */
708 int
709 vn_lock(struct vnode *vp, int flags)
710 {
711 int error;
712
713 #if 0
714 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
715 || (vp->v_flag & VONWORKLST) != 0);
716 #endif
717 KASSERT((flags &
718 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
719 LK_SETRECURSE|LK_CANRECURSE))
720 == 0);
721
722 do {
723 if ((flags & LK_INTERLOCK) == 0)
724 simple_lock(&vp->v_interlock);
725 if (vp->v_flag & VXLOCK) {
726 if (flags & LK_NOWAIT) {
727 simple_unlock(&vp->v_interlock);
728 return EBUSY;
729 }
730 vp->v_flag |= VXWANT;
731 ltsleep(vp, PINOD | PNORELOCK,
732 "vn_lock", 0, &vp->v_interlock);
733 error = ENOENT;
734 } else {
735 error = VOP_LOCK(vp,
736 (flags & ~LK_RETRY) | LK_INTERLOCK);
737 if (error == 0 || error == EDEADLK || error == EBUSY)
738 return (error);
739 }
740 flags &= ~LK_INTERLOCK;
741 } while (flags & LK_RETRY);
742 return (error);
743 }
744
745 /*
746 * File table vnode close routine.
747 */
748 static int
749 vn_closefile(struct file *fp, struct lwp *l)
750 {
751
752 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
753 fp->f_cred, l));
754 }
755
756 /*
757 * Enable LK_CANRECURSE on lock. Return prior status.
758 */
759 u_int
760 vn_setrecurse(struct vnode *vp)
761 {
762 struct lock *lkp = &vp->v_lock;
763 u_int retval = lkp->lk_flags & LK_CANRECURSE;
764
765 lkp->lk_flags |= LK_CANRECURSE;
766 return retval;
767 }
768
769 /*
770 * Called when done with locksetrecurse.
771 */
772 void
773 vn_restorerecurse(struct vnode *vp, u_int flags)
774 {
775 struct lock *lkp = &vp->v_lock;
776
777 lkp->lk_flags &= ~LK_CANRECURSE;
778 lkp->lk_flags |= flags;
779 }
780
781 int
782 vn_cow_establish(struct vnode *vp,
783 int (*func)(void *, struct buf *), void *cookie)
784 {
785 int s;
786 struct spec_cow_entry *e;
787
788 MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
789 M_DEVBUF, M_WAITOK);
790 e->ce_func = func;
791 e->ce_cookie = cookie;
792
793 SPEC_COW_LOCK(vp->v_specinfo, s);
794 vp->v_spec_cow_req++;
795 while (vp->v_spec_cow_count > 0)
796 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
797 &vp->v_spec_cow_slock);
798
799 SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
800
801 vp->v_spec_cow_req--;
802 if (vp->v_spec_cow_req == 0)
803 wakeup(&vp->v_spec_cow_req);
804 SPEC_COW_UNLOCK(vp->v_specinfo, s);
805
806 return 0;
807 }
808
809 int
810 vn_cow_disestablish(struct vnode *vp,
811 int (*func)(void *, struct buf *), void *cookie)
812 {
813 int s;
814 struct spec_cow_entry *e;
815
816 SPEC_COW_LOCK(vp->v_specinfo, s);
817 vp->v_spec_cow_req++;
818 while (vp->v_spec_cow_count > 0)
819 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
820 &vp->v_spec_cow_slock);
821
822 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
823 if (e->ce_func == func && e->ce_cookie == cookie) {
824 SLIST_REMOVE(&vp->v_spec_cow_head, e,
825 spec_cow_entry, ce_list);
826 FREE(e, M_DEVBUF);
827 break;
828 }
829
830 vp->v_spec_cow_req--;
831 if (vp->v_spec_cow_req == 0)
832 wakeup(&vp->v_spec_cow_req);
833 SPEC_COW_UNLOCK(vp->v_specinfo, s);
834
835 return e ? 0 : EINVAL;
836 }
837
838 /*
839 * Simplified in-kernel wrapper calls for extended attribute access.
840 * Both calls pass in a NULL credential, authorizing a "kernel" access.
841 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
842 */
843 int
844 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
845 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
846 {
847 struct uio auio;
848 struct iovec aiov;
849 int error;
850
851 aiov.iov_len = *buflen;
852 aiov.iov_base = bf;
853
854 auio.uio_iov = &aiov;
855 auio.uio_iovcnt = 1;
856 auio.uio_rw = UIO_READ;
857 auio.uio_offset = 0;
858 auio.uio_resid = *buflen;
859 UIO_SETUP_SYSSPACE(&auio);
860
861 if ((ioflg & IO_NODELOCKED) == 0)
862 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
863
864 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
865 l);
866
867 if ((ioflg & IO_NODELOCKED) == 0)
868 VOP_UNLOCK(vp, 0);
869
870 if (error == 0)
871 *buflen = *buflen - auio.uio_resid;
872
873 return (error);
874 }
875
876 /*
877 * XXX Failure mode if partially written?
878 */
879 int
880 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
881 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
882 {
883 struct uio auio;
884 struct iovec aiov;
885 struct mount *mp;
886 int error;
887
888 aiov.iov_len = buflen;
889 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
890
891 auio.uio_iov = &aiov;
892 auio.uio_iovcnt = 1;
893 auio.uio_rw = UIO_WRITE;
894 auio.uio_offset = 0;
895 auio.uio_resid = buflen;
896 UIO_SETUP_SYSSPACE(&auio);
897
898 if ((ioflg & IO_NODELOCKED) == 0) {
899 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
900 return (error);
901 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
902 }
903
904 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
905
906 if ((ioflg & IO_NODELOCKED) == 0) {
907 vn_finished_write(mp, 0);
908 VOP_UNLOCK(vp, 0);
909 }
910
911 return (error);
912 }
913
914 int
915 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
916 const char *attrname, struct lwp *l)
917 {
918 struct mount *mp;
919 int error;
920
921 if ((ioflg & IO_NODELOCKED) == 0) {
922 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
923 return (error);
924 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
925 }
926
927 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
928 if (error == EOPNOTSUPP)
929 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
930 NULL, l);
931
932 if ((ioflg & IO_NODELOCKED) == 0) {
933 vn_finished_write(mp, 0);
934 VOP_UNLOCK(vp, 0);
935 }
936
937 return (error);
938 }
939
940 /*
941 * Preparing to start a filesystem write operation. If the operation is
942 * permitted, then we bump the count of operations in progress and
943 * proceed. If a suspend request is in progress, we wait until the
944 * suspension is over, and then proceed.
945 * V_PCATCH adds PCATCH to the tsleep flags.
946 * V_WAIT waits until suspension is over. Otherwise returns EWOULDBLOCK.
947 * V_SLEEPONLY wait, but do not bump the operations count.
948 * V_LOWER this is a lower level operation. No further vnodes should be
949 * locked. Otherwise it is a upper level operation. No vnodes
950 * should be locked.
951 */
952 int
953 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
954 {
955 struct mount *mp;
956 int error, mask, prio;
957
958 /*
959 * If a vnode is provided, get and return the mount point that
960 * to which it will write.
961 */
962 if (vp != NULL) {
963 *mpp = vp->v_mount;
964 }
965 if ((mp = *mpp) == NULL)
966 return (0);
967 mp = mp->mnt_leaf;
968 /*
969 * Check on status of suspension.
970 */
971 prio = PUSER - 1;
972 if (flags & V_PCATCH)
973 prio |= PCATCH;
974
975 if ((flags & V_LOWER) == 0)
976 mask = IMNT_SUSPEND;
977 else
978 mask = IMNT_SUSPENDLOW;
979
980 while ((mp->mnt_iflag & mask) != 0) {
981 if ((flags & V_WAIT) == 0)
982 return (EWOULDBLOCK);
983 error = tsleep(&mp->mnt_flag, prio, "suspfs", 0);
984 if (error)
985 return (error);
986 }
987 if (flags & V_SLEEPONLY)
988 return (0);
989 simple_lock(&mp->mnt_slock);
990 if ((flags & V_LOWER) == 0)
991 mp->mnt_writeopcountupper++;
992 else
993 mp->mnt_writeopcountlower++;
994 simple_unlock(&mp->mnt_slock);
995 return (0);
996 }
997
998 /*
999 * Filesystem write operation has completed. If we are suspending and this
1000 * operation is the last one, notify the suspender that the suspension is
1001 * now in effect.
1002 */
1003 void
1004 vn_finished_write(struct mount *mp, int flags)
1005 {
1006 if (mp == NULL)
1007 return;
1008 mp = mp->mnt_leaf;
1009 simple_lock(&mp->mnt_slock);
1010 if ((flags & V_LOWER) == 0) {
1011 mp->mnt_writeopcountupper--;
1012 if (mp->mnt_writeopcountupper < 0)
1013 printf("vn_finished_write: neg cnt upper=%d\n",
1014 mp->mnt_writeopcountupper);
1015 if ((mp->mnt_iflag & IMNT_SUSPEND) != 0 &&
1016 mp->mnt_writeopcountupper <= 0)
1017 wakeup(&mp->mnt_writeopcountupper);
1018 } else {
1019 mp->mnt_writeopcountlower--;
1020 if (mp->mnt_writeopcountlower < 0)
1021 printf("vn_finished_write: neg cnt lower=%d\n",
1022 mp->mnt_writeopcountlower);
1023 if ((mp->mnt_iflag & IMNT_SUSPENDLOW) != 0 &&
1024 mp->mnt_writeopcountupper <= 0)
1025 wakeup(&mp->mnt_writeopcountlower);
1026 }
1027 simple_unlock(&mp->mnt_slock);
1028 }
1029
1030 void
1031 vn_ra_allocctx(struct vnode *vp)
1032 {
1033 struct uvm_ractx *ra = NULL;
1034
1035 if (vp->v_type != VREG) {
1036 return;
1037 }
1038 if (vp->v_ractx != NULL) {
1039 return;
1040 }
1041 simple_lock(&vp->v_interlock);
1042 if (vp->v_ractx == NULL) {
1043 simple_unlock(&vp->v_interlock);
1044 ra = uvm_ra_allocctx();
1045 simple_lock(&vp->v_interlock);
1046 if (ra != NULL && vp->v_ractx == NULL) {
1047 vp->v_ractx = ra;
1048 ra = NULL;
1049 }
1050 }
1051 simple_unlock(&vp->v_interlock);
1052 if (ra != NULL) {
1053 uvm_ra_freectx(ra);
1054 }
1055 }
1056