PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ubifs/ubifs.c

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