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