File: bulkstat.c

package info (click to toggle)
xfsprogs 6.17.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 11,324 kB
  • sloc: ansic: 167,334; sh: 4,604; makefile: 1,336; python: 835; cpp: 5
file content (614 lines) | stat: -rw-r--r-- 14,622 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2019 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include <string.h>
#include <strings.h>
#include "xfs.h"
#include "fsgeom.h"
#include "bulkstat.h"

/*
 * Wrapper functions for BULKSTAT and INUMBERS
 * ===========================================
 *
 * The functions in this file are thin wrappers around the most recent version
 * of the BULKSTAT and INUMBERS ioctls.  BULKSTAT is used to query XFS-specific
 * stat information about a group of inodes.  INUMBERS is used to query
 * allocation information about batches of XFS inodes.
 *
 * At the moment, the public xfrog_* functions provide all functionality of the
 * V5 interface.  If the V5 interface is not available on the running kernel,
 * the functions will emulate them as best they can with previous versions of
 * the interface (currently V1).  If emulation is not possible, EINVAL will be
 * returned.
 *
 * The XFROG_FLAG_BULKSTAT_FORCE_V[15] flags can be used to force use of a
 * particular version of the kernel interface for testing.
 */

/*
 * Grab the fs geometry information that is needed to needed to emulate v5 with
 * v1 interfaces.
 */
static inline int
xfrog_bulkstat_prep_v1_emulation(
	struct xfs_fd		*xfd)
{
	if (xfd->fsgeom.blocksize > 0)
		return 0;

	return xfd_prepare_geometry(xfd);
}

/* Bulkstat a single inode using v5 ioctl. */
static int
xfrog_bulkstat_single5(
	struct xfs_fd			*xfd,
	uint64_t			ino,
	unsigned int			flags,
	struct xfs_bulkstat		*bulkstat)
{
	struct xfs_bulkstat_req		*req;
	int				ret;

	if (flags & ~(XFS_BULK_IREQ_SPECIAL | XFS_BULK_IREQ_NREXT64 |
		      XFS_BULK_IREQ_METADIR))
		return -EINVAL;

	if (xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64)
		flags |= XFS_BULK_IREQ_NREXT64;

	ret = xfrog_bulkstat_alloc_req(1, ino, &req);
	if (ret)
		return ret;

	req->hdr.flags = flags;
	ret = ioctl(xfd->fd, XFS_IOC_BULKSTAT, req);
	if (ret) {
		ret = -errno;
		goto free;
	}

	if (req->hdr.ocount == 0) {
		ret = -ENOENT;
		goto free;
	}

	memcpy(bulkstat, req->bulkstat, sizeof(struct xfs_bulkstat));

	if (!(xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64)) {
		bulkstat->bs_extents64 = bulkstat->bs_extents;
		bulkstat->bs_extents = 0;
	}

free:
	free(req);
	return ret;
}

/* Bulkstat a single inode using v1 ioctl. */
static int
xfrog_bulkstat_single1(
	struct xfs_fd			*xfd,
	uint64_t			ino,
	unsigned int			flags,
	struct xfs_bulkstat		*bulkstat)
{
	struct xfs_bstat		bstat;
	struct xfs_fsop_bulkreq		bulkreq = { 0 };
	int				error;

	if (flags)
		return -EINVAL;

	error = xfrog_bulkstat_prep_v1_emulation(xfd);
	if (error)
		return error;

	bulkreq.lastip = (__u64 *)&ino;
	bulkreq.icount = 1;
	bulkreq.ubuffer = &bstat;
	error = ioctl(xfd->fd, XFS_IOC_FSBULKSTAT_SINGLE, &bulkreq);
	if (error)
		return -errno;

	xfrog_bulkstat_v1_to_v5(xfd, bulkstat, &bstat);
	return 0;
}

/* Bulkstat a single inode.  Returns zero or a negative error code. */
int
xfrog_bulkstat_single(
	struct xfs_fd			*xfd,
	uint64_t			ino,
	unsigned int			flags,
	struct xfs_bulkstat		*bulkstat)
{
	int				error;

	if (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V1)
		goto try_v1;

	error = xfrog_bulkstat_single5(xfd, ino, flags, bulkstat);
	if (error == 0 || (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V5))
		return error;

