xfs.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. grub_error (GRUB_ERR_BAD_FS, "unsupported XFS filesystem version");
  301. return 0;
  302. }
  303. static int
  304. grub_xfs_sb_needs_repair (struct grub_xfs_data *data)
  305. {
  306. return ((data->sblock.version &
  307. grub_cpu_to_be16_compile_time (XFS_SB_VERSION_NUMBITS)) ==
  308. grub_cpu_to_be16_compile_time (XFS_SB_VERSION_5) &&
  309. (data->sblock.sb_features_incompat &
  310. grub_cpu_to_be32_compile_time (XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)));
  311. }
  312. /* Filetype information as used in inodes. */
  313. #define FILETYPE_INO_MASK 0170000
  314. #define FILETYPE_INO_REG 0100000
  315. #define FILETYPE_INO_DIRECTORY 0040000
  316. #define FILETYPE_INO_SYMLINK 0120000
  317. static inline int
  318. GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
  319. {
  320. return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
  321. }
  322. static inline grub_uint64_t
  323. GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
  324. grub_uint64_t ino)
  325. {
  326. return (ino & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
  327. }
  328. static inline grub_uint64_t
  329. GRUB_XFS_INO_AG (struct grub_xfs_data *data,
  330. grub_uint64_t ino)
  331. {
  332. return (ino >> GRUB_XFS_INO_AGBITS (data));
  333. }
  334. static inline grub_disk_addr_t
  335. GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
  336. {
  337. return ((fsb >> data->sblock.log2_agblk) * data->agsize
  338. + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
  339. }
  340. static inline grub_uint64_t
  341. GRUB_XFS_EXTENT_OFFSET (struct grub_xfs_extent *exts, int ex)
  342. {
  343. return ((grub_be_to_cpu32 (exts[ex].raw[0]) & ~(1 << 31)) << 23
  344. | grub_be_to_cpu32 (exts[ex].raw[1]) >> 9);
  345. }
  346. static inline grub_uint64_t
  347. GRUB_XFS_EXTENT_BLOCK (struct grub_xfs_extent *exts, int ex)
  348. {
  349. return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex].raw[1])
  350. & (0x1ff)) << 43
  351. | (grub_uint64_t) grub_be_to_cpu32 (exts[ex].raw[2]) << 11
  352. | grub_be_to_cpu32 (exts[ex].raw[3]) >> 21);
  353. }
  354. static inline grub_uint64_t
  355. GRUB_XFS_EXTENT_SIZE (struct grub_xfs_extent *exts, int ex)
  356. {
  357. return (grub_be_to_cpu32 (exts[ex].raw[3]) & ((1 << 21) - 1));
  358. }
  359. static inline grub_uint64_t
  360. grub_xfs_inode_block (struct grub_xfs_data *data,
  361. grub_uint64_t ino)
  362. {
  363. long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
  364. long long ag = GRUB_XFS_INO_AG (data, ino);
  365. long long block;
  366. block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
  367. block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
  368. return block;
  369. }
  370. static inline int
  371. grub_xfs_inode_offset (struct grub_xfs_data *data,
  372. grub_uint64_t ino)
  373. {
  374. int inoag = GRUB_XFS_INO_INOINAG (data, ino);
  375. return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
  376. data->sblock.log2_inode);
  377. }
  378. static inline grub_size_t
  379. grub_xfs_inode_size(struct grub_xfs_data *data)
  380. {
  381. return (grub_size_t)1 << data->sblock.log2_inode;
  382. }
  383. /*
  384. * Returns size occupied by XFS inode stored in memory - we store struct
  385. * grub_fshelp_node there but on disk inode size may be actually larger than
  386. * struct grub_xfs_inode so we need to account for that so that we can read
  387. * from disk directly into in-memory structure.
  388. */
  389. static inline grub_size_t
  390. grub_xfs_fshelp_size(struct grub_xfs_data *data)
  391. {
  392. return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
  393. + grub_xfs_inode_size(data);
  394. }
  395. /* This should return void * but XFS code is error-prone with alignment, so
  396. return char to retain cast-align.
  397. */
  398. static char *
  399. grub_xfs_inode_data(struct grub_xfs_inode *inode)
  400. {
  401. if (inode->version <= 2)
  402. return ((char *)inode) + XFS_V2_INODE_SIZE;
  403. return ((char *)inode) + XFS_V3_INODE_SIZE;
  404. }
  405. static struct grub_xfs_dir_entry *
  406. grub_xfs_inline_de(struct grub_xfs_dir_header *head)
  407. {
  408. /*
  409. With small inode numbers the header is 4 bytes smaller because of
  410. smaller parent pointer
  411. */
  412. return (struct grub_xfs_dir_entry *)
  413. (((char *) head) + sizeof(struct grub_xfs_dir_header) -
  414. (head->largeino ? 0 : sizeof(grub_uint32_t)));
  415. }
  416. static grub_uint8_t *
  417. grub_xfs_inline_de_inopos(struct grub_xfs_data *data,
  418. struct grub_xfs_dir_entry *de)
  419. {
  420. return ((grub_uint8_t *)(de + 1)) + de->len - 1 + (data->hasftype ? 1 : 0);
  421. }
  422. static struct grub_xfs_dir_entry *
  423. grub_xfs_inline_next_de(struct grub_xfs_data *data,
  424. struct grub_xfs_dir_header *head,
  425. struct grub_xfs_dir_entry *de)
  426. {
  427. char *p = (char *)de + sizeof(struct grub_xfs_dir_entry) - 1 + de->len;
  428. p += head->largeino ? sizeof(grub_uint64_t) : sizeof(grub_uint32_t);
  429. if (data->hasftype)
  430. p++;
  431. return (struct grub_xfs_dir_entry *)p;
  432. }
  433. static struct grub_xfs_dirblock_tail *
  434. grub_xfs_dir_tail(struct grub_xfs_data *data, void *dirblock)
  435. {
  436. int dirblksize = 1 << (data->sblock.log2_bsize + data->sblock.log2_dirblk);
  437. return (struct grub_xfs_dirblock_tail *)
  438. ((char *)dirblock + dirblksize - sizeof (struct grub_xfs_dirblock_tail));
  439. }
  440. static struct grub_xfs_dir2_entry *
  441. grub_xfs_first_de(struct grub_xfs_data *data, void *dirblock)
  442. {
  443. if (data->hascrc)
  444. return (struct grub_xfs_dir2_entry *)((char *)dirblock + 64);
  445. return (struct grub_xfs_dir2_entry *)((char *)dirblock + 16);
  446. }
  447. static struct grub_xfs_dir2_entry *
  448. grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
  449. {
  450. int size = sizeof (struct grub_xfs_dir2_entry) + de->len + 2 /* Tag */;
  451. if (data->hasftype)
  452. size++; /* File type */
  453. return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
  454. }
  455. /* This should return void * but XFS code is error-prone with alignment, so
  456. return char to retain cast-align.
  457. */
  458. static char *
  459. grub_xfs_btree_keys(struct grub_xfs_data *data,
  460. struct grub_xfs_btree_node *leaf)
  461. {
  462. char *keys = (char *)(leaf + 1);
  463. if (data->hascrc)
  464. keys += 48; /* skip crc, uuid, ... */
  465. return keys;
  466. }
  467. static grub_err_t
  468. grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
  469. struct grub_xfs_inode *inode)
  470. {
  471. grub_uint64_t block = grub_xfs_inode_block (data, ino);
  472. int offset = grub_xfs_inode_offset (data, ino);
  473. grub_dprintf("xfs", "Reading inode (%" PRIuGRUB_UINT64_T ") - %" PRIuGRUB_UINT64_T ", %d\n",
  474. ino, block, offset);
  475. /* Read the inode. */
  476. if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
  477. inode))
  478. return grub_errno;
  479. if (grub_strncmp ((char *) inode->magic, "IN", 2))
  480. return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
  481. return 0;
  482. }
  483. static grub_uint64_t
  484. get_fsb (const void *keys, int idx)
  485. {
  486. const char *p = (const char *) keys + sizeof(grub_uint64_t) * idx;
  487. return grub_be_to_cpu64 (grub_get_unaligned64 (p));
  488. }
  489. static int
  490. grub_xfs_inode_has_large_extent_counts (const struct grub_xfs_inode *inode)
  491. {
  492. return inode->version >= 3 &&
  493. (inode->flags2 & grub_cpu_to_be64_compile_time (XFS_DIFLAG2_NREXT64));
  494. }
  495. static grub_uint64_t
  496. grub_xfs_get_inode_nextents (struct grub_xfs_inode *inode)
  497. {
  498. return (grub_xfs_inode_has_large_extent_counts (inode)) ?
  499. grub_be_to_cpu64 (inode->nextents_big) :
  500. grub_be_to_cpu32 (inode->nextents);
  501. }
  502. static grub_disk_addr_t
  503. grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  504. {
  505. struct grub_xfs_btree_node *leaf = 0;
  506. grub_uint64_t ex, nrec;
  507. struct grub_xfs_extent *exts;
  508. grub_uint64_t ret = 0;
  509. if (node->inode.format == XFS_INODE_FORMAT_BTREE)
  510. {
  511. struct grub_xfs_btree_root *root;
  512. const char *keys;
  513. int recoffset;
  514. leaf = grub_malloc (node->data->bsize);
  515. if (leaf == 0)
  516. return 0;
  517. root = (struct grub_xfs_btree_root *) grub_xfs_inode_data(&node->inode);
  518. nrec = grub_be_to_cpu16 (root->numrecs);
  519. keys = (char *) &root->keys[0];
  520. if (node->inode.fork_offset)
  521. recoffset = (node->inode.fork_offset - 1) / 2;
  522. else
  523. recoffset = (grub_xfs_inode_size(node->data)
  524. - ((char *) keys - (char *) &node->inode))
  525. / (2 * sizeof (grub_uint64_t));
  526. do
  527. {
  528. grub_uint64_t i;
  529. grub_addr_t keys_end, data_end;
  530. if (grub_mul (sizeof (grub_uint64_t), nrec, &keys_end) ||
  531. grub_add ((grub_addr_t) keys, keys_end, &keys_end) ||
  532. grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) ||
  533. keys_end > data_end)
  534. {
  535. grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS root keys");
  536. grub_free (leaf);
  537. return 0;
  538. }
  539. for (i = 0; i < nrec; i++)
  540. {
  541. if (fileblock < get_fsb(keys, i))
  542. break;
  543. }
  544. /* Sparse block. */
  545. if (i == 0)
  546. {
  547. grub_free (leaf);
  548. return 0;
  549. }
  550. if (grub_disk_read (node->data->disk,
  551. GRUB_XFS_FSB_TO_BLOCK (node->data, get_fsb (keys, i - 1 + recoffset)) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
  552. 0, node->data->bsize, leaf))
  553. {
  554. grub_free (leaf);
  555. return 0;
  556. }
  557. if ((!node->data->hascrc &&
  558. grub_strncmp ((char *) leaf->magic, "BMAP", 4)) ||
  559. (node->data->hascrc &&
  560. grub_strncmp ((char *) leaf->magic, "BMA3", 4)))
  561. {
  562. grub_free (leaf);
  563. grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
  564. return 0;
  565. }
  566. nrec = grub_be_to_cpu16 (leaf->numrecs);
  567. keys = grub_xfs_btree_keys(node->data, leaf);
  568. recoffset = ((node->data->bsize - ((char *) keys
  569. - (char *) leaf))
  570. / (2 * sizeof (grub_uint64_t)));
  571. }
  572. while (leaf->level);
  573. exts = (struct grub_xfs_extent *) keys;
  574. }
  575. else if (node->inode.format == XFS_INODE_FORMAT_EXT)
  576. {
  577. grub_addr_t exts_end = 0;
  578. grub_addr_t data_end = 0;
  579. nrec = grub_xfs_get_inode_nextents (&node->inode);
  580. exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
  581. if (grub_mul (sizeof (struct grub_xfs_extent), nrec, &exts_end) ||
  582. grub_add ((grub_addr_t) node->data, exts_end, &exts_end) ||
  583. grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) ||
  584. exts_end > data_end)
  585. {
  586. grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS extents");
  587. return 0;
  588. }
  589. }
  590. else
  591. {
  592. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  593. "XFS does not support inode format %d yet",
  594. node->inode.format);
  595. return 0;
  596. }
  597. /* Iterate over each extent to figure out which extent has
  598. the block we are looking for. */
  599. for (ex = 0; ex < nrec; ex++)
  600. {
  601. grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
  602. grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
  603. grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
  604. /* Sparse block. */
  605. if (fileblock < offset)
  606. break;
  607. else if (fileblock < offset + size)
  608. {
  609. ret = (fileblock - offset + start);
  610. break;
  611. }
  612. }
  613. grub_free (leaf);
  614. return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
  615. }
  616. /* Read LEN bytes from the file described by DATA starting with byte
  617. POS. Return the amount of read bytes in READ. */
  618. static grub_ssize_t
  619. grub_xfs_read_file (grub_fshelp_node_t node,
  620. grub_disk_read_hook_t read_hook, void *read_hook_data,
  621. grub_off_t pos, grub_size_t len, char *buf, grub_uint32_t header_size)
  622. {
  623. return grub_fshelp_read_file (node->data->disk, node,
  624. read_hook, read_hook_data,
  625. pos, len, buf, grub_xfs_read_block,
  626. grub_be_to_cpu64 (node->inode.size) + header_size,
  627. node->data->sblock.log2_bsize
  628. - GRUB_DISK_SECTOR_BITS, 0);
  629. }
  630. static char *
  631. grub_xfs_read_symlink (grub_fshelp_node_t node)
  632. {
  633. grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
  634. grub_size_t sz;
  635. if (size < 0)
  636. {
  637. grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
  638. return 0;
  639. }
  640. switch (node->inode.format)
  641. {
  642. case XFS_INODE_FORMAT_INO:
  643. return grub_strndup (grub_xfs_inode_data(&node->inode), size);
  644. case XFS_INODE_FORMAT_EXT:
  645. {
  646. char *symlink;
  647. grub_ssize_t numread;
  648. int off = 0;
  649. if (node->data->hascrc)
  650. off = 56;
  651. if (grub_add (size, 1, &sz))
  652. {
  653. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink size overflow"));
  654. return 0;
  655. }
  656. symlink = grub_malloc (sz);
  657. if (!symlink)
  658. return 0;
  659. node->inode.size = grub_be_to_cpu64 (size + off);
  660. numread = grub_xfs_read_file (node, 0, 0, off, size, symlink, off);
  661. if (numread != size)
  662. {
  663. grub_free (symlink);
  664. return 0;
  665. }
  666. symlink[size] = '\0';
  667. return symlink;
  668. }
  669. }
  670. return 0;
  671. }
  672. static enum grub_fshelp_filetype
  673. grub_xfs_mode_to_filetype (grub_uint16_t mode)
  674. {
  675. if ((grub_be_to_cpu16 (mode)
  676. & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
  677. return GRUB_FSHELP_DIR;
  678. else if ((grub_be_to_cpu16 (mode)
  679. & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
  680. return GRUB_FSHELP_SYMLINK;
  681. else if ((grub_be_to_cpu16 (mode)
  682. & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
  683. return GRUB_FSHELP_REG;
  684. return GRUB_FSHELP_UNKNOWN;
  685. }
  686. /* Context for grub_xfs_iterate_dir. */
  687. struct grub_xfs_iterate_dir_ctx
  688. {
  689. grub_fshelp_iterate_dir_hook_t hook;
  690. void *hook_data;
  691. struct grub_fshelp_node *diro;
  692. };
  693. /* Helper for grub_xfs_iterate_dir. */
  694. static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
  695. struct grub_xfs_iterate_dir_ctx *ctx)
  696. {
  697. struct grub_fshelp_node *fdiro;
  698. grub_err_t err;
  699. grub_size_t sz;
  700. if (grub_add (grub_xfs_fshelp_size(ctx->diro->data), 1, &sz))
  701. {
  702. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory data size overflow"));
  703. grub_print_error ();
  704. return 0;
  705. }
  706. fdiro = grub_malloc (sz);
  707. if (!fdiro)
  708. {
  709. grub_print_error ();
  710. return 0;
  711. }
  712. /* The inode should be read, otherwise the filetype can
  713. not be determined. */
  714. fdiro->ino = ino;
  715. fdiro->inode_read = 1;
  716. fdiro->data = ctx->diro->data;
  717. err = grub_xfs_read_inode (ctx->diro->data, ino, &fdiro->inode);
  718. if (err)
  719. {
  720. grub_print_error ();
  721. grub_free (fdiro);
  722. return 0;
  723. }
  724. return ctx->hook (filename, grub_xfs_mode_to_filetype (fdiro->inode.mode),
  725. fdiro, ctx->hook_data);
  726. }
  727. static int
  728. grub_xfs_iterate_dir (grub_fshelp_node_t dir,
  729. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  730. {
  731. struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
  732. struct grub_xfs_iterate_dir_ctx ctx = {
  733. .hook = hook,
  734. .hook_data = hook_data,
  735. .diro = diro
  736. };
  737. switch (diro->inode.format)
  738. {
  739. case XFS_INODE_FORMAT_INO:
  740. {
  741. struct grub_xfs_dir_header *head = (struct grub_xfs_dir_header *) grub_xfs_inode_data(&diro->inode);
  742. struct grub_xfs_dir_entry *de = grub_xfs_inline_de(head);
  743. int smallino = !head->largeino;
  744. int i;
  745. grub_uint64_t parent;
  746. /* If small inode numbers are used to pack the direntry, the
  747. parent inode number is small too. */
  748. if (smallino)
  749. parent = grub_be_to_cpu32 (head->parent.i4);
  750. else
  751. parent = grub_be_to_cpu64 (head->parent.i8);
  752. /* Synthesize the direntries for `.' and `..'. */
  753. if (iterate_dir_call_hook (diro->ino, ".", &ctx))
  754. return 1;
  755. if (iterate_dir_call_hook (parent, "..", &ctx))
  756. return 1;
  757. for (i = 0; i < head->count &&
  758. (grub_uint8_t *) de < ((grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data)); i++)
  759. {
  760. grub_uint64_t ino;
  761. grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
  762. grub_uint8_t c;
  763. if ((inopos + (smallino ? 4 : 8)) > (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data))
  764. return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
  765. /* inopos might be unaligned. */
  766. if (smallino)
  767. ino = (((grub_uint32_t) inopos[0]) << 24)
  768. | (((grub_uint32_t) inopos[1]) << 16)
  769. | (((grub_uint32_t) inopos[2]) << 8)
  770. | (((grub_uint32_t) inopos[3]) << 0);
  771. else
  772. ino = (((grub_uint64_t) inopos[0]) << 56)
  773. | (((grub_uint64_t) inopos[1]) << 48)
  774. | (((grub_uint64_t) inopos[2]) << 40)
  775. | (((grub_uint64_t) inopos[3]) << 32)
  776. | (((grub_uint64_t) inopos[4]) << 24)
  777. | (((grub_uint64_t) inopos[5]) << 16)
  778. | (((grub_uint64_t) inopos[6]) << 8)
  779. | (((grub_uint64_t) inopos[7]) << 0);
  780. c = de->name[de->len];
  781. de->name[de->len] = '\0';
  782. if (iterate_dir_call_hook (ino, de->name, &ctx))
  783. {
  784. de->name[de->len] = c;
  785. return 1;
  786. }
  787. de->name[de->len] = c;
  788. de = grub_xfs_inline_next_de(dir->data, head, de);
  789. }
  790. break;
  791. }
  792. case XFS_INODE_FORMAT_BTREE:
  793. case XFS_INODE_FORMAT_EXT:
  794. {
  795. grub_ssize_t numread;
  796. char *dirblock;
  797. grub_uint64_t blk;
  798. int dirblk_size, dirblk_log2;
  799. dirblk_log2 = (dir->data->sblock.log2_bsize
  800. + dir->data->sblock.log2_dirblk);
  801. dirblk_size = 1 << dirblk_log2;
  802. dirblock = grub_malloc (dirblk_size);
  803. if (! dirblock)
  804. return 0;
  805. /* Iterate over every block the directory has. */
  806. for (blk = 0;
  807. blk < (grub_be_to_cpu64 (dir->inode.size)
  808. >> dirblk_log2);
  809. blk++)
  810. {
  811. struct grub_xfs_dir2_entry *direntry =
  812. grub_xfs_first_de(dir->data, dirblock);
  813. int entries = -1;
  814. char *end = dirblock + dirblk_size;
  815. grub_uint32_t magic;
  816. numread = grub_xfs_read_file (dir, 0, 0,
  817. blk << dirblk_log2,
  818. dirblk_size, dirblock, 0);
  819. if (numread != dirblk_size)
  820. {
  821. grub_free (dirblock);
  822. return 0;
  823. }
  824. /*
  825. * If this data block isn't actually part of the extent list then
  826. * grub_xfs_read_file() returns a block of zeros. So, if the magic
  827. * number field is all zeros then this block should be skipped.
  828. */
  829. magic = *(grub_uint32_t *)(void *) dirblock;
  830. if (!magic)
  831. continue;
  832. /*
  833. * Leaf and tail information are only in the data block if the number
  834. * of extents is 1.
  835. */
  836. if (dir->inode.nextents == grub_cpu_to_be32_compile_time (1))
  837. {
  838. struct grub_xfs_dirblock_tail *tail = grub_xfs_dir_tail (dir->data, dirblock);
  839. end = (char *) tail;
  840. /* Subtract the space used by leaf nodes. */
  841. end -= grub_be_to_cpu32 (tail->leaf_count) * sizeof (struct grub_xfs_dir_leaf_entry);
  842. entries = grub_be_to_cpu32 (tail->leaf_count) - grub_be_to_cpu32 (tail->leaf_stale);
  843. if (!entries)
  844. continue;
  845. }
  846. /* Iterate over all entries within this block. */
  847. while ((char *) direntry < (char *) end)
  848. {
  849. grub_uint8_t *freetag;
  850. char *filename;
  851. freetag = (grub_uint8_t *) direntry;
  852. if (grub_get_unaligned16 (freetag) == 0XFFFF)
  853. {
  854. grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
  855. /* This entry is not used, go to the next one. */
  856. direntry = (struct grub_xfs_dir2_entry *)
  857. (((char *)direntry) +
  858. grub_be_to_cpu16 (grub_get_unaligned16 (skip)));
  859. continue;
  860. }
  861. filename = (char *)(direntry + 1);
  862. if (filename + direntry->len + 1 > (char *) end)
  863. return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry");
  864. /* The byte after the filename is for the filetype, padding, or
  865. tag, which is not used by GRUB. So it can be overwritten. */
  866. filename[direntry->len] = '\0';
  867. if (iterate_dir_call_hook (grub_be_to_cpu64(direntry->inode),
  868. filename, &ctx))
  869. {
  870. grub_free (dirblock);
  871. return 1;
  872. }
  873. /*
  874. * The expected number of directory entries is only tracked for the
  875. * single extent case.
  876. */
  877. if (dir->inode.nextents == grub_cpu_to_be32_compile_time (1))
  878. {
  879. /* Check if last direntry in this block is reached. */
  880. entries--;
  881. if (!entries)
  882. break;
  883. }
  884. /* Select the next directory entry. */
  885. direntry = grub_xfs_next_de(dir->data, direntry);
  886. }
  887. }
  888. grub_free (dirblock);
  889. break;
  890. }
  891. default:
  892. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  893. "XFS does not support inode format %d yet",
  894. diro->inode.format);
  895. }
  896. return 0;
  897. }
  898. static struct grub_xfs_data *
  899. grub_xfs_mount (grub_disk_t disk)
  900. {
  901. struct grub_xfs_data *data = 0;
  902. grub_size_t sz;
  903. data = grub_zalloc (sizeof (struct grub_xfs_data));
  904. if (!data)
  905. return 0;
  906. data->data_size = sizeof (struct grub_xfs_data);
  907. grub_dprintf("xfs", "Reading sb\n");
  908. /* Read the superblock. */
  909. if (grub_disk_read (disk, 0, 0,
  910. sizeof (struct grub_xfs_sblock), &data->sblock))
  911. goto fail;
  912. if (!grub_xfs_sb_valid(data))
  913. goto fail;
  914. if (grub_xfs_sb_needs_repair (data))
  915. grub_dprintf ("xfs", "XFS filesystem needs repair, boot may fail\n");
  916. if (grub_add (grub_xfs_inode_size (data),
  917. sizeof (struct grub_xfs_data) - sizeof (struct grub_xfs_inode) + 1, &sz))
  918. goto fail;
  919. data = grub_realloc (data, sz);
  920. if (! data)
  921. goto fail;
  922. data->data_size = sz;
  923. data->diropen.data = data;
  924. data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino);
  925. data->diropen.inode_read = 1;
  926. data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
  927. data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
  928. data->hasftype = grub_xfs_sb_hasftype(data);
  929. data->hascrc = grub_xfs_sb_hascrc(data);
  930. data->disk = disk;
  931. data->pos = 0;
  932. grub_dprintf("xfs", "Reading root ino %" PRIuGRUB_UINT64_T "\n",
  933. grub_cpu_to_be64(data->sblock.rootino));
  934. grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
  935. return data;
  936. fail:
  937. if (grub_errno == GRUB_ERR_OUT_OF_RANGE || grub_errno == GRUB_ERR_NONE)
  938. grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
  939. grub_free (data);
  940. return 0;
  941. }
  942. /* Context for grub_xfs_dir. */
  943. struct grub_xfs_dir_ctx
  944. {
  945. grub_fs_dir_hook_t hook;
  946. void *hook_data;
  947. };
  948. /* Bigtime inodes helpers. */
  949. #define XFS_BIGTIME_EPOCH_OFFSET (-(grub_int64_t) GRUB_INT32_MIN)
  950. static int grub_xfs_inode_has_bigtime (const struct grub_xfs_inode *inode)
  951. {
  952. return inode->version >= 3 &&
  953. (inode->flags2 & grub_cpu_to_be64_compile_time (XFS_DIFLAG2_BIGTIME));
  954. }
  955. static grub_int64_t
  956. grub_xfs_get_inode_time (struct grub_xfs_inode *inode)
  957. {
  958. struct grub_xfs_time_legacy *lts;
  959. if (grub_xfs_inode_has_bigtime (inode))
  960. return grub_divmod64 (grub_be_to_cpu64 (inode->mtime), NSEC_PER_SEC, NULL) - XFS_BIGTIME_EPOCH_OFFSET;
  961. lts = (struct grub_xfs_time_legacy *) &inode->mtime;
  962. return grub_be_to_cpu32 (lts->sec);
  963. }
  964. /* Helper for grub_xfs_dir. */
  965. static int
  966. grub_xfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  967. grub_fshelp_node_t node, void *data)
  968. {
  969. struct grub_xfs_dir_ctx *ctx = data;
  970. struct grub_dirhook_info info;
  971. grub_memset (&info, 0, sizeof (info));
  972. if (node->inode_read)
  973. {
  974. info.mtimeset = 1;
  975. info.mtime = grub_xfs_get_inode_time (&node->inode);
  976. }
  977. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  978. grub_free (node);
  979. return ctx->hook (filename, &info, ctx->hook_data);
  980. }
  981. static grub_err_t
  982. grub_xfs_dir (grub_device_t device, const char *path,
  983. grub_fs_dir_hook_t hook, void *hook_data)
  984. {
  985. struct grub_xfs_dir_ctx ctx = { hook, hook_data };
  986. struct grub_xfs_data *data = 0;
  987. struct grub_fshelp_node *fdiro = 0;
  988. grub_dl_ref (my_mod);
  989. data = grub_xfs_mount (device->disk);
  990. if (!data)
  991. goto mount_fail;
  992. grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  993. grub_xfs_read_symlink, GRUB_FSHELP_DIR);
  994. if (grub_errno)
  995. goto fail;
  996. grub_xfs_iterate_dir (fdiro, grub_xfs_dir_iter, &ctx);
  997. fail:
  998. if (fdiro != &data->diropen)
  999. grub_free (fdiro);
  1000. grub_free (data);
  1001. mount_fail:
  1002. grub_dl_unref (my_mod);
  1003. return grub_errno;
  1004. }
  1005. /* Open a file named NAME and initialize FILE. */
  1006. static grub_err_t
  1007. grub_xfs_open (struct grub_file *file, const char *name)
  1008. {
  1009. struct grub_xfs_data *data;
  1010. struct grub_fshelp_node *fdiro = 0;
  1011. grub_dl_ref (my_mod);
  1012. data = grub_xfs_mount (file->device->disk);
  1013. if (!data)
  1014. goto mount_fail;
  1015. grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
  1016. grub_xfs_read_symlink, GRUB_FSHELP_REG);
  1017. if (grub_errno)
  1018. goto fail;
  1019. if (!fdiro->inode_read)
  1020. {
  1021. grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
  1022. if (grub_errno)
  1023. goto fail;
  1024. }
  1025. if (fdiro != &data->diropen)
  1026. {
  1027. grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
  1028. grub_free (fdiro);
  1029. }
  1030. file->size = grub_be_to_cpu64 (data->diropen.inode.size);
  1031. file->data = data;
  1032. file->offset = 0;
  1033. return 0;
  1034. fail:
  1035. if (fdiro != &data->diropen)
  1036. grub_free (fdiro);
  1037. grub_free (data);
  1038. mount_fail:
  1039. grub_dl_unref (my_mod);
  1040. return grub_errno;
  1041. }
  1042. static grub_ssize_t
  1043. grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
  1044. {
  1045. struct grub_xfs_data *data =
  1046. (struct grub_xfs_data *) file->data;
  1047. return grub_xfs_read_file (&data->diropen,
  1048. file->read_hook, file->read_hook_data,
  1049. file->offset, len, buf, 0);
  1050. }
  1051. static grub_err_t
  1052. grub_xfs_close (grub_file_t file)
  1053. {
  1054. grub_free (file->data);
  1055. grub_dl_unref (my_mod);
  1056. return GRUB_ERR_NONE;
  1057. }
  1058. static grub_err_t
  1059. grub_xfs_label (grub_device_t device, char **label)
  1060. {
  1061. struct grub_xfs_data *data;
  1062. grub_disk_t disk = device->disk;
  1063. grub_dl_ref (my_mod);
  1064. data = grub_xfs_mount (disk);
  1065. if (data)
  1066. *label = grub_strndup ((char *) (data->sblock.label), 12);
  1067. else
  1068. *label = 0;
  1069. grub_dl_unref (my_mod);
  1070. grub_free (data);
  1071. return grub_errno;
  1072. }
  1073. static grub_err_t
  1074. grub_xfs_uuid (grub_device_t device, char **uuid)
  1075. {
  1076. struct grub_xfs_data *data;
  1077. grub_disk_t disk = device->disk;
  1078. grub_dl_ref (my_mod);
  1079. data = grub_xfs_mount (disk);
  1080. if (data)
  1081. {
  1082. *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  1083. grub_be_to_cpu16 (data->sblock.uuid[0]),
  1084. grub_be_to_cpu16 (data->sblock.uuid[1]),
  1085. grub_be_to_cpu16 (data->sblock.uuid[2]),
  1086. grub_be_to_cpu16 (data->sblock.uuid[3]),
  1087. grub_be_to_cpu16 (data->sblock.uuid[4]),
  1088. grub_be_to_cpu16 (data->sblock.uuid[5]),
  1089. grub_be_to_cpu16 (data->sblock.uuid[6]),
  1090. grub_be_to_cpu16 (data->sblock.uuid[7]));
  1091. }
  1092. else
  1093. *uuid = NULL;
  1094. grub_dl_unref (my_mod);
  1095. grub_free (data);
  1096. return grub_errno;
  1097. }
  1098. static struct grub_fs grub_xfs_fs =
  1099. {
  1100. .name = "xfs",
  1101. .fs_dir = grub_xfs_dir,
  1102. .fs_open = grub_xfs_open,
  1103. .fs_read = grub_xfs_read,
  1104. .fs_close = grub_xfs_close,
  1105. .fs_label = grub_xfs_label,
  1106. .fs_uuid = grub_xfs_uuid,
  1107. #ifdef GRUB_UTIL
  1108. .reserved_first_sector = 0,
  1109. .blocklist_install = 1,
  1110. #endif
  1111. .next = 0
  1112. };
  1113. GRUB_MOD_INIT(xfs)
  1114. {
  1115. grub_xfs_fs.mod = mod;
  1116. grub_fs_register (&grub_xfs_fs);
  1117. my_mod = mod;
  1118. }
  1119. GRUB_MOD_FINI(xfs)
  1120. {
  1121. grub_fs_unregister (&grub_xfs_fs);
  1122. }