Skip to contentSkip to conversation

fat: Editions to support fat_fallocate()

PATCH In review Steven Cavanagh 3 participants 5 messages Dec 22, 2007 – Jan 13, 2008 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fat: Editions to support fat_fallocate()

From: Steven Cavanagh <[email protected]>

Added support for fallocate for a msdos fat driver. This allows preallocation of clusters to an inode before writes to reduce file fragmentation

---

 fs/fat/file.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
fs/fat/file.c +45 -0
index 69a83b5..f753c6a 100644
@@ -15,6 +15,7 @@ #include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/backing-dev.h>
 #include <linux/blkdev.h>
+#include <linux/falloc.h>
 
 int fat_generic_ioctl(struct inode *inode, struct file *filp,
 		      unsigned int cmd, unsigned long arg)
@@ -312,8 +313,52 @@ int fat_getattr(struct vfsmount *mnt, st
 }
 EXPORT_SYMBOL_GPL(fat_getattr);
 
+/*
+ * preallocate space for a file. This implements fat fallocate inode
+ * operation, which gets called from sys_fallocate system call. User
+ * space requests len bytes at offset.
+ */
+long fat_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
+{
+	int ret = 0;
+	loff_t filesize = inode->i_size;
+
+	/* preallocation to directories is currently not supported */
+	if (S_ISDIR(inode->i_mode)) {
+		printk(KERN_ERR
+		"fat_fallocate(): Directory prealloc not supported\n");
+		return -ENODEV;
+	}
+
+	if ((offset + len) <= MSDOS_I(inode)->mmu_private) {
+		printk(KERN_INFO
+			"fat_fallocate():Blocks already allocated\n");
+			return 0;
+	}
+
+	if ((offset + len) > MSDOS_I(inode)->mmu_private) {
+
+		mutex_lock(&inode->i_mutex);
+		ret = fat_cont_expand(inode, (offset + len));
+		if (ret) {
+			printk(KERN_ERR
+				"fat_fallocate():fat_cont_expand() error\n");
+			mutex_unlock(&inode->i_mutex);
+			return ret;
+		}
+		mutex_unlock(&inode->i_mutex);
+	}
+	if (mode & FALLOC_FL_KEEP_SIZE) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, filesize);
+		mutex_unlock(&inode->i_mutex);
+	}
+	return ret;
+}
+
 const struct inode_operations fat_file_inode_operations = {
 	.truncate	= fat_truncate,
 	.setattr	= fat_notify_change,
 	.getattr	= fat_getattr,
+	.fallocate	= fat_fallocate,
 };
#1 lore raw

Thanks Steve, comments below.

Quoted 5 lines · Steven Cavanagh

From: Steven Cavanagh <[email protected]>

Added support for fallocate for a msdos fat driver. This allows preallocation of clusters to an inode before writes to reduce file fragmentation

Not technically true. This doesn't make any guarantees about fragmentation (even if in general it works pretty well). To really reduce fragmentation, then cluster allocation needs to be made smarter so it goes looking for contiguous clusters when allocating, but that's a task for a separate patch. Please adjust your description.

Also, please briefly describe the testing that you've performed.

More comments below.

Quoted 26 lines

---

 fs/fat/file.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
fs/fat/file.c +5 -0
index 69a83b5..f753c6a 100644
@@ -15,6 +15,7 @@ #include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/backing-dev.h>
 #include <linux/blkdev.h>
+#include <linux/falloc.h>
 
 int fat_generic_ioctl(struct inode *inode, struct file *filp,
                      unsigned int cmd, unsigned long arg)
@@ -312,8 +313,52 @@ int fat_getattr(struct vfsmount *mnt, st
 }
 EXPORT_SYMBOL_GPL(fat_getattr);
 
