check-integrity.c 99 KB

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