dm-table.c 29 KB

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