Commit 8c5f9c1a authored by Long Li's avatar Long Li Committed by Steve French

CIFS: Add support for direct I/O write

With direct I/O write, user supplied buffers are pinned to the memory and data
are transferred directly from user buffers to the transport layer.

Change in v3: add support for kernel AIO

Change in v4:
Refactor common write code to __cifs_writev for direct and non-direct I/O.
Retry on direct I/O failure.
Signed-off-by: default avatarLong Li <longli@microsoft.com>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent 6e6e2b86
...@@ -104,6 +104,7 @@ extern ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to); ...@@ -104,6 +104,7 @@ extern ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to);
extern ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to); extern ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to);
extern ssize_t cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to); extern ssize_t cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to);
extern ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from); extern ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from);
extern ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from);
extern ssize_t cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from); extern ssize_t cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from);
extern int cifs_lock(struct file *, int, struct file_lock *); extern int cifs_lock(struct file *, int, struct file_lock *);
extern int cifs_fsync(struct file *, loff_t, loff_t, int); extern int cifs_fsync(struct file *, loff_t, loff_t, int);
......
...@@ -2537,6 +2537,61 @@ wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, ...@@ -2537,6 +2537,61 @@ wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
return 0; return 0;
} }
static int
cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
struct cifs_aio_ctx *ctx)
{
int wait_retry = 0;
unsigned int wsize, credits;
int rc;
struct TCP_Server_Info *server =
tlink_tcon(wdata->cfile->tlink)->ses->server;
/*
* Try to resend this wdata, waiting for credits up to 3 seconds.
* Note: we are attempting to resend the whole wdata not in segments
*/
do {
rc = server->ops->wait_mtu_credits(
server, wdata->bytes, &wsize, &credits);
if (rc)
break;
if (wsize < wdata->bytes) {
add_credits_and_wake_if(server, credits, 0);
msleep(1000);
wait_retry++;
}
} while (wsize < wdata->bytes && wait_retry < 3);
if (wsize < wdata->bytes) {
rc = -EBUSY;
goto out;
}
rc = -EAGAIN;
while (rc == -EAGAIN) {
rc = 0;
if (wdata->cfile->invalidHandle)
rc = cifs_reopen_file(wdata->cfile, false);
if (!rc)
rc = server->ops->async_writev(wdata,
cifs_uncached_writedata_release);
}
if (!rc) {
list_add_tail(&wdata->list, wdata_list);
return 0;
}
add_credits_and_wake_if(server, wdata->credits, 0);
out:
kref_put(&wdata->refcount, cifs_uncached_writedata_release);
return rc;
}
static int static int
cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
struct cifsFileInfo *open_file, struct cifsFileInfo *open_file,
...@@ -2551,6 +2606,8 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, ...@@ -2551,6 +2606,8 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
loff_t saved_offset = offset; loff_t saved_offset = offset;
pid_t pid; pid_t pid;
struct TCP_Server_Info *server; struct TCP_Server_Info *server;
struct page **pagevec;
size_t start;
if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
pid = open_file->pid; pid = open_file->pid;
...@@ -2567,38 +2624,76 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, ...@@ -2567,38 +2624,76 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
if (rc) if (rc)
break; break;
nr_pages = get_numpages(wsize, len, &cur_len); if (ctx->direct_io) {
wdata = cifs_writedata_alloc(nr_pages, cur_len = iov_iter_get_pages_alloc(
from, &pagevec, wsize, &start);
if (cur_len < 0) {
cifs_dbg(VFS,
"direct_writev couldn't get user pages "
"(rc=%zd) iter type %d iov_offset %zd "
"count %zd\n",
cur_len, from->type,
from->iov_offset, from->count);
dump_stack();
break;
}
iov_iter_advance(from, cur_len);
nr_pages =
(cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
wdata = cifs_writedata_direct_alloc(pagevec,
cifs_uncached_writev_complete); cifs_uncached_writev_complete);
if (!wdata) { if (!wdata) {
rc = -ENOMEM; rc = -ENOMEM;
add_credits_and_wake_if(server, credits, 0); add_credits_and_wake_if(server, credits, 0);
break; break;
} }
rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
if (rc) {
kfree(wdata);
add_credits_and_wake_if(server, credits, 0);
break;
}
num_pages = nr_pages; wdata->page_offset = start;
rc = wdata_fill_from_iovec(wdata, from, &cur_len, &num_pages); wdata->tailsz =
if (rc) { nr_pages > 1 ?
for (i = 0; i < nr_pages; i++) cur_len - (PAGE_SIZE - start) -
put_page(wdata->pages[i]); (nr_pages - 2) * PAGE_SIZE :
kfree(wdata); cur_len;
add_credits_and_wake_if(server, credits, 0); } else {
break; nr_pages = get_numpages(wsize, len, &cur_len);
} wdata = cifs_writedata_alloc(nr_pages,
cifs_uncached_writev_complete);
if (!wdata) {
rc = -ENOMEM;
add_credits_and_wake_if(server, credits, 0);
break;
}
/* rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
* Bring nr_pages down to the number of pages we actually used, if (rc) {
* and free any pages that we didn't use. kfree(wdata);
*/ add_credits_and_wake_if(server, credits, 0);
for ( ; nr_pages > num_pages; nr_pages--) break;
put_page(wdata->pages[nr_pages - 1]); }
num_pages = nr_pages;
rc = wdata_fill_from_iovec(
wdata, from, &cur_len, &num_pages);
if (rc) {
for (i = 0; i < nr_pages; i++)
put_page(wdata->pages[i]);
kfree(wdata);
add_credits_and_wake_if(server, credits, 0);
break;
}
/*
* Bring nr_pages down to the number of pages we
* actually used, and free any pages that we didn't use.
*/
for ( ; nr_pages > num_pages; nr_pages--)
put_page(wdata->pages[nr_pages - 1]);
wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
}
wdata->sync_mode = WB_SYNC_ALL; wdata->sync_mode = WB_SYNC_ALL;
wdata->nr_pages = nr_pages; wdata->nr_pages = nr_pages;
...@@ -2607,7 +2702,6 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, ...@@ -2607,7 +2702,6 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
wdata->pid = pid; wdata->pid = pid;
wdata->bytes = cur_len; wdata->bytes = cur_len;
wdata->pagesz = PAGE_SIZE; wdata->pagesz = PAGE_SIZE;
wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
wdata->credits = credits; wdata->credits = credits;
wdata->ctx = ctx; wdata->ctx = ctx;
kref_get(&ctx->refcount); kref_get(&ctx->refcount);
...@@ -2682,13 +2776,18 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) ...@@ -2682,13 +2776,18 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
INIT_LIST_HEAD(&tmp_list); INIT_LIST_HEAD(&tmp_list);
list_del_init(&wdata->list); list_del_init(&wdata->list);
iov_iter_advance(&tmp_from, if (ctx->direct_io)
rc = cifs_resend_wdata(
wdata, &tmp_list, ctx);
else {
iov_iter_advance(&tmp_from,
wdata->offset - ctx->pos); wdata->offset - ctx->pos);
rc = cifs_write_from_iter(wdata->offset, rc = cifs_write_from_iter(wdata->offset,
wdata->bytes, &tmp_from, wdata->bytes, &tmp_from,
ctx->cfile, cifs_sb, &tmp_list, ctx->cfile, cifs_sb, &tmp_list,
ctx); ctx);
}
list_splice(&tmp_list, &ctx->list); list_splice(&tmp_list, &ctx->list);
...@@ -2701,8 +2800,9 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) ...@@ -2701,8 +2800,9 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
kref_put(&wdata->refcount, cifs_uncached_writedata_release); kref_put(&wdata->refcount, cifs_uncached_writedata_release);
} }
for (i = 0; i < ctx->npages; i++) if (!ctx->direct_io)
put_page(ctx->bv[i].bv_page); for (i = 0; i < ctx->npages; i++)
put_page(ctx->bv[i].bv_page);
cifs_stats_bytes_written(tcon, ctx->total_len); cifs_stats_bytes_written(tcon, ctx->total_len);
set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
...@@ -2717,7 +2817,8 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) ...@@ -2717,7 +2817,8 @@ static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
complete(&ctx->done); complete(&ctx->done);
} }
ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) static ssize_t __cifs_writev(
struct kiocb *iocb, struct iov_iter *from, bool direct)
{ {
struct file *file = iocb->ki_filp; struct file *file = iocb->ki_filp;
ssize_t total_written = 0; ssize_t total_written = 0;
...@@ -2726,13 +2827,18 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) ...@@ -2726,13 +2827,18 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
struct cifs_sb_info *cifs_sb; struct cifs_sb_info *cifs_sb;
struct cifs_aio_ctx *ctx; struct cifs_aio_ctx *ctx;
struct iov_iter saved_from = *from; struct iov_iter saved_from = *from;
size_t len = iov_iter_count(from);
int rc; int rc;
/* /*
* BB - optimize the way when signing is disabled. We can drop this * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
* extra memory-to-memory copying and use iovec buffers for constructing * In this case, fall back to non-direct write function.
* write request. * this could be improved by getting pages directly in ITER_KVEC
*/ */
if (direct && from->type & ITER_KVEC) {
cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
direct = false;
}
rc = generic_write_checks(iocb, from); rc = generic_write_checks(iocb, from);
if (rc <= 0) if (rc <= 0)
...@@ -2756,10 +2862,16 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) ...@@ -2756,10 +2862,16 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
ctx->pos = iocb->ki_pos; ctx->pos = iocb->ki_pos;
rc = setup_aio_ctx_iter(ctx, from, WRITE); if (direct) {
if (rc) { ctx->direct_io = true;
kref_put(&ctx->refcount, cifs_aio_ctx_release); ctx->iter = *from;
return rc; ctx->len = len;
} else {
rc = setup_aio_ctx_iter(ctx, from, WRITE);
if (rc) {
kref_put(&ctx->refcount, cifs_aio_ctx_release);
return rc;
}
} }
/* grab a lock here due to read response handlers can access ctx */ /* grab a lock here due to read response handlers can access ctx */
...@@ -2809,6 +2921,16 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) ...@@ -2809,6 +2921,16 @@ ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
return total_written; return total_written;
} }
ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
{
return __cifs_writev(iocb, from, true);
}
ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
{
return __cifs_writev(iocb, from, false);
}
static ssize_t static ssize_t
cifs_writev(struct kiocb *iocb, struct iov_iter *from) cifs_writev(struct kiocb *iocb, struct iov_iter *from)
{ {
......
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