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