提交 4344d07f 编写于 作者: T Tejun Heo 提交者: Jens Axboe

ide-tape: simplify read/write functions

Impact: cleanup

idetape_chrdev_read/write() functions are unnecessarily complex when
everything can be handled in a single loop.  Collapse
idetape_add_chrdev_read/write_request() into the rw functions and
simplify the implementation.
Signed-off-by: NTejun Heo <tj@kernel.org>
上级 71294cf9
...@@ -953,14 +953,6 @@ static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) ...@@ -953,14 +953,6 @@ static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
pc->flags |= PC_FLAG_WAIT_FOR_DSC; pc->flags |= PC_FLAG_WAIT_FOR_DSC;
} }
/* Queue up a character device originated write request. */
static int idetape_add_chrdev_write_request(ide_drive_t *drive, int size)
{
debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, size);
}
static void ide_tape_flush_merge_buffer(ide_drive_t *drive) static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
{ {
idetape_tape_t *tape = drive->driver_data; idetape_tape_t *tape = drive->driver_data;
...@@ -974,7 +966,7 @@ static void ide_tape_flush_merge_buffer(ide_drive_t *drive) ...@@ -974,7 +966,7 @@ static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
size_t aligned = roundup(tape->valid, tape->blk_size); size_t aligned = roundup(tape->valid, tape->blk_size);
memset(tape->cur, 0, aligned - tape->valid); memset(tape->cur, 0, aligned - tape->valid);
idetape_add_chrdev_write_request(drive, aligned); idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
kfree(tape->buf); kfree(tape->buf);
tape->buf = NULL; tape->buf = NULL;
} }
...@@ -1031,20 +1023,6 @@ static int idetape_init_rw(ide_drive_t *drive, int dir) ...@@ -1031,20 +1023,6 @@ static int idetape_init_rw(ide_drive_t *drive, int dir)
return 0; return 0;
} }
/* called from idetape_chrdev_read() to service a chrdev read request. */
static int idetape_add_chrdev_read_request(ide_drive_t *drive, int size)
{
debug_log(DBG_PROCS, "Enter %s, %d bytes\n", __func__, size);
/* If we are at a filemark, return a read length of 0 */
if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
return 0;
idetape_init_rw(drive, IDETAPE_DIR_READ);
return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, size);
}
static void idetape_pad_zeros(ide_drive_t *drive, int bcount) static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
{ {
idetape_tape_t *tape = drive->driver_data; idetape_tape_t *tape = drive->driver_data;
...@@ -1184,8 +1162,9 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, ...@@ -1184,8 +1162,9 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
{ {
struct ide_tape_obj *tape = file->private_data; struct ide_tape_obj *tape = file->private_data;
ide_drive_t *drive = tape->drive; ide_drive_t *drive = tape->drive;
ssize_t bytes_read, temp, actually_read = 0, rc; size_t done = 0;
ssize_t ret = 0; ssize_t ret = 0;
int rc;
debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
...@@ -1195,52 +1174,43 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, ...@@ -1195,52 +1174,43 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
(count % tape->blk_size) == 0) (count % tape->blk_size) == 0)
tape->user_bs_factor = count / tape->blk_size; tape->user_bs_factor = count / tape->blk_size;
} }
rc = idetape_init_rw(drive, IDETAPE_DIR_READ); rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
if (rc < 0) if (rc < 0)
return rc; return rc;
if (count == 0)
return (0); while (done < count) {
if (tape->valid) { size_t todo;
actually_read = min_t(unsigned int, tape->valid, count);
if (copy_to_user(buf, tape->cur, actually_read)) /* refill if staging buffer is empty */
ret = -EFAULT; if (!tape->valid) {
buf += actually_read; /* If we are at a filemark, nothing more to read */
count -= actually_read; if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
tape->cur += actually_read; break;
tape->valid -= actually_read; /* read */
} if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
while (count >= tape->buffer_size) { tape->buffer_size) <= 0)
bytes_read = idetape_add_chrdev_read_request(drive, break;
tape->buffer_size); }
if (bytes_read <= 0)
goto finish; /* copy out */
if (copy_to_user(buf, tape->cur, bytes_read)) todo = min_t(size_t, count - done, tape->valid);
ret = -EFAULT; if (copy_to_user(buf + done, tape->cur, todo))
buf += bytes_read;
count -= bytes_read;
actually_read += bytes_read;
}
if (count) {
bytes_read = idetape_add_chrdev_read_request(drive,
tape->buffer_size);
if (bytes_read <= 0)
goto finish;
temp = min((unsigned long)count, (unsigned long)bytes_read);
if (copy_to_user(buf, tape->cur, temp))
ret = -EFAULT; ret = -EFAULT;
actually_read += temp;
tape->cur += temp; tape->cur += todo;
tape->valid -= temp; tape->valid -= todo;
done += todo;
} }
finish:
if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) { if (!done && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
idetape_space_over_filemarks(drive, MTFSF, 1); idetape_space_over_filemarks(drive, MTFSF, 1);
return 0; return 0;
} }
return ret ? ret : actually_read; return ret ? ret : done;
} }
static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
...@@ -1248,7 +1218,7 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, ...@@ -1248,7 +1218,7 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
{ {
struct ide_tape_obj *tape = file->private_data; struct ide_tape_obj *tape = file->private_data;
ide_drive_t *drive = tape->drive; ide_drive_t *drive = tape->drive;
ssize_t actually_written = 0; size_t done = 0;
ssize_t ret = 0; ssize_t ret = 0;
int rc; int rc;
...@@ -1262,47 +1232,28 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, ...@@ -1262,47 +1232,28 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE); rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
if (rc < 0) if (rc < 0)
return rc; return rc;
if (count == 0)
return (0); while (done < count) {
if (tape->valid < tape->buffer_size) { size_t todo;
actually_written = min_t(unsigned int,
tape->buffer_size - tape->valid, /* flush if staging buffer is full */
count); if (tape->valid == tape->buffer_size &&
if (copy_from_user(tape->cur, buf, actually_written)) idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
ret = -EFAULT; tape->buffer_size) <= 0)
buf += actually_written; return rc;
count -= actually_written;
tape->cur += actually_written; /* copy in */
tape->valid += actually_written; todo = min_t(size_t, count - done,
tape->buffer_size - tape->valid);
if (tape->valid == tape->buffer_size) { if (copy_from_user(tape->cur, buf + done, todo))
ssize_t retval;
retval = idetape_add_chrdev_write_request(drive,
tape->buffer_size);
if (retval <= 0)
return (retval);
}
}
while (count >= tape->buffer_size) {
ssize_t retval;
if (copy_from_user(tape->cur, buf, tape->buffer_size))
ret = -EFAULT;
buf += tape->buffer_size;
count -= tape->buffer_size;
retval = idetape_add_chrdev_write_request(drive,
tape->buffer_size);
actually_written += tape->buffer_size;
if (retval <= 0)
return (retval);
}
if (count) {
actually_written += count;
if (copy_from_user(tape->cur, buf, count))
ret = -EFAULT; ret = -EFAULT;
tape->cur += count;
tape->valid += count; tape->cur += todo;
tape->valid += todo;
done += todo;
} }
return ret ? ret : actually_written;
return ret ? ret : done;
} }
static int idetape_write_filemark(ide_drive_t *drive) static int idetape_write_filemark(ide_drive_t *drive)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册