Linux vfs_重命名(…)所需的锁
我在做内核编程。 在内核模块中使用vfs_rename(…)函数之前,我想知道要保存哪些互斥锁() vfs_重命名的原型(…) int vfs_rename(结构inode*old_dir,结构dentry*old_dentry, 结构索引节点*新建目录,结构目录*新建目录,) 谢谢有评论:Linux vfs_重命名(…)所需的锁,linux,file,linux-kernel,kernel,kernel-module,Linux,File,Linux Kernel,Kernel,Kernel Module,我在做内核编程。 在内核模块中使用vfs_rename(…)函数之前,我想知道要保存哪些互斥锁() vfs_重命名的原型(…) int vfs_rename(结构inode*old_dir,结构dentry*old_dentry, 结构索引节点*新建目录,结构目录*新建目录,) 谢谢有评论: 4089 /** 4090 * vfs_rename - rename a filesystem object 4091 * @old_dir: parent of source 4092 *
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 */
因此,它让我们发挥作用:
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 }
“应用哪些锁”是什么意思?你到底在这里做什么,在代码中做一些修改和添加?我正在重命名一个文件,为此我想我需要对一些文件进行互斥,对这些文件执行重命名,但我不确定哪些文件将被授予互斥锁。你不能
互斥锁/锁定文件。锁定用于保护关键部分
,如果两个或多个线程同时访问该部分,可能会导致争用状态
。我会这样做:例如,如果要更改的文件名为file.c
,请对其进行备份,称之为file.backup
。更改文件.c
。就这样,您不必在这里进行任何其他更改。