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