jfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /* jfs.c - JFS. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2004,2005,2006,2007,2008,2009 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. #include <grub/err.h>
  20. #include <grub/file.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/disk.h>
  24. #include <grub/dl.h>
  25. #include <grub/types.h>
  26. #include <grub/charset.h>
  27. #define GRUB_JFS_MAX_SYMLNK_CNT 8
  28. #define GRUB_JFS_FILETYPE_MASK 0170000
  29. #define GRUB_JFS_FILETYPE_REG 0100000
  30. #define GRUB_JFS_FILETYPE_LNK 0120000
  31. #define GRUB_JFS_FILETYPE_DIR 0040000
  32. #define GRUB_JFS_SBLOCK 64
  33. #define GRUB_JFS_AGGR_INODE 2
  34. #define GRUB_JFS_FS1_INODE_BLK 104
  35. #define GRUB_JFS_TREE_LEAF 2
  36. struct grub_jfs_sblock
  37. {
  38. /* The magic for JFS. It should contain the string "JFS1". */
  39. grub_uint8_t magic[4];
  40. grub_uint32_t version;
  41. grub_uint64_t ag_size;
  42. /* The size of a filesystem block in bytes. XXX: currently only
  43. 4096 was tested. */
  44. grub_uint32_t blksz;
  45. grub_uint16_t log2_blksz;
  46. grub_uint8_t unused[71];
  47. grub_uint8_t volname[11];
  48. grub_uint8_t unused2[32];
  49. grub_uint8_t uuid[16];
  50. };
  51. struct grub_jfs_extent
  52. {
  53. /* The length of the extent in filesystem blocks. */
  54. grub_uint16_t length;
  55. grub_uint8_t length2;
  56. /* The physical offset of the first block on the disk. */
  57. grub_uint8_t blk1;
  58. grub_uint32_t blk2;
  59. } __attribute__ ((packed));
  60. struct grub_jfs_iag
  61. {
  62. grub_uint8_t unused[3072];
  63. struct grub_jfs_extent inodes[128];
  64. } __attribute__ ((packed));
  65. /* The head of the tree used to find extents. */
  66. struct grub_jfs_treehead
  67. {
  68. grub_uint64_t next;
  69. grub_uint64_t prev;
  70. grub_uint8_t flags;
  71. grub_uint8_t unused;
  72. grub_uint16_t count;
  73. grub_uint16_t max;
  74. grub_uint8_t unused2[10];
  75. } __attribute__ ((packed));
  76. /* A node in the extent tree. */
  77. struct grub_jfs_tree_extent
  78. {
  79. grub_uint8_t flags;
  80. grub_uint16_t unused;
  81. /* The offset is the key used to lookup an extent. */
  82. grub_uint8_t offset1;
  83. grub_uint32_t offset2;
  84. struct grub_jfs_extent extent;
  85. } __attribute__ ((packed));
  86. /* The tree of directory entries. */
  87. struct grub_jfs_tree_dir
  88. {
  89. /* Pointers to the previous and next tree headers of other nodes on
  90. this level. */
  91. grub_uint64_t nextb;
  92. grub_uint64_t prevb;
  93. grub_uint8_t flags;
  94. /* The amount of dirents in this node. */
  95. grub_uint8_t count;
  96. grub_uint8_t freecnt;
  97. grub_uint8_t freelist;
  98. grub_uint8_t maxslot;
  99. /* The location of the sorted array of pointers to dirents. */
  100. grub_uint8_t sindex;
  101. grub_uint8_t unused[10];
  102. } __attribute__ ((packed));
  103. /* An internal node in the dirents tree. */
  104. struct grub_jfs_internal_dirent
  105. {
  106. struct grub_jfs_extent ex;
  107. grub_uint8_t next;
  108. grub_uint8_t len;
  109. grub_uint16_t namepart[11];
  110. } __attribute__ ((packed));
  111. /* A leaf node in the dirents tree. */
  112. struct grub_jfs_leaf_dirent
  113. {
  114. /* The inode for this dirent. */
  115. grub_uint32_t inode;
  116. grub_uint8_t next;
  117. /* The size of the name. */
  118. grub_uint8_t len;
  119. grub_uint16_t namepart[11];
  120. grub_uint32_t index;
  121. } __attribute__ ((packed));
  122. /* A leaf in the dirents tree. This one is used if the previously
  123. dirent was not big enough to store the name. */
  124. struct grub_jfs_leaf_next_dirent
  125. {
  126. grub_uint8_t next;
  127. grub_uint8_t len;
  128. grub_uint16_t namepart[15];
  129. } __attribute__ ((packed));
  130. struct grub_jfs_inode
  131. {
  132. grub_uint32_t stamp;
  133. grub_uint32_t fileset;
  134. grub_uint32_t inode;
  135. grub_uint8_t unused[12];
  136. grub_uint64_t size;
  137. grub_uint8_t unused2[20];
  138. grub_uint32_t mode;
  139. grub_uint8_t unused3[72];
  140. grub_uint8_t unused4[96];
  141. union
  142. {
  143. /* The tree describing the extents of the file. */
  144. struct __attribute__ ((packed))
  145. {
  146. struct grub_jfs_treehead tree;
  147. struct grub_jfs_tree_extent extents[16];
  148. } file;
  149. union
  150. {
  151. /* The tree describing the dirents. */
  152. struct
  153. {
  154. grub_uint8_t unused[16];
  155. grub_uint8_t flags;
  156. /* Amount of dirents in this node. */
  157. grub_uint8_t count;
  158. grub_uint8_t freecnt;
  159. grub_uint8_t freelist;
  160. grub_uint32_t idotdot;
  161. grub_uint8_t sorted[8];
  162. } header;
  163. struct grub_jfs_leaf_dirent dirents[8];
  164. } dir __attribute__ ((packed));
  165. /* Fast symlink. */
  166. struct
  167. {
  168. grub_uint8_t unused[32];
  169. grub_uint8_t path[128];
  170. } symlink;
  171. } __attribute__ ((packed));
  172. } __attribute__ ((packed));
  173. struct grub_jfs_data
  174. {
  175. struct grub_jfs_sblock sblock;
  176. grub_disk_t disk;
  177. struct grub_jfs_inode fileset;
  178. struct grub_jfs_inode currinode;
  179. int pos;
  180. int linknest;
  181. } __attribute__ ((packed));
  182. struct grub_jfs_diropen
  183. {
  184. int index;
  185. union
  186. {
  187. struct grub_jfs_tree_dir header;
  188. struct grub_jfs_leaf_dirent dirent[0];
  189. struct grub_jfs_leaf_next_dirent next_dirent[0];
  190. char sorted[0];
  191. } *dirpage __attribute__ ((packed));
  192. struct grub_jfs_data *data;
  193. struct grub_jfs_inode *inode;
  194. int count;
  195. char *sorted;
  196. struct grub_jfs_leaf_dirent *leaf;
  197. struct grub_jfs_leaf_next_dirent *next_leaf;
  198. /* The filename and inode of the last read dirent. */
  199. char name[255];
  200. grub_uint32_t ino;
  201. } __attribute__ ((packed));
  202. static grub_dl_t my_mod;
  203. static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data,
  204. int ino);
  205. struct getblk_closure
  206. {
  207. struct grub_jfs_data *data;
  208. unsigned int blk;
  209. };
  210. static int
  211. getblk (struct grub_jfs_treehead *treehead,
  212. struct grub_jfs_tree_extent *extents,
  213. struct getblk_closure *c)
  214. {
  215. int found = -1;
  216. int i;
  217. for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
  218. {
  219. if (treehead->flags & GRUB_JFS_TREE_LEAF)
  220. {
  221. /* Read the leafnode. */
  222. if (grub_le_to_cpu32 (extents[i].offset2) <= c->blk
  223. && ((grub_le_to_cpu16 (extents[i].extent.length))
  224. + (extents[i].extent.length2 << 8)
  225. + grub_le_to_cpu32 (extents[i].offset2)) > c->blk)
  226. return (c->blk - grub_le_to_cpu32 (extents[i].offset2)
  227. + grub_le_to_cpu32 (extents[i].extent.blk2));
  228. }
  229. else
  230. if (c->blk >= grub_le_to_cpu32 (extents[i].offset2))
  231. found = i;
  232. }
  233. if (found != -1)
  234. {
  235. struct
  236. {
  237. struct grub_jfs_treehead treehead;
  238. struct grub_jfs_tree_extent extents[254];
  239. } tree;
  240. if (grub_disk_read (c->data->disk,
  241. grub_le_to_cpu32 (extents[found].extent.blk2)
  242. << (grub_le_to_cpu16 (c->data->sblock.log2_blksz)
  243. - GRUB_DISK_SECTOR_BITS), 0,
  244. sizeof (tree), (char *) &tree))
  245. return -1;
  246. return getblk (&tree.treehead, &tree.extents[0], c);
  247. }
  248. return -1;
  249. }
  250. /* Get the block number for the block BLK in the node INODE in the
  251. mounted filesystem DATA. */
  252. static int
  253. grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
  254. unsigned int blk)
  255. {
  256. struct getblk_closure c;
  257. c.data = data;
  258. c.blk = blk;
  259. return getblk (&inode->file.tree, &inode->file.extents[0], &c);
  260. }
  261. static grub_err_t
  262. grub_jfs_read_inode (struct grub_jfs_data *data, int ino,
  263. struct grub_jfs_inode *inode)
  264. {
  265. struct grub_jfs_iag iag;
  266. int iagnum = ino / 4096;
  267. int inoext = (ino % 4096) / 32;
  268. int inonum = (ino % 4096) % 32;
  269. grub_uint32_t iagblk;
  270. grub_uint32_t inoblk;
  271. iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
  272. if (grub_errno)
  273. return grub_errno;
  274. /* Read in the IAG. */
  275. if (grub_disk_read (data->disk,
  276. iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  277. - GRUB_DISK_SECTOR_BITS), 0,
  278. sizeof (struct grub_jfs_iag), &iag))
  279. return grub_errno;
  280. inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
  281. inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
  282. - GRUB_DISK_SECTOR_BITS);
  283. inoblk += inonum;
  284. if (grub_disk_read (data->disk, inoblk, 0,
  285. sizeof (struct grub_jfs_inode), inode))
  286. return grub_errno;
  287. return 0;
  288. }
  289. static struct grub_jfs_data *
  290. grub_jfs_mount (grub_disk_t disk)
  291. {
  292. struct grub_jfs_data *data = 0;
  293. data = grub_malloc (sizeof (struct grub_jfs_data));
  294. if (!data)
  295. return 0;
  296. /* Read the superblock. */
  297. if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
  298. sizeof (struct grub_jfs_sblock), &data->sblock))
  299. goto fail;
  300. if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
  301. {
  302. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  303. goto fail;
  304. }
  305. data->disk = disk;
  306. data->pos = 0;
  307. data->linknest = 0;
  308. /* Read the inode of the first fileset. */
  309. if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
  310. sizeof (struct grub_jfs_inode), &data->fileset))
  311. goto fail;
  312. return data;
  313. fail:
  314. grub_free (data);
  315. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  316. grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
  317. return 0;
  318. }
  319. static struct grub_jfs_diropen *
  320. grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
  321. {
  322. struct grub_jfs_internal_dirent *de;
  323. struct grub_jfs_diropen *diro;
  324. int blk;
  325. de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
  326. if (!((grub_le_to_cpu32 (inode->mode)
  327. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
  328. {
  329. grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
  330. return 0;
  331. }
  332. diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
  333. if (!diro)
  334. return 0;
  335. diro->data = data;
  336. diro->inode = inode;
  337. /* Check if the entire tree is contained within the inode. */
  338. if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  339. {
  340. diro->leaf = inode->dir.dirents;
  341. diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
  342. diro->sorted = (char *) (inode->dir.header.sorted);
  343. diro->count = inode->dir.header.count;
  344. return diro;
  345. }
  346. diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
  347. if (!diro->dirpage)
  348. {
  349. grub_free (diro);
  350. return 0;
  351. }
  352. blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
  353. blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
  354. /* Read in the nodes until we are on the leaf node level. */
  355. do
  356. {
  357. int index;
  358. if (grub_disk_read (data->disk, blk, 0,
  359. grub_le_to_cpu32 (data->sblock.blksz),
  360. diro->dirpage->sorted))
  361. {
  362. grub_free (diro->dirpage);
  363. grub_free (diro);
  364. return 0;
  365. }
  366. de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
  367. index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  368. blk = (grub_le_to_cpu32 (de[index].ex.blk2)
  369. << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  370. - GRUB_DISK_SECTOR_BITS));
  371. } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
  372. diro->leaf = diro->dirpage->dirent;
  373. diro->next_leaf = diro->dirpage->next_dirent;
  374. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  375. diro->count = diro->dirpage->header.count;
  376. return diro;
  377. }
  378. static void
  379. grub_jfs_closedir (struct grub_jfs_diropen *diro)
  380. {
  381. if (!diro)
  382. return;
  383. grub_free (diro->dirpage);
  384. grub_free (diro);
  385. }
  386. static void
  387. addstr (grub_uint16_t *name, int ulen, grub_uint16_t *filename, int *strpos)
  388. {
  389. while (ulen--)
  390. filename[(*strpos)++] = *(name++);
  391. }
  392. /* Read in the next dirent from the directory described by DIRO. */
  393. static grub_err_t
  394. grub_jfs_getent (struct grub_jfs_diropen *diro)
  395. {
  396. int strpos = 0;
  397. struct grub_jfs_leaf_dirent *leaf;
  398. struct grub_jfs_leaf_next_dirent *next_leaf;
  399. int len;
  400. int nextent;
  401. grub_uint16_t filename[255];
  402. /* Add the unicode string to the utf16 filename buffer. */
  403. /* The last node, read in more. */
  404. if (diro->index == diro->count)
  405. {
  406. unsigned int next;
  407. /* If the inode contains the entry tree or if this was the last
  408. node, there is nothing to read. */
  409. if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
  410. || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
  411. return GRUB_ERR_OUT_OF_RANGE;
  412. next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
  413. next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
  414. - GRUB_DISK_SECTOR_BITS);
  415. if (grub_disk_read (diro->data->disk, next, 0,
  416. grub_le_to_cpu32 (diro->data->sblock.blksz),
  417. diro->dirpage->sorted))
  418. return grub_errno;
  419. diro->leaf = diro->dirpage->dirent;
  420. diro->next_leaf = diro->dirpage->next_dirent;
  421. diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  422. diro->count = diro->dirpage->header.count;
  423. diro->index = 0;
  424. }
  425. leaf = &diro->leaf[(int) diro->sorted[diro->index]];
  426. next_leaf = &diro->next_leaf[diro->index];
  427. len = leaf->len;
  428. if (!len)
  429. {
  430. diro->index++;
  431. return grub_jfs_getent (diro);
  432. }
  433. addstr (leaf->namepart, len < 11 ? len : 11, filename, &strpos);
  434. diro->ino = grub_le_to_cpu32 (leaf->inode);
  435. len -= 11;
  436. /* Move down to the leaf level. */
  437. nextent = leaf->next;
  438. if (leaf->next != 255)
  439. do
  440. {
  441. next_leaf = &diro->next_leaf[nextent];
  442. addstr (next_leaf->namepart, len < 15 ? len : 15, filename, &strpos);
  443. len -= 15;
  444. nextent = next_leaf->next;
  445. } while (next_leaf->next != 255 && len > 0);
  446. diro->index++;
  447. /* Convert the temporary UTF16 filename to UTF8. */
  448. *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
  449. return 0;
  450. }
  451. /* Read LEN bytes from the file described by DATA starting with byte
  452. POS. Return the amount of read bytes in READ. */
  453. static grub_ssize_t
  454. grub_jfs_read_file (struct grub_jfs_data *data,
  455. void (*read_hook) (grub_disk_addr_t sector,
  456. unsigned offset, unsigned length,
  457. void *closure),
  458. void *closure,
  459. int pos, grub_size_t len, char *buf)
  460. {
  461. int i;
  462. int blockcnt;
  463. blockcnt = ((len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
  464. / grub_le_to_cpu32 (data->sblock.blksz));
  465. for (i = pos / grub_le_to_cpu32 (data->sblock.blksz); i < blockcnt; i++)
  466. {
  467. int blknr;
  468. int blockoff = pos % grub_le_to_cpu32 (data->sblock.blksz);
  469. int blockend = grub_le_to_cpu32 (data->sblock.blksz);
  470. int skipfirst = 0;
  471. blknr = grub_jfs_blkno (data, &data->currinode, i);
  472. if (grub_errno)
  473. return -1;
  474. /* Last block. */
  475. if (i == blockcnt - 1)
  476. {
  477. blockend = (len + pos) % grub_le_to_cpu32 (data->sblock.blksz);
  478. if (!blockend)
  479. blockend = grub_le_to_cpu32 (data->sblock.blksz);
  480. }
  481. /* First block. */
  482. if (i == (pos / (int) grub_le_to_cpu32 (data->sblock.blksz)))
  483. {
  484. skipfirst = blockoff;
  485. blockend -= skipfirst;
  486. }
  487. data->disk->read_hook = read_hook;
  488. data->disk->closure = closure;
  489. grub_disk_read (data->disk,
  490. blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  491. - GRUB_DISK_SECTOR_BITS),
  492. skipfirst, blockend, buf);
  493. data->disk->read_hook = 0;
  494. if (grub_errno)
  495. return -1;
  496. buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
  497. }
  498. return len;
  499. }
  500. /* Find the file with the pathname PATH on the filesystem described by
  501. DATA. */
  502. static grub_err_t
  503. grub_jfs_find_file (struct grub_jfs_data *data, const char *path)
  504. {
  505. char fpath[grub_strlen (path)];
  506. char *name = fpath;
  507. char *next;
  508. unsigned int pos = 0;
  509. struct grub_jfs_diropen *diro;
  510. grub_strncpy (fpath, path, grub_strlen (path) + 1);
  511. if (grub_jfs_read_inode (data, GRUB_JFS_AGGR_INODE, &data->currinode))
  512. return grub_errno;
  513. /* Skip the first slashes. */
  514. while (*name == '/')
  515. {
  516. name++;
  517. if (!*name)
  518. return 0;
  519. }
  520. /* Extract the actual part from the pathname. */
  521. next = grub_strchr (name, '/');
  522. if (next)
  523. {
  524. while (*next == '/')
  525. {
  526. next[0] = '\0';
  527. next++;
  528. }
  529. }
  530. diro = grub_jfs_opendir (data, &data->currinode);
  531. if (!diro)
  532. return grub_errno;
  533. for (;;)
  534. {
  535. if (grub_strlen (name) == 0)
  536. return GRUB_ERR_NONE;
  537. if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
  538. break;
  539. /* Check if the current direntry matches the current part of the
  540. pathname. */
  541. if (!grub_strcmp (name, diro->name))
  542. {
  543. int ino = diro->ino;
  544. int dirino = grub_le_to_cpu32 (data->currinode.inode);
  545. grub_jfs_closedir (diro);
  546. diro = 0;
  547. if (grub_jfs_read_inode (data, ino, &data->currinode))
  548. break;
  549. /* Check if this is a symlink. */
  550. if ((grub_le_to_cpu32 (data->currinode.mode)
  551. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
  552. {
  553. grub_jfs_lookup_symlink (data, dirino);
  554. if (grub_errno)
  555. return grub_errno;
  556. }
  557. if (!next)
  558. return 0;
  559. pos = 0;
  560. name = next;
  561. next = grub_strchr (name, '/');
  562. if (next)
  563. {
  564. next[0] = '\0';
  565. next++;
  566. }
  567. /* Open this directory for reading dirents. */
  568. diro = grub_jfs_opendir (data, &data->currinode);
  569. if (!diro)
  570. return grub_errno;
  571. continue;
  572. }
  573. }
  574. grub_jfs_closedir (diro);
  575. grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
  576. return grub_errno;
  577. }
  578. static grub_err_t
  579. grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino)
  580. {
  581. int size = grub_le_to_cpu64 (data->currinode.size);
  582. char symlink[size + 1];
  583. if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
  584. return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
  585. if (size <= 128)
  586. grub_strncpy (symlink, (char *) (data->currinode.symlink.path), 128);
  587. else if (grub_jfs_read_file (data, 0, 0, 0, size, symlink) < 0)
  588. return grub_errno;
  589. symlink[size] = '\0';
  590. /* The symlink is an absolute path, go back to the root inode. */
  591. if (symlink[0] == '/')
  592. ino = 2;
  593. /* Now load in the old inode. */
  594. if (grub_jfs_read_inode (data, ino, &data->currinode))
  595. return grub_errno;
  596. grub_jfs_find_file (data, symlink);
  597. if (grub_errno)
  598. grub_error (grub_errno, "cannot follow symlink `%s'", symlink);
  599. return grub_errno;
  600. }
  601. static grub_err_t
  602. grub_jfs_dir (grub_device_t device, const char *path,
  603. int (*hook) (const char *filename,
  604. const struct grub_dirhook_info *info,
  605. void *closure),
  606. void *closure)
  607. {
  608. struct grub_jfs_data *data = 0;
  609. struct grub_jfs_diropen *diro = 0;
  610. grub_dl_ref (my_mod);
  611. data = grub_jfs_mount (device->disk);
  612. if (!data)
  613. goto fail;
  614. if (grub_jfs_find_file (data, path))
  615. goto fail;
  616. diro = grub_jfs_opendir (data, &data->currinode);
  617. if (!diro)
  618. goto fail;
  619. /* Iterate over the dirents in the directory that was found. */
  620. while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
  621. {
  622. struct grub_jfs_inode inode;
  623. struct grub_dirhook_info info;
  624. grub_memset (&info, 0, sizeof (info));
  625. if (grub_jfs_read_inode (data, diro->ino, &inode))
  626. goto fail;
  627. info.dir = (grub_le_to_cpu32 (inode.mode)
  628. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
  629. if (hook (diro->name, &info, closure))
  630. goto fail;
  631. }
  632. /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
  633. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  634. grub_errno = 0;
  635. fail:
  636. grub_jfs_closedir (diro);
  637. grub_free (data);
  638. grub_dl_unref (my_mod);
  639. return grub_errno;
  640. }
  641. /* Open a file named NAME and initialize FILE. */
  642. static grub_err_t
  643. grub_jfs_open (struct grub_file *file, const char *name)
  644. {
  645. struct grub_jfs_data *data;
  646. grub_dl_ref (my_mod);
  647. data = grub_jfs_mount (file->device->disk);
  648. if (!data)
  649. goto fail;
  650. grub_jfs_find_file (data, name);
  651. if (grub_errno)
  652. goto fail;
  653. /* It is only possible for open regular files. */
  654. if (! ((grub_le_to_cpu32 (data->currinode.mode)
  655. & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
  656. {
  657. grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
  658. goto fail;
  659. }
  660. file->data = data;
  661. file->size = grub_le_to_cpu64 (data->currinode.size);
  662. return 0;
  663. fail:
  664. grub_dl_unref (my_mod);
  665. grub_free (data);
  666. return grub_errno;
  667. }
  668. static grub_ssize_t
  669. grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
  670. {
  671. struct grub_jfs_data *data =
  672. (struct grub_jfs_data *) file->data;
  673. return grub_jfs_read_file (data, file->read_hook, file->closure,
  674. file->offset, len, buf);
  675. }
  676. static grub_err_t
  677. grub_jfs_close (grub_file_t file)
  678. {
  679. grub_free (file->data);
  680. grub_dl_unref (my_mod);
  681. return GRUB_ERR_NONE;
  682. }
  683. static grub_err_t
  684. grub_jfs_uuid (grub_device_t device, char **uuid)
  685. {
  686. struct grub_jfs_data *data;
  687. grub_disk_t disk = device->disk;
  688. grub_dl_ref (my_mod);
  689. data = grub_jfs_mount (disk);
  690. if (data)
  691. {
  692. *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
  693. "%02x%02x%02x%02x%02x%02x",
  694. data->sblock.uuid[0], data->sblock.uuid[1],
  695. data->sblock.uuid[2], data->sblock.uuid[3],
  696. data->sblock.uuid[4], data->sblock.uuid[5],
  697. data->sblock.uuid[6], data->sblock.uuid[7],
  698. data->sblock.uuid[8], data->sblock.uuid[9],
  699. data->sblock.uuid[10], data->sblock.uuid[11],
  700. data->sblock.uuid[12], data->sblock.uuid[13],
  701. data->sblock.uuid[14], data->sblock.uuid[15]);
  702. }
  703. else
  704. *uuid = NULL;
  705. grub_dl_unref (my_mod);
  706. grub_free (data);
  707. return grub_errno;
  708. }
  709. static grub_err_t
  710. grub_jfs_label (grub_device_t device, char **label)
  711. {
  712. struct grub_jfs_data *data;
  713. data = grub_jfs_mount (device->disk);
  714. if (data)
  715. *label = grub_strndup ((char *) (data->sblock.volname), 11);
  716. else
  717. *label = 0;
  718. return grub_errno;
  719. }
  720. static struct grub_fs grub_jfs_fs =
  721. {
  722. .name = "jfs",
  723. .dir = grub_jfs_dir,
  724. .open = grub_jfs_open,
  725. .read = grub_jfs_read,
  726. .close = grub_jfs_close,
  727. .label = grub_jfs_label,
  728. .uuid = grub_jfs_uuid,
  729. .next = 0
  730. };
  731. GRUB_MOD_INIT(jfs)
  732. {
  733. grub_fs_register (&grub_jfs_fs);
  734. my_mod = mod;
  735. }
  736. GRUB_MOD_FINI(jfs)
  737. {
  738. grub_fs_unregister (&grub_jfs_fs);
  739. }