UFS

From PacBSD Wiki
Jump to: navigation, search


From Wikipedia

The Unix file system (UFS; also called the Berkeley Fast File System, the BSD Fast File System or FFS) is a file system used by many Unix and Unix-like operating systems. It is a distant descendant of the original filesystem used by Version 7 Unix.

Soft updates

From Wikipedia

Soft updates is an approach to maintaining file system meta-data integrity in the event of a crash or power outage. Soft updates work by tracking and enforcing dependencies among updates to file system meta-data. Soft updates are an alternative to the more commonly used approach of journaling file systems.

Soft updates allow only asynchronous metadata writes that does not cause inconsistencies in the filesystem. It avoids having to do ordered synchronous metadata writes by temporarily "rolling back" any part of a metadata block that depends on another potentially non-flushed (not yet written to disk) or partially rolled-back (reverting back to what is on disk) block when writing. This basically means that blocks may be flushed (written to disk) at any time and the soft-update code will always ensure what is on the disk is consistent. This makes recovering from a crash a simple matter of running a background process of walking the filesystem and garbage collecting any allocated space that has been orphaned. This is a relatively fast operation as there are no logs to be replayed during the recovery process like with there is with journaling. Another advantage aside from fast recovery times this brings is creating a file, using it for a short period of time then deleting it may cause no disk activity at all.

There are some disadvantages to this approach: first, in case of a crash the recovered file system could easily be several seconds or even a minute older as there were blocks that have yet to be flushed before the crash happened causing that data to be lost. Secondly, soft updates delay the freeing of file system blocks for the same reason that if a crash were to happen the filesystem would be restored to an "older" state.

New UFS filesystem with Soft Updates

See File System Creation

Converting existing file system from Journaling to Soft Updates

It is possible to switch from journaling to soft updates on an existing filesystem, with tunefs(8). It is important to ensure to make a backup before attempting to alter an existing filesystem or risk losing all data on it.

To alter the current filesystem to use soft updates use tunefs:


Note: TuneFS utility cannot be ran on an active file system. To make changes to the file system it must either be remounted as read-only or unmounted all together.
# tunefs -J enable /dev/ada0p2

If there is no errors then the filesystem can be remounted and will be journaled from this point on.

Journaling

Aside from using Soft Updates, UFS can also be journaled although the journal implementation is provided through a GOEM extension rather than being implemented as part of the file system. Due to this journaling in UFS is done at the block level and managed via gjournal(8). Journaling stores a log of file system transactions, such as changes that make up a complete disk write operation, before metadata and file writes are committed to the disk. This transaction log can later be replayed to redo file system transactions preventing file system inconsistencies. This method provides another mechanism to protect against data loss and inconsistencies of the file system due to a crash or power loss. Unlike Soft Updates, which tracks and enforces metadata updates and snapshots, which create an image of the file system in memory, a log is stored in disk space specifically for this task. The trade off is Journaling avoids the file system being restored to an "older state" after a crash at the cost of a slightly longer recovery process as the system has to play back to log to try and rebuild the filesystem to a state pre-crash.

To enable Journaling support in UFS first make sure that the geom_journal.ko kernel module gets loaded at boot time:

# echo 'geom_journal_load="YES"' >> /boot/loader.conf

New UFS filesystem with Journaling

To create a new UFS filesystem using journaling instead of Soft Updates first a journal must be created for the new filesystem:

# gjournal load
# gjournal label /dev/ada0p2

This will load the module and create a /dev/ada0p2.journal device which will need to be used when addressing this partition (using gpart, newfs, mount, etc).

With the journal block device created the filesystem can now be creating making sure to give the flag for journaling (-J) to enable it instead of soft updates:

# newfs -J /dev/ada0p2.journal

Converting existing file system from Soft Updates to Journal

It is possible to switch from soft updates to journaling on an existing filesystem, with tunefs(8). It is important to ensure to make a backup before attempting to alter an existing filesystem or risk losing all data on it. In most cases, gjournal will fail if it is unable to create the journal, but this does not protect against data loss caused by misusing tunefs.

To alter an existing UFS filesystem to use journaling instead of Soft Updates first a journal must be created for the filesystem:

# gjournal load
# gjournal label /dev/ada0p2

This will load the module and create a /dev/ada0p2.journal device which will need to be used when addressing this partition (using gpart, newfs, mount, etc).

Then alter the current filesystem with tunefs:


Note: TuneFS utility cannot be ran on an active file system. To make changes to the file system it must either be remounted as read-only or unmounted all together.
# tunefs -J enable /dev/ada0p2

If there is no errors then the filesystem can be remounted and will be journaled from this point on.

Soft Updates vs Journaling

Soft Updates and Journaling are both approaches to maintaining file system metadata integrity in the event of a crash or power outages. Both have been proven effective over time, and both approaches have their pros and cons. For most users Soft Updates will probably be good enough, and with soft update journaling the amount of data lost do to a crash or power outage can be minimized by introducing a log of changes that soft updates has stored in RAM waiting to be written to disk. In cases where potential losing seconds to a full minute worth of changes is to much, then journaling UFS may be the best option.

File System Creation

Note: This guide uses /dev/ada0 and /dev/adap2 as examples, make sure to use the correct drive and partition when creating your file system.

This guide is only an example of a simple single partition setup that not only contains the root partition but also the home partition.

To format the partition with UFS2 use:

# newfs -U -j -L root /dev/ada0p2

This will create a new UFS2 filesystem on /dev/ada0p2 labeled "root" that has soft updates (-U) and soft updates journaling (-j) enabled.