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