dm.c 76 KB

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