wok annotate linux-cloop/stuff/cloop.u @ rev 18533

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