1

netfs, cifs: Fix handling of short DIO read

Short DIO reads, particularly in relation to cifs, are not being handled
correctly by cifs and netfslib.  This can be tested by doing a DIO read of
a file where the size of read is larger than the size of the file.  When it
crosses the EOF, it gets a short read and this gets retried, and in the
case of cifs, the retry read fails, with the failure being translated to
ENODATA.

Fix this by the following means:

 (1) Add a flag, NETFS_SREQ_HIT_EOF, for the filesystem to set when it
     detects that the read did hit the EOF.

 (2) Make the netfslib read assessment stop processing subrequests when it
     encounters one with that flag set.

 (3) Return rreq->transferred, the accumulated contiguous amount read to
     that point, to userspace for a DIO read.

 (4) Make cifs set the flag and clear the error if the read RPC returned
     ENODATA.

 (5) Make cifs set the flag and clear the error if a short read occurred
     without error and the read-to file position is now at the remote inode
     size.

Fixes: 69c3c023af ("cifs: Implement netfslib hooks")
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.com>
cc: Jeff Layton <jlayton@kernel.org>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Steve French <stfrench@microsoft.com>
This commit is contained in:
David Howells 2024-08-22 23:06:49 +01:00 committed by Steve French
parent 6a5dcd4877
commit 1da29f2c39
3 changed files with 21 additions and 10 deletions

View File

@ -368,7 +368,8 @@ static void netfs_rreq_assess_dio(struct netfs_io_request *rreq)
if (subreq->error || subreq->transferred == 0) if (subreq->error || subreq->transferred == 0)
break; break;
transferred += subreq->transferred; transferred += subreq->transferred;
if (subreq->transferred < subreq->len) if (subreq->transferred < subreq->len ||
test_bit(NETFS_SREQ_HIT_EOF, &subreq->flags))
break; break;
} }
@ -503,7 +504,8 @@ void netfs_subreq_terminated(struct netfs_io_subrequest *subreq,
subreq->error = 0; subreq->error = 0;
subreq->transferred += transferred_or_error; subreq->transferred += transferred_or_error;
if (subreq->transferred < subreq->len) if (subreq->transferred < subreq->len &&
!test_bit(NETFS_SREQ_HIT_EOF, &subreq->flags))
goto incomplete; goto incomplete;
complete: complete:
@ -782,10 +784,13 @@ int netfs_begin_read(struct netfs_io_request *rreq, bool sync)
TASK_UNINTERRUPTIBLE); TASK_UNINTERRUPTIBLE);
ret = rreq->error; ret = rreq->error;
if (ret == 0 && rreq->submitted < rreq->len && if (ret == 0) {
rreq->origin != NETFS_DIO_READ) { if (rreq->origin == NETFS_DIO_READ) {
trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_read); ret = rreq->transferred;
ret = -EIO; } else if (rreq->submitted < rreq->len) {
trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_read);
ret = -EIO;
}
} }
} else { } else {
/* If we decrement nr_outstanding to 0, the ref belongs to us. */ /* If we decrement nr_outstanding to 0, the ref belongs to us. */

View File

@ -4507,6 +4507,7 @@ static void
smb2_readv_callback(struct mid_q_entry *mid) smb2_readv_callback(struct mid_q_entry *mid)
{ {
struct cifs_io_subrequest *rdata = mid->callback_data; struct cifs_io_subrequest *rdata = mid->callback_data;
struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink); struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
struct TCP_Server_Info *server = rdata->server; struct TCP_Server_Info *server = rdata->server;
struct smb2_hdr *shdr = struct smb2_hdr *shdr =
@ -4599,11 +4600,15 @@ smb2_readv_callback(struct mid_q_entry *mid)
rdata->got_bytes); rdata->got_bytes);
if (rdata->result == -ENODATA) { if (rdata->result == -ENODATA) {
/* We may have got an EOF error because fallocate __set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
* failed to enlarge the file. rdata->result = 0;
*/ } else {
if (rdata->subreq.start < rdata->subreq.rreq->i_size) if (rdata->got_bytes < rdata->actual_len &&
rdata->subreq.start + rdata->subreq.transferred + rdata->got_bytes ==
ictx->remote_i_size) {
__set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
rdata->result = 0; rdata->result = 0;
}
} }
trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value, trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value,
server->credits, server->in_flight, server->credits, server->in_flight,

View File

@ -198,6 +198,7 @@ struct netfs_io_subrequest {
#define NETFS_SREQ_NEED_RETRY 9 /* Set if the filesystem requests a retry */ #define NETFS_SREQ_NEED_RETRY 9 /* Set if the filesystem requests a retry */
#define NETFS_SREQ_RETRYING 10 /* Set if we're retrying */ #define NETFS_SREQ_RETRYING 10 /* Set if we're retrying */
#define NETFS_SREQ_FAILED 11 /* Set if the subreq failed unretryably */ #define NETFS_SREQ_FAILED 11 /* Set if the subreq failed unretryably */
#define NETFS_SREQ_HIT_EOF 12 /* Set if we hit the EOF */
}; };
enum netfs_io_origin { enum netfs_io_origin {