File: pwrite.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 (499 lines) | stat: -rw-r--r-- 10,814 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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

#include <sys/uio.h>
#include "command.h"
#include "input.h"
#include "init.h"
#include "io.h"

static cmdinfo_t pwrite_cmd;

static void
pwrite_help(void)
{
	printf(_(
"\n"
" writes a range of bytes (in block size increments) from the given offset\n"
"\n"
" Example:\n"
" 'pwrite 512 20' - writes 20 bytes at 512 bytes into the open file\n"
"\n"
" Writes into a segment of the currently open file, using either a buffer\n"
" filled with a set pattern (0xcdcdcdcd) or data read from an input file.\n"
" The writes are performed in sequential blocks starting at offset, with the\n"
" blocksize tunable using the -b option (default blocksize is 4096 bytes),\n"
" unless a different write pattern is requested.\n"
" -q   -- quiet mode, do not write anything to standard output.\n"
" -S   -- use an alternate seed number for filling the write buffer\n"
" -i   -- input file, source of data to write (used when writing forward)\n"
" -d   -- open the input file for direct IO\n"
" -s   -- skip a number of bytes at the start of the input file\n"
" -w   -- call fdatasync(2) at the end (included in timing results)\n"
" -W   -- call fsync(2) at the end (included in timing results)\n"
" -B   -- write backwards through the range from offset (backwards N bytes)\n"
" -F   -- write forwards through the range of bytes from offset (default)\n"
" -O   -- perform pwrite call once and return (maybe partial) bytes written\n"
" -R   -- write at random offsets in the specified range of bytes\n"
" -Z N -- zeed the random number generator (used when writing randomly)\n"
"         (heh, zorry, the -s/-S arguments were already in use in pwrite)\n"
" -V N -- use vectored IO with N iovecs of blocksize each (pwritev)\n"
#ifdef HAVE_PWRITEV2
" -N   -- Perform the pwritev2() with RWF_NOWAIT\n"
" -D   -- Perform the pwritev2() with RWF_DSYNC\n"
" -A   -- Perform the pwritev2() with RWF_ATOMIC\n"
" -U   -- Perform the pwritev2() with RWF_DONTCACHE\n"
#endif
"\n"));
}

static ssize_t
do_pwritev(
	int		fd,
	off_t		offset,
	long long	count,
	int		pwritev2_flags)
{
	int vecs = 0;
	ssize_t oldlen = 0;
	ssize_t bytes = 0;

	/* trim the iovec if necessary */
	if (count < io_buffersize) {
		size_t	len = 0;
		while (len + iov[vecs].iov_len < count) {
			len += iov[vecs].iov_len;
			vecs++;
		}
		oldlen = iov[vecs].iov_len;
		iov[vecs].iov_len = count - len;
		vecs++;
	} else {
		vecs = vectors;
	}
#ifdef HAVE_PWRITEV2
	if (pwritev2_flags)
		bytes = pwritev2(fd, iov, vectors, offset, pwritev2_flags);
	else
		bytes = pwritev(fd, iov, vectors, offset);
#else
	bytes = pwritev(fd, iov, vectors, offset);
#endif

	/* restore trimmed iov */
	if (oldlen)
		iov[vecs - 1].iov_len = oldlen;

	return bytes;
}

static ssize_t
do_pwrite(
	int		fd,
	off_t		offset,
	long long	count,
	size_t		buffer_size,
	int		pwritev2_flags)
{
	if (!vectors)
		return pwrite(fd, io_buffer, min(count, buffer_size), offset);

	return do_pwritev(fd, offset, count, pwritev2_flags);
}

static int
write_random(
	off_t		offset,
	long long	count,
	unsigned int	seed,
	long long	*total,
	int 		pwritev2_flags)
{
	off_t		off, range;
	ssize_t		bytes;
	int		ops = 0;

	srandom(seed);
	if ((bytes = (offset % io_buffersize)))
		offset -= bytes;
	offset = max(0, offset);
	if ((bytes = (count % io_buffersize)))
		count += bytes;
	count = max(io_buffersize, count);
	range = count - io_buffersize;

	*total = 0;
	while (count > 0) {
		if (range)
			off = ((offset + (random() % range)) / io_buffersize) *
				io_buffersize;
		else
			off = offset;
		bytes = do_pwrite(file->fd, off, io_buffersize, io_buffersize,
				pwritev2_flags);
		if (bytes == 0)
			break;
		if (bytes < 0) {
			perror("pwrite");
			return -1;
		}
		ops++;
		*total += bytes;
		if (bytes < io_buffersize)
			break;
		count -= bytes;
	}
	return ops;
}

