tree-checker.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Qu Wenruo 2017. All rights reserved.
  4. */
  5. /*
  6. * The module is used to catch unexpected/corrupted tree block data.
  7. * Such behavior can be caused either by a fuzzed image or bugs.
  8. *
  9. * The objective is to do leaf/node validation checks when tree block is read
  10. * from disk, and check *every* possible member, so other code won't
  11. * need to checking them again.
  12. *
  13. * Due to the potential and unwanted damage, every checker needs to be
  14. * carefully reviewed otherwise so it does not prevent mount of valid images.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/stddef.h>
  18. #include <linux/error-injection.h>
  19. #include "ctree.h"
  20. #include "tree-checker.h"
  21. #include "disk-io.h"
  22. #include "compression.h"
  23. #include "volumes.h"
  24. #include "misc.h"
  25. #include "btrfs_inode.h"
  26. /*
  27. * Error message should follow the following format:
  28. * corrupt <type>: <identifier>, <reason>[, <bad_value>]
  29. *
  30. * @type: leaf or node
  31. * @identifier: the necessary info to locate the leaf/node.
  32. * It's recommended to decode key.objecitd/offset if it's
  33. * meaningful.
  34. * @reason: describe the error
  35. * @bad_value: optional, it's recommended to output bad value and its
  36. * expected value (range).
  37. *
  38. * Since comma is used to separate the components, only space is allowed
  39. * inside each component.
  40. */
  41. /*
  42. * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
  43. * Allows callers to customize the output.
  44. */
  45. __printf(3, 4)
  46. __cold
  47. static void generic_err(const struct extent_buffer *eb, int slot,
  48. const char *fmt, ...)
  49. {
  50. const struct btrfs_fs_info *fs_info = eb->fs_info;
  51. struct va_format vaf;
  52. va_list args;
  53. va_start(args, fmt);
  54. vaf.fmt = fmt;
  55. vaf.va = &args;
  56. btrfs_crit(fs_info,
  57. "corrupt %s: root=%llu block=%llu slot=%d, %pV",
  58. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  59. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
  60. va_end(args);
  61. }
  62. /*
  63. * Customized reporter for extent data item, since its key objectid and
  64. * offset has its own meaning.
  65. */
  66. __printf(3, 4)
  67. __cold
  68. static void file_extent_err(const struct extent_buffer *eb, int slot,
  69. const char *fmt, ...)
  70. {
  71. const struct btrfs_fs_info *fs_info = eb->fs_info;
  72. struct btrfs_key key;
  73. struct va_format vaf;
  74. va_list args;
  75. btrfs_item_key_to_cpu(eb, &key, slot);
  76. va_start(args, fmt);
  77. vaf.fmt = fmt;
  78. vaf.va = &args;
  79. btrfs_crit(fs_info,
  80. "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
  81. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  82. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  83. key.objectid, key.offset, &vaf);
  84. va_end(args);
  85. }
  86. /*
  87. * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
  88. * Else return 1
  89. */
  90. #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
  91. ({ \
  92. if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), \
  93. (alignment)))) \
  94. file_extent_err((leaf), (slot), \
  95. "invalid %s for file extent, have %llu, should be aligned to %u", \
  96. (#name), btrfs_file_extent_##name((leaf), (fi)), \
  97. (alignment)); \
  98. (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
  99. })
  100. static u64 file_extent_end(struct extent_buffer *leaf,
  101. struct btrfs_key *key,
  102. struct btrfs_file_extent_item *extent)
  103. {
  104. u64 end;
  105. u64 len;
  106. if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
  107. len = btrfs_file_extent_ram_bytes(leaf, extent);
  108. end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
  109. } else {
  110. len = btrfs_file_extent_num_bytes(leaf, extent);
  111. end = key->offset + len;
  112. }
  113. return end;
  114. }
  115. /*
  116. * Customized report for dir_item, the only new important information is
  117. * key->objectid, which represents inode number
  118. */
  119. __printf(3, 4)
  120. __cold
  121. static void dir_item_err(const struct extent_buffer *eb, int slot,
  122. const char *fmt, ...)
  123. {
  124. const struct btrfs_fs_info *fs_info = eb->fs_info;
  125. struct btrfs_key key;
  126. struct va_format vaf;
  127. va_list args;
  128. btrfs_item_key_to_cpu(eb, &key, slot);
  129. va_start(args, fmt);
  130. vaf.fmt = fmt;
  131. vaf.va = &args;
  132. btrfs_crit(fs_info,
  133. "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
  134. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  135. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  136. key.objectid, &vaf);
  137. va_end(args);
  138. }
  139. /*
  140. * This functions checks prev_key->objectid, to ensure current key and prev_key
  141. * share the same objectid as inode number.
  142. *
  143. * This is to detect missing INODE_ITEM in subvolume trees.
  144. *
  145. * Return true if everything is OK or we don't need to check.
  146. * Return false if anything is wrong.
  147. */
  148. static bool check_prev_ino(struct extent_buffer *leaf,
  149. struct btrfs_key *key, int slot,
  150. struct btrfs_key *prev_key)
  151. {
  152. /* No prev key, skip check */
  153. if (slot == 0)
  154. return true;
  155. /* Only these key->types needs to be checked */
  156. ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
  157. key->type == BTRFS_INODE_REF_KEY ||
  158. key->type == BTRFS_DIR_INDEX_KEY ||
  159. key->type == BTRFS_DIR_ITEM_KEY ||
  160. key->type == BTRFS_EXTENT_DATA_KEY);
  161. /*
  162. * Only subvolume trees along with their reloc trees need this check.
  163. * Things like log tree doesn't follow this ino requirement.
  164. */
  165. if (!is_fstree(btrfs_header_owner(leaf)))
  166. return true;
  167. if (key->objectid == prev_key->objectid)
  168. return true;
  169. /* Error found */
  170. dir_item_err(leaf, slot,
  171. "invalid previous key objectid, have %llu expect %llu",
  172. prev_key->objectid, key->objectid);
  173. return false;
  174. }
  175. static int check_extent_data_item(struct extent_buffer *leaf,
  176. struct btrfs_key *key, int slot,
  177. struct btrfs_key *prev_key)
  178. {
  179. struct btrfs_fs_info *fs_info = leaf->fs_info;
  180. struct btrfs_file_extent_item *fi;
  181. u32 sectorsize = fs_info->sectorsize;
  182. u32 item_size = btrfs_item_size(leaf, slot);
  183. u64 extent_end;
  184. if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
  185. file_extent_err(leaf, slot,
  186. "unaligned file_offset for file extent, have %llu should be aligned to %u",
  187. key->offset, sectorsize);
  188. return -EUCLEAN;
  189. }
  190. /*
  191. * Previous key must have the same key->objectid (ino).
  192. * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
  193. * But if objectids mismatch, it means we have a missing
  194. * INODE_ITEM.
  195. */
  196. if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
  197. return -EUCLEAN;
  198. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  199. /*
  200. * Make sure the item contains at least inline header, so the file
  201. * extent type is not some garbage.
  202. */
  203. if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
  204. file_extent_err(leaf, slot,
  205. "invalid item size, have %u expect [%zu, %u)",
  206. item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
  207. SZ_4K);
  208. return -EUCLEAN;
  209. }
  210. if (unlikely(btrfs_file_extent_type(leaf, fi) >=
  211. BTRFS_NR_FILE_EXTENT_TYPES)) {
  212. file_extent_err(leaf, slot,
  213. "invalid type for file extent, have %u expect range [0, %u]",
  214. btrfs_file_extent_type(leaf, fi),
  215. BTRFS_NR_FILE_EXTENT_TYPES - 1);
  216. return -EUCLEAN;
  217. }
  218. /*
  219. * Support for new compression/encryption must introduce incompat flag,
  220. * and must be caught in open_ctree().
  221. */
  222. if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
  223. BTRFS_NR_COMPRESS_TYPES)) {
  224. file_extent_err(leaf, slot,
  225. "invalid compression for file extent, have %u expect range [0, %u]",
  226. btrfs_file_extent_compression(leaf, fi),
  227. BTRFS_NR_COMPRESS_TYPES - 1);
  228. return -EUCLEAN;
  229. }
  230. if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
  231. file_extent_err(leaf, slot,
  232. "invalid encryption for file extent, have %u expect 0",
  233. btrfs_file_extent_encryption(leaf, fi));
  234. return -EUCLEAN;
  235. }
  236. if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
  237. /* Inline extent must have 0 as key offset */
  238. if (unlikely(key->offset)) {
  239. file_extent_err(leaf, slot,
  240. "invalid file_offset for inline file extent, have %llu expect 0",
  241. key->offset);
  242. return -EUCLEAN;
  243. }
  244. /* Compressed inline extent has no on-disk size, skip it */
  245. if (btrfs_file_extent_compression(leaf, fi) !=
  246. BTRFS_COMPRESS_NONE)
  247. return 0;
  248. /* Uncompressed inline extent size must match item size */
  249. if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
  250. btrfs_file_extent_ram_bytes(leaf, fi))) {
  251. file_extent_err(leaf, slot,
  252. "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
  253. item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
  254. btrfs_file_extent_ram_bytes(leaf, fi));
  255. return -EUCLEAN;
  256. }
  257. return 0;
  258. }
  259. /* Regular or preallocated extent has fixed item size */
  260. if (unlikely(item_size != sizeof(*fi))) {
  261. file_extent_err(leaf, slot,
  262. "invalid item size for reg/prealloc file extent, have %u expect %zu",
  263. item_size, sizeof(*fi));
  264. return -EUCLEAN;
  265. }
  266. if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
  267. CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
  268. CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
  269. CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
  270. CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
  271. return -EUCLEAN;
  272. /* Catch extent end overflow */
  273. if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
  274. key->offset, &extent_end))) {
  275. file_extent_err(leaf, slot,
  276. "extent end overflow, have file offset %llu extent num bytes %llu",
  277. key->offset,
  278. btrfs_file_extent_num_bytes(leaf, fi));
  279. return -EUCLEAN;
  280. }
  281. /*
  282. * Check that no two consecutive file extent items, in the same leaf,
  283. * present ranges that overlap each other.
  284. */
  285. if (slot > 0 &&
  286. prev_key->objectid == key->objectid &&
  287. prev_key->type == BTRFS_EXTENT_DATA_KEY) {
  288. struct btrfs_file_extent_item *prev_fi;
  289. u64 prev_end;
  290. prev_fi = btrfs_item_ptr(leaf, slot - 1,
  291. struct btrfs_file_extent_item);
  292. prev_end = file_extent_end(leaf, prev_key, prev_fi);
  293. if (unlikely(prev_end > key->offset)) {
  294. file_extent_err(leaf, slot - 1,
  295. "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
  296. prev_end, key->offset);
  297. return -EUCLEAN;
  298. }
  299. }
  300. return 0;
  301. }
  302. static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
  303. int slot, struct btrfs_key *prev_key)
  304. {
  305. struct btrfs_fs_info *fs_info = leaf->fs_info;
  306. u32 sectorsize = fs_info->sectorsize;
  307. const u32 csumsize = fs_info->csum_size;
  308. if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
  309. generic_err(leaf, slot,
  310. "invalid key objectid for csum item, have %llu expect %llu",
  311. key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
  312. return -EUCLEAN;
  313. }
  314. if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
  315. generic_err(leaf, slot,
  316. "unaligned key offset for csum item, have %llu should be aligned to %u",
  317. key->offset, sectorsize);
  318. return -EUCLEAN;
  319. }
  320. if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
  321. generic_err(leaf, slot,
  322. "unaligned item size for csum item, have %u should be aligned to %u",
  323. btrfs_item_size(leaf, slot), csumsize);
  324. return -EUCLEAN;
  325. }
  326. if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
  327. u64 prev_csum_end;
  328. u32 prev_item_size;
  329. prev_item_size = btrfs_item_size(leaf, slot - 1);
  330. prev_csum_end = (prev_item_size / csumsize) * sectorsize;
  331. prev_csum_end += prev_key->offset;
  332. if (unlikely(prev_csum_end > key->offset)) {
  333. generic_err(leaf, slot - 1,
  334. "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
  335. prev_csum_end, key->offset);
  336. return -EUCLEAN;
  337. }
  338. }
  339. return 0;
  340. }
  341. /* Inode item error output has the same format as dir_item_err() */
  342. #define inode_item_err(eb, slot, fmt, ...) \
  343. dir_item_err(eb, slot, fmt, __VA_ARGS__)
  344. static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
  345. int slot)
  346. {
  347. struct btrfs_key item_key;
  348. bool is_inode_item;
  349. btrfs_item_key_to_cpu(leaf, &item_key, slot);
  350. is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
  351. /* For XATTR_ITEM, location key should be all 0 */
  352. if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
  353. if (unlikely(key->objectid != 0 || key->type != 0 ||
  354. key->offset != 0))
  355. return -EUCLEAN;
  356. return 0;
  357. }
  358. if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
  359. key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
  360. key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
  361. key->objectid != BTRFS_FREE_INO_OBJECTID)) {
  362. if (is_inode_item) {
  363. generic_err(leaf, slot,
  364. "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
  365. key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
  366. BTRFS_FIRST_FREE_OBJECTID,
  367. BTRFS_LAST_FREE_OBJECTID,
  368. BTRFS_FREE_INO_OBJECTID);
  369. } else {
  370. dir_item_err(leaf, slot,
  371. "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
  372. key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
  373. BTRFS_FIRST_FREE_OBJECTID,
  374. BTRFS_LAST_FREE_OBJECTID,
  375. BTRFS_FREE_INO_OBJECTID);
  376. }
  377. return -EUCLEAN;
  378. }
  379. if (unlikely(key->offset != 0)) {
  380. if (is_inode_item)
  381. inode_item_err(leaf, slot,
  382. "invalid key offset: has %llu expect 0",
  383. key->offset);
  384. else
  385. dir_item_err(leaf, slot,
  386. "invalid location key offset:has %llu expect 0",
  387. key->offset);
  388. return -EUCLEAN;
  389. }
  390. return 0;
  391. }
  392. static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
  393. int slot)
  394. {
  395. struct btrfs_key item_key;
  396. bool is_root_item;
  397. btrfs_item_key_to_cpu(leaf, &item_key, slot);
  398. is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
  399. /*
  400. * Bad rootid for reloc trees.
  401. *
  402. * Reloc trees are only for subvolume trees, other trees only need
  403. * to be COWed to be relocated.
  404. */
  405. if (unlikely(is_root_item && key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
  406. !is_fstree(key->offset))) {
  407. generic_err(leaf, slot,
  408. "invalid reloc tree for root %lld, root id is not a subvolume tree",
  409. key->offset);
  410. return -EUCLEAN;
  411. }
  412. /* No such tree id */
  413. if (unlikely(key->objectid == 0)) {
  414. if (is_root_item)
  415. generic_err(leaf, slot, "invalid root id 0");
  416. else
  417. dir_item_err(leaf, slot,
  418. "invalid location key root id 0");
  419. return -EUCLEAN;
  420. }
  421. /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
  422. if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
  423. dir_item_err(leaf, slot,
  424. "invalid location key objectid, have %llu expect [%llu, %llu]",
  425. key->objectid, BTRFS_FIRST_FREE_OBJECTID,
  426. BTRFS_LAST_FREE_OBJECTID);
  427. return -EUCLEAN;
  428. }
  429. /*
  430. * ROOT_ITEM with non-zero offset means this is a snapshot, created at
  431. * @offset transid.
  432. * Furthermore, for location key in DIR_ITEM, its offset is always -1.
  433. *
  434. * So here we only check offset for reloc tree whose key->offset must
  435. * be a valid tree.
  436. */
  437. if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
  438. key->offset == 0)) {
  439. generic_err(leaf, slot, "invalid root id 0 for reloc tree");
  440. return -EUCLEAN;
  441. }
  442. return 0;
  443. }
  444. static int check_dir_item(struct extent_buffer *leaf,
  445. struct btrfs_key *key, struct btrfs_key *prev_key,
  446. int slot)
  447. {
  448. struct btrfs_fs_info *fs_info = leaf->fs_info;
  449. struct btrfs_dir_item *di;
  450. u32 item_size = btrfs_item_size(leaf, slot);
  451. u32 cur = 0;
  452. if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
  453. return -EUCLEAN;
  454. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  455. while (cur < item_size) {
  456. struct btrfs_key location_key;
  457. u32 name_len;
  458. u32 data_len;
  459. u32 max_name_len;
  460. u32 total_size;
  461. u32 name_hash;
  462. u8 dir_type;
  463. int ret;
  464. /* header itself should not cross item boundary */
  465. if (unlikely(cur + sizeof(*di) > item_size)) {
  466. dir_item_err(leaf, slot,
  467. "dir item header crosses item boundary, have %zu boundary %u",
  468. cur + sizeof(*di), item_size);
  469. return -EUCLEAN;
  470. }
  471. /* Location key check */
  472. btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
  473. if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
  474. ret = check_root_key(leaf, &location_key, slot);
  475. if (unlikely(ret < 0))
  476. return ret;
  477. } else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
  478. location_key.type == 0) {
  479. ret = check_inode_key(leaf, &location_key, slot);
  480. if (unlikely(ret < 0))
  481. return ret;
  482. } else {
  483. dir_item_err(leaf, slot,
  484. "invalid location key type, have %u, expect %u or %u",
  485. location_key.type, BTRFS_ROOT_ITEM_KEY,
  486. BTRFS_INODE_ITEM_KEY);
  487. return -EUCLEAN;
  488. }
  489. /* dir type check */
  490. dir_type = btrfs_dir_type(leaf, di);
  491. if (unlikely(dir_type >= BTRFS_FT_MAX)) {
  492. dir_item_err(leaf, slot,
  493. "invalid dir item type, have %u expect [0, %u)",
  494. dir_type, BTRFS_FT_MAX);
  495. return -EUCLEAN;
  496. }
  497. if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
  498. dir_type != BTRFS_FT_XATTR)) {
  499. dir_item_err(leaf, slot,
  500. "invalid dir item type for XATTR key, have %u expect %u",
  501. dir_type, BTRFS_FT_XATTR);
  502. return -EUCLEAN;
  503. }
  504. if (unlikely(dir_type == BTRFS_FT_XATTR &&
  505. key->type != BTRFS_XATTR_ITEM_KEY)) {
  506. dir_item_err(leaf, slot,
  507. "xattr dir type found for non-XATTR key");
  508. return -EUCLEAN;
  509. }
  510. if (dir_type == BTRFS_FT_XATTR)
  511. max_name_len = XATTR_NAME_MAX;
  512. else
  513. max_name_len = BTRFS_NAME_LEN;
  514. /* Name/data length check */
  515. name_len = btrfs_dir_name_len(leaf, di);
  516. data_len = btrfs_dir_data_len(leaf, di);
  517. if (unlikely(name_len > max_name_len)) {
  518. dir_item_err(leaf, slot,
  519. "dir item name len too long, have %u max %u",
  520. name_len, max_name_len);
  521. return -EUCLEAN;
  522. }
  523. if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
  524. dir_item_err(leaf, slot,
  525. "dir item name and data len too long, have %u max %u",
  526. name_len + data_len,
  527. BTRFS_MAX_XATTR_SIZE(fs_info));
  528. return -EUCLEAN;
  529. }
  530. if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
  531. dir_item_err(leaf, slot,
  532. "dir item with invalid data len, have %u expect 0",
  533. data_len);
  534. return -EUCLEAN;
  535. }
  536. total_size = sizeof(*di) + name_len + data_len;
  537. /* header and name/data should not cross item boundary */
  538. if (unlikely(cur + total_size > item_size)) {
  539. dir_item_err(leaf, slot,
  540. "dir item data crosses item boundary, have %u boundary %u",
  541. cur + total_size, item_size);
  542. return -EUCLEAN;
  543. }
  544. /*
  545. * Special check for XATTR/DIR_ITEM, as key->offset is name
  546. * hash, should match its name
  547. */
  548. if (key->type == BTRFS_DIR_ITEM_KEY ||
  549. key->type == BTRFS_XATTR_ITEM_KEY) {
  550. char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
  551. read_extent_buffer(leaf, namebuf,
  552. (unsigned long)(di + 1), name_len);
  553. name_hash = btrfs_name_hash(namebuf, name_len);
  554. if (unlikely(key->offset != name_hash)) {
  555. dir_item_err(leaf, slot,
  556. "name hash mismatch with key, have 0x%016x expect 0x%016llx",
  557. name_hash, key->offset);
  558. return -EUCLEAN;
  559. }
  560. }
  561. cur += total_size;
  562. di = (struct btrfs_dir_item *)((void *)di + total_size);
  563. }
  564. return 0;
  565. }
  566. __printf(3, 4)
  567. __cold
  568. static void block_group_err(const struct extent_buffer *eb, int slot,
  569. const char *fmt, ...)
  570. {
  571. const struct btrfs_fs_info *fs_info = eb->fs_info;
  572. struct btrfs_key key;
  573. struct va_format vaf;
  574. va_list args;
  575. btrfs_item_key_to_cpu(eb, &key, slot);
  576. va_start(args, fmt);
  577. vaf.fmt = fmt;
  578. vaf.va = &args;
  579. btrfs_crit(fs_info,
  580. "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
  581. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  582. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  583. key.objectid, key.offset, &vaf);
  584. va_end(args);
  585. }
  586. static int check_block_group_item(struct extent_buffer *leaf,
  587. struct btrfs_key *key, int slot)
  588. {
  589. struct btrfs_fs_info *fs_info = leaf->fs_info;
  590. struct btrfs_block_group_item bgi;
  591. u32 item_size = btrfs_item_size(leaf, slot);
  592. u64 chunk_objectid;
  593. u64 flags;
  594. u64 type;
  595. /*
  596. * Here we don't really care about alignment since extent allocator can
  597. * handle it. We care more about the size.
  598. */
  599. if (unlikely(key->offset == 0)) {
  600. block_group_err(leaf, slot,
  601. "invalid block group size 0");
  602. return -EUCLEAN;
  603. }
  604. if (unlikely(item_size != sizeof(bgi))) {
  605. block_group_err(leaf, slot,
  606. "invalid item size, have %u expect %zu",
  607. item_size, sizeof(bgi));
  608. return -EUCLEAN;
  609. }
  610. read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
  611. sizeof(bgi));
  612. chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
  613. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  614. /*
  615. * We don't init the nr_global_roots until we load the global
  616. * roots, so this could be 0 at mount time. If it's 0 we'll
  617. * just assume we're fine, and later we'll check against our
  618. * actual value.
  619. */
  620. if (unlikely(fs_info->nr_global_roots &&
  621. chunk_objectid >= fs_info->nr_global_roots)) {
  622. block_group_err(leaf, slot,
  623. "invalid block group global root id, have %llu, needs to be <= %llu",
  624. chunk_objectid,
  625. fs_info->nr_global_roots);
  626. return -EUCLEAN;
  627. }
  628. } else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
  629. block_group_err(leaf, slot,
  630. "invalid block group chunk objectid, have %llu expect %llu",
  631. btrfs_stack_block_group_chunk_objectid(&bgi),
  632. BTRFS_FIRST_CHUNK_TREE_OBJECTID);
  633. return -EUCLEAN;
  634. }
  635. if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
  636. block_group_err(leaf, slot,
  637. "invalid block group used, have %llu expect [0, %llu)",
  638. btrfs_stack_block_group_used(&bgi), key->offset);
  639. return -EUCLEAN;
  640. }
  641. flags = btrfs_stack_block_group_flags(&bgi);
  642. if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
  643. block_group_err(leaf, slot,
  644. "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
  645. flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
  646. hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
  647. return -EUCLEAN;
  648. }
  649. type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  650. if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
  651. type != BTRFS_BLOCK_GROUP_METADATA &&
  652. type != BTRFS_BLOCK_GROUP_SYSTEM &&
  653. type != (BTRFS_BLOCK_GROUP_METADATA |
  654. BTRFS_BLOCK_GROUP_DATA))) {
  655. block_group_err(leaf, slot,
  656. "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
  657. type, hweight64(type),
  658. BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
  659. BTRFS_BLOCK_GROUP_SYSTEM,
  660. BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
  661. return -EUCLEAN;
  662. }
  663. return 0;
  664. }
  665. __printf(4, 5)
  666. __cold
  667. static void chunk_err(const struct extent_buffer *leaf,
  668. const struct btrfs_chunk *chunk, u64 logical,
  669. const char *fmt, ...)
  670. {
  671. const struct btrfs_fs_info *fs_info = leaf->fs_info;
  672. bool is_sb;
  673. struct va_format vaf;
  674. va_list args;
  675. int i;
  676. int slot = -1;
  677. /* Only superblock eb is able to have such small offset */
  678. is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
  679. if (!is_sb) {
  680. /*
  681. * Get the slot number by iterating through all slots, this
  682. * would provide better readability.
  683. */
  684. for (i = 0; i < btrfs_header_nritems(leaf); i++) {
  685. if (btrfs_item_ptr_offset(leaf, i) ==
  686. (unsigned long)chunk) {
  687. slot = i;
  688. break;
  689. }
  690. }
  691. }
  692. va_start(args, fmt);
  693. vaf.fmt = fmt;
  694. vaf.va = &args;
  695. if (is_sb)
  696. btrfs_crit(fs_info,
  697. "corrupt superblock syschunk array: chunk_start=%llu, %pV",
  698. logical, &vaf);
  699. else
  700. btrfs_crit(fs_info,
  701. "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
  702. BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
  703. logical, &vaf);
  704. va_end(args);
  705. }
  706. /*
  707. * The common chunk check which could also work on super block sys chunk array.
  708. *
  709. * Return -EUCLEAN if anything is corrupted.
  710. * Return 0 if everything is OK.
  711. */
  712. int btrfs_check_chunk_valid(struct extent_buffer *leaf,
  713. struct btrfs_chunk *chunk, u64 logical)
  714. {
  715. struct btrfs_fs_info *fs_info = leaf->fs_info;
  716. u64 length;
  717. u64 chunk_end;
  718. u64 stripe_len;
  719. u16 num_stripes;
  720. u16 sub_stripes;
  721. u64 type;
  722. u64 features;
  723. bool mixed = false;
  724. int raid_index;
  725. int nparity;
  726. int ncopies;
  727. length = btrfs_chunk_length(leaf, chunk);
  728. stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  729. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  730. sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  731. type = btrfs_chunk_type(leaf, chunk);
  732. raid_index = btrfs_bg_flags_to_raid_index(type);
  733. ncopies = btrfs_raid_array[raid_index].ncopies;
  734. nparity = btrfs_raid_array[raid_index].nparity;
  735. if (unlikely(!num_stripes)) {
  736. chunk_err(leaf, chunk, logical,
  737. "invalid chunk num_stripes, have %u", num_stripes);
  738. return -EUCLEAN;
  739. }
  740. if (unlikely(num_stripes < ncopies)) {
  741. chunk_err(leaf, chunk, logical,
  742. "invalid chunk num_stripes < ncopies, have %u < %d",
  743. num_stripes, ncopies);
  744. return -EUCLEAN;
  745. }
  746. if (unlikely(nparity && num_stripes == nparity)) {
  747. chunk_err(leaf, chunk, logical,
  748. "invalid chunk num_stripes == nparity, have %u == %d",
  749. num_stripes, nparity);
  750. return -EUCLEAN;
  751. }
  752. if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
  753. chunk_err(leaf, chunk, logical,
  754. "invalid chunk logical, have %llu should aligned to %u",
  755. logical, fs_info->sectorsize);
  756. return -EUCLEAN;
  757. }
  758. if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
  759. chunk_err(leaf, chunk, logical,
  760. "invalid chunk sectorsize, have %u expect %u",
  761. btrfs_chunk_sector_size(leaf, chunk),
  762. fs_info->sectorsize);
  763. return -EUCLEAN;
  764. }
  765. if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
  766. chunk_err(leaf, chunk, logical,
  767. "invalid chunk length, have %llu", length);
  768. return -EUCLEAN;
  769. }
  770. if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
  771. chunk_err(leaf, chunk, logical,
  772. "invalid chunk logical start and length, have logical start %llu length %llu",
  773. logical, length);
  774. return -EUCLEAN;
  775. }
  776. if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
  777. chunk_err(leaf, chunk, logical,
  778. "invalid chunk stripe length: %llu",
  779. stripe_len);
  780. return -EUCLEAN;
  781. }
  782. if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  783. BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
  784. chunk_err(leaf, chunk, logical,
  785. "unrecognized chunk type: 0x%llx",
  786. ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
  787. BTRFS_BLOCK_GROUP_PROFILE_MASK) &
  788. btrfs_chunk_type(leaf, chunk));
  789. return -EUCLEAN;
  790. }
  791. if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
  792. (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
  793. chunk_err(leaf, chunk, logical,
  794. "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
  795. type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
  796. return -EUCLEAN;
  797. }
  798. if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
  799. chunk_err(leaf, chunk, logical,
  800. "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
  801. type, BTRFS_BLOCK_GROUP_TYPE_MASK);
  802. return -EUCLEAN;
  803. }
  804. if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
  805. (type & (BTRFS_BLOCK_GROUP_METADATA |
  806. BTRFS_BLOCK_GROUP_DATA)))) {
  807. chunk_err(leaf, chunk, logical,
  808. "system chunk with data or metadata type: 0x%llx",
  809. type);
  810. return -EUCLEAN;
  811. }
  812. features = btrfs_super_incompat_flags(fs_info->super_copy);
  813. if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  814. mixed = true;
  815. if (!mixed) {
  816. if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
  817. (type & BTRFS_BLOCK_GROUP_DATA))) {
  818. chunk_err(leaf, chunk, logical,
  819. "mixed chunk type in non-mixed mode: 0x%llx", type);
  820. return -EUCLEAN;
  821. }
  822. }
  823. if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
  824. sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
  825. (type & BTRFS_BLOCK_GROUP_RAID1 &&
  826. num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
  827. (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
  828. num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
  829. (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
  830. num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
  831. (type & BTRFS_BLOCK_GROUP_RAID5 &&
  832. num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
  833. (type & BTRFS_BLOCK_GROUP_RAID6 &&
  834. num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
  835. (type & BTRFS_BLOCK_GROUP_DUP &&
  836. num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
  837. ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
  838. num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
  839. chunk_err(leaf, chunk, logical,
  840. "invalid num_stripes:sub_stripes %u:%u for profile %llu",
  841. num_stripes, sub_stripes,
  842. type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
  843. return -EUCLEAN;
  844. }
  845. return 0;
  846. }
  847. /*
  848. * Enhanced version of chunk item checker.
  849. *
  850. * The common btrfs_check_chunk_valid() doesn't check item size since it needs
  851. * to work on super block sys_chunk_array which doesn't have full item ptr.
  852. */
  853. static int check_leaf_chunk_item(struct extent_buffer *leaf,
  854. struct btrfs_chunk *chunk,
  855. struct btrfs_key *key, int slot)
  856. {
  857. int num_stripes;
  858. if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
  859. chunk_err(leaf, chunk, key->offset,
  860. "invalid chunk item size: have %u expect [%zu, %u)",
  861. btrfs_item_size(leaf, slot),
  862. sizeof(struct btrfs_chunk),
  863. BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
  864. return -EUCLEAN;
  865. }
  866. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  867. /* Let btrfs_check_chunk_valid() handle this error type */
  868. if (num_stripes == 0)
  869. goto out;
  870. if (unlikely(btrfs_chunk_item_size(num_stripes) !=
  871. btrfs_item_size(leaf, slot))) {
  872. chunk_err(leaf, chunk, key->offset,
  873. "invalid chunk item size: have %u expect %lu",
  874. btrfs_item_size(leaf, slot),
  875. btrfs_chunk_item_size(num_stripes));
  876. return -EUCLEAN;
  877. }
  878. out:
  879. return btrfs_check_chunk_valid(leaf, chunk, key->offset);
  880. }
  881. __printf(3, 4)
  882. __cold
  883. static void dev_item_err(const struct extent_buffer *eb, int slot,
  884. const char *fmt, ...)
  885. {
  886. struct btrfs_key key;
  887. struct va_format vaf;
  888. va_list args;
  889. btrfs_item_key_to_cpu(eb, &key, slot);
  890. va_start(args, fmt);
  891. vaf.fmt = fmt;
  892. vaf.va = &args;
  893. btrfs_crit(eb->fs_info,
  894. "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
  895. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  896. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  897. key.objectid, &vaf);
  898. va_end(args);
  899. }
  900. static int check_dev_item(struct extent_buffer *leaf,
  901. struct btrfs_key *key, int slot)
  902. {
  903. struct btrfs_dev_item *ditem;
  904. const u32 item_size = btrfs_item_size(leaf, slot);
  905. if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
  906. dev_item_err(leaf, slot,
  907. "invalid objectid: has=%llu expect=%llu",
  908. key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
  909. return -EUCLEAN;
  910. }
  911. if (unlikely(item_size != sizeof(*ditem))) {
  912. dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
  913. item_size, sizeof(*ditem));
  914. return -EUCLEAN;
  915. }
  916. ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
  917. if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
  918. dev_item_err(leaf, slot,
  919. "devid mismatch: key has=%llu item has=%llu",
  920. key->offset, btrfs_device_id(leaf, ditem));
  921. return -EUCLEAN;
  922. }
  923. /*
  924. * For device total_bytes, we don't have reliable way to check it, as
  925. * it can be 0 for device removal. Device size check can only be done
  926. * by dev extents check.
  927. */
  928. if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
  929. btrfs_device_total_bytes(leaf, ditem))) {
  930. dev_item_err(leaf, slot,
  931. "invalid bytes used: have %llu expect [0, %llu]",
  932. btrfs_device_bytes_used(leaf, ditem),
  933. btrfs_device_total_bytes(leaf, ditem));
  934. return -EUCLEAN;
  935. }
  936. /*
  937. * Remaining members like io_align/type/gen/dev_group aren't really
  938. * utilized. Skip them to make later usage of them easier.
  939. */
  940. return 0;
  941. }
  942. static int check_inode_item(struct extent_buffer *leaf,
  943. struct btrfs_key *key, int slot)
  944. {
  945. struct btrfs_fs_info *fs_info = leaf->fs_info;
  946. struct btrfs_inode_item *iitem;
  947. u64 super_gen = btrfs_super_generation(fs_info->super_copy);
  948. u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
  949. const u32 item_size = btrfs_item_size(leaf, slot);
  950. u32 mode;
  951. int ret;
  952. u32 flags;
  953. u32 ro_flags;
  954. ret = check_inode_key(leaf, key, slot);
  955. if (unlikely(ret < 0))
  956. return ret;
  957. if (unlikely(item_size != sizeof(*iitem))) {
  958. generic_err(leaf, slot, "invalid item size: has %u expect %zu",
  959. item_size, sizeof(*iitem));
  960. return -EUCLEAN;
  961. }
  962. iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
  963. /* Here we use super block generation + 1 to handle log tree */
  964. if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
  965. inode_item_err(leaf, slot,
  966. "invalid inode generation: has %llu expect (0, %llu]",
  967. btrfs_inode_generation(leaf, iitem),
  968. super_gen + 1);
  969. return -EUCLEAN;
  970. }
  971. /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
  972. if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
  973. inode_item_err(leaf, slot,
  974. "invalid inode transid: has %llu expect [0, %llu]",
  975. btrfs_inode_transid(leaf, iitem), super_gen + 1);
  976. return -EUCLEAN;
  977. }
  978. /*
  979. * For size and nbytes it's better not to be too strict, as for dir
  980. * item its size/nbytes can easily get wrong, but doesn't affect
  981. * anything in the fs. So here we skip the check.
  982. */
  983. mode = btrfs_inode_mode(leaf, iitem);
  984. if (unlikely(mode & ~valid_mask)) {
  985. inode_item_err(leaf, slot,
  986. "unknown mode bit detected: 0x%x",
  987. mode & ~valid_mask);
  988. return -EUCLEAN;
  989. }
  990. /*
  991. * S_IFMT is not bit mapped so we can't completely rely on
  992. * is_power_of_2/has_single_bit_set, but it can save us from checking
  993. * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS
  994. */
  995. if (!has_single_bit_set(mode & S_IFMT)) {
  996. if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
  997. inode_item_err(leaf, slot,
  998. "invalid mode: has 0%o expect valid S_IF* bit(s)",
  999. mode & S_IFMT);
  1000. return -EUCLEAN;
  1001. }
  1002. }
  1003. if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
  1004. inode_item_err(leaf, slot,
  1005. "invalid nlink: has %u expect no more than 1 for dir",
  1006. btrfs_inode_nlink(leaf, iitem));
  1007. return -EUCLEAN;
  1008. }
  1009. btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
  1010. if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
  1011. inode_item_err(leaf, slot,
  1012. "unknown incompat flags detected: 0x%x", flags);
  1013. return -EUCLEAN;
  1014. }
  1015. if (unlikely(!sb_rdonly(fs_info->sb) &&
  1016. (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
  1017. inode_item_err(leaf, slot,
  1018. "unknown ro-compat flags detected on writeable mount: 0x%x",
  1019. ro_flags);
  1020. return -EUCLEAN;
  1021. }
  1022. return 0;
  1023. }
  1024. static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
  1025. int slot)
  1026. {
  1027. struct btrfs_fs_info *fs_info = leaf->fs_info;
  1028. struct btrfs_root_item ri = { 0 };
  1029. const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
  1030. BTRFS_ROOT_SUBVOL_DEAD;
  1031. int ret;
  1032. ret = check_root_key(leaf, key, slot);
  1033. if (unlikely(ret < 0))
  1034. return ret;
  1035. if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
  1036. btrfs_item_size(leaf, slot) !=
  1037. btrfs_legacy_root_item_size())) {
  1038. generic_err(leaf, slot,
  1039. "invalid root item size, have %u expect %zu or %u",
  1040. btrfs_item_size(leaf, slot), sizeof(ri),
  1041. btrfs_legacy_root_item_size());
  1042. return -EUCLEAN;
  1043. }
  1044. /*
  1045. * For legacy root item, the members starting at generation_v2 will be
  1046. * all filled with 0.
  1047. * And since we allow geneartion_v2 as 0, it will still pass the check.
  1048. */
  1049. read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
  1050. btrfs_item_size(leaf, slot));
  1051. /* Generation related */
  1052. if (unlikely(btrfs_root_generation(&ri) >
  1053. btrfs_super_generation(fs_info->super_copy) + 1)) {
  1054. generic_err(leaf, slot,
  1055. "invalid root generation, have %llu expect (0, %llu]",
  1056. btrfs_root_generation(&ri),
  1057. btrfs_super_generation(fs_info->super_copy) + 1);
  1058. return -EUCLEAN;
  1059. }
  1060. if (unlikely(btrfs_root_generation_v2(&ri) >
  1061. btrfs_super_generation(fs_info->super_copy) + 1)) {
  1062. generic_err(leaf, slot,
  1063. "invalid root v2 generation, have %llu expect (0, %llu]",
  1064. btrfs_root_generation_v2(&ri),
  1065. btrfs_super_generation(fs_info->super_copy) + 1);
  1066. return -EUCLEAN;
  1067. }
  1068. if (unlikely(btrfs_root_last_snapshot(&ri) >
  1069. btrfs_super_generation(fs_info->super_copy) + 1)) {
  1070. generic_err(leaf, slot,
  1071. "invalid root last_snapshot, have %llu expect (0, %llu]",
  1072. btrfs_root_last_snapshot(&ri),
  1073. btrfs_super_generation(fs_info->super_copy) + 1);
  1074. return -EUCLEAN;
  1075. }
  1076. /* Alignment and level check */
  1077. if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
  1078. generic_err(leaf, slot,
  1079. "invalid root bytenr, have %llu expect to be aligned to %u",
  1080. btrfs_root_bytenr(&ri), fs_info->sectorsize);
  1081. return -EUCLEAN;
  1082. }
  1083. if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
  1084. generic_err(leaf, slot,
  1085. "invalid root level, have %u expect [0, %u]",
  1086. btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
  1087. return -EUCLEAN;
  1088. }
  1089. if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
  1090. generic_err(leaf, slot,
  1091. "invalid root level, have %u expect [0, %u]",
  1092. btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
  1093. return -EUCLEAN;
  1094. }
  1095. /* Flags check */
  1096. if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
  1097. generic_err(leaf, slot,
  1098. "invalid root flags, have 0x%llx expect mask 0x%llx",
  1099. btrfs_root_flags(&ri), valid_root_flags);
  1100. return -EUCLEAN;
  1101. }
  1102. return 0;
  1103. }
  1104. __printf(3,4)
  1105. __cold
  1106. static void extent_err(const struct extent_buffer *eb, int slot,
  1107. const char *fmt, ...)
  1108. {
  1109. struct btrfs_key key;
  1110. struct va_format vaf;
  1111. va_list args;
  1112. u64 bytenr;
  1113. u64 len;
  1114. btrfs_item_key_to_cpu(eb, &key, slot);
  1115. bytenr = key.objectid;
  1116. if (key.type == BTRFS_METADATA_ITEM_KEY ||
  1117. key.type == BTRFS_TREE_BLOCK_REF_KEY ||
  1118. key.type == BTRFS_SHARED_BLOCK_REF_KEY)
  1119. len = eb->fs_info->nodesize;
  1120. else
  1121. len = key.offset;
  1122. va_start(args, fmt);
  1123. vaf.fmt = fmt;
  1124. vaf.va = &args;
  1125. btrfs_crit(eb->fs_info,
  1126. "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
  1127. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  1128. eb->start, slot, bytenr, len, &vaf);
  1129. va_end(args);
  1130. }
  1131. static int check_extent_item(struct extent_buffer *leaf,
  1132. struct btrfs_key *key, int slot,
  1133. struct btrfs_key *prev_key)
  1134. {
  1135. struct btrfs_fs_info *fs_info = leaf->fs_info;
  1136. struct btrfs_extent_item *ei;
  1137. bool is_tree_block = false;
  1138. unsigned long ptr; /* Current pointer inside inline refs */
  1139. unsigned long end; /* Extent item end */
  1140. const u32 item_size = btrfs_item_size(leaf, slot);
  1141. u64 flags;
  1142. u64 generation;
  1143. u64 total_refs; /* Total refs in btrfs_extent_item */
  1144. u64 inline_refs = 0; /* found total inline refs */
  1145. if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
  1146. !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
  1147. generic_err(leaf, slot,
  1148. "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
  1149. return -EUCLEAN;
  1150. }
  1151. /* key->objectid is the bytenr for both key types */
  1152. if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
  1153. generic_err(leaf, slot,
  1154. "invalid key objectid, have %llu expect to be aligned to %u",
  1155. key->objectid, fs_info->sectorsize);
  1156. return -EUCLEAN;
  1157. }
  1158. /* key->offset is tree level for METADATA_ITEM_KEY */
  1159. if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
  1160. key->offset >= BTRFS_MAX_LEVEL)) {
  1161. extent_err(leaf, slot,
  1162. "invalid tree level, have %llu expect [0, %u]",
  1163. key->offset, BTRFS_MAX_LEVEL - 1);
  1164. return -EUCLEAN;
  1165. }
  1166. /*
  1167. * EXTENT/METADATA_ITEM consists of:
  1168. * 1) One btrfs_extent_item
  1169. * Records the total refs, type and generation of the extent.
  1170. *
  1171. * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
  1172. * Records the first key and level of the tree block.
  1173. *
  1174. * 2) Zero or more btrfs_extent_inline_ref(s)
  1175. * Each inline ref has one btrfs_extent_inline_ref shows:
  1176. * 2.1) The ref type, one of the 4
  1177. * TREE_BLOCK_REF Tree block only
  1178. * SHARED_BLOCK_REF Tree block only
  1179. * EXTENT_DATA_REF Data only
  1180. * SHARED_DATA_REF Data only
  1181. * 2.2) Ref type specific data
  1182. * Either using btrfs_extent_inline_ref::offset, or specific
  1183. * data structure.
  1184. */
  1185. if (unlikely(item_size < sizeof(*ei))) {
  1186. extent_err(leaf, slot,
  1187. "invalid item size, have %u expect [%zu, %u)",
  1188. item_size, sizeof(*ei),
  1189. BTRFS_LEAF_DATA_SIZE(fs_info));
  1190. return -EUCLEAN;
  1191. }
  1192. end = item_size + btrfs_item_ptr_offset(leaf, slot);
  1193. /* Checks against extent_item */
  1194. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  1195. flags = btrfs_extent_flags(leaf, ei);
  1196. total_refs = btrfs_extent_refs(leaf, ei);
  1197. generation = btrfs_extent_generation(leaf, ei);
  1198. if (unlikely(generation >
  1199. btrfs_super_generation(fs_info->super_copy) + 1)) {
  1200. extent_err(leaf, slot,
  1201. "invalid generation, have %llu expect (0, %llu]",
  1202. generation,
  1203. btrfs_super_generation(fs_info->super_copy) + 1);
  1204. return -EUCLEAN;
  1205. }
  1206. if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
  1207. BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
  1208. extent_err(leaf, slot,
  1209. "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
  1210. flags, BTRFS_EXTENT_FLAG_DATA |
  1211. BTRFS_EXTENT_FLAG_TREE_BLOCK);
  1212. return -EUCLEAN;
  1213. }
  1214. is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
  1215. if (is_tree_block) {
  1216. if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
  1217. key->offset != fs_info->nodesize)) {
  1218. extent_err(leaf, slot,
  1219. "invalid extent length, have %llu expect %u",
  1220. key->offset, fs_info->nodesize);
  1221. return -EUCLEAN;
  1222. }
  1223. } else {
  1224. if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
  1225. extent_err(leaf, slot,
  1226. "invalid key type, have %u expect %u for data backref",
  1227. key->type, BTRFS_EXTENT_ITEM_KEY);
  1228. return -EUCLEAN;
  1229. }
  1230. if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
  1231. extent_err(leaf, slot,
  1232. "invalid extent length, have %llu expect aligned to %u",
  1233. key->offset, fs_info->sectorsize);
  1234. return -EUCLEAN;
  1235. }
  1236. if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
  1237. extent_err(leaf, slot,
  1238. "invalid extent flag, data has full backref set");
  1239. return -EUCLEAN;
  1240. }
  1241. }
  1242. ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
  1243. /* Check the special case of btrfs_tree_block_info */
  1244. if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
  1245. struct btrfs_tree_block_info *info;
  1246. info = (struct btrfs_tree_block_info *)ptr;
  1247. if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
  1248. extent_err(leaf, slot,
  1249. "invalid tree block info level, have %u expect [0, %u]",
  1250. btrfs_tree_block_level(leaf, info),
  1251. BTRFS_MAX_LEVEL - 1);
  1252. return -EUCLEAN;
  1253. }
  1254. ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
  1255. }
  1256. /* Check inline refs */
  1257. while (ptr < end) {
  1258. struct btrfs_extent_inline_ref *iref;
  1259. struct btrfs_extent_data_ref *dref;
  1260. struct btrfs_shared_data_ref *sref;
  1261. u64 dref_offset;
  1262. u64 inline_offset;
  1263. u8 inline_type;
  1264. if (unlikely(ptr + sizeof(*iref) > end)) {
  1265. extent_err(leaf, slot,
  1266. "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
  1267. ptr, sizeof(*iref), end);
  1268. return -EUCLEAN;
  1269. }
  1270. iref = (struct btrfs_extent_inline_ref *)ptr;
  1271. inline_type = btrfs_extent_inline_ref_type(leaf, iref);
  1272. inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
  1273. if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
  1274. extent_err(leaf, slot,
  1275. "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
  1276. ptr, inline_type, end);
  1277. return -EUCLEAN;
  1278. }
  1279. switch (inline_type) {
  1280. /* inline_offset is subvolid of the owner, no need to check */
  1281. case BTRFS_TREE_BLOCK_REF_KEY:
  1282. inline_refs++;
  1283. break;
  1284. /* Contains parent bytenr */
  1285. case BTRFS_SHARED_BLOCK_REF_KEY:
  1286. if (unlikely(!IS_ALIGNED(inline_offset,
  1287. fs_info->sectorsize))) {
  1288. extent_err(leaf, slot,
  1289. "invalid tree parent bytenr, have %llu expect aligned to %u",
  1290. inline_offset, fs_info->sectorsize);
  1291. return -EUCLEAN;
  1292. }
  1293. inline_refs++;
  1294. break;
  1295. /*
  1296. * Contains owner subvolid, owner key objectid, adjusted offset.
  1297. * The only obvious corruption can happen in that offset.
  1298. */
  1299. case BTRFS_EXTENT_DATA_REF_KEY:
  1300. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  1301. dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
  1302. if (unlikely(!IS_ALIGNED(dref_offset,
  1303. fs_info->sectorsize))) {
  1304. extent_err(leaf, slot,
  1305. "invalid data ref offset, have %llu expect aligned to %u",
  1306. dref_offset, fs_info->sectorsize);
  1307. return -EUCLEAN;
  1308. }
  1309. inline_refs += btrfs_extent_data_ref_count(leaf, dref);
  1310. break;
  1311. /* Contains parent bytenr and ref count */
  1312. case BTRFS_SHARED_DATA_REF_KEY:
  1313. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  1314. if (unlikely(!IS_ALIGNED(inline_offset,
  1315. fs_info->sectorsize))) {
  1316. extent_err(leaf, slot,
  1317. "invalid data parent bytenr, have %llu expect aligned to %u",
  1318. inline_offset, fs_info->sectorsize);
  1319. return -EUCLEAN;
  1320. }
  1321. inline_refs += btrfs_shared_data_ref_count(leaf, sref);
  1322. break;
  1323. default:
  1324. extent_err(leaf, slot, "unknown inline ref type: %u",
  1325. inline_type);
  1326. return -EUCLEAN;
  1327. }
  1328. ptr += btrfs_extent_inline_ref_size(inline_type);
  1329. }
  1330. /* No padding is allowed */
  1331. if (unlikely(ptr != end)) {
  1332. extent_err(leaf, slot,
  1333. "invalid extent item size, padding bytes found");
  1334. return -EUCLEAN;
  1335. }
  1336. /* Finally, check the inline refs against total refs */
  1337. if (unlikely(inline_refs > total_refs)) {
  1338. extent_err(leaf, slot,
  1339. "invalid extent refs, have %llu expect >= inline %llu",
  1340. total_refs, inline_refs);
  1341. return -EUCLEAN;
  1342. }
  1343. if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
  1344. (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
  1345. u64 prev_end = prev_key->objectid;
  1346. if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
  1347. prev_end += fs_info->nodesize;
  1348. else
  1349. prev_end += prev_key->offset;
  1350. if (unlikely(prev_end > key->objectid)) {
  1351. extent_err(leaf, slot,
  1352. "previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
  1353. prev_key->objectid, prev_key->type,
  1354. prev_key->offset, key->objectid, key->type,
  1355. key->offset);
  1356. return -EUCLEAN;
  1357. }
  1358. }
  1359. return 0;
  1360. }
  1361. static int check_simple_keyed_refs(struct extent_buffer *leaf,
  1362. struct btrfs_key *key, int slot)
  1363. {
  1364. u32 expect_item_size = 0;
  1365. if (key->type == BTRFS_SHARED_DATA_REF_KEY)
  1366. expect_item_size = sizeof(struct btrfs_shared_data_ref);
  1367. if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
  1368. generic_err(leaf, slot,
  1369. "invalid item size, have %u expect %u for key type %u",
  1370. btrfs_item_size(leaf, slot),
  1371. expect_item_size, key->type);
  1372. return -EUCLEAN;
  1373. }
  1374. if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
  1375. generic_err(leaf, slot,
  1376. "invalid key objectid for shared block ref, have %llu expect aligned to %u",
  1377. key->objectid, leaf->fs_info->sectorsize);
  1378. return -EUCLEAN;
  1379. }
  1380. if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
  1381. !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
  1382. extent_err(leaf, slot,
  1383. "invalid tree parent bytenr, have %llu expect aligned to %u",
  1384. key->offset, leaf->fs_info->sectorsize);
  1385. return -EUCLEAN;
  1386. }
  1387. return 0;
  1388. }
  1389. static int check_extent_data_ref(struct extent_buffer *leaf,
  1390. struct btrfs_key *key, int slot)
  1391. {
  1392. struct btrfs_extent_data_ref *dref;
  1393. unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
  1394. const unsigned long end = ptr + btrfs_item_size(leaf, slot);
  1395. if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
  1396. generic_err(leaf, slot,
  1397. "invalid item size, have %u expect aligned to %zu for key type %u",
  1398. btrfs_item_size(leaf, slot),
  1399. sizeof(*dref), key->type);
  1400. return -EUCLEAN;
  1401. }
  1402. if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
  1403. generic_err(leaf, slot,
  1404. "invalid key objectid for shared block ref, have %llu expect aligned to %u",
  1405. key->objectid, leaf->fs_info->sectorsize);
  1406. return -EUCLEAN;
  1407. }
  1408. for (; ptr < end; ptr += sizeof(*dref)) {
  1409. u64 offset;
  1410. /*
  1411. * We cannot check the extent_data_ref hash due to possible
  1412. * overflow from the leaf due to hash collisions.
  1413. */
  1414. dref = (struct btrfs_extent_data_ref *)ptr;
  1415. offset = btrfs_extent_data_ref_offset(leaf, dref);
  1416. if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
  1417. extent_err(leaf, slot,
  1418. "invalid extent data backref offset, have %llu expect aligned to %u",
  1419. offset, leaf->fs_info->sectorsize);
  1420. return -EUCLEAN;
  1421. }
  1422. }
  1423. return 0;
  1424. }
  1425. #define inode_ref_err(eb, slot, fmt, args...) \
  1426. inode_item_err(eb, slot, fmt, ##args)
  1427. static int check_inode_ref(struct extent_buffer *leaf,
  1428. struct btrfs_key *key, struct btrfs_key *prev_key,
  1429. int slot)
  1430. {
  1431. struct btrfs_inode_ref *iref;
  1432. unsigned long ptr;
  1433. unsigned long end;
  1434. if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
  1435. return -EUCLEAN;
  1436. /* namelen can't be 0, so item_size == sizeof() is also invalid */
  1437. if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
  1438. inode_ref_err(leaf, slot,
  1439. "invalid item size, have %u expect (%zu, %u)",
  1440. btrfs_item_size(leaf, slot),
  1441. sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
  1442. return -EUCLEAN;
  1443. }
  1444. ptr = btrfs_item_ptr_offset(leaf, slot);
  1445. end = ptr + btrfs_item_size(leaf, slot);
  1446. while (ptr < end) {
  1447. u16 namelen;
  1448. if (unlikely(ptr + sizeof(iref) > end)) {
  1449. inode_ref_err(leaf, slot,
  1450. "inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
  1451. ptr, end, sizeof(iref));
  1452. return -EUCLEAN;
  1453. }
  1454. iref = (struct btrfs_inode_ref *)ptr;
  1455. namelen = btrfs_inode_ref_name_len(leaf, iref);
  1456. if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
  1457. inode_ref_err(leaf, slot,
  1458. "inode ref overflow, ptr %lu end %lu namelen %u",
  1459. ptr, end, namelen);
  1460. return -EUCLEAN;
  1461. }
  1462. /*
  1463. * NOTE: In theory we should record all found index numbers
  1464. * to find any duplicated indexes, but that will be too time
  1465. * consuming for inodes with too many hard links.
  1466. */
  1467. ptr += sizeof(*iref) + namelen;
  1468. }
  1469. return 0;
  1470. }
  1471. /*
  1472. * Common point to switch the item-specific validation.
  1473. */
  1474. static int check_leaf_item(struct extent_buffer *leaf,
  1475. struct btrfs_key *key, int slot,
  1476. struct btrfs_key *prev_key)
  1477. {
  1478. int ret = 0;
  1479. struct btrfs_chunk *chunk;
  1480. switch (key->type) {
  1481. case BTRFS_EXTENT_DATA_KEY:
  1482. ret = check_extent_data_item(leaf, key, slot, prev_key);
  1483. break;
  1484. case BTRFS_EXTENT_CSUM_KEY:
  1485. ret = check_csum_item(leaf, key, slot, prev_key);
  1486. break;
  1487. case BTRFS_DIR_ITEM_KEY:
  1488. case BTRFS_DIR_INDEX_KEY:
  1489. case BTRFS_XATTR_ITEM_KEY:
  1490. ret = check_dir_item(leaf, key, prev_key, slot);
  1491. break;
  1492. case BTRFS_INODE_REF_KEY:
  1493. ret = check_inode_ref(leaf, key, prev_key, slot);
  1494. break;
  1495. case BTRFS_BLOCK_GROUP_ITEM_KEY:
  1496. ret = check_block_group_item(leaf, key, slot);
  1497. break;
  1498. case BTRFS_CHUNK_ITEM_KEY:
  1499. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  1500. ret = check_leaf_chunk_item(leaf, chunk, key, slot);
  1501. break;
  1502. case BTRFS_DEV_ITEM_KEY:
  1503. ret = check_dev_item(leaf, key, slot);
  1504. break;
  1505. case BTRFS_INODE_ITEM_KEY:
  1506. ret = check_inode_item(leaf, key, slot);
  1507. break;
  1508. case BTRFS_ROOT_ITEM_KEY:
  1509. ret = check_root_item(leaf, key, slot);
  1510. break;
  1511. case BTRFS_EXTENT_ITEM_KEY:
  1512. case BTRFS_METADATA_ITEM_KEY:
  1513. ret = check_extent_item(leaf, key, slot, prev_key);
  1514. break;
  1515. case BTRFS_TREE_BLOCK_REF_KEY:
  1516. case BTRFS_SHARED_DATA_REF_KEY:
  1517. case BTRFS_SHARED_BLOCK_REF_KEY:
  1518. ret = check_simple_keyed_refs(leaf, key, slot);
  1519. break;
  1520. case BTRFS_EXTENT_DATA_REF_KEY:
  1521. ret = check_extent_data_ref(leaf, key, slot);
  1522. break;
  1523. }
  1524. return ret;
  1525. }
  1526. static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
  1527. {
  1528. struct btrfs_fs_info *fs_info = leaf->fs_info;
  1529. /* No valid key type is 0, so all key should be larger than this key */
  1530. struct btrfs_key prev_key = {0, 0, 0};
  1531. struct btrfs_key key;
  1532. u32 nritems = btrfs_header_nritems(leaf);
  1533. int slot;
  1534. if (unlikely(btrfs_header_level(leaf) != 0)) {
  1535. generic_err(leaf, 0,
  1536. "invalid level for leaf, have %d expect 0",
  1537. btrfs_header_level(leaf));
  1538. return -EUCLEAN;
  1539. }
  1540. /*
  1541. * Extent buffers from a relocation tree have a owner field that
  1542. * corresponds to the subvolume tree they are based on. So just from an
  1543. * extent buffer alone we can not find out what is the id of the
  1544. * corresponding subvolume tree, so we can not figure out if the extent
  1545. * buffer corresponds to the root of the relocation tree or not. So
  1546. * skip this check for relocation trees.
  1547. */
  1548. if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
  1549. u64 owner = btrfs_header_owner(leaf);
  1550. /* These trees must never be empty */
  1551. if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
  1552. owner == BTRFS_CHUNK_TREE_OBJECTID ||
  1553. owner == BTRFS_DEV_TREE_OBJECTID ||
  1554. owner == BTRFS_FS_TREE_OBJECTID ||
  1555. owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
  1556. generic_err(leaf, 0,
  1557. "invalid root, root %llu must never be empty",
  1558. owner);
  1559. return -EUCLEAN;
  1560. }
  1561. /* Unknown tree */
  1562. if (unlikely(owner == 0)) {
  1563. generic_err(leaf, 0,
  1564. "invalid owner, root 0 is not defined");
  1565. return -EUCLEAN;
  1566. }
  1567. /* EXTENT_TREE_V2 can have empty extent trees. */
  1568. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
  1569. return 0;
  1570. if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
  1571. generic_err(leaf, 0,
  1572. "invalid root, root %llu must never be empty",
  1573. owner);
  1574. return -EUCLEAN;
  1575. }
  1576. return 0;
  1577. }
  1578. if (unlikely(nritems == 0))
  1579. return 0;
  1580. /*
  1581. * Check the following things to make sure this is a good leaf, and
  1582. * leaf users won't need to bother with similar sanity checks:
  1583. *
  1584. * 1) key ordering
  1585. * 2) item offset and size
  1586. * No overlap, no hole, all inside the leaf.
  1587. * 3) item content
  1588. * If possible, do comprehensive sanity check.
  1589. * NOTE: All checks must only rely on the item data itself.
  1590. */
  1591. for (slot = 0; slot < nritems; slot++) {
  1592. u32 item_end_expected;
  1593. u64 item_data_end;
  1594. int ret;
  1595. btrfs_item_key_to_cpu(leaf, &key, slot);
  1596. /* Make sure the keys are in the right order */
  1597. if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
  1598. generic_err(leaf, slot,
  1599. "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
  1600. prev_key.objectid, prev_key.type,
  1601. prev_key.offset, key.objectid, key.type,
  1602. key.offset);
  1603. return -EUCLEAN;
  1604. }
  1605. item_data_end = (u64)btrfs_item_offset(leaf, slot) +
  1606. btrfs_item_size(leaf, slot);
  1607. /*
  1608. * Make sure the offset and ends are right, remember that the
  1609. * item data starts at the end of the leaf and grows towards the
  1610. * front.
  1611. */
  1612. if (slot == 0)
  1613. item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
  1614. else
  1615. item_end_expected = btrfs_item_offset(leaf,
  1616. slot - 1);
  1617. if (unlikely(item_data_end != item_end_expected)) {
  1618. generic_err(leaf, slot,
  1619. "unexpected item end, have %llu expect %u",
  1620. item_data_end, item_end_expected);
  1621. return -EUCLEAN;
  1622. }
  1623. /*
  1624. * Check to make sure that we don't point outside of the leaf,
  1625. * just in case all the items are consistent to each other, but
  1626. * all point outside of the leaf.
  1627. */
  1628. if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
  1629. generic_err(leaf, slot,
  1630. "slot end outside of leaf, have %llu expect range [0, %u]",
  1631. item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
  1632. return -EUCLEAN;
  1633. }
  1634. /* Also check if the item pointer overlaps with btrfs item. */
  1635. if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
  1636. btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item))) {
  1637. generic_err(leaf, slot,
  1638. "slot overlaps with its data, item end %lu data start %lu",
  1639. btrfs_item_nr_offset(slot) +
  1640. sizeof(struct btrfs_item),
  1641. btrfs_item_ptr_offset(leaf, slot));
  1642. return -EUCLEAN;
  1643. }
  1644. if (check_item_data) {
  1645. /*
  1646. * Check if the item size and content meet other
  1647. * criteria
  1648. */
  1649. ret = check_leaf_item(leaf, &key, slot, &prev_key);
  1650. if (unlikely(ret < 0))
  1651. return ret;
  1652. }
  1653. prev_key.objectid = key.objectid;
  1654. prev_key.type = key.type;
  1655. prev_key.offset = key.offset;
  1656. }
  1657. return 0;
  1658. }
  1659. int btrfs_check_leaf_full(struct extent_buffer *leaf)
  1660. {
  1661. return check_leaf(leaf, true);
  1662. }
  1663. ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
  1664. int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
  1665. {
  1666. return check_leaf(leaf, false);
  1667. }
  1668. int btrfs_check_node(struct extent_buffer *node)
  1669. {
  1670. struct btrfs_fs_info *fs_info = node->fs_info;
  1671. unsigned long nr = btrfs_header_nritems(node);
  1672. struct btrfs_key key, next_key;
  1673. int slot;
  1674. int level = btrfs_header_level(node);
  1675. u64 bytenr;
  1676. int ret = 0;
  1677. if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
  1678. generic_err(node, 0,
  1679. "invalid level for node, have %d expect [1, %d]",
  1680. level, BTRFS_MAX_LEVEL - 1);
  1681. return -EUCLEAN;
  1682. }
  1683. if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
  1684. btrfs_crit(fs_info,
  1685. "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
  1686. btrfs_header_owner(node), node->start,
  1687. nr == 0 ? "small" : "large", nr,
  1688. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1689. return -EUCLEAN;
  1690. }
  1691. for (slot = 0; slot < nr - 1; slot++) {
  1692. bytenr = btrfs_node_blockptr(node, slot);
  1693. btrfs_node_key_to_cpu(node, &key, slot);
  1694. btrfs_node_key_to_cpu(node, &next_key, slot + 1);
  1695. if (unlikely(!bytenr)) {
  1696. generic_err(node, slot,
  1697. "invalid NULL node pointer");
  1698. ret = -EUCLEAN;
  1699. goto out;
  1700. }
  1701. if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
  1702. generic_err(node, slot,
  1703. "unaligned pointer, have %llu should be aligned to %u",
  1704. bytenr, fs_info->sectorsize);
  1705. ret = -EUCLEAN;
  1706. goto out;
  1707. }
  1708. if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
  1709. generic_err(node, slot,
  1710. "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
  1711. key.objectid, key.type, key.offset,
  1712. next_key.objectid, next_key.type,
  1713. next_key.offset);
  1714. ret = -EUCLEAN;
  1715. goto out;
  1716. }
  1717. }
  1718. out:
  1719. return ret;
  1720. }
  1721. ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
  1722. int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
  1723. {
  1724. const bool is_subvol = is_fstree(root_owner);
  1725. const u64 eb_owner = btrfs_header_owner(eb);
  1726. /*
  1727. * Skip dummy fs, as selftests don't create unique ebs for each dummy
  1728. * root.
  1729. */
  1730. if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
  1731. return 0;
  1732. /*
  1733. * There are several call sites (backref walking, qgroup, and data
  1734. * reloc) passing 0 as @root_owner, as they are not holding the
  1735. * tree root. In that case, we can not do a reliable ownership check,
  1736. * so just exit.
  1737. */
  1738. if (root_owner == 0)
  1739. return 0;
  1740. /*
  1741. * These trees use key.offset as their owner, our callers don't have
  1742. * the extra capacity to pass key.offset here. So we just skip them.
  1743. */
  1744. if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
  1745. root_owner == BTRFS_TREE_RELOC_OBJECTID)
  1746. return 0;
  1747. if (!is_subvol) {
  1748. /* For non-subvolume trees, the eb owner should match root owner */
  1749. if (unlikely(root_owner != eb_owner)) {
  1750. btrfs_crit(eb->fs_info,
  1751. "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
  1752. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  1753. root_owner, btrfs_header_bytenr(eb), eb_owner,
  1754. root_owner);
  1755. return -EUCLEAN;
  1756. }
  1757. return 0;
  1758. }
  1759. /*
  1760. * For subvolume trees, owners can mismatch, but they should all belong
  1761. * to subvolume trees.
  1762. */
  1763. if (unlikely(is_subvol != is_fstree(eb_owner))) {
  1764. btrfs_crit(eb->fs_info,
  1765. "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
  1766. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  1767. root_owner, btrfs_header_bytenr(eb), eb_owner,
  1768. BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
  1769. return -EUCLEAN;
  1770. }
  1771. return 0;
  1772. }