Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / resize / banalysis.c
1 /*
2  * banalysis.c --- Analyze a filesystem by block 
3  *
4  * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14
15 #include <linux/ext2_fs.h>
16
17 #include "ext2fs/ext2fs.h"
18
19 #include "ext2fs/brel.h"
20 #include "banalysis.h"
21
22 struct process_block_struct {
23         struct ext2_block_analyzer_funcs *funcs;
24         struct ext2_inode_context *ctx;
25         void *private;
26 };
27
28 static int process_block(ext2_filsys fs, blk_t  *block_nr,
29                          int blockcnt, blk_t ref_block,
30                          int ref_offset, void *private)
31 {
32         struct process_block_struct *pb = private;
33         blk_t   new_block;
34         struct ext2_block_relocate_entry ent;
35
36         if (ref_block == 0)
37                 ref_offset = blockcnt;
38
39         new_block = pb->funcs->block_analyze(fs, *block_nr, ref_block,
40                                              ref_offset, pb->ctx, pb->private);
41         if (new_block) {
42                 ent.new = new_block;
43                 ent.offset = ref_offset;
44                 if (ref_block) {
45                         ent.owner.block_ref = ref_block;
46                         ent.flags = 0;
47                 } else {
48                         ent.owner.inode_ref = pb->ctx->ino;
49                         ent.flags = RELOCATE_INODE_REF;
50                 }
51                 ext2fs_brel_put(pb->ctx->brel, *block_nr, &ent);
52         }
53         return 0;
54 }
55
56 errcode_t ext2_block_analyze(ext2_filsys fs,
57                              struct ext2_block_analyzer_funcs *funcs,
58                              ext2_brel block_relocation_table,
59                              void *private)
60 {
61         ino_t   ino;
62         struct ext2_inode inode;
63         errcode_t       retval;
64         struct process_block_struct pb;
65         struct ext2_inode_context ctx;
66         ext2_inode_scan scan;
67         char            *block_buf;
68         
69         retval = ext2fs_open_inode_scan(fs, 0, &scan);
70         if (retval)
71                 return retval;
72
73         pb.funcs = funcs;
74         pb.private = private;
75         pb.ctx = &ctx;
76         
77         block_buf = malloc(fs->blocksize * 3);
78         if (!block_buf)
79                 return ENOMEM;
80
81         retval = ext2fs_get_next_inode(scan, &ino, &inode);
82         if (retval)
83                 return retval;
84         ctx.ctx = private;
85         ctx.brel = block_relocation_table;
86         while (ino) {
87                 if ((inode.i_links_count == 0) ||
88                     !ext2fs_inode_has_valid_blocks(&inode))
89                         goto next;
90                 
91                 ctx.ino = ino;
92                 ctx.inode = &inode;
93                 ctx.error = 0;
94
95                 if (funcs->pre_analyze &&
96                     !(*funcs->pre_analyze)(fs, &ctx, private))
97                         goto next;
98
99                 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
100                                               process_block, &pb);
101                 if (retval)
102                         return retval;
103
104                 if (funcs->post_analyze) 
105                         (funcs->post_analyze)(fs, &ctx, private);
106
107         next:
108                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
109                 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
110                         goto next;
111         }
112         return 0;
113 }
114