Skip to contentSkip to conversation

fs: add SEEK_HOLE and SEEK_DATA flags

PATCH Superseded Josef Bacik 4 participants 15 messages May 4, 2011 – May 5, 2011

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 fs: add SEEK_HOLE and SEEK_DATA flags

This just gets us ready to support the SEEK_HOLE and SEEK_DATA flags. Turns out using fiemap in things like cp cause more problems than it solves, so lets try and give userspace an interface that doesn't suck. So we have

-SEEK_HOLE: this moves the file pos to the nearest hole in the file from the given position. If the given position is a hole then pos won't move. A "hole" is defined by whatever the fs feels like defining it to be. In simple things like ext2/3 it will simplly mean an unallocated space in the file. For more complex things where you have preallocated space then that is left up to the filesystem. Since preallocated space is supposed to return all 0's it is perfectly legitimate to have SEEK_HOLE dump you out at the start of a preallocated extent, but then again if this is not something you can do and be sure the extent isn't in the middle of being converted to a real extent then it is also perfectly legitimate to skip preallocated extents and only park f_pos at a truly unallocated section.

-SEEK_DATA: this is obviously a little more self-explanatory. Again the only ambiguity comes in with preallocated extents. If you have an fs that can't reliably tell that the preallocated extent is in the process of turning into a real extent, it is correct for SEEK_DATA to park you at a preallocated extent.

In the generic case we will just assume the entire file is data and there is a virtual hole at i_size, so SEEK_DATA will return -ENXIO unless you provide an offset of 0 and the file size is larger than 0, and SEEK_HOLE will put you at i_size unless pos is i_size or larger, and i_size is larger than 0.

Thanks,

---
v1->v2: Make the generic case assume that the entire file is data and there is a virtual hole at the end of the file.

 fs/read_write.c    |   22 ++++++++++++++++++++++
 include/linux/fs.h |    4 +++-
 2 files changed, 25 insertions(+), 1 deletions(-)
fs/read_write.c +22 -0
index 5520f8a..6ee63a4 100644
@@ -64,6 +64,28 @@ generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
 			return file->f_pos;
 		offset += file->f_pos;
 		break;
+	case SEEK_DATA:
+		/*
+		 * In the generic case the entire file is data, so data only
+		 * starts at position 0 provided the file has an i_size,
+		 * otherwise it's an empty file and will always be ENXIO.
+		 */
+		if (offset != 0 || i_size_read(inode)) {
+			mutex_unlock(&inode->i_mutex);
+			return -ENXIO;
+		}
+		break;
+	case SEEK_HOLE:
+		/*
+		 * There is a virtual hole at the end of the file, so as long as
+		 * offset isn't i_size or larger, return i_size.
+		 */
+		if (offset >= i_size_read(inode)) {
+			mutex_unlock(&inode->i_mutex);
+			return -ENXIO;
+		}
+		offset = i_size_read(inode);
+		break;
 	}
 
 	if (offset < 0 && !unsigned_offsets(file))
include/linux/fs.h +3 -1
index dbd860a..1b72e0c 100644
@@ -31,7 +31,9 @@
 #define SEEK_SET	0	/* seek relative to beginning of file */
 #define SEEK_CUR	1	/* seek relative to current file position */
 #define SEEK_END	2	/* seek relative to end of file */
-#define SEEK_MAX	SEEK_END
+#define SEEK_HOLE	3	/* seek to the closest hole */
+#define SEEK_DATA	4	/* seek to the closest data */
+#define SEEK_MAX	SEEK_DATA
 
 struct fstrim_range {
 	__u64 start;
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH v2 2/2 Btrfs: implement our own ->llseek

In order to handle SEEK_HOLE/SEEK_DATA we need to implement our own llseek. Basically for the normal SEEK_*'s we will just defer to the generic helper, and for SEEK_HOLE/SEEK_DATA we will use our fiemap helper to figure out the nearest hole or data. Currently this helper doesn't check for delalloc bytes for prealloc space, so for now treat prealloc as data until that is fixed. Thanks,

---
v1->v2: fixed some weirdness with finding the next hole when we had delalloc ranges.

 fs/btrfs/ctree.h |    3 +
 fs/btrfs/file.c  |  128 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 130 insertions(+), 1 deletions(-)
fs/btrfs/ctree.h +3 -0
index d5f043e..d2991c8 100644
@@ -2474,6 +2474,9 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start,
 			     u64 end, struct list_head *list);
 /* inode.c */
+struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
+					   size_t pg_offset, u64 start, u64 len,
+					   int create);
 
 /* RHEL and EL kernels have a patch that renames PG_checked to FsMisc */
 #if defined(ClearPageFsMisc) && !defined(ClearPageChecked)
fs/btrfs/file.c +127 -1

135 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#2 lore raw
In reply to Josef Bacik
Quoted 2 lines · Josef Bacik

-SEEK_HOLE: this moves the file pos to the nearest hole in the file from the given position.

Nearest, or next? Solaris defines it as "next", for a good reason - otherwise you can get stuck in a case where the "nearest" hole is back towards the start of the file - and "seek data" will bounce back to the next byte at the other end of the hole.

Consider a file with this layout:

< 40K of data> A < 32K hole> B < 32K data> C < 8K hole> D <32K data> E ....

If you're in the range between "8K-1 before C" and "8K-1 after D", there's no application of seeks to "nearest" data/hole that doesn't leave you oscillating between C and D, and unable to reach B or E. If youre at C, "nearest hole" is where you are, and "nearest data" is at D, not B. Similarly for D - nearest data is C, not E.

However, this is easily dealt with if you define it as "next", as then it is simple to discover exactly where A/B/C/D/E are.

#3 lore raw
Quoted 10 lines · On 05/04/2011 03:04 PM, [email protected] wrote:
Quoted 2 lines · Josef Bacik

-SEEK_HOLE: this moves the file pos to the nearest hole in the file from the given position.

Nearest, or next? Solaris defines it as "next", for a good reason - otherwise you can get stuck in a case where the "nearest" hole is back towards the start of the file - and "seek data" will bounce back to the next byte at the other end of the hole.

Yeah sorry the log says "nearest" but the code says "next", if you look at it thats how it works. Thanks,

Josef

#4 lore raw
Quoted 2 lines · Josef Bacik

Yeah sorry the log says "nearest" but the code says "next", if you look at it thats how it works. Thanks,

Oh good - the changelog is usually easier to fix than the code is. :)

Probably want to fix the changelog before it gets committed, as there's a fair chance that text will end up being used as the basis for a manpage or other documentation.

#5 lore raw
Quoted 11 lines · On 05/04/2011 03:20 PM, [email protected] wrote:
Quoted 2 lines · Josef Bacik

Yeah sorry the log says "nearest" but the code says "next", if you look at it thats how it works. Thanks,

Oh good - the changelog is usually easier to fix than the code is. :)

Probably want to fix the changelog before it gets committed, as there's a fair chance that text will end up being used as the basis for a manpage or other documentation.

Yeah agreed I meant to change it this time around but forgot, I will make the log all nice and pretty next time around, as I doubt this will be the last iteration of these patches ;). Thanks,

Josef

#8 lore raw
In reply to Josef Bacik

include/linux/fs.h line 35

Quoted 2 lines · Josef Bacik
include/linux/fs.h +2 -0
+#define SEEK_HOLE	3	/* seek to the closest hole */
+#define SEEK_DATA	4	/* seek to the closest data */

Comments here need nearest/next fixing as well - otherwise the ext[34] crew may actually implement the commented semantics. ;)

Other than that, patch 1/2 looks OK to me (not that there's much code to review), and 2/2 *seems* sane and implement the "next" semantics, though I only examined the while/if structure and am assuming the btrfs innards are done correctly. In particular, that 'while (1)' looks like it can be painful for a sufficiently large and fragmented file (think a gigabyte file in 4K chunks, producing a million extents), but I'll let a btrfs expert analyse that performance issue ;)

#9 lore raw
Quoted 8 lines · On 05/04/2011 03:31 PM, [email protected] wrote:

include/linux/fs.h line 35

Quoted 2 lines · Josef Bacik
include/linux/fs.h +2 -0
+#define SEEK_HOLE	3	/* seek to the closest hole */
+#define SEEK_DATA	4	/* seek to the closest data */

Comments here need nearest/next fixing as well - otherwise the ext[34] crew may actually implement the commented semantics. ;)

Balls, thanks I'll fix that.

Quoted 8 lines

Other than that, patch 1/2 looks OK to me (not that there's much code to review), and 2/2 *seems* sane and implement the "next" semantics, though I only examined the while/if structure and am assuming the btrfs innards are done correctly. In particular, that 'while (1)' looks like it can be painful for a sufficiently large and fragmented file (think a gigabyte file in 4K chunks, producing a million extents), but I'll let a btrfs expert analyse that performance issue ;)

Heh well we do while (1) in btrfs _everywhere_, so this isn't anything new, tho I should probably throw a cond_resched() in there. Thanks,

