Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2011 Red Hat, Inc.
  4 *
  5 * This file is released under the GPL.
  6 */
  7
  8#include "dm-space-map-common.h"
  9#include "dm-space-map-disk.h"
 10#include "dm-space-map.h"
 11#include "dm-transaction-manager.h"
 12
 13#include <linux/list.h>
 14#include <linux/slab.h>
 15#include <linux/export.h>
 16#include <linux/device-mapper.h>
 17
 18#define DM_MSG_PREFIX "space map disk"
 19
 20/*----------------------------------------------------------------*/
 21
 22/*
 23 * Space map interface.
 24 */
 25struct sm_disk {
 26	struct dm_space_map sm;
 27
 28	struct ll_disk ll;
 29	struct ll_disk old_ll;
 30
 31	dm_block_t begin;
 32	dm_block_t nr_allocated_this_transaction;
 33};
 34
 35static void sm_disk_destroy(struct dm_space_map *sm)
 36{
 37	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 38
 39	kfree(smd);
 40}
 41
 42static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
 43{
 44	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 45
 46	return sm_ll_extend(&smd->ll, extra_blocks);
 47}
 48
 49static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
 50{
 51	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 52
 53	*count = smd->old_ll.nr_blocks;
 54
 55	return 0;
 56}
 57
 58static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
 59{
 60	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 61
 62	*count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
 63
 64	return 0;
 65}
 66
 67static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
 68			     uint32_t *result)
 69{
 70	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 71
 72	return sm_ll_lookup(&smd->ll, b, result);
 73}
 74
 75static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
 76					  int *result)
 77{
 78	int r;
 79	uint32_t count;
 80
 81	r = sm_disk_get_count(sm, b, &count);
 82	if (r)
 83		return r;
 84
 85	*result = count > 1;
 86
 87	return 0;
 88}
 89
 90static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
 91			     uint32_t count)
 92{
 93	int r;
 94	int32_t nr_allocations;
 
 95	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 96
 97	r = sm_ll_insert(&smd->ll, b, count, &nr_allocations);
 98	if (!r)
 99		smd->nr_allocated_this_transaction += nr_allocations;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101	return r;
102}
103
104static int sm_disk_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
105{
106	int r;
107	int32_t nr_allocations;
108	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
109
110	r = sm_ll_inc(&smd->ll, b, e, &nr_allocations);
111	if (!r)
112		smd->nr_allocated_this_transaction += nr_allocations;
 
 
 
 
113
114	return r;
115}
116
117static int sm_disk_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
118{
119	int r;
120	int32_t nr_allocations;
121	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
122
123	r = sm_ll_dec(&smd->ll, b, e, &nr_allocations);
124	if (!r)
125		smd->nr_allocated_this_transaction += nr_allocations;
126
127	return r;
128}
129
130static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
131{
132	int r;
133	int32_t nr_allocations;
134	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
135
136	/*
137	 * Any block we allocate has to be free in both the old and current ll.
138	 */
139	r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
140	if (r == -ENOSPC)
141		/*
142		 * There's no free block between smd->begin and the end of the metadata device.
143		 * We search before smd->begin in case something has been freed.
144		 */
145		r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
146
147	if (r)
148		return r;
149
150	smd->begin = *b + 1;
151	r = sm_ll_inc(&smd->ll, *b, *b + 1, &nr_allocations);
152	if (!r)
153		smd->nr_allocated_this_transaction += nr_allocations;
 
 
154
155	return r;
156}
157
158static int sm_disk_commit(struct dm_space_map *sm)
159{
160	int r;
 
161	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
162
 
 
 
 
163	r = sm_ll_commit(&smd->ll);
164	if (r)
165		return r;
166
167	memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
 
168	smd->nr_allocated_this_transaction = 0;
169
 
 
 
 
170	return 0;
171}
172
173static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
174{
175	*result = sizeof(struct disk_sm_root);
176
177	return 0;
178}
179
180static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
181{
182	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
183	struct disk_sm_root root_le;
184
185	root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
186	root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
187	root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
188	root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
189
190	if (max < sizeof(root_le))
191		return -ENOSPC;
192
193	memcpy(where_le, &root_le, sizeof(root_le));
194
195	return 0;
196}
197
198/*----------------------------------------------------------------*/
199
200static struct dm_space_map ops = {
201	.destroy = sm_disk_destroy,
202	.extend = sm_disk_extend,
203	.get_nr_blocks = sm_disk_get_nr_blocks,
204	.get_nr_free = sm_disk_get_nr_free,
205	.get_count = sm_disk_get_count,
206	.count_is_more_than_one = sm_disk_count_is_more_than_one,
207	.set_count = sm_disk_set_count,
208	.inc_blocks = sm_disk_inc_blocks,
209	.dec_blocks = sm_disk_dec_blocks,
210	.new_block = sm_disk_new_block,
211	.commit = sm_disk_commit,
212	.root_size = sm_disk_root_size,
213	.copy_root = sm_disk_copy_root,
214	.register_threshold_callback = NULL
215};
216
217struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
218				       dm_block_t nr_blocks)
219{
220	int r;
221	struct sm_disk *smd;
222
223	smd = kmalloc(sizeof(*smd), GFP_KERNEL);
224	if (!smd)
225		return ERR_PTR(-ENOMEM);
226
227	smd->begin = 0;
228	smd->nr_allocated_this_transaction = 0;
229	memcpy(&smd->sm, &ops, sizeof(smd->sm));
230
231	r = sm_ll_new_disk(&smd->ll, tm);
232	if (r)
233		goto bad;
234
235	r = sm_ll_extend(&smd->ll, nr_blocks);
236	if (r)
237		goto bad;
238
239	r = sm_disk_commit(&smd->sm);
240	if (r)
241		goto bad;
242
243	return &smd->sm;
244
245bad:
246	kfree(smd);
247	return ERR_PTR(r);
248}
249EXPORT_SYMBOL_GPL(dm_sm_disk_create);
250
251struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
252				     void *root_le, size_t len)
253{
254	int r;
255	struct sm_disk *smd;
256
257	smd = kmalloc(sizeof(*smd), GFP_KERNEL);
258	if (!smd)
259		return ERR_PTR(-ENOMEM);
260
261	smd->begin = 0;
262	smd->nr_allocated_this_transaction = 0;
263	memcpy(&smd->sm, &ops, sizeof(smd->sm));
264
265	r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
266	if (r)
267		goto bad;
268
269	r = sm_disk_commit(&smd->sm);
270	if (r)
271		goto bad;
272
273	return &smd->sm;
274
275bad:
276	kfree(smd);
277	return ERR_PTR(r);
278}
279EXPORT_SYMBOL_GPL(dm_sm_disk_open);
280
281/*----------------------------------------------------------------*/
v3.15
 
  1/*
  2 * Copyright (C) 2011 Red Hat, Inc.
  3 *
  4 * This file is released under the GPL.
  5 */
  6
  7#include "dm-space-map-common.h"
  8#include "dm-space-map-disk.h"
  9#include "dm-space-map.h"
 10#include "dm-transaction-manager.h"
 11
 12#include <linux/list.h>
 13#include <linux/slab.h>
 14#include <linux/export.h>
 15#include <linux/device-mapper.h>
 16
 17#define DM_MSG_PREFIX "space map disk"
 18
 19/*----------------------------------------------------------------*/
 20
 21/*
 22 * Space map interface.
 23 */
 24struct sm_disk {
 25	struct dm_space_map sm;
 26
 27	struct ll_disk ll;
 28	struct ll_disk old_ll;
 29
 30	dm_block_t begin;
 31	dm_block_t nr_allocated_this_transaction;
 32};
 33
 34static void sm_disk_destroy(struct dm_space_map *sm)
 35{
 36	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 37
 38	kfree(smd);
 39}
 40
 41static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
 42{
 43	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 44
 45	return sm_ll_extend(&smd->ll, extra_blocks);
 46}
 47
 48static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
 49{
 50	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 
 51	*count = smd->old_ll.nr_blocks;
 52
 53	return 0;
 54}
 55
 56static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
 57{
 58	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 
 59	*count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
 60
 61	return 0;
 62}
 63
 64static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
 65			     uint32_t *result)
 66{
 67	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 
 68	return sm_ll_lookup(&smd->ll, b, result);
 69}
 70
 71static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
 72					  int *result)
 73{
 74	int r;
 75	uint32_t count;
 76
 77	r = sm_disk_get_count(sm, b, &count);
 78	if (r)
 79		return r;
 80
 81	return count > 1;
 
 
 82}
 83
 84static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
 85			     uint32_t count)
 86{
 87	int r;
 88	uint32_t old_count;
 89	enum allocation_event ev;
 90	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
 91
 92	r = sm_ll_insert(&smd->ll, b, count, &ev);
 93	if (!r) {
 94		switch (ev) {
 95		case SM_NONE:
 96			break;
 97
 98		case SM_ALLOC:
 99			/*
100			 * This _must_ be free in the prior transaction
101			 * otherwise we've lost atomicity.
102			 */
103			smd->nr_allocated_this_transaction++;
104			break;
105
106		case SM_FREE:
107			/*
108			 * It's only free if it's also free in the last
109			 * transaction.
110			 */
111			r = sm_ll_lookup(&smd->old_ll, b, &old_count);
112			if (r)
113				return r;
114
115			if (!old_count)
116				smd->nr_allocated_this_transaction--;
117			break;
118		}
119	}
120
121	return r;
122}
123
124static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
125{
126	int r;
127	enum allocation_event ev;
128	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
129
130	r = sm_ll_inc(&smd->ll, b, &ev);
131	if (!r && (ev == SM_ALLOC))
132		/*
133		 * This _must_ be free in the prior transaction
134		 * otherwise we've lost atomicity.
135		 */
136		smd->nr_allocated_this_transaction++;
137
138	return r;
139}
140
141static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
142{
143	enum allocation_event ev;
 
144	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
145
146	return sm_ll_dec(&smd->ll, b, &ev);
 
 
 
 
147}
148
149static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
150{
151	int r;
152	enum allocation_event ev;
153	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
154
155	/* FIXME: we should loop round a couple of times */
156	r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
 
 
 
 
 
 
 
 
 
157	if (r)
158		return r;
159
160	smd->begin = *b + 1;
161	r = sm_ll_inc(&smd->ll, *b, &ev);
162	if (!r) {
163		BUG_ON(ev != SM_ALLOC);
164		smd->nr_allocated_this_transaction++;
165	}
166
167	return r;
168}
169
170static int sm_disk_commit(struct dm_space_map *sm)
171{
172	int r;
173	dm_block_t nr_free;
174	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
175
176	r = sm_disk_get_nr_free(sm, &nr_free);
177	if (r)
178		return r;
179
180	r = sm_ll_commit(&smd->ll);
181	if (r)
182		return r;
183
184	memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
185	smd->begin = 0;
186	smd->nr_allocated_this_transaction = 0;
187
188	r = sm_disk_get_nr_free(sm, &nr_free);
189	if (r)
190		return r;
191
192	return 0;
193}
194
195static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
196{
197	*result = sizeof(struct disk_sm_root);
198
199	return 0;
200}
201
202static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
203{
204	struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
205	struct disk_sm_root root_le;
206
207	root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
208	root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
209	root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
210	root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
211
212	if (max < sizeof(root_le))
213		return -ENOSPC;
214
215	memcpy(where_le, &root_le, sizeof(root_le));
216
217	return 0;
218}
219
220/*----------------------------------------------------------------*/
221
222static struct dm_space_map ops = {
223	.destroy = sm_disk_destroy,
224	.extend = sm_disk_extend,
225	.get_nr_blocks = sm_disk_get_nr_blocks,
226	.get_nr_free = sm_disk_get_nr_free,
227	.get_count = sm_disk_get_count,
228	.count_is_more_than_one = sm_disk_count_is_more_than_one,
229	.set_count = sm_disk_set_count,
230	.inc_block = sm_disk_inc_block,
231	.dec_block = sm_disk_dec_block,
232	.new_block = sm_disk_new_block,
233	.commit = sm_disk_commit,
234	.root_size = sm_disk_root_size,
235	.copy_root = sm_disk_copy_root,
236	.register_threshold_callback = NULL
237};
238
239struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
240				       dm_block_t nr_blocks)
241{
242	int r;
243	struct sm_disk *smd;
244
245	smd = kmalloc(sizeof(*smd), GFP_KERNEL);
246	if (!smd)
247		return ERR_PTR(-ENOMEM);
248
249	smd->begin = 0;
250	smd->nr_allocated_this_transaction = 0;
251	memcpy(&smd->sm, &ops, sizeof(smd->sm));
252
253	r = sm_ll_new_disk(&smd->ll, tm);
254	if (r)
255		goto bad;
256
257	r = sm_ll_extend(&smd->ll, nr_blocks);
258	if (r)
259		goto bad;
260
261	r = sm_disk_commit(&smd->sm);
262	if (r)
263		goto bad;
264
265	return &smd->sm;
266
267bad:
268	kfree(smd);
269	return ERR_PTR(r);
270}
271EXPORT_SYMBOL_GPL(dm_sm_disk_create);
272
273struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
274				     void *root_le, size_t len)
275{
276	int r;
277	struct sm_disk *smd;
278
279	smd = kmalloc(sizeof(*smd), GFP_KERNEL);
280	if (!smd)
281		return ERR_PTR(-ENOMEM);
282
283	smd->begin = 0;
284	smd->nr_allocated_this_transaction = 0;
285	memcpy(&smd->sm, &ops, sizeof(smd->sm));
286
287	r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
288	if (r)
289		goto bad;
290
291	r = sm_disk_commit(&smd->sm);
292	if (r)
293		goto bad;
294
295	return &smd->sm;
296
297bad:
298	kfree(smd);
299	return ERR_PTR(r);
300}
301EXPORT_SYMBOL_GPL(dm_sm_disk_open);
302
303/*----------------------------------------------------------------*/