lockfs

lockfs

Quite often the configuration of a feature or application mandates that the data on the disk doesn’t change while you activate it. An easy way to achieve this would be simply un-mounting the disk - that’s possible but then you can’t access the data on the disk at all. You can’t even read from the filesystem, even though this doesn’t change anything on the disk (okay, as long you’ve mounted the disk with noatime).

So: How else can you ensure that the content of a filesystem doesn’t change while you work with the disk? ufs has an interesting feature. It’s called lockfs and with it, you can lock the filesystem. You can lock it to an extent that you can only unmount and remount it to gather access to the data, but you can also lock out a subset of the many ways in which one might try to access it.

Types of Locks

The lockfs command can establish a number of different locks on an UFS filesystem:

  • Delete Lock -d: suspends all access that could remove directory entries

  • Hard Lock -h: suspends all access to the filesystem. It can’t be unlocked. You have to unmount and remount it

  • Name Lock -n: suspends all access to the filesystem that could remove or change directory entries

  • Error Lock -e: this lock is normally established when UFS detects an internal inconsistency. It’s released by the usage of fsck. It suspends all access to the filesystem.

  • write lock -w: this lock suspends all accesses that could modify the filesystem

  • flush log -f: this isn’t really a lock - this option forces a synchronous flush of the named filesystem. It returns when all data has been written to disk and the log has been rolled

Write Lock

Okay, let’s assume we’ve mounted a UFS filesystem at /mnt.

# cd /mnt

# echo "test" > testfile1

# ls 

lost+found  testfile1

No problem. Our testfile found its way into the file system. Now we establish a write lock on our file system.

# lockfs -w /mnt

You set the lock with the lockfs command, and the switch -w tells lockfs to set a write lock. With a write lock, you can read a filesystem, but you can’t write to it.

Okay, let’s check the existing locks. You use the lockfs command without any further options.

# lockfs   

Filesystem           Locktype   Comment
/mnt                 write

When we try to add an additional file, the write system call simply blocks.

# echo "test" > testfile2

^Cbash: testfile2: Interrupted system call

We have to break the echo command with CTRL-C. Okay, now let’s release the lock.

# lockfs -u /mnt

The -u commands lockfs to release the lock. When you list the existing locks, the lock on /mnt is gone.

# lockfs

And just to check it, we try to write "test" to testfile2 again.

# echo "test" > testfile2

The command returns instantly. When you check the filesystem, you will see both files.

# ls -l

total 20

drwx------   2 root     root        8192 Apr 25 18:10 lost+found

-rw-r--r--   1 root     root           5 Apr 27 03:49 testfile1

-rw-r--r--   1 root     root           5 Apr 27 03:50 testfile2

Delete lock

A rather strange kind of lock is the delete lock. It blocks all delete operations. This is actually of less practical use than you might think at first. You can’t delete a file, but you can stil zero it or fill it with other content. Let’s use the testfile from our last example. At first we try to delete the first file:

# rm testfile1

No problem. Now we establish the delete lock. This time we also add a comment. You can use this command to tell other administrators why you have established the lock.

# lockfs -c "no deletes today" -d /mnt

When you check for existing locks, you will see the delete lock on /mnt and the comment:

# lockfs

Filesystem           Locktype   Comment

/mnt                 delete     no deletes today

When you try to delete the file, the rmjust blocks and you have to break it with CTRL-C again:

# rm testfile2

^C

When you’ve delete-locked an filesystem, you can create new files, you can append data to existing files and you can overwrite them:

# echo "test" > testfile3

# echo "test" >> testfile3

# echo "test" > testfile3

There is only one thing you can’t do with this new file: delete it.

# rm testfile3

^C

Next we release the lock

# lockfs -u /mnt

Now you can clean up your test directory /mntagain.

# rm testfile2

# rm testfile3

Conclusion

The lockfs is a really neat feature to deny certain accesses to your filesystem without un-mounting it completely. Some locks are more useful for general use than others. For example, the write lock is really useful when you want to freeze the content of the filesystem while working with tools like AVS. Delete locks or name locks are useful when you need a stable directory, which is less of a day-to-day problem for administrators.

Do you want to learn more?

Documentation docs.sun.com: man page lockfs[^4]