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