]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - fsr/xfs_fsr.c
xfs_repair: clear extent size hints when clearing inode core
[thirdparty/xfsprogs-dev.git] / fsr / xfs_fsr.c
index 6852b3f7aa2e8841d8a79abfd53e9c851ee3856a..b74a70b59df6dec5e97576fa53b8d77ee3207d44 100644 (file)
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <xfs/xfs.h>
-#include <libxfs.h>
-#include <xfs/xfs_types.h>
-#include <xfs/jdm.h>
-#include <xfs/xfs_dfrag.h>
-#include <xfs/xfs_bmap_btree.h>
-#include <xfs/xfs_dinode.h>
-#include <xfs/xfs_attr_sf.h>
+#include "libxfs.h"
+#include "xfs.h"
+#include "xfs_types.h"
+#include "jdm.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_attr_sf.h"
+#include "path.h"
 
 #include <fcntl.h>
 #include <errno.h>
-#include <malloc.h>
-#include <mntent.h>
 #include <syslog.h>
 #include <signal.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
-#include <sys/vfs.h>
 #include <sys/statvfs.h>
 #include <sys/xattr.h>
-
-
-#ifndef XFS_XFLAG_NODEFRAG
-#define XFS_XFLAG_NODEFRAG 0x00002000 /* src dependancy, remove later */
-#endif
+#include <paths.h>
 
 #define _PATH_FSRLAST          "/var/tmp/.fsrlast_xfs"
 #define _PATH_PROC_MOUNTS      "/proc/mounts"
@@ -63,7 +55,7 @@ struct getbmap  *outmap = NULL;
 int             outmap_size = 0;
 int            RealUid;
 int            tmp_agi;
-static __int64_t       minimumfree = 2048;
+static int64_t         minimumfree = 2048;
 
 #define MNTTYPE_XFS             "xfs"
 
@@ -77,7 +69,6 @@ static __int64_t      minimumfree = 2048;
 #define        V_ALL           2
 #define BUFFER_SIZE    (1<<16)
 #define BUFFER_MAX     (1<<24)
-#define min(x, y) ((x) < (y) ? (x) : (y))
 
 static time_t howlong = 7200;          /* default seconds of reorganizing */
 static char *leftofffile = _PATH_FSRLAST; /* where we left off last */
@@ -99,7 +90,7 @@ static void fsrallfs(char *mtab, int howlong, char *leftofffile);
 static void fsrall_cleanup(int timeout);
 static int  getnextents(int);
 int xfsrtextsize(int fd);
-int xfs_getrt(int fd, struct statvfs64 *sfbp);
+int xfs_getrt(int fd, struct statvfs *sfbp);
 char * gettmpname(char *fname);
 char * getparent(char *fname);
 int fsrprintf(const char *fmt, ...);
@@ -177,70 +168,13 @@ aborter(int unused)
        exit(1);
 }
 
