Skip to content

Commit a17754f

Browse files
committed
CacheFiles: Don't write a full page if there's only a partial page to cache
cachefiles_write_page() writes a full page to the backing file for the last page of the netfs file, even if the netfs file's last page is only a partial page. This causes the EOF on the backing file to be extended beyond the EOF of the netfs, and thus the backing file will be truncated by cachefiles_attr_changed() called from cachefiles_lookup_object(). So we need to limit the write we make to the backing file on that last page such that it doesn't push the EOF too far. Also, if a backing file that has a partial page at the end is expanded, we discard the partial page and refetch it on the basis that we then have a hole in the file with invalid data, and should the power go out... A better way to deal with this could be to record a note that the partial page contains invalid data until the correct data is written into it. This isn't a problem for netfs's that discard the whole backing file if the file size changes (such as NFS). Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 868411b commit a17754f

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎fs/cachefiles/interface.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,26 @@ static int cachefiles_attr_changed(struct fscache_object *_object)
404404
if (oi_size == ni_size)
405405
return 0;
406406

407-
newattrs.ia_size = ni_size;
408-
newattrs.ia_valid = ATTR_SIZE;
409-
410407
cachefiles_begin_secure(cache, &saved_cred);
411408
mutex_lock(&object->backer->d_inode->i_mutex);
409+
410+
/* if there's an extension to a partial page at the end of the backing
411+
* file, we need to discard the partial page so that we pick up new
412+
* data after it */
413+
if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
414+
_debug("discard tail %llx", oi_size);
415+
newattrs.ia_valid = ATTR_SIZE;
416+
newattrs.ia_size = oi_size & PAGE_MASK;
417+
ret = notify_change(object->backer, &newattrs);
418+
if (ret < 0)
419+
goto truncate_failed;
420+
}
421+
422+
newattrs.ia_valid = ATTR_SIZE;
423+
newattrs.ia_size = ni_size;
412424
ret = notify_change(object->backer, &newattrs);
425+
426+
truncate_failed:
413427
mutex_unlock(&object->backer->d_inode->i_mutex);
414428
cachefiles_end_secure(cache, saved_cred);
415429

‎fs/cachefiles/rdwr.c‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page)
803803
struct cachefiles_cache *cache;
804804
mm_segment_t old_fs;
805805
struct file *file;
806-
loff_t pos;
806+
loff_t pos, eof;
807+
size_t len;
807808
void *data;
808809
int ret;
809810

@@ -837,15 +838,29 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page)
837838
ret = -EIO;
838839
if (file->f_op->write) {
839840
pos = (loff_t) page->index << PAGE_SHIFT;
841+
842+
/* we mustn't write more data than we have, so we have
843+
* to beware of a partial page at EOF */
844+
eof = object->fscache.store_limit_l;
845+
len = PAGE_SIZE;
846+
if (eof & ~PAGE_MASK) {
847+
ASSERTCMP(pos, <, eof);
848+
if (eof - pos < PAGE_SIZE) {
849+
_debug("cut short %llx to %llx",
850+
pos, eof);
851+
len = eof - pos;
852+
ASSERTCMP(pos + len, ==, eof);
853+
}
854+
}
855+
840856
data = kmap(page);
841857
old_fs = get_fs();
842858
set_fs(KERNEL_DS);
843859
ret = file->f_op->write(
844-
file, (const void __user *) data, PAGE_SIZE,
845-
&pos);
860+
file, (const void __user *) data, len, &pos);
846861
set_fs(old_fs);
847862
kunmap(page);
848-
if (ret != PAGE_SIZE)
863+
if (ret != len)
849864
ret = -EIO;
850865
}
851866
fput(file);

‎include/linux/fscache-cache.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ struct fscache_object {
395395
struct rb_node objlist_link; /* link in global object list */
396396
#endif
397397
pgoff_t store_limit; /* current storage limit */
398+
loff_t store_limit_l; /* current storage limit */
398399
};
399400

400401
extern const char *fscache_object_states[];
@@ -439,6 +440,7 @@ void fscache_object_init(struct fscache_object *object,
439440
object->events = object->event_mask = 0;
440441
object->flags = 0;
441442
object->store_limit = 0;
443+
object->store_limit_l = 0;
442444
object->cache = cache;
443445
object->cookie = cookie;
444446
object->parent = NULL;
@@ -491,6 +493,7 @@ static inline void fscache_object_lookup_error(struct fscache_object *object)
491493
static inline
492494
void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
493495
{
496+
object->store_limit_l = i_size;
494497
object->store_limit = i_size >> PAGE_SHIFT;
495498
if (i_size & ~PAGE_MASK)
496499
object->store_limit++;

0 commit comments

Comments
 (0)