vfs_vnops.c revision 1.106.2.5 1 /* $NetBSD: vfs_vnops.c,v 1.106.2.5 2006/08/11 15:45:47 yamt 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.106.2.5 2006/08/11 15:45:47 yamt 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 >= VERIEXEC_LOCKDOWN) {
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_ALWAYS|REPORT_ALARM);
227
228 /* IPS mode: Deny writing to monitored files. */
229 if (veriexec_strict >= VERIEXEC_IPS) {
230 error = EPERM;
231 goto bad;
232 } else {
233 vfe->status = FINGERPRINT_NOTEVAL;
234 }
235 }
236 #endif /* NVERIEXEC > 0 */
237 }
238 }
239
240 if (fmode & O_TRUNC) {
241 VOP_UNLOCK(vp, 0); /* XXX */
242 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
243 vrele(vp);
244 return (error);
245 }
246 VOP_LEASE(vp, l, cred, LEASE_WRITE);
247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
248 VATTR_NULL(&va);
249 va.va_size = 0;
250 error = VOP_SETATTR(vp, &va, cred, l);
251 vn_finished_write(mp, 0);
252 if (error != 0)
253 goto bad;
254 }
255 if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
256 goto bad;
257 if (vp->v_type == VREG &&
258 uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
259 error = EIO;
260 goto bad;
261 }
262 if (fmode & FWRITE)
263 vp->v_writecount++;
264
265 return (0);
266 bad:
267 vput(vp);
268 return (error);
269 }
270
271 /*
272 * Check for write permissions on the specified vnode.
273 * Prototype text segments cannot be written.
274 */
275 int
276 vn_writechk(struct vnode *vp)
277 {
278
279 /*
280 * If the vnode is in use as a process's text,
281 * we can't allow writing.
282 */
283 if (vp->v_flag & VTEXT)
284 return (ETXTBSY);
285 return (0);
286 }
287
288 /*
289 * Mark a vnode as having executable mappings.
290 */
291 void
292 vn_markexec(struct vnode *vp)
293 {
294 if ((vp->v_flag & VEXECMAP) == 0) {
295 uvmexp.filepages -= vp->v_uobj.uo_npages;
296 uvmexp.execpages += vp->v_uobj.uo_npages;
297 }
298 vp->v_flag |= VEXECMAP;
299 }
300
301 /*
302 * Mark a vnode as being the text of a process.
303 * Fail if the vnode is currently writable.
304 */
305 int
306 vn_marktext(struct vnode *vp)
307 {
308
309 if (vp->v_writecount != 0) {
310 KASSERT((vp->v_flag & VTEXT) == 0);
311 return (ETXTBSY);
312 }
313 vp->v_flag |= VTEXT;
314 vn_markexec(vp);
315 return (0);
316 }
317
318 /*
319 * Vnode close call
320 *
321 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
322 */
323 int
324 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
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, l);
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(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset,
341 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
342 struct lwp *l)
343 {
344 struct uio auio;
345 struct iovec aiov;
346 struct mount *mp = NULL;
347 int error;
348
349 if ((ioflg & IO_NODELOCKED) == 0) {
350 if (rw == UIO_READ) {
351 vn_lock(vp, LK_SHARED | LK_RETRY);
352 } else /* UIO_WRITE */ {
353 if (vp->v_type != VCHR &&
354 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
355 != 0)
356 return (error);
357 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
358 }
359 }
360 auio.uio_iov = &aiov;
361 auio.uio_iovcnt = 1;
362 aiov.iov_base = base;
363 aiov.iov_len = len;
364 auio.uio_resid = len;
365 auio.uio_offset = offset;
366 auio.uio_rw = rw;
367 if (segflg == UIO_SYSSPACE) {
368 UIO_SETUP_SYSSPACE(&auio);
369 } else {
370 auio.uio_vmspace = l->l_proc->p_vmspace;
371 }
372 if (rw == UIO_READ) {
373 error = VOP_READ(vp, &auio, ioflg, cred);
374 } else {
375 error = VOP_WRITE(vp, &auio, ioflg, cred);
376 }
377 if (aresid)
378 *aresid = auio.uio_resid;
379 else
380 if (auio.uio_resid && error == 0)
381 error = EIO;
382 if ((ioflg & IO_NODELOCKED) == 0) {
383 if (rw == UIO_WRITE)
384 vn_finished_write(mp, 0);
385 VOP_UNLOCK(vp, 0);
386 }
387 return (error);
388 }
389
390 int
391 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
392 struct lwp *l, off_t **cookies, int *ncookies)
393 {
394 struct vnode *vp = (struct vnode *)fp->f_data;
395 struct iovec aiov;
396 struct uio auio;
397 int error, eofflag;
398
399 /* Limit the size on any kernel buffers used by VOP_READDIR */
400 count = min(MAXBSIZE, count);
401
402 unionread:
403 if (vp->v_type != VDIR)
404 return (EINVAL);
405 aiov.iov_base = bf;
406 aiov.iov_len = count;
407 auio.uio_iov = &aiov;
408 auio.uio_iovcnt = 1;
409 auio.uio_rw = UIO_READ;
410 if (segflg == UIO_SYSSPACE) {
411 UIO_SETUP_SYSSPACE(&auio);
412 } else {
413 KASSERT(l == curlwp);
414 auio.uio_vmspace = l->l_proc->p_vmspace;
415 }
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, l);
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, kauth_cred_t cred,
457 int flags)
458 {
459 struct vnode *vp = (struct vnode *)fp->f_data;
460 int count, error, ioflag;
461 struct lwp *l = curlwp;
462
463 VOP_LEASE(vp, l, cred, LEASE_READ);
464 ioflag = IO_ADV_ENCODE(fp->f_advice);
465 if (fp->f_flag & FNONBLOCK)
466 ioflag |= IO_NDELAY;
467 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
468 ioflag |= IO_SYNC;
469 if (fp->f_flag & FALTIO)
470 ioflag |= IO_ALTSEMANTICS;
471 vn_lock(vp, LK_SHARED | LK_RETRY);
472 uio->uio_offset = *offset;
473 count = uio->uio_resid;
474 error = VOP_READ(vp, uio, ioflag, cred);
475 if (flags & FOF_UPDATE_OFFSET)
476 *offset += count - uio->uio_resid;
477 VOP_UNLOCK(vp, 0);
478 return (error);
479 }
480
481 /*
482 * File table vnode write routine.
483 */
484 static int
485 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
486 int flags)
487 {
488 struct vnode *vp = (struct vnode *)fp->f_data;
489 struct mount *mp;
490 int count, error, ioflag = IO_UNIT;
491 struct lwp *l = curlwp;
492
493 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
494 ioflag |= IO_APPEND;
495 if (fp->f_flag & FNONBLOCK)
496 ioflag |= IO_NDELAY;
497 if (fp->f_flag & FFSYNC ||
498 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
499 ioflag |= IO_SYNC;
500 else if (fp->f_flag & FDSYNC)
501 ioflag |= IO_DSYNC;
502 if (fp->f_flag & FALTIO)
503 ioflag |= IO_ALTSEMANTICS;
504 mp = NULL;
505 if (vp->v_type != VCHR &&
506 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
507 return (error);
508 VOP_LEASE(vp, l, cred, LEASE_WRITE);
509 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
510 uio->uio_offset = *offset;
511 count = uio->uio_resid;
512 error = VOP_WRITE(vp, uio, ioflag, cred);
513 if (flags & FOF_UPDATE_OFFSET) {
514 if (ioflag & IO_APPEND)
515 *offset = uio->uio_offset;
516 else
517 *offset += count - uio->uio_resid;
518 }
519 VOP_UNLOCK(vp, 0);
520 vn_finished_write(mp, 0);
521 return (error);
522 }
523
524 /*
525 * File table vnode stat routine.
526 */
527 static int
528 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
529 {
530 struct vnode *vp = (struct vnode *)fp->f_data;
531
532 return vn_stat(vp, sb, l);
533 }
534
535 int
536 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
537 {
538 struct vattr va;
539 int error;
540 mode_t mode;
541
542 error = VOP_GETATTR(vp, &va, l->l_cred, l);
543 if (error)
544 return (error);
545 /*
546 * Copy from vattr table
547 */
548 sb->st_dev = va.va_fsid;
549 sb->st_ino = va.va_fileid;
550 mode = va.va_mode;
551 switch (vp->v_type) {
552 case VREG:
553 mode |= S_IFREG;
554 break;
555 case VDIR:
556 mode |= S_IFDIR;
557 break;
558 case VBLK:
559 mode |= S_IFBLK;
560 break;
561 case VCHR:
562 mode |= S_IFCHR;
563 break;
564 case VLNK:
565 mode |= S_IFLNK;
566 break;
567 case VSOCK:
568 mode |= S_IFSOCK;
569 break;
570 case VFIFO:
571 mode |= S_IFIFO;
572 break;
573 default:
574 return (EBADF);
575 };
576 sb->st_mode = mode;
577 sb->st_nlink = va.va_nlink;
578 sb->st_uid = va.va_uid;
579 sb->st_gid = va.va_gid;
580 sb->st_rdev = va.va_rdev;
581 sb->st_size = va.va_size;
582 sb->st_atimespec = va.va_atime;
583 sb->st_mtimespec = va.va_mtime;
584 sb->st_ctimespec = va.va_ctime;
585 sb->st_birthtimespec = va.va_birthtime;
586 sb->st_blksize = va.va_blocksize;
587 sb->st_flags = va.va_flags;
588 sb->st_gen = 0;
589 sb->st_blocks = va.va_bytes / S_BLKSIZE;
590 return (0);
591 }
592
593 /*
594 * File table vnode fcntl routine.
595 */
596 static int
597 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
598 {
599 struct vnode *vp = ((struct vnode *)fp->f_data);
600 int error;
601
602 error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
603 return (error);
604 }
605
606 /*
607 * File table vnode ioctl routine.
608 */
609 static int
610 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
611 {
612 struct vnode *vp = ((struct vnode *)fp->f_data);
613 struct proc *p = l->l_proc;
614 struct vattr vattr;
615 int error;
616
617 switch (vp->v_type) {
618
619 case VREG:
620 case VDIR:
621 if (com == FIONREAD) {
622 error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
623 if (error)
624 return (error);
625 *(int *)data = vattr.va_size - fp->f_offset;
626 return (0);
627 }
628 if ((com == FIONWRITE) || (com == FIONSPACE)) {
629 /*
630 * Files don't have send queues, so there never
631 * are any bytes in them, nor is there any
632 * open space in them.
633 */
634 *(int *)data = 0;
635 return (0);
636 }
637 if (com == FIOGETBMAP) {
638 daddr_t *block;
639
640 if (*(daddr_t *)data < 0)
641 return (EINVAL);
642 block = (daddr_t *)data;
643 return (VOP_BMAP(vp, *block, NULL, block, NULL));
644 }
645 if (com == OFIOGETBMAP) {
646 daddr_t ibn, obn;
647
648 if (*(int32_t *)data < 0)
649 return (EINVAL);
650 ibn = (daddr_t)*(int32_t *)data;
651 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
652 *(int32_t *)data = (int32_t)obn;
653 return error;
654 }
655 if (com == FIONBIO || com == FIOASYNC) /* XXX */
656 return (0); /* XXX */
657 /* fall into ... */
658 case VFIFO:
659 case VCHR:
660 case VBLK:
661 error = VOP_IOCTL(vp, com, data, fp->f_flag,
662 l->l_cred, l);
663 if (error == 0 && com == TIOCSCTTY) {
664 if (p->p_session->s_ttyvp)
665 vrele(p->p_session->s_ttyvp);
666 p->p_session->s_ttyvp = vp;
667 VREF(vp);
668 }
669 return (error);
670
671 default:
672 return (EPASSTHROUGH);
673 }
674 }
675
676 /*
677 * File table vnode poll routine.
678 */
679 static int
680 vn_poll(struct file *fp, int events, struct lwp *l)
681 {
682
683 return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
684 }
685
686 /*
687 * File table vnode kqfilter routine.
688 */
689 int
690 vn_kqfilter(struct file *fp, struct knote *kn)
691 {
692
693 return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
694 }
695
696 /*
697 * Check that the vnode is still valid, and if so
698 * acquire requested lock.
699 */
700 int
701 vn_lock(struct vnode *vp, int flags)
702 {
703 int error;
704
705 #if 0
706 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
707 || (vp->v_flag & VONWORKLST) != 0);
708 #endif
709 KASSERT((flags &
710 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
711 LK_SETRECURSE|LK_CANRECURSE))
712 == 0);
713
714 do {
715 if ((flags & LK_INTERLOCK) == 0)
716 simple_lock(&vp->v_interlock);
717 if (vp->v_flag & VXLOCK) {
718 if (flags & LK_NOWAIT) {
719 simple_unlock(&vp->v_interlock);
720 return EBUSY;
721 }
722 vp->v_flag |= VXWANT;
723 ltsleep(vp, PINOD | PNORELOCK,
724 "vn_lock", 0, &vp->v_interlock);
725 error = ENOENT;
726 } else {
727 error = VOP_LOCK(vp,
728 (flags & ~LK_RETRY) | LK_INTERLOCK);
729 if (error == 0 || error == EDEADLK || error == EBUSY)
730 return (error);
731 }
732 flags &= ~LK_INTERLOCK;
733 } while (flags & LK_RETRY);
734 return (error);
735 }
736
737 /*
738 * File table vnode close routine.
739 */
740 static int
741 vn_closefile(struct file *fp, struct lwp *l)
742 {
743
744 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
745 fp->f_cred, l));
746 }
747
748 /*
749 * Enable LK_CANRECURSE on lock. Return prior status.
750 */
751 u_int
752 vn_setrecurse(struct vnode *vp)
753 {
754 struct lock *lkp = &vp->v_lock;
755 u_int retval = lkp->lk_flags & LK_CANRECURSE;
756
757 lkp->lk_flags |= LK_CANRECURSE;
758 return retval;
759 }
760
761 /*
762 * Called when done with locksetrecurse.
763 */
764 void
765 vn_restorerecurse(struct vnode *vp, u_int flags)
766 {
767 struct lock *lkp = &vp->v_lock;
768
769 lkp->lk_flags &= ~LK_CANRECURSE;
770 lkp->lk_flags |= flags;
771 }
772
773 int
774 vn_cow_establish(struct vnode *vp,
775 int (*func)(void *, struct buf *), void *cookie)
776 {
777 int s;
778 struct spec_cow_entry *e;
779
780 MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
781 M_DEVBUF, M_WAITOK);
782 e->ce_func = func;
783 e->ce_cookie = cookie;
784
785 SPEC_COW_LOCK(vp->v_specinfo, s);
786 vp->v_spec_cow_req++;
787 while (vp->v_spec_cow_count > 0)
788 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
789 &vp->v_spec_cow_slock);
790
791 SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
792
793 vp->v_spec_cow_req--;
794 if (vp->v_spec_cow_req == 0)
795 wakeup(&vp->v_spec_cow_req);
796 SPEC_COW_UNLOCK(vp->v_specinfo, s);
797
798 return 0;
799 }
800
801 int
802 vn_cow_disestablish(struct vnode *vp,
803 int (*func)(void *, struct buf *), void *cookie)
804 {
805 int s;
806 struct spec_cow_entry *e;
807
808 SPEC_COW_LOCK(vp->v_specinfo, s);
809 vp->v_spec_cow_req++;
810 while (vp->v_spec_cow_count > 0)
811 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
812 &vp->v_spec_cow_slock);
813
814 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
815 if (e->ce_func == func && e->ce_cookie == cookie) {
816 SLIST_REMOVE(&vp->v_spec_cow_head, e,
817 spec_cow_entry, ce_list);
818 FREE(e, M_DEVBUF);
819 break;
820 }
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 e ? 0 : EINVAL;
828 }
829
830 /*
831 * Simplified in-kernel wrapper calls for extended attribute access.
832 * Both calls pass in a NULL credential, authorizing a "kernel" access.
833 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
834 */
835 int
836 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
837 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
838 {
839 struct uio auio;
840 struct iovec aiov;
841 int error;
842
843 aiov.iov_len = *buflen;
844 aiov.iov_base = bf;
845
846 auio.uio_iov = &aiov;
847 auio.uio_iovcnt = 1;
848 auio.uio_rw = UIO_READ;
849 auio.uio_offset = 0;
850 auio.uio_resid = *buflen;
851 UIO_SETUP_SYSSPACE(&auio);
852
853 if ((ioflg & IO_NODELOCKED) == 0)
854 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
855
856 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
857 l);
858
859 if ((ioflg & IO_NODELOCKED) == 0)
860 VOP_UNLOCK(vp, 0);
861
862 if (error == 0)
863 *buflen = *buflen - auio.uio_resid;
864
865 return (error);
866 }
867
868 /*
869 * XXX Failure mode if partially written?
870 */
871 int
872 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
873 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
874 {
875 struct uio auio;
876 struct iovec aiov;
877 struct mount *mp = NULL; /* XXX: GCC */
878 int error;
879
880 aiov.iov_len = buflen;
881 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
882
883 auio.uio_iov = &aiov;
884 auio.uio_iovcnt = 1;
885 auio.uio_rw = UIO_WRITE;
886 auio.uio_offset = 0;
887 auio.uio_resid = buflen;
888 UIO_SETUP_SYSSPACE(&auio);
889
890 if ((ioflg & IO_NODELOCKED) == 0) {
891 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
892 return (error);
893 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
894 }
895
896 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
897
898 if ((ioflg & IO_NODELOCKED) == 0) {
899 vn_finished_write(mp, 0);
900 VOP_UNLOCK(vp, 0);
901 }
902
903 return (error);
904 }
905
906 int
907 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
908 const char *attrname, struct lwp *l)
909 {
910 struct mount *mp = NULL; /* XXX: GCC */
911 int error;
912
913 if ((ioflg & IO_NODELOCKED) == 0) {
914 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
915 return (error);
916 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
917 }
918
919 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
920 if (error == EOPNOTSUPP)
921 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
922 NULL, l);
923
924 if ((ioflg & IO_NODELOCKED) == 0) {
925 vn_finished_write(mp, 0);
926 VOP_UNLOCK(vp, 0);
927 }
928
929 return (error);
930 }
931
932 /*
933 * Preparing to start a filesystem write operation. If the operation is
934 * permitted, then we bump the count of operations in progress and
935 * proceed. If a suspend request is in progress, we wait until the
936 * suspension is over, and then proceed.
937 * V_PCATCH adds PCATCH to the tsleep flags.
938 * V_WAIT waits until suspension is over. Otherwise returns EWOULDBLOCK.
939 * V_SLEEPONLY wait, but do not bump the operations count.
940 * V_LOWER this is a lower level operation. No further vnodes should be
941 * locked. Otherwise it is a upper level operation. No vnodes
942 * should be locked.
943 */
944 int
945 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
946 {
947 struct mount *mp;
948 int error, mask, prio;
949
950 /*
951 * If a vnode is provided, get and return the mount point that
952 * to which it will write.
953 */
954 if (vp != NULL) {
955 *mpp = vp->v_mount;
956 }
957 if ((mp = *mpp) == NULL)
958 return (0);
959 mp = mp->mnt_leaf;
960 /*
961 * Check on status of suspension.
962 */
963 prio = PUSER - 1;
964 if (flags & V_PCATCH)
965 prio |= PCATCH;
966
967 if ((flags & V_LOWER) == 0)
968 mask = IMNT_SUSPEND;
969 else
970 mask = IMNT_SUSPENDLOW;
971
972 while ((mp->mnt_iflag & mask) != 0) {
973 if ((flags & V_WAIT) == 0)
974 return (EWOULDBLOCK);
975 error = tsleep(&mp->mnt_flag, prio, "suspfs", 0);
976 if (error)
977 return (error);
978 }
979 if (flags & V_SLEEPONLY)
980 return (0);
981 simple_lock(&mp->mnt_slock);
982 if ((flags & V_LOWER) == 0)
983 mp->mnt_writeopcountupper++;
984 else
985 mp->mnt_writeopcountlower++;
986 simple_unlock(&mp->mnt_slock);
987 return (0);
988 }
989
990 /*
991 * Filesystem write operation has completed. If we are suspending and this
992 * operation is the last one, notify the suspender that the suspension is
993 * now in effect.
994 */
995 void
996 vn_finished_write(struct mount *mp, int flags)
997 {
998 if (mp == NULL)
999 return;
1000 mp = mp->mnt_leaf;
1001 simple_lock(&mp->mnt_slock);
1002 if ((flags & V_LOWER) == 0) {
1003 mp->mnt_writeopcountupper--;
1004 if (mp->mnt_writeopcountupper < 0)
1005 printf("vn_finished_write: neg cnt upper=%d\n",
1006 mp->mnt_writeopcountupper);
1007 if ((mp->mnt_iflag & IMNT_SUSPEND) != 0 &&
1008 mp->mnt_writeopcountupper <= 0)
1009 wakeup(&mp->mnt_writeopcountupper);
1010 } else {
1011 mp->mnt_writeopcountlower--;
1012 if (mp->mnt_writeopcountlower < 0)
1013 printf("vn_finished_write: neg cnt lower=%d\n",
1014 mp->mnt_writeopcountlower);
1015 if ((mp->mnt_iflag & IMNT_SUSPENDLOW) != 0 &&
1016 mp->mnt_writeopcountupper <= 0)
1017 wakeup(&mp->mnt_writeopcountlower);
1018 }
1019 simple_unlock(&mp->mnt_slock);
1020 }
1021
1022 void
1023 vn_ra_allocctx(struct vnode *vp)
1024 {
1025 struct uvm_ractx *ra = NULL;
1026
1027 if (vp->v_type != VREG) {
1028 return;
1029 }
1030 if (vp->v_ractx != NULL) {
1031 return;
1032 }
1033 simple_lock(&vp->v_interlock);
1034 if (vp->v_ractx == NULL) {
1035 simple_unlock(&vp->v_interlock);
1036 ra = uvm_ra_allocctx();
1037 simple_lock(&vp->v_interlock);
1038 if (ra != NULL && vp->v_ractx == NULL) {
1039 vp->v_ractx = ra;
1040 ra = NULL;
1041 }
1042 }
1043 simple_unlock(&vp->v_interlock);
1044 if (ra != NULL) {
1045 uvm_ra_freectx(ra);
1046 }
1047 }
1048