vfs_vnops.c revision 1.132 1 /* $NetBSD: vfs_vnops.c,v 1.132 2007/02/09 21:55:32 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.132 2007/02/09 21:55:32 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, caddr_t 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 fp->f_offset = auio.uio_offset;
394 VOP_UNLOCK(vp, 0);
395 if (error)
396 return (error);
397
398 #if defined(UNION) || defined(LKM)
399 if (count == auio.uio_resid && vn_union_readdir_hook) {
400 struct vnode *ovp = vp;
401
402 error = (*vn_union_readdir_hook)(&vp, fp, l);
403 if (error)
404 return (error);
405 if (vp != ovp)
406 goto unionread;
407 }
408 #endif /* UNION || LKM */
409
410 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
411 (vp->v_mount->mnt_flag & MNT_UNION)) {
412 struct vnode *tvp = vp;
413 vp = vp->v_mount->mnt_vnodecovered;
414 VREF(vp);
415 fp->f_data = vp;
416 fp->f_offset = 0;
417 vrele(tvp);
418 goto unionread;
419 }
420 *done = count - auio.uio_resid;
421 return error;
422 }
423
424 /*
425 * File table vnode read routine.
426 */
427 static int
428 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
429 int flags)
430 {
431 struct vnode *vp = (struct vnode *)fp->f_data;
432 int count, error, ioflag;
433 struct lwp *l = curlwp;
434
435 VOP_LEASE(vp, l, cred, LEASE_READ);
436 ioflag = IO_ADV_ENCODE(fp->f_advice);
437 if (fp->f_flag & FNONBLOCK)
438 ioflag |= IO_NDELAY;
439 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
440 ioflag |= IO_SYNC;
441 if (fp->f_flag & FALTIO)
442 ioflag |= IO_ALTSEMANTICS;
443 if (fp->f_flag & FDIRECT)
444 ioflag |= IO_DIRECT;
445 vn_lock(vp, LK_SHARED | LK_RETRY);
446 uio->uio_offset = *offset;
447 count = uio->uio_resid;
448 error = VOP_READ(vp, uio, ioflag, cred);
449 if (flags & FOF_UPDATE_OFFSET)
450 *offset += count - uio->uio_resid;
451 VOP_UNLOCK(vp, 0);
452 return (error);
453 }
454
455 /*
456 * File table vnode write routine.
457 */
458 static int
459 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
460 int flags)
461 {
462 struct vnode *vp = (struct vnode *)fp->f_data;
463 struct mount *mp;
464 int count, error, ioflag = IO_UNIT;
465 struct lwp *l = curlwp;
466
467 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
468 ioflag |= IO_APPEND;
469 if (fp->f_flag & FNONBLOCK)
470 ioflag |= IO_NDELAY;
471 if (fp->f_flag & FFSYNC ||
472 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
473 ioflag |= IO_SYNC;
474 else if (fp->f_flag & FDSYNC)
475 ioflag |= IO_DSYNC;
476 if (fp->f_flag & FALTIO)
477 ioflag |= IO_ALTSEMANTICS;
478 if (fp->f_flag & FDIRECT)
479 ioflag |= IO_DIRECT;
480 mp = NULL;
481 if (vp->v_type != VCHR &&
482 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
483 return (error);
484 VOP_LEASE(vp, l, cred, LEASE_WRITE);
485 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
486 uio->uio_offset = *offset;
487 count = uio->uio_resid;
488 error = VOP_WRITE(vp, uio, ioflag, cred);
489 if (flags & FOF_UPDATE_OFFSET) {
490 if (ioflag & IO_APPEND)
491 *offset = uio->uio_offset;
492 else
493 *offset += count - uio->uio_resid;
494 }
495 VOP_UNLOCK(vp, 0);
496 vn_finished_write(mp, 0);
497 return (error);
498 }
499
500 /*
501 * File table vnode stat routine.
502 */
503 static int
504 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
505 {
506 struct vnode *vp = (struct vnode *)fp->f_data;
507
508 return vn_stat(vp, sb, l);
509 }
510
511 int
512 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
513 {
514 struct vattr va;
515 int error;
516 mode_t mode;
517
518 error = VOP_GETATTR(vp, &va, l->l_cred, l);
519 if (error)
520 return (error);
521 /*
522 * Copy from vattr table
523 */
524 sb->st_dev = va.va_fsid;
525 sb->st_ino = va.va_fileid;
526 mode = va.va_mode;
527 switch (vp->v_type) {
528 case VREG:
529 mode |= S_IFREG;
530 break;
531 case VDIR:
532 mode |= S_IFDIR;
533 break;
534 case VBLK:
535 mode |= S_IFBLK;
536 break;
537 case VCHR:
538 mode |= S_IFCHR;
539 break;
540 case VLNK:
541 mode |= S_IFLNK;
542 break;
543 case VSOCK:
544 mode |= S_IFSOCK;
545 break;
546 case VFIFO:
547 mode |= S_IFIFO;
548 break;
549 default:
550 return (EBADF);
551 };
552 sb->st_mode = mode;
553 sb->st_nlink = va.va_nlink;
554 sb->st_uid = va.va_uid;
555 sb->st_gid = va.va_gid;
556 sb->st_rdev = va.va_rdev;
557 sb->st_size = va.va_size;
558 sb->st_atimespec = va.va_atime;
559 sb->st_mtimespec = va.va_mtime;
560 sb->st_ctimespec = va.va_ctime;
561 sb->st_birthtimespec = va.va_birthtime;
562 sb->st_blksize = va.va_blocksize;
563 sb->st_flags = va.va_flags;
564 sb->st_gen = 0;
565 sb->st_blocks = va.va_bytes / S_BLKSIZE;
566 return (0);
567 }
568
569 /*
570 * File table vnode fcntl routine.
571 */
572 static int
573 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
574 {
575 struct vnode *vp = ((struct vnode *)fp->f_data);
576 int error;
577
578 error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
579 return (error);
580 }
581
582 /*
583 * File table vnode ioctl routine.
584 */
585 static int
586 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
587 {
588 struct vnode *vp = ((struct vnode *)fp->f_data), *ovp;
589 struct proc *p = l->l_proc;
590 struct vattr vattr;
591 int error;
592
593 switch (vp->v_type) {
594
595 case VREG:
596 case VDIR:
597 if (com == FIONREAD) {
598 error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
599 if (error)
600 return (error);
601 *(int *)data = vattr.va_size - fp->f_offset;
602 return (0);
603 }
604 if ((com == FIONWRITE) || (com == FIONSPACE)) {
605 /*
606 * Files don't have send queues, so there never
607 * are any bytes in them, nor is there any
608 * open space in them.
609 */
610 *(int *)data = 0;
611 return (0);
612 }
613 if (com == FIOGETBMAP) {
614 daddr_t *block;
615
616 if (*(daddr_t *)data < 0)
617 return (EINVAL);
618 block = (daddr_t *)data;
619 return (VOP_BMAP(vp, *block, NULL, block, NULL));
620 }
621 if (com == OFIOGETBMAP) {
622 daddr_t ibn, obn;
623
624 if (*(int32_t *)data < 0)
625 return (EINVAL);
626 ibn = (daddr_t)*(int32_t *)data;
627 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
628 *(int32_t *)data = (int32_t)obn;
629 return error;
630 }
631 if (com == FIONBIO || com == FIOASYNC) /* XXX */
632 return (0); /* XXX */
633 /* fall into ... */
634 case VFIFO:
635 case VCHR:
636 case VBLK:
637 error = VOP_IOCTL(vp, com, data, fp->f_flag,
638 l->l_cred, l);
639 if (error == 0 && com == TIOCSCTTY) {
640 VREF(vp);
641 rw_enter(&proclist_lock, RW_WRITER);
642 ovp = p->p_session->s_ttyvp;
643 p->p_session->s_ttyvp = vp;
644 rw_exit(&proclist_lock);
645 if (ovp != NULL)
646 vrele(ovp);
647 }
648 return (error);
649
650 default:
651 return (EPASSTHROUGH);
652 }
653 }
654
655 /*
656 * File table vnode poll routine.
657 */
658 static int
659 vn_poll(struct file *fp, int events, struct lwp *l)
660 {
661
662 return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
663 }
664
665 /*
666 * File table vnode kqfilter routine.
667 */
668 int
669 vn_kqfilter(struct file *fp, struct knote *kn)
670 {
671
672 return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
673 }
674
675 /*
676 * Check that the vnode is still valid, and if so
677 * acquire requested lock.
678 */
679 int
680 vn_lock(struct vnode *vp, int flags)
681 {
682 int error;
683
684 #if 0
685 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
686 || (vp->v_flag & VONWORKLST) != 0);
687 #endif
688 KASSERT((flags &
689 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
690 LK_SETRECURSE|LK_CANRECURSE))
691 == 0);
692
693 do {
694 if ((flags & LK_INTERLOCK) == 0)
695 simple_lock(&vp->v_interlock);
696 if (vp->v_flag & VXLOCK) {
697 if (flags & LK_NOWAIT) {
698 simple_unlock(&vp->v_interlock);
699 return EBUSY;
700 }
701 vp->v_flag |= VXWANT;
702 ltsleep(vp, PINOD | PNORELOCK,
703 "vn_lock", 0, &vp->v_interlock);
704 error = ENOENT;
705 } else {
706 error = VOP_LOCK(vp,
707 (flags & ~LK_RETRY) | LK_INTERLOCK);
708 if (error == 0 || error == EDEADLK || error == EBUSY)
709 return (error);
710 }
711 flags &= ~LK_INTERLOCK;
712 } while (flags & LK_RETRY);
713 return (error);
714 }
715
716 /*
717 * File table vnode close routine.
718 */
719 static int
720 vn_closefile(struct file *fp, struct lwp *l)
721 {
722
723 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
724 fp->f_cred, l));
725 }
726
727 /*
728 * Enable LK_CANRECURSE on lock. Return prior status.
729 */
730 u_int
731 vn_setrecurse(struct vnode *vp)
732 {
733 struct lock *lkp = &vp->v_lock;
734 u_int retval = lkp->lk_flags & LK_CANRECURSE;
735
736 lkp->lk_flags |= LK_CANRECURSE;
737 return retval;
738 }
739
740 /*
741 * Called when done with locksetrecurse.
742 */
743 void
744 vn_restorerecurse(struct vnode *vp, u_int flags)
745 {
746 struct lock *lkp = &vp->v_lock;
747
748 lkp->lk_flags &= ~LK_CANRECURSE;
749 lkp->lk_flags |= flags;
750 }
751
752 int
753 vn_cow_establish(struct vnode *vp,
754 int (*func)(void *, struct buf *), void *cookie)
755 {
756 int s;
757 struct spec_cow_entry *e;
758
759 MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
760 M_DEVBUF, M_WAITOK);
761 e->ce_func = func;
762 e->ce_cookie = cookie;
763
764 SPEC_COW_LOCK(vp->v_specinfo, s);
765 vp->v_spec_cow_req++;
766 while (vp->v_spec_cow_count > 0)
767 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
768 &vp->v_spec_cow_slock);
769
770 SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
771
772 vp->v_spec_cow_req--;
773 if (vp->v_spec_cow_req == 0)
774 wakeup(&vp->v_spec_cow_req);
775 SPEC_COW_UNLOCK(vp->v_specinfo, s);
776
777 return 0;
778 }
779
780 int
781 vn_cow_disestablish(struct vnode *vp,
782 int (*func)(void *, struct buf *), void *cookie)
783 {
784 int s;
785 struct spec_cow_entry *e;
786
787 SPEC_COW_LOCK(vp->v_specinfo, s);
788 vp->v_spec_cow_req++;
789 while (vp->v_spec_cow_count > 0)
790 ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
791 &vp->v_spec_cow_slock);
792
793 SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
794 if (e->ce_func == func && e->ce_cookie == cookie) {
795 SLIST_REMOVE(&vp->v_spec_cow_head, e,
796 spec_cow_entry, ce_list);
797 FREE(e, M_DEVBUF);
798 break;
799 }
800
801 vp->v_spec_cow_req--;
802 if (vp->v_spec_cow_req == 0)
803 wakeup(&vp->v_spec_cow_req);
804 SPEC_COW_UNLOCK(vp->v_specinfo, s);
805
806 return e ? 0 : EINVAL;
807 }
808
809 /*
810 * Simplified in-kernel wrapper calls for extended attribute access.
811 * Both calls pass in a NULL credential, authorizing a "kernel" access.
812 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
813 */
814 int
815 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
816 const char *attrname, size_t *buflen, void *bf, struct lwp *l)
817 {
818 struct uio auio;
819 struct iovec aiov;
820 int error;
821
822 aiov.iov_len = *buflen;
823 aiov.iov_base = bf;
824
825 auio.uio_iov = &aiov;
826 auio.uio_iovcnt = 1;
827 auio.uio_rw = UIO_READ;
828 auio.uio_offset = 0;
829 auio.uio_resid = *buflen;
830 UIO_SETUP_SYSSPACE(&auio);
831
832 if ((ioflg & IO_NODELOCKED) == 0)
833 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
834
835 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
836 l);
837
838 if ((ioflg & IO_NODELOCKED) == 0)
839 VOP_UNLOCK(vp, 0);
840
841 if (error == 0)
842 *buflen = *buflen - auio.uio_resid;
843
844 return (error);
845 }
846
847 /*
848 * XXX Failure mode if partially written?
849 */
850 int
851 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
852 const char *attrname, size_t buflen, const void *bf, struct lwp *l)
853 {
854 struct uio auio;
855 struct iovec aiov;
856 struct mount *mp = NULL; /* XXX: GCC */
857 int error;
858
859 aiov.iov_len = buflen;
860 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */
861
862 auio.uio_iov = &aiov;
863 auio.uio_iovcnt = 1;
864 auio.uio_rw = UIO_WRITE;
865 auio.uio_offset = 0;
866 auio.uio_resid = buflen;
867 UIO_SETUP_SYSSPACE(&auio);
868
869 if ((ioflg & IO_NODELOCKED) == 0) {
870 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
871 return (error);
872 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
873 }
874
875 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
876
877 if ((ioflg & IO_NODELOCKED) == 0) {
878 vn_finished_write(mp, 0);
879 VOP_UNLOCK(vp, 0);
880 }
881
882 return (error);
883 }
884
885 int
886 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
887 const char *attrname, struct lwp *l)
888 {
889 struct mount *mp = NULL; /* XXX: GCC */
890 int error;
891
892 if ((ioflg & IO_NODELOCKED) == 0) {
893 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
894 return (error);
895 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
896 }
897
898 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
899 if (error == EOPNOTSUPP)
900 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
901 NULL, l);
902
903 if ((ioflg & IO_NODELOCKED) == 0) {
904 vn_finished_write(mp, 0);
905 VOP_UNLOCK(vp, 0);
906 }
907
908 return (error);
909 }
910
911 /*
912 * Preparing to start a filesystem write operation. If the operation is
913 * permitted, then we bump the count of operations in progress and
914 * proceed. If a suspend request is in progress, we wait until the
915 * suspension is over, and then proceed.
916 * V_PCATCH adds PCATCH to the tsleep flags.
917 * V_WAIT waits until suspension is over. Otherwise returns EWOULDBLOCK.
918 * V_SLEEPONLY wait, but do not bump the operations count.
919 * V_LOWER this is a lower level operation. No further vnodes should be
920 * locked. Otherwise it is a upper level operation. No vnodes
921 * should be locked.
922 */
923 int
924 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
925 {
926 struct mount *mp;
927 int error, mask, prio;
928
929 /*
930 * If a vnode is provided, get and return the mount point that
931 * to which it will write.
932 */
933 if (vp != NULL) {
934 *mpp = vp->v_mount;
935 }
936 if ((mp = *mpp) == NULL)
937 return (0);
938 mp = mp->mnt_leaf;
939 if ((mp->mnt_iflag & IMNT_HAS_TRANS) != 0)
940 return 0;
941 /*
942 * Check on status of suspension.
943 */
944 prio = PUSER - 1;
945 if (flags & V_PCATCH)
946 prio |= PCATCH;
947
948 if ((flags & V_LOWER) == 0)
949 mask = IMNT_SUSPEND;
950 else
951 mask = IMNT_SUSPENDLOW;
952
953 while ((mp->mnt_iflag & mask) != 0) {
954 if ((flags & V_WAIT) == 0)
955 return (EWOULDBLOCK);
956 error = tsleep(&mp->mnt_flag, prio, "suspfs", 0);
957 if (error)
958 return (error);
959 }
960 if (flags & V_SLEEPONLY)
961 return (0);
962 simple_lock(&mp->mnt_slock);
963 if ((flags & V_LOWER) == 0)
964 mp->mnt_writeopcountupper++;
965 else
966 mp->mnt_writeopcountlower++;
967 simple_unlock(&mp->mnt_slock);
968 return (0);
969 }
970
971 /*
972 * Filesystem write operation has completed. If we are suspending and this
973 * operation is the last one, notify the suspender that the suspension is
974 * now in effect.
975 */
976 void
977 vn_finished_write(struct mount *mp, int flags)
978 {
979 if (mp == NULL)
980 return;
981 mp = mp->mnt_leaf;
982 if ((mp->mnt_iflag & IMNT_HAS_TRANS) != 0)
983 return;
984 simple_lock(&mp->mnt_slock);
985 if ((flags & V_LOWER) == 0) {
986 mp->mnt_writeopcountupper--;
987 if (mp->mnt_writeopcountupper < 0)
988 printf("vn_finished_write: neg cnt upper=%d\n",
989 mp->mnt_writeopcountupper);
990 if ((mp->mnt_iflag & IMNT_SUSPEND) != 0 &&
991 mp->mnt_writeopcountupper <= 0)
992 wakeup(&mp->mnt_writeopcountupper);
993 } else {
994 mp->mnt_writeopcountlower--;
995 if (mp->mnt_writeopcountlower < 0)
996 printf("vn_finished_write: neg cnt lower=%d\n",
997 mp->mnt_writeopcountlower);
998 if ((mp->mnt_iflag & IMNT_SUSPENDLOW) != 0 &&
999 mp->mnt_writeopcountupper <= 0)
1000 wakeup(&mp->mnt_writeopcountlower);
1001 }
1002 simple_unlock(&mp->mnt_slock);
1003 }
1004
1005 void
1006 vn_ra_allocctx(struct vnode *vp)
1007 {
1008 struct uvm_ractx *ra = NULL;
1009
1010 if (vp->v_type != VREG) {
1011 return;
1012 }
1013 if (vp->v_ractx != NULL) {
1014 return;
1015 }
1016 simple_lock(&vp->v_interlock);
1017 if (vp->v_ractx == NULL) {
1018 simple_unlock(&vp->v_interlock);
1019 ra = uvm_ra_allocctx();
1020 simple_lock(&vp->v_interlock);
1021 if (ra != NULL && vp->v_ractx == NULL) {
1022 vp->v_ractx = ra;
1023 ra = NULL;
1024 }
1025 }
1026 simple_unlock(&vp->v_interlock);
1027 if (ra != NULL) {
1028 uvm_ra_freectx(ra);
1029 }
1030 }
1031