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