summaryrefslogtreecommitdiff
path: root/common/punch
blob: 4d16b898c00cc5c9fe88ef3bc7b25b6df0479b2e (plain)
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
##/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2007 Silicon Graphics, Inc.  All Rights Reserved.
#
# common functions for excersizing hole punches with extent size hints etc.

_spawn_test_file() {
	echo "# spawning test file with $*"
	local blksize=$1
	local file_size=`expr $2 \* $blksize`
	local extent_size_hint=`expr $3 \* $blksize`
	local test_file=$4
	local reserve_space=$5

	if [ $extent_size_hint -ne 0 ]; then
		echo "+ setting extent size hint to $extent_size_hint"
		$XFS_IO_PROG -f \
		-c "extsize $extent_size_hint" \
		$test_file
	fi
	# print extent size hint for $test_file
	$XFS_IO_PROG -f \
	-c "extsize" \
	$test_file

	if [ "$reserve_space" == "noresv" ]; then
		echo "+ not using resvsp at file creation"
		$XFS_IO_PROG -f \
		-c "truncate $file_size" \
		$test_file
	else
		$XFS_IO_PROG -f \
		-c "truncate $file_size" \
		-c "resvsp 0 $file_size" \
		$test_file
	fi
}

_do_write() {
	echo "# writing with $*"
	local blksize=$1
	local write_offset=`expr $2 \* $blksize`
	local write_size=`expr $3 \* $blksize`
	local test_file=$4

	$XFS_IO_PROG -f \
	-c "pwrite $write_offset $write_size" \
	$test_file >/dev/null
}

_do_bmap() {
	echo "# showing file state $*"
	local test_file=$1

	$XFS_IO_PROG -f \
	-c "bmap -vvp" \
	$test_file
}

_coalesce_extents()
{
	block_size=$1

	[[ -z $block_size ]] && block_size=512

	awk -v block_size="$block_size" -F: '
	{
		range = $2;
		type = $3;

		split(range, bounds, "[\\[ \\.\\]]");
		low = bounds[3];
		high = bounds[5];

		if (type != prev_type) {
			if (prev_type != "")
				printf("%u]:%s\n", (low * 512 / block_size) - 1,
					prev_type);
			printf("%u: [%u..", out_count++,
				(low * 512) / block_size);
			prev_type = type;
		}
	}
	END {
		if (prev_type != "")
			printf("%u]:%s\n", ((high + 1) * 512 / block_size) - 1,
				prev_type);
	}'
}

_filter_fiemap()
{
	block_size=$1

	$AWK_PROG '
		$3 ~ /hole/ {
			print $1, $2, $3;
			next;
		}
		$5 ~ /0x[[:xdigit:]]*8[[:xdigit:]][[:xdigit:]]/ {
			print $1, $2, "unwritten";
			next;
		}
		$5 ~ /0x[[:xdigit:]]+/ {
			print $1, $2, "data";
		}' |
	_coalesce_extents $block_size
}

_filter_fiemap_flags()
{
	$AWK_PROG '
		$3 ~ /hole/ {
			print $1, $2, $3;
			next;
		}
		$5 ~ /0x[[:xdigit:]]*8[[:xdigit:]][[:xdigit:]]/ {
			print $1, $2, "unwritten";
			next;
		}
		$5 ~ /0x[[:xdigit:]]+/ {
			flags = strtonum($5);
			flag_str = "none";
			set = 0;

			if (and(flags, 0x2000)) {
				flag_str = "shared";
				set = 1;
			}
			if (and(flags, 0x1)) {
				if (set) {
					flag_str = flag_str"|";
				}
				flag_str = flag_str"last";
			}
			print $1, $2, flag_str
		}' |
	_coalesce_extents
}

# Filters fiemap output to only print the 
# file offset column and whether or not
# it is an extent or a hole
_filter_hole_fiemap()
{
	$AWK_PROG '
		$3 ~ /hole/ {
			print $1, $2, $3; 
			next;
		}   
		$5 ~ /0x[[:xdigit:]]+/ {
			print $1, $2, "extent";
		}' |
	_coalesce_extents
}

#     10000 Unwritten preallocated extent
#     01000 Doesn't begin on stripe unit
#     00100 Doesn't end   on stripe unit
#     00010 Doesn't begin on stripe width
#     00001 Doesn't end   on stripe width
_filter_bmap()
{
	awk '
		$3 ~ /hole/ {
			print $1, $2, $3;
			next;
		}
		$7 ~ /1[01][01][01][01]/ {
			print $1, $2, "unwritten";
			next;
		}
		$7 ~ /0[01][01][01][01]/ {
			print $1, $2, "data"
		}' |
	_coalesce_extents
}

