# Block devices

---

# Block Devices

_Peios Kernel / Administration / System administration / Block devices_

> Reference for Block Devices.

<a id="rst-block-devices"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [Floppy Driver](</kernel/administration/admin-guide/blockdev/floppy.md>)
- [Network Block Device (TCP version)](</kernel/administration/admin-guide/blockdev/nbd.md>)
- [Linux and parallel port IDE devices](</kernel/administration/admin-guide/blockdev/paride.md>)
- [Using the RAM disk block device with Linux](</kernel/administration/admin-guide/blockdev/ramdisk.md>)
- [Zoned Loop Block Device](</kernel/administration/admin-guide/blockdev/zoned-loop.md>)
- [zram: Compressed RAM-based block devices](</kernel/administration/admin-guide/blockdev/zram.md>)
- [Distributed Replicated Block Device - DRBD](</kernel/administration/admin-guide/blockdev-drbd/index.md>)

---

# Floppy Driver

_Peios Kernel / Administration / System administration / Block devices_

> A FAQ list may be found in the fdutils package (see below), and also at <https://fdutils.linux.lu/faq.html>.

<a id="rst-floppy-driver"></a>

<a id="rst-faq-list"></a>

## FAQ list:

A FAQ list may be found in the fdutils package (see below), and also at &lt;[https://fdutils.linux.lu/faq.html](<https://fdutils.linux.lu/faq.html>)&gt;.

<a id="rst-lilo-configuration-options-thinkpad-users-read-this"></a>

## LILO configuration options (Thinkpad users, read this)

The floppy driver is configured using the 'floppy=' option in lilo. This option can be typed at the boot prompt, or entered in the lilo configuration file.

Example: If your kernel is called linux-2.6.9, type the following line at the lilo boot prompt (if you have a thinkpad):



```text
linux-2.6.9 floppy=thinkpad
```

You may also enter the following line in /etc/lilo.conf, in the description of linux-2.6.9:



```text
append = "floppy=thinkpad"
```

Several floppy related options may be given, example:



```text
linux-2.6.9 floppy=daring floppy=two_fdc
append = "floppy=daring floppy=two_fdc"
```

If you give options both in the lilo config file and on the boot prompt, the option strings of both places are concatenated, the boot prompt options coming last. That's why there are also options to restore the default behavior.

<a id="rst-module-configuration-options"></a>

## Module configuration options

If you use the floppy driver as a module, use the following syntax:



```text
modprobe floppy floppy="<options>"
```

Example:



```text
modprobe floppy floppy="omnibook messages"
```

If you need certain options enabled every time you load the floppy driver, you can put:



```text
options floppy floppy="omnibook messages"
```

in a configuration file in /etc/modprobe.d/.

The floppy driver related options are:

> - **floppy=asus_pci**
>
>   Sets the bit mask to allow only units 0 and 1. (default)
>
> - **floppy=daring**
>
>   Tells the floppy driver that you have a well behaved floppy controller. This allows more efficient and smoother operation, but may fail on certain controllers. This may speed up certain operations.
>
> - **floppy=0,daring**
>
>   Tells the floppy driver that your floppy controller should be used with caution.
>
> - **floppy=one_fdc**
>
>   Tells the floppy driver that you have only one floppy controller. (default)
>
> - **floppy=two_fdc / floppy=&lt;address&gt;,two_fdc**
>
>   Tells the floppy driver that you have two floppy controllers. The second floppy controller is assumed to be at &lt;address&gt;. This option is not needed if the second controller is at address 0x370, and if you use the 'cmos' option.
>
> - **floppy=thinkpad**
>
>   Tells the floppy driver that you have a Thinkpad. Thinkpads use an inverted convention for the disk change line.
>
> - **floppy=0,thinkpad**
>
>   Tells the floppy driver that you don't have a Thinkpad.
>
> - **floppy=omnibook / floppy=nodma**
>
>   Tells the floppy driver not to use Dma for data transfers. This is needed on HP Omnibooks, which don't have a workable DMA channel for the floppy driver. This option is also useful if you frequently get "Unable to allocate DMA memory" messages. Indeed, dma memory needs to be continuous in physical memory, and is thus harder to find, whereas non-dma buffers may be allocated in virtual memory. However, I advise against this if you have an FDC without a FIFO (8272A or 82072). 82072A and later are OK. You also need at least a 486 to use nodma. If you use nodma mode, I suggest you also set the FIFO threshold to 10 or lower, in order to limit the number of data transfer interrupts.
>
>   If you have a FIFO-able FDC, the floppy driver automatically falls back on non DMA mode if no DMA-able memory can be found. If you want to avoid this, explicitly ask for 'yesdma'.
>
> - **floppy=yesdma**
>
>   Tells the floppy driver that a workable DMA channel is available. (default)
>
> - **floppy=nofifo**
>
>   Disables the FIFO entirely. This is needed if you get "Bus master arbitration error" messages from your Ethernet card (or from other devices) while accessing the floppy.
>
> - **floppy=usefifo**
>
>   Enables the FIFO. (default)
>
> - **floppy=&lt;threshold&gt;,fifo_depth**
>
>   Sets the FIFO threshold. This is mostly relevant in DMA mode. If this is higher, the floppy driver tolerates more interrupt latency, but it triggers more interrupts (i.e. it imposes more load on the rest of the system). If this is lower, the interrupt latency should be lower too (faster processor). The benefit of a lower threshold is less interrupts.
>
>   To tune the fifo threshold, switch on over/underrun messages using 'floppycontrol --messages'. Then access a floppy disk. If you get a huge amount of "Over/Underrun - retrying" messages, then the fifo threshold is too low. Try with a higher value, until you only get an occasional Over/Underrun. It is a good idea to compile the floppy driver as a module when doing this tuning. Indeed, it allows to try different fifo values without rebooting the machine for each test. Note that you need to do 'floppycontrol --messages' every time you re-insert the module.
>
>   Usually, tuning the fifo threshold should not be needed, as the default (0xa) is reasonable.
>
> - **floppy=&lt;drive&gt;,&lt;type&gt;,cmos**
>
>   Sets the CMOS type of &lt;drive&gt; to &lt;type&gt;. This is mandatory if you have more than two floppy drives (only two can be described in the physical CMOS), or if your BIOS uses non-standard CMOS types. The CMOS types are:
>
>   >
>   > <table>
>   > <tbody>
>   > <tr>
>   > <td>
>   >
>   >
>   > 0
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > Use the value of the physical CMOS
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 1
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 5 1/4 DD
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 2
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 5 1/4 HD
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 3
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 3 1/2 DD
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 4
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 3 1/2 HD
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 5
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 3 1/2 ED
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 6
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > 3 1/2 ED
>   >
>   > </td>
>   > </tr>
>   > <tr>
>   > <td>
>   >
>   >
>   > 16
>   >
>   > </td>
>   > <td>
>   >
>   >
>   > unknown or not installed
>   >
>   > </td>
>   > </tr>
>   > </tbody>
>   > </table>
>
>   (Note: there are two valid types for ED drives. This is because 5 was initially chosen to represent floppy *tapes*, and 6 for ED drives. AMI ignored this, and used 5 for ED drives. That's why the floppy driver handles both.)
>
> - **floppy=unexpected_interrupts**
>
>   Print a warning message when an unexpected interrupt is received. (default)
>
> - **floppy=no_unexpected_interrupts / floppy=L40SX**
>
>   Don't print a message when an unexpected interrupt is received. This is needed on IBM L40SX laptops in certain video modes. (There seems to be an interaction between video and floppy. The unexpected interrupts affect only performance, and can be safely ignored.)
>
> - **floppy=broken_dcl**
>
>   Don't use the disk change line, but assume that the disk was changed whenever the device node is reopened. Needed on some boxes where the disk change line is broken or unsupported. This should be regarded as a stopgap measure, indeed it makes floppy operation less efficient due to unneeded cache flushings, and slightly more unreliable. Please verify your cable, connection and jumper settings if you have any DCL problems. However, some older drives, and also some laptops are known not to have a DCL.
>
> - **floppy=debug**
>
>   Print debugging messages.
>
> - **floppy=messages**
>
>   Print informational messages for some operations (disk change notifications, warnings about over and underruns, and about autodetection).
>
> - **floppy=silent_dcl_clear**
>
>   Uses a less noisy way to clear the disk change line (which doesn't involve seeks). Implied by 'daring' option.
>
> - **floppy=&lt;nr&gt;,irq**
>
>   Sets the floppy IRQ to &lt;nr&gt; instead of 6.
>
> - **floppy=&lt;nr&gt;,dma**
>
>   Sets the floppy DMA channel to &lt;nr&gt; instead of 2.
>
> - **floppy=slow**
>
>   Use PS/2 stepping rate:
>
>
>
>   ```text
>   PS/2 floppies have much slower step rates than regular floppies.
>   It's been recommended that take about 1/4 of the default speed
>   in some more extreme cases.
>   ```

<a id="rst-supporting-utilities-and-additional-documentation"></a>

## Supporting utilities and additional documentation:

Additional parameters of the floppy driver can be configured at runtime. Utilities which do this can be found in the fdutils package. This package also contains a new version of mtools which allows to access high capacity disks (up to 1992K on a high density 3 1/2 disk\!). It also contains additional documentation about the floppy driver.

The latest version can be found at fdutils homepage:

> [https://fdutils.linux.lu](<https://fdutils.linux.lu>)

The fdutils releases can be found at:

> [https://fdutils.linux.lu/download.html](<https://fdutils.linux.lu/download.html>)
>
> [http://www.tux.org/pub/knaff/fdutils/](<http://www.tux.org/pub/knaff/fdutils/>)
>
> [ftp://metalab.unc.edu/pub/Linux/utils/disk-management/](<ftp://metalab.unc.edu/pub/Linux/utils/disk-management/>)

<a id="rst-reporting-problems-about-the-floppy-driver"></a>

## Reporting problems about the floppy driver

If you have a question or a bug report about the floppy driver, mail me at [Alain.Knaff@poboxes.com](<mailto:Alain.Knaff@poboxes.com>) . If you post to Usenet, preferably use comp.os.linux.hardware. As the volume in these groups is rather high, be sure to include the word "floppy" (or "FLOPPY") in the subject line.  If the reported problem happens when mounting floppy disks, be sure to mention also the type of the filesystem in the subject line.

Be sure to read the FAQ before mailing/posting any bug reports\!

Alain

<a id="rst-changelog"></a>

## Changelog

- **10-30-2004 :**

  Cleanup, updating, add reference to module configuration. James Nelson &lt;[james4765@gmail.com](<mailto:james4765@gmail.com>)&gt;

- **6-3-2000 :**

  Original Document

---

# Network Block Device (TCP version)

_Peios Kernel / Administration / System administration / Block devices_

> What is it: With this compiled in the kernel (or as a module), Linux can use a remote server as one of its block devices. So every time the client computer wants to read, e.g., /dev/nb0, it sends a request over TCP to the server, which…

<a id="rst-network-block-device-tcp-version"></a>

<a id="rst-overview"></a>

## 1) Overview

What is it: With this compiled in the kernel (or as a module), Linux can use a remote server as one of its block devices. So every time the client computer wants to read, e.g., /dev/nb0, it sends a request over TCP to the server, which will reply with the data read. This can be used for stations with low disk space (or even diskless) to borrow disk space from another computer. Unlike NFS, it is possible to put any filesystem on it, etc.

For more information, or to download the nbd-client and nbd-server tools, go to [https://github.com/NetworkBlockDevice/nbd](<https://github.com/NetworkBlockDevice/nbd>).

The nbd kernel module need only be installed on the client system, as the nbd-server is completely in userspace. In fact, the nbd-server has been successfully ported to other operating systems, including Windows.

<a id="rst-a-nbd-parameters"></a>

## A) NBD parameters

- **max_part**

  Number of partitions per device (default: 0).

- **nbds_max**

  Number of block devices that should be initialized (default: 16).

---

# Linux and parallel port IDE devices

_Peios Kernel / Administration / System administration / Block devices_

> PARIDE v1.03 (c) 1997-8 Grant Guenther <grant@torque.net> PATA_PARPORT (c) 2023 Ondrej Zary

<a id="rst-linux-and-parallel-port-ide-devices"></a>

PARIDE v1.03   (c) 1997-8  Grant Guenther &lt;[grant@torque.net](<mailto:grant@torque.net>)&gt; PATA_PARPORT   (c) 2023 Ondrej Zary

<a id="rst-introduction"></a>

## 1. Introduction

Owing to the simplicity and near universality of the parallel port interface to personal computers, many external devices such as portable hard-disk, CD-ROM, LS-120 and tape drives use the parallel port to connect to their host computer.  While some devices (notably scanners) use ad-hoc methods to pass commands and data through the parallel port interface, most external devices are actually identical to an internal model, but with a parallel-port adapter chip added in.  Some of the original parallel port adapters were little more than mechanisms for multiplexing a SCSI bus. (The Iomega PPA-3 adapter used in the ZIP drives is an example of this approach).  Most current designs, however, take a different approach. The adapter chip reproduces a small ISA or IDE bus in the external device and the communication protocol provides operations for reading and writing device registers, as well as data block transfer functions.  Sometimes, the device being addressed via the parallel cable is a standard SCSI controller like an NCR 5380.  The "ditto" family of external tape drives use the ISA replicator to interface a floppy disk controller, which is then connected to a floppy-tape mechanism.  The vast majority of external parallel port devices, however, are now based on standard IDE type devices, which require no intermediate controller.  If one were to open up a parallel port CD-ROM drive, for instance, one would find a standard ATAPI CD-ROM drive, a power supply, and a single adapter that interconnected a standard PC parallel port cable and a standard IDE cable.  It is usually possible to exchange the CD-ROM device with any other device using the IDE interface.

The document describes the support in Linux for parallel port IDE devices.  It does not cover parallel port SCSI devices, "ditto" tape drives or scanners.  Many different devices are supported by the parallel port IDE subsystem, including:

> - MicroSolutions backpack CD-ROM
>
> - MicroSolutions backpack PD/CD
>
> - MicroSolutions backpack hard-drives
>
> - MicroSolutions backpack 8000t tape drive
>
> - SyQuest EZ-135, EZ-230 &amp; SparQ drives
>
> - Avatar Shark
>
> - Imation Superdisk LS-120
>
> - Maxell Superdisk LS-120
>
> - FreeCom Power CD
>
> - Hewlett-Packard 5GB and 8GB tape drives
>
> - Hewlett-Packard 7100 and 7200 CD-RW drives

as well as most of the clone and no-name products on the market.

To support such a wide range of devices, pata_parport is actually structured in two parts. There is a base pata_parport module which provides an interface to kernel libata subsystem, registry and some common methods for accessing the parallel ports.

The second component is a set of low-level protocol drivers for each of the parallel port IDE adapter chips.  Thanks to the interest and encouragement of Linux users from many parts of the world, support is available for almost all known adapter protocols:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> aten
>
> </td>
> <td>
>
>
> ATEN EH-100
>
> </td>
> <td>
>
>
> (HK)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> bpck
>
> </td>
> <td>
>
>
> Microsolutions backpack
>
> </td>
> <td>
>
>
> (US)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> comm
>
> </td>
> <td>
>
>
> DataStor (old-type) "commuter" adapter
>
> </td>
> <td>
>
>
> (TW)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> dstr
>
> </td>
> <td>
>
>
> DataStor EP-2000
>
> </td>
> <td>
>
>
> (TW)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> epat
>
> </td>
> <td>
>
>
> Shuttle EPAT
>
> </td>
> <td>
>
>
> (UK)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> epia
>
> </td>
> <td>
>
>
> Shuttle EPIA
>
> </td>
> <td>
>
>
> (UK)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> fit2
>
> </td>
> <td>
>
>
> FIT TD-2000
>
> </td>
> <td>
>
>
> (US)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> fit3
>
> </td>
> <td>
>
>
> FIT TD-3000
>
> </td>
> <td>
>
>
> (US)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> friq
>
> </td>
> <td>
>
>
> Freecom IQ cable
>
> </td>
> <td>
>
>
> (DE)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> frpw
>
> </td>
> <td>
>
>
> Freecom Power
>
> </td>
> <td>
>
>
> (DE)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> kbic
>
> </td>
> <td>
>
>
> KingByte KBIC-951A and KBIC-971A
>
> </td>
> <td>
>
>
> (TW)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> ktti
>
> </td>
> <td>
>
>
> KT Technology PHd adapter
>
> </td>
> <td>
>
>
> (SG)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> on20
>
> </td>
> <td>
>
>
> OnSpec 90c20
>
> </td>
> <td>
>
>
> (US)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> on26
>
> </td>
> <td>
>
>
> OnSpec 90c26
>
> </td>
> <td>
>
>
> (US)
>
> </td>
> </tr>
> </tbody>
> </table>

<a id="rst-using-pata-parport-subsystem"></a>

## 2. Using pata_parport subsystem

While configuring the Linux kernel, you may choose either to build the pata_parport drivers into your kernel, or to build them as modules.

In either case, you will need to select "Parallel port IDE device support" and at least one of the parallel port communication protocols. If you do not know what kind of parallel port adapter is used in your drive, you could begin by checking the file names and any text files on your DOS installation floppy.  Alternatively, you can look at the markings on the adapter chip itself.  That's usually sufficient to identify the correct device.

You can actually select all the protocol modules, and allow the pata_parport subsystem to try them all for you.

For the "brand-name" products listed above, here are the protocol and high-level drivers that you would use:

>
> <table>
> <thead>
> <tr>
> <th>
>
>
> Manufacturer
>
> </th>
> <th>
>
>
> Model
>
> </th>
> <th>
>
>
> Protocol
>
> </th>
> </tr>
> </thead>
> <tbody>
> <tr>
> <td>
>
>
> MicroSolutions
>
> </td>
> <td>
>
>
> CD-ROM
>
> </td>
> <td>
>
>
> bpck
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> MicroSolutions
>
> </td>
> <td>
>
>
> PD drive
>
> </td>
> <td>
>
>
> bpck
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> MicroSolutions
>
> </td>
> <td>
>
>
> hard-drive
>
> </td>
> <td>
>
>
> bpck
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> MicroSolutions
>
> </td>
> <td>
>
>
> 8000t tape
>
> </td>
> <td>
>
>
> bpck
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> SyQuest
>
> </td>
> <td>
>
>
> EZ, SparQ
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Imation
>
> </td>
> <td>
>
>
> Superdisk
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Maxell
>
> </td>
> <td>
>
>
> Superdisk
>
> </td>
> <td>
>
>
> friq
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Avatar
>
> </td>
> <td>
>
>
> Shark
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> FreeCom
>
> </td>
> <td>
>
>
> CD-ROM
>
> </td>
> <td>
>
>
> frpw
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Hewlett-Packard
>
> </td>
> <td>
>
>
> 5GB Tape
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Hewlett-Packard
>
> </td>
> <td>
>
>
> 7200e (CD)
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Hewlett-Packard
>
> </td>
> <td>
>
>
> 7200e (CD-R)
>
> </td>
> <td>
>
>
> epat
>
> </td>
> </tr>
> </tbody>
> </table>

All parports and all protocol drivers are probed automatically unless probe=0 parameter is used. So just "modprobe epat" is enough for an Imation SuperDisk drive to work.

Manual device creation:



```text
# echo "port protocol mode unit delay" >/sys/bus/pata_parport/new_device
```

where:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> port
>
> </td>
> <td>
>
>
> parport name (or "auto" for all parports)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> protocol
>
> </td>
> <td>
>
>
> protocol name (or "auto" for all protocols)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> mode
>
> </td>
> <td>
>
>
> mode number (protocol-specific) or -1 for probe
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> unit
>
> </td>
> <td>
>
>
> unit number (for backpack only, see below)
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> delay
>
> </td>
> <td>
>
>
> I/O delay (see troubleshooting section below)
>
> </td>
> </tr>
> </tbody>
> </table>

If you happen to be using a MicroSolutions backpack device, you will also need to know the unit ID number for each drive.  This is usually the last two digits of the drive's serial number (but read MicroSolutions' documentation about this).

If you omit the parameters from the end, defaults will be used, e.g.:

Probe all parports with all protocols:



```text
# echo auto >/sys/bus/pata_parport/new_device
```

Probe parport0 using protocol epat and mode 4 (EPP-16):



```text
# echo "parport0 epat 4" >/sys/bus/pata_parport/new_device
```

Probe parport0 using all protocols:



```text
# echo "parport0 auto" >/sys/bus/pata_parport/new_device
```

Probe all parports using protoocol epat:



```text
# echo "auto epat" >/sys/bus/pata_parport/new_device
```

Deleting devices:



```text
# echo pata_parport.0 >/sys/bus/pata_parport/delete_device
```

<a id="rst-troubleshooting"></a>

## 3. Troubleshooting

<a id="rst-use-epp-mode-if-you-can"></a>

### 3.1  Use EPP mode if you can

The most common problems that people report with the pata_parport drivers concern the parallel port CMOS settings.  At this time, none of the protocol modules support ECP mode, or any ECP combination modes. If you are able to do so, please set your parallel port into EPP mode using your CMOS setup procedure.

<a id="rst-check-the-port-delay"></a>

### 3.2  Check the port delay

Some parallel ports cannot reliably transfer data at full speed.  To offset the errors, the protocol modules introduce a "port delay" between each access to the i/o ports.  Each protocol sets a default value for this delay.  In most cases, the user can override the default and set it to 0 - resulting in somewhat higher transfer rates.  In some rare cases (especially with older 486 systems) the default delays are not long enough.  if you experience corrupt data transfers, or unexpected failures, you may wish to increase the port delay.

<a id="rst-some-drives-need-a-printer-reset"></a>

### 3.3  Some drives need a printer reset

There appear to be a number of "noname" external drives on the market that do not always power up correctly.  We have noticed this with some drives based on OnSpec and older Freecom adapters.  In these rare cases, the adapter can often be reinitialised by issuing a "printer reset" on the parallel port.  As the reset operation is potentially disruptive in multiple device environments, the pata_parport drivers will not do it automatically.  You can however, force a printer reset by doing:



```text
insmod lp reset=1
rmmod lp
```

If you have one of these marginal cases, you should probably build your pata_parport drivers as modules, and arrange to do the printer reset before loading the pata_parport drivers.

---

# Using the RAM disk block device with Linux

_Peios Kernel / Administration / System administration / Block devices_

> The RAM disk driver is a way to use main system memory as a block device. It is required for initrd, an initial filesystem used if you need to load modules in order to access the root filesystem (see Documentation/admin-guide/initrd.rst).…

<a id="rst-using-the-ram-disk-block-device-with-linux"></a>

<!-- Contents:

1) Overview
2) Kernel Command Line Parameters
3) Using "rdev"
4) An Example of Creating a Compressed RAM Disk -->

<a id="rst-overview"></a>

## 1) Overview

The RAM disk driver is a way to use main system memory as a block device.  It is required for initrd, an initial filesystem used if you need to load modules in order to access the root filesystem (see [Documentation/admin-guide/initrd.rst](/kernel/administration/admin-guide/boot/initrd.md)).  It can also be used for a temporary filesystem for crypto work, since the contents are erased on reboot.

The RAM disk dynamically grows as more space is required. It does this by using RAM from the buffer cache. The driver marks the buffers it is using as dirty so that the VM subsystem does not try to reclaim them later.

The RAM disk supports up to 16 RAM disks by default, and can be reconfigured to support an unlimited number of RAM disks (at your own risk).  Just change the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu and (re)build the kernel.

To use RAM disk support with your system, run './MAKEDEV ram' from the /dev directory.  RAM disks are all major number 1, and start with minor number 0 for /dev/ram0, etc.  If used, modern kernels use /dev/ram0 for an initrd.

The new RAM disk also has the ability to load compressed RAM disk images, allowing one to squeeze more programs onto an average installation or rescue floppy disk.

<a id="rst-parameters"></a>

## 2) Parameters

2a) Kernel Command Line Parameters

> - **ramdisk_size=N**
>
>   Size of the ramdisk.

This parameter tells the RAM disk driver to set up RAM disks of N k size.  The default is 4096 (4 MB).

2b) Module parameters

> - **rd_nr**
>
>   /dev/ramX devices created.
>
> - **max_part**
>
>   Maximum partition number.
>
> - **rd_size**
>
>   See ramdisk_size.

<a id="rst-using-rdev"></a>

## 3) Using "rdev"

"rdev" is an obsolete, deprecated, antiquated utility that could be used to set the boot device in a Linux kernel image.

Instead of using rdev, just place the boot device information on the kernel command line and pass it to the kernel from the bootloader.

You can also pass arguments to the kernel by setting FDARGS in arch/x86/boot/Makefile and specify in initrd image by setting FDINITRD in arch/x86/boot/Makefile.

Some of the kernel command line boot options that may apply here are:



```text
ramdisk_start=N
ramdisk_size=M
```

If you make a boot disk that has LILO, then for the above, you would use:



```text
append = "ramdisk_start=N ramdisk_size=M"
```

<a id="rst-an-example-of-creating-a-compressed-ram-disk"></a>

## 4) An Example of Creating a Compressed RAM Disk

To create a RAM disk image, you will need a spare block device to construct it on. This can be the RAM disk device itself, or an unused disk partition (such as an unmounted swap partition). For this example, we will use the RAM disk device, "/dev/ram0".

Note: This technique should not be done on a machine with less than 8 MB of RAM. If using a spare disk partition instead of /dev/ram0, then this restriction does not apply.

- **a)** Decide on the RAM disk size that you want. Say 2 MB for this example. Create it by writing to the RAM disk device. (This step is not currently required, but may be in the future.) It is wise to zero out the area (esp. for disks) so that maximal compression is achieved for the unused blocks of the image that you are about to create:



  ```text
  dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
  ```

