-
Notifications
You must be signed in to change notification settings - Fork 69
/
fs.c
513 lines (451 loc) · 13.9 KB
/
fs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright (c) 2012 Dave Vasilevsky <dave@vasilevsky.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fs.h"
#include "file.h"
#include "dir.h"
#include "nonstd.h"
#include "swap.h"
#include "xattr.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef SQFS_MULTITHREADED
# define DATA_CACHED_BLKS 48
# define FRAG_CACHED_BLKS 48
#else
# define DATA_CACHED_BLKS 1
# define FRAG_CACHED_BLKS 3
#endif
void sqfs_version_supported(int *min_major, int *min_minor, int *max_major,
int *max_minor) {
*min_major = *max_major = SQUASHFS_MAJOR;
*min_minor = 0;
*max_minor = SQUASHFS_MINOR;
}
void sqfs_version(sqfs *fs, int *major, int *minor) {
*major = fs->sb.s_major;
*minor = fs->sb.s_minor;
}
sqfs_compression_type sqfs_compression(sqfs *fs) {
return fs->sb.compression;
}
sqfs_err sqfs_init_with_subdir(sqfs *fs, sqfs_fd_t fd, size_t offset, const char *subdir) {
sqfs_err err = SQFS_OK;
memset(fs, 0, sizeof(*fs));
fs->fd = fd;
fs->offset = offset;
if (sqfs_pread(fd, &fs->sb, sizeof(fs->sb), fs->offset) != sizeof(fs->sb))
return SQFS_BADFORMAT;
sqfs_swapin_super_block(&fs->sb);
if (fs->sb.s_magic != SQUASHFS_MAGIC) {
if (fs->sb.s_magic != SQFS_MAGIC_SWAP)
return SQFS_BADFORMAT;
sqfs_swap16(&fs->sb.s_major);
sqfs_swap16(&fs->sb.s_minor);
}
if (fs->sb.s_major != SQUASHFS_MAJOR || fs->sb.s_minor > SQUASHFS_MINOR)
return SQFS_BADVERSION;
if (!(fs->decompressor = sqfs_decompressor_get(fs->sb.compression)))
return SQFS_BADCOMP;
err = sqfs_table_init(&fs->id_table, fd, fs->sb.id_table_start + fs->offset,
sizeof(uint32_t), fs->sb.no_ids);
err |= sqfs_table_init(&fs->frag_table, fd, fs->sb.fragment_table_start + fs->offset,
sizeof(struct squashfs_fragment_entry), fs->sb.fragments);
if (sqfs_export_ok(fs)) {
err |= sqfs_table_init(&fs->export_table, fd, fs->sb.lookup_table_start + fs->offset,
sizeof(uint64_t), fs->sb.inodes);
}
err |= sqfs_xattr_init(fs);
err |= sqfs_block_cache_init(&fs->md_cache, SQUASHFS_CACHED_BLKS);
err |= sqfs_block_cache_init(&fs->data_cache, DATA_CACHED_BLKS);
err |= sqfs_block_cache_init(&fs->frag_cache, FRAG_CACHED_BLKS);
err |= sqfs_blockidx_init(&fs->blockidx);
if (subdir && subdir[0] != '\0') {
sqfs_inode root;
err |= sqfs_inode_get(fs, &root, sqfs_inode_root(fs));
sqfs_inode_id new_root;
bool found = false;
err |= sqfs_lookup_path_with_id(fs, &root, subdir, &found, &new_root);
if (!found) {
sqfs_destroy(fs);
return SQFS_ERR;
}
fs->sb.root_inode = new_root;
}
if (err) {
sqfs_destroy(fs);
return SQFS_ERR;
}
return SQFS_OK;
}
sqfs_err sqfs_init(sqfs *fs, sqfs_fd_t fd, size_t offset) {
return sqfs_init_with_subdir(fs, fd, offset, NULL);
}
void sqfs_destroy(sqfs *fs) {
sqfs_table_destroy(&fs->id_table);
sqfs_table_destroy(&fs->frag_table);
if (sqfs_export_ok(fs))
sqfs_table_destroy(&fs->export_table);
sqfs_cache_destroy(&fs->md_cache);
sqfs_cache_destroy(&fs->data_cache);
sqfs_cache_destroy(&fs->frag_cache);
sqfs_cache_destroy(&fs->blockidx);
}
void sqfs_md_header(uint16_t hdr, bool *compressed, uint16_t *size) {
*compressed = !(hdr & SQUASHFS_COMPRESSED_BIT);
*size = hdr & ~SQUASHFS_COMPRESSED_BIT;
if (!*size)
*size = SQUASHFS_COMPRESSED_BIT;
}
void sqfs_data_header(uint32_t hdr, bool *compressed, uint32_t *size) {
*compressed = !(hdr & SQUASHFS_COMPRESSED_BIT_BLOCK);
*size = hdr & ~SQUASHFS_COMPRESSED_BIT_BLOCK;
}
sqfs_err sqfs_block_read(sqfs *fs, sqfs_off_t pos, bool compressed,
uint32_t size, size_t outsize, sqfs_block **block) {
sqfs_err err = SQFS_ERR;
if (!(*block = malloc(sizeof(**block))))
return SQFS_ERR;
/* start with refcount one, so dispose on failure path works as expected. */
(*block)->refcount = 1;
if (!((*block)->data = malloc(size)))
goto error;
if (sqfs_pread(fs->fd, (*block)->data, size, pos + fs->offset) != size)
goto error;
if (compressed) {
char *decomp = malloc(outsize);
if (!decomp)
goto error;
err = fs->decompressor((*block)->data, size, decomp, &outsize);
if (err) {
free(decomp);
goto error;
}
free((*block)->data);
(*block)->data = decomp;
(*block)->size = outsize;
} else {
(*block)->size = size;
}
return SQFS_OK;
error:
sqfs_block_dispose(*block);
*block = NULL;
return err;
}
sqfs_err sqfs_md_block_read(sqfs *fs, sqfs_off_t pos, size_t *data_size,
sqfs_block **block) {
sqfs_err err = SQFS_OK;
uint16_t hdr;
bool compressed;
uint16_t size;
*data_size = 0;
if (sqfs_pread(fs->fd, &hdr, sizeof(hdr), pos + fs->offset) != sizeof(hdr))
return SQFS_ERR;
pos += sizeof(hdr);
*data_size += sizeof(hdr);
sqfs_swapin16(&hdr);
sqfs_md_header(hdr, &compressed, &size);
err = sqfs_block_read(fs, pos, compressed, size,
SQUASHFS_METADATA_SIZE, block);
*data_size += size;
return err;
}
sqfs_err sqfs_data_block_read(sqfs *fs, sqfs_off_t pos, uint32_t hdr,
sqfs_block **block) {
bool compressed;
uint32_t size;
sqfs_data_header(hdr, &compressed, &size);
return sqfs_block_read(fs, pos, compressed, size,
fs->sb.block_size, block);
}
sqfs_err sqfs_md_cache(sqfs *fs, sqfs_off_t *pos, sqfs_block **block) {
sqfs_block_cache_entry *entry = sqfs_cache_get(&fs->md_cache, *pos);
if (!sqfs_cache_entry_valid(&fs->md_cache, entry)) {
sqfs_err err = SQFS_OK;
/* fprintf(stderr, "MD BLOCK: %12llx\n", (long long)*pos); */
err = sqfs_md_block_read(fs, *pos,
&entry->data_size, &entry->block);
if (err) {
sqfs_cache_put(&fs->md_cache, entry);
return err;
}
sqfs_cache_entry_mark_valid(&fs->md_cache, entry);
}
/* block is created with refcount 1, which accounts for presence in the
* cache (will be decremented on eviction).
*
* We increment it here as a convienience for the caller, who will
* obviously want one. Therefore all callers must eventually call deref
* by means of calling sqfs_block_dispose().
*/
*block = entry->block;
*pos += entry->data_size;
sqfs_block_ref(entry->block);
/* it is now safe to evict the entry from the cache, we have a
* reference to the block so eviction will not destroy it.
*/
sqfs_cache_put(&fs->md_cache, entry);
return SQFS_OK;
}
sqfs_err sqfs_data_cache(sqfs *fs, sqfs_cache *cache, sqfs_off_t pos,
uint32_t hdr, sqfs_block **block) {
sqfs_block_cache_entry *entry = sqfs_cache_get(cache, pos);
if (!sqfs_cache_entry_valid(cache, entry)) {
sqfs_err err = SQFS_OK;
err = sqfs_data_block_read(fs, pos, hdr,
&entry->block);
if (err) {
sqfs_cache_put(cache, entry);
return err;
}
sqfs_cache_entry_mark_valid(cache, entry);
}
/* block is created with refcount 1, which accounts for presence in the
* cache (will be decremented on eviction).
*
* We increment it here as a convenience for the caller, who will
* obviously want one. Therefore all callers must eventually call deref
* by means of calling sqfs_block_dispose().
*/
*block = entry->block;
sqfs_block_ref(*block);
/* it is now safe to evict the entry from the cache, we have a
* reference to the block so eviction will not destroy it.
*/
sqfs_cache_put(cache, entry);
return SQFS_OK;
}
void sqfs_block_dispose(sqfs_block *block) {
if (sqfs_block_deref(block)) {
free(block->data);
free(block);
}
}
static void sqfs_block_cache_dispose(void *data) {
sqfs_block_cache_entry *entry = (sqfs_block_cache_entry*)data;
sqfs_block_dispose(entry->block);
}
sqfs_err sqfs_block_cache_init(sqfs_cache *cache, size_t count) {
return sqfs_cache_init(cache, sizeof(sqfs_block_cache_entry), count,
&sqfs_block_cache_dispose);
}
void sqfs_md_cursor_inode(sqfs_md_cursor *cur, sqfs_inode_id id, sqfs_off_t base) {
cur->block = (id >> 16) + base;
cur->offset = id & 0xffff;
}
sqfs_err sqfs_md_read(sqfs *fs, sqfs_md_cursor *cur, void *buf, size_t size) {
sqfs_off_t pos = cur->block;
while (size > 0) {
sqfs_block *block;
size_t take;
sqfs_err err = sqfs_md_cache(fs, &pos, &block);
if (err)
return err;
take = block->size - cur->offset;
if (take > size)
take = size;
if (buf)
memcpy(buf, (char*)block->data + cur->offset, take);
if (buf)
buf = (char*)buf + take;
size -= take;
cur->offset += take;
if (cur->offset == block->size) {
cur->block = pos;
cur->offset = 0;
}
sqfs_block_dispose(block);
}
return SQFS_OK;
}
size_t sqfs_divceil(uint64_t total, size_t group) {
size_t q = (size_t)(total / group);
if (total % group)
q += 1;
return q;
}
sqfs_err sqfs_id_get(sqfs *fs, uint16_t idx, sqfs_id_t *id) {
uint32_t rid;
sqfs_err err = sqfs_table_get(&fs->id_table, fs, idx, &rid);
if (err)
return err;
sqfs_swapin32(&rid);
*id = (sqfs_id_t)rid;
return SQFS_OK;
}
sqfs_err sqfs_readlink(sqfs *fs, sqfs_inode *inode, char *buf, size_t *size) {
size_t want;
sqfs_md_cursor cur;
sqfs_err err = SQFS_OK;
if (!S_ISLNK(inode->base.mode))
return SQFS_ERR;
want = inode->xtra.symlink_size;
if (!buf) {
*size = want + 1;
return SQFS_OK;
}
if (want > *size - 1)
want = *size - 1;
cur = inode->next;
err = sqfs_md_read(fs, &cur, buf, want);
buf[want] = '\0';
return err;
}
int sqfs_export_ok(sqfs *fs) {
return fs->sb.lookup_table_start != SQUASHFS_INVALID_BLK;
}
sqfs_err sqfs_export_inode(sqfs *fs, sqfs_inode_num n, sqfs_inode_id *i) {
uint64_t r;
sqfs_err err = SQFS_OK;
if (!sqfs_export_ok(fs))
return SQFS_UNSUP;
err = sqfs_table_get(&fs->export_table, fs, n - 1, &r);
if (err)
return err;
sqfs_swapin64(&r);
*i = r;
return SQFS_OK;
}
sqfs_inode_id sqfs_inode_root(sqfs *fs) {
return fs->sb.root_inode;
}
/* Turn the internal format of a device number to our system's dev_t
* It looks like rdev is just what the Linux kernel uses: 20 bit minor,
* split in two around a 12 bit major
*/
static void sqfs_decode_dev(sqfs_inode *i, uint32_t rdev) {
i->xtra.dev.major = (rdev >> 8) & 0xfff;
i->xtra.dev.minor = (rdev & 0xff) | ((rdev >> 12) & 0xfff00);
}
#define INODE_TYPE(_type) \
struct squashfs_##_type##_inode x; \
err = sqfs_md_read(fs, &inode->next, &x, sizeof(x)); \
if (err) return err; \
sqfs_swapin_##_type##_inode(&x)
sqfs_err sqfs_inode_get(sqfs *fs, sqfs_inode *inode, sqfs_inode_id id) {
sqfs_md_cursor cur;
sqfs_err err = SQFS_OK;
memset(inode, 0, sizeof(*inode));
inode->xattr = SQUASHFS_INVALID_XATTR;
sqfs_md_cursor_inode(&cur, id, fs->sb.inode_table_start);
inode->next = cur;
err = sqfs_md_read(fs, &cur, &inode->base, sizeof(inode->base));
if (err)
return err;
sqfs_swapin_base_inode(&inode->base);
inode->base.mode |= sqfs_mode(inode->base.inode_type);
switch (inode->base.inode_type) {
case SQUASHFS_REG_TYPE: {
INODE_TYPE(reg);
inode->nlink = 1;
inode->xtra.reg.start_block = x.start_block;
inode->xtra.reg.file_size = x.file_size;
inode->xtra.reg.frag_idx = x.fragment;
inode->xtra.reg.frag_off = x.offset;
break;
}
case SQUASHFS_LREG_TYPE: {
INODE_TYPE(lreg);
inode->nlink = x.nlink;
inode->xtra.reg.start_block = x.start_block;
inode->xtra.reg.file_size = x.file_size;
inode->xtra.reg.frag_idx = x.fragment;
inode->xtra.reg.frag_off = x.offset;
inode->xattr = x.xattr;
break;
}
case SQUASHFS_DIR_TYPE: {
INODE_TYPE(dir);
inode->nlink = x.nlink;
inode->xtra.dir.start_block = x.start_block;
inode->xtra.dir.offset = x.offset;
inode->xtra.dir.dir_size = x.file_size;
inode->xtra.dir.idx_count = 0;
inode->xtra.dir.parent_inode = x.parent_inode;
break;
}
case SQUASHFS_LDIR_TYPE: {
INODE_TYPE(ldir);
inode->nlink = x.nlink;
inode->xtra.dir.start_block = x.start_block;
inode->xtra.dir.offset = x.offset;
inode->xtra.dir.dir_size = x.file_size;
inode->xtra.dir.idx_count = x.i_count;
inode->xtra.dir.parent_inode = x.parent_inode;
inode->xattr = x.xattr;
break;
}
case SQUASHFS_SYMLINK_TYPE:
case SQUASHFS_LSYMLINK_TYPE: {
INODE_TYPE(symlink);
inode->nlink = x.nlink;
inode->xtra.symlink_size = x.symlink_size;
if (inode->base.inode_type == SQUASHFS_LSYMLINK_TYPE) {
/* skip symlink target */
cur = inode->next;
err = sqfs_md_read(fs, &cur, NULL, inode->xtra.symlink_size);
if (err)
return err;
err = sqfs_md_read(fs, &cur, &inode->xattr, sizeof(inode->xattr));
if (err)
return err;
sqfs_swapin32(&inode->xattr);
}
break;
}
case SQUASHFS_BLKDEV_TYPE:
case SQUASHFS_CHRDEV_TYPE: {
INODE_TYPE(dev);
inode->nlink = x.nlink;
sqfs_decode_dev(inode, x.rdev);
break;
}
case SQUASHFS_LBLKDEV_TYPE:
case SQUASHFS_LCHRDEV_TYPE: {
INODE_TYPE(ldev);
inode->nlink = x.nlink;
sqfs_decode_dev(inode, x.rdev);
inode->xattr = x.xattr;
break;
}
case SQUASHFS_SOCKET_TYPE:
case SQUASHFS_FIFO_TYPE: {
INODE_TYPE(ipc);
inode->nlink = x.nlink;
break;
}
case SQUASHFS_LSOCKET_TYPE:
case SQUASHFS_LFIFO_TYPE: {
INODE_TYPE(lipc);
inode->nlink = x.nlink;
inode->xattr = x.xattr;
break;
}
default: return SQFS_ERR;
}
return SQFS_OK;
}
#undef INODE_TYPE