2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * (C) Copyright 2008-2009
7 * Stefan Roese, DENX Software Engineering, sr@denx.de.
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.
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
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
22 * Authors: Artem Bityutskiy (Битюцкий Артём)
28 #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
29 #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
32 DECLARE_GLOBAL_DATA_PTR;
37 * We need a wrapper for gunzip() because the parameters are
38 * incompatible with the lzo decompressor.
40 static int gzip_decompress(const unsigned char *in, size_t in_len,
41 unsigned char *out, size_t *out_len)
43 unsigned long len = in_len;
44 return gunzip(out, *out_len, (unsigned char *)in, &len);
47 /* Fake description object for the "none" compressor */
48 static struct ubifs_compressor none_compr = {
49 .compr_type = UBIFS_COMPR_NONE,
50 .name = "no compression",
55 static struct ubifs_compressor lzo_compr = {
56 .compr_type = UBIFS_COMPR_LZO,
59 .decompress = lzo1x_decompress_safe,
62 static struct ubifs_compressor zlib_compr = {
63 .compr_type = UBIFS_COMPR_ZLIB,
65 .capi_name = "deflate",
66 .decompress = gzip_decompress,
69 /* All UBIFS compressors */
70 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
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
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.
84 int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
85 int *out_len, int compr_type)
88 struct ubifs_compressor *compr;
90 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
91 ubifs_err("invalid compression type %d", compr_type);
95 compr = ubifs_compressors[compr_type];
97 if (unlikely(!compr->capi_name)) {
98 ubifs_err("%s compression is not compiled in", compr->name);
102 if (compr_type == UBIFS_COMPR_NONE) {
103 memcpy(out_buf, in_buf, in_len);
108 err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
110 ubifs_err("cannot decompress %d bytes, compressor %s, "
111 "error %d", in_len, compr->name, err);
117 * compr_init - initialize a compressor.
118 * @compr: compressor description object
120 * This function initializes the requested compressor and returns zero in case
121 * of success or a negative error code in case of failure.
123 static int __init compr_init(struct ubifs_compressor *compr)
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;
133 * ubifs_compressors_init - initialize UBIFS compressors.
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.
138 int __init ubifs_compressors_init(void)
142 err = compr_init(&lzo_compr);
146 err = compr_init(&zlib_compr);
150 err = compr_init(&none_compr);
161 static int filldir(struct ubifs_info *c, const char *name, int namlen,
162 u64 ino, unsigned int d_type)
168 case UBIFS_ITYPE_REG:
171 case UBIFS_ITYPE_DIR:
174 case UBIFS_ITYPE_LNK:
182 inode = ubifs_iget(c->vfs_sb, ino);
184 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
185 __func__, ino, inode);
188 ctime_r((time_t *)&inode->i_mtime, filetime);
189 printf("%9lld %24.24s ", inode->i_size, filetime);
192 printf("%s\n", name);
197 static int ubifs_printdir(struct file *file, void *dirent)
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;
206 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
208 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
210 * The directory was seek'ed to a senseless position or there
211 * are no more entries.
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);
219 dent = ubifs_tnc_next_ent(c, &key, &nm);
225 file->f_pos = key_hash_flash(c, &dent->key);
226 file->private_data = dent;
229 dent = file->private_data;
232 * The directory was seek'ed to and is now readdir'ed.
233 * Find the entry corresponding to @file->f_pos or the
236 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
238 dent = ubifs_tnc_next_ent(c, &key, &nm);
243 file->f_pos = key_hash_flash(c, &dent->key);
244 file->private_data = dent;
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);
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);
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);
268 kfree(file->private_data);
269 file->f_pos = key_hash_flash(c, &dent->key);
270 file->private_data = dent;
275 if (err != -ENOENT) {
276 ubifs_err("cannot find next direntry, error %d", err);
280 kfree(file->private_data);
281 file->private_data = NULL;
286 static int ubifs_finddir(struct super_block *sb, char *dirname,
287 unsigned long root_inum, unsigned long *inum)
292 struct ubifs_dent_node *dent;
293 struct ubifs_info *c;
295 struct dentry *dentry;
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__);
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;
314 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
316 /* Find the first entry in TNC and save it */
317 lowest_dent_key(c, &key, dir->i_ino);
319 dent = ubifs_tnc_next_ent(c, &key, &nm);
325 file->f_pos = key_hash_flash(c, &dent->key);
326 file->private_data = dent;
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);
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);
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);
350 kfree(file->private_data);
351 file->f_pos = key_hash_flash(c, &dent->key);
352 file->private_data = dent;
357 if (err != -ENOENT) {
358 ubifs_err("cannot find next direntry, error %d", err);
369 if (file->private_data)
370 kfree(file->private_data);
371 file->private_data = NULL;
376 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
382 unsigned long root_inum = 1;
385 strcpy(fpath, filename);
387 /* Remove all leading slashes */
392 * Handle root-direcoty ('/')
395 if (!name || *name == '\0')
399 /* Extract the actual part from the pathname. */
400 next = strchr(name, '/');
402 /* Remove all leading slashes. */
407 ret = ubifs_finddir(sb, name, root_inum, &inum);
410 * Check if directory with this name exists
413 /* Found the node! */
414 if (!next || *next == '\0') {
428 int ubifs_ls(char *filename)
430 struct ubifs_info *c = ubifs_sb->s_fs_info;
432 struct dentry *dentry;
438 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
439 inum = ubifs_findfile(ubifs_sb, filename);
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__);
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;
460 file->private_data = NULL;
461 ubifs_printdir(file, dirent);
472 ubi_close_volume(c->ubi);
482 static inline void *kmap(struct page *page)
487 static int read_block(struct inode *inode, void *addr, unsigned int block,
488 struct ubifs_data_node *dn)
490 struct ubifs_info *c = inode->i_sb->s_fs_info;
491 int err, len, out_len;
495 data_key_init(c, &key, inode->i_ino, block);
496 err = ubifs_tnc_lookup(c, &key, dn);
499 /* Not found, so it must be a hole */
500 memset(addr, 0, UBIFS_BLOCK_SIZE);
504 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
506 len = le32_to_cpu(dn->size);
507 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
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)
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.
522 if (len < UBIFS_BLOCK_SIZE)
523 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
528 ubifs_err("bad data node (block %u, inode %lu)",
529 block, inode->i_ino);
530 dbg_dump_node(c, dn);
534 static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
538 unsigned int block, beyond;
539 struct ubifs_data_node *dn;
540 loff_t i_size = inode->i_size;
542 dbg_gen("ino %lu, pg %lu, i_size %lld",
543 inode->i_ino, page->index, i_size);
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);
555 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
565 if (block >= beyond) {
566 /* Reading beyond inode */
568 memset(addr, 0, UBIFS_BLOCK_SIZE);
570 ret = read_block(inode, addr, block, dn);
575 } else if (block + 1 == beyond) {
576 int dlen = le32_to_cpu(dn->size);
577 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
579 if (ilen && ilen < dlen)
580 memset(addr + ilen, 0, dlen - ilen);
583 if (++i >= UBIFS_BLOCKS_PER_PAGE)
586 addr += UBIFS_BLOCK_SIZE;
589 if (err == -ENOENT) {
590 /* Not found, so it must be a hole */
594 ubifs_err("cannot read page %lu of inode %lu, error %d",
595 page->index, inode->i_ino, err);
609 int ubifs_load(char *filename, u32 addr, u32 size)
611 struct ubifs_info *c = ubifs_sb->s_fs_info;
619 struct ubifs_inode *ui;
621 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
622 inum = ubifs_findfile(ubifs_sb, filename);
631 inode = ubifs_iget(ubifs_sb, inum);
633 printf("%s: Error reading inode %ld!\n", __func__, inum);
634 err = PTR_ERR(inode);
639 * Check for symbolic link
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);
644 printf("%s is linked to %s!\n", filename, link_name);
648 * Now we have the "real" filename, call ubifs_load()
649 * again (recursive call) to load this file instead
651 return ubifs_load(link_name, addr, size);
655 * If no size was specified or if size bigger than filesize
656 * set size to filesize
658 if ((size == 0) || (size > inode->i_size))
659 size = inode->i_size;
661 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
662 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
663 filename, addr, size, size);
665 page.addr = (void *)addr;
668 for (i = 0; i < count; i++) {
669 err = do_readpage(c, inode, &page);
673 page.addr += PAGE_SIZE;
678 printf("Error reading file '%s'\n", filename);
685 ubi_close_volume(c->ubi);