	/* If the v5 ioctl wasn't found, we punt to v1. */
	switch (error) {
	case -EOPNOTSUPP:
	case -ENOTTY:
		assert(!(xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64));
		xfd->flags |= XFROG_FLAG_BULKSTAT_FORCE_V1;
		break;
	}

try_v1:
	return xfrog_bulkstat_single1(xfd, ino, flags, bulkstat);
}

/*
 * Set up the necessary control structures to emulate a V5 bulk request ioctl
 * by calling a V1 bulk request ioctl.  This enables callers to run on older
 * kernels.
 *
 * Returns 0 if the emulation should proceed; ECANCELED if there are no
 * records; or a negative error code.
 */
static int
xfrog_bulk_req_v1_setup(
	struct xfs_fd		*xfd,
	struct xfs_bulk_ireq	*hdr,
	struct xfs_fsop_bulkreq	*bulkreq,
	size_t			rec_size)
{
	void			*buf;

	if (hdr->flags & XFS_BULK_IREQ_AGNO) {
		uint32_t	agno = cvt_ino_to_agno(xfd, hdr->ino);

		if (hdr->ino == 0)
			hdr->ino = cvt_agino_to_ino(xfd, hdr->agno, 0);
		else if (agno < hdr->agno)
			return -EINVAL;
		else if (agno > hdr->agno)
			goto no_results;
	}

	if (cvt_ino_to_agno(xfd, hdr->ino) > xfd->fsgeom.agcount)
		goto no_results;

	buf = malloc(hdr->icount * rec_size);
	if (!buf)
		return -errno;

	if (hdr->ino)
		hdr->ino--;
	bulkreq->lastip = (__u64 *)&hdr->ino,
	bulkreq->icount = hdr->icount,
	bulkreq->ocount = (__s32 *)&hdr->ocount,
	bulkreq->ubuffer = buf;
	return 0;

no_results:
	hdr->ocount = 0;
	return -ECANCELED;
}

/*
 * Clean up after using a V1 bulk request to emulate a V5 bulk request call.
 *
 * If the ioctl was successful, we need to convert the returned V1-format bulk
 * request data into the V5-format bulk request data and copy it into the
 * caller's buffer.  We also need to free all resources allocated during the
 * setup setup.
 */
static int
xfrog_bulk_req_v1_cleanup(
	struct xfs_fd		*xfd,
	struct xfs_bulk_ireq	*hdr,
	struct xfs_fsop_bulkreq	*bulkreq,
	size_t			v1_rec_size,
	uint64_t		(*v1_ino)(void *v1_rec),
	void			*v5_records,
	size_t			v5_rec_size,
	void			(*cvt)(struct xfs_fd *xfd, void *v5, void *v1),
	unsigned int		startino_adj,
	int			error)
{
	void			*v1_rec = bulkreq->ubuffer;
	void			*v5_rec = v5_records;
	unsigned int		i;

	if (error == -ECANCELED) {
		error = 0;
		goto free;
	}
	if (error)
		goto free;

	/*
	 * Convert each record from v1 to v5 format, keeping the startino
	 * value up to date and (if desired) stopping at the end of the
	 * AG.
	 */
	for (i = 0;
	     i < hdr->ocount;
	     i++, v1_rec += v1_rec_size, v5_rec += v5_rec_size) {
		uint64_t	ino = v1_ino(v1_rec);

		/* Stop if we hit a different AG. */
		if ((hdr->flags & XFS_BULK_IREQ_AGNO) &&
		    cvt_ino_to_agno(xfd, ino) != hdr->agno) {
			hdr->ocount = i;
			break;
		}
		cvt(xfd, v5_rec, v1_rec);
		hdr->ino = ino + startino_adj;
	}

free:
	free(bulkreq->ubuffer);
	return error;
}

static uint64_t xfrog_bstat_ino(void *v1_rec)
{
	return ((struct xfs_bstat *)v1_rec)->bs_ino;
}

static void xfrog_bstat_cvt(struct xfs_fd *xfd, void *v5, void *v1)
{
	xfrog_bulkstat_v1_to_v5(xfd, v5, v1);
}

