2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * (C) Copyright 2008-2010
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 (Битюцкий Артём)
29 #include <u-boot/zlib.h>
31 #include <linux/err.h>
32 #include <linux/lzo.h>
34 DECLARE_GLOBAL_DATA_PTR
;
39 * We need a wrapper for zunzip() because the parameters are
40 * incompatible with the lzo decompressor.
42 static int gzip_decompress(const unsigned char *in
, size_t in_len
,
43 unsigned char *out
, size_t *out_len
)
45 return zunzip(out
, *out_len
, (unsigned char *)in
,
46 (unsigned long *)out_len
, 0, 0);
49 /* Fake description object for the "none" compressor */
50 static struct ubifs_compressor none_compr
= {
51 .compr_type
= UBIFS_COMPR_NONE
,
57 static struct ubifs_compressor lzo_compr
= {
58 .compr_type
= UBIFS_COMPR_LZO
,
60 .comp_mutex
= &lzo_mutex
,
64 .decompress
= lzo1x_decompress_safe
,
67 static struct ubifs_compressor zlib_compr
= {
68 .compr_type
= UBIFS_COMPR_ZLIB
,
70 .comp_mutex
= &deflate_mutex
,
71 .decomp_mutex
= &inflate_mutex
,
74 .capi_name
= "deflate",
75 .decompress
= gzip_decompress
,
78 /* All UBIFS compressors */
79 struct ubifs_compressor
*ubifs_compressors
[UBIFS_COMPR_TYPES_CNT
];
86 * kmemdup - duplicate region of memory
88 * @src: memory region to duplicate
89 * @len: memory region length
90 * @gfp: GFP mask to use
92 void *kmemdup(const void *src
, size_t len
, gfp_t gfp
)
96 p
= kmalloc(len
, gfp
);
106 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
109 struct ubifs_compressor
*comp
;
110 struct crypto_comp
*ptr
;
113 ptr
= malloc_cache_aligned(sizeof(struct crypto_comp
));
114 while (i
< UBIFS_COMPR_TYPES_CNT
) {
115 comp
= ubifs_compressors
[i
];
120 if (strncmp(alg_name
, comp
->capi_name
, strlen(alg_name
)) == 0) {
126 if (i
>= UBIFS_COMPR_TYPES_CNT
) {
127 ubifs_err("invalid compression type %s", alg_name
);
133 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
134 const u8
*src
, unsigned int slen
,
135 u8
*dst
, unsigned int *dlen
)
137 struct ubifs_compressor
*compr
= ubifs_compressors
[tfm
->compressor
];
140 if (compr
->compr_type
== UBIFS_COMPR_NONE
) {
141 memcpy(dst
, src
, slen
);
146 err
= compr
->decompress(src
, slen
, dst
, (size_t *)dlen
);
148 ubifs_err("cannot decompress %d bytes, compressor %s, "
149 "error %d", slen
, compr
->name
, err
);
156 /* from shrinker.c */
158 /* Global clean znode counter (for all mounted UBIFS instances) */
159 atomic_long_t ubifs_clean_zn_cnt
;
164 * ubifs_decompress - decompress data.
165 * @in_buf: data to decompress
166 * @in_len: length of the data to decompress
167 * @out_buf: output buffer where decompressed data should
168 * @out_len: output length is returned here
169 * @compr_type: type of compression
171 * This function decompresses data from buffer @in_buf into buffer @out_buf.
172 * The length of the uncompressed data is returned in @out_len. This functions
173 * returns %0 on success or a negative error code on failure.
175 int ubifs_decompress(const void *in_buf
, int in_len
, void *out_buf
,
176 int *out_len
, int compr_type
)
179 struct ubifs_compressor
*compr
;
181 if (unlikely(compr_type
< 0 || compr_type
>= UBIFS_COMPR_TYPES_CNT
)) {
182 ubifs_err("invalid compression type %d", compr_type
);
186 compr
= ubifs_compressors
[compr_type
];
188 if (unlikely(!compr
->capi_name
)) {
189 ubifs_err("%s compression is not compiled in", compr
->name
);
193 if (compr_type
== UBIFS_COMPR_NONE
) {
194 memcpy(out_buf
, in_buf
, in_len
);
199 if (compr
->decomp_mutex
)
200 mutex_lock(compr
->decomp_mutex
);
201 err
= crypto_comp_decompress(compr
->cc
, in_buf
, in_len
, out_buf
,
202 (unsigned int *)out_len
);
203 if (compr
->decomp_mutex
)
204 mutex_unlock(compr
->decomp_mutex
);
206 ubifs_err("cannot decompress %d bytes, compressor %s, error %d",
207 in_len
, compr
->name
, err
);
213 * compr_init - initialize a compressor.
214 * @compr: compressor description object
216 * This function initializes the requested compressor and returns zero in case
217 * of success or a negative error code in case of failure.
219 static int __init
compr_init(struct ubifs_compressor
*compr
)
221 ubifs_compressors
[compr
->compr_type
] = compr
;
223 #ifdef CONFIG_NEEDS_MANUAL_RELOC
224 ubifs_compressors
[compr
->compr_type
]->name
+= gd
->reloc_off
;
225 ubifs_compressors
[compr
->compr_type
]->capi_name
+= gd
->reloc_off
;
226 ubifs_compressors
[compr
->compr_type
]->decompress
+= gd
->reloc_off
;
229 if (compr
->capi_name
) {
230 compr
->cc
= crypto_alloc_comp(compr
->capi_name
, 0, 0);
231 if (IS_ERR(compr
->cc
)) {
232 ubifs_err("cannot initialize compressor %s, error %ld",
233 compr
->name
, PTR_ERR(compr
->cc
));
234 return PTR_ERR(compr
->cc
);
242 * ubifs_compressors_init - initialize UBIFS compressors.
244 * This function initializes the compressor which were compiled in. Returns
245 * zero in case of success and a negative error code in case of failure.
247 int __init
ubifs_compressors_init(void)
251 err
= compr_init(&lzo_compr
);
255 err
= compr_init(&zlib_compr
);
259 err
= compr_init(&none_compr
);
270 static int filldir(struct ubifs_info
*c
, const char *name
, int namlen
,
271 u64 ino
, unsigned int d_type
)
277 case UBIFS_ITYPE_REG
:
280 case UBIFS_ITYPE_DIR
:
283 case UBIFS_ITYPE_LNK
:
291 inode
= ubifs_iget(c
->vfs_sb
, ino
);
293 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
294 __func__
, ino
, inode
);
297 ctime_r((time_t *)&inode
->i_mtime
, filetime
);
298 printf("%9lld %24.24s ", inode
->i_size
, filetime
);
303 printf("%s\n", name
);
308 static int ubifs_printdir(struct file
*file
, void *dirent
)
313 struct ubifs_dent_node
*dent
;
314 struct inode
*dir
= file
->f_path
.dentry
->d_inode
;
315 struct ubifs_info
*c
= dir
->i_sb
->s_fs_info
;
317 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, file
->f_pos
);
319 if (file
->f_pos
> UBIFS_S_KEY_HASH_MASK
|| file
->f_pos
== 2)
321 * The directory was seek'ed to a senseless position or there
322 * are no more entries.
326 if (file
->f_pos
== 1) {
327 /* Find the first entry in TNC and save it */
328 lowest_dent_key(c
, &key
, dir
->i_ino
);
330 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
336 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
337 file
->private_data
= dent
;
340 dent
= file
->private_data
;
343 * The directory was seek'ed to and is now readdir'ed.
344 * Find the entry corresponding to @file->f_pos or the
347 dent_key_init_hash(c
, &key
, dir
->i_ino
, file
->f_pos
);
349 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
354 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
355 file
->private_data
= dent
;
359 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
360 dent
->name
, (unsigned long long)le64_to_cpu(dent
->inum
),
361 key_hash_flash(c
, &dent
->key
));
362 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) > ubifs_inode(dir
)->creat_sqnum
);
364 nm
.len
= le16_to_cpu(dent
->nlen
);
365 over
= filldir(c
, (char *)dent
->name
, nm
.len
,
366 le64_to_cpu(dent
->inum
), dent
->type
);
370 /* Switch to the next entry */
371 key_read(c
, &dent
->key
, &key
);
372 nm
.name
= (char *)dent
->name
;
373 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
379 kfree(file
->private_data
);
380 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
381 file
->private_data
= dent
;
386 if (err
!= -ENOENT
) {
387 ubifs_err("cannot find next direntry, error %d", err
);
391 kfree(file
->private_data
);
392 file
->private_data
= NULL
;
397 static int ubifs_finddir(struct super_block
*sb
, char *dirname
,
398 unsigned long root_inum
, unsigned long *inum
)
403 struct ubifs_dent_node
*dent
;
404 struct ubifs_info
*c
;
406 struct dentry
*dentry
;
410 file
= kzalloc(sizeof(struct file
), 0);
411 dentry
= kzalloc(sizeof(struct dentry
), 0);
412 dir
= kzalloc(sizeof(struct inode
), 0);
413 if (!file
|| !dentry
|| !dir
) {
414 printf("%s: Error, no memory for malloc!\n", __func__
);
420 file
->f_path
.dentry
= dentry
;
421 file
->f_path
.dentry
->d_parent
= dentry
;
422 file
->f_path
.dentry
->d_inode
= dir
;
423 file
->f_path
.dentry
->d_inode
->i_ino
= root_inum
;
426 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, file
->f_pos
);
428 /* Find the first entry in TNC and save it */
429 lowest_dent_key(c
, &key
, dir
->i_ino
);
431 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
437 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
438 file
->private_data
= dent
;
441 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
442 dent
->name
, (unsigned long long)le64_to_cpu(dent
->inum
),
443 key_hash_flash(c
, &dent
->key
));
444 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) > ubifs_inode(dir
)->creat_sqnum
);
446 nm
.len
= le16_to_cpu(dent
->nlen
);
447 if ((strncmp(dirname
, (char *)dent
->name
, nm
.len
) == 0) &&
448 (strlen(dirname
) == nm
.len
)) {
449 *inum
= le64_to_cpu(dent
->inum
);
454 /* Switch to the next entry */
455 key_read(c
, &dent
->key
, &key
);
456 nm
.name
= (char *)dent
->name
;
457 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
463 kfree(file
->private_data
);
464 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
465 file
->private_data
= dent
;
471 ubifs_err("cannot find next direntry, error %d", err
);
474 if (file
->private_data
)
475 kfree(file
->private_data
);
486 static unsigned long ubifs_findfile(struct super_block
*sb
, char *filename
)
491 char symlinkpath
[128];
493 unsigned long root_inum
= 1;
495 int symlink_count
= 0; /* Don't allow symlink recursion */
498 strcpy(fpath
, filename
);
500 /* Remove all leading slashes */
505 * Handle root-direcoty ('/')
508 if (!name
|| *name
== '\0')
513 struct ubifs_inode
*ui
;
515 /* Extract the actual part from the pathname. */
516 next
= strchr(name
, '/');
518 /* Remove all leading slashes. */
523 ret
= ubifs_finddir(sb
, name
, root_inum
, &inum
);
526 inode
= ubifs_iget(sb
, inum
);
530 ui
= ubifs_inode(inode
);
532 if ((inode
->i_mode
& S_IFMT
) == S_IFLNK
) {
535 /* We have some sort of symlink recursion, bail out */
536 if (symlink_count
++ > 8) {
537 printf("Symlink recursion, aborting\n");
540 memcpy(link_name
, ui
->data
, ui
->data_len
);
541 link_name
[ui
->data_len
] = '\0';
543 if (link_name
[0] == '/') {
544 /* Absolute path, redo everything without
545 * the leading slash */
546 next
= name
= link_name
+ 1;
550 /* Relative to cur dir */
551 sprintf(buf
, "%s/%s",
552 link_name
, next
== NULL
? "" : next
);
553 memcpy(symlinkpath
, buf
, sizeof(buf
));
554 next
= name
= symlinkpath
;
559 * Check if directory with this name exists
562 /* Found the node! */
563 if (!next
|| *next
== '\0')
573 int ubifs_ls(const char *filename
)
575 struct ubifs_info
*c
= ubifs_sb
->s_fs_info
;
577 struct dentry
*dentry
;
583 c
->ubi
= ubi_open_volume(c
->vi
.ubi_num
, c
->vi
.vol_id
, UBI_READONLY
);
584 inum
= ubifs_findfile(ubifs_sb
, (char *)filename
);
590 file
= kzalloc(sizeof(struct file
), 0);
591 dentry
= kzalloc(sizeof(struct dentry
), 0);
592 dir
= kzalloc(sizeof(struct inode
), 0);
593 if (!file
|| !dentry
|| !dir
) {
594 printf("%s: Error, no memory for malloc!\n", __func__
);
599 dir
->i_sb
= ubifs_sb
;
600 file
->f_path
.dentry
= dentry
;
601 file
->f_path
.dentry
->d_parent
= dentry
;
602 file
->f_path
.dentry
->d_inode
= dir
;
603 file
->f_path
.dentry
->d_inode
->i_ino
= inum
;
605 file
->private_data
= NULL
;
606 ubifs_printdir(file
, dirent
);
617 ubi_close_volume(c
->ubi
);
627 static inline void *kmap(struct page
*page
)
632 static int read_block(struct inode
*inode
, void *addr
, unsigned int block
,
633 struct ubifs_data_node
*dn
)
635 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
636 int err
, len
, out_len
;
640 data_key_init(c
, &key
, inode
->i_ino
, block
);
641 err
= ubifs_tnc_lookup(c
, &key
, dn
);
644 /* Not found, so it must be a hole */
645 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
649 ubifs_assert(le64_to_cpu(dn
->ch
.sqnum
) > ubifs_inode(inode
)->creat_sqnum
);
651 len
= le32_to_cpu(dn
->size
);
652 if (len
<= 0 || len
> UBIFS_BLOCK_SIZE
)
655 dlen
= le32_to_cpu(dn
->ch
.len
) - UBIFS_DATA_NODE_SZ
;
656 out_len
= UBIFS_BLOCK_SIZE
;
657 err
= ubifs_decompress(&dn
->data
, dlen
, addr
, &out_len
,
658 le16_to_cpu(dn
->compr_type
));
659 if (err
|| len
!= out_len
)
663 * Data length can be less than a full block, even for blocks that are
664 * not the last in the file (e.g., as a result of making a hole and
665 * appending data). Ensure that the remainder is zeroed out.
667 if (len
< UBIFS_BLOCK_SIZE
)
668 memset(addr
+ len
, 0, UBIFS_BLOCK_SIZE
- len
);
673 ubifs_err("bad data node (block %u, inode %lu)",
674 block
, inode
->i_ino
);
675 ubifs_dump_node(c
, dn
);
679 static int do_readpage(struct ubifs_info
*c
, struct inode
*inode
,
680 struct page
*page
, int last_block_size
)
684 unsigned int block
, beyond
;
685 struct ubifs_data_node
*dn
;
686 loff_t i_size
= inode
->i_size
;
688 dbg_gen("ino %lu, pg %lu, i_size %lld",
689 inode
->i_ino
, page
->index
, i_size
);
693 block
= page
->index
<< UBIFS_BLOCKS_PER_PAGE_SHIFT
;
694 beyond
= (i_size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
695 if (block
>= beyond
) {
696 /* Reading beyond inode */
697 memset(addr
, 0, PAGE_CACHE_SIZE
);
701 dn
= kmalloc(UBIFS_MAX_DATA_NODE_SZ
, GFP_NOFS
);
709 if (block
>= beyond
) {
710 /* Reading beyond inode */
712 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
715 * Reading last block? Make sure to not write beyond
716 * the requested size in the destination buffer.
718 if (((block
+ 1) == beyond
) || last_block_size
) {
723 * We need to buffer the data locally for the
724 * last block. This is to not pad the
725 * destination area to a multiple of
728 buff
= malloc_cache_aligned(UBIFS_BLOCK_SIZE
);
730 printf("%s: Error, malloc fails!\n",
736 /* Read block-size into temp buffer */
737 ret
= read_block(inode
, buff
, block
, dn
);
740 if (err
!= -ENOENT
) {
747 dlen
= last_block_size
;
749 dlen
= le32_to_cpu(dn
->size
);
751 /* Now copy required size back to dest */
752 memcpy(addr
, buff
, dlen
);
756 ret
= read_block(inode
, addr
, block
, dn
);
764 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
767 addr
+= UBIFS_BLOCK_SIZE
;
770 if (err
== -ENOENT
) {
771 /* Not found, so it must be a hole */
775 ubifs_err("cannot read page %lu of inode %lu, error %d",
776 page
->index
, inode
->i_ino
, err
);
790 int ubifs_read(const char *filename
, void *buf
, loff_t offset
,
791 loff_t size
, loff_t
*actread
)
793 struct ubifs_info
*c
= ubifs_sb
->s_fs_info
;
800 int last_block_size
= 0;
804 if (offset
& (PAGE_SIZE
- 1)) {
805 printf("ubifs: Error offset must be a multple of %d\n",
810 c
->ubi
= ubi_open_volume(c
->vi
.ubi_num
, c
->vi
.vol_id
, UBI_READONLY
);
811 /* ubifs_findfile will resolve symlinks, so we know that we get
812 * the real file here */
813 inum
= ubifs_findfile(ubifs_sb
, (char *)filename
);
822 inode
= ubifs_iget(ubifs_sb
, inum
);
824 printf("%s: Error reading inode %ld!\n", __func__
, inum
);
825 err
= PTR_ERR(inode
);
829 if (offset
> inode
->i_size
) {
830 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
837 * If no size was specified or if size bigger than filesize
838 * set size to filesize
840 if ((size
== 0) || (size
> (inode
->i_size
- offset
)))
841 size
= inode
->i_size
- offset
;
843 count
= (size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
846 page
.index
= offset
/ PAGE_SIZE
;
848 for (i
= 0; i
< count
; i
++) {
850 * Make sure to not read beyond the requested size
852 if (((i
+ 1) == count
) && (size
< inode
->i_size
))
853 last_block_size
= size
- (i
* PAGE_SIZE
);
855 err
= do_readpage(c
, inode
, &page
, last_block_size
);
859 page
.addr
+= PAGE_SIZE
;
864 printf("Error reading file '%s'\n", filename
);
865 *actread
= i
* PAGE_SIZE
;
874 ubi_close_volume(c
->ubi
);
878 /* Compat wrappers for common/cmd_ubifs.c */
879 int ubifs_load(char *filename
, u32 addr
, u32 size
)
884 printf("Loading file '%s' to addr 0x%08x...\n", filename
, addr
);
886 err
= ubifs_read(filename
, (void *)addr
, 0, size
, &actread
);
888 setenv_hex("filesize", actread
);
895 void uboot_ubifs_umount(void)
898 printf("Unmounting UBIFS volume %s!\n",
899 ((struct ubifs_info
*)(ubifs_sb
->s_fs_info
))->vi
.name
);
900 ubifs_umount(ubifs_sb
->s_fs_info
);