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