check-integrity.c 100 KB

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