die_now()
{
	status=1
	exit
}

# test the different corner cases for zeroing a range:
#
#	1. into a hole
#	2. into allocated space
#	3. into unwritten space
#	4. hole -> data
#	5. hole -> unwritten
#	6. data -> hole
#	7. data -> unwritten
#	8. unwritten -> hole
#	9. unwritten -> data
#	10. hole -> data -> hole
#	11. data -> hole -> data
#	12. unwritten -> data -> unwritten
#	13. data -> unwritten -> data
#	14. data -> hole @ EOF
#	15. data -> hole @ 0
#	16. data -> cache cold ->hole
#	17. data -> hole in single block file
#
# Test file is removed, created and sync'd between tests.
#
# Use -k flag to keep the file between tests.  This will
# test the handling of pre-existing holes.
#
# Use the -d flag to not sync the file between tests.
# This will test the handling of delayed extents
#
# Use the -u flag to not run unwritten tests.
# This will eliminate some unnecessary information.
#
_test_generic_punch()
{

	remove_testfile=1
	sync_cmd="-c fsync"
	unwritten_tests=1
	OPTIND=1
	while getopts 'dku' OPTION
	do
		case $OPTION in
		k)	remove_testfile=
		;;
		d)	sync_cmd=
		;;
		u)	unwritten_tests=
		;;
		?)	echo Invalid flag
		exit 1
		;;
		esac
	done
	shift $(($OPTIND - 1))

	alloc_cmd=$1
	punch_cmd=$2
	zero_cmd=$3	#if not testing zero just set to punch
	map_cmd=$4
	filter_cmd=$5
	testfile=$6

	# The punch hole tests needs multiple of the largest extent size being
	# tested, with multiple=16 it can test extent size upto 64k.
	multiple=16
	_4k="$((multiple * 4))k"
	_8k="$((multiple * 8))k"
	_12k="$((multiple * 12))k"
	_20k="$((multiple * 20))k"

	# initial test state must be defined, otherwise the first test can fail
	# due ot stale file state left from previous tests.
	rm -f $testfile

	echo "	1. into a hole"
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "$zero_cmd $_4k $_8k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	echo "	2. into allocated space"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite 0 $_20k" $sync_cmd \
		-c "$zero_cmd $_4k $_8k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	if [ "$unwritten_tests" ]; then
		echo "	3. into unwritten space"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd 0 $_20k" \
			-c "$zero_cmd $_4k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile
	fi

	echo "	4. hole -> data"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite $_8k $_8k" $sync_cmd \
		-c "$zero_cmd $_4k $_8k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	if [ "$unwritten_tests" ]; then
		echo "	5. hole -> unwritten"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd $_8k $_8k" \
			-c "$zero_cmd $_4k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile
	fi

	echo "	6. data -> hole"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite 0 $_8k" $sync_cmd \
		 -c "$zero_cmd $_4k $_8k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	if [ "$unwritten_tests" ]; then
		echo "	7. data -> unwritten"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "pwrite 0 $_8k" $sync_cmd \
			-c "$alloc_cmd $_8k $_8k" \
			-c "$zero_cmd $_4k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile

		echo "	8. unwritten -> hole"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd 0 $_8k" \
			-c "$zero_cmd $_4k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile

		echo "	9. unwritten -> data"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd 0 $_8k" \
			-c "pwrite $_8k $_8k" $sync_cmd \
			-c "$zero_cmd $_4k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile
	fi

	echo "	10. hole -> data -> hole"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite $_8k $_4k" $sync_cmd \
		-c "$zero_cmd $_4k $_12k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	echo "	11. data -> hole -> data"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "$alloc_cmd 0 $_20k" \
		-c "pwrite 0 $_8k" \
		-c "pwrite $_12k $_8k" $sync_cmd \
		-c "$punch_cmd $_8k $_4k" \
		-c "$zero_cmd $_4k $_12k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	if [ "$unwritten_tests" ]; then
		echo "	12. unwritten -> data -> unwritten"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd 0 $_20k" \
			-c "pwrite $_8k $_4k" $sync_cmd \
			-c "$zero_cmd $_4k $_12k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile

		echo "	13. data -> unwritten -> data"
		if [ "$remove_testfile" ]; then
			rm -f $testfile
		fi
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "$alloc_cmd 0 $_20k" \
			-c "pwrite 0k $_4k" $sync_cmd \
			-c "pwrite $_12k $_8k" -c "fsync" \
			-c "$zero_cmd $_4k $_12k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile
	fi

	# Don't need to check EOF case for collapse range.
	# VFS layer return invalid error in this case,
	# So it is not a proper case for collapse range test of each local fs.
	if [ "$zero_cmd" != "fcollapse" ]; then
		echo "	14. data -> hole @ EOF"
		rm -f $testfile
		$XFS_IO_PROG -f -c "truncate $_20k" \
			-c "pwrite 0 $_20k" $sync_cmd \
			-c "$zero_cmd $_12k $_8k" \
			-c "$map_cmd -v" $testfile | $filter_cmd
		[ $? -ne 0 ] && die_now
		_md5_checksum $testfile
	fi

	if [ "$zero_cmd" == "fcollapse" ]; then
		echo "	14. data -> hole @ 0"
	else
		echo "	15. data -> hole @ 0"
	fi

	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite 0 $_20k" $sync_cmd \
		-c "$zero_cmd 0 $_8k" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	[ $? -ne 0 ] && die_now
	_md5_checksum $testfile

	# If zero_cmd is fcollpase, don't check unaligned offsets
	if [ "$zero_cmd" == "fcollapse" ]; then
		return
	fi

	# If zero_cmd is finsert, don't check unaligned offsets
	if [ "$zero_cmd" == "finsert" ]; then
		return
	fi

	echo "	16. data -> cache cold ->hole"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
		rm -f $testfile.2
	else
		cp $testfile $testfile.2
	fi
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite $_8k $_12k" -c "fsync" $testfile.2 \
		> /dev/null
	$XFS_IO_PROG -f -c "truncate $_20k" \
		-c "pwrite 0 $_20k" $sync_cmd \
		-c "$zero_cmd 0k $_8k" \
		-c "fadvise -d" \
		-c "$map_cmd -v" $testfile | $filter_cmd
	diff $testfile $testfile.2
	[ $? -ne 0 ] && die_now
	rm -f $testfile.2
	_md5_checksum $testfile

	# different file sizes mean we can't use md5sum to check the hole is
	# valid. Hence use hexdump to dump the contents and chop off the last
	# line of output that indicates the file size. We also have to fudge
	# the extent size as that will change with file size, too - that's what
	# the sed line noise does - it will always result in an output of [0..7]
	# so it matches 4k block size...
	echo "	17. data -> hole in single block file"
	if [ "$remove_testfile" ]; then
		rm -f $testfile
	fi
	block_size=`_get_file_block_size $TEST_DIR`
	$XFS_IO_PROG -f -c "truncate $block_size" \
		-c "pwrite 0 $block_size" $sync_cmd \
		-c "$zero_cmd 128 128" \
		-c "$map_cmd -v" $testfile | $filter_cmd | \
			 sed -e "s/\.\.[0-9]*\]/..7\]/"
	[ $? -ne 0 ] && die_now
	od -x $testfile | head -n -1
}

