1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * icount.c --- an efficient inode count abstraction
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 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 #if HAVE_UNISTD_H
14*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
15*6a54128fSAndroid Build Coastguard Worker #endif
16*6a54128fSAndroid Build Coastguard Worker #include <string.h>
17*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
18*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
19*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
20*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
21*6a54128fSAndroid Build Coastguard Worker
22*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
23*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
24*6a54128fSAndroid Build Coastguard Worker #include "tdb.h"
25*6a54128fSAndroid Build Coastguard Worker
26*6a54128fSAndroid Build Coastguard Worker /*
27*6a54128fSAndroid Build Coastguard Worker * The data storage strategy used by icount relies on the observation
28*6a54128fSAndroid Build Coastguard Worker * that most inode counts are either zero (for non-allocated inodes),
29*6a54128fSAndroid Build Coastguard Worker * one (for most files), and only a few that are two or more
30*6a54128fSAndroid Build Coastguard Worker * (directories and files that are linked to more than one directory).
31*6a54128fSAndroid Build Coastguard Worker *
32*6a54128fSAndroid Build Coastguard Worker * Also, e2fsck tends to load the icount data sequentially.
33*6a54128fSAndroid Build Coastguard Worker *
34*6a54128fSAndroid Build Coastguard Worker * So, we use an inode bitmap to indicate which inodes have a count of
35*6a54128fSAndroid Build Coastguard Worker * one, and then use a sorted list to store the counts for inodes
36*6a54128fSAndroid Build Coastguard Worker * which are greater than one.
37*6a54128fSAndroid Build Coastguard Worker *
38*6a54128fSAndroid Build Coastguard Worker * We also use an optional bitmap to indicate which inodes are already
39*6a54128fSAndroid Build Coastguard Worker * in the sorted list, to speed up the use of this abstraction by
40*6a54128fSAndroid Build Coastguard Worker * e2fsck's pass 2. Pass 2 increments inode counts as it finds them,
41*6a54128fSAndroid Build Coastguard Worker * so this extra bitmap avoids searching the sorted list to see if a
42*6a54128fSAndroid Build Coastguard Worker * particular inode is on the sorted list already.
43*6a54128fSAndroid Build Coastguard Worker */
44*6a54128fSAndroid Build Coastguard Worker
45*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el {
46*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
47*6a54128fSAndroid Build Coastguard Worker __u32 count;
48*6a54128fSAndroid Build Coastguard Worker };
49*6a54128fSAndroid Build Coastguard Worker
50*6a54128fSAndroid Build Coastguard Worker struct ext2_icount {
51*6a54128fSAndroid Build Coastguard Worker errcode_t magic;
52*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap single;
53*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap multiple;
54*6a54128fSAndroid Build Coastguard Worker ext2_ino_t count;
55*6a54128fSAndroid Build Coastguard Worker ext2_ino_t size;
56*6a54128fSAndroid Build Coastguard Worker ext2_ino_t num_inodes;
57*6a54128fSAndroid Build Coastguard Worker ext2_ino_t cursor;
58*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el *list;
59*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el *last_lookup;
60*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
61*6a54128fSAndroid Build Coastguard Worker char *tdb_fn;
62*6a54128fSAndroid Build Coastguard Worker TDB_CONTEXT *tdb;
63*6a54128fSAndroid Build Coastguard Worker #endif
64*6a54128fSAndroid Build Coastguard Worker __u16 *fullmap;
65*6a54128fSAndroid Build Coastguard Worker };
66*6a54128fSAndroid Build Coastguard Worker
67*6a54128fSAndroid Build Coastguard Worker /*
68*6a54128fSAndroid Build Coastguard Worker * We now use a 32-bit counter field because it doesn't cost us
69*6a54128fSAndroid Build Coastguard Worker * anything extra for the in-memory data structure, due to alignment
70*6a54128fSAndroid Build Coastguard Worker * padding. But there's no point changing the interface if most of
71*6a54128fSAndroid Build Coastguard Worker * the time we only care if the number is bigger than 65,000 or not.
72*6a54128fSAndroid Build Coastguard Worker * So use the following translation function to return a 16-bit count.
73*6a54128fSAndroid Build Coastguard Worker */
74*6a54128fSAndroid Build Coastguard Worker #define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
75*6a54128fSAndroid Build Coastguard Worker
ext2fs_free_icount(ext2_icount_t icount)76*6a54128fSAndroid Build Coastguard Worker void ext2fs_free_icount(ext2_icount_t icount)
77*6a54128fSAndroid Build Coastguard Worker {
78*6a54128fSAndroid Build Coastguard Worker if (!icount)
79*6a54128fSAndroid Build Coastguard Worker return;
80*6a54128fSAndroid Build Coastguard Worker
81*6a54128fSAndroid Build Coastguard Worker icount->magic = 0;
82*6a54128fSAndroid Build Coastguard Worker if (icount->list)
83*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icount->list);
84*6a54128fSAndroid Build Coastguard Worker if (icount->single)
85*6a54128fSAndroid Build Coastguard Worker ext2fs_free_inode_bitmap(icount->single);
86*6a54128fSAndroid Build Coastguard Worker if (icount->multiple)
87*6a54128fSAndroid Build Coastguard Worker ext2fs_free_inode_bitmap(icount->multiple);
88*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
89*6a54128fSAndroid Build Coastguard Worker if (icount->tdb)
90*6a54128fSAndroid Build Coastguard Worker tdb_close(icount->tdb);
91*6a54128fSAndroid Build Coastguard Worker if (icount->tdb_fn) {
92*6a54128fSAndroid Build Coastguard Worker (void) unlink(icount->tdb_fn);
93*6a54128fSAndroid Build Coastguard Worker free(icount->tdb_fn);
94*6a54128fSAndroid Build Coastguard Worker }
95*6a54128fSAndroid Build Coastguard Worker #endif
96*6a54128fSAndroid Build Coastguard Worker
97*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap)
98*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icount->fullmap);
99*6a54128fSAndroid Build Coastguard Worker
100*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icount);
101*6a54128fSAndroid Build Coastguard Worker }
102*6a54128fSAndroid Build Coastguard Worker
alloc_icount(ext2_filsys fs,int flags,ext2_icount_t * ret)103*6a54128fSAndroid Build Coastguard Worker static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
104*6a54128fSAndroid Build Coastguard Worker {
105*6a54128fSAndroid Build Coastguard Worker ext2_icount_t icount;
106*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
107*6a54128fSAndroid Build Coastguard Worker
108*6a54128fSAndroid Build Coastguard Worker *ret = 0;
109*6a54128fSAndroid Build Coastguard Worker
110*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
111*6a54128fSAndroid Build Coastguard Worker if (retval)
112*6a54128fSAndroid Build Coastguard Worker return retval;
113*6a54128fSAndroid Build Coastguard Worker memset(icount, 0, sizeof(struct ext2_icount));
114*6a54128fSAndroid Build Coastguard Worker icount->magic = EXT2_ET_MAGIC_ICOUNT;
115*6a54128fSAndroid Build Coastguard Worker icount->num_inodes = fs->super->s_inodes_count;
116*6a54128fSAndroid Build Coastguard Worker
117*6a54128fSAndroid Build Coastguard Worker if ((flags & EXT2_ICOUNT_OPT_FULLMAP) &&
118*6a54128fSAndroid Build Coastguard Worker (flags & EXT2_ICOUNT_OPT_INCREMENT)) {
119*6a54128fSAndroid Build Coastguard Worker unsigned sz = sizeof(*icount->fullmap) * icount->num_inodes;
120*6a54128fSAndroid Build Coastguard Worker
121*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sz, &icount->fullmap);
122*6a54128fSAndroid Build Coastguard Worker /* If we can't allocate, fall back */
123*6a54128fSAndroid Build Coastguard Worker if (!retval) {
124*6a54128fSAndroid Build Coastguard Worker memset(icount->fullmap, 0, sz);
125*6a54128fSAndroid Build Coastguard Worker *ret = icount;
126*6a54128fSAndroid Build Coastguard Worker return 0;
127*6a54128fSAndroid Build Coastguard Worker }
128*6a54128fSAndroid Build Coastguard Worker }
129*6a54128fSAndroid Build Coastguard Worker
130*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_allocate_inode_bitmap(fs, "icount", &icount->single);
131*6a54128fSAndroid Build Coastguard Worker if (retval)
132*6a54128fSAndroid Build Coastguard Worker goto errout;
133*6a54128fSAndroid Build Coastguard Worker
134*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
135*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_allocate_inode_bitmap(fs, "icount_inc",
136*6a54128fSAndroid Build Coastguard Worker &icount->multiple);
137*6a54128fSAndroid Build Coastguard Worker if (retval)
138*6a54128fSAndroid Build Coastguard Worker goto errout;
139*6a54128fSAndroid Build Coastguard Worker } else
140*6a54128fSAndroid Build Coastguard Worker icount->multiple = 0;
141*6a54128fSAndroid Build Coastguard Worker
142*6a54128fSAndroid Build Coastguard Worker *ret = icount;
143*6a54128fSAndroid Build Coastguard Worker return 0;
144*6a54128fSAndroid Build Coastguard Worker
145*6a54128fSAndroid Build Coastguard Worker errout:
146*6a54128fSAndroid Build Coastguard Worker ext2fs_free_icount(icount);
147*6a54128fSAndroid Build Coastguard Worker return(retval);
148*6a54128fSAndroid Build Coastguard Worker }
149*6a54128fSAndroid Build Coastguard Worker
150*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
151*6a54128fSAndroid Build Coastguard Worker struct uuid {
152*6a54128fSAndroid Build Coastguard Worker __u32 time_low;
153*6a54128fSAndroid Build Coastguard Worker __u16 time_mid;
154*6a54128fSAndroid Build Coastguard Worker __u16 time_hi_and_version;
155*6a54128fSAndroid Build Coastguard Worker __u16 clock_seq;
156*6a54128fSAndroid Build Coastguard Worker __u8 node[6];
157*6a54128fSAndroid Build Coastguard Worker };
158*6a54128fSAndroid Build Coastguard Worker
unpack_uuid(void * in,struct uuid * uu)159*6a54128fSAndroid Build Coastguard Worker static void unpack_uuid(void *in, struct uuid *uu)
160*6a54128fSAndroid Build Coastguard Worker {
161*6a54128fSAndroid Build Coastguard Worker __u8 *ptr = in;
162*6a54128fSAndroid Build Coastguard Worker __u32 tmp;
163*6a54128fSAndroid Build Coastguard Worker
164*6a54128fSAndroid Build Coastguard Worker tmp = *ptr++;
165*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
166*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
167*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
168*6a54128fSAndroid Build Coastguard Worker uu->time_low = tmp;
169*6a54128fSAndroid Build Coastguard Worker
170*6a54128fSAndroid Build Coastguard Worker tmp = *ptr++;
171*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
172*6a54128fSAndroid Build Coastguard Worker uu->time_mid = tmp;
173*6a54128fSAndroid Build Coastguard Worker
174*6a54128fSAndroid Build Coastguard Worker tmp = *ptr++;
175*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
176*6a54128fSAndroid Build Coastguard Worker uu->time_hi_and_version = tmp;
177*6a54128fSAndroid Build Coastguard Worker
178*6a54128fSAndroid Build Coastguard Worker tmp = *ptr++;
179*6a54128fSAndroid Build Coastguard Worker tmp = (tmp << 8) | *ptr++;
180*6a54128fSAndroid Build Coastguard Worker uu->clock_seq = tmp;
181*6a54128fSAndroid Build Coastguard Worker
182*6a54128fSAndroid Build Coastguard Worker memcpy(uu->node, ptr, 6);
183*6a54128fSAndroid Build Coastguard Worker }
184*6a54128fSAndroid Build Coastguard Worker
uuid_unparse(void * uu,char * out)185*6a54128fSAndroid Build Coastguard Worker static void uuid_unparse(void *uu, char *out)
186*6a54128fSAndroid Build Coastguard Worker {
187*6a54128fSAndroid Build Coastguard Worker struct uuid uuid;
188*6a54128fSAndroid Build Coastguard Worker
189*6a54128fSAndroid Build Coastguard Worker unpack_uuid(uu, &uuid);
190*6a54128fSAndroid Build Coastguard Worker sprintf(out,
191*6a54128fSAndroid Build Coastguard Worker "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
192*6a54128fSAndroid Build Coastguard Worker uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
193*6a54128fSAndroid Build Coastguard Worker uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
194*6a54128fSAndroid Build Coastguard Worker uuid.node[0], uuid.node[1], uuid.node[2],
195*6a54128fSAndroid Build Coastguard Worker uuid.node[3], uuid.node[4], uuid.node[5]);
196*6a54128fSAndroid Build Coastguard Worker }
197*6a54128fSAndroid Build Coastguard Worker #endif
198*6a54128fSAndroid Build Coastguard Worker
ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,char * tdb_dir EXT2FS_NO_TDB_UNUSED,int flags EXT2FS_NO_TDB_UNUSED,ext2_icount_t * ret EXT2FS_NO_TDB_UNUSED)199*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,
200*6a54128fSAndroid Build Coastguard Worker char *tdb_dir EXT2FS_NO_TDB_UNUSED,
201*6a54128fSAndroid Build Coastguard Worker int flags EXT2FS_NO_TDB_UNUSED,
202*6a54128fSAndroid Build Coastguard Worker ext2_icount_t *ret EXT2FS_NO_TDB_UNUSED)
203*6a54128fSAndroid Build Coastguard Worker {
204*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
205*6a54128fSAndroid Build Coastguard Worker ext2_icount_t icount;
206*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
207*6a54128fSAndroid Build Coastguard Worker char *fn, uuid[40];
208*6a54128fSAndroid Build Coastguard Worker ext2_ino_t num_inodes;
209*6a54128fSAndroid Build Coastguard Worker mode_t save_umask;
210*6a54128fSAndroid Build Coastguard Worker int fd;
211*6a54128fSAndroid Build Coastguard Worker
212*6a54128fSAndroid Build Coastguard Worker retval = alloc_icount(fs, flags, &icount);
213*6a54128fSAndroid Build Coastguard Worker if (retval)
214*6a54128fSAndroid Build Coastguard Worker return retval;
215*6a54128fSAndroid Build Coastguard Worker
216*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
217*6a54128fSAndroid Build Coastguard Worker if (retval)
218*6a54128fSAndroid Build Coastguard Worker goto errout;
219*6a54128fSAndroid Build Coastguard Worker uuid_unparse(fs->super->s_uuid, uuid);
220*6a54128fSAndroid Build Coastguard Worker sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
221*6a54128fSAndroid Build Coastguard Worker save_umask = umask(077);
222*6a54128fSAndroid Build Coastguard Worker fd = mkstemp(fn);
223*6a54128fSAndroid Build Coastguard Worker if (fd < 0) {
224*6a54128fSAndroid Build Coastguard Worker retval = errno;
225*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&fn);
226*6a54128fSAndroid Build Coastguard Worker goto errout;
227*6a54128fSAndroid Build Coastguard Worker }
228*6a54128fSAndroid Build Coastguard Worker icount->tdb_fn = fn;
229*6a54128fSAndroid Build Coastguard Worker umask(save_umask);
230*6a54128fSAndroid Build Coastguard Worker /*
231*6a54128fSAndroid Build Coastguard Worker * This is an overestimate of the size that we will need; the
232*6a54128fSAndroid Build Coastguard Worker * ideal value is the number of used inodes with a count
233*6a54128fSAndroid Build Coastguard Worker * greater than 1. OTOH the times when we really need this is
234*6a54128fSAndroid Build Coastguard Worker * with the backup programs that use lots of hard links, in
235*6a54128fSAndroid Build Coastguard Worker * which case the number of inodes in use approaches the ideal
236*6a54128fSAndroid Build Coastguard Worker * value.
237*6a54128fSAndroid Build Coastguard Worker */
238*6a54128fSAndroid Build Coastguard Worker num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
239*6a54128fSAndroid Build Coastguard Worker
240*6a54128fSAndroid Build Coastguard Worker icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
241*6a54128fSAndroid Build Coastguard Worker O_RDWR | O_CREAT | O_TRUNC, 0600);
242*6a54128fSAndroid Build Coastguard Worker close(fd);
243*6a54128fSAndroid Build Coastguard Worker if (icount->tdb == NULL) {
244*6a54128fSAndroid Build Coastguard Worker retval = errno;
245*6a54128fSAndroid Build Coastguard Worker goto errout;
246*6a54128fSAndroid Build Coastguard Worker }
247*6a54128fSAndroid Build Coastguard Worker *ret = icount;
248*6a54128fSAndroid Build Coastguard Worker return 0;
249*6a54128fSAndroid Build Coastguard Worker errout:
250*6a54128fSAndroid Build Coastguard Worker ext2fs_free_icount(icount);
251*6a54128fSAndroid Build Coastguard Worker return(retval);
252*6a54128fSAndroid Build Coastguard Worker #else
253*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_UNIMPLEMENTED;
254*6a54128fSAndroid Build Coastguard Worker #endif
255*6a54128fSAndroid Build Coastguard Worker }
256*6a54128fSAndroid Build Coastguard Worker
ext2fs_create_icount2(ext2_filsys fs,int flags,unsigned int size,ext2_icount_t hint,ext2_icount_t * ret)257*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
258*6a54128fSAndroid Build Coastguard Worker ext2_icount_t hint, ext2_icount_t *ret)
259*6a54128fSAndroid Build Coastguard Worker {
260*6a54128fSAndroid Build Coastguard Worker ext2_icount_t icount;
261*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
262*6a54128fSAndroid Build Coastguard Worker size_t bytes;
263*6a54128fSAndroid Build Coastguard Worker ext2_ino_t i;
264*6a54128fSAndroid Build Coastguard Worker
265*6a54128fSAndroid Build Coastguard Worker if (hint) {
266*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
267*6a54128fSAndroid Build Coastguard Worker if (hint->size > size)
268*6a54128fSAndroid Build Coastguard Worker size = (size_t) hint->size;
269*6a54128fSAndroid Build Coastguard Worker }
270*6a54128fSAndroid Build Coastguard Worker
271*6a54128fSAndroid Build Coastguard Worker retval = alloc_icount(fs, flags, &icount);
272*6a54128fSAndroid Build Coastguard Worker if (retval)
273*6a54128fSAndroid Build Coastguard Worker return retval;
274*6a54128fSAndroid Build Coastguard Worker
275*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap)
276*6a54128fSAndroid Build Coastguard Worker goto successout;
277*6a54128fSAndroid Build Coastguard Worker
278*6a54128fSAndroid Build Coastguard Worker if (size) {
279*6a54128fSAndroid Build Coastguard Worker icount->size = size;
280*6a54128fSAndroid Build Coastguard Worker } else {
281*6a54128fSAndroid Build Coastguard Worker /*
282*6a54128fSAndroid Build Coastguard Worker * Figure out how many special case inode counts we will
283*6a54128fSAndroid Build Coastguard Worker * have. We know we will need one for each directory;
284*6a54128fSAndroid Build Coastguard Worker * we also need to reserve some extra room for file links
285*6a54128fSAndroid Build Coastguard Worker */
286*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_num_dirs(fs, &icount->size);
287*6a54128fSAndroid Build Coastguard Worker if (retval)
288*6a54128fSAndroid Build Coastguard Worker goto errout;
289*6a54128fSAndroid Build Coastguard Worker icount->size += fs->super->s_inodes_count / 50;
290*6a54128fSAndroid Build Coastguard Worker }
291*6a54128fSAndroid Build Coastguard Worker
292*6a54128fSAndroid Build Coastguard Worker bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
293*6a54128fSAndroid Build Coastguard Worker #if 0
294*6a54128fSAndroid Build Coastguard Worker printf("Icount allocated %u entries, %d bytes.\n",
295*6a54128fSAndroid Build Coastguard Worker icount->size, bytes);
296*6a54128fSAndroid Build Coastguard Worker #endif
297*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
298*6a54128fSAndroid Build Coastguard Worker &icount->list);
299*6a54128fSAndroid Build Coastguard Worker if (retval)
300*6a54128fSAndroid Build Coastguard Worker goto errout;
301*6a54128fSAndroid Build Coastguard Worker memset(icount->list, 0, bytes);
302*6a54128fSAndroid Build Coastguard Worker
303*6a54128fSAndroid Build Coastguard Worker icount->count = 0;
304*6a54128fSAndroid Build Coastguard Worker icount->cursor = 0;
305*6a54128fSAndroid Build Coastguard Worker
306*6a54128fSAndroid Build Coastguard Worker /*
307*6a54128fSAndroid Build Coastguard Worker * Populate the sorted list with those entries which were
308*6a54128fSAndroid Build Coastguard Worker * found in the hint icount (since those are ones which will
309*6a54128fSAndroid Build Coastguard Worker * likely need to be in the sorted list this time around).
310*6a54128fSAndroid Build Coastguard Worker */
311*6a54128fSAndroid Build Coastguard Worker if (hint) {
312*6a54128fSAndroid Build Coastguard Worker for (i=0; i < hint->count; i++)
313*6a54128fSAndroid Build Coastguard Worker icount->list[i].ino = hint->list[i].ino;
314*6a54128fSAndroid Build Coastguard Worker icount->count = hint->count;
315*6a54128fSAndroid Build Coastguard Worker }
316*6a54128fSAndroid Build Coastguard Worker
317*6a54128fSAndroid Build Coastguard Worker successout:
318*6a54128fSAndroid Build Coastguard Worker *ret = icount;
319*6a54128fSAndroid Build Coastguard Worker return 0;
320*6a54128fSAndroid Build Coastguard Worker
321*6a54128fSAndroid Build Coastguard Worker errout:
322*6a54128fSAndroid Build Coastguard Worker ext2fs_free_icount(icount);
323*6a54128fSAndroid Build Coastguard Worker return(retval);
324*6a54128fSAndroid Build Coastguard Worker }
325*6a54128fSAndroid Build Coastguard Worker
ext2fs_create_icount(ext2_filsys fs,int flags,unsigned int size,ext2_icount_t * ret)326*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
327*6a54128fSAndroid Build Coastguard Worker unsigned int size,
328*6a54128fSAndroid Build Coastguard Worker ext2_icount_t *ret)
329*6a54128fSAndroid Build Coastguard Worker {
330*6a54128fSAndroid Build Coastguard Worker return ext2fs_create_icount2(fs, flags, size, 0, ret);
331*6a54128fSAndroid Build Coastguard Worker }
332*6a54128fSAndroid Build Coastguard Worker
333*6a54128fSAndroid Build Coastguard Worker /*
334*6a54128fSAndroid Build Coastguard Worker * insert_icount_el() --- Insert a new entry into the sorted list at a
335*6a54128fSAndroid Build Coastguard Worker * specified position.
336*6a54128fSAndroid Build Coastguard Worker */
insert_icount_el(ext2_icount_t icount,ext2_ino_t ino,int pos)337*6a54128fSAndroid Build Coastguard Worker static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
338*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, int pos)
339*6a54128fSAndroid Build Coastguard Worker {
340*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el *el;
341*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
342*6a54128fSAndroid Build Coastguard Worker ext2_ino_t new_size = 0;
343*6a54128fSAndroid Build Coastguard Worker int num;
344*6a54128fSAndroid Build Coastguard Worker
345*6a54128fSAndroid Build Coastguard Worker if (icount->last_lookup && icount->last_lookup->ino == ino)
346*6a54128fSAndroid Build Coastguard Worker return icount->last_lookup;
347*6a54128fSAndroid Build Coastguard Worker
348*6a54128fSAndroid Build Coastguard Worker if (icount->count >= icount->size) {
349*6a54128fSAndroid Build Coastguard Worker if (icount->count) {
350*6a54128fSAndroid Build Coastguard Worker new_size = icount->list[(unsigned)icount->count-1].ino;
351*6a54128fSAndroid Build Coastguard Worker new_size = (ext2_ino_t) (icount->count *
352*6a54128fSAndroid Build Coastguard Worker ((float) icount->num_inodes / new_size));
353*6a54128fSAndroid Build Coastguard Worker }
354*6a54128fSAndroid Build Coastguard Worker if (new_size < (icount->size + 100))
355*6a54128fSAndroid Build Coastguard Worker new_size = icount->size + 100;
356*6a54128fSAndroid Build Coastguard Worker #if 0
357*6a54128fSAndroid Build Coastguard Worker printf("Reallocating icount %u entries...\n", new_size);
358*6a54128fSAndroid Build Coastguard Worker #endif
359*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_resize_mem((size_t) icount->size *
360*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_icount_el),
361*6a54128fSAndroid Build Coastguard Worker (size_t) new_size *
362*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_icount_el),
363*6a54128fSAndroid Build Coastguard Worker &icount->list);
364*6a54128fSAndroid Build Coastguard Worker if (retval)
365*6a54128fSAndroid Build Coastguard Worker return 0;
366*6a54128fSAndroid Build Coastguard Worker icount->size = new_size;
367*6a54128fSAndroid Build Coastguard Worker }
368*6a54128fSAndroid Build Coastguard Worker num = (int) icount->count - pos;
369*6a54128fSAndroid Build Coastguard Worker if (num < 0)
370*6a54128fSAndroid Build Coastguard Worker return 0; /* should never happen */
371*6a54128fSAndroid Build Coastguard Worker if (num) {
372*6a54128fSAndroid Build Coastguard Worker memmove(&icount->list[pos+1], &icount->list[pos],
373*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_icount_el) * num);
374*6a54128fSAndroid Build Coastguard Worker }
375*6a54128fSAndroid Build Coastguard Worker icount->count++;
376*6a54128fSAndroid Build Coastguard Worker el = &icount->list[pos];
377*6a54128fSAndroid Build Coastguard Worker el->count = 0;
378*6a54128fSAndroid Build Coastguard Worker el->ino = ino;
379*6a54128fSAndroid Build Coastguard Worker icount->last_lookup = el;
380*6a54128fSAndroid Build Coastguard Worker return el;
381*6a54128fSAndroid Build Coastguard Worker }
382*6a54128fSAndroid Build Coastguard Worker
383*6a54128fSAndroid Build Coastguard Worker /*
384*6a54128fSAndroid Build Coastguard Worker * get_icount_el() --- given an inode number, try to find icount
385*6a54128fSAndroid Build Coastguard Worker * information in the sorted list. If the create flag is set,
386*6a54128fSAndroid Build Coastguard Worker * and we can't find an entry, create one in the sorted list.
387*6a54128fSAndroid Build Coastguard Worker */
get_icount_el(ext2_icount_t icount,ext2_ino_t ino,int create)388*6a54128fSAndroid Build Coastguard Worker static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
389*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, int create)
390*6a54128fSAndroid Build Coastguard Worker {
391*6a54128fSAndroid Build Coastguard Worker int low, high, mid;
392*6a54128fSAndroid Build Coastguard Worker
393*6a54128fSAndroid Build Coastguard Worker if (!icount || !icount->list)
394*6a54128fSAndroid Build Coastguard Worker return 0;
395*6a54128fSAndroid Build Coastguard Worker
396*6a54128fSAndroid Build Coastguard Worker if (create && ((icount->count == 0) ||
397*6a54128fSAndroid Build Coastguard Worker (ino > icount->list[(unsigned)icount->count-1].ino))) {
398*6a54128fSAndroid Build Coastguard Worker return insert_icount_el(icount, ino, (unsigned) icount->count);
399*6a54128fSAndroid Build Coastguard Worker }
400*6a54128fSAndroid Build Coastguard Worker if (icount->count == 0)
401*6a54128fSAndroid Build Coastguard Worker return 0;
402*6a54128fSAndroid Build Coastguard Worker
403*6a54128fSAndroid Build Coastguard Worker if (icount->cursor >= icount->count)
404*6a54128fSAndroid Build Coastguard Worker icount->cursor = 0;
405*6a54128fSAndroid Build Coastguard Worker if (ino == icount->list[icount->cursor].ino)
406*6a54128fSAndroid Build Coastguard Worker return &icount->list[icount->cursor++];
407*6a54128fSAndroid Build Coastguard Worker #if 0
408*6a54128fSAndroid Build Coastguard Worker printf("Non-cursor get_icount_el: %u\n", ino);
409*6a54128fSAndroid Build Coastguard Worker #endif
410*6a54128fSAndroid Build Coastguard Worker low = 0;
411*6a54128fSAndroid Build Coastguard Worker high = (int) icount->count-1;
412*6a54128fSAndroid Build Coastguard Worker while (low <= high) {
413*6a54128fSAndroid Build Coastguard Worker mid = ((unsigned)low + (unsigned)high) >> 1;
414*6a54128fSAndroid Build Coastguard Worker if (ino == icount->list[mid].ino) {
415*6a54128fSAndroid Build Coastguard Worker icount->cursor = mid+1;
416*6a54128fSAndroid Build Coastguard Worker return &icount->list[mid];
417*6a54128fSAndroid Build Coastguard Worker }
418*6a54128fSAndroid Build Coastguard Worker if (ino < icount->list[mid].ino)
419*6a54128fSAndroid Build Coastguard Worker high = mid-1;
420*6a54128fSAndroid Build Coastguard Worker else
421*6a54128fSAndroid Build Coastguard Worker low = mid+1;
422*6a54128fSAndroid Build Coastguard Worker }
423*6a54128fSAndroid Build Coastguard Worker /*
424*6a54128fSAndroid Build Coastguard Worker * If we need to create a new entry, it should be right at
425*6a54128fSAndroid Build Coastguard Worker * low (where high will be left at low-1).
426*6a54128fSAndroid Build Coastguard Worker */
427*6a54128fSAndroid Build Coastguard Worker if (create)
428*6a54128fSAndroid Build Coastguard Worker return insert_icount_el(icount, ino, low);
429*6a54128fSAndroid Build Coastguard Worker return 0;
430*6a54128fSAndroid Build Coastguard Worker }
431*6a54128fSAndroid Build Coastguard Worker
set_inode_count(ext2_icount_t icount,ext2_ino_t ino,__u32 count)432*6a54128fSAndroid Build Coastguard Worker static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
433*6a54128fSAndroid Build Coastguard Worker __u32 count)
434*6a54128fSAndroid Build Coastguard Worker {
435*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el *el;
436*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
437*6a54128fSAndroid Build Coastguard Worker TDB_DATA key, data;
438*6a54128fSAndroid Build Coastguard Worker
439*6a54128fSAndroid Build Coastguard Worker if (icount->tdb) {
440*6a54128fSAndroid Build Coastguard Worker key.dptr = (unsigned char *) &ino;
441*6a54128fSAndroid Build Coastguard Worker key.dsize = sizeof(ext2_ino_t);
442*6a54128fSAndroid Build Coastguard Worker data.dptr = (unsigned char *) &count;
443*6a54128fSAndroid Build Coastguard Worker data.dsize = sizeof(__u32);
444*6a54128fSAndroid Build Coastguard Worker if (count) {
445*6a54128fSAndroid Build Coastguard Worker if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
446*6a54128fSAndroid Build Coastguard Worker return tdb_error(icount->tdb) +
447*6a54128fSAndroid Build Coastguard Worker EXT2_ET_TDB_SUCCESS;
448*6a54128fSAndroid Build Coastguard Worker } else {
449*6a54128fSAndroid Build Coastguard Worker if (tdb_delete(icount->tdb, key))
450*6a54128fSAndroid Build Coastguard Worker return tdb_error(icount->tdb) +
451*6a54128fSAndroid Build Coastguard Worker EXT2_ET_TDB_SUCCESS;
452*6a54128fSAndroid Build Coastguard Worker }
453*6a54128fSAndroid Build Coastguard Worker return 0;
454*6a54128fSAndroid Build Coastguard Worker }
455*6a54128fSAndroid Build Coastguard Worker #endif
456*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap) {
457*6a54128fSAndroid Build Coastguard Worker icount->fullmap[ino] = icount_16_xlate(count);
458*6a54128fSAndroid Build Coastguard Worker return 0;
459*6a54128fSAndroid Build Coastguard Worker }
460*6a54128fSAndroid Build Coastguard Worker
461*6a54128fSAndroid Build Coastguard Worker el = get_icount_el(icount, ino, 1);
462*6a54128fSAndroid Build Coastguard Worker if (!el)
463*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
464*6a54128fSAndroid Build Coastguard Worker
465*6a54128fSAndroid Build Coastguard Worker el->count = count;
466*6a54128fSAndroid Build Coastguard Worker return 0;
467*6a54128fSAndroid Build Coastguard Worker }
468*6a54128fSAndroid Build Coastguard Worker
get_inode_count(ext2_icount_t icount,ext2_ino_t ino,__u32 * count)469*6a54128fSAndroid Build Coastguard Worker static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
470*6a54128fSAndroid Build Coastguard Worker __u32 *count)
471*6a54128fSAndroid Build Coastguard Worker {
472*6a54128fSAndroid Build Coastguard Worker struct ext2_icount_el *el;
473*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
474*6a54128fSAndroid Build Coastguard Worker TDB_DATA key, data;
475*6a54128fSAndroid Build Coastguard Worker
476*6a54128fSAndroid Build Coastguard Worker if (icount->tdb) {
477*6a54128fSAndroid Build Coastguard Worker key.dptr = (unsigned char *) &ino;
478*6a54128fSAndroid Build Coastguard Worker key.dsize = sizeof(ext2_ino_t);
479*6a54128fSAndroid Build Coastguard Worker
480*6a54128fSAndroid Build Coastguard Worker data = tdb_fetch(icount->tdb, key);
481*6a54128fSAndroid Build Coastguard Worker if (data.dptr == NULL) {
482*6a54128fSAndroid Build Coastguard Worker *count = 0;
483*6a54128fSAndroid Build Coastguard Worker return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
484*6a54128fSAndroid Build Coastguard Worker }
485*6a54128fSAndroid Build Coastguard Worker
486*6a54128fSAndroid Build Coastguard Worker *count = *((__u32 *) data.dptr);
487*6a54128fSAndroid Build Coastguard Worker free(data.dptr);
488*6a54128fSAndroid Build Coastguard Worker return 0;
489*6a54128fSAndroid Build Coastguard Worker }
490*6a54128fSAndroid Build Coastguard Worker #endif
491*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap) {
492*6a54128fSAndroid Build Coastguard Worker *count = icount->fullmap[ino];
493*6a54128fSAndroid Build Coastguard Worker return 0;
494*6a54128fSAndroid Build Coastguard Worker }
495*6a54128fSAndroid Build Coastguard Worker
496*6a54128fSAndroid Build Coastguard Worker el = get_icount_el(icount, ino, 0);
497*6a54128fSAndroid Build Coastguard Worker if (!el) {
498*6a54128fSAndroid Build Coastguard Worker *count = 0;
499*6a54128fSAndroid Build Coastguard Worker return ENOENT;
500*6a54128fSAndroid Build Coastguard Worker }
501*6a54128fSAndroid Build Coastguard Worker
502*6a54128fSAndroid Build Coastguard Worker *count = el->count;
503*6a54128fSAndroid Build Coastguard Worker return 0;
504*6a54128fSAndroid Build Coastguard Worker }
505*6a54128fSAndroid Build Coastguard Worker
ext2fs_icount_validate(ext2_icount_t icount,FILE * out)506*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
507*6a54128fSAndroid Build Coastguard Worker {
508*6a54128fSAndroid Build Coastguard Worker errcode_t ret = 0;
509*6a54128fSAndroid Build Coastguard Worker unsigned int i;
510*6a54128fSAndroid Build Coastguard Worker const char *bad = "bad icount";
511*6a54128fSAndroid Build Coastguard Worker
512*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
513*6a54128fSAndroid Build Coastguard Worker
514*6a54128fSAndroid Build Coastguard Worker if (icount->count > icount->size) {
515*6a54128fSAndroid Build Coastguard Worker fprintf(out, "%s: count > size\n", bad);
516*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
517*6a54128fSAndroid Build Coastguard Worker }
518*6a54128fSAndroid Build Coastguard Worker for (i=1; i < icount->count; i++) {
519*6a54128fSAndroid Build Coastguard Worker if (icount->list[i-1].ino >= icount->list[i].ino) {
520*6a54128fSAndroid Build Coastguard Worker fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
521*6a54128fSAndroid Build Coastguard Worker bad, i-1, icount->list[i-1].ino,
522*6a54128fSAndroid Build Coastguard Worker i, icount->list[i].ino);
523*6a54128fSAndroid Build Coastguard Worker ret = EXT2_ET_INVALID_ARGUMENT;
524*6a54128fSAndroid Build Coastguard Worker }
525*6a54128fSAndroid Build Coastguard Worker }
526*6a54128fSAndroid Build Coastguard Worker return ret;
527*6a54128fSAndroid Build Coastguard Worker }
528*6a54128fSAndroid Build Coastguard Worker
ext2fs_icount_fetch(ext2_icount_t icount,ext2_ino_t ino,__u16 * ret)529*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
530*6a54128fSAndroid Build Coastguard Worker {
531*6a54128fSAndroid Build Coastguard Worker __u32 val;
532*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
533*6a54128fSAndroid Build Coastguard Worker
534*6a54128fSAndroid Build Coastguard Worker if (!ino || (ino > icount->num_inodes))
535*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
536*6a54128fSAndroid Build Coastguard Worker
537*6a54128fSAndroid Build Coastguard Worker if (!icount->fullmap) {
538*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
539*6a54128fSAndroid Build Coastguard Worker *ret = 1;
540*6a54128fSAndroid Build Coastguard Worker return 0;
541*6a54128fSAndroid Build Coastguard Worker }
542*6a54128fSAndroid Build Coastguard Worker if (icount->multiple &&
543*6a54128fSAndroid Build Coastguard Worker !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
544*6a54128fSAndroid Build Coastguard Worker *ret = 0;
545*6a54128fSAndroid Build Coastguard Worker return 0;
546*6a54128fSAndroid Build Coastguard Worker }
547*6a54128fSAndroid Build Coastguard Worker }
548*6a54128fSAndroid Build Coastguard Worker get_inode_count(icount, ino, &val);
549*6a54128fSAndroid Build Coastguard Worker *ret = icount_16_xlate(val);
550*6a54128fSAndroid Build Coastguard Worker return 0;
551*6a54128fSAndroid Build Coastguard Worker }
552*6a54128fSAndroid Build Coastguard Worker
ext2fs_icount_increment(ext2_icount_t icount,ext2_ino_t ino,__u16 * ret)553*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
554*6a54128fSAndroid Build Coastguard Worker __u16 *ret)
555*6a54128fSAndroid Build Coastguard Worker {
556*6a54128fSAndroid Build Coastguard Worker __u32 curr_value;
557*6a54128fSAndroid Build Coastguard Worker
558*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
559*6a54128fSAndroid Build Coastguard Worker
560*6a54128fSAndroid Build Coastguard Worker if (!ino || (ino > icount->num_inodes))
561*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
562*6a54128fSAndroid Build Coastguard Worker
563*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap) {
564*6a54128fSAndroid Build Coastguard Worker curr_value = icount_16_xlate(icount->fullmap[ino] + 1);
565*6a54128fSAndroid Build Coastguard Worker icount->fullmap[ino] = curr_value;
566*6a54128fSAndroid Build Coastguard Worker } else if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
567*6a54128fSAndroid Build Coastguard Worker /*
568*6a54128fSAndroid Build Coastguard Worker * If the existing count is 1, then we know there is
569*6a54128fSAndroid Build Coastguard Worker * no entry in the list.
570*6a54128fSAndroid Build Coastguard Worker */
571*6a54128fSAndroid Build Coastguard Worker if (set_inode_count(icount, ino, 2))
572*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
573*6a54128fSAndroid Build Coastguard Worker curr_value = 2;
574*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->single, ino);
575*6a54128fSAndroid Build Coastguard Worker } else if (icount->multiple) {
576*6a54128fSAndroid Build Coastguard Worker /*
577*6a54128fSAndroid Build Coastguard Worker * The count is either zero or greater than 1; if the
578*6a54128fSAndroid Build Coastguard Worker * inode is set in icount->multiple, then there should
579*6a54128fSAndroid Build Coastguard Worker * be an entry in the list, so we need to fix it.
580*6a54128fSAndroid Build Coastguard Worker */
581*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
582*6a54128fSAndroid Build Coastguard Worker get_inode_count(icount, ino, &curr_value);
583*6a54128fSAndroid Build Coastguard Worker curr_value++;
584*6a54128fSAndroid Build Coastguard Worker if (set_inode_count(icount, ino, curr_value))
585*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
586*6a54128fSAndroid Build Coastguard Worker } else {
587*6a54128fSAndroid Build Coastguard Worker /*
588*6a54128fSAndroid Build Coastguard Worker * The count was zero; mark the single bitmap
589*6a54128fSAndroid Build Coastguard Worker * and return.
590*6a54128fSAndroid Build Coastguard Worker */
591*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(icount->single, ino);
592*6a54128fSAndroid Build Coastguard Worker if (ret)
593*6a54128fSAndroid Build Coastguard Worker *ret = 1;
594*6a54128fSAndroid Build Coastguard Worker return 0;
595*6a54128fSAndroid Build Coastguard Worker }
596*6a54128fSAndroid Build Coastguard Worker } else {
597*6a54128fSAndroid Build Coastguard Worker /*
598*6a54128fSAndroid Build Coastguard Worker * The count is either zero or greater than 1; try to
599*6a54128fSAndroid Build Coastguard Worker * find an entry in the list to determine which.
600*6a54128fSAndroid Build Coastguard Worker */
601*6a54128fSAndroid Build Coastguard Worker get_inode_count(icount, ino, &curr_value);
602*6a54128fSAndroid Build Coastguard Worker curr_value++;
603*6a54128fSAndroid Build Coastguard Worker if (set_inode_count(icount, ino, curr_value))
604*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
605*6a54128fSAndroid Build Coastguard Worker }
606*6a54128fSAndroid Build Coastguard Worker if (icount->multiple)
607*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(icount->multiple, ino);
608*6a54128fSAndroid Build Coastguard Worker if (ret)
609*6a54128fSAndroid Build Coastguard Worker *ret = icount_16_xlate(curr_value);
610*6a54128fSAndroid Build Coastguard Worker return 0;
611*6a54128fSAndroid Build Coastguard Worker }
612*6a54128fSAndroid Build Coastguard Worker
ext2fs_icount_decrement(ext2_icount_t icount,ext2_ino_t ino,__u16 * ret)613*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
614*6a54128fSAndroid Build Coastguard Worker __u16 *ret)
615*6a54128fSAndroid Build Coastguard Worker {
616*6a54128fSAndroid Build Coastguard Worker __u32 curr_value;
617*6a54128fSAndroid Build Coastguard Worker
618*6a54128fSAndroid Build Coastguard Worker if (!ino || (ino > icount->num_inodes))
619*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
620*6a54128fSAndroid Build Coastguard Worker
621*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
622*6a54128fSAndroid Build Coastguard Worker
623*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap) {
624*6a54128fSAndroid Build Coastguard Worker if (!icount->fullmap[ino])
625*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
626*6a54128fSAndroid Build Coastguard Worker
627*6a54128fSAndroid Build Coastguard Worker curr_value = --icount->fullmap[ino];
628*6a54128fSAndroid Build Coastguard Worker if (ret)
629*6a54128fSAndroid Build Coastguard Worker *ret = icount_16_xlate(curr_value);
630*6a54128fSAndroid Build Coastguard Worker return 0;
631*6a54128fSAndroid Build Coastguard Worker }
632*6a54128fSAndroid Build Coastguard Worker
633*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
634*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->single, ino);
635*6a54128fSAndroid Build Coastguard Worker if (icount->multiple)
636*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
637*6a54128fSAndroid Build Coastguard Worker else {
638*6a54128fSAndroid Build Coastguard Worker set_inode_count(icount, ino, 0);
639*6a54128fSAndroid Build Coastguard Worker }
640*6a54128fSAndroid Build Coastguard Worker if (ret)
641*6a54128fSAndroid Build Coastguard Worker *ret = 0;
642*6a54128fSAndroid Build Coastguard Worker return 0;
643*6a54128fSAndroid Build Coastguard Worker }
644*6a54128fSAndroid Build Coastguard Worker
645*6a54128fSAndroid Build Coastguard Worker if (icount->multiple &&
646*6a54128fSAndroid Build Coastguard Worker !ext2fs_test_inode_bitmap2(icount->multiple, ino))
647*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
648*6a54128fSAndroid Build Coastguard Worker
649*6a54128fSAndroid Build Coastguard Worker get_inode_count(icount, ino, &curr_value);
650*6a54128fSAndroid Build Coastguard Worker if (!curr_value)
651*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
652*6a54128fSAndroid Build Coastguard Worker curr_value--;
653*6a54128fSAndroid Build Coastguard Worker if (set_inode_count(icount, ino, curr_value))
654*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
655*6a54128fSAndroid Build Coastguard Worker
656*6a54128fSAndroid Build Coastguard Worker if (curr_value == 1)
657*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(icount->single, ino);
658*6a54128fSAndroid Build Coastguard Worker if ((curr_value == 0) && icount->multiple)
659*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
660*6a54128fSAndroid Build Coastguard Worker
661*6a54128fSAndroid Build Coastguard Worker if (ret)
662*6a54128fSAndroid Build Coastguard Worker *ret = icount_16_xlate(curr_value);
663*6a54128fSAndroid Build Coastguard Worker return 0;
664*6a54128fSAndroid Build Coastguard Worker }
665*6a54128fSAndroid Build Coastguard Worker
ext2fs_icount_store(ext2_icount_t icount,ext2_ino_t ino,__u16 count)666*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
667*6a54128fSAndroid Build Coastguard Worker __u16 count)
668*6a54128fSAndroid Build Coastguard Worker {
669*6a54128fSAndroid Build Coastguard Worker if (!ino || (ino > icount->num_inodes))
670*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
671*6a54128fSAndroid Build Coastguard Worker
672*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
673*6a54128fSAndroid Build Coastguard Worker
674*6a54128fSAndroid Build Coastguard Worker if (icount->fullmap)
675*6a54128fSAndroid Build Coastguard Worker return set_inode_count(icount, ino, count);
676*6a54128fSAndroid Build Coastguard Worker
677*6a54128fSAndroid Build Coastguard Worker if (count == 1) {
678*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(icount->single, ino);
679*6a54128fSAndroid Build Coastguard Worker if (icount->multiple)
680*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
681*6a54128fSAndroid Build Coastguard Worker return 0;
682*6a54128fSAndroid Build Coastguard Worker }
683*6a54128fSAndroid Build Coastguard Worker if (count == 0) {
684*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->single, ino);
685*6a54128fSAndroid Build Coastguard Worker if (icount->multiple) {
686*6a54128fSAndroid Build Coastguard Worker /*
687*6a54128fSAndroid Build Coastguard Worker * If the icount->multiple bitmap is enabled,
688*6a54128fSAndroid Build Coastguard Worker * we can just clear both bitmaps and we're done
689*6a54128fSAndroid Build Coastguard Worker */
690*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
691*6a54128fSAndroid Build Coastguard Worker } else
692*6a54128fSAndroid Build Coastguard Worker set_inode_count(icount, ino, 0);
693*6a54128fSAndroid Build Coastguard Worker return 0;
694*6a54128fSAndroid Build Coastguard Worker }
695*6a54128fSAndroid Build Coastguard Worker
696*6a54128fSAndroid Build Coastguard Worker if (set_inode_count(icount, ino, count))
697*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_MEMORY;
698*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(icount->single, ino);
699*6a54128fSAndroid Build Coastguard Worker if (icount->multiple)
700*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(icount->multiple, ino);
701*6a54128fSAndroid Build Coastguard Worker return 0;
702*6a54128fSAndroid Build Coastguard Worker }
703*6a54128fSAndroid Build Coastguard Worker
ext2fs_get_icount_size(ext2_icount_t icount)704*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
705*6a54128fSAndroid Build Coastguard Worker {
706*6a54128fSAndroid Build Coastguard Worker if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
707*6a54128fSAndroid Build Coastguard Worker return 0;
708*6a54128fSAndroid Build Coastguard Worker
709*6a54128fSAndroid Build Coastguard Worker return icount->size;
710*6a54128fSAndroid Build Coastguard Worker }
711*6a54128fSAndroid Build Coastguard Worker
712*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
713*6a54128fSAndroid Build Coastguard Worker
714*6a54128fSAndroid Build Coastguard Worker ext2_filsys test_fs;
715*6a54128fSAndroid Build Coastguard Worker ext2_icount_t icount;
716*6a54128fSAndroid Build Coastguard Worker
717*6a54128fSAndroid Build Coastguard Worker #define EXIT 0x00
718*6a54128fSAndroid Build Coastguard Worker #define FETCH 0x01
719*6a54128fSAndroid Build Coastguard Worker #define STORE 0x02
720*6a54128fSAndroid Build Coastguard Worker #define INCREMENT 0x03
721*6a54128fSAndroid Build Coastguard Worker #define DECREMENT 0x04
722*6a54128fSAndroid Build Coastguard Worker
723*6a54128fSAndroid Build Coastguard Worker struct test_program {
724*6a54128fSAndroid Build Coastguard Worker int cmd;
725*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
726*6a54128fSAndroid Build Coastguard Worker __u16 arg;
727*6a54128fSAndroid Build Coastguard Worker __u16 expected;
728*6a54128fSAndroid Build Coastguard Worker };
729*6a54128fSAndroid Build Coastguard Worker
730*6a54128fSAndroid Build Coastguard Worker struct test_program prog[] = {
731*6a54128fSAndroid Build Coastguard Worker { STORE, 42, 42, 42 },
732*6a54128fSAndroid Build Coastguard Worker { STORE, 1, 1, 1 },
733*6a54128fSAndroid Build Coastguard Worker { STORE, 2, 2, 2 },
734*6a54128fSAndroid Build Coastguard Worker { STORE, 3, 3, 3 },
735*6a54128fSAndroid Build Coastguard Worker { STORE, 10, 1, 1 },
736*6a54128fSAndroid Build Coastguard Worker { STORE, 42, 0, 0 },
737*6a54128fSAndroid Build Coastguard Worker { INCREMENT, 5, 0, 1 },
738*6a54128fSAndroid Build Coastguard Worker { INCREMENT, 5, 0, 2 },
739*6a54128fSAndroid Build Coastguard Worker { INCREMENT, 5, 0, 3 },
740*6a54128fSAndroid Build Coastguard Worker { INCREMENT, 5, 0, 4 },
741*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 5, 0, 3 },
742*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 5, 0, 2 },
743*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 5, 0, 1 },
744*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 5, 0, 0 },
745*6a54128fSAndroid Build Coastguard Worker { FETCH, 10, 0, 1 },
746*6a54128fSAndroid Build Coastguard Worker { FETCH, 1, 0, 1 },
747*6a54128fSAndroid Build Coastguard Worker { FETCH, 2, 0, 2 },
748*6a54128fSAndroid Build Coastguard Worker { FETCH, 3, 0, 3 },
749*6a54128fSAndroid Build Coastguard Worker { INCREMENT, 1, 0, 2 },
750*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 2, 0, 1 },
751*6a54128fSAndroid Build Coastguard Worker { DECREMENT, 2, 0, 0 },
752*6a54128fSAndroid Build Coastguard Worker { FETCH, 12, 0, 0 },
753*6a54128fSAndroid Build Coastguard Worker { EXIT, 0, 0, 0 }
754*6a54128fSAndroid Build Coastguard Worker };
755*6a54128fSAndroid Build Coastguard Worker
756*6a54128fSAndroid Build Coastguard Worker struct test_program extended[] = {
757*6a54128fSAndroid Build Coastguard Worker { STORE, 1, 1, 1 },
758*6a54128fSAndroid Build Coastguard Worker { STORE, 2, 2, 2 },
759*6a54128fSAndroid Build Coastguard Worker { STORE, 3, 3, 3 },
760*6a54128fSAndroid Build Coastguard Worker { STORE, 4, 4, 4 },
761*6a54128fSAndroid Build Coastguard Worker { STORE, 5, 5, 5 },
762*6a54128fSAndroid Build Coastguard Worker { STORE, 6, 1, 1 },
763*6a54128fSAndroid Build Coastguard Worker { STORE, 7, 2, 2 },
764*6a54128fSAndroid Build Coastguard Worker { STORE, 8, 3, 3 },
765*6a54128fSAndroid Build Coastguard Worker { STORE, 9, 4, 4 },
766*6a54128fSAndroid Build Coastguard Worker { STORE, 10, 5, 5 },
767*6a54128fSAndroid Build Coastguard Worker { STORE, 11, 1, 1 },
768*6a54128fSAndroid Build Coastguard Worker { STORE, 12, 2, 2 },
769*6a54128fSAndroid Build Coastguard Worker { STORE, 13, 3, 3 },
770*6a54128fSAndroid Build Coastguard Worker { STORE, 14, 4, 4 },
771*6a54128fSAndroid Build Coastguard Worker { STORE, 15, 5, 5 },
772*6a54128fSAndroid Build Coastguard Worker { STORE, 16, 1, 1 },
773*6a54128fSAndroid Build Coastguard Worker { STORE, 17, 2, 2 },
774*6a54128fSAndroid Build Coastguard Worker { STORE, 18, 3, 3 },
775*6a54128fSAndroid Build Coastguard Worker { STORE, 19, 4, 4 },
776*6a54128fSAndroid Build Coastguard Worker { STORE, 20, 5, 5 },
777*6a54128fSAndroid Build Coastguard Worker { STORE, 21, 1, 1 },
778*6a54128fSAndroid Build Coastguard Worker { STORE, 22, 2, 2 },
779*6a54128fSAndroid Build Coastguard Worker { STORE, 23, 3, 3 },
780*6a54128fSAndroid Build Coastguard Worker { STORE, 24, 4, 4 },
781*6a54128fSAndroid Build Coastguard Worker { STORE, 25, 5, 5 },
782*6a54128fSAndroid Build Coastguard Worker { STORE, 26, 1, 1 },
783*6a54128fSAndroid Build Coastguard Worker { STORE, 27, 2, 2 },
784*6a54128fSAndroid Build Coastguard Worker { STORE, 28, 3, 3 },
785*6a54128fSAndroid Build Coastguard Worker { STORE, 29, 4, 4 },
786*6a54128fSAndroid Build Coastguard Worker { STORE, 30, 5, 5 },
787*6a54128fSAndroid Build Coastguard Worker { EXIT, 0, 0, 0 }
788*6a54128fSAndroid Build Coastguard Worker };
789*6a54128fSAndroid Build Coastguard Worker
790*6a54128fSAndroid Build Coastguard Worker /*
791*6a54128fSAndroid Build Coastguard Worker * Setup the variables for doing the inode scan test.
792*6a54128fSAndroid Build Coastguard Worker */
setup(void)793*6a54128fSAndroid Build Coastguard Worker static void setup(void)
794*6a54128fSAndroid Build Coastguard Worker {
795*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
796*6a54128fSAndroid Build Coastguard Worker struct ext2_super_block param;
797*6a54128fSAndroid Build Coastguard Worker
798*6a54128fSAndroid Build Coastguard Worker initialize_ext2_error_table();
799*6a54128fSAndroid Build Coastguard Worker
800*6a54128fSAndroid Build Coastguard Worker memset(¶m, 0, sizeof(param));
801*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count_set(¶m, 12000);
802*6a54128fSAndroid Build Coastguard Worker
803*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
804*6a54128fSAndroid Build Coastguard Worker test_io_manager, &test_fs);
805*6a54128fSAndroid Build Coastguard Worker if (retval) {
806*6a54128fSAndroid Build Coastguard Worker com_err("setup", retval,
807*6a54128fSAndroid Build Coastguard Worker "while initializing filesystem");
808*6a54128fSAndroid Build Coastguard Worker exit(1);
809*6a54128fSAndroid Build Coastguard Worker }
810*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_allocate_tables(test_fs);
811*6a54128fSAndroid Build Coastguard Worker if (retval) {
812*6a54128fSAndroid Build Coastguard Worker com_err("setup", retval,
813*6a54128fSAndroid Build Coastguard Worker "while allocating tables for test filesystem");
814*6a54128fSAndroid Build Coastguard Worker exit(1);
815*6a54128fSAndroid Build Coastguard Worker }
816*6a54128fSAndroid Build Coastguard Worker }
817*6a54128fSAndroid Build Coastguard Worker
run_test(int flags,int size,char * dir,struct test_program * prog)818*6a54128fSAndroid Build Coastguard Worker int run_test(int flags, int size, char *dir, struct test_program *prog)
819*6a54128fSAndroid Build Coastguard Worker {
820*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
821*6a54128fSAndroid Build Coastguard Worker ext2_icount_t icount;
822*6a54128fSAndroid Build Coastguard Worker struct test_program *pc;
823*6a54128fSAndroid Build Coastguard Worker __u16 result;
824*6a54128fSAndroid Build Coastguard Worker int problem = 0;
825*6a54128fSAndroid Build Coastguard Worker
826*6a54128fSAndroid Build Coastguard Worker if (dir) {
827*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
828*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_icount_tdb(test_fs, dir,
829*6a54128fSAndroid Build Coastguard Worker flags, &icount);
830*6a54128fSAndroid Build Coastguard Worker if (retval) {
831*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval,
832*6a54128fSAndroid Build Coastguard Worker "while creating icount using tdb");
833*6a54128fSAndroid Build Coastguard Worker exit(1);
834*6a54128fSAndroid Build Coastguard Worker }
835*6a54128fSAndroid Build Coastguard Worker #else
836*6a54128fSAndroid Build Coastguard Worker printf("Skipped\n");
837*6a54128fSAndroid Build Coastguard Worker return 0;
838*6a54128fSAndroid Build Coastguard Worker #endif
839*6a54128fSAndroid Build Coastguard Worker } else {
840*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_icount2(test_fs, flags, size, 0,
841*6a54128fSAndroid Build Coastguard Worker &icount);
842*6a54128fSAndroid Build Coastguard Worker if (retval) {
843*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval, "while creating icount");
844*6a54128fSAndroid Build Coastguard Worker exit(1);
845*6a54128fSAndroid Build Coastguard Worker }
846*6a54128fSAndroid Build Coastguard Worker }
847*6a54128fSAndroid Build Coastguard Worker for (pc = prog; pc->cmd != EXIT; pc++) {
848*6a54128fSAndroid Build Coastguard Worker switch (pc->cmd) {
849*6a54128fSAndroid Build Coastguard Worker case FETCH:
850*6a54128fSAndroid Build Coastguard Worker printf("icount_fetch(%u) = ", pc->ino);
851*6a54128fSAndroid Build Coastguard Worker break;
852*6a54128fSAndroid Build Coastguard Worker case STORE:
853*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
854*6a54128fSAndroid Build Coastguard Worker if (retval) {
855*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval,
856*6a54128fSAndroid Build Coastguard Worker "while calling icount_store");
857*6a54128fSAndroid Build Coastguard Worker exit(1);
858*6a54128fSAndroid Build Coastguard Worker }
859*6a54128fSAndroid Build Coastguard Worker printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
860*6a54128fSAndroid Build Coastguard Worker break;
861*6a54128fSAndroid Build Coastguard Worker case INCREMENT:
862*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_icount_increment(icount, pc->ino, 0);
863*6a54128fSAndroid Build Coastguard Worker if (retval) {
864*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval,
865*6a54128fSAndroid Build Coastguard Worker "while calling icount_increment");
866*6a54128fSAndroid Build Coastguard Worker exit(1);
867*6a54128fSAndroid Build Coastguard Worker }
868*6a54128fSAndroid Build Coastguard Worker printf("icount_increment(%u) = ", pc->ino);
869*6a54128fSAndroid Build Coastguard Worker break;
870*6a54128fSAndroid Build Coastguard Worker case DECREMENT:
871*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_icount_decrement(icount, pc->ino, 0);
872*6a54128fSAndroid Build Coastguard Worker if (retval) {
873*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval,
874*6a54128fSAndroid Build Coastguard Worker "while calling icount_decrement");
875*6a54128fSAndroid Build Coastguard Worker exit(1);
876*6a54128fSAndroid Build Coastguard Worker }
877*6a54128fSAndroid Build Coastguard Worker printf("icount_decrement(%u) = ", pc->ino);
878*6a54128fSAndroid Build Coastguard Worker break;
879*6a54128fSAndroid Build Coastguard Worker }
880*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_icount_fetch(icount, pc->ino, &result);
881*6a54128fSAndroid Build Coastguard Worker if (retval) {
882*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval,
883*6a54128fSAndroid Build Coastguard Worker "while calling icount_fetch");
884*6a54128fSAndroid Build Coastguard Worker exit(1);
885*6a54128fSAndroid Build Coastguard Worker }
886*6a54128fSAndroid Build Coastguard Worker printf("%u (%s)\n", result, (result == pc->expected) ?
887*6a54128fSAndroid Build Coastguard Worker "OK" : "NOT OK");
888*6a54128fSAndroid Build Coastguard Worker if (result != pc->expected)
889*6a54128fSAndroid Build Coastguard Worker problem++;
890*6a54128fSAndroid Build Coastguard Worker }
891*6a54128fSAndroid Build Coastguard Worker printf("icount size is %u\n", ext2fs_get_icount_size(icount));
892*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_icount_validate(icount, stdout);
893*6a54128fSAndroid Build Coastguard Worker if (retval) {
894*6a54128fSAndroid Build Coastguard Worker com_err("run_test", retval, "while calling icount_validate");
895*6a54128fSAndroid Build Coastguard Worker exit(1);
896*6a54128fSAndroid Build Coastguard Worker }
897*6a54128fSAndroid Build Coastguard Worker ext2fs_free_icount(icount);
898*6a54128fSAndroid Build Coastguard Worker return problem;
899*6a54128fSAndroid Build Coastguard Worker }
900*6a54128fSAndroid Build Coastguard Worker
901*6a54128fSAndroid Build Coastguard Worker
main(int argc,char ** argv)902*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
903*6a54128fSAndroid Build Coastguard Worker {
904*6a54128fSAndroid Build Coastguard Worker int failed = 0;
905*6a54128fSAndroid Build Coastguard Worker
906*6a54128fSAndroid Build Coastguard Worker setup();
907*6a54128fSAndroid Build Coastguard Worker printf("Standard icount run:\n");
908*6a54128fSAndroid Build Coastguard Worker failed += run_test(0, 0, 0, prog);
909*6a54128fSAndroid Build Coastguard Worker printf("\nMultiple bitmap test:\n");
910*6a54128fSAndroid Build Coastguard Worker failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
911*6a54128fSAndroid Build Coastguard Worker printf("\nResizing icount:\n");
912*6a54128fSAndroid Build Coastguard Worker failed += run_test(0, 3, 0, extended);
913*6a54128fSAndroid Build Coastguard Worker printf("\nStandard icount run with tdb:\n");
914*6a54128fSAndroid Build Coastguard Worker failed += run_test(0, 0, ".", prog);
915*6a54128fSAndroid Build Coastguard Worker printf("\nMultiple bitmap test with tdb:\n");
916*6a54128fSAndroid Build Coastguard Worker failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);
917*6a54128fSAndroid Build Coastguard Worker if (failed)
918*6a54128fSAndroid Build Coastguard Worker printf("FAILED!\n");
919*6a54128fSAndroid Build Coastguard Worker return failed;
920*6a54128fSAndroid Build Coastguard Worker }
921*6a54128fSAndroid Build Coastguard Worker #endif
922