check-integrity.c 99 KB

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