]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/ubifs/ubifs.c
lib_generic: gunzip: New function zunzip
[people/ms/u-boot.git] / fs / ubifs / ubifs.c
CommitLineData
9eefe2a2
SR
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * (C) Copyright 2008-2009
7 * Stefan Roese, DENX Software Engineering, sr@denx.de.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 51
20 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * Authors: Artem Bityutskiy (Битюцкий Артём)
23 * Adrian Hunter
24 */
25
26#include "ubifs.h"
27
28#if !defined(CONFIG_SYS_64BIT_VSPRINTF)
29#warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
30#endif
31
32DECLARE_GLOBAL_DATA_PTR;
33
34/* compress.c */
35
36/*
37 * We need a wrapper for gunzip() because the parameters are
38 * incompatible with the lzo decompressor.
39 */
40static int gzip_decompress(const unsigned char *in, size_t in_len,
41 unsigned char *out, size_t *out_len)
42{
43 unsigned long len = in_len;
44 return gunzip(out, *out_len, (unsigned char *)in, &len);
45}
46
47/* Fake description object for the "none" compressor */
48static struct ubifs_compressor none_compr = {
49 .compr_type = UBIFS_COMPR_NONE,
50 .name = "no compression",
51 .capi_name = "",
52 .decompress = NULL,
53};
54
55static struct ubifs_compressor lzo_compr = {
56 .compr_type = UBIFS_COMPR_LZO,
57 .name = "LZO",
58 .capi_name = "lzo",
59 .decompress = lzo1x_decompress_safe,
60};
61
62static struct ubifs_compressor zlib_compr = {
63 .compr_type = UBIFS_COMPR_ZLIB,
64 .name = "zlib",
65 .capi_name = "deflate",
66 .decompress = gzip_decompress,
67};
68
69/* All UBIFS compressors */
70struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
71
72/**
73 * ubifs_decompress - decompress data.
74 * @in_buf: data to decompress
75 * @in_len: length of the data to decompress
76 * @out_buf: output buffer where decompressed data should
77 * @out_len: output length is returned here
78 * @compr_type: type of compression
79 *
80 * This function decompresses data from buffer @in_buf into buffer @out_buf.
81 * The length of the uncompressed data is returned in @out_len. This functions
82 * returns %0 on success or a negative error code on failure.
83 */
84int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
85 int *out_len, int compr_type)
86{
87 int err;
88 struct ubifs_compressor *compr;
89
90 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
91 ubifs_err("invalid compression type %d", compr_type);
92 return -EINVAL;
93 }
94
95 compr = ubifs_compressors[compr_type];
96
97 if (unlikely(!compr->capi_name)) {
98 ubifs_err("%s compression is not compiled in", compr->name);
99 return -EINVAL;
100 }
101
102 if (compr_type == UBIFS_COMPR_NONE) {
103 memcpy(out_buf, in_buf, in_len);
104 *out_len = in_len;
105 return 0;
106 }
107
108 err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
109 if (err)
110 ubifs_err("cannot decompress %d bytes, compressor %s, "
111 "error %d", in_len, compr->name, err);
112
113 return err;
114}
115
116/**
117 * compr_init - initialize a compressor.
118 * @compr: compressor description object
119 *
120 * This function initializes the requested compressor and returns zero in case
121 * of success or a negative error code in case of failure.
122 */
123static int __init compr_init(struct ubifs_compressor *compr)
124{
125 ubifs_compressors[compr->compr_type] = compr;
126 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
127 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
128 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
129 return 0;
130}
131
132/**
133 * ubifs_compressors_init - initialize UBIFS compressors.
134 *
135 * This function initializes the compressor which were compiled in. Returns
136 * zero in case of success and a negative error code in case of failure.
137 */
138int __init ubifs_compressors_init(void)
139{
140 int err;
141
142 err = compr_init(&lzo_compr);
143 if (err)
144 return err;
145
146 err = compr_init(&zlib_compr);
147 if (err)
148 return err;
149
faac4fd8
ML
150 err = compr_init(&none_compr);
151 if (err)
152 return err;
153
9eefe2a2
SR
154 return 0;
155}
156
157/*
158 * ubifsls...
159 */
160
161static int filldir(struct ubifs_info *c, const char *name, int namlen,
162 u64 ino, unsigned int d_type)
163{
164 struct inode *inode;
165 char filetime[32];
166
167 switch (d_type) {
168 case UBIFS_ITYPE_REG:
169 printf("\t");
170 break;
171 case UBIFS_ITYPE_DIR:
172 printf("<DIR>\t");
173 break;
174 case UBIFS_ITYPE_LNK:
175 printf("<LNK>\t");
176 break;
177 default:
178 printf("other\t");
179 break;
180 }
181
182 inode = ubifs_iget(c->vfs_sb, ino);
183 if (IS_ERR(inode)) {
184 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
185 __func__, ino, inode);
186 return -1;
187 }
188 ctime_r((time_t *)&inode->i_mtime, filetime);
189 printf("%9lld %24.24s ", inode->i_size, filetime);
190 ubifs_iput(inode);
191
192 printf("%s\n", name);
193
194 return 0;
195}
196
197static int ubifs_printdir(struct file *file, void *dirent)
198{
199 int err, over = 0;
200 struct qstr nm;
201 union ubifs_key key;
202 struct ubifs_dent_node *dent;
203 struct inode *dir = file->f_path.dentry->d_inode;
204 struct ubifs_info *c = dir->i_sb->s_fs_info;
205
206 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
207
208 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
209 /*
210 * The directory was seek'ed to a senseless position or there
211 * are no more entries.
212 */
213 return 0;
214
215 if (file->f_pos == 1) {
216 /* Find the first entry in TNC and save it */
217 lowest_dent_key(c, &key, dir->i_ino);
218 nm.name = NULL;
219 dent = ubifs_tnc_next_ent(c, &key, &nm);
220 if (IS_ERR(dent)) {
221 err = PTR_ERR(dent);
222 goto out;
223 }
224
225 file->f_pos = key_hash_flash(c, &dent->key);
226 file->private_data = dent;
227 }
228
229 dent = file->private_data;
230 if (!dent) {
231 /*
232 * The directory was seek'ed to and is now readdir'ed.
233 * Find the entry corresponding to @file->f_pos or the
234 * closest one.
235 */
236 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
237 nm.name = NULL;
238 dent = ubifs_tnc_next_ent(c, &key, &nm);
239 if (IS_ERR(dent)) {
240 err = PTR_ERR(dent);
241 goto out;
242 }
243 file->f_pos = key_hash_flash(c, &dent->key);
244 file->private_data = dent;
245 }
246
247 while (1) {
248 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
249 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
250 key_hash_flash(c, &dent->key));
251 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
252
253 nm.len = le16_to_cpu(dent->nlen);
254 over = filldir(c, (char *)dent->name, nm.len,
255 le64_to_cpu(dent->inum), dent->type);
256 if (over)
257 return 0;
258
259 /* Switch to the next entry */
260 key_read(c, &dent->key, &key);
261 nm.name = (char *)dent->name;
262 dent = ubifs_tnc_next_ent(c, &key, &nm);
263 if (IS_ERR(dent)) {
264 err = PTR_ERR(dent);
265 goto out;
266 }
267
268 kfree(file->private_data);
269 file->f_pos = key_hash_flash(c, &dent->key);
270 file->private_data = dent;
271 cond_resched();
272 }
273
274out:
275 if (err != -ENOENT) {
276 ubifs_err("cannot find next direntry, error %d", err);
277 return err;
278 }
279
280 kfree(file->private_data);
281 file->private_data = NULL;
282 file->f_pos = 2;
283 return 0;
284}
285
286static int ubifs_finddir(struct super_block *sb, char *dirname,
287 unsigned long root_inum, unsigned long *inum)
288{
289 int err;
290 struct qstr nm;
291 union ubifs_key key;
292 struct ubifs_dent_node *dent;
293 struct ubifs_info *c;
294 struct file *file;
295 struct dentry *dentry;
296 struct inode *dir;
297
298 file = kzalloc(sizeof(struct file), 0);
299 dentry = kzalloc(sizeof(struct dentry), 0);
300 dir = kzalloc(sizeof(struct inode), 0);
301 if (!file || !dentry || !dir) {
302 printf("%s: Error, no memory for malloc!\n", __func__);
303 err = -ENOMEM;
304 goto out;
305 }
306
307 dir->i_sb = sb;
308 file->f_path.dentry = dentry;
309 file->f_path.dentry->d_parent = dentry;
310 file->f_path.dentry->d_inode = dir;
311 file->f_path.dentry->d_inode->i_ino = root_inum;
312 c = sb->s_fs_info;
313
314 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
315
316 /* Find the first entry in TNC and save it */
317 lowest_dent_key(c, &key, dir->i_ino);
318 nm.name = NULL;
319 dent = ubifs_tnc_next_ent(c, &key, &nm);
320 if (IS_ERR(dent)) {
321 err = PTR_ERR(dent);
322 goto out;
323 }
324
325 file->f_pos = key_hash_flash(c, &dent->key);
326 file->private_data = dent;
327
328 while (1) {
329 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
330 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
331 key_hash_flash(c, &dent->key));
332 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
333
334 nm.len = le16_to_cpu(dent->nlen);
335 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
336 (strlen(dirname) == nm.len)) {
337 *inum = le64_to_cpu(dent->inum);
338 return 1;
339 }
340
341 /* Switch to the next entry */
342 key_read(c, &dent->key, &key);
343 nm.name = (char *)dent->name;
344 dent = ubifs_tnc_next_ent(c, &key, &nm);
345 if (IS_ERR(dent)) {
346 err = PTR_ERR(dent);
347 goto out;
348 }
349
350 kfree(file->private_data);
351 file->f_pos = key_hash_flash(c, &dent->key);
352 file->private_data = dent;
353 cond_resched();
354 }
355
356out:
357 if (err != -ENOENT) {
358 ubifs_err("cannot find next direntry, error %d", err);
359 return err;
360 }
361
362 if (file)
363 free(file);
364 if (dentry)
365 free(dentry);
366 if (dir)
367 free(dir);
368
369 if (file->private_data)
370 kfree(file->private_data);
371 file->private_data = NULL;
372 file->f_pos = 2;
373 return 0;
374}
375
376static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
377{
378 int ret;
379 char *next;
380 char fpath[128];
381 char *name = fpath;
382 unsigned long root_inum = 1;
383 unsigned long inum;
384
385 strcpy(fpath, filename);
386
387 /* Remove all leading slashes */
388 while (*name == '/')
389 name++;
390
391 /*
392 * Handle root-direcoty ('/')
393 */
394 inum = root_inum;
395 if (!name || *name == '\0')
396 return inum;
397
398 for (;;) {
399 /* Extract the actual part from the pathname. */
400 next = strchr(name, '/');
401 if (next) {
402 /* Remove all leading slashes. */
403 while (*next == '/')
404 *(next++) = '\0';
405 }
406
407 ret = ubifs_finddir(sb, name, root_inum, &inum);
408
409 /*
410 * Check if directory with this name exists
411 */
412
413 /* Found the node! */
414 if (!next || *next == '\0') {
415 if (ret)
416 return inum;
417
418 break;
419 }
420
421 root_inum = inum;
422 name = next;
423 }
424
425 return 0;
426}
427
428int ubifs_ls(char *filename)
429{
430 struct ubifs_info *c = ubifs_sb->s_fs_info;
431 struct file *file;
432 struct dentry *dentry;
433 struct inode *dir;
434 void *dirent = NULL;
435 unsigned long inum;
436 int ret = 0;
437
438 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
439 inum = ubifs_findfile(ubifs_sb, filename);
440 if (!inum) {
441 ret = -1;
442 goto out;
443 }
444
445 file = kzalloc(sizeof(struct file), 0);
446 dentry = kzalloc(sizeof(struct dentry), 0);
447 dir = kzalloc(sizeof(struct inode), 0);
448 if (!file || !dentry || !dir) {
449 printf("%s: Error, no memory for malloc!\n", __func__);
450 ret = -ENOMEM;
451 goto out_mem;
452 }
453
454 dir->i_sb = ubifs_sb;
455 file->f_path.dentry = dentry;
456 file->f_path.dentry->d_parent = dentry;
457 file->f_path.dentry->d_inode = dir;
458 file->f_path.dentry->d_inode->i_ino = inum;
459 file->f_pos = 1;
460 file->private_data = NULL;
461 ubifs_printdir(file, dirent);
462
463out_mem:
464 if (file)
465 free(file);
466 if (dentry)
467 free(dentry);
468 if (dir)
469 free(dir);
470
471out:
472 ubi_close_volume(c->ubi);
473 return ret;
474}
475
476/*
477 * ubifsload...
478 */
479
480/* file.c */
481
482static inline void *kmap(struct page *page)
483{
484 return page->addr;
485}
486
487static int read_block(struct inode *inode, void *addr, unsigned int block,
488 struct ubifs_data_node *dn)
489{
490 struct ubifs_info *c = inode->i_sb->s_fs_info;
491 int err, len, out_len;
492 union ubifs_key key;
493 unsigned int dlen;
494
495 data_key_init(c, &key, inode->i_ino, block);
496 err = ubifs_tnc_lookup(c, &key, dn);
497 if (err) {
498 if (err == -ENOENT)
499 /* Not found, so it must be a hole */
500 memset(addr, 0, UBIFS_BLOCK_SIZE);
501 return err;
502 }
503
504 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
505
506 len = le32_to_cpu(dn->size);
507 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
508 goto dump;
509
510 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
511 out_len = UBIFS_BLOCK_SIZE;
512 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
513 le16_to_cpu(dn->compr_type));
514 if (err || len != out_len)
515 goto dump;
516
517 /*
518 * Data length can be less than a full block, even for blocks that are
519 * not the last in the file (e.g., as a result of making a hole and
520 * appending data). Ensure that the remainder is zeroed out.
521 */
522 if (len < UBIFS_BLOCK_SIZE)
523 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
524
525 return 0;
526
527dump:
528 ubifs_err("bad data node (block %u, inode %lu)",
529 block, inode->i_ino);
530 dbg_dump_node(c, dn);
531 return -EINVAL;
532}
533
534static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
535{
536 void *addr;
537 int err = 0, i;
538 unsigned int block, beyond;
539 struct ubifs_data_node *dn;
540 loff_t i_size = inode->i_size;
541
542 dbg_gen("ino %lu, pg %lu, i_size %lld",
543 inode->i_ino, page->index, i_size);
544
545 addr = kmap(page);
546
547 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
548 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
549 if (block >= beyond) {
550 /* Reading beyond inode */
551 memset(addr, 0, PAGE_CACHE_SIZE);
552 goto out;
553 }
554
555 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
556 if (!dn) {
557 err = -ENOMEM;
558 goto error;
559 }
560
561 i = 0;
562 while (1) {
563 int ret;
564
565 if (block >= beyond) {
566 /* Reading beyond inode */
567 err = -ENOENT;
568 memset(addr, 0, UBIFS_BLOCK_SIZE);
569 } else {
570 ret = read_block(inode, addr, block, dn);
571 if (ret) {
572 err = ret;
573 if (err != -ENOENT)
574 break;
575 } else if (block + 1 == beyond) {
576 int dlen = le32_to_cpu(dn->size);
577 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
578
579 if (ilen && ilen < dlen)
580 memset(addr + ilen, 0, dlen - ilen);
581 }
582 }
583 if (++i >= UBIFS_BLOCKS_PER_PAGE)
584 break;
585 block += 1;
586 addr += UBIFS_BLOCK_SIZE;
587 }
588 if (err) {
589 if (err == -ENOENT) {
590 /* Not found, so it must be a hole */
591 dbg_gen("hole");
592 goto out_free;
593 }
594 ubifs_err("cannot read page %lu of inode %lu, error %d",
595 page->index, inode->i_ino, err);
596 goto error;
597 }
598
599out_free:
600 kfree(dn);
601out:
602 return 0;
603
604error:
605 kfree(dn);
606 return err;
607}
608
609int ubifs_load(char *filename, u32 addr, u32 size)
610{
611 struct ubifs_info *c = ubifs_sb->s_fs_info;
612 unsigned long inum;
613 struct inode *inode;
614 struct page page;
615 int err = 0;
616 int i;
617 int count;
618 char link_name[64];
619 struct ubifs_inode *ui;
620
621 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
622 inum = ubifs_findfile(ubifs_sb, filename);
623 if (!inum) {
624 err = -1;
625 goto out;
626 }
627
628 /*
629 * Read file inode
630 */
631 inode = ubifs_iget(ubifs_sb, inum);
632 if (IS_ERR(inode)) {
633 printf("%s: Error reading inode %ld!\n", __func__, inum);
634 err = PTR_ERR(inode);
635 goto out;
636 }
637
638 /*
639 * Check for symbolic link
640 */
641 ui = ubifs_inode(inode);
642 if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
643 memcpy(link_name, ui->data, ui->data_len);
dbd33614 644 link_name[ui->data_len] = '\0';
9eefe2a2
SR
645 printf("%s is linked to %s!\n", filename, link_name);
646 ubifs_iput(inode);
647
648 /*
649 * Now we have the "real" filename, call ubifs_load()
650 * again (recursive call) to load this file instead
651 */
652 return ubifs_load(link_name, addr, size);
653 }
654
655 /*
656 * If no size was specified or if size bigger than filesize
657 * set size to filesize
658 */
659 if ((size == 0) || (size > inode->i_size))
660 size = inode->i_size;
661
662 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
663 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
664 filename, addr, size, size);
665
666 page.addr = (void *)addr;
667 page.index = 0;
668 page.inode = inode;
669 for (i = 0; i < count; i++) {
670 err = do_readpage(c, inode, &page);
671 if (err)
672 break;
673
674 page.addr += PAGE_SIZE;
675 page.index++;
676 }
677
678 if (err)
679 printf("Error reading file '%s'\n", filename);
680 else
681 printf("Done\n");
682
683 ubifs_iput(inode);
684
685out:
686 ubi_close_volume(c->ubi);
687 return err;
688}