xfs: prevent corruption due to overlapping AIO DIO V2
cover xfs: prevent corruption due to overlapping AIO DIO V2
This series aims to serialise unaligned direct IOs to an inode to avoid corruption caused by sub-block zeroing races. The previous approaches at the direct IO layer fail because for !DIO_LOCKING filesystems like XFS, there is no way we can track and serialise all the direct IOs to a given inode in a race free manner. While we can track them, we cannot close the races between mapping blocks and tracked IO completion occuring before subsequent tracking lookups without adding some kind of locking to the DIO layer. Hence for !DIO_LOCKING users, unaligned direct IO needs to be serialised at a higher layer.
Because the xfs_file_aio_write() path is so twisted and difficult to follow, adding new locking cases to the code is difficult to verify that it is correct in all cases. Hence the series starts by cleaning up the code and splitting apart the direct IO and buffered IO paths before adding the unaligned direct IO detection and serialisation.
The first patch fixes a sync write error handling bug - we should consider pushing that to .38. The next patches factor code that is common to write and splice into helpers. The direct and buffered IO paths are then separated out and the common write checks and bounds limiting is factored out into a helper.
Finally, the serialisation of unaligned direct IOs is added by a big-hammer approach. That is, we take the i_mutex and XFS_IOLOCK_EXCL and hold them across the unaligned IO submission. This means that unaligned direct IO submission is serialised, and non-AIO DIO is serialised completely.
For unaligned AIO DIO, this would only serialise the submission of the DIO, leaving the sub-block zeroing races open for unaligned writes into unwritten extents. To avoid this problem, we use xfs_ioend_wait() to ensure all AIO writes have completed before we submit the unaligned write. We do this wait holding the i_mutex so we serialise against other unaligned AIO as there is no need to serialise against aligned DIO.
Version 2:
- fix initial sync write error return fixup
- add new patch to abstract locking from read/write path and remove the need for the need_i_mutex variable.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 1/8 xfs: ensure sync write errors are returned
From: Dave Chinner <[email protected]>
xfs_file_aio_write() only returns the error from synchronous flushing of the data and inode if error == 0. At the point where error is being checked, it is guaranteed to be > 0. Therefore any errors returned by the data or fsync flush will never be returned. Fix the checks so we overwrite the current error once and only if an error really occurred.
--- fs/xfs/linux-2.6/xfs_file.c | 51 +++++++++++++++++++++---------------------- 1 files changed, 25 insertions(+), 26 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +25 -26
@@ -574,7 +574,7 @@ xfs_file_aio_write(struct inode *inode = mapping->host;struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -ssize_t ret =0 , error =0 ; +ssize_t ret =0 ;int ioflags =0 ;xfs_fsize_t isize, new_size;int iolock;
@@ -590,9 +590,9 @@ xfs_file_aio_write(if (file->f_mode &FMODE_NOCMTIME ) ioflags |=IO_INVIS ; - error =generic_segment_checks (iovp, &nr_segs, &ocount,VERIFY_READ ); -if (error) -return error; + ret =generic_segment_checks (iovp, &nr_segs, &ocount,VERIFY_READ ); +if (ret) +return ret; count = ocount;if (count ==0 )
@@ -616,9 +616,9 @@ relock:xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); start: - error = -generic_write_checks (file, &pos, &count, + ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode)); -if (error) { +if (ret) {xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); goto out_unlock_mutex; }
@@ -660,8 +660,8 @@ start:*/ if (pos > ip->i_size) { - error =xfs_zero_eof (ip, pos, ip->i_size); -if (error) { + ret = -xfs_zero_eof (ip, pos, ip->i_size); +if (ret) {xfs_iunlock (ip,XFS_ILOCK_EXCL ); goto out_unlock_internal; }
@@ -674,8 +674,8 @@ start:* by root. This keeps people from modifying setuid and * setgid binaries. */ - error = -file_remove_suid (file); -if (unlikely (error)) + ret =file_remove_suid (file); +if (unlikely (ret)) goto out_unlock_internal;/* We can write back this queue in page reclaim */
@@ -684,10 +684,10 @@ start:if ((ioflags &IO_ISDIRECT )) {if (mapping->nrpages) {WARN_ON (need_i_mutex ==0 ); - error =xfs_flushinval_pages (ip, + ret = -xfs_flushinval_pages (ip, (pos &PAGE_CACHE_MASK ),-1 ,FI_REMAPF_LOCKED ); -if (error) +if (ret) goto out_unlock_internal; }
@@ -720,24 +720,24 @@ start: } }else {int enospc =0 ; -ssize_t ret2 =0 ; write_retry:trace_xfs_file_buffered_write (ip, count, iocb->ki_pos, ioflags); - ret2 =generic_file_buffered_write (iocb, iovp, nr_segs, + ret =generic_file_buffered_write (iocb, iovp, nr_segs, pos, &iocb->ki_pos, count, ret);/* * if we just got an ENOSPC, flush the inode now we * aren't holding any page locks and retry *once* */ -if (ret2 == -ENOSPC && !enospc) { - error =xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); -if (error) +if (ret == -ENOSPC && !enospc) { +ssize_t ret2 =xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); +if (ret2) { + ret = ret2; goto out_unlock_internal; + } enospc =1 ; goto write_retry; } - ret = ret2; } current->backing_dev_info =NULL ;
@@ -753,7 +753,6 @@ write_retry:xfs_iunlock (ip,XFS_ILOCK_EXCL ); } - error = -ret;if (ret <=0 ) goto out_unlock_internal;
@@ -762,23 +761,23 @@ write_retry:/* Handle various SYNC-type writes */ if ((file->f_flags &O_DSYNC ) ||IS_SYNC (inode)) {loff_t end = pos + ret -1 ; -int error2; +int error, error2;xfs_iunlock (ip, iolock);if (need_i_mutex)mutex_unlock (&inode->i_mutex); - error2 =filemap_write_and_wait_range (mapping, pos, end); -if (!error) - error = error2; + error =filemap_write_and_wait_range (mapping, pos, end);if (need_i_mutex)mutex_lock (&inode->i_mutex);xfs_ilock (ip, iolock); error2 = -xfs_file_fsync (file, (file->f_flags & __O_SYNC) ?0 :1 ); -if (!error) - error = error2; +if (error) + ret = error; +else if (error2) + ret = error2; } out_unlock_internal:
@@ -800,7 +799,7 @@ write_retry: out_unlock_mutex:if (need_i_mutex)mutex_unlock (&inode->i_mutex); -return -error; +return ret; }STATIC int
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 1/8] xfs: ensure sync write errors are returned
Quoted 9 lines · Dave Chinner
From: Dave Chinner <[email protected]>
xfs_file_aio_write()only returns the error from synchronous flushing of the data and inode if error == 0. At the point where error is being checked, it is guaranteed to be > 0. Therefore any errors returned by the data or fsync flush will never be returned. Fix the checks so we overwrite the current error once and only if an error really occurred.
This looks good. You improved it a lot by
fixing up the bizarre "error" logic.
Quoted 4 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 51 +++++++++++++++++++++---------------------- 1 files changed, 25 insertions(+), 26 deletions(-)
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 1/8] xfs: ensure sync write errors are returned
In reply to Dave Chinnerfs/xfs/linux-2.6/xfs_file.c start: · line 740
Quoted 9 lines
fs/xfs/linux-2.6/xfs_file.c +5
-0+if (ret == -ENOSPC && !enospc) { +ssize_t ret2 =xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); +if (ret2) { + ret = ret2; goto out_unlock_internal; + } enospc =1 ; goto write_retry; }
I don't think there is any reason for the ret2 variale here. If it's non-zero we assign it to ret, and if it's zero we overwrite it again after jumping to the write_retry label.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 1/8] xfs: ensure sync write errors are returned
Quoted 5 lines · Christoph Hellwig
Quoted 5 lines
fs/xfs/linux-2.6/xfs_file.c +4
-0+if (ret == -ENOSPC && !enospc) { +ssize_t ret2 =xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); +if (ret2) { + ret = ret2; goto out_unlock_internal;
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c start: · line 740
Quoted 4 lines
fs/xfs/linux-2.6/xfs_file.c +1
-0+ } enospc =1 ; goto write_retry; }I don't think there is any reason for the ret2 variale here. If it's non-zero we assign it to ret, and if it's zero we overwrite it again after jumping to the
write_retrylabel.
True, I'll fix it.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
PATCH 2/8 xfs: factor common post-write isize handling code
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
--- fs/xfs/linux-2.6/xfs_file.c | 52 +++++++++++++++++++++++------------------- 1 files changed, 28 insertions(+), 24 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +28 -24
@@ -321,6 +321,30 @@ xfs_file_splice_read(return ret; } +STATIC void +xfs_aio_write_isize_update ( +struct inode *inode, +loff_t *ppos, +ssize_t bytes_written) +{ +struct xfs_inode *ip =XFS_I (inode); +xfs_fsize_t isize =i_size_read (inode); + +if (bytes_written >0 ) +XFS_STATS_ADD (xs_write_bytes, bytes_written); + +if (unlikely (bytes_written <0 && bytes_written != -EFAULT && + *ppos > isize)) + *ppos = isize; + +if (*ppos > ip->i_size) { +xfs_ilock (ip,XFS_ILOCK_EXCL ); +if (*ppos > ip->i_size) + ip->i_size = *ppos; +xfs_iunlock (ip,XFS_ILOCK_EXCL ); + } +} + STATIC ssize_t xfs_file_splice_write( struct pipe_inode_info *pipe,
@@ -331,7 +355,7 @@ xfs_file_splice_write( {struct inode *inode = outfilp->f_mapping->host;struct xfs_inode *ip =XFS_I (inode); -xfs_fsize_t isize, new_size; +xfs_fsize_t new_size;int ioflags =0 ;ssize_t ret;
@@ -355,19 +379,8 @@ xfs_file_splice_write(trace_xfs_file_splice_write (ip, count, *ppos, ioflags); ret =generic_file_splice_write (pipe, outfilp, ppos, count, flags); -if (ret >0 ) -XFS_STATS_ADD (xs_write_bytes, ret); - - isize =i_size_read (inode); -if (unlikely (ret <0 && ret != -EFAULT && *ppos > isize)) - *ppos = isize; -if (*ppos > ip->i_size) { -xfs_ilock (ip,XFS_ILOCK_EXCL ); -if (*ppos > ip->i_size) - ip->i_size = *ppos; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); - } +xfs_aio_write_isize_update (inode, ppos, ret);if (ip->i_new_size) {xfs_ilock (ip,XFS_ILOCK_EXCL );
@@ -576,7 +589,7 @@ xfs_file_aio_write(struct xfs_mount *mp = ip->i_mount;ssize_t ret =0 ;int ioflags =0 ; -xfs_fsize_t isize, new_size; +xfs_fsize_t new_size;int iolock;size_t ocount =0 , count;int need_i_mutex;
@@ -742,16 +755,7 @@ write_retry: current->backing_dev_info =NULL ; - isize =i_size_read (inode); -if (unlikely (ret <0 && ret != -EFAULT && iocb->ki_pos > isize)) - iocb->ki_pos = isize; - -if (iocb->ki_pos > ip->i_size) { -xfs_ilock (ip,XFS_ILOCK_EXCL ); -if (iocb->ki_pos > ip->i_size) - ip->i_size = iocb->ki_pos; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); - } +xfs_aio_write_isize_update (inode, &iocb->ki_pos, ret);if (ret <=0 ) goto out_unlock_internal;
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 2/8] xfs: factor common post-write isize handling code
Quoted 2 lines · Dave Chinner
From: Dave Chinner <[email protected]>
With one exception noted below, this looks good.
Quoted 4 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 52 +++++++++++++++++++++++------------------- 1 files changed, 28 insertions(+), 24 deletions(-)
. . .
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c +0
-4@@ -742,16 +755,7 @@ write_retry: current->backing_dev_info =NULL ; - isize =i_size_read (inode); -if (unlikely (ret <0 && ret != -EFAULT && iocb->ki_pos > isize)) - iocb->ki_pos = isize; -
fs/xfs/linux-2.6/xfs_file.c write_retry: · line 761
Quoted 10 lines
fs/xfs/linux-2.6/xfs_file.c +1
-6-if (iocb->ki_pos > ip->i_size) { -xfs_ilock (ip,XFS_ILOCK_EXCL ); -if (iocb->ki_pos > ip->i_size) - ip->i_size = iocb->ki_pos; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); - } +xfs_aio_write_isize_update (inode, &iocb->ki_pos, ret);if (ret <=0 ) goto out_unlock_internal;
Just below this point in the original code there is a call to XFS_STATS_ADD(xs_write_bytes, ret); that also needs to be removed, because xfs_aio_write_isize_update() now does it for you.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 4/8 xfs: introduce xfs_rw_lock() helpers for locking the inode
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
We need to obtain the i_mutex, i_iolock and i_ilock during the read and write paths. Add a set of wrapper functions to neatly encapsulate the lock ordering and shared/exclusive semantics to make the locking easier to follow and get right.
--- fs/xfs/linux-2.6/xfs_file.c | 123 ++++++++++++++++++++++++------------------- 1 files changed, 68 insertions(+), 55 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +68 -55
@@ -41,6 +41,40 @@static const struct vm_operations_struct xfs_file_vm_ops;/* +* Locking primitives for read and write IO paths to ensure we consistently use +* and order the inode->i_mutex, ip->i_lock and ip->i_iolock. +*/ +static inline void +xfs_rw_ilock ( +struct xfs_inode *ip, +int type) +{ +if (type &XFS_IOLOCK_EXCL ) +mutex_lock (&VFS_I (ip)->i_mutex); +xfs_ilock (ip, type); +} + +static inline void +xfs_rw_iunlock ( +struct xfs_inode *ip, +int type) +{ +xfs_iunlock (ip, type); +if (type &XFS_IOLOCK_EXCL ) +mutex_unlock (&VFS_I (ip)->i_mutex); +} + +static inline void +xfs_rw_ilock_demote ( +struct xfs_inode *ip, +int type) +{ +xfs_ilock_demote (ip, type); +if (type &XFS_IOLOCK_EXCL ) +mutex_unlock (&VFS_I (ip)->i_mutex); +} + +/* * xfs_iozero * * xfs_iozero clears the specified range of buffer supplied,
@@ -262,22 +296,21 @@ xfs_file_aio_read(if (XFS_FORCED_SHUTDOWN (mp))return -EIO ; -if (unlikely (ioflags &IO_ISDIRECT )) -mutex_lock (&inode->i_mutex); -xfs_ilock (ip,XFS_IOLOCK_SHARED ); -if (unlikely (ioflags &IO_ISDIRECT )) { +xfs_rw_ilock (ip,XFS_IOLOCK_EXCL ); +if (inode->i_mapping->nrpages) { ret = -xfs_flushinval_pages (ip, (iocb->ki_pos &PAGE_CACHE_MASK ),-1 ,FI_REMAPF_LOCKED ); +if (ret) { +xfs_rw_iunlock (ip,XFS_IOLOCK_EXCL ); +return ret; + } } -mutex_unlock (&inode->i_mutex); -if (ret) { -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); -return ret; - } - } +xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); + }else +xfs_rw_ilock (ip,XFS_IOLOCK_SHARED );trace_xfs_file_read (ip, size, iocb->ki_pos, ioflags);
@@ -285,7 +318,7 @@ xfs_file_aio_read(if (ret >0 )XFS_STATS_ADD (xs_read_bytes, ret); -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); +xfs_rw_iunlock (ip,XFS_IOLOCK_SHARED );return ret; }
@@ -309,7 +342,7 @@ xfs_file_splice_read(if (XFS_FORCED_SHUTDOWN (ip->i_mount))return -EIO ; -xfs_ilock (ip,XFS_IOLOCK_SHARED ); +xfs_rw_ilock (ip,XFS_IOLOCK_SHARED );trace_xfs_file_splice_read (ip, count, *ppos, ioflags);
@@ -317,7 +350,7 @@ xfs_file_splice_read(if (ret >0 )XFS_STATS_ADD (xs_read_bytes, ret); -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); +xfs_rw_iunlock (ip,XFS_IOLOCK_SHARED );return ret; }
@@ -338,10 +371,10 @@ xfs_aio_write_isize_update( *ppos = isize;if (*ppos > ip->i_size) { -xfs_ilock (ip,XFS_ILOCK_EXCL ); +xfs_rw_ilock (ip,XFS_ILOCK_EXCL );if (*ppos > ip->i_size) ip->i_size = *ppos; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); } }
@@ -356,11 +389,11 @@ xfs_aio_write_newsize_update( struct xfs_inode *ip) {if (ip->i_new_size) { -xfs_ilock (ip,XFS_ILOCK_EXCL ); +xfs_rw_ilock (ip,XFS_ILOCK_EXCL ); ip->i_new_size =0 ;if (ip->i_d.di_size > ip->i_size) ip->i_d.di_size = ip->i_size; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); } }
@@ -386,14 +419,13 @@ xfs_file_splice_write(if (XFS_FORCED_SHUTDOWN (ip->i_mount))return -EIO ; -xfs_ilock (ip,XFS_IOLOCK_EXCL ); +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |XFS_IOLOCK_EXCL ); new_size = *ppos + count; -xfs_ilock (ip,XFS_ILOCK_EXCL );if (new_size > ip->i_size) ip->i_new_size = new_size; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL );trace_xfs_file_splice_write (ip, count, *ppos, ioflags);
@@ -401,7 +433,7 @@ xfs_file_splice_write(xfs_aio_write_isize_update (inode, ppos, ret);xfs_aio_write_newsize_update (ip); -xfs_iunlock (ip,XFS_IOLOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_IOLOCK_EXCL );return ret; }
@@ -604,7 +636,6 @@ xfs_file_aio_write(xfs_fsize_t new_size;int iolock;size_t ocount =0 , count; -int need_i_mutex;XFS_STATS_INC (xs_write_calls);
@@ -631,21 +662,16 @@ xfs_file_aio_write( relock:if (ioflags &IO_ISDIRECT ) { iolock =XFS_IOLOCK_SHARED ; - need_i_mutex =0 ; }else { iolock =XFS_IOLOCK_EXCL ; - need_i_mutex =1 ; -mutex_lock (&inode->i_mutex); } -xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); - start: ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (ret) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); - goto out_unlock_mutex; +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); +return ret; }if (ioflags &IO_ISDIRECT ) {
@@ -654,16 +680,14 @@ start: mp->m_rtdev_targp : mp->m_ddev_targp;if ((pos & target->bt_smask) || (count & target->bt_smask)) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock);return XFS_ERROR (-EINVAL ); } -if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); +if (iolock !=XFS_IOLOCK_EXCL && + (mapping->nrpages || pos > ip->i_size)) { +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); iolock =XFS_IOLOCK_EXCL ; - need_i_mutex =1 ; -mutex_lock (&inode->i_mutex); -xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); goto start; } }
@@ -687,11 +711,11 @@ start:if (pos > ip->i_size) { ret = -xfs_zero_eof (ip, pos, ip->i_size);if (ret) { -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); goto out_unlock_internal; } } -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL );/* * If we're writing the file then make sure to clear the
@@ -708,7 +732,7 @@ start:if ((ioflags &IO_ISDIRECT )) {if (mapping->nrpages) { -WARN_ON (need_i_mutex ==0 ); +WARN_ON (iolock !=XFS_IOLOCK_EXCL ); ret = -xfs_flushinval_pages (ip, (pos &PAGE_CACHE_MASK ),-1 ,FI_REMAPF_LOCKED );
@@ -716,13 +740,10 @@ start: goto out_unlock_internal; } -if (need_i_mutex) { +if (iolock ==XFS_IOLOCK_EXCL ) {/* demote the lock now the cached pages are gone */ -xfs_ilock_demote (ip,XFS_IOLOCK_EXCL ); -mutex_unlock (&inode->i_mutex); - +xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); iolock =XFS_IOLOCK_SHARED ; - need_i_mutex =0 ; }trace_xfs_file_direct_write (ip, count, iocb->ki_pos, ioflags);
@@ -740,7 +761,7 @@ start: count -= ret; ioflags &= ~IO_ISDIRECT ; -xfs_iunlock (ip, iolock); +xfs_rw_iunlock (ip, iolock); goto relock; } }else {
@@ -779,14 +800,9 @@ write_retry:loff_t end = pos + ret -1 ;int error, error2; -xfs_iunlock (ip, iolock); -if (need_i_mutex) -mutex_unlock (&inode->i_mutex); - +xfs_rw_iunlock (ip, iolock); error =filemap_write_and_wait_range (mapping, pos, end); -if (need_i_mutex) -mutex_lock (&inode->i_mutex); -xfs_ilock (ip, iolock); +xfs_rw_ilock (ip, iolock); error2 = -xfs_file_fsync (file, (file->f_flags & __O_SYNC) ?0 :1 );
@@ -798,10 +814,7 @@ write_retry: out_unlock_internal:xfs_aio_write_newsize_update (ip); -xfs_iunlock (ip, iolock); - out_unlock_mutex: -if (need_i_mutex) -mutex_unlock (&inode->i_mutex); +xfs_rw_iunlock (ip, iolock);return ret; }
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 4/8] xfs: introduce xfs_rw_lock() helpers for locking the inode
Quoted 8 lines · Dave Chinner
From: Dave Chinner <[email protected]>
We need to obtain the
i_mutex,i_iolockandi_ilockduring the read and write paths. Add a set of wrapper functions to neatly encapsulate the lock ordering and shared/exclusive semantics to make the locking easier to follow and get right.Signed-off-by Dave Chinner <[email protected]>
I like this change, but I think you missed a lock call. I also notice there are some locking differences, and I don't really question them but I wonder if you can offer a little more explanation.
fs/xfs/linux-2.6/xfs_file.c
Quoted 8 lines
--- fs/xfs/linux-2.6/xfs_file.c | 123 ++++++++++++++++++++++++------------------- 1 files changed, 68 insertions(+), 55 deletions(-)fs/xfs/linux-2.6/xfs_file.c +0
-0
. . .
fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_read( · line 301
Quoted 11 lines
fs/xfs/linux-2.6/xfs_file.c +2
-4@@ -262,22 +296,21 @@ xfs_file_aio_read(if (XFS_FORCED_SHUTDOWN (mp))return -EIO ; -if (unlikely (ioflags &IO_ISDIRECT )) -mutex_lock (&inode->i_mutex); -xfs_ilock (ip,XFS_IOLOCK_SHARED ); -if (unlikely (ioflags &IO_ISDIRECT )) { +xfs_rw_ilock (ip,XFS_IOLOCK_EXCL ); +
Previously only XFS_IOLOCK_SHARED was used here. I understand that using the IOLOCK_EXCL now gets the desired mutex_lock() call. Is the previous code in error here though? Can you anticipate any different behavior because of this lock change? Does this specific change justify separating it into a small patch just before this one?
Quoted 11 lines
fs/xfs/linux-2.6/xfs_file.c +4
-2if (inode->i_mapping->nrpages) { ret = -xfs_flushinval_pages (ip, (iocb->ki_pos &PAGE_CACHE_MASK ),-1 ,FI_REMAPF_LOCKED ); +if (ret) { +xfs_rw_iunlock (ip,XFS_IOLOCK_EXCL ); +return ret; + } } -mutex_unlock (&inode->i_mutex); -if (ret) {
fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_read( · line 316
Quoted 10 lines
fs/xfs/linux-2.6/xfs_file.c +3
-4-xfs_iunlock (ip,XFS_IOLOCK_SHARED ); -return ret; - } - } +xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); + }else +xfs_rw_ilock (ip,XFS_IOLOCK_SHARED );trace_xfs_file_read (ip, size, iocb->ki_pos, ioflags);
. . .
fs/xfs/linux-2.6/xfs_file.c xfs_file_splice_write( · line 422
Quoted 6 lines
fs/xfs/linux-2.6/xfs_file.c +1
-1@@ -386,14 +419,13 @@ xfs_file_splice_write(if (XFS_FORCED_SHUTDOWN (ip->i_mount))return -EIO ; -xfs_ilock (ip,XFS_IOLOCK_EXCL ); +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |XFS_IOLOCK_EXCL );
Similar sentiments here. We will now be acquiring i_mutex here where previously we did not. Is that OK?
fs/xfs/linux-2.6/xfs_file.c xfs_file_splice_write( · line 431
Quoted 10 lines
fs/xfs/linux-2.6/xfs_file.c +1
-2new_size = *ppos + count; -xfs_ilock (ip,XFS_ILOCK_EXCL );if (new_size > ip->i_size) ip->i_new_size = new_size; -xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL );trace_xfs_file_splice_write (ip, count, *ppos, ioflags);
. . .
fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_write( · line 642
Quoted 13 lines
fs/xfs/linux-2.6/xfs_file.c +0
-5@@ -631,21 +662,16 @@ xfs_file_aio_write( relock:if (ioflags &IO_ISDIRECT ) { iolock =XFS_IOLOCK_SHARED ; - need_i_mutex =0 ; }else { iolock =XFS_IOLOCK_EXCL ; - need_i_mutex =1 ; -mutex_lock (&inode->i_mutex); } -xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); -
Maybe I'm missing something, but I think you want to insert this here:
xfs_rw_ilock(ip, XFS_ILOCK_EXCL|iolock);
...because (for starters) if generic_write_checks() returns an error below you're going to be calling the unlock routine.
Quoted 12 lines
fs/xfs/linux-2.6/xfs_file.c +2
-2start: ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (ret) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); - goto out_unlock_mutex; +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); +return ret; }if (ioflags &IO_ISDIRECT ) {
fs/xfs/linux-2.6/xfs_file.c start: · line 686
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c +1
-1@@ -654,16 +680,14 @@ start: mp->m_rtdev_targp : mp->m_ddev_targp;if ((pos & target->bt_smask) || (count & target->bt_smask)) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock);return XFS_ERROR (-EINVAL ); }
One can get a little lost in this code. I don't know if this comment is exactly right, but something like it might be helpful (while you're in here).
/*
* For direct I/O, if there are cached pages or
* we're extending the file, we need IOLOCK_EXCL
* until we're sure the bytes at the new EOF have
* been zeroed and/or the cached pages are flushed
* out. Upgrade the I/O lock and start again. */
fs/xfs/linux-2.6/xfs_file.c start: · line 693
Quoted 12 lines
fs/xfs/linux-2.6/xfs_file.c +3
-5-if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); +if (iolock !=XFS_IOLOCK_EXCL && + (mapping->nrpages || pos > ip->i_size)) { +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); iolock =XFS_IOLOCK_EXCL ; - need_i_mutex =1 ; -mutex_lock (&inode->i_mutex); -xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); goto start; } }
. . .
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 4/8] xfs: introduce xfs_rw_lock() helpers for locking the inode
Quoted 30 lines · Alex Elder
Quoted 8 lines · Dave Chinner
From: Dave Chinner <[email protected]>
We need to obtain the
i_mutex,i_iolockandi_ilockduring the read and write paths. Add a set of wrapper functions to neatly encapsulate the lock ordering and shared/exclusive semantics to make the locking easier to follow and get right.Signed-off-by Dave Chinner <[email protected]>I like this change, but I think you missed a lock call. I also notice there are some locking differences, and I don't really question them but I wonder if you can offer a little more explanation.
fs/xfs/linux-2.6/xfs_file.c
Quoted 8 lines
--- fs/xfs/linux-2.6/xfs_file.c | 123 ++++++++++++++++++++++++------------------- 1 files changed, 68 insertions(+), 55 deletions(-)fs/xfs/linux-2.6/xfs_file.c +0
-0. . .
Quoted 4 lines
fs/xfs/linux-2.6/xfs_file.c +0
-0@@ -262,22 +296,21 @@ xfs_file_aio_read(if (XFS_FORCED_SHUTDOWN (mp))return -EIO ;
Quoted 12 lines
fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_read( · line 301
Quoted 7 lines
fs/xfs/linux-2.6/xfs_file.c +2
-4-if (unlikely (ioflags &IO_ISDIRECT )) -mutex_lock (&inode->i_mutex); -xfs_ilock (ip,XFS_IOLOCK_SHARED ); -if (unlikely (ioflags &IO_ISDIRECT )) { +xfs_rw_ilock (ip,XFS_IOLOCK_EXCL ); +Previously only
XFS_IOLOCK_SHAREDwas used here. I understand that using theIOLOCK_EXCLnow gets the desiredmutex_lock()call. Is the previous code in error here though?
No, it isn't wrong. However, the new code takes advantage of the demote process to drop back to IOLOCK_SHARED before the i_mutex is dropped so there's no externally visible change in behaviour.
This means that the locking heirarchy is much easier to maintain and understand as all operations use the same wrappers. i.e. there is no real need for a i_mutex + IOLOCK_SHARED state because i_mutex means exclusive access and is the outer lock. Hence the IOLOCK_SHARED vs IOLOCK_EXCL is pretty much irrelevant so I picked the simpler one to implement and understand.
Quoted 2 lines
Can you anticipate
any different behavior because of this lock change?
None that I can think of, because we are supposed to hold the i_mutex over page cache flushes and it is the outermost lock.
Quoted 2 lines
Does this specific change justify separating it into a small patch just before this one?
IMO, not really, but I can if you want. It'll just churn the patch series, though...
Quoted 28 lines
Quoted 11 lines
fs/xfs/linux-2.6/xfs_file.c +4
-2if (inode->i_mapping->nrpages) { ret = -xfs_flushinval_pages (ip, (iocb->ki_pos &PAGE_CACHE_MASK ),-1 ,FI_REMAPF_LOCKED ); +if (ret) { +xfs_rw_iunlock (ip,XFS_IOLOCK_EXCL ); +return ret; + } } -mutex_unlock (&inode->i_mutex); -if (ret) {fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_read( · line 316
Quoted 10 lines
fs/xfs/linux-2.6/xfs_file.c +3
-4-xfs_iunlock (ip,XFS_IOLOCK_SHARED ); -return ret; - } - } +xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); + }else +xfs_rw_ilock (ip,XFS_IOLOCK_SHARED );trace_xfs_file_read (ip, size, iocb->ki_pos, ioflags);. . .
Quoted 4 lines
fs/xfs/linux-2.6/xfs_file.c +0
-0@@ -386,14 +419,13 @@ xfs_file_splice_write(if (XFS_FORCED_SHUTDOWN (ip->i_mount))return -EIO ;
Quoted 5 lines
fs/xfs/linux-2.6/xfs_file.c xfs_file_splice_write( · line 422
Quoted 2 lines
fs/xfs/linux-2.6/xfs_file.c +1
-1-xfs_ilock (ip,XFS_IOLOCK_EXCL ); +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |XFS_IOLOCK_EXCL );Similar sentiments here. We will now be acquiring
i_mutexhere where previously we did not. Is that OK?
It should be because it will have exactly the same serialisation effect as the current XFS_IOLOCK_EXCL has - exclusive access across the write.
Quoted 16 lines
fs/xfs/linux-2.6/xfs_file.c xfs_file_aio_write( · line 642
Quoted 13 lines
fs/xfs/linux-2.6/xfs_file.c +0
-5@@ -631,21 +662,16 @@ xfs_file_aio_write( relock:if (ioflags &IO_ISDIRECT ) { iolock =XFS_IOLOCK_SHARED ; - need_i_mutex =0 ; }else { iolock =XFS_IOLOCK_EXCL ; - need_i_mutex =1 ; -mutex_lock (&inode->i_mutex); } -xfs_ilock (ip,XFS_ILOCK_EXCL |iolock); -Maybe I'm missing something, but I think you want to insert this here:
Quoted 4 lines
xfs_rw_ilock(ip, XFS_ILOCK_EXCL|iolock);...because (for starters) if
generic_write_checks()returns an error below you're going to be calling the unlock routine.
Yes, good catch. I didn't notice that because I didn't test the series patch by patch - just the end result. Will fix.
Quoted 26 lines
Quoted 13 lines
fs/xfs/linux-2.6/xfs_file.c +2
-2start: ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (ret) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); - goto out_unlock_mutex; +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); +return ret; }if (ioflags &IO_ISDIRECT ) {fs/xfs/linux-2.6/xfs_file.c start: · line 686
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c +1
-1@@ -654,16 +680,14 @@ start: mp->m_rtdev_targp : mp->m_ddev_targp;if ((pos & target->bt_smask) || (count & target->bt_smask)) { -xfs_iunlock (ip,XFS_ILOCK_EXCL |iolock); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock);return XFS_ERROR (-EINVAL ); }One can get a little lost in this code. I don't know if this comment is exactly right, but something like it might be helpful (while you're in here).
Quoted 7 lines
/*
* For direct I/O, if there are cached pages or
* we're extending the file, we needIOLOCK_EXCL
* until we're sure the bytes at the new EOF have
* been zeroed and/or the cached pages are flushed
* out. Upgrade the I/O lock and start again. */
Probably a good idea. I'll add something there.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
PATCH 5/8 xfs: split direct IO write path from xfs_file_aio_write
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The current xfs_file_aio_write code is a mess of locking shenanigans to handle the different locking requirements of buffered and direct IO. Start to clean this up by disentangling the direct IO path from the mess.
--- fs/xfs/linux-2.6/xfs_file.c | 168 ++++++++++++++++++++++++++++--------------- 1 files changed, 110 insertions(+), 58 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +110 -58
@@ -619,6 +619,110 @@ out_lock:return error; } +/* +* xfs_file_dio_aio_write - handle direct IO writes +* +* Lock the inode appropriately to prepare for and issue a direct IO write. +* By spearating it from the buffered write path we remove all the tricky to +* follow locking changes and looping. This also clearly indicates that XFS +* does not fall back to buffered IO in the direct IO write path. +* +* Returns with locks held indicated by @iolock and errors indicated by +* negative return values. +*/ +STATIC ssize_t +xfs_file_dio_aio_write ( +struct kiocb *iocb, +const struct iovec *iovp, +unsigned long nr_segs, +loff_t pos, +size_t ocount, +int *iolock) +{ +struct file *file = iocb->ki_filp; +struct address_space *mapping = file->f_mapping; +struct inode *inode = mapping->host; +struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount; +ssize_t ret =0 ; +xfs_fsize_t new_size; +size_t count = ocount; +xfs_buftarg_t *target =XFS_IS_REALTIME_INODE (ip) ? + mp->m_rtdev_targp : mp->m_ddev_targp; + + *iolock =0 ; +if ((pos & target->bt_smask) || (count & target->bt_smask)) +return -XFS_ERROR (EINVAL ); + +if (mapping->nrpages || pos > ip->i_size) + *iolock =XFS_IOLOCK_EXCL ; +else + *iolock =XFS_IOLOCK_SHARED ; +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |*iolock); + + ret =generic_write_checks (file, &pos, &count, +S_ISBLK (inode->i_mode)); +if (ret) { +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |*iolock); + *iolock =0 ; +return ret; + } + + new_size = pos + count; +if (new_size > ip->i_size) + ip->i_new_size = new_size; + +if (likely (!(file->f_mode &FMODE_NOCMTIME ))) +file_update_time (file); + +/* +* If the offset is beyond the size of the file, we have a couple of +* things to do. First, if there is already space allocated we need to +* either create holes or zero the disk or ... +* +* If there is a page where the previous size lands, we need to zero it +* out up to the new size. +*/ +if (pos > ip->i_size) { + ret = -xfs_zero_eof (ip, pos, ip->i_size); +if (ret) { +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); +return ret; + } + } +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); + +/* +* If we're writing the file then make sure to clear the setuid and +* setgid bits if the process is not being run by root. This keeps +* people from modifying setuid and setgid binaries. +*/ + ret =file_remove_suid (file); +if (unlikely (ret)) +return ret; + +if (mapping->nrpages) { +WARN_ON (*iolock !=XFS_IOLOCK_EXCL ); + ret = -xfs_flushinval_pages (ip, (pos &PAGE_CACHE_MASK ),-1 , +FI_REMAPF_LOCKED ); +if (ret) +return ret; + } + +if (*iolock ==XFS_IOLOCK_EXCL ) { +/* demote the lock now the cached pages are gone */ +xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); + *iolock =XFS_IOLOCK_SHARED ; + } + +trace_xfs_file_direct_write (ip, count, iocb->ki_pos,0 ); + ret =generic_file_direct_write (iocb, iovp, + &nr_segs, pos, &iocb->ki_pos, count, ocount); + +/* No fallback to buffered IO on errors for XFS. */ +return ret; +} + STATIC ssize_t xfs_file_aio_write( struct kiocb *iocb,
@@ -659,14 +763,13 @@ xfs_file_aio_write(if (XFS_FORCED_SHUTDOWN (mp))return -EIO ; -relock:if (ioflags &IO_ISDIRECT ) { - iolock =XFS_IOLOCK_SHARED ; - }else { - iolock =XFS_IOLOCK_EXCL ; + ret =xfs_file_dio_aio_write (iocb, iovp, nr_segs, pos, + ocount, &iolock); + goto done_io; } -start: + iolock =XFS_IOLOCK_EXCL ; ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (ret) {
@@ -674,24 +777,6 @@ start:return ret; } -if (ioflags &IO_ISDIRECT ) { -xfs_buftarg_t *target = -XFS_IS_REALTIME_INODE (ip) ? - mp->m_rtdev_targp : mp->m_ddev_targp; - -if ((pos & target->bt_smask) || (count & target->bt_smask)) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); -return XFS_ERROR (-EINVAL ); - } - -if (iolock !=XFS_IOLOCK_EXCL && - (mapping->nrpages || pos > ip->i_size)) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); - iolock =XFS_IOLOCK_EXCL ; - goto start; - } - } - new_size = pos + count;if (new_size > ip->i_size) ip->i_new_size = new_size;
@@ -730,41 +815,7 @@ start:/* We can write back this queue in page reclaim */ current->backing_dev_info = mapping->backing_dev_info; -if ((ioflags &IO_ISDIRECT )) { -if (mapping->nrpages) { -WARN_ON (iolock !=XFS_IOLOCK_EXCL ); - ret = -xfs_flushinval_pages (ip, - (pos &PAGE_CACHE_MASK ), --1 ,FI_REMAPF_LOCKED ); -if (ret) - goto out_unlock_internal; - } - -if (iolock ==XFS_IOLOCK_EXCL ) { -/* demote the lock now the cached pages are gone */ -xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); - iolock =XFS_IOLOCK_SHARED ; - } - -trace_xfs_file_direct_write (ip, count, iocb->ki_pos, ioflags); - ret =generic_file_direct_write (iocb, iovp, - &nr_segs, pos, &iocb->ki_pos, count, ocount); - -/* -* direct-io write to a hole: fall through to buffered I/O -* for completing the rest of the request. -*/ -if (ret >=0 && ret != count) { -XFS_STATS_ADD (xs_write_bytes, ret); - - pos += ret; - count -= ret; - - ioflags &= ~IO_ISDIRECT ; -xfs_rw_iunlock (ip, iolock); - goto relock; - } - }else { +if (!(ioflags &IO_ISDIRECT )) {int enospc =0 ; write_retry:
@@ -788,6 +839,7 @@ write_retry: current->backing_dev_info =NULL ; +done_io:xfs_aio_write_isize_update (inode, &iocb->ki_pos, ret);if (ret <=0 )
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 5/8] xfs: split direct IO write path from xfs_file_aio_write
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The current
xfs_file_aio_writecode is a mess of locking shenanigans to handle the different locking requirements of buffered and direct IO. Start to clean this up by disentangling the direct IO path from the mess.
All good, very good. But I'm not sure why you cut out the code that backed off to buffered I/O if generic_file_direct_write() returns an error. (You gave no explanation.)
-Alex
Quoted 10 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 168 ++++++++++++++++++++++++++++--------------- 1 files changed, 110 insertions(+), 58 deletions(-)fs/xfs/linux-2.6/xfs_file.c +0
-0
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 626
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c +5
-0@@ -619,6 +619,110 @@ out_lock:return error; } +/* +* xfs_file_dio_aio_write - handle direct IO writes +* +* Lock the inode appropriately to prepare for and issue a direct IO write. +* By spearating it from the buffered write path we remove all the tricky to
separating
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 636
Quoted 10 lines
fs/xfs/linux-2.6/xfs_file.c +10
-0+* follow locking changes and looping. This also clearly indicates that XFS +* does not fall back to buffered IO in the direct IO write path. +* +* Returns with locks held indicated by @iolock and errors indicated by +* negative return values. +*/ +STATIC ssize_t +xfs_file_dio_aio_write( + struct kiocb *iocb, + const struct iovec *iovp,
. . .
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 722
Quoted 5 lines
fs/xfs/linux-2.6/xfs_file.c +5
-0+trace_xfs_file_direct_write (ip, count, iocb->ki_pos,0 ); + ret =generic_file_direct_write (iocb, iovp, + &nr_segs, pos, &iocb->ki_pos, count, ocount); + +/* No fallback to buffered IO on errors for XFS. */
Why is this? The previous code did fall back (so this change is doing more than just splitting out the direct I/O path).
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 728
Quoted 6 lines
fs/xfs/linux-2.6/xfs_file.c +3
-0+return ret; +} + STATIC ssize_t xfs_file_aio_write( struct kiocb *iocb,
. . .
fs/xfs/linux-2.6/xfs_file.c write_retry: · line 845
Quoted 8 lines
fs/xfs/linux-2.6/xfs_file.c +1
-0@@ -788,6 +839,7 @@ write_retry: current->backing_dev_info =NULL ; +done_io:xfs_aio_write_isize_update (inode, &iocb->ki_pos, ret);if (ret <=0 )
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 5/8] xfs: split direct IO write path from xfs_file_aio_write
Quoted 8 lines · Alex Elder
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The current
xfs_file_aio_writecode is a mess of locking shenanigans to handle the different locking requirements of buffered and direct IO. Start to clean this up by disentangling the direct IO path from the mess.
Quoted 4 lines
All good, very good. But I'm not sure why you cut out the code that backed off to buffered I/O if
generic_file_direct_write()returns an error. (You gave no explanation.)
Oh, I thought I put one in there. It's simple, though - XFS handles all allocation cases in the direct IO code and never returns a result that will require falling back to the buffered IO path. It is basically a code path that has been unused for years and all it does is make the logic much more complex to understand.
I'll add that to the commit message....
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 5/8] xfs: split direct IO write path from xfs_file_aio_write
In reply to Dave ChinnerQuoted 2 lines
+This also clearly indicates that XFS
+ * does not fall back to buffered IO in the direct IO write path.
I don't think this comment belongs into the function header. It's already present in the right place anyway, so it can just be removed.
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 651
Quoted 2 lines
fs/xfs/linux-2.6/xfs_file.c +2
-0+xfs_buftarg_t *target =XFS_IS_REALTIME_INODE (ip) ? + mp->m_rtdev_targp : mp->m_ddev_targp;
struct xfs_buftarg, please.
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 723
Quoted 6 lines
fs/xfs/linux-2.6/xfs_file.c +6
-0+trace_xfs_file_direct_write (ip, count, iocb->ki_pos,0 ); + ret =generic_file_direct_write (iocb, iovp, + &nr_segs, pos, &iocb->ki_pos, count, ocount); + +/* No fallback to buffered IO on errors for XFS. */ +return ret;
I'd add an
ASSERT(ret < 0 || ret == count);
here to make sure we don't get problems due to changes in the core direct I/O code.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 5/8] xfs: split direct IO write path from xfs_file_aio_write
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines
+This also clearly indicates that XFS
+ * does not fall back to buffered IO in the direct IO write path.I don't think this comment belongs into the function header. It's already present in the right place anyway, so it can just be removed.
OK.
Quoted 4 lines
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 651
Quoted 2 lines
fs/xfs/linux-2.6/xfs_file.c +2
-0+xfs_buftarg_t *target =XFS_IS_REALTIME_INODE (ip) ? + mp->m_rtdev_targp : mp->m_ddev_targp;struct
xfs_buftarg, please.
Was copy-n-paste. Will fix.
Quoted 9 lines
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 723
Quoted 6 lines
fs/xfs/linux-2.6/xfs_file.c +6
-0+trace_xfs_file_direct_write (ip, count, iocb->ki_pos,0 ); + ret =generic_file_direct_write (iocb, iovp, + &nr_segs, pos, &iocb->ki_pos, count, ocount); + +/* No fallback to buffered IO on errors for XFS. */ +return ret;I'd add an
Quoted 4 lines
ASSERT(ret < 0 || ret == count);here to make sure we don't get problems due to changes in the core direct I/O code.
Yup, good idea.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
PATCH 6/8 xfs: split buffered IO write path from xfs_file_aio_write
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Complete the split of the different write IO paths by splitting the buffered IO write path out of xfs_file_aio_write(). This makes the different mechanisms of the write patchs easier to follow.
--- fs/xfs/linux-2.6/xfs_file.c | 146 ++++++++++++++++++++----------------------- 1 files changed, 69 insertions(+), 77 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +69 -77
@@ -724,56 +724,31 @@ xfs_file_dio_aio_write( } STATIC ssize_t -xfs_file_aio_write ( +xfs_file_buffered_aio_write (struct kiocb *iocb,const struct iovec *iovp,unsigned long nr_segs, -loff_t pos) +loff_t pos, +size_t ocount, +int *iolock) {struct file *file = iocb->ki_filp;struct address_space *mapping = file->f_mapping;struct inode *inode = mapping->host;struct xfs_inode *ip =XFS_I (inode); -struct xfs_mount *mp = ip->i_mount; -ssize_t ret =0 ; -int ioflags =0 ; +ssize_t ret; +int enospc =0 ;xfs_fsize_t new_size; -int iolock; -size_t ocount =0 , count; - -XFS_STATS_INC (xs_write_calls); - -BUG_ON (iocb->ki_pos != pos); - -if (unlikely (file->f_flags &O_DIRECT )) - ioflags |=IO_ISDIRECT ; -if (file->f_mode &FMODE_NOCMTIME ) - ioflags |=IO_INVIS ; - - ret =generic_segment_checks (iovp, &nr_segs, &ocount,VERIFY_READ ); -if (ret) -return ret; - - count = ocount; -if (count ==0 ) -return 0 ; - -xfs_wait_for_freeze (mp,SB_FREEZE_WRITE ); - -if (XFS_FORCED_SHUTDOWN (mp)) -return -EIO ; +size_t count = ocount; -if (ioflags &IO_ISDIRECT ) { - ret =xfs_file_dio_aio_write (iocb, iovp, nr_segs, pos, - ocount, &iolock); - goto done_io; - } + *iolock =XFS_IOLOCK_EXCL ; +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |*iolock); - iolock =XFS_IOLOCK_EXCL ; ret =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (ret) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |iolock); +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |*iolock); + *iolock =0 ;return ret; }
@@ -781,69 +756,86 @@ xfs_file_aio_write(if (new_size > ip->i_size) ip->i_new_size = new_size; -if (likely (!(ioflags &IO_INVIS ))) +if (likely (!(file->f_mode &FMODE_NOCMTIME )))file_update_time (file); -/* -* If the offset is beyond the size of the file, we have a couple -* of things to do. First, if there is already space allocated -* we need to either create holes or zero the disk or ... -* -* If there is a page where the previous size lands, we need -* to zero it out up to the new size. -*/ -if (pos > ip->i_size) { ret = -xfs_zero_eof (ip, pos, ip->i_size);if (ret) {xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); - goto out_unlock_internal; +return ret; } }xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); -/* -* If we're writing the file then make sure to clear the -* setuid and setgid bits if the process is not being run -* by root. This keeps people from modifying setuid and -* setgid binaries. -*/ ret =file_remove_suid (file);if (unlikely (ret)) - goto out_unlock_internal; +return ret;/* We can write back this queue in page reclaim */ current->backing_dev_info = mapping->backing_dev_info; -if (!(ioflags &IO_ISDIRECT )) { -int enospc =0 ; - write_retry: -trace_xfs_file_buffered_write (ip, count, iocb->ki_pos, ioflags); - ret =generic_file_buffered_write (iocb, iovp, nr_segs, - pos, &iocb->ki_pos, count, ret); -/* -* if we just got an ENOSPC, flush the inode now we -* aren't holding any page locks and retry *once* -*/ -if (ret == -ENOSPC && !enospc) { -ssize_t ret2 =xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); -if (ret2) { - ret = ret2; - goto out_unlock_internal; - } - enospc =1 ; - goto write_retry; - } +trace_xfs_file_buffered_write (ip, count, iocb->ki_pos,0 ); + ret =generic_file_buffered_write (iocb, iovp, nr_segs, + pos, &iocb->ki_pos, count, ret); +/* +* if we just got an ENOSPC, flush the inode now we aren't holding any +* page locks and retry *once* +*/ +if (ret == -ENOSPC && !enospc) { + ret = -xfs_flush_pages (ip,0 ,-1 ,0 ,FI_NONE ); +if (ret) +return ret; + enospc =1 ; + goto write_retry; } - current->backing_dev_info =NULL ; +return ret; +} + +STATIC ssize_t +xfs_file_aio_write ( +struct kiocb *iocb, +const struct iovec *iovp, +unsigned long nr_segs, +loff_t pos) +{ +struct file *file = iocb->ki_filp; +struct address_space *mapping = file->f_mapping; +struct inode *inode = mapping->host; +struct xfs_inode *ip =XFS_I (inode); +ssize_t ret; +int iolock; +size_t ocount =0 ; + +XFS_STATS_INC (xs_write_calls); + +BUG_ON (iocb->ki_pos != pos); + + ret =generic_segment_checks (iovp, &nr_segs, &ocount,VERIFY_READ ); +if (ret) +return ret; + +if (ocount ==0 ) +return 0 ; + +xfs_wait_for_freeze (ip->i_mount,SB_FREEZE_WRITE ); + +if (XFS_FORCED_SHUTDOWN (ip->i_mount)) +return -EIO ; + +if (unlikely (file->f_flags &O_DIRECT )) + ret =xfs_file_dio_aio_write (iocb, iovp, nr_segs, pos, + ocount, &iolock); +else + ret =xfs_file_buffered_aio_write (iocb, iovp, nr_segs, pos, + ocount, &iolock); -done_io:xfs_aio_write_isize_update (inode, &iocb->ki_pos, ret);if (ret <=0 ) - goto out_unlock_internal; + goto out_unlock;XFS_STATS_ADD (xs_write_bytes, ret);
@@ -864,7 +856,7 @@ done_io: ret = error2; } - out_unlock_internal: +out_unlock:xfs_aio_write_newsize_update (ip);xfs_rw_iunlock (ip, iolock);return ret;
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 6/8] xfs: split buffered IO write path from xfs_file_aio_write
Quoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Complete the split of the different write IO paths by splitting the buffered IO write path out of
xfs_file_aio_write(). This makes the different mechanisms of the write patchs easier to follow.
Looks good.
fs/xfs/linux-2.6/xfs_file.c
Quoted 9 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 146 ++++++++++++++++++++----------------------- 1 files changed, 69 insertions(+), 77 deletions(-)fs/xfs/linux-2.6/xfs_file.c +0
-0
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH 7/8 xfs: factor common write setup code
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The buffered IO and direct Io write paths share a common set of checks and limiting code prior to issuing the write. Factor that into a common helper function.
--- fs/xfs/linux-2.6/xfs_file.c | 129 ++++++++++++++++++++----------------------- 1 files changed, 59 insertions(+), 70 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +59 -70
@@ -620,6 +620,61 @@ out_lock: }/* +* Common pre-write limit and setup checks. +* +* Returns with iolock held according to @iolock. +*/ +STATIC ssize_t +xfs_file_aio_write_checks ( +struct file *file, +loff_t *pos, +size_t *count, +int *iolock) +{ +struct inode *inode = file->f_mapping->host; +struct xfs_inode *ip =XFS_I (inode); +xfs_fsize_t new_size; +int error =0 ; + +xfs_rw_ilock (ip,XFS_ILOCK_EXCL |*iolock); + + error =generic_write_checks (file, pos, count,S_ISBLK (inode->i_mode)); +if (error) { +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |*iolock); + *iolock =0 ; +return error; + } + + new_size = *pos + *count; +if (new_size > ip->i_size) + ip->i_new_size = new_size; + +if (likely (!(file->f_mode &FMODE_NOCMTIME ))) +file_update_time (file); + +/* +* If the offset is beyond the size of the file, we need to zero any +* blocks that fall between the existing EOF and the start of this +* write. +*/ +if (*pos > ip->i_size) + error = -xfs_zero_eof (ip, *pos, ip->i_size); + +xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); +if (error) +return error; + +/* +* If we're writing the file then make sure to clear the setuid and +* setgid bits if the process is not being run by root. This keeps +* people from modifying setuid and setgid binaries. +*/ + error =file_remove_suid (file); +return error; + +} + +/* * xfs_file_dio_aio_write - handle direct IO writes * * Lock the inode appropriately to prepare for and issue a direct IO write.
@@ -645,7 +700,6 @@ xfs_file_dio_aio_write(struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount;ssize_t ret =0 ; -xfs_fsize_t new_size;size_t count = ocount;xfs_buftarg_t *target =XFS_IS_REALTIME_INODE (ip) ? mp->m_rtdev_targp : mp->m_ddev_targp;
@@ -658,47 +712,9 @@ xfs_file_dio_aio_write( *iolock =XFS_IOLOCK_EXCL ; else *iolock =XFS_IOLOCK_SHARED ; -xfs_rw_ilock (ip,XFS_ILOCK_EXCL |*iolock); - ret =generic_write_checks (file, &pos, &count, -S_ISBLK (inode->i_mode)); -if (ret) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |*iolock); - *iolock =0 ; -return ret; - } - - new_size = pos + count; -if (new_size > ip->i_size) - ip->i_new_size = new_size; - -if (likely (!(file->f_mode &FMODE_NOCMTIME ))) -file_update_time (file); - -/* -* If the offset is beyond the size of the file, we have a couple of -* things to do. First, if there is already space allocated we need to -* either create holes or zero the disk or ... -* -* If there is a page where the previous size lands, we need to zero it -* out up to the new size. -*/ -if (pos > ip->i_size) { - ret = -xfs_zero_eof (ip, pos, ip->i_size); -if (ret) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); -return ret; - } - } -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); - -/* -* If we're writing the file then make sure to clear the setuid and -* setgid bits if the process is not being run by root. This keeps -* people from modifying setuid and setgid binaries. -*/ - ret =file_remove_suid (file); -if (unlikely (ret)) + ret =xfs_file_aio_write_checks (file, &pos, &count, iolock); +if (ret)return ret;if (mapping->nrpages) {
@@ -738,38 +754,11 @@ xfs_file_buffered_aio_write(struct xfs_inode *ip =XFS_I (inode);ssize_t ret;int enospc =0 ; -xfs_fsize_t new_size;size_t count = ocount; *iolock =XFS_IOLOCK_EXCL ; -xfs_rw_ilock (ip,XFS_ILOCK_EXCL |*iolock); - - ret =generic_write_checks (file, &pos, &count, -S_ISBLK (inode->i_mode)); -if (ret) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL |*iolock); - *iolock =0 ; -return ret; - } - - new_size = pos + count; -if (new_size > ip->i_size) - ip->i_new_size = new_size; - -if (likely (!(file->f_mode &FMODE_NOCMTIME ))) -file_update_time (file); - -if (pos > ip->i_size) { - ret = -xfs_zero_eof (ip, pos, ip->i_size); -if (ret) { -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); -return ret; - } - } -xfs_rw_iunlock (ip,XFS_ILOCK_EXCL ); - - ret =file_remove_suid (file); -if (unlikely (ret)) + ret =xfs_file_aio_write_checks (file, &pos, &count, iolock); +if (ret)return ret;/* We can write back this queue in page reclaim */
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 7/8] xfs: factor common write setup code
Quoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The buffered IO and direct Io write paths share a common set of checks and limiting code prior to issuing the write. Factor that into a common helper function.
Looks good.
fs/xfs/linux-2.6/xfs_file.c
Quoted 8 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 129 ++++++++++++++++++++----------------------- 1 files changed, 59 insertions(+), 70 deletions(-)fs/xfs/linux-2.6/xfs_file.c +0
-1--- a/fs/xfs/linux-2.6 /xfs_file.c
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 7/8] xfs: factor common write setup code
In reply to Dave ChinnerQuoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The buffered IO and direct Io write paths share a common set of checks and limiting code prior to issuing the write. Factor that into a common helper function.
Wouldn't it be better to create the helper before duplicating the code, aka move it before the buffer/direct splits in the series?
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 673
Quoted 7 lines
fs/xfs/linux-2.6/xfs_file.c +7
-0+/* +* If we're writing the file then make sure to clear the setuid and +* setgid bits if the process is not being run by root. This keeps +* people from modifying setuid and setgid binaries. +*/ + error =file_remove_suid (file); +return error;
just
return file_remove_suid(file);
?
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 7/8] xfs: factor common write setup code
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The buffered IO and direct Io write paths share a common set of checks and limiting code prior to issuing the write. Factor that into a common helper function.
Wouldn't it be better to create the helper before duplicating the code, aka move it before the buffer/direct splits in the series?
Maybe, but at this I'd prefer not to move it as it would mean having to rework several other patches in the series as well. Moving it doesn't change the end result at all, so unless you feel really strongly about this I'm going to say no this time because we're now under time pressure to get this tested and finished for 2.6.38.
Quoted 11 lines
fs/xfs/linux-2.6/xfs_file.c out_lock: · line 673
Quoted 7 lines
fs/xfs/linux-2.6/xfs_file.c +7
-0+/* +* If we're writing the file then make sure to clear the setuid and +* setgid bits if the process is not being run by root. This keeps +* people from modifying setuid and setgid binaries. +*/ + error =file_remove_suid (file); +return error;just
return file_remove_suid(file);
Yup, will fix.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
PATCH 8/8 xfs: serialise unaligned direct IOs
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
When two concurrent unaligned, non-overlapping direct IOs are issued to the same block, the direct Io layer will race to zero the block. The result is that one of the concurrent IOs will overwrite data written by the other IO with zeros. This is demonstrated by the xfsqa test 240.
To avoid this problem, serialise all unaligned direct IOs to an inode with a big hammer. We need a big hammer approach as we need to serialise AIO as well, so we can't just block writes on locks. Hence, the big hammer is calling xfs_ioend_wait() while holding out other unaligned direct IOs from starting.
We don't bother trying to serialised aligned vs unaligned IOs as they are overlapping IO and the result of concurrent overlapping IOs is undefined - the result of either IO is a valid result so we let them race. Hence we only penalise unaligned IO, which already has a major overhead compared to aligned IO so this isn't a major problem.
--- fs/xfs/linux-2.6/xfs_file.c | 28 ++++++++++++++++++++++++---- 1 files changed, 24 insertions(+), 4 deletions(-)
fs/xfs/linux-2.6/xfs_file.c +24 -4
@@ -678,10 +678,21 @@ xfs_file_aio_write_checks(* xfs_file_dio_aio_write - handle direct IO writes * * Lock the inode appropriately to prepare for and issue a direct IO write. -* By spearating it from the buffered write path we remove all the tricky to +* By separating it from the buffered write path we remove all the tricky to * follow locking changes and looping. This also clearly indicates that XFS * does not fall back to buffered IO in the direct IO write path. * +* In most cases the direct IO writes will be done with IOLOCK_SHARED allowing +* them to be done in parallel with reads and other direct IO writes. However, +* if the IO is not aligned to filesystem blocks, the direct IO layer needs to +* do sub-block zeroing and that requires serialisation against other direct +* IOs to the same block. In this case we need to serialise the submission of +* the unaligned IOs so that we don't get racing block zeroing in the dio layer. +* To avoid the problem with aio, we also need to wait for outstanding IOs to +* complete so that unwritten extent conversion is completed before we try to +* map the overlapping block. This is currently implemented by hitting it +* with a big hammer (i.e. xfs_ioend_wait()). +* * Returns with locks held indicated by @iolock and errors indicated by * negative return values. */
@@ -701,6 +712,7 @@ xfs_file_dio_aio_write(struct xfs_mount *mp = ip->i_mount;ssize_t ret =0 ;size_t count = ocount; +int unaligned_io =0 ;xfs_buftarg_t *target =XFS_IS_REALTIME_INODE (ip) ? mp->m_rtdev_targp : mp->m_ddev_targp;
@@ -708,7 +720,10 @@ xfs_file_dio_aio_write(if ((pos & target->bt_smask) || (count & target->bt_smask))return -XFS_ERROR (EINVAL ); -if (mapping->nrpages || pos > ip->i_size) +if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask)) + unaligned_io =1 ; + +if (unaligned_io || mapping->nrpages || pos > ip->i_size) *iolock =XFS_IOLOCK_EXCL ;else *iolock =XFS_IOLOCK_SHARED ;
@@ -725,8 +740,13 @@ xfs_file_dio_aio_write(return ret; } -if (*iolock ==XFS_IOLOCK_EXCL ) { -/* demote the lock now the cached pages are gone */ +/* +* If we are doing unaligned IO, wait for all other IO to drain, +* otherwise demote the lock if we had to flush cached pages +*/ +if (unaligned_io) +xfs_ioend_wait (ip); +else if (*iolock ==XFS_IOLOCK_EXCL ) {xfs_rw_ilock_demote (ip,XFS_IOLOCK_EXCL ); *iolock =XFS_IOLOCK_SHARED ; }
Signature · 6 lines
1.7.2.3 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Re: [PATCH 8/8] xfs: serialise unaligned direct IOs
Quoted 14 lines · Dave Chinner
From: Dave Chinner <[email protected]>
When two concurrent unaligned, non-overlapping direct IOs are issued to the same block, the direct Io layer will race to zero the block. The result is that one of the concurrent IOs will overwrite data written by the other IO with zeros. This is demonstrated by the xfsqa test 240.
To avoid this problem, serialise all unaligned direct IOs to an inode with a big hammer. We need a big hammer approach as we need to serialise AIO as well, so we can't just block writes on locks. Hence, the big hammer is calling
xfs_ioend_wait()while holding out other unaligned direct IOs from starting.
Quoted 5 lines
We don't bother trying to serialised aligned vs unaligned IOs as they are overlapping IO and the result of concurrent overlapping IOs is undefined - the result of either IO is a valid result so we let them race. Hence we only penalise unaligned IO, which already has a major overhead compared to aligned IO so this isn't a major problem.
Wow, after the rest of this series it gets easy!
Looks good.
Quoted 4 lines
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/linux-2.6/xfs_file.c | 28 ++++++++++++++++++++++++---- 1 files changed, 24 insertions(+), 4 deletions(-)
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 37 lines · Dave Chinner
This series aims to serialise unaligned direct IOs to an inode to avoid corruption caused by sub-block zeroing races. The previous approaches at the direct IO layer fail because for !DIO_LOCKING filesystems like XFS, there is no way we can track and serialise all the direct IOs to a given inode in a race free manner. While we can track them, we cannot close the races between mapping blocks and tracked IO completion occuring before subsequent tracking lookups without adding some kind of locking to the DIO layer. Hence for !DIO_LOCKING users, unaligned direct IO needs to be serialised at a higher layer.
Because the
xfs_file_aio_write()path is so twisted and difficult to follow, adding new locking cases to the code is difficult to verify that it is correct in all cases. Hence the series starts by cleaning up the code and splitting apart the direct IO and buffered IO paths before adding the unaligned direct IO detection and serialisation.The first patch fixes a sync write error handling bug - we should consider pushing that to .38. The next patches factor code that is common to write and splice into helpers. The direct and buffered IO paths are then separated out and the common write checks and bounds limiting is factored out into a helper.
Finally, the serialisation of unaligned direct IOs is added by a big-hammer approach. That is, we take the
i_mutexandXFS_IOLOCK_EXCLand hold them across the unaligned IO submission. This means that unaligned direct IO submission is serialised, and non-AIO DIO is serialised completely.For unaligned AIO DIO, this would only serialise the submission of the DIO, leaving the sub-block zeroing races open for unaligned writes into unwritten extents. To avoid this problem, we use
xfs_ioend_wait()to ensure all AIO writes have completed before we submit the unaligned write. We do this wait holding thei_mutexso we serialise against other unaligned AIO as there is no need to serialise against aligned DIO.
Quoted 5 lines
Version 2:
- fix initial sync write error return fixup
- add new patch to abstract locking from read/write path and remove the need for theneed_i_mutexvariable.
I've reviewed this series, and the net result (the code cleanup leading to the real fix in particular) is great.
There are some small things among the patches that ought to be fixed in order to allow them to stand alone without bugs (to facilitate git bisects, for example). I've noted them in my reviews.
I also think that, having looked over them entirely, a few of my earlier comments get addressed or made non-applicable because of your changes. I'm not going to go fix them now; if you see things like that you can explain if you feel it's useful or just mention that the series renders my comment inoperative or something.
-Alex
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs