check-integrity.c 86 KB

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