diff options
-rw-r--r-- | fs/ecryptfs/mmap.c | 31 | ||||
-rw-r--r-- | fs/ecryptfs/read_write.c | 27 |
2 files changed, 41 insertions, 17 deletions
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c index 16a7a555f39..32c5711d79a 100644 --- a/fs/ecryptfs/mmap.c +++ b/fs/ecryptfs/mmap.c | |||
@@ -263,14 +263,13 @@ out: | |||
263 | return 0; | 263 | return 0; |
264 | } | 264 | } |
265 | 265 | ||
266 | /* This function must zero any hole we create */ | ||
266 | static int ecryptfs_prepare_write(struct file *file, struct page *page, | 267 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
267 | unsigned from, unsigned to) | 268 | unsigned from, unsigned to) |
268 | { | 269 | { |
269 | int rc = 0; | 270 | int rc = 0; |
271 | loff_t prev_page_end_size; | ||
270 | 272 | ||
271 | if (from == 0 && to == PAGE_CACHE_SIZE) | ||
272 | goto out; /* If we are writing a full page, it will be | ||
273 | up to date. */ | ||
274 | if (!PageUptodate(page)) { | 273 | if (!PageUptodate(page)) { |
275 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, | 274 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
276 | PAGE_CACHE_SIZE, | 275 | PAGE_CACHE_SIZE, |
@@ -283,22 +282,32 @@ static int ecryptfs_prepare_write(struct file *file, struct page *page, | |||
283 | } else | 282 | } else |
284 | SetPageUptodate(page); | 283 | SetPageUptodate(page); |
285 | } | 284 | } |
286 | if (page->index != 0) { | ||
287 | loff_t end_of_prev_pg_pos = | ||
288 | (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1); | ||
289 | 285 | ||
290 | if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) { | 286 | prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT); |
287 | |||
288 | /* | ||
289 | * If creating a page or more of holes, zero them out via truncate. | ||
290 | * Note, this will increase i_size. | ||
291 | */ | ||
292 | if (page->index != 0) { | ||
293 | if (prev_page_end_size > i_size_read(page->mapping->host)) { | ||
291 | rc = ecryptfs_truncate(file->f_path.dentry, | 294 | rc = ecryptfs_truncate(file->f_path.dentry, |
292 | end_of_prev_pg_pos); | 295 | prev_page_end_size); |
293 | if (rc) { | 296 | if (rc) { |
294 | printk(KERN_ERR "Error on attempt to " | 297 | printk(KERN_ERR "Error on attempt to " |
295 | "truncate to (higher) offset [%lld];" | 298 | "truncate to (higher) offset [%lld];" |
296 | " rc = [%d]\n", end_of_prev_pg_pos, rc); | 299 | " rc = [%d]\n", prev_page_end_size, rc); |
297 | goto out; | 300 | goto out; |
298 | } | 301 | } |
299 | } | 302 | } |
300 | if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host)) | 303 | } |
301 | zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); | 304 | /* |
305 | * Writing to a new page, and creating a small hole from start of page? | ||
306 | * Zero it out. | ||
307 | */ | ||
308 | if ((i_size_read(page->mapping->host) == prev_page_end_size) && | ||
309 | (from != 0)) { | ||
310 | zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); | ||
302 | } | 311 | } |
303 | out: | 312 | out: |
304 | return rc; | 313 | return rc; |
diff --git a/fs/ecryptfs/read_write.c b/fs/ecryptfs/read_write.c index 6b7474a4336..948f57624c0 100644 --- a/fs/ecryptfs/read_write.c +++ b/fs/ecryptfs/read_write.c | |||
@@ -124,6 +124,10 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset, | |||
124 | loff_t pos; | 124 | loff_t pos; |
125 | int rc = 0; | 125 | int rc = 0; |
126 | 126 | ||
127 | /* | ||
128 | * if we are writing beyond current size, then start pos | ||
129 | * at the current size - we'll fill in zeros from there. | ||
130 | */ | ||
127 | if (offset > ecryptfs_file_size) | 131 | if (offset > ecryptfs_file_size) |
128 | pos = ecryptfs_file_size; | 132 | pos = ecryptfs_file_size; |
129 | else | 133 | else |
@@ -137,6 +141,7 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset, | |||
137 | if (num_bytes > total_remaining_bytes) | 141 | if (num_bytes > total_remaining_bytes) |
138 | num_bytes = total_remaining_bytes; | 142 | num_bytes = total_remaining_bytes; |
139 | if (pos < offset) { | 143 | if (pos < offset) { |
144 | /* remaining zeros to write, up to destination offset */ | ||
140 | size_t total_remaining_zeros = (offset - pos); | 145 | size_t total_remaining_zeros = (offset - pos); |
141 | 146 | ||
142 | if (num_bytes > total_remaining_zeros) | 147 | if (num_bytes > total_remaining_zeros) |
@@ -167,17 +172,27 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset, | |||
167 | } | 172 | } |
168 | } | 173 | } |
169 | ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0); | 174 | ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0); |
175 | |||
176 | /* | ||
177 | * pos: where we're now writing, offset: where the request was | ||
178 | * If current pos is before request, we are filling zeros | ||
179 | * If we are at or beyond request, we are writing the *data* | ||
180 | * If we're in a fresh page beyond eof, zero it in either case | ||
181 | */ | ||
182 | if (pos < offset || !start_offset_in_page) { | ||
183 | /* We are extending past the previous end of the file. | ||
184 | * Fill in zero values to the end of the page */ | ||
185 | memset(((char *)ecryptfs_page_virt | ||
186 | + start_offset_in_page), 0, | ||
187 | PAGE_CACHE_SIZE - start_offset_in_page); | ||
188 | } | ||
189 | |||
190 | /* pos >= offset, we are now writing the data request */ | ||
170 | if (pos >= offset) { | 191 | if (pos >= offset) { |
171 | memcpy(((char *)ecryptfs_page_virt | 192 | memcpy(((char *)ecryptfs_page_virt |
172 | + start_offset_in_page), | 193 | + start_offset_in_page), |
173 | (data + data_offset), num_bytes); | 194 | (data + data_offset), num_bytes); |
174 | data_offset += num_bytes; | 195 | data_offset += num_bytes; |
175 | } else { | ||
176 | /* We are extending past the previous end of the file. | ||
177 | * Fill in zero values up to the start of where we | ||
178 | * will be writing data. */ | ||
179 | memset(((char *)ecryptfs_page_virt | ||
180 | + start_offset_in_page), 0, num_bytes); | ||
181 | } | 196 | } |
182 | kunmap_atomic(ecryptfs_page_virt, KM_USER0); | 197 | kunmap_atomic(ecryptfs_page_virt, KM_USER0); |
183 | flush_dcache_page(ecryptfs_page); | 198 | flush_dcache_page(ecryptfs_page); |