Class: NFS::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/nfs/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil, fsid = 0) ⇒ Handler

Returns a new instance of Handler.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nfs/handler.rb', line 3

def initialize(root = nil, fsid = 0)
  @mount_prog = Mount::MOUNTPROG.dup
  @mount_vers = Mount::MOUNTVERS
  @nfs_prog = NFS::NFS_PROGRAM.dup
  @nfs_vers = NFS::NFS_VERSION

  @exports = {}
  @fh_table = {}
  @file_objects = {}
  @next_fh = Filehandle.new

  @fsid = fsid

  if !root.nil?
    export('/', root)
  end

  define_mount_procedures
  define_nfs_procedures

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



568
569
570
# File 'lib/nfs/handler.rb', line 568

def root
  @root
end

Instance Method Details

#add_filehandle(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nfs/handler.rb', line 34

def add_filehandle(file)
  if @file_objects.include?(file)
    @file_objects[file]
  else
    @next_fh.dup.tap do |fh|
      @fh_table[fh] = file
      @file_objects[file] = fh
      @next_fh.increment!
    end
  end
end

#convert_attrs(attrs) ⇒ Object

Convert Ruby Stat object to an NFS fattr



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
# File 'lib/nfs/handler.rb', line 140

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

#define_mount_proceduresObject



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
# File 'lib/nfs/handler.rb', line 91

def define_mount_procedures
  @mount_prog.on_call(@mount_vers, :MNT) do |arg, auth, verf|
    if @exports.include?(arg)
      ::NFS.logger.info("MNT #{arg}")

      {
        _discriminant: :NFS_OK,
        fhs_fhandle: {
          data: @exports[arg]
        }
      }
    else
      { _discriminant: :NFSERR_ACCES }
    end
  end

  @mount_prog.on_call(@mount_vers, :DUMP) do |arg, auth, verf|
    ::NFS.logger.info('DUMP')
    nil
  end

  @mount_prog.on_call(@mount_vers, :UMNT) do |arg, auth, verf|
    ::NFS.logger.info("UMNT #{arg}")
  end

  @mount_prog.on_call(@mount_vers, :UMNTALL) do |arg, auth, verf|
    ::NFS.logger.info("UMNTALL #{arg}")
  end

  export = proc do |arg, auth, verf|
    ::NFS.logger.info('EXPORT')
    result = nil

    @exports.each_key do |name|
      result = {
        ex_dir: name,
        ex_groups: nil,
        ex_next: result
      }
    end

    result
  end

  @mount_prog.on_call(@mount_vers, :EXPORT, &export)
  @mount_prog.on_call(@mount_vers, :EXPORTALL, &export)
end

#define_nfs_proceduresObject



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
# File 'lib/nfs/handler.rb', line 191

