]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libfrog/fsgeom.c
mkfs: use cvtnum from libfrog
[thirdparty/xfsprogs-dev.git] / libfrog / fsgeom.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
96aaf9bc 2/*
959ef981 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
96aaf9bc 4 */
33c692a2
DW
5#include "platform_defs.h"
6#include "xfs.h"
7#include "bitops.h"
96aaf9bc 8#include "fsgeom.h"
660b5d96 9#include "util.h"
96aaf9bc
DW
10
11void
12xfs_report_geom(
13 struct xfs_fsop_geom *geo,
14 const char *mntpoint,
15 const char *logname,
16 const char *rtname)
17{
18 int isint;
19 int lazycount;
20 int dirversion;
21 int logversion;
22 int attrversion;
23 int projid32bit;
24 int crcs_enabled;
25 int cimode;
26 int ftype_enabled;
27 int finobt_enabled;
28 int spinodes;
29 int rmapbt_enabled;
30 int reflink_enabled;
31
32 isint = geo->logstart > 0;
33 lazycount = geo->flags & XFS_FSOP_GEOM_FLAGS_LAZYSB ? 1 : 0;
34 dirversion = geo->flags & XFS_FSOP_GEOM_FLAGS_DIRV2 ? 2 : 1;
35 logversion = geo->flags & XFS_FSOP_GEOM_FLAGS_LOGV2 ? 2 : 1;
36 attrversion = geo->flags & XFS_FSOP_GEOM_FLAGS_ATTR2 ? 2 : \
37 (geo->flags & XFS_FSOP_GEOM_FLAGS_ATTR ? 1 : 0);
38 cimode = geo->flags & XFS_FSOP_GEOM_FLAGS_DIRV2CI ? 1 : 0;
39 projid32bit = geo->flags & XFS_FSOP_GEOM_FLAGS_PROJID32 ? 1 : 0;
40 crcs_enabled = geo->flags & XFS_FSOP_GEOM_FLAGS_V5SB ? 1 : 0;
41 ftype_enabled = geo->flags & XFS_FSOP_GEOM_FLAGS_FTYPE ? 1 : 0;
42 finobt_enabled = geo->flags & XFS_FSOP_GEOM_FLAGS_FINOBT ? 1 : 0;
43 spinodes = geo->flags & XFS_FSOP_GEOM_FLAGS_SPINODES ? 1 : 0;
44 rmapbt_enabled = geo->flags & XFS_FSOP_GEOM_FLAGS_RMAPBT ? 1 : 0;
45 reflink_enabled = geo->flags & XFS_FSOP_GEOM_FLAGS_REFLINK ? 1 : 0;
46
47 printf(_(
48"meta-data=%-22s isize=%-6d agcount=%u, agsize=%u blks\n"
49" =%-22s sectsz=%-5u attr=%u, projid32bit=%u\n"
50" =%-22s crc=%-8u finobt=%u, sparse=%u, rmapbt=%u\n"
51" =%-22s reflink=%u\n"
52"data =%-22s bsize=%-6u blocks=%llu, imaxpct=%u\n"
53" =%-22s sunit=%-6u swidth=%u blks\n"
54"naming =version %-14u bsize=%-6u ascii-ci=%d, ftype=%d\n"
55"log =%-22s bsize=%-6d blocks=%u, version=%d\n"
56" =%-22s sectsz=%-5u sunit=%d blks, lazy-count=%d\n"
57"realtime =%-22s extsz=%-6d blocks=%lld, rtextents=%lld\n"),
58 mntpoint, geo->inodesize, geo->agcount, geo->agblocks,
59 "", geo->sectsize, attrversion, projid32bit,
60 "", crcs_enabled, finobt_enabled, spinodes, rmapbt_enabled,
61 "", reflink_enabled,
62 "", geo->blocksize, (unsigned long long)geo->datablocks,
63 geo->imaxpct,
64 "", geo->sunit, geo->swidth,
65 dirversion, geo->dirblocksize, cimode, ftype_enabled,
66 isint ? _("internal log") : logname ? logname : _("external"),
67 geo->blocksize, geo->logblocks, logversion,
68 "", geo->logsectsize, geo->logsunit / geo->blocksize, lazycount,
69 !geo->rtblocks ? _("none") : rtname ? rtname : _("external"),
70 geo->rtextsize * geo->blocksize, (unsigned long long)geo->rtblocks,
71 (unsigned long long)geo->rtextents);
72}
9612817d 73
03d96c64 74/* Try to obtain the xfs geometry. On error returns a negative error code. */
9612817d
DW
75int
76xfrog_geometry(
77 int fd,
78 struct xfs_fsop_geom *fsgeo)
79{
80 int ret;
81
82 memset(fsgeo, 0, sizeof(*fsgeo));
83
84 ret = ioctl(fd, XFS_IOC_FSGEOMETRY, fsgeo);
85 if (!ret)
86 return 0;
87
88 ret = ioctl(fd, XFS_IOC_FSGEOMETRY_V4, fsgeo);
89 if (!ret)
90 return 0;
91
92 ret = ioctl(fd, XFS_IOC_FSGEOMETRY_V1, fsgeo);
93 if (!ret)
94 return 0;
95
03d96c64 96 return -errno;
9612817d 97}
3f9efb2e
DW
98
99/*
100 * Prepare xfs_fd structure for future ioctl operations by computing the xfs
03d96c64 101 * geometry for @xfd->fd. Returns zero or a negative error code.
3f9efb2e
DW
102 */
103int
104xfd_prepare_geometry(
105 struct xfs_fd *xfd)
106{
5b5c7336
DW
107 int ret;
108
109 ret = xfrog_geometry(xfd->fd, &xfd->fsgeom);
110 if (ret)
111 return ret;
112
113 xfd->agblklog = log2_roundup(xfd->fsgeom.agblocks);
114 xfd->blocklog = highbit32(xfd->fsgeom.blocksize);
115 xfd->inodelog = highbit32(xfd->fsgeom.inodesize);
116 xfd->inopblog = xfd->blocklog - xfd->inodelog;
a749451c 117 xfd->aginolog = xfd->agblklog + xfd->inopblog;
b3803ff1 118 xfd->blkbb_log = xfd->blocklog - BBSHIFT;
5b5c7336 119 return 0;
3f9efb2e
DW
120}
121
03d96c64 122/* Open a file on an XFS filesystem. Returns zero or a negative error code. */
248af7cb
DW
123int
124xfd_open(
125 struct xfs_fd *xfd,
126 const char *pathname,
127 int flags)
128{
129 int ret;
130
131 xfd->fd = open(pathname, flags);
132 if (xfd->fd < 0)
03d96c64 133 return -errno;
248af7cb
DW
134
135 ret = xfd_prepare_geometry(xfd);
136 if (ret) {
137 xfd_close(xfd);
138 return ret;
139 }
140
141 return 0;
142}
143
3f9efb2e
DW
144/*
145 * Release any resources associated with this xfs_fd structure. Returns zero
03d96c64 146 * or a negative error code.
3f9efb2e
DW
147 */
148int
149xfd_close(
150 struct xfs_fd *xfd)
151{
152 int ret = 0;
153
154 if (xfd->fd < 0)
155 return 0;
156
157 ret = close(xfd->fd);
158 xfd->fd = -1;
159 if (ret < 0)
03d96c64 160 return -errno;
3f9efb2e
DW
161
162 return 0;
163}
bb85ae74 164
03d96c64 165/* Try to obtain an AG's geometry. Returns zero or a negative error code. */
bb85ae74
DW
166int
167xfrog_ag_geometry(
168 int fd,
169 unsigned int agno,
170 struct xfs_ag_geometry *ageo)
171{
172 int ret;
173
174 ageo->ag_number = agno;
175 ret = ioctl(fd, XFS_IOC_AG_GEOMETRY, ageo);
176 if (ret)
03d96c64 177 return -errno;
bb85ae74
DW
178 return 0;
179}