aboutsummaryrefslogtreecommitdiffstats
path: root/fs/read_write.c
diff options
context:
space:
mode:
authorJosef Bacik <josef@redhat.com>2011-07-18 13:21:35 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2011-07-20 20:47:56 -0400
commit982d816581eeeacfe5b2b7c6d47d13a157616eff (patch)
tree1f1cb5725646e9de13969c8894b26ab3d5e3102e /fs/read_write.c
parentb4d5b10fb2e3a4327838c07d8ebd9e350fcc133d (diff)
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. We need to match solaris here, and the definitions are *o* If /whence/ is SEEK_HOLE, the offset of the start of the next hole greater than or equal to the supplied offset is returned. The definition of a hole is provided near the end of the DESCRIPTION. *o* If /whence/ is SEEK_DATA, the file pointer is set to the start of the next non-hole file region greater than or equal to the supplied offset. So in the generic case the entire file is data and there is a virtual hole at the end. That means we will just return i_size for SEEK_HOLE and will return the same offset for SEEK_DATA. This is how Solaris does it so we have to do it the same way. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/read_write.c')
-rw-r--r--fs/read_write.c44
1 files changed, 41 insertions, 3 deletions
diff --git a/fs/read_write.c b/fs/read_write.c
index 5520f8ad5504..5907b49e4d7e 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -64,6 +64,23 @@ generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
64 return file->f_pos; 64 return file->f_pos;
65 offset += file->f_pos; 65 offset += file->f_pos;
66 break; 66 break;
67 case SEEK_DATA:
68 /*
69 * In the generic case the entire file is data, so as long as
70 * offset isn't at the end of the file then the offset is data.
71 */
72 if (offset >= inode->i_size)
73 return -ENXIO;
74 break;
75 case SEEK_HOLE:
76 /*
77 * There is a virtual hole at the end of the file, so as long as
78 * offset isn't i_size or larger, return i_size.
79 */
80 if (offset >= inode->i_size)
81 return -ENXIO;
82 offset = inode->i_size;
83 break;
67 } 84 }
68 85
69 if (offset < 0 && !unsigned_offsets(file)) 86 if (offset < 0 && !unsigned_offsets(file))
@@ -128,12 +145,13 @@ EXPORT_SYMBOL(no_llseek);
128 145
129loff_t default_llseek(struct file *file, loff_t offset, int origin) 146loff_t default_llseek(struct file *file, loff_t offset, int origin)
130{ 147{
148 struct inode *inode = file->f_path.dentry->d_inode;
131 loff_t retval; 149 loff_t retval;
132 150
133 mutex_lock(&file->f_dentry->d_inode->i_mutex); 151 mutex_lock(&inode->i_mutex);
134 switch (origin) { 152 switch (origin) {
135 case SEEK_END: 153 case SEEK_END:
136 offset += i_size_read(file->f_path.dentry->d_inode); 154 offset += i_size_read(inode);
137 break; 155 break;
138 case SEEK_CUR: 156 case SEEK_CUR:
139 if (offset == 0) { 157 if (offset == 0) {
@@ -141,6 +159,26 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
141 goto out; 159 goto out;
142 } 160 }
143 offset += file->f_pos; 161 offset += file->f_pos;
162 break;
163 case SEEK_DATA:
164 /*
165 * In the generic case the entire file is data, so as
166 * long as offset isn't at the end of the file then the
167 * offset is data.
168 */
169 if (offset >= inode->i_size)
170 return -ENXIO;
171 break;
172 case SEEK_HOLE:
173 /*
174 * There is a virtual hole at the end of the file, so
175 * as long as offset isn't i_size or larger, return
176 * i_size.
177 */
178 if (offset >= inode->i_size)
179 return -ENXIO;
180 offset = inode->i_size;
181 break;
144 } 182 }
145 retval = -EINVAL; 183 retval = -EINVAL;
146 if (offset >= 0 || unsigned_offsets(file)) { 184 if (offset >= 0 || unsigned_offsets(file)) {
@@ -151,7 +189,7 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
151 retval = offset; 189 retval = offset;
152 } 190 }
153out: 191out:
154 mutex_unlock(&file->f_dentry->d_inode->i_mutex); 192 mutex_unlock(&inode->i_mutex);
155 return retval; 193 return retval;
156} 194}
157EXPORT_SYMBOL(default_llseek); 195EXPORT_SYMBOL(default_llseek);