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