]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/init.c
libxfs: remove libxfs_physmem
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
index 71da69bc32d7654241a536d8b2443e986421e5a8..537b73bd96de971094887d5499825cca4494182d 100644 (file)
@@ -1,42 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2005 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 <xfs.h>
 #include <sys/stat.h>
 #include "init.h"
 
-char *progname = "libxfs";     /* default, changed by each tool */
+#include "libxfs_priv.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_mount.h"
+#include "xfs_defer.h"
+#include "xfs_inode_buf.h"
+#include "xfs_inode_fork.h"
+#include "xfs_inode.h"
+#include "xfs_trans.h"
+#include "xfs_rmap_btree.h"
+#include "xfs_refcount_btree.h"
+
+#include "libxfs.h"            /* for now */
 
-struct cache *libxfs_icache;   /* global inode cache */
-int libxfs_ihash_size;         /* #buckets in icache */
+char *progname = "libxfs";     /* default, changed by each tool */
 
 struct cache *libxfs_bcache;   /* global buffer cache */
 int libxfs_bhash_size;         /* #buckets in bcache */
 
 int    use_xfs_buf_lock;       /* global flag: use xfs_buf_t locks for MT */
 
-static void manage_zones(int); /* setup global zones */
-
 /*
  * dev_map - map open devices to fd.
  */
 #define MAX_DEVS 10    /* arbitary maximum */
