dm-table.c 30 KB

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