def define_nfs_procedures
  @nfs_prog.on_call(@nfs_vers, :GETATTR) do |arg, auth, verf|
    handle_errors do
      f = @fh_table[arg[:data]]
      attrs = f.lstat

      ::NFS.logger.info("GETATTR #{f.path}")

      {
        _discriminant: :NFS_OK,
        attributes: convert_attrs(attrs)
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :SETATTR) do |arg, auth, verf|
    changes = []

    handle_errors do
      f = @fh_table[arg[:file][:data]]
      attrs = convert_attrs(f.lstat)

      # Get -1 represented as an unsigned integer. The sattr fields
      # are -1 to represent that they should not be changed.
      neg_one = 4294967295

      # Start with the mode. Setattr won't change the type of a file
      # and apparently some NFS clients don't set the type, so mask
      # that part out to keep what we have already.
      if arg[:attributes][:mode] != neg_one
        attrs[:mode] &= ~07777
        attrs[:mode] |= 07777 & arg[:attributes][:mode]

        new_mode = arg[:attributes][:mode] & 07777
        changes << "mode: #{new_mode.to_s(8).rjust(5, '0')}"
        f.chmod(new_mode)
      end

      # Next do the UID and GID
      if arg[:attributes][:uid] != neg_one or
        arg[:attributes][:gid] != neg_one

        uid = arg[:attributes][:uid]
        gid = arg[:attributes][:gid]

        if uid == neg_one
          uid = attrs[:uid]
        end

        if gid == neg_one
          gid = attrs[:gid]
        end

        attrs[:uid] = uid
        attrs[:gid] = gid

        changes << "uid: #{uid}"
        changes << "gid: #{gid}"

        f.chown(uid, gid)
      end

      # Set size (truncate)
      if arg[:attributes][:size] != neg_one
        attrs[:size] = arg[:attributes][:size]
        changes << "size: #{attrs[:size]}"
        f.truncate(arg[:attributes][:size])
      end

      # Set time
      if arg[:attributes][:atime][:seconds] != neg_one or
        arg[:attributes][:mtime][:seconds] != neg_one

        atime = arg[:attributes][:atime]
        mtime = arg[:attributes][:mtime]

        if atime[:seconds] == neg_one
          atime = attrs[:atime]
        end

        if mtime[:seconds] == neg_one
          mtime = attrs[:mtime]
        end

        attrs[:atime] = atime
        attrs[:mtime] = mtime

        atime = Time.at(atime[:seconds], atime[:useconds])
        mtime = Time.at(mtime[:seconds], mtime[:useconds])

        changes << "atime: #{atime}"
        changes << "mtime: #{mtime}"

        f.utime(atime, mtime)
      end

      ::NFS.logger.info("SETATTR #{f} #{changes.join(', ')}")

      {
        _discriminant: :NFS_OK,
        attributes: attrs
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :ROOT) do |arg, auth, verf|
    ::NFS.logger.info('ROOT')
    # obsolete
  end

  @nfs_prog.on_call(@nfs_vers, :LOOKUP) do |arg, auth, verf|
    handle_errors do
      f = @fh_table[arg[:dir][:data]].lookup(arg[:name])
      ::NFS.logger.info("LOOKUP #{f.path}")
      fh = add_filehandle(f)
      attrs = f.lstat

      result = {
        _discriminant: :NFS_OK,
        diropres: {
          file: {
            data: fh
          },
          attributes: convert_attrs(attrs)
        }
      }

      result
    end
  end

  @nfs_prog.on_call(@nfs_vers, :READLINK) do |arg, auth, verf|
    handle_errors do
      f = @fh_table[arg[:data]]
      ::NFS.logger.info("READLINK #{f.path}")
      result = f.readlink

      {
        _discriminant: :NFS_OK,
        data: result
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :READ) do |arg, auth, verf|
    handle_errors do
      f = @fh_table[arg[:file][:data]]
      ::NFS.logger.info("READ #{f.path}")
      attrs = f.lstat
      f.pos = arg[:offset]
      result = f.read(arg[:count])

      {
        _discriminant: :NFS_OK,
        reply: {
          attributes: convert_attrs(attrs),
          data: result
        }
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :WRITECACHE) do |arg, auth, verf|
    ::NFS.logger.info('WRITECACHE')
  end

  @nfs_prog.on_call(@nfs_vers, :WRITE) do |arg, auth, verf|

    handle_errors do
      f = @fh_table[arg[:file][:data]]
      ::NFS.logger.info("WRITE #{f.path}")
      f.pos = arg[:offset]
      f.write(arg[:data])
      f.flush
      attrs = f.lstat

      {
        _discriminant: :NFS_OK,
        attributes: convert_attrs(attrs)
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :CREATE) do |arg, auth, verf|
    handle_errors do
      dir = @fh_table[arg[:where][:dir][:data]]
      name = arg[:where][:name]
      ::NFS.logger.info("CREATE #{name}")

      f, attrs = dir.create(
        name,
        arg[:attributes][:mode], arg[:attributes][:uid],
        arg[:attributes][:gid]
      )

      fh = add_filehandle(f)

      {
        _discriminant: :NFS_OK,
        diropres: {
          file: {
            data: fh
          },
          attributes: convert_attrs(attrs)
        }
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :REMOVE) do |arg, auth, verf|
    (handle_errors do
      dir = @fh_table[arg[:dir][:data]]
      name = arg[:name]
      ::NFS.logger.info("REMOVE #{name}")
      dir.unlink(name)

      { _discriminant: :NFS_OK }
    end)[:_discriminant]
  end

  @nfs_prog.on_call(@nfs_vers, :RENAME) do |arg, auth, verf|
    (handle_errors do
      from_dir = @fh_table[arg[:from][:dir][:data]]
      from_name = arg[:from][:name]
      to_dir = @fh_table[arg[:to][:dir][:data]]
      to_name = arg[:to][:name]

      ::NFS.logger.info(
        "RENAME #{File.join(from_dir.path, from_name)} -> #{File.join(to_dir.path, to_name)}"
      )

      from_dir.rename(from_name, to_dir, to_name)

      { _discriminant: :NFS_OK }
    end)[:_discriminant]
  end

  @nfs_prog.on_call(@nfs_vers, :LINK) do |arg, auth, verf|
    (handle_errors do
      from = @fh_table[arg[:from][:data]]
      to_dir = @fh_table[arg[:to][:dir][:data]]
      to_name = arg[:to][:name]

      ::NFS.logger.info("LINK #{from.path} -> #{File.join(to_dir.path, to_name)}")

      from.link(to_dir, to_name)

      { _discriminant: :NFS_OK }
    end)[:_discriminant]
  end

  @nfs_prog.on_call(@nfs_vers, :SYMLINK) do |arg, auth, verf|
    (handle_errors do
      dir = @fh_table[arg[:from][:dir][:data]]
      name = arg[:from][:name]
      to_name = arg[:to]
      attrs = arg[:attributes]

      ::NFS.logger.info("SYMLINK #{File.join(dir.path, name)} -> #{to_name}")

      dir.symlink(name, to_name)

      { _discriminant: :NFS_OK }
    end)[:_discriminant]
  end

  @nfs_prog.on_call(@nfs_vers, :MKDIR) do |arg, auth, verf|
    handle_errors do
      dir = @fh_table[arg[:where][:dir][:data]]
      name = arg[:where][:name]

      ::NFS.logger.info("MKDIR #{name}")

      f, attrs = dir.mkdir(name, arg[:attributes][:mode],
        arg[:attributes][:uid], arg[:attributes][:gid])

      fh = add_filehandle(f)

      {
        _discriminant: :NFS_OK,
        diropres: {
          file: {
            data: fh
          },
          attributes: convert_attrs(attrs)
        }
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :RMDIR) do |arg, auth, verf|
    (handle_errors do
      dir = @fh_table[arg[:dir][:data]]
      name = arg[:name]
      ::NFS.logger.info("RMDIR #{name}")
      dir.rmdir(name)

      { _discriminant: :NFS_OK}
    end)[:_discriminant]
  end

  @nfs_prog.on_call(@nfs_vers, :READDIR) do |arg, auth, verf|
    handle_errors do
      dir = @fh_table[arg[:dir][:data]]
      ::NFS.logger.info("READDIR #{dir.path}")

      cookie = arg[:cookie]
      count = arg[:count]

      need_bytes = 16 + 12

      entries = dir.entries

      result_entries = nil
      last_entry = nil

      while cookie < entries.size && need_bytes < count
        need_bytes += NFS::Filename.encode(entries[cookie]).size

        next_entry = {
          fileid: 1,
          name: entries[cookie],
          cookie: cookie
        }

        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

      if not last_entry.nil?
        last_entry[:nextentry] = nil
      end

      {
        _discriminant: :NFS_OK,
        reply: {
          entries: result_entries,
          eof: eof
        }
      }
    end
  end

  @nfs_prog.on_call(@nfs_vers, :STATFS) do |arg, auth, verf|
    ::NFS.logger.info('STATFS')

    handle_errors do
      {
        _discriminant: :NFS_OK,
        reply: {
          tsize: 1024,
          bsize: 1024,
          blocks: 100,
          bfree: 100,
          bavail: 100
        }
      }
    end
  end
end

#export(path, file) ⇒ Object



30
31
32
# File 'lib/nfs/handler.rb', line 30

def export(path, file)
  @exports[path] = add_filehandle(file)
end

#handle_errorsObject



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
# File 'lib/nfs/handler.rb', line 46

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
    ::NFS.logger.error(e.message)
    ::NFS.logger.error(e.backtrace.join("\n"))
    { _discriminant: :NFSERR_IO }
  end
end

#programsObject



26
27
28
# File 'lib/nfs/handler.rb', line 26

def programs
  [@mount_prog, @nfs_prog]
end