reiserfs.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /* reiserfs.c - ReiserFS versions up to 3.6 */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. TODO:
  21. implement journal handling (ram replay)
  22. test tail packing & direct files
  23. validate partition label position
  24. */
  25. #if 0
  26. # define GRUB_REISERFS_DEBUG
  27. # define GRUB_REISERFS_JOURNALING
  28. # define GRUB_HEXDUMP
  29. #endif
  30. #include <grub/err.h>
  31. #include <grub/file.h>
  32. #include <grub/mm.h>
  33. #include <grub/misc.h>
  34. #include <grub/disk.h>
  35. #include <grub/dl.h>
  36. #include <grub/types.h>
  37. #include <grub/fshelp.h>
  38. #include <grub/i18n.h>
  39. GRUB_MOD_LICENSE ("GPLv3+");
  40. #define MIN(a, b) \
  41. ({ typeof (a) _a = (a); \
  42. typeof (b) _b = (b); \
  43. _a < _b ? _a : _b; })
  44. #define MAX(a, b) \
  45. ({ typeof (a) _a = (a); \
  46. typeof (b) _b = (b); \
  47. _a > _b ? _a : _b; })
  48. #define REISERFS_SUPER_BLOCK_OFFSET 0x10000
  49. #define REISERFS_MAGIC_LEN 12
  50. #define REISERFS_MAGIC_STRING "ReIsEr"
  51. #define REISERFS_MAGIC_DESC_BLOCK "ReIsErLB"
  52. /* If the 3rd bit of an item state is set, then it's visible. */
  53. #define GRUB_REISERFS_VISIBLE_MASK ((grub_uint16_t) 0x04)
  54. #define S_IFLNK 0xA000
  55. static grub_dl_t my_mod;
  56. #define assert(boolean) real_assert (boolean, GRUB_FILE, __LINE__)
  57. static inline void
  58. real_assert (int boolean, const char *file, const int line)
  59. {
  60. if (! boolean)
  61. grub_printf ("Assertion failed at %s:%d\n", file, line);
  62. }
  63. enum grub_reiserfs_item_type
  64. {
  65. GRUB_REISERFS_STAT,
  66. GRUB_REISERFS_DIRECTORY,
  67. GRUB_REISERFS_DIRECT,
  68. GRUB_REISERFS_INDIRECT,
  69. /* Matches both _DIRECT and _INDIRECT when searching. */
  70. GRUB_REISERFS_ANY,
  71. GRUB_REISERFS_UNKNOWN
  72. };
  73. struct grub_reiserfs_superblock
  74. {
  75. grub_uint32_t block_count;
  76. grub_uint32_t block_free_count;
  77. grub_uint32_t root_block;
  78. grub_uint32_t journal_block;
  79. grub_uint32_t journal_device;
  80. grub_uint32_t journal_original_size;
  81. grub_uint32_t journal_max_transaction_size;
  82. grub_uint32_t journal_block_count;
  83. grub_uint32_t journal_max_batch;
  84. grub_uint32_t journal_max_commit_age;
  85. grub_uint32_t journal_max_transaction_age;
  86. grub_uint16_t block_size;
  87. grub_uint16_t oid_max_size;
  88. grub_uint16_t oid_current_size;
  89. grub_uint16_t state;
  90. grub_uint8_t magic_string[REISERFS_MAGIC_LEN];
  91. grub_uint32_t function_hash_code;
  92. grub_uint16_t tree_height;
  93. grub_uint16_t bitmap_number;
  94. grub_uint16_t version;
  95. grub_uint16_t reserved;
  96. grub_uint32_t inode_generation;
  97. grub_uint8_t unused[4];
  98. grub_uint16_t uuid[8];
  99. char label[16];
  100. } GRUB_PACKED;
  101. struct grub_reiserfs_journal_header
  102. {
  103. grub_uint32_t last_flush_uid;
  104. grub_uint32_t unflushed_offset;
  105. grub_uint32_t mount_id;
  106. } GRUB_PACKED;
  107. struct grub_reiserfs_description_block
  108. {
  109. grub_uint32_t id;
  110. grub_uint32_t len;
  111. grub_uint32_t mount_id;
  112. grub_uint32_t real_blocks[0];
  113. } GRUB_PACKED;
  114. struct grub_reiserfs_commit_block
  115. {
  116. grub_uint32_t id;
  117. grub_uint32_t len;
  118. grub_uint32_t real_blocks[0];
  119. } GRUB_PACKED;
  120. struct grub_reiserfs_stat_item_v1
  121. {
  122. grub_uint16_t mode;
  123. grub_uint16_t hardlink_count;
  124. grub_uint16_t uid;
  125. grub_uint16_t gid;
  126. grub_uint32_t size;
  127. grub_uint32_t atime;
  128. grub_uint32_t mtime;
  129. grub_uint32_t ctime;
  130. grub_uint32_t rdev;
  131. grub_uint32_t first_direct_byte;
  132. } GRUB_PACKED;
  133. struct grub_reiserfs_stat_item_v2
  134. {
  135. grub_uint16_t mode;
  136. grub_uint16_t reserved;
  137. grub_uint32_t hardlink_count;
  138. grub_uint64_t size;
  139. grub_uint32_t uid;
  140. grub_uint32_t gid;
  141. grub_uint32_t atime;
  142. grub_uint32_t mtime;
  143. grub_uint32_t ctime;
  144. grub_uint32_t blocks;
  145. grub_uint32_t first_direct_byte;
  146. } GRUB_PACKED;
  147. struct grub_reiserfs_key
  148. {
  149. grub_uint32_t directory_id;
  150. grub_uint32_t object_id;
  151. union
  152. {
  153. struct
  154. {
  155. grub_uint32_t offset;
  156. grub_uint32_t type;
  157. } GRUB_PACKED v1;
  158. struct
  159. {
  160. grub_uint64_t offset_type;
  161. } GRUB_PACKED v2;
  162. } u;
  163. } GRUB_PACKED;
  164. struct grub_reiserfs_item_header
  165. {
  166. struct grub_reiserfs_key key;
  167. union
  168. {
  169. grub_uint16_t free_space;
  170. grub_uint16_t entry_count;
  171. } GRUB_PACKED u;
  172. grub_uint16_t item_size;
  173. grub_uint16_t item_location;
  174. grub_uint16_t version;
  175. } GRUB_PACKED;
  176. struct grub_reiserfs_block_header
  177. {
  178. grub_uint16_t level;
  179. grub_uint16_t item_count;
  180. grub_uint16_t free_space;
  181. grub_uint16_t reserved;
  182. struct grub_reiserfs_key block_right_delimiting_key;
  183. } GRUB_PACKED;
  184. struct grub_reiserfs_disk_child
  185. {
  186. grub_uint32_t block_number;
  187. grub_uint16_t size;
  188. grub_uint16_t reserved;
  189. } GRUB_PACKED;
  190. struct grub_reiserfs_directory_header
  191. {
  192. grub_uint32_t offset;
  193. grub_uint32_t directory_id;
  194. grub_uint32_t object_id;
  195. grub_uint16_t location;
  196. grub_uint16_t state;
  197. } GRUB_PACKED;
  198. struct grub_fshelp_node
  199. {
  200. struct grub_reiserfs_data *data;
  201. grub_uint32_t block_number; /* 0 if node is not found. */
  202. grub_uint16_t block_position;
  203. grub_uint64_t next_offset;
  204. grub_int32_t mtime;
  205. grub_off_t size;
  206. enum grub_reiserfs_item_type type; /* To know how to read the header. */
  207. struct grub_reiserfs_item_header header;
  208. };
  209. /* Returned when opening a file. */
  210. struct grub_reiserfs_data
  211. {
  212. struct grub_reiserfs_superblock superblock;
  213. grub_disk_t disk;
  214. };
  215. static grub_ssize_t
  216. grub_reiserfs_read_real (struct grub_fshelp_node *node,
  217. grub_off_t off, char *buf, grub_size_t len,
  218. grub_disk_read_hook_t read_hook,
  219. void *read_hook_data);
  220. /* Internal-only functions. Not to be used outside of this file. */
  221. /* Return the type of given v2 key. */
  222. static enum grub_reiserfs_item_type
  223. grub_reiserfs_get_key_v2_type (const struct grub_reiserfs_key *key)
  224. {
  225. switch (grub_le_to_cpu64 (key->u.v2.offset_type) >> 60)
  226. {
  227. case 0:
  228. return GRUB_REISERFS_STAT;
  229. case 15:
  230. return GRUB_REISERFS_ANY;
  231. case 3:
  232. return GRUB_REISERFS_DIRECTORY;
  233. case 2:
  234. return GRUB_REISERFS_DIRECT;
  235. case 1:
  236. return GRUB_REISERFS_INDIRECT;
  237. }
  238. return GRUB_REISERFS_UNKNOWN;
  239. }
  240. /* Return the type of given v1 key. */
  241. static enum grub_reiserfs_item_type
  242. grub_reiserfs_get_key_v1_type (const struct grub_reiserfs_key *key)
  243. {
  244. switch (grub_le_to_cpu32 (key->u.v1.type))
  245. {
  246. case 0:
  247. return GRUB_REISERFS_STAT;
  248. case 555:
  249. return GRUB_REISERFS_ANY;
  250. case 500:
  251. return GRUB_REISERFS_DIRECTORY;
  252. case 0x20000000:
  253. case 0xFFFFFFFF:
  254. return GRUB_REISERFS_DIRECT;
  255. case 0x10000000:
  256. case 0xFFFFFFFE:
  257. return GRUB_REISERFS_INDIRECT;
  258. }
  259. return GRUB_REISERFS_UNKNOWN;
  260. }
  261. /* Return 1 if the given key is version 1 key, 2 otherwise. */
  262. static int
  263. grub_reiserfs_get_key_version (const struct grub_reiserfs_key *key)
  264. {
  265. return grub_reiserfs_get_key_v1_type (key) == GRUB_REISERFS_UNKNOWN ? 2 : 1;
  266. }
  267. #ifdef GRUB_HEXDUMP
  268. static void
  269. grub_hexdump (char *buffer, grub_size_t len)
  270. {
  271. grub_size_t a;
  272. for (a = 0; a < len; a++)
  273. {
  274. if (! (a & 0x0F))
  275. grub_printf ("\n%08x ", a);
  276. grub_printf ("%02x ",
  277. ((unsigned int) ((unsigned char *) buffer)[a]) & 0xFF);
  278. }
  279. grub_printf ("\n");
  280. }
  281. #endif
  282. #ifdef GRUB_REISERFS_DEBUG
  283. static grub_uint64_t
  284. grub_reiserfs_get_key_offset (const struct grub_reiserfs_key *key);
  285. static enum grub_reiserfs_item_type
  286. grub_reiserfs_get_key_type (const struct grub_reiserfs_key *key);
  287. static void
  288. grub_reiserfs_print_key (const struct grub_reiserfs_key *key)
  289. {
  290. unsigned int a;
  291. char *reiserfs_type_strings[] = {
  292. "stat ",
  293. "directory",
  294. "direct ",
  295. "indirect ",
  296. "any ",
  297. "unknown "
  298. };
  299. for (a = 0; a < sizeof (struct grub_reiserfs_key); a++)
  300. grub_printf ("%02x ", ((unsigned int) ((unsigned char *) key)[a]) & 0xFF);
  301. grub_printf ("parent id = 0x%08x, self id = 0x%08x, type = %s, offset = ",
  302. grub_le_to_cpu32 (key->directory_id),
  303. grub_le_to_cpu32 (key->object_id),
  304. reiserfs_type_strings [grub_reiserfs_get_key_type (key)]);
  305. if (grub_reiserfs_get_key_version (key) == 1)
  306. grub_printf("%08x", (unsigned int) grub_reiserfs_get_key_offset (key));
  307. else
  308. grub_printf("0x%07x%08x",
  309. (unsigned) (grub_reiserfs_get_key_offset (key) >> 32),
  310. (unsigned) (grub_reiserfs_get_key_offset (key) & 0xFFFFFFFF));
  311. grub_printf ("\n");
  312. }
  313. #endif
  314. /* Return the offset of given key. */
  315. static grub_uint64_t
  316. grub_reiserfs_get_key_offset (const struct grub_reiserfs_key *key)
  317. {
  318. if (grub_reiserfs_get_key_version (key) == 1)
  319. return grub_le_to_cpu32 (key->u.v1.offset);
  320. else
  321. return grub_le_to_cpu64 (key->u.v2.offset_type) & (~0ULL >> 4);
  322. }
  323. /* Set the offset of given key. */
  324. static void
  325. grub_reiserfs_set_key_offset (struct grub_reiserfs_key *key,
  326. grub_uint64_t value)
  327. {
  328. if (grub_reiserfs_get_key_version (key) == 1)
  329. key->u.v1.offset = grub_cpu_to_le32 (value);
  330. else
  331. key->u.v2.offset_type \
  332. = ((key->u.v2.offset_type & grub_cpu_to_le64_compile_time (15ULL << 60))
  333. | grub_cpu_to_le64 (value & (~0ULL >> 4)));
  334. }
  335. /* Return the type of given key. */
  336. static enum grub_reiserfs_item_type
  337. grub_reiserfs_get_key_type (const struct grub_reiserfs_key *key)
  338. {
  339. if (grub_reiserfs_get_key_version (key) == 1)
  340. return grub_reiserfs_get_key_v1_type (key);
  341. else
  342. return grub_reiserfs_get_key_v2_type (key);
  343. }
  344. /* Set the type of given key, with given version number. */
  345. static void
  346. grub_reiserfs_set_key_type (struct grub_reiserfs_key *key,
  347. enum grub_reiserfs_item_type grub_type,
  348. int version)
  349. {
  350. grub_uint32_t type;
  351. switch (grub_type)
  352. {
  353. case GRUB_REISERFS_STAT:
  354. type = 0;
  355. break;
  356. case GRUB_REISERFS_ANY:
  357. type = (version == 1) ? 555 : 15;
  358. break;
  359. case GRUB_REISERFS_DIRECTORY:
  360. type = (version == 1) ? 500 : 3;
  361. break;
  362. case GRUB_REISERFS_DIRECT:
  363. type = (version == 1) ? 0xFFFFFFFF : 2;
  364. break;
  365. case GRUB_REISERFS_INDIRECT:
  366. type = (version == 1) ? 0xFFFFFFFE : 1;
  367. break;
  368. default:
  369. return;
  370. }
  371. if (version == 1)
  372. key->u.v1.type = grub_cpu_to_le32 (type);
  373. else
  374. key->u.v2.offset_type
  375. = ((key->u.v2.offset_type & grub_cpu_to_le64_compile_time (~0ULL >> 4))
  376. | grub_cpu_to_le64 ((grub_uint64_t) type << 60));
  377. assert (grub_reiserfs_get_key_type (key) == grub_type);
  378. }
  379. /* -1 if key 1 if lower than key 2.
  380. 0 if key 1 is equal to key 2.
  381. 1 if key 1 is higher than key 2. */
  382. static int
  383. grub_reiserfs_compare_keys (const struct grub_reiserfs_key *key1,
  384. const struct grub_reiserfs_key *key2)
  385. {
  386. grub_uint64_t offset1, offset2;
  387. enum grub_reiserfs_item_type type1, type2;
  388. grub_uint32_t id1, id2;
  389. if (! key1 || ! key2)
  390. return -2;
  391. id1 = grub_le_to_cpu32 (key1->directory_id);
  392. id2 = grub_le_to_cpu32 (key2->directory_id);
  393. if (id1 < id2)
  394. return -1;
  395. if (id1 > id2)
  396. return 1;
  397. id1 = grub_le_to_cpu32 (key1->object_id);
  398. id2 = grub_le_to_cpu32 (key2->object_id);
  399. if (id1 < id2)
  400. return -1;
  401. if (id1 > id2)
  402. return 1;
  403. offset1 = grub_reiserfs_get_key_offset (key1);
  404. offset2 = grub_reiserfs_get_key_offset (key2);
  405. if (offset1 < offset2)
  406. return -1;
  407. if (offset1 > offset2)
  408. return 1;
  409. type1 = grub_reiserfs_get_key_type (key1);
  410. type2 = grub_reiserfs_get_key_type (key2);
  411. if ((type1 == GRUB_REISERFS_ANY
  412. && (type2 == GRUB_REISERFS_DIRECT
  413. || type2 == GRUB_REISERFS_INDIRECT))
  414. || (type2 == GRUB_REISERFS_ANY
  415. && (type1 == GRUB_REISERFS_DIRECT
  416. || type1 == GRUB_REISERFS_INDIRECT)))
  417. return 0;
  418. if (type1 < type2)
  419. return -1;
  420. if (type1 > type2)
  421. return 1;
  422. return 0;
  423. }
  424. /* Find the item identified by KEY in mounted filesystem DATA, and fill ITEM
  425. accordingly to what was found. */
  426. static grub_err_t
  427. grub_reiserfs_get_item (struct grub_reiserfs_data *data,
  428. const struct grub_reiserfs_key *key,
  429. struct grub_fshelp_node *item, int exact)
  430. {
  431. grub_uint32_t block_number;
  432. struct grub_reiserfs_block_header *block_header = 0;
  433. struct grub_reiserfs_key *block_key = 0;
  434. grub_uint16_t block_size, item_count, current_level;
  435. grub_uint16_t i;
  436. grub_uint16_t previous_level = ~0;
  437. struct grub_reiserfs_item_header *item_headers = 0;
  438. #if 0
  439. if (! data)
  440. {
  441. grub_error (GRUB_ERR_BAD_FS, "data is NULL");
  442. goto fail;
  443. }
  444. if (! key)
  445. {
  446. grub_error (GRUB_ERR_BAD_FS, "key is NULL");
  447. goto fail;
  448. }
  449. if (! item)
  450. {
  451. grub_error (GRUB_ERR_BAD_FS, "item is NULL");
  452. goto fail;
  453. }
  454. #endif
  455. block_size = grub_le_to_cpu16 (data->superblock.block_size);
  456. block_number = grub_le_to_cpu32 (data->superblock.root_block);
  457. #ifdef GRUB_REISERFS_DEBUG
  458. grub_printf("Searching for ");
  459. grub_reiserfs_print_key (key);
  460. #endif
  461. block_header = grub_malloc (block_size);
  462. if (! block_header)
  463. goto fail;
  464. item->next_offset = 0;
  465. do
  466. {
  467. grub_disk_read (data->disk,
  468. block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
  469. (((grub_off_t) block_number * block_size)
  470. & (GRUB_DISK_SECTOR_SIZE - 1)),
  471. block_size, block_header);
  472. if (grub_errno)
  473. goto fail;
  474. current_level = grub_le_to_cpu16 (block_header->level);
  475. grub_dprintf ("reiserfs_tree", " at level %d\n", current_level);
  476. if (current_level >= previous_level)
  477. {
  478. grub_dprintf ("reiserfs_tree", "level loop detected, aborting\n");
  479. grub_error (GRUB_ERR_BAD_FS, "level loop");
  480. goto fail;
  481. }
  482. previous_level = current_level;
  483. item_count = grub_le_to_cpu16 (block_header->item_count);
  484. grub_dprintf ("reiserfs_tree", " number of contained items : %d\n",
  485. item_count);
  486. if (current_level > 1)
  487. {
  488. /* Internal node. Navigate to the child that should contain
  489. the searched key. */
  490. struct grub_reiserfs_key *keys
  491. = (struct grub_reiserfs_key *) (block_header + 1);
  492. struct grub_reiserfs_disk_child *children
  493. = ((struct grub_reiserfs_disk_child *)
  494. (keys + item_count));
  495. for (i = 0;
  496. i < item_count
  497. && grub_reiserfs_compare_keys (key, &(keys[i])) >= 0;
  498. i++)
  499. {
  500. #ifdef GRUB_REISERFS_DEBUG
  501. grub_printf("i %03d/%03d ", i + 1, item_count + 1);
  502. grub_reiserfs_print_key (&(keys[i]));
  503. #endif
  504. }
  505. block_number = grub_le_to_cpu32 (children[i].block_number);
  506. if ((i < item_count) && (key->directory_id == keys[i].directory_id)
  507. && (key->object_id == keys[i].object_id))
  508. item->next_offset = grub_reiserfs_get_key_offset(&(keys[i]));
  509. #ifdef GRUB_REISERFS_DEBUG
  510. if (i == item_count
  511. || grub_reiserfs_compare_keys (key, &(keys[i])) == 0)
  512. grub_printf(">");
  513. else
  514. grub_printf("<");
  515. if (i < item_count)
  516. {
  517. grub_printf (" %03d/%03d ", i + 1, item_count + 1);
  518. grub_reiserfs_print_key (&(keys[i]));
  519. if (i + 1 < item_count)
  520. {
  521. grub_printf ("+ %03d/%03d ", i + 2, item_count);
  522. grub_reiserfs_print_key (&(keys[i + 1]));
  523. }
  524. }
  525. else
  526. grub_printf ("Accessing rightmost child at block %d.\n",
  527. block_number);
  528. #endif
  529. }
  530. else
  531. {
  532. /* Leaf node. Check that the key is actually present. */
  533. item_headers
  534. = (struct grub_reiserfs_item_header *) (block_header + 1);
  535. for (i = 0;
  536. i < item_count;
  537. i++)
  538. {
  539. int val;
  540. val = grub_reiserfs_compare_keys (key, &(item_headers[i].key));
  541. if (val == 0)
  542. {
  543. block_key = &(item_headers[i].key);
  544. break;
  545. }
  546. if (val < 0 && exact)
  547. break;
  548. if (val < 0)
  549. {
  550. if (i == 0)
  551. {
  552. grub_error (GRUB_ERR_READ_ERROR, "unexpected btree node");
  553. goto fail;
  554. }
  555. i--;
  556. block_key = &(item_headers[i].key);
  557. break;
  558. }
  559. }
  560. if (!exact && i == item_count)
  561. {
  562. if (i == 0)
  563. {
  564. grub_error (GRUB_ERR_READ_ERROR, "unexpected btree node");
  565. goto fail;
  566. }
  567. i--;
  568. block_key = &(item_headers[i].key);
  569. }
  570. }
  571. }
  572. while (current_level > 1);
  573. item->data = data;
  574. if (!block_key)
  575. {
  576. item->block_number = 0;
  577. item->block_position = 0;
  578. item->type = GRUB_REISERFS_UNKNOWN;
  579. #ifdef GRUB_REISERFS_DEBUG
  580. grub_printf("Not found.\n");
  581. #endif
  582. }
  583. else
  584. {
  585. item->block_number = block_number;
  586. item->block_position = i;
  587. item->type = grub_reiserfs_get_key_type (block_key);
  588. grub_memcpy (&(item->header), &(item_headers[i]),
  589. sizeof (struct grub_reiserfs_item_header));
  590. #ifdef GRUB_REISERFS_DEBUG
  591. grub_printf ("F %03d/%03d ", i + 1, item_count);
  592. grub_reiserfs_print_key (block_key);
  593. #endif
  594. }
  595. assert (grub_errno == GRUB_ERR_NONE);
  596. grub_free (block_header);
  597. return GRUB_ERR_NONE;
  598. fail:
  599. assert (grub_errno != GRUB_ERR_NONE);
  600. grub_free (block_header);
  601. assert (grub_errno != GRUB_ERR_NONE);
  602. return grub_errno;
  603. }
  604. /* Return the path of the file which is pointed at by symlink NODE. */
  605. static char *
  606. grub_reiserfs_read_symlink (grub_fshelp_node_t node)
  607. {
  608. char *symlink_buffer = 0;
  609. grub_size_t len = node->size;
  610. grub_ssize_t ret;
  611. symlink_buffer = grub_malloc (len + 1);
  612. if (! symlink_buffer)
  613. return 0;
  614. ret = grub_reiserfs_read_real (node, 0, symlink_buffer, len, 0, 0);
  615. if (ret < 0)
  616. {
  617. grub_free (symlink_buffer);
  618. return 0;
  619. }
  620. symlink_buffer[ret] = 0;
  621. return symlink_buffer;
  622. }
  623. /* Fill the mounted filesystem structure and return it. */
  624. static struct grub_reiserfs_data *
  625. grub_reiserfs_mount (grub_disk_t disk)
  626. {
  627. struct grub_reiserfs_data *data = 0;
  628. data = grub_malloc (sizeof (*data));
  629. if (! data)
  630. goto fail;
  631. grub_disk_read (disk, REISERFS_SUPER_BLOCK_OFFSET / GRUB_DISK_SECTOR_SIZE,
  632. 0, sizeof (data->superblock), &(data->superblock));
  633. if (grub_errno)
  634. goto fail;
  635. if (grub_memcmp (data->superblock.magic_string,
  636. REISERFS_MAGIC_STRING, sizeof (REISERFS_MAGIC_STRING) - 1))
  637. {
  638. grub_error (GRUB_ERR_BAD_FS, "not a ReiserFS filesystem");
  639. goto fail;
  640. }
  641. data->disk = disk;
  642. return data;
  643. fail:
  644. /* Disk is too small to contain a ReiserFS. */
  645. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  646. grub_error (GRUB_ERR_BAD_FS, "not a ReiserFS filesystem");
  647. grub_free (data);
  648. return 0;
  649. }
  650. /* Call HOOK for each file in directory ITEM. */
  651. static int
  652. grub_reiserfs_iterate_dir (grub_fshelp_node_t item,
  653. grub_fshelp_iterate_dir_hook_t hook,
  654. void *hook_data)
  655. {
  656. struct grub_reiserfs_data *data = item->data;
  657. struct grub_reiserfs_block_header *block_header = 0;
  658. grub_uint16_t block_size, block_position;
  659. grub_uint32_t block_number;
  660. grub_uint64_t next_offset = item->next_offset;
  661. int ret = 0;
  662. if (item->type != GRUB_REISERFS_DIRECTORY)
  663. {
  664. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  665. goto fail;
  666. }
  667. block_size = grub_le_to_cpu16 (data->superblock.block_size);
  668. block_header = grub_malloc (block_size + 1);
  669. if (! block_header)
  670. goto fail;
  671. block_number = item->block_number;
  672. block_position = item->block_position;
  673. grub_dprintf ("reiserfs", "Iterating directory...\n");
  674. do
  675. {
  676. struct grub_reiserfs_directory_header *directory_headers;
  677. struct grub_fshelp_node directory_item;
  678. grub_uint16_t entry_count, entry_number;
  679. struct grub_reiserfs_item_header *item_headers;
  680. grub_disk_read (data->disk,
  681. block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
  682. (((grub_off_t) block_number * block_size)
  683. & (GRUB_DISK_SECTOR_SIZE - 1)),
  684. block_size, (char *) block_header);
  685. if (grub_errno)
  686. goto fail;
  687. ((char *) block_header)[block_size] = 0;
  688. #if 0
  689. if (grub_le_to_cpu16 (block_header->level) != 1)
  690. {
  691. grub_error (GRUB_ERR_BAD_FS,
  692. "reiserfs: block %d is not a leaf block",
  693. block_number);
  694. goto fail;
  695. }
  696. #endif
  697. item_headers = (struct grub_reiserfs_item_header *) (block_header + 1);
  698. directory_headers
  699. = ((struct grub_reiserfs_directory_header *)
  700. ((char *) block_header
  701. + grub_le_to_cpu16 (item_headers[block_position].item_location)));
  702. entry_count
  703. = grub_le_to_cpu16 (item_headers[block_position].u.entry_count);
  704. for (entry_number = 0; entry_number < entry_count; entry_number++)
  705. {
  706. struct grub_reiserfs_directory_header *directory_header
  707. = &directory_headers[entry_number];
  708. grub_uint16_t entry_state
  709. = grub_le_to_cpu16 (directory_header->state);
  710. grub_fshelp_node_t entry_item;
  711. struct grub_reiserfs_key entry_key;
  712. enum grub_fshelp_filetype entry_type;
  713. char *entry_name;
  714. char *entry_name_end = 0;
  715. char c;
  716. if (!(entry_state & GRUB_REISERFS_VISIBLE_MASK))
  717. continue;
  718. entry_name = (((char *) directory_headers)
  719. + grub_le_to_cpu16 (directory_header->location));
  720. if (entry_number == 0)
  721. {
  722. entry_name_end = (char *) block_header
  723. + grub_le_to_cpu16 (item_headers[block_position].item_location)
  724. + grub_le_to_cpu16 (item_headers[block_position].item_size);
  725. }
  726. else
  727. {
  728. entry_name_end = (((char *) directory_headers)
  729. + grub_le_to_cpu16 (directory_headers[entry_number - 1].location));
  730. }
  731. if (entry_name_end < entry_name || entry_name_end > (char *) block_header + block_size)
  732. {
  733. entry_name_end = (char *) block_header + block_size;
  734. }
  735. entry_key.directory_id = directory_header->directory_id;
  736. entry_key.object_id = directory_header->object_id;
  737. entry_key.u.v2.offset_type = 0;
  738. grub_reiserfs_set_key_type (&entry_key, GRUB_REISERFS_DIRECTORY,
  739. 2);
  740. grub_reiserfs_set_key_offset (&entry_key, 1);
  741. entry_item = grub_malloc (sizeof (*entry_item));
  742. if (! entry_item)
  743. goto fail;
  744. if (grub_reiserfs_get_item (data, &entry_key, entry_item, 1)
  745. != GRUB_ERR_NONE)
  746. {
  747. grub_free (entry_item);
  748. goto fail;
  749. }
  750. if (entry_item->type == GRUB_REISERFS_DIRECTORY)
  751. entry_type = GRUB_FSHELP_DIR;
  752. else
  753. {
  754. grub_uint32_t entry_block_number;
  755. /* Order is very important here.
  756. First set the offset to 0 using current key version.
  757. Then change the key type, which affects key version
  758. detection. */
  759. grub_reiserfs_set_key_offset (&entry_key, 0);
  760. grub_reiserfs_set_key_type (&entry_key, GRUB_REISERFS_STAT,
  761. 2);
  762. if (grub_reiserfs_get_item (data, &entry_key, entry_item, 1)
  763. != GRUB_ERR_NONE)
  764. {
  765. grub_free (entry_item);
  766. goto fail;
  767. }
  768. if (entry_item->block_number != 0)
  769. {
  770. grub_uint16_t entry_version;
  771. entry_version
  772. = grub_le_to_cpu16 (entry_item->header.version);
  773. entry_block_number = entry_item->block_number;
  774. #if 0
  775. grub_dprintf ("reiserfs",
  776. "version %04x block %08x (%08x) position %08x\n",
  777. entry_version, entry_block_number,
  778. ((grub_disk_addr_t) entry_block_number * block_size) / GRUB_DISK_SECTOR_SIZE,
  779. grub_le_to_cpu16 (entry_item->header.item_location));
  780. #endif
  781. if (entry_version == 0) /* Version 1 stat item. */
  782. {
  783. struct grub_reiserfs_stat_item_v1 entry_v1_stat;
  784. grub_disk_read (data->disk,
  785. entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
  786. grub_le_to_cpu16 (entry_item->header.item_location),
  787. sizeof (entry_v1_stat),
  788. (char *) &entry_v1_stat);
  789. if (grub_errno)
  790. goto fail;
  791. #if 0
  792. grub_dprintf ("reiserfs",
  793. "%04x %04x %04x %04x %08x %08x | %08x %08x %08x %08x\n",
  794. grub_le_to_cpu16 (entry_v1_stat.mode),
  795. grub_le_to_cpu16 (entry_v1_stat.hardlink_count),
  796. grub_le_to_cpu16 (entry_v1_stat.uid),
  797. grub_le_to_cpu16 (entry_v1_stat.gid),
  798. grub_le_to_cpu32 (entry_v1_stat.size),
  799. grub_le_to_cpu32 (entry_v1_stat.atime),
  800. grub_le_to_cpu32 (entry_v1_stat.mtime),
  801. grub_le_to_cpu32 (entry_v1_stat.ctime),
  802. grub_le_to_cpu32 (entry_v1_stat.rdev),
  803. grub_le_to_cpu32 (entry_v1_stat.first_direct_byte));
  804. grub_dprintf ("reiserfs",
  805. "%04x %04x %04x %04x %08x %08x | %08x %08x %08x %08x\n",
  806. entry_v1_stat.mode,
  807. entry_v1_stat.hardlink_count,
  808. entry_v1_stat.uid,
  809. entry_v1_stat.gid,
  810. entry_v1_stat.size,
  811. entry_v1_stat.atime,
  812. entry_v1_stat.mtime,
  813. entry_v1_stat.ctime,
  814. entry_v1_stat.rdev,
  815. entry_v1_stat.first_direct_byte);
  816. #endif
  817. entry_item->mtime = grub_le_to_cpu32 (entry_v1_stat.mtime);
  818. if ((grub_le_to_cpu16 (entry_v1_stat.mode) & S_IFLNK)
  819. == S_IFLNK)
  820. entry_type = GRUB_FSHELP_SYMLINK;
  821. else
  822. entry_type = GRUB_FSHELP_REG;
  823. entry_item->size = (grub_off_t) grub_le_to_cpu32 (entry_v1_stat.size);
  824. }
  825. else
  826. {
  827. struct grub_reiserfs_stat_item_v2 entry_v2_stat;
  828. grub_disk_read (data->disk,
  829. entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
  830. grub_le_to_cpu16 (entry_item->header.item_location),
  831. sizeof (entry_v2_stat),
  832. (char *) &entry_v2_stat);
  833. if (grub_errno)
  834. goto fail;
  835. #if 0
  836. grub_dprintf ("reiserfs",
  837. "%04x %04x %08x %08x%08x | %08x %08x %08x %08x | %08x %08x %08x\n",
  838. grub_le_to_cpu16 (entry_v2_stat.mode),
  839. grub_le_to_cpu16 (entry_v2_stat.reserved),
  840. grub_le_to_cpu32 (entry_v2_stat.hardlink_count),
  841. (unsigned int) (grub_le_to_cpu64 (entry_v2_stat.size) >> 32),
  842. (unsigned int) (grub_le_to_cpu64 (entry_v2_stat.size) && 0xFFFFFFFF),
  843. grub_le_to_cpu32 (entry_v2_stat.uid),
  844. grub_le_to_cpu32 (entry_v2_stat.gid),
  845. grub_le_to_cpu32 (entry_v2_stat.atime),
  846. grub_le_to_cpu32 (entry_v2_stat.mtime),
  847. grub_le_to_cpu32 (entry_v2_stat.ctime),
  848. grub_le_to_cpu32 (entry_v2_stat.blocks),
  849. grub_le_to_cpu32 (entry_v2_stat.first_direct_byte));
  850. grub_dprintf ("reiserfs",
  851. "%04x %04x %08x %08x%08x | %08x %08x %08x %08x | %08x %08x %08x\n",
  852. entry_v2_stat.mode,
  853. entry_v2_stat.reserved,
  854. entry_v2_stat.hardlink_count,
  855. (unsigned int) (entry_v2_stat.size >> 32),
  856. (unsigned int) (entry_v2_stat.size && 0xFFFFFFFF),
  857. entry_v2_stat.uid,
  858. entry_v2_stat.gid,
  859. entry_v2_stat.atime,
  860. entry_v2_stat.mtime,
  861. entry_v2_stat.ctime,
  862. entry_v2_stat.blocks,
  863. entry_v2_stat.first_direct_byte);
  864. #endif
  865. entry_item->mtime = grub_le_to_cpu32 (entry_v2_stat.mtime);
  866. entry_item->size = (grub_off_t) grub_le_to_cpu64 (entry_v2_stat.size);
  867. if ((grub_le_to_cpu16 (entry_v2_stat.mode) & S_IFLNK)
  868. == S_IFLNK)
  869. entry_type = GRUB_FSHELP_SYMLINK;
  870. else
  871. entry_type = GRUB_FSHELP_REG;
  872. }
  873. }
  874. else
  875. {
  876. /* Pseudo file ".." never has stat block. */
  877. if (entry_name_end == entry_name + 2 && grub_memcmp (entry_name, "..", 2) != 0)
  878. grub_dprintf ("reiserfs",
  879. "Warning : %s has no stat block !\n",
  880. entry_name);
  881. grub_free (entry_item);
  882. goto next;
  883. }
  884. }
  885. c = *entry_name_end;
  886. *entry_name_end = 0;
  887. if (hook (entry_name, entry_type, entry_item, hook_data))
  888. {
  889. *entry_name_end = c;
  890. grub_dprintf ("reiserfs", "Found : %s, type=%d\n",
  891. entry_name, entry_type);
  892. ret = 1;
  893. goto found;
  894. }
  895. *entry_name_end = c;
  896. next:
  897. ;
  898. }
  899. if (next_offset == 0)
  900. break;
  901. grub_reiserfs_set_key_offset (&(item_headers[block_position].key),
  902. next_offset);
  903. if (grub_reiserfs_get_item (data, &(item_headers[block_position].key),
  904. &directory_item, 1) != GRUB_ERR_NONE)
  905. goto fail;
  906. block_number = directory_item.block_number;
  907. block_position = directory_item.block_position;
  908. next_offset = directory_item.next_offset;
  909. }
  910. while (block_number);
  911. found:
  912. assert (grub_errno == GRUB_ERR_NONE);
  913. grub_free (block_header);
  914. return ret;
  915. fail:
  916. assert (grub_errno != GRUB_ERR_NONE);
  917. grub_free (block_header);
  918. return 0;
  919. }
  920. /****************************************************************************/
  921. /* grub api functions */
  922. /****************************************************************************/
  923. /* Open a file named NAME and initialize FILE. */
  924. static grub_err_t
  925. grub_reiserfs_open (struct grub_file *file, const char *name)
  926. {
  927. struct grub_reiserfs_data *data = 0;
  928. struct grub_fshelp_node root, *found = 0;
  929. struct grub_reiserfs_key key;
  930. grub_dl_ref (my_mod);
  931. data = grub_reiserfs_mount (file->device->disk);
  932. if (! data)
  933. goto fail;
  934. key.directory_id = grub_cpu_to_le32_compile_time (1);
  935. key.object_id = grub_cpu_to_le32_compile_time (2);
  936. key.u.v2.offset_type = 0;
  937. grub_reiserfs_set_key_type (&key, GRUB_REISERFS_DIRECTORY, 2);
  938. grub_reiserfs_set_key_offset (&key, 1);
  939. if (grub_reiserfs_get_item (data, &key, &root, 1) != GRUB_ERR_NONE)
  940. goto fail;
  941. if (root.block_number == 0)
  942. {
  943. grub_error (GRUB_ERR_BAD_FS, "unable to find root item");
  944. goto fail; /* Should never happen since checked at mount. */
  945. }
  946. grub_fshelp_find_file (name, &root, &found,
  947. grub_reiserfs_iterate_dir,
  948. grub_reiserfs_read_symlink, GRUB_FSHELP_REG);
  949. if (grub_errno)
  950. goto fail;
  951. file->size = found->size;
  952. grub_dprintf ("reiserfs", "file size : %d (%08x%08x)\n",
  953. (unsigned int) file->size,
  954. (unsigned int) (file->size >> 32), (unsigned int) file->size);
  955. file->offset = 0;
  956. file->data = found;
  957. return GRUB_ERR_NONE;
  958. fail:
  959. assert (grub_errno != GRUB_ERR_NONE);
  960. if (found != &root)
  961. grub_free (found);
  962. grub_free (data);
  963. grub_dl_unref (my_mod);
  964. return grub_errno;
  965. }
  966. static grub_ssize_t
  967. grub_reiserfs_read_real (struct grub_fshelp_node *node,
  968. grub_off_t off, char *buf, grub_size_t len,
  969. grub_disk_read_hook_t read_hook, void *read_hook_data)
  970. {
  971. unsigned int indirect_block, indirect_block_count;
  972. struct grub_reiserfs_key key;
  973. struct grub_reiserfs_data *data = node->data;
  974. struct grub_fshelp_node found;
  975. grub_uint16_t block_size = grub_le_to_cpu16 (data->superblock.block_size);
  976. grub_uint16_t item_size;
  977. grub_uint32_t *indirect_block_ptr = 0;
  978. grub_uint64_t current_key_offset = 1;
  979. grub_off_t initial_position, current_position, final_position, length;
  980. grub_disk_addr_t block;
  981. grub_off_t offset;
  982. key.directory_id = node->header.key.directory_id;
  983. key.object_id = node->header.key.object_id;
  984. key.u.v2.offset_type = 0;
  985. grub_reiserfs_set_key_type (&key, GRUB_REISERFS_ANY, 2);
  986. initial_position = off;
  987. current_position = 0;
  988. final_position = MIN (len + initial_position, node->size);
  989. grub_dprintf ("reiserfs",
  990. "Reading from %lld to %lld (%lld instead of requested %ld)\n",
  991. (unsigned long long) initial_position,
  992. (unsigned long long) final_position,
  993. (unsigned long long) (final_position - initial_position),
  994. (unsigned long) len);
  995. grub_reiserfs_set_key_offset (&key, initial_position + 1);
  996. if (grub_reiserfs_get_item (data, &key, &found, 0) != GRUB_ERR_NONE)
  997. goto fail;
  998. if (found.block_number == 0)
  999. {
  1000. grub_error (GRUB_ERR_READ_ERROR, "offset %lld not found",
  1001. (unsigned long long) initial_position);
  1002. goto fail;
  1003. }
  1004. current_key_offset = grub_reiserfs_get_key_offset (&found.header.key);
  1005. current_position = current_key_offset - 1;
  1006. while (current_position < final_position)
  1007. {
  1008. grub_reiserfs_set_key_offset (&key, current_key_offset);
  1009. if (grub_reiserfs_get_item (data, &key, &found, 1) != GRUB_ERR_NONE)
  1010. goto fail;
  1011. if (found.block_number == 0)
  1012. goto fail;
  1013. item_size = grub_le_to_cpu16 (found.header.item_size);
  1014. switch (found.type)
  1015. {
  1016. case GRUB_REISERFS_DIRECT:
  1017. block = ((grub_disk_addr_t) found.block_number) * (block_size >> GRUB_DISK_SECTOR_BITS);
  1018. grub_dprintf ("reiserfs_blocktype", "D: %u\n", (unsigned) block);
  1019. if (initial_position < current_position + item_size)
  1020. {
  1021. offset = MAX ((signed) (initial_position - current_position), 0);
  1022. length = (MIN (item_size, final_position - current_position)
  1023. - offset);
  1024. grub_dprintf ("reiserfs",
  1025. "Reading direct block %u from %u to %u...\n",
  1026. (unsigned) block, (unsigned) offset,
  1027. (unsigned) (offset + length));
  1028. found.data->disk->read_hook = read_hook;
  1029. found.data->disk->read_hook_data = read_hook_data;
  1030. grub_disk_read (found.data->disk,
  1031. block,
  1032. offset
  1033. + grub_le_to_cpu16 (found.header.item_location),
  1034. length, buf);
  1035. found.data->disk->read_hook = 0;
  1036. if (grub_errno)
  1037. goto fail;
  1038. buf += length;
  1039. current_position += offset + length;
  1040. }
  1041. else
  1042. current_position += item_size;
  1043. break;
  1044. case GRUB_REISERFS_INDIRECT:
  1045. indirect_block_count = item_size / sizeof (*indirect_block_ptr);
  1046. indirect_block_ptr = grub_malloc (item_size);
  1047. if (! indirect_block_ptr)
  1048. goto fail;
  1049. grub_disk_read (found.data->disk,
  1050. found.block_number * (block_size >> GRUB_DISK_SECTOR_BITS),
  1051. grub_le_to_cpu16 (found.header.item_location),
  1052. item_size, indirect_block_ptr);
  1053. if (grub_errno)
  1054. goto fail;
  1055. found.data->disk->read_hook = read_hook;
  1056. found.data->disk->read_hook_data = read_hook_data;
  1057. for (indirect_block = 0;
  1058. indirect_block < indirect_block_count
  1059. && current_position < final_position;
  1060. indirect_block++)
  1061. {
  1062. block = grub_le_to_cpu32 (indirect_block_ptr[indirect_block]) *
  1063. (block_size >> GRUB_DISK_SECTOR_BITS);
  1064. grub_dprintf ("reiserfs_blocktype", "I: %u\n", (unsigned) block);
  1065. if (current_position + block_size >= initial_position)
  1066. {
  1067. offset = MAX ((signed) (initial_position - current_position),
  1068. 0);
  1069. length = (MIN (block_size, final_position - current_position)
  1070. - offset);
  1071. grub_dprintf ("reiserfs",
  1072. "Reading indirect block %u from %u to %u...\n",
  1073. (unsigned) block, (unsigned) offset,
  1074. (unsigned) (offset + length));
  1075. #if 0
  1076. grub_dprintf ("reiserfs",
  1077. "\nib=%04d/%04d, ip=%d, cp=%d, fp=%d, off=%d, l=%d, tl=%d\n",
  1078. indirect_block + 1, indirect_block_count,
  1079. initial_position, current_position,
  1080. final_position, offset, length, len);
  1081. #endif
  1082. grub_disk_read (found.data->disk, block, offset, length, buf);
  1083. if (grub_errno)
  1084. goto fail;
  1085. buf += length;
  1086. current_position += offset + length;
  1087. }
  1088. else
  1089. current_position += block_size;
  1090. }
  1091. found.data->disk->read_hook = 0;
  1092. grub_free (indirect_block_ptr);
  1093. indirect_block_ptr = 0;
  1094. break;
  1095. default:
  1096. goto fail;
  1097. }
  1098. current_key_offset = current_position + 1;
  1099. }
  1100. grub_dprintf ("reiserfs",
  1101. "Have successfully read %lld bytes (%ld requested)\n",
  1102. (unsigned long long) (current_position - initial_position),
  1103. (unsigned long) len);
  1104. return current_position - initial_position;
  1105. #if 0
  1106. switch (found.type)
  1107. {
  1108. case GRUB_REISERFS_DIRECT:
  1109. read_length = MIN (len, item_size - file->offset);
  1110. grub_disk_read (found.data->disk,
  1111. (found.block_number * block_size) / GRUB_DISK_SECTOR_SIZE,
  1112. grub_le_to_cpu16 (found.header.item_location) + file->offset,
  1113. read_length, buf);
  1114. if (grub_errno)
  1115. goto fail;
  1116. break;
  1117. case GRUB_REISERFS_INDIRECT:
  1118. indirect_block_count = item_size / sizeof (*indirect_block_ptr);
  1119. indirect_block_ptr = grub_malloc (item_size);
  1120. if (!indirect_block_ptr)
  1121. goto fail;
  1122. grub_disk_read (found.data->disk,
  1123. (found.block_number * block_size) / GRUB_DISK_SECTOR_SIZE,
  1124. grub_le_to_cpu16 (found.header.item_location),
  1125. item_size, (char *) indirect_block_ptr);
  1126. if (grub_errno)
  1127. goto fail;
  1128. len = MIN (len, file->size - file->offset);
  1129. for (indirect_block = file->offset / block_size;
  1130. indirect_block < indirect_block_count && read_length < len;
  1131. indirect_block++)
  1132. {
  1133. read = MIN (block_size, len - read_length);
  1134. grub_disk_read (found.data->disk,
  1135. (grub_le_to_cpu32 (indirect_block_ptr[indirect_block]) * block_size) / GRUB_DISK_SECTOR_SIZE,
  1136. file->offset % block_size, read,
  1137. ((void *) buf) + read_length);
  1138. if (grub_errno)
  1139. goto fail;
  1140. read_length += read;
  1141. }
  1142. grub_free (indirect_block_ptr);
  1143. break;
  1144. default:
  1145. goto fail;
  1146. }
  1147. return read_length;
  1148. #endif
  1149. fail:
  1150. grub_free (indirect_block_ptr);
  1151. return -1;
  1152. }
  1153. static grub_ssize_t
  1154. grub_reiserfs_read (grub_file_t file, char *buf, grub_size_t len)
  1155. {
  1156. return grub_reiserfs_read_real (file->data, file->offset, buf, len,
  1157. file->read_hook, file->read_hook_data);
  1158. }
  1159. /* Close the file FILE. */
  1160. static grub_err_t
  1161. grub_reiserfs_close (grub_file_t file)
  1162. {
  1163. struct grub_fshelp_node *node = file->data;
  1164. struct grub_reiserfs_data *data = node->data;
  1165. grub_free (data);
  1166. grub_free (node);
  1167. grub_dl_unref (my_mod);
  1168. return GRUB_ERR_NONE;
  1169. }
  1170. /* Context for grub_reiserfs_dir. */
  1171. struct grub_reiserfs_dir_ctx
  1172. {
  1173. grub_fs_dir_hook_t hook;
  1174. void *hook_data;
  1175. };
  1176. /* Helper for grub_reiserfs_dir. */
  1177. static int
  1178. grub_reiserfs_dir_iter (const char *filename,
  1179. enum grub_fshelp_filetype filetype,
  1180. grub_fshelp_node_t node, void *data)
  1181. {
  1182. struct grub_reiserfs_dir_ctx *ctx = data;
  1183. struct grub_dirhook_info info;
  1184. grub_memset (&info, 0, sizeof (info));
  1185. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  1186. info.mtimeset = 1;
  1187. info.mtime = node->mtime;
  1188. grub_free (node);
  1189. return ctx->hook (filename, &info, ctx->hook_data);
  1190. }
  1191. /* Call HOOK with each file under DIR. */
  1192. static grub_err_t
  1193. grub_reiserfs_dir (grub_device_t device, const char *path,
  1194. grub_fs_dir_hook_t hook, void *hook_data)
  1195. {
  1196. struct grub_reiserfs_dir_ctx ctx = { hook, hook_data };
  1197. struct grub_reiserfs_data *data = 0;
  1198. struct grub_fshelp_node root, *found;
  1199. struct grub_reiserfs_key root_key;
  1200. grub_dl_ref (my_mod);
  1201. data = grub_reiserfs_mount (device->disk);
  1202. if (! data)
  1203. goto fail;
  1204. root_key.directory_id = grub_cpu_to_le32_compile_time (1);
  1205. root_key.object_id = grub_cpu_to_le32_compile_time (2);
  1206. root_key.u.v2.offset_type = 0;
  1207. grub_reiserfs_set_key_type (&root_key, GRUB_REISERFS_DIRECTORY, 2);
  1208. grub_reiserfs_set_key_offset (&root_key, 1);
  1209. if (grub_reiserfs_get_item (data, &root_key, &root, 1) != GRUB_ERR_NONE)
  1210. goto fail;
  1211. if (root.block_number == 0)
  1212. {
  1213. grub_error(GRUB_ERR_BAD_FS, "root not found");
  1214. goto fail;
  1215. }
  1216. grub_fshelp_find_file (path, &root, &found, grub_reiserfs_iterate_dir,
  1217. grub_reiserfs_read_symlink, GRUB_FSHELP_DIR);
  1218. if (grub_errno)
  1219. goto fail;
  1220. grub_reiserfs_iterate_dir (found, grub_reiserfs_dir_iter, &ctx);
  1221. grub_free (data);
  1222. grub_dl_unref (my_mod);
  1223. return GRUB_ERR_NONE;
  1224. fail:
  1225. grub_free (data);
  1226. grub_dl_unref (my_mod);
  1227. return grub_errno;
  1228. }
  1229. /* Return the label of the device DEVICE in LABEL. The label is
  1230. returned in a grub_malloc'ed buffer and should be freed by the
  1231. caller. */
  1232. static grub_err_t
  1233. grub_reiserfs_label (grub_device_t device, char **label)
  1234. {
  1235. struct grub_reiserfs_data *data;
  1236. grub_disk_t disk = device->disk;
  1237. grub_dl_ref (my_mod);
  1238. data = grub_reiserfs_mount (disk);
  1239. if (data)
  1240. {
  1241. *label = grub_strndup (data->superblock.label,
  1242. sizeof (data->superblock.label));
  1243. }
  1244. else
  1245. *label = NULL;
  1246. grub_dl_unref (my_mod);
  1247. grub_free (data);
  1248. return grub_errno;
  1249. }
  1250. static grub_err_t
  1251. grub_reiserfs_uuid (grub_device_t device, char **uuid)
  1252. {
  1253. struct grub_reiserfs_data *data;
  1254. grub_disk_t disk = device->disk;
  1255. grub_dl_ref (my_mod);
  1256. *uuid = NULL;
  1257. data = grub_reiserfs_mount (disk);
  1258. if (data)
  1259. {
  1260. unsigned i;
  1261. for (i = 0; i < ARRAY_SIZE (data->superblock.uuid); i++)
  1262. if (data->superblock.uuid[i])
  1263. break;
  1264. if (i < ARRAY_SIZE (data->superblock.uuid))
  1265. *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  1266. grub_be_to_cpu16 (data->superblock.uuid[0]),
  1267. grub_be_to_cpu16 (data->superblock.uuid[1]),
  1268. grub_be_to_cpu16 (data->superblock.uuid[2]),
  1269. grub_be_to_cpu16 (data->superblock.uuid[3]),
  1270. grub_be_to_cpu16 (data->superblock.uuid[4]),
  1271. grub_be_to_cpu16 (data->superblock.uuid[5]),
  1272. grub_be_to_cpu16 (data->superblock.uuid[6]),
  1273. grub_be_to_cpu16 (data->superblock.uuid[7]));
  1274. }
  1275. grub_dl_unref (my_mod);
  1276. grub_free (data);
  1277. return grub_errno;
  1278. }
  1279. static struct grub_fs grub_reiserfs_fs =
  1280. {
  1281. .name = "reiserfs",
  1282. .fs_dir = grub_reiserfs_dir,
  1283. .fs_open = grub_reiserfs_open,
  1284. .fs_read = grub_reiserfs_read,
  1285. .fs_close = grub_reiserfs_close,
  1286. .fs_label = grub_reiserfs_label,
  1287. .fs_uuid = grub_reiserfs_uuid,
  1288. #ifdef GRUB_UTIL
  1289. .reserved_first_sector = 1,
  1290. .blocklist_install = 1,
  1291. #endif
  1292. .next = 0
  1293. };
  1294. GRUB_MOD_INIT(reiserfs)
  1295. {
  1296. grub_fs_register (&grub_reiserfs_fs);
  1297. my_mod = mod;
  1298. }
  1299. GRUB_MOD_FINI(reiserfs)
  1300. {
  1301. grub_fs_unregister (&grub_reiserfs_fs);
  1302. }