Whamcloud - gitweb
ChangeLog, .del-io.h~72680822, irel_ma.c, llseek.c, rw_bitmaps.c:
[tools/e2fsprogs.git] / lib / ext2fs / irel_ma.c
1 /*
2  * irel_ma.c
3  * 
4  * Copyright (C) 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.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 <linux/ext2_fs.h>
24
25 #include "ext2fs.h"
26 #include "irel.h"
27
28 static errcode_t ima_put(ext2_irel irel, ino_t old,
29                          struct ext2_inode_relocate_entry *ent);
30 static errcode_t ima_get(ext2_irel irel, ino_t old,
31                          struct ext2_inode_relocate_entry *ent);
32 static errcode_t ima_get_by_orig(ext2_irel irel, ino_t orig, ino_t *old,
33                                  struct ext2_inode_relocate_entry *ent);
34 static errcode_t ima_start_iter(ext2_irel irel);
35 static errcode_t ima_next(ext2_irel irel, ino_t *old,
36                           struct ext2_inode_relocate_entry *ent);
37 static errcode_t ima_add_ref(ext2_irel irel, ino_t ino,
38                              struct ext2_inode_reference *ref);
39 static errcode_t ima_start_iter_ref(ext2_irel irel, ino_t ino);
40 static errcode_t ima_next_ref(ext2_irel irel, struct ext2_inode_reference *ref);
41 static errcode_t ima_move(ext2_irel irel, ino_t old, ino_t new);
42 static errcode_t ima_delete(ext2_irel irel, ino_t old);
43 static errcode_t ima_free(ext2_irel irel);
44
45 /*
46  * This data structure stores the array of inode references; there is
47  * a structure for each inode.
48  */
49 struct inode_reference_entry {
50         __u16 num;
51         struct ext2_inode_reference *refs;
52 };
53
54 struct irel_ma {
55         __u32 magic;
56         ino_t max_inode;
57         ino_t ref_current;
58         int   ref_iter;
59         ino_t   *orig_map;
60         struct ext2_inode_relocate_entry *entries;
61         struct inode_reference_entry *ref_entries;
62 };
63
64 errcode_t ext2fs_irel_memarray_create(char *name, ino_t max_inode,
65                                       ext2_irel *new_irel)
66 {
67         ext2_irel               irel = 0;
68         errcode_t       retval;
69         struct irel_ma  *ma = 0;
70         size_t          size;
71
72         *new_irel = 0;
73
74         /*
75          * Allocate memory structures
76          */
77         retval = ENOMEM;
78         irel = malloc(sizeof(struct ext2_inode_relocation_table));
79         if (!irel)
80                 goto errout;
81         memset(irel, 0, sizeof(struct ext2_inode_relocation_table));
82         
83         irel->name = malloc(strlen(name)+1);
84         if (!irel->name)
85                 goto errout;
86         strcpy(irel->name, name);
87         
88         ma = malloc(sizeof(struct irel_ma));
89         if (!ma)
90                 goto errout;
91         memset(ma, 0, sizeof(struct irel_ma));
92         irel->private = ma;
93         
94         size = (size_t) (sizeof(ino_t) * (max_inode+1));
95         ma->orig_map = malloc(size);
96         if (!ma->orig_map)
97                 goto errout;
98         memset(ma->orig_map, 0, size);
99
100         size = (size_t) (sizeof(struct ext2_inode_relocate_entry) *
101                          (max_inode+1));
102         ma->entries = malloc(size);
103         if (!ma->entries)
104                 goto errout;
105         memset(ma->entries, 0, size);
106
107         size = (size_t) (sizeof(struct inode_reference_entry) *
108                          (max_inode+1));
109         ma->ref_entries = malloc(size);
110         if (!ma->ref_entries)
111                 goto errout;
112         memset(ma->ref_entries, 0, size);
113         ma->max_inode = max_inode;
114
115         /*
116          * Fill in the irel data structure
117          */
118         irel->put = ima_put;
119         irel->get = ima_get;
120         irel->get_by_orig = ima_get_by_orig;
121         irel->start_iter = ima_start_iter;
122         irel->next = ima_next;
123         irel->add_ref = ima_add_ref;
124         irel->start_iter_ref = ima_start_iter_ref;
125         irel->next_ref = ima_next_ref;
126         irel->move = ima_move;
127         irel->delete = ima_delete;
128         irel->free = ima_free;
129         
130         *new_irel = irel;
131         return 0;
132
133 errout:
134         ima_free(irel);
135         return retval;
136 }
137
138 static errcode_t ima_put(ext2_irel irel, ino_t old,
139                         struct ext2_inode_relocate_entry *ent)
140 {
141         struct irel_ma  *ma;
142         struct inode_reference_entry *ref_ent;
143         struct ext2_inode_reference *new_refs;
144         int size;
145
146         ma = irel->private;
147         if (old > ma->max_inode)
148                 return EINVAL;
149
150         /*
151          * Force the orig field to the correct value; the application
152          * program shouldn't be messing with this field.
153          */
154         if (ma->entries[(unsigned) old].new == 0)
155                 ent->orig = old;
156         else
157                 ent->orig = ma->entries[(unsigned) old].orig;
158         
159         /*
160          * If max_refs has changed, reallocate the refs array
161          */
162         ref_ent = ma->ref_entries + (unsigned) old;
163         if (ref_ent->refs && ent->max_refs !=
164             ma->entries[(unsigned) old].max_refs) {
165                 size = (sizeof(struct ext2_inode_reference) * ent->max_refs);
166                 new_refs = realloc(ref_ent->refs, size);
167                 if (!new_refs)
168                         return ENOMEM;
169                 ref_ent->refs = new_refs;
170         }
171
172         ma->entries[(unsigned) old] = *ent;
173         ma->orig_map[(unsigned) ent->orig] = old;
174         return 0;
175 }
176
177 static errcode_t ima_get(ext2_irel irel, ino_t old,
178                         struct ext2_inode_relocate_entry *ent)
179 {
180         struct irel_ma  *ma;
181
182         ma = irel->private;
183         if (old > ma->max_inode)
184                 return EINVAL;
185         if (ma->entries[(unsigned) old].new == 0)
186                 return ENOENT;
187         *ent = ma->entries[(unsigned) old];
188         return 0;
189 }
190
191 static errcode_t ima_get_by_orig(ext2_irel irel, ino_t orig, ino_t *old,
192                         struct ext2_inode_relocate_entry *ent)
193 {
194         struct irel_ma  *ma;
195         ino_t   ino;
196
197         ma = irel->private;
198         if (orig > ma->max_inode)
199                 return EINVAL;
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
208 static errcode_t ima_start_iter(ext2_irel irel)
209 {
210         irel->current = 0;
211         return 0;
212 }
213
214 static errcode_t ima_next(ext2_irel irel, ino_t *old,
215                          struct ext2_inode_relocate_entry *ent)
216 {
217         struct irel_ma  *ma;
218
219         ma = irel->private;
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
231 static errcode_t ima_add_ref(ext2_irel irel, ino_t ino,
232                              struct ext2_inode_reference *ref)
233 {
234         struct irel_ma  *ma;
235         size_t          size;
236         struct inode_reference_entry *ref_ent;
237         struct ext2_inode_relocate_entry *ent;
238
239         ma = irel->private;
240         if (ino > ma->max_inode)
241                 return EINVAL;
242
243         ref_ent = ma->ref_entries + (unsigned) ino;
244         ent = ma->entries + (unsigned) ino;
245         
246         /*
247          * If the inode reference array doesn't exist, create it.
248          */
249         if (ref_ent->refs == 0) {
250                 size = (size_t) ((sizeof(struct ext2_inode_reference) * 
251                                   ent->max_refs));
252                 ref_ent->refs = malloc(size);
253                 if (ref_ent->refs == 0)
254                         return ENOMEM;
255                 memset(ref_ent->refs, 0, size);
256                 ref_ent->num = 0;
257         }
258
259         if (ref_ent->num >= ent->max_refs)
260                 return ENOSPC;
261
262         ref_ent->refs[(unsigned) ref_ent->num++] = *ref;
263         return 0;
264 }
265
266 static errcode_t ima_start_iter_ref(ext2_irel irel, ino_t ino)
267 {
268         struct irel_ma  *ma;
269
270         ma = irel->private;
271         if (ino > ma->max_inode)
272                 return EINVAL;
273         if (ma->entries[(unsigned) ino].new == 0)
274                 return ENOENT;
275         ma->ref_current = ino;
276         ma->ref_iter = 0;
277         return 0;
278 }
279
280 static errcode_t ima_next_ref(ext2_irel irel,
281                               struct ext2_inode_reference *ref)
282 {
283         struct irel_ma  *ma;
284         struct inode_reference_entry *ref_ent;
285
286         ma = irel->private;
287         
288         ref_ent = ma->ref_entries + ma->ref_current;
289
290         if ((ref_ent->refs == NULL) ||
291             (ma->ref_iter >= ref_ent->num)) {
292                 ref->block = 0;
293                 ref->offset = 0;
294                 return 0;
295         }
296         *ref = ref_ent->refs[ma->ref_iter++];
297         return 0;
298 }
299
300
301 static errcode_t ima_move(ext2_irel irel, ino_t old, ino_t new)
302 {
303         struct irel_ma  *ma;
304
305         ma = irel->private;
306         if ((old > ma->max_inode) || (new > ma->max_inode))
307                 return EINVAL;
308         if (ma->entries[(unsigned) old].new == 0)
309                 return ENOENT;
310         
311         ma->entries[(unsigned) new] = ma->entries[(unsigned) old];
312         if (ma->ref_entries[(unsigned) new].refs)
313                 free(ma->ref_entries[(unsigned) new].refs);
314         ma->ref_entries[(unsigned) new] = ma->ref_entries[(unsigned) old];
315         
316         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
324 static errcode_t ima_delete(ext2_irel irel, ino_t old)
325 {
326         struct irel_ma  *ma;
327
328         ma = irel->private;
329         if (old > ma->max_inode)
330                 return EINVAL;
331         if (ma->entries[(unsigned) old].new == 0)
332                 return ENOENT;
333         
334         ma->entries[old].new = 0;
335         if (ma->ref_entries[(unsigned) old].refs)
336                 free(ma->ref_entries[(unsigned) old].refs);
337         ma->orig_map[ma->entries[(unsigned) old].orig] = 0;
338         
339         ma->ref_entries[(unsigned) old].num = 0;
340         ma->ref_entries[(unsigned) old].refs = 0;
341         return 0;
342 }
343
344 static errcode_t ima_free(ext2_irel irel)
345 {
346         struct irel_ma  *ma;
347         ino_t   ino;
348
349         if (!irel)
350                 return 0;
351
352         ma = irel->private;
353
354         if (ma) {
355                 if (ma->orig_map)
356                         free (ma->orig_map);
357                 if (ma->entries)
358                         free (ma->entries);
359                 if (ma->ref_entries) {
360                         for (ino = 0; ino <= ma->max_inode; ino++) {
361                                 if (ma->ref_entries[(unsigned) ino].refs)
362                                         free(ma->ref_entries[(unsigned) ino].refs);
363                         }
364                         free(ma->ref_entries);
365                 }
366                 free(ma);
367         }
368         if (irel->name)
369                 free(irel->name);
370         free (irel);
371         return 0;
372 }