1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * (C) Copyright 2008-2010
10 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 #include <asm/global_data.h>
23 #include <dm/devres.h>
24 #include <u-boot/zlib.h>
26 #include <linux/compat.h>
27 #include <linux/err.h>
28 #include <linux/lzo.h>
30 DECLARE_GLOBAL_DATA_PTR;
35 * We need a wrapper for zunzip() because the parameters are
36 * incompatible with the lzo decompressor.
38 static int gzip_decompress(const unsigned char *in, size_t in_len,
39 unsigned char *out, size_t *out_len)
41 return zunzip(out, *out_len, (unsigned char *)in,
42 (unsigned long *)out_len, 0, 0);
45 /* Fake description object for the "none" compressor */
46 static struct ubifs_compressor none_compr = {
47 .compr_type = UBIFS_COMPR_NONE,
53 static struct ubifs_compressor lzo_compr = {
54 .compr_type = UBIFS_COMPR_LZO,
56 .comp_mutex = &lzo_mutex,
60 .decompress = lzo1x_decompress_safe,
63 static struct ubifs_compressor zlib_compr = {
64 .compr_type = UBIFS_COMPR_ZLIB,
66 .comp_mutex = &deflate_mutex,
67 .decomp_mutex = &inflate_mutex,
70 .capi_name = "deflate",
71 .decompress = gzip_decompress,
74 /* All UBIFS compressors */
75 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
84 static inline struct crypto_comp
85 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
87 struct ubifs_compressor *comp;
88 struct crypto_comp *ptr;
91 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
92 while (i < UBIFS_COMPR_TYPES_CNT) {
93 comp = ubifs_compressors[i];
98 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
104 if (i >= UBIFS_COMPR_TYPES_CNT) {
105 dbg_gen("invalid compression type %s", alg_name);
112 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
113 const u8 *src, unsigned int slen, u8 *dst,
116 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
118 size_t tmp_len = *dlen;
120 if (compr->compr_type == UBIFS_COMPR_NONE) {
121 memcpy(dst, src, slen);
126 err = compr->decompress(src, slen, dst, &tmp_len);
128 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
129 "error %d", slen, compr->name, err);
137 /* from shrinker.c */
139 /* Global clean znode counter (for all mounted UBIFS instances) */
140 atomic_long_t ubifs_clean_zn_cnt;
145 * ubifs_decompress - decompress data.
146 * @in_buf: data to decompress
147 * @in_len: length of the data to decompress
148 * @out_buf: output buffer where decompressed data should
149 * @out_len: output length is returned here
150 * @compr_type: type of compression
152 * This function decompresses data from buffer @in_buf into buffer @out_buf.
153 * The length of the uncompressed data is returned in @out_len. This functions
154 * returns %0 on success or a negative error code on failure.
156 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
157 int in_len, void *out_buf, int *out_len, int compr_type)
160 struct ubifs_compressor *compr;
162 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
163 ubifs_err(c, "invalid compression type %d", compr_type);
167 compr = ubifs_compressors[compr_type];
169 if (unlikely(!compr->capi_name)) {
170 ubifs_err(c, "%s compression is not compiled in", compr->name);
174 if (compr_type == UBIFS_COMPR_NONE) {
175 memcpy(out_buf, in_buf, in_len);
180 if (compr->decomp_mutex)
181 mutex_lock(compr->decomp_mutex);
182 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
183 (unsigned int *)out_len);
184 if (compr->decomp_mutex)
185 mutex_unlock(compr->decomp_mutex);
187 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
188 " error %d", in_len, compr->name, err);
194 * compr_init - initialize a compressor.
195 * @compr: compressor description object
197 * This function initializes the requested compressor and returns zero in case
198 * of success or a negative error code in case of failure.
200 static int __init compr_init(struct ubifs_compressor *compr)
202 ubifs_compressors[compr->compr_type] = compr;
204 if (compr->capi_name) {
205 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
206 if (IS_ERR(compr->cc)) {
207 dbg_gen("cannot initialize compressor %s,"
208 " error %ld", compr->name,
210 return PTR_ERR(compr->cc);
218 * ubifs_compressors_init - initialize UBIFS compressors.
220 * This function initializes the compressor which were compiled in. Returns
221 * zero in case of success and a negative error code in case of failure.
223 int __init ubifs_compressors_init(void)
227 err = compr_init(&lzo_compr);
231 err = compr_init(&zlib_compr);
235 err = compr_init(&none_compr);
246 static int filldir(struct ubifs_info *c, const char *name, int namlen,
247 u64 ino, unsigned int d_type)
253 case UBIFS_ITYPE_REG:
256 case UBIFS_ITYPE_DIR:
259 case UBIFS_ITYPE_LNK:
267 inode = ubifs_iget(c->vfs_sb, ino);
269 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
270 __func__, ino, inode);
273 ctime_r((time_t *)&inode->i_mtime, filetime);
274 printf("%9lld %24.24s ", inode->i_size, filetime);
279 printf("%s\n", name);
284 static int ubifs_printdir(struct file *file, void *dirent)
289 struct ubifs_dent_node *dent;
290 struct inode *dir = file->f_path.dentry->d_inode;
291 struct ubifs_info *c = dir->i_sb->s_fs_info;
293 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
295 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
297 * The directory was seek'ed to a senseless position or there
298 * are no more entries.
302 if (file->f_pos == 1) {
303 /* Find the first entry in TNC and save it */
304 lowest_dent_key(c, &key, dir->i_ino);
306 dent = ubifs_tnc_next_ent(c, &key, &nm);
312 file->f_pos = key_hash_flash(c, &dent->key);
313 file->private_data = dent;
316 dent = file->private_data;
319 * The directory was seek'ed to and is now readdir'ed.
320 * Find the entry corresponding to @file->f_pos or the
323 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
325 dent = ubifs_tnc_next_ent(c, &key, &nm);
330 file->f_pos = key_hash_flash(c, &dent->key);
331 file->private_data = dent;
335 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
336 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
337 key_hash_flash(c, &dent->key));
339 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
342 nm.len = le16_to_cpu(dent->nlen);
343 over = filldir(c, (char *)dent->name, nm.len,
344 le64_to_cpu(dent->inum), dent->type);
348 /* Switch to the next entry */
349 key_read(c, &dent->key, &key);
350 nm.name = (char *)dent->name;
351 dent = ubifs_tnc_next_ent(c, &key, &nm);
357 kfree(file->private_data);
358 file->f_pos = key_hash_flash(c, &dent->key);
359 file->private_data = dent;
364 if (err != -ENOENT) {
365 ubifs_err(c, "cannot find next direntry, error %d", err);
369 kfree(file->private_data);
370 file->private_data = NULL;
375 static int ubifs_finddir(struct super_block *sb, char *dirname,
376 unsigned long root_inum, unsigned long *inum)
381 struct ubifs_dent_node *dent;
382 struct ubifs_info *c;
384 struct dentry *dentry;
388 file = kzalloc(sizeof(struct file), 0);
389 dentry = kzalloc(sizeof(struct dentry), 0);
390 dir = kzalloc(sizeof(struct inode), 0);
391 if (!file || !dentry || !dir) {
392 printf("%s: Error, no memory for malloc!\n", __func__);
398 file->f_path.dentry = dentry;
399 file->f_path.dentry->d_parent = dentry;
400 file->f_path.dentry->d_inode = dir;
401 file->f_path.dentry->d_inode->i_ino = root_inum;
404 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
406 /* Find the first entry in TNC and save it */
407 lowest_dent_key(c, &key, dir->i_ino);
409 dent = ubifs_tnc_next_ent(c, &key, &nm);
415 file->f_pos = key_hash_flash(c, &dent->key);
416 file->private_data = dent;
419 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
420 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
421 key_hash_flash(c, &dent->key));
423 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
426 nm.len = le16_to_cpu(dent->nlen);
427 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
428 (strlen(dirname) == nm.len)) {
429 *inum = le64_to_cpu(dent->inum);
434 /* Switch to the next entry */
435 key_read(c, &dent->key, &key);
436 nm.name = (char *)dent->name;
437 dent = ubifs_tnc_next_ent(c, &key, &nm);
443 kfree(file->private_data);
444 file->f_pos = key_hash_flash(c, &dent->key);
445 file->private_data = dent;
451 dbg_gen("cannot find next direntry, error %d", err);
454 kfree(file->private_data);
462 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
467 char symlinkpath[128];
469 unsigned long root_inum = 1;
471 int symlink_count = 0; /* Don't allow symlink recursion */
474 strcpy(fpath, filename);
476 /* Remove all leading slashes */
481 * Handle root-direcoty ('/')
484 if (!name || *name == '\0')
489 struct ubifs_inode *ui;
491 /* Extract the actual part from the pathname. */
492 next = strchr(name, '/');
494 /* Remove all leading slashes. */
499 ret = ubifs_finddir(sb, name, root_inum, &inum);
502 inode = ubifs_iget(sb, inum);
506 ui = ubifs_inode(inode);
508 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
511 /* We have some sort of symlink recursion, bail out */
512 if (symlink_count++ > 8) {
513 printf("Symlink recursion, aborting\n");
516 memcpy(link_name, ui->data, ui->data_len);
517 link_name[ui->data_len] = '\0';
519 if (link_name[0] == '/') {
520 /* Absolute path, redo everything without
521 * the leading slash */
522 next = name = link_name + 1;
526 /* Relative to cur dir */
527 sprintf(buf, "%s/%s",
528 link_name, next == NULL ? "" : next);
529 memcpy(symlinkpath, buf, sizeof(buf));
530 next = name = symlinkpath;
535 * Check if directory with this name exists
538 /* Found the node! */
539 if (!next || *next == '\0')
549 int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
552 debug("UBIFS cannot be used with normal block devices\n");
557 * Should never happen since blk_get_device_part_str() already checks
558 * this, but better safe then sorry.
560 if (!ubifs_is_mounted()) {
561 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
568 int ubifs_ls(const char *filename)
570 struct ubifs_info *c = ubifs_sb->s_fs_info;
572 struct dentry *dentry;
578 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
579 inum = ubifs_findfile(ubifs_sb, (char *)filename);
585 file = kzalloc(sizeof(struct file), 0);
586 dentry = kzalloc(sizeof(struct dentry), 0);
587 dir = kzalloc(sizeof(struct inode), 0);
588 if (!file || !dentry || !dir) {
589 printf("%s: Error, no memory for malloc!\n", __func__);
594 dir->i_sb = ubifs_sb;
595 file->f_path.dentry = dentry;
596 file->f_path.dentry->d_parent = dentry;
597 file->f_path.dentry->d_inode = dir;
598 file->f_path.dentry->d_inode->i_ino = inum;
600 file->private_data = NULL;
601 ubifs_printdir(file, dirent);
612 ubi_close_volume(c->ubi);
616 int ubifs_exists(const char *filename)
618 struct ubifs_info *c = ubifs_sb->s_fs_info;
621 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
622 inum = ubifs_findfile(ubifs_sb, (char *)filename);
623 ubi_close_volume(c->ubi);
628 int ubifs_size(const char *filename, loff_t *size)
630 struct ubifs_info *c = ubifs_sb->s_fs_info;
635 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
637 inum = ubifs_findfile(ubifs_sb, (char *)filename);
643 inode = ubifs_iget(ubifs_sb, inum);
645 printf("%s: Error reading inode %ld!\n", __func__, inum);
646 err = PTR_ERR(inode);
650 *size = inode->i_size;
654 ubi_close_volume(c->ubi);
664 static inline void *kmap(struct page *page)
669 static int read_block(struct inode *inode, void *addr, unsigned int block,
670 struct ubifs_data_node *dn)
672 struct ubifs_info *c = inode->i_sb->s_fs_info;
673 int err, len, out_len;
677 data_key_init(c, &key, inode->i_ino, block);
678 err = ubifs_tnc_lookup(c, &key, dn);
681 /* Not found, so it must be a hole */
682 memset(addr, 0, UBIFS_BLOCK_SIZE);
686 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
688 len = le32_to_cpu(dn->size);
689 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
692 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
693 out_len = UBIFS_BLOCK_SIZE;
694 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
695 le16_to_cpu(dn->compr_type));
696 if (err || len != out_len)
700 * Data length can be less than a full block, even for blocks that are
701 * not the last in the file (e.g., as a result of making a hole and
702 * appending data). Ensure that the remainder is zeroed out.
704 if (len < UBIFS_BLOCK_SIZE)
705 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
710 ubifs_err(c, "bad data node (block %u, inode %lu)",
711 block, inode->i_ino);
712 ubifs_dump_node(c, dn);
716 static int do_readpage(struct ubifs_info *c, struct inode *inode,
717 struct page *page, int last_block_size)
721 unsigned int block, beyond;
722 struct ubifs_data_node *dn;
723 loff_t i_size = inode->i_size;
725 dbg_gen("ino %lu, pg %lu, i_size %lld",
726 inode->i_ino, page->index, i_size);
730 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
731 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
732 if (block >= beyond) {
733 /* Reading beyond inode */
734 memset(addr, 0, PAGE_CACHE_SIZE);
738 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
746 if (block >= beyond) {
747 /* Reading beyond inode */
749 memset(addr, 0, UBIFS_BLOCK_SIZE);
752 * Reading last block? Make sure to not write beyond
753 * the requested size in the destination buffer.
755 if (((block + 1) == beyond) || last_block_size) {
760 * We need to buffer the data locally for the
761 * last block. This is to not pad the
762 * destination area to a multiple of
765 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
767 printf("%s: Error, malloc fails!\n",
773 /* Read block-size into temp buffer */
774 ret = read_block(inode, buff, block, dn);
777 if (err != -ENOENT) {
784 dlen = last_block_size;
786 dlen = UBIFS_BLOCK_SIZE;
788 dlen = le32_to_cpu(dn->size);
790 /* Now copy required size back to dest */
791 memcpy(addr, buff, dlen);
795 ret = read_block(inode, addr, block, dn);
803 if (++i >= UBIFS_BLOCKS_PER_PAGE)
806 addr += UBIFS_BLOCK_SIZE;
809 if (err == -ENOENT) {
810 /* Not found, so it must be a hole */
814 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
815 page->index, inode->i_ino, err);
829 int ubifs_read(const char *filename, void *buf, loff_t offset,
830 loff_t size, loff_t *actread)
832 struct ubifs_info *c = ubifs_sb->s_fs_info;
839 int last_block_size = 0;
843 if (offset & (PAGE_SIZE - 1)) {
844 printf("ubifs: Error offset must be a multiple of %d\n",
849 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
850 /* ubifs_findfile will resolve symlinks, so we know that we get
851 * the real file here */
852 inum = ubifs_findfile(ubifs_sb, (char *)filename);
861 inode = ubifs_iget(ubifs_sb, inum);
863 printf("%s: Error reading inode %ld!\n", __func__, inum);
864 err = PTR_ERR(inode);
868 if (offset > inode->i_size) {
869 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
876 * If no size was specified or if size bigger than filesize
877 * set size to filesize
879 if ((size == 0) || (size > (inode->i_size - offset)))
880 size = inode->i_size - offset;
882 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
885 page.index = offset / PAGE_SIZE;
887 for (i = 0; i < count; i++) {
889 * Make sure to not read beyond the requested size
891 if (((i + 1) == count) && (size < inode->i_size))
892 last_block_size = size - (i * PAGE_SIZE);
894 err = do_readpage(c, inode, &page, last_block_size);
898 page.addr += PAGE_SIZE;
903 printf("Error reading file '%s'\n", filename);
904 *actread = i * PAGE_SIZE;
913 ubi_close_volume(c->ubi);
917 void ubifs_close(void)
921 /* Compat wrappers for common/cmd_ubifs.c */
922 int ubifs_load(char *filename, unsigned long addr, u32 size)
927 printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr);
929 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
931 env_set_hex("filesize", actread);
938 void uboot_ubifs_umount(void)
941 printf("Unmounting UBIFS volume %s!\n",
942 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
943 ubifs_umount(ubifs_sb->s_fs_info);