dm.c 62 KB

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