]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - fsr/xfs_fsr.c
xfs_scrub: remove moveon from progress report helpers
[thirdparty/xfsprogs-dev.git] / fsr / xfs_fsr.c
index 580604cbad75cab8c311ea9f847355fccc85004f..af5d6169eb8ab4c98dca582be28a0b14482d1472 100644 (file)
@@ -1,45 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2002 Silicon Graphics, Inc.
  * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <libxfs.h>
-#include <xfs/xfs.h>
-#include <xfs/xfs_types.h>
-#include <xfs/jdm.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 "libfrog/paths.h"
+#include "libfrog/fsgeom.h"
+#include "libfrog/bulkstat.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"
 
 char *progname;
 
-int vflag;
-int gflag;
+static int vflag;
+static int gflag;
 static int Mflag;
 /* static int nflag; */
-int dflag = 0;
+static int dflag = 0;
 /* static int sflag; */
-int argv_blksz_dio;
+static int argv_blksz_dio;
 extern int max_ext_size;
 static int npasses = 10;
 static int startpass = 0;
 
-struct getbmap  *outmap = NULL;
-int             outmap_size = 0;
-int            RealUid;
-int            tmp_agi;
-static __int64_t       minimumfree = 2048;
+static struct getbmap  *outmap = NULL;
+static int             outmap_size = 0;
+static int             RealUid;
+static int             tmp_agi;
+static int64_t         minimumfree = 2048;
 
 #define MNTTYPE_XFS             "xfs"
 
@@ -71,10 +54,6 @@ static __int64_t     minimumfree = 2048;
 #define NULLFD         -1
 #define GRABSZ         64
 #define TARGETRANGE    10
-#define        V_NONE          0
-#define        V_OVERVIEW      1
-#define        V_ALL           2
-#define BUFFER_SIZE    (1<<16)
 #define BUFFER_MAX     (1<<24)
 
 static time_t howlong = 7200;          /* default seconds of reorganizing */
@@ -87,9 +66,9 @@ static int    pagesize;
 void usage(int ret);
 static int  fsrfile(char *fname, xfs_ino_t ino);
 static int  fsrfile_common( char *fname, char *tname, char *mnt,
-                            int fd, xfs_bstat_t *statp);
+                            int fd, struct xfs_bstat *statp);
 static int  packfile(char *fname, char *tname, int fd,
-                     xfs_bstat_t *statp, struct fsxattr *fsxp);
+                     struct xfs_bstat *statp, struct fsxattr *fsxp);
 static void fsrdir(char *dirname);
 static int  fsrfs(char *mntdir, xfs_ino_t ino, int targetrange);
 static void initallfs(char *mtab);
@@ -97,18 +76,17 @@ 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, ...);
-int read_fd_bmap(int, xfs_bstat_t *, int *);
+int read_fd_bmap(int, struct xfs_bstat *, int *);
 int cmp(const void *, const void *);
 static void tmp_init(char *mnt);
 static char * tmp_next(char *mnt);
 static void tmp_close(char *mnt);
-int xfs_getgeom(int , xfs_fsop_geom_v1_t * );
 
-xfs_fsop_geom_v1_t fsgeom;     /* geometry of active mounted system */
+static struct xfs_fsop_geom fsgeom;    /* geometry of active mounted system */
 
 #define NMOUNT 64
 static int numfs;
@@ -119,126 +97,38 @@ typedef struct fsdesc {
        int  npass;
 } fsdesc_t;
 
-fsdesc_t       *fs, *fsbase, *fsend;
-int            fsbufsize = 10; /* A starting value */
-int            nfrags = 0;     /* Debug option: Coerse into specific number
+static fsdesc_t        *fs, *fsbase, *fsend;
+static int     fsbufsize = 10; /* A starting value */
+static int     nfrags = 0;     /* Debug option: Coerse into specific number
                                 * of extents */
