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