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 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
158 static int filldir(struct ubifs_info *c, const char *name, int namlen,
159 u64 ino, unsigned int d_type)
165 case UBIFS_ITYPE_REG:
168 case UBIFS_ITYPE_DIR:
171 case UBIFS_ITYPE_LNK:
179 inode = ubifs_iget(c->vfs_sb, ino);
181 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
182 __func__, ino, inode);
185 ctime_r((time_t *)&inode->i_mtime, filetime);
186 printf("%9lld %24.24s ", inode->i_size, filetime);
189 printf("%s\n", name);
194 static int ubifs_printdir(struct file *file, void *dirent)
199 struct ubifs_dent_node *dent;
200 struct inode *dir = file->f_path.dentry->d_inode;
201 struct ubifs_info *c = dir->i_sb->s_fs_info;
203 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
205 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
207 * The directory was seek'ed to a senseless position or there
208 * are no more entries.
212 if (file->f_pos == 1) {
213 /* Find the first entry in TNC and save it */
214 lowest_dent_key(c, &key, dir->i_ino);
216 dent = ubifs_tnc_next_ent(c, &key, &nm);
222 file->f_pos = key_hash_flash(c, &dent->key);
223 file->private_data = dent;
226 dent = file->private_data;
229 * The directory was seek'ed to and is now readdir'ed.
230 * Find the entry corresponding to @file->f_pos or the
233 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
235 dent = ubifs_tnc_next_ent(c, &key, &nm);
240 file->f_pos = key_hash_flash(c, &dent->key);
241 file->private_data = dent;
245 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
246 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
247 key_hash_flash(c, &dent->key));
248 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
250 nm.len = le16_to_cpu(dent->nlen);
251 over = filldir(c, (char *)dent->name, nm.len,
252 le64_to_cpu(dent->inum), dent->type);
256 /* Switch to the next entry */
257 key_read(c, &dent->key, &key);
258 nm.name = (char *)dent->name;
259 dent = ubifs_tnc_next_ent(c, &key, &nm);
265 kfree(file->private_data);
266 file->f_pos = key_hash_flash(c, &dent->key);
267 file->private_data = dent;
272 if (err != -ENOENT) {
273 ubifs_err("cannot find next direntry, error %d", err);
277 kfree(file->private_data);
278 file->private_data = NULL;
283 static int ubifs_finddir(struct super_block *sb, char *dirname,
284 unsigned long root_inum, unsigned long *inum)
289 struct ubifs_dent_node *dent;
290 struct ubifs_info *c;
292 struct dentry *dentry;
295 file = kzalloc(sizeof(struct file), 0);
296 dentry = kzalloc(sizeof(struct dentry), 0);
297 dir = kzalloc(sizeof(struct inode), 0);
298 if (!file || !dentry || !dir) {
299 printf("%s: Error, no memory for malloc!\n", __func__);
305 file->f_path.dentry = dentry;
306 file->f_path.dentry->d_parent = dentry;
307 file->f_path.dentry->d_inode = dir;
308 file->f_path.dentry->d_inode->i_ino = root_inum;
311 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
313 /* Find the first entry in TNC and save it */
314 lowest_dent_key(c, &key, dir->i_ino);
316 dent = ubifs_tnc_next_ent(c, &key, &nm);
322 file->f_pos = key_hash_flash(c, &dent->key);
323 file->private_data = dent;
326 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
327 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
328 key_hash_flash(c, &dent->key));
329 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
331 nm.len = le16_to_cpu(dent->nlen);
332 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
333 (strlen(dirname) == nm.len)) {
334 *inum = le64_to_cpu(dent->inum);
338 /* Switch to the next entry */
339 key_read(c, &dent->key, &key);
340 nm.name = (char *)dent->name;
341 dent = ubifs_tnc_next_ent(c, &key, &nm);
347 kfree(file->private_data);
348 file->f_pos = key_hash_flash(c, &dent->key);
349 file->private_data = dent;
354 if (err != -ENOENT) {
355 ubifs_err("cannot find next direntry, error %d", err);
366 if (file->private_data)
367 kfree(file->private_data);
368 file->private_data = NULL;
373 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
379 unsigned long root_inum = 1;
382 strcpy(fpath, filename);
384 /* Remove all leading slashes */
389 * Handle root-direcoty ('/')
392 if (!name || *name == '\0')
396 /* Extract the actual part from the pathname. */
397 next = strchr(name, '/');
399 /* Remove all leading slashes. */
404 ret = ubifs_finddir(sb, name, root_inum, &inum);
407 * Check if directory with this name exists
410 /* Found the node! */
411 if (!next || *next == '\0') {
425 int ubifs_ls(char *filename)
427 struct ubifs_info *c = ubifs_sb->s_fs_info;
429 struct dentry *dentry;
435 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
436 inum = ubifs_findfile(ubifs_sb, filename);
442 file = kzalloc(sizeof(struct file), 0);
443 dentry = kzalloc(sizeof(struct dentry), 0);
444 dir = kzalloc(sizeof(struct inode), 0);
445 if (!file || !dentry || !dir) {
446 printf("%s: Error, no memory for malloc!\n", __func__);
451 dir->i_sb = ubifs_sb;
452 file->f_path.dentry = dentry;
453 file->f_path.dentry->d_parent = dentry;
454 file->f_path.dentry->d_inode = dir;
455 file->f_path.dentry->d_inode->i_ino = inum;
457 file->private_data = NULL;
458 ubifs_printdir(file, dirent);
469 ubi_close_volume(c->ubi);
479 static inline void *kmap(struct page *page)
484 static int read_block(struct inode *inode, void *addr, unsigned int block,
485 struct ubifs_data_node *dn)
487 struct ubifs_info *c = inode->i_sb->s_fs_info;
488 int err, len, out_len;
492 data_key_init(c, &key, inode->i_ino, block);
493 err = ubifs_tnc_lookup(c, &key, dn);
496 /* Not found, so it must be a hole */
497 memset(addr, 0, UBIFS_BLOCK_SIZE);
501 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
503 len = le32_to_cpu(dn->size);
504 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
507 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
508 out_len = UBIFS_BLOCK_SIZE;
509 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
510 le16_to_cpu(dn->compr_type));
511 if (err || len != out_len)
515 * Data length can be less than a full block, even for blocks that are
516 * not the last in the file (e.g., as a result of making a hole and
517 * appending data). Ensure that the remainder is zeroed out.
519 if (len < UBIFS_BLOCK_SIZE)
520 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
525 ubifs_err("bad data node (block %u, inode %lu)",
526 block, inode->i_ino);
527 dbg_dump_node(c, dn);
531 static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
535 unsigned int block, beyond;
536 struct ubifs_data_node *dn;
537 loff_t i_size = inode->i_size;
539 dbg_gen("ino %lu, pg %lu, i_size %lld",
540 inode->i_ino, page->index, i_size);
544 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
545 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
546 if (block >= beyond) {
547 /* Reading beyond inode */
548 memset(addr, 0, PAGE_CACHE_SIZE);
552 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
562 if (block >= beyond) {
563 /* Reading beyond inode */
565 memset(addr, 0, UBIFS_BLOCK_SIZE);
567 ret = read_block(inode, addr, block, dn);
572 } else if (block + 1 == beyond) {
573 int dlen = le32_to_cpu(dn->size);
574 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
576 if (ilen && ilen < dlen)
577 memset(addr + ilen, 0, dlen - ilen);
580 if (++i >= UBIFS_BLOCKS_PER_PAGE)
583 addr += UBIFS_BLOCK_SIZE;
586 if (err == -ENOENT) {
587 /* Not found, so it must be a hole */
591 ubifs_err("cannot read page %lu of inode %lu, error %d",
592 page->index, inode->i_ino, err);
606 int ubifs_load(char *filename, u32 addr, u32 size)
608 struct ubifs_info *c = ubifs_sb->s_fs_info;
616 struct ubifs_inode *ui;
618 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
619 inum = ubifs_findfile(ubifs_sb, filename);
628 inode = ubifs_iget(ubifs_sb, inum);
630 printf("%s: Error reading inode %ld!\n", __func__, inum);
631 err = PTR_ERR(inode);
636 * Check for symbolic link
638 ui = ubifs_inode(inode);
639 if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
640 memcpy(link_name, ui->data, ui->data_len);
641 printf("%s is linked to %s!\n", filename, link_name);
645 * Now we have the "real" filename, call ubifs_load()
646 * again (recursive call) to load this file instead
648 return ubifs_load(link_name, addr, size);
652 * If no size was specified or if size bigger than filesize
653 * set size to filesize
655 if ((size == 0) || (size > inode->i_size))
656 size = inode->i_size;
658 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
659 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
660 filename, addr, size, size);
662 page.addr = (void *)addr;
665 for (i = 0; i < count; i++) {
666 err = do_readpage(c, inode, &page);
670 page.addr += PAGE_SIZE;
675 printf("Error reading file '%s'\n", filename);
682 ubi_close_volume(c->ubi);