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 26static 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 38static 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 56static 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 97static 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 118static 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 143static 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 178void 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 if (sb->s_mount_opts[0]) 215 fprintf(f, "Mount options: %s\n", sb->s_mount_opts); 216 fprintf(f, "Filesystem state: "); 217 print_fs_state (f, sb->s_state); 218 fprintf(f, "\n"); 219 fprintf(f, "Errors behavior: "); 220 print_fs_errors(f, sb->s_errors); 221 fprintf(f, "\n"); 222 str = e2p_os2string(sb->s_creator_os); 223 fprintf(f, "Filesystem OS type: %s\n", str); 224 free(str); 225 fprintf(f, "Inode count: %u\n", sb->s_inodes_count); 226 fprintf(f, "Block count: %u\n", sb->s_blocks_count); 227 fprintf(f, "Reserved block count: %u\n", sb->s_r_blocks_count); 228 fprintf(f, "Free blocks: %u\n", sb->s_free_blocks_count); 229 fprintf(f, "Free inodes: %u\n", sb->s_free_inodes_count); 230 fprintf(f, "First block: %u\n", sb->s_first_data_block); 231 fprintf(f, "Block size: %u\n", EXT2_BLOCK_SIZE(sb)); 232 fprintf(f, "Fragment size: %u\n", EXT2_FRAG_SIZE(sb)); 233 if (sb->s_reserved_gdt_blocks) 234 fprintf(f, "Reserved GDT blocks: %u\n", 235 sb->s_reserved_gdt_blocks); 236 fprintf(f, "Blocks per group: %u\n", sb->s_blocks_per_group); 237 fprintf(f, "Fragments per group: %u\n", sb->s_frags_per_group); 238 fprintf(f, "Inodes per group: %u\n", sb->s_inodes_per_group); 239 fprintf(f, "Inode blocks per group: %u\n", inode_blocks_per_group); 240 if (sb->s_raid_stride) 241 fprintf(f, "RAID stride: %u\n", 242 sb->s_raid_stride); 243 if (sb->s_raid_stripe_width) 244 fprintf(f, "RAID stripe width: %u\n", 245 sb->s_raid_stripe_width); 246 if (sb->s_first_meta_bg) 247 fprintf(f, "First meta block group: %u\n", 248 sb->s_first_meta_bg); 249 if (sb->s_log_groups_per_flex) 250 fprintf(f, "Flex block group size: %u\n", 251 1 << sb->s_log_groups_per_flex); 252 if (sb->s_mkfs_time) { 253 tm = sb->s_mkfs_time; 254 fprintf(f, "Filesystem created: %s", ctime(&tm)); 255 } 256 tm = sb->s_mtime; 257 fprintf(f, "Last mount time: %s", 258 sb->s_mtime ? ctime(&tm) : "n/a\n"); 259 tm = sb->s_wtime; 260 fprintf(f, "Last write time: %s", ctime(&tm)); 261 fprintf(f, "Mount count: %u\n", sb->s_mnt_count); 262 fprintf(f, "Maximum mount count: %d\n", sb->s_max_mnt_count); 263 tm = sb->s_lastcheck; 264 fprintf(f, "Last checked: %s", ctime(&tm)); 265 fprintf(f, "Check interval: %u (%s)\n", sb->s_checkinterval, 266 interval_string(sb->s_checkinterval)); 267 if (sb->s_checkinterval) 268 { 269 time_t next; 270 271 next = sb->s_lastcheck + sb->s_checkinterval; 272 fprintf(f, "Next check after: %s", ctime(&next)); 273 } 274#define POW2(x) ((__u64) 1 << (x)) 275 if (sb->s_kbytes_written) { 276 fprintf(f, "Lifetime writes: "); 277 if (sb->s_kbytes_written < POW2(13)) 278 fprintf(f, "%llu kB\n", sb->s_kbytes_written); 279 else if (sb->s_kbytes_written < POW2(23)) 280 fprintf(f, "%llu MB\n", 281 (sb->s_kbytes_written + POW2(9)) >> 10); 282 else if (sb->s_kbytes_written < POW2(33)) 283 fprintf(f, "%llu GB\n", 284 (sb->s_kbytes_written + POW2(19)) >> 20); 285 else if (sb->s_kbytes_written < POW2(43)) 286 fprintf(f, "%llu TB\n", 287 (sb->s_kbytes_written + POW2(29)) >> 30); 288 else 289 fprintf(f, "%llu PB\n", 290 (sb->s_kbytes_written + POW2(39)) >> 40); 291 } 292 fprintf(f, "Reserved blocks uid: "); 293 print_user(sb->s_def_resuid, f); 294 fprintf(f, "Reserved blocks gid: "); 295 print_group(sb->s_def_resgid, f); 296 if (sb->s_rev_level >= EXT2_DYNAMIC_REV) { 297 fprintf(f, "First inode: %d\n", sb->s_first_ino); 298 fprintf(f, "Inode size: %d\n", sb->s_inode_size); 299 if (sb->s_min_extra_isize) 300 fprintf(f, "Required extra isize: %d\n", 301 sb->s_min_extra_isize); 302 if (sb->s_want_extra_isize) 303 fprintf(f, "Desired extra isize: %d\n", 304 sb->s_want_extra_isize); 305 } 306 if (!e2p_is_null_uuid(sb->s_journal_uuid)) 307 fprintf(f, "Journal UUID: %s\n", 308 e2p_uuid2str(sb->s_journal_uuid)); 309 if (sb->s_journal_inum) 310 fprintf(f, "Journal inode: %u\n", 311 sb->s_journal_inum); 312 if (sb->s_journal_dev) 313 fprintf(f, "Journal device: 0x%04x\n", 314 sb->s_journal_dev); 315 if (sb->s_last_orphan) 316 fprintf(f, "First orphan inode: %u\n", 317 sb->s_last_orphan); 318 if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || 319 sb->s_def_hash_version) 320 fprintf(f, "Default directory hash: %s\n", 321 e2p_hash2string(sb->s_def_hash_version)); 322 if (!e2p_is_null_uuid(sb->s_hash_seed)) 323 fprintf(f, "Directory Hash Seed: %s\n", 324 e2p_uuid2str(sb->s_hash_seed)); 325 if (sb->s_jnl_backup_type) { 326 fprintf(f, "Journal backup: "); 327 switch (sb->s_jnl_backup_type) { 328 case 1: 329 fprintf(f, "inode blocks\n"); 330 break; 331 default: 332 fprintf(f, "type %u\n", sb->s_jnl_backup_type); 333 } 334 } 335 if (sb->s_snapshot_inum) { 336 fprintf(f, "Snapshot inode: %u\n", 337 sb->s_snapshot_inum); 338 fprintf(f, "Snapshot ID: %u\n", 339 sb->s_snapshot_id); 340 fprintf(f, "Snapshot reserved blocks: %llu\n", 341 sb->s_snapshot_r_blocks_count); 342 } 343 if (sb->s_snapshot_list) 344 fprintf(f, "Snapshot list head: %u\n", 345 sb->s_snapshot_list); 346 if (sb->s_error_count) 347 fprintf(f, "FS Error count: %u\n", 348 sb->s_error_count); 349 if (sb->s_first_error_time) { 350 tm = sb->s_first_error_time; 351 fprintf(f, "First error time: %s", ctime(&tm)); 352 memset(buf, 0, sizeof(buf)); 353 strncpy(buf, sb->s_first_error_func, 354 sizeof(sb->s_first_error_func)); 355 fprintf(f, "First error function: %s\n", buf); 356 fprintf(f, "First error line #: %u\n", 357 sb->s_first_error_line); 358 fprintf(f, "First error inode #: %u\n", 359 sb->s_first_error_ino); 360 fprintf(f, "First error block #: %llu\n", 361 sb->s_first_error_block); 362 } 363 if (sb->s_last_error_time) { 364 tm = sb->s_last_error_time; 365 fprintf(f, "Last error time: %s", ctime(&tm)); 366 memset(buf, 0, sizeof(buf)); 367 strncpy(buf, sb->s_last_error_func, 368 sizeof(sb->s_last_error_func)); 369 fprintf(f, "Last error function: %s\n", buf); 370 fprintf(f, "Last error line #: %u\n", 371 sb->s_last_error_line); 372 fprintf(f, "Last error inode #: %u\n", 373 sb->s_last_error_ino); 374 fprintf(f, "Last error block #: %llu\n", 375 sb->s_last_error_block); 376 } 377} 378 379void list_super (struct ext2_super_block * s) 380{ 381 list_super2(s, stdout); 382} 383 384