dm.c 73 KB

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