static int
write_backward(
	off_t		offset,
	long long	*count,
	long long	*total,
	int		pwritev2_flags)
{
	off_t		end, off = offset;
	ssize_t		bytes = 0, bytes_requested;
	long long	cnt = *count;
	int		ops = 0;

	if ((end = off - cnt) < 0) {
		cnt += end;	/* subtraction, end is negative */
		end = 0;
	}
	*total = 0;
	*count = cnt;

	/* Do initial unaligned write if needed */
	if ((bytes_requested = (off % io_buffersize))) {
		bytes_requested = min(cnt, bytes_requested);
		off -= bytes_requested;
		bytes = do_pwrite(file->fd, off, bytes_requested, io_buffersize,
				pwritev2_flags);
		if (bytes == 0)
			return ops;
		if (bytes < 0) {
			perror("pwrite");
			return -1;
		}
		ops++;
		*total += bytes;
		if (bytes < bytes_requested)
			return ops;
		cnt -= bytes;
	}

	/* Iterate backward through the rest of the range */
	while (cnt > end) {
		bytes_requested = min(cnt, io_buffersize);
		off -= bytes_requested;
		bytes = do_pwrite(file->fd, off, cnt, io_buffersize,
				pwritev2_flags);
		if (bytes == 0)
			break;
		if (bytes < 0) {
			perror("pwrite");
			return -1;
		}
		ops++;
		*total += bytes;
		if (bytes < bytes_requested)
			break;
		cnt -= bytes;
	}
	return ops;
}

static int
write_buffer(
	off_t		offset,
	long long	count,
	size_t		bs,
	int		fd,
	off_t		skip,
	long long	*total,
	int		pwritev2_flags)
{
	ssize_t		bytes;
	long long	bar = min(bs, count);
	int		ops = 0;

	*total = 0;
	while (count >= 0) {
		if (fd > 0) {	/* input file given, read buffer first */
			if (read_buffer(fd, skip + *total, bs, &bar, 0, 1) < 0)
				break;
		}
		bytes = do_pwrite(file->fd, offset, count, bar, pwritev2_flags);
		if (bytes == 0)
			break;
		if (bytes < 0) {
			perror("pwrite");
			return -1;
		}
		ops++;
		*total += bytes;
		if (bytes <  min(count, bar))
			break;
		offset += bytes;
		count -= bytes;
		if (count == 0)
			break;
	}
	return ops;
}

static int
write_once(
	off_t		offset,
	long long	count,
	long long	*total,
	int		pwritev2_flags)
{
	ssize_t bytes;
	bytes = do_pwrite(file->fd, offset, count, count, pwritev2_flags);
	if (bytes < 0) {
		perror("pwrite");
		return -1;
	}
	*total = bytes;
	return 1;
}


