Home | History | Annotate | Download | only in e2p
      1 /*
      2  * ls.c			- List the contents of an ext2fs superblock
      3  *
      4  * Copyright (C) 1992, 1993, 1994  Remy Card <card (at) 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 (at) 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_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
    262 		fprintf(f, "Group descriptor size:    %u\n", sb->s_desc_size);
    263 	if (sb->s_reserved_gdt_blocks)
    264 		fprintf(f, "Reserved GDT blocks:      %u\n",
    265 			sb->s_reserved_gdt_blocks);
    266 	fprintf(f, "Blocks per group:         %u\n", sb->s_blocks_per_group);
    267 	if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC)
    268 		fprintf(f, "Clusters per group:       %u\n",
    269 			sb->s_clusters_per_group);
    270 	else
    271 		fprintf(f, "Fragments per group:      %u\n",
    272 			sb->s_clusters_per_group);
    273 	fprintf(f, "Inodes per group:         %u\n", sb->s_inodes_per_group);
    274 	fprintf(f, "Inode blocks per group:   %u\n", inode_blocks_per_group);
    275 	if (sb->s_raid_stride)
    276 		fprintf(f, "RAID stride:              %u\n",
    277 			sb->s_raid_stride);
    278 	if (sb->s_raid_stripe_width)
    279 		fprintf(f, "RAID stripe width:        %u\n",
    280 			sb->s_raid_stripe_width);
    281 	if (sb->s_first_meta_bg)
    282 		fprintf(f, "First meta block group:   %u\n",
    283 			sb->s_first_meta_bg);
    284 	if (sb->s_log_groups_per_flex)
    285 		fprintf(f, "Flex block group size:    %u\n",
    286 			1 << sb->s_log_groups_per_flex);
    287 	if (sb->s_mkfs_time) {
    288 		tm = sb->s_mkfs_time;
    289 		fprintf(f, "Filesystem created:       %s", ctime(&tm));
    290 	}
    291 	tm = sb->s_mtime;
    292 	fprintf(f, "Last mount time:          %s",
    293 		sb->s_mtime ? ctime(&tm) : "n/a\n");
    294 	tm = sb->s_wtime;
    295 	fprintf(f, "Last write time:          %s", ctime(&tm));
    296 	fprintf(f, "Mount count:              %u\n", sb->s_mnt_count);
    297 	fprintf(f, "Maximum mount count:      %d\n", sb->s_max_mnt_count);
    298 	tm = sb->s_lastcheck;
    299 	fprintf(f, "Last checked:             %s", ctime(&tm));
    300 	fprintf(f, "Check interval:           %u (%s)\n", sb->s_checkinterval,
    301 	       interval_string(sb->s_checkinterval));
    302 	if (sb->s_checkinterval)
    303 	{
    304 		time_t next;
    305 
    306 		next = sb->s_lastcheck + sb->s_checkinterval;
    307 		fprintf(f, "Next check after:         %s", ctime(&next));
    308 	}
    309 #define POW2(x) ((__u64) 1 << (x))
    310 	if (sb->s_kbytes_written) {
    311 		fprintf(f, "Lifetime writes:          ");
    312 		if (sb->s_kbytes_written < POW2(13))
    313 			fprintf(f, "%llu kB\n", sb->s_kbytes_written);
    314 		else if (sb->s_kbytes_written < POW2(23))
    315 			fprintf(f, "%llu MB\n",
    316 				(sb->s_kbytes_written + POW2(9)) >> 10);
    317 		else if (sb->s_kbytes_written < POW2(33))
    318 			fprintf(f, "%llu GB\n",
    319 				(sb->s_kbytes_written + POW2(19)) >> 20);
    320 		else if (sb->s_kbytes_written < POW2(43))
    321 			fprintf(f, "%llu TB\n",
    322 				(sb->s_kbytes_written + POW2(29)) >> 30);
    323 		else
    324 			fprintf(f, "%llu PB\n",
    325 				(sb->s_kbytes_written + POW2(39)) >> 40);
    326 	}
    327 	fprintf(f, "Reserved blocks uid:      ");
    328 	print_user(sb->s_def_resuid, f);
    329 	fprintf(f, "Reserved blocks gid:      ");
    330 	print_group(sb->s_def_resgid, f);
    331 	if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
    332 		fprintf(f, "First inode:              %d\n", sb->s_first_ino);
    333 		fprintf(f, "Inode size:	          %d\n", sb->s_inode_size);
    334 		if (sb->s_min_extra_isize)
    335 			fprintf(f, "Required extra isize:     %d\n",
    336 				sb->s_min_extra_isize);
    337 		if (sb->s_want_extra_isize)
    338 			fprintf(f, "Desired extra isize:      %d\n",
    339 				sb->s_want_extra_isize);
    340 	}
    341 	if (!e2p_is_null_uuid(sb->s_journal_uuid))
    342 		fprintf(f, "Journal UUID:             %s\n",
    343 			e2p_uuid2str(sb->s_journal_uuid));
    344 	if (sb->s_journal_inum)
    345 		fprintf(f, "Journal inode:            %u\n",
    346 			sb->s_journal_inum);
    347 	if (sb->s_journal_dev)
    348 		fprintf(f, "Journal device:	          0x%04x\n",
    349 			sb->s_journal_dev);
    350 	if (sb->s_last_orphan)
    351 		fprintf(f, "First orphan inode:       %u\n",
    352 			sb->s_last_orphan);
    353 	if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
    354 	    sb->s_def_hash_version)
    355 		fprintf(f, "Default directory hash:   %s\n",
    356 			e2p_hash2string(sb->s_def_hash_version));
    357 	if (!e2p_is_null_uuid(sb->s_hash_seed))
    358 		fprintf(f, "Directory Hash Seed:      %s\n",
    359 			e2p_uuid2str(sb->s_hash_seed));
    360 	if (sb->s_jnl_backup_type) {
    361 		fprintf(f, "Journal backup:           ");
    362 		switch (sb->s_jnl_backup_type) {
    363 		case 1:
    364 			fprintf(f, "inode blocks\n");
    365 			break;
    366 		default:
    367 			fprintf(f, "type %u\n", sb->s_jnl_backup_type);
    368 		}
    369 	}
    370 	if (sb->s_snapshot_inum) {
    371 		fprintf(f, "Snapshot inode:           %u\n",
    372 			sb->s_snapshot_inum);
    373 		fprintf(f, "Snapshot ID:              %u\n",
    374 			sb->s_snapshot_id);
    375 		fprintf(f, "Snapshot reserved blocks: %llu\n",
    376 			sb->s_snapshot_r_blocks_count);
    377 	}
    378 	if (sb->s_snapshot_list)
    379 		fprintf(f, "Snapshot list head:       %u\n",
    380 			sb->s_snapshot_list);
    381 	if (sb->s_error_count)
    382 		fprintf(f, "FS Error count:           %u\n",
    383 			sb->s_error_count);
    384 	if (sb->s_first_error_time) {
    385 		tm = sb->s_first_error_time;
    386 		fprintf(f, "First error time:         %s", ctime(&tm));
    387 		memset(buf, 0, sizeof(buf));
    388 		strncpy(buf, (char *)sb->s_first_error_func,
    389 			sizeof(sb->s_first_error_func));
    390 		fprintf(f, "First error function:     %s\n", buf);
    391 		fprintf(f, "First error line #:       %u\n",
    392 			sb->s_first_error_line);
    393 		fprintf(f, "First error inode #:      %u\n",
    394 			sb->s_first_error_ino);
    395 		fprintf(f, "First error block #:      %llu\n",
    396 			sb->s_first_error_block);
    397 	}
    398 	if (sb->s_last_error_time) {
    399 		tm = sb->s_last_error_time;
    400 		fprintf(f, "Last error time:          %s", ctime(&tm));
    401 		memset(buf, 0, sizeof(buf));
    402 		strncpy(buf, (char *)sb->s_last_error_func,
    403 			sizeof(sb->s_last_error_func));
    404 		fprintf(f, "Last error function:      %s\n", buf);
    405 		fprintf(f, "Last error line #:        %u\n",
    406 			sb->s_last_error_line);
    407 		fprintf(f, "Last error inode #:       %u\n",
    408 			sb->s_last_error_ino);
    409 		fprintf(f, "Last error block #:       %llu\n",
    410 			sb->s_last_error_block);
    411 	}
    412 	if (sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
    413 		fprintf(f, "MMP block number:         %llu\n",
    414 			(long long)sb->s_mmp_block);
    415 		fprintf(f, "MMP update interval:      %u\n",
    416 			sb->s_mmp_update_interval);
    417 	}
    418 	if (sb->s_usr_quota_inum)
    419 		fprintf(f, "User quota inode:         %u\n",
    420 			sb->s_usr_quota_inum);
    421 	if (sb->s_grp_quota_inum)
    422 		fprintf(f, "Group quota inode:        %u\n",
    423 			sb->s_grp_quota_inum);
    424 
    425 	if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
    426 		fprintf(f, "Checksum:                 0x%08x\n",
    427 			sb->s_checksum);
    428 }
    429 
    430 void list_super (struct ext2_super_block * s)
    431 {
    432 	list_super2(s, stdout);
    433 }
    434 
    435