dm-table.c 38 KB

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