xfs.c 35 KB

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