]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_vnops.c
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
71 #include <sys/mount.h>
72 #include <sys/namei.h>
73 #include <sys/vnode.h>
74 #include <sys/ioctl.h>
80 #include <vm/vm_kern.h>
82 #include <miscfs/specfs/specdev.h>
84 static int vn_closefile
__P((struct file
*fp
, struct proc
*p
));
85 static int vn_ioctl
__P((struct file
*fp
, u_long com
, caddr_t data
,
87 static int vn_read
__P((struct file
*fp
, struct uio
*uio
,
88 struct ucred
*cred
, int flags
, struct proc
*p
));
89 static int vn_write
__P((struct file
*fp
, struct uio
*uio
,
90 struct ucred
*cred
, int flags
, struct proc
*p
));
91 static int vn_select
__P(( struct file
*fp
, int which
, void * wql
,
94 struct fileops vnops
=
95 { vn_read
, vn_write
, vn_ioctl
, vn_select
, vn_closefile
};
98 * Common code for vnode open operations.
99 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
102 vn_open(ndp
, fmode
, cmode
)
103 register struct nameidata
*ndp
;
106 register struct vnode
*vp
;
107 register struct proc
*p
= ndp
->ni_cnd
.cn_proc
;
108 register struct ucred
*cred
= p
->p_ucred
;
110 struct vattr
*vap
= &vat
;
114 if (fmode
& O_CREAT
) {
115 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
116 ndp
->ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
117 if ((fmode
& O_EXCL
) == 0)
118 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
120 if (error
= namei(ndp
))
122 if (ndp
->ni_vp
== NULL
) {
125 vap
->va_mode
= cmode
;
127 vap
->va_vaflags
|= VA_EXCLUSIVE
;
128 VOP_LEASE(ndp
->ni_dvp
, p
, cred
, LEASE_WRITE
);
129 if (error
= VOP_CREATE(ndp
->ni_dvp
, &ndp
->ni_vp
,
135 VOP_ABORTOP(ndp
->ni_dvp
, &ndp
->ni_cnd
);
136 if (ndp
->ni_dvp
== ndp
->ni_vp
)
142 if (fmode
& O_EXCL
) {
149 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
150 ndp
->ni_cnd
.cn_flags
= FOLLOW
| LOCKLEAF
;
151 if (error
= namei(ndp
))
155 if (vp
->v_type
== VSOCK
) {
161 if (UBCINFOMISSING(vp
))
162 panic("vn_open: ubc_info_init");
163 #endif /* DIAGNOSTIC */
165 if (UBCINFOEXISTS(vp
) && ((didhold
= ubc_hold(vp
)) == 0)) {
170 if ((fmode
& O_CREAT
) == 0) {
171 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
173 if (vp
->v_type
== VDIR
)
176 err
= vn_writechk(vp
);
177 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
179 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
,
182 } else if (fmode
& FREAD
) {
183 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
185 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
186 if (vp
->v_type
== VDIR
) {
190 if ((error
= vn_writechk(vp
)) ||
191 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
195 if (fmode
& O_TRUNC
) {
196 VOP_UNLOCK(vp
, 0, p
); /* XXX */
197 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
198 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
201 if (error
= VOP_SETATTR(vp
, vap
, cred
, p
))
205 if (error
= VOP_OPEN(vp
, fmode
, cred
, p
)) {
210 if (++vp
->v_writecount
<= 0)
211 panic("vn_open: v_writecount");
214 VOP_UNLOCK(vp
, 0, p
);
222 * Check for write permissions on the specified vnode.
223 * Prototype text segments cannot be written.
227 register struct vnode
*vp
;
231 * If there's shared text associated with
232 * the vnode, try to free it up once. If
233 * we fail, we can't allow writing.
236 /* XXXXX Not sure we need this */
237 if (vp
->v_flag
& VTEXT
)
247 vn_close(vp
, flags
, cred
, p
)
248 register struct vnode
*vp
;
257 error
= VOP_CLOSE(vp
, flags
, cred
, p
);
264 * Package up an I/O request on a vnode into a uio and do it.
267 vn_rdwr(rw
, vp
, base
, len
, offset
, segflg
, ioflg
, cred
, aresid
, p
)
284 if ((ioflg
& IO_NODELOCKED
) == 0)
285 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
286 auio
.uio_iov
= &aiov
;
288 aiov
.iov_base
= base
;
290 auio
.uio_resid
= len
;
291 auio
.uio_offset
= offset
;
292 auio
.uio_segflg
= segflg
;
297 error
= VOP_READ(vp
, &auio
, ioflg
, cred
);
299 error
= VOP_WRITE(vp
, &auio
, ioflg
, cred
);
302 *aresid
= auio
.uio_resid
;
304 if (auio
.uio_resid
&& error
== 0)
306 if ((ioflg
& IO_NODELOCKED
) == 0)
307 VOP_UNLOCK(vp
, 0, p
);
312 * File table vnode read routine.
315 vn_read(fp
, uio
, cred
, flags
, p
)
326 if (p
!= uio
->uio_procp
)
327 panic("vn_read: uio_procp does not match p");
329 vp
= (struct vnode
*)fp
->f_data
;
331 if (fp
->f_flag
& FNONBLOCK
)
333 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
334 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
337 if ((flags
& FOF_OFFSET
) == 0)
338 uio
->uio_offset
= fp
->f_offset
;
339 count
= uio
->uio_resid
;
341 if(UBCINFOEXISTS(vp
)) {
342 memory_object_t pager
;
345 kern_return_t kr
= KERN_SUCCESS
;
346 kern_return_t ret
= KERN_SUCCESS
;
349 pager
= (memory_object_t
)ubc_getpager(vp
);
350 file_off
= uio
->uio_offset
;
352 count
= uio
->uio_iovcnt
;
354 kr
= vm_conflict_check(current_map(),
355 (vm_offset_t
)iov
->iov_base
, iov
->iov_len
,
357 if(kr
== KERN_ALREADY_WAITING
) {
358 if((count
!= uio
->uio_iovcnt
) &&
359 (ret
!= KERN_ALREADY_WAITING
)) {
363 ret
= KERN_ALREADY_WAITING
;
364 } else if (kr
!= KERN_SUCCESS
) {
372 file_off
+= iov
->iov_len
;
376 if(ret
== KERN_ALREADY_WAITING
) {
378 if ((flags
& FOF_OFFSET
) == 0)
380 count
- uio
->uio_resid
;
385 error
= VOP_READ(vp
, uio
, ioflag
, cred
);
386 if ((flags
& FOF_OFFSET
) == 0)
387 fp
->f_offset
+= count
- uio
->uio_resid
;
389 VOP_UNLOCK(vp
, 0, p
);
395 * File table vnode write routine.
398 vn_write(fp
, uio
, cred
, flags
, p
)
409 if (p
!= uio
->uio_procp
)
410 panic("vn_write: uio_procp does not match p");
412 vp
= (struct vnode
*)fp
->f_data
;
414 if (vp
->v_type
== VREG
)
416 if (vp
->v_type
== VREG
&& (fp
->f_flag
& O_APPEND
))
418 if (fp
->f_flag
& FNONBLOCK
)
420 if ((fp
->f_flag
& O_FSYNC
) ||
421 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
)))
423 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
424 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
427 if ((flags
& FOF_OFFSET
) == 0) {
428 uio
->uio_offset
= fp
->f_offset
;
429 count
= uio
->uio_resid
;
432 if(UBCINFOEXISTS(vp
)) {
433 memory_object_t pager
;
436 kern_return_t kr
= KERN_SUCCESS
;
437 kern_return_t ret
= KERN_SUCCESS
;
440 pager
= (memory_object_t
)ubc_getpager(vp
);
441 file_off
= uio
->uio_offset
;
443 count
= uio
->uio_iovcnt
;
445 kr
= vm_conflict_check(current_map(),
446 (vm_offset_t
)iov
->iov_base
,
447 iov
->iov_len
, pager
, file_off
);
448 if(kr
== KERN_ALREADY_WAITING
) {
449 if((count
!= uio
->uio_iovcnt
) &&
450 (ret
!= KERN_ALREADY_WAITING
)) {
454 ret
= KERN_ALREADY_WAITING
;
455 } else if (kr
!= KERN_SUCCESS
) {
463 file_off
+= iov
->iov_len
;
467 if(ret
== KERN_ALREADY_WAITING
) {
469 if ((flags
& FOF_OFFSET
) == 0)
471 count
- uio
->uio_resid
;
476 error
= VOP_WRITE(vp
, uio
, ioflag
, cred
);
478 if ((flags
& FOF_OFFSET
) == 0) {
479 if (ioflag
& IO_APPEND
)
480 fp
->f_offset
= uio
->uio_offset
;
482 fp
->f_offset
+= count
- uio
->uio_resid
;
486 * Set the credentials on successful writes
488 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
493 VOP_UNLOCK(vp
, 0, p
);
498 * File table vnode stat routine.
503 register struct stat
*sb
;
507 register struct vattr
*vap
;
512 error
= VOP_GETATTR(vp
, vap
, p
->p_ucred
, p
);
516 * Copy from vattr table
518 sb
->st_dev
= vap
->va_fsid
;
519 sb
->st_ino
= vap
->va_fileid
;
521 switch (vp
->v_type
) {
547 sb
->st_nlink
= vap
->va_nlink
;
548 sb
->st_uid
= vap
->va_uid
;
549 sb
->st_gid
= vap
->va_gid
;
550 sb
->st_rdev
= vap
->va_rdev
;
551 sb
->st_size
= vap
->va_size
;
552 sb
->st_atimespec
= vap
->va_atime
;
553 sb
->st_mtimespec
= vap
->va_mtime
;
554 sb
->st_ctimespec
= vap
->va_ctime
;
555 sb
->st_blksize
= vap
->va_blocksize
;
556 sb
->st_flags
= vap
->va_flags
;
557 /* Do not give the generation number out to unpriviledged users */
558 if (suser(p
->p_ucred
, &p
->p_acflag
))
561 sb
->st_gen
= vap
->va_gen
;
562 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
567 * File table vnode ioctl routine.
570 vn_ioctl(fp
, com
, data
, p
)
576 register struct vnode
*vp
= ((struct vnode
*)fp
->f_data
);
581 switch (vp
->v_type
) {
585 if (com
== FIONREAD
) {
586 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
))
588 *(int *)data
= vattr
.va_size
- fp
->f_offset
;
591 if (com
== FIONBIO
|| com
== FIOASYNC
) /* XXX */
592 return (0); /* XXX */
602 /* Should not be able to set block size from user space */
603 if(com
== DKIOCSETBLOCKSIZE
)
606 if (com
== FIODTYPE
) {
607 if (vp
->v_type
== VBLK
) {
608 if (major(vp
->v_rdev
) >= nblkdev
)
610 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
611 } else if (vp
->v_type
== VCHR
) {
612 if (major(vp
->v_rdev
) >= nchrdev
)
614 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
620 error
= VOP_IOCTL(vp
, com
, data
, fp
->f_flag
, p
->p_ucred
, p
);
621 if (error
== 0 && com
== TIOCSCTTY
) {
623 ttyvp
= p
->p_session
->s_ttyvp
;
624 p
->p_session
->s_ttyvp
= vp
;
633 * File table vnode select routine.
636 vn_select(fp
, which
, wql
, p
)
643 return(VOP_SELECT(((struct vnode
*)fp
->f_data
), which
, fp
->f_flag
,
644 fp
->f_cred
, wql
, p
));
648 * Check that the vnode is still valid, and if so
649 * acquire requested lock.
652 vn_lock(vp
, flags
, p
)
660 if ((flags
& LK_INTERLOCK
) == 0)
661 simple_lock(&vp
->v_interlock
);
662 if (vp
->v_flag
& VXLOCK
) {
663 while (vp
->v_flag
& VXLOCK
) {
664 vp
->v_flag
|= VXWANT
;
665 simple_unlock(&vp
->v_interlock
);
666 (void)tsleep((caddr_t
)vp
, PINOD
, "vn_lock", 0);
670 error
= VOP_LOCK(vp
, flags
| LK_INTERLOCK
, p
);
674 flags
&= ~LK_INTERLOCK
;
675 } while (flags
& LK_RETRY
);
680 * File table vnode close routine.
688 return (vn_close(((struct vnode
*)fp
->f_data
), fp
->f_flag
,