dm-zoned-metadata.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  1. /*
  2. * Copyright (C) 2017 Western Digital Corporation or its affiliates.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-zoned.h"
  7. #include <linux/module.h>
  8. #include <linux/crc32.h>
  9. #define DM_MSG_PREFIX "zoned metadata"
  10. /*
  11. * Metadata version.
  12. */
  13. #define DMZ_META_VER 1
  14. /*
  15. * On-disk super block magic.
  16. */
  17. #define DMZ_MAGIC ((((unsigned int)('D')) << 24) | \
  18. (((unsigned int)('Z')) << 16) | \
  19. (((unsigned int)('B')) << 8) | \
  20. ((unsigned int)('D')))
  21. /*
  22. * On disk super block.
  23. * This uses only 512 B but uses on disk a full 4KB block. This block is
  24. * followed on disk by the mapping table of chunks to zones and the bitmap
  25. * blocks indicating zone block validity.
  26. * The overall resulting metadata format is:
  27. * (1) Super block (1 block)
  28. * (2) Chunk mapping table (nr_map_blocks)
  29. * (3) Bitmap blocks (nr_bitmap_blocks)
  30. * All metadata blocks are stored in conventional zones, starting from the
  31. * the first conventional zone found on disk.
  32. */
  33. struct dmz_super {
  34. /* Magic number */
  35. __le32 magic; /* 4 */
  36. /* Metadata version number */
  37. __le32 version; /* 8 */
  38. /* Generation number */
  39. __le64 gen; /* 16 */
  40. /* This block number */
  41. __le64 sb_block; /* 24 */
  42. /* The number of metadata blocks, including this super block */
  43. __le32 nr_meta_blocks; /* 28 */
  44. /* The number of sequential zones reserved for reclaim */
  45. __le32 nr_reserved_seq; /* 32 */
  46. /* The number of entries in the mapping table */
  47. __le32 nr_chunks; /* 36 */
  48. /* The number of blocks used for the chunk mapping table */
  49. __le32 nr_map_blocks; /* 40 */
  50. /* The number of blocks used for the block bitmaps */
  51. __le32 nr_bitmap_blocks; /* 44 */
  52. /* Checksum */
  53. __le32 crc; /* 48 */
  54. /* Padding to full 512B sector */
  55. u8 reserved[464]; /* 512 */
  56. };
  57. /*
  58. * Chunk mapping entry: entries are indexed by chunk number
  59. * and give the zone ID (dzone_id) mapping the chunk on disk.
  60. * This zone may be sequential or random. If it is a sequential
  61. * zone, a second zone (bzone_id) used as a write buffer may
  62. * also be specified. This second zone will always be a randomly
  63. * writeable zone.
  64. */
  65. struct dmz_map {
  66. __le32 dzone_id;
  67. __le32 bzone_id;
  68. };
  69. /*
  70. * Chunk mapping table metadata: 512 8-bytes entries per 4KB block.
  71. */
  72. #define DMZ_MAP_ENTRIES (DMZ_BLOCK_SIZE / sizeof(struct dmz_map))
  73. #define DMZ_MAP_ENTRIES_SHIFT (ilog2(DMZ_MAP_ENTRIES))
  74. #define DMZ_MAP_ENTRIES_MASK (DMZ_MAP_ENTRIES - 1)
  75. #define DMZ_MAP_UNMAPPED UINT_MAX
  76. /*
  77. * Meta data block descriptor (for cached metadata blocks).
  78. */
  79. struct dmz_mblock {
  80. struct rb_node node;
  81. struct list_head link;
  82. sector_t no;
  83. unsigned int ref;
  84. unsigned long state;
  85. struct page *page;
  86. void *data;
  87. };
  88. /*
  89. * Metadata block state flags.
  90. */
  91. enum {
  92. DMZ_META_DIRTY,
  93. DMZ_META_READING,
  94. DMZ_META_WRITING,
  95. DMZ_META_ERROR,
  96. };
  97. /*
  98. * Super block information (one per metadata set).
  99. */
  100. struct dmz_sb {
  101. sector_t block;
  102. struct dmz_mblock *mblk;
  103. struct dmz_super *sb;
  104. };
  105. /*
  106. * In-memory metadata.
  107. */
  108. struct dmz_metadata {
  109. struct dmz_dev *dev;
  110. sector_t zone_bitmap_size;
  111. unsigned int zone_nr_bitmap_blocks;
  112. unsigned int zone_bits_per_mblk;
  113. unsigned int nr_bitmap_blocks;
  114. unsigned int nr_map_blocks;
  115. unsigned int nr_useable_zones;
  116. unsigned int nr_meta_blocks;
  117. unsigned int nr_meta_zones;
  118. unsigned int nr_data_zones;
  119. unsigned int nr_rnd_zones;
  120. unsigned int nr_reserved_seq;
  121. unsigned int nr_chunks;
  122. /* Zone information array */
  123. struct dm_zone *zones;
  124. struct dm_zone *sb_zone;
  125. struct dmz_sb sb[2];
  126. unsigned int mblk_primary;
  127. u64 sb_gen;
  128. unsigned int min_nr_mblks;
  129. unsigned int max_nr_mblks;
  130. atomic_t nr_mblks;
  131. struct rw_semaphore mblk_sem;
  132. struct mutex mblk_flush_lock;
  133. spinlock_t mblk_lock;
  134. struct rb_root mblk_rbtree;
  135. struct list_head mblk_lru_list;
  136. struct list_head mblk_dirty_list;
  137. struct shrinker mblk_shrinker;
  138. /* Zone allocation management */
  139. struct mutex map_lock;
  140. struct dmz_mblock **map_mblk;
  141. unsigned int nr_rnd;
  142. atomic_t unmap_nr_rnd;
  143. struct list_head unmap_rnd_list;
  144. struct list_head map_rnd_list;
  145. unsigned int nr_seq;
  146. atomic_t unmap_nr_seq;
  147. struct list_head unmap_seq_list;
  148. struct list_head map_seq_list;
  149. atomic_t nr_reserved_seq_zones;
  150. struct list_head reserved_seq_zones_list;
  151. wait_queue_head_t free_wq;
  152. };
  153. /*
  154. * Various accessors
  155. */
  156. unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone)
  157. {
  158. return ((unsigned int)(zone - zmd->zones));
  159. }
  160. sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone)
  161. {
  162. return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_sectors_shift;
  163. }
  164. sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone)
  165. {
  166. return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_blocks_shift;
  167. }
  168. unsigned int dmz_nr_chunks(struct dmz_metadata *zmd)
  169. {
  170. return zmd->nr_chunks;
  171. }
  172. unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd)
  173. {
  174. return zmd->nr_rnd;
  175. }
  176. unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd)
  177. {
  178. return atomic_read(&zmd->unmap_nr_rnd);
  179. }
  180. /*
  181. * Lock/unlock mapping table.
  182. * The map lock also protects all the zone lists.
  183. */
  184. void dmz_lock_map(struct dmz_metadata *zmd)
  185. {
  186. mutex_lock(&zmd->map_lock);
  187. }
  188. void dmz_unlock_map(struct dmz_metadata *zmd)
  189. {
  190. mutex_unlock(&zmd->map_lock);
  191. }
  192. /*
  193. * Lock/unlock metadata access. This is a "read" lock on a semaphore
  194. * that prevents metadata flush from running while metadata are being
  195. * modified. The actual metadata write mutual exclusion is achieved with
  196. * the map lock and zone styate management (active and reclaim state are
  197. * mutually exclusive).
  198. */
  199. void dmz_lock_metadata(struct dmz_metadata *zmd)
  200. {
  201. down_read(&zmd->mblk_sem);
  202. }
  203. void dmz_unlock_metadata(struct dmz_metadata *zmd)
  204. {
  205. up_read(&zmd->mblk_sem);
  206. }
  207. /*
  208. * Lock/unlock flush: prevent concurrent executions
  209. * of dmz_flush_metadata as well as metadata modification in reclaim
  210. * while flush is being executed.
  211. */
  212. void dmz_lock_flush(struct dmz_metadata *zmd)
  213. {
  214. mutex_lock(&zmd->mblk_flush_lock);
  215. }
  216. void dmz_unlock_flush(struct dmz_metadata *zmd)
  217. {
  218. mutex_unlock(&zmd->mblk_flush_lock);
  219. }
  220. /*
  221. * Allocate a metadata block.
  222. */
  223. static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd,
  224. sector_t mblk_no)
  225. {
  226. struct dmz_mblock *mblk = NULL;
  227. /* See if we can reuse cached blocks */
  228. if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) {
  229. spin_lock(&zmd->mblk_lock);
  230. mblk = list_first_entry_or_null(&zmd->mblk_lru_list,
  231. struct dmz_mblock, link);
  232. if (mblk) {
  233. list_del_init(&mblk->link);
  234. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  235. mblk->no = mblk_no;
  236. }
  237. spin_unlock(&zmd->mblk_lock);
  238. if (mblk)
  239. return mblk;
  240. }
  241. /* Allocate a new block */
  242. mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO);
  243. if (!mblk)
  244. return NULL;
  245. mblk->page = alloc_page(GFP_NOIO);
  246. if (!mblk->page) {
  247. kfree(mblk);
  248. return NULL;
  249. }
  250. RB_CLEAR_NODE(&mblk->node);
  251. INIT_LIST_HEAD(&mblk->link);
  252. mblk->ref = 0;
  253. mblk->state = 0;
  254. mblk->no = mblk_no;
  255. mblk->data = page_address(mblk->page);
  256. atomic_inc(&zmd->nr_mblks);
  257. return mblk;
  258. }
  259. /*
  260. * Free a metadata block.
  261. */
  262. static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
  263. {
  264. __free_pages(mblk->page, 0);
  265. kfree(mblk);
  266. atomic_dec(&zmd->nr_mblks);
  267. }
  268. /*
  269. * Insert a metadata block in the rbtree.
  270. */
  271. static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
  272. {
  273. struct rb_root *root = &zmd->mblk_rbtree;
  274. struct rb_node **new = &(root->rb_node), *parent = NULL;
  275. struct dmz_mblock *b;
  276. /* Figure out where to put the new node */
  277. while (*new) {
  278. b = container_of(*new, struct dmz_mblock, node);
  279. parent = *new;
  280. new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right);
  281. }
  282. /* Add new node and rebalance tree */
  283. rb_link_node(&mblk->node, parent, new);
  284. rb_insert_color(&mblk->node, root);
  285. }
  286. /*
  287. * Lookup a metadata block in the rbtree. If the block is found, increment
  288. * its reference count.
  289. */
  290. static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd,
  291. sector_t mblk_no)
  292. {
  293. struct rb_root *root = &zmd->mblk_rbtree;
  294. struct rb_node *node = root->rb_node;
  295. struct dmz_mblock *mblk;
  296. while (node) {
  297. mblk = container_of(node, struct dmz_mblock, node);
  298. if (mblk->no == mblk_no) {
  299. /*
  300. * If this is the first reference to the block,
  301. * remove it from the LRU list.
  302. */
  303. mblk->ref++;
  304. if (mblk->ref == 1 &&
  305. !test_bit(DMZ_META_DIRTY, &mblk->state))
  306. list_del_init(&mblk->link);
  307. return mblk;
  308. }
  309. node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right;
  310. }
  311. return NULL;
  312. }
  313. /*
  314. * Metadata block BIO end callback.
  315. */
  316. static void dmz_mblock_bio_end_io(struct bio *bio)
  317. {
  318. struct dmz_mblock *mblk = bio->bi_private;
  319. int flag;
  320. if (bio->bi_status)
  321. set_bit(DMZ_META_ERROR, &mblk->state);
  322. if (bio_op(bio) == REQ_OP_WRITE)
  323. flag = DMZ_META_WRITING;
  324. else
  325. flag = DMZ_META_READING;
  326. clear_bit_unlock(flag, &mblk->state);
  327. smp_mb__after_atomic();
  328. wake_up_bit(&mblk->state, flag);
  329. bio_put(bio);
  330. }
  331. /*
  332. * Read an uncached metadata block from disk and add it to the cache.
  333. */
  334. static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd,
  335. sector_t mblk_no)
  336. {
  337. struct dmz_mblock *mblk, *m;
  338. sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no;
  339. struct bio *bio;
  340. if (dmz_bdev_is_dying(zmd->dev))
  341. return ERR_PTR(-EIO);
  342. /* Get a new block and a BIO to read it */
  343. mblk = dmz_alloc_mblock(zmd, mblk_no);
  344. if (!mblk)
  345. return ERR_PTR(-ENOMEM);
  346. bio = bio_alloc(GFP_NOIO, 1);
  347. if (!bio) {
  348. dmz_free_mblock(zmd, mblk);
  349. return ERR_PTR(-ENOMEM);
  350. }
  351. spin_lock(&zmd->mblk_lock);
  352. /*
  353. * Make sure that another context did not start reading
  354. * the block already.
  355. */
  356. m = dmz_get_mblock_fast(zmd, mblk_no);
  357. if (m) {
  358. spin_unlock(&zmd->mblk_lock);
  359. dmz_free_mblock(zmd, mblk);
  360. bio_put(bio);
  361. return m;
  362. }
  363. mblk->ref++;
  364. set_bit(DMZ_META_READING, &mblk->state);
  365. dmz_insert_mblock(zmd, mblk);
  366. spin_unlock(&zmd->mblk_lock);
  367. /* Submit read BIO */
  368. bio->bi_iter.bi_sector = dmz_blk2sect(block);
  369. bio_set_dev(bio, zmd->dev->bdev);
  370. bio->bi_private = mblk;
  371. bio->bi_end_io = dmz_mblock_bio_end_io;
  372. bio_set_op_attrs(bio, REQ_OP_READ, REQ_META | REQ_PRIO);
  373. bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
  374. submit_bio(bio);
  375. return mblk;
  376. }
  377. /*
  378. * Free metadata blocks.
  379. */
  380. static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd,
  381. unsigned long limit)
  382. {
  383. struct dmz_mblock *mblk;
  384. unsigned long count = 0;
  385. if (!zmd->max_nr_mblks)
  386. return 0;
  387. while (!list_empty(&zmd->mblk_lru_list) &&
  388. atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks &&
  389. count < limit) {
  390. mblk = list_first_entry(&zmd->mblk_lru_list,
  391. struct dmz_mblock, link);
  392. list_del_init(&mblk->link);
  393. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  394. dmz_free_mblock(zmd, mblk);
  395. count++;
  396. }
  397. return count;
  398. }
  399. /*
  400. * For mblock shrinker: get the number of unused metadata blocks in the cache.
  401. */
  402. static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink,
  403. struct shrink_control *sc)
  404. {
  405. struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
  406. return atomic_read(&zmd->nr_mblks);
  407. }
  408. /*
  409. * For mblock shrinker: scan unused metadata blocks and shrink the cache.
  410. */
  411. static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink,
  412. struct shrink_control *sc)
  413. {
  414. struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
  415. unsigned long count;
  416. spin_lock(&zmd->mblk_lock);
  417. count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan);
  418. spin_unlock(&zmd->mblk_lock);
  419. return count ? count : SHRINK_STOP;
  420. }
  421. /*
  422. * Release a metadata block.
  423. */
  424. static void dmz_release_mblock(struct dmz_metadata *zmd,
  425. struct dmz_mblock *mblk)
  426. {
  427. if (!mblk)
  428. return;
  429. spin_lock(&zmd->mblk_lock);
  430. mblk->ref--;
  431. if (mblk->ref == 0) {
  432. if (test_bit(DMZ_META_ERROR, &mblk->state)) {
  433. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  434. dmz_free_mblock(zmd, mblk);
  435. } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) {
  436. list_add_tail(&mblk->link, &zmd->mblk_lru_list);
  437. dmz_shrink_mblock_cache(zmd, 1);
  438. }
  439. }
  440. spin_unlock(&zmd->mblk_lock);
  441. }
  442. /*
  443. * Get a metadata block from the rbtree. If the block
  444. * is not present, read it from disk.
  445. */
  446. static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd,
  447. sector_t mblk_no)
  448. {
  449. struct dmz_mblock *mblk;
  450. /* Check rbtree */
  451. spin_lock(&zmd->mblk_lock);
  452. mblk = dmz_get_mblock_fast(zmd, mblk_no);
  453. spin_unlock(&zmd->mblk_lock);
  454. if (!mblk) {
  455. /* Cache miss: read the block from disk */
  456. mblk = dmz_get_mblock_slow(zmd, mblk_no);
  457. if (IS_ERR(mblk))
  458. return mblk;
  459. }
  460. /* Wait for on-going read I/O and check for error */
  461. wait_on_bit_io(&mblk->state, DMZ_META_READING,
  462. TASK_UNINTERRUPTIBLE);
  463. if (test_bit(DMZ_META_ERROR, &mblk->state)) {
  464. dmz_release_mblock(zmd, mblk);
  465. dmz_check_bdev(zmd->dev);
  466. return ERR_PTR(-EIO);
  467. }
  468. return mblk;
  469. }
  470. /*
  471. * Mark a metadata block dirty.
  472. */
  473. static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
  474. {
  475. spin_lock(&zmd->mblk_lock);
  476. if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state))
  477. list_add_tail(&mblk->link, &zmd->mblk_dirty_list);
  478. spin_unlock(&zmd->mblk_lock);
  479. }
  480. /*
  481. * Issue a metadata block write BIO.
  482. */
  483. static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk,
  484. unsigned int set)
  485. {
  486. sector_t block = zmd->sb[set].block + mblk->no;
  487. struct bio *bio;
  488. if (dmz_bdev_is_dying(zmd->dev))
  489. return -EIO;
  490. bio = bio_alloc(GFP_NOIO, 1);
  491. if (!bio) {
  492. set_bit(DMZ_META_ERROR, &mblk->state);
  493. return -ENOMEM;
  494. }
  495. set_bit(DMZ_META_WRITING, &mblk->state);
  496. bio->bi_iter.bi_sector = dmz_blk2sect(block);
  497. bio_set_dev(bio, zmd->dev->bdev);
  498. bio->bi_private = mblk;
  499. bio->bi_end_io = dmz_mblock_bio_end_io;
  500. bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_META | REQ_PRIO);
  501. bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
  502. submit_bio(bio);
  503. return 0;
  504. }
  505. /*
  506. * Read/write a metadata block.
  507. */
  508. static int dmz_rdwr_block(struct dmz_metadata *zmd, int op, sector_t block,
  509. struct page *page)
  510. {
  511. struct bio *bio;
  512. int ret;
  513. if (dmz_bdev_is_dying(zmd->dev))
  514. return -EIO;
  515. bio = bio_alloc(GFP_NOIO, 1);
  516. if (!bio)
  517. return -ENOMEM;
  518. bio->bi_iter.bi_sector = dmz_blk2sect(block);
  519. bio_set_dev(bio, zmd->dev->bdev);
  520. bio_set_op_attrs(bio, op, REQ_SYNC | REQ_META | REQ_PRIO);
  521. bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0);
  522. ret = submit_bio_wait(bio);
  523. bio_put(bio);
  524. if (ret)
  525. dmz_check_bdev(zmd->dev);
  526. return ret;
  527. }
  528. /*
  529. * Write super block of the specified metadata set.
  530. */
  531. static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set)
  532. {
  533. sector_t block = zmd->sb[set].block;
  534. struct dmz_mblock *mblk = zmd->sb[set].mblk;
  535. struct dmz_super *sb = zmd->sb[set].sb;
  536. u64 sb_gen = zmd->sb_gen + 1;
  537. int ret;
  538. sb->magic = cpu_to_le32(DMZ_MAGIC);
  539. sb->version = cpu_to_le32(DMZ_META_VER);
  540. sb->gen = cpu_to_le64(sb_gen);
  541. sb->sb_block = cpu_to_le64(block);
  542. sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks);
  543. sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq);
  544. sb->nr_chunks = cpu_to_le32(zmd->nr_chunks);
  545. sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks);
  546. sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks);
  547. sb->crc = 0;
  548. sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE));
  549. ret = dmz_rdwr_block(zmd, REQ_OP_WRITE, block, mblk->page);
  550. if (ret == 0)
  551. ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
  552. return ret;
  553. }
  554. /*
  555. * Write dirty metadata blocks to the specified set.
  556. */
  557. static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd,
  558. struct list_head *write_list,
  559. unsigned int set)
  560. {
  561. struct dmz_mblock *mblk;
  562. struct blk_plug plug;
  563. int ret = 0, nr_mblks_submitted = 0;
  564. /* Issue writes */
  565. blk_start_plug(&plug);
  566. list_for_each_entry(mblk, write_list, link) {
  567. ret = dmz_write_mblock(zmd, mblk, set);
  568. if (ret)
  569. break;
  570. nr_mblks_submitted++;
  571. }
  572. blk_finish_plug(&plug);
  573. /* Wait for completion */
  574. list_for_each_entry(mblk, write_list, link) {
  575. if (!nr_mblks_submitted)
  576. break;
  577. wait_on_bit_io(&mblk->state, DMZ_META_WRITING,
  578. TASK_UNINTERRUPTIBLE);
  579. if (test_bit(DMZ_META_ERROR, &mblk->state)) {
  580. clear_bit(DMZ_META_ERROR, &mblk->state);
  581. dmz_check_bdev(zmd->dev);
  582. ret = -EIO;
  583. }
  584. nr_mblks_submitted--;
  585. }
  586. /* Flush drive cache (this will also sync data) */
  587. if (ret == 0)
  588. ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
  589. return ret;
  590. }
  591. /*
  592. * Log dirty metadata blocks.
  593. */
  594. static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd,
  595. struct list_head *write_list)
  596. {
  597. unsigned int log_set = zmd->mblk_primary ^ 0x1;
  598. int ret;
  599. /* Write dirty blocks to the log */
  600. ret = dmz_write_dirty_mblocks(zmd, write_list, log_set);
  601. if (ret)
  602. return ret;
  603. /*
  604. * No error so far: now validate the log by updating the
  605. * log index super block generation.
  606. */
  607. ret = dmz_write_sb(zmd, log_set);
  608. if (ret)
  609. return ret;
  610. return 0;
  611. }
  612. /*
  613. * Flush dirty metadata blocks.
  614. */
  615. int dmz_flush_metadata(struct dmz_metadata *zmd)
  616. {
  617. struct dmz_mblock *mblk;
  618. struct list_head write_list;
  619. int ret;
  620. if (WARN_ON(!zmd))
  621. return 0;
  622. INIT_LIST_HEAD(&write_list);
  623. /*
  624. * Make sure that metadata blocks are stable before logging: take
  625. * the write lock on the metadata semaphore to prevent target BIOs
  626. * from modifying metadata.
  627. */
  628. down_write(&zmd->mblk_sem);
  629. /*
  630. * This is called from the target flush work and reclaim work.
  631. * Concurrent execution is not allowed.
  632. */
  633. dmz_lock_flush(zmd);
  634. if (dmz_bdev_is_dying(zmd->dev)) {
  635. ret = -EIO;
  636. goto out;
  637. }
  638. /* Get dirty blocks */
  639. spin_lock(&zmd->mblk_lock);
  640. list_splice_init(&zmd->mblk_dirty_list, &write_list);
  641. spin_unlock(&zmd->mblk_lock);
  642. /* If there are no dirty metadata blocks, just flush the device cache */
  643. if (list_empty(&write_list)) {
  644. ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
  645. goto err;
  646. }
  647. /*
  648. * The primary metadata set is still clean. Keep it this way until
  649. * all updates are successful in the secondary set. That is, use
  650. * the secondary set as a log.
  651. */
  652. ret = dmz_log_dirty_mblocks(zmd, &write_list);
  653. if (ret)
  654. goto err;
  655. /*
  656. * The log is on disk. It is now safe to update in place
  657. * in the primary metadata set.
  658. */
  659. ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary);
  660. if (ret)
  661. goto err;
  662. ret = dmz_write_sb(zmd, zmd->mblk_primary);
  663. if (ret)
  664. goto err;
  665. while (!list_empty(&write_list)) {
  666. mblk = list_first_entry(&write_list, struct dmz_mblock, link);
  667. list_del_init(&mblk->link);
  668. spin_lock(&zmd->mblk_lock);
  669. clear_bit(DMZ_META_DIRTY, &mblk->state);
  670. if (mblk->ref == 0)
  671. list_add_tail(&mblk->link, &zmd->mblk_lru_list);
  672. spin_unlock(&zmd->mblk_lock);
  673. }
  674. zmd->sb_gen++;
  675. out:
  676. dmz_unlock_flush(zmd);
  677. up_write(&zmd->mblk_sem);
  678. return ret;
  679. err:
  680. if (!list_empty(&write_list)) {
  681. spin_lock(&zmd->mblk_lock);
  682. list_splice(&write_list, &zmd->mblk_dirty_list);
  683. spin_unlock(&zmd->mblk_lock);
  684. }
  685. if (!dmz_check_bdev(zmd->dev))
  686. ret = -EIO;
  687. goto out;
  688. }
  689. /*
  690. * Check super block.
  691. */
  692. static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_super *sb)
  693. {
  694. unsigned int nr_meta_zones, nr_data_zones;
  695. struct dmz_dev *dev = zmd->dev;
  696. u32 crc, stored_crc;
  697. u64 gen;
  698. gen = le64_to_cpu(sb->gen);
  699. stored_crc = le32_to_cpu(sb->crc);
  700. sb->crc = 0;
  701. crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE);
  702. if (crc != stored_crc) {
  703. dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)",
  704. crc, stored_crc);
  705. return -ENXIO;
  706. }
  707. if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
  708. dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)",
  709. DMZ_MAGIC, le32_to_cpu(sb->magic));
  710. return -ENXIO;
  711. }
  712. if (le32_to_cpu(sb->version) != DMZ_META_VER) {
  713. dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
  714. DMZ_META_VER, le32_to_cpu(sb->version));
  715. return -ENXIO;
  716. }
  717. nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + dev->zone_nr_blocks - 1)
  718. >> dev->zone_nr_blocks_shift;
  719. if (!nr_meta_zones ||
  720. nr_meta_zones >= zmd->nr_rnd_zones) {
  721. dmz_dev_err(dev, "Invalid number of metadata blocks");
  722. return -ENXIO;
  723. }
  724. if (!le32_to_cpu(sb->nr_reserved_seq) ||
  725. le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
  726. dmz_dev_err(dev, "Invalid number of reserved sequential zones");
  727. return -ENXIO;
  728. }
  729. nr_data_zones = zmd->nr_useable_zones -
  730. (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq));
  731. if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) {
  732. dmz_dev_err(dev, "Invalid number of chunks %u / %u",
  733. le32_to_cpu(sb->nr_chunks), nr_data_zones);
  734. return -ENXIO;
  735. }
  736. /* OK */
  737. zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
  738. zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
  739. zmd->nr_chunks = le32_to_cpu(sb->nr_chunks);
  740. zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks);
  741. zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks);
  742. zmd->nr_meta_zones = nr_meta_zones;
  743. zmd->nr_data_zones = nr_data_zones;
  744. return 0;
  745. }
  746. /*
  747. * Read the first or second super block from disk.
  748. */
  749. static int dmz_read_sb(struct dmz_metadata *zmd, unsigned int set)
  750. {
  751. return dmz_rdwr_block(zmd, REQ_OP_READ, zmd->sb[set].block,
  752. zmd->sb[set].mblk->page);
  753. }
  754. /*
  755. * Determine the position of the secondary super blocks on disk.
  756. * This is used only if a corruption of the primary super block
  757. * is detected.
  758. */
  759. static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
  760. {
  761. unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
  762. struct dmz_mblock *mblk;
  763. int i;
  764. /* Allocate a block */
  765. mblk = dmz_alloc_mblock(zmd, 0);
  766. if (!mblk)
  767. return -ENOMEM;
  768. zmd->sb[1].mblk = mblk;
  769. zmd->sb[1].sb = mblk->data;
  770. /* Bad first super block: search for the second one */
  771. zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
  772. for (i = 0; i < zmd->nr_rnd_zones - 1; i++) {
  773. if (dmz_read_sb(zmd, 1) != 0)
  774. break;
  775. if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
  776. return 0;
  777. zmd->sb[1].block += zone_nr_blocks;
  778. }
  779. dmz_free_mblock(zmd, mblk);
  780. zmd->sb[1].mblk = NULL;
  781. return -EIO;
  782. }
  783. /*
  784. * Read the first or second super block from disk.
  785. */
  786. static int dmz_get_sb(struct dmz_metadata *zmd, unsigned int set)
  787. {
  788. struct dmz_mblock *mblk;
  789. int ret;
  790. /* Allocate a block */
  791. mblk = dmz_alloc_mblock(zmd, 0);
  792. if (!mblk)
  793. return -ENOMEM;
  794. zmd->sb[set].mblk = mblk;
  795. zmd->sb[set].sb = mblk->data;
  796. /* Read super block */
  797. ret = dmz_read_sb(zmd, set);
  798. if (ret) {
  799. dmz_free_mblock(zmd, mblk);
  800. zmd->sb[set].mblk = NULL;
  801. return ret;
  802. }
  803. return 0;
  804. }
  805. /*
  806. * Recover a metadata set.
  807. */
  808. static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set)
  809. {
  810. unsigned int src_set = dst_set ^ 0x1;
  811. struct page *page;
  812. int i, ret;
  813. dmz_dev_warn(zmd->dev, "Metadata set %u invalid: recovering", dst_set);
  814. if (dst_set == 0)
  815. zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
  816. else {
  817. zmd->sb[1].block = zmd->sb[0].block +
  818. (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
  819. }
  820. page = alloc_page(GFP_NOIO);
  821. if (!page)
  822. return -ENOMEM;
  823. /* Copy metadata blocks */
  824. for (i = 1; i < zmd->nr_meta_blocks; i++) {
  825. ret = dmz_rdwr_block(zmd, REQ_OP_READ,
  826. zmd->sb[src_set].block + i, page);
  827. if (ret)
  828. goto out;
  829. ret = dmz_rdwr_block(zmd, REQ_OP_WRITE,
  830. zmd->sb[dst_set].block + i, page);
  831. if (ret)
  832. goto out;
  833. }
  834. /* Finalize with the super block */
  835. if (!zmd->sb[dst_set].mblk) {
  836. zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0);
  837. if (!zmd->sb[dst_set].mblk) {
  838. ret = -ENOMEM;
  839. goto out;
  840. }
  841. zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data;
  842. }
  843. ret = dmz_write_sb(zmd, dst_set);
  844. out:
  845. __free_pages(page, 0);
  846. return ret;
  847. }
  848. /*
  849. * Get super block from disk.
  850. */
  851. static int dmz_load_sb(struct dmz_metadata *zmd)
  852. {
  853. bool sb_good[2] = {false, false};
  854. u64 sb_gen[2] = {0, 0};
  855. int ret;
  856. /* Read and check the primary super block */
  857. zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
  858. ret = dmz_get_sb(zmd, 0);
  859. if (ret) {
  860. dmz_dev_err(zmd->dev, "Read primary super block failed");
  861. return ret;
  862. }
  863. ret = dmz_check_sb(zmd, zmd->sb[0].sb);
  864. /* Read and check secondary super block */
  865. if (ret == 0) {
  866. sb_good[0] = true;
  867. zmd->sb[1].block = zmd->sb[0].block +
  868. (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
  869. ret = dmz_get_sb(zmd, 1);
  870. } else
  871. ret = dmz_lookup_secondary_sb(zmd);
  872. if (ret) {
  873. dmz_dev_err(zmd->dev, "Read secondary super block failed");
  874. return ret;
  875. }
  876. ret = dmz_check_sb(zmd, zmd->sb[1].sb);
  877. if (ret == 0)
  878. sb_good[1] = true;
  879. /* Use highest generation sb first */
  880. if (!sb_good[0] && !sb_good[1]) {
  881. dmz_dev_err(zmd->dev, "No valid super block found");
  882. return -EIO;
  883. }
  884. if (sb_good[0])
  885. sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen);
  886. else
  887. ret = dmz_recover_mblocks(zmd, 0);
  888. if (sb_good[1])
  889. sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen);
  890. else
  891. ret = dmz_recover_mblocks(zmd, 1);
  892. if (ret) {
  893. dmz_dev_err(zmd->dev, "Recovery failed");
  894. return -EIO;
  895. }
  896. if (sb_gen[0] >= sb_gen[1]) {
  897. zmd->sb_gen = sb_gen[0];
  898. zmd->mblk_primary = 0;
  899. } else {
  900. zmd->sb_gen = sb_gen[1];
  901. zmd->mblk_primary = 1;
  902. }
  903. dmz_dev_debug(zmd->dev, "Using super block %u (gen %llu)",
  904. zmd->mblk_primary, zmd->sb_gen);
  905. return 0;
  906. }
  907. /*
  908. * Initialize a zone descriptor.
  909. */
  910. static int dmz_init_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
  911. struct blk_zone *blkz)
  912. {
  913. struct dmz_dev *dev = zmd->dev;
  914. /* Ignore the eventual last runt (smaller) zone */
  915. if (blkz->len != dev->zone_nr_sectors) {
  916. if (blkz->start + blkz->len == dev->capacity)
  917. return 0;
  918. return -ENXIO;
  919. }
  920. INIT_LIST_HEAD(&zone->link);
  921. atomic_set(&zone->refcount, 0);
  922. zone->chunk = DMZ_MAP_UNMAPPED;
  923. if (blkz->type == BLK_ZONE_TYPE_CONVENTIONAL) {
  924. set_bit(DMZ_RND, &zone->flags);
  925. zmd->nr_rnd_zones++;
  926. } else if (blkz->type == BLK_ZONE_TYPE_SEQWRITE_REQ ||
  927. blkz->type == BLK_ZONE_TYPE_SEQWRITE_PREF) {
  928. set_bit(DMZ_SEQ, &zone->flags);
  929. } else
  930. return -ENXIO;
  931. if (blkz->cond == BLK_ZONE_COND_OFFLINE)
  932. set_bit(DMZ_OFFLINE, &zone->flags);
  933. else if (blkz->cond == BLK_ZONE_COND_READONLY)
  934. set_bit(DMZ_READ_ONLY, &zone->flags);
  935. if (dmz_is_rnd(zone))
  936. zone->wp_block = 0;
  937. else
  938. zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
  939. if (!dmz_is_offline(zone) && !dmz_is_readonly(zone)) {
  940. zmd->nr_useable_zones++;
  941. if (dmz_is_rnd(zone)) {
  942. zmd->nr_rnd_zones++;
  943. if (!zmd->sb_zone) {
  944. /* Super block zone */
  945. zmd->sb_zone = zone;
  946. }
  947. }
  948. }
  949. return 0;
  950. }
  951. /*
  952. * Free zones descriptors.
  953. */
  954. static void dmz_drop_zones(struct dmz_metadata *zmd)
  955. {
  956. kfree(zmd->zones);
  957. zmd->zones = NULL;
  958. }
  959. /*
  960. * The size of a zone report in number of zones.
  961. * This results in 4096*64B=256KB report zones commands.
  962. */
  963. #define DMZ_REPORT_NR_ZONES 4096
  964. /*
  965. * Allocate and initialize zone descriptors using the zone
  966. * information from disk.
  967. */
  968. static int dmz_init_zones(struct dmz_metadata *zmd)
  969. {
  970. struct dmz_dev *dev = zmd->dev;
  971. struct dm_zone *zone;
  972. struct blk_zone *blkz;
  973. unsigned int nr_blkz;
  974. sector_t sector = 0;
  975. int i, ret = 0;
  976. /* Init */
  977. zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
  978. zmd->zone_nr_bitmap_blocks =
  979. max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
  980. zmd->zone_bits_per_mblk = min_t(sector_t, dev->zone_nr_blocks,
  981. DMZ_BLOCK_SIZE_BITS);
  982. /* Allocate zone array */
  983. zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
  984. if (!zmd->zones)
  985. return -ENOMEM;
  986. dmz_dev_info(dev, "Using %zu B for zone information",
  987. sizeof(struct dm_zone) * dev->nr_zones);
  988. /* Get zone information */
  989. nr_blkz = DMZ_REPORT_NR_ZONES;
  990. blkz = kcalloc(nr_blkz, sizeof(struct blk_zone), GFP_KERNEL);
  991. if (!blkz) {
  992. ret = -ENOMEM;
  993. goto out;
  994. }
  995. /*
  996. * Get zone information and initialize zone descriptors.
  997. * At the same time, determine where the super block
  998. * should be: first block of the first randomly writable
  999. * zone.
  1000. */
  1001. zone = zmd->zones;
  1002. while (sector < dev->capacity) {
  1003. /* Get zone information */
  1004. nr_blkz = DMZ_REPORT_NR_ZONES;
  1005. ret = blkdev_report_zones(dev->bdev, sector, blkz,
  1006. &nr_blkz, GFP_KERNEL);
  1007. if (ret) {
  1008. dmz_dev_err(dev, "Report zones failed %d", ret);
  1009. goto out;
  1010. }
  1011. if (!nr_blkz)
  1012. break;
  1013. /* Process report */
  1014. for (i = 0; i < nr_blkz; i++) {
  1015. ret = dmz_init_zone(zmd, zone, &blkz[i]);
  1016. if (ret)
  1017. goto out;
  1018. sector += dev->zone_nr_sectors;
  1019. zone++;
  1020. }
  1021. }
  1022. /* The entire zone configuration of the disk should now be known */
  1023. if (sector < dev->capacity) {
  1024. dmz_dev_err(dev, "Failed to get correct zone information");
  1025. ret = -ENXIO;
  1026. }
  1027. out:
  1028. kfree(blkz);
  1029. if (ret)
  1030. dmz_drop_zones(zmd);
  1031. return ret;
  1032. }
  1033. /*
  1034. * Update a zone information.
  1035. */
  1036. static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1037. {
  1038. unsigned int nr_blkz = 1;
  1039. struct blk_zone blkz;
  1040. int ret;
  1041. /* Get zone information from disk */
  1042. ret = blkdev_report_zones(zmd->dev->bdev, dmz_start_sect(zmd, zone),
  1043. &blkz, &nr_blkz, GFP_NOIO);
  1044. if (!nr_blkz)
  1045. ret = -EIO;
  1046. if (ret) {
  1047. dmz_dev_err(zmd->dev, "Get zone %u report failed",
  1048. dmz_id(zmd, zone));
  1049. dmz_check_bdev(zmd->dev);
  1050. return ret;
  1051. }
  1052. clear_bit(DMZ_OFFLINE, &zone->flags);
  1053. clear_bit(DMZ_READ_ONLY, &zone->flags);
  1054. if (blkz.cond == BLK_ZONE_COND_OFFLINE)
  1055. set_bit(DMZ_OFFLINE, &zone->flags);
  1056. else if (blkz.cond == BLK_ZONE_COND_READONLY)
  1057. set_bit(DMZ_READ_ONLY, &zone->flags);
  1058. if (dmz_is_seq(zone))
  1059. zone->wp_block = dmz_sect2blk(blkz.wp - blkz.start);
  1060. else
  1061. zone->wp_block = 0;
  1062. return 0;
  1063. }
  1064. /*
  1065. * Check a zone write pointer position when the zone is marked
  1066. * with the sequential write error flag.
  1067. */
  1068. static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
  1069. struct dm_zone *zone)
  1070. {
  1071. unsigned int wp = 0;
  1072. int ret;
  1073. wp = zone->wp_block;
  1074. ret = dmz_update_zone(zmd, zone);
  1075. if (ret)
  1076. return ret;
  1077. dmz_dev_warn(zmd->dev, "Processing zone %u write error (zone wp %u/%u)",
  1078. dmz_id(zmd, zone), zone->wp_block, wp);
  1079. if (zone->wp_block < wp) {
  1080. dmz_invalidate_blocks(zmd, zone, zone->wp_block,
  1081. wp - zone->wp_block);
  1082. }
  1083. return 0;
  1084. }
  1085. static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
  1086. {
  1087. return &zmd->zones[zone_id];
  1088. }
  1089. /*
  1090. * Reset a zone write pointer.
  1091. */
  1092. static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1093. {
  1094. int ret;
  1095. /*
  1096. * Ignore offline zones, read only zones,
  1097. * and conventional zones.
  1098. */
  1099. if (dmz_is_offline(zone) ||
  1100. dmz_is_readonly(zone) ||
  1101. dmz_is_rnd(zone))
  1102. return 0;
  1103. if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
  1104. struct dmz_dev *dev = zmd->dev;
  1105. ret = blkdev_reset_zones(dev->bdev,
  1106. dmz_start_sect(zmd, zone),
  1107. dev->zone_nr_sectors, GFP_NOIO);
  1108. if (ret) {
  1109. dmz_dev_err(dev, "Reset zone %u failed %d",
  1110. dmz_id(zmd, zone), ret);
  1111. return ret;
  1112. }
  1113. }
  1114. /* Clear write error bit and rewind write pointer position */
  1115. clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
  1116. zone->wp_block = 0;
  1117. return 0;
  1118. }
  1119. static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
  1120. /*
  1121. * Initialize chunk mapping.
  1122. */
  1123. static int dmz_load_mapping(struct dmz_metadata *zmd)
  1124. {
  1125. struct dmz_dev *dev = zmd->dev;
  1126. struct dm_zone *dzone, *bzone;
  1127. struct dmz_mblock *dmap_mblk = NULL;
  1128. struct dmz_map *dmap;
  1129. unsigned int i = 0, e = 0, chunk = 0;
  1130. unsigned int dzone_id;
  1131. unsigned int bzone_id;
  1132. /* Metadata block array for the chunk mapping table */
  1133. zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
  1134. sizeof(struct dmz_mblk *), GFP_KERNEL);
  1135. if (!zmd->map_mblk)
  1136. return -ENOMEM;
  1137. /* Get chunk mapping table blocks and initialize zone mapping */
  1138. while (chunk < zmd->nr_chunks) {
  1139. if (!dmap_mblk) {
  1140. /* Get mapping block */
  1141. dmap_mblk = dmz_get_mblock(zmd, i + 1);
  1142. if (IS_ERR(dmap_mblk))
  1143. return PTR_ERR(dmap_mblk);
  1144. zmd->map_mblk[i] = dmap_mblk;
  1145. dmap = (struct dmz_map *) dmap_mblk->data;
  1146. i++;
  1147. e = 0;
  1148. }
  1149. /* Check data zone */
  1150. dzone_id = le32_to_cpu(dmap[e].dzone_id);
  1151. if (dzone_id == DMZ_MAP_UNMAPPED)
  1152. goto next;
  1153. if (dzone_id >= dev->nr_zones) {
  1154. dmz_dev_err(dev, "Chunk %u mapping: invalid data zone ID %u",
  1155. chunk, dzone_id);
  1156. return -EIO;
  1157. }
  1158. dzone = dmz_get(zmd, dzone_id);
  1159. set_bit(DMZ_DATA, &dzone->flags);
  1160. dzone->chunk = chunk;
  1161. dmz_get_zone_weight(zmd, dzone);
  1162. if (dmz_is_rnd(dzone))
  1163. list_add_tail(&dzone->link, &zmd->map_rnd_list);
  1164. else
  1165. list_add_tail(&dzone->link, &zmd->map_seq_list);
  1166. /* Check buffer zone */
  1167. bzone_id = le32_to_cpu(dmap[e].bzone_id);
  1168. if (bzone_id == DMZ_MAP_UNMAPPED)
  1169. goto next;
  1170. if (bzone_id >= dev->nr_zones) {
  1171. dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone ID %u",
  1172. chunk, bzone_id);
  1173. return -EIO;
  1174. }
  1175. bzone = dmz_get(zmd, bzone_id);
  1176. if (!dmz_is_rnd(bzone)) {
  1177. dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone %u",
  1178. chunk, bzone_id);
  1179. return -EIO;
  1180. }
  1181. set_bit(DMZ_DATA, &bzone->flags);
  1182. set_bit(DMZ_BUF, &bzone->flags);
  1183. bzone->chunk = chunk;
  1184. bzone->bzone = dzone;
  1185. dzone->bzone = bzone;
  1186. dmz_get_zone_weight(zmd, bzone);
  1187. list_add_tail(&bzone->link, &zmd->map_rnd_list);
  1188. next:
  1189. chunk++;
  1190. e++;
  1191. if (e >= DMZ_MAP_ENTRIES)
  1192. dmap_mblk = NULL;
  1193. }
  1194. /*
  1195. * At this point, only meta zones and mapped data zones were
  1196. * fully initialized. All remaining zones are unmapped data
  1197. * zones. Finish initializing those here.
  1198. */
  1199. for (i = 0; i < dev->nr_zones; i++) {
  1200. dzone = dmz_get(zmd, i);
  1201. if (dmz_is_meta(dzone))
  1202. continue;
  1203. if (dmz_is_rnd(dzone))
  1204. zmd->nr_rnd++;
  1205. else
  1206. zmd->nr_seq++;
  1207. if (dmz_is_data(dzone)) {
  1208. /* Already initialized */
  1209. continue;
  1210. }
  1211. /* Unmapped data zone */
  1212. set_bit(DMZ_DATA, &dzone->flags);
  1213. dzone->chunk = DMZ_MAP_UNMAPPED;
  1214. if (dmz_is_rnd(dzone)) {
  1215. list_add_tail(&dzone->link, &zmd->unmap_rnd_list);
  1216. atomic_inc(&zmd->unmap_nr_rnd);
  1217. } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
  1218. list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
  1219. atomic_inc(&zmd->nr_reserved_seq_zones);
  1220. zmd->nr_seq--;
  1221. } else {
  1222. list_add_tail(&dzone->link, &zmd->unmap_seq_list);
  1223. atomic_inc(&zmd->unmap_nr_seq);
  1224. }
  1225. }
  1226. return 0;
  1227. }
  1228. /*
  1229. * Set a data chunk mapping.
  1230. */
  1231. static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
  1232. unsigned int dzone_id, unsigned int bzone_id)
  1233. {
  1234. struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
  1235. struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
  1236. int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
  1237. dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
  1238. dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
  1239. dmz_dirty_mblock(zmd, dmap_mblk);
  1240. }
  1241. /*
  1242. * The list of mapped zones is maintained in LRU order.
  1243. * This rotates a zone at the end of its map list.
  1244. */
  1245. static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1246. {
  1247. if (list_empty(&zone->link))
  1248. return;
  1249. list_del_init(&zone->link);
  1250. if (dmz_is_seq(zone)) {
  1251. /* LRU rotate sequential zone */
  1252. list_add_tail(&zone->link, &zmd->map_seq_list);
  1253. } else {
  1254. /* LRU rotate random zone */
  1255. list_add_tail(&zone->link, &zmd->map_rnd_list);
  1256. }
  1257. }
  1258. /*
  1259. * The list of mapped random zones is maintained
  1260. * in LRU order. This rotates a zone at the end of the list.
  1261. */
  1262. static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1263. {
  1264. __dmz_lru_zone(zmd, zone);
  1265. if (zone->bzone)
  1266. __dmz_lru_zone(zmd, zone->bzone);
  1267. }
  1268. /*
  1269. * Wait for any zone to be freed.
  1270. */
  1271. static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
  1272. {
  1273. DEFINE_WAIT(wait);
  1274. prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
  1275. dmz_unlock_map(zmd);
  1276. dmz_unlock_metadata(zmd);
  1277. io_schedule_timeout(HZ);
  1278. dmz_lock_metadata(zmd);
  1279. dmz_lock_map(zmd);
  1280. finish_wait(&zmd->free_wq, &wait);
  1281. }
  1282. /*
  1283. * Lock a zone for reclaim (set the zone RECLAIM bit).
  1284. * Returns false if the zone cannot be locked or if it is already locked
  1285. * and 1 otherwise.
  1286. */
  1287. int dmz_lock_zone_reclaim(struct dm_zone *zone)
  1288. {
  1289. /* Active zones cannot be reclaimed */
  1290. if (dmz_is_active(zone))
  1291. return 0;
  1292. return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
  1293. }
  1294. /*
  1295. * Clear a zone reclaim flag.
  1296. */
  1297. void dmz_unlock_zone_reclaim(struct dm_zone *zone)
  1298. {
  1299. WARN_ON(dmz_is_active(zone));
  1300. WARN_ON(!dmz_in_reclaim(zone));
  1301. clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
  1302. smp_mb__after_atomic();
  1303. wake_up_bit(&zone->flags, DMZ_RECLAIM);
  1304. }
  1305. /*
  1306. * Wait for a zone reclaim to complete.
  1307. */
  1308. static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
  1309. {
  1310. dmz_unlock_map(zmd);
  1311. dmz_unlock_metadata(zmd);
  1312. wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
  1313. dmz_lock_metadata(zmd);
  1314. dmz_lock_map(zmd);
  1315. }
  1316. /*
  1317. * Select a random write zone for reclaim.
  1318. */
  1319. static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd)
  1320. {
  1321. struct dm_zone *dzone = NULL;
  1322. struct dm_zone *zone;
  1323. if (list_empty(&zmd->map_rnd_list))
  1324. return ERR_PTR(-EBUSY);
  1325. list_for_each_entry(zone, &zmd->map_rnd_list, link) {
  1326. if (dmz_is_buf(zone))
  1327. dzone = zone->bzone;
  1328. else
  1329. dzone = zone;
  1330. if (dmz_lock_zone_reclaim(dzone))
  1331. return dzone;
  1332. }
  1333. return ERR_PTR(-EBUSY);
  1334. }
  1335. /*
  1336. * Select a buffered sequential zone for reclaim.
  1337. */
  1338. static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd)
  1339. {
  1340. struct dm_zone *zone;
  1341. if (list_empty(&zmd->map_seq_list))
  1342. return ERR_PTR(-EBUSY);
  1343. list_for_each_entry(zone, &zmd->map_seq_list, link) {
  1344. if (!zone->bzone)
  1345. continue;
  1346. if (dmz_lock_zone_reclaim(zone))
  1347. return zone;
  1348. }
  1349. return ERR_PTR(-EBUSY);
  1350. }
  1351. /*
  1352. * Select a zone for reclaim.
  1353. */
  1354. struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd)
  1355. {
  1356. struct dm_zone *zone;
  1357. /*
  1358. * Search for a zone candidate to reclaim: 2 cases are possible.
  1359. * (1) There is no free sequential zones. Then a random data zone
  1360. * cannot be reclaimed. So choose a sequential zone to reclaim so
  1361. * that afterward a random zone can be reclaimed.
  1362. * (2) At least one free sequential zone is available, then choose
  1363. * the oldest random zone (data or buffer) that can be locked.
  1364. */
  1365. dmz_lock_map(zmd);
  1366. if (list_empty(&zmd->reserved_seq_zones_list))
  1367. zone = dmz_get_seq_zone_for_reclaim(zmd);
  1368. else
  1369. zone = dmz_get_rnd_zone_for_reclaim(zmd);
  1370. dmz_unlock_map(zmd);
  1371. return zone;
  1372. }
  1373. /*
  1374. * Get the zone mapping a chunk, if the chunk is mapped already.
  1375. * If no mapping exist and the operation is WRITE, a zone is
  1376. * allocated and used to map the chunk.
  1377. * The zone returned will be set to the active state.
  1378. */
  1379. struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op)
  1380. {
  1381. struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
  1382. struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
  1383. int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
  1384. unsigned int dzone_id;
  1385. struct dm_zone *dzone = NULL;
  1386. int ret = 0;
  1387. dmz_lock_map(zmd);
  1388. again:
  1389. /* Get the chunk mapping */
  1390. dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
  1391. if (dzone_id == DMZ_MAP_UNMAPPED) {
  1392. /*
  1393. * Read or discard in unmapped chunks are fine. But for
  1394. * writes, we need a mapping, so get one.
  1395. */
  1396. if (op != REQ_OP_WRITE)
  1397. goto out;
  1398. /* Alloate a random zone */
  1399. dzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
  1400. if (!dzone) {
  1401. if (dmz_bdev_is_dying(zmd->dev)) {
  1402. dzone = ERR_PTR(-EIO);
  1403. goto out;
  1404. }
  1405. dmz_wait_for_free_zones(zmd);
  1406. goto again;
  1407. }
  1408. dmz_map_zone(zmd, dzone, chunk);
  1409. } else {
  1410. /* The chunk is already mapped: get the mapping zone */
  1411. dzone = dmz_get(zmd, dzone_id);
  1412. if (dzone->chunk != chunk) {
  1413. dzone = ERR_PTR(-EIO);
  1414. goto out;
  1415. }
  1416. /* Repair write pointer if the sequential dzone has error */
  1417. if (dmz_seq_write_err(dzone)) {
  1418. ret = dmz_handle_seq_write_err(zmd, dzone);
  1419. if (ret) {
  1420. dzone = ERR_PTR(-EIO);
  1421. goto out;
  1422. }
  1423. clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
  1424. }
  1425. }
  1426. /*
  1427. * If the zone is being reclaimed, the chunk mapping may change
  1428. * to a different zone. So wait for reclaim and retry. Otherwise,
  1429. * activate the zone (this will prevent reclaim from touching it).
  1430. */
  1431. if (dmz_in_reclaim(dzone)) {
  1432. dmz_wait_for_reclaim(zmd, dzone);
  1433. goto again;
  1434. }
  1435. dmz_activate_zone(dzone);
  1436. dmz_lru_zone(zmd, dzone);
  1437. out:
  1438. dmz_unlock_map(zmd);
  1439. return dzone;
  1440. }
  1441. /*
  1442. * Write and discard change the block validity of data zones and their buffer
  1443. * zones. Check here that valid blocks are still present. If all blocks are
  1444. * invalid, the zones can be unmapped on the fly without waiting for reclaim
  1445. * to do it.
  1446. */
  1447. void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
  1448. {
  1449. struct dm_zone *bzone;
  1450. dmz_lock_map(zmd);
  1451. bzone = dzone->bzone;
  1452. if (bzone) {
  1453. if (dmz_weight(bzone))
  1454. dmz_lru_zone(zmd, bzone);
  1455. else {
  1456. /* Empty buffer zone: reclaim it */
  1457. dmz_unmap_zone(zmd, bzone);
  1458. dmz_free_zone(zmd, bzone);
  1459. bzone = NULL;
  1460. }
  1461. }
  1462. /* Deactivate the data zone */
  1463. dmz_deactivate_zone(dzone);
  1464. if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
  1465. dmz_lru_zone(zmd, dzone);
  1466. else {
  1467. /* Unbuffered inactive empty data zone: reclaim it */
  1468. dmz_unmap_zone(zmd, dzone);
  1469. dmz_free_zone(zmd, dzone);
  1470. }
  1471. dmz_unlock_map(zmd);
  1472. }
  1473. /*
  1474. * Allocate and map a random zone to buffer a chunk
  1475. * already mapped to a sequential zone.
  1476. */
  1477. struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
  1478. struct dm_zone *dzone)
  1479. {
  1480. struct dm_zone *bzone;
  1481. dmz_lock_map(zmd);
  1482. again:
  1483. bzone = dzone->bzone;
  1484. if (bzone)
  1485. goto out;
  1486. /* Alloate a random zone */
  1487. bzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
  1488. if (!bzone) {
  1489. if (dmz_bdev_is_dying(zmd->dev)) {
  1490. bzone = ERR_PTR(-EIO);
  1491. goto out;
  1492. }
  1493. dmz_wait_for_free_zones(zmd);
  1494. goto again;
  1495. }
  1496. /* Update the chunk mapping */
  1497. dmz_set_chunk_mapping(zmd, dzone->chunk, dmz_id(zmd, dzone),
  1498. dmz_id(zmd, bzone));
  1499. set_bit(DMZ_BUF, &bzone->flags);
  1500. bzone->chunk = dzone->chunk;
  1501. bzone->bzone = dzone;
  1502. dzone->bzone = bzone;
  1503. list_add_tail(&bzone->link, &zmd->map_rnd_list);
  1504. out:
  1505. dmz_unlock_map(zmd);
  1506. return bzone;
  1507. }
  1508. /*
  1509. * Get an unmapped (free) zone.
  1510. * This must be called with the mapping lock held.
  1511. */
  1512. struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags)
  1513. {
  1514. struct list_head *list;
  1515. struct dm_zone *zone;
  1516. if (flags & DMZ_ALLOC_RND)
  1517. list = &zmd->unmap_rnd_list;
  1518. else
  1519. list = &zmd->unmap_seq_list;
  1520. again:
  1521. if (list_empty(list)) {
  1522. /*
  1523. * No free zone: if this is for reclaim, allow using the
  1524. * reserved sequential zones.
  1525. */
  1526. if (!(flags & DMZ_ALLOC_RECLAIM) ||
  1527. list_empty(&zmd->reserved_seq_zones_list))
  1528. return NULL;
  1529. zone = list_first_entry(&zmd->reserved_seq_zones_list,
  1530. struct dm_zone, link);
  1531. list_del_init(&zone->link);
  1532. atomic_dec(&zmd->nr_reserved_seq_zones);
  1533. return zone;
  1534. }
  1535. zone = list_first_entry(list, struct dm_zone, link);
  1536. list_del_init(&zone->link);
  1537. if (dmz_is_rnd(zone))
  1538. atomic_dec(&zmd->unmap_nr_rnd);
  1539. else
  1540. atomic_dec(&zmd->unmap_nr_seq);
  1541. if (dmz_is_offline(zone)) {
  1542. dmz_dev_warn(zmd->dev, "Zone %u is offline", dmz_id(zmd, zone));
  1543. zone = NULL;
  1544. goto again;
  1545. }
  1546. return zone;
  1547. }
  1548. /*
  1549. * Free a zone.
  1550. * This must be called with the mapping lock held.
  1551. */
  1552. void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1553. {
  1554. /* If this is a sequential zone, reset it */
  1555. if (dmz_is_seq(zone))
  1556. dmz_reset_zone(zmd, zone);
  1557. /* Return the zone to its type unmap list */
  1558. if (dmz_is_rnd(zone)) {
  1559. list_add_tail(&zone->link, &zmd->unmap_rnd_list);
  1560. atomic_inc(&zmd->unmap_nr_rnd);
  1561. } else if (atomic_read(&zmd->nr_reserved_seq_zones) <
  1562. zmd->nr_reserved_seq) {
  1563. list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
  1564. atomic_inc(&zmd->nr_reserved_seq_zones);
  1565. } else {
  1566. list_add_tail(&zone->link, &zmd->unmap_seq_list);
  1567. atomic_inc(&zmd->unmap_nr_seq);
  1568. }
  1569. wake_up_all(&zmd->free_wq);
  1570. }
  1571. /*
  1572. * Map a chunk to a zone.
  1573. * This must be called with the mapping lock held.
  1574. */
  1575. void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
  1576. unsigned int chunk)
  1577. {
  1578. /* Set the chunk mapping */
  1579. dmz_set_chunk_mapping(zmd, chunk, dmz_id(zmd, dzone),
  1580. DMZ_MAP_UNMAPPED);
  1581. dzone->chunk = chunk;
  1582. if (dmz_is_rnd(dzone))
  1583. list_add_tail(&dzone->link, &zmd->map_rnd_list);
  1584. else
  1585. list_add_tail(&dzone->link, &zmd->map_seq_list);
  1586. }
  1587. /*
  1588. * Unmap a zone.
  1589. * This must be called with the mapping lock held.
  1590. */
  1591. void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1592. {
  1593. unsigned int chunk = zone->chunk;
  1594. unsigned int dzone_id;
  1595. if (chunk == DMZ_MAP_UNMAPPED) {
  1596. /* Already unmapped */
  1597. return;
  1598. }
  1599. if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
  1600. /*
  1601. * Unmapping the chunk buffer zone: clear only
  1602. * the chunk buffer mapping
  1603. */
  1604. dzone_id = dmz_id(zmd, zone->bzone);
  1605. zone->bzone->bzone = NULL;
  1606. zone->bzone = NULL;
  1607. } else {
  1608. /*
  1609. * Unmapping the chunk data zone: the zone must
  1610. * not be buffered.
  1611. */
  1612. if (WARN_ON(zone->bzone)) {
  1613. zone->bzone->bzone = NULL;
  1614. zone->bzone = NULL;
  1615. }
  1616. dzone_id = DMZ_MAP_UNMAPPED;
  1617. }
  1618. dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
  1619. zone->chunk = DMZ_MAP_UNMAPPED;
  1620. list_del_init(&zone->link);
  1621. }
  1622. /*
  1623. * Set @nr_bits bits in @bitmap starting from @bit.
  1624. * Return the number of bits changed from 0 to 1.
  1625. */
  1626. static unsigned int dmz_set_bits(unsigned long *bitmap,
  1627. unsigned int bit, unsigned int nr_bits)
  1628. {
  1629. unsigned long *addr;
  1630. unsigned int end = bit + nr_bits;
  1631. unsigned int n = 0;
  1632. while (bit < end) {
  1633. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1634. ((end - bit) >= BITS_PER_LONG)) {
  1635. /* Try to set the whole word at once */
  1636. addr = bitmap + BIT_WORD(bit);
  1637. if (*addr == 0) {
  1638. *addr = ULONG_MAX;
  1639. n += BITS_PER_LONG;
  1640. bit += BITS_PER_LONG;
  1641. continue;
  1642. }
  1643. }
  1644. if (!test_and_set_bit(bit, bitmap))
  1645. n++;
  1646. bit++;
  1647. }
  1648. return n;
  1649. }
  1650. /*
  1651. * Get the bitmap block storing the bit for chunk_block in zone.
  1652. */
  1653. static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
  1654. struct dm_zone *zone,
  1655. sector_t chunk_block)
  1656. {
  1657. sector_t bitmap_block = 1 + zmd->nr_map_blocks +
  1658. (sector_t)(dmz_id(zmd, zone) * zmd->zone_nr_bitmap_blocks) +
  1659. (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
  1660. return dmz_get_mblock(zmd, bitmap_block);
  1661. }
  1662. /*
  1663. * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone.
  1664. */
  1665. int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  1666. struct dm_zone *to_zone)
  1667. {
  1668. struct dmz_mblock *from_mblk, *to_mblk;
  1669. sector_t chunk_block = 0;
  1670. /* Get the zones bitmap blocks */
  1671. while (chunk_block < zmd->dev->zone_nr_blocks) {
  1672. from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
  1673. if (IS_ERR(from_mblk))
  1674. return PTR_ERR(from_mblk);
  1675. to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
  1676. if (IS_ERR(to_mblk)) {
  1677. dmz_release_mblock(zmd, from_mblk);
  1678. return PTR_ERR(to_mblk);
  1679. }
  1680. memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
  1681. dmz_dirty_mblock(zmd, to_mblk);
  1682. dmz_release_mblock(zmd, to_mblk);
  1683. dmz_release_mblock(zmd, from_mblk);
  1684. chunk_block += zmd->zone_bits_per_mblk;
  1685. }
  1686. to_zone->weight = from_zone->weight;
  1687. return 0;
  1688. }
  1689. /*
  1690. * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone,
  1691. * starting from chunk_block.
  1692. */
  1693. int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  1694. struct dm_zone *to_zone, sector_t chunk_block)
  1695. {
  1696. unsigned int nr_blocks;
  1697. int ret;
  1698. /* Get the zones bitmap blocks */
  1699. while (chunk_block < zmd->dev->zone_nr_blocks) {
  1700. /* Get a valid region from the source zone */
  1701. ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
  1702. if (ret <= 0)
  1703. return ret;
  1704. nr_blocks = ret;
  1705. ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
  1706. if (ret)
  1707. return ret;
  1708. chunk_block += nr_blocks;
  1709. }
  1710. return 0;
  1711. }
  1712. /*
  1713. * Validate all the blocks in the range [block..block+nr_blocks-1].
  1714. */
  1715. int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  1716. sector_t chunk_block, unsigned int nr_blocks)
  1717. {
  1718. unsigned int count, bit, nr_bits;
  1719. unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
  1720. struct dmz_mblock *mblk;
  1721. unsigned int n = 0;
  1722. dmz_dev_debug(zmd->dev, "=> VALIDATE zone %u, block %llu, %u blocks",
  1723. dmz_id(zmd, zone), (unsigned long long)chunk_block,
  1724. nr_blocks);
  1725. WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
  1726. while (nr_blocks) {
  1727. /* Get bitmap block */
  1728. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1729. if (IS_ERR(mblk))
  1730. return PTR_ERR(mblk);
  1731. /* Set bits */
  1732. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1733. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1734. count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
  1735. if (count) {
  1736. dmz_dirty_mblock(zmd, mblk);
  1737. n += count;
  1738. }
  1739. dmz_release_mblock(zmd, mblk);
  1740. nr_blocks -= nr_bits;
  1741. chunk_block += nr_bits;
  1742. }
  1743. if (likely(zone->weight + n <= zone_nr_blocks))
  1744. zone->weight += n;
  1745. else {
  1746. dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be <= %u",
  1747. dmz_id(zmd, zone), zone->weight,
  1748. zone_nr_blocks - n);
  1749. zone->weight = zone_nr_blocks;
  1750. }
  1751. return 0;
  1752. }
  1753. /*
  1754. * Clear nr_bits bits in bitmap starting from bit.
  1755. * Return the number of bits cleared.
  1756. */
  1757. static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
  1758. {
  1759. unsigned long *addr;
  1760. int end = bit + nr_bits;
  1761. int n = 0;
  1762. while (bit < end) {
  1763. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1764. ((end - bit) >= BITS_PER_LONG)) {
  1765. /* Try to clear whole word at once */
  1766. addr = bitmap + BIT_WORD(bit);
  1767. if (*addr == ULONG_MAX) {
  1768. *addr = 0;
  1769. n += BITS_PER_LONG;
  1770. bit += BITS_PER_LONG;
  1771. continue;
  1772. }
  1773. }
  1774. if (test_and_clear_bit(bit, bitmap))
  1775. n++;
  1776. bit++;
  1777. }
  1778. return n;
  1779. }
  1780. /*
  1781. * Invalidate all the blocks in the range [block..block+nr_blocks-1].
  1782. */
  1783. int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  1784. sector_t chunk_block, unsigned int nr_blocks)
  1785. {
  1786. unsigned int count, bit, nr_bits;
  1787. struct dmz_mblock *mblk;
  1788. unsigned int n = 0;
  1789. dmz_dev_debug(zmd->dev, "=> INVALIDATE zone %u, block %llu, %u blocks",
  1790. dmz_id(zmd, zone), (u64)chunk_block, nr_blocks);
  1791. WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
  1792. while (nr_blocks) {
  1793. /* Get bitmap block */
  1794. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1795. if (IS_ERR(mblk))
  1796. return PTR_ERR(mblk);
  1797. /* Clear bits */
  1798. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1799. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1800. count = dmz_clear_bits((unsigned long *)mblk->data,
  1801. bit, nr_bits);
  1802. if (count) {
  1803. dmz_dirty_mblock(zmd, mblk);
  1804. n += count;
  1805. }
  1806. dmz_release_mblock(zmd, mblk);
  1807. nr_blocks -= nr_bits;
  1808. chunk_block += nr_bits;
  1809. }
  1810. if (zone->weight >= n)
  1811. zone->weight -= n;
  1812. else {
  1813. dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be >= %u",
  1814. dmz_id(zmd, zone), zone->weight, n);
  1815. zone->weight = 0;
  1816. }
  1817. return 0;
  1818. }
  1819. /*
  1820. * Get a block bit value.
  1821. */
  1822. static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1823. sector_t chunk_block)
  1824. {
  1825. struct dmz_mblock *mblk;
  1826. int ret;
  1827. WARN_ON(chunk_block >= zmd->dev->zone_nr_blocks);
  1828. /* Get bitmap block */
  1829. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1830. if (IS_ERR(mblk))
  1831. return PTR_ERR(mblk);
  1832. /* Get offset */
  1833. ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
  1834. (unsigned long *) mblk->data) != 0;
  1835. dmz_release_mblock(zmd, mblk);
  1836. return ret;
  1837. }
  1838. /*
  1839. * Return the number of blocks from chunk_block to the first block with a bit
  1840. * value specified by set. Search at most nr_blocks blocks from chunk_block.
  1841. */
  1842. static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1843. sector_t chunk_block, unsigned int nr_blocks,
  1844. int set)
  1845. {
  1846. struct dmz_mblock *mblk;
  1847. unsigned int bit, set_bit, nr_bits;
  1848. unsigned int zone_bits = zmd->zone_bits_per_mblk;
  1849. unsigned long *bitmap;
  1850. int n = 0;
  1851. WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
  1852. while (nr_blocks) {
  1853. /* Get bitmap block */
  1854. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1855. if (IS_ERR(mblk))
  1856. return PTR_ERR(mblk);
  1857. /* Get offset */
  1858. bitmap = (unsigned long *) mblk->data;
  1859. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1860. nr_bits = min(nr_blocks, zone_bits - bit);
  1861. if (set)
  1862. set_bit = find_next_bit(bitmap, zone_bits, bit);
  1863. else
  1864. set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
  1865. dmz_release_mblock(zmd, mblk);
  1866. n += set_bit - bit;
  1867. if (set_bit < zone_bits)
  1868. break;
  1869. nr_blocks -= nr_bits;
  1870. chunk_block += nr_bits;
  1871. }
  1872. return n;
  1873. }
  1874. /*
  1875. * Test if chunk_block is valid. If it is, the number of consecutive
  1876. * valid blocks from chunk_block will be returned.
  1877. */
  1878. int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
  1879. sector_t chunk_block)
  1880. {
  1881. int valid;
  1882. valid = dmz_test_block(zmd, zone, chunk_block);
  1883. if (valid <= 0)
  1884. return valid;
  1885. /* The block is valid: get the number of valid blocks from block */
  1886. return dmz_to_next_set_block(zmd, zone, chunk_block,
  1887. zmd->dev->zone_nr_blocks - chunk_block, 0);
  1888. }
  1889. /*
  1890. * Find the first valid block from @chunk_block in @zone.
  1891. * If such a block is found, its number is returned using
  1892. * @chunk_block and the total number of valid blocks from @chunk_block
  1893. * is returned.
  1894. */
  1895. int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1896. sector_t *chunk_block)
  1897. {
  1898. sector_t start_block = *chunk_block;
  1899. int ret;
  1900. ret = dmz_to_next_set_block(zmd, zone, start_block,
  1901. zmd->dev->zone_nr_blocks - start_block, 1);
  1902. if (ret < 0)
  1903. return ret;
  1904. start_block += ret;
  1905. *chunk_block = start_block;
  1906. return dmz_to_next_set_block(zmd, zone, start_block,
  1907. zmd->dev->zone_nr_blocks - start_block, 0);
  1908. }
  1909. /*
  1910. * Count the number of bits set starting from bit up to bit + nr_bits - 1.
  1911. */
  1912. static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
  1913. {
  1914. unsigned long *addr;
  1915. int end = bit + nr_bits;
  1916. int n = 0;
  1917. while (bit < end) {
  1918. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1919. ((end - bit) >= BITS_PER_LONG)) {
  1920. addr = (unsigned long *)bitmap + BIT_WORD(bit);
  1921. if (*addr == ULONG_MAX) {
  1922. n += BITS_PER_LONG;
  1923. bit += BITS_PER_LONG;
  1924. continue;
  1925. }
  1926. }
  1927. if (test_bit(bit, bitmap))
  1928. n++;
  1929. bit++;
  1930. }
  1931. return n;
  1932. }
  1933. /*
  1934. * Get a zone weight.
  1935. */
  1936. static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
  1937. {
  1938. struct dmz_mblock *mblk;
  1939. sector_t chunk_block = 0;
  1940. unsigned int bit, nr_bits;
  1941. unsigned int nr_blocks = zmd->dev->zone_nr_blocks;
  1942. void *bitmap;
  1943. int n = 0;
  1944. while (nr_blocks) {
  1945. /* Get bitmap block */
  1946. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1947. if (IS_ERR(mblk)) {
  1948. n = 0;
  1949. break;
  1950. }
  1951. /* Count bits in this block */
  1952. bitmap = mblk->data;
  1953. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1954. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1955. n += dmz_count_bits(bitmap, bit, nr_bits);
  1956. dmz_release_mblock(zmd, mblk);
  1957. nr_blocks -= nr_bits;
  1958. chunk_block += nr_bits;
  1959. }
  1960. zone->weight = n;
  1961. }
  1962. /*
  1963. * Cleanup the zoned metadata resources.
  1964. */
  1965. static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
  1966. {
  1967. struct rb_root *root;
  1968. struct dmz_mblock *mblk, *next;
  1969. int i;
  1970. /* Release zone mapping resources */
  1971. if (zmd->map_mblk) {
  1972. for (i = 0; i < zmd->nr_map_blocks; i++)
  1973. dmz_release_mblock(zmd, zmd->map_mblk[i]);
  1974. kfree(zmd->map_mblk);
  1975. zmd->map_mblk = NULL;
  1976. }
  1977. /* Release super blocks */
  1978. for (i = 0; i < 2; i++) {
  1979. if (zmd->sb[i].mblk) {
  1980. dmz_free_mblock(zmd, zmd->sb[i].mblk);
  1981. zmd->sb[i].mblk = NULL;
  1982. }
  1983. }
  1984. /* Free cached blocks */
  1985. while (!list_empty(&zmd->mblk_dirty_list)) {
  1986. mblk = list_first_entry(&zmd->mblk_dirty_list,
  1987. struct dmz_mblock, link);
  1988. dmz_dev_warn(zmd->dev, "mblock %llu still in dirty list (ref %u)",
  1989. (u64)mblk->no, mblk->ref);
  1990. list_del_init(&mblk->link);
  1991. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  1992. dmz_free_mblock(zmd, mblk);
  1993. }
  1994. while (!list_empty(&zmd->mblk_lru_list)) {
  1995. mblk = list_first_entry(&zmd->mblk_lru_list,
  1996. struct dmz_mblock, link);
  1997. list_del_init(&mblk->link);
  1998. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  1999. dmz_free_mblock(zmd, mblk);
  2000. }
  2001. /* Sanity checks: the mblock rbtree should now be empty */
  2002. root = &zmd->mblk_rbtree;
  2003. rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
  2004. dmz_dev_warn(zmd->dev, "mblock %llu ref %u still in rbtree",
  2005. (u64)mblk->no, mblk->ref);
  2006. mblk->ref = 0;
  2007. dmz_free_mblock(zmd, mblk);
  2008. }
  2009. /* Free the zone descriptors */
  2010. dmz_drop_zones(zmd);
  2011. mutex_destroy(&zmd->mblk_flush_lock);
  2012. mutex_destroy(&zmd->map_lock);
  2013. }
  2014. /*
  2015. * Initialize the zoned metadata.
  2016. */
  2017. int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **metadata)
  2018. {
  2019. struct dmz_metadata *zmd;
  2020. unsigned int i, zid;
  2021. struct dm_zone *zone;
  2022. int ret;
  2023. zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
  2024. if (!zmd)
  2025. return -ENOMEM;
  2026. zmd->dev = dev;
  2027. zmd->mblk_rbtree = RB_ROOT;
  2028. init_rwsem(&zmd->mblk_sem);
  2029. mutex_init(&zmd->mblk_flush_lock);
  2030. spin_lock_init(&zmd->mblk_lock);
  2031. INIT_LIST_HEAD(&zmd->mblk_lru_list);
  2032. INIT_LIST_HEAD(&zmd->mblk_dirty_list);
  2033. mutex_init(&zmd->map_lock);
  2034. atomic_set(&zmd->unmap_nr_rnd, 0);
  2035. INIT_LIST_HEAD(&zmd->unmap_rnd_list);
  2036. INIT_LIST_HEAD(&zmd->map_rnd_list);
  2037. atomic_set(&zmd->unmap_nr_seq, 0);
  2038. INIT_LIST_HEAD(&zmd->unmap_seq_list);
  2039. INIT_LIST_HEAD(&zmd->map_seq_list);
  2040. atomic_set(&zmd->nr_reserved_seq_zones, 0);
  2041. INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
  2042. init_waitqueue_head(&zmd->free_wq);
  2043. /* Initialize zone descriptors */
  2044. ret = dmz_init_zones(zmd);
  2045. if (ret)
  2046. goto err;
  2047. /* Get super block */
  2048. ret = dmz_load_sb(zmd);
  2049. if (ret)
  2050. goto err;
  2051. /* Set metadata zones starting from sb_zone */
  2052. zid = dmz_id(zmd, zmd->sb_zone);
  2053. for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
  2054. zone = dmz_get(zmd, zid + i);
  2055. if (!dmz_is_rnd(zone))
  2056. goto err;
  2057. set_bit(DMZ_META, &zone->flags);
  2058. }
  2059. /* Load mapping table */
  2060. ret = dmz_load_mapping(zmd);
  2061. if (ret)
  2062. goto err;
  2063. /*
  2064. * Cache size boundaries: allow at least 2 super blocks, the chunk map
  2065. * blocks and enough blocks to be able to cache the bitmap blocks of
  2066. * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow
  2067. * the cache to add 512 more metadata blocks.
  2068. */
  2069. zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
  2070. zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
  2071. zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
  2072. zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
  2073. zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
  2074. /* Metadata cache shrinker */
  2075. ret = register_shrinker(&zmd->mblk_shrinker);
  2076. if (ret) {
  2077. dmz_dev_err(dev, "Register metadata cache shrinker failed");
  2078. goto err;
  2079. }
  2080. dmz_dev_info(dev, "Host-%s zoned block device",
  2081. bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
  2082. "aware" : "managed");
  2083. dmz_dev_info(dev, " %llu 512-byte logical sectors",
  2084. (u64)dev->capacity);
  2085. dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
  2086. dev->nr_zones, (u64)dev->zone_nr_sectors);
  2087. dmz_dev_info(dev, " %u metadata zones",
  2088. zmd->nr_meta_zones * 2);
  2089. dmz_dev_info(dev, " %u data zones for %u chunks",
  2090. zmd->nr_data_zones, zmd->nr_chunks);
  2091. dmz_dev_info(dev, " %u random zones (%u unmapped)",
  2092. zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd));
  2093. dmz_dev_info(dev, " %u sequential zones (%u unmapped)",
  2094. zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq));
  2095. dmz_dev_info(dev, " %u reserved sequential data zones",
  2096. zmd->nr_reserved_seq);
  2097. dmz_dev_debug(dev, "Format:");
  2098. dmz_dev_debug(dev, "%u metadata blocks per set (%u max cache)",
  2099. zmd->nr_meta_blocks, zmd->max_nr_mblks);
  2100. dmz_dev_debug(dev, " %u data zone mapping blocks",
  2101. zmd->nr_map_blocks);
  2102. dmz_dev_debug(dev, " %u bitmap blocks",
  2103. zmd->nr_bitmap_blocks);
  2104. *metadata = zmd;
  2105. return 0;
  2106. err:
  2107. dmz_cleanup_metadata(zmd);
  2108. kfree(zmd);
  2109. *metadata = NULL;
  2110. return ret;
  2111. }
  2112. /*
  2113. * Cleanup the zoned metadata resources.
  2114. */
  2115. void dmz_dtr_metadata(struct dmz_metadata *zmd)
  2116. {
  2117. unregister_shrinker(&zmd->mblk_shrinker);
  2118. dmz_cleanup_metadata(zmd);
  2119. kfree(zmd);
  2120. }
  2121. /*
  2122. * Check zone information on resume.
  2123. */
  2124. int dmz_resume_metadata(struct dmz_metadata *zmd)
  2125. {
  2126. struct dmz_dev *dev = zmd->dev;
  2127. struct dm_zone *zone;
  2128. sector_t wp_block;
  2129. unsigned int i;
  2130. int ret;
  2131. /* Check zones */
  2132. for (i = 0; i < dev->nr_zones; i++) {
  2133. zone = dmz_get(zmd, i);
  2134. if (!zone) {
  2135. dmz_dev_err(dev, "Unable to get zone %u", i);
  2136. return -EIO;
  2137. }
  2138. wp_block = zone->wp_block;
  2139. ret = dmz_update_zone(zmd, zone);
  2140. if (ret) {
  2141. dmz_dev_err(dev, "Broken zone %u", i);
  2142. return ret;
  2143. }
  2144. if (dmz_is_offline(zone)) {
  2145. dmz_dev_warn(dev, "Zone %u is offline", i);
  2146. continue;
  2147. }
  2148. /* Check write pointer */
  2149. if (!dmz_is_seq(zone))
  2150. zone->wp_block = 0;
  2151. else if (zone->wp_block != wp_block) {
  2152. dmz_dev_err(dev, "Zone %u: Invalid wp (%llu / %llu)",
  2153. i, (u64)zone->wp_block, (u64)wp_block);
  2154. zone->wp_block = wp_block;
  2155. dmz_invalidate_blocks(zmd, zone, zone->wp_block,
  2156. dev->zone_nr_blocks - zone->wp_block);
  2157. }
  2158. }
  2159. return 0;
  2160. }