btrfs.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. /* btrfs.c - B-tree file system. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010,2011,2012,2013 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. /*
  20. * Tell zstd to expose functions that aren't part of the stable API, which
  21. * aren't safe to use when linking against a dynamic library. We vendor in a
  22. * specific zstd version, so we know what we're getting. We need these unstable
  23. * functions to provide our own allocator, which uses grub_malloc(), to zstd.
  24. */
  25. #define ZSTD_STATIC_LINKING_ONLY
  26. #include <grub/err.h>
  27. #include <grub/file.h>
  28. #include <grub/mm.h>
  29. #include <grub/misc.h>
  30. #include <grub/disk.h>
  31. #include <grub/dl.h>
  32. #include <grub/types.h>
  33. #include <grub/lib/crc.h>
  34. #include <grub/deflate.h>
  35. #include <minilzo.h>
  36. #include <zstd.h>
  37. #include <grub/i18n.h>
  38. #include <grub/btrfs.h>
  39. #include <grub/crypto.h>
  40. #include <grub/diskfilter.h>
  41. #include <grub/safemath.h>
  42. GRUB_MOD_LICENSE ("GPLv3+");
  43. #define GRUB_BTRFS_SIGNATURE "_BHRfS_M"
  44. /* From http://www.oberhumer.com/opensource/lzo/lzofaq.php
  45. * LZO will expand incompressible data by a little amount. I still haven't
  46. * computed the exact values, but I suggest using these formulas for
  47. * a worst-case expansion calculation:
  48. *
  49. * output_block_size = input_block_size + (input_block_size / 16) + 64 + 3
  50. * */
  51. #define GRUB_BTRFS_LZO_BLOCK_SIZE 4096
  52. #define GRUB_BTRFS_LZO_BLOCK_MAX_CSIZE (GRUB_BTRFS_LZO_BLOCK_SIZE + \
  53. (GRUB_BTRFS_LZO_BLOCK_SIZE / 16) + 64 + 3)
  54. #define ZSTD_BTRFS_MAX_WINDOWLOG 17
  55. #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
  56. typedef grub_uint8_t grub_btrfs_checksum_t[0x20];
  57. typedef grub_uint16_t grub_btrfs_uuid_t[8];
  58. struct grub_btrfs_device
  59. {
  60. grub_uint64_t device_id;
  61. grub_uint64_t size;
  62. grub_uint8_t dummy[0x62 - 0x10];
  63. } GRUB_PACKED;
  64. struct grub_btrfs_superblock
  65. {
  66. grub_btrfs_checksum_t checksum;
  67. grub_btrfs_uuid_t uuid;
  68. grub_uint8_t dummy[0x10];
  69. grub_uint8_t signature[sizeof (GRUB_BTRFS_SIGNATURE) - 1];
  70. grub_uint64_t generation;
  71. grub_uint64_t root_tree;
  72. grub_uint64_t chunk_tree;
  73. grub_uint8_t dummy2[0x20];
  74. grub_uint64_t root_dir_objectid;
  75. grub_uint8_t dummy3[0x41];
  76. struct grub_btrfs_device this_device;
  77. char label[0x100];
  78. grub_uint8_t dummy4[0x100];
  79. grub_uint8_t bootstrap_mapping[0x800];
  80. } GRUB_PACKED;
  81. struct btrfs_header
  82. {
  83. grub_btrfs_checksum_t checksum;
  84. grub_btrfs_uuid_t uuid;
  85. grub_uint64_t bytenr;
  86. grub_uint8_t dummy[0x28];
  87. grub_uint32_t nitems;
  88. grub_uint8_t level;
  89. } GRUB_PACKED;
  90. struct grub_btrfs_device_desc
  91. {
  92. grub_device_t dev;
  93. grub_uint64_t id;
  94. };
  95. struct grub_btrfs_data
  96. {
  97. struct grub_btrfs_superblock sblock;
  98. grub_uint64_t tree;
  99. grub_uint64_t inode;
  100. struct grub_btrfs_device_desc *devices_attached;
  101. unsigned n_devices_attached;
  102. unsigned n_devices_allocated;
  103. /* Cached extent data. */
  104. grub_uint64_t extstart;
  105. grub_uint64_t extend;
  106. grub_uint64_t extino;
  107. grub_uint64_t exttree;
  108. grub_size_t extsize;
  109. struct grub_btrfs_extent_data *extent;
  110. };
  111. struct grub_btrfs_chunk_item
  112. {
  113. grub_uint64_t size;
  114. grub_uint64_t dummy;
  115. grub_uint64_t stripe_length;
  116. grub_uint64_t type;
  117. #define GRUB_BTRFS_CHUNK_TYPE_BITS_DONTCARE 0x07
  118. #define GRUB_BTRFS_CHUNK_TYPE_SINGLE 0x00
  119. #define GRUB_BTRFS_CHUNK_TYPE_RAID0 0x08
  120. #define GRUB_BTRFS_CHUNK_TYPE_RAID1 0x10
  121. #define GRUB_BTRFS_CHUNK_TYPE_DUPLICATED 0x20
  122. #define GRUB_BTRFS_CHUNK_TYPE_RAID10 0x40
  123. #define GRUB_BTRFS_CHUNK_TYPE_RAID5 0x80
  124. #define GRUB_BTRFS_CHUNK_TYPE_RAID6 0x100
  125. #define GRUB_BTRFS_CHUNK_TYPE_RAID1C3 0x200
  126. #define GRUB_BTRFS_CHUNK_TYPE_RAID1C4 0x400
  127. grub_uint8_t dummy2[0xc];
  128. grub_uint16_t nstripes;
  129. grub_uint16_t nsubstripes;
  130. } GRUB_PACKED;
  131. struct grub_btrfs_chunk_stripe
  132. {
  133. grub_uint64_t device_id;
  134. grub_uint64_t offset;
  135. grub_btrfs_uuid_t device_uuid;
  136. } GRUB_PACKED;
  137. struct grub_btrfs_leaf_node
  138. {
  139. struct grub_btrfs_key key;
  140. grub_uint32_t offset;
  141. grub_uint32_t size;
  142. } GRUB_PACKED;
  143. struct grub_btrfs_internal_node
  144. {
  145. struct grub_btrfs_key key;
  146. grub_uint64_t addr;
  147. grub_uint64_t dummy;
  148. } GRUB_PACKED;
  149. struct grub_btrfs_dir_item
  150. {
  151. struct grub_btrfs_key key;
  152. grub_uint8_t dummy[8];
  153. grub_uint16_t m;
  154. grub_uint16_t n;
  155. #define GRUB_BTRFS_DIR_ITEM_TYPE_REGULAR 1
  156. #define GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY 2
  157. #define GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK 7
  158. grub_uint8_t type;
  159. char name[0];
  160. } GRUB_PACKED;
  161. struct grub_btrfs_leaf_descriptor
  162. {
  163. unsigned depth;
  164. unsigned allocated;
  165. struct
  166. {
  167. grub_disk_addr_t addr;
  168. unsigned iter;
  169. unsigned maxiter;
  170. int leaf;
  171. } *data;
  172. };
  173. struct grub_btrfs_time
  174. {
  175. grub_int64_t sec;
  176. grub_uint32_t nanosec;
  177. } GRUB_PACKED;
  178. struct grub_btrfs_inode
  179. {
  180. grub_uint8_t dummy1[0x10];
  181. grub_uint64_t size;
  182. grub_uint8_t dummy2[0x70];
  183. struct grub_btrfs_time mtime;
  184. } GRUB_PACKED;
  185. struct grub_btrfs_extent_data
  186. {
  187. grub_uint64_t dummy;
  188. grub_uint64_t size;
  189. grub_uint8_t compression;
  190. grub_uint8_t encryption;
  191. grub_uint16_t encoding;
  192. grub_uint8_t type;
  193. union
  194. {
  195. char inl[0];
  196. struct
  197. {
  198. grub_uint64_t laddr;
  199. grub_uint64_t compressed_size;
  200. grub_uint64_t offset;
  201. grub_uint64_t filled;
  202. };
  203. };
  204. } GRUB_PACKED;
  205. #define GRUB_BTRFS_EXTENT_INLINE 0
  206. #define GRUB_BTRFS_EXTENT_REGULAR 1
  207. #define GRUB_BTRFS_COMPRESSION_NONE 0
  208. #define GRUB_BTRFS_COMPRESSION_ZLIB 1
  209. #define GRUB_BTRFS_COMPRESSION_LZO 2
  210. #define GRUB_BTRFS_COMPRESSION_ZSTD 3
  211. #define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
  212. static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
  213. 256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
  214. };
  215. static grub_err_t
  216. grub_btrfs_read_logical (struct grub_btrfs_data *data,
  217. grub_disk_addr_t addr, void *buf, grub_size_t size,
  218. int recursion_depth);
  219. static grub_err_t
  220. read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)
  221. {
  222. struct grub_btrfs_superblock sblock;
  223. unsigned i;
  224. grub_err_t err = GRUB_ERR_NONE;
  225. for (i = 0; i < ARRAY_SIZE (superblock_sectors); i++)
  226. {
  227. /* Don't try additional superblocks beyond device size. */
  228. if (i && (grub_le_to_cpu64 (sblock.this_device.size)
  229. >> GRUB_DISK_SECTOR_BITS) <= superblock_sectors[i])
  230. break;
  231. err = grub_disk_read (disk, superblock_sectors[i], 0,
  232. sizeof (sblock), &sblock);
  233. if (err == GRUB_ERR_OUT_OF_RANGE)
  234. break;
  235. if (grub_memcmp ((char *) sblock.signature, GRUB_BTRFS_SIGNATURE,
  236. sizeof (GRUB_BTRFS_SIGNATURE) - 1) != 0)
  237. break;
  238. if (i == 0 || grub_le_to_cpu64 (sblock.generation)
  239. > grub_le_to_cpu64 (sb->generation))
  240. grub_memcpy (sb, &sblock, sizeof (sblock));
  241. }
  242. if ((err == GRUB_ERR_OUT_OF_RANGE || !err) && i == 0)
  243. return grub_error (GRUB_ERR_BAD_FS, "not a Btrfs filesystem");
  244. if (err == GRUB_ERR_OUT_OF_RANGE)
  245. grub_errno = err = GRUB_ERR_NONE;
  246. return err;
  247. }
  248. static int
  249. key_cmp (const struct grub_btrfs_key *a, const struct grub_btrfs_key *b)
  250. {
  251. if (grub_le_to_cpu64 (a->object_id) < grub_le_to_cpu64 (b->object_id))
  252. return -1;
  253. if (grub_le_to_cpu64 (a->object_id) > grub_le_to_cpu64 (b->object_id))
  254. return +1;
  255. if (a->type < b->type)
  256. return -1;
  257. if (a->type > b->type)
  258. return +1;
  259. if (grub_le_to_cpu64 (a->offset) < grub_le_to_cpu64 (b->offset))
  260. return -1;
  261. if (grub_le_to_cpu64 (a->offset) > grub_le_to_cpu64 (b->offset))
  262. return +1;
  263. return 0;
  264. }
  265. static void
  266. free_iterator (struct grub_btrfs_leaf_descriptor *desc)
  267. {
  268. grub_free (desc->data);
  269. }
  270. static grub_err_t
  271. check_btrfs_header (struct grub_btrfs_data *data, struct btrfs_header *header,
  272. grub_disk_addr_t addr)
  273. {
  274. if (grub_le_to_cpu64 (header->bytenr) != addr)
  275. {
  276. grub_dprintf ("btrfs", "btrfs_header.bytenr is not equal node addr\n");
  277. return grub_error (GRUB_ERR_BAD_FS,
  278. "header bytenr is not equal node addr");
  279. }
  280. if (grub_memcmp (data->sblock.uuid, header->uuid, sizeof(grub_btrfs_uuid_t)))
  281. {
  282. grub_dprintf ("btrfs", "btrfs_header.uuid doesn't match sblock uuid\n");
  283. return grub_error (GRUB_ERR_BAD_FS,
  284. "header uuid doesn't match sblock uuid");
  285. }
  286. return GRUB_ERR_NONE;
  287. }
  288. static grub_err_t
  289. save_ref (struct grub_btrfs_leaf_descriptor *desc,
  290. grub_disk_addr_t addr, unsigned i, unsigned m, int l)
  291. {
  292. desc->depth++;
  293. if (desc->allocated < desc->depth)
  294. {
  295. void *newdata;
  296. grub_size_t sz;
  297. if (grub_mul (desc->allocated, 2, &desc->allocated) ||
  298. grub_mul (desc->allocated, sizeof (desc->data[0]), &sz))
  299. return GRUB_ERR_OUT_OF_RANGE;
  300. newdata = grub_realloc (desc->data, sz);
  301. if (!newdata)
  302. return grub_errno;
  303. desc->data = newdata;
  304. }
  305. desc->data[desc->depth - 1].addr = addr;
  306. desc->data[desc->depth - 1].iter = i;
  307. desc->data[desc->depth - 1].maxiter = m;
  308. desc->data[desc->depth - 1].leaf = l;
  309. return GRUB_ERR_NONE;
  310. }
  311. static int
  312. next (struct grub_btrfs_data *data,
  313. struct grub_btrfs_leaf_descriptor *desc,
  314. grub_disk_addr_t * outaddr, grub_size_t * outsize,
  315. struct grub_btrfs_key *key_out)
  316. {
  317. grub_err_t err;
  318. struct grub_btrfs_leaf_node leaf;
  319. for (; desc->depth > 0; desc->depth--)
  320. {
  321. desc->data[desc->depth - 1].iter++;
  322. if (desc->data[desc->depth - 1].iter
  323. < desc->data[desc->depth - 1].maxiter)
  324. break;
  325. }
  326. if (desc->depth == 0)
  327. return 0;
  328. while (!desc->data[desc->depth - 1].leaf)
  329. {
  330. struct grub_btrfs_internal_node node;
  331. struct btrfs_header head;
  332. err = grub_btrfs_read_logical (data, desc->data[desc->depth - 1].iter
  333. * sizeof (node)
  334. + sizeof (struct btrfs_header)
  335. + desc->data[desc->depth - 1].addr,
  336. &node, sizeof (node), 0);
  337. if (err)
  338. return -err;
  339. err = grub_btrfs_read_logical (data, grub_le_to_cpu64 (node.addr),
  340. &head, sizeof (head), 0);
  341. if (err)
  342. return -err;
  343. check_btrfs_header (data, &head, grub_le_to_cpu64 (node.addr));
  344. save_ref (desc, grub_le_to_cpu64 (node.addr), 0,
  345. grub_le_to_cpu32 (head.nitems), !head.level);
  346. }
  347. err = grub_btrfs_read_logical (data, desc->data[desc->depth - 1].iter
  348. * sizeof (leaf)
  349. + sizeof (struct btrfs_header)
  350. + desc->data[desc->depth - 1].addr, &leaf,
  351. sizeof (leaf), 0);
  352. if (err)
  353. return -err;
  354. *outsize = grub_le_to_cpu32 (leaf.size);
  355. *outaddr = desc->data[desc->depth - 1].addr + sizeof (struct btrfs_header)
  356. + grub_le_to_cpu32 (leaf.offset);
  357. *key_out = leaf.key;
  358. return 1;
  359. }
  360. static grub_err_t
  361. lower_bound (struct grub_btrfs_data *data,
  362. const struct grub_btrfs_key *key_in,
  363. struct grub_btrfs_key *key_out,
  364. grub_uint64_t root,
  365. grub_disk_addr_t *outaddr, grub_size_t *outsize,
  366. struct grub_btrfs_leaf_descriptor *desc,
  367. int recursion_depth)
  368. {
  369. grub_disk_addr_t addr = grub_le_to_cpu64 (root);
  370. int depth = -1;
  371. if (desc)
  372. {
  373. desc->allocated = 16;
  374. desc->depth = 0;
  375. desc->data = grub_calloc (desc->allocated, sizeof (desc->data[0]));
  376. if (!desc->data)
  377. return grub_errno;
  378. }
  379. /* > 2 would work as well but be robust and allow a bit more just in case.
  380. */
  381. if (recursion_depth > 10)
  382. return grub_error (GRUB_ERR_BAD_FS, "too deep btrfs virtual nesting");
  383. grub_dprintf ("btrfs",
  384. "retrieving %" PRIxGRUB_UINT64_T
  385. " %x %" PRIxGRUB_UINT64_T "\n",
  386. key_in->object_id, key_in->type, key_in->offset);
  387. while (1)
  388. {
  389. grub_err_t err;
  390. struct btrfs_header head;
  391. reiter:
  392. depth++;
  393. /* FIXME: preread few nodes into buffer. */
  394. err = grub_btrfs_read_logical (data, addr, &head, sizeof (head),
  395. recursion_depth + 1);
  396. if (err)
  397. return err;
  398. check_btrfs_header (data, &head, addr);
  399. addr += sizeof (head);
  400. if (head.level)
  401. {
  402. unsigned i;
  403. struct grub_btrfs_internal_node node, node_last;
  404. int have_last = 0;
  405. grub_memset (&node_last, 0, sizeof (node_last));
  406. for (i = 0; i < grub_le_to_cpu32 (head.nitems); i++)
  407. {
  408. err = grub_btrfs_read_logical (data, addr + i * sizeof (node),
  409. &node, sizeof (node),
  410. recursion_depth + 1);
  411. if (err)
  412. return err;
  413. grub_dprintf ("btrfs",
  414. "internal node (depth %d) %" PRIxGRUB_UINT64_T
  415. " %x %" PRIxGRUB_UINT64_T "\n", depth,
  416. node.key.object_id, node.key.type,
  417. node.key.offset);
  418. if (key_cmp (&node.key, key_in) == 0)
  419. {
  420. err = GRUB_ERR_NONE;
  421. if (desc)
  422. err = save_ref (desc, addr - sizeof (head), i,
  423. grub_le_to_cpu32 (head.nitems), 0);
  424. if (err)
  425. return err;
  426. addr = grub_le_to_cpu64 (node.addr);
  427. goto reiter;
  428. }
  429. if (key_cmp (&node.key, key_in) > 0)
  430. break;
  431. node_last = node;
  432. have_last = 1;
  433. }
  434. if (have_last)
  435. {
  436. err = GRUB_ERR_NONE;
  437. if (desc)
  438. err = save_ref (desc, addr - sizeof (head), i - 1,
  439. grub_le_to_cpu32 (head.nitems), 0);
  440. if (err)
  441. return err;
  442. addr = grub_le_to_cpu64 (node_last.addr);
  443. goto reiter;
  444. }
  445. *outsize = 0;
  446. *outaddr = 0;
  447. grub_memset (key_out, 0, sizeof (*key_out));
  448. if (desc)
  449. return save_ref (desc, addr - sizeof (head), -1,
  450. grub_le_to_cpu32 (head.nitems), 0);
  451. return GRUB_ERR_NONE;
  452. }
  453. {
  454. unsigned i;
  455. struct grub_btrfs_leaf_node leaf, leaf_last;
  456. int have_last = 0;
  457. for (i = 0; i < grub_le_to_cpu32 (head.nitems); i++)
  458. {
  459. err = grub_btrfs_read_logical (data, addr + i * sizeof (leaf),
  460. &leaf, sizeof (leaf),
  461. recursion_depth + 1);
  462. if (err)
  463. return err;
  464. grub_dprintf ("btrfs",
  465. "leaf (depth %d) %" PRIxGRUB_UINT64_T
  466. " %x %" PRIxGRUB_UINT64_T "\n", depth,
  467. leaf.key.object_id, leaf.key.type, leaf.key.offset);
  468. if (key_cmp (&leaf.key, key_in) == 0)
  469. {
  470. grub_memcpy (key_out, &leaf.key, sizeof (*key_out));
  471. *outsize = grub_le_to_cpu32 (leaf.size);
  472. *outaddr = addr + grub_le_to_cpu32 (leaf.offset);
  473. if (desc)
  474. return save_ref (desc, addr - sizeof (head), i,
  475. grub_le_to_cpu32 (head.nitems), 1);
  476. return GRUB_ERR_NONE;
  477. }
  478. if (key_cmp (&leaf.key, key_in) > 0)
  479. break;
  480. have_last = 1;
  481. leaf_last = leaf;
  482. }
  483. if (have_last)
  484. {
  485. grub_memcpy (key_out, &leaf_last.key, sizeof (*key_out));
  486. *outsize = grub_le_to_cpu32 (leaf_last.size);
  487. *outaddr = addr + grub_le_to_cpu32 (leaf_last.offset);
  488. if (desc)
  489. return save_ref (desc, addr - sizeof (head), i - 1,
  490. grub_le_to_cpu32 (head.nitems), 1);
  491. return GRUB_ERR_NONE;
  492. }
  493. *outsize = 0;
  494. *outaddr = 0;
  495. grub_memset (key_out, 0, sizeof (*key_out));
  496. if (desc)
  497. return save_ref (desc, addr - sizeof (head), -1,
  498. grub_le_to_cpu32 (head.nitems), 1);
  499. return GRUB_ERR_NONE;
  500. }
  501. }
  502. }
  503. /* Context for find_device. */
  504. struct find_device_ctx
  505. {
  506. struct grub_btrfs_data *data;
  507. grub_uint64_t id;
  508. grub_device_t dev_found;
  509. };
  510. /* Helper for find_device. */
  511. static int
  512. find_device_iter (const char *name, void *data)
  513. {
  514. struct find_device_ctx *ctx = data;
  515. grub_device_t dev;
  516. grub_err_t err;
  517. struct grub_btrfs_superblock sb;
  518. dev = grub_device_open (name);
  519. if (!dev)
  520. return 0;
  521. if (!dev->disk)
  522. {
  523. grub_device_close (dev);
  524. return 0;
  525. }
  526. err = read_sblock (dev->disk, &sb);
  527. if (err == GRUB_ERR_BAD_FS)
  528. {
  529. grub_device_close (dev);
  530. grub_errno = GRUB_ERR_NONE;
  531. return 0;
  532. }
  533. if (err)
  534. {
  535. grub_device_close (dev);
  536. grub_print_error ();
  537. return 0;
  538. }
  539. if (grub_memcmp (ctx->data->sblock.uuid, sb.uuid, sizeof (sb.uuid)) != 0
  540. || sb.this_device.device_id != ctx->id)
  541. {
  542. grub_device_close (dev);
  543. return 0;
  544. }
  545. ctx->dev_found = dev;
  546. return 1;
  547. }
  548. static grub_device_t
  549. find_device (struct grub_btrfs_data *data, grub_uint64_t id)
  550. {
  551. struct find_device_ctx ctx = {
  552. .data = data,
  553. .id = id,
  554. .dev_found = NULL
  555. };
  556. unsigned i;
  557. for (i = 0; i < data->n_devices_attached; i++)
  558. if (id == data->devices_attached[i].id)
  559. return data->devices_attached[i].dev;
  560. grub_device_iterate (find_device_iter, &ctx);
  561. data->n_devices_attached++;
  562. if (data->n_devices_attached > data->n_devices_allocated)
  563. {
  564. void *tmp;
  565. grub_size_t sz;
  566. if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
  567. grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
  568. grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
  569. goto fail;
  570. data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
  571. if (!data->devices_attached)
  572. {
  573. data->devices_attached = tmp;
  574. fail:
  575. if (ctx.dev_found)
  576. grub_device_close (ctx.dev_found);
  577. return NULL;
  578. }
  579. }
  580. data->devices_attached[data->n_devices_attached - 1].id = id;
  581. data->devices_attached[data->n_devices_attached - 1].dev = ctx.dev_found;
  582. return ctx.dev_found;
  583. }
  584. static grub_err_t
  585. btrfs_read_from_chunk (struct grub_btrfs_data *data,
  586. struct grub_btrfs_chunk_item *chunk,
  587. grub_uint64_t stripen, grub_uint64_t stripe_offset,
  588. int redundancy, grub_uint64_t csize,
  589. void *buf)
  590. {
  591. struct grub_btrfs_chunk_stripe *stripe;
  592. grub_disk_addr_t paddr;
  593. grub_device_t dev;
  594. grub_err_t err;
  595. stripe = (struct grub_btrfs_chunk_stripe *) (chunk + 1);
  596. /* Right now the redundancy handling is easy.
  597. With RAID5-like it will be more difficult. */
  598. stripe += stripen + redundancy;
  599. paddr = grub_le_to_cpu64 (stripe->offset) + stripe_offset;
  600. grub_dprintf ("btrfs", "stripe %" PRIxGRUB_UINT64_T
  601. " maps to 0x%" PRIxGRUB_UINT64_T "\n"
  602. "reading paddr 0x%" PRIxGRUB_UINT64_T "\n",
  603. stripen, stripe->offset, paddr);
  604. dev = find_device (data, stripe->device_id);
  605. if (!dev)
  606. {
  607. grub_dprintf ("btrfs",
  608. "couldn't find a necessary member device "
  609. "of multi-device filesystem\n");
  610. grub_errno = GRUB_ERR_NONE;
  611. return GRUB_ERR_READ_ERROR;
  612. }
  613. err = grub_disk_read (dev->disk, paddr >> GRUB_DISK_SECTOR_BITS,
  614. paddr & (GRUB_DISK_SECTOR_SIZE - 1),
  615. csize, buf);
  616. return err;
  617. }
  618. struct raid56_buffer {
  619. void *buf;
  620. int data_is_valid;
  621. };
  622. static void
  623. rebuild_raid5 (char *dest, struct raid56_buffer *buffers,
  624. grub_uint64_t nstripes, grub_uint64_t csize)
  625. {
  626. grub_uint64_t i;
  627. int first;
  628. for(i = 0; buffers[i].data_is_valid && i < nstripes; i++);
  629. if (i == nstripes)
  630. {
  631. grub_dprintf ("btrfs", "called rebuild_raid5(), but all disks are OK\n");
  632. return;
  633. }
  634. grub_dprintf ("btrfs", "rebuilding RAID 5 stripe #%" PRIuGRUB_UINT64_T "\n", i);
  635. for (i = 0, first = 1; i < nstripes; i++)
  636. {
  637. if (!buffers[i].data_is_valid)
  638. continue;
  639. if (first) {
  640. grub_memcpy(dest, buffers[i].buf, csize);
  641. first = 0;
  642. } else
  643. grub_crypto_xor (dest, dest, buffers[i].buf, csize);
  644. }
  645. }
  646. static grub_err_t
  647. raid6_recover_read_buffer (void *data, int disk_nr,
  648. grub_uint64_t addr __attribute__ ((unused)),
  649. void *dest, grub_size_t size)
  650. {
  651. struct raid56_buffer *buffers = data;
  652. if (!buffers[disk_nr].data_is_valid)
  653. return grub_errno = GRUB_ERR_READ_ERROR;
  654. grub_memcpy(dest, buffers[disk_nr].buf, size);
  655. return grub_errno = GRUB_ERR_NONE;
  656. }
  657. static void
  658. rebuild_raid6 (struct raid56_buffer *buffers, grub_uint64_t nstripes,
  659. grub_uint64_t csize, grub_uint64_t parities_pos, void *dest,
  660. grub_uint64_t stripen)
  661. {
  662. grub_raid6_recover_gen (buffers, nstripes, stripen, parities_pos,
  663. dest, 0, csize, 0, raid6_recover_read_buffer);
  664. }
  665. static grub_err_t
  666. raid56_read_retry (struct grub_btrfs_data *data,
  667. struct grub_btrfs_chunk_item *chunk,
  668. grub_uint64_t stripe_offset, grub_uint64_t stripen,
  669. grub_uint64_t csize, void *buf, grub_uint64_t parities_pos)
  670. {
  671. struct raid56_buffer *buffers;
  672. grub_uint64_t nstripes = grub_le_to_cpu16 (chunk->nstripes);
  673. grub_uint64_t chunk_type = grub_le_to_cpu64 (chunk->type);
  674. grub_err_t ret = GRUB_ERR_OUT_OF_MEMORY;
  675. grub_uint64_t i, failed_devices;
  676. buffers = grub_calloc (nstripes, sizeof (*buffers));
  677. if (!buffers)
  678. goto cleanup;
  679. for (i = 0; i < nstripes; i++)
  680. {
  681. buffers[i].buf = grub_zalloc (csize);
  682. if (!buffers[i].buf)
  683. goto cleanup;
  684. }
  685. for (failed_devices = 0, i = 0; i < nstripes; i++)
  686. {
  687. struct grub_btrfs_chunk_stripe *stripe;
  688. grub_disk_addr_t paddr;
  689. grub_device_t dev;
  690. grub_err_t err;
  691. /*
  692. * The struct grub_btrfs_chunk_stripe array lives
  693. * behind struct grub_btrfs_chunk_item.
  694. */
  695. stripe = (struct grub_btrfs_chunk_stripe *) (chunk + 1) + i;
  696. paddr = grub_le_to_cpu64 (stripe->offset) + stripe_offset;
  697. grub_dprintf ("btrfs", "reading paddr %" PRIxGRUB_UINT64_T
  698. " from stripe ID %" PRIxGRUB_UINT64_T "\n",
  699. paddr, stripe->device_id);
  700. dev = find_device (data, stripe->device_id);
  701. if (!dev)
  702. {
  703. grub_dprintf ("btrfs", "stripe %" PRIuGRUB_UINT64_T " FAILED (dev ID %"
  704. PRIxGRUB_UINT64_T ")\n", i, stripe->device_id);
  705. failed_devices++;
  706. continue;
  707. }
  708. err = grub_disk_read (dev->disk, paddr >> GRUB_DISK_SECTOR_BITS,
  709. paddr & (GRUB_DISK_SECTOR_SIZE - 1),
  710. csize, buffers[i].buf);
  711. if (err == GRUB_ERR_NONE)
  712. {
  713. buffers[i].data_is_valid = 1;
  714. grub_dprintf ("btrfs", "stripe %" PRIuGRUB_UINT64_T " OK (dev ID %"
  715. PRIxGRUB_UINT64_T ")\n", i, stripe->device_id);
  716. }
  717. else
  718. {
  719. grub_dprintf ("btrfs", "stripe %" PRIuGRUB_UINT64_T
  720. " READ FAILED (dev ID %" PRIxGRUB_UINT64_T ")\n",
  721. i, stripe->device_id);
  722. failed_devices++;
  723. }
  724. }
  725. if (failed_devices > 1 && (chunk_type & GRUB_BTRFS_CHUNK_TYPE_RAID5))
  726. {
  727. grub_dprintf ("btrfs", "not enough disks for RAID 5: total %" PRIuGRUB_UINT64_T
  728. ", missing %" PRIuGRUB_UINT64_T "\n",
  729. nstripes, failed_devices);
  730. ret = GRUB_ERR_READ_ERROR;
  731. goto cleanup;
  732. }
  733. else if (failed_devices > 2 && (chunk_type & GRUB_BTRFS_CHUNK_TYPE_RAID6))
  734. {
  735. grub_dprintf ("btrfs", "not enough disks for RAID 6: total %" PRIuGRUB_UINT64_T
  736. ", missing %" PRIuGRUB_UINT64_T "\n",
  737. nstripes, failed_devices);
  738. ret = GRUB_ERR_READ_ERROR;
  739. goto cleanup;
  740. }
  741. else
  742. grub_dprintf ("btrfs", "enough disks for RAID 5: total %"
  743. PRIuGRUB_UINT64_T ", missing %" PRIuGRUB_UINT64_T "\n",
  744. nstripes, failed_devices);
  745. /* We have enough disks. So, rebuild the data. */
  746. if (chunk_type & GRUB_BTRFS_CHUNK_TYPE_RAID5)
  747. rebuild_raid5 (buf, buffers, nstripes, csize);
  748. else
  749. rebuild_raid6 (buffers, nstripes, csize, parities_pos, buf, stripen);
  750. ret = GRUB_ERR_NONE;
  751. cleanup:
  752. if (buffers)
  753. for (i = 0; i < nstripes; i++)
  754. grub_free (buffers[i].buf);
  755. grub_free (buffers);
  756. return ret;
  757. }
  758. static grub_err_t
  759. grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
  760. void *buf, grub_size_t size, int recursion_depth)
  761. {
  762. while (size > 0)
  763. {
  764. grub_uint8_t *ptr;
  765. struct grub_btrfs_key *key;
  766. struct grub_btrfs_chunk_item *chunk;
  767. grub_uint64_t csize;
  768. grub_err_t err = 0;
  769. struct grub_btrfs_key key_out;
  770. int challoc = 0;
  771. struct grub_btrfs_key key_in;
  772. grub_size_t chsize;
  773. grub_disk_addr_t chaddr;
  774. grub_dprintf ("btrfs", "searching for laddr %" PRIxGRUB_UINT64_T "\n",
  775. addr);
  776. for (ptr = data->sblock.bootstrap_mapping;
  777. ptr < data->sblock.bootstrap_mapping
  778. + sizeof (data->sblock.bootstrap_mapping)
  779. - sizeof (struct grub_btrfs_key);)
  780. {
  781. key = (struct grub_btrfs_key *) ptr;
  782. if (key->type != GRUB_BTRFS_ITEM_TYPE_CHUNK)
  783. break;
  784. chunk = (struct grub_btrfs_chunk_item *) (key + 1);
  785. grub_dprintf ("btrfs",
  786. "%" PRIxGRUB_UINT64_T " %" PRIxGRUB_UINT64_T " \n",
  787. grub_le_to_cpu64 (key->offset),
  788. grub_le_to_cpu64 (chunk->size));
  789. if (grub_le_to_cpu64 (key->offset) <= addr
  790. && addr < grub_le_to_cpu64 (key->offset)
  791. + grub_le_to_cpu64 (chunk->size))
  792. goto chunk_found;
  793. ptr += sizeof (*key) + sizeof (*chunk)
  794. + sizeof (struct grub_btrfs_chunk_stripe)
  795. * grub_le_to_cpu16 (chunk->nstripes);
  796. }
  797. key_in.object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
  798. key_in.type = GRUB_BTRFS_ITEM_TYPE_CHUNK;
  799. key_in.offset = grub_cpu_to_le64 (addr);
  800. err = lower_bound (data, &key_in, &key_out,
  801. data->sblock.chunk_tree,
  802. &chaddr, &chsize, NULL, recursion_depth);
  803. if (err)
  804. return err;
  805. key = &key_out;
  806. if (key->type != GRUB_BTRFS_ITEM_TYPE_CHUNK
  807. || !(grub_le_to_cpu64 (key->offset) <= addr))
  808. return grub_error (GRUB_ERR_BAD_FS,
  809. "couldn't find the chunk descriptor");
  810. if (!chsize)
  811. {
  812. grub_dprintf ("btrfs", "zero-size chunk\n");
  813. return grub_error (GRUB_ERR_BAD_FS,
  814. "got an invalid zero-size chunk");
  815. }
  816. /*
  817. * The space being allocated for a chunk should at least be able to
  818. * contain one chunk item.
  819. */
  820. if (chsize < sizeof (struct grub_btrfs_chunk_item))
  821. {
  822. grub_dprintf ("btrfs", "chunk-size too small\n");
  823. return grub_error (GRUB_ERR_BAD_FS,
  824. "got an invalid chunk size");
  825. }
  826. chunk = grub_malloc (chsize);
  827. if (!chunk)
  828. return grub_errno;
  829. challoc = 1;
  830. err = grub_btrfs_read_logical (data, chaddr, chunk, chsize,
  831. recursion_depth);
  832. if (err)
  833. {
  834. grub_free (chunk);
  835. return err;
  836. }
  837. chunk_found:
  838. {
  839. grub_uint64_t stripen;
  840. grub_uint64_t stripe_offset;
  841. grub_uint64_t off = addr - grub_le_to_cpu64 (key->offset);
  842. grub_uint64_t chunk_stripe_length;
  843. grub_uint16_t nstripes;
  844. unsigned redundancy = 1;
  845. unsigned i, j;
  846. int is_raid56;
  847. grub_uint64_t parities_pos = 0;
  848. is_raid56 = !!(grub_le_to_cpu64 (chunk->type) &
  849. (GRUB_BTRFS_CHUNK_TYPE_RAID5 |
  850. GRUB_BTRFS_CHUNK_TYPE_RAID6));
  851. if (grub_le_to_cpu64 (chunk->size) <= off)
  852. {
  853. grub_dprintf ("btrfs", "no chunk\n");
  854. return grub_error (GRUB_ERR_BAD_FS,
  855. "couldn't find the chunk descriptor");
  856. }
  857. nstripes = grub_le_to_cpu16 (chunk->nstripes) ? : 1;
  858. chunk_stripe_length = grub_le_to_cpu64 (chunk->stripe_length) ? : 512;
  859. grub_dprintf ("btrfs", "chunk 0x%" PRIxGRUB_UINT64_T
  860. "+0x%" PRIxGRUB_UINT64_T
  861. " (%d stripes (%d substripes) of %"
  862. PRIxGRUB_UINT64_T ")\n",
  863. grub_le_to_cpu64 (key->offset),
  864. grub_le_to_cpu64 (chunk->size),
  865. nstripes,
  866. grub_le_to_cpu16 (chunk->nsubstripes),
  867. chunk_stripe_length);
  868. switch (grub_le_to_cpu64 (chunk->type)
  869. & ~GRUB_BTRFS_CHUNK_TYPE_BITS_DONTCARE)
  870. {
  871. case GRUB_BTRFS_CHUNK_TYPE_SINGLE:
  872. {
  873. grub_uint64_t stripe_length;
  874. grub_dprintf ("btrfs", "single\n");
  875. stripe_length = grub_divmod64 (grub_le_to_cpu64 (chunk->size),
  876. nstripes,
  877. NULL);
  878. /* For single, there should be exactly 1 stripe. */
  879. if (grub_le_to_cpu16 (chunk->nstripes) != 1)
  880. {
  881. grub_dprintf ("btrfs", "invalid RAID_SINGLE: nstripes != 1 (%u)\n",
  882. grub_le_to_cpu16 (chunk->nstripes));
  883. return grub_error (GRUB_ERR_BAD_FS,
  884. "invalid RAID_SINGLE: nstripes != 1 (%u)",
  885. grub_le_to_cpu16 (chunk->nstripes));
  886. }
  887. if (stripe_length == 0)
  888. stripe_length = 512;
  889. stripen = grub_divmod64 (off, stripe_length, &stripe_offset);
  890. csize = (stripen + 1) * stripe_length - off;
  891. break;
  892. }
  893. case GRUB_BTRFS_CHUNK_TYPE_RAID1C4:
  894. redundancy++;
  895. /* fall through */
  896. case GRUB_BTRFS_CHUNK_TYPE_RAID1C3:
  897. redundancy++;
  898. /* fall through */
  899. case GRUB_BTRFS_CHUNK_TYPE_DUPLICATED:
  900. case GRUB_BTRFS_CHUNK_TYPE_RAID1:
  901. {
  902. grub_dprintf ("btrfs", "RAID1 (copies: %d)\n", ++redundancy);
  903. stripen = 0;
  904. stripe_offset = off;
  905. csize = grub_le_to_cpu64 (chunk->size) - off;
  906. /*
  907. * Redundancy, and substripes only apply to RAID10, and there
  908. * should be exactly 2 sub-stripes.
  909. */
  910. if (grub_le_to_cpu16 (chunk->nstripes) != redundancy)
  911. {
  912. grub_dprintf ("btrfs", "invalid RAID1: nstripes != %u (%u)\n",
  913. redundancy, grub_le_to_cpu16 (chunk->nstripes));
  914. return grub_error (GRUB_ERR_BAD_FS,
  915. "invalid RAID1: nstripes != %u (%u)",
  916. redundancy, grub_le_to_cpu16 (chunk->nstripes));
  917. }
  918. break;
  919. }
  920. case GRUB_BTRFS_CHUNK_TYPE_RAID0:
  921. {
  922. grub_uint64_t middle, high;
  923. grub_uint64_t low;
  924. grub_dprintf ("btrfs", "RAID0\n");
  925. middle = grub_divmod64 (off,
  926. chunk_stripe_length,
  927. &low);
  928. high = grub_divmod64 (middle, nstripes,
  929. &stripen);
  930. stripe_offset =
  931. low + chunk_stripe_length * high;
  932. csize = chunk_stripe_length - low;
  933. break;
  934. }
  935. case GRUB_BTRFS_CHUNK_TYPE_RAID10:
  936. {
  937. grub_uint64_t middle, high;
  938. grub_uint64_t low;
  939. grub_uint16_t nsubstripes;
  940. nsubstripes = grub_le_to_cpu16 (chunk->nsubstripes) ? : 1;
  941. middle = grub_divmod64 (off,
  942. chunk_stripe_length,
  943. &low);
  944. high = grub_divmod64 (middle,
  945. nstripes / nsubstripes ? : 1,
  946. &stripen);
  947. stripen *= nsubstripes;
  948. redundancy = nsubstripes;
  949. stripe_offset = low + chunk_stripe_length
  950. * high;
  951. csize = chunk_stripe_length - low;
  952. /*
  953. * Substripes only apply to RAID10, and there
  954. * should be exactly 2 sub-stripes.
  955. */
  956. if (grub_le_to_cpu16 (chunk->nsubstripes) != 2)
  957. {
  958. grub_dprintf ("btrfs", "invalid RAID10: nsubstripes != 2 (%u)",
  959. grub_le_to_cpu16 (chunk->nsubstripes));
  960. return grub_error (GRUB_ERR_BAD_FS,
  961. "invalid RAID10: nsubstripes != 2 (%u)",
  962. grub_le_to_cpu16 (chunk->nsubstripes));
  963. }
  964. break;
  965. }
  966. case GRUB_BTRFS_CHUNK_TYPE_RAID5:
  967. case GRUB_BTRFS_CHUNK_TYPE_RAID6:
  968. {
  969. grub_uint64_t nparities, stripe_nr, high, low;
  970. redundancy = 1; /* no redundancy for now */
  971. if (grub_le_to_cpu64 (chunk->type) & GRUB_BTRFS_CHUNK_TYPE_RAID5)
  972. {
  973. grub_dprintf ("btrfs", "RAID5\n");
  974. nparities = 1;
  975. }
  976. else
  977. {
  978. grub_dprintf ("btrfs", "RAID6\n");
  979. nparities = 2;
  980. }
  981. /*
  982. * RAID 6 layout consists of several stripes spread over
  983. * the disks, e.g.:
  984. *
  985. * Disk_0 Disk_1 Disk_2 Disk_3
  986. * A0 B0 P0 Q0
  987. * Q1 A1 B1 P1
  988. * P2 Q2 A2 B2
  989. *
  990. * Note: placement of the parities depend on row number.
  991. *
  992. * Pay attention that the btrfs terminology may differ from
  993. * terminology used in other RAID implementations, e.g. LVM,
  994. * dm or md. The main difference is that btrfs calls contiguous
  995. * block of data on a given disk, e.g. A0, stripe instead of chunk.
  996. *
  997. * The variables listed below have following meaning:
  998. * - stripe_nr is the stripe number excluding the parities
  999. * (A0 = 0, B0 = 1, A1 = 2, B1 = 3, etc.),
  1000. * - high is the row number (0 for A0...Q0, 1 for Q1...P1, etc.),
  1001. * - stripen is the disk number in a row (0 for A0, Q1, P2,
  1002. * 1 for B0, A1, Q2, etc.),
  1003. * - off is the logical address to read,
  1004. * - chunk_stripe_length is the size of a stripe (typically 64 KiB),
  1005. * - nstripes is the number of disks in a row,
  1006. * - low is the offset of the data inside a stripe,
  1007. * - stripe_offset is the data offset in an array,
  1008. * - csize is the "potential" data to read; it will be reduced
  1009. * to size if the latter is smaller,
  1010. * - nparities is the number of parities (1 for RAID 5, 2 for
  1011. * RAID 6); used only in RAID 5/6 code.
  1012. */
  1013. stripe_nr = grub_divmod64 (off, chunk_stripe_length, &low);
  1014. /*
  1015. * stripen is computed without the parities
  1016. * (0 for A0, A1, A2, 1 for B0, B1, B2, etc.).
  1017. */
  1018. if (nparities >= nstripes)
  1019. return grub_error (GRUB_ERR_BAD_FS,
  1020. "invalid RAID5/6: nparities >= nstripes");
  1021. high = grub_divmod64 (stripe_nr, nstripes - nparities, &stripen);
  1022. /*
  1023. * The stripes are spread over the disks. Every each row their
  1024. * positions are shifted by 1 place. So, the real disks number
  1025. * change. Hence, we have to take into account current row number
  1026. * modulo nstripes (0 for A0, 1 for A1, 2 for A2, etc.).
  1027. */
  1028. grub_divmod64 (high + stripen, nstripes, &stripen);
  1029. /*
  1030. * parities_pos is equal to ((high - nparities) % nstripes)
  1031. * (see the diagram above). However, (high - nparities) can
  1032. * be negative, e.g. when high == 0, leading to an incorrect
  1033. * results. (high + nstripes - nparities) is always positive and
  1034. * modulo nstripes is equal to ((high - nparities) % nstripes).
  1035. */
  1036. grub_divmod64 (high + nstripes - nparities, nstripes, &parities_pos);
  1037. stripe_offset = chunk_stripe_length * high + low;
  1038. csize = chunk_stripe_length - low;
  1039. break;
  1040. }
  1041. default:
  1042. grub_dprintf ("btrfs", "unsupported RAID\n");
  1043. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1044. "unsupported RAID flags %" PRIxGRUB_UINT64_T,
  1045. grub_le_to_cpu64 (chunk->type));
  1046. }
  1047. if (csize == 0)
  1048. return grub_error (GRUB_ERR_BUG,
  1049. "couldn't find the chunk descriptor");
  1050. if (csize > (grub_uint64_t) size)
  1051. csize = size;
  1052. /*
  1053. * The space for a chunk stripe is limited to the space provide in the super-block's
  1054. * bootstrap mapping with an initial btrfs key at the start of each chunk.
  1055. */
  1056. grub_size_t avail_stripes = sizeof (data->sblock.bootstrap_mapping) /
  1057. (sizeof (struct grub_btrfs_key) + sizeof (struct grub_btrfs_chunk_stripe));
  1058. for (j = 0; j < 2; j++)
  1059. {
  1060. grub_size_t est_chunk_alloc = 0;
  1061. grub_dprintf ("btrfs", "chunk 0x%" PRIxGRUB_UINT64_T
  1062. "+0x%" PRIxGRUB_UINT64_T
  1063. " (%d stripes (%d substripes) of %"
  1064. PRIxGRUB_UINT64_T ")\n",
  1065. grub_le_to_cpu64 (key->offset),
  1066. grub_le_to_cpu64 (chunk->size),
  1067. grub_le_to_cpu16 (chunk->nstripes),
  1068. grub_le_to_cpu16 (chunk->nsubstripes),
  1069. grub_le_to_cpu64 (chunk->stripe_length));
  1070. grub_dprintf ("btrfs", "reading laddr 0x%" PRIxGRUB_UINT64_T "\n",
  1071. addr);
  1072. if (grub_mul (sizeof (struct grub_btrfs_chunk_stripe),
  1073. grub_le_to_cpu16 (chunk->nstripes), &est_chunk_alloc) ||
  1074. grub_add (est_chunk_alloc,
  1075. sizeof (struct grub_btrfs_chunk_item), &est_chunk_alloc) ||
  1076. est_chunk_alloc > chunk->size)
  1077. {
  1078. err = GRUB_ERR_BAD_FS;
  1079. break;
  1080. }
  1081. if (grub_le_to_cpu16 (chunk->nstripes) > avail_stripes)
  1082. {
  1083. err = GRUB_ERR_BAD_FS;
  1084. break;
  1085. }
  1086. if (is_raid56)
  1087. {
  1088. err = btrfs_read_from_chunk (data, chunk, stripen,
  1089. stripe_offset,
  1090. 0, /* no mirror */
  1091. csize, buf);
  1092. grub_errno = GRUB_ERR_NONE;
  1093. if (err)
  1094. err = raid56_read_retry (data, chunk, stripe_offset,
  1095. stripen, csize, buf, parities_pos);
  1096. }
  1097. else
  1098. for (i = 0; i < redundancy; i++)
  1099. {
  1100. err = btrfs_read_from_chunk (data, chunk, stripen,
  1101. stripe_offset,
  1102. i, /* redundancy */
  1103. csize, buf);
  1104. if (!err)
  1105. break;
  1106. grub_errno = GRUB_ERR_NONE;
  1107. }
  1108. if (!err)
  1109. break;
  1110. }
  1111. if (err)
  1112. return grub_errno = err;
  1113. }
  1114. size -= csize;
  1115. buf = (grub_uint8_t *) buf + csize;
  1116. addr += csize;
  1117. if (challoc)
  1118. grub_free (chunk);
  1119. }
  1120. return GRUB_ERR_NONE;
  1121. }
  1122. static struct grub_btrfs_data *
  1123. grub_btrfs_mount (grub_device_t dev)
  1124. {
  1125. struct grub_btrfs_data *data;
  1126. grub_err_t err;
  1127. if (!dev->disk)
  1128. {
  1129. grub_error (GRUB_ERR_BAD_FS, "not BtrFS");
  1130. return NULL;
  1131. }
  1132. data = grub_zalloc (sizeof (*data));
  1133. if (!data)
  1134. return NULL;
  1135. err = read_sblock (dev->disk, &data->sblock);
  1136. if (err)
  1137. {
  1138. grub_free (data);
  1139. return NULL;
  1140. }
  1141. data->n_devices_allocated = 16;
  1142. data->devices_attached = grub_malloc (sizeof (data->devices_attached[0])
  1143. * data->n_devices_allocated);
  1144. if (!data->devices_attached)
  1145. {
  1146. grub_free (data);
  1147. return NULL;
  1148. }
  1149. data->n_devices_attached = 1;
  1150. data->devices_attached[0].dev = dev;
  1151. data->devices_attached[0].id = data->sblock.this_device.device_id;
  1152. return data;
  1153. }
  1154. static void
  1155. grub_btrfs_unmount (struct grub_btrfs_data *data)
  1156. {
  1157. unsigned i;
  1158. /* The device 0 is closed one layer upper. */
  1159. for (i = 1; i < data->n_devices_attached; i++)
  1160. if (data->devices_attached[i].dev)
  1161. grub_device_close (data->devices_attached[i].dev);
  1162. grub_free (data->devices_attached);
  1163. grub_free (data->extent);
  1164. grub_free (data);
  1165. }
  1166. static grub_err_t
  1167. grub_btrfs_read_inode (struct grub_btrfs_data *data,
  1168. struct grub_btrfs_inode *inode, grub_uint64_t num,
  1169. grub_uint64_t tree)
  1170. {
  1171. struct grub_btrfs_key key_in, key_out;
  1172. grub_disk_addr_t elemaddr;
  1173. grub_size_t elemsize;
  1174. grub_err_t err;
  1175. key_in.object_id = num;
  1176. key_in.type = GRUB_BTRFS_ITEM_TYPE_INODE_ITEM;
  1177. key_in.offset = 0;
  1178. err = lower_bound (data, &key_in, &key_out, tree, &elemaddr, &elemsize, NULL,
  1179. 0);
  1180. if (err)
  1181. return err;
  1182. if (num != key_out.object_id
  1183. || key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_ITEM)
  1184. return grub_error (GRUB_ERR_BAD_FS, "inode not found");
  1185. return grub_btrfs_read_logical (data, elemaddr, inode, sizeof (*inode), 0);
  1186. }
  1187. static void *grub_zstd_malloc (void *state __attribute__((unused)), size_t size)
  1188. {
  1189. return grub_malloc (size);
  1190. }
  1191. static void grub_zstd_free (void *state __attribute__((unused)), void *address)
  1192. {
  1193. return grub_free (address);
  1194. }
  1195. static ZSTD_customMem grub_zstd_allocator (void)
  1196. {
  1197. ZSTD_customMem allocator;
  1198. allocator.customAlloc = &grub_zstd_malloc;
  1199. allocator.customFree = &grub_zstd_free;
  1200. allocator.opaque = NULL;
  1201. return allocator;
  1202. }
  1203. static grub_ssize_t
  1204. grub_btrfs_zstd_decompress (char *ibuf, grub_size_t isize, grub_off_t off,
  1205. char *obuf, grub_size_t osize)
  1206. {
  1207. void *allocated = NULL;
  1208. char *otmpbuf = obuf;
  1209. grub_size_t otmpsize = osize;
  1210. ZSTD_DCtx *dctx = NULL;
  1211. grub_size_t zstd_ret;
  1212. grub_ssize_t ret = -1;
  1213. /*
  1214. * Zstd will fail if it can't fit the entire output in the destination
  1215. * buffer, so if osize isn't large enough, allocate a temporary buffer.
  1216. */
  1217. if (otmpsize < ZSTD_BTRFS_MAX_INPUT)
  1218. {
  1219. allocated = grub_malloc (ZSTD_BTRFS_MAX_INPUT);
  1220. if (!allocated)
  1221. {
  1222. grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed allocate a zstd buffer");
  1223. goto err;
  1224. }
  1225. otmpbuf = (char *) allocated;
  1226. otmpsize = ZSTD_BTRFS_MAX_INPUT;
  1227. }
  1228. /* Create the ZSTD_DCtx. */
  1229. dctx = ZSTD_createDCtx_advanced (grub_zstd_allocator ());
  1230. if (!dctx)
  1231. {
  1232. /* ZSTD_createDCtx_advanced() only fails if it is out of memory. */
  1233. grub_error (GRUB_ERR_OUT_OF_MEMORY, "failed to create a zstd context");
  1234. goto err;
  1235. }
  1236. /*
  1237. * Get the real input size, there may be junk at the
  1238. * end of the frame.
  1239. */
  1240. isize = ZSTD_findFrameCompressedSize (ibuf, isize);
  1241. if (ZSTD_isError (isize))
  1242. {
  1243. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "zstd data corrupted");
  1244. goto err;
  1245. }
  1246. /* Decompress and check for errors. */
  1247. zstd_ret = ZSTD_decompressDCtx (dctx, otmpbuf, otmpsize, ibuf, isize);
  1248. if (ZSTD_isError (zstd_ret))
  1249. {
  1250. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "zstd data corrupted");
  1251. goto err;
  1252. }
  1253. /*
  1254. * Move the requested data into the obuf. obuf may be equal
  1255. * to otmpbuf, which is why grub_memmove() is required.
  1256. */
  1257. grub_memmove (obuf, otmpbuf + off, osize);
  1258. ret = osize;
  1259. err:
  1260. grub_free (allocated);
  1261. ZSTD_freeDCtx (dctx);
  1262. return ret;
  1263. }
  1264. static grub_ssize_t
  1265. grub_btrfs_lzo_decompress(char *ibuf, grub_size_t isize, grub_off_t off,
  1266. char *obuf, grub_size_t osize)
  1267. {
  1268. grub_uint32_t total_size, cblock_size;
  1269. grub_size_t ret = 0;
  1270. char *ibuf0 = ibuf;
  1271. total_size = grub_le_to_cpu32 (grub_get_unaligned32 (ibuf));
  1272. ibuf += sizeof (total_size);
  1273. if (isize < total_size)
  1274. return -1;
  1275. /* Jump forward to first block with requested data. */
  1276. while (off >= GRUB_BTRFS_LZO_BLOCK_SIZE)
  1277. {
  1278. /* Don't let following uint32_t cross the page boundary. */
  1279. if (((ibuf - ibuf0) & 0xffc) == 0xffc)
  1280. ibuf = ((ibuf - ibuf0 + 3) & ~3) + ibuf0;
  1281. cblock_size = grub_le_to_cpu32 (grub_get_unaligned32 (ibuf));
  1282. ibuf += sizeof (cblock_size);
  1283. if (cblock_size > GRUB_BTRFS_LZO_BLOCK_MAX_CSIZE)
  1284. return -1;
  1285. off -= GRUB_BTRFS_LZO_BLOCK_SIZE;
  1286. ibuf += cblock_size;
  1287. }
  1288. while (osize > 0)
  1289. {
  1290. lzo_uint usize = GRUB_BTRFS_LZO_BLOCK_SIZE;
  1291. /* Don't let following uint32_t cross the page boundary. */
  1292. if (((ibuf - ibuf0) & 0xffc) == 0xffc)
  1293. ibuf = ((ibuf - ibuf0 + 3) & ~3) + ibuf0;
  1294. cblock_size = grub_le_to_cpu32 (grub_get_unaligned32 (ibuf));
  1295. ibuf += sizeof (cblock_size);
  1296. if (cblock_size > GRUB_BTRFS_LZO_BLOCK_MAX_CSIZE)
  1297. return -1;
  1298. /* Block partially filled with requested data. */
  1299. if (off > 0 || osize < GRUB_BTRFS_LZO_BLOCK_SIZE)
  1300. {
  1301. grub_size_t to_copy = GRUB_BTRFS_LZO_BLOCK_SIZE - off;
  1302. grub_uint8_t *buf;
  1303. if (to_copy > osize)
  1304. to_copy = osize;
  1305. buf = grub_malloc (GRUB_BTRFS_LZO_BLOCK_SIZE);
  1306. if (!buf)
  1307. return -1;
  1308. if (lzo1x_decompress_safe ((lzo_bytep)ibuf, cblock_size, buf, &usize,
  1309. NULL) != LZO_E_OK)
  1310. {
  1311. grub_free (buf);
  1312. return -1;
  1313. }
  1314. if (to_copy > usize)
  1315. to_copy = usize;
  1316. grub_memcpy(obuf, buf + off, to_copy);
  1317. osize -= to_copy;
  1318. ret += to_copy;
  1319. obuf += to_copy;
  1320. ibuf += cblock_size;
  1321. off = 0;
  1322. grub_free (buf);
  1323. continue;
  1324. }
  1325. /* Decompress whole block directly to output buffer. */
  1326. if (lzo1x_decompress_safe ((lzo_bytep)ibuf, cblock_size, (lzo_bytep)obuf,
  1327. &usize, NULL) != LZO_E_OK)
  1328. return -1;
  1329. osize -= usize;
  1330. ret += usize;
  1331. obuf += usize;
  1332. ibuf += cblock_size;
  1333. }
  1334. return ret;
  1335. }
  1336. static grub_ssize_t
  1337. grub_btrfs_extent_read (struct grub_btrfs_data *data,
  1338. grub_uint64_t ino, grub_uint64_t tree,
  1339. grub_off_t pos0, char *buf, grub_size_t len)
  1340. {
  1341. grub_off_t pos = pos0;
  1342. while (len)
  1343. {
  1344. grub_size_t csize;
  1345. grub_err_t err;
  1346. grub_off_t extoff;
  1347. struct grub_btrfs_leaf_descriptor desc;
  1348. if (!data->extent || data->extstart > pos || data->extino != ino
  1349. || data->exttree != tree || data->extend <= pos)
  1350. {
  1351. struct grub_btrfs_key key_in, key_out;
  1352. grub_disk_addr_t elemaddr;
  1353. grub_size_t elemsize;
  1354. grub_free (data->extent);
  1355. key_in.object_id = ino;
  1356. key_in.type = GRUB_BTRFS_ITEM_TYPE_EXTENT_ITEM;
  1357. key_in.offset = grub_cpu_to_le64 (pos);
  1358. err = lower_bound (data, &key_in, &key_out, tree,
  1359. &elemaddr, &elemsize, &desc, 0);
  1360. if (err)
  1361. return -1;
  1362. if (key_out.object_id != ino
  1363. || key_out.type != GRUB_BTRFS_ITEM_TYPE_EXTENT_ITEM)
  1364. {
  1365. grub_error (GRUB_ERR_BAD_FS, "extent not found");
  1366. return -1;
  1367. }
  1368. if ((grub_ssize_t) elemsize < ((char *) &data->extent->inl
  1369. - (char *) data->extent))
  1370. {
  1371. grub_error (GRUB_ERR_BAD_FS, "extent descriptor is too short");
  1372. return -1;
  1373. }
  1374. data->extstart = grub_le_to_cpu64 (key_out.offset);
  1375. data->extsize = elemsize;
  1376. data->extent = grub_malloc (elemsize);
  1377. data->extino = ino;
  1378. data->exttree = tree;
  1379. if (!data->extent)
  1380. return grub_errno;
  1381. err = grub_btrfs_read_logical (data, elemaddr, data->extent,
  1382. elemsize, 0);
  1383. if (err)
  1384. return err;
  1385. data->extend = data->extstart + grub_le_to_cpu64 (data->extent->size);
  1386. if (data->extent->type == GRUB_BTRFS_EXTENT_REGULAR
  1387. && (char *) data->extent + elemsize
  1388. >= (char *) &data->extent->filled + sizeof (data->extent->filled))
  1389. data->extend =
  1390. data->extstart + grub_le_to_cpu64 (data->extent->filled);
  1391. grub_dprintf ("btrfs", "regular extent 0x%" PRIxGRUB_UINT64_T "+0x%"
  1392. PRIxGRUB_UINT64_T "\n",
  1393. grub_le_to_cpu64 (key_out.offset),
  1394. grub_le_to_cpu64 (data->extent->size));
  1395. /*
  1396. * The way of extent item iteration is pretty bad, it completely
  1397. * requires all extents are contiguous, which is not ensured.
  1398. *
  1399. * Features like NO_HOLE and mixed inline/regular extents can cause
  1400. * gaps between file extent items.
  1401. *
  1402. * The correct way is to follow Linux kernel/U-boot to iterate item
  1403. * by item, without any assumption on the file offset continuity.
  1404. *
  1405. * Here we just manually skip to next item and re-do the verification.
  1406. *
  1407. * TODO: Rework the whole extent item iteration code, if not the
  1408. * whole btrfs implementation.
  1409. */
  1410. if (data->extend <= pos)
  1411. {
  1412. err = next (data, &desc, &elemaddr, &elemsize, &key_out);
  1413. if (err < 0)
  1414. return -1;
  1415. /* No next item for the inode, we hit the end. */
  1416. if (err == 0 || key_out.object_id != ino ||
  1417. key_out.type != GRUB_BTRFS_ITEM_TYPE_EXTENT_ITEM)
  1418. return pos - pos0;
  1419. csize = grub_le_to_cpu64 (key_out.offset) - pos;
  1420. if (csize > len)
  1421. csize = len;
  1422. grub_memset (buf, 0, csize);
  1423. buf += csize;
  1424. pos += csize;
  1425. len -= csize;
  1426. continue;
  1427. }
  1428. }
  1429. csize = data->extend - pos;
  1430. extoff = pos - data->extstart;
  1431. if (csize > len)
  1432. csize = len;
  1433. if (data->extent->encryption)
  1434. {
  1435. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1436. "encryption not supported");
  1437. return -1;
  1438. }
  1439. if (data->extent->compression != GRUB_BTRFS_COMPRESSION_NONE
  1440. && data->extent->compression != GRUB_BTRFS_COMPRESSION_ZLIB
  1441. && data->extent->compression != GRUB_BTRFS_COMPRESSION_LZO
  1442. && data->extent->compression != GRUB_BTRFS_COMPRESSION_ZSTD)
  1443. {
  1444. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1445. "compression type 0x%x not supported",
  1446. data->extent->compression);
  1447. return -1;
  1448. }
  1449. if (data->extent->encoding)
  1450. {
  1451. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "encoding not supported");
  1452. return -1;
  1453. }
  1454. switch (data->extent->type)
  1455. {
  1456. case GRUB_BTRFS_EXTENT_INLINE:
  1457. if (data->extent->compression == GRUB_BTRFS_COMPRESSION_ZLIB)
  1458. {
  1459. if (grub_zlib_decompress (data->extent->inl, data->extsize -
  1460. ((grub_uint8_t *) data->extent->inl
  1461. - (grub_uint8_t *) data->extent),
  1462. extoff, buf, csize)
  1463. != (grub_ssize_t) csize)
  1464. {
  1465. if (!grub_errno)
  1466. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  1467. "premature end of compressed");
  1468. return -1;
  1469. }
  1470. }
  1471. else if (data->extent->compression == GRUB_BTRFS_COMPRESSION_LZO)
  1472. {
  1473. if (grub_btrfs_lzo_decompress(data->extent->inl, data->extsize -
  1474. ((grub_uint8_t *) data->extent->inl
  1475. - (grub_uint8_t *) data->extent),
  1476. extoff, buf, csize)
  1477. != (grub_ssize_t) csize)
  1478. return -1;
  1479. }
  1480. else if (data->extent->compression == GRUB_BTRFS_COMPRESSION_ZSTD)
  1481. {
  1482. if (grub_btrfs_zstd_decompress (data->extent->inl, data->extsize -
  1483. ((grub_uint8_t *) data->extent->inl
  1484. - (grub_uint8_t *) data->extent),
  1485. extoff, buf, csize)
  1486. != (grub_ssize_t) csize)
  1487. return -1;
  1488. }
  1489. else
  1490. grub_memcpy (buf, data->extent->inl + extoff, csize);
  1491. break;
  1492. case GRUB_BTRFS_EXTENT_REGULAR:
  1493. if (!data->extent->laddr)
  1494. {
  1495. grub_memset (buf, 0, csize);
  1496. break;
  1497. }
  1498. if (data->extent->compression != GRUB_BTRFS_COMPRESSION_NONE)
  1499. {
  1500. char *tmp;
  1501. grub_uint64_t zsize;
  1502. grub_ssize_t ret;
  1503. zsize = grub_le_to_cpu64 (data->extent->compressed_size);
  1504. tmp = grub_malloc (zsize);
  1505. if (!tmp)
  1506. return -1;
  1507. err = grub_btrfs_read_logical (data,
  1508. grub_le_to_cpu64 (data->extent->laddr),
  1509. tmp, zsize, 0);
  1510. if (err)
  1511. {
  1512. grub_free (tmp);
  1513. return -1;
  1514. }
  1515. if (data->extent->compression == GRUB_BTRFS_COMPRESSION_ZLIB)
  1516. ret = grub_zlib_decompress (tmp, zsize, extoff
  1517. + grub_le_to_cpu64 (data->extent->offset),
  1518. buf, csize);
  1519. else if (data->extent->compression == GRUB_BTRFS_COMPRESSION_LZO)
  1520. ret = grub_btrfs_lzo_decompress (tmp, zsize, extoff
  1521. + grub_le_to_cpu64 (data->extent->offset),
  1522. buf, csize);
  1523. else if (data->extent->compression == GRUB_BTRFS_COMPRESSION_ZSTD)
  1524. ret = grub_btrfs_zstd_decompress (tmp, zsize, extoff
  1525. + grub_le_to_cpu64 (data->extent->offset),
  1526. buf, csize);
  1527. else
  1528. ret = -1;
  1529. grub_free (tmp);
  1530. if (ret != (grub_ssize_t) csize)
  1531. {
  1532. if (!grub_errno)
  1533. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  1534. "premature end of compressed");
  1535. return -1;
  1536. }
  1537. break;
  1538. }
  1539. err = grub_btrfs_read_logical (data,
  1540. grub_le_to_cpu64 (data->extent->laddr)
  1541. + grub_le_to_cpu64 (data->extent->offset)
  1542. + extoff, buf, csize, 0);
  1543. if (err)
  1544. return -1;
  1545. break;
  1546. default:
  1547. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1548. "unsupported extent type 0x%x", data->extent->type);
  1549. return -1;
  1550. }
  1551. buf += csize;
  1552. pos += csize;
  1553. len -= csize;
  1554. }
  1555. return pos - pos0;
  1556. }
  1557. static grub_err_t
  1558. get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
  1559. grub_uint64_t *tree, grub_uint8_t *type)
  1560. {
  1561. grub_err_t err;
  1562. grub_disk_addr_t elemaddr;
  1563. grub_size_t elemsize;
  1564. struct grub_btrfs_key key_out, key_in;
  1565. struct grub_btrfs_root_item ri;
  1566. key_in.object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_ROOT_VOL_OBJECTID);
  1567. key_in.offset = 0;
  1568. key_in.type = GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM;
  1569. err = lower_bound (data, &key_in, &key_out,
  1570. data->sblock.root_tree,
  1571. &elemaddr, &elemsize, NULL, 0);
  1572. if (err)
  1573. return err;
  1574. if (key_in.object_id != key_out.object_id
  1575. || key_in.type != key_out.type
  1576. || key_in.offset != key_out.offset)
  1577. return grub_error (GRUB_ERR_BAD_FS, "no root");
  1578. err = grub_btrfs_read_logical (data, elemaddr, &ri,
  1579. sizeof (ri), 0);
  1580. if (err)
  1581. return err;
  1582. key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
  1583. key->offset = 0;
  1584. key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
  1585. *tree = ri.tree;
  1586. *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
  1587. return GRUB_ERR_NONE;
  1588. }
  1589. static grub_err_t
  1590. find_path (struct grub_btrfs_data *data,
  1591. const char *path, struct grub_btrfs_key *key,
  1592. grub_uint64_t *tree, grub_uint8_t *type)
  1593. {
  1594. const char *slash = path;
  1595. grub_err_t err;
  1596. grub_disk_addr_t elemaddr;
  1597. grub_size_t elemsize;
  1598. grub_size_t allocated = 0;
  1599. struct grub_btrfs_dir_item *direl = NULL;
  1600. struct grub_btrfs_key key_out;
  1601. const char *ctoken;
  1602. grub_size_t ctokenlen;
  1603. char *path_alloc = NULL;
  1604. char *origpath = NULL;
  1605. unsigned symlinks_max = 32;
  1606. err = get_root (data, key, tree, type);
  1607. if (err)
  1608. return err;
  1609. origpath = grub_strdup (path);
  1610. if (!origpath)
  1611. return grub_errno;
  1612. while (1)
  1613. {
  1614. while (path[0] == '/')
  1615. path++;
  1616. if (!path[0])
  1617. break;
  1618. slash = grub_strchr (path, '/');
  1619. if (!slash)
  1620. slash = path + grub_strlen (path);
  1621. ctoken = path;
  1622. ctokenlen = slash - path;
  1623. if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
  1624. {
  1625. grub_free (path_alloc);
  1626. grub_free (origpath);
  1627. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  1628. }
  1629. if (ctokenlen == 1 && ctoken[0] == '.')
  1630. {
  1631. path = slash;
  1632. continue;
  1633. }
  1634. if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
  1635. {
  1636. key->type = GRUB_BTRFS_ITEM_TYPE_INODE_REF;
  1637. key->offset = -1;
  1638. err = lower_bound (data, key, &key_out, *tree, &elemaddr, &elemsize,
  1639. NULL, 0);
  1640. if (err)
  1641. {
  1642. grub_free (direl);
  1643. grub_free (path_alloc);
  1644. grub_free (origpath);
  1645. return err;
  1646. }
  1647. if (key_out.type != key->type
  1648. || key->object_id != key_out.object_id)
  1649. {
  1650. grub_free (direl);
  1651. grub_free (path_alloc);
  1652. err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), origpath);
  1653. grub_free (origpath);
  1654. return err;
  1655. }
  1656. *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
  1657. key->object_id = key_out.offset;
  1658. path = slash;
  1659. continue;
  1660. }
  1661. key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
  1662. key->offset = grub_cpu_to_le64 (~grub_getcrc32c (1, ctoken, ctokenlen));
  1663. err = lower_bound (data, key, &key_out, *tree, &elemaddr, &elemsize,
  1664. NULL, 0);
  1665. if (err)
  1666. {
  1667. grub_free (direl);
  1668. grub_free (path_alloc);
  1669. grub_free (origpath);
  1670. return err;
  1671. }
  1672. if (key_cmp (key, &key_out) != 0)
  1673. {
  1674. grub_free (direl);
  1675. grub_free (path_alloc);
  1676. err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), origpath);
  1677. grub_free (origpath);
  1678. return err;
  1679. }
  1680. struct grub_btrfs_dir_item *cdirel;
  1681. if (elemsize > allocated)
  1682. {
  1683. allocated = 2 * elemsize;
  1684. grub_free (direl);
  1685. direl = grub_malloc (allocated + 1);
  1686. if (!direl)
  1687. {
  1688. grub_free (path_alloc);
  1689. grub_free (origpath);
  1690. return grub_errno;
  1691. }
  1692. }
  1693. err = grub_btrfs_read_logical (data, elemaddr, direl, elemsize, 0);
  1694. if (err)
  1695. {
  1696. grub_free (direl);
  1697. grub_free (path_alloc);
  1698. grub_free (origpath);
  1699. return err;
  1700. }
  1701. for (cdirel = direl;
  1702. (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
  1703. < (grub_ssize_t) elemsize;
  1704. cdirel = (void *) ((grub_uint8_t *) (direl + 1)
  1705. + grub_le_to_cpu16 (cdirel->n)
  1706. + grub_le_to_cpu16 (cdirel->m)))
  1707. {
  1708. if (ctokenlen == grub_le_to_cpu16 (cdirel->n)
  1709. && grub_memcmp (cdirel->name, ctoken, ctokenlen) == 0)
  1710. break;
  1711. }
  1712. if ((grub_uint8_t *) cdirel - (grub_uint8_t *) direl
  1713. >= (grub_ssize_t) elemsize)
  1714. {
  1715. grub_free (direl);
  1716. grub_free (path_alloc);
  1717. err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), origpath);
  1718. grub_free (origpath);
  1719. return err;
  1720. }
  1721. path = slash;
  1722. if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
  1723. {
  1724. struct grub_btrfs_inode inode;
  1725. char *tmp;
  1726. if (--symlinks_max == 0)
  1727. {
  1728. grub_free (direl);
  1729. grub_free (path_alloc);
  1730. grub_free (origpath);
  1731. return grub_error (GRUB_ERR_SYMLINK_LOOP,
  1732. N_("too deep nesting of symlinks"));
  1733. }
  1734. err = grub_btrfs_read_inode (data, &inode,
  1735. cdirel->key.object_id, *tree);
  1736. if (err)
  1737. {
  1738. grub_free (direl);
  1739. grub_free (path_alloc);
  1740. grub_free (origpath);
  1741. return err;
  1742. }
  1743. tmp = grub_malloc (grub_le_to_cpu64 (inode.size)
  1744. + grub_strlen (path) + 1);
  1745. if (!tmp)
  1746. {
  1747. grub_free (direl);
  1748. grub_free (path_alloc);
  1749. grub_free (origpath);
  1750. return grub_errno;
  1751. }
  1752. if (grub_btrfs_extent_read (data, cdirel->key.object_id,
  1753. *tree, 0, tmp,
  1754. grub_le_to_cpu64 (inode.size))
  1755. != (grub_ssize_t) grub_le_to_cpu64 (inode.size))
  1756. {
  1757. grub_free (direl);
  1758. grub_free (path_alloc);
  1759. grub_free (origpath);
  1760. grub_free (tmp);
  1761. return grub_errno;
  1762. }
  1763. grub_memcpy (tmp + grub_le_to_cpu64 (inode.size), path,
  1764. grub_strlen (path) + 1);
  1765. grub_free (path_alloc);
  1766. path = path_alloc = tmp;
  1767. if (path[0] == '/')
  1768. {
  1769. err = get_root (data, key, tree, type);
  1770. if (err)
  1771. {
  1772. grub_free (direl);
  1773. grub_free (path_alloc);
  1774. grub_free (origpath);
  1775. return err;
  1776. }
  1777. }
  1778. continue;
  1779. }
  1780. *type = cdirel->type;
  1781. switch (cdirel->key.type)
  1782. {
  1783. case GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM:
  1784. {
  1785. struct grub_btrfs_root_item ri;
  1786. err = lower_bound (data, &cdirel->key, &key_out,
  1787. data->sblock.root_tree,
  1788. &elemaddr, &elemsize, NULL, 0);
  1789. if (err)
  1790. {
  1791. grub_free (direl);
  1792. grub_free (path_alloc);
  1793. grub_free (origpath);
  1794. return err;
  1795. }
  1796. if (cdirel->key.object_id != key_out.object_id
  1797. || cdirel->key.type != key_out.type)
  1798. {
  1799. grub_free (direl);
  1800. grub_free (path_alloc);
  1801. err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), origpath);
  1802. grub_free (origpath);
  1803. return err;
  1804. }
  1805. err = grub_btrfs_read_logical (data, elemaddr, &ri,
  1806. sizeof (ri), 0);
  1807. if (err)
  1808. {
  1809. grub_free (direl);
  1810. grub_free (path_alloc);
  1811. grub_free (origpath);
  1812. return err;
  1813. }
  1814. key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
  1815. key->offset = 0;
  1816. key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
  1817. *tree = ri.tree;
  1818. break;
  1819. }
  1820. case GRUB_BTRFS_ITEM_TYPE_INODE_ITEM:
  1821. if (*slash && *type == GRUB_BTRFS_DIR_ITEM_TYPE_REGULAR)
  1822. {
  1823. grub_free (direl);
  1824. grub_free (path_alloc);
  1825. err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), origpath);
  1826. grub_free (origpath);
  1827. return err;
  1828. }
  1829. *key = cdirel->key;
  1830. if (*type == GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
  1831. key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
  1832. break;
  1833. default:
  1834. grub_free (path_alloc);
  1835. grub_free (origpath);
  1836. grub_free (direl);
  1837. return grub_error (GRUB_ERR_BAD_FS, "unrecognised object type 0x%x",
  1838. cdirel->key.type);
  1839. }
  1840. }
  1841. grub_free (direl);
  1842. grub_free (origpath);
  1843. grub_free (path_alloc);
  1844. return GRUB_ERR_NONE;
  1845. }
  1846. static grub_err_t
  1847. grub_btrfs_dir (grub_device_t device, const char *path,
  1848. grub_fs_dir_hook_t hook, void *hook_data)
  1849. {
  1850. struct grub_btrfs_data *data = grub_btrfs_mount (device);
  1851. struct grub_btrfs_key key_in, key_out;
  1852. grub_err_t err;
  1853. grub_disk_addr_t elemaddr;
  1854. grub_size_t elemsize;
  1855. grub_size_t allocated = 0;
  1856. struct grub_btrfs_dir_item *direl = NULL;
  1857. struct grub_btrfs_leaf_descriptor desc;
  1858. int r = 0;
  1859. grub_uint64_t tree;
  1860. grub_uint8_t type;
  1861. grub_size_t est_size = 0;
  1862. if (!data)
  1863. return grub_errno;
  1864. err = find_path (data, path, &key_in, &tree, &type);
  1865. if (err)
  1866. {
  1867. grub_btrfs_unmount (data);
  1868. return err;
  1869. }
  1870. if (type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
  1871. {
  1872. grub_btrfs_unmount (data);
  1873. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  1874. }
  1875. err = lower_bound (data, &key_in, &key_out, tree,
  1876. &elemaddr, &elemsize, &desc, 0);
  1877. if (err)
  1878. {
  1879. grub_btrfs_unmount (data);
  1880. return err;
  1881. }
  1882. if (key_out.type != GRUB_BTRFS_ITEM_TYPE_DIR_ITEM
  1883. || key_out.object_id != key_in.object_id)
  1884. {
  1885. r = next (data, &desc, &elemaddr, &elemsize, &key_out);
  1886. if (r <= 0)
  1887. goto out;
  1888. }
  1889. do
  1890. {
  1891. struct grub_btrfs_dir_item *cdirel;
  1892. if (key_out.type != GRUB_BTRFS_ITEM_TYPE_DIR_ITEM
  1893. || key_out.object_id != key_in.object_id)
  1894. {
  1895. r = 0;
  1896. break;
  1897. }
  1898. if (elemsize > allocated)
  1899. {
  1900. allocated = 2 * elemsize;
  1901. grub_free (direl);
  1902. direl = grub_malloc (allocated + 1);
  1903. if (!direl)
  1904. {
  1905. r = -grub_errno;
  1906. break;
  1907. }
  1908. }
  1909. err = grub_btrfs_read_logical (data, elemaddr, direl, elemsize, 0);
  1910. if (err)
  1911. {
  1912. r = -err;
  1913. break;
  1914. }
  1915. if (direl == NULL ||
  1916. grub_add (grub_le_to_cpu16 (direl->n),
  1917. grub_le_to_cpu16 (direl->m), &est_size) ||
  1918. grub_add (est_size, sizeof (*direl), &est_size) ||
  1919. grub_sub (est_size, sizeof (direl->name), &est_size) ||
  1920. est_size > allocated)
  1921. {
  1922. grub_errno = GRUB_ERR_OUT_OF_RANGE;
  1923. r = -grub_errno;
  1924. goto out;
  1925. }
  1926. for (cdirel = direl;
  1927. (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
  1928. < (grub_ssize_t) elemsize;
  1929. cdirel = (void *) ((grub_uint8_t *) (direl + 1)
  1930. + grub_le_to_cpu16 (cdirel->n)
  1931. + grub_le_to_cpu16 (cdirel->m)))
  1932. {
  1933. char c;
  1934. struct grub_btrfs_inode inode;
  1935. struct grub_dirhook_info info;
  1936. if (cdirel == NULL ||
  1937. grub_add (grub_le_to_cpu16 (cdirel->n),
  1938. grub_le_to_cpu16 (cdirel->m), &est_size) ||
  1939. grub_add (est_size, sizeof (*cdirel), &est_size) ||
  1940. grub_sub (est_size, sizeof (cdirel->name), &est_size) ||
  1941. est_size > allocated)
  1942. {
  1943. grub_errno = GRUB_ERR_OUT_OF_RANGE;
  1944. r = -grub_errno;
  1945. goto out;
  1946. }
  1947. err = grub_btrfs_read_inode (data, &inode, cdirel->key.object_id,
  1948. tree);
  1949. grub_memset (&info, 0, sizeof (info));
  1950. if (err)
  1951. grub_errno = GRUB_ERR_NONE;
  1952. else
  1953. {
  1954. info.mtime = grub_le_to_cpu64 (inode.mtime.sec);
  1955. info.mtimeset = 1;
  1956. }
  1957. c = cdirel->name[grub_le_to_cpu16 (cdirel->n)];
  1958. cdirel->name[grub_le_to_cpu16 (cdirel->n)] = 0;
  1959. info.dir = (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY);
  1960. if (hook (cdirel->name, &info, hook_data))
  1961. goto out;
  1962. cdirel->name[grub_le_to_cpu16 (cdirel->n)] = c;
  1963. }
  1964. r = next (data, &desc, &elemaddr, &elemsize, &key_out);
  1965. }
  1966. while (r > 0);
  1967. out:
  1968. grub_free (direl);
  1969. free_iterator (&desc);
  1970. grub_btrfs_unmount (data);
  1971. return -r;
  1972. }
  1973. static grub_err_t
  1974. grub_btrfs_open (struct grub_file *file, const char *name)
  1975. {
  1976. struct grub_btrfs_data *data = grub_btrfs_mount (file->device);
  1977. grub_err_t err;
  1978. struct grub_btrfs_inode inode;
  1979. grub_uint8_t type;
  1980. struct grub_btrfs_key key_in;
  1981. if (!data)
  1982. return grub_errno;
  1983. err = find_path (data, name, &key_in, &data->tree, &type);
  1984. if (err)
  1985. {
  1986. grub_btrfs_unmount (data);
  1987. return err;
  1988. }
  1989. if (type != GRUB_BTRFS_DIR_ITEM_TYPE_REGULAR)
  1990. {
  1991. grub_btrfs_unmount (data);
  1992. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
  1993. }
  1994. data->inode = key_in.object_id;
  1995. err = grub_btrfs_read_inode (data, &inode, data->inode, data->tree);
  1996. if (err)
  1997. {
  1998. grub_btrfs_unmount (data);
  1999. return err;
  2000. }
  2001. file->data = data;
  2002. file->size = grub_le_to_cpu64 (inode.size);
  2003. return err;
  2004. }
  2005. static grub_err_t
  2006. grub_btrfs_close (grub_file_t file)
  2007. {
  2008. grub_btrfs_unmount (file->data);
  2009. return GRUB_ERR_NONE;
  2010. }
  2011. static grub_ssize_t
  2012. grub_btrfs_read (grub_file_t file, char *buf, grub_size_t len)
  2013. {
  2014. struct grub_btrfs_data *data = file->data;
  2015. return grub_btrfs_extent_read (data, data->inode,
  2016. data->tree, file->offset, buf, len);
  2017. }
  2018. static grub_err_t
  2019. grub_btrfs_uuid (grub_device_t device, char **uuid)
  2020. {
  2021. struct grub_btrfs_data *data;
  2022. *uuid = NULL;
  2023. data = grub_btrfs_mount (device);
  2024. if (!data)
  2025. return grub_errno;
  2026. *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  2027. grub_be_to_cpu16 (data->sblock.uuid[0]),
  2028. grub_be_to_cpu16 (data->sblock.uuid[1]),
  2029. grub_be_to_cpu16 (data->sblock.uuid[2]),
  2030. grub_be_to_cpu16 (data->sblock.uuid[3]),
  2031. grub_be_to_cpu16 (data->sblock.uuid[4]),
  2032. grub_be_to_cpu16 (data->sblock.uuid[5]),
  2033. grub_be_to_cpu16 (data->sblock.uuid[6]),
  2034. grub_be_to_cpu16 (data->sblock.uuid[7]));
  2035. grub_btrfs_unmount (data);
  2036. return grub_errno;
  2037. }
  2038. static grub_err_t
  2039. grub_btrfs_label (grub_device_t device, char **label)
  2040. {
  2041. struct grub_btrfs_data *data;
  2042. *label = NULL;
  2043. data = grub_btrfs_mount (device);
  2044. if (!data)
  2045. return grub_errno;
  2046. *label = grub_strndup (data->sblock.label, sizeof (data->sblock.label));
  2047. grub_btrfs_unmount (data);
  2048. return grub_errno;
  2049. }
  2050. #ifdef GRUB_UTIL
  2051. struct embed_region {
  2052. unsigned int start;
  2053. unsigned int secs;
  2054. };
  2055. /*
  2056. * https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs(5)#BOOTLOADER_SUPPORT
  2057. * The first 1 MiB on each device is unused with the exception of primary
  2058. * superblock that is on the offset 64 KiB and spans 4 KiB.
  2059. */
  2060. static const struct {
  2061. struct embed_region available;
  2062. struct embed_region used[6];
  2063. } btrfs_head = {
  2064. .available = {0, GRUB_DISK_KiB_TO_SECTORS (1024)}, /* The first 1 MiB. */
  2065. .used = {
  2066. {0, 1}, /* boot.S. */
  2067. {GRUB_DISK_KiB_TO_SECTORS (64) - 1, 1}, /* Overflow guard. */
  2068. {GRUB_DISK_KiB_TO_SECTORS (64), GRUB_DISK_KiB_TO_SECTORS (4)}, /* 4 KiB superblock. */
  2069. {GRUB_DISK_KiB_TO_SECTORS (68), 1}, /* Overflow guard. */
  2070. {GRUB_DISK_KiB_TO_SECTORS (1024) - 1, 1}, /* Overflow guard. */
  2071. {0, 0} /* Array terminator. */
  2072. }
  2073. };
  2074. static grub_err_t
  2075. grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
  2076. unsigned int *nsectors,
  2077. unsigned int max_nsectors,
  2078. grub_embed_type_t embed_type,
  2079. grub_disk_addr_t **sectors)
  2080. {
  2081. unsigned int i, j, n = 0;
  2082. const struct embed_region *u;
  2083. grub_disk_addr_t *map;
  2084. if (embed_type != GRUB_EMBED_PCBIOS)
  2085. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  2086. "BtrFS currently supports only PC-BIOS embedding");
  2087. map = grub_calloc (btrfs_head.available.secs, sizeof (*map));
  2088. if (map == NULL)
  2089. return grub_errno;
  2090. /*
  2091. * Populating the map array so that it can be used to index if a disk
  2092. * address is available to embed:
  2093. * - 0: available,
  2094. * - 1: unavailable.
  2095. */
  2096. for (u = btrfs_head.used; u->secs; ++u)
  2097. {
  2098. unsigned int end = u->start + u->secs;
  2099. if (end > btrfs_head.available.secs)
  2100. end = btrfs_head.available.secs;
  2101. for (i = u->start; i < end; ++i)
  2102. map[i] = 1;
  2103. }
  2104. /* Adding up n until it matches total size of available embedding area. */
  2105. for (i = 0; i < btrfs_head.available.secs; ++i)
  2106. if (map[i] == 0)
  2107. n++;
  2108. if (n < *nsectors)
  2109. {
  2110. grub_free (map);
  2111. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  2112. N_("your core.img is unusually large. "
  2113. "It won't fit in the embedding area"));
  2114. }
  2115. if (n > max_nsectors)
  2116. n = max_nsectors;
  2117. /*
  2118. * Populating the array so that it can used to index disk block address for
  2119. * an image file's offset to be embedded on disk (the unit is in sectors):
  2120. * - i: The disk block address relative to btrfs_head.available.start,
  2121. * - j: The offset in image file.
  2122. */
  2123. for (i = 0, j = 0; i < btrfs_head.available.secs && j < n; ++i)
  2124. if (map[i] == 0)
  2125. map[j++] = btrfs_head.available.start + i;
  2126. *nsectors = n;
  2127. *sectors = map;
  2128. return GRUB_ERR_NONE;
  2129. }
  2130. #endif
  2131. static struct grub_fs grub_btrfs_fs = {
  2132. .name = "btrfs",
  2133. .fs_dir = grub_btrfs_dir,
  2134. .fs_open = grub_btrfs_open,
  2135. .fs_read = grub_btrfs_read,
  2136. .fs_close = grub_btrfs_close,
  2137. .fs_uuid = grub_btrfs_uuid,
  2138. .fs_label = grub_btrfs_label,
  2139. #ifdef GRUB_UTIL
  2140. .fs_embed = grub_btrfs_embed,
  2141. .reserved_first_sector = 1,
  2142. .blocklist_install = 0,
  2143. #endif
  2144. };
  2145. GRUB_MOD_INIT (btrfs)
  2146. {
  2147. grub_fs_register (&grub_btrfs_fs);
  2148. }
  2149. GRUB_MOD_FINI (btrfs)
  2150. {
  2151. grub_fs_unregister (&grub_btrfs_fs);
  2152. }