1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * lofi (loopback file) driver - allows you to attach a file to a device, 28 * which can then be accessed through that device. The simple model is that 29 * you tell lofi to open a file, and then use the block device you get as 30 * you would any block device. lofi translates access to the block device 31 * into I/O on the underlying file. This is mostly useful for 32 * mounting images of filesystems. 33 * 34 * lofi is controlled through /dev/lofictl - this is the only device exported 35 * during attach, and is minor number 0. lofiadm communicates with lofi through 36 * ioctls on this device. When a file is attached to lofi, block and character 37 * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices 38 * are identified by their minor number, and the minor number is also used 39 * as the name in /dev/lofi. If we ever decide to support virtual disks, 40 * we'll have to divide the minor number space to identify fdisk partitions 41 * and slices, and the name will then be the minor number shifted down a 42 * few bits. Minor devices are tracked with state structures handled with 43 * ddi_soft_state(9F) for simplicity. 44 * 45 * A file attached to lofi is opened when attached and not closed until 46 * explicitly detached from lofi. This seems more sensible than deferring 47 * the open until the /dev/lofi device is opened, for a number of reasons. 48 * One is that any failure is likely to be noticed by the person (or script) 49 * running lofiadm. Another is that it would be a security problem if the 50 * file was replaced by another one after being added but before being opened. 51 * 52 * The only hard part about lofi is the ioctls. In order to support things 53 * like 'newfs' on a lofi device, it needs to support certain disk ioctls. 54 * So it has to fake disk geometry and partition information. More may need 55 * to be faked if your favorite utility doesn't work and you think it should 56 * (fdformat doesn't work because it really wants to know the type of floppy 57 * controller to talk to, and that didn't seem easy to fake. Or possibly even 58 * necessary, since we have mkfs_pcfs now). 59 * 60 * Normally, a lofi device cannot be detached if it is open (i.e. busy). To 61 * support simulation of hotplug events, an optional force flag is provided. 62 * If a lofi device is open when a force detach is requested, then the 63 * underlying file is closed and any subsequent operations return EIO. When the 64 * device is closed for the last time, it will be cleaned up at that time. In 65 * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is 66 * detached but not removed. 67 * 68 * Known problems: 69 * 70 * UFS logging. Mounting a UFS filesystem image "logging" 71 * works for basic copy testing but wedges during a build of ON through 72 * that image. Some deadlock in lufs holding the log mutex and then 73 * getting stuck on a buf. So for now, don't do that. 74 * 75 * Direct I/O. Since the filesystem data is being cached in the buffer 76 * cache, _and_ again in the underlying filesystem, it's tempting to 77 * enable direct I/O on the underlying file. Don't, because that deadlocks. 78 * I think to fix the cache-twice problem we might need filesystem support. 79 * 80 * lofi on itself. The simple lock strategy (lofi_lock) precludes this 81 * because you'll be in lofi_ioctl, holding the lock when you open the 82 * file, which, if it's lofi, will grab lofi_lock. We prevent this for 83 * now, though not using ddi_soft_state(9F) would make it possible to 84 * do. Though it would still be silly. 85 * 86 * Interesting things to do: 87 * 88 * Allow multiple files for each device. A poor-man's metadisk, basically. 89 * 90 * Pass-through ioctls on block devices. You can (though it's not 91 * documented), give lofi a block device as a file name. Then we shouldn't 92 * need to fake a geometry, however, it may be relevant if you're replacing 93 * metadisk, or using lofi to get crypto. 94 * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1 95 * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home. 96 * In fact this even makes sense if you have lofi "above" metadisk. 97 * 98 * Encryption: 99 * Each lofi device can have its own symmetric key and cipher. 100 * They are passed to us by lofiadm(1m) in the correct format for use 101 * with the misc/kcf crypto_* routines. 102 * 103 * Each block has its own IV, that is calculated in lofi_blk_mech(), based 104 * on the "master" key held in the lsp and the block number of the buffer. 105 */ 106 107 #include <sys/types.h> 108 #include <netinet/in.h> 109 #include <sys/sysmacros.h> 110 #include <sys/uio.h> 111 #include <sys/kmem.h> 112 #include <sys/cred.h> 113 #include <sys/mman.h> 114 #include <sys/errno.h> 115 #include <sys/aio_req.h> 116 #include <sys/stat.h> 117 #include <sys/file.h> 118 #include <sys/modctl.h> 119 #include <sys/conf.h> 120 #include <sys/debug.h> 121 #include <sys/vnode.h> 122 #include <sys/lofi.h> 123 #include <sys/fcntl.h> 124 #include <sys/pathname.h> 125 #include <sys/filio.h> 126 #include <sys/fdio.h> 127 #include <sys/open.h> 128 #include <sys/disp.h> 129 #include <vm/seg_map.h> 130 #include <sys/ddi.h> 131 #include <sys/sunddi.h> 132 #include <sys/zmod.h> 133 #include <sys/crypto/common.h> 134 #include <sys/crypto/api.h> 135 136 /* 137 * The basis for CRYOFF is derived from usr/src/uts/common/sys/fs/ufs_fs.h. 138 * Crypto metadata, if it exists, is located at the end of the boot block 139 * (BBOFF + BBSIZE, which is SBOFF). The super block and everything after 140 * is offset by the size of the crypto metadata which is handled by 141 * lsp->ls_crypto_offset. 142 */ 143 #define CRYOFF ((off_t)8192) 144 145 #define NBLOCKS_PROP_NAME "Nblocks" 146 #define SIZE_PROP_NAME "Size" 147 148 #define SETUP_C_DATA(cd, buf, len) \ 149 (cd).cd_format = CRYPTO_DATA_RAW; \ 150 (cd).cd_offset = 0; \ 151 (cd).cd_miscdata = NULL; \ 152 (cd).cd_length = (len); \ 153 (cd).cd_raw.iov_base = (buf); \ 154 (cd).cd_raw.iov_len = (len); 155 156 #define UIO_CHECK(uio) \ 157 if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \ 158 ((uio)->uio_resid % DEV_BSIZE) != 0) { \ 159 return (EINVAL); \ 160 } 161 162 static dev_info_t *lofi_dip = NULL; 163 static void *lofi_statep = NULL; 164 static kmutex_t lofi_lock; /* state lock */ 165 166 /* 167 * Because lofi_taskq_nthreads limits the actual swamping of the device, the 168 * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively 169 * high. If we want to be assured that the underlying device is always busy, 170 * we must be sure that the number of bytes enqueued when the number of 171 * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for 172 * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should 173 * set maxalloc to be the maximum throughput (in bytes per second) of the 174 * underlying device divided by the minimum I/O size. We assume a realistic 175 * maximum throughput of one hundred megabytes per second; we set maxalloc on 176 * the lofi task queue to be 104857600 divided by DEV_BSIZE. 177 */ 178 static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE; 179 static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */ 180 181 uint32_t lofi_max_files = LOFI_MAX_FILES; 182 const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC; 183 184 static int gzip_decompress(void *src, size_t srclen, void *dst, 185 size_t *destlen, int level); 186 187 lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = { 188 {gzip_decompress, NULL, 6, "gzip"}, /* default */ 189 {gzip_decompress, NULL, 6, "gzip-6"}, 190 {gzip_decompress, NULL, 9, "gzip-9"} 191 }; 192 193 static int 194 lofi_busy(void) 195 { 196 minor_t minor; 197 198 /* 199 * We need to make sure no mappings exist - mod_remove won't 200 * help because the device isn't open. 201 */ 202 mutex_enter(&lofi_lock); 203 for (minor = 1; minor <= lofi_max_files; minor++) { 204 if (ddi_get_soft_state(lofi_statep, minor) != NULL) { 205 mutex_exit(&lofi_lock); 206 return (EBUSY); 207 } 208 } 209 mutex_exit(&lofi_lock); 210 return (0); 211 } 212 213 static int 214 is_opened(struct lofi_state *lsp) 215 { 216 ASSERT(mutex_owned(&lofi_lock)); 217 return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count); 218 } 219 220 static int 221 mark_opened(struct lofi_state *lsp, int otyp) 222 { 223 ASSERT(mutex_owned(&lofi_lock)); 224 switch (otyp) { 225 case OTYP_CHR: 226 lsp->ls_chr_open = 1; 227 break; 228 case OTYP_BLK: 229 lsp->ls_blk_open = 1; 230 break; 231 case OTYP_LYR: 232 lsp->ls_lyr_open_count++; 233 break; 234 default: 235 return (-1); 236 } 237 return (0); 238 } 239 240 static void 241 mark_closed(struct lofi_state *lsp, int otyp) 242 { 243 ASSERT(mutex_owned(&lofi_lock)); 244 switch (otyp) { 245 case OTYP_CHR: 246 lsp->ls_chr_open = 0; 247 break; 248 case OTYP_BLK: 249 lsp->ls_blk_open = 0; 250 break; 251 case OTYP_LYR: 252 lsp->ls_lyr_open_count--; 253 break; 254 default: 255 break; 256 } 257 } 258 259 static void 260 lofi_free_crypto(struct lofi_state *lsp) 261 { 262 ASSERT(mutex_owned(&lofi_lock)); 263 264 if (lsp->ls_crypto_enabled) { 265 /* 266 * Clean up the crypto state so that it doesn't hang around 267 * in memory after we are done with it. 268 */ 269 bzero(lsp->ls_key.ck_data, 270 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 271 kmem_free(lsp->ls_key.ck_data, 272 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 273 lsp->ls_key.ck_data = NULL; 274 lsp->ls_key.ck_length = 0; 275 276 if (lsp->ls_mech.cm_param != NULL) { 277 kmem_free(lsp->ls_mech.cm_param, 278 lsp->ls_mech.cm_param_len); 279 lsp->ls_mech.cm_param = NULL; 280 lsp->ls_mech.cm_param_len = 0; 281 } 282 283 if (lsp->ls_iv_mech.cm_param != NULL) { 284 kmem_free(lsp->ls_iv_mech.cm_param, 285 lsp->ls_iv_mech.cm_param_len); 286 lsp->ls_iv_mech.cm_param = NULL; 287 lsp->ls_iv_mech.cm_param_len = 0; 288 } 289 290 mutex_destroy(&lsp->ls_crypto_lock); 291 } 292 } 293 294 static void 295 lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp, 296 cred_t *credp) 297 { 298 dev_t newdev; 299 char namebuf[50]; 300 301 ASSERT(mutex_owned(&lofi_lock)); 302 303 lofi_free_crypto(lsp); 304 305 if (lsp->ls_vp) { 306 (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 307 1, 0, credp, NULL); 308 VN_RELE(lsp->ls_vp); 309 lsp->ls_vp = NULL; 310 } 311 312 newdev = makedevice(getmajor(dev), minor); 313 (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 314 (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 315 316 (void) snprintf(namebuf, sizeof (namebuf), "%d", minor); 317 ddi_remove_minor_node(lofi_dip, namebuf); 318 (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor); 319 ddi_remove_minor_node(lofi_dip, namebuf); 320 321 kmem_free(lsp->ls_filename, lsp->ls_filename_sz); 322 taskq_destroy(lsp->ls_taskq); 323 if (lsp->ls_kstat) { 324 kstat_delete(lsp->ls_kstat); 325 mutex_destroy(&lsp->ls_kstat_lock); 326 } 327 328 if (lsp->ls_uncomp_seg_sz > 0) { 329 kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz); 330 lsp->ls_uncomp_seg_sz = 0; 331 } 332 ddi_soft_state_free(lofi_statep, minor); 333 } 334 335 /*ARGSUSED*/ 336 static int 337 lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp) 338 { 339 minor_t minor; 340 struct lofi_state *lsp; 341 342 mutex_enter(&lofi_lock); 343 minor = getminor(*devp); 344 if (minor == 0) { 345 /* master control device */ 346 /* must be opened exclusively */ 347 if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) { 348 mutex_exit(&lofi_lock); 349 return (EINVAL); 350 } 351 lsp = ddi_get_soft_state(lofi_statep, 0); 352 if (lsp == NULL) { 353 mutex_exit(&lofi_lock); 354 return (ENXIO); 355 } 356 if (is_opened(lsp)) { 357 mutex_exit(&lofi_lock); 358 return (EBUSY); 359 } 360 (void) mark_opened(lsp, OTYP_CHR); 361 mutex_exit(&lofi_lock); 362 return (0); 363 } 364 365 /* otherwise, the mapping should already exist */ 366 lsp = ddi_get_soft_state(lofi_statep, minor); 367 if (lsp == NULL) { 368 mutex_exit(&lofi_lock); 369 return (EINVAL); 370 } 371 372 if (lsp->ls_vp == NULL) { 373 mutex_exit(&lofi_lock); 374 return (ENXIO); 375 } 376 377 if (mark_opened(lsp, otyp) == -1) { 378 mutex_exit(&lofi_lock); 379 return (EINVAL); 380 } 381 382 mutex_exit(&lofi_lock); 383 return (0); 384 } 385 386 /*ARGSUSED*/ 387 static int 388 lofi_close(dev_t dev, int flag, int otyp, struct cred *credp) 389 { 390 minor_t minor; 391 struct lofi_state *lsp; 392 393 mutex_enter(&lofi_lock); 394 minor = getminor(dev); 395 lsp = ddi_get_soft_state(lofi_statep, minor); 396 if (lsp == NULL) { 397 mutex_exit(&lofi_lock); 398 return (EINVAL); 399 } 400 mark_closed(lsp, otyp); 401 402 /* 403 * If we forcibly closed the underlying device (li_force), or 404 * asked for cleanup (li_cleanup), finish up if we're the last 405 * out of the door. 406 */ 407 if (minor != 0 && !is_opened(lsp) && 408 (lsp->ls_cleanup || lsp->ls_vp == NULL)) 409 lofi_free_handle(dev, minor, lsp, credp); 410 411 mutex_exit(&lofi_lock); 412 return (0); 413 } 414 415 /* 416 * Sets the mechanism's initialization vector (IV) if one is needed. 417 * The IV is computed from the data block number. lsp->ls_mech is 418 * altered so that: 419 * lsp->ls_mech.cm_param_len is set to the IV len. 420 * lsp->ls_mech.cm_param is set to the IV. 421 */ 422 static int 423 lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno) 424 { 425 int ret; 426 crypto_data_t cdata; 427 char *iv; 428 size_t iv_len; 429 size_t min; 430 void *data; 431 size_t datasz; 432 433 ASSERT(mutex_owned(&lsp->ls_crypto_lock)); 434 435 if (lsp == NULL) 436 return (CRYPTO_DEVICE_ERROR); 437 438 /* lsp->ls_mech.cm_param{_len} has already been set for static iv */ 439 if (lsp->ls_iv_type == IVM_NONE) { 440 return (CRYPTO_SUCCESS); 441 } 442 443 /* 444 * if kmem already alloced from previous call and it's the same size 445 * we need now, just recycle it; allocate new kmem only if we have to 446 */ 447 if (lsp->ls_mech.cm_param == NULL || 448 lsp->ls_mech.cm_param_len != lsp->ls_iv_len) { 449 iv_len = lsp->ls_iv_len; 450 iv = kmem_zalloc(iv_len, KM_SLEEP); 451 } else { 452 iv_len = lsp->ls_mech.cm_param_len; 453 iv = lsp->ls_mech.cm_param; 454 bzero(iv, iv_len); 455 } 456 457 switch (lsp->ls_iv_type) { 458 case IVM_ENC_BLKNO: 459 /* iv is not static, lblkno changes each time */ 460 data = &lblkno; 461 datasz = sizeof (lblkno); 462 break; 463 default: 464 data = 0; 465 datasz = 0; 466 break; 467 } 468 469 /* 470 * write blkno into the iv buffer padded on the left in case 471 * blkno ever grows bigger than its current longlong_t size 472 * or a variation other than blkno is used for the iv data 473 */ 474 min = MIN(datasz, iv_len); 475 bcopy(data, iv + (iv_len - min), min); 476 477 /* encrypt the data in-place to get the IV */ 478 SETUP_C_DATA(cdata, iv, iv_len); 479 480 ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key, 481 NULL, NULL, NULL); 482 if (ret != CRYPTO_SUCCESS) { 483 cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)", 484 lblkno, ret); 485 if (lsp->ls_mech.cm_param != iv) 486 kmem_free(iv, iv_len); 487 return (ret); 488 } 489 490 /* clean up the iv from the last computation */ 491 if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv) 492 kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len); 493 lsp->ls_mech.cm_param_len = iv_len; 494 lsp->ls_mech.cm_param = iv; 495 496 return (CRYPTO_SUCCESS); 497 } 498 499 /* 500 * Performs encryption and decryption of a chunk of data of size "len", 501 * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of 502 * DEV_BSIZE. 503 */ 504 static int 505 lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext, 506 caddr_t ciphertext, size_t len, boolean_t op_encrypt) 507 { 508 crypto_data_t cdata; 509 crypto_data_t wdata; 510 int ret; 511 longlong_t lblkno = bp->b_lblkno; 512 513 mutex_enter(&lsp->ls_crypto_lock); 514 515 /* 516 * though we could encrypt/decrypt entire "len" chunk of data, we need 517 * to break it into DEV_BSIZE pieces to capture blkno incrementing 518 */ 519 SETUP_C_DATA(cdata, plaintext, len); 520 cdata.cd_length = DEV_BSIZE; 521 if (ciphertext != NULL) { /* not in-place crypto */ 522 SETUP_C_DATA(wdata, ciphertext, len); 523 wdata.cd_length = DEV_BSIZE; 524 } 525 526 do { 527 ret = lofi_blk_mech(lsp, lblkno); 528 if (ret != CRYPTO_SUCCESS) 529 continue; 530 531 if (op_encrypt) { 532 ret = crypto_encrypt(&lsp->ls_mech, &cdata, 533 &lsp->ls_key, NULL, 534 ((ciphertext != NULL) ? &wdata : NULL), NULL); 535 } else { 536 ret = crypto_decrypt(&lsp->ls_mech, &cdata, 537 &lsp->ls_key, NULL, 538 ((ciphertext != NULL) ? &wdata : NULL), NULL); 539 } 540 541 cdata.cd_offset += DEV_BSIZE; 542 if (ciphertext != NULL) 543 wdata.cd_offset += DEV_BSIZE; 544 lblkno++; 545 } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len); 546 547 mutex_exit(&lsp->ls_crypto_lock); 548 549 if (ret != CRYPTO_SUCCESS) { 550 cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)", 551 op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()", 552 lblkno, ret); 553 } 554 555 return (ret); 556 } 557 558 #define RDWR_RAW 1 559 #define RDWR_BCOPY 2 560 561 static int 562 lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 563 struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn) 564 { 565 ssize_t resid; 566 int isread; 567 int error; 568 569 /* 570 * Handles reads/writes for both plain and encrypted lofi 571 * Note: offset is already shifted by lsp->ls_crypto_offset 572 * when it gets here. 573 */ 574 575 isread = bp->b_flags & B_READ; 576 if (isread) { 577 if (method == RDWR_BCOPY) { 578 /* DO NOT update bp->b_resid for bcopy */ 579 bcopy(bcopy_locn, bufaddr, len); 580 error = 0; 581 } else { /* RDWR_RAW */ 582 error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len, 583 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 584 &resid); 585 bp->b_resid = resid; 586 } 587 if (lsp->ls_crypto_enabled && error == 0) { 588 if (lofi_crypto(lsp, bp, bufaddr, NULL, len, 589 B_FALSE) != CRYPTO_SUCCESS) { 590 /* 591 * XXX: original code didn't set residual 592 * back to len because no error was expected 593 * from bcopy() if encryption is not enabled 594 */ 595 if (method != RDWR_BCOPY) 596 bp->b_resid = len; 597 error = EIO; 598 } 599 } 600 return (error); 601 } else { 602 void *iobuf = bufaddr; 603 604 if (lsp->ls_crypto_enabled) { 605 /* don't do in-place crypto to keep bufaddr intact */ 606 iobuf = kmem_alloc(len, KM_SLEEP); 607 if (lofi_crypto(lsp, bp, bufaddr, iobuf, len, 608 B_TRUE) != CRYPTO_SUCCESS) { 609 kmem_free(iobuf, len); 610 if (method != RDWR_BCOPY) 611 bp->b_resid = len; 612 return (EIO); 613 } 614 } 615 if (method == RDWR_BCOPY) { 616 /* DO NOT update bp->b_resid for bcopy */ 617 bcopy(iobuf, bcopy_locn, len); 618 error = 0; 619 } else { /* RDWR_RAW */ 620 error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len, 621 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, 622 &resid); 623 bp->b_resid = resid; 624 } 625 if (lsp->ls_crypto_enabled) { 626 kmem_free(iobuf, len); 627 } 628 return (error); 629 } 630 } 631 632 static int 633 lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp, 634 struct lofi_state *lsp) 635 { 636 int error; 637 offset_t alignedoffset, mapoffset; 638 size_t xfersize; 639 int isread; 640 int smflags; 641 caddr_t mapaddr; 642 size_t len; 643 enum seg_rw srw; 644 int save_error; 645 646 /* 647 * Note: offset is already shifted by lsp->ls_crypto_offset 648 * when it gets here. 649 */ 650 if (lsp->ls_crypto_enabled) 651 ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size); 652 653 /* 654 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on 655 * an 8K boundary, but the buf transfer address may not be 656 * aligned on more than a 512-byte boundary (we don't enforce 657 * that even though we could). This matters since the initial 658 * part of the transfer may not start at offset 0 within the 659 * segmap'd chunk. So we have to compensate for that with 660 * 'mapoffset'. Subsequent chunks always start off at the 661 * beginning, and the last is capped by b_resid 662 * 663 * Visually, where "|" represents page map boundaries: 664 * alignedoffset (mapaddr begins at this segmap boundary) 665 * | offset (from beginning of file) 666 * | | len 667 * v v v 668 * ===|====X========|====...======|========X====|==== 669 * /-------------...---------------/ 670 * ^ bp->b_bcount/bp->b_resid at start 671 * /----/--------/----...------/--------/ 672 * ^ ^ ^ ^ ^ 673 * | | | | nth xfersize (<= MAXBSIZE) 674 * | | 2nd thru n-1st xfersize (= MAXBSIZE) 675 * | 1st xfersize (<= MAXBSIZE) 676 * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter) 677 * 678 * Notes: "alignedoffset" is "offset" rounded down to nearest 679 * MAXBSIZE boundary. "len" is next page boundary of size 680 * PAGESIZE after "alignedoffset". 681 */ 682 mapoffset = offset & MAXBOFFSET; 683 alignedoffset = offset - mapoffset; 684 bp->b_resid = bp->b_bcount; 685 isread = bp->b_flags & B_READ; 686 srw = isread ? S_READ : S_WRITE; 687 do { 688 xfersize = MIN(lsp->ls_vp_comp_size - offset, 689 MIN(MAXBSIZE - mapoffset, bp->b_resid)); 690 len = roundup(mapoffset + xfersize, PAGESIZE); 691 mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp, 692 alignedoffset, MAXBSIZE, 1, srw); 693 /* 694 * Now fault in the pages. This lets us check 695 * for errors before we reference mapaddr and 696 * try to resolve the fault in bcopy (which would 697 * panic instead). And this can easily happen, 698 * particularly if you've lofi'd a file over NFS 699 * and someone deletes the file on the server. 700 */ 701 error = segmap_fault(kas.a_hat, segkmap, mapaddr, 702 len, F_SOFTLOCK, srw); 703 if (error) { 704 (void) segmap_release(segkmap, mapaddr, 0); 705 if (FC_CODE(error) == FC_OBJERR) 706 error = FC_ERRNO(error); 707 else 708 error = EIO; 709 break; 710 } 711 /* error may be non-zero for encrypted lofi */ 712 error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize, 713 RDWR_BCOPY, mapaddr + mapoffset); 714 if (error == 0) { 715 bp->b_resid -= xfersize; 716 bufaddr += xfersize; 717 offset += xfersize; 718 } 719 smflags = 0; 720 if (isread) { 721 smflags |= SM_FREE; 722 /* 723 * If we're reading an entire page starting 724 * at a page boundary, there's a good chance 725 * we won't need it again. Put it on the 726 * head of the freelist. 727 */ 728 if (mapoffset == 0 && xfersize == MAXBSIZE) 729 smflags |= SM_DONTNEED; 730 } else { 731 if (error == 0) /* write back good pages */ 732 smflags |= SM_WRITE; 733 } 734 (void) segmap_fault(kas.a_hat, segkmap, mapaddr, 735 len, F_SOFTUNLOCK, srw); 736 save_error = segmap_release(segkmap, mapaddr, smflags); 737 if (error == 0) 738 error = save_error; 739 /* only the first map may start partial */ 740 mapoffset = 0; 741 alignedoffset += MAXBSIZE; 742 } while ((error == 0) && (bp->b_resid > 0) && 743 (offset < lsp->ls_vp_comp_size)); 744 745 return (error); 746 } 747 748 /*ARGSUSED*/ 749 static int gzip_decompress(void *src, size_t srclen, void *dst, 750 size_t *dstlen, int level) 751 { 752 ASSERT(*dstlen >= srclen); 753 754 if (z_uncompress(dst, dstlen, src, srclen) != Z_OK) 755 return (-1); 756 return (0); 757 } 758 759 /* 760 * This is basically what strategy used to be before we found we 761 * needed task queues. 762 */ 763 static void 764 lofi_strategy_task(void *arg) 765 { 766 struct buf *bp = (struct buf *)arg; 767 int error; 768 struct lofi_state *lsp; 769 offset_t offset; 770 caddr_t bufaddr; 771 size_t len; 772 size_t xfersize; 773 boolean_t bufinited = B_FALSE; 774 775 lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 776 if (lsp == NULL) { 777 error = ENXIO; 778 goto errout; 779 } 780 if (lsp->ls_kstat) { 781 mutex_enter(lsp->ls_kstat->ks_lock); 782 kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat)); 783 mutex_exit(lsp->ls_kstat->ks_lock); 784 } 785 bp_mapin(bp); 786 bufaddr = bp->b_un.b_addr; 787 offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 788 if (lsp->ls_crypto_enabled) { 789 /* encrypted data really begins after crypto header */ 790 offset += lsp->ls_crypto_offset; 791 } 792 len = bp->b_bcount; 793 bufinited = B_TRUE; 794 795 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 796 error = EIO; 797 goto errout; 798 } 799 800 /* 801 * We used to always use vn_rdwr here, but we cannot do that because 802 * we might decide to read or write from the the underlying 803 * file during this call, which would be a deadlock because 804 * we have the rw_lock. So instead we page, unless it's not 805 * mapable or it's a character device or it's an encrypted lofi. 806 */ 807 if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) || 808 lsp->ls_crypto_enabled) { 809 error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW, 810 NULL); 811 } else if (lsp->ls_uncomp_seg_sz == 0) { 812 error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp); 813 } else { 814 unsigned char *compressed_seg = NULL, *cmpbuf; 815 unsigned char *uncompressed_seg = NULL; 816 lofi_compress_info_t *li; 817 size_t oblkcount; 818 unsigned long seglen; 819 uint64_t sblkno, eblkno, cmpbytes; 820 offset_t sblkoff, eblkoff; 821 u_offset_t salign, ealign; 822 u_offset_t sdiff; 823 uint32_t comp_data_sz; 824 uint64_t i; 825 826 /* 827 * From here on we're dealing primarily with compressed files 828 */ 829 ASSERT(!lsp->ls_crypto_enabled); 830 831 /* 832 * Compressed files can only be read from and 833 * not written to 834 */ 835 if (!(bp->b_flags & B_READ)) { 836 bp->b_resid = bp->b_bcount; 837 error = EROFS; 838 goto done; 839 } 840 841 ASSERT(lsp->ls_comp_algorithm_index >= 0); 842 li = &lofi_compress_table[lsp->ls_comp_algorithm_index]; 843 /* 844 * Compute starting and ending compressed segment numbers 845 * We use only bitwise operations avoiding division and 846 * modulus because we enforce the compression segment size 847 * to a power of 2 848 */ 849 sblkno = offset >> lsp->ls_comp_seg_shift; 850 sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1); 851 eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift; 852 eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1); 853 854 /* 855 * Align start offset to block boundary for segmap 856 */ 857 salign = lsp->ls_comp_seg_index[sblkno]; 858 sdiff = salign & (DEV_BSIZE - 1); 859 salign -= sdiff; 860 if (eblkno >= (lsp->ls_comp_index_sz - 1)) { 861 /* 862 * We're dealing with the last segment of 863 * the compressed file -- the size of this 864 * segment *may not* be the same as the 865 * segment size for the file 866 */ 867 eblkoff = (offset + bp->b_bcount) & 868 (lsp->ls_uncomp_last_seg_sz - 1); 869 ealign = lsp->ls_vp_comp_size; 870 } else { 871 ealign = lsp->ls_comp_seg_index[eblkno + 1]; 872 } 873 874 /* 875 * Preserve original request paramaters 876 */ 877 oblkcount = bp->b_bcount; 878 879 /* 880 * Assign the calculated parameters 881 */ 882 comp_data_sz = ealign - salign; 883 bp->b_bcount = comp_data_sz; 884 885 /* 886 * Allocate fixed size memory blocks to hold compressed 887 * segments and one uncompressed segment since we 888 * uncompress segments one at a time 889 */ 890 compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP); 891 uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP); 892 /* 893 * Map in the calculated number of blocks 894 */ 895 error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign, 896 bp, lsp); 897 898 bp->b_bcount = oblkcount; 899 bp->b_resid = oblkcount; 900 if (error != 0) 901 goto done; 902 903 /* 904 * We have the compressed blocks, now uncompress them 905 */ 906 cmpbuf = compressed_seg + sdiff; 907 for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz; 908 i++) { 909 /* 910 * Each of the segment index entries contains 911 * the starting block number for that segment. 912 * The number of compressed bytes in a segment 913 * is thus the difference between the starting 914 * block number of this segment and the starting 915 * block number of the next segment. 916 */ 917 if ((i == eblkno) && 918 (i == lsp->ls_comp_index_sz - 1)) { 919 cmpbytes = lsp->ls_vp_comp_size - 920 lsp->ls_comp_seg_index[i]; 921 } else { 922 cmpbytes = lsp->ls_comp_seg_index[i + 1] - 923 lsp->ls_comp_seg_index[i]; 924 } 925 926 /* 927 * The first byte in a compressed segment is a flag 928 * that indicates whether this segment is compressed 929 * at all 930 */ 931 if (*cmpbuf == UNCOMPRESSED) { 932 bcopy((cmpbuf + SEGHDR), uncompressed_seg, 933 (cmpbytes - SEGHDR)); 934 } else { 935 seglen = lsp->ls_uncomp_seg_sz; 936 937 if (li->l_decompress((cmpbuf + SEGHDR), 938 (cmpbytes - SEGHDR), uncompressed_seg, 939 &seglen, li->l_level) != 0) { 940 error = EIO; 941 goto done; 942 } 943 } 944 945 /* 946 * Determine how much uncompressed data we 947 * have to copy and copy it 948 */ 949 xfersize = lsp->ls_uncomp_seg_sz - sblkoff; 950 if (i == eblkno) { 951 if (i == (lsp->ls_comp_index_sz - 1)) 952 xfersize -= (lsp->ls_uncomp_last_seg_sz 953 - eblkoff); 954 else 955 xfersize -= 956 (lsp->ls_uncomp_seg_sz - eblkoff); 957 } 958 959 bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize); 960 961 cmpbuf += cmpbytes; 962 bufaddr += xfersize; 963 bp->b_resid -= xfersize; 964 sblkoff = 0; 965 966 if (bp->b_resid == 0) 967 break; 968 } 969 done: 970 if (compressed_seg != NULL) 971 kmem_free(compressed_seg, comp_data_sz); 972 if (uncompressed_seg != NULL) 973 kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz); 974 } /* end of handling compressed files */ 975 976 errout: 977 if (bufinited && lsp->ls_kstat) { 978 size_t n_done = bp->b_bcount - bp->b_resid; 979 kstat_io_t *kioptr; 980 981 mutex_enter(lsp->ls_kstat->ks_lock); 982 kioptr = KSTAT_IO_PTR(lsp->ls_kstat); 983 if (bp->b_flags & B_READ) { 984 kioptr->nread += n_done; 985 kioptr->reads++; 986 } else { 987 kioptr->nwritten += n_done; 988 kioptr->writes++; 989 } 990 kstat_runq_exit(kioptr); 991 mutex_exit(lsp->ls_kstat->ks_lock); 992 } 993 994 mutex_enter(&lsp->ls_vp_lock); 995 if (--lsp->ls_vp_iocount == 0) 996 cv_broadcast(&lsp->ls_vp_cv); 997 mutex_exit(&lsp->ls_vp_lock); 998 999 bioerror(bp, error); 1000 biodone(bp); 1001 } 1002 1003 static int 1004 lofi_strategy(struct buf *bp) 1005 { 1006 struct lofi_state *lsp; 1007 offset_t offset; 1008 1009 /* 1010 * We cannot just do I/O here, because the current thread 1011 * _might_ end up back in here because the underlying filesystem 1012 * wants a buffer, which eventually gets into bio_recycle and 1013 * might call into lofi to write out a delayed-write buffer. 1014 * This is bad if the filesystem above lofi is the same as below. 1015 * 1016 * We could come up with a complex strategy using threads to 1017 * do the I/O asynchronously, or we could use task queues. task 1018 * queues were incredibly easy so they win. 1019 */ 1020 lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev)); 1021 if (lsp == NULL) { 1022 bioerror(bp, ENXIO); 1023 biodone(bp); 1024 return (0); 1025 } 1026 1027 mutex_enter(&lsp->ls_vp_lock); 1028 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) { 1029 bioerror(bp, EIO); 1030 biodone(bp); 1031 mutex_exit(&lsp->ls_vp_lock); 1032 return (0); 1033 } 1034 1035 offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */ 1036 if (lsp->ls_crypto_enabled) { 1037 /* encrypted data really begins after crypto header */ 1038 offset += lsp->ls_crypto_offset; 1039 } 1040 if (offset == lsp->ls_vp_size) { 1041 /* EOF */ 1042 if ((bp->b_flags & B_READ) != 0) { 1043 bp->b_resid = bp->b_bcount; 1044 bioerror(bp, 0); 1045 } else { 1046 /* writes should fail */ 1047 bioerror(bp, ENXIO); 1048 } 1049 biodone(bp); 1050 mutex_exit(&lsp->ls_vp_lock); 1051 return (0); 1052 } 1053 if (offset > lsp->ls_vp_size) { 1054 bioerror(bp, ENXIO); 1055 biodone(bp); 1056 mutex_exit(&lsp->ls_vp_lock); 1057 return (0); 1058 } 1059 lsp->ls_vp_iocount++; 1060 mutex_exit(&lsp->ls_vp_lock); 1061 1062 if (lsp->ls_kstat) { 1063 mutex_enter(lsp->ls_kstat->ks_lock); 1064 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat)); 1065 mutex_exit(lsp->ls_kstat->ks_lock); 1066 } 1067 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP); 1068 return (0); 1069 } 1070 1071 /*ARGSUSED2*/ 1072 static int 1073 lofi_read(dev_t dev, struct uio *uio, struct cred *credp) 1074 { 1075 if (getminor(dev) == 0) 1076 return (EINVAL); 1077 UIO_CHECK(uio); 1078 return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio)); 1079 } 1080 1081 /*ARGSUSED2*/ 1082 static int 1083 lofi_write(dev_t dev, struct uio *uio, struct cred *credp) 1084 { 1085 if (getminor(dev) == 0) 1086 return (EINVAL); 1087 UIO_CHECK(uio); 1088 return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio)); 1089 } 1090 1091 /*ARGSUSED2*/ 1092 static int 1093 lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp) 1094 { 1095 if (getminor(dev) == 0) 1096 return (EINVAL); 1097 UIO_CHECK(aio->aio_uio); 1098 return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio)); 1099 } 1100 1101 /*ARGSUSED2*/ 1102 static int 1103 lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp) 1104 { 1105 if (getminor(dev) == 0) 1106 return (EINVAL); 1107 UIO_CHECK(aio->aio_uio); 1108 return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio)); 1109 } 1110 1111 /*ARGSUSED*/ 1112 static int 1113 lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1114 { 1115 switch (infocmd) { 1116 case DDI_INFO_DEVT2DEVINFO: 1117 *result = lofi_dip; 1118 return (DDI_SUCCESS); 1119 case DDI_INFO_DEVT2INSTANCE: 1120 *result = 0; 1121 return (DDI_SUCCESS); 1122 } 1123 return (DDI_FAILURE); 1124 } 1125 1126 static int 1127 lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1128 { 1129 int error; 1130 1131 if (cmd != DDI_ATTACH) 1132 return (DDI_FAILURE); 1133 error = ddi_soft_state_zalloc(lofi_statep, 0); 1134 if (error == DDI_FAILURE) { 1135 return (DDI_FAILURE); 1136 } 1137 error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0, 1138 DDI_PSEUDO, NULL); 1139 if (error == DDI_FAILURE) { 1140 ddi_soft_state_free(lofi_statep, 0); 1141 return (DDI_FAILURE); 1142 } 1143 /* driver handles kernel-issued IOCTLs */ 1144 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, 1145 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) { 1146 ddi_remove_minor_node(dip, NULL); 1147 ddi_soft_state_free(lofi_statep, 0); 1148 return (DDI_FAILURE); 1149 } 1150 lofi_dip = dip; 1151 ddi_report_dev(dip); 1152 return (DDI_SUCCESS); 1153 } 1154 1155 static int 1156 lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1157 { 1158 if (cmd != DDI_DETACH) 1159 return (DDI_FAILURE); 1160 if (lofi_busy()) 1161 return (DDI_FAILURE); 1162 lofi_dip = NULL; 1163 ddi_remove_minor_node(dip, NULL); 1164 ddi_prop_remove_all(dip); 1165 ddi_soft_state_free(lofi_statep, 0); 1166 return (DDI_SUCCESS); 1167 } 1168 1169 /* 1170 * With addition of encryption, be careful that encryption key is wiped before 1171 * kernel memory structures are freed, and also that key is not accidentally 1172 * passed out into userland structures. 1173 */ 1174 static void 1175 free_lofi_ioctl(struct lofi_ioctl *klip) 1176 { 1177 /* Make sure this encryption key doesn't stick around */ 1178 bzero(klip->li_key, sizeof (klip->li_key)); 1179 kmem_free(klip, sizeof (struct lofi_ioctl)); 1180 } 1181 1182 /* 1183 * These two just simplify the rest of the ioctls that need to copyin/out 1184 * the lofi_ioctl structure. 1185 */ 1186 struct lofi_ioctl * 1187 copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag) 1188 { 1189 struct lofi_ioctl *klip; 1190 int error; 1191 1192 klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP); 1193 error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag); 1194 if (error) { 1195 free_lofi_ioctl(klip); 1196 return (NULL); 1197 } 1198 1199 /* make sure filename is always null-terminated */ 1200 klip->li_filename[MAXPATHLEN-1] = '\0'; 1201 1202 /* validate minor number */ 1203 if (klip->li_minor > lofi_max_files) { 1204 free_lofi_ioctl(klip); 1205 cmn_err(CE_WARN, "attempt to map more than lofi_max_files (%d)", 1206 lofi_max_files); 1207 return (NULL); 1208 } 1209 return (klip); 1210 } 1211 1212 int 1213 copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip, 1214 int flag) 1215 { 1216 int error; 1217 1218 /* 1219 * NOTE: Do NOT copy the crypto_key_t "back" to userland. 1220 * This ensures that an attacker can't trivially find the 1221 * key for a mapping just by issuing the ioctl. 1222 * 1223 * It can still be found by poking around in kmem with mdb(1), 1224 * but there is no point in making it easy when the info isn't 1225 * of any use in this direction anyway. 1226 * 1227 * Either way we don't actually have the raw key stored in 1228 * a form that we can get it anyway, since we just used it 1229 * to create a ctx template and didn't keep "the original". 1230 */ 1231 error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag); 1232 if (error) 1233 return (EFAULT); 1234 return (0); 1235 } 1236 1237 /* 1238 * Return the minor number 'filename' is mapped to, if it is. 1239 */ 1240 static int 1241 file_to_minor(char *filename) 1242 { 1243 minor_t minor; 1244 struct lofi_state *lsp; 1245 1246 ASSERT(mutex_owned(&lofi_lock)); 1247 for (minor = 1; minor <= lofi_max_files; minor++) { 1248 lsp = ddi_get_soft_state(lofi_statep, minor); 1249 if (lsp == NULL) 1250 continue; 1251 if (strcmp(lsp->ls_filename, filename) == 0) 1252 return (minor); 1253 } 1254 return (0); 1255 } 1256 1257 /* 1258 * lofiadm does some validation, but since Joe Random (or crashme) could 1259 * do our ioctls, we need to do some validation too. 1260 */ 1261 static int 1262 valid_filename(const char *filename) 1263 { 1264 static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/"; 1265 static char *charprefix = "/dev/" LOFI_CHAR_NAME "/"; 1266 1267 /* must be absolute path */ 1268 if (filename[0] != '/') 1269 return (0); 1270 /* must not be lofi */ 1271 if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0) 1272 return (0); 1273 if (strncmp(filename, charprefix, strlen(charprefix)) == 0) 1274 return (0); 1275 return (1); 1276 } 1277 1278 /* 1279 * Fakes up a disk geometry, and one big partition, based on the size 1280 * of the file. This is needed because we allow newfs'ing the device, 1281 * and newfs will do several disk ioctls to figure out the geometry and 1282 * partition information. It uses that information to determine the parameters 1283 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we 1284 * have to support it. 1285 */ 1286 static void 1287 fake_disk_geometry(struct lofi_state *lsp) 1288 { 1289 u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset; 1290 1291 /* dk_geom - see dkio(7I) */ 1292 /* 1293 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs 1294 * of sectors), but that breaks programs like fdisk which want to 1295 * partition a disk by cylinder. With one cylinder, you can't create 1296 * an fdisk partition and put pcfs on it for testing (hard to pick 1297 * a number between one and one). 1298 * 1299 * The cheezy floppy test is an attempt to not have too few cylinders 1300 * for a small file, or so many on a big file that you waste space 1301 * for backup superblocks or cylinder group structures. 1302 */ 1303 if (dsize < (2 * 1024 * 1024)) /* floppy? */ 1304 lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024); 1305 else 1306 lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024); 1307 /* in case file file is < 100k */ 1308 if (lsp->ls_dkg.dkg_ncyl == 0) 1309 lsp->ls_dkg.dkg_ncyl = 1; 1310 lsp->ls_dkg.dkg_acyl = 0; 1311 lsp->ls_dkg.dkg_bcyl = 0; 1312 lsp->ls_dkg.dkg_nhead = 1; 1313 lsp->ls_dkg.dkg_obs1 = 0; 1314 lsp->ls_dkg.dkg_intrlv = 0; 1315 lsp->ls_dkg.dkg_obs2 = 0; 1316 lsp->ls_dkg.dkg_obs3 = 0; 1317 lsp->ls_dkg.dkg_apc = 0; 1318 lsp->ls_dkg.dkg_rpm = 7200; 1319 lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl; 1320 lsp->ls_dkg.dkg_nsect = dsize / (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl); 1321 lsp->ls_dkg.dkg_write_reinstruct = 0; 1322 lsp->ls_dkg.dkg_read_reinstruct = 0; 1323 1324 /* vtoc - see dkio(7I) */ 1325 bzero(&lsp->ls_vtoc, sizeof (struct vtoc)); 1326 lsp->ls_vtoc.v_sanity = VTOC_SANE; 1327 lsp->ls_vtoc.v_version = V_VERSION; 1328 (void) strncpy(lsp->ls_vtoc.v_volume, LOFI_DRIVER_NAME, 1329 sizeof (lsp->ls_vtoc.v_volume)); 1330 lsp->ls_vtoc.v_sectorsz = DEV_BSIZE; 1331 lsp->ls_vtoc.v_nparts = 1; 1332 lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED; 1333 1334 /* 1335 * A compressed file is read-only, other files can 1336 * be read-write 1337 */ 1338 if (lsp->ls_uncomp_seg_sz > 0) { 1339 lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY; 1340 } else { 1341 lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT; 1342 } 1343 lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0; 1344 /* 1345 * The partition size cannot just be the number of sectors, because 1346 * that might not end on a cylinder boundary. And if that's the case, 1347 * newfs/mkfs will print a scary warning. So just figure the size 1348 * based on the number of cylinders and sectors/cylinder. 1349 */ 1350 lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl * 1351 lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead; 1352 1353 /* dk_cinfo - see dkio(7I) */ 1354 bzero(&lsp->ls_ci, sizeof (struct dk_cinfo)); 1355 (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME); 1356 lsp->ls_ci.dki_ctype = DKC_MD; 1357 lsp->ls_ci.dki_flags = 0; 1358 lsp->ls_ci.dki_cnum = 0; 1359 lsp->ls_ci.dki_addr = 0; 1360 lsp->ls_ci.dki_space = 0; 1361 lsp->ls_ci.dki_prio = 0; 1362 lsp->ls_ci.dki_vec = 0; 1363 (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME); 1364 lsp->ls_ci.dki_unit = 0; 1365 lsp->ls_ci.dki_slave = 0; 1366 lsp->ls_ci.dki_partition = 0; 1367 /* 1368 * newfs uses this to set maxcontig. Must not be < 16, or it 1369 * will be 0 when newfs multiplies it by DEV_BSIZE and divides 1370 * it by the block size. Then tunefs doesn't work because 1371 * maxcontig is 0. 1372 */ 1373 lsp->ls_ci.dki_maxtransfer = 16; 1374 } 1375 1376 /* 1377 * map in a compressed file 1378 * 1379 * Read in the header and the index that follows. 1380 * 1381 * The header is as follows - 1382 * 1383 * Signature (name of the compression algorithm) 1384 * Compression segment size (a multiple of 512) 1385 * Number of index entries 1386 * Size of the last block 1387 * The array containing the index entries 1388 * 1389 * The header information is always stored in 1390 * network byte order on disk. 1391 */ 1392 static int 1393 lofi_map_compressed_file(struct lofi_state *lsp, char *buf) 1394 { 1395 uint32_t index_sz, header_len, i; 1396 ssize_t resid; 1397 enum uio_rw rw; 1398 char *tbuf = buf; 1399 int error; 1400 1401 /* The signature has already been read */ 1402 tbuf += sizeof (lsp->ls_comp_algorithm); 1403 bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz)); 1404 lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz); 1405 1406 /* 1407 * The compressed segment size must be a power of 2 1408 */ 1409 if (lsp->ls_uncomp_seg_sz % 2) 1410 return (EINVAL); 1411 1412 for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++) 1413 ; 1414 1415 lsp->ls_comp_seg_shift = i; 1416 1417 tbuf += sizeof (lsp->ls_uncomp_seg_sz); 1418 bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz)); 1419 lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz); 1420 1421 tbuf += sizeof (lsp->ls_comp_index_sz); 1422 bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz), 1423 sizeof (lsp->ls_uncomp_last_seg_sz)); 1424 lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz); 1425 1426 /* 1427 * Compute the total size of the uncompressed data 1428 * for use in fake_disk_geometry and other calculations. 1429 * Disk geometry has to be faked with respect to the 1430 * actual uncompressed data size rather than the 1431 * compressed file size. 1432 */ 1433 lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz 1434 + lsp->ls_uncomp_last_seg_sz; 1435 1436 /* 1437 * Index size is rounded up to a 512 byte boundary for ease 1438 * of segmapping 1439 */ 1440 index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz; 1441 header_len = sizeof (lsp->ls_comp_algorithm) + 1442 sizeof (lsp->ls_uncomp_seg_sz) + 1443 sizeof (lsp->ls_comp_index_sz) + 1444 sizeof (lsp->ls_uncomp_last_seg_sz); 1445 lsp->ls_comp_offbase = header_len + index_sz; 1446 1447 index_sz += header_len; 1448 index_sz = roundup(index_sz, DEV_BSIZE); 1449 1450 lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP); 1451 lsp->ls_comp_index_data_sz = index_sz; 1452 1453 /* 1454 * Read in the index -- this has a side-effect 1455 * of reading in the header as well 1456 */ 1457 rw = UIO_READ; 1458 error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz, 1459 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 1460 1461 if (error != 0) 1462 return (error); 1463 1464 /* Skip the header, this is where the index really begins */ 1465 lsp->ls_comp_seg_index = 1466 /*LINTED*/ 1467 (uint64_t *)(lsp->ls_comp_index_data + header_len); 1468 1469 /* 1470 * Now recompute offsets in the index to account for 1471 * the header length 1472 */ 1473 for (i = 0; i < lsp->ls_comp_index_sz; i++) { 1474 lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase + 1475 BE_64(lsp->ls_comp_seg_index[i]); 1476 } 1477 1478 return (error); 1479 } 1480 1481 /* 1482 * Check to see if the passed in signature is a valid 1483 * one. If it is valid, return the index into 1484 * lofi_compress_table. 1485 * 1486 * Return -1 if it is invalid 1487 */ 1488 static int lofi_compress_select(char *signature) 1489 { 1490 int i; 1491 1492 for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) { 1493 if (strcmp(lofi_compress_table[i].l_name, signature) == 0) 1494 return (i); 1495 } 1496 1497 return (-1); 1498 } 1499 1500 /* 1501 * map a file to a minor number. Return the minor number. 1502 */ 1503 static int 1504 lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor, 1505 int *rvalp, struct cred *credp, int ioctl_flag) 1506 { 1507 minor_t newminor; 1508 struct lofi_state *lsp; 1509 struct lofi_ioctl *klip; 1510 int error; 1511 struct vnode *vp; 1512 int64_t Nblocks_prop_val; 1513 int64_t Size_prop_val; 1514 int compress_index; 1515 vattr_t vattr; 1516 int flag; 1517 enum vtype v_type; 1518 int zalloced = 0; 1519 dev_t newdev; 1520 char namebuf[50]; 1521 char buf[DEV_BSIZE]; 1522 char crybuf[DEV_BSIZE]; 1523 ssize_t resid; 1524 boolean_t need_vn_close = B_FALSE; 1525 boolean_t keycopied = B_FALSE; 1526 boolean_t need_size_update = B_FALSE; 1527 1528 klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 1529 if (klip == NULL) 1530 return (EFAULT); 1531 1532 mutex_enter(&lofi_lock); 1533 1534 if (!valid_filename(klip->li_filename)) { 1535 error = EINVAL; 1536 goto out; 1537 } 1538 1539 if (file_to_minor(klip->li_filename) != 0) { 1540 error = EBUSY; 1541 goto out; 1542 } 1543 1544 if (pickminor) { 1545 /* Find a free one */ 1546 for (newminor = 1; newminor <= lofi_max_files; newminor++) 1547 if (ddi_get_soft_state(lofi_statep, newminor) == NULL) 1548 break; 1549 if (newminor >= lofi_max_files) { 1550 error = EAGAIN; 1551 goto out; 1552 } 1553 } else { 1554 newminor = klip->li_minor; 1555 if (ddi_get_soft_state(lofi_statep, newminor) != NULL) { 1556 error = EEXIST; 1557 goto out; 1558 } 1559 } 1560 1561 /* make sure it's valid */ 1562 error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW, 1563 NULLVPP, &vp); 1564 if (error) { 1565 goto out; 1566 } 1567 v_type = vp->v_type; 1568 VN_RELE(vp); 1569 if (!V_ISLOFIABLE(v_type)) { 1570 error = EINVAL; 1571 goto out; 1572 } 1573 flag = FREAD | FWRITE | FOFFMAX | FEXCL; 1574 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0); 1575 if (error) { 1576 /* try read-only */ 1577 flag &= ~FWRITE; 1578 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, 1579 &vp, 0, 0); 1580 if (error) { 1581 goto out; 1582 } 1583 } 1584 need_vn_close = B_TRUE; 1585 1586 vattr.va_mask = AT_SIZE; 1587 error = VOP_GETATTR(vp, &vattr, 0, credp, NULL); 1588 if (error) { 1589 goto out; 1590 } 1591 /* the file needs to be a multiple of the block size */ 1592 if ((vattr.va_size % DEV_BSIZE) != 0) { 1593 error = EINVAL; 1594 goto out; 1595 } 1596 newdev = makedevice(getmajor(dev), newminor); 1597 Size_prop_val = vattr.va_size; 1598 if ((ddi_prop_update_int64(newdev, lofi_dip, 1599 SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) { 1600 error = EINVAL; 1601 goto out; 1602 } 1603 Nblocks_prop_val = vattr.va_size / DEV_BSIZE; 1604 if ((ddi_prop_update_int64(newdev, lofi_dip, 1605 NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 1606 error = EINVAL; 1607 goto propout; 1608 } 1609 error = ddi_soft_state_zalloc(lofi_statep, newminor); 1610 if (error == DDI_FAILURE) { 1611 error = ENOMEM; 1612 goto propout; 1613 } 1614 zalloced = 1; 1615 (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 1616 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor, 1617 DDI_PSEUDO, NULL); 1618 if (error != DDI_SUCCESS) { 1619 error = ENXIO; 1620 goto propout; 1621 } 1622 (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor); 1623 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor, 1624 DDI_PSEUDO, NULL); 1625 if (error != DDI_SUCCESS) { 1626 /* remove block node */ 1627 (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor); 1628 ddi_remove_minor_node(lofi_dip, namebuf); 1629 error = ENXIO; 1630 goto propout; 1631 } 1632 lsp = ddi_get_soft_state(lofi_statep, newminor); 1633 lsp->ls_filename_sz = strlen(klip->li_filename) + 1; 1634 lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP); 1635 (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d", 1636 LOFI_DRIVER_NAME, newminor); 1637 lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads, 1638 minclsyspri, 1, lofi_taskq_maxalloc, 0); 1639 lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor, 1640 NULL, "disk", KSTAT_TYPE_IO, 1, 0); 1641 if (lsp->ls_kstat) { 1642 mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL); 1643 lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock; 1644 kstat_install(lsp->ls_kstat); 1645 } 1646 cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL); 1647 mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL); 1648 1649 /* 1650 * save open mode so file can be closed properly and vnode counts 1651 * updated correctly. 1652 */ 1653 lsp->ls_openflag = flag; 1654 1655 /* 1656 * Try to handle stacked lofs vnodes. 1657 */ 1658 if (vp->v_type == VREG) { 1659 if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) { 1660 lsp->ls_vp = vp; 1661 } else { 1662 /* 1663 * Even though vp was obtained via vn_open(), we 1664 * can't call vn_close() on it, since lofs will 1665 * pass the VOP_CLOSE() on down to the realvp 1666 * (which we are about to use). Hence we merely 1667 * drop the reference to the lofs vnode and hold 1668 * the realvp so things behave as if we've 1669 * opened the realvp without any interaction 1670 * with lofs. 1671 */ 1672 VN_HOLD(lsp->ls_vp); 1673 VN_RELE(vp); 1674 } 1675 } else { 1676 lsp->ls_vp = vp; 1677 } 1678 lsp->ls_vp_size = vattr.va_size; 1679 (void) strcpy(lsp->ls_filename, klip->li_filename); 1680 if (rvalp) 1681 *rvalp = (int)newminor; 1682 klip->li_minor = newminor; 1683 1684 /* 1685 * Initialize crypto details for encrypted lofi 1686 */ 1687 if (klip->li_crypto_enabled) { 1688 int ret; 1689 1690 mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL); 1691 1692 lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher); 1693 if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) { 1694 cmn_err(CE_WARN, "invalid cipher %s requested for %s", 1695 klip->li_cipher, lsp->ls_filename); 1696 error = EINVAL; 1697 goto propout; 1698 } 1699 1700 /* this is just initialization here */ 1701 lsp->ls_mech.cm_param = NULL; 1702 lsp->ls_mech.cm_param_len = 0; 1703 1704 lsp->ls_iv_type = klip->li_iv_type; 1705 lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher); 1706 if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) { 1707 cmn_err(CE_WARN, "invalid iv cipher %s requested" 1708 " for %s", klip->li_iv_cipher, lsp->ls_filename); 1709 error = EINVAL; 1710 goto propout; 1711 } 1712 1713 /* iv mech must itself take a null iv */ 1714 lsp->ls_iv_mech.cm_param = NULL; 1715 lsp->ls_iv_mech.cm_param_len = 0; 1716 lsp->ls_iv_len = klip->li_iv_len; 1717 1718 /* 1719 * Create ctx using li_cipher & the raw li_key after checking 1720 * that it isn't a weak key. 1721 */ 1722 lsp->ls_key.ck_format = CRYPTO_KEY_RAW; 1723 lsp->ls_key.ck_length = klip->li_key_len; 1724 lsp->ls_key.ck_data = kmem_alloc( 1725 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP); 1726 bcopy(klip->li_key, lsp->ls_key.ck_data, 1727 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 1728 keycopied = B_TRUE; 1729 1730 ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key); 1731 if (ret != CRYPTO_SUCCESS) { 1732 error = EINVAL; 1733 cmn_err(CE_WARN, "weak key check failed for cipher " 1734 "%s on file %s (0x%x)", klip->li_cipher, 1735 lsp->ls_filename, ret); 1736 goto propout; 1737 } 1738 } 1739 lsp->ls_crypto_enabled = klip->li_crypto_enabled; 1740 1741 /* 1742 * Read the file signature to check if it is compressed or encrypted. 1743 * Crypto signature is in a different location; both areas should 1744 * read to keep compression and encryption mutually exclusive. 1745 */ 1746 if (lsp->ls_crypto_enabled) { 1747 error = vn_rdwr(UIO_READ, lsp->ls_vp, crybuf, DEV_BSIZE, 1748 CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 1749 if (error != 0) 1750 goto propout; 1751 } 1752 error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE, 1753 0, RLIM64_INFINITY, kcred, &resid); 1754 if (error != 0) 1755 goto propout; 1756 1757 /* initialize these variables for all lofi files */ 1758 lsp->ls_uncomp_seg_sz = 0; 1759 lsp->ls_vp_comp_size = lsp->ls_vp_size; 1760 lsp->ls_comp_algorithm[0] = '\0'; 1761 1762 /* encrypted lofi reads/writes shifted by crypto metadata size */ 1763 lsp->ls_crypto_offset = 0; 1764 1765 /* this is a compressed lofi */ 1766 if ((compress_index = lofi_compress_select(buf)) != -1) { 1767 1768 /* compression and encryption are mutually exclusive */ 1769 if (klip->li_crypto_enabled) { 1770 error = ENOTSUP; 1771 goto propout; 1772 } 1773 1774 /* initialize compression info for compressed lofi */ 1775 lsp->ls_comp_algorithm_index = compress_index; 1776 (void) strlcpy(lsp->ls_comp_algorithm, 1777 lofi_compress_table[compress_index].l_name, 1778 sizeof (lsp->ls_comp_algorithm)); 1779 1780 error = lofi_map_compressed_file(lsp, buf); 1781 if (error != 0) 1782 goto propout; 1783 need_size_update = B_TRUE; 1784 1785 /* this is an encrypted lofi */ 1786 } else if (strncmp(crybuf, lofi_crypto_magic, 1787 sizeof (lofi_crypto_magic)) == 0) { 1788 1789 char *marker = crybuf; 1790 1791 /* 1792 * This is the case where the header in the lofi image is 1793 * already initialized to indicate it is encrypted. 1794 * There is another case (see below) where encryption is 1795 * requested but the lofi image has never been used yet, 1796 * so the header needs to be written with encryption magic. 1797 */ 1798 1799 /* indicate this must be an encrypted lofi due to magic */ 1800 klip->li_crypto_enabled = B_TRUE; 1801 1802 /* 1803 * The encryption header information is laid out this way: 1804 * 6 bytes: hex "CFLOFI" 1805 * 2 bytes: version = 0 ... for now 1806 * 96 bytes: reserved1 (not implemented yet) 1807 * 4 bytes: data_sector = 2 ... for now 1808 * more... not implemented yet 1809 */ 1810 1811 /* copy the magic */ 1812 bcopy(marker, lsp->ls_crypto.magic, 1813 sizeof (lsp->ls_crypto.magic)); 1814 marker += sizeof (lsp->ls_crypto.magic); 1815 1816 /* read the encryption version number */ 1817 bcopy(marker, &(lsp->ls_crypto.version), 1818 sizeof (lsp->ls_crypto.version)); 1819 lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version); 1820 marker += sizeof (lsp->ls_crypto.version); 1821 1822 /* read a chunk of reserved data */ 1823 bcopy(marker, lsp->ls_crypto.reserved1, 1824 sizeof (lsp->ls_crypto.reserved1)); 1825 marker += sizeof (lsp->ls_crypto.reserved1); 1826 1827 /* read block number where encrypted data begins */ 1828 bcopy(marker, &(lsp->ls_crypto.data_sector), 1829 sizeof (lsp->ls_crypto.data_sector)); 1830 lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector); 1831 marker += sizeof (lsp->ls_crypto.data_sector); 1832 1833 /* and ignore the rest until it is implemented */ 1834 1835 lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 1836 need_size_update = B_TRUE; 1837 1838 /* neither compressed nor encrypted, BUT could be new encrypted lofi */ 1839 } else if (klip->li_crypto_enabled) { 1840 1841 /* 1842 * This is the case where encryption was requested but the 1843 * appears to be entirely blank where the encryption header 1844 * would have been in the lofi image. If it is blank, 1845 * assume it is a brand new lofi image and initialize the 1846 * header area with encryption magic and current version 1847 * header data. If it is not blank, that's an error. 1848 */ 1849 int i; 1850 char *marker; 1851 struct crypto_meta chead; 1852 1853 for (i = 0; i < sizeof (struct crypto_meta); i++) 1854 if (crybuf[i] != '\0') 1855 break; 1856 if (i != sizeof (struct crypto_meta)) { 1857 error = EINVAL; 1858 goto propout; 1859 } 1860 1861 /* nothing there, initialize as encrypted lofi */ 1862 marker = crybuf; 1863 bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic)); 1864 marker += sizeof (lofi_crypto_magic); 1865 chead.version = htons(LOFI_CRYPTO_VERSION); 1866 bcopy(&(chead.version), marker, sizeof (chead.version)); 1867 marker += sizeof (chead.version); 1868 marker += sizeof (chead.reserved1); 1869 chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR); 1870 bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector)); 1871 1872 /* write the header */ 1873 error = vn_rdwr(UIO_WRITE, lsp->ls_vp, crybuf, DEV_BSIZE, 1874 CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid); 1875 if (error != 0) 1876 goto propout; 1877 1878 /* fix things up so it looks like we read this info */ 1879 bcopy(lofi_crypto_magic, lsp->ls_crypto.magic, 1880 sizeof (lofi_crypto_magic)); 1881 lsp->ls_crypto.version = LOFI_CRYPTO_VERSION; 1882 lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR; 1883 1884 lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE; 1885 need_size_update = B_TRUE; 1886 } 1887 1888 /* 1889 * Either lsp->ls_vp_size or lsp->ls_crypto_offset changed; 1890 * for encrypted lofi, advertise that it is somewhat shorter 1891 * due to embedded crypto metadata section 1892 */ 1893 if (need_size_update) { 1894 /* update DDI properties */ 1895 Size_prop_val = lsp->ls_vp_size - lsp->ls_crypto_offset; 1896 if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME, 1897 Size_prop_val)) != DDI_PROP_SUCCESS) { 1898 error = EINVAL; 1899 goto propout; 1900 } 1901 Nblocks_prop_val = 1902 (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE; 1903 if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME, 1904 Nblocks_prop_val)) != DDI_PROP_SUCCESS) { 1905 error = EINVAL; 1906 goto propout; 1907 } 1908 } 1909 1910 fake_disk_geometry(lsp); 1911 mutex_exit(&lofi_lock); 1912 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 1913 free_lofi_ioctl(klip); 1914 return (0); 1915 1916 propout: 1917 if (keycopied) { 1918 bzero(lsp->ls_key.ck_data, 1919 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 1920 kmem_free(lsp->ls_key.ck_data, 1921 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length)); 1922 lsp->ls_key.ck_data = NULL; 1923 lsp->ls_key.ck_length = 0; 1924 } 1925 1926 if (zalloced) 1927 ddi_soft_state_free(lofi_statep, newminor); 1928 1929 (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME); 1930 (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME); 1931 1932 out: 1933 if (need_vn_close) { 1934 (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL); 1935 VN_RELE(vp); 1936 } 1937 1938 mutex_exit(&lofi_lock); 1939 free_lofi_ioctl(klip); 1940 return (error); 1941 } 1942 1943 /* 1944 * unmap a file. 1945 */ 1946 static int 1947 lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename, 1948 struct cred *credp, int ioctl_flag) 1949 { 1950 struct lofi_state *lsp; 1951 struct lofi_ioctl *klip; 1952 minor_t minor; 1953 1954 klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 1955 if (klip == NULL) 1956 return (EFAULT); 1957 1958 mutex_enter(&lofi_lock); 1959 if (byfilename) { 1960 minor = file_to_minor(klip->li_filename); 1961 } else { 1962 minor = klip->li_minor; 1963 } 1964 if (minor == 0) { 1965 mutex_exit(&lofi_lock); 1966 free_lofi_ioctl(klip); 1967 return (ENXIO); 1968 } 1969 lsp = ddi_get_soft_state(lofi_statep, minor); 1970 if (lsp == NULL || lsp->ls_vp == NULL) { 1971 mutex_exit(&lofi_lock); 1972 free_lofi_ioctl(klip); 1973 return (ENXIO); 1974 } 1975 1976 /* 1977 * If it's still held open, we'll do one of three things: 1978 * 1979 * If no flag is set, just return EBUSY. 1980 * 1981 * If the 'cleanup' flag is set, unmap and remove the device when 1982 * the last user finishes. 1983 * 1984 * If the 'force' flag is set, then we forcibly close the underlying 1985 * file. Subsequent operations will fail, and the DKIOCSTATE ioctl 1986 * will return DKIO_DEV_GONE. When the device is last closed, the 1987 * device will be cleaned up appropriately. 1988 * 1989 * This is complicated by the fact that we may have outstanding 1990 * dispatched I/Os. Rather than having a single mutex to serialize all 1991 * I/O, we keep a count of the number of outstanding I/O requests 1992 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os 1993 * should be dispatched (ls_vp_closereq). 1994 * 1995 * We set the flag, wait for the number of outstanding I/Os to reach 0, 1996 * and then close the underlying vnode. 1997 */ 1998 if (is_opened(lsp)) { 1999 if (klip->li_force) { 2000 /* 2001 * XXX: the section marked here should probably be 2002 * carefully incorporated into lofi_free_handle(); 2003 * afterward just replace this section with: 2004 * lofi_free_handle(dev, minor, lsp, credp); 2005 * and clean up lofi_unmap_file() a bit more 2006 */ 2007 lofi_free_crypto(lsp); 2008 2009 mutex_enter(&lsp->ls_vp_lock); 2010 lsp->ls_vp_closereq = B_TRUE; 2011 while (lsp->ls_vp_iocount > 0) 2012 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock); 2013 (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, 2014 credp, NULL); 2015 VN_RELE(lsp->ls_vp); 2016 lsp->ls_vp = NULL; 2017 cv_broadcast(&lsp->ls_vp_cv); 2018 mutex_exit(&lsp->ls_vp_lock); 2019 /* 2020 * XXX: to here 2021 */ 2022 2023 klip->li_minor = minor; 2024 mutex_exit(&lofi_lock); 2025 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2026 free_lofi_ioctl(klip); 2027 return (0); 2028 } else if (klip->li_cleanup) { 2029 lsp->ls_cleanup = 1; 2030 mutex_exit(&lofi_lock); 2031 free_lofi_ioctl(klip); 2032 return (0); 2033 } 2034 2035 mutex_exit(&lofi_lock); 2036 free_lofi_ioctl(klip); 2037 return (EBUSY); 2038 } 2039 2040 lofi_free_handle(dev, minor, lsp, credp); 2041 2042 klip->li_minor = minor; 2043 mutex_exit(&lofi_lock); 2044 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2045 free_lofi_ioctl(klip); 2046 return (0); 2047 } 2048 2049 /* 2050 * get the filename given the minor number, or the minor number given 2051 * the name. 2052 */ 2053 /*ARGSUSED*/ 2054 static int 2055 lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which, 2056 struct cred *credp, int ioctl_flag) 2057 { 2058 struct lofi_state *lsp; 2059 struct lofi_ioctl *klip; 2060 int error; 2061 minor_t minor; 2062 2063 klip = copy_in_lofi_ioctl(ulip, ioctl_flag); 2064 if (klip == NULL) 2065 return (EFAULT); 2066 2067 switch (which) { 2068 case LOFI_GET_FILENAME: 2069 minor = klip->li_minor; 2070 if (minor == 0) { 2071 free_lofi_ioctl(klip); 2072 return (EINVAL); 2073 } 2074 2075 mutex_enter(&lofi_lock); 2076 lsp = ddi_get_soft_state(lofi_statep, minor); 2077 if (lsp == NULL) { 2078 mutex_exit(&lofi_lock); 2079 free_lofi_ioctl(klip); 2080 return (ENXIO); 2081 } 2082 (void) strcpy(klip->li_filename, lsp->ls_filename); 2083 (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 2084 sizeof (klip->li_algorithm)); 2085 klip->li_crypto_enabled = lsp->ls_crypto_enabled; 2086 mutex_exit(&lofi_lock); 2087 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2088 free_lofi_ioctl(klip); 2089 return (error); 2090 case LOFI_GET_MINOR: 2091 mutex_enter(&lofi_lock); 2092 klip->li_minor = file_to_minor(klip->li_filename); 2093 /* caller should not depend on klip->li_crypto_enabled here */ 2094 mutex_exit(&lofi_lock); 2095 if (klip->li_minor == 0) { 2096 free_lofi_ioctl(klip); 2097 return (ENOENT); 2098 } 2099 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2100 free_lofi_ioctl(klip); 2101 return (error); 2102 case LOFI_CHECK_COMPRESSED: 2103 mutex_enter(&lofi_lock); 2104 klip->li_minor = file_to_minor(klip->li_filename); 2105 mutex_exit(&lofi_lock); 2106 if (klip->li_minor == 0) { 2107 free_lofi_ioctl(klip); 2108 return (ENOENT); 2109 } 2110 mutex_enter(&lofi_lock); 2111 lsp = ddi_get_soft_state(lofi_statep, klip->li_minor); 2112 if (lsp == NULL) { 2113 mutex_exit(&lofi_lock); 2114 free_lofi_ioctl(klip); 2115 return (ENXIO); 2116 } 2117 ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0); 2118 2119 (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm, 2120 sizeof (klip->li_algorithm)); 2121 mutex_exit(&lofi_lock); 2122 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag); 2123 free_lofi_ioctl(klip); 2124 return (error); 2125 default: 2126 free_lofi_ioctl(klip); 2127 return (EINVAL); 2128 } 2129 2130 } 2131 2132 static int 2133 lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, 2134 int *rvalp) 2135 { 2136 int error; 2137 enum dkio_state dkstate; 2138 struct lofi_state *lsp; 2139 minor_t minor; 2140 2141 minor = getminor(dev); 2142 /* lofi ioctls only apply to the master device */ 2143 if (minor == 0) { 2144 struct lofi_ioctl *lip = (struct lofi_ioctl *)arg; 2145 2146 /* 2147 * the query command only need read-access - i.e., normal 2148 * users are allowed to do those on the ctl device as 2149 * long as they can open it read-only. 2150 */ 2151 switch (cmd) { 2152 case LOFI_MAP_FILE: 2153 if ((flag & FWRITE) == 0) 2154 return (EPERM); 2155 return (lofi_map_file(dev, lip, 1, rvalp, credp, flag)); 2156 case LOFI_MAP_FILE_MINOR: 2157 if ((flag & FWRITE) == 0) 2158 return (EPERM); 2159 return (lofi_map_file(dev, lip, 0, rvalp, credp, flag)); 2160 case LOFI_UNMAP_FILE: 2161 if ((flag & FWRITE) == 0) 2162 return (EPERM); 2163 return (lofi_unmap_file(dev, lip, 1, credp, flag)); 2164 case LOFI_UNMAP_FILE_MINOR: 2165 if ((flag & FWRITE) == 0) 2166 return (EPERM); 2167 return (lofi_unmap_file(dev, lip, 0, credp, flag)); 2168 case LOFI_GET_FILENAME: 2169 return (lofi_get_info(dev, lip, LOFI_GET_FILENAME, 2170 credp, flag)); 2171 case LOFI_GET_MINOR: 2172 return (lofi_get_info(dev, lip, LOFI_GET_MINOR, 2173 credp, flag)); 2174 case LOFI_GET_MAXMINOR: 2175 error = ddi_copyout(&lofi_max_files, &lip->li_minor, 2176 sizeof (lofi_max_files), flag); 2177 if (error) 2178 return (EFAULT); 2179 return (0); 2180 case LOFI_CHECK_COMPRESSED: 2181 return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED, 2182 credp, flag)); 2183 default: 2184 break; 2185 } 2186 } 2187 2188 lsp = ddi_get_soft_state(lofi_statep, minor); 2189 if (lsp == NULL) 2190 return (ENXIO); 2191 2192 /* 2193 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with 2194 * EIO as if the device was no longer present. 2195 */ 2196 if (lsp->ls_vp == NULL && cmd != DKIOCSTATE) 2197 return (EIO); 2198 2199 /* these are for faking out utilities like newfs */ 2200 switch (cmd) { 2201 case DKIOCGVTOC: 2202 switch (ddi_model_convert_from(flag & FMODELS)) { 2203 case DDI_MODEL_ILP32: { 2204 struct vtoc32 vtoc32; 2205 2206 vtoctovtoc32(lsp->ls_vtoc, vtoc32); 2207 if (ddi_copyout(&vtoc32, (void *)arg, 2208 sizeof (struct vtoc32), flag)) 2209 return (EFAULT); 2210 break; 2211 } 2212 2213 case DDI_MODEL_NONE: 2214 if (ddi_copyout(&lsp->ls_vtoc, (void *)arg, 2215 sizeof (struct vtoc), flag)) 2216 return (EFAULT); 2217 break; 2218 } 2219 return (0); 2220 case DKIOCINFO: 2221 error = ddi_copyout(&lsp->ls_ci, (void *)arg, 2222 sizeof (struct dk_cinfo), flag); 2223 if (error) 2224 return (EFAULT); 2225 return (0); 2226 case DKIOCG_VIRTGEOM: 2227 case DKIOCG_PHYGEOM: 2228 case DKIOCGGEOM: 2229 error = ddi_copyout(&lsp->ls_dkg, (void *)arg, 2230 sizeof (struct dk_geom), flag); 2231 if (error) 2232 return (EFAULT); 2233 return (0); 2234 case DKIOCSTATE: 2235 /* 2236 * Normally, lofi devices are always in the INSERTED state. If 2237 * a device is forcefully unmapped, then the device transitions 2238 * to the DKIO_DEV_GONE state. 2239 */ 2240 if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate), 2241 flag) != 0) 2242 return (EFAULT); 2243 2244 mutex_enter(&lsp->ls_vp_lock); 2245 while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) || 2246 (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) { 2247 /* 2248 * By virtue of having the device open, we know that 2249 * 'lsp' will remain valid when we return. 2250 */ 2251 if (!cv_wait_sig(&lsp->ls_vp_cv, 2252 &lsp->ls_vp_lock)) { 2253 mutex_exit(&lsp->ls_vp_lock); 2254 return (EINTR); 2255 } 2256 } 2257 2258 dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE); 2259 mutex_exit(&lsp->ls_vp_lock); 2260 2261 if (ddi_copyout(&dkstate, (void *)arg, 2262 sizeof (dkstate), flag) != 0) 2263 return (EFAULT); 2264 return (0); 2265 default: 2266 return (ENOTTY); 2267 } 2268 } 2269 2270 static struct cb_ops lofi_cb_ops = { 2271 lofi_open, /* open */ 2272 lofi_close, /* close */ 2273 lofi_strategy, /* strategy */ 2274 nodev, /* print */ 2275 nodev, /* dump */ 2276 lofi_read, /* read */ 2277 lofi_write, /* write */ 2278 lofi_ioctl, /* ioctl */ 2279 nodev, /* devmap */ 2280 nodev, /* mmap */ 2281 nodev, /* segmap */ 2282 nochpoll, /* poll */ 2283 ddi_prop_op, /* prop_op */ 2284 0, /* streamtab */ 2285 D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */ 2286 CB_REV, 2287 lofi_aread, 2288 lofi_awrite 2289 }; 2290 2291 static struct dev_ops lofi_ops = { 2292 DEVO_REV, /* devo_rev, */ 2293 0, /* refcnt */ 2294 lofi_info, /* info */ 2295 nulldev, /* identify */ 2296 nulldev, /* probe */ 2297 lofi_attach, /* attach */ 2298 lofi_detach, /* detach */ 2299 nodev, /* reset */ 2300 &lofi_cb_ops, /* driver operations */ 2301 NULL, /* no bus operations */ 2302 NULL, /* power */ 2303 ddi_quiesce_not_needed, /* quiesce */ 2304 }; 2305 2306 static struct modldrv modldrv = { 2307 &mod_driverops, 2308 "loopback file driver", 2309 &lofi_ops, 2310 }; 2311 2312 static struct modlinkage modlinkage = { 2313 MODREV_1, 2314 &modldrv, 2315 NULL 2316 }; 2317 2318 int 2319 _init(void) 2320 { 2321 int error; 2322 2323 error = ddi_soft_state_init(&lofi_statep, 2324 sizeof (struct lofi_state), 0); 2325 if (error) 2326 return (error); 2327 2328 mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL); 2329 error = mod_install(&modlinkage); 2330 if (error) { 2331 mutex_destroy(&lofi_lock); 2332 ddi_soft_state_fini(&lofi_statep); 2333 } 2334 2335 return (error); 2336 } 2337 2338 int 2339 _fini(void) 2340 { 2341 int error; 2342 2343 if (lofi_busy()) 2344 return (EBUSY); 2345 2346 error = mod_remove(&modlinkage); 2347 if (error) 2348 return (error); 2349 2350 mutex_destroy(&lofi_lock); 2351 ddi_soft_state_fini(&lofi_statep); 2352 2353 return (error); 2354 } 2355 2356 int 2357 _info(struct modinfo *modinfop) 2358 { 2359 return (mod_info(&modlinkage, modinfop)); 2360 } 2361