-int            openopts = O_CREAT|O_EXCL|O_RDWR|O_DIRECT;
-
-int
-xfs_fsgeometry(int fd, xfs_fsop_geom_v1_t *geom)
-{
-    return ioctl(fd, XFS_IOC_FSGEOMETRY_V1, geom);
-}
-
-int
-xfs_bulkstat_single(int fd, xfs_ino_t *lastip, xfs_bstat_t *ubuffer)
-{
-    xfs_fsop_bulkreq_t  bulkreq;
-
-    bulkreq.lastip = (__u64 *)lastip;
-    bulkreq.icount = 1;
-    bulkreq.ubuffer = ubuffer;
-    bulkreq.ocount = NULL;
-    return ioctl(fd, XFS_IOC_FSBULKSTAT_SINGLE, &bulkreq);
-}
+static int     openopts = O_CREAT|O_EXCL|O_RDWR|O_DIRECT;
 
-int
-xfs_bulkstat(int fd, xfs_ino_t *lastip, int icount,
-                    xfs_bstat_t *ubuffer, __s32 *ocount)
-{
-    xfs_fsop_bulkreq_t  bulkreq;
-
-    bulkreq.lastip = (__u64 *)lastip;
-    bulkreq.icount = icount;
-    bulkreq.ubuffer = ubuffer;
-    bulkreq.ocount = ocount;
-    return ioctl(fd, XFS_IOC_FSBULKSTAT, &bulkreq);
-}
-
-int
+static int
 xfs_swapext(int fd, xfs_swapext_t *sx)
 {
     return ioctl(fd, XFS_IOC_SWAPEXT, sx);
 }
 
-int
+static int
 xfs_fscounts(int fd, xfs_fsop_counts_t *counts)
 {
     return ioctl(fd, XFS_IOC_FSCOUNTS, counts);
 }
 
-void
+static void
 aborter(int unused)
 {
        fsrall_cleanup(1);
        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);
@@ -321,18 +211,18 @@ main(int argc, char **argv)
        if (vflag)
                setbuf(stdout, NULL);
 
-       starttime = time(0);
+       starttime = time(NULL);
 
        /* Save the caller's real uid */
        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));
@@ -340,17 +230,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"),
@@ -406,17 +298,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)))) {
@@ -428,15 +314,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++;
@@ -444,7 +336,7 @@ initallfs(char *mtab)
                if (rw == 0) {
                        if (dflag)
                                fsrprintf(_("Skipping %s: not mounted rw\n"),
-                                       mp->mnt_fsname);
+                                       mnt->mnt_fsname);
                        continue;
                }
 
@@ -464,23 +356,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);
@@ -506,7 +399,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);
 
@@ -514,11 +407,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)
@@ -573,6 +466,17 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
                                ptr = strchr(ptr, ' ');
                                if (ptr) {
                                        startino = strtoull(++ptr, NULL, 10);
+                                       /*
+                                        * NOTE: The inode number read in from
+                                        * the leftoff file is the last inode
+                                        * to have been fsr'd.  Since the v5
+                                        * xfrog_bulkstat function wants to be
+                                        * passed the first inode that we want
+                                        * to examine, increment the value that
+                                        * we read in.  The debug message below
+                                        * prints the lastoff value.
+                                        */
+                                       startino++;
                                }
                        }
                        if (startpass < 0)
@@ -591,7 +495,7 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
 
        if (vflag) {
                fsrprintf(_("START: pass=%d ino=%llu %s %s\n"),
-                         fs->npass, (unsigned long long)startino,
+                         fs->npass, (unsigned long long)startino - 1,
                          fs->dev, fs->mnt);
        }
 
@@ -602,14 +506,9 @@ fsrallfs(char *mtab, int howlong, char *leftofffile)
        signal(SIGTERM, aborter);
 
        /* reorg for 'howlong' -- checked in 'fsrfs' */
-       while (endtime > time(0)) {
+       while (endtime > time(NULL)) {
                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,8 +534,14 @@ 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));
+       fsrall_cleanup(endtime <= time(NULL));
 }
 
 /*
@@ -654,7 +559,7 @@ fsrall_cleanup(int timeout)
        if (timeout) {
                fsrprintf(_("%s startpass %d, endpass %d, time %d seconds\n"),
                        progname, startpass, fs->npass,
-                       time(0) - endtime + howlong);
+                       time(NULL) - endtime + howlong);
 
                /* record where we left off */
                fd = open(leftofffile, O_WRONLY|O_CREAT|O_EXCL, 0644);
