check-integrity.c 102 KB

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