Whamcloud - gitweb
e2fsck: regression tests for INCOMPAT_MMP feature
[tools/e2fsprogs.git] / e2fsck / dirinfo.c
1 /*
2  * dirinfo.c --- maintains the directory information table for e2fsck.
3  *
4  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #undef DIRINFO_DEBUG
9
10 #include "config.h"
11 #include "e2fsck.h"
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include "uuid/uuid.h"
15
16 #include <ext2fs/tdb.h>
17
18 struct dir_info_db {
19         int             count;
20         int             size;
21         struct dir_info *array;
22         struct dir_info *last_lookup;
23         char            *tdb_fn;
24         TDB_CONTEXT     *tdb;
25 };
26
27 struct dir_info_iter {
28         int     i;
29         TDB_DATA        tdb_iter;
30 };
31
32 struct dir_info_ent {
33         ext2_ino_t              dotdot; /* Parent according to '..' */
34         ext2_ino_t              parent; /* Parent according to treewalk */
35 };
36
37
38 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
39
40 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
41 {
42         struct dir_info_db      *db = ctx->dir_info;
43         unsigned int            threshold;
44         errcode_t               retval;
45         char                    *tdb_dir, uuid[40];
46         int                     fd, enable;
47
48         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
49                            &tdb_dir);
50         profile_get_uint(ctx->profile, "scratch_files",
51                          "numdirs_threshold", 0, 0, &threshold);
52         profile_get_boolean(ctx->profile, "scratch_files",
53                             "dirinfo", 0, 1, &enable);
54
55         if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
56             (threshold && num_dirs <= threshold))
57                 return;
58
59         retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
60         if (retval)
61                 return;
62
63         uuid_unparse(ctx->fs->super->s_uuid, uuid);
64         sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
65         fd = mkstemp(db->tdb_fn);
66         if (fd < 0) {
67                 db->tdb = NULL;
68                 return;
69         }
70         db->tdb = tdb_open(db->tdb_fn, 0, TDB_CLEAR_IF_FIRST,
71                            O_RDWR | O_CREAT | O_TRUNC, 0600);
72         close(fd);
73 }
74
75 static void setup_db(e2fsck_t ctx)
76 {
77         struct dir_info_db      *db;
78         ext2_ino_t              num_dirs;
79         errcode_t               retval;
80
81         db = (struct dir_info_db *)
82                 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
83                                        "directory map db");
84         db->count = db->size = 0;
85         db->array = 0;
86
87         ctx->dir_info = db;
88
89         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
90         if (retval)
91                 num_dirs = 1024;        /* Guess */
92
93         setup_tdb(ctx, num_dirs);
94
95         if (db->tdb) {
96 #ifdef DIRINFO_DEBUG
97                 printf("Note: using tdb!\n");
98 #endif
99                 return;
100         }
101
102         db->size = num_dirs + 10;
103         db->array  = (struct dir_info *)
104                 e2fsck_allocate_memory(ctx, db->size
105                                        * sizeof (struct dir_info),
106                                        "directory map");
107 }
108
109 /*
110  * This subroutine is called during pass1 to create a directory info
111  * entry.  During pass1, the passed-in parent is 0; it will get filled
112  * in during pass2.
113  */
114 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
115 {
116         struct dir_info_db      *db;
117         struct dir_info         *dir, ent;
118         int                     i, j;
119         errcode_t               retval;
120         unsigned long           old_size;
121
122 #ifdef DIRINFO_DEBUG
123         printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
124 #endif
125         if (!ctx->dir_info)
126                 setup_db(ctx);
127         db = ctx->dir_info;
128
129         if (ctx->dir_info->count >= ctx->dir_info->size) {
130                 old_size = ctx->dir_info->size * sizeof(struct dir_info);
131                 ctx->dir_info->size += 10;
132                 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
133                                            sizeof(struct dir_info),
134                                            &ctx->dir_info->array);
135                 if (retval) {
136                         ctx->dir_info->size -= 10;
137                         return;
138                 }
139         }
140
141         ent.ino = ino;
142         ent.parent = parent;
143         ent.dotdot = parent;
144
145         if (db->tdb) {
146                 e2fsck_put_dir_info(ctx, &ent);
147                 return;
148         }
149
150         /*
151          * Normally, add_dir_info is called with each inode in
152          * sequential order; but once in a while (like when pass 3
153          * needs to recreate the root directory or lost+found
154          * directory) it is called out of order.  In those cases, we
155          * need to move the dir_info entries down to make room, since
156          * the dir_info array needs to be sorted by inode number for
157          * get_dir_info()'s sake.
158          */
159         if (ctx->dir_info->count &&
160             ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
161                 for (i = ctx->dir_info->count-1; i > 0; i--)
162                         if (ctx->dir_info->array[i-1].ino < ino)
163                                 break;
164                 dir = &ctx->dir_info->array[i];
165                 if (dir->ino != ino)
166                         for (j = ctx->dir_info->count++; j > i; j--)
167                                 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
168         } else
169                 dir = &ctx->dir_info->array[ctx->dir_info->count++];
170
171         dir->ino = ino;
172         dir->dotdot = parent;
173         dir->parent = parent;
174 }
175
176 /*
177  * get_dir_info() --- given an inode number, try to find the directory
178  * information entry for it.
179  */
180 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
181 {
182         struct dir_info_db      *db = ctx->dir_info;
183         int                     low, high, mid;
184         struct dir_info_ent     *buf;
185         static struct dir_info  ret_dir_info;
186
187         if (!db)
188                 return 0;
189
190 #ifdef DIRINFO_DEBUG
191         printf("e2fsck_get_dir_info %d...", ino);
192 #endif
193
194         if (db->tdb) {
195                 TDB_DATA key, data;
196
197                 key.dptr = (unsigned char *) &ino;
198                 key.dsize = sizeof(ext2_ino_t);
199
200                 data = tdb_fetch(db->tdb, key);
201                 if (!data.dptr) {
202                         if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
203                                 printf("fetch failed: %s\n",
204                                        tdb_errorstr(db->tdb));
205                         return 0;
206                 }
207
208                 buf = (struct dir_info_ent *) data.dptr;
209                 ret_dir_info.ino = ino;
210                 ret_dir_info.dotdot = buf->dotdot;
211                 ret_dir_info.parent = buf->parent;
212 #ifdef DIRINFO_DEBUG
213                 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
214 #endif
215                 free(data.dptr);
216                 return &ret_dir_info;
217         }
218
219         if (db->last_lookup && db->last_lookup->ino == ino)
220                 return db->last_lookup;
221
222         low = 0;
223         high = ctx->dir_info->count-1;
224         if (ino == ctx->dir_info->array[low].ino) {
225 #ifdef DIRINFO_DEBUG
226                 printf("(%d,%d,%d)\n", ino,
227                        ctx->dir_info->array[low].dotdot,
228                        ctx->dir_info->array[low].parent);
229 #endif
230                 return &ctx->dir_info->array[low];
231         }
232         if (ino == ctx->dir_info->array[high].ino) {
233 #ifdef DIRINFO_DEBUG
234                 printf("(%d,%d,%d)\n", ino,
235                        ctx->dir_info->array[high].dotdot,
236                        ctx->dir_info->array[high].parent);
237 #endif
238                 return &ctx->dir_info->array[high];
239         }
240
241         while (low < high) {
242                 mid = (low+high)/2;
243                 if (mid == low || mid == high)
244                         break;
245                 if (ino == ctx->dir_info->array[mid].ino) {
246 #ifdef DIRINFO_DEBUG
247                         printf("(%d,%d,%d)\n", ino,
248                                ctx->dir_info->array[mid].dotdot,
249                                ctx->dir_info->array[mid].parent);
250 #endif
251                         return &ctx->dir_info->array[mid];
252                 }
253                 if (ino < ctx->dir_info->array[mid].ino)
254                         high = mid;
255                 else
256                         low = mid;
257         }
258         return 0;
259 }
260
261 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
262 {
263         struct dir_info_db      *db = ctx->dir_info;
264         struct dir_info_ent     buf;
265         TDB_DATA                key, data;
266
267 #ifdef DIRINFO_DEBUG
268         printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
269                dir->parent);
270 #endif
271
272         if (!db->tdb)
273                 return;
274
275         buf.parent = dir->parent;
276         buf.dotdot = dir->dotdot;
277
278         key.dptr = (unsigned char *) &dir->ino;
279         key.dsize = sizeof(ext2_ino_t);
280         data.dptr = (unsigned char *) &buf;
281         data.dsize = sizeof(buf);
282
283         if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
284                 printf("store failed: %s\n", tdb_errorstr(db->tdb));
285         }
286         return;
287 }
288
289 /*
290  * Free the dir_info structure when it isn't needed any more.
291  */
292 void e2fsck_free_dir_info(e2fsck_t ctx)
293 {
294         if (ctx->dir_info) {
295                 if (ctx->dir_info->tdb)
296                         tdb_close(ctx->dir_info->tdb);
297                 if (ctx->dir_info->tdb_fn) {
298                         unlink(ctx->dir_info->tdb_fn);
299                         free(ctx->dir_info->tdb_fn);
300                 }
301                 if (ctx->dir_info->array)
302                         ext2fs_free_mem(&ctx->dir_info->array);
303                 ctx->dir_info->array = 0;
304                 ctx->dir_info->size = 0;
305                 ctx->dir_info->count = 0;
306                 ext2fs_free_mem(&ctx->dir_info);
307                 ctx->dir_info = 0;
308         }
309 }
310
311 /*
312  * Return the count of number of directories in the dir_info structure
313  */
314 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
315 {
316         return ctx->dir_info ? ctx->dir_info->count : 0;
317 }
318
319 extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
320 {
321         struct dir_info_iter *iter;
322         struct dir_info_db *db = ctx->dir_info;
323
324         iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
325                                       "dir_info iterator");
326
327         if (db->tdb)
328                 iter->tdb_iter = tdb_firstkey(db->tdb);
329
330         return iter;
331 }
332
333 extern void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
334                                      struct dir_info_iter *iter)
335 {
336         free(iter->tdb_iter.dptr);
337         ext2fs_free_mem(&iter);
338 }
339
340 /*
341  * A simple interator function
342  */
343 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
344 {
345         TDB_DATA data, key;
346         struct dir_info_db *db = ctx->dir_info;
347         struct dir_info_ent *buf;
348         static struct dir_info ret_dir_info;
349
350         if (!ctx->dir_info || !iter)
351                 return 0;
352
353         if (db->tdb) {
354                 if (iter->tdb_iter.dptr == 0)
355                         return 0;
356                 key = iter->tdb_iter;
357                 data = tdb_fetch(db->tdb, key);
358                 if (!data.dptr) {
359                         printf("iter fetch failed: %s\n",
360                                tdb_errorstr(db->tdb));
361                         return 0;
362                 }
363                 buf = (struct dir_info_ent *) data.dptr;
364                 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
365                 ret_dir_info.dotdot = buf->dotdot;
366                 ret_dir_info.parent = buf->parent;
367                 iter->tdb_iter = tdb_nextkey(db->tdb, key);
368                 free(key.dptr);
369                 free(data.dptr);
370                 return &ret_dir_info;
371         }
372
373         if (iter->i >= ctx->dir_info->count)
374                 return 0;
375
376 #ifdef DIRINFO_DEBUG
377         printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
378                ctx->dir_info->array[iter->i].dotdot,
379                ctx->dir_info->array[iter->i].parent);
380 #endif
381         ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
382         return(ctx->dir_info->last_lookup);
383 }
384
385 /*
386  * This function only sets the parent pointer, and requires that
387  * dirinfo structure has already been created.
388  */
389 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
390                                ext2_ino_t parent)
391 {
392         struct dir_info *p;
393
394         p = e2fsck_get_dir_info(ctx, ino);
395         if (!p)
396                 return 1;
397         p->parent = parent;
398         e2fsck_put_dir_info(ctx, p);
399         return 0;
400 }
401
402 /*
403  * This function only sets the dot dot pointer, and requires that
404  * dirinfo structure has already been created.
405  */
406 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
407                                ext2_ino_t dotdot)
408 {
409         struct dir_info *p;
410
411         p = e2fsck_get_dir_info(ctx, ino);
412         if (!p)
413                 return 1;
414         p->dotdot = dotdot;
415         e2fsck_put_dir_info(ctx, p);
416         return 0;
417 }
418
419 /*
420  * This function only sets the parent pointer, and requires that
421  * dirinfo structure has already been created.
422  */
423 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
424                                ext2_ino_t *parent)
425 {
426         struct dir_info *p;
427
428         p = e2fsck_get_dir_info(ctx, ino);
429         if (!p)
430                 return 1;
431         *parent = p->parent;
432         return 0;
433 }
434
435 /*
436  * This function only sets the dot dot pointer, and requires that
437  * dirinfo structure has already been created.
438  */
439 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
440                                ext2_ino_t *dotdot)
441 {
442         struct dir_info *p;
443
444         p = e2fsck_get_dir_info(ctx, ino);
445         if (!p)
446                 return 1;
447         *dotdot = p->dotdot;
448         return 0;
449 }
450