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