This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock2file.rb
executable file
·225 lines (200 loc) · 5.23 KB
/
block2file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env ruby
# vim: set ai ts=2 sw=2:
require 'date'
require 'ostruct'
require 'rexml/document'
# TODO break into libraries and utilities
def OpenStruct.nested(hash)
OpenStruct.new(hash.inject({}) {|r,p|
r[p[0]] = p[1].kind_of?(Hash) ? OpenStruct.nested(p[1]) : p[1]; r
})
end
def ffsinfo(dev)
IO.popen "ffsinfo -l 1 #{dev}" do |io|
kw = {}
stack = []
io.each_line do |line|
case line
when /^#/
when /^=+ START (.*) =+$/
stack.push kw
case io.readline
when /^# (\d+)@(\S+): (\S+) (\S+)/
kw = kw[$4] = {}
end
when /^=+ END (.*) =+$/
kw = stack.pop
when /^(\S+)[^"]+\s+"(.*)"$/ # string
kw[$1] = $2
when /^(\S+).*\]\S+(.+)/ # array
kw[$1] = $2.split.map {|i| Integer i }
when /^(\S+).*\s(.+)$/ # int
kw[$1] = Integer $2
end
end
OpenStruct.nested kw
end
end
class GEOM
# TODO follow @ref
# TODO convert to hash with inject
include REXML
def initialize
@gmesh = REXML::Document.new `sysctl -b kern.geom.confxml`.strip
end
def provider(name)
Obj.new XPath.first(@gmesh, '//provider[name=$name]', nil, 'name' => name)
end
private
class Obj
def initialize(node)
@node = node
end
def method_missing(m, *args, &block)
# XXX child = REXML::XPath.first(@node, '$m', nil, 'm' => m)
child = REXML::XPath.first(@node, m.to_s)
if child.has_elements? then
Obj.new child
else
begin
Integer child.text
rescue ArgumentError
child.text
end
end
end
end
end
$gmesh = GEOM.new
class FSInfo
# underlying device (geom name)
attr_reader :dev
# current mount point
attr_reader :mountpoint
# sector size of geom provider
attr_reader :gsectorsize
# filesystem superblock
attr_reader :fs
# Cache instances since REXML is dog slow
@@CACHED = {}
# @param geomdev short gom device name
# @return cached FSInfo
def FSInfo.get(geomdev)
@@CACHED[geomdev] ||= FSInfo.new(geomdev)
end
# @param geom device name
def initialize(geomdev)
@dev = geomdev
@fs = ffsinfo(dev).sblock
@mountpoint = fs.fsmnt # XXX is it current or just previous mountpoint ?
@gprovider = $gmesh.provider(dev)
@gsectorsize = 512 # XXX why @gprovider.sectorsize is 4096 ?
end
# from ufs/ffs/fs.h:
def fsbtodb(b)
b << fs.fsbtodb
end
def dbtofsb(b)
b >> fs.fsbtodb
end
def fragnum(fsb)
# fsb % fs.frag
fsb & (fs.frag - 1)
end
def blknum(fsb)
# rounddown(fsb, fs.frag)
fsb &~ (fs.frag - 1)
end
def dputs(*args)
puts(*args) if $DEBUG
end
# @param offset in bytes
# @return disk block
def offset2diskblock(offset)
dputs "off #{offset} blk +#{offset % fs.bsize}"
# logical block number
lbn = offset / gsectorsize
dputs "lbn #{lbn}"
# disk block this lbn lies in
db = blknum(lbn)
dputs "db #{db}"
# convert to fs block
fsb = dbtofsb(db)
dputs "fsb #{fsb}"
# add fragment number of this lbn
fsb += fragnum(lbn)
dputs "fragnum #{fragnum(lbn)}"
# convert back to disk block
db = fsbtodb(fsb)
dputs "db #{db}"
dputs
db
end
# @param disk_block number (up to 32)
# @return map of blocks to inodes
# TODO move to separate class FSDB and keep process working between calls
# XXX reading before exit blocks no matter what, even with PTY.spawn, why?
def findblk(*disk_blocks)
IO.popen "fsdb -r /dev/#{dev}",'w+' do |fsdb|
# TODO fsdb.puts "help" and grab commands
fsdb.puts "findblk #{disk_blocks.join ' '}"
fsdb.puts "exit"
results = {}
fsdb.each_line do |line|
case line
when /(\d+): data block of inode (\d+)/
results[Integer $1] = Integer $2
end
end
results
end
end
# @param list of inodes
# @return list of paths
def findpaths(inodes)
return [] if inodes.empty?
`find -x #{fs.fsmnt} \\( -inum #{inodes.join ' -or -inum '} \\) -print0`.split "\0"
end
end
GeomError = Struct.new(:date,:geom,:op,:off,:len)
FSDB_FINDBLK_MAXARGC = 32
# GEOM_FOO: g_foo_read_done() failed ad0s1d[READ(offset=123456, length=512)]
RE_GEOMERR = /(GEOM_\S+): (\S+) failed (\S+)\[(\S+)\(offset=(\d+), length=(\d+)\)\]/
//x if false # XXX fix vim indent
errors = []
while gets do
for gclass,fun,geom,op,off,len in scan(RE_GEOMERR) do
begin # try to parse date like in syslog
date = DateTime.strptime($_, '%b %e %T')
rescue ArgumentError
end
errors << GeomError.new(date, geom, op, Integer(off), Integer(len))
end
end
for geom,gerrors in errors.group_by {|e|e.geom} do
puts "GEOM #{geom}"
fsinfo = FSInfo.get(geom)
# findblk handles up to 32 blocks per run
# Each offset+length is unique location
# Maybe group by offset and select largest length ?
for loc,lerrors in gerrors.group_by {|e|[e.off,e.len]} do
off,len = loc
puts "ERROR OFFSET #{off} SIZE #{len} COUNT #{lerrors.length}"
end
inodes = {}
errbyloc = gerrors.group_by {|e|e.off}
errbyloc.keys.sort.each_slice(FSDB_FINDBLK_MAXARGC) do |offsets|
dblocks = offsets.map{|o| fsinfo.offset2diskblock(o)}
puts "FINDINODE BLOCKS #{dblocks.join ' '}"
inodes.merge! fsinfo.findblk(dblocks)
end
inums = inodes.values.uniq.sort
puts "FINDPATH \"#{fsinfo.mountpoint}\" INODES #{inums.join ' '}"
blocks = inodes.group_by {|blk,ino|ino}
paths = fsinfo.findpaths inums
for path in paths.sort do
inode = File.stat(path).ino
blks = blocks[inode].map{|blk,ino|blk}
printf "PATH \"%s\" INODE %d BLOCKS %s\n", path, inode, blks.join(' ')
end
end