-/*
- * Check if the argument is either the device name or mountpoint of an XFS
- * filesystem.  Note that we do not care about bind mounted regular files
- * here - the code that handles defragmentation of invidual files takes care
- * of that.
- */
-static char *
-find_mountpoint(char *mtab, char *argname, struct stat64 *sb)
-{
-       struct mntent *t;
-       struct stat64 ms;
-       FILE *mtabp;
-       char *mntp = NULL;
-
-       mtabp = setmntent(mtab, "r");
-       if (!mtabp) {
-               fprintf(stderr, _("%s: cannot read %s\n"),
-                       progname, mtab);
-               exit(1);
-       }
-
-       while ((t = getmntent(mtabp))) {
-               if (S_ISDIR(sb->st_mode)) {             /* mount point */
-                       if (stat64(t->mnt_dir, &ms) < 0)
-                               continue;
-                       if (sb->st_ino != ms.st_ino)
-                               continue;
-                       if (sb->st_dev != ms.st_dev)
-                               continue;
-                       if (strcmp(t->mnt_type, MNTTYPE_XFS) != 0)
-                               continue;
-               } else {                                /* device */
-                       struct stat64 sb2;
-
-                       if (stat64(t->mnt_fsname, &ms) < 0)
-                               continue;
-                       if (sb->st_rdev != ms.st_rdev)
-                               continue;
-                       if (strcmp(t->mnt_type, MNTTYPE_XFS) != 0)
-                               continue;
-
-                       /*
-                        * Make sure the mountpoint given by mtab is accessible
-                        * before using it.
-                        */
-                       if (stat64(t->mnt_dir, &sb2) < 0)
-                               continue;
-               }
-
-               mntp = t->mnt_dir;
-               break;
-       }
-
-       endmntent(mtabp);
-       return mntp;
-}
-
 int
 main(int argc, char **argv)
 {
-       struct stat64 sb;
+       struct stat sb;
        char *argname;
        int c;
-       char *mntp;
+       struct fs_path  *fsp;
        char *mtab = NULL;
 
        setlinebuf(stdout);
@@ -329,12 +263,12 @@ main(int argc, char **argv)
        RealUid = getuid();
 
        pagesize = getpagesize();
-
+       fs_table_initialise(0, NULL, 0, NULL);
        if (optind < argc) {
                for (; optind < argc; optind++) {
                        argname = argv[optind];
 
-                       if (lstat64(argname, &sb) < 0) {
+                       if (lstat(argname, &sb) < 0) {
                                fprintf(stderr,
                                        _("%s: could not stat: %s: %s\n"),
                                        progname, argname, strerror(errno));
@@ -342,17 +276,19 @@ main(int argc, char **argv)
                        }
 
                        if (S_ISLNK(sb.st_mode)) {
-                               struct stat64 sb2;
+                               struct stat sb2;
 
-                               if (stat64(argname, &sb2) == 0 &&
+                               if (stat(argname, &sb2) == 0 &&
                                    (S_ISBLK(sb2.st_mode) ||
                                     S_ISCHR(sb2.st_mode)))
                                sb = sb2;
                        }
 
-                       mntp = find_mountpoint(mtab, argname, &sb);
-                       if (mntp != NULL) {
-                               fsrfs(mntp, 0, 100);
+                       fsp = fs_table_lookup_mount(argname);
+                       if (!fsp)
+                               fsp = fs_table_lookup_blkdev(argname);
+                       if (fsp != NULL) {
+                               fsrfs(fsp->fs_dir, 0, 100);
                        } else if (S_ISCHR(sb.st_mode)) {
                                fprintf(stderr, _(
                                        "%s: char special not supported: %s\n"),
@@ -408,17 +344,11 @@ usage(int ret)
 static void
 initallfs(char *mtab)
 {
-       FILE *fp;
-       struct mntent *mp;
+       struct mntent_cursor cursor;
+       struct mntent *mnt= NULL;
        int mi;
        char *cp;
-       struct stat64 sb;
-
-       fp = setmntent(mtab, "r");
-       if (fp == NULL) {
-               fsrprintf(_("could not open mtab file: %s\n"), mtab);
-               exit(1);
-       }
+       struct stat sb;
 
        /* malloc a number of descriptors, increased later if needed */
        if (!(fsbase = (fsdesc_t *)malloc(fsbufsize * sizeof(fsdesc_t)))) {
@@ -430,15 +360,21 @@ initallfs(char *mtab)
        /* find all rw xfs file systems */
        mi = 0;
        fs = fsbase;
-       while ((mp = getmntent(fp))) {
+
+       if (platform_mntent_open(&cursor, mtab) != 0){
+               fprintf(stderr, "Error: can't get mntent entries.\n");
+               exit(1);
+       }
+
+       while ((mnt = platform_mntent_next(&cursor)) != NULL) {
                int rw = 0;
 
-               if (strcmp(mp->mnt_type, MNTTYPE_XFS ) != 0 ||
-                   stat64(mp->mnt_fsname, &sb) == -1 ||
+               if (strcmp(mnt->mnt_type, MNTTYPE_XFS ) != 0 ||
+                   stat(mnt->mnt_fsname, &sb) == -1 ||
                    !S_ISBLK(sb.st_mode))
                        continue;
 
-               cp = strtok(mp->mnt_opts,",");
+               cp = strtok(mnt->mnt_opts,",");
                do {
                        if (strcmp("rw", cp) == 0)
                                rw++;
@@ -446,7 +382,7 @@ initallfs(char *mtab)
                if (rw == 0) {
                        if (dflag)
                                fsrprintf(_("Skipping %s: not mounted rw\n"),
-                                       mp->mnt_fsname);
+                                       mnt->mnt_fsname);
                        continue;
                }
 
@@ -466,23 +402,24 @@ initallfs(char *mtab)
                        fs = (fsbase + mi);  /* Needed ? */
                }
 
-               fs->dev = strdup(mp->mnt_fsname);
-               fs->mnt = strdup(mp->mnt_dir);
+               fs->dev = strdup(mnt->mnt_fsname);
+               fs->mnt = strdup(mnt->mnt_dir);
 
                if (fs->dev == NULL) {
-                       fsrprintf(_("strdup(%s) failed\n"), mp->mnt_fsname);
+                       fsrprintf(_("strdup(%s) failed\n"), mnt->mnt_fsname);
                        exit(1);
                }
                if (fs->mnt == NULL) {
-                       fsrprintf(_("strdup(%s) failed\n"), mp->mnt_dir);
+                       fsrprintf(_("strdup(%s) failed\n"), mnt->mnt_dir);
                        exit(1);
                }
                mi++;
                fs++;
        }
+       platform_mntent_close(&cursor);
+
        numfs = mi;
        fsend = (fsbase + numfs);
-       endmntent(fp);
        if (numfs == 0) {
                fsrprintf(_("no rw xfs file systems in mtab: %s\n"), mtab);
                exit(0);
@@ -508,7 +445,7 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
        char *ptr;
        xfs_ino_t startino = 0;
        fsdesc_t *fsp;
-       struct stat64 sb, sb2;
+       struct stat sb, sb2;
 
        fsrprintf("xfs_fsr -m %s -t %d -f %s ...\n", mtab, howlong, leftofffile);
 
@@ -516,11 +453,11 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
        fs = fsbase;
 
        /* where'd we leave off last time? */
-       if (lstat64(leftofffile, &sb) == 0) {
+       if (lstat(leftofffile, &sb) == 0) {
                if ( (fd = open(leftofffile, O_RDONLY)) == -1 ) {
                        fsrprintf(_("%s: open failed\n"), leftofffile);
                }
-               else if ( fstat64(fd, &sb2) == 0) {
+               else if ( fstat(fd, &sb2) == 0) {
                        /*
                         * Verify that lstat & fstat point to the
                         * same regular file (no links/no quick spoofs)
@@ -556,6 +493,8 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
                        fsrprintf(_("could not read %s, starting with %s\n"),
                                leftofffile, *fs->dev);
                } else {
+                       /* Ensure the buffer we read is null terminated */
+                       buf[SMBUFSZ-1] = '\0';
                        for (fs = fsbase; fs < fsend; fs++) {
                                fsname = fs->dev;
                                if ((strncmp(buf,fsname,strlen(fsname)) == 0)
@@ -604,12 +543,7 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
        /* reorg for 'howlong' -- checked in 'fsrfs' */
        while (endtime > time(0)) {
                pid_t pid;
-               if (fs == fsend)
-                       fs = fsbase;
-               if (fs->npass == npasses) {
-                       fsrprintf(_("Completed all %d passes\n"), npasses);
-                       break;
-               }
+
                if (npasses > 1 && !fs->npass)
                        Mflag = 1;
                else
@@ -635,6 +569,12 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
                startino = 0;  /* reset after the first time through */
                fs->npass++;
                fs++;
+               if (fs == fsend)
+                       fs = fsbase;
+               if (fs->npass == npasses) {
+                       fsrprintf(_("Completed all %d passes\n"), npasses);
+                       break;
+               }
        }
        fsrall_cleanup(endtime <= time(0));
 }
@@ -649,14 +589,19 @@ fsrall_cleanup(int timeout)
        int ret;
        char buf[SMBUFSZ];
 
-       /* record where we left off */
        unlink(leftofffile);
-       fd = open(leftofffile, O_WRONLY|O_CREAT|O_EXCL, 0644);
-       if (fd == -1)
-               fsrprintf(_("open(%s) failed: %s\n"),
-                         leftofffile, strerror(errno));
-       else {
-               if (timeout) {
+
+       if (timeout) {
+               fsrprintf(_("%s startpass %d, endpass %d, time %d seconds\n"),
+                       progname, startpass, fs->npass,
+                       time(0) - endtime + howlong);
+
+               /* record where we left off */
+               fd = open(leftofffile, O_WRONLY|O_CREAT|O_EXCL, 0644);
+               if (fd == -1) {
+                       fsrprintf(_("open(%s) failed: %s\n"),
+                                 leftofffile, strerror(errno));
+               } else {
                        ret = sprintf(buf, "%s %d %llu\n", fs->dev,
                                fs->npass, (unsigned long long)leftoffino);
                        if (write(fd, buf, ret) < strlen(buf))
@@ -665,11 +610,6 @@ fsrall_cleanup(int timeout)
                        close(fd);
                }
        }
-
-       if (timeout)
-               fsrprintf(_("%s startpass %d, endpass %d, time %d seconds\n"),
-                       progname, startpass, fs->npass,
-                       time(0) - endtime + howlong);
 }
 
 /*
@@ -702,19 +642,22 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
        if ((fsfd = open(mntdir, O_RDONLY)) < 0) {
                fsrprintf(_("unable to open: %s: %s\n"),
                          mntdir, strerror( errno ));
+               free(fshandlep);
                return -1;
        }
 
        if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
                fsrprintf(_("Skipping %s: could not get XFS geometry\n"),
                          mntdir);
+               close(fsfd);
+               free(fshandlep);
                return -1;
        }
 
        tmp_init(mntdir);
 
        while ((ret = xfs_bulkstat(fsfd,
-                               &lastino, GRABSZ, &buf[0], &buflenout) == 0)) {
+                               &lastino, GRABSZ, &buf[0], &buflenout)) == 0) {
                xfs_bstat_t *p;
                xfs_bstat_t *endp;
 
@@ -732,7 +675,8 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                             (p->bs_extents < 2))
                                continue;
 
-                       if ((fd = jdm_open(fshandlep, p, O_RDWR)) < 0) {
+                       fd = jdm_open(fshandlep, p, O_RDWR|O_DIRECT);
+                       if (fd < 0) {
                                /* This probably means the file was
                                 * removed while in progress of handling
                                 * it.  Just quietly ignore this file.
@@ -772,6 +716,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
 out0:
        tmp_close(mntdir);
        close(fsfd);
+       free(fshandlep);
        return 0;
 }
 
@@ -807,15 +752,15 @@ fsrfile(char *fname, xfs_ino_t ino)
 {
        xfs_bstat_t     statbuf;
        jdm_fshandle_t  *fshandlep;
-       int     fd, fsfd;
-       int     error = 0;
+       int     fd = -1, fsfd = -1;
+       int     error = -1;
        char    *tname;
 
        fshandlep = jdm_getfshandle(getparent (fname) );
-       if (! fshandlep) {
+       if (!fshandlep) {
                fsrprintf(_("unable to construct sys handle for %s: %s\n"),
                        fname, strerror(errno));
-               return -1;
+               goto out;
        }
 
        /*
@@ -826,39 +771,39 @@ fsrfile(char *fname, xfs_ino_t ino)
        if (fsfd < 0) {
                fsrprintf(_("unable to open sys handle for %s: %s\n"),
                        fname, strerror(errno));
-               return -1;
+               goto out;
        }
 
        if ((xfs_bulkstat_single(fsfd, &ino, &statbuf)) < 0) {
                fsrprintf(_("unable to get bstat on %s: %s\n"),
                        fname, strerror(errno));
-               close(fsfd);
-               return -1;
+               goto out;
        }
 
-       fd = jdm_open( fshandlep, &statbuf, O_RDWR);
+       fd = jdm_open(fshandlep, &statbuf, O_RDWR|O_DIRECT);
        if (fd < 0) {
                fsrprintf(_("unable to open handle %s: %s\n"),
                        fname, strerror(errno));
-               close(fsfd);
-               return -1;
+               goto out;
        }
 
        /* Get the fs geometry */
        if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
                fsrprintf(_("Unable to get geom on fs for: %s\n"), fname);
-               close(fsfd);
-               return -1;
+               goto out;
        }
 
-       close(fsfd);
-
        tname = gettmpname(fname);
 
        if (tname)
                error = fsrfile_common(fname, tname, NULL, fd, &statbuf);
 
-       close(fd);
+out:
+       if (fsfd >= 0)
+               close(fsfd);
+       if (fd >= 0)
+               close(fd);
+       free(fshandlep);
 
        return error;
 }
@@ -887,7 +832,7 @@ fsrfile_common(
        xfs_bstat_t     *statp)
 {
        int             error;
-       struct statvfs64 vfss;
+       struct statvfs  vfss;
        struct fsxattr  fsx;
        unsigned long   bsize;
 
@@ -939,7 +884,7 @@ fsrfile_common(
         * Note that xfs_bstat.bs_blksize returns the filesystem blocksize,
         * not the optimal I/O size as struct stat.
         */
-       if (statvfs64(fsname ? fsname : fname, &vfss) < 0) {
+       if (statvfs(fsname ? fsname : fname, &vfss) < 0) {
                fsrprintf(_("unable to get fs stat on %s: %s\n"),
                        fname, strerror(errno));
                return -1;
@@ -953,22 +898,22 @@ fsrfile_common(
                return 1;
        }
 
-       if ((ioctl(fd, XFS_IOC_FSGETXATTR, &fsx)) < 0) {
+       if ((ioctl(fd, FS_IOC_FSGETXATTR, &fsx)) < 0) {
                fsrprintf(_("failed to get inode attrs: %s\n"), fname);
                return(-1);
        }
-       if (fsx.fsx_xflags & (XFS_XFLAG_IMMUTABLE|XFS_XFLAG_APPEND)) {
+       if (fsx.fsx_xflags & (FS_XFLAG_IMMUTABLE|FS_XFLAG_APPEND)) {
                if (vflag)
                        fsrprintf(_("%s: immutable/append, ignoring\n"), fname);
                return(0);
        }
-       if (fsx.fsx_xflags & XFS_XFLAG_NODEFRAG) {
+       if (fsx.fsx_xflags & FS_XFLAG_NODEFRAG) {
                if (vflag)
                        fsrprintf(_("%s: marked as don't defrag, ignoring\n"),
                            fname);
                return(0);
        }
-       if (fsx.fsx_xflags & XFS_XFLAG_REALTIME) {
+       if (fsx.fsx_xflags & FS_XFLAG_REALTIME) {
                if (xfs_getrt(fd, &vfss) < 0) {
                        fsrprintf(_("cannot get realtime geometry for: %s\n"),
                                fname);
@@ -1021,13 +966,15 @@ fsr_setup_attr_fork(
        int             tfd,
        xfs_bstat_t     *bstatp)
 {
-       struct stat64   tstatbuf;
+#ifdef HAVE_FSETXATTR
+       struct stat     tstatbuf;
        int             i;
+       int             diff = 0;
        int             last_forkoff = 0;
        int             no_change_cnt = 0;
        int             ret;
 
-       if (!(bstatp->bs_xflags & XFS_XFLAG_HASATTR))
+       if (!(bstatp->bs_xflags & FS_XFLAG_HASATTR))
                return 0;
 
        /*
@@ -1047,7 +994,7 @@ fsr_setup_attr_fork(
 
        /* attr2 w/ fork offsets */
 
-       if (fstat64(tfd, &tstatbuf) < 0) {
+       if (fstat(tfd, &tstatbuf) < 0) {
                fsrprintf(_("unable to stat temp file: %s\n"),
                                        strerror(errno));
                return -1;
@@ -1058,10 +1005,9 @@ fsr_setup_attr_fork(
                xfs_bstat_t     tbstat;
                xfs_ino_t       ino;
                char            name[64];
-               int             diff;
 
                /*
-                * bulkstat the temp inode  to see what the forkoff is. Use
+                * bulkstat the temp inode to see what the forkoff is.  Use
                 * this to compare against the target and determine what we
                 * need to do.
                 */
@@ -1074,6 +1020,11 @@ fsr_setup_attr_fork(
                if (dflag)
                        fsrprintf(_("orig forkoff %d, temp forkoff %d\n"),
                                        bstatp->bs_forkoff, tbstat.bs_forkoff);
+               diff = tbstat.bs_forkoff - bstatp->bs_forkoff;
+
+               /* if they are equal, we are done */
+               if (!diff)
+                       goto out;
 
                snprintf(name, sizeof(name), "user.%d", i);
 
@@ -1082,12 +1033,50 @@ fsr_setup_attr_fork(
                 * an attribute fork at the default location.
                 */
                if (!tbstat.bs_forkoff) {
+                       ASSERT(i == 0);
                        ret = fsetxattr(tfd, name, "XX", 2, XATTR_CREATE);
                        if (ret) {
                                fsrprintf(_("could not set ATTR\n"));
                                return -1;
                        }
                        continue;
+               } else if (i == 0) {
+                       /*
+                        * First pass, and temp file already has an inline
+                        * xattr, probably due to selinux.
+                        *
+                        * It's *possible* that the temp file attr area
+                        * is larger than the target file's:
+                        *
+                        *  Target               Temp
+                        * +-------+ 0          +-------+ 0
+                        * |       |            |       |
+                        * |       |            | Data  |
+                        * | Data  |            |       |
+                        * |       |            v-------v forkoff
+                        * |       |            |       |
+                        * v-------v forkoff    | Attr  | local
+                        * | Attr  |            |       |
+                        * +-------+            +-------+
+                        */
+
+                       /*
+                        * If target attr area is less than the temp's
+                        * (diff < 0) write a big attr to the temp file to knock
+                        * the attr out of local format.
+                        * (This should actually *increase* the temp file's
+                        * forkoffset when the attr moves out of the inode)
+                        */
+                       if (diff < 0) {
+                               char val[2048];
+                               memset(val, 'X', 2048);
+                               if (fsetxattr(tfd, name, val, 2048, 0)) {
+                                       fsrprintf(_("big ATTR set failed\n"));
+                                       return -1;
+                               }
+                               /* Go back & see where we're at now */
+                               continue;
+                       }
                }
 
                /*
@@ -1102,19 +1091,14 @@ fsr_setup_attr_fork(
                last_forkoff = tbstat.bs_forkoff;
 
                /* work out which way to grow the fork */
-               diff = tbstat.bs_forkoff - bstatp->bs_forkoff;
                if (abs(diff) > fsgeom.inodesize - sizeof(struct xfs_dinode)) {
                        fsrprintf(_("forkoff diff %d too large!\n"), diff);
                        return -1;
                }
 
-               /* if they are equal, we are done */
-               if (!diff)
-                       goto out;
-
                /*
-                * if the temp inode fork offset is smaller then we have to
-                * grow the data fork
+                * if the temp inode fork offset is still smaller then we have
+                * to grow the data fork
                 */
                if (diff < 0) {
                        /*
@@ -1124,6 +1108,8 @@ fsr_setup_attr_fork(
                         * non-contiguous offsets.
                         */
                        /* XXX: unimplemented! */
+                       if (dflag)
+                               printf(_("data fork growth unimplemented\n"));
                        goto out;
                }
 
@@ -1139,6 +1125,11 @@ fsr_setup_attr_fork(
 out:
        if (dflag)
                fsrprintf(_("set temp attr\n"));
+       /* We failed to resolve the fork difference */
+       if (dflag && diff)
+               fsrprintf(_("failed to match fork offset\n"));;
+
+#endif /* HAVE_FSETXATTR */
        return 0;
 }
 
@@ -1147,14 +1138,20 @@ out:
  * 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;
@@ -1162,7 +1159,7 @@ packfile(char *fname, char *tname, int fd,
        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;
@@ -1178,7 +1175,8 @@ packfile(char *fname, char *tname, int fd,
        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)
@@ -1190,31 +1188,28 @@ packfile(char *fname, char *tname, int fd,
                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 (fsxp->fsx_xflags || fsxp->fsx_projid) {
-               if (ioctl(tfd, XFS_IOC_FSSETXATTR, fsxp) < 0) {
+               if (ioctl(tfd, FS_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;
@@ -1236,8 +1231,7 @@ packfile(char *fname, char *tname, int fd,
 
        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) {
@@ -1248,9 +1242,7 @@ packfile(char *fname, char *tname, int fd,
                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);
        }
@@ -1266,7 +1258,11 @@ packfile(char *fname, char *tname, int fd,
                                fsrprintf(_("could not trunc tmp %s\n"),
                                           tname);
                        }
-                       lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR);
+                       if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                               fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
+                                  tname, strerror(errno));
+                               goto out;
+                       }
                        continue;
                } else if (outmap[extent].bmv_length == 0) {
                        /* to catch holes at the beginning of the file */
@@ -1280,19 +1276,19 @@ packfile(char *fname, char *tname, int fd,
                        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;
+                       }
+                       if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                               fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
+                                  tname, strerror(errno));
+                               goto out;
                        }
-                       lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR);
                }
        } /* end of space allocation loop */
 
-       if (lseek64(tfd, 0, SEEK_SET)) {
+       if (lseek(tfd, 0, SEEK_SET)) {
                fsrprintf(_("Couldn't rewind on temporary file\n"));
-               close(tfd);
-               free(fbuf);
-               return -1;
+               goto out;
        }
 
        /* Check if the temporary file has fewer extents */
@@ -1302,17 +1298,24 @@ packfile(char *fname, char *tname, int fd,
        if (cur_nextents <= new_nextents) {
                if (vflag)
                        fsrprintf(_("No improvement will be made (skipping): %s\n"), fname);
-               free(fbuf);
-               close(tfd);
-               return 1; /* no change/no error */
+               retval = 1; /* no change/no error */
+               goto out;
        }
 
        /* Loop through block map copying the file. */
        for (extent = 0; extent < nextents; extent++) {
                pos = outmap[extent].bmv_offset;
                if (outmap[extent].bmv_block == -1) {
-                       lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR);
-                       lseek64(fd, outmap[extent].bmv_length, SEEK_CUR);
+                       if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                               fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
+                                  tname, strerror(errno));
+                               goto out;
+                       }
+                       if (lseek(fd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                               fsrprintf(_("could not lseek in file: %s : %s\n"),
+                                  fname, strerror(errno));
+                               goto out;
+                       }
                        continue;
                } else if (outmap[extent].bmv_length == 0) {
                        /* to catch holes at the beginning of the file */
@@ -1375,9 +1378,7 @@ packfile(char *fname, char *tname, int fd,
                                                        tname);
                                        }
                                }
-                               free(fbuf);
-                               close(tfd);
-                               return -1;
+                               goto out;
                        }
                        if (nfrags) {
                                /* Do a matching write to the tmp file */
@@ -1390,11 +1391,16 @@ packfile(char *fname, char *tname, int fd,
                        }
                }
        }
-       ftruncate64(tfd, statp->bs_size);
-       if (ffd > 0) close(ffd);
-       fsync(tfd);
-
-       free(fbuf);
+       if (ftruncate(tfd, statp->bs_size) < 0) {
+               fsrprintf(_("could not truncate tmpfile: %s : %s\n"),
+                               fname, strerror(errno));
+               goto out;
+       }
+       if (fsync(tfd) < 0) {
+               fsrprintf(_("could not fsync tmpfile: %s : %s\n"),
+                               fname, strerror(errno));
+               goto out;
+       }
 
        sx.sx_stat     = *statp; /* struct copy */
        sx.sx_version  = XFS_SX_VERSION;
@@ -1408,8 +1414,7 @@ packfile(char *fname, char *tname, int fd,
                 if (vflag)
                         fsrprintf(_("failed to fchown tmpfile %s: %s\n"),
                                    tname, strerror(errno));
-               close(tfd);
-                return -1;
+               goto out;
         }
 
        /* Swap the extents */
@@ -1431,8 +1436,7 @@ packfile(char *fname, char *tname, int fd,
                        fsrprintf(_("XFS_IOC_SWAPEXT failed: %s: %s\n"),
                                  fname, strerror(errno));
                }
-               close(tfd);
-               return -1;
+               goto out;
        }
 
        /* Report progress */
@@ -1441,8 +1445,15 @@ packfile(char *fname, char *tname, int fd,
                          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 *
@@ -1633,7 +1644,7 @@ xfs_getgeom(int fd, xfs_fsop_geom_v1_t * fsgeom)
  * Get xfs realtime space information
  */
 int
-xfs_getrt(int fd, struct statvfs64 *sfbp)
+xfs_getrt(int fd, struct statvfs *sfbp)
 {
        unsigned long   bsize;
        unsigned long   factor;