blob: c871b1891982866f043f26bfe1ac43d422492179 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * irel_ma.c
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00005 * Copyright (C) 1996, 1997 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <fcntl.h>
14#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#if HAVE_ERRNO_H
20#include <errno.h>
21#endif
22
23#include "ext2_fs.h"
24#include "ext2fs.h"
25#include "irel.h"
26
27static errcode_t ima_put(ext2_irel irel, ext2_ino_t old,
28 struct ext2_inode_relocate_entry *ent);
29static errcode_t ima_get(ext2_irel irel, ext2_ino_t old,
30 struct ext2_inode_relocate_entry *ent);
31static errcode_t ima_get_by_orig(ext2_irel irel, ext2_ino_t orig, ext2_ino_t *old,
32 struct ext2_inode_relocate_entry *ent);
33static errcode_t ima_start_iter(ext2_irel irel);
34static errcode_t ima_next(ext2_irel irel, ext2_ino_t *old,
35 struct ext2_inode_relocate_entry *ent);
36static errcode_t ima_add_ref(ext2_irel irel, ext2_ino_t ino,
37 struct ext2_inode_reference *ref);
38static errcode_t ima_start_iter_ref(ext2_irel irel, ext2_ino_t ino);
39static errcode_t ima_next_ref(ext2_irel irel, struct ext2_inode_reference *ref);
40static errcode_t ima_move(ext2_irel irel, ext2_ino_t old, ext2_ino_t new);
41static errcode_t ima_delete(ext2_irel irel, ext2_ino_t old);
42static errcode_t ima_free(ext2_irel irel);
43
44/*
45 * This data structure stores the array of inode references; there is
46 * a structure for each inode.
47 */
48struct inode_reference_entry {
49 __u16 num;
50 struct ext2_inode_reference *refs;
51};
52
53struct irel_ma {
54 __u32 magic;
55 ext2_ino_t max_inode;
56 ext2_ino_t ref_current;
57 int ref_iter;
58 ext2_ino_t *orig_map;
59 struct ext2_inode_relocate_entry *entries;
60 struct inode_reference_entry *ref_entries;
61};
62
63errcode_t ext2fs_irel_memarray_create(char *name, ext2_ino_t max_inode,
64 ext2_irel *new_irel)
65{
66 ext2_irel irel = 0;
67 errcode_t retval;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000068 struct irel_ma *ma = 0;
Mike Frysinger1fd98e02005-05-09 22:10:42 +000069 size_t size;
70
71 *new_irel = 0;
72
73 /*
74 * Allocate memory structures
75 */
76 retval = ext2fs_get_mem(sizeof(struct ext2_inode_relocation_table),
77 &irel);
78 if (retval)
79 goto errout;
80 memset(irel, 0, sizeof(struct ext2_inode_relocation_table));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Mike Frysinger1fd98e02005-05-09 22:10:42 +000082 retval = ext2fs_get_mem(strlen(name)+1, &irel->name);
83 if (retval)
84 goto errout;
85 strcpy(irel->name, name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000086
Mike Frysinger1fd98e02005-05-09 22:10:42 +000087 retval = ext2fs_get_mem(sizeof(struct irel_ma), &ma);
88 if (retval)
89 goto errout;
90 memset(ma, 0, sizeof(struct irel_ma));
91 irel->priv_data = ma;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092
Mike Frysinger1fd98e02005-05-09 22:10:42 +000093 size = (size_t) (sizeof(ext2_ino_t) * (max_inode+1));
94 retval = ext2fs_get_mem(size, &ma->orig_map);
95 if (retval)
96 goto errout;
97 memset(ma->orig_map, 0, size);
98
99 size = (size_t) (sizeof(struct ext2_inode_relocate_entry) *
100 (max_inode+1));
101 retval = ext2fs_get_mem(size, &ma->entries);
102 if (retval)
103 goto errout;
104 memset(ma->entries, 0, size);
105
106 size = (size_t) (sizeof(struct inode_reference_entry) *
107 (max_inode+1));
108 retval = ext2fs_get_mem(size, &ma->ref_entries);
109 if (retval)
110 goto errout;
111 memset(ma->ref_entries, 0, size);
112 ma->max_inode = max_inode;
113
114 /*
115 * Fill in the irel data structure
116 */
117 irel->put = ima_put;
118 irel->get = ima_get;
119 irel->get_by_orig = ima_get_by_orig;
120 irel->start_iter = ima_start_iter;
121 irel->next = ima_next;
122 irel->add_ref = ima_add_ref;
123 irel->start_iter_ref = ima_start_iter_ref;
124 irel->next_ref = ima_next_ref;
125 irel->move = ima_move;
126 irel->delete = ima_delete;
127 irel->free = ima_free;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000128
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000129 *new_irel = irel;
130 return 0;
131
132errout:
133 ima_free(irel);
134 return retval;
135}
136
137static errcode_t ima_put(ext2_irel irel, ext2_ino_t old,
138 struct ext2_inode_relocate_entry *ent)
139{
140 struct inode_reference_entry *ref_ent;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000141 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000142 errcode_t retval;
143 size_t size, old_size;
144
145 ma = irel->priv_data;
146 if (old > ma->max_inode)
147 return EXT2_ET_INVALID_ARGUMENT;
148
149 /*
150 * Force the orig field to the correct value; the application
151 * program shouldn't be messing with this field.
152 */
153 if (ma->entries[(unsigned) old].new == 0)
154 ent->orig = old;
155 else
156 ent->orig = ma->entries[(unsigned) old].orig;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000157
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000158 /*
159 * If max_refs has changed, reallocate the refs array
160 */
161 ref_ent = ma->ref_entries + (unsigned) old;
162 if (ref_ent->refs && ent->max_refs !=
163 ma->entries[(unsigned) old].max_refs) {
164 size = (sizeof(struct ext2_inode_reference) * ent->max_refs);
165 old_size = (sizeof(struct ext2_inode_reference) *
166 ma->entries[(unsigned) old].max_refs);
167 retval = ext2fs_resize_mem(old_size, size, &ref_ent->refs);
168 if (retval)
169 return retval;
170 }
171
172 ma->entries[(unsigned) old] = *ent;
173 ma->orig_map[(unsigned) ent->orig] = old;
174 return 0;
175}
176
177static errcode_t ima_get(ext2_irel irel, ext2_ino_t old,
178 struct ext2_inode_relocate_entry *ent)
179{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000180 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000181
182 ma = irel->priv_data;
183 if (old > ma->max_inode)
184 return EXT2_ET_INVALID_ARGUMENT;
185 if (ma->entries[(unsigned) old].new == 0)
186 return ENOENT;
187 *ent = ma->entries[(unsigned) old];
188 return 0;
189}
190
191static errcode_t ima_get_by_orig(ext2_irel irel, ext2_ino_t orig, ext2_ino_t *old,
192 struct ext2_inode_relocate_entry *ent)
193{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000194 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000195 ext2_ino_t ino;
196
197 ma = irel->priv_data;
198 if (orig > ma->max_inode)
199 return EXT2_ET_INVALID_ARGUMENT;
200 ino = ma->orig_map[(unsigned) orig];
201 if (ino == 0)
202 return ENOENT;
203 *old = ino;
204 *ent = ma->entries[(unsigned) ino];
205 return 0;
206}
207
208static errcode_t ima_start_iter(ext2_irel irel)
209{
210 irel->current = 0;
211 return 0;
212}
213
214static errcode_t ima_next(ext2_irel irel, ext2_ino_t *old,
215 struct ext2_inode_relocate_entry *ent)
216{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000217 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000218
219 ma = irel->priv_data;
220 while (++irel->current < ma->max_inode) {
221 if (ma->entries[(unsigned) irel->current].new == 0)
222 continue;
223 *old = irel->current;
224 *ent = ma->entries[(unsigned) irel->current];
225 return 0;
226 }
227 *old = 0;
228 return 0;
229}
230
231static errcode_t ima_add_ref(ext2_irel irel, ext2_ino_t ino,
232 struct ext2_inode_reference *ref)
233{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000234 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000235 size_t size;
236 struct inode_reference_entry *ref_ent;
237 struct ext2_inode_relocate_entry *ent;
238 errcode_t retval;
239
240 ma = irel->priv_data;
241 if (ino > ma->max_inode)
242 return EXT2_ET_INVALID_ARGUMENT;
243
244 ref_ent = ma->ref_entries + (unsigned) ino;
245 ent = ma->entries + (unsigned) ino;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000246
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000247 /*
248 * If the inode reference array doesn't exist, create it.
249 */
250 if (ref_ent->refs == 0) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000251 size = (size_t) ((sizeof(struct ext2_inode_reference) *
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000252 ent->max_refs));
253 retval = ext2fs_get_mem(size, &ref_ent->refs);
254 if (retval)
255 return retval;
256 memset(ref_ent->refs, 0, size);
257 ref_ent->num = 0;
258 }
259
260 if (ref_ent->num >= ent->max_refs)
261 return EXT2_ET_TOO_MANY_REFS;
262
263 ref_ent->refs[(unsigned) ref_ent->num++] = *ref;
264 return 0;
265}
266
267static errcode_t ima_start_iter_ref(ext2_irel irel, ext2_ino_t ino)
268{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000269 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000270
271 ma = irel->priv_data;
272 if (ino > ma->max_inode)
273 return EXT2_ET_INVALID_ARGUMENT;
274 if (ma->entries[(unsigned) ino].new == 0)
275 return ENOENT;
276 ma->ref_current = ino;
277 ma->ref_iter = 0;
278 return 0;
279}
280
281static errcode_t ima_next_ref(ext2_irel irel,
282 struct ext2_inode_reference *ref)
283{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000284 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000285 struct inode_reference_entry *ref_ent;
286
287 ma = irel->priv_data;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000288
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000289 ref_ent = ma->ref_entries + ma->ref_current;
290
291 if ((ref_ent->refs == NULL) ||
292 (ma->ref_iter >= ref_ent->num)) {
293 ref->block = 0;
294 ref->offset = 0;
295 return 0;
296 }
297 *ref = ref_ent->refs[ma->ref_iter++];
298 return 0;
299}
300
301
302static errcode_t ima_move(ext2_irel irel, ext2_ino_t old, ext2_ino_t new)
303{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000304 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000305
306 ma = irel->priv_data;
307 if ((old > ma->max_inode) || (new > ma->max_inode))
308 return EXT2_ET_INVALID_ARGUMENT;
309 if (ma->entries[(unsigned) old].new == 0)
310 return ENOENT;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000311
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000312 ma->entries[(unsigned) new] = ma->entries[(unsigned) old];
Rob Landleye7c43b62006-03-01 16:39:45 +0000313 ext2fs_free_mem(&ma->ref_entries[(unsigned) new].refs);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000314 ma->ref_entries[(unsigned) new] = ma->ref_entries[(unsigned) old];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000315
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000316 ma->entries[(unsigned) old].new = 0;
317 ma->ref_entries[(unsigned) old].num = 0;
318 ma->ref_entries[(unsigned) old].refs = 0;
319
320 ma->orig_map[ma->entries[new].orig] = new;
321 return 0;
322}
323
324static errcode_t ima_delete(ext2_irel irel, ext2_ino_t old)
325{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000326 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000327
328 ma = irel->priv_data;
329 if (old > ma->max_inode)
330 return EXT2_ET_INVALID_ARGUMENT;
331 if (ma->entries[(unsigned) old].new == 0)
332 return ENOENT;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000333
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000334 ma->entries[old].new = 0;
Rob Landleye7c43b62006-03-01 16:39:45 +0000335 ext2fs_free_mem(&ma->ref_entries[(unsigned) old].refs);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000336 ma->orig_map[ma->entries[(unsigned) old].orig] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000337
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000338 ma->ref_entries[(unsigned) old].num = 0;
339 ma->ref_entries[(unsigned) old].refs = 0;
340 return 0;
341}
342
343static errcode_t ima_free(ext2_irel irel)
344{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000345 struct irel_ma *ma;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000346 ext2_ino_t ino;
347
348 if (!irel)
349 return 0;
350
351 ma = irel->priv_data;
352
353 if (ma) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000354 ext2fs_free_mem(&ma->orig_map);
355 ext2fs_free_mem(&ma->entries);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000356 if (ma->ref_entries) {
357 for (ino = 0; ino <= ma->max_inode; ino++) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000358 ext2fs_free_mem(&ma->ref_entries[(unsigned) ino].refs);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000359 }
360 ext2fs_free_mem(&ma->ref_entries);
361 }
362 ext2fs_free_mem(&ma);
363 }
Rob Landleye7c43b62006-03-01 16:39:45 +0000364 ext2fs_free_mem(&irel->name);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000365 ext2fs_free_mem(&irel);
366 return 0;
367}