check-integrity.c 94 KB

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