check-integrity.c 102 KB

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