Print this page
PSARC 2007/569 lofi(7D) compression support
6618343 lofi compression support
6603856 Lofi(7D) can thrash the page cache
| 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 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #pragma ident "%Z%%M% %I% %E% SMI"
27 27
28 28 /*
29 29 * lofi (loopback file) driver - allows you to attach a file to a device,
30 30 * which can then be accessed through that device. The simple model is that
31 31 * you tell lofi to open a file, and then use the block device you get as
32 32 * you would any block device. lofi translates access to the block device
33 33 * into I/O on the underlying file. This is mostly useful for
34 34 * mounting images of filesystems.
35 35 *
36 36 * lofi is controlled through /dev/lofictl - this is the only device exported
37 37 * during attach, and is minor number 0. lofiadm communicates with lofi through
38 38 * ioctls on this device. When a file is attached to lofi, block and character
39 39 * devices are exported in /dev/lofi and /dev/rlofi. Currently, these devices
40 40 * are identified by their minor number, and the minor number is also used
41 41 * as the name in /dev/lofi. If we ever decide to support virtual disks,
42 42 * we'll have to divide the minor number space to identify fdisk partitions
43 43 * and slices, and the name will then be the minor number shifted down a
44 44 * few bits. Minor devices are tracked with state structures handled with
45 45 * ddi_soft_state(9F) for simplicity.
46 46 *
47 47 * A file attached to lofi is opened when attached and not closed until
48 48 * explicitly detached from lofi. This seems more sensible than deferring
49 49 * the open until the /dev/lofi device is opened, for a number of reasons.
50 50 * One is that any failure is likely to be noticed by the person (or script)
51 51 * running lofiadm. Another is that it would be a security problem if the
52 52 * file was replaced by another one after being added but before being opened.
53 53 *
54 54 * The only hard part about lofi is the ioctls. In order to support things
55 55 * like 'newfs' on a lofi device, it needs to support certain disk ioctls.
56 56 * So it has to fake disk geometry and partition information. More may need
57 57 * to be faked if your favorite utility doesn't work and you think it should
58 58 * (fdformat doesn't work because it really wants to know the type of floppy
59 59 * controller to talk to, and that didn't seem easy to fake. Or possibly even
60 60 * necessary, since we have mkfs_pcfs now).
61 61 *
62 62 * Normally, a lofi device cannot be detached if it is open (i.e. busy). To
63 63 * support simulation of hotplug events, an optional force flag is provided.
64 64 * If a lofi device is open when a force detach is requested, then the
65 65 * underlying file is closed and any subsequent operations return EIO. When the
66 66 * device is closed for the last time, it will be cleaned up at that time. In
67 67 * addition, the DKIOCSTATE ioctl will return DKIO_DEV_GONE when the device is
68 68 * detached but not removed.
69 69 *
70 70 * Known problems:
71 71 *
72 72 * UFS logging. Mounting a UFS filesystem image "logging"
73 73 * works for basic copy testing but wedges during a build of ON through
74 74 * that image. Some deadlock in lufs holding the log mutex and then
75 75 * getting stuck on a buf. So for now, don't do that.
76 76 *
77 77 * Direct I/O. Since the filesystem data is being cached in the buffer
78 78 * cache, _and_ again in the underlying filesystem, it's tempting to
79 79 * enable direct I/O on the underlying file. Don't, because that deadlocks.
80 80 * I think to fix the cache-twice problem we might need filesystem support.
81 81 *
82 82 * lofi on itself. The simple lock strategy (lofi_lock) precludes this
83 83 * because you'll be in lofi_ioctl, holding the lock when you open the
84 84 * file, which, if it's lofi, will grab lofi_lock. We prevent this for
85 85 * now, though not using ddi_soft_state(9F) would make it possible to
86 86 * do. Though it would still be silly.
87 87 *
88 88 * Interesting things to do:
89 89 *
90 90 * Allow multiple files for each device. A poor-man's metadisk, basically.
91 91 *
|
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
92 92 * Pass-through ioctls on block devices. You can (though it's not
93 93 * documented), give lofi a block device as a file name. Then we shouldn't
94 94 * need to fake a geometry. But this is also silly unless you're replacing
95 95 * metadisk.
96 96 *
97 97 * Encryption. tpm would like this. Apparently Windows 2000 has it, and
98 98 * so does Linux.
99 99 */
100 100
101 101 #include <sys/types.h>
102 +#include <netinet/in.h>
102 103 #include <sys/sysmacros.h>
103 104 #include <sys/cmn_err.h>
104 105 #include <sys/uio.h>
105 106 #include <sys/kmem.h>
106 107 #include <sys/cred.h>
107 108 #include <sys/mman.h>
108 109 #include <sys/errno.h>
109 110 #include <sys/aio_req.h>
110 111 #include <sys/stat.h>
111 112 #include <sys/file.h>
112 113 #include <sys/modctl.h>
113 114 #include <sys/conf.h>
114 115 #include <sys/debug.h>
115 116 #include <sys/vnode.h>
|
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
116 117 #include <sys/lofi.h>
117 118 #include <sys/fcntl.h>
118 119 #include <sys/pathname.h>
119 120 #include <sys/filio.h>
120 121 #include <sys/fdio.h>
121 122 #include <sys/open.h>
122 123 #include <sys/disp.h>
123 124 #include <vm/seg_map.h>
124 125 #include <sys/ddi.h>
125 126 #include <sys/sunddi.h>
127 +#include <sys/zmod.h>
126 128
127 -/* seems safer than having to get the string right many times */
128 129 #define NBLOCKS_PROP_NAME "Nblocks"
129 -#define SIZE_PROP_NAME "Size"
130 +#define SIZE_PROP_NAME "Size"
130 131
131 132 static dev_info_t *lofi_dip;
132 133 static void *lofi_statep;
133 134 static kmutex_t lofi_lock; /* state lock */
134 135
135 136 /*
136 137 * Because lofi_taskq_nthreads limits the actual swamping of the device, the
137 138 * maxalloc parameter (lofi_taskq_maxalloc) should be tuned conservatively
138 139 * high. If we want to be assured that the underlying device is always busy,
139 140 * we must be sure that the number of bytes enqueued when the number of
140 141 * enqueued tasks exceeds maxalloc is sufficient to keep the device busy for
141 142 * the duration of the sleep time in taskq_ent_alloc(). That is, lofi should
|
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
142 143 * set maxalloc to be the maximum throughput (in bytes per second) of the
143 144 * underlying device divided by the minimum I/O size. We assume a realistic
144 145 * maximum throughput of one hundred megabytes per second; we set maxalloc on
145 146 * the lofi task queue to be 104857600 divided by DEV_BSIZE.
146 147 */
147 148 static int lofi_taskq_maxalloc = 104857600 / DEV_BSIZE;
148 149 static int lofi_taskq_nthreads = 4; /* # of taskq threads per device */
149 150
150 151 uint32_t lofi_max_files = LOFI_MAX_FILES;
151 152
153 +static int gzip_decompress(void *src, size_t srclen, void *dst,
154 + size_t *destlen, int level);
155 +
156 +lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
157 + {gzip_decompress, NULL, 6, "gzip"}, /* default */
158 + {gzip_decompress, NULL, 6, "gzip-6"},
159 + {gzip_decompress, NULL, 9, "gzip-9"}
160 +};
161 +
152 162 static int
153 163 lofi_busy(void)
154 164 {
155 165 minor_t minor;
156 166
157 167 /*
158 168 * We need to make sure no mappings exist - mod_remove won't
159 169 * help because the device isn't open.
160 170 */
161 171 mutex_enter(&lofi_lock);
162 172 for (minor = 1; minor <= lofi_max_files; minor++) {
163 173 if (ddi_get_soft_state(lofi_statep, minor) != NULL) {
164 174 mutex_exit(&lofi_lock);
165 175 return (EBUSY);
166 176 }
167 177 }
168 178 mutex_exit(&lofi_lock);
169 179 return (0);
170 180 }
171 181
172 182 static int
173 183 is_opened(struct lofi_state *lsp)
174 184 {
175 185 ASSERT(mutex_owned(&lofi_lock));
176 186 return (lsp->ls_chr_open || lsp->ls_blk_open || lsp->ls_lyr_open_count);
177 187 }
178 188
179 189 static int
180 190 mark_opened(struct lofi_state *lsp, int otyp)
181 191 {
182 192 ASSERT(mutex_owned(&lofi_lock));
183 193 switch (otyp) {
184 194 case OTYP_CHR:
185 195 lsp->ls_chr_open = 1;
186 196 break;
187 197 case OTYP_BLK:
188 198 lsp->ls_blk_open = 1;
189 199 break;
190 200 case OTYP_LYR:
191 201 lsp->ls_lyr_open_count++;
192 202 break;
193 203 default:
194 204 return (-1);
195 205 }
196 206 return (0);
197 207 }
198 208
199 209 static void
200 210 mark_closed(struct lofi_state *lsp, int otyp)
201 211 {
202 212 ASSERT(mutex_owned(&lofi_lock));
203 213 switch (otyp) {
204 214 case OTYP_CHR:
205 215 lsp->ls_chr_open = 0;
206 216 break;
207 217 case OTYP_BLK:
208 218 lsp->ls_blk_open = 0;
209 219 break;
210 220 case OTYP_LYR:
211 221 lsp->ls_lyr_open_count--;
212 222 break;
213 223 default:
214 224 break;
215 225 }
|
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
216 226 }
217 227
218 228 static void
219 229 lofi_free_handle(dev_t dev, minor_t minor, struct lofi_state *lsp,
220 230 cred_t *credp)
221 231 {
222 232 dev_t newdev;
223 233 char namebuf[50];
224 234
225 235 if (lsp->ls_vp) {
226 - (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag,
227 - 1, 0, credp, NULL);
236 + (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0, credp);
228 237 VN_RELE(lsp->ls_vp);
229 238 lsp->ls_vp = NULL;
230 239 }
231 240
232 241 newdev = makedevice(getmajor(dev), minor);
233 242 (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
234 243 (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
235 244
236 245 (void) snprintf(namebuf, sizeof (namebuf), "%d", minor);
237 246 ddi_remove_minor_node(lofi_dip, namebuf);
238 247 (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", minor);
239 248 ddi_remove_minor_node(lofi_dip, namebuf);
240 249
241 250 kmem_free(lsp->ls_filename, lsp->ls_filename_sz);
242 251 taskq_destroy(lsp->ls_taskq);
243 252 if (lsp->ls_kstat) {
244 253 kstat_delete(lsp->ls_kstat);
245 254 mutex_destroy(&lsp->ls_kstat_lock);
246 255 }
247 256 ddi_soft_state_free(lofi_statep, minor);
248 257 }
249 258
250 259 /*ARGSUSED*/
251 260 static int
252 261 lofi_open(dev_t *devp, int flag, int otyp, struct cred *credp)
253 262 {
254 263 minor_t minor;
255 264 struct lofi_state *lsp;
256 265
257 266 mutex_enter(&lofi_lock);
258 267 minor = getminor(*devp);
259 268 if (minor == 0) {
260 269 /* master control device */
261 270 /* must be opened exclusively */
262 271 if (((flag & FEXCL) != FEXCL) || (otyp != OTYP_CHR)) {
263 272 mutex_exit(&lofi_lock);
264 273 return (EINVAL);
265 274 }
266 275 lsp = ddi_get_soft_state(lofi_statep, 0);
267 276 if (lsp == NULL) {
268 277 mutex_exit(&lofi_lock);
269 278 return (ENXIO);
270 279 }
271 280 if (is_opened(lsp)) {
272 281 mutex_exit(&lofi_lock);
273 282 return (EBUSY);
274 283 }
275 284 (void) mark_opened(lsp, OTYP_CHR);
276 285 mutex_exit(&lofi_lock);
277 286 return (0);
278 287 }
279 288
280 289 /* otherwise, the mapping should already exist */
281 290 lsp = ddi_get_soft_state(lofi_statep, minor);
282 291 if (lsp == NULL) {
283 292 mutex_exit(&lofi_lock);
284 293 return (EINVAL);
285 294 }
286 295
287 296 if (lsp->ls_vp == NULL) {
288 297 mutex_exit(&lofi_lock);
289 298 return (ENXIO);
290 299 }
291 300
292 301 if (mark_opened(lsp, otyp) == -1) {
293 302 mutex_exit(&lofi_lock);
294 303 return (EINVAL);
295 304 }
296 305
297 306 mutex_exit(&lofi_lock);
298 307 return (0);
299 308 }
300 309
301 310 /*ARGSUSED*/
302 311 static int
303 312 lofi_close(dev_t dev, int flag, int otyp, struct cred *credp)
304 313 {
305 314 minor_t minor;
306 315 struct lofi_state *lsp;
307 316
308 317 mutex_enter(&lofi_lock);
309 318 minor = getminor(dev);
310 319 lsp = ddi_get_soft_state(lofi_statep, minor);
311 320 if (lsp == NULL) {
312 321 mutex_exit(&lofi_lock);
313 322 return (EINVAL);
314 323 }
315 324 mark_closed(lsp, otyp);
316 325
|
↓ open down ↓ |
79 lines elided |
↑ open up ↑ |
317 326 /*
318 327 * If we have forcibly closed the underlying device, and this is the
319 328 * last close, then tear down the rest of the device.
320 329 */
321 330 if (minor != 0 && lsp->ls_vp == NULL && !is_opened(lsp))
322 331 lofi_free_handle(dev, minor, lsp, credp);
323 332 mutex_exit(&lofi_lock);
324 333 return (0);
325 334 }
326 335
336 +static int
337 +lofi_mapped_rdwr(caddr_t bufaddr, offset_t offset, struct buf *bp,
338 + struct lofi_state *lsp)
339 +{
340 + int error;
341 + offset_t alignedoffset, mapoffset;
342 + size_t xfersize;
343 + int isread;
344 + int smflags;
345 + caddr_t mapaddr;
346 + size_t len;
347 + enum seg_rw srw;
348 +
349 + /*
350 + * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
351 + * an 8K boundary, but the buf transfer address may not be
352 + * aligned on more than a 512-byte boundary (we don't enforce
353 + * that even though we could). This matters since the initial
354 + * part of the transfer may not start at offset 0 within the
355 + * segmap'd chunk. So we have to compensate for that with
356 + * 'mapoffset'. Subsequent chunks always start off at the
357 + * beginning, and the last is capped by b_resid
358 + */
359 + mapoffset = offset & MAXBOFFSET;
360 + alignedoffset = offset - mapoffset;
361 + bp->b_resid = bp->b_bcount;
362 + isread = bp->b_flags & B_READ;
363 + srw = isread ? S_READ : S_WRITE;
364 + do {
365 + xfersize = MIN(lsp->ls_vp_comp_size - offset,
366 + MIN(MAXBSIZE - mapoffset, bp->b_resid));
367 + len = roundup(mapoffset + xfersize, PAGESIZE);
368 + mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
369 + alignedoffset, MAXBSIZE, 1, srw);
370 + /*
371 + * Now fault in the pages. This lets us check
372 + * for errors before we reference mapaddr and
373 + * try to resolve the fault in bcopy (which would
374 + * panic instead). And this can easily happen,
375 + * particularly if you've lofi'd a file over NFS
376 + * and someone deletes the file on the server.
377 + */
378 + error = segmap_fault(kas.a_hat, segkmap, mapaddr,
379 + len, F_SOFTLOCK, srw);
380 + if (error) {
381 + (void) segmap_release(segkmap, mapaddr, 0);
382 + if (FC_CODE(error) == FC_OBJERR)
383 + error = FC_ERRNO(error);
384 + else
385 + error = EIO;
386 + break;
387 + }
388 + smflags = 0;
389 + if (isread) {
390 + smflags |= SM_FREE;
391 + /*
392 + * If we're reading an entire page starting
393 + * at a page boundary, there's a good chance
394 + * we won't need it again. Put it on the
395 + * head of the freelist.
396 + */
397 + if (mapoffset == 0 && xfersize == PAGESIZE)
398 + smflags |= SM_DONTNEED;
399 + bcopy(mapaddr + mapoffset, bufaddr, xfersize);
400 + } else {
401 + smflags |= SM_WRITE;
402 + bcopy(bufaddr, mapaddr + mapoffset, xfersize);
403 + }
404 + bp->b_resid -= xfersize;
405 + bufaddr += xfersize;
406 + offset += xfersize;
407 + (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
408 + len, F_SOFTUNLOCK, srw);
409 + error = segmap_release(segkmap, mapaddr, smflags);
410 + /* only the first map may start partial */
411 + mapoffset = 0;
412 + alignedoffset += MAXBSIZE;
413 + } while ((error == 0) && (bp->b_resid > 0) &&
414 + (offset < lsp->ls_vp_comp_size));
415 +
416 + return (error);
417 +}
418 +
419 +/*ARGSUSED*/
420 +static int gzip_decompress(void *src, size_t srclen, void *dst,
421 + size_t *dstlen, int level)
422 +{
423 + ASSERT(*dstlen >= srclen);
424 +
425 + if (z_uncompress(dst, dstlen, src, srclen) != Z_OK)
426 + return (-1);
427 + return (0);
428 +}
429 +
327 430 /*
328 431 * This is basically what strategy used to be before we found we
329 432 * needed task queues.
330 433 */
331 434 static void
332 435 lofi_strategy_task(void *arg)
333 436 {
334 437 struct buf *bp = (struct buf *)arg;
335 438 int error;
336 439 struct lofi_state *lsp;
337 - offset_t offset, alignedoffset;
338 - offset_t mapoffset;
339 - caddr_t bufaddr;
340 - caddr_t mapaddr;
341 - size_t xfersize;
342 - size_t len;
343 - int isread;
344 - int smflags;
345 - enum seg_rw srw;
440 + uint64_t sblkno, eblkno, cmpbytes;
441 + offset_t offset, sblkoff, eblkoff;
442 + offset_t salign, ealign;
443 + offset_t sdiff;
444 + uint32_t comp_data_sz;
445 + caddr_t bufaddr;
446 + unsigned char *compressed_seg = NULL, *cmpbuf;
447 + unsigned char *uncompressed_seg = NULL;
448 + lofi_compress_info_t *li;
449 + size_t oblkcount, xfersize;
450 + unsigned long seglen;
346 451
347 452 lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
348 453 if (lsp->ls_kstat) {
349 454 mutex_enter(lsp->ls_kstat->ks_lock);
350 455 kstat_waitq_to_runq(KSTAT_IO_PTR(lsp->ls_kstat));
351 456 mutex_exit(lsp->ls_kstat->ks_lock);
352 457 }
353 458 bp_mapin(bp);
354 459 bufaddr = bp->b_un.b_addr;
355 460 offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */
356 461
357 462 /*
|
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
358 463 * We used to always use vn_rdwr here, but we cannot do that because
359 464 * we might decide to read or write from the the underlying
360 465 * file during this call, which would be a deadlock because
361 466 * we have the rw_lock. So instead we page, unless it's not
362 467 * mapable or it's a character device.
363 468 */
364 469 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
365 470 error = EIO;
366 471 } else if (((lsp->ls_vp->v_flag & VNOMAP) == 0) &&
367 472 (lsp->ls_vp->v_type != VCHR)) {
473 + uint64_t i;
474 +
368 475 /*
369 - * segmap always gives us an 8K (MAXBSIZE) chunk, aligned on
370 - * an 8K boundary, but the buf transfer address may not be
371 - * aligned on more than a 512-byte boundary (we don't
372 - * enforce that, though we could). This matters since the
373 - * initial part of the transfer may not start at offset 0
374 - * within the segmap'd chunk. So we have to compensate for
375 - * that with 'mapoffset'. Subsequent chunks always start
376 - * off at the beginning, and the last is capped by b_resid.
476 + * Handle uncompressed files with a regular read
377 477 */
378 - mapoffset = offset & MAXBOFFSET;
379 - alignedoffset = offset - mapoffset; /* now map-aligned */
380 - bp->b_resid = bp->b_bcount;
381 - isread = bp->b_flags & B_READ;
382 - srw = isread ? S_READ : S_WRITE;
383 - do {
384 - xfersize = MIN(lsp->ls_vp_size - offset,
385 - MIN(MAXBSIZE - mapoffset, bp->b_resid));
386 - len = roundup(mapoffset + xfersize, PAGESIZE);
387 - mapaddr = segmap_getmapflt(segkmap, lsp->ls_vp,
388 - alignedoffset, MAXBSIZE, 1, srw);
478 + if (lsp->ls_uncomp_seg_sz == 0) {
479 + error = lofi_mapped_rdwr(bufaddr, offset, bp, lsp);
480 + goto done;
481 + }
482 +
483 + /*
484 + * From here on we're dealing primarily with compressed files
485 + */
486 +
487 + /*
488 + * Compressed files can only be read from and
489 + * not written to
490 + */
491 + if (!(bp->b_flags & B_READ)) {
492 + bp->b_resid = bp->b_bcount;
493 + error = EROFS;
494 + goto done;
495 + }
496 +
497 + ASSERT(lsp->ls_comp_algorithm_index >= 0);
498 + li = &lofi_compress_table[lsp->ls_comp_algorithm_index];
499 + /*
500 + * Compute starting and ending compressed segment numbers
501 + * We use only bitwise operations avoiding division and
502 + * modulus because we enforce the compression segment size
503 + * to a power of 2
504 + */
505 + sblkno = offset >> lsp->ls_comp_seg_shift;
506 + sblkoff = offset & (lsp->ls_uncomp_seg_sz - 1);
507 + eblkno = (offset + bp->b_bcount) >> lsp->ls_comp_seg_shift;
508 + eblkoff = (offset + bp->b_bcount) & (lsp->ls_uncomp_seg_sz - 1);
509 +
510 + /*
511 + * Align start offset to block boundary for segmap
512 + */
513 + salign = lsp->ls_comp_seg_index[sblkno];
514 + sdiff = salign & (DEV_BSIZE - 1);
515 + salign -= sdiff;
516 + if (eblkno >= (lsp->ls_comp_index_sz - 1)) {
389 517 /*
390 - * Now fault in the pages. This lets us check
391 - * for errors before we reference mapaddr and
392 - * try to resolve the fault in bcopy (which would
393 - * panic instead). And this can easily happen,
394 - * particularly if you've lofi'd a file over NFS
395 - * and someone deletes the file on the server.
518 + * We're dealing with the last segment of
519 + * the compressed file -- the size of this
520 + * segment *may not* be the same as the
521 + * segment size for the file
396 522 */
397 - error = segmap_fault(kas.a_hat, segkmap, mapaddr,
398 - len, F_SOFTLOCK, srw);
399 - if (error) {
400 - (void) segmap_release(segkmap, mapaddr, 0);
401 - if (FC_CODE(error) == FC_OBJERR)
402 - error = FC_ERRNO(error);
403 - else
404 - error = EIO;
405 - break;
523 + eblkoff = (offset + bp->b_bcount) &
524 + (lsp->ls_uncomp_last_seg_sz - 1);
525 + ealign = lsp->ls_vp_comp_size;
526 + } else {
527 + ealign = lsp->ls_comp_seg_index[eblkno + 1];
528 + }
529 +
530 + /*
531 + * Preserve original request paramaters
532 + */
533 + oblkcount = bp->b_bcount;
534 +
535 + /*
536 + * Assign the calculated parameters
537 + */
538 + comp_data_sz = ealign - salign;
539 + bp->b_bcount = comp_data_sz;
540 +
541 + /*
542 + * Allocate fixed size memory blocks to hold one
543 + * compressed and uncompressed segment since we
544 + * uncompress segments one at a time
545 + */
546 + compressed_seg = kmem_alloc(bp->b_bcount, KM_SLEEP);
547 + uncompressed_seg = kmem_alloc(lsp->ls_uncomp_seg_sz, KM_SLEEP);
548 + /*
549 + * Map in the calculated number of blocks
550 + */
551 + error = lofi_mapped_rdwr((caddr_t)compressed_seg, salign,
552 + bp, lsp);
553 +
554 + bp->b_bcount = oblkcount;
555 + bp->b_resid = oblkcount;
556 + if (error != 0)
557 + goto done;
558 +
559 + /*
560 + * We have the compressed blocks, now uncompress them
561 + */
562 + cmpbuf = compressed_seg + sdiff;
563 + for (i = sblkno; i < (eblkno + 1) && i < lsp->ls_comp_index_sz;
564 + i++) {
565 + /*
566 + * Each of the segment index entries contains
567 + * the starting block number for that segment.
568 + * The number of compressed bytes in a segment
569 + * is thus the difference between the starting
570 + * block number of this segment and the starting
571 + * block number of the next segment.
572 + */
573 + if ((i == eblkno) &&
574 + (i == lsp->ls_comp_index_sz - 1)) {
575 + cmpbytes = lsp->ls_vp_comp_size -
576 + lsp->ls_comp_seg_index[i];
577 + } else {
578 + cmpbytes = lsp->ls_comp_seg_index[i + 1] -
579 + lsp->ls_comp_seg_index[i];
406 580 }
407 - smflags = 0;
408 - if (isread) {
409 - bcopy(mapaddr + mapoffset, bufaddr, xfersize);
581 +
582 + /*
583 + * The first byte in a compressed segment is a flag
584 + * that indicates whether is this segment is
585 + * compressed at all
586 + */
587 + if (*cmpbuf == UNCOMPRESSED) {
588 + bcopy((cmpbuf + SEGHDR), uncompressed_seg,
589 + (cmpbytes - SEGHDR));
410 590 } else {
411 - smflags |= SM_WRITE;
412 - bcopy(bufaddr, mapaddr + mapoffset, xfersize);
591 + seglen = lsp->ls_uncomp_seg_sz;
592 +
593 + if (li->l_decompress((cmpbuf + SEGHDR),
594 + (cmpbytes - SEGHDR), uncompressed_seg,
595 + &seglen, li->l_level) != 0) {
596 + error = EIO;
597 + goto done;
598 + }
413 599 }
414 - bp->b_resid -= xfersize;
600 +
601 + /*
602 + * Determine how much uncompressed data we
603 + * have to copy and copy it
604 + */
605 + xfersize = lsp->ls_uncomp_seg_sz - sblkoff;
606 + if (i == eblkno) {
607 + if (i == (lsp->ls_comp_index_sz - 1))
608 + xfersize -= (lsp->ls_uncomp_last_seg_sz
609 + - eblkoff);
610 + else
611 + xfersize -=
612 + (lsp->ls_uncomp_seg_sz - eblkoff);
613 + }
614 +
615 + bcopy((uncompressed_seg + sblkoff), bufaddr, xfersize);
616 +
617 + cmpbuf += cmpbytes;
415 618 bufaddr += xfersize;
416 - offset += xfersize;
417 - (void) segmap_fault(kas.a_hat, segkmap, mapaddr,
418 - len, F_SOFTUNLOCK, srw);
419 - error = segmap_release(segkmap, mapaddr, smflags);
420 - /* only the first map may start partial */
421 - mapoffset = 0;
422 - alignedoffset += MAXBSIZE;
423 - } while ((error == 0) && (bp->b_resid > 0) &&
424 - (offset < lsp->ls_vp_size));
619 + bp->b_resid -= xfersize;
620 + sblkoff = 0;
621 +
622 + if (bp->b_resid == 0)
623 + break;
624 + }
425 625 } else {
426 626 ssize_t resid;
427 627 enum uio_rw rw;
428 628
429 629 if (bp->b_flags & B_READ)
430 630 rw = UIO_READ;
431 631 else
432 632 rw = UIO_WRITE;
433 633 error = vn_rdwr(rw, lsp->ls_vp, bufaddr, bp->b_bcount,
434 634 offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
435 635 bp->b_resid = resid;
436 636 }
437 637
638 +done:
639 + if (compressed_seg != NULL)
640 + kmem_free(compressed_seg, comp_data_sz);
641 + if (uncompressed_seg != NULL)
642 + kmem_free(uncompressed_seg, lsp->ls_uncomp_seg_sz);
643 +
438 644 if (lsp->ls_kstat) {
439 645 size_t n_done = bp->b_bcount - bp->b_resid;
440 646 kstat_io_t *kioptr;
441 647
442 648 mutex_enter(lsp->ls_kstat->ks_lock);
443 649 kioptr = KSTAT_IO_PTR(lsp->ls_kstat);
444 650 if (bp->b_flags & B_READ) {
445 651 kioptr->nread += n_done;
446 652 kioptr->reads++;
447 653 } else {
448 654 kioptr->nwritten += n_done;
449 655 kioptr->writes++;
450 656 }
451 657 kstat_runq_exit(kioptr);
452 658 mutex_exit(lsp->ls_kstat->ks_lock);
453 659 }
454 660
455 661 mutex_enter(&lsp->ls_vp_lock);
456 662 if (--lsp->ls_vp_iocount == 0)
457 663 cv_broadcast(&lsp->ls_vp_cv);
458 664 mutex_exit(&lsp->ls_vp_lock);
459 665
460 666 bioerror(bp, error);
461 667 biodone(bp);
462 668 }
463 669
464 670 static int
465 671 lofi_strategy(struct buf *bp)
466 672 {
467 673 struct lofi_state *lsp;
468 674 offset_t offset;
469 675
470 676 /*
471 677 * We cannot just do I/O here, because the current thread
472 678 * _might_ end up back in here because the underlying filesystem
473 679 * wants a buffer, which eventually gets into bio_recycle and
474 680 * might call into lofi to write out a delayed-write buffer.
475 681 * This is bad if the filesystem above lofi is the same as below.
476 682 *
477 683 * We could come up with a complex strategy using threads to
478 684 * do the I/O asynchronously, or we could use task queues. task
479 685 * queues were incredibly easy so they win.
480 686 */
481 687 lsp = ddi_get_soft_state(lofi_statep, getminor(bp->b_edev));
482 688 mutex_enter(&lsp->ls_vp_lock);
483 689 if (lsp->ls_vp == NULL || lsp->ls_vp_closereq) {
484 690 bioerror(bp, EIO);
485 691 biodone(bp);
486 692 mutex_exit(&lsp->ls_vp_lock);
487 693 return (0);
488 694 }
489 695
490 696 offset = bp->b_lblkno * DEV_BSIZE; /* offset within file */
491 697 if (offset == lsp->ls_vp_size) {
492 698 /* EOF */
493 699 if ((bp->b_flags & B_READ) != 0) {
494 700 bp->b_resid = bp->b_bcount;
495 701 bioerror(bp, 0);
496 702 } else {
497 703 /* writes should fail */
498 704 bioerror(bp, ENXIO);
499 705 }
500 706 biodone(bp);
501 707 mutex_exit(&lsp->ls_vp_lock);
502 708 return (0);
503 709 }
504 710 if (offset > lsp->ls_vp_size) {
505 711 bioerror(bp, ENXIO);
506 712 biodone(bp);
507 713 mutex_exit(&lsp->ls_vp_lock);
508 714 return (0);
509 715 }
510 716 lsp->ls_vp_iocount++;
511 717 mutex_exit(&lsp->ls_vp_lock);
512 718
513 719 if (lsp->ls_kstat) {
514 720 mutex_enter(lsp->ls_kstat->ks_lock);
515 721 kstat_waitq_enter(KSTAT_IO_PTR(lsp->ls_kstat));
516 722 mutex_exit(lsp->ls_kstat->ks_lock);
517 723 }
518 724 (void) taskq_dispatch(lsp->ls_taskq, lofi_strategy_task, bp, KM_SLEEP);
519 725 return (0);
520 726 }
521 727
522 728 /*ARGSUSED2*/
523 729 static int
524 730 lofi_read(dev_t dev, struct uio *uio, struct cred *credp)
525 731 {
526 732 if (getminor(dev) == 0)
527 733 return (EINVAL);
528 734 return (physio(lofi_strategy, NULL, dev, B_READ, minphys, uio));
529 735 }
530 736
531 737 /*ARGSUSED2*/
532 738 static int
533 739 lofi_write(dev_t dev, struct uio *uio, struct cred *credp)
534 740 {
535 741 if (getminor(dev) == 0)
536 742 return (EINVAL);
537 743 return (physio(lofi_strategy, NULL, dev, B_WRITE, minphys, uio));
538 744 }
539 745
540 746 /*ARGSUSED2*/
541 747 static int
542 748 lofi_aread(dev_t dev, struct aio_req *aio, struct cred *credp)
543 749 {
544 750 if (getminor(dev) == 0)
545 751 return (EINVAL);
546 752 return (aphysio(lofi_strategy, anocancel, dev, B_READ, minphys, aio));
547 753 }
548 754
549 755 /*ARGSUSED2*/
550 756 static int
551 757 lofi_awrite(dev_t dev, struct aio_req *aio, struct cred *credp)
552 758 {
553 759 if (getminor(dev) == 0)
554 760 return (EINVAL);
555 761 return (aphysio(lofi_strategy, anocancel, dev, B_WRITE, minphys, aio));
556 762 }
557 763
558 764 /*ARGSUSED*/
559 765 static int
560 766 lofi_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
561 767 {
562 768 switch (infocmd) {
563 769 case DDI_INFO_DEVT2DEVINFO:
564 770 *result = lofi_dip;
565 771 return (DDI_SUCCESS);
566 772 case DDI_INFO_DEVT2INSTANCE:
567 773 *result = 0;
568 774 return (DDI_SUCCESS);
569 775 }
570 776 return (DDI_FAILURE);
571 777 }
572 778
573 779 static int
574 780 lofi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
575 781 {
576 782 int error;
577 783
578 784 if (cmd != DDI_ATTACH)
579 785 return (DDI_FAILURE);
580 786 error = ddi_soft_state_zalloc(lofi_statep, 0);
581 787 if (error == DDI_FAILURE) {
582 788 return (DDI_FAILURE);
583 789 }
584 790 error = ddi_create_minor_node(dip, LOFI_CTL_NODE, S_IFCHR, 0,
585 791 DDI_PSEUDO, NULL);
586 792 if (error == DDI_FAILURE) {
587 793 ddi_soft_state_free(lofi_statep, 0);
588 794 return (DDI_FAILURE);
589 795 }
590 796 /* driver handles kernel-issued IOCTLs */
591 797 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
592 798 DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
593 799 ddi_remove_minor_node(dip, NULL);
594 800 ddi_soft_state_free(lofi_statep, 0);
595 801 return (DDI_FAILURE);
596 802 }
597 803 lofi_dip = dip;
598 804 ddi_report_dev(dip);
599 805 return (DDI_SUCCESS);
600 806 }
601 807
602 808 static int
603 809 lofi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
604 810 {
605 811 if (cmd != DDI_DETACH)
606 812 return (DDI_FAILURE);
607 813 if (lofi_busy())
608 814 return (DDI_FAILURE);
609 815 lofi_dip = NULL;
610 816 ddi_remove_minor_node(dip, NULL);
611 817 ddi_prop_remove_all(dip);
612 818 ddi_soft_state_free(lofi_statep, 0);
613 819 return (DDI_SUCCESS);
614 820 }
615 821
616 822 /*
617 823 * These two just simplify the rest of the ioctls that need to copyin/out
618 824 * the lofi_ioctl structure.
619 825 */
620 826 struct lofi_ioctl *
621 827 copy_in_lofi_ioctl(const struct lofi_ioctl *ulip, int flag)
622 828 {
623 829 struct lofi_ioctl *klip;
|
↓ open down ↓ |
176 lines elided |
↑ open up ↑ |
624 830 int error;
625 831
626 832 klip = kmem_alloc(sizeof (struct lofi_ioctl), KM_SLEEP);
627 833 error = ddi_copyin(ulip, klip, sizeof (struct lofi_ioctl), flag);
628 834 if (error) {
629 835 kmem_free(klip, sizeof (struct lofi_ioctl));
630 836 return (NULL);
631 837 }
632 838
633 839 /* make sure filename is always null-terminated */
634 - klip->li_filename[MAXPATHLEN] = '\0';
840 + klip->li_filename[MAXPATHLEN - 1] = '\0';
635 841
636 842 /* validate minor number */
637 843 if (klip->li_minor > lofi_max_files) {
638 844 kmem_free(klip, sizeof (struct lofi_ioctl));
639 845 return (NULL);
640 846 }
641 847 return (klip);
642 848 }
643 849
644 850 int
645 851 copy_out_lofi_ioctl(const struct lofi_ioctl *klip, struct lofi_ioctl *ulip,
646 852 int flag)
647 853 {
648 854 int error;
649 855
650 856 error = ddi_copyout(klip, ulip, sizeof (struct lofi_ioctl), flag);
651 857 if (error)
652 858 return (EFAULT);
653 859 return (0);
654 860 }
655 861
656 862 void
657 863 free_lofi_ioctl(struct lofi_ioctl *klip)
658 864 {
659 865 kmem_free(klip, sizeof (struct lofi_ioctl));
660 866 }
661 867
662 868 /*
663 869 * Return the minor number 'filename' is mapped to, if it is.
664 870 */
665 871 static int
666 872 file_to_minor(char *filename)
667 873 {
668 874 minor_t minor;
669 875 struct lofi_state *lsp;
670 876
671 877 ASSERT(mutex_owned(&lofi_lock));
672 878 for (minor = 1; minor <= lofi_max_files; minor++) {
673 879 lsp = ddi_get_soft_state(lofi_statep, minor);
674 880 if (lsp == NULL)
675 881 continue;
676 882 if (strcmp(lsp->ls_filename, filename) == 0)
677 883 return (minor);
678 884 }
679 885 return (0);
680 886 }
681 887
682 888 /*
683 889 * lofiadm does some validation, but since Joe Random (or crashme) could
684 890 * do our ioctls, we need to do some validation too.
685 891 */
686 892 static int
687 893 valid_filename(const char *filename)
688 894 {
689 895 static char *blkprefix = "/dev/" LOFI_BLOCK_NAME "/";
690 896 static char *charprefix = "/dev/" LOFI_CHAR_NAME "/";
691 897
692 898 /* must be absolute path */
693 899 if (filename[0] != '/')
694 900 return (0);
695 901 /* must not be lofi */
696 902 if (strncmp(filename, blkprefix, strlen(blkprefix)) == 0)
697 903 return (0);
698 904 if (strncmp(filename, charprefix, strlen(charprefix)) == 0)
699 905 return (0);
700 906 return (1);
701 907 }
702 908
703 909 /*
704 910 * Fakes up a disk geometry, and one big partition, based on the size
705 911 * of the file. This is needed because we allow newfs'ing the device,
706 912 * and newfs will do several disk ioctls to figure out the geometry and
707 913 * partition information. It uses that information to determine the parameters
708 914 * to pass to mkfs. Geometry is pretty much irrelevant these days, but we
709 915 * have to support it.
710 916 */
711 917 static void
712 918 fake_disk_geometry(struct lofi_state *lsp)
713 919 {
714 920 /* dk_geom - see dkio(7I) */
715 921 /*
716 922 * dkg_ncyl _could_ be set to one here (one big cylinder with gobs
717 923 * of sectors), but that breaks programs like fdisk which want to
718 924 * partition a disk by cylinder. With one cylinder, you can't create
719 925 * an fdisk partition and put pcfs on it for testing (hard to pick
720 926 * a number between one and one).
721 927 *
722 928 * The cheezy floppy test is an attempt to not have too few cylinders
723 929 * for a small file, or so many on a big file that you waste space
724 930 * for backup superblocks or cylinder group structures.
725 931 */
726 932 if (lsp->ls_vp_size < (2 * 1024 * 1024)) /* floppy? */
727 933 lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (100 * 1024);
728 934 else
729 935 lsp->ls_dkg.dkg_ncyl = lsp->ls_vp_size / (300 * 1024);
730 936 /* in case file file is < 100k */
731 937 if (lsp->ls_dkg.dkg_ncyl == 0)
732 938 lsp->ls_dkg.dkg_ncyl = 1;
733 939 lsp->ls_dkg.dkg_acyl = 0;
734 940 lsp->ls_dkg.dkg_bcyl = 0;
735 941 lsp->ls_dkg.dkg_nhead = 1;
736 942 lsp->ls_dkg.dkg_obs1 = 0;
737 943 lsp->ls_dkg.dkg_intrlv = 0;
738 944 lsp->ls_dkg.dkg_obs2 = 0;
739 945 lsp->ls_dkg.dkg_obs3 = 0;
740 946 lsp->ls_dkg.dkg_apc = 0;
741 947 lsp->ls_dkg.dkg_rpm = 7200;
742 948 lsp->ls_dkg.dkg_pcyl = lsp->ls_dkg.dkg_ncyl + lsp->ls_dkg.dkg_acyl;
743 949 lsp->ls_dkg.dkg_nsect = lsp->ls_vp_size /
744 950 (DEV_BSIZE * lsp->ls_dkg.dkg_ncyl);
745 951 lsp->ls_dkg.dkg_write_reinstruct = 0;
|
↓ open down ↓ |
101 lines elided |
↑ open up ↑ |
746 952 lsp->ls_dkg.dkg_read_reinstruct = 0;
747 953
748 954 /* vtoc - see dkio(7I) */
749 955 bzero(&lsp->ls_vtoc, sizeof (struct vtoc));
750 956 lsp->ls_vtoc.v_sanity = VTOC_SANE;
751 957 lsp->ls_vtoc.v_version = V_VERSION;
752 958 bcopy(LOFI_DRIVER_NAME, lsp->ls_vtoc.v_volume, 7);
753 959 lsp->ls_vtoc.v_sectorsz = DEV_BSIZE;
754 960 lsp->ls_vtoc.v_nparts = 1;
755 961 lsp->ls_vtoc.v_part[0].p_tag = V_UNASSIGNED;
756 - lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
962 +
963 + /*
964 + * A compressed file is read-only, other files can
965 + * be read-write
966 + */
967 + if (lsp->ls_uncomp_seg_sz > 0) {
968 + lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT | V_RONLY;
969 + } else {
970 + lsp->ls_vtoc.v_part[0].p_flag = V_UNMNT;
971 + }
757 972 lsp->ls_vtoc.v_part[0].p_start = (daddr_t)0;
758 973 /*
759 974 * The partition size cannot just be the number of sectors, because
760 975 * that might not end on a cylinder boundary. And if that's the case,
761 976 * newfs/mkfs will print a scary warning. So just figure the size
762 977 * based on the number of cylinders and sectors/cylinder.
763 978 */
764 979 lsp->ls_vtoc.v_part[0].p_size = lsp->ls_dkg.dkg_pcyl *
765 980 lsp->ls_dkg.dkg_nsect * lsp->ls_dkg.dkg_nhead;
766 981
767 982 /* dk_cinfo - see dkio(7I) */
768 983 bzero(&lsp->ls_ci, sizeof (struct dk_cinfo));
769 984 (void) strcpy(lsp->ls_ci.dki_cname, LOFI_DRIVER_NAME);
770 985 lsp->ls_ci.dki_ctype = DKC_MD;
771 986 lsp->ls_ci.dki_flags = 0;
772 987 lsp->ls_ci.dki_cnum = 0;
773 988 lsp->ls_ci.dki_addr = 0;
774 989 lsp->ls_ci.dki_space = 0;
775 990 lsp->ls_ci.dki_prio = 0;
776 991 lsp->ls_ci.dki_vec = 0;
777 992 (void) strcpy(lsp->ls_ci.dki_dname, LOFI_DRIVER_NAME);
778 993 lsp->ls_ci.dki_unit = 0;
779 994 lsp->ls_ci.dki_slave = 0;
780 995 lsp->ls_ci.dki_partition = 0;
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
781 996 /*
782 997 * newfs uses this to set maxcontig. Must not be < 16, or it
783 998 * will be 0 when newfs multiplies it by DEV_BSIZE and divides
784 999 * it by the block size. Then tunefs doesn't work because
785 1000 * maxcontig is 0.
786 1001 */
787 1002 lsp->ls_ci.dki_maxtransfer = 16;
788 1003 }
789 1004
790 1005 /*
1006 + * map in a compressed file
1007 + *
1008 + * Read in the header and the index that follows.
1009 + *
1010 + * The header is as follows -
1011 + *
1012 + * Signature (name of the compression algorithm)
1013 + * Compression segment size (a multiple of 512)
1014 + * Number of index entries
1015 + * Size of the last block
1016 + * The array containing the index entries
1017 + *
1018 + * The header information is always stored in
1019 + * network byte order on disk.
1020 + */
1021 +static int
1022 +lofi_map_compressed_file(struct lofi_state *lsp, char *buf)
1023 +{
1024 + uint32_t index_sz, header_len, i;
1025 + ssize_t resid;
1026 + enum uio_rw rw;
1027 + char *tbuf = buf;
1028 + int error;
1029 +
1030 + /* The signature has already been read */
1031 + tbuf += lsp->ls_comp_algorithm_len;
1032 + bcopy(tbuf, &(lsp->ls_uncomp_seg_sz), sizeof (lsp->ls_uncomp_seg_sz));
1033 + lsp->ls_uncomp_seg_sz = ntohl(lsp->ls_uncomp_seg_sz);
1034 +
1035 + /*
1036 + * The compressed segment size must be a power of 2
1037 + */
1038 + if (lsp->ls_uncomp_seg_sz % 2)
1039 + return (EINVAL);
1040 +
1041 + for (i = 0; !((lsp->ls_uncomp_seg_sz >> i) & 1); i++)
1042 + ;
1043 +
1044 + lsp->ls_comp_seg_shift = i;
1045 +
1046 + tbuf += sizeof (lsp->ls_uncomp_seg_sz);
1047 + bcopy(tbuf, &(lsp->ls_comp_index_sz), sizeof (lsp->ls_comp_index_sz));
1048 + lsp->ls_comp_index_sz = ntohl(lsp->ls_comp_index_sz);
1049 +
1050 + tbuf += sizeof (lsp->ls_comp_index_sz);
1051 + bcopy(tbuf, &(lsp->ls_uncomp_last_seg_sz),
1052 + sizeof (lsp->ls_uncomp_last_seg_sz));
1053 + lsp->ls_uncomp_last_seg_sz = ntohl(lsp->ls_uncomp_last_seg_sz);
1054 +
1055 + /*
1056 + * Compute the total size of the uncompressed data
1057 + * for use in fake_disk_geometry and other calculations.
1058 + * Disk geometry has to be faked with respect to the
1059 + * actual uncompressed data size rather than the
1060 + * compressed file size.
1061 + */
1062 + /* XXX '2' shouldn't subtracted here - should be '1' */
1063 + lsp->ls_vp_size = (lsp->ls_comp_index_sz - 2) * lsp->ls_uncomp_seg_sz
1064 + + lsp->ls_uncomp_last_seg_sz;
1065 +
1066 + /*
1067 + * Index size is rounded up to a 512 byte boundary for ease
1068 + * of segmapping
1069 + */
1070 + index_sz = sizeof (lsp->ls_comp_seg_index) * lsp->ls_comp_index_sz;
1071 + header_len = lsp->ls_comp_algorithm_len +
1072 + sizeof (lsp->ls_uncomp_seg_sz) +
1073 + sizeof (lsp->ls_comp_index_sz) +
1074 + sizeof (lsp->ls_uncomp_last_seg_sz);
1075 + lsp->ls_comp_offbase = header_len + index_sz;
1076 +
1077 + index_sz += header_len;
1078 + index_sz = roundup(index_sz, DEV_BSIZE);
1079 +
1080 + lsp->ls_comp_index_data = kmem_alloc(index_sz, KM_SLEEP);
1081 + lsp->ls_comp_index_data_sz = index_sz;
1082 +
1083 + /*
1084 + * Read in the index -- this has a side-effect
1085 + * of reading in the header as well
1086 + */
1087 + rw = UIO_READ;
1088 + error = vn_rdwr(rw, lsp->ls_vp, lsp->ls_comp_index_data, index_sz,
1089 + 0, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
1090 +
1091 + if (error != 0)
1092 + return (error);
1093 +
1094 + /* Skip the header, this is where the index really begins */
1095 + lsp->ls_comp_seg_index =
1096 + /*LINTED*/
1097 + (uint64_t *)(lsp->ls_comp_index_data + header_len);
1098 +
1099 + /* Now map the index into memory */
1100 + for (i = 0; i < lsp->ls_comp_index_sz; i++)
1101 + lsp->ls_comp_seg_index[i] = lsp->ls_comp_offbase +
1102 + lsp->ls_comp_seg_index[i];
1103 +
1104 + return (error);
1105 +}
1106 +
1107 +/*
1108 + * Check to see if the passed in signature is a valid
1109 + * one. If it is valid, return the index into
1110 + * lofi_compress_table.
1111 + *
1112 + * Return -1 if it is invalid
1113 + */
1114 +static int lofi_compress_select(char *signature)
1115 +{
1116 + int i;
1117 +
1118 + for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
1119 + if (strcmp(lofi_compress_table[i].l_name, signature) == 0)
1120 + return (i);
1121 + }
1122 +
1123 + return (-1);
1124 +}
1125 +
1126 +/*
791 1127 * map a file to a minor number. Return the minor number.
792 1128 */
793 1129 static int
794 1130 lofi_map_file(dev_t dev, struct lofi_ioctl *ulip, int pickminor,
795 1131 int *rvalp, struct cred *credp, int ioctl_flag)
796 1132 {
797 1133 minor_t newminor;
798 1134 struct lofi_state *lsp;
799 1135 struct lofi_ioctl *klip;
800 1136 int error;
801 1137 struct vnode *vp;
802 1138 int64_t Nblocks_prop_val;
803 1139 int64_t Size_prop_val;
1140 + int compress_index;
804 1141 vattr_t vattr;
805 1142 int flag;
806 1143 enum vtype v_type;
807 1144 int zalloced = 0;
808 1145 dev_t newdev;
809 1146 char namebuf[50];
1147 + char buf[DEV_BSIZE];
1148 + char *tbuf;
1149 + ssize_t resid;
1150 + enum uio_rw rw;
810 1151
811 1152 klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
812 1153 if (klip == NULL)
813 1154 return (EFAULT);
814 1155
815 1156 mutex_enter(&lofi_lock);
816 1157
817 1158 if (!valid_filename(klip->li_filename)) {
818 1159 error = EINVAL;
819 1160 goto out;
820 1161 }
821 1162
822 1163 if (file_to_minor(klip->li_filename) != 0) {
823 1164 error = EBUSY;
824 1165 goto out;
825 1166 }
826 1167
827 1168 if (pickminor) {
828 1169 /* Find a free one */
829 1170 for (newminor = 1; newminor <= lofi_max_files; newminor++)
830 1171 if (ddi_get_soft_state(lofi_statep, newminor) == NULL)
831 1172 break;
832 1173 if (newminor >= lofi_max_files) {
833 1174 error = EAGAIN;
834 1175 goto out;
835 1176 }
836 1177 } else {
837 1178 newminor = klip->li_minor;
838 1179 if (ddi_get_soft_state(lofi_statep, newminor) != NULL) {
839 1180 error = EEXIST;
840 1181 goto out;
841 1182 }
842 1183 }
843 1184
844 1185 /* make sure it's valid */
845 1186 error = lookupname(klip->li_filename, UIO_SYSSPACE, FOLLOW,
846 1187 NULLVPP, &vp);
847 1188 if (error) {
848 1189 goto out;
849 1190 }
850 1191 v_type = vp->v_type;
851 1192 VN_RELE(vp);
852 1193 if (!V_ISLOFIABLE(v_type)) {
853 1194 error = EINVAL;
854 1195 goto out;
855 1196 }
856 1197 flag = FREAD | FWRITE | FOFFMAX | FEXCL;
857 1198 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0, &vp, 0, 0);
|
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
858 1199 if (error) {
859 1200 /* try read-only */
860 1201 flag &= ~FWRITE;
861 1202 error = vn_open(klip->li_filename, UIO_SYSSPACE, flag, 0,
862 1203 &vp, 0, 0);
863 1204 if (error) {
864 1205 goto out;
865 1206 }
866 1207 }
867 1208 vattr.va_mask = AT_SIZE;
868 - error = VOP_GETATTR(vp, &vattr, 0, credp, NULL);
1209 + error = VOP_GETATTR(vp, &vattr, 0, credp);
869 1210 if (error) {
870 1211 goto closeout;
871 1212 }
872 1213 /* the file needs to be a multiple of the block size */
873 1214 if ((vattr.va_size % DEV_BSIZE) != 0) {
874 1215 error = EINVAL;
875 1216 goto closeout;
876 1217 }
877 1218 newdev = makedevice(getmajor(dev), newminor);
878 1219 Size_prop_val = vattr.va_size;
879 1220 if ((ddi_prop_update_int64(newdev, lofi_dip,
880 1221 SIZE_PROP_NAME, Size_prop_val)) != DDI_PROP_SUCCESS) {
881 1222 error = EINVAL;
882 1223 goto closeout;
883 1224 }
884 1225 Nblocks_prop_val = vattr.va_size / DEV_BSIZE;
885 1226 if ((ddi_prop_update_int64(newdev, lofi_dip,
886 1227 NBLOCKS_PROP_NAME, Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
887 1228 error = EINVAL;
888 1229 goto propout;
889 1230 }
890 1231 error = ddi_soft_state_zalloc(lofi_statep, newminor);
891 1232 if (error == DDI_FAILURE) {
892 1233 error = ENOMEM;
893 1234 goto propout;
894 1235 }
895 1236 zalloced = 1;
896 1237 (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
897 1238 (void) ddi_create_minor_node(lofi_dip, namebuf, S_IFBLK, newminor,
898 1239 DDI_PSEUDO, NULL);
899 1240 if (error != DDI_SUCCESS) {
900 1241 error = ENXIO;
901 1242 goto propout;
902 1243 }
903 1244 (void) snprintf(namebuf, sizeof (namebuf), "%d,raw", newminor);
904 1245 error = ddi_create_minor_node(lofi_dip, namebuf, S_IFCHR, newminor,
905 1246 DDI_PSEUDO, NULL);
906 1247 if (error != DDI_SUCCESS) {
907 1248 /* remove block node */
908 1249 (void) snprintf(namebuf, sizeof (namebuf), "%d", newminor);
909 1250 ddi_remove_minor_node(lofi_dip, namebuf);
910 1251 error = ENXIO;
911 1252 goto propout;
912 1253 }
913 1254 lsp = ddi_get_soft_state(lofi_statep, newminor);
914 1255 lsp->ls_filename_sz = strlen(klip->li_filename) + 1;
915 1256 lsp->ls_filename = kmem_alloc(lsp->ls_filename_sz, KM_SLEEP);
916 1257 (void) snprintf(namebuf, sizeof (namebuf), "%s_taskq_%d",
917 1258 LOFI_DRIVER_NAME, newminor);
918 1259 lsp->ls_taskq = taskq_create(namebuf, lofi_taskq_nthreads,
919 1260 minclsyspri, 1, lofi_taskq_maxalloc, 0);
920 1261 lsp->ls_kstat = kstat_create(LOFI_DRIVER_NAME, newminor,
921 1262 NULL, "disk", KSTAT_TYPE_IO, 1, 0);
922 1263 if (lsp->ls_kstat) {
923 1264 mutex_init(&lsp->ls_kstat_lock, NULL, MUTEX_DRIVER, NULL);
924 1265 lsp->ls_kstat->ks_lock = &lsp->ls_kstat_lock;
925 1266 kstat_install(lsp->ls_kstat);
926 1267 }
927 1268 cv_init(&lsp->ls_vp_cv, NULL, CV_DRIVER, NULL);
928 1269 mutex_init(&lsp->ls_vp_lock, NULL, MUTEX_DRIVER, NULL);
929 1270
|
↓ open down ↓ |
51 lines elided |
↑ open up ↑ |
930 1271 /*
931 1272 * save open mode so file can be closed properly and vnode counts
932 1273 * updated correctly.
933 1274 */
934 1275 lsp->ls_openflag = flag;
935 1276
936 1277 /*
937 1278 * Try to handle stacked lofs vnodes.
938 1279 */
939 1280 if (vp->v_type == VREG) {
940 - if (VOP_REALVP(vp, &lsp->ls_vp, NULL) != 0) {
1281 + if (VOP_REALVP(vp, &lsp->ls_vp) != 0) {
941 1282 lsp->ls_vp = vp;
942 1283 } else {
943 1284 /*
944 1285 * Even though vp was obtained via vn_open(), we
945 1286 * can't call vn_close() on it, since lofs will
946 1287 * pass the VOP_CLOSE() on down to the realvp
947 1288 * (which we are about to use). Hence we merely
948 1289 * drop the reference to the lofs vnode and hold
949 1290 * the realvp so things behave as if we've
950 1291 * opened the realvp without any interaction
951 1292 * with lofs.
952 1293 */
953 1294 VN_HOLD(lsp->ls_vp);
954 1295 VN_RELE(vp);
|
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
955 1296 }
956 1297 } else {
957 1298 lsp->ls_vp = vp;
958 1299 }
959 1300 lsp->ls_vp_size = vattr.va_size;
960 1301 (void) strcpy(lsp->ls_filename, klip->li_filename);
961 1302 if (rvalp)
962 1303 *rvalp = (int)newminor;
963 1304 klip->li_minor = newminor;
964 1305
1306 + /*
1307 + * Read the file signature to check if it is compressed.
1308 + * 'rw' is set to read since only reads are allowed to
1309 + * a compressed file.
1310 + */
1311 + rw = UIO_READ;
1312 + error = vn_rdwr(rw, lsp->ls_vp, buf, DEV_BSIZE, 0, UIO_SYSSPACE,
1313 + 0, RLIM64_INFINITY, kcred, &resid);
1314 +
1315 + if (error != 0)
1316 + goto propout;
1317 +
1318 + tbuf = buf;
1319 + lsp->ls_uncomp_seg_sz = 0;
1320 + lsp->ls_vp_comp_size = lsp->ls_vp_size;
1321 + lsp->ls_comp_algorithm_len = 0;
1322 +
1323 + compress_index = lofi_compress_select(tbuf);
1324 + if (compress_index != -1) {
1325 + lsp->ls_comp_algorithm_index = compress_index;
1326 + lsp->ls_comp_algorithm_len =
1327 + strlen(lofi_compress_table[compress_index].l_name);
1328 + error = lofi_map_compressed_file(lsp, buf);
1329 + if (error != 0)
1330 + goto propout;
1331 +
1332 + /* update DDI properties */
1333 + Size_prop_val = lsp->ls_vp_size;
1334 + if ((ddi_prop_update_int64(newdev, lofi_dip, SIZE_PROP_NAME,
1335 + Size_prop_val)) != DDI_PROP_SUCCESS) {
1336 + error = EINVAL;
1337 + goto propout;
1338 + }
1339 +
1340 + Nblocks_prop_val = lsp->ls_vp_size / DEV_BSIZE;
1341 + if ((ddi_prop_update_int64(newdev, lofi_dip, NBLOCKS_PROP_NAME,
1342 + Nblocks_prop_val)) != DDI_PROP_SUCCESS) {
1343 + error = EINVAL;
1344 + goto propout;
1345 + }
1346 + }
1347 +
965 1348 fake_disk_geometry(lsp);
966 1349 mutex_exit(&lofi_lock);
967 1350 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
968 1351 free_lofi_ioctl(klip);
969 1352 return (0);
970 1353
971 1354 propout:
972 1355 (void) ddi_prop_remove(newdev, lofi_dip, SIZE_PROP_NAME);
973 1356 (void) ddi_prop_remove(newdev, lofi_dip, NBLOCKS_PROP_NAME);
974 1357 closeout:
975 - (void) VOP_CLOSE(vp, flag, 1, 0, credp, NULL);
1358 + (void) VOP_CLOSE(vp, flag, 1, 0, credp);
976 1359 VN_RELE(vp);
977 1360 out:
978 1361 if (zalloced)
979 1362 ddi_soft_state_free(lofi_statep, newminor);
980 1363 mutex_exit(&lofi_lock);
981 1364 free_lofi_ioctl(klip);
982 1365 return (error);
983 1366 }
984 1367
985 1368 /*
986 1369 * unmap a file.
987 1370 */
988 1371 static int
989 1372 lofi_unmap_file(dev_t dev, struct lofi_ioctl *ulip, int byfilename,
990 1373 struct cred *credp, int ioctl_flag)
991 1374 {
992 1375 struct lofi_state *lsp;
993 1376 struct lofi_ioctl *klip;
994 1377 minor_t minor;
995 1378
996 1379 klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
997 1380 if (klip == NULL)
998 1381 return (EFAULT);
999 1382
1000 1383 mutex_enter(&lofi_lock);
1001 1384 if (byfilename) {
1002 1385 minor = file_to_minor(klip->li_filename);
1003 1386 } else {
1004 1387 minor = klip->li_minor;
1005 1388 }
1006 1389 if (minor == 0) {
1007 1390 mutex_exit(&lofi_lock);
1008 1391 free_lofi_ioctl(klip);
1009 1392 return (ENXIO);
1010 1393 }
1011 1394 lsp = ddi_get_soft_state(lofi_statep, minor);
1012 1395 if (lsp == NULL || lsp->ls_vp == NULL) {
1013 1396 mutex_exit(&lofi_lock);
1014 1397 free_lofi_ioctl(klip);
1015 1398 return (ENXIO);
1016 1399 }
1017 1400
1018 1401 if (is_opened(lsp)) {
1019 1402 /*
1020 1403 * If the 'force' flag is set, then we forcibly close the
1021 1404 * underlying file. Subsequent operations will fail, and the
1022 1405 * DKIOCSTATE ioctl will return DKIO_DEV_GONE. When the device
1023 1406 * is last closed, the device will be cleaned up appropriately.
1024 1407 *
1025 1408 * This is complicated by the fact that we may have outstanding
1026 1409 * dispatched I/Os. Rather than having a single mutex to
1027 1410 * serialize all I/O, we keep a count of the number of
1028 1411 * outstanding I/O requests, as well as a flag to indicate that
|
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
1029 1412 * no new I/Os should be dispatched. We set the flag, wait for
1030 1413 * the number of outstanding I/Os to reach 0, and then close the
1031 1414 * underlying vnode.
1032 1415 */
1033 1416 if (klip->li_force) {
1034 1417 mutex_enter(&lsp->ls_vp_lock);
1035 1418 lsp->ls_vp_closereq = B_TRUE;
1036 1419 while (lsp->ls_vp_iocount > 0)
1037 1420 cv_wait(&lsp->ls_vp_cv, &lsp->ls_vp_lock);
1038 1421 (void) VOP_CLOSE(lsp->ls_vp, lsp->ls_openflag, 1, 0,
1039 - credp, NULL);
1422 + credp);
1040 1423 VN_RELE(lsp->ls_vp);
1041 1424 lsp->ls_vp = NULL;
1042 1425 cv_broadcast(&lsp->ls_vp_cv);
1043 1426 mutex_exit(&lsp->ls_vp_lock);
1044 1427 mutex_exit(&lofi_lock);
1045 1428 klip->li_minor = minor;
1046 1429 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1047 1430 free_lofi_ioctl(klip);
1048 1431 return (0);
1049 1432 }
1050 1433 mutex_exit(&lofi_lock);
1051 1434 free_lofi_ioctl(klip);
1052 1435 return (EBUSY);
1053 1436 }
1054 1437
1438 + if (lsp->ls_uncomp_seg_sz > 0) {
1439 + kmem_free(lsp->ls_comp_index_data, lsp->ls_comp_index_data_sz);
1440 + lsp->ls_uncomp_seg_sz = 0;
1441 + }
1442 +
1055 1443 lofi_free_handle(dev, minor, lsp, credp);
1056 1444
1057 1445 klip->li_minor = minor;
1058 1446 mutex_exit(&lofi_lock);
1059 1447 (void) copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1060 1448 free_lofi_ioctl(klip);
1061 1449 return (0);
1062 1450 }
1063 1451
1064 1452 /*
1065 1453 * get the filename given the minor number, or the minor number given
1066 1454 * the name.
1067 1455 */
1068 1456 /*ARGSUSED*/
1069 1457 static int
1070 1458 lofi_get_info(dev_t dev, struct lofi_ioctl *ulip, int which,
1071 1459 struct cred *credp, int ioctl_flag)
1072 1460 {
1073 1461 struct lofi_state *lsp;
1074 1462 struct lofi_ioctl *klip;
1075 1463 int error;
1076 1464 minor_t minor;
1077 1465
1078 1466 klip = copy_in_lofi_ioctl(ulip, ioctl_flag);
1079 1467 if (klip == NULL)
1080 1468 return (EFAULT);
1081 1469
1082 1470 switch (which) {
1083 1471 case LOFI_GET_FILENAME:
1084 1472 minor = klip->li_minor;
1085 1473 if (minor == 0) {
1086 1474 free_lofi_ioctl(klip);
1087 1475 return (EINVAL);
|
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
1088 1476 }
1089 1477
1090 1478 mutex_enter(&lofi_lock);
1091 1479 lsp = ddi_get_soft_state(lofi_statep, minor);
1092 1480 if (lsp == NULL) {
1093 1481 mutex_exit(&lofi_lock);
1094 1482 free_lofi_ioctl(klip);
1095 1483 return (ENXIO);
1096 1484 }
1097 1485 (void) strcpy(klip->li_filename, lsp->ls_filename);
1486 + if (lsp->ls_comp_algorithm_len == 0)
1487 + klip->li_algorithm[0] = '\0';
1488 + else
1489 + (void) strlcpy(klip->li_algorithm, lofi_compress_table[
1490 + lsp->ls_comp_algorithm_index].l_name,
1491 + lsp->ls_comp_algorithm_len + 1);
1098 1492 mutex_exit(&lofi_lock);
1099 1493 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1100 1494 free_lofi_ioctl(klip);
1101 1495 return (error);
1102 1496 case LOFI_GET_MINOR:
1103 1497 mutex_enter(&lofi_lock);
1104 1498 klip->li_minor = file_to_minor(klip->li_filename);
1105 1499 mutex_exit(&lofi_lock);
1106 1500 if (klip->li_minor == 0) {
1107 1501 free_lofi_ioctl(klip);
1108 1502 return (ENOENT);
1109 1503 }
1110 1504 error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1111 1505 free_lofi_ioctl(klip);
1112 1506 return (error);
1507 + case LOFI_CHECK_COMPRESSED:
1508 + mutex_enter(&lofi_lock);
1509 + klip->li_minor = file_to_minor(klip->li_filename);
1510 + mutex_exit(&lofi_lock);
1511 + if (klip->li_minor == 0) {
1512 + free_lofi_ioctl(klip);
1513 + return (ENOENT);
1514 + }
1515 + mutex_enter(&lofi_lock);
1516 + lsp = ddi_get_soft_state(lofi_statep, klip->li_minor);
1517 + if (lsp == NULL) {
1518 + mutex_exit(&lofi_lock);
1519 + free_lofi_ioctl(klip);
1520 + return (ENXIO);
1521 + }
1522 + ASSERT(strcmp(klip->li_filename, lsp->ls_filename) == 0);
1523 +
1524 + if (lsp->ls_comp_algorithm_len == 0)
1525 + klip->li_algorithm[0] = '\0';
1526 + else
1527 + (void) strlcpy(klip->li_algorithm, lofi_compress_table[
1528 + lsp->ls_comp_algorithm_index].l_name,
1529 + lsp->ls_comp_algorithm_len + 1);
1530 +
1531 + mutex_exit(&lofi_lock);
1532 + error = copy_out_lofi_ioctl(klip, ulip, ioctl_flag);
1533 + free_lofi_ioctl(klip);
1534 + return (error);
1113 1535 default:
1114 1536 free_lofi_ioctl(klip);
1115 1537 return (EINVAL);
1116 1538 }
1117 1539
1118 1540 }
1119 1541
1120 1542 static int
1121 1543 lofi_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp,
1122 1544 int *rvalp)
1123 1545 {
1124 1546 int error;
1125 1547 enum dkio_state dkstate;
1126 1548 struct lofi_state *lsp;
1127 1549 minor_t minor;
1128 1550
1129 1551 #ifdef lint
1130 1552 credp = credp;
1131 1553 #endif
1132 1554
1133 1555 minor = getminor(dev);
1134 1556 /* lofi ioctls only apply to the master device */
1135 1557 if (minor == 0) {
1136 1558 struct lofi_ioctl *lip = (struct lofi_ioctl *)arg;
1137 1559
1138 1560 /*
1139 1561 * the query command only need read-access - i.e., normal
1140 1562 * users are allowed to do those on the ctl device as
1141 1563 * long as they can open it read-only.
1142 1564 */
1143 1565 switch (cmd) {
1144 1566 case LOFI_MAP_FILE:
1145 1567 if ((flag & FWRITE) == 0)
1146 1568 return (EPERM);
1147 1569 return (lofi_map_file(dev, lip, 1, rvalp, credp, flag));
1148 1570 case LOFI_MAP_FILE_MINOR:
1149 1571 if ((flag & FWRITE) == 0)
1150 1572 return (EPERM);
1151 1573 return (lofi_map_file(dev, lip, 0, rvalp, credp, flag));
1152 1574 case LOFI_UNMAP_FILE:
1153 1575 if ((flag & FWRITE) == 0)
1154 1576 return (EPERM);
1155 1577 return (lofi_unmap_file(dev, lip, 1, credp, flag));
1156 1578 case LOFI_UNMAP_FILE_MINOR:
1157 1579 if ((flag & FWRITE) == 0)
1158 1580 return (EPERM);
1159 1581 return (lofi_unmap_file(dev, lip, 0, credp, flag));
1160 1582 case LOFI_GET_FILENAME:
1161 1583 return (lofi_get_info(dev, lip, LOFI_GET_FILENAME,
|
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
1162 1584 credp, flag));
1163 1585 case LOFI_GET_MINOR:
1164 1586 return (lofi_get_info(dev, lip, LOFI_GET_MINOR,
1165 1587 credp, flag));
1166 1588 case LOFI_GET_MAXMINOR:
1167 1589 error = ddi_copyout(&lofi_max_files, &lip->li_minor,
1168 1590 sizeof (lofi_max_files), flag);
1169 1591 if (error)
1170 1592 return (EFAULT);
1171 1593 return (0);
1594 + case LOFI_CHECK_COMPRESSED:
1595 + return (lofi_get_info(dev, lip, LOFI_CHECK_COMPRESSED,
1596 + credp, flag));
1172 1597 default:
1173 1598 break;
1174 1599 }
1175 1600 }
1176 1601
1177 1602 lsp = ddi_get_soft_state(lofi_statep, minor);
1178 1603 if (lsp == NULL)
1179 1604 return (ENXIO);
1180 1605
1181 1606 /*
1182 1607 * We explicitly allow DKIOCSTATE, but all other ioctls should fail with
1183 1608 * EIO as if the device was no longer present.
1184 1609 */
1185 1610 if (lsp->ls_vp == NULL && cmd != DKIOCSTATE)
1186 1611 return (EIO);
1187 1612
1188 1613 /* these are for faking out utilities like newfs */
1189 1614 switch (cmd) {
1190 1615 case DKIOCGVTOC:
1191 1616 switch (ddi_model_convert_from(flag & FMODELS)) {
1192 1617 case DDI_MODEL_ILP32: {
1193 1618 struct vtoc32 vtoc32;
1194 1619
1195 1620 vtoctovtoc32(lsp->ls_vtoc, vtoc32);
1196 1621 if (ddi_copyout(&vtoc32, (void *)arg,
1197 1622 sizeof (struct vtoc32), flag))
1198 1623 return (EFAULT);
1199 1624 break;
1200 1625 }
1201 1626
1202 1627 case DDI_MODEL_NONE:
1203 1628 if (ddi_copyout(&lsp->ls_vtoc, (void *)arg,
1204 1629 sizeof (struct vtoc), flag))
1205 1630 return (EFAULT);
1206 1631 break;
1207 1632 }
1208 1633 return (0);
1209 1634 case DKIOCINFO:
1210 1635 error = ddi_copyout(&lsp->ls_ci, (void *)arg,
1211 1636 sizeof (struct dk_cinfo), flag);
1212 1637 if (error)
1213 1638 return (EFAULT);
1214 1639 return (0);
1215 1640 case DKIOCG_VIRTGEOM:
1216 1641 case DKIOCG_PHYGEOM:
1217 1642 case DKIOCGGEOM:
1218 1643 error = ddi_copyout(&lsp->ls_dkg, (void *)arg,
1219 1644 sizeof (struct dk_geom), flag);
1220 1645 if (error)
1221 1646 return (EFAULT);
1222 1647 return (0);
1223 1648 case DKIOCSTATE:
1224 1649 /*
1225 1650 * Normally, lofi devices are always in the INSERTED state. If
1226 1651 * a device is forcefully unmapped, then the device transitions
1227 1652 * to the DKIO_DEV_GONE state.
1228 1653 */
1229 1654 if (ddi_copyin((void *)arg, &dkstate, sizeof (dkstate),
1230 1655 flag) != 0)
1231 1656 return (EFAULT);
1232 1657
1233 1658 mutex_enter(&lsp->ls_vp_lock);
1234 1659 while ((dkstate == DKIO_INSERTED && lsp->ls_vp != NULL) ||
1235 1660 (dkstate == DKIO_DEV_GONE && lsp->ls_vp == NULL)) {
1236 1661 /*
1237 1662 * By virtue of having the device open, we know that
1238 1663 * 'lsp' will remain valid when we return.
1239 1664 */
1240 1665 if (!cv_wait_sig(&lsp->ls_vp_cv,
1241 1666 &lsp->ls_vp_lock)) {
1242 1667 mutex_exit(&lsp->ls_vp_lock);
1243 1668 return (EINTR);
1244 1669 }
1245 1670 }
1246 1671
1247 1672 dkstate = (lsp->ls_vp != NULL ? DKIO_INSERTED : DKIO_DEV_GONE);
1248 1673 mutex_exit(&lsp->ls_vp_lock);
1249 1674
1250 1675 if (ddi_copyout(&dkstate, (void *)arg,
1251 1676 sizeof (dkstate), flag) != 0)
1252 1677 return (EFAULT);
1253 1678 return (0);
1254 1679 default:
1255 1680 return (ENOTTY);
1256 1681 }
1257 1682 }
1258 1683
1259 1684 static struct cb_ops lofi_cb_ops = {
1260 1685 lofi_open, /* open */
1261 1686 lofi_close, /* close */
1262 1687 lofi_strategy, /* strategy */
1263 1688 nodev, /* print */
1264 1689 nodev, /* dump */
1265 1690 lofi_read, /* read */
1266 1691 lofi_write, /* write */
1267 1692 lofi_ioctl, /* ioctl */
1268 1693 nodev, /* devmap */
1269 1694 nodev, /* mmap */
1270 1695 nodev, /* segmap */
1271 1696 nochpoll, /* poll */
1272 1697 ddi_prop_op, /* prop_op */
1273 1698 0, /* streamtab */
1274 1699 D_64BIT | D_NEW | D_MP, /* Driver compatibility flag */
1275 1700 CB_REV,
1276 1701 lofi_aread,
1277 1702 lofi_awrite
1278 1703 };
1279 1704
1280 1705 static struct dev_ops lofi_ops = {
1281 1706 DEVO_REV, /* devo_rev, */
1282 1707 0, /* refcnt */
1283 1708 lofi_info, /* info */
1284 1709 nulldev, /* identify */
1285 1710 nulldev, /* probe */
1286 1711 lofi_attach, /* attach */
1287 1712 lofi_detach, /* detach */
1288 1713 nodev, /* reset */
1289 1714 &lofi_cb_ops, /* driver operations */
1290 1715 NULL /* no bus operations */
1291 1716 };
1292 1717
1293 1718 static struct modldrv modldrv = {
1294 1719 &mod_driverops,
1295 1720 "loopback file driver (%I%)",
1296 1721 &lofi_ops,
1297 1722 };
1298 1723
1299 1724 static struct modlinkage modlinkage = {
1300 1725 MODREV_1,
1301 1726 &modldrv,
1302 1727 NULL
1303 1728 };
1304 1729
1305 1730 int
1306 1731 _init(void)
1307 1732 {
1308 1733 int error;
1309 1734
1310 1735 error = ddi_soft_state_init(&lofi_statep,
1311 1736 sizeof (struct lofi_state), 0);
1312 1737 if (error)
1313 1738 return (error);
1314 1739
1315 1740 mutex_init(&lofi_lock, NULL, MUTEX_DRIVER, NULL);
1316 1741 error = mod_install(&modlinkage);
1317 1742 if (error) {
1318 1743 mutex_destroy(&lofi_lock);
1319 1744 ddi_soft_state_fini(&lofi_statep);
1320 1745 }
1321 1746
1322 1747 return (error);
1323 1748 }
1324 1749
1325 1750 int
1326 1751 _fini(void)
1327 1752 {
1328 1753 int error;
1329 1754
1330 1755 if (lofi_busy())
1331 1756 return (EBUSY);
1332 1757
1333 1758 error = mod_remove(&modlinkage);
1334 1759 if (error)
1335 1760 return (error);
1336 1761
1337 1762 mutex_destroy(&lofi_lock);
1338 1763 ddi_soft_state_fini(&lofi_statep);
1339 1764
1340 1765 return (error);
1341 1766 }
1342 1767
1343 1768 int
1344 1769 _info(struct modinfo *modinfop)
1345 1770 {
1346 1771 return (mod_info(&modlinkage, modinfop));
1347 1772 }
|
↓ open down ↓ |
166 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX