reiserfs.c 27 KB

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