gfs2_jadd: Use fallocate to preallocate journals
PATCH 1/2 gfs2_jadd: Use fallocate to preallocate journals
Fall back to writes for ancient kernels and use larger writes in that case to reduce the chance of fragmentation.
Signed-off-by Andrew Price <[email protected]>
--- gfs2/mkfs/main_jadd.c | 48 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-)
gfs2/mkfs/main_jadd.c +40 -8
@@ -480,6 +480,43 @@ static uint64_t find_block_address(int fd, off_t offset, unsigned bsize) }#endif +static int alloc_new_journal (int fd,unsigned bytes) +{ +#define ALLOC_BUF_SIZE (4 << 20) +unsigned left = bytes; +int error; +char *buf; + + error =fallocate (fd,0 ,0 , bytes); +if (error ==0 ) +return 0 ; +if (errno !=EOPNOTSUPP ) + goto out_errno; + +/* No fallocate support, fall back to writes */ + buf =calloc (1 ,ALLOC_BUF_SIZE ); +if (buf ==NULL ) + goto out_errno; + +while (left >0 ) { +unsigned sz =ALLOC_BUF_SIZE ; + +if (left <ALLOC_BUF_SIZE ) + sz = left; + +if (pwrite (fd, buf, sz, bytes - left) != sz) { +free (buf); + goto out_errno; + } + left -= sz; + } +free (buf); +return 0 ; +out_errno: +perror ("Failed to allocate space for new journal" ); +return -1 ; +} +static int add_j (struct gfs2_sbd *sdp,struct jadd_opts *opts) {int fd, error =0 ;
@@ -496,14 +533,9 @@ static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)if ((error =set_flags (fd,JA_FL_CLEAR ,FS_JOURNAL_DATA_FL ))) goto close_fd; -memset (buf,0 , sdp->bsize); -for (x=0 ; x<blocks; x++) { -if (write (fd, buf, sdp->bsize) != sdp->bsize) { -perror ("add_j write" ); - error =-1 ; - goto close_fd; - } - } + error =alloc_new_journal (fd, sdp->jsize <<20 ); +if (error !=0 ) + goto close_fd;if ((error =lseek (fd,0 ,SEEK_SET )) <0 ) {perror ("add_j lseek" );
Signature · 1 line
2.30.2
PATCH 2/2 gfs2_jadd: Don't fsync after each block written
gfs2_jadd has always called fsync() after writing each block of the journal. There doesn't seem to be any need for that so take the fsync() call out of the loop.
Add an additional fsync() after preallocation to make sure we're in good shape before writing the log headers.
In my tests this reduces the time to add one journal from 5 minutes to 9 seconds.
Signed-off-by Andrew Price <[email protected]>
--- gfs2/mkfs/main_jadd.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
gfs2/mkfs/main_jadd.c +10 -5
@@ -537,6 +537,11 @@ static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)if (error !=0 ) goto close_fd; + error =fsync (fd); +if (error !=0 ) { +perror ("Failed to sync journal metadata" ); + goto close_fd; + }if ((error =lseek (fd,0 ,SEEK_SET )) <0 ) {perror ("add_j lseek" ); goto close_fd;
@@ -580,12 +585,12 @@ static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts) seq =0 ; off += sdp->bsize; -if ((error =fsync (fd))) { -perror ("add_j fsync" ); - goto close_fd; - } } - + error =fsync (fd); +if (error !=0 ) { +perror ("Failed to sync journal metadata" ); + goto close_fd; + }sprintf (new_name,"journal%u" , opts->journals); error =rename2system (opts, opts->jindex, new_name);if (error <0 && errno !=EEXIST ){
Signature · 1 line
2.30.2