2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * (C) Copyright 2008-2010
9 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 * SPDX-License-Identifier: GPL-2.0
18 #include <u-boot/zlib.h>
20 #include <linux/err.h>
21 #include <linux/lzo.h>
23 DECLARE_GLOBAL_DATA_PTR;
28 * We need a wrapper for zunzip() because the parameters are
29 * incompatible with the lzo decompressor.
31 static int gzip_decompress(const unsigned char *in, size_t in_len,
32 unsigned char *out, size_t *out_len)
34 return zunzip(out, *out_len, (unsigned char *)in,
35 (unsigned long *)out_len, 0, 0);
38 /* Fake description object for the "none" compressor */
39 static struct ubifs_compressor none_compr = {
40 .compr_type = UBIFS_COMPR_NONE,
46 static struct ubifs_compressor lzo_compr = {
47 .compr_type = UBIFS_COMPR_LZO,
49 .comp_mutex = &lzo_mutex,
53 .decompress = lzo1x_decompress_safe,
56 static struct ubifs_compressor zlib_compr = {
57 .compr_type = UBIFS_COMPR_ZLIB,
59 .comp_mutex = &deflate_mutex,
60 .decomp_mutex = &inflate_mutex,
63 .capi_name = "deflate",
64 .decompress = gzip_decompress,
67 /* All UBIFS compressors */
68 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
75 * kmemdup - duplicate region of memory
77 * @src: memory region to duplicate
78 * @len: memory region length
79 * @gfp: GFP mask to use
81 void *kmemdup(const void *src, size_t len, gfp_t gfp)
85 p = kmalloc(len, gfp);
95 static inline struct crypto_comp
96 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
98 struct ubifs_compressor *comp;
99 struct crypto_comp *ptr;
102 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
103 while (i < UBIFS_COMPR_TYPES_CNT) {
104 comp = ubifs_compressors[i];
109 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
115 if (i >= UBIFS_COMPR_TYPES_CNT) {
116 dbg_gen("invalid compression type %s", alg_name);
123 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
124 const u8 *src, unsigned int slen, u8 *dst,
127 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
130 if (compr->compr_type == UBIFS_COMPR_NONE) {
131 memcpy(dst, src, slen);
136 err = compr->decompress(src, slen, dst, (size_t *)dlen);
138 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
139 "error %d", slen, compr->name, err);
146 /* from shrinker.c */
148 /* Global clean znode counter (for all mounted UBIFS instances) */
149 atomic_long_t ubifs_clean_zn_cnt;
154 * ubifs_decompress - decompress data.
155 * @in_buf: data to decompress
156 * @in_len: length of the data to decompress
157 * @out_buf: output buffer where decompressed data should
158 * @out_len: output length is returned here
159 * @compr_type: type of compression
161 * This function decompresses data from buffer @in_buf into buffer @out_buf.
162 * The length of the uncompressed data is returned in @out_len. This functions
163 * returns %0 on success or a negative error code on failure.
165 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
166 int in_len, void *out_buf, int *out_len, int compr_type)
169 struct ubifs_compressor *compr;
171 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
172 ubifs_err(c, "invalid compression type %d", compr_type);
176 compr = ubifs_compressors[compr_type];
178 if (unlikely(!compr->capi_name)) {
179 ubifs_err(c, "%s compression is not compiled in", compr->name);
183 if (compr_type == UBIFS_COMPR_NONE) {
184 memcpy(out_buf, in_buf, in_len);
189 if (compr->decomp_mutex)
190 mutex_lock(compr->decomp_mutex);
191 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
192 (unsigned int *)out_len);
193 if (compr->decomp_mutex)
194 mutex_unlock(compr->decomp_mutex);
196 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
197 " error %d", in_len, compr->name, err);
203 * compr_init - initialize a compressor.
204 * @compr: compressor description object
206 * This function initializes the requested compressor and returns zero in case
207 * of success or a negative error code in case of failure.
209 static int __init compr_init(struct ubifs_compressor *compr)
211 ubifs_compressors[compr->compr_type] = compr;
213 #ifdef CONFIG_NEEDS_MANUAL_RELOC
214 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
215 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
216 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
219 if (compr->capi_name) {
220 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
221 if (IS_ERR(compr->cc)) {
222 dbg_gen("cannot initialize compressor %s,"
223 " error %ld", compr->name,
225 return PTR_ERR(compr->cc);
233 * ubifs_compressors_init - initialize UBIFS compressors.
235 * This function initializes the compressor which were compiled in. Returns
236 * zero in case of success and a negative error code in case of failure.
238 int __init ubifs_compressors_init(void)
242 err = compr_init(&lzo_compr);
246 err = compr_init(&zlib_compr);
250 err = compr_init(&none_compr);
261 static int filldir(struct ubifs_info *c, const char *name, int namlen,
262 u64 ino, unsigned int d_type)
268 case UBIFS_ITYPE_REG:
271 case UBIFS_ITYPE_DIR:
274 case UBIFS_ITYPE_LNK:
282 inode = ubifs_iget(c->vfs_sb, ino);
284 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
285 __func__, ino, inode);
288 ctime_r((time_t *)&inode->i_mtime, filetime);
289 printf("%9lld %24.24s ", inode->i_size, filetime);
294 printf("%s\n", name);
299 static int ubifs_printdir(struct file *file, void *dirent)
304 struct ubifs_dent_node *dent;
305 struct inode *dir = file->f_path.dentry->d_inode;
306 struct ubifs_info *c = dir->i_sb->s_fs_info;
308 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
310 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
312 * The directory was seek'ed to a senseless position or there
313 * are no more entries.
317 if (file->f_pos == 1) {
318 /* Find the first entry in TNC and save it */
319 lowest_dent_key(c, &key, dir->i_ino);
321 dent = ubifs_tnc_next_ent(c, &key, &nm);
327 file->f_pos = key_hash_flash(c, &dent->key);
328 file->private_data = dent;
331 dent = file->private_data;
334 * The directory was seek'ed to and is now readdir'ed.
335 * Find the entry corresponding to @file->f_pos or the
338 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
340 dent = ubifs_tnc_next_ent(c, &key, &nm);
345 file->f_pos = key_hash_flash(c, &dent->key);
346 file->private_data = dent;
350 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
351 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
352 key_hash_flash(c, &dent->key));
353 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
355 nm.len = le16_to_cpu(dent->nlen);
356 over = filldir(c, (char *)dent->name, nm.len,
357 le64_to_cpu(dent->inum), dent->type);
361 /* Switch to the next entry */
362 key_read(c, &dent->key, &key);
363 nm.name = (char *)dent->name;
364 dent = ubifs_tnc_next_ent(c, &key, &nm);
370 kfree(file->private_data);
371 file->f_pos = key_hash_flash(c, &dent->key);
372 file->private_data = dent;
377 if (err != -ENOENT) {
378 ubifs_err(c, "cannot find next direntry, error %d", err);
382 kfree(file->private_data);
383 file->private_data = NULL;
388 static int ubifs_finddir(struct super_block *sb, char *dirname,
389 unsigned long root_inum, unsigned long *inum)
394 struct ubifs_dent_node *dent;
395 struct ubifs_info *c;
397 struct dentry *dentry;
401 file = kzalloc(sizeof(struct file), 0);
402 dentry = kzalloc(sizeof(struct dentry), 0);
403 dir = kzalloc(sizeof(struct inode), 0);
404 if (!file || !dentry || !dir) {
405 printf("%s: Error, no memory for malloc!\n", __func__);
411 file->f_path.dentry = dentry;
412 file->f_path.dentry->d_parent = dentry;
413 file->f_path.dentry->d_inode = dir;
414 file->f_path.dentry->d_inode->i_ino = root_inum;
417 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
419 /* Find the first entry in TNC and save it */
420 lowest_dent_key(c, &key, dir->i_ino);
422 dent = ubifs_tnc_next_ent(c, &key, &nm);
428 file->f_pos = key_hash_flash(c, &dent->key);
429 file->private_data = dent;
432 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
433 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
434 key_hash_flash(c, &dent->key));
435 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
437 nm.len = le16_to_cpu(dent->nlen);
438 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
439 (strlen(dirname) == nm.len)) {
440 *inum = le64_to_cpu(dent->inum);
445 /* Switch to the next entry */
446 key_read(c, &dent->key, &key);
447 nm.name = (char *)dent->name;
448 dent = ubifs_tnc_next_ent(c, &key, &nm);
454 kfree(file->private_data);
455 file->f_pos = key_hash_flash(c, &dent->key);
456 file->private_data = dent;
462 dbg_gen("cannot find next direntry, error %d", err);
465 kfree(file->private_data);
473 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
478 char symlinkpath[128];
480 unsigned long root_inum = 1;
482 int symlink_count = 0; /* Don't allow symlink recursion */
485 strcpy(fpath, filename);
487 /* Remove all leading slashes */
492 * Handle root-direcoty ('/')
495 if (!name || *name == '\0')
500 struct ubifs_inode *ui;
502 /* Extract the actual part from the pathname. */
503 next = strchr(name, '/');
505 /* Remove all leading slashes. */
510 ret = ubifs_finddir(sb, name, root_inum, &inum);
513 inode = ubifs_iget(sb, inum);
517 ui = ubifs_inode(inode);
519 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
522 /* We have some sort of symlink recursion, bail out */
523 if (symlink_count++ > 8) {
524 printf("Symlink recursion, aborting\n");
527 memcpy(link_name, ui->data, ui->data_len);
528 link_name[ui->data_len] = '\0';
530 if (link_name[0] == '/') {
531 /* Absolute path, redo everything without
532 * the leading slash */
533 next = name = link_name + 1;
537 /* Relative to cur dir */
538 sprintf(buf, "%s/%s",
539 link_name, next == NULL ? "" : next);
540 memcpy(symlinkpath, buf, sizeof(buf));
541 next = name = symlinkpath;
546 * Check if directory with this name exists
549 /* Found the node! */
550 if (!next || *next == '\0')
560 int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
563 debug("UBIFS cannot be used with normal block devices\n");
568 * Should never happen since blk_get_device_part_str() already checks
569 * this, but better safe then sorry.
571 if (!ubifs_is_mounted()) {
572 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
579 int ubifs_ls(const char *filename)
581 struct ubifs_info *c = ubifs_sb->s_fs_info;
583 struct dentry *dentry;
589 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
590 inum = ubifs_findfile(ubifs_sb, (char *)filename);
596 file = kzalloc(sizeof(struct file), 0);
597 dentry = kzalloc(sizeof(struct dentry), 0);
598 dir = kzalloc(sizeof(struct inode), 0);
599 if (!file || !dentry || !dir) {
600 printf("%s: Error, no memory for malloc!\n", __func__);
605 dir->i_sb = ubifs_sb;
606 file->f_path.dentry = dentry;
607 file->f_path.dentry->d_parent = dentry;
608 file->f_path.dentry->d_inode = dir;
609 file->f_path.dentry->d_inode->i_ino = inum;
611 file->private_data = NULL;
612 ubifs_printdir(file, dirent);
623 ubi_close_volume(c->ubi);
627 int ubifs_exists(const char *filename)
629 struct ubifs_info *c = ubifs_sb->s_fs_info;
632 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
633 inum = ubifs_findfile(ubifs_sb, (char *)filename);
634 ubi_close_volume(c->ubi);
639 int ubifs_size(const char *filename, loff_t *size)
641 struct ubifs_info *c = ubifs_sb->s_fs_info;
646 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
648 inum = ubifs_findfile(ubifs_sb, (char *)filename);
654 inode = ubifs_iget(ubifs_sb, inum);
656 printf("%s: Error reading inode %ld!\n", __func__, inum);
657 err = PTR_ERR(inode);
661 *size = inode->i_size;
665 ubi_close_volume(c->ubi);
675 static inline void *kmap(struct page *page)
680 static int read_block(struct inode *inode, void *addr, unsigned int block,
681 struct ubifs_data_node *dn)
683 struct ubifs_info *c = inode->i_sb->s_fs_info;
684 int err, len, out_len;
688 data_key_init(c, &key, inode->i_ino, block);
689 err = ubifs_tnc_lookup(c, &key, dn);
692 /* Not found, so it must be a hole */
693 memset(addr, 0, UBIFS_BLOCK_SIZE);
697 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
699 len = le32_to_cpu(dn->size);
700 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
703 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
704 out_len = UBIFS_BLOCK_SIZE;
705 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
706 le16_to_cpu(dn->compr_type));
707 if (err || len != out_len)
711 * Data length can be less than a full block, even for blocks that are
712 * not the last in the file (e.g., as a result of making a hole and
713 * appending data). Ensure that the remainder is zeroed out.
715 if (len < UBIFS_BLOCK_SIZE)
716 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
721 ubifs_err(c, "bad data node (block %u, inode %lu)",
722 block, inode->i_ino);
723 ubifs_dump_node(c, dn);
727 static int do_readpage(struct ubifs_info *c, struct inode *inode,
728 struct page *page, int last_block_size)
732 unsigned int block, beyond;
733 struct ubifs_data_node *dn;
734 loff_t i_size = inode->i_size;
736 dbg_gen("ino %lu, pg %lu, i_size %lld",
737 inode->i_ino, page->index, i_size);
741 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
742 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
743 if (block >= beyond) {
744 /* Reading beyond inode */
745 memset(addr, 0, PAGE_CACHE_SIZE);
749 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
757 if (block >= beyond) {
758 /* Reading beyond inode */
760 memset(addr, 0, UBIFS_BLOCK_SIZE);
763 * Reading last block? Make sure to not write beyond
764 * the requested size in the destination buffer.
766 if (((block + 1) == beyond) || last_block_size) {
771 * We need to buffer the data locally for the
772 * last block. This is to not pad the
773 * destination area to a multiple of
776 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
778 printf("%s: Error, malloc fails!\n",
784 /* Read block-size into temp buffer */
785 ret = read_block(inode, buff, block, dn);
788 if (err != -ENOENT) {
795 dlen = last_block_size;
797 dlen = le32_to_cpu(dn->size);
799 /* Now copy required size back to dest */
800 memcpy(addr, buff, dlen);
804 ret = read_block(inode, addr, block, dn);
812 if (++i >= UBIFS_BLOCKS_PER_PAGE)
815 addr += UBIFS_BLOCK_SIZE;
818 if (err == -ENOENT) {
819 /* Not found, so it must be a hole */
823 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
824 page->index, inode->i_ino, err);
838 int ubifs_read(const char *filename, void *buf, loff_t offset,
839 loff_t size, loff_t *actread)
841 struct ubifs_info *c = ubifs_sb->s_fs_info;
848 int last_block_size = 0;
852 if (offset & (PAGE_SIZE - 1)) {
853 printf("ubifs: Error offset must be a multiple of %d\n",
858 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
859 /* ubifs_findfile will resolve symlinks, so we know that we get
860 * the real file here */
861 inum = ubifs_findfile(ubifs_sb, (char *)filename);
870 inode = ubifs_iget(ubifs_sb, inum);
872 printf("%s: Error reading inode %ld!\n", __func__, inum);
873 err = PTR_ERR(inode);
877 if (offset > inode->i_size) {
878 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
885 * If no size was specified or if size bigger than filesize
886 * set size to filesize
888 if ((size == 0) || (size > (inode->i_size - offset)))
889 size = inode->i_size - offset;
891 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
894 page.index = offset / PAGE_SIZE;
896 for (i = 0; i < count; i++) {
898 * Make sure to not read beyond the requested size
900 if (((i + 1) == count) && (size < inode->i_size))
901 last_block_size = size - (i * PAGE_SIZE);
903 err = do_readpage(c, inode, &page, last_block_size);
907 page.addr += PAGE_SIZE;
912 printf("Error reading file '%s'\n", filename);
913 *actread = i * PAGE_SIZE;
922 ubi_close_volume(c->ubi);
926 void ubifs_close(void)
930 /* Compat wrappers for common/cmd_ubifs.c */
931 int ubifs_load(char *filename, u32 addr, u32 size)
936 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
938 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
940 env_set_hex("filesize", actread);
947 void uboot_ubifs_umount(void)
950 printf("Unmounting UBIFS volume %s!\n",
951 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
952 ubifs_umount(ubifs_sb->s_fs_info);