]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/vn/vn.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / bsd / dev / vn / vn.c
CommitLineData
91447636 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636 27 */
9bccf70c
A
28
29/*
30 * Copyright (c) 1988 University of Utah.
31 * Copyright (c) 1990, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * the Systems Programming Group of the University of Utah Computer
36 * Science Department.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * from: Utah Hdr: vn.c 1.13 94/04/02
67 *
68 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
69 * $FreeBSD: src/sys/dev/vn/vn.c,v 1.105.2.4 2001/11/18 07:11:00 dillon Exp $
70 */
71
72/*
73 * Vnode disk driver.
74 *
75 * Block/character interface to a vnode. Allows one to treat a file
76 * as a disk (e.g. build a filesystem in it, mount it, etc.).
77 *
91447636 78 * NOTE 1: This uses the vnop_blockmap/vnop_strategy interface to the vnode
9bccf70c
A
79 * instead of a simple VOP_RDWR. We do this to avoid distorting the
80 * local buffer cache.
81 *
82 * NOTE 2: There is a security issue involved with this driver.
83 * Once mounted all access to the contents of the "mapped" file via
84 * the special file is controlled by the permissions on the special
85 * file, the protection of the mapped file is ignored (effectively,
86 * by using root credentials in all transactions).
87 *
88 * NOTE 3: Doesn't interact with leases, should it?
89 */
90
91#include "vndevice.h"
92
93#if NVNDEVICE > 0
94
95#include <sys/param.h>
96#include <sys/systm.h>
97#include <sys/kernel.h>
98#include <sys/mount.h>
99#include <sys/namei.h>
100#include <sys/proc.h>
91447636 101#include <sys/kauth.h>
9bccf70c
A
102#include <sys/buf.h>
103#include <sys/malloc.h>
91447636 104#include <sys/vnode_internal.h>
9bccf70c
A
105#include <sys/fcntl.h>
106#include <sys/conf.h>
55e303ae 107#include <sys/disk.h>
9bccf70c
A
108#include <sys/stat.h>
109#include <sys/conf.h>
91447636 110#include <sys/uio_internal.h>
9bccf70c
A
111
112#include <sys/vnioctl.h>
113
114#include <sys/vm.h>
115
116#include <vm/vm_pager.h>
9bccf70c
A
117#include <mach/memory_object_types.h>
118
119#include <miscfs/devfs/devfs.h>
120
55e303ae 121
9bccf70c 122#include "shadow.h"
2d21ac55 123static void
39236c6e 124vndevice_do_init(void);
9bccf70c
A
125
126static ioctl_fcn_t vnioctl_chr;
127static ioctl_fcn_t vnioctl_blk;
128static open_close_fcn_t vnopen;
129static open_close_fcn_t vnclose;
130static psize_fcn_t vnsize;
131static strategy_fcn_t vnstrategy;
132static read_write_fcn_t vnread;
133static read_write_fcn_t vnwrite;
134
135static int vndevice_bdev_major;
136static int vndevice_cdev_major;
137
138/*
139 * cdevsw
140 * D_DISK we want to look like a disk
141 * D_CANFREE We support B_FREEBUF
142 */
143
144static struct bdevsw vn_bdevsw = {
145 /* open */ vnopen,
146 /* close */ vnclose,
147 /* strategy */ vnstrategy,
148 /* ioctl */ vnioctl_blk,
149 /* dump */ eno_dump,
150 /* psize */ vnsize,
151 /* flags */ D_DISK,
152};
153
154static struct cdevsw vn_cdevsw = {
155 /* open */ vnopen,
156 /* close */ vnclose,
157 /* read */ vnread,
158 /* write */ vnwrite,
159 /* ioctl */ vnioctl_chr,
160 /* stop */ eno_stop,
161 /* reset */ eno_reset,
2d21ac55 162 /* ttys */ NULL,
9bccf70c
A
163 /* select */ eno_select,
164 /* mmap */ eno_mmap,
165 /* strategy */ eno_strat,
166 /* getc */ eno_getc,
167 /* putc */ eno_putc,
168 /* flags */ D_DISK,
169};
170
171struct vn_softc {
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 */
b0d623f7 175 u_int32_t sc_secsize; /* sector size */
9bccf70c 176 struct vnode *sc_vp; /* vnode if not NULL */
91447636 177 uint32_t sc_vid;
9bccf70c
A
178 int sc_open_flags;
179 struct vnode *sc_shadow_vp; /* shadow vnode if not NULL */
91447636 180 uint32_t sc_shadow_vid;
9bccf70c 181 shadow_map_t * sc_shadow_map; /* shadow map if not NULL */
2d21ac55 182 kauth_cred_t sc_cred; /* credentials */
91447636 183 u_int32_t sc_options; /* options */
9bccf70c
A
184 void * sc_bdev;
185 void * sc_cdev;
186} vn_table[NVNDEVICE];
187
188#define ROOT_IMAGE_UNIT 0
189
190/* sc_flags */
191#define VNF_INITED 0x01
192#define VNF_READONLY 0x02
193
91447636 194static u_int32_t vn_options;
9bccf70c
A
195
196#define IFOPT(vn,opt) if (((vn)->sc_options|vn_options) & (opt))
197#define TESTOPT(vn,opt) (((vn)->sc_options|vn_options) & (opt))
198
2d21ac55
A
199static int setcred(struct vnode * vp, kauth_cred_t cred);
200static void vnclear (struct vn_softc *vn, vfs_context_t ctx);
b0d623f7 201static void vn_ioctl_to_64(struct vn_ioctl_32 *from, struct vn_ioctl_64 *to);
91447636
A
202void vndevice_init(void);
203int vndevice_root_image(char * path, char devname[], dev_t * dev_p);
9bccf70c
A
204
205static int
206vniocattach_file(struct vn_softc *vn,
b0d623f7 207 struct vn_ioctl_64 *vniop,
9bccf70c
A
208 dev_t dev,
209 int in_kernel,
2d21ac55 210 proc_t p);
9bccf70c
A
211static int
212vniocattach_shadow(struct vn_softc * vn,
b0d623f7 213 struct vn_ioctl_64 *vniop,
9bccf70c
A
214 dev_t dev,
215 int in_kernel,
2d21ac55 216 proc_t p);
91447636 217static __inline__ int
9bccf70c
A
218vnunit(dev_t dev)
219{
220 return (minor(dev));
221}
222
223static int
91447636 224vnclose(__unused dev_t dev, __unused int flags,
2d21ac55 225 __unused int devtype, __unused proc_t p)
9bccf70c
A
226{
227 return (0);
228}
229
230static int
2d21ac55 231vnopen(dev_t dev, int flags, __unused int devtype, __unused proc_t p)
9bccf70c
A
232{
233 struct vn_softc *vn;
234 int unit;
235
236 unit = vnunit(dev);
237 if (vnunit(dev) >= NVNDEVICE) {
238 return (ENXIO);
239 }
240 vn = vn_table + unit;
241 if ((flags & FWRITE) && (vn->sc_flags & VNF_READONLY))
242 return (EACCES);
243
244 return(0);
245}
246
91447636 247static int
2d21ac55 248file_io(struct vnode * vp, vfs_context_t ctx,
91447636
A
249 enum uio_rw op, char * base, off_t offset, user_ssize_t count,
250 user_ssize_t * resid)
251{
252 uio_t auio;
253 int error;
254 char uio_buf[UIO_SIZEOF(1)];
255
256 auio = uio_createwithbuffer(1, offset, UIO_SYSSPACE, op,
257 &uio_buf[0], sizeof(uio_buf));
258 uio_addiov(auio, CAST_USER_ADDR_T(base), count);
259 if (op == UIO_READ)
2d21ac55 260 error = VNOP_READ(vp, auio, IO_SYNC, ctx);
91447636 261 else
2d21ac55 262 error = VNOP_WRITE(vp, auio, IO_SYNC, ctx);
91447636
A
263
264 if (resid != NULL) {
265 *resid = uio_resid(auio);
266 }
267 return (error);
268}
269
270static __inline__ off_t
271block_round(off_t o, int blocksize)
272{
273 return ((o + blocksize - 1) / blocksize);
274}
275
276static __inline__ off_t
277block_truncate(off_t o, int blocksize)
278{
279 return (o / blocksize);
280}
281
282static __inline__ int
283block_remainder(off_t o, int blocksize)
284{
285 return (o % blocksize);
286}
287
288static int
289vnread_shadow(struct vn_softc * vn, struct uio *uio, int ioflag,
2d21ac55 290 vfs_context_t ctx)
91447636 291{
b0d623f7 292 u_int32_t blocksize = vn->sc_secsize;
91447636
A
293 int error = 0;
294 off_t offset;
295 user_ssize_t resid;
296 off_t orig_offset;
297 user_ssize_t orig_resid;
298
299 orig_resid = resid = uio_resid(uio);
300 orig_offset = offset = uio_offset(uio);
301
302 while (resid > 0) {
b0d623f7
A
303 u_int32_t remainder;
304 u_int32_t this_block_number;
305 u_int32_t this_block_count;
91447636
A
306 off_t this_offset;
307 user_ssize_t this_resid;
308 struct vnode * vp;
309
310 /* figure out which blocks to read */
311 remainder = block_remainder(offset, blocksize);
312 if (shadow_map_read(vn->sc_shadow_map,
313 block_truncate(offset, blocksize),
314 block_round(resid + remainder, blocksize),
315 &this_block_number, &this_block_count)) {
316 vp = vn->sc_shadow_vp;
317 }
318 else {
319 vp = vn->sc_vp;
320 }
321
322 /* read the blocks (or parts thereof) */
323 this_offset = (off_t)this_block_number * blocksize + remainder;
324 uio_setoffset(uio, this_offset);
325 this_resid = this_block_count * blocksize - remainder;
326 if (this_resid > resid) {
327 this_resid = resid;
328 }
329 uio_setresid(uio, this_resid);
2d21ac55 330 error = VNOP_READ(vp, uio, ioflag, ctx);
91447636
A
331 if (error) {
332 break;
333 }
334
335 /* figure out how much we actually read */
336 this_resid -= uio_resid(uio);
337 if (this_resid == 0) {
338 printf("vn device: vnread_shadow zero length read\n");
339 break;
340 }
341 resid -= this_resid;
342 offset += this_resid;
343 }
344 uio_setresid(uio, resid);
345 uio_setoffset(uio, offset);
346 return (error);
347}
348
349static int
2d21ac55 350vncopy_block_to_shadow(struct vn_softc * vn, vfs_context_t ctx,
b0d623f7 351 u_int32_t file_block, u_int32_t shadow_block)
91447636
A
352{
353 int error;
354 char * tmpbuf;
355
356 tmpbuf = _MALLOC(vn->sc_secsize, M_TEMP, M_WAITOK);
357 if (tmpbuf == NULL) {
358 return (ENOMEM);
359 }
360 /* read one block from file at file_block offset */
2d21ac55 361 error = file_io(vn->sc_vp, ctx, UIO_READ,
91447636
A
362 tmpbuf, (off_t)file_block * vn->sc_secsize,
363 vn->sc_secsize, NULL);
364 if (error) {
365 goto done;
366 }
367 /* write one block to shadow file at shadow_block offset */
2d21ac55 368 error = file_io(vn->sc_shadow_vp, ctx, UIO_WRITE,
91447636
A
369 tmpbuf, (off_t)shadow_block * vn->sc_secsize,
370 vn->sc_secsize, NULL);
371 done:
372 FREE(tmpbuf, M_TEMP);
373 return (error);
374}
375
376enum {
377 FLAGS_FIRST_BLOCK_PARTIAL = 0x1,
378 FLAGS_LAST_BLOCK_PARTIAL = 0x2
379};
380
381static int
382vnwrite_shadow(struct vn_softc * vn, struct uio *uio, int ioflag,
2d21ac55 383 vfs_context_t ctx)
91447636 384{
b0d623f7 385 u_int32_t blocksize = vn->sc_secsize;
91447636
A
386 int error = 0;
387 user_ssize_t resid;
388 off_t offset;
389
390 resid = uio_resid(uio);
391 offset = uio_offset(uio);
392
393 while (resid > 0) {
394 int flags = 0;
b0d623f7
A
395 u_int32_t offset_block_number;
396 u_int32_t remainder;
397 u_int32_t resid_block_count;
398 u_int32_t shadow_block_count;
399 u_int32_t shadow_block_number;
91447636
A
400 user_ssize_t this_resid;
401
402 /* figure out which blocks to write */
403 offset_block_number = block_truncate(offset, blocksize);
404 remainder = block_remainder(offset, blocksize);
405 resid_block_count = block_round(resid + remainder, blocksize);
406 /* figure out if the first or last blocks are partial writes */
407 if (remainder > 0
408 && !shadow_map_is_written(vn->sc_shadow_map,
409 offset_block_number)) {
410 /* the first block is a partial write */
411 flags |= FLAGS_FIRST_BLOCK_PARTIAL;
412 }
413 if (resid_block_count > 1
414 && !shadow_map_is_written(vn->sc_shadow_map,
415 offset_block_number
416 + resid_block_count - 1)
417 && block_remainder(offset + resid, blocksize) > 0) {
418 /* the last block is a partial write */
419 flags |= FLAGS_LAST_BLOCK_PARTIAL;
420 }
421 if (shadow_map_write(vn->sc_shadow_map,
422 offset_block_number, resid_block_count,
423 &shadow_block_number,
424 &shadow_block_count)) {
425 /* shadow file is growing */
426#if 0
427 /* truncate the file to its new length before write */
428 off_t size;
429 size = (off_t)shadow_map_shadow_size(vn->sc_shadow_map)
430 * vn->sc_secsize;
2d21ac55 431 vnode_setsize(vn->sc_shadow_vp, size, IO_SYNC, ctx);
b0d623f7 432#endif
91447636
A
433 }
434 /* write the blocks (or parts thereof) */
435 uio_setoffset(uio, (off_t)
436 shadow_block_number * blocksize + remainder);
437 this_resid = (off_t)shadow_block_count * blocksize - remainder;
438 if (this_resid >= resid) {
439 this_resid = resid;
440 if ((flags & FLAGS_LAST_BLOCK_PARTIAL) != 0) {
441 /* copy the last block to the shadow */
b0d623f7
A
442 u_int32_t d;
443 u_int32_t s;
91447636
A
444
445 s = offset_block_number
446 + resid_block_count - 1;
447 d = shadow_block_number
448 + shadow_block_count - 1;
2d21ac55 449 error = vncopy_block_to_shadow(vn, ctx, s, d);
91447636
A
450 if (error) {
451 printf("vnwrite_shadow: failed to copy"
b0d623f7 452 " block %u to shadow block %u\n",
91447636
A
453 s, d);
454 break;
455 }
456 }
457 }
458 uio_setresid(uio, this_resid);
459 if ((flags & FLAGS_FIRST_BLOCK_PARTIAL) != 0) {
460 /* copy the first block to the shadow */
2d21ac55 461 error = vncopy_block_to_shadow(vn, ctx,
91447636
A
462 offset_block_number,
463 shadow_block_number);
464 if (error) {
465 printf("vnwrite_shadow: failed to"
b0d623f7 466 " copy block %u to shadow block %u\n",
91447636
A
467 offset_block_number,
468 shadow_block_number);
469 break;
470 }
471 }
2d21ac55 472 error = VNOP_WRITE(vn->sc_shadow_vp, uio, ioflag, ctx);
91447636
A
473 if (error) {
474 break;
475 }
476 /* figure out how much we actually wrote */
477 this_resid -= uio_resid(uio);
478 if (this_resid == 0) {
479 printf("vn device: vnwrite_shadow zero length write\n");
480 break;
481 }
482 resid -= this_resid;
483 offset += this_resid;
484 }
485 uio_setresid(uio, resid);
486 uio_setoffset(uio, offset);
487 return (error);
488}
489
9bccf70c
A
490static int
491vnread(dev_t dev, struct uio *uio, int ioflag)
492{
91447636
A
493 struct vfs_context context;
494 int error = 0;
91447636 495 off_t offset;
2d21ac55 496 proc_t p;
91447636 497 user_ssize_t resid;
9bccf70c
A
498 struct vn_softc * vn;
499 int unit;
500
501 unit = vnunit(dev);
502 if (vnunit(dev) >= NVNDEVICE) {
503 return (ENXIO);
504 }
91447636 505 p = current_proc();
9bccf70c
A
506 vn = vn_table + unit;
507 if ((vn->sc_flags & VNF_INITED) == 0) {
91447636
A
508 error = ENXIO;
509 goto done;
9bccf70c 510 }
2d21ac55
A
511
512 context.vc_thread = current_thread();
513 context.vc_ucred = vn->sc_cred;
514
91447636
A
515 error = vnode_getwithvid(vn->sc_vp, vn->sc_vid);
516 if (error != 0) {
517 /* the vnode is no longer available, abort */
518 error = ENXIO;
2d21ac55 519 vnclear(vn, &context);
91447636
A
520 goto done;
521 }
522
523 resid = uio_resid(uio);
524 offset = uio_offset(uio);
525
526 /*
527 * If out of bounds return an error. If at the EOF point,
528 * simply read less.
529 */
530 if (offset >= (off_t)vn->sc_fsize) {
531 if (offset > (off_t)vn->sc_fsize) {
532 error = EINVAL;
533 }
534 goto done;
535 }
536 /*
537 * If the request crosses EOF, truncate the request.
538 */
539 if ((offset + resid) > (off_t)vn->sc_fsize) {
540 resid = vn->sc_fsize - offset;
541 uio_setresid(uio, resid);
9bccf70c 542 }
9bccf70c 543
91447636
A
544 if (vn->sc_shadow_vp != NULL) {
545 error = vnode_getwithvid(vn->sc_shadow_vp,
546 vn->sc_shadow_vid);
547 if (error != 0) {
548 /* the vnode is no longer available, abort */
549 error = ENXIO;
550 vnode_put(vn->sc_vp);
2d21ac55 551 vnclear(vn, &context);
91447636
A
552 goto done;
553 }
554 error = vnread_shadow(vn, uio, ioflag, &context);
555 vnode_put(vn->sc_shadow_vp);
556 } else {
557 error = VNOP_READ(vn->sc_vp, uio, ioflag, &context);
558 }
559 vnode_put(vn->sc_vp);
560 done:
91447636 561 return (error);
9bccf70c
A
562}
563
564static int
565vnwrite(dev_t dev, struct uio *uio, int ioflag)
566{
91447636
A
567 struct vfs_context context;
568 int error;
91447636 569 off_t offset;
2d21ac55 570 proc_t p;
91447636 571 user_ssize_t resid;
9bccf70c
A
572 struct vn_softc * vn;
573 int unit;
574
575 unit = vnunit(dev);
576 if (vnunit(dev) >= NVNDEVICE) {
577 return (ENXIO);
578 }
91447636 579 p = current_proc();
9bccf70c
A
580 vn = vn_table + unit;
581 if ((vn->sc_flags & VNF_INITED) == 0) {
91447636
A
582 error = ENXIO;
583 goto done;
9bccf70c
A
584 }
585 if (vn->sc_flags & VNF_READONLY) {
91447636
A
586 error = EROFS;
587 goto done;
9bccf70c 588 }
2d21ac55
A
589
590 context.vc_thread = current_thread();
591 context.vc_ucred = vn->sc_cred;
592
91447636
A
593 error = vnode_getwithvid(vn->sc_vp, vn->sc_vid);
594 if (error != 0) {
595 /* the vnode is no longer available, abort */
596 error = ENXIO;
2d21ac55 597 vnclear(vn, &context);
91447636
A
598 goto done;
599 }
600 resid = uio_resid(uio);
601 offset = uio_offset(uio);
9bccf70c 602
91447636
A
603 /*
604 * If out of bounds return an error. If at the EOF point,
605 * simply write less.
606 */
607 if (offset >= (off_t)vn->sc_fsize) {
608 if (offset > (off_t)vn->sc_fsize) {
609 error = EINVAL;
610 }
611 goto done;
612 }
613 /*
614 * If the request crosses EOF, truncate the request.
615 */
616 if ((offset + resid) > (off_t)vn->sc_fsize) {
617 resid = (off_t)vn->sc_fsize - offset;
618 uio_setresid(uio, resid);
619 }
9bccf70c 620
91447636
A
621 if (vn->sc_shadow_vp != NULL) {
622 error = vnode_getwithvid(vn->sc_shadow_vp,
623 vn->sc_shadow_vid);
624 if (error != 0) {
625 /* the vnode is no longer available, abort */
626 error = ENXIO;
627 vnode_put(vn->sc_vp);
2d21ac55 628 vnclear(vn, &context);
91447636 629 goto done;
9bccf70c 630 }
91447636
A
631 error = vnwrite_shadow(vn, uio, ioflag, &context);
632 vnode_put(vn->sc_shadow_vp);
633 } else {
634 error = VNOP_WRITE(vn->sc_vp, uio, ioflag, &context);
9bccf70c 635 }
91447636
A
636 vnode_put(vn->sc_vp);
637 done:
9bccf70c
A
638 return (error);
639}
640
641static int
2d21ac55
A
642shadow_read(struct vn_softc * vn, struct buf * bp, char * base,
643 vfs_context_t ctx)
9bccf70c 644{
b0d623f7 645 u_int32_t blocksize = vn->sc_secsize;
9bccf70c 646 int error = 0;
b0d623f7 647 u_int32_t offset;
9bccf70c 648 boolean_t read_shadow;
b0d623f7
A
649 u_int32_t resid;
650 u_int32_t start = 0;
9bccf70c 651
91447636
A
652 offset = buf_blkno(bp);
653 resid = buf_resid(bp) / blocksize;
9bccf70c 654 while (resid > 0) {
91447636 655 user_ssize_t temp_resid;
b0d623f7
A
656 u_int32_t this_offset;
657 u_int32_t this_resid;
9bccf70c
A
658 struct vnode * vp;
659
660 read_shadow = shadow_map_read(vn->sc_shadow_map,
661 offset, resid,
662 &this_offset, &this_resid);
663 if (read_shadow) {
664 vp = vn->sc_shadow_vp;
665 }
666 else {
667 vp = vn->sc_vp;
668 }
2d21ac55 669 error = file_io(vp, ctx, UIO_READ, base + start,
91447636
A
670 (off_t)this_offset * blocksize,
671 (user_ssize_t)this_resid * blocksize,
672 &temp_resid);
673 if (error) {
9bccf70c 674 break;
91447636
A
675 }
676 this_resid -= (temp_resid / blocksize);
677 if (this_resid == 0) {
678 printf("vn device: shadow_read zero length read\n");
9bccf70c
A
679 break;
680 }
91447636
A
681 resid -= this_resid;
682 offset += this_resid;
683 start += this_resid * blocksize;
9bccf70c 684 }
91447636 685 buf_setresid(bp, resid * blocksize);
9bccf70c
A
686 return (error);
687}
688
689static int
690shadow_write(struct vn_softc * vn, struct buf * bp, char * base,
2d21ac55 691 vfs_context_t ctx)
9bccf70c 692{
b0d623f7 693 u_int32_t blocksize = vn->sc_secsize;
9bccf70c 694 int error = 0;
b0d623f7 695 u_int32_t offset;
9bccf70c 696 boolean_t shadow_grew;
b0d623f7
A
697 u_int32_t resid;
698 u_int32_t start = 0;
9bccf70c 699
91447636
A
700 offset = buf_blkno(bp);
701 resid = buf_resid(bp) / blocksize;
9bccf70c 702 while (resid > 0) {
91447636 703 user_ssize_t temp_resid;
b0d623f7
A
704 u_int32_t this_offset;
705 u_int32_t this_resid;
9bccf70c
A
706
707 shadow_grew = shadow_map_write(vn->sc_shadow_map,
708 offset, resid,
709 &this_offset, &this_resid);
710 if (shadow_grew) {
711#if 0
712 off_t size;
713 /* truncate the file to its new length before write */
714 size = (off_t)shadow_map_shadow_size(vn->sc_shadow_map)
91447636 715 * blocksize;
2d21ac55 716 vnode_setsize(vn->sc_shadow_vp, size, IO_SYNC, ctx);
55e303ae 717#endif
9bccf70c 718 }
2d21ac55 719 error = file_io(vn->sc_shadow_vp, ctx, UIO_WRITE,
9bccf70c 720 base + start,
91447636
A
721 (off_t)this_offset * blocksize,
722 (user_ssize_t)this_resid * blocksize,
723 &temp_resid);
9bccf70c
A
724 if (error) {
725 break;
726 }
91447636
A
727 this_resid -= (temp_resid / blocksize);
728 if (this_resid == 0) {
729 printf("vn device: shadow_write zero length write\n");
9bccf70c
A
730 break;
731 }
91447636
A
732 resid -= this_resid;
733 offset += this_resid;
734 start += this_resid * blocksize;
9bccf70c 735 }
91447636 736 buf_setresid(bp, resid * blocksize);
9bccf70c
A
737 return (error);
738}
739
740static int
2d21ac55 741vn_readwrite_io(struct vn_softc * vn, struct buf * bp, vfs_context_t ctx)
9bccf70c
A
742{
743 int error = 0;
744 char * iov_base;
91447636 745 caddr_t vaddr;
9bccf70c 746
91447636
A
747 if (buf_map(bp, &vaddr))
748 panic("vn device: buf_map failed");
749 iov_base = (char *)vaddr;
750
9bccf70c 751 if (vn->sc_shadow_vp == NULL) {
91447636
A
752 user_ssize_t temp_resid;
753
2d21ac55 754 error = file_io(vn->sc_vp, ctx,
91447636
A
755 buf_flags(bp) & B_READ ? UIO_READ : UIO_WRITE,
756 iov_base,
757 (off_t)buf_blkno(bp) * vn->sc_secsize,
758 buf_resid(bp), &temp_resid);
759 buf_setresid(bp, temp_resid);
9bccf70c
A
760 }
761 else {
91447636 762 if (buf_flags(bp) & B_READ)
2d21ac55 763 error = shadow_read(vn, bp, iov_base, ctx);
9bccf70c 764 else
2d21ac55 765 error = shadow_write(vn, bp, iov_base, ctx);
9bccf70c 766 }
91447636
A
767 buf_unmap(bp);
768
9bccf70c
A
769 return (error);
770}
771
772static void
773vnstrategy(struct buf *bp)
774{
775 struct vn_softc *vn;
776 int error = 0;
777 long sz; /* in sc_secsize chunks */
91447636 778 daddr64_t blk_num;
91447636
A
779 struct vnode * shadow_vp = NULL;
780 struct vnode * vp = NULL;
2d21ac55 781 struct vfs_context context;
9bccf70c 782
91447636 783 vn = vn_table + vnunit(buf_device(bp));
9bccf70c 784 if ((vn->sc_flags & VNF_INITED) == 0) {
91447636
A
785 error = ENXIO;
786 goto done;
9bccf70c
A
787 }
788
2d21ac55
A
789 context.vc_thread = current_thread();
790 context.vc_ucred = vn->sc_cred;
791
91447636 792 buf_setresid(bp, buf_count(bp));
9bccf70c
A
793 /*
794 * Check for required alignment. Transfers must be a valid
795 * multiple of the sector size.
796 */
91447636
A
797 blk_num = buf_blkno(bp);
798 if (buf_count(bp) % vn->sc_secsize != 0) {
799 error = EINVAL;
800 goto done;
9bccf70c 801 }
91447636 802 sz = howmany(buf_count(bp), vn->sc_secsize);
9bccf70c
A
803
804 /*
805 * If out of bounds return an error. If at the EOF point,
806 * simply read or write less.
807 */
91447636
A
808 if (blk_num >= 0 && (u_int64_t)blk_num >= vn->sc_size) {
809 if (blk_num > 0 && (u_int64_t)blk_num > vn->sc_size) {
810 error = EINVAL;
55e303ae 811 }
91447636 812 goto done;
9bccf70c
A
813 }
814 /*
815 * If the request crosses EOF, truncate the request.
816 */
91447636
A
817 if ((blk_num + sz) > 0 && ((u_int64_t)(blk_num + sz)) > vn->sc_size) {
818 buf_setcount(bp, (vn->sc_size - blk_num) * vn->sc_secsize);
819 buf_setresid(bp, buf_count(bp));
9bccf70c 820 }
91447636
A
821 vp = vn->sc_vp;
822 if (vp == NULL) {
823 error = ENXIO;
824 goto done;
825 }
2d21ac55 826
91447636
A
827 error = vnode_getwithvid(vp, vn->sc_vid);
828 if (error != 0) {
829 /* the vnode is no longer available, abort */
830 error = ENXIO;
2d21ac55 831 vnclear(vn, &context);
91447636
A
832 goto done;
833 }
834 shadow_vp = vn->sc_shadow_vp;
835 if (shadow_vp != NULL) {
836 error = vnode_getwithvid(shadow_vp,
837 vn->sc_shadow_vid);
838 if (error != 0) {
839 /* the vnode is no longer available, abort */
840 error = ENXIO;
841 vnode_put(vn->sc_vp);
2d21ac55 842 vnclear(vn, &context);
91447636 843 goto done;
9bccf70c 844 }
9bccf70c 845 }
2d21ac55
A
846
847 error = vn_readwrite_io(vn, bp, &context);
91447636
A
848 vnode_put(vp);
849 if (shadow_vp != NULL) {
850 vnode_put(shadow_vp);
9bccf70c 851 }
91447636
A
852
853 done:
91447636
A
854 if (error) {
855 buf_seterror(bp, error);
856 }
857 buf_biodone(bp);
858 return;
9bccf70c
A
859}
860
861/* ARGSUSED */
862static int
91447636 863vnioctl(dev_t dev, u_long cmd, caddr_t data,
2d21ac55 864 __unused int flag, proc_t p,
9bccf70c
A
865 int is_char)
866{
867 struct vn_softc *vn;
b0d623f7 868 struct vn_ioctl_64 *viop;
9bccf70c 869 int error;
91447636 870 u_int32_t *f;
9bccf70c
A
871 u_int64_t * o;
872 int unit;
91447636 873 struct vfsioattr ioattr;
b0d623f7 874 struct vn_ioctl_64 user_vnio;
fe8ab488 875 struct vfs_context context;
9bccf70c
A
876
877 unit = vnunit(dev);
878 if (vnunit(dev) >= NVNDEVICE) {
879 return (ENXIO);
880 }
91447636 881
9bccf70c 882 vn = vn_table + unit;
91447636
A
883 error = proc_suser(p);
884 if (error) {
885 goto done;
886 }
9bccf70c 887
2d21ac55
A
888 context.vc_thread = current_thread();
889 context.vc_ucred = vn->sc_cred;
890
b0d623f7 891 viop = (struct vn_ioctl_64 *)data;
91447636 892 f = (u_int32_t *)data;
9bccf70c
A
893 o = (u_int64_t *)data;
894 switch (cmd) {
b0d623f7
A
895#ifdef __LP64__
896 case VNIOCDETACH32:
897 case VNIOCDETACH:
898#else
9bccf70c 899 case VNIOCDETACH:
91447636 900 case VNIOCDETACH64:
b0d623f7 901#endif
55e303ae 902 case DKIOCGETBLOCKSIZE:
91447636 903 case DKIOCSETBLOCKSIZE:
9bccf70c
A
904 case DKIOCGETMAXBLOCKCOUNTREAD:
905 case DKIOCGETMAXBLOCKCOUNTWRITE:
906 case DKIOCGETMAXSEGMENTCOUNTREAD:
907 case DKIOCGETMAXSEGMENTCOUNTWRITE:
55e303ae
A
908 case DKIOCGETMAXSEGMENTBYTECOUNTREAD:
909 case DKIOCGETMAXSEGMENTBYTECOUNTWRITE:
910 case DKIOCGETBLOCKCOUNT:
9bccf70c 911 if ((vn->sc_flags & VNF_INITED) == 0) {
91447636
A
912 error = ENXIO;
913 goto done;
9bccf70c
A
914 }
915 break;
916 default:
917 break;
918 }
91447636
A
919
920 if (vn->sc_vp != NULL)
921 vfs_ioattr(vnode_mount(vn->sc_vp), &ioattr);
922 else
923 bzero(&ioattr, sizeof(ioattr));
924
9bccf70c 925 switch (cmd) {
91447636
A
926 case DKIOCISVIRTUAL:
927 *f = 1;
928 break;
9bccf70c 929 case DKIOCGETMAXBLOCKCOUNTREAD:
91447636 930 *o = ioattr.io_maxreadcnt / vn->sc_secsize;
9bccf70c
A
931 break;
932 case DKIOCGETMAXBLOCKCOUNTWRITE:
91447636 933 *o = ioattr.io_maxwritecnt / vn->sc_secsize;
55e303ae
A
934 break;
935 case DKIOCGETMAXBYTECOUNTREAD:
91447636 936 *o = ioattr.io_maxreadcnt;
55e303ae
A
937 break;
938 case DKIOCGETMAXBYTECOUNTWRITE:
91447636 939 *o = ioattr.io_maxwritecnt;
9bccf70c
A
940 break;
941 case DKIOCGETMAXSEGMENTCOUNTREAD:
91447636 942 *o = ioattr.io_segreadcnt;
9bccf70c
A
943 break;
944 case DKIOCGETMAXSEGMENTCOUNTWRITE:
91447636 945 *o = ioattr.io_segwritecnt;
55e303ae
A
946 break;
947 case DKIOCGETMAXSEGMENTBYTECOUNTREAD:
91447636 948 *o = ioattr.io_maxsegreadsize;
55e303ae
A
949 break;
950 case DKIOCGETMAXSEGMENTBYTECOUNTWRITE:
91447636 951 *o = ioattr.io_maxsegwritesize;
9bccf70c 952 break;
91447636
A
953 case DKIOCGETBLOCKSIZE:
954 *f = vn->sc_secsize;
9bccf70c 955 break;
91447636 956 case DKIOCSETBLOCKSIZE:
9bccf70c
A
957 if (is_char) {
958 /* can only set block size on block device */
91447636
A
959 error = ENODEV;
960 break;
9bccf70c
A
961 }
962 if (*f < DEV_BSIZE) {
91447636
A
963 error = EINVAL;
964 break;
965 }
966 if (vn->sc_shadow_vp != NULL) {
967 if (*f == (unsigned)vn->sc_secsize) {
968 break;
969 }
970 /* can't change the block size if already shadowing */
971 error = EBUSY;
972 break;
9bccf70c
A
973 }
974 vn->sc_secsize = *f;
975 /* recompute the size in terms of the new blocksize */
976 vn->sc_size = vn->sc_fsize / vn->sc_secsize;
977 break;
978 case DKIOCISWRITABLE:
979 *f = 1;
980 break;
55e303ae 981 case DKIOCGETBLOCKCOUNT:
9bccf70c
A
982 *o = vn->sc_size;
983 break;
b0d623f7
A
984#ifdef __LP64__
985 case VNIOCSHADOW32:
986 case VNIOCSHADOW:
987#else
9bccf70c 988 case VNIOCSHADOW:
91447636 989 case VNIOCSHADOW64:
b0d623f7 990#endif
9bccf70c 991 if (vn->sc_shadow_vp != NULL) {
91447636
A
992 error = EBUSY;
993 break;
9bccf70c
A
994 }
995 if (vn->sc_vp == NULL) {
996 /* much be attached before we can shadow */
91447636
A
997 error = EINVAL;
998 break;
999 }
1000 if (!proc_is64bit(p)) {
1001 /* downstream code expects LP64 version of vn_ioctl structure */
b0d623f7 1002 vn_ioctl_to_64((struct vn_ioctl_32 *)viop, &user_vnio);
91447636 1003 viop = &user_vnio;
9bccf70c 1004 }
91447636
A
1005 if (viop->vn_file == USER_ADDR_NULL) {
1006 error = EINVAL;
1007 break;
9bccf70c 1008 }
91447636 1009 error = vniocattach_shadow(vn, viop, dev, 0, p);
9bccf70c
A
1010 break;
1011
b0d623f7
A
1012#ifdef __LP64__
1013 case VNIOCATTACH32:
1014 case VNIOCATTACH:
1015#else
9bccf70c 1016 case VNIOCATTACH:
91447636 1017 case VNIOCATTACH64:
b0d623f7 1018#endif
9bccf70c
A
1019 if (is_char) {
1020 /* attach only on block device */
91447636
A
1021 error = ENODEV;
1022 break;
9bccf70c
A
1023 }
1024 if (vn->sc_flags & VNF_INITED) {
91447636
A
1025 error = EBUSY;
1026 break;
1027 }
1028 if (!proc_is64bit(p)) {
1029 /* downstream code expects LP64 version of vn_ioctl structure */
b0d623f7 1030 vn_ioctl_to_64((struct vn_ioctl_32 *)viop, &user_vnio);
91447636 1031 viop = &user_vnio;
9bccf70c 1032 }
91447636
A
1033 if (viop->vn_file == USER_ADDR_NULL) {
1034 error = EINVAL;
1035 break;
9bccf70c 1036 }
91447636 1037 error = vniocattach_file(vn, viop, dev, 0, p);
9bccf70c
A
1038 break;
1039
b0d623f7
A
1040#ifdef __LP64__
1041 case VNIOCDETACH32:
1042 case VNIOCDETACH:
1043#else
9bccf70c 1044 case VNIOCDETACH:
91447636 1045 case VNIOCDETACH64:
b0d623f7 1046#endif
9bccf70c
A
1047 if (is_char) {
1048 /* detach only on block device */
91447636
A
1049 error = ENODEV;
1050 break;
9bccf70c
A
1051 }
1052 /* Note: spec_open won't open a mounted block device */
1053
1054 /*
1055 * XXX handle i/o in progress. Return EBUSY, or wait, or
1056 * flush the i/o.
1057 * XXX handle multiple opens of the device. Return EBUSY,
1058 * or revoke the fd's.
1059 * How are these problems handled for removable and failing
1060 * hardware devices? (Hint: They are not)
1061 */
2d21ac55 1062 vnclear(vn, &context);
9bccf70c
A
1063 break;
1064
1065 case VNIOCGSET:
1066 vn_options |= *f;
1067 *f = vn_options;
1068 break;
1069
1070 case VNIOCGCLEAR:
1071 vn_options &= ~(*f);
1072 *f = vn_options;
1073 break;
1074
1075 case VNIOCUSET:
1076 vn->sc_options |= *f;
1077 *f = vn->sc_options;
1078 break;
1079
1080 case VNIOCUCLEAR:
1081 vn->sc_options &= ~(*f);
1082 *f = vn->sc_options;
1083 break;
1084
1085 default:
1086 error = ENOTTY;
1087 break;
1088 }
91447636 1089 done:
9bccf70c
A
1090 return(error);
1091}
1092
1093static int
2d21ac55 1094vnioctl_chr(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p)
9bccf70c
A
1095{
1096 return (vnioctl(dev, cmd, data, flag, p, TRUE));
1097}
1098
1099static int
2d21ac55 1100vnioctl_blk(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p)
9bccf70c
A
1101{
1102 return (vnioctl(dev, cmd, data, flag, p, FALSE));
1103}
1104
1105/*
1106 * vniocattach_file:
1107 *
1108 * Attach a file to a VN partition. Return the size in the vn_size
1109 * field.
1110 */
1111
1112static int
1113vniocattach_file(struct vn_softc *vn,
b0d623f7 1114 struct vn_ioctl_64 *vniop,
9bccf70c
A
1115 dev_t dev,
1116 int in_kernel,
2d21ac55 1117 proc_t p)
9bccf70c 1118{
91447636 1119 dev_t cdev;
2d21ac55 1120 vfs_context_t ctx = vfs_context_current();
91447636 1121 kauth_cred_t cred;
9bccf70c 1122 struct nameidata nd;
91447636 1123 off_t file_size;
9bccf70c 1124 int error, flags;
91447636 1125
9bccf70c
A
1126 flags = FREAD|FWRITE;
1127 if (in_kernel) {
6d2010ae 1128 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW, UIO_SYSSPACE, vniop->vn_file, ctx);
9bccf70c
A
1129 }
1130 else {
6d2010ae 1131 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW,
91447636 1132 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
2d21ac55 1133 vniop->vn_file, ctx);
9bccf70c 1134 }
91447636 1135 /* vn_open gives both long- and short-term references */
9bccf70c
A
1136 error = vn_open(&nd, flags, 0);
1137 if (error) {
0c530ab8 1138 if (error != EACCES && error != EPERM && error != EROFS) {
9bccf70c 1139 return (error);
0c530ab8 1140 }
9bccf70c
A
1141 flags &= ~FWRITE;
1142 if (in_kernel) {
6d2010ae 1143 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW, UIO_SYSSPACE,
2d21ac55 1144 vniop->vn_file, ctx);
9bccf70c
A
1145 }
1146 else {
6d2010ae 1147 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW,
91447636 1148 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
2d21ac55 1149 vniop->vn_file, ctx);
9bccf70c
A
1150 }
1151 error = vn_open(&nd, flags, 0);
0c530ab8 1152 if (error) {
9bccf70c 1153 return (error);
0c530ab8 1154 }
9bccf70c
A
1155 }
1156 if (nd.ni_vp->v_type != VREG) {
1157 error = EINVAL;
1158 }
9bccf70c 1159 else {
2d21ac55 1160 error = vnode_size(nd.ni_vp, &file_size, ctx);
9bccf70c
A
1161 }
1162 if (error != 0) {
2d21ac55 1163 (void) vn_close(nd.ni_vp, flags, ctx);
91447636 1164 vnode_put(nd.ni_vp);
9bccf70c
A
1165 return (error);
1166 }
91447636
A
1167 cred = kauth_cred_proc_ref(p);
1168 nd.ni_vp->v_flag |= VNOCACHE_DATA;
2d21ac55 1169 error = setcred(nd.ni_vp, cred);
9bccf70c 1170 if (error) {
2d21ac55 1171 (void)vn_close(nd.ni_vp, flags, ctx);
91447636 1172 vnode_put(nd.ni_vp);
0c530ab8 1173 kauth_cred_unref(&cred);
9bccf70c
A
1174 return(error);
1175 }
91447636
A
1176 vn->sc_secsize = DEV_BSIZE;
1177 vn->sc_fsize = file_size;
1178 vn->sc_size = file_size / vn->sc_secsize;
1179 vn->sc_vp = nd.ni_vp;
1180 vn->sc_vid = vnode_vid(nd.ni_vp);
1181 vn->sc_open_flags = flags;
1182 vn->sc_cred = cred;
1183 cdev = makedev(vndevice_cdev_major, minor(dev));
1184 vn->sc_cdev = devfs_make_node(cdev, DEVFS_CHAR,
1185 UID_ROOT, GID_OPERATOR,
1186 0600, "rvn%d",
1187 minor(dev));
9bccf70c
A
1188 vn->sc_flags |= VNF_INITED;
1189 if (flags == FREAD)
1190 vn->sc_flags |= VNF_READONLY;
91447636
A
1191 /* lose the short-term reference */
1192 vnode_put(nd.ni_vp);
9bccf70c
A
1193 return(0);
1194}
1195
1196static int
b0d623f7
A
1197vniocattach_shadow(struct vn_softc *vn, struct vn_ioctl_64 *vniop,
1198 __unused dev_t dev, int in_kernel, proc_t p)
9bccf70c 1199{
2d21ac55 1200 vfs_context_t ctx = vfs_context_current();
9bccf70c
A
1201 struct nameidata nd;
1202 int error, flags;
1203 shadow_map_t * map;
91447636
A
1204 off_t file_size;
1205
9bccf70c
A
1206 flags = FREAD|FWRITE;
1207 if (in_kernel) {
6d2010ae 1208 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW, UIO_SYSSPACE, vniop->vn_file, ctx);
9bccf70c
A
1209 }
1210 else {
6d2010ae 1211 NDINIT(&nd, LOOKUP, OP_OPEN, FOLLOW,
91447636 1212 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
2d21ac55 1213 vniop->vn_file, ctx);
9bccf70c 1214 }
91447636 1215 /* vn_open gives both long- and short-term references */
9bccf70c
A
1216 error = vn_open(&nd, flags, 0);
1217 if (error) {
1218 /* shadow MUST be writable! */
1219 return (error);
1220 }
91447636 1221 if (nd.ni_vp->v_type != VREG
2d21ac55
A
1222 || (error = vnode_size(nd.ni_vp, &file_size, ctx))) {
1223 (void)vn_close(nd.ni_vp, flags, ctx);
91447636 1224 vnode_put(nd.ni_vp);
9bccf70c
A
1225 return (error ? error : EINVAL);
1226 }
91447636 1227 map = shadow_map_create(vn->sc_fsize, file_size,
9bccf70c
A
1228 0, vn->sc_secsize);
1229 if (map == NULL) {
2d21ac55 1230 (void)vn_close(nd.ni_vp, flags, ctx);
91447636 1231 vnode_put(nd.ni_vp);
9bccf70c
A
1232 vn->sc_shadow_vp = NULL;
1233 return (ENOMEM);
1234 }
91447636
A
1235 vn->sc_shadow_vp = nd.ni_vp;
1236 vn->sc_shadow_vid = vnode_vid(nd.ni_vp);
1237 vn->sc_shadow_vp->v_flag |= VNOCACHE_DATA;
9bccf70c
A
1238 vn->sc_shadow_map = map;
1239 vn->sc_flags &= ~VNF_READONLY; /* we're now read/write */
91447636
A
1240
1241 /* lose the short-term reference */
1242 vnode_put(nd.ni_vp);
9bccf70c
A
1243 return(0);
1244}
1245
1246int
1247vndevice_root_image(char * path, char devname[], dev_t * dev_p)
1248{
1249 int error = 0;
91447636 1250 struct vn_softc * vn;
b0d623f7 1251 struct vn_ioctl_64 vnio;
9bccf70c 1252
91447636
A
1253 vnio.vn_file = CAST_USER_ADDR_T(path);
1254 vnio.vn_size = 0;
9bccf70c
A
1255
1256 vn = vn_table + ROOT_IMAGE_UNIT;
1257 *dev_p = makedev(vndevice_bdev_major,
1258 ROOT_IMAGE_UNIT);
2d21ac55 1259 snprintf(devname, 16, "vn%d", ROOT_IMAGE_UNIT);
91447636 1260 error = vniocattach_file(vn, &vnio, *dev_p, 1, current_proc());
9bccf70c
A
1261 return (error);
1262}
1263
1264/*
1265 * Duplicate the current processes' credentials. Since we are called only
1266 * as the result of a SET ioctl and only root can do that, any future access
1267 * to this "disk" is essentially as root. Note that credentials may change
1268 * if some other uid can write directly to the mapped file (NFS).
1269 */
91447636 1270static int
2d21ac55 1271setcred(struct vnode * vp, kauth_cred_t cred)
9bccf70c
A
1272{
1273 char *tmpbuf;
1274 int error = 0;
91447636 1275 struct vfs_context context;
9bccf70c
A
1276
1277 /*
1278 * Horrible kludge to establish credentials for NFS XXX.
1279 */
2d21ac55 1280 context.vc_thread = current_thread();
91447636
A
1281 context.vc_ucred = cred;
1282 tmpbuf = _MALLOC(DEV_BSIZE, M_TEMP, M_WAITOK);
1283 error = file_io(vp, &context, UIO_READ, tmpbuf, 0, DEV_BSIZE, NULL);
1284 FREE(tmpbuf, M_TEMP);
9bccf70c
A
1285 return (error);
1286}
1287
1288void
2d21ac55 1289vnclear(struct vn_softc *vn, vfs_context_t ctx)
9bccf70c 1290{
9bccf70c 1291 if (vn->sc_vp != NULL) {
91447636 1292 /* release long-term reference */
2d21ac55 1293 (void)vn_close(vn->sc_vp, vn->sc_open_flags, ctx);
9bccf70c
A
1294 vn->sc_vp = NULL;
1295 }
1296 if (vn->sc_shadow_vp != NULL) {
91447636 1297 /* release long-term reference */
2d21ac55 1298 (void)vn_close(vn->sc_shadow_vp, FREAD | FWRITE, ctx);
9bccf70c
A
1299 vn->sc_shadow_vp = NULL;
1300 }
1301 if (vn->sc_shadow_map != NULL) {
1302 shadow_map_free(vn->sc_shadow_map);
1303 vn->sc_shadow_map = NULL;
1304 }
91447636 1305 vn->sc_flags &= ~(VNF_INITED | VNF_READONLY);
9bccf70c 1306 if (vn->sc_cred) {
0c530ab8 1307 kauth_cred_unref(&vn->sc_cred);
9bccf70c
A
1308 }
1309 vn->sc_size = 0;
1310 vn->sc_fsize = 0;
1311 if (vn->sc_cdev) {
1312 devfs_remove(vn->sc_cdev);
1313 vn->sc_cdev = NULL;
1314 }
1315}
1316
1317static int
1318vnsize(dev_t dev)
1319{
91447636 1320 int secsize;
9bccf70c
A
1321 struct vn_softc *vn;
1322 int unit;
1323
1324 unit = vnunit(dev);
1325 if (vnunit(dev) >= NVNDEVICE) {
91447636 1326 return (-1);
9bccf70c 1327 }
9bccf70c 1328
91447636 1329 vn = vn_table + unit;
9bccf70c 1330 if ((vn->sc_flags & VNF_INITED) == 0)
91447636
A
1331 secsize = -1;
1332 else
1333 secsize = vn->sc_secsize;
fe8ab488 1334
91447636 1335 return (secsize);
9bccf70c
A
1336}
1337
1338#define CDEV_MAJOR -1
1339#define BDEV_MAJOR -1
1340static int vndevice_inited = 0;
1341
1342void
91447636 1343vndevice_init(void)
9bccf70c 1344{
9bccf70c
A
1345 if (vndevice_inited)
1346 return;
2d21ac55
A
1347
1348 vndevice_do_init();
1349}
1350
1351static void
1352vndevice_do_init( void )
1353{
1354 int i;
1355
9bccf70c
A
1356 vndevice_bdev_major = bdevsw_add(BDEV_MAJOR, &vn_bdevsw);
1357
1358 if (vndevice_bdev_major < 0) {
1359 printf("vndevice_init: bdevsw_add() returned %d\n",
1360 vndevice_bdev_major);
1361 return;
1362 }
1363 vndevice_cdev_major = cdevsw_add_with_bdev(CDEV_MAJOR, &vn_cdevsw,
1364 vndevice_bdev_major);
1365 if (vndevice_cdev_major < 0) {
1366 printf("vndevice_init: cdevsw_add() returned %d\n",
1367 vndevice_cdev_major);
1368 return;
1369 }
1370 for (i = 0; i < NVNDEVICE; i++) {
1371 dev_t dev = makedev(vndevice_bdev_major, i);
1372 vn_table[i].sc_bdev = devfs_make_node(dev, DEVFS_BLOCK,
1373 UID_ROOT, GID_OPERATOR,
1374 0600, "vn%d",
1375 i);
1376 if (vn_table[i].sc_bdev == NULL)
1377 printf("vninit: devfs_make_node failed!\n");
1378 }
1379}
91447636
A
1380
1381static void
b0d623f7 1382vn_ioctl_to_64(struct vn_ioctl_32 *from, struct vn_ioctl_64 *to)
91447636
A
1383{
1384 to->vn_file = CAST_USER_ADDR_T(from->vn_file);
1385 to->vn_size = from->vn_size;
1386 to->vn_control = from->vn_control;
1387}
1388
55e303ae 1389#endif /* NVNDEVICE */