check-integrity.c 102 KB

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