Commit c48722c6 authored by Al Viro's avatar Al Viro

Merge branch '9p-iov_iter' into for-next

parents 34d0640e dcdbd7b2
...@@ -68,14 +68,10 @@ int v9fs_file_open(struct inode *inode, struct file *file); ...@@ -68,14 +68,10 @@ int v9fs_file_open(struct inode *inode, struct file *file);
void v9fs_inode2stat(struct inode *inode, struct p9_wstat *stat); void v9fs_inode2stat(struct inode *inode, struct p9_wstat *stat);
int v9fs_uflags2omode(int uflags, int extended); int v9fs_uflags2omode(int uflags, int extended);
ssize_t v9fs_file_readn(struct file *, char *, char __user *, u32, u64);
ssize_t v9fs_fid_readn(struct p9_fid *, char *, char __user *, u32, u64);
void v9fs_blank_wstat(struct p9_wstat *wstat); void v9fs_blank_wstat(struct p9_wstat *wstat);
int v9fs_vfs_setattr_dotl(struct dentry *, struct iattr *); int v9fs_vfs_setattr_dotl(struct dentry *, struct iattr *);
int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end, int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
int datasync); int datasync);
ssize_t v9fs_file_write_internal(struct inode *, struct p9_fid *,
const char __user *, size_t, loff_t *, int);
int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode); int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode);
int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode); int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode);
static inline void v9fs_invalidate_inode_attr(struct inode *inode) static inline void v9fs_invalidate_inode_attr(struct inode *inode)
......
...@@ -51,12 +51,11 @@ ...@@ -51,12 +51,11 @@
*/ */
static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page) static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page)
{ {
int retval; struct inode *inode = page->mapping->host;
loff_t offset; struct bio_vec bvec = {.bv_page = page, .bv_len = PAGE_SIZE};
char *buffer; struct iov_iter to;
struct inode *inode; int retval, err;
inode = page->mapping->host;
p9_debug(P9_DEBUG_VFS, "\n"); p9_debug(P9_DEBUG_VFS, "\n");
BUG_ON(!PageLocked(page)); BUG_ON(!PageLocked(page));
...@@ -65,16 +64,16 @@ static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page) ...@@ -65,16 +64,16 @@ static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page)
if (retval == 0) if (retval == 0)
return retval; return retval;
buffer = kmap(page); iov_iter_bvec(&to, ITER_BVEC | READ, &bvec, 1, PAGE_SIZE);
offset = page_offset(page);
retval = v9fs_fid_readn(fid, buffer, NULL, PAGE_CACHE_SIZE, offset); retval = p9_client_read(fid, page_offset(page), &to, &err);
if (retval < 0) { if (err) {
v9fs_uncache_page(inode, page); v9fs_uncache_page(inode, page);
retval = err;
goto done; goto done;
} }
memset(buffer + retval, 0, PAGE_CACHE_SIZE - retval); zero_user(page, retval, PAGE_SIZE - retval);
flush_dcache_page(page); flush_dcache_page(page);
SetPageUptodate(page); SetPageUptodate(page);
...@@ -82,7 +81,6 @@ static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page) ...@@ -82,7 +81,6 @@ static int v9fs_fid_readpage(struct p9_fid *fid, struct page *page)
retval = 0; retval = 0;
done: done:
kunmap(page);
unlock_page(page); unlock_page(page);
return retval; return retval;
} }
...@@ -161,41 +159,32 @@ static void v9fs_invalidate_page(struct page *page, unsigned int offset, ...@@ -161,41 +159,32 @@ static void v9fs_invalidate_page(struct page *page, unsigned int offset,
static int v9fs_vfs_writepage_locked(struct page *page) static int v9fs_vfs_writepage_locked(struct page *page)
{ {
char *buffer;
int retval, len;
loff_t offset, size;
mm_segment_t old_fs;
struct v9fs_inode *v9inode;
struct inode *inode = page->mapping->host; struct inode *inode = page->mapping->host;
struct v9fs_inode *v9inode = V9FS_I(inode);
loff_t size = i_size_read(inode);
struct iov_iter from;
struct bio_vec bvec;
int err, len;
v9inode = V9FS_I(inode);
size = i_size_read(inode);
if (page->index == size >> PAGE_CACHE_SHIFT) if (page->index == size >> PAGE_CACHE_SHIFT)
len = size & ~PAGE_CACHE_MASK; len = size & ~PAGE_CACHE_MASK;
else else
len = PAGE_CACHE_SIZE; len = PAGE_CACHE_SIZE;
set_page_writeback(page); bvec.bv_page = page;
bvec.bv_offset = 0;
buffer = kmap(page); bvec.bv_len = len;
offset = page_offset(page); iov_iter_bvec(&from, ITER_BVEC | WRITE, &bvec, 1, len);
old_fs = get_fs();
set_fs(get_ds());
/* We should have writeback_fid always set */ /* We should have writeback_fid always set */
BUG_ON(!v9inode->writeback_fid); BUG_ON(!v9inode->writeback_fid);
retval = v9fs_file_write_internal(inode, set_page_writeback(page);
v9inode->writeback_fid,
(__force const char __user *)buffer, p9_client_write(v9inode->writeback_fid, page_offset(page), &from, &err);
len, &offset, 0);
if (retval > 0)
retval = 0;
set_fs(old_fs);
kunmap(page);
end_page_writeback(page); end_page_writeback(page);
return retval; return err;
} }
static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc) static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
...@@ -261,16 +250,21 @@ static int v9fs_launder_page(struct page *page) ...@@ -261,16 +250,21 @@ static int v9fs_launder_page(struct page *page)
static ssize_t static ssize_t
v9fs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos) v9fs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
{ {
/* struct file *file = iocb->ki_filp;
* FIXME ssize_t n;
* Now that we do caching with cache mode enabled, We need int err = 0;
* to support direct IO if (rw & WRITE) {
*/ n = p9_client_write(file->private_data, pos, iter, &err);
p9_debug(P9_DEBUG_VFS, "v9fs_direct_IO: v9fs_direct_IO (%pD) off/no(%lld/%lu) EINVAL\n", if (n) {
iocb->ki_filp, struct inode *inode = file_inode(file);
(long long)pos, iter->nr_segs); loff_t i_size = i_size_read(inode);
if (pos + n > i_size)
return -EINVAL; inode_add_bytes(inode, pos + n - i_size);
}
} else {
n = p9_client_read(file->private_data, pos, iter, &err);
}
return n ? n : err;
} }
static int v9fs_write_begin(struct file *filp, struct address_space *mapping, static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <linux/inet.h> #include <linux/inet.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/uio.h>
#include <net/9p/9p.h> #include <net/9p/9p.h>
#include <net/9p/client.h> #include <net/9p/client.h>
...@@ -115,6 +116,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) ...@@ -115,6 +116,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
int buflen; int buflen;
int reclen = 0; int reclen = 0;
struct p9_rdir *rdir; struct p9_rdir *rdir;
struct kvec kvec;
p9_debug(P9_DEBUG_VFS, "name %pD\n", file); p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
fid = file->private_data; fid = file->private_data;
...@@ -124,16 +126,21 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) ...@@ -124,16 +126,21 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
rdir = v9fs_alloc_rdir_buf(file, buflen); rdir = v9fs_alloc_rdir_buf(file, buflen);
if (!rdir) if (!rdir)
return -ENOMEM; return -ENOMEM;
kvec.iov_base = rdir->buf;
kvec.iov_len = buflen;
while (1) { while (1) {
if (rdir->tail == rdir->head) { if (rdir->tail == rdir->head) {
err = v9fs_file_readn(file, rdir->buf, NULL, struct iov_iter to;
buflen, ctx->pos); int n;
if (err <= 0) iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buflen);
n = p9_client_read(file->private_data, ctx->pos, &to,
&err);
if (err)
return err; return err;
rdir->head = 0; rdir->head = 0;
rdir->tail = err; rdir->tail = n;
} }
while (rdir->head < rdir->tail) { while (rdir->head < rdir->tail) {
p9stat_init(&st); p9stat_init(&st);
......
This diff is collapsed.
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/uio.h>
#include <net/9p/9p.h> #include <net/9p/9p.h>
#include <net/9p/client.h> #include <net/9p/client.h>
...@@ -25,50 +26,34 @@ ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name, ...@@ -25,50 +26,34 @@ ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
void *buffer, size_t buffer_size) void *buffer, size_t buffer_size)
{ {
ssize_t retval; ssize_t retval;
int msize, read_count; u64 attr_size;
u64 offset = 0, attr_size;
struct p9_fid *attr_fid; struct p9_fid *attr_fid;
struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
struct iov_iter to;
int err;
iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buffer_size);
attr_fid = p9_client_xattrwalk(fid, name, &attr_size); attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
if (IS_ERR(attr_fid)) { if (IS_ERR(attr_fid)) {
retval = PTR_ERR(attr_fid); retval = PTR_ERR(attr_fid);
p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n", p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
retval); retval);
attr_fid = NULL; return retval;
goto error;
}
if (!buffer_size) {
/* request to get the attr_size */
retval = attr_size;
goto error;
} }
if (attr_size > buffer_size) { if (attr_size > buffer_size) {
retval = -ERANGE; if (!buffer_size) /* request to get the attr_size */
goto error; retval = attr_size;
}
msize = attr_fid->clnt->msize;
while (attr_size) {
if (attr_size > (msize - P9_IOHDRSZ))
read_count = msize - P9_IOHDRSZ;
else else
read_count = attr_size; retval = -ERANGE;
read_count = p9_client_read(attr_fid, ((char *)buffer)+offset, } else {
NULL, offset, read_count); iov_iter_truncate(&to, attr_size);
if (read_count < 0) { retval = p9_client_read(attr_fid, 0, &to, &err);
/* error in xattr read */ if (err)
retval = read_count; retval = err;
goto error;
}
offset += read_count;
attr_size -= read_count;
} }
/* Total read xattr bytes */
retval = offset;
error:
if (attr_fid)
p9_client_clunk(attr_fid); p9_client_clunk(attr_fid);
return retval; return retval;
} }
...@@ -120,8 +105,11 @@ int v9fs_xattr_set(struct dentry *dentry, const char *name, ...@@ -120,8 +105,11 @@ int v9fs_xattr_set(struct dentry *dentry, const char *name,
int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name, int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
const void *value, size_t value_len, int flags) const void *value, size_t value_len, int flags)
{ {
u64 offset = 0; struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
int retval, msize, write_count; struct iov_iter from;
int retval;
iov_iter_kvec(&from, WRITE | ITER_KVEC, &kvec, 1, value_len);
p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n", p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
name, value_len, flags); name, value_len, flags);
...@@ -135,29 +123,11 @@ int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name, ...@@ -135,29 +123,11 @@ int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
* On success fid points to xattr * On success fid points to xattr
*/ */
retval = p9_client_xattrcreate(fid, name, value_len, flags); retval = p9_client_xattrcreate(fid, name, value_len, flags);
if (retval < 0) { if (retval < 0)
p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n", p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
retval); retval);
goto err;
}
msize = fid->clnt->msize;
while (value_len) {
if (value_len > (msize - P9_IOHDRSZ))
write_count = msize - P9_IOHDRSZ;
else else
write_count = value_len; p9_client_write(fid, 0, &from, &retval);
write_count = p9_client_write(fid, ((char *)value)+offset,
NULL, offset, write_count);
if (write_count < 0) {
/* error in xattr write */
retval = write_count;
goto err;
}
offset += write_count;
value_len -= write_count;
}
retval = 0;
err:
p9_client_clunk(fid); p9_client_clunk(fid);
return retval; return retval;
} }
......
...@@ -211,6 +211,8 @@ struct p9_dirent { ...@@ -211,6 +211,8 @@ struct p9_dirent {
char d_name[256]; char d_name[256];
}; };
struct iov_iter;
int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb); int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb);
int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid, int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid,
const char *name); const char *name);
...@@ -236,10 +238,8 @@ int p9_client_clunk(struct p9_fid *fid); ...@@ -236,10 +238,8 @@ int p9_client_clunk(struct p9_fid *fid);
int p9_client_fsync(struct p9_fid *fid, int datasync); int p9_client_fsync(struct p9_fid *fid, int datasync);
int p9_client_remove(struct p9_fid *fid); int p9_client_remove(struct p9_fid *fid);
int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags); int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags);
int p9_client_read(struct p9_fid *fid, char *data, char __user *udata, int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err);
u64 offset, u32 count); int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err);
int p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
u64 offset, u32 count);
int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset); int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset);
int p9dirent_read(struct p9_client *clnt, char *buf, int len, int p9dirent_read(struct p9_client *clnt, char *buf, int len,
struct p9_dirent *dirent); struct p9_dirent *dirent);
......
...@@ -61,7 +61,7 @@ struct p9_trans_module { ...@@ -61,7 +61,7 @@ struct p9_trans_module {
int (*cancel) (struct p9_client *, struct p9_req_t *req); int (*cancel) (struct p9_client *, struct p9_req_t *req);
int (*cancelled)(struct p9_client *, struct p9_req_t *req); int (*cancelled)(struct p9_client *, struct p9_req_t *req);
int (*zc_request)(struct p9_client *, struct p9_req_t *, int (*zc_request)(struct p9_client *, struct p9_req_t *,
char *, char *, int , int, int, int); struct iov_iter *, struct iov_iter *, int , int, int);
}; };
void v9fs_register_trans(struct p9_trans_module *m); void v9fs_register_trans(struct p9_trans_module *m);
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/uio.h>
#include <net/9p/9p.h> #include <net/9p/9p.h>
#include <linux/parser.h> #include <linux/parser.h>
#include <net/9p/client.h> #include <net/9p/client.h>
...@@ -555,7 +556,7 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) ...@@ -555,7 +556,7 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
*/ */
static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req, static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
char *uidata, int in_hdrlen, int kern_buf) struct iov_iter *uidata, int in_hdrlen)
{ {
int err; int err;
int ecode; int ecode;
...@@ -591,18 +592,13 @@ static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req, ...@@ -591,18 +592,13 @@ static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
ename = &req->rc->sdata[req->rc->offset]; ename = &req->rc->sdata[req->rc->offset];
if (len > inline_len) { if (len > inline_len) {
/* We have error in external buffer */ /* We have error in external buffer */
if (kern_buf) { err = copy_from_iter(ename + inline_len,
memcpy(ename + inline_len, uidata, len - inline_len, uidata);
len - inline_len); if (err != len - inline_len) {
} else {
err = copy_from_user(ename + inline_len,
uidata, len - inline_len);
if (err) {
err = -EFAULT; err = -EFAULT;
goto out_err; goto out_err;
} }
} }
}
ename = NULL; ename = NULL;
err = p9pdu_readf(req->rc, c->proto_version, "s?d", err = p9pdu_readf(req->rc, c->proto_version, "s?d",
&ename, &ecode); &ename, &ecode);
...@@ -806,8 +802,8 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...) ...@@ -806,8 +802,8 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
* p9_client_zc_rpc - issue a request and wait for a response * p9_client_zc_rpc - issue a request and wait for a response
* @c: client session * @c: client session
* @type: type of request * @type: type of request
* @uidata: user bffer that should be ued for zero copy read * @uidata: destination for zero copy read
* @uodata: user buffer that shoud be user for zero copy write * @uodata: source for zero copy write
* @inlen: read buffer size * @inlen: read buffer size
* @olen: write buffer size * @olen: write buffer size
* @hdrlen: reader header size, This is the size of response protocol data * @hdrlen: reader header size, This is the size of response protocol data
...@@ -816,9 +812,10 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...) ...@@ -816,9 +812,10 @@ p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
* Returns request structure (which client must free using p9_free_req) * Returns request structure (which client must free using p9_free_req)
*/ */
static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type, static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
char *uidata, char *uodata, struct iov_iter *uidata,
struct iov_iter *uodata,
int inlen, int olen, int in_hdrlen, int inlen, int olen, int in_hdrlen,
int kern_buf, const char *fmt, ...) const char *fmt, ...)
{ {
va_list ap; va_list ap;
int sigpending, err; int sigpending, err;
...@@ -841,12 +838,8 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type, ...@@ -841,12 +838,8 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
} else } else
sigpending = 0; sigpending = 0;
/* If we are called with KERNEL_DS force kern_buf */
if (segment_eq(get_fs(), KERNEL_DS))
kern_buf = 1;
err = c->trans_mod->zc_request(c, req, uidata, uodata, err = c->trans_mod->zc_request(c, req, uidata, uodata,
inlen, olen, in_hdrlen, kern_buf); inlen, olen, in_hdrlen);
if (err < 0) { if (err < 0) {
if (err == -EIO) if (err == -EIO)
c->status = Disconnected; c->status = Disconnected;
...@@ -876,7 +869,7 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type, ...@@ -876,7 +869,7 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
if (err < 0) if (err < 0)
goto reterr; goto reterr;
err = p9_check_zc_errors(c, req, uidata, in_hdrlen, kern_buf); err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
trace_9p_client_res(c, type, req->rc->tag, err); trace_9p_client_res(c, type, req->rc->tag, err);
if (!err) if (!err)
return req; return req;
...@@ -1123,6 +1116,7 @@ struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, ...@@ -1123,6 +1116,7 @@ struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
fid = NULL; fid = NULL;
goto error; goto error;
} }
fid->uid = n_uname;
req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid, req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
afid ? afid->fid : P9_NOFID, uname, aname, n_uname); afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
...@@ -1541,20 +1535,19 @@ int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags) ...@@ -1541,20 +1535,19 @@ int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
EXPORT_SYMBOL(p9_client_unlinkat); EXPORT_SYMBOL(p9_client_unlinkat);
int int
p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
u32 count)
{ {
char *dataptr; struct p9_client *clnt = fid->clnt;
int kernel_buf = 0;
struct p9_req_t *req; struct p9_req_t *req;
struct p9_client *clnt; int total = 0;
int err, rsize, non_zc = 0;
p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n",
fid->fid, (unsigned long long) offset, count); fid->fid, (unsigned long long) offset, (int)iov_iter_count(to));
err = 0;
clnt = fid->clnt; while (iov_iter_count(to)) {
int count = iov_iter_count(to);
int rsize, non_zc = 0;
char *dataptr;
rsize = fid->iounit; rsize = fid->iounit;
if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
...@@ -1565,18 +1558,12 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, ...@@ -1565,18 +1558,12 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
/* Don't bother zerocopy for small IO (< 1024) */ /* Don't bother zerocopy for small IO (< 1024) */
if (clnt->trans_mod->zc_request && rsize > 1024) { if (clnt->trans_mod->zc_request && rsize > 1024) {
char *indata;
if (data) {
kernel_buf = 1;
indata = data;
} else
indata = (__force char *)udata;
/* /*
* response header len is 11 * response header len is 11
* PDU Header(7) + IO Size (4) * PDU Header(7) + IO Size (4)
*/ */
req = p9_client_zc_rpc(clnt, P9_TREAD, indata, NULL, rsize, 0, req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
11, kernel_buf, "dqd", fid->fid, 0, 11, "dqd", fid->fid,
offset, rsize); offset, rsize);
} else { } else {
non_zc = 1; non_zc = 1;
...@@ -1584,54 +1571,58 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, ...@@ -1584,54 +1571,58 @@ p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
rsize); rsize);
} }
if (IS_ERR(req)) { if (IS_ERR(req)) {
err = PTR_ERR(req); *err = PTR_ERR(req);
goto error; break;
} }
err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr); *err = p9pdu_readf(req->rc, clnt->proto_version,
if (err) { "D", &count, &dataptr);
if (*err) {
trace_9p_protocol_dump(clnt, req->rc); trace_9p_protocol_dump(clnt, req->rc);
goto free_and_error; p9_free_req(clnt, req);
break;
} }
p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count); p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
if (!count) {
p9_free_req(clnt, req);
break;
}
if (non_zc) { if (non_zc) {
if (data) { int n = copy_to_iter(dataptr, count, to);
memmove(data, dataptr, count); total += n;
} else { offset += n;
err = copy_to_user(udata, dataptr, count); if (n != count) {
if (err) { *err = -EFAULT;
err = -EFAULT; p9_free_req(clnt, req);
goto free_and_error; break;
}
} }
} else {
iov_iter_advance(to, count);
total += count;
offset += count;
} }
p9_free_req(clnt, req); p9_free_req(clnt, req);
return count; }
return total;
free_and_error:
p9_free_req(clnt, req);
error:
return err;
} }
EXPORT_SYMBOL(p9_client_read); EXPORT_SYMBOL(p9_client_read);
int int
p9_client_write(struct p9_fid *fid, char *data, const char __user *udata, p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
u64 offset, u32 count)
{ {
int err, rsize; struct p9_client *clnt = fid->clnt;
int kernel_buf = 0;
struct p9_client *clnt;
struct p9_req_t *req; struct p9_req_t *req;
int total = 0;
p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n", p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
fid->fid, (unsigned long long) offset, count); fid->fid, (unsigned long long) offset,
err = 0; iov_iter_count(from));
clnt = fid->clnt;
rsize = fid->iounit; while (iov_iter_count(from)) {
int count = iov_iter_count(from);
int rsize = fid->iounit;
if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
rsize = clnt->msize - P9_IOHDRSZ; rsize = clnt->msize - P9_IOHDRSZ;
...@@ -1640,43 +1631,32 @@ p9_client_write(struct p9_fid *fid, char *data, const char __user *udata, ...@@ -1640,43 +1631,32 @@ p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
/* Don't bother zerocopy for small IO (< 1024) */ /* Don't bother zerocopy for small IO (< 1024) */
if (clnt->trans_mod->zc_request && rsize > 1024) { if (clnt->trans_mod->zc_request && rsize > 1024) {
char *odata; req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
if (data) { rsize, P9_ZC_HDR_SZ, "dqd",
kernel_buf = 1;
odata = data;
} else
odata = (char *)udata;
req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, odata, 0, rsize,
P9_ZC_HDR_SZ, kernel_buf, "dqd",
fid->fid, offset, rsize); fid->fid, offset, rsize);
} else { } else {
if (data) req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset, rsize, from);
offset, rsize, data);
else
req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid,
offset, rsize, udata);
} }
if (IS_ERR(req)) { if (IS_ERR(req)) {
err = PTR_ERR(req); *err = PTR_ERR(req);
goto error; break;
} }
err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count); *err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
if (err) { if (*err) {
trace_9p_protocol_dump(clnt, req->rc); trace_9p_protocol_dump(clnt, req->rc);
goto free_and_error; p9_free_req(clnt, req);
} }
p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count); p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
p9_free_req(clnt, req); p9_free_req(clnt, req);
return count; iov_iter_advance(from, count);
total += count;
free_and_error: offset += count;
p9_free_req(clnt, req); }
error: return total;
return err;
} }
EXPORT_SYMBOL(p9_client_write); EXPORT_SYMBOL(p9_client_write);
...@@ -2068,6 +2048,10 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset) ...@@ -2068,6 +2048,10 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
struct p9_client *clnt; struct p9_client *clnt;
struct p9_req_t *req; struct p9_req_t *req;
char *dataptr; char *dataptr;
struct kvec kv = {.iov_base = data, .iov_len = count};
struct iov_iter to;
iov_iter_kvec(&to, READ | ITER_KVEC, &kv, 1, count);
p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n", p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
fid->fid, (unsigned long long) offset, count); fid->fid, (unsigned long long) offset, count);
...@@ -2088,8 +2072,8 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset) ...@@ -2088,8 +2072,8 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
* response header len is 11 * response header len is 11
* PDU Header(7) + IO Size (4) * PDU Header(7) + IO Size (4)
*/ */
req = p9_client_zc_rpc(clnt, P9_TREADDIR, data, NULL, rsize, 0, req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
11, 1, "dqd", fid->fid, offset, rsize); 11, "dqd", fid->fid, offset, rsize);
} else { } else {
non_zc = 1; non_zc = 1;
req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid, req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/stddef.h> #include <linux/stddef.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/uio.h>
#include <net/9p/9p.h> #include <net/9p/9p.h>
#include <net/9p/client.h> #include <net/9p/client.h>
#include "protocol.h" #include "protocol.h"
...@@ -69,10 +70,11 @@ static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) ...@@ -69,10 +70,11 @@ static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
} }
static size_t static size_t
pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size) pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
{ {
size_t len = min(pdu->capacity - pdu->size, size); size_t len = min(pdu->capacity - pdu->size, size);
if (copy_from_user(&pdu->sdata[pdu->size], udata, len)) struct iov_iter i = *from;
if (copy_from_iter(&pdu->sdata[pdu->size], len, &i) != len)
len = 0; len = 0;
pdu->size += len; pdu->size += len;
...@@ -437,23 +439,13 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...@@ -437,23 +439,13 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
stbuf->extension, stbuf->n_uid, stbuf->extension, stbuf->n_uid,
stbuf->n_gid, stbuf->n_muid); stbuf->n_gid, stbuf->n_muid);
} break; } break;
case 'D':{ case 'V':{
uint32_t count = va_arg(ap, uint32_t);
const void *data = va_arg(ap, const void *);
errcode = p9pdu_writef(pdu, proto_version, "d",
count);
if (!errcode && pdu_write(pdu, data, count))
errcode = -EFAULT;
}
break;
case 'U':{
int32_t count = va_arg(ap, int32_t); int32_t count = va_arg(ap, int32_t);
const char __user *udata = struct iov_iter *from =
va_arg(ap, const void __user *); va_arg(ap, struct iov_iter *);
errcode = p9pdu_writef(pdu, proto_version, "d", errcode = p9pdu_writef(pdu, proto_version, "d",
count); count);
if (!errcode && pdu_write_u(pdu, udata, count)) if (!errcode && pdu_write_u(pdu, from, count))
errcode = -EFAULT; errcode = -EFAULT;
} }
break; break;
......
...@@ -12,12 +12,8 @@ ...@@ -12,12 +12,8 @@
* *
*/ */
#include <linux/slab.h> #include <linux/mm.h>
#include <linux/module.h> #include <linux/module.h>
#include <net/9p/9p.h>
#include <net/9p/client.h>
#include <linux/scatterlist.h>
#include "trans_common.h"
/** /**
* p9_release_req_pages - Release pages after the transaction. * p9_release_req_pages - Release pages after the transaction.
...@@ -31,39 +27,3 @@ void p9_release_pages(struct page **pages, int nr_pages) ...@@ -31,39 +27,3 @@ void p9_release_pages(struct page **pages, int nr_pages)
put_page(pages[i]); put_page(pages[i]);
} }
EXPORT_SYMBOL(p9_release_pages); EXPORT_SYMBOL(p9_release_pages);
/**
* p9_nr_pages - Return number of pages needed to accommodate the payload.
*/
int p9_nr_pages(char *data, int len)
{
unsigned long start_page, end_page;
start_page = (unsigned long)data >> PAGE_SHIFT;
end_page = ((unsigned long)data + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
return end_page - start_page;
}
EXPORT_SYMBOL(p9_nr_pages);
/**
* payload_gup - Translates user buffer into kernel pages and
* pins them either for read/write through get_user_pages_fast().
* @req: Request to be sent to server.
* @pdata_off: data offset into the first page after translation (gup).
* @pdata_len: Total length of the IO. gup may not return requested # of pages.
* @nr_pages: number of pages to accommodate the payload
* @rw: Indicates if the pages are for read or write.
*/
int p9_payload_gup(char *data, int *nr_pages, struct page **pages, int write)
{
int nr_mapped_pages;
nr_mapped_pages = get_user_pages_fast((unsigned long)data,
*nr_pages, write, pages);
if (nr_mapped_pages <= 0)
return nr_mapped_pages;
*nr_pages = nr_mapped_pages;
return 0;
}
EXPORT_SYMBOL(p9_payload_gup);
...@@ -13,5 +13,3 @@ ...@@ -13,5 +13,3 @@
*/ */
void p9_release_pages(struct page **, int); void p9_release_pages(struct page **, int);
int p9_payload_gup(char *, int *, struct page **, int);
int p9_nr_pages(char *, int);
...@@ -217,15 +217,15 @@ static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req) ...@@ -217,15 +217,15 @@ static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
* @start: which segment of the sg_list to start at * @start: which segment of the sg_list to start at
* @pdata: a list of pages to add into sg. * @pdata: a list of pages to add into sg.
* @nr_pages: number of pages to pack into the scatter/gather list * @nr_pages: number of pages to pack into the scatter/gather list
* @data: data to pack into scatter/gather list * @offs: amount of data in the beginning of first page _not_ to pack
* @count: amount of data to pack into the scatter/gather list * @count: amount of data to pack into the scatter/gather list
*/ */
static int static int
pack_sg_list_p(struct scatterlist *sg, int start, int limit, pack_sg_list_p(struct scatterlist *sg, int start, int limit,
struct page **pdata, int nr_pages, char *data, int count) struct page **pdata, int nr_pages, size_t offs, int count)
{ {
int i = 0, s; int i = 0, s;
int data_off; int data_off = offs;
int index = start; int index = start;
BUG_ON(nr_pages > (limit - start)); BUG_ON(nr_pages > (limit - start));
...@@ -233,16 +233,14 @@ pack_sg_list_p(struct scatterlist *sg, int start, int limit, ...@@ -233,16 +233,14 @@ pack_sg_list_p(struct scatterlist *sg, int start, int limit,
* if the first page doesn't start at * if the first page doesn't start at
* page boundary find the offset * page boundary find the offset
*/ */
data_off = offset_in_page(data);
while (nr_pages) { while (nr_pages) {
s = rest_of_page(data); s = PAGE_SIZE - data_off;
if (s > count) if (s > count)
s = count; s = count;
/* Make sure we don't terminate early. */ /* Make sure we don't terminate early. */
sg_unmark_end(&sg[index]); sg_unmark_end(&sg[index]);
sg_set_page(&sg[index++], pdata[i++], s, data_off); sg_set_page(&sg[index++], pdata[i++], s, data_off);
data_off = 0; data_off = 0;
data += s;
count -= s; count -= s;
nr_pages--; nr_pages--;
} }
...@@ -314,11 +312,20 @@ p9_virtio_request(struct p9_client *client, struct p9_req_t *req) ...@@ -314,11 +312,20 @@ p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
} }
static int p9_get_mapped_pages(struct virtio_chan *chan, static int p9_get_mapped_pages(struct virtio_chan *chan,
struct page **pages, char *data, struct page ***pages,
int nr_pages, int write, int kern_buf) struct iov_iter *data,
int count,
size_t *offs,
int *need_drop)
{ {
int nr_pages;
int err; int err;
if (!kern_buf) {
if (!iov_iter_count(data))
return 0;
if (!(data->type & ITER_KVEC)) {
int n;
/* /*
* We allow only p9_max_pages pinned. We wait for the * We allow only p9_max_pages pinned. We wait for the
* Other zc request to finish here * Other zc request to finish here
...@@ -329,26 +336,49 @@ static int p9_get_mapped_pages(struct virtio_chan *chan, ...@@ -329,26 +336,49 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
if (err == -ERESTARTSYS) if (err == -ERESTARTSYS)
return err; return err;
} }
err = p9_payload_gup(data, &nr_pages, pages, write); n = iov_iter_get_pages_alloc(data, pages, count, offs);
if (err < 0) if (n < 0)
return err; return n;
*need_drop = 1;
nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
atomic_add(nr_pages, &vp_pinned); atomic_add(nr_pages, &vp_pinned);
return n;
} else { } else {
/* kernel buffer, no need to pin pages */ /* kernel buffer, no need to pin pages */
int s, index = 0; int index;
int count = nr_pages; size_t len;
while (nr_pages) { void *p;
s = rest_of_page(data);
if (is_vmalloc_addr(data)) /* we'd already checked that it's non-empty */
pages[index++] = vmalloc_to_page(data); while (1) {
len = iov_iter_single_seg_count(data);
if (likely(len)) {
p = data->kvec->iov_base + data->iov_offset;
break;
}
iov_iter_advance(data, 0);
}
if (len > count)
len = count;
nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
(unsigned long)p / PAGE_SIZE;
*pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
if (!*pages)
return -ENOMEM;
*need_drop = 0;
p -= (*offs = (unsigned long)p % PAGE_SIZE);
for (index = 0; index < nr_pages; index++) {
if (is_vmalloc_addr(p))
(*pages)[index] = vmalloc_to_page(p);
else else
pages[index++] = kmap_to_page(data); (*pages)[index] = kmap_to_page(p);
data += s; p += PAGE_SIZE;
nr_pages--;
} }
nr_pages = count; return len;
} }
return nr_pages;
} }
/** /**
...@@ -364,8 +394,8 @@ static int p9_get_mapped_pages(struct virtio_chan *chan, ...@@ -364,8 +394,8 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
*/ */
static int static int
p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
char *uidata, char *uodata, int inlen, struct iov_iter *uidata, struct iov_iter *uodata,
int outlen, int in_hdr_len, int kern_buf) int inlen, int outlen, int in_hdr_len)
{ {
int in, out, err, out_sgs, in_sgs; int in, out, err, out_sgs, in_sgs;
unsigned long flags; unsigned long flags;
...@@ -373,41 +403,32 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, ...@@ -373,41 +403,32 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
struct page **in_pages = NULL, **out_pages = NULL; struct page **in_pages = NULL, **out_pages = NULL;
struct virtio_chan *chan = client->trans; struct virtio_chan *chan = client->trans;
struct scatterlist *sgs[4]; struct scatterlist *sgs[4];
size_t offs;
int need_drop = 0;
p9_debug(P9_DEBUG_TRANS, "virtio request\n"); p9_debug(P9_DEBUG_TRANS, "virtio request\n");
if (uodata) { if (uodata) {
out_nr_pages = p9_nr_pages(uodata, outlen); int n = p9_get_mapped_pages(chan, &out_pages, uodata,
out_pages = kmalloc(sizeof(struct page *) * out_nr_pages, outlen, &offs, &need_drop);
GFP_NOFS); if (n < 0)
if (!out_pages) { return n;
err = -ENOMEM; out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
goto err_out; if (n != outlen) {
} __le32 v = cpu_to_le32(n);
out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata, memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
out_nr_pages, 0, kern_buf); outlen = n;
if (out_nr_pages < 0) { }
err = out_nr_pages; } else if (uidata) {
kfree(out_pages); int n = p9_get_mapped_pages(chan, &in_pages, uidata,
out_pages = NULL; inlen, &offs, &need_drop);
goto err_out; if (n < 0)
} return n;
} in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
if (uidata) { if (n != inlen) {
in_nr_pages = p9_nr_pages(uidata, inlen); __le32 v = cpu_to_le32(n);
in_pages = kmalloc(sizeof(struct page *) * in_nr_pages, memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
GFP_NOFS); inlen = n;
if (!in_pages) {
err = -ENOMEM;
goto err_out;
}
in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
in_nr_pages, 1, kern_buf);
if (in_nr_pages < 0) {
err = in_nr_pages;
kfree(in_pages);
in_pages = NULL;
goto err_out;
} }
} }
req->status = REQ_STATUS_SENT; req->status = REQ_STATUS_SENT;
...@@ -426,7 +447,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, ...@@ -426,7 +447,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
if (out_pages) { if (out_pages) {
sgs[out_sgs++] = chan->sg + out; sgs[out_sgs++] = chan->sg + out;
out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM, out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
out_pages, out_nr_pages, uodata, outlen); out_pages, out_nr_pages, offs, outlen);
} }
/* /*
...@@ -444,7 +465,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, ...@@ -444,7 +465,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
if (in_pages) { if (in_pages) {
sgs[out_sgs + in_sgs++] = chan->sg + out + in; sgs[out_sgs + in_sgs++] = chan->sg + out + in;
in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM, in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
in_pages, in_nr_pages, uidata, inlen); in_pages, in_nr_pages, offs, inlen);
} }
BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs)); BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
...@@ -478,7 +499,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, ...@@ -478,7 +499,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
* Non kernel buffers are pinned, unpin them * Non kernel buffers are pinned, unpin them
*/ */
err_out: err_out:
if (!kern_buf) { if (need_drop) {
if (in_pages) { if (in_pages) {
p9_release_pages(in_pages, in_nr_pages); p9_release_pages(in_pages, in_nr_pages);
atomic_sub(in_nr_pages, &vp_pinned); atomic_sub(in_nr_pages, &vp_pinned);
......
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