reiserfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
  258. || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
  259. (char *) &super)
  260. || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  261. && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  262. && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
  263. || (/* check that this is not a copy inside the journal log */
  264. sb_journal_block(&super) * sb_blocksize(&super)
  265. <= REISERFS_DISK_OFFSET_IN_BYTES))
  266. {
  267. /* Try old super block position */
  268. superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
  269. if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
  270. || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
  271. (char *) &super))
  272. return 0;
  273. if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
  274. && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
  275. {
  276. /* pre journaling super block ? */
  277. if (substring (REISERFS_SUPER_MAGIC_STRING,
  278. (char*) ((int) &super + 20)) > 0)
  279. return 0;
  280. set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
  281. set_sb_journal_block(&super, 0);
  282. set_sb_version(&super, 0);
  283. }
  284. }
  285. /* check the version number. */
  286. if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
  287. return 0;
  288. INFO->version = sb_version(&super);
  289. INFO->blocksize = sb_blocksize(&super);
  290. INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
  291. INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
  292. INFO->cached_slots =
  293. (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
  294. #ifdef REISERDEBUG
  295. printf ("reiserfs_mount: version=%d, blocksize=%d\n",
  296. INFO->version, INFO->blocksize);
  297. #endif /* REISERDEBUG */
  298. /* Clear node cache. */
  299. memset (INFO->blocks, 0, sizeof (INFO->blocks));
  300. if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
  301. || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
  302. || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
  303. return 0;
  304. /* Initialize journal code. If something fails we end with zero
  305. * journal_transactions, so we don't access the journal at all.
  306. */
  307. INFO->journal_transactions = 0;
  308. if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
  309. {
  310. INFO->journal_block = sb_journal_block(&super);
  311. INFO->journal_block_count = sb_journal_size(&super);
  312. if (is_power_of_two (INFO->journal_block_count))
  313. journal_init ();
  314. /* Read in super block again, maybe it is in the journal */
  315. block_read (superblock >> INFO->blocksize_shift,
  316. 0, sizeof (struct reiserfs_super_block), (char *) &super);
  317. }
  318. if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
  319. return 0;
  320. INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (ROOT)->blk_level);
  321. #ifdef REISERDEBUG
  322. printf ("root read_in: block=%d, depth=%d\n",
  323. sb_root_block(&super), INFO->tree_depth);
  324. #endif /* REISERDEBUG */
  325. if (INFO->tree_depth >= MAX_HEIGHT)
  326. return 0;
  327. if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
  328. {
  329. /* There is only one node in the whole filesystem,
  330. * which is simultanously leaf and root */
  331. memcpy (LEAF, ROOT, INFO->blocksize);
  332. }
  333. return 1;
  334. }
  335. /***************** TREE ACCESSING METHODS *****************************/
  336. /* I assume you are familiar with the ReiserFS tree, if not go to
  337. * http://www.namesys.com/content_table.html
  338. *
  339. * My tree node cache is organized as following
  340. * 0 ROOT node
  341. * 1 LEAF node (if the ROOT is also a LEAF it is copied here
  342. * 2-n other nodes on current path from bottom to top.
  343. * if there is not enough space in the cache, the top most are
  344. * omitted.
  345. *
  346. * I have only two methods to find a key in the tree:
  347. * search_stat(dir_id, objectid) searches for the stat entry (always
  348. * the first entry) of an object.
  349. * next_key() gets the next key in tree order.
  350. *
  351. * This means, that I can only sequential reads of files are
  352. * efficient, but this really doesn't hurt for grub.
  353. */
  354. /* Read in the node at the current path and depth into the node cache.
  355. * You must set INFO->blocks[depth] before.
  356. */
  357. static char *
  358. read_tree_node (unsigned int blockNr, int depth)
  359. {
  360. char* cache = CACHE(depth);
  361. int num_cached = INFO->cached_slots;
  362. if (depth < num_cached)
  363. {
  364. /* This is the cached part of the path. Check if same block is
  365. * needed.
  366. */
  367. if (blockNr == INFO->blocks[depth])
  368. return cache;
  369. }
  370. else
  371. cache = CACHE(num_cached);
  372. #ifdef REISERDEBUG
  373. printf (" next read_in: block=%d (depth=%d)\n",
  374. blockNr, depth);
  375. #endif /* REISERDEBUG */
  376. if (! block_read (blockNr, 0, INFO->blocksize, cache))
  377. return 0;
  378. /* Make sure it has the right node level */
  379. if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
  380. {
  381. errnum = ERR_FSYS_CORRUPT;
  382. return 0;
  383. }
  384. INFO->blocks[depth] = blockNr;
  385. return cache;
  386. }
  387. /* Get the next key, i.e. the key following the last retrieved key in
  388. * tree order. INFO->current_ih and
  389. * INFO->current_info are adapted accordingly. */
  390. static int
  391. next_key (void)
  392. {
  393. int depth;
  394. struct item_head *ih = INFO->current_ih + 1;
  395. char *cache;
  396. #ifdef REISERDEBUG
  397. printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n",
  398. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  399. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  400. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  401. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  402. __le16_to_cpu(INFO->current_ih->ih_version));
  403. #endif /* REISERDEBUG */
  404. if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
  405. {
  406. depth = DISK_LEAF_NODE_LEVEL;
  407. /* The last item, was the last in the leaf node.
  408. * Read in the next block
  409. */
  410. do
  411. {
  412. if (depth == INFO->tree_depth)
  413. {
  414. /* There are no more keys at all.
  415. * Return a dummy item with MAX_KEY */
  416. ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
  417. goto found;
  418. }
  419. depth++;
  420. #ifdef REISERDEBUG
  421. printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
  422. #endif /* REISERDEBUG */
  423. }
  424. while (INFO->next_key_nr[depth] == 0);
  425. if (depth == INFO->tree_depth)
  426. cache = ROOT;
  427. else if (depth <= INFO->cached_slots)
  428. cache = CACHE (depth);
  429. else
  430. {
  431. cache = read_tree_node (INFO->blocks[depth], depth);
  432. if (! cache)
  433. return 0;
  434. }
  435. do
  436. {
  437. int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
  438. int key_nr = INFO->next_key_nr[depth]++;
  439. #ifdef REISERDEBUG
  440. printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
  441. #endif /* REISERDEBUG */
  442. if (key_nr == nr_item)
  443. /* This is the last item in this block, set the next_key_nr to 0 */
  444. INFO->next_key_nr[depth] = 0;
  445. cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
  446. if (! cache)
  447. return 0;
  448. }
  449. while (depth > DISK_LEAF_NODE_LEVEL);
  450. ih = ITEMHEAD;
  451. }
  452. found:
  453. INFO->current_ih = ih;
  454. INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
  455. #ifdef REISERDEBUG
  456. printf (" new ih: key %d:%d:%d:%d version:%d\n",
  457. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  458. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  459. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  460. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  461. __le16_to_cpu(INFO->current_ih->ih_version));
  462. #endif /* REISERDEBUG */
  463. return 1;
  464. }
  465. /* preconditions: reiserfs_mount already executed, therefore
  466. * INFO block is valid
  467. * returns: 0 if error (errnum is set),
  468. * nonzero iff we were able to find the key successfully.
  469. * postconditions: on a nonzero return, the current_ih and
  470. * current_item fields describe the key that equals the
  471. * searched key. INFO->next_key contains the next key after
  472. * the searched key.
  473. * side effects: messes around with the cache.
  474. */
  475. static int
  476. search_stat (__u32 dir_id, __u32 objectid)
  477. {
  478. char *cache;
  479. int depth;
  480. int nr_item;
  481. int i;
  482. struct item_head *ih;
  483. #ifdef REISERDEBUG
  484. printf ("search_stat:\n key %d:%d:0:0\n", dir_id, objectid);
  485. #endif /* REISERDEBUG */
  486. depth = INFO->tree_depth;
  487. cache = ROOT;
  488. while (depth > DISK_LEAF_NODE_LEVEL)
  489. {
  490. struct key *key;
  491. nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
  492. key = KEY (cache);
  493. for (i = 0; i < nr_item; i++)
  494. {
  495. if (__le32_to_cpu(key->k_dir_id) > dir_id
  496. || (__le32_to_cpu(key->k_dir_id) == dir_id
  497. && (__le32_to_cpu(key->k_objectid) > objectid
  498. || (__le32_to_cpu(key->k_objectid) == objectid
  499. && (__le32_to_cpu(key->u.v1.k_offset)
  500. | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
  501. break;
  502. key++;
  503. }
  504. #ifdef REISERDEBUG
  505. printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
  506. #endif /* REISERDEBUG */
  507. INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
  508. cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
  509. if (! cache)
  510. return 0;
  511. }
  512. /* cache == LEAF */
  513. nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
  514. ih = ITEMHEAD;
  515. for (i = 0; i < nr_item; i++)
  516. {
  517. if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
  518. && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
  519. && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
  520. && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
  521. {
  522. #ifdef REISERDEBUG
  523. printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
  524. #endif /* REISERDEBUG */
  525. INFO->current_ih = ih;
  526. INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
  527. return 1;
  528. }
  529. ih++;
  530. }
  531. errnum = ERR_FSYS_CORRUPT;
  532. return 0;
  533. }
  534. int
  535. reiserfs_read (char *buf, unsigned len)
  536. {
  537. unsigned int blocksize;
  538. unsigned int offset;
  539. unsigned int to_read;
  540. char *prev_buf = buf;
  541. #ifdef REISERDEBUG
  542. printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
  543. filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
  544. #endif /* REISERDEBUG */
  545. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
  546. || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
  547. {
  548. search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
  549. goto get_next_key;
  550. }
  551. while (! errnum)
  552. {
  553. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
  554. break;
  555. }
  556. offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
  557. blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
  558. #ifdef REISERDEBUG
  559. printf (" loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
  560. filepos, len, offset, blocksize);
  561. #endif /* REISERDEBUG */
  562. if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
  563. && offset < blocksize)
  564. {
  565. #ifdef REISERDEBUG
  566. printf ("direct_read: offset=%d, blocksize=%d\n",
  567. offset, blocksize);
  568. #endif /* REISERDEBUG */
  569. to_read = blocksize - offset;
  570. if (to_read > len)
  571. to_read = len;
  572. memcpy (buf, INFO->current_item + offset, to_read);
  573. goto update_buf_len;
  574. }
  575. else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
  576. {
  577. blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
  578. #ifdef REISERDEBUG
  579. printf ("indirect_read: offset=%d, blocksize=%d\n",
  580. offset, blocksize);
  581. #endif /* REISERDEBUG */
  582. while (offset < blocksize)
  583. {
  584. __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
  585. [offset >> INFO->fullblocksize_shift]);
  586. int blk_offset = offset & (INFO->blocksize-1);
  587. to_read = INFO->blocksize - blk_offset;
  588. if (to_read > len)
  589. to_read = len;
  590. /* Journal is only for meta data. Data blocks can be read
  591. * directly without using block_read
  592. */
  593. reiserfs_devread (blocknr << INFO->blocksize_shift,
  594. blk_offset, to_read, buf);
  595. update_buf_len:
  596. len -= to_read;
  597. buf += to_read;
  598. offset += to_read;
  599. filepos += to_read;
  600. if (len == 0)
  601. goto done;
  602. }
  603. }
  604. get_next_key:
  605. next_key ();
  606. }
  607. done:
  608. return errnum ? 0 : buf - prev_buf;
  609. }
  610. /* preconditions: reiserfs_mount already executed, therefore
  611. * INFO block is valid
  612. * returns: 0 if error, nonzero iff we were able to find the file successfully
  613. * postconditions: on a nonzero return, INFO->fileinfo contains the info
  614. * of the file we were trying to look up, filepos is 0 and filemax is
  615. * the size of the file.
  616. */
  617. static int
  618. reiserfs_dir (char *dirname)
  619. {
  620. struct reiserfs_de_head *de_head;
  621. char *rest, ch;
  622. __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
  623. #ifndef STAGE1_5
  624. int do_possibilities = 0;
  625. #endif /* ! STAGE1_5 */
  626. char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
  627. int link_count = 0;
  628. int mode;
  629. dir_id = REISERFS_ROOT_PARENT_OBJECTID;
  630. objectid = REISERFS_ROOT_OBJECTID;
  631. while (1)
  632. {
  633. #ifdef REISERDEBUG
  634. printf ("dirname=%s\n", dirname);
  635. #endif /* REISERDEBUG */
  636. /* Search for the stat info first. */
  637. if (! search_stat (dir_id, objectid))
  638. return 0;
  639. #ifdef REISERDEBUG
  640. printf ("sd_mode=%x sd_size=%d\n",
  641. stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
  642. sd_v2_mode((struct stat_data *) (INFO->current_item)),
  643. stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  644. sd_v2_size((struct stat_data *) INFO->current_item)
  645. );
  646. #endif /* REISERDEBUG */
  647. mode = stat_data_v1(INFO->current_ih) ?
  648. sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
  649. sd_v2_mode((struct stat_data *) INFO->current_item);
  650. /* If we've got a symbolic link, then chase it. */
  651. if (S_ISLNK (mode))
  652. {
  653. unsigned int len;
  654. if (++link_count > MAX_LINK_COUNT)
  655. {
  656. errnum = ERR_SYMLINK_LOOP;
  657. return 0;
  658. }
  659. /* Get the symlink size. */
  660. filemax = stat_data_v1(INFO->current_ih) ?
  661. sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  662. sd_v2_size((struct stat_data *) INFO->current_item);
  663. /* Find out how long our remaining name is. */
  664. len = 0;
  665. while (dirname[len] && !isspace (dirname[len]))
  666. len++;
  667. if (filemax + len > sizeof (linkbuf) - 1)
  668. {
  669. errnum = ERR_FILELENGTH;
  670. return 0;
  671. }
  672. /* Copy the remaining name to the end of the symlink data.
  673. Note that DIRNAME and LINKBUF may overlap! */
  674. memmove (linkbuf + filemax, dirname, len+1);
  675. INFO->fileinfo.k_dir_id = dir_id;
  676. INFO->fileinfo.k_objectid = objectid;
  677. filepos = 0;
  678. if (! next_key ()
  679. || reiserfs_read (linkbuf, filemax) != filemax)
  680. {
  681. if (! errnum)
  682. errnum = ERR_FSYS_CORRUPT;
  683. return 0;
  684. }
  685. #ifdef REISERDEBUG
  686. printf ("symlink=%s\n", linkbuf);
  687. #endif /* REISERDEBUG */
  688. dirname = linkbuf;
  689. if (*dirname == '/')
  690. {
  691. /* It's an absolute link, so look it up in root. */
  692. dir_id = REISERFS_ROOT_PARENT_OBJECTID;
  693. objectid = REISERFS_ROOT_OBJECTID;
  694. }
  695. else
  696. {
  697. /* Relative, so look it up in our parent directory. */
  698. dir_id = parent_dir_id;
  699. objectid = parent_objectid;
  700. }
  701. /* Now lookup the new name. */
  702. continue;
  703. }
  704. /* if we have a real file (and we're not just printing possibilities),
  705. then this is where we want to exit */
  706. if (! *dirname || isspace (*dirname))
  707. {
  708. if (! S_ISREG (mode))
  709. {
  710. errnum = ERR_BAD_FILETYPE;
  711. return 0;
  712. }
  713. filepos = 0;
  714. filemax = stat_data_v1(INFO->current_ih) ?
  715. sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
  716. sd_v2_size((struct stat_data *) INFO->current_item);
  717. #if 0
  718. /* If this is a new stat data and size is > 4GB set filemax to
  719. * maximum
  720. */
  721. if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
  722. && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
  723. filemax = 0xffffffff;
  724. #endif
  725. INFO->fileinfo.k_dir_id = dir_id;
  726. INFO->fileinfo.k_objectid = objectid;
  727. return next_key ();
  728. }
  729. /* continue with the file/directory name interpretation */
  730. while (*dirname == '/')
  731. dirname++;
  732. if (! S_ISDIR (mode))
  733. {
  734. errnum = ERR_BAD_FILETYPE;
  735. return 0;
  736. }
  737. for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
  738. *rest = 0;
  739. # ifndef STAGE1_5
  740. if (print_possibilities && ch != '/')
  741. do_possibilities = 1;
  742. # endif /* ! STAGE1_5 */
  743. while (1)
  744. {
  745. char *name_end;
  746. int num_entries;
  747. if (! next_key ())
  748. return 0;
  749. #ifdef REISERDEBUG
  750. printf ("ih: key %d:%d:%d:%d version:%d\n",
  751. __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
  752. __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
  753. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
  754. __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
  755. __le16_to_cpu(INFO->current_ih->ih_version));
  756. #endif /* REISERDEBUG */
  757. if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
  758. break;
  759. name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
  760. de_head = (struct reiserfs_de_head *) INFO->current_item;
  761. num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
  762. while (num_entries > 0)
  763. {
  764. char *filename = INFO->current_item + deh_location(de_head);
  765. char tmp = *name_end;
  766. if ((deh_state(de_head) & DEH_Visible))
  767. {
  768. int cmp;
  769. /* Directory names in ReiserFS are not null
  770. * terminated. We write a temporary 0 behind it.
  771. * NOTE: that this may overwrite the first block in
  772. * the tree cache. That doesn't hurt as long as we
  773. * don't call next_key () in between.
  774. */
  775. *name_end = 0;
  776. cmp = substring (dirname, filename);
  777. *name_end = tmp;
  778. # ifndef STAGE1_5
  779. if (do_possibilities)
  780. {
  781. if (cmp <= 0)
  782. {
  783. char fn[PATH_MAX];
  784. struct fsys_reiser_info info_save;
  785. if (print_possibilities > 0)
  786. print_possibilities = -print_possibilities;
  787. *name_end = 0;
  788. strcpy(fn, filename);
  789. *name_end = tmp;
  790. /* If NAME is "." or "..", do not count it. */
  791. if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
  792. memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
  793. search_stat (deh_dir_id(de_head), deh_objectid(de_head));
  794. sd_print_item(INFO->current_ih, INFO->current_item);
  795. printf(" %s\n", fn);
  796. search_stat (dir_id, objectid);
  797. memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
  798. }
  799. }
  800. }
  801. else
  802. # endif /* ! STAGE1_5 */
  803. if (cmp == 0)
  804. goto found;
  805. }
  806. /* The beginning of this name marks the end of the next name.
  807. */
  808. name_end = filename;
  809. de_head++;
  810. num_entries--;
  811. }
  812. }
  813. # ifndef STAGE1_5
  814. if (print_possibilities < 0)
  815. return 1;
  816. # endif /* ! STAGE1_5 */
  817. errnum = ERR_FILE_NOT_FOUND;
  818. *rest = ch;
  819. return 0;
  820. found:
  821. *rest = ch;
  822. dirname = rest;
  823. parent_dir_id = dir_id;
  824. parent_objectid = objectid;
  825. dir_id = deh_dir_id(de_head);
  826. objectid = deh_objectid(de_head);
  827. }
  828. }
  829. /*
  830. * U-Boot interface functions
  831. */
  832. /*
  833. * List given directory
  834. *
  835. * RETURN: 0 - OK, else grub_error_t errnum
  836. */
  837. int
  838. reiserfs_ls (char *dirname)
  839. {
  840. char *dir_slash;
  841. int res;
  842. errnum = 0;
  843. dir_slash = malloc(strlen(dirname) + 1);
  844. if (dir_slash == NULL) {
  845. return ERR_NUMBER_OVERFLOW;
  846. }
  847. strcpy(dir_slash, dirname);
  848. /* add "/" to the directory name */
  849. strcat(dir_slash, "/");
  850. print_possibilities = 1;
  851. res = reiserfs_dir (dir_slash);
  852. free(dir_slash);
  853. if (!res || errnum) {
  854. return errnum;
  855. }
  856. return 0;
  857. }
  858. /*
  859. * Open file for reading
  860. *
  861. * RETURN: >0 - OK, size of opened file
  862. * <0 - ERROR -grub_error_t errnum
  863. */
  864. int
  865. reiserfs_open (char *filename)
  866. {
  867. /* open the file */
  868. errnum = 0;
  869. print_possibilities = 0;
  870. if (!reiserfs_dir (filename) || errnum) {
  871. return -errnum;
  872. }
  873. return filemax;
  874. }