]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_vnops.c
a16dfb16759b4278374f3a61f0d9fd9b486acd57
2 * Copyright (c) 2000-2001 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>
78 struct fileops vnops
=
79 { vn_read
, vn_write
, vn_ioctl
, vn_select
, vn_closefile
};
82 * Common code for vnode open operations.
83 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
85 vn_open(ndp
, fmode
, cmode
)
86 register struct nameidata
*ndp
;
89 register struct vnode
*vp
;
90 register struct proc
*p
= ndp
->ni_cnd
.cn_proc
;
91 register struct ucred
*cred
= p
->p_ucred
;
93 struct vattr
*vap
= &vat
;
96 if (fmode
& O_CREAT
) {
97 ndp
->ni_cnd
.cn_nameiop
= CREATE
;
98 ndp
->ni_cnd
.cn_flags
= LOCKPARENT
| LOCKLEAF
;
99 if ((fmode
& O_EXCL
) == 0)
100 ndp
->ni_cnd
.cn_flags
|= FOLLOW
;
101 if (error
= namei(ndp
))
103 if (ndp
->ni_vp
== NULL
) {
106 vap
->va_mode
= cmode
;
108 vap
->va_vaflags
|= VA_EXCLUSIVE
;
109 VOP_LEASE(ndp
->ni_dvp
, p
, cred
, LEASE_WRITE
);
110 if (error
= VOP_CREATE(ndp
->ni_dvp
, &ndp
->ni_vp
,
116 VOP_ABORTOP(ndp
->ni_dvp
, &ndp
->ni_cnd
);
117 if (ndp
->ni_dvp
== ndp
->ni_vp
)
123 if (fmode
& O_EXCL
) {
130 ndp
->ni_cnd
.cn_nameiop
= LOOKUP
;
131 ndp
->ni_cnd
.cn_flags
= FOLLOW
| LOCKLEAF
;
132 if (error
= namei(ndp
))
136 if (vp
->v_type
== VSOCK
) {
140 if ((fmode
& O_CREAT
) == 0) {
141 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
143 if (vp
->v_type
== VDIR
)
146 err
= vn_writechk(vp
);
147 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
149 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
,
152 } else if (fmode
& FREAD
) {
153 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
155 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
156 if (vp
->v_type
== VDIR
) {
160 if ((error
= vn_writechk(vp
)) ||
161 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
165 if (fmode
& O_TRUNC
) {
166 VOP_UNLOCK(vp
, 0, p
); /* XXX */
167 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
168 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
171 if (error
= VOP_SETATTR(vp
, vap
, cred
, p
))
176 if (UBCINFOMISSING(vp
))
177 panic("vn_open: ubc_info_init");
178 #endif /* DIAGNOSTIC */
180 if (UBCINFOEXISTS(vp
) && !ubc_hold(vp
)) {
185 if (error
= VOP_OPEN(vp
, fmode
, cred
, p
)) {
191 if (++vp
->v_writecount
<= 0)
192 panic("vn_open: v_writecount");
200 * Check for write permissions on the specified vnode.
201 * Prototype text segments cannot be written.
204 register struct vnode
*vp
;
208 * If there's shared text associated with
209 * the vnode, try to free it up once. If
210 * we fail, we can't allow writing.
213 /* XXXXX Not sure we need this */
214 if (vp
->v_flag
& VTEXT
)
223 vn_close(vp
, flags
, cred
, p
)
224 register struct vnode
*vp
;
233 error
= VOP_CLOSE(vp
, flags
, cred
, p
);
240 * Package up an I/O request on a vnode into a uio and do it.
242 vn_rdwr(rw
, vp
, base
, len
, offset
, segflg
, ioflg
, cred
, aresid
, p
)
259 if ((ioflg
& IO_NODELOCKED
) == 0)
260 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
261 auio
.uio_iov
= &aiov
;
263 aiov
.iov_base
= base
;
265 auio
.uio_resid
= len
;
266 auio
.uio_offset
= offset
;
267 auio
.uio_segflg
= segflg
;
272 error
= VOP_READ(vp
, &auio
, ioflg
, cred
);
274 error
= VOP_WRITE(vp
, &auio
, ioflg
, cred
);
277 *aresid
= auio
.uio_resid
;
279 if (auio
.uio_resid
&& error
== 0)
281 if ((ioflg
& IO_NODELOCKED
) == 0)
282 VOP_UNLOCK(vp
, 0, p
);
287 * File table vnode read routine.
289 vn_read(fp
, uio
, cred
)
294 struct vnode
*vp
= (struct vnode
*)fp
->f_data
;
295 struct proc
*p
= uio
->uio_procp
;
299 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
300 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
301 uio
->uio_offset
= fp
->f_offset
;
302 count
= uio
->uio_resid
;
304 error
= VOP_READ(vp
, uio
, (fp
->f_flag
& FNONBLOCK
) ? IO_NDELAY
: 0, cred
);
306 fp
->f_offset
+= count
- uio
->uio_resid
;
307 VOP_UNLOCK(vp
, 0, p
);
313 * File table vnode write routine.
315 vn_write(fp
, uio
, cred
)
320 struct vnode
*vp
= (struct vnode
*)fp
->f_data
;
321 struct proc
*p
= uio
->uio_procp
;
322 int error
, ioflag
= IO_UNIT
;
325 if (vp
->v_type
== VREG
&& (fp
->f_flag
& O_APPEND
))
327 if (fp
->f_flag
& FNONBLOCK
)
329 if ((fp
->f_flag
& O_FSYNC
) ||
330 (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
)))
332 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
333 (void)vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
334 uio
->uio_offset
= fp
->f_offset
;
335 count
= uio
->uio_resid
;
337 error
= VOP_WRITE(vp
, uio
, ioflag
, cred
);
339 if (ioflag
& IO_APPEND
)
340 fp
->f_offset
= uio
->uio_offset
;
342 fp
->f_offset
+= count
- uio
->uio_resid
;
344 * Set the credentials on successful writes
346 if ((error
== 0) && (vp
->v_tag
== VT_NFS
) && (UBCINFOEXISTS(vp
))) {
350 VOP_UNLOCK(vp
, 0, p
);
355 * File table vnode stat routine.
359 register struct stat
*sb
;
363 register struct vattr
*vap
;
368 error
= VOP_GETATTR(vp
, vap
, p
->p_ucred
, p
);
372 * Copy from vattr table
374 sb
->st_dev
= vap
->va_fsid
;
375 sb
->st_ino
= vap
->va_fileid
;
377 switch (vp
->v_type
) {
403 sb
->st_nlink
= vap
->va_nlink
;
404 sb
->st_uid
= vap
->va_uid
;
405 sb
->st_gid
= vap
->va_gid
;
406 sb
->st_rdev
= vap
->va_rdev
;
407 sb
->st_size
= vap
->va_size
;
408 sb
->st_atimespec
= vap
->va_atime
;
409 sb
->st_mtimespec
= vap
->va_mtime
;
410 sb
->st_ctimespec
= vap
->va_ctime
;
411 sb
->st_blksize
= vap
->va_blocksize
;
412 sb
->st_flags
= vap
->va_flags
;
413 /* Do not give the generation number out to unpriviledged users */
414 if (suser(p
->p_ucred
, &p
->p_acflag
))
417 sb
->st_gen
= vap
->va_gen
;
418 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
423 * File table vnode ioctl routine.
425 vn_ioctl(fp
, com
, data
, p
)
431 register struct vnode
*vp
= ((struct vnode
*)fp
->f_data
);
435 switch (vp
->v_type
) {
439 if (com
== FIONREAD
) {
440 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
))
442 *(int *)data
= vattr
.va_size
- fp
->f_offset
;
445 if (com
== FIONBIO
|| com
== FIOASYNC
) /* XXX */
446 return (0); /* XXX */
455 error
= VOP_IOCTL(vp
, com
, data
, fp
->f_flag
, p
->p_ucred
, p
);
456 if (error
== 0 && com
== TIOCSCTTY
) {
457 if (p
->p_session
->s_ttyvp
)
458 vrele(p
->p_session
->s_ttyvp
);
459 p
->p_session
->s_ttyvp
= vp
;
467 * File table vnode select routine.
469 vn_select(fp
, which
, wql
, p
)
476 return(VOP_SELECT(((struct vnode
*)fp
->f_data
), which
, fp
->f_flag
,
477 fp
->f_cred
, wql
, p
));
481 * Check that the vnode is still valid, and if so
482 * acquire requested lock.
485 vn_lock(vp
, flags
, p
)
493 if ((flags
& LK_INTERLOCK
) == 0)
494 simple_lock(&vp
->v_interlock
);
495 if (vp
->v_flag
& VXLOCK
) {
496 while (vp
->v_flag
& VXLOCK
) {
497 vp
->v_flag
|= VXWANT
;
498 simple_unlock(&vp
->v_interlock
);
499 (void)tsleep((caddr_t
)vp
, PINOD
, "vn_lock", 0);
503 error
= VOP_LOCK(vp
, flags
| LK_INTERLOCK
, p
);
507 flags
&= ~LK_INTERLOCK
;
508 } while (flags
& LK_RETRY
);
513 * File table vnode close routine.
520 return (vn_close(((struct vnode
*)fp
->f_data
), fp
->f_flag
,