vfs_vnops.c revision 1.135.2.2 1 /* $NetBSD: vfs_vnops.c,v 1.135.2.2 2007/03/21 20:11:54 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.2 2007/03/21 20:11:54 ad Exp $");
41
42 #include "fs_union.h"
43 #include "veriexec.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/ioctl.h>
57 #include <sys/tty.h>
58 #include <sys/poll.h>
59 #include <sys/kauth.h>
60 #include <sys/syslog.h>
61
62 #include <miscfs/specfs/specdev.h>
63
64 #include <uvm/uvm_extern.h>
65 #include <uvm/uvm_readahead.h>
66
67 #ifdef UNION
68 #include <fs/union/union.h>
69 #endif
70
71 #if defined(LKM) || defined(UNION)
72 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
73 #endif
74
75 #if NVERIEXEC > 0
76 #include <sys/verified_exec.h>
77 #endif /* NVERIEXEC > 0 */
78
79 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
80 kauth_cred_t cred, int flags);
81 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
82 kauth_cred_t cred, int flags);
83 static int vn_closefile(struct file *fp, struct lwp *l);
84 static int vn_poll(struct file *fp, int events, struct lwp *l);
85 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
86 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
87 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
88
89 const struct fileops vnops = {
90 vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
91 vn_statfile, vn_closefile, vn_kqfilter
92 };
93
94 /*
95 * Common code for vnode open operations.
96 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
97 */
98 int
99 vn_open(struct nameidata *ndp, int fmode, int cmode)
100 {
101 struct vnode *vp;
102 struct mount *mp = NULL; /* XXX: GCC */
103 struct lwp *l = ndp->ni_cnd.cn_lwp;
104 kauth_cred_t cred = l->l_cred;
105 struct vattr va;
106 int error;
107 pathname_t pn = NULL;
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 } 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 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
149 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
150 vput(ndp->ni_dvp);
151 if ((error = vn_start_write(NULL, &mp,
152 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
153 goto bad2;
154 goto restart;
155 }
156 VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
157 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
158 &ndp->ni_cnd, &va);
159 vn_finished_write(mp, 0);
160 if (error)
161 goto bad2;
162 fmode &= ~O_TRUNC;
163 vp = ndp->ni_vp;
164 } else {
165 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
166 if (ndp->ni_dvp == ndp->ni_vp)
167 vrele(ndp->ni_dvp);
168 else
169 vput(ndp->ni_dvp);
170 ndp->ni_dvp = NULL;
171 vp = ndp->ni_vp;
172 if (fmode & O_EXCL) {
173 error = EEXIST;
174 goto bad;
175 }
176 fmode &= ~O_CREAT;
177 }
178 } else {
179 vp = ndp->ni_vp;
180 }
181 if (vp->v_type == VSOCK) {
182 error = EOPNOTSUPP;
183 goto bad;
184 }
185 if (ndp->ni_vp->v_type == VLNK) {
186 error = EFTYPE;
187 goto bad;
188 }
189
190 if ((fmode & O_CREAT) == 0) {
191 if (fmode & FREAD) {
192 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
193 goto bad;
194 }
195
196 if (fmode & (FWRITE | O_TRUNC)) {
197 if (vp->v_type == VDIR) {
198 error = EISDIR;
199 goto bad;
200 }
201 if ((error = vn_writechk(vp)) != 0 ||
202 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
203 goto bad;
204 }
205 }
206
207 if (fmode & O_TRUNC) {
208 VOP_UNLOCK(vp, 0); /* XXX */
209
210 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
211 vrele(vp);
212 goto bad2;
213 }
214 VOP_LEASE(vp, l, cred, LEASE_WRITE);
215 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
216 VATTR_NULL(&va);
217 va.va_size = 0;
218 error = VOP_SETATTR(vp, &va, cred, l);
219 vn_finished_write(mp, 0);
220 if (error != 0)
221 goto bad;
222 }
223 if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
224 goto bad;
225 if (vp->v_type == VREG &&
226 uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
227 error = EIO;
228 goto bad;
229 }
230 if (fmode & FWRITE)
231 vp->v_writecount++;
232
233 bad:
234 if (error)
235 vput(vp);
236
237 bad2:
238 pathname_put(pn);
239
240 return (error);
241 }
242
243 /*
244 * Check for write permissions on the specified vnode.
245 * Prototype text segments cannot be written.
246 */
247 int
248 vn_writechk(struct vnode *vp)
249 {
250
251 /*
252 * If the vnode is in use as a process's text,
253 * we can't allow writing.
254 */
255 if (vp->v_flag & VTEXT)
256 return (ETXTBSY);
257 return (0);
258 }
259
260 /*
261 * Mark a vnode as having executable mappings.
262 */
263 void
264 vn_markexec(struct vnode *vp)
265 {
266 if ((vp->v_flag & VEXECMAP) == 0) {
267 uvmexp.filepages -= vp->v_uobj.uo_npages;
268 uvmexp.execpages += vp->v_uobj.uo_npages;
269 }
270 vp->v_flag |= VEXECMAP;
271 }
272
273 /*
274 * Mark a vnode as being the text of a process.
275 * Fail if the vnode is currently writable.
276 */
277 int
278 vn_marktext(struct vnode *vp)
279 {
280
281 if (vp->v_writecount != 0) {
282 KASSERT((vp->v_flag & VTEXT) == 0);
283 return (ETXTBSY);
284 }
285 vp->v_flag |= VTEXT;
286 vn_markexec(vp);
287 return (0);
288 }
289
290 /*
291 * Vnode close call
292 *
293 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
294 */
295 int
296 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
297 {
298 int error;
299
300 if (flags & FWRITE)
301 vp->v_writecount--;
302 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
303 error = VOP_CLOSE(vp, flags, cred, l);
304 vput(vp);
305 return (error);
306 }
307
308 /*
309 * Package up an I/O request on a vnode into a uio and do it.
310 */
311 int
312 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
313 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
314 struct lwp *l)
315 {
316 struct uio auio;
317 struct iovec aiov;
318 struct mount *mp = NULL;
319 int error;
320
321 if ((ioflg & IO_NODELOCKED) == 0) {
322 if (rw == UIO_READ) {
323 vn_lock(vp, LK_SHARED | LK_RETRY);
324 } else /* UIO_WRITE */ {
325 if (vp->v_type != VCHR &&
326 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
327 != 0)
328 return (error);
329 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
330 }
331 }
332 auio.uio_iov = &aiov;
333 auio.uio_iovcnt = 1;
334 aiov.iov_base = base;
335 aiov.iov_len = len;
336 auio.uio_resid = len;
337 auio.uio_offset = offset;
338 auio.uio_rw = rw;
339 if (segflg == UIO_SYSSPACE) {
340 UIO_SETUP_SYSSPACE(&auio);
341 } else {
342 auio.uio_vmspace = l->l_proc->p_vmspace;
343 }
344 if (rw == UIO_READ) {
345 error = VOP_READ(vp, &auio, ioflg, cred);
346 } else {
347 error = VOP_WRITE(vp, &auio, ioflg, cred);
348 }
349 if (aresid)
350 *aresid = auio.uio_resid;
351 else
352 if (auio.uio_resid && error == 0)
353 error = EIO;
354 if ((ioflg & IO_NODELOCKED) == 0) {
355 if (rw == UIO_WRITE)
356 vn_finished_write(mp, 0);
357 VOP_UNLOCK(vp, 0);
358 }
359 return (error);
360 }
361
362 int
363 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
364 struct lwp *l, off_t **cookies, int *ncookies)
365 {
366 struct vnode *vp = (struct vnode *)fp->f_data;
367 struct iovec aiov;
368 struct uio auio;
369 int error, eofflag;
370
371 /* Limit the size on any kernel buffers used by VOP_READDIR */
372 count = min(MAXBSIZE, count);
373
374 unionread:
375 if (vp->v_type != VDIR)
376 return (EINVAL);
377 aiov.iov_base = bf;
378 aiov.iov_len = count;
379 auio.uio_iov = &aiov;
380 auio.uio_iovcnt = 1;
381 auio.uio_rw = UIO_READ;
382 if (segflg == UIO_SYSSPACE) {
383 UIO_SETUP_SYSSPACE(&auio);
384 } else {
385 KASSERT(l == curlwp);
386 auio.uio_vmspace = l->l_proc->p_vmspace;
387 }
388 auio.uio_resid = count;
389 vn_lock(vp, LK_SHARED | LK_RETRY);
390 auio.uio_offset = fp->f_offset;
391 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
392 ncookies);
393 mutex_enter(&fp->f_lock);
394 fp->f_offset = auio.uio_offset;
395 mutex_exit(&fp->f_lock);
396 VOP_UNLOCK(vp, 0);
397 if (error)
398 return (error);
399
400 #if defined(UNION) || defined(LKM)
401 if (count == auio.uio_resid && vn_union_readdir_hook) {
402 struct vnode *ovp = vp;
403
404 error = (*vn_union_readdir_hook)(&vp, fp, l);
405 if (error)
406 return (error);
407 if (vp != ovp)
408 goto unionread;
409 }
410 #endif /* UNION || LKM */
411
412 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
413 (vp->v_mount->mnt_flag & MNT_UNION)) {
414 struct vnode *tvp = vp;
415 vp = vp->v_mount->mnt_vnodecovered;
416 VREF(vp);
417 fp->f_data = vp;
418 mutex_enter(&fp->f_lock);
419 fp->f_offset = 0;
420 mutex_exit(&fp->f_lock);
421 vrele(tvp);
422 goto unionread;
423 }
424 *done = count - auio.uio_resid;
425 return error;
426 }
427
428 /*
429 * File table vnode read routine.
430 */
431 static int
432 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
433 int flags)
434 {
435 struct vnode *vp = (struct vnode *)fp->f_data;
436 int count, error, ioflag;
437 struct lwp *l = curlwp;
438
439 VOP_LEASE(vp, l, cred, LEASE_READ);
440 ioflag = IO_ADV_ENCODE(fp->f_advice);
441 if (fp->f_flag & FNONBLOCK)
442 ioflag |= IO_NDELAY;
443 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
444 ioflag |= IO_SYNC;
445 if (fp->f_flag & FALTIO)
446 ioflag |= IO_ALTSEMANTICS;
447 if (fp->f_flag & FDIRECT)
448 ioflag |= IO_DIRECT;
449 vn_lock(vp, LK_SHARED | LK_RETRY);
450 uio->uio_offset = *offset;
451 count = uio->uio_resid;
452 error = VOP_READ(vp, uio, ioflag, cred);
453 if (flags & FOF_UPDATE_OFFSET)
454 *offset += count - uio->uio_resid;
455 VOP_UNLOCK(vp, 0);
456 return (error);
457 }
458
459 /*
460 * File table vnode write routine.
461 */
462 static int
463 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
464 int flags)
465 {
466 struct vnode *vp = (struct vnode *)fp->f_data;
467 struct mount *mp;
468 int count, error, ioflag = IO_UNIT;
469 struct lwp *l = curlwp;
470
471 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
472 ioflag |= IO_APPEND;
473 if (fp->f_flag & FNONBLOCK)
474 ioflag |= IO_NDELAY;
475 if (fp->f_flag & FFSYNC ||
476 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
477 ioflag |= IO_SYNC;
478 else if (fp->f_flag & FDSYNC)
479 ioflag |= IO_DSYNC;
480 if (fp->f_flag & FALTIO)
481 ioflag |= IO_ALTSEMANTICS;
482 if (fp->f_flag & FDIRECT)
483 ioflag |= IO_DIRECT;
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, l, 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 lwp *l)
509 {
510 struct vnode *vp = (struct vnode *)fp->f_data;
511
512 return vn_stat(vp, sb, l);
513 }
514
515 int
516 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
517 {
518 struct vattr va;
519 int error;
520 mode_t mode;
521
522 error = VOP_GETATTR(vp, &va, l->l_cred, l);
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 lwp *l)
578 {
579 struct vnode *vp = ((struct vnode *)fp->f_data);
580 int error;
581
582 error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
583 return (error);
584 }
585
586 /*
587 * File table vnode ioctl routine.
588 */
589 static int
590 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
591 {
592 struct vnode *vp = ((struct vnode *)fp->f_data), *ovp;
593 struct proc *p = l->l_proc;
594 struct vattr vattr;
595 int error;
596
597 switch (vp->v_type) {
598
599 case VREG:
600 case VDIR:
601 if (com == FIONREAD) {
602 error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
603 if (error)
604 return (error);
605 *(int *)data = vattr.va_size - fp->f_offset;
606 return (0);
607 }
608 if ((com == FIONWRITE) || (com == FIONSPACE)) {
609 /*
610 * Files don't have send queues, so there never
611 * are any bytes in them, nor is there any
612 * open space in them.
613 */
614 *(int *)data = 0;
615 return (0);
616 }
617 if (com == FIOGETBMAP) {
618 daddr_t *block;
619
620 if (*(daddr_t *)data < 0)
621 return (EINVAL);
622 block = (daddr_t *)data;
623 return (VOP_BMAP(vp, *block, NULL, block, NULL));
624 }
625 if (com == OFIOGETBMAP) {
626 daddr_t ibn, obn;
627
628 if (*(int32_t *)data < 0)
629 return (EINVAL);
630 ibn = (daddr_t)*(int32_t *)data;
631 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
632 *(int32_t *)data = (int32_t)obn;
633 return error;
634 }
635 if (com == FIONBIO || com == FIOASYNC) /* XXX */
636 return (0); /* XXX */
637 /* fall into ... */
638 case VFIFO:
639 case VCHR:
640 case VBLK:
641 error = VOP_IOCTL(vp, com, data, fp->f_flag,
642 l->l_cred, l);
643 if (error == 0 && com == TIOCSCTTY) {
644 VREF(vp);
645 mutex_enter(&proclist_lock);
646 ovp = p->p_session->s_ttyvp;
647 p->p_session->s_ttyvp = vp;
648 mutex_exit(&proclist_lock);
649 if (ovp != NULL)
650 vrele(ovp);
651 }
652 return (error);
653
654 default:
655 return (EPASSTHROUGH);
656 }
657 }
658
659 /*
660 * File table vnode poll routine.
661 */
662 static int
663 vn_poll(struct file *fp, int events, struct lwp *l)
664 {
665
666 return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
667 }
668
669 /*
670 * File table vnode kqfilter routine.
671 */
672 int
673 vn_kqfilter(struct file *fp, struct knote *kn)
674 {
675
676 return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
677 }
678
679 /*
680 * Check that the vnode is still valid, and if so
681 * acquire requested lock.
682 */
683 int
684 vn_lock(struct vnode *vp, int flags)
685 {
686 int error;
687
688 #if 0
689 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
690 || (vp->v_flag & VONWORKLST) != 0);
691 #endif
692 KASSERT((flags &
693 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
694 LK_SETRECURSE|LK_CANRECURSE))
695 == 0);
696
697 do {
698 if ((flags & LK_INTERLOCK) == 0)
699 mutex_enter(&vp->v_interlock);
700 if (vp->v_flag & VXLOCK) {
701 if (flags & LK_NOWAIT) {
702 mutex_exit(&vp->v_interlock);
703 return EBUSY;
704 }
705 vp->v_flag |= VXWANT;
706 cv_wait(&vp->v_cv, &vp->v_interlock);
707 mutex_exit(&vp->v_interlock);
708 error = ENOENT;
709 } else {
710 error = VOP_LOCK(vp,
711 (flags & ~LK_RETRY) | LK_INTERLOCK);
712 if (error == 0 || error == EDEADLK || error == EBUSY)
713 return (error);
714 }
715 flags &= ~LK_INTERLOCK;
716 } while (flags & LK_RETRY);
717 return (error);
718 }
719
720 /*
721 * File table vnode close routine.
722 */
723 static int
724 vn_closefile(struct file *fp, struct lwp *l)
725 {
726
727 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
728 fp->f_cred, l));
729 }
730
731 /*
732 * Enable LK_CANRECURSE on lock. Return prior status.
733 */
734 u_int
735 vn_setrecurse(struct vnode *vp)
736 {
737 struct lock *lkp = &vp->v_lock;
738 u_int retval = lkp->lk_flags & LK_CANRECURSE;
739
740 lkp->lk_flags |= LK_CANRECURSE;
741 return retval;
742 }
743
744 /*
745 * Called when done with locksetrecurse.
746 */
747 void
748 vn_restorerecurse(struct vnode *vp, u_int flags)
749 {
750 struct lock *lkp = &vp->v_lock;
751
752 lkp->lk_flags &= ~LK_CANRECURSE;
753 lkp->lk_flags |= flags;
754 }
755
756 int
757 vn_cow_establish(struct vnode *vp,
758 int (*func)(void *, struct buf *), void *cookie)
759 {
760 struct spec_cow_entry *e;
761
762 MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
763 M_DEVBUF, M_WAITOK);
764 e->ce_func = func;
765 e->ce_cookie = cookie;
766
767 mutex_enter(&vp->v_spec_cow_lock);
768 vp->v_spec_cow_req++;
769 while (vp->v_spec_cow_count > 0)
770 mtsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
771 &vp->v_spec_cow_lock);
772
773 SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
774
775 vp->v_spec_cow_req--;
776 if (vp->v_spec_cow_req == 0)
777 wakeup(&vp->v_spec_cow_req);
778 mutex_exit(&vp->v_spec_cow_lock);
779
780 return 0;
781 }
782
783 int
784 vn_cow_disestablish(struct vnode *vp,
785 int (*func)(void *, struct buf *), void *cookie)
786 {
787 struct spec_cow_entry *e;
788
789 mutex_enter(&vp->v_spec_cow_lock);
790 vp->v_spec_cow_req++;
791 while (vp->v_spec_cow_count > 0)
792 mtsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
793 &vp->v_spec_cow_lock);
794
795 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
796 if (e->ce_func == func && e->ce_cookie == cookie) {
797 SLIST_REMOVE(&vp->v_spec_cow_head, e,
798 spec_cow_entry, ce_list);
799 FREE(e, M_DEVBUF);
800 break;
801 }
802
803 vp->v_spec_cow_req--;
804 if (vp->v_spec_cow_req == 0)
805 wakeup(&vp->v_spec_cow_req);
806 mutex_exit(&vp->v_spec_cow_lock);
807
808 return e ? 0 : EINVAL;
809 }
810
811 /*
812 * Simplified in-kernel wrapper calls for extended attribute access.
813 * Both calls pass in a NULL credential, authorizing a "kernel" access.
814 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
815 */
816 int
817 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
818 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
819 {
820 struct uio auio;
821 struct iovec aiov;
822 int error;
823
824 aiov.iov_len = *buflen;
825 aiov.iov_base = bf;
826
827 auio.uio_iov = &aiov;
828 auio.uio_iovcnt = 1;
829 auio.uio_rw = UIO_READ;
830 auio.uio_offset = 0;
831 auio.uio_resid = *buflen;
832 UIO_SETUP_SYSSPACE(&auio);
833
834 if ((ioflg & IO_NODELOCKED) == 0)
835 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
836
837 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
838 l);
839
840 if ((ioflg & IO_NODELOCKED) == 0)
841 VOP_UNLOCK(vp, 0);
842
843 if (error == 0)
844 *buflen = *buflen - auio.uio_resid;
845
846 return (error);
847 }
848
849 /*
850 * XXX Failure mode if partially written?
851 */
852 int
853 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
854 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
855 {
856 struct uio auio;
857 struct iovec aiov;
858 struct mount *mp = NULL; /* XXX: GCC */
859 int error;
860
861 aiov.iov_len = buflen;
862 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
863
864 auio.uio_iov = &aiov;
865 auio.uio_iovcnt = 1;
866 auio.uio_rw = UIO_WRITE;
867 auio.uio_offset = 0;
868 auio.uio_resid = buflen;
869 UIO_SETUP_SYSSPACE(&auio);
870
871 if ((ioflg & IO_NODELOCKED) == 0) {
872 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
873 return (error);
874 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
875 }
876
877 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
878
879 if ((ioflg & IO_NODELOCKED) == 0) {
880 vn_finished_write(mp, 0);
881 VOP_UNLOCK(vp, 0);
882 }
883
884 return (error);
885 }
886
887 int
888 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
889 const char *attrname, struct lwp *l)
890 {
891 struct mount *mp = NULL; /* XXX: GCC */
892 int error;
893
894 if ((ioflg & IO_NODELOCKED) == 0) {
895 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
896 return (error);
897 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
898 }
899
900 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
901 if (error == EOPNOTSUPP)
902 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
903 NULL, l);
904
905 if ((ioflg & IO_NODELOCKED) == 0) {
906 vn_finished_write(mp, 0);
907 VOP_UNLOCK(vp, 0);
908 }
909
910 return (error);
911 }
912
913 /*
914 * OBSOLETE -- this function will be removed in the near future!
915 *
916 * Preparing to start a filesystem write operation. If the operation is
917 * permitted, then we bump the count of operations in progress and
918 * proceed. If a suspend request is in progress, we wait until the
919 * suspension is over, and then proceed.
920 * V_PCATCH adds PCATCH to the tsleep flags.
921 * V_WAIT waits until suspension is over. Otherwise returns EWOULDBLOCK.
922 * V_SLEEPONLY wait, but do not bump the operations count.
923 * V_LOWER this is a lower level operation. No further vnodes should be
924 * locked. Otherwise it is a upper level operation. No vnodes
925 * should be locked.
926 */
927 int
928 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
929 {
930
931 /*
932 * If a vnode is provided, get and return the mount point that
933 * to which it will write.
934 */
935 if (vp != NULL) {
936 *mpp = vp->v_mount;
937 }
938 return 0;
939 }
940
941 /*
942 * OBSOLETE -- this function will be removed in the near future!
943 *
944 * Filesystem write operation has completed. If we are suspending and this
945 * operation is the last one, notify the suspender that the suspension is
946 * now in effect.
947 */
948 void
949 vn_finished_write(struct mount *mp, int flags)
950 {
951 }
952
953 void
954 vn_ra_allocctx(struct vnode *vp)
955 {
956 struct uvm_ractx *ra = NULL;
957
958 if (vp->v_type != VREG) {
959 return;
960 }
961 if (vp->v_ractx != NULL) {
962 return;
963 }
964 mutex_enter(&vp->v_interlock);
965 if (vp->v_ractx == NULL) {
966 mutex_exit(&vp->v_interlock);
967 ra = uvm_ra_allocctx();
968 mutex_enter(&vp->v_interlock);
969 if (ra != NULL && vp->v_ractx == NULL) {
970 vp->v_ractx = ra;
971 ra = NULL;
972 }
973 }
974 mutex_exit(&vp->v_interlock);
975 if (ra != NULL) {
976 uvm_ra_freectx(ra);
977 }
978 }
979