/* Bulkstat a bunch of inodes using the v5 interface. */
static int
xfrog_bulkstat5(
	struct xfs_fd		*xfd,
	struct xfs_bulkstat_req	*req)
{
	int			ret;
	int			i;

	if (xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64)
		req->hdr.flags |= XFS_BULK_IREQ_NREXT64;

	ret = ioctl(xfd->fd, XFS_IOC_BULKSTAT, req);
	if (ret)
		return -errno;

	if (!(xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64)) {
		for (i = 0; i < req->hdr.ocount; i++) {
			req->bulkstat[i].bs_extents64 =
				req->bulkstat[i].bs_extents;
			req->bulkstat[i].bs_extents = 0;
		}
	}

	return 0;
}

/* Bulkstat a bunch of inodes using the v1 interface. */
static int
xfrog_bulkstat1(
	struct xfs_fd		*xfd,
	struct xfs_bulkstat_req	*req)
{
	struct xfs_fsop_bulkreq	bulkreq = { 0 };
	int			error;

	error = xfrog_bulkstat_prep_v1_emulation(xfd);
	if (error)
		return error;

	error = xfrog_bulk_req_v1_setup(xfd, &req->hdr, &bulkreq,
			sizeof(struct xfs_bstat));
	if (error == -ECANCELED)
		goto out_teardown;
	if (error)
		return error;

	error = ioctl(xfd->fd, XFS_IOC_FSBULKSTAT, &bulkreq);
	if (error)
		error = -errno;

out_teardown:
	return xfrog_bulk_req_v1_cleanup(xfd, &req->hdr, &bulkreq,
			sizeof(struct xfs_bstat), xfrog_bstat_ino,
			&req->bulkstat, sizeof(struct xfs_bulkstat),
			xfrog_bstat_cvt, 1, error);
}

/* Bulkstat a bunch of inodes.  Returns zero or a positive error code. */
int
xfrog_bulkstat(
	struct xfs_fd		*xfd,
	struct xfs_bulkstat_req	*req)
{
	int			error;

	if (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V1)
		goto try_v1;

	error = xfrog_bulkstat5(xfd, req);
	if (error == 0 || (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V5))
		return error;

	/* If the v5 ioctl wasn't found, we punt to v1. */
	switch (error) {
	case -EOPNOTSUPP:
	case -ENOTTY:
		assert(!(xfd->fsgeom.flags & XFS_FSOP_GEOM_FLAGS_NREXT64));
		xfd->flags |= XFROG_FLAG_BULKSTAT_FORCE_V1;
		break;
	}

try_v1:
	return xfrog_bulkstat1(xfd, req);
}

static bool
time_too_big(
	uint64_t	time)
{
	time_t		TIME_MAX;

	memset(&TIME_MAX, 0xFF, sizeof(TIME_MAX));
	return time > TIME_MAX;
}

/* Convert bulkstat data from v5 format to v1 format. */
int
xfrog_bulkstat_v5_to_v1(
	struct xfs_fd			*xfd,
	struct xfs_bstat		*bs1,
	const struct xfs_bulkstat	*bs5)
{
	if (bs5->bs_aextents > UINT16_MAX ||
	    bs5->bs_extents64 > INT32_MAX ||
	    cvt_off_fsb_to_b(xfd, bs5->bs_extsize_blks) > UINT32_MAX ||
	    cvt_off_fsb_to_b(xfd, bs5->bs_cowextsize_blks) > UINT32_MAX ||
	    time_too_big(bs5->bs_atime) ||
	    time_too_big(bs5->bs_ctime) ||
	    time_too_big(bs5->bs_mtime))
		return -ERANGE;

	bs1->bs_ino = bs5->bs_ino;
	bs1->bs_mode = bs5->bs_mode;
	bs1->bs_nlink = bs5->bs_nlink;
	bs1->bs_uid = bs5->bs_uid;
	bs1->bs_gid = bs5->bs_gid;
	bs1->bs_rdev = bs5->bs_rdev;
	bs1->bs_blksize = bs5->bs_blksize;
	bs1->bs_size = bs5->bs_size;
	bs1->bs_atime.tv_sec = bs5->bs_atime;
	bs1->bs_mtime.tv_sec = bs5->bs_mtime;
	bs1->bs_ctime.tv_sec = bs5->bs_ctime;
	bs1->bs_atime.tv_nsec = bs5->bs_atime_nsec;
	bs1->bs_mtime.tv_nsec = bs5->bs_mtime_nsec;
	bs1->bs_ctime.tv_nsec = bs5->bs_ctime_nsec;
	bs1->bs_blocks = bs5->bs_blocks;
	bs1->bs_xflags = bs5->bs_xflags;
	bs1->bs_extsize = cvt_off_fsb_to_b(xfd, bs5->bs_extsize_blks);
	bs1->bs_extents = bs5->bs_extents64;
	bs1->bs_gen = bs5->bs_gen;
	bs1->bs_projid_lo = bs5->bs_projectid & 0xFFFF;
	bs1->bs_forkoff = bs5->bs_forkoff;
	bs1->bs_projid_hi = bs5->bs_projectid >> 16;
	bs1->bs_sick = bs5->bs_sick;
	bs1->bs_checked = bs5->bs_checked;
	bs1->bs_cowextsize = cvt_off_fsb_to_b(xfd, bs5->bs_cowextsize_blks);
	bs1->bs_dmevmask = 0;
	bs1->bs_dmstate = 0;
	bs1->bs_aextents = bs5->bs_aextents;
	return 0;
}

