* We already are pretty sure we can and want to
* defragment the file. Create the tmp file, copy
* the data (maintaining holes) and call the kernel
- * extent swap routinte.
+ * extent swap routine.
+ *
+ * Return values:
+ * -1: Some error was encountered
+ * 0: Successfully defragmented the file
+ * 1: No change / No Error
*/
static int
packfile(char *fname, char *tname, int fd,
xfs_bstat_t *statp, struct fsxattr *fsxp)
{
- int tfd;
+ int tfd = -1;
int srval;
+ int retval = -1; /* Failure is the default */
int nextents, extent, cur_nextents, new_nextents;
unsigned blksz_dio;
unsigned dio_min;
static xfs_swapext_t sx;
struct xfs_flock64 space;
off64_t cnt, pos;
- void *fbuf;
+ void *fbuf = NULL;
int ct, wc, wc_b4;
char ffname[SMBUFSZ];
int ffd = -1;
if (cur_nextents == 1 || cur_nextents <= nextents) {
if (vflag)
fsrprintf(_("%s already fully defragmented.\n"), fname);
- return 1; /* indicates no change/no error */
+ retval = 1; /* indicates no change/no error */
+ goto out;
}
if (dflag)
if (vflag)
fsrprintf(_("could not open tmp file: %s: %s\n"),
tname, strerror(errno));
- return -1;
+ goto out;
}
unlink(tname);
/* Setup extended attributes */
if (fsr_setup_attr_fork(fd, tfd, statp) != 0) {
fsrprintf(_("failed to set ATTR fork on tmp: %s:\n"), tname);
- close(tfd);
- return -1;
+ goto out;
}
/* Setup extended inode flags, project identifier, etc */
if (ioctl(tfd, XFS_IOC_FSSETXATTR, fsxp) < 0) {
fsrprintf(_("could not set inode attrs on tmp: %s\n"),
tname);
- close(tfd);
- return -1;
+ goto out;
}
}
if ((ioctl(tfd, XFS_IOC_DIOINFO, &dio)) < 0 ) {
fsrprintf(_("could not get DirectIO info on tmp: %s\n"), tname);
- close(tfd);
- return -1;
+ goto out;
}
dio_min = dio.d_miniosz;
if (!(fbuf = (char *)memalign(dio.d_mem, blksz_dio))) {
fsrprintf(_("could not allocate buf: %s\n"), tname);
- close(tfd);
- return -1;
+ goto out;
}
if (nfrags) {
if ((ffd = open(ffname, openopts, 0666)) < 0) {
fsrprintf(_("could not open fragfile: %s : %s\n"),
ffname, strerror(errno));
- close(tfd);
- free(fbuf);
- return -1;
+ goto out;
}
unlink(ffname);
}
if (ioctl(tfd, XFS_IOC_RESVSP64, &space) < 0) {
fsrprintf(_("could not pre-allocate tmp space:"
" %s\n"), tname);
- close(tfd);
- free(fbuf);
- return -1;
+ goto out;
}
lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR);
}
if (lseek64(tfd, 0, SEEK_SET)) {
fsrprintf(_("Couldn't rewind on temporary file\n"));
- close(tfd);
- if (ffd != -1)
- close(ffd);
- free(fbuf);
- return -1;
+ goto out;
}
/* Check if the temporary file has fewer extents */
if (cur_nextents <= new_nextents) {
if (vflag)
fsrprintf(_("No improvement will be made (skipping): %s\n"), fname);
- free(fbuf);
- close(tfd);
- if (ffd != -1)
- close(ffd);
- return 1; /* no change/no error */
+ retval = 1; /* no change/no error */
+ goto out;
}
/* Loop through block map copying the file. */
tname);
}
}
- free(fbuf);
- close(tfd);
- if (ffd != -1)
- close(ffd);
- return -1;
+ goto out;
}
if (nfrags) {
/* Do a matching write to the tmp file */
}
}
ftruncate64(tfd, statp->bs_size);
- if (ffd != -1)
- close(ffd);
fsync(tfd);
- free(fbuf);
-
sx.sx_stat = *statp; /* struct copy */
sx.sx_version = XFS_SX_VERSION;
sx.sx_fdtarget = fd;
if (vflag)
fsrprintf(_("failed to fchown tmpfile %s: %s\n"),
tname, strerror(errno));
- close(tfd);
- return -1;
+ goto out;
}
/* Swap the extents */
fsrprintf(_("XFS_IOC_SWAPEXT failed: %s: %s\n"),
fname, strerror(errno));
}
- close(tfd);
- return -1;
+ goto out;
}
/* Report progress */
cur_nextents, new_nextents,
(new_nextents <= nextents ? "DONE" : " " ),
fname);
- close(tfd);
- return 0;
+ retval = 0;
+
+out:
+ free(fbuf);
+ if (tfd != -1)
+ close(tfd);
+ if (ffd != -1)
+ close(ffd);
+ return retval;
}
char *