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