reiserfs.c 27 KB

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