What is NILFS?

NILFS is a log-structured file system (LFS) developed for the Linux kernel 2.6. NILFS supports continuous snapshotting, so users can restore files mistakenly overwritten or destroyed. In addition, LFS has the characteristic that all file system data including metadata is written in a log-like format. Data is never overwritten, only appended in the file system. This nature of LFS is not only well-suited to keep past data and namespace data, but also has benefits to ensure consistency after a system crash or an unclean shutdown like journaling filesystems and to get better performance especially for flash devices such as Solid State Drives (SSD).

NILFS also has the following specific features:

NILFS is an abbreviation of the New Implementation of a Log-structured File System.

What is NILFS version 2?

NILFS version 2 (NILFS2) is a new version of NILFS redesigned for practical use. NILFS2 realizes online garbage collection that reclaims disk space with keeping multiple snapshots. As well as preserving recent data and namespaces of a filesystem which save users from unexpected data lost, NILFS2 can keep selected filesystem checkpoints (i.e. snapshots) for long periods, for an indefinite period if you want. This flexible snapshot feature helps you to maintain significant versions of data or system state, and would make snapshotting or versioning of the POSIX filesystem much familiar to you.

Snapshot administration is easy and quickly performable as well as its creation is instantaneous and spontaneous like NILFS version 1. You can receive full benefits of snapshotting without wasting valuable time. The possible use of NILFS2 includes disaster recovery, versioning, tamper detection, SOX compliance logging, and so on. It can serve as an alternative filesystem for Linux desktop environment, or as a basis of advanced storage appliances.

Using NILFS

Here we illustrate by examples how to use NILFS version 2:

  1. Format a disk partition with ``mkfs''.

    e.g.
    # mkfs -t nilfs2 /dev/sdb1
    mkfs.nilfs2 ver 2.0
    Start writing file system initial data to the device
           Blocksize:4096  Device:/dev/sdb1  Device Size:73402366464
    File system initialization succeeded !! 
    
  2. Mount it on a mount-point with ``mount'' command,

    # mkdir /nilfs
    # mount -t nilfs2 /dev/sdb1 /nilfs
    

    This will invoke a garbage collector (GC) through an external mount program (i.e. ``mount.nilfs2''). This GC is implemented as an userland daemon whose name is ``nilfs_cleanerd''.

  3. Use the nilfs mount point as usual

    It is available as an ordinary POSIX filesystem.

  4. Make snapshots

    NILFS makes ``checkpoints'' at regular intervals (unless there is no change) or with synchronous writings. Each checkpoint represents a consistent state of NILFS filesystem, and a number of checkpoints are created continuously. There is no practical limit on the number of checkpoints and snapshots.

    These checkpoints can be listed by ``lscp'' command.

    $ lscp
           CNO        DATE     TIME  MODE  SKT   NBLKINC       ICNT
             1  2008-05-08 14:45:49  cp     -         11          3
             2  2008-05-08 14:50:22  cp     -     200523         81
             3  2008-05-08 20:40:34  cp     -        136         61
             4  2008-05-08 20:41:20  cp     -     187666       1604
             5  2008-05-08 20:41:42  cp     -         51       1634
             6  2008-05-08 20:42:00  cp     -         37       1653
             7  2008-05-08 20:42:42  cp     -     272146       2116
             8  2008-05-08 20:43:13  cp     -     264649       2117
             9  2008-05-08 20:43:44  cp     -     285848       2117
            10  2008-05-08 20:44:16  cp     -     139876       7357
    

    A snapshot is the checkpoint marked not to be deleted by GC. It is possible to make a snapshot of the current state by ``mkcp'' command, and is also possible to change an existing checkpoint into a snapshot. The checkpoint and the snapshot are handled through the following userland tools:

     lscp     lists checkpoints.
     mkcp     makes a checkpoint.
     mkcp -s  makes a snapshot.
     chcp     changes an existing checkpoint to a snapshot or
              vice versa.
     rmcp     invalidates specified checkpoint(s).
    

    In the following example, the existing checkpoint whose checkpoint number is two, is changed into a snapshot after a period of time.

    $ sudo chcp ss 2
    $ lscp
           CNO        DATE     TIME  MODE  SKT   NBLKINC       ICNT
             1  2008-05-08 14:45:49  cp     -         11          3
             2  2008-05-08 14:50:22  ss     -     200523         81
             3  2008-05-08 20:40:34  cp     -        136         61
             4  2008-05-08 20:41:20  cp     -     187666       1604
             5  2008-05-08 20:41:42  cp     -         51       1634
             6  2008-05-08 20:42:00  cp     -         37       1653
             7  2008-05-08 20:42:42  cp     -     272146       2116
             8  2008-05-08 20:43:13  cp     -     264649       2117
             9  2008-05-08 20:43:44  cp     -     285848       2117
            10  2008-05-08 20:44:16  cp     -     139876       7357
            11  2008-05-08 21:05:23  cp     -         10       7357
    

    The recent checkpoints are protected from GC during the period given by a GC parameter ``protection_period''; GC never deletes checkpoints whose age from its creation time is less than the value given by the protection_period on the second time scale.

    GC parameters including the protection_period are described in /etc/nilfs_cleanerd.conf, and the behaviour of GC is adjustable by rewriting the file.

  5. Mount snapshots.

    Snapshots are mountable as read-only filesystems. They are mounted with two options, a read-only option (``-r'' or ``-o ro'') and the ``cp'' option which specifies the checkpoint number.

    # mount -t nilfs2 -r -o cp=2 /dev/sdb1 /nilfs-cp
    # df -t nilfs2
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/sdb1             71679996   3203068  64888832   5% /nilfs
    /dev/sdb1             71679996   3203068  64888832   5% /nilfs-cp
    # mount -t nilfs2
    /dev/sdb1 on /nilfs type nilfs2 (rw,gcpid=13296)
    /dev/sdb1 on /nilfs-cp type nilfs2 (ro,cp=2)
    

    The ``current'' filesystem and snapshots are mountable independently, which means that the online backup is possible with NILFS.

  6. Unmount snapshots or rw-mount.

    # umount /nilfs
    # umount /nilfs-cp
    

    When unmounting the ``current'' filesystem, this will shutdown GC for the partition.