/* Convert bulkstat data from v1 format to v5 format. */
void
xfrog_bulkstat_v1_to_v5(
	struct xfs_fd			*xfd,
	struct xfs_bulkstat		*bs5,
	const struct xfs_bstat		*bs1)
{
	memset(bs5, 0, sizeof(*bs5));
	bs5->bs_version = XFS_BULKSTAT_VERSION_V1;

	bs5->bs_ino = bs1->bs_ino;
	bs5->bs_mode = bs1->bs_mode;
	bs5->bs_nlink = bs1->bs_nlink;
	bs5->bs_uid = bs1->bs_uid;
	bs5->bs_gid = bs1->bs_gid;
	bs5->bs_rdev = bs1->bs_rdev;
	bs5->bs_blksize = bs1->bs_blksize;
	bs5->bs_size = bs1->bs_size;
	bs5->bs_atime = bs1->bs_atime.tv_sec;
	bs5->bs_mtime = bs1->bs_mtime.tv_sec;
	bs5->bs_ctime = bs1->bs_ctime.tv_sec;
	bs5->bs_atime_nsec = bs1->bs_atime.tv_nsec;
	bs5->bs_mtime_nsec = bs1->bs_mtime.tv_nsec;
	bs5->bs_ctime_nsec = bs1->bs_ctime.tv_nsec;
	bs5->bs_blocks = bs1->bs_blocks;
	bs5->bs_xflags = bs1->bs_xflags;
	bs5->bs_extsize_blks = cvt_b_to_off_fsbt(xfd, bs1->bs_extsize);
	bs5->bs_gen = bs1->bs_gen;
	bs5->bs_projectid = bstat_get_projid(bs1);
	bs5->bs_forkoff = bs1->bs_forkoff;
	bs5->bs_sick = bs1->bs_sick;
	bs5->bs_checked = bs1->bs_checked;
	bs5->bs_cowextsize_blks = cvt_b_to_off_fsbt(xfd, bs1->bs_cowextsize);
	bs5->bs_aextents = bs1->bs_aextents;
	bs5->bs_extents64 = bs1->bs_extents;
}

/* Allocate a bulkstat request.  Returns zero or a negative error code. */
int
xfrog_bulkstat_alloc_req(
	uint32_t		nr,
	uint64_t		startino,
	struct xfs_bulkstat_req **preq)
{
	struct xfs_bulkstat_req	*breq;

	breq = calloc(1, XFS_BULKSTAT_REQ_SIZE(nr));
	if (!breq)
		return -ENOMEM;

	breq->hdr.icount = nr;
	breq->hdr.ino = startino;

	*preq = breq;
	return 0;
}

/* Set a bulkstat cursor to iterate only a particular AG. */
void
xfrog_bulkstat_set_ag(
	struct xfs_bulkstat_req	*req,
	uint32_t		agno)
{
	req->hdr.agno = agno;
	req->hdr.flags |= XFS_BULK_IREQ_AGNO;
}

