dm.c 63 KB

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