* [PATCH] afs: Fix accidental truncation when storing data
@ 2023-06-28 13:47 David Howells
2023-06-28 20:15 ` Jeffrey E Altman
2023-06-29 11:27 ` Marc Dionne
0 siblings, 2 replies; 4+ messages in thread
From: David Howells @ 2023-06-28 13:47 UTC (permalink / raw)
To: Marc Dionne; +Cc: dhowells, linux-afs, linux-fsdevel, linux-kernel
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: bd80d8a80e12 ("afs: Use ITER_XARRAY for writing")
Reported-by: Marc Dionne <marc.dionne@auristor.com>
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-afs@lists.infradead.org
cc: linux-fsdevel@vger.kernel.org
---
fs/afs/write.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/afs/write.c b/fs/afs/write.c
index 8750b99c3f56..c1f4391ccd7c 100644
--- a/fs/afs/write.c
+++ b/fs/afs/write.c
@@ -413,17 +413,19 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t
afs_op_set_vnode(op, 0, vnode);
op->file[0].dv_delta = 1;
op->file[0].modification = true;
- op->store.write_iter = iter;
op->store.pos = pos;
op->store.size = size;
- op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
op->store.laundering = laundering;
- op->mtime = vnode->netfs.inode.i_mtime;
op->flags |= AFS_OPERATION_UNINTR;
op->ops = &afs_store_data_operation;
try_next_key:
afs_begin_vnode_operation(op);
+
+ op->store.write_iter = iter;
+ op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
+ op->mtime = vnode->netfs.inode.i_mtime;
+
afs_wait_for_operation(op);
switch (op->error) {
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] afs: Fix accidental truncation when storing data
2023-06-28 13:47 [PATCH] afs: Fix accidental truncation when storing data David Howells
@ 2023-06-28 20:15 ` Jeffrey E Altman
2023-06-29 11:27 ` Marc Dionne
1 sibling, 0 replies; 4+ messages in thread
From: Jeffrey E Altman @ 2023-06-28 20:15 UTC (permalink / raw)
To: David Howells, Marc Dionne; +Cc: linux-afs, linux-fsdevel, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 2398 bytes --]
On 6/28/2023 9:47 AM, David Howells wrote:
>
> 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: bd80d8a80e12 ("afs: Use ITER_XARRAY for writing")
> Reported-by: Marc Dionne <marc.dionne@auristor.com>
> Signed-off-by: David Howells <dhowells@redhat.com>
> cc: linux-afs@lists.infradead.org
> cc: linux-fsdevel@vger.kernel.org
> ---
> fs/afs/write.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fs/afs/write.c b/fs/afs/write.c
> index 8750b99c3f56..c1f4391ccd7c 100644
> --- a/fs/afs/write.c
> +++ b/fs/afs/write.c
> @@ -413,17 +413,19 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t
> afs_op_set_vnode(op, 0, vnode);
> op->file[0].dv_delta = 1;
> op->file[0].modification = true;
> - op->store.write_iter = iter;
> op->store.pos = pos;
> op->store.size = size;
> - op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
> op->store.laundering = laundering;
> - op->mtime = vnode->netfs.inode.i_mtime;
> op->flags |= AFS_OPERATION_UNINTR;
> op->ops = &afs_store_data_operation;
>
> try_next_key:
> afs_begin_vnode_operation(op);
> +
> + op->store.write_iter = iter;
> + op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
> + op->mtime = vnode->netfs.inode.i_mtime;
> +
> afs_wait_for_operation(op);
>
> switch (op->error) {
Reviewed-by: Jeffrey Altman <jaltman@auristor.com>
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4039 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] afs: Fix accidental truncation when storing data
2023-06-28 13:47 [PATCH] afs: Fix accidental truncation when storing data David Howells
2023-06-28 20:15 ` Jeffrey E Altman
@ 2023-06-29 11:27 ` Marc Dionne
1 sibling, 0 replies; 4+ messages in thread
From: Marc Dionne @ 2023-06-29 11:27 UTC (permalink / raw)
To: David Howells; +Cc: linux-afs, linux-fsdevel, linux-kernel
On Wed, Jun 28, 2023 at 10:47 AM David Howells <dhowells@redhat.com> wrote:
>
>
> 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: bd80d8a80e12 ("afs: Use ITER_XARRAY for writing")
> Reported-by: Marc Dionne <marc.dionne@auristor.com>
> Signed-off-by: David Howells <dhowells@redhat.com>
> cc: linux-afs@lists.infradead.org
> cc: linux-fsdevel@vger.kernel.org
> ---
> fs/afs/write.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fs/afs/write.c b/fs/afs/write.c
> index 8750b99c3f56..c1f4391ccd7c 100644
> --- a/fs/afs/write.c
> +++ b/fs/afs/write.c
> @@ -413,17 +413,19 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t
> afs_op_set_vnode(op, 0, vnode);
> op->file[0].dv_delta = 1;
> op->file[0].modification = true;
> - op->store.write_iter = iter;
> op->store.pos = pos;
> op->store.size = size;
> - op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
> op->store.laundering = laundering;
> - op->mtime = vnode->netfs.inode.i_mtime;
> op->flags |= AFS_OPERATION_UNINTR;
> op->ops = &afs_store_data_operation;
>
> try_next_key:
> afs_begin_vnode_operation(op);
> +
> + op->store.write_iter = iter;
> + op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
> + op->mtime = vnode->netfs.inode.i_mtime;
> +
> afs_wait_for_operation(op);
>
> switch (op->error) {
Looks good to me; the traces where I got a failure indicate that an
extending store occurred in a different thread while waiting for the
io lock, so this looks like the right fix.
Reviewed-by: Marc Dionne <marc.dionne@auristor.com>
Marc
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] afs: Fix accidental truncation when storing data
@ 2023-07-04 19:22 David Howells
0 siblings, 0 replies; 4+ messages in thread
From: David Howells @ 2023-07-04 19:22 UTC (permalink / raw)
To: torvalds
Cc: dhowells, Jeffrey Altman, Marc Dionne, linux-afs, linux-fsdevel,
linux-kernel
Hi Linus,
Could you apply this fix please?
Thanks,
David
---
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: bd80d8a80e12 ("afs: Use ITER_XARRAY for writing")
Reported-by: Marc Dionne <marc.dionne@auristor.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeffrey Altman <jaltman@auristor.com>
Reviewed-by: Marc Dionne <marc.dionne@auristor.com>
cc: linux-afs@lists.infradead.org
cc: linux-fsdevel@vger.kernel.org
Link: https://lore.kernel.org/r/3526895.1687960024@warthog.procyon.org.uk/
---
fs/afs/write.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/afs/write.c b/fs/afs/write.c
index 8750b99c3f56..c1f4391ccd7c 100644
--- a/fs/afs/write.c
+++ b/fs/afs/write.c
@@ -413,17 +413,19 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t
afs_op_set_vnode(op, 0, vnode);
op->file[0].dv_delta = 1;
op->file[0].modification = true;
- op->store.write_iter = iter;
op->store.pos = pos;
op->store.size = size;
- op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
op->store.laundering = laundering;
- op->mtime = vnode->netfs.inode.i_mtime;
op->flags |= AFS_OPERATION_UNINTR;
op->ops = &afs_store_data_operation;
try_next_key:
afs_begin_vnode_operation(op);
+
+ op->store.write_iter = iter;
+ op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
+ op->mtime = vnode->netfs.inode.i_mtime;
+
afs_wait_for_operation(op);
switch (op->error) {
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-07-04 19:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-28 13:47 [PATCH] afs: Fix accidental truncation when storing data David Howells
2023-06-28 20:15 ` Jeffrey E Altman
2023-06-29 11:27 ` Marc Dionne
-- strict thread matches above, loose matches on Subject: below --
2023-07-04 19:22 David Howells
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).