Whamcloud - gitweb
3f82a29d9aa03680cf16b5237c3f6862f4f7ff47
[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 <stdlib.h>
18 #include <string.h>
19 #if HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #if HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25
26 #include <linux/ext2_fs.h>
27
28 #include "ext2fs.h"
29 #include "brel.h"
30
31 static errcode_t bma_put(ext2_brel brel, blk_t old,
32                         struct ext2_block_relocate_entry *ent);
33 static errcode_t bma_get(ext2_brel brel, blk_t old,
34                         struct ext2_block_relocate_entry *ent);
35 static errcode_t bma_start_iter(ext2_brel brel);
36 static errcode_t bma_next(ext2_brel brel, blk_t *old,
37                          struct ext2_block_relocate_entry *ent);
38 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new);
39 static errcode_t bma_delete(ext2_brel brel, blk_t old);
40 static errcode_t bma_free(ext2_brel brel);
41
42 struct brel_ma {
43         __u32 magic;
44         blk_t max_block;
45         struct ext2_block_relocate_entry *entries;
46 };
47
48 errcode_t ext2fs_brel_memarray_create(char *name, blk_t max_block,
49                                       ext2_brel *new_brel)
50 {
51         ext2_brel               brel = 0;
52         errcode_t       retval;
53         struct brel_ma  *ma = 0;
54         size_t          size;
55
56         *new_brel = 0;
57
58         /*
59          * Allocate memory structures
60          */
61         retval = ext2fs_get_mem(sizeof(struct ext2_block_relocation_table),
62                                 (void **) &brel);
63         if (retval)
64                 goto errout;
65         memset(brel, 0, sizeof(struct ext2_block_relocation_table));
66         
67         retval = ext2fs_get_mem(strlen(name)+1, (void **) &brel->name);
68         if (retval)
69                 goto errout;
70         strcpy(brel->name, name);
71         
72         retval = ext2fs_get_mem(sizeof(struct brel_ma), (void **) &ma);
73         if (retval)
74                 goto errout;
75         memset(ma, 0, sizeof(struct brel_ma));
76         brel->private = ma;
77         
78         size = (size_t) (sizeof(struct ext2_block_relocate_entry) *
79                          (max_block+1));
80         retval = ext2fs_get_mem(size, (void **) &ma->entries);
81         if (retval)
82                 goto errout;
83         memset(ma->entries, 0, size);
84         ma->max_block = max_block;
85
86         /*
87          * Fill in the brel data structure
88          */
89         brel->put = bma_put;
90         brel->get = bma_get;
91         brel->start_iter = bma_start_iter;
92         brel->next = bma_next;
93         brel->move = bma_move;
94         brel->delete = bma_delete;
95         brel->free = bma_free;
96         
97         *new_brel = brel;
98         return 0;
99
100 errout:
101         bma_free(brel);
102         return retval;
103 }
104
105 static errcode_t bma_put(ext2_brel brel, blk_t old,
106                         struct ext2_block_relocate_entry *ent)
107 {
108         struct brel_ma  *ma;
109
110         ma = brel->private;
111         if (old > ma->max_block)
112                 return EXT2_ET_INVALID_ARGUMENT;
113         ma->entries[(unsigned)old] = *ent;
114         return 0;
115 }
116
117 static errcode_t bma_get(ext2_brel brel, blk_t old,
118                         struct ext2_block_relocate_entry *ent)
119 {
120         struct brel_ma  *ma;
121
122         ma = brel->private;
123         if (old > ma->max_block)
124                 return EXT2_ET_INVALID_ARGUMENT;
125         if (ma->entries[(unsigned)old].new == 0)
126                 return ENOENT;
127         *ent = ma->entries[old];
128         return 0;
129 }
130
131 static errcode_t bma_start_iter(ext2_brel brel)
132 {
133         brel->current = 0;
134         return 0;
135 }
136
137 static errcode_t bma_next(ext2_brel brel, blk_t *old,
138                           struct ext2_block_relocate_entry *ent)
139 {
140         struct brel_ma  *ma;
141
142         ma = brel->private;
143         while (++brel->current < ma->max_block) {
144                 if (ma->entries[(unsigned)brel->current].new == 0)
145                         continue;
146                 *old = brel->current;
147                 *ent = ma->entries[(unsigned)brel->current];
148                 return 0;
149         }
150         *old = 0;
151         return 0;
152 }
153
154 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new)
155 {
156         struct brel_ma  *ma;
157
158         ma = brel->private;
159         if ((old > ma->max_block) || (new > ma->max_block))
160                 return EXT2_ET_INVALID_ARGUMENT;
161         if (ma->entries[(unsigned)old].new == 0)
162                 return ENOENT;
163         ma->entries[(unsigned)new] = ma->entries[old];
164         ma->entries[(unsigned)old].new = 0;
165         return 0;
166 }
167
168 static errcode_t bma_delete(ext2_brel brel, blk_t old)
169 {
170         struct brel_ma  *ma;
171
172         ma = brel->private;
173         if (old > ma->max_block)
174                 return EXT2_ET_INVALID_ARGUMENT;
175         if (ma->entries[(unsigned)old].new == 0)
176                 return ENOENT;
177         ma->entries[(unsigned)old].new = 0;
178         return 0;
179 }
180
181 static errcode_t bma_free(ext2_brel brel)
182 {
183         struct brel_ma  *ma;
184
185         if (!brel)
186                 return 0;
187
188         ma = brel->private;
189
190         if (ma) {
191                 if (ma->entries)
192                         ext2fs_free_mem((void **) &ma->entries);
193                 ext2fs_free_mem((void **) &ma);
194         }
195         if (brel->name)
196                 ext2fs_free_mem((void **) &brel->name);
197         ext2fs_free_mem((void **) &brel);
198         return 0;
199 }