xfs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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/time.h>
  26. #include <grub/types.h>
  27. #include <grub/fshelp.h>
  28. #include <grub/safemath.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. #define XFS_INODE_EXTENTS 9
  31. #define XFS_INODE_FORMAT_INO 1
  32. #define XFS_INODE_FORMAT_EXT 2
  33. #define XFS_INODE_FORMAT_BTREE 3
  34. /* Superblock version field flags */
  35. #define XFS_SB_VERSION_NUMBITS 0x000f
  36. #define XFS_SB_VERSION_ATTRBIT 0x0010
  37. #define XFS_SB_VERSION_NLINKBIT 0x0020
  38. #define XFS_SB_VERSION_QUOTABIT 0x0040
  39. #define XFS_SB_VERSION_ALIGNBIT 0x0080
  40. #define XFS_SB_VERSION_DALIGNBIT 0x0100
  41. #define XFS_SB_VERSION_LOGV2BIT 0x0400
  42. #define XFS_SB_VERSION_SECTORBIT 0x0800
  43. #define XFS_SB_VERSION_EXTFLGBIT 0x1000
  44. #define XFS_SB_VERSION_DIRV2BIT 0x2000
  45. #define XFS_SB_VERSION_MOREBITSBIT 0x8000
  46. #define XFS_SB_VERSION_BITS_SUPPORTED \
  47. (XFS_SB_VERSION_NUMBITS | \
  48. XFS_SB_VERSION_ATTRBIT | \
  49. XFS_SB_VERSION_NLINKBIT | \
  50. XFS_SB_VERSION_QUOTABIT | \
  51. XFS_SB_VERSION_ALIGNBIT | \
  52. XFS_SB_VERSION_DALIGNBIT | \
  53. XFS_SB_VERSION_LOGV2BIT | \
  54. XFS_SB_VERSION_SECTORBIT | \
  55. XFS_SB_VERSION_EXTFLGBIT | \
  56. XFS_SB_VERSION_DIRV2BIT | \
  57. XFS_SB_VERSION_MOREBITSBIT)
  58. /* Recognized xfs format versions */
  59. #define XFS_SB_VERSION_4 4 /* Good old XFS filesystem */
  60. #define XFS_SB_VERSION_5 5 /* CRC enabled filesystem */
  61. /* features2 field flags */
  62. #define XFS_SB_VERSION2_LAZYSBCOUNTBIT 0x00000002 /* Superblk counters */
  63. #define XFS_SB_VERSION2_ATTR2BIT 0x00000008 /* Inline attr rework */
  64. #define XFS_SB_VERSION2_PROJID32BIT 0x00000080 /* 32-bit project ids */
  65. #define XFS_SB_VERSION2_FTYPE 0x00000200 /* inode type in dir */
  66. #define XFS_SB_VERSION2_BITS_SUPPORTED \
  67. (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
  68. XFS_SB_VERSION2_ATTR2BIT | \
  69. XFS_SB_VERSION2_PROJID32BIT | \
  70. XFS_SB_VERSION2_FTYPE)
  71. /* Inode flags2 flags */
  72. #define XFS_DIFLAG2_BIGTIME_BIT 3
  73. #define XFS_DIFLAG2_BIGTIME (1 << XFS_DIFLAG2_BIGTIME_BIT)
  74. /* incompat feature flags */
  75. #define XFS_SB_FEAT_INCOMPAT_FTYPE (1 << 0) /* filetype in dirent */
  76. #define XFS_SB_FEAT_INCOMPAT_SPINODES (1 << 1) /* sparse inode chunks */
  77. #define XFS_SB_FEAT_INCOMPAT_META_UUID (1 << 2) /* metadata UUID */
  78. #define XFS_SB_FEAT_INCOMPAT_BIGTIME (1 << 3) /* large timestamps */
  79. #define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4) /* needs xfs_repair */
  80. /*
  81. * Directory entries with ftype are explicitly handled by GRUB code.
  82. *
  83. * We do not currently read the inode btrees, so it is safe to read filesystems
  84. * with the XFS_SB_FEAT_INCOMPAT_SPINODES feature.
  85. *
  86. * We do not currently verify metadata UUID, so it is safe to read filesystems
  87. * with the XFS_SB_FEAT_INCOMPAT_META_UUID feature.
  88. */
  89. #define XFS_SB_FEAT_INCOMPAT_SUPPORTED \
  90. (XFS_SB_FEAT_INCOMPAT_FTYPE | \
  91. XFS_SB_FEAT_INCOMPAT_SPINODES | \
  92. XFS_SB_FEAT_INCOMPAT_META_UUID | \
  93. XFS_SB_FEAT_INCOMPAT_BIGTIME | \
  94. XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
  95. struct grub_xfs_sblock
  96. {
  97. grub_uint8_t magic[4];
  98. grub_uint32_t bsize;
  99. grub_uint8_t unused1[24];
  100. grub_uint16_t uuid[8];
  101. grub_uint8_t unused2[8];
  102. grub_uint64_t rootino;
  103. grub_uint8_t unused3[20];
  104. grub_uint32_t agsize;
  105. grub_uint8_t unused4[12];
  106. grub_uint16_t version;
  107. grub_uint8_t unused5[6];
  108. grub_uint8_t label[12];
  109. grub_uint8_t log2_bsize;
  110. grub_uint8_t log2_sect;
  111. grub_uint8_t log2_inode;
  112. grub_uint8_t log2_inop;
  113. grub_uint8_t log2_agblk;
  114. grub_uint8_t unused6[67];
  115. grub_uint8_t log2_dirblk;
  116. grub_uint8_t unused7[7];
  117. grub_uint32_t features2;
  118. grub_uint8_t unused8[4];
  119. grub_uint32_t sb_features_compat;
  120. grub_uint32_t sb_features_ro_compat;
  121. grub_uint32_t sb_features_incompat;
  122. grub_uint32_t sb_features_log_incompat;
  123. } GRUB_PACKED;
  124. struct grub_xfs_dir_header
  125. {
  126. grub_uint8_t count;
  127. grub_uint8_t largeino;
  128. union
  129. {
  130. grub_uint32_t i4;
  131. grub_uint64_t i8;
  132. } GRUB_PACKED parent;
  133. } GRUB_PACKED;
  134. /* Structure for directory entry inlined in the inode */
  135. struct grub_xfs_dir_entry
  136. {
  137. grub_uint8_t len;
  138. grub_uint16_t offset;
  139. char name[1];
  140. /* Inode number follows, 32 / 64 bits. */
  141. } GRUB_PACKED;
  142. /* Structure for directory entry in a block */
  143. struct grub_xfs_dir2_entry
  144. {
  145. grub_uint64_t inode;
  146. grub_uint8_t len;
  147. } GRUB_PACKED;
  148. struct grub_xfs_extent
  149. {
  150. /* This should be a bitfield but bietfields are unportable, so just have
  151. a raw array and functions extracting useful info from it.
  152. */
  153. grub_uint32_t raw[4];
  154. } GRUB_PACKED;
  155. struct grub_xfs_btree_node
  156. {
  157. grub_uint8_t magic[4];
  158. grub_uint16_t level;
  159. grub_uint16_t numrecs;
  160. grub_uint64_t left;
  161. grub_uint64_t right;
  162. /* In V5 here follow crc, uuid, etc. */
  163. /* Then follow keys and block pointers */
  164. } GRUB_PACKED;
  165. struct grub_xfs_btree_root
  166. {
  167. grub_uint16_t level;
  168. grub_uint16_t numrecs;
  169. grub_uint64_t keys[1];
  170. } GRUB_PACKED;
  171. struct grub_xfs_time_legacy
  172. {
  173. grub_uint32_t sec;
  174. grub_uint32_t nanosec;
  175. } GRUB_PACKED;
  176. /*
  177. * The struct grub_xfs_inode layout was taken from the
  178. * struct xfs_dinode_core which is described here:
  179. * https://mirrors.edge.kernel.org/pub/linux/utils/fs/xfs/docs/xfs_filesystem_structure.pdf
  180. */
  181. struct grub_xfs_inode
  182. {
  183. grub_uint8_t magic[2];
  184. grub_uint16_t mode;
  185. grub_uint8_t version;
  186. grub_uint8_t format;
  187. grub_uint8_t unused2[26];
  188. grub_uint64_t atime;
  189. grub_uint64_t mtime;
  190. grub_uint64_t ctime;
  191. grub_uint64_t size;
  192. grub_uint64_t nblocks;
  193. grub_uint32_t extsize;
  194. grub_uint32_t nextents;
  195. grub_uint16_t unused3;
  196. grub_uint8_t fork_offset;
  197. grub_uint8_t unused4[17]; /* Last member of inode v2. */
  198. grub_uint8_t unused5[20]; /* First member of inode v3. */
  199. grub_uint64_t flags2;
  200. grub_uint8_t unused6[48]; /* Last member of inode v3. */
  201. } GRUB_PACKED;
  202. #define XFS_V3_INODE_SIZE sizeof(struct grub_xfs_inode)
  203. /* Size of struct grub_xfs_inode v2, up to unused4 member included. */
  204. #define XFS_V2_INODE_SIZE (XFS_V3_INODE_SIZE - 76)
  205. struct grub_xfs_dirblock_tail
  206. {
  207. grub_uint32_t leaf_count;
  208. grub_uint32_t leaf_stale;
  209. } GRUB_PACKED;
  210. struct grub_fshelp_node
  211. {
  212. struct grub_xfs_data *data;
  213. grub_uint64_t ino;
  214. int inode_read;
  215. struct grub_xfs_inode inode;
  216. };
  217. struct grub_xfs_data
  218. {
  219. grub_size_t data_size;
  220. struct grub_xfs_sblock sblock;
  221. grub_disk_t disk;
  222. int pos;
  223. int bsize;
  224. grub_uint32_t agsize;
  225. unsigned int hasftype:1;
  226. unsigned int hascrc:1;
  227. struct grub_fshelp_node diropen;
  228. };
  229. static grub_dl_t my_mod;
  230. static int grub_xfs_sb_hascrc(struct grub_xfs_data *data)
  231. {
  232. return (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
  233. grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5);
  234. }
  235. static int grub_xfs_sb_hasftype(struct grub_xfs_data *data)
  236. {
  237. if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
  238. grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5) &&
  239. data->sblock.sb_features_incompat & grub_cpu_to_be32_compile_time(XFS_SB_FEAT_INCOMPAT_FTYPE))
  240. return 1;
  241. if (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
  242. data->sblock.features2 & grub_cpu_to_be32_compile_time(XFS_SB_VERSION2_FTYPE))
  243. return 1;
  244. return 0;
  245. }
  246. static int grub_xfs_sb_valid(struct grub_xfs_data *data)
  247. {
  248. grub_dprintf("xfs", "Validating superblock\n");
  249. if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)
  250. || data->sblock.log2_bsize < GRUB_DISK_SECTOR_BITS
  251. || ((int) data->sblock.log2_bsize
  252. + (int) data->sblock.log2_dirblk) >= 27)
  253. {
  254. grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
  255. return 0;
  256. }
  257. if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
  258. grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5))
  259. {
  260. grub_dprintf("xfs", "XFS v5 superblock detected\n");
  261. if (data->sblock.sb_features_incompat &
  262. grub_cpu_to_be32_compile_time(~XFS_SB_FEAT_INCOMPAT_SUPPORTED))
  263. {
  264. grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported "
  265. "incompatible features");
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. else if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
  271. grub_cpu_to_be16_compile_time(XFS_SB_VERSION_4))
  272. {
  273. grub_dprintf("xfs", "XFS v4 superblock detected\n");
  274. if (!(data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_DIRV2BIT)))
  275. {
  276. grub_error (GRUB_ERR_BAD_FS, "XFS filesystem without V2 directories "
  277. "is unsupported");
  278. return 0;
  279. }
  280. if (data->sblock.version & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION_BITS_SUPPORTED) ||
  281. (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
  282. data->sblock.features2 & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION2_BITS_SUPPORTED)))
  283. {
  284. grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported version "
  285. "bits");
  286. return 0;
  287. }
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. static int
  293. grub_xfs_sb_needs_repair (struct grub_xfs_data *data)
  294. {
  295. return ((data->sblock.version &
  296. grub_cpu_to_be16_compile_time (XFS_SB_VERSION_NUMBITS)) ==
  297. grub_cpu_to_be16_compile_time (XFS_SB_VERSION_5) &&
  298. (data->sblock.sb_features_incompat &
  299. grub_cpu_to_be32_compile_time (XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)));
  300. }
  301. /* Filetype information as used in inodes. */
  302. #define FILETYPE_INO_MASK 0170000
  303. #define FILETYPE_INO_REG 0100000
  304. #define FILETYPE_INO_DIRECTORY 0040000
  305. #define FILETYPE_INO_SYMLINK 0120000
  306. static inline int
  307. GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
  308. {
  309. return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
  310. }
  311. static inline grub_uint64_t
  312. GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
  313. grub_uint64_t ino)
  314. {
  315. return (ino & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
  316. }
  317. static inline grub_uint64_t
  318. GRUB_XFS_INO_AG (struct grub_xfs_data *data,
  319. grub_uint64_t ino)
  320. {
  321. return (ino >> GRUB_XFS_INO_AGBITS (data));
  322. }
  323. static inline grub_disk_addr_t
  324. GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
  325. {
  326. return ((fsb >> data->sblock.log2_agblk) * data->agsize
  327. + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
  328. }
  329. static inline grub_uint64_t
  330. GRUB_XFS_EXTENT_OFFSET (struct grub_xfs_extent *exts, int ex)
  331. {
  332. return ((grub_be_to_cpu32 (exts[ex].raw[0]) & ~(1 << 31)) << 23
  333. | grub_be_to_cpu32 (exts[ex].raw[1]) >> 9);
  334. }
  335. static inline grub_uint64_t
  336. GRUB_XFS_EXTENT_BLOCK (struct grub_xfs_extent *exts, int ex)
  337. {
  338. return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex].raw[1])
  339. & (0x1ff)) << 43
  340. | (grub_uint64_t) grub_be_to_cpu32 (exts[ex].raw[2]) << 11
  341. | grub_be_to_cpu32 (exts[ex].raw[3]) >> 21);
  342. }
  343. static inline grub_uint64_t
  344. GRUB_XFS_EXTENT_SIZE (struct grub_xfs_extent *exts, int ex)
  345. {
  346. return (grub_be_to_cpu32 (exts[ex].raw[3]) & ((1 << 21) - 1));
  347. }
  348. static inline grub_uint64_t
  349. grub_xfs_inode_block (struct grub_xfs_data *data,
  350. grub_uint64_t ino)
  351. {
  352. long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
  353. long long ag = GRUB_XFS_INO_AG (data, ino);
  354. long long block;
  355. block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
  356. block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
  357. return block;
  358. }
  359. static inline int
  360. grub_xfs_inode_offset (struct grub_xfs_data *data,
  361. grub_uint64_t ino)
  362. {
  363. int inoag = GRUB_XFS_INO_INOINAG (data, ino);
  364. return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
  365. data->sblock.log2_inode);
  366. }
  367. static inline grub_size_t
  368. grub_xfs_inode_size(struct grub_xfs_data *data)
  369. {
  370. return (grub_size_t)1 << data->sblock.log2_inode;
  371. }
  372. /*
  373. * Returns size occupied by XFS inode stored in memory - we store struct
  374. * grub_fshelp_node there but on disk inode size may be actually larger than
  375. * struct grub_xfs_inode so we need to account for that so that we can read
  376. * from disk directly into in-memory structure.
  377. */
  378. static inline grub_size_t
  379. grub_xfs_fshelp_size(struct grub_xfs_data *data)
  380. {
  381. return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
  382. + grub_xfs_inode_size(data);
  383. }
  384. /* This should return void * but XFS code is error-prone with alignment, so
  385. return char to retain cast-align.
  386. */
  387. static char *
  388. grub_xfs_inode_data(struct grub_xfs_inode *inode)
  389. {
  390. if (inode->version <= 2)
  391. return ((char *)inode) + XFS_V2_INODE_SIZE;
  392. return ((char *)inode) + XFS_V3_INODE_SIZE;
  393. }
  394. static struct grub_xfs_dir_entry *
  395. grub_xfs_inline_de(struct grub_xfs_dir_header *head)
  396. {
  397. /*
  398. With small inode numbers the header is 4 bytes smaller because of
  399. smaller parent pointer
  400. */
  401. return (struct grub_xfs_dir_entry *)
  402. (((char *) head) + sizeof(struct grub_xfs_dir_header) -
  403. (head->largeino ? 0 : sizeof(grub_uint32_t)));
  404. }
  405. static grub_uint8_t *
  406. grub_xfs_inline_de_inopos(struct grub_xfs_data *data,
  407. struct grub_xfs_dir_entry *de)
  408. {
  409. return ((grub_uint8_t *)(de + 1)) + de->len - 1 + (data->hasftype ? 1 : 0);
  410. }
  411. static struct grub_xfs_dir_entry *
  412. grub_xfs_inline_next_de(struct grub_xfs_data *data,
  413. struct grub_xfs_dir_header *head,
  414. struct grub_xfs_dir_entry *de)
  415. {
  416. char *p = (char *)de + sizeof(struct grub_xfs_dir_entry) - 1 + de->len;
  417. p += head->largeino ? sizeof(grub_uint64_t) : sizeof(grub_uint32_t);
  418. if (data->hasftype)
  419. p++;
  420. return (struct grub_xfs_dir_entry *)p;
  421. }
  422. static struct grub_xfs_dirblock_tail *
  423. grub_xfs_dir_tail(struct grub_xfs_data *data, void *dirblock)
  424. {
  425. int dirblksize = 1 << (data->sblock.log2_bsize + data->sblock.log2_dirblk);
  426. return (struct grub_xfs_dirblock_tail *)
  427. ((char *)dirblock + dirblksize - sizeof (struct grub_xfs_dirblock_tail));
  428. }
  429. static struct grub_xfs_dir2_entry *
  430. grub_xfs_first_de(struct grub_xfs_data *data, void *dirblock)
  431. {
  432. if (data->hascrc)
  433. return (struct grub_xfs_dir2_entry *)((char *)dirblock + 64);
  434. return (struct grub_xfs_dir2_entry *)((char *)dirblock + 16);
  435. }
  436. static struct grub_xfs_dir2_entry *
  437. grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
  438. {
  439. int size = sizeof (struct grub_xfs_dir2_entry) + de->len + 2 /* Tag */;
  440. if (data->hasftype)
  441. size++; /* File type */
  442. return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
  443. }
  444. /* This should return void * but XFS code is error-prone with alignment, so
  445. return char to retain cast-align.
  446. */
  447. static char *
  448. grub_xfs_btree_keys(struct grub_xfs_data *data,
  449. struct grub_xfs_btree_node *leaf)
  450. {
  451. char *keys = (char *)(leaf + 1);
  452. if (data->hascrc)
  453. keys += 48; /* skip crc, uuid, ... */
  454. return keys;
  455. }
  456. static grub_err_t
  457. grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
  458. struct grub_xfs_inode *inode)
  459. {
  460. grub_uint64_t block = grub_xfs_inode_block (data, ino);
  461. int offset = grub_xfs_inode_offset (data, ino);
  462. grub_dprintf("xfs", "Reading inode (%" PRIuGRUB_UINT64_T ") - %" PRIuGRUB_UINT64_T ", %d\n",
  463. ino, block, offset);
  464. /* Read the inode. */
  465. if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
  466. inode))
  467. return grub_errno;
  468. if (grub_strncmp ((char *) inode->magic, "IN", 2))
  469. return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
  470. return 0;
  471. }
  472. static grub_uint64_t
  473. get_fsb (const void *keys, int idx)
  474. {
  475. const char *p = (const char *) keys + sizeof(grub_uint64_t) * idx;
  476. return grub_be_to_cpu64 (grub_get_unaligned64 (p));
  477. }
  478. static grub_disk_addr_t
  479. grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  480. {
  481. struct grub_xfs_btree_node *leaf = 0;
  482. int ex, nrec;
  483. struct grub_xfs_extent *exts;
  484. grub_uint64_t ret = 0;
  485. if (node->inode.format == XFS_INODE_FORMAT_BTREE)
  486. {
  487. struct grub_xfs_btree_root *root;
  488. const char *keys;
  489. int recoffset;
  490. leaf = grub_malloc (node->data->bsize);
  491. if (leaf == 0)
  492. return 0;
  493. root = (struct grub_xfs_btree_root *) grub_xfs_inode_data(&node->inode);
  494. nrec = grub_be_to_cpu16 (root->numrecs);
  495. keys = (char *) &root->keys[0];
  496. if (node->inode.fork_offset)
  497. recoffset = (node->inode.fork_offset - 1) / 2;
  498. else
  499. recoffset = (grub_xfs_inode_size(node->data)
  500. - ((char *) keys - (char *) &node->inode))
  501. / (2 * sizeof (grub_uint64_t));
  502. do
  503. {
  504. int i;
  505. for (i = 0; i < nrec; i++)
  506. {
  507. if (fileblock < get_fsb(keys, i))
  508. break;
  509. }
  510. /* Sparse block. */
  511. if (i == 0)
  512. {
  513. grub_free (leaf);
  514. return 0;
  515. }
  516. if (grub_disk_read (node->data->disk,
  517. GRUB_XFS_FSB_TO_BLOCK (node->data, get_fsb (keys, i - 1 + recoffset)) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
  518. 0, node->data->bsize, leaf))
  519. {
  520. grub_free (leaf);
  521. return 0;
  522. }
  523. if ((!node->data->hascrc &&
  524. grub_strncmp ((char *) leaf->magic, "BMAP", 4)) ||
  525. (node->data->hascrc &&
  526. grub_strncmp ((char *) leaf->magic, "BMA3", 4)))
  527. {
  528. grub_free (leaf);
  529. grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
  530. return 0;
  531. }
  532. nrec = grub_be_to_cpu16 (leaf->numrecs);
  533. keys = grub_xfs_btree_keys(node->data, leaf);
  534. recoffset = ((node->data->bsize - ((char *) keys
  535. - (char *) leaf))
  536. / (2 * sizeof (grub_uint64_t)));
  537. }
  538. while (leaf->level);
  539. exts = (struct grub_xfs_extent *) keys;
  540. }
  541. else if (node->inode.format == XFS_INODE_FORMAT_EXT)
  542. {
  543. grub_addr_t exts_end = 0;
  544. grub_addr_t data_end = 0;
  545. nrec = grub_be_to_cpu32 (node->inode.nextents);
  546. exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
  547. if (grub_mul (sizeof (struct grub_xfs_extent), nrec, &exts_end) ||
  548. grub_add ((grub_addr_t) node->data, exts_end, &exts_end) ||
  549. grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) ||
  550. exts_end > data_end)
  551. {
  552. grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS extents");
  553. return 0;
  554. }
  555. }
  556. else
  557. {
  558. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  559. "XFS does not support inode format %d yet",
  560. node->inode.format);
  561. return 0;
  562. }
  563. /* Iterate over each extent to figure out which extent has
  564. the block we are looking for. */
  565. for (ex = 0; ex < nrec; ex++)
  566. {
  567. grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
  568. grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
  569. grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
  570. /* Sparse block. */
  571. if (fileblock < offset)
  572. break;
  573. else if (fileblock < offset + size)
  574. {
  575. ret = (fileblock - offset + start);
  576. break;
  577. }
  578. }
  579. grub_free (leaf);
  580. return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
  581. }
  582. /* Read LEN bytes from the file described by DATA starting with byte
  583. POS. Return the amount of read bytes in READ. */
  584. static grub_ssize_t
  585. grub_xfs_read_file (grub_fshelp_node_t node,
  586. grub_disk_read_hook_t read_hook, void *read_hook_data,
  587. grub_off_t pos, grub_size_t len, char *buf, grub_uint32_t header_size)
  588. {
  589. return grub_fshelp_read_file (node->data->disk, node,
  590. read_hook, read_hook_data,
  591. pos, len, buf, grub_xfs_read_block,
  592. grub_be_to_cpu64 (node->inode.size) + header_size,
  593. node->data->sblock.log2_bsize
  594. - GRUB_DISK_SECTOR_BITS, 0);
  595. }
  596. static char *
  597. grub_xfs_read_symlink (grub_fshelp_node_t node)
  598. {
  599. grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
  600. if (size < 0)
  601. {
  602. grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
  603. return 0;
  604. }
  605. switch (node->inode.format)
  606. {
  607. case XFS_INODE_FORMAT_INO:
  608. return grub_strndup (grub_xfs_inode_data(&node->inode), size);
  609. case XFS_INODE_FORMAT_EXT:
  610. {
  611. char *symlink;
  612. grub_ssize_t numread;
  613. int off = 0;
  614. if (node->data->hascrc)
  615. off = 56;
  616. symlink = grub_malloc (size + 1);
  617. if (!symlink)
  618. return 0;
  619. node->inode.size = grub_be_to_cpu64 (size + off);
  620. numread = grub_xfs_read_file (node, 0, 0, off, size, symlink, off);
  621. if (numread != size)
  622. {
  623. grub_free (symlink);
  624. return 0;
  625. }
  626. symlink[size] = '\0';
  627. return symlink;
  628. }
  629. }
  630. return 0;
  631. }
  632. static enum grub_fshelp_filetype
  633. grub_xfs_mode_to_filetype (grub_uint16_t mode)
  634. {
  635. if ((grub_be_to_cpu16 (mode)
  636. & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
  637. return GRUB_FSHELP_DIR;
  638. else if ((grub_be_to_cpu16 (mode)
  639. & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
  640. return GRUB_FSHELP_SYMLINK;
  641. else if ((grub_be_to_cpu16 (mode)
  642. & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
  643. return GRUB_FSHELP_REG;
  644. return GRUB_FSHELP_UNKNOWN;
  645. }
  646. /* Context for grub_xfs_iterate_dir. */
  647. struct grub_xfs_iterate_dir_ctx
  648. {
  649. grub_fshelp_iterate_dir_hook_t hook;
  650. void *hook_data;
  651. struct grub_fshelp_node *diro;
  652. };
  653. /* Helper for grub_xfs_iterate_dir. */
  654. static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
  655. struct grub_xfs_iterate_dir_ctx *ctx)
  656. {
  657. struct grub_fshelp_node *fdiro;
  658. grub_err_t err;
  659. fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1);
  660. if (!fdiro)
  661. {
  662. grub_print_error ();
  663. return 0;
  664. }
  665. /* The inode should be read, otherwise the filetype can
  666. not be determined. */
  667. fdiro->ino = ino;
  668. fdiro->inode_read = 1;
  669. fdiro->data = ctx->diro->data;
  670. err = grub_xfs_read_inode (ctx->diro->data, ino, &fdiro->inode);
  671. if (err)
  672. {
  673. grub_print_error ();
  674. grub_free (fdiro);
  675. return 0;
  676. }
  677. return ctx->hook (filename, grub_xfs_mode_to_filetype (fdiro->inode.mode),
  678. fdiro, ctx->hook_data);
  679. }
  680. static int
  681. grub_xfs_iterate_dir (grub_fshelp_node_t dir,
  682. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  683. {
  684. struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
  685. struct grub_xfs_iterate_dir_ctx ctx = {
  686. .hook = hook,
  687. .hook_data = hook_data,
  688. .diro = diro
  689. };
  690. switch (diro->inode.format)
  691. {
  692. case XFS_INODE_FORMAT_INO:
  693. {
  694. struct grub_xfs_dir_header *head = (struct grub_xfs_dir_header *) grub_xfs_inode_data(&diro->inode);
  695. struct grub_xfs_dir_entry *de = grub_xfs_inline_de(head);
  696. int smallino = !head->largeino;
  697. int i;
  698. grub_uint64_t parent;
  699. /* If small inode numbers are used to pack the direntry, the
  700. parent inode number is small too. */
  701. if (smallino)
  702. parent = grub_be_to_cpu32 (head->parent.i4);
  703. else
  704. parent = grub_be_to_cpu64 (head->parent.i8);
  705. /* Synthesize the direntries for `.' and `..'. */
  706. if (iterate_dir_call_hook (diro->ino, ".", &ctx))
  707. return 1;
  708. if (iterate_dir_call_hook (parent, "..", &ctx))
  709. return 1;
  710. for (i = 0; i < head->count; i++)
  711. {
  712. grub_uint64_t ino;
  713. grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
  714. grub_uint8_t c;
  715. if ((inopos + (smallino ? 4 : 8)) > (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data))
  716. return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
  717. /* inopos might be unaligned. */
  718. if (smallino)
  719. ino = (((grub_uint32_t) inopos[0]) << 24)
  720. | (((grub_uint32_t) inopos[1]) << 16)
  721. | (((grub_uint32_t) inopos[2]) << 8)
  722. | (((grub_uint32_t) inopos[3]) << 0);
  723. else
  724. ino = (((grub_uint64_t) inopos[0]) << 56)
  725. | (((grub_uint64_t) inopos[1]) << 48)
  726. | (((grub_uint64_t) inopos[2]) << 40)
  727. | (((grub_uint64_t) inopos[3]) << 32)
  728. | (((grub_uint64_t) inopos[4]) << 24)
  729. | (((grub_uint64_t) inopos[5]) << 16)
  730. | (((grub_uint64_t) inopos[6]) << 8)
  731. | (((grub_uint64_t) inopos[7]) << 0);
  732. c = de->name[de->len];
  733. de->name[de->len] = '\0';
  734. if (iterate_dir_call_hook (ino, de->name, &ctx))
  735. {
  736. de->name[de->len] = c;
  737. return 1;
  738. }
  739. de->name[de->len] = c;
  740. de = grub_xfs_inline_next_de(dir->data, head, de);
  741. if ((grub_uint8_t *) de >= (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data))
  742. return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry");
  743. }
  744. break;
  745. }
  746. case XFS_INODE_FORMAT_BTREE:
  747. case XFS_INODE_FORMAT_EXT:
  748. {
  749. grub_ssize_t numread;
  750. char *dirblock;
  751. grub_uint64_t blk;
  752. int dirblk_size, dirblk_log2;
  753. dirblk_log2 = (dir->data->sblock.log2_bsize
  754. + dir->data->sblock.log2_dirblk);
  755. dirblk_size = 1 << dirblk_log2;
  756. dirblock = grub_malloc (dirblk_size);
  757. if (! dirblock)
  758. return 0;
  759. /* Iterate over every block the directory has. */
  760. for (blk = 0;
  761. blk < (grub_be_to_cpu64 (dir->inode.size)
  762. >> dirblk_log2);
  763. blk++)
  764. {
  765. struct grub_xfs_dir2_entry *direntry =
  766. grub_xfs_first_de(dir->data, dirblock);
  767. int entries;
  768. struct grub_xfs_dirblock_tail *tail =
  769. grub_xfs_dir_tail(dir->data, dirblock);
  770. numread = grub_xfs_read_file (dir, 0, 0,
  771. blk << dirblk_log2,
  772. dirblk_size, dirblock, 0);
  773. if (numread != dirblk_size)
  774. {
  775. grub_free (dirblock);
  776. return 0;
  777. }
  778. entries = (grub_be_to_cpu32 (tail->leaf_count)
  779. - grub_be_to_cpu32 (tail->leaf_stale));
  780. if (!entries)
  781. continue;
  782. /* Iterate over all entries within this block. */
  783. while ((char *)direntry < (char *)tail)
  784. {
  785. grub_uint8_t *freetag;
  786. char *filename;
  787. freetag = (grub_uint8_t *) direntry;
  788. if (grub_get_unaligned16 (freetag) == 0XFFFF)
  789. {
  790. grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
  791. /* This entry is not used, go to the next one. */
  792. direntry = (struct grub_xfs_dir2_entry *)
  793. (((char *)direntry) +
  794. grub_be_to_cpu16 (grub_get_unaligned16 (skip)));
  795. continue;
  796. }
  797. filename = (char *)(direntry + 1);
  798. if (filename + direntry->len - 1 > (char *) tail)
  799. return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry");
  800. /* The byte after the filename is for the filetype, padding, or
  801. tag, which is not used by GRUB. So it can be overwritten. */
  802. filename[direntry->len] = '\0';
  803. if (iterate_dir_call_hook (grub_be_to_cpu64(direntry->inode),
  804. filename, &ctx))
  805. {
  806. grub_free (dirblock);
  807. return 1;
  808. }
  809. /* Check if last direntry in this block is
  810. reached. */
  811. entries--;
  812. if (!entries)
  813. break;
  814. /* Select the next directory entry. */
  815. direntry = grub_xfs_next_de(dir->data, direntry);
  816. }
  817. }
  818. grub_free (dirblock);
  819. break;
  820. }
  821. default:
  822. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  823. "XFS does not support inode format %d yet",
  824. diro->inode.format);
  825. }
  826. return 0;
  827. }
  828. static struct grub_xfs_data *
  829. grub_xfs_mount (grub_disk_t disk)
  830. {
  831. struct grub_xfs_data *data = 0;
  832. grub_size_t sz;
  833. data = grub_zalloc (sizeof (struct grub_xfs_data));
  834. if (!data)
  835. return 0;
  836. data->data_size = sizeof (struct grub_xfs_data);
  837. grub_dprintf("xfs", "Reading sb\n");
  838. /* Read the superblock. */
  839. if (grub_disk_read (disk, 0, 0,
  840. sizeof (struct grub_xfs_sblock), &data->sblock))
  841. goto fail;
  842. if (!grub_xfs_sb_valid(data))
  843. goto fail;
  844. if (grub_xfs_sb_needs_repair (data))
  845. grub_dprintf ("xfs", "XFS filesystem needs repair, boot may fail\n");
  846. if (grub_add (grub_xfs_inode_size (data),
  847. sizeof (struct grub_xfs_data) - sizeof (struct grub_xfs_inode) + 1, &sz))
  848. goto fail;
  849. data = grub_realloc (data, sz);
  850. if (! data)
  851. goto fail;
  852. data->data_size = sz;
  853. data->diropen.data = data;
  854. data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino);
  855. data->diropen.inode_read = 1;
  856. data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
  857. data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
  858. data->hasftype = grub_xfs_sb_hasftype(data);
  859. data->hascrc = grub_xfs_sb_hascrc(data);
  860. data->disk = disk;
  861. data->pos = 0;
  862. grub_dprintf("xfs", "Reading root ino %" PRIuGRUB_UINT64_T "\n",
  863. grub_cpu_to_be64(data->sblock.rootino));
  864. grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
  865. return data;
  866. fail:
  867. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  868. grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
  869. grub_free (data);
  870. return 0;
  871. }
  872. /* Context for grub_xfs_dir. */
  873. struct grub_xfs_dir_ctx
  874. {
  875. grub_fs_dir_hook_t hook;
  876. void *hook_data;
  877. };
  878. /* Bigtime inodes helpers. */
  879. #define XFS_BIGTIME_EPOCH_OFFSET (-(grub_int64_t) GRUB_INT32_MIN)
  880. static int grub_xfs_inode_has_bigtime (const struct grub_xfs_inode *inode)
  881. {
  882. return inode->version >= 3 &&
  883. (inode->flags2 & grub_cpu_to_be64_compile_time (XFS_DIFLAG2_BIGTIME));
  884. }
  885. static grub_int64_t
  886. grub_xfs_get_inode_time (struct grub_xfs_inode *inode)
  887. {
  888. struct grub_xfs_time_legacy *lts;
  889. if (grub_xfs_inode_has_bigtime (inode))
  890. return grub_divmod64 (grub_be_to_cpu64 (inode->mtime), NSEC_PER_SEC, NULL) - XFS_BIGTIME_EPOCH_OFFSET;
  891. lts = (struct grub_xfs_time_legacy *) &inode->mtime;
  892. return grub_be_to_cpu32 (lts->sec);
  893. }
  894. /* Helper for grub_xfs_dir. */
  895. static int
  896. grub_xfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  897. grub_fshelp_node_t node, void *data)
  898. {
  899. struct grub_xfs_dir_ctx *ctx = data;
  900. struct grub_dirhook_info info;
  901. grub_memset (&info, 0, sizeof (info));
  902. if (node->inode_read)
  903. {
  904. info.mtimeset = 1;
  905. info.mtime = grub_xfs_get_inode_time (&node->inode);
  906. }
  907. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  908. grub_free (node);
  909. return ctx->hook (filename, &info, ctx->hook_data);
  910. }
  911. static grub_err_t
  912. grub_xfs_dir (grub_device_t device, const char *path,
  913. grub_fs_dir_hook_t hook, void *hook_data)
  914. {
  915. struct grub_xfs_dir_ctx ctx = { hook, hook_data };
  916. struct grub_xfs_data *data = 0;
  917. struct grub_fshelp_node *fdiro = 0;
  918. grub_dl_ref (my_mod);
  919. data = grub_xfs_mount (device->disk);
  920. if (!data)
  921. goto mount_fail;
  922. grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  923. grub_xfs_read_symlink, GRUB_FSHELP_DIR);
  924. if (grub_errno)
  925. goto fail;
  926. grub_xfs_iterate_dir (fdiro, grub_xfs_dir_iter, &ctx);
  927. fail:
  928. if (fdiro != &data->diropen)
  929. grub_free (fdiro);
  930. grub_free (data);
  931. mount_fail:
  932. grub_dl_unref (my_mod);
  933. return grub_errno;
  934. }
  935. /* Open a file named NAME and initialize FILE. */
  936. static grub_err_t
  937. grub_xfs_open (struct grub_file *file, const char *name)
  938. {
  939. struct grub_xfs_data *data;
  940. struct grub_fshelp_node *fdiro = 0;
  941. grub_dl_ref (my_mod);
  942. data = grub_xfs_mount (file->device->disk);
  943. if (!data)
  944. goto mount_fail;
  945. grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  946. grub_xfs_read_symlink, GRUB_FSHELP_REG);
  947. if (grub_errno)
  948. goto fail;
  949. if (!fdiro->inode_read)
  950. {
  951. grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
  952. if (grub_errno)
  953. goto fail;
  954. }
  955. if (fdiro != &data->diropen)
  956. {
  957. grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
  958. grub_free (fdiro);
  959. }
  960. file->size = grub_be_to_cpu64 (data->diropen.inode.size);
  961. file->data = data;
  962. file->offset = 0;
  963. return 0;
  964. fail:
  965. if (fdiro != &data->diropen)
  966. grub_free (fdiro);
  967. grub_free (data);
  968. mount_fail:
  969. grub_dl_unref (my_mod);
  970. return grub_errno;
  971. }
  972. static grub_ssize_t
  973. grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
  974. {
  975. struct grub_xfs_data *data =
  976. (struct grub_xfs_data *) file->data;
  977. return grub_xfs_read_file (&data->diropen,
  978. file->read_hook, file->read_hook_data,
  979. file->offset, len, buf, 0);
  980. }
  981. static grub_err_t
  982. grub_xfs_close (grub_file_t file)
  983. {
  984. grub_free (file->data);
  985. grub_dl_unref (my_mod);
  986. return GRUB_ERR_NONE;
  987. }
  988. static grub_err_t
  989. grub_xfs_label (grub_device_t device, char **label)
  990. {
  991. struct grub_xfs_data *data;
  992. grub_disk_t disk = device->disk;
  993. grub_dl_ref (my_mod);
  994. data = grub_xfs_mount (disk);
  995. if (data)
  996. *label = grub_strndup ((char *) (data->sblock.label), 12);
  997. else
  998. *label = 0;
  999. grub_dl_unref (my_mod);
  1000. grub_free (data);
  1001. return grub_errno;
  1002. }
  1003. static grub_err_t
  1004. grub_xfs_uuid (grub_device_t device, char **uuid)
  1005. {
  1006. struct grub_xfs_data *data;
  1007. grub_disk_t disk = device->disk;
  1008. grub_dl_ref (my_mod);
  1009. data = grub_xfs_mount (disk);
  1010. if (data)
  1011. {
  1012. *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  1013. grub_be_to_cpu16 (data->sblock.uuid[0]),
  1014. grub_be_to_cpu16 (data->sblock.uuid[1]),
  1015. grub_be_to_cpu16 (data->sblock.uuid[2]),
  1016. grub_be_to_cpu16 (data->sblock.uuid[3]),
  1017. grub_be_to_cpu16 (data->sblock.uuid[4]),
  1018. grub_be_to_cpu16 (data->sblock.uuid[5]),
  1019. grub_be_to_cpu16 (data->sblock.uuid[6]),
  1020. grub_be_to_cpu16 (data->sblock.uuid[7]));
  1021. }
  1022. else
  1023. *uuid = NULL;
  1024. grub_dl_unref (my_mod);
  1025. grub_free (data);
  1026. return grub_errno;
  1027. }
  1028. static struct grub_fs grub_xfs_fs =
  1029. {
  1030. .name = "xfs",
  1031. .fs_dir = grub_xfs_dir,
  1032. .fs_open = grub_xfs_open,
  1033. .fs_read = grub_xfs_read,
  1034. .fs_close = grub_xfs_close,
  1035. .fs_label = grub_xfs_label,
  1036. .fs_uuid = grub_xfs_uuid,
  1037. #ifdef GRUB_UTIL
  1038. .reserved_first_sector = 0,
  1039. .blocklist_install = 1,
  1040. #endif
  1041. .next = 0
  1042. };
  1043. GRUB_MOD_INIT(xfs)
  1044. {
  1045. grub_fs_register (&grub_xfs_fs);
  1046. my_mod = mod;
  1047. }
  1048. GRUB_MOD_FINI(xfs)
  1049. {
  1050. grub_fs_unregister (&grub_xfs_fs);
  1051. }