check-integrity.c 97 KB

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