lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 "config.h" |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <string.h> |
| 21 | #include <grp.h> |
| 22 | #include <pwd.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | #include "e2p.h" |
| 26 | |
| 27 | static void print_user (unsigned short uid, FILE *f) |
| 28 | { |
| 29 | struct passwd *pw; |
| 30 | |
| 31 | fprintf(f, "%u ", uid); |
| 32 | pw = getpwuid (uid); |
| 33 | if (pw == NULL) |
| 34 | fprintf(f, "(user unknown)\n"); |
| 35 | else |
| 36 | fprintf(f, "(user %s)\n", pw->pw_name); |
| 37 | } |
| 38 | |
| 39 | static void print_group (unsigned short gid, FILE *f) |
| 40 | { |
| 41 | struct group *gr; |
| 42 | |
| 43 | fprintf(f, "%u ", gid); |
| 44 | gr = getgrgid (gid); |
| 45 | if (gr == NULL) |
| 46 | fprintf(f, "(group unknown)\n"); |
| 47 | else |
| 48 | fprintf(f, "(group %s)\n", gr->gr_name); |
| 49 | } |
| 50 | |
| 51 | #define MONTH_INT (86400 * 30) |
| 52 | #define WEEK_INT (86400 * 7) |
| 53 | #define DAY_INT (86400) |
| 54 | #define HOUR_INT (60 * 60) |
| 55 | #define MINUTE_INT (60) |
| 56 | |
| 57 | static const char *interval_string(unsigned int secs) |
| 58 | { |
| 59 | static char buf[256], tmp[80]; |
| 60 | int hr, min, num; |
| 61 | |
| 62 | buf[0] = 0; |
| 63 | |
| 64 | if (secs == 0) |
| 65 | return "<none>"; |
| 66 | |
| 67 | if (secs >= MONTH_INT) { |
| 68 | num = secs / MONTH_INT; |
| 69 | secs -= num*MONTH_INT; |
| 70 | sprintf(buf, "%d month%s", num, (num>1) ? "s" : ""); |
| 71 | } |
| 72 | if (secs >= WEEK_INT) { |
| 73 | num = secs / WEEK_INT; |
| 74 | secs -= num*WEEK_INT; |
| 75 | sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "", |
| 76 | num, (num>1) ? "s" : ""); |
| 77 | strcat(buf, tmp); |
| 78 | } |
| 79 | if (secs >= DAY_INT) { |
| 80 | num = secs / DAY_INT; |
| 81 | secs -= num*DAY_INT; |
| 82 | sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "", |
| 83 | num, (num>1) ? "s" : ""); |
| 84 | strcat(buf, tmp); |
| 85 | } |
| 86 | if (secs > 0) { |
| 87 | hr = secs / HOUR_INT; |
| 88 | secs -= hr*HOUR_INT; |
| 89 | min = secs / MINUTE_INT; |
| 90 | secs -= min*MINUTE_INT; |
| 91 | sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "", |
| 92 | hr, min, secs); |
| 93 | strcat(buf, tmp); |
| 94 | } |
| 95 | return buf; |
| 96 | } |
| 97 | |
| 98 | static void print_features(struct ext2_super_block * s, FILE *f) |
| 99 | { |
| 100 | #ifdef EXT2_DYNAMIC_REV |
| 101 | int i, j, printed=0; |
| 102 | __u32 *mask = &s->s_feature_compat, m; |
| 103 | |
| 104 | fprintf(f, "Filesystem features: "); |
| 105 | for (i=0; i <3; i++,mask++) { |
| 106 | for (j=0,m=1; j < 32; j++, m<<=1) { |
| 107 | if (*mask & m) { |
| 108 | fprintf(f, " %s", e2p_feature2string(i, m)); |
| 109 | printed++; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | if (printed == 0) |
| 114 | fprintf(f, " (none)"); |
| 115 | fprintf(f, "\n"); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | static void print_mntopts(struct ext2_super_block * s, FILE *f) |
| 120 | { |
| 121 | #ifdef EXT2_DYNAMIC_REV |
| 122 | int i, printed=0; |
| 123 | __u32 mask = s->s_default_mount_opts, m; |
| 124 | |
| 125 | fprintf(f, "Default mount options: "); |
| 126 | if (mask & EXT3_DEFM_JMODE) { |
| 127 | fprintf(f, " %s", e2p_mntopt2string(mask & EXT3_DEFM_JMODE)); |
| 128 | printed++; |
| 129 | } |
| 130 | for (i=0,m=1; i < 32; i++, m<<=1) { |
| 131 | if (m & EXT3_DEFM_JMODE) |
| 132 | continue; |
| 133 | if (mask & m) { |
| 134 | fprintf(f, " %s", e2p_mntopt2string(m)); |
| 135 | printed++; |
| 136 | } |
| 137 | } |
| 138 | if (printed == 0) |
| 139 | fprintf(f, " (none)"); |
| 140 | fprintf(f, "\n"); |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | static void print_super_flags(struct ext2_super_block * s, FILE *f) |
| 145 | { |
| 146 | int flags_found = 0; |
| 147 | |
| 148 | if (s->s_flags == 0) |
| 149 | return; |
| 150 | |
| 151 | fputs("Filesystem flags: ", f); |
| 152 | if (s->s_flags & EXT2_FLAGS_SIGNED_HASH) { |
| 153 | fputs("signed_directory_hash ", f); |
| 154 | flags_found++; |
| 155 | } |
| 156 | if (s->s_flags & EXT2_FLAGS_UNSIGNED_HASH) { |
| 157 | fputs("unsigned_directory_hash ", f); |
| 158 | flags_found++; |
| 159 | } |
| 160 | if (s->s_flags & EXT2_FLAGS_TEST_FILESYS) { |
| 161 | fputs("test_filesystem ", f); |
| 162 | flags_found++; |
| 163 | } |
| 164 | if (flags_found) |
| 165 | fputs("\n", f); |
| 166 | else |
| 167 | fputs("(none)\n", f); |
| 168 | } |
| 169 | |
| 170 | static __u64 e2p_blocks_count(struct ext2_super_block *super) |
| 171 | { |
| 172 | return super->s_blocks_count | |
| 173 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 174 | (__u64) super->s_blocks_count_hi << 32 : 0); |
| 175 | } |
| 176 | |
| 177 | static __u64 e2p_r_blocks_count(struct ext2_super_block *super) |
| 178 | { |
| 179 | return super->s_r_blocks_count | |
| 180 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 181 | (__u64) super->s_r_blocks_count_hi << 32 : 0); |
| 182 | } |
| 183 | |
| 184 | static __u64 e2p_free_blocks_count(struct ext2_super_block *super) |
| 185 | { |
| 186 | return super->s_free_blocks_count | |
| 187 | (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ? |
| 188 | (__u64) super->s_free_blocks_hi << 32 : 0); |
| 189 | } |
| 190 | |
| 191 | #ifndef EXT2_INODE_SIZE |
| 192 | #define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode) |
| 193 | #endif |
| 194 | |
| 195 | #ifndef EXT2_GOOD_OLD_REV |
| 196 | #define EXT2_GOOD_OLD_REV 0 |
| 197 | #endif |
| 198 | |
| 199 | void list_super2(struct ext2_super_block * sb, FILE *f) |
| 200 | { |
| 201 | int inode_blocks_per_group; |
| 202 | char buf[80], *str; |
| 203 | time_t tm; |
| 204 | |
| 205 | inode_blocks_per_group = (((sb->s_inodes_per_group * |
| 206 | EXT2_INODE_SIZE(sb)) + |
| 207 | EXT2_BLOCK_SIZE(sb) - 1) / |
| 208 | EXT2_BLOCK_SIZE(sb)); |
| 209 | if (sb->s_volume_name[0]) { |
| 210 | memset(buf, 0, sizeof(buf)); |
| 211 | strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name)); |
| 212 | } else |
| 213 | strcpy(buf, "<none>"); |
| 214 | fprintf(f, "Filesystem volume name: %s\n", buf); |
| 215 | if (sb->s_last_mounted[0]) { |
| 216 | memset(buf, 0, sizeof(buf)); |
| 217 | strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted)); |
| 218 | } else |
| 219 | strcpy(buf, "<not available>"); |
| 220 | fprintf(f, "Last mounted on: %s\n", buf); |
| 221 | fprintf(f, "Filesystem UUID: %s\n", e2p_uuid2str(sb->s_uuid)); |
| 222 | fprintf(f, "Filesystem magic number: 0x%04X\n", sb->s_magic); |
| 223 | fprintf(f, "Filesystem revision #: %d", sb->s_rev_level); |
| 224 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV) { |
| 225 | fprintf(f, " (original)\n"); |
| 226 | #ifdef EXT2_DYNAMIC_REV |
| 227 | } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) { |
| 228 | fprintf(f, " (dynamic)\n"); |
| 229 | #endif |
| 230 | } else |
| 231 | fprintf(f, " (unknown)\n"); |
| 232 | print_features(sb, f); |
| 233 | print_super_flags(sb, f); |
| 234 | print_mntopts(sb, f); |
| 235 | if (sb->s_mount_opts[0]) |
| 236 | fprintf(f, "Mount options: %s\n", sb->s_mount_opts); |
| 237 | fprintf(f, "Filesystem state: "); |
| 238 | print_fs_state (f, sb->s_state); |
| 239 | fprintf(f, "\n"); |
| 240 | fprintf(f, "Errors behavior: "); |
| 241 | print_fs_errors(f, sb->s_errors); |
| 242 | fprintf(f, "\n"); |
| 243 | str = e2p_os2string(sb->s_creator_os); |
| 244 | fprintf(f, "Filesystem OS type: %s\n", str); |
| 245 | free(str); |
| 246 | fprintf(f, "Inode count: %u\n", sb->s_inodes_count); |
| 247 | fprintf(f, "Block count: %llu\n", e2p_blocks_count(sb)); |
| 248 | fprintf(f, "Reserved block count: %llu\n", e2p_r_blocks_count(sb)); |
| 249 | if (sb->s_overhead_blocks) |
| 250 | fprintf(f, "Overhead blocks: %u\n", |
| 251 | sb->s_overhead_blocks); |
| 252 | fprintf(f, "Free blocks: %llu\n", e2p_free_blocks_count(sb)); |
| 253 | fprintf(f, "Free inodes: %u\n", sb->s_free_inodes_count); |
| 254 | fprintf(f, "First block: %u\n", sb->s_first_data_block); |
| 255 | fprintf(f, "Block size: %u\n", EXT2_BLOCK_SIZE(sb)); |
| 256 | if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) |
| 257 | fprintf(f, "Cluster size: %u\n", |
| 258 | EXT2_CLUSTER_SIZE(sb)); |
| 259 | else |
| 260 | fprintf(f, "Fragment size: %u\n", |
| 261 | EXT2_CLUSTER_SIZE(sb)); |
| 262 | if (sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
| 263 | fprintf(f, "Group descriptor size: %u\n", sb->s_desc_size); |
| 264 | if (sb->s_reserved_gdt_blocks) |
| 265 | fprintf(f, "Reserved GDT blocks: %u\n", |
| 266 | sb->s_reserved_gdt_blocks); |
| 267 | fprintf(f, "Blocks per group: %u\n", sb->s_blocks_per_group); |
| 268 | if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) |
| 269 | fprintf(f, "Clusters per group: %u\n", |
| 270 | sb->s_clusters_per_group); |
| 271 | else |
| 272 | fprintf(f, "Fragments per group: %u\n", |
| 273 | sb->s_clusters_per_group); |
| 274 | fprintf(f, "Inodes per group: %u\n", sb->s_inodes_per_group); |
| 275 | fprintf(f, "Inode blocks per group: %u\n", inode_blocks_per_group); |
| 276 | if (sb->s_raid_stride) |
| 277 | fprintf(f, "RAID stride: %u\n", |
| 278 | sb->s_raid_stride); |
| 279 | if (sb->s_raid_stripe_width) |
| 280 | fprintf(f, "RAID stripe width: %u\n", |
| 281 | sb->s_raid_stripe_width); |
| 282 | if (sb->s_first_meta_bg) |
| 283 | fprintf(f, "First meta block group: %u\n", |
| 284 | sb->s_first_meta_bg); |
| 285 | if (sb->s_log_groups_per_flex) |
| 286 | fprintf(f, "Flex block group size: %u\n", |
| 287 | 1 << sb->s_log_groups_per_flex); |
| 288 | if (sb->s_mkfs_time) { |
| 289 | tm = sb->s_mkfs_time; |
| 290 | fprintf(f, "Filesystem created: %s", ctime(&tm)); |
| 291 | } |
| 292 | tm = sb->s_mtime; |
| 293 | fprintf(f, "Last mount time: %s", |
| 294 | sb->s_mtime ? ctime(&tm) : "n/a\n"); |
| 295 | tm = sb->s_wtime; |
| 296 | fprintf(f, "Last write time: %s", ctime(&tm)); |
| 297 | fprintf(f, "Mount count: %u\n", sb->s_mnt_count); |
| 298 | fprintf(f, "Maximum mount count: %d\n", sb->s_max_mnt_count); |
| 299 | tm = sb->s_lastcheck; |
| 300 | fprintf(f, "Last checked: %s", ctime(&tm)); |
| 301 | fprintf(f, "Check interval: %u (%s)\n", sb->s_checkinterval, |
| 302 | interval_string(sb->s_checkinterval)); |
| 303 | if (sb->s_checkinterval) |
| 304 | { |
| 305 | time_t next; |
| 306 | |
| 307 | next = sb->s_lastcheck + sb->s_checkinterval; |
| 308 | fprintf(f, "Next check after: %s", ctime(&next)); |
| 309 | } |
| 310 | #define POW2(x) ((__u64) 1 << (x)) |
| 311 | if (sb->s_kbytes_written) { |
| 312 | fprintf(f, "Lifetime writes: "); |
| 313 | if (sb->s_kbytes_written < POW2(13)) |
| 314 | fprintf(f, "%llu kB\n", sb->s_kbytes_written); |
| 315 | else if (sb->s_kbytes_written < POW2(23)) |
| 316 | fprintf(f, "%llu MB\n", |
| 317 | (sb->s_kbytes_written + POW2(9)) >> 10); |
| 318 | else if (sb->s_kbytes_written < POW2(33)) |
| 319 | fprintf(f, "%llu GB\n", |
| 320 | (sb->s_kbytes_written + POW2(19)) >> 20); |
| 321 | else if (sb->s_kbytes_written < POW2(43)) |
| 322 | fprintf(f, "%llu TB\n", |
| 323 | (sb->s_kbytes_written + POW2(29)) >> 30); |
| 324 | else |
| 325 | fprintf(f, "%llu PB\n", |
| 326 | (sb->s_kbytes_written + POW2(39)) >> 40); |
| 327 | } |
| 328 | fprintf(f, "Reserved blocks uid: "); |
| 329 | print_user(sb->s_def_resuid, f); |
| 330 | fprintf(f, "Reserved blocks gid: "); |
| 331 | print_group(sb->s_def_resgid, f); |
| 332 | if (sb->s_rev_level >= EXT2_DYNAMIC_REV) { |
| 333 | fprintf(f, "First inode: %d\n", sb->s_first_ino); |
| 334 | fprintf(f, "Inode size: %d\n", sb->s_inode_size); |
| 335 | if (sb->s_min_extra_isize) |
| 336 | fprintf(f, "Required extra isize: %d\n", |
| 337 | sb->s_min_extra_isize); |
| 338 | if (sb->s_want_extra_isize) |
| 339 | fprintf(f, "Desired extra isize: %d\n", |
| 340 | sb->s_want_extra_isize); |
| 341 | } |
| 342 | if (!e2p_is_null_uuid(sb->s_journal_uuid)) |
| 343 | fprintf(f, "Journal UUID: %s\n", |
| 344 | e2p_uuid2str(sb->s_journal_uuid)); |
| 345 | if (sb->s_journal_inum) |
| 346 | fprintf(f, "Journal inode: %u\n", |
| 347 | sb->s_journal_inum); |
| 348 | if (sb->s_journal_dev) |
| 349 | fprintf(f, "Journal device: 0x%04x\n", |
| 350 | sb->s_journal_dev); |
| 351 | if (sb->s_last_orphan) |
| 352 | fprintf(f, "First orphan inode: %u\n", |
| 353 | sb->s_last_orphan); |
| 354 | if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || |
| 355 | sb->s_def_hash_version) |
| 356 | fprintf(f, "Default directory hash: %s\n", |
| 357 | e2p_hash2string(sb->s_def_hash_version)); |
| 358 | if (!e2p_is_null_uuid(sb->s_hash_seed)) |
| 359 | fprintf(f, "Directory Hash Seed: %s\n", |
| 360 | e2p_uuid2str(sb->s_hash_seed)); |
| 361 | if (sb->s_jnl_backup_type) { |
| 362 | fprintf(f, "Journal backup: "); |
| 363 | switch (sb->s_jnl_backup_type) { |
| 364 | case 1: |
| 365 | fprintf(f, "inode blocks\n"); |
| 366 | break; |
| 367 | default: |
| 368 | fprintf(f, "type %u\n", sb->s_jnl_backup_type); |
| 369 | } |
| 370 | } |
| 371 | if (sb->s_snapshot_inum) { |
| 372 | fprintf(f, "Snapshot inode: %u\n", |
| 373 | sb->s_snapshot_inum); |
| 374 | fprintf(f, "Snapshot ID: %u\n", |
| 375 | sb->s_snapshot_id); |
| 376 | fprintf(f, "Snapshot reserved blocks: %llu\n", |
| 377 | sb->s_snapshot_r_blocks_count); |
| 378 | } |
| 379 | if (sb->s_snapshot_list) |
| 380 | fprintf(f, "Snapshot list head: %u\n", |
| 381 | sb->s_snapshot_list); |
| 382 | if (sb->s_error_count) |
| 383 | fprintf(f, "FS Error count: %u\n", |
| 384 | sb->s_error_count); |
| 385 | if (sb->s_first_error_time) { |
| 386 | tm = sb->s_first_error_time; |
| 387 | fprintf(f, "First error time: %s", ctime(&tm)); |
| 388 | memset(buf, 0, sizeof(buf)); |
| 389 | strncpy(buf, (char *)sb->s_first_error_func, |
| 390 | sizeof(sb->s_first_error_func)); |
| 391 | fprintf(f, "First error function: %s\n", buf); |
| 392 | fprintf(f, "First error line #: %u\n", |
| 393 | sb->s_first_error_line); |
| 394 | fprintf(f, "First error inode #: %u\n", |
| 395 | sb->s_first_error_ino); |
| 396 | fprintf(f, "First error block #: %llu\n", |
| 397 | sb->s_first_error_block); |
| 398 | } |
| 399 | if (sb->s_last_error_time) { |
| 400 | tm = sb->s_last_error_time; |
| 401 | fprintf(f, "Last error time: %s", ctime(&tm)); |
| 402 | memset(buf, 0, sizeof(buf)); |
| 403 | strncpy(buf, (char *)sb->s_last_error_func, |
| 404 | sizeof(sb->s_last_error_func)); |
| 405 | fprintf(f, "Last error function: %s\n", buf); |
| 406 | fprintf(f, "Last error line #: %u\n", |
| 407 | sb->s_last_error_line); |
| 408 | fprintf(f, "Last error inode #: %u\n", |
| 409 | sb->s_last_error_ino); |
| 410 | fprintf(f, "Last error block #: %llu\n", |
| 411 | sb->s_last_error_block); |
| 412 | } |
| 413 | if (sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) { |
| 414 | fprintf(f, "MMP block number: %llu\n", |
| 415 | (long long)sb->s_mmp_block); |
| 416 | fprintf(f, "MMP update interval: %u\n", |
| 417 | sb->s_mmp_update_interval); |
| 418 | } |
| 419 | if (sb->s_usr_quota_inum) |
| 420 | fprintf(f, "User quota inode: %u\n", |
| 421 | sb->s_usr_quota_inum); |
| 422 | if (sb->s_grp_quota_inum) |
| 423 | fprintf(f, "Group quota inode: %u\n", |
| 424 | sb->s_grp_quota_inum); |
| 425 | |
| 426 | if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) |
| 427 | fprintf(f, "Checksum: 0x%08x\n", |
| 428 | sb->s_checksum); |
| 429 | } |
| 430 | |
| 431 | void list_super (struct ext2_super_block * s) |
| 432 | { |
| 433 | list_super2(s, stdout); |
| 434 | } |
| 435 | |