- **b)** Make a filesystem on it. Say ext2fs for this example:



  ```text
  mke2fs -vm0 /dev/ram0 2048
  ```

- **c)** Mount it, copy the files you want to it (eg: /etc/\* /dev/\* ...) and unmount it again.

- **d)** Compress the contents of the RAM disk. The level of compression will be approximately 50% of the space used by the files. Unused space on the RAM disk will compress to almost nothing:



  ```text
  dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
  ```

- **e)** Put the kernel onto the floppy:



  ```text
  dd if=zImage of=/dev/fd0 bs=1k
  ```

- **f)** Put the RAM disk image onto the floppy, after the kernel. Use an offset that is slightly larger than the kernel, so that you can put another (possibly larger) kernel onto the same floppy later without overlapping the RAM disk image. An offset of 400 kB for kernels about 350 kB in size would be reasonable. Make sure offset+size of ram_image.gz is not larger than the total space on your floppy (usually 1440 kB):



  ```text
  dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400
  ```

- **g)** Make sure that you have already specified the boot information in FDARGS and FDINITRD or that you use a bootloader to pass kernel command line boot options to the kernel.

That is it. You now have your boot/root compressed RAM disk floppy. Some users may wish to combine steps (d) and (f) by using a pipe.

> Paul Gortmaker 12/95

<a id="rst-changelog"></a>

## Changelog:

SEPT-2020 :

> Removed usage of "rdev"

- **10-22-04 :**

  Updated to reflect changes in command line options, remove obsolete references, general cleanup. James Nelson ([james4765@gmail.com](<mailto:james4765@gmail.com>))

- **12-95 :**

  Original Document

---

# Zoned Loop Block Device

_Peios Kernel / Administration / System administration / Block devices_

> The zoned loop block device driver (zloop) allows a user to create a zoned block device using one regular file per zone as backing storage. This driver does not directly control any hardware and uses read, write and truncate operations to…

<a id="rst-zoned-loop-block-device"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<!-- Contents:

1) Overview
2) Creating a Zoned Device
3) Deleting a Zoned Device
4) Example -->

<a id="rst-overview"></a>

## 1) Overview

The zoned loop block device driver (zloop) allows a user to create a zoned block device using one regular file per zone as backing storage. This driver does not directly control any hardware and uses read, write and truncate operations to regular files of a file system to emulate a zoned block device.

Using zloop, zoned block devices with a configurable capacity, zone size and number of conventional zones can be created. The storage for each zone of the device is implemented using a regular file with a maximum size equal to the zone size. The size of a file backing a conventional zone is always equal to the zone size. The size of a file backing a sequential zone indicates the amount of data sequentially written to the file, that is, the size of the file directly indicates the position of the write pointer of the zone.