-int nextfakedev = -1;  /* device number to give to next fake device */
+static int nextfakedev = -1;   /* device number to give to next fake device */
 static struct dev_to_fd {
        dev_t   dev;
        int     fd;
@@ -53,15 +52,17 @@ static struct dev_to_fd {
 static int
 check_isactive(char *name, char *block, int fatal)
 {
-       struct stat64   st;
+       struct stat     st;
 
-       if (stat64(block, &st) < 0)
+       if (stat(block, &st) < 0)
                return 0;
        if ((st.st_mode & S_IFMT) != S_IFBLK)
                return 0;
        if (platform_check_ismounted(name, block, &st, 0) == 0)
                return 0;
-       return platform_check_iswritable(name, block, &st, fatal);
+       if (platform_check_iswritable(name, block, &st))
+               return fatal ? 1 : 0;
+       return 0;
 }
 
 /* libxfs_device_to_fd:
@@ -92,7 +93,7 @@ libxfs_device_open(char *path, int creat, int xflags, int setblksize)
        dev_t           dev;
        int             fd, d, flags;
        int             readonly, dio, excl;
-       struct stat64   statb;
+       struct stat     statb;
 
        readonly = (xflags & LIBXFS_ISREADONLY);
        excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
@@ -112,7 +113,7 @@ retry:
                exit(1);
        }
 
-       if (fstat64(fd, &statb) < 0) {
+       if (fstat(fd, &statb) < 0) {
                fprintf(stderr, _("%s: cannot stat %s: %s\n"),
                        progname, path, strerror(errno));
                exit(1);
@@ -187,9 +188,9 @@ check_open(char *path, int flags, char **rawfile, char **blockfile)
        int readonly = (flags & LIBXFS_ISREADONLY);
        int inactive = (flags & LIBXFS_ISINACTIVE);
        int dangerously = (flags & LIBXFS_DANGEROUSLY);
-       struct stat64   stbuf;
+       struct stat     stbuf;
 
-       if (stat64(path, &stbuf) < 0) {
+       if (stat(path, &stbuf) < 0) {
                perror(path);
                return 0;
        }
@@ -214,6 +215,49 @@ check_open(char *path, int flags, char **rawfile, char **blockfile)
        return 1;
 }
 
+/*
+ * Initialize/destroy all of the zone allocators we use.
+ */
+static void
+init_zones(void)
+{
+       /* initialise zone allocation */
+       xfs_buf_zone = kmem_zone_init(sizeof(struct xfs_buf), "xfs_buffer");
+       xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode");
+       xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork");
+       xfs_ili_zone = kmem_zone_init(
+                       sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
+       xfs_buf_item_zone = kmem_zone_init(
+                       sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
+       xfs_da_state_zone = kmem_zone_init(
+                       sizeof(struct xfs_da_state), "xfs_da_state");
+       xfs_btree_cur_zone = kmem_zone_init(
+                       sizeof(struct xfs_btree_cur), "xfs_btree_cur");
+       xfs_bmap_free_item_zone = kmem_zone_init(
+                       sizeof(struct xfs_extent_free_item),
+                       "xfs_bmap_free_item");
+       xfs_trans_zone = kmem_zone_init(
+                       sizeof(struct xfs_trans), "xfs_trans");
+}
+
+static int
+destroy_zones(void)
+{
+       int     leaked = 0;
+
+       leaked += kmem_zone_destroy(xfs_buf_zone);
+       leaked += kmem_zone_destroy(xfs_ili_zone);
+       leaked += kmem_zone_destroy(xfs_inode_zone);
+       leaked += kmem_zone_destroy(xfs_ifork_zone);
+       leaked += kmem_zone_destroy(xfs_buf_item_zone);
+       leaked += kmem_zone_destroy(xfs_da_state_zone);
+       leaked += kmem_zone_destroy(xfs_btree_cur_zone);
+       leaked += kmem_zone_destroy(xfs_bmap_free_item_zone);
+       leaked += kmem_zone_destroy(xfs_trans_zone);
+
+       return leaked;
+}
+
 /*
  * libxfs initialization.
  * Caller gets a 0 on failure (and we print a message), 1 on success.
@@ -222,13 +266,11 @@ int
 libxfs_init(libxfs_init_t *a)
 {
        char            *blockfile;
-       char            curdir[MAXPATHLEN];
        char            *dname;
        char            dpath[25];
        int             fd;
        char            *logname;
        char            logpath[25];
-       int             needcd;
        char            *rawfile;
        char            *rtname;
        char            rtpath[25];
@@ -241,11 +283,9 @@ libxfs_init(libxfs_init_t *a)
        rtname = a->rtname;
        a->dfd = a->logfd = a->rtfd = -1;
        a->ddev = a->logdev = a->rtdev = 0;
-       a->dbsize = a->lbsize = a->rtbsize = 0;
-       a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
+       a->dsize = a->lbsize = a->rtbsize = 0;
+       a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
 
-       (void)getcwd(curdir,MAXPATHLEN);
-       needcd = 0;
        fd = -1;
        flags = (a->isreadonly | a->isdirect);
 
@@ -254,18 +294,17 @@ libxfs_init(libxfs_init_t *a)
        if (a->volname) {
                if(!check_open(a->volname,flags,&rawfile,&blockfile))
                        goto done;
-               needcd = 1;
                fd = open(rawfile, O_RDONLY);
                dname = a->dname = a->volname;
                a->volname = NULL;
        }
        if (dname) {
-               if (dname[0] != '/' && needcd)
-                       chdir(curdir);
                if (a->disfile) {
                        a->ddev= libxfs_device_open(dname, a->dcreat, flags,
                                                    a->setblksize);
                        a->dfd = libxfs_device_to_fd(a->ddev);
+                       platform_findsizes(dname, a->dfd, &a->dsize,
+                                          &a->dbsize);
                } else {
                        if (!check_open(dname, flags, &rawfile, &blockfile))
                                goto done;
@@ -273,18 +312,17 @@ libxfs_init(libxfs_init_t *a)
                                        a->dcreat, flags, a->setblksize);
                        a->dfd = libxfs_device_to_fd(a->ddev);
                        platform_findsizes(rawfile, a->dfd,
-                                               &a->dsize, &a->dbsize);
+                                          &a->dsize, &a->dbsize);
                }
-               needcd = 1;
        } else
                a->dsize = 0;
        if (logname) {
-               if (logname[0] != '/' && needcd)
-                       chdir(curdir);
                if (a->lisfile) {
                        a->logdev = libxfs_device_open(logname,
                                        a->lcreat, flags, a->setblksize);
                        a->logfd = libxfs_device_to_fd(a->logdev);
+                       platform_findsizes(dname, a->logfd, &a->logBBsize,
+                                          &a->lbsize);
                } else {
                        if (!check_open(logname, flags, &rawfile, &blockfile))
                                goto done;
@@ -292,18 +330,17 @@ libxfs_init(libxfs_init_t *a)
                                        a->lcreat, flags, a->setblksize);
                        a->logfd = libxfs_device_to_fd(a->logdev);
                        platform_findsizes(rawfile, a->logfd,
-                                               &a->logBBsize, &a->lbsize);
+                                          &a->logBBsize, &a->lbsize);
                }
-               needcd = 1;
        } else
                a->logBBsize = 0;
        if (rtname) {
-               if (rtname[0] != '/' && needcd)
-                       chdir(curdir);
                if (a->risfile) {
                        a->rtdev = libxfs_device_open(rtname,
                                        a->rcreat, flags, a->setblksize);
                        a->rtfd = libxfs_device_to_fd(a->rtdev);
+                       platform_findsizes(dname, a->rtfd, &a->rtsize,
+                                          &a->rtbsize);
                } else {
                        if (!check_open(rtname, flags, &rawfile, &blockfile))
                                goto done;
@@ -311,9 +348,8 @@ libxfs_init(libxfs_init_t *a)
                                        a->rcreat, flags, a->setblksize);
                        a->rtfd = libxfs_device_to_fd(a->rtdev);
                        platform_findsizes(rawfile, a->rtfd,
-                                               &a->rtsize, &a->rtbsize);
+                                          &a->rtsize, &a->rtbsize);
                }
-               needcd = 1;
        } else
                a->rtsize = 0;
        if (a->dsize < 0) {
@@ -331,16 +367,13 @@ libxfs_init(libxfs_init_t *a)
                        progname);
                goto done;
        }
-       if (needcd)
-               chdir(curdir);
-       if (!libxfs_ihash_size)
-               libxfs_ihash_size = LIBXFS_IHASHSIZE(sbp);
-       libxfs_icache = cache_init(libxfs_ihash_size, &libxfs_icache_operations);
        if (!libxfs_bhash_size)
                libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
-       libxfs_bcache = cache_init(libxfs_bhash_size, &libxfs_bcache_operations);
+       libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
+                                  &libxfs_bcache_operations);
        use_xfs_buf_lock = a->usebuflock;
-       manage_zones(0);
+       xfs_dir_startup();
+       init_zones();
        rval = 1;
 done:
        if (dpath[0])
@@ -361,87 +394,6 @@ done:
 }
 
 
-/*
- * Initialize/destroy all of the zone allocators we use.
- */
-static void
-manage_zones(int release)
-{
-       extern kmem_zone_t      *xfs_buf_zone;
-       extern kmem_zone_t      *xfs_ili_zone;
-       extern kmem_zone_t      *xfs_inode_zone;
-       extern kmem_zone_t      *xfs_ifork_zone;
-       extern kmem_zone_t      *xfs_buf_item_zone;
-       extern kmem_zone_t      *xfs_da_state_zone;
-       extern kmem_zone_t      *xfs_btree_cur_zone;
-       extern kmem_zone_t      *xfs_bmap_free_item_zone;
-       extern kmem_zone_t      *xfs_log_item_desc_zone;
-       extern void             xfs_dir_startup();
-
-       if (release) {  /* free zone allocation */
-               kmem_free(xfs_buf_zone);
-               kmem_free(xfs_inode_zone);
-               kmem_free(xfs_ifork_zone);
-               kmem_free(xfs_buf_item_zone);
-               kmem_free(xfs_da_state_zone);
-               kmem_free(xfs_btree_cur_zone);
-               kmem_free(xfs_bmap_free_item_zone);
-               kmem_free(xfs_log_item_desc_zone);
-               return;
-       }
-       /* otherwise initialise zone allocation */
-       xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer");
-       xfs_inode_zone = kmem_zone_init(sizeof(xfs_inode_t), "xfs_inode");
-       xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
-       xfs_ili_zone = kmem_zone_init(
-                       sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
-       xfs_buf_item_zone = kmem_zone_init(
-                       sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
-       xfs_da_state_zone = kmem_zone_init(
-                       sizeof(xfs_da_state_t), "xfs_da_state");
-       xfs_btree_cur_zone = kmem_zone_init(
-                       sizeof(xfs_btree_cur_t), "xfs_btree_cur");
-       xfs_bmap_free_item_zone = kmem_zone_init(
-                       sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item");
-       xfs_log_item_desc_zone = kmem_zone_init(
-                       sizeof(struct xfs_log_item_desc), "xfs_log_item_desc");
-       xfs_dir_startup();
-}
-
-/*
- * Get the bitmap and summary inodes into the mount structure
- * at mount time.
- */
-static int
-rtmount_inodes(xfs_mount_t *mp)
-{
-       int             error;
-       xfs_sb_t        *sbp;
-
-       sbp = &mp->m_sb;
-       if (sbp->sb_rbmino == NULLFSINO)
-               return 0;
-       error = libxfs_iget(mp, NULL, sbp->sb_rbmino, 0, &mp->m_rbmip, 0);
-       if (error) {
-               fprintf(stderr,
-                       _("%s: cannot read realtime bitmap inode (%d)\n"),
-                       progname, error);
-               return error;
-       }
-       ASSERT(mp->m_rbmip != NULL);
-       ASSERT(sbp->sb_rsumino != NULLFSINO);
-       error = libxfs_iget(mp, NULL, sbp->sb_rsumino, 0, &mp->m_rsumip, 0);
-       if (error) {
-               libxfs_iput(mp->m_rbmip, 0);
-               fprintf(stderr,
-                       _("%s: cannot read realtime summary inode (%d)\n"),
-                       progname, error);
-               return error;
-       }
-       ASSERT(mp->m_rsumip != NULL);
-       return 0;
-}
-
 /*
  * Initialize realtime fields in the mount structure.
  */
@@ -457,7 +409,7 @@ rtmount_init(
        sbp = &mp->m_sb;
        if (sbp->sb_rblocks == 0)
                return 0;
-       if (mp->m_rtdev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
+       if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
                fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
                        progname);
                return -1;
@@ -486,7 +438,7 @@ rtmount_init(
                return -1;
        }
        bp = libxfs_readbuf(mp->m_rtdev,
-                       d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0);
+                       d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
        if (bp == NULL) {
                fprintf(stderr, _("%s: realtime size check failed\n"),
                        progname);
@@ -540,7 +492,7 @@ libxfs_initialize_perag(
         * If we mount with the inode64 option, or no inode overflows
         * the legacy 32-bit address space clear the inode32 option.
         */
-       agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
+       agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
        ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
 
        if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
@@ -553,8 +505,8 @@ libxfs_initialize_perag(
                 * Calculate how much should be reserved for inodes to meet
                 * the max inode percentage.
                 */
-               if (mp->m_maxicount) {
-                       __uint64_t      icount;
+               if (M_IGEO(mp)->maxicount) {
+                       uint64_t        icount;
 
                        icount = sbp->sb_dblocks * sbp->sb_imax_pct;
                        do_div(icount, 100);
@@ -588,6 +540,8 @@ libxfs_initialize_perag(
 
        if (maxagi)
                *maxagi = index;
+
+       mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
        return 0;
 
 out_unwind:
@@ -599,6 +553,72 @@ out_unwind:
        return error;
 }
 
+static struct xfs_buftarg *
+libxfs_buftarg_alloc(
+       struct xfs_mount        *mp,
+       dev_t                   dev)
+{
+       struct xfs_buftarg      *btp;
+
+       btp = malloc(sizeof(*btp));
+       if (!btp) {
+               fprintf(stderr, _("%s: buftarg init failed\n"),
+                       progname);
+               exit(1);
+       }
+       btp->bt_mount = mp;
+       btp->dev = dev;
+       return btp;
+}
+
+void
+libxfs_buftarg_init(
+       struct xfs_mount        *mp,
+       dev_t                   dev,
+       dev_t                   logdev,
+       dev_t                   rtdev)
+{
+       if (mp->m_ddev_targp) {
+               /* should already have all buftargs initialised */
+               if (mp->m_ddev_targp->dev != dev ||
+                   mp->m_ddev_targp->bt_mount != mp) {
+                       fprintf(stderr,
+                               _("%s: bad buftarg reinit, ddev\n"),
+                               progname);
+                       exit(1);
+               }
+               if (!logdev || logdev == dev) {
+                       if (mp->m_logdev_targp != mp->m_ddev_targp) {
+                               fprintf(stderr,
+                               _("%s: bad buftarg reinit, ldev mismatch\n"),
+                                       progname);
+                               exit(1);
+                       }
+               } else if (mp->m_logdev_targp->dev != logdev ||
+                          mp->m_logdev_targp->bt_mount != mp) {
+                       fprintf(stderr,
+                               _("%s: bad buftarg reinit, logdev\n"),
+                               progname);
+                       exit(1);
+               }
+               if (rtdev && (mp->m_rtdev_targp->dev != rtdev ||
+                             mp->m_rtdev_targp->bt_mount != mp)) {
+                       fprintf(stderr,
+                               _("%s: bad buftarg reinit, rtdev\n"),
+                               progname);
+                       exit(1);
+               }
+               return;
+       }
+
+       mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev);
+       if (!logdev || logdev == dev)
+               mp->m_logdev_targp = mp->m_ddev_targp;
+       else
+               mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev);
+       mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev);
+}
+
 /*
  * Mount structure initialization, provides a filled-in xfs_mount_t
  * such that the numerous XFS_* macros can be used.  If dev is zero,
@@ -618,32 +638,15 @@ libxfs_mount(
        xfs_sb_t        *sbp;
        int             error;
 
-       mp->m_dev = dev;
-       mp->m_rtdev = rtdev;
-       mp->m_logdev = logdev;
+       libxfs_buftarg_init(mp, dev, logdev, rtdev);
+
+       mp->m_finobt_nores = true;
        mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
        mp->m_sb = *sb;
        INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
        sbp = &(mp->m_sb);
 
-       xfs_mount_common(mp, sb);
-
-       xfs_alloc_compute_maxlevels(mp);
-       xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
-       xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
-       xfs_ialloc_compute_maxlevels(mp);
-
-       if (sbp->sb_imax_pct) {
-               /* Make sure the maximum inode count is a multiple of the
-                * units we allocate inodes in.
-                */
-               mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
-               mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
-                                 mp->m_ialloc_blks)  << sbp->sb_inopblog;
-       } else
-               mp->m_maxicount = 0;
-
-       mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
+       xfs_sb_mount_common(mp, sb);
 
        /*
         * Set whether we're using stripe alignment.
@@ -653,24 +656,12 @@ libxfs_mount(
                mp->m_swidth = sbp->sb_width;
        }
 
-       /*
-        * Set whether we're using inode alignment.
-        */
-       if (xfs_sb_version_hasalign(&mp->m_sb) &&
-           mp->m_sb.sb_inoalignmt >=
-           XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
-               mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
-       else
-               mp->m_inoalign_mask = 0;
-       /*
-        * If we are using stripe alignment, check whether
-        * the stripe unit is a multiple of the inode alignment
-        */
-       if (mp->m_dalign && mp->m_inoalign_mask &&
-                                       !(mp->m_dalign & mp->m_inoalign_mask))
-               mp->m_sinoalign = mp->m_dalign;
-       else
-               mp->m_sinoalign = 0;
+       xfs_alloc_compute_maxlevels(mp);
+       xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
+       xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
+       xfs_ialloc_setup_geometry(mp);
+       xfs_rmapbt_compute_maxlevels(mp);
+       xfs_refcountbt_compute_maxlevels(mp);
 
        /*
         * Check that the data (and log if separate) are an ok size.
@@ -682,18 +673,37 @@ libxfs_mount(
                        return NULL;
        }
 
-       /* Initialize the appropriate directory manager */
-       if (!xfs_sb_version_hasdirv2(sbp)) {
+       /*
+        * We automatically convert v1 inodes to v2 inodes now, so if
+        * the NLINK bit is not set we can't operate on the filesystem.
+        */
+       if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
+
+               fprintf(stderr, _(
+       "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
+                                progname);
+               exit(1);
+       }
+
+       /* Check for supported directory formats */
+       if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
 
                fprintf(stderr, _(
        "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
                                 progname);
                exit(1);
        }
-       xfs_dir_mount(mp);
 
-       /* Initialize cached values for the attribute manager */
-       mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
+       /* check for unsupported other features */
+       if (!xfs_sb_good_version(sbp)) {
+               fprintf(stderr, _(
+       "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
+                                progname);
+               exit(1);
+       }
+
+       xfs_da_mount(mp);
+
        if (xfs_sb_version_hasattr2(&mp->m_sb))
                mp->m_flags |= LIBXFS_MOUNT_ATTR2;
 
@@ -705,7 +715,7 @@ libxfs_mount(
 
        bp = libxfs_readbuf(mp->m_dev,
                        d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
-                       !(flags & LIBXFS_MOUNT_DEBUGGER));
+                       !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
        if (!bp) {
                fprintf(stderr, _("%s: data size check failed\n"), progname);
                if (!(flags & LIBXFS_MOUNT_DEBUGGER))
@@ -713,13 +723,14 @@ libxfs_mount(
        } else
                libxfs_putbuf(bp);
 
-       if (mp->m_logdev && mp->m_logdev != mp->m_dev) {
+       if (mp->m_logdev_targp->dev &&
+           mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) {
                d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
                if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
-                    (!(bp = libxfs_readbuf(mp->m_logdev,
+                    (!(bp = libxfs_readbuf(mp->m_logdev_targp,
                                        d - XFS_FSB_TO_BB(mp, 1),
                                        XFS_FSB_TO_BB(mp, 1),
-                                       !(flags & LIBXFS_MOUNT_DEBUGGER)))) ) {
+                                       !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) {
                        fprintf(stderr, _("%s: log size checks failed\n"),
                                        progname);
                        if (!(flags & LIBXFS_MOUNT_DEBUGGER))
@@ -736,44 +747,34 @@ libxfs_mount(
                        return NULL;
        }
 
-       error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
-       if (error) {
-               fprintf(stderr, _("%s: perag init failed\n"),
-                       progname);
-               exit(1);
-       }
-
        /*
-        * mkfs calls mount before the root inode is allocated.
+        * libxfs_initialize_perag will allocate a perag structure for each ag.
+        * If agcount is corrupted and insanely high, this will OOM the box.
+        * If the agount seems (arbitrarily) high, try to read what would be
+        * the last AG, and if that fails for a relatively high agcount, just
+        * read the first one and let the user know to check the geometry.
         */
-       if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO) {
-               error = libxfs_iget(mp, NULL, sbp->sb_rootino, 0,
-                               &mp->m_rootip, 0);
-               if (error) {
-                       fprintf(stderr, _("%s: cannot read root inode (%d)\n"),
-                               progname, error);
+       if (sbp->sb_agcount > 1000000) {
+               bp = libxfs_readbuf(mp->m_dev,
+                               XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
+                               !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
+               if (bp->b_error) {
+                       fprintf(stderr, _("%s: read of AG %u failed\n"),
+                                               progname, sbp->sb_agcount);
                        if (!(flags & LIBXFS_MOUNT_DEBUGGER))
                                return NULL;
+                       fprintf(stderr, _("%s: limiting reads to AG 0\n"),
+                                                               progname);
+                       sbp->sb_agcount = 1;
                }
-               ASSERT(mp->m_rootip != NULL);
-       }
-       if ((flags & LIBXFS_MOUNT_ROOTINOS) && rtmount_inodes(mp)) {
-               if (mp->m_rootip)
-                       libxfs_iput(mp->m_rootip, 0);
-               return NULL;
+               libxfs_putbuf(bp);
        }
 
-       /*
-        * mkfs calls mount before the AGF/AGI structures are written.
-        */
-       if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO &&
-           xfs_sb_version_haslazysbcount(&mp->m_sb)) {
-               error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
-               if (error) {
-                       fprintf(stderr, _("%s: cannot init perag data (%d)\n"),
-                               progname, error);
-                       return NULL;
-               }
+       error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
+       if (error) {
+               fprintf(stderr, _("%s: perag init failed\n"),
+                       progname);
+               exit(1);
        }
 
        return mp;
@@ -783,9 +784,9 @@ void
 libxfs_rtmount_destroy(xfs_mount_t *mp)
 {
        if (mp->m_rsumip)
-               libxfs_iput(mp->m_rsumip, 0);
+               libxfs_irele(mp->m_rsumip);
        if (mp->m_rbmip)
-               libxfs_iput(mp->m_rbmip, 0);
+               libxfs_irele(mp->m_rbmip);
        mp->m_rsumip = mp->m_rbmip = NULL;
 }
 
@@ -799,13 +800,21 @@ libxfs_umount(xfs_mount_t *mp)
        int                     agno;
 
        libxfs_rtmount_destroy(mp);
-       libxfs_icache_purge();
        libxfs_bcache_purge();
 
        for (agno = 0; agno < mp->m_maxagi; agno++) {
                pag = radix_tree_delete(&mp->m_perag_tree, agno);
                kmem_free(pag);
        }
+
+       kmem_free(mp->m_attr_geo);
+       kmem_free(mp->m_dir_geo);
+
+       kmem_free(mp->m_rtdev_targp);
+       if (mp->m_logdev_targp != mp->m_ddev_targp)
+               kmem_free(mp->m_logdev_targp);
+       kmem_free(mp->m_ddev_targp);
+
 }
 
 /*
@@ -814,9 +823,15 @@ libxfs_umount(xfs_mount_t *mp)
 void
 libxfs_destroy(void)
 {
-       manage_zones(1);
-       cache_destroy(libxfs_icache);
+       int     leaked;
+
+       /* Free everything from the buffer cache before freeing buffer zone */
+       libxfs_bcache_purge();
+       libxfs_bcache_free();
        cache_destroy(libxfs_bcache);
+       leaked = destroy_zones();
+       if (getenv("LIBXFS_LEAK_CHECK") && leaked)
+               exit(1);
 }
 
 int
@@ -831,22 +846,9 @@ libxfs_report(FILE *fp)
        time_t t;
        char *c;
 
-       cache_report(fp, "libxfs_icache", libxfs_icache);
        cache_report(fp, "libxfs_bcache", libxfs_bcache);
 
        t = time(NULL);
        c = asctime(localtime(&t));
        fprintf(fp, "%s", c);
 }
-
-int
-libxfs_nproc(void)
-{
-       return platform_nproc();
-}
-
-unsigned long
-libxfs_physmem(void)
-{
-       return platform_physmem();
-}