Class: UBLK::Params

Inherits:
Data
  • Object
show all
Defined in:
lib/ublk/params.rb,
sig/ublk.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, logical_block_size: 512, physical_block_size: 4096, max_io_bytes: 512 * 1024, read_only: false, rotational: false, discard: true) ⇒ Params

Returns a new instance of Params.

Parameters:

  • size: (Integer)
  • logical_block_size: (Integer) (defaults to: 512)
  • physical_block_size: (Integer) (defaults to: 4096)
  • max_io_bytes: (Integer) (defaults to: 512 * 1024)
  • read_only: (Boolean) (defaults to: false)
  • rotational: (Boolean) (defaults to: false)
  • discard: (Boolean) (defaults to: true)

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ublk/params.rb', line 8

def initialize(size:, logical_block_size: 512, physical_block_size: 4096,
               max_io_bytes: 512 * 1024, read_only: false, rotational: false,
               discard: true)
  [logical_block_size, physical_block_size].each do |block_size|
    valid = block_size.is_a?(Integer) && block_size.between?(512, 4096) && (block_size & (block_size - 1)).zero?
    raise ArgumentError, "block sizes must be powers of two between 512 and 4096" unless valid
  end
  raise ArgumentError, "physical block size must not be smaller than logical block size" if physical_block_size < logical_block_size
  raise ArgumentError, "size must be aligned to the logical block size" unless (size % logical_block_size).zero?
  raise ArgumentError, "max_io_bytes must be a positive multiple of 512" unless max_io_bytes.is_a?(Integer) && max_io_bytes.positive? && (max_io_bytes % 512).zero?
  raise ArgumentError, "max_io_bytes must not exceed 32 MiB" if max_io_bytes > 32 * 1024 * 1024

  super
end

Instance Attribute Details

#discardObject (readonly)

Returns the value of attribute discard

Returns:

  • (Object)

    the current value of discard



4
5
6
# File 'lib/ublk/params.rb', line 4

def discard
  @discard
end

#logical_block_sizeObject (readonly)

Returns the value of attribute logical_block_size

Returns:

  • (Object)

    the current value of logical_block_size



4
5
6
# File 'lib/ublk/params.rb', line 4

def logical_block_size
  @logical_block_size
end

#max_io_bytesObject (readonly)

Returns the value of attribute max_io_bytes

Returns:

  • (Object)

    the current value of max_io_bytes



4
5
6
# File 'lib/ublk/params.rb', line 4

def max_io_bytes
  @max_io_bytes
end

#physical_block_sizeObject (readonly)

Returns the value of attribute physical_block_size

Returns:

  • (Object)

    the current value of physical_block_size



4
5
6
# File 'lib/ublk/params.rb', line 4

def physical_block_size
  @physical_block_size
end

#read_onlyObject (readonly)

Returns the value of attribute read_only

Returns:

  • (Object)

    the current value of read_only



4
5
6
# File 'lib/ublk/params.rb', line 4

def read_only
  @read_only
end

#rotationalObject (readonly)

Returns the value of attribute rotational

Returns:

  • (Object)

    the current value of rotational



4
5
6
# File 'lib/ublk/params.rb', line 4

def rotational
  @rotational
end

#sizeObject (readonly)

Returns the value of attribute size

Returns:

  • (Object)

    the current value of size



4
5
6
# File 'lib/ublk/params.rb', line 4

def size
  @size
end

Class Method Details

.from_target(target, max_io_bytes: 512 * 1024) ⇒ Params

Parameters:

  • target (Target)
  • max_io_bytes: (Integer) (defaults to: 512 * 1024)

Returns:



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ublk/params.rb', line 23

def self.from_target(target, max_io_bytes: 512 * 1024)
  new(
    size: target.size,
    logical_block_size: target.logical_block_size,
    physical_block_size: target.physical_block_size,
    max_io_bytes:,
    read_only: target.read_only?,
    rotational: target.rotational?,
    discard: target.method(:discard).owner != Target || target.method(:write_zeroes).owner != Target
  )
end