1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* filefrag.c --- display the fragmentation information for a file
*
* Copyright (C) 2011 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*/
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern int optind;
extern char *optarg;
#endif
#include "debugfs.h"
#define VERBOSE_OPT 0x0001
#define DIR_OPT 0x0002
#define RECURSIVE_OPT 0x0004
struct dir_list {
char *name;
ext2_ino_t ino;
struct dir_list *next;
};
struct filefrag_struct {
FILE *f;
const char *name;
const char *dir_name;
int options;
int logical_width;
int physical_width;
int ext;
int cont_ext;
e2_blkcnt_t num;
e2_blkcnt_t logical_start;
blk64_t physical_start;
blk64_t expected;
struct dir_list *dir_list, *dir_last;
};
static int int_log10(unsigned long long arg)
{
int l = 0;
arg = arg / 10;
while (arg) {
l++;
arg = arg / 10;
}
return l;
}
static void print_header(struct filefrag_struct *fs)
{
if (fs->options & VERBOSE_OPT) {
fprintf(fs->f, "%4s %*s %*s %*s %*s\n", "ext",
fs->logical_width, "logical", fs->physical_width,
"physical", fs->physical_width, "expected",
fs->logical_width, "length");
}
}
static void report_filefrag(struct filefrag_struct *fs)
{
if (fs->num == 0)
return;
if (fs->options & VERBOSE_OPT) {
if (fs->expected)
fprintf(fs->f, "%4d %*lu %*llu %*llu %*lu\n", fs->ext,
fs->logical_width,
(unsigned long) fs->logical_start,
fs->physical_width,
(unsigned long long) fs->physical_start,
fs->physical_width,
(unsigned long long) fs->expected,
fs->logical_width, (unsigned long) fs->num);
else
fprintf(fs->f, "%4d %*lu %*llu %*s %*lu\n", fs->ext,
fs->logical_width,
(unsigned long) fs->logical_start,
fs->physical_width,
(unsigned long long) fs->physical_start,
fs->physical_width, "",
fs->logical_width, (unsigned long) fs->num);
}
fs->ext++;
}
static int filefrag_blocks_proc(ext2_filsys ext4_fs EXT2FS_ATTR((unused)),
blk64_t *blocknr, e2_blkcnt_t blockcnt,
blk64_t ref_block EXT2FS_ATTR((unused)),
int ref_offset EXT2FS_ATTR((unused)),
void *private)
{
struct filefrag_struct *fs = private;
if (blockcnt < 0 || *blocknr == 0)
return 0;
if ((fs->num == 0) || (blockcnt != fs->logical_start + fs->num) ||
(*blocknr != fs->physical_start + fs->num)) {
report_filefrag(fs);
if (blockcnt == fs->logical_start + fs->num)
fs->expected = fs->physical_start + fs->num;
else
fs->expected = 0;
fs->logical_start = blockcnt;
fs->physical_start = *blocknr;
fs->num = 1;
fs->cont_ext++;
} else
fs->num++;
return 0;
}
static void filefrag(ext2_ino_t ino, struct ext2_inode *inode,
struct filefrag_struct *fs)
{
errcode_t retval;
int blocksize = current_fs->blocksize;
fs->logical_width = int_log10((EXT2_I_SIZE(inode) + blocksize - 1) /
blocksize) + 1;
if (fs->logical_width < 7)
fs->logical_width = 7;
fs->ext = 0;
fs->cont_ext = 0;
fs->logical_start = 0;
fs->physical_start = 0;
fs->num = 0;
if (fs->options & VERBOSE_OPT) {
blk64_t num_blocks = ext2fs_inode_i_blocks(current_fs, inode);
if (!ext2fs_has_feature_huge_file(current_fs->super) ||
!(inode->i_flags & EXT4_HUGE_FILE_FL))
num_blocks /= current_fs->blocksize / 512;
fprintf(fs->f, "\n%s has %llu block(s), i_size is %llu\n",
fs->name, (unsigned long long) num_blocks,
(unsigned long long) EXT2_I_SIZE(inode));
}
print_header(fs);
if (ext2fs_inode_has_valid_blocks2(current_fs, inode)) {
retval = ext2fs_block_iterate3(current_fs, ino,
BLOCK_FLAG_READ_ONLY, NULL,
filefrag_blocks_proc, fs);
if (retval)
com_err("ext2fs_block_iterate3", retval, 0);
}
report_filefrag(fs);
fprintf(fs->f, "%s: %d contiguous extents%s\n", fs->name, fs->ext,
LINUX_S_ISDIR(inode->i_mode) ? " (dir)" : "");
}
static int filefrag_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
int entry,
struct ext2_dir_entry *dirent,
int offset EXT2FS_ATTR((unused)),
int blocksize EXT2FS_ATTR((unused)),
char *buf EXT2FS_ATTR((unused)),
void *private)
{
struct filefrag_struct *fs = private;
struct ext2_inode inode;
ext2_ino_t ino;
char name[EXT2_NAME_LEN + 1];
char *cp;
int thislen;
if (entry == DIRENT_DELETED_FILE)
return 0;
thislen = ext2fs_dirent_name_len(dirent);
strncpy(name, dirent->name, thislen);
name[thislen] = '\0';
ino = dirent->inode;
if (!strcmp(name, ".") || !strcmp(name, ".."))
return 0;
cp = malloc(strlen(fs->dir_name) + strlen(name) + 2);
if (!cp) {
fprintf(stderr, "Couldn't allocate memory for %s/%s\n",
fs->dir_name, name);
return 0;
}
sprintf(cp, "%s/%s", fs->dir_name, name);
fs->name = cp;
if (debugfs_read_inode(ino, &inode, fs->name))
goto errout;
filefrag(ino, &inode, fs);
if ((fs->options & RECURSIVE_OPT) && LINUX_S_ISDIR(inode.i_mode)) {
struct dir_list *p;
p = malloc(sizeof(struct dir_list));
if (!p) {
fprintf(stderr, "Couldn't allocate dir_list for %s\n",
fs->name);
goto errout;
}
memset(p, 0, sizeof(struct dir_list));
p->name = cp;
p->ino = ino;
if (fs->dir_last)
fs->dir_last->next = p;
else
fs->dir_list = p;
fs->dir_last = p;
return 0;
}
errout:
free(cp);
fs->name = 0;
return 0;
}
static void dir_iterate(ext2_ino_t ino, struct filefrag_struct *fs)
{
errcode_t retval;
struct dir_list *p = NULL;
fs->dir_name = fs->name;
while (1) {
retval = ext2fs_dir_iterate2(current_fs, ino, 0,
0, filefrag_dir_proc, fs);
if (retval)
com_err("ext2fs_dir_iterate2", retval, 0);
if (p) {
free(p->name);
fs->dir_list = p->next;
if (!fs->dir_list)
fs->dir_last = 0;
free(p);
}
p = fs->dir_list;
if (!p)
break;
ino = p->ino;
fs->dir_name = p->name;
}
}
void do_filefrag(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
void *infop EXT2FS_ATTR((unused)))
{
struct filefrag_struct fs;
struct ext2_inode inode;
ext2_ino_t ino;
int c;
memset(&fs, 0, sizeof(fs));
if (check_fs_open(argv[0]))
return;
reset_getopt();
while ((c = getopt(argc, argv, "dvr")) != EOF) {
switch (c) {
case 'd':
fs.options |= DIR_OPT;
break;
case 'v':
fs.options |= VERBOSE_OPT;
break;
case 'r':
fs.options |= RECURSIVE_OPT;
break;
default:
goto print_usage;
}
}
if (argc > optind+1) {
print_usage:
com_err(0, 0, "Usage: filefrag [-dvr] file");
return;
}
if (argc == optind) {
ino = cwd;
fs.name = ".";
} else {
ino = string_to_inode(argv[optind]);
fs.name = argv[optind];
}
if (!ino)
return;
if (debugfs_read_inode(ino, &inode, argv[0]))
return;
fs.f = open_pager();
fs.physical_width = int_log10(ext2fs_blocks_count(current_fs->super));
fs.physical_width++;
if (fs.physical_width < 8)
fs.physical_width = 8;
if (!LINUX_S_ISDIR(inode.i_mode) || (fs.options & DIR_OPT))
filefrag(ino, &inode, &fs);
else
dir_iterate(ino, &fs);
fprintf(fs.f, "\n");
close_pager(fs.f);
return;
}
|