blob: 5544e256ad2879d923b85bb1d3368dc4497952f2 [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 */
76 __u32 s_reserved[206]; /* Padding to the end of the block */
77};
78
Theodore Ts'o521e3681997-04-29 17:48:10 +000079#ifndef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
80#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
81#endif
82
Theodore Ts'of3db3561997-04-26 13:34:30 +000083static void print_user (unsigned short uid)
84{
85 struct passwd *pw;
86
87 printf ("%u ", uid);
88 pw = getpwuid (uid);
89 if (pw == NULL)
90 printf ("(user unknown)\n");
91 else
92 printf ("(user %s)\n", pw->pw_name);
93}
94
95static void print_group (unsigned short gid)
96{
97 struct group *gr;
98
99 printf ("%u ", gid);
100 gr = getgrgid (gid);
101 if (gr == NULL)
102 printf ("(group unknown)\n");
103 else
104 printf ("(group %s)\n", gr->gr_name);
105}
106
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000107#define MONTH_INT (86400 * 30)
108#define WEEK_INT (86400 * 7)
109#define DAY_INT (86400)
110#define HOUR_INT (60 * 60)
111#define MINUTE_INT (60)
112
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113static const char *interval_string(unsigned int secs)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000114{
115 static char buf[256], tmp[80];
116 int hr, min, num;
117
118 buf[0] = 0;
119
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000120 if (secs == 0)
121 return "<none>";
122
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000123 if (secs >= MONTH_INT) {
124 num = secs / MONTH_INT;
125 secs -= num*MONTH_INT;
126 sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
127 }
128 if (secs >= WEEK_INT) {
129 num = secs / WEEK_INT;
130 secs -= num*WEEK_INT;
131 sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
132 num, (num>1) ? "s" : "");
133 strcat(buf, tmp);
134 }
135 if (secs >= DAY_INT) {
136 num = secs / DAY_INT;
137 secs -= num*DAY_INT;
138 sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
139 num, (num>1) ? "s" : "");
140 strcat(buf, tmp);
141 }
142 if (secs > 0) {
143 hr = secs / HOUR_INT;
144 secs -= hr*HOUR_INT;
145 min = secs / MINUTE_INT;
146 secs -= min*MINUTE_INT;
147 sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
148 hr, min, secs);
149 strcat(buf, tmp);
150 }
151 return buf;
152}
153
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000154static void print_features(struct ext2_super_block * s)
155{
156#ifdef EXT2_DYNAMIC_REV
157 int i, j, printed=0;
158 __u32 *mask = &s->s_feature_compat, m;
159
160 printf ("Filesystem features: ");
161 for (i=0; i <3; i++,mask++) {
162 for (j=0,m=1; j < 32; j++, m<<=1) {
163 if (*mask & m) {
164 printf(" %s", e2p_feature2string(i, m));
165 printed++;
166 }
167 }
168 }
169 if (printed == 0)
170 printf("(none)");
171 printf("\n");
172#endif
173}
174
175
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000176
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000177#ifndef EXT2_INODE_SIZE
178#define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
179#endif
180
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000181#ifndef EXT2_GOOD_OLD_REV
182#define EXT2_GOOD_OLD_REV 0
183#endif
184
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185void list_super (struct ext2_super_block * s)
186{
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000187 int inode_blocks_per_group;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000188 struct ext2fs_sb *sb = (struct ext2fs_sb *) s;
189 char buf[80];
190 const char *os;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000191 time_t tm;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000192
193 inode_blocks_per_group = (((s->s_inodes_per_group *
194 EXT2_INODE_SIZE(s)) +
195 EXT2_BLOCK_SIZE(s) - 1) /
196 EXT2_BLOCK_SIZE(s));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000197 if (sb->s_volume_name[0]) {
198 memset(buf, 0, sizeof(buf));
199 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000200 } else
201 strcpy(buf, "<none>");
202 printf("Filesystem volume name: %s\n", buf);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000203 if (sb->s_last_mounted[0]) {
204 memset(buf, 0, sizeof(buf));
205 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000206 } else
207 strcpy(buf, "<not available>");
208 printf("Last mounted on: %s\n", buf);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000209 if (!e2p_is_null_uuid(sb->s_uuid)) {
210 e2p_uuid_to_str(sb->s_uuid, buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000211 } else
212 strcpy(buf, "<none>");
213 printf("Filesystem UUID: %s\n", buf);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000214 printf ("Filesystem magic number: 0x%04X\n", s->s_magic);
215 printf ("Filesystem revision #: %d", s->s_rev_level);
216 if (s->s_rev_level == EXT2_GOOD_OLD_REV) {
217 printf(" (original)\n");
218#ifdef EXT2_DYNAMIC_REV
219 } else if (s->s_rev_level == EXT2_DYNAMIC_REV) {
220 printf(" (dynamic)\n");
221#endif
222 } else
223 printf("\n");
Theodore Ts'od7b701d1999-09-14 20:17:38 +0000224 print_features(s);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 printf ("Filesystem state: ");
226 print_fs_state (stdout, s->s_state);
227 printf ("\n");
228 printf ("Errors behavior: ");
229 print_fs_errors (stdout, s->s_errors);
230 printf ("\n");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000231 switch (s->s_creator_os) {
232 case EXT2_OS_LINUX: os = "Linux"; break;
233 case EXT2_OS_HURD: os = "GNU"; break;
234 case EXT2_OS_MASIX: os = "Masix"; break;
235 default: os = "unknown"; break;
236 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000237 printf("Filesystem OS type: %s\n", os);
238 printf("Inode count: %u\n", s->s_inodes_count);
239 printf("Block count: %u\n", s->s_blocks_count);
240 printf("Reserved block count: %u\n", s->s_r_blocks_count);
241 printf("Free blocks: %u\n", s->s_free_blocks_count);
242 printf("Free inodes: %u\n", s->s_free_inodes_count);
243 printf("First block: %u\n", s->s_first_data_block);
244 printf("Block size: %u\n", EXT2_BLOCK_SIZE(s));
245 printf("Fragment size: %u\n", EXT2_FRAG_SIZE(s));
246 printf("Blocks per group: %u\n", s->s_blocks_per_group);
247 printf("Fragments per group: %u\n", s->s_frags_per_group);
248 printf("Inodes per group: %u\n", s->s_inodes_per_group);
249 printf("Inode blocks per group: %u\n", inode_blocks_per_group);
250 tm = s->s_mtime;
251 printf("Last mount time: %s", ctime(&tm));
252 tm = s->s_wtime;
253 printf("Last write time: %s", ctime(&tm));
254 printf("Mount count: %u\n", s->s_mnt_count);
255 printf("Maximum mount count: %d\n", s->s_max_mnt_count);
256 tm = s->s_lastcheck;
257 printf("Last checked: %s", ctime(&tm));
258 printf("Check interval: %u (%s)\n", s->s_checkinterval,
259 interval_string(s->s_checkinterval));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260 if (s->s_checkinterval)
261 {
262 time_t next;
263
264 next = s->s_lastcheck + s->s_checkinterval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000265 printf("Next check after: %s", ctime(&next));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000267#ifdef EXT2_DEF_RESUID
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000268 printf("Reserved blocks uid: ");
269 print_user(s->s_def_resuid);
270 printf("Reserved blocks gid: ");
271 print_group(s->s_def_resgid);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272#endif
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000273#ifdef EXT2_DYNAMIC_REV
274 if (s->s_rev_level >= EXT2_DYNAMIC_REV) {
275 printf("First inode: %d\n", s->s_first_ino);
276 printf("Inode size: %d\n", s->s_inode_size);
277 }
278#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000280
281
282
283