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