Lock data files in linux. Suppose a sudden power failure occurs. Will the lock always exist on this file after restart?

Source: Internet
Author: User
Tags flock flock lock
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:flockThe system calls to lock the entire file.fcntlSystem Call completes fine-grained record locks (locking a range in a file)

Open System Call

In Linux, when a file is initially openedopenThis system call.

Many things are done in the open system call (@./fs/open. c), which is mainly related to file lock allocation.struct fileAndstruct inode *inodeAndstruct file_operations *f_opAssign 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_fopThat is, a collection of function pointers for file system operations.

The two times mentioned abovestruct file_operationsObject inclusionflockAndlockTwo function pointers are used for the two different lock mechanisms described above.

The kernel allocates only one file to the memory.struct inodeObject. When different processes open the same file, onestruct fileObject, andfile->inodePoint to the inode object corresponding to the file. Wherefile->f_opIn the process of calling the open systeminode->i_fopCopied. (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 fileObject, judgmentfile->f_op->flockWhether the pointer is valid. if the pointer is valid, the file system specifies the locking mechanism, for example, fs/fuse/file. c specifiesfuse_file_flockFunction, 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_inodeWill 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_lockThis spinlock is added, deleted, modified, and queried.inode->i_flctx->flc_flockThis 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

AndflockSimilarly, the difference is that the judgment function pointer isfile->f_op->lockIf it does not exist, callposix_lock_fileApply a record lock to the inode.inode->i_flctxThe above is not used.inode->i_flctx->flc_lockThe 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_filesThe function clears all files opened by this process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.