dm-zoned-metadata.c 60 KB

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