+/*
+ * preallocate space for a file. This implements fat fallocate inode
+ * operation, which gets called from sys_fallocate system call. User
+ * space requests len bytes at offset.

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 329

Quoted 10 lines
fs/fat/file.c +10 -0
+ */
+long fat_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
+{
+       int ret = 0;
+       loff_t filesize = inode->i_size;
+
+       /* preallocation to directories is currently not supported */
+       if (S_ISDIR(inode->i_mode)) {
+               printk(KERN_ERR
+               "fat_fallocate(): Directory prealloc not supported\n");

This printk is probably not needed, or at least make it a pr_debug()

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 335

Quoted 6 lines
fs/fat/file.c +6 -0
+               return -ENODEV;
+       }
+
+       if ((offset + len) <= MSDOS_I(inode)->mmu_private) {
+               printk(KERN_INFO
+                       "fat_fallocate():Blocks already allocated\n");

Drop the printk() here. It will cause a write to the system log everytime userspace does an unnecessary fat_fallocate(). That becomes a performance hit which I don't think we want.

Quoted 10 lines
fs/fat/file.c +10 -0
+                       return 0;
+       }
+
+       if ((offset + len) > MSDOS_I(inode)->mmu_private) {
+
+               mutex_lock(&inode->i_mutex);
+               ret = fat_cont_expand(inode, (offset + len));
+               if (ret) {
+                       printk(KERN_ERR
+                               "fat_fallocate():fat_cont_expand() error\n");

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 355

Quoted 10 lines
fs/fat/file.c +10 -0
+                       mutex_unlock(&inode->i_mutex);
+                       return ret;
+               }
+               mutex_unlock(&inode->i_mutex);
+       }
+       if (mode & FALLOC_FL_KEEP_SIZE) {
+               mutex_lock(&inode->i_mutex);
+               i_size_write(inode, filesize);
+               mutex_unlock(&inode->i_mutex);
+       }

Race condition. The file is increased in size and then returned to the original size if FALLOC_FL_KEEP_SIZE is set, but the lock is dropped then reobtained between increasing the size and restoring to the original. Another file operation can occur between the two operations. Badness!

However, digging further, when FALLOC_FL_KEEP_SIZE is set, I don't think fat_cont_expand() has the behaviour that we want to implement. When that flag is set, I think we simply want to add clusters associated with the file to the FAT. We don't want to clear them or map them into the page cache yet (that should be done when the filesize is increased for real).

I believe a call to fat_allocate_clusters() is all that is needed in this case. Hirofumi, please correct me if I'm wrong.

Cheers,
g.

Signature · 4 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
[email protected]
(403) 399-0195
maintainer
#2 lore raw
Quoted 6 lines · Grant Likely

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 321

Quoted 6 lines
fs/fat/file.c +6 -0
+/*
+ * preallocate space for a file. This implements fat fallocate inode
+ * operation, which gets called from sys_fallocate system call. User
+ * space requests len bytes at offset.
+ */
+long fat_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)

This should be "static".

Quoted 10 lines

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 329

