Commit 9a1c67e8 authored by Pavel Shilovsky's avatar Pavel Shilovsky Committed by Steve French

CIFS: Adjust MTU credits before reopening a file

Currently we adjust MTU credits before sending an IO request
and after reopening a file. This approach doesn't allow the
reopen routine to use existing credits that are not needed
for IO. Reorder credit adjustment and reopening a file to
use credits available to the client more efficiently. Also
unwrap complex if statement into few pieces to improve
readability.
Signed-off-by: default avatarPavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent 97ea4998
...@@ -457,6 +457,10 @@ struct smb_version_operations { ...@@ -457,6 +457,10 @@ struct smb_version_operations {
/* get mtu credits */ /* get mtu credits */
int (*wait_mtu_credits)(struct TCP_Server_Info *, unsigned int, int (*wait_mtu_credits)(struct TCP_Server_Info *, unsigned int,
unsigned int *, struct cifs_credits *); unsigned int *, struct cifs_credits *);
/* adjust previously taken mtu credits to request size */
int (*adjust_credits)(struct TCP_Server_Info *server,
struct cifs_credits *credits,
const unsigned int payload_size);
/* check if we need to issue closedir */ /* check if we need to issue closedir */
bool (*dir_needs_close)(struct cifsFileInfo *); bool (*dir_needs_close)(struct cifsFileInfo *);
long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t, long (*fallocate)(struct file *, struct cifs_tcon *, int, loff_t,
...@@ -763,6 +767,14 @@ set_credits(struct TCP_Server_Info *server, const int val) ...@@ -763,6 +767,14 @@ set_credits(struct TCP_Server_Info *server, const int val)
server->ops->set_credits(server, val); server->ops->set_credits(server, val);
} }
static inline int
adjust_credits(struct TCP_Server_Info *server, struct cifs_credits *credits,
const unsigned int payload_size)
{
return server->ops->adjust_credits ?
server->ops->adjust_credits(server, credits, payload_size) : 0;
}
static inline __le64 static inline __le64
get_next_mid64(struct TCP_Server_Info *server) get_next_mid64(struct TCP_Server_Info *server)
{ {
......
...@@ -2076,11 +2076,11 @@ wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, ...@@ -2076,11 +2076,11 @@ wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
} }
static int static int
wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, wdata_send_pages(struct TCP_Server_Info *server, struct cifs_writedata *wdata,
struct address_space *mapping, struct writeback_control *wbc) unsigned int nr_pages, struct address_space *mapping,
struct writeback_control *wbc)
{ {
int rc = 0; int rc = 0;
struct TCP_Server_Info *server;
unsigned int i; unsigned int i;
wdata->sync_mode = wbc->sync_mode; wdata->sync_mode = wbc->sync_mode;
...@@ -2092,6 +2092,10 @@ wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, ...@@ -2092,6 +2092,10 @@ wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
(loff_t)PAGE_SIZE); (loff_t)PAGE_SIZE);
wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
rc = adjust_credits(server, &wdata->credits, wdata->bytes);
if (rc)
goto send_pages_out;
if (wdata->cfile != NULL) if (wdata->cfile != NULL)
cifsFileInfo_put(wdata->cfile); cifsFileInfo_put(wdata->cfile);
wdata->cfile = find_writable_file(CIFS_I(mapping->host), false); wdata->cfile = find_writable_file(CIFS_I(mapping->host), false);
...@@ -2100,10 +2104,10 @@ wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, ...@@ -2100,10 +2104,10 @@ wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
rc = -EBADF; rc = -EBADF;
} else { } else {
wdata->pid = wdata->cfile->pid; wdata->pid = wdata->cfile->pid;
server = tlink_tcon(wdata->cfile->tlink)->ses->server;
rc = server->ops->async_writev(wdata, cifs_writedata_release); rc = server->ops->async_writev(wdata, cifs_writedata_release);
} }
send_pages_out:
for (i = 0; i < nr_pages; ++i) for (i = 0; i < nr_pages; ++i)
unlock_page(wdata->pages[i]); unlock_page(wdata->pages[i]);
...@@ -2184,7 +2188,7 @@ static int cifs_writepages(struct address_space *mapping, ...@@ -2184,7 +2188,7 @@ static int cifs_writepages(struct address_space *mapping,
wdata->credits = credits_on_stack; wdata->credits = credits_on_stack;
rc = wdata_send_pages(wdata, nr_pages, mapping, wbc); rc = wdata_send_pages(server, wdata, nr_pages, mapping, wbc);
/* send failure -- clean up the mess */ /* send failure -- clean up the mess */
if (rc != 0) { if (rc != 0) {
...@@ -2743,10 +2747,17 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, ...@@ -2743,10 +2747,17 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
wdata->ctx = ctx; wdata->ctx = ctx;
kref_get(&ctx->refcount); kref_get(&ctx->refcount);
if (!wdata->cfile->invalidHandle || rc = adjust_credits(server, &wdata->credits, wdata->bytes);
!(rc = cifs_reopen_file(wdata->cfile, false)))
if (!rc) {
if (wdata->cfile->invalidHandle)
rc = cifs_reopen_file(wdata->cfile, false);
if (!rc)
rc = server->ops->async_writev(wdata, rc = server->ops->async_writev(wdata,
cifs_uncached_writedata_release); cifs_uncached_writedata_release);
}
if (rc) { if (rc) {
add_credits_and_wake_if(server, &wdata->credits, 0); add_credits_and_wake_if(server, &wdata->credits, 0);
kref_put(&wdata->refcount, kref_put(&wdata->refcount,
...@@ -3423,9 +3434,16 @@ cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file, ...@@ -3423,9 +3434,16 @@ cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
rdata->ctx = ctx; rdata->ctx = ctx;
kref_get(&ctx->refcount); kref_get(&ctx->refcount);
if (!rdata->cfile->invalidHandle || rc = adjust_credits(server, &rdata->credits, rdata->bytes);
!(rc = cifs_reopen_file(rdata->cfile, true)))
if (!rc) {
if (rdata->cfile->invalidHandle)
rc = cifs_reopen_file(rdata->cfile, true);
if (!rc)
rc = server->ops->async_readv(rdata); rc = server->ops->async_readv(rdata);
}
if (rc) { if (rc) {
add_credits_and_wake_if(server, &rdata->credits, 0); add_credits_and_wake_if(server, &rdata->credits, 0);
kref_put(&rdata->refcount, kref_put(&rdata->refcount,
...@@ -4163,9 +4181,16 @@ static int cifs_readpages(struct file *file, struct address_space *mapping, ...@@ -4163,9 +4181,16 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
rdata->pages[rdata->nr_pages++] = page; rdata->pages[rdata->nr_pages++] = page;
} }
if (!rdata->cfile->invalidHandle || rc = adjust_credits(server, &rdata->credits, rdata->bytes);
!(rc = cifs_reopen_file(rdata->cfile, true)))
if (!rc) {
if (rdata->cfile->invalidHandle)
rc = cifs_reopen_file(rdata->cfile, true);
if (!rc)
rc = server->ops->async_readv(rdata); rc = server->ops->async_readv(rdata);
}
if (rc) { if (rc) {
add_credits_and_wake_if(server, &rdata->credits, 0); add_credits_and_wake_if(server, &rdata->credits, 0);
for (i = 0; i < rdata->nr_pages; i++) { for (i = 0; i < rdata->nr_pages; i++) {
......
...@@ -222,6 +222,38 @@ smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size, ...@@ -222,6 +222,38 @@ smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
return rc; return rc;
} }
static int
smb2_adjust_credits(struct TCP_Server_Info *server,
struct cifs_credits *credits,
const unsigned int payload_size)
{
int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
if (!credits->value || credits->value == new_val)
return 0;
if (credits->value < new_val) {
WARN_ONCE(1, "request has less credits (%d) than required (%d)",
credits->value, new_val);
return -ENOTSUPP;
}
spin_lock(&server->req_lock);
if (server->reconnect_instance != credits->instance) {
spin_unlock(&server->req_lock);
cifs_dbg(VFS, "trying to return %d credits to old session\n",
credits->value - new_val);
return -EAGAIN;
}
server->credits += credits->value - new_val;
spin_unlock(&server->req_lock);
wake_up(&server->request_q);
credits->value = new_val;
return 0;
}
static __u64 static __u64
smb2_get_next_mid(struct TCP_Server_Info *server) smb2_get_next_mid(struct TCP_Server_Info *server)
{ {
...@@ -3678,6 +3710,7 @@ struct smb_version_operations smb21_operations = { ...@@ -3678,6 +3710,7 @@ struct smb_version_operations smb21_operations = {
.get_credits_field = smb2_get_credits_field, .get_credits_field = smb2_get_credits_field,
.get_credits = smb2_get_credits, .get_credits = smb2_get_credits,
.wait_mtu_credits = smb2_wait_mtu_credits, .wait_mtu_credits = smb2_wait_mtu_credits,
.adjust_credits = smb2_adjust_credits,
.get_next_mid = smb2_get_next_mid, .get_next_mid = smb2_get_next_mid,
.revert_current_mid = smb2_revert_current_mid, .revert_current_mid = smb2_revert_current_mid,
.read_data_offset = smb2_read_data_offset, .read_data_offset = smb2_read_data_offset,
...@@ -3775,6 +3808,7 @@ struct smb_version_operations smb30_operations = { ...@@ -3775,6 +3808,7 @@ struct smb_version_operations smb30_operations = {
.get_credits_field = smb2_get_credits_field, .get_credits_field = smb2_get_credits_field,
.get_credits = smb2_get_credits, .get_credits = smb2_get_credits,
.wait_mtu_credits = smb2_wait_mtu_credits, .wait_mtu_credits = smb2_wait_mtu_credits,
.adjust_credits = smb2_adjust_credits,
.get_next_mid = smb2_get_next_mid, .get_next_mid = smb2_get_next_mid,
.revert_current_mid = smb2_revert_current_mid, .revert_current_mid = smb2_revert_current_mid,
.read_data_offset = smb2_read_data_offset, .read_data_offset = smb2_read_data_offset,
...@@ -3881,6 +3915,7 @@ struct smb_version_operations smb311_operations = { ...@@ -3881,6 +3915,7 @@ struct smb_version_operations smb311_operations = {
.get_credits_field = smb2_get_credits_field, .get_credits_field = smb2_get_credits_field,
.get_credits = smb2_get_credits, .get_credits = smb2_get_credits,
.wait_mtu_credits = smb2_wait_mtu_credits, .wait_mtu_credits = smb2_wait_mtu_credits,
.adjust_credits = smb2_adjust_credits,
.get_next_mid = smb2_get_next_mid, .get_next_mid = smb2_get_next_mid,
.revert_current_mid = smb2_revert_current_mid, .revert_current_mid = smb2_revert_current_mid,
.read_data_offset = smb2_read_data_offset, .read_data_offset = smb2_read_data_offset,
......
...@@ -3322,21 +3322,11 @@ smb2_async_readv(struct cifs_readdata *rdata) ...@@ -3322,21 +3322,11 @@ smb2_async_readv(struct cifs_readdata *rdata)
SMB2_MAX_BUFFER_SIZE)); SMB2_MAX_BUFFER_SIZE));
shdr->CreditRequest = shdr->CreditRequest =
cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
spin_lock(&server->req_lock);
if (server->reconnect_instance == rdata->credits.instance) rc = adjust_credits(server, &rdata->credits, rdata->bytes);
server->credits += rdata->credits.value - if (rc)
le16_to_cpu(shdr->CreditCharge);
else {
spin_unlock(&server->req_lock);
cifs_dbg(VFS, "trying to return %u credits to old session\n",
rdata->credits.value
- le16_to_cpu(shdr->CreditCharge));
rc = -EAGAIN;
goto async_readv_out; goto async_readv_out;
}
spin_unlock(&server->req_lock);
wake_up(&server->request_q);
rdata->credits.value = le16_to_cpu(shdr->CreditCharge);
flags |= CIFS_HAS_CREDITS; flags |= CIFS_HAS_CREDITS;
} }
...@@ -3626,21 +3616,11 @@ smb2_async_writev(struct cifs_writedata *wdata, ...@@ -3626,21 +3616,11 @@ smb2_async_writev(struct cifs_writedata *wdata,
SMB2_MAX_BUFFER_SIZE)); SMB2_MAX_BUFFER_SIZE));
shdr->CreditRequest = shdr->CreditRequest =
cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1); cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
spin_lock(&server->req_lock);
if (server->reconnect_instance == wdata->credits.instance) rc = adjust_credits(server, &wdata->credits, wdata->bytes);
server->credits += wdata->credits.value - if (rc)
le16_to_cpu(shdr->CreditCharge);
else {
spin_unlock(&server->req_lock);
cifs_dbg(VFS, "trying to return %d credits to old session\n",
wdata->credits.value
- le16_to_cpu(shdr->CreditCharge));
rc = -EAGAIN;
goto async_writev_out; goto async_writev_out;
}
spin_unlock(&server->req_lock);
wake_up(&server->request_q);
wdata->credits.value = le16_to_cpu(shdr->CreditCharge);
flags |= CIFS_HAS_CREDITS; flags |= CIFS_HAS_CREDITS;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment