Skip to content

Commit

Permalink
fuse: truncate pagecache range on hole punch
Browse files Browse the repository at this point in the history
fuse supports hole punch via the fallocate() FALLOC_FL_PUNCH_HOLE
interface. When a hole punch is passed through, the page cache
is not cleared and thus allows reading stale data from the cache.

This is easily demonstrable (using FOPEN_KEEP_CACHE) by reading a
smallish random data file into cache, punching a hole and creating
a copy of the file. Drop caches or remount and observe that the
original file no longer matches the file copied after the hole
punch. The original file contains a zeroed range and the latter
file contains stale data.

Protect against writepage requests in progress and punch out the
associated page cache range after a successful client fs hole
punch.

Signed-off-by: Brian Foster <[email protected]>
Signed-off-by: Miklos Szeredi <[email protected]>
  • Loading branch information
Brian Foster authored and Miklos Szeredi committed May 20, 2013
1 parent de82b92 commit 3634a63
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions fs/fuse/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/compat.h>
#include <linux/swap.h>
#include <linux/aio.h>
#include <linux/falloc.h>

static const struct file_operations fuse_direct_io_file_operations;

Expand Down Expand Up @@ -2453,6 +2454,7 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
loff_t length)
{
struct fuse_file *ff = file->private_data;
struct inode *inode = file->f_inode;
struct fuse_conn *fc = ff->fc;
struct fuse_req *req;
struct fuse_fallocate_in inarg = {
Expand All @@ -2466,9 +2468,16 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
if (fc->no_fallocate)
return -EOPNOTSUPP;

if (mode & FALLOC_FL_PUNCH_HOLE) {
mutex_lock(&inode->i_mutex);
fuse_set_nowrite(inode);
}

req = fuse_get_req_nopages(fc);
if (IS_ERR(req))
return PTR_ERR(req);
if (IS_ERR(req)) {
err = PTR_ERR(req);
goto out;
}

req->in.h.opcode = FUSE_FALLOCATE;
req->in.h.nodeid = ff->nodeid;
Expand All @@ -2483,6 +2492,15 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
}
fuse_put_request(fc, req);

out:
if (mode & FALLOC_FL_PUNCH_HOLE) {
if (!err)
truncate_pagecache_range(inode, offset,
offset + length - 1);
fuse_release_nowrite(inode);
mutex_unlock(&inode->i_mutex);
}

return err;
}

Expand Down

0 comments on commit 3634a63

Please sign in to comment.