check-integrity.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382
  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. strncmp((char *)(&(super_tmp->magic)), BTRFS_MAGIC,
  737. sizeof(super_tmp->magic)) ||
  738. memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
  739. btrfs_super_nodesize(super_tmp) != state->metablock_size ||
  740. btrfs_super_leafsize(super_tmp) != state->metablock_size ||
  741. btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
  742. brelse(bh);
  743. return 0;
  744. }
  745. superblock_tmp =
  746. btrfsic_block_hashtable_lookup(superblock_bdev,
  747. dev_bytenr,
  748. &state->block_hashtable);
  749. if (NULL == superblock_tmp) {
  750. superblock_tmp = btrfsic_block_alloc();
  751. if (NULL == superblock_tmp) {
  752. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  753. brelse(bh);
  754. return -1;
  755. }
  756. /* for superblock, only the dev_bytenr makes sense */
  757. superblock_tmp->dev_bytenr = dev_bytenr;
  758. superblock_tmp->dev_state = dev_state;
  759. superblock_tmp->logical_bytenr = dev_bytenr;
  760. superblock_tmp->generation = btrfs_super_generation(super_tmp);
  761. superblock_tmp->is_metadata = 1;
  762. superblock_tmp->is_superblock = 1;
  763. superblock_tmp->is_iodone = 1;
  764. superblock_tmp->never_written = 0;
  765. superblock_tmp->mirror_num = 1 + superblock_mirror_num;
  766. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  767. printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
  768. " @%llu (%s/%llu/%d)\n",
  769. superblock_bdev,
  770. rcu_str_deref(device->name),
  771. (unsigned long long)dev_bytenr,
  772. dev_state->name,
  773. (unsigned long long)dev_bytenr,
  774. superblock_mirror_num);
  775. list_add(&superblock_tmp->all_blocks_node,
  776. &state->all_blocks_list);
  777. btrfsic_block_hashtable_add(superblock_tmp,
  778. &state->block_hashtable);
  779. }
  780. /* select the one with the highest generation field */
  781. if (btrfs_super_generation(super_tmp) >
  782. state->max_superblock_generation ||
  783. 0 == state->max_superblock_generation) {
  784. memcpy(selected_super, super_tmp, sizeof(*selected_super));
  785. *selected_dev_state = dev_state;
  786. state->max_superblock_generation =
  787. btrfs_super_generation(super_tmp);
  788. state->latest_superblock = superblock_tmp;
  789. }
  790. for (pass = 0; pass < 3; pass++) {
  791. u64 next_bytenr;
  792. int num_copies;
  793. int mirror_num;
  794. const char *additional_string = NULL;
  795. struct btrfs_disk_key tmp_disk_key;
  796. tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
  797. tmp_disk_key.offset = 0;
  798. switch (pass) {
  799. case 0:
  800. tmp_disk_key.objectid =
  801. cpu_to_le64(BTRFS_ROOT_TREE_OBJECTID);
  802. additional_string = "initial root ";
  803. next_bytenr = btrfs_super_root(super_tmp);
  804. break;
  805. case 1:
  806. tmp_disk_key.objectid =
  807. cpu_to_le64(BTRFS_CHUNK_TREE_OBJECTID);
  808. additional_string = "initial chunk ";
  809. next_bytenr = btrfs_super_chunk_root(super_tmp);
  810. break;
  811. case 2:
  812. tmp_disk_key.objectid =
  813. cpu_to_le64(BTRFS_TREE_LOG_OBJECTID);
  814. additional_string = "initial log ";
  815. next_bytenr = btrfs_super_log_root(super_tmp);
  816. if (0 == next_bytenr)
  817. continue;
  818. break;
  819. }
  820. num_copies =
  821. btrfs_num_copies(state->root->fs_info,
  822. next_bytenr, state->metablock_size);
  823. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  824. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  825. (unsigned long long)next_bytenr, num_copies);
  826. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  827. struct btrfsic_block *next_block;
  828. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  829. struct btrfsic_block_link *l;
  830. if (btrfsic_map_block(state, next_bytenr,
  831. state->metablock_size,
  832. &tmp_next_block_ctx,
  833. mirror_num)) {
  834. printk(KERN_INFO "btrfsic: btrfsic_map_block("
  835. "bytenr @%llu, mirror %d) failed!\n",
  836. (unsigned long long)next_bytenr,
  837. mirror_num);
  838. brelse(bh);
  839. return -1;
  840. }
  841. next_block = btrfsic_block_lookup_or_add(
  842. state, &tmp_next_block_ctx,
  843. additional_string, 1, 1, 0,
  844. mirror_num, NULL);
  845. if (NULL == next_block) {
  846. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  847. brelse(bh);
  848. return -1;
  849. }
  850. next_block->disk_key = tmp_disk_key;
  851. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  852. l = btrfsic_block_link_lookup_or_add(
  853. state, &tmp_next_block_ctx,
  854. next_block, superblock_tmp,
  855. BTRFSIC_GENERATION_UNKNOWN);
  856. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  857. if (NULL == l) {
  858. brelse(bh);
  859. return -1;
  860. }
  861. }
  862. }
  863. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
  864. btrfsic_dump_tree_sub(state, superblock_tmp, 0);
  865. brelse(bh);
  866. return 0;
  867. }
  868. static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
  869. {
  870. struct btrfsic_stack_frame *sf;
  871. sf = kzalloc(sizeof(*sf), GFP_NOFS);
  872. if (NULL == sf)
  873. printk(KERN_INFO "btrfsic: alloc memory failed!\n");
  874. else
  875. sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
  876. return sf;
  877. }
  878. static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
  879. {
  880. BUG_ON(!(NULL == sf ||
  881. BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
  882. kfree(sf);
  883. }
  884. static int btrfsic_process_metablock(
  885. struct btrfsic_state *state,
  886. struct btrfsic_block *const first_block,
  887. struct btrfsic_block_data_ctx *const first_block_ctx,
  888. int first_limit_nesting, int force_iodone_flag)
  889. {
  890. struct btrfsic_stack_frame initial_stack_frame = { 0 };
  891. struct btrfsic_stack_frame *sf;
  892. struct btrfsic_stack_frame *next_stack;
  893. struct btrfs_header *const first_hdr =
  894. (struct btrfs_header *)first_block_ctx->datav[0];
  895. BUG_ON(!first_hdr);
  896. sf = &initial_stack_frame;
  897. sf->error = 0;
  898. sf->i = -1;
  899. sf->limit_nesting = first_limit_nesting;
  900. sf->block = first_block;
  901. sf->block_ctx = first_block_ctx;
  902. sf->next_block = NULL;
  903. sf->hdr = first_hdr;
  904. sf->prev = NULL;
  905. continue_with_new_stack_frame:
  906. sf->block->generation = le64_to_cpu(sf->hdr->generation);
  907. if (0 == sf->hdr->level) {
  908. struct btrfs_leaf *const leafhdr =
  909. (struct btrfs_leaf *)sf->hdr;
  910. if (-1 == sf->i) {
  911. sf->nr = le32_to_cpu(leafhdr->header.nritems);
  912. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  913. printk(KERN_INFO
  914. "leaf %llu items %d generation %llu"
  915. " owner %llu\n",
  916. (unsigned long long)
  917. sf->block_ctx->start,
  918. sf->nr,
  919. (unsigned long long)
  920. le64_to_cpu(leafhdr->header.generation),
  921. (unsigned long long)
  922. le64_to_cpu(leafhdr->header.owner));
  923. }
  924. continue_with_current_leaf_stack_frame:
  925. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  926. sf->i++;
  927. sf->num_copies = 0;
  928. }
  929. if (sf->i < sf->nr) {
  930. struct btrfs_item disk_item;
  931. u32 disk_item_offset =
  932. (uintptr_t)(leafhdr->items + sf->i) -
  933. (uintptr_t)leafhdr;
  934. struct btrfs_disk_key *disk_key;
  935. u8 type;
  936. u32 item_offset;
  937. u32 item_size;
  938. if (disk_item_offset + sizeof(struct btrfs_item) >
  939. sf->block_ctx->len) {
  940. leaf_item_out_of_bounce_error:
  941. printk(KERN_INFO
  942. "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
  943. sf->block_ctx->start,
  944. sf->block_ctx->dev->name);
  945. goto one_stack_frame_backwards;
  946. }
  947. btrfsic_read_from_block_data(sf->block_ctx,
  948. &disk_item,
  949. disk_item_offset,
  950. sizeof(struct btrfs_item));
  951. item_offset = le32_to_cpu(disk_item.offset);
  952. item_size = le32_to_cpu(disk_item.size);
  953. disk_key = &disk_item.key;
  954. type = disk_key->type;
  955. if (BTRFS_ROOT_ITEM_KEY == type) {
  956. struct btrfs_root_item root_item;
  957. u32 root_item_offset;
  958. u64 next_bytenr;
  959. root_item_offset = item_offset +
  960. offsetof(struct btrfs_leaf, items);
  961. if (root_item_offset + item_size >
  962. sf->block_ctx->len)
  963. goto leaf_item_out_of_bounce_error;
  964. btrfsic_read_from_block_data(
  965. sf->block_ctx, &root_item,
  966. root_item_offset,
  967. item_size);
  968. next_bytenr = le64_to_cpu(root_item.bytenr);
  969. sf->error =
  970. btrfsic_create_link_to_next_block(
  971. state,
  972. sf->block,
  973. sf->block_ctx,
  974. next_bytenr,
  975. sf->limit_nesting,
  976. &sf->next_block_ctx,
  977. &sf->next_block,
  978. force_iodone_flag,
  979. &sf->num_copies,
  980. &sf->mirror_num,
  981. disk_key,
  982. le64_to_cpu(root_item.
  983. generation));
  984. if (sf->error)
  985. goto one_stack_frame_backwards;
  986. if (NULL != sf->next_block) {
  987. struct btrfs_header *const next_hdr =
  988. (struct btrfs_header *)
  989. sf->next_block_ctx.datav[0];
  990. next_stack =
  991. btrfsic_stack_frame_alloc();
  992. if (NULL == next_stack) {
  993. btrfsic_release_block_ctx(
  994. &sf->
  995. next_block_ctx);
  996. goto one_stack_frame_backwards;
  997. }
  998. next_stack->i = -1;
  999. next_stack->block = sf->next_block;
  1000. next_stack->block_ctx =
  1001. &sf->next_block_ctx;
  1002. next_stack->next_block = NULL;
  1003. next_stack->hdr = next_hdr;
  1004. next_stack->limit_nesting =
  1005. sf->limit_nesting - 1;
  1006. next_stack->prev = sf;
  1007. sf = next_stack;
  1008. goto continue_with_new_stack_frame;
  1009. }
  1010. } else if (BTRFS_EXTENT_DATA_KEY == type &&
  1011. state->include_extent_data) {
  1012. sf->error = btrfsic_handle_extent_data(
  1013. state,
  1014. sf->block,
  1015. sf->block_ctx,
  1016. item_offset,
  1017. force_iodone_flag);
  1018. if (sf->error)
  1019. goto one_stack_frame_backwards;
  1020. }
  1021. goto continue_with_current_leaf_stack_frame;
  1022. }
  1023. } else {
  1024. struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
  1025. if (-1 == sf->i) {
  1026. sf->nr = le32_to_cpu(nodehdr->header.nritems);
  1027. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1028. printk(KERN_INFO "node %llu level %d items %d"
  1029. " generation %llu owner %llu\n",
  1030. (unsigned long long)
  1031. sf->block_ctx->start,
  1032. nodehdr->header.level, sf->nr,
  1033. (unsigned long long)
  1034. le64_to_cpu(nodehdr->header.generation),
  1035. (unsigned long long)
  1036. le64_to_cpu(nodehdr->header.owner));
  1037. }
  1038. continue_with_current_node_stack_frame:
  1039. if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
  1040. sf->i++;
  1041. sf->num_copies = 0;
  1042. }
  1043. if (sf->i < sf->nr) {
  1044. struct btrfs_key_ptr key_ptr;
  1045. u32 key_ptr_offset;
  1046. u64 next_bytenr;
  1047. key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
  1048. (uintptr_t)nodehdr;
  1049. if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
  1050. sf->block_ctx->len) {
  1051. printk(KERN_INFO
  1052. "btrfsic: node item out of bounce at logical %llu, dev %s\n",
  1053. sf->block_ctx->start,
  1054. sf->block_ctx->dev->name);
  1055. goto one_stack_frame_backwards;
  1056. }
  1057. btrfsic_read_from_block_data(
  1058. sf->block_ctx, &key_ptr, key_ptr_offset,
  1059. sizeof(struct btrfs_key_ptr));
  1060. next_bytenr = le64_to_cpu(key_ptr.blockptr);
  1061. sf->error = btrfsic_create_link_to_next_block(
  1062. state,
  1063. sf->block,
  1064. sf->block_ctx,
  1065. next_bytenr,
  1066. sf->limit_nesting,
  1067. &sf->next_block_ctx,
  1068. &sf->next_block,
  1069. force_iodone_flag,
  1070. &sf->num_copies,
  1071. &sf->mirror_num,
  1072. &key_ptr.key,
  1073. le64_to_cpu(key_ptr.generation));
  1074. if (sf->error)
  1075. goto one_stack_frame_backwards;
  1076. if (NULL != sf->next_block) {
  1077. struct btrfs_header *const next_hdr =
  1078. (struct btrfs_header *)
  1079. sf->next_block_ctx.datav[0];
  1080. next_stack = btrfsic_stack_frame_alloc();
  1081. if (NULL == next_stack)
  1082. goto one_stack_frame_backwards;
  1083. next_stack->i = -1;
  1084. next_stack->block = sf->next_block;
  1085. next_stack->block_ctx = &sf->next_block_ctx;
  1086. next_stack->next_block = NULL;
  1087. next_stack->hdr = next_hdr;
  1088. next_stack->limit_nesting =
  1089. sf->limit_nesting - 1;
  1090. next_stack->prev = sf;
  1091. sf = next_stack;
  1092. goto continue_with_new_stack_frame;
  1093. }
  1094. goto continue_with_current_node_stack_frame;
  1095. }
  1096. }
  1097. one_stack_frame_backwards:
  1098. if (NULL != sf->prev) {
  1099. struct btrfsic_stack_frame *const prev = sf->prev;
  1100. /* the one for the initial block is freed in the caller */
  1101. btrfsic_release_block_ctx(sf->block_ctx);
  1102. if (sf->error) {
  1103. prev->error = sf->error;
  1104. btrfsic_stack_frame_free(sf);
  1105. sf = prev;
  1106. goto one_stack_frame_backwards;
  1107. }
  1108. btrfsic_stack_frame_free(sf);
  1109. sf = prev;
  1110. goto continue_with_new_stack_frame;
  1111. } else {
  1112. BUG_ON(&initial_stack_frame != sf);
  1113. }
  1114. return sf->error;
  1115. }
  1116. static void btrfsic_read_from_block_data(
  1117. struct btrfsic_block_data_ctx *block_ctx,
  1118. void *dstv, u32 offset, size_t len)
  1119. {
  1120. size_t cur;
  1121. size_t offset_in_page;
  1122. char *kaddr;
  1123. char *dst = (char *)dstv;
  1124. size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
  1125. unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
  1126. WARN_ON(offset + len > block_ctx->len);
  1127. offset_in_page = (start_offset + offset) &
  1128. ((unsigned long)PAGE_CACHE_SIZE - 1);
  1129. while (len > 0) {
  1130. cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
  1131. BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
  1132. PAGE_CACHE_SHIFT);
  1133. kaddr = block_ctx->datav[i];
  1134. memcpy(dst, kaddr + offset_in_page, cur);
  1135. dst += cur;
  1136. len -= cur;
  1137. offset_in_page = 0;
  1138. i++;
  1139. }
  1140. }
  1141. static int btrfsic_create_link_to_next_block(
  1142. struct btrfsic_state *state,
  1143. struct btrfsic_block *block,
  1144. struct btrfsic_block_data_ctx *block_ctx,
  1145. u64 next_bytenr,
  1146. int limit_nesting,
  1147. struct btrfsic_block_data_ctx *next_block_ctx,
  1148. struct btrfsic_block **next_blockp,
  1149. int force_iodone_flag,
  1150. int *num_copiesp, int *mirror_nump,
  1151. struct btrfs_disk_key *disk_key,
  1152. u64 parent_generation)
  1153. {
  1154. struct btrfsic_block *next_block = NULL;
  1155. int ret;
  1156. struct btrfsic_block_link *l;
  1157. int did_alloc_block_link;
  1158. int block_was_created;
  1159. *next_blockp = NULL;
  1160. if (0 == *num_copiesp) {
  1161. *num_copiesp =
  1162. btrfs_num_copies(state->root->fs_info,
  1163. next_bytenr, state->metablock_size);
  1164. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1165. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1166. (unsigned long long)next_bytenr, *num_copiesp);
  1167. *mirror_nump = 1;
  1168. }
  1169. if (*mirror_nump > *num_copiesp)
  1170. return 0;
  1171. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1172. printk(KERN_INFO
  1173. "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
  1174. *mirror_nump);
  1175. ret = btrfsic_map_block(state, next_bytenr,
  1176. state->metablock_size,
  1177. next_block_ctx, *mirror_nump);
  1178. if (ret) {
  1179. printk(KERN_INFO
  1180. "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
  1181. (unsigned long long)next_bytenr, *mirror_nump);
  1182. btrfsic_release_block_ctx(next_block_ctx);
  1183. *next_blockp = NULL;
  1184. return -1;
  1185. }
  1186. next_block = btrfsic_block_lookup_or_add(state,
  1187. next_block_ctx, "referenced ",
  1188. 1, force_iodone_flag,
  1189. !force_iodone_flag,
  1190. *mirror_nump,
  1191. &block_was_created);
  1192. if (NULL == next_block) {
  1193. btrfsic_release_block_ctx(next_block_ctx);
  1194. *next_blockp = NULL;
  1195. return -1;
  1196. }
  1197. if (block_was_created) {
  1198. l = NULL;
  1199. next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1200. } else {
  1201. if (next_block->logical_bytenr != next_bytenr &&
  1202. !(!next_block->is_metadata &&
  1203. 0 == next_block->logical_bytenr)) {
  1204. printk(KERN_INFO
  1205. "Referenced block @%llu (%s/%llu/%d)"
  1206. " found in hash table, %c,"
  1207. " bytenr mismatch (!= stored %llu).\n",
  1208. (unsigned long long)next_bytenr,
  1209. next_block_ctx->dev->name,
  1210. (unsigned long long)next_block_ctx->dev_bytenr,
  1211. *mirror_nump,
  1212. btrfsic_get_block_type(state, next_block),
  1213. (unsigned long long)next_block->logical_bytenr);
  1214. } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1215. printk(KERN_INFO
  1216. "Referenced block @%llu (%s/%llu/%d)"
  1217. " found in hash table, %c.\n",
  1218. (unsigned long long)next_bytenr,
  1219. next_block_ctx->dev->name,
  1220. (unsigned long long)next_block_ctx->dev_bytenr,
  1221. *mirror_nump,
  1222. btrfsic_get_block_type(state, next_block));
  1223. next_block->logical_bytenr = next_bytenr;
  1224. next_block->mirror_num = *mirror_nump;
  1225. l = btrfsic_block_link_hashtable_lookup(
  1226. next_block_ctx->dev->bdev,
  1227. next_block_ctx->dev_bytenr,
  1228. block_ctx->dev->bdev,
  1229. block_ctx->dev_bytenr,
  1230. &state->block_link_hashtable);
  1231. }
  1232. next_block->disk_key = *disk_key;
  1233. if (NULL == l) {
  1234. l = btrfsic_block_link_alloc();
  1235. if (NULL == l) {
  1236. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  1237. btrfsic_release_block_ctx(next_block_ctx);
  1238. *next_blockp = NULL;
  1239. return -1;
  1240. }
  1241. did_alloc_block_link = 1;
  1242. l->block_ref_to = next_block;
  1243. l->block_ref_from = block;
  1244. l->ref_cnt = 1;
  1245. l->parent_generation = parent_generation;
  1246. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1247. btrfsic_print_add_link(state, l);
  1248. list_add(&l->node_ref_to, &block->ref_to_list);
  1249. list_add(&l->node_ref_from, &next_block->ref_from_list);
  1250. btrfsic_block_link_hashtable_add(l,
  1251. &state->block_link_hashtable);
  1252. } else {
  1253. did_alloc_block_link = 0;
  1254. if (0 == limit_nesting) {
  1255. l->ref_cnt++;
  1256. l->parent_generation = parent_generation;
  1257. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1258. btrfsic_print_add_link(state, l);
  1259. }
  1260. }
  1261. if (limit_nesting > 0 && did_alloc_block_link) {
  1262. ret = btrfsic_read_block(state, next_block_ctx);
  1263. if (ret < (int)next_block_ctx->len) {
  1264. printk(KERN_INFO
  1265. "btrfsic: read block @logical %llu failed!\n",
  1266. (unsigned long long)next_bytenr);
  1267. btrfsic_release_block_ctx(next_block_ctx);
  1268. *next_blockp = NULL;
  1269. return -1;
  1270. }
  1271. *next_blockp = next_block;
  1272. } else {
  1273. *next_blockp = NULL;
  1274. }
  1275. (*mirror_nump)++;
  1276. return 0;
  1277. }
  1278. static int btrfsic_handle_extent_data(
  1279. struct btrfsic_state *state,
  1280. struct btrfsic_block *block,
  1281. struct btrfsic_block_data_ctx *block_ctx,
  1282. u32 item_offset, int force_iodone_flag)
  1283. {
  1284. int ret;
  1285. struct btrfs_file_extent_item file_extent_item;
  1286. u64 file_extent_item_offset;
  1287. u64 next_bytenr;
  1288. u64 num_bytes;
  1289. u64 generation;
  1290. struct btrfsic_block_link *l;
  1291. file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
  1292. item_offset;
  1293. if (file_extent_item_offset +
  1294. offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
  1295. block_ctx->len) {
  1296. printk(KERN_INFO
  1297. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1298. block_ctx->start, block_ctx->dev->name);
  1299. return -1;
  1300. }
  1301. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1302. file_extent_item_offset,
  1303. offsetof(struct btrfs_file_extent_item, disk_num_bytes));
  1304. if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
  1305. ((u64)0) == le64_to_cpu(file_extent_item.disk_bytenr)) {
  1306. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1307. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
  1308. file_extent_item.type,
  1309. (unsigned long long)
  1310. le64_to_cpu(file_extent_item.disk_bytenr));
  1311. return 0;
  1312. }
  1313. if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
  1314. block_ctx->len) {
  1315. printk(KERN_INFO
  1316. "btrfsic: file item out of bounce at logical %llu, dev %s\n",
  1317. block_ctx->start, block_ctx->dev->name);
  1318. return -1;
  1319. }
  1320. btrfsic_read_from_block_data(block_ctx, &file_extent_item,
  1321. file_extent_item_offset,
  1322. sizeof(struct btrfs_file_extent_item));
  1323. next_bytenr = le64_to_cpu(file_extent_item.disk_bytenr) +
  1324. le64_to_cpu(file_extent_item.offset);
  1325. generation = le64_to_cpu(file_extent_item.generation);
  1326. num_bytes = le64_to_cpu(file_extent_item.num_bytes);
  1327. generation = le64_to_cpu(file_extent_item.generation);
  1328. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1329. printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
  1330. " offset = %llu, num_bytes = %llu\n",
  1331. file_extent_item.type,
  1332. (unsigned long long)
  1333. le64_to_cpu(file_extent_item.disk_bytenr),
  1334. (unsigned long long)le64_to_cpu(file_extent_item.offset),
  1335. (unsigned long long)num_bytes);
  1336. while (num_bytes > 0) {
  1337. u32 chunk_len;
  1338. int num_copies;
  1339. int mirror_num;
  1340. if (num_bytes > state->datablock_size)
  1341. chunk_len = state->datablock_size;
  1342. else
  1343. chunk_len = num_bytes;
  1344. num_copies =
  1345. btrfs_num_copies(state->root->fs_info,
  1346. next_bytenr, state->datablock_size);
  1347. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  1348. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  1349. (unsigned long long)next_bytenr, num_copies);
  1350. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  1351. struct btrfsic_block_data_ctx next_block_ctx;
  1352. struct btrfsic_block *next_block;
  1353. int block_was_created;
  1354. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1355. printk(KERN_INFO "btrfsic_handle_extent_data("
  1356. "mirror_num=%d)\n", mirror_num);
  1357. if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
  1358. printk(KERN_INFO
  1359. "\tdisk_bytenr = %llu, num_bytes %u\n",
  1360. (unsigned long long)next_bytenr,
  1361. chunk_len);
  1362. ret = btrfsic_map_block(state, next_bytenr,
  1363. chunk_len, &next_block_ctx,
  1364. mirror_num);
  1365. if (ret) {
  1366. printk(KERN_INFO
  1367. "btrfsic: btrfsic_map_block(@%llu,"
  1368. " mirror=%d) failed!\n",
  1369. (unsigned long long)next_bytenr,
  1370. mirror_num);
  1371. return -1;
  1372. }
  1373. next_block = btrfsic_block_lookup_or_add(
  1374. state,
  1375. &next_block_ctx,
  1376. "referenced ",
  1377. 0,
  1378. force_iodone_flag,
  1379. !force_iodone_flag,
  1380. mirror_num,
  1381. &block_was_created);
  1382. if (NULL == next_block) {
  1383. printk(KERN_INFO
  1384. "btrfsic: error, kmalloc failed!\n");
  1385. btrfsic_release_block_ctx(&next_block_ctx);
  1386. return -1;
  1387. }
  1388. if (!block_was_created) {
  1389. if (next_block->logical_bytenr != next_bytenr &&
  1390. !(!next_block->is_metadata &&
  1391. 0 == next_block->logical_bytenr)) {
  1392. printk(KERN_INFO
  1393. "Referenced block"
  1394. " @%llu (%s/%llu/%d)"
  1395. " found in hash table, D,"
  1396. " bytenr mismatch"
  1397. " (!= stored %llu).\n",
  1398. (unsigned long long)next_bytenr,
  1399. next_block_ctx.dev->name,
  1400. (unsigned long long)
  1401. next_block_ctx.dev_bytenr,
  1402. mirror_num,
  1403. (unsigned long long)
  1404. next_block->logical_bytenr);
  1405. }
  1406. next_block->logical_bytenr = next_bytenr;
  1407. next_block->mirror_num = mirror_num;
  1408. }
  1409. l = btrfsic_block_link_lookup_or_add(state,
  1410. &next_block_ctx,
  1411. next_block, block,
  1412. generation);
  1413. btrfsic_release_block_ctx(&next_block_ctx);
  1414. if (NULL == l)
  1415. return -1;
  1416. }
  1417. next_bytenr += chunk_len;
  1418. num_bytes -= chunk_len;
  1419. }
  1420. return 0;
  1421. }
  1422. static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
  1423. struct btrfsic_block_data_ctx *block_ctx_out,
  1424. int mirror_num)
  1425. {
  1426. int ret;
  1427. u64 length;
  1428. struct btrfs_bio *multi = NULL;
  1429. struct btrfs_device *device;
  1430. length = len;
  1431. ret = btrfs_map_block(state->root->fs_info, READ,
  1432. bytenr, &length, &multi, mirror_num);
  1433. if (ret) {
  1434. block_ctx_out->start = 0;
  1435. block_ctx_out->dev_bytenr = 0;
  1436. block_ctx_out->len = 0;
  1437. block_ctx_out->dev = NULL;
  1438. block_ctx_out->datav = NULL;
  1439. block_ctx_out->pagev = NULL;
  1440. block_ctx_out->mem_to_free = NULL;
  1441. return ret;
  1442. }
  1443. device = multi->stripes[0].dev;
  1444. block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
  1445. block_ctx_out->dev_bytenr = multi->stripes[0].physical;
  1446. block_ctx_out->start = bytenr;
  1447. block_ctx_out->len = len;
  1448. block_ctx_out->datav = NULL;
  1449. block_ctx_out->pagev = NULL;
  1450. block_ctx_out->mem_to_free = NULL;
  1451. kfree(multi);
  1452. if (NULL == block_ctx_out->dev) {
  1453. ret = -ENXIO;
  1454. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
  1455. }
  1456. return ret;
  1457. }
  1458. static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
  1459. u32 len, struct block_device *bdev,
  1460. struct btrfsic_block_data_ctx *block_ctx_out)
  1461. {
  1462. block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
  1463. block_ctx_out->dev_bytenr = bytenr;
  1464. block_ctx_out->start = bytenr;
  1465. block_ctx_out->len = len;
  1466. block_ctx_out->datav = NULL;
  1467. block_ctx_out->pagev = NULL;
  1468. block_ctx_out->mem_to_free = NULL;
  1469. if (NULL != block_ctx_out->dev) {
  1470. return 0;
  1471. } else {
  1472. printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
  1473. return -ENXIO;
  1474. }
  1475. }
  1476. static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
  1477. {
  1478. if (block_ctx->mem_to_free) {
  1479. unsigned int num_pages;
  1480. BUG_ON(!block_ctx->datav);
  1481. BUG_ON(!block_ctx->pagev);
  1482. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1483. PAGE_CACHE_SHIFT;
  1484. while (num_pages > 0) {
  1485. num_pages--;
  1486. if (block_ctx->datav[num_pages]) {
  1487. kunmap(block_ctx->pagev[num_pages]);
  1488. block_ctx->datav[num_pages] = NULL;
  1489. }
  1490. if (block_ctx->pagev[num_pages]) {
  1491. __free_page(block_ctx->pagev[num_pages]);
  1492. block_ctx->pagev[num_pages] = NULL;
  1493. }
  1494. }
  1495. kfree(block_ctx->mem_to_free);
  1496. block_ctx->mem_to_free = NULL;
  1497. block_ctx->pagev = NULL;
  1498. block_ctx->datav = NULL;
  1499. }
  1500. }
  1501. static int btrfsic_read_block(struct btrfsic_state *state,
  1502. struct btrfsic_block_data_ctx *block_ctx)
  1503. {
  1504. unsigned int num_pages;
  1505. unsigned int i;
  1506. u64 dev_bytenr;
  1507. int ret;
  1508. BUG_ON(block_ctx->datav);
  1509. BUG_ON(block_ctx->pagev);
  1510. BUG_ON(block_ctx->mem_to_free);
  1511. if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
  1512. printk(KERN_INFO
  1513. "btrfsic: read_block() with unaligned bytenr %llu\n",
  1514. (unsigned long long)block_ctx->dev_bytenr);
  1515. return -1;
  1516. }
  1517. num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
  1518. PAGE_CACHE_SHIFT;
  1519. block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
  1520. sizeof(*block_ctx->pagev)) *
  1521. num_pages, GFP_NOFS);
  1522. if (!block_ctx->mem_to_free)
  1523. return -1;
  1524. block_ctx->datav = block_ctx->mem_to_free;
  1525. block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
  1526. for (i = 0; i < num_pages; i++) {
  1527. block_ctx->pagev[i] = alloc_page(GFP_NOFS);
  1528. if (!block_ctx->pagev[i])
  1529. return -1;
  1530. }
  1531. dev_bytenr = block_ctx->dev_bytenr;
  1532. for (i = 0; i < num_pages;) {
  1533. struct bio *bio;
  1534. unsigned int j;
  1535. DECLARE_COMPLETION_ONSTACK(complete);
  1536. bio = bio_alloc(GFP_NOFS, num_pages - i);
  1537. if (!bio) {
  1538. printk(KERN_INFO
  1539. "btrfsic: bio_alloc() for %u pages failed!\n",
  1540. num_pages - i);
  1541. return -1;
  1542. }
  1543. bio->bi_bdev = block_ctx->dev->bdev;
  1544. bio->bi_sector = dev_bytenr >> 9;
  1545. bio->bi_end_io = btrfsic_complete_bio_end_io;
  1546. bio->bi_private = &complete;
  1547. for (j = i; j < num_pages; j++) {
  1548. ret = bio_add_page(bio, block_ctx->pagev[j],
  1549. PAGE_CACHE_SIZE, 0);
  1550. if (PAGE_CACHE_SIZE != ret)
  1551. break;
  1552. }
  1553. if (j == i) {
  1554. printk(KERN_INFO
  1555. "btrfsic: error, failed to add a single page!\n");
  1556. return -1;
  1557. }
  1558. submit_bio(READ, bio);
  1559. /* this will also unplug the queue */
  1560. wait_for_completion(&complete);
  1561. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1562. printk(KERN_INFO
  1563. "btrfsic: read error at logical %llu dev %s!\n",
  1564. block_ctx->start, block_ctx->dev->name);
  1565. bio_put(bio);
  1566. return -1;
  1567. }
  1568. bio_put(bio);
  1569. dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
  1570. i = j;
  1571. }
  1572. for (i = 0; i < num_pages; i++) {
  1573. block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
  1574. if (!block_ctx->datav[i]) {
  1575. printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
  1576. block_ctx->dev->name);
  1577. return -1;
  1578. }
  1579. }
  1580. return block_ctx->len;
  1581. }
  1582. static void btrfsic_complete_bio_end_io(struct bio *bio, int err)
  1583. {
  1584. complete((struct completion *)bio->bi_private);
  1585. }
  1586. static void btrfsic_dump_database(struct btrfsic_state *state)
  1587. {
  1588. struct list_head *elem_all;
  1589. BUG_ON(NULL == state);
  1590. printk(KERN_INFO "all_blocks_list:\n");
  1591. list_for_each(elem_all, &state->all_blocks_list) {
  1592. const struct btrfsic_block *const b_all =
  1593. list_entry(elem_all, struct btrfsic_block,
  1594. all_blocks_node);
  1595. struct list_head *elem_ref_to;
  1596. struct list_head *elem_ref_from;
  1597. printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
  1598. btrfsic_get_block_type(state, b_all),
  1599. (unsigned long long)b_all->logical_bytenr,
  1600. b_all->dev_state->name,
  1601. (unsigned long long)b_all->dev_bytenr,
  1602. b_all->mirror_num);
  1603. list_for_each(elem_ref_to, &b_all->ref_to_list) {
  1604. const struct btrfsic_block_link *const l =
  1605. list_entry(elem_ref_to,
  1606. struct btrfsic_block_link,
  1607. node_ref_to);
  1608. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1609. " refers %u* to"
  1610. " %c @%llu (%s/%llu/%d)\n",
  1611. btrfsic_get_block_type(state, b_all),
  1612. (unsigned long long)b_all->logical_bytenr,
  1613. b_all->dev_state->name,
  1614. (unsigned long long)b_all->dev_bytenr,
  1615. b_all->mirror_num,
  1616. l->ref_cnt,
  1617. btrfsic_get_block_type(state, l->block_ref_to),
  1618. (unsigned long long)
  1619. l->block_ref_to->logical_bytenr,
  1620. l->block_ref_to->dev_state->name,
  1621. (unsigned long long)l->block_ref_to->dev_bytenr,
  1622. l->block_ref_to->mirror_num);
  1623. }
  1624. list_for_each(elem_ref_from, &b_all->ref_from_list) {
  1625. const struct btrfsic_block_link *const l =
  1626. list_entry(elem_ref_from,
  1627. struct btrfsic_block_link,
  1628. node_ref_from);
  1629. printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
  1630. " is ref %u* from"
  1631. " %c @%llu (%s/%llu/%d)\n",
  1632. btrfsic_get_block_type(state, b_all),
  1633. (unsigned long long)b_all->logical_bytenr,
  1634. b_all->dev_state->name,
  1635. (unsigned long long)b_all->dev_bytenr,
  1636. b_all->mirror_num,
  1637. l->ref_cnt,
  1638. btrfsic_get_block_type(state, l->block_ref_from),
  1639. (unsigned long long)
  1640. l->block_ref_from->logical_bytenr,
  1641. l->block_ref_from->dev_state->name,
  1642. (unsigned long long)
  1643. l->block_ref_from->dev_bytenr,
  1644. l->block_ref_from->mirror_num);
  1645. }
  1646. printk(KERN_INFO "\n");
  1647. }
  1648. }
  1649. /*
  1650. * Test whether the disk block contains a tree block (leaf or node)
  1651. * (note that this test fails for the super block)
  1652. */
  1653. static int btrfsic_test_for_metadata(struct btrfsic_state *state,
  1654. char **datav, unsigned int num_pages)
  1655. {
  1656. struct btrfs_header *h;
  1657. u8 csum[BTRFS_CSUM_SIZE];
  1658. u32 crc = ~(u32)0;
  1659. unsigned int i;
  1660. if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
  1661. return 1; /* not metadata */
  1662. num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
  1663. h = (struct btrfs_header *)datav[0];
  1664. if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
  1665. return 1;
  1666. for (i = 0; i < num_pages; i++) {
  1667. u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
  1668. size_t sublen = i ? PAGE_CACHE_SIZE :
  1669. (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
  1670. crc = crc32c(crc, data, sublen);
  1671. }
  1672. btrfs_csum_final(crc, csum);
  1673. if (memcmp(csum, h->csum, state->csum_size))
  1674. return 1;
  1675. return 0; /* is metadata */
  1676. }
  1677. static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
  1678. u64 dev_bytenr, char **mapped_datav,
  1679. unsigned int num_pages,
  1680. struct bio *bio, int *bio_is_patched,
  1681. struct buffer_head *bh,
  1682. int submit_bio_bh_rw)
  1683. {
  1684. int is_metadata;
  1685. struct btrfsic_block *block;
  1686. struct btrfsic_block_data_ctx block_ctx;
  1687. int ret;
  1688. struct btrfsic_state *state = dev_state->state;
  1689. struct block_device *bdev = dev_state->bdev;
  1690. unsigned int processed_len;
  1691. if (NULL != bio_is_patched)
  1692. *bio_is_patched = 0;
  1693. again:
  1694. if (num_pages == 0)
  1695. return;
  1696. processed_len = 0;
  1697. is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
  1698. num_pages));
  1699. block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
  1700. &state->block_hashtable);
  1701. if (NULL != block) {
  1702. u64 bytenr = 0;
  1703. struct list_head *elem_ref_to;
  1704. struct list_head *tmp_ref_to;
  1705. if (block->is_superblock) {
  1706. bytenr = le64_to_cpu(((struct btrfs_super_block *)
  1707. mapped_datav[0])->bytenr);
  1708. if (num_pages * PAGE_CACHE_SIZE <
  1709. BTRFS_SUPER_INFO_SIZE) {
  1710. printk(KERN_INFO
  1711. "btrfsic: cannot work with too short bios!\n");
  1712. return;
  1713. }
  1714. is_metadata = 1;
  1715. BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
  1716. processed_len = BTRFS_SUPER_INFO_SIZE;
  1717. if (state->print_mask &
  1718. BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
  1719. printk(KERN_INFO
  1720. "[before new superblock is written]:\n");
  1721. btrfsic_dump_tree_sub(state, block, 0);
  1722. }
  1723. }
  1724. if (is_metadata) {
  1725. if (!block->is_superblock) {
  1726. if (num_pages * PAGE_CACHE_SIZE <
  1727. state->metablock_size) {
  1728. printk(KERN_INFO
  1729. "btrfsic: cannot work with too short bios!\n");
  1730. return;
  1731. }
  1732. processed_len = state->metablock_size;
  1733. bytenr = le64_to_cpu(((struct btrfs_header *)
  1734. mapped_datav[0])->bytenr);
  1735. btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
  1736. dev_state,
  1737. dev_bytenr);
  1738. }
  1739. if (block->logical_bytenr != bytenr) {
  1740. printk(KERN_INFO
  1741. "Written block @%llu (%s/%llu/%d)"
  1742. " found in hash table, %c,"
  1743. " bytenr mismatch"
  1744. " (!= stored %llu).\n",
  1745. (unsigned long long)bytenr,
  1746. dev_state->name,
  1747. (unsigned long long)dev_bytenr,
  1748. block->mirror_num,
  1749. btrfsic_get_block_type(state, block),
  1750. (unsigned long long)
  1751. block->logical_bytenr);
  1752. block->logical_bytenr = bytenr;
  1753. } else if (state->print_mask &
  1754. BTRFSIC_PRINT_MASK_VERBOSE)
  1755. printk(KERN_INFO
  1756. "Written block @%llu (%s/%llu/%d)"
  1757. " found in hash table, %c.\n",
  1758. (unsigned long long)bytenr,
  1759. dev_state->name,
  1760. (unsigned long long)dev_bytenr,
  1761. block->mirror_num,
  1762. btrfsic_get_block_type(state, block));
  1763. } else {
  1764. if (num_pages * PAGE_CACHE_SIZE <
  1765. state->datablock_size) {
  1766. printk(KERN_INFO
  1767. "btrfsic: cannot work with too short bios!\n");
  1768. return;
  1769. }
  1770. processed_len = state->datablock_size;
  1771. bytenr = block->logical_bytenr;
  1772. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1773. printk(KERN_INFO
  1774. "Written block @%llu (%s/%llu/%d)"
  1775. " found in hash table, %c.\n",
  1776. (unsigned long long)bytenr,
  1777. dev_state->name,
  1778. (unsigned long long)dev_bytenr,
  1779. block->mirror_num,
  1780. btrfsic_get_block_type(state, block));
  1781. }
  1782. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1783. printk(KERN_INFO
  1784. "ref_to_list: %cE, ref_from_list: %cE\n",
  1785. list_empty(&block->ref_to_list) ? ' ' : '!',
  1786. list_empty(&block->ref_from_list) ? ' ' : '!');
  1787. if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
  1788. printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
  1789. " @%llu (%s/%llu/%d), old(gen=%llu,"
  1790. " objectid=%llu, type=%d, offset=%llu),"
  1791. " new(gen=%llu),"
  1792. " which is referenced by most recent superblock"
  1793. " (superblockgen=%llu)!\n",
  1794. btrfsic_get_block_type(state, block),
  1795. (unsigned long long)bytenr,
  1796. dev_state->name,
  1797. (unsigned long long)dev_bytenr,
  1798. block->mirror_num,
  1799. (unsigned long long)block->generation,
  1800. (unsigned long long)
  1801. le64_to_cpu(block->disk_key.objectid),
  1802. block->disk_key.type,
  1803. (unsigned long long)
  1804. le64_to_cpu(block->disk_key.offset),
  1805. (unsigned long long)
  1806. le64_to_cpu(((struct btrfs_header *)
  1807. mapped_datav[0])->generation),
  1808. (unsigned long long)
  1809. state->max_superblock_generation);
  1810. btrfsic_dump_tree(state);
  1811. }
  1812. if (!block->is_iodone && !block->never_written) {
  1813. printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
  1814. " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
  1815. " which is not yet iodone!\n",
  1816. btrfsic_get_block_type(state, block),
  1817. (unsigned long long)bytenr,
  1818. dev_state->name,
  1819. (unsigned long long)dev_bytenr,
  1820. block->mirror_num,
  1821. (unsigned long long)block->generation,
  1822. (unsigned long long)
  1823. le64_to_cpu(((struct btrfs_header *)
  1824. mapped_datav[0])->generation));
  1825. /* it would not be safe to go on */
  1826. btrfsic_dump_tree(state);
  1827. goto continue_loop;
  1828. }
  1829. /*
  1830. * Clear all references of this block. Do not free
  1831. * the block itself even if is not referenced anymore
  1832. * because it still carries valueable information
  1833. * like whether it was ever written and IO completed.
  1834. */
  1835. list_for_each_safe(elem_ref_to, tmp_ref_to,
  1836. &block->ref_to_list) {
  1837. struct btrfsic_block_link *const l =
  1838. list_entry(elem_ref_to,
  1839. struct btrfsic_block_link,
  1840. node_ref_to);
  1841. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1842. btrfsic_print_rem_link(state, l);
  1843. l->ref_cnt--;
  1844. if (0 == l->ref_cnt) {
  1845. list_del(&l->node_ref_to);
  1846. list_del(&l->node_ref_from);
  1847. btrfsic_block_link_hashtable_remove(l);
  1848. btrfsic_block_link_free(l);
  1849. }
  1850. }
  1851. if (block->is_superblock)
  1852. ret = btrfsic_map_superblock(state, bytenr,
  1853. processed_len,
  1854. bdev, &block_ctx);
  1855. else
  1856. ret = btrfsic_map_block(state, bytenr, processed_len,
  1857. &block_ctx, 0);
  1858. if (ret) {
  1859. printk(KERN_INFO
  1860. "btrfsic: btrfsic_map_block(root @%llu)"
  1861. " failed!\n", (unsigned long long)bytenr);
  1862. goto continue_loop;
  1863. }
  1864. block_ctx.datav = mapped_datav;
  1865. /* the following is required in case of writes to mirrors,
  1866. * use the same that was used for the lookup */
  1867. block_ctx.dev = dev_state;
  1868. block_ctx.dev_bytenr = dev_bytenr;
  1869. if (is_metadata || state->include_extent_data) {
  1870. block->never_written = 0;
  1871. block->iodone_w_error = 0;
  1872. if (NULL != bio) {
  1873. block->is_iodone = 0;
  1874. BUG_ON(NULL == bio_is_patched);
  1875. if (!*bio_is_patched) {
  1876. block->orig_bio_bh_private =
  1877. bio->bi_private;
  1878. block->orig_bio_bh_end_io.bio =
  1879. bio->bi_end_io;
  1880. block->next_in_same_bio = NULL;
  1881. bio->bi_private = block;
  1882. bio->bi_end_io = btrfsic_bio_end_io;
  1883. *bio_is_patched = 1;
  1884. } else {
  1885. struct btrfsic_block *chained_block =
  1886. (struct btrfsic_block *)
  1887. bio->bi_private;
  1888. BUG_ON(NULL == chained_block);
  1889. block->orig_bio_bh_private =
  1890. chained_block->orig_bio_bh_private;
  1891. block->orig_bio_bh_end_io.bio =
  1892. chained_block->orig_bio_bh_end_io.
  1893. bio;
  1894. block->next_in_same_bio = chained_block;
  1895. bio->bi_private = block;
  1896. }
  1897. } else if (NULL != bh) {
  1898. block->is_iodone = 0;
  1899. block->orig_bio_bh_private = bh->b_private;
  1900. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  1901. block->next_in_same_bio = NULL;
  1902. bh->b_private = block;
  1903. bh->b_end_io = btrfsic_bh_end_io;
  1904. } else {
  1905. block->is_iodone = 1;
  1906. block->orig_bio_bh_private = NULL;
  1907. block->orig_bio_bh_end_io.bio = NULL;
  1908. block->next_in_same_bio = NULL;
  1909. }
  1910. }
  1911. block->flush_gen = dev_state->last_flush_gen + 1;
  1912. block->submit_bio_bh_rw = submit_bio_bh_rw;
  1913. if (is_metadata) {
  1914. block->logical_bytenr = bytenr;
  1915. block->is_metadata = 1;
  1916. if (block->is_superblock) {
  1917. BUG_ON(PAGE_CACHE_SIZE !=
  1918. BTRFS_SUPER_INFO_SIZE);
  1919. ret = btrfsic_process_written_superblock(
  1920. state,
  1921. block,
  1922. (struct btrfs_super_block *)
  1923. mapped_datav[0]);
  1924. if (state->print_mask &
  1925. BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
  1926. printk(KERN_INFO
  1927. "[after new superblock is written]:\n");
  1928. btrfsic_dump_tree_sub(state, block, 0);
  1929. }
  1930. } else {
  1931. block->mirror_num = 0; /* unknown */
  1932. ret = btrfsic_process_metablock(
  1933. state,
  1934. block,
  1935. &block_ctx,
  1936. 0, 0);
  1937. }
  1938. if (ret)
  1939. printk(KERN_INFO
  1940. "btrfsic: btrfsic_process_metablock"
  1941. "(root @%llu) failed!\n",
  1942. (unsigned long long)dev_bytenr);
  1943. } else {
  1944. block->is_metadata = 0;
  1945. block->mirror_num = 0; /* unknown */
  1946. block->generation = BTRFSIC_GENERATION_UNKNOWN;
  1947. if (!state->include_extent_data
  1948. && list_empty(&block->ref_from_list)) {
  1949. /*
  1950. * disk block is overwritten with extent
  1951. * data (not meta data) and we are configured
  1952. * to not include extent data: take the
  1953. * chance and free the block's memory
  1954. */
  1955. btrfsic_block_hashtable_remove(block);
  1956. list_del(&block->all_blocks_node);
  1957. btrfsic_block_free(block);
  1958. }
  1959. }
  1960. btrfsic_release_block_ctx(&block_ctx);
  1961. } else {
  1962. /* block has not been found in hash table */
  1963. u64 bytenr;
  1964. if (!is_metadata) {
  1965. processed_len = state->datablock_size;
  1966. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1967. printk(KERN_INFO "Written block (%s/%llu/?)"
  1968. " !found in hash table, D.\n",
  1969. dev_state->name,
  1970. (unsigned long long)dev_bytenr);
  1971. if (!state->include_extent_data) {
  1972. /* ignore that written D block */
  1973. goto continue_loop;
  1974. }
  1975. /* this is getting ugly for the
  1976. * include_extent_data case... */
  1977. bytenr = 0; /* unknown */
  1978. block_ctx.start = bytenr;
  1979. block_ctx.len = processed_len;
  1980. block_ctx.mem_to_free = NULL;
  1981. block_ctx.pagev = NULL;
  1982. } else {
  1983. processed_len = state->metablock_size;
  1984. bytenr = le64_to_cpu(((struct btrfs_header *)
  1985. mapped_datav[0])->bytenr);
  1986. btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
  1987. dev_bytenr);
  1988. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  1989. printk(KERN_INFO
  1990. "Written block @%llu (%s/%llu/?)"
  1991. " !found in hash table, M.\n",
  1992. (unsigned long long)bytenr,
  1993. dev_state->name,
  1994. (unsigned long long)dev_bytenr);
  1995. ret = btrfsic_map_block(state, bytenr, processed_len,
  1996. &block_ctx, 0);
  1997. if (ret) {
  1998. printk(KERN_INFO
  1999. "btrfsic: btrfsic_map_block(root @%llu)"
  2000. " failed!\n",
  2001. (unsigned long long)dev_bytenr);
  2002. goto continue_loop;
  2003. }
  2004. }
  2005. block_ctx.datav = mapped_datav;
  2006. /* the following is required in case of writes to mirrors,
  2007. * use the same that was used for the lookup */
  2008. block_ctx.dev = dev_state;
  2009. block_ctx.dev_bytenr = dev_bytenr;
  2010. block = btrfsic_block_alloc();
  2011. if (NULL == block) {
  2012. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  2013. btrfsic_release_block_ctx(&block_ctx);
  2014. goto continue_loop;
  2015. }
  2016. block->dev_state = dev_state;
  2017. block->dev_bytenr = dev_bytenr;
  2018. block->logical_bytenr = bytenr;
  2019. block->is_metadata = is_metadata;
  2020. block->never_written = 0;
  2021. block->iodone_w_error = 0;
  2022. block->mirror_num = 0; /* unknown */
  2023. block->flush_gen = dev_state->last_flush_gen + 1;
  2024. block->submit_bio_bh_rw = submit_bio_bh_rw;
  2025. if (NULL != bio) {
  2026. block->is_iodone = 0;
  2027. BUG_ON(NULL == bio_is_patched);
  2028. if (!*bio_is_patched) {
  2029. block->orig_bio_bh_private = bio->bi_private;
  2030. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2031. block->next_in_same_bio = NULL;
  2032. bio->bi_private = block;
  2033. bio->bi_end_io = btrfsic_bio_end_io;
  2034. *bio_is_patched = 1;
  2035. } else {
  2036. struct btrfsic_block *chained_block =
  2037. (struct btrfsic_block *)
  2038. bio->bi_private;
  2039. BUG_ON(NULL == chained_block);
  2040. block->orig_bio_bh_private =
  2041. chained_block->orig_bio_bh_private;
  2042. block->orig_bio_bh_end_io.bio =
  2043. chained_block->orig_bio_bh_end_io.bio;
  2044. block->next_in_same_bio = chained_block;
  2045. bio->bi_private = block;
  2046. }
  2047. } else if (NULL != bh) {
  2048. block->is_iodone = 0;
  2049. block->orig_bio_bh_private = bh->b_private;
  2050. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2051. block->next_in_same_bio = NULL;
  2052. bh->b_private = block;
  2053. bh->b_end_io = btrfsic_bh_end_io;
  2054. } else {
  2055. block->is_iodone = 1;
  2056. block->orig_bio_bh_private = NULL;
  2057. block->orig_bio_bh_end_io.bio = NULL;
  2058. block->next_in_same_bio = NULL;
  2059. }
  2060. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2061. printk(KERN_INFO
  2062. "New written %c-block @%llu (%s/%llu/%d)\n",
  2063. is_metadata ? 'M' : 'D',
  2064. (unsigned long long)block->logical_bytenr,
  2065. block->dev_state->name,
  2066. (unsigned long long)block->dev_bytenr,
  2067. block->mirror_num);
  2068. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2069. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2070. if (is_metadata) {
  2071. ret = btrfsic_process_metablock(state, block,
  2072. &block_ctx, 0, 0);
  2073. if (ret)
  2074. printk(KERN_INFO
  2075. "btrfsic: process_metablock(root @%llu)"
  2076. " failed!\n",
  2077. (unsigned long long)dev_bytenr);
  2078. }
  2079. btrfsic_release_block_ctx(&block_ctx);
  2080. }
  2081. continue_loop:
  2082. BUG_ON(!processed_len);
  2083. dev_bytenr += processed_len;
  2084. mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
  2085. num_pages -= processed_len >> PAGE_CACHE_SHIFT;
  2086. goto again;
  2087. }
  2088. static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
  2089. {
  2090. struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
  2091. int iodone_w_error;
  2092. /* mutex is not held! This is not save if IO is not yet completed
  2093. * on umount */
  2094. iodone_w_error = 0;
  2095. if (bio_error_status)
  2096. iodone_w_error = 1;
  2097. BUG_ON(NULL == block);
  2098. bp->bi_private = block->orig_bio_bh_private;
  2099. bp->bi_end_io = block->orig_bio_bh_end_io.bio;
  2100. do {
  2101. struct btrfsic_block *next_block;
  2102. struct btrfsic_dev_state *const dev_state = block->dev_state;
  2103. if ((dev_state->state->print_mask &
  2104. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2105. printk(KERN_INFO
  2106. "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
  2107. bio_error_status,
  2108. btrfsic_get_block_type(dev_state->state, block),
  2109. (unsigned long long)block->logical_bytenr,
  2110. dev_state->name,
  2111. (unsigned long long)block->dev_bytenr,
  2112. block->mirror_num);
  2113. next_block = block->next_in_same_bio;
  2114. block->iodone_w_error = iodone_w_error;
  2115. if (block->submit_bio_bh_rw & REQ_FLUSH) {
  2116. dev_state->last_flush_gen++;
  2117. if ((dev_state->state->print_mask &
  2118. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2119. printk(KERN_INFO
  2120. "bio_end_io() new %s flush_gen=%llu\n",
  2121. dev_state->name,
  2122. (unsigned long long)
  2123. dev_state->last_flush_gen);
  2124. }
  2125. if (block->submit_bio_bh_rw & REQ_FUA)
  2126. block->flush_gen = 0; /* FUA completed means block is
  2127. * on disk */
  2128. block->is_iodone = 1; /* for FLUSH, this releases the block */
  2129. block = next_block;
  2130. } while (NULL != block);
  2131. bp->bi_end_io(bp, bio_error_status);
  2132. }
  2133. static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
  2134. {
  2135. struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
  2136. int iodone_w_error = !uptodate;
  2137. struct btrfsic_dev_state *dev_state;
  2138. BUG_ON(NULL == block);
  2139. dev_state = block->dev_state;
  2140. if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2141. printk(KERN_INFO
  2142. "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
  2143. iodone_w_error,
  2144. btrfsic_get_block_type(dev_state->state, block),
  2145. (unsigned long long)block->logical_bytenr,
  2146. block->dev_state->name,
  2147. (unsigned long long)block->dev_bytenr,
  2148. block->mirror_num);
  2149. block->iodone_w_error = iodone_w_error;
  2150. if (block->submit_bio_bh_rw & REQ_FLUSH) {
  2151. dev_state->last_flush_gen++;
  2152. if ((dev_state->state->print_mask &
  2153. BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
  2154. printk(KERN_INFO
  2155. "bh_end_io() new %s flush_gen=%llu\n",
  2156. dev_state->name,
  2157. (unsigned long long)dev_state->last_flush_gen);
  2158. }
  2159. if (block->submit_bio_bh_rw & REQ_FUA)
  2160. block->flush_gen = 0; /* FUA completed means block is on disk */
  2161. bh->b_private = block->orig_bio_bh_private;
  2162. bh->b_end_io = block->orig_bio_bh_end_io.bh;
  2163. block->is_iodone = 1; /* for FLUSH, this releases the block */
  2164. bh->b_end_io(bh, uptodate);
  2165. }
  2166. static int btrfsic_process_written_superblock(
  2167. struct btrfsic_state *state,
  2168. struct btrfsic_block *const superblock,
  2169. struct btrfs_super_block *const super_hdr)
  2170. {
  2171. int pass;
  2172. superblock->generation = btrfs_super_generation(super_hdr);
  2173. if (!(superblock->generation > state->max_superblock_generation ||
  2174. 0 == state->max_superblock_generation)) {
  2175. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2176. printk(KERN_INFO
  2177. "btrfsic: superblock @%llu (%s/%llu/%d)"
  2178. " with old gen %llu <= %llu\n",
  2179. (unsigned long long)superblock->logical_bytenr,
  2180. superblock->dev_state->name,
  2181. (unsigned long long)superblock->dev_bytenr,
  2182. superblock->mirror_num,
  2183. (unsigned long long)
  2184. btrfs_super_generation(super_hdr),
  2185. (unsigned long long)
  2186. state->max_superblock_generation);
  2187. } else {
  2188. if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
  2189. printk(KERN_INFO
  2190. "btrfsic: got new superblock @%llu (%s/%llu/%d)"
  2191. " with new gen %llu > %llu\n",
  2192. (unsigned long long)superblock->logical_bytenr,
  2193. superblock->dev_state->name,
  2194. (unsigned long long)superblock->dev_bytenr,
  2195. superblock->mirror_num,
  2196. (unsigned long long)
  2197. btrfs_super_generation(super_hdr),
  2198. (unsigned long long)
  2199. state->max_superblock_generation);
  2200. state->max_superblock_generation =
  2201. btrfs_super_generation(super_hdr);
  2202. state->latest_superblock = superblock;
  2203. }
  2204. for (pass = 0; pass < 3; pass++) {
  2205. int ret;
  2206. u64 next_bytenr;
  2207. struct btrfsic_block *next_block;
  2208. struct btrfsic_block_data_ctx tmp_next_block_ctx;
  2209. struct btrfsic_block_link *l;
  2210. int num_copies;
  2211. int mirror_num;
  2212. const char *additional_string = NULL;
  2213. struct btrfs_disk_key tmp_disk_key;
  2214. tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
  2215. tmp_disk_key.offset = 0;
  2216. switch (pass) {
  2217. case 0:
  2218. tmp_disk_key.objectid =
  2219. cpu_to_le64(BTRFS_ROOT_TREE_OBJECTID);
  2220. additional_string = "root ";
  2221. next_bytenr = btrfs_super_root(super_hdr);
  2222. if (state->print_mask &
  2223. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2224. printk(KERN_INFO "root@%llu\n",
  2225. (unsigned long long)next_bytenr);
  2226. break;
  2227. case 1:
  2228. tmp_disk_key.objectid =
  2229. cpu_to_le64(BTRFS_CHUNK_TREE_OBJECTID);
  2230. additional_string = "chunk ";
  2231. next_bytenr = btrfs_super_chunk_root(super_hdr);
  2232. if (state->print_mask &
  2233. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2234. printk(KERN_INFO "chunk@%llu\n",
  2235. (unsigned long long)next_bytenr);
  2236. break;
  2237. case 2:
  2238. tmp_disk_key.objectid =
  2239. cpu_to_le64(BTRFS_TREE_LOG_OBJECTID);
  2240. additional_string = "log ";
  2241. next_bytenr = btrfs_super_log_root(super_hdr);
  2242. if (0 == next_bytenr)
  2243. continue;
  2244. if (state->print_mask &
  2245. BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
  2246. printk(KERN_INFO "log@%llu\n",
  2247. (unsigned long long)next_bytenr);
  2248. break;
  2249. }
  2250. num_copies =
  2251. btrfs_num_copies(state->root->fs_info,
  2252. next_bytenr, BTRFS_SUPER_INFO_SIZE);
  2253. if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
  2254. printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
  2255. (unsigned long long)next_bytenr, num_copies);
  2256. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2257. int was_created;
  2258. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2259. printk(KERN_INFO
  2260. "btrfsic_process_written_superblock("
  2261. "mirror_num=%d)\n", mirror_num);
  2262. ret = btrfsic_map_block(state, next_bytenr,
  2263. BTRFS_SUPER_INFO_SIZE,
  2264. &tmp_next_block_ctx,
  2265. mirror_num);
  2266. if (ret) {
  2267. printk(KERN_INFO
  2268. "btrfsic: btrfsic_map_block(@%llu,"
  2269. " mirror=%d) failed!\n",
  2270. (unsigned long long)next_bytenr,
  2271. mirror_num);
  2272. return -1;
  2273. }
  2274. next_block = btrfsic_block_lookup_or_add(
  2275. state,
  2276. &tmp_next_block_ctx,
  2277. additional_string,
  2278. 1, 0, 1,
  2279. mirror_num,
  2280. &was_created);
  2281. if (NULL == next_block) {
  2282. printk(KERN_INFO
  2283. "btrfsic: error, kmalloc failed!\n");
  2284. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2285. return -1;
  2286. }
  2287. next_block->disk_key = tmp_disk_key;
  2288. if (was_created)
  2289. next_block->generation =
  2290. BTRFSIC_GENERATION_UNKNOWN;
  2291. l = btrfsic_block_link_lookup_or_add(
  2292. state,
  2293. &tmp_next_block_ctx,
  2294. next_block,
  2295. superblock,
  2296. BTRFSIC_GENERATION_UNKNOWN);
  2297. btrfsic_release_block_ctx(&tmp_next_block_ctx);
  2298. if (NULL == l)
  2299. return -1;
  2300. }
  2301. }
  2302. if (-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)) {
  2303. WARN_ON(1);
  2304. btrfsic_dump_tree(state);
  2305. }
  2306. return 0;
  2307. }
  2308. static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
  2309. struct btrfsic_block *const block,
  2310. int recursion_level)
  2311. {
  2312. struct list_head *elem_ref_to;
  2313. int ret = 0;
  2314. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2315. /*
  2316. * Note that this situation can happen and does not
  2317. * indicate an error in regular cases. It happens
  2318. * when disk blocks are freed and later reused.
  2319. * The check-integrity module is not aware of any
  2320. * block free operations, it just recognizes block
  2321. * write operations. Therefore it keeps the linkage
  2322. * information for a block until a block is
  2323. * rewritten. This can temporarily cause incorrect
  2324. * and even circular linkage informations. This
  2325. * causes no harm unless such blocks are referenced
  2326. * by the most recent super block.
  2327. */
  2328. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2329. printk(KERN_INFO
  2330. "btrfsic: abort cyclic linkage (case 1).\n");
  2331. return ret;
  2332. }
  2333. /*
  2334. * This algorithm is recursive because the amount of used stack
  2335. * space is very small and the max recursion depth is limited.
  2336. */
  2337. list_for_each(elem_ref_to, &block->ref_to_list) {
  2338. const struct btrfsic_block_link *const l =
  2339. list_entry(elem_ref_to, struct btrfsic_block_link,
  2340. node_ref_to);
  2341. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2342. printk(KERN_INFO
  2343. "rl=%d, %c @%llu (%s/%llu/%d)"
  2344. " %u* refers to %c @%llu (%s/%llu/%d)\n",
  2345. recursion_level,
  2346. btrfsic_get_block_type(state, block),
  2347. (unsigned long long)block->logical_bytenr,
  2348. block->dev_state->name,
  2349. (unsigned long long)block->dev_bytenr,
  2350. block->mirror_num,
  2351. l->ref_cnt,
  2352. btrfsic_get_block_type(state, l->block_ref_to),
  2353. (unsigned long long)
  2354. l->block_ref_to->logical_bytenr,
  2355. l->block_ref_to->dev_state->name,
  2356. (unsigned long long)l->block_ref_to->dev_bytenr,
  2357. l->block_ref_to->mirror_num);
  2358. if (l->block_ref_to->never_written) {
  2359. printk(KERN_INFO "btrfs: attempt to write superblock"
  2360. " which references block %c @%llu (%s/%llu/%d)"
  2361. " which is never written!\n",
  2362. btrfsic_get_block_type(state, l->block_ref_to),
  2363. (unsigned long long)
  2364. l->block_ref_to->logical_bytenr,
  2365. l->block_ref_to->dev_state->name,
  2366. (unsigned long long)l->block_ref_to->dev_bytenr,
  2367. l->block_ref_to->mirror_num);
  2368. ret = -1;
  2369. } else if (!l->block_ref_to->is_iodone) {
  2370. printk(KERN_INFO "btrfs: attempt to write superblock"
  2371. " which references block %c @%llu (%s/%llu/%d)"
  2372. " which is not yet iodone!\n",
  2373. btrfsic_get_block_type(state, l->block_ref_to),
  2374. (unsigned long long)
  2375. l->block_ref_to->logical_bytenr,
  2376. l->block_ref_to->dev_state->name,
  2377. (unsigned long long)l->block_ref_to->dev_bytenr,
  2378. l->block_ref_to->mirror_num);
  2379. ret = -1;
  2380. } else if (l->block_ref_to->iodone_w_error) {
  2381. printk(KERN_INFO "btrfs: attempt to write superblock"
  2382. " which references block %c @%llu (%s/%llu/%d)"
  2383. " which has write error!\n",
  2384. btrfsic_get_block_type(state, l->block_ref_to),
  2385. (unsigned long long)
  2386. l->block_ref_to->logical_bytenr,
  2387. l->block_ref_to->dev_state->name,
  2388. (unsigned long long)l->block_ref_to->dev_bytenr,
  2389. l->block_ref_to->mirror_num);
  2390. ret = -1;
  2391. } else if (l->parent_generation !=
  2392. l->block_ref_to->generation &&
  2393. BTRFSIC_GENERATION_UNKNOWN !=
  2394. l->parent_generation &&
  2395. BTRFSIC_GENERATION_UNKNOWN !=
  2396. l->block_ref_to->generation) {
  2397. printk(KERN_INFO "btrfs: attempt to write superblock"
  2398. " which references block %c @%llu (%s/%llu/%d)"
  2399. " with generation %llu !="
  2400. " parent generation %llu!\n",
  2401. btrfsic_get_block_type(state, l->block_ref_to),
  2402. (unsigned long long)
  2403. l->block_ref_to->logical_bytenr,
  2404. l->block_ref_to->dev_state->name,
  2405. (unsigned long long)l->block_ref_to->dev_bytenr,
  2406. l->block_ref_to->mirror_num,
  2407. (unsigned long long)l->block_ref_to->generation,
  2408. (unsigned long long)l->parent_generation);
  2409. ret = -1;
  2410. } else if (l->block_ref_to->flush_gen >
  2411. l->block_ref_to->dev_state->last_flush_gen) {
  2412. printk(KERN_INFO "btrfs: attempt to write superblock"
  2413. " which references block %c @%llu (%s/%llu/%d)"
  2414. " which is not flushed out of disk's write cache"
  2415. " (block flush_gen=%llu,"
  2416. " dev->flush_gen=%llu)!\n",
  2417. btrfsic_get_block_type(state, l->block_ref_to),
  2418. (unsigned long long)
  2419. l->block_ref_to->logical_bytenr,
  2420. l->block_ref_to->dev_state->name,
  2421. (unsigned long long)l->block_ref_to->dev_bytenr,
  2422. l->block_ref_to->mirror_num,
  2423. (unsigned long long)block->flush_gen,
  2424. (unsigned long long)
  2425. l->block_ref_to->dev_state->last_flush_gen);
  2426. ret = -1;
  2427. } else if (-1 == btrfsic_check_all_ref_blocks(state,
  2428. l->block_ref_to,
  2429. recursion_level +
  2430. 1)) {
  2431. ret = -1;
  2432. }
  2433. }
  2434. return ret;
  2435. }
  2436. static int btrfsic_is_block_ref_by_superblock(
  2437. const struct btrfsic_state *state,
  2438. const struct btrfsic_block *block,
  2439. int recursion_level)
  2440. {
  2441. struct list_head *elem_ref_from;
  2442. if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
  2443. /* refer to comment at "abort cyclic linkage (case 1)" */
  2444. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2445. printk(KERN_INFO
  2446. "btrfsic: abort cyclic linkage (case 2).\n");
  2447. return 0;
  2448. }
  2449. /*
  2450. * This algorithm is recursive because the amount of used stack space
  2451. * is very small and the max recursion depth is limited.
  2452. */
  2453. list_for_each(elem_ref_from, &block->ref_from_list) {
  2454. const struct btrfsic_block_link *const l =
  2455. list_entry(elem_ref_from, struct btrfsic_block_link,
  2456. node_ref_from);
  2457. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2458. printk(KERN_INFO
  2459. "rl=%d, %c @%llu (%s/%llu/%d)"
  2460. " is ref %u* from %c @%llu (%s/%llu/%d)\n",
  2461. recursion_level,
  2462. btrfsic_get_block_type(state, block),
  2463. (unsigned long long)block->logical_bytenr,
  2464. block->dev_state->name,
  2465. (unsigned long long)block->dev_bytenr,
  2466. block->mirror_num,
  2467. l->ref_cnt,
  2468. btrfsic_get_block_type(state, l->block_ref_from),
  2469. (unsigned long long)
  2470. l->block_ref_from->logical_bytenr,
  2471. l->block_ref_from->dev_state->name,
  2472. (unsigned long long)
  2473. l->block_ref_from->dev_bytenr,
  2474. l->block_ref_from->mirror_num);
  2475. if (l->block_ref_from->is_superblock &&
  2476. state->latest_superblock->dev_bytenr ==
  2477. l->block_ref_from->dev_bytenr &&
  2478. state->latest_superblock->dev_state->bdev ==
  2479. l->block_ref_from->dev_state->bdev)
  2480. return 1;
  2481. else if (btrfsic_is_block_ref_by_superblock(state,
  2482. l->block_ref_from,
  2483. recursion_level +
  2484. 1))
  2485. return 1;
  2486. }
  2487. return 0;
  2488. }
  2489. static void btrfsic_print_add_link(const struct btrfsic_state *state,
  2490. const struct btrfsic_block_link *l)
  2491. {
  2492. printk(KERN_INFO
  2493. "Add %u* link from %c @%llu (%s/%llu/%d)"
  2494. " to %c @%llu (%s/%llu/%d).\n",
  2495. l->ref_cnt,
  2496. btrfsic_get_block_type(state, l->block_ref_from),
  2497. (unsigned long long)l->block_ref_from->logical_bytenr,
  2498. l->block_ref_from->dev_state->name,
  2499. (unsigned long long)l->block_ref_from->dev_bytenr,
  2500. l->block_ref_from->mirror_num,
  2501. btrfsic_get_block_type(state, l->block_ref_to),
  2502. (unsigned long long)l->block_ref_to->logical_bytenr,
  2503. l->block_ref_to->dev_state->name,
  2504. (unsigned long long)l->block_ref_to->dev_bytenr,
  2505. l->block_ref_to->mirror_num);
  2506. }
  2507. static void btrfsic_print_rem_link(const struct btrfsic_state *state,
  2508. const struct btrfsic_block_link *l)
  2509. {
  2510. printk(KERN_INFO
  2511. "Rem %u* link from %c @%llu (%s/%llu/%d)"
  2512. " to %c @%llu (%s/%llu/%d).\n",
  2513. l->ref_cnt,
  2514. btrfsic_get_block_type(state, l->block_ref_from),
  2515. (unsigned long long)l->block_ref_from->logical_bytenr,
  2516. l->block_ref_from->dev_state->name,
  2517. (unsigned long long)l->block_ref_from->dev_bytenr,
  2518. l->block_ref_from->mirror_num,
  2519. btrfsic_get_block_type(state, l->block_ref_to),
  2520. (unsigned long long)l->block_ref_to->logical_bytenr,
  2521. l->block_ref_to->dev_state->name,
  2522. (unsigned long long)l->block_ref_to->dev_bytenr,
  2523. l->block_ref_to->mirror_num);
  2524. }
  2525. static char btrfsic_get_block_type(const struct btrfsic_state *state,
  2526. const struct btrfsic_block *block)
  2527. {
  2528. if (block->is_superblock &&
  2529. state->latest_superblock->dev_bytenr == block->dev_bytenr &&
  2530. state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
  2531. return 'S';
  2532. else if (block->is_superblock)
  2533. return 's';
  2534. else if (block->is_metadata)
  2535. return 'M';
  2536. else
  2537. return 'D';
  2538. }
  2539. static void btrfsic_dump_tree(const struct btrfsic_state *state)
  2540. {
  2541. btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
  2542. }
  2543. static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
  2544. const struct btrfsic_block *block,
  2545. int indent_level)
  2546. {
  2547. struct list_head *elem_ref_to;
  2548. int indent_add;
  2549. static char buf[80];
  2550. int cursor_position;
  2551. /*
  2552. * Should better fill an on-stack buffer with a complete line and
  2553. * dump it at once when it is time to print a newline character.
  2554. */
  2555. /*
  2556. * This algorithm is recursive because the amount of used stack space
  2557. * is very small and the max recursion depth is limited.
  2558. */
  2559. indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
  2560. btrfsic_get_block_type(state, block),
  2561. (unsigned long long)block->logical_bytenr,
  2562. block->dev_state->name,
  2563. (unsigned long long)block->dev_bytenr,
  2564. block->mirror_num);
  2565. if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2566. printk("[...]\n");
  2567. return;
  2568. }
  2569. printk(buf);
  2570. indent_level += indent_add;
  2571. if (list_empty(&block->ref_to_list)) {
  2572. printk("\n");
  2573. return;
  2574. }
  2575. if (block->mirror_num > 1 &&
  2576. !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
  2577. printk(" [...]\n");
  2578. return;
  2579. }
  2580. cursor_position = indent_level;
  2581. list_for_each(elem_ref_to, &block->ref_to_list) {
  2582. const struct btrfsic_block_link *const l =
  2583. list_entry(elem_ref_to, struct btrfsic_block_link,
  2584. node_ref_to);
  2585. while (cursor_position < indent_level) {
  2586. printk(" ");
  2587. cursor_position++;
  2588. }
  2589. if (l->ref_cnt > 1)
  2590. indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
  2591. else
  2592. indent_add = sprintf(buf, " --> ");
  2593. if (indent_level + indent_add >
  2594. BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
  2595. printk("[...]\n");
  2596. cursor_position = 0;
  2597. continue;
  2598. }
  2599. printk(buf);
  2600. btrfsic_dump_tree_sub(state, l->block_ref_to,
  2601. indent_level + indent_add);
  2602. cursor_position = 0;
  2603. }
  2604. }
  2605. static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
  2606. struct btrfsic_state *state,
  2607. struct btrfsic_block_data_ctx *next_block_ctx,
  2608. struct btrfsic_block *next_block,
  2609. struct btrfsic_block *from_block,
  2610. u64 parent_generation)
  2611. {
  2612. struct btrfsic_block_link *l;
  2613. l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
  2614. next_block_ctx->dev_bytenr,
  2615. from_block->dev_state->bdev,
  2616. from_block->dev_bytenr,
  2617. &state->block_link_hashtable);
  2618. if (NULL == l) {
  2619. l = btrfsic_block_link_alloc();
  2620. if (NULL == l) {
  2621. printk(KERN_INFO
  2622. "btrfsic: error, kmalloc" " failed!\n");
  2623. return NULL;
  2624. }
  2625. l->block_ref_to = next_block;
  2626. l->block_ref_from = from_block;
  2627. l->ref_cnt = 1;
  2628. l->parent_generation = parent_generation;
  2629. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2630. btrfsic_print_add_link(state, l);
  2631. list_add(&l->node_ref_to, &from_block->ref_to_list);
  2632. list_add(&l->node_ref_from, &next_block->ref_from_list);
  2633. btrfsic_block_link_hashtable_add(l,
  2634. &state->block_link_hashtable);
  2635. } else {
  2636. l->ref_cnt++;
  2637. l->parent_generation = parent_generation;
  2638. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2639. btrfsic_print_add_link(state, l);
  2640. }
  2641. return l;
  2642. }
  2643. static struct btrfsic_block *btrfsic_block_lookup_or_add(
  2644. struct btrfsic_state *state,
  2645. struct btrfsic_block_data_ctx *block_ctx,
  2646. const char *additional_string,
  2647. int is_metadata,
  2648. int is_iodone,
  2649. int never_written,
  2650. int mirror_num,
  2651. int *was_created)
  2652. {
  2653. struct btrfsic_block *block;
  2654. block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
  2655. block_ctx->dev_bytenr,
  2656. &state->block_hashtable);
  2657. if (NULL == block) {
  2658. struct btrfsic_dev_state *dev_state;
  2659. block = btrfsic_block_alloc();
  2660. if (NULL == block) {
  2661. printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
  2662. return NULL;
  2663. }
  2664. dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
  2665. if (NULL == dev_state) {
  2666. printk(KERN_INFO
  2667. "btrfsic: error, lookup dev_state failed!\n");
  2668. btrfsic_block_free(block);
  2669. return NULL;
  2670. }
  2671. block->dev_state = dev_state;
  2672. block->dev_bytenr = block_ctx->dev_bytenr;
  2673. block->logical_bytenr = block_ctx->start;
  2674. block->is_metadata = is_metadata;
  2675. block->is_iodone = is_iodone;
  2676. block->never_written = never_written;
  2677. block->mirror_num = mirror_num;
  2678. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  2679. printk(KERN_INFO
  2680. "New %s%c-block @%llu (%s/%llu/%d)\n",
  2681. additional_string,
  2682. btrfsic_get_block_type(state, block),
  2683. (unsigned long long)block->logical_bytenr,
  2684. dev_state->name,
  2685. (unsigned long long)block->dev_bytenr,
  2686. mirror_num);
  2687. list_add(&block->all_blocks_node, &state->all_blocks_list);
  2688. btrfsic_block_hashtable_add(block, &state->block_hashtable);
  2689. if (NULL != was_created)
  2690. *was_created = 1;
  2691. } else {
  2692. if (NULL != was_created)
  2693. *was_created = 0;
  2694. }
  2695. return block;
  2696. }
  2697. static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
  2698. u64 bytenr,
  2699. struct btrfsic_dev_state *dev_state,
  2700. u64 dev_bytenr)
  2701. {
  2702. int num_copies;
  2703. int mirror_num;
  2704. int ret;
  2705. struct btrfsic_block_data_ctx block_ctx;
  2706. int match = 0;
  2707. num_copies = btrfs_num_copies(state->root->fs_info,
  2708. bytenr, state->metablock_size);
  2709. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2710. ret = btrfsic_map_block(state, bytenr, state->metablock_size,
  2711. &block_ctx, mirror_num);
  2712. if (ret) {
  2713. printk(KERN_INFO "btrfsic:"
  2714. " btrfsic_map_block(logical @%llu,"
  2715. " mirror %d) failed!\n",
  2716. (unsigned long long)bytenr, mirror_num);
  2717. continue;
  2718. }
  2719. if (dev_state->bdev == block_ctx.dev->bdev &&
  2720. dev_bytenr == block_ctx.dev_bytenr) {
  2721. match++;
  2722. btrfsic_release_block_ctx(&block_ctx);
  2723. break;
  2724. }
  2725. btrfsic_release_block_ctx(&block_ctx);
  2726. }
  2727. if (!match) {
  2728. printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
  2729. " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
  2730. " phys_bytenr=%llu)!\n",
  2731. (unsigned long long)bytenr, dev_state->name,
  2732. (unsigned long long)dev_bytenr);
  2733. for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
  2734. ret = btrfsic_map_block(state, bytenr,
  2735. state->metablock_size,
  2736. &block_ctx, mirror_num);
  2737. if (ret)
  2738. continue;
  2739. printk(KERN_INFO "Read logical bytenr @%llu maps to"
  2740. " (%s/%llu/%d)\n",
  2741. (unsigned long long)bytenr,
  2742. block_ctx.dev->name,
  2743. (unsigned long long)block_ctx.dev_bytenr,
  2744. mirror_num);
  2745. }
  2746. WARN_ON(1);
  2747. }
  2748. }
  2749. static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
  2750. struct block_device *bdev)
  2751. {
  2752. struct btrfsic_dev_state *ds;
  2753. ds = btrfsic_dev_state_hashtable_lookup(bdev,
  2754. &btrfsic_dev_state_hashtable);
  2755. return ds;
  2756. }
  2757. int btrfsic_submit_bh(int rw, struct buffer_head *bh)
  2758. {
  2759. struct btrfsic_dev_state *dev_state;
  2760. if (!btrfsic_is_initialized)
  2761. return submit_bh(rw, bh);
  2762. mutex_lock(&btrfsic_mutex);
  2763. /* since btrfsic_submit_bh() might also be called before
  2764. * btrfsic_mount(), this might return NULL */
  2765. dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
  2766. /* Only called to write the superblock (incl. FLUSH/FUA) */
  2767. if (NULL != dev_state &&
  2768. (rw & WRITE) && bh->b_size > 0) {
  2769. u64 dev_bytenr;
  2770. dev_bytenr = 4096 * bh->b_blocknr;
  2771. if (dev_state->state->print_mask &
  2772. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2773. printk(KERN_INFO
  2774. "submit_bh(rw=0x%x, blocknr=%lu (bytenr %llu),"
  2775. " size=%lu, data=%p, bdev=%p)\n",
  2776. rw, (unsigned long)bh->b_blocknr,
  2777. (unsigned long long)dev_bytenr,
  2778. (unsigned long)bh->b_size, bh->b_data,
  2779. bh->b_bdev);
  2780. btrfsic_process_written_block(dev_state, dev_bytenr,
  2781. &bh->b_data, 1, NULL,
  2782. NULL, bh, rw);
  2783. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2784. if (dev_state->state->print_mask &
  2785. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2786. printk(KERN_INFO
  2787. "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
  2788. rw, bh->b_bdev);
  2789. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2790. if ((dev_state->state->print_mask &
  2791. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2792. BTRFSIC_PRINT_MASK_VERBOSE)))
  2793. printk(KERN_INFO
  2794. "btrfsic_submit_bh(%s) with FLUSH"
  2795. " but dummy block already in use"
  2796. " (ignored)!\n",
  2797. dev_state->name);
  2798. } else {
  2799. struct btrfsic_block *const block =
  2800. &dev_state->dummy_block_for_bio_bh_flush;
  2801. block->is_iodone = 0;
  2802. block->never_written = 0;
  2803. block->iodone_w_error = 0;
  2804. block->flush_gen = dev_state->last_flush_gen + 1;
  2805. block->submit_bio_bh_rw = rw;
  2806. block->orig_bio_bh_private = bh->b_private;
  2807. block->orig_bio_bh_end_io.bh = bh->b_end_io;
  2808. block->next_in_same_bio = NULL;
  2809. bh->b_private = block;
  2810. bh->b_end_io = btrfsic_bh_end_io;
  2811. }
  2812. }
  2813. mutex_unlock(&btrfsic_mutex);
  2814. return submit_bh(rw, bh);
  2815. }
  2816. void btrfsic_submit_bio(int rw, struct bio *bio)
  2817. {
  2818. struct btrfsic_dev_state *dev_state;
  2819. if (!btrfsic_is_initialized) {
  2820. submit_bio(rw, bio);
  2821. return;
  2822. }
  2823. mutex_lock(&btrfsic_mutex);
  2824. /* since btrfsic_submit_bio() is also called before
  2825. * btrfsic_mount(), this might return NULL */
  2826. dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
  2827. if (NULL != dev_state &&
  2828. (rw & WRITE) && NULL != bio->bi_io_vec) {
  2829. unsigned int i;
  2830. u64 dev_bytenr;
  2831. int bio_is_patched;
  2832. char **mapped_datav;
  2833. dev_bytenr = 512 * bio->bi_sector;
  2834. bio_is_patched = 0;
  2835. if (dev_state->state->print_mask &
  2836. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2837. printk(KERN_INFO
  2838. "submit_bio(rw=0x%x, bi_vcnt=%u,"
  2839. " bi_sector=%lu (bytenr %llu), bi_bdev=%p)\n",
  2840. rw, bio->bi_vcnt, (unsigned long)bio->bi_sector,
  2841. (unsigned long long)dev_bytenr,
  2842. bio->bi_bdev);
  2843. mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
  2844. GFP_NOFS);
  2845. if (!mapped_datav)
  2846. goto leave;
  2847. for (i = 0; i < bio->bi_vcnt; i++) {
  2848. BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
  2849. mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
  2850. if (!mapped_datav[i]) {
  2851. while (i > 0) {
  2852. i--;
  2853. kunmap(bio->bi_io_vec[i].bv_page);
  2854. }
  2855. kfree(mapped_datav);
  2856. goto leave;
  2857. }
  2858. if ((BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2859. BTRFSIC_PRINT_MASK_VERBOSE) ==
  2860. (dev_state->state->print_mask &
  2861. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2862. BTRFSIC_PRINT_MASK_VERBOSE)))
  2863. printk(KERN_INFO
  2864. "#%u: page=%p, len=%u, offset=%u\n",
  2865. i, bio->bi_io_vec[i].bv_page,
  2866. bio->bi_io_vec[i].bv_len,
  2867. bio->bi_io_vec[i].bv_offset);
  2868. }
  2869. btrfsic_process_written_block(dev_state, dev_bytenr,
  2870. mapped_datav, bio->bi_vcnt,
  2871. bio, &bio_is_patched,
  2872. NULL, rw);
  2873. while (i > 0) {
  2874. i--;
  2875. kunmap(bio->bi_io_vec[i].bv_page);
  2876. }
  2877. kfree(mapped_datav);
  2878. } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
  2879. if (dev_state->state->print_mask &
  2880. BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
  2881. printk(KERN_INFO
  2882. "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
  2883. rw, bio->bi_bdev);
  2884. if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
  2885. if ((dev_state->state->print_mask &
  2886. (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
  2887. BTRFSIC_PRINT_MASK_VERBOSE)))
  2888. printk(KERN_INFO
  2889. "btrfsic_submit_bio(%s) with FLUSH"
  2890. " but dummy block already in use"
  2891. " (ignored)!\n",
  2892. dev_state->name);
  2893. } else {
  2894. struct btrfsic_block *const block =
  2895. &dev_state->dummy_block_for_bio_bh_flush;
  2896. block->is_iodone = 0;
  2897. block->never_written = 0;
  2898. block->iodone_w_error = 0;
  2899. block->flush_gen = dev_state->last_flush_gen + 1;
  2900. block->submit_bio_bh_rw = rw;
  2901. block->orig_bio_bh_private = bio->bi_private;
  2902. block->orig_bio_bh_end_io.bio = bio->bi_end_io;
  2903. block->next_in_same_bio = NULL;
  2904. bio->bi_private = block;
  2905. bio->bi_end_io = btrfsic_bio_end_io;
  2906. }
  2907. }
  2908. leave:
  2909. mutex_unlock(&btrfsic_mutex);
  2910. submit_bio(rw, bio);
  2911. }
  2912. int btrfsic_mount(struct btrfs_root *root,
  2913. struct btrfs_fs_devices *fs_devices,
  2914. int including_extent_data, u32 print_mask)
  2915. {
  2916. int ret;
  2917. struct btrfsic_state *state;
  2918. struct list_head *dev_head = &fs_devices->devices;
  2919. struct btrfs_device *device;
  2920. if (root->nodesize != root->leafsize) {
  2921. printk(KERN_INFO
  2922. "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
  2923. root->nodesize, root->leafsize);
  2924. return -1;
  2925. }
  2926. if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2927. printk(KERN_INFO
  2928. "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2929. root->nodesize, (unsigned long)PAGE_CACHE_SIZE);
  2930. return -1;
  2931. }
  2932. if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2933. printk(KERN_INFO
  2934. "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2935. root->leafsize, (unsigned long)PAGE_CACHE_SIZE);
  2936. return -1;
  2937. }
  2938. if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
  2939. printk(KERN_INFO
  2940. "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
  2941. root->sectorsize, (unsigned long)PAGE_CACHE_SIZE);
  2942. return -1;
  2943. }
  2944. state = kzalloc(sizeof(*state), GFP_NOFS);
  2945. if (NULL == state) {
  2946. printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
  2947. return -1;
  2948. }
  2949. if (!btrfsic_is_initialized) {
  2950. mutex_init(&btrfsic_mutex);
  2951. btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
  2952. btrfsic_is_initialized = 1;
  2953. }
  2954. mutex_lock(&btrfsic_mutex);
  2955. state->root = root;
  2956. state->print_mask = print_mask;
  2957. state->include_extent_data = including_extent_data;
  2958. state->csum_size = 0;
  2959. state->metablock_size = root->nodesize;
  2960. state->datablock_size = root->sectorsize;
  2961. INIT_LIST_HEAD(&state->all_blocks_list);
  2962. btrfsic_block_hashtable_init(&state->block_hashtable);
  2963. btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
  2964. state->max_superblock_generation = 0;
  2965. state->latest_superblock = NULL;
  2966. list_for_each_entry(device, dev_head, dev_list) {
  2967. struct btrfsic_dev_state *ds;
  2968. char *p;
  2969. if (!device->bdev || !device->name)
  2970. continue;
  2971. ds = btrfsic_dev_state_alloc();
  2972. if (NULL == ds) {
  2973. printk(KERN_INFO
  2974. "btrfs check-integrity: kmalloc() failed!\n");
  2975. mutex_unlock(&btrfsic_mutex);
  2976. return -1;
  2977. }
  2978. ds->bdev = device->bdev;
  2979. ds->state = state;
  2980. bdevname(ds->bdev, ds->name);
  2981. ds->name[BDEVNAME_SIZE - 1] = '\0';
  2982. for (p = ds->name; *p != '\0'; p++);
  2983. while (p > ds->name && *p != '/')
  2984. p--;
  2985. if (*p == '/')
  2986. p++;
  2987. strlcpy(ds->name, p, sizeof(ds->name));
  2988. btrfsic_dev_state_hashtable_add(ds,
  2989. &btrfsic_dev_state_hashtable);
  2990. }
  2991. ret = btrfsic_process_superblock(state, fs_devices);
  2992. if (0 != ret) {
  2993. mutex_unlock(&btrfsic_mutex);
  2994. btrfsic_unmount(root, fs_devices);
  2995. return ret;
  2996. }
  2997. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
  2998. btrfsic_dump_database(state);
  2999. if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
  3000. btrfsic_dump_tree(state);
  3001. mutex_unlock(&btrfsic_mutex);
  3002. return 0;
  3003. }
  3004. void btrfsic_unmount(struct btrfs_root *root,
  3005. struct btrfs_fs_devices *fs_devices)
  3006. {
  3007. struct list_head *elem_all;
  3008. struct list_head *tmp_all;
  3009. struct btrfsic_state *state;
  3010. struct list_head *dev_head = &fs_devices->devices;
  3011. struct btrfs_device *device;
  3012. if (!btrfsic_is_initialized)
  3013. return;
  3014. mutex_lock(&btrfsic_mutex);
  3015. state = NULL;
  3016. list_for_each_entry(device, dev_head, dev_list) {
  3017. struct btrfsic_dev_state *ds;
  3018. if (!device->bdev || !device->name)
  3019. continue;
  3020. ds = btrfsic_dev_state_hashtable_lookup(
  3021. device->bdev,
  3022. &btrfsic_dev_state_hashtable);
  3023. if (NULL != ds) {
  3024. state = ds->state;
  3025. btrfsic_dev_state_hashtable_remove(ds);
  3026. btrfsic_dev_state_free(ds);
  3027. }
  3028. }
  3029. if (NULL == state) {
  3030. printk(KERN_INFO
  3031. "btrfsic: error, cannot find state information"
  3032. " on umount!\n");
  3033. mutex_unlock(&btrfsic_mutex);
  3034. return;
  3035. }
  3036. /*
  3037. * Don't care about keeping the lists' state up to date,
  3038. * just free all memory that was allocated dynamically.
  3039. * Free the blocks and the block_links.
  3040. */
  3041. list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
  3042. struct btrfsic_block *const b_all =
  3043. list_entry(elem_all, struct btrfsic_block,
  3044. all_blocks_node);
  3045. struct list_head *elem_ref_to;
  3046. struct list_head *tmp_ref_to;
  3047. list_for_each_safe(elem_ref_to, tmp_ref_to,
  3048. &b_all->ref_to_list) {
  3049. struct btrfsic_block_link *const l =
  3050. list_entry(elem_ref_to,
  3051. struct btrfsic_block_link,
  3052. node_ref_to);
  3053. if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
  3054. btrfsic_print_rem_link(state, l);
  3055. l->ref_cnt--;
  3056. if (0 == l->ref_cnt)
  3057. btrfsic_block_link_free(l);
  3058. }
  3059. if (b_all->is_iodone || b_all->never_written)
  3060. btrfsic_block_free(b_all);
  3061. else
  3062. printk(KERN_INFO "btrfs: attempt to free %c-block"
  3063. " @%llu (%s/%llu/%d) on umount which is"
  3064. " not yet iodone!\n",
  3065. btrfsic_get_block_type(state, b_all),
  3066. (unsigned long long)b_all->logical_bytenr,
  3067. b_all->dev_state->name,
  3068. (unsigned long long)b_all->dev_bytenr,
  3069. b_all->mirror_num);
  3070. }
  3071. mutex_unlock(&btrfsic_mutex);
  3072. kfree(state);
  3073. }