blob: ee83ab64c3ec1284bb27adcbbc23ba81cce98244 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +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 *
8 * Copyright (C) 1995, 1996, 1997 Theodore Ts'o <tytso@mit.edu>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00009 *
Mike Frysingerd89e6292005-04-24 05:07:59 +000010 * This file can be redistributed under the terms of the GNU Library General
11 * Public License
12 */
13
14#include <stdio.h>
Mike Frysinger874af852006-03-08 07:03:27 +000015#include <stdlib.h>
Mike Frysingerd89e6292005-04-24 05:07:59 +000016#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
Mike Frysinger85cffcc2005-06-11 00:08:50 +000024static void print_user(unsigned short uid, FILE *f)
Mike Frysingerd89e6292005-04-24 05:07:59 +000025{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000026 struct passwd *pw = getpwuid(uid);
27 fprintf(f, "%u (user %s)\n", uid,
28 (pw == NULL ? "unknown" : pw->pw_name));
Mike Frysingerd89e6292005-04-24 05:07:59 +000029}
30
Mike Frysinger85cffcc2005-06-11 00:08:50 +000031static void print_group(unsigned short gid, FILE *f)
Mike Frysingerd89e6292005-04-24 05:07:59 +000032{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000033 struct group *gr = getgrgid(gid);
34 fprintf(f, "%u (group %s)\n", gid,
35 (gr == NULL ? "unknown" : gr->gr_name));
Mike Frysingerd89e6292005-04-24 05:07:59 +000036}
37
38#define MONTH_INT (86400 * 30)
39#define WEEK_INT (86400 * 7)
40#define DAY_INT (86400)
41#define HOUR_INT (60 * 60)
42#define MINUTE_INT (60)
43
44static const char *interval_string(unsigned int secs)
45{
46 static char buf[256], tmp[80];
47 int hr, min, num;
48
49 buf[0] = 0;
50
51 if (secs == 0)
52 return "<none>";
53
54 if (secs >= MONTH_INT) {
55 num = secs / MONTH_INT;
56 secs -= num*MONTH_INT;
57 sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
58 }
59 if (secs >= WEEK_INT) {
60 num = secs / WEEK_INT;
61 secs -= num*WEEK_INT;
62 sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
63 num, (num>1) ? "s" : "");
64 strcat(buf, tmp);
65 }
66 if (secs >= DAY_INT) {
67 num = secs / DAY_INT;
68 secs -= num*DAY_INT;
69 sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
70 num, (num>1) ? "s" : "");
71 strcat(buf, tmp);
72 }
73 if (secs > 0) {
74 hr = secs / HOUR_INT;
75 secs -= hr*HOUR_INT;
76 min = secs / MINUTE_INT;
77 secs -= min*MINUTE_INT;
78 sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
79 hr, min, secs);
80 strcat(buf, tmp);
81 }
82 return buf;
83}
84
85static void print_features(struct ext2_super_block * s, FILE *f)
86{
87#ifdef EXT2_DYNAMIC_REV
88 int i, j, printed=0;
89 __u32 *mask = &s->s_feature_compat, m;
90
91 fprintf(f, "Filesystem features: ");
92 for (i=0; i <3; i++,mask++) {
93 for (j=0,m=1; j < 32; j++, m<<=1) {
94 if (*mask & m) {
95 fprintf(f, " %s", e2p_feature2string(i, m));
96 printed++;
97 }
98 }
99 }
100 if (printed == 0)
101 fprintf(f, " (none)");
102 fprintf(f, "\n");
103#endif
104}
105
106static void print_mntopts(struct ext2_super_block * s, FILE *f)
107{
108#ifdef EXT2_DYNAMIC_REV
109 int i, printed=0;
110 __u32 mask = s->s_default_mount_opts, m;
111
112 fprintf(f, "Default mount options: ");
113 if (mask & EXT3_DEFM_JMODE) {
114 fprintf(f, " %s", e2p_mntopt2string(mask & EXT3_DEFM_JMODE));
115 printed++;
116 }
117 for (i=0,m=1; i < 32; i++, m<<=1) {
118 if (m & EXT3_DEFM_JMODE)
119 continue;
120 if (mask & m) {
121 fprintf(f, " %s", e2p_mntopt2string(m));
122 printed++;
123 }
124 }
125 if (printed == 0)
126 fprintf(f, " (none)");
127 fprintf(f, "\n");
128#endif
129}
130
131
132#ifndef EXT2_INODE_SIZE
133#define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
134#endif
135
136#ifndef EXT2_GOOD_OLD_REV
137#define EXT2_GOOD_OLD_REV 0
138#endif
139
140void list_super2(struct ext2_super_block * sb, FILE *f)
141{
142 int inode_blocks_per_group;
143 char buf[80], *str;
144 time_t tm;
145
146 inode_blocks_per_group = (((sb->s_inodes_per_group *
147 EXT2_INODE_SIZE(sb)) +
148 EXT2_BLOCK_SIZE(sb) - 1) /
149 EXT2_BLOCK_SIZE(sb));
150 if (sb->s_volume_name[0]) {
151 memset(buf, 0, sizeof(buf));
152 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
153 } else
154 strcpy(buf, "<none>");
155 fprintf(f, "Filesystem volume name: %s\n", buf);
156 if (sb->s_last_mounted[0]) {
157 memset(buf, 0, sizeof(buf));
158 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
159 } else
160 strcpy(buf, "<not available>");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000161 fprintf(f,
162 "Last mounted on: %s\n"
163 "Filesystem UUID: %s\n"
164 "Filesystem magic number: 0x%04X\n"
165 "Filesystem revision #: %d",
166 buf, e2p_uuid2str(sb->s_uuid), sb->s_magic, sb->s_rev_level);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000167 if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
168 fprintf(f, " (original)\n");
169#ifdef EXT2_DYNAMIC_REV
170 } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
171 fprintf(f, " (dynamic)\n");
172#endif
173 } else
174 fprintf(f, " (unknown)\n");
175 print_features(sb, f);
176 print_mntopts(sb, f);
177 fprintf(f, "Filesystem state: ");
178 print_fs_state (f, sb->s_state);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000179 fprintf(f, "\nErrors behavior: ");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000180 print_fs_errors(f, sb->s_errors);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000181 str = e2p_os2string(sb->s_creator_os);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000182 fprintf(f,
183 "\n"
184 "Filesystem OS type: %s\n"
185 "Inode count: %u\n"
186 "Block count: %u\n"
187 "Reserved block count: %u\n"
188 "Free blocks: %u\n"
189 "Free inodes: %u\n"
190 "First block: %u\n"
191 "Block size: %u\n"
192 "Fragment size: %u\n",
193 str, sb->s_inodes_count, sb->s_blocks_count, sb->s_r_blocks_count,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000194 sb->s_free_blocks_count, sb->s_free_inodes_count,
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000195 sb->s_first_data_block, EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
Mike Frysingerd89e6292005-04-24 05:07:59 +0000196 free(str);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000197 if (sb->s_reserved_gdt_blocks)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000198 fprintf(f, "Reserved GDT blocks: %u\n",
Mike Frysingerd89e6292005-04-24 05:07:59 +0000199 sb->s_reserved_gdt_blocks);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000200 fprintf(f,
201 "Blocks per group: %u\n"
202 "Fragments per group: %u\n"
203 "Inodes per group: %u\n"
204 "Inode blocks per group: %u\n",
205 sb->s_blocks_per_group, sb->s_frags_per_group,
206 sb->s_inodes_per_group, inode_blocks_per_group);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000207 if (sb->s_first_meta_bg)
208 fprintf(f, "First meta block group: %u\n",
209 sb->s_first_meta_bg);
210 if (sb->s_mkfs_time) {
211 tm = sb->s_mkfs_time;
212 fprintf(f, "Filesystem created: %s", ctime(&tm));
213 }
214 tm = sb->s_mtime;
215 fprintf(f, "Last mount time: %s",
216 sb->s_mtime ? ctime(&tm) : "n/a\n");
217 tm = sb->s_wtime;
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000218 fprintf(f,
219 "Last write time: %s"
220 "Mount count: %u\n"
221 "Maximum mount count: %d\n",
222 ctime(&tm), sb->s_mnt_count, sb->s_max_mnt_count);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000223 tm = sb->s_lastcheck;
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000224 fprintf(f,
225 "Last checked: %s"
226 "Check interval: %u (%s)\n",
227 ctime(&tm),
228 sb->s_checkinterval, interval_string(sb->s_checkinterval));
Mike Frysingerd89e6292005-04-24 05:07:59 +0000229 if (sb->s_checkinterval)
230 {
231 time_t next;
232
233 next = sb->s_lastcheck + sb->s_checkinterval;
234 fprintf(f, "Next check after: %s", ctime(&next));
235 }
236 fprintf(f, "Reserved blocks uid: ");
237 print_user(sb->s_def_resuid, f);
238 fprintf(f, "Reserved blocks gid: ");
239 print_group(sb->s_def_resgid, f);
240 if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000241 fprintf(f,
242 "First inode: %d\n"
243 "Inode size: %d\n",
244 sb->s_first_ino, sb->s_inode_size);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000245 }
246 if (!e2p_is_null_uuid(sb->s_journal_uuid))
247 fprintf(f, "Journal UUID: %s\n",
248 e2p_uuid2str(sb->s_journal_uuid));
249 if (sb->s_journal_inum)
250 fprintf(f, "Journal inode: %u\n",
251 sb->s_journal_inum);
252 if (sb->s_journal_dev)
253 fprintf(f, "Journal device: 0x%04x\n",
254 sb->s_journal_dev);
255 if (sb->s_last_orphan)
256 fprintf(f, "First orphan inode: %u\n",
257 sb->s_last_orphan);
258 if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
259 sb->s_def_hash_version)
260 fprintf(f, "Default directory hash: %s\n",
261 e2p_hash2string(sb->s_def_hash_version));
262 if (!e2p_is_null_uuid(sb->s_hash_seed))
263 fprintf(f, "Directory Hash Seed: %s\n",
264 e2p_uuid2str(sb->s_hash_seed));
265 if (sb->s_jnl_backup_type) {
266 fprintf(f, "Journal backup: ");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000267 if (sb->s_jnl_backup_type == 1)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000268 fprintf(f, "inode blocks\n");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000269 else
Mike Frysingerd89e6292005-04-24 05:07:59 +0000270 fprintf(f, "type %u\n", sb->s_jnl_backup_type);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000271 }
272}