vfs_vnops.c revision 1.72.2.9 1 /* $NetBSD: vfs_vnops.c,v 1.72.2.9 2005/01/24 08:59:40 skrll 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.72.2.9 2005/01/24 08:59:40 skrll Exp $");
41
42 #include "fs_union.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/poll.h>
58
59 #include <miscfs/specfs/specdev.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #ifdef UNION
64 #include <fs/union/union.h>
65 #endif
66
67 #if defined(LKM) || defined(UNION)
68 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
69 #endif
70
71 #ifdef VERIFIED_EXEC
72 #include <sys/verified_exec.h>
73
74 extern LIST_HEAD(veriexec_devhead, veriexec_dev_list) veriexec_dev_head;
75 extern struct veriexec_devhead veriexec_file_dev_head;
76 #endif
77
78 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
79 struct ucred *cred, int flags);
80 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
81 struct ucred *cred, int flags);
82 static int vn_closefile(struct file *fp, struct lwp *l);
83 static int vn_poll(struct file *fp, int events, struct lwp *l);
84 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
85 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
86 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
87
88 const struct fileops vnops = {
89 vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
90 vn_statfile, vn_closefile, vn_kqfilter
91 };
92
93 /*
94 * Common code for vnode open operations.
95 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
96 */
97 int
98 vn_open(ndp, fmode, cmode)
99 struct nameidata *ndp;
100 int fmode, cmode;
101 {
102 struct vnode *vp;
103 struct mount *mp;
104 struct lwp *l = ndp->ni_cnd.cn_lwp;
105 struct ucred *cred = l->l_proc->p_ucred;
106 struct vattr va;
107 int error;
108 #ifdef VERIFIED_EXEC
109 char got_dev;
110 struct veriexec_inode_list *veriexec_node;
111 char fingerprint[MAXFINGERPRINTLEN];
112 #endif
113
114 restart:
115 if (fmode & O_CREAT) {
116 ndp->ni_cnd.cn_nameiop = CREATE;
117 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
118 if ((fmode & O_EXCL) == 0 &&
119 ((fmode & O_NOFOLLOW) == 0))
120 ndp->ni_cnd.cn_flags |= FOLLOW;
121 if ((error = namei(ndp)) != 0)
122 return (error);
123 if (ndp->ni_vp == NULL) {
124 VATTR_NULL(&va);
125 va.va_type = VREG;
126 va.va_mode = cmode;
127 if (fmode & O_EXCL)
128 va.va_vaflags |= VA_EXCLUSIVE;
129 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
130 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
131 vput(ndp->ni_dvp);
132 if ((error = vn_start_write(NULL, &mp,
133 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
134 return (error);
135 goto restart;
136 }
137 VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
138 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
139 &ndp->ni_cnd, &va);
140 vn_finished_write(mp, 0);
141 if (error)
142 return (error);
143 fmode &= ~O_TRUNC;
144 vp = ndp->ni_vp;
145 } else {
146 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
147 if (ndp->ni_dvp == ndp->ni_vp)
148 vrele(ndp->ni_dvp);
149 else
150 vput(ndp->ni_dvp);
151 ndp->ni_dvp = NULL;
152 vp = ndp->ni_vp;
153 if (fmode & O_EXCL) {
154 error = EEXIST;
155 goto bad;
156 }
157 fmode &= ~O_CREAT;
158 }
159 } else {
160 ndp->ni_cnd.cn_nameiop = LOOKUP;
161 ndp->ni_cnd.cn_flags = LOCKLEAF;
162 if ((fmode & O_NOFOLLOW) == 0)
163 ndp->ni_cnd.cn_flags |= FOLLOW;
164 if ((error = namei(ndp)) != 0)
165 return (error);
166 vp = ndp->ni_vp;
167 }
168 if (vp->v_type == VSOCK) {
169 error = EOPNOTSUPP;
170 goto bad;
171 }
172 if (ndp->ni_vp->v_type == VLNK) {
173 error = EFTYPE;
174 goto bad;
175 }
176
177 #ifdef VERIFIED_EXEC
178 veriexec_node = NULL;
179
180 if ((error = VOP_GETATTR(vp, &va, cred, l)) != 0)
181 goto bad;
182 #endif
183
184 if ((fmode & O_CREAT) == 0) {
185 #ifdef VERIFIED_EXEC
186 /*
187 * Look for the file on the fingerprint lists iff
188 * it has not been seen before.
189 */
190 if ((vp->fp_status == FINGERPRINT_INVALID) ||
191 (vp->fp_status == FINGERPRINT_NODEV)) {
192 /* check the file list for the finger print */
193 veriexec_node = get_veriexec_inode(&veriexec_file_dev_head,
194 va.va_fsid,
195 va.va_fileid,
196 &got_dev);
197 if (veriexec_node == NULL) {
198 /* failing that, check the exec list */
199 veriexec_node = get_veriexec_inode(
200 &veriexec_dev_head, va.va_fsid,
201 va.va_fileid, &got_dev);
202 }
203
204 if ((veriexec_node == NULL) && (got_dev == 1))
205 vp->fp_status = FINGERPRINT_NOENTRY;
206
207 if (veriexec_node != NULL) {
208 if ((error = evaluate_fingerprint(vp,
209 veriexec_node, l->l_proc, va.va_size,
210 fingerprint)) != 0)
211 goto bad;
212
213 if (fingerprintcmp(veriexec_node,
214 fingerprint) == 0) {
215 /* fingerprint ok */
216 vp->fp_status = FINGERPRINT_VALID;
217 #ifdef VERIFIED_EXEC_DEBUG
218 printf(
219 "file fingerprint matches for dev %lu, file %lu\n",
220 va.va_fsid, va.va_fileid);
221 #endif
222 } else {
223 vp->fp_status = FINGERPRINT_NOMATCH;
224 }
225 }
226 }
227 #endif
228
229 if (fmode & FREAD) {
230 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
231 goto bad;
232
233 #ifdef VERIFIED_EXEC
234 /* file is on finger print list */
235 if (vp->fp_status == FINGERPRINT_NOMATCH) {
236 /* fingerprint bad */
237 printf(
238 "file fingerprint does not match on dev %lu, file %lu\n",
239 va.va_fsid, va.va_fileid);
240 if (securelevel > 2) {
241 error = EPERM;
242 goto bad;
243 }
244 }
245 #endif
246 }
247 if (fmode & (FWRITE | O_TRUNC)) {
248 if (vp->v_type == VDIR) {
249 error = EISDIR;
250 goto bad;
251 }
252 if ((error = vn_writechk(vp)) != 0 ||
253 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
254 goto bad;
255 #ifdef VERIFIED_EXEC
256 /*
257 * If file has a fingerprint then
258 * deny the write request, otherwise
259 * invalidate the status so we don't
260 * keep checking for the file having
261 * a fingerprint.
262 */
263 if (vp->fp_status == FINGERPRINT_VALID) {
264 printf(
265 "writing to fingerprinted file for dev %lu, file %lu\n",
266 va.va_fsid, va.va_fileid);
267 if (securelevel > 2) {
268 error = EPERM;
269 goto bad;
270 } else {
271 vp->fp_status = FINGERPRINT_INVALID;
272 }
273 }
274 #endif
275 }
276 }
277 if (fmode & O_TRUNC) {
278 VOP_UNLOCK(vp, 0); /* XXX */
279 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
280 vput(vp);
281 return (error);
282 }
283 VOP_LEASE(vp, l, cred, LEASE_WRITE);
284 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
285 VATTR_NULL(&va);
286 va.va_size = 0;
287 error = VOP_SETATTR(vp, &va, cred, l);
288 vn_finished_write(mp, 0);
289 if (error != 0)
290 goto bad;
291 }
292 if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
293 goto bad;
294 if (vp->v_type == VREG &&
295 uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
296 error = EIO;
297 goto bad;
298 }
299 if (fmode & FWRITE)
300 vp->v_writecount++;
301
302 return (0);
303 bad:
304 vput(vp);
305 return (error);
306 }
307
308 /*
309 * Check for write permissions on the specified vnode.
310 * Prototype text segments cannot be written.
311 */
312 int
313 vn_writechk(vp)
314 struct vnode *vp;
315 {
316
317 /*
318 * If the vnode is in use as a process's text,
319 * we can't allow writing.
320 */
321 if (vp->v_flag & VTEXT)
322 return (ETXTBSY);
323 return (0);
324 }
325
326 /*
327 * Mark a vnode as having executable mappings.
328 */
329 void
330 vn_markexec(vp)
331 struct vnode *vp;
332 {
333 if ((vp->v_flag & VEXECMAP) == 0) {
334 uvmexp.filepages -= vp->v_uobj.uo_npages;
335 uvmexp.execpages += vp->v_uobj.uo_npages;
336 }
337 vp->v_flag |= VEXECMAP;
338 }
339
340 /*
341 * Mark a vnode as being the text of a process.
342 * Fail if the vnode is currently writable.
343 */
344 int
345 vn_marktext(vp)
346 struct vnode *vp;
347 {
348
349 if (vp->v_writecount != 0) {
350 KASSERT((vp->v_flag & VTEXT) == 0);
351 return (ETXTBSY);
352 }
353 vp->v_flag |= VTEXT;
354 vn_markexec(vp);
355 return (0);
356 }
357
358 /*
359 * Vnode close call
360 *
361 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
362 */
363 int
364 vn_close(vp, flags, cred, l)
365 struct vnode *vp;
366 int flags;
367 struct ucred *cred;
368 struct lwp *l;
369 {
370 int error;
371
372 if (flags & FWRITE)
373 vp->v_writecount--;
374 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
375 error = VOP_CLOSE(vp, flags, cred, l);
376 vput(vp);
377 return (error);
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(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, l)
385 enum uio_rw rw;
386 struct vnode *vp;
387 caddr_t base;
388 int len;
389 off_t offset;
390 enum uio_seg segflg;
391 int ioflg;
392 struct ucred *cred;
393 size_t *aresid;
394 struct lwp *l;
395 {
396 struct uio auio;
397 struct iovec aiov;
398 struct mount *mp;
399 int error;
400
401 if ((ioflg & IO_NODELOCKED) == 0) {
402 if (rw == UIO_READ) {
403 vn_lock(vp, LK_SHARED | LK_RETRY);
404 } else /* UIO_WRITE */ {
405 if (vp->v_type != VCHR &&
406 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
407 != 0)
408 return (error);
409 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
410 }
411 }
412 auio.uio_iov = &aiov;
413 auio.uio_iovcnt = 1;
414 aiov.iov_base = base;
415 aiov.iov_len = len;
416 auio.uio_resid = len;
417 auio.uio_offset = offset;
418 auio.uio_segflg = segflg;
419 auio.uio_rw = rw;
420 auio.uio_lwp = l;
421 if (rw == UIO_READ) {
422 error = VOP_READ(vp, &auio, ioflg, cred);
423 } else {
424 error = VOP_WRITE(vp, &auio, ioflg, cred);
425 }
426 if (aresid)
427 *aresid = auio.uio_resid;
428 else
429 if (auio.uio_resid && error == 0)
430 error = EIO;
431 if ((ioflg & IO_NODELOCKED) == 0) {
432 if (rw == UIO_WRITE)
433 vn_finished_write(mp, 0);
434 VOP_UNLOCK(vp, 0);
435 }
436 return (error);
437 }
438
439 int
440 vn_readdir(fp, buf, segflg, count, done, l, cookies, ncookies)
441 struct file *fp;
442 char *buf;
443 int segflg, *done, *ncookies;
444 u_int count;
445 struct lwp *l;
446 off_t **cookies;
447 {
448 struct vnode *vp = (struct vnode *)fp->f_data;
449 struct iovec aiov;
450 struct uio auio;
451 int error, eofflag;
452
453 unionread:
454 if (vp->v_type != VDIR)
455 return (EINVAL);
456 aiov.iov_base = buf;
457 aiov.iov_len = count;
458 auio.uio_iov = &aiov;
459 auio.uio_iovcnt = 1;
460 auio.uio_rw = UIO_READ;
461 auio.uio_segflg = segflg;
462 auio.uio_lwp = l;
463 auio.uio_resid = count;
464 vn_lock(vp, LK_SHARED | LK_RETRY);
465 auio.uio_offset = fp->f_offset;
466 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
467 ncookies);
468 fp->f_offset = auio.uio_offset;
469 VOP_UNLOCK(vp, 0);
470 if (error)
471 return (error);
472
473 #if defined(UNION) || defined(LKM)
474 if (count == auio.uio_resid && vn_union_readdir_hook) {
475 struct vnode *ovp = vp;
476
477 error = (*vn_union_readdir_hook)(&vp, fp, l);
478 if (error)
479 return (error);
480 if (vp != ovp)
481 goto unionread;
482 }
483 #endif /* UNION || LKM */
484
485 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
486 (vp->v_mount->mnt_flag & MNT_UNION)) {
487 struct vnode *tvp = vp;
488 vp = vp->v_mount->mnt_vnodecovered;
489 VREF(vp);
490 fp->f_data = vp;
491 fp->f_offset = 0;
492 vrele(tvp);
493 goto unionread;
494 }
495 *done = count - auio.uio_resid;
496 return error;
497 }
498
499 /*
500 * File table vnode read routine.
501 */
502 static int
503 vn_read(fp, offset, uio, cred, flags)
504 struct file *fp;
505 off_t *offset;
506 struct uio *uio;
507 struct ucred *cred;
508 int flags;
509 {
510 struct vnode *vp = (struct vnode *)fp->f_data;
511 int count, error, ioflag = 0;
512
513 VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_READ);
514 if (fp->f_flag & FNONBLOCK)
515 ioflag |= IO_NDELAY;
516 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
517 ioflag |= IO_SYNC;
518 if (fp->f_flag & FALTIO)
519 ioflag |= IO_ALTSEMANTICS;
520 vn_lock(vp, LK_SHARED | LK_RETRY);
521 uio->uio_offset = *offset;
522 count = uio->uio_resid;
523 error = VOP_READ(vp, uio, ioflag, cred);
524 if (flags & FOF_UPDATE_OFFSET)
525 *offset += count - uio->uio_resid;
526 VOP_UNLOCK(vp, 0);
527 return (error);
528 }
529
530 /*
531 * File table vnode write routine.
532 */
533 static int
534 vn_write(fp, offset, uio, cred, flags)
535 struct file *fp;
536 off_t *offset;
537 struct uio *uio;
538 struct ucred *cred;
539 int flags;
540 {
541 struct vnode *vp = (struct vnode *)fp->f_data;
542 struct mount *mp;
543 int count, error, ioflag = IO_UNIT;
544
545 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
546 ioflag |= IO_APPEND;
547 if (fp->f_flag & FNONBLOCK)
548 ioflag |= IO_NDELAY;
549 if (fp->f_flag & FFSYNC ||
550 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
551 ioflag |= IO_SYNC;
552 else if (fp->f_flag & FDSYNC)
553 ioflag |= IO_DSYNC;
554 if (fp->f_flag & FALTIO)
555 ioflag |= IO_ALTSEMANTICS;
556 mp = NULL;
557 if (vp->v_type != VCHR &&
558 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
559 return (error);
560 VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_WRITE);
561 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
562 uio->uio_offset = *offset;
563 count = uio->uio_resid;
564 error = VOP_WRITE(vp, uio, ioflag, cred);
565 if (flags & FOF_UPDATE_OFFSET) {
566 if (ioflag & IO_APPEND)
567 *offset = uio->uio_offset;
568 else
569 *offset += count - uio->uio_resid;
570 }
571 VOP_UNLOCK(vp, 0);
572 vn_finished_write(mp, 0);
573 return (error);
574 }
575
576 /*
577 * File table vnode stat routine.
578 */
579 static int
580 vn_statfile(fp, sb, l)
581 struct file *fp;
582 struct stat *sb;
583 struct lwp *l;
584 {
585 struct vnode *vp = (struct vnode *)fp->f_data;
586
587 return vn_stat(vp, sb, l);
588 }
589
590 int
591 vn_stat(vp, sb, l)
592 struct vnode *vp;
593 struct stat *sb;
594 struct lwp *l;
595 {
596 struct vattr va;
597 int error;
598 mode_t mode;
599
600 error = VOP_GETATTR(vp, &va, l->l_proc->p_ucred, l);
601 if (error)
602 return (error);
603 /*
604 * Copy from vattr table
605 */
606 sb->st_dev = va.va_fsid;
607 sb->st_ino = va.va_fileid;
608 mode = va.va_mode;
609 switch (vp->v_type) {
610 case VREG:
611 mode |= S_IFREG;
612 break;
613 case VDIR:
614 mode |= S_IFDIR;
615 break;
616 case VBLK:
617 mode |= S_IFBLK;
618 break;
619 case VCHR:
620 mode |= S_IFCHR;
621 break;
622 case VLNK:
623 mode |= S_IFLNK;
624 break;
625 case VSOCK:
626 mode |= S_IFSOCK;
627 break;
628 case VFIFO:
629 mode |= S_IFIFO;
630 break;
631 default:
632 return (EBADF);
633 };
634 sb->st_mode = mode;
635 sb->st_nlink = va.va_nlink;
636 sb->st_uid = va.va_uid;
637 sb->st_gid = va.va_gid;
638 sb->st_rdev = va.va_rdev;
639 sb->st_size = va.va_size;
640 sb->st_atimespec = va.va_atime;
641 sb->st_mtimespec = va.va_mtime;
642 sb->st_ctimespec = va.va_ctime;
643 sb->st_birthtimespec = va.va_birthtime;
644 sb->st_blksize = va.va_blocksize;
645 sb->st_flags = va.va_flags;
646 sb->st_gen = 0;
647 sb->st_blocks = va.va_bytes / S_BLKSIZE;
648 return (0);
649 }
650
651 /*
652 * File table vnode fcntl routine.
653 */
654 static int
655 vn_fcntl(fp, com, data, l)
656 struct file *fp;
657 u_int com;
658 void *data;
659 struct lwp *l;
660 {
661 struct vnode *vp = ((struct vnode *)fp->f_data);
662 int error;
663
664 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
665 error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_proc->p_ucred, l);
666 VOP_UNLOCK(vp, 0);
667 return (error);
668 }
669
670 /*
671 * File table vnode ioctl routine.
672 */
673 static int
674 vn_ioctl(fp, com, data, l)
675 struct file *fp;
676 u_long com;
677 void *data;
678 struct lwp *l;
679 {
680 struct vnode *vp = ((struct vnode *)fp->f_data);
681 struct proc *p = l->l_proc;
682 struct vattr vattr;
683 int error;
684
685 switch (vp->v_type) {
686
687 case VREG:
688 case VDIR:
689 if (com == FIONREAD) {
690 error = VOP_GETATTR(vp, &vattr, l->l_proc->p_ucred, l);
691 if (error)
692 return (error);
693 *(int *)data = vattr.va_size - fp->f_offset;
694 return (0);
695 }
696 if ((com == FIONWRITE) || (com == FIONSPACE)) {
697 /*
698 * Files don't have send queues, so there never
699 * are any bytes in them, nor is there any
700 * open space in them.
701 */
702 *(int *)data = 0;
703 return (0);
704 }
705 if (com == FIOGETBMAP) {
706 daddr_t *block;
707
708 if (*(daddr_t *)data < 0)
709 return (EINVAL);
710 block = (daddr_t *)data;
711 return (VOP_BMAP(vp, *block, NULL, block, NULL));
712 }
713 if (com == OFIOGETBMAP) {
714 daddr_t ibn, obn;
715
716 if (*(int32_t *)data < 0)
717 return (EINVAL);
718 ibn = (daddr_t)*(int32_t *)data;
719 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
720 *(int32_t *)data = (int32_t)obn;
721 return error;
722 }
723 if (com == FIONBIO || com == FIOASYNC) /* XXX */
724 return (0); /* XXX */
725 /* fall into ... */
726 case VFIFO:
727 case VCHR:
728 case VBLK:
729 error = VOP_IOCTL(vp, com, data, fp->f_flag,
730 l->l_proc->p_ucred, l);
731 if (error == 0 && com == TIOCSCTTY) {
732 if (p->p_session->s_ttyvp)
733 vrele(p->p_session->s_ttyvp);
734 p->p_session->s_ttyvp = vp;
735 VREF(vp);
736 }
737 return (error);
738
739 default:
740 return (EPASSTHROUGH);
741 }
742 }
743
744 /*
745 * File table vnode poll routine.
746 */
747 static int
748 vn_poll(fp, events, l)
749 struct file *fp;
750 int events;
751 struct lwp *l;
752 {
753
754 return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
755 }
756
757 /*
758 * File table vnode kqfilter routine.
759 */
760 int
761 vn_kqfilter(fp, kn)
762 struct file *fp;
763 struct knote *kn;
764 {
765
766 return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
767 }
768
769 /*
770 * Check that the vnode is still valid, and if so
771 * acquire requested lock.
772 */
773 int
774 vn_lock(vp, flags)
775 struct vnode *vp;
776 int flags;
777 {
778 int error;
779
780 #if 0
781 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
782 || (vp->v_flag & VONWORKLST) != 0);
783 #endif
784
785 do {
786 if ((flags & LK_INTERLOCK) == 0)
787 simple_lock(&vp->v_interlock);
788 if (vp->v_flag & VXLOCK) {
789 if (flags & LK_NOWAIT) {
790 simple_unlock(&vp->v_interlock);
791 return EBUSY;
792 }
793 vp->v_flag |= VXWANT;
794 ltsleep(vp, PINOD | PNORELOCK,
795 "vn_lock", 0, &vp->v_interlock);
796 error = ENOENT;
797 } else {
798 error = VOP_LOCK(vp,
799 (flags & ~LK_RETRY) | LK_INTERLOCK);
800 if (error == 0 || error == EDEADLK || error == EBUSY)
801 return (error);
802 }
803 flags &= ~LK_INTERLOCK;
804 } while (flags & LK_RETRY);
805 return (error);
806 }
807
808 /*
809 * File table vnode close routine.
810 */
811 static int
812 vn_closefile(fp, l)
813 struct file *fp;
814 struct lwp *l;
815 {
816
817 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
818 fp->f_cred, l));
819 }
820
821 /*
822 * Enable LK_CANRECURSE on lock. Return prior status.
823 */
824 u_int
825 vn_setrecurse(vp)
826 struct vnode *vp;
827 {
828 struct lock *lkp = &vp->v_lock;
829 u_int retval = lkp->lk_flags & LK_CANRECURSE;
830
831 lkp->lk_flags |= LK_CANRECURSE;
832 return retval;
833 }
834
835 /*
836 * Called when done with locksetrecurse.
837 */
838 void
839 vn_restorerecurse(vp, flags)
840 struct vnode *vp;
841 u_int flags;
842 {
843 struct lock *lkp = &vp->v_lock;
844
845 lkp->lk_flags &= ~LK_CANRECURSE;
846 lkp->lk_flags |= flags;
847 }
848
849 int
850 vn_cow_establish(struct vnode *vp,
851 int (*func)(void *, struct buf *), void *cookie)
852 {
853 int s;
854 struct spec_cow_entry *e;
855
856 MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
857 M_DEVBUF, M_WAITOK);
858 e->ce_func = func;
859 e->ce_cookie = cookie;
860
861 SPEC_COW_LOCK(vp->v_specinfo, s);
862 vp->v_spec_cow_req++;
863 while (vp->v_spec_cow_count > 0)
864 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
865 &vp->v_spec_cow_slock);
866
867 SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
868
869 vp->v_spec_cow_req--;
870 if (vp->v_spec_cow_req == 0)
871 wakeup(&vp->v_spec_cow_req);
872 SPEC_COW_UNLOCK(vp->v_specinfo, s);
873
874 return 0;
875 }
876
877 int
878 vn_cow_disestablish(struct vnode *vp,
879 int (*func)(void *, struct buf *), void *cookie)
880 {
881 int s;
882 struct spec_cow_entry *e;
883
884 SPEC_COW_LOCK(vp->v_specinfo, s);
885 vp->v_spec_cow_req++;
886 while (vp->v_spec_cow_count > 0)
887 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
888 &vp->v_spec_cow_slock);
889
890 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
891 if (e->ce_func == func && e->ce_cookie == cookie) {
892 SLIST_REMOVE(&vp->v_spec_cow_head, e,
893 spec_cow_entry, ce_list);
894 FREE(e, M_DEVBUF);
895 break;
896 }
897
898 vp->v_spec_cow_req--;
899 if (vp->v_spec_cow_req == 0)
900 wakeup(&vp->v_spec_cow_req);
901 SPEC_COW_UNLOCK(vp->v_specinfo, s);
902
903 return e ? 0 : EINVAL;
904 }
905
906 /*
907 * Simplified in-kernel wrapper calls for extended attribute access.
908 * Both calls pass in a NULL credential, authorizing a "kernel" access.
909 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
910 */
911 int
912 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
913 const char *attrname, size_t *buflen, void *buf, struct lwp *l)
914 {
915 struct uio auio;
916 struct iovec aiov;
917 int error;
918
919 aiov.iov_len = *buflen;
920 aiov.iov_base = buf;
921
922 auio.uio_iov = &aiov;
923 auio.uio_iovcnt = 1;
924 auio.uio_rw = UIO_READ;
925 auio.uio_segflg = UIO_SYSSPACE;
926 auio.uio_lwp = l;
927 auio.uio_offset = 0;
928 auio.uio_resid = *buflen;
929
930 if ((ioflg & IO_NODELOCKED) == 0)
931 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
932
933 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
934 l);
935
936 if ((ioflg & IO_NODELOCKED) == 0)
937 VOP_UNLOCK(vp, 0);
938
939 if (error == 0)
940 *buflen = *buflen - auio.uio_resid;
941
942 return (error);
943 }
944
945 /*
946 * XXX Failure mode if partially written?
947 */
948 int
949 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
950 const char *attrname, size_t buflen, const void *buf, struct lwp *l)
951 {
952 struct uio auio;
953 struct iovec aiov;
954 struct mount *mp;
955 int error;
956
957 aiov.iov_len = buflen;
958 aiov.iov_base = (caddr_t) buf; /* XXX kills const */
959
960 auio.uio_iov = &aiov;
961 auio.uio_iovcnt = 1;
962 auio.uio_rw = UIO_WRITE;
963 auio.uio_segflg = UIO_SYSSPACE;
964 auio.uio_lwp = l;
965 auio.uio_offset = 0;
966 auio.uio_resid = buflen;
967
968 if ((ioflg & IO_NODELOCKED) == 0) {
969 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
970 return (error);
971 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
972 }
973
974 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
975
976 if ((ioflg & IO_NODELOCKED) == 0) {
977 vn_finished_write(mp, 0);
978 VOP_UNLOCK(vp, 0);
979 }
980
981 return (error);
982 }
983
984 int
985 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
986 const char *attrname, struct lwp *l)
987 {
988 struct mount *mp;
989 int error;
990
991 if ((ioflg & IO_NODELOCKED) == 0) {
992 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
993 return (error);
994 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
995 }
996
997 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
998 if (error == EOPNOTSUPP)
999 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1000 NULL, l);
1001
1002 if ((ioflg & IO_NODELOCKED) == 0) {
1003 vn_finished_write(mp, 0);
1004 VOP_UNLOCK(vp, 0);
1005 }
1006
1007 return (error);
1008 }
1009