linux 文件删除过程浅析

1.Linux文件删除原理

Linux是通过link的数量控制文件删除的,只有当文件不存在任何链接时,该文件才会被删除,一般每个文件有两个link计数器: i_count 和 i_nlink,从VFS inode结构体中可以找到:

struct inode {
struct hlist_node i_hash; /* hash链表的指针 */
struct list_head i_list; /* backing dev IO list */
struct list_head i_sb_list; /* 超级块的inode链表 */
struct list_head i_dentry; /* 引用inode的目录项对象链表头 */
unsigned long i_ino; /* 索引节点号 */
atomic_t i_count; /* 引用计数器 */
unsigned int i_nlink; /* 硬链接数目 */
...

i_count: 引用计数器,文件被一进程引用,i_count数增加 ,可以认为是当前文件使用者的数量; 
i_nlink: 硬链接数目(可以理解为磁盘的引用计数器),创建硬链接对应的 i_nlink 就会增加

对于rm命令来说,实际就是减少磁盘的引用计数 i_nlink 。如果当文件被另外一个进程调用时,用户执行rm命令删除文件,再去cat文件内容时就会找不到文件,但是调用该删除文件的那个进程却仍然可以对文件进行正常的操作。这就是因为 i_nlink 为 0 ,但 i_count 并不为 0 。只有当 i_nlink 和 i_count 均为 0 时,文件才会被删除(这里的删除是指将文件名到 inode 的链接删除了,但文件在磁盘上的block数据块并未被删除)。

首先使用strace追踪rm命令,看看rm具体使用了哪些系统调用:

[root@ty ~]# strace rm test
execve("/bin/rm", ["rm", "test"], [/* 19 vars */]) = 0
brk(0) = 0x175c000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1365a2f000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=62329, ...}) = 0
mmap(NULL, 62329, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1365a1f000
close(3) = 0
open("/lib64/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360\355\201\2160\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1922152, ...}) = 0
... ...
brk(0) = 0x175c000
brk(0x177d000) = 0x177d000
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=99158576, ...}) = 0
mmap(NULL, 99158576, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f135fb8b000
close(3) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
newfstatat(AT_FDCWD, "test", {st_mode=S_IFREG|0644, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
geteuid() = 0
unlinkat(AT_FDCWD, "test", 0) = 0
close(0) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?

由上发现最终调用unlinkat系统调用删除文件。 

2.系统调用unlinkat

unistd.h、unistd_64.h、unistd_32.h中定义系统调用号

#define __NR_link 9
#define __NR_unlink 10
...
#define __NR_unlinkat 456

两个函数定义在 Namei.c 文件中,如下:

SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
{
if ((flag & ~AT_REMOVEDIR) != 0)
return -EINVAL;
 
if (flag & AT_REMOVEDIR)
return do_rmdir(dfd, pathname);
 
return do_unlinkat(dfd, pathname);
}
 
SYSCALL_DEFINE1(unlink, const char __user *, pathname)
{
return do_unlinkat(AT_FDCWD, pathname);
}

在unlinkat函数定义中会判断flag标志,如果有设置AT_REMOVEDIR标志,说明删除的是目录则调用do_rmdir函数,否则调用do_unlinkat函数删除一个文件。

3.do_unlinkat函数

/*
* 确保文件的实际截断发生在其目录的i_mutex之外,如果有很多写操作发生截断需要很长时间
* 在等待I/O发生时,我们不想阻止目录的访问
*/
static long do_unlinkat(int dfd, const char __user *pathname)
{
int error;
char *name;
struct dentry *dentry;
struct nameidata nd;
struct inode *inode = NULL;
 
error = user_path_parent(dfd, pathname, &nd, &name); /* 获取父目录信息,成功保存在nd结构体中,并返回0*/
if (error)
return error;
 
error = -EISDIR;
if (nd.last_type != LAST_NORM) /*路径名中最后一个分量类型不是一个普通文件名*/
goto exit1;
 
nd.flags &= ~LOOKUP_PARENT; /*清除LOOKUP_PARENT标志*/
 
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
dentry = lookup_hash(&nd); /*返回要删除文件的dentry结构*/
error = PTR_ERR(dentry);
if (!IS_ERR(dentry)) {
/* Why not before? Because we want correct error value */
if (nd.last.name[nd.last.len]) /*last域代表路径名中的最后一个分量,在LOOKUP_PARENT标志设置时使用,因为上边已经做了清除标志操作,此处这样做可以判断是否清除成功,否的话,返回正确的错误码*/
goto slashes;
inode = dentry->d_inode;
if (inode)
atomic_inc(&inode->i_count); /*i_count引用计数器(文件当前使用者的数量)原子的加1*/
error = mnt_want_write(nd.path.mnt); /*判断mnt对象是否有可写权限,是返回0*/
if (error)
goto exit2;
error = security_path_unlink(&nd.path, dentry); /*检查权限允许删除一个到文件的硬链接,调用path_unlink是一个函数指针(如果有定义)*/
if (error)
goto exit3;
error = vfs_unlink(nd.path.dentry->d_inode, dentry);
exit3:
mnt_drop_write(nd.path.mnt);
exit2:
dput(dentry); /*释放dentry结构体,并释放资源*/
}
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
if (inode)
iput(inode); /* truncate the inode here */
exit1:
path_put(&nd.path); /*释放引用的一个path结构体*/
putname(name); /*释放name*/
return error;
 
slashes:
error = !dentry->d_inode ? -ENOENT :
S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
goto exit2;
}

主要分为以下几个步骤:

(1) error = user_path_parent(dfd, pathname, &nd, &name);
(2) dentry = lookup_hash(&nd);
(3) atomic_inc(&inode->i_count);
(4) vfs_unlink(nd.dentry->d_inode, dentry);
(5) dput(dentry);
(6) iput(inode); /* truncate the inode here */

该函数中首先使用user_path_parent()函数获取要删除文件的父目录信息,成功会返回0,并且将父目录信息保存在nameidata类型的结构体nd中,然后通过lookup_hash函数在当前目录中找寻要删除文件的目录项信息,if语句判断获取的dentry是否有错误,有错误就使用path_put()和putname()释放前几步获取到的数据,然后返回,结束;若没有错误,得到该文件的inode,增加其i_count进程引用计数,判断当前挂载点是否有可写权限,有可写权限就调用vfs_unlink函数执行文件dentry的删除,释放dentry结构体,并释放资源,最后调用iput函数截断inode。

3.1 vfs_unlink函数

int vfs_unlink(struct inode *dir, struct dentry *dentry) /*删除dentry,即就是硬链接*/
{
int error = may_delete(dir, dentry, 0); /*权限检查,是否有删除权限*/
 
if (error)
return error;
 
if (!dir->i_op->unlink)
return -EPERM;
 
vfs_dq_init(dir);
 
mutex_lock(&dentry->d_inode->i_mutex);
if (d_mountpoint(dentry))
error = -EBUSY;
else {
error = security_inode_unlink(dir, dentry);
if (!error)
error = dir->i_op->unlink(dir, dentry); /*调用具体文件系统的unlink函数,如ext3_unlink()*/
}
mutex_unlock(&dentry->d_inode->i_mutex);
 
/* We don't d_delete() NFS sillyrenamed files--they still exist. */
if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
fsnotify_link_count(dentry->d_inode);
d_delete(dentry); /* 若dentry的使用计数为 1,说明没有其他进程引用该dentry,就尝试把该dentry的inode删除 */
}
return error;
}

在vfs_unlink()函数中会调用具体文件系统的删除函数。首先检查要删除文件所在目录的权限,判断是否有删除权限,有删除权限,然后判断具体文件系统是否还有定义自己的unlink函数,没有就退出vfs_unlink函数,否则调用具体文件系统unlink函数(如ext3_unlink()函数)。

3.2 ext3_unlink函数

static int ext3_unlink(struct inode * dir, struct dentry *dentry)
{
int retval;
struct inode * inode;
struct buffer_head * bh;
struct ext3_dir_entry_2 * de;
handle_t *handle;
......
bh = ext3_find_entry(dir, &dentry->d_name, &de); /*返回该inode对应的buffer_head,并填充其ext3_dir_entry_2结构体*/
if (!bh)
goto end_unlink;
 
inode = dentry->d_inode;
 
retval = -EIO;
if (le32_to_cpu(de->inode) != inode->i_ino)
goto end_unlink;
......
retval = ext3_delete_entry(handle, dir, de, bh); /*从该文件父目录中删除该文件目录项*/
if (retval)
goto end_unlink;
dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
ext3_update_dx_flag(dir);
ext3_mark_inode_dirty(handle, dir);
 
/*drop_nlink()将i_nlink数减1 */
drop_nlink(inode);
/*
* 如果i_nlink为0,说明该inode再也没有任何dentry引用它了(即就是硬链接数位0)
* 则认为当前节点为孤儿节点(准备删除的inode),加入到orphan inode链表中*/
*/
if (!inode->i_nlink)
ext3_orphan_add(handle, inode);
inode->i_ctime = dir->i_ctime; /*更新修改索引节点的时间*/
ext3_mark_inode_dirty(handle, inode);
retval = 0;
 
end_unlink:
ext3_journal_stop(handle);
brelse (bh);
return retval;
}

该函数中最主要的操作是ext3_delete_entry函数,从该文件父目录中删除文件目录项,然后修改父目录的i_ctime(上次修改文件)和i_mtime(上次写文件)字段。然后将该文件inode 的硬链接计数减一后若为0,就将其添加到orphan inode(孤儿链表,后边介绍)链表中,更新该inode的修改时间。

3.3 dput函数(release a dentry)

void dput(struct dentry *dentry)
{
if (!dentry)
return;
 
repeat:
if (atomic_read(&dentry->d_count) == 1)
might_sleep(); /*提醒用户调用该函数的函数可能会sleep*/
 
/*
* 将d_count原子的减1,并同时对dcache_lock进行加锁,若d_count减1后仍非0,则直接返回return
* 这块就是判断d_count个数,如果此时d_count大于1,存在多个引用就不能释放该dentry,直接返回;
* 若为1,则可以删除dentry,继续后边的释放语句
**/
if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
return;
 
spin_lock(&dentry->d_lock); /*上一步对dentry->d_count递减,此处加锁,防止其他进程进行操作*/
if (atomic_read(&dentry->d_count)) { /**/
spin_unlock(&dentry->d_lock);
spin_unlock(&dcache_lock);
return;
}
 
/*
* AV: ->d_delete() is _NOT_ allowed to block now.
*
*/
/*
* 判断具体文件系统是否定义了d_op->d_delete接口函数,
* ext2、ext3没定义,NFS有定义该接口函数,但没什么实质内容,基本是直接返回;
* 若定义该接口函数则调用__d_drop()函数,把dentry从哈希链上移除,再调用dentry_iput函数尝试删除inode
*/
if (dentry->d_op && dentry->d_op->d_delete) {
if (dentry->d_op->d_delete(dentry))
goto unhash_it;
}
/* Unreachable? Get rid of it */
if (d_unhashed(dentry)) /*dentry从dcache hash链上移除了,表示该元数据对应的对象已经被删除,此时可以释放该元数据*/
goto kill_it;
if (list_empty(&dentry->d_lru)) { /*没有从dcache哈希链移除,表示该元数据对应的对象没有被删除,这时把dentry挂到LRU队列dentry_unused上*/
dentry->d_flags |= DCACHE_REFERENCED;
dentry_lru_add(dentry);
}
spin_unlock(&dentry->d_lock);
spin_unlock(&dcache_lock);
return;
 
unhash_it:
__d_drop(dentry);
kill_it:
/* if dentry was on the d_lru list delete it from there */
dentry_lru_del(dentry);
dentry = d_kill(dentry); /*删除该dentry,返回父目录项*/
if (dentry)
goto repeat;
}

dput函数的主要功能是释放一个dentry结构体,并且将该结构体的使用计数d_count的值减1操作,将该结构体从队列中删除,同时,释放该结构体的资源,无返回值。 
函数中出现repeat字段,每次释放一个dentry,都要获取其父目录项,然后又跳转到dput()开头,继续对父目录dentry进行释放操作;是因为:每次创建一个dentry结构,除了增加自身的使用计数外,还会增加其父目录dentry的使用计数,所以当释放了一个dentry后也需要递减其父目录dentry的使用计数。才能保证父目录为空时能够被释放。

4.iput函数(truncate the inode here)

void iput(struct inode *inode)
{
if (inode) {
BUG_ON(inode->i_state == I_CLEAR);
 
if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
iput_final(inode);
}
}

atomic_dec_and_lock宏先对i_count(进程使用计数)加锁后原子的减一,结果为0时,返回true,再进行调用iput_final函数进行删除操作,否则不进行任何操作。

4.1 iput_final函数

static inline void iput_final(struct inode *inode)
{
const struct super_operations *op = inode->i_sb->s_op;
void (*drop)(struct inode *) = generic_drop_inode;
 
if (op && op->drop_inode)
drop = op->drop_inode;
drop(inode);
}

该函数主要是调用generic_drop_inode()函数,其会判断inode->i_nlink的值,若为0,则该inode可以被删除,调用generic_delete_inode()实现。

void generic_drop_inode(struct inode *inode)
{
if (!inode->i_nlink) /*i_nlink硬链接数为 0,删除 */
generic_delete_inode(inode);
else /* 不为 0 ,不能删除*/
generic_forget_inode(inode);
}

在generic_delete_inode()函数中会判断是否定义具体文件系统的超级块操作函数delete_inode,若定义的就调用具体的inode删除函数(如ext3_delete_inode ),否则调用truncate_inode_pages和clear_inode函数(在具体文件系统的delete_inode函数中也必须调用这两个函数)。

4.2 ext3_delete_inode 函数

void ext3_delete_inode (struct inode * inode)
{
handle_t *handle;
truncate_inode_pages(&inode->i_data, 0);
 
if (is_bad_inode(inode))
goto no_delete;
 
handle = start_transaction(inode);
if (IS_ERR(handle)) {
/* 如果我们要跳过正常清理,我们仍然需要确保内核孤儿链表进行适当的清理。*/
ext3_orphan_del(NULL, inode);
goto no_delete;
}
 
if (IS_SYNC(inode))
handle->h_sync = 1;
inode->i_size = 0; /*将索引节点中记录的文件大小设置为0*/
if (inode->i_blocks)
ext3_truncate(inode); /*将索引节点中的文件占用块数清除*/
 
ext3_orphan_del(handle, inode);
EXT3_I(inode)->i_dtime = get_seconds(); /*修改该文件索引节点中的删除时间*/
 
if (ext3_mark_inode_dirty(handle, inode))
/* If that failed, just do the required in-core inode clear. */
clear_inode(inode);
else
ext3_free_inode(handle, inode);
ext3_journal_stop(handle);
return;
no_delete:
clear_inode(inode); /* We must guarantee clearing of inode... */
}

该函数会删除指定的inode,其中主要会调用如下函数(还没看完):

  • ext3_truncate 截断磁盘上的索引信息
  • ext3_orphan_del
  • ext3_free_inode 从内存中和磁盘上分别删除该inode

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章