dm-zoned-metadata.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  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. } else if (blkz->type == BLK_ZONE_TYPE_SEQWRITE_REQ ||
  926. blkz->type == BLK_ZONE_TYPE_SEQWRITE_PREF) {
  927. set_bit(DMZ_SEQ, &zone->flags);
  928. } else
  929. return -ENXIO;
  930. if (blkz->cond == BLK_ZONE_COND_OFFLINE)
  931. set_bit(DMZ_OFFLINE, &zone->flags);
  932. else if (blkz->cond == BLK_ZONE_COND_READONLY)
  933. set_bit(DMZ_READ_ONLY, &zone->flags);
  934. if (dmz_is_rnd(zone))
  935. zone->wp_block = 0;
  936. else
  937. zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
  938. if (!dmz_is_offline(zone) && !dmz_is_readonly(zone)) {
  939. zmd->nr_useable_zones++;
  940. if (dmz_is_rnd(zone)) {
  941. zmd->nr_rnd_zones++;
  942. if (!zmd->sb_zone) {
  943. /* Super block zone */
  944. zmd->sb_zone = zone;
  945. }
  946. }
  947. }
  948. return 0;
  949. }
  950. /*
  951. * Free zones descriptors.
  952. */
  953. static void dmz_drop_zones(struct dmz_metadata *zmd)
  954. {
  955. kfree(zmd->zones);
  956. zmd->zones = NULL;
  957. }
  958. /*
  959. * The size of a zone report in number of zones.
  960. * This results in 4096*64B=256KB report zones commands.
  961. */
  962. #define DMZ_REPORT_NR_ZONES 4096
  963. /*
  964. * Allocate and initialize zone descriptors using the zone
  965. * information from disk.
  966. */
  967. static int dmz_init_zones(struct dmz_metadata *zmd)
  968. {
  969. struct dmz_dev *dev = zmd->dev;
  970. struct dm_zone *zone;
  971. struct blk_zone *blkz;
  972. unsigned int nr_blkz;
  973. sector_t sector = 0;
  974. int i, ret = 0;
  975. /* Init */
  976. zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
  977. zmd->zone_nr_bitmap_blocks =
  978. max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
  979. zmd->zone_bits_per_mblk = min_t(sector_t, dev->zone_nr_blocks,
  980. DMZ_BLOCK_SIZE_BITS);
  981. /* Allocate zone array */
  982. zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
  983. if (!zmd->zones)
  984. return -ENOMEM;
  985. dmz_dev_info(dev, "Using %zu B for zone information",
  986. sizeof(struct dm_zone) * dev->nr_zones);
  987. /* Get zone information */
  988. nr_blkz = DMZ_REPORT_NR_ZONES;
  989. blkz = kcalloc(nr_blkz, sizeof(struct blk_zone), GFP_KERNEL);
  990. if (!blkz) {
  991. ret = -ENOMEM;
  992. goto out;
  993. }
  994. /*
  995. * Get zone information and initialize zone descriptors.
  996. * At the same time, determine where the super block
  997. * should be: first block of the first randomly writable
  998. * zone.
  999. */
  1000. zone = zmd->zones;
  1001. while (sector < dev->capacity) {
  1002. /* Get zone information */
  1003. nr_blkz = DMZ_REPORT_NR_ZONES;
  1004. ret = blkdev_report_zones(dev->bdev, sector, blkz,
  1005. &nr_blkz, GFP_KERNEL);
  1006. if (ret) {
  1007. dmz_dev_err(dev, "Report zones failed %d", ret);
  1008. goto out;
  1009. }
  1010. if (!nr_blkz)
  1011. break;
  1012. /* Process report */
  1013. for (i = 0; i < nr_blkz; i++) {
  1014. ret = dmz_init_zone(zmd, zone, &blkz[i]);
  1015. if (ret)
  1016. goto out;
  1017. sector += dev->zone_nr_sectors;
  1018. zone++;
  1019. }
  1020. }
  1021. /* The entire zone configuration of the disk should now be known */
  1022. if (sector < dev->capacity) {
  1023. dmz_dev_err(dev, "Failed to get correct zone information");
  1024. ret = -ENXIO;
  1025. }
  1026. out:
  1027. kfree(blkz);
  1028. if (ret)
  1029. dmz_drop_zones(zmd);
  1030. return ret;
  1031. }
  1032. /*
  1033. * Update a zone information.
  1034. */
  1035. static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1036. {
  1037. unsigned int nr_blkz = 1;
  1038. struct blk_zone blkz;
  1039. int ret;
  1040. /* Get zone information from disk */
  1041. ret = blkdev_report_zones(zmd->dev->bdev, dmz_start_sect(zmd, zone),
  1042. &blkz, &nr_blkz, GFP_NOIO);
  1043. if (!nr_blkz)
  1044. ret = -EIO;
  1045. if (ret) {
  1046. dmz_dev_err(zmd->dev, "Get zone %u report failed",
  1047. dmz_id(zmd, zone));
  1048. dmz_check_bdev(zmd->dev);
  1049. return ret;
  1050. }
  1051. clear_bit(DMZ_OFFLINE, &zone->flags);
  1052. clear_bit(DMZ_READ_ONLY, &zone->flags);
  1053. if (blkz.cond == BLK_ZONE_COND_OFFLINE)
  1054. set_bit(DMZ_OFFLINE, &zone->flags);
  1055. else if (blkz.cond == BLK_ZONE_COND_READONLY)
  1056. set_bit(DMZ_READ_ONLY, &zone->flags);
  1057. if (dmz_is_seq(zone))
  1058. zone->wp_block = dmz_sect2blk(blkz.wp - blkz.start);
  1059. else
  1060. zone->wp_block = 0;
  1061. return 0;
  1062. }
  1063. /*
  1064. * Check a zone write pointer position when the zone is marked
  1065. * with the sequential write error flag.
  1066. */
  1067. static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
  1068. struct dm_zone *zone)
  1069. {
  1070. unsigned int wp = 0;
  1071. int ret;
  1072. wp = zone->wp_block;
  1073. ret = dmz_update_zone(zmd, zone);
  1074. if (ret)
  1075. return ret;
  1076. dmz_dev_warn(zmd->dev, "Processing zone %u write error (zone wp %u/%u)",
  1077. dmz_id(zmd, zone), zone->wp_block, wp);
  1078. if (zone->wp_block < wp) {
  1079. dmz_invalidate_blocks(zmd, zone, zone->wp_block,
  1080. wp - zone->wp_block);
  1081. }
  1082. return 0;
  1083. }
  1084. static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
  1085. {
  1086. return &zmd->zones[zone_id];
  1087. }
  1088. /*
  1089. * Reset a zone write pointer.
  1090. */
  1091. static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1092. {
  1093. int ret;
  1094. /*
  1095. * Ignore offline zones, read only zones,
  1096. * and conventional zones.
  1097. */
  1098. if (dmz_is_offline(zone) ||
  1099. dmz_is_readonly(zone) ||
  1100. dmz_is_rnd(zone))
  1101. return 0;
  1102. if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
  1103. struct dmz_dev *dev = zmd->dev;
  1104. ret = blkdev_reset_zones(dev->bdev,
  1105. dmz_start_sect(zmd, zone),
  1106. dev->zone_nr_sectors, GFP_NOIO);
  1107. if (ret) {
  1108. dmz_dev_err(dev, "Reset zone %u failed %d",
  1109. dmz_id(zmd, zone), ret);
  1110. return ret;
  1111. }
  1112. }
  1113. /* Clear write error bit and rewind write pointer position */
  1114. clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
  1115. zone->wp_block = 0;
  1116. return 0;
  1117. }
  1118. static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
  1119. /*
  1120. * Initialize chunk mapping.
  1121. */
  1122. static int dmz_load_mapping(struct dmz_metadata *zmd)
  1123. {
  1124. struct dmz_dev *dev = zmd->dev;
  1125. struct dm_zone *dzone, *bzone;
  1126. struct dmz_mblock *dmap_mblk = NULL;
  1127. struct dmz_map *dmap;
  1128. unsigned int i = 0, e = 0, chunk = 0;
  1129. unsigned int dzone_id;
  1130. unsigned int bzone_id;
  1131. /* Metadata block array for the chunk mapping table */
  1132. zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
  1133. sizeof(struct dmz_mblk *), GFP_KERNEL);
  1134. if (!zmd->map_mblk)
  1135. return -ENOMEM;
  1136. /* Get chunk mapping table blocks and initialize zone mapping */
  1137. while (chunk < zmd->nr_chunks) {
  1138. if (!dmap_mblk) {
  1139. /* Get mapping block */
  1140. dmap_mblk = dmz_get_mblock(zmd, i + 1);
  1141. if (IS_ERR(dmap_mblk))
  1142. return PTR_ERR(dmap_mblk);
  1143. zmd->map_mblk[i] = dmap_mblk;
  1144. dmap = (struct dmz_map *) dmap_mblk->data;
  1145. i++;
  1146. e = 0;
  1147. }
  1148. /* Check data zone */
  1149. dzone_id = le32_to_cpu(dmap[e].dzone_id);
  1150. if (dzone_id == DMZ_MAP_UNMAPPED)
  1151. goto next;
  1152. if (dzone_id >= dev->nr_zones) {
  1153. dmz_dev_err(dev, "Chunk %u mapping: invalid data zone ID %u",
  1154. chunk, dzone_id);
  1155. return -EIO;
  1156. }
  1157. dzone = dmz_get(zmd, dzone_id);
  1158. set_bit(DMZ_DATA, &dzone->flags);
  1159. dzone->chunk = chunk;
  1160. dmz_get_zone_weight(zmd, dzone);
  1161. if (dmz_is_rnd(dzone))
  1162. list_add_tail(&dzone->link, &zmd->map_rnd_list);
  1163. else
  1164. list_add_tail(&dzone->link, &zmd->map_seq_list);
  1165. /* Check buffer zone */
  1166. bzone_id = le32_to_cpu(dmap[e].bzone_id);
  1167. if (bzone_id == DMZ_MAP_UNMAPPED)
  1168. goto next;
  1169. if (bzone_id >= dev->nr_zones) {
  1170. dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone ID %u",
  1171. chunk, bzone_id);
  1172. return -EIO;
  1173. }
  1174. bzone = dmz_get(zmd, bzone_id);
  1175. if (!dmz_is_rnd(bzone)) {
  1176. dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone %u",
  1177. chunk, bzone_id);
  1178. return -EIO;
  1179. }
  1180. set_bit(DMZ_DATA, &bzone->flags);
  1181. set_bit(DMZ_BUF, &bzone->flags);
  1182. bzone->chunk = chunk;
  1183. bzone->bzone = dzone;
  1184. dzone->bzone = bzone;
  1185. dmz_get_zone_weight(zmd, bzone);
  1186. list_add_tail(&bzone->link, &zmd->map_rnd_list);
  1187. next:
  1188. chunk++;
  1189. e++;
  1190. if (e >= DMZ_MAP_ENTRIES)
  1191. dmap_mblk = NULL;
  1192. }
  1193. /*
  1194. * At this point, only meta zones and mapped data zones were
  1195. * fully initialized. All remaining zones are unmapped data
  1196. * zones. Finish initializing those here.
  1197. */
  1198. for (i = 0; i < dev->nr_zones; i++) {
  1199. dzone = dmz_get(zmd, i);
  1200. if (dmz_is_meta(dzone))
  1201. continue;
  1202. if (dmz_is_rnd(dzone))
  1203. zmd->nr_rnd++;
  1204. else
  1205. zmd->nr_seq++;
  1206. if (dmz_is_data(dzone)) {
  1207. /* Already initialized */
  1208. continue;
  1209. }
  1210. /* Unmapped data zone */
  1211. set_bit(DMZ_DATA, &dzone->flags);
  1212. dzone->chunk = DMZ_MAP_UNMAPPED;
  1213. if (dmz_is_rnd(dzone)) {
  1214. list_add_tail(&dzone->link, &zmd->unmap_rnd_list);
  1215. atomic_inc(&zmd->unmap_nr_rnd);
  1216. } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
  1217. list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
  1218. atomic_inc(&zmd->nr_reserved_seq_zones);
  1219. zmd->nr_seq--;
  1220. } else {
  1221. list_add_tail(&dzone->link, &zmd->unmap_seq_list);
  1222. atomic_inc(&zmd->unmap_nr_seq);
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. /*
  1228. * Set a data chunk mapping.
  1229. */
  1230. static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
  1231. unsigned int dzone_id, unsigned int bzone_id)
  1232. {
  1233. struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
  1234. struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
  1235. int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
  1236. dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
  1237. dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
  1238. dmz_dirty_mblock(zmd, dmap_mblk);
  1239. }
  1240. /*
  1241. * The list of mapped zones is maintained in LRU order.
  1242. * This rotates a zone at the end of its map list.
  1243. */
  1244. static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1245. {
  1246. if (list_empty(&zone->link))
  1247. return;
  1248. list_del_init(&zone->link);
  1249. if (dmz_is_seq(zone)) {
  1250. /* LRU rotate sequential zone */
  1251. list_add_tail(&zone->link, &zmd->map_seq_list);
  1252. } else {
  1253. /* LRU rotate random zone */
  1254. list_add_tail(&zone->link, &zmd->map_rnd_list);
  1255. }
  1256. }
  1257. /*
  1258. * The list of mapped random zones is maintained
  1259. * in LRU order. This rotates a zone at the end of the list.
  1260. */
  1261. static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1262. {
  1263. __dmz_lru_zone(zmd, zone);
  1264. if (zone->bzone)
  1265. __dmz_lru_zone(zmd, zone->bzone);
  1266. }
  1267. /*
  1268. * Wait for any zone to be freed.
  1269. */
  1270. static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
  1271. {
  1272. DEFINE_WAIT(wait);
  1273. prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
  1274. dmz_unlock_map(zmd);
  1275. dmz_unlock_metadata(zmd);
  1276. io_schedule_timeout(HZ);
  1277. dmz_lock_metadata(zmd);
  1278. dmz_lock_map(zmd);
  1279. finish_wait(&zmd->free_wq, &wait);
  1280. }
  1281. /*
  1282. * Lock a zone for reclaim (set the zone RECLAIM bit).
  1283. * Returns false if the zone cannot be locked or if it is already locked
  1284. * and 1 otherwise.
  1285. */
  1286. int dmz_lock_zone_reclaim(struct dm_zone *zone)
  1287. {
  1288. /* Active zones cannot be reclaimed */
  1289. if (dmz_is_active(zone))
  1290. return 0;
  1291. return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
  1292. }
  1293. /*
  1294. * Clear a zone reclaim flag.
  1295. */
  1296. void dmz_unlock_zone_reclaim(struct dm_zone *zone)
  1297. {
  1298. WARN_ON(dmz_is_active(zone));
  1299. WARN_ON(!dmz_in_reclaim(zone));
  1300. clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
  1301. smp_mb__after_atomic();
  1302. wake_up_bit(&zone->flags, DMZ_RECLAIM);
  1303. }
  1304. /*
  1305. * Wait for a zone reclaim to complete.
  1306. */
  1307. static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
  1308. {
  1309. dmz_unlock_map(zmd);
  1310. dmz_unlock_metadata(zmd);
  1311. wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
  1312. dmz_lock_metadata(zmd);
  1313. dmz_lock_map(zmd);
  1314. }
  1315. /*
  1316. * Select a random write zone for reclaim.
  1317. */
  1318. static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd)
  1319. {
  1320. struct dm_zone *dzone = NULL;
  1321. struct dm_zone *zone;
  1322. if (list_empty(&zmd->map_rnd_list))
  1323. return ERR_PTR(-EBUSY);
  1324. list_for_each_entry(zone, &zmd->map_rnd_list, link) {
  1325. if (dmz_is_buf(zone))
  1326. dzone = zone->bzone;
  1327. else
  1328. dzone = zone;
  1329. if (dmz_lock_zone_reclaim(dzone))
  1330. return dzone;
  1331. }
  1332. return NULL;
  1333. }
  1334. /*
  1335. * Select a buffered sequential zone for reclaim.
  1336. */
  1337. static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd)
  1338. {
  1339. struct dm_zone *zone;
  1340. if (list_empty(&zmd->map_seq_list))
  1341. return ERR_PTR(-EBUSY);
  1342. list_for_each_entry(zone, &zmd->map_seq_list, link) {
  1343. if (!zone->bzone)
  1344. continue;
  1345. if (dmz_lock_zone_reclaim(zone))
  1346. return zone;
  1347. }
  1348. return NULL;
  1349. }
  1350. /*
  1351. * Select a zone for reclaim.
  1352. */
  1353. struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd)
  1354. {
  1355. struct dm_zone *zone;
  1356. /*
  1357. * Search for a zone candidate to reclaim: 2 cases are possible.
  1358. * (1) There is no free sequential zones. Then a random data zone
  1359. * cannot be reclaimed. So choose a sequential zone to reclaim so
  1360. * that afterward a random zone can be reclaimed.
  1361. * (2) At least one free sequential zone is available, then choose
  1362. * the oldest random zone (data or buffer) that can be locked.
  1363. */
  1364. dmz_lock_map(zmd);
  1365. if (list_empty(&zmd->reserved_seq_zones_list))
  1366. zone = dmz_get_seq_zone_for_reclaim(zmd);
  1367. else
  1368. zone = dmz_get_rnd_zone_for_reclaim(zmd);
  1369. dmz_unlock_map(zmd);
  1370. return zone;
  1371. }
  1372. /*
  1373. * Get the zone mapping a chunk, if the chunk is mapped already.
  1374. * If no mapping exist and the operation is WRITE, a zone is
  1375. * allocated and used to map the chunk.
  1376. * The zone returned will be set to the active state.
  1377. */
  1378. struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op)
  1379. {
  1380. struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
  1381. struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
  1382. int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
  1383. unsigned int dzone_id;
  1384. struct dm_zone *dzone = NULL;
  1385. int ret = 0;
  1386. dmz_lock_map(zmd);
  1387. again:
  1388. /* Get the chunk mapping */
  1389. dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
  1390. if (dzone_id == DMZ_MAP_UNMAPPED) {
  1391. /*
  1392. * Read or discard in unmapped chunks are fine. But for
  1393. * writes, we need a mapping, so get one.
  1394. */
  1395. if (op != REQ_OP_WRITE)
  1396. goto out;
  1397. /* Alloate a random zone */
  1398. dzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
  1399. if (!dzone) {
  1400. if (dmz_bdev_is_dying(zmd->dev)) {
  1401. dzone = ERR_PTR(-EIO);
  1402. goto out;
  1403. }
  1404. dmz_wait_for_free_zones(zmd);
  1405. goto again;
  1406. }
  1407. dmz_map_zone(zmd, dzone, chunk);
  1408. } else {
  1409. /* The chunk is already mapped: get the mapping zone */
  1410. dzone = dmz_get(zmd, dzone_id);
  1411. if (dzone->chunk != chunk) {
  1412. dzone = ERR_PTR(-EIO);
  1413. goto out;
  1414. }
  1415. /* Repair write pointer if the sequential dzone has error */
  1416. if (dmz_seq_write_err(dzone)) {
  1417. ret = dmz_handle_seq_write_err(zmd, dzone);
  1418. if (ret) {
  1419. dzone = ERR_PTR(-EIO);
  1420. goto out;
  1421. }
  1422. clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
  1423. }
  1424. }
  1425. /*
  1426. * If the zone is being reclaimed, the chunk mapping may change
  1427. * to a different zone. So wait for reclaim and retry. Otherwise,
  1428. * activate the zone (this will prevent reclaim from touching it).
  1429. */
  1430. if (dmz_in_reclaim(dzone)) {
  1431. dmz_wait_for_reclaim(zmd, dzone);
  1432. goto again;
  1433. }
  1434. dmz_activate_zone(dzone);
  1435. dmz_lru_zone(zmd, dzone);
  1436. out:
  1437. dmz_unlock_map(zmd);
  1438. return dzone;
  1439. }
  1440. /*
  1441. * Write and discard change the block validity of data zones and their buffer
  1442. * zones. Check here that valid blocks are still present. If all blocks are
  1443. * invalid, the zones can be unmapped on the fly without waiting for reclaim
  1444. * to do it.
  1445. */
  1446. void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
  1447. {
  1448. struct dm_zone *bzone;
  1449. dmz_lock_map(zmd);
  1450. bzone = dzone->bzone;
  1451. if (bzone) {
  1452. if (dmz_weight(bzone))
  1453. dmz_lru_zone(zmd, bzone);
  1454. else {
  1455. /* Empty buffer zone: reclaim it */
  1456. dmz_unmap_zone(zmd, bzone);
  1457. dmz_free_zone(zmd, bzone);
  1458. bzone = NULL;
  1459. }
  1460. }
  1461. /* Deactivate the data zone */
  1462. dmz_deactivate_zone(dzone);
  1463. if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
  1464. dmz_lru_zone(zmd, dzone);
  1465. else {
  1466. /* Unbuffered inactive empty data zone: reclaim it */
  1467. dmz_unmap_zone(zmd, dzone);
  1468. dmz_free_zone(zmd, dzone);
  1469. }
  1470. dmz_unlock_map(zmd);
  1471. }
  1472. /*
  1473. * Allocate and map a random zone to buffer a chunk
  1474. * already mapped to a sequential zone.
  1475. */
  1476. struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
  1477. struct dm_zone *dzone)
  1478. {
  1479. struct dm_zone *bzone;
  1480. dmz_lock_map(zmd);
  1481. again:
  1482. bzone = dzone->bzone;
  1483. if (bzone)
  1484. goto out;
  1485. /* Alloate a random zone */
  1486. bzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
  1487. if (!bzone) {
  1488. if (dmz_bdev_is_dying(zmd->dev)) {
  1489. bzone = ERR_PTR(-EIO);
  1490. goto out;
  1491. }
  1492. dmz_wait_for_free_zones(zmd);
  1493. goto again;
  1494. }
  1495. /* Update the chunk mapping */
  1496. dmz_set_chunk_mapping(zmd, dzone->chunk, dmz_id(zmd, dzone),
  1497. dmz_id(zmd, bzone));
  1498. set_bit(DMZ_BUF, &bzone->flags);
  1499. bzone->chunk = dzone->chunk;
  1500. bzone->bzone = dzone;
  1501. dzone->bzone = bzone;
  1502. list_add_tail(&bzone->link, &zmd->map_rnd_list);
  1503. out:
  1504. dmz_unlock_map(zmd);
  1505. return bzone;
  1506. }
  1507. /*
  1508. * Get an unmapped (free) zone.
  1509. * This must be called with the mapping lock held.
  1510. */
  1511. struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags)
  1512. {
  1513. struct list_head *list;
  1514. struct dm_zone *zone;
  1515. if (flags & DMZ_ALLOC_RND)
  1516. list = &zmd->unmap_rnd_list;
  1517. else
  1518. list = &zmd->unmap_seq_list;
  1519. again:
  1520. if (list_empty(list)) {
  1521. /*
  1522. * No free zone: if this is for reclaim, allow using the
  1523. * reserved sequential zones.
  1524. */
  1525. if (!(flags & DMZ_ALLOC_RECLAIM) ||
  1526. list_empty(&zmd->reserved_seq_zones_list))
  1527. return NULL;
  1528. zone = list_first_entry(&zmd->reserved_seq_zones_list,
  1529. struct dm_zone, link);
  1530. list_del_init(&zone->link);
  1531. atomic_dec(&zmd->nr_reserved_seq_zones);
  1532. return zone;
  1533. }
  1534. zone = list_first_entry(list, struct dm_zone, link);
  1535. list_del_init(&zone->link);
  1536. if (dmz_is_rnd(zone))
  1537. atomic_dec(&zmd->unmap_nr_rnd);
  1538. else
  1539. atomic_dec(&zmd->unmap_nr_seq);
  1540. if (dmz_is_offline(zone)) {
  1541. dmz_dev_warn(zmd->dev, "Zone %u is offline", dmz_id(zmd, zone));
  1542. zone = NULL;
  1543. goto again;
  1544. }
  1545. return zone;
  1546. }
  1547. /*
  1548. * Free a zone.
  1549. * This must be called with the mapping lock held.
  1550. */
  1551. void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1552. {
  1553. /* If this is a sequential zone, reset it */
  1554. if (dmz_is_seq(zone))
  1555. dmz_reset_zone(zmd, zone);
  1556. /* Return the zone to its type unmap list */
  1557. if (dmz_is_rnd(zone)) {
  1558. list_add_tail(&zone->link, &zmd->unmap_rnd_list);
  1559. atomic_inc(&zmd->unmap_nr_rnd);
  1560. } else if (atomic_read(&zmd->nr_reserved_seq_zones) <
  1561. zmd->nr_reserved_seq) {
  1562. list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
  1563. atomic_inc(&zmd->nr_reserved_seq_zones);
  1564. } else {
  1565. list_add_tail(&zone->link, &zmd->unmap_seq_list);
  1566. atomic_inc(&zmd->unmap_nr_seq);
  1567. }
  1568. wake_up_all(&zmd->free_wq);
  1569. }
  1570. /*
  1571. * Map a chunk to a zone.
  1572. * This must be called with the mapping lock held.
  1573. */
  1574. void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
  1575. unsigned int chunk)
  1576. {
  1577. /* Set the chunk mapping */
  1578. dmz_set_chunk_mapping(zmd, chunk, dmz_id(zmd, dzone),
  1579. DMZ_MAP_UNMAPPED);
  1580. dzone->chunk = chunk;
  1581. if (dmz_is_rnd(dzone))
  1582. list_add_tail(&dzone->link, &zmd->map_rnd_list);
  1583. else
  1584. list_add_tail(&dzone->link, &zmd->map_seq_list);
  1585. }
  1586. /*
  1587. * Unmap a zone.
  1588. * This must be called with the mapping lock held.
  1589. */
  1590. void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
  1591. {
  1592. unsigned int chunk = zone->chunk;
  1593. unsigned int dzone_id;
  1594. if (chunk == DMZ_MAP_UNMAPPED) {
  1595. /* Already unmapped */
  1596. return;
  1597. }
  1598. if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
  1599. /*
  1600. * Unmapping the chunk buffer zone: clear only
  1601. * the chunk buffer mapping
  1602. */
  1603. dzone_id = dmz_id(zmd, zone->bzone);
  1604. zone->bzone->bzone = NULL;
  1605. zone->bzone = NULL;
  1606. } else {
  1607. /*
  1608. * Unmapping the chunk data zone: the zone must
  1609. * not be buffered.
  1610. */
  1611. if (WARN_ON(zone->bzone)) {
  1612. zone->bzone->bzone = NULL;
  1613. zone->bzone = NULL;
  1614. }
  1615. dzone_id = DMZ_MAP_UNMAPPED;
  1616. }
  1617. dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
  1618. zone->chunk = DMZ_MAP_UNMAPPED;
  1619. list_del_init(&zone->link);
  1620. }
  1621. /*
  1622. * Set @nr_bits bits in @bitmap starting from @bit.
  1623. * Return the number of bits changed from 0 to 1.
  1624. */
  1625. static unsigned int dmz_set_bits(unsigned long *bitmap,
  1626. unsigned int bit, unsigned int nr_bits)
  1627. {
  1628. unsigned long *addr;
  1629. unsigned int end = bit + nr_bits;
  1630. unsigned int n = 0;
  1631. while (bit < end) {
  1632. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1633. ((end - bit) >= BITS_PER_LONG)) {
  1634. /* Try to set the whole word at once */
  1635. addr = bitmap + BIT_WORD(bit);
  1636. if (*addr == 0) {
  1637. *addr = ULONG_MAX;
  1638. n += BITS_PER_LONG;
  1639. bit += BITS_PER_LONG;
  1640. continue;
  1641. }
  1642. }
  1643. if (!test_and_set_bit(bit, bitmap))
  1644. n++;
  1645. bit++;
  1646. }
  1647. return n;
  1648. }
  1649. /*
  1650. * Get the bitmap block storing the bit for chunk_block in zone.
  1651. */
  1652. static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
  1653. struct dm_zone *zone,
  1654. sector_t chunk_block)
  1655. {
  1656. sector_t bitmap_block = 1 + zmd->nr_map_blocks +
  1657. (sector_t)(dmz_id(zmd, zone) * zmd->zone_nr_bitmap_blocks) +
  1658. (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
  1659. return dmz_get_mblock(zmd, bitmap_block);
  1660. }
  1661. /*
  1662. * Copy the valid blocks bitmap of from_zone to the bitmap of to_zone.
  1663. */
  1664. int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  1665. struct dm_zone *to_zone)
  1666. {
  1667. struct dmz_mblock *from_mblk, *to_mblk;
  1668. sector_t chunk_block = 0;
  1669. /* Get the zones bitmap blocks */
  1670. while (chunk_block < zmd->dev->zone_nr_blocks) {
  1671. from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
  1672. if (IS_ERR(from_mblk))
  1673. return PTR_ERR(from_mblk);
  1674. to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
  1675. if (IS_ERR(to_mblk)) {
  1676. dmz_release_mblock(zmd, from_mblk);
  1677. return PTR_ERR(to_mblk);
  1678. }
  1679. memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
  1680. dmz_dirty_mblock(zmd, to_mblk);
  1681. dmz_release_mblock(zmd, to_mblk);
  1682. dmz_release_mblock(zmd, from_mblk);
  1683. chunk_block += zmd->zone_bits_per_mblk;
  1684. }
  1685. to_zone->weight = from_zone->weight;
  1686. return 0;
  1687. }
  1688. /*
  1689. * Merge the valid blocks bitmap of from_zone into the bitmap of to_zone,
  1690. * starting from chunk_block.
  1691. */
  1692. int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  1693. struct dm_zone *to_zone, sector_t chunk_block)
  1694. {
  1695. unsigned int nr_blocks;
  1696. int ret;
  1697. /* Get the zones bitmap blocks */
  1698. while (chunk_block < zmd->dev->zone_nr_blocks) {
  1699. /* Get a valid region from the source zone */
  1700. ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
  1701. if (ret <= 0)
  1702. return ret;
  1703. nr_blocks = ret;
  1704. ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
  1705. if (ret)
  1706. return ret;
  1707. chunk_block += nr_blocks;
  1708. }
  1709. return 0;
  1710. }
  1711. /*
  1712. * Validate all the blocks in the range [block..block+nr_blocks-1].
  1713. */
  1714. int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  1715. sector_t chunk_block, unsigned int nr_blocks)
  1716. {
  1717. unsigned int count, bit, nr_bits;
  1718. unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
  1719. struct dmz_mblock *mblk;
  1720. unsigned int n = 0;
  1721. dmz_dev_debug(zmd->dev, "=> VALIDATE zone %u, block %llu, %u blocks",
  1722. dmz_id(zmd, zone), (unsigned long long)chunk_block,
  1723. nr_blocks);
  1724. WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
  1725. while (nr_blocks) {
  1726. /* Get bitmap block */
  1727. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1728. if (IS_ERR(mblk))
  1729. return PTR_ERR(mblk);
  1730. /* Set bits */
  1731. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1732. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1733. count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
  1734. if (count) {
  1735. dmz_dirty_mblock(zmd, mblk);
  1736. n += count;
  1737. }
  1738. dmz_release_mblock(zmd, mblk);
  1739. nr_blocks -= nr_bits;
  1740. chunk_block += nr_bits;
  1741. }
  1742. if (likely(zone->weight + n <= zone_nr_blocks))
  1743. zone->weight += n;
  1744. else {
  1745. dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be <= %u",
  1746. dmz_id(zmd, zone), zone->weight,
  1747. zone_nr_blocks - n);
  1748. zone->weight = zone_nr_blocks;
  1749. }
  1750. return 0;
  1751. }
  1752. /*
  1753. * Clear nr_bits bits in bitmap starting from bit.
  1754. * Return the number of bits cleared.
  1755. */
  1756. static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
  1757. {
  1758. unsigned long *addr;
  1759. int end = bit + nr_bits;
  1760. int n = 0;
  1761. while (bit < end) {
  1762. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1763. ((end - bit) >= BITS_PER_LONG)) {
  1764. /* Try to clear whole word at once */
  1765. addr = bitmap + BIT_WORD(bit);
  1766. if (*addr == ULONG_MAX) {
  1767. *addr = 0;
  1768. n += BITS_PER_LONG;
  1769. bit += BITS_PER_LONG;
  1770. continue;
  1771. }
  1772. }
  1773. if (test_and_clear_bit(bit, bitmap))
  1774. n++;
  1775. bit++;
  1776. }
  1777. return n;
  1778. }
  1779. /*
  1780. * Invalidate all the blocks in the range [block..block+nr_blocks-1].
  1781. */
  1782. int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  1783. sector_t chunk_block, unsigned int nr_blocks)
  1784. {
  1785. unsigned int count, bit, nr_bits;
  1786. struct dmz_mblock *mblk;
  1787. unsigned int n = 0;
  1788. dmz_dev_debug(zmd->dev, "=> INVALIDATE zone %u, block %llu, %u blocks",
  1789. dmz_id(zmd, zone), (u64)chunk_block, nr_blocks);
  1790. WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
  1791. while (nr_blocks) {
  1792. /* Get bitmap block */
  1793. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1794. if (IS_ERR(mblk))
  1795. return PTR_ERR(mblk);
  1796. /* Clear bits */
  1797. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1798. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1799. count = dmz_clear_bits((unsigned long *)mblk->data,
  1800. bit, nr_bits);
  1801. if (count) {
  1802. dmz_dirty_mblock(zmd, mblk);
  1803. n += count;
  1804. }
  1805. dmz_release_mblock(zmd, mblk);
  1806. nr_blocks -= nr_bits;
  1807. chunk_block += nr_bits;
  1808. }
  1809. if (zone->weight >= n)
  1810. zone->weight -= n;
  1811. else {
  1812. dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be >= %u",
  1813. dmz_id(zmd, zone), zone->weight, n);
  1814. zone->weight = 0;
  1815. }
  1816. return 0;
  1817. }
  1818. /*
  1819. * Get a block bit value.
  1820. */
  1821. static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1822. sector_t chunk_block)
  1823. {
  1824. struct dmz_mblock *mblk;
  1825. int ret;
  1826. WARN_ON(chunk_block >= zmd->dev->zone_nr_blocks);
  1827. /* Get bitmap block */
  1828. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1829. if (IS_ERR(mblk))
  1830. return PTR_ERR(mblk);
  1831. /* Get offset */
  1832. ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
  1833. (unsigned long *) mblk->data) != 0;
  1834. dmz_release_mblock(zmd, mblk);
  1835. return ret;
  1836. }
  1837. /*
  1838. * Return the number of blocks from chunk_block to the first block with a bit
  1839. * value specified by set. Search at most nr_blocks blocks from chunk_block.
  1840. */
  1841. static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1842. sector_t chunk_block, unsigned int nr_blocks,
  1843. int set)
  1844. {
  1845. struct dmz_mblock *mblk;
  1846. unsigned int bit, set_bit, nr_bits;
  1847. unsigned int zone_bits = zmd->zone_bits_per_mblk;
  1848. unsigned long *bitmap;
  1849. int n = 0;
  1850. WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
  1851. while (nr_blocks) {
  1852. /* Get bitmap block */
  1853. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1854. if (IS_ERR(mblk))
  1855. return PTR_ERR(mblk);
  1856. /* Get offset */
  1857. bitmap = (unsigned long *) mblk->data;
  1858. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1859. nr_bits = min(nr_blocks, zone_bits - bit);
  1860. if (set)
  1861. set_bit = find_next_bit(bitmap, zone_bits, bit);
  1862. else
  1863. set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
  1864. dmz_release_mblock(zmd, mblk);
  1865. n += set_bit - bit;
  1866. if (set_bit < zone_bits)
  1867. break;
  1868. nr_blocks -= nr_bits;
  1869. chunk_block += nr_bits;
  1870. }
  1871. return n;
  1872. }
  1873. /*
  1874. * Test if chunk_block is valid. If it is, the number of consecutive
  1875. * valid blocks from chunk_block will be returned.
  1876. */
  1877. int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
  1878. sector_t chunk_block)
  1879. {
  1880. int valid;
  1881. valid = dmz_test_block(zmd, zone, chunk_block);
  1882. if (valid <= 0)
  1883. return valid;
  1884. /* The block is valid: get the number of valid blocks from block */
  1885. return dmz_to_next_set_block(zmd, zone, chunk_block,
  1886. zmd->dev->zone_nr_blocks - chunk_block, 0);
  1887. }
  1888. /*
  1889. * Find the first valid block from @chunk_block in @zone.
  1890. * If such a block is found, its number is returned using
  1891. * @chunk_block and the total number of valid blocks from @chunk_block
  1892. * is returned.
  1893. */
  1894. int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  1895. sector_t *chunk_block)
  1896. {
  1897. sector_t start_block = *chunk_block;
  1898. int ret;
  1899. ret = dmz_to_next_set_block(zmd, zone, start_block,
  1900. zmd->dev->zone_nr_blocks - start_block, 1);
  1901. if (ret < 0)
  1902. return ret;
  1903. start_block += ret;
  1904. *chunk_block = start_block;
  1905. return dmz_to_next_set_block(zmd, zone, start_block,
  1906. zmd->dev->zone_nr_blocks - start_block, 0);
  1907. }
  1908. /*
  1909. * Count the number of bits set starting from bit up to bit + nr_bits - 1.
  1910. */
  1911. static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
  1912. {
  1913. unsigned long *addr;
  1914. int end = bit + nr_bits;
  1915. int n = 0;
  1916. while (bit < end) {
  1917. if (((bit & (BITS_PER_LONG - 1)) == 0) &&
  1918. ((end - bit) >= BITS_PER_LONG)) {
  1919. addr = (unsigned long *)bitmap + BIT_WORD(bit);
  1920. if (*addr == ULONG_MAX) {
  1921. n += BITS_PER_LONG;
  1922. bit += BITS_PER_LONG;
  1923. continue;
  1924. }
  1925. }
  1926. if (test_bit(bit, bitmap))
  1927. n++;
  1928. bit++;
  1929. }
  1930. return n;
  1931. }
  1932. /*
  1933. * Get a zone weight.
  1934. */
  1935. static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
  1936. {
  1937. struct dmz_mblock *mblk;
  1938. sector_t chunk_block = 0;
  1939. unsigned int bit, nr_bits;
  1940. unsigned int nr_blocks = zmd->dev->zone_nr_blocks;
  1941. void *bitmap;
  1942. int n = 0;
  1943. while (nr_blocks) {
  1944. /* Get bitmap block */
  1945. mblk = dmz_get_bitmap(zmd, zone, chunk_block);
  1946. if (IS_ERR(mblk)) {
  1947. n = 0;
  1948. break;
  1949. }
  1950. /* Count bits in this block */
  1951. bitmap = mblk->data;
  1952. bit = chunk_block & DMZ_BLOCK_MASK_BITS;
  1953. nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
  1954. n += dmz_count_bits(bitmap, bit, nr_bits);
  1955. dmz_release_mblock(zmd, mblk);
  1956. nr_blocks -= nr_bits;
  1957. chunk_block += nr_bits;
  1958. }
  1959. zone->weight = n;
  1960. }
  1961. /*
  1962. * Cleanup the zoned metadata resources.
  1963. */
  1964. static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
  1965. {
  1966. struct rb_root *root;
  1967. struct dmz_mblock *mblk, *next;
  1968. int i;
  1969. /* Release zone mapping resources */
  1970. if (zmd->map_mblk) {
  1971. for (i = 0; i < zmd->nr_map_blocks; i++)
  1972. dmz_release_mblock(zmd, zmd->map_mblk[i]);
  1973. kfree(zmd->map_mblk);
  1974. zmd->map_mblk = NULL;
  1975. }
  1976. /* Release super blocks */
  1977. for (i = 0; i < 2; i++) {
  1978. if (zmd->sb[i].mblk) {
  1979. dmz_free_mblock(zmd, zmd->sb[i].mblk);
  1980. zmd->sb[i].mblk = NULL;
  1981. }
  1982. }
  1983. /* Free cached blocks */
  1984. while (!list_empty(&zmd->mblk_dirty_list)) {
  1985. mblk = list_first_entry(&zmd->mblk_dirty_list,
  1986. struct dmz_mblock, link);
  1987. dmz_dev_warn(zmd->dev, "mblock %llu still in dirty list (ref %u)",
  1988. (u64)mblk->no, mblk->ref);
  1989. list_del_init(&mblk->link);
  1990. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  1991. dmz_free_mblock(zmd, mblk);
  1992. }
  1993. while (!list_empty(&zmd->mblk_lru_list)) {
  1994. mblk = list_first_entry(&zmd->mblk_lru_list,
  1995. struct dmz_mblock, link);
  1996. list_del_init(&mblk->link);
  1997. rb_erase(&mblk->node, &zmd->mblk_rbtree);
  1998. dmz_free_mblock(zmd, mblk);
  1999. }
  2000. /* Sanity checks: the mblock rbtree should now be empty */
  2001. root = &zmd->mblk_rbtree;
  2002. rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
  2003. dmz_dev_warn(zmd->dev, "mblock %llu ref %u still in rbtree",
  2004. (u64)mblk->no, mblk->ref);
  2005. mblk->ref = 0;
  2006. dmz_free_mblock(zmd, mblk);
  2007. }
  2008. /* Free the zone descriptors */
  2009. dmz_drop_zones(zmd);
  2010. }
  2011. /*
  2012. * Initialize the zoned metadata.
  2013. */
  2014. int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **metadata)
  2015. {
  2016. struct dmz_metadata *zmd;
  2017. unsigned int i, zid;
  2018. struct dm_zone *zone;
  2019. int ret;
  2020. zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
  2021. if (!zmd)
  2022. return -ENOMEM;
  2023. zmd->dev = dev;
  2024. zmd->mblk_rbtree = RB_ROOT;
  2025. init_rwsem(&zmd->mblk_sem);
  2026. mutex_init(&zmd->mblk_flush_lock);
  2027. spin_lock_init(&zmd->mblk_lock);
  2028. INIT_LIST_HEAD(&zmd->mblk_lru_list);
  2029. INIT_LIST_HEAD(&zmd->mblk_dirty_list);
  2030. mutex_init(&zmd->map_lock);
  2031. atomic_set(&zmd->unmap_nr_rnd, 0);
  2032. INIT_LIST_HEAD(&zmd->unmap_rnd_list);
  2033. INIT_LIST_HEAD(&zmd->map_rnd_list);
  2034. atomic_set(&zmd->unmap_nr_seq, 0);
  2035. INIT_LIST_HEAD(&zmd->unmap_seq_list);
  2036. INIT_LIST_HEAD(&zmd->map_seq_list);
  2037. atomic_set(&zmd->nr_reserved_seq_zones, 0);
  2038. INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
  2039. init_waitqueue_head(&zmd->free_wq);
  2040. /* Initialize zone descriptors */
  2041. ret = dmz_init_zones(zmd);
  2042. if (ret)
  2043. goto err;
  2044. /* Get super block */
  2045. ret = dmz_load_sb(zmd);
  2046. if (ret)
  2047. goto err;
  2048. /* Set metadata zones starting from sb_zone */
  2049. zid = dmz_id(zmd, zmd->sb_zone);
  2050. for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
  2051. zone = dmz_get(zmd, zid + i);
  2052. if (!dmz_is_rnd(zone))
  2053. goto err;
  2054. set_bit(DMZ_META, &zone->flags);
  2055. }
  2056. /* Load mapping table */
  2057. ret = dmz_load_mapping(zmd);
  2058. if (ret)
  2059. goto err;
  2060. /*
  2061. * Cache size boundaries: allow at least 2 super blocks, the chunk map
  2062. * blocks and enough blocks to be able to cache the bitmap blocks of
  2063. * up to 16 zones when idle (min_nr_mblks). Otherwise, if busy, allow
  2064. * the cache to add 512 more metadata blocks.
  2065. */
  2066. zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
  2067. zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
  2068. zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
  2069. zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
  2070. zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
  2071. /* Metadata cache shrinker */
  2072. ret = register_shrinker(&zmd->mblk_shrinker);
  2073. if (ret) {
  2074. dmz_dev_err(dev, "Register metadata cache shrinker failed");
  2075. goto err;
  2076. }
  2077. dmz_dev_info(dev, "Host-%s zoned block device",
  2078. bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
  2079. "aware" : "managed");
  2080. dmz_dev_info(dev, " %llu 512-byte logical sectors",
  2081. (u64)dev->capacity);
  2082. dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
  2083. dev->nr_zones, (u64)dev->zone_nr_sectors);
  2084. dmz_dev_info(dev, " %u metadata zones",
  2085. zmd->nr_meta_zones * 2);
  2086. dmz_dev_info(dev, " %u data zones for %u chunks",
  2087. zmd->nr_data_zones, zmd->nr_chunks);
  2088. dmz_dev_info(dev, " %u random zones (%u unmapped)",
  2089. zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd));
  2090. dmz_dev_info(dev, " %u sequential zones (%u unmapped)",
  2091. zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq));
  2092. dmz_dev_info(dev, " %u reserved sequential data zones",
  2093. zmd->nr_reserved_seq);
  2094. dmz_dev_debug(dev, "Format:");
  2095. dmz_dev_debug(dev, "%u metadata blocks per set (%u max cache)",
  2096. zmd->nr_meta_blocks, zmd->max_nr_mblks);
  2097. dmz_dev_debug(dev, " %u data zone mapping blocks",
  2098. zmd->nr_map_blocks);
  2099. dmz_dev_debug(dev, " %u bitmap blocks",
  2100. zmd->nr_bitmap_blocks);
  2101. *metadata = zmd;
  2102. return 0;
  2103. err:
  2104. dmz_cleanup_metadata(zmd);
  2105. kfree(zmd);
  2106. *metadata = NULL;
  2107. return ret;
  2108. }
  2109. /*
  2110. * Cleanup the zoned metadata resources.
  2111. */
  2112. void dmz_dtr_metadata(struct dmz_metadata *zmd)
  2113. {
  2114. unregister_shrinker(&zmd->mblk_shrinker);
  2115. dmz_cleanup_metadata(zmd);
  2116. kfree(zmd);
  2117. }
  2118. /*
  2119. * Check zone information on resume.
  2120. */
  2121. int dmz_resume_metadata(struct dmz_metadata *zmd)
  2122. {
  2123. struct dmz_dev *dev = zmd->dev;
  2124. struct dm_zone *zone;
  2125. sector_t wp_block;
  2126. unsigned int i;
  2127. int ret;
  2128. /* Check zones */
  2129. for (i = 0; i < dev->nr_zones; i++) {
  2130. zone = dmz_get(zmd, i);
  2131. if (!zone) {
  2132. dmz_dev_err(dev, "Unable to get zone %u", i);
  2133. return -EIO;
  2134. }
  2135. wp_block = zone->wp_block;
  2136. ret = dmz_update_zone(zmd, zone);
  2137. if (ret) {
  2138. dmz_dev_err(dev, "Broken zone %u", i);
  2139. return ret;
  2140. }
  2141. if (dmz_is_offline(zone)) {
  2142. dmz_dev_warn(dev, "Zone %u is offline", i);
  2143. continue;
  2144. }
  2145. /* Check write pointer */
  2146. if (!dmz_is_seq(zone))
  2147. zone->wp_block = 0;
  2148. else if (zone->wp_block != wp_block) {
  2149. dmz_dev_err(dev, "Zone %u: Invalid wp (%llu / %llu)",
  2150. i, (u64)zone->wp_block, (u64)wp_block);
  2151. zone->wp_block = wp_block;
  2152. dmz_invalidate_blocks(zmd, zone, zone->wp_block,
  2153. dev->zone_nr_blocks - zone->wp_block);
  2154. }
  2155. }
  2156. return 0;
  2157. }