Quoted 8 lines
fs/fat/file.c +8 -0
+{
+       int ret = 0;
+       loff_t filesize = inode->i_size;
+
+       /* preallocation to directories is currently not supported */
+       if (S_ISDIR(inode->i_mode)) {
+               printk(KERN_ERR
+               "fat_fallocate(): Directory prealloc not supported\n");

This printk is probably not needed, or at least make it a pr_debug()

Yes. Please remove printk().

Quoted 10 lines

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 335

Quoted 6 lines
fs/fat/file.c +6 -0
+               return -ENODEV;
+       }
+
+       if ((offset + len) <= MSDOS_I(inode)->mmu_private) {
+               printk(KERN_INFO
+                       "fat_fallocate():Blocks already allocated\n");

Drop the printk() here. It will cause a write to the system log everytime userspace does an unnecessary fat_fallocate(). That becomes a performance hit which I don't think we want.

Yes. And it should be ->i_size, not ->mmu_private.

Quoted 1 line
Quoted 1 line

+ if ((offset + len) > MSDOS_I(inode)->mmu_private) {

Ditto. This should also be ->i_size. Furthermore, this check should be under ->i_mutex, otherwise others may expand ->i_size before this already.

Quoted 29 lines
Quoted 5 lines
fs/fat/file.c +5 -0
+               mutex_lock(&inode->i_mutex);
+               ret = fat_cont_expand(inode, (offset + len));
+               if (ret) {
+                       printk(KERN_ERR
+                               "fat_fallocate():fat_cont_expand() error\n");

fs/fat/file.c int fat_getattr(struct vfsmount *mnt, st · line 355

Quoted 10 lines
fs/fat/file.c +10 -0
+                       mutex_unlock(&inode->i_mutex);
+                       return ret;
+               }
+               mutex_unlock(&inode->i_mutex);
+       }
+       if (mode & FALLOC_FL_KEEP_SIZE) {
+               mutex_lock(&inode->i_mutex);
+               i_size_write(inode, filesize);
+               mutex_unlock(&inode->i_mutex);
+       }

Race condition. The file is increased in size and then returned to the original size if FALLOC_FL_KEEP_SIZE is set, but the lock is dropped then reobtained between increasing the size and restoring to the original. Another file operation can occur between the two operations. Badness!

However, digging further, when FALLOC_FL_KEEP_SIZE is set, I don't think fat_cont_expand() has the behaviour that we want to implement. When that flag is set, I think we simply want to add clusters associated with the file to the FAT. We don't want to clear them or map them into the page cache yet (that should be done when the filesize is increased for real).

Quoted 2 lines

I believe a call to fat_allocate_clusters() is all that is needed in this case. Hirofumi, please correct me if I'm wrong.

Right. And we need to care the limitation on FAT specification (compatibility).

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#3 lore raw
Quoted 9 lines · OGAWA Hirofumi
Quoted 8 lines · Grant Likely

However, digging further, when FALLOC_FL_KEEP_SIZE is set, I don't think fat_cont_expand() has the behaviour that we want to implement. When that flag is set, I think we simply want to add clusters associated with the file to the FAT. We don't want to clear them or map them into the page cache yet (that should be done when the filesize is increased for real).

Quoted 4 lines
Quoted 2 lines

I believe a call to fat_allocate_clusters() is all that is needed in this case. Hirofumi, please correct me if I'm wrong.

Right. And we need to care the limitation on FAT specification (compatibility).

I not sure I fully understand what you mean. Can you please elaborate? Are you referring to whether on not it will break other FAT implementations if a file has more clusters allocated than it needs? If so, how do we decide whether or not it is acceptable?

Thanks,
g.

Signature · 4 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
[email protected]
(403) 399-0195
maintainer
#4 lore raw
Quoted 13 lines · Grant Likely
Quoted 12 lines · OGAWA Hirofumi
Quoted 10 lines · Grant Likely

However, digging further, when FALLOC_FL_KEEP_SIZE is set, I don't think fat_cont_expand() has the behaviour that we want to implement. When that flag is set, I think we simply want to add clusters associated with the file to the FAT. We don't want to clear them or map them into the page cache yet (that should be done when the filesize is increased for real).

I believe a call to fat_allocate_clusters() is all that is needed in this case. Hirofumi, please correct me if I'm wrong.

Quoted 6 lines
Quoted 1 line

Right. And we need to care the limitation on FAT specification (compatibility).

I not sure I fully understand what you mean. Can you please elaborate? Are you referring to whether on not it will break other FAT implementations if a file has more clusters allocated than it needs? If so, how do we decide whether or not it is acceptable?

[Sorry for long delay. I was on vacation.]

Probably we need to check how Widnows behave in some situations.

E.g. if we store the longer cluster-chain than i_size (in the case of FALLOC_FL_KEEP_SIZE), the driver will be seen like corrupted files. Because we doesn't know the file is whether file was "fallocate" or not after reboot. At least, I think current linux implementation will detect it as corrupted file (filesystem).

So we have to handle somehow those situations. Also I think we'll need to more investigate problem like this.

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>