check-integrity.c 103 KB

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