dm-table.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 <linux/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. struct target_type *immutable_target_type;
  49. unsigned integrity_supported:1;
  50. unsigned singleton:1;
  51. /*
  52. * Indicates the rw permissions for the new logical
  53. * device. This should be a combination of FMODE_READ
  54. * and FMODE_WRITE.
  55. */
  56. fmode_t mode;
  57. /* a list of devices used by this table */
  58. struct list_head devices;
  59. /* events get handed up using this callback */
  60. void (*event_fn)(void *);
  61. void *event_context;
  62. struct dm_md_mempools *mempools;
  63. struct list_head target_callbacks;
  64. };
  65. /*
  66. * Similar to ceiling(log_size(n))
  67. */
  68. static unsigned int int_log(unsigned int n, unsigned int base)
  69. {
  70. int result = 0;
  71. while (n > 1) {
  72. n = dm_div_up(n, base);
  73. result++;
  74. }
  75. return result;
  76. }
  77. /*
  78. * Calculate the index of the child node of the n'th node k'th key.
  79. */
  80. static inline unsigned int get_child(unsigned int n, unsigned int k)
  81. {
  82. return (n * CHILDREN_PER_NODE) + k;
  83. }
  84. /*
  85. * Return the n'th node of level l from table t.
  86. */
  87. static inline sector_t *get_node(struct dm_table *t,
  88. unsigned int l, unsigned int n)
  89. {
  90. return t->index[l] + (n * KEYS_PER_NODE);
  91. }
  92. /*
  93. * Return the highest key that you could lookup from the n'th
  94. * node on level l of the btree.
  95. */
  96. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  97. {
  98. for (; l < t->depth - 1; l++)
  99. n = get_child(n, CHILDREN_PER_NODE - 1);
  100. if (n >= t->counts[l])
  101. return (sector_t) - 1;
  102. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  103. }
  104. /*
  105. * Fills in a level of the btree based on the highs of the level
  106. * below it.
  107. */
  108. static int setup_btree_index(unsigned int l, struct dm_table *t)
  109. {
  110. unsigned int n, k;
  111. sector_t *node;
  112. for (n = 0U; n < t->counts[l]; n++) {
  113. node = get_node(t, l, n);
  114. for (k = 0U; k < KEYS_PER_NODE; k++)
  115. node[k] = high(t, l + 1, get_child(n, k));
  116. }
  117. return 0;
  118. }
  119. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  120. {
  121. unsigned long size;
  122. void *addr;
  123. /*
  124. * Check that we're not going to overflow.
  125. */
  126. if (nmemb > (ULONG_MAX / elem_size))
  127. return NULL;
  128. size = nmemb * elem_size;
  129. addr = vzalloc(size);
  130. return addr;
  131. }
  132. EXPORT_SYMBOL(dm_vcalloc);
  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. 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. free_devices(&t->devices);
  217. dm_free_md_mempools(t->mempools);
  218. kfree(t);
  219. }
  220. void dm_table_get(struct dm_table *t)
  221. {
  222. atomic_inc(&t->holders);
  223. }
  224. EXPORT_SYMBOL(dm_table_get);
  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. EXPORT_SYMBOL(dm_table_put);
  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. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  371. 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. struct dm_table *t = ti->table;
  378. char dummy;
  379. BUG_ON(!t);
  380. if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
  381. /* Extract the major/minor numbers */
  382. dev = MKDEV(major, minor);
  383. if (MAJOR(dev) != major || MINOR(dev) != minor)
  384. return -EOVERFLOW;
  385. } else {
  386. /* convert the path to a device */
  387. struct block_device *bdev = lookup_bdev(path);
  388. if (IS_ERR(bdev))
  389. return PTR_ERR(bdev);
  390. dev = bdev->bd_dev;
  391. bdput(bdev);
  392. }
  393. dd = find_device(&t->devices, dev);
  394. if (!dd) {
  395. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  396. if (!dd)
  397. return -ENOMEM;
  398. dd->dm_dev.mode = mode;
  399. dd->dm_dev.bdev = NULL;
  400. if ((r = open_dev(dd, dev, t->md))) {
  401. kfree(dd);
  402. return r;
  403. }
  404. format_dev_t(dd->dm_dev.name, dev);
  405. atomic_set(&dd->count, 0);
  406. list_add(&dd->list, &t->devices);
  407. } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
  408. r = upgrade_mode(dd, mode, t->md);
  409. if (r)
  410. return r;
  411. }
  412. atomic_inc(&dd->count);
  413. *result = &dd->dm_dev;
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(dm_get_device);
  417. int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  418. sector_t start, sector_t len, void *data)
  419. {
  420. struct queue_limits *limits = data;
  421. struct block_device *bdev = dev->bdev;
  422. struct request_queue *q = bdev_get_queue(bdev);
  423. char b[BDEVNAME_SIZE];
  424. if (unlikely(!q)) {
  425. DMWARN("%s: Cannot set limits for nonexistent device %s",
  426. dm_device_name(ti->table->md), bdevname(bdev, b));
  427. return 0;
  428. }
  429. if (bdev_stack_limits(limits, bdev, start) < 0)
  430. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  431. "physical_block_size=%u, logical_block_size=%u, "
  432. "alignment_offset=%u, start=%llu",
  433. dm_device_name(ti->table->md), bdevname(bdev, b),
  434. q->limits.physical_block_size,
  435. q->limits.logical_block_size,
  436. q->limits.alignment_offset,
  437. (unsigned long long) start << SECTOR_SHIFT);
  438. /*
  439. * Check if merge fn is supported.
  440. * If not we'll force DM to use PAGE_SIZE or
  441. * smaller I/O, just to be safe.
  442. */
  443. if (dm_queue_merge_is_compulsory(q) && !ti->type->merge)
  444. blk_limits_max_hw_sectors(limits,
  445. (unsigned int) (PAGE_SIZE >> 9));
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(dm_set_device_limits);
  449. /*
  450. * Decrement a device's use count and remove it if necessary.
  451. */
  452. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  453. {
  454. struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
  455. dm_dev);
  456. if (atomic_dec_and_test(&dd->count)) {
  457. close_dev(dd, ti->table->md);
  458. list_del(&dd->list);
  459. kfree(dd);
  460. }
  461. }
  462. EXPORT_SYMBOL(dm_put_device);
  463. /*
  464. * Checks to see if the target joins onto the end of the table.
  465. */
  466. static int adjoin(struct dm_table *table, struct dm_target *ti)
  467. {
  468. struct dm_target *prev;
  469. if (!table->num_targets)
  470. return !ti->begin;
  471. prev = &table->targets[table->num_targets - 1];
  472. return (ti->begin == (prev->begin + prev->len));
  473. }
  474. /*
  475. * Used to dynamically allocate the arg array.
  476. */
  477. static char **realloc_argv(unsigned *array_size, char **old_argv)
  478. {
  479. char **argv;
  480. unsigned new_size;
  481. new_size = *array_size ? *array_size * 2 : 64;
  482. argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
  483. if (argv) {
  484. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  485. *array_size = new_size;
  486. }
  487. kfree(old_argv);
  488. return argv;
  489. }
  490. /*
  491. * Destructively splits up the argument list to pass to ctr.
  492. */
  493. int dm_split_args(int *argc, char ***argvp, char *input)
  494. {
  495. char *start, *end = input, *out, **argv = NULL;
  496. unsigned array_size = 0;
  497. *argc = 0;
  498. if (!input) {
  499. *argvp = NULL;
  500. return 0;
  501. }
  502. argv = realloc_argv(&array_size, argv);
  503. if (!argv)
  504. return -ENOMEM;
  505. while (1) {
  506. /* Skip whitespace */
  507. start = skip_spaces(end);
  508. if (!*start)
  509. break; /* success, we hit the end */
  510. /* 'out' is used to remove any back-quotes */
  511. end = out = start;
  512. while (*end) {
  513. /* Everything apart from '\0' can be quoted */
  514. if (*end == '\\' && *(end + 1)) {
  515. *out++ = *(end + 1);
  516. end += 2;
  517. continue;
  518. }
  519. if (isspace(*end))
  520. break; /* end of token */
  521. *out++ = *end++;
  522. }
  523. /* have we already filled the array ? */
  524. if ((*argc + 1) > array_size) {
  525. argv = realloc_argv(&array_size, argv);
  526. if (!argv)
  527. return -ENOMEM;
  528. }
  529. /* we know this is whitespace */
  530. if (*end)
  531. end++;
  532. /* terminate the string and put it in the array */
  533. *out = '\0';
  534. argv[*argc] = start;
  535. (*argc)++;
  536. }
  537. *argvp = argv;
  538. return 0;
  539. }
  540. /*
  541. * Impose necessary and sufficient conditions on a devices's table such
  542. * that any incoming bio which respects its logical_block_size can be
  543. * processed successfully. If it falls across the boundary between
  544. * two or more targets, the size of each piece it gets split into must
  545. * be compatible with the logical_block_size of the target processing it.
  546. */
  547. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  548. struct queue_limits *limits)
  549. {
  550. /*
  551. * This function uses arithmetic modulo the logical_block_size
  552. * (in units of 512-byte sectors).
  553. */
  554. unsigned short device_logical_block_size_sects =
  555. limits->logical_block_size >> SECTOR_SHIFT;
  556. /*
  557. * Offset of the start of the next table entry, mod logical_block_size.
  558. */
  559. unsigned short next_target_start = 0;
  560. /*
  561. * Given an aligned bio that extends beyond the end of a
  562. * target, how many sectors must the next target handle?
  563. */
  564. unsigned short remaining = 0;
  565. struct dm_target *uninitialized_var(ti);
  566. struct queue_limits ti_limits;
  567. unsigned i = 0;
  568. /*
  569. * Check each entry in the table in turn.
  570. */
  571. while (i < dm_table_get_num_targets(table)) {
  572. ti = dm_table_get_target(table, i++);
  573. blk_set_stacking_limits(&ti_limits);
  574. /* combine all target devices' limits */
  575. if (ti->type->iterate_devices)
  576. ti->type->iterate_devices(ti, dm_set_device_limits,
  577. &ti_limits);
  578. /*
  579. * If the remaining sectors fall entirely within this
  580. * table entry are they compatible with its logical_block_size?
  581. */
  582. if (remaining < ti->len &&
  583. remaining & ((ti_limits.logical_block_size >>
  584. SECTOR_SHIFT) - 1))
  585. break; /* Error */
  586. next_target_start =
  587. (unsigned short) ((next_target_start + ti->len) &
  588. (device_logical_block_size_sects - 1));
  589. remaining = next_target_start ?
  590. device_logical_block_size_sects - next_target_start : 0;
  591. }
  592. if (remaining) {
  593. DMWARN("%s: table line %u (start sect %llu len %llu) "
  594. "not aligned to h/w logical block size %u",
  595. dm_device_name(table->md), i,
  596. (unsigned long long) ti->begin,
  597. (unsigned long long) ti->len,
  598. limits->logical_block_size);
  599. return -EINVAL;
  600. }
  601. return 0;
  602. }
  603. int dm_table_add_target(struct dm_table *t, const char *type,
  604. sector_t start, sector_t len, char *params)
  605. {
  606. int r = -EINVAL, argc;
  607. char **argv;
  608. struct dm_target *tgt;
  609. if (t->singleton) {
  610. DMERR("%s: target type %s must appear alone in table",
  611. dm_device_name(t->md), t->targets->type->name);
  612. return -EINVAL;
  613. }
  614. if ((r = check_space(t)))
  615. return r;
  616. tgt = t->targets + t->num_targets;
  617. memset(tgt, 0, sizeof(*tgt));
  618. if (!len) {
  619. DMERR("%s: zero-length target", dm_device_name(t->md));
  620. return -EINVAL;
  621. }
  622. tgt->type = dm_get_target_type(type);
  623. if (!tgt->type) {
  624. DMERR("%s: %s: unknown target type", dm_device_name(t->md),
  625. type);
  626. return -EINVAL;
  627. }
  628. if (dm_target_needs_singleton(tgt->type)) {
  629. if (t->num_targets) {
  630. DMERR("%s: target type %s must appear alone in table",
  631. dm_device_name(t->md), type);
  632. return -EINVAL;
  633. }
  634. t->singleton = 1;
  635. }
  636. if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
  637. DMERR("%s: target type %s may not be included in read-only tables",
  638. dm_device_name(t->md), type);
  639. return -EINVAL;
  640. }
  641. if (t->immutable_target_type) {
  642. if (t->immutable_target_type != tgt->type) {
  643. DMERR("%s: immutable target type %s cannot be mixed with other target types",
  644. dm_device_name(t->md), t->immutable_target_type->name);
  645. return -EINVAL;
  646. }
  647. } else if (dm_target_is_immutable(tgt->type)) {
  648. if (t->num_targets) {
  649. DMERR("%s: immutable target type %s cannot be mixed with other target types",
  650. dm_device_name(t->md), tgt->type->name);
  651. return -EINVAL;
  652. }
  653. t->immutable_target_type = tgt->type;
  654. }
  655. tgt->table = t;
  656. tgt->begin = start;
  657. tgt->len = len;
  658. tgt->error = "Unknown error";
  659. /*
  660. * Does this target adjoin the previous one ?
  661. */
  662. if (!adjoin(t, tgt)) {
  663. tgt->error = "Gap in table";
  664. r = -EINVAL;
  665. goto bad;
  666. }
  667. r = dm_split_args(&argc, &argv, params);
  668. if (r) {
  669. tgt->error = "couldn't split parameters (insufficient memory)";
  670. goto bad;
  671. }
  672. r = tgt->type->ctr(tgt, argc, argv);
  673. kfree(argv);
  674. if (r)
  675. goto bad;
  676. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  677. if (!tgt->num_discard_requests && tgt->discards_supported)
  678. DMWARN("%s: %s: ignoring discards_supported because num_discard_requests is zero.",
  679. dm_device_name(t->md), type);
  680. return 0;
  681. bad:
  682. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  683. dm_put_target_type(tgt->type);
  684. return r;
  685. }
  686. /*
  687. * Target argument parsing helpers.
  688. */
  689. static int validate_next_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  690. unsigned *value, char **error, unsigned grouped)
  691. {
  692. const char *arg_str = dm_shift_arg(arg_set);
  693. char dummy;
  694. if (!arg_str ||
  695. (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
  696. (*value < arg->min) ||
  697. (*value > arg->max) ||
  698. (grouped && arg_set->argc < *value)) {
  699. *error = arg->error;
  700. return -EINVAL;
  701. }
  702. return 0;
  703. }
  704. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  705. unsigned *value, char **error)
  706. {
  707. return validate_next_arg(arg, arg_set, value, error, 0);
  708. }
  709. EXPORT_SYMBOL(dm_read_arg);
  710. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  711. unsigned *value, char **error)
  712. {
  713. return validate_next_arg(arg, arg_set, value, error, 1);
  714. }
  715. EXPORT_SYMBOL(dm_read_arg_group);
  716. const char *dm_shift_arg(struct dm_arg_set *as)
  717. {
  718. char *r;
  719. if (as->argc) {
  720. as->argc--;
  721. r = *as->argv;
  722. as->argv++;
  723. return r;
  724. }
  725. return NULL;
  726. }
  727. EXPORT_SYMBOL(dm_shift_arg);
  728. void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
  729. {
  730. BUG_ON(as->argc < num_args);
  731. as->argc -= num_args;
  732. as->argv += num_args;
  733. }
  734. EXPORT_SYMBOL(dm_consume_args);
  735. static int dm_table_set_type(struct dm_table *t)
  736. {
  737. unsigned i;
  738. unsigned bio_based = 0, request_based = 0;
  739. struct dm_target *tgt;
  740. struct dm_dev_internal *dd;
  741. struct list_head *devices;
  742. for (i = 0; i < t->num_targets; i++) {
  743. tgt = t->targets + i;
  744. if (dm_target_request_based(tgt))
  745. request_based = 1;
  746. else
  747. bio_based = 1;
  748. if (bio_based && request_based) {
  749. DMWARN("Inconsistent table: different target types"
  750. " can't be mixed up");
  751. return -EINVAL;
  752. }
  753. }
  754. if (bio_based) {
  755. /* We must use this table as bio-based */
  756. t->type = DM_TYPE_BIO_BASED;
  757. return 0;
  758. }
  759. BUG_ON(!request_based); /* No targets in this table */
  760. /* Non-request-stackable devices can't be used for request-based dm */
  761. devices = dm_table_get_devices(t);
  762. list_for_each_entry(dd, devices, list) {
  763. if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev.bdev))) {
  764. DMWARN("table load rejected: including"
  765. " non-request-stackable devices");
  766. return -EINVAL;
  767. }
  768. }
  769. /*
  770. * Request-based dm supports only tables that have a single target now.
  771. * To support multiple targets, request splitting support is needed,
  772. * and that needs lots of changes in the block-layer.
  773. * (e.g. request completion process for partial completion.)
  774. */
  775. if (t->num_targets > 1) {
  776. DMWARN("Request-based dm doesn't support multiple targets yet");
  777. return -EINVAL;
  778. }
  779. t->type = DM_TYPE_REQUEST_BASED;
  780. return 0;
  781. }
  782. unsigned dm_table_get_type(struct dm_table *t)
  783. {
  784. return t->type;
  785. }
  786. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  787. {
  788. return t->immutable_target_type;
  789. }
  790. bool dm_table_request_based(struct dm_table *t)
  791. {
  792. return dm_table_get_type(t) == DM_TYPE_REQUEST_BASED;
  793. }
  794. int dm_table_alloc_md_mempools(struct dm_table *t)
  795. {
  796. unsigned type = dm_table_get_type(t);
  797. if (unlikely(type == DM_TYPE_NONE)) {
  798. DMWARN("no table type is set, can't allocate mempools");
  799. return -EINVAL;
  800. }
  801. t->mempools = dm_alloc_md_mempools(type, t->integrity_supported);
  802. if (!t->mempools)
  803. return -ENOMEM;
  804. return 0;
  805. }
  806. void dm_table_free_md_mempools(struct dm_table *t)
  807. {
  808. dm_free_md_mempools(t->mempools);
  809. t->mempools = NULL;
  810. }
  811. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  812. {
  813. return t->mempools;
  814. }
  815. static int setup_indexes(struct dm_table *t)
  816. {
  817. int i;
  818. unsigned int total = 0;
  819. sector_t *indexes;
  820. /* allocate the space for *all* the indexes */
  821. for (i = t->depth - 2; i >= 0; i--) {
  822. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  823. total += t->counts[i];
  824. }
  825. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  826. if (!indexes)
  827. return -ENOMEM;
  828. /* set up internal nodes, bottom-up */
  829. for (i = t->depth - 2; i >= 0; i--) {
  830. t->index[i] = indexes;
  831. indexes += (KEYS_PER_NODE * t->counts[i]);
  832. setup_btree_index(i, t);
  833. }
  834. return 0;
  835. }
  836. /*
  837. * Builds the btree to index the map.
  838. */
  839. static int dm_table_build_index(struct dm_table *t)
  840. {
  841. int r = 0;
  842. unsigned int leaf_nodes;
  843. /* how many indexes will the btree have ? */
  844. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  845. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  846. /* leaf layer has already been set up */
  847. t->counts[t->depth - 1] = leaf_nodes;
  848. t->index[t->depth - 1] = t->highs;
  849. if (t->depth >= 2)
  850. r = setup_indexes(t);
  851. return r;
  852. }
  853. /*
  854. * Get a disk whose integrity profile reflects the table's profile.
  855. * If %match_all is true, all devices' profiles must match.
  856. * If %match_all is false, all devices must at least have an
  857. * allocated integrity profile; but uninitialized is ok.
  858. * Returns NULL if integrity support was inconsistent or unavailable.
  859. */
  860. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t,
  861. bool match_all)
  862. {
  863. struct list_head *devices = dm_table_get_devices(t);
  864. struct dm_dev_internal *dd = NULL;
  865. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  866. list_for_each_entry(dd, devices, list) {
  867. template_disk = dd->dm_dev.bdev->bd_disk;
  868. if (!blk_get_integrity(template_disk))
  869. goto no_integrity;
  870. if (!match_all && !blk_integrity_is_initialized(template_disk))
  871. continue; /* skip uninitialized profiles */
  872. else if (prev_disk &&
  873. blk_integrity_compare(prev_disk, template_disk) < 0)
  874. goto no_integrity;
  875. prev_disk = template_disk;
  876. }
  877. return template_disk;
  878. no_integrity:
  879. if (prev_disk)
  880. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  881. dm_device_name(t->md),
  882. prev_disk->disk_name,
  883. template_disk->disk_name);
  884. return NULL;
  885. }
  886. /*
  887. * Register the mapped device for blk_integrity support if
  888. * the underlying devices have an integrity profile. But all devices
  889. * may not have matching profiles (checking all devices isn't reliable
  890. * during table load because this table may use other DM device(s) which
  891. * must be resumed before they will have an initialized integity profile).
  892. * Stacked DM devices force a 2 stage integrity profile validation:
  893. * 1 - during load, validate all initialized integrity profiles match
  894. * 2 - during resume, validate all integrity profiles match
  895. */
  896. static int dm_table_prealloc_integrity(struct dm_table *t, struct mapped_device *md)
  897. {
  898. struct gendisk *template_disk = NULL;
  899. template_disk = dm_table_get_integrity_disk(t, false);
  900. if (!template_disk)
  901. return 0;
  902. if (!blk_integrity_is_initialized(dm_disk(md))) {
  903. t->integrity_supported = 1;
  904. return blk_integrity_register(dm_disk(md), NULL);
  905. }
  906. /*
  907. * If DM device already has an initalized integrity
  908. * profile the new profile should not conflict.
  909. */
  910. if (blk_integrity_is_initialized(template_disk) &&
  911. blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  912. DMWARN("%s: conflict with existing integrity profile: "
  913. "%s profile mismatch",
  914. dm_device_name(t->md),
  915. template_disk->disk_name);
  916. return 1;
  917. }
  918. /* Preserve existing initialized integrity profile */
  919. t->integrity_supported = 1;
  920. return 0;
  921. }
  922. /*
  923. * Prepares the table for use by building the indices,
  924. * setting the type, and allocating mempools.
  925. */
  926. int dm_table_complete(struct dm_table *t)
  927. {
  928. int r;
  929. r = dm_table_set_type(t);
  930. if (r) {
  931. DMERR("unable to set table type");
  932. return r;
  933. }
  934. r = dm_table_build_index(t);
  935. if (r) {
  936. DMERR("unable to build btrees");
  937. return r;
  938. }
  939. r = dm_table_prealloc_integrity(t, t->md);
  940. if (r) {
  941. DMERR("could not register integrity profile.");
  942. return r;
  943. }
  944. r = dm_table_alloc_md_mempools(t);
  945. if (r)
  946. DMERR("unable to allocate mempools");
  947. return r;
  948. }
  949. static DEFINE_MUTEX(_event_lock);
  950. void dm_table_event_callback(struct dm_table *t,
  951. void (*fn)(void *), void *context)
  952. {
  953. mutex_lock(&_event_lock);
  954. t->event_fn = fn;
  955. t->event_context = context;
  956. mutex_unlock(&_event_lock);
  957. }
  958. void dm_table_event(struct dm_table *t)
  959. {
  960. /*
  961. * You can no longer call dm_table_event() from interrupt
  962. * context, use a bottom half instead.
  963. */
  964. BUG_ON(in_interrupt());
  965. mutex_lock(&_event_lock);
  966. if (t->event_fn)
  967. t->event_fn(t->event_context);
  968. mutex_unlock(&_event_lock);
  969. }
  970. EXPORT_SYMBOL(dm_table_event);
  971. sector_t dm_table_get_size(struct dm_table *t)
  972. {
  973. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  974. }
  975. EXPORT_SYMBOL(dm_table_get_size);
  976. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  977. {
  978. if (index >= t->num_targets)
  979. return NULL;
  980. return t->targets + index;
  981. }
  982. /*
  983. * Search the btree for the correct target.
  984. *
  985. * Caller should check returned pointer with dm_target_is_valid()
  986. * to trap I/O beyond end of device.
  987. */
  988. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  989. {
  990. unsigned int l, n = 0, k = 0;
  991. sector_t *node;
  992. for (l = 0; l < t->depth; l++) {
  993. n = get_child(n, k);
  994. node = get_node(t, l, n);
  995. for (k = 0; k < KEYS_PER_NODE; k++)
  996. if (node[k] >= sector)
  997. break;
  998. }
  999. return &t->targets[(KEYS_PER_NODE * n) + k];
  1000. }
  1001. /*
  1002. * Establish the new table's queue_limits and validate them.
  1003. */
  1004. int dm_calculate_queue_limits(struct dm_table *table,
  1005. struct queue_limits *limits)
  1006. {
  1007. struct dm_target *uninitialized_var(ti);
  1008. struct queue_limits ti_limits;
  1009. unsigned i = 0;
  1010. blk_set_stacking_limits(limits);
  1011. while (i < dm_table_get_num_targets(table)) {
  1012. blk_set_stacking_limits(&ti_limits);
  1013. ti = dm_table_get_target(table, i++);
  1014. if (!ti->type->iterate_devices)
  1015. goto combine_limits;
  1016. /*
  1017. * Combine queue limits of all the devices this target uses.
  1018. */
  1019. ti->type->iterate_devices(ti, dm_set_device_limits,
  1020. &ti_limits);
  1021. /* Set I/O hints portion of queue limits */
  1022. if (ti->type->io_hints)
  1023. ti->type->io_hints(ti, &ti_limits);
  1024. /*
  1025. * Check each device area is consistent with the target's
  1026. * overall queue limits.
  1027. */
  1028. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1029. &ti_limits))
  1030. return -EINVAL;
  1031. combine_limits:
  1032. /*
  1033. * Merge this target's queue limits into the overall limits
  1034. * for the table.
  1035. */
  1036. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1037. DMWARN("%s: adding target device "
  1038. "(start sect %llu len %llu) "
  1039. "caused an alignment inconsistency",
  1040. dm_device_name(table->md),
  1041. (unsigned long long) ti->begin,
  1042. (unsigned long long) ti->len);
  1043. }
  1044. return validate_hardware_logical_block_alignment(table, limits);
  1045. }
  1046. /*
  1047. * Set the integrity profile for this device if all devices used have
  1048. * matching profiles. We're quite deep in the resume path but still
  1049. * don't know if all devices (particularly DM devices this device
  1050. * may be stacked on) have matching profiles. Even if the profiles
  1051. * don't match we have no way to fail (to resume) at this point.
  1052. */
  1053. static void dm_table_set_integrity(struct dm_table *t)
  1054. {
  1055. struct gendisk *template_disk = NULL;
  1056. if (!blk_get_integrity(dm_disk(t->md)))
  1057. return;
  1058. template_disk = dm_table_get_integrity_disk(t, true);
  1059. if (template_disk)
  1060. blk_integrity_register(dm_disk(t->md),
  1061. blk_get_integrity(template_disk));
  1062. else if (blk_integrity_is_initialized(dm_disk(t->md)))
  1063. DMWARN("%s: device no longer has a valid integrity profile",
  1064. dm_device_name(t->md));
  1065. else
  1066. DMWARN("%s: unable to establish an integrity profile",
  1067. dm_device_name(t->md));
  1068. }
  1069. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1070. sector_t start, sector_t len, void *data)
  1071. {
  1072. unsigned flush = (*(unsigned *)data);
  1073. struct request_queue *q = bdev_get_queue(dev->bdev);
  1074. return q && (q->flush_flags & flush);
  1075. }
  1076. static bool dm_table_supports_flush(struct dm_table *t, unsigned flush)
  1077. {
  1078. struct dm_target *ti;
  1079. unsigned i = 0;
  1080. /*
  1081. * Require at least one underlying device to support flushes.
  1082. * t->devices includes internal dm devices such as mirror logs
  1083. * so we need to use iterate_devices here, which targets
  1084. * supporting flushes must provide.
  1085. */
  1086. while (i < dm_table_get_num_targets(t)) {
  1087. ti = dm_table_get_target(t, i++);
  1088. if (!ti->num_flush_requests)
  1089. continue;
  1090. if (ti->type->iterate_devices &&
  1091. ti->type->iterate_devices(ti, device_flush_capable, &flush))
  1092. return 1;
  1093. }
  1094. return 0;
  1095. }
  1096. static bool dm_table_discard_zeroes_data(struct dm_table *t)
  1097. {
  1098. struct dm_target *ti;
  1099. unsigned i = 0;
  1100. /* Ensure that all targets supports discard_zeroes_data. */
  1101. while (i < dm_table_get_num_targets(t)) {
  1102. ti = dm_table_get_target(t, i++);
  1103. if (ti->discard_zeroes_data_unsupported)
  1104. return 0;
  1105. }
  1106. return 1;
  1107. }
  1108. static int device_is_nonrot(struct dm_target *ti, struct dm_dev *dev,
  1109. sector_t start, sector_t len, void *data)
  1110. {
  1111. struct request_queue *q = bdev_get_queue(dev->bdev);
  1112. return q && blk_queue_nonrot(q);
  1113. }
  1114. static bool dm_table_is_nonrot(struct dm_table *t)
  1115. {
  1116. struct dm_target *ti;
  1117. unsigned i = 0;
  1118. /* Ensure that all underlying device are non-rotational. */
  1119. while (i < dm_table_get_num_targets(t)) {
  1120. ti = dm_table_get_target(t, i++);
  1121. if (!ti->type->iterate_devices ||
  1122. !ti->type->iterate_devices(ti, device_is_nonrot, NULL))
  1123. return 0;
  1124. }
  1125. return 1;
  1126. }
  1127. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1128. struct queue_limits *limits)
  1129. {
  1130. unsigned flush = 0;
  1131. /*
  1132. * Copy table's limits to the DM device's request_queue
  1133. */
  1134. q->limits = *limits;
  1135. if (!dm_table_supports_discards(t))
  1136. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  1137. else
  1138. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1139. if (dm_table_supports_flush(t, REQ_FLUSH)) {
  1140. flush |= REQ_FLUSH;
  1141. if (dm_table_supports_flush(t, REQ_FUA))
  1142. flush |= REQ_FUA;
  1143. }
  1144. blk_queue_flush(q, flush);
  1145. if (!dm_table_discard_zeroes_data(t))
  1146. q->limits.discard_zeroes_data = 0;
  1147. if (dm_table_is_nonrot(t))
  1148. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  1149. else
  1150. queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
  1151. dm_table_set_integrity(t);
  1152. /*
  1153. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  1154. * visible to other CPUs because, once the flag is set, incoming bios
  1155. * are processed by request-based dm, which refers to the queue
  1156. * settings.
  1157. * Until the flag set, bios are passed to bio-based dm and queued to
  1158. * md->deferred where queue settings are not needed yet.
  1159. * Those bios are passed to request-based dm at the resume time.
  1160. */
  1161. smp_mb();
  1162. if (dm_table_request_based(t))
  1163. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  1164. }
  1165. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1166. {
  1167. return t->num_targets;
  1168. }
  1169. struct list_head *dm_table_get_devices(struct dm_table *t)
  1170. {
  1171. return &t->devices;
  1172. }
  1173. fmode_t dm_table_get_mode(struct dm_table *t)
  1174. {
  1175. return t->mode;
  1176. }
  1177. EXPORT_SYMBOL(dm_table_get_mode);
  1178. static void suspend_targets(struct dm_table *t, unsigned postsuspend)
  1179. {
  1180. int i = t->num_targets;
  1181. struct dm_target *ti = t->targets;
  1182. while (i--) {
  1183. if (postsuspend) {
  1184. if (ti->type->postsuspend)
  1185. ti->type->postsuspend(ti);
  1186. } else if (ti->type->presuspend)
  1187. ti->type->presuspend(ti);
  1188. ti++;
  1189. }
  1190. }
  1191. void dm_table_presuspend_targets(struct dm_table *t)
  1192. {
  1193. if (!t)
  1194. return;
  1195. suspend_targets(t, 0);
  1196. }
  1197. void dm_table_postsuspend_targets(struct dm_table *t)
  1198. {
  1199. if (!t)
  1200. return;
  1201. suspend_targets(t, 1);
  1202. }
  1203. int dm_table_resume_targets(struct dm_table *t)
  1204. {
  1205. int i, r = 0;
  1206. for (i = 0; i < t->num_targets; i++) {
  1207. struct dm_target *ti = t->targets + i;
  1208. if (!ti->type->preresume)
  1209. continue;
  1210. r = ti->type->preresume(ti);
  1211. if (r)
  1212. return r;
  1213. }
  1214. for (i = 0; i < t->num_targets; i++) {
  1215. struct dm_target *ti = t->targets + i;
  1216. if (ti->type->resume)
  1217. ti->type->resume(ti);
  1218. }
  1219. return 0;
  1220. }
  1221. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1222. {
  1223. list_add(&cb->list, &t->target_callbacks);
  1224. }
  1225. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1226. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1227. {
  1228. struct dm_dev_internal *dd;
  1229. struct list_head *devices = dm_table_get_devices(t);
  1230. struct dm_target_callbacks *cb;
  1231. int r = 0;
  1232. list_for_each_entry(dd, devices, list) {
  1233. struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
  1234. char b[BDEVNAME_SIZE];
  1235. if (likely(q))
  1236. r |= bdi_congested(&q->backing_dev_info, bdi_bits);
  1237. else
  1238. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1239. dm_device_name(t->md),
  1240. bdevname(dd->dm_dev.bdev, b));
  1241. }
  1242. list_for_each_entry(cb, &t->target_callbacks, list)
  1243. if (cb->congested_fn)
  1244. r |= cb->congested_fn(cb, bdi_bits);
  1245. return r;
  1246. }
  1247. int dm_table_any_busy_target(struct dm_table *t)
  1248. {
  1249. unsigned i;
  1250. struct dm_target *ti;
  1251. for (i = 0; i < t->num_targets; i++) {
  1252. ti = t->targets + i;
  1253. if (ti->type->busy && ti->type->busy(ti))
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1259. {
  1260. return t->md;
  1261. }
  1262. EXPORT_SYMBOL(dm_table_get_md);
  1263. static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1264. sector_t start, sector_t len, void *data)
  1265. {
  1266. struct request_queue *q = bdev_get_queue(dev->bdev);
  1267. return q && blk_queue_discard(q);
  1268. }
  1269. bool dm_table_supports_discards(struct dm_table *t)
  1270. {
  1271. struct dm_target *ti;
  1272. unsigned i = 0;
  1273. /*
  1274. * Unless any target used by the table set discards_supported,
  1275. * require at least one underlying device to support discards.
  1276. * t->devices includes internal dm devices such as mirror logs
  1277. * so we need to use iterate_devices here, which targets
  1278. * supporting discard selectively must provide.
  1279. */
  1280. while (i < dm_table_get_num_targets(t)) {
  1281. ti = dm_table_get_target(t, i++);
  1282. if (!ti->num_discard_requests)
  1283. continue;
  1284. if (ti->discards_supported)
  1285. return 1;
  1286. if (ti->type->iterate_devices &&
  1287. ti->type->iterate_devices(ti, device_discard_capable, NULL))
  1288. return 1;
  1289. }
  1290. return 0;
  1291. }