wok-6.x view linux64-cloop/stuff/cloop.u @ rev 19985
Up zstd (1.2.0)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Jul 01 15:39:55 2017 +0200 (2017-07-01) |
parents | 8fe10eb4f215 |
children | 36e9a3dcd5de |
line source
1 --- cloop.h
2 +++ cloop.h
3 @@ -20,6 +20,80 @@
4 /* data_index (num_blocks 64bit pointers, network order)... */
5 /* compressed data (gzip block compressed format)... */
7 +struct cloop_tail
8 +{
9 + u_int32_t table_size;
10 + u_int32_t index_size;
11 + u_int32_t num_blocks;
12 +};
13 +
14 +struct block_info
15 +{
16 + loff_t offset; /* 64-bit offsets of compressed block */
17 + u_int32_t size; /* 32-bit compressed block size */
18 + u_int32_t optidx; /* 32-bit index number */
19 +};
20 +
21 +static inline char *build_index(struct block_info *offsets, unsigned long n)
22 +{
23 + u_int32_t *ofs32 = (u_int32_t *) offsets;
24 + loff_t *ofs64 = (loff_t *) offsets;
25 +
26 + if (ofs32[0] == 0) {
27 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
28 + while (n--) {
29 + offsets[n].offset = __be64_to_cpu(offsets[n].offset);
30 + offsets[n].size = ntohl(offsets[n].size);
31 + }
32 + return (char *) "128BE accelerated knoppix 1.0";
33 + }
34 + else { /* V2.0 */
35 + loff_t last = __be64_to_cpu(ofs64[n - 1]);
36 + while (n--) {
37 + offsets[n].size = last -
38 + (offsets[n].offset = __be64_to_cpu(ofs64[n]));
39 + last = offsets[n].offset;
40 + }
41 + return (char *) "64BE v2.0";
42 + }
43 + }
44 + else if (ofs32[1] == 0) { /* V1.0 */
45 + loff_t last = __le64_to_cpu(ofs64[n - 1]);
46 + while (n--) {
47 + offsets[n].size = last -
48 + (offsets[n].offset = __le64_to_cpu(ofs64[n]));
49 + last = offsets[n].offset;
50 + }
51 + return (char *) "64LE v1.0";
52 + }
53 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
54 + loff_t last = ntohl(ofs32[n - 1]);
55 + while (n--) {
56 + offsets[n].size = last -
57 + (offsets[n].offset = ntohl(ofs32[n]));
58 + last = offsets[n].offset;
59 + }
60 + return (char *) "32BE v0.68";
61 + }
62 + else { /* V3.0 */
63 + unsigned long i;
64 + loff_t j;
65 +
66 + for (i = n; i-- != 0; )
67 + offsets[i].size = ntohl(ofs32[i]);
68 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
69 + offsets[i].offset = j;
70 + if (offsets[i].size & 0x80000000) {
71 + unsigned long k = offsets[i].size & 0x7FFFFFFF;
72 + offsets[i].offset = offsets[k].offset;
73 + offsets[i].size = offsets[k].size;
74 + }
75 + else j += offsets[i].size;
76 + }
77 + return (char *) "32BE v3.0";
78 + }
79 +}
80 +
81 /* Cloop suspend IOCTL */
82 #define CLOOP_SUSPEND 0x4C07
84 --- cloop.c
85 +++ cloop.c
86 @@ -5,11 +5,18 @@
87 * A cloop file looks like this:
88 * [32-bit uncompressed block size: network order]
89 * [32-bit number of blocks (n_blocks): network order]
90 - * [64-bit file offsets of start of blocks: network order]
91 + * [for version < 3]
92 + * [32-bit, 64-bit or 128-bit file offsets of start of blocks]
93 * ...
94 * (n_blocks + 1).
95 * n_blocks consisting of:
96 * [compressed block]
97 + * ...
98 + * [for version >= 3]
99 + * [compressed list of 32-bit block sizes]
100 + * [32-bit compressed index size: network order]
101 + * [32-bit index size = 4: network order]
102 + * [32-bit number of blocks (n_blocks): network order]
103 *
104 * Every version greatly inspired by code seen in loop.c
105 * by Theodore Ts'o, 3/29/93.
106 @@ -115,7 +122,7 @@
107 struct cloop_head head;
109 /* An array of offsets of compressed blocks within the file */
110 - loff_t *offsets;
111 + struct block_info *offsets;
113 /* We buffer some uncompressed blocks for performance */
114 int buffered_blocknum[BUFFERED_BLOCKS];
115 @@ -256,11 +263,11 @@
116 return i;
117 }
119 - buf_length = be64_to_cpu(clo->offsets[blocknum+1]) - be64_to_cpu(clo->offsets[blocknum]);
120 + buf_length = clo->offsets[blocknum].size;
122 /* Load one compressed block from the file. */
123 cloop_read_from_file(clo, clo->backing_file, (char *)clo->compressed_buffer,
124 - be64_to_cpu(clo->offsets[blocknum]), buf_length);
125 + clo->offsets[blocknum].offset, buf_length);
127 buflen = ntohl(clo->head.block_size);
129 @@ -275,9 +282,9 @@
130 if (ret != 0)
131 {
132 printk(KERN_ERR "%s: zlib decompression error %i uncompressing block %u %u/%lu/%u/%u "
133 - "%Lu-%Lu\n", cloop_name, ret, blocknum,
134 + "%Lu:%u\n", cloop_name, ret, blocknum,
135 ntohl(clo->head.block_size), buflen, buf_length, buf_done,
136 - be64_to_cpu(clo->offsets[blocknum]), be64_to_cpu(clo->offsets[blocknum+1]));
137 + clo->offsets[blocknum].offset, clo->offsets[blocknum].size);
138 clo->buffered_blocknum[clo->current_bufnum] = -1;
139 return -1;
140 }
141 @@ -489,30 +496,73 @@
142 cloop_name, ntohl(clo->head.block_size));
143 error=-EBADF; goto error_release;
144 }
145 - if (clo->head.preamble[0x0B]!='V'||clo->head.preamble[0x0C]<'1')
146 - {
147 - printk(KERN_ERR "%s: Cannot read old 32-bit (version 0.68) images, "
148 - "please use an older version of %s for this file.\n",
149 - cloop_name, cloop_name);
150 - error=-EBADF; goto error_release;
151 - }
152 - if (clo->head.preamble[0x0C]<'2')
153 - {
154 - printk(KERN_ERR "%s: Cannot read old architecture-dependent "
155 - "(format <= 1.0) images, please use an older "
156 - "version of %s for this file.\n",
157 - cloop_name, cloop_name);
158 - error=-EBADF; goto error_release;
159 - }
160 - total_offsets=ntohl(clo->head.num_blocks)+1;
161 - if (!isblkdev && (sizeof(struct cloop_head)+sizeof(loff_t)*
162 + total_offsets=ntohl(clo->head.num_blocks);
163 + if (!isblkdev && (sizeof(struct cloop_head)+sizeof(struct block_info)*
164 total_offsets > inode->i_size))
165 {
166 printk(KERN_ERR "%s: file too small for %u blocks\n",
167 cloop_name, ntohl(clo->head.num_blocks));
168 error=-EBADF; goto error_release;
169 }
170 - clo->offsets = cloop_malloc(sizeof(loff_t) * total_offsets);
171 + if (total_offsets + 1 == 0) /* Version >= 3.0 */
172 + {
173 + struct cloop_tail tail;
174 + if(isblkdev)
175 + {
176 + /* No end of file: can't find index */
177 + printk(KERN_ERR "%s: no V3 support for block device\n",
178 + cloop_name);
179 + error=-EBADF; goto error_release;
180 + }
181 + bytes_read = cloop_read_from_file(clo, file, (void *) &tail,
182 + inode->i_size - sizeof(struct cloop_tail),
183 + sizeof(struct cloop_tail));
184 + if(bytes_read == sizeof(struct cloop_tail))
185 + {
186 + unsigned long len, zlen;
187 + void *zbuf;
188 + clo->head.num_blocks = tail.num_blocks;
189 + total_offsets = ntohl(clo->head.num_blocks);
190 + clo->offsets = cloop_malloc(sizeof(struct block_info) * total_offsets);
191 + if (!clo->offsets)
192 + {
193 + printk(KERN_ERR "%s: can't alloc index\n",
194 + cloop_name);
195 + error=-EBADF; goto error_release;
196 + }
197 + zbuf = &clo->offsets[total_offsets/2];
198 + zlen = ntohl(tail.table_size);
199 + len = ntohl(tail.index_size) * total_offsets;
200 + bytes_read = cloop_read_from_file(clo, file, zbuf,
201 + inode->i_size - zlen - sizeof(struct cloop_tail),
202 + zlen);
203 + if (bytes_read != zlen)
204 + {
205 + printk(KERN_ERR "%s: can't read index\n",
206 + cloop_name);
207 + error=-EBADF; goto error_release;
208 + }
209 + clo->zstream.workspace = cloop_malloc(zlib_inflate_workspacesize());
210 + if(!clo->zstream.workspace)
211 + {
212 + printk(KERN_ERR "%s: can't alloc index workspace\n",
213 + cloop_name);
214 + error=-EBADF; goto error_release;
215 + }
216 + zlib_inflateInit(&clo->zstream);
217 + uncompress(clo, (void *) clo->offsets, &len, zbuf, zlen);
218 + cloop_free(clo->zstream.workspace, zlib_inflate_workspacesize());
219 + clo->zstream.workspace = NULL;
220 + break;
221 + }
222 + else
223 + {
224 + printk(KERN_ERR "%s: can't find index\n",
225 + cloop_name);
226 + error=-EBADF; goto error_release;
227 + }
228 + }
229 + clo->offsets = cloop_malloc(sizeof(struct block_info) * total_offsets);
230 if (!clo->offsets)
231 {
232 printk(KERN_ERR "%s: out of kernel mem for offsets\n", cloop_name);
233 @@ -521,19 +571,22 @@
234 }
235 num_readable = MIN(total_offsets - offsets_read,
236 (clo->underlying_blksize - offset)
237 - / sizeof(loff_t));
238 - memcpy(&clo->offsets[offsets_read], bbuf+offset, num_readable * sizeof(loff_t));
239 + / sizeof(struct block_info));
240 + memcpy(&clo->offsets[offsets_read], bbuf+offset, num_readable * sizeof(struct block_info));
241 offsets_read += num_readable;
242 }
243 { /* Search for largest block rather than estimate. KK. */
244 int i;
245 - for(i=0;i<total_offsets-1;i++)
246 + char *version = build_index(clo->offsets, ntohl(clo->head.num_blocks));
247 + for(i=0,clo->largest_block=0;i<total_offsets;i++)
248 {
249 - loff_t d=be64_to_cpu(clo->offsets[i+1]) - be64_to_cpu(clo->offsets[i]);
250 - clo->largest_block=MAX(clo->largest_block,d);
251 + clo->largest_block=MAX(clo->largest_block,clo->offsets[i].size);
252 }
253 - printk(KERN_INFO "%s: %s: %u blocks, %u bytes/block, largest block is %lu bytes.\n",
254 - cloop_name, filename, ntohl(clo->head.num_blocks),
255 + i = ntohl(clo->head.block_size);
256 + i += i/1000 + 12 + 4; /* max gzip block size */
257 + if (clo->largest_block > i) clo->largest_block = i; /* broken index ? */
258 + printk(KERN_INFO "%s: %s: %s, %u blocks, %u bytes/block, largest block is %lu bytes.\n",
259 + cloop_name, filename, version, ntohl(clo->head.num_blocks),
260 ntohl(clo->head.block_size), clo->largest_block);
261 }
262 /* Combo kmalloc used too large chunks (>130000). */
263 @@ -565,16 +618,6 @@
264 error=-ENOMEM; goto error_release_free_all;
265 }
266 zlib_inflateInit(&clo->zstream);
267 - if(!isblkdev &&
268 - be64_to_cpu(clo->offsets[ntohl(clo->head.num_blocks)]) != inode->i_size)
269 - {
270 - printk(KERN_ERR "%s: final offset wrong (%Lu not %Lu)\n",
271 - cloop_name,
272 - be64_to_cpu(clo->offsets[ntohl(clo->head.num_blocks)]),
273 - inode->i_size);
274 - cloop_free(clo->zstream.workspace, zlib_inflate_workspacesize()); clo->zstream.workspace=NULL;
275 - goto error_release_free_all;
276 - }
277 {
278 int i;
279 for(i=0; i<BUFFERED_BLOCKS; i++) clo->buffered_blocknum[i] = -1;
280 @@ -653,7 +696,7 @@
281 }
282 }
283 error_release_free:
284 - cloop_free(clo->offsets, sizeof(loff_t) * total_offsets);
285 + cloop_free(clo->offsets, sizeof(struct block_info) * total_offsets);
286 clo->offsets=NULL;
287 error_release:
288 if(bbuf) cloop_free(bbuf, clo->underlying_blksize);