Josef

#6 lore raw
In reply to Josef Bacik
Quoted 14 lines · Josef Bacik
Quoted 6 lines · On 05/04/2011 03:04 PM, [email protected] wrote:
Quoted 3 lines · Josef Bacik

-SEEK_HOLE: this moves the file pos to the nearest hole in the file from the
given position.

Quoted 6 lines

Nearest, or next? Solaris defines it as "next", for a good reason - otherwise
you can get stuck in a case where the "nearest" hole is back towards the start of the file - and "seek data" will bounce back to the next byte at the other end of the hole.

Quoted 2 lines

Yeah sorry the log says "nearest" but the code says "next", if you look at it thats how it works. Thanks,

The comments in fs.h say "closest". You may want to change them to "next" as well.

Thanks,
Shaggy

#7 lore raw
Quoted 2 lines · Dave Kleikamp

The comments in fs.h say "closest". You may want to change them to "next" as well.

Sorry. Missed some of the replies before I responded. Already addressed.

Shaggy

#10 lore raw
In reply to Josef Bacik

Il 04/05/2011 19:58, Josef Bacik ha scritto:

fs/read_write.c generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin) · line 88

Quoted 6 lines
fs/read_write.c +6 -0
+		if (offset>= i_size_read(inode)) {
+			mutex_unlock(&inode->i_mutex);
+			return -ENXIO;
+		}
+		offset = i_size_read(inode);
+		break;

Here maybe it's possible to use offset bigger than i_size, because i_size_read is "atomic" but something can happen between two calls, isn't it?

Marco

#12 lore raw
In reply to Josef Bacik

Il 05/05/2011 21:01, Josef Bacik ha scritto:

Quoted 14 lines · Marco Stornelli
Quoted 8 lines

Il 04/05/2011 19:58, Josef Bacik ha scritto:

Quoted 6 lines

+ if (offset>= i_size_read(inode)) {
+ mutex_unlock(&inode->i_mutex);
+ return -ENXIO;
+ }
+ offset = i_size_read(inode);
+ break;

Quoted 4 lines

Here maybe it's possible to use offset bigger than i_size, because i_size_read is "atomic" but something can happen between two calls, isn't it?

Quoted 5 lines

We're holding the i_mutex so we are safe, i_size_read is used just for consistency sake. Thanks,

Josef

Oh, I'm sorry, I misread the patch, ok. Maybe we can use i_size at this point without i_size_read.

Marco

#11 lore raw
In reply to Marco Stornelli
Quoted 8 lines · Marco Stornelli

Il 04/05/2011 19:58, Josef Bacik ha scritto:

Quoted 6 lines

+ if (offset>= i_size_read(inode)) {
+ mutex_unlock(&inode->i_mutex);
+ return -ENXIO;
+ }
+ offset = i_size_read(inode);
+ break;

Quoted 4 lines

Here maybe it's possible to use offset bigger than i_size, because i_size_read is "atomic" but something can happen between two calls, isn't it?

We're holding the i_mutex so we are safe, i_size_read is used just for consistency sake. Thanks,

Josef

#13 lore raw
In reply to Josef Bacik

Il 04/05/2011 19:58, Josef Bacik ha scritto:

fs/read_write.c generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin) · line 88

Quoted 6 lines
fs/read_write.c +6 -0
+		if (offset>= i_size_read(inode)) {
+			mutex_unlock(&inode->i_mutex);
+			return -ENXIO;
+		}
+		offset = i_size_read(inode);
+		break;

I can add that generic_file_llseek_unlocked means *unlocked* so you shouldn't unlock any mutex but only return a value. The current version, in case of SEEK_END uses directly i_size indeed, so maybe I'm missing something.

Marco

#14 lore raw
Quoted 8 lines · Marco Stornelli

Il 04/05/2011 19:58, Josef Bacik ha scritto:

Quoted 6 lines

+ if (offset>= i_size_read(inode)) {
+ mutex_unlock(&inode->i_mutex);
+ return -ENXIO;
+ }
+ offset = i_size_read(inode);
+ break;

Quoted 4 lines

I can add that generic_file_llseek_unlocked means *unlocked* so you shouldn't unlock any mutex but only return a value. The current version, in case of SEEK_END uses directly i_size indeed, so maybe I'm missing something.

Yeah this was a copy+paste mistake, ext4 has it's own llseek that I modified to run my tests against and then I just copied and pasted it over to the generic things. I've fixed this earlier, I'll be sending a refreshed set out soon. Thanks,

Josef