Whamcloud - gitweb
misc: clean up compiler warnings
[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 static __u64 e2p_blocks_count(struct ext2_super_block *super)
170 {
171         return super->s_blocks_count |
172                 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
173                 (__u64) super->s_blocks_count_hi << 32 : 0);
174 }
175
176 static __u64 e2p_r_blocks_count(struct ext2_super_block *super)
177 {
178         return super->s_r_blocks_count |
179                 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
180                 (__u64) super->s_r_blocks_count_hi << 32 : 0);
181 }
182
183 static __u64 e2p_free_blocks_count(struct ext2_super_block *super)
184 {
185         return super->s_free_blocks_count |
186                 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
187                 (__u64) super->s_free_blocks_hi << 32 : 0);
188 }
189
190 #ifndef EXT2_INODE_SIZE
191 #define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
192 #endif
193
194 #ifndef EXT2_GOOD_OLD_REV
195 #define EXT2_GOOD_OLD_REV 0
196 #endif
197
198 void list_super2(struct ext2_super_block * sb, FILE *f)
199 {
200         int inode_blocks_per_group;
201         char buf[80], *str;
202         time_t  tm;
203
204         inode_blocks_per_group = (((sb->s_inodes_per_group *
205                                     EXT2_INODE_SIZE(sb)) +
206                                    EXT2_BLOCK_SIZE(sb) - 1) /
207                                   EXT2_BLOCK_SIZE(sb));
208         if (sb->s_volume_name[0]) {
209                 memset(buf, 0, sizeof(buf));
210                 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
211         } else
212                 strcpy(buf, "<none>");
213         fprintf(f, "Filesystem volume name:   %s\n", buf);
214         if (sb->s_last_mounted[0]) {
215                 memset(buf, 0, sizeof(buf));
216                 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
217         } else
218                 strcpy(buf, "<not available>");
219         fprintf(f, "Last mounted on:          %s\n", buf);
220         fprintf(f, "Filesystem UUID:          %s\n", e2p_uuid2str(sb->s_uuid));
221         fprintf(f, "Filesystem magic number:  0x%04X\n", sb->s_magic);
222         fprintf(f, "Filesystem revision #:    %d", sb->s_rev_level);
223         if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
224                 fprintf(f, " (original)\n");
225 #ifdef EXT2_DYNAMIC_REV
226         } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
227                 fprintf(f, " (dynamic)\n");
228 #endif
229         } else
230                 fprintf(f, " (unknown)\n");
231         print_features(sb, f);
232         print_super_flags(sb, f);
233         print_mntopts(sb, f);
234         if (sb->s_mount_opts[0])
235                 fprintf(f, "Mount options:            %s\n", sb->s_mount_opts);
236         fprintf(f, "Filesystem state:        ");
237         print_fs_state (f, sb->s_state);
238         fprintf(f, "\n");
239         fprintf(f, "Errors behavior:          ");
240         print_fs_errors(f, sb->s_errors);
241         fprintf(f, "\n");
242         str = e2p_os2string(sb->s_creator_os);
243         fprintf(f, "Filesystem OS type:       %s\n", str);
244         free(str);
245         fprintf(f, "Inode count:              %u\n", sb->s_inodes_count);
246         fprintf(f, "Block count:              %llu\n", e2p_blocks_count(sb));
247         fprintf(f, "Reserved block count:     %llu\n", e2p_r_blocks_count(sb));
248         if (sb->s_overhead_blocks)
249                 fprintf(f, "Overhead blocks:          %u\n",
250                         sb->s_overhead_blocks);
251         fprintf(f, "Free blocks:              %llu\n", e2p_free_blocks_count(sb));
252         fprintf(f, "Free inodes:              %u\n", sb->s_free_inodes_count);
253         fprintf(f, "First block:              %u\n", sb->s_first_data_block);
254         fprintf(f, "Block size:               %u\n", EXT2_BLOCK_SIZE(sb));
255         if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
256                 fprintf(f, "Cluster size:             %u\n",
257                         EXT2_CLUSTER_SIZE(sb));
258         else
259                 fprintf(f, "Fragment size:            %u\n",
260                         EXT2_CLUSTER_SIZE(sb));
261         if (sb->s_reserved_gdt_blocks)
262                 fprintf(f, "Reserved GDT blocks:      %u\n",
263                         sb->s_reserved_gdt_blocks);
264         fprintf(f, "Blocks per group:         %u\n", sb->s_blocks_per_group);
265         if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
266                 fprintf(f, "Clusters per group:       %u\n",
267                         sb->s_clusters_per_group);
268         else
269                 fprintf(f, "Fragments per group:      %u\n",
270                         sb->s_clusters_per_group);
271         fprintf(f, "Inodes per group:         %u\n", sb->s_inodes_per_group);
272         fprintf(f, "Inode blocks per group:   %u\n", inode_blocks_per_group);
273         if (sb->s_raid_stride)
274                 fprintf(f, "RAID stride:              %u\n",
275                         sb->s_raid_stride);
276         if (sb->s_raid_stripe_width)
277                 fprintf(f, "RAID stripe width:        %u\n",
278                         sb->s_raid_stripe_width);
279         if (sb->s_first_meta_bg)
280                 fprintf(f, "First meta block group:   %u\n",
281                         sb->s_first_meta_bg);
282         if (sb->s_log_groups_per_flex)
283                 fprintf(f, "Flex block group size:    %u\n",
284                         1 << sb->s_log_groups_per_flex);
285         if (sb->s_mkfs_time) {
286                 tm = sb->s_mkfs_time;
287                 fprintf(f, "Filesystem created:       %s", ctime(&tm));
288         }
289         tm = sb->s_mtime;
290         fprintf(f, "Last mount time:          %s",
291                 sb->s_mtime ? ctime(&tm) : "n/a\n");
292         tm = sb->s_wtime;
293         fprintf(f, "Last write time:          %s", ctime(&tm));
294         fprintf(f, "Mount count:              %u\n", sb->s_mnt_count);
295         fprintf(f, "Maximum mount count:      %d\n", sb->s_max_mnt_count);
296         tm = sb->s_lastcheck;
297         fprintf(f, "Last checked:             %s", ctime(&tm));
298         fprintf(f, "Check interval:           %u (%s)\n", sb->s_checkinterval,
299                interval_string(sb->s_checkinterval));
300         if (sb->s_checkinterval)
301         {
302                 time_t next;
303
304                 next = sb->s_lastcheck + sb->s_checkinterval;
305                 fprintf(f, "Next check after:         %s", ctime(&next));
306         }
307 #define POW2(x) ((__u64) 1 << (x))
308         if (sb->s_kbytes_written) {
309                 fprintf(f, "Lifetime writes:          ");
310                 if (sb->s_kbytes_written < POW2(13))
311                         fprintf(f, "%llu kB\n", sb->s_kbytes_written);
312                 else if (sb->s_kbytes_written < POW2(23))
313                         fprintf(f, "%llu MB\n",
314                                 (sb->s_kbytes_written + POW2(9)) >> 10);
315                 else if (sb->s_kbytes_written < POW2(33))
316                         fprintf(f, "%llu GB\n",
317                                 (sb->s_kbytes_written + POW2(19)) >> 20);
318                 else if (sb->s_kbytes_written < POW2(43))
319                         fprintf(f, "%llu TB\n",
320                                 (sb->s_kbytes_written + POW2(29)) >> 30);
321                 else
322                         fprintf(f, "%llu PB\n",
323                                 (sb->s_kbytes_written + POW2(39)) >> 40);
324         }
325         fprintf(f, "Reserved blocks uid:      ");
326         print_user(sb->s_def_resuid, f);
327         fprintf(f, "Reserved blocks gid:      ");
328         print_group(sb->s_def_resgid, f);
329         if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
330                 fprintf(f, "First inode:              %d\n", sb->s_first_ino);
331                 fprintf(f, "Inode size:           %d\n", sb->s_inode_size);
332                 if (sb->s_min_extra_isize)
333                         fprintf(f, "Required extra isize:     %d\n",
334                                 sb->s_min_extra_isize);
335                 if (sb->s_want_extra_isize)
336                         fprintf(f, "Desired extra isize:      %d\n",
337                                 sb->s_want_extra_isize);
338         }
339         if (!e2p_is_null_uuid(sb->s_journal_uuid))
340                 fprintf(f, "Journal UUID:             %s\n",
341                         e2p_uuid2str(sb->s_journal_uuid));
342         if (sb->s_journal_inum)
343                 fprintf(f, "Journal inode:            %u\n",
344                         sb->s_journal_inum);
345         if (sb->s_journal_dev)
346                 fprintf(f, "Journal device:               0x%04x\n",
347                         sb->s_journal_dev);
348         if (sb->s_last_orphan)
349                 fprintf(f, "First orphan inode:       %u\n",
350                         sb->s_last_orphan);
351         if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
352             sb->s_def_hash_version)
353                 fprintf(f, "Default directory hash:   %s\n",
354                         e2p_hash2string(sb->s_def_hash_version));
355         if (!e2p_is_null_uuid(sb->s_hash_seed))
356                 fprintf(f, "Directory Hash Seed:      %s\n",
357                         e2p_uuid2str(sb->s_hash_seed));
358         if (sb->s_jnl_backup_type) {
359                 fprintf(f, "Journal backup:           ");
360                 switch (sb->s_jnl_backup_type) {
361                 case 1:
362                         fprintf(f, "inode blocks\n");
363                         break;
364                 default:
365                         fprintf(f, "type %u\n", sb->s_jnl_backup_type);
366                 }
367         }
368         if (sb->s_snapshot_inum) {
369                 fprintf(f, "Snapshot inode:           %u\n",
370                         sb->s_snapshot_inum);
371                 fprintf(f, "Snapshot ID:              %u\n",
372                         sb->s_snapshot_id);
373                 fprintf(f, "Snapshot reserved blocks: %llu\n",
374                         sb->s_snapshot_r_blocks_count);
375         }
376         if (sb->s_snapshot_list)
377                 fprintf(f, "Snapshot list head:       %u\n",
378                         sb->s_snapshot_list);
379         if (sb->s_error_count)
380                 fprintf(f, "FS Error count:           %u\n",
381                         sb->s_error_count);
382         if (sb->s_first_error_time) {
383                 tm = sb->s_first_error_time;
384                 fprintf(f, "First error time:         %s", ctime(&tm));
385                 memset(buf, 0, sizeof(buf));
386                 strncpy(buf, (char *)sb->s_first_error_func,
387                         sizeof(sb->s_first_error_func));
388                 fprintf(f, "First error function:     %s\n", buf);
389                 fprintf(f, "First error line #:       %u\n",
390                         sb->s_first_error_line);
391                 fprintf(f, "First error inode #:      %u\n",
392                         sb->s_first_error_ino);
393                 fprintf(f, "First error block #:      %llu\n",
394                         sb->s_first_error_block);
395         }
396         if (sb->s_last_error_time) {
397                 tm = sb->s_last_error_time;
398                 fprintf(f, "Last error time:          %s", ctime(&tm));
399                 memset(buf, 0, sizeof(buf));
400                 strncpy(buf, (char *)sb->s_last_error_func,
401                         sizeof(sb->s_last_error_func));
402                 fprintf(f, "Last error function:      %s\n", buf);
403                 fprintf(f, "Last error line #:        %u\n",
404                         sb->s_last_error_line);
405                 fprintf(f, "Last error inode #:       %u\n",
406                         sb->s_last_error_ino);
407                 fprintf(f, "Last error block #:       %llu\n",
408                         sb->s_last_error_block);
409         }
410         if (sb->s_usr_quota_inum)
411                 fprintf(f, "User quota inode:         %u\n",
412                         sb->s_usr_quota_inum);
413         if (sb->s_grp_quota_inum)
414                 fprintf(f, "Group quota inode:        %u\n",
415                         sb->s_grp_quota_inum);
416 }
417
418 void list_super (struct ext2_super_block * s)
419 {
420         list_super2(s, stdout);
421 }
422