File: init.c

package info (click to toggle)
xfsprogs 6.18.0-4
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,304 kB
  • sloc: ansic: 167,330; sh: 4,604; makefile: 1,337; python: 835; cpp: 5
file content (274 lines) | stat: -rw-r--r-- 4,998 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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

#include <pthread.h>
#include "platform_defs.h"
#include "command.h"
#include "input.h"
#include "init.h"
#include "io.h"

char	*progname;
int	exitcode;
int	expert;
static int	idlethread;
size_t	pagesize;
struct timeval stopwatch;

static void
usage(void)
{
	fprintf(stderr,
_("Usage: %s [-adfinrRstVx] [-m mode] [-p prog] [[-c|-C] cmd]... file\n"),
		progname);
	exit(1);
}

void
init_cvtnum(
	size_t *blocksize,
	size_t *sectsize)
{
	if (!file || (file->flags & IO_FOREIGN)) {
		*blocksize = 4096;
		*sectsize = 512;
	} else {
		*blocksize = file->geom.blocksize;
		*sectsize = file->geom.sectsize;
	}
}

static void
init_commands(void)
{
	aginfo_init();
	attr_init();
	bmap_init();
	bulkstat_init();
	copy_range_init();
	cachestat_init();
	cowextsize_init();
	encrypt_init();
	fadvise_init();
	fiemap_init();
	file_init();
	flink_init();
	freeze_init();
	fsmap_init();
	fsuuid_init();
	fsync_init();
	getrusage_init();
	help_init();
	imap_init();
	inject_init();
	label_init();
	log_writes_init();
	madvise_init();
	mincore_init();
	mmap_init();
	open_init();
	parent_init();
	pread_init();
	prealloc_init();
	pwrite_init();
	quit_init();
	readdir_init();
	reflink_init();
	repair_init();
	resblks_init();
	scrub_init();
	seek_init();
	sendfile_init();
	shutdown_init();
	stat_init();
	swapext_init();
	sync_init();
	sync_range_init();
	truncate_init();
	utimes_init();
	crc32cselftest_init();
	exchangerange_init();
	fsprops_init();
}

/*
 * This allows xfs_io commands specified on the command line to be run on every
 * open file in the file table. Commands that should not be iterated across all
 * open files need to specify CMD_FLAG_ONESHOT in their command flags.
 */
static int
filetable_iterator(
	int	index)
{
	if (index >= filecount)
		return 0;
	file = &filetable[index++];
	return index;
}

static int
init_check_command(
	const cmdinfo_t	*ct)
{
	if (!file && !(ct->flags & CMD_NOFILE_OK)) {
		fprintf(stderr, _("no files are open, try 'help open'\n"));
		return 0;
	}
	if (!mapping && !(ct->flags & CMD_NOMAP_OK)) {
		fprintf(stderr, _("no mapped regions, try 'help mmap'\n"));
		return 0;
	}
	if (file && !(ct->flags & CMD_FOREIGN_OK) &&
					(file->flags & IO_FOREIGN)) {
		fprintf(stderr,
	_("foreign file active, %s command is for XFS filesystems only\n"),
			ct->name);
		return 0;
	}
	return 1;
}

static void
init(
	int		argc,
	char		**argv)
{
	int		c, flags = 0;
	char		*sp;
	mode_t		mode = 0600;
	struct xfs_fsop_geom geometry = { 0 };
	struct fs_path	fsp;

	progname = basename(argv[0]);
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	pagesize = getpagesize();
	gettimeofday(&stopwatch, NULL);

	fs_table_initialise(0, NULL, 0, NULL);
	while ((c = getopt(argc, argv, "ac:C:dFfiLm:p:PnrRstTVx")) != EOF) {
		switch (c) {
		case 'a':
			flags |= IO_APPEND;
			break;
		case 'c':
			add_user_command(optarg);
			break;
		case 'C':
			add_oneshot_user_command(optarg);
			break;
		case 'd':
			flags |= IO_DIRECT;
			break;
		case 'F':
			/* Ignored / deprecated now, handled automatically */
			break;
		case 'f':
			flags |= IO_CREAT;
			break;
		case 'i':
			idlethread = 1;
			break;
		case 'm':
			mode = strtoul(optarg, &sp, 0);
			if (!sp || sp == optarg) {
				fprintf(stderr, _("non-numeric mode -- %s\n"),
					optarg);
				exit(1);
			}
			break;
		case 'n':
			flags |= IO_NONBLOCK;
			break;
		case 'p':
			progname = optarg;
			break;
		case 'r':
			flags |= IO_READONLY;
			break;
		case 's':
			flags |= IO_OSYNC;
			break;
		case 't':
			flags |= IO_TRUNC;
			break;
		case 'P':
			flags |= IO_PATH;
			break;
		case 'L':
			flags |= IO_NOFOLLOW;
			break;
		case 'R':
			flags |= IO_REALTIME;
			break;
		case 'T':
			flags |= IO_TMPFILE;
			break;
		case 'x':
			expert = 1;
			break;
		case 'V':
			printf(_("%s version %s\n"), progname, VERSION);
			exit(0);
		default:
			usage();
		}
	}

	while (optind < argc) {
		c = openfile(argv[optind], &geometry, flags, mode, &fsp);
		if (c < 0)
			exit(1);
		if (!platform_test_xfs_fd(c))
			flags |= IO_FOREIGN;
		if (addfile(argv[optind], c, &geometry, flags, &fsp) < 0)
			exit(1);
		optind++;
	}

	init_commands();
	add_command_iterator(filetable_iterator);
	add_check_command(init_check_command);
}

/*
 * The purpose of this idle thread is to test io from a multi threaded process.
 * With single threaded process, the file table is not shared and file structs
 * are not reference counted. Spawning an idle thread can help detecting file
 * struct reference leaks.
 */
static void *
idle_loop(void *arg)
{
	for (;;)
		pause();
	return NULL;
}

static void
start_idle_thread(void)
{
	pthread_t t;

	if (pthread_create(&t, NULL, idle_loop, NULL)) {
		fprintf(stderr, "Error creating idle thread\n");
		exit(1);
	}
}

int
main(
	int	argc,
	char	**argv)
{
	init(argc, argv);
	if (idlethread)
		start_idle_thread();
	command_loop();
	return exitcode;
}