@@ -678,16 +583,14 @@ fsrall_cleanup(int timeout)
 static int
 fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
 {
-
-       int     fsfd, fd;
+       struct xfs_fd   fsxfd = XFS_FD_INIT_EMPTY;
+       int     fd;
        int     count = 0;
        int     ret;
-       __s32   buflenout;
-       xfs_bstat_t buf[GRABSZ];
        char    fname[64];
        char    *tname;
        jdm_fshandle_t  *fshandlep;
-       xfs_ino_t       lastino = startino;
+       struct xfs_bulkstat_req *breq;
 
        fsrprintf(_("%s start inode=%llu\n"), mntdir,
                (unsigned long long)startino);
@@ -699,27 +602,32 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                return -1;
        }
 
-       if ((fsfd = open(mntdir, O_RDONLY)) < 0) {
-               fsrprintf(_("unable to open: %s: %s\n"),
-                         mntdir, strerror( errno ));
+       ret = xfd_open(&fsxfd, mntdir, O_RDONLY);
+       if (ret) {
+               fsrprintf(_("unable to open XFS file: %s: %s\n"),
+                         mntdir, strerror(ret));
                free(fshandlep);
                return -1;
        }
+       memcpy(&fsgeom, &fsxfd.fsgeom, sizeof(fsgeom));
+
+       tmp_init(mntdir);
 
-       if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
-               fsrprintf(_("Skipping %s: could not get XFS geometry\n"),
+       breq = xfrog_bulkstat_alloc_req(GRABSZ, startino);
+       if (!breq) {
+               fsrprintf(_("Skipping %s: not enough memory\n"),
                          mntdir);
-               close(fsfd);
+               xfd_close(&fsxfd);
                free(fshandlep);
                return -1;
        }
 
-       tmp_init(mntdir);
-
-       while ((ret = xfs_bulkstat(fsfd,
-                               &lastino, GRABSZ, &buf[0], &buflenout) == 0)) {
-               xfs_bstat_t *p;
-               xfs_bstat_t *endp;
+       while ((ret = xfrog_bulkstat(&fsxfd, breq) == 0)) {
+               struct xfs_bstat        bs1;
+               struct xfs_bulkstat     *buf = breq->bulkstat;
+               struct xfs_bulkstat     *p;
+               struct xfs_bulkstat     *endp;
+               uint32_t                buflenout = breq->hdr.ocount;
 
                if (buflenout == 0)
                        goto out0;
@@ -727,7 +635,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                /* Each loop through, defrag targetrange percent of the files */
                count = (buflenout * targetrange) / 100;
 
-               qsort((char *)buf, buflenout, sizeof(struct xfs_bstat), cmp);
+               qsort((char *)buf, buflenout, sizeof(struct xfs_bulkstat), cmp);
 
                for (p = buf, endp = (buf + buflenout); p < endp ; p++) {
                        /* Do some obvious checks now */
@@ -735,7 +643,14 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                             (p->bs_extents < 2))
                                continue;
 
-                       fd = jdm_open(fshandlep, p, O_RDWR|O_DIRECT);
+                       ret = xfrog_bulkstat_v5_to_v1(&fsxfd, &bs1, p);
+                       if (ret) {
+                               fsrprintf(_("bstat conversion error: %s\n"),
+                                               strerror(ret));
+                               continue;
+                       }
+
+                       fd = jdm_open(fshandlep, &bs1, O_RDWR | O_DIRECT);
                        if (fd < 0) {
                                /* This probably means the file was
                                 * removed while in progress of handling
@@ -753,7 +668,7 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                        /* Get a tmp file name */
                        tname = tmp_next(mntdir);
 
-                       ret = fsrfile_common(fname, tname, mntdir, fd, p);
+                       ret = fsrfile_common(fname, tname, mntdir, fd, &bs1);
 
                        leftoffino = p->bs_ino;
 
@@ -764,18 +679,20 @@ fsrfs(char *mntdir, xfs_ino_t startino, int targetrange)
                                        break;
                        }
                }
-               if (endtime && endtime < time(0)) {
+               if (endtime && endtime < time(NULL)) {
+                       free(breq);
                        tmp_close(mntdir);
-                       close(fsfd);
+                       xfd_close(&fsxfd);
                        fsrall_cleanup(1);
                        exit(1);
                }
        }
-       if (ret < 0)
-               fsrprintf(_("%s: xfs_bulkstat: %s\n"), progname, strerror(errno));
+       if (ret)
+               fsrprintf(_("%s: bulkstat: %s\n"), progname, strerror(ret));
 out0:
+       free(breq);
        tmp_close(mntdir);
-       close(fsfd);
+       xfd_close(&fsxfd);
        free(fshandlep);
        return 0;
 }
@@ -786,8 +703,8 @@ out0:
 int
 cmp(const void *s1, const void *s2)
 {
-       return( ((xfs_bstat_t *)s2)->bs_extents -
-               ((xfs_bstat_t *)s1)->bs_extents);
+       return( ((struct xfs_bstat *)s2)->bs_extents -
+               ((struct xfs_bstat *)s1)->bs_extents);
 
 }
 
@@ -808,13 +725,17 @@ fsrdir(char *dirname)
  * an open on the file and passes this all to fsrfile_common.
  */
 static int
-fsrfile(char *fname, xfs_ino_t ino)
+fsrfile(
+       char                    *fname,
+       xfs_ino_t               ino)
 {
-       xfs_bstat_t     statbuf;
-       jdm_fshandle_t  *fshandlep;
-       int     fd = -1, fsfd = -1;
-       int     error = -1;
-       char    *tname;
+       struct xfs_fd           fsxfd = XFS_FD_INIT_EMPTY;
+       struct xfs_bulkstat     bulkstat;
+       struct xfs_bstat        statbuf;
+       jdm_fshandle_t          *fshandlep;
+       int                     fd = -1;
+       int                     error = -1;
+       char                    *tname;
 
        fshandlep = jdm_getfshandle(getparent (fname) );
        if (!fshandlep) {
@@ -827,16 +748,23 @@ fsrfile(char *fname, xfs_ino_t ino)
         * Need to open something on the same filesystem as the
         * file.  Open the parent.
         */
-       fsfd = open(getparent(fname), O_RDONLY);
-       if (fsfd < 0) {
-               fsrprintf(_("unable to open sys handle for %s: %s\n"),
-                       fname, strerror(errno));
+       error = xfd_open(&fsxfd, getparent(fname), O_RDONLY);
+       if (error) {
+               fsrprintf(_("unable to open sys handle for XFS file %s: %s\n"),
+                       fname, strerror(error));
                goto out;
        }
 
-       if ((xfs_bulkstat_single(fsfd, &ino, &statbuf)) < 0) {
+       error = xfrog_bulkstat_single(&fsxfd, ino, 0, &bulkstat);
+       if (error) {
                fsrprintf(_("unable to get bstat on %s: %s\n"),
-                       fname, strerror(errno));
+                       fname, strerror(error));
+               goto out;
+       }
+       error = xfrog_bulkstat_v5_to_v1(&fsxfd, &statbuf, &bulkstat);
+       if (error) {
+               fsrprintf(_("bstat conversion error on %s: %s\n"),
+                       fname, strerror(error));
                goto out;
        }
 
@@ -847,11 +775,8 @@ fsrfile(char *fname, xfs_ino_t ino)
                goto out;
        }
 
-       /* Get the fs geometry */
-       if (xfs_getgeom(fsfd, &fsgeom) < 0 ) {
-               fsrprintf(_("Unable to get geom on fs for: %s\n"), fname);
-               goto out;
-       }
+       /* Stash the fs geometry for general use. */
+       memcpy(&fsgeom, &fsxfd.fsgeom, sizeof(fsgeom));
 
        tname = gettmpname(fname);
 
@@ -859,8 +784,7 @@ fsrfile(char *fname, xfs_ino_t ino)
                error = fsrfile_common(fname, tname, NULL, fd, &statbuf);
 
 out:
-       if (fsfd >= 0)
-               close(fsfd);
+       xfd_close(&fsxfd);
        if (fd >= 0)
                close(fd);
        free(fshandlep);
@@ -889,10 +813,10 @@ fsrfile_common(
        char            *tname,
        char            *fsname,
        int             fd,
-       xfs_bstat_t     *statp)
+       struct xfs_bstat *statp)
 {
        int             error;
-       struct statvfs64 vfss;
+       struct statvfs  vfss;
        struct fsxattr  fsx;
        unsigned long   bsize;
 
@@ -944,7 +868,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;
@@ -958,22 +882,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);
@@ -1024,16 +948,18 @@ static int
 fsr_setup_attr_fork(
        int             fd,
        int             tfd,
-       xfs_bstat_t     *bstatp)
+       struct xfs_bstat *bstatp)
 {
-       struct stat64   tstatbuf;
+#ifdef HAVE_FSETXATTR
+       struct xfs_fd   txfd = XFS_FD_INIT(tfd);
+       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;
 
        /*
@@ -1043,7 +969,7 @@ fsr_setup_attr_fork(
        if (!(fsgeom.flags & XFS_FSOP_GEOM_FLAGS_ATTR2) ||
            bstatp->bs_forkoff == 0) {
                /* attr1 */
-               ret = fsetxattr(tfd, "user.X", "X", 1, XATTR_CREATE);
+               ret = fsetxattr(txfd.fd, "user.X", "X", 1, XATTR_CREATE);
                if (ret) {
                        fsrprintf(_("could not set ATTR\n"));
                        return -1;
@@ -1053,7 +979,7 @@ fsr_setup_attr_fork(
 
        /* attr2 w/ fork offsets */
 
-       if (fstat64(tfd, &tstatbuf) < 0) {
+       if (fstat(txfd.fd, &tstatbuf) < 0) {
                fsrprintf(_("unable to stat temp file: %s\n"),
                                        strerror(errno));
                return -1;
@@ -1061,19 +987,19 @@ fsr_setup_attr_fork(
 
        i = 0;
        do {
-               xfs_bstat_t     tbstat;
-               xfs_ino_t       ino;
+               struct xfs_bulkstat     tbstat;
                char            name[64];
+               int             ret;
 
                /*
                 * bulkstat the temp inode to see what the forkoff is.  Use
                 * this to compare against the target and determine what we
                 * need to do.
                 */
-               ino = tstatbuf.st_ino;
-               if ((xfs_bulkstat_single(tfd, &ino, &tbstat)) < 0) {
+               ret = xfrog_bulkstat_single(&txfd, tstatbuf.st_ino, 0, &tbstat);
+               if (ret) {
                        fsrprintf(_("unable to get bstat on temp file: %s\n"),
-                                               strerror(errno));
+                                               strerror(ret));
                        return -1;
                }
                if (dflag)
@@ -1093,21 +1019,19 @@ fsr_setup_attr_fork(
                 */
                if (!tbstat.bs_forkoff) {
                        ASSERT(i == 0);
-                       ret = fsetxattr(tfd, name, "XX", 2, XATTR_CREATE);
+                       ret = fsetxattr(txfd.fd, name, "XX", 2, XATTR_CREATE);
                        if (ret) {
                                fsrprintf(_("could not set ATTR\n"));
                                return -1;
                        }
                        continue;
                } else if (i == 0) {
-                       struct fsxattr  fsx;
                        /*
                         * 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, if the 
-                        * target file's attrs are not inline:
+                        * is larger than the target file's:
                         *
                         *  Target               Temp
                         * +-------+ 0          +-------+ 0
@@ -1117,31 +1041,21 @@ fsr_setup_attr_fork(
                         * |       |            v-------v forkoff
                         * |       |            |       |
                         * v-------v forkoff    | Attr  | local
-                        * | Attr  | ext/btree  |       |
+                        * | Attr  |            |       |
                         * +-------+            +-------+
-                        *
-                        * FSGETXATTRA will tell us nr of attr extents in
-                        * target, if any.  If none, it's local:
                         */
 
-                       memset(&fsx, 0, sizeof(fsx));
-                       if (ioctl(fd, XFS_IOC_FSGETXATTRA, &fsx)) {
-                               fsrprintf(_("FSGETXATTRA failed on target\n"));
-                               return -1;
-                       }
-
                        /*
-                        * If target attr area is less than the temp's (diff < 0)
-                        * and the target is not local, write a big attr to
-                        * the temp file to knock the attr out of local format,
-                        * to match the target.  (This should actually *increase*
-                        * the temp file's forkoffset when the attr moves out
-                        * of the inode)
+                        * 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 && fsx.fsx_nextents > 0) {
+                       if (diff < 0) {
                                char val[2048];
                                memset(val, 'X', 2048);
-                               if (fsetxattr(tfd, name, val, 2048, 0)) {
+                               if (fsetxattr(txfd.fd, name, val, 2048, 0)) {
                                        fsrprintf(_("big ATTR set failed\n"));
                                        return -1;
                                }
@@ -1185,7 +1099,7 @@ fsr_setup_attr_fork(
                }
 
                /* we need to grow the attr fork, so create another attr */
-               ret = fsetxattr(tfd, name, "XX", 2, XATTR_CREATE);
+               ret = fsetxattr(txfd.fd, name, "XX", 2, XATTR_CREATE);
                if (ret) {
                        fsrprintf(_("could not set ATTR\n"));
                        return -1;
@@ -1200,6 +1114,7 @@ out:
        if (dflag && diff)
                fsrprintf(_("failed to match fork offset\n"));;
 
+#endif /* HAVE_FSETXATTR */
        return 0;
 }
 
@@ -1217,7 +1132,7 @@ out:
  */
 static int
 packfile(char *fname, char *tname, int fd,
-        xfs_bstat_t *statp, struct fsxattr *fsxp)
+        struct xfs_bstat *statp, struct fsxattr *fsxp)
 {
        int             tfd = -1;
        int             srval;
@@ -1270,7 +1185,7 @@ packfile(char *fname, char *tname, int fd,
 
        /* 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);
                        goto out;
@@ -1328,7 +1243,7 @@ packfile(char *fname, char *tname, int fd,
                                fsrprintf(_("could not trunc tmp %s\n"),
                                           tname);
                        }
-                       if (lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                       if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
                                fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
                                   tname, strerror(errno));
                                goto out;
@@ -1348,7 +1263,7 @@ packfile(char *fname, char *tname, int fd,
                                        " %s\n"), tname);
                                goto out;
                        }
-                       if (lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                       if (lseek(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
                                fsrprintf(_("could not lseek in tmpfile: %s : %s\n"),
                                   tname, strerror(errno));
                                goto out;
@@ -1356,7 +1271,7 @@ packfile(char *fname, char *tname, int fd,
                }
        } /* 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"));
                goto out;
        }
@@ -1376,12 +1291,12 @@ packfile(char *fname, char *tname, int fd,
        for (extent = 0; extent < nextents; extent++) {
                pos = outmap[extent].bmv_offset;
                if (outmap[extent].bmv_block == -1) {
-                       if (lseek64(tfd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                       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 (lseek64(fd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
+                       if (lseek(fd, outmap[extent].bmv_length, SEEK_CUR) < 0) {
                                fsrprintf(_("could not lseek in file: %s : %s\n"),
                                   fname, strerror(errno));
                                goto out;
@@ -1461,7 +1376,7 @@ packfile(char *fname, char *tname, int fd,
                        }
                }
        }
-       if (ftruncate64(tfd, statp->bs_size) < 0) {
+       if (ftruncate(tfd, statp->bs_size) < 0) {
                fsrprintf(_("could not truncate tmpfile: %s : %s\n"),
                                fname, strerror(errno));
                goto out;
@@ -1584,7 +1499,7 @@ getparent(char *fname)
 #define MAPSIZE        128
 #define        OUTMAP_SIZE_INCREMENT   MAPSIZE
 
-int    read_fd_bmap(int fd, xfs_bstat_t *sin, int *cur_nextents)
+int    read_fd_bmap(int fd, struct xfs_bstat *sin, int *cur_nextents)
 {
        int             i, cnt;
        struct getbmap  map[MAPSIZE];
@@ -1672,7 +1587,7 @@ int       read_fd_bmap(int fd, xfs_bstat_t *sin, int *cur_nextents)
 /*
  * Read the block map and return the number of extents.
  */
-int
+static int
 getnextents(int fd)
 {
        int             nextents;
@@ -1698,23 +1613,11 @@ getnextents(int fd)
        return(nextents);
 }
 
-/*
- * Get the fs geometry
- */
-int
-xfs_getgeom(int fd, xfs_fsop_geom_v1_t * fsgeom)
-{
-       if (xfs_fsgeometry(fd, fsgeom) < 0) {
-               return -1;
-       }
-       return 0;
-}
-
 /*
  * 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;