xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/brel_ma.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * brel_ma.c
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * TODO: rewrite to not use a direct array!!!  (Fortunately this
7*6a54128fSAndroid Build Coastguard Worker  * module isn't really used yet.)
8*6a54128fSAndroid Build Coastguard Worker  *
9*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
10*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
11*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
12*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
13*6a54128fSAndroid Build Coastguard Worker  */
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker #include "config.h"
16*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
17*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
18*6a54128fSAndroid Build Coastguard Worker #include <string.h>
19*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
20*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
21*6a54128fSAndroid Build Coastguard Worker #endif
22*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
23*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
27*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
28*6a54128fSAndroid Build Coastguard Worker #include "brel.h"
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_put(ext2_brel brel, blk64_t old,
31*6a54128fSAndroid Build Coastguard Worker 			struct ext2_block_relocate_entry *ent);
32*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_get(ext2_brel brel, blk64_t old,
33*6a54128fSAndroid Build Coastguard Worker 			struct ext2_block_relocate_entry *ent);
34*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_start_iter(ext2_brel brel);
35*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_next(ext2_brel brel, blk64_t *old,
36*6a54128fSAndroid Build Coastguard Worker 			 struct ext2_block_relocate_entry *ent);
37*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_move(ext2_brel brel, blk64_t old, blk64_t new);
38*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_delete(ext2_brel brel, blk64_t old);
39*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_free(ext2_brel brel);
40*6a54128fSAndroid Build Coastguard Worker 
41*6a54128fSAndroid Build Coastguard Worker struct brel_ma {
42*6a54128fSAndroid Build Coastguard Worker 	__u32 magic;
43*6a54128fSAndroid Build Coastguard Worker 	blk64_t max_block;
44*6a54128fSAndroid Build Coastguard Worker 	struct ext2_block_relocate_entry *entries;
45*6a54128fSAndroid Build Coastguard Worker };
46*6a54128fSAndroid Build Coastguard Worker 
ext2fs_brel_memarray_create(char * name,blk64_t max_block,ext2_brel * new_brel)47*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_brel_memarray_create(char *name, blk64_t max_block,
48*6a54128fSAndroid Build Coastguard Worker 				      ext2_brel *new_brel)
49*6a54128fSAndroid Build Coastguard Worker {
50*6a54128fSAndroid Build Coastguard Worker 	ext2_brel		brel = 0;
51*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
52*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma = 0;
53*6a54128fSAndroid Build Coastguard Worker 	size_t		size;
54*6a54128fSAndroid Build Coastguard Worker 
55*6a54128fSAndroid Build Coastguard Worker 	*new_brel = 0;
56*6a54128fSAndroid Build Coastguard Worker 
57*6a54128fSAndroid Build Coastguard Worker 	/*
58*6a54128fSAndroid Build Coastguard Worker 	 * Allocate memory structures
59*6a54128fSAndroid Build Coastguard Worker 	 */
60*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct ext2_block_relocation_table),
61*6a54128fSAndroid Build Coastguard Worker 				&brel);
62*6a54128fSAndroid Build Coastguard Worker 	if (retval)
63*6a54128fSAndroid Build Coastguard Worker 		goto errout;
64*6a54128fSAndroid Build Coastguard Worker 	memset(brel, 0, sizeof(struct ext2_block_relocation_table));
65*6a54128fSAndroid Build Coastguard Worker 
66*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(strlen(name)+1, &brel->name);
67*6a54128fSAndroid Build Coastguard Worker 	if (retval)
68*6a54128fSAndroid Build Coastguard Worker 		goto errout;
69*6a54128fSAndroid Build Coastguard Worker 	strcpy(brel->name, name);
70*6a54128fSAndroid Build Coastguard Worker 
71*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct brel_ma), &ma);
72*6a54128fSAndroid Build Coastguard Worker 	if (retval)
73*6a54128fSAndroid Build Coastguard Worker 		goto errout;
74*6a54128fSAndroid Build Coastguard Worker 	memset(ma, 0, sizeof(struct brel_ma));
75*6a54128fSAndroid Build Coastguard Worker 	brel->priv_data = ma;
76*6a54128fSAndroid Build Coastguard Worker 
77*6a54128fSAndroid Build Coastguard Worker 	size = (size_t) (sizeof(struct ext2_block_relocate_entry) *
78*6a54128fSAndroid Build Coastguard Worker 			 (max_block+1));
79*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_array(max_block+1,
80*6a54128fSAndroid Build Coastguard Worker 		sizeof(struct ext2_block_relocate_entry), &ma->entries);
81*6a54128fSAndroid Build Coastguard Worker 	if (retval)
82*6a54128fSAndroid Build Coastguard Worker 		goto errout;
83*6a54128fSAndroid Build Coastguard Worker 	memset(ma->entries, 0, size);
84*6a54128fSAndroid Build Coastguard Worker 	ma->max_block = max_block;
85*6a54128fSAndroid Build Coastguard Worker 
86*6a54128fSAndroid Build Coastguard Worker 	/*
87*6a54128fSAndroid Build Coastguard Worker 	 * Fill in the brel data structure
88*6a54128fSAndroid Build Coastguard Worker 	 */
89*6a54128fSAndroid Build Coastguard Worker 	brel->put = bma_put;
90*6a54128fSAndroid Build Coastguard Worker 	brel->get = bma_get;
91*6a54128fSAndroid Build Coastguard Worker 	brel->start_iter = bma_start_iter;
92*6a54128fSAndroid Build Coastguard Worker 	brel->next = bma_next;
93*6a54128fSAndroid Build Coastguard Worker 	brel->move = bma_move;
94*6a54128fSAndroid Build Coastguard Worker 	brel->delete = bma_delete;
95*6a54128fSAndroid Build Coastguard Worker 	brel->free = bma_free;
96*6a54128fSAndroid Build Coastguard Worker 
97*6a54128fSAndroid Build Coastguard Worker 	*new_brel = brel;
98*6a54128fSAndroid Build Coastguard Worker 	return 0;
99*6a54128fSAndroid Build Coastguard Worker 
100*6a54128fSAndroid Build Coastguard Worker errout:
101*6a54128fSAndroid Build Coastguard Worker 	bma_free(brel);
102*6a54128fSAndroid Build Coastguard Worker 	return retval;
103*6a54128fSAndroid Build Coastguard Worker }
104*6a54128fSAndroid Build Coastguard Worker 
bma_put(ext2_brel brel,blk64_t old,struct ext2_block_relocate_entry * ent)105*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_put(ext2_brel brel, blk64_t old,
106*6a54128fSAndroid Build Coastguard Worker 			struct ext2_block_relocate_entry *ent)
107*6a54128fSAndroid Build Coastguard Worker {
108*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
109*6a54128fSAndroid Build Coastguard Worker 
110*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
111*6a54128fSAndroid Build Coastguard Worker 	if (old > ma->max_block)
112*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
113*6a54128fSAndroid Build Coastguard Worker 	ma->entries[(unsigned)old] = *ent;
114*6a54128fSAndroid Build Coastguard Worker 	return 0;
115*6a54128fSAndroid Build Coastguard Worker }
116*6a54128fSAndroid Build Coastguard Worker 
bma_get(ext2_brel brel,blk64_t old,struct ext2_block_relocate_entry * ent)117*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_get(ext2_brel brel, blk64_t old,
118*6a54128fSAndroid Build Coastguard Worker 			struct ext2_block_relocate_entry *ent)
119*6a54128fSAndroid Build Coastguard Worker {
120*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
121*6a54128fSAndroid Build Coastguard Worker 
122*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
123*6a54128fSAndroid Build Coastguard Worker 	if (old > ma->max_block)
124*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
125*6a54128fSAndroid Build Coastguard Worker 	if (ma->entries[(unsigned)old].new == 0)
126*6a54128fSAndroid Build Coastguard Worker 		return ENOENT;
127*6a54128fSAndroid Build Coastguard Worker 	*ent = ma->entries[old];
128*6a54128fSAndroid Build Coastguard Worker 	return 0;
129*6a54128fSAndroid Build Coastguard Worker }
130*6a54128fSAndroid Build Coastguard Worker 
bma_start_iter(ext2_brel brel)131*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_start_iter(ext2_brel brel)
132*6a54128fSAndroid Build Coastguard Worker {
133*6a54128fSAndroid Build Coastguard Worker 	brel->current = 0;
134*6a54128fSAndroid Build Coastguard Worker 	return 0;
135*6a54128fSAndroid Build Coastguard Worker }
136*6a54128fSAndroid Build Coastguard Worker 
bma_next(ext2_brel brel,blk64_t * old,struct ext2_block_relocate_entry * ent)137*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_next(ext2_brel brel, blk64_t *old,
138*6a54128fSAndroid Build Coastguard Worker 			  struct ext2_block_relocate_entry *ent)
139*6a54128fSAndroid Build Coastguard Worker {
140*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
141*6a54128fSAndroid Build Coastguard Worker 
142*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
143*6a54128fSAndroid Build Coastguard Worker 	while (++brel->current < ma->max_block) {
144*6a54128fSAndroid Build Coastguard Worker 		if (ma->entries[(unsigned)brel->current].new == 0)
145*6a54128fSAndroid Build Coastguard Worker 			continue;
146*6a54128fSAndroid Build Coastguard Worker 		*old = brel->current;
147*6a54128fSAndroid Build Coastguard Worker 		*ent = ma->entries[(unsigned)brel->current];
148*6a54128fSAndroid Build Coastguard Worker 		return 0;
149*6a54128fSAndroid Build Coastguard Worker 	}
150*6a54128fSAndroid Build Coastguard Worker 	*old = 0;
151*6a54128fSAndroid Build Coastguard Worker 	return 0;
152*6a54128fSAndroid Build Coastguard Worker }
153*6a54128fSAndroid Build Coastguard Worker 
bma_move(ext2_brel brel,blk64_t old,blk64_t new)154*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_move(ext2_brel brel, blk64_t old, blk64_t new)
155*6a54128fSAndroid Build Coastguard Worker {
156*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
157*6a54128fSAndroid Build Coastguard Worker 
158*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
159*6a54128fSAndroid Build Coastguard Worker 	if ((old > ma->max_block) || (new > ma->max_block))
160*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
161*6a54128fSAndroid Build Coastguard Worker 	if (ma->entries[(unsigned)old].new == 0)
162*6a54128fSAndroid Build Coastguard Worker 		return ENOENT;
163*6a54128fSAndroid Build Coastguard Worker 	ma->entries[(unsigned)new] = ma->entries[old];
164*6a54128fSAndroid Build Coastguard Worker 	ma->entries[(unsigned)old].new = 0;
165*6a54128fSAndroid Build Coastguard Worker 	return 0;
166*6a54128fSAndroid Build Coastguard Worker }
167*6a54128fSAndroid Build Coastguard Worker 
bma_delete(ext2_brel brel,blk64_t old)168*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_delete(ext2_brel brel, blk64_t old)
169*6a54128fSAndroid Build Coastguard Worker {
170*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
171*6a54128fSAndroid Build Coastguard Worker 
172*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
173*6a54128fSAndroid Build Coastguard Worker 	if (old > ma->max_block)
174*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_INVALID_ARGUMENT;
175*6a54128fSAndroid Build Coastguard Worker 	if (ma->entries[(unsigned)old].new == 0)
176*6a54128fSAndroid Build Coastguard Worker 		return ENOENT;
177*6a54128fSAndroid Build Coastguard Worker 	ma->entries[(unsigned)old].new = 0;
178*6a54128fSAndroid Build Coastguard Worker 	return 0;
179*6a54128fSAndroid Build Coastguard Worker }
180*6a54128fSAndroid Build Coastguard Worker 
bma_free(ext2_brel brel)181*6a54128fSAndroid Build Coastguard Worker static errcode_t bma_free(ext2_brel brel)
182*6a54128fSAndroid Build Coastguard Worker {
183*6a54128fSAndroid Build Coastguard Worker 	struct brel_ma 	*ma;
184*6a54128fSAndroid Build Coastguard Worker 
185*6a54128fSAndroid Build Coastguard Worker 	if (!brel)
186*6a54128fSAndroid Build Coastguard Worker 		return 0;
187*6a54128fSAndroid Build Coastguard Worker 
188*6a54128fSAndroid Build Coastguard Worker 	ma = brel->priv_data;
189*6a54128fSAndroid Build Coastguard Worker 
190*6a54128fSAndroid Build Coastguard Worker 	if (ma) {
191*6a54128fSAndroid Build Coastguard Worker 		if (ma->entries)
192*6a54128fSAndroid Build Coastguard Worker 			ext2fs_free_mem(&ma->entries);
193*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ma);
194*6a54128fSAndroid Build Coastguard Worker 	}
195*6a54128fSAndroid Build Coastguard Worker 	if (brel->name)
196*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&brel->name);
197*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&brel);
198*6a54128fSAndroid Build Coastguard Worker 	return 0;
199*6a54128fSAndroid Build Coastguard Worker }
200