dm-table.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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 <asm/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. /*
  25. * The table has always exactly one reference from either mapped_device->map
  26. * or hash_cell->new_map. This reference is not counted in table->holders.
  27. * A pair of dm_create_table/dm_destroy_table functions is used for table
  28. * creation/destruction.
  29. *
  30. * Temporary references from the other code increase table->holders. A pair
  31. * of dm_table_get/dm_table_put functions is used to manipulate it.
  32. *
  33. * When the table is about to be destroyed, we wait for table->holders to
  34. * drop to zero.
  35. */
  36. struct dm_table {
  37. struct mapped_device *md;
  38. atomic_t holders;
  39. unsigned type;
  40. /* btree table */
  41. unsigned int depth;
  42. unsigned int counts[MAX_DEPTH]; /* in nodes */
  43. sector_t *index[MAX_DEPTH];
  44. unsigned int num_targets;
  45. unsigned int num_allocated;
  46. sector_t *highs;
  47. struct dm_target *targets;
  48. unsigned discards_supported:1;
  49. /*
  50. * Indicates the rw permissions for the new logical
  51. * device. This should be a combination of FMODE_READ
  52. * and FMODE_WRITE.
  53. */
  54. fmode_t mode;
  55. /* a list of devices used by this table */
  56. struct list_head devices;
  57. /* events get handed up using this callback */
  58. void (*event_fn)(void *);
  59. void *event_context;
  60. struct dm_md_mempools *mempools;
  61. };
  62. /*
  63. * Similar to ceiling(log_size(n))
  64. */
  65. static unsigned int int_log(unsigned int n, unsigned int base)
  66. {
  67. int result = 0;
  68. while (n > 1) {
  69. n = dm_div_up(n, base);
  70. result++;
  71. }
  72. return result;
  73. }
  74. /*
  75. * Calculate the index of the child node of the n'th node k'th key.
  76. */
  77. static inline unsigned int get_child(unsigned int n, unsigned int k)
  78. {
  79. return (n * CHILDREN_PER_NODE) + k;
  80. }
  81. /*
  82. * Return the n'th node of level l from table t.
  83. */
  84. static inline sector_t *get_node(struct dm_table *t,
  85. unsigned int l, unsigned int n)
  86. {
  87. return t->index[l] + (n * KEYS_PER_NODE);
  88. }
  89. /*
  90. * Return the highest key that you could lookup from the n'th
  91. * node on level l of the btree.
  92. */
  93. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  94. {
  95. for (; l < t->depth - 1; l++)
  96. n = get_child(n, CHILDREN_PER_NODE - 1);
  97. if (n >= t->counts[l])
  98. return (sector_t) - 1;
  99. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  100. }
  101. /*
  102. * Fills in a level of the btree based on the highs of the level
  103. * below it.
  104. */
  105. static int setup_btree_index(unsigned int l, struct dm_table *t)
  106. {
  107. unsigned int n, k;
  108. sector_t *node;
  109. for (n = 0U; n < t->counts[l]; n++) {
  110. node = get_node(t, l, n);
  111. for (k = 0U; k < KEYS_PER_NODE; k++)
  112. node[k] = high(t, l + 1, get_child(n, k));
  113. }
  114. return 0;
  115. }
  116. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  117. {
  118. unsigned long size;
  119. void *addr;
  120. /*
  121. * Check that we're not going to overflow.
  122. */
  123. if (nmemb > (ULONG_MAX / elem_size))
  124. return NULL;
  125. size = nmemb * elem_size;
  126. addr = vmalloc(size);
  127. if (addr)
  128. memset(addr, 0, size);
  129. return addr;
  130. }
  131. /*
  132. * highs, and targets are managed as dynamic arrays during a
  133. * table load.
  134. */
  135. static int alloc_targets(struct dm_table *t, unsigned int num)
  136. {
  137. sector_t *n_highs;
  138. struct dm_target *n_targets;
  139. int n = t->num_targets;
  140. /*
  141. * Allocate both the target array and offset array at once.
  142. * Append an empty entry to catch sectors beyond the end of
  143. * the device.
  144. */
  145. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  146. sizeof(sector_t));
  147. if (!n_highs)
  148. return -ENOMEM;
  149. n_targets = (struct dm_target *) (n_highs + num);
  150. if (n) {
  151. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  152. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  153. }
  154. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  155. vfree(t->highs);
  156. t->num_allocated = num;
  157. t->highs = n_highs;
  158. t->targets = n_targets;
  159. return 0;
  160. }
  161. int dm_table_create(struct dm_table **result, fmode_t mode,
  162. unsigned num_targets, struct mapped_device *md)
  163. {
  164. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  165. if (!t)
  166. return -ENOMEM;
  167. INIT_LIST_HEAD(&t->devices);
  168. atomic_set(&t->holders, 0);
  169. t->discards_supported = 1;
  170. if (!num_targets)
  171. num_targets = KEYS_PER_NODE;
  172. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  173. if (alloc_targets(t, num_targets)) {
  174. kfree(t);
  175. t = NULL;
  176. return -ENOMEM;
  177. }
  178. t->mode = mode;
  179. t->md = md;
  180. *result = t;
  181. return 0;
  182. }
  183. static void free_devices(struct list_head *devices)
  184. {
  185. struct list_head *tmp, *next;
  186. list_for_each_safe(tmp, next, devices) {
  187. struct dm_dev_internal *dd =
  188. list_entry(tmp, struct dm_dev_internal, list);
  189. DMWARN("dm_table_destroy: dm_put_device call missing for %s",
  190. dd->dm_dev.name);
  191. kfree(dd);
  192. }
  193. }
  194. void dm_table_destroy(struct dm_table *t)
  195. {
  196. unsigned int i;
  197. if (!t)
  198. return;
  199. while (atomic_read(&t->holders))
  200. msleep(1);
  201. smp_mb();
  202. /* free the indexes */
  203. if (t->depth >= 2)
  204. vfree(t->index[t->depth - 2]);
  205. /* free the targets */
  206. for (i = 0; i < t->num_targets; i++) {
  207. struct dm_target *tgt = t->targets + i;
  208. if (tgt->type->dtr)
  209. tgt->type->dtr(tgt);
  210. dm_put_target_type(tgt->type);
  211. }
  212. vfree(t->highs);
  213. /* free the device list */
  214. if (t->devices.next != &t->devices)
  215. free_devices(&t->devices);
  216. dm_free_md_mempools(t->mempools);
  217. kfree(t);
  218. }
  219. void dm_table_get(struct dm_table *t)
  220. {
  221. atomic_inc(&t->holders);
  222. }
  223. void dm_table_put(struct dm_table *t)
  224. {
  225. if (!t)
  226. return;
  227. smp_mb__before_atomic_dec();
  228. atomic_dec(&t->holders);
  229. }
  230. /*
  231. * Checks to see if we need to extend highs or targets.
  232. */
  233. static inline int check_space(struct dm_table *t)
  234. {
  235. if (t->num_targets >= t->num_allocated)
  236. return alloc_targets(t, t->num_allocated * 2);
  237. return 0;
  238. }
  239. /*
  240. * See if we've already got a device in the list.
  241. */
  242. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  243. {
  244. struct dm_dev_internal *dd;
  245. list_for_each_entry (dd, l, list)
  246. if (dd->dm_dev.bdev->bd_dev == dev)
  247. return dd;
  248. return NULL;
  249. }
  250. /*
  251. * Open a device so we can use it as a map destination.
  252. */
  253. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  254. struct mapped_device *md)
  255. {
  256. static char *_claim_ptr = "I belong to device-mapper";
  257. struct block_device *bdev;
  258. int r;
  259. BUG_ON(d->dm_dev.bdev);
  260. bdev = open_by_devnum(dev, d->dm_dev.mode);
  261. if (IS_ERR(bdev))
  262. return PTR_ERR(bdev);
  263. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  264. if (r)
  265. blkdev_put(bdev, d->dm_dev.mode);
  266. else
  267. d->dm_dev.bdev = bdev;
  268. return r;
  269. }
  270. /*
  271. * Close a device that we've been using.
  272. */
  273. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  274. {
  275. if (!d->dm_dev.bdev)
  276. return;
  277. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  278. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  279. d->dm_dev.bdev = NULL;
  280. }
  281. /*
  282. * If possible, this checks an area of a destination device is invalid.
  283. */
  284. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  285. sector_t start, sector_t len, void *data)
  286. {
  287. struct queue_limits *limits = data;
  288. struct block_device *bdev = dev->bdev;
  289. sector_t dev_size =
  290. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  291. unsigned short logical_block_size_sectors =
  292. limits->logical_block_size >> SECTOR_SHIFT;
  293. char b[BDEVNAME_SIZE];
  294. if (!dev_size)
  295. return 0;
  296. if ((start >= dev_size) || (start + len > dev_size)) {
  297. DMWARN("%s: %s too small for target: "
  298. "start=%llu, len=%llu, dev_size=%llu",
  299. dm_device_name(ti->table->md), bdevname(bdev, b),
  300. (unsigned long long)start,
  301. (unsigned long long)len,
  302. (unsigned long long)dev_size);
  303. return 1;
  304. }
  305. if (logical_block_size_sectors <= 1)
  306. return 0;
  307. if (start & (logical_block_size_sectors - 1)) {
  308. DMWARN("%s: start=%llu not aligned to h/w "
  309. "logical block size %u of %s",
  310. dm_device_name(ti->table->md),
  311. (unsigned long long)start,
  312. limits->logical_block_size, bdevname(bdev, b));
  313. return 1;
  314. }
  315. if (len & (logical_block_size_sectors - 1)) {
  316. DMWARN("%s: len=%llu not aligned to h/w "
  317. "logical block size %u of %s",
  318. dm_device_name(ti->table->md),
  319. (unsigned long long)len,
  320. limits->logical_block_size, bdevname(bdev, b));
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * This upgrades the mode on an already open dm_dev, being
  327. * careful to leave things as they were if we fail to reopen the
  328. * device and not to touch the existing bdev field in case
  329. * it is accessed concurrently inside dm_table_any_congested().
  330. */
  331. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  332. struct mapped_device *md)
  333. {
  334. int r;
  335. struct dm_dev_internal dd_new, dd_old;
  336. dd_new = dd_old = *dd;
  337. dd_new.dm_dev.mode |= new_mode;
  338. dd_new.dm_dev.bdev = NULL;
  339. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  340. if (r)
  341. return r;
  342. dd->dm_dev.mode |= new_mode;
  343. close_dev(&dd_old, md);
  344. return 0;
  345. }
  346. /*
  347. * Add a device to the list, or just increment the usage count if
  348. * it's already present.
  349. */
  350. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  351. const char *path, fmode_t mode, struct dm_dev **result)
  352. {
  353. int r;
  354. dev_t uninitialized_var(dev);
  355. struct dm_dev_internal *dd;
  356. unsigned int major, minor;
  357. BUG_ON(!t);
  358. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  359. /* Extract the major/minor numbers */
  360. dev = MKDEV(major, minor);
  361. if (MAJOR(dev) != major || MINOR(dev) != minor)
  362. return -EOVERFLOW;
  363. } else {
  364. /* convert the path to a device */
  365. struct block_device *bdev = lookup_bdev(path);
  366. if (IS_ERR(bdev))
  367. return PTR_ERR(bdev);
  368. dev = bdev->bd_dev;
  369. bdput(bdev);
  370. }
  371. dd = find_device(&t->devices, dev);
  372. if (!dd) {
  373. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  374. if (!dd)
  375. return -ENOMEM;
  376. dd->dm_dev.mode = mode;
  377. dd->dm_dev.bdev = NULL;
  378. if ((r = open_dev(dd, dev, t->md))) {
  379. kfree(dd);
  380. return r;
  381. }
  382. format_dev_t(dd->dm_dev.name, dev);
  383. atomic_set(&dd->count, 0);
  384. list_add(&dd->list, &t->devices);
  385. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  386. r = upgrade_mode(dd, mode, t->md);
  387. if (r)
  388. return r;
  389. }
  390. atomic_inc(&dd->count);
  391. *result = &dd->dm_dev;
  392. return 0;
  393. }
  394. /*
  395. * Returns the minimum that is _not_ zero, unless both are zero.
  396. */
  397. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  398. int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  399. sector_t start, sector_t len, void *data)
  400. {
  401. struct queue_limits *limits = data;
  402. struct block_device *bdev = dev->bdev;
  403. struct request_queue *q = bdev_get_queue(bdev);
  404. char b[BDEVNAME_SIZE];
  405. if (unlikely(!q)) {
  406. DMWARN("%s: Cannot set limits for nonexistent device %s",
  407. dm_device_name(ti->table->md), bdevname(bdev, b));
  408. return 0;
  409. }
  410. if (bdev_stack_limits(limits, bdev, start) < 0)
  411. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  412. "physical_block_size=%u, logical_block_size=%u, "
  413. "alignment_offset=%u, start=%llu",
  414. dm_device_name(ti->table->md), bdevname(bdev, b),
  415. q->limits.physical_block_size,
  416. q->limits.logical_block_size,
  417. q->limits.alignment_offset,
  418. (unsigned long long) start << SECTOR_SHIFT);
  419. /*
  420. * Check if merge fn is supported.
  421. * If not we'll force DM to use PAGE_SIZE or
  422. * smaller I/O, just to be safe.
  423. */
  424. if (q->merge_bvec_fn && !ti->type->merge)
  425. limits->max_sectors =
  426. min_not_zero(limits->max_sectors,
  427. (unsigned int) (PAGE_SIZE >> 9));
  428. return 0;
  429. }
  430. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  431. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  432. struct dm_dev **result)
  433. {
  434. return __table_get_device(ti->table, ti, path, mode, result);
  435. }
  436. /*
  437. * Decrement a devices use count and remove it if necessary.
  438. */
  439. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  440. {
  441. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  442. dm_dev);
  443. if (atomic_dec_and_test(&dd->count)) {
  444. close_dev(dd, ti->table->md);
  445. list_del(&dd->list);
  446. kfree(dd);
  447. }
  448. }
  449. /*
  450. * Checks to see if the target joins onto the end of the table.
  451. */
  452. static int adjoin(struct dm_table *table, struct dm_target *ti)
  453. {
  454. struct dm_target *prev;
  455. if (!table->num_targets)
  456. return !ti->begin;
  457. prev = &table->targets[table->num_targets - 1];
  458. return (ti->begin == (prev->begin + prev->len));
  459. }
  460. /*
  461. * Used to dynamically allocate the arg array.
  462. */
  463. static char **realloc_argv(unsigned *array_size, char **old_argv)
  464. {
  465. char **argv;
  466. unsigned new_size;
  467. new_size = *array_size ? *array_size * 2 : 64;
  468. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  469. if (argv) {
  470. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  471. *array_size = new_size;
  472. }
  473. kfree(old_argv);
  474. return argv;
  475. }
  476. /*
  477. * Destructively splits up the argument list to pass to ctr.
  478. */
  479. int dm_split_args(int *argc, char ***argvp, char *input)
  480. {
  481. char *start, *end = input, *out, **argv = NULL;
  482. unsigned array_size = 0;
  483. *argc = 0;
  484. if (!input) {
  485. *argvp = NULL;
  486. return 0;
  487. }
  488. argv = realloc_argv(&array_size, argv);
  489. if (!argv)
  490. return -ENOMEM;
  491. while (1) {
  492. /* Skip whitespace */
  493. start = skip_spaces(end);
  494. if (!*start)
  495. break; /* success, we hit the end */
  496. /* 'out' is used to remove any back-quotes */
  497. end = out = start;
  498. while (*end) {
  499. /* Everything apart from '\0' can be quoted */
  500. if (*end == '\\' && *(end + 1)) {
  501. *out++ = *(end + 1);
  502. end += 2;
  503. continue;
  504. }
  505. if (isspace(*end))
  506. break; /* end of token */
  507. *out++ = *end++;
  508. }
  509. /* have we already filled the array ? */
  510. if ((*argc + 1) > array_size) {
  511. argv = realloc_argv(&array_size, argv);
  512. if (!argv)
  513. return -ENOMEM;
  514. }
  515. /* we know this is whitespace */
  516. if (*end)
  517. end++;
  518. /* terminate the string and put it in the array */
  519. *out = '\0';
  520. argv[*argc] = start;
  521. (*argc)++;
  522. }
  523. *argvp = argv;
  524. return 0;
  525. }
  526. /*
  527. * Impose necessary and sufficient conditions on a devices's table such
  528. * that any incoming bio which respects its logical_block_size can be
  529. * processed successfully. If it falls across the boundary between
  530. * two or more targets, the size of each piece it gets split into must
  531. * be compatible with the logical_block_size of the target processing it.
  532. */
  533. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  534. struct queue_limits *limits)
  535. {
  536. /*
  537. * This function uses arithmetic modulo the logical_block_size
  538. * (in units of 512-byte sectors).
  539. */
  540. unsigned short device_logical_block_size_sects =
  541. limits->logical_block_size >> SECTOR_SHIFT;
  542. /*
  543. * Offset of the start of the next table entry, mod logical_block_size.
  544. */
  545. unsigned short next_target_start = 0;
  546. /*
  547. * Given an aligned bio that extends beyond the end of a
  548. * target, how many sectors must the next target handle?
  549. */
  550. unsigned short remaining = 0;
  551. struct dm_target *uninitialized_var(ti);
  552. struct queue_limits ti_limits;
  553. unsigned i = 0;
  554. /*
  555. * Check each entry in the table in turn.
  556. */
  557. while (i < dm_table_get_num_targets(table)) {
  558. ti = dm_table_get_target(table, i++);
  559. blk_set_default_limits(&ti_limits);
  560. /* combine all target devices' limits */
  561. if (ti->type->iterate_devices)
  562. ti->type->iterate_devices(ti, dm_set_device_limits,
  563. &ti_limits);
  564. /*
  565. * If the remaining sectors fall entirely within this
  566. * table entry are they compatible with its logical_block_size?
  567. */
  568. if (remaining < ti->len &&
  569. remaining & ((ti_limits.logical_block_size >>
  570. SECTOR_SHIFT) - 1))
  571. break; /* Error */
  572. next_target_start =
  573. (unsigned short) ((next_target_start + ti->len) &
  574. (device_logical_block_size_sects - 1));
  575. remaining = next_target_start ?
  576. device_logical_block_size_sects - next_target_start : 0;
  577. }
  578. if (remaining) {
  579. DMWARN("%s: table line %u (start sect %llu len %llu) "
  580. "not aligned to h/w logical block size %u",
  581. dm_device_name(table->md), i,
  582. (unsigned long long) ti->begin,
  583. (unsigned long long) ti->len,
  584. limits->logical_block_size);
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. int dm_table_add_target(struct dm_table *t, const char *type,
  590. sector_t start, sector_t len, char *params)
  591. {
  592. int r = -EINVAL, argc;
  593. char **argv;
  594. struct dm_target *tgt;
  595. if ((r = check_space(t)))
  596. return r;
  597. tgt = t->targets + t->num_targets;
  598. memset(tgt, 0, sizeof(*tgt));
  599. if (!len) {
  600. DMERR("%s: zero-length target", dm_device_name(t->md));
  601. return -EINVAL;
  602. }
  603. tgt->type = dm_get_target_type(type);
  604. if (!tgt->type) {
  605. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  606. type);
  607. return -EINVAL;
  608. }
  609. tgt->table = t;
  610. tgt->begin = start;
  611. tgt->len = len;
  612. tgt->error = "Unknown error";
  613. /*
  614. * Does this target adjoin the previous one ?
  615. */
  616. if (!adjoin(t, tgt)) {
  617. tgt->error = "Gap in table";
  618. r = -EINVAL;
  619. goto bad;
  620. }
  621. r = dm_split_args(&argc, &argv, params);
  622. if (r) {
  623. tgt->error = "couldn't split parameters (insufficient memory)";
  624. goto bad;
  625. }
  626. r = tgt->type->ctr(tgt, argc, argv);
  627. kfree(argv);
  628. if (r)
  629. goto bad;
  630. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  631. if (!tgt->num_discard_requests)
  632. t->discards_supported = 0;
  633. return 0;
  634. bad:
  635. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  636. dm_put_target_type(tgt->type);
  637. return r;
  638. }
  639. static int dm_table_set_type(struct dm_table *t)
  640. {
  641. unsigned i;
  642. unsigned bio_based = 0, request_based = 0;
  643. struct dm_target *tgt;
  644. struct dm_dev_internal *dd;
  645. struct list_head *devices;
  646. for (i = 0; i < t->num_targets; i++) {
  647. tgt = t->targets + i;
  648. if (dm_target_request_based(tgt))
  649. request_based = 1;
  650. else
  651. bio_based = 1;
  652. if (bio_based && request_based) {
  653. DMWARN("Inconsistent table: different target types"
  654. " can't be mixed up");
  655. return -EINVAL;
  656. }
  657. }
  658. if (bio_based) {
  659. /* We must use this table as bio-based */
  660. t->type = DM_TYPE_BIO_BASED;
  661. return 0;
  662. }
  663. BUG_ON(!request_based); /* No targets in this table */
  664. /* Non-request-stackable devices can't be used for request-based dm */
  665. devices = dm_table_get_devices(t);
  666. list_for_each_entry(dd, devices, list) {
  667. if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev.bdev))) {
  668. DMWARN("table load rejected: including"
  669. " non-request-stackable devices");
  670. return -EINVAL;
  671. }
  672. }
  673. /*
  674. * Request-based dm supports only tables that have a single target now.
  675. * To support multiple targets, request splitting support is needed,
  676. * and that needs lots of changes in the block-layer.
  677. * (e.g. request completion process for partial completion.)
  678. */
  679. if (t->num_targets > 1) {
  680. DMWARN("Request-based dm doesn't support multiple targets yet");
  681. return -EINVAL;
  682. }
  683. t->type = DM_TYPE_REQUEST_BASED;
  684. return 0;
  685. }
  686. unsigned dm_table_get_type(struct dm_table *t)
  687. {
  688. return t->type;
  689. }
  690. bool dm_table_request_based(struct dm_table *t)
  691. {
  692. return dm_table_get_type(t) == DM_TYPE_REQUEST_BASED;
  693. }
  694. int dm_table_alloc_md_mempools(struct dm_table *t)
  695. {
  696. unsigned type = dm_table_get_type(t);
  697. if (unlikely(type == DM_TYPE_NONE)) {
  698. DMWARN("no table type is set, can't allocate mempools");
  699. return -EINVAL;
  700. }
  701. t->mempools = dm_alloc_md_mempools(type);
  702. if (!t->mempools)
  703. return -ENOMEM;
  704. return 0;
  705. }
  706. void dm_table_free_md_mempools(struct dm_table *t)
  707. {
  708. dm_free_md_mempools(t->mempools);
  709. t->mempools = NULL;
  710. }
  711. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  712. {
  713. return t->mempools;
  714. }
  715. static int setup_indexes(struct dm_table *t)
  716. {
  717. int i;
  718. unsigned int total = 0;
  719. sector_t *indexes;
  720. /* allocate the space for *all* the indexes */
  721. for (i = t->depth - 2; i >= 0; i--) {
  722. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  723. total += t->counts[i];
  724. }
  725. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  726. if (!indexes)
  727. return -ENOMEM;
  728. /* set up internal nodes, bottom-up */
  729. for (i = t->depth - 2; i >= 0; i--) {
  730. t->index[i] = indexes;
  731. indexes += (KEYS_PER_NODE * t->counts[i]);
  732. setup_btree_index(i, t);
  733. }
  734. return 0;
  735. }
  736. /*
  737. * Builds the btree to index the map.
  738. */
  739. static int dm_table_build_index(struct dm_table *t)
  740. {
  741. int r = 0;
  742. unsigned int leaf_nodes;
  743. /* how many indexes will the btree have ? */
  744. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  745. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  746. /* leaf layer has already been set up */
  747. t->counts[t->depth - 1] = leaf_nodes;
  748. t->index[t->depth - 1] = t->highs;
  749. if (t->depth >= 2)
  750. r = setup_indexes(t);
  751. return r;
  752. }
  753. /*
  754. * Register the mapped device for blk_integrity support if
  755. * the underlying devices support it.
  756. */
  757. static int dm_table_prealloc_integrity(struct dm_table *t, struct mapped_device *md)
  758. {
  759. struct list_head *devices = dm_table_get_devices(t);
  760. struct dm_dev_internal *dd;
  761. list_for_each_entry(dd, devices, list)
  762. if (bdev_get_integrity(dd->dm_dev.bdev))
  763. return blk_integrity_register(dm_disk(md), NULL);
  764. return 0;
  765. }
  766. /*
  767. * Prepares the table for use by building the indices,
  768. * setting the type, and allocating mempools.
  769. */
  770. int dm_table_complete(struct dm_table *t)
  771. {
  772. int r;
  773. r = dm_table_set_type(t);
  774. if (r) {
  775. DMERR("unable to set table type");
  776. return r;
  777. }
  778. r = dm_table_build_index(t);
  779. if (r) {
  780. DMERR("unable to build btrees");
  781. return r;
  782. }
  783. r = dm_table_prealloc_integrity(t, t->md);
  784. if (r) {
  785. DMERR("could not register integrity profile.");
  786. return r;
  787. }
  788. r = dm_table_alloc_md_mempools(t);
  789. if (r)
  790. DMERR("unable to allocate mempools");
  791. return r;
  792. }
  793. static DEFINE_MUTEX(_event_lock);
  794. void dm_table_event_callback(struct dm_table *t,
  795. void (*fn)(void *), void *context)
  796. {
  797. mutex_lock(&_event_lock);
  798. t->event_fn = fn;
  799. t->event_context = context;
  800. mutex_unlock(&_event_lock);
  801. }
  802. void dm_table_event(struct dm_table *t)
  803. {
  804. /*
  805. * You can no longer call dm_table_event() from interrupt
  806. * context, use a bottom half instead.
  807. */
  808. BUG_ON(in_interrupt());
  809. mutex_lock(&_event_lock);
  810. if (t->event_fn)
  811. t->event_fn(t->event_context);
  812. mutex_unlock(&_event_lock);
  813. }
  814. sector_t dm_table_get_size(struct dm_table *t)
  815. {
  816. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  817. }
  818. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  819. {
  820. if (index >= t->num_targets)
  821. return NULL;
  822. return t->targets + index;
  823. }
  824. /*
  825. * Search the btree for the correct target.
  826. *
  827. * Caller should check returned pointer with dm_target_is_valid()
  828. * to trap I/O beyond end of device.
  829. */
  830. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  831. {
  832. unsigned int l, n = 0, k = 0;
  833. sector_t *node;
  834. for (l = 0; l < t->depth; l++) {
  835. n = get_child(n, k);
  836. node = get_node(t, l, n);
  837. for (k = 0; k < KEYS_PER_NODE; k++)
  838. if (node[k] >= sector)
  839. break;
  840. }
  841. return &t->targets[(KEYS_PER_NODE * n) + k];
  842. }
  843. /*
  844. * Establish the new table's queue_limits and validate them.
  845. */
  846. int dm_calculate_queue_limits(struct dm_table *table,
  847. struct queue_limits *limits)
  848. {
  849. struct dm_target *uninitialized_var(ti);
  850. struct queue_limits ti_limits;
  851. unsigned i = 0;
  852. blk_set_default_limits(limits);
  853. while (i < dm_table_get_num_targets(table)) {
  854. blk_set_default_limits(&ti_limits);
  855. ti = dm_table_get_target(table, i++);
  856. if (!ti->type->iterate_devices)
  857. goto combine_limits;
  858. /*
  859. * Combine queue limits of all the devices this target uses.
  860. */
  861. ti->type->iterate_devices(ti, dm_set_device_limits,
  862. &ti_limits);
  863. /* Set I/O hints portion of queue limits */
  864. if (ti->type->io_hints)
  865. ti->type->io_hints(ti, &ti_limits);
  866. /*
  867. * Check each device area is consistent with the target's
  868. * overall queue limits.
  869. */
  870. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  871. &ti_limits))
  872. return -EINVAL;
  873. combine_limits:
  874. /*
  875. * Merge this target's queue limits into the overall limits
  876. * for the table.
  877. */
  878. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  879. DMWARN("%s: adding target device "
  880. "(start sect %llu len %llu) "
  881. "caused an alignment inconsistency",
  882. dm_device_name(table->md),
  883. (unsigned long long) ti->begin,
  884. (unsigned long long) ti->len);
  885. }
  886. return validate_hardware_logical_block_alignment(table, limits);
  887. }
  888. /*
  889. * Set the integrity profile for this device if all devices used have
  890. * matching profiles.
  891. */
  892. static void dm_table_set_integrity(struct dm_table *t)
  893. {
  894. struct list_head *devices = dm_table_get_devices(t);
  895. struct dm_dev_internal *prev = NULL, *dd = NULL;
  896. if (!blk_get_integrity(dm_disk(t->md)))
  897. return;
  898. list_for_each_entry(dd, devices, list) {
  899. if (prev &&
  900. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  901. dd->dm_dev.bdev->bd_disk) < 0) {
  902. DMWARN("%s: integrity not set: %s and %s mismatch",
  903. dm_device_name(t->md),
  904. prev->dm_dev.bdev->bd_disk->disk_name,
  905. dd->dm_dev.bdev->bd_disk->disk_name);
  906. goto no_integrity;
  907. }
  908. prev = dd;
  909. }
  910. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  911. goto no_integrity;
  912. blk_integrity_register(dm_disk(t->md),
  913. bdev_get_integrity(prev->dm_dev.bdev));
  914. return;
  915. no_integrity:
  916. blk_integrity_register(dm_disk(t->md), NULL);
  917. return;
  918. }
  919. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  920. struct queue_limits *limits)
  921. {
  922. /*
  923. * Copy table's limits to the DM device's request_queue
  924. */
  925. q->limits = *limits;
  926. if (limits->no_cluster)
  927. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  928. else
  929. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  930. if (!dm_table_supports_discards(t))
  931. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  932. else
  933. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  934. dm_table_set_integrity(t);
  935. /*
  936. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  937. * visible to other CPUs because, once the flag is set, incoming bios
  938. * are processed by request-based dm, which refers to the queue
  939. * settings.
  940. * Until the flag set, bios are passed to bio-based dm and queued to
  941. * md->deferred where queue settings are not needed yet.
  942. * Those bios are passed to request-based dm at the resume time.
  943. */
  944. smp_mb();
  945. if (dm_table_request_based(t))
  946. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  947. }
  948. unsigned int dm_table_get_num_targets(struct dm_table *t)
  949. {
  950. return t->num_targets;
  951. }
  952. struct list_head *dm_table_get_devices(struct dm_table *t)
  953. {
  954. return &t->devices;
  955. }
  956. fmode_t dm_table_get_mode(struct dm_table *t)
  957. {
  958. return t->mode;
  959. }
  960. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  961. {
  962. int i = t->num_targets;
  963. struct dm_target *ti = t->targets;
  964. while (i--) {
  965. if (postsuspend) {
  966. if (ti->type->postsuspend)
  967. ti->type->postsuspend(ti);
  968. } else if (ti->type->presuspend)
  969. ti->type->presuspend(ti);
  970. ti++;
  971. }
  972. }
  973. void dm_table_presuspend_targets(struct dm_table *t)
  974. {
  975. if (!t)
  976. return;
  977. suspend_targets(t, 0);
  978. }
  979. void dm_table_postsuspend_targets(struct dm_table *t)
  980. {
  981. if (!t)
  982. return;
  983. suspend_targets(t, 1);
  984. }
  985. int dm_table_resume_targets(struct dm_table *t)
  986. {
  987. int i, r = 0;
  988. for (i = 0; i < t->num_targets; i++) {
  989. struct dm_target *ti = t->targets + i;
  990. if (!ti->type->preresume)
  991. continue;
  992. r = ti->type->preresume(ti);
  993. if (r)
  994. return r;
  995. }
  996. for (i = 0; i < t->num_targets; i++) {
  997. struct dm_target *ti = t->targets + i;
  998. if (ti->type->resume)
  999. ti->type->resume(ti);
  1000. }
  1001. return 0;
  1002. }
  1003. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1004. {
  1005. struct dm_dev_internal *dd;
  1006. struct list_head *devices = dm_table_get_devices(t);
  1007. int r = 0;
  1008. list_for_each_entry(dd, devices, list) {
  1009. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  1010. char b[BDEVNAME_SIZE];
  1011. if (likely(q))
  1012. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  1013. else
  1014. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1015. dm_device_name(t->md),
  1016. bdevname(dd->dm_dev.bdev, b));
  1017. }
  1018. return r;
  1019. }
  1020. int dm_table_any_busy_target(struct dm_table *t)
  1021. {
  1022. unsigned i;
  1023. struct dm_target *ti;
  1024. for (i = 0; i < t->num_targets; i++) {
  1025. ti = t->targets + i;
  1026. if (ti->type->busy && ti->type->busy(ti))
  1027. return 1;
  1028. }
  1029. return 0;
  1030. }
  1031. void dm_table_unplug_all(struct dm_table *t)
  1032. {
  1033. struct dm_dev_internal *dd;
  1034. struct list_head *devices = dm_table_get_devices(t);
  1035. list_for_each_entry(dd, devices, list) {
  1036. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  1037. char b[BDEVNAME_SIZE];
  1038. if (likely(q))
  1039. blk_unplug(q);
  1040. else
  1041. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  1042. dm_device_name(t->md),
  1043. bdevname(dd->dm_dev.bdev, b));
  1044. }
  1045. }
  1046. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1047. {
  1048. return t->md;
  1049. }
  1050. static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1051. sector_t start, sector_t len, void *data)
  1052. {
  1053. struct request_queue *q = bdev_get_queue(dev->bdev);
  1054. return q && blk_queue_discard(q);
  1055. }
  1056. bool dm_table_supports_discards(struct dm_table *t)
  1057. {
  1058. struct dm_target *ti;
  1059. unsigned i = 0;
  1060. if (!t->discards_supported)
  1061. return 0;
  1062. /*
  1063. * Ensure that at least one underlying device supports discards.
  1064. * t->devices includes internal dm devices such as mirror logs
  1065. * so we need to use iterate_devices here, which targets
  1066. * supporting discard must provide.
  1067. */
  1068. while (i < dm_table_get_num_targets(t)) {
  1069. ti = dm_table_get_target(t, i++);
  1070. if (ti->type->iterate_devices &&
  1071. ti->type->iterate_devices(ti, device_discard_capable, NULL))
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL(dm_vcalloc);
  1077. EXPORT_SYMBOL(dm_get_device);
  1078. EXPORT_SYMBOL(dm_put_device);
  1079. EXPORT_SYMBOL(dm_table_event);
  1080. EXPORT_SYMBOL(dm_table_get_size);
  1081. EXPORT_SYMBOL(dm_table_get_mode);
  1082. EXPORT_SYMBOL(dm_table_get_md);
  1083. EXPORT_SYMBOL(dm_table_put);
  1084. EXPORT_SYMBOL(dm_table_get);
  1085. EXPORT_SYMBOL(dm_table_unplug_all);