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