Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/lofi.c
+++ new/usr/src/uts/common/io/lofi.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 *
24 24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 25 * Copyright (c) 2016 Andrey Sokolov
26 26 * Copyright 2016 Toomas Soome <tsoome@me.com>
27 27 */
28 28
29 29 /*
30 30 * lofi (loopback file) driver - allows you to attach a file to a device,
31 31 * which can then be accessed through that device. The simple model is that
32 32 * you tell lofi to open a file, and then use the block device you get as
33 33 * you would any block device. lofi translates access to the block device
34 34 * into I/O on the underlying file. This is mostly useful for
35 35 * mounting images of filesystems.
36 36 *
37 37 * lofi is controlled through /dev/lofictl - this is the only device exported
38 38 * during attach, and is instance number 0. lofiadm communicates with lofi
39 39 * through ioctls on this device. When a file is attached to lofi, block and
40 40 * character devices are exported in /dev/lofi and /dev/rlofi. These devices
41 41 * are identified by lofi instance number, and the instance number is also used
42 42 * as the name in /dev/lofi.
43 43 *
44 44 * Virtual disks, or, labeled lofi, implements virtual disk support to
45 45 * support partition table and related tools. Such mappings will cause
46 46 * block and character devices to be exported in /dev/dsk and /dev/rdsk
47 47 * directories.
48 48 *
49 49 * To support virtual disks, the instance number space is divided to two
50 50 * parts, upper part for instance number and lower part for minor number
51 51 * space to identify partitions and slices. The virtual disk support is
52 52 * implemented by stacking cmlb module. For virtual disks, the partition
53 53 * related ioctl calls are routed to cmlb module. Compression and encryption
54 54 * is not supported for virtual disks.
55 55 *
56 56 * Mapped devices are tracked with state structures handled with
57 57 * ddi_soft_state(9F) for simplicity.
58 58 *
59 59 * A file attached to lofi is opened when attached and not closed until
60 60 * explicitly detached from lofi. This seems more sensible than deferring
61 61 * the open until the /dev/lofi device is opened, for a number of reasons.
62 62 * One is that any failure is likely to be noticed by the person (or script)
63 63 * running lofiadm. Another is that it would be a security problem if the
64 64 * file was replaced by another one after being added but before being opened.
65 65 *
66 66 * The only hard part about lofi is the ioctls. In order to support things
67 67 * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
68 68 * So it has to fake disk geometry and partition information. More may need
69 69 * to be faked if your favorite utility doesn't work and you think it should
70 70 * (fdformat doesn't work because it really wants to know the type of floppy
71 71 * controller to talk to, and that didn't seem easy to fake. Or possibly even
72 72 * necessary, since we have mkfs_pcfs now).
73 73 *
74 74 * Normally, a lofi device cannot be detached if it is open (i.e. busy). To
75 75 * support simulation of hotplug events, an optional force flag is provided.
76 76 * If a lofi device is open when a force detach is requested, then the
77 77 * underlying file is closed and any subsequent operations return EIO. When the
78 78 * device is closed for the last time, it will be cleaned up at that time. In
79 79 * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
80 80 * detached but not removed.
81 81 *
82 82 * Known problems:
83 83 *
84 84 * UFS logging. Mounting a UFS filesystem image "logging"
85 85 * works for basic copy testing but wedges during a build of ON through
86 86 * that image. Some deadlock in lufs holding the log mutex and then
87 87 * getting stuck on a buf. So for now, don't do that.
88 88 *
89 89 * Direct I/O. Since the filesystem data is being cached in the buffer
90 90 * cache, _and_ again in the underlying filesystem, it's tempting to
91 91 * enable direct I/O on the underlying file. Don't, because that deadlocks.
92 92 * I think to fix the cache-twice problem we might need filesystem support.
93 93 *
94 94 * Interesting things to do:
95 95 *
96 96 * Allow multiple files for each device. A poor-man's metadisk, basically.
97 97 *
98 98 * Pass-through ioctls on block devices. You can (though it's not
99 99 * documented), give lofi a block device as a file name. Then we shouldn't
100 100 * need to fake a geometry, however, it may be relevant if you're replacing
101 101 * metadisk, or using lofi to get crypto.
102 102 * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
103 103 * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
104 104 * In fact this even makes sense if you have lofi "above" metadisk.
105 105 *
106 106 * Encryption:
107 107 * Each lofi device can have its own symmetric key and cipher.
108 108 * They are passed to us by lofiadm(1m) in the correct format for use
109 109 * with the misc/kcf crypto_* routines.
110 110 *
111 111 * Each block has its own IV, that is calculated in lofi_blk_mech(), based
112 112 * on the "master" key held in the lsp and the block number of the buffer.
113 113 */
114 114
115 115 #include <sys/types.h>
116 116 #include <netinet/in.h>
117 117 #include <sys/sysmacros.h>
118 118 #include <sys/uio.h>
119 119 #include <sys/kmem.h>
120 120 #include <sys/cred.h>
121 121 #include <sys/mman.h>
122 122 #include <sys/errno.h>
123 123 #include <sys/aio_req.h>
124 124 #include <sys/stat.h>
125 125 #include <sys/file.h>
126 126 #include <sys/modctl.h>
127 127 #include <sys/conf.h>
128 128 #include <sys/debug.h>
129 129 #include <sys/vnode.h>
130 130 #include <sys/lofi.h>
131 131 #include <sys/fcntl.h>
132 132 #include <sys/pathname.h>
133 133 #include <sys/filio.h>
134 134 #include <sys/fdio.h>
135 135 #include <sys/open.h>
136 136 #include <sys/disp.h>
137 137 #include <vm/seg_map.h>
138 138 #include <sys/ddi.h>
139 139 #include <sys/sunddi.h>
140 140 #include <sys/zmod.h>
141 141 #include <sys/id_space.h>
142 142 #include <sys/mkdev.h>
143 143 #include <sys/crypto/common.h>
144 144 #include <sys/crypto/api.h>
145 145 #include <sys/rctl.h>
146 146 #include <sys/vtoc.h>
147 147 #include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
148 148 #include <sys/scsi/impl/uscsi.h>
149 149 #include <sys/sysevent/dev.h>
150 150 #include <LzmaDec.h>
151 151
152 152 #define NBLOCKS_PROP_NAME "Nblocks"
153 153 #define SIZE_PROP_NAME "Size"
154 154 #define ZONE_PROP_NAME "zone"
155 155
156 156 #define SETUP_C_DATA(cd, buf, len) \
157 157 (cd).cd_format = CRYPTO_DATA_RAW; \
158 158 (cd).cd_offset = 0; \
159 159 (cd).cd_miscdata = NULL; \
160 160 (cd).cd_length = (len); \
161 161 (cd).cd_raw.iov_base = (buf); \
162 162 (cd).cd_raw.iov_len = (len);
163 163
164 164 #define UIO_CHECK(uio) \
165 165 if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
166 166 ((uio)->uio_resid % DEV_BSIZE) != 0) { \
167 167 return (EINVAL); \
168 168 }
169 169
170 170 #define DEVFS_CHANNEL "devfsadm_event_channel"
171 171 #define LOFI_TIMEOUT 30
172 172 static evchan_t *lofi_chan;
173 173 static kmutex_t lofi_chan_lock;
174 174 static kcondvar_t lofi_chan_cv;
175 175 static nvlist_t *lofi_devlink_cache;
176 176
177 177 static void *lofi_statep;
178 178 static kmutex_t lofi_lock; /* state lock */
179 179 static id_space_t *lofi_id; /* lofi ID values */
180 180 static list_t lofi_list;
181 181 static zone_key_t lofi_zone_key;
182 182
183 183 /*
184 184 * Because lofi_taskq_nthreads limits the actual swamping of the device, the
185 185 * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
186 186 * high. If we want to be assured that the underlying device is always busy,
187 187 * we must be sure that the number of bytes enqueued when the number of
188 188 * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
189 189 * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should
190 190 * set maxalloc to be the maximum throughput (in bytes per second) of the
191 191 * underlying device divided by the minimum I/O size. We assume a realistic
192 192 * maximum throughput of one hundred megabytes per second; we set maxalloc on
193 193 * the lofi task queue to be 104857600 divided by DEV_BSIZE.
194 194 */
195 195 static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
196 196 static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */
197 197
198 198 const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
199 199
200 200 /*
201 201 * To avoid decompressing data in a compressed segment multiple times
202 202 * when accessing small parts of a segment's data, we cache and reuse
203 203 * the uncompressed segment's data.
204 204 *
205 205 * A single cached segment is sufficient to avoid lots of duplicate
206 206 * segment decompress operations. A small cache size also reduces the
207 207 * memory footprint.
208 208 *
209 209 * lofi_max_comp_cache is the maximum number of decompressed data segments
210 210 * cached for each compressed lofi image. It can be set to 0 to disable
211 211 * caching.
212 212 */
213 213
214 214 uint32_t lofi_max_comp_cache = 1;
215 215
216 216 static int gzip_decompress(void *src, size_t srclen, void *dst,
217 217 size_t *destlen, int level);
218 218
219 219 static int lzma_decompress(void *src, size_t srclen, void *dst,
220 220 size_t *dstlen, int level);
221 221
222 222 lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
223 223 {gzip_decompress, NULL, 6, "gzip"}, /* default */
224 224 {gzip_decompress, NULL, 6, "gzip-6"},
225 225 {gzip_decompress, NULL, 9, "gzip-9"},
226 226 {lzma_decompress, NULL, 0, "lzma"}
227 227 };
228 228
229 229 static void lofi_strategy_task(void *);
230 230 static int lofi_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t,
231 231 size_t, void *);
232 232 static int lofi_tg_getinfo(dev_info_t *, int, void *, void *);
233 233
234 234 struct cmlb_tg_ops lofi_tg_ops = {
235 235 TG_DK_OPS_VERSION_1,
236 236 lofi_tg_rdwr,
237 237 lofi_tg_getinfo
238 238 };
239 239
240 240 /*ARGSUSED*/
241 241 static void
242 242 *SzAlloc(void *p, size_t size)
243 243 {
244 244 return (kmem_alloc(size, KM_SLEEP));
245 245 }
246 246
247 247 /*ARGSUSED*/
248 248 static void
249 249 SzFree(void *p, void *address, size_t size)
250 250 {
251 251 kmem_free(address, size);
252 252 }
253 253
254 254 static ISzAlloc g_Alloc = { SzAlloc, SzFree };
255 255
256 256 /*
257 257 * Free data referenced by the linked list of cached uncompressed
258 258 * segments.
259 259 */
260 260 static void
261 261 lofi_free_comp_cache(struct lofi_state *lsp)
262 262 {
263 263 struct lofi_comp_cache *lc;
264 264
265 265 while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
266 266 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
267 267 kmem_free(lc, sizeof (struct lofi_comp_cache));
268 268 lsp->ls_comp_cache_count--;
269 269 }
270 270 ASSERT(lsp->ls_comp_cache_count == 0);
271 271 }
272 272
273 273 static int
274 274 is_opened(struct lofi_state *lsp)
275 275 {
276 276 int i;
277 277 boolean_t last = B_TRUE;
278 278
279 279 ASSERT(MUTEX_HELD(&lofi_lock));
280 280 for (i = 0; i < LOFI_PART_MAX; i++) {
281 281 if (lsp->ls_open_lyr[i]) {
282 282 last = B_FALSE;
283 283 break;
284 284 }
285 285 }
286 286
287 287 for (i = 0; last && (i < OTYP_LYR); i++) {
288 288 if (lsp->ls_open_reg[i]) {
289 289 last = B_FALSE;
290 290 }
291 291 }
292 292
293 293 return (!last);
294 294 }
295 295
296 296 static void
297 297 lofi_free_crypto(struct lofi_state *lsp)
298 298 {
299 299 ASSERT(MUTEX_HELD(&lofi_lock));
300 300
301 301 if (lsp->ls_crypto_enabled) {
302 302 /*
303 303 * Clean up the crypto state so that it doesn't hang around
304 304 * in memory after we are done with it.
305 305 */
306 306 if (lsp->ls_key.ck_data != NULL) {
307 307 bzero(lsp->ls_key.ck_data,
308 308 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
309 309 kmem_free(lsp->ls_key.ck_data,
310 310 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
311 311 lsp->ls_key.ck_data = NULL;
312 312 lsp->ls_key.ck_length = 0;
313 313 }
314 314
315 315 if (lsp->ls_mech.cm_param != NULL) {
316 316 kmem_free(lsp->ls_mech.cm_param,
317 317 lsp->ls_mech.cm_param_len);
318 318 lsp->ls_mech.cm_param = NULL;
319 319 lsp->ls_mech.cm_param_len = 0;
320 320 }
321 321
322 322 if (lsp->ls_iv_mech.cm_param != NULL) {
323 323 kmem_free(lsp->ls_iv_mech.cm_param,
324 324 lsp->ls_iv_mech.cm_param_len);
325 325 lsp->ls_iv_mech.cm_param = NULL;
326 326 lsp->ls_iv_mech.cm_param_len = 0;
327 327 }
328 328
329 329 mutex_destroy(&lsp->ls_crypto_lock);
330 330 }
331 331 }
332 332
333 333 /* ARGSUSED */
334 334 static int
335 335 lofi_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
336 336 size_t length, void *tg_cookie)
337 337 {
338 338 struct lofi_state *lsp;
339 339 buf_t *bp;
340 340 int instance;
341 341 int rv = 0;
342 342
343 343 instance = ddi_get_instance(dip);
344 344 if (instance == 0) /* control node does not have disk */
345 345 return (ENXIO);
346 346
347 347 lsp = ddi_get_soft_state(lofi_statep, instance);
348 348
349 349 if (lsp == NULL)
350 350 return (ENXIO);
351 351
352 352 if (cmd != TG_READ && cmd != TG_WRITE)
353 353 return (EINVAL);
354 354
355 355 /*
356 356 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
357 357 */
358 358 mutex_enter(&lsp->ls_vp_lock);
359 359 while (lsp->ls_vp_ready == B_FALSE)
360 360 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
361 361 mutex_exit(&lsp->ls_vp_lock);
362 362
363 363 if (P2PHASE(length, (1U << lsp->ls_lbshift)) != 0) {
364 364 /* We can only transfer whole blocks at a time! */
365 365 return (EINVAL);
366 366 }
367 367
368 368 bp = getrbuf(KM_SLEEP);
369 369
370 370 if (cmd == TG_READ) {
371 371 bp->b_flags = B_READ;
372 372 } else {
373 373 if (lsp->ls_readonly == B_TRUE) {
374 374 freerbuf(bp);
375 375 return (EROFS);
376 376 }
377 377 bp->b_flags = B_WRITE;
378 378 }
379 379
380 380 bp->b_un.b_addr = bufaddr;
381 381 bp->b_bcount = length;
382 382 bp->b_lblkno = start;
383 383 bp->b_private = NULL;
384 384 bp->b_edev = lsp->ls_dev;
385 385
386 386 if (lsp->ls_kstat) {
387 387 mutex_enter(lsp->ls_kstat->ks_lock);
388 388 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
389 389 mutex_exit(lsp->ls_kstat->ks_lock);
390 390 }
391 391 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
392 392 (void) biowait(bp);
393 393
394 394 rv = geterror(bp);
395 395 freerbuf(bp);
396 396 return (rv);
397 397 }
398 398
399 399 /*
400 400 * Get device geometry info for cmlb.
401 401 *
402 402 * We have mapped disk image as virtual block device and have to report
403 403 * physical/virtual geometry to cmlb.
404 404 *
405 405 * So we have two principal cases:
406 406 * 1. Uninitialised image without any existing labels,
407 407 * for this case we fabricate the data based on mapped image.
408 408 * 2. Image with existing label information.
409 409 * Since we have no information how the image was created (it may be
410 410 * dump from some physical device), we need to rely on label information
411 411 * from image, or we get "corrupted label" errors.
412 412 * NOTE: label can be MBR, MBR+SMI, GPT
413 413 */
414 414 static int
415 415 lofi_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
416 416 {
417 417 struct lofi_state *lsp;
418 418 int instance;
419 419 int ashift;
420 420
421 421 _NOTE(ARGUNUSED(tg_cookie));
422 422 instance = ddi_get_instance(dip);
423 423 if (instance == 0) /* control device has no storage */
424 424 return (ENXIO);
425 425
426 426 lsp = ddi_get_soft_state(lofi_statep, instance);
427 427
428 428 if (lsp == NULL)
429 429 return (ENXIO);
430 430
431 431 /*
432 432 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
433 433 *
434 434 * When mapping is created, new lofi instance is created and
435 435 * lofi_attach() will call cmlb_attach() as part of the procedure
436 436 * to set the mapping up. This chain of events will happen in
437 437 * the same thread.
438 438 * Since cmlb_attach() will call lofi_tg_getinfo to get
439 439 * capacity, we return error on that call if cookie is set,
440 440 * otherwise lofi_attach will be stuck as the mapping is not yet
441 441 * finalized and lofi is not yet ready.
442 442 * Note, such error is not fatal for cmlb, as the label setup
443 443 * will be finalized when cmlb_validate() is called.
444 444 */
445 445 mutex_enter(&lsp->ls_vp_lock);
446 446 if (tg_cookie != NULL && lsp->ls_vp_ready == B_FALSE) {
447 447 mutex_exit(&lsp->ls_vp_lock);
448 448 return (ENXIO);
449 449 }
450 450 while (lsp->ls_vp_ready == B_FALSE)
451 451 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
452 452 mutex_exit(&lsp->ls_vp_lock);
453 453
454 454 ashift = lsp->ls_lbshift;
455 455
456 456 switch (cmd) {
457 457 case TG_GETPHYGEOM: {
458 458 cmlb_geom_t *geomp = arg;
459 459
460 460 geomp->g_capacity =
461 461 (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
462 462 geomp->g_nsect = lsp->ls_dkg.dkg_nsect;
463 463 geomp->g_nhead = lsp->ls_dkg.dkg_nhead;
464 464 geomp->g_acyl = lsp->ls_dkg.dkg_acyl;
465 465 geomp->g_ncyl = lsp->ls_dkg.dkg_ncyl;
466 466 geomp->g_secsize = (1U << ashift);
467 467 geomp->g_intrlv = lsp->ls_dkg.dkg_intrlv;
468 468 geomp->g_rpm = lsp->ls_dkg.dkg_rpm;
469 469 return (0);
470 470 }
471 471
472 472 case TG_GETCAPACITY:
473 473 *(diskaddr_t *)arg =
474 474 (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
475 475 return (0);
476 476
477 477 case TG_GETBLOCKSIZE:
478 478 *(uint32_t *)arg = (1U << ashift);
479 479 return (0);
480 480
481 481 case TG_GETATTR: {
482 482 tg_attribute_t *tgattr = arg;
483 483
484 484 tgattr->media_is_writable = !lsp->ls_readonly;
485 485 tgattr->media_is_solid_state = B_FALSE;
486 486 return (0);
487 487 }
488 488
489 489 default:
490 490 return (EINVAL);
491 491 }
492 492 }
493 493
494 494 static void
495 495 lofi_destroy(struct lofi_state *lsp, cred_t *credp)
496 496 {
497 497 int id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
498 498 int i;
499 499
500 500 ASSERT(MUTEX_HELD(&lofi_lock));
501 501
502 502 list_remove(&lofi_list, lsp);
503 503
504 504 lofi_free_crypto(lsp);
505 505
506 506 /*
507 507 * Free pre-allocated compressed buffers
508 508 */
509 509 if (lsp->ls_comp_bufs != NULL) {
510 510 for (i = 0; i < lofi_taskq_nthreads; i++) {
511 511 if (lsp->ls_comp_bufs[i].bufsize > 0)
512 512 kmem_free(lsp->ls_comp_bufs[i].buf,
513 513 lsp->ls_comp_bufs[i].bufsize);
514 514 }
515 515 kmem_free(lsp->ls_comp_bufs,
516 516 sizeof (struct compbuf) * lofi_taskq_nthreads);
517 517 }
518 518
519 519 if (lsp->ls_vp != NULL) {
520 520 (void) VOP_PUTPAGE(lsp->ls_vp, 0, 0, B_INVAL, credp, NULL);
521 521 (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
522 522 1, 0, credp, NULL);
523 523 VN_RELE(lsp->ls_vp);
524 524 }
525 525 if (lsp->ls_stacked_vp != lsp->ls_vp)
526 526 VN_RELE(lsp->ls_stacked_vp);
527 527
528 528 if (lsp->ls_taskq != NULL)
529 529 taskq_destroy(lsp->ls_taskq);
530 530
531 531 if (lsp->ls_kstat != NULL)
532 532 kstat_delete(lsp->ls_kstat);
533 533
534 534 /*
535 535 * Free cached decompressed segment data
536 536 */
537 537 lofi_free_comp_cache(lsp);
538 538 list_destroy(&lsp->ls_comp_cache);
539 539
540 540 if (lsp->ls_uncomp_seg_sz > 0) {
541 541 kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
542 542 lsp->ls_uncomp_seg_sz = 0;
543 543 }
544 544
545 545 rctl_decr_lofi(lsp->ls_zone.zref_zone, 1);
546 546 zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
547 547
548 548 mutex_destroy(&lsp->ls_comp_cache_lock);
549 549 mutex_destroy(&lsp->ls_comp_bufs_lock);
550 550 mutex_destroy(&lsp->ls_kstat_lock);
551 551 mutex_destroy(&lsp->ls_vp_lock);
552 552 cv_destroy(&lsp->ls_vp_cv);
553 553 lsp->ls_vp_ready = B_FALSE;
554 554
555 555 ASSERT(ddi_get_soft_state(lofi_statep, id) == lsp);
556 556 (void) ndi_devi_offline(lsp->ls_dip, NDI_DEVI_REMOVE);
557 557 id_free(lofi_id, id);
558 558 }
559 559
560 560 static void
561 561 lofi_free_dev(struct lofi_state *lsp)
562 562 {
563 563 ASSERT(MUTEX_HELD(&lofi_lock));
564 564
565 565 if (lsp->ls_cmlbhandle != NULL) {
566 566 cmlb_invalidate(lsp->ls_cmlbhandle, 0);
567 567 cmlb_detach(lsp->ls_cmlbhandle, 0);
568 568 cmlb_free_handle(&lsp->ls_cmlbhandle);
569 569 lsp->ls_cmlbhandle = NULL;
570 570 }
571 571 (void) ddi_prop_remove_all(lsp->ls_dip);
572 572 ddi_remove_minor_node(lsp->ls_dip, NULL);
573 573 }
574 574
575 575 /*ARGSUSED*/
576 576 static void
577 577 lofi_zone_shutdown(zoneid_t zoneid, void *arg)
578 578 {
579 579 struct lofi_state *lsp;
580 580 struct lofi_state *next;
581 581
582 582 mutex_enter(&lofi_lock);
583 583
584 584 for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) {
585 585
586 586 /* lofi_destroy() frees lsp */
587 587 next = list_next(&lofi_list, lsp);
588 588
589 589 if (lsp->ls_zone.zref_zone->zone_id != zoneid)
590 590 continue;
591 591
592 592 /*
593 593 * No in-zone processes are running, but something has this
594 594 * open. It's either a global zone process, or a lofi
595 595 * mount. In either case we set ls_cleanup so the last
596 596 * user destroys the device.
597 597 */
598 598 if (is_opened(lsp)) {
599 599 lsp->ls_cleanup = 1;
600 600 } else {
601 601 lofi_free_dev(lsp);
602 602 lofi_destroy(lsp, kcred);
603 603 }
604 604 }
605 605
606 606 mutex_exit(&lofi_lock);
607 607 }
608 608
609 609 /*ARGSUSED*/
610 610 static int
611 611 lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
612 612 {
613 613 int id;
614 614 minor_t part;
615 615 uint64_t mask;
616 616 diskaddr_t nblks;
617 617 diskaddr_t lba;
618 618 boolean_t ndelay;
619 619
620 620 struct lofi_state *lsp;
621 621
622 622 if (otyp >= OTYPCNT)
623 623 return (EINVAL);
624 624
625 625 ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
626 626
627 627 /*
628 628 * lofiadm -a /dev/lofi/1 gets us here.
629 629 */
630 630 if (mutex_owner(&lofi_lock) == curthread)
631 631 return (EINVAL);
632 632
633 633 mutex_enter(&lofi_lock);
634 634
635 635 id = LOFI_MINOR2ID(getminor(*devp));
636 636 part = LOFI_PART(getminor(*devp));
637 637 mask = (1U << part);
638 638
639 639 /* master control device */
640 640 if (id == 0) {
641 641 mutex_exit(&lofi_lock);
642 642 return (0);
643 643 }
644 644
645 645 /* otherwise, the mapping should already exist */
646 646 lsp = ddi_get_soft_state(lofi_statep, id);
647 647 if (lsp == NULL) {
648 648 mutex_exit(&lofi_lock);
649 649 return (EINVAL);
650 650 }
651 651
652 652 if (lsp->ls_vp == NULL) {
653 653 mutex_exit(&lofi_lock);
654 654 return (ENXIO);
655 655 }
656 656
657 657 if (lsp->ls_readonly && (flag & FWRITE)) {
658 658 mutex_exit(&lofi_lock);
659 659 return (EROFS);
660 660 }
661 661
662 662 if ((lsp->ls_open_excl) & (mask)) {
663 663 mutex_exit(&lofi_lock);
664 664 return (EBUSY);
665 665 }
666 666
667 667 if (flag & FEXCL) {
668 668 if (lsp->ls_open_lyr[part]) {
669 669 mutex_exit(&lofi_lock);
670 670 return (EBUSY);
671 671 }
672 672 for (int i = 0; i < OTYP_LYR; i++) {
673 673 if (lsp->ls_open_reg[i] & mask) {
674 674 mutex_exit(&lofi_lock);
675 675 return (EBUSY);
676 676 }
677 677 }
678 678 }
679 679
680 680 if (lsp->ls_cmlbhandle != NULL) {
681 681 if (cmlb_validate(lsp->ls_cmlbhandle, 0, 0) != 0) {
682 682 /*
683 683 * non-blocking opens are allowed to succeed to
684 684 * support format and fdisk to create partitioning.
685 685 */
686 686 if (!ndelay) {
687 687 mutex_exit(&lofi_lock);
688 688 return (ENXIO);
689 689 }
690 690 } else if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &nblks, &lba,
691 691 NULL, NULL, 0) == 0) {
692 692 if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
693 693 mutex_exit(&lofi_lock);
694 694 return (ENXIO);
695 695 }
696 696 } else if (!ndelay) {
697 697 mutex_exit(&lofi_lock);
698 698 return (ENXIO);
699 699 }
700 700 }
701 701
702 702 if (otyp == OTYP_LYR) {
703 703 lsp->ls_open_lyr[part]++;
704 704 } else {
705 705 lsp->ls_open_reg[otyp] |= mask;
706 706 }
707 707 if (flag & FEXCL) {
708 708 lsp->ls_open_excl |= mask;
709 709 }
710 710
711 711 mutex_exit(&lofi_lock);
712 712 return (0);
713 713 }
714 714
715 715 /*ARGSUSED*/
716 716 static int
717 717 lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
718 718 {
719 719 minor_t part;
720 720 int id;
721 721 uint64_t mask;
722 722 struct lofi_state *lsp;
723 723
724 724 id = LOFI_MINOR2ID(getminor(dev));
725 725 part = LOFI_PART(getminor(dev));
726 726 mask = (1U << part);
727 727
728 728 mutex_enter(&lofi_lock);
729 729 lsp = ddi_get_soft_state(lofi_statep, id);
730 730 if (lsp == NULL) {
731 731 mutex_exit(&lofi_lock);
732 732 return (EINVAL);
733 733 }
734 734
735 735 if (id == 0) {
736 736 mutex_exit(&lofi_lock);
737 737 return (0);
738 738 }
739 739
740 740 if (lsp->ls_open_excl & mask)
741 741 lsp->ls_open_excl &= ~mask;
742 742
743 743 if (otyp == OTYP_LYR) {
744 744 lsp->ls_open_lyr[part]--;
745 745 } else {
746 746 lsp->ls_open_reg[otyp] &= ~mask;
747 747 }
748 748
749 749 /*
750 750 * If we forcibly closed the underlying device (li_force), or
751 751 * asked for cleanup (li_cleanup), finish up if we're the last
752 752 * out of the door.
753 753 */
754 754 if (!is_opened(lsp) && (lsp->ls_cleanup || lsp->ls_vp == NULL)) {
755 755 lofi_free_dev(lsp);
756 756 lofi_destroy(lsp, credp);
757 757 }
758 758
759 759 mutex_exit(&lofi_lock);
760 760 return (0);
761 761 }
762 762
763 763 /*
764 764 * Sets the mechanism's initialization vector (IV) if one is needed.
765 765 * The IV is computed from the data block number. lsp->ls_mech is
766 766 * altered so that:
767 767 * lsp->ls_mech.cm_param_len is set to the IV len.
768 768 * lsp->ls_mech.cm_param is set to the IV.
769 769 */
770 770 static int
771 771 lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
772 772 {
773 773 int ret;
774 774 crypto_data_t cdata;
775 775 char *iv;
776 776 size_t iv_len;
777 777 size_t min;
778 778 void *data;
779 779 size_t datasz;
780 780
781 781 ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock));
782 782
783 783 if (lsp == NULL)
784 784 return (CRYPTO_DEVICE_ERROR);
785 785
786 786 /* lsp->ls_mech.cm_param{_len} has already been set for static iv */
787 787 if (lsp->ls_iv_type == IVM_NONE) {
788 788 return (CRYPTO_SUCCESS);
789 789 }
790 790
791 791 /*
792 792 * if kmem already alloced from previous call and it's the same size
793 793 * we need now, just recycle it; allocate new kmem only if we have to
794 794 */
795 795 if (lsp->ls_mech.cm_param == NULL ||
796 796 lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
797 797 iv_len = lsp->ls_iv_len;
798 798 iv = kmem_zalloc(iv_len, KM_SLEEP);
799 799 } else {
800 800 iv_len = lsp->ls_mech.cm_param_len;
801 801 iv = lsp->ls_mech.cm_param;
802 802 bzero(iv, iv_len);
803 803 }
804 804
805 805 switch (lsp->ls_iv_type) {
806 806 case IVM_ENC_BLKNO:
807 807 /* iv is not static, lblkno changes each time */
808 808 data = &lblkno;
809 809 datasz = sizeof (lblkno);
810 810 break;
811 811 default:
812 812 data = 0;
813 813 datasz = 0;
814 814 break;
815 815 }
816 816
817 817 /*
818 818 * write blkno into the iv buffer padded on the left in case
819 819 * blkno ever grows bigger than its current longlong_t size
820 820 * or a variation other than blkno is used for the iv data
821 821 */
822 822 min = MIN(datasz, iv_len);
823 823 bcopy(data, iv + (iv_len - min), min);
824 824
825 825 /* encrypt the data in-place to get the IV */
826 826 SETUP_C_DATA(cdata, iv, iv_len);
827 827
828 828 ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
829 829 NULL, NULL, NULL);
830 830 if (ret != CRYPTO_SUCCESS) {
831 831 cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
832 832 lblkno, ret);
833 833 if (lsp->ls_mech.cm_param != iv)
834 834 kmem_free(iv, iv_len);
835 835
836 836 return (ret);
837 837 }
838 838
839 839 /* clean up the iv from the last computation */
840 840 if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
841 841 kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
842 842
843 843 lsp->ls_mech.cm_param_len = iv_len;
844 844 lsp->ls_mech.cm_param = iv;
845 845
846 846 return (CRYPTO_SUCCESS);
847 847 }
848 848
849 849 /*
850 850 * Performs encryption and decryption of a chunk of data of size "len",
851 851 * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of
852 852 * DEV_BSIZE.
853 853 */
854 854 static int
855 855 lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
856 856 caddr_t ciphertext, size_t len, boolean_t op_encrypt)
857 857 {
858 858 crypto_data_t cdata;
859 859 crypto_data_t wdata;
860 860 int ret;
861 861 longlong_t lblkno = bp->b_lblkno;
862 862
863 863 mutex_enter(&lsp->ls_crypto_lock);
864 864
865 865 /*
866 866 * though we could encrypt/decrypt entire "len" chunk of data, we need
867 867 * to break it into DEV_BSIZE pieces to capture blkno incrementing
868 868 */
869 869 SETUP_C_DATA(cdata, plaintext, len);
870 870 cdata.cd_length = DEV_BSIZE;
871 871 if (ciphertext != NULL) { /* not in-place crypto */
872 872 SETUP_C_DATA(wdata, ciphertext, len);
873 873 wdata.cd_length = DEV_BSIZE;
874 874 }
875 875
876 876 do {
877 877 ret = lofi_blk_mech(lsp, lblkno);
878 878 if (ret != CRYPTO_SUCCESS)
879 879 continue;
880 880
881 881 if (op_encrypt) {
882 882 ret = crypto_encrypt(&lsp->ls_mech, &cdata,
883 883 &lsp->ls_key, NULL,
884 884 ((ciphertext != NULL) ? &wdata : NULL), NULL);
885 885 } else {
886 886 ret = crypto_decrypt(&lsp->ls_mech, &cdata,
887 887 &lsp->ls_key, NULL,
888 888 ((ciphertext != NULL) ? &wdata : NULL), NULL);
889 889 }
890 890
891 891 cdata.cd_offset += DEV_BSIZE;
892 892 if (ciphertext != NULL)
893 893 wdata.cd_offset += DEV_BSIZE;
894 894 lblkno++;
895 895 } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
896 896
897 897 mutex_exit(&lsp->ls_crypto_lock);
898 898
899 899 if (ret != CRYPTO_SUCCESS) {
900 900 cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)",
901 901 op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
902 902 lblkno, ret);
903 903 }
904 904
905 905 return (ret);
906 906 }
907 907
908 908 #define RDWR_RAW 1
909 909 #define RDWR_BCOPY 2
910 910
911 911 static int
912 912 lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
913 913 struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
914 914 {
915 915 ssize_t resid;
916 916 int isread;
917 917 int error;
918 918
919 919 /*
920 920 * Handles reads/writes for both plain and encrypted lofi
921 921 * Note: offset is already shifted by lsp->ls_crypto_offset
922 922 * when it gets here.
923 923 */
924 924
925 925 isread = bp->b_flags & B_READ;
926 926 if (isread) {
927 927 if (method == RDWR_BCOPY) {
928 928 /* DO NOT update bp->b_resid for bcopy */
929 929 bcopy(bcopy_locn, bufaddr, len);
930 930 error = 0;
931 931 } else { /* RDWR_RAW */
932 932 error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
933 933 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
934 934 &resid);
935 935 bp->b_resid = resid;
936 936 }
937 937 if (lsp->ls_crypto_enabled && error == 0) {
938 938 if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
939 939 B_FALSE) != CRYPTO_SUCCESS) {
940 940 /*
941 941 * XXX: original code didn't set residual
942 942 * back to len because no error was expected
943 943 * from bcopy() if encryption is not enabled
944 944 */
945 945 if (method != RDWR_BCOPY)
946 946 bp->b_resid = len;
947 947 error = EIO;
948 948 }
949 949 }
950 950 return (error);
951 951 } else {
952 952 void *iobuf = bufaddr;
953 953
954 954 if (lsp->ls_crypto_enabled) {
955 955 /* don't do in-place crypto to keep bufaddr intact */
956 956 iobuf = kmem_alloc(len, KM_SLEEP);
957 957 if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
958 958 B_TRUE) != CRYPTO_SUCCESS) {
959 959 kmem_free(iobuf, len);
960 960 if (method != RDWR_BCOPY)
961 961 bp->b_resid = len;
962 962 return (EIO);
963 963 }
964 964 }
965 965 if (method == RDWR_BCOPY) {
966 966 /* DO NOT update bp->b_resid for bcopy */
967 967 bcopy(iobuf, bcopy_locn, len);
968 968 error = 0;
969 969 } else { /* RDWR_RAW */
970 970 error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
971 971 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
972 972 &resid);
973 973 bp->b_resid = resid;
974 974 }
975 975 if (lsp->ls_crypto_enabled) {
976 976 kmem_free(iobuf, len);
977 977 }
978 978 return (error);
979 979 }
980 980 }
981 981
982 982 static int
983 983 lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
984 984 struct lofi_state *lsp)
985 985 {
986 986 int error;
987 987 offset_t alignedoffset, mapoffset;
988 988 size_t xfersize;
989 989 int isread;
990 990 int smflags;
991 991 caddr_t mapaddr;
992 992 size_t len;
993 993 enum seg_rw srw;
994 994 int save_error;
995 995
996 996 /*
997 997 * Note: offset is already shifted by lsp->ls_crypto_offset
998 998 * when it gets here.
999 999 */
1000 1000 if (lsp->ls_crypto_enabled)
1001 1001 ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
1002 1002
1003 1003 /*
1004 1004 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
1005 1005 * an 8K boundary, but the buf transfer address may not be
1006 1006 * aligned on more than a 512-byte boundary (we don't enforce
1007 1007 * that even though we could). This matters since the initial
1008 1008 * part of the transfer may not start at offset 0 within the
1009 1009 * segmap'd chunk. So we have to compensate for that with
1010 1010 * 'mapoffset'. Subsequent chunks always start off at the
1011 1011 * beginning, and the last is capped by b_resid
1012 1012 *
1013 1013 * Visually, where "|" represents page map boundaries:
1014 1014 * alignedoffset (mapaddr begins at this segmap boundary)
1015 1015 * | offset (from beginning of file)
1016 1016 * | | len
1017 1017 * v v v
1018 1018 * ===|====X========|====...======|========X====|====
1019 1019 * /-------------...---------------/
1020 1020 * ^ bp->b_bcount/bp->b_resid at start
1021 1021 * /----/--------/----...------/--------/
1022 1022 * ^ ^ ^ ^ ^
1023 1023 * | | | | nth xfersize (<= MAXBSIZE)
1024 1024 * | | 2nd thru n-1st xfersize (= MAXBSIZE)
1025 1025 * | 1st xfersize (<= MAXBSIZE)
1026 1026 * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
1027 1027 *
1028 1028 * Notes: "alignedoffset" is "offset" rounded down to nearest
1029 1029 * MAXBSIZE boundary. "len" is next page boundary of size
1030 1030 * PAGESIZE after "alignedoffset".
1031 1031 */
1032 1032 mapoffset = offset & MAXBOFFSET;
1033 1033 alignedoffset = offset - mapoffset;
1034 1034 bp->b_resid = bp->b_bcount;
1035 1035 isread = bp->b_flags & B_READ;
1036 1036 srw = isread ? S_READ : S_WRITE;
1037 1037 do {
1038 1038 xfersize = MIN(lsp->ls_vp_comp_size - offset,
1039 1039 MIN(MAXBSIZE - mapoffset, bp->b_resid));
1040 1040 len = roundup(mapoffset + xfersize, PAGESIZE);
1041 1041 mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
1042 1042 alignedoffset, MAXBSIZE, 1, srw);
1043 1043 /*
1044 1044 * Now fault in the pages. This lets us check
1045 1045 * for errors before we reference mapaddr and
1046 1046 * try to resolve the fault in bcopy (which would
1047 1047 * panic instead). And this can easily happen,
1048 1048 * particularly if you've lofi'd a file over NFS
1049 1049 * and someone deletes the file on the server.
1050 1050 */
1051 1051 error = segmap_fault(kas.a_hat, segkmap, mapaddr,
1052 1052 len, F_SOFTLOCK, srw);
1053 1053 if (error) {
1054 1054 (void) segmap_release(segkmap, mapaddr, 0);
1055 1055 if (FC_CODE(error) == FC_OBJERR)
1056 1056 error = FC_ERRNO(error);
1057 1057 else
1058 1058 error = EIO;
1059 1059 break;
1060 1060 }
1061 1061 /* error may be non-zero for encrypted lofi */
1062 1062 error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
1063 1063 RDWR_BCOPY, mapaddr + mapoffset);
1064 1064 if (error == 0) {
1065 1065 bp->b_resid -= xfersize;
1066 1066 bufaddr += xfersize;
1067 1067 offset += xfersize;
1068 1068 }
1069 1069 smflags = 0;
1070 1070 if (isread) {
1071 1071 smflags |= SM_FREE;
1072 1072 /*
1073 1073 * If we're reading an entire page starting
1074 1074 * at a page boundary, there's a good chance
1075 1075 * we won't need it again. Put it on the
1076 1076 * head of the freelist.
1077 1077 */
1078 1078 if (mapoffset == 0 && xfersize == MAXBSIZE)
1079 1079 smflags |= SM_DONTNEED;
1080 1080 } else {
1081 1081 /*
1082 1082 * Write back good pages, it is okay to
1083 1083 * always release asynchronous here as we'll
1084 1084 * follow with VOP_FSYNC for B_SYNC buffers.
1085 1085 */
1086 1086 if (error == 0)
1087 1087 smflags |= SM_WRITE | SM_ASYNC;
1088 1088 }
1089 1089 (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
1090 1090 len, F_SOFTUNLOCK, srw);
1091 1091 save_error = segmap_release(segkmap, mapaddr, smflags);
1092 1092 if (error == 0)
1093 1093 error = save_error;
1094 1094 /* only the first map may start partial */
1095 1095 mapoffset = 0;
1096 1096 alignedoffset += MAXBSIZE;
1097 1097 } while ((error == 0) && (bp->b_resid > 0) &&
1098 1098 (offset < lsp->ls_vp_comp_size));
1099 1099
1100 1100 return (error);
1101 1101 }
1102 1102
1103 1103 /*
1104 1104 * Check if segment seg_index is present in the decompressed segment
1105 1105 * data cache.
1106 1106 *
1107 1107 * Returns a pointer to the decompressed segment data cache entry if
1108 1108 * found, and NULL when decompressed data for this segment is not yet
1109 1109 * cached.
1110 1110 */
1111 1111 static struct lofi_comp_cache *
1112 1112 lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
1113 1113 {
1114 1114 struct lofi_comp_cache *lc;
1115 1115
1116 1116 ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
1117 1117
1118 1118 for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
1119 1119 lc = list_next(&lsp->ls_comp_cache, lc)) {
1120 1120 if (lc->lc_index == seg_index) {
1121 1121 /*
1122 1122 * Decompressed segment data was found in the
1123 1123 * cache.
1124 1124 *
1125 1125 * The cache uses an LRU replacement strategy;
1126 1126 * move the entry to head of list.
1127 1127 */
1128 1128 list_remove(&lsp->ls_comp_cache, lc);
1129 1129 list_insert_head(&lsp->ls_comp_cache, lc);
1130 1130 return (lc);
1131 1131 }
1132 1132 }
1133 1133 return (NULL);
1134 1134 }
1135 1135
1136 1136 /*
1137 1137 * Add the data for a decompressed segment at segment index
1138 1138 * seg_index to the cache of the decompressed segments.
1139 1139 *
1140 1140 * Returns a pointer to the cache element structure in case
1141 1141 * the data was added to the cache; returns NULL when the data
1142 1142 * wasn't cached.
1143 1143 */
1144 1144 static struct lofi_comp_cache *
1145 1145 lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
1146 1146 uchar_t *data)
1147 1147 {
1148 1148 struct lofi_comp_cache *lc;
1149 1149
1150 1150 ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
1151 1151
1152 1152 while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
1153 1153 lc = list_remove_tail(&lsp->ls_comp_cache);
1154 1154 ASSERT(lc != NULL);
1155 1155 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
1156 1156 kmem_free(lc, sizeof (struct lofi_comp_cache));
1157 1157 lsp->ls_comp_cache_count--;
1158 1158 }
1159 1159
1160 1160 /*
1161 1161 * Do not cache when disabled by tunable variable
1162 1162 */
1163 1163 if (lofi_max_comp_cache == 0)
1164 1164 return (NULL);
1165 1165
1166 1166 /*
1167 1167 * When the cache has not yet reached the maximum allowed
1168 1168 * number of segments, allocate a new cache element.
1169 1169 * Otherwise the cache is full; reuse the last list element
1170 1170 * (LRU) for caching the decompressed segment data.
1171 1171 *
1172 1172 * The cache element for the new decompressed segment data is
1173 1173 * added to the head of the list.
1174 1174 */
1175 1175 if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
1176 1176 lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
1177 1177 lc->lc_data = NULL;
1178 1178 list_insert_head(&lsp->ls_comp_cache, lc);
1179 1179 lsp->ls_comp_cache_count++;
1180 1180 } else {
1181 1181 lc = list_remove_tail(&lsp->ls_comp_cache);
1182 1182 if (lc == NULL)
1183 1183 return (NULL);
1184 1184 list_insert_head(&lsp->ls_comp_cache, lc);
1185 1185 }
1186 1186
1187 1187 /*
1188 1188 * Free old uncompressed segment data when reusing a cache
1189 1189 * entry.
1190 1190 */
1191 1191 if (lc->lc_data != NULL)
1192 1192 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
1193 1193
1194 1194 lc->lc_data = data;
1195 1195 lc->lc_index = seg_index;
1196 1196 return (lc);
1197 1197 }
1198 1198
1199 1199
1200 1200 /*ARGSUSED*/
1201 1201 static int
1202 1202 gzip_decompress(void *src, size_t srclen, void *dst,
1203 1203 size_t *dstlen, int level)
1204 1204 {
1205 1205 ASSERT(*dstlen >= srclen);
1206 1206
1207 1207 if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
1208 1208 return (-1);
1209 1209 return (0);
1210 1210 }
1211 1211
1212 1212 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
1213 1213 /*ARGSUSED*/
1214 1214 static int
1215 1215 lzma_decompress(void *src, size_t srclen, void *dst,
1216 1216 size_t *dstlen, int level)
1217 1217 {
1218 1218 size_t insizepure;
1219 1219 void *actual_src;
1220 1220 ELzmaStatus status;
1221 1221
1222 1222 insizepure = srclen - LZMA_HEADER_SIZE;
1223 1223 actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
1224 1224
1225 1225 if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
1226 1226 (const Byte *)actual_src, &insizepure,
1227 1227 (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
1228 1228 &g_Alloc) != SZ_OK) {
1229 1229 return (-1);
1230 1230 }
1231 1231 return (0);
1232 1232 }
1233 1233
1234 1234 /*
1235 1235 * This is basically what strategy used to be before we found we
1236 1236 * needed task queues.
1237 1237 */
1238 1238 static void
1239 1239 lofi_strategy_task(void *arg)
1240 1240 {
1241 1241 struct buf *bp = (struct buf *)arg;
1242 1242 int error;
1243 1243 int syncflag = 0;
1244 1244 struct lofi_state *lsp;
1245 1245 offset_t offset;
1246 1246 caddr_t bufaddr;
1247 1247 size_t len;
1248 1248 size_t xfersize;
1249 1249 boolean_t bufinited = B_FALSE;
1250 1250
1251 1251 lsp = ddi_get_soft_state(lofi_statep,
1252 1252 LOFI_MINOR2ID(getminor(bp->b_edev)));
1253 1253
1254 1254 if (lsp == NULL) {
1255 1255 error = ENXIO;
1256 1256 goto errout;
1257 1257 }
1258 1258 if (lsp->ls_kstat) {
1259 1259 mutex_enter(lsp->ls_kstat->ks_lock);
1260 1260 kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
1261 1261 mutex_exit(lsp->ls_kstat->ks_lock);
1262 1262 }
1263 1263
1264 1264 mutex_enter(&lsp->ls_vp_lock);
1265 1265 lsp->ls_vp_iocount++;
1266 1266 mutex_exit(&lsp->ls_vp_lock);
1267 1267
1268 1268 bp_mapin(bp);
1269 1269 bufaddr = bp->b_un.b_addr;
1270 1270 offset = (bp->b_lblkno + (diskaddr_t)(uintptr_t)bp->b_private)
1271 1271 << lsp->ls_lbshift; /* offset within file */
1272 1272 if (lsp->ls_crypto_enabled) {
1273 1273 /* encrypted data really begins after crypto header */
1274 1274 offset += lsp->ls_crypto_offset;
1275 1275 }
1276 1276 len = bp->b_bcount;
1277 1277 bufinited = B_TRUE;
1278 1278
1279 1279 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1280 1280 error = EIO;
1281 1281 goto errout;
1282 1282 }
1283 1283
1284 1284 /*
1285 1285 * If we're writing and the buffer was not B_ASYNC
1286 1286 * we'll follow up with a VOP_FSYNC() to force any
1287 1287 * asynchronous I/O to stable storage.
1288 1288 */
1289 1289 if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
1290 1290 syncflag = FSYNC;
1291 1291
1292 1292 /*
1293 1293 * We used to always use vn_rdwr here, but we cannot do that because
1294 1294 * we might decide to read or write from the the underlying
1295 1295 * file during this call, which would be a deadlock because
1296 1296 * we have the rw_lock. So instead we page, unless it's not
1297 1297 * mapable or it's a character device or it's an encrypted lofi.
1298 1298 */
1299 1299 if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
1300 1300 lsp->ls_crypto_enabled) {
1301 1301 error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
1302 1302 NULL);
1303 1303 } else if (lsp->ls_uncomp_seg_sz == 0) {
1304 1304 error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
1305 1305 } else {
1306 1306 uchar_t *compressed_seg = NULL, *cmpbuf;
1307 1307 uchar_t *uncompressed_seg = NULL;
1308 1308 lofi_compress_info_t *li;
1309 1309 size_t oblkcount;
1310 1310 ulong_t seglen;
1311 1311 uint64_t sblkno, eblkno, cmpbytes;
1312 1312 uint64_t uncompressed_seg_index;
1313 1313 struct lofi_comp_cache *lc;
1314 1314 offset_t sblkoff, eblkoff;
1315 1315 u_offset_t salign, ealign;
1316 1316 u_offset_t sdiff;
1317 1317 uint32_t comp_data_sz;
1318 1318 uint64_t i;
1319 1319 int j;
1320 1320
1321 1321 /*
1322 1322 * From here on we're dealing primarily with compressed files
1323 1323 */
1324 1324 ASSERT(!lsp->ls_crypto_enabled);
1325 1325
1326 1326 /*
1327 1327 * Compressed files can only be read from and
1328 1328 * not written to
1329 1329 */
1330 1330 if (!(bp->b_flags & B_READ)) {
1331 1331 bp->b_resid = bp->b_bcount;
1332 1332 error = EROFS;
1333 1333 goto done;
1334 1334 }
1335 1335
1336 1336 ASSERT(lsp->ls_comp_algorithm_index >= 0);
1337 1337 li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
1338 1338 /*
1339 1339 * Compute starting and ending compressed segment numbers
1340 1340 * We use only bitwise operations avoiding division and
1341 1341 * modulus because we enforce the compression segment size
1342 1342 * to a power of 2
1343 1343 */
1344 1344 sblkno = offset >> lsp->ls_comp_seg_shift;
1345 1345 sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
1346 1346 eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
1347 1347 eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
1348 1348
1349 1349 /*
1350 1350 * Check the decompressed segment cache.
1351 1351 *
1352 1352 * The cache is used only when the requested data
1353 1353 * is within a segment. Requests that cross
1354 1354 * segment boundaries bypass the cache.
1355 1355 */
1356 1356 if (sblkno == eblkno ||
1357 1357 (sblkno + 1 == eblkno && eblkoff == 0)) {
1358 1358 /*
1359 1359 * Request doesn't cross a segment boundary,
1360 1360 * now check the cache.
1361 1361 */
1362 1362 mutex_enter(&lsp->ls_comp_cache_lock);
1363 1363 lc = lofi_find_comp_data(lsp, sblkno);
1364 1364 if (lc != NULL) {
1365 1365 /*
1366 1366 * We've found the decompressed segment
1367 1367 * data in the cache; reuse it.
1368 1368 */
1369 1369 bcopy(lc->lc_data + sblkoff, bufaddr,
1370 1370 bp->b_bcount);
1371 1371 mutex_exit(&lsp->ls_comp_cache_lock);
1372 1372 bp->b_resid = 0;
1373 1373 error = 0;
1374 1374 goto done;
1375 1375 }
1376 1376 mutex_exit(&lsp->ls_comp_cache_lock);
1377 1377 }
1378 1378
1379 1379 /*
1380 1380 * Align start offset to block boundary for segmap
1381 1381 */
1382 1382 salign = lsp->ls_comp_seg_index[sblkno];
1383 1383 sdiff = salign & (DEV_BSIZE - 1);
1384 1384 salign -= sdiff;
1385 1385 if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
1386 1386 /*
1387 1387 * We're dealing with the last segment of
1388 1388 * the compressed file -- the size of this
1389 1389 * segment *may not* be the same as the
1390 1390 * segment size for the file
1391 1391 */
1392 1392 eblkoff = (offset + bp->b_bcount) &
1393 1393 (lsp->ls_uncomp_last_seg_sz - 1);
1394 1394 ealign = lsp->ls_vp_comp_size;
1395 1395 } else {
1396 1396 ealign = lsp->ls_comp_seg_index[eblkno + 1];
1397 1397 }
1398 1398
1399 1399 /*
1400 1400 * Preserve original request paramaters
1401 1401 */
1402 1402 oblkcount = bp->b_bcount;
1403 1403
1404 1404 /*
1405 1405 * Assign the calculated parameters
1406 1406 */
1407 1407 comp_data_sz = ealign - salign;
1408 1408 bp->b_bcount = comp_data_sz;
1409 1409
1410 1410 /*
1411 1411 * Buffers to hold compressed segments are pre-allocated
1412 1412 * on a per-thread basis. Find a pre-allocated buffer
1413 1413 * that is not currently in use and mark it for use.
1414 1414 */
1415 1415 mutex_enter(&lsp->ls_comp_bufs_lock);
1416 1416 for (j = 0; j < lofi_taskq_nthreads; j++) {
1417 1417 if (lsp->ls_comp_bufs[j].inuse == 0) {
1418 1418 lsp->ls_comp_bufs[j].inuse = 1;
1419 1419 break;
1420 1420 }
1421 1421 }
1422 1422
1423 1423 mutex_exit(&lsp->ls_comp_bufs_lock);
1424 1424 ASSERT(j < lofi_taskq_nthreads);
1425 1425
1426 1426 /*
1427 1427 * If the pre-allocated buffer size does not match
1428 1428 * the size of the I/O request, re-allocate it with
1429 1429 * the appropriate size
1430 1430 */
1431 1431 if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
1432 1432 if (lsp->ls_comp_bufs[j].bufsize > 0)
1433 1433 kmem_free(lsp->ls_comp_bufs[j].buf,
1434 1434 lsp->ls_comp_bufs[j].bufsize);
1435 1435 lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
1436 1436 KM_SLEEP);
1437 1437 lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
1438 1438 }
1439 1439 compressed_seg = lsp->ls_comp_bufs[j].buf;
1440 1440
1441 1441 /*
1442 1442 * Map in the calculated number of blocks
1443 1443 */
1444 1444 error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
1445 1445 bp, lsp);
1446 1446
1447 1447 bp->b_bcount = oblkcount;
1448 1448 bp->b_resid = oblkcount;
1449 1449 if (error != 0)
1450 1450 goto done;
1451 1451
1452 1452 /*
1453 1453 * decompress compressed blocks start
1454 1454 */
1455 1455 cmpbuf = compressed_seg + sdiff;
1456 1456 for (i = sblkno; i <= eblkno; i++) {
1457 1457 ASSERT(i < lsp->ls_comp_index_sz - 1);
1458 1458 uchar_t *useg;
1459 1459
1460 1460 /*
1461 1461 * The last segment is special in that it is
1462 1462 * most likely not going to be the same
1463 1463 * (uncompressed) size as the other segments.
1464 1464 */
1465 1465 if (i == (lsp->ls_comp_index_sz - 2)) {
1466 1466 seglen = lsp->ls_uncomp_last_seg_sz;
1467 1467 } else {
1468 1468 seglen = lsp->ls_uncomp_seg_sz;
1469 1469 }
1470 1470
1471 1471 /*
1472 1472 * Each of the segment index entries contains
1473 1473 * the starting block number for that segment.
1474 1474 * The number of compressed bytes in a segment
1475 1475 * is thus the difference between the starting
1476 1476 * block number of this segment and the starting
1477 1477 * block number of the next segment.
1478 1478 */
1479 1479 cmpbytes = lsp->ls_comp_seg_index[i + 1] -
1480 1480 lsp->ls_comp_seg_index[i];
1481 1481
1482 1482 /*
1483 1483 * The first byte in a compressed segment is a flag
1484 1484 * that indicates whether this segment is compressed
1485 1485 * at all.
1486 1486 *
1487 1487 * The variable 'useg' is used (instead of
1488 1488 * uncompressed_seg) in this loop to keep a
1489 1489 * reference to the uncompressed segment.
1490 1490 *
1491 1491 * N.B. If 'useg' is replaced with uncompressed_seg,
1492 1492 * it leads to memory leaks and heap corruption in
1493 1493 * corner cases where compressed segments lie
1494 1494 * adjacent to uncompressed segments.
1495 1495 */
1496 1496 if (*cmpbuf == UNCOMPRESSED) {
1497 1497 useg = cmpbuf + SEGHDR;
1498 1498 } else {
1499 1499 if (uncompressed_seg == NULL)
1500 1500 uncompressed_seg =
1501 1501 kmem_alloc(lsp->ls_uncomp_seg_sz,
1502 1502 KM_SLEEP);
1503 1503 useg = uncompressed_seg;
1504 1504 uncompressed_seg_index = i;
1505 1505
1506 1506 if (li->l_decompress((cmpbuf + SEGHDR),
1507 1507 (cmpbytes - SEGHDR), uncompressed_seg,
1508 1508 &seglen, li->l_level) != 0) {
1509 1509 error = EIO;
1510 1510 goto done;
1511 1511 }
1512 1512 }
1513 1513
1514 1514 /*
1515 1515 * Determine how much uncompressed data we
1516 1516 * have to copy and copy it
1517 1517 */
1518 1518 xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
1519 1519 if (i == eblkno)
1520 1520 xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
1521 1521
1522 1522 bcopy((useg + sblkoff), bufaddr, xfersize);
1523 1523
1524 1524 cmpbuf += cmpbytes;
1525 1525 bufaddr += xfersize;
1526 1526 bp->b_resid -= xfersize;
1527 1527 sblkoff = 0;
1528 1528
1529 1529 if (bp->b_resid == 0)
1530 1530 break;
1531 1531 } /* decompress compressed blocks ends */
1532 1532
1533 1533 /*
1534 1534 * Skip to done if there is no uncompressed data to cache
1535 1535 */
1536 1536 if (uncompressed_seg == NULL)
1537 1537 goto done;
1538 1538
1539 1539 /*
1540 1540 * Add the data for the last decompressed segment to
1541 1541 * the cache.
1542 1542 *
1543 1543 * In case the uncompressed segment data was added to (and
1544 1544 * is referenced by) the cache, make sure we don't free it
1545 1545 * here.
1546 1546 */
1547 1547 mutex_enter(&lsp->ls_comp_cache_lock);
1548 1548 if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
1549 1549 uncompressed_seg)) != NULL) {
1550 1550 uncompressed_seg = NULL;
1551 1551 }
1552 1552 mutex_exit(&lsp->ls_comp_cache_lock);
1553 1553
1554 1554 done:
1555 1555 if (compressed_seg != NULL) {
1556 1556 mutex_enter(&lsp->ls_comp_bufs_lock);
1557 1557 lsp->ls_comp_bufs[j].inuse = 0;
1558 1558 mutex_exit(&lsp->ls_comp_bufs_lock);
1559 1559 }
1560 1560 if (uncompressed_seg != NULL)
1561 1561 kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
1562 1562 } /* end of handling compressed files */
1563 1563
1564 1564 if ((error == 0) && (syncflag != 0))
1565 1565 error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
1566 1566
1567 1567 errout:
1568 1568 if (bufinited && lsp->ls_kstat) {
1569 1569 size_t n_done = bp->b_bcount - bp->b_resid;
1570 1570 kstat_io_t *kioptr;
1571 1571
1572 1572 mutex_enter(lsp->ls_kstat->ks_lock);
1573 1573 kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
1574 1574 if (bp->b_flags & B_READ) {
1575 1575 kioptr->nread += n_done;
1576 1576 kioptr->reads++;
1577 1577 } else {
1578 1578 kioptr->nwritten += n_done;
1579 1579 kioptr->writes++;
1580 1580 }
1581 1581 kstat_runq_exit(kioptr);
1582 1582 mutex_exit(lsp->ls_kstat->ks_lock);
1583 1583 }
1584 1584
1585 1585 mutex_enter(&lsp->ls_vp_lock);
1586 1586 if (--lsp->ls_vp_iocount == 0)
1587 1587 cv_broadcast(&lsp->ls_vp_cv);
1588 1588 mutex_exit(&lsp->ls_vp_lock);
1589 1589
1590 1590 bioerror(bp, error);
1591 1591 biodone(bp);
1592 1592 }
1593 1593
1594 1594 static int
1595 1595 lofi_strategy(struct buf *bp)
1596 1596 {
1597 1597 struct lofi_state *lsp;
1598 1598 offset_t offset;
1599 1599 minor_t part;
1600 1600 diskaddr_t p_lba;
1601 1601 diskaddr_t p_nblks;
1602 1602 int shift;
1603 1603
1604 1604 /*
1605 1605 * We cannot just do I/O here, because the current thread
1606 1606 * _might_ end up back in here because the underlying filesystem
1607 1607 * wants a buffer, which eventually gets into bio_recycle and
1608 1608 * might call into lofi to write out a delayed-write buffer.
1609 1609 * This is bad if the filesystem above lofi is the same as below.
1610 1610 *
1611 1611 * We could come up with a complex strategy using threads to
1612 1612 * do the I/O asynchronously, or we could use task queues. task
1613 1613 * queues were incredibly easy so they win.
1614 1614 */
1615 1615
1616 1616 lsp = ddi_get_soft_state(lofi_statep,
1617 1617 LOFI_MINOR2ID(getminor(bp->b_edev)));
1618 1618 part = LOFI_PART(getminor(bp->b_edev));
1619 1619
1620 1620 if (lsp == NULL) {
1621 1621 bioerror(bp, ENXIO);
1622 1622 biodone(bp);
1623 1623 return (0);
1624 1624 }
1625 1625 shift = lsp->ls_lbshift;
1626 1626
1627 1627 p_lba = 0;
1628 1628 p_nblks = lsp->ls_vp_size >> shift;
1629 1629
1630 1630 if (lsp->ls_cmlbhandle != NULL) {
1631 1631 if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &p_nblks, &p_lba,
1632 1632 NULL, NULL, 0)) {
1633 1633 bioerror(bp, ENXIO);
1634 1634 biodone(bp);
1635 1635 return (0);
1636 1636 }
1637 1637 }
1638 1638
1639 1639 /* start block past partition end? */
1640 1640 if (bp->b_lblkno > p_nblks) {
1641 1641 bioerror(bp, ENXIO);
1642 1642 biodone(bp);
1643 1643 return (0);
1644 1644 }
1645 1645
1646 1646 offset = (bp->b_lblkno+p_lba) << shift; /* offset within file */
1647 1647
1648 1648 mutex_enter(&lsp->ls_vp_lock);
1649 1649 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1650 1650 bioerror(bp, EIO);
1651 1651 biodone(bp);
1652 1652 mutex_exit(&lsp->ls_vp_lock);
1653 1653 return (0);
1654 1654 }
1655 1655
1656 1656 if (lsp->ls_crypto_enabled) {
1657 1657 /* encrypted data really begins after crypto header */
1658 1658 offset += lsp->ls_crypto_offset;
1659 1659 }
1660 1660
1661 1661 /* make sure we will not pass the file or partition size */
1662 1662 if (offset == lsp->ls_vp_size ||
1663 1663 offset == (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) {
1664 1664 /* EOF */
1665 1665 if ((bp->b_flags & B_READ) != 0) {
1666 1666 bp->b_resid = bp->b_bcount;
1667 1667 bioerror(bp, 0);
1668 1668 } else {
1669 1669 /* writes should fail */
1670 1670 bioerror(bp, ENXIO);
1671 1671 }
1672 1672 biodone(bp);
1673 1673 mutex_exit(&lsp->ls_vp_lock);
1674 1674 return (0);
1675 1675 }
1676 1676 if ((offset > lsp->ls_vp_size) ||
1677 1677 (offset > (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) ||
1678 1678 ((offset + bp->b_bcount) > ((p_lba + p_nblks) << shift))) {
1679 1679 bioerror(bp, ENXIO);
1680 1680 biodone(bp);
1681 1681 mutex_exit(&lsp->ls_vp_lock);
1682 1682 return (0);
1683 1683 }
1684 1684
1685 1685 mutex_exit(&lsp->ls_vp_lock);
1686 1686
1687 1687 if (lsp->ls_kstat) {
1688 1688 mutex_enter(lsp->ls_kstat->ks_lock);
1689 1689 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
1690 1690 mutex_exit(lsp->ls_kstat->ks_lock);
1691 1691 }
1692 1692 bp->b_private = (void *)(uintptr_t)p_lba; /* partition start */
1693 1693 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
1694 1694 return (0);
1695 1695 }
1696 1696
1697 1697 /*ARGSUSED2*/
1698 1698 static int
1699 1699 lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
1700 1700 {
1701 1701 if (getminor(dev) == 0)
1702 1702 return (EINVAL);
1703 1703 UIO_CHECK(uio);
1704 1704 return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
1705 1705 }
1706 1706
1707 1707 /*ARGSUSED2*/
1708 1708 static int
1709 1709 lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
1710 1710 {
1711 1711 if (getminor(dev) == 0)
1712 1712 return (EINVAL);
1713 1713 UIO_CHECK(uio);
1714 1714 return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
1715 1715 }
1716 1716
1717 1717 /*ARGSUSED2*/
1718 1718 static int
1719 1719 lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
1720 1720 {
1721 1721 if (getminor(dev) == 0)
1722 1722 return (EINVAL);
1723 1723 UIO_CHECK(aio->aio_uio);
1724 1724 return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
1725 1725 }
1726 1726
1727 1727 /*ARGSUSED2*/
1728 1728 static int
1729 1729 lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
1730 1730 {
1731 1731 if (getminor(dev) == 0)
1732 1732 return (EINVAL);
1733 1733 UIO_CHECK(aio->aio_uio);
1734 1734 return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
1735 1735 }
1736 1736
1737 1737 /*ARGSUSED*/
1738 1738 static int
1739 1739 lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1740 1740 {
1741 1741 struct lofi_state *lsp;
1742 1742 dev_t dev = (dev_t)arg;
1743 1743 int instance;
1744 1744
1745 1745 instance = LOFI_MINOR2ID(getminor(dev));
1746 1746 switch (infocmd) {
1747 1747 case DDI_INFO_DEVT2DEVINFO:
1748 1748 lsp = ddi_get_soft_state(lofi_statep, instance);
1749 1749 if (lsp == NULL)
1750 1750 return (DDI_FAILURE);
1751 1751 *result = lsp->ls_dip;
1752 1752 return (DDI_SUCCESS);
1753 1753 case DDI_INFO_DEVT2INSTANCE:
1754 1754 *result = (void *) (intptr_t)instance;
1755 1755 return (DDI_SUCCESS);
1756 1756 }
1757 1757 return (DDI_FAILURE);
1758 1758 }
1759 1759
1760 1760 static int
1761 1761 lofi_create_minor_nodes(struct lofi_state *lsp, boolean_t labeled)
1762 1762 {
1763 1763 int error = 0;
1764 1764 int instance = ddi_get_instance(lsp->ls_dip);
1765 1765
1766 1766 if (labeled == B_TRUE) {
1767 1767 cmlb_alloc_handle(&lsp->ls_cmlbhandle);
1768 1768 error = cmlb_attach(lsp->ls_dip, &lofi_tg_ops, DTYPE_DIRECT,
1769 1769 B_FALSE, B_FALSE, DDI_NT_BLOCK_CHAN,
1770 1770 CMLB_CREATE_P0_MINOR_NODE, lsp->ls_cmlbhandle, (void *)1);
1771 1771
1772 1772 if (error != DDI_SUCCESS) {
1773 1773 cmlb_free_handle(&lsp->ls_cmlbhandle);
1774 1774 lsp->ls_cmlbhandle = NULL;
1775 1775 error = ENXIO;
1776 1776 }
1777 1777 } else {
1778 1778 /* create minor nodes */
1779 1779 error = ddi_create_minor_node(lsp->ls_dip, LOFI_BLOCK_NODE,
1780 1780 S_IFBLK, LOFI_ID2MINOR(instance), DDI_PSEUDO, 0);
1781 1781 if (error == DDI_SUCCESS) {
1782 1782 error = ddi_create_minor_node(lsp->ls_dip,
1783 1783 LOFI_CHAR_NODE, S_IFCHR, LOFI_ID2MINOR(instance),
1784 1784 DDI_PSEUDO, 0);
1785 1785 if (error != DDI_SUCCESS) {
1786 1786 ddi_remove_minor_node(lsp->ls_dip,
1787 1787 LOFI_BLOCK_NODE);
1788 1788 error = ENXIO;
1789 1789 }
1790 1790 } else
1791 1791 error = ENXIO;
1792 1792 }
1793 1793 return (error);
1794 1794 }
1795 1795
1796 1796 static int
1797 1797 lofi_zone_bind(struct lofi_state *lsp)
1798 1798 {
1799 1799 int error = 0;
1800 1800
1801 1801 mutex_enter(&curproc->p_lock);
1802 1802 if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) {
1803 1803 mutex_exit(&curproc->p_lock);
1804 1804 return (error);
1805 1805 }
1806 1806 mutex_exit(&curproc->p_lock);
1807 1807
1808 1808 if (ddi_prop_update_string(lsp->ls_dev, lsp->ls_dip, ZONE_PROP_NAME,
1809 1809 (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) {
1810 1810 rctl_decr_lofi(curproc->p_zone, 1);
1811 1811 error = EINVAL;
1812 1812 } else {
1813 1813 zone_init_ref(&lsp->ls_zone);
1814 1814 zone_hold_ref(curzone, &lsp->ls_zone, ZONE_REF_LOFI);
1815 1815 }
1816 1816 return (error);
1817 1817 }
1818 1818
1819 1819 static void
1820 1820 lofi_zone_unbind(struct lofi_state *lsp)
1821 1821 {
1822 1822 (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME);
1823 1823 rctl_decr_lofi(curproc->p_zone, 1);
1824 1824 zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
1825 1825 }
1826 1826
1827 1827 static int
1828 1828 lofi_online_dev(dev_info_t *dip)
1829 1829 {
1830 1830 boolean_t labeled;
1831 1831 int error;
1832 1832 int instance = ddi_get_instance(dip);
1833 1833 struct lofi_state *lsp;
1834 1834
1835 1835 labeled = B_FALSE;
1836 1836 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "labeled"))
1837 1837 labeled = B_TRUE;
1838 1838
1839 1839 /* lsp alloc+init, soft state is freed in lofi_detach */
1840 1840 error = ddi_soft_state_zalloc(lofi_statep, instance);
1841 1841 if (error == DDI_FAILURE) {
1842 1842 return (ENOMEM);
1843 1843 }
1844 1844
1845 1845 lsp = ddi_get_soft_state(lofi_statep, instance);
1846 1846 lsp->ls_dip = dip;
1847 1847
1848 1848 if ((error = lofi_zone_bind(lsp)) != 0)
1849 1849 goto err;
1850 1850
1851 1851 cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
1852 1852 mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
1853 1853 mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
1854 1854 mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
1855 1855 mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
1856 1856
1857 1857 if ((error = lofi_create_minor_nodes(lsp, labeled)) != 0) {
1858 1858 lofi_zone_unbind(lsp);
1859 1859 goto lerr;
1860 1860 }
1861 1861
1862 1862 /* driver handles kernel-issued IOCTLs */
1863 1863 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1864 1864 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1865 1865 error = DDI_FAILURE;
1866 1866 goto merr;
1867 1867 }
1868 1868
1869 1869 lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, instance,
1870 1870 NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid());
1871 1871 if (lsp->ls_kstat == NULL) {
1872 1872 (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip,
1873 1873 DDI_KERNEL_IOCTL);
1874 1874 error = ENOMEM;
1875 1875 goto merr;
1876 1876 }
1877 1877
1878 1878 lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
1879 1879 kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID);
1880 1880 kstat_install(lsp->ls_kstat);
1881 1881 return (DDI_SUCCESS);
1882 1882 merr:
1883 1883 if (lsp->ls_cmlbhandle != NULL) {
1884 1884 cmlb_detach(lsp->ls_cmlbhandle, 0);
1885 1885 cmlb_free_handle(&lsp->ls_cmlbhandle);
1886 1886 }
1887 1887 ddi_remove_minor_node(dip, NULL);
1888 1888 lofi_zone_unbind(lsp);
1889 1889 lerr:
1890 1890 mutex_destroy(&lsp->ls_comp_cache_lock);
1891 1891 mutex_destroy(&lsp->ls_comp_bufs_lock);
1892 1892 mutex_destroy(&lsp->ls_kstat_lock);
1893 1893 mutex_destroy(&lsp->ls_vp_lock);
1894 1894 cv_destroy(&lsp->ls_vp_cv);
1895 1895 err:
1896 1896 ddi_soft_state_free(lofi_statep, instance);
1897 1897 return (error);
1898 1898 }
1899 1899
1900 1900 /*ARGSUSED*/
1901 1901 static int
1902 1902 lofi_dev_callback(sysevent_t *ev, void *cookie)
1903 1903 {
1904 1904 nvlist_t *nvlist;
1905 1905 char *class, *driver;
1906 1906 char name[10];
1907 1907 int32_t instance;
1908 1908
1909 1909 class = sysevent_get_class_name(ev);
1910 1910 if (strcmp(class, EC_DEV_ADD) && strcmp(class, EC_DEV_REMOVE))
1911 1911 return (0);
1912 1912
1913 1913 (void) sysevent_get_attr_list(ev, &nvlist);
1914 1914 driver = fnvlist_lookup_string(nvlist, DEV_DRIVER_NAME);
1915 1915 instance = fnvlist_lookup_int32(nvlist, DEV_INSTANCE);
1916 1916
1917 1917 if (strcmp(driver, LOFI_DRIVER_NAME) != 0) {
1918 1918 fnvlist_free(nvlist);
1919 1919 return (0);
1920 1920 }
1921 1921
1922 1922 /*
1923 1923 * insert or remove device info, then announce the change
1924 1924 * via cv_broadcast.
1925 1925 * This allows the MAP/UNMAP to monitor device change.
1926 1926 */
1927 1927 (void) snprintf(name, sizeof (name), "%d", instance);
1928 1928 if (strcmp(class, EC_DEV_ADD) == 0) {
1929 1929 mutex_enter(&lofi_chan_lock);
1930 1930 fnvlist_add_nvlist(lofi_devlink_cache, name, nvlist);
1931 1931 cv_broadcast(&lofi_chan_cv);
1932 1932 mutex_exit(&lofi_chan_lock);
1933 1933 } else if (strcmp(class, EC_DEV_REMOVE) == 0) {
1934 1934 mutex_enter(&lofi_chan_lock);
1935 1935 /* Can not use fnvlist_remove() as we can get ENOENT. */
1936 1936 (void) nvlist_remove_all(lofi_devlink_cache, name);
1937 1937 cv_broadcast(&lofi_chan_cv);
1938 1938 mutex_exit(&lofi_chan_lock);
1939 1939 }
1940 1940
1941 1941 fnvlist_free(nvlist);
1942 1942 return (0);
1943 1943 }
1944 1944
1945 1945 static int
1946 1946 lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1947 1947 {
1948 1948 int rv;
1949 1949 int instance = ddi_get_instance(dip);
1950 1950 struct lofi_state *lsp;
1951 1951
1952 1952 if (cmd != DDI_ATTACH)
1953 1953 return (DDI_FAILURE);
1954 1954
1955 1955 /*
1956 1956 * Instance 0 is control instance, attaching control instance
1957 1957 * will set the lofi up and ready.
1958 1958 */
1959 1959 if (instance == 0) {
1960 1960 rv = ddi_soft_state_zalloc(lofi_statep, 0);
1961 1961 if (rv == DDI_FAILURE) {
1962 1962 return (DDI_FAILURE);
1963 1963 }
1964 1964 lsp = ddi_get_soft_state(lofi_statep, instance);
1965 1965 rv = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
1966 1966 DDI_PSEUDO, 0);
1967 1967 if (rv == DDI_FAILURE) {
1968 1968 ddi_soft_state_free(lofi_statep, 0);
1969 1969 return (DDI_FAILURE);
1970 1970 }
1971 1971 /* driver handles kernel-issued IOCTLs */
1972 1972 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1973 1973 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1974 1974 ddi_remove_minor_node(dip, NULL);
1975 1975 ddi_soft_state_free(lofi_statep, 0);
1976 1976 return (DDI_FAILURE);
1977 1977 }
1978 1978
1979 1979 rv = sysevent_evc_bind(DEVFS_CHANNEL, &lofi_chan,
1980 1980 EVCH_CREAT | EVCH_HOLD_PEND);
1981 1981 if (rv == 0) {
1982 1982 rv = sysevent_evc_subscribe(lofi_chan, "lofi",
1983 1983 EC_ALL, lofi_dev_callback, NULL, 0);
1984 1984 rv |= sysevent_evc_subscribe(lofi_chan, "disk",
1985 1985 EC_ALL, lofi_dev_callback, NULL, 0);
1986 1986 } else
1987 1987 lofi_chan = NULL;
1988 1988 if (rv != 0) {
1989 1989 if (lofi_chan != NULL)
1990 1990 (void) sysevent_evc_unbind(lofi_chan);
1991 1991 ddi_prop_remove_all(dip);
1992 1992 ddi_remove_minor_node(dip, NULL);
1993 1993 ddi_soft_state_free(lofi_statep, 0);
1994 1994 return (DDI_FAILURE);
1995 1995 }
1996 1996 zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL);
1997 1997
1998 1998 lsp->ls_dip = dip;
1999 1999 } else {
2000 2000 if (lofi_online_dev(dip) == DDI_FAILURE)
2001 2001 return (DDI_FAILURE);
2002 2002 }
2003 2003
2004 2004 ddi_report_dev(dip);
2005 2005 return (DDI_SUCCESS);
2006 2006 }
2007 2007
2008 2008 static int
2009 2009 lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2010 2010 {
2011 2011 struct lofi_state *lsp;
2012 2012 int instance = ddi_get_instance(dip);
2013 2013
2014 2014 if (cmd != DDI_DETACH)
2015 2015 return (DDI_FAILURE);
2016 2016
2017 2017 /*
2018 2018 * If the instance is not 0, release state.
2019 2019 * The instance 0 is control device, we can not detach it
2020 2020 * before other instances are detached.
2021 2021 */
2022 2022 if (instance != 0) {
2023 2023 lsp = ddi_get_soft_state(lofi_statep, instance);
2024 2024 if (lsp != NULL && lsp->ls_vp_ready == B_FALSE) {
2025 2025 ddi_soft_state_free(lofi_statep, instance);
2026 2026 return (DDI_SUCCESS);
2027 2027 } else
2028 2028 return (DDI_FAILURE);
2029 2029 }
2030 2030 mutex_enter(&lofi_lock);
2031 2031
2032 2032 if (!list_is_empty(&lofi_list)) {
2033 2033 mutex_exit(&lofi_lock);
2034 2034 return (DDI_FAILURE);
2035 2035 }
2036 2036
2037 2037 ddi_remove_minor_node(dip, NULL);
2038 2038 ddi_prop_remove_all(dip);
2039 2039
2040 2040 mutex_exit(&lofi_lock);
2041 2041
2042 2042 (void) sysevent_evc_unbind(lofi_chan);
2043 2043 if (zone_key_delete(lofi_zone_key) != 0)
2044 2044 cmn_err(CE_WARN, "failed to delete zone key");
2045 2045
2046 2046 ddi_soft_state_free(lofi_statep, 0);
2047 2047
2048 2048 return (DDI_SUCCESS);
2049 2049 }
2050 2050
2051 2051 /*
2052 2052 * With the addition of encryption, we must be careful that encryption key is
2053 2053 * wiped before kernel's data structures are freed so it cannot accidentally
2054 2054 * slip out to userland through uninitialized data elsewhere.
2055 2055 */
2056 2056 static void
2057 2057 free_lofi_ioctl(struct lofi_ioctl *klip)
2058 2058 {
2059 2059 /* Make sure this encryption key doesn't stick around */
2060 2060 bzero(klip->li_key, sizeof (klip->li_key));
2061 2061 kmem_free(klip, sizeof (struct lofi_ioctl));
2062 2062 }
2063 2063
2064 2064 /*
2065 2065 * These two functions simplify the rest of the ioctls that need to copyin/out
2066 2066 * the lofi_ioctl structure.
2067 2067 */
2068 2068 int
2069 2069 copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp,
2070 2070 int flag)
2071 2071 {
2072 2072 struct lofi_ioctl *klip;
2073 2073 int error;
2074 2074
2075 2075 klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
2076 2076 error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
2077 2077 if (error)
2078 2078 goto err;
2079 2079
2080 2080 /* ensure NULL termination */
2081 2081 klip->li_filename[MAXPATHLEN-1] = '\0';
2082 2082 klip->li_devpath[MAXPATHLEN-1] = '\0';
2083 2083 klip->li_algorithm[MAXALGLEN-1] = '\0';
2084 2084 klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
2085 2085 klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
2086 2086
2087 2087 if (klip->li_id > L_MAXMIN32) {
2088 2088 error = EINVAL;
2089 2089 goto err;
2090 2090 }
2091 2091
2092 2092 return (0);
2093 2093
2094 2094 err:
2095 2095 free_lofi_ioctl(klip);
2096 2096 return (error);
2097 2097 }
2098 2098
2099 2099 int
2100 2100 copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
2101 2101 int flag)
2102 2102 {
2103 2103 int error;
2104 2104
2105 2105 /*
2106 2106 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
2107 2107 * This ensures that an attacker can't trivially find the
2108 2108 * key for a mapping just by issuing the ioctl.
2109 2109 *
2110 2110 * It can still be found by poking around in kmem with mdb(1),
2111 2111 * but there is no point in making it easy when the info isn't
2112 2112 * of any use in this direction anyway.
2113 2113 *
2114 2114 * Either way we don't actually have the raw key stored in
2115 2115 * a form that we can get it anyway, since we just used it
2116 2116 * to create a ctx template and didn't keep "the original".
2117 2117 */
2118 2118 error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
2119 2119 if (error)
2120 2120 return (EFAULT);
2121 2121 return (0);
2122 2122 }
2123 2123
2124 2124 static int
2125 2125 lofi_access(struct lofi_state *lsp)
2126 2126 {
2127 2127 ASSERT(MUTEX_HELD(&lofi_lock));
2128 2128 if (INGLOBALZONE(curproc) || lsp->ls_zone.zref_zone == curzone)
2129 2129 return (0);
2130 2130 return (EPERM);
2131 2131 }
2132 2132
2133 2133 /*
2134 2134 * Find the lofi state for the given filename. We compare by vnode to
2135 2135 * allow the global zone visibility into NGZ lofi nodes.
2136 2136 */
2137 2137 static int
2138 2138 file_to_lofi_nocheck(char *filename, boolean_t readonly,
2139 2139 struct lofi_state **lspp)
2140 2140 {
2141 2141 struct lofi_state *lsp;
2142 2142 vnode_t *vp = NULL;
2143 2143 int err = 0;
2144 2144 int rdfiles = 0;
2145 2145
2146 2146 ASSERT(MUTEX_HELD(&lofi_lock));
2147 2147
2148 2148 if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW,
2149 2149 NULLVPP, &vp)) != 0)
2150 2150 goto out;
2151 2151
2152 2152 if (vp->v_type == VREG) {
2153 2153 vnode_t *realvp;
2154 2154 if (VOP_REALVP(vp, &realvp, NULL) == 0) {
2155 2155 VN_HOLD(realvp);
2156 2156 VN_RELE(vp);
2157 2157 vp = realvp;
2158 2158 }
2159 2159 }
2160 2160
2161 2161 for (lsp = list_head(&lofi_list); lsp != NULL;
2162 2162 lsp = list_next(&lofi_list, lsp)) {
2163 2163 if (lsp->ls_vp == vp) {
2164 2164 if (lspp != NULL)
2165 2165 *lspp = lsp;
2166 2166 if (lsp->ls_readonly) {
2167 2167 rdfiles++;
2168 2168 /* Skip if '-r' is specified */
2169 2169 if (readonly)
2170 2170 continue;
2171 2171 }
2172 2172 goto out;
2173 2173 }
2174 2174 }
2175 2175
2176 2176 err = ENOENT;
2177 2177
2178 2178 /*
2179 2179 * If a filename is given as an argument for lofi_unmap, we shouldn't
2180 2180 * allow unmap if there are multiple read-only lofi devices associated
2181 2181 * with this file.
2182 2182 */
2183 2183 if (lspp != NULL) {
2184 2184 if (rdfiles == 1)
2185 2185 err = 0;
2186 2186 else if (rdfiles > 1)
2187 2187 err = EBUSY;
2188 2188 }
2189 2189
2190 2190 out:
2191 2191 if (vp != NULL)
2192 2192 VN_RELE(vp);
2193 2193 return (err);
2194 2194 }
2195 2195
2196 2196 /*
2197 2197 * Find the minor for the given filename, checking the zone can access
2198 2198 * it.
2199 2199 */
2200 2200 static int
2201 2201 file_to_lofi(char *filename, boolean_t readonly, struct lofi_state **lspp)
2202 2202 {
2203 2203 int err = 0;
2204 2204
2205 2205 ASSERT(MUTEX_HELD(&lofi_lock));
2206 2206
2207 2207 if ((err = file_to_lofi_nocheck(filename, readonly, lspp)) != 0)
2208 2208 return (err);
2209 2209
2210 2210 if ((err = lofi_access(*lspp)) != 0)
2211 2211 return (err);
2212 2212
2213 2213 return (0);
2214 2214 }
2215 2215
2216 2216 /*
2217 2217 * Fakes up a disk geometry based on the size of the file. This is needed
2218 2218 * to support newfs on traditional lofi device, but also will provide
2219 2219 * geometry hint for cmlb.
2220 2220 */
2221 2221 static void
2222 2222 fake_disk_geometry(struct lofi_state *lsp)
2223 2223 {
2224 2224 u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
2225 2225
2226 2226 /* dk_geom - see dkio(7I) */
2227 2227 /*
2228 2228 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
2229 2229 * of sectors), but that breaks programs like fdisk which want to
2230 2230 * partition a disk by cylinder. With one cylinder, you can't create
2231 2231 * an fdisk partition and put pcfs on it for testing (hard to pick
2232 2232 * a number between one and one).
2233 2233 *
2234 2234 * The cheezy floppy test is an attempt to not have too few cylinders
2235 2235 * for a small file, or so many on a big file that you waste space
2236 2236 * for backup superblocks or cylinder group structures.
2237 2237 */
2238 2238 bzero(&lsp->ls_dkg, sizeof (lsp->ls_dkg));
2239 2239 if (dsize < (2 * 1024 * 1024)) /* floppy? */
2240 2240 lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
2241 2241 else
2242 2242 lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
2243 2243 /* in case file file is < 100k */
2244 2244 if (lsp->ls_dkg.dkg_ncyl == 0)
2245 2245 lsp->ls_dkg.dkg_ncyl = 1;
2246 2246
2247 2247 lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl;
2248 2248 lsp->ls_dkg.dkg_nhead = 1;
2249 2249 lsp->ls_dkg.dkg_rpm = 7200;
2250 2250
2251 2251 lsp->ls_dkg.dkg_nsect = dsize /
2252 2252 (lsp->ls_dkg.dkg_ncyl << lsp->ls_pbshift);
2253 2253 }
2254 2254
2255 2255 /*
2256 2256 * build vtoc - see dkio(7I)
2257 2257 *
2258 2258 * Fakes one big partition based on the size of the file. This is needed
2259 2259 * because we allow newfs'ing the traditional lofi device and newfs will
2260 2260 * do several disk ioctls to figure out the geometry and partition information.
2261 2261 * It uses that information to determine the parameters to pass to mkfs.
2262 2262 */
2263 2263 static void
2264 2264 fake_disk_vtoc(struct lofi_state *lsp, struct vtoc *vt)
2265 2265 {
2266 2266 bzero(vt, sizeof (struct vtoc));
2267 2267 vt->v_sanity = VTOC_SANE;
2268 2268 vt->v_version = V_VERSION;
2269 2269 (void) strncpy(vt->v_volume, LOFI_DRIVER_NAME,
2270 2270 sizeof (vt->v_volume));
2271 2271 vt->v_sectorsz = 1 << lsp->ls_pbshift;
2272 2272 vt->v_nparts = 1;
2273 2273 vt->v_part[0].p_tag = V_UNASSIGNED;
2274 2274
2275 2275 /*
2276 2276 * A compressed file is read-only, other files can
2277 2277 * be read-write
2278 2278 */
2279 2279 if (lsp->ls_uncomp_seg_sz > 0) {
2280 2280 vt->v_part[0].p_flag = V_UNMNT | V_RONLY;
2281 2281 } else {
2282 2282 vt->v_part[0].p_flag = V_UNMNT;
2283 2283 }
2284 2284 vt->v_part[0].p_start = (daddr_t)0;
2285 2285 /*
2286 2286 * The partition size cannot just be the number of sectors, because
2287 2287 * that might not end on a cylinder boundary. And if that's the case,
2288 2288 * newfs/mkfs will print a scary warning. So just figure the size
2289 2289 * based on the number of cylinders and sectors/cylinder.
2290 2290 */
2291 2291 vt->v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
2292 2292 lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
2293 2293 }
2294 2294
2295 2295 /*
2296 2296 * build dk_cinfo - see dkio(7I)
2297 2297 */
2298 2298 static void
2299 2299 fake_disk_info(dev_t dev, struct dk_cinfo *ci)
2300 2300 {
2301 2301 bzero(ci, sizeof (struct dk_cinfo));
2302 2302 (void) strlcpy(ci->dki_cname, LOFI_DRIVER_NAME, sizeof (ci->dki_cname));
2303 2303 ci->dki_ctype = DKC_SCSI_CCS;
2304 2304 (void) strlcpy(ci->dki_dname, LOFI_DRIVER_NAME, sizeof (ci->dki_dname));
2305 2305 ci->dki_unit = LOFI_MINOR2ID(getminor(dev));
2306 2306 ci->dki_partition = LOFI_PART(getminor(dev));
2307 2307 /*
2308 2308 * newfs uses this to set maxcontig. Must not be < 16, or it
2309 2309 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
2310 2310 * it by the block size. Then tunefs doesn't work because
2311 2311 * maxcontig is 0.
2312 2312 */
2313 2313 ci->dki_maxtransfer = 16;
2314 2314 }
2315 2315
2316 2316 /*
2317 2317 * map in a compressed file
2318 2318 *
2319 2319 * Read in the header and the index that follows.
2320 2320 *
2321 2321 * The header is as follows -
2322 2322 *
2323 2323 * Signature (name of the compression algorithm)
2324 2324 * Compression segment size (a multiple of 512)
2325 2325 * Number of index entries
2326 2326 * Size of the last block
2327 2327 * The array containing the index entries
2328 2328 *
2329 2329 * The header information is always stored in
2330 2330 * network byte order on disk.
2331 2331 */
2332 2332 static int
2333 2333 lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
2334 2334 {
2335 2335 uint32_t index_sz, header_len, i;
2336 2336 ssize_t resid;
2337 2337 enum uio_rw rw;
2338 2338 char *tbuf = buf;
2339 2339 int error;
2340 2340
2341 2341 /* The signature has already been read */
2342 2342 tbuf += sizeof (lsp->ls_comp_algorithm);
2343 2343 bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
2344 2344 lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
2345 2345
2346 2346 /*
2347 2347 * The compressed segment size must be a power of 2
2348 2348 */
2349 2349 if (lsp->ls_uncomp_seg_sz < DEV_BSIZE ||
2350 2350 !ISP2(lsp->ls_uncomp_seg_sz))
2351 2351 return (EINVAL);
2352 2352
2353 2353 for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
2354 2354 ;
2355 2355
2356 2356 lsp->ls_comp_seg_shift = i;
2357 2357
2358 2358 tbuf += sizeof (lsp->ls_uncomp_seg_sz);
2359 2359 bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
2360 2360 lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
2361 2361
2362 2362 tbuf += sizeof (lsp->ls_comp_index_sz);
2363 2363 bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
2364 2364 sizeof (lsp->ls_uncomp_last_seg_sz));
2365 2365 lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
2366 2366
2367 2367 /*
2368 2368 * Compute the total size of the uncompressed data
2369 2369 * for use in fake_disk_geometry and other calculations.
2370 2370 * Disk geometry has to be faked with respect to the
2371 2371 * actual uncompressed data size rather than the
2372 2372 * compressed file size.
2373 2373 */
2374 2374 lsp->ls_vp_size =
2375 2375 (u_offset_t)(lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
2376 2376 + lsp->ls_uncomp_last_seg_sz;
2377 2377
2378 2378 /*
2379 2379 * Index size is rounded up to DEV_BSIZE for ease
2380 2380 * of segmapping
2381 2381 */
2382 2382 index_sz = sizeof (*lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
2383 2383 header_len = sizeof (lsp->ls_comp_algorithm) +
2384 2384 sizeof (lsp->ls_uncomp_seg_sz) +
2385 2385 sizeof (lsp->ls_comp_index_sz) +
2386 2386 sizeof (lsp->ls_uncomp_last_seg_sz);
2387 2387 lsp->ls_comp_offbase = header_len + index_sz;
2388 2388
2389 2389 index_sz += header_len;
2390 2390 index_sz = roundup(index_sz, DEV_BSIZE);
2391 2391
2392 2392 lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
2393 2393 lsp->ls_comp_index_data_sz = index_sz;
2394 2394
2395 2395 /*
2396 2396 * Read in the index -- this has a side-effect
2397 2397 * of reading in the header as well
2398 2398 */
2399 2399 rw = UIO_READ;
2400 2400 error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
2401 2401 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
2402 2402
2403 2403 if (error != 0)
2404 2404 return (error);
2405 2405
2406 2406 /* Skip the header, this is where the index really begins */
2407 2407 lsp->ls_comp_seg_index =
2408 2408 /*LINTED*/
2409 2409 (uint64_t *)(lsp->ls_comp_index_data + header_len);
2410 2410
2411 2411 /*
2412 2412 * Now recompute offsets in the index to account for
2413 2413 * the header length
2414 2414 */
2415 2415 for (i = 0; i < lsp->ls_comp_index_sz; i++) {
2416 2416 lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
2417 2417 BE_64(lsp->ls_comp_seg_index[i]);
2418 2418 }
2419 2419
2420 2420 return (error);
2421 2421 }
2422 2422
2423 2423 static int
2424 2424 lofi_init_crypto(struct lofi_state *lsp, struct lofi_ioctl *klip)
2425 2425 {
2426 2426 struct crypto_meta chead;
2427 2427 char buf[DEV_BSIZE];
2428 2428 ssize_t resid;
2429 2429 char *marker;
2430 2430 int error;
2431 2431 int ret;
2432 2432 int i;
2433 2433
2434 2434 if (!klip->li_crypto_enabled)
2435 2435 return (0);
2436 2436
2437 2437 /*
2438 2438 * All current algorithms have a max of 448 bits.
2439 2439 */
2440 2440 if (klip->li_iv_len > CRYPTO_BITS2BYTES(512))
2441 2441 return (EINVAL);
2442 2442
2443 2443 if (CRYPTO_BITS2BYTES(klip->li_key_len) > sizeof (klip->li_key))
2444 2444 return (EINVAL);
2445 2445
2446 2446 lsp->ls_crypto_enabled = klip->li_crypto_enabled;
2447 2447
2448 2448 mutex_init(&lsp->ls_crypto_lock, NULL, MUTEX_DRIVER, NULL);
2449 2449
2450 2450 lsp->ls_mech.cm_type = crypto_mech2id(klip->li_cipher);
2451 2451 if (lsp->ls_mech.cm_type == CRYPTO_MECH_INVALID) {
2452 2452 cmn_err(CE_WARN, "invalid cipher %s requested for %s",
2453 2453 klip->li_cipher, klip->li_filename);
2454 2454 return (EINVAL);
2455 2455 }
2456 2456
2457 2457 /* this is just initialization here */
2458 2458 lsp->ls_mech.cm_param = NULL;
2459 2459 lsp->ls_mech.cm_param_len = 0;
2460 2460
2461 2461 lsp->ls_iv_type = klip->li_iv_type;
2462 2462 lsp->ls_iv_mech.cm_type = crypto_mech2id(klip->li_iv_cipher);
2463 2463 if (lsp->ls_iv_mech.cm_type == CRYPTO_MECH_INVALID) {
2464 2464 cmn_err(CE_WARN, "invalid iv cipher %s requested"
2465 2465 " for %s", klip->li_iv_cipher, klip->li_filename);
2466 2466 return (EINVAL);
2467 2467 }
2468 2468
2469 2469 /* iv mech must itself take a null iv */
2470 2470 lsp->ls_iv_mech.cm_param = NULL;
2471 2471 lsp->ls_iv_mech.cm_param_len = 0;
2472 2472 lsp->ls_iv_len = klip->li_iv_len;
2473 2473
2474 2474 /*
2475 2475 * Create ctx using li_cipher & the raw li_key after checking
2476 2476 * that it isn't a weak key.
2477 2477 */
2478 2478 lsp->ls_key.ck_format = CRYPTO_KEY_RAW;
2479 2479 lsp->ls_key.ck_length = klip->li_key_len;
2480 2480 lsp->ls_key.ck_data = kmem_alloc(
2481 2481 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length), KM_SLEEP);
2482 2482 bcopy(klip->li_key, lsp->ls_key.ck_data,
2483 2483 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
2484 2484
2485 2485 ret = crypto_key_check(&lsp->ls_mech, &lsp->ls_key);
2486 2486 if (ret != CRYPTO_SUCCESS) {
2487 2487 cmn_err(CE_WARN, "weak key check failed for cipher "
2488 2488 "%s on file %s (0x%x)", klip->li_cipher,
2489 2489 klip->li_filename, ret);
2490 2490 return (EINVAL);
2491 2491 }
2492 2492
2493 2493 error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE,
2494 2494 CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
2495 2495 if (error != 0)
2496 2496 return (error);
2497 2497
2498 2498 /*
2499 2499 * This is the case where the header in the lofi image is already
2500 2500 * initialized to indicate it is encrypted.
2501 2501 */
2502 2502 if (strncmp(buf, lofi_crypto_magic, sizeof (lofi_crypto_magic)) == 0) {
2503 2503 /*
2504 2504 * The encryption header information is laid out this way:
2505 2505 * 6 bytes: hex "CFLOFI"
2506 2506 * 2 bytes: version = 0 ... for now
2507 2507 * 96 bytes: reserved1 (not implemented yet)
2508 2508 * 4 bytes: data_sector = 2 ... for now
2509 2509 * more... not implemented yet
2510 2510 */
2511 2511
2512 2512 marker = buf;
2513 2513
2514 2514 /* copy the magic */
2515 2515 bcopy(marker, lsp->ls_crypto.magic,
2516 2516 sizeof (lsp->ls_crypto.magic));
2517 2517 marker += sizeof (lsp->ls_crypto.magic);
2518 2518
2519 2519 /* read the encryption version number */
2520 2520 bcopy(marker, &(lsp->ls_crypto.version),
2521 2521 sizeof (lsp->ls_crypto.version));
2522 2522 lsp->ls_crypto.version = ntohs(lsp->ls_crypto.version);
2523 2523 marker += sizeof (lsp->ls_crypto.version);
2524 2524
2525 2525 /* read a chunk of reserved data */
2526 2526 bcopy(marker, lsp->ls_crypto.reserved1,
2527 2527 sizeof (lsp->ls_crypto.reserved1));
2528 2528 marker += sizeof (lsp->ls_crypto.reserved1);
2529 2529
2530 2530 /* read block number where encrypted data begins */
2531 2531 bcopy(marker, &(lsp->ls_crypto.data_sector),
2532 2532 sizeof (lsp->ls_crypto.data_sector));
2533 2533 lsp->ls_crypto.data_sector = ntohl(lsp->ls_crypto.data_sector);
2534 2534 marker += sizeof (lsp->ls_crypto.data_sector);
2535 2535
2536 2536 /* and ignore the rest until it is implemented */
2537 2537
2538 2538 lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
2539 2539 return (0);
2540 2540 }
2541 2541
2542 2542 /*
2543 2543 * We've requested encryption, but no magic was found, so it must be
2544 2544 * a new image.
2545 2545 */
2546 2546
2547 2547 for (i = 0; i < sizeof (struct crypto_meta); i++) {
2548 2548 if (buf[i] != '\0')
2549 2549 return (EINVAL);
2550 2550 }
2551 2551
2552 2552 marker = buf;
2553 2553 bcopy(lofi_crypto_magic, marker, sizeof (lofi_crypto_magic));
2554 2554 marker += sizeof (lofi_crypto_magic);
2555 2555 chead.version = htons(LOFI_CRYPTO_VERSION);
2556 2556 bcopy(&(chead.version), marker, sizeof (chead.version));
2557 2557 marker += sizeof (chead.version);
2558 2558 marker += sizeof (chead.reserved1);
2559 2559 chead.data_sector = htonl(LOFI_CRYPTO_DATA_SECTOR);
2560 2560 bcopy(&(chead.data_sector), marker, sizeof (chead.data_sector));
2561 2561
2562 2562 /* write the header */
2563 2563 error = vn_rdwr(UIO_WRITE, lsp->ls_vp, buf, DEV_BSIZE,
2564 2564 CRYOFF, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
2565 2565 if (error != 0)
2566 2566 return (error);
2567 2567
2568 2568 /* fix things up so it looks like we read this info */
2569 2569 bcopy(lofi_crypto_magic, lsp->ls_crypto.magic,
2570 2570 sizeof (lofi_crypto_magic));
2571 2571 lsp->ls_crypto.version = LOFI_CRYPTO_VERSION;
2572 2572 lsp->ls_crypto.data_sector = LOFI_CRYPTO_DATA_SECTOR;
2573 2573 lsp->ls_crypto_offset = lsp->ls_crypto.data_sector * DEV_BSIZE;
2574 2574 return (0);
2575 2575 }
2576 2576
2577 2577 /*
2578 2578 * Check to see if the passed in signature is a valid one. If it is
2579 2579 * valid, return the index into lofi_compress_table.
2580 2580 *
2581 2581 * Return -1 if it is invalid
2582 2582 */
2583 2583 static int
2584 2584 lofi_compress_select(const char *signature)
2585 2585 {
2586 2586 int i;
2587 2587
2588 2588 for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
2589 2589 if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
2590 2590 return (i);
2591 2591 }
2592 2592
2593 2593 return (-1);
2594 2594 }
2595 2595
2596 2596 static int
2597 2597 lofi_init_compress(struct lofi_state *lsp)
2598 2598 {
2599 2599 char buf[DEV_BSIZE];
2600 2600 int compress_index;
2601 2601 ssize_t resid;
2602 2602 int error;
2603 2603
2604 2604 error = vn_rdwr(UIO_READ, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
2605 2605 0, RLIM64_INFINITY, kcred, &resid);
2606 2606
2607 2607 if (error != 0)
2608 2608 return (error);
2609 2609
2610 2610 if ((compress_index = lofi_compress_select(buf)) == -1)
2611 2611 return (0);
2612 2612
2613 2613 /* compression and encryption are mutually exclusive */
2614 2614 if (lsp->ls_crypto_enabled)
2615 2615 return (ENOTSUP);
2616 2616
2617 2617 /* initialize compression info for compressed lofi */
2618 2618 lsp->ls_comp_algorithm_index = compress_index;
2619 2619 (void) strlcpy(lsp->ls_comp_algorithm,
2620 2620 lofi_compress_table[compress_index].l_name,
2621 2621 sizeof (lsp->ls_comp_algorithm));
2622 2622
2623 2623 /* Finally setup per-thread pre-allocated buffers */
2624 2624 lsp->ls_comp_bufs = kmem_zalloc(lofi_taskq_nthreads *
2625 2625 sizeof (struct compbuf), KM_SLEEP);
2626 2626
2627 2627 return (lofi_map_compressed_file(lsp, buf));
2628 2628 }
2629 2629
2630 2630 /*
2631 2631 * Allocate new or proposed id from lofi_id.
2632 2632 *
2633 2633 * Special cases for proposed id:
2634 2634 * 0: not allowed, 0 is id for control device.
2635 2635 * -1: allocate first usable id from lofi_id.
2636 2636 * any other value is proposed value from userland
2637 2637 *
2638 2638 * returns DDI_SUCCESS or errno.
2639 2639 */
2640 2640 static int
2641 2641 lofi_alloc_id(int *idp)
2642 2642 {
2643 2643 int id, error = DDI_SUCCESS;
2644 2644
2645 2645 if (*idp == -1) {
2646 2646 id = id_allocff_nosleep(lofi_id);
2647 2647 if (id == -1) {
2648 2648 error = EAGAIN;
2649 2649 goto err;
2650 2650 }
2651 2651 } else if (*idp == 0) {
2652 2652 error = EINVAL;
2653 2653 goto err;
2654 2654 } else if (*idp > ((1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)) - 1)) {
2655 2655 error = ERANGE;
2656 2656 goto err;
2657 2657 } else {
2658 2658 if (ddi_get_soft_state(lofi_statep, *idp) != NULL) {
2659 2659 error = EEXIST;
2660 2660 goto err;
2661 2661 }
2662 2662
2663 2663 id = id_alloc_specific_nosleep(lofi_id, *idp);
2664 2664 if (id == -1) {
2665 2665 error = EAGAIN;
2666 2666 goto err;
2667 2667 }
2668 2668 }
2669 2669 *idp = id;
2670 2670 err:
2671 2671 return (error);
2672 2672 }
2673 2673
2674 2674 static int
2675 2675 lofi_create_dev(struct lofi_ioctl *klip)
2676 2676 {
2677 2677 dev_info_t *parent, *child;
2678 2678 struct lofi_state *lsp = NULL;
2679 2679 char namebuf[MAXNAMELEN];
2680 2680 int error, circ;
2681 2681
2682 2682 /* get control device */
2683 2683 lsp = ddi_get_soft_state(lofi_statep, 0);
2684 2684 parent = ddi_get_parent(lsp->ls_dip);
2685 2685
2686 2686 if ((error = lofi_alloc_id((int *)&klip->li_id)))
2687 2687 return (error);
2688 2688
2689 2689 (void) snprintf(namebuf, sizeof (namebuf), LOFI_DRIVER_NAME "@%d",
2690 2690 klip->li_id);
2691 2691
2692 2692 ndi_devi_enter(parent, &circ);
2693 2693 child = ndi_devi_findchild(parent, namebuf);
2694 2694 ndi_devi_exit(parent, circ);
2695 2695
2696 2696 if (child == NULL) {
2697 2697 child = ddi_add_child(parent, LOFI_DRIVER_NAME,
2698 2698 (pnode_t)DEVI_SID_NODEID, klip->li_id);
2699 2699 if ((error = ddi_prop_update_int(DDI_DEV_T_NONE, child,
2700 2700 "instance", klip->li_id)) != DDI_PROP_SUCCESS)
2701 2701 goto err;
2702 2702
2703 2703 if (klip->li_labeled == B_TRUE) {
2704 2704 if ((error = ddi_prop_create(DDI_DEV_T_NONE, child,
2705 2705 DDI_PROP_CANSLEEP, "labeled", 0, 0))
2706 2706 != DDI_PROP_SUCCESS)
2707 2707 goto err;
2708 2708 }
2709 2709
2710 2710 if ((error = ndi_devi_online(child, NDI_ONLINE_ATTACH))
2711 2711 != NDI_SUCCESS)
2712 2712 goto err;
2713 2713 } else {
2714 2714 id_free(lofi_id, klip->li_id);
2715 2715 error = EEXIST;
2716 2716 return (error);
2717 2717 }
2718 2718
2719 2719 goto done;
2720 2720
2721 2721 err:
2722 2722 ddi_prop_remove_all(child);
2723 2723 (void) ndi_devi_offline(child, NDI_DEVI_REMOVE);
2724 2724 id_free(lofi_id, klip->li_id);
2725 2725 done:
2726 2726
2727 2727 return (error);
2728 2728 }
2729 2729
2730 2730 static void
2731 2731 lofi_create_inquiry(struct lofi_state *lsp, struct scsi_inquiry *inq)
2732 2732 {
2733 2733 char *p = NULL;
2734 2734
2735 2735 (void) strlcpy(inq->inq_vid, LOFI_DRIVER_NAME, sizeof (inq->inq_vid));
2736 2736
2737 2737 mutex_enter(&lsp->ls_vp_lock);
2738 2738 if (lsp->ls_vp != NULL)
2739 2739 p = strrchr(lsp->ls_vp->v_path, '/');
2740 2740 if (p != NULL)
2741 2741 (void) strncpy(inq->inq_pid, p + 1, sizeof (inq->inq_pid));
2742 2742 mutex_exit(&lsp->ls_vp_lock);
2743 2743 (void) strlcpy(inq->inq_revision, "1.0", sizeof (inq->inq_revision));
2744 2744 }
2745 2745
2746 2746 /*
2747 2747 * copy devlink name from event cache
2748 2748 */
2749 2749 static void
2750 2750 lofi_copy_devpath(struct lofi_ioctl *klip)
2751 2751 {
2752 2752 int error;
2753 2753 char namebuf[MAXNAMELEN], *str;
2754 2754 clock_t ticks;
2755 2755 nvlist_t *nvl;
2756 2756
2757 2757 if (klip->li_labeled == B_TRUE)
2758 2758 klip->li_devpath[0] = '\0';
2759 2759 else {
2760 2760 /* no need to wait for messages */
2761 2761 (void) snprintf(klip->li_devpath, sizeof (klip->li_devpath),
2762 2762 "/dev/" LOFI_CHAR_NAME "/%d", klip->li_id);
2763 2763 return;
2764 2764 }
2765 2765
2766 2766 (void) snprintf(namebuf, sizeof (namebuf), "%d", klip->li_id);
2767 2767 ticks = ddi_get_lbolt() + LOFI_TIMEOUT * drv_usectohz(1000000);
2768 2768
2769 2769 nvl = NULL;
2770 2770
2771 2771 mutex_enter(&lofi_chan_lock);
2772 2772 while (nvlist_lookup_nvlist(lofi_devlink_cache, namebuf, &nvl) != 0) {
2773 2773 error = cv_timedwait(&lofi_chan_cv, &lofi_chan_lock, ticks);
2774 2774 if (error == -1)
2775 2775 break;
2776 2776 }
2777 2777
2778 2778 if (nvl != NULL) {
2779 2779 if (nvlist_lookup_string(nvl, DEV_NAME, &str) == 0) {
2780 2780 (void) strlcpy(klip->li_devpath, str,
2781 2781 sizeof (klip->li_devpath));
2782 2782 }
2783 2783 }
2784 2784 mutex_exit(&lofi_chan_lock);
2785 2785 }
2786 2786
2787 2787 /*
2788 2788 * map a file to a minor number. Return the minor number.
2789 2789 */
2790 2790 static int
2791 2791 lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
2792 2792 int *rvalp, struct cred *credp, int ioctl_flag)
2793 2793 {
2794 2794 int id = -1;
2795 2795 struct lofi_state *lsp = NULL;
2796 2796 struct lofi_ioctl *klip;
2797 2797 int error;
2798 2798 struct vnode *vp = NULL;
2799 2799 vattr_t vattr;
2800 2800 int flag;
2801 2801 char namebuf[MAXNAMELEN];
2802 2802
2803 2803 error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
2804 2804 if (error != 0)
2805 2805 return (error);
2806 2806
2807 2807 mutex_enter(&lofi_lock);
2808 2808
2809 2809 if (file_to_lofi_nocheck(klip->li_filename, klip->li_readonly,
2810 2810 NULL) == 0) {
2811 2811 error = EBUSY;
2812 2812 goto err;
2813 2813 }
2814 2814
2815 2815 flag = FREAD | FWRITE | FOFFMAX | FEXCL;
2816 2816 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
2817 2817 if (error) {
2818 2818 /* try read-only */
2819 2819 flag &= ~FWRITE;
2820 2820 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
2821 2821 &vp, 0, 0);
2822 2822 if (error)
2823 2823 goto err;
2824 2824 }
2825 2825
2826 2826 if (!V_ISLOFIABLE(vp->v_type)) {
2827 2827 error = EINVAL;
2828 2828 goto err;
2829 2829 }
2830 2830
2831 2831 vattr.va_mask = AT_SIZE;
2832 2832 error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
2833 2833 if (error)
2834 2834 goto err;
2835 2835
2836 2836 /* the file needs to be a multiple of the block size */
2837 2837 if ((vattr.va_size % DEV_BSIZE) != 0) {
2838 2838 error = EINVAL;
2839 2839 goto err;
2840 2840 }
2841 2841
2842 2842 if (pickminor) {
2843 2843 klip->li_id = (uint32_t)-1;
2844 2844 }
2845 2845 if ((error = lofi_create_dev(klip)) != 0)
2846 2846 goto err;
2847 2847
2848 2848 id = klip->li_id;
2849 2849 lsp = ddi_get_soft_state(lofi_statep, id);
2850 2850 if (lsp == NULL)
2851 2851 goto err;
2852 2852
2853 2853 /*
2854 2854 * from this point lofi_destroy() is used to clean up on error
2855 2855 * make sure the basic data is set
2856 2856 */
2857 2857 lsp->ls_dev = makedevice(getmajor(dev), LOFI_ID2MINOR(id));
2858 2858
2859 2859 list_create(&lsp->ls_comp_cache, sizeof (struct lofi_comp_cache),
2860 2860 offsetof(struct lofi_comp_cache, lc_list));
2861 2861
2862 2862 /*
2863 2863 * save open mode so file can be closed properly and vnode counts
2864 2864 * updated correctly.
2865 2865 */
2866 2866 lsp->ls_openflag = flag;
2867 2867
2868 2868 lsp->ls_vp = vp;
2869 2869 lsp->ls_stacked_vp = vp;
2870 2870
2871 2871 lsp->ls_vp_size = vattr.va_size;
2872 2872 lsp->ls_vp_comp_size = lsp->ls_vp_size;
2873 2873
2874 2874 /*
2875 2875 * Try to handle stacked lofs vnodes.
2876 2876 */
2877 2877 if (vp->v_type == VREG) {
2878 2878 vnode_t *realvp;
2879 2879
2880 2880 if (VOP_REALVP(vp, &realvp, NULL) == 0) {
2881 2881 /*
2882 2882 * We need to use the realvp for uniqueness
2883 2883 * checking, but keep the stacked vp for
2884 2884 * LOFI_GET_FILENAME display.
2885 2885 */
2886 2886 VN_HOLD(realvp);
2887 2887 lsp->ls_vp = realvp;
2888 2888 }
2889 2889 }
2890 2890
2891 2891 lsp->ls_lbshift = highbit(DEV_BSIZE) - 1;
2892 2892 lsp->ls_pbshift = lsp->ls_lbshift;
2893 2893
2894 2894 lsp->ls_readonly = klip->li_readonly;
2895 2895 lsp->ls_uncomp_seg_sz = 0;
2896 2896 lsp->ls_comp_algorithm[0] = '\0';
2897 2897 lsp->ls_crypto_offset = 0;
2898 2898
2899 2899 (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
2900 2900 LOFI_DRIVER_NAME, id);
2901 2901 lsp->ls_taskq = taskq_create_proc(namebuf, lofi_taskq_nthreads,
2902 2902 minclsyspri, 1, lofi_taskq_maxalloc, curzone->zone_zsched, 0);
2903 2903
2904 2904 if ((error = lofi_init_crypto(lsp, klip)) != 0)
2905 2905 goto err;
2906 2906
2907 2907 if ((error = lofi_init_compress(lsp)) != 0)
2908 2908 goto err;
2909 2909
2910 2910 fake_disk_geometry(lsp);
2911 2911
2912 2912 if ((ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip, SIZE_PROP_NAME,
2913 2913 lsp->ls_vp_size - lsp->ls_crypto_offset)) != DDI_PROP_SUCCESS) {
2914 2914 error = EINVAL;
2915 2915 goto err;
2916 2916 }
2917 2917
2918 2918 if ((ddi_prop_update_int64(lsp->ls_dev, lsp->ls_dip, NBLOCKS_PROP_NAME,
2919 2919 (lsp->ls_vp_size - lsp->ls_crypto_offset) / DEV_BSIZE))
2920 2920 != DDI_PROP_SUCCESS) {
2921 2921 error = EINVAL;
2922 2922 goto err;
2923 2923 }
2924 2924
2925 2925 list_insert_tail(&lofi_list, lsp);
2926 2926 /*
2927 2927 * Notify we are ready to rock.
2928 2928 */
2929 2929 mutex_enter(&lsp->ls_vp_lock);
2930 2930 lsp->ls_vp_ready = B_TRUE;
2931 2931 cv_broadcast(&lsp->ls_vp_cv);
2932 2932 mutex_exit(&lsp->ls_vp_lock);
2933 2933 mutex_exit(&lofi_lock);
2934 2934
2935 2935 lofi_copy_devpath(klip);
2936 2936
2937 2937 if (rvalp)
2938 2938 *rvalp = id;
2939 2939 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
2940 2940 free_lofi_ioctl(klip);
2941 2941 return (0);
2942 2942
2943 2943 err:
2944 2944 if (lsp != NULL) {
2945 2945 lofi_destroy(lsp, credp);
2946 2946 } else {
2947 2947 if (vp != NULL) {
2948 2948 (void) VOP_PUTPAGE(vp, 0, 0, B_INVAL, credp, NULL);
2949 2949 (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
2950 2950 VN_RELE(vp);
2951 2951 }
2952 2952 }
2953 2953
2954 2954 mutex_exit(&lofi_lock);
2955 2955 free_lofi_ioctl(klip);
2956 2956 return (error);
2957 2957 }
2958 2958
2959 2959 /*
2960 2960 * unmap a file.
2961 2961 */
2962 2962 static int
2963 2963 lofi_unmap_file(struct lofi_ioctl *ulip, int byfilename,
2964 2964 struct cred *credp, int ioctl_flag)
2965 2965 {
2966 2966 struct lofi_state *lsp;
2967 2967 struct lofi_ioctl *klip;
2968 2968 nvlist_t *nvl = NULL;
2969 2969 clock_t ticks;
2970 2970 char name[MAXNAMELEN];
2971 2971 int err;
2972 2972
2973 2973 err = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
2974 2974 if (err != 0)
2975 2975 return (err);
2976 2976
2977 2977 mutex_enter(&lofi_lock);
2978 2978 if (byfilename) {
2979 2979 if ((err = file_to_lofi(klip->li_filename, klip->li_readonly,
2980 2980 &lsp)) != 0) {
2981 2981 mutex_exit(&lofi_lock);
2982 2982 return (err);
2983 2983 }
2984 2984 } else if (klip->li_id == 0) {
2985 2985 mutex_exit(&lofi_lock);
2986 2986 free_lofi_ioctl(klip);
2987 2987 return (ENXIO);
2988 2988 } else {
2989 2989 lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
2990 2990 }
2991 2991
2992 2992 if (lsp == NULL || lsp->ls_vp == NULL || lofi_access(lsp) != 0) {
2993 2993 mutex_exit(&lofi_lock);
2994 2994 free_lofi_ioctl(klip);
2995 2995 return (ENXIO);
2996 2996 }
2997 2997
2998 2998 klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
2999 2999
3000 3000 /*
3001 3001 * If it's still held open, we'll do one of three things:
3002 3002 *
3003 3003 * If no flag is set, just return EBUSY.
3004 3004 *
3005 3005 * If the 'cleanup' flag is set, unmap and remove the device when
3006 3006 * the last user finishes.
3007 3007 *
3008 3008 * If the 'force' flag is set, then we forcibly close the underlying
3009 3009 * file. Subsequent operations will fail, and the DKIOCSTATE ioctl
3010 3010 * will return DKIO_DEV_GONE. When the device is last closed, the
3011 3011 * device will be cleaned up appropriately.
3012 3012 *
3013 3013 * This is complicated by the fact that we may have outstanding
3014 3014 * dispatched I/Os. Rather than having a single mutex to serialize all
3015 3015 * I/O, we keep a count of the number of outstanding I/O requests
3016 3016 * (ls_vp_iocount), as well as a flag to indicate that no new I/Os
3017 3017 * should be dispatched (ls_vp_closereq).
3018 3018 *
3019 3019 * We set the flag, wait for the number of outstanding I/Os to reach 0,
3020 3020 * and then close the underlying vnode.
3021 3021 */
3022 3022 if (is_opened(lsp)) {
3023 3023 if (klip->li_force) {
3024 3024 mutex_enter(&lsp->ls_vp_lock);
3025 3025 lsp->ls_vp_closereq = B_TRUE;
3026 3026 /* wake up any threads waiting on dkiocstate */
3027 3027 cv_broadcast(&lsp->ls_vp_cv);
3028 3028 while (lsp->ls_vp_iocount > 0)
3029 3029 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
3030 3030 mutex_exit(&lsp->ls_vp_lock);
3031 3031
3032 3032 goto out;
3033 3033 } else if (klip->li_cleanup) {
3034 3034 lsp->ls_cleanup = 1;
3035 3035 mutex_exit(&lofi_lock);
3036 3036 free_lofi_ioctl(klip);
3037 3037 return (0);
3038 3038 }
3039 3039
3040 3040 mutex_exit(&lofi_lock);
3041 3041 free_lofi_ioctl(klip);
3042 3042 return (EBUSY);
3043 3043 }
3044 3044
3045 3045 out:
3046 3046 lofi_free_dev(lsp);
3047 3047 lofi_destroy(lsp, credp);
3048 3048
3049 3049 /*
3050 3050 * check the lofi_devlink_cache if device is really gone.
3051 3051 * note: we just wait for timeout here and dont give error if
3052 3052 * timer will expire. This check is to try to ensure the unmap is
3053 3053 * really done when lofiadm -d completes.
3054 3054 * Since lofi_lock is held, also hopefully the lofiadm -a calls
3055 3055 * wont interfere the the unmap.
3056 3056 */
3057 3057 (void) snprintf(name, sizeof (name), "%d", klip->li_id);
3058 3058 ticks = ddi_get_lbolt() + LOFI_TIMEOUT * drv_usectohz(1000000);
3059 3059 mutex_enter(&lofi_chan_lock);
3060 3060 while (nvlist_lookup_nvlist(lofi_devlink_cache, name, &nvl) == 0) {
3061 3061 err = cv_timedwait(&lofi_chan_cv, &lofi_chan_lock, ticks);
3062 3062 if (err == -1)
3063 3063 break;
3064 3064 }
3065 3065 mutex_exit(&lofi_chan_lock);
3066 3066
3067 3067 mutex_exit(&lofi_lock);
3068 3068 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
3069 3069 free_lofi_ioctl(klip);
3070 3070 return (0);
3071 3071 }
3072 3072
3073 3073 /*
3074 3074 * get the filename given the minor number, or the minor number given
3075 3075 * the name.
3076 3076 */
3077 3077 /*ARGSUSED*/
3078 3078 static int
3079 3079 lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
3080 3080 struct cred *credp, int ioctl_flag)
3081 3081 {
3082 3082 struct lofi_ioctl *klip;
3083 3083 struct lofi_state *lsp;
3084 3084 int error;
3085 3085
3086 3086 error = copy_in_lofi_ioctl(ulip, &klip, ioctl_flag);
3087 3087 if (error != 0)
3088 3088 return (error);
3089 3089
3090 3090 switch (which) {
3091 3091 case LOFI_GET_FILENAME:
3092 3092 if (klip->li_id == 0) {
3093 3093 free_lofi_ioctl(klip);
3094 3094 return (EINVAL);
3095 3095 }
3096 3096
3097 3097 mutex_enter(&lofi_lock);
3098 3098 lsp = ddi_get_soft_state(lofi_statep, klip->li_id);
3099 3099 if (lsp == NULL || lofi_access(lsp) != 0) {
3100 3100 mutex_exit(&lofi_lock);
3101 3101 free_lofi_ioctl(klip);
3102 3102 return (ENXIO);
3103 3103 }
3104 3104
3105 3105 /*
3106 3106 * This may fail if, for example, we're trying to look
3107 3107 * up a zoned NFS path from the global zone.
3108 3108 */
3109 3109 if (vnodetopath(NULL, lsp->ls_stacked_vp, klip->li_filename,
3110 3110 sizeof (klip->li_filename), CRED()) != 0) {
3111 3111 (void) strlcpy(klip->li_filename, "?",
3112 3112 sizeof (klip->li_filename));
3113 3113 }
3114 3114
3115 3115 klip->li_readonly = lsp->ls_readonly;
3116 3116 klip->li_labeled = lsp->ls_cmlbhandle != NULL;
3117 3117
3118 3118 (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
3119 3119 sizeof (klip->li_algorithm));
3120 3120 klip->li_crypto_enabled = lsp->ls_crypto_enabled;
3121 3121 mutex_exit(&lofi_lock);
3122 3122
3123 3123 lofi_copy_devpath(klip);
3124 3124 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
3125 3125 free_lofi_ioctl(klip);
3126 3126 return (error);
3127 3127 case LOFI_GET_MINOR:
3128 3128 mutex_enter(&lofi_lock);
3129 3129 error = file_to_lofi(klip->li_filename,
3130 3130 klip->li_readonly, &lsp);
3131 3131 if (error != 0) {
3132 3132 mutex_exit(&lofi_lock);
3133 3133 free_lofi_ioctl(klip);
3134 3134 return (error);
3135 3135 }
3136 3136 klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
3137 3137
3138 3138 klip->li_readonly = lsp->ls_readonly;
3139 3139 klip->li_labeled = lsp->ls_cmlbhandle != NULL;
3140 3140 mutex_exit(&lofi_lock);
3141 3141
3142 3142 lofi_copy_devpath(klip);
3143 3143 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
3144 3144
3145 3145 free_lofi_ioctl(klip);
3146 3146 return (error);
3147 3147 case LOFI_CHECK_COMPRESSED:
3148 3148 mutex_enter(&lofi_lock);
3149 3149 error = file_to_lofi(klip->li_filename,
3150 3150 klip->li_readonly, &lsp);
3151 3151 if (error != 0) {
3152 3152 mutex_exit(&lofi_lock);
3153 3153 free_lofi_ioctl(klip);
3154 3154 return (error);
3155 3155 }
3156 3156
3157 3157 klip->li_id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
3158 3158 (void) strlcpy(klip->li_algorithm, lsp->ls_comp_algorithm,
3159 3159 sizeof (klip->li_algorithm));
3160 3160
3161 3161 mutex_exit(&lofi_lock);
3162 3162 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
3163 3163 free_lofi_ioctl(klip);
3164 3164 return (error);
3165 3165 default:
3166 3166 free_lofi_ioctl(klip);
3167 3167 return (EINVAL);
3168 3168 }
3169 3169 }
3170 3170
3171 3171 static int
3172 3172 uscsi_is_inquiry(intptr_t arg, int flag, union scsi_cdb *cdb,
3173 3173 struct uscsi_cmd *uscmd)
3174 3174 {
3175 3175 int rval;
3176 3176
3177 3177 #ifdef _MULTI_DATAMODEL
3178 3178 switch (ddi_model_convert_from(flag & FMODELS)) {
3179 3179 case DDI_MODEL_ILP32: {
3180 3180 struct uscsi_cmd32 ucmd32;
3181 3181
3182 3182 if (ddi_copyin((void *)arg, &ucmd32, sizeof (ucmd32), flag)) {
3183 3183 rval = EFAULT;
3184 3184 goto err;
3185 3185 }
3186 3186 uscsi_cmd32touscsi_cmd((&ucmd32), uscmd);
3187 3187 break;
3188 3188 }
3189 3189 case DDI_MODEL_NONE:
3190 3190 if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3191 3191 rval = EFAULT;
3192 3192 goto err;
3193 3193 }
3194 3194 break;
3195 3195 default:
3196 3196 rval = EFAULT;
3197 3197 goto err;
3198 3198 }
3199 3199 #else
3200 3200 if (ddi_copyin((void *)arg, uscmd, sizeof (*uscmd), flag)) {
3201 3201 rval = EFAULT;
3202 3202 goto err;
3203 3203 }
3204 3204 #endif /* _MULTI_DATAMODEL */
3205 3205 if (ddi_copyin(uscmd->uscsi_cdb, cdb, uscmd->uscsi_cdblen, flag)) {
3206 3206 rval = EFAULT;
3207 3207 goto err;
3208 3208 }
3209 3209 if (cdb->scc_cmd == SCMD_INQUIRY) {
3210 3210 return (0);
3211 3211 }
3212 3212 err:
3213 3213 return (rval);
3214 3214 }
3215 3215
3216 3216 static int
3217 3217 lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
3218 3218 int *rvalp)
3219 3219 {
3220 3220 int error;
3221 3221 enum dkio_state dkstate;
3222 3222 struct lofi_state *lsp;
3223 3223 int id;
3224 3224
3225 3225 id = LOFI_MINOR2ID(getminor(dev));
3226 3226
3227 3227 /* lofi ioctls only apply to the master device */
3228 3228 if (id == 0) {
3229 3229 struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
3230 3230
3231 3231 /*
3232 3232 * the query command only need read-access - i.e., normal
3233 3233 * users are allowed to do those on the ctl device as
3234 3234 * long as they can open it read-only.
3235 3235 */
3236 3236 switch (cmd) {
3237 3237 case LOFI_MAP_FILE:
3238 3238 if ((flag & FWRITE) == 0)
3239 3239 return (EPERM);
3240 3240 return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
3241 3241 case LOFI_MAP_FILE_MINOR:
3242 3242 if ((flag & FWRITE) == 0)
3243 3243 return (EPERM);
3244 3244 return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
3245 3245 case LOFI_UNMAP_FILE:
3246 3246 if ((flag & FWRITE) == 0)
3247 3247 return (EPERM);
3248 3248 return (lofi_unmap_file(lip, 1, credp, flag));
3249 3249 case LOFI_UNMAP_FILE_MINOR:
3250 3250 if ((flag & FWRITE) == 0)
3251 3251 return (EPERM);
3252 3252 return (lofi_unmap_file(lip, 0, credp, flag));
3253 3253 case LOFI_GET_FILENAME:
3254 3254 return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
3255 3255 credp, flag));
3256 3256 case LOFI_GET_MINOR:
3257 3257 return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
3258 3258 credp, flag));
3259 3259
3260 3260 /*
3261 3261 * This API made limited sense when this value was fixed
3262 3262 * at LOFI_MAX_FILES. However, its use to iterate
3263 3263 * across all possible devices in lofiadm means we don't
3264 3264 * want to return L_MAXMIN, but the highest
3265 3265 * *allocated* id.
3266 3266 */
3267 3267 case LOFI_GET_MAXMINOR:
3268 3268 id = 0;
3269 3269
3270 3270 mutex_enter(&lofi_lock);
3271 3271
3272 3272 for (lsp = list_head(&lofi_list); lsp != NULL;
3273 3273 lsp = list_next(&lofi_list, lsp)) {
3274 3274 int i;
3275 3275 if (lofi_access(lsp) != 0)
3276 3276 continue;
3277 3277
3278 3278 i = ddi_get_instance(lsp->ls_dip);
3279 3279 if (i > id)
3280 3280 id = i;
3281 3281 }
3282 3282
3283 3283 mutex_exit(&lofi_lock);
3284 3284
3285 3285 error = ddi_copyout(&id, &lip->li_id,
3286 3286 sizeof (id), flag);
3287 3287 if (error)
3288 3288 return (EFAULT);
3289 3289 return (0);
3290 3290
3291 3291 case LOFI_CHECK_COMPRESSED:
3292 3292 return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
3293 3293 credp, flag));
3294 3294 default:
3295 3295 return (EINVAL);
3296 3296 }
3297 3297 }
3298 3298
3299 3299 mutex_enter(&lofi_lock);
3300 3300 lsp = ddi_get_soft_state(lofi_statep, id);
3301 3301 if (lsp == NULL || lsp->ls_vp_closereq) {
3302 3302 mutex_exit(&lofi_lock);
3303 3303 return (ENXIO);
3304 3304 }
3305 3305 mutex_exit(&lofi_lock);
3306 3306
3307 3307 if (ddi_prop_exists(DDI_DEV_T_ANY, lsp->ls_dip, DDI_PROP_DONTPASS,
3308 3308 "labeled") == 1) {
3309 3309 error = cmlb_ioctl(lsp->ls_cmlbhandle, dev, cmd, arg, flag,
3310 3310 credp, rvalp, 0);
3311 3311 if (error != ENOTTY)
3312 3312 return (error);
3313 3313 }
3314 3314
3315 3315 /*
3316 3316 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
3317 3317 * EIO as if the device was no longer present.
3318 3318 */
3319 3319 if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
3320 3320 return (EIO);
3321 3321
3322 3322 /* these are for faking out utilities like newfs */
3323 3323 switch (cmd) {
3324 3324 case DKIOCGMEDIAINFO:
3325 3325 case DKIOCGMEDIAINFOEXT: {
3326 3326 struct dk_minfo_ext media_info;
3327 3327 int shift = lsp->ls_lbshift;
3328 3328 int size;
3329 3329
3330 3330 if (cmd == DKIOCGMEDIAINFOEXT) {
3331 3331 media_info.dki_pbsize = 1U << lsp->ls_pbshift;
3332 3332 size = sizeof (struct dk_minfo_ext);
3333 3333 } else {
3334 3334 size = sizeof (struct dk_minfo);
3335 3335 }
3336 3336
3337 3337 media_info.dki_media_type = DK_FIXED_DISK;
3338 3338 media_info.dki_lbsize = 1U << shift;
3339 3339 media_info.dki_capacity =
3340 3340 (lsp->ls_vp_size - lsp->ls_crypto_offset) >> shift;
3341 3341
3342 3342 if (ddi_copyout(&media_info, (void *)arg, size, flag))
3343 3343 return (EFAULT);
3344 3344 return (0);
3345 3345 }
3346 3346 case DKIOCREMOVABLE: {
3347 3347 int i = 0;
3348 3348 if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), flag))
3349 3349 return (EFAULT);
3350 3350 return (0);
3351 3351 }
3352 3352
3353 3353 case DKIOCGVTOC: {
3354 3354 struct vtoc vt;
3355 3355 fake_disk_vtoc(lsp, &vt);
3356 3356
3357 3357 switch (ddi_model_convert_from(flag & FMODELS)) {
3358 3358 case DDI_MODEL_ILP32: {
3359 3359 struct vtoc32 vtoc32;
3360 3360
3361 3361 vtoctovtoc32(vt, vtoc32);
3362 3362 if (ddi_copyout(&vtoc32, (void *)arg,
3363 3363 sizeof (struct vtoc32), flag))
3364 3364 return (EFAULT);
3365 3365 break;
3366 3366 }
3367 3367
3368 3368 case DDI_MODEL_NONE:
3369 3369 if (ddi_copyout(&vt, (void *)arg,
3370 3370 sizeof (struct vtoc), flag))
3371 3371 return (EFAULT);
3372 3372 break;
3373 3373 }
3374 3374 return (0);
3375 3375 }
3376 3376 case DKIOCINFO: {
3377 3377 struct dk_cinfo ci;
3378 3378 fake_disk_info(dev, &ci);
3379 3379 if (ddi_copyout(&ci, (void *)arg, sizeof (ci), flag))
3380 3380 return (EFAULT);
3381 3381 return (0);
3382 3382 }
3383 3383 case DKIOCG_VIRTGEOM:
3384 3384 case DKIOCG_PHYGEOM:
3385 3385 case DKIOCGGEOM:
3386 3386 error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
3387 3387 sizeof (struct dk_geom), flag);
3388 3388 if (error)
3389 3389 return (EFAULT);
3390 3390 return (0);
3391 3391 case DKIOCSTATE:
3392 3392 /*
3393 3393 * Normally, lofi devices are always in the INSERTED state. If
3394 3394 * a device is forcefully unmapped, then the device transitions
3395 3395 * to the DKIO_DEV_GONE state.
3396 3396 */
3397 3397 if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
3398 3398 flag) != 0)
3399 3399 return (EFAULT);
3400 3400
3401 3401 mutex_enter(&lsp->ls_vp_lock);
3402 3402 lsp->ls_vp_iocount++;
3403 3403 while (((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
3404 3404 (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) &&
3405 3405 !lsp->ls_vp_closereq) {
3406 3406 /*
3407 3407 * By virtue of having the device open, we know that
3408 3408 * 'lsp' will remain valid when we return.
3409 3409 */
3410 3410 if (!cv_wait_sig(&lsp->ls_vp_cv,
3411 3411 &lsp->ls_vp_lock)) {
3412 3412 lsp->ls_vp_iocount--;
3413 3413 cv_broadcast(&lsp->ls_vp_cv);
3414 3414 mutex_exit(&lsp->ls_vp_lock);
3415 3415 return (EINTR);
3416 3416 }
3417 3417 }
3418 3418
3419 3419 dkstate = (!lsp->ls_vp_closereq && lsp->ls_vp != NULL ?
3420 3420 DKIO_INSERTED : DKIO_DEV_GONE);
3421 3421 lsp->ls_vp_iocount--;
3422 3422 cv_broadcast(&lsp->ls_vp_cv);
3423 3423 mutex_exit(&lsp->ls_vp_lock);
3424 3424
3425 3425 if (ddi_copyout(&dkstate, (void *)arg,
3426 3426 sizeof (dkstate), flag) != 0)
3427 3427 return (EFAULT);
3428 3428 return (0);
3429 3429 case USCSICMD: {
3430 3430 struct uscsi_cmd uscmd;
3431 3431 union scsi_cdb cdb;
3432 3432
3433 3433 if (uscsi_is_inquiry(arg, flag, &cdb, &uscmd) == 0) {
3434 3434 struct scsi_inquiry inq = {0};
3435 3435
3436 3436 lofi_create_inquiry(lsp, &inq);
3437 3437 if (ddi_copyout(&inq, uscmd.uscsi_bufaddr,
3438 3438 uscmd.uscsi_buflen, flag) != 0)
3439 3439 return (EFAULT);
3440 3440 return (0);
3441 3441 } else if (cdb.scc_cmd == SCMD_READ_CAPACITY) {
3442 3442 struct scsi_capacity capacity;
3443 3443
3444 3444 capacity.capacity =
3445 3445 BE_32((lsp->ls_vp_size - lsp->ls_crypto_offset) >>
3446 3446 lsp->ls_lbshift);
3447 3447 capacity.lbasize = BE_32(1 << lsp->ls_lbshift);
3448 3448 if (ddi_copyout(&capacity, uscmd.uscsi_bufaddr,
3449 3449 uscmd.uscsi_buflen, flag) != 0)
3450 3450 return (EFAULT);
3451 3451 return (0);
3452 3452 }
3453 3453
3454 3454 uscmd.uscsi_rqstatus = 0xff;
3455 3455 #ifdef _MULTI_DATAMODEL
3456 3456 switch (ddi_model_convert_from(flag & FMODELS)) {
3457 3457 case DDI_MODEL_ILP32: {
3458 3458 struct uscsi_cmd32 ucmd32;
3459 3459 uscsi_cmdtouscsi_cmd32((&uscmd), (&ucmd32));
3460 3460 if (ddi_copyout(&ucmd32, (void *)arg, sizeof (ucmd32),
3461 3461 flag) != 0)
3462 3462 return (EFAULT);
3463 3463 break;
3464 3464 }
3465 3465 case DDI_MODEL_NONE:
3466 3466 if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd),
3467 3467 flag) != 0)
3468 3468 return (EFAULT);
3469 3469 break;
3470 3470 default:
3471 3471 return (EFAULT);
3472 3472 }
3473 3473 #else
3474 3474 if (ddi_copyout(&uscmd, (void *)arg, sizeof (uscmd), flag) != 0)
3475 3475 return (EFAULT);
3476 3476 #endif /* _MULTI_DATAMODEL */
3477 3477 return (0);
3478 3478 }
3479 3479 default:
3480 3480 #ifdef DEBUG
3481 3481 cmn_err(CE_WARN, "lofi_ioctl: %d is not implemented\n", cmd);
3482 3482 #endif /* DEBUG */
3483 3483 return (ENOTTY);
3484 3484 }
3485 3485 }
3486 3486
3487 3487 static int
3488 3488 lofi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
3489 3489 char *name, caddr_t valuep, int *lengthp)
3490 3490 {
3491 3491 struct lofi_state *lsp;
3492 3492
3493 3493 lsp = ddi_get_soft_state(lofi_statep, ddi_get_instance(dip));
3494 3494 if (lsp == NULL) {
3495 3495 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
3496 3496 name, valuep, lengthp));
3497 3497 }
3498 3498
3499 3499 return (cmlb_prop_op(lsp->ls_cmlbhandle, dev, dip, prop_op, mod_flags,
3500 3500 name, valuep, lengthp, LOFI_PART(getminor(dev)), NULL));
3501 3501 }
3502 3502
3503 3503 static struct cb_ops lofi_cb_ops = {
3504 3504 lofi_open, /* open */
3505 3505 lofi_close, /* close */
3506 3506 lofi_strategy, /* strategy */
3507 3507 nodev, /* print */
3508 3508 nodev, /* dump */
3509 3509 lofi_read, /* read */
3510 3510 lofi_write, /* write */
3511 3511 lofi_ioctl, /* ioctl */
3512 3512 nodev, /* devmap */
3513 3513 nodev, /* mmap */
3514 3514 nodev, /* segmap */
3515 3515 nochpoll, /* poll */
3516 3516 lofi_prop_op, /* prop_op */
3517 3517 0, /* streamtab */
3518 3518 D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */
3519 3519 CB_REV,
3520 3520 lofi_aread,
3521 3521 lofi_awrite
3522 3522 };
3523 3523
3524 3524 static struct dev_ops lofi_ops = {
3525 3525 DEVO_REV, /* devo_rev, */
3526 3526 0, /* refcnt */
3527 3527 lofi_info, /* info */
3528 3528 nulldev, /* identify */
3529 3529 nulldev, /* probe */
3530 3530 lofi_attach, /* attach */
3531 3531 lofi_detach, /* detach */
3532 3532 nodev, /* reset */
3533 3533 &lofi_cb_ops, /* driver operations */
3534 3534 NULL, /* no bus operations */
3535 3535 NULL, /* power */
3536 3536 ddi_quiesce_not_needed, /* quiesce */
↓ open down ↓ |
3536 lines elided |
↑ open up ↑ |
3537 3537 };
3538 3538
3539 3539 static struct modldrv modldrv = {
3540 3540 &mod_driverops,
3541 3541 "loopback file driver",
3542 3542 &lofi_ops,
3543 3543 };
3544 3544
3545 3545 static struct modlinkage modlinkage = {
3546 3546 MODREV_1,
3547 - &modldrv,
3548 - NULL
3547 + { &modldrv, NULL }
3549 3548 };
3550 3549
3551 3550 int
3552 3551 _init(void)
3553 3552 {
3554 3553 int error;
3555 3554
3556 3555 list_create(&lofi_list, sizeof (struct lofi_state),
3557 3556 offsetof(struct lofi_state, ls_list));
3558 3557
3559 3558 error = ddi_soft_state_init((void **)&lofi_statep,
3560 3559 sizeof (struct lofi_state), 0);
3561 3560 if (error) {
3562 3561 list_destroy(&lofi_list);
3563 3562 return (error);
3564 3563 }
3565 3564
3566 3565 /*
3567 3566 * The minor number is stored as id << LOFI_CMLB_SHIFT as
3568 3567 * we need to reserve space for cmlb minor numbers.
3569 3568 * This will leave out 4096 id values on 32bit kernel, which should
3570 3569 * still suffice.
3571 3570 */
3572 3571 lofi_id = id_space_create("lofi_id", 1,
3573 3572 (1 << (L_BITSMINOR - LOFI_CMLB_SHIFT)));
3574 3573
3575 3574 if (lofi_id == NULL) {
3576 3575 ddi_soft_state_fini((void **)&lofi_statep);
3577 3576 list_destroy(&lofi_list);
3578 3577 return (DDI_FAILURE);
3579 3578 }
3580 3579
3581 3580 mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
3582 3581 mutex_init(&lofi_chan_lock, NULL, MUTEX_DRIVER, NULL);
3583 3582 cv_init(&lofi_chan_cv, NULL, CV_DRIVER, NULL);
3584 3583 error = nvlist_alloc(&lofi_devlink_cache, NV_UNIQUE_NAME, KM_SLEEP);
3585 3584
3586 3585 if (error == 0)
3587 3586 error = mod_install(&modlinkage);
3588 3587 if (error) {
3589 3588 id_space_destroy(lofi_id);
3590 3589 if (lofi_devlink_cache != NULL)
3591 3590 nvlist_free(lofi_devlink_cache);
3592 3591 mutex_destroy(&lofi_chan_lock);
3593 3592 cv_destroy(&lofi_chan_cv);
3594 3593 mutex_destroy(&lofi_lock);
3595 3594 ddi_soft_state_fini((void **)&lofi_statep);
3596 3595 list_destroy(&lofi_list);
3597 3596 }
3598 3597
3599 3598 return (error);
3600 3599 }
3601 3600
3602 3601 int
3603 3602 _fini(void)
3604 3603 {
3605 3604 int error;
3606 3605
3607 3606 mutex_enter(&lofi_lock);
3608 3607
3609 3608 if (!list_is_empty(&lofi_list)) {
3610 3609 mutex_exit(&lofi_lock);
3611 3610 return (EBUSY);
3612 3611 }
3613 3612
3614 3613 mutex_exit(&lofi_lock);
3615 3614
3616 3615 error = mod_remove(&modlinkage);
3617 3616 if (error)
3618 3617 return (error);
3619 3618
3620 3619 mutex_enter(&lofi_chan_lock);
3621 3620 nvlist_free(lofi_devlink_cache);
3622 3621 lofi_devlink_cache = NULL;
3623 3622 mutex_exit(&lofi_chan_lock);
3624 3623
3625 3624 mutex_destroy(&lofi_chan_lock);
3626 3625 cv_destroy(&lofi_chan_cv);
3627 3626 mutex_destroy(&lofi_lock);
3628 3627 id_space_destroy(lofi_id);
3629 3628 ddi_soft_state_fini((void **)&lofi_statep);
3630 3629 list_destroy(&lofi_list);
3631 3630
3632 3631 return (error);
3633 3632 }
3634 3633
3635 3634 int
3636 3635 _info(struct modinfo *modinfop)
3637 3636 {
3638 3637 return (mod_info(&modlinkage, modinfop));
3639 3638 }
↓ open down ↓ |
81 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX