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