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