File: xfs_rtcp.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 (389 lines) | stat: -rw-r--r-- 8,010 bytes parent folder | download | duplicates (4)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

#include "libxfs.h"
#include "libfrog/fsgeom.h"

int rtcp(char *, char *, int);
int xfsrtextsize(char *path);

static int pflag;
char *progname;

static void
usage(void)
{
	fprintf(stderr, _("%s [-e extsize] [-p] [-V] source target\n"), progname);
	exit(2);
}

int
main(int argc, char **argv)
{
	int	c, i, r, errflg = 0;
	struct stat	s2;
	int		extsize = - 1;

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

	while ((c = getopt(argc, argv, "pe:V")) != EOF) {
		switch (c) {
		case 'e':
			extsize = atoi(optarg);
			break;
		case 'p':
			pflag = 1;
			break;
		case 'V':
			printf(_("%s version %s\n"), progname, VERSION);
			exit(0);
		default:
			errflg++;
		}
	}

	/*
	 * Check for sufficient arguments or a usage error.
	 */
	argc -= optind;
	argv  = &argv[optind];

	if (argc < 2) {
		fprintf(stderr, _("%s: must specify files to copy\n"),
			progname);
		errflg++;
	}

	if (errflg)
		usage();

	/*
	 * If there is more than a source and target,
	 * the last argument (the target) must be a directory
	 * which really exists.
	 */
	if (argc > 2) {
		if (stat(argv[argc-1], &s2) < 0) {
			fprintf(stderr, _("%s: stat of %s failed\n"),
				progname, argv[argc-1]);
			exit(2);
		}

		if (!S_ISDIR(s2.st_mode)) {
			fprintf(stderr,
				_("%s: final argument is not directory\n"),
				progname);
			usage();
		}
	}

	/*
	 * Perform a multiple argument rtcp by
	 * multiple invocations of rtcp().
	 */
	r = 0;
	for (i = 0; i < argc-1; i++)
		r += rtcp(argv[i], argv[argc-1], extsize);

	/*
	 * Show errors by nonzero exit code.
	 */
	exit(r?2:0);
}

