dm.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-core.h"
  8. #include "dm-rq.h"
  9. #include "dm-uevent.h"
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/blkpg.h>
  15. #include <linux/bio.h>
  16. #include <linux/mempool.h>
  17. #include <linux/slab.h>
  18. #include <linux/idr.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include <linux/pr.h>
  23. #define DM_MSG_PREFIX "core"
  24. #ifdef CONFIG_PRINTK
  25. /*
  26. * ratelimit state to be used in DMXXX_LIMIT().
  27. */
  28. DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
  29. DEFAULT_RATELIMIT_INTERVAL,
  30. DEFAULT_RATELIMIT_BURST);
  31. EXPORT_SYMBOL(dm_ratelimit_state);
  32. #endif
  33. /*
  34. * Cookies are numeric values sent with CHANGE and REMOVE
  35. * uevents while resuming, removing or renaming the device.
  36. */
  37. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  38. #define DM_COOKIE_LENGTH 24
  39. static const char *_name = DM_NAME;
  40. static unsigned int major = 0;
  41. static unsigned int _major = 0;
  42. static DEFINE_IDR(_minor_idr);
  43. static DEFINE_SPINLOCK(_minor_lock);
  44. static void do_deferred_remove(struct work_struct *w);
  45. static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
  46. static struct workqueue_struct *deferred_remove_workqueue;
  47. /*
  48. * One of these is allocated per bio.
  49. */
  50. struct dm_io {
  51. struct mapped_device *md;
  52. int error;
  53. atomic_t io_count;
  54. struct bio *bio;
  55. unsigned long start_time;
  56. spinlock_t endio_lock;
  57. struct dm_stats_aux stats_aux;
  58. };
  59. #define MINOR_ALLOCED ((void *)-1)
  60. /*
  61. * Bits for the md->flags field.
  62. */
  63. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  64. #define DMF_SUSPENDED 1
  65. #define DMF_FROZEN 2
  66. #define DMF_FREEING 3
  67. #define DMF_DELETING 4
  68. #define DMF_NOFLUSH_SUSPENDING 5
  69. #define DMF_DEFERRED_REMOVE 6
  70. #define DMF_SUSPENDED_INTERNALLY 7
  71. #define DM_NUMA_NODE NUMA_NO_NODE
  72. static int dm_numa_node = DM_NUMA_NODE;
  73. /*
  74. * For mempools pre-allocation at the table loading time.
  75. */
  76. struct dm_md_mempools {
  77. mempool_t *io_pool;
  78. struct bio_set *bs;
  79. };
  80. struct table_device {
  81. struct list_head list;
  82. atomic_t count;
  83. struct dm_dev dm_dev;
  84. };
  85. static struct kmem_cache *_io_cache;
  86. static struct kmem_cache *_rq_tio_cache;
  87. static struct kmem_cache *_rq_cache;
  88. /*
  89. * Bio-based DM's mempools' reserved IOs set by the user.
  90. */
  91. #define RESERVED_BIO_BASED_IOS 16
  92. static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
  93. static int __dm_get_module_param_int(int *module_param, int min, int max)
  94. {
  95. int param = ACCESS_ONCE(*module_param);
  96. int modified_param = 0;
  97. bool modified = true;
  98. if (param < min)
  99. modified_param = min;
  100. else if (param > max)
  101. modified_param = max;
  102. else
  103. modified = false;
  104. if (modified) {
  105. (void)cmpxchg(module_param, param, modified_param);
  106. param = modified_param;
  107. }
  108. return param;
  109. }
  110. unsigned __dm_get_module_param(unsigned *module_param,
  111. unsigned def, unsigned max)
  112. {
  113. unsigned param = ACCESS_ONCE(*module_param);
  114. unsigned modified_param = 0;
  115. if (!param)
  116. modified_param = def;
  117. else if (param > max)
  118. modified_param = max;
  119. if (modified_param) {
  120. (void)cmpxchg(module_param, param, modified_param);
  121. param = modified_param;
  122. }
  123. return param;
  124. }
  125. unsigned dm_get_reserved_bio_based_ios(void)
  126. {
  127. return __dm_get_module_param(&reserved_bio_based_ios,
  128. RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
  129. }
  130. EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
  131. static unsigned dm_get_numa_node(void)
  132. {
  133. return __dm_get_module_param_int(&dm_numa_node,
  134. DM_NUMA_NODE, num_online_nodes() - 1);
  135. }
  136. static int __init local_init(void)
  137. {
  138. int r = -ENOMEM;
  139. /* allocate a slab for the dm_ios */
  140. _io_cache = KMEM_CACHE(dm_io, 0);
  141. if (!_io_cache)
  142. return r;
  143. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  144. if (!_rq_tio_cache)
  145. goto out_free_io_cache;
  146. _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
  147. __alignof__(struct request), 0, NULL);
  148. if (!_rq_cache)
  149. goto out_free_rq_tio_cache;
  150. r = dm_uevent_init();
  151. if (r)
  152. goto out_free_rq_cache;
  153. deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
  154. if (!deferred_remove_workqueue) {
  155. r = -ENOMEM;
  156. goto out_uevent_exit;
  157. }
  158. _major = major;
  159. r = register_blkdev(_major, _name);
  160. if (r < 0)
  161. goto out_free_workqueue;
  162. if (!_major)
  163. _major = r;
  164. return 0;
  165. out_free_workqueue:
  166. destroy_workqueue(deferred_remove_workqueue);
  167. out_uevent_exit:
  168. dm_uevent_exit();
  169. out_free_rq_cache:
  170. kmem_cache_destroy(_rq_cache);
  171. out_free_rq_tio_cache:
  172. kmem_cache_destroy(_rq_tio_cache);
  173. out_free_io_cache:
  174. kmem_cache_destroy(_io_cache);
  175. return r;
  176. }
  177. static void local_exit(void)
  178. {
  179. flush_scheduled_work();
  180. destroy_workqueue(deferred_remove_workqueue);
  181. kmem_cache_destroy(_rq_cache);
  182. kmem_cache_destroy(_rq_tio_cache);
  183. kmem_cache_destroy(_io_cache);
  184. unregister_blkdev(_major, _name);
  185. dm_uevent_exit();
  186. _major = 0;
  187. DMINFO("cleaned up");
  188. }
  189. static int (*_inits[])(void) __initdata = {
  190. local_init,
  191. dm_target_init,
  192. dm_linear_init,
  193. dm_stripe_init,
  194. dm_io_init,
  195. dm_kcopyd_init,
  196. dm_interface_init,
  197. dm_statistics_init,
  198. };
  199. static void (*_exits[])(void) = {
  200. local_exit,
  201. dm_target_exit,
  202. dm_linear_exit,
  203. dm_stripe_exit,
  204. dm_io_exit,
  205. dm_kcopyd_exit,
  206. dm_interface_exit,
  207. dm_statistics_exit,
  208. };
  209. static int __init dm_init(void)
  210. {
  211. const int count = ARRAY_SIZE(_inits);
  212. int r, i;
  213. for (i = 0; i < count; i++) {
  214. r = _inits[i]();
  215. if (r)
  216. goto bad;
  217. }
  218. return 0;
  219. bad:
  220. while (i--)
  221. _exits[i]();
  222. return r;
  223. }
  224. static void __exit dm_exit(void)
  225. {
  226. int i = ARRAY_SIZE(_exits);
  227. while (i--)
  228. _exits[i]();
  229. /*
  230. * Should be empty by this point.
  231. */
  232. idr_destroy(&_minor_idr);
  233. }
  234. /*
  235. * Block device functions
  236. */
  237. int dm_deleting_md(struct mapped_device *md)
  238. {
  239. return test_bit(DMF_DELETING, &md->flags);
  240. }
  241. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  242. {
  243. struct mapped_device *md;
  244. spin_lock(&_minor_lock);
  245. md = bdev->bd_disk->private_data;
  246. if (!md)
  247. goto out;
  248. if (test_bit(DMF_FREEING, &md->flags) ||
  249. dm_deleting_md(md)) {
  250. md = NULL;
  251. goto out;
  252. }
  253. dm_get(md);
  254. atomic_inc(&md->open_count);
  255. out:
  256. spin_unlock(&_minor_lock);
  257. return md ? 0 : -ENXIO;
  258. }
  259. static void dm_blk_close(struct gendisk *disk, fmode_t mode)
  260. {
  261. struct mapped_device *md;
  262. spin_lock(&_minor_lock);
  263. md = disk->private_data;
  264. if (WARN_ON(!md))
  265. goto out;
  266. if (atomic_dec_and_test(&md->open_count) &&
  267. (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
  268. queue_work(deferred_remove_workqueue, &deferred_remove_work);
  269. dm_put(md);
  270. out:
  271. spin_unlock(&_minor_lock);
  272. }
  273. int dm_open_count(struct mapped_device *md)
  274. {
  275. return atomic_read(&md->open_count);
  276. }
  277. /*
  278. * Guarantees nothing is using the device before it's deleted.
  279. */
  280. int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
  281. {
  282. int r = 0;
  283. spin_lock(&_minor_lock);
  284. if (dm_open_count(md)) {
  285. r = -EBUSY;
  286. if (mark_deferred)
  287. set_bit(DMF_DEFERRED_REMOVE, &md->flags);
  288. } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
  289. r = -EEXIST;
  290. else
  291. set_bit(DMF_DELETING, &md->flags);
  292. spin_unlock(&_minor_lock);
  293. return r;
  294. }
  295. int dm_cancel_deferred_remove(struct mapped_device *md)
  296. {
  297. int r = 0;
  298. spin_lock(&_minor_lock);
  299. if (test_bit(DMF_DELETING, &md->flags))
  300. r = -EBUSY;
  301. else
  302. clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
  303. spin_unlock(&_minor_lock);
  304. return r;
  305. }
  306. static void do_deferred_remove(struct work_struct *w)
  307. {
  308. dm_deferred_remove();
  309. }
  310. sector_t dm_get_size(struct mapped_device *md)
  311. {
  312. return get_capacity(md->disk);
  313. }
  314. struct request_queue *dm_get_md_queue(struct mapped_device *md)
  315. {
  316. return md->queue;
  317. }
  318. struct dm_stats *dm_get_stats(struct mapped_device *md)
  319. {
  320. return &md->stats;
  321. }
  322. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  323. {
  324. struct mapped_device *md = bdev->bd_disk->private_data;
  325. return dm_get_geometry(md, geo);
  326. }
  327. static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
  328. struct block_device **bdev,
  329. fmode_t *mode)
  330. {
  331. struct dm_target *tgt;
  332. struct dm_table *map;
  333. int srcu_idx, r;
  334. retry:
  335. r = -ENOTTY;
  336. map = dm_get_live_table(md, &srcu_idx);
  337. if (!map || !dm_table_get_size(map))
  338. goto out;
  339. /* We only support devices that have a single target */
  340. if (dm_table_get_num_targets(map) != 1)
  341. goto out;
  342. tgt = dm_table_get_target(map, 0);
  343. if (!tgt->type->prepare_ioctl)
  344. goto out;
  345. if (dm_suspended_md(md)) {
  346. r = -EAGAIN;
  347. goto out;
  348. }
  349. r = tgt->type->prepare_ioctl(tgt, bdev, mode);
  350. if (r < 0)
  351. goto out;
  352. bdgrab(*bdev);
  353. dm_put_live_table(md, srcu_idx);
  354. return r;
  355. out:
  356. dm_put_live_table(md, srcu_idx);
  357. if (r == -ENOTCONN && !fatal_signal_pending(current)) {
  358. msleep(10);
  359. goto retry;
  360. }
  361. return r;
  362. }
  363. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  364. unsigned int cmd, unsigned long arg)
  365. {
  366. struct mapped_device *md = bdev->bd_disk->private_data;
  367. int r;
  368. r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
  369. if (r < 0)
  370. return r;
  371. if (r > 0) {
  372. /*
  373. * Target determined this ioctl is being issued against a
  374. * subset of the parent bdev; require extra privileges.
  375. */
  376. if (!capable(CAP_SYS_RAWIO)) {
  377. DMWARN_LIMIT(
  378. "%s: sending ioctl %x to DM device without required privilege.",
  379. current->comm, cmd);
  380. r = -ENOIOCTLCMD;
  381. goto out;
  382. }
  383. }
  384. r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  385. out:
  386. bdput(bdev);
  387. return r;
  388. }
  389. static struct dm_io *alloc_io(struct mapped_device *md)
  390. {
  391. return mempool_alloc(md->io_pool, GFP_NOIO);
  392. }
  393. static void free_io(struct mapped_device *md, struct dm_io *io)
  394. {
  395. mempool_free(io, md->io_pool);
  396. }
  397. static void free_tio(struct dm_target_io *tio)
  398. {
  399. bio_put(&tio->clone);
  400. }
  401. int md_in_flight(struct mapped_device *md)
  402. {
  403. return atomic_read(&md->pending[READ]) +
  404. atomic_read(&md->pending[WRITE]);
  405. }
  406. static void start_io_acct(struct dm_io *io)
  407. {
  408. struct mapped_device *md = io->md;
  409. struct bio *bio = io->bio;
  410. int cpu;
  411. int rw = bio_data_dir(bio);
  412. io->start_time = jiffies;
  413. cpu = part_stat_lock();
  414. part_round_stats(cpu, &dm_disk(md)->part0);
  415. part_stat_unlock();
  416. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  417. atomic_inc_return(&md->pending[rw]));
  418. if (unlikely(dm_stats_used(&md->stats)))
  419. dm_stats_account_io(&md->stats, bio_data_dir(bio),
  420. bio->bi_iter.bi_sector, bio_sectors(bio),
  421. false, 0, &io->stats_aux);
  422. }
  423. static void end_io_acct(struct dm_io *io)
  424. {
  425. struct mapped_device *md = io->md;
  426. struct bio *bio = io->bio;
  427. unsigned long duration = jiffies - io->start_time;
  428. int pending;
  429. int rw = bio_data_dir(bio);
  430. generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
  431. if (unlikely(dm_stats_used(&md->stats)))
  432. dm_stats_account_io(&md->stats, bio_data_dir(bio),
  433. bio->bi_iter.bi_sector, bio_sectors(bio),
  434. true, duration, &io->stats_aux);
  435. /*
  436. * After this is decremented the bio must not be touched if it is
  437. * a flush.
  438. */
  439. pending = atomic_dec_return(&md->pending[rw]);
  440. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  441. pending += atomic_read(&md->pending[rw^0x1]);
  442. /* nudge anyone waiting on suspend queue */
  443. if (!pending)
  444. wake_up(&md->wait);
  445. }
  446. /*
  447. * Add the bio to the list of deferred io.
  448. */
  449. static void queue_io(struct mapped_device *md, struct bio *bio)
  450. {
  451. unsigned long flags;
  452. spin_lock_irqsave(&md->deferred_lock, flags);
  453. bio_list_add(&md->deferred, bio);
  454. spin_unlock_irqrestore(&md->deferred_lock, flags);
  455. queue_work(md->wq, &md->work);
  456. }
  457. /*
  458. * Everyone (including functions in this file), should use this
  459. * function to access the md->map field, and make sure they call
  460. * dm_put_live_table() when finished.
  461. */
  462. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
  463. {
  464. *srcu_idx = srcu_read_lock(&md->io_barrier);
  465. return srcu_dereference(md->map, &md->io_barrier);
  466. }
  467. void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
  468. {
  469. srcu_read_unlock(&md->io_barrier, srcu_idx);
  470. }
  471. void dm_sync_table(struct mapped_device *md)
  472. {
  473. synchronize_srcu(&md->io_barrier);
  474. synchronize_rcu_expedited();
  475. }
  476. /*
  477. * A fast alternative to dm_get_live_table/dm_put_live_table.
  478. * The caller must not block between these two functions.
  479. */
  480. static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
  481. {
  482. rcu_read_lock();
  483. return rcu_dereference(md->map);
  484. }
  485. static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
  486. {
  487. rcu_read_unlock();
  488. }
  489. /*
  490. * Open a table device so we can use it as a map destination.
  491. */
  492. static int open_table_device(struct table_device *td, dev_t dev,
  493. struct mapped_device *md)
  494. {
  495. static char *_claim_ptr = "I belong to device-mapper";
  496. struct block_device *bdev;
  497. int r;
  498. BUG_ON(td->dm_dev.bdev);
  499. bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
  500. if (IS_ERR(bdev))
  501. return PTR_ERR(bdev);
  502. r = bd_link_disk_holder(bdev, dm_disk(md));
  503. if (r) {
  504. blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
  505. return r;
  506. }
  507. td->dm_dev.bdev = bdev;
  508. return 0;
  509. }
  510. /*
  511. * Close a table device that we've been using.
  512. */
  513. static void close_table_device(struct table_device *td, struct mapped_device *md)
  514. {
  515. if (!td->dm_dev.bdev)
  516. return;
  517. bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
  518. blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
  519. td->dm_dev.bdev = NULL;
  520. }
  521. static struct table_device *find_table_device(struct list_head *l, dev_t dev,
  522. fmode_t mode) {
  523. struct table_device *td;
  524. list_for_each_entry(td, l, list)
  525. if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
  526. return td;
  527. return NULL;
  528. }
  529. int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
  530. struct dm_dev **result) {
  531. int r;
  532. struct table_device *td;
  533. mutex_lock(&md->table_devices_lock);
  534. td = find_table_device(&md->table_devices, dev, mode);
  535. if (!td) {
  536. td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
  537. if (!td) {
  538. mutex_unlock(&md->table_devices_lock);
  539. return -ENOMEM;
  540. }
  541. td->dm_dev.mode = mode;
  542. td->dm_dev.bdev = NULL;
  543. if ((r = open_table_device(td, dev, md))) {
  544. mutex_unlock(&md->table_devices_lock);
  545. kfree(td);
  546. return r;
  547. }
  548. format_dev_t(td->dm_dev.name, dev);
  549. atomic_set(&td->count, 0);
  550. list_add(&td->list, &md->table_devices);
  551. }
  552. atomic_inc(&td->count);
  553. mutex_unlock(&md->table_devices_lock);
  554. *result = &td->dm_dev;
  555. return 0;
  556. }
  557. EXPORT_SYMBOL_GPL(dm_get_table_device);
  558. void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
  559. {
  560. struct table_device *td = container_of(d, struct table_device, dm_dev);
  561. mutex_lock(&md->table_devices_lock);
  562. if (atomic_dec_and_test(&td->count)) {
  563. close_table_device(td, md);
  564. list_del(&td->list);
  565. kfree(td);
  566. }
  567. mutex_unlock(&md->table_devices_lock);
  568. }
  569. EXPORT_SYMBOL(dm_put_table_device);
  570. static void free_table_devices(struct list_head *devices)
  571. {
  572. struct list_head *tmp, *next;
  573. list_for_each_safe(tmp, next, devices) {
  574. struct table_device *td = list_entry(tmp, struct table_device, list);
  575. DMWARN("dm_destroy: %s still exists with %d references",
  576. td->dm_dev.name, atomic_read(&td->count));
  577. kfree(td);
  578. }
  579. }
  580. /*
  581. * Get the geometry associated with a dm device
  582. */
  583. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  584. {
  585. *geo = md->geometry;
  586. return 0;
  587. }
  588. /*
  589. * Set the geometry of a device.
  590. */
  591. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  592. {
  593. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  594. if (geo->start > sz) {
  595. DMWARN("Start sector is beyond the geometry limits.");
  596. return -EINVAL;
  597. }
  598. md->geometry = *geo;
  599. return 0;
  600. }
  601. /*-----------------------------------------------------------------
  602. * CRUD START:
  603. * A more elegant soln is in the works that uses the queue
  604. * merge fn, unfortunately there are a couple of changes to
  605. * the block layer that I want to make for this. So in the
  606. * interests of getting something for people to use I give
  607. * you this clearly demarcated crap.
  608. *---------------------------------------------------------------*/
  609. static int __noflush_suspending(struct mapped_device *md)
  610. {
  611. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  612. }
  613. /*
  614. * Decrements the number of outstanding ios that a bio has been
  615. * cloned into, completing the original io if necc.
  616. */
  617. static void dec_pending(struct dm_io *io, int error)
  618. {
  619. unsigned long flags;
  620. int io_error;
  621. struct bio *bio;
  622. struct mapped_device *md = io->md;
  623. /* Push-back supersedes any I/O errors */
  624. if (unlikely(error)) {
  625. spin_lock_irqsave(&io->endio_lock, flags);
  626. if (!(io->error > 0 && __noflush_suspending(md)))
  627. io->error = error;
  628. spin_unlock_irqrestore(&io->endio_lock, flags);
  629. }
  630. if (atomic_dec_and_test(&io->io_count)) {
  631. if (io->error == DM_ENDIO_REQUEUE) {
  632. /*
  633. * Target requested pushing back the I/O.
  634. */
  635. spin_lock_irqsave(&md->deferred_lock, flags);
  636. if (__noflush_suspending(md))
  637. bio_list_add_head(&md->deferred, io->bio);
  638. else
  639. /* noflush suspend was interrupted. */
  640. io->error = -EIO;
  641. spin_unlock_irqrestore(&md->deferred_lock, flags);
  642. }
  643. io_error = io->error;
  644. bio = io->bio;
  645. end_io_acct(io);
  646. free_io(md, io);
  647. if (io_error == DM_ENDIO_REQUEUE)
  648. return;
  649. if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
  650. /*
  651. * Preflush done for flush with data, reissue
  652. * without REQ_PREFLUSH.
  653. */
  654. bio->bi_opf &= ~REQ_PREFLUSH;
  655. queue_io(md, bio);
  656. } else {
  657. /* done with normal IO or empty flush */
  658. trace_block_bio_complete(md->queue, bio, io_error);
  659. bio->bi_error = io_error;
  660. bio_endio(bio);
  661. }
  662. }
  663. }
  664. void disable_write_same(struct mapped_device *md)
  665. {
  666. struct queue_limits *limits = dm_get_queue_limits(md);
  667. /* device doesn't really support WRITE SAME, disable it */
  668. limits->max_write_same_sectors = 0;
  669. }
  670. static void clone_endio(struct bio *bio)
  671. {
  672. int error = bio->bi_error;
  673. int r = error;
  674. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  675. struct dm_io *io = tio->io;
  676. struct mapped_device *md = tio->io->md;
  677. dm_endio_fn endio = tio->ti->type->end_io;
  678. if (endio) {
  679. r = endio(tio->ti, bio, error);
  680. if (r < 0 || r == DM_ENDIO_REQUEUE)
  681. /*
  682. * error and requeue request are handled
  683. * in dec_pending().
  684. */
  685. error = r;
  686. else if (r == DM_ENDIO_INCOMPLETE)
  687. /* The target will handle the io */
  688. return;
  689. else if (r) {
  690. DMWARN("unimplemented target endio return value: %d", r);
  691. BUG();
  692. }
  693. }
  694. if (unlikely(r == -EREMOTEIO && (bio_op(bio) == REQ_OP_WRITE_SAME) &&
  695. !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
  696. disable_write_same(md);
  697. free_tio(tio);
  698. dec_pending(io, error);
  699. }
  700. /*
  701. * Return maximum size of I/O possible at the supplied sector up to the current
  702. * target boundary.
  703. */
  704. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  705. {
  706. sector_t target_offset = dm_target_offset(ti, sector);
  707. return ti->len - target_offset;
  708. }
  709. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  710. {
  711. sector_t len = max_io_len_target_boundary(sector, ti);
  712. sector_t offset, max_len;
  713. /*
  714. * Does the target need to split even further?
  715. */
  716. if (ti->max_io_len) {
  717. offset = dm_target_offset(ti, sector);
  718. if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
  719. max_len = sector_div(offset, ti->max_io_len);
  720. else
  721. max_len = offset & (ti->max_io_len - 1);
  722. max_len = ti->max_io_len - max_len;
  723. if (len > max_len)
  724. len = max_len;
  725. }
  726. return len;
  727. }
  728. int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
  729. {
  730. if (len > UINT_MAX) {
  731. DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
  732. (unsigned long long)len, UINT_MAX);
  733. ti->error = "Maximum size of target IO is too large";
  734. return -EINVAL;
  735. }
  736. ti->max_io_len = (uint32_t) len;
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
  740. static long dm_blk_direct_access(struct block_device *bdev, sector_t sector,
  741. void **kaddr, pfn_t *pfn, long size)
  742. {
  743. struct mapped_device *md = bdev->bd_disk->private_data;
  744. struct dm_table *map;
  745. struct dm_target *ti;
  746. int srcu_idx;
  747. long len, ret = -EIO;
  748. map = dm_get_live_table(md, &srcu_idx);
  749. if (!map)
  750. goto out;
  751. ti = dm_table_find_target(map, sector);
  752. if (!dm_target_is_valid(ti))
  753. goto out;
  754. len = max_io_len(sector, ti) << SECTOR_SHIFT;
  755. size = min(len, size);
  756. if (ti->type->direct_access)
  757. ret = ti->type->direct_access(ti, sector, kaddr, pfn, size);
  758. out:
  759. dm_put_live_table(md, srcu_idx);
  760. return min(ret, size);
  761. }
  762. /*
  763. * A target may call dm_accept_partial_bio only from the map routine. It is
  764. * allowed for all bio types except REQ_PREFLUSH.
  765. *
  766. * dm_accept_partial_bio informs the dm that the target only wants to process
  767. * additional n_sectors sectors of the bio and the rest of the data should be
  768. * sent in a next bio.
  769. *
  770. * A diagram that explains the arithmetics:
  771. * +--------------------+---------------+-------+
  772. * | 1 | 2 | 3 |
  773. * +--------------------+---------------+-------+
  774. *
  775. * <-------------- *tio->len_ptr --------------->
  776. * <------- bi_size ------->
  777. * <-- n_sectors -->
  778. *
  779. * Region 1 was already iterated over with bio_advance or similar function.
  780. * (it may be empty if the target doesn't use bio_advance)
  781. * Region 2 is the remaining bio size that the target wants to process.
  782. * (it may be empty if region 1 is non-empty, although there is no reason
  783. * to make it empty)
  784. * The target requires that region 3 is to be sent in the next bio.
  785. *
  786. * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
  787. * the partially processed part (the sum of regions 1+2) must be the same for all
  788. * copies of the bio.
  789. */
  790. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
  791. {
  792. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  793. unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  794. BUG_ON(bio->bi_opf & REQ_PREFLUSH);
  795. BUG_ON(bi_size > *tio->len_ptr);
  796. BUG_ON(n_sectors > bi_size);
  797. *tio->len_ptr -= bi_size - n_sectors;
  798. bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
  799. }
  800. EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
  801. /*
  802. * Flush current->bio_list when the target map method blocks.
  803. * This fixes deadlocks in snapshot and possibly in other targets.
  804. */
  805. struct dm_offload {
  806. struct blk_plug plug;
  807. struct blk_plug_cb cb;
  808. };
  809. static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
  810. {
  811. struct dm_offload *o = container_of(cb, struct dm_offload, cb);
  812. struct bio_list list;
  813. struct bio *bio;
  814. int i;
  815. INIT_LIST_HEAD(&o->cb.list);
  816. if (unlikely(!current->bio_list))
  817. return;
  818. for (i = 0; i < 2; i++) {
  819. list = current->bio_list[i];
  820. bio_list_init(&current->bio_list[i]);
  821. while ((bio = bio_list_pop(&list))) {
  822. struct bio_set *bs = bio->bi_pool;
  823. if (unlikely(!bs) || bs == fs_bio_set) {
  824. bio_list_add(&current->bio_list[i], bio);
  825. continue;
  826. }
  827. spin_lock(&bs->rescue_lock);
  828. bio_list_add(&bs->rescue_list, bio);
  829. queue_work(bs->rescue_workqueue, &bs->rescue_work);
  830. spin_unlock(&bs->rescue_lock);
  831. }
  832. }
  833. }
  834. static void dm_offload_start(struct dm_offload *o)
  835. {
  836. blk_start_plug(&o->plug);
  837. o->cb.callback = flush_current_bio_list;
  838. list_add(&o->cb.list, &current->plug->cb_list);
  839. }
  840. static void dm_offload_end(struct dm_offload *o)
  841. {
  842. list_del(&o->cb.list);
  843. blk_finish_plug(&o->plug);
  844. }
  845. static void __map_bio(struct dm_target_io *tio)
  846. {
  847. int r;
  848. sector_t sector;
  849. struct dm_offload o;
  850. struct bio *clone = &tio->clone;
  851. struct dm_target *ti = tio->ti;
  852. clone->bi_end_io = clone_endio;
  853. /*
  854. * Map the clone. If r == 0 we don't need to do
  855. * anything, the target has assumed ownership of
  856. * this io.
  857. */
  858. atomic_inc(&tio->io->io_count);
  859. sector = clone->bi_iter.bi_sector;
  860. dm_offload_start(&o);
  861. r = ti->type->map(ti, clone);
  862. dm_offload_end(&o);
  863. if (r == DM_MAPIO_REMAPPED) {
  864. /* the bio has been remapped so dispatch it */
  865. trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
  866. tio->io->bio->bi_bdev->bd_dev, sector);
  867. generic_make_request(clone);
  868. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  869. /* error the io and bail out, or requeue it if needed */
  870. dec_pending(tio->io, r);
  871. free_tio(tio);
  872. } else if (r != DM_MAPIO_SUBMITTED) {
  873. DMWARN("unimplemented target map return value: %d", r);
  874. BUG();
  875. }
  876. }
  877. struct clone_info {
  878. struct mapped_device *md;
  879. struct dm_table *map;
  880. struct bio *bio;
  881. struct dm_io *io;
  882. sector_t sector;
  883. unsigned sector_count;
  884. };
  885. static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
  886. {
  887. bio->bi_iter.bi_sector = sector;
  888. bio->bi_iter.bi_size = to_bytes(len);
  889. }
  890. /*
  891. * Creates a bio that consists of range of complete bvecs.
  892. */
  893. static int clone_bio(struct dm_target_io *tio, struct bio *bio,
  894. sector_t sector, unsigned len)
  895. {
  896. struct bio *clone = &tio->clone;
  897. __bio_clone_fast(clone, bio);
  898. if (bio_integrity(bio)) {
  899. int r = bio_integrity_clone(clone, bio, GFP_NOIO);
  900. if (r < 0)
  901. return r;
  902. }
  903. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  904. clone->bi_iter.bi_size = to_bytes(len);
  905. if (bio_integrity(bio))
  906. bio_integrity_trim(clone, 0, len);
  907. return 0;
  908. }
  909. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  910. struct dm_target *ti,
  911. unsigned target_bio_nr)
  912. {
  913. struct dm_target_io *tio;
  914. struct bio *clone;
  915. clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
  916. tio = container_of(clone, struct dm_target_io, clone);
  917. tio->io = ci->io;
  918. tio->ti = ti;
  919. tio->target_bio_nr = target_bio_nr;
  920. return tio;
  921. }
  922. static void __clone_and_map_simple_bio(struct clone_info *ci,
  923. struct dm_target *ti,
  924. unsigned target_bio_nr, unsigned *len)
  925. {
  926. struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
  927. struct bio *clone = &tio->clone;
  928. tio->len_ptr = len;
  929. __bio_clone_fast(clone, ci->bio);
  930. if (len)
  931. bio_setup_sector(clone, ci->sector, *len);
  932. __map_bio(tio);
  933. }
  934. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  935. unsigned num_bios, unsigned *len)
  936. {
  937. unsigned target_bio_nr;
  938. for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
  939. __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
  940. }
  941. static int __send_empty_flush(struct clone_info *ci)
  942. {
  943. unsigned target_nr = 0;
  944. struct dm_target *ti;
  945. BUG_ON(bio_has_data(ci->bio));
  946. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  947. __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
  948. return 0;
  949. }
  950. static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  951. sector_t sector, unsigned *len)
  952. {
  953. struct bio *bio = ci->bio;
  954. struct dm_target_io *tio;
  955. unsigned target_bio_nr;
  956. unsigned num_target_bios = 1;
  957. int r = 0;
  958. /*
  959. * Does the target want to receive duplicate copies of the bio?
  960. */
  961. if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
  962. num_target_bios = ti->num_write_bios(ti, bio);
  963. for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
  964. tio = alloc_tio(ci, ti, target_bio_nr);
  965. tio->len_ptr = len;
  966. r = clone_bio(tio, bio, sector, *len);
  967. if (r < 0) {
  968. free_tio(tio);
  969. break;
  970. }
  971. __map_bio(tio);
  972. }
  973. return r;
  974. }
  975. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  976. static unsigned get_num_discard_bios(struct dm_target *ti)
  977. {
  978. return ti->num_discard_bios;
  979. }
  980. static unsigned get_num_write_same_bios(struct dm_target *ti)
  981. {
  982. return ti->num_write_same_bios;
  983. }
  984. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  985. static bool is_split_required_for_discard(struct dm_target *ti)
  986. {
  987. return ti->split_discard_bios;
  988. }
  989. static int __send_changing_extent_only(struct clone_info *ci,
  990. get_num_bios_fn get_num_bios,
  991. is_split_required_fn is_split_required)
  992. {
  993. struct dm_target *ti;
  994. unsigned len;
  995. unsigned num_bios;
  996. do {
  997. ti = dm_table_find_target(ci->map, ci->sector);
  998. if (!dm_target_is_valid(ti))
  999. return -EIO;
  1000. /*
  1001. * Even though the device advertised support for this type of
  1002. * request, that does not mean every target supports it, and
  1003. * reconfiguration might also have changed that since the
  1004. * check was performed.
  1005. */
  1006. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1007. if (!num_bios)
  1008. return -EOPNOTSUPP;
  1009. if (is_split_required && !is_split_required(ti))
  1010. len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1011. else
  1012. len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
  1013. __send_duplicate_bios(ci, ti, num_bios, &len);
  1014. ci->sector += len;
  1015. } while (ci->sector_count -= len);
  1016. return 0;
  1017. }
  1018. static int __send_discard(struct clone_info *ci)
  1019. {
  1020. return __send_changing_extent_only(ci, get_num_discard_bios,
  1021. is_split_required_for_discard);
  1022. }
  1023. static int __send_write_same(struct clone_info *ci)
  1024. {
  1025. return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
  1026. }
  1027. /*
  1028. * Select the correct strategy for processing a non-flush bio.
  1029. */
  1030. static int __split_and_process_non_flush(struct clone_info *ci)
  1031. {
  1032. struct bio *bio = ci->bio;
  1033. struct dm_target *ti;
  1034. unsigned len;
  1035. int r;
  1036. if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
  1037. return __send_discard(ci);
  1038. else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
  1039. return __send_write_same(ci);
  1040. ti = dm_table_find_target(ci->map, ci->sector);
  1041. if (!dm_target_is_valid(ti))
  1042. return -EIO;
  1043. len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
  1044. r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
  1045. if (r < 0)
  1046. return r;
  1047. ci->sector += len;
  1048. ci->sector_count -= len;
  1049. return 0;
  1050. }
  1051. /*
  1052. * Entry point to split a bio into clones and submit them to the targets.
  1053. */
  1054. static void __split_and_process_bio(struct mapped_device *md,
  1055. struct dm_table *map, struct bio *bio)
  1056. {
  1057. struct clone_info ci;
  1058. int error = 0;
  1059. if (unlikely(!map)) {
  1060. bio_io_error(bio);
  1061. return;
  1062. }
  1063. ci.map = map;
  1064. ci.md = md;
  1065. ci.io = alloc_io(md);
  1066. ci.io->error = 0;
  1067. atomic_set(&ci.io->io_count, 1);
  1068. ci.io->bio = bio;
  1069. ci.io->md = md;
  1070. spin_lock_init(&ci.io->endio_lock);
  1071. ci.sector = bio->bi_iter.bi_sector;
  1072. start_io_acct(ci.io);
  1073. if (bio->bi_opf & REQ_PREFLUSH) {
  1074. ci.bio = &ci.md->flush_bio;
  1075. ci.sector_count = 0;
  1076. error = __send_empty_flush(&ci);
  1077. /* dec_pending submits any data associated with flush */
  1078. } else {
  1079. ci.bio = bio;
  1080. ci.sector_count = bio_sectors(bio);
  1081. while (ci.sector_count && !error)
  1082. error = __split_and_process_non_flush(&ci);
  1083. }
  1084. /* drop the extra reference count */
  1085. dec_pending(ci.io, error);
  1086. }
  1087. /*-----------------------------------------------------------------
  1088. * CRUD END
  1089. *---------------------------------------------------------------*/
  1090. /*
  1091. * The request function that just remaps the bio built up by
  1092. * dm_merge_bvec.
  1093. */
  1094. static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
  1095. {
  1096. int rw = bio_data_dir(bio);
  1097. struct mapped_device *md = q->queuedata;
  1098. int srcu_idx;
  1099. struct dm_table *map;
  1100. map = dm_get_live_table(md, &srcu_idx);
  1101. generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
  1102. /* if we're suspended, we have to queue this io for later */
  1103. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1104. dm_put_live_table(md, srcu_idx);
  1105. if (!(bio->bi_opf & REQ_RAHEAD))
  1106. queue_io(md, bio);
  1107. else
  1108. bio_io_error(bio);
  1109. return BLK_QC_T_NONE;
  1110. }
  1111. __split_and_process_bio(md, map, bio);
  1112. dm_put_live_table(md, srcu_idx);
  1113. return BLK_QC_T_NONE;
  1114. }
  1115. static int dm_any_congested(void *congested_data, int bdi_bits)
  1116. {
  1117. int r = bdi_bits;
  1118. struct mapped_device *md = congested_data;
  1119. struct dm_table *map;
  1120. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1121. if (dm_request_based(md)) {
  1122. /*
  1123. * With request-based DM we only need to check the
  1124. * top-level queue for congestion.
  1125. */
  1126. r = md->queue->backing_dev_info->wb.state & bdi_bits;
  1127. } else {
  1128. map = dm_get_live_table_fast(md);
  1129. if (map)
  1130. r = dm_table_any_congested(map, bdi_bits);
  1131. dm_put_live_table_fast(md);
  1132. }
  1133. }
  1134. return r;
  1135. }
  1136. /*-----------------------------------------------------------------
  1137. * An IDR is used to keep track of allocated minor numbers.
  1138. *---------------------------------------------------------------*/
  1139. static void free_minor(int minor)
  1140. {
  1141. spin_lock(&_minor_lock);
  1142. idr_remove(&_minor_idr, minor);
  1143. spin_unlock(&_minor_lock);
  1144. }
  1145. /*
  1146. * See if the device with a specific minor # is free.
  1147. */
  1148. static int specific_minor(int minor)
  1149. {
  1150. int r;
  1151. if (minor >= (1 << MINORBITS))
  1152. return -EINVAL;
  1153. idr_preload(GFP_KERNEL);
  1154. spin_lock(&_minor_lock);
  1155. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1156. spin_unlock(&_minor_lock);
  1157. idr_preload_end();
  1158. if (r < 0)
  1159. return r == -ENOSPC ? -EBUSY : r;
  1160. return 0;
  1161. }
  1162. static int next_free_minor(int *minor)
  1163. {
  1164. int r;
  1165. idr_preload(GFP_KERNEL);
  1166. spin_lock(&_minor_lock);
  1167. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1168. spin_unlock(&_minor_lock);
  1169. idr_preload_end();
  1170. if (r < 0)
  1171. return r;
  1172. *minor = r;
  1173. return 0;
  1174. }
  1175. static const struct block_device_operations dm_blk_dops;
  1176. static void dm_wq_work(struct work_struct *work);
  1177. void dm_init_md_queue(struct mapped_device *md)
  1178. {
  1179. /*
  1180. * Request-based dm devices cannot be stacked on top of bio-based dm
  1181. * devices. The type of this dm device may not have been decided yet.
  1182. * The type is decided at the first table loading time.
  1183. * To prevent problematic device stacking, clear the queue flag
  1184. * for request stacking support until then.
  1185. *
  1186. * This queue is new, so no concurrency on the queue_flags.
  1187. */
  1188. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1189. /*
  1190. * Initialize data that will only be used by a non-blk-mq DM queue
  1191. * - must do so here (in alloc_dev callchain) before queue is used
  1192. */
  1193. md->queue->queuedata = md;
  1194. md->queue->backing_dev_info->congested_data = md;
  1195. }
  1196. void dm_init_normal_md_queue(struct mapped_device *md)
  1197. {
  1198. md->use_blk_mq = false;
  1199. dm_init_md_queue(md);
  1200. /*
  1201. * Initialize aspects of queue that aren't relevant for blk-mq
  1202. */
  1203. md->queue->backing_dev_info->congested_fn = dm_any_congested;
  1204. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1205. }
  1206. static void cleanup_mapped_device(struct mapped_device *md)
  1207. {
  1208. if (md->wq)
  1209. destroy_workqueue(md->wq);
  1210. if (md->kworker_task)
  1211. kthread_stop(md->kworker_task);
  1212. mempool_destroy(md->io_pool);
  1213. if (md->bs)
  1214. bioset_free(md->bs);
  1215. if (md->disk) {
  1216. spin_lock(&_minor_lock);
  1217. md->disk->private_data = NULL;
  1218. spin_unlock(&_minor_lock);
  1219. del_gendisk(md->disk);
  1220. put_disk(md->disk);
  1221. }
  1222. if (md->queue)
  1223. blk_cleanup_queue(md->queue);
  1224. cleanup_srcu_struct(&md->io_barrier);
  1225. if (md->bdev) {
  1226. bdput(md->bdev);
  1227. md->bdev = NULL;
  1228. }
  1229. dm_mq_cleanup_mapped_device(md);
  1230. }
  1231. /*
  1232. * Allocate and initialise a blank device with a given minor.
  1233. */
  1234. static struct mapped_device *alloc_dev(int minor)
  1235. {
  1236. int r, numa_node_id = dm_get_numa_node();
  1237. struct mapped_device *md;
  1238. void *old_md;
  1239. md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
  1240. if (!md) {
  1241. DMWARN("unable to allocate device, out of memory.");
  1242. return NULL;
  1243. }
  1244. if (!try_module_get(THIS_MODULE))
  1245. goto bad_module_get;
  1246. /* get a minor number for the dev */
  1247. if (minor == DM_ANY_MINOR)
  1248. r = next_free_minor(&minor);
  1249. else
  1250. r = specific_minor(minor);
  1251. if (r < 0)
  1252. goto bad_minor;
  1253. r = init_srcu_struct(&md->io_barrier);
  1254. if (r < 0)
  1255. goto bad_io_barrier;
  1256. md->numa_node_id = numa_node_id;
  1257. md->use_blk_mq = dm_use_blk_mq_default();
  1258. md->init_tio_pdu = false;
  1259. md->type = DM_TYPE_NONE;
  1260. mutex_init(&md->suspend_lock);
  1261. mutex_init(&md->type_lock);
  1262. mutex_init(&md->table_devices_lock);
  1263. spin_lock_init(&md->deferred_lock);
  1264. atomic_set(&md->holders, 1);
  1265. atomic_set(&md->open_count, 0);
  1266. atomic_set(&md->event_nr, 0);
  1267. atomic_set(&md->uevent_seq, 0);
  1268. INIT_LIST_HEAD(&md->uevent_list);
  1269. INIT_LIST_HEAD(&md->table_devices);
  1270. spin_lock_init(&md->uevent_lock);
  1271. md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
  1272. if (!md->queue)
  1273. goto bad;
  1274. dm_init_md_queue(md);
  1275. md->disk = alloc_disk_node(1, numa_node_id);
  1276. if (!md->disk)
  1277. goto bad;
  1278. atomic_set(&md->pending[0], 0);
  1279. atomic_set(&md->pending[1], 0);
  1280. init_waitqueue_head(&md->wait);
  1281. INIT_WORK(&md->work, dm_wq_work);
  1282. init_waitqueue_head(&md->eventq);
  1283. init_completion(&md->kobj_holder.completion);
  1284. md->kworker_task = NULL;
  1285. md->disk->major = _major;
  1286. md->disk->first_minor = minor;
  1287. md->disk->fops = &dm_blk_dops;
  1288. md->disk->queue = md->queue;
  1289. md->disk->private_data = md;
  1290. sprintf(md->disk->disk_name, "dm-%d", minor);
  1291. add_disk(md->disk);
  1292. format_dev_t(md->name, MKDEV(_major, minor));
  1293. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1294. if (!md->wq)
  1295. goto bad;
  1296. md->bdev = bdget_disk(md->disk, 0);
  1297. if (!md->bdev)
  1298. goto bad;
  1299. bio_init(&md->flush_bio, NULL, 0);
  1300. md->flush_bio.bi_bdev = md->bdev;
  1301. md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  1302. dm_stats_init(&md->stats);
  1303. /* Populate the mapping, nobody knows we exist yet */
  1304. spin_lock(&_minor_lock);
  1305. old_md = idr_replace(&_minor_idr, md, minor);
  1306. spin_unlock(&_minor_lock);
  1307. BUG_ON(old_md != MINOR_ALLOCED);
  1308. return md;
  1309. bad:
  1310. cleanup_mapped_device(md);
  1311. bad_io_barrier:
  1312. free_minor(minor);
  1313. bad_minor:
  1314. module_put(THIS_MODULE);
  1315. bad_module_get:
  1316. kfree(md);
  1317. return NULL;
  1318. }
  1319. static void unlock_fs(struct mapped_device *md);
  1320. static void free_dev(struct mapped_device *md)
  1321. {
  1322. int minor = MINOR(disk_devt(md->disk));
  1323. unlock_fs(md);
  1324. cleanup_mapped_device(md);
  1325. free_table_devices(&md->table_devices);
  1326. dm_stats_cleanup(&md->stats);
  1327. free_minor(minor);
  1328. module_put(THIS_MODULE);
  1329. kfree(md);
  1330. }
  1331. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1332. {
  1333. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  1334. if (md->bs) {
  1335. /* The md already has necessary mempools. */
  1336. if (dm_table_bio_based(t)) {
  1337. /*
  1338. * Reload bioset because front_pad may have changed
  1339. * because a different table was loaded.
  1340. */
  1341. bioset_free(md->bs);
  1342. md->bs = p->bs;
  1343. p->bs = NULL;
  1344. }
  1345. /*
  1346. * There's no need to reload with request-based dm
  1347. * because the size of front_pad doesn't change.
  1348. * Note for future: If you are to reload bioset,
  1349. * prep-ed requests in the queue may refer
  1350. * to bio from the old bioset, so you must walk
  1351. * through the queue to unprep.
  1352. */
  1353. goto out;
  1354. }
  1355. BUG_ON(!p || md->io_pool || md->bs);
  1356. md->io_pool = p->io_pool;
  1357. p->io_pool = NULL;
  1358. md->bs = p->bs;
  1359. p->bs = NULL;
  1360. out:
  1361. /* mempool bind completed, no longer need any mempools in the table */
  1362. dm_table_free_md_mempools(t);
  1363. }
  1364. /*
  1365. * Bind a table to the device.
  1366. */
  1367. static void event_callback(void *context)
  1368. {
  1369. unsigned long flags;
  1370. LIST_HEAD(uevents);
  1371. struct mapped_device *md = (struct mapped_device *) context;
  1372. spin_lock_irqsave(&md->uevent_lock, flags);
  1373. list_splice_init(&md->uevent_list, &uevents);
  1374. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1375. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1376. atomic_inc(&md->event_nr);
  1377. wake_up(&md->eventq);
  1378. }
  1379. /*
  1380. * Protected by md->suspend_lock obtained by dm_swap_table().
  1381. */
  1382. static void __set_size(struct mapped_device *md, sector_t size)
  1383. {
  1384. set_capacity(md->disk, size);
  1385. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1386. }
  1387. /*
  1388. * Returns old map, which caller must destroy.
  1389. */
  1390. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  1391. struct queue_limits *limits)
  1392. {
  1393. struct dm_table *old_map;
  1394. struct request_queue *q = md->queue;
  1395. sector_t size;
  1396. lockdep_assert_held(&md->suspend_lock);
  1397. size = dm_table_get_size(t);
  1398. /*
  1399. * Wipe any geometry if the size of the table changed.
  1400. */
  1401. if (size != dm_get_size(md))
  1402. memset(&md->geometry, 0, sizeof(md->geometry));
  1403. __set_size(md, size);
  1404. dm_table_event_callback(t, event_callback, md);
  1405. /*
  1406. * The queue hasn't been stopped yet, if the old table type wasn't
  1407. * for request-based during suspension. So stop it to prevent
  1408. * I/O mapping before resume.
  1409. * This must be done before setting the queue restrictions,
  1410. * because request-based dm may be run just after the setting.
  1411. */
  1412. if (dm_table_request_based(t)) {
  1413. dm_stop_queue(q);
  1414. /*
  1415. * Leverage the fact that request-based DM targets are
  1416. * immutable singletons and establish md->immutable_target
  1417. * - used to optimize both dm_request_fn and dm_mq_queue_rq
  1418. */
  1419. md->immutable_target = dm_table_get_immutable_target(t);
  1420. }
  1421. __bind_mempools(md, t);
  1422. old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1423. rcu_assign_pointer(md->map, (void *)t);
  1424. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  1425. dm_table_set_restrictions(t, q, limits);
  1426. if (old_map)
  1427. dm_sync_table(md);
  1428. return old_map;
  1429. }
  1430. /*
  1431. * Returns unbound table for the caller to free.
  1432. */
  1433. static struct dm_table *__unbind(struct mapped_device *md)
  1434. {
  1435. struct dm_table *map = rcu_dereference_protected(md->map, 1);
  1436. if (!map)
  1437. return NULL;
  1438. dm_table_event_callback(map, NULL, NULL);
  1439. RCU_INIT_POINTER(md->map, NULL);
  1440. dm_sync_table(md);
  1441. return map;
  1442. }
  1443. /*
  1444. * Constructor for a new device.
  1445. */
  1446. int dm_create(int minor, struct mapped_device **result)
  1447. {
  1448. struct mapped_device *md;
  1449. md = alloc_dev(minor);
  1450. if (!md)
  1451. return -ENXIO;
  1452. dm_sysfs_init(md);
  1453. *result = md;
  1454. return 0;
  1455. }
  1456. /*
  1457. * Functions to manage md->type.
  1458. * All are required to hold md->type_lock.
  1459. */
  1460. void dm_lock_md_type(struct mapped_device *md)
  1461. {
  1462. mutex_lock(&md->type_lock);
  1463. }
  1464. void dm_unlock_md_type(struct mapped_device *md)
  1465. {
  1466. mutex_unlock(&md->type_lock);
  1467. }
  1468. void dm_set_md_type(struct mapped_device *md, unsigned type)
  1469. {
  1470. BUG_ON(!mutex_is_locked(&md->type_lock));
  1471. md->type = type;
  1472. }
  1473. unsigned dm_get_md_type(struct mapped_device *md)
  1474. {
  1475. return md->type;
  1476. }
  1477. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  1478. {
  1479. return md->immutable_target_type;
  1480. }
  1481. /*
  1482. * The queue_limits are only valid as long as you have a reference
  1483. * count on 'md'.
  1484. */
  1485. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  1486. {
  1487. BUG_ON(!atomic_read(&md->holders));
  1488. return &md->queue->limits;
  1489. }
  1490. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  1491. /*
  1492. * Setup the DM device's queue based on md's type
  1493. */
  1494. int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
  1495. {
  1496. int r;
  1497. unsigned type = dm_get_md_type(md);
  1498. switch (type) {
  1499. case DM_TYPE_REQUEST_BASED:
  1500. r = dm_old_init_request_queue(md, t);
  1501. if (r) {
  1502. DMERR("Cannot initialize queue for request-based mapped device");
  1503. return r;
  1504. }
  1505. break;
  1506. case DM_TYPE_MQ_REQUEST_BASED:
  1507. r = dm_mq_init_request_queue(md, t);
  1508. if (r) {
  1509. DMERR("Cannot initialize queue for request-based dm-mq mapped device");
  1510. return r;
  1511. }
  1512. break;
  1513. case DM_TYPE_BIO_BASED:
  1514. case DM_TYPE_DAX_BIO_BASED:
  1515. dm_init_normal_md_queue(md);
  1516. blk_queue_make_request(md->queue, dm_make_request);
  1517. /*
  1518. * DM handles splitting bios as needed. Free the bio_split bioset
  1519. * since it won't be used (saves 1 process per bio-based DM device).
  1520. */
  1521. bioset_free(md->queue->bio_split);
  1522. md->queue->bio_split = NULL;
  1523. if (type == DM_TYPE_DAX_BIO_BASED)
  1524. queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
  1525. break;
  1526. }
  1527. return 0;
  1528. }
  1529. struct mapped_device *dm_get_md(dev_t dev)
  1530. {
  1531. struct mapped_device *md;
  1532. unsigned minor = MINOR(dev);
  1533. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1534. return NULL;
  1535. spin_lock(&_minor_lock);
  1536. md = idr_find(&_minor_idr, minor);
  1537. if (md) {
  1538. if ((md == MINOR_ALLOCED ||
  1539. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1540. dm_deleting_md(md) ||
  1541. test_bit(DMF_FREEING, &md->flags))) {
  1542. md = NULL;
  1543. goto out;
  1544. }
  1545. dm_get(md);
  1546. }
  1547. out:
  1548. spin_unlock(&_minor_lock);
  1549. return md;
  1550. }
  1551. EXPORT_SYMBOL_GPL(dm_get_md);
  1552. void *dm_get_mdptr(struct mapped_device *md)
  1553. {
  1554. return md->interface_ptr;
  1555. }
  1556. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1557. {
  1558. md->interface_ptr = ptr;
  1559. }
  1560. void dm_get(struct mapped_device *md)
  1561. {
  1562. atomic_inc(&md->holders);
  1563. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1564. }
  1565. int dm_hold(struct mapped_device *md)
  1566. {
  1567. spin_lock(&_minor_lock);
  1568. if (test_bit(DMF_FREEING, &md->flags)) {
  1569. spin_unlock(&_minor_lock);
  1570. return -EBUSY;
  1571. }
  1572. dm_get(md);
  1573. spin_unlock(&_minor_lock);
  1574. return 0;
  1575. }
  1576. EXPORT_SYMBOL_GPL(dm_hold);
  1577. const char *dm_device_name(struct mapped_device *md)
  1578. {
  1579. return md->name;
  1580. }
  1581. EXPORT_SYMBOL_GPL(dm_device_name);
  1582. static void __dm_destroy(struct mapped_device *md, bool wait)
  1583. {
  1584. struct request_queue *q = dm_get_md_queue(md);
  1585. struct dm_table *map;
  1586. int srcu_idx;
  1587. might_sleep();
  1588. spin_lock(&_minor_lock);
  1589. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  1590. set_bit(DMF_FREEING, &md->flags);
  1591. spin_unlock(&_minor_lock);
  1592. blk_set_queue_dying(q);
  1593. if (dm_request_based(md) && md->kworker_task)
  1594. kthread_flush_worker(&md->kworker);
  1595. /*
  1596. * Take suspend_lock so that presuspend and postsuspend methods
  1597. * do not race with internal suspend.
  1598. */
  1599. mutex_lock(&md->suspend_lock);
  1600. map = dm_get_live_table(md, &srcu_idx);
  1601. if (!dm_suspended_md(md)) {
  1602. dm_table_presuspend_targets(map);
  1603. dm_table_postsuspend_targets(map);
  1604. }
  1605. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  1606. dm_put_live_table(md, srcu_idx);
  1607. mutex_unlock(&md->suspend_lock);
  1608. /*
  1609. * Rare, but there may be I/O requests still going to complete,
  1610. * for example. Wait for all references to disappear.
  1611. * No one should increment the reference count of the mapped_device,
  1612. * after the mapped_device state becomes DMF_FREEING.
  1613. */
  1614. if (wait)
  1615. while (atomic_read(&md->holders))
  1616. msleep(1);
  1617. else if (atomic_read(&md->holders))
  1618. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  1619. dm_device_name(md), atomic_read(&md->holders));
  1620. dm_sysfs_exit(md);
  1621. dm_table_destroy(__unbind(md));
  1622. free_dev(md);
  1623. }
  1624. void dm_destroy(struct mapped_device *md)
  1625. {
  1626. __dm_destroy(md, true);
  1627. }
  1628. void dm_destroy_immediate(struct mapped_device *md)
  1629. {
  1630. __dm_destroy(md, false);
  1631. }
  1632. void dm_put(struct mapped_device *md)
  1633. {
  1634. atomic_dec(&md->holders);
  1635. }
  1636. EXPORT_SYMBOL_GPL(dm_put);
  1637. static int dm_wait_for_completion(struct mapped_device *md, long task_state)
  1638. {
  1639. int r = 0;
  1640. DEFINE_WAIT(wait);
  1641. while (1) {
  1642. prepare_to_wait(&md->wait, &wait, task_state);
  1643. if (!md_in_flight(md))
  1644. break;
  1645. if (signal_pending_state(task_state, current)) {
  1646. r = -EINTR;
  1647. break;
  1648. }
  1649. io_schedule();
  1650. }
  1651. finish_wait(&md->wait, &wait);
  1652. return r;
  1653. }
  1654. /*
  1655. * Process the deferred bios
  1656. */
  1657. static void dm_wq_work(struct work_struct *work)
  1658. {
  1659. struct mapped_device *md = container_of(work, struct mapped_device,
  1660. work);
  1661. struct bio *c;
  1662. int srcu_idx;
  1663. struct dm_table *map;
  1664. map = dm_get_live_table(md, &srcu_idx);
  1665. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1666. spin_lock_irq(&md->deferred_lock);
  1667. c = bio_list_pop(&md->deferred);
  1668. spin_unlock_irq(&md->deferred_lock);
  1669. if (!c)
  1670. break;
  1671. if (dm_request_based(md))
  1672. generic_make_request(c);
  1673. else
  1674. __split_and_process_bio(md, map, c);
  1675. }
  1676. dm_put_live_table(md, srcu_idx);
  1677. }
  1678. static void dm_queue_flush(struct mapped_device *md)
  1679. {
  1680. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1681. smp_mb__after_atomic();
  1682. queue_work(md->wq, &md->work);
  1683. }
  1684. /*
  1685. * Swap in a new table, returning the old one for the caller to destroy.
  1686. */
  1687. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1688. {
  1689. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  1690. struct queue_limits limits;
  1691. int r;
  1692. mutex_lock(&md->suspend_lock);
  1693. /* device must be suspended */
  1694. if (!dm_suspended_md(md))
  1695. goto out;
  1696. /*
  1697. * If the new table has no data devices, retain the existing limits.
  1698. * This helps multipath with queue_if_no_path if all paths disappear,
  1699. * then new I/O is queued based on these limits, and then some paths
  1700. * reappear.
  1701. */
  1702. if (dm_table_has_no_data_devices(table)) {
  1703. live_map = dm_get_live_table_fast(md);
  1704. if (live_map)
  1705. limits = md->queue->limits;
  1706. dm_put_live_table_fast(md);
  1707. }
  1708. if (!live_map) {
  1709. r = dm_calculate_queue_limits(table, &limits);
  1710. if (r) {
  1711. map = ERR_PTR(r);
  1712. goto out;
  1713. }
  1714. }
  1715. map = __bind(md, table, &limits);
  1716. out:
  1717. mutex_unlock(&md->suspend_lock);
  1718. return map;
  1719. }
  1720. /*
  1721. * Functions to lock and unlock any filesystem running on the
  1722. * device.
  1723. */
  1724. static int lock_fs(struct mapped_device *md)
  1725. {
  1726. int r;
  1727. WARN_ON(md->frozen_sb);
  1728. md->frozen_sb = freeze_bdev(md->bdev);
  1729. if (IS_ERR(md->frozen_sb)) {
  1730. r = PTR_ERR(md->frozen_sb);
  1731. md->frozen_sb = NULL;
  1732. return r;
  1733. }
  1734. set_bit(DMF_FROZEN, &md->flags);
  1735. return 0;
  1736. }
  1737. static void unlock_fs(struct mapped_device *md)
  1738. {
  1739. if (!test_bit(DMF_FROZEN, &md->flags))
  1740. return;
  1741. thaw_bdev(md->bdev, md->frozen_sb);
  1742. md->frozen_sb = NULL;
  1743. clear_bit(DMF_FROZEN, &md->flags);
  1744. }
  1745. /*
  1746. * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
  1747. * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
  1748. * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
  1749. *
  1750. * If __dm_suspend returns 0, the device is completely quiescent
  1751. * now. There is no request-processing activity. All new requests
  1752. * are being added to md->deferred list.
  1753. *
  1754. * Caller must hold md->suspend_lock
  1755. */
  1756. static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
  1757. unsigned suspend_flags, long task_state,
  1758. int dmf_suspended_flag)
  1759. {
  1760. bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
  1761. bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
  1762. int r;
  1763. lockdep_assert_held(&md->suspend_lock);
  1764. /*
  1765. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1766. * This flag is cleared before dm_suspend returns.
  1767. */
  1768. if (noflush)
  1769. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1770. /*
  1771. * This gets reverted if there's an error later and the targets
  1772. * provide the .presuspend_undo hook.
  1773. */
  1774. dm_table_presuspend_targets(map);
  1775. /*
  1776. * Flush I/O to the device.
  1777. * Any I/O submitted after lock_fs() may not be flushed.
  1778. * noflush takes precedence over do_lockfs.
  1779. * (lock_fs() flushes I/Os and waits for them to complete.)
  1780. */
  1781. if (!noflush && do_lockfs) {
  1782. r = lock_fs(md);
  1783. if (r) {
  1784. dm_table_presuspend_undo_targets(map);
  1785. return r;
  1786. }
  1787. }
  1788. /*
  1789. * Here we must make sure that no processes are submitting requests
  1790. * to target drivers i.e. no one may be executing
  1791. * __split_and_process_bio. This is called from dm_request and
  1792. * dm_wq_work.
  1793. *
  1794. * To get all processes out of __split_and_process_bio in dm_request,
  1795. * we take the write lock. To prevent any process from reentering
  1796. * __split_and_process_bio from dm_request and quiesce the thread
  1797. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  1798. * flush_workqueue(md->wq).
  1799. */
  1800. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1801. if (map)
  1802. synchronize_srcu(&md->io_barrier);
  1803. /*
  1804. * Stop md->queue before flushing md->wq in case request-based
  1805. * dm defers requests to md->wq from md->queue.
  1806. */
  1807. if (dm_request_based(md)) {
  1808. dm_stop_queue(md->queue);
  1809. if (md->kworker_task)
  1810. kthread_flush_worker(&md->kworker);
  1811. }
  1812. flush_workqueue(md->wq);
  1813. /*
  1814. * At this point no more requests are entering target request routines.
  1815. * We call dm_wait_for_completion to wait for all existing requests
  1816. * to finish.
  1817. */
  1818. r = dm_wait_for_completion(md, task_state);
  1819. if (!r)
  1820. set_bit(dmf_suspended_flag, &md->flags);
  1821. if (noflush)
  1822. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1823. if (map)
  1824. synchronize_srcu(&md->io_barrier);
  1825. /* were we interrupted ? */
  1826. if (r < 0) {
  1827. dm_queue_flush(md);
  1828. if (dm_request_based(md))
  1829. dm_start_queue(md->queue);
  1830. unlock_fs(md);
  1831. dm_table_presuspend_undo_targets(map);
  1832. /* pushback list is already flushed, so skip flush */
  1833. }
  1834. return r;
  1835. }
  1836. /*
  1837. * We need to be able to change a mapping table under a mounted
  1838. * filesystem. For example we might want to move some data in
  1839. * the background. Before the table can be swapped with
  1840. * dm_bind_table, dm_suspend must be called to flush any in
  1841. * flight bios and ensure that any further io gets deferred.
  1842. */
  1843. /*
  1844. * Suspend mechanism in request-based dm.
  1845. *
  1846. * 1. Flush all I/Os by lock_fs() if needed.
  1847. * 2. Stop dispatching any I/O by stopping the request_queue.
  1848. * 3. Wait for all in-flight I/Os to be completed or requeued.
  1849. *
  1850. * To abort suspend, start the request_queue.
  1851. */
  1852. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1853. {
  1854. struct dm_table *map = NULL;
  1855. int r = 0;
  1856. retry:
  1857. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  1858. if (dm_suspended_md(md)) {
  1859. r = -EINVAL;
  1860. goto out_unlock;
  1861. }
  1862. if (dm_suspended_internally_md(md)) {
  1863. /* already internally suspended, wait for internal resume */
  1864. mutex_unlock(&md->suspend_lock);
  1865. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  1866. if (r)
  1867. return r;
  1868. goto retry;
  1869. }
  1870. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1871. r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
  1872. if (r)
  1873. goto out_unlock;
  1874. dm_table_postsuspend_targets(map);
  1875. out_unlock:
  1876. mutex_unlock(&md->suspend_lock);
  1877. return r;
  1878. }
  1879. static int __dm_resume(struct mapped_device *md, struct dm_table *map)
  1880. {
  1881. if (map) {
  1882. int r = dm_table_resume_targets(map);
  1883. if (r)
  1884. return r;
  1885. }
  1886. dm_queue_flush(md);
  1887. /*
  1888. * Flushing deferred I/Os must be done after targets are resumed
  1889. * so that mapping of targets can work correctly.
  1890. * Request-based dm is queueing the deferred I/Os in its request_queue.
  1891. */
  1892. if (dm_request_based(md))
  1893. dm_start_queue(md->queue);
  1894. unlock_fs(md);
  1895. return 0;
  1896. }
  1897. int dm_resume(struct mapped_device *md)
  1898. {
  1899. int r;
  1900. struct dm_table *map = NULL;
  1901. retry:
  1902. r = -EINVAL;
  1903. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  1904. if (!dm_suspended_md(md))
  1905. goto out;
  1906. if (dm_suspended_internally_md(md)) {
  1907. /* already internally suspended, wait for internal resume */
  1908. mutex_unlock(&md->suspend_lock);
  1909. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  1910. if (r)
  1911. return r;
  1912. goto retry;
  1913. }
  1914. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1915. if (!map || !dm_table_get_size(map))
  1916. goto out;
  1917. r = __dm_resume(md, map);
  1918. if (r)
  1919. goto out;
  1920. clear_bit(DMF_SUSPENDED, &md->flags);
  1921. out:
  1922. mutex_unlock(&md->suspend_lock);
  1923. return r;
  1924. }
  1925. /*
  1926. * Internal suspend/resume works like userspace-driven suspend. It waits
  1927. * until all bios finish and prevents issuing new bios to the target drivers.
  1928. * It may be used only from the kernel.
  1929. */
  1930. static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
  1931. {
  1932. struct dm_table *map = NULL;
  1933. if (md->internal_suspend_count++)
  1934. return; /* nested internal suspend */
  1935. if (dm_suspended_md(md)) {
  1936. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  1937. return; /* nest suspend */
  1938. }
  1939. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1940. /*
  1941. * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
  1942. * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
  1943. * would require changing .presuspend to return an error -- avoid this
  1944. * until there is a need for more elaborate variants of internal suspend.
  1945. */
  1946. (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
  1947. DMF_SUSPENDED_INTERNALLY);
  1948. dm_table_postsuspend_targets(map);
  1949. }
  1950. static void __dm_internal_resume(struct mapped_device *md)
  1951. {
  1952. BUG_ON(!md->internal_suspend_count);
  1953. if (--md->internal_suspend_count)
  1954. return; /* resume from nested internal suspend */
  1955. if (dm_suspended_md(md))
  1956. goto done; /* resume from nested suspend */
  1957. /*
  1958. * NOTE: existing callers don't need to call dm_table_resume_targets
  1959. * (which may fail -- so best to avoid it for now by passing NULL map)
  1960. */
  1961. (void) __dm_resume(md, NULL);
  1962. done:
  1963. clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  1964. smp_mb__after_atomic();
  1965. wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
  1966. }
  1967. void dm_internal_suspend_noflush(struct mapped_device *md)
  1968. {
  1969. mutex_lock(&md->suspend_lock);
  1970. __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
  1971. mutex_unlock(&md->suspend_lock);
  1972. }
  1973. EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
  1974. void dm_internal_resume(struct mapped_device *md)
  1975. {
  1976. mutex_lock(&md->suspend_lock);
  1977. __dm_internal_resume(md);
  1978. mutex_unlock(&md->suspend_lock);
  1979. }
  1980. EXPORT_SYMBOL_GPL(dm_internal_resume);
  1981. /*
  1982. * Fast variants of internal suspend/resume hold md->suspend_lock,
  1983. * which prevents interaction with userspace-driven suspend.
  1984. */
  1985. void dm_internal_suspend_fast(struct mapped_device *md)
  1986. {
  1987. mutex_lock(&md->suspend_lock);
  1988. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  1989. return;
  1990. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1991. synchronize_srcu(&md->io_barrier);
  1992. flush_workqueue(md->wq);
  1993. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1994. }
  1995. EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
  1996. void dm_internal_resume_fast(struct mapped_device *md)
  1997. {
  1998. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  1999. goto done;
  2000. dm_queue_flush(md);
  2001. done:
  2002. mutex_unlock(&md->suspend_lock);
  2003. }
  2004. EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
  2005. /*-----------------------------------------------------------------
  2006. * Event notification.
  2007. *---------------------------------------------------------------*/
  2008. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2009. unsigned cookie)
  2010. {
  2011. char udev_cookie[DM_COOKIE_LENGTH];
  2012. char *envp[] = { udev_cookie, NULL };
  2013. if (!cookie)
  2014. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2015. else {
  2016. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2017. DM_COOKIE_ENV_VAR_NAME, cookie);
  2018. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2019. action, envp);
  2020. }
  2021. }
  2022. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2023. {
  2024. return atomic_add_return(1, &md->uevent_seq);
  2025. }
  2026. uint32_t dm_get_event_nr(struct mapped_device *md)
  2027. {
  2028. return atomic_read(&md->event_nr);
  2029. }
  2030. int dm_wait_event(struct mapped_device *md, int event_nr)
  2031. {
  2032. return wait_event_interruptible(md->eventq,
  2033. (event_nr != atomic_read(&md->event_nr)));
  2034. }
  2035. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2036. {
  2037. unsigned long flags;
  2038. spin_lock_irqsave(&md->uevent_lock, flags);
  2039. list_add(elist, &md->uevent_list);
  2040. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2041. }
  2042. /*
  2043. * The gendisk is only valid as long as you have a reference
  2044. * count on 'md'.
  2045. */
  2046. struct gendisk *dm_disk(struct mapped_device *md)
  2047. {
  2048. return md->disk;
  2049. }
  2050. EXPORT_SYMBOL_GPL(dm_disk);
  2051. struct kobject *dm_kobject(struct mapped_device *md)
  2052. {
  2053. return &md->kobj_holder.kobj;
  2054. }
  2055. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2056. {
  2057. struct mapped_device *md;
  2058. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2059. if (test_bit(DMF_FREEING, &md->flags) ||
  2060. dm_deleting_md(md))
  2061. return NULL;
  2062. dm_get(md);
  2063. return md;
  2064. }
  2065. int dm_suspended_md(struct mapped_device *md)
  2066. {
  2067. return test_bit(DMF_SUSPENDED, &md->flags);
  2068. }
  2069. int dm_suspended_internally_md(struct mapped_device *md)
  2070. {
  2071. return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2072. }
  2073. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2074. {
  2075. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2076. }
  2077. int dm_suspended(struct dm_target *ti)
  2078. {
  2079. return dm_suspended_md(dm_table_get_md(ti->table));
  2080. }
  2081. EXPORT_SYMBOL_GPL(dm_suspended);
  2082. int dm_noflush_suspending(struct dm_target *ti)
  2083. {
  2084. return __noflush_suspending(dm_table_get_md(ti->table));
  2085. }
  2086. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2087. struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
  2088. unsigned integrity, unsigned per_io_data_size)
  2089. {
  2090. struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
  2091. unsigned int pool_size = 0;
  2092. unsigned int front_pad;
  2093. if (!pools)
  2094. return NULL;
  2095. switch (type) {
  2096. case DM_TYPE_BIO_BASED:
  2097. case DM_TYPE_DAX_BIO_BASED:
  2098. pool_size = dm_get_reserved_bio_based_ios();
  2099. front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2100. pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
  2101. if (!pools->io_pool)
  2102. goto out;
  2103. break;
  2104. case DM_TYPE_REQUEST_BASED:
  2105. case DM_TYPE_MQ_REQUEST_BASED:
  2106. pool_size = dm_get_reserved_rq_based_ios();
  2107. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2108. /* per_io_data_size is used for blk-mq pdu at queue allocation */
  2109. break;
  2110. default:
  2111. BUG();
  2112. }
  2113. pools->bs = bioset_create_nobvec(pool_size, front_pad);
  2114. if (!pools->bs)
  2115. goto out;
  2116. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2117. goto out;
  2118. return pools;
  2119. out:
  2120. dm_free_md_mempools(pools);
  2121. return NULL;
  2122. }
  2123. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2124. {
  2125. if (!pools)
  2126. return;
  2127. mempool_destroy(pools->io_pool);
  2128. if (pools->bs)
  2129. bioset_free(pools->bs);
  2130. kfree(pools);
  2131. }
  2132. struct dm_pr {
  2133. u64 old_key;
  2134. u64 new_key;
  2135. u32 flags;
  2136. bool fail_early;
  2137. };
  2138. static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
  2139. void *data)
  2140. {
  2141. struct mapped_device *md = bdev->bd_disk->private_data;
  2142. struct dm_table *table;
  2143. struct dm_target *ti;
  2144. int ret = -ENOTTY, srcu_idx;
  2145. table = dm_get_live_table(md, &srcu_idx);
  2146. if (!table || !dm_table_get_size(table))
  2147. goto out;
  2148. /* We only support devices that have a single target */
  2149. if (dm_table_get_num_targets(table) != 1)
  2150. goto out;
  2151. ti = dm_table_get_target(table, 0);
  2152. ret = -EINVAL;
  2153. if (!ti->type->iterate_devices)
  2154. goto out;
  2155. ret = ti->type->iterate_devices(ti, fn, data);
  2156. out:
  2157. dm_put_live_table(md, srcu_idx);
  2158. return ret;
  2159. }
  2160. /*
  2161. * For register / unregister we need to manually call out to every path.
  2162. */
  2163. static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
  2164. sector_t start, sector_t len, void *data)
  2165. {
  2166. struct dm_pr *pr = data;
  2167. const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
  2168. if (!ops || !ops->pr_register)
  2169. return -EOPNOTSUPP;
  2170. return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
  2171. }
  2172. static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  2173. u32 flags)
  2174. {
  2175. struct dm_pr pr = {
  2176. .old_key = old_key,
  2177. .new_key = new_key,
  2178. .flags = flags,
  2179. .fail_early = true,
  2180. };
  2181. int ret;
  2182. ret = dm_call_pr(bdev, __dm_pr_register, &pr);
  2183. if (ret && new_key) {
  2184. /* unregister all paths if we failed to register any path */
  2185. pr.old_key = new_key;
  2186. pr.new_key = 0;
  2187. pr.flags = 0;
  2188. pr.fail_early = false;
  2189. dm_call_pr(bdev, __dm_pr_register, &pr);
  2190. }
  2191. return ret;
  2192. }
  2193. static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  2194. u32 flags)
  2195. {
  2196. struct mapped_device *md = bdev->bd_disk->private_data;
  2197. const struct pr_ops *ops;
  2198. fmode_t mode;
  2199. int r;
  2200. r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
  2201. if (r < 0)
  2202. return r;
  2203. ops = bdev->bd_disk->fops->pr_ops;
  2204. if (ops && ops->pr_reserve)
  2205. r = ops->pr_reserve(bdev, key, type, flags);
  2206. else
  2207. r = -EOPNOTSUPP;
  2208. bdput(bdev);
  2209. return r;
  2210. }
  2211. static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  2212. {
  2213. struct mapped_device *md = bdev->bd_disk->private_data;
  2214. const struct pr_ops *ops;
  2215. fmode_t mode;
  2216. int r;
  2217. r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
  2218. if (r < 0)
  2219. return r;
  2220. ops = bdev->bd_disk->fops->pr_ops;
  2221. if (ops && ops->pr_release)
  2222. r = ops->pr_release(bdev, key, type);
  2223. else
  2224. r = -EOPNOTSUPP;
  2225. bdput(bdev);
  2226. return r;
  2227. }
  2228. static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  2229. enum pr_type type, bool abort)
  2230. {
  2231. struct mapped_device *md = bdev->bd_disk->private_data;
  2232. const struct pr_ops *ops;
  2233. fmode_t mode;
  2234. int r;
  2235. r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
  2236. if (r < 0)
  2237. return r;
  2238. ops = bdev->bd_disk->fops->pr_ops;
  2239. if (ops && ops->pr_preempt)
  2240. r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
  2241. else
  2242. r = -EOPNOTSUPP;
  2243. bdput(bdev);
  2244. return r;
  2245. }
  2246. static int dm_pr_clear(struct block_device *bdev, u64 key)
  2247. {
  2248. struct mapped_device *md = bdev->bd_disk->private_data;
  2249. const struct pr_ops *ops;
  2250. fmode_t mode;
  2251. int r;
  2252. r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
  2253. if (r < 0)
  2254. return r;
  2255. ops = bdev->bd_disk->fops->pr_ops;
  2256. if (ops && ops->pr_clear)
  2257. r = ops->pr_clear(bdev, key);
  2258. else
  2259. r = -EOPNOTSUPP;
  2260. bdput(bdev);
  2261. return r;
  2262. }
  2263. static const struct pr_ops dm_pr_ops = {
  2264. .pr_register = dm_pr_register,
  2265. .pr_reserve = dm_pr_reserve,
  2266. .pr_release = dm_pr_release,
  2267. .pr_preempt = dm_pr_preempt,
  2268. .pr_clear = dm_pr_clear,
  2269. };
  2270. static const struct block_device_operations dm_blk_dops = {
  2271. .open = dm_blk_open,
  2272. .release = dm_blk_close,
  2273. .ioctl = dm_blk_ioctl,
  2274. .direct_access = dm_blk_direct_access,
  2275. .getgeo = dm_blk_getgeo,
  2276. .pr_ops = &dm_pr_ops,
  2277. .owner = THIS_MODULE
  2278. };
  2279. /*
  2280. * module hooks
  2281. */
  2282. module_init(dm_init);
  2283. module_exit(dm_exit);
  2284. module_param(major, uint, 0);
  2285. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2286. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  2287. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  2288. module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
  2289. MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
  2290. MODULE_DESCRIPTION(DM_NAME " driver");
  2291. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2292. MODULE_LICENSE("GPL");