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