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