linux - vfs_rename(...) 所需的锁

标签 linux file linux-kernel kernel kernel-module

我正在做内核编程。 在内核模块中使用 vfs_rename(...) 函数之前,我想知道要持有哪个 mutex_locks()。

vfs_rename(...) 的原型(prototype)

int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 结构 inode *new_dir, 结构 dentry *new_dentry,)

谢谢

最佳答案

vfs_rename 有评论:

4089 /**
4090  * vfs_rename - rename a filesystem object
4091  * @old_dir:    parent of source
4092  * @old_dentry: source
4093  * @new_dir:    parent of destination
4094  * @new_dentry: destination
4095  * @delegated_inode: returns an inode needing a delegation break
4096  *
4097  * The caller must hold multiple mutexes--see lock_rename()).
4098  *
4099  * If vfs_rename discovers a delegation in need of breaking at either
4100  * the source or destination, it will return -EWOULDBLOCK and return a
4101  * reference to the inode in delegated_inode.  The caller should then
4102  * break the delegation and retry.  Because breaking a delegation may
4103  * take a long time, the caller should drop all locks before doing
4104  * so.
4105  *
4106  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4107  * be appropriate for callers that expect the underlying filesystem not
4108  * to be NFS exported.
4109  */

所以,它把我们带到 lock_rename 函数:

2440 /*
2441  * p1 and p2 should be directories on the same fs.
2442  */
2443 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2444 {
2445         struct dentry *p;
2446 
2447         if (p1 == p2) {
2448                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2449                 return NULL;
2450         }
2451 
2452         mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2453 
2454         p = d_ancestor(p2, p1);
2455         if (p) {
2456                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2457                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2458                 return p;
2459         }
2460 
2461         p = d_ancestor(p1, p2);
2462         if (p) {
2463                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2464                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2465                 return p;
2466         }
2467 
2468         mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2469         mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2470         return NULL;
2471 }

关于linux - vfs_rename(...) 所需的锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22186006/

相关文章:

linux - 地址 sanitizer : No matching source file lines in the report after separating debuginfo

ruby-on-rails - Rails 文件上传错误 "undefined method ` original_filename' for nil :NilClass"

c - 在c中使用pread()读取和使用pwrite()写入

c - 用户空间程序中的 "volatile"是否表示存在错误?

ruby-on-rails - 无法安装旧版本的 gem "rmagick"

java - 从 Java 调用 Haskell,中间使用 C

linux - 在 SUSE 上的任何包含文件夹下都找不到 zlib.h,但存在 libz.so.1

c - OpenSSL文件加密麻烦

php - 如何在 ubuntu 服务器中创建 .sql 文件?

c++ - iptables 的替代品?