xfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* xfs.c - XFS. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/fshelp.h>
  27. #define XFS_INODE_EXTENTS 9
  28. #define XFS_INODE_FORMAT_INO 1
  29. #define XFS_INODE_FORMAT_EXT 2
  30. #define XFS_INODE_FORMAT_BTREE 3
  31. struct grub_xfs_sblock
  32. {
  33. grub_uint8_t magic[4];
  34. grub_uint32_t bsize;
  35. grub_uint8_t unused1[24];
  36. grub_uint16_t uuid[8];
  37. grub_uint8_t unused2[8];
  38. grub_uint64_t rootino;
  39. grub_uint8_t unused3[20];
  40. grub_uint32_t agsize;
  41. grub_uint8_t unused4[20];
  42. grub_uint8_t label[12];
  43. grub_uint8_t log2_bsize;
  44. grub_uint8_t log2_sect;
  45. grub_uint8_t log2_inode;
  46. grub_uint8_t log2_inop;
  47. grub_uint8_t log2_agblk;
  48. grub_uint8_t unused6[67];
  49. grub_uint8_t log2_dirblk;
  50. } __attribute__ ((packed));
  51. struct grub_xfs_dir_header
  52. {
  53. grub_uint8_t count;
  54. grub_uint8_t smallino;
  55. union
  56. {
  57. grub_uint32_t i4;
  58. grub_uint64_t i8;
  59. } parent __attribute__ ((packed));
  60. } __attribute__ ((packed));
  61. struct grub_xfs_dir_entry
  62. {
  63. grub_uint8_t len;
  64. grub_uint16_t offset;
  65. char name[1];
  66. /* Inode number follows, 32 bits. */
  67. } __attribute__ ((packed));
  68. struct grub_xfs_dir2_entry
  69. {
  70. grub_uint64_t inode;
  71. grub_uint8_t len;
  72. } __attribute__ ((packed));
  73. typedef grub_uint32_t grub_xfs_extent[4];
  74. struct grub_xfs_btree_node
  75. {
  76. grub_uint8_t magic[4];
  77. grub_uint16_t level;
  78. grub_uint16_t numrecs;
  79. grub_uint64_t left;
  80. grub_uint64_t right;
  81. grub_uint64_t keys[1];
  82. } __attribute__ ((packed));
  83. struct grub_xfs_btree_root
  84. {
  85. grub_uint16_t level;
  86. grub_uint16_t numrecs;
  87. grub_uint64_t keys[1];
  88. } __attribute__ ((packed));
  89. struct grub_xfs_inode
  90. {
  91. grub_uint8_t magic[2];
  92. grub_uint16_t mode;
  93. grub_uint8_t version;
  94. grub_uint8_t format;
  95. grub_uint8_t unused2[50];
  96. grub_uint64_t size;
  97. grub_uint64_t nblocks;
  98. grub_uint32_t extsize;
  99. grub_uint32_t nextents;
  100. grub_uint8_t unused3[20];
  101. union
  102. {
  103. char raw[156];
  104. struct dir
  105. {
  106. struct grub_xfs_dir_header dirhead;
  107. struct grub_xfs_dir_entry direntry[1];
  108. } dir;
  109. grub_xfs_extent extents[XFS_INODE_EXTENTS];
  110. struct grub_xfs_btree_root btree;
  111. } data __attribute__ ((packed));
  112. } __attribute__ ((packed));
  113. struct grub_xfs_dirblock_tail
  114. {
  115. grub_uint32_t leaf_count;
  116. grub_uint32_t leaf_stale;
  117. } __attribute__ ((packed));
  118. struct grub_fshelp_node
  119. {
  120. struct grub_xfs_data *data;
  121. grub_uint64_t ino;
  122. int inode_read;
  123. struct grub_xfs_inode inode;
  124. };
  125. struct grub_xfs_data
  126. {
  127. struct grub_xfs_sblock sblock;
  128. grub_disk_t disk;
  129. int pos;
  130. int bsize;
  131. int agsize;
  132. struct grub_fshelp_node diropen;
  133. };
  134. static grub_dl_t my_mod;
  135. /* Filetype information as used in inodes. */
  136. #define FILETYPE_INO_MASK 0170000
  137. #define FILETYPE_INO_REG 0100000
  138. #define FILETYPE_INO_DIRECTORY 0040000
  139. #define FILETYPE_INO_SYMLINK 0120000
  140. #define GRUB_XFS_INO_AGBITS(data) \
  141. ((data)->sblock.log2_agblk + (data)->sblock.log2_inop)
  142. #define GRUB_XFS_INO_INOINAG(data, ino) \
  143. (grub_be_to_cpu64 (ino) & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1))
  144. #define GRUB_XFS_INO_AG(data,ino) \
  145. (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data))
  146. #define GRUB_XFS_FSB_TO_BLOCK(data, fsb) \
  147. (((fsb) >> (data)->sblock.log2_agblk) * (data)->agsize \
  148. + ((fsb) & ((1LL << (data)->sblock.log2_agblk) - 1)))
  149. #define GRUB_XFS_EXTENT_OFFSET(exts,ex) \
  150. ((grub_be_to_cpu32 (exts[ex][0]) & ~(1 << 31)) << 23 \
  151. | grub_be_to_cpu32 (exts[ex][1]) >> 9)
  152. #define GRUB_XFS_EXTENT_BLOCK(exts,ex) \
  153. ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex][1]) \
  154. & (0x1ff)) << 43 \
  155. | (grub_uint64_t) grub_be_to_cpu32 (exts[ex][2]) << 11 \
  156. | grub_be_to_cpu32 (exts[ex][3]) >> 21)
  157. #define GRUB_XFS_EXTENT_SIZE(exts,ex) \
  158. (grub_be_to_cpu32 (exts[ex][3]) & ((1 << 20) - 1))
  159. #define GRUB_XFS_ROUND_TO_DIRENT(pos) ((((pos) + 8 - 1) / 8) * 8)
  160. #define GRUB_XFS_NEXT_DIRENT(pos,len) \
  161. (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
  162. static inline grub_uint64_t
  163. grub_xfs_inode_block (struct grub_xfs_data *data,
  164. grub_uint64_t ino)
  165. {
  166. long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
  167. long long ag = GRUB_XFS_INO_AG (data, ino);
  168. long long block;
  169. block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
  170. block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
  171. return block;
  172. }
  173. static inline int
  174. grub_xfs_inode_offset (struct grub_xfs_data *data,
  175. grub_uint64_t ino)
  176. {
  177. int inoag = GRUB_XFS_INO_INOINAG (data, ino);
  178. return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
  179. data->sblock.log2_inode);
  180. }
  181. static grub_err_t
  182. grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
  183. struct grub_xfs_inode *inode)
  184. {
  185. grub_uint64_t block = grub_xfs_inode_block (data, ino);
  186. int offset = grub_xfs_inode_offset (data, ino);
  187. /* Read the inode. */
  188. if (grub_disk_read (data->disk, block, offset,
  189. 1 << data->sblock.log2_inode, inode))
  190. return grub_errno;
  191. if (grub_strncmp ((char *) inode->magic, "IN", 2))
  192. return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
  193. return 0;
  194. }
  195. static grub_disk_addr_t
  196. grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  197. {
  198. struct grub_xfs_btree_node *leaf = 0;
  199. int ex, nrec;
  200. grub_xfs_extent *exts;
  201. grub_uint64_t ret = 0;
  202. if (node->inode.format == XFS_INODE_FORMAT_BTREE)
  203. {
  204. grub_uint64_t *keys;
  205. leaf = grub_malloc (node->data->sblock.bsize);
  206. if (leaf == 0)
  207. return 0;
  208. nrec = grub_be_to_cpu16 (node->inode.data.btree.numrecs);
  209. keys = &node->inode.data.btree.keys[0];
  210. do
  211. {
  212. int i;
  213. for (i = 0; i < nrec; i++)
  214. {
  215. if (fileblock < grub_be_to_cpu64 (keys[i]))
  216. break;
  217. }
  218. /* Sparse block. */
  219. if (i == 0)
  220. {
  221. grub_free (leaf);
  222. return 0;
  223. }
  224. if (grub_disk_read (node->data->disk,
  225. grub_be_to_cpu64 (keys[i - 1 + nrec])
  226. << (node->data->sblock.log2_bsize
  227. - GRUB_DISK_SECTOR_BITS),
  228. 0, node->data->sblock.bsize, leaf))
  229. return 0;
  230. if (grub_strncmp ((char *) leaf->magic, "BMAP", 4))
  231. {
  232. grub_free (leaf);
  233. grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
  234. return 0;
  235. }
  236. nrec = grub_be_to_cpu16 (leaf->numrecs);
  237. keys = &leaf->keys[0];
  238. } while (leaf->level);
  239. exts = (grub_xfs_extent *) keys;
  240. }
  241. else if (node->inode.format == XFS_INODE_FORMAT_EXT)
  242. {
  243. nrec = grub_be_to_cpu32 (node->inode.nextents);
  244. exts = &node->inode.data.extents[0];
  245. }
  246. else
  247. {
  248. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  249. "XFS does not support inode format %d yet",
  250. node->inode.format);
  251. return 0;
  252. }
  253. /* Iterate over each extent to figure out which extent has
  254. the block we are looking for. */
  255. for (ex = 0; ex < nrec; ex++)
  256. {
  257. grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
  258. grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
  259. grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
  260. /* Sparse block. */
  261. if (fileblock < offset)
  262. break;
  263. else if (fileblock < offset + size)
  264. {
  265. ret = (fileblock - offset + start);
  266. break;
  267. }
  268. }
  269. if (leaf)
  270. grub_free (leaf);
  271. return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
  272. }
  273. /* Read LEN bytes from the file described by DATA starting with byte
  274. POS. Return the amount of read bytes in READ. */
  275. static grub_ssize_t
  276. grub_xfs_read_file (grub_fshelp_node_t node,
  277. void (*read_hook) (grub_disk_addr_t sector,
  278. unsigned offset, unsigned length,
  279. void *closure),
  280. void *closure, int flags,
  281. int pos, grub_size_t len, char *buf)
  282. {
  283. return grub_fshelp_read_file (node->data->disk, node, read_hook, closure,
  284. flags, pos, len, buf, grub_xfs_read_block,
  285. grub_be_to_cpu64 (node->inode.size),
  286. node->data->sblock.log2_bsize
  287. - GRUB_DISK_SECTOR_BITS);
  288. }
  289. static char *
  290. grub_xfs_read_symlink (grub_fshelp_node_t node)
  291. {
  292. int size = grub_be_to_cpu64 (node->inode.size);
  293. switch (node->inode.format)
  294. {
  295. case XFS_INODE_FORMAT_INO:
  296. return grub_strndup (node->inode.data.raw, size);
  297. case XFS_INODE_FORMAT_EXT:
  298. {
  299. char *symlink;
  300. grub_ssize_t numread;
  301. symlink = grub_malloc (size + 1);
  302. if (!symlink)
  303. return 0;
  304. numread = grub_xfs_read_file (node, 0, 0, 0, 0, size, symlink);
  305. if (numread != size)
  306. {
  307. grub_free (symlink);
  308. return 0;
  309. }
  310. symlink[size] = '\0';
  311. return symlink;
  312. }
  313. }
  314. return 0;
  315. }
  316. static enum grub_fshelp_filetype
  317. grub_xfs_mode_to_filetype (grub_uint16_t mode)
  318. {
  319. if ((grub_be_to_cpu16 (mode)
  320. & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
  321. return GRUB_FSHELP_DIR;
  322. else if ((grub_be_to_cpu16 (mode)
  323. & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
  324. return GRUB_FSHELP_SYMLINK;
  325. else if ((grub_be_to_cpu16 (mode)
  326. & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
  327. return GRUB_FSHELP_REG;
  328. return GRUB_FSHELP_UNKNOWN;
  329. }
  330. struct grub_xfs_iterate_dir_closure
  331. {
  332. int (*hook) (const char *filename,
  333. enum grub_fshelp_filetype filetype,
  334. grub_fshelp_node_t node,
  335. void *closure);
  336. void *closure;
  337. struct grub_fshelp_node *diro;
  338. };
  339. static int
  340. call_hook (grub_uint64_t ino, char *filename,
  341. struct grub_xfs_iterate_dir_closure *c)
  342. {
  343. struct grub_fshelp_node *fdiro;
  344. fdiro = grub_malloc (sizeof (struct grub_fshelp_node)
  345. - sizeof (struct grub_xfs_inode)
  346. + (1 << c->diro->data->sblock.log2_inode));
  347. if (!fdiro)
  348. return 0;
  349. /* The inode should be read, otherwise the filetype can
  350. not be determined. */
  351. fdiro->ino = ino;
  352. fdiro->inode_read = 1;
  353. fdiro->data = c->diro->data;
  354. grub_xfs_read_inode (fdiro->data, ino, &fdiro->inode);
  355. return c->hook (filename,
  356. grub_xfs_mode_to_filetype (fdiro->inode.mode),
  357. fdiro, c->closure);
  358. }
  359. static int
  360. grub_xfs_iterate_dir (grub_fshelp_node_t dir,
  361. int (*hook) (const char *filename,
  362. enum grub_fshelp_filetype filetype,
  363. grub_fshelp_node_t node,
  364. void *closure),
  365. void *closure)
  366. {
  367. struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
  368. struct grub_xfs_iterate_dir_closure c;
  369. c.hook = hook;
  370. c.closure = closure;
  371. c.diro = diro;
  372. switch (diro->inode.format)
  373. {
  374. case XFS_INODE_FORMAT_INO:
  375. {
  376. struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0];
  377. int smallino = !diro->inode.data.dir.dirhead.smallino;
  378. int i;
  379. grub_uint64_t parent;
  380. /* If small inode numbers are used to pack the direntry, the
  381. parent inode number is small too. */
  382. if (smallino)
  383. {
  384. parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent.i4);
  385. parent = grub_cpu_to_be64 (parent);
  386. /* The header is a bit smaller than usual. */
  387. de = (struct grub_xfs_dir_entry *) ((char *) de - 4);
  388. }
  389. else
  390. {
  391. parent = diro->inode.data.dir.dirhead.parent.i8;
  392. }
  393. /* Synthesize the direntries for `.' and `..'. */
  394. if (call_hook (diro->ino, ".", &c))
  395. return 1;
  396. if (call_hook (parent, "..", &c))
  397. return 1;
  398. for (i = 0; i < diro->inode.data.dir.dirhead.count; i++)
  399. {
  400. grub_uint64_t ino;
  401. void *inopos = (((char *) de)
  402. + sizeof (struct grub_xfs_dir_entry)
  403. + de->len - 1);
  404. char name[de->len + 1];
  405. if (smallino)
  406. {
  407. ino = grub_be_to_cpu32 (*(grub_uint32_t *) inopos);
  408. ino = grub_cpu_to_be64 (ino);
  409. }
  410. else
  411. ino = *(grub_uint64_t *) inopos;
  412. grub_memcpy (name, de->name, de->len);
  413. name[de->len] = '\0';
  414. if (call_hook (ino, name, &c))
  415. return 1;
  416. de = ((struct grub_xfs_dir_entry *)
  417. (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len
  418. + ((smallino ? sizeof (grub_uint32_t)
  419. : sizeof (grub_uint64_t))) - 1));
  420. }
  421. break;
  422. }
  423. case XFS_INODE_FORMAT_BTREE:
  424. case XFS_INODE_FORMAT_EXT:
  425. {
  426. grub_ssize_t numread;
  427. char *dirblock;
  428. grub_uint64_t blk;
  429. int dirblk_size, dirblk_log2;
  430. dirblk_log2 = (dir->data->sblock.log2_bsize
  431. + dir->data->sblock.log2_dirblk);
  432. dirblk_size = 1 << dirblk_log2;
  433. dirblock = grub_malloc (dirblk_size);
  434. if (! dirblock)
  435. return 0;
  436. /* Iterate over every block the directory has. */
  437. for (blk = 0;
  438. blk < (grub_be_to_cpu64 (dir->inode.size)
  439. >> dirblk_log2);
  440. blk++)
  441. {
  442. /* The header is skipped, the first direntry is stored
  443. from byte 16. */
  444. int pos = 16;
  445. int entries;
  446. int tail_start = (dirblk_size
  447. - sizeof (struct grub_xfs_dirblock_tail));
  448. struct grub_xfs_dirblock_tail *tail;
  449. tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start];
  450. numread = grub_xfs_read_file (dir, 0, 0, 0,
  451. blk << dirblk_log2,
  452. dirblk_size, dirblock);
  453. if (numread != dirblk_size)
  454. return 0;
  455. entries = (grub_be_to_cpu32 (tail->leaf_count)
  456. - grub_be_to_cpu32 (tail->leaf_stale));
  457. /* Iterate over all entries within this block. */
  458. while (pos < (dirblk_size
  459. - (int) sizeof (struct grub_xfs_dir2_entry)))
  460. {
  461. struct grub_xfs_dir2_entry *direntry;
  462. grub_uint16_t *freetag;
  463. char *filename;
  464. direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
  465. freetag = (grub_uint16_t *) direntry;
  466. if (*freetag == 0XFFFF)
  467. {
  468. grub_uint16_t *skip = (grub_uint16_t *) (freetag + 1);
  469. /* This entry is not used, go to the next one. */
  470. pos += grub_be_to_cpu16 (*skip);
  471. continue;
  472. }
  473. filename = &dirblock[pos + sizeof (*direntry)];
  474. /* The byte after the filename is for the tag, which
  475. is not used by GRUB. So it can be overwritten. */
  476. filename[direntry->len] = '\0';
  477. if (call_hook (direntry->inode, filename, &c))
  478. {
  479. grub_free (dirblock);
  480. return 1;
  481. }
  482. /* Check if last direntry in this block is
  483. reached. */
  484. entries--;
  485. if (!entries)
  486. break;
  487. /* Select the next directory entry. */
  488. pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len);
  489. pos = GRUB_XFS_ROUND_TO_DIRENT (pos);
  490. }
  491. }
  492. grub_free (dirblock);
  493. break;
  494. }
  495. default:
  496. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  497. "XFS does not support inode format %d yet",
  498. diro->inode.format);
  499. }
  500. return 0;
  501. }
  502. static struct grub_xfs_data *
  503. grub_xfs_mount (grub_disk_t disk)
  504. {
  505. struct grub_xfs_data *data = 0;
  506. data = grub_zalloc (sizeof (struct grub_xfs_data));
  507. if (!data)
  508. return 0;
  509. /* Read the superblock. */
  510. if (grub_disk_read (disk, 0, 0,
  511. sizeof (struct grub_xfs_sblock), &data->sblock))
  512. goto fail;
  513. if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4))
  514. {
  515. grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
  516. goto fail;
  517. }
  518. data = grub_realloc (data,
  519. sizeof (struct grub_xfs_data)
  520. - sizeof (struct grub_xfs_inode)
  521. + (1 << data->sblock.log2_inode));
  522. if (! data)
  523. goto fail;
  524. data->diropen.data = data;
  525. data->diropen.ino = data->sblock.rootino;
  526. data->diropen.inode_read = 1;
  527. data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
  528. data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
  529. data->disk = disk;
  530. data->pos = 0;
  531. grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
  532. return data;
  533. fail:
  534. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  535. grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
  536. grub_free (data);
  537. return 0;
  538. }
  539. struct grub_xfs_dir_closure
  540. {
  541. int (*hook) (const char *filename,
  542. const struct grub_dirhook_info *info,
  543. void *closure);
  544. void *closure;
  545. };
  546. static int
  547. iterate (const char *filename,
  548. enum grub_fshelp_filetype filetype,
  549. grub_fshelp_node_t node,
  550. void *closure)
  551. {
  552. struct grub_xfs_dir_closure *c = closure;
  553. struct grub_dirhook_info info;
  554. grub_memset (&info, 0, sizeof (info));
  555. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  556. grub_free (node);
  557. return c->hook (filename, &info, c->closure);
  558. }
  559. static grub_err_t
  560. grub_xfs_dir (grub_device_t device, const char *path,
  561. int (*hook) (const char *filename,
  562. const struct grub_dirhook_info *info,
  563. void *closure),
  564. void *closure)
  565. {
  566. struct grub_xfs_data *data = 0;
  567. struct grub_fshelp_node *fdiro = 0;
  568. struct grub_xfs_dir_closure c;
  569. grub_dl_ref (my_mod);
  570. data = grub_xfs_mount (device->disk);
  571. if (!data)
  572. goto mount_fail;
  573. grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  574. 0, grub_xfs_read_symlink, GRUB_FSHELP_DIR);
  575. if (grub_errno)
  576. goto fail;
  577. c.hook = hook;
  578. c.closure = closure;
  579. grub_xfs_iterate_dir (fdiro, iterate, &c);
  580. fail:
  581. if (fdiro != &data->diropen)
  582. grub_free (fdiro);
  583. grub_free (data);
  584. mount_fail:
  585. grub_dl_unref (my_mod);
  586. return grub_errno;
  587. }
  588. /* Open a file named NAME and initialize FILE. */
  589. static grub_err_t
  590. grub_xfs_open (struct grub_file *file, const char *name)
  591. {
  592. struct grub_xfs_data *data;
  593. struct grub_fshelp_node *fdiro = 0;
  594. grub_dl_ref (my_mod);
  595. data = grub_xfs_mount (file->device->disk);
  596. if (!data)
  597. goto mount_fail;
  598. grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  599. 0, grub_xfs_read_symlink, GRUB_FSHELP_REG);
  600. if (grub_errno)
  601. goto fail;
  602. if (!fdiro->inode_read)
  603. {
  604. grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
  605. if (grub_errno)
  606. goto fail;
  607. }
  608. if (fdiro != &data->diropen)
  609. grub_memcpy (&data->diropen, fdiro,
  610. sizeof (struct grub_fshelp_node)
  611. - sizeof (struct grub_xfs_inode)
  612. + (1 << data->sblock.log2_inode));
  613. file->size = grub_be_to_cpu64 (data->diropen.inode.size);
  614. file->data = data;
  615. file->offset = 0;
  616. return 0;
  617. fail:
  618. if (fdiro != &data->diropen)
  619. grub_free (fdiro);
  620. grub_free (data);
  621. mount_fail:
  622. grub_dl_unref (my_mod);
  623. return grub_errno;
  624. }
  625. static grub_ssize_t
  626. grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
  627. {
  628. struct grub_xfs_data *data =
  629. (struct grub_xfs_data *) file->data;
  630. return grub_xfs_read_file (&data->diropen, file->read_hook, file->closure,
  631. file->flags, file->offset, len, buf);
  632. }
  633. static grub_err_t
  634. grub_xfs_close (grub_file_t file)
  635. {
  636. grub_free (file->data);
  637. grub_dl_unref (my_mod);
  638. return GRUB_ERR_NONE;
  639. }
  640. static grub_err_t
  641. grub_xfs_label (grub_device_t device, char **label)
  642. {
  643. struct grub_xfs_data *data;
  644. grub_disk_t disk = device->disk;
  645. grub_dl_ref (my_mod);
  646. data = grub_xfs_mount (disk);
  647. if (data)
  648. *label = grub_strndup ((char *) (data->sblock.label), 12);
  649. else
  650. *label = 0;
  651. grub_dl_unref (my_mod);
  652. grub_free (data);
  653. return grub_errno;
  654. }
  655. static grub_err_t
  656. grub_xfs_uuid (grub_device_t device, char **uuid)
  657. {
  658. struct grub_xfs_data *data;
  659. grub_disk_t disk = device->disk;
  660. grub_dl_ref (my_mod);
  661. data = grub_xfs_mount (disk);
  662. if (data)
  663. {
  664. *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  665. grub_be_to_cpu16 (data->sblock.uuid[0]),
  666. grub_be_to_cpu16 (data->sblock.uuid[1]),
  667. grub_be_to_cpu16 (data->sblock.uuid[2]),
  668. grub_be_to_cpu16 (data->sblock.uuid[3]),
  669. grub_be_to_cpu16 (data->sblock.uuid[4]),
  670. grub_be_to_cpu16 (data->sblock.uuid[5]),
  671. grub_be_to_cpu16 (data->sblock.uuid[6]),
  672. grub_be_to_cpu16 (data->sblock.uuid[7]));
  673. }
  674. else
  675. *uuid = NULL;
  676. grub_dl_unref (my_mod);
  677. grub_free (data);
  678. return grub_errno;
  679. }
  680. static struct grub_fs grub_xfs_fs =
  681. {
  682. .name = "xfs",
  683. .dir = grub_xfs_dir,
  684. .open = grub_xfs_open,
  685. .read = grub_xfs_read,
  686. .close = grub_xfs_close,
  687. .label = grub_xfs_label,
  688. .uuid = grub_xfs_uuid,
  689. .next = 0
  690. };
  691. GRUB_MOD_INIT(xfs)
  692. {
  693. grub_fs_register (&grub_xfs_fs);
  694. my_mod = mod;
  695. }
  696. GRUB_MOD_FINI(xfs)
  697. {
  698. grub_fs_unregister (&grub_xfs_fs);
  699. }