dm-array.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-array.h"
  8. #include "dm-space-map.h"
  9. #include "dm-transaction-manager.h"
  10. #include <linux/export.h>
  11. #include <linux/device-mapper.h>
  12. #define DM_MSG_PREFIX "array"
  13. /*----------------------------------------------------------------*/
  14. /*
  15. * The array is implemented as a fully populated btree, which points to
  16. * blocks that contain the packed values. This is more space efficient
  17. * than just using a btree since we don't store 1 key per value.
  18. */
  19. struct array_block {
  20. __le32 csum;
  21. __le32 max_entries;
  22. __le32 nr_entries;
  23. __le32 value_size;
  24. __le64 blocknr; /* Block this node is supposed to live in. */
  25. } __packed;
  26. /*----------------------------------------------------------------*/
  27. /*
  28. * Validator methods. As usual we calculate a checksum, and also write the
  29. * block location into the header (paranoia about ssds remapping areas by
  30. * mistake).
  31. */
  32. #define CSUM_XOR 595846735
  33. static void array_block_prepare_for_write(const struct dm_block_validator *v,
  34. struct dm_block *b,
  35. size_t size_of_block)
  36. {
  37. struct array_block *bh_le = dm_block_data(b);
  38. bh_le->blocknr = cpu_to_le64(dm_block_location(b));
  39. bh_le->csum = cpu_to_le32(dm_bm_checksum(&bh_le->max_entries,
  40. size_of_block - sizeof(__le32),
  41. CSUM_XOR));
  42. }
  43. static int array_block_check(const struct dm_block_validator *v,
  44. struct dm_block *b,
  45. size_t size_of_block)
  46. {
  47. struct array_block *bh_le = dm_block_data(b);
  48. __le32 csum_disk;
  49. if (dm_block_location(b) != le64_to_cpu(bh_le->blocknr)) {
  50. DMERR_LIMIT("%s failed: blocknr %llu != wanted %llu", __func__,
  51. (unsigned long long) le64_to_cpu(bh_le->blocknr),
  52. (unsigned long long) dm_block_location(b));
  53. return -ENOTBLK;
  54. }
  55. csum_disk = cpu_to_le32(dm_bm_checksum(&bh_le->max_entries,
  56. size_of_block - sizeof(__le32),
  57. CSUM_XOR));
  58. if (csum_disk != bh_le->csum) {
  59. DMERR_LIMIT("%s failed: csum %u != wanted %u", __func__,
  60. (unsigned int) le32_to_cpu(csum_disk),
  61. (unsigned int) le32_to_cpu(bh_le->csum));
  62. return -EILSEQ;
  63. }
  64. return 0;
  65. }
  66. static const struct dm_block_validator array_validator = {
  67. .name = "array",
  68. .prepare_for_write = array_block_prepare_for_write,
  69. .check = array_block_check
  70. };
  71. /*----------------------------------------------------------------*/
  72. /*
  73. * Functions for manipulating the array blocks.
  74. */
  75. /*
  76. * Returns a pointer to a value within an array block.
  77. *
  78. * index - The index into _this_ specific block.
  79. */
  80. static void *element_at(struct dm_array_info *info, struct array_block *ab,
  81. unsigned int index)
  82. {
  83. unsigned char *entry = (unsigned char *) (ab + 1);
  84. entry += index * info->value_type.size;
  85. return entry;
  86. }
  87. /*
  88. * Utility function that calls one of the value_type methods on every value
  89. * in an array block.
  90. */
  91. static void on_entries(struct dm_array_info *info, struct array_block *ab,
  92. void (*fn)(void *, const void *, unsigned int))
  93. {
  94. unsigned int nr_entries = le32_to_cpu(ab->nr_entries);
  95. fn(info->value_type.context, element_at(info, ab, 0), nr_entries);
  96. }
  97. /*
  98. * Increment every value in an array block.
  99. */
  100. static void inc_ablock_entries(struct dm_array_info *info, struct array_block *ab)
  101. {
  102. struct dm_btree_value_type *vt = &info->value_type;
  103. if (vt->inc)
  104. on_entries(info, ab, vt->inc);
  105. }
  106. /*
  107. * Decrement every value in an array block.
  108. */
  109. static void dec_ablock_entries(struct dm_array_info *info, struct array_block *ab)
  110. {
  111. struct dm_btree_value_type *vt = &info->value_type;
  112. if (vt->dec)
  113. on_entries(info, ab, vt->dec);
  114. }
  115. /*
  116. * Each array block can hold this many values.
  117. */
  118. static uint32_t calc_max_entries(size_t value_size, size_t size_of_block)
  119. {
  120. return (size_of_block - sizeof(struct array_block)) / value_size;
  121. }
  122. /*
  123. * Allocate a new array block. The caller will need to unlock block.
  124. */
  125. static int alloc_ablock(struct dm_array_info *info, size_t size_of_block,
  126. uint32_t max_entries,
  127. struct dm_block **block, struct array_block **ab)
  128. {
  129. int r;
  130. r = dm_tm_new_block(info->btree_info.tm, &array_validator, block);
  131. if (r)
  132. return r;
  133. (*ab) = dm_block_data(*block);
  134. (*ab)->max_entries = cpu_to_le32(max_entries);
  135. (*ab)->nr_entries = cpu_to_le32(0);
  136. (*ab)->value_size = cpu_to_le32(info->value_type.size);
  137. return 0;
  138. }
  139. /*
  140. * Pad an array block out with a particular value. Every instance will
  141. * cause an increment of the value_type. new_nr must always be more than
  142. * the current number of entries.
  143. */
  144. static void fill_ablock(struct dm_array_info *info, struct array_block *ab,
  145. const void *value, unsigned int new_nr)
  146. {
  147. uint32_t nr_entries, delta, i;
  148. struct dm_btree_value_type *vt = &info->value_type;
  149. BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
  150. BUG_ON(new_nr < le32_to_cpu(ab->nr_entries));
  151. nr_entries = le32_to_cpu(ab->nr_entries);
  152. delta = new_nr - nr_entries;
  153. if (vt->inc)
  154. vt->inc(vt->context, value, delta);
  155. for (i = nr_entries; i < new_nr; i++)
  156. memcpy(element_at(info, ab, i), value, vt->size);
  157. ab->nr_entries = cpu_to_le32(new_nr);
  158. }
  159. /*
  160. * Remove some entries from the back of an array block. Every value
  161. * removed will be decremented. new_nr must be <= the current number of
  162. * entries.
  163. */
  164. static void trim_ablock(struct dm_array_info *info, struct array_block *ab,
  165. unsigned int new_nr)
  166. {
  167. uint32_t nr_entries, delta;
  168. struct dm_btree_value_type *vt = &info->value_type;
  169. BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
  170. BUG_ON(new_nr > le32_to_cpu(ab->nr_entries));
  171. nr_entries = le32_to_cpu(ab->nr_entries);
  172. delta = nr_entries - new_nr;
  173. if (vt->dec)
  174. vt->dec(vt->context, element_at(info, ab, new_nr - 1), delta);
  175. ab->nr_entries = cpu_to_le32(new_nr);
  176. }
  177. /*
  178. * Read locks a block, and coerces it to an array block. The caller must
  179. * unlock 'block' when finished.
  180. */
  181. static int get_ablock(struct dm_array_info *info, dm_block_t b,
  182. struct dm_block **block, struct array_block **ab)
  183. {
  184. int r;
  185. r = dm_tm_read_lock(info->btree_info.tm, b, &array_validator, block);
  186. if (r)
  187. return r;
  188. *ab = dm_block_data(*block);
  189. return 0;
  190. }
  191. /*
  192. * Unlocks an array block.
  193. */
  194. static void unlock_ablock(struct dm_array_info *info, struct dm_block *block)
  195. {
  196. dm_tm_unlock(info->btree_info.tm, block);
  197. }
  198. /*----------------------------------------------------------------*/
  199. /*
  200. * Btree manipulation.
  201. */
  202. /*
  203. * Looks up an array block in the btree, and then read locks it.
  204. *
  205. * index is the index of the index of the array_block, (ie. the array index
  206. * / max_entries).
  207. */
  208. static int lookup_ablock(struct dm_array_info *info, dm_block_t root,
  209. unsigned int index, struct dm_block **block,
  210. struct array_block **ab)
  211. {
  212. int r;
  213. uint64_t key = index;
  214. __le64 block_le;
  215. r = dm_btree_lookup(&info->btree_info, root, &key, &block_le);
  216. if (r)
  217. return r;
  218. return get_ablock(info, le64_to_cpu(block_le), block, ab);
  219. }
  220. /*
  221. * Insert an array block into the btree. The block is _not_ unlocked.
  222. */
  223. static int insert_ablock(struct dm_array_info *info, uint64_t index,
  224. struct dm_block *block, dm_block_t *root)
  225. {
  226. __le64 block_le = cpu_to_le64(dm_block_location(block));
  227. __dm_bless_for_disk(block_le);
  228. return dm_btree_insert(&info->btree_info, *root, &index, &block_le, root);
  229. }
  230. /*----------------------------------------------------------------*/
  231. static int __shadow_ablock(struct dm_array_info *info, dm_block_t b,
  232. struct dm_block **block, struct array_block **ab)
  233. {
  234. int inc;
  235. int r = dm_tm_shadow_block(info->btree_info.tm, b,
  236. &array_validator, block, &inc);
  237. if (r)
  238. return r;
  239. *ab = dm_block_data(*block);
  240. if (inc)
  241. inc_ablock_entries(info, *ab);
  242. return 0;
  243. }
  244. /*
  245. * The shadow op will often be a noop. Only insert if it really
  246. * copied data.
  247. */
  248. static int __reinsert_ablock(struct dm_array_info *info, unsigned int index,
  249. struct dm_block *block, dm_block_t b,
  250. dm_block_t *root)
  251. {
  252. int r = 0;
  253. if (dm_block_location(block) != b) {
  254. /*
  255. * dm_tm_shadow_block will have already decremented the old
  256. * block, but it is still referenced by the btree. We
  257. * increment to stop the insert decrementing it below zero
  258. * when overwriting the old value.
  259. */
  260. dm_tm_inc(info->btree_info.tm, b);
  261. r = insert_ablock(info, index, block, root);
  262. }
  263. return r;
  264. }
  265. /*
  266. * Looks up an array block in the btree. Then shadows it, and updates the
  267. * btree to point to this new shadow. 'root' is an input/output parameter
  268. * for both the current root block, and the new one.
  269. */
  270. static int shadow_ablock(struct dm_array_info *info, dm_block_t *root,
  271. unsigned int index, struct dm_block **block,
  272. struct array_block **ab)
  273. {
  274. int r;
  275. uint64_t key = index;
  276. dm_block_t b;
  277. __le64 block_le;
  278. r = dm_btree_lookup(&info->btree_info, *root, &key, &block_le);
  279. if (r)
  280. return r;
  281. b = le64_to_cpu(block_le);
  282. r = __shadow_ablock(info, b, block, ab);
  283. if (r)
  284. return r;
  285. return __reinsert_ablock(info, index, *block, b, root);
  286. }
  287. /*
  288. * Allocate an new array block, and fill it with some values.
  289. */
  290. static int insert_new_ablock(struct dm_array_info *info, size_t size_of_block,
  291. uint32_t max_entries,
  292. unsigned int block_index, uint32_t nr,
  293. const void *value, dm_block_t *root)
  294. {
  295. int r;
  296. struct dm_block *block;
  297. struct array_block *ab;
  298. r = alloc_ablock(info, size_of_block, max_entries, &block, &ab);
  299. if (r)
  300. return r;
  301. fill_ablock(info, ab, value, nr);
  302. r = insert_ablock(info, block_index, block, root);
  303. unlock_ablock(info, block);
  304. return r;
  305. }
  306. static int insert_full_ablocks(struct dm_array_info *info, size_t size_of_block,
  307. unsigned int begin_block, unsigned int end_block,
  308. unsigned int max_entries, const void *value,
  309. dm_block_t *root)
  310. {
  311. int r = 0;
  312. for (; !r && begin_block != end_block; begin_block++)
  313. r = insert_new_ablock(info, size_of_block, max_entries, begin_block, max_entries, value, root);
  314. return r;
  315. }
  316. /*
  317. * There are a bunch of functions involved with resizing an array. This
  318. * structure holds information that commonly needed by them. Purely here
  319. * to reduce parameter count.
  320. */
  321. struct resize {
  322. /*
  323. * Describes the array.
  324. */
  325. struct dm_array_info *info;
  326. /*
  327. * The current root of the array. This gets updated.
  328. */
  329. dm_block_t root;
  330. /*
  331. * Metadata block size. Used to calculate the nr entries in an
  332. * array block.
  333. */
  334. size_t size_of_block;
  335. /*
  336. * Maximum nr entries in an array block.
  337. */
  338. unsigned int max_entries;
  339. /*
  340. * nr of completely full blocks in the array.
  341. *
  342. * 'old' refers to before the resize, 'new' after.
  343. */
  344. unsigned int old_nr_full_blocks, new_nr_full_blocks;
  345. /*
  346. * Number of entries in the final block. 0 iff only full blocks in
  347. * the array.
  348. */
  349. unsigned int old_nr_entries_in_last_block, new_nr_entries_in_last_block;
  350. /*
  351. * The default value used when growing the array.
  352. */
  353. const void *value;
  354. };
  355. /*
  356. * Removes a consecutive set of array blocks from the btree. The values
  357. * in block are decremented as a side effect of the btree remove.
  358. *
  359. * begin_index - the index of the first array block to remove.
  360. * end_index - the one-past-the-end value. ie. this block is not removed.
  361. */
  362. static int drop_blocks(struct resize *resize, unsigned int begin_index,
  363. unsigned int end_index)
  364. {
  365. int r;
  366. while (begin_index != end_index) {
  367. uint64_t key = begin_index++;
  368. r = dm_btree_remove(&resize->info->btree_info, resize->root,
  369. &key, &resize->root);
  370. if (r)
  371. return r;
  372. }
  373. return 0;
  374. }
  375. /*
  376. * Calculates how many blocks are needed for the array.
  377. */
  378. static unsigned int total_nr_blocks_needed(unsigned int nr_full_blocks,
  379. unsigned int nr_entries_in_last_block)
  380. {
  381. return nr_full_blocks + (nr_entries_in_last_block ? 1 : 0);
  382. }
  383. /*
  384. * Shrink an array.
  385. */
  386. static int shrink(struct resize *resize)
  387. {
  388. int r;
  389. unsigned int begin, end;
  390. struct dm_block *block;
  391. struct array_block *ab;
  392. /*
  393. * Lose some blocks from the back?
  394. */
  395. if (resize->new_nr_full_blocks < resize->old_nr_full_blocks) {
  396. begin = total_nr_blocks_needed(resize->new_nr_full_blocks,
  397. resize->new_nr_entries_in_last_block);
  398. end = total_nr_blocks_needed(resize->old_nr_full_blocks,
  399. resize->old_nr_entries_in_last_block);
  400. r = drop_blocks(resize, begin, end);
  401. if (r)
  402. return r;
  403. }
  404. /*
  405. * Trim the new tail block
  406. */
  407. if (resize->new_nr_entries_in_last_block) {
  408. r = shadow_ablock(resize->info, &resize->root,
  409. resize->new_nr_full_blocks, &block, &ab);
  410. if (r)
  411. return r;
  412. trim_ablock(resize->info, ab, resize->new_nr_entries_in_last_block);
  413. unlock_ablock(resize->info, block);
  414. }
  415. return 0;
  416. }
  417. /*
  418. * Grow an array.
  419. */
  420. static int grow_extend_tail_block(struct resize *resize, uint32_t new_nr_entries)
  421. {
  422. int r;
  423. struct dm_block *block;
  424. struct array_block *ab;
  425. r = shadow_ablock(resize->info, &resize->root,
  426. resize->old_nr_full_blocks, &block, &ab);
  427. if (r)
  428. return r;
  429. fill_ablock(resize->info, ab, resize->value, new_nr_entries);
  430. unlock_ablock(resize->info, block);
  431. return r;
  432. }
  433. static int grow_add_tail_block(struct resize *resize)
  434. {
  435. return insert_new_ablock(resize->info, resize->size_of_block,
  436. resize->max_entries,
  437. resize->new_nr_full_blocks,
  438. resize->new_nr_entries_in_last_block,
  439. resize->value, &resize->root);
  440. }
  441. static int grow_needs_more_blocks(struct resize *resize)
  442. {
  443. int r;
  444. unsigned int old_nr_blocks = resize->old_nr_full_blocks;
  445. if (resize->old_nr_entries_in_last_block > 0) {
  446. old_nr_blocks++;
  447. r = grow_extend_tail_block(resize, resize->max_entries);
  448. if (r)
  449. return r;
  450. }
  451. r = insert_full_ablocks(resize->info, resize->size_of_block,
  452. old_nr_blocks,
  453. resize->new_nr_full_blocks,
  454. resize->max_entries, resize->value,
  455. &resize->root);
  456. if (r)
  457. return r;
  458. if (resize->new_nr_entries_in_last_block)
  459. r = grow_add_tail_block(resize);
  460. return r;
  461. }
  462. static int grow(struct resize *resize)
  463. {
  464. if (resize->new_nr_full_blocks > resize->old_nr_full_blocks)
  465. return grow_needs_more_blocks(resize);
  466. else if (resize->old_nr_entries_in_last_block)
  467. return grow_extend_tail_block(resize, resize->new_nr_entries_in_last_block);
  468. else
  469. return grow_add_tail_block(resize);
  470. }
  471. /*----------------------------------------------------------------*/
  472. /*
  473. * These are the value_type functions for the btree elements, which point
  474. * to array blocks.
  475. */
  476. static void block_inc(void *context, const void *value, unsigned int count)
  477. {
  478. const __le64 *block_le = value;
  479. struct dm_array_info *info = context;
  480. unsigned int i;
  481. for (i = 0; i < count; i++, block_le++)
  482. dm_tm_inc(info->btree_info.tm, le64_to_cpu(*block_le));
  483. }
  484. static void __block_dec(void *context, const void *value)
  485. {
  486. int r;
  487. uint64_t b;
  488. __le64 block_le;
  489. uint32_t ref_count;
  490. struct dm_block *block;
  491. struct array_block *ab;
  492. struct dm_array_info *info = context;
  493. memcpy(&block_le, value, sizeof(block_le));
  494. b = le64_to_cpu(block_le);
  495. r = dm_tm_ref(info->btree_info.tm, b, &ref_count);
  496. if (r) {
  497. DMERR_LIMIT("couldn't get reference count for block %llu",
  498. (unsigned long long) b);
  499. return;
  500. }
  501. if (ref_count == 1) {
  502. /*
  503. * We're about to drop the last reference to this ablock.
  504. * So we need to decrement the ref count of the contents.
  505. */
  506. r = get_ablock(info, b, &block, &ab);
  507. if (r) {
  508. DMERR_LIMIT("couldn't get array block %llu",
  509. (unsigned long long) b);
  510. return;
  511. }
  512. dec_ablock_entries(info, ab);
  513. unlock_ablock(info, block);
  514. }
  515. dm_tm_dec(info->btree_info.tm, b);
  516. }
  517. static void block_dec(void *context, const void *value, unsigned int count)
  518. {
  519. unsigned int i;
  520. for (i = 0; i < count; i++, value += sizeof(__le64))
  521. __block_dec(context, value);
  522. }
  523. static int block_equal(void *context, const void *value1, const void *value2)
  524. {
  525. return !memcmp(value1, value2, sizeof(__le64));
  526. }
  527. /*----------------------------------------------------------------*/
  528. void dm_array_info_init(struct dm_array_info *info,
  529. struct dm_transaction_manager *tm,
  530. struct dm_btree_value_type *vt)
  531. {
  532. struct dm_btree_value_type *bvt = &info->btree_info.value_type;
  533. memcpy(&info->value_type, vt, sizeof(info->value_type));
  534. info->btree_info.tm = tm;
  535. info->btree_info.levels = 1;
  536. bvt->context = info;
  537. bvt->size = sizeof(__le64);
  538. bvt->inc = block_inc;
  539. bvt->dec = block_dec;
  540. bvt->equal = block_equal;
  541. }
  542. EXPORT_SYMBOL_GPL(dm_array_info_init);
  543. int dm_array_empty(struct dm_array_info *info, dm_block_t *root)
  544. {
  545. return dm_btree_empty(&info->btree_info, root);
  546. }
  547. EXPORT_SYMBOL_GPL(dm_array_empty);
  548. static int array_resize(struct dm_array_info *info, dm_block_t root,
  549. uint32_t old_size, uint32_t new_size,
  550. const void *value, dm_block_t *new_root)
  551. {
  552. int r;
  553. struct resize resize;
  554. if (old_size == new_size) {
  555. *new_root = root;
  556. return 0;
  557. }
  558. resize.info = info;
  559. resize.root = root;
  560. resize.size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
  561. resize.max_entries = calc_max_entries(info->value_type.size,
  562. resize.size_of_block);
  563. resize.old_nr_full_blocks = old_size / resize.max_entries;
  564. resize.old_nr_entries_in_last_block = old_size % resize.max_entries;
  565. resize.new_nr_full_blocks = new_size / resize.max_entries;
  566. resize.new_nr_entries_in_last_block = new_size % resize.max_entries;
  567. resize.value = value;
  568. r = ((new_size > old_size) ? grow : shrink)(&resize);
  569. if (r)
  570. return r;
  571. *new_root = resize.root;
  572. return 0;
  573. }
  574. int dm_array_resize(struct dm_array_info *info, dm_block_t root,
  575. uint32_t old_size, uint32_t new_size,
  576. const void *value, dm_block_t *new_root)
  577. __dm_written_to_disk(value)
  578. {
  579. int r = array_resize(info, root, old_size, new_size, value, new_root);
  580. __dm_unbless_for_disk(value);
  581. return r;
  582. }
  583. EXPORT_SYMBOL_GPL(dm_array_resize);
  584. static int populate_ablock_with_values(struct dm_array_info *info, struct array_block *ab,
  585. value_fn fn, void *context,
  586. unsigned int base, unsigned int new_nr)
  587. {
  588. int r;
  589. unsigned int i;
  590. struct dm_btree_value_type *vt = &info->value_type;
  591. BUG_ON(le32_to_cpu(ab->nr_entries));
  592. BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
  593. for (i = 0; i < new_nr; i++) {
  594. r = fn(base + i, element_at(info, ab, i), context);
  595. if (r)
  596. return r;
  597. if (vt->inc)
  598. vt->inc(vt->context, element_at(info, ab, i), 1);
  599. }
  600. ab->nr_entries = cpu_to_le32(new_nr);
  601. return 0;
  602. }
  603. int dm_array_new(struct dm_array_info *info, dm_block_t *root,
  604. uint32_t size, value_fn fn, void *context)
  605. {
  606. int r;
  607. struct dm_block *block;
  608. struct array_block *ab;
  609. unsigned int block_index, end_block, size_of_block, max_entries;
  610. r = dm_array_empty(info, root);
  611. if (r)
  612. return r;
  613. size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
  614. max_entries = calc_max_entries(info->value_type.size, size_of_block);
  615. end_block = dm_div_up(size, max_entries);
  616. for (block_index = 0; block_index != end_block; block_index++) {
  617. r = alloc_ablock(info, size_of_block, max_entries, &block, &ab);
  618. if (r)
  619. break;
  620. r = populate_ablock_with_values(info, ab, fn, context,
  621. block_index * max_entries,
  622. min(max_entries, size));
  623. if (r) {
  624. unlock_ablock(info, block);
  625. break;
  626. }
  627. r = insert_ablock(info, block_index, block, root);
  628. unlock_ablock(info, block);
  629. if (r)
  630. break;
  631. size -= max_entries;
  632. }
  633. return r;
  634. }
  635. EXPORT_SYMBOL_GPL(dm_array_new);
  636. int dm_array_del(struct dm_array_info *info, dm_block_t root)
  637. {
  638. return dm_btree_del(&info->btree_info, root);
  639. }
  640. EXPORT_SYMBOL_GPL(dm_array_del);
  641. int dm_array_get_value(struct dm_array_info *info, dm_block_t root,
  642. uint32_t index, void *value_le)
  643. {
  644. int r;
  645. struct dm_block *block;
  646. struct array_block *ab;
  647. size_t size_of_block;
  648. unsigned int entry, max_entries;
  649. size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
  650. max_entries = calc_max_entries(info->value_type.size, size_of_block);
  651. r = lookup_ablock(info, root, index / max_entries, &block, &ab);
  652. if (r)
  653. return r;
  654. entry = index % max_entries;
  655. if (entry >= le32_to_cpu(ab->nr_entries))
  656. r = -ENODATA;
  657. else
  658. memcpy(value_le, element_at(info, ab, entry),
  659. info->value_type.size);
  660. unlock_ablock(info, block);
  661. return r;
  662. }
  663. EXPORT_SYMBOL_GPL(dm_array_get_value);
  664. static int array_set_value(struct dm_array_info *info, dm_block_t root,
  665. uint32_t index, const void *value, dm_block_t *new_root)
  666. {
  667. int r;
  668. struct dm_block *block;
  669. struct array_block *ab;
  670. size_t size_of_block;
  671. unsigned int max_entries;
  672. unsigned int entry;
  673. void *old_value;
  674. struct dm_btree_value_type *vt = &info->value_type;
  675. size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
  676. max_entries = calc_max_entries(info->value_type.size, size_of_block);
  677. r = shadow_ablock(info, &root, index / max_entries, &block, &ab);
  678. if (r)
  679. return r;
  680. *new_root = root;
  681. entry = index % max_entries;
  682. if (entry >= le32_to_cpu(ab->nr_entries)) {
  683. r = -ENODATA;
  684. goto out;
  685. }
  686. old_value = element_at(info, ab, entry);
  687. if (vt->dec &&
  688. (!vt->equal || !vt->equal(vt->context, old_value, value))) {
  689. vt->dec(vt->context, old_value, 1);
  690. if (vt->inc)
  691. vt->inc(vt->context, value, 1);
  692. }
  693. memcpy(old_value, value, info->value_type.size);
  694. out:
  695. unlock_ablock(info, block);
  696. return r;
  697. }
  698. int dm_array_set_value(struct dm_array_info *info, dm_block_t root,
  699. uint32_t index, const void *value, dm_block_t *new_root)
  700. __dm_written_to_disk(value)
  701. {
  702. int r;
  703. r = array_set_value(info, root, index, value, new_root);
  704. __dm_unbless_for_disk(value);
  705. return r;
  706. }
  707. EXPORT_SYMBOL_GPL(dm_array_set_value);
  708. struct walk_info {
  709. struct dm_array_info *info;
  710. int (*fn)(void *context, uint64_t key, void *leaf);
  711. void *context;
  712. };
  713. static int walk_ablock(void *context, uint64_t *keys, void *leaf)
  714. {
  715. struct walk_info *wi = context;
  716. int r;
  717. unsigned int i;
  718. __le64 block_le;
  719. unsigned int nr_entries, max_entries;
  720. struct dm_block *block;
  721. struct array_block *ab;
  722. memcpy(&block_le, leaf, sizeof(block_le));
  723. r = get_ablock(wi->info, le64_to_cpu(block_le), &block, &ab);
  724. if (r)
  725. return r;
  726. max_entries = le32_to_cpu(ab->max_entries);
  727. nr_entries = le32_to_cpu(ab->nr_entries);
  728. for (i = 0; i < nr_entries; i++) {
  729. r = wi->fn(wi->context, keys[0] * max_entries + i,
  730. element_at(wi->info, ab, i));
  731. if (r)
  732. break;
  733. }
  734. unlock_ablock(wi->info, block);
  735. return r;
  736. }
  737. int dm_array_walk(struct dm_array_info *info, dm_block_t root,
  738. int (*fn)(void *, uint64_t key, void *leaf),
  739. void *context)
  740. {
  741. struct walk_info wi;
  742. wi.info = info;
  743. wi.fn = fn;
  744. wi.context = context;
  745. return dm_btree_walk(&info->btree_info, root, walk_ablock, &wi);
  746. }
  747. EXPORT_SYMBOL_GPL(dm_array_walk);
  748. /*----------------------------------------------------------------*/
  749. static int load_ablock(struct dm_array_cursor *c)
  750. {
  751. int r;
  752. __le64 value_le;
  753. uint64_t key;
  754. if (c->block)
  755. unlock_ablock(c->info, c->block);
  756. c->index = 0;
  757. r = dm_btree_cursor_get_value(&c->cursor, &key, &value_le);
  758. if (r) {
  759. DMERR("dm_btree_cursor_get_value failed");
  760. goto out;
  761. } else {
  762. r = get_ablock(c->info, le64_to_cpu(value_le), &c->block, &c->ab);
  763. if (r) {
  764. DMERR("get_ablock failed");
  765. goto out;
  766. }
  767. }
  768. return 0;
  769. out:
  770. dm_btree_cursor_end(&c->cursor);
  771. c->block = NULL;
  772. c->ab = NULL;
  773. return r;
  774. }
  775. int dm_array_cursor_begin(struct dm_array_info *info, dm_block_t root,
  776. struct dm_array_cursor *c)
  777. {
  778. int r;
  779. memset(c, 0, sizeof(*c));
  780. c->info = info;
  781. r = dm_btree_cursor_begin(&info->btree_info, root, true, &c->cursor);
  782. if (r) {
  783. DMERR("couldn't create btree cursor");
  784. return r;
  785. }
  786. return load_ablock(c);
  787. }
  788. EXPORT_SYMBOL_GPL(dm_array_cursor_begin);
  789. void dm_array_cursor_end(struct dm_array_cursor *c)
  790. {
  791. if (c->block)
  792. unlock_ablock(c->info, c->block);
  793. dm_btree_cursor_end(&c->cursor);
  794. }
  795. EXPORT_SYMBOL_GPL(dm_array_cursor_end);
  796. int dm_array_cursor_next(struct dm_array_cursor *c)
  797. {
  798. int r;
  799. if (!c->block)
  800. return -ENODATA;
  801. c->index++;
  802. if (c->index >= le32_to_cpu(c->ab->nr_entries)) {
  803. r = dm_btree_cursor_next(&c->cursor);
  804. if (r)
  805. return r;
  806. r = load_ablock(c);
  807. if (r)
  808. return r;
  809. }
  810. return 0;
  811. }
  812. EXPORT_SYMBOL_GPL(dm_array_cursor_next);
  813. int dm_array_cursor_skip(struct dm_array_cursor *c, uint32_t count)
  814. {
  815. int r;
  816. do {
  817. uint32_t remaining = le32_to_cpu(c->ab->nr_entries) - c->index;
  818. if (count < remaining) {
  819. c->index += count;
  820. return 0;
  821. }
  822. count -= remaining;
  823. c->index += (remaining - 1);
  824. r = dm_array_cursor_next(c);
  825. } while (!r);
  826. return r;
  827. }
  828. EXPORT_SYMBOL_GPL(dm_array_cursor_skip);
  829. void dm_array_cursor_get_value(struct dm_array_cursor *c, void **value_le)
  830. {
  831. *value_le = element_at(c->info, c->ab, c->index);
  832. }
  833. EXPORT_SYMBOL_GPL(dm_array_cursor_get_value);
  834. /*----------------------------------------------------------------*/