Whamcloud - gitweb
f2a04ffa559f713dfc8c73508a855f2e0d272d5a
[tools/e2fsprogs.git] / lib / e2p / ls.c
1 /*
2  * ls.c                 - List the contents of an ext2fs superblock
3  *
4  * Copyright (C) 1992, 1993, 1994  Remy Card <card@masi.ibp.fr>
5  *                                 Laboratoire MASI, Institut Blaise Pascal
6  *                                 Universite Pierre et Marie Curie (Paris VI)
7  *
8  * Copyright (C) 1995, 1996, 1997  Theodore Ts'o <tytso@mit.edu>
9  *
10  * %Begin-Header%
11  * This file may be redistributed under the terms of the GNU Library
12  * General Public License, version 2.
13  * %End-Header%
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <string.h>
20 #include <grp.h>
21 #include <pwd.h>
22 #include <time.h>
23
24 #include "e2p.h"
25
26 static void print_user (unsigned short uid, FILE *f)
27 {
28         struct passwd *pw;
29
30         fprintf(f, "%u ", uid);
31         pw = getpwuid (uid);
32         if (pw == NULL)
33                 fprintf(f, "(user unknown)\n");
34         else
35                 fprintf(f, "(user %s)\n", pw->pw_name);
36 }
37
38 static void print_group (unsigned short gid, FILE *f)
39 {
40         struct group *gr;
41
42         fprintf(f, "%u ", gid);
43         gr = getgrgid (gid);
44         if (gr == NULL)
45                 fprintf(f, "(group unknown)\n");
46         else
47                 fprintf(f, "(group %s)\n", gr->gr_name);
48 }
49
50 #define MONTH_INT (86400 * 30)
51 #define WEEK_INT (86400 * 7)
52 #define DAY_INT (86400)
53 #define HOUR_INT (60 * 60)
54 #define MINUTE_INT (60)
55
56 static const char *interval_string(unsigned int secs)
57 {
58         static char buf[256], tmp[80];
59         int             hr, min, num;
60
61         buf[0] = 0;
62
63         if (secs == 0)
64                 return "<none>";
65
66         if (secs >= MONTH_INT) {
67                 num = secs / MONTH_INT;
68                 secs -= num*MONTH_INT;
69                 sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
70         }
71         if (secs >= WEEK_INT) {
72                 num = secs / WEEK_INT;
73                 secs -= num*WEEK_INT;
74                 sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
75                         num, (num>1) ? "s" : "");
76                 strcat(buf, tmp);
77         }
78         if (secs >= DAY_INT) {
79                 num = secs / DAY_INT;
80                 secs -= num*DAY_INT;
81                 sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
82                         num, (num>1) ? "s" : "");
83                 strcat(buf, tmp);
84         }
85         if (secs > 0) {
86                 hr = secs / HOUR_INT;
87                 secs -= hr*HOUR_INT;
88                 min = secs / MINUTE_INT;
89                 secs -= min*MINUTE_INT;
90                 sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
91                         hr, min, secs);
92                 strcat(buf, tmp);
93         }
94         return buf;
95 }
96
97 static void print_features(struct ext2_super_block * s, FILE *f)
98 {
99 #ifdef EXT2_DYNAMIC_REV
100         int     i, j, printed=0;
101         __u32   *mask = &s->s_feature_compat, m;
102
103         fprintf(f, "Filesystem features:     ");
104         for (i=0; i <3; i++,mask++) {
105                 for (j=0,m=1; j < 32; j++, m<<=1) {
106                         if (*mask & m) {
107                                 fprintf(f, " %s", e2p_feature2string(i, m));
108                                 printed++;
109                         }
110                 }
111         }
112         if (printed == 0)
113                 fprintf(f, " (none)");
114         fprintf(f, "\n");
115 #endif
116 }
117
118 static void print_mntopts(struct ext2_super_block * s, FILE *f)
119 {
120 #ifdef EXT2_DYNAMIC_REV
121         int     i, printed=0;
122         __u32   mask = s->s_default_mount_opts, m;
123
124         fprintf(f, "Default mount options:   ");
125         if (mask & EXT3_DEFM_JMODE) {
126                 fprintf(f, " %s", e2p_mntopt2string(mask & EXT3_DEFM_JMODE));
127                 printed++;
128         }
129         for (i=0,m=1; i < 32; i++, m<<=1) {
130                 if (m & EXT3_DEFM_JMODE)
131                         continue;
132                 if (mask & m) {
133                         fprintf(f, " %s", e2p_mntopt2string(m));
134                         printed++;
135                 }
136         }
137         if (printed == 0)
138                 fprintf(f, " (none)");
139         fprintf(f, "\n");
140 #endif
141 }
142
143 static void print_super_flags(struct ext2_super_block * s, FILE *f)
144 {
145         int     flags_found = 0;
146
147         if (s->s_flags == 0)
148                 return;
149
150         fputs("Filesystem flags:         ", f);
151         if (s->s_flags & EXT2_FLAGS_SIGNED_HASH) {
152                 fputs("signed_directory_hash ", f);
153                 flags_found++;
154         }
155         if (s->s_flags & EXT2_FLAGS_UNSIGNED_HASH) {
156                 fputs("unsigned_directory_hash ", f);
157                 flags_found++;
158         }
159         if (s->s_flags & EXT2_FLAGS_TEST_FILESYS) {
160                 fputs("test_filesystem ", f);
161                 flags_found++;
162         }
163         if (flags_found)
164                 fputs("\n", f);
165         else
166                 fputs("(none)\n", f);
167 }
168
169
170 #ifndef EXT2_INODE_SIZE
171 #define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
172 #endif
173
174 #ifndef EXT2_GOOD_OLD_REV
175 #define EXT2_GOOD_OLD_REV 0
176 #endif
177
178 void list_super2(struct ext2_super_block * sb, FILE *f)
179 {
180         int inode_blocks_per_group;
181         char buf[80], *str;
182         time_t  tm;
183
184         inode_blocks_per_group = (((sb->s_inodes_per_group *
185                                     EXT2_INODE_SIZE(sb)) +
186                                    EXT2_BLOCK_SIZE(sb) - 1) /
187                                   EXT2_BLOCK_SIZE(sb));
188         if (sb->s_volume_name[0]) {
189                 memset(buf, 0, sizeof(buf));
190                 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
191         } else
192                 strcpy(buf, "<none>");
193         fprintf(f, "Filesystem volume name:   %s\n", buf);
194         if (sb->s_last_mounted[0]) {
195                 memset(buf, 0, sizeof(buf));
196                 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
197         } else
198                 strcpy(buf, "<not available>");
199         fprintf(f, "Last mounted on:          %s\n", buf);
200         fprintf(f, "Filesystem UUID:          %s\n", e2p_uuid2str(sb->s_uuid));
201         fprintf(f, "Filesystem magic number:  0x%04X\n", sb->s_magic);
202         fprintf(f, "Filesystem revision #:    %d", sb->s_rev_level);
203         if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
204                 fprintf(f, " (original)\n");
205 #ifdef EXT2_DYNAMIC_REV
206         } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
207                 fprintf(f, " (dynamic)\n");
208 #endif
209         } else
210                 fprintf(f, " (unknown)\n");
211         print_features(sb, f);
212         print_super_flags(sb, f);
213         print_mntopts(sb, f);
214         fprintf(f, "Filesystem state:        ");
215         print_fs_state (f, sb->s_state);
216         fprintf(f, "\n");
217         fprintf(f, "Errors behavior:          ");
218         print_fs_errors(f, sb->s_errors);
219         fprintf(f, "\n");
220         str = e2p_os2string(sb->s_creator_os);
221         fprintf(f, "Filesystem OS type:       %s\n", str);
222         free(str);
223         fprintf(f, "Inode count:              %u\n", sb->s_inodes_count);
224         fprintf(f, "Block count:              %u\n", sb->s_blocks_count);
225         fprintf(f, "Reserved block count:     %u\n", sb->s_r_blocks_count);
226         fprintf(f, "Free blocks:              %u\n", sb->s_free_blocks_count);
227         fprintf(f, "Free inodes:              %u\n", sb->s_free_inodes_count);
228         fprintf(f, "First block:              %u\n", sb->s_first_data_block);
229         fprintf(f, "Block size:               %u\n", EXT2_BLOCK_SIZE(sb));
230         fprintf(f, "Fragment size:            %u\n", EXT2_FRAG_SIZE(sb));
231         if (sb->s_reserved_gdt_blocks)
232                 fprintf(f, "Reserved GDT blocks:      %u\n",
233                         sb->s_reserved_gdt_blocks);
234         fprintf(f, "Blocks per group:         %u\n", sb->s_blocks_per_group);
235         fprintf(f, "Fragments per group:      %u\n", sb->s_frags_per_group);
236         fprintf(f, "Inodes per group:         %u\n", sb->s_inodes_per_group);
237         fprintf(f, "Inode blocks per group:   %u\n", inode_blocks_per_group);
238         if (sb->s_raid_stride)
239                 fprintf(f, "RAID stride:              %u\n",
240                         sb->s_raid_stride);
241         if (sb->s_raid_stripe_width)
242                 fprintf(f, "RAID stripe width:        %u\n",
243                         sb->s_raid_stripe_width);
244         if (sb->s_first_meta_bg)
245                 fprintf(f, "First meta block group:   %u\n",
246                         sb->s_first_meta_bg);
247         if (sb->s_log_groups_per_flex)
248                 fprintf(f, "Flex block group size:    %u\n",
249                         1 << sb->s_log_groups_per_flex);
250         if (sb->s_mkfs_time) {
251                 tm = sb->s_mkfs_time;
252                 fprintf(f, "Filesystem created:       %s", ctime(&tm));
253         }
254         tm = sb->s_mtime;
255         fprintf(f, "Last mount time:          %s",
256                 sb->s_mtime ? ctime(&tm) : "n/a\n");
257         tm = sb->s_wtime;
258         fprintf(f, "Last write time:          %s", ctime(&tm));
259         fprintf(f, "Mount count:              %u\n", sb->s_mnt_count);
260         fprintf(f, "Maximum mount count:      %d\n", sb->s_max_mnt_count);
261         tm = sb->s_lastcheck;
262         fprintf(f, "Last checked:             %s", ctime(&tm));
263         fprintf(f, "Check interval:           %u (%s)\n", sb->s_checkinterval,
264                interval_string(sb->s_checkinterval));
265         if (sb->s_checkinterval)
266         {
267                 time_t next;
268
269                 next = sb->s_lastcheck + sb->s_checkinterval;
270                 fprintf(f, "Next check after:         %s", ctime(&next));
271         }
272 #define POW2(x) ((__u64) 1 << (x))
273         if (sb->s_kbytes_written) {
274                 fprintf(f, "Lifetime writes:          ");
275                 if (sb->s_kbytes_written < POW2(13))
276                         fprintf(f, "%llu kB\n", sb->s_kbytes_written);
277                 else if (sb->s_kbytes_written < POW2(23))
278                         fprintf(f, "%llu MB\n",
279                                 (sb->s_kbytes_written + POW2(9)) >> 10);
280                 else if (sb->s_kbytes_written < POW2(33))
281                         fprintf(f, "%llu GB\n",
282                                 (sb->s_kbytes_written + POW2(19)) >> 20);
283                 else if (sb->s_kbytes_written < POW2(43))
284                         fprintf(f, "%llu TB\n",
285                                 (sb->s_kbytes_written + POW2(29)) >> 30);
286                 else
287                         fprintf(f, "%llu PB\n",
288                                 (sb->s_kbytes_written + POW2(39)) >> 40);
289         }
290         fprintf(f, "Reserved blocks uid:      ");
291         print_user(sb->s_def_resuid, f);
292         fprintf(f, "Reserved blocks gid:      ");
293         print_group(sb->s_def_resgid, f);
294         if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
295                 fprintf(f, "First inode:              %d\n", sb->s_first_ino);
296                 fprintf(f, "Inode size:           %d\n", sb->s_inode_size);
297                 if (sb->s_min_extra_isize)
298                         fprintf(f, "Required extra isize:     %d\n",
299                                 sb->s_min_extra_isize);
300                 if (sb->s_want_extra_isize)
301                         fprintf(f, "Desired extra isize:      %d\n",
302                                 sb->s_want_extra_isize);
303         }
304         if (!e2p_is_null_uuid(sb->s_journal_uuid))
305                 fprintf(f, "Journal UUID:             %s\n",
306                         e2p_uuid2str(sb->s_journal_uuid));
307         if (sb->s_journal_inum)
308                 fprintf(f, "Journal inode:            %u\n",
309                         sb->s_journal_inum);
310         if (sb->s_journal_dev)
311                 fprintf(f, "Journal device:               0x%04x\n",
312                         sb->s_journal_dev);
313         if (sb->s_last_orphan)
314                 fprintf(f, "First orphan inode:       %u\n",
315                         sb->s_last_orphan);
316         if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
317             sb->s_def_hash_version)
318                 fprintf(f, "Default directory hash:   %s\n",
319                         e2p_hash2string(sb->s_def_hash_version));
320         if (!e2p_is_null_uuid(sb->s_hash_seed))
321                 fprintf(f, "Directory Hash Seed:      %s\n",
322                         e2p_uuid2str(sb->s_hash_seed));
323         if (sb->s_jnl_backup_type) {
324                 fprintf(f, "Journal backup:           ");
325                 switch (sb->s_jnl_backup_type) {
326                 case 1:
327                         fprintf(f, "inode blocks\n");
328                         break;
329                 default:
330                         fprintf(f, "type %u\n", sb->s_jnl_backup_type);
331                 }
332         }
333         if (sb->s_snapshot_inum) {
334                 fprintf(f, "Snapshot inode:           %u\n",
335                         sb->s_snapshot_inum);
336                 fprintf(f, "Snapshot ID:              %u\n",
337                         sb->s_snapshot_id);
338                 fprintf(f, "Snapshot reserved blocks: %llu\n",
339                         sb->s_snapshot_r_blocks_count);
340         }
341         if (sb->s_snapshot_list)
342                 fprintf(f, "Snapshot list head:       %u\n",
343                         sb->s_snapshot_list);
344         if (sb->s_error_count)
345                 fprintf(f, "FS Error count:           %u\n",
346                         sb->s_error_count);
347         if (sb->s_first_error_time) {
348                 tm = sb->s_first_error_time;
349                 fprintf(f, "First error time:         %s", ctime(&tm));
350                 memset(buf, 0, sizeof(buf));
351                 strncpy(buf, sb->s_first_error_func,
352                         sizeof(sb->s_first_error_func));
353                 fprintf(f, "First error function:     %s\n", buf);
354                 fprintf(f, "First error line #:       %u\n",
355                         sb->s_first_error_line);
356                 fprintf(f, "First error inode #:      %u\n",
357                         sb->s_first_error_ino);
358                 fprintf(f, "First error block #:      %llu\n",
359                         sb->s_first_error_block);
360         }
361         if (sb->s_last_error_time) {
362                 tm = sb->s_last_error_time;
363                 fprintf(f, "Last error time:          %s", ctime(&tm));
364                 memset(buf, 0, sizeof(buf));
365                 strncpy(buf, sb->s_last_error_func,
366                         sizeof(sb->s_last_error_func));
367                 fprintf(f, "Last error function:      %s\n", buf);
368                 fprintf(f, "Last error line #:        %u\n",
369                         sb->s_last_error_line);
370                 fprintf(f, "Last error inode #:       %u\n",
371                         sb->s_last_error_ino);
372                 fprintf(f, "Last error block #:       %llu\n",
373                         sb->s_last_error_block);
374         }
375 }
376
377 void list_super (struct ext2_super_block * s)
378 {
379         list_super2(s, stdout);
380 }
381