wok-next view linux64-cloop/stuff/cloop.u @ rev 17701
Add libmateweather
author | Yuri Pourre <yuripourre@gmail.com> |
---|---|
date | Fri Feb 27 01:06:08 2015 -0300 (2015-02-27) |
parents | b30e264381c7 |
children | 8fe10eb4f215 |
line source
1 --- cloop.h
2 +++ cloop.h
3 @@ -20,6 +20,75 @@
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 + j += offsets[i].size;
71 + }
72 + return (char *) "32BE v3.0";
73 + }
74 +}
75 +
76 /* Cloop suspend IOCTL */
77 #define CLOOP_SUSPEND 0x4C07
79 --- cloop.c
80 +++ cloop.c
81 @@ -5,11 +5,18 @@
82 * A cloop file looks like this:
83 * [32-bit uncompressed block size: network order]
84 * [32-bit number of blocks (n_blocks): network order]
85 - * [64-bit file offsets of start of blocks: network order]
86 + * [for version < 3]
87 + * [32-bit, 64-bit or 128-bit file offsets of start of blocks]
88 * ...
89 * (n_blocks + 1).
90 * n_blocks consisting of:
91 * [compressed block]
92 + * ...
93 + * [for version >= 3]
94 + * [compressed list of 32-bit block sizes]
95 + * [32-bit compressed index size: network order]
96 + * [32-bit index size = 4: network order]
97 + * [32-bit number of blocks (n_blocks): network order]
98 *
99 * Every version greatly inspired by code seen in loop.c
100 * by Theodore Ts'o, 3/29/93.
101 @@ -115,7 +122,7 @@
102 struct cloop_head head;
104 /* An array of offsets of compressed blocks within the file */
105 - loff_t *offsets;
106 + struct block_info *offsets;
108 /* We buffer some uncompressed blocks for performance */
109 int buffered_blocknum[BUFFERED_BLOCKS];
110 @@ -256,11 +263,11 @@
111 return i;
112 }
114 - buf_length = be64_to_cpu(clo->offsets[blocknum+1]) - be64_to_cpu(clo->offsets[blocknum]);
115 + buf_length = clo->offsets[blocknum].size;
117 /* Load one compressed block from the file. */
118 cloop_read_from_file(clo, clo->backing_file, (char *)clo->compressed_buffer,
119 - be64_to_cpu(clo->offsets[blocknum]), buf_length);
120 + clo->offsets[blocknum].offset, buf_length);
122 buflen = ntohl(clo->head.block_size);
124 @@ -275,9 +282,9 @@
125 if (ret != 0)
126 {
127 printk(KERN_ERR "%s: zlib decompression error %i uncompressing block %u %u/%lu/%u/%u "
128 - "%Lu-%Lu\n", cloop_name, ret, blocknum,
129 + "%Lu:%u\n", cloop_name, ret, blocknum,
130 ntohl(clo->head.block_size), buflen, buf_length, buf_done,
131 - be64_to_cpu(clo->offsets[blocknum]), be64_to_cpu(clo->offsets[blocknum+1]));
132 + clo->offsets[blocknum].offset, clo->offsets[blocknum].size);
133 clo->buffered_blocknum[clo->current_bufnum] = -1;
134 return -1;
135 }
136 @@ -489,30 +496,73 @@
137 cloop_name, ntohl(clo->head.block_size));
138 error=-EBADF; goto error_release;
139 }
140 - if (clo->head.preamble[0x0B]!='V'||clo->head.preamble[0x0C]<'1')
141 - {
142 - printk(KERN_ERR "%s: Cannot read old 32-bit (version 0.68) images, "
143 - "please use an older version of %s for this file.\n",
144 - cloop_name, cloop_name);
145 - error=-EBADF; goto error_release;
146 - }
147 - if (clo->head.preamble[0x0C]<'2')
148 - {
149 - printk(KERN_ERR "%s: Cannot read old architecture-dependent "
150 - "(format <= 1.0) images, please use an older "
151 - "version of %s for this file.\n",
152 - cloop_name, cloop_name);
153 - error=-EBADF; goto error_release;
154 - }
155 - total_offsets=ntohl(clo->head.num_blocks)+1;
156 - if (!isblkdev && (sizeof(struct cloop_head)+sizeof(loff_t)*
157 + total_offsets=ntohl(clo->head.num_blocks);
158 + if (!isblkdev && (sizeof(struct cloop_head)+sizeof(struct block_info)*
159 total_offsets > inode->i_size))
160 {
161 printk(KERN_ERR "%s: file too small for %u blocks\n",
162 cloop_name, ntohl(clo->head.num_blocks));
163 error=-EBADF; goto error_release;
164 }
165 - clo->offsets = cloop_malloc(sizeof(loff_t) * total_offsets);
166 + if (total_offsets + 1 == 0) /* Version >= 3.0 */
167 + {
168 + struct cloop_tail tail;
169 + if(isblkdev)
170 + {
171 + /* No end of file: can't find index */
172 + printk(KERN_ERR "%s: no V3 support for block device\n",
173 + cloop_name);
174 + error=-EBADF; goto error_release;
175 + }
176 + bytes_read = cloop_read_from_file(clo, file, (void *) &tail,
177 + inode->i_size - sizeof(struct cloop_tail),
178 + sizeof(struct cloop_tail));
179 + if(bytes_read == sizeof(struct cloop_tail))
180 + {
181 + unsigned long len, zlen;
182 + void *zbuf;
183 + clo->head.num_blocks = tail.num_blocks;
184 + total_offsets = ntohl(clo->head.num_blocks);
185 + clo->offsets = cloop_malloc(sizeof(struct block_info) * total_offsets);
186 + if (!clo->offsets)
187 + {
188 + printk(KERN_ERR "%s: can't alloc index\n",
189 + cloop_name);
190 + error=-EBADF; goto error_release;
191 + }
192 + zbuf = &clo->offsets[total_offsets/2];
193 + zlen = ntohl(tail.table_size);
194 + len = ntohl(tail.index_size) * total_offsets;
195 + bytes_read = cloop_read_from_file(clo, file, zbuf,
196 + inode->i_size - zlen - sizeof(struct cloop_tail),
197 + zlen);
198 + if (bytes_read != zlen)
199 + {
200 + printk(KERN_ERR "%s: can't read index\n",
201 + cloop_name);
202 + error=-EBADF; goto error_release;
203 + }
204 + clo->zstream.workspace = cloop_malloc(zlib_inflate_workspacesize());
205 + if(!clo->zstream.workspace)
206 + {
207 + printk(KERN_ERR "%s: can't alloc index workspace\n",
208 + cloop_name);
209 + error=-EBADF; goto error_release;
210 + }
211 + zlib_inflateInit(&clo->zstream);
212 + uncompress(clo, (void *) clo->offsets, &len, zbuf, zlen);
213 + cloop_free(clo->zstream.workspace, zlib_inflate_workspacesize());
214 + clo->zstream.workspace = NULL;
215 + break;
216 + }
217 + else
218 + {
219 + printk(KERN_ERR "%s: can't find index\n",
220 + cloop_name);
221 + error=-EBADF; goto error_release;
222 + }
223 + }
224 + clo->offsets = cloop_malloc(sizeof(struct block_info) * total_offsets);
225 if (!clo->offsets)
226 {
227 printk(KERN_ERR "%s: out of kernel mem for offsets\n", cloop_name);
228 @@ -521,19 +571,22 @@
229 }
230 num_readable = MIN(total_offsets - offsets_read,
231 (clo->underlying_blksize - offset)
232 - / sizeof(loff_t));
233 - memcpy(&clo->offsets[offsets_read], bbuf+offset, num_readable * sizeof(loff_t));
234 + / sizeof(struct block_info));
235 + memcpy(&clo->offsets[offsets_read], bbuf+offset, num_readable * sizeof(struct block_info));
236 offsets_read += num_readable;
237 }
238 { /* Search for largest block rather than estimate. KK. */
239 int i;
240 - for(i=0;i<total_offsets-1;i++)
241 + char *version = build_index(clo->offsets, ntohl(clo->head.num_blocks));
242 + for(i=0,clo->largest_block=0;i<total_offsets;i++)
243 {
244 - loff_t d=be64_to_cpu(clo->offsets[i+1]) - be64_to_cpu(clo->offsets[i]);
245 - clo->largest_block=MAX(clo->largest_block,d);
246 + clo->largest_block=MAX(clo->largest_block,clo->offsets[i].size);
247 }
248 - printk(KERN_INFO "%s: %s: %u blocks, %u bytes/block, largest block is %lu bytes.\n",
249 - cloop_name, filename, ntohl(clo->head.num_blocks),
250 + i = ntohl(clo->head.block_size);
251 + i += i/1000 + 12 + 4; /* max gzip block size */
252 + if (clo->largest_block > i) clo->largest_block = i; /* broken index ? */
253 + printk(KERN_INFO "%s: %s: %s, %u blocks, %u bytes/block, largest block is %lu bytes.\n",
254 + cloop_name, filename, version, ntohl(clo->head.num_blocks),
255 ntohl(clo->head.block_size), clo->largest_block);
256 }
257 /* Combo kmalloc used too large chunks (>130000). */
258 @@ -565,16 +618,6 @@
259 error=-ENOMEM; goto error_release_free_all;
260 }
261 zlib_inflateInit(&clo->zstream);
262 - if(!isblkdev &&
263 - be64_to_cpu(clo->offsets[ntohl(clo->head.num_blocks)]) != inode->i_size)
264 - {
265 - printk(KERN_ERR "%s: final offset wrong (%Lu not %Lu)\n",
266 - cloop_name,
267 - be64_to_cpu(clo->offsets[ntohl(clo->head.num_blocks)]),
268 - inode->i_size);
269 - cloop_free(clo->zstream.workspace, zlib_inflate_workspacesize()); clo->zstream.workspace=NULL;
270 - goto error_release_free_all;
271 - }
272 {
273 int i;
274 for(i=0; i<BUFFERED_BLOCKS; i++) clo->buffered_blocknum[i] = -1;
275 @@ -653,7 +696,7 @@
276 }
277 }
278 error_release_free:
279 - cloop_free(clo->offsets, sizeof(loff_t) * total_offsets);
280 + cloop_free(clo->offsets, sizeof(struct block_info) * total_offsets);
281 clo->offsets=NULL;
282 error_release:
283 if(bbuf) cloop_free(bbuf, clo->underlying_blksize);