]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - db/frag.c
misc: fix libxfs api violations
[thirdparty/xfsprogs-dev.git] / db / frag.c
index 0a21dd97de8b1457ae148a7f58975276f57e4b3a..8005e45dfbaf8ada66e3d5555728af7b6ec9dcfc 100644 (file)
--- a/db/frag.c
+++ b/db/frag.c
@@ -1,36 +1,22 @@
 /*
- * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
+ * 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.
+ * 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.
  *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * 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/libxfs.h>
+#include "libxfs.h"
 #include <sys/time.h>
 #include "bmap.h"
 #include "command.h"
@@ -65,12 +51,12 @@ static int          Rflag;
 static int             rflag;
 static int             vflag;
 
-typedef void   (*scan_lbtree_f_t)(xfs_btree_lblock_t   *block,
+typedef void   (*scan_lbtree_f_t)(struct xfs_btree_block *block,
                                   int                  level,
                                   extmap_t             **extmapp,
                                   typnm_t              btype);
 
-typedef void   (*scan_sbtree_f_t)(xfs_btree_sblock_t   *block,
+typedef void   (*scan_sbtree_f_t)(struct xfs_btree_block *block,
                                   int                  level,
                                   xfs_agf_t            *agf);
 
@@ -80,7 +66,7 @@ static void           extmap_set_ext(extmap_t **extmapp, xfs_fileoff_t o,
                                       xfs_extlen_t c);
 static int             frag_f(int argc, char **argv);
 static int             init(int argc, char **argv);
-static void            process_bmbt_reclist(xfs_bmbt_rec_32_t *rp, int numrecs,
+static void            process_bmbt_reclist(xfs_bmbt_rec_t *rp, int numrecs,
                                             extmap_t **extmapp);
 static void            process_btinode(xfs_dinode_t *dip, extmap_t **extmapp,
                                        int whichfork);
@@ -96,9 +82,9 @@ static void           scan_lbtree(xfs_fsblock_t root, int nlevels,
 static void            scan_sbtree(xfs_agf_t *agf, xfs_agblock_t root,
                                    int nlevels, scan_sbtree_f_t func,
                                    typnm_t btype);
-static void            scanfunc_bmap(xfs_btree_lblock_t *ablock, int level,
+static void            scanfunc_bmap(struct xfs_btree_block *block, int level,
                                      extmap_t **extmapp, typnm_t btype);
-static void            scanfunc_ino(xfs_btree_sblock_t *ablock, int level,
+static void            scanfunc_ino(struct xfs_btree_block *block, int level,
                                     xfs_agf_t *agf);
 
 static const cmdinfo_t frag_cmd =
@@ -184,8 +170,12 @@ frag_f(
                         (double)extcount_actual;
        else
                answer = 0.0;
-       dbprintf("actual %llu, ideal %llu, fragmentation factor %.2f%%\n",
+       dbprintf(_("actual %llu, ideal %llu, fragmentation factor %.2f%%\n"),
                extcount_actual, extcount_ideal, answer);
+       dbprintf(_("Note, this number is largely meaningless.\n"));
+       answer = (double)extcount_actual / (double)extcount_ideal;
+       dbprintf(_("Files on this filesystem average %.2f extents per file\n"),
+               answer);
        return 0;
 }
 
@@ -225,7 +215,7 @@ init(
                        vflag = 1;
                        break;
                default:
-                       dbprintf("bad option for frag command\n");
+                       dbprintf(_("bad option for frag command\n"));
                        return 0;
                }
        }
@@ -237,18 +227,18 @@ init(
 
 static void
 process_bmbt_reclist(
-       xfs_bmbt_rec_32_t       *rp,
+       xfs_bmbt_rec_t          *rp,
        int                     numrecs,
        extmap_t                **extmapp)
 {
-       xfs_dfilblks_t          c;
+       xfs_filblks_t           c;
        int                     f;
        int                     i;
-       xfs_dfiloff_t           o;
-       xfs_dfsbno_t            s;
+       xfs_fileoff_t           o;
+       xfs_fsblock_t           s;
 
        for (i = 0; i < numrecs; i++, rp++) {
-               convert_extent((xfs_bmbt_rec_64_t *)rp, &o, &s, &c, &f);
+               convert_extent(rp, &o, &s, &c, &f);
                extmap_set_ext(extmapp, (xfs_fileoff_t)o, (xfs_extlen_t)c);
        }
 }
@@ -262,26 +252,18 @@ process_btinode(
        xfs_bmdr_block_t        *dib;
        int                     i;
        xfs_bmbt_ptr_t          *pp;
-       xfs_bmbt_rec_32_t       *rp;
 
        dib = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
-       if (INT_GET(dib->bb_level, ARCH_CONVERT) == 0) {
-               rp = (xfs_bmbt_rec_32_t *)XFS_BTREE_REC_ADDR(
-                       XFS_DFORK_SIZE(dip, mp, whichfork),
-                       xfs_bmdr, dib, 1,
-                       XFS_BTREE_BLOCK_MAXRECS(XFS_DFORK_SIZE(dip, mp,
-                                       whichfork),
-                               xfs_bmdr, 1));
-               process_bmbt_reclist(rp, INT_GET(dib->bb_numrecs, ARCH_CONVERT), extmapp);
+       if (be16_to_cpu(dib->bb_level) == 0) {
+               xfs_bmbt_rec_t          *rp = XFS_BMDR_REC_ADDR(dib, 1);
+               process_bmbt_reclist(rp, be16_to_cpu(dib->bb_numrecs), extmapp);
                return;
        }
-       pp = XFS_BTREE_PTR_ADDR(XFS_DFORK_SIZE(dip, mp, whichfork),
-               xfs_bmdr, dib, 1,
-               XFS_BTREE_BLOCK_MAXRECS(XFS_DFORK_SIZE(dip, mp, whichfork),
-                                       xfs_bmdr, 0));
-       for (i = 0; i < INT_GET(dib->bb_numrecs, ARCH_CONVERT); i++)
-               scan_lbtree((xfs_fsblock_t)INT_GET(pp[i], ARCH_CONVERT), INT_GET(dib->bb_level, ARCH_CONVERT), scanfunc_bmap,
-                       extmapp,
+       pp = XFS_BMDR_PTR_ADDR(dib, 1,
+               libxfs_bmdr_maxrecs(XFS_DFORK_SIZE(dip, mp, whichfork), 0));
+       for (i = 0; i < be16_to_cpu(dib->bb_numrecs); i++)
+               scan_lbtree(get_unaligned_be64(&pp[i]),
+                        be16_to_cpu(dib->bb_level), scanfunc_bmap, extmapp,
                        whichfork == XFS_DATA_FORK ? TYP_BMAPBTD : TYP_BMAPBTA);
 }
 
@@ -291,9 +273,9 @@ process_exinode(
        extmap_t                **extmapp,
        int                     whichfork)
 {
-       xfs_bmbt_rec_32_t       *rp;
+       xfs_bmbt_rec_t          *rp;
 
-       rp = (xfs_bmbt_rec_32_t *)XFS_DFORK_PTR(dip, whichfork);
+       rp = (xfs_bmbt_rec_t *)XFS_DFORK_PTR(dip, whichfork);
        process_bmbt_reclist(rp, XFS_DFORK_NEXTENTS(dip, whichfork), extmapp);
 }
 
@@ -329,20 +311,18 @@ process_inode(
        xfs_dinode_t            *dip)
 {
        __uint64_t              actual;
-       xfs_dinode_core_t       *dic;
        __uint64_t              ideal;
        xfs_ino_t               ino;
        int                     skipa;
        int                     skipd;
 
-       dic = &dip->di_core;
-       ino = XFS_AGINO_TO_INO(mp, INT_GET(agf->agf_seqno, ARCH_CONVERT), agino);
-       switch (dic->di_mode & S_IFMT) {
+       ino = XFS_AGINO_TO_INO(mp, be32_to_cpu(agf->agf_seqno), agino);
+       switch (be16_to_cpu(dip->di_mode) & S_IFMT) {
        case S_IFDIR:
                skipd = !dflag;
                break;
        case S_IFREG:
-               if (!rflag && (dic->di_flags & XFS_DIFLAG_REALTIME))
+               if (!rflag && (be16_to_cpu(dip->di_flags) & XFS_DIFLAG_REALTIME))
                        skipd = 1;
                else if (!Rflag &&
                         (ino == mp->m_sb.sb_rbmino ||
@@ -350,7 +330,8 @@ process_inode(
                        skipd = 1;
                else if (!qflag &&
                         (ino == mp->m_sb.sb_uquotino ||
-                         ino == mp->m_sb.sb_gquotino))
+                         ino == mp->m_sb.sb_gquotino ||
+                         ino == mp->m_sb.sb_pquotino))
                        skipd = 1;
                else
                        skipd = !fflag;
@@ -370,7 +351,7 @@ process_inode(
        if (!skipa)
                process_fork(dip, XFS_ATTR_FORK);
        if (vflag && (!skipd || !skipa))
-               dbprintf("inode %lld actual %lld ideal %lld\n",
+               dbprintf(_("inode %lld actual %lld ideal %lld\n"),
                        ino, extcount_actual - actual, extcount_ideal - ideal);
 }
 
@@ -386,7 +367,7 @@ scan_ag(
                XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
                XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
        if ((agf = iocur_top->data) == NULL) {
-               dbprintf("can't read agf block for ag %u\n", agno);
+               dbprintf(_("can't read agf block for ag %u\n"), agno);
                pop_cur();
                return;
        }
@@ -395,15 +376,13 @@ scan_ag(
                XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
                XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
        if ((agi = iocur_top->data) == NULL) {
-               dbprintf("can't read agi block for ag %u\n", agno);
+               dbprintf(_("can't read agi block for ag %u\n"), agno);
                pop_cur();
                pop_cur();
                return;
        }
-       scan_sbtree(agf,
-               INT_GET(agi->agi_root, ARCH_CONVERT),
-               INT_GET(agi->agi_level, ARCH_CONVERT),
-               scanfunc_ino, TYP_INOBT);
+       scan_sbtree(agf, be32_to_cpu(agi->agi_root),
+                       be32_to_cpu(agi->agi_level), scanfunc_ino, TYP_INOBT);
        pop_cur();
        pop_cur();
 }
@@ -420,7 +399,7 @@ scan_lbtree(
        set_cur(&typtab[btype], XFS_FSB_TO_DADDR(mp, root), blkbb, DB_RING_IGN,
                NULL);
        if (iocur_top->data == NULL) {
-               dbprintf("can't read btree block %u/%u\n",
+               dbprintf(_("can't read btree block %u/%u\n"),
                        XFS_FSB_TO_AGNO(mp, root),
                        XFS_FSB_TO_AGBNO(mp, root));
                return;
@@ -437,13 +416,13 @@ scan_sbtree(
        scan_sbtree_f_t func,
        typnm_t         btype)
 {
-       xfs_agnumber_t  seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
+       xfs_agnumber_t  seqno = be32_to_cpu(agf->agf_seqno);
 
        push_cur();
        set_cur(&typtab[btype], XFS_AGB_TO_DADDR(mp, seqno, root),
                blkbb, DB_RING_IGN, NULL);
        if (iocur_top->data == NULL) {
-               dbprintf("can't read btree block %u/%u\n", seqno, root);
+               dbprintf(_("can't read btree block %u/%u\n"), seqno, root);
                return;
        }
        (*func)(iocur_top->data, nlevels - 1, agf);
@@ -452,38 +431,48 @@ scan_sbtree(
 
 static void
 scanfunc_bmap(
-       xfs_btree_lblock_t      *ablock,
+       struct xfs_btree_block  *block,
        int                     level,
        extmap_t                **extmapp,
        typnm_t                 btype)
 {
-       xfs_bmbt_block_t        *block = (xfs_bmbt_block_t *)ablock;
        int                     i;
        xfs_bmbt_ptr_t          *pp;
-       xfs_bmbt_rec_32_t       *rp;
+       xfs_bmbt_rec_t          *rp;
+       int                     nrecs;
+
+       nrecs = be16_to_cpu(block->bb_numrecs);
 
        if (level == 0) {
-               rp = (xfs_bmbt_rec_32_t *)
-                       XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
-                       block, 1, mp->m_bmap_dmxr[0]);
-               process_bmbt_reclist(rp, INT_GET(block->bb_numrecs, ARCH_CONVERT), extmapp);
+               if (nrecs > mp->m_bmap_dmxr[0]) {
+                       dbprintf(_("invalid numrecs (%u) in %s block\n"),
+                                  nrecs, typtab[btype].name);
+                       return;
+               }
+               rp = XFS_BMBT_REC_ADDR(mp, block, 1);
+               process_bmbt_reclist(rp, nrecs, extmapp);
                return;
        }
-       pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block, 1,
-               mp->m_bmap_dmxr[0]);
-       for (i = 0; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++)
-               scan_lbtree(INT_GET(pp[i], ARCH_CONVERT), level, scanfunc_bmap, extmapp, btype);
+
+       if (nrecs > mp->m_bmap_dmxr[1]) {
+               dbprintf(_("invalid numrecs (%u) in %s block\n"),
+                          nrecs, typtab[btype].name);
+               return;
+       }
+       pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[0]);
+       for (i = 0; i < nrecs; i++)
+               scan_lbtree(be64_to_cpu(pp[i]), level, scanfunc_bmap, extmapp,
+                                                                       btype);
 }
 
 static void
 scanfunc_ino(
-       xfs_btree_sblock_t      *ablock,
+       struct xfs_btree_block  *block,
        int                     level,
        xfs_agf_t               *agf)
 {
        xfs_agino_t             agino;
-       xfs_inobt_block_t       *block = (xfs_inobt_block_t *)ablock;
-       xfs_agnumber_t          seqno = INT_GET(agf->agf_seqno, ARCH_CONVERT);
+       xfs_agnumber_t          seqno = be32_to_cpu(agf->agf_seqno);
        int                     i;
        int                     j;
        int                     off;
@@ -491,44 +480,34 @@ scanfunc_ino(
        xfs_inobt_rec_t         *rp;
 
        if (level == 0) {
-               rp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_inobt, block,
-                       1, mp->m_inobt_mxr[0]);
-               for (i = 0; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++) {
-                       agino = INT_GET(rp[i].ir_startino, ARCH_CONVERT);
+               rp = XFS_INOBT_REC_ADDR(mp, block, 1);
+               for (i = 0; i < be16_to_cpu(block->bb_numrecs); i++) {
+                       agino = be32_to_cpu(rp[i].ir_startino);
                        off = XFS_INO_TO_OFFSET(mp, agino);
                        push_cur();
                        set_cur(&typtab[TYP_INODE],
                                XFS_AGB_TO_DADDR(mp, seqno,
                                                 XFS_AGINO_TO_AGBNO(mp, agino)),
-                               XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)),
+                               XFS_FSB_TO_BB(mp, mp->m_ialloc_blks),
                                DB_RING_IGN, NULL);
                        if (iocur_top->data == NULL) {
-                               dbprintf("can't read inode block %u/%u\n",
+                               dbprintf(_("can't read inode block %u/%u\n"),
                                        seqno, XFS_AGINO_TO_AGBNO(mp, agino));
                                continue;
                        }
                        for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
-                               xfs_dinode_t            *dip;
-                               xfs_dinode_core_t       tdic;
-
-                               dip=(xfs_dinode_t *)((char *)iocur_top->data + ((off + j) << mp->m_sb.sb_inodelog));
-
-                               /* convert the core, then copy it back into the inode */
-                               libxfs_xlate_dinode_core( (xfs_caddr_t)
-                                       &dip->di_core, &tdic, 1);
-                               memcpy(&dip->di_core, &tdic, sizeof(xfs_dinode_core_t));
-
-                               if (XFS_INOBT_IS_FREE(&rp[i], j, ARCH_CONVERT))
+                               if (XFS_INOBT_IS_FREE_DISK(&rp[i], j))
                                        continue;
-                               process_inode(agf, agino + j,
-                                       (xfs_dinode_t *)((char *)iocur_top->data + ((off + j) << mp->m_sb.sb_inodelog)));
+                               process_inode(agf, agino + j, (xfs_dinode_t *)
+                                       ((char *)iocur_top->data +
+                                       ((off + j) << mp->m_sb.sb_inodelog)));
                        }
                        pop_cur();
                }
                return;
        }
-       pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_inobt, block, 1,
-               mp->m_inobt_mxr[1]);
-       for (i = 0; i < INT_GET(block->bb_numrecs, ARCH_CONVERT); i++)
-               scan_sbtree(agf, INT_GET(pp[i], ARCH_CONVERT), level, scanfunc_ino, TYP_INOBT);
+       pp = XFS_INOBT_PTR_ADDR(mp, block, 1, mp->m_inobt_mxr[1]);
+       for (i = 0; i < be16_to_cpu(block->bb_numrecs); i++)
+               scan_sbtree(agf, be32_to_cpu(pp[i]), level, scanfunc_ino,
+                                                               TYP_INOBT);
 }