Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/sbin/dumpsfs/dumpsfs.c
734 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
#include <sys/types.h>
31
#include <stdint.h>
32
#include <string.h>
33
#include <stdio.h>
34
#include <assert.h>
35
#include <limits.h>
36
#include <err.h>
37
38
#include "support.h"
39
#include "kern/sfs.h"
40
41
42
#ifdef HOST
43
44
#include <netinet/in.h> // for arpa/inet.h
45
#include <arpa/inet.h> // for ntohl
46
#include "hostcompat.h"
47
#define SWAPL(x) ntohl(x)
48
#define SWAPS(x) ntohs(x)
49
50
#else
51
52
#define SWAPL(x) (x)
53
#define SWAPS(x) (x)
54
55
#endif
56
57
#include "disk.h"
58
59
static
60
uint32_t
61
dumpsb(void)
62
{
63
struct sfs_super sp;
64
diskread(&sp, SFS_SB_LOCATION);
65
if (SWAPL(sp.sp_magic) != SFS_MAGIC) {
66
errx(1, "Not an sfs filesystem");
67
}
68
sp.sp_volname[sizeof(sp.sp_volname)-1] = 0;
69
printf("Volume name: %-40s %u blocks\n", sp.sp_volname,
70
SWAPL(sp.sp_nblocks));
71
72
return SWAPL(sp.sp_nblocks);
73
}
74
75
static
76
void
77
dodirblock(uint32_t block)
78
{
79
struct sfs_dir sds[SFS_BLOCKSIZE/sizeof(struct sfs_dir)];
80
int nsds = SFS_BLOCKSIZE/sizeof(struct sfs_dir);
81
int i;
82
83
diskread(&sds, block);
84
85
printf(" [block %u]\n", block);
86
for (i=0; i<nsds; i++) {
87
uint32_t ino = SWAPL(sds[i].sfd_ino);
88
if (ino==SFS_NOINO) {
89
printf(" [free entry]\n");
90
}
91
else {
92
sds[i].sfd_name[SFS_NAMELEN-1] = 0; /* just in case */
93
printf(" %u %s\n", ino, sds[i].sfd_name);
94
}
95
}
96
}
97
98
static
99
void
100
dumpdir(uint32_t ino)
101
{
102
struct sfs_inode sfi;
103
uint32_t ib[SFS_DBPERIDB];
104
int nentries, i;
105
uint32_t block, nblocks=0;
106
107
diskread(&sfi, ino);
108
109
nentries = SWAPL(sfi.sfi_size) / sizeof(struct sfs_dir);
110
if (SWAPL(sfi.sfi_size) % sizeof(struct sfs_dir) != 0) {
111
warnx("Warning: dir size is not a multiple of dir entry size");
112
}
113
printf("Directory %u: %d entries\n", ino, nentries);
114
115
for (i=0; i<SFS_NDIRECT; i++) {
116
block = SWAPL(sfi.sfi_direct[i]);
117
if (block) {
118
dodirblock(block);
119
nblocks++;
120
}
121
}
122
if (SWAPL(sfi.sfi_indirect)) {
123
diskread(&ib, SWAPL(sfi.sfi_indirect));
124
for (i=0; i<SFS_DBPERIDB; i++) {
125
block = SWAPL(ib[i]);
126
if (block) {
127
dodirblock(block);
128
nblocks++;
129
}
130
}
131
}
132
printf(" %u blocks in directory\n", nblocks);
133
}
134
135
static
136
void
137
dumpbits(uint32_t fsblocks)
138
{
139
uint32_t nblocks = SFS_BITBLOCKS(fsblocks);
140
uint32_t i, j;
141
char data[SFS_BLOCKSIZE];
142
143
printf("Freemap: %u blocks (%u %u %u)\n", nblocks, SFS_BITMAPSIZE(fsblocks), fsblocks, SFS_BLOCKBITS);
144
145
for (i=0; i<nblocks; i++) {
146
diskread(data, SFS_MAP_LOCATION+i);
147
for (j=0; j<SFS_BLOCKSIZE; j++) {
148
printf("%02x", (unsigned char)data[j]);
149
if (j%32==31) {
150
printf("\n");
151
}
152
}
153
}
154
printf("\n");
155
}
156
157
int
158
main(int argc, char **argv)
159
{
160
uint32_t nblocks;
161
162
#ifdef HOST
163
hostcompat_init(argc, argv);
164
#endif
165
166
if (argc!=2) {
167
errx(1, "Usage: dumpsfs device/diskfile");
168
}
169
170
opendisk(argv[1]);
171
nblocks = dumpsb();
172
dumpbits(nblocks);
173
dumpdir(SFS_ROOT_LOCATION);
174
175
closedisk();
176
177
return 0;
178
}
179
180