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