Commit c8aedb3b authored by David Howells's avatar David Howells Committed by Luis Henriques

FS-Cache: Handle a write to the page immediately beyond the EOF marker

commit 102f4d90 upstream.

Handle a write being requested to the page immediately beyond the EOF
marker on a cache object.  Currently this gets an assertion failure in
CacheFiles because the EOF marker is used there to encode information about
a partial page at the EOF - which could lead to an unknown blank spot in
the file if we extend the file over it.

The problem is actually in fscache where we check the index of the page
being written against store_limit.  store_limit is set to the number of
pages that we're allowed to store by fscache_set_store_limit() - which
means it's one more than the index of the last page we're allowed to store.
The problem is that we permit writing to a page with an index _equal_ to
the store limit - when we should reject that case.

Whilst we're at it, change the triggered assertion in CacheFiles to just
return -ENOBUFS instead.

The assertion failure looks something like this:

CacheFiles: Assertion failed
1000 < 7b1 is false
------------[ cut here ]------------
kernel BUG at fs/cachefiles/rdwr.c:962!
...
RIP: 0010:[<ffffffffa02c9e83>]  [<ffffffffa02c9e83>] cachefiles_write_page+0x273/0x2d0 [cachefiles]
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
[ kamal: backport to 3.13-stable: no __kernel_write(); thanks Ben H. ]
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent fd7060f6
...@@ -912,6 +912,15 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page) ...@@ -912,6 +912,15 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page)
cache = container_of(object->fscache.cache, cache = container_of(object->fscache.cache,
struct cachefiles_cache, cache); struct cachefiles_cache, cache);
pos = (loff_t)page->index << PAGE_SHIFT;
/* We mustn't write more data than we have, so we have to beware of a
* partial page at EOF.
*/
eof = object->fscache.store_limit_l;
if (pos >= eof)
goto error;
/* write the page to the backing filesystem and let it store it in its /* write the page to the backing filesystem and let it store it in its
* own time */ * own time */
path.mnt = cache->mnt; path.mnt = cache->mnt;
...@@ -919,17 +928,11 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page) ...@@ -919,17 +928,11 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page)
file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred); file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
if (IS_ERR(file)) { if (IS_ERR(file)) {
ret = PTR_ERR(file); ret = PTR_ERR(file);
} else { goto error_2;
ret = -EIO; }
if (file->f_op->write) {
pos = (loff_t) page->index << PAGE_SHIFT;
/* we mustn't write more data than we have, so we have
* to beware of a partial page at EOF */
eof = object->fscache.store_limit_l;
len = PAGE_SIZE; len = PAGE_SIZE;
if (eof & ~PAGE_MASK) { if (eof & ~PAGE_MASK) {
ASSERTCMP(pos, <, eof);
if (eof - pos < PAGE_SIZE) { if (eof - pos < PAGE_SIZE) {
_debug("cut short %llx to %llx", _debug("cut short %llx to %llx",
pos, eof); pos, eof);
...@@ -946,22 +949,22 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page) ...@@ -946,22 +949,22 @@ int cachefiles_write_page(struct fscache_storage *op, struct page *page)
file, (const void __user *) data, len, &pos); file, (const void __user *) data, len, &pos);
set_fs(old_fs); set_fs(old_fs);
kunmap(page); kunmap(page);
file_end_write(file);
if (ret != len)
ret = -EIO;
}
fput(file); fput(file);
} if (ret != len)
goto error_eio;
if (ret < 0) { _leave(" = 0");
if (ret == -EIO) return 0;
cachefiles_io_error_obj(
object, "Write page to backing file failed");
ret = -ENOBUFS;
}
_leave(" = %d", ret); error_eio:
return ret; ret = -EIO;
error_2:
if (ret == -EIO)
cachefiles_io_error_obj(object,
"Write page to backing file failed");
error:
_leave(" = -ENOBUFS [%d]", ret);
return -ENOBUFS;
} }
/* /*
......
...@@ -801,7 +801,7 @@ static void fscache_write_op(struct fscache_operation *_op) ...@@ -801,7 +801,7 @@ static void fscache_write_op(struct fscache_operation *_op)
goto superseded; goto superseded;
page = results[0]; page = results[0];
_debug("gang %d [%lx]", n, page->index); _debug("gang %d [%lx]", n, page->index);
if (page->index > op->store_limit) { if (page->index >= op->store_limit) {
fscache_stat(&fscache_n_store_pages_over_limit); fscache_stat(&fscache_n_store_pages_over_limit);
goto superseded; goto superseded;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment