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