When resetting a sequential zone, its backing file size is truncated to zero. Conversely, for a zone finish operation, the backing file is truncated to the zone size. With this, the maximum capacity of a zloop zoned block device created can be larger configured to be larger than the storage space available on the backing file system. Of course, for such configuration, writing more data than the storage space available on the backing file system will result in write errors.

The zoned loop block device driver implements a complete zone transition state machine. That is, zones can be empty, implicitly opened, explicitly opened, closed or full. The current implementation does not support any limits on the maximum number of open and active zones.

No user tools are necessary to create and delete zloop devices.

<a id="rst-creating-a-zoned-device"></a>

## 2) Creating a Zoned Device

Once the zloop module is loaded (or if zloop is compiled in the kernel), the character device file /dev/zloop-control can be used to add a zloop device. This is done by writing an "add" command directly to the /dev/zloop-control device:



```text
$ modprobe zloop
$ ls -l /dev/zloop*
crw-------. 1 root root 10, 123 Jan  6 19:18 /dev/zloop-control

$ mkdir -p <base directory/<device ID>
$ echo "add [options]" > /dev/zloop-control
```

The options available for the add command can be listed by reading the /dev/zloop-control device:



```text
$ cat /dev/zloop-control
add id=%d,capacity_mb=%u,zone_size_mb=%u,zone_capacity_mb=%u,conv_zones=%u,base_dir=%s,nr_queues=%u,queue_depth=%u,buffered_io
remove id=%d
```

