vfs_rename(...)所需的锁

时间:2014-03-05 00:19:04

标签: linux file linux-kernel kernel kernel-module

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

vfs_rename的原型(...)

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

由于

1 个答案:

答案 0 :(得分:1)

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 }