dm-table.c 32 KB

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