dm-table.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * Copyright (C) 2001 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/namei.h>
  12. #include <linux/ctype.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/mutex.h>
  16. #include <linux/delay.h>
  17. #include <asm/atomic.h>
  18. #define DM_MSG_PREFIX "table"
  19. #define MAX_DEPTH 16
  20. #define NODE_SIZE L1_CACHE_BYTES
  21. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  22. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  23. /*
  24. * The table has always exactly one reference from either mapped_device->map
  25. * or hash_cell->new_map. This reference is not counted in table->holders.
  26. * A pair of dm_create_table/dm_destroy_table functions is used for table
  27. * creation/destruction.
  28. *
  29. * Temporary references from the other code increase table->holders. A pair
  30. * of dm_table_get/dm_table_put functions is used to manipulate it.
  31. *
  32. * When the table is about to be destroyed, we wait for table->holders to
  33. * drop to zero.
  34. */
  35. struct dm_table {
  36. struct mapped_device *md;
  37. atomic_t holders;
  38. unsigned type;
  39. /* btree table */
  40. unsigned int depth;
  41. unsigned int counts[MAX_DEPTH]; /* in nodes */
  42. sector_t *index[MAX_DEPTH];
  43. unsigned int num_targets;
  44. unsigned int num_allocated;
  45. sector_t *highs;
  46. struct dm_target *targets;
  47. /*
  48. * Indicates the rw permissions for the new logical
  49. * device. This should be a combination of FMODE_READ
  50. * and FMODE_WRITE.
  51. */
  52. fmode_t mode;
  53. /* a list of devices used by this table */
  54. struct list_head devices;
  55. /* events get handed up using this callback */
  56. void (*event_fn)(void *);
  57. void *event_context;
  58. struct dm_md_mempools *mempools;
  59. };
  60. /*
  61. * Similar to ceiling(log_size(n))
  62. */
  63. static unsigned int int_log(unsigned int n, unsigned int base)
  64. {
  65. int result = 0;
  66. while (n > 1) {
  67. n = dm_div_up(n, base);
  68. result++;
  69. }
  70. return result;
  71. }
  72. /*
  73. * Calculate the index of the child node of the n'th node k'th key.
  74. */
  75. static inline unsigned int get_child(unsigned int n, unsigned int k)
  76. {
  77. return (n * CHILDREN_PER_NODE) + k;
  78. }
  79. /*
  80. * Return the n'th node of level l from table t.
  81. */
  82. static inline sector_t *get_node(struct dm_table *t,
  83. unsigned int l, unsigned int n)
  84. {
  85. return t->index[l] + (n * KEYS_PER_NODE);
  86. }
  87. /*
  88. * Return the highest key that you could lookup from the n'th
  89. * node on level l of the btree.
  90. */
  91. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  92. {
  93. for (; l < t->depth - 1; l++)
  94. n = get_child(n, CHILDREN_PER_NODE - 1);
  95. if (n >= t->counts[l])
  96. return (sector_t) - 1;
  97. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  98. }
  99. /*
  100. * Fills in a level of the btree based on the highs of the level
  101. * below it.
  102. */
  103. static int setup_btree_index(unsigned int l, struct dm_table *t)
  104. {
  105. unsigned int n, k;
  106. sector_t *node;
  107. for (n = 0U; n < t->counts[l]; n++) {
  108. node = get_node(t, l, n);
  109. for (k = 0U; k < KEYS_PER_NODE; k++)
  110. node[k] = high(t, l + 1, get_child(n, k));
  111. }
  112. return 0;
  113. }
  114. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  115. {
  116. unsigned long size;
  117. void *addr;
  118. /*
  119. * Check that we're not going to overflow.
  120. */
  121. if (nmemb > (ULONG_MAX / elem_size))
  122. return NULL;
  123. size = nmemb * elem_size;
  124. addr = vmalloc(size);
  125. if (addr)
  126. memset(addr, 0, size);
  127. return addr;
  128. }
  129. /*
  130. * highs, and targets are managed as dynamic arrays during a
  131. * table load.
  132. */
  133. static int alloc_targets(struct dm_table *t, unsigned int num)
  134. {
  135. sector_t *n_highs;
  136. struct dm_target *n_targets;
  137. int n = t->num_targets;
  138. /*
  139. * Allocate both the target array and offset array at once.
  140. * Append an empty entry to catch sectors beyond the end of
  141. * the device.
  142. */
  143. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  144. sizeof(sector_t));
  145. if (!n_highs)
  146. return -ENOMEM;
  147. n_targets = (struct dm_target *) (n_highs + num);
  148. if (n) {
  149. memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
  150. memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
  151. }
  152. memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
  153. vfree(t->highs);
  154. t->num_allocated = num;
  155. t->highs = n_highs;
  156. t->targets = n_targets;
  157. return 0;
  158. }
  159. int dm_table_create(struct dm_table **result, fmode_t mode,
  160. unsigned num_targets, struct mapped_device *md)
  161. {
  162. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  163. if (!t)
  164. return -ENOMEM;
  165. INIT_LIST_HEAD(&t->devices);
  166. atomic_set(&t->holders, 0);
  167. if (!num_targets)
  168. num_targets = KEYS_PER_NODE;
  169. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  170. if (alloc_targets(t, num_targets)) {
  171. kfree(t);
  172. t = NULL;
  173. return -ENOMEM;
  174. }
  175. t->mode = mode;
  176. t->md = md;
  177. *result = t;
  178. return 0;
  179. }
  180. static void free_devices(struct list_head *devices)
  181. {
  182. struct list_head *tmp, *next;
  183. list_for_each_safe(tmp, next, devices) {
  184. struct dm_dev_internal *dd =
  185. list_entry(tmp, struct dm_dev_internal, list);
  186. DMWARN("dm_table_destroy: dm_put_device call missing for %s",
  187. dd->dm_dev.name);
  188. kfree(dd);
  189. }
  190. }
  191. void dm_table_destroy(struct dm_table *t)
  192. {
  193. unsigned int i;
  194. while (atomic_read(&t->holders))
  195. msleep(1);
  196. smp_mb();
  197. /* free the indexes (see dm_table_complete) */
  198. if (t->depth >= 2)
  199. vfree(t->index[t->depth - 2]);
  200. /* free the targets */
  201. for (i = 0; i < t->num_targets; i++) {
  202. struct dm_target *tgt = t->targets + i;
  203. if (tgt->type->dtr)
  204. tgt->type->dtr(tgt);
  205. dm_put_target_type(tgt->type);
  206. }
  207. vfree(t->highs);
  208. /* free the device list */
  209. if (t->devices.next != &t->devices)
  210. free_devices(&t->devices);
  211. dm_free_md_mempools(t->mempools);
  212. kfree(t);
  213. }
  214. void dm_table_get(struct dm_table *t)
  215. {
  216. atomic_inc(&t->holders);
  217. }
  218. void dm_table_put(struct dm_table *t)
  219. {
  220. if (!t)
  221. return;
  222. smp_mb__before_atomic_dec();
  223. atomic_dec(&t->holders);
  224. }
  225. /*
  226. * Checks to see if we need to extend highs or targets.
  227. */
  228. static inline int check_space(struct dm_table *t)
  229. {
  230. if (t->num_targets >= t->num_allocated)
  231. return alloc_targets(t, t->num_allocated * 2);
  232. return 0;
  233. }
  234. /*
  235. * See if we've already got a device in the list.
  236. */
  237. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  238. {
  239. struct dm_dev_internal *dd;
  240. list_for_each_entry (dd, l, list)
  241. if (dd->dm_dev.bdev->bd_dev == dev)
  242. return dd;
  243. return NULL;
  244. }
  245. /*
  246. * Open a device so we can use it as a map destination.
  247. */
  248. static int open_dev(struct dm_dev_internal *d, dev_t dev,
  249. struct mapped_device *md)
  250. {
  251. static char *_claim_ptr = "I belong to device-mapper";
  252. struct block_device *bdev;
  253. int r;
  254. BUG_ON(d->dm_dev.bdev);
  255. bdev = open_by_devnum(dev, d->dm_dev.mode);
  256. if (IS_ERR(bdev))
  257. return PTR_ERR(bdev);
  258. r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
  259. if (r)
  260. blkdev_put(bdev, d->dm_dev.mode);
  261. else
  262. d->dm_dev.bdev = bdev;
  263. return r;
  264. }
  265. /*
  266. * Close a device that we've been using.
  267. */
  268. static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
  269. {
  270. if (!d->dm_dev.bdev)
  271. return;
  272. bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
  273. blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
  274. d->dm_dev.bdev = NULL;
  275. }
  276. /*
  277. * If possible, this checks an area of a destination device is invalid.
  278. */
  279. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  280. sector_t start, sector_t len, void *data)
  281. {
  282. struct queue_limits *limits = data;
  283. struct block_device *bdev = dev->bdev;
  284. sector_t dev_size =
  285. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  286. unsigned short logical_block_size_sectors =
  287. limits->logical_block_size >> SECTOR_SHIFT;
  288. char b[BDEVNAME_SIZE];
  289. if (!dev_size)
  290. return 0;
  291. if ((start >= dev_size) || (start + len > dev_size)) {
  292. DMWARN("%s: %s too small for target: "
  293. "start=%llu, len=%llu, dev_size=%llu",
  294. dm_device_name(ti->table->md), bdevname(bdev, b),
  295. (unsigned long long)start,
  296. (unsigned long long)len,
  297. (unsigned long long)dev_size);
  298. return 1;
  299. }
  300. if (logical_block_size_sectors <= 1)
  301. return 0;
  302. if (start & (logical_block_size_sectors - 1)) {
  303. DMWARN("%s: start=%llu not aligned to h/w "
  304. "logical block size %u of %s",
  305. dm_device_name(ti->table->md),
  306. (unsigned long long)start,
  307. limits->logical_block_size, bdevname(bdev, b));
  308. return 1;
  309. }
  310. if (len & (logical_block_size_sectors - 1)) {
  311. DMWARN("%s: len=%llu not aligned to h/w "
  312. "logical block size %u of %s",
  313. dm_device_name(ti->table->md),
  314. (unsigned long long)len,
  315. limits->logical_block_size, bdevname(bdev, b));
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. /*
  321. * This upgrades the mode on an already open dm_dev, being
  322. * careful to leave things as they were if we fail to reopen the
  323. * device and not to touch the existing bdev field in case
  324. * it is accessed concurrently inside dm_table_any_congested().
  325. */
  326. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  327. struct mapped_device *md)
  328. {
  329. int r;
  330. struct dm_dev_internal dd_new, dd_old;
  331. dd_new = dd_old = *dd;
  332. dd_new.dm_dev.mode |= new_mode;
  333. dd_new.dm_dev.bdev = NULL;
  334. r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
  335. if (r)
  336. return r;
  337. dd->dm_dev.mode |= new_mode;
  338. close_dev(&dd_old, md);
  339. return 0;
  340. }
  341. /*
  342. * Add a device to the list, or just increment the usage count if
  343. * it's already present.
  344. */
  345. static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  346. const char *path, sector_t start, sector_t len,
  347. fmode_t mode, struct dm_dev **result)
  348. {
  349. int r;
  350. dev_t uninitialized_var(dev);
  351. struct dm_dev_internal *dd;
  352. unsigned int major, minor;
  353. BUG_ON(!t);
  354. if (sscanf(path, "%u:%u", &major, &minor) == 2) {
  355. /* Extract the major/minor numbers */
  356. dev = MKDEV(major, minor);
  357. if (MAJOR(dev) != major || MINOR(dev) != minor)
  358. return -EOVERFLOW;
  359. } else {
  360. /* convert the path to a device */
  361. struct block_device *bdev = lookup_bdev(path);
  362. if (IS_ERR(bdev))
  363. return PTR_ERR(bdev);
  364. dev = bdev->bd_dev;
  365. bdput(bdev);
  366. }
  367. dd = find_device(&t->devices, dev);
  368. if (!dd) {
  369. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  370. if (!dd)
  371. return -ENOMEM;
  372. dd->dm_dev.mode = mode;
  373. dd->dm_dev.bdev = NULL;
  374. if ((r = open_dev(dd, dev, t->md))) {
  375. kfree(dd);
  376. return r;
  377. }
  378. format_dev_t(dd->dm_dev.name, dev);
  379. atomic_set(&dd->count, 0);
  380. list_add(&dd->list, &t->devices);
  381. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  382. r = upgrade_mode(dd, mode, t->md);
  383. if (r)
  384. return r;
  385. }
  386. atomic_inc(&dd->count);
  387. *result = &dd->dm_dev;
  388. return 0;
  389. }
  390. /*
  391. * Returns the minimum that is _not_ zero, unless both are zero.
  392. */
  393. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  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 (blk_stack_limits(limits, &q->limits, start << 9) < 0)
  407. DMWARN("%s: target device %s is misaligned: "
  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 << 9);
  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. limits->max_sectors =
  422. min_not_zero(limits->max_sectors,
  423. (unsigned int) (PAGE_SIZE >> 9));
  424. return 0;
  425. }
  426. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  427. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  428. sector_t len, fmode_t mode, struct dm_dev **result)
  429. {
  430. return __table_get_device(ti->table, ti, path,
  431. start, len, 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. start = end;
  490. /* Skip whitespace */
  491. while (*start && isspace(*start))
  492. start++;
  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. 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. 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. int dm_table_complete(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. static DEFINE_MUTEX(_event_lock);
  751. void dm_table_event_callback(struct dm_table *t,
  752. void (*fn)(void *), void *context)
  753. {
  754. mutex_lock(&_event_lock);
  755. t->event_fn = fn;
  756. t->event_context = context;
  757. mutex_unlock(&_event_lock);
  758. }
  759. void dm_table_event(struct dm_table *t)
  760. {
  761. /*
  762. * You can no longer call dm_table_event() from interrupt
  763. * context, use a bottom half instead.
  764. */
  765. BUG_ON(in_interrupt());
  766. mutex_lock(&_event_lock);
  767. if (t->event_fn)
  768. t->event_fn(t->event_context);
  769. mutex_unlock(&_event_lock);
  770. }
  771. sector_t dm_table_get_size(struct dm_table *t)
  772. {
  773. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  774. }
  775. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  776. {
  777. if (index >= t->num_targets)
  778. return NULL;
  779. return t->targets + index;
  780. }
  781. /*
  782. * Search the btree for the correct target.
  783. *
  784. * Caller should check returned pointer with dm_target_is_valid()
  785. * to trap I/O beyond end of device.
  786. */
  787. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  788. {
  789. unsigned int l, n = 0, k = 0;
  790. sector_t *node;
  791. for (l = 0; l < t->depth; l++) {
  792. n = get_child(n, k);
  793. node = get_node(t, l, n);
  794. for (k = 0; k < KEYS_PER_NODE; k++)
  795. if (node[k] >= sector)
  796. break;
  797. }
  798. return &t->targets[(KEYS_PER_NODE * n) + k];
  799. }
  800. /*
  801. * Establish the new table's queue_limits and validate them.
  802. */
  803. int dm_calculate_queue_limits(struct dm_table *table,
  804. struct queue_limits *limits)
  805. {
  806. struct dm_target *uninitialized_var(ti);
  807. struct queue_limits ti_limits;
  808. unsigned i = 0;
  809. blk_set_default_limits(limits);
  810. while (i < dm_table_get_num_targets(table)) {
  811. blk_set_default_limits(&ti_limits);
  812. ti = dm_table_get_target(table, i++);
  813. if (!ti->type->iterate_devices)
  814. goto combine_limits;
  815. /*
  816. * Combine queue limits of all the devices this target uses.
  817. */
  818. ti->type->iterate_devices(ti, dm_set_device_limits,
  819. &ti_limits);
  820. /* Set I/O hints portion of queue limits */
  821. if (ti->type->io_hints)
  822. ti->type->io_hints(ti, &ti_limits);
  823. /*
  824. * Check each device area is consistent with the target's
  825. * overall queue limits.
  826. */
  827. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  828. &ti_limits))
  829. return -EINVAL;
  830. combine_limits:
  831. /*
  832. * Merge this target's queue limits into the overall limits
  833. * for the table.
  834. */
  835. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  836. DMWARN("%s: target device "
  837. "(start sect %llu len %llu) "
  838. "is misaligned",
  839. dm_device_name(table->md),
  840. (unsigned long long) ti->begin,
  841. (unsigned long long) ti->len);
  842. }
  843. return validate_hardware_logical_block_alignment(table, limits);
  844. }
  845. /*
  846. * Set the integrity profile for this device if all devices used have
  847. * matching profiles.
  848. */
  849. static void dm_table_set_integrity(struct dm_table *t)
  850. {
  851. struct list_head *devices = dm_table_get_devices(t);
  852. struct dm_dev_internal *prev = NULL, *dd = NULL;
  853. if (!blk_get_integrity(dm_disk(t->md)))
  854. return;
  855. list_for_each_entry(dd, devices, list) {
  856. if (prev &&
  857. blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
  858. dd->dm_dev.bdev->bd_disk) < 0) {
  859. DMWARN("%s: integrity not set: %s and %s mismatch",
  860. dm_device_name(t->md),
  861. prev->dm_dev.bdev->bd_disk->disk_name,
  862. dd->dm_dev.bdev->bd_disk->disk_name);
  863. goto no_integrity;
  864. }
  865. prev = dd;
  866. }
  867. if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
  868. goto no_integrity;
  869. blk_integrity_register(dm_disk(t->md),
  870. bdev_get_integrity(prev->dm_dev.bdev));
  871. return;
  872. no_integrity:
  873. blk_integrity_register(dm_disk(t->md), NULL);
  874. return;
  875. }
  876. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  877. struct queue_limits *limits)
  878. {
  879. /*
  880. * Each target device in the table has a data area that should normally
  881. * be aligned such that the DM device's alignment_offset is 0.
  882. * FIXME: Propagate alignment_offsets up the stack and warn of
  883. * sub-optimal or inconsistent settings.
  884. */
  885. limits->alignment_offset = 0;
  886. limits->misaligned = 0;
  887. /*
  888. * Copy table's limits to the DM device's request_queue
  889. */
  890. q->limits = *limits;
  891. if (limits->no_cluster)
  892. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  893. else
  894. queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
  895. dm_table_set_integrity(t);
  896. /*
  897. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  898. * visible to other CPUs because, once the flag is set, incoming bios
  899. * are processed by request-based dm, which refers to the queue
  900. * settings.
  901. * Until the flag set, bios are passed to bio-based dm and queued to
  902. * md->deferred where queue settings are not needed yet.
  903. * Those bios are passed to request-based dm at the resume time.
  904. */
  905. smp_mb();
  906. if (dm_table_request_based(t))
  907. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  908. }
  909. unsigned int dm_table_get_num_targets(struct dm_table *t)
  910. {
  911. return t->num_targets;
  912. }
  913. struct list_head *dm_table_get_devices(struct dm_table *t)
  914. {
  915. return &t->devices;
  916. }
  917. fmode_t dm_table_get_mode(struct dm_table *t)
  918. {
  919. return t->mode;
  920. }
  921. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  922. {
  923. int i = t->num_targets;
  924. struct dm_target *ti = t->targets;
  925. while (i--) {
  926. if (postsuspend) {
  927. if (ti->type->postsuspend)
  928. ti->type->postsuspend(ti);
  929. } else if (ti->type->presuspend)
  930. ti->type->presuspend(ti);
  931. ti++;
  932. }
  933. }
  934. void dm_table_presuspend_targets(struct dm_table *t)
  935. {
  936. if (!t)
  937. return;
  938. suspend_targets(t, 0);
  939. }
  940. void dm_table_postsuspend_targets(struct dm_table *t)
  941. {
  942. if (!t)
  943. return;
  944. suspend_targets(t, 1);
  945. }
  946. int dm_table_resume_targets(struct dm_table *t)
  947. {
  948. int i, r = 0;
  949. for (i = 0; i < t->num_targets; i++) {
  950. struct dm_target *ti = t->targets + i;
  951. if (!ti->type->preresume)
  952. continue;
  953. r = ti->type->preresume(ti);
  954. if (r)
  955. return r;
  956. }
  957. for (i = 0; i < t->num_targets; i++) {
  958. struct dm_target *ti = t->targets + i;
  959. if (ti->type->resume)
  960. ti->type->resume(ti);
  961. }
  962. return 0;
  963. }
  964. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  965. {
  966. struct dm_dev_internal *dd;
  967. struct list_head *devices = dm_table_get_devices(t);
  968. int r = 0;
  969. list_for_each_entry(dd, devices, list) {
  970. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  971. char b[BDEVNAME_SIZE];
  972. if (likely(q))
  973. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  974. else
  975. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  976. dm_device_name(t->md),
  977. bdevname(dd->dm_dev.bdev, b));
  978. }
  979. return r;
  980. }
  981. int dm_table_any_busy_target(struct dm_table *t)
  982. {
  983. unsigned i;
  984. struct dm_target *ti;
  985. for (i = 0; i < t->num_targets; i++) {
  986. ti = t->targets + i;
  987. if (ti->type->busy && ti->type->busy(ti))
  988. return 1;
  989. }
  990. return 0;
  991. }
  992. void dm_table_unplug_all(struct dm_table *t)
  993. {
  994. struct dm_dev_internal *dd;
  995. struct list_head *devices = dm_table_get_devices(t);
  996. list_for_each_entry(dd, devices, list) {
  997. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  998. char b[BDEVNAME_SIZE];
  999. if (likely(q))
  1000. blk_unplug(q);
  1001. else
  1002. DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
  1003. dm_device_name(t->md),
  1004. bdevname(dd->dm_dev.bdev, b));
  1005. }
  1006. }
  1007. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1008. {
  1009. dm_get(t->md);
  1010. return t->md;
  1011. }
  1012. EXPORT_SYMBOL(dm_vcalloc);
  1013. EXPORT_SYMBOL(dm_get_device);
  1014. EXPORT_SYMBOL(dm_put_device);
  1015. EXPORT_SYMBOL(dm_table_event);
  1016. EXPORT_SYMBOL(dm_table_get_size);
  1017. EXPORT_SYMBOL(dm_table_get_mode);
  1018. EXPORT_SYMBOL(dm_table_get_md);
  1019. EXPORT_SYMBOL(dm_table_put);
  1020. EXPORT_SYMBOL(dm_table_get);
  1021. EXPORT_SYMBOL(dm_table_unplug_all);