In more details, the options that can be used with the "add" command are as follows.


<table>
<tbody>
<tr>
<td>


id

</td>
<td>


Device number (the X in /dev/zloopX). Default: automatically assigned.

</td>
</tr>
<tr>
<td>


capacity_mb

</td>
<td>


Device total capacity in MiB. This is always rounded up to the nearest higher multiple of the zone size. Default: 16384 MiB (16 GiB).

</td>
</tr>
<tr>
<td>


zone_size_mb

</td>
<td>


Device zone size in MiB. Default: 256 MiB.

</td>
</tr>
<tr>
<td>


zone_capacity_mb

</td>
<td>


Device zone capacity (must always be equal to or lower than the zone size. Default: zone size.

</td>
</tr>
<tr>
<td>


conv_zones

</td>
<td>


Total number of conventioanl zones starting from sector 0 Default: 8

</td>
</tr>
<tr>
<td>


base_dir

</td>
<td>


Path to the base directory where to create the directory containing the zone files of the device. Default=/var/local/zloop. The device directory containing the zone files is always named with the device ID. E.g. the default zone file directory for /dev/zloop0 is /var/local/zloop/0.

</td>
</tr>
<tr>
<td>


nr_queues

</td>
<td>


Number of I/O queues of the zoned block device. This value is always capped by the number of online CPUs Default: 1

</td>
</tr>
<tr>
<td>


queue_depth

</td>
<td>


Maximum I/O queue depth per I/O queue. Default: 64

</td>
</tr>
<tr>
<td>


buffered_io

</td>
<td>


Do buffered IOs instead of direct IOs (default: false)

</td>
</tr>
<tr>
<td>


zone_append

</td>
<td>


Enable or disable a zloop device native zone append support. Default: 1 (enabled). If native zone append support is disabled, the block layer will emulate this operation using regular write operations.

</td>
</tr>
<tr>
<td>


ordered_zone_append

</td>
<td>


Enable zloop mitigation of zone append reordering. Default: disabled. This is useful for testing file systems file data mapping (extents), as when enabled, this can significantly reduce the number of data extents needed to for a file data mapping.

</td>
</tr>
</tbody>
</table>

<a id="rst-deleting-a-zoned-device"></a>

## 3) Deleting a Zoned Device

Deleting an unused zoned loop block device is done by issuing the "remove" command to /dev/zloop-control, specifying the ID of the device to remove:



```text
$ echo "remove id=X" > /dev/zloop-control
```

The remove command does not have any option.

A zoned device that was removed can be re-added again without any change to the state of the device zones: the device zones are restored to their last state before the device was removed. Adding again a zoned device after it was removed must always be done using the same configuration as when the device was first added. If a zone configuration change is detected, an error will be returned and the zoned device will not be created.

To fully delete a zoned device, after executing the remove operation, the device base directory containing the backing files of the device zones must be deleted.

<a id="rst-example"></a>

## 4) Example

The following sequence of commands creates a 2GB zoned device with zones of 64 MB and a zone capacity of 63 MB:



```text
$ modprobe zloop
$ mkdir -p /var/local/zloop/0
$ echo "add capacity_mb=2048,zone_size_mb=64,zone_capacity_mb=63" > /dev/zloop-control
```

For the device created (/dev/zloop0), the zone backing files are all created under the default base directory (/var/local/zloop):



```text
$ ls -l /var/local/zloop/0
total 0
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000000
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000001
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000002
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000003
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000004
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000005
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000006
-rw-------. 1 root root 67108864 Jan  6 22:23 cnv-000007
-rw-------. 1 root root        0 Jan  6 22:23 seq-000008
-rw-------. 1 root root        0 Jan  6 22:23 seq-000009
...
```

The zoned device created (/dev/zloop0) can then be used normally:



```text
$ lsblk -z
NAME   ZONED        ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN
zloop0 host-managed     64M      32         0         0       1M         4K
$ blkzone report /dev/zloop0
  start: 0x000000000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x000020000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x000040000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x000060000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x000080000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x0000a0000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x0000c0000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x0000e0000, len 0x020000, cap 0x020000, wptr 0x000000 reset:0 non-seq:0, zcond: 0(nw) [type: 1(CONVENTIONAL)]
  start: 0x000100000, len 0x020000, cap 0x01f800, wptr 0x000000 reset:0 non-seq:0, zcond: 1(em) [type: 2(SEQ_WRITE_REQUIRED)]
  start: 0x000120000, len 0x020000, cap 0x01f800, wptr 0x000000 reset:0 non-seq:0, zcond: 1(em) [type: 2(SEQ_WRITE_REQUIRED)]
  ...
```

Deleting this device is done using the command:



```text
$ echo "remove id=0" > /dev/zloop-control
```

The removed device can be re-added again using the same "add" command as when the device was first created. To fully delete a zoned device, its backing files should also be deleted after executing the remove command:



```text
$ rm -r /var/local/zloop/0
```

---

# zram: Compressed RAM-based block devices

_Peios Kernel / Administration / System administration / Block devices_

> The zram module creates RAM-based block devices named /dev/zram<id> (<id> = 0, 1, ...). Pages written to these disks are compressed and stored in memory itself. These disks allow very fast I/O and compression provides good amounts of…

<a id="rst-zram-compressed-ram-based-block-devices"></a>

<a id="rst-introduction"></a>

## Introduction

The zram module creates RAM-based block devices named /dev/zram&lt;id&gt; (&lt;id&gt; = 0, 1, ...). Pages written to these disks are compressed and stored in memory itself. These disks allow very fast I/O and compression provides good amounts of memory savings. Some of the use cases include /tmp storage, use as swap disks, various caches under /var and maybe many more. :)

