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