Whamcloud - gitweb
LU-16067 misc: cleanup compiler warnings
[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 <assert.h>
11 #include "config.h"
12 #include "e2fsck.h"
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include "uuid/uuid.h"
16
17 #include "ext2fs/ext2fs.h"
18 #include <ext2fs/tdb.h>
19
20 struct dir_info_db {
21         ext2_ino_t      count;
22         ext2_ino_t      size;
23         struct dir_info *array;
24         struct dir_info *last_lookup;
25 #ifdef CONFIG_TDB
26         char            *tdb_fn;
27         TDB_CONTEXT     *tdb;
28 #endif
29 };
30
31 struct dir_info_iter {
32         ext2_ino_t      i;
33 #ifdef CONFIG_TDB
34         TDB_DATA        tdb_iter;
35 #endif
36 };
37
38 struct dir_info_ent {
39         ext2_ino_t              dotdot; /* Parent according to '..' */
40         ext2_ino_t              parent; /* Parent according to treewalk */
41 };
42
43
44 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
45
46 #ifdef CONFIG_TDB
47 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
48 {
49         struct dir_info_db      *db = ctx->dir_info;
50         ext2_ino_t              threshold;
51         errcode_t               retval;
52         mode_t                  save_umask;
53         char                    *tdb_dir, uuid[40];
54         int                     fd, enable;
55
56         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
57                            &tdb_dir);
58         profile_get_uint(ctx->profile, "scratch_files",
59                          "numdirs_threshold", 0, 0, &threshold);
60         profile_get_boolean(ctx->profile, "scratch_files",
61                             "dirinfo", 0, 1, &enable);
62
63         if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
64             (threshold && num_dirs <= threshold))
65                 return;
66
67         retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
68         if (retval)
69                 return;
70
71         uuid_unparse(ctx->fs->super->s_uuid, uuid);
72         sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
73         save_umask = umask(077);
74         fd = mkstemp(db->tdb_fn);
75         umask(save_umask);
76         if (fd < 0) {
77                 db->tdb = NULL;
78                 return;
79         }
80
81         if (num_dirs < 99991)
82                 num_dirs = 99991; /* largest 5 digit prime */
83
84         db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
85                            O_RDWR | O_CREAT | O_TRUNC, 0600);
86         close(fd);
87 }
88 #endif
89
90 static void setup_db(e2fsck_t ctx)
91 {
92         struct dir_info_db      *db;
93         ext2_ino_t              num_dirs;
94         errcode_t               retval;
95
96         db = (struct dir_info_db *)
97                 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
98                                        "directory map db");
99         db->count = db->size = 0;
100         db->array = 0;
101
102         ctx->dir_info = db;
103
104         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
105         if (retval)
106                 num_dirs = 1024;        /* Guess */
107
108 #ifdef CONFIG_TDB
109         setup_tdb(ctx, num_dirs);
110
111         if (db->tdb) {
112 #ifdef DIRINFO_DEBUG
113                 printf("Note: using tdb!\n");
114 #endif
115                 return;
116         }
117 #endif
118
119         db->size = num_dirs + 10;
120         db->array  = (struct dir_info *)
121                 e2fsck_allocate_memory(ctx, db->size
122                                        * sizeof (struct dir_info),
123                                        "directory map");
124 }
125
126 /*
127  * Return the min index that has ino larger or equal to @ino
128  * If not found, return -ENOENT
129  */
130 static int
131 e2fsck_dir_info_min_larger_equal(struct dir_info_db *dir_info,
132                                  ext2_ino_t ino, ext2_ino_t *index)
133 {
134         ext2_ino_t low = 0;
135         ext2_ino_t mid, high;
136         ext2_ino_t tmp_ino;
137         int found = 0;
138
139         if (dir_info->count == 0)
140                 return -ENOENT;
141
142         high = dir_info->count - 1;
143         while (low <= high) {
144                 /* sum may overflow, but result will fit into mid again */
145                 mid = (unsigned long long)(low + high) / 2;
146                 tmp_ino = dir_info->array[mid].ino;
147                 if (ino == tmp_ino) {
148                         *index = mid;
149                         found = 1;
150                         return 0;
151                 } else if (ino < tmp_ino) {
152                         /*
153                          * The mid ino is larger than @ino, remember the index
154                          * here so we won't miss this ino
155                          */
156                         *index = mid;
157                         found = 1;
158                         if (mid == 0)
159                                 break;
160                         high = mid - 1;
161                 } else {
162                         low = mid + 1;
163                 }
164         }
165
166         if (found)
167                 return 0;
168
169         return -ENOENT;
170 }
171
172 /*
173  * Merge two sorted dir info to @dest
174  */
175 void e2fsck_merge_dir_info(e2fsck_t ctx, struct dir_info_db *src,
176                            struct dir_info_db *dest)
177 {
178         size_t           size_dir_info = sizeof(struct dir_info);
179         ext2_ino_t       size = dest->size;
180         struct dir_info  *src_array = src->array;
181         struct dir_info  *dest_array = dest->array;
182         ext2_ino_t       src_count = src->count;
183         ext2_ino_t       dest_count = dest->count;
184         ext2_ino_t       total_count = src_count + dest_count;
185         struct dir_info *tmp_array;
186         struct dir_info *array_ptr;
187         ext2_ino_t       src_index = 0;
188         ext2_ino_t       dest_index = 0;
189
190         if (src->count == 0)
191                 return;
192
193         if (size < total_count)
194                 size = total_count;
195
196         if (size < src->size)
197                 size = src->size;
198
199         tmp_array = e2fsck_allocate_memory(ctx, size * size_dir_info,
200                                             "directory map");
201         array_ptr = tmp_array;
202         /*
203          * This can be improved by binary search and memcpy, but codes
204          * would be more complex. And if the groups distributed to each
205          * thread are strided, this implementation won't be too bad
206          * comparing to the optimiztion.
207          */
208         while (src_index < src_count || dest_index < dest_count) {
209                 if (src_index >= src_count) {
210                         memcpy(array_ptr, &dest_array[dest_index],
211                                (dest_count - dest_index) * size_dir_info);
212                         break;
213                 }
214                 if (dest_index >= dest_count) {
215                         memcpy(array_ptr, &src_array[src_index],
216                                (src_count - src_index) * size_dir_info);
217                         break;
218                 }
219                 if (src_array[src_index].ino < dest_array[dest_index].ino) {
220                         *array_ptr = src_array[src_index];
221                         src_index++;
222                 } else {
223                         assert(src_array[src_index].ino >
224                                dest_array[dest_index].ino);
225                         *array_ptr = dest_array[dest_index];
226                         dest_index++;
227                 }
228                 array_ptr++;
229         }
230
231         if (dest->array)
232                 ext2fs_free_mem(&dest->array);
233         dest->array = tmp_array;
234         dest->size = size;
235         dest->count = total_count;
236 }
237
238 /*
239  *
240  * Insert an inode into the sorted array. The array should have at least one
241  * free slot.
242  *
243  * Normally, add_dir_info is called with each inode in
244  * sequential order; but once in a while (like when pass 3
245  * needs to recreate the root directory or lost+found
246  * directory) it is called out of order.  In those cases, we
247  * need to move the dir_info entries down to make room, since
248  * the dir_info array needs to be sorted by inode number for
249  * get_dir_info()'s sake.
250  */
251 static void e2fsck_insert_dir_info(struct dir_info_db *dir_info, ext2_ino_t ino, ext2_ino_t parent)
252 {
253         ext2_ino_t              index;
254         struct dir_info         *dir;
255         size_t                  dir_size = sizeof(*dir);
256         struct dir_info         *array = dir_info->array;
257         ext2_ino_t              array_count = dir_info->count;
258         int                     err;
259
260         /*
261          * Removing this check won't break anything. But since seqential ino
262          * inserting happens a lot, this check avoids binary search.
263          */
264         if (array_count == 0 || array[array_count - 1].ino < ino) {
265                 dir = &array[array_count];
266                 dir_info->count++;
267                 goto out;
268         }
269
270         err = e2fsck_dir_info_min_larger_equal(dir_info, ino, &index);
271         if (err >= 0 && array[index].ino == ino) {
272                 dir = &array[index];
273                 goto out;
274         }
275         if (err < 0) {
276                 dir = &array[array_count];
277                 dir_info->count++;
278                 goto out;
279         }
280
281         dir = &array[index];
282         memmove((char *)dir + dir_size, dir, dir_size * (array_count - index));
283         dir_info->count++;
284 out:
285         dir->ino = ino;
286         dir->dotdot = parent;
287         dir->parent = parent;
288 }
289
290 /*
291  * This subroutine is called during pass1 to create a directory info
292  * entry.  During pass1, the passed-in parent is 0; it will get filled
293  * in during pass2.
294  */
295 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
296 {
297         struct dir_info         *old_array;
298         errcode_t               retval;
299         unsigned long           old_size;
300
301 #ifdef DIRINFO_DEBUG
302         printf("add_dir_info for inode (%u, %u)...\n", ino, parent);
303 #endif
304         if (!ctx->dir_info)
305                 setup_db(ctx);
306
307         if (ctx->dir_info->count >= ctx->dir_info->size) {
308                 old_size = ctx->dir_info->size * sizeof(struct dir_info);
309                 ctx->dir_info->size += 10;
310                 old_array = ctx->dir_info->array;
311                 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
312                                            sizeof(struct dir_info),
313                                            &ctx->dir_info->array);
314                 if (retval) {
315                         fprintf(stderr, "Couldn't reallocate dir_info "
316                                 "structure to %u entries\n",
317                                 ctx->dir_info->size);
318                         fatal_error(ctx, 0);
319                         ctx->dir_info->size -= 10;
320                         return;
321                 }
322                 if (old_array != ctx->dir_info->array)
323                         ctx->dir_info->last_lookup = NULL;
324         }
325
326 #ifdef CONFIG_TDB
327         if (ctx->dir_info->tdb) {
328                 struct dir_info ent;
329
330                 ent.ino = ino;
331                 ent.parent = parent;
332                 ent.dotdot = parent;
333                 e2fsck_put_dir_info(ctx, &ent);
334                 return;
335         }
336 #endif
337
338         e2fsck_insert_dir_info(ctx->dir_info, ino, parent);
339 }
340
341 /*
342  * get_dir_info() --- given an inode number, try to find the directory
343  * information entry for it.
344  */
345 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
346 {
347         struct dir_info_db      *db = ctx->dir_info;
348         ext2_ino_t              index;
349         int                     err;
350
351         if (!db)
352                 return 0;
353
354 #ifdef DIRINFO_DEBUG
355         printf("e2fsck_get_dir_info %u...", ino);
356 #endif
357
358 #ifdef CONFIG_TDB
359         if (db->tdb) {
360                 static struct dir_info  ret_dir_info;
361                 TDB_DATA key, data;
362                 struct dir_info_ent     *buf;
363
364                 key.dptr = (unsigned char *) &ino;
365                 key.dsize = sizeof(ext2_ino_t);
366
367                 data = tdb_fetch(db->tdb, key);
368                 if (!data.dptr) {
369                         if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
370                                 printf("fetch failed: %s\n",
371                                        tdb_errorstr(db->tdb));
372                         return 0;
373                 }
374
375                 buf = (struct dir_info_ent *) data.dptr;
376                 ret_dir_info.ino = ino;
377                 ret_dir_info.dotdot = buf->dotdot;
378                 ret_dir_info.parent = buf->parent;
379 #ifdef DIRINFO_DEBUG
380                 printf("(%u,%u,%u)\n", ino, buf->dotdot, buf->parent);
381 #endif
382                 free(data.dptr);
383                 return &ret_dir_info;
384         }
385 #endif
386
387         if (db->last_lookup && db->last_lookup->ino == ino)
388                 return db->last_lookup;
389
390         err = e2fsck_dir_info_min_larger_equal(ctx->dir_info, ino, &index);
391         if (err < 0)
392                 return NULL;
393         assert(ino <= ctx->dir_info->array[index].ino);
394         if (ino == ctx->dir_info->array[index].ino) {
395 #ifdef DIRINFO_DEBUG
396                 printf("(%d,%d,%d)\n", ino,
397                        ctx->dir_info->array[index].dotdot,
398                        ctx->dir_info->array[index].parent);
399 #endif
400                 return &ctx->dir_info->array[index];
401         }
402         return NULL;
403 }
404
405 static void e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,
406                                 struct dir_info *dir EXT2FS_NO_TDB_UNUSED)
407 {
408 #ifdef CONFIG_TDB
409         struct dir_info_db      *db = ctx->dir_info;
410         struct dir_info_ent     buf;
411         TDB_DATA                key, data;
412 #endif
413
414 #ifdef DIRINFO_DEBUG
415         printf("e2fsck_put_dir_info (%u, %u, %u)...", dir->ino, dir->dotdot,
416                dir->parent);
417 #endif
418
419 #ifdef CONFIG_TDB
420         if (!db->tdb)
421                 return;
422
423         buf.parent = dir->parent;
424         buf.dotdot = dir->dotdot;
425
426         key.dptr = (unsigned char *) &dir->ino;
427         key.dsize = sizeof(ext2_ino_t);
428         data.dptr = (unsigned char *) &buf;
429         data.dsize = sizeof(buf);
430
431         if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
432                 printf("store failed: %s\n", tdb_errorstr(db->tdb));
433         }
434 #endif
435 }
436
437 /*
438  * Free the dir_info structure when it isn't needed any more.
439  */
440 void e2fsck_free_dir_info(e2fsck_t ctx)
441 {
442         if (ctx->dir_info) {
443 #ifdef CONFIG_TDB
444                 if (ctx->dir_info->tdb)
445                         tdb_close(ctx->dir_info->tdb);
446                 if (ctx->dir_info->tdb_fn) {
447                         if (unlink(ctx->dir_info->tdb_fn) < 0)
448                                 com_err("e2fsck_free_dir_info", errno,
449                                         _("while freeing dir_info tdb file"));
450                         ext2fs_free_mem(&ctx->dir_info->tdb_fn);
451                 }
452 #endif
453                 if (ctx->dir_info->array)
454                         ext2fs_free_mem(&ctx->dir_info->array);
455                 ctx->dir_info->array = 0;
456                 ctx->dir_info->size = 0;
457                 ctx->dir_info->count = 0;
458                 ext2fs_free_mem(&ctx->dir_info);
459                 ctx->dir_info = 0;
460         }
461 }
462
463 /*
464  * Return the count of number of directories in the dir_info structure
465  */
466 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
467 {
468         return ctx->dir_info ? ctx->dir_info->count : 0;
469 }
470
471 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
472 {
473         struct dir_info_iter *iter;
474
475         iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
476                                       "dir_info iterator");
477
478 #ifdef CONFIG_TDB
479         if (ctx->dir_info->tdb)
480                 iter->tdb_iter = tdb_firstkey(ctx->dir_info->tdb);
481 #endif
482
483         return iter;
484 }
485
486 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
487                               struct dir_info_iter *iter)
488 {
489 #ifdef CONFIG_TDB
490         free(iter->tdb_iter.dptr);
491 #endif
492         ext2fs_free_mem(&iter);
493 }
494
495 /*
496  * A simple interator function
497  */
498 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
499 {
500         if (!ctx->dir_info || !iter)
501                 return 0;
502
503 #ifdef CONFIG_TDB
504         if (ctx->dir_info->tdb) {
505                 static struct dir_info ret_dir_info;
506                 struct dir_info_ent *buf;
507                 TDB_DATA data, key;
508
509                 if (iter->tdb_iter.dptr == 0)
510                         return 0;
511                 key = iter->tdb_iter;
512                 data = tdb_fetch(ctx->dir_info->tdb, key);
513                 if (!data.dptr) {
514                         printf("iter fetch failed: %s\n",
515                                tdb_errorstr(ctx->dir_info->tdb));
516                         return 0;
517                 }
518                 buf = (struct dir_info_ent *) data.dptr;
519                 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
520                 ret_dir_info.dotdot = buf->dotdot;
521                 ret_dir_info.parent = buf->parent;
522                 iter->tdb_iter = tdb_nextkey(ctx->dir_info->tdb, key);
523                 free(key.dptr);
524                 free(data.dptr);
525                 return &ret_dir_info;
526         }
527 #endif
528
529         if (iter->i >= ctx->dir_info->count)
530                 return 0;
531
532 #ifdef DIRINFO_DEBUG
533         printf("iter(%u, %u, %u)...", ctx->dir_info->array[iter->i].ino,
534                ctx->dir_info->array[iter->i].dotdot,
535                ctx->dir_info->array[iter->i].parent);
536 #endif
537         ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
538         return(ctx->dir_info->last_lookup);
539 }
540
541 /*
542  * This function only sets the parent pointer, and requires that
543  * dirinfo structure has already been created.
544  */
545 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
546                                ext2_ino_t parent)
547 {
548         struct dir_info *p;
549
550         p = e2fsck_get_dir_info(ctx, ino);
551         if (!p)
552                 return 1;
553         p->parent = parent;
554         e2fsck_put_dir_info(ctx, p);
555         return 0;
556 }
557
558 /*
559  * This function only sets the dot dot pointer, and requires that
560  * dirinfo structure has already been created.
561  */
562 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
563                                ext2_ino_t dotdot)
564 {
565         struct dir_info *p;
566
567         p = e2fsck_get_dir_info(ctx, ino);
568         if (!p)
569                 return 1;
570         p->dotdot = dotdot;
571         e2fsck_put_dir_info(ctx, p);
572         return 0;
573 }
574
575 /*
576  * This function only sets the parent pointer, and requires that
577  * dirinfo structure has already been created.
578  */
579 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
580                                ext2_ino_t *parent)
581 {
582         struct dir_info *p;
583
584         p = e2fsck_get_dir_info(ctx, ino);
585         if (!p)
586                 return 1;
587         *parent = p->parent;
588         return 0;
589 }
590
591 /*
592  * This function only sets the dot dot pointer, and requires that
593  * dirinfo structure has already been created.
594  */
595 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
596                                ext2_ino_t *dotdot)
597 {
598         struct dir_info *p;
599
600         p = e2fsck_get_dir_info(ctx, ino);
601         if (!p)
602                 return 1;
603         *dotdot = p->dotdot;
604         return 0;
605 }
606