blob: 4cb7ca18927653831b7cca73ea130f8c14736e7f [file] [log] [blame]
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001/*
2 * dblist.c -- directory block list functions
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright 1997 by Theodore Ts'o
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00009 * %End-Header%
Theodore Ts'o21c84b71997-04-29 16:15:03 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000013#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o21c84b71997-04-29 16:15:03 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000017#include <string.h>
18#include <time.h>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000019
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000020#include "ext2_fs.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000021#include "ext2fsP.h"
22
Theodore Ts'o4c77fe51998-04-30 17:35:59 +000023static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b);
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040024static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b);
25static EXT2_QSORT_TYPE (*sortfunc32)(const void *a, const void *b);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000026
27/*
28 * Returns the number of directories in the filesystem as reported by
29 * the group descriptors. Of course, the group descriptors could be
30 * wrong!
31 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000032errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs)
Theodore Ts'o21c84b71997-04-29 16:15:03 +000033{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +000034 dgrp_t i;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000035 ext2_ino_t num_dirs, max_dirs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000036
37 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040038
Theodore Ts'o21c84b71997-04-29 16:15:03 +000039 num_dirs = 0;
Theodore Ts'o79b05db1998-02-21 05:01:19 +000040 max_dirs = fs->super->s_inodes_per_group;
Theodore Ts'o5be8dc21997-12-01 18:24:10 +000041 for (i = 0; i < fs->group_desc_count; i++) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040042 if (ext2fs_bg_used_dirs_count(fs, i) > max_dirs)
Theodore Ts'o79b05db1998-02-21 05:01:19 +000043 num_dirs += max_dirs / 8;
Theodore Ts'o5be8dc21997-12-01 18:24:10 +000044 else
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040045 num_dirs += ext2fs_bg_used_dirs_count(fs, i);
Theodore Ts'o5be8dc21997-12-01 18:24:10 +000046 }
47 if (num_dirs > fs->super->s_inodes_count)
48 num_dirs = fs->super->s_inodes_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000049
50 *ret_num_dirs = num_dirs;
51
52 return 0;
53}
54
55/*
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000056 * helper function for making a new directory block list (for
57 * initialize and copy).
Theodore Ts'o21c84b71997-04-29 16:15:03 +000058 */
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040059static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size,
60 ext2_ino_t count,
61 struct ext2_db_entry2 *list,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000062 ext2_dblist *ret_dblist)
Theodore Ts'o21c84b71997-04-29 16:15:03 +000063{
Andreas Dilger00eb0ee2012-11-29 05:47:53 -070064 ext2_dblist dblist = NULL;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000065 errcode_t retval;
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040066 ext2_ino_t num_dirs;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000067 size_t len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068
69 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70
71 if ((ret_dblist == 0) && fs->dblist &&
72 (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
73 return 0;
74
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040075 retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000076 if (retval)
Eric Sandeen624e8eb2011-09-16 15:49:28 -050077 goto cleanup;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000078 memset(dblist, 0, sizeof(struct ext2_struct_dblist));
79
80 dblist->magic = EXT2_ET_MAGIC_DBLIST;
81 dblist->fs = fs;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000082 if (size)
83 dblist->size = size;
84 else {
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040085 retval = ext2fs_get_num_dirs(fs, &num_dirs);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000086 if (retval)
87 goto cleanup;
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040088 dblist->size = (num_dirs * 2) + 12;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000089 }
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040090 len = (size_t) sizeof(struct ext2_db_entry2) * dblist->size;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000091 dblist->count = count;
Theodore Ts'oab13b5a2010-06-13 10:00:00 -040092 retval = ext2fs_get_array(dblist->size, sizeof(struct ext2_db_entry2),
Theodore Ts'oee010792007-11-09 19:01:06 -050093 &dblist->list);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000094 if (retval)
Theodore Ts'o21c84b71997-04-29 16:15:03 +000095 goto cleanup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040096
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000097 if (list)
98 memcpy(dblist->list, list, len);
99 else
100 memset(dblist->list, 0, len);
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000101 if (ret_dblist)
102 *ret_dblist = dblist;
103 else
104 fs->dblist = dblist;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000105 return 0;
106cleanup:
107 if (dblist)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400108 ext2fs_free_mem(&dblist);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000109 return retval;
110}
111
112/*
113 * Initialize a directory block list
114 */
115errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
116{
117 ext2_dblist dblist;
118 errcode_t retval;
119
120 retval = make_dblist(fs, 0, 0, 0, &dblist);
121 if (retval)
122 return retval;
123
124 dblist->sorted = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000125 if (ret_dblist)
126 *ret_dblist = dblist;
127 else
128 fs->dblist = dblist;
129
130 return 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000131}
132
133/*
134 * Copy a directory block list
135 */
136errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
137{
138 ext2_dblist dblist;
139 errcode_t retval;
140
141 retval = make_dblist(src->fs, src->size, src->count, src->list,
142 &dblist);
143 if (retval)
144 return retval;
145 dblist->sorted = src->sorted;
146 *dest = dblist;
147 return 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000148}
149
150/*
151 * Close a directory block list
152 *
153 * (moved to closefs.c)
154 */
155
156
157/*
158 * Add a directory block to the directory block list
159 */
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400160errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
161 blk64_t blk, e2_blkcnt_t blockcnt)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162{
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400163 struct ext2_db_entry2 *new_entry;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000164 errcode_t retval;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000165 unsigned long old_size;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400166
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000167 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
168
169 if (dblist->count >= dblist->size) {
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400170 old_size = dblist->size * sizeof(struct ext2_db_entry2);
Andreas Dilger9f9e5c32008-08-23 21:17:17 -0600171 dblist->size += dblist->size > 200 ? dblist->size / 2 : 100;
Theodore Ts'o76f875d1998-04-27 01:41:13 +0000172 retval = ext2fs_resize_mem(old_size, (size_t) dblist->size *
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400173 sizeof(struct ext2_db_entry2),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400174 &dblist->list);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000175 if (retval) {
Theodore Ts'o8f36e692011-06-11 12:54:50 -0400176 dblist->size = old_size / sizeof(struct ext2_db_entry2);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000177 return retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000178 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000179 }
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400180 new_entry = dblist->list + ( dblist->count++);
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +0000181 new_entry->blk = blk;
182 new_entry->ino = ino;
183 new_entry->blockcnt = blockcnt;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000184
185 dblist->sorted = 0;
186
187 return 0;
188}
189
190/*
Theodore Ts'o521e3681997-04-29 17:48:10 +0000191 * Change the directory block to the directory block list
192 */
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400193errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
194 blk64_t blk, e2_blkcnt_t blockcnt)
Theodore Ts'o521e3681997-04-29 17:48:10 +0000195{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000196 dgrp_t i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400197
Theodore Ts'o521e3681997-04-29 17:48:10 +0000198 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
199
200 for (i=0; i < dblist->count; i++) {
201 if ((dblist->list[i].ino != ino) ||
202 (dblist->list[i].blockcnt != blockcnt))
203 continue;
204 dblist->list[i].blk = blk;
205 dblist->sorted = 0;
206 return 0;
207 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000208 return EXT2_ET_DB_NOT_FOUND;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000209}
210
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400211void ext2fs_dblist_sort2(ext2_dblist dblist,
212 EXT2_QSORT_TYPE (*sortfunc)(const void *,
213 const void *))
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400214{
215 if (!sortfunc)
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400216 sortfunc = dir_block_cmp2;
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400217 qsort(dblist->list, (size_t) dblist->count,
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400218 sizeof(struct ext2_db_entry2), sortfunc);
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400219 dblist->sorted = 1;
220}
221
Theodore Ts'o521e3681997-04-29 17:48:10 +0000222/*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000223 * This function iterates over the directory block list
224 */
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400225errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist,
226 int (*func)(ext2_filsys fs,
227 struct ext2_db_entry2 *db_info,
228 void *priv_data),
229 void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000230{
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400231 unsigned long long i;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000232 int ret;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400233
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000234 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
235
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400236 if (!dblist->sorted)
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400237 ext2fs_dblist_sort2(dblist, 0);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000238 for (i=0; i < dblist->count; i++) {
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400239 ret = (*func)(dblist->fs, &dblist->list[i], priv_data);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000240 if (ret & DBLIST_ABORT)
241 return 0;
242 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000243 return 0;
244}
245
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400246static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000247{
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400248 const struct ext2_db_entry2 *db_a =
249 (const struct ext2_db_entry2 *) a;
250 const struct ext2_db_entry2 *db_b =
251 (const struct ext2_db_entry2 *) b;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000252
253 if (db_a->blk != db_b->blk)
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000254 return (int) (db_a->blk - db_b->blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400255
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000256 if (db_a->ino != db_b->ino)
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000257 return (int) (db_a->ino - db_b->ino);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000258
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400259 return (db_a->blockcnt - db_b->blockcnt);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000260}
261
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400262blk64_t ext2fs_dblist_count2(ext2_dblist dblist)
Theodore Ts'o549860c1997-06-17 03:55:00 +0000263{
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400264 return dblist->count;
Theodore Ts'o549860c1997-06-17 03:55:00 +0000265}
Theodore Ts'o52b1dd52008-03-13 00:34:16 -0400266
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400267errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist,
268 struct ext2_db_entry2 **entry)
Theodore Ts'o52b1dd52008-03-13 00:34:16 -0400269{
Theodore Ts'o52b1dd52008-03-13 00:34:16 -0400270 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
271
272 if (dblist->count == 0)
273 return EXT2_ET_DBLIST_EMPTY;
274
275 if (entry)
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400276 *entry = dblist->list + ( dblist->count-1);
Theodore Ts'o52b1dd52008-03-13 00:34:16 -0400277 return 0;
278}
279
280errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400281{
Theodore Ts'o52b1dd52008-03-13 00:34:16 -0400282 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
283
284 if (dblist->count == 0)
285 return EXT2_ET_DBLIST_EMPTY;
286
287 dblist->count--;
288 return 0;
289}
Theodore Ts'oab13b5a2010-06-13 10:00:00 -0400290
291/*
292 * Legacy 32-bit versions
293 */
294
295/*
296 * Add a directory block to the directory block list
297 */
298errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
299 int blockcnt)
300{
301 return ext2fs_add_dir_block2(dblist, ino, blk, blockcnt);
302}
303
304/*
305 * Change the directory block to the directory block list
306 */
307errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
308 int blockcnt)
309{
310 return ext2fs_set_dir_block2(dblist, ino, blk, blockcnt);
311}
312
313void ext2fs_dblist_sort(ext2_dblist dblist,
314 EXT2_QSORT_TYPE (*sortfunc)(const void *,
315 const void *))
316{
317 if (sortfunc) {
318 sortfunc32 = sortfunc;
319 sortfunc = dir_block_cmp;
320 } else
321 sortfunc = dir_block_cmp2;
322 qsort(dblist->list, (size_t) dblist->count,
323 sizeof(struct ext2_db_entry2), sortfunc);
324 dblist->sorted = 1;
325}
326
327/*
328 * This function iterates over the directory block list
329 */
330struct iterate_passthrough {
331 int (*func)(ext2_filsys fs,
332 struct ext2_db_entry *db_info,
333 void *priv_data);
334 void *priv_data;
335};
336
337static int passthrough_func(ext2_filsys fs,
338 struct ext2_db_entry2 *db_info,
339 void *priv_data)
340{
341 struct iterate_passthrough *p = priv_data;
342 struct ext2_db_entry db;
343 int ret;
344
345 db.ino = db_info->ino;
346 db.blk = (blk_t) db_info->blk;
347 db.blockcnt = (int) db_info->blockcnt;
348 ret = (p->func)(fs, &db, p->priv_data);
349 db_info->ino = db.ino;
350 db_info->blk = db.blk;
351 db_info->blockcnt = db.blockcnt;
352 return ret;
353}
354
355errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
356 int (*func)(ext2_filsys fs,
357 struct ext2_db_entry *db_info,
358 void *priv_data),
359 void *priv_data)
360{
361 struct iterate_passthrough pass;
362
363 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
364 pass.func = func;
365 pass.priv_data = priv_data;
366
367 return ext2fs_dblist_iterate2(dblist, passthrough_func, &pass);
368}
369
370static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b)
371{
372 const struct ext2_db_entry2 *db_a =
373 (const struct ext2_db_entry2 *) a;
374 const struct ext2_db_entry2 *db_b =
375 (const struct ext2_db_entry2 *) b;
376
377 struct ext2_db_entry a32, b32;
378
379 a32.ino = db_a->ino; a32.blk = db_a->blk;
380 a32.blockcnt = db_a->blockcnt;
381
382 b32.ino = db_b->ino; b32.blk = db_b->blk;
383 b32.blockcnt = db_b->blockcnt;
384
385 return sortfunc32(&a32, &b32);
386}
387
388int ext2fs_dblist_count(ext2_dblist dblist)
389{
390 return dblist->count;
391}
392
393errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
394 struct ext2_db_entry **entry)
395{
396 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
397 static struct ext2_db_entry ret_entry;
398 struct ext2_db_entry2 *last;
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