dm-table.c 27 KB

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