2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 * Copyright (c) 1988 University of Utah.
33 * Copyright (c) 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * the Systems Programming Group of the University of Utah Computer
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * from: Utah Hdr: vn.c 1.13 94/04/02
70 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
71 * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $
77 * Block/character interface to a vnode. Allows one to treat a file
78 * as a disk (e.g. build a filesystem in it, mount it, etc.).
80 * NOTE 1: This uses the vnop_blockmap/vnop_strategy interface to the vnode
81 * instead of a simple VOP_RDWR. We do this to avoid distorting the
84 * NOTE 2: There is a security issue involved with this driver.
85 * Once mounted all access to the contents of the "mapped" file via
86 * the special file is controlled by the permissions on the special
87 * file, the protection of the mapped file is ignored (effectively,
88 * by using root credentials in all transactions).
90 * NOTE 3: Doesn't interact with leases, should it?
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/kernel.h>
100 #include <sys/mount.h>
101 #include <sys/namei.h>
102 #include <sys/proc.h>
103 #include <sys/kauth.h>
105 #include <sys/malloc.h>
106 #include <sys/vnode_internal.h>
107 #include <sys/fcntl.h>
108 #include <sys/conf.h>
109 #include <sys/disk.h>
110 #include <sys/stat.h>
111 #include <sys/conf.h>
112 #include <sys/uio_internal.h>
114 #include <sys/vnioctl.h>
118 #include <vm/vm_pager.h>
119 #include <mach/memory_object_types.h>
121 #include <miscfs/devfs/devfs.h>
126 static ioctl_fcn_t vnioctl_chr
;
127 static ioctl_fcn_t vnioctl_blk
;
128 static open_close_fcn_t vnopen
;
129 static open_close_fcn_t vnclose
;
130 static psize_fcn_t vnsize
;
131 static strategy_fcn_t vnstrategy
;
132 static read_write_fcn_t vnread
;
133 static read_write_fcn_t vnwrite
;
135 static int vndevice_bdev_major
;
136 static int vndevice_cdev_major
;
140 * D_DISK we want to look like a disk
141 * D_CANFREE We support B_FREEBUF
144 static struct bdevsw vn_bdevsw
= {
147 /* strategy */ vnstrategy
,
148 /* ioctl */ vnioctl_blk
,
154 static struct cdevsw vn_cdevsw
= {
159 /* ioctl */ vnioctl_chr
,
161 /* reset */ eno_reset
,
163 /* select */ eno_select
,
165 /* strategy */ eno_strat
,
172 u_int64_t sc_fsize
; /* file size in bytes */
173 u_int64_t sc_size
; /* size of vn, sc_secsize scale */
174 int sc_flags
; /* flags */
175 u_long sc_secsize
; /* sector size */
176 struct vnode
*sc_vp
; /* vnode if not NULL */
179 struct vnode
*sc_shadow_vp
; /* shadow vnode if not NULL */
180 uint32_t sc_shadow_vid
;
181 shadow_map_t
* sc_shadow_map
; /* shadow map if not NULL */
182 kauth_cred_t sc_cred
; /* credentials */
183 u_int32_t sc_options
; /* options */
186 } vn_table
[NVNDEVICE
];
188 #define ROOT_IMAGE_UNIT 0
191 #define VNF_INITED 0x01
192 #define VNF_READONLY 0x02
194 static u_int32_t vn_options
;
196 #define IFOPT(vn,opt) if (((vn)->sc_options|vn_options) & (opt))
197 #define TESTOPT(vn,opt) (((vn)->sc_options|vn_options) & (opt))
199 static int setcred(struct vnode
* vp
, struct proc
* p
,
201 static void vnclear (struct vn_softc
*vn
, struct proc
* p
);
202 static void vn_ioctl_to_64(struct vn_ioctl
*from
, struct user_vn_ioctl
*to
);
203 void vndevice_init(void);
204 int vndevice_root_image(char * path
, char devname
[], dev_t
* dev_p
);
207 vniocattach_file(struct vn_softc
*vn
,
208 struct user_vn_ioctl
*vniop
,
213 vniocattach_shadow(struct vn_softc
* vn
,
214 struct user_vn_ioctl
*vniop
,
218 static __inline__
int
225 vnclose(__unused dev_t dev
, __unused
int flags
,
226 __unused
int devtype
, __unused
struct proc
*p
)
232 vnopen(dev_t dev
, int flags
, __unused
int devtype
, __unused
struct proc
*p
)
238 if (vnunit(dev
) >= NVNDEVICE
) {
241 vn
= vn_table
+ unit
;
242 if ((flags
& FWRITE
) && (vn
->sc_flags
& VNF_READONLY
))
249 file_io(struct vnode
* vp
, struct vfs_context
* context_p
,
250 enum uio_rw op
, char * base
, off_t offset
, user_ssize_t count
,
251 user_ssize_t
* resid
)
255 char uio_buf
[UIO_SIZEOF(1)];
257 auio
= uio_createwithbuffer(1, offset
, UIO_SYSSPACE
, op
,
258 &uio_buf
[0], sizeof(uio_buf
));
259 uio_addiov(auio
, CAST_USER_ADDR_T(base
), count
);
261 error
= VNOP_READ(vp
, auio
, IO_SYNC
, context_p
);
263 error
= VNOP_WRITE(vp
, auio
, IO_SYNC
, context_p
);
266 *resid
= uio_resid(auio
);
271 static __inline__ off_t
272 block_round(off_t o
, int blocksize
)
274 return ((o
+ blocksize
- 1) / blocksize
);
277 static __inline__ off_t
278 block_truncate(off_t o
, int blocksize
)
280 return (o
/ blocksize
);
283 static __inline__
int
284 block_remainder(off_t o
, int blocksize
)
286 return (o
% blocksize
);
290 vnread_shadow(struct vn_softc
* vn
, struct uio
*uio
, int ioflag
,
291 struct vfs_context
* context_p
)
293 u_long blocksize
= vn
->sc_secsize
;
298 user_ssize_t orig_resid
;
300 orig_resid
= resid
= uio_resid(uio
);
301 orig_offset
= offset
= uio_offset(uio
);
305 u_long this_block_number
;
306 u_long this_block_count
;
308 user_ssize_t this_resid
;
311 /* figure out which blocks to read */
312 remainder
= block_remainder(offset
, blocksize
);
313 if (shadow_map_read(vn
->sc_shadow_map
,
314 block_truncate(offset
, blocksize
),
315 block_round(resid
+ remainder
, blocksize
),
316 &this_block_number
, &this_block_count
)) {
317 vp
= vn
->sc_shadow_vp
;
323 /* read the blocks (or parts thereof) */
324 this_offset
= (off_t
)this_block_number
* blocksize
+ remainder
;
325 uio_setoffset(uio
, this_offset
);
326 this_resid
= this_block_count
* blocksize
- remainder
;
327 if (this_resid
> resid
) {
330 uio_setresid(uio
, this_resid
);
331 error
= VNOP_READ(vp
, uio
, ioflag
, context_p
);
336 /* figure out how much we actually read */
337 this_resid
-= uio_resid(uio
);
338 if (this_resid
== 0) {
339 printf("vn device: vnread_shadow zero length read\n");
343 offset
+= this_resid
;
345 uio_setresid(uio
, resid
);
346 uio_setoffset(uio
, offset
);
351 vncopy_block_to_shadow(struct vn_softc
* vn
, struct vfs_context
* context_p
,
352 u_long file_block
, u_long shadow_block
)
357 tmpbuf
= _MALLOC(vn
->sc_secsize
, M_TEMP
, M_WAITOK
);
358 if (tmpbuf
== NULL
) {
361 /* read one block from file at file_block offset */
362 error
= file_io(vn
->sc_vp
, context_p
, UIO_READ
,
363 tmpbuf
, (off_t
)file_block
* vn
->sc_secsize
,
364 vn
->sc_secsize
, NULL
);
368 /* write one block to shadow file at shadow_block offset */
369 error
= file_io(vn
->sc_shadow_vp
, context_p
, UIO_WRITE
,
370 tmpbuf
, (off_t
)shadow_block
* vn
->sc_secsize
,
371 vn
->sc_secsize
, NULL
);
373 FREE(tmpbuf
, M_TEMP
);
378 FLAGS_FIRST_BLOCK_PARTIAL
= 0x1,
379 FLAGS_LAST_BLOCK_PARTIAL
= 0x2
383 vnwrite_shadow(struct vn_softc
* vn
, struct uio
*uio
, int ioflag
,
384 struct vfs_context
* context_p
)
386 u_long blocksize
= vn
->sc_secsize
;
391 resid
= uio_resid(uio
);
392 offset
= uio_offset(uio
);
396 u_long offset_block_number
;
398 u_long resid_block_count
;
399 u_long shadow_block_count
;
400 u_long shadow_block_number
;
401 user_ssize_t this_resid
;
403 /* figure out which blocks to write */
404 offset_block_number
= block_truncate(offset
, blocksize
);
405 remainder
= block_remainder(offset
, blocksize
);
406 resid_block_count
= block_round(resid
+ remainder
, blocksize
);
407 /* figure out if the first or last blocks are partial writes */
409 && !shadow_map_is_written(vn
->sc_shadow_map
,
410 offset_block_number
)) {
411 /* the first block is a partial write */
412 flags
|= FLAGS_FIRST_BLOCK_PARTIAL
;
414 if (resid_block_count
> 1
415 && !shadow_map_is_written(vn
->sc_shadow_map
,
417 + resid_block_count
- 1)
418 && block_remainder(offset
+ resid
, blocksize
) > 0) {
419 /* the last block is a partial write */
420 flags
|= FLAGS_LAST_BLOCK_PARTIAL
;
422 if (shadow_map_write(vn
->sc_shadow_map
,
423 offset_block_number
, resid_block_count
,
424 &shadow_block_number
,
425 &shadow_block_count
)) {
426 /* shadow file is growing */
428 /* truncate the file to its new length before write */
430 size
= (off_t
)shadow_map_shadow_size(vn
->sc_shadow_map
)
432 vnode_setsize(vn
->sc_shadow_vp
, size
, IO_SYNC
,
436 /* write the blocks (or parts thereof) */
437 uio_setoffset(uio
, (off_t
)
438 shadow_block_number
* blocksize
+ remainder
);
439 this_resid
= (off_t
)shadow_block_count
* blocksize
- remainder
;
440 if (this_resid
>= resid
) {
442 if ((flags
& FLAGS_LAST_BLOCK_PARTIAL
) != 0) {
443 /* copy the last block to the shadow */
447 s
= offset_block_number
448 + resid_block_count
- 1;
449 d
= shadow_block_number
450 + shadow_block_count
- 1;
451 error
= vncopy_block_to_shadow(vn
, context_p
,
454 printf("vnwrite_shadow: failed to copy"
455 " block %d to shadow block %d\n",
461 uio_setresid(uio
, this_resid
);
462 if ((flags
& FLAGS_FIRST_BLOCK_PARTIAL
) != 0) {
463 /* copy the first block to the shadow */
464 error
= vncopy_block_to_shadow(vn
, context_p
,
466 shadow_block_number
);
468 printf("vnwrite_shadow: failed to"
469 " copy block %d to shadow block %d\n",
471 shadow_block_number
);
475 error
= VNOP_WRITE(vn
->sc_shadow_vp
, uio
, ioflag
, context_p
);
479 /* figure out how much we actually wrote */
480 this_resid
-= uio_resid(uio
);
481 if (this_resid
== 0) {
482 printf("vn device: vnwrite_shadow zero length write\n");
486 offset
+= this_resid
;
488 uio_setresid(uio
, resid
);
489 uio_setoffset(uio
, offset
);
494 vnread(dev_t dev
, struct uio
*uio
, int ioflag
)
496 struct vfs_context context
;
498 boolean_t funnel_state
;
502 struct vn_softc
* vn
;
506 if (vnunit(dev
) >= NVNDEVICE
) {
510 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
511 vn
= vn_table
+ unit
;
512 if ((vn
->sc_flags
& VNF_INITED
) == 0) {
516 error
= vnode_getwithvid(vn
->sc_vp
, vn
->sc_vid
);
518 /* the vnode is no longer available, abort */
524 resid
= uio_resid(uio
);
525 offset
= uio_offset(uio
);
528 * If out of bounds return an error. If at the EOF point,
531 if (offset
>= (off_t
)vn
->sc_fsize
) {
532 if (offset
> (off_t
)vn
->sc_fsize
) {
538 * If the request crosses EOF, truncate the request.
540 if ((offset
+ resid
) > (off_t
)vn
->sc_fsize
) {
541 resid
= vn
->sc_fsize
- offset
;
542 uio_setresid(uio
, resid
);
546 context
.vc_ucred
= vn
->sc_cred
;
547 if (vn
->sc_shadow_vp
!= NULL
) {
548 error
= vnode_getwithvid(vn
->sc_shadow_vp
,
551 /* the vnode is no longer available, abort */
553 vnode_put(vn
->sc_vp
);
557 error
= vnread_shadow(vn
, uio
, ioflag
, &context
);
558 vnode_put(vn
->sc_shadow_vp
);
560 error
= VNOP_READ(vn
->sc_vp
, uio
, ioflag
, &context
);
562 vnode_put(vn
->sc_vp
);
564 (void) thread_funnel_set(kernel_flock
, funnel_state
);
569 vnwrite(dev_t dev
, struct uio
*uio
, int ioflag
)
571 struct vfs_context context
;
573 boolean_t funnel_state
;
577 struct vn_softc
* vn
;
581 if (vnunit(dev
) >= NVNDEVICE
) {
585 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
586 vn
= vn_table
+ unit
;
587 if ((vn
->sc_flags
& VNF_INITED
) == 0) {
591 if (vn
->sc_flags
& VNF_READONLY
) {
595 error
= vnode_getwithvid(vn
->sc_vp
, vn
->sc_vid
);
597 /* the vnode is no longer available, abort */
602 resid
= uio_resid(uio
);
603 offset
= uio_offset(uio
);
606 * If out of bounds return an error. If at the EOF point,
609 if (offset
>= (off_t
)vn
->sc_fsize
) {
610 if (offset
> (off_t
)vn
->sc_fsize
) {
616 * If the request crosses EOF, truncate the request.
618 if ((offset
+ resid
) > (off_t
)vn
->sc_fsize
) {
619 resid
= (off_t
)vn
->sc_fsize
- offset
;
620 uio_setresid(uio
, resid
);
624 context
.vc_ucred
= vn
->sc_cred
;
626 if (vn
->sc_shadow_vp
!= NULL
) {
627 error
= vnode_getwithvid(vn
->sc_shadow_vp
,
630 /* the vnode is no longer available, abort */
632 vnode_put(vn
->sc_vp
);
636 error
= vnwrite_shadow(vn
, uio
, ioflag
, &context
);
637 vnode_put(vn
->sc_shadow_vp
);
639 error
= VNOP_WRITE(vn
->sc_vp
, uio
, ioflag
, &context
);
641 vnode_put(vn
->sc_vp
);
643 (void) thread_funnel_set(kernel_flock
, funnel_state
);
648 shadow_read(struct vn_softc
* vn
, struct buf
* bp
, char * base
, struct proc
* p
)
650 u_long blocksize
= vn
->sc_secsize
;
651 struct vfs_context context
;
654 boolean_t read_shadow
;
659 context
.vc_ucred
= vn
->sc_cred
;
660 offset
= buf_blkno(bp
);
661 resid
= buf_resid(bp
) / blocksize
;
663 user_ssize_t temp_resid
;
668 read_shadow
= shadow_map_read(vn
->sc_shadow_map
,
670 &this_offset
, &this_resid
);
672 vp
= vn
->sc_shadow_vp
;
677 error
= file_io(vp
, &context
, UIO_READ
, base
+ start
,
678 (off_t
)this_offset
* blocksize
,
679 (user_ssize_t
)this_resid
* blocksize
,
684 this_resid
-= (temp_resid
/ blocksize
);
685 if (this_resid
== 0) {
686 printf("vn device: shadow_read zero length read\n");
690 offset
+= this_resid
;
691 start
+= this_resid
* blocksize
;
693 buf_setresid(bp
, resid
* blocksize
);
698 shadow_write(struct vn_softc
* vn
, struct buf
* bp
, char * base
,
701 u_long blocksize
= vn
->sc_secsize
;
702 struct vfs_context context
;
705 boolean_t shadow_grew
;
710 context
.vc_ucred
= vn
->sc_cred
;
711 offset
= buf_blkno(bp
);
712 resid
= buf_resid(bp
) / blocksize
;
714 user_ssize_t temp_resid
;
718 shadow_grew
= shadow_map_write(vn
->sc_shadow_map
,
720 &this_offset
, &this_resid
);
724 /* truncate the file to its new length before write */
725 size
= (off_t
)shadow_map_shadow_size(vn
->sc_shadow_map
)
727 vnode_setsize(vn
->sc_shadow_vp
, size
, IO_SYNC
,
731 error
= file_io(vn
->sc_shadow_vp
, &context
, UIO_WRITE
,
733 (off_t
)this_offset
* blocksize
,
734 (user_ssize_t
)this_resid
* blocksize
,
739 this_resid
-= (temp_resid
/ blocksize
);
740 if (this_resid
== 0) {
741 printf("vn device: shadow_write zero length write\n");
745 offset
+= this_resid
;
746 start
+= this_resid
* blocksize
;
748 buf_setresid(bp
, resid
* blocksize
);
753 vn_readwrite_io(struct vn_softc
* vn
, struct buf
* bp
, struct proc
* p
)
760 if (buf_map(bp
, &vaddr
))
761 panic("vn device: buf_map failed");
762 iov_base
= (char *)vaddr
;
764 if (vn
->sc_shadow_vp
== NULL
) {
765 struct vfs_context context
;
766 user_ssize_t temp_resid
;
769 context
.vc_ucred
= vn
->sc_cred
;
771 error
= file_io(vn
->sc_vp
, &context
,
772 buf_flags(bp
) & B_READ
? UIO_READ
: UIO_WRITE
,
774 (off_t
)buf_blkno(bp
) * vn
->sc_secsize
,
775 buf_resid(bp
), &temp_resid
);
776 buf_setresid(bp
, temp_resid
);
779 if (buf_flags(bp
) & B_READ
)
780 error
= shadow_read(vn
, bp
, iov_base
, p
);
782 error
= shadow_write(vn
, bp
, iov_base
, p
);
790 vnstrategy(struct buf
*bp
)
794 long sz
; /* in sc_secsize chunks */
796 boolean_t funnel_state
;
797 struct proc
* p
= current_proc();
798 struct vnode
* shadow_vp
= NULL
;
799 struct vnode
* vp
= NULL
;
801 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
802 vn
= vn_table
+ vnunit(buf_device(bp
));
803 if ((vn
->sc_flags
& VNF_INITED
) == 0) {
808 buf_setresid(bp
, buf_count(bp
));
810 * Check for required alignment. Transfers must be a valid
811 * multiple of the sector size.
813 blk_num
= buf_blkno(bp
);
814 if (buf_count(bp
) % vn
->sc_secsize
!= 0) {
818 sz
= howmany(buf_count(bp
), vn
->sc_secsize
);
821 * If out of bounds return an error. If at the EOF point,
822 * simply read or write less.
824 if (blk_num
>= 0 && (u_int64_t
)blk_num
>= vn
->sc_size
) {
825 if (blk_num
> 0 && (u_int64_t
)blk_num
> vn
->sc_size
) {
831 * If the request crosses EOF, truncate the request.
833 if ((blk_num
+ sz
) > 0 && ((u_int64_t
)(blk_num
+ sz
)) > vn
->sc_size
) {
834 buf_setcount(bp
, (vn
->sc_size
- blk_num
) * vn
->sc_secsize
);
835 buf_setresid(bp
, buf_count(bp
));
842 error
= vnode_getwithvid(vp
, vn
->sc_vid
);
844 /* the vnode is no longer available, abort */
849 shadow_vp
= vn
->sc_shadow_vp
;
850 if (shadow_vp
!= NULL
) {
851 error
= vnode_getwithvid(shadow_vp
,
854 /* the vnode is no longer available, abort */
856 vnode_put(vn
->sc_vp
);
861 error
= vn_readwrite_io(vn
, bp
, p
);
863 if (shadow_vp
!= NULL
) {
864 vnode_put(shadow_vp
);
868 (void) thread_funnel_set(kernel_flock
, funnel_state
);
870 buf_seterror(bp
, error
);
878 vnioctl(dev_t dev
, u_long cmd
, caddr_t data
,
879 __unused
int flag
, struct proc
*p
,
883 struct user_vn_ioctl
*viop
;
888 struct vfsioattr ioattr
;
889 struct user_vn_ioctl user_vnio
;
890 boolean_t funnel_state
;
893 if (vnunit(dev
) >= NVNDEVICE
) {
897 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
898 vn
= vn_table
+ unit
;
899 error
= proc_suser(p
);
904 viop
= (struct user_vn_ioctl
*)data
;
905 f
= (u_int32_t
*)data
;
906 o
= (u_int64_t
*)data
;
910 case DKIOCGETBLOCKSIZE
:
911 case DKIOCSETBLOCKSIZE
:
912 case DKIOCGETMAXBLOCKCOUNTREAD
:
913 case DKIOCGETMAXBLOCKCOUNTWRITE
:
914 case DKIOCGETMAXSEGMENTCOUNTREAD
:
915 case DKIOCGETMAXSEGMENTCOUNTWRITE
:
916 case DKIOCGETMAXSEGMENTBYTECOUNTREAD
:
917 case DKIOCGETMAXSEGMENTBYTECOUNTWRITE
:
918 case DKIOCGETBLOCKCOUNT
:
919 case DKIOCGETBLOCKCOUNT32
:
920 if ((vn
->sc_flags
& VNF_INITED
) == 0) {
929 if (vn
->sc_vp
!= NULL
)
930 vfs_ioattr(vnode_mount(vn
->sc_vp
), &ioattr
);
932 bzero(&ioattr
, sizeof(ioattr
));
938 case DKIOCGETMAXBLOCKCOUNTREAD
:
939 *o
= ioattr
.io_maxreadcnt
/ vn
->sc_secsize
;
941 case DKIOCGETMAXBLOCKCOUNTWRITE
:
942 *o
= ioattr
.io_maxwritecnt
/ vn
->sc_secsize
;
944 case DKIOCGETMAXBYTECOUNTREAD
:
945 *o
= ioattr
.io_maxreadcnt
;
947 case DKIOCGETMAXBYTECOUNTWRITE
:
948 *o
= ioattr
.io_maxwritecnt
;
950 case DKIOCGETMAXSEGMENTCOUNTREAD
:
951 *o
= ioattr
.io_segreadcnt
;
953 case DKIOCGETMAXSEGMENTCOUNTWRITE
:
954 *o
= ioattr
.io_segwritecnt
;
956 case DKIOCGETMAXSEGMENTBYTECOUNTREAD
:
957 *o
= ioattr
.io_maxsegreadsize
;
959 case DKIOCGETMAXSEGMENTBYTECOUNTWRITE
:
960 *o
= ioattr
.io_maxsegwritesize
;
962 case DKIOCGETBLOCKSIZE
:
965 case DKIOCSETBLOCKSIZE
:
967 /* can only set block size on block device */
971 if (*f
< DEV_BSIZE
) {
975 if (vn
->sc_shadow_vp
!= NULL
) {
976 if (*f
== (unsigned)vn
->sc_secsize
) {
979 /* can't change the block size if already shadowing */
984 /* recompute the size in terms of the new blocksize */
985 vn
->sc_size
= vn
->sc_fsize
/ vn
->sc_secsize
;
987 case DKIOCISWRITABLE
:
990 case DKIOCGETBLOCKCOUNT32
:
993 case DKIOCGETBLOCKCOUNT
:
998 if (vn
->sc_shadow_vp
!= NULL
) {
1002 if (vn
->sc_vp
== NULL
) {
1003 /* much be attached before we can shadow */
1007 if (!proc_is64bit(p
)) {
1008 /* downstream code expects LP64 version of vn_ioctl structure */
1009 vn_ioctl_to_64((struct vn_ioctl
*)viop
, &user_vnio
);
1012 if (viop
->vn_file
== USER_ADDR_NULL
) {
1016 error
= vniocattach_shadow(vn
, viop
, dev
, 0, p
);
1022 /* attach only on block device */
1026 if (vn
->sc_flags
& VNF_INITED
) {
1030 if (!proc_is64bit(p
)) {
1031 /* downstream code expects LP64 version of vn_ioctl structure */
1032 vn_ioctl_to_64((struct vn_ioctl
*)viop
, &user_vnio
);
1035 if (viop
->vn_file
== USER_ADDR_NULL
) {
1039 error
= vniocattach_file(vn
, viop
, dev
, 0, p
);
1045 /* detach only on block device */
1049 /* Note: spec_open won't open a mounted block device */
1052 * XXX handle i/o in progress. Return EBUSY, or wait, or
1054 * XXX handle multiple opens of the device. Return EBUSY,
1055 * or revoke the fd's.
1056 * How are these problems handled for removable and failing
1057 * hardware devices? (Hint: They are not)
1068 vn_options
&= ~(*f
);
1073 vn
->sc_options
|= *f
;
1074 *f
= vn
->sc_options
;
1078 vn
->sc_options
&= ~(*f
);
1079 *f
= vn
->sc_options
;
1087 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1092 vnioctl_chr(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
1094 return (vnioctl(dev
, cmd
, data
, flag
, p
, TRUE
));
1098 vnioctl_blk(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
1100 return (vnioctl(dev
, cmd
, data
, flag
, p
, FALSE
));
1106 * Attach a file to a VN partition. Return the size in the vn_size
1111 vniocattach_file(struct vn_softc
*vn
,
1112 struct user_vn_ioctl
*vniop
,
1118 struct vfs_context context
;
1120 struct nameidata nd
;
1124 context
.vc_proc
= p
;
1125 context
.vc_ucred
= proc_ucred(p
);
1127 flags
= FREAD
|FWRITE
;
1129 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
, vniop
->vn_file
, &context
);
1132 NDINIT(&nd
, LOOKUP
, FOLLOW
,
1133 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1134 vniop
->vn_file
, &context
);
1136 /* vn_open gives both long- and short-term references */
1137 error
= vn_open(&nd
, flags
, 0);
1139 if (error
!= EACCES
&& error
!= EPERM
&& error
!= EROFS
)
1143 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
,
1144 vniop
->vn_file
, &context
);
1147 NDINIT(&nd
, LOOKUP
, FOLLOW
,
1148 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1149 vniop
->vn_file
, &context
);
1151 error
= vn_open(&nd
, flags
, 0);
1155 if (nd
.ni_vp
->v_type
!= VREG
) {
1159 error
= vnode_size(nd
.ni_vp
, &file_size
, &context
);
1162 (void) vn_close(nd
.ni_vp
, flags
, proc_ucred(p
), p
);
1163 vnode_put(nd
.ni_vp
);
1166 cred
= kauth_cred_proc_ref(p
);
1167 nd
.ni_vp
->v_flag
|= VNOCACHE_DATA
;
1168 error
= setcred(nd
.ni_vp
, p
, cred
);
1170 (void)vn_close(nd
.ni_vp
, flags
, proc_ucred(p
), p
);
1171 vnode_put(nd
.ni_vp
);
1172 kauth_cred_rele(cred
);
1175 vn
->sc_secsize
= DEV_BSIZE
;
1176 vn
->sc_fsize
= file_size
;
1177 vn
->sc_size
= file_size
/ vn
->sc_secsize
;
1178 vn
->sc_vp
= nd
.ni_vp
;
1179 vn
->sc_vid
= vnode_vid(nd
.ni_vp
);
1180 vn
->sc_open_flags
= flags
;
1182 cdev
= makedev(vndevice_cdev_major
, minor(dev
));
1183 vn
->sc_cdev
= devfs_make_node(cdev
, DEVFS_CHAR
,
1184 UID_ROOT
, GID_OPERATOR
,
1187 vn
->sc_flags
|= VNF_INITED
;
1189 vn
->sc_flags
|= VNF_READONLY
;
1190 /* lose the short-term reference */
1191 vnode_put(nd
.ni_vp
);
1196 vniocattach_shadow(struct vn_softc
*vn
, struct user_vn_ioctl
*vniop
,
1197 __unused
int dev
, int in_kernel
, struct proc
*p
)
1199 struct vfs_context context
;
1200 struct nameidata nd
;
1205 context
.vc_proc
= p
;
1206 context
.vc_ucred
= proc_ucred(p
);
1208 flags
= FREAD
|FWRITE
;
1210 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE32
, vniop
->vn_file
, &context
);
1213 NDINIT(&nd
, LOOKUP
, FOLLOW
,
1214 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
1215 vniop
->vn_file
, &context
);
1217 /* vn_open gives both long- and short-term references */
1218 error
= vn_open(&nd
, flags
, 0);
1220 /* shadow MUST be writable! */
1223 if (nd
.ni_vp
->v_type
!= VREG
1224 || (error
= vnode_size(nd
.ni_vp
, &file_size
, &context
))) {
1225 (void)vn_close(nd
.ni_vp
, flags
, proc_ucred(p
), p
);
1226 vnode_put(nd
.ni_vp
);
1227 return (error
? error
: EINVAL
);
1229 map
= shadow_map_create(vn
->sc_fsize
, file_size
,
1232 (void)vn_close(nd
.ni_vp
, flags
, proc_ucred(p
), p
);
1233 vnode_put(nd
.ni_vp
);
1234 vn
->sc_shadow_vp
= NULL
;
1237 vn
->sc_shadow_vp
= nd
.ni_vp
;
1238 vn
->sc_shadow_vid
= vnode_vid(nd
.ni_vp
);
1239 vn
->sc_shadow_vp
->v_flag
|= VNOCACHE_DATA
;
1240 vn
->sc_shadow_map
= map
;
1241 vn
->sc_flags
&= ~VNF_READONLY
; /* we're now read/write */
1243 /* lose the short-term reference */
1244 vnode_put(nd
.ni_vp
);
1249 vndevice_root_image(char * path
, char devname
[], dev_t
* dev_p
)
1252 struct vn_softc
* vn
;
1253 struct user_vn_ioctl vnio
;
1255 vnio
.vn_file
= CAST_USER_ADDR_T(path
);
1258 vn
= vn_table
+ ROOT_IMAGE_UNIT
;
1259 *dev_p
= makedev(vndevice_bdev_major
,
1261 sprintf(devname
, "vn%d", ROOT_IMAGE_UNIT
);
1262 error
= vniocattach_file(vn
, &vnio
, *dev_p
, 1, current_proc());
1267 * Duplicate the current processes' credentials. Since we are called only
1268 * as the result of a SET ioctl and only root can do that, any future access
1269 * to this "disk" is essentially as root. Note that credentials may change
1270 * if some other uid can write directly to the mapped file (NFS).
1273 setcred(struct vnode
* vp
, struct proc
* p
, kauth_cred_t cred
)
1277 struct vfs_context context
;
1280 * Horrible kludge to establish credentials for NFS XXX.
1282 context
.vc_proc
= p
;
1283 context
.vc_ucred
= cred
;
1284 tmpbuf
= _MALLOC(DEV_BSIZE
, M_TEMP
, M_WAITOK
);
1285 error
= file_io(vp
, &context
, UIO_READ
, tmpbuf
, 0, DEV_BSIZE
, NULL
);
1286 FREE(tmpbuf
, M_TEMP
);
1291 vnclear(struct vn_softc
*vn
, struct proc
* p
)
1293 if (vn
->sc_vp
!= NULL
) {
1294 /* release long-term reference */
1295 (void)vn_close(vn
->sc_vp
, vn
->sc_open_flags
, vn
->sc_cred
, p
);
1298 if (vn
->sc_shadow_vp
!= NULL
) {
1299 /* release long-term reference */
1300 (void)vn_close(vn
->sc_shadow_vp
, FREAD
| FWRITE
,
1302 vn
->sc_shadow_vp
= NULL
;
1304 if (vn
->sc_shadow_map
!= NULL
) {
1305 shadow_map_free(vn
->sc_shadow_map
);
1306 vn
->sc_shadow_map
= NULL
;
1308 vn
->sc_flags
&= ~(VNF_INITED
| VNF_READONLY
);
1310 kauth_cred_rele(vn
->sc_cred
);
1316 devfs_remove(vn
->sc_cdev
);
1325 struct vn_softc
*vn
;
1327 boolean_t funnel_state
;
1330 if (vnunit(dev
) >= NVNDEVICE
) {
1334 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1335 vn
= vn_table
+ unit
;
1336 if ((vn
->sc_flags
& VNF_INITED
) == 0)
1339 secsize
= vn
->sc_secsize
;
1340 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1344 #define CDEV_MAJOR -1
1345 #define BDEV_MAJOR -1
1346 static int vndevice_inited
= 0;
1353 if (vndevice_inited
)
1355 vndevice_bdev_major
= bdevsw_add(BDEV_MAJOR
, &vn_bdevsw
);
1357 if (vndevice_bdev_major
< 0) {
1358 printf("vndevice_init: bdevsw_add() returned %d\n",
1359 vndevice_bdev_major
);
1362 vndevice_cdev_major
= cdevsw_add_with_bdev(CDEV_MAJOR
, &vn_cdevsw
,
1363 vndevice_bdev_major
);
1364 if (vndevice_cdev_major
< 0) {
1365 printf("vndevice_init: cdevsw_add() returned %d\n",
1366 vndevice_cdev_major
);
1369 for (i
= 0; i
< NVNDEVICE
; i
++) {
1370 dev_t dev
= makedev(vndevice_bdev_major
, i
);
1371 vn_table
[i
].sc_bdev
= devfs_make_node(dev
, DEVFS_BLOCK
,
1372 UID_ROOT
, GID_OPERATOR
,
1375 if (vn_table
[i
].sc_bdev
== NULL
)
1376 printf("vninit: devfs_make_node failed!\n");
1381 vn_ioctl_to_64(struct vn_ioctl
*from
, struct user_vn_ioctl
*to
)
1383 to
->vn_file
= CAST_USER_ADDR_T(from
->vn_file
);
1384 to
->vn_size
= from
->vn_size
;
1385 to
->vn_control
= from
->vn_control
;
1388 #endif /* NVNDEVICE */