check-integrity.c 103 KB

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