]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/fscounters.c
xfs_scrub: fix #include ordering to avoid build failure
[thirdparty/xfsprogs-dev.git] / scrub / fscounters.c
1 /*
2 * Copyright (C) 2018 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 #include "xfs.h"
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sys/statvfs.h>
24 #include "platform_defs.h"
25 #include "xfs_arch.h"
26 #include "xfs_format.h"
27 #include "path.h"
28 #include "workqueue.h"
29 #include "xfs_scrub.h"
30 #include "common.h"
31 #include "fscounters.h"
32
33 /*
34 * Filesystem counter collection routines. We can count the number of
35 * inodes in the filesystem, and we can estimate the block counters.
36 */
37
38 /* Count the number of inodes in the filesystem. */
39
40 /* INUMBERS wrapper routines. */
41 struct xfs_count_inodes {
42 bool moveon;
43 uint64_t counters[0];
44 };
45
46 /*
47 * Count the number of inodes. Use INUMBERS to figure out how many inodes
48 * exist in the filesystem, assuming we've already scrubbed that.
49 */
50 static bool
51 xfs_count_inodes_range(
52 struct scrub_ctx *ctx,
53 const char *descr,
54 uint64_t first_ino,
55 uint64_t last_ino,
56 uint64_t *count)
57 {
58 struct xfs_fsop_bulkreq igrpreq = {0};
59 struct xfs_inogrp inogrp;
60 __u64 igrp_ino;
61 uint64_t nr = 0;
62 __s32 igrplen = 0;
63 int error;
64
65 ASSERT(!(first_ino & (XFS_INODES_PER_CHUNK - 1)));
66 ASSERT((last_ino & (XFS_INODES_PER_CHUNK - 1)));
67
68 igrpreq.lastip = &igrp_ino;
69 igrpreq.icount = 1;
70 igrpreq.ubuffer = &inogrp;
71 igrpreq.ocount = &igrplen;
72
73 igrp_ino = first_ino;
74 error = ioctl(ctx->mnt_fd, XFS_IOC_FSINUMBERS, &igrpreq);
75 while (!error && igrplen && inogrp.xi_startino < last_ino) {
76 nr += inogrp.xi_alloccount;
77 error = ioctl(ctx->mnt_fd, XFS_IOC_FSINUMBERS, &igrpreq);
78 }
79
80 if (error) {
81 str_errno(ctx, descr);
82 return false;
83 }
84
85 *count = nr;
86 return true;
87 }
88
89 /* Scan all the inodes in an AG. */
90 static void
91 xfs_count_ag_inodes(
92 struct workqueue *wq,
93 xfs_agnumber_t agno,
94 void *arg)
95 {
96 struct xfs_count_inodes *ci = arg;
97 struct scrub_ctx *ctx = (struct scrub_ctx *)wq->wq_ctx;
98 char descr[DESCR_BUFSZ];
99 uint64_t ag_ino;
100 uint64_t next_ag_ino;
101 bool moveon;
102
103 snprintf(descr, DESCR_BUFSZ, _("dev %d:%d AG %u inodes"),
104 major(ctx->fsinfo.fs_datadev),
105 minor(ctx->fsinfo.fs_datadev),
106 agno);
107
108 ag_ino = (__u64)agno << (ctx->inopblog + ctx->agblklog);
109 next_ag_ino = (__u64)(agno + 1) << (ctx->inopblog + ctx->agblklog);
110
111 moveon = xfs_count_inodes_range(ctx, descr, ag_ino, next_ag_ino - 1,
112 &ci->counters[agno]);
113 if (!moveon)
114 ci->moveon = false;
115 }
116
117 /* Count all the inodes in a filesystem. */
118 bool
119 xfs_count_all_inodes(
120 struct scrub_ctx *ctx,
121 uint64_t *count)
122 {
123 struct xfs_count_inodes *ci;
124 xfs_agnumber_t agno;
125 struct workqueue wq;
126 bool moveon;
127 int ret;
128
129 ci = calloc(1, sizeof(struct xfs_count_inodes) +
130 (ctx->geo.agcount * sizeof(uint64_t)));
131 if (!ci)
132 return false;
133 ci->moveon = true;
134
135 ret = workqueue_create(&wq, (struct xfs_mount *)ctx,
136 scrub_nproc_workqueue(ctx));
137 if (ret) {
138 moveon = false;
139 str_info(ctx, ctx->mntpoint, _("Could not create workqueue."));
140 goto out_free;
141 }
142 for (agno = 0; agno < ctx->geo.agcount; agno++) {
143 ret = workqueue_add(&wq, xfs_count_ag_inodes, agno, ci);
144 if (ret) {
145 moveon = false;
146 str_info(ctx, ctx->mntpoint,
147 _("Could not queue AG %u icount work."), agno);
148 break;
149 }
150 }
151 workqueue_destroy(&wq);
152
153 for (agno = 0; agno < ctx->geo.agcount; agno++)
154 *count += ci->counters[agno];
155 moveon = ci->moveon;
156
157 out_free:
158 free(ci);
159 return moveon;
160 }
161
162 /* Estimate the number of blocks and inodes in the filesystem. */
163 bool
164 xfs_scan_estimate_blocks(
165 struct scrub_ctx *ctx,
166 unsigned long long *d_blocks,
167 unsigned long long *d_bfree,
168 unsigned long long *r_blocks,
169 unsigned long long *r_bfree,
170 unsigned long long *f_files,
171 unsigned long long *f_free)
172 {
173 struct xfs_fsop_counts fc;
174 struct xfs_fsop_resblks rb;
175 struct statvfs sfs;
176 int error;
177
178 /* Grab the fstatvfs counters, since it has to report accurately. */
179 error = fstatvfs(ctx->mnt_fd, &sfs);
180 if (error) {
181 str_errno(ctx, ctx->mntpoint);
182 return false;
183 }
184
185 /* Fetch the filesystem counters. */
186 error = ioctl(ctx->mnt_fd, XFS_IOC_FSCOUNTS, &fc);
187 if (error) {
188 str_errno(ctx, ctx->mntpoint);
189 return false;
190 }
191
192 /*
193 * XFS reserves some blocks to prevent hard ENOSPC, so add those
194 * blocks back to the free data counts.
195 */
196 error = ioctl(ctx->mnt_fd, XFS_IOC_GET_RESBLKS, &rb);
197 if (error)
198 str_errno(ctx, ctx->mntpoint);
199 sfs.f_bfree += rb.resblks_avail;
200
201 *d_blocks = sfs.f_blocks + (ctx->geo.logstart ? ctx->geo.logblocks : 0);
202 *d_bfree = sfs.f_bfree;
203 *r_blocks = ctx->geo.rtblocks;
204 *r_bfree = fc.freertx;
205 *f_files = sfs.f_files;
206 *f_free = sfs.f_ffree;
207
208 return true;
209 }