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