dm-table.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * Copyright (C) 2001 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-core.h"
  8. #include <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/namei.h>
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/atomic.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/mount.h>
  21. #define DM_MSG_PREFIX "table"
  22. #define MAX_DEPTH 16
  23. #define NODE_SIZE L1_CACHE_BYTES
  24. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  25. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  26. struct dm_table {
  27. struct mapped_device *md;
  28. enum dm_queue_mode type;
  29. /* btree table */
  30. unsigned int depth;
  31. unsigned int counts[MAX_DEPTH]; /* in nodes */
  32. sector_t *index[MAX_DEPTH];
  33. unsigned int num_targets;
  34. unsigned int num_allocated;
  35. sector_t *highs;
  36. struct dm_target *targets;
  37. struct target_type *immutable_target_type;
  38. bool integrity_supported:1;
  39. bool singleton:1;
  40. bool all_blk_mq:1;
  41. unsigned integrity_added:1;
  42. /*
  43. * Indicates the rw permissions for the new logical
  44. * device. This should be a combination of FMODE_READ
  45. * and FMODE_WRITE.
  46. */
  47. fmode_t mode;
  48. /* a list of devices used by this table */
  49. struct list_head devices;
  50. /* events get handed up using this callback */
  51. void (*event_fn)(void *);
  52. void *event_context;
  53. struct dm_md_mempools *mempools;
  54. struct list_head target_callbacks;
  55. };
  56. /*
  57. * Similar to ceiling(log_size(n))
  58. */
  59. static unsigned int int_log(unsigned int n, unsigned int base)
  60. {
  61. int result = 0;
  62. while (n > 1) {
  63. n = dm_div_up(n, base);
  64. result++;
  65. }
  66. return result;
  67. }
  68. /*
  69. * Calculate the index of the child node of the n'th node k'th key.
  70. */
  71. static inline unsigned int get_child(unsigned int n, unsigned int k)
  72. {
  73. return (n * CHILDREN_PER_NODE) + k;
  74. }
  75. /*
  76. * Return the n'th node of level l from table t.
  77. */
  78. static inline sector_t *get_node(struct dm_table *t,
  79. unsigned int l, unsigned int n)
  80. {
  81. return t->index[l] + (n * KEYS_PER_NODE);
  82. }
  83. /*
  84. * Return the highest key that you could lookup from the n'th
  85. * node on level l of the btree.
  86. */
  87. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  88. {
  89. for (; l < t->depth - 1; l++)
  90. n = get_child(n, CHILDREN_PER_NODE - 1);
  91. if (n >= t->counts[l])
  92. return (sector_t) - 1;
  93. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  94. }
  95. /*
  96. * Fills in a level of the btree based on the highs of the level
  97. * below it.
  98. */
  99. static int setup_btree_index(unsigned int l, struct dm_table *t)
  100. {
  101. unsigned int n, k;
  102. sector_t *node;
  103. for (n = 0U; n < t->counts[l]; n++) {
  104. node = get_node(t, l, n);
  105. for (k = 0U; k < KEYS_PER_NODE; k++)
  106. node[k] = high(t, l + 1, get_child(n, k));
  107. }
  108. return 0;
  109. }
  110. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  111. {
  112. unsigned long size;
  113. void *addr;
  114. /*
  115. * Check that we're not going to overflow.
  116. */
  117. if (nmemb > (ULONG_MAX / elem_size))
  118. return NULL;
  119. size = nmemb * elem_size;
  120. addr = vzalloc(size);
  121. return addr;
  122. }
  123. EXPORT_SYMBOL(dm_vcalloc);
  124. /*
  125. * highs, and targets are managed as dynamic arrays during a
  126. * table load.
  127. */
  128. static int alloc_targets(struct dm_table *t, unsigned int num)
  129. {
  130. sector_t *n_highs;
  131. struct dm_target *n_targets;
  132. /*
  133. * Allocate both the target array and offset array at once.
  134. * Append an empty entry to catch sectors beyond the end of
  135. * the device.
  136. */
  137. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  138. sizeof(sector_t));
  139. if (!n_highs)
  140. return -ENOMEM;
  141. n_targets = (struct dm_target *) (n_highs + num);
  142. memset(n_highs, -1, sizeof(*n_highs) * num);
  143. vfree(t->highs);
  144. t->num_allocated = num;
  145. t->highs = n_highs;
  146. t->targets = n_targets;
  147. return 0;
  148. }
  149. int dm_table_create(struct dm_table **result, fmode_t mode,
  150. unsigned num_targets, struct mapped_device *md)
  151. {
  152. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  153. if (!t)
  154. return -ENOMEM;
  155. INIT_LIST_HEAD(&t->devices);
  156. INIT_LIST_HEAD(&t->target_callbacks);
  157. if (!num_targets)
  158. num_targets = KEYS_PER_NODE;
  159. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  160. if (!num_targets) {
  161. kfree(t);
  162. return -ENOMEM;
  163. }
  164. if (alloc_targets(t, num_targets)) {
  165. kfree(t);
  166. return -ENOMEM;
  167. }
  168. t->type = DM_TYPE_NONE;
  169. t->mode = mode;
  170. t->md = md;
  171. *result = t;
  172. return 0;
  173. }
  174. static void free_devices(struct list_head *devices, struct mapped_device *md)
  175. {
  176. struct list_head *tmp, *next;
  177. list_for_each_safe(tmp, next, devices) {
  178. struct dm_dev_internal *dd =
  179. list_entry(tmp, struct dm_dev_internal, list);
  180. DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
  181. dm_device_name(md), dd->dm_dev->name);
  182. dm_put_table_device(md, dd->dm_dev);
  183. kfree(dd);
  184. }
  185. }
  186. void dm_table_destroy(struct dm_table *t)
  187. {
  188. unsigned int i;
  189. if (!t)
  190. return;
  191. /* free the indexes */
  192. if (t->depth >= 2)
  193. vfree(t->index[t->depth - 2]);
  194. /* free the targets */
  195. for (i = 0; i < t->num_targets; i++) {
  196. struct dm_target *tgt = t->targets + i;
  197. if (tgt->type->dtr)
  198. tgt->type->dtr(tgt);
  199. dm_put_target_type(tgt->type);
  200. }
  201. vfree(t->highs);
  202. /* free the device list */
  203. free_devices(&t->devices, t->md);
  204. dm_free_md_mempools(t->mempools);
  205. kfree(t);
  206. }
  207. /*
  208. * See if we've already got a device in the list.
  209. */
  210. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  211. {
  212. struct dm_dev_internal *dd;
  213. list_for_each_entry (dd, l, list)
  214. if (dd->dm_dev->bdev->bd_dev == dev)
  215. return dd;
  216. return NULL;
  217. }
  218. /*
  219. * If possible, this checks an area of a destination device is invalid.
  220. */
  221. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  222. sector_t start, sector_t len, void *data)
  223. {
  224. struct request_queue *q;
  225. struct queue_limits *limits = data;
  226. struct block_device *bdev = dev->bdev;
  227. sector_t dev_size =
  228. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  229. unsigned short logical_block_size_sectors =
  230. limits->logical_block_size >> SECTOR_SHIFT;
  231. char b[BDEVNAME_SIZE];
  232. /*
  233. * Some devices exist without request functions,
  234. * such as loop devices not yet bound to backing files.
  235. * Forbid the use of such devices.
  236. */
  237. q = bdev_get_queue(bdev);
  238. if (!q || !q->make_request_fn) {
  239. DMWARN("%s: %s is not yet initialised: "
  240. "start=%llu, len=%llu, dev_size=%llu",
  241. dm_device_name(ti->table->md), bdevname(bdev, b),
  242. (unsigned long long)start,
  243. (unsigned long long)len,
  244. (unsigned long long)dev_size);
  245. return 1;
  246. }
  247. if (!dev_size)
  248. return 0;
  249. if ((start >= dev_size) || (start + len > dev_size)) {
  250. DMWARN("%s: %s too small for target: "
  251. "start=%llu, len=%llu, dev_size=%llu",
  252. dm_device_name(ti->table->md), bdevname(bdev, b),
  253. (unsigned long long)start,
  254. (unsigned long long)len,
  255. (unsigned long long)dev_size);
  256. return 1;
  257. }
  258. if (logical_block_size_sectors <= 1)
  259. return 0;
  260. if (start & (logical_block_size_sectors - 1)) {
  261. DMWARN("%s: start=%llu not aligned to h/w "
  262. "logical block size %u of %s",
  263. dm_device_name(ti->table->md),
  264. (unsigned long long)start,
  265. limits->logical_block_size, bdevname(bdev, b));
  266. return 1;
  267. }
  268. if (len & (logical_block_size_sectors - 1)) {
  269. DMWARN("%s: len=%llu not aligned to h/w "
  270. "logical block size %u of %s",
  271. dm_device_name(ti->table->md),
  272. (unsigned long long)len,
  273. limits->logical_block_size, bdevname(bdev, b));
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. /*
  279. * This upgrades the mode on an already open dm_dev, being
  280. * careful to leave things as they were if we fail to reopen the
  281. * device and not to touch the existing bdev field in case
  282. * it is accessed concurrently inside dm_table_any_congested().
  283. */
  284. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  285. struct mapped_device *md)
  286. {
  287. int r;
  288. struct dm_dev *old_dev, *new_dev;
  289. old_dev = dd->dm_dev;
  290. r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
  291. dd->dm_dev->mode | new_mode, &new_dev);
  292. if (r)
  293. return r;
  294. dd->dm_dev = new_dev;
  295. dm_put_table_device(md, old_dev);
  296. return 0;
  297. }
  298. /*
  299. * Convert the path to a device
  300. */
  301. dev_t dm_get_dev_t(const char *path)
  302. {
  303. dev_t dev;
  304. struct block_device *bdev;
  305. bdev = lookup_bdev(path);
  306. if (IS_ERR(bdev))
  307. dev = name_to_dev_t(path);
  308. else {
  309. dev = bdev->bd_dev;
  310. bdput(bdev);
  311. }
  312. return dev;
  313. }
  314. EXPORT_SYMBOL_GPL(dm_get_dev_t);
  315. /*
  316. * Add a device to the list, or just increment the usage count if
  317. * it's already present.
  318. */
  319. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  320. struct dm_dev **result)
  321. {
  322. int r;
  323. dev_t dev;
  324. struct dm_dev_internal *dd;
  325. struct dm_table *t = ti->table;
  326. BUG_ON(!t);
  327. dev = dm_get_dev_t(path);
  328. if (!dev)
  329. return -ENODEV;
  330. dd = find_device(&t->devices, dev);
  331. if (!dd) {
  332. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  333. if (!dd)
  334. return -ENOMEM;
  335. if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
  336. kfree(dd);
  337. return r;
  338. }
  339. atomic_set(&dd->count, 0);
  340. list_add(&dd->list, &t->devices);
  341. } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
  342. r = upgrade_mode(dd, mode, t->md);
  343. if (r)
  344. return r;
  345. }
  346. atomic_inc(&dd->count);
  347. *result = dd->dm_dev;
  348. return 0;
  349. }
  350. EXPORT_SYMBOL(dm_get_device);
  351. static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  352. sector_t start, sector_t len, void *data)
  353. {
  354. struct queue_limits *limits = data;
  355. struct block_device *bdev = dev->bdev;
  356. struct request_queue *q = bdev_get_queue(bdev);
  357. char b[BDEVNAME_SIZE];
  358. if (unlikely(!q)) {
  359. DMWARN("%s: Cannot set limits for nonexistent device %s",
  360. dm_device_name(ti->table->md), bdevname(bdev, b));
  361. return 0;
  362. }
  363. if (bdev_stack_limits(limits, bdev, start) < 0)
  364. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  365. "physical_block_size=%u, logical_block_size=%u, "
  366. "alignment_offset=%u, start=%llu",
  367. dm_device_name(ti->table->md), bdevname(bdev, b),
  368. q->limits.physical_block_size,
  369. q->limits.logical_block_size,
  370. q->limits.alignment_offset,
  371. (unsigned long long) start << SECTOR_SHIFT);
  372. return 0;
  373. }
  374. /*
  375. * Decrement a device's use count and remove it if necessary.
  376. */
  377. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  378. {
  379. int found = 0;
  380. struct list_head *devices = &ti->table->devices;
  381. struct dm_dev_internal *dd;
  382. list_for_each_entry(dd, devices, list) {
  383. if (dd->dm_dev == d) {
  384. found = 1;
  385. break;
  386. }
  387. }
  388. if (!found) {
  389. DMWARN("%s: device %s not in table devices list",
  390. dm_device_name(ti->table->md), d->name);
  391. return;
  392. }
  393. if (atomic_dec_and_test(&dd->count)) {
  394. dm_put_table_device(ti->table->md, d);
  395. list_del(&dd->list);
  396. kfree(dd);
  397. }
  398. }
  399. EXPORT_SYMBOL(dm_put_device);
  400. /*
  401. * Checks to see if the target joins onto the end of the table.
  402. */
  403. static int adjoin(struct dm_table *table, struct dm_target *ti)
  404. {
  405. struct dm_target *prev;
  406. if (!table->num_targets)
  407. return !ti->begin;
  408. prev = &table->targets[table->num_targets - 1];
  409. return (ti->begin == (prev->begin + prev->len));
  410. }
  411. /*
  412. * Used to dynamically allocate the arg array.
  413. *
  414. * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
  415. * process messages even if some device is suspended. These messages have a
  416. * small fixed number of arguments.
  417. *
  418. * On the other hand, dm-switch needs to process bulk data using messages and
  419. * excessive use of GFP_NOIO could cause trouble.
  420. */
  421. static char **realloc_argv(unsigned *array_size, char **old_argv)
  422. {
  423. char **argv;
  424. unsigned new_size;
  425. gfp_t gfp;
  426. if (*array_size) {
  427. new_size = *array_size * 2;
  428. gfp = GFP_KERNEL;
  429. } else {
  430. new_size = 8;
  431. gfp = GFP_NOIO;
  432. }
  433. argv = kmalloc(new_size * sizeof(*argv), gfp);
  434. if (argv) {
  435. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  436. *array_size = new_size;
  437. }
  438. kfree(old_argv);
  439. return argv;
  440. }
  441. /*
  442. * Destructively splits up the argument list to pass to ctr.
  443. */
  444. int dm_split_args(int *argc, char ***argvp, char *input)
  445. {
  446. char *start, *end = input, *out, **argv = NULL;
  447. unsigned array_size = 0;
  448. *argc = 0;
  449. if (!input) {
  450. *argvp = NULL;
  451. return 0;
  452. }
  453. argv = realloc_argv(&array_size, argv);
  454. if (!argv)
  455. return -ENOMEM;
  456. while (1) {
  457. /* Skip whitespace */
  458. start = skip_spaces(end);
  459. if (!*start)
  460. break; /* success, we hit the end */
  461. /* 'out' is used to remove any back-quotes */
  462. end = out = start;
  463. while (*end) {
  464. /* Everything apart from '\0' can be quoted */
  465. if (*end == '\\' && *(end + 1)) {
  466. *out++ = *(end + 1);
  467. end += 2;
  468. continue;
  469. }
  470. if (isspace(*end))
  471. break; /* end of token */
  472. *out++ = *end++;
  473. }
  474. /* have we already filled the array ? */
  475. if ((*argc + 1) > array_size) {
  476. argv = realloc_argv(&array_size, argv);
  477. if (!argv)
  478. return -ENOMEM;
  479. }
  480. /* we know this is whitespace */
  481. if (*end)
  482. end++;
  483. /* terminate the string and put it in the array */
  484. *out = '\0';
  485. argv[*argc] = start;
  486. (*argc)++;
  487. }
  488. *argvp = argv;
  489. return 0;
  490. }
  491. /*
  492. * Impose necessary and sufficient conditions on a devices's table such
  493. * that any incoming bio which respects its logical_block_size can be
  494. * processed successfully. If it falls across the boundary between
  495. * two or more targets, the size of each piece it gets split into must
  496. * be compatible with the logical_block_size of the target processing it.
  497. */
  498. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  499. struct queue_limits *limits)
  500. {
  501. /*
  502. * This function uses arithmetic modulo the logical_block_size
  503. * (in units of 512-byte sectors).
  504. */
  505. unsigned short device_logical_block_size_sects =
  506. limits->logical_block_size >> SECTOR_SHIFT;
  507. /*
  508. * Offset of the start of the next table entry, mod logical_block_size.
  509. */
  510. unsigned short next_target_start = 0;
  511. /*
  512. * Given an aligned bio that extends beyond the end of a
  513. * target, how many sectors must the next target handle?
  514. */
  515. unsigned short remaining = 0;
  516. struct dm_target *uninitialized_var(ti);
  517. struct queue_limits ti_limits;
  518. unsigned i;
  519. /*
  520. * Check each entry in the table in turn.
  521. */
  522. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  523. ti = dm_table_get_target(table, i);
  524. blk_set_stacking_limits(&ti_limits);
  525. /* combine all target devices' limits */
  526. if (ti->type->iterate_devices)
  527. ti->type->iterate_devices(ti, dm_set_device_limits,
  528. &ti_limits);
  529. /*
  530. * If the remaining sectors fall entirely within this
  531. * table entry are they compatible with its logical_block_size?
  532. */
  533. if (remaining < ti->len &&
  534. remaining & ((ti_limits.logical_block_size >>
  535. SECTOR_SHIFT) - 1))
  536. break; /* Error */
  537. next_target_start =
  538. (unsigned short) ((next_target_start + ti->len) &
  539. (device_logical_block_size_sects - 1));
  540. remaining = next_target_start ?
  541. device_logical_block_size_sects - next_target_start : 0;
  542. }
  543. if (remaining) {
  544. DMWARN("%s: table line %u (start sect %llu len %llu) "
  545. "not aligned to h/w logical block size %u",
  546. dm_device_name(table->md), i,
  547. (unsigned long long) ti->begin,
  548. (unsigned long long) ti->len,
  549. limits->logical_block_size);
  550. return -EINVAL;
  551. }
  552. return 0;
  553. }
  554. int dm_table_add_target(struct dm_table *t, const char *type,
  555. sector_t start, sector_t len, char *params)
  556. {
  557. int r = -EINVAL, argc;
  558. char **argv;
  559. struct dm_target *tgt;
  560. if (t->singleton) {
  561. DMERR("%s: target type %s must appear alone in table",
  562. dm_device_name(t->md), t->targets->type->name);
  563. return -EINVAL;
  564. }
  565. BUG_ON(t->num_targets >= t->num_allocated);
  566. tgt = t->targets + t->num_targets;
  567. memset(tgt, 0, sizeof(*tgt));
  568. if (!len) {
  569. DMERR("%s: zero-length target", dm_device_name(t->md));
  570. return -EINVAL;
  571. }
  572. tgt->type = dm_get_target_type(type);
  573. if (!tgt->type) {
  574. DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
  575. return -EINVAL;
  576. }
  577. if (dm_target_needs_singleton(tgt->type)) {
  578. if (t->num_targets) {
  579. tgt->error = "singleton target type must appear alone in table";
  580. goto bad;
  581. }
  582. t->singleton = true;
  583. }
  584. if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
  585. tgt->error = "target type may not be included in a read-only table";
  586. goto bad;
  587. }
  588. if (t->immutable_target_type) {
  589. if (t->immutable_target_type != tgt->type) {
  590. tgt->error = "immutable target type cannot be mixed with other target types";
  591. goto bad;
  592. }
  593. } else if (dm_target_is_immutable(tgt->type)) {
  594. if (t->num_targets) {
  595. tgt->error = "immutable target type cannot be mixed with other target types";
  596. goto bad;
  597. }
  598. t->immutable_target_type = tgt->type;
  599. }
  600. if (dm_target_has_integrity(tgt->type))
  601. t->integrity_added = 1;
  602. tgt->table = t;
  603. tgt->begin = start;
  604. tgt->len = len;
  605. tgt->error = "Unknown error";
  606. /*
  607. * Does this target adjoin the previous one ?
  608. */
  609. if (!adjoin(t, tgt)) {
  610. tgt->error = "Gap in table";
  611. goto bad;
  612. }
  613. r = dm_split_args(&argc, &argv, params);
  614. if (r) {
  615. tgt->error = "couldn't split parameters (insufficient memory)";
  616. goto bad;
  617. }
  618. r = tgt->type->ctr(tgt, argc, argv);
  619. kfree(argv);
  620. if (r)
  621. goto bad;
  622. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  623. if (!tgt->num_discard_bios && tgt->discards_supported)
  624. DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
  625. dm_device_name(t->md), type);
  626. return 0;
  627. bad:
  628. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  629. dm_put_target_type(tgt->type);
  630. return r;
  631. }
  632. /*
  633. * Target argument parsing helpers.
  634. */
  635. static int validate_next_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  636. unsigned *value, char **error, unsigned grouped)
  637. {
  638. const char *arg_str = dm_shift_arg(arg_set);
  639. char dummy;
  640. if (!arg_str ||
  641. (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
  642. (*value < arg->min) ||
  643. (*value > arg->max) ||
  644. (grouped && arg_set->argc < *value)) {
  645. *error = arg->error;
  646. return -EINVAL;
  647. }
  648. return 0;
  649. }
  650. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  651. unsigned *value, char **error)
  652. {
  653. return validate_next_arg(arg, arg_set, value, error, 0);
  654. }
  655. EXPORT_SYMBOL(dm_read_arg);
  656. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  657. unsigned *value, char **error)
  658. {
  659. return validate_next_arg(arg, arg_set, value, error, 1);
  660. }
  661. EXPORT_SYMBOL(dm_read_arg_group);
  662. const char *dm_shift_arg(struct dm_arg_set *as)
  663. {
  664. char *r;
  665. if (as->argc) {
  666. as->argc--;
  667. r = *as->argv;
  668. as->argv++;
  669. return r;
  670. }
  671. return NULL;
  672. }
  673. EXPORT_SYMBOL(dm_shift_arg);
  674. void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
  675. {
  676. BUG_ON(as->argc < num_args);
  677. as->argc -= num_args;
  678. as->argv += num_args;
  679. }
  680. EXPORT_SYMBOL(dm_consume_args);
  681. static bool __table_type_bio_based(enum dm_queue_mode table_type)
  682. {
  683. return (table_type == DM_TYPE_BIO_BASED ||
  684. table_type == DM_TYPE_DAX_BIO_BASED);
  685. }
  686. static bool __table_type_request_based(enum dm_queue_mode table_type)
  687. {
  688. return (table_type == DM_TYPE_REQUEST_BASED ||
  689. table_type == DM_TYPE_MQ_REQUEST_BASED);
  690. }
  691. void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
  692. {
  693. t->type = type;
  694. }
  695. EXPORT_SYMBOL_GPL(dm_table_set_type);
  696. static int device_supports_dax(struct dm_target *ti, struct dm_dev *dev,
  697. sector_t start, sector_t len, void *data)
  698. {
  699. struct request_queue *q = bdev_get_queue(dev->bdev);
  700. return q && blk_queue_dax(q);
  701. }
  702. static bool dm_table_supports_dax(struct dm_table *t)
  703. {
  704. struct dm_target *ti;
  705. unsigned i;
  706. /* Ensure that all targets support DAX. */
  707. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  708. ti = dm_table_get_target(t, i);
  709. if (!ti->type->direct_access)
  710. return false;
  711. if (!ti->type->iterate_devices ||
  712. !ti->type->iterate_devices(ti, device_supports_dax, NULL))
  713. return false;
  714. }
  715. return true;
  716. }
  717. static int dm_table_determine_type(struct dm_table *t)
  718. {
  719. unsigned i;
  720. unsigned bio_based = 0, request_based = 0, hybrid = 0;
  721. unsigned sq_count = 0, mq_count = 0;
  722. struct dm_target *tgt;
  723. struct dm_dev_internal *dd;
  724. struct list_head *devices = dm_table_get_devices(t);
  725. enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
  726. if (t->type != DM_TYPE_NONE) {
  727. /* target already set the table's type */
  728. if (t->type == DM_TYPE_BIO_BASED)
  729. return 0;
  730. BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
  731. goto verify_rq_based;
  732. }
  733. for (i = 0; i < t->num_targets; i++) {
  734. tgt = t->targets + i;
  735. if (dm_target_hybrid(tgt))
  736. hybrid = 1;
  737. else if (dm_target_request_based(tgt))
  738. request_based = 1;
  739. else
  740. bio_based = 1;
  741. if (bio_based && request_based) {
  742. DMWARN("Inconsistent table: different target types"
  743. " can't be mixed up");
  744. return -EINVAL;
  745. }
  746. }
  747. if (hybrid && !bio_based && !request_based) {
  748. /*
  749. * The targets can work either way.
  750. * Determine the type from the live device.
  751. * Default to bio-based if device is new.
  752. */
  753. if (__table_type_request_based(live_md_type))
  754. request_based = 1;
  755. else
  756. bio_based = 1;
  757. }
  758. if (bio_based) {
  759. /* We must use this table as bio-based */
  760. t->type = DM_TYPE_BIO_BASED;
  761. if (dm_table_supports_dax(t) ||
  762. (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED))
  763. t->type = DM_TYPE_DAX_BIO_BASED;
  764. return 0;
  765. }
  766. BUG_ON(!request_based); /* No targets in this table */
  767. /*
  768. * The only way to establish DM_TYPE_MQ_REQUEST_BASED is by
  769. * having a compatible target use dm_table_set_type.
  770. */
  771. t->type = DM_TYPE_REQUEST_BASED;
  772. verify_rq_based:
  773. /*
  774. * Request-based dm supports only tables that have a single target now.
  775. * To support multiple targets, request splitting support is needed,
  776. * and that needs lots of changes in the block-layer.
  777. * (e.g. request completion process for partial completion.)
  778. */
  779. if (t->num_targets > 1) {
  780. DMWARN("Request-based dm doesn't support multiple targets yet");
  781. return -EINVAL;
  782. }
  783. if (list_empty(devices)) {
  784. int srcu_idx;
  785. struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
  786. /* inherit live table's type and all_blk_mq */
  787. if (live_table) {
  788. t->type = live_table->type;
  789. t->all_blk_mq = live_table->all_blk_mq;
  790. }
  791. dm_put_live_table(t->md, srcu_idx);
  792. return 0;
  793. }
  794. /* Non-request-stackable devices can't be used for request-based dm */
  795. list_for_each_entry(dd, devices, list) {
  796. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  797. if (!blk_queue_stackable(q)) {
  798. DMERR("table load rejected: including"
  799. " non-request-stackable devices");
  800. return -EINVAL;
  801. }
  802. if (q->mq_ops)
  803. mq_count++;
  804. else
  805. sq_count++;
  806. }
  807. if (sq_count && mq_count) {
  808. DMERR("table load rejected: not all devices are blk-mq request-stackable");
  809. return -EINVAL;
  810. }
  811. t->all_blk_mq = mq_count > 0;
  812. if (t->type == DM_TYPE_MQ_REQUEST_BASED && !t->all_blk_mq) {
  813. DMERR("table load rejected: all devices are not blk-mq request-stackable");
  814. return -EINVAL;
  815. }
  816. return 0;
  817. }
  818. enum dm_queue_mode dm_table_get_type(struct dm_table *t)
  819. {
  820. return t->type;
  821. }
  822. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  823. {
  824. return t->immutable_target_type;
  825. }
  826. struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
  827. {
  828. /* Immutable target is implicitly a singleton */
  829. if (t->num_targets > 1 ||
  830. !dm_target_is_immutable(t->targets[0].type))
  831. return NULL;
  832. return t->targets;
  833. }
  834. struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
  835. {
  836. struct dm_target *ti;
  837. unsigned i;
  838. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  839. ti = dm_table_get_target(t, i);
  840. if (dm_target_is_wildcard(ti->type))
  841. return ti;
  842. }
  843. return NULL;
  844. }
  845. bool dm_table_bio_based(struct dm_table *t)
  846. {
  847. return __table_type_bio_based(dm_table_get_type(t));
  848. }
  849. bool dm_table_request_based(struct dm_table *t)
  850. {
  851. return __table_type_request_based(dm_table_get_type(t));
  852. }
  853. bool dm_table_all_blk_mq_devices(struct dm_table *t)
  854. {
  855. return t->all_blk_mq;
  856. }
  857. static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
  858. {
  859. enum dm_queue_mode type = dm_table_get_type(t);
  860. unsigned per_io_data_size = 0;
  861. struct dm_target *tgt;
  862. unsigned i;
  863. if (unlikely(type == DM_TYPE_NONE)) {
  864. DMWARN("no table type is set, can't allocate mempools");
  865. return -EINVAL;
  866. }
  867. if (__table_type_bio_based(type))
  868. for (i = 0; i < t->num_targets; i++) {
  869. tgt = t->targets + i;
  870. per_io_data_size = max(per_io_data_size, tgt->per_io_data_size);
  871. }
  872. t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported, per_io_data_size);
  873. if (!t->mempools)
  874. return -ENOMEM;
  875. return 0;
  876. }
  877. void dm_table_free_md_mempools(struct dm_table *t)
  878. {
  879. dm_free_md_mempools(t->mempools);
  880. t->mempools = NULL;
  881. }
  882. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  883. {
  884. return t->mempools;
  885. }
  886. static int setup_indexes(struct dm_table *t)
  887. {
  888. int i;
  889. unsigned int total = 0;
  890. sector_t *indexes;
  891. /* allocate the space for *all* the indexes */
  892. for (i = t->depth - 2; i >= 0; i--) {
  893. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  894. total += t->counts[i];
  895. }
  896. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  897. if (!indexes)
  898. return -ENOMEM;
  899. /* set up internal nodes, bottom-up */
  900. for (i = t->depth - 2; i >= 0; i--) {
  901. t->index[i] = indexes;
  902. indexes += (KEYS_PER_NODE * t->counts[i]);
  903. setup_btree_index(i, t);
  904. }
  905. return 0;
  906. }
  907. /*
  908. * Builds the btree to index the map.
  909. */
  910. static int dm_table_build_index(struct dm_table *t)
  911. {
  912. int r = 0;
  913. unsigned int leaf_nodes;
  914. /* how many indexes will the btree have ? */
  915. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  916. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  917. /* leaf layer has already been set up */
  918. t->counts[t->depth - 1] = leaf_nodes;
  919. t->index[t->depth - 1] = t->highs;
  920. if (t->depth >= 2)
  921. r = setup_indexes(t);
  922. return r;
  923. }
  924. static bool integrity_profile_exists(struct gendisk *disk)
  925. {
  926. return !!blk_get_integrity(disk);
  927. }
  928. /*
  929. * Get a disk whose integrity profile reflects the table's profile.
  930. * Returns NULL if integrity support was inconsistent or unavailable.
  931. */
  932. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
  933. {
  934. struct list_head *devices = dm_table_get_devices(t);
  935. struct dm_dev_internal *dd = NULL;
  936. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  937. unsigned i;
  938. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  939. struct dm_target *ti = dm_table_get_target(t, i);
  940. if (!dm_target_passes_integrity(ti->type))
  941. goto no_integrity;
  942. }
  943. list_for_each_entry(dd, devices, list) {
  944. template_disk = dd->dm_dev->bdev->bd_disk;
  945. if (!integrity_profile_exists(template_disk))
  946. goto no_integrity;
  947. else if (prev_disk &&
  948. blk_integrity_compare(prev_disk, template_disk) < 0)
  949. goto no_integrity;
  950. prev_disk = template_disk;
  951. }
  952. return template_disk;
  953. no_integrity:
  954. if (prev_disk)
  955. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  956. dm_device_name(t->md),
  957. prev_disk->disk_name,
  958. template_disk->disk_name);
  959. return NULL;
  960. }
  961. /*
  962. * Register the mapped device for blk_integrity support if the
  963. * underlying devices have an integrity profile. But all devices may
  964. * not have matching profiles (checking all devices isn't reliable
  965. * during table load because this table may use other DM device(s) which
  966. * must be resumed before they will have an initialized integity
  967. * profile). Consequently, stacked DM devices force a 2 stage integrity
  968. * profile validation: First pass during table load, final pass during
  969. * resume.
  970. */
  971. static int dm_table_register_integrity(struct dm_table *t)
  972. {
  973. struct mapped_device *md = t->md;
  974. struct gendisk *template_disk = NULL;
  975. /* If target handles integrity itself do not register it here. */
  976. if (t->integrity_added)
  977. return 0;
  978. template_disk = dm_table_get_integrity_disk(t);
  979. if (!template_disk)
  980. return 0;
  981. if (!integrity_profile_exists(dm_disk(md))) {
  982. t->integrity_supported = true;
  983. /*
  984. * Register integrity profile during table load; we can do
  985. * this because the final profile must match during resume.
  986. */
  987. blk_integrity_register(dm_disk(md),
  988. blk_get_integrity(template_disk));
  989. return 0;
  990. }
  991. /*
  992. * If DM device already has an initialized integrity
  993. * profile the new profile should not conflict.
  994. */
  995. if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  996. DMWARN("%s: conflict with existing integrity profile: "
  997. "%s profile mismatch",
  998. dm_device_name(t->md),
  999. template_disk->disk_name);
  1000. return 1;
  1001. }
  1002. /* Preserve existing integrity profile */
  1003. t->integrity_supported = true;
  1004. return 0;
  1005. }
  1006. /*
  1007. * Prepares the table for use by building the indices,
  1008. * setting the type, and allocating mempools.
  1009. */
  1010. int dm_table_complete(struct dm_table *t)
  1011. {
  1012. int r;
  1013. r = dm_table_determine_type(t);
  1014. if (r) {
  1015. DMERR("unable to determine table type");
  1016. return r;
  1017. }
  1018. r = dm_table_build_index(t);
  1019. if (r) {
  1020. DMERR("unable to build btrees");
  1021. return r;
  1022. }
  1023. r = dm_table_register_integrity(t);
  1024. if (r) {
  1025. DMERR("could not register integrity profile.");
  1026. return r;
  1027. }
  1028. r = dm_table_alloc_md_mempools(t, t->md);
  1029. if (r)
  1030. DMERR("unable to allocate mempools");
  1031. return r;
  1032. }
  1033. static DEFINE_MUTEX(_event_lock);
  1034. void dm_table_event_callback(struct dm_table *t,
  1035. void (*fn)(void *), void *context)
  1036. {
  1037. mutex_lock(&_event_lock);
  1038. t->event_fn = fn;
  1039. t->event_context = context;
  1040. mutex_unlock(&_event_lock);
  1041. }
  1042. void dm_table_event(struct dm_table *t)
  1043. {
  1044. /*
  1045. * You can no longer call dm_table_event() from interrupt
  1046. * context, use a bottom half instead.
  1047. */
  1048. BUG_ON(in_interrupt());
  1049. mutex_lock(&_event_lock);
  1050. if (t->event_fn)
  1051. t->event_fn(t->event_context);
  1052. mutex_unlock(&_event_lock);
  1053. }
  1054. EXPORT_SYMBOL(dm_table_event);
  1055. sector_t dm_table_get_size(struct dm_table *t)
  1056. {
  1057. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  1058. }
  1059. EXPORT_SYMBOL(dm_table_get_size);
  1060. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  1061. {
  1062. if (index >= t->num_targets)
  1063. return NULL;
  1064. return t->targets + index;
  1065. }
  1066. /*
  1067. * Search the btree for the correct target.
  1068. *
  1069. * Caller should check returned pointer with dm_target_is_valid()
  1070. * to trap I/O beyond end of device.
  1071. */
  1072. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  1073. {
  1074. unsigned int l, n = 0, k = 0;
  1075. sector_t *node;
  1076. for (l = 0; l < t->depth; l++) {
  1077. n = get_child(n, k);
  1078. node = get_node(t, l, n);
  1079. for (k = 0; k < KEYS_PER_NODE; k++)
  1080. if (node[k] >= sector)
  1081. break;
  1082. }
  1083. return &t->targets[(KEYS_PER_NODE * n) + k];
  1084. }
  1085. static int count_device(struct dm_target *ti, struct dm_dev *dev,
  1086. sector_t start, sector_t len, void *data)
  1087. {
  1088. unsigned *num_devices = data;
  1089. (*num_devices)++;
  1090. return 0;
  1091. }
  1092. /*
  1093. * Check whether a table has no data devices attached using each
  1094. * target's iterate_devices method.
  1095. * Returns false if the result is unknown because a target doesn't
  1096. * support iterate_devices.
  1097. */
  1098. bool dm_table_has_no_data_devices(struct dm_table *table)
  1099. {
  1100. struct dm_target *ti;
  1101. unsigned i, num_devices;
  1102. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1103. ti = dm_table_get_target(table, i);
  1104. if (!ti->type->iterate_devices)
  1105. return false;
  1106. num_devices = 0;
  1107. ti->type->iterate_devices(ti, count_device, &num_devices);
  1108. if (num_devices)
  1109. return false;
  1110. }
  1111. return true;
  1112. }
  1113. /*
  1114. * Establish the new table's queue_limits and validate them.
  1115. */
  1116. int dm_calculate_queue_limits(struct dm_table *table,
  1117. struct queue_limits *limits)
  1118. {
  1119. struct dm_target *ti;
  1120. struct queue_limits ti_limits;
  1121. unsigned i;
  1122. blk_set_stacking_limits(limits);
  1123. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1124. blk_set_stacking_limits(&ti_limits);
  1125. ti = dm_table_get_target(table, i);
  1126. if (!ti->type->iterate_devices)
  1127. goto combine_limits;
  1128. /*
  1129. * Combine queue limits of all the devices this target uses.
  1130. */
  1131. ti->type->iterate_devices(ti, dm_set_device_limits,
  1132. &ti_limits);
  1133. /* Set I/O hints portion of queue limits */
  1134. if (ti->type->io_hints)
  1135. ti->type->io_hints(ti, &ti_limits);
  1136. /*
  1137. * Check each device area is consistent with the target's
  1138. * overall queue limits.
  1139. */
  1140. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1141. &ti_limits))
  1142. return -EINVAL;
  1143. combine_limits:
  1144. /*
  1145. * Merge this target's queue limits into the overall limits
  1146. * for the table.
  1147. */
  1148. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1149. DMWARN("%s: adding target device "
  1150. "(start sect %llu len %llu) "
  1151. "caused an alignment inconsistency",
  1152. dm_device_name(table->md),
  1153. (unsigned long long) ti->begin,
  1154. (unsigned long long) ti->len);
  1155. }
  1156. return validate_hardware_logical_block_alignment(table, limits);
  1157. }
  1158. /*
  1159. * Verify that all devices have an integrity profile that matches the
  1160. * DM device's registered integrity profile. If the profiles don't
  1161. * match then unregister the DM device's integrity profile.
  1162. */
  1163. static void dm_table_verify_integrity(struct dm_table *t)
  1164. {
  1165. struct gendisk *template_disk = NULL;
  1166. if (t->integrity_added)
  1167. return;
  1168. if (t->integrity_supported) {
  1169. /*
  1170. * Verify that the original integrity profile
  1171. * matches all the devices in this table.
  1172. */
  1173. template_disk = dm_table_get_integrity_disk(t);
  1174. if (template_disk &&
  1175. blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
  1176. return;
  1177. }
  1178. if (integrity_profile_exists(dm_disk(t->md))) {
  1179. DMWARN("%s: unable to establish an integrity profile",
  1180. dm_device_name(t->md));
  1181. blk_integrity_unregister(dm_disk(t->md));
  1182. }
  1183. }
  1184. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1185. sector_t start, sector_t len, void *data)
  1186. {
  1187. unsigned long flush = (unsigned long) data;
  1188. struct request_queue *q = bdev_get_queue(dev->bdev);
  1189. return q && (q->queue_flags & flush);
  1190. }
  1191. static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
  1192. {
  1193. struct dm_target *ti;
  1194. unsigned i;
  1195. /*
  1196. * Require at least one underlying device to support flushes.
  1197. * t->devices includes internal dm devices such as mirror logs
  1198. * so we need to use iterate_devices here, which targets
  1199. * supporting flushes must provide.
  1200. */
  1201. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1202. ti = dm_table_get_target(t, i);
  1203. if (!ti->num_flush_bios)
  1204. continue;
  1205. if (ti->flush_supported)
  1206. return true;
  1207. if (ti->type->iterate_devices &&
  1208. ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
  1209. return true;
  1210. }
  1211. return false;
  1212. }
  1213. static int device_is_nonrot(struct dm_target *ti, struct dm_dev *dev,
  1214. sector_t start, sector_t len, void *data)
  1215. {
  1216. struct request_queue *q = bdev_get_queue(dev->bdev);
  1217. return q && blk_queue_nonrot(q);
  1218. }
  1219. static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
  1220. sector_t start, sector_t len, void *data)
  1221. {
  1222. struct request_queue *q = bdev_get_queue(dev->bdev);
  1223. return q && !blk_queue_add_random(q);
  1224. }
  1225. static int queue_supports_sg_merge(struct dm_target *ti, struct dm_dev *dev,
  1226. sector_t start, sector_t len, void *data)
  1227. {
  1228. struct request_queue *q = bdev_get_queue(dev->bdev);
  1229. return q && !test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
  1230. }
  1231. static bool dm_table_all_devices_attribute(struct dm_table *t,
  1232. iterate_devices_callout_fn func)
  1233. {
  1234. struct dm_target *ti;
  1235. unsigned i;
  1236. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1237. ti = dm_table_get_target(t, i);
  1238. if (!ti->type->iterate_devices ||
  1239. !ti->type->iterate_devices(ti, func, NULL))
  1240. return false;
  1241. }
  1242. return true;
  1243. }
  1244. static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
  1245. sector_t start, sector_t len, void *data)
  1246. {
  1247. struct request_queue *q = bdev_get_queue(dev->bdev);
  1248. return q && !q->limits.max_write_same_sectors;
  1249. }
  1250. static bool dm_table_supports_write_same(struct dm_table *t)
  1251. {
  1252. struct dm_target *ti;
  1253. unsigned i;
  1254. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1255. ti = dm_table_get_target(t, i);
  1256. if (!ti->num_write_same_bios)
  1257. return false;
  1258. if (!ti->type->iterate_devices ||
  1259. ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
  1260. return false;
  1261. }
  1262. return true;
  1263. }
  1264. static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
  1265. sector_t start, sector_t len, void *data)
  1266. {
  1267. struct request_queue *q = bdev_get_queue(dev->bdev);
  1268. return q && !q->limits.max_write_zeroes_sectors;
  1269. }
  1270. static bool dm_table_supports_write_zeroes(struct dm_table *t)
  1271. {
  1272. struct dm_target *ti;
  1273. unsigned i = 0;
  1274. while (i < dm_table_get_num_targets(t)) {
  1275. ti = dm_table_get_target(t, i++);
  1276. if (!ti->num_write_zeroes_bios)
  1277. return false;
  1278. if (!ti->type->iterate_devices ||
  1279. ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
  1280. return false;
  1281. }
  1282. return true;
  1283. }
  1284. static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1285. sector_t start, sector_t len, void *data)
  1286. {
  1287. struct request_queue *q = bdev_get_queue(dev->bdev);
  1288. return q && blk_queue_discard(q);
  1289. }
  1290. static bool dm_table_supports_discards(struct dm_table *t)
  1291. {
  1292. struct dm_target *ti;
  1293. unsigned i;
  1294. /*
  1295. * Unless any target used by the table set discards_supported,
  1296. * require at least one underlying device to support discards.
  1297. * t->devices includes internal dm devices such as mirror logs
  1298. * so we need to use iterate_devices here, which targets
  1299. * supporting discard selectively must provide.
  1300. */
  1301. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1302. ti = dm_table_get_target(t, i);
  1303. if (!ti->num_discard_bios)
  1304. continue;
  1305. if (ti->discards_supported)
  1306. return true;
  1307. if (ti->type->iterate_devices &&
  1308. ti->type->iterate_devices(ti, device_discard_capable, NULL))
  1309. return true;
  1310. }
  1311. return false;
  1312. }
  1313. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1314. struct queue_limits *limits)
  1315. {
  1316. bool wc = false, fua = false;
  1317. /*
  1318. * Copy table's limits to the DM device's request_queue
  1319. */
  1320. q->limits = *limits;
  1321. if (!dm_table_supports_discards(t))
  1322. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  1323. else
  1324. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1325. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
  1326. wc = true;
  1327. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
  1328. fua = true;
  1329. }
  1330. blk_queue_write_cache(q, wc, fua);
  1331. /* Ensure that all underlying devices are non-rotational. */
  1332. if (dm_table_all_devices_attribute(t, device_is_nonrot))
  1333. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  1334. else
  1335. queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
  1336. if (!dm_table_supports_write_same(t))
  1337. q->limits.max_write_same_sectors = 0;
  1338. if (!dm_table_supports_write_zeroes(t))
  1339. q->limits.max_write_zeroes_sectors = 0;
  1340. if (dm_table_all_devices_attribute(t, queue_supports_sg_merge))
  1341. queue_flag_clear_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1342. else
  1343. queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1344. dm_table_verify_integrity(t);
  1345. /*
  1346. * Determine whether or not this queue's I/O timings contribute
  1347. * to the entropy pool, Only request-based targets use this.
  1348. * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
  1349. * have it set.
  1350. */
  1351. if (blk_queue_add_random(q) && dm_table_all_devices_attribute(t, device_is_not_random))
  1352. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
  1353. /*
  1354. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  1355. * visible to other CPUs because, once the flag is set, incoming bios
  1356. * are processed by request-based dm, which refers to the queue
  1357. * settings.
  1358. * Until the flag set, bios are passed to bio-based dm and queued to
  1359. * md->deferred where queue settings are not needed yet.
  1360. * Those bios are passed to request-based dm at the resume time.
  1361. */
  1362. smp_mb();
  1363. if (dm_table_request_based(t))
  1364. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  1365. }
  1366. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1367. {
  1368. return t->num_targets;
  1369. }
  1370. struct list_head *dm_table_get_devices(struct dm_table *t)
  1371. {
  1372. return &t->devices;
  1373. }
  1374. fmode_t dm_table_get_mode(struct dm_table *t)
  1375. {
  1376. return t->mode;
  1377. }
  1378. EXPORT_SYMBOL(dm_table_get_mode);
  1379. enum suspend_mode {
  1380. PRESUSPEND,
  1381. PRESUSPEND_UNDO,
  1382. POSTSUSPEND,
  1383. };
  1384. static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
  1385. {
  1386. int i = t->num_targets;
  1387. struct dm_target *ti = t->targets;
  1388. lockdep_assert_held(&t->md->suspend_lock);
  1389. while (i--) {
  1390. switch (mode) {
  1391. case PRESUSPEND:
  1392. if (ti->type->presuspend)
  1393. ti->type->presuspend(ti);
  1394. break;
  1395. case PRESUSPEND_UNDO:
  1396. if (ti->type->presuspend_undo)
  1397. ti->type->presuspend_undo(ti);
  1398. break;
  1399. case POSTSUSPEND:
  1400. if (ti->type->postsuspend)
  1401. ti->type->postsuspend(ti);
  1402. break;
  1403. }
  1404. ti++;
  1405. }
  1406. }
  1407. void dm_table_presuspend_targets(struct dm_table *t)
  1408. {
  1409. if (!t)
  1410. return;
  1411. suspend_targets(t, PRESUSPEND);
  1412. }
  1413. void dm_table_presuspend_undo_targets(struct dm_table *t)
  1414. {
  1415. if (!t)
  1416. return;
  1417. suspend_targets(t, PRESUSPEND_UNDO);
  1418. }
  1419. void dm_table_postsuspend_targets(struct dm_table *t)
  1420. {
  1421. if (!t)
  1422. return;
  1423. suspend_targets(t, POSTSUSPEND);
  1424. }
  1425. int dm_table_resume_targets(struct dm_table *t)
  1426. {
  1427. int i, r = 0;
  1428. lockdep_assert_held(&t->md->suspend_lock);
  1429. for (i = 0; i < t->num_targets; i++) {
  1430. struct dm_target *ti = t->targets + i;
  1431. if (!ti->type->preresume)
  1432. continue;
  1433. r = ti->type->preresume(ti);
  1434. if (r) {
  1435. DMERR("%s: %s: preresume failed, error = %d",
  1436. dm_device_name(t->md), ti->type->name, r);
  1437. return r;
  1438. }
  1439. }
  1440. for (i = 0; i < t->num_targets; i++) {
  1441. struct dm_target *ti = t->targets + i;
  1442. if (ti->type->resume)
  1443. ti->type->resume(ti);
  1444. }
  1445. return 0;
  1446. }
  1447. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1448. {
  1449. list_add(&cb->list, &t->target_callbacks);
  1450. }
  1451. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1452. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1453. {
  1454. struct dm_dev_internal *dd;
  1455. struct list_head *devices = dm_table_get_devices(t);
  1456. struct dm_target_callbacks *cb;
  1457. int r = 0;
  1458. list_for_each_entry(dd, devices, list) {
  1459. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  1460. char b[BDEVNAME_SIZE];
  1461. if (likely(q))
  1462. r |= bdi_congested(q->backing_dev_info, bdi_bits);
  1463. else
  1464. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1465. dm_device_name(t->md),
  1466. bdevname(dd->dm_dev->bdev, b));
  1467. }
  1468. list_for_each_entry(cb, &t->target_callbacks, list)
  1469. if (cb->congested_fn)
  1470. r |= cb->congested_fn(cb, bdi_bits);
  1471. return r;
  1472. }
  1473. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1474. {
  1475. return t->md;
  1476. }
  1477. EXPORT_SYMBOL(dm_table_get_md);
  1478. void dm_table_run_md_queue_async(struct dm_table *t)
  1479. {
  1480. struct mapped_device *md;
  1481. struct request_queue *queue;
  1482. unsigned long flags;
  1483. if (!dm_table_request_based(t))
  1484. return;
  1485. md = dm_table_get_md(t);
  1486. queue = dm_get_md_queue(md);
  1487. if (queue) {
  1488. if (queue->mq_ops)
  1489. blk_mq_run_hw_queues(queue, true);
  1490. else {
  1491. spin_lock_irqsave(queue->queue_lock, flags);
  1492. blk_run_queue_async(queue);
  1493. spin_unlock_irqrestore(queue->queue_lock, flags);
  1494. }
  1495. }
  1496. }
  1497. EXPORT_SYMBOL(dm_table_run_md_queue_async);