static int
pwrite_f(
	int		argc,
	char		**argv)
{
	size_t		bsize;
	off_t		offset, skip = 0;
	long long	count, total, tmp;
	unsigned int	zeed = 0, seed = 0xcdcdcdcd;
	size_t		fsblocksize, fssectsize;
	struct timeval	t1, t2;
	char		*sp, *infile = NULL;
	int		Cflag, qflag, uflag, dflag, wflag, Wflag;
	int		direction = IO_FORWARD;
	int		c, fd = -1;
	int		pwritev2_flags = 0;

	Cflag = qflag = uflag = dflag = wflag = Wflag = 0;
	init_cvtnum(&fsblocksize, &fssectsize);
	bsize = fsblocksize;

	while ((c = getopt(argc, argv, "Ab:BCdDf:Fi:NqRs:OS:uUV:wWZ:")) != EOF) {
		switch (c) {
		case 'b':
			tmp = cvtnum(fsblocksize, fssectsize, optarg);
			if (tmp < 0) {
				printf(_("non-numeric bsize -- %s\n"), optarg);
				exitcode = 1;
				return 0;
			}
			bsize = tmp;
			break;
		case 'C':
			Cflag = 1;
			break;
		case 'F':
			direction = IO_FORWARD;
			break;
		case 'B':
			direction = IO_BACKWARD;
			break;
		case 'R':
			direction = IO_RANDOM;
			break;
		case 'O':
			direction = IO_ONCE;
			break;
		case 'd':
			dflag = 1;
			break;
		case 'f':
		case 'i':
			infile = optarg;
			break;
#ifdef HAVE_PWRITEV2
		case 'N':
			pwritev2_flags |= RWF_NOWAIT;
			break;
		case 'D':
			pwritev2_flags |= RWF_DSYNC;
			break;
		case 'A':
			pwritev2_flags |= RWF_ATOMIC;
			break;
		case 'U':
			pwritev2_flags |= RWF_DONTCACHE;
			break;
#endif
		case 's':
			skip = cvtnum(fsblocksize, fssectsize, optarg);
			if (skip < 0) {
				printf(_("non-numeric skip -- %s\n"), optarg);
				exitcode = 1;
				return 0;
			}
			break;
		case 'S':
			seed = strtoul(optarg, &sp, 0);
			if (!sp || sp == optarg) {
				printf(_("non-numeric seed -- %s\n"), optarg);
				exitcode = 1;
				return 0;
			}
			break;
		case 'q':
			qflag = 1;
			break;
		case 'u':
			uflag = 1;
			break;
		case 'V':
			vectors = strtoul(optarg, &sp, 0);
			if (!sp || sp == optarg) {
				printf(_("non-numeric vector count == %s\n"),
					optarg);
				exitcode = 1;
				return 0;
			}
			break;
		case 'w':
			wflag = 1;
			break;
		case 'W':
			Wflag = 1;
			break;
		case 'Z':
			zeed = strtoul(optarg, &sp, 0);
			if (!sp || sp == optarg) {
				printf(_("non-numeric seed -- %s\n"), optarg);
				exitcode = 1;
				return 0;
			}
			break;
		default:
			/* Handle ifdef'd-out options above */
			exitcode = 1;
			if (c != '?')
				printf(_("%s: command -%c not supported\n"), argv[0], c);
			else
				command_usage(&pwrite_cmd);
			return 0;
		}
	}
	if (((skip || dflag) && !infile) || (optind != argc - 2)) {
		exitcode = 1;
		return command_usage(&pwrite_cmd);
	}
	if (infile && direction != IO_FORWARD) {
		exitcode = 1;
		return command_usage(&pwrite_cmd);
	}
	if (pwritev2_flags != 0 && vectors == 0) {
		printf(_("pwritev2 flags require vectored I/O (-V)\n"));
		exitcode = 1;
		return command_usage(&pwrite_cmd);
	}

	offset = cvtnum(fsblocksize, fssectsize, argv[optind]);
	if (offset < 0) {
		printf(_("non-numeric offset argument -- %s\n"), argv[optind]);
		exitcode = 1;
		return 0;
	}
	optind++;
	count = cvtnum(fsblocksize, fssectsize, argv[optind]);
	if (count < 0) {
		printf(_("non-numeric length argument -- %s\n"), argv[optind]);
		exitcode = 1;
		return 0;
	}

	if (alloc_buffer(bsize, uflag, seed) < 0) {
		exitcode = 1;
		return 0;
	}

	c = IO_READONLY | (dflag ? IO_DIRECT : 0);
	if (infile && ((fd = openfile(infile, NULL, c, 0, NULL)) < 0)) {
		exitcode = 1;
		return 0;
	}

	gettimeofday(&t1, NULL);
	switch (direction) {
	case IO_RANDOM:
		if (!zeed)	/* srandom seed */
			zeed = time(NULL);
		c = write_random(offset, count, zeed, &total, pwritev2_flags);
		break;
	case IO_FORWARD:
		c = write_buffer(offset, count, bsize, fd, skip, &total,
				pwritev2_flags);
		break;
	case IO_BACKWARD:
		c = write_backward(offset, &count, &total, pwritev2_flags);
		break;
	case IO_ONCE:
		c = write_once(offset, count, &total, pwritev2_flags);
		break;
	default:
		total = 0;
		ASSERT(0);
	}
	if (c < 0) {
		exitcode = 1;
		goto done;
	}
	if (Wflag) {
		if (fsync(file->fd) < 0) {
			perror("fsync");
			exitcode = 1;
			goto done;
		}
	}
	if (wflag) {
		if (fdatasync(file->fd) < 0) {
			perror("fdatasync");
			exitcode = 1;
			goto done;
		}
	}

	if (qflag)
		goto done;
	gettimeofday(&t2, NULL);
	t2 = tsub(t2, t1);

	report_io_times("wrote", &t2, (long long)offset, count, total, c,
			Cflag);
done:
	if (infile)
		close(fd);
	return 0;
}

void
pwrite_init(void)
{
	pwrite_cmd.name = "pwrite";
	pwrite_cmd.altname = "w";
	pwrite_cmd.cfunc = pwrite_f;
	pwrite_cmd.argmin = 2;
	pwrite_cmd.argmax = -1;
	pwrite_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
	pwrite_cmd.args =
_("[-i infile [-qAdDwNOUW] [-s skip]] [-b bs] [-S seed] [-FBR [-Z N]] [-V N] off len");
	pwrite_cmd.oneline =
		_("writes a number of bytes at a specified offset");
	pwrite_cmd.help = pwrite_help;

	add_command(&pwrite_cmd);
}