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