what is the real necessary change

Time: 五月 16, 2013
Category: Kernel

看内核patchs的时候,经常会看到一些清理代码的patch,理由是unnecessary,比如这个:

[PATCH] vfs: no need to check about IS_IMMUTABLE in do_fallocate

From: Ashish Sangwan <a.sangwan <at> samsung.com>

In do_fallocate, first there is check for FMODE_WRITE and after that
there is second check for IS_IMMUTABLE.
A file cannot be opened in write mode if the corresponding inode is
immutable, hence the second check is not required.

Signed-off-by: Ashish Sangwan <a.sangwan <at> samsung.com>
Signed-off-by: Namjae Jeon <namjae.jeon <at> samsung.com>
---
 fs/open.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)

diff --git a/fs/open.c b/fs/open.c
index 8c74100..939e402 100644
--- a/fs/open.c
+++ b/fs/open.c
 <at>  <at>  -245,9 +245,6  <at>  <at>
 	int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
 	if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
 		return -EPERM;

-	if (IS_IMMUTABLE(inode))
-		return -EPERM;
-
 	/*
 	 * Revalidate the write permissions, in case security policy has
 	 * changed since the files were opened.

事实上看起来这处代码是多余的。不过我更偏向的想法是,基础函数或者通常一下比较通用的代码,要尽可能的具备语义自描述的特性,就是说,它不需要理解外界的环境是什么样的,只对输入做符合规格的检查。

比如这里的do_fallocate()函数,正常情况下,能以write模式打开的文件,它的inode肯定不是IS_IMMUTABLE,所以这个patch里把这个不必要的一步去掉了。

但是反过来考虑一下,如果将来某天,我们可能为了满足某种特别的需求,即使文件的inode是IS_IMMUTABLE的,也可以以write模式打开,但是实现这个功能代码的作者,未必会想到此处可能会引发BUG,而且这种地方还不少。

Leave a Comment