xfs.c 30 KB

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