1#!/bin/ksh -p
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13#
14# Copyright 2018 Nutanix Inc.  All rights reserved.
15#
16
17. $STF_SUITE/tests/functional/casenorm/casenorm.kshlib
18
19# DESCRIPTION:
20# For the filesystem with casesensitivity=mixed, normalization=none,
21# when multiple files with the same name (differing only in case) are created,
22# the number of files is limited to what can fit in a fatzap leaf-block.
23# And beyond that, it fails with ENOSPC.
24#
25# Ensure that the create/rename operations fail gracefully and not trigger an
26# ASSERT.
27#
28# STRATEGY:
29# Repeat the below steps for objects: files, directories, symlinks and hardlinks
30# 1. Create objects with same name but varying in case.
31#    E.g. 'abcdefghijklmnop', 'Abcdefghijklmnop', 'ABcdefghijklmnop' etc.
32#    The create should fail with ENOSPC.
33# 2. Create an object with name 'tmp_obj' and try to rename it to name that we
34#    failed to add in step 1 above.
35#    This should fail as well.
36
37verify_runnable "global"
38
39function cleanup
40{
41        destroy_testfs
42}
43
44log_onexit cleanup
45log_assert "With mixed mode: ensure create fails with ENOSPC beyond a certain limit"
46
47create_testfs "-o casesensitivity=mixed -o normalization=none"
48
49# Different object types
50obj_type=('file' 'dir' 'symlink' 'hardlink')
51
52# Commands to create different object types
53typeset -A ops
54ops['file']='touch'
55ops['dir']='mkdir'
56ops['symlink']='ln -s'
57ops['hardlink']='ln'
58
59# This function tests the following for a give object type :
60# - Create multiple objects with the same name (varying only in case).
61#   Ensure that it eventually fails once the leaf-block limit is exceeded.
62# - Create another object with a different name. And attempt rename it to the
63#   name (for which the create had failed in the previous step).
64#   This should fail as well.
65# Args :
66#   $1 - object type (file/dir/symlink/hardlink)
67#   $2 - test directory
68#
69function test_ops
70{
71	typeset obj_type=$1
72	typeset testdir=$2
73	typeset save_name=
74
75	target_obj='target-file'
76
77	op="${ops[$obj_type]}"
78
79	log_note "The op : $op"
80	log_note "testdir=$testdir obj_type=$obj_type"
81
82	test_path="$testdir/$obj_type"
83	mkdir $test_path
84	log_note "Created test dir $test_path"
85
86	if [[ $obj_type = "symlink" || $obj_type = "hardlink" ]]; then
87		> $test_path/$target_obj
88		log_note "Created target: $test_path/$target_obj"
89		op="$op $test_path/$target_obj"
90	fi
91
92	log_note "op : $op"
93	names='{a,A}{b,B}{c,C}{d,D}{e,E}{f,F}{g,G}{h,H}{i,I}{j,J}{k,K}{l,L}'
94	for name in $names; do
95		cmd="$op $test_path/$name"
96		out=$($cmd 2>&1)
97		ret=$?
98		log_note "cmd: $cmd ret: $ret out=$out"
99		if (($ret != 0)); then
100			if [[ $out = *@(No space left on device)* ]]; then
101				save_name="$test_path/$name"
102				break;
103			else
104				log_fail "$cmd failed: $out"
105			fi
106		fi
107	done
108	[ -n "$save_name" ] || log_fail "Didn't ENOSPC!"
109
110	log_note 'Test rename "sample_name" rename'
111	TMP_OBJ="$test_path/tmp_obj"
112	cmd="$op $TMP_OBJ"
113	log_must $cmd
114
115	# Now, try to rename the tmp_obj to the name which we failed to add earlier.
116	# This should fail as well.
117	if ! out=$(mv $TMP_OBJ $save_name 2>&1); then
118		if [[ $out = *@(No space left on device)* ]]; then
119			log_note "$cmd failed as expected: $out"
120		else
121			log_fail "$cmd failed: $out"
122		fi
123	fi
124}
125
126for obj_type in ${obj_type[*]};
127do
128	log_note "Testing create of $obj_type"
129	test_ops $obj_type $TESTDIR
130done
131
132log_pass "Mixed mode FS: Ops on large number of colliding names fail gracefully"
133