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