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