/* Convert a inumbers data from v5 format to v1 format. */
void
xfrog_inumbers_v5_to_v1(
	struct xfs_inogrp		*ig1,
	const struct xfs_inumbers	*ig5)
{
	ig1->xi_startino = ig5->xi_startino;
	ig1->xi_alloccount = ig5->xi_alloccount;
	ig1->xi_allocmask = ig5->xi_allocmask;
}

/* Convert a inumbers data from v1 format to v5 format. */
void
xfrog_inumbers_v1_to_v5(
	struct xfs_inumbers		*ig5,
	const struct xfs_inogrp		*ig1)
{
	memset(ig5, 0, sizeof(*ig5));
	ig5->xi_version = XFS_INUMBERS_VERSION_V1;

	ig5->xi_startino = ig1->xi_startino;
	ig5->xi_alloccount = ig1->xi_alloccount;
	ig5->xi_allocmask = ig1->xi_allocmask;
}

static uint64_t xfrog_inum_ino(void *v1_rec)
{
	return ((struct xfs_inogrp *)v1_rec)->xi_startino;
}

static void xfrog_inum_cvt(struct xfs_fd *xfd, void *v5, void *v1)
{
	xfrog_inumbers_v1_to_v5(v5, v1);
}

/* Query inode allocation bitmask information using v5 ioctl. */
static int
xfrog_inumbers5(
	struct xfs_fd		*xfd,
	struct xfs_inumbers_req	*req)
{
	int			ret;

	ret = ioctl(xfd->fd, XFS_IOC_INUMBERS, req);
	if (ret)
		return -errno;
	return 0;
}

/* Query inode allocation bitmask information using v1 ioctl. */
static int
xfrog_inumbers1(
	struct xfs_fd		*xfd,
	struct xfs_inumbers_req	*req)
{
	struct xfs_fsop_bulkreq	bulkreq = { 0 };
	int			error;

	error = xfrog_bulkstat_prep_v1_emulation(xfd);
	if (error)
		return error;

	error = xfrog_bulk_req_v1_setup(xfd, &req->hdr, &bulkreq,
			sizeof(struct xfs_inogrp));
	if (error == -ECANCELED)
		goto out_teardown;
	if (error)
		return error;

	error = ioctl(xfd->fd, XFS_IOC_FSINUMBERS, &bulkreq);
	if (error)
		error = -errno;

out_teardown:
	return xfrog_bulk_req_v1_cleanup(xfd, &req->hdr, &bulkreq,
			sizeof(struct xfs_inogrp), xfrog_inum_ino,
			&req->inumbers, sizeof(struct xfs_inumbers),
			xfrog_inum_cvt, 64, error);
}

/*
 * Query inode allocation bitmask information.  Returns zero or a negative
 * error code.
 */
int
xfrog_inumbers(
	struct xfs_fd		*xfd,
	struct xfs_inumbers_req	*req)
{
	int			error;

	if (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V1)
		goto try_v1;

	error = xfrog_inumbers5(xfd, req);
	if (error == 0 || (xfd->flags & XFROG_FLAG_BULKSTAT_FORCE_V5))
		return error;

	/* If the v5 ioctl wasn't found, we punt to v1. */
	switch (error) {
	case -EOPNOTSUPP:
	case -ENOTTY:
		xfd->flags |= XFROG_FLAG_BULKSTAT_FORCE_V1;
		break;
	}

try_v1:
	return xfrog_inumbers1(xfd, req);
}

/* Allocate a inumbers request.  Returns zero or a negative error code. */
int
xfrog_inumbers_alloc_req(
	uint32_t		nr,
	uint64_t		startino,
	struct xfs_inumbers_req **preq)
{
	struct xfs_inumbers_req	*ireq;

	ireq = calloc(1, XFS_INUMBERS_REQ_SIZE(nr));
	if (!ireq)
		return -errno;

	ireq->hdr.icount = nr;
	ireq->hdr.ino = startino;

	*preq = ireq;
	return 0;
}

/* Set an inumbers cursor to iterate only a particular AG. */
void
xfrog_inumbers_set_ag(
	struct xfs_inumbers_req	*req,
	uint32_t		agno)
{
	req->hdr.agno = agno;
	req->hdr.flags |= XFS_BULK_IREQ_AGNO;
}