blob: 95f4cd7254acf55be0888c348d0c2dff0d451630 [file] [log] [blame]
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
heppobd07e072006-03-22 10:04:02 -08005 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07007 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
batschul44a1e322010-05-10 08:19:18 -070022 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
Alexander Eremincd69fab2013-09-23 10:41:22 +040023 *
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
Andrey Sokolova1b1a2a2016-03-10 22:31:08 +030025 * Copyright (c) 2016 Andrey Sokolov
Toomas Soome406fc512015-10-23 09:36:11 +030026 * Copyright 2016 Toomas Soome <tsoome@me.com>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070027 */
28
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070029/*
30 * lofi (loopback file) driver - allows you to attach a file to a device,
31 * which can then be accessed through that device. The simple model is that
32 * you tell lofi to open a file, and then use the block device you get as
33 * you would any block device. lofi translates access to the block device
34 * into I/O on the underlying file. This is mostly useful for
35 * mounting images of filesystems.
36 *
37 * lofi is controlled through /dev/lofictl - this is the only device exported
Toomas Soome406fc512015-10-23 09:36:11 +030038 * during attach, and is instance number 0. lofiadm communicates with lofi
39 * through ioctls on this device. When a file is attached to lofi, block and
40 * character devices are exported in /dev/lofi and /dev/rlofi. These devices
41 * are identified by lofi instance number, and the instance number is also used
42 * as the name in /dev/lofi.
43 *
44 * Virtual disks, or, labeled lofi, implements virtual disk support to
45 * support partition table and related tools. Such mappings will cause
46 * block and character devices to be exported in /dev/dsk and /dev/rdsk
47 * directories.
48 *
49 * To support virtual disks, the instance number space is divided to two
50 * parts, upper part for instance number and lower part for minor number
51 * space to identify partitions and slices. The virtual disk support is
52 * implemented by stacking cmlb module. For virtual disks, the partition
53 * related ioctl calls are routed to cmlb module. Compression and encryption
54 * is not supported for virtual disks.
55 *
56 * Mapped devices are tracked with state structures handled with
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -070057 * ddi_soft_state(9F) for simplicity.
58 *
59 * A file attached to lofi is opened when attached and not closed until
60 * explicitly detached from lofi. This seems more sensible than deferring
61 * the open until the /dev/lofi device is opened, for a number of reasons.
62 * One is that any failure is likely to be noticed by the person (or script)
63 * running lofiadm. Another is that it would be a security problem if the
64 * file was replaced by another one after being added but before being opened.
65 *
66 * The only hard part about lofi is the ioctls. In order to support things
67 * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
68 * So it has to fake disk geometry and partition information. More may need
69 * to be faked if your favorite utility doesn't work and you think it should
70 * (fdformat doesn't work because it really wants to know the type of floppy
71 * controller to talk to, and that didn't seem easy to fake. Or possibly even
72 * necessary, since we have mkfs_pcfs now).
73 *
eschrock3d7072f2007-06-12 13:18:17 -070074 * Normally, a lofi device cannot be detached if it is open (i.e. busy). To
75 * support simulation of hotplug events, an optional force flag is provided.
76 * If a lofi device is open when a force detach is requested, then the
77 * underlying file is closed and any subsequent operations return EIO. When the
78 * device is closed for the last time, it will be cleaned up at that time. In
79 * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
80 * detached but not removed.
81 *
Toomas Soomeb4844712016-10-13 00:15:40 +030082 * If detach was requested and lofi device is not open, we will perform
83 * unmap and remove the lofi instance.
84 *
85 * If the lofi device is open and the li_cleanup is set on ioctl request,
86 * we set ls_cleanup flag to notify the cleanup is requested, and the
87 * last lofi_close will perform the unmapping and this lofi instance will be
88 * removed.
89 *
90 * If the lofi device is open and the li_force is set on ioctl request,
91 * we set ls_cleanup flag to notify the cleanup is requested,
92 * we also set ls_vp_closereq to notify IO tasks to return EIO on new
93 * IO requests and wait in process IO count to become 0, indicating there
94 * are no more IO requests. Since ls_cleanup is set, the last lofi_close
95 * will perform unmap and this lofi instance will be removed.
96 * See also lofi_unmap_file() for details.
97 *
98 * Once ls_cleanup is set for the instance, we do not allow lofi_open()
99 * calls to succeed and can have last lofi_close() to remove the instance.
100 *
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700101 * Known problems:
102 *
103 * UFS logging. Mounting a UFS filesystem image "logging"
104 * works for basic copy testing but wedges during a build of ON through
105 * that image. Some deadlock in lufs holding the log mutex and then
106 * getting stuck on a buf. So for now, don't do that.
107 *
108 * Direct I/O. Since the filesystem data is being cached in the buffer
109 * cache, _and_ again in the underlying filesystem, it's tempting to
110 * enable direct I/O on the underlying file. Don't, because that deadlocks.
111 * I think to fix the cache-twice problem we might need filesystem support.
112 *
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700113 * Interesting things to do:
114 *
115 * Allow multiple files for each device. A poor-man's metadisk, basically.
116 *
117 * Pass-through ioctls on block devices. You can (though it's not
118 * documented), give lofi a block device as a file name. Then we shouldn't
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800119 * need to fake a geometry, however, it may be relevant if you're replacing
120 * metadisk, or using lofi to get crypto.
121 * It makes sense to do lofiadm -c aes -a /dev/dsk/c0t0d0s4 /dev/lofi/1
122 * and then in /etc/vfstab have an entry for /dev/lofi/1 as /export/home.
123 * In fact this even makes sense if you have lofi "above" metadisk.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700124 *
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800125 * Encryption:
126 * Each lofi device can have its own symmetric key and cipher.
127 * They are passed to us by lofiadm(1m) in the correct format for use
128 * with the misc/kcf crypto_* routines.
129 *
130 * Each block has its own IV, that is calculated in lofi_blk_mech(), based
131 * on the "master" key held in the lsp and the block number of the buffer.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700132 */
133
134#include <sys/types.h>
aalok87117652007-12-07 14:08:39 -0800135#include <netinet/in.h>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700136#include <sys/sysmacros.h>
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700137#include <sys/uio.h>
138#include <sys/kmem.h>
139#include <sys/cred.h>
140#include <sys/mman.h>
141#include <sys/errno.h>
142#include <sys/aio_req.h>
143#include <sys/stat.h>
144#include <sys/file.h>
145#include <sys/modctl.h>
146#include <sys/conf.h>
147#include <sys/debug.h>
148#include <sys/vnode.h>
149#include <sys/lofi.h>
Toomas Soome8ae05c12016-09-16 01:57:23 +0300150#include <sys/lofi_impl.h> /* for cache structure */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700151#include <sys/fcntl.h>
152#include <sys/pathname.h>
153#include <sys/filio.h>
154#include <sys/fdio.h>
155#include <sys/open.h>
156#include <sys/disp.h>
157#include <vm/seg_map.h>
158#include <sys/ddi.h>
159#include <sys/sunddi.h>
aalok87117652007-12-07 14:08:39 -0800160#include <sys/zmod.h>
John Levon0fbb7512010-06-16 10:02:44 -0700161#include <sys/id_space.h>
162#include <sys/mkdev.h>
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800163#include <sys/crypto/common.h>
164#include <sys/crypto/api.h>
John Levon0fbb7512010-06-16 10:02:44 -0700165#include <sys/rctl.h>
Toomas Soome406fc512015-10-23 09:36:11 +0300166#include <sys/vtoc.h>
167#include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
168#include <sys/scsi/impl/uscsi.h>
169#include <sys/sysevent/dev.h>
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400170#include <LzmaDec.h>
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800171
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700172#define NBLOCKS_PROP_NAME "Nblocks"
aalok87117652007-12-07 14:08:39 -0800173#define SIZE_PROP_NAME "Size"
John Levon0fbb7512010-06-16 10:02:44 -0700174#define ZONE_PROP_NAME "zone"
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700175
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800176#define SETUP_C_DATA(cd, buf, len) \
177 (cd).cd_format = CRYPTO_DATA_RAW; \
178 (cd).cd_offset = 0; \
179 (cd).cd_miscdata = NULL; \
180 (cd).cd_length = (len); \
181 (cd).cd_raw.iov_base = (buf); \
182 (cd).cd_raw.iov_len = (len);
183
184#define UIO_CHECK(uio) \
185 if (((uio)->uio_loffset % DEV_BSIZE) != 0 || \
186 ((uio)->uio_resid % DEV_BSIZE) != 0) { \
187 return (EINVAL); \
188 }
189
Toomas Soome406fc512015-10-23 09:36:11 +0300190#define LOFI_TIMEOUT 30
Toomas Soome406fc512015-10-23 09:36:11 +0300191
192static void *lofi_statep;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700193static kmutex_t lofi_lock; /* state lock */
Toomas Soome406fc512015-10-23 09:36:11 +0300194static id_space_t *lofi_id; /* lofi ID values */
John Levon0fbb7512010-06-16 10:02:44 -0700195static list_t lofi_list;
196static zone_key_t lofi_zone_key;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700197
198/*
199 * Because lofi_taskq_nthreads limits the actual swamping of the device, the
200 * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
201 * high. If we want to be assured that the underlying device is always busy,
202 * we must be sure that the number of bytes enqueued when the number of
203 * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
204 * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should
205 * set maxalloc to be the maximum throughput (in bytes per second) of the
206 * underlying device divided by the minimum I/O size. We assume a realistic
207 * maximum throughput of one hundred megabytes per second; we set maxalloc on
208 * the lofi task queue to be 104857600 divided by DEV_BSIZE.
209 */
210static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
211static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */
212
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800213const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700214
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -0600215/*
216 * To avoid decompressing data in a compressed segment multiple times
217 * when accessing small parts of a segment's data, we cache and reuse
218 * the uncompressed segment's data.
219 *
220 * A single cached segment is sufficient to avoid lots of duplicate
221 * segment decompress operations. A small cache size also reduces the
222 * memory footprint.
223 *
224 * lofi_max_comp_cache is the maximum number of decompressed data segments
225 * cached for each compressed lofi image. It can be set to 0 to disable
226 * caching.
227 */
228
229uint32_t lofi_max_comp_cache = 1;
230
aalok87117652007-12-07 14:08:39 -0800231static int gzip_decompress(void *src, size_t srclen, void *dst,
232 size_t *destlen, int level);
233
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400234static int lzma_decompress(void *src, size_t srclen, void *dst,
235 size_t *dstlen, int level);
236
aalok87117652007-12-07 14:08:39 -0800237lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
238 {gzip_decompress, NULL, 6, "gzip"}, /* default */
239 {gzip_decompress, NULL, 6, "gzip-6"},
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400240 {gzip_decompress, NULL, 9, "gzip-9"},
241 {lzma_decompress, NULL, 0, "lzma"}
aalok87117652007-12-07 14:08:39 -0800242};
243
Toomas Soome406fc512015-10-23 09:36:11 +0300244static void lofi_strategy_task(void *);
245static int lofi_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t,
246 size_t, void *);
247static int lofi_tg_getinfo(dev_info_t *, int, void *, void *);
248
249struct cmlb_tg_ops lofi_tg_ops = {
250 TG_DK_OPS_VERSION_1,
251 lofi_tg_rdwr,
252 lofi_tg_getinfo
253};
254
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400255/*ARGSUSED*/
256static void
257*SzAlloc(void *p, size_t size)
258{
259 return (kmem_alloc(size, KM_SLEEP));
260}
261
262/*ARGSUSED*/
263static void
264SzFree(void *p, void *address, size_t size)
265{
266 kmem_free(address, size);
267}
268
269static ISzAlloc g_Alloc = { SzAlloc, SzFree };
270
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -0600271/*
272 * Free data referenced by the linked list of cached uncompressed
273 * segments.
274 */
275static void
276lofi_free_comp_cache(struct lofi_state *lsp)
277{
278 struct lofi_comp_cache *lc;
279
280 while ((lc = list_remove_head(&lsp->ls_comp_cache)) != NULL) {
281 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
282 kmem_free(lc, sizeof (struct lofi_comp_cache));
283 lsp->ls_comp_cache_count--;
284 }
285 ASSERT(lsp->ls_comp_cache_count == 0);
286}
287
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700288static int
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700289is_opened(struct lofi_state *lsp)
290{
Toomas Soome406fc512015-10-23 09:36:11 +0300291 int i;
292 boolean_t last = B_TRUE;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700293
John Levon0fbb7512010-06-16 10:02:44 -0700294 ASSERT(MUTEX_HELD(&lofi_lock));
Toomas Soome406fc512015-10-23 09:36:11 +0300295 for (i = 0; i < LOFI_PART_MAX; i++) {
296 if (lsp->ls_open_lyr[i]) {
297 last = B_FALSE;
298 break;
299 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700300 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700301
Toomas Soome406fc512015-10-23 09:36:11 +0300302 for (i = 0; last && (i < OTYP_LYR); i++) {
303 if (lsp->ls_open_reg[i]) {
304 last = B_FALSE;
305 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700306 }
Toomas Soome406fc512015-10-23 09:36:11 +0300307
308 return (!last);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700309}
310
eschrock3d7072f2007-06-12 13:18:17 -0700311static void
Toomas Soomeb4844712016-10-13 00:15:40 +0300312lofi_set_cleanup(struct lofi_state *lsp)
313{
314 ASSERT(MUTEX_HELD(&lofi_lock));
315
316 lsp->ls_cleanup = B_TRUE;
317
318 /* wake up any threads waiting on dkiocstate */
319 cv_broadcast(&lsp->ls_vp_cv);
320}
321
322static void
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800323lofi_free_crypto(struct lofi_state *lsp)
324{
John Levon0fbb7512010-06-16 10:02:44 -0700325 ASSERT(MUTEX_HELD(&lofi_lock));
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800326
327 if (lsp->ls_crypto_enabled) {
328 /*
329 * Clean up the crypto state so that it doesn't hang around
330 * in memory after we are done with it.
331 */
John Levon0fbb7512010-06-16 10:02:44 -0700332 if (lsp->ls_key.ck_data != NULL) {
333 bzero(lsp->ls_key.ck_data,
334 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
335 kmem_free(lsp->ls_key.ck_data,
336 CRYPTO_BITS2BYTES(lsp->ls_key.ck_length));
337 lsp->ls_key.ck_data = NULL;
338 lsp->ls_key.ck_length = 0;
339 }
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800340
341 if (lsp->ls_mech.cm_param != NULL) {
342 kmem_free(lsp->ls_mech.cm_param,
343 lsp->ls_mech.cm_param_len);
344 lsp->ls_mech.cm_param = NULL;
345 lsp->ls_mech.cm_param_len = 0;
346 }
347
348 if (lsp->ls_iv_mech.cm_param != NULL) {
349 kmem_free(lsp->ls_iv_mech.cm_param,
350 lsp->ls_iv_mech.cm_param_len);
351 lsp->ls_iv_mech.cm_param = NULL;
352 lsp->ls_iv_mech.cm_param_len = 0;
353 }
354
355 mutex_destroy(&lsp->ls_crypto_lock);
356 }
357}
358
Toomas Soome406fc512015-10-23 09:36:11 +0300359/* ARGSUSED */
360static int
361lofi_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
362 size_t length, void *tg_cookie)
363{
364 struct lofi_state *lsp;
365 buf_t *bp;
366 int instance;
367 int rv = 0;
368
369 instance = ddi_get_instance(dip);
370 if (instance == 0) /* control node does not have disk */
371 return (ENXIO);
372
373 lsp = ddi_get_soft_state(lofi_statep, instance);
374
375 if (lsp == NULL)
376 return (ENXIO);
377
378 if (cmd != TG_READ && cmd != TG_WRITE)
379 return (EINVAL);
380
381 /*
382 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
383 */
384 mutex_enter(&lsp->ls_vp_lock);
385 while (lsp->ls_vp_ready == B_FALSE)
386 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
387 mutex_exit(&lsp->ls_vp_lock);
388
389 if (P2PHASE(length, (1U << lsp->ls_lbshift)) != 0) {
390 /* We can only transfer whole blocks at a time! */
391 return (EINVAL);
392 }
393
394 bp = getrbuf(KM_SLEEP);
395
396 if (cmd == TG_READ) {
397 bp->b_flags = B_READ;
398 } else {
399 if (lsp->ls_readonly == B_TRUE) {
400 freerbuf(bp);
401 return (EROFS);
402 }
403 bp->b_flags = B_WRITE;
404 }
405
406 bp->b_un.b_addr = bufaddr;
407 bp->b_bcount = length;
408 bp->b_lblkno = start;
409 bp->b_private = NULL;
410 bp->b_edev = lsp->ls_dev;
411
412 if (lsp->ls_kstat) {
413 mutex_enter(lsp->ls_kstat->ks_lock);
414 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
415 mutex_exit(lsp->ls_kstat->ks_lock);
416 }
417 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
418 (void) biowait(bp);
419
420 rv = geterror(bp);
421 freerbuf(bp);
422 return (rv);
423}
424
425/*
426 * Get device geometry info for cmlb.
427 *
428 * We have mapped disk image as virtual block device and have to report
429 * physical/virtual geometry to cmlb.
430 *
431 * So we have two principal cases:
432 * 1. Uninitialised image without any existing labels,
433 * for this case we fabricate the data based on mapped image.
434 * 2. Image with existing label information.
435 * Since we have no information how the image was created (it may be
436 * dump from some physical device), we need to rely on label information
437 * from image, or we get "corrupted label" errors.
438 * NOTE: label can be MBR, MBR+SMI, GPT
439 */
440static int
441lofi_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
442{
443 struct lofi_state *lsp;
444 int instance;
445 int ashift;
446
447 _NOTE(ARGUNUSED(tg_cookie));
448 instance = ddi_get_instance(dip);
449 if (instance == 0) /* control device has no storage */
450 return (ENXIO);
451
452 lsp = ddi_get_soft_state(lofi_statep, instance);
453
454 if (lsp == NULL)
455 return (ENXIO);
456
457 /*
458 * Make sure the mapping is set up by checking lsp->ls_vp_ready.
459 *
460 * When mapping is created, new lofi instance is created and
461 * lofi_attach() will call cmlb_attach() as part of the procedure
462 * to set the mapping up. This chain of events will happen in
463 * the same thread.
464 * Since cmlb_attach() will call lofi_tg_getinfo to get
465 * capacity, we return error on that call if cookie is set,
466 * otherwise lofi_attach will be stuck as the mapping is not yet
467 * finalized and lofi is not yet ready.
468 * Note, such error is not fatal for cmlb, as the label setup
469 * will be finalized when cmlb_validate() is called.
470 */
471 mutex_enter(&lsp->ls_vp_lock);
472 if (tg_cookie != NULL && lsp->ls_vp_ready == B_FALSE) {
473 mutex_exit(&lsp->ls_vp_lock);
474 return (ENXIO);
475 }
476 while (lsp->ls_vp_ready == B_FALSE)
477 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
478 mutex_exit(&lsp->ls_vp_lock);
479
480 ashift = lsp->ls_lbshift;
481
482 switch (cmd) {
483 case TG_GETPHYGEOM: {
484 cmlb_geom_t *geomp = arg;
485
486 geomp->g_capacity =
487 (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
488 geomp->g_nsect = lsp->ls_dkg.dkg_nsect;
489 geomp->g_nhead = lsp->ls_dkg.dkg_nhead;
490 geomp->g_acyl = lsp->ls_dkg.dkg_acyl;
491 geomp->g_ncyl = lsp->ls_dkg.dkg_ncyl;
492 geomp->g_secsize = (1U << ashift);
493 geomp->g_intrlv = lsp->ls_dkg.dkg_intrlv;
494 geomp->g_rpm = lsp->ls_dkg.dkg_rpm;
495 return (0);
496 }
497
498 case TG_GETCAPACITY:
499 *(diskaddr_t *)arg =
500 (lsp->ls_vp_size - lsp->ls_crypto_offset) >> ashift;
501 return (0);
502
503 case TG_GETBLOCKSIZE:
504 *(uint32_t *)arg = (1U << ashift);
505 return (0);
506
507 case TG_GETATTR: {
508 tg_attribute_t *tgattr = arg;
509
510 tgattr->media_is_writable = !lsp->ls_readonly;
511 tgattr->media_is_solid_state = B_FALSE;
Yuri Pankovacb450d2017-03-06 00:19:36 +0300512 tgattr->media_is_rotational = B_FALSE;
Toomas Soome406fc512015-10-23 09:36:11 +0300513 return (0);
514 }
515
516 default:
517 return (EINVAL);
518 }
519}
520
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800521static void
John Levon0fbb7512010-06-16 10:02:44 -0700522lofi_destroy(struct lofi_state *lsp, cred_t *credp)
eschrock3d7072f2007-06-12 13:18:17 -0700523{
Toomas Soome406fc512015-10-23 09:36:11 +0300524 int id = LOFI_MINOR2ID(getminor(lsp->ls_dev));
John Levon0fbb7512010-06-16 10:02:44 -0700525 int i;
eschrock3d7072f2007-06-12 13:18:17 -0700526
John Levon0fbb7512010-06-16 10:02:44 -0700527 ASSERT(MUTEX_HELD(&lofi_lock));
528
Toomas Soomeb4844712016-10-13 00:15:40 +0300529 /*
530 * Before we can start to release the other resources,
531 * make sure we have all tasks completed and taskq removed.
532 */
533 if (lsp->ls_taskq != NULL) {
534 taskq_destroy(lsp->ls_taskq);
535 lsp->ls_taskq = NULL;
536 }
537
John Levon0fbb7512010-06-16 10:02:44 -0700538 list_remove(&lofi_list, lsp);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800539
540 lofi_free_crypto(lsp);
541
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -0700542 /*
543 * Free pre-allocated compressed buffers
544 */
545 if (lsp->ls_comp_bufs != NULL) {
546 for (i = 0; i < lofi_taskq_nthreads; i++) {
547 if (lsp->ls_comp_bufs[i].bufsize > 0)
548 kmem_free(lsp->ls_comp_bufs[i].buf,
549 lsp->ls_comp_bufs[i].bufsize);
550 }
551 kmem_free(lsp->ls_comp_bufs,
552 sizeof (struct compbuf) * lofi_taskq_nthreads);
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -0700553 }
554
Toomas Soome406fc512015-10-23 09:36:11 +0300555 if (lsp->ls_vp != NULL) {
556 (void) VOP_PUTPAGE(lsp->ls_vp, 0, 0, B_INVAL, credp, NULL);
557 (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
558 1, 0, credp, NULL);
559 VN_RELE(lsp->ls_vp);
560 }
John Levon0fbb7512010-06-16 10:02:44 -0700561 if (lsp->ls_stacked_vp != lsp->ls_vp)
562 VN_RELE(lsp->ls_stacked_vp);
Toomas Soomeb4844712016-10-13 00:15:40 +0300563 lsp->ls_vp = lsp->ls_stacked_vp = NULL;
John Levon0fbb7512010-06-16 10:02:44 -0700564
Toomas Soomeb4844712016-10-13 00:15:40 +0300565 if (lsp->ls_kstat != NULL) {
John Levon0fbb7512010-06-16 10:02:44 -0700566 kstat_delete(lsp->ls_kstat);
Toomas Soomeb4844712016-10-13 00:15:40 +0300567 lsp->ls_kstat = NULL;
568 }
John Levon0fbb7512010-06-16 10:02:44 -0700569
570 /*
571 * Free cached decompressed segment data
572 */
573 lofi_free_comp_cache(lsp);
574 list_destroy(&lsp->ls_comp_cache);
575
576 if (lsp->ls_uncomp_seg_sz > 0) {
577 kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
578 lsp->ls_uncomp_seg_sz = 0;
579 }
580
jv227347a19609f2010-08-12 17:34:26 -0700581 rctl_decr_lofi(lsp->ls_zone.zref_zone, 1);
582 zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
John Levon0fbb7512010-06-16 10:02:44 -0700583
584 mutex_destroy(&lsp->ls_comp_cache_lock);
585 mutex_destroy(&lsp->ls_comp_bufs_lock);
586 mutex_destroy(&lsp->ls_kstat_lock);
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -0600587 mutex_destroy(&lsp->ls_vp_lock);
Toomas Soome406fc512015-10-23 09:36:11 +0300588 cv_destroy(&lsp->ls_vp_cv);
589 lsp->ls_vp_ready = B_FALSE;
Toomas Soomeb4844712016-10-13 00:15:40 +0300590 lsp->ls_vp_closereq = B_FALSE;
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -0600591
Toomas Soome406fc512015-10-23 09:36:11 +0300592 ASSERT(ddi_get_soft_state(lofi_statep, id) == lsp);
593 (void) ndi_devi_offline(lsp->ls_dip, NDI_DEVI_REMOVE);
594 id_free(lofi_id, id);
John Levon0fbb7512010-06-16 10:02:44 -0700595}
596
597static void
Toomas Soome406fc512015-10-23 09:36:11 +0300598lofi_free_dev(struct lofi_state *lsp)
John Levon0fbb7512010-06-16 10:02:44 -0700599{
John Levon0fbb7512010-06-16 10:02:44 -0700600 ASSERT(MUTEX_HELD(&lofi_lock));
601
Toomas Soome406fc512015-10-23 09:36:11 +0300602 if (lsp->ls_cmlbhandle != NULL) {
603 cmlb_invalidate(lsp->ls_cmlbhandle, 0);
604 cmlb_detach(lsp->ls_cmlbhandle, 0);
605 cmlb_free_handle(&lsp->ls_cmlbhandle);
606 lsp->ls_cmlbhandle = NULL;
607 }
608 (void) ddi_prop_remove_all(lsp->ls_dip);
609 ddi_remove_minor_node(lsp->ls_dip, NULL);
John Levon0fbb7512010-06-16 10:02:44 -0700610}
611
612/*ARGSUSED*/
613static void
614lofi_zone_shutdown(zoneid_t zoneid, void *arg)
615{
616 struct lofi_state *lsp;
617 struct lofi_state *next;
618
619 mutex_enter(&lofi_lock);
620
621 for (lsp = list_head(&lofi_list); lsp != NULL; lsp = next) {
622
623 /* lofi_destroy() frees lsp */
624 next = list_next(&lofi_list, lsp);
625
jv227347a19609f2010-08-12 17:34:26 -0700626 if (lsp->ls_zone.zref_zone->zone_id != zoneid)
John Levon0fbb7512010-06-16 10:02:44 -0700627 continue;
628
629 /*
630 * No in-zone processes are running, but something has this
631 * open. It's either a global zone process, or a lofi
632 * mount. In either case we set ls_cleanup so the last
633 * user destroys the device.
634 */
635 if (is_opened(lsp)) {
Toomas Soomeb4844712016-10-13 00:15:40 +0300636 lofi_set_cleanup(lsp);
John Levon0fbb7512010-06-16 10:02:44 -0700637 } else {
Toomas Soome406fc512015-10-23 09:36:11 +0300638 lofi_free_dev(lsp);
John Levon0fbb7512010-06-16 10:02:44 -0700639 lofi_destroy(lsp, kcred);
640 }
641 }
642
643 mutex_exit(&lofi_lock);
eschrock3d7072f2007-06-12 13:18:17 -0700644}
645
646/*ARGSUSED*/
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700647static int
648lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
649{
Toomas Soome406fc512015-10-23 09:36:11 +0300650 int id;
651 minor_t part;
652 uint64_t mask;
653 diskaddr_t nblks;
654 diskaddr_t lba;
655 boolean_t ndelay;
656
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700657 struct lofi_state *lsp;
658
Toomas Soome406fc512015-10-23 09:36:11 +0300659 if (otyp >= OTYPCNT)
660 return (EINVAL);
661
662 ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
663
John Levon0fbb7512010-06-16 10:02:44 -0700664 /*
665 * lofiadm -a /dev/lofi/1 gets us here.
666 */
667 if (mutex_owner(&lofi_lock) == curthread)
668 return (EINVAL);
669
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700670 mutex_enter(&lofi_lock);
John Levon0fbb7512010-06-16 10:02:44 -0700671
Toomas Soome406fc512015-10-23 09:36:11 +0300672 id = LOFI_MINOR2ID(getminor(*devp));
673 part = LOFI_PART(getminor(*devp));
674 mask = (1U << part);
John Levon0fbb7512010-06-16 10:02:44 -0700675
676 /* master control device */
Toomas Soome406fc512015-10-23 09:36:11 +0300677 if (id == 0) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700678 mutex_exit(&lofi_lock);
679 return (0);
680 }
681
682 /* otherwise, the mapping should already exist */
Toomas Soome406fc512015-10-23 09:36:11 +0300683 lsp = ddi_get_soft_state(lofi_statep, id);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700684 if (lsp == NULL) {
685 mutex_exit(&lofi_lock);
686 return (EINVAL);
687 }
688
Toomas Soomeb4844712016-10-13 00:15:40 +0300689 if (lsp->ls_cleanup == B_TRUE) {
690 mutex_exit(&lofi_lock);
691 return (ENXIO);
692 }
693
eschrock3d7072f2007-06-12 13:18:17 -0700694 if (lsp->ls_vp == NULL) {
695 mutex_exit(&lofi_lock);
696 return (ENXIO);
697 }
698
Alexander Eremincd69fab2013-09-23 10:41:22 +0400699 if (lsp->ls_readonly && (flag & FWRITE)) {
700 mutex_exit(&lofi_lock);
701 return (EROFS);
702 }
703
Toomas Soome406fc512015-10-23 09:36:11 +0300704 if ((lsp->ls_open_excl) & (mask)) {
Alexander Eremina67ac132013-11-01 20:22:54 +0400705 mutex_exit(&lofi_lock);
Toomas Soome406fc512015-10-23 09:36:11 +0300706 return (EBUSY);
707 }
708
709 if (flag & FEXCL) {
710 if (lsp->ls_open_lyr[part]) {
711 mutex_exit(&lofi_lock);
712 return (EBUSY);
713 }
714 for (int i = 0; i < OTYP_LYR; i++) {
715 if (lsp->ls_open_reg[i] & mask) {
716 mutex_exit(&lofi_lock);
717 return (EBUSY);
718 }
719 }
720 }
721
722 if (lsp->ls_cmlbhandle != NULL) {
723 if (cmlb_validate(lsp->ls_cmlbhandle, 0, 0) != 0) {
724 /*
725 * non-blocking opens are allowed to succeed to
726 * support format and fdisk to create partitioning.
727 */
728 if (!ndelay) {
729 mutex_exit(&lofi_lock);
730 return (ENXIO);
731 }
732 } else if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &nblks, &lba,
733 NULL, NULL, 0) == 0) {
734 if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
735 mutex_exit(&lofi_lock);
736 return (ENXIO);
737 }
738 } else if (!ndelay) {
739 mutex_exit(&lofi_lock);
740 return (ENXIO);
741 }
742 }
743
744 if (otyp == OTYP_LYR) {
745 lsp->ls_open_lyr[part]++;
746 } else {
747 lsp->ls_open_reg[otyp] |= mask;
748 }
749 if (flag & FEXCL) {
750 lsp->ls_open_excl |= mask;
Alexander Eremina67ac132013-11-01 20:22:54 +0400751 }
752
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700753 mutex_exit(&lofi_lock);
754 return (0);
755}
756
eschrock3d7072f2007-06-12 13:18:17 -0700757/*ARGSUSED*/
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700758static int
759lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
760{
Toomas Soome406fc512015-10-23 09:36:11 +0300761 minor_t part;
762 int id;
763 uint64_t mask;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700764 struct lofi_state *lsp;
765
Toomas Soome406fc512015-10-23 09:36:11 +0300766 id = LOFI_MINOR2ID(getminor(dev));
767 part = LOFI_PART(getminor(dev));
768 mask = (1U << part);
769
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700770 mutex_enter(&lofi_lock);
Toomas Soome406fc512015-10-23 09:36:11 +0300771 lsp = ddi_get_soft_state(lofi_statep, id);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700772 if (lsp == NULL) {
773 mutex_exit(&lofi_lock);
774 return (EINVAL);
775 }
John Levon0fbb7512010-06-16 10:02:44 -0700776
Toomas Soome406fc512015-10-23 09:36:11 +0300777 if (id == 0) {
John Levon0fbb7512010-06-16 10:02:44 -0700778 mutex_exit(&lofi_lock);
779 return (0);
780 }
781
Toomas Soome406fc512015-10-23 09:36:11 +0300782 if (lsp->ls_open_excl & mask)
783 lsp->ls_open_excl &= ~mask;
784
785 if (otyp == OTYP_LYR) {
786 lsp->ls_open_lyr[part]--;
787 } else {
788 lsp->ls_open_reg[otyp] &= ~mask;
789 }
eschrock3d7072f2007-06-12 13:18:17 -0700790
791 /*
johnlev93239add2008-05-27 18:25:14 -0700792 * If we forcibly closed the underlying device (li_force), or
793 * asked for cleanup (li_cleanup), finish up if we're the last
794 * out of the door.
eschrock3d7072f2007-06-12 13:18:17 -0700795 */
Toomas Soomeb4844712016-10-13 00:15:40 +0300796 if (!is_opened(lsp) &&
797 (lsp->ls_cleanup == B_TRUE || lsp->ls_vp == NULL)) {
Toomas Soome406fc512015-10-23 09:36:11 +0300798 lofi_free_dev(lsp);
John Levon0fbb7512010-06-16 10:02:44 -0700799 lofi_destroy(lsp, credp);
800 }
johnlev93239add2008-05-27 18:25:14 -0700801
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -0700802 mutex_exit(&lofi_lock);
803 return (0);
804}
805
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800806/*
807 * Sets the mechanism's initialization vector (IV) if one is needed.
808 * The IV is computed from the data block number. lsp->ls_mech is
809 * altered so that:
810 * lsp->ls_mech.cm_param_len is set to the IV len.
811 * lsp->ls_mech.cm_param is set to the IV.
812 */
813static int
814lofi_blk_mech(struct lofi_state *lsp, longlong_t lblkno)
815{
816 int ret;
817 crypto_data_t cdata;
818 char *iv;
819 size_t iv_len;
820 size_t min;
821 void *data;
822 size_t datasz;
823
John Levon0fbb7512010-06-16 10:02:44 -0700824 ASSERT(MUTEX_HELD(&lsp->ls_crypto_lock));
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800825
826 if (lsp == NULL)
827 return (CRYPTO_DEVICE_ERROR);
828
829 /* lsp->ls_mech.cm_param{_len} has already been set for static iv */
830 if (lsp->ls_iv_type == IVM_NONE) {
831 return (CRYPTO_SUCCESS);
832 }
833
834 /*
835 * if kmem already alloced from previous call and it's the same size
836 * we need now, just recycle it; allocate new kmem only if we have to
837 */
838 if (lsp->ls_mech.cm_param == NULL ||
839 lsp->ls_mech.cm_param_len != lsp->ls_iv_len) {
840 iv_len = lsp->ls_iv_len;
841 iv = kmem_zalloc(iv_len, KM_SLEEP);
842 } else {
843 iv_len = lsp->ls_mech.cm_param_len;
844 iv = lsp->ls_mech.cm_param;
845 bzero(iv, iv_len);
846 }
847
848 switch (lsp->ls_iv_type) {
849 case IVM_ENC_BLKNO:
850 /* iv is not static, lblkno changes each time */
851 data = &lblkno;
852 datasz = sizeof (lblkno);
853 break;
854 default:
855 data = 0;
856 datasz = 0;
857 break;
858 }
859
860 /*
861 * write blkno into the iv buffer padded on the left in case
862 * blkno ever grows bigger than its current longlong_t size
863 * or a variation other than blkno is used for the iv data
864 */
865 min = MIN(datasz, iv_len);
866 bcopy(data, iv + (iv_len - min), min);
867
868 /* encrypt the data in-place to get the IV */
869 SETUP_C_DATA(cdata, iv, iv_len);
870
871 ret = crypto_encrypt(&lsp->ls_iv_mech, &cdata, &lsp->ls_key,
872 NULL, NULL, NULL);
873 if (ret != CRYPTO_SUCCESS) {
874 cmn_err(CE_WARN, "failed to create iv for block %lld: (0x%x)",
875 lblkno, ret);
876 if (lsp->ls_mech.cm_param != iv)
877 kmem_free(iv, iv_len);
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400878
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800879 return (ret);
880 }
881
882 /* clean up the iv from the last computation */
883 if (lsp->ls_mech.cm_param != NULL && lsp->ls_mech.cm_param != iv)
884 kmem_free(lsp->ls_mech.cm_param, lsp->ls_mech.cm_param_len);
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -0400885
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -0800886 lsp->ls_mech.cm_param_len = iv_len;
887 lsp->ls_mech.cm_param = iv;
888
889 return (CRYPTO_SUCCESS);
890}
891
892/*
893 * Performs encryption and decryption of a chunk of data of size "len",
894 * one DEV_BSIZE block at a time. "len" is assumed to be a multiple of
895 * DEV_BSIZE.
896 */
897static int
898lofi_crypto(struct lofi_state *lsp, struct buf *bp, caddr_t plaintext,
899 caddr_t ciphertext, size_t len, boolean_t op_encrypt)
900{
901 crypto_data_t cdata;
902 crypto_data_t wdata;
903 int ret;
904 longlong_t lblkno = bp->b_lblkno;
905
906 mutex_enter(&lsp->ls_crypto_lock);
907
908 /*
909 * though we could encrypt/decrypt entire "len" chunk of data, we need
910 * to break it into DEV_BSIZE pieces to capture blkno incrementing
911 */
912 SETUP_C_DATA(cdata, plaintext, len);
913 cdata.cd_length = DEV_BSIZE;
914 if (ciphertext != NULL) { /* not in-place crypto */
915 SETUP_C_DATA(wdata, ciphertext, len);
916 wdata.cd_length = DEV_BSIZE;
917 }
918
919 do {
920 ret = lofi_blk_mech(lsp, lblkno);
921 if (ret != CRYPTO_SUCCESS)
922 continue;
923
924 if (op_encrypt) {
925 ret = crypto_encrypt(&lsp->ls_mech, &cdata,
926 &lsp->ls_key, NULL,
927 ((ciphertext != NULL) ? &wdata : NULL), NULL);
928 } else {
929 ret = crypto_decrypt(&lsp->ls_mech, &cdata,
930 &lsp->ls_key, NULL,
931 ((ciphertext != NULL) ? &wdata : NULL), NULL);
932 }
933
934 cdata.cd_offset += DEV_BSIZE;
935 if (ciphertext != NULL)
936 wdata.cd_offset += DEV_BSIZE;
937 lblkno++;
938 } while (ret == CRYPTO_SUCCESS && cdata.cd_offset < len);
939
940 mutex_exit(&lsp->ls_crypto_lock);
941
942 if (ret != CRYPTO_SUCCESS) {
943 cmn_err(CE_WARN, "%s failed for block %lld: (0x%x)",
944 op_encrypt ? "crypto_encrypt()" : "crypto_decrypt()",
945 lblkno, ret);
946 }
947
948 return (ret);
949}
950
951#define RDWR_RAW 1
952#define RDWR_BCOPY 2
953
954static int
955lofi_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
956 struct lofi_state *lsp, size_t len, int method, caddr_t bcopy_locn)
957{
958 ssize_t resid;
959 int isread;
960 int error;
961
962 /*
963 * Handles reads/writes for both plain and encrypted lofi
964 * Note: offset is already shifted by lsp->ls_crypto_offset
965 * when it gets here.
966 */
967
968 isread = bp->b_flags & B_READ;
969 if (isread) {
970 if (method == RDWR_BCOPY) {
971 /* DO NOT update bp->b_resid for bcopy */
972 bcopy(bcopy_locn, bufaddr, len);
973 error = 0;
974 } else { /* RDWR_RAW */
975 error = vn_rdwr(UIO_READ, lsp->ls_vp, bufaddr, len,
976 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
977 &resid);
978 bp->b_resid = resid;
979 }
980 if (lsp->ls_crypto_enabled && error == 0) {
981 if (lofi_crypto(lsp, bp, bufaddr, NULL, len,
982 B_FALSE) != CRYPTO_SUCCESS) {
983 /*
984 * XXX: original code didn't set residual
985 * back to len because no error was expected
986 * from bcopy() if encryption is not enabled
987 */
988 if (method != RDWR_BCOPY)
989 bp->b_resid = len;
990 error = EIO;
991 }
992 }
993 return (error);
994 } else {
995 void *iobuf = bufaddr;
996
997 if (lsp->ls_crypto_enabled) {
998 /* don't do in-place crypto to keep bufaddr intact */
999 iobuf = kmem_alloc(len, KM_SLEEP);
1000 if (lofi_crypto(lsp, bp, bufaddr, iobuf, len,
1001 B_TRUE) != CRYPTO_SUCCESS) {
1002 kmem_free(iobuf, len);
1003 if (method != RDWR_BCOPY)
1004 bp->b_resid = len;
1005 return (EIO);
1006 }
1007 }
1008 if (method == RDWR_BCOPY) {
1009 /* DO NOT update bp->b_resid for bcopy */
1010 bcopy(iobuf, bcopy_locn, len);
1011 error = 0;
1012 } else { /* RDWR_RAW */
1013 error = vn_rdwr(UIO_WRITE, lsp->ls_vp, iobuf, len,
1014 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred,
1015 &resid);
1016 bp->b_resid = resid;
1017 }
1018 if (lsp->ls_crypto_enabled) {
1019 kmem_free(iobuf, len);
1020 }
1021 return (error);
1022 }
1023}
1024
aalok87117652007-12-07 14:08:39 -08001025static int
1026lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001027 struct lofi_state *lsp)
aalok87117652007-12-07 14:08:39 -08001028{
1029 int error;
1030 offset_t alignedoffset, mapoffset;
1031 size_t xfersize;
1032 int isread;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001033 int smflags;
aalok87117652007-12-07 14:08:39 -08001034 caddr_t mapaddr;
1035 size_t len;
1036 enum seg_rw srw;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001037 int save_error;
1038
1039 /*
1040 * Note: offset is already shifted by lsp->ls_crypto_offset
1041 * when it gets here.
1042 */
1043 if (lsp->ls_crypto_enabled)
1044 ASSERT(lsp->ls_vp_comp_size == lsp->ls_vp_size);
aalok87117652007-12-07 14:08:39 -08001045
1046 /*
1047 * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
1048 * an 8K boundary, but the buf transfer address may not be
1049 * aligned on more than a 512-byte boundary (we don't enforce
1050 * that even though we could). This matters since the initial
1051 * part of the transfer may not start at offset 0 within the
1052 * segmap'd chunk. So we have to compensate for that with
1053 * 'mapoffset'. Subsequent chunks always start off at the
1054 * beginning, and the last is capped by b_resid
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001055 *
1056 * Visually, where "|" represents page map boundaries:
1057 * alignedoffset (mapaddr begins at this segmap boundary)
1058 * | offset (from beginning of file)
1059 * | | len
1060 * v v v
1061 * ===|====X========|====...======|========X====|====
1062 * /-------------...---------------/
1063 * ^ bp->b_bcount/bp->b_resid at start
1064 * /----/--------/----...------/--------/
1065 * ^ ^ ^ ^ ^
1066 * | | | | nth xfersize (<= MAXBSIZE)
1067 * | | 2nd thru n-1st xfersize (= MAXBSIZE)
1068 * | 1st xfersize (<= MAXBSIZE)
1069 * mapoffset (offset into 1st segmap, non-0 1st time, 0 thereafter)
1070 *
1071 * Notes: "alignedoffset" is "offset" rounded down to nearest
1072 * MAXBSIZE boundary. "len" is next page boundary of size
Dina K Nimeha363f652009-02-06 14:18:09 -08001073 * PAGESIZE after "alignedoffset".
aalok87117652007-12-07 14:08:39 -08001074 */
1075 mapoffset = offset & MAXBOFFSET;
1076 alignedoffset = offset - mapoffset;
1077 bp->b_resid = bp->b_bcount;
1078 isread = bp->b_flags & B_READ;
1079 srw = isread ? S_READ : S_WRITE;
1080 do {
1081 xfersize = MIN(lsp->ls_vp_comp_size - offset,
1082 MIN(MAXBSIZE - mapoffset, bp->b_resid));
Dina K Nimeha363f652009-02-06 14:18:09 -08001083 len = roundup(mapoffset + xfersize, PAGESIZE);
aalok87117652007-12-07 14:08:39 -08001084 mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
1085 alignedoffset, MAXBSIZE, 1, srw);
1086 /*
1087 * Now fault in the pages. This lets us check
1088 * for errors before we reference mapaddr and
1089 * try to resolve the fault in bcopy (which would
1090 * panic instead). And this can easily happen,
1091 * particularly if you've lofi'd a file over NFS
1092 * and someone deletes the file on the server.
1093 */
1094 error = segmap_fault(kas.a_hat, segkmap, mapaddr,
1095 len, F_SOFTLOCK, srw);
1096 if (error) {
1097 (void) segmap_release(segkmap, mapaddr, 0);
1098 if (FC_CODE(error) == FC_OBJERR)
1099 error = FC_ERRNO(error);
1100 else
1101 error = EIO;
1102 break;
1103 }
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001104 /* error may be non-zero for encrypted lofi */
1105 error = lofi_rdwr(bufaddr, 0, bp, lsp, xfersize,
1106 RDWR_BCOPY, mapaddr + mapoffset);
1107 if (error == 0) {
1108 bp->b_resid -= xfersize;
1109 bufaddr += xfersize;
1110 offset += xfersize;
1111 }
aalok87117652007-12-07 14:08:39 -08001112 smflags = 0;
1113 if (isread) {
1114 smflags |= SM_FREE;
1115 /*
1116 * If we're reading an entire page starting
1117 * at a page boundary, there's a good chance
1118 * we won't need it again. Put it on the
1119 * head of the freelist.
1120 */
Dina K Nimehbbd65dd2008-11-10 11:54:03 -08001121 if (mapoffset == 0 && xfersize == MAXBSIZE)
aalok87117652007-12-07 14:08:39 -08001122 smflags |= SM_DONTNEED;
aalok87117652007-12-07 14:08:39 -08001123 } else {
batschul44a1e322010-05-10 08:19:18 -07001124 /*
1125 * Write back good pages, it is okay to
1126 * always release asynchronous here as we'll
1127 * follow with VOP_FSYNC for B_SYNC buffers.
1128 */
1129 if (error == 0)
1130 smflags |= SM_WRITE | SM_ASYNC;
aalok87117652007-12-07 14:08:39 -08001131 }
aalok87117652007-12-07 14:08:39 -08001132 (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
1133 len, F_SOFTUNLOCK, srw);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001134 save_error = segmap_release(segkmap, mapaddr, smflags);
1135 if (error == 0)
1136 error = save_error;
aalok87117652007-12-07 14:08:39 -08001137 /* only the first map may start partial */
1138 mapoffset = 0;
1139 alignedoffset += MAXBSIZE;
1140 } while ((error == 0) && (bp->b_resid > 0) &&
1141 (offset < lsp->ls_vp_comp_size));
1142
1143 return (error);
1144}
1145
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001146/*
1147 * Check if segment seg_index is present in the decompressed segment
1148 * data cache.
1149 *
1150 * Returns a pointer to the decompressed segment data cache entry if
1151 * found, and NULL when decompressed data for this segment is not yet
1152 * cached.
1153 */
1154static struct lofi_comp_cache *
1155lofi_find_comp_data(struct lofi_state *lsp, uint64_t seg_index)
1156{
1157 struct lofi_comp_cache *lc;
1158
John Levon0fbb7512010-06-16 10:02:44 -07001159 ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001160
1161 for (lc = list_head(&lsp->ls_comp_cache); lc != NULL;
1162 lc = list_next(&lsp->ls_comp_cache, lc)) {
1163 if (lc->lc_index == seg_index) {
1164 /*
1165 * Decompressed segment data was found in the
1166 * cache.
1167 *
1168 * The cache uses an LRU replacement strategy;
1169 * move the entry to head of list.
1170 */
1171 list_remove(&lsp->ls_comp_cache, lc);
1172 list_insert_head(&lsp->ls_comp_cache, lc);
1173 return (lc);
1174 }
1175 }
1176 return (NULL);
1177}
1178
1179/*
1180 * Add the data for a decompressed segment at segment index
1181 * seg_index to the cache of the decompressed segments.
1182 *
1183 * Returns a pointer to the cache element structure in case
1184 * the data was added to the cache; returns NULL when the data
1185 * wasn't cached.
1186 */
1187static struct lofi_comp_cache *
1188lofi_add_comp_data(struct lofi_state *lsp, uint64_t seg_index,
1189 uchar_t *data)
1190{
1191 struct lofi_comp_cache *lc;
1192
John Levon0fbb7512010-06-16 10:02:44 -07001193 ASSERT(MUTEX_HELD(&lsp->ls_comp_cache_lock));
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001194
1195 while (lsp->ls_comp_cache_count > lofi_max_comp_cache) {
1196 lc = list_remove_tail(&lsp->ls_comp_cache);
1197 ASSERT(lc != NULL);
1198 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
1199 kmem_free(lc, sizeof (struct lofi_comp_cache));
1200 lsp->ls_comp_cache_count--;
1201 }
1202
1203 /*
1204 * Do not cache when disabled by tunable variable
1205 */
1206 if (lofi_max_comp_cache == 0)
1207 return (NULL);
1208
1209 /*
1210 * When the cache has not yet reached the maximum allowed
1211 * number of segments, allocate a new cache element.
1212 * Otherwise the cache is full; reuse the last list element
1213 * (LRU) for caching the decompressed segment data.
1214 *
1215 * The cache element for the new decompressed segment data is
1216 * added to the head of the list.
1217 */
1218 if (lsp->ls_comp_cache_count < lofi_max_comp_cache) {
1219 lc = kmem_alloc(sizeof (struct lofi_comp_cache), KM_SLEEP);
1220 lc->lc_data = NULL;
1221 list_insert_head(&lsp->ls_comp_cache, lc);
1222 lsp->ls_comp_cache_count++;
1223 } else {
1224 lc = list_remove_tail(&lsp->ls_comp_cache);
1225 if (lc == NULL)
1226 return (NULL);
1227 list_insert_head(&lsp->ls_comp_cache, lc);
1228 }
1229
1230 /*
1231 * Free old uncompressed segment data when reusing a cache
1232 * entry.
1233 */
1234 if (lc->lc_data != NULL)
1235 kmem_free(lc->lc_data, lsp->ls_uncomp_seg_sz);
1236
1237 lc->lc_data = data;
1238 lc->lc_index = seg_index;
1239 return (lc);
1240}
1241
1242
aalok87117652007-12-07 14:08:39 -08001243/*ARGSUSED*/
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001244static int
1245gzip_decompress(void *src, size_t srclen, void *dst,
aalok87117652007-12-07 14:08:39 -08001246 size_t *dstlen, int level)
1247{
1248 ASSERT(*dstlen >= srclen);
1249
1250 if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
1251 return (-1);
1252 return (0);
1253}
1254
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001255#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
1256/*ARGSUSED*/
1257static int
1258lzma_decompress(void *src, size_t srclen, void *dst,
Toomas Soome406fc512015-10-23 09:36:11 +03001259 size_t *dstlen, int level)
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001260{
1261 size_t insizepure;
1262 void *actual_src;
1263 ELzmaStatus status;
1264
1265 insizepure = srclen - LZMA_HEADER_SIZE;
1266 actual_src = (void *)((Byte *)src + LZMA_HEADER_SIZE);
1267
1268 if (LzmaDecode((Byte *)dst, (size_t *)dstlen,
1269 (const Byte *)actual_src, &insizepure,
1270 (const Byte *)src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status,
1271 &g_Alloc) != SZ_OK) {
1272 return (-1);
1273 }
1274 return (0);
1275}
1276
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001277/*
1278 * This is basically what strategy used to be before we found we
1279 * needed task queues.
1280 */
1281static void
1282lofi_strategy_task(void *arg)
1283{
1284 struct buf *bp = (struct buf *)arg;
1285 int error;
batschul44a1e322010-05-10 08:19:18 -07001286 int syncflag = 0;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001287 struct lofi_state *lsp;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001288 offset_t offset;
1289 caddr_t bufaddr;
1290 size_t len;
1291 size_t xfersize;
1292 boolean_t bufinited = B_FALSE;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001293
Toomas Soome406fc512015-10-23 09:36:11 +03001294 lsp = ddi_get_soft_state(lofi_statep,
1295 LOFI_MINOR2ID(getminor(bp->b_edev)));
1296
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001297 if (lsp == NULL) {
1298 error = ENXIO;
1299 goto errout;
1300 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001301 if (lsp->ls_kstat) {
1302 mutex_enter(lsp->ls_kstat->ks_lock);
1303 kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
1304 mutex_exit(lsp->ls_kstat->ks_lock);
1305 }
Toomas Soome406fc512015-10-23 09:36:11 +03001306
1307 mutex_enter(&lsp->ls_vp_lock);
1308 lsp->ls_vp_iocount++;
1309 mutex_exit(&lsp->ls_vp_lock);
1310
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001311 bp_mapin(bp);
1312 bufaddr = bp->b_un.b_addr;
Toomas Soome406fc512015-10-23 09:36:11 +03001313 offset = (bp->b_lblkno + (diskaddr_t)(uintptr_t)bp->b_private)
1314 << lsp->ls_lbshift; /* offset within file */
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001315 if (lsp->ls_crypto_enabled) {
1316 /* encrypted data really begins after crypto header */
1317 offset += lsp->ls_crypto_offset;
1318 }
1319 len = bp->b_bcount;
1320 bufinited = B_TRUE;
1321
1322 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1323 error = EIO;
1324 goto errout;
1325 }
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001326
1327 /*
batschul44a1e322010-05-10 08:19:18 -07001328 * If we're writing and the buffer was not B_ASYNC
1329 * we'll follow up with a VOP_FSYNC() to force any
1330 * asynchronous I/O to stable storage.
1331 */
1332 if (!(bp->b_flags & B_READ) && !(bp->b_flags & B_ASYNC))
1333 syncflag = FSYNC;
1334
1335 /*
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001336 * We used to always use vn_rdwr here, but we cannot do that because
1337 * we might decide to read or write from the the underlying
1338 * file during this call, which would be a deadlock because
1339 * we have the rw_lock. So instead we page, unless it's not
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001340 * mapable or it's a character device or it's an encrypted lofi.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001341 */
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001342 if ((lsp->ls_vp->v_flag & VNOMAP) || (lsp->ls_vp->v_type == VCHR) ||
1343 lsp->ls_crypto_enabled) {
1344 error = lofi_rdwr(bufaddr, offset, bp, lsp, len, RDWR_RAW,
1345 NULL);
1346 } else if (lsp->ls_uncomp_seg_sz == 0) {
1347 error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
1348 } else {
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001349 uchar_t *compressed_seg = NULL, *cmpbuf;
1350 uchar_t *uncompressed_seg = NULL;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001351 lofi_compress_info_t *li;
1352 size_t oblkcount;
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001353 ulong_t seglen;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001354 uint64_t sblkno, eblkno, cmpbytes;
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001355 uint64_t uncompressed_seg_index;
1356 struct lofi_comp_cache *lc;
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001357 offset_t sblkoff, eblkoff;
1358 u_offset_t salign, ealign;
1359 u_offset_t sdiff;
1360 uint32_t comp_data_sz;
aalok87117652007-12-07 14:08:39 -08001361 uint64_t i;
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001362 int j;
aalok87117652007-12-07 14:08:39 -08001363
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001364 /*
aalok87117652007-12-07 14:08:39 -08001365 * From here on we're dealing primarily with compressed files
1366 */
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001367 ASSERT(!lsp->ls_crypto_enabled);
aalok87117652007-12-07 14:08:39 -08001368
1369 /*
1370 * Compressed files can only be read from and
1371 * not written to
1372 */
1373 if (!(bp->b_flags & B_READ)) {
1374 bp->b_resid = bp->b_bcount;
1375 error = EROFS;
1376 goto done;
1377 }
1378
1379 ASSERT(lsp->ls_comp_algorithm_index >= 0);
1380 li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
1381 /*
1382 * Compute starting and ending compressed segment numbers
1383 * We use only bitwise operations avoiding division and
1384 * modulus because we enforce the compression segment size
1385 * to a power of 2
1386 */
1387 sblkno = offset >> lsp->ls_comp_seg_shift;
1388 sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
1389 eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
1390 eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
1391
1392 /*
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001393 * Check the decompressed segment cache.
1394 *
1395 * The cache is used only when the requested data
1396 * is within a segment. Requests that cross
1397 * segment boundaries bypass the cache.
1398 */
1399 if (sblkno == eblkno ||
1400 (sblkno + 1 == eblkno && eblkoff == 0)) {
1401 /*
1402 * Request doesn't cross a segment boundary,
1403 * now check the cache.
1404 */
1405 mutex_enter(&lsp->ls_comp_cache_lock);
1406 lc = lofi_find_comp_data(lsp, sblkno);
1407 if (lc != NULL) {
1408 /*
1409 * We've found the decompressed segment
1410 * data in the cache; reuse it.
1411 */
1412 bcopy(lc->lc_data + sblkoff, bufaddr,
1413 bp->b_bcount);
1414 mutex_exit(&lsp->ls_comp_cache_lock);
1415 bp->b_resid = 0;
1416 error = 0;
1417 goto done;
1418 }
1419 mutex_exit(&lsp->ls_comp_cache_lock);
1420 }
1421
1422 /*
aalok87117652007-12-07 14:08:39 -08001423 * Align start offset to block boundary for segmap
1424 */
1425 salign = lsp->ls_comp_seg_index[sblkno];
1426 sdiff = salign & (DEV_BSIZE - 1);
1427 salign -= sdiff;
1428 if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001429 /*
aalok87117652007-12-07 14:08:39 -08001430 * We're dealing with the last segment of
1431 * the compressed file -- the size of this
1432 * segment *may not* be the same as the
1433 * segment size for the file
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001434 */
aalok87117652007-12-07 14:08:39 -08001435 eblkoff = (offset + bp->b_bcount) &
1436 (lsp->ls_uncomp_last_seg_sz - 1);
1437 ealign = lsp->ls_vp_comp_size;
1438 } else {
1439 ealign = lsp->ls_comp_seg_index[eblkno + 1];
1440 }
1441
1442 /*
1443 * Preserve original request paramaters
1444 */
1445 oblkcount = bp->b_bcount;
1446
1447 /*
1448 * Assign the calculated parameters
1449 */
1450 comp_data_sz = ealign - salign;
1451 bp->b_bcount = comp_data_sz;
1452
1453 /*
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001454 * Buffers to hold compressed segments are pre-allocated
1455 * on a per-thread basis. Find a pre-allocated buffer
1456 * that is not currently in use and mark it for use.
aalok87117652007-12-07 14:08:39 -08001457 */
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001458 mutex_enter(&lsp->ls_comp_bufs_lock);
1459 for (j = 0; j < lofi_taskq_nthreads; j++) {
1460 if (lsp->ls_comp_bufs[j].inuse == 0) {
1461 lsp->ls_comp_bufs[j].inuse = 1;
1462 break;
1463 }
1464 }
1465
1466 mutex_exit(&lsp->ls_comp_bufs_lock);
1467 ASSERT(j < lofi_taskq_nthreads);
1468
1469 /*
1470 * If the pre-allocated buffer size does not match
1471 * the size of the I/O request, re-allocate it with
1472 * the appropriate size
1473 */
1474 if (lsp->ls_comp_bufs[j].bufsize < bp->b_bcount) {
1475 if (lsp->ls_comp_bufs[j].bufsize > 0)
1476 kmem_free(lsp->ls_comp_bufs[j].buf,
1477 lsp->ls_comp_bufs[j].bufsize);
1478 lsp->ls_comp_bufs[j].buf = kmem_alloc(bp->b_bcount,
1479 KM_SLEEP);
1480 lsp->ls_comp_bufs[j].bufsize = bp->b_bcount;
1481 }
1482 compressed_seg = lsp->ls_comp_bufs[j].buf;
1483
aalok87117652007-12-07 14:08:39 -08001484 /*
1485 * Map in the calculated number of blocks
1486 */
1487 error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
1488 bp, lsp);
1489
1490 bp->b_bcount = oblkcount;
1491 bp->b_resid = oblkcount;
1492 if (error != 0)
1493 goto done;
1494
1495 /*
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001496 * decompress compressed blocks start
aalok87117652007-12-07 14:08:39 -08001497 */
1498 cmpbuf = compressed_seg + sdiff;
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001499 for (i = sblkno; i <= eblkno; i++) {
1500 ASSERT(i < lsp->ls_comp_index_sz - 1);
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001501 uchar_t *useg;
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001502
1503 /*
1504 * The last segment is special in that it is
1505 * most likely not going to be the same
1506 * (uncompressed) size as the other segments.
1507 */
1508 if (i == (lsp->ls_comp_index_sz - 2)) {
1509 seglen = lsp->ls_uncomp_last_seg_sz;
1510 } else {
1511 seglen = lsp->ls_uncomp_seg_sz;
1512 }
1513
aalok87117652007-12-07 14:08:39 -08001514 /*
1515 * Each of the segment index entries contains
1516 * the starting block number for that segment.
1517 * The number of compressed bytes in a segment
1518 * is thus the difference between the starting
1519 * block number of this segment and the starting
1520 * block number of the next segment.
1521 */
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001522 cmpbytes = lsp->ls_comp_seg_index[i + 1] -
1523 lsp->ls_comp_seg_index[i];
aalok87117652007-12-07 14:08:39 -08001524
1525 /*
1526 * The first byte in a compressed segment is a flag
1527 * that indicates whether this segment is compressed
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001528 * at all.
1529 *
1530 * The variable 'useg' is used (instead of
1531 * uncompressed_seg) in this loop to keep a
1532 * reference to the uncompressed segment.
1533 *
1534 * N.B. If 'useg' is replaced with uncompressed_seg,
1535 * it leads to memory leaks and heap corruption in
1536 * corner cases where compressed segments lie
1537 * adjacent to uncompressed segments.
aalok87117652007-12-07 14:08:39 -08001538 */
1539 if (*cmpbuf == UNCOMPRESSED) {
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001540 useg = cmpbuf + SEGHDR;
aalok87117652007-12-07 14:08:39 -08001541 } else {
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001542 if (uncompressed_seg == NULL)
1543 uncompressed_seg =
1544 kmem_alloc(lsp->ls_uncomp_seg_sz,
1545 KM_SLEEP);
1546 useg = uncompressed_seg;
1547 uncompressed_seg_index = i;
1548
aalok87117652007-12-07 14:08:39 -08001549 if (li->l_decompress((cmpbuf + SEGHDR),
1550 (cmpbytes - SEGHDR), uncompressed_seg,
1551 &seglen, li->l_level) != 0) {
1552 error = EIO;
1553 goto done;
1554 }
1555 }
1556
1557 /*
1558 * Determine how much uncompressed data we
1559 * have to copy and copy it
1560 */
1561 xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
Alok Aggarwalb1efbcd2009-03-08 16:47:15 -04001562 if (i == eblkno)
1563 xfersize -= (lsp->ls_uncomp_seg_sz - eblkoff);
aalok87117652007-12-07 14:08:39 -08001564
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001565 bcopy((useg + sblkoff), bufaddr, xfersize);
aalok87117652007-12-07 14:08:39 -08001566
1567 cmpbuf += cmpbytes;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001568 bufaddr += xfersize;
aalok87117652007-12-07 14:08:39 -08001569 bp->b_resid -= xfersize;
1570 sblkoff = 0;
1571
1572 if (bp->b_resid == 0)
1573 break;
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001574 } /* decompress compressed blocks ends */
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001575
1576 /*
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001577 * Skip to done if there is no uncompressed data to cache
1578 */
1579 if (uncompressed_seg == NULL)
1580 goto done;
1581
1582 /*
1583 * Add the data for the last decompressed segment to
jrgn.keil@googlemail.com4058a202009-03-16 15:15:29 -06001584 * the cache.
1585 *
1586 * In case the uncompressed segment data was added to (and
1587 * is referenced by) the cache, make sure we don't free it
1588 * here.
1589 */
1590 mutex_enter(&lsp->ls_comp_cache_lock);
1591 if ((lc = lofi_add_comp_data(lsp, uncompressed_seg_index,
1592 uncompressed_seg)) != NULL) {
1593 uncompressed_seg = NULL;
1594 }
1595 mutex_exit(&lsp->ls_comp_cache_lock);
1596
aalok87117652007-12-07 14:08:39 -08001597done:
Alok Aggarwalb9c7fb02010-05-11 10:12:30 -07001598 if (compressed_seg != NULL) {
1599 mutex_enter(&lsp->ls_comp_bufs_lock);
1600 lsp->ls_comp_bufs[j].inuse = 0;
1601 mutex_exit(&lsp->ls_comp_bufs_lock);
1602 }
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001603 if (uncompressed_seg != NULL)
1604 kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
1605 } /* end of handling compressed files */
aalok87117652007-12-07 14:08:39 -08001606
batschul44a1e322010-05-10 08:19:18 -07001607 if ((error == 0) && (syncflag != 0))
1608 error = VOP_FSYNC(lsp->ls_vp, syncflag, kcred, NULL);
1609
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001610errout:
1611 if (bufinited && lsp->ls_kstat) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001612 size_t n_done = bp->b_bcount - bp->b_resid;
1613 kstat_io_t *kioptr;
1614
1615 mutex_enter(lsp->ls_kstat->ks_lock);
1616 kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
1617 if (bp->b_flags & B_READ) {
1618 kioptr->nread += n_done;
1619 kioptr->reads++;
1620 } else {
1621 kioptr->nwritten += n_done;
1622 kioptr->writes++;
1623 }
1624 kstat_runq_exit(kioptr);
1625 mutex_exit(lsp->ls_kstat->ks_lock);
1626 }
eschrock3d7072f2007-06-12 13:18:17 -07001627
1628 mutex_enter(&lsp->ls_vp_lock);
1629 if (--lsp->ls_vp_iocount == 0)
1630 cv_broadcast(&lsp->ls_vp_cv);
1631 mutex_exit(&lsp->ls_vp_lock);
1632
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001633 bioerror(bp, error);
1634 biodone(bp);
1635}
1636
1637static int
1638lofi_strategy(struct buf *bp)
1639{
1640 struct lofi_state *lsp;
1641 offset_t offset;
Toomas Soome406fc512015-10-23 09:36:11 +03001642 minor_t part;
1643 diskaddr_t p_lba;
1644 diskaddr_t p_nblks;
1645 int shift;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001646
1647 /*
1648 * We cannot just do I/O here, because the current thread
1649 * _might_ end up back in here because the underlying filesystem
1650 * wants a buffer, which eventually gets into bio_recycle and
1651 * might call into lofi to write out a delayed-write buffer.
1652 * This is bad if the filesystem above lofi is the same as below.
1653 *
1654 * We could come up with a complex strategy using threads to
1655 * do the I/O asynchronously, or we could use task queues. task
1656 * queues were incredibly easy so they win.
1657 */
Toomas Soome406fc512015-10-23 09:36:11 +03001658
1659 lsp = ddi_get_soft_state(lofi_statep,
1660 LOFI_MINOR2ID(getminor(bp->b_edev)));
1661 part = LOFI_PART(getminor(bp->b_edev));
1662
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001663 if (lsp == NULL) {
1664 bioerror(bp, ENXIO);
1665 biodone(bp);
1666 return (0);
1667 }
Toomas Soome406fc512015-10-23 09:36:11 +03001668
Toomas Soomeb4844712016-10-13 00:15:40 +03001669 /* Check if we are closing. */
1670 mutex_enter(&lsp->ls_vp_lock);
1671 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
1672 mutex_exit(&lsp->ls_vp_lock);
1673 bioerror(bp, EIO);
1674 biodone(bp);
1675 return (0);
1676 }
1677 mutex_exit(&lsp->ls_vp_lock);
1678
1679 shift = lsp->ls_lbshift;
Toomas Soome406fc512015-10-23 09:36:11 +03001680 p_lba = 0;
1681 p_nblks = lsp->ls_vp_size >> shift;
1682
1683 if (lsp->ls_cmlbhandle != NULL) {
1684 if (cmlb_partinfo(lsp->ls_cmlbhandle, part, &p_nblks, &p_lba,
1685 NULL, NULL, 0)) {
1686 bioerror(bp, ENXIO);
1687 biodone(bp);
1688 return (0);
1689 }
1690 }
1691
1692 /* start block past partition end? */
1693 if (bp->b_lblkno > p_nblks) {
1694 bioerror(bp, ENXIO);
1695 biodone(bp);
1696 return (0);
1697 }
1698
1699 offset = (bp->b_lblkno+p_lba) << shift; /* offset within file */
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001700
eschrock3d7072f2007-06-12 13:18:17 -07001701 mutex_enter(&lsp->ls_vp_lock);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001702 if (lsp->ls_crypto_enabled) {
1703 /* encrypted data really begins after crypto header */
1704 offset += lsp->ls_crypto_offset;
1705 }
Toomas Soome406fc512015-10-23 09:36:11 +03001706
1707 /* make sure we will not pass the file or partition size */
1708 if (offset == lsp->ls_vp_size ||
1709 offset == (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001710 /* EOF */
1711 if ((bp->b_flags & B_READ) != 0) {
1712 bp->b_resid = bp->b_bcount;
1713 bioerror(bp, 0);
1714 } else {
1715 /* writes should fail */
1716 bioerror(bp, ENXIO);
1717 }
1718 biodone(bp);
eschrock3d7072f2007-06-12 13:18:17 -07001719 mutex_exit(&lsp->ls_vp_lock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001720 return (0);
1721 }
Toomas Soome406fc512015-10-23 09:36:11 +03001722 if ((offset > lsp->ls_vp_size) ||
1723 (offset > (((p_lba + p_nblks) << shift) + lsp->ls_crypto_offset)) ||
1724 ((offset + bp->b_bcount) > ((p_lba + p_nblks) << shift))) {
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001725 bioerror(bp, ENXIO);
1726 biodone(bp);
eschrock3d7072f2007-06-12 13:18:17 -07001727 mutex_exit(&lsp->ls_vp_lock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001728 return (0);
1729 }
Toomas Soome406fc512015-10-23 09:36:11 +03001730
eschrock3d7072f2007-06-12 13:18:17 -07001731 mutex_exit(&lsp->ls_vp_lock);
1732
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001733 if (lsp->ls_kstat) {
1734 mutex_enter(lsp->ls_kstat->ks_lock);
1735 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
1736 mutex_exit(lsp->ls_kstat->ks_lock);
1737 }
Toomas Soome406fc512015-10-23 09:36:11 +03001738 bp->b_private = (void *)(uintptr_t)p_lba; /* partition start */
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001739 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
1740 return (0);
1741}
1742
1743/*ARGSUSED2*/
1744static int
1745lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
1746{
1747 if (getminor(dev) == 0)
1748 return (EINVAL);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001749 UIO_CHECK(uio);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001750 return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
1751}
1752
1753/*ARGSUSED2*/
1754static int
1755lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
1756{
1757 if (getminor(dev) == 0)
1758 return (EINVAL);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001759 UIO_CHECK(uio);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001760 return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
1761}
1762
1763/*ARGSUSED2*/
1764static int
1765lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
1766{
1767 if (getminor(dev) == 0)
1768 return (EINVAL);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001769 UIO_CHECK(aio->aio_uio);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001770 return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
1771}
1772
1773/*ARGSUSED2*/
1774static int
1775lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
1776{
1777 if (getminor(dev) == 0)
1778 return (EINVAL);
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08001779 UIO_CHECK(aio->aio_uio);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001780 return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
1781}
1782
1783/*ARGSUSED*/
1784static int
1785lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1786{
Toomas Soome406fc512015-10-23 09:36:11 +03001787 struct lofi_state *lsp;
1788 dev_t dev = (dev_t)arg;
1789 int instance;
1790
1791 instance = LOFI_MINOR2ID(getminor(dev));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001792 switch (infocmd) {
1793 case DDI_INFO_DEVT2DEVINFO:
Toomas Soome406fc512015-10-23 09:36:11 +03001794 lsp = ddi_get_soft_state(lofi_statep, instance);
1795 if (lsp == NULL)
1796 return (DDI_FAILURE);
1797 *result = lsp->ls_dip;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001798 return (DDI_SUCCESS);
1799 case DDI_INFO_DEVT2INSTANCE:
Toomas Soome406fc512015-10-23 09:36:11 +03001800 *result = (void *) (intptr_t)instance;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001801 return (DDI_SUCCESS);
1802 }
1803 return (DDI_FAILURE);
1804}
1805
1806static int
Toomas Soome406fc512015-10-23 09:36:11 +03001807lofi_create_minor_nodes(struct lofi_state *lsp, boolean_t labeled)
1808{
1809 int error = 0;
1810 int instance = ddi_get_instance(lsp->ls_dip);
1811
1812 if (labeled == B_TRUE) {
1813 cmlb_alloc_handle(&lsp->ls_cmlbhandle);
1814 error = cmlb_attach(lsp->ls_dip, &lofi_tg_ops, DTYPE_DIRECT,
1815 B_FALSE, B_FALSE, DDI_NT_BLOCK_CHAN,
1816 CMLB_CREATE_P0_MINOR_NODE, lsp->ls_cmlbhandle, (void *)1);
1817
1818 if (error != DDI_SUCCESS) {
1819 cmlb_free_handle(&lsp->ls_cmlbhandle);
1820 lsp->ls_cmlbhandle = NULL;
1821 error = ENXIO;
1822 }
1823 } else {
1824 /* create minor nodes */
1825 error = ddi_create_minor_node(lsp->ls_dip, LOFI_BLOCK_NODE,
1826 S_IFBLK, LOFI_ID2MINOR(instance), DDI_PSEUDO, 0);
1827 if (error == DDI_SUCCESS) {
1828 error = ddi_create_minor_node(lsp->ls_dip,
1829 LOFI_CHAR_NODE, S_IFCHR, LOFI_ID2MINOR(instance),
1830 DDI_PSEUDO, 0);
1831 if (error != DDI_SUCCESS) {
1832 ddi_remove_minor_node(lsp->ls_dip,
1833 LOFI_BLOCK_NODE);
1834 error = ENXIO;
1835 }
1836 } else
1837 error = ENXIO;
1838 }
1839 return (error);
1840}
1841
1842static int
1843lofi_zone_bind(struct lofi_state *lsp)
1844{
1845 int error = 0;
1846
1847 mutex_enter(&curproc->p_lock);
1848 if ((error = rctl_incr_lofi(curproc, curproc->p_zone, 1)) != 0) {
1849 mutex_exit(&curproc->p_lock);
1850 return (error);
1851 }
1852 mutex_exit(&curproc->p_lock);
1853
Toomas Soome450b24a2016-06-28 19:04:10 +03001854 if (ddi_prop_update_string(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME,
Toomas Soome406fc512015-10-23 09:36:11 +03001855 (char *)curproc->p_zone->zone_name) != DDI_PROP_SUCCESS) {
1856 rctl_decr_lofi(curproc->p_zone, 1);
1857 error = EINVAL;
1858 } else {
1859 zone_init_ref(&lsp->ls_zone);
1860 zone_hold_ref(curzone, &lsp->ls_zone, ZONE_REF_LOFI);
1861 }
1862 return (error);
1863}
1864
1865static void
1866lofi_zone_unbind(struct lofi_state *lsp)
1867{
1868 (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip, ZONE_PROP_NAME);
1869 rctl_decr_lofi(curproc->p_zone, 1);
1870 zone_rele_ref(&lsp->ls_zone, ZONE_REF_LOFI);
1871}
1872
1873static int
1874lofi_online_dev(dev_info_t *dip)
1875{
1876 boolean_t labeled;
1877 int error;
1878 int instance = ddi_get_instance(dip);
1879 struct lofi_state *lsp;
1880
1881 labeled = B_FALSE;
1882 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "labeled"))
1883 labeled = B_TRUE;
1884
1885 /* lsp alloc+init, soft state is freed in lofi_detach */
1886 error = ddi_soft_state_zalloc(lofi_statep, instance);
1887 if (error == DDI_FAILURE) {
1888 return (ENOMEM);
1889 }
1890
1891 lsp = ddi_get_soft_state(lofi_statep, instance);
1892 lsp->ls_dip = dip;
1893
1894 if ((error = lofi_zone_bind(lsp)) != 0)
1895 goto err;
1896
1897 cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
1898 mutex_init(&lsp->ls_comp_cache_lock, NULL, MUTEX_DRIVER, NULL);
1899 mutex_init(&lsp->ls_comp_bufs_lock, NULL, MUTEX_DRIVER, NULL);
1900 mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
1901 mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
1902
1903 if ((error = lofi_create_minor_nodes(lsp, labeled)) != 0) {
1904 lofi_zone_unbind(lsp);
1905 goto lerr;
1906 }
1907
1908 /* driver handles kernel-issued IOCTLs */
1909 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1910 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1911 error = DDI_FAILURE;
1912 goto merr;
1913 }
1914
1915 lsp->ls_kstat = kstat_create_zone(LOFI_DRIVER_NAME, instance,
1916 NULL, "disk", KSTAT_TYPE_IO, 1, 0, getzoneid());
1917 if (lsp->ls_kstat == NULL) {
1918 (void) ddi_prop_remove(DDI_DEV_T_NONE, lsp->ls_dip,
1919 DDI_KERNEL_IOCTL);
1920 error = ENOMEM;
1921 goto merr;
1922 }
1923
1924 lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
1925 kstat_zone_add(lsp->ls_kstat, GLOBAL_ZONEID);
1926 kstat_install(lsp->ls_kstat);
1927 return (DDI_SUCCESS);
1928merr:
1929 if (lsp->ls_cmlbhandle != NULL) {
1930 cmlb_detach(lsp->ls_cmlbhandle, 0);
1931 cmlb_free_handle(&lsp->ls_cmlbhandle);
1932 }
1933 ddi_remove_minor_node(dip, NULL);
1934 lofi_zone_unbind(lsp);
1935lerr:
1936 mutex_destroy(&lsp->ls_comp_cache_lock);
1937 mutex_destroy(&lsp->ls_comp_bufs_lock);
1938 mutex_destroy(&lsp->ls_kstat_lock);
1939 mutex_destroy(&lsp->ls_vp_lock);
1940 cv_destroy(&lsp->ls_vp_cv);
1941err:
1942 ddi_soft_state_free(lofi_statep, instance);
1943 return (error);
1944}
1945
Toomas Soome406fc512015-10-23 09:36:11 +03001946static int
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001947lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1948{
Toomas Soome406fc512015-10-23 09:36:11 +03001949 int rv;
1950 int instance = ddi_get_instance(dip);
1951 struct lofi_state *lsp;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001952
1953 if (cmd != DDI_ATTACH)
1954 return (DDI_FAILURE);
John Levon0fbb7512010-06-16 10:02:44 -07001955
Toomas Soome406fc512015-10-23 09:36:11 +03001956 /*
1957 * Instance 0 is control instance, attaching control instance
1958 * will set the lofi up and ready.
1959 */
1960 if (instance == 0) {
1961 rv = ddi_soft_state_zalloc(lofi_statep, 0);
1962 if (rv == DDI_FAILURE) {
1963 return (DDI_FAILURE);
1964 }
1965 lsp = ddi_get_soft_state(lofi_statep, instance);
1966 rv = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
1967 DDI_PSEUDO, 0);
1968 if (rv == DDI_FAILURE) {
1969 ddi_soft_state_free(lofi_statep, 0);
1970 return (DDI_FAILURE);
1971 }
1972 /* driver handles kernel-issued IOCTLs */
1973 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1974 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
1975 ddi_remove_minor_node(dip, NULL);
1976 ddi_soft_state_free(lofi_statep, 0);
1977 return (DDI_FAILURE);
1978 }
John Levon0fbb7512010-06-16 10:02:44 -07001979
Toomas Soome406fc512015-10-23 09:36:11 +03001980 zone_key_create(&lofi_zone_key, NULL, lofi_zone_shutdown, NULL);
John Levon0fbb7512010-06-16 10:02:44 -07001981
Toomas Soome406fc512015-10-23 09:36:11 +03001982 lsp->ls_dip = dip;
1983 } else {
1984 if (lofi_online_dev(dip) == DDI_FAILURE)
1985 return (DDI_FAILURE);
johnlev843e1982007-09-18 15:46:43 -07001986 }
John Levon0fbb7512010-06-16 10:02:44 -07001987
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001988 ddi_report_dev(dip);
1989 return (DDI_SUCCESS);
1990}
1991
1992static int
1993lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1994{
Toomas Soome406fc512015-10-23 09:36:11 +03001995 struct lofi_state *lsp;
1996 int instance = ddi_get_instance(dip);
1997
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07001998 if (cmd != DDI_DETACH)
1999 return (DDI_FAILURE);
John Levon0fbb7512010-06-16 10:02:44 -07002000
Toomas Soome406fc512015-10-23 09:36:11 +03002001 /*
2002 * If the instance is not 0, release state.
2003 * The instance 0 is control device, we can not detach it
2004 * before other instances are detached.
2005 */
2006 if (instance != 0) {
2007 lsp = ddi_get_soft_state(lofi_statep, instance);
2008 if (lsp != NULL && lsp->ls_vp_ready == B_FALSE) {
2009 ddi_soft_state_free(lofi_statep, instance);
2010 return (DDI_SUCCESS);
2011 } else
2012 return (DDI_FAILURE);
2013 }
John Levon0fbb7512010-06-16 10:02:44 -07002014 mutex_enter(&lofi_lock);
2015
2016 if (!list_is_empty(&lofi_list)) {
2017 mutex_exit(&lofi_lock);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002018 return (DDI_FAILURE);
John Levon0fbb7512010-06-16 10:02:44 -07002019 }
2020
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002021 ddi_remove_minor_node(dip, NULL);
johnlev843e1982007-09-18 15:46:43 -07002022 ddi_prop_remove_all(dip);
John Levon0fbb7512010-06-16 10:02:44 -07002023
2024 mutex_exit(&lofi_lock);
2025
2026 if (zone_key_delete(lofi_zone_key) != 0)
2027 cmn_err(CE_WARN, "failed to delete zone key");
2028
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002029 ddi_soft_state_free(lofi_statep, 0);
John Levon0fbb7512010-06-16 10:02:44 -07002030
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002031 return (DDI_SUCCESS);
2032}
2033
2034/*
Toomas Soome406fc512015-10-23 09:36:11 +03002035 * With the addition of encryption, we must be careful that encryption key is
2036 * wiped before kernel's data structures are freed so it cannot accidentally
2037 * slip out to userland through uninitialized data elsewhere.
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002038 */
2039static void
2040free_lofi_ioctl(struct lofi_ioctl *klip)
2041{
2042 /* Make sure this encryption key doesn't stick around */
2043 bzero(klip->li_key, sizeof (klip->li_key));
2044 kmem_free(klip, sizeof (struct lofi_ioctl));
2045}
2046
2047/*
Toomas Soome406fc512015-10-23 09:36:11 +03002048 * These two functions simplify the rest of the ioctls that need to copyin/out
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002049 * the lofi_ioctl structure.
2050 */
John Levon0fbb7512010-06-16 10:02:44 -07002051int
2052copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, struct lofi_ioctl **klipp,
2053 int flag)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002054{
2055 struct lofi_ioctl *klip;
2056 int error;
2057
John Levon0fbb7512010-06-16 10:02:44 -07002058 klip = *klipp = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
heppobd07e072006-03-22 10:04:02 -08002059 error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
John Levon0fbb7512010-06-16 10:02:44 -07002060 if (error)
2061 goto err;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002062
John Levon0fbb7512010-06-16 10:02:44 -07002063 /* ensure NULL termination */
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002064 klip->li_filename[MAXPATHLEN-1] = '\0';
Toomas Soome406fc512015-10-23 09:36:11 +03002065 klip->li_devpath[MAXPATHLEN-1] = '\0';
John Levon0fbb7512010-06-16 10:02:44 -07002066 klip->li_algorithm[MAXALGLEN-1] = '\0';
2067 klip->li_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
2068 klip->li_iv_cipher[CRYPTO_MAX_MECH_NAME-1] = '\0';
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002069
Toomas Soome406fc512015-10-23 09:36:11 +03002070 if (klip->li_id > L_MAXMIN32) {
John Levon0fbb7512010-06-16 10:02:44 -07002071 error = EINVAL;
2072 goto err;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002073 }
John Levon0fbb7512010-06-16 10:02:44 -07002074
2075 return (0);
2076
2077err:
2078 free_lofi_ioctl(klip);
2079 return (error);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002080}
2081
2082int
heppobd07e072006-03-22 10:04:02 -08002083copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
Toomas Soome406fc512015-10-23 09:36:11 +03002084 int flag)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002085{
2086 int error;
2087
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002088 /*
2089 * NOTE: Do NOT copy the crypto_key_t "back" to userland.
2090 * This ensures that an attacker can't trivially find the
2091 * key for a mapping just by issuing the ioctl.
2092 *
2093 * It can still be found by poking around in kmem with mdb(1),
2094 * but there is no point in making it easy when the info isn't
2095 * of any use in this direction anyway.
2096 *
2097 * Either way we don't actually have the raw key stored in
2098 * a form that we can get it anyway, since we just used it
2099 * to create a ctx template and didn't keep "the original".
2100 */
heppobd07e072006-03-22 10:04:02 -08002101 error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002102 if (error)
2103 return (EFAULT);
2104 return (0);
2105}
2106
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002107static int
John Levon0fbb7512010-06-16 10:02:44 -07002108lofi_access(struct lofi_state *lsp)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002109{
John Levon0fbb7512010-06-16 10:02:44 -07002110 ASSERT(MUTEX_HELD(&lofi_lock));
jv227347a19609f2010-08-12 17:34:26 -07002111 if (INGLOBALZONE(curproc) || lsp->ls_zone.zref_zone == curzone)
John Levon0fbb7512010-06-16 10:02:44 -07002112 return (0);
2113 return (EPERM);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002114}
2115
2116/*
John Levon0fbb7512010-06-16 10:02:44 -07002117 * Find the lofi state for the given filename. We compare by vnode to
2118 * allow the global zone visibility into NGZ lofi nodes.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002119 */
2120static int
Alexander Eremincd69fab2013-09-23 10:41:22 +04002121file_to_lofi_nocheck(char *filename, boolean_t readonly,
2122 struct lofi_state **lspp)
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002123{
John Levon0fbb7512010-06-16 10:02:44 -07002124 struct lofi_state *lsp;
2125 vnode_t *vp = NULL;
2126 int err = 0;
Alexander Eremincd69fab2013-09-23 10:41:22 +04002127 int rdfiles = 0;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002128
John Levon0fbb7512010-06-16 10:02:44 -07002129 ASSERT(MUTEX_HELD(&lofi_lock));
2130
2131 if ((err = lookupname(filename, UIO_SYSSPACE, FOLLOW,
2132 NULLVPP, &vp)) != 0)
2133 goto out;
2134
2135 if (vp->v_type == VREG) {
2136 vnode_t *realvp;
2137 if (VOP_REALVP(vp, &realvp, NULL) == 0) {
2138 VN_HOLD(realvp);
2139 VN_RELE(vp);
2140 vp = realvp;
2141 }
2142 }
2143
2144 for (lsp = list_head(&lofi_list); lsp != NULL;
2145 lsp = list_next(&lofi_list, lsp)) {
2146 if (lsp->ls_vp == vp) {
2147 if (lspp != NULL)
2148 *lspp = lsp;
Alexander Eremincd69fab2013-09-23 10:41:22 +04002149 if (lsp->ls_readonly) {
2150 rdfiles++;
2151 /* Skip if '-r' is specified */
2152 if (readonly)
2153 continue;
2154 }
John Levon0fbb7512010-06-16 10:02:44 -07002155 goto out;
2156 }
2157 }
2158
2159 err = ENOENT;
2160
Alexander Eremincd69fab2013-09-23 10:41:22 +04002161 /*
2162 * If a filename is given as an argument for lofi_unmap, we shouldn't
2163 * allow unmap if there are multiple read-only lofi devices associated
2164 * with this file.
2165 */
2166 if (lspp != NULL) {
2167 if (rdfiles == 1)
2168 err = 0;
2169 else if (rdfiles > 1)
2170 err = EBUSY;
2171 }
2172
John Levon0fbb7512010-06-16 10:02:44 -07002173out:
2174 if (vp != NULL)
2175 VN_RELE(vp);
2176 return (err);
2177}
2178
2179/*
2180 * Find the minor for the given filename, checking the zone can access
2181 * it.
2182 */
2183static int
Alexander Eremincd69fab2013-09-23 10:41:22 +04002184file_to_lofi(char *filename, boolean_t readonly, struct lofi_state **lspp)
John Levon0fbb7512010-06-16 10:02:44 -07002185{
2186 int err = 0;
2187
2188 ASSERT(MUTEX_HELD(&lofi_lock));
2189
Alexander Eremincd69fab2013-09-23 10:41:22 +04002190 if ((err = file_to_lofi_nocheck(filename, readonly, lspp)) != 0)
John Levon0fbb7512010-06-16 10:02:44 -07002191 return (err);
2192
2193 if ((err = lofi_access(*lspp)) != 0)
2194 return (err);
2195
2196 return (0);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002197}
2198
2199/*
Toomas Soome406fc512015-10-23 09:36:11 +03002200 * Fakes up a disk geometry based on the size of the file. This is needed
2201 * to support newfs on traditional lofi device, but also will provide
2202 * geometry hint for cmlb.
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002203 */
2204static void
2205fake_disk_geometry(struct lofi_state *lsp)
2206{
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002207 u_offset_t dsize = lsp->ls_vp_size - lsp->ls_crypto_offset;
2208
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002209 /* dk_geom - see dkio(7I) */
2210 /*
2211 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
2212 * of sectors), but that breaks programs like fdisk which want to
2213 * partition a disk by cylinder. With one cylinder, you can't create
2214 * an fdisk partition and put pcfs on it for testing (hard to pick
2215 * a number between one and one).
2216 *
2217 * The cheezy floppy test is an attempt to not have too few cylinders
2218 * for a small file, or so many on a big file that you waste space
2219 * for backup superblocks or cylinder group structures.
2220 */
Toomas Soome406fc512015-10-23 09:36:11 +03002221 bzero(&lsp->ls_dkg, sizeof (lsp->ls_dkg));
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002222 if (dsize < (2 * 1024 * 1024)) /* floppy? */
2223 lsp->ls_dkg.dkg_ncyl = dsize / (100 * 1024);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002224 else
Dina K Nimeh7d82f0f2008-12-08 16:24:12 -08002225 lsp->ls_dkg.dkg_ncyl = dsize / (300 * 1024);
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002226 /* in case file file is < 100k */
2227 if (lsp->ls_dkg.dkg_ncyl == 0)
2228 lsp->ls_dkg.dkg_ncyl = 1;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002229
Toomas Soome406fc512015-10-23 09:36:11 +03002230 lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl;
2231 lsp->ls_dkg.dkg_nhead = 1;
2232 lsp->ls_dkg.dkg_rpm = 7200;
2233
2234 lsp->ls_dkg.dkg_nsect = dsize /
2235 (lsp->ls_dkg.dkg_ncyl << lsp->ls_pbshift);
2236}
2237
2238/*
2239 * build vtoc - see dkio(7I)
2240 *
2241 * Fakes one big partition based on the size of the file. This is needed
2242 * because we allow newfs'ing the traditional lofi device and newfs will
2243 * do several disk ioctls to figure out the geometry and partition information.
2244 * It uses that information to determine the parameters to pass to mkfs.
2245 */
2246static void
2247fake_disk_vtoc(struct lofi_state *lsp, struct vtoc *vt)
2248{
2249 bzero(vt, sizeof (struct vtoc));
2250 vt->v_sanity = VTOC_SANE;
2251 vt->v_version = V_VERSION;
2252 (void) strncpy(vt->v_volume, LOFI_DRIVER_NAME,
2253 sizeof (vt->v_volume));
2254 vt->v_sectorsz = 1 << lsp->ls_pbshift;
2255 vt->v_nparts = 1;
2256 vt->v_part[0].p_tag = V_UNASSIGNED;
aalok87117652007-12-07 14:08:39 -08002257
2258 /*
2259 * A compressed file is read-only, other files can
2260 * be read-write
2261 */
2262 if (lsp->ls_uncomp_seg_sz > 0) {
Toomas Soome406fc512015-10-23 09:36:11 +03002263 vt->v_part[0].p_flag = V_UNMNT | V_RONLY;
aalok87117652007-12-07 14:08:39 -08002264 } else {
Toomas Soome406fc512015-10-23 09:36:11 +03002265 vt->v_part[0].p_flag = V_UNMNT;
aalok87117652007-12-07 14:08:39 -08002266 }
Toomas Soome406fc512015-10-23 09:36:11 +03002267 vt->v_part[0].p_start = (daddr_t)0;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002268 /*
2269 * The partition size cannot just be the number of sectors, because
2270 * that might not end on a cylinder boundary. And if that's the case,
2271 * newfs/mkfs will print a scary warning. So just figure the size
2272 * based on the number of cylinders and sectors/cylinder.
2273 */
Toomas Soome406fc512015-10-23 09:36:11 +03002274 vt->v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002275 lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
Toomas Soome406fc512015-10-23 09:36:11 +03002276}
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002277
Toomas Soome406fc512015-10-23 09:36:11 +03002278/*
2279 * build dk_cinfo - see dkio(7I)
2280 */
2281static void
2282fake_disk_info(dev_t dev, struct dk_cinfo *ci)
2283{
2284 bzero(ci, sizeof (struct dk_cinfo));
2285 (void) strlcpy(ci->dki_cname, LOFI_DRIVER_NAME, sizeof (ci->dki_cname));
2286 ci->dki_ctype = DKC_SCSI_CCS;
2287 (void) strlcpy(ci->dki_dname, LOFI_DRIVER_NAME, sizeof (ci->dki_dname));
2288 ci->dki_unit = LOFI_MINOR2ID(getminor(dev));
2289 ci->dki_partition = LOFI_PART(getminor(dev));
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002290 /*
2291 * newfs uses this to set maxcontig. Must not be < 16, or it
2292 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
2293 * it by the block size. Then tunefs doesn't work because
2294 * maxcontig is 0.
2295 */
Toomas Soome406fc512015-10-23 09:36:11 +03002296 ci->dki_maxtransfer = 16;
stevel@tonic-gate7c478bd2005-06-14 00:00:00 -07002297}
2298