• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dblist.c -- directory block list functions
3  *
4  * Copyright 1997 by Theodore Ts'o
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <string.h>
17 #include <time.h>
18 
19 #include "ext2_fs.h"
20 #include "ext2fsP.h"
21 
22 static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b);
23 static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b);
24 static EXT2_QSORT_TYPE (*sortfunc32)(const void *a, const void *b);
25 
26 /*
27  * Returns the number of directories in the filesystem as reported by
28  * the group descriptors.  Of course, the group descriptors could be
29  * wrong!
30  */
ext2fs_get_num_dirs(ext2_filsys fs,ext2_ino_t * ret_num_dirs)31 errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs)
32 {
33 	dgrp_t	i;
34 	ext2_ino_t	num_dirs, max_dirs;
35 
36 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
37 
38 	num_dirs = 0;
39 	max_dirs = fs->super->s_inodes_per_group;
40 	for (i = 0; i < fs->group_desc_count; i++) {
41 		if (ext2fs_bg_used_dirs_count(fs, i) > max_dirs)
42 			num_dirs += max_dirs / 8;
43 		else
44 			num_dirs += ext2fs_bg_used_dirs_count(fs, i);
45 	}
46 	if (num_dirs > fs->super->s_inodes_count)
47 		num_dirs = fs->super->s_inodes_count;
48 
49 	*ret_num_dirs = num_dirs;
50 
51 	return 0;
52 }
53 
54 /*
55  * helper function for making a new directory block list (for
56  * initialize and copy).
57  */
make_dblist(ext2_filsys fs,ext2_ino_t size,ext2_ino_t count,struct ext2_db_entry2 * list,ext2_dblist * ret_dblist)58 static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size,
59 			     ext2_ino_t count,
60 			     struct ext2_db_entry2 *list,
61 			     ext2_dblist *ret_dblist)
62 {
63 	ext2_dblist	dblist = NULL;
64 	errcode_t	retval;
65 	ext2_ino_t	num_dirs;
66 	size_t		len;
67 
68 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69 
70 	if ((ret_dblist == 0) && fs->dblist &&
71 	    (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
72 		return 0;
73 
74 	retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist);
75 	if (retval)
76 		goto cleanup;
77 	memset(dblist, 0, sizeof(struct ext2_struct_dblist));
78 
79 	dblist->magic = EXT2_ET_MAGIC_DBLIST;
80 	dblist->fs = fs;
81 	if (size)
82 		dblist->size = size;
83 	else {
84 		retval = ext2fs_get_num_dirs(fs, &num_dirs);
85 		if (retval)
86 			goto cleanup;
87 		dblist->size = (num_dirs * 2) + 12;
88 	}
89 	len = (size_t) sizeof(struct ext2_db_entry2) * dblist->size;
90 	dblist->count = count;
91 	retval = ext2fs_get_array(dblist->size, sizeof(struct ext2_db_entry2),
92 		&dblist->list);
93 	if (retval)
94 		goto cleanup;
95 
96 	if (list)
97 		memcpy(dblist->list, list, len);
98 	else
99 		memset(dblist->list, 0, len);
100 	if (ret_dblist)
101 		*ret_dblist = dblist;
102 	else
103 		fs->dblist = dblist;
104 	return 0;
105 cleanup:
106 	if (dblist)
107 		ext2fs_free_mem(&dblist);
108 	return retval;
109 }
110 
111 /*
112  * Initialize a directory block list
113  */
ext2fs_init_dblist(ext2_filsys fs,ext2_dblist * ret_dblist)114 errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
115 {
116 	ext2_dblist	dblist;
117 	errcode_t	retval;
118 
119 	retval = make_dblist(fs, 0, 0, 0, &dblist);
120 	if (retval)
121 		return retval;
122 
123 	dblist->sorted = 1;
124 	if (ret_dblist)
125 		*ret_dblist = dblist;
126 	else
127 		fs->dblist = dblist;
128 
129 	return 0;
130 }
131 
132 /*
133  * Copy a directory block list
134  */
ext2fs_copy_dblist(ext2_dblist src,ext2_dblist * dest)135 errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
136 {
137 	ext2_dblist	dblist;
138 	errcode_t	retval;
139 
140 	retval = make_dblist(src->fs, src->size, src->count, src->list,
141 			     &dblist);
142 	if (retval)
143 		return retval;
144 	dblist->sorted = src->sorted;
145 	*dest = dblist;
146 	return 0;
147 }
148 
149 /*
150  * Close a directory block list
151  *
152  * (moved to closefs.c)
153  */
154 
155 
156 /*
157  * Add a directory block to the directory block list
158  */
ext2fs_add_dir_block2(ext2_dblist dblist,ext2_ino_t ino,blk64_t blk,e2_blkcnt_t blockcnt)159 errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
160 				blk64_t blk, e2_blkcnt_t blockcnt)
161 {
162 	struct ext2_db_entry2 	*new_entry;
163 	errcode_t		retval;
164 	unsigned long		old_size;
165 
166 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
167 
168 	if (dblist->count >= dblist->size) {
169 		old_size = dblist->size * sizeof(struct ext2_db_entry2);
170 		dblist->size += dblist->size > 200 ? dblist->size / 2 : 100;
171 		retval = ext2fs_resize_mem(old_size, (size_t) dblist->size *
172 					   sizeof(struct ext2_db_entry2),
173 					   &dblist->list);
174 		if (retval) {
175 			dblist->size = old_size / sizeof(struct ext2_db_entry2);
176 			return retval;
177 		}
178 	}
179 	new_entry = dblist->list + ( dblist->count++);
180 	new_entry->blk = blk;
181 	new_entry->ino = ino;
182 	new_entry->blockcnt = blockcnt;
183 
184 	dblist->sorted = 0;
185 
186 	return 0;
187 }
188 
189 /*
190  * Change the directory block to the directory block list
191  */
ext2fs_set_dir_block2(ext2_dblist dblist,ext2_ino_t ino,blk64_t blk,e2_blkcnt_t blockcnt)192 errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
193 				blk64_t blk, e2_blkcnt_t blockcnt)
194 {
195 	dgrp_t			i;
196 
197 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
198 
199 	for (i=0; i < dblist->count; i++) {
200 		if ((dblist->list[i].ino != ino) ||
201 		    (dblist->list[i].blockcnt != blockcnt))
202 			continue;
203 		dblist->list[i].blk = blk;
204 		dblist->sorted = 0;
205 		return 0;
206 	}
207 	return EXT2_ET_DB_NOT_FOUND;
208 }
209 
ext2fs_dblist_sort2(ext2_dblist dblist,EXT2_QSORT_TYPE (* sortfunc)(const void *,const void *))210 void ext2fs_dblist_sort2(ext2_dblist dblist,
211 			 EXT2_QSORT_TYPE (*sortfunc)(const void *,
212 						     const void *))
213 {
214 	if (!sortfunc)
215 		sortfunc = dir_block_cmp2;
216 	qsort(dblist->list, (size_t) dblist->count,
217 	      sizeof(struct ext2_db_entry2), sortfunc);
218 	dblist->sorted = 1;
219 }
220 
221 /*
222  * This function iterates over the directory block list
223  */
ext2fs_dblist_iterate2(ext2_dblist dblist,int (* func)(ext2_filsys fs,struct ext2_db_entry2 * db_info,void * priv_data),void * priv_data)224 errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist,
225 				 int (*func)(ext2_filsys fs,
226 					     struct ext2_db_entry2 *db_info,
227 					     void	*priv_data),
228 				 void *priv_data)
229 {
230 	unsigned long long	i;
231 	int		ret;
232 
233 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
234 
235 	if (!dblist->sorted)
236 		ext2fs_dblist_sort2(dblist, 0);
237 	for (i=0; i < dblist->count; i++) {
238 		ret = (*func)(dblist->fs, &dblist->list[i], priv_data);
239 		if (ret & DBLIST_ABORT)
240 			return 0;
241 	}
242 	return 0;
243 }
244 
dir_block_cmp2(const void * a,const void * b)245 static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b)
246 {
247 	const struct ext2_db_entry2 *db_a =
248 		(const struct ext2_db_entry2 *) a;
249 	const struct ext2_db_entry2 *db_b =
250 		(const struct ext2_db_entry2 *) b;
251 
252 	if (db_a->blk != db_b->blk)
253 		return (int) (db_a->blk - db_b->blk);
254 
255 	if (db_a->ino != db_b->ino)
256 		return (int) (db_a->ino - db_b->ino);
257 
258 	return (db_a->blockcnt - db_b->blockcnt);
259 }
260 
ext2fs_dblist_count2(ext2_dblist dblist)261 blk64_t ext2fs_dblist_count2(ext2_dblist dblist)
262 {
263 	return dblist->count;
264 }
265 
ext2fs_dblist_get_last2(ext2_dblist dblist,struct ext2_db_entry2 ** entry)266 errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist,
267 				  struct ext2_db_entry2 **entry)
268 {
269 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
270 
271 	if (dblist->count == 0)
272 		return EXT2_ET_DBLIST_EMPTY;
273 
274 	if (entry)
275 		*entry = dblist->list + ( dblist->count-1);
276 	return 0;
277 }
278 
ext2fs_dblist_drop_last(ext2_dblist dblist)279 errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist)
280 {
281 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
282 
283 	if (dblist->count == 0)
284 		return EXT2_ET_DBLIST_EMPTY;
285 
286 	dblist->count--;
287 	return 0;
288 }
289 
290 /*
291  * Legacy 32-bit versions
292  */
293 
294 /*
295  * Add a directory block to the directory block list
296  */
ext2fs_add_dir_block(ext2_dblist dblist,ext2_ino_t ino,blk_t blk,int blockcnt)297 errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
298 			       int blockcnt)
299 {
300 	return ext2fs_add_dir_block2(dblist, ino, blk, blockcnt);
301 }
302 
303 /*
304  * Change the directory block to the directory block list
305  */
ext2fs_set_dir_block(ext2_dblist dblist,ext2_ino_t ino,blk_t blk,int blockcnt)306 errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
307 			       int blockcnt)
308 {
309 	return ext2fs_set_dir_block2(dblist, ino, blk, blockcnt);
310 }
311 
ext2fs_dblist_sort(ext2_dblist dblist,EXT2_QSORT_TYPE (* sortfunc)(const void *,const void *))312 void ext2fs_dblist_sort(ext2_dblist dblist,
313 			EXT2_QSORT_TYPE (*sortfunc)(const void *,
314 						    const void *))
315 {
316 	if (sortfunc) {
317 		sortfunc32 = sortfunc;
318 		sortfunc = dir_block_cmp;
319 	} else
320 		sortfunc = dir_block_cmp2;
321 	qsort(dblist->list, (size_t) dblist->count,
322 	      sizeof(struct ext2_db_entry2), sortfunc);
323 	dblist->sorted = 1;
324 }
325 
326 /*
327  * This function iterates over the directory block list
328  */
329 struct iterate_passthrough {
330 	int (*func)(ext2_filsys fs,
331 		    struct ext2_db_entry *db_info,
332 		    void	*priv_data);
333 	void *priv_data;
334 };
335 
passthrough_func(ext2_filsys fs,struct ext2_db_entry2 * db_info,void * priv_data)336 static int passthrough_func(ext2_filsys fs,
337 			    struct ext2_db_entry2 *db_info,
338 			    void	*priv_data)
339 {
340 	struct iterate_passthrough *p = priv_data;
341 	struct ext2_db_entry db;
342 	int ret;
343 
344 	db.ino = db_info->ino;
345 	db.blk = (blk_t) db_info->blk;
346 	db.blockcnt = (int) db_info->blockcnt;
347 	ret = (p->func)(fs, &db, p->priv_data);
348 	db_info->ino = db.ino;
349 	db_info->blk = db.blk;
350 	db_info->blockcnt = db.blockcnt;
351 	return ret;
352 }
353 
ext2fs_dblist_iterate(ext2_dblist dblist,int (* func)(ext2_filsys fs,struct ext2_db_entry * db_info,void * priv_data),void * priv_data)354 errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
355 				int (*func)(ext2_filsys fs,
356 					    struct ext2_db_entry *db_info,
357 					    void	*priv_data),
358 				void *priv_data)
359 {
360 	struct iterate_passthrough pass;
361 
362 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
363 	pass.func = func;
364 	pass.priv_data = priv_data;
365 
366 	return ext2fs_dblist_iterate2(dblist, passthrough_func, &pass);
367 }
368 
dir_block_cmp(const void * a,const void * b)369 static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b)
370 {
371 	const struct ext2_db_entry2 *db_a =
372 		(const struct ext2_db_entry2 *) a;
373 	const struct ext2_db_entry2 *db_b =
374 		(const struct ext2_db_entry2 *) b;
375 
376 	struct ext2_db_entry a32, b32;
377 
378 	a32.ino = db_a->ino;  a32.blk = db_a->blk;
379 	a32.blockcnt = db_a->blockcnt;
380 
381 	b32.ino = db_b->ino;  b32.blk = db_b->blk;
382 	b32.blockcnt = db_b->blockcnt;
383 
384 	return sortfunc32(&a32, &b32);
385 }
386 
ext2fs_dblist_count(ext2_dblist dblist)387 int ext2fs_dblist_count(ext2_dblist dblist)
388 {
389 	return dblist->count;
390 }
391 
ext2fs_dblist_get_last(ext2_dblist dblist,struct ext2_db_entry ** entry)392 errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
393 				 struct ext2_db_entry **entry)
394 {
395 	static struct ext2_db_entry ret_entry;
396 	struct ext2_db_entry2 *last;
397 
398 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
399 
400 	if (dblist->count == 0)
401 		return EXT2_ET_DBLIST_EMPTY;
402 
403 	if (!entry)
404 		return 0;
405 
406 	last = dblist->list + dblist->count -1;
407 
408 	ret_entry.ino = last->ino;
409 	ret_entry.blk = last->blk;
410 	ret_entry.blockcnt = last->blockcnt;
411 	*entry = &ret_entry;
412 
413 	return 0;
414 }
415 
416