Statistics for individual zram devices are exported through sysfs nodes at /sys/block/zram&lt;id&gt;/

<a id="rst-usage"></a>

## Usage

There are several ways to configure and manage zram device(-s):

- **a)** using zram and zram_control sysfs attributes

- **b)** using zramctl utility, provided by util-linux ([util-linux@vger.kernel.org](<mailto:util-linux@vger.kernel.org>)).

In this document we will describe only 'manual' zram configuration steps, IOW, zram and zram_control sysfs attributes.

In order to get a better idea about zramctl please consult util-linux documentation, zramctl man-page or `zramctl --help`. Please be informed that zram maintainers do not develop/maintain util-linux or zramctl, should you have any questions please contact [util-linux@vger.kernel.org](<mailto:util-linux@vger.kernel.org>)

Following shows a typical sequence of steps for using zram.

<a id="rst-warning"></a>

## WARNING

For the sake of simplicity we skip error checking parts in most of the examples below. However, it is your sole responsibility to handle errors.

zram sysfs attributes always return negative values in case of errors. The list of possible return codes:


<table>
<tbody>
<tr>
<td>


-EBUSY

</td>
<td>


an attempt to modify an attribute that cannot be changed once the device has been initialised. Please reset device first.

</td>
</tr>
<tr>
<td>


-ENOMEM

</td>
<td>


zram was not able to allocate enough memory to fulfil your needs.

</td>
</tr>
<tr>
<td>


-EINVAL

</td>
<td>


invalid input has been provided.

</td>
</tr>
<tr>
<td>


-EAGAIN

</td>
<td>


re-try operation later (e.g. when attempting to run recompress and writeback simultaneously).

</td>
</tr>
</tbody>
</table>

If you use 'echo', the returned value is set by the 'echo' utility, and, in general case, something like:



```text
echo foo > /sys/block/zram0/comp_algorithm
if [ $? -ne 0 ]; then
        handle_error
fi
```

should suffice.

<a id="rst-load-module"></a>

## 1) Load Module



```text
modprobe zram num_devices=4
```

This creates 4 devices: /dev/zram\{0,1,2,3\}

num_devices parameter is optional and tells zram how many devices should be pre-created. Default: 1.

<a id="rst-select-compression-algorithm"></a>

## 2) Select compression algorithm

Using comp_algorithm device attribute one can see available and currently selected (shown in square brackets) compression algorithms, or change the selected compression algorithm (once the device is initialised there is no way to change compression algorithm).

Examples:



```text
#show supported compression algorithms
cat /sys/block/zram0/comp_algorithm
lzo [lz4]

#select lzo compression algorithm
echo lzo > /sys/block/zram0/comp_algorithm
```

For the time being, the `comp_algorithm` content shows only compression algorithms that are supported by zram.

<a id="rst-set-compression-algorithm-parameters-optional"></a>

## 3) Set compression algorithm parameters: Optional

Compression algorithms may support specific parameters which can be tweaked for particular dataset. ZRAM has an `algorithm_params` device attribute which provides a per-algorithm params configuration.

For example, several compression algorithms support `level` parameter. In addition, certain compression algorithms support pre-trained dictionaries, which significantly change algorithms' characteristics. In order to configure compression algorithm to use external pre-trained dictionary, pass full path to the `dict` along with other parameters:



```text
#pass path to pre-trained zstd dictionary
echo "algo=zstd dict=/etc/dictionary" > /sys/block/zram0/algorithm_params

#same, but using algorithm priority
echo "priority=1 dict=/etc/dictionary" > \
        /sys/block/zram0/algorithm_params

#pass path to pre-trained zstd dictionary and compression level
echo "algo=zstd level=8 dict=/etc/dictionary" > \
        /sys/block/zram0/algorithm_params
```

Parameters are algorithm specific: not all algorithms support pre-trained dictionaries, not all algorithms support `level`. Furthermore, for certain algorithms `level` controls the compression level (the higher the value the better the compression ratio, it even can take negatives values for some algorithms), for other algorithms `level` is acceleration level (the higher the value the lower the compression ratio).

<a id="rst-set-disksize"></a>

## 4) Set Disksize

Set disk size by writing the value to sysfs node 'disksize'. The value can be either in bytes or you can use mem suffixes. Examples:



```text
# Initialize /dev/zram0 with 50MB disksize
echo $((50*1024*1024)) > /sys/block/zram0/disksize

# Using mem suffixes
echo 256K > /sys/block/zram0/disksize
echo 512M > /sys/block/zram0/disksize
echo 1G > /sys/block/zram0/disksize
```

Note: There is little point creating a zram of greater than twice the size of memory since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the size of the disk when not in use so a huge zram is wasteful.

<a id="rst-set-memory-limit-optional"></a>

## 5) Set memory limit: Optional

Set memory limit by writing the value to sysfs node 'mem_limit'. The value can be either in bytes or you can use mem suffixes. In addition, you could change the value in runtime. Examples:



```text
# limit /dev/zram0 with 50MB memory
echo $((50*1024*1024)) > /sys/block/zram0/mem_limit

# Using mem suffixes
echo 256K > /sys/block/zram0/mem_limit
echo 512M > /sys/block/zram0/mem_limit
echo 1G > /sys/block/zram0/mem_limit

# To disable memory limit
echo 0 > /sys/block/zram0/mem_limit
```

<a id="rst-activate"></a>

## 6) Activate



```text
mkswap /dev/zram0
swapon /dev/zram0

mkfs.ext4 /dev/zram1
mount /dev/zram1 /tmp
```

<a id="rst-add-remove-zram-devices"></a>

## 7) Add/remove zram devices

zram provides a control interface, which enables dynamic (on-demand) device addition and removal.

In order to add a new /dev/zramX device, perform a read operation on the hot_add attribute. This will return either the new device's device id (meaning that you can use /dev/zram&lt;id&gt;) or an error code.

Example:



```text
cat /sys/class/zram-control/hot_add
1
```

To remove the existing /dev/zramX device (where X is a device id) execute:



```text
echo X > /sys/class/zram-control/hot_remove
```

<a id="rst-stats"></a>

## 8) Stats

Per-device statistics are exported as various nodes under /sys/block/zram&lt;id&gt;/

A brief description of exported device attributes follows. For more details please read Documentation/ABI/testing/sysfs-block-zram.


<table>
<thead>
<tr>
<th>


Name

</th>
<th>


access

</th>
<th>


description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


disksize

</td>
<td>


RW

</td>
<td>


show and set the device's disk size

</td>
</tr>
<tr>
<td>


initstate

</td>
<td>


RO

</td>
<td>


shows the initialization state of the device

</td>
</tr>
<tr>
<td>


reset

</td>
<td>


WO

</td>
<td>


trigger device reset

</td>
</tr>
<tr>
<td>


mem_used_max

</td>
<td>


WO

</td>
<td>


reset the `mem_used_max` counter (see later)

</td>
</tr>
<tr>
<td>


mem_limit

</td>
<td>


WO

</td>
<td>


specifies the maximum amount of memory ZRAM can use to store the compressed data

</td>
</tr>
<tr>
<td>


writeback_limit

</td>
<td>


WO

</td>
<td>


specifies the maximum amount of write IO zram can write out to backing device as 4KB unit

