Okay, quite often the configuration of a feature or application mandates that the data on the disk doesn´t change while you activate it. Okay, an easy way would be simply unmounting the disk. Okay, that´s possible but then you can´t access the data on the disk at all. You can´t even read from the filesystem, albeit this doesn´t change anything on the disk (okay, as long you´ve mounted the disk with noatime).
So: How do you ensure, that the content of a filesystem doesn´t change while you work with the disk. ufs has a an interesting feature. It´s called
lockfs you can lock the filesystem. You can lock it to an extend that you can only unmount and remount it to gather access to the data, but you can lock only some ways to access it.
Types of Locks
The
lockfs command can establish a number of locks on an UFS filesystem:
- Delete Lock (
-d):suspends 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 (
-n):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 wenn all data has been written to disk and the log has been rolled
Write Lock
Okay, let´s assume we´ve mounted an UFS filesystem at
/mnt.
# echo "test" > testfile1
# ls
lost+found testfile1
No problem. Our testfile found it´s way into file system. Okay, now we establish a write lock on our file system.
# lockfs -w /mnt
You set the locks with the
lockfs command, the switch
-w tells command to set an 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 addtional 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
Not problem. The command returns an an instance. 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. It´s of less practical use, as you would think at first. You can´t delete a file, but you can zero it or fill it with other content. Okay, 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 add a comment. You can use this command to tell other admins 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 rm just 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 ist, you can overwrite it:# echo "test" > testfile3
# echo "test" >> testfile3
# echo "test" > testfile3
There is only one thing you can´t do with this new file: You simply can´t delete it.# rm testfile3
^C
Now we release the lock# lockfs -u /mnt
Now you can clean up your testdirectory /mnt again.# rm testfile2
# rm testfile3
Conclusion
The lockfs is a really neat feature to deny certain accesses to your filesystem without unmounting it completly. Some locks are more useful for general use than other other. 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 lock or name lock are more of use, when you need a stable directory. The usecase is more an internal one for other tools.
Do you want to learn more?
Documentation
docs.sun.com: man page lockfs