dm-table.c 27 KB

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