</td>
</tr>
<tr>
<td>


writeback_limit_enable

</td>
<td>


RW

</td>
<td>


show and set writeback_limit feature

</td>
</tr>
<tr>
<td>


writeback_batch_size

</td>
<td>


RW

</td>
<td>


show and set maximum number of in-flight writeback operations

</td>
</tr>
<tr>
<td>


compressed_writeback

</td>
<td>


RW

</td>
<td>


show and set compressed writeback feature

</td>
</tr>
<tr>
<td>


comp_algorithm

</td>
<td>


RW

</td>
<td>


show and change the compression algorithm

</td>
</tr>
<tr>
<td>


algorithm_params

</td>
<td>


WO

</td>
<td>


setup compression algorithm parameters

</td>
</tr>
<tr>
<td>


compact

</td>
<td>


WO

</td>
<td>


trigger memory compaction

</td>
</tr>
<tr>
<td>


debug_stat

</td>
<td>


RO

</td>
<td>


this file is used for zram debugging purposes

</td>
</tr>
<tr>
<td>


backing_dev

</td>
<td>


RW

</td>
<td>


set up backend storage for zram to write out

</td>
</tr>
<tr>
<td>


idle

</td>
<td>


WO

</td>
<td>


mark allocated slot as idle

</td>
</tr>
</tbody>
</table>

User space is advised to use the following files to read the device statistics.

File /sys/block/zram&lt;id&gt;/stat

Represents block layer statistics. Read [Documentation/block/stat.rst](/kernel/internals/block/stat.md) for details.

File /sys/block/zram&lt;id&gt;/io_stat

The stat file represents device's I/O statistics not accounted by block layer and, thus, not available in zram&lt;id&gt;/stat file. It consists of a single line of text and contains the following stats separated by whitespace:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> failed_reads
>
> </td>
> <td>
>
>
> The number of failed reads
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> failed_writes
>
> </td>
> <td>
>
>
> The number of failed writes
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> invalid_io
>
> </td>
> <td>
>
>
> The number of non-page-size-aligned I/O requests
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> notify_free
>
> </td>
> <td>
>
>
> Depending on device usage scenario it may account
>
> - **a)** the number of pages freed because of swap slot free notifications
>
> - **b)** the number of pages freed because of REQ_OP_DISCARD requests sent by bio. The former ones are sent to a swap block device when a swap slot is freed, which implies that this disk is being used as a swap disk.
>
> The latter ones are sent by filesystem mounted with discard option, whenever some data blocks are getting discarded.
>
> </td>
> </tr>
> </tbody>
> </table>

File /sys/block/zram&lt;id&gt;/mm_stat

The mm_stat file represents the device's mm statistics. It consists of a single line of text and contains the following stats separated by whitespace:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> orig_data_size
>
> </td>
> <td>
>
>
> uncompressed size of data stored in this disk. Unit: bytes
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> compr_data_size
>
> </td>
> <td>
>
>
> compressed size of data stored in this disk
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> mem_used_total
>
> </td>
> <td>
>
>
> the amount of memory allocated for this disk. This includes allocator fragmentation and metadata overhead, allocated for this disk. So, allocator space efficiency can be calculated using compr_data_size and this statistic. Unit: bytes
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> mem_limit
>
> </td>
> <td>
>
>
> the maximum amount of memory ZRAM can use to store the compressed data
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> mem_used_max
>
> </td>
> <td>
>
>
> the maximum amount of memory zram has consumed to store the data
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> same_pages
>
> </td>
> <td>
>
>
> the number of same element filled pages written to this disk. No memory is allocated for such pages.
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> pages_compacted
>
> </td>
> <td>
>
>
> the number of pages freed during compaction
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> huge_pages
>
> </td>
> <td>
>
>
> the number of incompressible pages
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> huge_pages_since
>
> </td>
> <td>
>
>
> the number of incompressible pages since zram set up
>
> </td>
> </tr>
> </tbody>
> </table>

File /sys/block/zram&lt;id&gt;/bd_stat

The bd_stat file represents a device's backing device statistics. It consists of a single line of text and contains the following stats separated by whitespace:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> bd_count
>
> </td>
> <td>
>
>
> size of data written in backing device. Unit: 4K bytes
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> bd_reads
>
> </td>
> <td>
>
>
> the number of reads from backing device Unit: 4K bytes
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> bd_writes
>
> </td>
> <td>
>
>
> the number of writes to backing device Unit: 4K bytes
>
> </td>
> </tr>
> </tbody>
> </table>

<a id="rst-deactivate"></a>

## 9) Deactivate



```text
swapoff /dev/zram0
umount /dev/zram1
```

<a id="rst-reset"></a>

## 10) Reset

> Write any positive value to 'reset' sysfs node:
>
>
>
> ```text
> echo 1 > /sys/block/zram0/reset
> echo 1 > /sys/block/zram1/reset
> ```
>
> This frees all the memory allocated for the given device and resets the disksize to zero. You must set the disksize again before reusing the device.

<a id="rst-optional-feature"></a>

## Optional Feature

<a id="rst-idle-pages-tracking"></a>

### IDLE pages tracking

zram has built-in support for idle pages tracking (that is, allocated but not used pages). This feature is useful for e.g. zram writeback and recompression. In order to mark pages as idle, execute the following command:



```text
echo all > /sys/block/zramX/idle
```

This will mark all allocated zram pages as idle. The idle mark will be removed only when the page (block) is accessed (e.g. overwritten or freed). Additionally, when CONFIG_ZRAM_TRACK_ENTRY_ACTIME is enabled, pages can be marked as idle based on how many seconds have passed since the last access to a particular zram page:



```text
echo 86400 > /sys/block/zramX/idle
```

In this example, all pages which haven't been accessed in more than 86400 seconds (one day) will be marked idle.

<a id="rst-writeback"></a>

### writeback

With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page to backing storage rather than keeping it in memory. To use the feature, admin should set up backing device via:



```text
echo /dev/sda5 > /sys/block/zramX/backing_dev
```

before disksize setting. It supports only partitions at this moment. If admin wants to use incompressible page writeback, they could do it via:



```text
echo huge > /sys/block/zramX/writeback
```

Admin can request writeback of idle pages at right timing via:



```text
echo idle > /sys/block/zramX/writeback
```

With the command, zram will writeback idle pages from memory to the storage.

Additionally, if a user choose to writeback only huge and idle pages this can be accomplished with:



```text
echo huge_idle > /sys/block/zramX/writeback
```

If a user chooses to writeback only incompressible pages (pages that none of algorithms can compress) this can be accomplished with:



```text
echo incompressible > /sys/block/zramX/writeback
```

If an admin wants to write a specific page in zram device to the backing device, they could write a page index into the interface:



```text
echo "page_index=1251" > /sys/block/zramX/writeback
```

In Linux 6.16 this interface underwent some rework.  First, the interface now supports `key=value` format for all of its parameters (`type=huge_idle`, etc.)  Second, the support for `page_indexes` was introduced, which specify `LOW-HIGH` range (or ranges) of pages to be written-back.  This reduces the number of syscalls, but more importantly this enables optimal post-processing target selection strategy. Usage example:



