dm.c 64 KB

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