]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_vnops.c
2 * Copyright (c) 2000-2003 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
,
93 static int vn_kqfilt_add
__P((struct file
*fp
, struct knote
*kn
, struct proc
*p
));
94 static int vn_kqfilt_remove
__P((struct vnode
*vp
, uintptr_t ident
, struct proc
*p
));
96 struct fileops vnops
=
97 { vn_read
, vn_write
, vn_ioctl
, vn_select
, vn_closefile
, vn_kqfilt_add
};
100 * Common code for vnode open operations.
101 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
104 vn_open(ndp
, fmode
, cmode
)
105 register struct nameidata
*ndp
;
108 return vn_open_modflags(ndp
,&fmode
,cmode
);
111 __private_extern__
int
112 vn_open_modflags(ndp
, fmodep
, cmode
)
113 register struct nameidata
*ndp
;
117 register struct vnode
*vp
;
118 register struct proc
*p
= ndp
->ni_cnd
.cn_proc
;
119 register struct ucred
*cred
= p
->p_ucred
;
121 struct vattr
*vap
= &vat
;
127 if (fmode
& O_CREAT
) {
128 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
129 ndp
->ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
| AUDITVNPATH1
;
130 if ((fmode
& O_EXCL
) == 0)
131 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
133 if (error
= namei(ndp
))
135 if (ndp
->ni_vp
== NULL
) {
136 nameptr
= add_name(ndp
->ni_cnd
.cn_nameptr
,
137 ndp
->ni_cnd
.cn_namelen
,
138 ndp
->ni_cnd
.cn_hash
, 0);
142 vap
->va_mode
= cmode
;
144 vap
->va_vaflags
|= VA_EXCLUSIVE
;
145 VOP_LEASE(ndp
->ni_dvp
, p
, cred
, LEASE_WRITE
);
146 if (error
= VOP_CREATE(ndp
->ni_dvp
, &ndp
->ni_vp
,
147 &ndp
->ni_cnd
, vap
)) {
148 remove_name(nameptr
);
155 if (vget(ndp
->ni_dvp
, 0, p
) == 0) {
156 VPARENT(vp
) = ndp
->ni_dvp
;
159 VOP_ABORTOP(ndp
->ni_dvp
, &ndp
->ni_cnd
);
160 if (ndp
->ni_dvp
== ndp
->ni_vp
)
166 if (fmode
& O_EXCL
) {
173 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
174 ndp
->ni_cnd
.cn_flags
= FOLLOW
| LOCKLEAF
| AUDITVNPATH1
;
175 if (error
= namei(ndp
))
179 if (vp
->v_type
== VSOCK
) {
185 if (UBCINFOMISSING(vp
))
186 panic("vn_open: ubc_info_init");
187 #endif /* DIAGNOSTIC */
189 if (UBCINFOEXISTS(vp
) && ((didhold
= ubc_hold(vp
)) == 0)) {
194 if ((fmode
& O_CREAT
) == 0) {
195 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
197 if (vp
->v_type
== VDIR
)
200 err
= vn_writechk(vp
);
201 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
203 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
,
206 } else if (fmode
& FREAD
) {
207 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
209 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
210 if (vp
->v_type
== VDIR
) {
214 if ((error
= vn_writechk(vp
)) ||
215 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
220 if (error
= VOP_OPEN(vp
, fmode
, cred
, p
)) {
225 if (++vp
->v_writecount
<= 0)
226 panic("vn_open: v_writecount");
230 VOP_UNLOCK(vp
, 0, p
);
239 * Check for write permissions on the specified vnode.
240 * Prototype text segments cannot be written.
244 register struct vnode
*vp
;
248 * If there's shared text associated with
249 * the vnode, try to free it up once. If
250 * we fail, we can't allow writing.
253 /* XXXXX Not sure we need this */
254 if (vp
->v_flag
& VTEXT
)
264 vn_close(vp
, flags
, cred
, p
)
265 register struct vnode
*vp
;
272 if (flags
& FWRITE
) {
277 extern void notify_filemod_watchers(struct vnode
*vp
, struct proc
*p
);
279 notify_filemod_watchers(vp
, p
);
283 error
= VOP_CLOSE(vp
, flags
, cred
, p
);
290 * Package up an I/O request on a vnode into a uio and do it.
293 vn_rdwr(rw
, vp
, base
, len
, offset
, segflg
, ioflg
, cred
, aresid
, p
)
310 if ((ioflg
& IO_NODELOCKED
) == 0)
311 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
312 auio
.uio_iov
= &aiov
;
314 aiov
.iov_base
= base
;
316 auio
.uio_resid
= len
;
317 auio
.uio_offset
= offset
;
318 auio
.uio_segflg
= segflg
;
323 error
= VOP_READ(vp
, &auio
, ioflg
, cred
);
325 error
= VOP_WRITE(vp
, &auio
, ioflg
, cred
);
328 *aresid
= auio
.uio_resid
;
330 if (auio
.uio_resid
&& error
== 0)
332 if ((ioflg
& IO_NODELOCKED
) == 0)
333 VOP_UNLOCK(vp
, 0, p
);
338 * File table vnode read routine.
341 vn_read(fp
, uio
, cred
, flags
, p
)
352 if (p
!= uio
->uio_procp
)
353 panic("vn_read: uio_procp does not match p");
355 vp
= (struct vnode
*)fp
->f_data
;
357 if (fp
->f_flag
& FNONBLOCK
)
359 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
360 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
363 if ((flags
& FOF_OFFSET
) == 0)
364 uio
->uio_offset
= fp
->f_offset
;
365 count
= uio
->uio_resid
;
367 if(UBCINFOEXISTS(vp
)) {
368 memory_object_t pager
;
371 kern_return_t kr
= KERN_SUCCESS
;
372 kern_return_t ret
= KERN_SUCCESS
;
375 pager
= (memory_object_t
)ubc_getpager(vp
);
376 file_off
= uio
->uio_offset
;
378 count
= uio
->uio_iovcnt
;
380 kr
= vm_conflict_check(current_map(),
381 (vm_offset_t
)iov
->iov_base
, iov
->iov_len
,
383 if(kr
== KERN_ALREADY_WAITING
) {
384 if((count
!= uio
->uio_iovcnt
) &&
385 (ret
!= KERN_ALREADY_WAITING
)) {
389 ret
= KERN_ALREADY_WAITING
;
390 } else if (kr
!= KERN_SUCCESS
) {
398 file_off
+= iov
->iov_len
;
402 if(ret
== KERN_ALREADY_WAITING
) {
404 if ((flags
& FOF_OFFSET
) == 0)
406 count
- uio
->uio_resid
;
411 error
= VOP_READ(vp
, uio
, ioflag
, cred
);
412 if ((flags
& FOF_OFFSET
) == 0)
413 fp
->f_offset
+= count
- uio
->uio_resid
;
415 VOP_UNLOCK(vp
, 0, p
);
421 * File table vnode write routine.
424 vn_write(fp
, uio
, cred
, flags
, p
)
435 if (p
!= uio
->uio_procp
)
436 panic("vn_write: uio_procp does not match p");
438 vp
= (struct vnode
*)fp
->f_data
;
440 if (vp
->v_type
== VREG
)
442 if (vp
->v_type
== VREG
&& (fp
->f_flag
& O_APPEND
))
444 if (fp
->f_flag
& FNONBLOCK
)
446 if ((fp
->f_flag
& O_FSYNC
) ||
447 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
)))
449 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
450 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
453 if ((flags
& FOF_OFFSET
) == 0) {
454 uio
->uio_offset
= fp
->f_offset
;
455 count
= uio
->uio_resid
;
458 if(UBCINFOEXISTS(vp
)) {
459 memory_object_t pager
;
462 kern_return_t kr
= KERN_SUCCESS
;
463 kern_return_t ret
= KERN_SUCCESS
;
466 pager
= (memory_object_t
)ubc_getpager(vp
);
467 file_off
= uio
->uio_offset
;
469 count
= uio
->uio_iovcnt
;
471 kr
= vm_conflict_check(current_map(),
472 (vm_offset_t
)iov
->iov_base
,
473 iov
->iov_len
, pager
, file_off
);
474 if(kr
== KERN_ALREADY_WAITING
) {
475 if((count
!= uio
->uio_iovcnt
) &&
476 (ret
!= KERN_ALREADY_WAITING
)) {
480 ret
= KERN_ALREADY_WAITING
;
481 } else if (kr
!= KERN_SUCCESS
) {
489 file_off
+= iov
->iov_len
;
493 if(ret
== KERN_ALREADY_WAITING
) {
495 if ((flags
& FOF_OFFSET
) == 0)
497 count
- uio
->uio_resid
;
502 error
= VOP_WRITE(vp
, uio
, ioflag
, cred
);
504 if ((flags
& FOF_OFFSET
) == 0) {
505 if (ioflag
& IO_APPEND
)
506 fp
->f_offset
= uio
->uio_offset
;
508 fp
->f_offset
+= count
- uio
->uio_resid
;
512 * Set the credentials on successful writes
514 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
519 VOP_UNLOCK(vp
, 0, p
);
524 * File table vnode stat routine.
529 register struct stat
*sb
;
533 register struct vattr
*vap
;
538 error
= VOP_GETATTR(vp
, vap
, p
->p_ucred
, p
);
542 * Copy from vattr table
544 sb
->st_dev
= vap
->va_fsid
;
545 sb
->st_ino
= vap
->va_fileid
;
547 switch (vp
->v_type
) {
573 sb
->st_nlink
= vap
->va_nlink
;
574 sb
->st_uid
= vap
->va_uid
;
575 sb
->st_gid
= vap
->va_gid
;
576 sb
->st_rdev
= vap
->va_rdev
;
577 sb
->st_size
= vap
->va_size
;
578 sb
->st_atimespec
= vap
->va_atime
;
579 sb
->st_mtimespec
= vap
->va_mtime
;
580 sb
->st_ctimespec
= vap
->va_ctime
;
581 sb
->st_blksize
= vap
->va_blocksize
;
582 sb
->st_flags
= vap
->va_flags
;
583 /* Do not give the generation number out to unpriviledged users */
584 if (vap
->va_gen
&& suser(p
->p_ucred
, &p
->p_acflag
))
587 sb
->st_gen
= vap
->va_gen
;
588 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
593 * File table vnode ioctl routine.
596 vn_ioctl(fp
, com
, data
, p
)
602 register struct vnode
*vp
= ((struct vnode
*)fp
->f_data
);
607 switch (vp
->v_type
) {
611 if (com
== FIONREAD
) {
612 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
))
614 *(int *)data
= vattr
.va_size
- fp
->f_offset
;
617 if (com
== FIONBIO
|| com
== FIOASYNC
) /* XXX */
618 return (0); /* XXX */
628 /* Should not be able to set block size from user space */
629 if(com
== DKIOCSETBLOCKSIZE
)
632 if (com
== FIODTYPE
) {
633 if (vp
->v_type
== VBLK
) {
634 if (major(vp
->v_rdev
) >= nblkdev
)
636 *(int *)data
= bdevsw
[major(vp
->v_rdev
)].d_type
;
637 } else if (vp
->v_type
== VCHR
) {
638 if (major(vp
->v_rdev
) >= nchrdev
)
640 *(int *)data
= cdevsw
[major(vp
->v_rdev
)].d_type
;
646 error
= VOP_IOCTL(vp
, com
, data
, fp
->f_flag
, p
->p_ucred
, p
);
647 if (error
== 0 && com
== TIOCSCTTY
) {
649 ttyvp
= p
->p_session
->s_ttyvp
;
650 p
->p_session
->s_ttyvp
= vp
;
659 * File table vnode select routine.
662 vn_select(fp
, which
, wql
, p
)
669 return(VOP_SELECT(((struct vnode
*)fp
->f_data
), which
, fp
->f_flag
,
670 fp
->f_cred
, wql
, p
));
674 * Check that the vnode is still valid, and if so
675 * acquire requested lock.
678 vn_lock(vp
, flags
, p
)
686 if ((flags
& LK_INTERLOCK
) == 0)
687 simple_lock(&vp
->v_interlock
);
688 if (vp
->v_flag
& VXLOCK
) {
689 while (vp
->v_flag
& VXLOCK
) {
690 vp
->v_flag
|= VXWANT
;
691 simple_unlock(&vp
->v_interlock
);
692 (void)tsleep((caddr_t
)vp
, PINOD
, "vn_lock", 0);
696 error
= VOP_LOCK(vp
, flags
| LK_INTERLOCK
, p
);
700 flags
&= ~LK_INTERLOCK
;
701 } while (flags
& LK_RETRY
);
706 * File table vnode close routine.
714 return (vn_close(((struct vnode
*)fp
->f_data
), fp
->f_flag
,
719 vn_kqfilt_add(fp
, kn
, p
)
724 struct vnode
*vp
= (struct vnode
*)fp
->f_data
;
727 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
728 if (error
) return (error
);
729 error
= VOP_KQFILT_ADD(vp
, kn
, p
);
730 (void)VOP_UNLOCK(vp
, 0, p
);
735 vn_kqfilt_remove(vp
, ident
, p
)
742 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
743 if (error
) return (error
);
744 error
= VOP_KQFILT_REMOVE(vp
, ident
, p
);
745 (void)VOP_UNLOCK(vp
, 0, p
);