dm-table.c 31 KB

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