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