-
Notifications
You must be signed in to change notification settings - Fork 5
/
vcfs_server.rb
603 lines (509 loc) · 12.6 KB
/
vcfs_server.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# An NFS server.
#
# Author: Brian Ollenberger
require 'nfs'
require 'mount'
require 'vcfs'
class VCFSServer
def initialize(vcfs_params, fsid = 0)
@mount_prog = Mount::MOUNTPROG.dup
@mount_vers = Mount::MOUNTVERS
@nfs_prog = NFS::NFS_PROGRAM.dup
@nfs_vers = NFS::NFS_VERSION
if not vcfs_params.kind_of?(Array)
vcfs_params = [vcfs_params]
end
# TODO pool-ize the VCFS so that we can do things concurrently
@vcfs = VCFS::VCFS.new(*vcfs_params)
@fsid = fsid
define_mount_procedures
define_nfs_procedures
end
def auth_unix(auth)
if auth[:flavor] != :AUTH_UNIX
raise Errno::EACCES
end
SUNRPC::Auth_unix.decode(auth[:body])
end
def programs
[@mount_prog, @nfs_prog]
end
def to_fh(branch, file)
[branch, file, ''].pack('N2a24')
end
def from_fh(fh)
branch, file = fh.unpack('N2a24')
if branch == 0 and file == 0
[1, 1]
else
[branch, file]
end
end
def handle_errors
begin
yield
rescue Errno::EPERM
{:_discriminant => :NFSERR_PERM}
rescue Errno::ENOENT
{:_discriminant => :NFSERR_NOENT}
rescue Errno::EIO
{:_discriminant => :NFSERR_IO}
rescue Errno::ENXIO
{:_discriminant => :NFSERR_NXIO}
rescue Errno::EACCES
{:_discriminant => :NFSERR_ACCES}
rescue Errno::EEXIST
{:_discriminant => :NFSERR_EXIST}
rescue Errno::ENODEV
{:_discriminant => :NFSERR_NODEV}
rescue Errno::ENOTDIR
{:_discriminant => :NFSERR_NOTDIR}
rescue Errno::EISDIR
{:_discriminant => :NFSERR_ISDIR}
rescue Errno::EINVAL
{:_discriminant => :NFSERR_INVAL}
rescue Errno::EFBIG
{:_discriminant => :NFSERR_FBIG}
rescue Errno::ENOSPC
{:_discriminant => :NFSERR_NOSPC}
rescue Errno::EROFS
{:_discriminant => :NFSERR_ROFS}
rescue Errno::ENAMETOOLONG
{:_discriminant => :NFSERR_NAMETOOLONG}
rescue Errno::ENOTEMPTY
{:_discriminant => :NFSERR_NOTEMPTY}
rescue Errno::EDQUOT
{:_discriminant => :NFSERR_DQUOT}
rescue Errno::ESTALE
{:_discriminant => :NFSERR_STALE}
rescue => e
# LOG
$stderr.puts e
$stderr.print e.backtrace.join("\n")
{:_discriminant => :NFSERR_IO}
end
end
def define_mount_procedures
@mount_prog.on_call(@mount_vers, :MNT) do |arg, auth, verf|
puts 'MNT'
puts arg.inspect
if arg == '/'
{
:_discriminant => :NFS_OK,
:fhs_fhandle => {
:data => to_fh(1, 1)
}
}
else
{:_discriminant => :NFSERR_ACCES}
end
end
@mount_prog.on_call(@mount_vers, :DUMP) do |arg, auth, verf|
puts 'DUMP'
puts arg.inspect
nil
end
@mount_prog.on_call(@mount_vers, :UMNT) do |arg, auth, verf|
puts 'UMNT'
puts arg.inspect
# do nothing
end
@mount_prog.on_call(@mount_vers, :UMNTALL) do |arg, auth, verf|
puts 'UMNTALL'
puts arg.inspect
# do nothing
end
export = proc do |arg, auth, verf|
puts 'EXPORT'
puts arg.inspect
{
:ex_dir => '/',
:ex_groups => nil,
:ex_next => nil
}
end
@mount_prog.on_call(@mount_vers, :EXPORT, &export)
@mount_prog.on_call(@mount_vers, :EXPORTALL, &export)
end
# Convert Ruby File::Stat(-like) object to an NFS fattr
def convert_attrs(attrs)
type = :NFNON
mode = attrs.mode
if attrs.file?
type = :NFREG
mode |= NFS::MODE_REG
elsif attrs.directory?
type = :NFDIR
mode |= NFS::MODE_DIR
elsif attrs.blockdev?
type = :NFBLK
mode |= NFS::MODE_BLK
elsif attrs.chardev?
type = :NFCHR
mode |= NFS::MODE_CHR
elsif attrs.symlink?
type = :NFLNK
mode |= NFS::MODE_LNK
elsif attrs.socket?
type = :NFSOCK
mode |= NFS::MODE_SOCK
end
{
:type => type,
:mode => mode,
:nlink => attrs.nlink,
:uid => attrs.uid,
:gid => attrs.gid,
:size => attrs.size,
:blocksize => attrs.blksize,
:rdev => attrs.rdev,
:blocks => attrs.blocks,
:fsid => @fsid,
:fileid => attrs.ino,
:atime => {
:seconds => attrs.atime.tv_sec,
:useconds => attrs.atime.tv_usec
},
:mtime => {
:seconds => attrs.mtime.tv_sec,
:useconds => attrs.mtime.tv_usec
},
:ctime => {
:seconds => attrs.ctime.tv_sec,
:useconds => attrs.ctime.tv_usec
}
}
end
def define_nfs_procedures
@nfs_prog.on_call(@nfs_vers, :GETATTR) do |arg, auth, verf|
puts 'GETATTR'
puts arg.inspect
handle_errors do
attrs = @vcfs.getattr(*from_fh(arg[:data]))
{
:_discriminant => :NFS_OK,
:attributes => convert_attrs(attrs)
}
end
end
@nfs_prog.on_call(@nfs_vers, :SETATTR) do |arg, auth, verf|
puts 'SETATTR'
puts arg.inspect
handle_errors do
# Get -1 represented as an unsigned integer. The sattr fields
# are -1 to represent that they should not be changed.
neg_one = 4294967295
if arg[:attributes][:mode] == neg_one
arg[:attributes][:mode] = nil
end
if arg[:attributes][:uid] == neg_one
arg[:attributes][:uid] = nil
end
if arg[:attributes][:gid] == neg_one
arg[:attributes][:gid] = nil
end
if arg[:attributes][:size] == neg_one
arg[:attributes][:size] = nil
end
if arg[:attributes][:atime][:seconds] == neg_one
arg[:attributes][:atime] = nil
else
arg[:attributes][:atime] = Time.at(
arg[:attributes][:atime][:seconds],
arg[:attributes][:atime][:useconds])
end
if arg[:attributes][:mtime][:seconds] == neg_one
arg[:attributes][:mtime] = nil
else
arg[:attributes][:mtime] = Time.at(
arg[:attributes][:mtime][:seconds],
arg[:attributes][:mtime][:useconds])
end
branch, file = from_fh(arg[:file][:data])
attrs = @vcfs.setattr(branch, file,
arg[:attributes][:mode],
arg[:attributes][:uid],
arg[:attributes][:gid],
arg[:attributes][:size],
arg[:attributes][:atime],
arg[:attributes][:mtime])
{
:_discriminant => :NFS_OK,
:attributes => convert_attrs(attrs)
}
end
end
@nfs_prog.on_call(@nfs_vers, :ROOT) do |arg, auth, verf|
puts 'ROOT'
puts arg.inspect
# obsolete
end
@nfs_prog.on_call(@nfs_vers, :LOOKUP) do |arg, auth, verf|
puts 'LOOKUP'
puts arg.inspect
handle_errors do
branch, dir = from_fh(arg[:dir][:data])
file = @vcfs.lookup(branch, dir, arg[:name])
attrs = @vcfs.getattr(branch, file)
result = {
:_discriminant => :NFS_OK,
:diropres => {
:file => {
:data => to_fh(branch, file)
},
:attributes => convert_attrs(attrs)
}
}
result
end
end
@nfs_prog.on_call(@nfs_vers, :READLINK) do |arg, auth, verf|
puts 'READLINK'
puts arg.inspect
handle_errors do
branch, file = from_fh(arg[:data])
from = 0
length = 1024
result = ''
s = @vcfs.read(branch, file, from, length)
puts s
while s.size > 0
result += s
from += length
if s.size < length
break
end
s = @vcfs.read(branch, file, from, length)
end
puts result
{
:_discriminant => :NFS_OK,
:data => result
}
end
end
@nfs_prog.on_call(@nfs_vers, :READ) do |arg, auth, verf|
puts 'READ'
puts arg.inspect
handle_errors do
branch, file = from_fh(arg[:file][:data])
result = @vcfs.read(branch, file, arg[:offset], arg[:count])
attrs = @vcfs.getattr(branch, file)
{
:_discriminant => :NFS_OK,
:reply => {
:attributes => convert_attrs(attrs),
:data => result
}
}
end
end
@nfs_prog.on_call(@nfs_vers, :WRITECACHE) do |arg, auth, verf|
puts 'WRITECACHE'
puts arg.inspect
# do nothing
end
@nfs_prog.on_call(@nfs_vers, :WRITE) do |arg, auth, verf|
puts 'WRITE'
#puts arg.inspect
handle_errors do
branch, file = from_fh(arg[:file][:data])
@vcfs.write(branch, file, arg[:offset], arg[:data])
attrs = @vcfs.getattr(branch, file)
{
:_discriminant => :NFS_OK,
:attributes => convert_attrs(attrs)
}
end
end
@nfs_prog.on_call(@nfs_vers, :CREATE) do |arg, auth, verf|
puts 'CREATE'
puts arg.inspect
handle_errors do
auth = auth_unix(auth)
neg_one = 4294967295
uid = arg[:attributes][:uid]
if uid == neg_one
uid = auth[:uid]
end
gid = arg[:attributes][:gid]
if gid == neg_one
gid = auth[:gid]
end
branch, dir = from_fh(arg[:where][:dir][:data])
file = @vcfs.mkfile(branch, dir, arg[:where][:name],
arg[:attributes][:mode], uid, gid)
attrs = @vcfs.getattr(branch, file)
{
:_discriminant => :NFS_OK,
:diropres => {
:file => {
:data => to_fh(branch, file)
},
:attributes => convert_attrs(attrs)
}
}
end
end
@nfs_prog.on_call(@nfs_vers, :REMOVE) do |arg, auth, verf|
puts 'REMOVE'
puts arg.inspect
(handle_errors do
branch, dir = from_fh(arg[:dir][:data])
@vcfs.unlink(branch, dir, arg[:name])
{:_discriminant => :NFS_OK}
end)[:_discriminant]
end
@nfs_prog.on_call(@nfs_vers, :RENAME) do |arg, auth, verf|
puts 'RENAME'
puts arg.inspect
(handle_errors do
branch, from_dir = from_fh(arg[:from][:dir][:data])
to_branch, to_dir = from_fh(arg[:to][:dir][:data])
from_name = arg[:from][:name]
to_name = arg[:to][:name]
if branch != to_branch
raise Errno::EIO
end
@vcfs.rename(branch, from_dir, from_name, to_dir, to_name)
{:_discriminant => :NFS_OK}
end)[:_discriminant]
end
@nfs_prog.on_call(@nfs_vers, :LINK) do |arg, auth, verf|
puts 'LINK'
puts arg.inspect
(handle_errors do
branch, from = from_fh(arg[:from][:data])
to_branch, to_dir = from_fh(arg[:to][:dir][:data])
to_name = arg[:to][:name]
if branch != to_branch
raise Errno::EIO
end
@vcfs.link(branch, from, to_dir, to_name)
{:_discriminant => :NFS_OK}
end)[:_discriminant]
end
@nfs_prog.on_call(@nfs_vers, :SYMLINK) do |arg, auth, verf|
puts 'SYMLINK'
puts arg.inspect
(handle_errors do
branch, dir = from_fh(arg[:from][:dir][:data])
name = arg[:from][:name]
to_name = arg[:to]
attrs = arg[:attributes]
auth = auth_unix(auth)
neg_one = 4294967295
uid = attrs[:uid]
if uid == neg_one
uid = auth[:uid]
end
gid = attrs[:gid]
if gid == neg_one
gid = auth[:gid]
end
puts to_name
@vcfs.symlink(branch, dir, name, to_name,
attrs[:mode], uid, gid)
{:_discriminant => :NFS_OK}
end)[:_discriminant]
end
@nfs_prog.on_call(@nfs_vers, :MKDIR) do |arg, auth, verf|
puts 'MKDIR'
puts arg.inspect
handle_errors do
auth = auth_unix(auth)
branch, dir = from_fh(arg[:where][:dir][:data])
neg_one = 4294967295
uid = arg[:attributes][:uid]
if uid == neg_one
uid = auth[:uid]
end
gid = arg[:attributes][:gid]
if gid == neg_one
gid = auth[:gid]
end
newdir = @vcfs.mkdir(branch, dir, arg[:where][:name],
arg[:attributes][:mode], uid, gid)
attrs = @vcfs.getattr(branch, newdir)
{
:_discriminant => :NFS_OK,
:diropres => {
:file => {
:data => to_fh(branch, newdir)
},
:attributes => convert_attrs(attrs)
}
}
end
end
@nfs_prog.on_call(@nfs_vers, :RMDIR) do |arg, auth, verf|
puts 'RMDIR'
puts arg.inspect
(handle_errors do
branch, dir = from_fh(arg[:dir][:data])
@vcfs.rmdir(branch, dir, arg[:name])
{:_discriminant => :NFS_OK}
end)[:_discriminant]
end
@nfs_prog.on_call(@nfs_vers, :READDIR) do |arg, auth, verf|
puts 'READDIR'
puts arg.inspect
handle_errors do
branch, dir = from_fh(arg[:dir][:data])
cookie = arg[:cookie]
count = arg[:count]
need_bytes = 16 + 12
entries = @vcfs.readdir(branch, dir)
result_entries = nil
last_entry = nil
while cookie < entries.size and need_bytes < count
need_bytes += NFS::Filename.encode(entries[cookie]).size
next_entry = {
:fileid => 1,
:name => entries[cookie],
:cookie => cookie,
:nextentry => nil
}
if not last_entry.nil?
last_entry[:nextentry] = next_entry
last_entry = next_entry
end
if result_entries.nil?
result_entries = next_entry
last_entry = next_entry
end
cookie += 1
need_bytes += 16
end
eof = :TRUE
if need_bytes > count
eof = :FALSE
end
{
:_discriminant => :NFS_OK,
:reply => {
:entries => result_entries,
:eof => eof
}
}
end
end
@nfs_prog.on_call(@nfs_vers, :STATFS) do |arg, auth, verf|
puts 'STATFS'
puts arg.inspect
handle_errors do
{
:_discriminant => :NFS_OK,
:reply => {
:tsize => 1024,
:bsize => 1024,
:blocks => 100,
:bfree => 100,
:bavail => 100
}
}
end
end
end
attr_reader :root
end