reiserfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
  3. *
  4. * GRUB -- GRand Unified Bootloader
  5. * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  6. *
  7. * (C) Copyright 2003 - 2004
  8. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /* An implementation for the ReiserFS filesystem ported from GRUB.
  26. * Some parts of this code (mainly the structures and defines) are
  27. * from the original reiser fs code, as found in the linux kernel.
  28. */
  29. #include <common.h>
  30. #if (CONFIG_COMMANDS & CFG_CMD_REISER)
  31. #include <malloc.h>
  32. #include <linux/ctype.h>
  33. #include <linux/time.h>
  34. #include <asm/byteorder.h>
  35. #include <reiserfs.h>
  36. #include "reiserfs_private.h"
  37. #undef REISERDEBUG
  38. /* Some parts of this code (mainly the structures and defines) are
  39. * from the original reiser fs code, as found in the linux kernel.
  40. */
  41. static char fsys_buf[FSYS_BUFLEN];
  42. static reiserfs_error_t errnum = ERR_NONE;
  43. static int print_possibilities;
  44. static unsigned int filepos, filemax;
  45. static int
  46. substring (const char *s1, const char *s2)
  47. {
  48. while (*s1 == *s2)
  49. {
  50. /* The strings match exactly. */
  51. if (! *(s1++))
  52. return 0;
  53. s2 ++;
  54. }
  55. /* S1 is a substring of S2. */
  56. if (*s1 == 0)
  57. return -1;
  58. /* S1 isn't a substring. */
  59. return 1;
  60. }
  61. static void sd_print_item (struct item_head * ih, char * item)
  62. {
  63. char filetime[30];
  64. time_t ttime;
  65. if (stat_data_v1 (ih)) {
  66. struct stat_data_v1 * sd = (struct stat_data_v1 *)item;
  67. ttime = sd_v1_mtime(sd);
  68. ctime_r(&ttime, filetime);
  69. printf ("%-10s %4hd %6d %6d %9d %24.24s",
  70. bb_mode_string(sd_v1_mode(sd)), sd_v1_nlink(sd),sd_v1_uid(sd), sd_v1_gid(sd),
  71. sd_v1_size(sd), filetime);
  72. } else {
  73. struct stat_data * sd = (struct stat_data *)item;
  74. ttime = sd_v2_mtime(sd);
  75. ctime_r(&ttime, filetime);
  76. printf ("%-10s %4d %6d %6d %9d %24.24s",
  77. bb_mode_string(sd_v2_mode(sd)), sd_v2_nlink(sd),sd_v2_uid(sd),sd_v2_gid(sd),
  78. (__u32) sd_v2_size(sd), filetime);
  79. }
  80. }
  81. static int
  82. journal_read (int block, int len, char *buffer)
  83. {
  84. return reiserfs_devread ((INFO->journal_block + block) << INFO->blocksize_shift,
  85. 0, len, buffer);
  86. }
  87. /* Read a block from ReiserFS file system, taking the journal into
  88. * account. If the block nr is in the journal, the block from the
  89. * journal taken.
  90. */
  91. static int
  92. block_read (unsigned int blockNr, int start, int len, char *buffer)
  93. {
  94. int transactions = INFO->journal_transactions;
  95. int desc_block = INFO->journal_first_desc;
  96. int journal_mask = INFO->journal_block_count - 1;
  97. int translatedNr = blockNr;
  98. __u32 *journal_table = JOURNAL_START;
  99. while (transactions-- > 0)
  100. {
  101. int i = 0;
  102. int j_len;
  103. if (__le32_to_cpu(*journal_table) != 0xffffffff)
  104. {
  105. /* Search for the blockNr in cached journal */
  106. j_len = __le32_to_cpu(*journal_table++);
  107. while (i++ < j_len)
  108. {
  109. if (__le32_to_cpu(*journal_table++) == blockNr)
  110. {
  111. journal_table += j_len - i;
  112. goto found;
  113. }
  114. }
  115. }
  116. else
  117. {
  118. /* This is the end of cached journal marker. The remaining
  119. * transactions are still on disk.
  120. */
  121. struct reiserfs_journal_desc desc;
  122. struct reiserfs_journal_commit commit;
  123. if (! journal_read (desc_block, sizeof (desc), (char *) &desc))
  124. return 0;
  125. j_len = __le32_to_cpu(desc.j_len);
  126. while (i < j_len && i < JOURNAL_TRANS_HALF)
  127. if (__le32_to_cpu(desc.j_realblock[i++]) == blockNr)
  128. goto found;
  129. if (j_len >= JOURNAL_TRANS_HALF)
  130. {
  131. int commit_block = (desc_block + 1 + j_len) & journal_mask;
  132. if (! journal_read (commit_block,
  133. sizeof (commit), (char *) &commit))
  134. return 0;
  135. while (i < j_len)
  136. if (__le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr)
  137. goto found;
  138. }
  139. }
  140. goto not_found;
  141. found:
  142. translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);
  143. #ifdef REISERDEBUG
  144. printf ("block_read: block %d is mapped to journal block %d.\n",
  145. blockNr, translatedNr - INFO->journal_block);
  146. #endif
  147. /* We must continue the search, as this block may be overwritten
  148. * in later transactions.
  149. */
  150. not_found:
  151. desc_block = (desc_block + 2 + j_len) & journal_mask;
  152. }
  153. return reiserfs_devread (translatedNr << INFO->blocksize_shift, start, len, buffer);
  154. }
  155. /* Init the journal data structure. We try to cache as much as
  156. * possible in the JOURNAL_START-JOURNAL_END space, but if it is full
  157. * we can still read the rest from the disk on demand.
  158. *
  159. * The first number of valid transactions and the descriptor block of the
  160. * first valid transaction are held in INFO. The transactions are all
  161. * adjacent, but we must take care of the journal wrap around.
  162. */
  163. static int
  164. journal_init (void)
  165. {
  166. unsigned int block_count = INFO->journal_block_count;
  167. unsigned int desc_block;
  168. unsigned int commit_block;
  169. unsigned int next_trans_id;
  170. struct reiserfs_journal_header header;
  171. struct reiserfs_journal_desc desc;
  172. struct reiserfs_journal_commit commit;
  173. __u32 *journal_table = JOURNAL_START;
  174. journal_read (block_count, sizeof (header), (char *) &header);
  175. desc_block = __le32_to_cpu(header.j_first_unflushed_offset);
  176. if (desc_block >= block_count)
  177. return 0;
  178. INFO->journal_first_desc = desc_block;
  179. next_trans_id = __le32_to_cpu(header.j_last_flush_trans_id) + 1;
  180. #ifdef REISERDEBUG
  181. printf ("journal_init: last flushed %d\n",
  182. __le32_to_cpu(header.j_last_flush_trans_id));
  183. #endif
  184. while (1)
  185. {
  186. journal_read (desc_block, sizeof (desc), (char *) &desc);
  187. if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0
  188. || __le32_to_cpu(desc.j_trans_id) != next_trans_id
  189. || __le32_to_cpu(desc.j_mount_id) != __le32_to_cpu(header.j_mount_id))
  190. /* no more valid transactions */
  191. break;
  192. commit_block = (desc_block + __le32_to_cpu(desc.j_len) + 1) & (block_count - 1);
  193. journal_read (commit_block, sizeof (commit), (char *) &commit);
  194. if (__le32_to_cpu(desc.j_trans_id) != commit.j_trans_id
  195. || __le32_to_cpu(desc.j_len) != __le32_to_cpu(commit.j_len))
  196. /* no more valid transactions */
  197. break;
  198. #ifdef REISERDEBUG
  199. printf ("Found valid transaction %d/%d at %d.\n",
  200. __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
  201. #endif
  202. next_trans_id++;
  203. if (journal_table < JOURNAL_END)
  204. {
  205. if ((journal_table + 1 + __le32_to_cpu(desc.j_len)) >= JOURNAL_END)
  206. {
  207. /* The table is almost full; mark the end of the cached
  208. * journal.*/
  209. *journal_table = __cpu_to_le32(0xffffffff);
  210. journal_table = JOURNAL_END;
  211. }
  212. else
  213. {
  214. unsigned int i;
  215. /* Cache the length and the realblock numbers in the table.
  216. * The block number of descriptor can easily be computed.
  217. * and need not to be stored here.
  218. */
  219. /* both are in the little endian format */
  220. *journal_table++ = desc.j_len;
  221. for (i = 0; i < __le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++)
  222. {
  223. /* both are in the little endian format */
  224. *journal_table++ = desc.j_realblock[i];
  225. #ifdef REISERDEBUG
  226. printf ("block %d is in journal %d.\n",
  227. __le32_to_cpu(desc.j_realblock[i]), desc_block);
  228. #endif
  229. }
  230. for ( ; i < __le32_to_cpu(desc.j_len); i++)
  231. {
  232. /* both are in the little endian format */
  233. *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];
  234. #ifdef REISERDEBUG
  235. printf ("block %d is in journal %d.\n",
  236. __le32_to_cpu(commit.j_realblock[i-JOURNAL_TRANS_HALF]),
  237. desc_block);
  238. #endif
  239. }
  240. }
  241. }
  242. desc_block = (commit_block + 1) & (block_count - 1);
  243. }
  244. #ifdef REISERDEBUG
  245. printf ("Transaction %d/%d at %d isn't valid.\n",
  246. __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
  247. #endif
  248. INFO->journal_transactions
  249. = next_trans_id - __le32_to_cpu(header.j_last_flush_trans_id) - 1;
  250. return errnum == 0;
  251. }
  252. /* check filesystem types and read superblock into memory buffer */
  253. int
  254. reiserfs_mount (unsigned part_length)
  255. {
  256. struct reiserfs_super_block super;
  257. int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
  258. if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
  259. || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
  260. (char *) &super)
  261. || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  262. && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  263. && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
  264. || (/* check that this is not a copy inside the journal log */
  265. sb_journal_block(&super) * sb_blocksize(&super)
  266. <= REISERFS_DISK_OFFSET_IN_BYTES))
  267. {
  268. /* Try old super block position */
  269. superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
  270. if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
  271. || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
  272. (char *) &super))
  273. return 0;
  274. if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  275. && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
  276. {
  277. /* pre journaling super block ? */
  278. if (substring (REISERFS_SUPER_MAGIC_STRING,
  279. (char*) ((int) &super + 20)) > 0)
  280. return 0;
  281. set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
  282. set_sb_journal_block(&super, 0);
  283. set_sb_version(&super, 0);
  284. }
  285. }
  286. /* check the version number. */
  287. if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
  288. return 0;
  289. INFO->version = sb_version(&super);
  290. INFO->blocksize = sb_blocksize(&super);
  291. INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
  292. INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
  293. INFO->cached_slots =
  294. (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
  295. #ifdef REISERDEBUG
  296. printf ("reiserfs_mount: version=%d, blocksize=%d\n",
  297. INFO->version, INFO->blocksize);
  298. #endif /* REISERDEBUG */
  299. /* Clear node cache. */
  300. memset (INFO->blocks, 0, sizeof (INFO->blocks));
  301. if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
  302. || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
  303. || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
  304. return 0;
  305. /* Initialize journal code. If something fails we end with zero
  306. * journal_transactions, so we don't access the journal at all.
  307. */
  308. INFO->journal_transactions = 0;
  309. if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
  310. {
  311. INFO->journal_block = sb_journal_block(&super);
  312. INFO->journal_block_count = sb_journal_size(&super);
  313. if (is_power_of_two (INFO->journal_block_count))
  314. journal_init ();
  315. /* Read in super block again, maybe it is in the journal */
  316. block_read (superblock >> INFO->blocksize_shift,
  317. 0, sizeof (struct reiserfs_super_block), (char *) &super);
  318. }
  319. if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
  320. return 0;
  321. INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (ROOT)->blk_level);
  322. #ifdef REISERDEBUG
  323. printf ("root read_in: block=%d, depth=%d\n",
  324. sb_root_block(&super), INFO->tree_depth);
  325. #endif /* REISERDEBUG */
  326. if (INFO->tree_depth >= MAX_HEIGHT)
  327. return 0;
  328. if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
  329. {
  330. /* There is only one node in the whole filesystem,
  331. * which is simultanously leaf and root */
  332. memcpy (LEAF, ROOT, INFO->blocksize);
  333. }
  334. return 1;
  335. }
  336. /***************** TREE ACCESSING METHODS *****************************/
  337. /* I assume you are familiar with the ReiserFS tree, if not go to
  338. * http://www.namesys.com/content_table.html
  339. *
  340. * My tree node cache is organized as following
  341. * 0 ROOT node
  342. * 1 LEAF node (if the ROOT is also a LEAF it is copied here
  343. * 2-n other nodes on current path from bottom to top.
  344. * if there is not enough space in the cache, the top most are
  345. * omitted.
  346. *
  347. * I have only two methods to find a key in the tree:
  348. * search_stat(dir_id, objectid) searches for the stat entry (always
  349. * the first entry) of an object.
  350. * next_key() gets the next key in tree order.
  351. *
  352. * This means, that I can only sequential reads of files are
  353. * efficient, but this really doesn't hurt for grub.
  354. */
  355. /* Read in the node at the current path and depth into the node cache.
  356. * You must set INFO->blocks[depth] before.
  357. */
  358. static char *
  359. read_tree_node (unsigned int blockNr, int depth)
  360. {
  361. char* cache = CACHE(depth);
  362. int num_cached = INFO->cached_slots;
  363. if (depth < num_cached)
  364. {
  365. /* This is the cached part of the path. Check if same block is
  366. * needed.
  367. */
  368. if (blockNr == INFO->blocks[depth])
  369. return cache;
  370. }
  371. else
  372. cache = CACHE(num_cached);
  373. #ifdef REISERDEBUG
  374. printf (" next read_in: block=%d (depth=%d)\n",
  375. blockNr, depth);
  376. #endif /* REISERDEBUG */
  377. if (! block_read (blockNr, 0, INFO->blocksize, cache))
  378. return 0;
  379. /* Make sure it has the right node level */
  380. if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
  381. {
  382. errnum = ERR_FSYS_CORRUPT;
  383. return 0;
  384. }
  385. INFO->blocks[depth] = blockNr;
  386. return cache;
  387. }
  388. /* Get the next key, i.e. the key following the last retrieved key in
  389. * tree order. INFO->current_ih and
  390. * INFO->current_info are adapted accordingly. */
  391. static int
  392. next_key (void)
  393. {
  394. int depth;
  395. struct item_head *ih = INFO->current_ih + 1;
  396. char *cache;
  397. #ifdef REISERDEBUG
  398. printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n",
  399. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  400. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  401. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  402. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  403. __le16_to_cpu(INFO->current_ih->ih_version));
  404. #endif /* REISERDEBUG */
  405. if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
  406. {
  407. depth = DISK_LEAF_NODE_LEVEL;
  408. /* The last item, was the last in the leaf node.
  409. * Read in the next block
  410. */
  411. do
  412. {
  413. if (depth == INFO->tree_depth)
  414. {
  415. /* There are no more keys at all.
  416. * Return a dummy item with MAX_KEY */
  417. ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
  418. goto found;
  419. }
  420. depth++;
  421. #ifdef REISERDEBUG
  422. printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
  423. #endif /* REISERDEBUG */
  424. }
  425. while (INFO->next_key_nr[depth] == 0);
  426. if (depth == INFO->tree_depth)
  427. cache = ROOT;
  428. else if (depth <= INFO->cached_slots)
  429. cache = CACHE (depth);
  430. else
  431. {
  432. cache = read_tree_node (INFO->blocks[depth], depth);
  433. if (! cache)
  434. return 0;
  435. }
  436. do
  437. {
  438. int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
  439. int key_nr = INFO->next_key_nr[depth]++;
  440. #ifdef REISERDEBUG
  441. printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
  442. #endif /* REISERDEBUG */
  443. if (key_nr == nr_item)
  444. /* This is the last item in this block, set the next_key_nr to 0 */
  445. INFO->next_key_nr[depth] = 0;
  446. cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
  447. if (! cache)
  448. return 0;
  449. }
  450. while (depth > DISK_LEAF_NODE_LEVEL);
  451. ih = ITEMHEAD;
  452. }
  453. found:
  454. INFO->current_ih = ih;
  455. INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
  456. #ifdef REISERDEBUG
  457. printf (" new ih: key %d:%d:%d:%d version:%d\n",
  458. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  459. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  460. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  461. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  462. __le16_to_cpu(INFO->current_ih->ih_version));
  463. #endif /* REISERDEBUG */
  464. return 1;
  465. }
  466. /* preconditions: reiserfs_mount already executed, therefore
  467. * INFO block is valid
  468. * returns: 0 if error (errnum is set),
  469. * nonzero iff we were able to find the key successfully.
  470. * postconditions: on a nonzero return, the current_ih and
  471. * current_item fields describe the key that equals the
  472. * searched key. INFO->next_key contains the next key after
  473. * the searched key.
  474. * side effects: messes around with the cache.
  475. */
  476. static int
  477. search_stat (__u32 dir_id, __u32 objectid)
  478. {
  479. char *cache;
  480. int depth;
  481. int nr_item;
  482. int i;
  483. struct item_head *ih;
  484. #ifdef REISERDEBUG
  485. printf ("search_stat:\n key %d:%d:0:0\n", dir_id, objectid);
  486. #endif /* REISERDEBUG */
  487. depth = INFO->tree_depth;
  488. cache = ROOT;
  489. while (depth > DISK_LEAF_NODE_LEVEL)
  490. {
  491. struct key *key;
  492. nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
  493. key = KEY (cache);
  494. for (i = 0; i < nr_item; i++)
  495. {
  496. if (__le32_to_cpu(key->k_dir_id) > dir_id
  497. || (__le32_to_cpu(key->k_dir_id) == dir_id
  498. && (__le32_to_cpu(key->k_objectid) > objectid
  499. || (__le32_to_cpu(key->k_objectid) == objectid
  500. && (__le32_to_cpu(key->u.v1.k_offset)
  501. | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
  502. break;
  503. key++;
  504. }
  505. #ifdef REISERDEBUG
  506. printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
  507. #endif /* REISERDEBUG */
  508. INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
  509. cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
  510. if (! cache)
  511. return 0;
  512. }
  513. /* cache == LEAF */
  514. nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
  515. ih = ITEMHEAD;
  516. for (i = 0; i < nr_item; i++)
  517. {
  518. if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
  519. && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
  520. && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
  521. && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
  522. {
  523. #ifdef REISERDEBUG
  524. printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
  525. #endif /* REISERDEBUG */
  526. INFO->current_ih = ih;
  527. INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
  528. return 1;
  529. }
  530. ih++;
  531. }
  532. errnum = ERR_FSYS_CORRUPT;
  533. return 0;
  534. }
  535. int
  536. reiserfs_read (char *buf, unsigned len)
  537. {
  538. unsigned int blocksize;
  539. unsigned int offset;
  540. unsigned int to_read;
  541. char *prev_buf = buf;
  542. #ifdef REISERDEBUG
  543. printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
  544. filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
  545. #endif /* REISERDEBUG */
  546. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
  547. || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
  548. {
  549. search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
  550. goto get_next_key;
  551. }
  552. while (! errnum)
  553. {
  554. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
  555. break;
  556. }
  557. offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
  558. blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
  559. #ifdef REISERDEBUG
  560. printf (" loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
  561. filepos, len, offset, blocksize);
  562. #endif /* REISERDEBUG */
  563. if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
  564. && offset < blocksize)
  565. {
  566. #ifdef REISERDEBUG
  567. printf ("direct_read: offset=%d, blocksize=%d\n",
  568. offset, blocksize);
  569. #endif /* REISERDEBUG */
  570. to_read = blocksize - offset;
  571. if (to_read > len)
  572. to_read = len;
  573. memcpy (buf, INFO->current_item + offset, to_read);
  574. goto update_buf_len;
  575. }
  576. else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
  577. {
  578. blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
  579. #ifdef REISERDEBUG
  580. printf ("indirect_read: offset=%d, blocksize=%d\n",
  581. offset, blocksize);
  582. #endif /* REISERDEBUG */
  583. while (offset < blocksize)
  584. {
  585. __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
  586. [offset >> INFO->fullblocksize_shift]);
  587. int blk_offset = offset & (INFO->blocksize-1);
  588. to_read = INFO->blocksize - blk_offset;
  589. if (to_read > len)
  590. to_read = len;
  591. /* Journal is only for meta data. Data blocks can be read
  592. * directly without using block_read
  593. */
  594. reiserfs_devread (blocknr << INFO->blocksize_shift,
  595. blk_offset, to_read, buf);
  596. update_buf_len:
  597. len -= to_read;
  598. buf += to_read;
  599. offset += to_read;
  600. filepos += to_read;
  601. if (len == 0)
  602. goto done;
  603. }
  604. }
  605. get_next_key:
  606. next_key ();
  607. }
  608. done:
  609. return errnum ? 0 : buf - prev_buf;
  610. }
  611. /* preconditions: reiserfs_mount already executed, therefore
  612. * INFO block is valid
  613. * returns: 0 if error, nonzero iff we were able to find the file successfully
  614. * postconditions: on a nonzero return, INFO->fileinfo contains the info
  615. * of the file we were trying to look up, filepos is 0 and filemax is
  616. * the size of the file.
  617. */
  618. static int
  619. reiserfs_dir (char *dirname)
  620. {
  621. struct reiserfs_de_head *de_head;
  622. char *rest, ch;
  623. __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
  624. #ifndef STAGE1_5
  625. int do_possibilities = 0;
  626. #endif /* ! STAGE1_5 */
  627. char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
  628. int link_count = 0;
  629. int mode;
  630. dir_id = REISERFS_ROOT_PARENT_OBJECTID;
  631. objectid = REISERFS_ROOT_OBJECTID;
  632. while (1)
  633. {
  634. #ifdef REISERDEBUG
  635. printf ("dirname=%s\n", dirname);
  636. #endif /* REISERDEBUG */
  637. /* Search for the stat info first. */
  638. if (! search_stat (dir_id, objectid))
  639. return 0;
  640. #ifdef REISERDEBUG
  641. printf ("sd_mode=%x sd_size=%d\n",
  642. stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
  643. sd_v2_mode((struct stat_data *) (INFO->current_item)),
  644. stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  645. sd_v2_size((struct stat_data *) INFO->current_item)
  646. );
  647. #endif /* REISERDEBUG */
  648. mode = stat_data_v1(INFO->current_ih) ?
  649. sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
  650. sd_v2_mode((struct stat_data *) INFO->current_item);
  651. /* If we've got a symbolic link, then chase it. */
  652. if (S_ISLNK (mode))
  653. {
  654. unsigned int len;
  655. if (++link_count > MAX_LINK_COUNT)
  656. {
  657. errnum = ERR_SYMLINK_LOOP;
  658. return 0;
  659. }
  660. /* Get the symlink size. */
  661. filemax = stat_data_v1(INFO->current_ih) ?
  662. sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  663. sd_v2_size((struct stat_data *) INFO->current_item);
  664. /* Find out how long our remaining name is. */
  665. len = 0;
  666. while (dirname[len] && !isspace (dirname[len]))
  667. len++;
  668. if (filemax + len > sizeof (linkbuf) - 1)
  669. {
  670. errnum = ERR_FILELENGTH;
  671. return 0;
  672. }
  673. /* Copy the remaining name to the end of the symlink data.
  674. Note that DIRNAME and LINKBUF may overlap! */
  675. memmove (linkbuf + filemax, dirname, len+1);
  676. INFO->fileinfo.k_dir_id = dir_id;
  677. INFO->fileinfo.k_objectid = objectid;
  678. filepos = 0;
  679. if (! next_key ()
  680. || reiserfs_read (linkbuf, filemax) != filemax)
  681. {
  682. if (! errnum)
  683. errnum = ERR_FSYS_CORRUPT;
  684. return 0;
  685. }
  686. #ifdef REISERDEBUG
  687. printf ("symlink=%s\n", linkbuf);
  688. #endif /* REISERDEBUG */
  689. dirname = linkbuf;
  690. if (*dirname == '/')
  691. {
  692. /* It's an absolute link, so look it up in root. */
  693. dir_id = REISERFS_ROOT_PARENT_OBJECTID;
  694. objectid = REISERFS_ROOT_OBJECTID;
  695. }
  696. else
  697. {
  698. /* Relative, so look it up in our parent directory. */
  699. dir_id = parent_dir_id;
  700. objectid = parent_objectid;
  701. }
  702. /* Now lookup the new name. */
  703. continue;
  704. }
  705. /* if we have a real file (and we're not just printing possibilities),
  706. then this is where we want to exit */
  707. if (! *dirname || isspace (*dirname))
  708. {
  709. if (! S_ISREG (mode))
  710. {
  711. errnum = ERR_BAD_FILETYPE;
  712. return 0;
  713. }
  714. filepos = 0;
  715. filemax = stat_data_v1(INFO->current_ih) ?
  716. sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  717. sd_v2_size((struct stat_data *) INFO->current_item);
  718. #if 0
  719. /* If this is a new stat data and size is > 4GB set filemax to
  720. * maximum
  721. */
  722. if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
  723. && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
  724. filemax = 0xffffffff;
  725. #endif
  726. INFO->fileinfo.k_dir_id = dir_id;
  727. INFO->fileinfo.k_objectid = objectid;
  728. return next_key ();
  729. }
  730. /* continue with the file/directory name interpretation */
  731. while (*dirname == '/')
  732. dirname++;
  733. if (! S_ISDIR (mode))
  734. {
  735. errnum = ERR_BAD_FILETYPE;
  736. return 0;
  737. }
  738. for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
  739. *rest = 0;
  740. # ifndef STAGE1_5
  741. if (print_possibilities && ch != '/')
  742. do_possibilities = 1;
  743. # endif /* ! STAGE1_5 */
  744. while (1)
  745. {
  746. char *name_end;
  747. int num_entries;
  748. if (! next_key ())
  749. return 0;
  750. #ifdef REISERDEBUG
  751. printf ("ih: key %d:%d:%d:%d version:%d\n",
  752. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  753. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  754. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  755. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  756. __le16_to_cpu(INFO->current_ih->ih_version));
  757. #endif /* REISERDEBUG */
  758. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
  759. break;
  760. name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
  761. de_head = (struct reiserfs_de_head *) INFO->current_item;
  762. num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
  763. while (num_entries > 0)
  764. {
  765. char *filename = INFO->current_item + deh_location(de_head);
  766. char tmp = *name_end;
  767. if ((deh_state(de_head) & DEH_Visible))
  768. {
  769. int cmp;
  770. /* Directory names in ReiserFS are not null
  771. * terminated. We write a temporary 0 behind it.
  772. * NOTE: that this may overwrite the first block in
  773. * the tree cache. That doesn't hurt as long as we
  774. * don't call next_key () in between.
  775. */
  776. *name_end = 0;
  777. cmp = substring (dirname, filename);
  778. *name_end = tmp;
  779. # ifndef STAGE1_5
  780. if (do_possibilities)
  781. {
  782. if (cmp <= 0)
  783. {
  784. char fn[PATH_MAX];
  785. struct fsys_reiser_info info_save;
  786. if (print_possibilities > 0)
  787. print_possibilities = -print_possibilities;
  788. *name_end = 0;
  789. strcpy(fn, filename);
  790. *name_end = tmp;
  791. /* If NAME is "." or "..", do not count it. */
  792. if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
  793. memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
  794. search_stat (deh_dir_id(de_head), deh_objectid(de_head));
  795. sd_print_item(INFO->current_ih, INFO->current_item);
  796. printf(" %s\n", fn);
  797. search_stat (dir_id, objectid);
  798. memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
  799. }
  800. }
  801. }
  802. else
  803. # endif /* ! STAGE1_5 */
  804. if (cmp == 0)
  805. goto found;
  806. }
  807. /* The beginning of this name marks the end of the next name.
  808. */
  809. name_end = filename;
  810. de_head++;
  811. num_entries--;
  812. }
  813. }
  814. # ifndef STAGE1_5
  815. if (print_possibilities < 0)
  816. return 1;
  817. # endif /* ! STAGE1_5 */
  818. errnum = ERR_FILE_NOT_FOUND;
  819. *rest = ch;
  820. return 0;
  821. found:
  822. *rest = ch;
  823. dirname = rest;
  824. parent_dir_id = dir_id;
  825. parent_objectid = objectid;
  826. dir_id = deh_dir_id(de_head);
  827. objectid = deh_objectid(de_head);
  828. }
  829. }
  830. /*
  831. * U-Boot interface functions
  832. */
  833. /*
  834. * List given directory
  835. *
  836. * RETURN: 0 - OK, else grub_error_t errnum
  837. */
  838. int
  839. reiserfs_ls (char *dirname)
  840. {
  841. char *dir_slash;
  842. int res;
  843. errnum = 0;
  844. dir_slash = malloc(strlen(dirname) + 1);
  845. if (dir_slash == NULL) {
  846. return ERR_NUMBER_OVERFLOW;
  847. }
  848. strcpy(dir_slash, dirname);
  849. /* add "/" to the directory name */
  850. strcat(dir_slash, "/");
  851. print_possibilities = 1;
  852. res = reiserfs_dir (dir_slash);
  853. free(dir_slash);
  854. if (!res || errnum) {
  855. return errnum;
  856. }
  857. return 0;
  858. }
  859. /*
  860. * Open file for reading
  861. *
  862. * RETURN: >0 - OK, size of opened file
  863. * <0 - ERROR -grub_error_t errnum
  864. */
  865. int
  866. reiserfs_open (char *filename)
  867. {
  868. /* open the file */
  869. errnum = 0;
  870. print_possibilities = 0;
  871. if (!reiserfs_dir (filename) || errnum) {
  872. return -errnum;
  873. }
  874. return filemax;
  875. }
  876. #endif /* CFG_CMD_REISER */