On Thu, Aug 22, 2024 at 10:44:20AM -0400, Brian Foster wrote: > In preparation for support for eof page pollution, factor out a file > size update helper. This updates the internally tracked file size > based on the upcoming operation and zeroes the appropriate range in > the good buffer for extending operations. > > Note that a handful of callers currently make these updates after > performing the associated operation. Order is not important to > current behavior, but it will be for a follow on patch, so make > those calls a bit earlier as well. > > Signed-off-by: Brian Foster <bfoster@xxxxxxxxxx> > --- > ltp/fsx.c | 57 +++++++++++++++++++++++++------------------------------ > 1 file changed, 26 insertions(+), 31 deletions(-) > > diff --git a/ltp/fsx.c b/ltp/fsx.c > index 2dc59b06..1389c51d 100644 > --- a/ltp/fsx.c > +++ b/ltp/fsx.c > @@ -983,6 +983,17 @@ gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size) > } > } > > +/* > + * Helper to update the tracked file size. If the offset begins beyond current > + * EOF, zero the range from EOF to offset in the good buffer. > + */ > +void > +update_file_size(unsigned offset, unsigned size) > +{ > + if (offset > file_size) > + memset(good_buf + file_size, '\0', offset - file_size); > + file_size = offset + size; > +} > > void > dowrite(unsigned offset, unsigned size) > @@ -1003,10 +1014,8 @@ dowrite(unsigned offset, unsigned size) > log4(OP_WRITE, offset, size, FL_NONE); > > gendata(original_buf, good_buf, offset, size); > - if (file_size < offset + size) { > - if (file_size < offset) > - memset(good_buf + file_size, '\0', offset - file_size); > - file_size = offset + size; > + if (offset + size > file_size) { > + update_file_size(offset, size); > if (lite) { > warn("Lite file size bug in fsx!"); > report_failure(149); > @@ -1070,10 +1079,8 @@ domapwrite(unsigned offset, unsigned size) > log4(OP_MAPWRITE, offset, size, FL_NONE); > > gendata(original_buf, good_buf, offset, size); > - if (file_size < offset + size) { > - if (file_size < offset) > - memset(good_buf + file_size, '\0', offset - file_size); > - file_size = offset + size; > + if (offset + size > file_size) { > + update_file_size(offset, size); > if (lite) { > warn("Lite file size bug in fsx!"); > report_failure(200); > @@ -1136,9 +1143,7 @@ dotruncate(unsigned size) > > log4(OP_TRUNCATE, 0, size, FL_NONE); > > - if (size > file_size) > - memset(good_buf + file_size, '\0', size - file_size); > - file_size = size; > + update_file_size(size, 0); > > if (testcalls <= simulatedopcount) > return; > @@ -1247,6 +1252,9 @@ do_zero_range(unsigned offset, unsigned length, int keep_size) > log4(OP_ZERO_RANGE, offset, length, > keep_size ? FL_KEEP_SIZE : FL_NONE); > > + if (end_offset > file_size) > + update_file_size(offset, length); > + > if (testcalls <= simulatedopcount) > return; Don't we only want to do the goodbuf zeroing if we don't bail out due to the (testcalls <= simulatedopcount) logic? Same question for do_clone_range and do_copy_range. /me reads the second patch but doesn't quite get it. :/ Are you doing this to mirror what the kernel does? A comment here to explain why we're doing this differently would help me. --D > > @@ -1263,17 +1271,6 @@ do_zero_range(unsigned offset, unsigned length, int keep_size) > } > > memset(good_buf + offset, '\0', length); > - > - if (!keep_size && end_offset > file_size) { > - /* > - * If there's a gap between the old file size and the offset of > - * the zero range operation, fill the gap with zeroes. > - */ > - if (offset > file_size) > - memset(good_buf + file_size, '\0', offset - file_size); > - > - file_size = end_offset; > - } > } > > #else > @@ -1538,6 +1535,9 @@ do_clone_range(unsigned offset, unsigned length, unsigned dest) > > log5(OP_CLONE_RANGE, offset, length, dest, FL_NONE); > > + if (dest + length > file_size) > + update_file_size(dest, length); > + > if (testcalls <= simulatedopcount) > return; > > @@ -1556,10 +1556,6 @@ do_clone_range(unsigned offset, unsigned length, unsigned dest) > } > > memcpy(good_buf + dest, good_buf + offset, length); > - if (dest > file_size) > - memset(good_buf + file_size, '\0', dest - file_size); > - if (dest + length > file_size) > - file_size = dest + length; > } > > #else > @@ -1756,6 +1752,9 @@ do_copy_range(unsigned offset, unsigned length, unsigned dest) > > log5(OP_COPY_RANGE, offset, length, dest, FL_NONE); > > + if (dest + length > file_size) > + update_file_size(dest, length); > + > if (testcalls <= simulatedopcount) > return; > > @@ -1792,10 +1791,6 @@ do_copy_range(unsigned offset, unsigned length, unsigned dest) > } > > memcpy(good_buf + dest, good_buf + offset, length); > - if (dest > file_size) > - memset(good_buf + file_size, '\0', dest - file_size); > - if (dest + length > file_size) > - file_size = dest + length; > } > > #else > @@ -1846,7 +1841,7 @@ do_preallocate(unsigned offset, unsigned length, int keep_size) > > if (end_offset > file_size) { > memset(good_buf + file_size, '\0', end_offset - file_size); > - file_size = end_offset; > + update_file_size(offset, length); > } > > if (testcalls <= simulatedopcount) > -- > 2.45.0 > >