]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_vnops.c
40177b401cd36e39de8a8a6566c68cf032e18e7e
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
74 #include <sys/mount.h>
75 #include <sys/namei.h>
76 #include <sys/vnode.h>
77 #include <sys/ioctl.h>
83 #include <vm/vm_kern.h>
85 #include <miscfs/specfs/specdev.h>
87 static int vn_closefile
__P((struct file
*fp
, struct proc
*p
));
88 static int vn_ioctl
__P((struct file
*fp
, u_long com
, caddr_t data
,
90 static int vn_read
__P((struct file
*fp
, struct uio
*uio
,
91 struct ucred
*cred
, int flags
, struct proc
*p
));
92 static int vn_write
__P((struct file
*fp
, struct uio
*uio
,
93 struct ucred
*cred
, int flags
, struct proc
*p
));
94 static int vn_select
__P(( struct file
*fp
, int which
, void * wql
,
97 struct fileops vnops
=
98 { vn_read
, vn_write
, vn_ioctl
, vn_select
, vn_closefile
};
101 * Common code for vnode open operations.
102 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
105 vn_open(ndp
, fmode
, cmode
)
106 register struct nameidata
*ndp
;
109 register struct vnode
*vp
;
110 register struct proc
*p
= ndp
->ni_cnd
.cn_proc
;
111 register struct ucred
*cred
= p
->p_ucred
;
113 struct vattr
*vap
= &vat
;
117 if (fmode
& O_CREAT
) {
118 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
119 ndp
->ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
120 if ((fmode
& O_EXCL
) == 0)
121 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
123 if (error
= namei(ndp
))
125 if (ndp
->ni_vp
== NULL
) {
128 vap
->va_mode
= cmode
;
130 vap
->va_vaflags
|= VA_EXCLUSIVE
;
131 VOP_LEASE(ndp
->ni_dvp
, p
, cred
, LEASE_WRITE
);
132 if (error
= VOP_CREATE(ndp
->ni_dvp
, &ndp
->ni_vp
,
138 VOP_ABORTOP(ndp
->ni_dvp
, &ndp
->ni_cnd
);
139 if (ndp
->ni_dvp
== ndp
->ni_vp
)
145 if (fmode
& O_EXCL
) {
152 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
153 ndp
->ni_cnd
.cn_flags
= FOLLOW
| LOCKLEAF
;
154 if (error
= namei(ndp
))
158 if (vp
->v_type
== VSOCK
) {
164 if (UBCINFOMISSING(vp
))
165 panic("vn_open: ubc_info_init");
166 #endif /* DIAGNOSTIC */
168 if (UBCINFOEXISTS(vp
) && ((didhold
= ubc_hold(vp
)) == 0)) {
173 if ((fmode
& O_CREAT
) == 0) {
174 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
176 if (vp
->v_type
== VDIR
)
179 err
= vn_writechk(vp
);
180 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
182 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
,
185 } else if (fmode
& FREAD
) {
186 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
188 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
189 if (vp
->v_type
== VDIR
) {
193 if ((error
= vn_writechk(vp
)) ||
194 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
198 if (fmode
& O_TRUNC
) {
199 VOP_UNLOCK(vp
, 0, p
); /* XXX */
200 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
201 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
204 if (error
= VOP_SETATTR(vp
, vap
, cred
, p
))
208 if (error
= VOP_OPEN(vp
, fmode
, cred
, p
)) {
213 if (++vp
->v_writecount
<= 0)
214 panic("vn_open: v_writecount");
217 VOP_UNLOCK(vp
, 0, p
);
225 * Check for write permissions on the specified vnode.
226 * Prototype text segments cannot be written.
230 register struct vnode
*vp
;
234 * If there's shared text associated with
235 * the vnode, try to free it up once. If
236 * we fail, we can't allow writing.
239 /* XXXXX Not sure we need this */
240 if (vp
->v_flag
& VTEXT
)
250 vn_close(vp
, flags
, cred
, p
)
251 register struct vnode
*vp
;
260 error
= VOP_CLOSE(vp
, flags
, cred
, p
);
267 * Package up an I/O request on a vnode into a uio and do it.
270 vn_rdwr(rw
, vp
, base
, len
, offset
, segflg
, ioflg
, cred
, aresid
, p
)
287 if ((ioflg
& IO_NODELOCKED
) == 0)
288 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
289 auio
.uio_iov
= &aiov
;
291 aiov
.iov_base
= base
;
293 auio
.uio_resid
= len
;
294 auio
.uio_offset
= offset
;
295 auio
.uio_segflg
= segflg
;
300 error
= VOP_READ(vp
, &auio
, ioflg
, cred
);
302 error
= VOP_WRITE(vp
, &auio
, ioflg
, cred
);
305 *aresid
= auio
.uio_resid
;
307 if (auio
.uio_resid
&& error
== 0)
309 if ((ioflg
& IO_NODELOCKED
) == 0)
310 VOP_UNLOCK(vp
, 0, p
);
315 * File table vnode read routine.
318 vn_read(fp
, uio
, cred
, flags
, p
)
329 if (p
!= uio
->uio_procp
)
330 panic("vn_read: uio_procp does not match p");
332 vp
= (struct vnode
*)fp
->f_data
;
334 if (fp
->f_flag
& FNONBLOCK
)
336 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
337 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
340 if ((flags
& FOF_OFFSET
) == 0)
341 uio
->uio_offset
= fp
->f_offset
;
342 count
= uio
->uio_resid
;
344 if(UBCINFOEXISTS(vp
)) {
345 memory_object_t pager
;
348 kern_return_t kr
= KERN_SUCCESS
;
349 kern_return_t ret
= KERN_SUCCESS
;
352 pager
= (memory_object_t
)ubc_getpager(vp
);
353 file_off
= uio
->uio_offset
;
355 count
= uio
->uio_iovcnt
;
357 kr
= vm_conflict_check(current_map(),
358 (vm_offset_t
)iov
->iov_base
, iov
->iov_len
,
360 if(kr
== KERN_ALREADY_WAITING
) {
361 if((count
!= uio
->uio_iovcnt
) &&
362 (ret
!= KERN_ALREADY_WAITING
)) {
366 ret
= KERN_ALREADY_WAITING
;
367 } else if (kr
!= KERN_SUCCESS
) {
375 file_off
+= iov
->iov_len
;
379 if(ret
== KERN_ALREADY_WAITING
) {
381 if ((flags
& FOF_OFFSET
) == 0)
383 count
- uio
->uio_resid
;
388 error
= VOP_READ(vp
, uio
, ioflag
, cred
);
389 if ((flags
& FOF_OFFSET
) == 0)
390 fp
->f_offset
+= count
- uio
->uio_resid
;
392 VOP_UNLOCK(vp
, 0, p
);
398 * File table vnode write routine.
401 vn_write(fp
, uio
, cred
, flags
, p
)
412 if (p
!= uio
->uio_procp
)
413 panic("vn_write: uio_procp does not match p");
415 vp
= (struct vnode
*)fp
->f_data
;
417 if (vp
->v_type
== VREG
)
419 if (vp
->v_type
== VREG
&& (fp
->f_flag
& O_APPEND
))
421 if (fp
->f_flag
& FNONBLOCK
)
423 if ((fp
->f_flag
& O_FSYNC
) ||
424 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
)))
426 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
427 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
430 if ((flags
& FOF_OFFSET
) == 0) {
431 uio
->uio_offset
= fp
->f_offset
;
432 count
= uio
->uio_resid
;
435 if(UBCINFOEXISTS(vp
)) {
436 memory_object_t pager
;
439 kern_return_t kr
= KERN_SUCCESS
;
440 kern_return_t ret
= KERN_SUCCESS
;
443 pager
= (memory_object_t
)ubc_getpager(vp
);
444 file_off
= uio
->uio_offset
;
446 count
= uio
->uio_iovcnt
;
448 kr
= vm_conflict_check(current_map(),
449 (vm_offset_t
)iov
->iov_base
,
450 iov
->iov_len
, pager
, file_off
);
451 if(kr
== KERN_ALREADY_WAITING
) {
452 if((count
!= uio
->uio_iovcnt
) &&
453 (ret
!= KERN_ALREADY_WAITING
)) {
457 ret
= KERN_ALREADY_WAITING
;
458 } else if (kr
!= KERN_SUCCESS
) {
466 file_off
+= iov
->iov_len
;
470 if(ret
== KERN_ALREADY_WAITING
) {
472 if ((flags
& FOF_OFFSET
) == 0)
474 count
- uio
->uio_resid
;
479 error
= VOP_WRITE(vp
, uio
, ioflag
, cred
);
481 if ((flags
& FOF_OFFSET
) == 0) {
482 if (ioflag
& IO_APPEND
)
483 fp
->f_offset
= uio
->uio_offset
;
485 fp
->f_offset
+= count
- uio
->uio_resid
;
489 * Set the credentials on successful writes
491 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
496 VOP_UNLOCK(vp
, 0, p
);
501 * File table vnode stat routine.
506 register struct stat
*sb
;
510 register struct vattr
*vap
;
515 error
= VOP_GETATTR(vp
, vap
, p
->p_ucred
, p
);
519 * Copy from vattr table
521 sb
->st_dev
= vap
->va_fsid
;
522 sb
->st_ino
= vap
->va_fileid
;
524 switch (vp
->v_type
) {
550 sb
->st_nlink
= vap
->va_nlink
;
551 sb
->st_uid
= vap
->va_uid
;
552 sb
->st_gid
= vap
->va_gid
;
553 sb
->st_rdev
= vap
->va_rdev
;
554 sb
->st_size
= vap
->va_size
;
555 sb
->st_atimespec
= vap
->va_atime
;
556 sb
->st_mtimespec
= vap
->va_mtime
;
557 sb
->st_ctimespec
= vap
->va_ctime
;
558 sb
->st_blksize
= vap
->va_blocksize
;
559 sb
->st_flags
= vap
->va_flags
;
560 /* Do not give the generation number out to unpriviledged users */
561 if (suser(p
->p_ucred
, &p
->p_acflag
))
564 sb
->st_gen
= vap
->va_gen
;
565 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
570 * File table vnode ioctl routine.
573 vn_ioctl(fp
, com
, data
, p
)
579 register struct vnode
*vp
= ((struct vnode
*)fp
->f_data
);
584 switch (vp
->v_type
) {
588 if (com
== FIONREAD
) {
589 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
))
591 *(int *)data
= vattr
.va_size
- fp
->f_offset
;
594 if (com
== FIONBIO
|| com
== FIOASYNC
) /* XXX */
595 return (0); /* XXX */
605 /* Should not be able to set block size from user space */
606 if(com
== DKIOCSETBLOCKSIZE
)
609 if (com
== FIODTYPE
) {
610 if (vp
->v_type
== VBLK
) {
611 if (major(vp
->v_rdev
) >= nblkdev
)
613 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
614 } else if (vp
->v_type
== VCHR
) {
615 if (major(vp
->v_rdev
) >= nchrdev
)
617 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
623 error
= VOP_IOCTL(vp
, com
, data
, fp
->f_flag
, p
->p_ucred
, p
);
624 if (error
== 0 && com
== TIOCSCTTY
) {
626 ttyvp
= p
->p_session
->s_ttyvp
;
627 p
->p_session
->s_ttyvp
= vp
;
636 * File table vnode select routine.
639 vn_select(fp
, which
, wql
, p
)
646 return(VOP_SELECT(((struct vnode
*)fp
->f_data
), which
, fp
->f_flag
,
647 fp
->f_cred
, wql
, p
));
651 * Check that the vnode is still valid, and if so
652 * acquire requested lock.
655 vn_lock(vp
, flags
, p
)
663 if ((flags
& LK_INTERLOCK
) == 0)
664 simple_lock(&vp
->v_interlock
);
665 if (vp
->v_flag
& VXLOCK
) {
666 while (vp
->v_flag
& VXLOCK
) {
667 vp
->v_flag
|= VXWANT
;
668 simple_unlock(&vp
->v_interlock
);
669 (void)tsleep((caddr_t
)vp
, PINOD
, "vn_lock", 0);
673 error
= VOP_LOCK(vp
, flags
| LK_INTERLOCK
, p
);
677 flags
&= ~LK_INTERLOCK
;
678 } while (flags
& LK_RETRY
);
683 * File table vnode close routine.
691 return (vn_close(((struct vnode
*)fp
->f_data
), fp
->f_flag
,