Whamcloud - gitweb
Enable e2fsck to use the tdb library to store the dirinfo abstraction
[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                 return &ret_dir_info;
209         }
210
211         if (db->last_lookup && db->last_lookup->ino == ino)
212                 return db->last_lookup;
213
214         low = 0;
215         high = ctx->dir_info->count-1;
216         if (ino == ctx->dir_info->array[low].ino) {
217 #ifdef DIRINFO_DEBUG
218                 printf("(%d,%d,%d)\n", ino,
219                        ctx->dir_info->array[low].dotdot,
220                        ctx->dir_info->array[low].parent);
221 #endif
222                 return &ctx->dir_info->array[low];
223         }
224         if (ino == ctx->dir_info->array[high].ino) {
225 #ifdef DIRINFO_DEBUG
226                 printf("(%d,%d,%d)\n", ino,
227                        ctx->dir_info->array[high].dotdot,
228                        ctx->dir_info->array[high].parent);
229 #endif
230                 return &ctx->dir_info->array[high];
231         }
232
233         while (low < high) {
234                 mid = (low+high)/2;
235                 if (mid == low || mid == high)
236                         break;
237                 if (ino == ctx->dir_info->array[mid].ino) {
238 #ifdef DIRINFO_DEBUG
239                         printf("(%d,%d,%d)\n", ino,
240                                ctx->dir_info->array[mid].dotdot,
241                                ctx->dir_info->array[mid].parent);
242 #endif
243                         return &ctx->dir_info->array[mid];
244                 }
245                 if (ino < ctx->dir_info->array[mid].ino)
246                         high = mid;
247                 else
248                         low = mid;
249         }
250         return 0;
251 }
252
253 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
254 {
255         struct dir_info_db      *db = ctx->dir_info;
256         struct dir_info_ent     buf;
257         TDB_DATA                key, data;
258
259 #ifdef DIRINFO_DEBUG
260         printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
261                dir->parent);
262 #endif
263
264         if (!db->tdb)
265                 return;
266
267         buf.parent = dir->parent;
268         buf.dotdot = dir->dotdot;
269
270         key.dptr = (unsigned char *) &dir->ino;
271         key.dsize = sizeof(ext2_ino_t);
272         data.dptr = (unsigned char *) &buf;
273         data.dsize = sizeof(buf);
274
275         if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
276                 printf("store failed: %s\n", tdb_errorstr(db->tdb));
277         }
278         return;
279 }
280
281 /*
282  * Free the dir_info structure when it isn't needed any more.
283  */
284 void e2fsck_free_dir_info(e2fsck_t ctx)
285 {
286         if (ctx->dir_info) {
287                 if (ctx->dir_info->tdb)
288                         tdb_close(ctx->dir_info->tdb);
289                 if (ctx->dir_info->tdb_fn) {
290                         unlink(ctx->dir_info->tdb_fn);
291                         free(ctx->dir_info->tdb_fn);
292                 }
293                 ctx->dir_info->size = 0;
294                 ctx->dir_info->count = 0;
295                 ext2fs_free_mem(&ctx->dir_info);
296                 ctx->dir_info = 0;
297         }
298 }
299
300 /*
301  * Return the count of number of directories in the dir_info structure
302  */
303 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
304 {
305         return ctx->dir_info ? ctx->dir_info->count : 0;
306 }
307
308 extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
309 {
310         struct dir_info_iter *iter;
311         struct dir_info_db *db = ctx->dir_info;
312         int ret;
313
314         iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
315                                       "dir_info iterator");
316         memset(iter, 0, sizeof(iter));
317
318         if (db->tdb)
319                 iter->tdb_iter = tdb_firstkey(db->tdb);
320
321         return iter;
322 }
323
324 extern void e2fsck_dir_info_iter_end(e2fsck_t ctx,
325                                      struct dir_info_iter *iter)
326 {
327         ext2fs_free_mem(&iter);
328 }
329
330 /*
331  * A simple interator function
332  */
333 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
334 {
335         TDB_DATA data;
336         struct dir_info_db *db = ctx->dir_info;
337         struct dir_info_ent *buf;
338         static struct dir_info ret_dir_info;
339
340         if (!ctx->dir_info || !iter)
341                 return 0;
342
343         if (db->tdb) {
344                 if (iter->tdb_iter.dptr == 0)
345                         return 0;
346                 data = tdb_fetch(db->tdb, iter->tdb_iter);
347                 if (!data.dptr) {
348                         printf("iter fetch failed: %s\n",
349                                tdb_errorstr(db->tdb));
350                         return 0;
351                 }
352                 buf = (struct dir_info_ent *) data.dptr;
353                 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
354                 ret_dir_info.dotdot = buf->dotdot;
355                 ret_dir_info.parent = buf->parent;
356                 iter->tdb_iter = tdb_nextkey(db->tdb, iter->tdb_iter);
357                 return &ret_dir_info;
358         }
359
360         if (iter->i >= ctx->dir_info->count)
361                 return 0;
362
363 #ifdef DIRINFO_DEBUG
364         printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
365                ctx->dir_info->array[iter->i].dotdot,
366                ctx->dir_info->array[iter->i].parent);
367 #endif
368         ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
369         return(ctx->dir_info->last_lookup);
370 }
371
372 /*
373  * This function only sets the parent pointer, and requires that
374  * dirinfo structure has already been created.
375  */
376 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
377                                ext2_ino_t parent)
378 {
379         struct dir_info *p;
380
381         p = e2fsck_get_dir_info(ctx, ino);
382         if (!p)
383                 return 1;
384         p->parent = parent;
385         e2fsck_put_dir_info(ctx, p);
386         return 0;
387 }
388
389 /*
390  * This function only sets the dot dot pointer, and requires that
391  * dirinfo structure has already been created.
392  */
393 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
394                                ext2_ino_t dotdot)
395 {
396         struct dir_info *p;
397
398         p = e2fsck_get_dir_info(ctx, ino);
399         if (!p)
400                 return 1;
401         p->dotdot = dotdot;
402         e2fsck_put_dir_info(ctx, p);
403         return 0;
404 }
405
406 /*
407  * This function only sets the parent pointer, and requires that
408  * dirinfo structure has already been created.
409  */
410 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
411                                ext2_ino_t *parent)
412 {
413         struct dir_info *p;
414
415         p = e2fsck_get_dir_info(ctx, ino);
416         if (!p)
417                 return 1;
418         *parent = p->parent;
419         return 0;
420 }
421
422 /*
423  * This function only sets the dot dot pointer, and requires that
424  * dirinfo structure has already been created.
425  */
426 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
427                                ext2_ino_t *dotdot)
428 {
429         struct dir_info *p;
430
431         p = e2fsck_get_dir_info(ctx, ino);
432         if (!p)
433                 return 1;
434         *dotdot = p->dotdot;
435         return 0;
436 }
437