Skip to content

Commit 793fe82

Browse files
committed
afs: Fix truncation issues and mmap writeback size
Fix the following issues: (1) Fix writeback to reduce the size of a store operation to i_size, effectively discarding the extra data. The problem comes when afs_page_mkwrite() records that a page is about to be modified by mmap(). It doesn't know what bits of the page are going to be modified, so it records the whole page as being dirty (this is stored in page->private as start and end offsets). Without this, the marshalling for the store to the server extends the size of the file to the end of the page (in afs_fs_store_data() and yfs_fs_store_data()). (2) Fix setattr to actually truncate the pagecache, thereby clearing the discarded part of a file. (3) Fix setattr to check that the new size is okay and to disable ATTR_SIZE if i_size wouldn't change. (4) Force i_size to be updated as the result of a truncate. (5) Don't truncate if ATTR_SIZE is not set. (6) Call pagecache_isize_extended() if the file was enlarged. Note that truncate_set_size() isn't used because the setting of i_size is done inside afs_vnode_commit_status() under the vnode->cb_lock. Found with the generic/029 and generic/393 xfstests. Fixes: 31143d5 ("AFS: implement basic file write support") Fixes: 4343d00 ("afs: Get rid of the afs_writeback record") Signed-off-by: David Howells <dhowells@redhat.com>
1 parent da8d075 commit 793fe82

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎fs/afs/inode.c‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static void afs_apply_status(struct afs_operation *op,
169169
struct timespec64 t;
170170
umode_t mode;
171171
bool data_changed = false;
172-
bool change_size = false;
172+
bool change_size = vp->set_size;
173173

174174
_enter("{%llx:%llu.%u} %s",
175175
vp->fid.vid, vp->fid.vnode, vp->fid.unique,
@@ -799,7 +799,15 @@ void afs_evict_inode(struct inode *inode)
799799

800800
static void afs_setattr_success(struct afs_operation *op)
801801
{
802+
struct inode *inode = &op->file[0].vnode->vfs_inode;
803+
802804
afs_vnode_commit_status(op, &op->file[0]);
805+
if (op->setattr.attr->ia_valid & ATTR_SIZE) {
806+
loff_t i_size = inode->i_size, size = op->setattr.attr->ia_size;
807+
if (size > i_size)
808+
pagecache_isize_extended(inode, i_size, size);
809+
truncate_pagecache(inode, size);
810+
}
803811
}
804812

805813
static const struct afs_operation_ops afs_setattr_operation = {
@@ -815,6 +823,7 @@ int afs_setattr(struct dentry *dentry, struct iattr *attr)
815823
{
816824
struct afs_operation *op;
817825
struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
826+
int ret;
818827

819828
_enter("{%llx:%llu},{n=%pd},%x",
820829
vnode->fid.vid, vnode->fid.vnode, dentry,
@@ -827,6 +836,18 @@ int afs_setattr(struct dentry *dentry, struct iattr *attr)
827836
return 0;
828837
}
829838

839+
if (attr->ia_valid & ATTR_SIZE) {
840+
if (!S_ISREG(vnode->vfs_inode.i_mode))
841+
return -EISDIR;
842+
843+
ret = inode_newsize_ok(&vnode->vfs_inode, attr->ia_size);
844+
if (ret)
845+
return ret;
846+
847+
if (attr->ia_size == i_size_read(&vnode->vfs_inode))
848+
attr->ia_valid &= ~ATTR_SIZE;
849+
}
850+
830851
/* flush any dirty data outstanding on a regular file */
831852
if (S_ISREG(vnode->vfs_inode.i_mode))
832853
filemap_write_and_wait(vnode->vfs_inode.i_mapping);
@@ -840,8 +861,10 @@ int afs_setattr(struct dentry *dentry, struct iattr *attr)
840861
afs_op_set_vnode(op, 0, vnode);
841862
op->setattr.attr = attr;
842863

843-
if (attr->ia_valid & ATTR_SIZE)
864+
if (attr->ia_valid & ATTR_SIZE) {
844865
op->file[0].dv_delta = 1;
866+
op->file[0].set_size = true;
867+
}
845868
op->ctime = attr->ia_ctime;
846869
op->file[0].update_ctime = 1;
847870

‎fs/afs/internal.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,10 @@ struct afs_vnode_param {
744744
afs_dataversion_t dv_before; /* Data version before the call */
745745
unsigned int cb_break_before; /* cb_break + cb_s_break before the call */
746746
u8 dv_delta; /* Expected change in data version */
747-
bool put_vnode; /* T if we have a ref on the vnode */
748-
bool need_io_lock; /* T if we need the I/O lock on this */
749-
bool update_ctime; /* Need to update the ctime */
747+
bool put_vnode:1; /* T if we have a ref on the vnode */
748+
bool need_io_lock:1; /* T if we need the I/O lock on this */
749+
bool update_ctime:1; /* Need to update the ctime */
750+
bool set_size:1; /* Must update i_size */
750751
};
751752

752753
/*

‎fs/afs/write.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ static int afs_write_back_from_locked_page(struct address_space *mapping,
492492
unsigned long count, priv;
493493
unsigned n, offset, to, f, t;
494494
pgoff_t start, first, last;
495+
loff_t i_size, end;
495496
int loop, ret;
496497

497498
_enter(",%lx", primary_page->index);
@@ -592,7 +593,12 @@ static int afs_write_back_from_locked_page(struct address_space *mapping,
592593
first = primary_page->index;
593594
last = first + count - 1;
594595

596+
end = (loff_t)last * PAGE_SIZE + to;
597+
i_size = i_size_read(&vnode->vfs_inode);
598+
595599
_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
600+
if (end > i_size)
601+
to = i_size & ~PAGE_MASK;
596602

597603
ret = afs_store_data(mapping, first, last, offset, to);
598604
switch (ret) {

0 commit comments

Comments
 (0)