check-integrity.c 98 KB

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