First, is the lock information part of the file? After a power failure is restarted, it will automatically disappear or will it never be lifted (because the lock-setting process no longer exists )? First, is the lock information part of the file?
After a power failure is restarted, it will automatically disappear or will it never be lifted (because the lock-setting process no longer exists )?
Reply content:
First, is the lock information part of the file?
After a power failure is restarted, it will automatically disappear or will it never be lifted (because the lock-setting process no longer exists )?
File lock in Linux
There are two different file lock mechanisms in Linux:flock
The system calls to lock the entire file.fcntl
System Call completes fine-grained record locks (locking a range in a file)
Open System Call
In Linux, when a file is initially openedopen
This system call.
Many things are done in the open system call (@./fs/open. c), which is mainly related to file lock allocation.struct file
Andstruct inode *inode
Andstruct file_operations *f_op
Assign values to members.
Inode is allocated to the corresponding file system (for example, the ext4 file system's allocation function is in fs/ext4/inode. cext4_iget
), Which also containsstruct file_operations *
Type Memberi_fop
That is, a collection of function pointers for file system operations.
The two times mentioned abovestruct file_operations
Object inclusionflock
Andlock
Two function pointers are used for the two different lock mechanisms described above.
The kernel allocates only one file to the memory.struct inode
Object. When different processes open the same file, onestruct file
Object, andfile->inode
Point to the inode object corresponding to the file. Wherefile->f_op
In the process of calling the open systeminode->i_fop
Copied. (Open-> do_sys_open-> do_filp_open-> path_openat-> do_last-> vfs_open-> do_dentry_open:f->f_op = fops_get(inode->i_fop)
)
Flock lock
The kernel first findsstruct file
Object, judgmentfile->f_op->flock
Whether the pointer is valid. if the pointer is valid, the file system specifies the locking mechanism, for example, fs/fuse/file. c specifiesfuse_file_flock
Function, but ext2/3/4 is not specified), call the function pointed to by the pointer to execute the lock operation; if not, executeflock_lock_file_wait
->flock_lock_inode
(@ Fs/locks. c ).
If the file system does not specify the locking mechanismflock_lock_inode
Will try to apply the file lock to the inode corresponding to the file (the locking mechanism is trivial, I didn't see it too fine, It is roughly to getinode->i_flctx->flc_lock
This spinlock is added, deleted, modified, and queried.inode->i_flctx->flc_flock
This linked list, but it can be seen that it is all memory operations ).
If the file system specifies the locking mechanism, it is necessary to check whether the corresponding locking mechanism is persistent on the disk. However, we need to check the locking requirement, in most cases, it should be expected to be quickly completed. except in special cases, it can be considered that operations are only performed in the memory and not persisted to the disk.
Fcntl lock
Andflock
Similarly, the difference is that the judgment function pointer isfile->f_op->lock
If it does not exist, callposix_lock_file
Apply a record lock to the inode.inode->i_flctx
The above is not used.inode->i_flctx->flc_lock
The specific operations of this linked list are more complex, and we need to consider the intersection of record locks and other issues.
Conclusion
The specific situation depends on whether the specified locking mechanism is used by the file system. If there is a lock mechanism, analyze it separately.
If no, all locks are maintained in the memory. If the power is down, the lock will automatically fail. This is also in line with the general understanding.
In addition, the lock action on files is generally performed by the process. When the process exits (including unexpected exit), the exit system callsexit_files
The function clears all files opened by this process.