Commit 03d254ed authored by Yan, Zheng's avatar Yan, Zheng Committed by Sage Weil

ceph: apply write checks in ceph_aio_write

copy write checks in __generic_file_aio_write to ceph_aio_write.
To make these checks cover sync write path.
Signed-off-by: default avatarYan, Zheng <zheng.z.yan@intel.com>
Reviewed-by: default avatarAlex Elder <elder@inktank.com>
parent 37505d57
...@@ -486,7 +486,7 @@ static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe) ...@@ -486,7 +486,7 @@ static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
* objects, rollback on failure, etc.) * objects, rollback on failure, etc.)
*/ */
static ssize_t ceph_sync_write(struct file *file, const char __user *data, static ssize_t ceph_sync_write(struct file *file, const char __user *data,
size_t left, loff_t *offset) size_t left, loff_t pos, loff_t *ppos)
{ {
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
struct ceph_inode_info *ci = ceph_inode(inode); struct ceph_inode_info *ci = ceph_inode(inode);
...@@ -497,7 +497,6 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data, ...@@ -497,7 +497,6 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data,
int num_ops = 1; int num_ops = 1;
struct page **pages; struct page **pages;
int num_pages; int num_pages;
long long unsigned pos;
u64 len; u64 len;
int written = 0; int written = 0;
int flags; int flags;
...@@ -511,14 +510,9 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data, ...@@ -511,14 +510,9 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data,
if (ceph_snap(file_inode(file)) != CEPH_NOSNAP) if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
return -EROFS; return -EROFS;
dout("sync_write on file %p %lld~%u %s\n", file, *offset, dout("sync_write on file %p %lld~%u %s\n", file, pos,
(unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : ""); (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
if (file->f_flags & O_APPEND)
pos = i_size_read(inode);
else
pos = *offset;
ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left); ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -617,7 +611,7 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data, ...@@ -617,7 +611,7 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data,
goto more; goto more;
ret = written; ret = written;
*offset = pos; *ppos = pos;
if (pos > i_size_read(inode)) if (pos > i_size_read(inode))
check_caps = ceph_inode_set_size(inode, pos); check_caps = ceph_inode_set_size(inode, pos);
if (check_caps) if (check_caps)
...@@ -714,51 +708,75 @@ static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov, ...@@ -714,51 +708,75 @@ static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
struct ceph_inode_info *ci = ceph_inode(inode); struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_osd_client *osdc = struct ceph_osd_client *osdc =
&ceph_sb_to_client(inode->i_sb)->client->osdc; &ceph_sb_to_client(inode->i_sb)->client->osdc;
loff_t endoff = pos + iov->iov_len; ssize_t count, written = 0;
int want, got = 0; int err, want, got;
int ret, err; bool hold_mutex;
if (ceph_snap(inode) != CEPH_NOSNAP) if (ceph_snap(inode) != CEPH_NOSNAP)
return -EROFS; return -EROFS;
sb_start_write(inode->i_sb); sb_start_write(inode->i_sb);
mutex_lock(&inode->i_mutex);
hold_mutex = true;
err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
if (err)
goto out;
/* We can write back this queue in page reclaim */
current->backing_dev_info = file->f_mapping->backing_dev_info;
err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
if (err)
goto out;
if (count == 0)
goto out;
err = file_remove_suid(file);
if (err)
goto out;
err = file_update_time(file);
if (err)
goto out;
retry_snap: retry_snap:
if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) { if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
ret = -ENOSPC; err = -ENOSPC;
goto out; goto out;
} }
mutex_lock(&inode->i_mutex);
dout("aio_write %p %llx.%llx %llu~%u getting caps. i_size %llu\n", dout("aio_write %p %llx.%llx %llu~%ld getting caps. i_size %llu\n",
inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len, inode, ceph_vinop(inode), pos, count, inode->i_size);
inode->i_size);
if (fi->fmode & CEPH_FILE_MODE_LAZY) if (fi->fmode & CEPH_FILE_MODE_LAZY)
want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
else else
want = CEPH_CAP_FILE_BUFFER; want = CEPH_CAP_FILE_BUFFER;
ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff); got = 0;
if (ret < 0) { err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
mutex_unlock(&inode->i_mutex); if (err < 0)
goto out; goto out;
}
dout("aio_write %p %llx.%llx %llu~%u got cap refs on %s\n", dout("aio_write %p %llx.%llx %llu~%ld got cap refs on %s\n",
inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len, inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
ceph_cap_string(got));
if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 || if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
(iocb->ki_filp->f_flags & O_DIRECT) || (iocb->ki_filp->f_flags & O_DIRECT) ||
(inode->i_sb->s_flags & MS_SYNCHRONOUS) || (inode->i_sb->s_flags & MS_SYNCHRONOUS) ||
(fi->flags & CEPH_F_SYNC)) { (fi->flags & CEPH_F_SYNC)) {
mutex_unlock(&inode->i_mutex); mutex_unlock(&inode->i_mutex);
ret = ceph_sync_write(file, iov->iov_base, iov->iov_len, written = ceph_sync_write(file, iov->iov_base, count,
&iocb->ki_pos); pos, &iocb->ki_pos);
} else { } else {
ret = __generic_file_aio_write(iocb, iov, nr_segs, written = generic_file_buffered_write(iocb, iov, nr_segs,
&iocb->ki_pos); pos, &iocb->ki_pos,
count, 0);
mutex_unlock(&inode->i_mutex); mutex_unlock(&inode->i_mutex);
} }
hold_mutex = false;
if (ret >= 0) { if (written >= 0) {
int dirty; int dirty;
spin_lock(&ci->i_ceph_lock); spin_lock(&ci->i_ceph_lock);
dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR); dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
...@@ -772,22 +790,28 @@ static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov, ...@@ -772,22 +790,28 @@ static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
ceph_cap_string(got)); ceph_cap_string(got));
ceph_put_cap_refs(ci, got); ceph_put_cap_refs(ci, got);
if (ret >= 0 && if (written >= 0 &&
((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) || ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) { ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
err = vfs_fsync_range(file, pos, pos + ret - 1, 1); err = vfs_fsync_range(file, pos, pos + written - 1, 1);
if (err < 0) if (err < 0)
ret = err; written = err;
} }
out:
if (ret == -EOLDSNAPC) { if (written == -EOLDSNAPC) {
dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n", dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n",
inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len); inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len);
mutex_lock(&inode->i_mutex);
hold_mutex = true;
goto retry_snap; goto retry_snap;
} }
out:
if (hold_mutex)
mutex_unlock(&inode->i_mutex);
sb_end_write(inode->i_sb); sb_end_write(inode->i_sb);
current->backing_dev_info = NULL;
return ret; return written ? written : err;
} }
/* /*
......
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