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