```text
echo "type=idle" > /sys/block/zramX/writeback
echo "page_indexes=1-100 page_indexes=200-300" > \
        /sys/block/zramX/writeback
```

We also now permit multiple page_index params per call and a mix of single pages and page ranges:



```text
echo page_index=42 page_index=99 page_indexes=100-200 \
        page_indexes=500-700 > /sys/block/zramX/writeback
```

If there are lots of write IO with flash device, potentially, it has flash wearout problem so that admin needs to design write limitation to guarantee storage health for entire product life.

To overcome the concern, zram supports "writeback_limit" feature. The "writeback_limit_enable"'s default value is 0 so that it doesn't limit any writeback. IOW, if admin wants to apply writeback budget, they should enable writeback_limit_enable via:



```text
$ echo 1 > /sys/block/zramX/writeback_limit_enable
```

Once writeback_limit_enable is set, zram doesn't allow any writeback until admin sets the budget via /sys/block/zramX/writeback_limit.

(If admin doesn't enable writeback_limit_enable, writeback_limit's value assigned via /sys/block/zramX/writeback_limit is meaningless.)

If admin wants to limit writeback as per-day 400M, they could do it like below:



```text
$ MB_SHIFT=20
$ 4K_SHIFT=12
$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
        /sys/block/zram0/writeback_limit.
$ echo 1 > /sys/block/zram0/writeback_limit_enable
```

If admins want to allow further write again once the budget is exhausted, they could do it like below:



```text
$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
        /sys/block/zram0/writeback_limit
```

If an admin wants to see the remaining writeback budget since last set:



```text
$ cat /sys/block/zramX/writeback_limit
```

If an admin wants to disable writeback limit, they could do:



```text
$ echo 0 > /sys/block/zramX/writeback_limit_enable
```

The writeback_limit count will reset whenever you reset zram (e.g., system reboot, echo 1 &gt; /sys/block/zramX/reset) so keeping how many of writeback happened until you reset the zram to allocate extra writeback budget in next setting is user's job.

By default zram stores written back pages in decompressed (raw) form, which means that writeback operation involves decompression of the page before writing it to the backing device.  This behavior can be changed by enabling `compressed_writeback` feature, which causes zram to write compressed pages to the backing device, thus avoiding decompression overhead.  To enable this feature, execute:



```text
$ echo yes > /sys/block/zramX/compressed_writeback
```

Note that this feature should be configured before the `zramX` device is initialized.

Depending on backing device storage type, writeback operation may benefit from a higher number of in-flight write requests (batched writes).  The number of maximum in-flight writeback operations can be configured via `writeback_batch_size` attribute.  To change the default value (which is 32), execute:



```text
$ echo 64 > /sys/block/zramX/writeback_batch_size
```

If admin wants to measure writeback count in a certain period, they could know it via /sys/block/zram0/bd_stat's 3rd column.

<a id="rst-recompression"></a>

### recompression

With CONFIG_ZRAM_MULTI_COMP, zram can recompress pages using alternative (secondary) compression algorithms. The basic idea is that alternative compression algorithm can provide better compression ratio at a price of (potentially) slower compression/decompression speeds. Alternative compression algorithm can, for example, be more successful compressing huge pages (those that default algorithm failed to compress). Another application is idle pages recompression - pages that are cold and sit in the memory can be recompressed using more effective algorithm and, hence, reduce zsmalloc memory usage.

With CONFIG_ZRAM_MULTI_COMP, zram supports up to 4 compression algorithms: one primary and up to 3 secondary ones. Primary zram compressor is explained in "3) Select compression algorithm", secondary algorithms are configured using recomp_algorithm device attribute.

Example::



```text
#show supported recompression algorithms
cat /sys/block/zramX/recomp_algorithm
#1: lzo lzo-rle lz4 lz4hc [zstd]
#2: lzo lzo-rle lz4 [lz4hc] zstd
```

Alternative compression algorithms are sorted by priority. In the example above, zstd is used as the first alternative algorithm, which has priority of 1, while lz4hc is configured as a compression algorithm with priority 2. Alternative compression algorithm's priority is provided during algorithms configuration::



```text
#select zstd recompression algorithm, priority 1
echo "algo=zstd priority=1" > /sys/block/zramX/recomp_algorithm

#select deflate recompression algorithm, priority 2
echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm
```

Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress, which controls recompression.

Examples::



```text
#IDLE pages recompression is activated by `idle` mode
echo "type=idle" > /sys/block/zramX/recompress

#HUGE pages recompression is activated by `huge` mode
echo "type=huge" > /sys/block/zram0/recompress

#HUGE_IDLE pages recompression is activated by `huge_idle` mode
echo "type=huge_idle" > /sys/block/zramX/recompress
```

The number of idle pages can be significant, so user-space can pass a size threshold (in bytes) to the recompress knob: zram will recompress only pages of equal or greater size::



```text
#recompress all pages larger than 3000 bytes
echo "threshold=3000" > /sys/block/zramX/recompress

#recompress idle pages larger than 2000 bytes
echo "type=idle threshold=2000" > /sys/block/zramX/recompress
```

It is also possible to limit the number of pages zram re-compression will attempt to recompress::



```text
echo "type=huge_idle max_pages=42" > /sys/block/zramX/recompress
```

During re-compression for every page, that matches re-compression criteria, ZRAM iterates the list of registered alternative compression algorithms in order of their priorities. ZRAM stops either when re-compression was successful (re-compressed object is smaller in size than the original one) and matches re-compression criteria (e.g. size threshold) or when there are no secondary algorithms left to try. If none of the secondary algorithms can successfully re-compressed the page such a page is marked as incompressible, so ZRAM will not attempt to re-compress it in the future.

This re-compression behaviour, when it iterates through the list of registered compression algorithms, increases our chances of finding the algorithm that successfully compresses a particular page. Sometimes, however, it is convenient (and sometimes even necessary) to limit recompression to only one particular algorithm so that it will not try any other algorithms. This can be achieved by providing a `algo` or `priority` parameter::



```text
#use zstd algorithm only (if registered)
echo "type=huge algo=zstd" > /sys/block/zramX/recompress

#use zstd algorithm only (if zstd was registered under priority 1)
echo "type=huge priority=1" > /sys/block/zramX/recompress
```

<a id="rst-memory-tracking"></a>

## memory tracking

With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the zram block. It could be useful to catch cold or incompressible pages of the process with\*pagemap.

If you enable the feature, you could see block state via /sys/kernel/debug/zram/zram0/block_state". The output is as follows:



```text
300    75.033841 .wh...
301    63.806904 s.....
302    63.806919 ..hi..
303    62.801919 ....r.
304   146.781902 ..hi.n
```

- **First column**

  zram's block index.

- **Second column**

  access time since the system was booted

- **Third column**

  state of the block:

  - **s:**

    same page

  - **w:**

    written page to backing store

  - **h:**

    huge page

  - **i:**

    idle page

  - **r:**

    recompressed page (secondary compression algorithm)

  - **n:**

    none (including secondary) of algorithms could compress it

First line of above example says 300th block is accessed at 75.033841sec and the block's state is huge so it is written back to the backing storage. It's a debugging feature so anyone shouldn't rely on it to work properly.

Nitin Gupta [ngupta@vflare.org](<mailto:ngupta@vflare.org>)
