Skip to content

Commit 19dfc1f

Browse files
author
Al Viro
committed
cifs: fix the race in cifs_writev()
O_APPEND handling there hadn't been completely fixed by Pavel's patch; it checks the right value, but it's racy - we can't really do that until i_mutex has been taken. Fix by switching to __generic_file_aio_write() (open-coding generic_file_aio_write(), actually) and pulling mutex_lock() above inode_size_read(). Cc: [email protected] Signed-off-by: Al Viro <[email protected]>
1 parent eab8723 commit 19dfc1f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

fs/cifs/file.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,19 +2579,32 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,
25792579
struct cifsInodeInfo *cinode = CIFS_I(inode);
25802580
struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
25812581
ssize_t rc = -EACCES;
2582-
loff_t lock_pos = pos;
2582+
loff_t lock_pos = iocb->ki_pos;
25832583

2584-
if (file->f_flags & O_APPEND)
2585-
lock_pos = i_size_read(inode);
25862584
/*
25872585
* We need to hold the sem to be sure nobody modifies lock list
25882586
* with a brlock that prevents writing.
25892587
*/
25902588
down_read(&cinode->lock_sem);
2589+
mutex_lock(&inode->i_mutex);
2590+
if (file->f_flags & O_APPEND)
2591+
lock_pos = i_size_read(inode);
25912592
if (!cifs_find_lock_conflict(cfile, lock_pos, iov_length(iov, nr_segs),
25922593
server->vals->exclusive_lock_type, NULL,
2593-
CIFS_WRITE_OP))
2594-
rc = generic_file_aio_write(iocb, iov, nr_segs, pos);
2594+
CIFS_WRITE_OP)) {
2595+
rc = __generic_file_aio_write(iocb, iov, nr_segs);
2596+
mutex_unlock(&inode->i_mutex);
2597+
2598+
if (rc > 0) {
2599+
ssize_t err;
2600+
2601+
err = generic_write_sync(file, iocb->ki_pos - rc, rc);
2602+
if (rc < 0)
2603+
rc = err;
2604+
}
2605+
} else {
2606+
mutex_unlock(&inode->i_mutex);
2607+
}
25952608
up_read(&cinode->lock_sem);
25962609
return rc;
25972610
}

0 commit comments

Comments
 (0)