blob: 51ca9edef444ee9354337ce371860172311eb382 [file] [log] [blame]
Joe Thornber3241b1d2011-10-31 20:19:11 +00001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-space-map.h"
8#include "dm-space-map-common.h"
9#include "dm-space-map-metadata.h"
10
11#include <linux/list.h>
12#include <linux/slab.h>
13#include <linux/device-mapper.h>
14
15#define DM_MSG_PREFIX "space map metadata"
16
17/*----------------------------------------------------------------*/
18
19/*
20 * Space map interface.
21 *
22 * The low level disk format is written using the standard btree and
23 * transaction manager. This means that performing disk operations may
24 * cause us to recurse into the space map in order to allocate new blocks.
25 * For this reason we have a pool of pre-allocated blocks large enough to
26 * service any metadata_ll_disk operation.
27 */
28
29/*
30 * FIXME: we should calculate this based on the size of the device.
31 * Only the metadata space map needs this functionality.
32 */
33#define MAX_RECURSIVE_ALLOCATIONS 1024
34
35enum block_op_type {
36 BOP_INC,
37 BOP_DEC
38};
39
40struct block_op {
41 enum block_op_type type;
42 dm_block_t block;
43};
44
45struct sm_metadata {
46 struct dm_space_map sm;
47
48 struct ll_disk ll;
49 struct ll_disk old_ll;
50
51 dm_block_t begin;
52
53 unsigned recursion_count;
54 unsigned allocated_this_transaction;
55 unsigned nr_uncommitted;
56 struct block_op uncommitted[MAX_RECURSIVE_ALLOCATIONS];
57};
58
59static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
60{
61 struct block_op *op;
62
63 if (smm->nr_uncommitted == MAX_RECURSIVE_ALLOCATIONS) {
64 DMERR("too many recursive allocations");
65 return -ENOMEM;
66 }
67
68 op = smm->uncommitted + smm->nr_uncommitted++;
69 op->type = type;
70 op->block = b;
71
72 return 0;
73}
74
75static int commit_bop(struct sm_metadata *smm, struct block_op *op)
76{
77 int r = 0;
78 enum allocation_event ev;
79
80 switch (op->type) {
81 case BOP_INC:
82 r = sm_ll_inc(&smm->ll, op->block, &ev);
83 break;
84
85 case BOP_DEC:
86 r = sm_ll_dec(&smm->ll, op->block, &ev);
87 break;
88 }
89
90 return r;
91}
92
93static void in(struct sm_metadata *smm)
94{
95 smm->recursion_count++;
96}
97
98static int out(struct sm_metadata *smm)
99{
100 int r = 0;
101
102 /*
103 * If we're not recursing then very bad things are happening.
104 */
105 if (!smm->recursion_count) {
106 DMERR("lost track of recursion depth");
107 return -ENOMEM;
108 }
109
110 if (smm->recursion_count == 1 && smm->nr_uncommitted) {
111 while (smm->nr_uncommitted && !r) {
112 smm->nr_uncommitted--;
113 r = commit_bop(smm, smm->uncommitted +
114 smm->nr_uncommitted);
115 if (r)
116 break;
117 }
118 }
119
120 smm->recursion_count--;
121
122 return r;
123}
124
125/*
126 * When using the out() function above, we often want to combine an error
127 * code for the operation run in the recursive context with that from
128 * out().
129 */
130static int combine_errors(int r1, int r2)
131{
132 return r1 ? r1 : r2;
133}
134
135static int recursing(struct sm_metadata *smm)
136{
137 return smm->recursion_count;
138}
139
140static void sm_metadata_destroy(struct dm_space_map *sm)
141{
142 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
143
144 kfree(smm);
145}
146
Joe Thornber3241b1d2011-10-31 20:19:11 +0000147static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
148{
149 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
150
151 *count = smm->ll.nr_blocks;
152
153 return 0;
154}
155
156static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
157{
158 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
159
160 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
161 smm->allocated_this_transaction;
162
163 return 0;
164}
165
166static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
167 uint32_t *result)
168{
169 int r, i;
170 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
171 unsigned adjustment = 0;
172
173 /*
174 * We may have some uncommitted adjustments to add. This list
175 * should always be really short.
176 */
177 for (i = 0; i < smm->nr_uncommitted; i++) {
178 struct block_op *op = smm->uncommitted + i;
179
180 if (op->block != b)
181 continue;
182
183 switch (op->type) {
184 case BOP_INC:
185 adjustment++;
186 break;
187
188 case BOP_DEC:
189 adjustment--;
190 break;
191 }
192 }
193
194 r = sm_ll_lookup(&smm->ll, b, result);
195 if (r)
196 return r;
197
198 *result += adjustment;
199
200 return 0;
201}
202
203static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
204 dm_block_t b, int *result)
205{
206 int r, i, adjustment = 0;
207 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
208 uint32_t rc;
209
210 /*
211 * We may have some uncommitted adjustments to add. This list
212 * should always be really short.
213 */
214 for (i = 0; i < smm->nr_uncommitted; i++) {
215 struct block_op *op = smm->uncommitted + i;
216
217 if (op->block != b)
218 continue;
219
220 switch (op->type) {
221 case BOP_INC:
222 adjustment++;
223 break;
224
225 case BOP_DEC:
226 adjustment--;
227 break;
228 }
229 }
230
231 if (adjustment > 1) {
232 *result = 1;
233 return 0;
234 }
235
236 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
237 if (r)
238 return r;
239
240 if (rc == 3)
241 /*
242 * We err on the side of caution, and always return true.
243 */
244 *result = 1;
245 else
246 *result = rc + adjustment > 1;
247
248 return 0;
249}
250
251static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
252 uint32_t count)
253{
254 int r, r2;
255 enum allocation_event ev;
256 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
257
258 if (smm->recursion_count) {
259 DMERR("cannot recurse set_count()");
260 return -EINVAL;
261 }
262
263 in(smm);
264 r = sm_ll_insert(&smm->ll, b, count, &ev);
265 r2 = out(smm);
266
267 return combine_errors(r, r2);
268}
269
270static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
271{
272 int r, r2 = 0;
273 enum allocation_event ev;
274 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
275
276 if (recursing(smm))
277 r = add_bop(smm, BOP_INC, b);
278 else {
279 in(smm);
280 r = sm_ll_inc(&smm->ll, b, &ev);
281 r2 = out(smm);
282 }
283
284 return combine_errors(r, r2);
285}
286
287static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
288{
289 int r, r2 = 0;
290 enum allocation_event ev;
291 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
292
293 if (recursing(smm))
294 r = add_bop(smm, BOP_DEC, b);
295 else {
296 in(smm);
297 r = sm_ll_dec(&smm->ll, b, &ev);
298 r2 = out(smm);
299 }
300
301 return combine_errors(r, r2);
302}
303
304static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
305{
306 int r, r2 = 0;
307 enum allocation_event ev;
308 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
309
310 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
311 if (r)
312 return r;
313
314 smm->begin = *b + 1;
315
316 if (recursing(smm))
317 r = add_bop(smm, BOP_INC, *b);
318 else {
319 in(smm);
320 r = sm_ll_inc(&smm->ll, *b, &ev);
321 r2 = out(smm);
322 }
323
324 if (!r)
325 smm->allocated_this_transaction++;
326
327 return combine_errors(r, r2);
328}
329
330static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
331{
332 int r = sm_metadata_new_block_(sm, b);
333 if (r)
Joe Thornber79601232012-12-21 20:23:36 +0000334 DMERR("unable to allocate new metadata block");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000335 return r;
336}
337
338static int sm_metadata_commit(struct dm_space_map *sm)
339{
340 int r;
341 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
342
343 r = sm_ll_commit(&smm->ll);
344 if (r)
345 return r;
346
347 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
348 smm->begin = 0;
349 smm->allocated_this_transaction = 0;
350
351 return 0;
352}
353
354static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
355{
356 *result = sizeof(struct disk_sm_root);
357
358 return 0;
359}
360
361static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
362{
363 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
364 struct disk_sm_root root_le;
365
366 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
367 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
368 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
369 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
370
371 if (max < sizeof(root_le))
372 return -ENOSPC;
373
374 memcpy(where_le, &root_le, sizeof(root_le));
375
376 return 0;
377}
378
Joe Thornber1921c562013-05-10 14:37:19 +0100379static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
380
Joe Thornber3241b1d2011-10-31 20:19:11 +0000381static struct dm_space_map ops = {
382 .destroy = sm_metadata_destroy,
383 .extend = sm_metadata_extend,
384 .get_nr_blocks = sm_metadata_get_nr_blocks,
385 .get_nr_free = sm_metadata_get_nr_free,
386 .get_count = sm_metadata_get_count,
387 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
388 .set_count = sm_metadata_set_count,
389 .inc_block = sm_metadata_inc_block,
390 .dec_block = sm_metadata_dec_block,
391 .new_block = sm_metadata_new_block,
392 .commit = sm_metadata_commit,
393 .root_size = sm_metadata_root_size,
394 .copy_root = sm_metadata_copy_root
395};
396
397/*----------------------------------------------------------------*/
398
399/*
400 * When a new space map is created that manages its own space. We use
401 * this tiny bootstrap allocator.
402 */
403static void sm_bootstrap_destroy(struct dm_space_map *sm)
404{
405}
406
407static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
408{
Joe Thornber88a488f2013-05-10 14:37:17 +0100409 DMERR("bootstrap doesn't support extend");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000410
411 return -EINVAL;
412}
413
414static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
415{
416 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
417
418 return smm->ll.nr_blocks;
419}
420
421static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
422{
423 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
424
425 *count = smm->ll.nr_blocks - smm->begin;
426
427 return 0;
428}
429
430static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
431 uint32_t *result)
432{
433 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
434
435 return b < smm->begin ? 1 : 0;
436}
437
438static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
439 dm_block_t b, int *result)
440{
441 *result = 0;
442
443 return 0;
444}
445
446static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
447 uint32_t count)
448{
Joe Thornber88a488f2013-05-10 14:37:17 +0100449 DMERR("bootstrap doesn't support set_count");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000450
451 return -EINVAL;
452}
453
454static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
455{
456 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
457
458 /*
459 * We know the entire device is unused.
460 */
461 if (smm->begin == smm->ll.nr_blocks)
462 return -ENOSPC;
463
464 *b = smm->begin++;
465
466 return 0;
467}
468
469static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
470{
471 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
472
473 return add_bop(smm, BOP_INC, b);
474}
475
476static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
477{
478 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
479
480 return add_bop(smm, BOP_DEC, b);
481}
482
483static int sm_bootstrap_commit(struct dm_space_map *sm)
484{
485 return 0;
486}
487
488static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
489{
Joe Thornber88a488f2013-05-10 14:37:17 +0100490 DMERR("bootstrap doesn't support root_size");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000491
492 return -EINVAL;
493}
494
495static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
496 size_t max)
497{
Joe Thornber88a488f2013-05-10 14:37:17 +0100498 DMERR("bootstrap doesn't support copy_root");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000499
500 return -EINVAL;
501}
502
503static struct dm_space_map bootstrap_ops = {
504 .destroy = sm_bootstrap_destroy,
505 .extend = sm_bootstrap_extend,
506 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
507 .get_nr_free = sm_bootstrap_get_nr_free,
508 .get_count = sm_bootstrap_get_count,
509 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
510 .set_count = sm_bootstrap_set_count,
511 .inc_block = sm_bootstrap_inc_block,
512 .dec_block = sm_bootstrap_dec_block,
513 .new_block = sm_bootstrap_new_block,
514 .commit = sm_bootstrap_commit,
515 .root_size = sm_bootstrap_root_size,
516 .copy_root = sm_bootstrap_copy_root
517};
518
519/*----------------------------------------------------------------*/
520
Joe Thornber1921c562013-05-10 14:37:19 +0100521static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
522{
523 int r, i;
524 enum allocation_event ev;
525 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
526 dm_block_t old_len = smm->ll.nr_blocks;
527
528 /*
529 * Flick into a mode where all blocks get allocated in the new area.
530 */
531 smm->begin = old_len;
532 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
533
534 /*
535 * Extend.
536 */
537 r = sm_ll_extend(&smm->ll, extra_blocks);
538
539 /*
540 * Switch back to normal behaviour.
541 */
542 memcpy(&smm->sm, &ops, sizeof(smm->sm));
543 for (i = old_len; !r && i < smm->begin; i++)
544 r = sm_ll_inc(&smm->ll, i, &ev);
545
546 return r;
547}
548
549/*----------------------------------------------------------------*/
550
Joe Thornber3241b1d2011-10-31 20:19:11 +0000551struct dm_space_map *dm_sm_metadata_init(void)
552{
553 struct sm_metadata *smm;
554
555 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
556 if (!smm)
557 return ERR_PTR(-ENOMEM);
558
559 memcpy(&smm->sm, &ops, sizeof(smm->sm));
560
561 return &smm->sm;
562}
563
564int dm_sm_metadata_create(struct dm_space_map *sm,
565 struct dm_transaction_manager *tm,
566 dm_block_t nr_blocks,
567 dm_block_t superblock)
568{
569 int r;
570 dm_block_t i;
571 enum allocation_event ev;
572 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
573
574 smm->begin = superblock + 1;
575 smm->recursion_count = 0;
576 smm->allocated_this_transaction = 0;
577 smm->nr_uncommitted = 0;
578
579 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
580
581 r = sm_ll_new_metadata(&smm->ll, tm);
582 if (r)
583 return r;
584
585 r = sm_ll_extend(&smm->ll, nr_blocks);
586 if (r)
587 return r;
588
589 memcpy(&smm->sm, &ops, sizeof(smm->sm));
590
591 /*
592 * Now we need to update the newly created data structures with the
593 * allocated blocks that they were built from.
594 */
595 for (i = superblock; !r && i < smm->begin; i++)
596 r = sm_ll_inc(&smm->ll, i, &ev);
597
598 if (r)
599 return r;
600
601 return sm_metadata_commit(sm);
602}
603
604int dm_sm_metadata_open(struct dm_space_map *sm,
605 struct dm_transaction_manager *tm,
606 void *root_le, size_t len)
607{
608 int r;
609 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
610
611 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
612 if (r)
613 return r;
614
615 smm->begin = 0;
616 smm->recursion_count = 0;
617 smm->allocated_this_transaction = 0;
618 smm->nr_uncommitted = 0;
619
620 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
621 return 0;
622}