reiserfs.c 28 KB

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