int
rtcp( char *source, char *target, int fextsize)
{
	int		fromfd, tofd, readct, writect, iosz, reopen;
	int		remove = 0, rtextsize;
	char		*sp, *fbuf, *ptr;
	char		tbuf[ PATH_MAX ];
	struct stat	s1, s2;
	struct fsxattr	fsxattr;
	struct dioattr	dioattr;

	/*
	 * While source or target have trailing /, remove them
	 * unless only "/".
	 */
	sp = source + strlen(source);
	if (sp) {
		while (*--sp == '/' && sp > source)
			*sp = '\0';
	}
	sp = target + strlen(target);
	if (sp) {
		while (*--sp == '/' && sp > target)
			*sp = '\0';
	}

	if ( stat(source, &s1) ) {
		fprintf(stderr, _("%s: failed stat on %s: %s\n"),
			progname, source, strerror(errno));
		return( -1);
	}

	/*
	 * check for a realtime partition
	 */
	snprintf(tbuf, sizeof(tbuf), "%s", target);
	if ( stat(target, &s2) ) {
		if (!S_ISDIR(s2.st_mode)) {
			/* take out target file name */
			if ((ptr = strrchr(tbuf, '/')) != NULL)
				*ptr = '\0';
			else
				snprintf(tbuf, sizeof(tbuf), ".");
		}
	}

	if ( (rtextsize = xfsrtextsize( tbuf ))  <= 0 ) {
		fprintf(stderr,
			_("%s: %s filesystem has no realtime partition\n"),
			progname, tbuf);
		return( -1 );
	}

	/*
	 * check if target is a directory
	 */
	snprintf(tbuf, sizeof(tbuf), "%s", target);
	if ( !stat(target, &s2) ) {
		if (S_ISDIR(s2.st_mode)) {
			snprintf(tbuf, sizeof(tbuf), "%s/%s", target,
				basename(source));
		}
	}

	if ( stat(tbuf, &s2) ) {
		/*
		 * create the file if it does not exist
		 */
		if ( (tofd = open(tbuf, O_RDWR|O_CREAT|O_DIRECT, 0666)) < 0 ) {
			fprintf(stderr, _("%s: open of %s failed: %s\n"),
				progname, tbuf, strerror(errno));
			return( -1 );
		}
		remove = 1;

		/*
		 * mark the file as a realtime file
		 */
		fsxattr.fsx_xflags = FS_XFLAG_REALTIME;
		if (fextsize != -1 )
			fsxattr.fsx_extsize = fextsize;
		else
			fsxattr.fsx_extsize = 0;

		if ( xfsctl(tbuf, tofd, FS_IOC_FSSETXATTR, &fsxattr) ) {
			fprintf(stderr,
				_("%s: set attributes on %s failed: %s\n"),
				progname, tbuf, strerror(errno));
			close( tofd );
			unlink( tbuf );
			return( -1 );
		}
	} else {
		/*
		 * open existing file
		 */
		if ( (tofd = open(tbuf, O_RDWR|O_DIRECT)) < 0 ) {
			fprintf(stderr, _("%s: open of %s failed: %s\n"),
				progname, tbuf, strerror(errno));
			return( -1 );
		}

		if ( xfsctl(tbuf, tofd, FS_IOC_FSGETXATTR, &fsxattr) ) {
			fprintf(stderr,
				_("%s: get attributes of %s failed: %s\n"),
				progname, tbuf, strerror(errno));
			close( tofd );
			return( -1 );
		}

		/*
		 * check if the existing file is already a realtime file
		 */
		if ( !(fsxattr.fsx_xflags & FS_XFLAG_REALTIME) ) {
			fprintf(stderr, _("%s: %s is not a realtime file.\n"),
				progname, tbuf);
			close( tofd );
			return( -1 );
		}

		/*
		 * check for matching extent size
		 */
		if ( (fextsize != -1) && (fsxattr.fsx_extsize != fextsize) ) {
			fprintf(stderr, _("%s: %s file extent size is %d, "
					"instead of %d.\n"),
				progname, tbuf, fsxattr.fsx_extsize, fextsize);
			close( tofd );
			return( -1 );
		}
	}

	/*
	 * open the source file
	 */
	reopen = 0;
	if ( (fromfd = open(source, O_RDONLY|O_DIRECT)) < 0 ) {
		fprintf(stderr, _("%s: open of %s source failed: %s\n"),
			progname, source, strerror(errno));
		close( tofd );
		if (remove)
			unlink( tbuf );
		return( -1 );
	}

	fsxattr.fsx_xflags = 0;
	fsxattr.fsx_extsize = 0;
	if ( xfsctl(source, fromfd, FS_IOC_FSGETXATTR, &fsxattr) ) {
		reopen = 1;
	} else {
		if (! (fsxattr.fsx_xflags & FS_XFLAG_REALTIME) ){
			fprintf(stderr, _("%s: %s is not a realtime file.\n"),
				progname, source);
			reopen = 1;
		}
	}

	if (reopen) {
		close( fromfd );
		if ( (fromfd = open(source, O_RDONLY )) < 0 ) {
			fprintf(stderr, _("%s: open of %s source failed: %s\n"),
				progname, source, strerror(errno));
			close( tofd );
			if (remove)
				unlink( tbuf );
			return( -1 );
		}
	}

	/*
	 * get direct I/O parameters
	 */
	if ( xfsctl(tbuf, tofd, XFS_IOC_DIOINFO, &dioattr) ) {
		fprintf(stderr,
			_("%s: couldn't get direct I/O information: %s\n"),
			progname, strerror(errno));
		close( fromfd );
		close( tofd );
		if ( remove )
			unlink( tbuf );
		return( -1 );
	}

	if ( rtextsize % dioattr.d_miniosz ) {
		fprintf(stderr, _("%s: extent size %d not a multiple of %d.\n"),
			progname, rtextsize, dioattr.d_miniosz);
		close( fromfd );
		close( tofd );
		if ( remove )
			unlink( tbuf );
		return( -1 );
	}

	/*
	 * Check that the source file size is a multiple of the
	 * file system block size.
	 */
	if ( s1.st_size % dioattr.d_miniosz ) {
		printf(_("The size of %s is not a multiple of %d.\n"),
			source, dioattr.d_miniosz);
		if ( pflag ) {
			printf(_("%s will be padded to %lld bytes.\n"),
				tbuf, (long long)
				(((s1.st_size / dioattr.d_miniosz) + 1)  *
					dioattr.d_miniosz) );

		} else {
			printf(_("Use the -p option to pad %s to a "
				"size which is a multiple of %d bytes.\n"),
				tbuf, dioattr.d_miniosz);
			close( fromfd );
			close( tofd );
			if ( remove )
				unlink( tbuf );
			return( -1 );
		}
	}

	iosz =  dioattr.d_miniosz;
	fbuf = memalign( dioattr.d_mem, iosz);
	memset(fbuf, 0, iosz);

	/*
	 * read the entire source file
	 */
	while ( ( readct = read( fromfd, fbuf, iosz) ) != 0 ) {
		/*
		 * if there is a read error - break
		 */
		if (readct < 0 ) {
			break;
		}

		/*
		 * if there is a short read, pad to a block boundary
		 */
		if ( readct != iosz ) {
			if ( (readct % dioattr.d_miniosz)  != 0 )  {
				readct = ( (readct/dioattr.d_miniosz) + 1 ) *
					 dioattr.d_miniosz;
			}
		}

		/*
		 * write to target file
		 */
		writect = write( tofd, fbuf, readct);

		if ( writect != readct ) {
			fprintf(stderr, _("%s: write error: %s\n"),
				progname, strerror(errno));
			close(fromfd);
			close(tofd);
			free( fbuf );
			return( -1 );
		}

		memset( fbuf, 0, iosz);
	}

	close(fromfd);
	close(tofd);
	free( fbuf );
	return( 0 );
}

/*
 * Determine the realtime extent size of the XFS file system
 */
int
xfsrtextsize( char *path)
{
	struct xfs_fsop_geom	geo;
	int			fd, rval, rtextsize;

	fd = open( path, O_RDONLY );
	if ( fd < 0 ) {
		fprintf(stderr, _("%s: could not open %s: %s\n"),
			progname, path, strerror(errno));
		return -1;
	}
	rval = -xfrog_geometry(fd, &geo);
	close(fd);
	if (rval)
		return -1;

	rtextsize = geo.rtextsize * geo.blocksize;

	return rtextsize;
}