ubifs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * (C) Copyright 2008-2009
  7. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  8. *
  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.
  12. *
  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
  16. * more details.
  17. *
  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
  21. *
  22. * Authors: Artem Bityutskiy (Битюцкий Артём)
  23. * Adrian Hunter
  24. */
  25. #include "ubifs.h"
  26. #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
  27. #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
  28. #endif
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /* compress.c */
  31. /*
  32. * We need a wrapper for gunzip() because the parameters are
  33. * incompatible with the lzo decompressor.
  34. */
  35. static int gzip_decompress(const unsigned char *in, size_t in_len,
  36. unsigned char *out, size_t *out_len)
  37. {
  38. unsigned long len = in_len;
  39. return gunzip(out, *out_len, (unsigned char *)in, &len);
  40. }
  41. /* Fake description object for the "none" compressor */
  42. static struct ubifs_compressor none_compr = {
  43. .compr_type = UBIFS_COMPR_NONE,
  44. .name = "no compression",
  45. .capi_name = "",
  46. .decompress = NULL,
  47. };
  48. static struct ubifs_compressor lzo_compr = {
  49. .compr_type = UBIFS_COMPR_LZO,
  50. .name = "LZO",
  51. .capi_name = "lzo",
  52. .decompress = lzo1x_decompress_safe,
  53. };
  54. static struct ubifs_compressor zlib_compr = {
  55. .compr_type = UBIFS_COMPR_ZLIB,
  56. .name = "zlib",
  57. .capi_name = "deflate",
  58. .decompress = gzip_decompress,
  59. };
  60. /* All UBIFS compressors */
  61. struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
  62. /**
  63. * ubifs_decompress - decompress data.
  64. * @in_buf: data to decompress
  65. * @in_len: length of the data to decompress
  66. * @out_buf: output buffer where decompressed data should
  67. * @out_len: output length is returned here
  68. * @compr_type: type of compression
  69. *
  70. * This function decompresses data from buffer @in_buf into buffer @out_buf.
  71. * The length of the uncompressed data is returned in @out_len. This functions
  72. * returns %0 on success or a negative error code on failure.
  73. */
  74. int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
  75. int *out_len, int compr_type)
  76. {
  77. int err;
  78. struct ubifs_compressor *compr;
  79. if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
  80. ubifs_err("invalid compression type %d", compr_type);
  81. return -EINVAL;
  82. }
  83. compr = ubifs_compressors[compr_type];
  84. if (unlikely(!compr->capi_name)) {
  85. ubifs_err("%s compression is not compiled in", compr->name);
  86. return -EINVAL;
  87. }
  88. if (compr_type == UBIFS_COMPR_NONE) {
  89. memcpy(out_buf, in_buf, in_len);
  90. *out_len = in_len;
  91. return 0;
  92. }
  93. err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
  94. if (err)
  95. ubifs_err("cannot decompress %d bytes, compressor %s, "
  96. "error %d", in_len, compr->name, err);
  97. return err;
  98. }
  99. /**
  100. * compr_init - initialize a compressor.
  101. * @compr: compressor description object
  102. *
  103. * This function initializes the requested compressor and returns zero in case
  104. * of success or a negative error code in case of failure.
  105. */
  106. static int __init compr_init(struct ubifs_compressor *compr)
  107. {
  108. ubifs_compressors[compr->compr_type] = compr;
  109. ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
  110. ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
  111. ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
  112. return 0;
  113. }
  114. /**
  115. * ubifs_compressors_init - initialize UBIFS compressors.
  116. *
  117. * This function initializes the compressor which were compiled in. Returns
  118. * zero in case of success and a negative error code in case of failure.
  119. */
  120. int __init ubifs_compressors_init(void)
  121. {
  122. int err;
  123. err = compr_init(&lzo_compr);
  124. if (err)
  125. return err;
  126. err = compr_init(&zlib_compr);
  127. if (err)
  128. return err;
  129. ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
  130. return 0;
  131. }
  132. /*
  133. * ubifsls...
  134. */
  135. static int filldir(struct ubifs_info *c, const char *name, int namlen,
  136. u64 ino, unsigned int d_type)
  137. {
  138. struct inode *inode;
  139. char filetime[32];
  140. switch (d_type) {
  141. case UBIFS_ITYPE_REG:
  142. printf("\t");
  143. break;
  144. case UBIFS_ITYPE_DIR:
  145. printf("<DIR>\t");
  146. break;
  147. case UBIFS_ITYPE_LNK:
  148. printf("<LNK>\t");
  149. break;
  150. default:
  151. printf("other\t");
  152. break;
  153. }
  154. inode = ubifs_iget(c->vfs_sb, ino);
  155. if (IS_ERR(inode)) {
  156. printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
  157. __func__, ino, inode);
  158. return -1;
  159. }
  160. ctime_r((time_t *)&inode->i_mtime, filetime);
  161. printf("%9lld %24.24s ", inode->i_size, filetime);
  162. ubifs_iput(inode);
  163. printf("%s\n", name);
  164. return 0;
  165. }
  166. static int ubifs_printdir(struct file *file, void *dirent)
  167. {
  168. int err, over = 0;
  169. struct qstr nm;
  170. union ubifs_key key;
  171. struct ubifs_dent_node *dent;
  172. struct inode *dir = file->f_path.dentry->d_inode;
  173. struct ubifs_info *c = dir->i_sb->s_fs_info;
  174. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  175. if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
  176. /*
  177. * The directory was seek'ed to a senseless position or there
  178. * are no more entries.
  179. */
  180. return 0;
  181. if (file->f_pos == 1) {
  182. /* Find the first entry in TNC and save it */
  183. lowest_dent_key(c, &key, dir->i_ino);
  184. nm.name = NULL;
  185. dent = ubifs_tnc_next_ent(c, &key, &nm);
  186. if (IS_ERR(dent)) {
  187. err = PTR_ERR(dent);
  188. goto out;
  189. }
  190. file->f_pos = key_hash_flash(c, &dent->key);
  191. file->private_data = dent;
  192. }
  193. dent = file->private_data;
  194. if (!dent) {
  195. /*
  196. * The directory was seek'ed to and is now readdir'ed.
  197. * Find the entry corresponding to @file->f_pos or the
  198. * closest one.
  199. */
  200. dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
  201. nm.name = NULL;
  202. dent = ubifs_tnc_next_ent(c, &key, &nm);
  203. if (IS_ERR(dent)) {
  204. err = PTR_ERR(dent);
  205. goto out;
  206. }
  207. file->f_pos = key_hash_flash(c, &dent->key);
  208. file->private_data = dent;
  209. }
  210. while (1) {
  211. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  212. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  213. key_hash_flash(c, &dent->key));
  214. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  215. nm.len = le16_to_cpu(dent->nlen);
  216. over = filldir(c, (char *)dent->name, nm.len,
  217. le64_to_cpu(dent->inum), dent->type);
  218. if (over)
  219. return 0;
  220. /* Switch to the next entry */
  221. key_read(c, &dent->key, &key);
  222. nm.name = (char *)dent->name;
  223. dent = ubifs_tnc_next_ent(c, &key, &nm);
  224. if (IS_ERR(dent)) {
  225. err = PTR_ERR(dent);
  226. goto out;
  227. }
  228. kfree(file->private_data);
  229. file->f_pos = key_hash_flash(c, &dent->key);
  230. file->private_data = dent;
  231. cond_resched();
  232. }
  233. out:
  234. if (err != -ENOENT) {
  235. ubifs_err("cannot find next direntry, error %d", err);
  236. return err;
  237. }
  238. kfree(file->private_data);
  239. file->private_data = NULL;
  240. file->f_pos = 2;
  241. return 0;
  242. }
  243. static int ubifs_finddir(struct super_block *sb, char *dirname,
  244. unsigned long root_inum, unsigned long *inum)
  245. {
  246. int err;
  247. struct qstr nm;
  248. union ubifs_key key;
  249. struct ubifs_dent_node *dent;
  250. struct ubifs_info *c;
  251. struct file *file;
  252. struct dentry *dentry;
  253. struct inode *dir;
  254. file = kzalloc(sizeof(struct file), 0);
  255. dentry = kzalloc(sizeof(struct dentry), 0);
  256. dir = kzalloc(sizeof(struct inode), 0);
  257. if (!file || !dentry || !dir) {
  258. printf("%s: Error, no memory for malloc!\n", __func__);
  259. err = -ENOMEM;
  260. goto out;
  261. }
  262. dir->i_sb = sb;
  263. file->f_path.dentry = dentry;
  264. file->f_path.dentry->d_parent = dentry;
  265. file->f_path.dentry->d_inode = dir;
  266. file->f_path.dentry->d_inode->i_ino = root_inum;
  267. c = sb->s_fs_info;
  268. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  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. while (1) {
  280. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  281. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  282. key_hash_flash(c, &dent->key));
  283. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  284. nm.len = le16_to_cpu(dent->nlen);
  285. if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
  286. (strlen(dirname) == nm.len)) {
  287. *inum = le64_to_cpu(dent->inum);
  288. return 1;
  289. }
  290. /* Switch to the next entry */
  291. key_read(c, &dent->key, &key);
  292. nm.name = (char *)dent->name;
  293. dent = ubifs_tnc_next_ent(c, &key, &nm);
  294. if (IS_ERR(dent)) {
  295. err = PTR_ERR(dent);
  296. goto out;
  297. }
  298. kfree(file->private_data);
  299. file->f_pos = key_hash_flash(c, &dent->key);
  300. file->private_data = dent;
  301. cond_resched();
  302. }
  303. out:
  304. if (err != -ENOENT) {
  305. ubifs_err("cannot find next direntry, error %d", err);
  306. return err;
  307. }
  308. if (file)
  309. free(file);
  310. if (dentry)
  311. free(dentry);
  312. if (dir)
  313. free(dir);
  314. if (file->private_data)
  315. kfree(file->private_data);
  316. file->private_data = NULL;
  317. file->f_pos = 2;
  318. return 0;
  319. }
  320. static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
  321. {
  322. int ret;
  323. char *next;
  324. char fpath[128];
  325. char *name = fpath;
  326. unsigned long root_inum = 1;
  327. unsigned long inum;
  328. strcpy(fpath, filename);
  329. /* Remove all leading slashes */
  330. while (*name == '/')
  331. name++;
  332. /*
  333. * Handle root-direcoty ('/')
  334. */
  335. inum = root_inum;
  336. if (!name || *name == '\0')
  337. return inum;
  338. for (;;) {
  339. /* Extract the actual part from the pathname. */
  340. next = strchr(name, '/');
  341. if (next) {
  342. /* Remove all leading slashes. */
  343. while (*next == '/')
  344. *(next++) = '\0';
  345. }
  346. ret = ubifs_finddir(sb, name, root_inum, &inum);
  347. /*
  348. * Check if directory with this name exists
  349. */
  350. /* Found the node! */
  351. if (!next || *next == '\0') {
  352. if (ret)
  353. return inum;
  354. break;
  355. }
  356. root_inum = inum;
  357. name = next;
  358. }
  359. return 0;
  360. }
  361. int ubifs_ls(char *filename)
  362. {
  363. struct ubifs_info *c = ubifs_sb->s_fs_info;
  364. struct file *file;
  365. struct dentry *dentry;
  366. struct inode *dir;
  367. void *dirent = NULL;
  368. unsigned long inum;
  369. int ret = 0;
  370. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  371. inum = ubifs_findfile(ubifs_sb, filename);
  372. if (!inum) {
  373. ret = -1;
  374. goto out;
  375. }
  376. file = kzalloc(sizeof(struct file), 0);
  377. dentry = kzalloc(sizeof(struct dentry), 0);
  378. dir = kzalloc(sizeof(struct inode), 0);
  379. if (!file || !dentry || !dir) {
  380. printf("%s: Error, no memory for malloc!\n", __func__);
  381. ret = -ENOMEM;
  382. goto out_mem;
  383. }
  384. dir->i_sb = ubifs_sb;
  385. file->f_path.dentry = dentry;
  386. file->f_path.dentry->d_parent = dentry;
  387. file->f_path.dentry->d_inode = dir;
  388. file->f_path.dentry->d_inode->i_ino = inum;
  389. file->f_pos = 1;
  390. file->private_data = NULL;
  391. ubifs_printdir(file, dirent);
  392. out_mem:
  393. if (file)
  394. free(file);
  395. if (dentry)
  396. free(dentry);
  397. if (dir)
  398. free(dir);
  399. out:
  400. ubi_close_volume(c->ubi);
  401. return ret;
  402. }
  403. /*
  404. * ubifsload...
  405. */
  406. /* file.c */
  407. static inline void *kmap(struct page *page)
  408. {
  409. return page->addr;
  410. }
  411. static int read_block(struct inode *inode, void *addr, unsigned int block,
  412. struct ubifs_data_node *dn)
  413. {
  414. struct ubifs_info *c = inode->i_sb->s_fs_info;
  415. int err, len, out_len;
  416. union ubifs_key key;
  417. unsigned int dlen;
  418. data_key_init(c, &key, inode->i_ino, block);
  419. err = ubifs_tnc_lookup(c, &key, dn);
  420. if (err) {
  421. if (err == -ENOENT)
  422. /* Not found, so it must be a hole */
  423. memset(addr, 0, UBIFS_BLOCK_SIZE);
  424. return err;
  425. }
  426. ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
  427. len = le32_to_cpu(dn->size);
  428. if (len <= 0 || len > UBIFS_BLOCK_SIZE)
  429. goto dump;
  430. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  431. out_len = UBIFS_BLOCK_SIZE;
  432. err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
  433. le16_to_cpu(dn->compr_type));
  434. if (err || len != out_len)
  435. goto dump;
  436. /*
  437. * Data length can be less than a full block, even for blocks that are
  438. * not the last in the file (e.g., as a result of making a hole and
  439. * appending data). Ensure that the remainder is zeroed out.
  440. */
  441. if (len < UBIFS_BLOCK_SIZE)
  442. memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
  443. return 0;
  444. dump:
  445. ubifs_err("bad data node (block %u, inode %lu)",
  446. block, inode->i_ino);
  447. dbg_dump_node(c, dn);
  448. return -EINVAL;
  449. }
  450. static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
  451. {
  452. void *addr;
  453. int err = 0, i;
  454. unsigned int block, beyond;
  455. struct ubifs_data_node *dn;
  456. loff_t i_size = inode->i_size;
  457. dbg_gen("ino %lu, pg %lu, i_size %lld",
  458. inode->i_ino, page->index, i_size);
  459. addr = kmap(page);
  460. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  461. beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  462. if (block >= beyond) {
  463. /* Reading beyond inode */
  464. memset(addr, 0, PAGE_CACHE_SIZE);
  465. goto out;
  466. }
  467. dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
  468. if (!dn) {
  469. err = -ENOMEM;
  470. goto error;
  471. }
  472. i = 0;
  473. while (1) {
  474. int ret;
  475. if (block >= beyond) {
  476. /* Reading beyond inode */
  477. err = -ENOENT;
  478. memset(addr, 0, UBIFS_BLOCK_SIZE);
  479. } else {
  480. ret = read_block(inode, addr, block, dn);
  481. if (ret) {
  482. err = ret;
  483. if (err != -ENOENT)
  484. break;
  485. } else if (block + 1 == beyond) {
  486. int dlen = le32_to_cpu(dn->size);
  487. int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
  488. if (ilen && ilen < dlen)
  489. memset(addr + ilen, 0, dlen - ilen);
  490. }
  491. }
  492. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  493. break;
  494. block += 1;
  495. addr += UBIFS_BLOCK_SIZE;
  496. }
  497. if (err) {
  498. if (err == -ENOENT) {
  499. /* Not found, so it must be a hole */
  500. dbg_gen("hole");
  501. goto out_free;
  502. }
  503. ubifs_err("cannot read page %lu of inode %lu, error %d",
  504. page->index, inode->i_ino, err);
  505. goto error;
  506. }
  507. out_free:
  508. kfree(dn);
  509. out:
  510. return 0;
  511. error:
  512. kfree(dn);
  513. return err;
  514. }
  515. int ubifs_load(char *filename, u32 addr, u32 size)
  516. {
  517. struct ubifs_info *c = ubifs_sb->s_fs_info;
  518. unsigned long inum;
  519. struct inode *inode;
  520. struct page page;
  521. int err = 0;
  522. int i;
  523. int count;
  524. char link_name[64];
  525. struct ubifs_inode *ui;
  526. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  527. inum = ubifs_findfile(ubifs_sb, filename);
  528. if (!inum) {
  529. err = -1;
  530. goto out;
  531. }
  532. /*
  533. * Read file inode
  534. */
  535. inode = ubifs_iget(ubifs_sb, inum);
  536. if (IS_ERR(inode)) {
  537. printf("%s: Error reading inode %ld!\n", __func__, inum);
  538. err = PTR_ERR(inode);
  539. goto out;
  540. }
  541. /*
  542. * Check for symbolic link
  543. */
  544. ui = ubifs_inode(inode);
  545. if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
  546. memcpy(link_name, ui->data, ui->data_len);
  547. printf("%s is linked to %s!\n", filename, link_name);
  548. ubifs_iput(inode);
  549. /*
  550. * Now we have the "real" filename, call ubifs_load()
  551. * again (recursive call) to load this file instead
  552. */
  553. return ubifs_load(link_name, addr, size);
  554. }
  555. /*
  556. * If no size was specified or if size bigger than filesize
  557. * set size to filesize
  558. */
  559. if ((size == 0) || (size > inode->i_size))
  560. size = inode->i_size;
  561. count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  562. printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
  563. filename, addr, size, size);
  564. page.addr = (void *)addr;
  565. page.index = 0;
  566. page.inode = inode;
  567. for (i = 0; i < count; i++) {
  568. err = do_readpage(c, inode, &page);
  569. if (err)
  570. break;
  571. page.addr += PAGE_SIZE;
  572. page.index++;
  573. }
  574. if (err)
  575. printf("Error reading file '%s'\n", filename);
  576. else
  577. printf("Done\n");
  578. ubifs_iput(inode);
  579. out:
  580. ubi_close_volume(c->ubi);
  581. return err;
  582. }