• 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 buf[80], *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 		memset(buf, 0, sizeof(buf));
237 		strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
238 	} else
239 		strcpy(buf, "<none>");
240 	fprintf(f, "Filesystem volume name:   %s\n", buf);
241 	if (sb->s_last_mounted[0]) {
242 		memset(buf, 0, sizeof(buf));
243 		strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
244 	} else
245 		strcpy(buf, "<not available>");
246 	fprintf(f, "Last mounted on:          %s\n", buf);
247 	fprintf(f, "Filesystem UUID:          %s\n", e2p_uuid2str(sb->s_uuid));
248 	fprintf(f, "Filesystem magic number:  0x%04X\n", sb->s_magic);
249 	fprintf(f, "Filesystem revision #:    %d", sb->s_rev_level);
250 	if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
251 		fprintf(f, " (original)\n");
252 #ifdef EXT2_DYNAMIC_REV
253 	} else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
254 		fprintf(f, " (dynamic)\n");
255 #endif
256 	} else
257 		fprintf(f, " (unknown)\n");
258 	print_features(sb, f);
259 	print_super_flags(sb, f);
260 	print_mntopts(sb, f);
261 	if (sb->s_mount_opts[0])
262 		fprintf(f, "Mount options:            %s\n", sb->s_mount_opts);
263 	fprintf(f, "Filesystem state:        ");
264 	print_fs_state (f, sb->s_state);
265 	fprintf(f, "\n");
266 	fprintf(f, "Errors behavior:          ");
267 	print_fs_errors(f, sb->s_errors);
268 	fprintf(f, "\n");
269 	str = e2p_os2string(sb->s_creator_os);
270 	fprintf(f, "Filesystem OS type:       %s\n", str);
271 	free(str);
272 	fprintf(f, "Inode count:              %u\n", sb->s_inodes_count);
273 	fprintf(f, "Block count:              %llu\n", e2p_blocks_count(sb));
274 	fprintf(f, "Reserved block count:     %llu\n", e2p_r_blocks_count(sb));
275 	if (sb->s_overhead_blocks)
276 		fprintf(f, "Overhead blocks:          %u\n",
277 			sb->s_overhead_blocks);
278 	fprintf(f, "Free blocks:              %llu\n", e2p_free_blocks_count(sb));
279 	fprintf(f, "Free inodes:              %u\n", sb->s_free_inodes_count);
280 	fprintf(f, "First block:              %u\n", sb->s_first_data_block);
281 	fprintf(f, "Block size:               %u\n", EXT2_BLOCK_SIZE(sb));
282 	if (ext2fs_has_feature_bigalloc(sb))
283 		fprintf(f, "Cluster size:             %u\n",
284 			EXT2_CLUSTER_SIZE(sb));
285 	else
286 		fprintf(f, "Fragment size:            %u\n",
287 			EXT2_CLUSTER_SIZE(sb));
288 	if (ext2fs_has_feature_64bit(sb))
289 		fprintf(f, "Group descriptor size:    %u\n", sb->s_desc_size);
290 	if (sb->s_reserved_gdt_blocks)
291 		fprintf(f, "Reserved GDT blocks:      %u\n",
292 			sb->s_reserved_gdt_blocks);
293 	fprintf(f, "Blocks per group:         %u\n", sb->s_blocks_per_group);
294 	if (ext2fs_has_feature_bigalloc(sb))
295 		fprintf(f, "Clusters per group:       %u\n",
296 			sb->s_clusters_per_group);
297 	else
298 		fprintf(f, "Fragments per group:      %u\n",
299 			sb->s_clusters_per_group);
300 	fprintf(f, "Inodes per group:         %u\n", sb->s_inodes_per_group);
301 	fprintf(f, "Inode blocks per group:   %u\n", inode_blocks_per_group);
302 	if (sb->s_raid_stride)
303 		fprintf(f, "RAID stride:              %u\n",
304 			sb->s_raid_stride);
305 	if (sb->s_raid_stripe_width)
306 		fprintf(f, "RAID stripe width:        %u\n",
307 			sb->s_raid_stripe_width);
308 	if (sb->s_first_meta_bg)
309 		fprintf(f, "First meta block group:   %u\n",
310 			sb->s_first_meta_bg);
311 	if (sb->s_log_groups_per_flex)
312 		fprintf(f, "Flex block group size:    %u\n",
313 			1 << sb->s_log_groups_per_flex);
314 	if (sb->s_mkfs_time) {
315 		tm = sb->s_mkfs_time;
316 		fprintf(f, "Filesystem created:       %s", ctime(&tm));
317 	}
318 	tm = sb->s_mtime;
319 	fprintf(f, "Last mount time:          %s",
320 		sb->s_mtime ? ctime(&tm) : "n/a\n");
321 	tm = sb->s_wtime;
322 	fprintf(f, "Last write time:          %s", ctime(&tm));
323 	fprintf(f, "Mount count:              %u\n", sb->s_mnt_count);
324 	fprintf(f, "Maximum mount count:      %d\n", sb->s_max_mnt_count);
325 	tm = sb->s_lastcheck;
326 	fprintf(f, "Last checked:             %s", ctime(&tm));
327 	fprintf(f, "Check interval:           %u (%s)\n", sb->s_checkinterval,
328 	       interval_string(sb->s_checkinterval));
329 	if (sb->s_checkinterval)
330 	{
331 		time_t next;
332 
333 		next = sb->s_lastcheck + sb->s_checkinterval;
334 		fprintf(f, "Next check after:         %s", ctime(&next));
335 	}
336 #define POW2(x) ((__u64) 1 << (x))
337 	if (sb->s_kbytes_written) {
338 		fprintf(f, "Lifetime writes:          ");
339 		if (sb->s_kbytes_written < POW2(13))
340 			fprintf(f, "%llu kB\n", sb->s_kbytes_written);
341 		else if (sb->s_kbytes_written < POW2(23))
342 			fprintf(f, "%llu MB\n",
343 				(sb->s_kbytes_written + POW2(9)) >> 10);
344 		else if (sb->s_kbytes_written < POW2(33))
345 			fprintf(f, "%llu GB\n",
346 				(sb->s_kbytes_written + POW2(19)) >> 20);
347 		else if (sb->s_kbytes_written < POW2(43))
348 			fprintf(f, "%llu TB\n",
349 				(sb->s_kbytes_written + POW2(29)) >> 30);
350 		else
351 			fprintf(f, "%llu PB\n",
352 				(sb->s_kbytes_written + POW2(39)) >> 40);
353 	}
354 	fprintf(f, "Reserved blocks uid:      ");
355 	print_user(sb->s_def_resuid, f);
356 	fprintf(f, "Reserved blocks gid:      ");
357 	print_group(sb->s_def_resgid, f);
358 	if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
359 		fprintf(f, "First inode:              %d\n", sb->s_first_ino);
360 		fprintf(f, "Inode size:	          %d\n", sb->s_inode_size);
361 		if (sb->s_min_extra_isize)
362 			fprintf(f, "Required extra isize:     %d\n",
363 				sb->s_min_extra_isize);
364 		if (sb->s_want_extra_isize)
365 			fprintf(f, "Desired extra isize:      %d\n",
366 				sb->s_want_extra_isize);
367 	}
368 	if (!e2p_is_null_uuid(sb->s_journal_uuid))
369 		fprintf(f, "Journal UUID:             %s\n",
370 			e2p_uuid2str(sb->s_journal_uuid));
371 	if (sb->s_journal_inum)
372 		fprintf(f, "Journal inode:            %u\n",
373 			sb->s_journal_inum);
374 	if (sb->s_journal_dev)
375 		fprintf(f, "Journal device:	          0x%04x\n",
376 			sb->s_journal_dev);
377 	if (sb->s_last_orphan)
378 		fprintf(f, "First orphan inode:       %u\n",
379 			sb->s_last_orphan);
380 	if (ext2fs_has_feature_dir_index(sb) ||
381 	    sb->s_def_hash_version)
382 		fprintf(f, "Default directory hash:   %s\n",
383 			e2p_hash2string(sb->s_def_hash_version));
384 	if (!e2p_is_null_uuid(sb->s_hash_seed))
385 		fprintf(f, "Directory Hash Seed:      %s\n",
386 			e2p_uuid2str(sb->s_hash_seed));
387 	if (sb->s_jnl_backup_type) {
388 		fprintf(f, "Journal backup:           ");
389 		switch (sb->s_jnl_backup_type) {
390 		case 1:
391 			fprintf(f, "inode blocks\n");
392 			break;
393 		default:
394 			fprintf(f, "type %u\n", sb->s_jnl_backup_type);
395 		}
396 	}
397 	if (sb->s_backup_bgs[0] || sb->s_backup_bgs[1]) {
398 		fprintf(f, "Backup block groups:      ");
399 		if (sb->s_backup_bgs[0])
400 			fprintf(f, "%u ", sb->s_backup_bgs[0]);
401 		if (sb->s_backup_bgs[1])
402 			fprintf(f, "%u ", sb->s_backup_bgs[1]);
403 		fputc('\n', f);
404 	}
405 	if (sb->s_snapshot_inum) {
406 		fprintf(f, "Snapshot inode:           %u\n",
407 			sb->s_snapshot_inum);
408 		fprintf(f, "Snapshot ID:              %u\n",
409 			sb->s_snapshot_id);
410 		fprintf(f, "Snapshot reserved blocks: %llu\n",
411 			sb->s_snapshot_r_blocks_count);
412 	}
413 	if (sb->s_snapshot_list)
414 		fprintf(f, "Snapshot list head:       %u\n",
415 			sb->s_snapshot_list);
416 	if (sb->s_error_count)
417 		fprintf(f, "FS Error count:           %u\n",
418 			sb->s_error_count);
419 	if (sb->s_first_error_time) {
420 		tm = sb->s_first_error_time;
421 		fprintf(f, "First error time:         %s", ctime(&tm));
422 		memset(buf, 0, sizeof(buf));
423 		strncpy(buf, (char *)sb->s_first_error_func,
424 			sizeof(sb->s_first_error_func));
425 		fprintf(f, "First error function:     %s\n", buf);
426 		fprintf(f, "First error line #:       %u\n",
427 			sb->s_first_error_line);
428 		fprintf(f, "First error inode #:      %u\n",
429 			sb->s_first_error_ino);
430 		fprintf(f, "First error block #:      %llu\n",
431 			sb->s_first_error_block);
432 	}
433 	if (sb->s_last_error_time) {
434 		tm = sb->s_last_error_time;
435 		fprintf(f, "Last error time:          %s", ctime(&tm));
436 		memset(buf, 0, sizeof(buf));
437 		strncpy(buf, (char *)sb->s_last_error_func,
438 			sizeof(sb->s_last_error_func));
439 		fprintf(f, "Last error function:      %s\n", buf);
440 		fprintf(f, "Last error line #:        %u\n",
441 			sb->s_last_error_line);
442 		fprintf(f, "Last error inode #:       %u\n",
443 			sb->s_last_error_ino);
444 		fprintf(f, "Last error block #:       %llu\n",
445 			sb->s_last_error_block);
446 	}
447 	if (ext2fs_has_feature_mmp(sb)) {
448 		fprintf(f, "MMP block number:         %llu\n",
449 			(long long)sb->s_mmp_block);
450 		fprintf(f, "MMP update interval:      %u\n",
451 			sb->s_mmp_update_interval);
452 	}
453 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
454 		if (*quota_sb_inump(sb, qtype) != 0)
455 			fprintf(f, "%-26s%u\n",
456 				quota_type2prefix(qtype),
457 				*quota_sb_inump(sb, qtype));
458 	}
459 
460 	if (ext2fs_has_feature_metadata_csum(sb)) {
461 		fprintf(f, "Checksum type:            %s\n",
462 			checksum_type(sb->s_checksum_type));
463 		fprintf(f, "Checksum:                 0x%08x\n",
464 			sb->s_checksum);
465 	}
466 	if (!e2p_is_null_uuid(sb->s_encrypt_pw_salt))
467 		fprintf(f, "Encryption PW Salt:       %s\n",
468 			e2p_uuid2str(sb->s_encrypt_pw_salt));
469 
470 	if (ext2fs_has_feature_csum_seed(sb))
471 		fprintf(f, "Checksum seed:            0x%08x\n",
472 			sb->s_checksum_seed);
473 }
474 
list_super(struct ext2_super_block * s)475 void list_super (struct ext2_super_block * s)
476 {
477 	list_super2(s, stdout);
478 }
479 
480