_test_block_boundaries()
{

	remove_testfile=1
	sync_cmd="-c fsync"
	unwritten_tests=1
	OPTIND=1
	while getopts 'dk' OPTION
	do
		case $OPTION in
		k)	remove_testfile=
		;;
		d)	sync_cmd=
		;;
		?)	echo Invalid flag
		exit 1
		;;
		esac
	done
	shift $(($OPTIND - 1))

	bs=$1
	zero_cmd=$2
	filter_cmd=$3
	testfile=$4

	# Block size plus 1
	bs_p1=$(($bs + 1))
	# Block size plus 2
	bs_p2=$(($bs + 2))

	# Block size minus 1
	bs_m1=$(($bs - 1))

	# Block size multiplied by 2
	bs_t2=$(($bs * 2))

	# Block size divided by 2
	bs_d2=$(($bs / 2))

	echo "zero 0, 1"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd 0 1" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero 0, $bs_m1"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd 0 $bs_m1" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero 0, $bs"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd 0 $bs" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero 0, $bs_p1"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd 0 $bs_p1" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero $bs_m1, $bs"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd $bs_m1 $bs" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero $bs_m1, $bs_p1"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd $bs_m1 $bs_p1" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd

	echo "zero $bs_m1, $bs_p2"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd $bs_m1 $bs_p2" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd


	echo "zero $bs, $bs"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd $bs $bs" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd


	echo "zero $bs_d2 , $bs"
	$XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
			   -c "pwrite -S 0x42 $bs $bs" \
			   -c "$zero_cmd $bs_d2 $bs" \
			   -c "pread -v 0 $bs_t2" \
			   $testfile | $filter_cmd
}