spi: zynqmp_gqspi: fix set_speed bug on multiple runs
[platform/kernel/u-boot.git] / fs / ubifs / ubifs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file is part of UBIFS.
4  *
5  * Copyright (C) 2006-2008 Nokia Corporation.
6  *
7  * (C) Copyright 2008-2010
8  * Stefan Roese, DENX Software Engineering, sr@denx.de.
9  *
10  * Authors: Artem Bityutskiy (Битюцкий Артём)
11  *          Adrian Hunter
12  */
13
14 #include <common.h>
15 #include <env.h>
16 #include <gzip.h>
17 #include <log.h>
18 #include <malloc.h>
19 #include <memalign.h>
20 #include "ubifs.h"
21 #include <part.h>
22 #include <dm/devres.h>
23 #include <u-boot/zlib.h>
24
25 #include <linux/compat.h>
26 #include <linux/err.h>
27 #include <linux/lzo.h>
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 /* compress.c */
32
33 /*
34  * We need a wrapper for zunzip() because the parameters are
35  * incompatible with the lzo decompressor.
36  */
37 static int gzip_decompress(const unsigned char *in, size_t in_len,
38                            unsigned char *out, size_t *out_len)
39 {
40         return zunzip(out, *out_len, (unsigned char *)in,
41                       (unsigned long *)out_len, 0, 0);
42 }
43
44 /* Fake description object for the "none" compressor */
45 static struct ubifs_compressor none_compr = {
46         .compr_type = UBIFS_COMPR_NONE,
47         .name = "none",
48         .capi_name = "",
49         .decompress = NULL,
50 };
51
52 static struct ubifs_compressor lzo_compr = {
53         .compr_type = UBIFS_COMPR_LZO,
54 #ifndef __UBOOT__
55         .comp_mutex = &lzo_mutex,
56 #endif
57         .name = "lzo",
58         .capi_name = "lzo",
59         .decompress = lzo1x_decompress_safe,
60 };
61
62 static struct ubifs_compressor zlib_compr = {
63         .compr_type = UBIFS_COMPR_ZLIB,
64 #ifndef __UBOOT__
65         .comp_mutex = &deflate_mutex,
66         .decomp_mutex = &inflate_mutex,
67 #endif
68         .name = "zlib",
69         .capi_name = "deflate",
70         .decompress = gzip_decompress,
71 };
72
73 /* All UBIFS compressors */
74 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
75
76
77 #ifdef __UBOOT__
78
79 struct crypto_comp {
80         int compressor;
81 };
82
83 static inline struct crypto_comp
84 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
85 {
86         struct ubifs_compressor *comp;
87         struct crypto_comp *ptr;
88         int i = 0;
89
90         ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
91         while (i < UBIFS_COMPR_TYPES_CNT) {
92                 comp = ubifs_compressors[i];
93                 if (!comp) {
94                         i++;
95                         continue;
96                 }
97                 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
98                         ptr->compressor = i;
99                         return ptr;
100                 }
101                 i++;
102         }
103         if (i >= UBIFS_COMPR_TYPES_CNT) {
104                 dbg_gen("invalid compression type %s", alg_name);
105                 free (ptr);
106                 return NULL;
107         }
108         return ptr;
109 }
110 static inline int
111 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
112                        const u8 *src, unsigned int slen, u8 *dst,
113                        unsigned int *dlen)
114 {
115         struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
116         int err;
117         size_t tmp_len = *dlen;
118
119         if (compr->compr_type == UBIFS_COMPR_NONE) {
120                 memcpy(dst, src, slen);
121                 *dlen = slen;
122                 return 0;
123         }
124
125         err = compr->decompress(src, slen, dst, &tmp_len);
126         if (err)
127                 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
128                           "error %d", slen, compr->name, err);
129
130         *dlen = tmp_len;
131         return err;
132
133         return 0;
134 }
135
136 /* from shrinker.c */
137
138 /* Global clean znode counter (for all mounted UBIFS instances) */
139 atomic_long_t ubifs_clean_zn_cnt;
140
141 #endif
142
143 /**
144  * ubifs_decompress - decompress data.
145  * @in_buf: data to decompress
146  * @in_len: length of the data to decompress
147  * @out_buf: output buffer where decompressed data should
148  * @out_len: output length is returned here
149  * @compr_type: type of compression
150  *
151  * This function decompresses data from buffer @in_buf into buffer @out_buf.
152  * The length of the uncompressed data is returned in @out_len. This functions
153  * returns %0 on success or a negative error code on failure.
154  */
155 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
156                      int in_len, void *out_buf, int *out_len, int compr_type)
157 {
158         int err;
159         struct ubifs_compressor *compr;
160
161         if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
162                 ubifs_err(c, "invalid compression type %d", compr_type);
163                 return -EINVAL;
164         }
165
166         compr = ubifs_compressors[compr_type];
167
168         if (unlikely(!compr->capi_name)) {
169                 ubifs_err(c, "%s compression is not compiled in", compr->name);
170                 return -EINVAL;
171         }
172
173         if (compr_type == UBIFS_COMPR_NONE) {
174                 memcpy(out_buf, in_buf, in_len);
175                 *out_len = in_len;
176                 return 0;
177         }
178
179         if (compr->decomp_mutex)
180                 mutex_lock(compr->decomp_mutex);
181         err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
182                                      (unsigned int *)out_len);
183         if (compr->decomp_mutex)
184                 mutex_unlock(compr->decomp_mutex);
185         if (err)
186                 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
187                           " error %d", in_len, compr->name, err);
188
189         return err;
190 }
191
192 /**
193  * compr_init - initialize a compressor.
194  * @compr: compressor description object
195  *
196  * This function initializes the requested compressor and returns zero in case
197  * of success or a negative error code in case of failure.
198  */
199 static int __init compr_init(struct ubifs_compressor *compr)
200 {
201         ubifs_compressors[compr->compr_type] = compr;
202
203 #ifdef CONFIG_NEEDS_MANUAL_RELOC
204         ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
205         ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
206         ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
207 #endif
208
209         if (compr->capi_name) {
210                 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
211                 if (IS_ERR(compr->cc)) {
212                         dbg_gen("cannot initialize compressor %s,"
213                                   " error %ld", compr->name,
214                                   PTR_ERR(compr->cc));
215                         return PTR_ERR(compr->cc);
216                 }
217         }
218
219         return 0;
220 }
221
222 /**
223  * ubifs_compressors_init - initialize UBIFS compressors.
224  *
225  * This function initializes the compressor which were compiled in. Returns
226  * zero in case of success and a negative error code in case of failure.
227  */
228 int __init ubifs_compressors_init(void)
229 {
230         int err;
231
232         err = compr_init(&lzo_compr);
233         if (err)
234                 return err;
235
236         err = compr_init(&zlib_compr);
237         if (err)
238                 return err;
239
240         err = compr_init(&none_compr);
241         if (err)
242                 return err;
243
244         return 0;
245 }
246
247 /*
248  * ubifsls...
249  */
250
251 static int filldir(struct ubifs_info *c, const char *name, int namlen,
252                    u64 ino, unsigned int d_type)
253 {
254         struct inode *inode;
255         char filetime[32];
256
257         switch (d_type) {
258         case UBIFS_ITYPE_REG:
259                 printf("\t");
260                 break;
261         case UBIFS_ITYPE_DIR:
262                 printf("<DIR>\t");
263                 break;
264         case UBIFS_ITYPE_LNK:
265                 printf("<LNK>\t");
266                 break;
267         default:
268                 printf("other\t");
269                 break;
270         }
271
272         inode = ubifs_iget(c->vfs_sb, ino);
273         if (IS_ERR(inode)) {
274                 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
275                        __func__, ino, inode);
276                 return -1;
277         }
278         ctime_r((time_t *)&inode->i_mtime, filetime);
279         printf("%9lld  %24.24s  ", inode->i_size, filetime);
280 #ifndef __UBOOT__
281         ubifs_iput(inode);
282 #endif
283
284         printf("%s\n", name);
285
286         return 0;
287 }
288
289 static int ubifs_printdir(struct file *file, void *dirent)
290 {
291         int err, over = 0;
292         struct qstr nm;
293         union ubifs_key key;
294         struct ubifs_dent_node *dent;
295         struct inode *dir = file->f_path.dentry->d_inode;
296         struct ubifs_info *c = dir->i_sb->s_fs_info;
297
298         dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
299
300         if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
301                 /*
302                  * The directory was seek'ed to a senseless position or there
303                  * are no more entries.
304                  */
305                 return 0;
306
307         if (file->f_pos == 1) {
308                 /* Find the first entry in TNC and save it */
309                 lowest_dent_key(c, &key, dir->i_ino);
310                 nm.name = NULL;
311                 dent = ubifs_tnc_next_ent(c, &key, &nm);
312                 if (IS_ERR(dent)) {
313                         err = PTR_ERR(dent);
314                         goto out;
315                 }
316
317                 file->f_pos = key_hash_flash(c, &dent->key);
318                 file->private_data = dent;
319         }
320
321         dent = file->private_data;
322         if (!dent) {
323                 /*
324                  * The directory was seek'ed to and is now readdir'ed.
325                  * Find the entry corresponding to @file->f_pos or the
326                  * closest one.
327                  */
328                 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
329                 nm.name = NULL;
330                 dent = ubifs_tnc_next_ent(c, &key, &nm);
331                 if (IS_ERR(dent)) {
332                         err = PTR_ERR(dent);
333                         goto out;
334                 }
335                 file->f_pos = key_hash_flash(c, &dent->key);
336                 file->private_data = dent;
337         }
338
339         while (1) {
340                 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
341                         dent->name, (unsigned long long)le64_to_cpu(dent->inum),
342                         key_hash_flash(c, &dent->key));
343 #ifndef __UBOOT__
344                 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
345 #endif
346
347                 nm.len = le16_to_cpu(dent->nlen);
348                 over = filldir(c, (char *)dent->name, nm.len,
349                                le64_to_cpu(dent->inum), dent->type);
350                 if (over)
351                         return 0;
352
353                 /* Switch to the next entry */
354                 key_read(c, &dent->key, &key);
355                 nm.name = (char *)dent->name;
356                 dent = ubifs_tnc_next_ent(c, &key, &nm);
357                 if (IS_ERR(dent)) {
358                         err = PTR_ERR(dent);
359                         goto out;
360                 }
361
362                 kfree(file->private_data);
363                 file->f_pos = key_hash_flash(c, &dent->key);
364                 file->private_data = dent;
365                 cond_resched();
366         }
367
368 out:
369         if (err != -ENOENT) {
370                 ubifs_err(c, "cannot find next direntry, error %d", err);
371                 return err;
372         }
373
374         kfree(file->private_data);
375         file->private_data = NULL;
376         file->f_pos = 2;
377         return 0;
378 }
379
380 static int ubifs_finddir(struct super_block *sb, char *dirname,
381                          unsigned long root_inum, unsigned long *inum)
382 {
383         int err;
384         struct qstr nm;
385         union ubifs_key key;
386         struct ubifs_dent_node *dent;
387         struct ubifs_info *c;
388         struct file *file;
389         struct dentry *dentry;
390         struct inode *dir;
391         int ret = 0;
392
393         file = kzalloc(sizeof(struct file), 0);
394         dentry = kzalloc(sizeof(struct dentry), 0);
395         dir = kzalloc(sizeof(struct inode), 0);
396         if (!file || !dentry || !dir) {
397                 printf("%s: Error, no memory for malloc!\n", __func__);
398                 err = -ENOMEM;
399                 goto out;
400         }
401
402         dir->i_sb = sb;
403         file->f_path.dentry = dentry;
404         file->f_path.dentry->d_parent = dentry;
405         file->f_path.dentry->d_inode = dir;
406         file->f_path.dentry->d_inode->i_ino = root_inum;
407         c = sb->s_fs_info;
408
409         dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
410
411         /* Find the first entry in TNC and save it */
412         lowest_dent_key(c, &key, dir->i_ino);
413         nm.name = NULL;
414         dent = ubifs_tnc_next_ent(c, &key, &nm);
415         if (IS_ERR(dent)) {
416                 err = PTR_ERR(dent);
417                 goto out;
418         }
419
420         file->f_pos = key_hash_flash(c, &dent->key);
421         file->private_data = dent;
422
423         while (1) {
424                 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
425                         dent->name, (unsigned long long)le64_to_cpu(dent->inum),
426                         key_hash_flash(c, &dent->key));
427 #ifndef __UBOOT__
428                 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
429 #endif
430
431                 nm.len = le16_to_cpu(dent->nlen);
432                 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
433                     (strlen(dirname) == nm.len)) {
434                         *inum = le64_to_cpu(dent->inum);
435                         ret = 1;
436                         goto out_free;
437                 }
438
439                 /* Switch to the next entry */
440                 key_read(c, &dent->key, &key);
441                 nm.name = (char *)dent->name;
442                 dent = ubifs_tnc_next_ent(c, &key, &nm);
443                 if (IS_ERR(dent)) {
444                         err = PTR_ERR(dent);
445                         goto out;
446                 }
447
448                 kfree(file->private_data);
449                 file->f_pos = key_hash_flash(c, &dent->key);
450                 file->private_data = dent;
451                 cond_resched();
452         }
453
454 out:
455         if (err != -ENOENT)
456                 dbg_gen("cannot find next direntry, error %d", err);
457
458 out_free:
459         kfree(file->private_data);
460         free(file);
461         free(dentry);
462         free(dir);
463
464         return ret;
465 }
466
467 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
468 {
469         int ret;
470         char *next;
471         char fpath[128];
472         char symlinkpath[128];
473         char *name = fpath;
474         unsigned long root_inum = 1;
475         unsigned long inum;
476         int symlink_count = 0; /* Don't allow symlink recursion */
477         char link_name[64];
478
479         strcpy(fpath, filename);
480
481         /* Remove all leading slashes */
482         while (*name == '/')
483                 name++;
484
485         /*
486          * Handle root-direcoty ('/')
487          */
488         inum = root_inum;
489         if (!name || *name == '\0')
490                 return inum;
491
492         for (;;) {
493                 struct inode *inode;
494                 struct ubifs_inode *ui;
495
496                 /* Extract the actual part from the pathname.  */
497                 next = strchr(name, '/');
498                 if (next) {
499                         /* Remove all leading slashes.  */
500                         while (*next == '/')
501                                 *(next++) = '\0';
502                 }
503
504                 ret = ubifs_finddir(sb, name, root_inum, &inum);
505                 if (!ret)
506                         return 0;
507                 inode = ubifs_iget(sb, inum);
508
509                 if (!inode)
510                         return 0;
511                 ui = ubifs_inode(inode);
512
513                 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
514                         char buf[128];
515
516                         /* We have some sort of symlink recursion, bail out */
517                         if (symlink_count++ > 8) {
518                                 printf("Symlink recursion, aborting\n");
519                                 return 0;
520                         }
521                         memcpy(link_name, ui->data, ui->data_len);
522                         link_name[ui->data_len] = '\0';
523
524                         if (link_name[0] == '/') {
525                                 /* Absolute path, redo everything without
526                                  * the leading slash */
527                                 next = name = link_name + 1;
528                                 root_inum = 1;
529                                 continue;
530                         }
531                         /* Relative to cur dir */
532                         sprintf(buf, "%s/%s",
533                                         link_name, next == NULL ? "" : next);
534                         memcpy(symlinkpath, buf, sizeof(buf));
535                         next = name = symlinkpath;
536                         continue;
537                 }
538
539                 /*
540                  * Check if directory with this name exists
541                  */
542
543                 /* Found the node!  */
544                 if (!next || *next == '\0')
545                         return inum;
546
547                 root_inum = inum;
548                 name = next;
549         }
550
551         return 0;
552 }
553
554 int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
555 {
556         if (rbdd) {
557                 debug("UBIFS cannot be used with normal block devices\n");
558                 return -1;
559         }
560
561         /*
562          * Should never happen since blk_get_device_part_str() already checks
563          * this, but better safe then sorry.
564          */
565         if (!ubifs_is_mounted()) {
566                 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
567                 return -1;
568         }
569
570         return 0;
571 }
572
573 int ubifs_ls(const char *filename)
574 {
575         struct ubifs_info *c = ubifs_sb->s_fs_info;
576         struct file *file;
577         struct dentry *dentry;
578         struct inode *dir;
579         void *dirent = NULL;
580         unsigned long inum;
581         int ret = 0;
582
583         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
584         inum = ubifs_findfile(ubifs_sb, (char *)filename);
585         if (!inum) {
586                 ret = -1;
587                 goto out;
588         }
589
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__);
595                 ret = -ENOMEM;
596                 goto out_mem;
597         }
598
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;
604         file->f_pos = 1;
605         file->private_data = NULL;
606         ubifs_printdir(file, dirent);
607
608 out_mem:
609         if (file)
610                 free(file);
611         if (dentry)
612                 free(dentry);
613         if (dir)
614                 free(dir);
615
616 out:
617         ubi_close_volume(c->ubi);
618         return ret;
619 }
620
621 int ubifs_exists(const char *filename)
622 {
623         struct ubifs_info *c = ubifs_sb->s_fs_info;
624         unsigned long inum;
625
626         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
627         inum = ubifs_findfile(ubifs_sb, (char *)filename);
628         ubi_close_volume(c->ubi);
629
630         return inum != 0;
631 }
632
633 int ubifs_size(const char *filename, loff_t *size)
634 {
635         struct ubifs_info *c = ubifs_sb->s_fs_info;
636         unsigned long inum;
637         struct inode *inode;
638         int err = 0;
639
640         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
641
642         inum = ubifs_findfile(ubifs_sb, (char *)filename);
643         if (!inum) {
644                 err = -1;
645                 goto out;
646         }
647
648         inode = ubifs_iget(ubifs_sb, inum);
649         if (IS_ERR(inode)) {
650                 printf("%s: Error reading inode %ld!\n", __func__, inum);
651                 err = PTR_ERR(inode);
652                 goto out;
653         }
654
655         *size = inode->i_size;
656
657         ubifs_iput(inode);
658 out:
659         ubi_close_volume(c->ubi);
660         return err;
661 }
662
663 /*
664  * ubifsload...
665  */
666
667 /* file.c */
668
669 static inline void *kmap(struct page *page)
670 {
671         return page->addr;
672 }
673
674 static int read_block(struct inode *inode, void *addr, unsigned int block,
675                       struct ubifs_data_node *dn)
676 {
677         struct ubifs_info *c = inode->i_sb->s_fs_info;
678         int err, len, out_len;
679         union ubifs_key key;
680         unsigned int dlen;
681
682         data_key_init(c, &key, inode->i_ino, block);
683         err = ubifs_tnc_lookup(c, &key, dn);
684         if (err) {
685                 if (err == -ENOENT)
686                         /* Not found, so it must be a hole */
687                         memset(addr, 0, UBIFS_BLOCK_SIZE);
688                 return err;
689         }
690
691         ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
692
693         len = le32_to_cpu(dn->size);
694         if (len <= 0 || len > UBIFS_BLOCK_SIZE)
695                 goto dump;
696
697         dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
698         out_len = UBIFS_BLOCK_SIZE;
699         err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
700                                le16_to_cpu(dn->compr_type));
701         if (err || len != out_len)
702                 goto dump;
703
704         /*
705          * Data length can be less than a full block, even for blocks that are
706          * not the last in the file (e.g., as a result of making a hole and
707          * appending data). Ensure that the remainder is zeroed out.
708          */
709         if (len < UBIFS_BLOCK_SIZE)
710                 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
711
712         return 0;
713
714 dump:
715         ubifs_err(c, "bad data node (block %u, inode %lu)",
716                   block, inode->i_ino);
717         ubifs_dump_node(c, dn);
718         return -EINVAL;
719 }
720
721 static int do_readpage(struct ubifs_info *c, struct inode *inode,
722                        struct page *page, int last_block_size)
723 {
724         void *addr;
725         int err = 0, i;
726         unsigned int block, beyond;
727         struct ubifs_data_node *dn;
728         loff_t i_size = inode->i_size;
729
730         dbg_gen("ino %lu, pg %lu, i_size %lld",
731                 inode->i_ino, page->index, i_size);
732
733         addr = kmap(page);
734
735         block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
736         beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
737         if (block >= beyond) {
738                 /* Reading beyond inode */
739                 memset(addr, 0, PAGE_CACHE_SIZE);
740                 goto out;
741         }
742
743         dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
744         if (!dn)
745                 return -ENOMEM;
746
747         i = 0;
748         while (1) {
749                 int ret;
750
751                 if (block >= beyond) {
752                         /* Reading beyond inode */
753                         err = -ENOENT;
754                         memset(addr, 0, UBIFS_BLOCK_SIZE);
755                 } else {
756                         /*
757                          * Reading last block? Make sure to not write beyond
758                          * the requested size in the destination buffer.
759                          */
760                         if (((block + 1) == beyond) || last_block_size) {
761                                 void *buff;
762                                 int dlen;
763
764                                 /*
765                                  * We need to buffer the data locally for the
766                                  * last block. This is to not pad the
767                                  * destination area to a multiple of
768                                  * UBIFS_BLOCK_SIZE.
769                                  */
770                                 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
771                                 if (!buff) {
772                                         printf("%s: Error, malloc fails!\n",
773                                                __func__);
774                                         err = -ENOMEM;
775                                         break;
776                                 }
777
778                                 /* Read block-size into temp buffer */
779                                 ret = read_block(inode, buff, block, dn);
780                                 if (ret) {
781                                         err = ret;
782                                         if (err != -ENOENT) {
783                                                 free(buff);
784                                                 break;
785                                         }
786                                 }
787
788                                 if (last_block_size)
789                                         dlen = last_block_size;
790                                 else
791                                         dlen = le32_to_cpu(dn->size);
792
793                                 /* Now copy required size back to dest */
794                                 memcpy(addr, buff, dlen);
795
796                                 free(buff);
797                         } else {
798                                 ret = read_block(inode, addr, block, dn);
799                                 if (ret) {
800                                         err = ret;
801                                         if (err != -ENOENT)
802                                                 break;
803                                 }
804                         }
805                 }
806                 if (++i >= UBIFS_BLOCKS_PER_PAGE)
807                         break;
808                 block += 1;
809                 addr += UBIFS_BLOCK_SIZE;
810         }
811         if (err) {
812                 if (err == -ENOENT) {
813                         /* Not found, so it must be a hole */
814                         dbg_gen("hole");
815                         goto out_free;
816                 }
817                 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
818                           page->index, inode->i_ino, err);
819                 goto error;
820         }
821
822 out_free:
823         kfree(dn);
824 out:
825         return 0;
826
827 error:
828         kfree(dn);
829         return err;
830 }
831
832 int ubifs_read(const char *filename, void *buf, loff_t offset,
833                loff_t size, loff_t *actread)
834 {
835         struct ubifs_info *c = ubifs_sb->s_fs_info;
836         unsigned long inum;
837         struct inode *inode;
838         struct page page;
839         int err = 0;
840         int i;
841         int count;
842         int last_block_size = 0;
843
844         *actread = 0;
845
846         if (offset & (PAGE_SIZE - 1)) {
847                 printf("ubifs: Error offset must be a multiple of %d\n",
848                        PAGE_SIZE);
849                 return -1;
850         }
851
852         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
853         /* ubifs_findfile will resolve symlinks, so we know that we get
854          * the real file here */
855         inum = ubifs_findfile(ubifs_sb, (char *)filename);
856         if (!inum) {
857                 err = -1;
858                 goto out;
859         }
860
861         /*
862          * Read file inode
863          */
864         inode = ubifs_iget(ubifs_sb, inum);
865         if (IS_ERR(inode)) {
866                 printf("%s: Error reading inode %ld!\n", __func__, inum);
867                 err = PTR_ERR(inode);
868                 goto out;
869         }
870
871         if (offset > inode->i_size) {
872                 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
873                        offset, size);
874                 err = -1;
875                 goto put_inode;
876         }
877
878         /*
879          * If no size was specified or if size bigger than filesize
880          * set size to filesize
881          */
882         if ((size == 0) || (size > (inode->i_size - offset)))
883                 size = inode->i_size - offset;
884
885         count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
886
887         page.addr = buf;
888         page.index = offset / PAGE_SIZE;
889         page.inode = inode;
890         for (i = 0; i < count; i++) {
891                 /*
892                  * Make sure to not read beyond the requested size
893                  */
894                 if (((i + 1) == count) && (size < inode->i_size))
895                         last_block_size = size - (i * PAGE_SIZE);
896
897                 err = do_readpage(c, inode, &page, last_block_size);
898                 if (err)
899                         break;
900
901                 page.addr += PAGE_SIZE;
902                 page.index++;
903         }
904
905         if (err) {
906                 printf("Error reading file '%s'\n", filename);
907                 *actread = i * PAGE_SIZE;
908         } else {
909                 *actread = size;
910         }
911
912 put_inode:
913         ubifs_iput(inode);
914
915 out:
916         ubi_close_volume(c->ubi);
917         return err;
918 }
919
920 void ubifs_close(void)
921 {
922 }
923
924 /* Compat wrappers for common/cmd_ubifs.c */
925 int ubifs_load(char *filename, u32 addr, u32 size)
926 {
927         loff_t actread;
928         int err;
929
930         printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
931
932         err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
933         if (err == 0) {
934                 env_set_hex("filesize", actread);
935                 printf("Done\n");
936         }
937
938         return err;
939 }
940
941 void uboot_ubifs_umount(void)
942 {
943         if (ubifs_sb) {
944                 printf("Unmounting UBIFS volume %s!\n",
945                        ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
946                 ubifs_umount(ubifs_sb->s_fs_info);
947                 ubifs_sb = NULL;
948         }
949 }