check-integrity.c 101 KB

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