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