blob: 10bf7326d1d513b85726357cce390cb6ecb3dfe1 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00008 * Copyright (C) 1995, 1996, 1997 Theodore Ts'o <tytso@mit.edu>
9 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 * This file can be redistributed under the terms of the GNU Library General
11 * Public License
12 */
13
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000014#include <stdio.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000015#include <sys/types.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000016#include <string.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <grp.h>
18#include <pwd.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <time.h>
20
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include "e2p.h"
22
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000023/*
24 * The ext2fs library private definition of the ext2 superblock, so we
25 * don't have to depend on the kernel's definition of the superblock,
26 * which might not have the latest features.
27 */
28struct ext2fs_sb {
29 __u32 s_inodes_count; /* Inodes count */
30 __u32 s_blocks_count; /* Blocks count */
31 __u32 s_r_blocks_count; /* Reserved blocks count */
32 __u32 s_free_blocks_count; /* Free blocks count */
33 __u32 s_free_inodes_count; /* Free inodes count */
34 __u32 s_first_data_block; /* First Data Block */
35 __u32 s_log_block_size; /* Block size */
36 __s32 s_log_frag_size; /* Fragment size */
37 __u32 s_blocks_per_group; /* # Blocks per group */
38 __u32 s_frags_per_group; /* # Fragments per group */
39 __u32 s_inodes_per_group; /* # Inodes per group */
40 __u32 s_mtime; /* Mount time */
41 __u32 s_wtime; /* Write time */
42 __u16 s_mnt_count; /* Mount count */
43 __s16 s_max_mnt_count; /* Maximal mount count */
44 __u16 s_magic; /* Magic signature */
45 __u16 s_state; /* File system state */
46 __u16 s_errors; /* Behaviour when detecting errors */
47 __u16 s_minor_rev_level; /* minor revision level */
48 __u32 s_lastcheck; /* time of last check */
49 __u32 s_checkinterval; /* max. time between checks */
50 __u32 s_creator_os; /* OS */
51 __u32 s_rev_level; /* Revision level */
52 __u16 s_def_resuid; /* Default uid for reserved blocks */
53 __u16 s_def_resgid; /* Default gid for reserved blocks */
54 /*
55 * These fields are for EXT2_DYNAMIC_REV superblocks only.
56 *
57 * Note: the difference between the compatible feature set and
58 * the incompatible feature set is that if there is a bit set
59 * in the incompatible feature set that the kernel doesn't
60 * know about, it should refuse to mount the filesystem.
61 *
62 * e2fsck's requirements are more strict; if it doesn't know
63 * about a feature in either the compatible or incompatible
64 * feature set, it must abort and not try to meddle with
65 * things it doesn't understand...
66 */
67 __u32 s_first_ino; /* First non-reserved inode */
68 __u16 s_inode_size; /* size of inode structure */
69 __u16 s_block_group_nr; /* block group # of this superblock */
70 __u32 s_feature_compat; /* compatible feature set */
71 __u32 s_feature_incompat; /* incompatible feature set */
72 __u32 s_feature_ro_compat; /* readonly-compatible feature set */
73 __u8 s_uuid[16]; /* 128-bit uuid for volume */
74 char s_volume_name[16]; /* volume name */
75 char s_last_mounted[64]; /* directory where last mounted */
Theodore Ts'o19c68912000-07-07 03:25:13 +000076 __u32 s_algorithm_usage_bitmap; /* For compression */
77 /*
78 * Performance hints. Directory preallocation should only
79 * happen if the EXT2_FEATURE_COMPAT_DIR_PREALLOC flag is on.
80 */
81 __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
82 __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
83 __u16 s_padding1;
84 /*
85 * Journaling support.
86 */
87 __u8 s_journal_uuid[16]; /* uuid of journal superblock */
88 __u32 s_journal_inum; /* inode number of journal file */
89 __u32 s_journal_dev; /* device number of journal file */
90 __u32 s_last_orphan; /* start of list of inodes to delete */
91
92 __u32 s_reserved[197]; /* Padding to the end of the block */
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000093};
94
Theodore Ts'o521e3681997-04-29 17:48:10 +000095#ifndef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
96#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
97#endif
98
Theodore Ts'o53d39552000-08-14 20:13:32 +000099static void print_user (unsigned short uid, FILE *f)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000100{
101 struct passwd *pw;
102
Theodore Ts'o53d39552000-08-14 20:13:32 +0000103 fprintf(f, "%u ", uid);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000104 pw = getpwuid (uid);
105 if (pw == NULL)
Theodore Ts'o53d39552000-08-14 20:13:32 +0000106 fprintf(f, "(user unknown)\n");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107 else
Theodore Ts'o53d39552000-08-14 20:13:32 +0000108 fprintf(f, "(user %s)\n", pw->pw_name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000109}
110
Theodore Ts'o53d39552000-08-14 20:13:32 +0000111static void print_group (unsigned short gid, FILE *f)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112{
113 struct group *gr;
114
Theodore Ts'o53d39552000-08-14 20:13:32 +0000115 fprintf(f, "%u ", gid);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 gr = getgrgid (gid);
117 if (gr == NULL)
Theodore Ts'o53d39552000-08-14 20:13:32 +0000118 fprintf(f, "(group unknown)\n");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119 else
Theodore Ts'o53d39552000-08-14 20:13:32 +0000120 fprintf(f, "(group %s)\n", gr->gr_name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121}
122
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000123#define MONTH_INT (86400 * 30)
124#define WEEK_INT (86400 * 7)
125#define DAY_INT (86400)
126#define HOUR_INT (60 * 60)
127#define MINUTE_INT (60)
128
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000129static const char *interval_string(unsigned int secs)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000130{
131 static char buf[256], tmp[80];
132 int hr, min, num;
133
134 buf[0] = 0;
135
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000136 if (secs == 0)
137 return "<none>";
138
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000139 if (secs >= MONTH_INT) {
140 num = secs / MONTH_INT;
141 secs -= num*MONTH_INT;
142 sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
143 }
144 if (secs >= WEEK_INT) {
145 num = secs / WEEK_INT;
146 secs -= num*WEEK_INT;
147 sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
148 num, (num>1) ? "s" : "");
149 strcat(buf, tmp);
150 }
151 if (secs >= DAY_INT) {
152 num = secs / DAY_INT;
153 secs -= num*DAY_INT;
154 sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
155 num, (num>1) ? "s" : "");
156 strcat(buf, tmp);
157 }
158 if (secs > 0) {
159 hr = secs / HOUR_INT;
160 secs -= hr*HOUR_INT;
161 min = secs / MINUTE_INT;
162 secs -= min*MINUTE_INT;
163 sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
164 hr, min, secs);
165 strcat(buf, tmp);
166 }
167 return buf;
168}
169
Theodore Ts'o53d39552000-08-14 20:13:32 +0000170static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000171{
172#ifdef EXT2_DYNAMIC_REV
173 int i, j, printed=0;
174 __u32 *mask = &s->s_feature_compat, m;
175
Theodore Ts'o53d39552000-08-14 20:13:32 +0000176 fprintf(f, "Filesystem features: ");
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000177 for (i=0; i <3; i++,mask++) {
178 for (j=0,m=1; j < 32; j++, m<<=1) {
179 if (*mask & m) {
Theodore Ts'o53d39552000-08-14 20:13:32 +0000180 fprintf(f, " %s", e2p_feature2string(i, m));
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000181 printed++;
182 }
183 }
184 }
185 if (printed == 0)
Theodore Ts'o53d39552000-08-14 20:13:32 +0000186 fprintf(f, "(none)");
187 fprintf(f, "\n");
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000188#endif
189}
190
191
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000192
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000193#ifndef EXT2_INODE_SIZE
194#define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
195#endif
196
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000197#ifndef EXT2_GOOD_OLD_REV
198#define EXT2_GOOD_OLD_REV 0
199#endif
200
Theodore Ts'o53d39552000-08-14 20:13:32 +0000201void list_super2(struct ext2_super_block * s, FILE *f)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000203 int inode_blocks_per_group;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000204 struct ext2fs_sb *sb = (struct ext2fs_sb *) s;
205 char buf[80];
206 const char *os;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000207 time_t tm;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000208
209 inode_blocks_per_group = (((s->s_inodes_per_group *
210 EXT2_INODE_SIZE(s)) +
211 EXT2_BLOCK_SIZE(s) - 1) /
212 EXT2_BLOCK_SIZE(s));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000213 if (sb->s_volume_name[0]) {
214 memset(buf, 0, sizeof(buf));
215 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000216 } else
217 strcpy(buf, "<none>");
Theodore Ts'o53d39552000-08-14 20:13:32 +0000218 fprintf(f, "Filesystem volume name: %s\n", buf);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000219 if (sb->s_last_mounted[0]) {
220 memset(buf, 0, sizeof(buf));
221 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000222 } else
223 strcpy(buf, "<not available>");
Theodore Ts'o53d39552000-08-14 20:13:32 +0000224 fprintf(f, "Last mounted on: %s\n", buf);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000225 if (!e2p_is_null_uuid(sb->s_uuid)) {
226 e2p_uuid_to_str(sb->s_uuid, buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000227 } else
228 strcpy(buf, "<none>");
Theodore Ts'o53d39552000-08-14 20:13:32 +0000229 fprintf(f, "Filesystem UUID: %s\n", buf);
230 fprintf(f, "Filesystem magic number: 0x%04X\n", s->s_magic);
231 fprintf(f, "Filesystem revision #: %d", s->s_rev_level);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000232 if (s->s_rev_level == EXT2_GOOD_OLD_REV) {
Theodore Ts'o53d39552000-08-14 20:13:32 +0000233 fprintf(f, " (original)\n");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000234#ifdef EXT2_DYNAMIC_REV
235 } else if (s->s_rev_level == EXT2_DYNAMIC_REV) {
Theodore Ts'o53d39552000-08-14 20:13:32 +0000236 fprintf(f, " (dynamic)\n");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000237#endif
238 } else
Theodore Ts'o53d39552000-08-14 20:13:32 +0000239 fprintf(f, "\n");
240 print_features(s, f);
241 fprintf(f, "Filesystem state: ");
242 print_fs_state (f, s->s_state);
243 fprintf(f, "\n");
244 fprintf(f, "Errors behavior: ");
245 print_fs_errors(f, s->s_errors);
246 fprintf(f, "\n");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000247 switch (s->s_creator_os) {
248 case EXT2_OS_LINUX: os = "Linux"; break;
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000249 case EXT2_OS_HURD: os = "GNU/Hurd"; break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000250 case EXT2_OS_MASIX: os = "Masix"; break;
251 default: os = "unknown"; break;
252 }
Theodore Ts'o53d39552000-08-14 20:13:32 +0000253 fprintf(f, "Filesystem OS type: %s\n", os);
254 fprintf(f, "Inode count: %u\n", s->s_inodes_count);
255 fprintf(f, "Block count: %u\n", s->s_blocks_count);
256 fprintf(f, "Reserved block count: %u\n", s->s_r_blocks_count);
257 fprintf(f, "Free blocks: %u\n", s->s_free_blocks_count);
258 fprintf(f, "Free inodes: %u\n", s->s_free_inodes_count);
259 fprintf(f, "First block: %u\n", s->s_first_data_block);
260 fprintf(f, "Block size: %u\n", EXT2_BLOCK_SIZE(s));
261 fprintf(f, "Fragment size: %u\n", EXT2_FRAG_SIZE(s));
262 fprintf(f, "Blocks per group: %u\n", s->s_blocks_per_group);
263 fprintf(f, "Fragments per group: %u\n", s->s_frags_per_group);
264 fprintf(f, "Inodes per group: %u\n", s->s_inodes_per_group);
265 fprintf(f, "Inode blocks per group: %u\n", inode_blocks_per_group);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000266 tm = s->s_mtime;
Theodore Ts'o53d39552000-08-14 20:13:32 +0000267 fprintf(f, "Last mount time: %s", ctime(&tm));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000268 tm = s->s_wtime;
Theodore Ts'o53d39552000-08-14 20:13:32 +0000269 fprintf(f, "Last write time: %s", ctime(&tm));
270 fprintf(f, "Mount count: %u\n", s->s_mnt_count);
271 fprintf(f, "Maximum mount count: %d\n", s->s_max_mnt_count);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000272 tm = s->s_lastcheck;
Theodore Ts'o53d39552000-08-14 20:13:32 +0000273 fprintf(f, "Last checked: %s", ctime(&tm));
274 fprintf(f, "Check interval: %u (%s)\n", s->s_checkinterval,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000275 interval_string(s->s_checkinterval));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 if (s->s_checkinterval)
277 {
278 time_t next;
279
280 next = s->s_lastcheck + s->s_checkinterval;
Theodore Ts'o53d39552000-08-14 20:13:32 +0000281 fprintf(f, "Next check after: %s", ctime(&next));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282 }
Theodore Ts'o53d39552000-08-14 20:13:32 +0000283 fprintf(f, "Reserved blocks uid: ");
284 print_user(s->s_def_resuid, f);
285 fprintf(f, "Reserved blocks gid: ");
286 print_group(s->s_def_resgid, f);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000287 if (s->s_rev_level >= EXT2_DYNAMIC_REV) {
Theodore Ts'o53d39552000-08-14 20:13:32 +0000288 fprintf(f, "First inode: %d\n", s->s_first_ino);
289 fprintf(f, "Inode size: %d\n", s->s_inode_size);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000290 }
Theodore Ts'o19c68912000-07-07 03:25:13 +0000291 if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
292 if (e2p_is_null_uuid(sb->s_journal_uuid)) {
293 strcpy(buf, "<none>");
294 } else
295 e2p_uuid_to_str(sb->s_uuid, buf);
Theodore Ts'o53d39552000-08-14 20:13:32 +0000296 fprintf(f, "Journal UUID: %s\n", buf);
297 fprintf(f, "Journal inode: %u\n", s->s_journal_inum);
298 fprintf(f, "Journal device: 0x%04x\n", s->s_journal_dev);
299 fprintf(f, "First orphan inode: %u\n", s->s_last_orphan);
Theodore Ts'o19c68912000-07-07 03:25:13 +0000300 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000302
Theodore Ts'o53d39552000-08-14 20:13:32 +0000303void list_super (struct ext2_super_block * s)
304{
305 list_super2(s, stdout);
306}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000307