Skip to content

Commit 0327558

Browse files
dhowellstorvalds
authored andcommitted
afs: Fix accidental truncation when storing data
When an AFS FS.StoreData RPC call is made, amongst other things it is given the resultant file size to be. On the server, this is processed by truncating the file to new size and then writing the data. Now, kafs has a lock (vnode->io_lock) that serves to serialise operations against a specific vnode (ie. inode), but the parameters for the op are set before the lock is taken. This allows two writebacks (say sync and kswapd) to race - and if writes are ongoing the writeback for a later write could occur before the writeback for an earlier one if the latter gets interrupted. Note that afs_writepages() cannot take i_mutex and only takes a shared lock on vnode->validate_lock. Also note that the server does the truncation and the write inside a lock, so there's no problem at that end. Fix this by moving the calculation for the proposed new i_size inside the vnode->io_lock. Also reset the iterator (which we might have read from) and update the mtime setting there. Fixes: bd80d8a ("afs: Use ITER_XARRAY for writing") Reported-by: Marc Dionne <[email protected]> Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeffrey Altman <[email protected]> Reviewed-by: Marc Dionne <[email protected]> cc: [email protected] cc: [email protected] Link: https://lore.kernel.org/r/[email protected]/ Signed-off-by: Linus Torvalds <[email protected]>
1 parent 538140c commit 0327558

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

fs/afs/write.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,19 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t
413413
afs_op_set_vnode(op, 0, vnode);
414414
op->file[0].dv_delta = 1;
415415
op->file[0].modification = true;
416-
op->store.write_iter = iter;
417416
op->store.pos = pos;
418417
op->store.size = size;
419-
op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
420418
op->store.laundering = laundering;
421-
op->mtime = vnode->netfs.inode.i_mtime;
422419
op->flags |= AFS_OPERATION_UNINTR;
423420
op->ops = &afs_store_data_operation;
424421

425422
try_next_key:
426423
afs_begin_vnode_operation(op);
424+
425+
op->store.write_iter = iter;
426+
op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
427+
op->mtime = vnode->netfs.inode.i_mtime;
428+
427429
afs_wait_for_operation(op);
428430

429431
switch (op->error) {

0 commit comments

Comments
 (0)