xfs: fix broken multi-fsb buffer logging
PATCH 1/1 xfs: fix broken multi-fsb buffer logging
Multi-block buffers are logged based on buffer offset in xfs_trans_log_buf(). xfs_buf_item_log() ultimately walks each mapping in the buffer and marks the associated range to be logged in the xfs_buf_log_format bitmap for that mapping. This code is broken, however, in that it marks the actual buffer offsets of the associated range in each bitmap rather than shifting to the byte range for that particular mapping.
For example, on a 4k fsb fs, buffer offset 4096 refers to the first byte of the second mapping in the buffer. This means byte 0 of the second log format bitmap should be tagged as dirty. Instead, the current code marks byte offset 4096 of the second log format bitmap, which is invalid and potentially out of range of the mapping.
As a result of this, the log item format code invoked at transaction commit time is not be able to correctly identify what parts of the buffer to copy into log vectors. This can lead to NULL log vector pointer dereferences in CIL push context if the item format code was not able to locate any dirty ranges at all. This crash has been reproduced on a 4k FSB filesystem using 16k directory blocks where an unlink operation happened not to log anything in the first block of the mapping. The logged offsets were all over 4k, marked as such in the subsequent log format mappings, and thus left the transaction with an xfs_log_item that is marked DIRTY but without any logged regions.
Further, even when the logged regions are marked correctly in the buffer log format bitmaps, the format code doesn't copy the correct ranges of the buffer into the log. This means that any logged region beyond the first block of a multi-block buffer is subject to corruption after a crash and log recovery sequence. This is due to a failure to convert the mapping bm_len field from basic blocks to bytes in the buffer offset tracking code in xfs_buf_item_format().
Update xfs_buf_item_log() to convert buffer offsets to segment relative offsets when logging multi-block buffers. This ensures that the modified regions of a buffer are logged correctly and avoids the aforementioned crash. Also update xfs_buf_item_format() to correctly track the source offset into the buffer for the log vector formatting code. This ensures that the correct data is copied into the log.
---
Note that this has only been tested so far in a particular filesystem environment that reproduces the crash/corruption discussed in the commit log description. Generic testing still required, posting just to get a jump on review...
Brian
fs/xfs/xfs_buf_item.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
fs/xfs/xfs_buf_item.c +13 -5
@@ -359,7 +359,7 @@ xfs_buf_item_format(for (i =0 ; i < bip->bli_format_count; i++) {xfs_buf_item_format_segment (bip, lv, &vecp, offset, &bip->bli_formats[i]); - offset += bp->b_maps[i].bm_len; + offset +=BBTOB (bp->b_maps[i].bm_len); }/*
@@ -915,20 +915,28 @@ xfs_buf_item_log(for (i =0 ; i < bip->bli_format_count; i++) {if (start > last)break ; - end = start +BBTOB (bp->b_maps[i].bm_len); + end = start +BBTOB (bp->b_maps[i].bm_len) -1 ; + +/* skip to the map that includes the first byte to log */ if (first > end) { start +=BBTOB (bp->b_maps[i].bm_len);continue ; } + +/* +* Trim the range to this segment and mark it in the bitmap. +* Note that we must convert buffer offsets to segment relative +* offsets (e.g., the first byte of each segment is byte 0 of +* that segment). +*/ if (first < start) first = start;if (end > last) end = last; - -xfs_buf_item_log_segment (first, end, +xfs_buf_item_log_segment (first - start, end - start, &bip->bli_formats[i].blf_data_map[0 ]); - start += bp->b_maps[i].bm_len; + start +=BBTOB (bp->b_maps[i].bm_len); } }
Signature · 6 lines
2.4.11 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 40 lines · Brian Foster
Multi-block buffers are logged based on buffer offset in
xfs_trans_log_buf().xfs_buf_item_log()ultimately walks each mapping in the buffer and marks the associated range to be logged in thexfs_buf_log_formatbitmap for that mapping. This code is broken, however, in that it marks the actual buffer offsets of the associated range in each bitmap rather than shifting to the byte range for that particular mapping.For example, on a 4k fsb fs, buffer offset 4096 refers to the first byte of the second mapping in the buffer. This means byte 0 of the second log format bitmap should be tagged as dirty. Instead, the current code marks byte offset 4096 of the second log format bitmap, which is invalid and potentially out of range of the mapping.
As a result of this, the log item format code invoked at transaction commit time is not be able to correctly identify what parts of the buffer to copy into log vectors. This can lead to NULL log vector pointer dereferences in CIL push context if the item format code was not able to locate any dirty ranges at all. This crash has been reproduced on a 4k FSB filesystem using 16k directory blocks where an unlink operation happened not to log anything in the first block of the mapping. The logged offsets were all over 4k, marked as such in the subsequent log format mappings, and thus left the transaction with an
xfs_log_itemthat is marked DIRTY but without any logged regions.Further, even when the logged regions are marked correctly in the buffer log format bitmaps, the format code doesn't copy the correct ranges of the buffer into the log. This means that any logged region beyond the first block of a multi-block buffer is subject to corruption after a crash and log recovery sequence. This is due to a failure to convert the mapping
bm_lenfield from basic blocks to bytes in the buffer offset tracking code inxfs_buf_item_format().Update
xfs_buf_item_log()to convert buffer offsets to segment relative offsets when logging multi-block buffers. This ensures that the modified regions of a buffer are logged correctly and avoids the aforementioned crash. Also updatexfs_buf_item_format()to correctly track the source offset into the buffer for the log vector formatting code. This ensures that the correct data is copied into the log.
Quoted 8 lines
Signed-off-by Brian Foster <[email protected]>---
Note that this has only been tested so far in a particular filesystem environment that reproduces the crash/corruption discussed in the commit log description. Generic testing still required, posting just to get a jump on review...
This has passed my tests without any obvious problems or regressions. Any other thoughts/comments or reviews? I'd like to get a backport going for this, but can't do so until there is at least some kind of acknowledgement this is acceptable.
Brian
Quoted 58 lines
Brian
fs/xfs/xfs_buf_item.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)fs/xfs/xfs_buf_item.c +13
-5@@ -359,7 +359,7 @@ xfs_buf_item_format(for (i =0 ; i < bip->bli_format_count; i++) {xfs_buf_item_format_segment (bip, lv, &vecp, offset, &bip->bli_formats[i]); - offset += bp->b_maps[i].bm_len; + offset +=BBTOB (bp->b_maps[i].bm_len); }/* @@ -915,20 +915,28 @@ xfs_buf_item_log(for (i =0 ; i < bip->bli_format_count; i++) {if (start > last)break ; - end = start +BBTOB (bp->b_maps[i].bm_len); + end = start +BBTOB (bp->b_maps[i].bm_len) -1 ; + +/* skip to the map that includes the first byte to log */ if (first > end) { start +=BBTOB (bp->b_maps[i].bm_len);continue ; } + +/* +* Trim the range to this segment and mark it in the bitmap. +* Note that we must convert buffer offsets to segment relative +* offsets (e.g., the first byte of each segment is byte 0 of +* that segment). +*/ if (first < start) first = start;if (end > last) end = last; - -xfs_buf_item_log_segment (first, end, +xfs_buf_item_log_segment (first - start, end - start, &bip->bli_formats[i].blf_data_map[0 ]); - start += bp->b_maps[i].bm_len; + start +=BBTOB (bp->b_maps[i].bm_len); } }Signature · 6 lines
2.4.11 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 13 lines · Brian Foster
Quoted 2 lines
...
This has passed my tests without any obvious problems or regressions. Any other thoughts/comments or reviews? I'd like to get a backport going for this, but can't do so until there is at least some kind of acknowledgement this is acceptable.
Brian
Sorry Brian, I meant to review this last Friday, but got wrapped up in other things… My first pass over it looks fine, I just want to give it a little more in depth review, and I will do my best to get that done tomorrow.
Eric
Quoted 1 line
Quoted 1 line
Brian
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 5 lines · Brian Foster
Multi-block buffers are logged based on buffer offset in
xfs_trans_log_buf().xfs_buf_item_log()ultimately walks each mapping in the buffer and marks the associated range to be logged in thexfs_buf_log_formatbitmap for that mapping. This code is broken, however, [....]
[snip description I've not read, and go look at the code changes]
fs/xfs/xfs_buf_item.c xfs_buf_item_format( · line 363
Quoted 11 lines
fs/xfs/xfs_buf_item.c +1
-1@@ -359,7 +359,7 @@ xfs_buf_item_format(for (i =0 ; i < bip->bli_format_count; i++) {xfs_buf_item_format_segment (bip, lv, &vecp, offset, &bip->bli_formats[i]); - offset += bp->b_maps[i].bm_len; + offset +=BBTOB (bp->b_maps[i].bm_len); }
Ok, that means the offset into the multi-fsb buffer is wrong for the second and subsequent chunks, resulting in copying the wrong range into the log vector. Offset should be in bytes, so this is a valid fix all by itself.
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 918
Quoted 8 lines
fs/xfs/xfs_buf_item.c +1
-1/* @@ -915,20 +915,28 @@ xfs_buf_item_log(for (i =0 ; i < bip->bli_format_count; i++) {if (start > last)break ; - end = start +BBTOB (bp->b_maps[i].bm_len); + end = start +BBTOB (bp->b_maps[i].bm_len) -1 ;
Ok, buffer starts at offset 0, last byte in buffer is length - 1. Yup, there's an off-by-one there - end points to the offset of the first byte of the next buffer. This will screw up the range trimming done below. I think this is probably harmless as it doesn't impact the next iteration of the loop, but still good to get it right.
Quoted 11 lines
fs/xfs/xfs_buf_item.c +7
-0+ +/* skip to the map that includes the first byte to log */ if (first > end) { start +=BBTOB (bp->b_maps[i].bm_len);continue ; } + +/* +* Trim the range to this segment and mark it in the bitmap. +* Note that we must convert buffer offsets to segment relative +* offsets (e.g., the first byte of each segment is byte 0 of
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 937
Quoted 10 lines
fs/xfs/xfs_buf_item.c +3
-2+* that segment). +*/ if (first < start) first = start;if (end > last) end = last; - -xfs_buf_item_log_segment (first, end, +xfs_buf_item_log_segment (first - start, end - start, &bip->bli_formats[i].blf_data_map[0 ]);
Ok so we pass offsets first and end as the range that needs to be logged in the segment, and start is the byte offset of the first byte of data in the current buffer segment.
/me looks at xfs_buf_item_log_segment()
It sets bits in the format item based on offsets passed in. That means if the start is non-zero (i.e. we're in the second buffer range) we'll set bits in the dirty mask that map to first/end rather than 0/buffer len.
/me looks at xfs_buf_item_size, follows blf_map_size to it's initialisation, sees that first/end bits will be beyond blf_map_size for second+ buffer range and so never seen by xfs_buf_item_size().
Hence subtracting start normalises the range being logged to offsets within the buffer range, and so now xfs_buf_item_size (and hence xfs_buf_item_format) will see them correctly as dirty.
Yes, that change looks correct.
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 939
Quoted 3 lines
fs/xfs/xfs_buf_item.c +1
-1- start += bp->b_maps[i].bm_len; + start +=BBTOB (bp->b_maps[i].bm_len);
Yup, same as the first hunk in the patch.
So from the code perspective the change looks correct. I've looked over all the other users of bp->b_maps[i].bm_len and loops over bli_format_count and I can't see any other obvious problems. I'm going to leave this under test overnight and see if anything pops up...
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 58 lines · Brian Foster
Multi-block buffers are logged based on buffer offset in
xfs_trans_log_buf().xfs_buf_item_log()ultimately walks each mapping in the buffer and marks the associated range to be logged in thexfs_buf_log_formatbitmap for that mapping. This code is broken, however, in that it marks the actual buffer offsets of the associated range in each bitmap rather than shifting to the byte range for that particular mapping.For example, on a 4k fsb fs, buffer offset 4096 refers to the first byte of the second mapping in the buffer. This means byte 0 of the second log format bitmap should be tagged as dirty. Instead, the current code marks byte offset 4096 of the second log format bitmap, which is invalid and potentially out of range of the mapping.
As a result of this, the log item format code invoked at transaction commit time is not be able to correctly identify what parts of the buffer to copy into log vectors. This can lead to NULL log vector pointer dereferences in CIL push context if the item format code was not able to locate any dirty ranges at all. This crash has been reproduced on a 4k FSB filesystem using 16k directory blocks where an unlink operation happened not to log anything in the first block of the mapping. The logged offsets were all over 4k, marked as such in the subsequent log format mappings, and thus left the transaction with an
xfs_log_itemthat is marked DIRTY but without any logged regions.Further, even when the logged regions are marked correctly in the buffer log format bitmaps, the format code doesn't copy the correct ranges of the buffer into the log. This means that any logged region beyond the first block of a multi-block buffer is subject to corruption after a crash and log recovery sequence. This is due to a failure to convert the mapping
bm_lenfield from basic blocks to bytes in the buffer offset tracking code inxfs_buf_item_format().Update
xfs_buf_item_log()to convert buffer offsets to segment relative offsets when logging multi-block buffers. This ensures that the modified regions of a buffer are logged correctly and avoids the aforementioned crash. Also updatexfs_buf_item_format()to correctly track the source offset into the buffer for the log vector formatting code. This ensures that the correct data is copied into the log.Signed-off-by Brian Foster <[email protected]>---
Note that this has only been tested so far in a particular filesystem environment that reproduces the crash/corruption discussed in the commit log description. Generic testing still required, posting just to get a jump on review...
Brian
fs/xfs/xfs_buf_item.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)fs/xfs/xfs_buf_item.c +0
-0
fs/xfs/xfs_buf_item.c xfs_buf_item_format( · line 362
Quoted 5 lines
fs/xfs/xfs_buf_item.c +1
-1@@ -359,7 +359,7 @@ xfs_buf_item_format(for (i =0 ; i < bip->bli_format_count; i++) {xfs_buf_item_format_segment (bip, lv, &vecp, offset, &bip->bli_formats[i]); - offset += bp->b_maps[i].bm_len; + offset +=BBTOB (bp->b_maps[i].bm_len);
Yep this and the other instance in the other hunk look obviously correct.
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 919
Quoted 10 lines
fs/xfs/xfs_buf_item.c +2
-1}/* @@ -915,20 +915,28 @@ xfs_buf_item_log(for (i =0 ; i < bip->bli_format_count; i++) {if (start > last)break ; - end = start +BBTOB (bp->b_maps[i].bm_len); + end = start +BBTOB (bp->b_maps[i].bm_len) -1 ; +
So let's say start is 0, and len is 8, just making up nice numbers. That means a range of 0 through 7, with the next one starting at 8. 7 is the last block, so if "first" is beyond that, we need to skip. Makes sense.
Quoted 10 lines
fs/xfs/xfs_buf_item.c +6
-0+/* skip to the map that includes the first byte to log */ if (first > end) { start +=BBTOB (bp->b_maps[i].bm_len);continue ; } + +/* +* Trim the range to this segment and mark it in the bitmap. +* Note that we must convert buffer offsets to segment relative +* offsets (e.g., the first byte of each segment is byte 0 of
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 937
Quoted 10 lines
fs/xfs/xfs_buf_item.c +3
-2+* that segment). +*/ if (first < start) first = start;if (end > last) end = last; - -xfs_buf_item_log_segment (first, end, +xfs_buf_item_log_segment (first - start, end - start, &bip->bli_formats[i].blf_data_map[0 ]);
I guess this is the trickiest part.
in the called function, we do:
/*
* Get a pointer to the first word in the bitmap
* to set a bit in.
*/
word_num = first_bit >> BIT_TO_WORD_SHIFT;
wordp = &map[word_num];where first_bit is derived from the passed-in "first" argument (in bytes). So we're marking bits in map[]; this skips us out into the word_num index in map[]. But what's map?
The map[] we passed in is &bip->bli_formats[i].blf_data_map[0], where 'i' is the i'th segment. So yes, this needs to be segment-relative bytes, whereas start (and therefore end) are offsets in the whole buffer. So yes, this makes sense to me as well.
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 939
Quoted 3 lines
fs/xfs/xfs_buf_item.c +1
-1- start += bp->b_maps[i].bm_len; + start +=BBTOB (bp->b_maps[i].bm_len);
Again obviously correct.
fs/xfs/xfs_buf_item.c xfs_buf_item_log( · line 942
Quoted 4 lines
fs/xfs/xfs_buf_item.c +0
-0} }
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Dave Chinner
Reviewed-byQuoted 8 lines · Dave Chinner
Quoted 5 lines · Brian Foster
Multi-block buffers are logged based on buffer offset in
xfs_trans_log_buf().xfs_buf_item_log()ultimately walks each mapping in the buffer and marks the associated range to be logged in thexfs_buf_log_formatbitmap for that mapping. This code is broken, however, [....][snip description I've not read, and go look at the code changes]
....
Quoted 5 lines
So from the code perspective the change looks correct. I've looked over all the other users of bp->b_maps[i].bm_len and loops over
bli_format_countand I can't see any other obvious problems. I'm going to leave this under test overnight and see if anything pops up...
Passes all the tests I've thrown at it. multiple xfstests runs on multiple machines with dir block sizes from 16k to 64k, fsmark runs with 16k and 64k block sizes, dbench stress loops with up to 500 processes running at once, etc.
I'll commit this when I get to updating the xfs trees now that the merge window has closed and 4.7-rc1 is out. Hopefully that will be later today....
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs