Whamcloud - gitweb
LU-6722 jbd: double minimum journal size for RHEL7
[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, *old_array;
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                 old_array = ctx->dir_info->array;
140                 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
141                                            sizeof(struct dir_info),
142                                            &ctx->dir_info->array);
143                 if (retval) {
144                         fprintf(stderr, "Couldn't reallocate dir_info "
145                                 "structure to %d entries\n",
146                                 ctx->dir_info->size);
147                         fatal_error(ctx, 0);
148                         ctx->dir_info->size -= 10;
149                         return;
150                 }
151                 if (old_array != ctx->dir_info->array)
152                         ctx->dir_info->last_lookup = NULL;
153         }
154
155         ent.ino = ino;
156         ent.parent = parent;
157         ent.dotdot = parent;
158
159         if (db->tdb) {
160                 e2fsck_put_dir_info(ctx, &ent);
161                 return;
162         }
163
164         /*
165          * Normally, add_dir_info is called with each inode in
166          * sequential order; but once in a while (like when pass 3
167          * needs to recreate the root directory or lost+found
168          * directory) it is called out of order.  In those cases, we
169          * need to move the dir_info entries down to make room, since
170          * the dir_info array needs to be sorted by inode number for
171          * get_dir_info()'s sake.
172          */
173         if (ctx->dir_info->count &&
174             ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
175                 for (i = ctx->dir_info->count-1; i > 0; i--)
176                         if (ctx->dir_info->array[i-1].ino < ino)
177                                 break;
178                 dir = &ctx->dir_info->array[i];
179                 if (dir->ino != ino)
180                         for (j = ctx->dir_info->count++; j > i; j--)
181                                 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
182         } else
183                 dir = &ctx->dir_info->array[ctx->dir_info->count++];
184
185         dir->ino = ino;
186         dir->dotdot = parent;
187         dir->parent = parent;
188 }
189
190 /*
191  * get_dir_info() --- given an inode number, try to find the directory
192  * information entry for it.
193  */
194 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
195 {
196         struct dir_info_db      *db = ctx->dir_info;
197         int                     low, high, mid;
198         struct dir_info_ent     *buf;
199         static struct dir_info  ret_dir_info;
200
201         if (!db)
202                 return 0;
203
204 #ifdef DIRINFO_DEBUG
205         printf("e2fsck_get_dir_info %d...", ino);
206 #endif
207
208         if (db->tdb) {
209                 TDB_DATA key, data;
210
211                 key.dptr = (unsigned char *) &ino;
212                 key.dsize = sizeof(ext2_ino_t);
213
214                 data = tdb_fetch(db->tdb, key);
215                 if (!data.dptr) {
216                         if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
217                                 printf("fetch failed: %s\n",
218                                        tdb_errorstr(db->tdb));
219                         return 0;
220                 }
221
222                 buf = (struct dir_info_ent *) data.dptr;
223                 ret_dir_info.ino = ino;
224                 ret_dir_info.dotdot = buf->dotdot;
225                 ret_dir_info.parent = buf->parent;
226 #ifdef DIRINFO_DEBUG
227                 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
228 #endif
229                 free(data.dptr);
230                 return &ret_dir_info;
231         }
232
233         if (db->last_lookup && db->last_lookup->ino == ino)
234                 return db->last_lookup;
235
236         low = 0;
237         high = ctx->dir_info->count-1;
238         if (ino == ctx->dir_info->array[low].ino) {
239 #ifdef DIRINFO_DEBUG
240                 printf("(%d,%d,%d)\n", ino,
241                        ctx->dir_info->array[low].dotdot,
242                        ctx->dir_info->array[low].parent);
243 #endif
244                 return &ctx->dir_info->array[low];
245         }
246         if (ino == ctx->dir_info->array[high].ino) {
247 #ifdef DIRINFO_DEBUG
248                 printf("(%d,%d,%d)\n", ino,
249                        ctx->dir_info->array[high].dotdot,
250                        ctx->dir_info->array[high].parent);
251 #endif
252                 return &ctx->dir_info->array[high];
253         }
254
255         while (low < high) {
256                 mid = (low+high)/2;
257                 if (mid == low || mid == high)
258                         break;
259                 if (ino == ctx->dir_info->array[mid].ino) {
260 #ifdef DIRINFO_DEBUG
261                         printf("(%d,%d,%d)\n", ino,
262                                ctx->dir_info->array[mid].dotdot,
263                                ctx->dir_info->array[mid].parent);
264 #endif
265                         return &ctx->dir_info->array[mid];
266                 }
267                 if (ino < ctx->dir_info->array[mid].ino)
268                         high = mid;
269                 else
270                         low = mid;
271         }
272         return 0;
273 }
274
275 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
276 {
277         struct dir_info_db      *db = ctx->dir_info;
278         struct dir_info_ent     buf;
279         TDB_DATA                key, data;
280
281 #ifdef DIRINFO_DEBUG
282         printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
283                dir->parent);
284 #endif
285
286         if (!db->tdb)
287                 return;
288
289         buf.parent = dir->parent;
290         buf.dotdot = dir->dotdot;
291
292         key.dptr = (unsigned char *) &dir->ino;
293         key.dsize = sizeof(ext2_ino_t);
294         data.dptr = (unsigned char *) &buf;
295         data.dsize = sizeof(buf);
296
297         if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
298                 printf("store failed: %s\n", tdb_errorstr(db->tdb));
299         }
300         return;
301 }
302
303 /*
304  * Free the dir_info structure when it isn't needed any more.
305  */
306 void e2fsck_free_dir_info(e2fsck_t ctx)
307 {
308         if (ctx->dir_info) {
309                 if (ctx->dir_info->tdb)
310                         tdb_close(ctx->dir_info->tdb);
311                 if (ctx->dir_info->tdb_fn) {
312                         unlink(ctx->dir_info->tdb_fn);
313                         free(ctx->dir_info->tdb_fn);
314                 }
315                 if (ctx->dir_info->array)
316                         ext2fs_free_mem(&ctx->dir_info->array);
317                 ctx->dir_info->array = 0;
318                 ctx->dir_info->size = 0;
319                 ctx->dir_info->count = 0;
320                 ext2fs_free_mem(&ctx->dir_info);
321                 ctx->dir_info = 0;
322         }
323 }
324
325 /*
326  * Return the count of number of directories in the dir_info structure
327  */
328 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
329 {
330         return ctx->dir_info ? ctx->dir_info->count : 0;
331 }
332
333 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
334 {
335         struct dir_info_iter *iter;
336         struct dir_info_db *db = ctx->dir_info;
337
338         iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
339                                       "dir_info iterator");
340
341         if (db->tdb)
342                 iter->tdb_iter = tdb_firstkey(db->tdb);
343
344         return iter;
345 }
346
347 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
348                               struct dir_info_iter *iter)
349 {
350         free(iter->tdb_iter.dptr);
351         ext2fs_free_mem(&iter);
352 }
353
354 /*
355  * A simple interator function
356  */
357 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
358 {
359         TDB_DATA data, key;
360         struct dir_info_db *db = ctx->dir_info;
361         struct dir_info_ent *buf;
362         static struct dir_info ret_dir_info;
363
364         if (!ctx->dir_info || !iter)
365                 return 0;
366
367         if (db->tdb) {
368                 if (iter->tdb_iter.dptr == 0)
369                         return 0;
370                 key = iter->tdb_iter;
371                 data = tdb_fetch(db->tdb, key);
372                 if (!data.dptr) {
373                         printf("iter fetch failed: %s\n",
374                                tdb_errorstr(db->tdb));
375                         return 0;
376                 }
377                 buf = (struct dir_info_ent *) data.dptr;
378                 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
379                 ret_dir_info.dotdot = buf->dotdot;
380                 ret_dir_info.parent = buf->parent;
381                 iter->tdb_iter = tdb_nextkey(db->tdb, key);
382                 free(key.dptr);
383                 free(data.dptr);
384                 return &ret_dir_info;
385         }
386
387         if (iter->i >= ctx->dir_info->count)
388                 return 0;
389
390 #ifdef DIRINFO_DEBUG
391         printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
392                ctx->dir_info->array[iter->i].dotdot,
393                ctx->dir_info->array[iter->i].parent);
394 #endif
395         ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
396         return(ctx->dir_info->last_lookup);
397 }
398
399 /*
400  * This function only sets the parent pointer, and requires that
401  * dirinfo structure has already been created.
402  */
403 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
404                                ext2_ino_t parent)
405 {
406         struct dir_info *p;
407
408         p = e2fsck_get_dir_info(ctx, ino);
409         if (!p)
410                 return 1;
411         p->parent = parent;
412         e2fsck_put_dir_info(ctx, p);
413         return 0;
414 }
415
416 /*
417  * This function only sets the dot dot pointer, and requires that
418  * dirinfo structure has already been created.
419  */
420 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
421                                ext2_ino_t dotdot)
422 {
423         struct dir_info *p;
424
425         p = e2fsck_get_dir_info(ctx, ino);
426         if (!p)
427                 return 1;
428         p->dotdot = dotdot;
429         e2fsck_put_dir_info(ctx, p);
430         return 0;
431 }
432
433 /*
434  * This function only sets the parent pointer, and requires that
435  * dirinfo structure has already been created.
436  */
437 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
438                                ext2_ino_t *parent)
439 {
440         struct dir_info *p;
441
442         p = e2fsck_get_dir_info(ctx, ino);
443         if (!p)
444                 return 1;
445         *parent = p->parent;
446         return 0;
447 }
448
449 /*
450  * This function only sets the dot dot pointer, and requires that
451  * dirinfo structure has already been created.
452  */
453 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
454                                ext2_ino_t *dotdot)
455 {
456         struct dir_info *p;
457
458         p = e2fsck_get_dir_info(ctx, ino);
459         if (!p)
460                 return 1;
461         *dotdot = p->dotdot;
462         return 0;
463 }
464