ubifs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. err = compr_init(&none_compr);
  130. if (err)
  131. return err;
  132. return 0;
  133. }
  134. /*
  135. * ubifsls...
  136. */
  137. static int filldir(struct ubifs_info *c, const char *name, int namlen,
  138. u64 ino, unsigned int d_type)
  139. {
  140. struct inode *inode;
  141. char filetime[32];
  142. switch (d_type) {
  143. case UBIFS_ITYPE_REG:
  144. printf("\t");
  145. break;
  146. case UBIFS_ITYPE_DIR:
  147. printf("<DIR>\t");
  148. break;
  149. case UBIFS_ITYPE_LNK:
  150. printf("<LNK>\t");
  151. break;
  152. default:
  153. printf("other\t");
  154. break;
  155. }
  156. inode = ubifs_iget(c->vfs_sb, ino);
  157. if (IS_ERR(inode)) {
  158. printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
  159. __func__, ino, inode);
  160. return -1;
  161. }
  162. ctime_r((time_t *)&inode->i_mtime, filetime);
  163. printf("%9lld %24.24s ", inode->i_size, filetime);
  164. ubifs_iput(inode);
  165. printf("%s\n", name);
  166. return 0;
  167. }
  168. static int ubifs_printdir(struct file *file, void *dirent)
  169. {
  170. int err, over = 0;
  171. struct qstr nm;
  172. union ubifs_key key;
  173. struct ubifs_dent_node *dent;
  174. struct inode *dir = file->f_path.dentry->d_inode;
  175. struct ubifs_info *c = dir->i_sb->s_fs_info;
  176. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  177. if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
  178. /*
  179. * The directory was seek'ed to a senseless position or there
  180. * are no more entries.
  181. */
  182. return 0;
  183. if (file->f_pos == 1) {
  184. /* Find the first entry in TNC and save it */
  185. lowest_dent_key(c, &key, dir->i_ino);
  186. nm.name = NULL;
  187. dent = ubifs_tnc_next_ent(c, &key, &nm);
  188. if (IS_ERR(dent)) {
  189. err = PTR_ERR(dent);
  190. goto out;
  191. }
  192. file->f_pos = key_hash_flash(c, &dent->key);
  193. file->private_data = dent;
  194. }
  195. dent = file->private_data;
  196. if (!dent) {
  197. /*
  198. * The directory was seek'ed to and is now readdir'ed.
  199. * Find the entry corresponding to @file->f_pos or the
  200. * closest one.
  201. */
  202. dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
  203. nm.name = NULL;
  204. dent = ubifs_tnc_next_ent(c, &key, &nm);
  205. if (IS_ERR(dent)) {
  206. err = PTR_ERR(dent);
  207. goto out;
  208. }
  209. file->f_pos = key_hash_flash(c, &dent->key);
  210. file->private_data = dent;
  211. }
  212. while (1) {
  213. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  214. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  215. key_hash_flash(c, &dent->key));
  216. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  217. nm.len = le16_to_cpu(dent->nlen);
  218. over = filldir(c, (char *)dent->name, nm.len,
  219. le64_to_cpu(dent->inum), dent->type);
  220. if (over)
  221. return 0;
  222. /* Switch to the next entry */
  223. key_read(c, &dent->key, &key);
  224. nm.name = (char *)dent->name;
  225. dent = ubifs_tnc_next_ent(c, &key, &nm);
  226. if (IS_ERR(dent)) {
  227. err = PTR_ERR(dent);
  228. goto out;
  229. }
  230. kfree(file->private_data);
  231. file->f_pos = key_hash_flash(c, &dent->key);
  232. file->private_data = dent;
  233. cond_resched();
  234. }
  235. out:
  236. if (err != -ENOENT) {
  237. ubifs_err("cannot find next direntry, error %d", err);
  238. return err;
  239. }
  240. kfree(file->private_data);
  241. file->private_data = NULL;
  242. file->f_pos = 2;
  243. return 0;
  244. }
  245. static int ubifs_finddir(struct super_block *sb, char *dirname,
  246. unsigned long root_inum, unsigned long *inum)
  247. {
  248. int err;
  249. struct qstr nm;
  250. union ubifs_key key;
  251. struct ubifs_dent_node *dent;
  252. struct ubifs_info *c;
  253. struct file *file;
  254. struct dentry *dentry;
  255. struct inode *dir;
  256. file = kzalloc(sizeof(struct file), 0);
  257. dentry = kzalloc(sizeof(struct dentry), 0);
  258. dir = kzalloc(sizeof(struct inode), 0);
  259. if (!file || !dentry || !dir) {
  260. printf("%s: Error, no memory for malloc!\n", __func__);
  261. err = -ENOMEM;
  262. goto out;
  263. }
  264. dir->i_sb = sb;
  265. file->f_path.dentry = dentry;
  266. file->f_path.dentry->d_parent = dentry;
  267. file->f_path.dentry->d_inode = dir;
  268. file->f_path.dentry->d_inode->i_ino = root_inum;
  269. c = sb->s_fs_info;
  270. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  271. /* Find the first entry in TNC and save it */
  272. lowest_dent_key(c, &key, dir->i_ino);
  273. nm.name = NULL;
  274. dent = ubifs_tnc_next_ent(c, &key, &nm);
  275. if (IS_ERR(dent)) {
  276. err = PTR_ERR(dent);
  277. goto out;
  278. }
  279. file->f_pos = key_hash_flash(c, &dent->key);
  280. file->private_data = dent;
  281. while (1) {
  282. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  283. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  284. key_hash_flash(c, &dent->key));
  285. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  286. nm.len = le16_to_cpu(dent->nlen);
  287. if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
  288. (strlen(dirname) == nm.len)) {
  289. *inum = le64_to_cpu(dent->inum);
  290. return 1;
  291. }
  292. /* Switch to the next entry */
  293. key_read(c, &dent->key, &key);
  294. nm.name = (char *)dent->name;
  295. dent = ubifs_tnc_next_ent(c, &key, &nm);
  296. if (IS_ERR(dent)) {
  297. err = PTR_ERR(dent);
  298. goto out;
  299. }
  300. kfree(file->private_data);
  301. file->f_pos = key_hash_flash(c, &dent->key);
  302. file->private_data = dent;
  303. cond_resched();
  304. }
  305. out:
  306. if (err != -ENOENT) {
  307. ubifs_err("cannot find next direntry, error %d", err);
  308. return err;
  309. }
  310. if (file)
  311. free(file);
  312. if (dentry)
  313. free(dentry);
  314. if (dir)
  315. free(dir);
  316. if (file->private_data)
  317. kfree(file->private_data);
  318. file->private_data = NULL;
  319. file->f_pos = 2;
  320. return 0;
  321. }
  322. static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
  323. {
  324. int ret;
  325. char *next;
  326. char fpath[128];
  327. char *name = fpath;
  328. unsigned long root_inum = 1;
  329. unsigned long inum;
  330. strcpy(fpath, filename);
  331. /* Remove all leading slashes */
  332. while (*name == '/')
  333. name++;
  334. /*
  335. * Handle root-direcoty ('/')
  336. */
  337. inum = root_inum;
  338. if (!name || *name == '\0')
  339. return inum;
  340. for (;;) {
  341. /* Extract the actual part from the pathname. */
  342. next = strchr(name, '/');
  343. if (next) {
  344. /* Remove all leading slashes. */
  345. while (*next == '/')
  346. *(next++) = '\0';
  347. }
  348. ret = ubifs_finddir(sb, name, root_inum, &inum);
  349. /*
  350. * Check if directory with this name exists
  351. */
  352. /* Found the node! */
  353. if (!next || *next == '\0') {
  354. if (ret)
  355. return inum;
  356. break;
  357. }
  358. root_inum = inum;
  359. name = next;
  360. }
  361. return 0;
  362. }
  363. int ubifs_ls(char *filename)
  364. {
  365. struct ubifs_info *c = ubifs_sb->s_fs_info;
  366. struct file *file;
  367. struct dentry *dentry;
  368. struct inode *dir;
  369. void *dirent = NULL;
  370. unsigned long inum;
  371. int ret = 0;
  372. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  373. inum = ubifs_findfile(ubifs_sb, filename);
  374. if (!inum) {
  375. ret = -1;
  376. goto out;
  377. }
  378. file = kzalloc(sizeof(struct file), 0);
  379. dentry = kzalloc(sizeof(struct dentry), 0);
  380. dir = kzalloc(sizeof(struct inode), 0);
  381. if (!file || !dentry || !dir) {
  382. printf("%s: Error, no memory for malloc!\n", __func__);
  383. ret = -ENOMEM;
  384. goto out_mem;
  385. }
  386. dir->i_sb = ubifs_sb;
  387. file->f_path.dentry = dentry;
  388. file->f_path.dentry->d_parent = dentry;
  389. file->f_path.dentry->d_inode = dir;
  390. file->f_path.dentry->d_inode->i_ino = inum;
  391. file->f_pos = 1;
  392. file->private_data = NULL;
  393. ubifs_printdir(file, dirent);
  394. out_mem:
  395. if (file)
  396. free(file);
  397. if (dentry)
  398. free(dentry);
  399. if (dir)
  400. free(dir);
  401. out:
  402. ubi_close_volume(c->ubi);
  403. return ret;
  404. }
  405. /*
  406. * ubifsload...
  407. */
  408. /* file.c */
  409. static inline void *kmap(struct page *page)
  410. {
  411. return page->addr;
  412. }
  413. static int read_block(struct inode *inode, void *addr, unsigned int block,
  414. struct ubifs_data_node *dn)
  415. {
  416. struct ubifs_info *c = inode->i_sb->s_fs_info;
  417. int err, len, out_len;
  418. union ubifs_key key;
  419. unsigned int dlen;
  420. data_key_init(c, &key, inode->i_ino, block);
  421. err = ubifs_tnc_lookup(c, &key, dn);
  422. if (err) {
  423. if (err == -ENOENT)
  424. /* Not found, so it must be a hole */
  425. memset(addr, 0, UBIFS_BLOCK_SIZE);
  426. return err;
  427. }
  428. ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
  429. len = le32_to_cpu(dn->size);
  430. if (len <= 0 || len > UBIFS_BLOCK_SIZE)
  431. goto dump;
  432. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  433. out_len = UBIFS_BLOCK_SIZE;
  434. err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
  435. le16_to_cpu(dn->compr_type));
  436. if (err || len != out_len)
  437. goto dump;
  438. /*
  439. * Data length can be less than a full block, even for blocks that are
  440. * not the last in the file (e.g., as a result of making a hole and
  441. * appending data). Ensure that the remainder is zeroed out.
  442. */
  443. if (len < UBIFS_BLOCK_SIZE)
  444. memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
  445. return 0;
  446. dump:
  447. ubifs_err("bad data node (block %u, inode %lu)",
  448. block, inode->i_ino);
  449. dbg_dump_node(c, dn);
  450. return -EINVAL;
  451. }
  452. static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
  453. {
  454. void *addr;
  455. int err = 0, i;
  456. unsigned int block, beyond;
  457. struct ubifs_data_node *dn;
  458. loff_t i_size = inode->i_size;
  459. dbg_gen("ino %lu, pg %lu, i_size %lld",
  460. inode->i_ino, page->index, i_size);
  461. addr = kmap(page);
  462. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  463. beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  464. if (block >= beyond) {
  465. /* Reading beyond inode */
  466. memset(addr, 0, PAGE_CACHE_SIZE);
  467. goto out;
  468. }
  469. dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
  470. if (!dn) {
  471. err = -ENOMEM;
  472. goto error;
  473. }
  474. i = 0;
  475. while (1) {
  476. int ret;
  477. if (block >= beyond) {
  478. /* Reading beyond inode */
  479. err = -ENOENT;
  480. memset(addr, 0, UBIFS_BLOCK_SIZE);
  481. } else {
  482. ret = read_block(inode, addr, block, dn);
  483. if (ret) {
  484. err = ret;
  485. if (err != -ENOENT)
  486. break;
  487. } else if (block + 1 == beyond) {
  488. int dlen = le32_to_cpu(dn->size);
  489. int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
  490. if (ilen && ilen < dlen)
  491. memset(addr + ilen, 0, dlen - ilen);
  492. }
  493. }
  494. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  495. break;
  496. block += 1;
  497. addr += UBIFS_BLOCK_SIZE;
  498. }
  499. if (err) {
  500. if (err == -ENOENT) {
  501. /* Not found, so it must be a hole */
  502. dbg_gen("hole");
  503. goto out_free;
  504. }
  505. ubifs_err("cannot read page %lu of inode %lu, error %d",
  506. page->index, inode->i_ino, err);
  507. goto error;
  508. }
  509. out_free:
  510. kfree(dn);
  511. out:
  512. return 0;
  513. error:
  514. kfree(dn);
  515. return err;
  516. }
  517. int ubifs_load(char *filename, u32 addr, u32 size)
  518. {
  519. struct ubifs_info *c = ubifs_sb->s_fs_info;
  520. unsigned long inum;
  521. struct inode *inode;
  522. struct page page;
  523. int err = 0;
  524. int i;
  525. int count;
  526. char link_name[64];
  527. struct ubifs_inode *ui;
  528. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  529. inum = ubifs_findfile(ubifs_sb, filename);
  530. if (!inum) {
  531. err = -1;
  532. goto out;
  533. }
  534. /*
  535. * Read file inode
  536. */
  537. inode = ubifs_iget(ubifs_sb, inum);
  538. if (IS_ERR(inode)) {
  539. printf("%s: Error reading inode %ld!\n", __func__, inum);
  540. err = PTR_ERR(inode);
  541. goto out;
  542. }
  543. /*
  544. * Check for symbolic link
  545. */
  546. ui = ubifs_inode(inode);
  547. if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
  548. memcpy(link_name, ui->data, ui->data_len);
  549. printf("%s is linked to %s!\n", filename, link_name);
  550. ubifs_iput(inode);
  551. /*
  552. * Now we have the "real" filename, call ubifs_load()
  553. * again (recursive call) to load this file instead
  554. */
  555. return ubifs_load(link_name, addr, size);
  556. }
  557. /*
  558. * If no size was specified or if size bigger than filesize
  559. * set size to filesize
  560. */
  561. if ((size == 0) || (size > inode->i_size))
  562. size = inode->i_size;
  563. count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  564. printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
  565. filename, addr, size, size);
  566. page.addr = (void *)addr;
  567. page.index = 0;
  568. page.inode = inode;
  569. for (i = 0; i < count; i++) {
  570. err = do_readpage(c, inode, &page);
  571. if (err)
  572. break;
  573. page.addr += PAGE_SIZE;
  574. page.index++;
  575. }
  576. if (err)
  577. printf("Error reading file '%s'\n", filename);
  578. else
  579. printf("Done\n");
  580. ubifs_iput(inode);
  581. out:
  582. ubi_close_volume(c->ubi);
  583. return err;
  584. }