check-integrity.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STRATO AG 2011. All rights reserved.
  4. */
  5. /*
  6. * This module can be used to catch cases when the btrfs kernel
  7. * code executes write requests to the disk that bring the file
  8. * system in an inconsistent state. In such a state, a power-loss
  9. * or kernel panic event would cause that the data on disk is
  10. * lost or at least damaged.
  11. *
  12. * Code is added that examines all block write requests during
  13. * runtime (including writes of the super block). Three rules
  14. * are verified and an error is printed on violation of the
  15. * rules:
  16. * 1. It is not allowed to write a disk block which is
  17. * currently referenced by the super block (either directly
  18. * or indirectly).
  19. * 2. When a super block is written, it is verified that all
  20. * referenced (directly or indirectly) blocks fulfill the
  21. * following requirements:
  22. * 2a. All referenced blocks have either been present when
  23. * the file system was mounted, (i.e., they have been
  24. * referenced by the super block) or they have been
  25. * written since then and the write completion callback
  26. * was called and no write error was indicated and a
  27. * FLUSH request to the device where these blocks are
  28. * located was received and completed.
  29. * 2b. All referenced blocks need to have a generation
  30. * number which is equal to the parent's number.
  31. *
  32. * One issue that was found using this module was that the log
  33. * tree on disk became temporarily corrupted because disk blocks
  34. * that had been in use for the log tree had been freed and
  35. * reused too early, while being referenced by the written super
  36. * block.
  37. *
  38. * The search term in the kernel log that can be used to filter
  39. * on the existence of detected integrity issues is
  40. * "btrfs: attempt".
  41. *
  42. * The integrity check is enabled via mount options. These
  43. * mount options are only supported if the integrity check
  44. * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY.
  45. *
  46. * Example #1, apply integrity checks to all metadata:
  47. * mount /dev/sdb1 /mnt -o check_int
  48. *
  49. * Example #2, apply integrity checks to all metadata and
  50. * to data extents:
  51. * mount /dev/sdb1 /mnt -o check_int_data
  52. *
  53. * Example #3, apply integrity checks to all metadata and dump
  54. * the tree that the super block references to kernel messages
  55. * each time after a super block was written:
  56. * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263
  57. *
  58. * If the integrity check tool is included and activated in
  59. * the mount options, plenty of kernel memory is used, and
  60. * plenty of additional CPU cycles are spent. Enabling this
  61. * functionality is not intended for normal use. In most
  62. * cases, unless you are a btrfs developer who needs to verify
  63. * the integrity of (super)-block write requests, do not
  64. * enable the config option BTRFS_FS_CHECK_INTEGRITY to
  65. * include and compile the integrity check tool.
  66. *
  67. * Expect millions of lines of information in the kernel log with an
  68. * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
  69. * kernel config to at least 26 (which is 64MB). Usually the value is
  70. * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
  71. * changed like this before LOG_BUF_SHIFT can be set to a high value:
  72. * config LOG_BUF_SHIFT
  73. * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
  74. * range 12 30
  75. */
  76. #include <linux/sched.h>
  77. #include <linux/slab.h>
  78. #include <linux/buffer_head.h>
  79. #include <linux/mutex.h>
  80. #include <linux/genhd.h>
  81. #include <linux/blkdev.h>
  82. #include <linux/mm.h>
  83. #include <linux/string.h>
  84. #include <linux/crc32c.h>
  85. #include "ctree.h"
  86. #include "disk-io.h"
  87. #include "transaction.h"
  88. #include "extent_io.h"
  89. #include "volumes.h"
  90. #include "print-tree.h"
  91. #include "locking.h"
  92. #include "check-integrity.h"
  93. #include "rcu-string.h"
  94. #include "compression.h"
  95. #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
  96. #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
  97. #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
  98. #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
  99. #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
  100. #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
  101. #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
  102. #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
  103. * excluding " [...]" */
  104. #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
  105. /*
  106. * The definition of the bitmask fields for the print_mask.
  107. * They are specified with the mount option check_integrity_print_mask.
  108. */
  109. #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
  110. #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
  111. #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
  112. #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
  113. #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
  114. #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
  115. #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
  116. #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
  117. #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
  118. #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
  119. #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
  120. #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
  121. #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
  122. #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
  123. struct btrfsic_dev_state;
  124. struct btrfsic_state;
  125. struct btrfsic_block {
  126. u32 magic_num; /* only used for debug purposes */
  127. unsigned int is_metadata:1; /* if it is meta-data, not data-data */
  128. unsigned int is_superblock:1; /* if it is one of the superblocks */
  129. unsigned int is_iodone:1; /* if is done by lower subsystem */
  130. unsigned int iodone_w_error:1; /* error was indicated to endio */
  131. unsigned int never_written:1; /* block was added because it was
  132. * referenced, not because it was
  133. * written */
  134. unsigned int mirror_num; /* large enough to hold
  135. * BTRFS_SUPER_MIRROR_MAX */
  136. struct btrfsic_dev_state *dev_state;
  137. u64 dev_bytenr; /* key, physical byte num on disk */
  138. u64 logical_bytenr; /* logical byte num on disk */
  139. u64 generation;
  140. struct btrfs_disk_key disk_key; /* extra info to print in case of
  141. * issues, will not always be correct */
  142. struct list_head collision_resolving_node; /* list node */
  143. struct list_head all_blocks_node; /* list node */
  144. /* the following two lists contain block_link items */
  145. struct list_head ref_to_list; /* list */
  146. struct list_head ref_from_list; /* list */
  147. struct btrfsic_block *next_in_same_bio;
  148. void *orig_bio_bh_private;
  149. union {
  150. bio_end_io_t *bio;
  151. bh_end_io_t *bh;
  152. } orig_bio_bh_end_io;
  153. int submit_bio_bh_rw;
  154. u64 flush_gen; /* only valid if !never_written */
  155. };
  156. /*
  157. * Elements of this type are allocated dynamically and required because
  158. * each block object can refer to and can be ref from multiple blocks.
  159. * The key to lookup them in the hashtable is the dev_bytenr of
  160. * the block ref to plus the one from the block referred from.
  161. * The fact that they are searchable via a hashtable and that a
  162. * ref_cnt is maintained is not required for the btrfs integrity
  163. * check algorithm itself, it is only used to make the output more
  164. * beautiful in case that an error is detected (an error is defined
  165. * as a write operation to a block while that block is still referenced).
  166. */
  167. struct btrfsic_block_link {
  168. u32 magic_num; /* only used for debug purposes */
  169. u32 ref_cnt;
  170. struct list_head node_ref_to; /* list node */
  171. struct list_head node_ref_from; /* list node */
  172. struct list_head collision_resolving_node; /* list node */
  173. struct btrfsic_block *block_ref_to;
  174. struct btrfsic_block *block_ref_from;
  175. u64 parent_generation;
  176. };
  177. struct btrfsic_dev_state {
  178. u32 magic_num; /* only used for debug purposes */
  179. struct block_device *bdev;
  180. struct btrfsic_state *state;
  181. struct list_head collision_resolving_node; /* list node */
  182. struct btrfsic_block dummy_block_for_bio_bh_flush;
  183. u64 last_flush_gen;
  184. char name[BDEVNAME_SIZE];
  185. };
  186. struct btrfsic_block_hashtable {
  187. struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
  188. };
  189. struct btrfsic_block_link_hashtable {
  190. struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
  191. };
  192. struct btrfsic_dev_state_hashtable {
  193. struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
  194. };
  195. struct btrfsic_block_data_ctx {
  196. u64 start; /* virtual bytenr */
  197. u64 dev_bytenr; /* physical bytenr on device */
  198. u32 len;
  199. struct btrfsic_dev_state *dev;
  200. char **datav;
  201. struct page **pagev;
  202. void *mem_to_free;
  203. };
  204. /* This structure is used to implement recursion without occupying
  205. * any stack space, refer to btrfsic_process_metablock() */
  206. struct btrfsic_stack_frame {
  207. u32 magic;
  208. u32 nr;
  209. int error;
  210. int i;
  211. int limit_nesting;
  212. int num_copies;
  213. int mirror_num;
  214. struct btrfsic_block *block;
  215. struct btrfsic_block_data_ctx *block_ctx;
  216. struct btrfsic_block *next_block;
  217. struct btrfsic_block_data_ctx next_block_ctx;
  218. struct btrfs_header *hdr;
  219. struct btrfsic_stack_frame *prev;
  220. };
  221. /* Some state per mounted filesystem */
  222. struct btrfsic_state {
  223. u32 print_mask;
  224. int include_extent_data;
  225. int csum_size;
  226. struct list_head all_blocks_list;
  227. struct btrfsic_block_hashtable block_hashtable;
  228. struct btrfsic_block_link_hashtable block_link_hashtable;
  229. struct btrfs_fs_info *fs_info;
  230. u64 max_superblock_generation;
  231. struct btrfsic_block *latest_superblock;
  232. u32 metablock_size;
  233. u32 datablock_size;
  234. };
  235. static void btrfsic_block_init(struct btrfsic_block *b);
  236. static struct btrfsic_block *btrfsic_block_alloc(void);
  237. static void btrfsic_block_free(struct btrfsic_block *b);
  238. static void btrfsic_block_link_init(struct btrfsic_block_link *n);
  239. static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
  240. static void btrfsic_block_link_free(struct btrfsic_block_link *n);
  241. static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
  242. static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
  243. static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
  244. static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
  245. static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
  246. struct btrfsic_block_hashtable *h);
  247. static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
  248. static struct btrfsic_block *btrfsic_block_hashtable_lookup(
  249. struct block_device *bdev,
  250. u64 dev_bytenr,
  251. struct btrfsic_block_hashtable *h);
  252. static void btrfsic_block_link_hashtable_init(
  253. struct btrfsic_block_link_hashtable *h);
  254. static void btrfsic_block_link_hashtable_add(
  255. struct btrfsic_block_link *l,
  256. struct btrfsic_block_link_hashtable *h);
  257. static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
  258. static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
  259. struct block_device *bdev_ref_to,
  260. u64 dev_bytenr_ref_to,
  261. struct block_device *bdev_ref_from,
  262. u64 dev_bytenr_ref_from,
  263. struct btrfsic_block_link_hashtable *h);
  264. static void btrfsic_dev_state_hashtable_init(
  265. struct btrfsic_dev_state_hashtable *h);
  266. static void btrfsic_dev_state_hashtable_add(
  267. struct btrfsic_dev_state *ds,
  268. struct btrfsic_dev_state_hashtable *h);
  269. static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
  270. static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
  271. struct btrfsic_dev_state_hashtable *h);
  272. static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
  273. static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
  274. static int btrfsic_process_superblock(struct btrfsic_state *state,
  275. struct btrfs_fs_devices *fs_devices);
  276. static int btrfsic_process_metablock(struct btrfsic_state *state,
  277. struct btrfsic_block *block,
  278. struct btrfsic_block_data_ctx *block_ctx,
  279. int limit_nesting, int force_iodone_flag);
  280. static void btrfsic_read_from_block_data(
  281. struct btrfsic_block_data_ctx *block_ctx,
  282. void *dst, u32 offset, size_t len);
  283. static int btrfsic_create_link_to_next_block(
  284. struct btrfsic_state *state,
  285. struct btrfsic_block *block,
  286. struct btrfsic_block_data_ctx
  287. *block_ctx, u64 next_bytenr,
  288. int limit_nesting,
  289. struct btrfsic_block_data_ctx *next_block_ctx,
  290. struct btrfsic_block **next_blockp,
  291. int force_iodone_flag,
  292. int *num_copiesp, int *mirror_nump,
  293. struct btrfs_disk_key *disk_key,
  294. u64 parent_generation);
  295. static int btrfsic_handle_extent_data(struct btrfsic_state *state,
  296. struct btrfsic_block *block,
  297. struct btrfsic_block_data_ctx *block_ctx,
  298. u32 item_offset, int force_iodone_flag);
  299. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  300. struct btrfsic_block_data_ctx *block_ctx_out,
  301. int mirror_num);
  302. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
  303. static int btrfsic_read_block(struct btrfsic_state *state,
  304. struct btrfsic_block_data_ctx *block_ctx);
  305. static void btrfsic_dump_database(struct btrfsic_state *state);
  306. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  307. char **datav, unsigned int num_pages);
  308. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  309. u64 dev_bytenr, char **mapped_datav,
  310. unsigned int num_pages,
  311. struct bio *bio, int *bio_is_patched,
  312. struct buffer_head *bh,
  313. int submit_bio_bh_rw);
  314. static int btrfsic_process_written_superblock(
  315. struct btrfsic_state *state,
  316. struct btrfsic_block *const block,
  317. struct btrfs_super_block *const super_hdr);
  318. static void btrfsic_bio_end_io(struct bio *bp);
  319. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
  320. static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
  321. const struct btrfsic_block *block,
  322. int recursion_level);
  323. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  324. struct btrfsic_block *const block,
  325. int recursion_level);
  326. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  327. const struct btrfsic_block_link *l);
  328. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  329. const struct btrfsic_block_link *l);
  330. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  331. const struct btrfsic_block *block);
  332. static void btrfsic_dump_tree(const struct btrfsic_state *state);
  333. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  334. const struct btrfsic_block *block,
  335. int indent_level);
  336. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  337. struct btrfsic_state *state,
  338. struct btrfsic_block_data_ctx *next_block_ctx,
  339. struct btrfsic_block *next_block,
  340. struct btrfsic_block *from_block,
  341. u64 parent_generation);
  342. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  343. struct btrfsic_state *state,
  344. struct btrfsic_block_data_ctx *block_ctx,
  345. const char *additional_string,
  346. int is_metadata,
  347. int is_iodone,
  348. int never_written,
  349. int mirror_num,
  350. int *was_created);
  351. static int btrfsic_process_superblock_dev_mirror(
  352. struct btrfsic_state *state,
  353. struct btrfsic_dev_state *dev_state,
  354. struct btrfs_device *device,
  355. int superblock_mirror_num,
  356. struct btrfsic_dev_state **selected_dev_state,
  357. struct btrfs_super_block *selected_super);
  358. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev);
  359. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  360. u64 bytenr,
  361. struct btrfsic_dev_state *dev_state,
  362. u64 dev_bytenr);
  363. static struct mutex btrfsic_mutex;
  364. static int btrfsic_is_initialized;
  365. static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
  366. static void btrfsic_block_init(struct btrfsic_block *b)
  367. {
  368. b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
  369. b->dev_state = NULL;
  370. b->dev_bytenr = 0;
  371. b->logical_bytenr = 0;
  372. b->generation = BTRFSIC_GENERATION_UNKNOWN;
  373. b->disk_key.objectid = 0;
  374. b->disk_key.type = 0;
  375. b->disk_key.offset = 0;
  376. b->is_metadata = 0;
  377. b->is_superblock = 0;
  378. b->is_iodone = 0;
  379. b->iodone_w_error = 0;
  380. b->never_written = 0;
  381. b->mirror_num = 0;
  382. b->next_in_same_bio = NULL;
  383. b->orig_bio_bh_private = NULL;
  384. b->orig_bio_bh_end_io.bio = NULL;
  385. INIT_LIST_HEAD(&b->collision_resolving_node);
  386. INIT_LIST_HEAD(&b->all_blocks_node);
  387. INIT_LIST_HEAD(&b->ref_to_list);
  388. INIT_LIST_HEAD(&b->ref_from_list);
  389. b->submit_bio_bh_rw = 0;
  390. b->flush_gen = 0;
  391. }
  392. static struct btrfsic_block *btrfsic_block_alloc(void)
  393. {
  394. struct btrfsic_block *b;
  395. b = kzalloc(sizeof(*b), GFP_NOFS);
  396. if (NULL != b)
  397. btrfsic_block_init(b);
  398. return b;
  399. }
  400. static void btrfsic_block_free(struct btrfsic_block *b)
  401. {
  402. BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
  403. kfree(b);
  404. }
  405. static void btrfsic_block_link_init(struct btrfsic_block_link *l)
  406. {
  407. l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
  408. l->ref_cnt = 1;
  409. INIT_LIST_HEAD(&l->node_ref_to);
  410. INIT_LIST_HEAD(&l->node_ref_from);
  411. INIT_LIST_HEAD(&l->collision_resolving_node);
  412. l->block_ref_to = NULL;
  413. l->block_ref_from = NULL;
  414. }
  415. static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
  416. {
  417. struct btrfsic_block_link *l;
  418. l = kzalloc(sizeof(*l), GFP_NOFS);
  419. if (NULL != l)
  420. btrfsic_block_link_init(l);
  421. return l;
  422. }
  423. static void btrfsic_block_link_free(struct btrfsic_block_link *l)
  424. {
  425. BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
  426. kfree(l);
  427. }
  428. static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
  429. {
  430. ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
  431. ds->bdev = NULL;
  432. ds->state = NULL;
  433. ds->name[0] = '\0';
  434. INIT_LIST_HEAD(&ds->collision_resolving_node);
  435. ds->last_flush_gen = 0;
  436. btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
  437. ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
  438. ds->dummy_block_for_bio_bh_flush.dev_state = ds;
  439. }
  440. static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
  441. {
  442. struct btrfsic_dev_state *ds;
  443. ds = kzalloc(sizeof(*ds), GFP_NOFS);
  444. if (NULL != ds)
  445. btrfsic_dev_state_init(ds);
  446. return ds;
  447. }
  448. static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
  449. {
  450. BUG_ON(!(NULL == ds ||
  451. BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
  452. kfree(ds);
  453. }
  454. static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
  455. {
  456. int i;
  457. for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
  458. INIT_LIST_HEAD(h->table + i);
  459. }
  460. static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
  461. struct btrfsic_block_hashtable *h)
  462. {
  463. const unsigned int hashval =
  464. (((unsigned int)(b->dev_bytenr >> 16)) ^
  465. ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
  466. (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
  467. list_add(&b->collision_resolving_node, h->table + hashval);
  468. }
  469. static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
  470. {
  471. list_del(&b->collision_resolving_node);
  472. }
  473. static struct btrfsic_block *btrfsic_block_hashtable_lookup(
  474. struct block_device *bdev,
  475. u64 dev_bytenr,
  476. struct btrfsic_block_hashtable *h)
  477. {
  478. const unsigned int hashval =
  479. (((unsigned int)(dev_bytenr >> 16)) ^
  480. ((unsigned int)((uintptr_t)bdev))) &
  481. (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
  482. struct btrfsic_block *b;
  483. list_for_each_entry(b, h->table + hashval, collision_resolving_node) {
  484. if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
  485. return b;
  486. }
  487. return NULL;
  488. }
  489. static void btrfsic_block_link_hashtable_init(
  490. struct btrfsic_block_link_hashtable *h)
  491. {
  492. int i;
  493. for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
  494. INIT_LIST_HEAD(h->table + i);
  495. }
  496. static void btrfsic_block_link_hashtable_add(
  497. struct btrfsic_block_link *l,
  498. struct btrfsic_block_link_hashtable *h)
  499. {
  500. const unsigned int hashval =
  501. (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
  502. ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
  503. ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
  504. ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
  505. & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
  506. BUG_ON(NULL == l->block_ref_to);
  507. BUG_ON(NULL == l->block_ref_from);
  508. list_add(&l->collision_resolving_node, h->table + hashval);
  509. }
  510. static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
  511. {
  512. list_del(&l->collision_resolving_node);
  513. }
  514. static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
  515. struct block_device *bdev_ref_to,
  516. u64 dev_bytenr_ref_to,
  517. struct block_device *bdev_ref_from,
  518. u64 dev_bytenr_ref_from,
  519. struct btrfsic_block_link_hashtable *h)
  520. {
  521. const unsigned int hashval =
  522. (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
  523. ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
  524. ((unsigned int)((uintptr_t)bdev_ref_to)) ^
  525. ((unsigned int)((uintptr_t)bdev_ref_from))) &
  526. (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
  527. struct btrfsic_block_link *l;
  528. list_for_each_entry(l, h->table + hashval, collision_resolving_node) {
  529. BUG_ON(NULL == l->block_ref_to);
  530. BUG_ON(NULL == l->block_ref_from);
  531. if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
  532. l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
  533. l->block_ref_from->dev_state->bdev == bdev_ref_from &&
  534. l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
  535. return l;
  536. }
  537. return NULL;
  538. }
  539. static void btrfsic_dev_state_hashtable_init(
  540. struct btrfsic_dev_state_hashtable *h)
  541. {
  542. int i;
  543. for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
  544. INIT_LIST_HEAD(h->table + i);
  545. }
  546. static void btrfsic_dev_state_hashtable_add(
  547. struct btrfsic_dev_state *ds,
  548. struct btrfsic_dev_state_hashtable *h)
  549. {
  550. const unsigned int hashval =
  551. (((unsigned int)((uintptr_t)ds->bdev->bd_dev)) &
  552. (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
  553. list_add(&ds->collision_resolving_node, h->table + hashval);
  554. }
  555. static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
  556. {
  557. list_del(&ds->collision_resolving_node);
  558. }
  559. static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
  560. struct btrfsic_dev_state_hashtable *h)
  561. {
  562. const unsigned int hashval =
  563. dev & (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1);
  564. struct btrfsic_dev_state *ds;
  565. list_for_each_entry(ds, h->table + hashval, collision_resolving_node) {
  566. if (ds->bdev->bd_dev == dev)
  567. return ds;
  568. }
  569. return NULL;
  570. }
  571. static int btrfsic_process_superblock(struct btrfsic_state *state,
  572. struct btrfs_fs_devices *fs_devices)
  573. {
  574. struct btrfs_fs_info *fs_info = state->fs_info;
  575. struct btrfs_super_block *selected_super;
  576. struct list_head *dev_head = &fs_devices->devices;
  577. struct btrfs_device *device;
  578. struct btrfsic_dev_state *selected_dev_state = NULL;
  579. int ret = 0;
  580. int pass;
  581. BUG_ON(NULL == state);
  582. selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
  583. if (NULL == selected_super) {
  584. pr_info("btrfsic: error, kmalloc failed!\n");
  585. return -ENOMEM;
  586. }
  587. list_for_each_entry(device, dev_head, dev_list) {
  588. int i;
  589. struct btrfsic_dev_state *dev_state;
  590. if (!device->bdev || !device->name)
  591. continue;
  592. dev_state = btrfsic_dev_state_lookup(device->bdev->bd_dev);
  593. BUG_ON(NULL == dev_state);
  594. for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
  595. ret = btrfsic_process_superblock_dev_mirror(
  596. state, dev_state, device, i,
  597. &selected_dev_state, selected_super);
  598. if (0 != ret && 0 == i) {
  599. kfree(selected_super);
  600. return ret;
  601. }
  602. }
  603. }
  604. if (NULL == state->latest_superblock) {
  605. pr_info("btrfsic: no superblock found!\n");
  606. kfree(selected_super);
  607. return -1;
  608. }
  609. state->csum_size = btrfs_super_csum_size(selected_super);
  610. for (pass = 0; pass < 3; pass++) {
  611. int num_copies;
  612. int mirror_num;
  613. u64 next_bytenr;
  614. switch (pass) {
  615. case 0:
  616. next_bytenr = btrfs_super_root(selected_super);
  617. if (state->print_mask &
  618. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  619. pr_info("root@%llu\n", next_bytenr);
  620. break;
  621. case 1:
  622. next_bytenr = btrfs_super_chunk_root(selected_super);
  623. if (state->print_mask &
  624. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  625. pr_info("chunk@%llu\n", next_bytenr);
  626. break;
  627. case 2:
  628. next_bytenr = btrfs_super_log_root(selected_super);
  629. if (0 == next_bytenr)
  630. continue;
  631. if (state->print_mask &
  632. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  633. pr_info("log@%llu\n", next_bytenr);
  634. break;
  635. }
  636. num_copies = btrfs_num_copies(fs_info, next_bytenr,
  637. state->metablock_size);
  638. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  639. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  640. next_bytenr, num_copies);
  641. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  642. struct btrfsic_block *next_block;
  643. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  644. struct btrfsic_block_link *l;
  645. ret = btrfsic_map_block(state, next_bytenr,
  646. state->metablock_size,
  647. &tmp_next_block_ctx,
  648. mirror_num);
  649. if (ret) {
  650. pr_info("btrfsic: btrfsic_map_block(root @%llu, mirror %d) failed!\n",
  651. next_bytenr, mirror_num);
  652. kfree(selected_super);
  653. return -1;
  654. }
  655. next_block = btrfsic_block_hashtable_lookup(
  656. tmp_next_block_ctx.dev->bdev,
  657. tmp_next_block_ctx.dev_bytenr,
  658. &state->block_hashtable);
  659. BUG_ON(NULL == next_block);
  660. l = btrfsic_block_link_hashtable_lookup(
  661. tmp_next_block_ctx.dev->bdev,
  662. tmp_next_block_ctx.dev_bytenr,
  663. state->latest_superblock->dev_state->
  664. bdev,
  665. state->latest_superblock->dev_bytenr,
  666. &state->block_link_hashtable);
  667. BUG_ON(NULL == l);
  668. ret = btrfsic_read_block(state, &tmp_next_block_ctx);
  669. if (ret < (int)PAGE_SIZE) {
  670. pr_info("btrfsic: read @logical %llu failed!\n",
  671. tmp_next_block_ctx.start);
  672. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  673. kfree(selected_super);
  674. return -1;
  675. }
  676. ret = btrfsic_process_metablock(state,
  677. next_block,
  678. &tmp_next_block_ctx,
  679. BTRFS_MAX_LEVEL + 3, 1);
  680. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  681. }
  682. }
  683. kfree(selected_super);
  684. return ret;
  685. }
  686. static int btrfsic_process_superblock_dev_mirror(
  687. struct btrfsic_state *state,
  688. struct btrfsic_dev_state *dev_state,
  689. struct btrfs_device *device,
  690. int superblock_mirror_num,
  691. struct btrfsic_dev_state **selected_dev_state,
  692. struct btrfs_super_block *selected_super)
  693. {
  694. struct btrfs_fs_info *fs_info = state->fs_info;
  695. struct btrfs_super_block *super_tmp;
  696. u64 dev_bytenr;
  697. struct buffer_head *bh;
  698. struct btrfsic_block *superblock_tmp;
  699. int pass;
  700. struct block_device *const superblock_bdev = device->bdev;
  701. /* super block bytenr is always the unmapped device bytenr */
  702. dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
  703. if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
  704. return -1;
  705. bh = __bread(superblock_bdev, dev_bytenr / BTRFS_BDEV_BLOCKSIZE,
  706. BTRFS_SUPER_INFO_SIZE);
  707. if (NULL == bh)
  708. return -1;
  709. super_tmp = (struct btrfs_super_block *)
  710. (bh->b_data + (dev_bytenr & (BTRFS_BDEV_BLOCKSIZE - 1)));
  711. if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
  712. btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
  713. memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
  714. btrfs_super_nodesize(super_tmp) != state->metablock_size ||
  715. btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
  716. brelse(bh);
  717. return 0;
  718. }
  719. superblock_tmp =
  720. btrfsic_block_hashtable_lookup(superblock_bdev,
  721. dev_bytenr,
  722. &state->block_hashtable);
  723. if (NULL == superblock_tmp) {
  724. superblock_tmp = btrfsic_block_alloc();
  725. if (NULL == superblock_tmp) {
  726. pr_info("btrfsic: error, kmalloc failed!\n");
  727. brelse(bh);
  728. return -1;
  729. }
  730. /* for superblock, only the dev_bytenr makes sense */
  731. superblock_tmp->dev_bytenr = dev_bytenr;
  732. superblock_tmp->dev_state = dev_state;
  733. superblock_tmp->logical_bytenr = dev_bytenr;
  734. superblock_tmp->generation = btrfs_super_generation(super_tmp);
  735. superblock_tmp->is_metadata = 1;
  736. superblock_tmp->is_superblock = 1;
  737. superblock_tmp->is_iodone = 1;
  738. superblock_tmp->never_written = 0;
  739. superblock_tmp->mirror_num = 1 + superblock_mirror_num;
  740. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  741. btrfs_info_in_rcu(fs_info,
  742. "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)",
  743. superblock_bdev,
  744. rcu_str_deref(device->name), dev_bytenr,
  745. dev_state->name, dev_bytenr,
  746. superblock_mirror_num);
  747. list_add(&superblock_tmp->all_blocks_node,
  748. &state->all_blocks_list);
  749. btrfsic_block_hashtable_add(superblock_tmp,
  750. &state->block_hashtable);
  751. }
  752. /* select the one with the highest generation field */
  753. if (btrfs_super_generation(super_tmp) >
  754. state->max_superblock_generation ||
  755. 0 == state->max_superblock_generation) {
  756. memcpy(selected_super, super_tmp, sizeof(*selected_super));
  757. *selected_dev_state = dev_state;
  758. state->max_superblock_generation =
  759. btrfs_super_generation(super_tmp);
  760. state->latest_superblock = superblock_tmp;
  761. }
  762. for (pass = 0; pass < 3; pass++) {
  763. u64 next_bytenr;
  764. int num_copies;
  765. int mirror_num;
  766. const char *additional_string = NULL;
  767. struct btrfs_disk_key tmp_disk_key;
  768. tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
  769. tmp_disk_key.offset = 0;
  770. switch (pass) {
  771. case 0:
  772. btrfs_set_disk_key_objectid(&tmp_disk_key,
  773. BTRFS_ROOT_TREE_OBJECTID);
  774. additional_string = "initial root ";
  775. next_bytenr = btrfs_super_root(super_tmp);
  776. break;
  777. case 1:
  778. btrfs_set_disk_key_objectid(&tmp_disk_key,
  779. BTRFS_CHUNK_TREE_OBJECTID);
  780. additional_string = "initial chunk ";
  781. next_bytenr = btrfs_super_chunk_root(super_tmp);
  782. break;
  783. case 2:
  784. btrfs_set_disk_key_objectid(&tmp_disk_key,
  785. BTRFS_TREE_LOG_OBJECTID);
  786. additional_string = "initial log ";
  787. next_bytenr = btrfs_super_log_root(super_tmp);
  788. if (0 == next_bytenr)
  789. continue;
  790. break;
  791. }
  792. num_copies = btrfs_num_copies(fs_info, next_bytenr,
  793. state->metablock_size);
  794. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  795. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  796. next_bytenr, num_copies);
  797. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  798. struct btrfsic_block *next_block;
  799. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  800. struct btrfsic_block_link *l;
  801. if (btrfsic_map_block(state, next_bytenr,
  802. state->metablock_size,
  803. &tmp_next_block_ctx,
  804. mirror_num)) {
  805. pr_info("btrfsic: btrfsic_map_block(bytenr @%llu, mirror %d) failed!\n",
  806. next_bytenr, mirror_num);
  807. brelse(bh);
  808. return -1;
  809. }
  810. next_block = btrfsic_block_lookup_or_add(
  811. state, &tmp_next_block_ctx,
  812. additional_string, 1, 1, 0,
  813. mirror_num, NULL);
  814. if (NULL == next_block) {
  815. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  816. brelse(bh);
  817. return -1;
  818. }
  819. next_block->disk_key = tmp_disk_key;
  820. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  821. l = btrfsic_block_link_lookup_or_add(
  822. state, &tmp_next_block_ctx,
  823. next_block, superblock_tmp,
  824. BTRFSIC_GENERATION_UNKNOWN);
  825. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  826. if (NULL == l) {
  827. brelse(bh);
  828. return -1;
  829. }
  830. }
  831. }
  832. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
  833. btrfsic_dump_tree_sub(state, superblock_tmp, 0);
  834. brelse(bh);
  835. return 0;
  836. }
  837. static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
  838. {
  839. struct btrfsic_stack_frame *sf;
  840. sf = kzalloc(sizeof(*sf), GFP_NOFS);
  841. if (NULL == sf)
  842. pr_info("btrfsic: alloc memory failed!\n");
  843. else
  844. sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
  845. return sf;
  846. }
  847. static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
  848. {
  849. BUG_ON(!(NULL == sf ||
  850. BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
  851. kfree(sf);
  852. }
  853. static int btrfsic_process_metablock(
  854. struct btrfsic_state *state,
  855. struct btrfsic_block *const first_block,
  856. struct btrfsic_block_data_ctx *const first_block_ctx,
  857. int first_limit_nesting, int force_iodone_flag)
  858. {
  859. struct btrfsic_stack_frame initial_stack_frame = { 0 };
  860. struct btrfsic_stack_frame *sf;
  861. struct btrfsic_stack_frame *next_stack;
  862. struct btrfs_header *const first_hdr =
  863. (struct btrfs_header *)first_block_ctx->datav[0];
  864. BUG_ON(!first_hdr);
  865. sf = &initial_stack_frame;
  866. sf->error = 0;
  867. sf->i = -1;
  868. sf->limit_nesting = first_limit_nesting;
  869. sf->block = first_block;
  870. sf->block_ctx = first_block_ctx;
  871. sf->next_block = NULL;
  872. sf->hdr = first_hdr;
  873. sf->prev = NULL;
  874. continue_with_new_stack_frame:
  875. sf->block->generation = le64_to_cpu(sf->hdr->generation);
  876. if (0 == sf->hdr->level) {
  877. struct btrfs_leaf *const leafhdr =
  878. (struct btrfs_leaf *)sf->hdr;
  879. if (-1 == sf->i) {
  880. sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
  881. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  882. pr_info("leaf %llu items %d generation %llu owner %llu\n",
  883. sf->block_ctx->start, sf->nr,
  884. btrfs_stack_header_generation(
  885. &leafhdr->header),
  886. btrfs_stack_header_owner(
  887. &leafhdr->header));
  888. }
  889. continue_with_current_leaf_stack_frame:
  890. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  891. sf->i++;
  892. sf->num_copies = 0;
  893. }
  894. if (sf->i < sf->nr) {
  895. struct btrfs_item disk_item;
  896. u32 disk_item_offset =
  897. (uintptr_t)(leafhdr->items + sf->i) -
  898. (uintptr_t)leafhdr;
  899. struct btrfs_disk_key *disk_key;
  900. u8 type;
  901. u32 item_offset;
  902. u32 item_size;
  903. if (disk_item_offset + sizeof(struct btrfs_item) >
  904. sf->block_ctx->len) {
  905. leaf_item_out_of_bounce_error:
  906. pr_info("btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
  907. sf->block_ctx->start,
  908. sf->block_ctx->dev->name);
  909. goto one_stack_frame_backwards;
  910. }
  911. btrfsic_read_from_block_data(sf->block_ctx,
  912. &disk_item,
  913. disk_item_offset,
  914. sizeof(struct btrfs_item));
  915. item_offset = btrfs_stack_item_offset(&disk_item);
  916. item_size = btrfs_stack_item_size(&disk_item);
  917. disk_key = &disk_item.key;
  918. type = btrfs_disk_key_type(disk_key);
  919. if (BTRFS_ROOT_ITEM_KEY == type) {
  920. struct btrfs_root_item root_item;
  921. u32 root_item_offset;
  922. u64 next_bytenr;
  923. root_item_offset = item_offset +
  924. offsetof(struct btrfs_leaf, items);
  925. if (root_item_offset + item_size >
  926. sf->block_ctx->len)
  927. goto leaf_item_out_of_bounce_error;
  928. btrfsic_read_from_block_data(
  929. sf->block_ctx, &root_item,
  930. root_item_offset,
  931. item_size);
  932. next_bytenr = btrfs_root_bytenr(&root_item);
  933. sf->error =
  934. btrfsic_create_link_to_next_block(
  935. state,
  936. sf->block,
  937. sf->block_ctx,
  938. next_bytenr,
  939. sf->limit_nesting,
  940. &sf->next_block_ctx,
  941. &sf->next_block,
  942. force_iodone_flag,
  943. &sf->num_copies,
  944. &sf->mirror_num,
  945. disk_key,
  946. btrfs_root_generation(
  947. &root_item));
  948. if (sf->error)
  949. goto one_stack_frame_backwards;
  950. if (NULL != sf->next_block) {
  951. struct btrfs_header *const next_hdr =
  952. (struct btrfs_header *)
  953. sf->next_block_ctx.datav[0];
  954. next_stack =
  955. btrfsic_stack_frame_alloc();
  956. if (NULL == next_stack) {
  957. sf->error = -1;
  958. btrfsic_release_block_ctx(
  959. &sf->
  960. next_block_ctx);
  961. goto one_stack_frame_backwards;
  962. }
  963. next_stack->i = -1;
  964. next_stack->block = sf->next_block;
  965. next_stack->block_ctx =
  966. &sf->next_block_ctx;
  967. next_stack->next_block = NULL;
  968. next_stack->hdr = next_hdr;
  969. next_stack->limit_nesting =
  970. sf->limit_nesting - 1;
  971. next_stack->prev = sf;
  972. sf = next_stack;
  973. goto continue_with_new_stack_frame;
  974. }
  975. } else if (BTRFS_EXTENT_DATA_KEY == type &&
  976. state->include_extent_data) {
  977. sf->error = btrfsic_handle_extent_data(
  978. state,
  979. sf->block,
  980. sf->block_ctx,
  981. item_offset,
  982. force_iodone_flag);
  983. if (sf->error)
  984. goto one_stack_frame_backwards;
  985. }
  986. goto continue_with_current_leaf_stack_frame;
  987. }
  988. } else {
  989. struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
  990. if (-1 == sf->i) {
  991. sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
  992. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  993. pr_info("node %llu level %d items %d generation %llu owner %llu\n",
  994. sf->block_ctx->start,
  995. nodehdr->header.level, sf->nr,
  996. btrfs_stack_header_generation(
  997. &nodehdr->header),
  998. btrfs_stack_header_owner(
  999. &nodehdr->header));
  1000. }
  1001. continue_with_current_node_stack_frame:
  1002. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  1003. sf->i++;
  1004. sf->num_copies = 0;
  1005. }
  1006. if (sf->i < sf->nr) {
  1007. struct btrfs_key_ptr key_ptr;
  1008. u32 key_ptr_offset;
  1009. u64 next_bytenr;
  1010. key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
  1011. (uintptr_t)nodehdr;
  1012. if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
  1013. sf->block_ctx->len) {
  1014. pr_info("btrfsic: node item out of bounce at logical %llu, dev %s\n",
  1015. sf->block_ctx->start,
  1016. sf->block_ctx->dev->name);
  1017. goto one_stack_frame_backwards;
  1018. }
  1019. btrfsic_read_from_block_data(
  1020. sf->block_ctx, &key_ptr, key_ptr_offset,
  1021. sizeof(struct btrfs_key_ptr));
  1022. next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
  1023. sf->error = btrfsic_create_link_to_next_block(
  1024. state,
  1025. sf->block,
  1026. sf->block_ctx,
  1027. next_bytenr,
  1028. sf->limit_nesting,
  1029. &sf->next_block_ctx,
  1030. &sf->next_block,
  1031. force_iodone_flag,
  1032. &sf->num_copies,
  1033. &sf->mirror_num,
  1034. &key_ptr.key,
  1035. btrfs_stack_key_generation(&key_ptr));
  1036. if (sf->error)
  1037. goto one_stack_frame_backwards;
  1038. if (NULL != sf->next_block) {
  1039. struct btrfs_header *const next_hdr =
  1040. (struct btrfs_header *)
  1041. sf->next_block_ctx.datav[0];
  1042. next_stack = btrfsic_stack_frame_alloc();
  1043. if (NULL == next_stack) {
  1044. sf->error = -1;
  1045. goto one_stack_frame_backwards;
  1046. }
  1047. next_stack->i = -1;
  1048. next_stack->block = sf->next_block;
  1049. next_stack->block_ctx = &sf->next_block_ctx;
  1050. next_stack->next_block = NULL;
  1051. next_stack->hdr = next_hdr;
  1052. next_stack->limit_nesting =
  1053. sf->limit_nesting - 1;
  1054. next_stack->prev = sf;
  1055. sf = next_stack;
  1056. goto continue_with_new_stack_frame;
  1057. }
  1058. goto continue_with_current_node_stack_frame;
  1059. }
  1060. }
  1061. one_stack_frame_backwards:
  1062. if (NULL != sf->prev) {
  1063. struct btrfsic_stack_frame *const prev = sf->prev;
  1064. /* the one for the initial block is freed in the caller */
  1065. btrfsic_release_block_ctx(sf->block_ctx);
  1066. if (sf->error) {
  1067. prev->error = sf->error;
  1068. btrfsic_stack_frame_free(sf);
  1069. sf = prev;
  1070. goto one_stack_frame_backwards;
  1071. }
  1072. btrfsic_stack_frame_free(sf);
  1073. sf = prev;
  1074. goto continue_with_new_stack_frame;
  1075. } else {
  1076. BUG_ON(&initial_stack_frame != sf);
  1077. }
  1078. return sf->error;
  1079. }
  1080. static void btrfsic_read_from_block_data(
  1081. struct btrfsic_block_data_ctx *block_ctx,
  1082. void *dstv, u32 offset, size_t len)
  1083. {
  1084. size_t cur;
  1085. size_t offset_in_page;
  1086. char *kaddr;
  1087. char *dst = (char *)dstv;
  1088. size_t start_offset = block_ctx->start & ((u64)PAGE_SIZE - 1);
  1089. unsigned long i = (start_offset + offset) >> PAGE_SHIFT;
  1090. WARN_ON(offset + len > block_ctx->len);
  1091. offset_in_page = (start_offset + offset) & (PAGE_SIZE - 1);
  1092. while (len > 0) {
  1093. cur = min(len, ((size_t)PAGE_SIZE - offset_in_page));
  1094. BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE));
  1095. kaddr = block_ctx->datav[i];
  1096. memcpy(dst, kaddr + offset_in_page, cur);
  1097. dst += cur;
  1098. len -= cur;
  1099. offset_in_page = 0;
  1100. i++;
  1101. }
  1102. }
  1103. static int btrfsic_create_link_to_next_block(
  1104. struct btrfsic_state *state,
  1105. struct btrfsic_block *block,
  1106. struct btrfsic_block_data_ctx *block_ctx,
  1107. u64 next_bytenr,
  1108. int limit_nesting,
  1109. struct btrfsic_block_data_ctx *next_block_ctx,
  1110. struct btrfsic_block **next_blockp,
  1111. int force_iodone_flag,
  1112. int *num_copiesp, int *mirror_nump,
  1113. struct btrfs_disk_key *disk_key,
  1114. u64 parent_generation)
  1115. {
  1116. struct btrfs_fs_info *fs_info = state->fs_info;
  1117. struct btrfsic_block *next_block = NULL;
  1118. int ret;
  1119. struct btrfsic_block_link *l;
  1120. int did_alloc_block_link;
  1121. int block_was_created;
  1122. *next_blockp = NULL;
  1123. if (0 == *num_copiesp) {
  1124. *num_copiesp = btrfs_num_copies(fs_info, next_bytenr,
  1125. state->metablock_size);
  1126. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1127. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  1128. next_bytenr, *num_copiesp);
  1129. *mirror_nump = 1;
  1130. }
  1131. if (*mirror_nump > *num_copiesp)
  1132. return 0;
  1133. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1134. pr_info("btrfsic_create_link_to_next_block(mirror_num=%d)\n",
  1135. *mirror_nump);
  1136. ret = btrfsic_map_block(state, next_bytenr,
  1137. state->metablock_size,
  1138. next_block_ctx, *mirror_nump);
  1139. if (ret) {
  1140. pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  1141. next_bytenr, *mirror_nump);
  1142. btrfsic_release_block_ctx(next_block_ctx);
  1143. *next_blockp = NULL;
  1144. return -1;
  1145. }
  1146. next_block = btrfsic_block_lookup_or_add(state,
  1147. next_block_ctx, "referenced ",
  1148. 1, force_iodone_flag,
  1149. !force_iodone_flag,
  1150. *mirror_nump,
  1151. &block_was_created);
  1152. if (NULL == next_block) {
  1153. btrfsic_release_block_ctx(next_block_ctx);
  1154. *next_blockp = NULL;
  1155. return -1;
  1156. }
  1157. if (block_was_created) {
  1158. l = NULL;
  1159. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1160. } else {
  1161. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
  1162. if (next_block->logical_bytenr != next_bytenr &&
  1163. !(!next_block->is_metadata &&
  1164. 0 == next_block->logical_bytenr))
  1165. pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
  1166. next_bytenr, next_block_ctx->dev->name,
  1167. next_block_ctx->dev_bytenr, *mirror_nump,
  1168. btrfsic_get_block_type(state,
  1169. next_block),
  1170. next_block->logical_bytenr);
  1171. else
  1172. pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c.\n",
  1173. next_bytenr, next_block_ctx->dev->name,
  1174. next_block_ctx->dev_bytenr, *mirror_nump,
  1175. btrfsic_get_block_type(state,
  1176. next_block));
  1177. }
  1178. next_block->logical_bytenr = next_bytenr;
  1179. next_block->mirror_num = *mirror_nump;
  1180. l = btrfsic_block_link_hashtable_lookup(
  1181. next_block_ctx->dev->bdev,
  1182. next_block_ctx->dev_bytenr,
  1183. block_ctx->dev->bdev,
  1184. block_ctx->dev_bytenr,
  1185. &state->block_link_hashtable);
  1186. }
  1187. next_block->disk_key = *disk_key;
  1188. if (NULL == l) {
  1189. l = btrfsic_block_link_alloc();
  1190. if (NULL == l) {
  1191. pr_info("btrfsic: error, kmalloc failed!\n");
  1192. btrfsic_release_block_ctx(next_block_ctx);
  1193. *next_blockp = NULL;
  1194. return -1;
  1195. }
  1196. did_alloc_block_link = 1;
  1197. l->block_ref_to = next_block;
  1198. l->block_ref_from = block;
  1199. l->ref_cnt = 1;
  1200. l->parent_generation = parent_generation;
  1201. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1202. btrfsic_print_add_link(state, l);
  1203. list_add(&l->node_ref_to, &block->ref_to_list);
  1204. list_add(&l->node_ref_from, &next_block->ref_from_list);
  1205. btrfsic_block_link_hashtable_add(l,
  1206. &state->block_link_hashtable);
  1207. } else {
  1208. did_alloc_block_link = 0;
  1209. if (0 == limit_nesting) {
  1210. l->ref_cnt++;
  1211. l->parent_generation = parent_generation;
  1212. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1213. btrfsic_print_add_link(state, l);
  1214. }
  1215. }
  1216. if (limit_nesting > 0 && did_alloc_block_link) {
  1217. ret = btrfsic_read_block(state, next_block_ctx);
  1218. if (ret < (int)next_block_ctx->len) {
  1219. pr_info("btrfsic: read block @logical %llu failed!\n",
  1220. next_bytenr);
  1221. btrfsic_release_block_ctx(next_block_ctx);
  1222. *next_blockp = NULL;
  1223. return -1;
  1224. }
  1225. *next_blockp = next_block;
  1226. } else {
  1227. *next_blockp = NULL;
  1228. }
  1229. (*mirror_nump)++;
  1230. return 0;
  1231. }
  1232. static int btrfsic_handle_extent_data(
  1233. struct btrfsic_state *state,
  1234. struct btrfsic_block *block,
  1235. struct btrfsic_block_data_ctx *block_ctx,
  1236. u32 item_offset, int force_iodone_flag)
  1237. {
  1238. struct btrfs_fs_info *fs_info = state->fs_info;
  1239. struct btrfs_file_extent_item file_extent_item;
  1240. u64 file_extent_item_offset;
  1241. u64 next_bytenr;
  1242. u64 num_bytes;
  1243. u64 generation;
  1244. struct btrfsic_block_link *l;
  1245. int ret;
  1246. file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
  1247. item_offset;
  1248. if (file_extent_item_offset +
  1249. offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
  1250. block_ctx->len) {
  1251. pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1252. block_ctx->start, block_ctx->dev->name);
  1253. return -1;
  1254. }
  1255. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1256. file_extent_item_offset,
  1257. offsetof(struct btrfs_file_extent_item, disk_num_bytes));
  1258. if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
  1259. btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
  1260. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1261. pr_info("extent_data: type %u, disk_bytenr = %llu\n",
  1262. file_extent_item.type,
  1263. btrfs_stack_file_extent_disk_bytenr(
  1264. &file_extent_item));
  1265. return 0;
  1266. }
  1267. if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
  1268. block_ctx->len) {
  1269. pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1270. block_ctx->start, block_ctx->dev->name);
  1271. return -1;
  1272. }
  1273. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1274. file_extent_item_offset,
  1275. sizeof(struct btrfs_file_extent_item));
  1276. next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
  1277. if (btrfs_stack_file_extent_compression(&file_extent_item) ==
  1278. BTRFS_COMPRESS_NONE) {
  1279. next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
  1280. num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
  1281. } else {
  1282. num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
  1283. }
  1284. generation = btrfs_stack_file_extent_generation(&file_extent_item);
  1285. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1286. pr_info("extent_data: type %u, disk_bytenr = %llu, offset = %llu, num_bytes = %llu\n",
  1287. file_extent_item.type,
  1288. btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
  1289. btrfs_stack_file_extent_offset(&file_extent_item),
  1290. num_bytes);
  1291. while (num_bytes > 0) {
  1292. u32 chunk_len;
  1293. int num_copies;
  1294. int mirror_num;
  1295. if (num_bytes > state->datablock_size)
  1296. chunk_len = state->datablock_size;
  1297. else
  1298. chunk_len = num_bytes;
  1299. num_copies = btrfs_num_copies(fs_info, next_bytenr,
  1300. state->datablock_size);
  1301. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1302. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  1303. next_bytenr, num_copies);
  1304. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  1305. struct btrfsic_block_data_ctx next_block_ctx;
  1306. struct btrfsic_block *next_block;
  1307. int block_was_created;
  1308. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1309. pr_info("btrfsic_handle_extent_data(mirror_num=%d)\n",
  1310. mirror_num);
  1311. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1312. pr_info("\tdisk_bytenr = %llu, num_bytes %u\n",
  1313. next_bytenr, chunk_len);
  1314. ret = btrfsic_map_block(state, next_bytenr,
  1315. chunk_len, &next_block_ctx,
  1316. mirror_num);
  1317. if (ret) {
  1318. pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  1319. next_bytenr, mirror_num);
  1320. return -1;
  1321. }
  1322. next_block = btrfsic_block_lookup_or_add(
  1323. state,
  1324. &next_block_ctx,
  1325. "referenced ",
  1326. 0,
  1327. force_iodone_flag,
  1328. !force_iodone_flag,
  1329. mirror_num,
  1330. &block_was_created);
  1331. if (NULL == next_block) {
  1332. pr_info("btrfsic: error, kmalloc failed!\n");
  1333. btrfsic_release_block_ctx(&next_block_ctx);
  1334. return -1;
  1335. }
  1336. if (!block_was_created) {
  1337. if ((state->print_mask &
  1338. BTRFSIC_PRINT_MASK_VERBOSE) &&
  1339. next_block->logical_bytenr != next_bytenr &&
  1340. !(!next_block->is_metadata &&
  1341. 0 == next_block->logical_bytenr)) {
  1342. pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, D, bytenr mismatch (!= stored %llu).\n",
  1343. next_bytenr,
  1344. next_block_ctx.dev->name,
  1345. next_block_ctx.dev_bytenr,
  1346. mirror_num,
  1347. next_block->logical_bytenr);
  1348. }
  1349. next_block->logical_bytenr = next_bytenr;
  1350. next_block->mirror_num = mirror_num;
  1351. }
  1352. l = btrfsic_block_link_lookup_or_add(state,
  1353. &next_block_ctx,
  1354. next_block, block,
  1355. generation);
  1356. btrfsic_release_block_ctx(&next_block_ctx);
  1357. if (NULL == l)
  1358. return -1;
  1359. }
  1360. next_bytenr += chunk_len;
  1361. num_bytes -= chunk_len;
  1362. }
  1363. return 0;
  1364. }
  1365. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  1366. struct btrfsic_block_data_ctx *block_ctx_out,
  1367. int mirror_num)
  1368. {
  1369. struct btrfs_fs_info *fs_info = state->fs_info;
  1370. int ret;
  1371. u64 length;
  1372. struct btrfs_bio *multi = NULL;
  1373. struct btrfs_device *device;
  1374. length = len;
  1375. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
  1376. bytenr, &length, &multi, mirror_num);
  1377. if (ret) {
  1378. block_ctx_out->start = 0;
  1379. block_ctx_out->dev_bytenr = 0;
  1380. block_ctx_out->len = 0;
  1381. block_ctx_out->dev = NULL;
  1382. block_ctx_out->datav = NULL;
  1383. block_ctx_out->pagev = NULL;
  1384. block_ctx_out->mem_to_free = NULL;
  1385. return ret;
  1386. }
  1387. device = multi->stripes[0].dev;
  1388. block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev->bd_dev);
  1389. block_ctx_out->dev_bytenr = multi->stripes[0].physical;
  1390. block_ctx_out->start = bytenr;
  1391. block_ctx_out->len = len;
  1392. block_ctx_out->datav = NULL;
  1393. block_ctx_out->pagev = NULL;
  1394. block_ctx_out->mem_to_free = NULL;
  1395. kfree(multi);
  1396. if (NULL == block_ctx_out->dev) {
  1397. ret = -ENXIO;
  1398. pr_info("btrfsic: error, cannot lookup dev (#1)!\n");
  1399. }
  1400. return ret;
  1401. }
  1402. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
  1403. {
  1404. if (block_ctx->mem_to_free) {
  1405. unsigned int num_pages;
  1406. BUG_ON(!block_ctx->datav);
  1407. BUG_ON(!block_ctx->pagev);
  1408. num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
  1409. PAGE_SHIFT;
  1410. while (num_pages > 0) {
  1411. num_pages--;
  1412. if (block_ctx->datav[num_pages]) {
  1413. kunmap(block_ctx->pagev[num_pages]);
  1414. block_ctx->datav[num_pages] = NULL;
  1415. }
  1416. if (block_ctx->pagev[num_pages]) {
  1417. __free_page(block_ctx->pagev[num_pages]);
  1418. block_ctx->pagev[num_pages] = NULL;
  1419. }
  1420. }
  1421. kfree(block_ctx->mem_to_free);
  1422. block_ctx->mem_to_free = NULL;
  1423. block_ctx->pagev = NULL;
  1424. block_ctx->datav = NULL;
  1425. }
  1426. }
  1427. static int btrfsic_read_block(struct btrfsic_state *state,
  1428. struct btrfsic_block_data_ctx *block_ctx)
  1429. {
  1430. unsigned int num_pages;
  1431. unsigned int i;
  1432. u64 dev_bytenr;
  1433. int ret;
  1434. BUG_ON(block_ctx->datav);
  1435. BUG_ON(block_ctx->pagev);
  1436. BUG_ON(block_ctx->mem_to_free);
  1437. if (block_ctx->dev_bytenr & ((u64)PAGE_SIZE - 1)) {
  1438. pr_info("btrfsic: read_block() with unaligned bytenr %llu\n",
  1439. block_ctx->dev_bytenr);
  1440. return -1;
  1441. }
  1442. num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
  1443. PAGE_SHIFT;
  1444. block_ctx->mem_to_free = kcalloc(sizeof(*block_ctx->datav) +
  1445. sizeof(*block_ctx->pagev),
  1446. num_pages, GFP_NOFS);
  1447. if (!block_ctx->mem_to_free)
  1448. return -ENOMEM;
  1449. block_ctx->datav = block_ctx->mem_to_free;
  1450. block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
  1451. for (i = 0; i < num_pages; i++) {
  1452. block_ctx->pagev[i] = alloc_page(GFP_NOFS);
  1453. if (!block_ctx->pagev[i])
  1454. return -1;
  1455. }
  1456. dev_bytenr = block_ctx->dev_bytenr;
  1457. for (i = 0; i < num_pages;) {
  1458. struct bio *bio;
  1459. unsigned int j;
  1460. bio = btrfs_io_bio_alloc(num_pages - i);
  1461. bio_set_dev(bio, block_ctx->dev->bdev);
  1462. bio->bi_iter.bi_sector = dev_bytenr >> 9;
  1463. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  1464. for (j = i; j < num_pages; j++) {
  1465. ret = bio_add_page(bio, block_ctx->pagev[j],
  1466. PAGE_SIZE, 0);
  1467. if (PAGE_SIZE != ret)
  1468. break;
  1469. }
  1470. if (j == i) {
  1471. pr_info("btrfsic: error, failed to add a single page!\n");
  1472. return -1;
  1473. }
  1474. if (submit_bio_wait(bio)) {
  1475. pr_info("btrfsic: read error at logical %llu dev %s!\n",
  1476. block_ctx->start, block_ctx->dev->name);
  1477. bio_put(bio);
  1478. return -1;
  1479. }
  1480. bio_put(bio);
  1481. dev_bytenr += (j - i) * PAGE_SIZE;
  1482. i = j;
  1483. }
  1484. for (i = 0; i < num_pages; i++)
  1485. block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
  1486. return block_ctx->len;
  1487. }
  1488. static void btrfsic_dump_database(struct btrfsic_state *state)
  1489. {
  1490. const struct btrfsic_block *b_all;
  1491. BUG_ON(NULL == state);
  1492. pr_info("all_blocks_list:\n");
  1493. list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
  1494. const struct btrfsic_block_link *l;
  1495. pr_info("%c-block @%llu (%s/%llu/%d)\n",
  1496. btrfsic_get_block_type(state, b_all),
  1497. b_all->logical_bytenr, b_all->dev_state->name,
  1498. b_all->dev_bytenr, b_all->mirror_num);
  1499. list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
  1500. pr_info(" %c @%llu (%s/%llu/%d) refers %u* to %c @%llu (%s/%llu/%d)\n",
  1501. btrfsic_get_block_type(state, b_all),
  1502. b_all->logical_bytenr, b_all->dev_state->name,
  1503. b_all->dev_bytenr, b_all->mirror_num,
  1504. l->ref_cnt,
  1505. btrfsic_get_block_type(state, l->block_ref_to),
  1506. l->block_ref_to->logical_bytenr,
  1507. l->block_ref_to->dev_state->name,
  1508. l->block_ref_to->dev_bytenr,
  1509. l->block_ref_to->mirror_num);
  1510. }
  1511. list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
  1512. pr_info(" %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
  1513. btrfsic_get_block_type(state, b_all),
  1514. b_all->logical_bytenr, b_all->dev_state->name,
  1515. b_all->dev_bytenr, b_all->mirror_num,
  1516. l->ref_cnt,
  1517. btrfsic_get_block_type(state, l->block_ref_from),
  1518. l->block_ref_from->logical_bytenr,
  1519. l->block_ref_from->dev_state->name,
  1520. l->block_ref_from->dev_bytenr,
  1521. l->block_ref_from->mirror_num);
  1522. }
  1523. pr_info("\n");
  1524. }
  1525. }
  1526. /*
  1527. * Test whether the disk block contains a tree block (leaf or node)
  1528. * (note that this test fails for the super block)
  1529. */
  1530. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  1531. char **datav, unsigned int num_pages)
  1532. {
  1533. struct btrfs_fs_info *fs_info = state->fs_info;
  1534. struct btrfs_header *h;
  1535. u8 csum[BTRFS_CSUM_SIZE];
  1536. u32 crc = ~(u32)0;
  1537. unsigned int i;
  1538. if (num_pages * PAGE_SIZE < state->metablock_size)
  1539. return 1; /* not metadata */
  1540. num_pages = state->metablock_size >> PAGE_SHIFT;
  1541. h = (struct btrfs_header *)datav[0];
  1542. if (memcmp(h->fsid, fs_info->fsid, BTRFS_FSID_SIZE))
  1543. return 1;
  1544. for (i = 0; i < num_pages; i++) {
  1545. u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
  1546. size_t sublen = i ? PAGE_SIZE :
  1547. (PAGE_SIZE - BTRFS_CSUM_SIZE);
  1548. crc = crc32c(crc, data, sublen);
  1549. }
  1550. btrfs_csum_final(crc, csum);
  1551. if (memcmp(csum, h->csum, state->csum_size))
  1552. return 1;
  1553. return 0; /* is metadata */
  1554. }
  1555. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  1556. u64 dev_bytenr, char **mapped_datav,
  1557. unsigned int num_pages,
  1558. struct bio *bio, int *bio_is_patched,
  1559. struct buffer_head *bh,
  1560. int submit_bio_bh_rw)
  1561. {
  1562. int is_metadata;
  1563. struct btrfsic_block *block;
  1564. struct btrfsic_block_data_ctx block_ctx;
  1565. int ret;
  1566. struct btrfsic_state *state = dev_state->state;
  1567. struct block_device *bdev = dev_state->bdev;
  1568. unsigned int processed_len;
  1569. if (NULL != bio_is_patched)
  1570. *bio_is_patched = 0;
  1571. again:
  1572. if (num_pages == 0)
  1573. return;
  1574. processed_len = 0;
  1575. is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
  1576. num_pages));
  1577. block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
  1578. &state->block_hashtable);
  1579. if (NULL != block) {
  1580. u64 bytenr = 0;
  1581. struct btrfsic_block_link *l, *tmp;
  1582. if (block->is_superblock) {
  1583. bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
  1584. mapped_datav[0]);
  1585. if (num_pages * PAGE_SIZE <
  1586. BTRFS_SUPER_INFO_SIZE) {
  1587. pr_info("btrfsic: cannot work with too short bios!\n");
  1588. return;
  1589. }
  1590. is_metadata = 1;
  1591. BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_SIZE - 1));
  1592. processed_len = BTRFS_SUPER_INFO_SIZE;
  1593. if (state->print_mask &
  1594. BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
  1595. pr_info("[before new superblock is written]:\n");
  1596. btrfsic_dump_tree_sub(state, block, 0);
  1597. }
  1598. }
  1599. if (is_metadata) {
  1600. if (!block->is_superblock) {
  1601. if (num_pages * PAGE_SIZE <
  1602. state->metablock_size) {
  1603. pr_info("btrfsic: cannot work with too short bios!\n");
  1604. return;
  1605. }
  1606. processed_len = state->metablock_size;
  1607. bytenr = btrfs_stack_header_bytenr(
  1608. (struct btrfs_header *)
  1609. mapped_datav[0]);
  1610. btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
  1611. dev_state,
  1612. dev_bytenr);
  1613. }
  1614. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
  1615. if (block->logical_bytenr != bytenr &&
  1616. !(!block->is_metadata &&
  1617. block->logical_bytenr == 0))
  1618. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
  1619. bytenr, dev_state->name,
  1620. dev_bytenr,
  1621. block->mirror_num,
  1622. btrfsic_get_block_type(state,
  1623. block),
  1624. block->logical_bytenr);
  1625. else
  1626. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
  1627. bytenr, dev_state->name,
  1628. dev_bytenr, block->mirror_num,
  1629. btrfsic_get_block_type(state,
  1630. block));
  1631. }
  1632. block->logical_bytenr = bytenr;
  1633. } else {
  1634. if (num_pages * PAGE_SIZE <
  1635. state->datablock_size) {
  1636. pr_info("btrfsic: cannot work with too short bios!\n");
  1637. return;
  1638. }
  1639. processed_len = state->datablock_size;
  1640. bytenr = block->logical_bytenr;
  1641. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1642. pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
  1643. bytenr, dev_state->name, dev_bytenr,
  1644. block->mirror_num,
  1645. btrfsic_get_block_type(state, block));
  1646. }
  1647. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1648. pr_info("ref_to_list: %cE, ref_from_list: %cE\n",
  1649. list_empty(&block->ref_to_list) ? ' ' : '!',
  1650. list_empty(&block->ref_from_list) ? ' ' : '!');
  1651. if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
  1652. pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), old(gen=%llu, objectid=%llu, type=%d, offset=%llu), new(gen=%llu), which is referenced by most recent superblock (superblockgen=%llu)!\n",
  1653. btrfsic_get_block_type(state, block), bytenr,
  1654. dev_state->name, dev_bytenr, block->mirror_num,
  1655. block->generation,
  1656. btrfs_disk_key_objectid(&block->disk_key),
  1657. block->disk_key.type,
  1658. btrfs_disk_key_offset(&block->disk_key),
  1659. btrfs_stack_header_generation(
  1660. (struct btrfs_header *) mapped_datav[0]),
  1661. state->max_superblock_generation);
  1662. btrfsic_dump_tree(state);
  1663. }
  1664. if (!block->is_iodone && !block->never_written) {
  1665. pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu, which is not yet iodone!\n",
  1666. btrfsic_get_block_type(state, block), bytenr,
  1667. dev_state->name, dev_bytenr, block->mirror_num,
  1668. block->generation,
  1669. btrfs_stack_header_generation(
  1670. (struct btrfs_header *)
  1671. mapped_datav[0]));
  1672. /* it would not be safe to go on */
  1673. btrfsic_dump_tree(state);
  1674. goto continue_loop;
  1675. }
  1676. /*
  1677. * Clear all references of this block. Do not free
  1678. * the block itself even if is not referenced anymore
  1679. * because it still carries valuable information
  1680. * like whether it was ever written and IO completed.
  1681. */
  1682. list_for_each_entry_safe(l, tmp, &block->ref_to_list,
  1683. node_ref_to) {
  1684. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1685. btrfsic_print_rem_link(state, l);
  1686. l->ref_cnt--;
  1687. if (0 == l->ref_cnt) {
  1688. list_del(&l->node_ref_to);
  1689. list_del(&l->node_ref_from);
  1690. btrfsic_block_link_hashtable_remove(l);
  1691. btrfsic_block_link_free(l);
  1692. }
  1693. }
  1694. block_ctx.dev = dev_state;
  1695. block_ctx.dev_bytenr = dev_bytenr;
  1696. block_ctx.start = bytenr;
  1697. block_ctx.len = processed_len;
  1698. block_ctx.pagev = NULL;
  1699. block_ctx.mem_to_free = NULL;
  1700. block_ctx.datav = mapped_datav;
  1701. if (is_metadata || state->include_extent_data) {
  1702. block->never_written = 0;
  1703. block->iodone_w_error = 0;
  1704. if (NULL != bio) {
  1705. block->is_iodone = 0;
  1706. BUG_ON(NULL == bio_is_patched);
  1707. if (!*bio_is_patched) {
  1708. block->orig_bio_bh_private =
  1709. bio->bi_private;
  1710. block->orig_bio_bh_end_io.bio =
  1711. bio->bi_end_io;
  1712. block->next_in_same_bio = NULL;
  1713. bio->bi_private = block;
  1714. bio->bi_end_io = btrfsic_bio_end_io;
  1715. *bio_is_patched = 1;
  1716. } else {
  1717. struct btrfsic_block *chained_block =
  1718. (struct btrfsic_block *)
  1719. bio->bi_private;
  1720. BUG_ON(NULL == chained_block);
  1721. block->orig_bio_bh_private =
  1722. chained_block->orig_bio_bh_private;
  1723. block->orig_bio_bh_end_io.bio =
  1724. chained_block->orig_bio_bh_end_io.
  1725. bio;
  1726. block->next_in_same_bio = chained_block;
  1727. bio->bi_private = block;
  1728. }
  1729. } else if (NULL != bh) {
  1730. block->is_iodone = 0;
  1731. block->orig_bio_bh_private = bh->b_private;
  1732. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1733. block->next_in_same_bio = NULL;
  1734. bh->b_private = block;
  1735. bh->b_end_io = btrfsic_bh_end_io;
  1736. } else {
  1737. block->is_iodone = 1;
  1738. block->orig_bio_bh_private = NULL;
  1739. block->orig_bio_bh_end_io.bio = NULL;
  1740. block->next_in_same_bio = NULL;
  1741. }
  1742. }
  1743. block->flush_gen = dev_state->last_flush_gen + 1;
  1744. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1745. if (is_metadata) {
  1746. block->logical_bytenr = bytenr;
  1747. block->is_metadata = 1;
  1748. if (block->is_superblock) {
  1749. BUG_ON(PAGE_SIZE !=
  1750. BTRFS_SUPER_INFO_SIZE);
  1751. ret = btrfsic_process_written_superblock(
  1752. state,
  1753. block,
  1754. (struct btrfs_super_block *)
  1755. mapped_datav[0]);
  1756. if (state->print_mask &
  1757. BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
  1758. pr_info("[after new superblock is written]:\n");
  1759. btrfsic_dump_tree_sub(state, block, 0);
  1760. }
  1761. } else {
  1762. block->mirror_num = 0; /* unknown */
  1763. ret = btrfsic_process_metablock(
  1764. state,
  1765. block,
  1766. &block_ctx,
  1767. 0, 0);
  1768. }
  1769. if (ret)
  1770. pr_info("btrfsic: btrfsic_process_metablock(root @%llu) failed!\n",
  1771. dev_bytenr);
  1772. } else {
  1773. block->is_metadata = 0;
  1774. block->mirror_num = 0; /* unknown */
  1775. block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1776. if (!state->include_extent_data
  1777. && list_empty(&block->ref_from_list)) {
  1778. /*
  1779. * disk block is overwritten with extent
  1780. * data (not meta data) and we are configured
  1781. * to not include extent data: take the
  1782. * chance and free the block's memory
  1783. */
  1784. btrfsic_block_hashtable_remove(block);
  1785. list_del(&block->all_blocks_node);
  1786. btrfsic_block_free(block);
  1787. }
  1788. }
  1789. btrfsic_release_block_ctx(&block_ctx);
  1790. } else {
  1791. /* block has not been found in hash table */
  1792. u64 bytenr;
  1793. if (!is_metadata) {
  1794. processed_len = state->datablock_size;
  1795. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1796. pr_info("Written block (%s/%llu/?) !found in hash table, D.\n",
  1797. dev_state->name, dev_bytenr);
  1798. if (!state->include_extent_data) {
  1799. /* ignore that written D block */
  1800. goto continue_loop;
  1801. }
  1802. /* this is getting ugly for the
  1803. * include_extent_data case... */
  1804. bytenr = 0; /* unknown */
  1805. } else {
  1806. processed_len = state->metablock_size;
  1807. bytenr = btrfs_stack_header_bytenr(
  1808. (struct btrfs_header *)
  1809. mapped_datav[0]);
  1810. btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
  1811. dev_bytenr);
  1812. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1813. pr_info("Written block @%llu (%s/%llu/?) !found in hash table, M.\n",
  1814. bytenr, dev_state->name, dev_bytenr);
  1815. }
  1816. block_ctx.dev = dev_state;
  1817. block_ctx.dev_bytenr = dev_bytenr;
  1818. block_ctx.start = bytenr;
  1819. block_ctx.len = processed_len;
  1820. block_ctx.pagev = NULL;
  1821. block_ctx.mem_to_free = NULL;
  1822. block_ctx.datav = mapped_datav;
  1823. block = btrfsic_block_alloc();
  1824. if (NULL == block) {
  1825. pr_info("btrfsic: error, kmalloc failed!\n");
  1826. btrfsic_release_block_ctx(&block_ctx);
  1827. goto continue_loop;
  1828. }
  1829. block->dev_state = dev_state;
  1830. block->dev_bytenr = dev_bytenr;
  1831. block->logical_bytenr = bytenr;
  1832. block->is_metadata = is_metadata;
  1833. block->never_written = 0;
  1834. block->iodone_w_error = 0;
  1835. block->mirror_num = 0; /* unknown */
  1836. block->flush_gen = dev_state->last_flush_gen + 1;
  1837. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1838. if (NULL != bio) {
  1839. block->is_iodone = 0;
  1840. BUG_ON(NULL == bio_is_patched);
  1841. if (!*bio_is_patched) {
  1842. block->orig_bio_bh_private = bio->bi_private;
  1843. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  1844. block->next_in_same_bio = NULL;
  1845. bio->bi_private = block;
  1846. bio->bi_end_io = btrfsic_bio_end_io;
  1847. *bio_is_patched = 1;
  1848. } else {
  1849. struct btrfsic_block *chained_block =
  1850. (struct btrfsic_block *)
  1851. bio->bi_private;
  1852. BUG_ON(NULL == chained_block);
  1853. block->orig_bio_bh_private =
  1854. chained_block->orig_bio_bh_private;
  1855. block->orig_bio_bh_end_io.bio =
  1856. chained_block->orig_bio_bh_end_io.bio;
  1857. block->next_in_same_bio = chained_block;
  1858. bio->bi_private = block;
  1859. }
  1860. } else if (NULL != bh) {
  1861. block->is_iodone = 0;
  1862. block->orig_bio_bh_private = bh->b_private;
  1863. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1864. block->next_in_same_bio = NULL;
  1865. bh->b_private = block;
  1866. bh->b_end_io = btrfsic_bh_end_io;
  1867. } else {
  1868. block->is_iodone = 1;
  1869. block->orig_bio_bh_private = NULL;
  1870. block->orig_bio_bh_end_io.bio = NULL;
  1871. block->next_in_same_bio = NULL;
  1872. }
  1873. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1874. pr_info("New written %c-block @%llu (%s/%llu/%d)\n",
  1875. is_metadata ? 'M' : 'D',
  1876. block->logical_bytenr, block->dev_state->name,
  1877. block->dev_bytenr, block->mirror_num);
  1878. list_add(&block->all_blocks_node, &state->all_blocks_list);
  1879. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  1880. if (is_metadata) {
  1881. ret = btrfsic_process_metablock(state, block,
  1882. &block_ctx, 0, 0);
  1883. if (ret)
  1884. pr_info("btrfsic: process_metablock(root @%llu) failed!\n",
  1885. dev_bytenr);
  1886. }
  1887. btrfsic_release_block_ctx(&block_ctx);
  1888. }
  1889. continue_loop:
  1890. BUG_ON(!processed_len);
  1891. dev_bytenr += processed_len;
  1892. mapped_datav += processed_len >> PAGE_SHIFT;
  1893. num_pages -= processed_len >> PAGE_SHIFT;
  1894. goto again;
  1895. }
  1896. static void btrfsic_bio_end_io(struct bio *bp)
  1897. {
  1898. struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
  1899. int iodone_w_error;
  1900. /* mutex is not held! This is not save if IO is not yet completed
  1901. * on umount */
  1902. iodone_w_error = 0;
  1903. if (bp->bi_status)
  1904. iodone_w_error = 1;
  1905. BUG_ON(NULL == block);
  1906. bp->bi_private = block->orig_bio_bh_private;
  1907. bp->bi_end_io = block->orig_bio_bh_end_io.bio;
  1908. do {
  1909. struct btrfsic_block *next_block;
  1910. struct btrfsic_dev_state *const dev_state = block->dev_state;
  1911. if ((dev_state->state->print_mask &
  1912. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1913. pr_info("bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
  1914. bp->bi_status,
  1915. btrfsic_get_block_type(dev_state->state, block),
  1916. block->logical_bytenr, dev_state->name,
  1917. block->dev_bytenr, block->mirror_num);
  1918. next_block = block->next_in_same_bio;
  1919. block->iodone_w_error = iodone_w_error;
  1920. if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
  1921. dev_state->last_flush_gen++;
  1922. if ((dev_state->state->print_mask &
  1923. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1924. pr_info("bio_end_io() new %s flush_gen=%llu\n",
  1925. dev_state->name,
  1926. dev_state->last_flush_gen);
  1927. }
  1928. if (block->submit_bio_bh_rw & REQ_FUA)
  1929. block->flush_gen = 0; /* FUA completed means block is
  1930. * on disk */
  1931. block->is_iodone = 1; /* for FLUSH, this releases the block */
  1932. block = next_block;
  1933. } while (NULL != block);
  1934. bp->bi_end_io(bp);
  1935. }
  1936. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
  1937. {
  1938. struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
  1939. int iodone_w_error = !uptodate;
  1940. struct btrfsic_dev_state *dev_state;
  1941. BUG_ON(NULL == block);
  1942. dev_state = block->dev_state;
  1943. if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1944. pr_info("bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
  1945. iodone_w_error,
  1946. btrfsic_get_block_type(dev_state->state, block),
  1947. block->logical_bytenr, block->dev_state->name,
  1948. block->dev_bytenr, block->mirror_num);
  1949. block->iodone_w_error = iodone_w_error;
  1950. if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
  1951. dev_state->last_flush_gen++;
  1952. if ((dev_state->state->print_mask &
  1953. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  1954. pr_info("bh_end_io() new %s flush_gen=%llu\n",
  1955. dev_state->name, dev_state->last_flush_gen);
  1956. }
  1957. if (block->submit_bio_bh_rw & REQ_FUA)
  1958. block->flush_gen = 0; /* FUA completed means block is on disk */
  1959. bh->b_private = block->orig_bio_bh_private;
  1960. bh->b_end_io = block->orig_bio_bh_end_io.bh;
  1961. block->is_iodone = 1; /* for FLUSH, this releases the block */
  1962. bh->b_end_io(bh, uptodate);
  1963. }
  1964. static int btrfsic_process_written_superblock(
  1965. struct btrfsic_state *state,
  1966. struct btrfsic_block *const superblock,
  1967. struct btrfs_super_block *const super_hdr)
  1968. {
  1969. struct btrfs_fs_info *fs_info = state->fs_info;
  1970. int pass;
  1971. superblock->generation = btrfs_super_generation(super_hdr);
  1972. if (!(superblock->generation > state->max_superblock_generation ||
  1973. 0 == state->max_superblock_generation)) {
  1974. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  1975. pr_info("btrfsic: superblock @%llu (%s/%llu/%d) with old gen %llu <= %llu\n",
  1976. superblock->logical_bytenr,
  1977. superblock->dev_state->name,
  1978. superblock->dev_bytenr, superblock->mirror_num,
  1979. btrfs_super_generation(super_hdr),
  1980. state->max_superblock_generation);
  1981. } else {
  1982. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  1983. pr_info("btrfsic: got new superblock @%llu (%s/%llu/%d) with new gen %llu > %llu\n",
  1984. superblock->logical_bytenr,
  1985. superblock->dev_state->name,
  1986. superblock->dev_bytenr, superblock->mirror_num,
  1987. btrfs_super_generation(super_hdr),
  1988. state->max_superblock_generation);
  1989. state->max_superblock_generation =
  1990. btrfs_super_generation(super_hdr);
  1991. state->latest_superblock = superblock;
  1992. }
  1993. for (pass = 0; pass < 3; pass++) {
  1994. int ret;
  1995. u64 next_bytenr;
  1996. struct btrfsic_block *next_block;
  1997. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  1998. struct btrfsic_block_link *l;
  1999. int num_copies;
  2000. int mirror_num;
  2001. const char *additional_string = NULL;
  2002. struct btrfs_disk_key tmp_disk_key = {0};
  2003. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2004. BTRFS_ROOT_ITEM_KEY);
  2005. btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
  2006. switch (pass) {
  2007. case 0:
  2008. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2009. BTRFS_ROOT_TREE_OBJECTID);
  2010. additional_string = "root ";
  2011. next_bytenr = btrfs_super_root(super_hdr);
  2012. if (state->print_mask &
  2013. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2014. pr_info("root@%llu\n", next_bytenr);
  2015. break;
  2016. case 1:
  2017. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2018. BTRFS_CHUNK_TREE_OBJECTID);
  2019. additional_string = "chunk ";
  2020. next_bytenr = btrfs_super_chunk_root(super_hdr);
  2021. if (state->print_mask &
  2022. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2023. pr_info("chunk@%llu\n", next_bytenr);
  2024. break;
  2025. case 2:
  2026. btrfs_set_disk_key_objectid(&tmp_disk_key,
  2027. BTRFS_TREE_LOG_OBJECTID);
  2028. additional_string = "log ";
  2029. next_bytenr = btrfs_super_log_root(super_hdr);
  2030. if (0 == next_bytenr)
  2031. continue;
  2032. if (state->print_mask &
  2033. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2034. pr_info("log@%llu\n", next_bytenr);
  2035. break;
  2036. }
  2037. num_copies = btrfs_num_copies(fs_info, next_bytenr,
  2038. BTRFS_SUPER_INFO_SIZE);
  2039. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  2040. pr_info("num_copies(log_bytenr=%llu) = %d\n",
  2041. next_bytenr, num_copies);
  2042. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2043. int was_created;
  2044. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2045. pr_info("btrfsic_process_written_superblock(mirror_num=%d)\n", mirror_num);
  2046. ret = btrfsic_map_block(state, next_bytenr,
  2047. BTRFS_SUPER_INFO_SIZE,
  2048. &tmp_next_block_ctx,
  2049. mirror_num);
  2050. if (ret) {
  2051. pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  2052. next_bytenr, mirror_num);
  2053. return -1;
  2054. }
  2055. next_block = btrfsic_block_lookup_or_add(
  2056. state,
  2057. &tmp_next_block_ctx,
  2058. additional_string,
  2059. 1, 0, 1,
  2060. mirror_num,
  2061. &was_created);
  2062. if (NULL == next_block) {
  2063. pr_info("btrfsic: error, kmalloc failed!\n");
  2064. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2065. return -1;
  2066. }
  2067. next_block->disk_key = tmp_disk_key;
  2068. if (was_created)
  2069. next_block->generation =
  2070. BTRFSIC_GENERATION_UNKNOWN;
  2071. l = btrfsic_block_link_lookup_or_add(
  2072. state,
  2073. &tmp_next_block_ctx,
  2074. next_block,
  2075. superblock,
  2076. BTRFSIC_GENERATION_UNKNOWN);
  2077. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2078. if (NULL == l)
  2079. return -1;
  2080. }
  2081. }
  2082. if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
  2083. btrfsic_dump_tree(state);
  2084. return 0;
  2085. }
  2086. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  2087. struct btrfsic_block *const block,
  2088. int recursion_level)
  2089. {
  2090. const struct btrfsic_block_link *l;
  2091. int ret = 0;
  2092. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2093. /*
  2094. * Note that this situation can happen and does not
  2095. * indicate an error in regular cases. It happens
  2096. * when disk blocks are freed and later reused.
  2097. * The check-integrity module is not aware of any
  2098. * block free operations, it just recognizes block
  2099. * write operations. Therefore it keeps the linkage
  2100. * information for a block until a block is
  2101. * rewritten. This can temporarily cause incorrect
  2102. * and even circular linkage informations. This
  2103. * causes no harm unless such blocks are referenced
  2104. * by the most recent super block.
  2105. */
  2106. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2107. pr_info("btrfsic: abort cyclic linkage (case 1).\n");
  2108. return ret;
  2109. }
  2110. /*
  2111. * This algorithm is recursive because the amount of used stack
  2112. * space is very small and the max recursion depth is limited.
  2113. */
  2114. list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
  2115. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2116. pr_info("rl=%d, %c @%llu (%s/%llu/%d) %u* refers to %c @%llu (%s/%llu/%d)\n",
  2117. recursion_level,
  2118. btrfsic_get_block_type(state, block),
  2119. block->logical_bytenr, block->dev_state->name,
  2120. block->dev_bytenr, block->mirror_num,
  2121. l->ref_cnt,
  2122. btrfsic_get_block_type(state, l->block_ref_to),
  2123. l->block_ref_to->logical_bytenr,
  2124. l->block_ref_to->dev_state->name,
  2125. l->block_ref_to->dev_bytenr,
  2126. l->block_ref_to->mirror_num);
  2127. if (l->block_ref_to->never_written) {
  2128. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is never written!\n",
  2129. btrfsic_get_block_type(state, l->block_ref_to),
  2130. l->block_ref_to->logical_bytenr,
  2131. l->block_ref_to->dev_state->name,
  2132. l->block_ref_to->dev_bytenr,
  2133. l->block_ref_to->mirror_num);
  2134. ret = -1;
  2135. } else if (!l->block_ref_to->is_iodone) {
  2136. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not yet iodone!\n",
  2137. btrfsic_get_block_type(state, l->block_ref_to),
  2138. l->block_ref_to->logical_bytenr,
  2139. l->block_ref_to->dev_state->name,
  2140. l->block_ref_to->dev_bytenr,
  2141. l->block_ref_to->mirror_num);
  2142. ret = -1;
  2143. } else if (l->block_ref_to->iodone_w_error) {
  2144. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which has write error!\n",
  2145. btrfsic_get_block_type(state, l->block_ref_to),
  2146. l->block_ref_to->logical_bytenr,
  2147. l->block_ref_to->dev_state->name,
  2148. l->block_ref_to->dev_bytenr,
  2149. l->block_ref_to->mirror_num);
  2150. ret = -1;
  2151. } else if (l->parent_generation !=
  2152. l->block_ref_to->generation &&
  2153. BTRFSIC_GENERATION_UNKNOWN !=
  2154. l->parent_generation &&
  2155. BTRFSIC_GENERATION_UNKNOWN !=
  2156. l->block_ref_to->generation) {
  2157. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) with generation %llu != parent generation %llu!\n",
  2158. btrfsic_get_block_type(state, l->block_ref_to),
  2159. l->block_ref_to->logical_bytenr,
  2160. l->block_ref_to->dev_state->name,
  2161. l->block_ref_to->dev_bytenr,
  2162. l->block_ref_to->mirror_num,
  2163. l->block_ref_to->generation,
  2164. l->parent_generation);
  2165. ret = -1;
  2166. } else if (l->block_ref_to->flush_gen >
  2167. l->block_ref_to->dev_state->last_flush_gen) {
  2168. pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not flushed out of disk's write cache (block flush_gen=%llu, dev->flush_gen=%llu)!\n",
  2169. btrfsic_get_block_type(state, l->block_ref_to),
  2170. l->block_ref_to->logical_bytenr,
  2171. l->block_ref_to->dev_state->name,
  2172. l->block_ref_to->dev_bytenr,
  2173. l->block_ref_to->mirror_num, block->flush_gen,
  2174. l->block_ref_to->dev_state->last_flush_gen);
  2175. ret = -1;
  2176. } else if (-1 == btrfsic_check_all_ref_blocks(state,
  2177. l->block_ref_to,
  2178. recursion_level +
  2179. 1)) {
  2180. ret = -1;
  2181. }
  2182. }
  2183. return ret;
  2184. }
  2185. static int btrfsic_is_block_ref_by_superblock(
  2186. const struct btrfsic_state *state,
  2187. const struct btrfsic_block *block,
  2188. int recursion_level)
  2189. {
  2190. const struct btrfsic_block_link *l;
  2191. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2192. /* refer to comment at "abort cyclic linkage (case 1)" */
  2193. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2194. pr_info("btrfsic: abort cyclic linkage (case 2).\n");
  2195. return 0;
  2196. }
  2197. /*
  2198. * This algorithm is recursive because the amount of used stack space
  2199. * is very small and the max recursion depth is limited.
  2200. */
  2201. list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
  2202. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2203. pr_info("rl=%d, %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
  2204. recursion_level,
  2205. btrfsic_get_block_type(state, block),
  2206. block->logical_bytenr, block->dev_state->name,
  2207. block->dev_bytenr, block->mirror_num,
  2208. l->ref_cnt,
  2209. btrfsic_get_block_type(state, l->block_ref_from),
  2210. l->block_ref_from->logical_bytenr,
  2211. l->block_ref_from->dev_state->name,
  2212. l->block_ref_from->dev_bytenr,
  2213. l->block_ref_from->mirror_num);
  2214. if (l->block_ref_from->is_superblock &&
  2215. state->latest_superblock->dev_bytenr ==
  2216. l->block_ref_from->dev_bytenr &&
  2217. state->latest_superblock->dev_state->bdev ==
  2218. l->block_ref_from->dev_state->bdev)
  2219. return 1;
  2220. else if (btrfsic_is_block_ref_by_superblock(state,
  2221. l->block_ref_from,
  2222. recursion_level +
  2223. 1))
  2224. return 1;
  2225. }
  2226. return 0;
  2227. }
  2228. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  2229. const struct btrfsic_block_link *l)
  2230. {
  2231. pr_info("Add %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
  2232. l->ref_cnt,
  2233. btrfsic_get_block_type(state, l->block_ref_from),
  2234. l->block_ref_from->logical_bytenr,
  2235. l->block_ref_from->dev_state->name,
  2236. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2237. btrfsic_get_block_type(state, l->block_ref_to),
  2238. l->block_ref_to->logical_bytenr,
  2239. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2240. l->block_ref_to->mirror_num);
  2241. }
  2242. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  2243. const struct btrfsic_block_link *l)
  2244. {
  2245. pr_info("Rem %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
  2246. l->ref_cnt,
  2247. btrfsic_get_block_type(state, l->block_ref_from),
  2248. l->block_ref_from->logical_bytenr,
  2249. l->block_ref_from->dev_state->name,
  2250. l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
  2251. btrfsic_get_block_type(state, l->block_ref_to),
  2252. l->block_ref_to->logical_bytenr,
  2253. l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
  2254. l->block_ref_to->mirror_num);
  2255. }
  2256. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  2257. const struct btrfsic_block *block)
  2258. {
  2259. if (block->is_superblock &&
  2260. state->latest_superblock->dev_bytenr == block->dev_bytenr &&
  2261. state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
  2262. return 'S';
  2263. else if (block->is_superblock)
  2264. return 's';
  2265. else if (block->is_metadata)
  2266. return 'M';
  2267. else
  2268. return 'D';
  2269. }
  2270. static void btrfsic_dump_tree(const struct btrfsic_state *state)
  2271. {
  2272. btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
  2273. }
  2274. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  2275. const struct btrfsic_block *block,
  2276. int indent_level)
  2277. {
  2278. const struct btrfsic_block_link *l;
  2279. int indent_add;
  2280. static char buf[80];
  2281. int cursor_position;
  2282. /*
  2283. * Should better fill an on-stack buffer with a complete line and
  2284. * dump it at once when it is time to print a newline character.
  2285. */
  2286. /*
  2287. * This algorithm is recursive because the amount of used stack space
  2288. * is very small and the max recursion depth is limited.
  2289. */
  2290. indent_add = sprintf(buf, "%c-%llu(%s/%llu/%u)",
  2291. btrfsic_get_block_type(state, block),
  2292. block->logical_bytenr, block->dev_state->name,
  2293. block->dev_bytenr, block->mirror_num);
  2294. if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2295. printk("[...]\n");
  2296. return;
  2297. }
  2298. printk(buf);
  2299. indent_level += indent_add;
  2300. if (list_empty(&block->ref_to_list)) {
  2301. printk("\n");
  2302. return;
  2303. }
  2304. if (block->mirror_num > 1 &&
  2305. !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
  2306. printk(" [...]\n");
  2307. return;
  2308. }
  2309. cursor_position = indent_level;
  2310. list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
  2311. while (cursor_position < indent_level) {
  2312. printk(" ");
  2313. cursor_position++;
  2314. }
  2315. if (l->ref_cnt > 1)
  2316. indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
  2317. else
  2318. indent_add = sprintf(buf, " --> ");
  2319. if (indent_level + indent_add >
  2320. BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2321. printk("[...]\n");
  2322. cursor_position = 0;
  2323. continue;
  2324. }
  2325. printk(buf);
  2326. btrfsic_dump_tree_sub(state, l->block_ref_to,
  2327. indent_level + indent_add);
  2328. cursor_position = 0;
  2329. }
  2330. }
  2331. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  2332. struct btrfsic_state *state,
  2333. struct btrfsic_block_data_ctx *next_block_ctx,
  2334. struct btrfsic_block *next_block,
  2335. struct btrfsic_block *from_block,
  2336. u64 parent_generation)
  2337. {
  2338. struct btrfsic_block_link *l;
  2339. l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
  2340. next_block_ctx->dev_bytenr,
  2341. from_block->dev_state->bdev,
  2342. from_block->dev_bytenr,
  2343. &state->block_link_hashtable);
  2344. if (NULL == l) {
  2345. l = btrfsic_block_link_alloc();
  2346. if (NULL == l) {
  2347. pr_info("btrfsic: error, kmalloc failed!\n");
  2348. return NULL;
  2349. }
  2350. l->block_ref_to = next_block;
  2351. l->block_ref_from = from_block;
  2352. l->ref_cnt = 1;
  2353. l->parent_generation = parent_generation;
  2354. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2355. btrfsic_print_add_link(state, l);
  2356. list_add(&l->node_ref_to, &from_block->ref_to_list);
  2357. list_add(&l->node_ref_from, &next_block->ref_from_list);
  2358. btrfsic_block_link_hashtable_add(l,
  2359. &state->block_link_hashtable);
  2360. } else {
  2361. l->ref_cnt++;
  2362. l->parent_generation = parent_generation;
  2363. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2364. btrfsic_print_add_link(state, l);
  2365. }
  2366. return l;
  2367. }
  2368. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  2369. struct btrfsic_state *state,
  2370. struct btrfsic_block_data_ctx *block_ctx,
  2371. const char *additional_string,
  2372. int is_metadata,
  2373. int is_iodone,
  2374. int never_written,
  2375. int mirror_num,
  2376. int *was_created)
  2377. {
  2378. struct btrfsic_block *block;
  2379. block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
  2380. block_ctx->dev_bytenr,
  2381. &state->block_hashtable);
  2382. if (NULL == block) {
  2383. struct btrfsic_dev_state *dev_state;
  2384. block = btrfsic_block_alloc();
  2385. if (NULL == block) {
  2386. pr_info("btrfsic: error, kmalloc failed!\n");
  2387. return NULL;
  2388. }
  2389. dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev->bd_dev);
  2390. if (NULL == dev_state) {
  2391. pr_info("btrfsic: error, lookup dev_state failed!\n");
  2392. btrfsic_block_free(block);
  2393. return NULL;
  2394. }
  2395. block->dev_state = dev_state;
  2396. block->dev_bytenr = block_ctx->dev_bytenr;
  2397. block->logical_bytenr = block_ctx->start;
  2398. block->is_metadata = is_metadata;
  2399. block->is_iodone = is_iodone;
  2400. block->never_written = never_written;
  2401. block->mirror_num = mirror_num;
  2402. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2403. pr_info("New %s%c-block @%llu (%s/%llu/%d)\n",
  2404. additional_string,
  2405. btrfsic_get_block_type(state, block),
  2406. block->logical_bytenr, dev_state->name,
  2407. block->dev_bytenr, mirror_num);
  2408. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2409. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2410. if (NULL != was_created)
  2411. *was_created = 1;
  2412. } else {
  2413. if (NULL != was_created)
  2414. *was_created = 0;
  2415. }
  2416. return block;
  2417. }
  2418. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  2419. u64 bytenr,
  2420. struct btrfsic_dev_state *dev_state,
  2421. u64 dev_bytenr)
  2422. {
  2423. struct btrfs_fs_info *fs_info = state->fs_info;
  2424. struct btrfsic_block_data_ctx block_ctx;
  2425. int num_copies;
  2426. int mirror_num;
  2427. int match = 0;
  2428. int ret;
  2429. num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
  2430. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2431. ret = btrfsic_map_block(state, bytenr, state->metablock_size,
  2432. &block_ctx, mirror_num);
  2433. if (ret) {
  2434. pr_info("btrfsic: btrfsic_map_block(logical @%llu, mirror %d) failed!\n",
  2435. bytenr, mirror_num);
  2436. continue;
  2437. }
  2438. if (dev_state->bdev == block_ctx.dev->bdev &&
  2439. dev_bytenr == block_ctx.dev_bytenr) {
  2440. match++;
  2441. btrfsic_release_block_ctx(&block_ctx);
  2442. break;
  2443. }
  2444. btrfsic_release_block_ctx(&block_ctx);
  2445. }
  2446. if (WARN_ON(!match)) {
  2447. pr_info("btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio, buffer->log_bytenr=%llu, submit_bio(bdev=%s, phys_bytenr=%llu)!\n",
  2448. bytenr, dev_state->name, dev_bytenr);
  2449. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2450. ret = btrfsic_map_block(state, bytenr,
  2451. state->metablock_size,
  2452. &block_ctx, mirror_num);
  2453. if (ret)
  2454. continue;
  2455. pr_info("Read logical bytenr @%llu maps to (%s/%llu/%d)\n",
  2456. bytenr, block_ctx.dev->name,
  2457. block_ctx.dev_bytenr, mirror_num);
  2458. }
  2459. }
  2460. }
  2461. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev)
  2462. {
  2463. return btrfsic_dev_state_hashtable_lookup(dev,
  2464. &btrfsic_dev_state_hashtable);
  2465. }
  2466. int btrfsic_submit_bh(int op, int op_flags, struct buffer_head *bh)
  2467. {
  2468. struct btrfsic_dev_state *dev_state;
  2469. if (!btrfsic_is_initialized)
  2470. return submit_bh(op, op_flags, bh);
  2471. mutex_lock(&btrfsic_mutex);
  2472. /* since btrfsic_submit_bh() might also be called before
  2473. * btrfsic_mount(), this might return NULL */
  2474. dev_state = btrfsic_dev_state_lookup(bh->b_bdev->bd_dev);
  2475. /* Only called to write the superblock (incl. FLUSH/FUA) */
  2476. if (NULL != dev_state &&
  2477. (op == REQ_OP_WRITE) && bh->b_size > 0) {
  2478. u64 dev_bytenr;
  2479. dev_bytenr = BTRFS_BDEV_BLOCKSIZE * bh->b_blocknr;
  2480. if (dev_state->state->print_mask &
  2481. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2482. pr_info("submit_bh(op=0x%x,0x%x, blocknr=%llu (bytenr %llu), size=%zu, data=%p, bdev=%p)\n",
  2483. op, op_flags, (unsigned long long)bh->b_blocknr,
  2484. dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
  2485. btrfsic_process_written_block(dev_state, dev_bytenr,
  2486. &bh->b_data, 1, NULL,
  2487. NULL, bh, op_flags);
  2488. } else if (NULL != dev_state && (op_flags & REQ_PREFLUSH)) {
  2489. if (dev_state->state->print_mask &
  2490. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2491. pr_info("submit_bh(op=0x%x,0x%x FLUSH, bdev=%p)\n",
  2492. op, op_flags, bh->b_bdev);
  2493. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2494. if ((dev_state->state->print_mask &
  2495. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2496. BTRFSIC_PRINT_MASK_VERBOSE)))
  2497. pr_info("btrfsic_submit_bh(%s) with FLUSH but dummy block already in use (ignored)!\n",
  2498. dev_state->name);
  2499. } else {
  2500. struct btrfsic_block *const block =
  2501. &dev_state->dummy_block_for_bio_bh_flush;
  2502. block->is_iodone = 0;
  2503. block->never_written = 0;
  2504. block->iodone_w_error = 0;
  2505. block->flush_gen = dev_state->last_flush_gen + 1;
  2506. block->submit_bio_bh_rw = op_flags;
  2507. block->orig_bio_bh_private = bh->b_private;
  2508. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2509. block->next_in_same_bio = NULL;
  2510. bh->b_private = block;
  2511. bh->b_end_io = btrfsic_bh_end_io;
  2512. }
  2513. }
  2514. mutex_unlock(&btrfsic_mutex);
  2515. return submit_bh(op, op_flags, bh);
  2516. }
  2517. static void __btrfsic_submit_bio(struct bio *bio)
  2518. {
  2519. struct btrfsic_dev_state *dev_state;
  2520. if (!btrfsic_is_initialized)
  2521. return;
  2522. mutex_lock(&btrfsic_mutex);
  2523. /* since btrfsic_submit_bio() is also called before
  2524. * btrfsic_mount(), this might return NULL */
  2525. dev_state = btrfsic_dev_state_lookup(bio_dev(bio) + bio->bi_partno);
  2526. if (NULL != dev_state &&
  2527. (bio_op(bio) == REQ_OP_WRITE) && bio_has_data(bio)) {
  2528. unsigned int i = 0;
  2529. u64 dev_bytenr;
  2530. u64 cur_bytenr;
  2531. struct bio_vec bvec;
  2532. struct bvec_iter iter;
  2533. int bio_is_patched;
  2534. char **mapped_datav;
  2535. unsigned int segs = bio_segments(bio);
  2536. dev_bytenr = 512 * bio->bi_iter.bi_sector;
  2537. bio_is_patched = 0;
  2538. if (dev_state->state->print_mask &
  2539. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2540. pr_info("submit_bio(rw=%d,0x%x, bi_vcnt=%u, bi_sector=%llu (bytenr %llu), bi_disk=%p)\n",
  2541. bio_op(bio), bio->bi_opf, segs,
  2542. (unsigned long long)bio->bi_iter.bi_sector,
  2543. dev_bytenr, bio->bi_disk);
  2544. mapped_datav = kmalloc_array(segs,
  2545. sizeof(*mapped_datav), GFP_NOFS);
  2546. if (!mapped_datav)
  2547. goto leave;
  2548. cur_bytenr = dev_bytenr;
  2549. bio_for_each_segment(bvec, bio, iter) {
  2550. BUG_ON(bvec.bv_len != PAGE_SIZE);
  2551. mapped_datav[i] = kmap(bvec.bv_page);
  2552. i++;
  2553. if (dev_state->state->print_mask &
  2554. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
  2555. pr_info("#%u: bytenr=%llu, len=%u, offset=%u\n",
  2556. i, cur_bytenr, bvec.bv_len, bvec.bv_offset);
  2557. cur_bytenr += bvec.bv_len;
  2558. }
  2559. btrfsic_process_written_block(dev_state, dev_bytenr,
  2560. mapped_datav, segs,
  2561. bio, &bio_is_patched,
  2562. NULL, bio->bi_opf);
  2563. bio_for_each_segment(bvec, bio, iter)
  2564. kunmap(bvec.bv_page);
  2565. kfree(mapped_datav);
  2566. } else if (NULL != dev_state && (bio->bi_opf & REQ_PREFLUSH)) {
  2567. if (dev_state->state->print_mask &
  2568. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2569. pr_info("submit_bio(rw=%d,0x%x FLUSH, disk=%p)\n",
  2570. bio_op(bio), bio->bi_opf, bio->bi_disk);
  2571. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2572. if ((dev_state->state->print_mask &
  2573. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2574. BTRFSIC_PRINT_MASK_VERBOSE)))
  2575. pr_info("btrfsic_submit_bio(%s) with FLUSH but dummy block already in use (ignored)!\n",
  2576. dev_state->name);
  2577. } else {
  2578. struct btrfsic_block *const block =
  2579. &dev_state->dummy_block_for_bio_bh_flush;
  2580. block->is_iodone = 0;
  2581. block->never_written = 0;
  2582. block->iodone_w_error = 0;
  2583. block->flush_gen = dev_state->last_flush_gen + 1;
  2584. block->submit_bio_bh_rw = bio->bi_opf;
  2585. block->orig_bio_bh_private = bio->bi_private;
  2586. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2587. block->next_in_same_bio = NULL;
  2588. bio->bi_private = block;
  2589. bio->bi_end_io = btrfsic_bio_end_io;
  2590. }
  2591. }
  2592. leave:
  2593. mutex_unlock(&btrfsic_mutex);
  2594. }
  2595. void btrfsic_submit_bio(struct bio *bio)
  2596. {
  2597. __btrfsic_submit_bio(bio);
  2598. submit_bio(bio);
  2599. }
  2600. int btrfsic_submit_bio_wait(struct bio *bio)
  2601. {
  2602. __btrfsic_submit_bio(bio);
  2603. return submit_bio_wait(bio);
  2604. }
  2605. int btrfsic_mount(struct btrfs_fs_info *fs_info,
  2606. struct btrfs_fs_devices *fs_devices,
  2607. int including_extent_data, u32 print_mask)
  2608. {
  2609. int ret;
  2610. struct btrfsic_state *state;
  2611. struct list_head *dev_head = &fs_devices->devices;
  2612. struct btrfs_device *device;
  2613. if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
  2614. pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
  2615. fs_info->nodesize, PAGE_SIZE);
  2616. return -1;
  2617. }
  2618. if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
  2619. pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
  2620. fs_info->sectorsize, PAGE_SIZE);
  2621. return -1;
  2622. }
  2623. state = kvzalloc(sizeof(*state), GFP_KERNEL);
  2624. if (!state) {
  2625. pr_info("btrfs check-integrity: allocation failed!\n");
  2626. return -ENOMEM;
  2627. }
  2628. if (!btrfsic_is_initialized) {
  2629. mutex_init(&btrfsic_mutex);
  2630. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2631. btrfsic_is_initialized = 1;
  2632. }
  2633. mutex_lock(&btrfsic_mutex);
  2634. state->fs_info = fs_info;
  2635. state->print_mask = print_mask;
  2636. state->include_extent_data = including_extent_data;
  2637. state->csum_size = 0;
  2638. state->metablock_size = fs_info->nodesize;
  2639. state->datablock_size = fs_info->sectorsize;
  2640. INIT_LIST_HEAD(&state->all_blocks_list);
  2641. btrfsic_block_hashtable_init(&state->block_hashtable);
  2642. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2643. state->max_superblock_generation = 0;
  2644. state->latest_superblock = NULL;
  2645. list_for_each_entry(device, dev_head, dev_list) {
  2646. struct btrfsic_dev_state *ds;
  2647. const char *p;
  2648. if (!device->bdev || !device->name)
  2649. continue;
  2650. ds = btrfsic_dev_state_alloc();
  2651. if (NULL == ds) {
  2652. pr_info("btrfs check-integrity: kmalloc() failed!\n");
  2653. mutex_unlock(&btrfsic_mutex);
  2654. return -ENOMEM;
  2655. }
  2656. ds->bdev = device->bdev;
  2657. ds->state = state;
  2658. bdevname(ds->bdev, ds->name);
  2659. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2660. p = kbasename(ds->name);
  2661. strlcpy(ds->name, p, sizeof(ds->name));
  2662. btrfsic_dev_state_hashtable_add(ds,
  2663. &btrfsic_dev_state_hashtable);
  2664. }
  2665. ret = btrfsic_process_superblock(state, fs_devices);
  2666. if (0 != ret) {
  2667. mutex_unlock(&btrfsic_mutex);
  2668. btrfsic_unmount(fs_devices);
  2669. return ret;
  2670. }
  2671. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2672. btrfsic_dump_database(state);
  2673. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  2674. btrfsic_dump_tree(state);
  2675. mutex_unlock(&btrfsic_mutex);
  2676. return 0;
  2677. }
  2678. void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
  2679. {
  2680. struct btrfsic_block *b_all, *tmp_all;
  2681. struct btrfsic_state *state;
  2682. struct list_head *dev_head = &fs_devices->devices;
  2683. struct btrfs_device *device;
  2684. if (!btrfsic_is_initialized)
  2685. return;
  2686. mutex_lock(&btrfsic_mutex);
  2687. state = NULL;
  2688. list_for_each_entry(device, dev_head, dev_list) {
  2689. struct btrfsic_dev_state *ds;
  2690. if (!device->bdev || !device->name)
  2691. continue;
  2692. ds = btrfsic_dev_state_hashtable_lookup(
  2693. device->bdev->bd_dev,
  2694. &btrfsic_dev_state_hashtable);
  2695. if (NULL != ds) {
  2696. state = ds->state;
  2697. btrfsic_dev_state_hashtable_remove(ds);
  2698. btrfsic_dev_state_free(ds);
  2699. }
  2700. }
  2701. if (NULL == state) {
  2702. pr_info("btrfsic: error, cannot find state information on umount!\n");
  2703. mutex_unlock(&btrfsic_mutex);
  2704. return;
  2705. }
  2706. /*
  2707. * Don't care about keeping the lists' state up to date,
  2708. * just free all memory that was allocated dynamically.
  2709. * Free the blocks and the block_links.
  2710. */
  2711. list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
  2712. all_blocks_node) {
  2713. struct btrfsic_block_link *l, *tmp;
  2714. list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
  2715. node_ref_to) {
  2716. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2717. btrfsic_print_rem_link(state, l);
  2718. l->ref_cnt--;
  2719. if (0 == l->ref_cnt)
  2720. btrfsic_block_link_free(l);
  2721. }
  2722. if (b_all->is_iodone || b_all->never_written)
  2723. btrfsic_block_free(b_all);
  2724. else
  2725. pr_info("btrfs: attempt to free %c-block @%llu (%s/%llu/%d) on umount which is not yet iodone!\n",
  2726. btrfsic_get_block_type(state, b_all),
  2727. b_all->logical_bytenr, b_all->dev_state->name,
  2728. b_all->dev_bytenr, b_all->mirror_num);
  2729. }
  2730. mutex_unlock(&btrfsic_mutex);
  2731. kvfree(state);
  2732. }