Whamcloud - gitweb
b9a0e431611e332bfd280643d82744c4bf8778af
[fs/lustre-release.git] / lustre / tests / sanity-sec.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 LUSTRE=${LUSTRE:-$(dirname $0)/..}
12 . $LUSTRE/tests/test-framework.sh
13 init_test_env $@
14
15 init_logging
16
17 ALWAYS_EXCEPT="$SANITY_SEC_EXCEPT "
18 # bug number for skipped test:
19 ALWAYS_EXCEPT+=" "
20 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
21
22 [ "$SLOW" = "no" ] && EXCEPT_SLOW="26"
23
24 NODEMAP_TESTS=$(seq 7 26)
25
26 if ! check_versions; then
27         echo "It is NOT necessary to test nodemap under interoperation mode"
28         EXCEPT="$EXCEPT $NODEMAP_TESTS"
29 fi
30
31 build_test_filter
32
33 RUNAS_CMD=${RUNAS_CMD:-runas}
34
35 WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
36
37 CONFDIR=/etc/lustre
38 PERM_CONF=$CONFDIR/perm.conf
39 FAIL_ON_ERROR=false
40 HOSTNAME_CHECKSUM=$(hostname | sum | awk '{ print $1 }')
41 SUBNET_CHECKSUM=$(expr $HOSTNAME_CHECKSUM % 250 + 1)
42
43 require_dsh_mds || exit 0
44 require_dsh_ost || exit 0
45
46 clients=${CLIENTS//,/ }
47 num_clients=$(get_node_count ${clients})
48 clients_arr=($clients)
49
50 ID0=${ID0:-500}
51 ID1=${ID1:-501}
52 USER0=$(getent passwd | grep :$ID0:$ID0: | cut -d: -f1)
53 USER1=$(getent passwd | grep :$ID1:$ID1: | cut -d: -f1)
54
55 NODEMAP_COUNT=16
56 NODEMAP_RANGE_COUNT=3
57 NODEMAP_IPADDR_LIST="1 10 64 128 200 250"
58 NODEMAP_ID_COUNT=10
59 NODEMAP_MAX_ID=$((ID0 + NODEMAP_ID_COUNT))
60
61 [ -z "$USER0" ] &&
62         skip "need to add user0 ($ID0:$ID0)" && exit 0
63
64 [ -z "$USER1" ] &&
65         skip "need to add user1 ($ID1:$ID1)" && exit 0
66
67 IDBASE=${IDBASE:-60000}
68
69 # changes to mappings must be reflected in test 23
70 FOPS_IDMAPS=(
71         [0]="$((IDBASE+3)):$((IDBASE+0)) $((IDBASE+4)):$((IDBASE+2))"
72         [1]="$((IDBASE+5)):$((IDBASE+1)) $((IDBASE+6)):$((IDBASE+2))"
73         )
74
75 check_and_setup_lustre
76
77 assert_DIR
78
79 # for GSS_SUP
80 GSS_REF=$(lsmod | grep ^ptlrpc_gss | awk '{print $3}')
81 if [ ! -z "$GSS_REF" -a "$GSS_REF" != "0" ]; then
82         GSS_SUP=1
83         echo "with GSS support"
84 else
85         GSS_SUP=0
86         echo "without GSS support"
87 fi
88
89 MDT=$(do_facet $SINGLEMDS lctl get_param -N "mdt.\*MDT0000" |
90         cut -d. -f2 || true)
91 [ -z "$MDT" ] && error "fail to get MDT device" && exit 1
92 do_facet $SINGLEMDS "mkdir -p $CONFDIR"
93 IDENTITY_FLUSH=mdt.$MDT.identity_flush
94 IDENTITY_UPCALL=mdt.$MDT.identity_upcall
95
96 SAVE_PWD=$PWD
97
98 sec_login() {
99         local user=$1
100         local group=$2
101
102         $GSS_KRB5 || return
103         if ! $RUNAS_CMD -u $user krb5_login.sh; then
104                 error "$user login kerberos failed."
105                 exit 1
106         fi
107
108         if ! $RUNAS_CMD -u $user -g $group ls $DIR > /dev/null 2>&1; then
109                 $RUNAS_CMD -u $user lfs flushctx -k
110                 $RUNAS_CMD -u $user krb5_login.sh
111                 if ! $RUNAS_CMD -u$user -g$group ls $DIR > /dev/null 2>&1; then
112                         error "init $user $group failed."
113                         exit 2
114                 fi
115         fi
116 }
117
118 declare -a identity_old
119
120 sec_setup() {
121         for num in $(seq $MDSCOUNT); do
122                 switch_identity $num true || identity_old[$num]=$?
123         done
124
125         if ! $RUNAS_CMD -u $ID0 ls $DIR > /dev/null 2>&1; then
126                 sec_login $USER0 $USER0
127         fi
128
129         if ! $RUNAS_CMD -u $ID1 ls $DIR > /dev/null 2>&1; then
130                 sec_login $USER1 $USER1
131         fi
132 }
133 sec_setup
134
135 # run as different user
136 test_0() {
137         umask 0022
138
139         chmod 0755 $DIR || error "chmod (1)"
140         rm -rf $DIR/$tdir || error "rm (1)"
141         mkdir -p $DIR/$tdir || error "mkdir (1)"
142         chown $USER0 $DIR/$tdir || error "chown (2)"
143         $RUNAS_CMD -u $ID0 ls $DIR || error "ls (1)"
144         rm -f $DIR/f0 || error "rm (2)"
145         $RUNAS_CMD -u $ID0 touch $DIR/f0 && error "touch (1)"
146         $RUNAS_CMD -u $ID0 touch $DIR/$tdir/f1 || error "touch (2)"
147         $RUNAS_CMD -u $ID1 touch $DIR/$tdir/f2 && error "touch (3)"
148         touch $DIR/$tdir/f3 || error "touch (4)"
149         chown root $DIR/$tdir || error "chown (3)"
150         chgrp $USER0 $DIR/$tdir || error "chgrp (1)"
151         chmod 0775 $DIR/$tdir || error "chmod (2)"
152         $RUNAS_CMD -u $ID0 touch $DIR/$tdir/f4 || error "touch (5)"
153         $RUNAS_CMD -u $ID1 touch $DIR/$tdir/f5 && error "touch (6)"
154         touch $DIR/$tdir/f6 || error "touch (7)"
155         rm -rf $DIR/$tdir || error "rm (3)"
156 }
157 run_test 0 "uid permission ============================="
158
159 # setuid/gid
160 test_1() {
161         [ $GSS_SUP = 0 ] && skip "without GSS support." && return
162
163         rm -rf $DIR/$tdir
164         mkdir -p $DIR/$tdir
165
166         chown $USER0 $DIR/$tdir || error "chown (1)"
167         $RUNAS_CMD -u $ID1 -v $ID0 touch $DIR/$tdir/f0 && error "touch (2)"
168         echo "enable uid $ID1 setuid"
169         do_facet $SINGLEMDS "echo '* $ID1 setuid' >> $PERM_CONF"
170         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
171         $RUNAS_CMD -u $ID1 -v $ID0 touch $DIR/$tdir/f1 || error "touch (3)"
172
173         chown root $DIR/$tdir || error "chown (4)"
174         chgrp $USER0 $DIR/$tdir || error "chgrp (5)"
175         chmod 0770 $DIR/$tdir || error "chmod (6)"
176         $RUNAS_CMD -u $ID1 -g $ID1 touch $DIR/$tdir/f2 && error "touch (7)"
177         $RUNAS_CMD -u$ID1 -g$ID1 -j$ID0 touch $DIR/$tdir/f3 && error "touch (8)"
178         echo "enable uid $ID1 setuid,setgid"
179         do_facet $SINGLEMDS "echo '* $ID1 setuid,setgid' > $PERM_CONF"
180         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
181         $RUNAS_CMD -u $ID1 -g $ID1 -j $ID0 touch $DIR/$tdir/f4 ||
182                 error "touch (9)"
183         $RUNAS_CMD -u $ID1 -v $ID0 -g $ID1 -j $ID0 touch $DIR/$tdir/f5 ||
184                 error "touch (10)"
185
186         rm -rf $DIR/$tdir
187
188         do_facet $SINGLEMDS "rm -f $PERM_CONF"
189         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
190 }
191 run_test 1 "setuid/gid ============================="
192
193 # bug 3285 - supplementary group should always succeed.
194 # NB: the supplementary groups are set for local client only,
195 # as for remote client, the groups of the specified uid on MDT
196 # will be obtained by upcall /sbin/l_getidentity and used.
197 test_4() {
198         [[ "$MDS1_VERSION" -ge $(version_code 2.6.93) ]] ||
199         [[ "$MDS1_VERSION" -ge $(version_code 2.5.35) &&
200            "$MDS1_VERSION" -lt $(version_code 2.5.50) ]] ||
201                 skip "Need MDS version at least 2.6.93 or 2.5.35"
202
203         rm -rf $DIR/$tdir
204         mkdir -p $DIR/$tdir
205         chmod 0771 $DIR/$tdir
206         chgrp $ID0 $DIR/$tdir
207         $RUNAS_CMD -u $ID0 ls $DIR/$tdir || error "setgroups (1)"
208         do_facet $SINGLEMDS "echo '* $ID1 setgrp' > $PERM_CONF"
209         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
210         $RUNAS_CMD -u $ID1 -G1,2,$ID0 ls $DIR/$tdir ||
211                 error "setgroups (2)"
212         $RUNAS_CMD -u $ID1 -G1,2 ls $DIR/$tdir && error "setgroups (3)"
213         rm -rf $DIR/$tdir
214
215         do_facet $SINGLEMDS "rm -f $PERM_CONF"
216         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
217 }
218 run_test 4 "set supplementary group ==============="
219
220 create_nodemaps() {
221         local i
222         local rc
223
224         squash_id default 99 0
225         wait_nm_sync default squash_uid '' inactive
226         squash_id default 99 1
227         wait_nm_sync default squash_gid '' inactive
228         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
229                 local csum=${HOSTNAME_CHECKSUM}_${i}
230
231                 do_facet mgs $LCTL nodemap_add $csum
232                 rc=$?
233                 if [ $rc -ne 0 ]; then
234                         echo "nodemap_add $csum failed with $rc"
235                         return $rc
236                 fi
237
238                 wait_update_facet --verbose mgs \
239                         "$LCTL get_param nodemap.$csum.id 2>/dev/null | \
240                         grep -c $csum || true" 1 30 ||
241                     return 1
242         done
243         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
244                 local csum=${HOSTNAME_CHECKSUM}_${i}
245
246                 wait_nm_sync $csum id '' inactive
247         done
248         return 0
249 }
250
251 delete_nodemaps() {
252         local i
253
254         for ((i = 0; i < NODEMAP_COUNT; i++)); do
255                 local csum=${HOSTNAME_CHECKSUM}_${i}
256
257                 if ! do_facet mgs $LCTL nodemap_del $csum; then
258                         error "nodemap_del $csum failed with $?"
259                         return 3
260                 fi
261
262                 wait_update_facet --verbose mgs \
263                         "$LCTL get_param nodemap.$csum.id 2>/dev/null | \
264                         grep -c $csum || true" 0 30 ||
265                     return 1
266         done
267         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
268                 local csum=${HOSTNAME_CHECKSUM}_${i}
269
270                 wait_nm_sync $csum id '' inactive
271         done
272         return 0
273 }
274
275 add_range() {
276         local j
277         local cmd="$LCTL nodemap_add_range"
278         local range
279         local rc=0
280
281         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
282                 range="$SUBNET_CHECKSUM.${2}.${j}.[1-253]@tcp"
283                 if ! do_facet mgs $cmd --name $1 --range $range; then
284                         rc=$((rc + 1))
285                 fi
286         done
287         return $rc
288 }
289
290 delete_range() {
291         local j
292         local cmd="$LCTL nodemap_del_range"
293         local range
294         local rc=0
295
296         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
297                 range="$SUBNET_CHECKSUM.${2}.${j}.[1-253]@tcp"
298                 if ! do_facet mgs $cmd --name $1 --range $range; then
299                         rc=$((rc + 1))
300                 fi
301         done
302
303         return $rc
304 }
305
306 add_idmaps() {
307         local i
308         local cmd="$LCTL nodemap_add_idmap"
309         local rc=0
310
311         echo "Start to add idmaps ..."
312         for ((i = 0; i < NODEMAP_COUNT; i++)); do
313                 local j
314
315                 for ((j = $ID0; j < NODEMAP_MAX_ID; j++)); do
316                         local csum=${HOSTNAME_CHECKSUM}_${i}
317                         local client_id=$j
318                         local fs_id=$((j + 1))
319
320                         if ! do_facet mgs $cmd --name $csum --idtype uid \
321                              --idmap $client_id:$fs_id; then
322                                 rc=$((rc + 1))
323                         fi
324                         if ! do_facet mgs $cmd --name $csum --idtype gid \
325                              --idmap $client_id:$fs_id; then
326                                 rc=$((rc + 1))
327                         fi
328                 done
329         done
330
331         return $rc
332 }
333
334 update_idmaps() { #LU-10040
335         [ "$MGS_VERSION" -lt $(version_code 2.10.55) ] &&
336                 skip "Need MGS >= 2.10.55"
337
338         local csum=${HOSTNAME_CHECKSUM}_0
339         local old_id_client=$ID0
340         local old_id_fs=$((ID0 + 1))
341         local new_id=$((ID0 + 100))
342         local tmp_id
343         local cmd
344         local run
345         local idtype
346         local rc=0
347
348         echo "Start to update idmaps ..."
349
350         #Inserting an existed idmap should return error
351         cmd="$LCTL nodemap_add_idmap --name $csum --idtype uid"
352         if do_facet mgs \
353                 $cmd --idmap $old_id_client:$old_id_fs 2>/dev/null; then
354                 error "insert idmap {$old_id_client:$old_id_fs} " \
355                         "should return error"
356                 rc=$((rc + 1))
357                 return rc
358         fi
359
360         #Update id_fs and check it
361         if ! do_facet mgs $cmd --idmap $old_id_client:$new_id; then
362                 error "$cmd --idmap $old_id_client:$new_id failed"
363                 rc=$((rc + 1))
364                 return $rc
365         fi
366         tmp_id=$(do_facet mgs $LCTL get_param -n nodemap.$csum.idmap |
367                 awk '{ print $7 }' | sed -n '2p')
368         [ $tmp_id != $new_id ] && { error "new id_fs $tmp_id != $new_id"; \
369                 rc=$((rc + 1)); return $rc; }
370
371         #Update id_client and check it
372         if ! do_facet mgs $cmd --idmap $new_id:$new_id; then
373                 error "$cmd --idmap $new_id:$new_id failed"
374                 rc=$((rc + 1))
375                 return $rc
376         fi
377         tmp_id=$(do_facet mgs $LCTL get_param -n nodemap.$csum.idmap |
378                 awk '{ print $5 }' | sed -n "$((NODEMAP_ID_COUNT + 1)) p")
379         tmp_id=$(echo ${tmp_id%,*}) #e.g. "501,"->"501"
380         [ $tmp_id != $new_id ] && { error "new id_client $tmp_id != $new_id"; \
381                 rc=$((rc + 1)); return $rc; }
382
383         #Delete above updated idmap
384         cmd="$LCTL nodemap_del_idmap --name $csum --idtype uid"
385         if ! do_facet mgs $cmd --idmap $new_id:$new_id; then
386                 error "$cmd --idmap $new_id:$new_id failed"
387                 rc=$((rc + 1))
388                 return $rc
389         fi
390
391         #restore the idmaps to make delete_idmaps work well
392         cmd="$LCTL nodemap_add_idmap --name $csum --idtype uid"
393         if ! do_facet mgs $cmd --idmap $old_id_client:$old_id_fs; then
394                 error "$cmd --idmap $old_id_client:$old_id_fs failed"
395                 rc=$((rc + 1))
396                 return $rc
397         fi
398
399         return $rc
400 }
401
402 delete_idmaps() {
403         local i
404         local cmd="$LCTL nodemap_del_idmap"
405         local rc=0
406
407         echo "Start to delete idmaps ..."
408         for ((i = 0; i < NODEMAP_COUNT; i++)); do
409                 local j
410
411                 for ((j = $ID0; j < NODEMAP_MAX_ID; j++)); do
412                         local csum=${HOSTNAME_CHECKSUM}_${i}
413                         local client_id=$j
414                         local fs_id=$((j + 1))
415
416                         if ! do_facet mgs $cmd --name $csum --idtype uid \
417                              --idmap $client_id:$fs_id; then
418                                 rc=$((rc + 1))
419                         fi
420                         if ! do_facet mgs $cmd --name $csum --idtype gid \
421                              --idmap $client_id:$fs_id; then
422                                 rc=$((rc + 1))
423                         fi
424                 done
425         done
426
427         return $rc
428 }
429
430 modify_flags() {
431         local i
432         local proc
433         local option
434         local cmd="$LCTL nodemap_modify"
435         local rc=0
436
437         proc[0]="admin_nodemap"
438         proc[1]="trusted_nodemap"
439         option[0]="admin"
440         option[1]="trusted"
441
442         for ((idx = 0; idx < 2; idx++)); do
443                 if ! do_facet mgs $cmd --name $1 --property ${option[$idx]} \
444                      --value 1; then
445                         rc=$((rc + 1))
446                 fi
447
448                 if ! do_facet mgs $cmd --name $1 --property ${option[$idx]} \
449                      --value 0; then
450                         rc=$((rc + 1))
451                 fi
452         done
453
454         return $rc
455 }
456
457 squash_id() {
458         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
459                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
460
461         local cmd
462
463         cmd[0]="$LCTL nodemap_modify --property squash_uid"
464         cmd[1]="$LCTL nodemap_modify --property squash_gid"
465
466         if ! do_facet mgs ${cmd[$3]} --name $1 --value $2; then
467                 return 1
468         fi
469 }
470
471 wait_nm_sync() {
472         local nodemap_name=$1
473         local key=$2
474         local value=$3
475         local opt=$4
476         local proc_param
477         local is_active=$(do_facet mgs $LCTL get_param -n nodemap.active)
478         local max_retries=20
479         local is_sync
480         local out1=""
481         local out2
482         local mgs_ip=$(host_nids_address $mgs_HOST $NETTYPE | cut -d' ' -f1)
483         local i
484
485         if [ "$nodemap_name" == "active" ]; then
486                 proc_param="active"
487         elif [ -z "$key" ]; then
488                 proc_param=${nodemap_name}
489         else
490                 proc_param="${nodemap_name}.${key}"
491         fi
492         if [ "$opt" == "inactive" ]; then
493                 # check nm sync even if nodemap is not activated
494                 is_active=1
495                 opt=""
496         fi
497         (( is_active == 0 )) && [ "$proc_param" != "active" ] && return
498
499         if [ -z "$value" ]; then
500                 out1=$(do_facet mgs $LCTL get_param $opt \
501                         nodemap.${proc_param} 2>/dev/null)
502                 echo "On MGS ${mgs_ip}, ${proc_param} = $out1"
503         else
504                 out1=$value;
505         fi
506
507         # wait up to 10 seconds for other servers to sync with mgs
508         for i in $(seq 1 10); do
509                 for node in $(all_server_nodes); do
510                     local node_ip=$(host_nids_address $node $NETTYPE |
511                                     cut -d' ' -f1)
512
513                     is_sync=true
514                     if [ -z "$value" ]; then
515                         [ $node_ip == $mgs_ip ] && continue
516                     fi
517
518                     out2=$(do_node $node_ip $LCTL get_param $opt \
519                                    nodemap.$proc_param 2>/dev/null)
520                     echo "On $node ${node_ip}, ${proc_param} = $out2"
521                     [ "$out1" != "$out2" ] && is_sync=false && break
522                 done
523                 $is_sync && break
524                 sleep 1
525         done
526         if ! $is_sync; then
527                 echo MGS
528                 echo $out1
529                 echo OTHER - IP: $node_ip
530                 echo $out2
531                 error "mgs and $nodemap_name ${key} mismatch, $i attempts"
532         fi
533         echo "waited $((i - 1)) seconds for sync"
534 }
535
536 # ensure that the squash defaults are the expected defaults
537 squash_id default 99 0
538 wait_nm_sync default squash_uid '' inactive
539 squash_id default 99 1
540 wait_nm_sync default squash_gid '' inactive
541
542 test_nid() {
543         local cmd
544
545         cmd="$LCTL nodemap_test_nid"
546
547         nid=$(do_facet mgs $cmd $1)
548
549         if [ $nid == $2 ]; then
550                 return 0
551         fi
552
553         return 1
554 }
555
556 cleanup_active() {
557         # restore activation state
558         do_facet mgs $LCTL nodemap_activate 0
559         wait_nm_sync active
560 }
561
562 test_idmap() {
563         local i
564         local cmd="$LCTL nodemap_test_id"
565         local rc=0
566
567         echo "Start to test idmaps ..."
568         ## nodemap deactivated
569         if ! do_facet mgs $LCTL nodemap_activate 0; then
570                 return 1
571         fi
572         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
573                 local j
574
575                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
576                         local nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
577                         local fs_id=$(do_facet mgs $cmd --nid $nid      \
578                                       --idtype uid --id $id)
579                         if [ $fs_id != $id ]; then
580                                 echo "expected $id, got $fs_id"
581                                 rc=$((rc + 1))
582                         fi
583                 done
584         done
585
586         ## nodemap activated
587         if ! do_facet mgs $LCTL nodemap_activate 1; then
588                 return 2
589         fi
590
591         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
592                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
593                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
594                         fs_id=$(do_facet mgs $cmd --nid $nid    \
595                                 --idtype uid --id $id)
596                         expected_id=$((id + 1))
597                         if [ $fs_id != $expected_id ]; then
598                                 echo "expected $expected_id, got $fs_id"
599                                 rc=$((rc + 1))
600                         fi
601                 done
602         done
603
604         ## trust client ids
605         for ((i = 0; i < NODEMAP_COUNT; i++)); do
606                 local csum=${HOSTNAME_CHECKSUM}_${i}
607
608                 if ! do_facet mgs $LCTL nodemap_modify --name $csum \
609                      --property trusted --value 1; then
610                         error "nodemap_modify $csum failed with $?"
611                         return 3
612                 fi
613         done
614
615         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
616                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
617                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
618                         fs_id=$(do_facet mgs $cmd --nid $nid    \
619                                 --idtype uid --id $id)
620                         if [ $fs_id != $id ]; then
621                                 echo "expected $id, got $fs_id"
622                                 rc=$((rc + 1))
623                         fi
624                 done
625         done
626
627         ## ensure allow_root_access is enabled
628         for ((i = 0; i < NODEMAP_COUNT; i++)); do
629                 local csum=${HOSTNAME_CHECKSUM}_${i}
630
631                 if ! do_facet mgs $LCTL nodemap_modify --name $csum     \
632                      --property admin --value 1; then
633                         error "nodemap_modify $csum failed with $?"
634                         return 3
635                 fi
636         done
637
638         ## check that root allowed
639         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
640                 nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
641                 fs_id=$(do_facet mgs $cmd --nid $nid --idtype uid --id 0)
642                 if [ $fs_id != 0 ]; then
643                         echo "root allowed expected 0, got $fs_id"
644                         rc=$((rc + 1))
645                 fi
646         done
647
648         ## ensure allow_root_access is disabled
649         for ((i = 0; i < NODEMAP_COUNT; i++)); do
650                 local csum=${HOSTNAME_CHECKSUM}_${i}
651
652                 if ! do_facet mgs $LCTL nodemap_modify --name $csum     \
653                                 --property admin --value 0; then
654                         error "nodemap_modify ${HOSTNAME_CHECKSUM}_${i} "
655                                 "failed with $rc"
656                         return 3
657                 fi
658         done
659
660         ## check that root is mapped to 99
661         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
662                 nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
663                 fs_id=$(do_facet mgs $cmd --nid $nid --idtype uid --id 0)
664                 if [ $fs_id != 99 ]; then
665                         error "root squash expected 99, got $fs_id"
666                         rc=$((rc + 1))
667                 fi
668         done
669
670         ## reset client trust to 0
671         for ((i = 0; i < NODEMAP_COUNT; i++)); do
672                 if ! do_facet mgs $LCTL nodemap_modify          \
673                         --name ${HOSTNAME_CHECKSUM}_${i}        \
674                         --property trusted --value 0; then
675                         error "nodemap_modify ${HOSTNAME_CHECKSUM}_${i} "
676                                 "failed with $rc"
677                         return 3
678                 fi
679         done
680
681         return $rc
682 }
683
684 test_7() {
685         local rc
686
687         remote_mgs_nodsh && skip "remote MGS with nodsh"
688         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
689                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
690
691         create_nodemaps
692         rc=$?
693         [[ $rc != 0 ]] && error "nodemap_add failed with $rc"
694
695         delete_nodemaps
696         rc=$?
697         [[ $rc != 0 ]] && error "nodemap_del failed with $rc"
698
699         return 0
700 }
701 run_test 7 "nodemap create and delete"
702
703 test_8() {
704         local rc
705
706         remote_mgs_nodsh && skip "remote MGS with nodsh"
707         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
708                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
709
710         # Set up nodemaps
711
712         create_nodemaps
713         rc=$?
714         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
715
716         # Try duplicates
717
718         create_nodemaps
719         rc=$?
720         [[ $rc == 0 ]] && error "duplicate nodemap_add allowed with $rc" &&
721         return 2
722
723         # Clean up
724         delete_nodemaps
725         rc=$?
726         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 3
727
728         return 0
729 }
730 run_test 8 "nodemap reject duplicates"
731
732 test_9() {
733         local i
734         local rc
735
736         remote_mgs_nodsh && skip "remote MGS with nodsh"
737         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
738                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
739
740         rc=0
741         create_nodemaps
742         rc=$?
743         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
744
745         rc=0
746         for ((i = 0; i < NODEMAP_COUNT; i++)); do
747                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
748                         rc=$((rc + 1))
749                 fi
750         done
751         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
752
753         rc=0
754         for ((i = 0; i < NODEMAP_COUNT; i++)); do
755                 if ! delete_range ${HOSTNAME_CHECKSUM}_${i} $i; then
756                         rc=$((rc + 1))
757                 fi
758         done
759         [[ $rc != 0 ]] && error "nodemap_del_range failed with $rc" && return 4
760
761         rc=0
762         delete_nodemaps
763         rc=$?
764         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
765
766         return 0
767 }
768 run_test 9 "nodemap range add"
769
770 test_10a() {
771         local rc
772
773         remote_mgs_nodsh && skip "remote MGS with nodsh"
774         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
775                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
776
777         rc=0
778         create_nodemaps
779         rc=$?
780         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
781
782         rc=0
783         for ((i = 0; i < NODEMAP_COUNT; i++)); do
784                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
785                         rc=$((rc + 1))
786                 fi
787         done
788         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
789
790         rc=0
791         for ((i = 0; i < NODEMAP_COUNT; i++)); do
792                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
793                         rc=$((rc + 1))
794                 fi
795         done
796         [[ $rc == 0 ]] && error "nodemap_add_range duplicate add with $rc" &&
797                 return 2
798
799
800         rc=0
801         for ((i = 0; i < NODEMAP_COUNT; i++)); do
802                 if ! delete_range ${HOSTNAME_CHECKSUM}_${i} $i; then
803                         rc=$((rc + 1))
804                 fi
805         done
806         [[ $rc != 0 ]] && error "nodemap_del_range failed with $rc" && return 4
807
808         delete_nodemaps
809         rc=$?
810         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 5
811
812         return 0
813 }
814 run_test 10a "nodemap reject duplicate ranges"
815
816 test_10b() {
817         [ "$MGS_VERSION" -lt $(version_code 2.10.53) ] &&
818                 skip "Need MGS >= 2.10.53"
819
820         local nm1="nodemap1"
821         local nm2="nodemap2"
822         local nids="192.168.19.[0-255]@o2ib20"
823
824         do_facet mgs $LCTL nodemap_del $nm1 2>/dev/null
825         do_facet mgs $LCTL nodemap_del $nm2 2>/dev/null
826
827         do_facet mgs $LCTL nodemap_add $nm1 || error "Add $nm1 failed"
828         do_facet mgs $LCTL nodemap_add $nm2 || error "Add $nm2 failed"
829         do_facet mgs $LCTL nodemap_add_range --name $nm1 --range $nids ||
830                 error "Add range $nids to $nm1 failed"
831         [ -n "$(do_facet mgs $LCTL get_param nodemap.$nm1.* |
832                 grep start_nid)" ] || error "No range was found"
833         do_facet mgs $LCTL nodemap_del_range --name $nm2 --range $nids &&
834                 error "Deleting range $nids from $nm2 should fail"
835         [ -n "$(do_facet mgs $LCTL get_param nodemap.$nm1.* |
836                 grep start_nid)" ] || error "Range $nids should be there"
837
838         do_facet mgs $LCTL nodemap_del $nm1 || error "Delete $nm1 failed"
839         do_facet mgs $LCTL nodemap_del $nm2 || error "Delete $nm2 failed"
840         return 0
841 }
842 run_test 10b "delete range from the correct nodemap"
843
844 test_10c() { #LU-8912
845         [ "$MGS_VERSION" -lt $(version_code 2.10.57) ] &&
846                 skip "Need MGS >= 2.10.57"
847
848         local nm="nodemap_lu8912"
849         local nid_range="10.210.[32-47].[0-255]@o2ib3"
850         local start_nid="10.210.32.0@o2ib3"
851         local end_nid="10.210.47.255@o2ib3"
852         local start_nid_found
853         local end_nid_found
854
855         do_facet mgs $LCTL nodemap_del $nm 2>/dev/null
856         do_facet mgs $LCTL nodemap_add $nm || error "Add $nm failed"
857         do_facet mgs $LCTL nodemap_add_range --name $nm --range $nid_range ||
858                 error "Add range $nid_range to $nm failed"
859
860         start_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
861                 awk -F '[,: ]' /start_nid/'{ print $9 }')
862         [ "$start_nid" == "$start_nid_found" ] ||
863                 error "start_nid: $start_nid_found != $start_nid"
864         end_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
865                 awk -F '[,: ]' /end_nid/'{ print $13 }')
866         [ "$end_nid" == "$end_nid_found" ] ||
867                 error "end_nid: $end_nid_found != $end_nid"
868
869         do_facet mgs $LCTL nodemap_del $nm || error "Delete $nm failed"
870         return 0
871 }
872 run_test 10c "verfify contiguous range support"
873
874 test_10d() { #LU-8913
875         [ "$MGS_VERSION" -lt $(version_code 2.10.59) ] &&
876                 skip "Need MGS >= 2.10.59"
877
878         local nm="nodemap_lu8913"
879         local nid_range="*@o2ib3"
880         local start_nid="0.0.0.0@o2ib3"
881         local end_nid="255.255.255.255@o2ib3"
882         local start_nid_found
883         local end_nid_found
884
885         do_facet mgs $LCTL nodemap_del $nm 2>/dev/null
886         do_facet mgs $LCTL nodemap_add $nm || error "Add $nm failed"
887         do_facet mgs $LCTL nodemap_add_range --name $nm --range $nid_range ||
888                 error "Add range $nid_range to $nm failed"
889
890         start_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
891                 awk -F '[,: ]' /start_nid/'{ print $9 }')
892         [ "$start_nid" == "$start_nid_found" ] ||
893                 error "start_nid: $start_nid_found != $start_nid"
894         end_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
895                 awk -F '[,: ]' /end_nid/'{ print $13 }')
896         [ "$end_nid" == "$end_nid_found" ] ||
897                 error "end_nid: $end_nid_found != $end_nid"
898
899         do_facet mgs $LCTL nodemap_del $nm || error "Delete $nm failed"
900         return 0
901 }
902 run_test 10d "verfify nodemap range format '*@<net>' support"
903
904 test_11() {
905         local rc
906
907         remote_mgs_nodsh && skip "remote MGS with nodsh"
908         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
909                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
910
911         rc=0
912         create_nodemaps
913         rc=$?
914         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
915
916         rc=0
917         for ((i = 0; i < NODEMAP_COUNT; i++)); do
918                 if ! modify_flags ${HOSTNAME_CHECKSUM}_${i}; then
919                         rc=$((rc + 1))
920                 fi
921         done
922         [[ $rc != 0 ]] && error "nodemap_modify with $rc" && return 2
923
924         rc=0
925         delete_nodemaps
926         rc=$?
927         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 3
928
929         return 0
930 }
931 run_test 11 "nodemap modify"
932
933 test_12() {
934         local rc
935
936         remote_mgs_nodsh && skip "remote MGS with nodsh"
937         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
938                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
939
940         rc=0
941         create_nodemaps
942         rc=$?
943         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
944
945         rc=0
946         for ((i = 0; i < NODEMAP_COUNT; i++)); do
947                 if ! squash_id ${HOSTNAME_CHECKSUM}_${i} 88 0; then
948                         rc=$((rc + 1))
949                 fi
950         done
951         [[ $rc != 0 ]] && error "nodemap squash_uid with $rc" && return 2
952
953         rc=0
954         for ((i = 0; i < NODEMAP_COUNT; i++)); do
955                 if ! squash_id ${HOSTNAME_CHECKSUM}_${i} 88 1; then
956                         rc=$((rc + 1))
957                 fi
958         done
959         [[ $rc != 0 ]] && error "nodemap squash_gid with $rc" && return 3
960
961         rc=0
962         delete_nodemaps
963         rc=$?
964         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
965
966         return 0
967 }
968 run_test 12 "nodemap set squash ids"
969
970 test_13() {
971         local rc
972
973         remote_mgs_nodsh && skip "remote MGS with nodsh"
974         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
975                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
976
977         rc=0
978         create_nodemaps
979         rc=$?
980         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
981
982         rc=0
983         for ((i = 0; i < NODEMAP_COUNT; i++)); do
984                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
985                         rc=$((rc + 1))
986                 fi
987         done
988         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
989
990         rc=0
991         for ((i = 0; i < NODEMAP_COUNT; i++)); do
992                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
993                         for k in $NODEMAP_IPADDR_LIST; do
994                                 if ! test_nid $SUBNET_CHECKSUM.$i.$j.$k \
995                                        ${HOSTNAME_CHECKSUM}_${i}; then
996                                         rc=$((rc + 1))
997                                 fi
998                         done
999                 done
1000         done
1001         [[ $rc != 0 ]] && error "nodemap_test_nid failed with $rc" && return 3
1002
1003         rc=0
1004         delete_nodemaps
1005         rc=$?
1006         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
1007
1008         return 0
1009 }
1010 run_test 13 "test nids"
1011
1012 test_14() {
1013         local rc
1014
1015         remote_mgs_nodsh && skip "remote MGS with nodsh"
1016         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1017                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1018
1019         rc=0
1020         create_nodemaps
1021         rc=$?
1022         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1023
1024         rc=0
1025         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1026                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
1027                         for k in $NODEMAP_IPADDR_LIST; do
1028                                 if ! test_nid $SUBNET_CHECKSUM.$i.$j.$k \
1029                                         default; then
1030                                         rc=$((rc + 1))
1031                                 fi
1032                         done
1033                 done
1034         done
1035         [[ $rc != 0 ]] && error "nodemap_test_nid failed with $rc" && return 3
1036
1037         rc=0
1038         delete_nodemaps
1039         rc=$?
1040         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
1041
1042         return 0
1043 }
1044 run_test 14 "test default nodemap nid lookup"
1045
1046 test_15() {
1047         local rc
1048
1049         remote_mgs_nodsh && skip "remote MGS with nodsh"
1050         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1051                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1052
1053         rc=0
1054         create_nodemaps
1055         rc=$?
1056         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1057
1058         rc=0
1059         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1060                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
1061                         rc=$((rc + 1))
1062                 fi
1063         done
1064         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
1065
1066         rc=0
1067         add_idmaps
1068         rc=$?
1069         [[ $rc != 0 ]] && error "nodemap_add_idmap failed with $rc" && return 3
1070
1071         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
1072         if [[ "$activedefault" != "1" ]]; then
1073                 stack_trap cleanup_active EXIT
1074         fi
1075
1076         rc=0
1077         test_idmap
1078         rc=$?
1079         [[ $rc != 0 ]] && error "nodemap_test_id failed with $rc" && return 4
1080
1081         rc=0
1082         update_idmaps
1083         rc=$?
1084         [[ $rc != 0 ]] && error "update_idmaps failed with $rc" && return 5
1085
1086         rc=0
1087         delete_idmaps
1088         rc=$?
1089         [[ $rc != 0 ]] && error "nodemap_del_idmap failed with $rc" && return 6
1090
1091         rc=0
1092         delete_nodemaps
1093         rc=$?
1094         [[ $rc != 0 ]] && error "nodemap_delete failed with $rc" && return 7
1095
1096         return 0
1097 }
1098 run_test 15 "test id mapping"
1099
1100 create_fops_nodemaps() {
1101         local i=0
1102         local client
1103         for client in $clients; do
1104                 local client_ip=$(host_nids_address $client $NETTYPE)
1105                 local client_nid=$(h2nettype $client_ip)
1106                 do_facet mgs $LCTL nodemap_add c${i} || return 1
1107                 do_facet mgs $LCTL nodemap_add_range    \
1108                         --name c${i} --range $client_nid || return 1
1109                 for map in ${FOPS_IDMAPS[i]}; do
1110                         do_facet mgs $LCTL nodemap_add_idmap --name c${i} \
1111                                 --idtype uid --idmap ${map} || return 1
1112                         do_facet mgs $LCTL nodemap_add_idmap --name c${i} \
1113                                 --idtype gid --idmap ${map} || return 1
1114                 done
1115
1116                 wait_nm_sync c$i idmap
1117
1118                 i=$((i + 1))
1119         done
1120         return 0
1121 }
1122
1123 delete_fops_nodemaps() {
1124         local i=0
1125         local client
1126         for client in $clients; do
1127                 do_facet mgs $LCTL nodemap_del c${i} || return 1
1128                 i=$((i + 1))
1129         done
1130         return 0
1131 }
1132
1133 fops_mds_index=0
1134 nm_test_mkdir() {
1135         if [ $MDSCOUNT -le 1 ]; then
1136                 do_node ${clients_arr[0]} mkdir -p $DIR/$tdir
1137         else
1138                 # round-robin MDTs to test DNE nodemap support
1139                 [ ! -d $DIR ] && do_node ${clients_arr[0]} mkdir -p $DIR
1140                 do_node ${clients_arr[0]} $LFS setdirstripe -c 1 -i \
1141                         $((fops_mds_index % MDSCOUNT)) $DIR/$tdir
1142                 ((fops_mds_index++))
1143         fi
1144 }
1145
1146 # acl test directory needs to be initialized on a privileged client
1147 fops_test_setup() {
1148         local admin=$(do_facet mgs $LCTL get_param -n nodemap.c0.admin_nodemap)
1149         local trust=$(do_facet mgs $LCTL get_param -n \
1150                 nodemap.c0.trusted_nodemap)
1151
1152         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1153         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1154
1155         wait_nm_sync c0 admin_nodemap
1156         wait_nm_sync c0 trusted_nodemap
1157
1158         do_node ${clients_arr[0]} rm -rf $DIR/$tdir
1159         nm_test_mkdir
1160         do_node ${clients_arr[0]} chown $user $DIR/$tdir
1161
1162         do_facet mgs $LCTL nodemap_modify --name c0 \
1163                 --property admin --value $admin
1164         do_facet mgs $LCTL nodemap_modify --name c0 \
1165                 --property trusted --value $trust
1166
1167         # flush MDT locks to make sure they are reacquired before test
1168         do_node ${clients_arr[0]} $LCTL set_param \
1169                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1170
1171         wait_nm_sync c0 admin_nodemap
1172         wait_nm_sync c0 trusted_nodemap
1173 }
1174
1175 # fileset test directory needs to be initialized on a privileged client
1176 fileset_test_setup() {
1177         local nm=$1
1178
1179         if [ -n "$FILESET" -a -z "$SKIP_FILESET" ]; then
1180                 cleanup_mount $MOUNT
1181                 FILESET="" zconf_mount_clients $CLIENTS $MOUNT
1182         fi
1183
1184         local admin=$(do_facet mgs $LCTL get_param -n \
1185                 nodemap.${nm}.admin_nodemap)
1186         local trust=$(do_facet mgs $LCTL get_param -n \
1187                 nodemap.${nm}.trusted_nodemap)
1188
1189         do_facet mgs $LCTL nodemap_modify --name $nm --property admin --value 1
1190         do_facet mgs $LCTL nodemap_modify --name $nm --property trusted \
1191                 --value 1
1192
1193         wait_nm_sync $nm admin_nodemap
1194         wait_nm_sync $nm trusted_nodemap
1195
1196         # create directory and populate it for subdir mount
1197         do_node ${clients_arr[0]} mkdir $MOUNT/$subdir ||
1198                 error "unable to create dir $MOUNT/$subdir"
1199         do_node ${clients_arr[0]} touch $MOUNT/$subdir/this_is_$subdir ||
1200                 error "unable to create file $MOUNT/$subdir/this_is_$subdir"
1201         do_node ${clients_arr[0]} mkdir $MOUNT/$subdir/$subsubdir ||
1202                 error "unable to create dir $MOUNT/$subdir/$subsubdir"
1203         do_node ${clients_arr[0]} touch \
1204                         $MOUNT/$subdir/$subsubdir/this_is_$subsubdir ||
1205                 error "unable to create file \
1206                         $MOUNT/$subdir/$subsubdir/this_is_$subsubdir"
1207
1208         do_facet mgs $LCTL nodemap_modify --name $nm \
1209                 --property admin --value $admin
1210         do_facet mgs $LCTL nodemap_modify --name $nm \
1211                 --property trusted --value $trust
1212
1213         # flush MDT locks to make sure they are reacquired before test
1214         do_node ${clients_arr[0]} $LCTL set_param \
1215                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1216
1217         wait_nm_sync $nm admin_nodemap
1218         wait_nm_sync $nm trusted_nodemap
1219 }
1220
1221 # fileset test directory needs to be initialized on a privileged client
1222 fileset_test_cleanup() {
1223         local nm=$1
1224         local admin=$(do_facet mgs $LCTL get_param -n \
1225                 nodemap.${nm}.admin_nodemap)
1226         local trust=$(do_facet mgs $LCTL get_param -n \
1227                 nodemap.${nm}.trusted_nodemap)
1228
1229         do_facet mgs $LCTL nodemap_modify --name $nm --property admin --value 1
1230         do_facet mgs $LCTL nodemap_modify --name $nm --property trusted \
1231                 --value 1
1232
1233         wait_nm_sync $nm admin_nodemap
1234         wait_nm_sync $nm trusted_nodemap
1235
1236         # cleanup directory created for subdir mount
1237         do_node ${clients_arr[0]} rm -rf $MOUNT/$subdir ||
1238                 error "unable to remove dir $MOUNT/$subdir"
1239
1240         do_facet mgs $LCTL nodemap_modify --name $nm \
1241                 --property admin --value $admin
1242         do_facet mgs $LCTL nodemap_modify --name $nm \
1243                 --property trusted --value $trust
1244
1245         # flush MDT locks to make sure they are reacquired before test
1246         do_node ${clients_arr[0]} $LCTL set_param \
1247                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1248
1249         wait_nm_sync $nm admin_nodemap
1250         wait_nm_sync $nm trusted_nodemap
1251         if [ -n "$FILESET" -a -z "$SKIP_FILESET" ]; then
1252                 cleanup_mount $MOUNT
1253                 zconf_mount_clients $CLIENTS $MOUNT
1254         fi
1255 }
1256
1257 do_create_delete() {
1258         local run_u=$1
1259         local key=$2
1260         local testfile=$DIR/$tdir/$tfile
1261         local rc=0
1262         local c=0 d=0
1263         local qused_new
1264         if $run_u touch $testfile >& /dev/null; then
1265                 c=1
1266                 $run_u rm $testfile && d=1
1267         fi >& /dev/null
1268
1269         local res="$c $d"
1270         local expected=$(get_cr_del_expected $key)
1271         [ "$res" != "$expected" ] &&
1272                 error "test $key, wanted $expected, got $res" && rc=$((rc + 1))
1273         return $rc
1274 }
1275
1276 nodemap_check_quota() {
1277         local run_u="$1"
1278         $run_u lfs quota -q $DIR | awk '{ print $2; exit; }'
1279 }
1280
1281 do_fops_quota_test() {
1282         local run_u=$1
1283         # fuzz quota used to account for possible indirect blocks, etc
1284         local quota_fuzz=$(fs_log_size)
1285         local qused_orig=$(nodemap_check_quota "$run_u")
1286         local qused_high=$((qused_orig + quota_fuzz))
1287         local qused_low=$((qused_orig - quota_fuzz))
1288         local testfile=$DIR/$tdir/$tfile
1289         $run_u dd if=/dev/zero of=$testfile oflag=sync bs=1M count=1 \
1290                 >& /dev/null || error "unable to write quota test file"
1291         sync; sync_all_data || true
1292
1293         local qused_new=$(nodemap_check_quota "$run_u")
1294         [ $((qused_new)) -lt $((qused_low + 1024)) -o \
1295           $((qused_new)) -gt $((qused_high + 1024)) ] &&
1296                 error "$qused_new != $qused_orig + 1M after write, " \
1297                       "fuzz is $quota_fuzz"
1298         $run_u rm $testfile || error "unable to remove quota test file"
1299         wait_delete_completed_mds
1300
1301         qused_new=$(nodemap_check_quota "$run_u")
1302         [ $((qused_new)) -lt $((qused_low)) \
1303                 -o $((qused_new)) -gt $((qused_high)) ] &&
1304                 error "quota not reclaimed, expect $qused_orig, " \
1305                       "got $qused_new, fuzz $quota_fuzz"
1306 }
1307
1308 get_fops_mapped_user() {
1309         local cli_user=$1
1310
1311         for ((i=0; i < ${#FOPS_IDMAPS[@]}; i++)); do
1312                 for map in ${FOPS_IDMAPS[i]}; do
1313                         if [ $(cut -d: -f1 <<< "$map") == $cli_user ]; then
1314                                 cut -d: -f2 <<< "$map"
1315                                 return
1316                         fi
1317                 done
1318         done
1319         echo -1
1320 }
1321
1322 get_cr_del_expected() {
1323         local -a key
1324         IFS=":" read -a key <<< "$1"
1325         local mapmode="${key[0]}"
1326         local mds_user="${key[1]}"
1327         local cluster="${key[2]}"
1328         local cli_user="${key[3]}"
1329         local mode="0${key[4]}"
1330         local SUCCESS="1 1"
1331         local FAILURE="0 0"
1332         local noadmin=0
1333         local mapped=0
1334         local other=0
1335
1336         [[ $mapmode == *mapped* ]] && mapped=1
1337         # only c1 is mapped in these test cases
1338         [[ $mapmode == mapped_trusted* ]] && [ "$cluster" == "c0" ] && mapped=0
1339         [[ $mapmode == *noadmin* ]] && noadmin=1
1340
1341         # o+wx works as long as the user isn't mapped
1342         if [ $((mode & 3)) -eq 3 ]; then
1343                 other=1
1344         fi
1345
1346         # if client user is root, check if root is squashed
1347         if [ "$cli_user" == "0" ]; then
1348                 # squash root succeed, if other bit is on
1349                 case $noadmin in
1350                         0) echo $SUCCESS;;
1351                         1) [ "$other" == "1" ] && echo $SUCCESS
1352                            [ "$other" == "0" ] && echo $FAILURE;;
1353                 esac
1354                 return
1355         fi
1356         if [ "$mapped" == "0" ]; then
1357                 [ "$other" == "1" ] && echo $SUCCESS
1358                 [ "$other" == "0" ] && echo $FAILURE
1359                 return
1360         fi
1361
1362         # if mapped user is mds user, check for u+wx
1363         mapped_user=$(get_fops_mapped_user $cli_user)
1364         [ "$mapped_user" == "-1" ] &&
1365                 error "unable to find mapping for client user $cli_user"
1366
1367         if [ "$mapped_user" == "$mds_user" -a \
1368              $(((mode & 0300) == 0300)) -eq 1 ]; then
1369                 echo $SUCCESS
1370                 return
1371         fi
1372         if [ "$mapped_user" != "$mds_user" -a "$other" == "1" ]; then
1373                 echo $SUCCESS
1374                 return
1375         fi
1376         echo $FAILURE
1377 }
1378
1379 test_fops_admin_cli_i=""
1380 test_fops_chmod_dir() {
1381         local current_cli_i=$1
1382         local perm_bits=$2
1383         local dir_to_chmod=$3
1384         local new_admin_cli_i=""
1385
1386         # do we need to set up a new admin client?
1387         [ "$current_cli_i" == "0" ] && [ "$test_fops_admin_cli_i" != "1" ] &&
1388                 new_admin_cli_i=1
1389         [ "$current_cli_i" != "0" ] && [ "$test_fops_admin_cli_i" != "0" ] &&
1390                 new_admin_cli_i=0
1391
1392         # if only one client, and non-admin, need to flip admin everytime
1393         if [ "$num_clients" == "1" ]; then
1394                 test_fops_admin_client=$clients
1395                 test_fops_admin_val=$(do_facet mgs $LCTL get_param -n \
1396                         nodemap.c0.admin_nodemap)
1397                 if [ "$test_fops_admin_val" != "1" ]; then
1398                         do_facet mgs $LCTL nodemap_modify \
1399                                 --name c0 \
1400                                 --property admin \
1401                                 --value 1
1402                         wait_nm_sync c0 admin_nodemap
1403                 fi
1404         elif [ "$new_admin_cli_i" != "" ]; then
1405                 # restore admin val to old admin client
1406                 if [ "$test_fops_admin_cli_i" != "" ] &&
1407                                 [ "$test_fops_admin_val" != "1" ]; then
1408                         do_facet mgs $LCTL nodemap_modify \
1409                                 --name c${test_fops_admin_cli_i} \
1410                                 --property admin \
1411                                 --value $test_fops_admin_val
1412                         wait_nm_sync c${test_fops_admin_cli_i} admin_nodemap
1413                 fi
1414
1415                 test_fops_admin_cli_i=$new_admin_cli_i
1416                 test_fops_admin_client=${clients_arr[$new_admin_cli_i]}
1417                 test_fops_admin_val=$(do_facet mgs $LCTL get_param -n \
1418                         nodemap.c${new_admin_cli_i}.admin_nodemap)
1419
1420                 if [ "$test_fops_admin_val" != "1" ]; then
1421                         do_facet mgs $LCTL nodemap_modify \
1422                                 --name c${new_admin_cli_i} \
1423                                 --property admin \
1424                                 --value 1
1425                         wait_nm_sync c${new_admin_cli_i} admin_nodemap
1426                 fi
1427         fi
1428
1429         do_node $test_fops_admin_client chmod $perm_bits $DIR/$tdir || return 1
1430
1431         # remove admin for single client if originally non-admin
1432         if [ "$num_clients" == "1" ] && [ "$test_fops_admin_val" != "1" ]; then
1433                 do_facet mgs $LCTL nodemap_modify --name c0 --property admin \
1434                         --value 0
1435                 wait_nm_sync c0 admin_nodemap
1436         fi
1437
1438         return 0
1439 }
1440
1441 test_fops() {
1442         local mapmode="$1"
1443         local single_client="$2"
1444         local client_user_list=([0]="0 $((IDBASE+3)) $((IDBASE+4))"
1445                                 [1]="0 $((IDBASE+5)) $((IDBASE+6))")
1446         local mds_i
1447         local rc=0
1448         local perm_bit_list="0 3 $((0300)) $((0303))"
1449         # SLOW tests 000-007, 010-070, 100-700 (octal modes)
1450         [ "$SLOW" == "yes" ] &&
1451                 perm_bit_list="0 $(seq 1 7) $(seq 8 8 63) $(seq 64 64 511) \
1452                                $((0303))"
1453
1454         # step through mds users. -1 means root
1455         for mds_i in -1 0 1 2; do
1456                 local user=$((mds_i + IDBASE))
1457                 local client
1458                 local x
1459
1460                 [ "$mds_i" == "-1" ] && user=0
1461
1462                 echo mkdir -p $DIR/$tdir
1463                 fops_test_setup
1464                 local cli_i=0
1465                 for client in $clients; do
1466                         local u
1467                         for u in ${client_user_list[$cli_i]}; do
1468                                 local run_u="do_node $client \
1469                                              $RUNAS_CMD -u$u -g$u -G$u"
1470                                 for perm_bits in $perm_bit_list; do
1471                                         local mode=$(printf %03o $perm_bits)
1472                                         local key
1473                                         key="$mapmode:$user:c$cli_i:$u:$mode"
1474                                         test_fops_chmod_dir $cli_i $mode \
1475                                                 $DIR/$tdir ||
1476                                                         error cannot chmod $key
1477                                         do_create_delete "$run_u" "$key"
1478                                 done
1479
1480                                 # check quota
1481                                 test_fops_chmod_dir $cli_i 777 $DIR/$tdir ||
1482                                         error cannot chmod $key
1483                                 do_fops_quota_test "$run_u"
1484                         done
1485
1486                         cli_i=$((cli_i + 1))
1487                         [ "$single_client" == "1" ] && break
1488                 done
1489                 rm -rf $DIR/$tdir
1490         done
1491         return $rc
1492 }
1493
1494 nodemap_version_check () {
1495         remote_mgs_nodsh && skip "remote MGS with nodsh" && return 1
1496         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1497                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53" &&
1498                 return 1
1499         return 0
1500 }
1501
1502 nodemap_test_setup() {
1503         local rc
1504         local active_nodemap=1
1505
1506         [ "$1" == "0" ] && active_nodemap=0
1507
1508         do_nodes $(comma_list $(all_mdts_nodes)) \
1509                 $LCTL set_param mdt.*.identity_upcall=NONE
1510
1511         rc=0
1512         create_fops_nodemaps
1513         rc=$?
1514         [[ $rc != 0 ]] && error "adding fops nodemaps failed $rc"
1515
1516         do_facet mgs $LCTL nodemap_activate $active_nodemap
1517         wait_nm_sync active
1518
1519         do_facet mgs $LCTL nodemap_modify --name default \
1520                 --property admin --value 1
1521         wait_nm_sync default admin_nodemap
1522         do_facet mgs $LCTL nodemap_modify --name default \
1523                 --property trusted --value 1
1524         wait_nm_sync default trusted_nodemap
1525 }
1526
1527 nodemap_test_cleanup() {
1528         trap 0
1529         delete_fops_nodemaps
1530         rc=$?
1531         [[ $rc != 0 ]] && error "removing fops nodemaps failed $rc"
1532
1533         do_facet mgs $LCTL nodemap_modify --name default \
1534                  --property admin --value 0
1535         wait_nm_sync default admin_nodemap
1536         do_facet mgs $LCTL nodemap_modify --name default \
1537                  --property trusted --value 0
1538         wait_nm_sync default trusted_nodemap
1539
1540         do_facet mgs $LCTL nodemap_activate 0
1541         wait_nm_sync active 0
1542
1543         export SK_UNIQUE_NM=false
1544         return 0
1545 }
1546
1547 nodemap_clients_admin_trusted() {
1548         local admin=$1
1549         local tr=$2
1550         local i=0
1551         for client in $clients; do
1552                 do_facet mgs $LCTL nodemap_modify --name c0 \
1553                         --property admin --value $admin
1554                 do_facet mgs $LCTL nodemap_modify --name c0 \
1555                         --property trusted --value $tr
1556                 i=$((i + 1))
1557         done
1558         wait_nm_sync c$((i - 1)) admin_nodemap
1559         wait_nm_sync c$((i - 1)) trusted_nodemap
1560 }
1561
1562 test_16() {
1563         nodemap_version_check || return 0
1564         nodemap_test_setup 0
1565
1566         trap nodemap_test_cleanup EXIT
1567         test_fops all_off
1568         nodemap_test_cleanup
1569 }
1570 run_test 16 "test nodemap all_off fileops"
1571
1572 test_17() {
1573         if $SHARED_KEY &&
1574         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1575                 skip "Need MDS >= 2.11.55"
1576         fi
1577
1578         nodemap_version_check || return 0
1579         nodemap_test_setup
1580
1581         trap nodemap_test_cleanup EXIT
1582         nodemap_clients_admin_trusted 0 1
1583         test_fops trusted_noadmin 1
1584         nodemap_test_cleanup
1585 }
1586 run_test 17 "test nodemap trusted_noadmin fileops"
1587
1588 test_18() {
1589         if $SHARED_KEY &&
1590         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1591                 skip "Need MDS >= 2.11.55"
1592         fi
1593
1594         nodemap_version_check || return 0
1595         nodemap_test_setup
1596
1597         trap nodemap_test_cleanup EXIT
1598         nodemap_clients_admin_trusted 0 0
1599         test_fops mapped_noadmin 1
1600         nodemap_test_cleanup
1601 }
1602 run_test 18 "test nodemap mapped_noadmin fileops"
1603
1604 test_19() {
1605         if $SHARED_KEY &&
1606         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1607                 skip "Need MDS >= 2.11.55"
1608         fi
1609
1610         nodemap_version_check || return 0
1611         nodemap_test_setup
1612
1613         trap nodemap_test_cleanup EXIT
1614         nodemap_clients_admin_trusted 1 1
1615         test_fops trusted_admin 1
1616         nodemap_test_cleanup
1617 }
1618 run_test 19 "test nodemap trusted_admin fileops"
1619
1620 test_20() {
1621         if $SHARED_KEY &&
1622         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1623                 skip "Need MDS >= 2.11.55"
1624         fi
1625
1626         nodemap_version_check || return 0
1627         nodemap_test_setup
1628
1629         trap nodemap_test_cleanup EXIT
1630         nodemap_clients_admin_trusted 1 0
1631         test_fops mapped_admin 1
1632         nodemap_test_cleanup
1633 }
1634 run_test 20 "test nodemap mapped_admin fileops"
1635
1636 test_21() {
1637         if $SHARED_KEY &&
1638         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1639                 skip "Need MDS >= 2.11.55"
1640         fi
1641
1642         nodemap_version_check || return 0
1643         nodemap_test_setup
1644
1645         trap nodemap_test_cleanup EXIT
1646         local x=1
1647         local i=0
1648         for client in $clients; do
1649                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1650                         --property admin --value 0
1651                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1652                         --property trusted --value $x
1653                 x=0
1654                 i=$((i + 1))
1655         done
1656         wait_nm_sync c$((i - 1)) trusted_nodemap
1657
1658         test_fops mapped_trusted_noadmin
1659         nodemap_test_cleanup
1660 }
1661 run_test 21 "test nodemap mapped_trusted_noadmin fileops"
1662
1663 test_22() {
1664         if $SHARED_KEY &&
1665         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1666                 skip "Need MDS >= 2.11.55"
1667         fi
1668
1669         nodemap_version_check || return 0
1670         nodemap_test_setup
1671
1672         trap nodemap_test_cleanup EXIT
1673         local x=1
1674         local i=0
1675         for client in $clients; do
1676                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1677                         --property admin --value 1
1678                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1679                         --property trusted --value $x
1680                 x=0
1681                 i=$((i + 1))
1682         done
1683         wait_nm_sync c$((i - 1)) trusted_nodemap
1684
1685         test_fops mapped_trusted_admin
1686         nodemap_test_cleanup
1687 }
1688 run_test 22 "test nodemap mapped_trusted_admin fileops"
1689
1690 # acl test directory needs to be initialized on a privileged client
1691 nodemap_acl_test_setup() {
1692         local admin=$(do_facet mgs $LCTL get_param -n \
1693                       nodemap.c0.admin_nodemap)
1694         local trust=$(do_facet mgs $LCTL get_param -n \
1695                       nodemap.c0.trusted_nodemap)
1696
1697         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1698         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1699
1700         wait_nm_sync c0 admin_nodemap
1701         wait_nm_sync c0 trusted_nodemap
1702
1703         do_node ${clients_arr[0]} rm -rf $DIR/$tdir
1704         nm_test_mkdir
1705         do_node ${clients_arr[0]} chmod a+rwx $DIR/$tdir ||
1706                 error unable to chmod a+rwx test dir $DIR/$tdir
1707
1708         do_facet mgs $LCTL nodemap_modify --name c0 \
1709                 --property admin --value $admin
1710         do_facet mgs $LCTL nodemap_modify --name c0 \
1711                 --property trusted --value $trust
1712
1713         wait_nm_sync c0 trusted_nodemap
1714 }
1715
1716 # returns 0 if the number of ACLs does not change on the second (mapped) client
1717 # after being set on the first client
1718 nodemap_acl_test() {
1719         local user="$1"
1720         local set_client="$2"
1721         local get_client="$3"
1722         local check_setfacl="$4"
1723         local setfacl_error=0
1724         local testfile=$DIR/$tdir/$tfile
1725         local RUNAS_USER="$RUNAS_CMD -u $user"
1726         local acl_count=0
1727         local acl_count_post=0
1728
1729         nodemap_acl_test_setup
1730         sleep 5
1731
1732         do_node $set_client $RUNAS_USER touch $testfile
1733
1734         # ACL masks aren't filtered by nodemap code, so we ignore them
1735         acl_count=$(do_node $get_client getfacl $testfile | grep -v mask |
1736                 wc -l)
1737         do_node $set_client $RUNAS_USER setfacl -m $user:rwx $testfile ||
1738                 setfacl_error=1
1739
1740         # if check setfacl is set to 1, then it's supposed to error
1741         if [ "$check_setfacl" == "1" ]; then
1742                 [ "$setfacl_error" != "1" ] && return 1
1743                 return 0
1744         fi
1745         [ "$setfacl_error" == "1" ] && echo "WARNING: unable to setfacl"
1746
1747         acl_count_post=$(do_node $get_client getfacl $testfile | grep -v mask |
1748                 wc -l)
1749         [ $acl_count -eq $acl_count_post ] && return 0
1750         return 1
1751 }
1752
1753 test_23a() {
1754         [ $num_clients -lt 2 ] && skip "Need 2 clients at least" && return
1755         nodemap_version_check || return 0
1756         nodemap_test_setup
1757
1758         trap nodemap_test_cleanup EXIT
1759         # 1 trusted cluster, 1 mapped cluster
1760         local unmapped_fs=$((IDBASE+0))
1761         local unmapped_c1=$((IDBASE+5))
1762         local mapped_fs=$((IDBASE+2))
1763         local mapped_c0=$((IDBASE+4))
1764         local mapped_c1=$((IDBASE+6))
1765
1766         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1767         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1768
1769         do_facet mgs $LCTL nodemap_modify --name c1 --property admin --value 0
1770         do_facet mgs $LCTL nodemap_modify --name c1 --property trusted --value 0
1771
1772         wait_nm_sync c1 trusted_nodemap
1773
1774         # setfacl on trusted cluster to unmapped user, verify it's not seen
1775         nodemap_acl_test $unmapped_fs ${clients_arr[0]} ${clients_arr[1]} ||
1776                 error "acl count (1)"
1777
1778         # setfacl on trusted cluster to mapped user, verify it's seen
1779         nodemap_acl_test $mapped_fs ${clients_arr[0]} ${clients_arr[1]} &&
1780                 error "acl count (2)"
1781
1782         # setfacl on mapped cluster to mapped user, verify it's seen
1783         nodemap_acl_test $mapped_c1 ${clients_arr[1]} ${clients_arr[0]} &&
1784                 error "acl count (3)"
1785
1786         # setfacl on mapped cluster to unmapped user, verify error
1787         nodemap_acl_test $unmapped_fs ${clients_arr[1]} ${clients_arr[0]} 1 ||
1788                 error "acl count (4)"
1789
1790         # 2 mapped clusters
1791         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 0
1792         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 0
1793
1794         wait_nm_sync c0 trusted_nodemap
1795
1796         # setfacl to mapped user on c1, also mapped to c0, verify it's seen
1797         nodemap_acl_test $mapped_c1 ${clients_arr[1]} ${clients_arr[0]} &&
1798                 error "acl count (5)"
1799
1800         # setfacl to mapped user on c1, not mapped to c0, verify not seen
1801         nodemap_acl_test $unmapped_c1 ${clients_arr[1]} ${clients_arr[0]} ||
1802                 error "acl count (6)"
1803
1804         nodemap_test_cleanup
1805 }
1806 run_test 23a "test mapped regular ACLs"
1807
1808 test_23b() { #LU-9929
1809         [ $num_clients -lt 2 ] && skip "Need 2 clients at least"
1810         [ "$MGS_VERSION" -lt $(version_code 2.10.53) ] &&
1811                 skip "Need MGS >= 2.10.53"
1812
1813         export SK_UNIQUE_NM=true
1814         nodemap_test_setup
1815         trap nodemap_test_cleanup EXIT
1816
1817         local testdir=$DIR/$tdir
1818         local fs_id=$((IDBASE+10))
1819         local unmapped_id
1820         local mapped_id
1821         local fs_user
1822
1823         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1824         wait_nm_sync c0 admin_nodemap
1825         do_facet mgs $LCTL nodemap_modify --name c1 --property admin --value 1
1826         wait_nm_sync c1 admin_nodemap
1827         do_facet mgs $LCTL nodemap_modify --name c1 --property trusted --value 1
1828         wait_nm_sync c1 trusted_nodemap
1829
1830         # Add idmap $ID0:$fs_id (500:60010)
1831         do_facet mgs $LCTL nodemap_add_idmap --name c0 --idtype gid \
1832                 --idmap $ID0:$fs_id ||
1833                 error "add idmap $ID0:$fs_id to nodemap c0 failed"
1834         wait_nm_sync c0 idmap
1835
1836         # set/getfacl default acl on client 1 (unmapped gid=500)
1837         do_node ${clients_arr[0]} rm -rf $testdir
1838         do_node ${clients_arr[0]} mkdir -p $testdir
1839         # Here, USER0=$(getent passwd | grep :$ID0:$ID0: | cut -d: -f1)
1840         do_node ${clients_arr[0]} setfacl -R -d -m group:$USER0:rwx $testdir ||
1841                 error "setfacl $testdir on ${clients_arr[0]} failed"
1842         unmapped_id=$(do_node ${clients_arr[0]} getfacl $testdir |
1843                         grep -E "default:group:.*:rwx" | awk -F: '{print $3}')
1844         [ "$unmapped_id" = "$USER0" ] ||
1845                 error "gid=$ID0 was not unmapped correctly on ${clients_arr[0]}"
1846
1847         # getfacl default acl on client 2 (mapped gid=60010)
1848         mapped_id=$(do_node ${clients_arr[1]} getfacl $testdir |
1849                         grep -E "default:group:.*:rwx" | awk -F: '{print $3}')
1850         fs_user=$(do_node ${clients_arr[1]} getent passwd |
1851                         grep :$fs_id:$fs_id: | cut -d: -f1)
1852         [ -z "$fs_user" ] && fs_user=$fs_id
1853         [ $mapped_id -eq $fs_id -o "$mapped_id" = "$fs_user" ] ||
1854                 error "Should return gid=$fs_id or $fs_user on client2"
1855
1856         rm -rf $testdir
1857         nodemap_test_cleanup
1858         export SK_UNIQUE_NM=false
1859 }
1860 run_test 23b "test mapped default ACLs"
1861
1862 test_24() {
1863         nodemap_test_setup
1864
1865         trap nodemap_test_cleanup EXIT
1866         do_nodes $(comma_list $(all_server_nodes)) $LCTL get_param -R nodemap
1867
1868         nodemap_test_cleanup
1869 }
1870 run_test 24 "check nodemap proc files for LBUGs and Oopses"
1871
1872 test_25() {
1873         local tmpfile=$(mktemp)
1874         local tmpfile2=$(mktemp)
1875         local tmpfile3=$(mktemp)
1876         local tmpfile4=$(mktemp)
1877         local subdir=c0dir
1878         local client
1879
1880         nodemap_version_check || return 0
1881
1882         # stop clients for this test
1883         zconf_umount_clients $CLIENTS $MOUNT ||
1884             error "unable to umount clients $CLIENTS"
1885
1886         export SK_UNIQUE_NM=true
1887         nodemap_test_setup
1888
1889         # enable trusted/admin for setquota call in cleanup_and_setup_lustre()
1890         i=0
1891         for client in $clients; do
1892                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1893                         --property admin --value 1
1894                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1895                         --property trusted --value 1
1896                 ((i++))
1897         done
1898         wait_nm_sync c$((i - 1)) trusted_nodemap
1899
1900         trap nodemap_test_cleanup EXIT
1901
1902         # create a new, empty nodemap, and add fileset info to it
1903         do_facet mgs $LCTL nodemap_add test25 ||
1904                 error "unable to create nodemap $testname"
1905         do_facet mgs $LCTL set_param -P nodemap.$testname.fileset=/$subdir ||
1906                 error "unable to add fileset info to nodemap test25"
1907
1908         wait_nm_sync test25 id
1909
1910         do_facet mgs $LCTL nodemap_info > $tmpfile
1911         do_facet mds $LCTL nodemap_info > $tmpfile2
1912
1913         if ! $SHARED_KEY; then
1914                 # will conflict with SK's nodemaps
1915                 cleanup_and_setup_lustre
1916         fi
1917         # stop clients for this test
1918         zconf_umount_clients $CLIENTS $MOUNT ||
1919             error "unable to umount clients $CLIENTS"
1920
1921         do_facet mgs $LCTL nodemap_info > $tmpfile3
1922         diff -q $tmpfile3 $tmpfile >& /dev/null ||
1923                 error "nodemap_info diff on MGS after remount"
1924
1925         do_facet mds $LCTL nodemap_info > $tmpfile4
1926         diff -q $tmpfile4 $tmpfile2 >& /dev/null ||
1927                 error "nodemap_info diff on MDS after remount"
1928
1929         # cleanup nodemap
1930         do_facet mgs $LCTL nodemap_del test25 ||
1931             error "cannot delete nodemap test25 from config"
1932         nodemap_test_cleanup
1933         # restart clients previously stopped
1934         zconf_mount_clients $CLIENTS $MOUNT ||
1935             error "unable to mount clients $CLIENTS"
1936
1937         rm -f $tmpfile $tmpfile2
1938         export SK_UNIQUE_NM=false
1939 }
1940 run_test 25 "test save and reload nodemap config"
1941
1942 test_26() {
1943         nodemap_version_check || return 0
1944
1945         local large_i=32000
1946
1947         do_facet mgs "seq -f 'c%g' $large_i | xargs -n1 $LCTL nodemap_add"
1948         wait_nm_sync c$large_i admin_nodemap
1949
1950         do_facet mgs "seq -f 'c%g' $large_i | xargs -n1 $LCTL nodemap_del"
1951         wait_nm_sync c$large_i admin_nodemap
1952 }
1953 run_test 26 "test transferring very large nodemap"
1954
1955 nodemap_exercise_fileset() {
1956         local nm="$1"
1957         local loop=0
1958
1959         # setup
1960         if [ "$nm" == "default" ]; then
1961                 do_facet mgs $LCTL nodemap_activate 1
1962                 wait_nm_sync active
1963         else
1964                 nodemap_test_setup
1965         fi
1966         if $SHARED_KEY; then
1967                 export SK_UNIQUE_NM=true
1968         else
1969                 # will conflict with SK's nodemaps
1970                 trap "fileset_test_cleanup $nm" EXIT
1971         fi
1972         fileset_test_setup "$nm"
1973
1974         # add fileset info to $nm nodemap
1975         if ! combined_mgs_mds; then
1976             do_facet mgs $LCTL set_param nodemap.${nm}.fileset=/$subdir ||
1977                 error "unable to add fileset info to $nm nodemap on MGS"
1978         fi
1979         do_facet mgs $LCTL set_param -P nodemap.${nm}.fileset=/$subdir ||
1980                error "unable to add fileset info to $nm nodemap for servers"
1981         wait_nm_sync $nm fileset "nodemap.${nm}.fileset=/$subdir"
1982
1983         # re-mount client
1984         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
1985                 error "unable to umount client ${clients_arr[0]}"
1986         # set some generic fileset to trigger SSK code
1987         export FILESET=/
1988         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
1989                 error "unable to remount client ${clients_arr[0]}"
1990         unset FILESET
1991
1992         # test mount point content
1993         do_node ${clients_arr[0]} test -f $MOUNT/this_is_$subdir ||
1994                 error "fileset not taken into account"
1995
1996         # re-mount client with sub-subdir
1997         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
1998                 error "unable to umount client ${clients_arr[0]}"
1999         export FILESET=/$subsubdir
2000         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2001                 error "unable to remount client ${clients_arr[0]}"
2002         unset FILESET
2003
2004         # test mount point content
2005         do_node ${clients_arr[0]} test -f $MOUNT/this_is_$subsubdir ||
2006                 error "subdir of fileset not taken into account"
2007
2008         # remove fileset info from nodemap
2009         do_facet mgs $LCTL nodemap_set_fileset --name $nm --fileset clear ||
2010                 error "unable to delete fileset info on $nm nodemap"
2011         wait_update_facet mgs "$LCTL get_param nodemap.${nm}.fileset" \
2012                           "nodemap.${nm}.fileset=" ||
2013                 error "fileset info still not cleared on $nm nodemap"
2014         do_facet mgs $LCTL set_param -P nodemap.${nm}.fileset=clear ||
2015                 error "unable to reset fileset info on $nm nodemap"
2016         wait_nm_sync $nm fileset "nodemap.${nm}.fileset="
2017
2018         # re-mount client
2019         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2020                 error "unable to umount client ${clients_arr[0]}"
2021         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2022                 error "unable to remount client ${clients_arr[0]}"
2023
2024         # test mount point content
2025         if ! $(do_node ${clients_arr[0]} test -d $MOUNT/$subdir); then
2026                 ls $MOUNT
2027                 error "fileset not cleared on $nm nodemap"
2028         fi
2029
2030         # back to non-nodemap setup
2031         if $SHARED_KEY; then
2032                 export SK_UNIQUE_NM=false
2033                 zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2034                         error "unable to umount client ${clients_arr[0]}"
2035         fi
2036         fileset_test_cleanup "$nm"
2037         if [ "$nm" == "default" ]; then
2038                 do_facet mgs $LCTL nodemap_activate 0
2039                 wait_nm_sync active 0
2040                 trap 0
2041                 export SK_UNIQUE_NM=false
2042         else
2043                 nodemap_test_cleanup
2044         fi
2045         if $SHARED_KEY; then
2046                 zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2047                         error "unable to remount client ${clients_arr[0]}"
2048         fi
2049 }
2050
2051 test_27a() {
2052         [ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
2053                 skip "Need MDS >= 2.11.50"
2054
2055         for nm in "default" "c0"; do
2056                 local subdir="subdir_${nm}"
2057                 local subsubdir="subsubdir_${nm}"
2058
2059                 if [ "$nm" == "default" ] && [ "$SHARED_KEY" == "true" ]; then
2060                         echo "Skipping nodemap $nm with SHARED_KEY";
2061                         continue;
2062                 fi
2063
2064                 echo "Exercising fileset for nodemap $nm"
2065                 nodemap_exercise_fileset "$nm"
2066         done
2067 }
2068 run_test 27a "test fileset in various nodemaps"
2069
2070 test_27b() { #LU-10703
2071         [ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
2072                 skip "Need MDS >= 2.11.50"
2073         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs"
2074
2075         nodemap_test_setup
2076         trap nodemap_test_cleanup EXIT
2077
2078         # Add the nodemaps and set their filesets
2079         for i in $(seq 1 $MDSCOUNT); do
2080                 do_facet mgs $LCTL nodemap_del nm$i 2>/dev/null
2081                 do_facet mgs $LCTL nodemap_add nm$i ||
2082                         error "add nodemap nm$i failed"
2083                 wait_nm_sync nm$i "" "" "-N"
2084
2085                 if ! combined_mgs_mds; then
2086                         do_facet mgs \
2087                                 $LCTL set_param nodemap.nm$i.fileset=/dir$i ||
2088                                 error "set nm$i.fileset=/dir$i failed on MGS"
2089                 fi
2090                 do_facet mgs $LCTL set_param -P nodemap.nm$i.fileset=/dir$i ||
2091                         error "set nm$i.fileset=/dir$i failed on servers"
2092                 wait_nm_sync nm$i fileset "nodemap.nm$i.fileset=/dir$i"
2093         done
2094
2095         # Check if all the filesets are correct
2096         for i in $(seq 1 $MDSCOUNT); do
2097                 fileset=$(do_facet mds$i \
2098                           $LCTL get_param -n nodemap.nm$i.fileset)
2099                 [ "$fileset" = "/dir$i" ] ||
2100                         error "nm$i.fileset $fileset != /dir$i on mds$i"
2101                 do_facet mgs $LCTL nodemap_del nm$i ||
2102                         error "delete nodemap nm$i failed"
2103         done
2104
2105         nodemap_test_cleanup
2106 }
2107 run_test 27b "The new nodemap won't clear the old nodemap's fileset"
2108
2109 test_28() {
2110         if ! $SHARED_KEY; then
2111                 skip "need shared key feature for this test" && return
2112         fi
2113         mkdir -p $DIR/$tdir || error "mkdir failed"
2114         touch $DIR/$tdir/$tdir.out || error "touch failed"
2115         if [ ! -f $DIR/$tdir/$tdir.out ]; then
2116                 error "read before rotation failed"
2117         fi
2118         # store top key identity to ensure rotation has occurred
2119         SK_IDENTITY_OLD=$(lctl get_param *.*.*srpc* | grep "expire" |
2120                 head -1 | awk '{print $15}' | cut -c1-8)
2121         do_facet $SINGLEMDS lfs flushctx ||
2122                  error "could not run flushctx on $SINGLEMDS"
2123         sleep 5
2124         lfs flushctx || error "could not run flushctx on client"
2125         sleep 5
2126         # verify new key is in place
2127         SK_IDENTITY_NEW=$(lctl get_param *.*.*srpc* | grep "expire" |
2128                 head -1 | awk '{print $15}' | cut -c1-8)
2129         if [ $SK_IDENTITY_OLD == $SK_IDENTITY_NEW ]; then
2130                 error "key did not rotate correctly"
2131         fi
2132         if [ ! -f $DIR/$tdir/$tdir.out ]; then
2133                 error "read after rotation failed"
2134         fi
2135 }
2136 run_test 28 "check shared key rotation method"
2137
2138 test_29() {
2139         if ! $SHARED_KEY; then
2140                 skip "need shared key feature for this test" && return
2141         fi
2142         if [ $SK_FLAVOR != "ski" ] && [ $SK_FLAVOR != "skpi" ]; then
2143                 skip "test only valid if integrity is active"
2144         fi
2145         rm -r $DIR/$tdir
2146         mkdir $DIR/$tdir || error "mkdir"
2147         touch $DIR/$tdir/$tfile || error "touch"
2148         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2149                 error "unable to umount clients"
2150         do_node ${clients_arr[0]} "keyctl show |
2151                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2152         OLD_SK_PATH=$SK_PATH
2153         export SK_PATH=/dev/null
2154         if zconf_mount_clients ${clients_arr[0]} $MOUNT; then
2155                 export SK_PATH=$OLD_SK_PATH
2156                 do_node ${clients_arr[0]} "ls $DIR/$tdir/$tfile"
2157                 if [ $? -eq 0 ]; then
2158                         error "able to mount and read without key"
2159                 else
2160                         error "able to mount without key"
2161                 fi
2162         else
2163                 export SK_PATH=$OLD_SK_PATH
2164                 do_node ${clients_arr[0]} "keyctl show |
2165                         awk '/lustre/ { print \\\$1 }' |
2166                         xargs -IX keyctl unlink X"
2167         fi
2168         zconf_mount_clients ${clients_arr[0]} $MOUNT ||
2169                 error "unable to mount clients"
2170 }
2171 run_test 29 "check for missing shared key"
2172
2173 test_30() {
2174         if ! $SHARED_KEY; then
2175                 skip "need shared key feature for this test" && return
2176         fi
2177         if [ $SK_FLAVOR != "ski" ] && [ $SK_FLAVOR != "skpi" ]; then
2178                 skip "test only valid if integrity is active"
2179         fi
2180         mkdir -p $DIR/$tdir || error "mkdir failed"
2181         touch $DIR/$tdir/$tdir.out || error "touch failed"
2182         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2183                 error "unable to umount clients"
2184         # unload keys from ring
2185         do_node ${clients_arr[0]} "keyctl show |
2186                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2187         # generate key with bogus filesystem name
2188         do_node ${clients_arr[0]} "lgss_sk -w $SK_PATH/$FSNAME-bogus.key \
2189                 -f $FSNAME.bogus -t client -d /dev/urandom" ||
2190                 error "lgss_sk failed (1)"
2191         do_facet $SINGLEMDS lfs flushctx || error "could not run flushctx"
2192         OLD_SK_PATH=$SK_PATH
2193         export SK_PATH=$SK_PATH/$FSNAME-bogus.key
2194         if zconf_mount_clients ${clients_arr[0]} $MOUNT; then
2195                 SK_PATH=$OLD_SK_PATH
2196                 do_node ${clients_arr[0]} "ls $DIR/$tdir/$tdir.out"
2197                 if [ $? -eq 0 ]; then
2198                         error "mount and read file with invalid key"
2199                 else
2200                         error "mount with invalid key"
2201                 fi
2202         fi
2203         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2204                 error "unable to umount clients"
2205         # unload keys from ring
2206         do_node ${clients_arr[0]} "keyctl show |
2207                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2208         rm -f $SK_PATH
2209         SK_PATH=$OLD_SK_PATH
2210         zconf_mount_clients ${clients_arr[0]} $MOUNT ||
2211                 error "unable to mount clients"
2212 }
2213 run_test 30 "check for invalid shared key"
2214
2215 basic_ios() {
2216         local flvr=$1
2217
2218         mkdir -p $DIR/$tdir || error "mkdir $flvr"
2219         touch $DIR/$tdir/f0 || error "touch $flvr"
2220         ls $DIR/$tdir || error "ls $flvr"
2221         dd if=/dev/zero of=$DIR/$tdir/f0 conv=fsync bs=1M count=10 \
2222                 >& /dev/null || error "dd $flvr"
2223         rm -f $DIR/$tdir/f0 || error "rm $flvr"
2224         rmdir $DIR/$tdir || error "rmdir $flvr"
2225
2226         sync ; sync
2227         echo 3 > /proc/sys/vm/drop_caches
2228 }
2229
2230 test_30b() {
2231         local save_flvr=$SK_FLAVOR
2232
2233         if ! $SHARED_KEY; then
2234                 skip "need shared key feature for this test"
2235         fi
2236
2237         stack_trap restore_to_default_flavor EXIT
2238
2239         for flvr in skn ska ski skpi; do
2240                 # set flavor
2241                 SK_FLAVOR=$flvr
2242                 restore_to_default_flavor || error "cannot set $flvr flavor"
2243                 SK_FLAVOR=$save_flvr
2244
2245                 basic_ios $flvr
2246         done
2247 }
2248 run_test 30b "basic test of all different SSK flavors"
2249
2250 cleanup_31() {
2251         # unmount client
2252         zconf_umount $HOSTNAME $MOUNT || error "unable to umount client"
2253
2254         # remove ${NETTYPE}999 network on all nodes
2255         do_nodes $(comma_list $(all_nodes)) \
2256                  "$LNETCTL net del --net ${NETTYPE}999 && \
2257                   $LNETCTL lnet unconfigure 2>/dev/null || true"
2258
2259         # necessary to do writeconf in order to de-register
2260         # @${NETTYPE}999 nid for targets
2261         KZPOOL=$KEEP_ZPOOL
2262         export KEEP_ZPOOL="true"
2263         stopall
2264         export SK_MOUNTED=false
2265         writeconf_all
2266         setupall || echo 1
2267         export KEEP_ZPOOL="$KZPOOL"
2268 }
2269
2270 test_31() {
2271         local nid=$(lctl list_nids | grep ${NETTYPE} | head -n1)
2272         local addr=${nid%@*}
2273         local net=${nid#*@}
2274
2275         export LNETCTL=$(which lnetctl 2> /dev/null)
2276
2277         [ -z "$LNETCTL" ] && skip "without lnetctl support." && return
2278         local_mode && skip "in local mode."
2279
2280         stack_trap cleanup_31 EXIT
2281
2282         # umount client
2283         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
2284                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
2285         fi
2286         if $(grep -q $MOUNT' ' /proc/mounts); then
2287                 umount_client $MOUNT || error "umount $MOUNT failed"
2288         fi
2289
2290         # check exports on servers are empty for client
2291         do_facet mgs "lctl get_param -n *.MGS*.exports.'$nid'.uuid 2>/dev/null |
2292                       grep -q -" && error "export on MGS should be empty"
2293         do_nodes $(comma_list $(mdts_nodes) $(osts_nodes)) \
2294                  "lctl get_param -n *.${FSNAME}*.exports.'$nid'.uuid \
2295                   2>/dev/null | grep -q -" &&
2296                 error "export on servers should be empty"
2297
2298         # add network ${NETTYPE}999 on all nodes
2299         do_nodes $(comma_list $(all_nodes)) \
2300                  "$LNETCTL lnet configure && $LNETCTL net add --if \
2301                   \$($LNETCTL net show --net $net | awk 'BEGIN{inf=0} \
2302                   {if (inf==1) print \$2; fi; inf=0} /interfaces/{inf=1}') \
2303                   --net ${NETTYPE}999" ||
2304                 error "unable to configure NID ${NETTYPE}999"
2305
2306         # necessary to do writeconf in order to register
2307         # new @${NETTYPE}999 nid for targets
2308         KZPOOL=$KEEP_ZPOOL
2309         export KEEP_ZPOOL="true"
2310         stopall
2311         export SK_MOUNTED=false
2312         writeconf_all
2313         setupall server_only || echo 1
2314         export KEEP_ZPOOL="$KZPOOL"
2315
2316         # backup MGSNID
2317         local mgsnid_orig=$MGSNID
2318         # compute new MGSNID
2319         MGSNID=$(do_facet mgs "$LCTL list_nids | grep ${NETTYPE}999")
2320
2321         # on client, turn LNet Dynamic Discovery on
2322         lnetctl set discovery 1
2323
2324         # mount client with -o network=${NETTYPE}999 option:
2325         # should fail because of LNet Dynamic Discovery
2326         mount_client $MOUNT ${MOUNT_OPTS},network=${NETTYPE}999 &&
2327                 error "client mount with '-o network' option should be refused"
2328
2329         # on client, reconfigure LNet and turn LNet Dynamic Discovery off
2330         $LNETCTL net del --net ${NETTYPE}999 && lnetctl lnet unconfigure
2331         lustre_rmmod
2332         modprobe lnet
2333         lnetctl set discovery 0
2334         modprobe ptlrpc
2335         $LNETCTL lnet configure && $LNETCTL net add --if \
2336           $($LNETCTL net show --net $net | awk 'BEGIN{inf=0} \
2337           {if (inf==1) print $2; fi; inf=0} /interfaces/{inf=1}') \
2338           --net ${NETTYPE}999 ||
2339         error "unable to configure NID ${NETTYPE}999 on client"
2340
2341         # mount client with -o network=${NETTYPE}999 option
2342         mount_client $MOUNT ${MOUNT_OPTS},network=${NETTYPE}999 ||
2343                 error "unable to remount client"
2344
2345         # restore MGSNID
2346         MGSNID=$mgsnid_orig
2347
2348         # check export on MGS
2349         do_facet mgs "lctl get_param -n *.MGS*.exports.'$nid'.uuid 2>/dev/null |
2350                       grep -q -"
2351         [ $? -ne 0 ] || error "export for $nid on MGS should not exist"
2352
2353         do_facet mgs \
2354                 "lctl get_param -n *.MGS*.exports.'${addr}@${NETTYPE}999'.uuid \
2355                  2>/dev/null | grep -q -"
2356         [ $? -eq 0 ] ||
2357                 error "export for ${addr}@${NETTYPE}999 on MGS should exist"
2358
2359         # check {mdc,osc} imports
2360         lctl get_param mdc.${FSNAME}-*.import | grep current_connection |
2361             grep -q ${NETTYPE}999
2362         [ $? -eq 0 ] ||
2363                 error "import for mdc should use ${addr}@${NETTYPE}999"
2364         lctl get_param osc.${FSNAME}-*.import | grep current_connection |
2365             grep -q ${NETTYPE}999
2366         [ $? -eq 0 ] ||
2367                 error "import for osc should use ${addr}@${NETTYPE}999"
2368 }
2369 run_test 31 "client mount option '-o network'"
2370
2371 cleanup_32() {
2372         # umount client
2373         zconf_umount_clients ${clients_arr[0]} $MOUNT
2374
2375         # disable sk flavor enforcement on MGS
2376         set_rule _mgs any any null
2377
2378         # stop gss daemon on MGS
2379         if ! combined_mgs_mds ; then
2380                 send_sigint $mgs_HOST lsvcgssd
2381         fi
2382
2383         # re-mount client
2384         MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
2385         mountcli
2386
2387         restore_to_default_flavor
2388 }
2389
2390 test_32() {
2391         if ! $SHARED_KEY; then
2392                 skip "need shared key feature for this test"
2393         fi
2394
2395         stack_trap cleanup_32 EXIT
2396
2397         # restore to default null flavor
2398         save_flvr=$SK_FLAVOR
2399         SK_FLAVOR=null
2400         restore_to_default_flavor || error "cannot set null flavor"
2401         SK_FLAVOR=$save_flvr
2402
2403         # umount client
2404         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
2405                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
2406         fi
2407         if $(grep -q $MOUNT' ' /proc/mounts); then
2408         umount_client $MOUNT || error "umount $MOUNT failed"
2409         fi
2410
2411         # start gss daemon on MGS
2412         if combined_mgs_mds ; then
2413                 send_sigint $mds_HOST lsvcgssd
2414         fi
2415         start_gss_daemons $mgs_HOST "$LSVCGSSD -vvv -s -g"
2416
2417         # add mgs key type and MGS NIDs in key on MGS
2418         do_nodes $mgs_HOST "lgss_sk -t mgs,server -g $MGSNID -m \
2419                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2420                 error "could not modify keyfile on MGS"
2421
2422         # load modified key file on MGS
2423         do_nodes $mgs_HOST "lgss_sk -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2424                 error "could not load keyfile on MGS"
2425
2426         # add MGS NIDs in key on client
2427         do_nodes ${clients_arr[0]} "lgss_sk -g $MGSNID -m \
2428                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2429                 error "could not modify keyfile on MGS"
2430
2431         # set perms for per-nodemap keys else permission denied
2432         do_nodes $(comma_list $(all_nodes)) \
2433                  "keyctl show | grep lustre | cut -c1-11 |
2434                                 sed -e 's/ //g;' |
2435                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
2436
2437         # re-mount client with mgssec=skn
2438         save_opts=$MOUNT_OPTS
2439         if [ -z "$MOUNT_OPTS" ]; then
2440                 MOUNT_OPTS="-o mgssec=skn"
2441         else
2442                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
2443         fi
2444         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2445                 error "mount ${clients_arr[0]} with mgssec=skn failed"
2446         MOUNT_OPTS=$save_opts
2447
2448         # umount client
2449         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2450                 error "umount ${clients_arr[0]} failed"
2451
2452         # enforce ska flavor on MGS
2453         set_rule _mgs any any ska
2454
2455         # re-mount client without mgssec
2456         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS &&
2457                 error "mount ${clients_arr[0]} without mgssec should fail"
2458
2459         # re-mount client with mgssec=skn
2460         save_opts=$MOUNT_OPTS
2461         if [ -z "$MOUNT_OPTS" ]; then
2462                 MOUNT_OPTS="-o mgssec=skn"
2463         else
2464                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
2465         fi
2466         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS &&
2467                 error "mount ${clients_arr[0]} with mgssec=skn should fail"
2468         MOUNT_OPTS=$save_opts
2469
2470         # re-mount client with mgssec=ska
2471         save_opts=$MOUNT_OPTS
2472         if [ -z "$MOUNT_OPTS" ]; then
2473                 MOUNT_OPTS="-o mgssec=ska"
2474         else
2475                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=ska"
2476         fi
2477         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2478                 error "mount ${clients_arr[0]} with mgssec=ska failed"
2479         MOUNT_OPTS=$save_opts
2480
2481         exit 0
2482 }
2483 run_test 32 "check for mgssec"
2484
2485 cleanup_33() {
2486         # disable sk flavor enforcement
2487         set_rule $FSNAME any cli2mdt null
2488         wait_flavor cli2mdt null
2489
2490         # umount client
2491         zconf_umount_clients ${clients_arr[0]} $MOUNT
2492
2493         # stop gss daemon on MGS
2494         if ! combined_mgs_mds ; then
2495                 send_sigint $mgs_HOST lsvcgssd
2496         fi
2497
2498         # re-mount client
2499         MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
2500         mountcli
2501
2502         restore_to_default_flavor
2503 }
2504
2505 test_33() {
2506         if ! $SHARED_KEY; then
2507                 skip "need shared key feature for this test"
2508         fi
2509
2510         stack_trap cleanup_33 EXIT
2511
2512         # restore to default null flavor
2513         save_flvr=$SK_FLAVOR
2514         SK_FLAVOR=null
2515         restore_to_default_flavor || error "cannot set null flavor"
2516         SK_FLAVOR=$save_flvr
2517
2518         # umount client
2519         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
2520                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
2521         fi
2522         if $(grep -q $MOUNT' ' /proc/mounts); then
2523         umount_client $MOUNT || error "umount $MOUNT failed"
2524         fi
2525
2526         # start gss daemon on MGS
2527         if combined_mgs_mds ; then
2528                 send_sigint $mds_HOST lsvcgssd
2529         fi
2530         start_gss_daemons $mgs_HOST "$LSVCGSSD -vvv -s -g"
2531
2532         # add mgs key type and MGS NIDs in key on MGS
2533         do_nodes $mgs_HOST "lgss_sk -t mgs,server -g $MGSNID -m \
2534                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2535                 error "could not modify keyfile on MGS"
2536
2537         # load modified key file on MGS
2538         do_nodes $mgs_HOST "lgss_sk -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2539                 error "could not load keyfile on MGS"
2540
2541         # add MGS NIDs in key on client
2542         do_nodes ${clients_arr[0]} "lgss_sk -g $MGSNID -m \
2543                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
2544                 error "could not modify keyfile on MGS"
2545
2546         # set perms for per-nodemap keys else permission denied
2547         do_nodes $(comma_list $(all_nodes)) \
2548                  "keyctl show | grep lustre | cut -c1-11 |
2549                                 sed -e 's/ //g;' |
2550                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
2551
2552         # re-mount client with mgssec=skn
2553         save_opts=$MOUNT_OPTS
2554         if [ -z "$MOUNT_OPTS" ]; then
2555                 MOUNT_OPTS="-o mgssec=skn"
2556         else
2557                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
2558         fi
2559         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2560                 error "mount ${clients_arr[0]} with mgssec=skn failed"
2561         MOUNT_OPTS=$save_opts
2562
2563         # enforce ska flavor for cli2mdt
2564         set_rule $FSNAME any cli2mdt ska
2565         wait_flavor cli2mdt ska
2566
2567         # check error message
2568         $LCTL dk | grep "faked source" &&
2569                 error "MGS connection srpc flags incorrect"
2570
2571         exit 0
2572 }
2573 run_test 33 "correct srpc flags for MGS connection"
2574
2575 cleanup_34_deny() {
2576         # restore deny_unknown
2577         do_facet mgs $LCTL nodemap_modify --name default \
2578                            --property deny_unknown --value $denydefault
2579         if [ $? -ne 0 ]; then
2580                 error_noexit "cannot reset deny_unknown on default nodemap"
2581                 return
2582         fi
2583
2584         wait_nm_sync default deny_unknown
2585 }
2586
2587 test_34() {
2588         local denynew
2589         local activedefault
2590
2591         [ $MGS_VERSION -lt $(version_code 2.12.51) ] &&
2592                 skip "deny_unknown on default nm not supported before 2.12.51"
2593
2594         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
2595
2596         if [[ "$activedefault" != "1" ]]; then
2597                 do_facet mgs $LCTL nodemap_activate 1
2598                 wait_nm_sync active
2599                 stack_trap cleanup_active EXIT
2600         fi
2601
2602         denydefault=$(do_facet mgs $LCTL get_param -n \
2603                       nodemap.default.deny_unknown)
2604         [ -z "$denydefault" ] &&
2605                 error "cannot get deny_unknown on default nodemap"
2606         if [ "$denydefault" -eq 0 ]; then
2607                 denynew=1;
2608         else
2609                 denynew=0;
2610         fi
2611
2612         do_facet mgs $LCTL nodemap_modify --name default \
2613                         --property deny_unknown --value $denynew ||
2614                 error "cannot set deny_unknown on default nodemap"
2615
2616         [ "$(do_facet mgs $LCTL get_param -n nodemap.default.deny_unknown)" \
2617                         -eq $denynew ] ||
2618                 error "setting deny_unknown on default nodemap did not work"
2619
2620         stack_trap cleanup_34_deny EXIT
2621
2622         wait_nm_sync default deny_unknown
2623 }
2624 run_test 34 "deny_unknown on default nodemap"
2625
2626 test_35() {
2627         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.13.50) ] ||
2628                 skip "Need MDS >= 2.13.50"
2629
2630         # activate changelogs
2631         changelog_register || error "changelog_register failed"
2632         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
2633         changelog_users $SINGLEMDS | grep -q $cl_user ||
2634                 error "User $cl_user not found in changelog_users"
2635         changelog_chmask ALL
2636
2637         # do some IOs
2638         mkdir $DIR/$tdir || error "failed to mkdir $tdir"
2639         touch $DIR/$tdir/$tfile || error "failed to touch $tfile"
2640
2641         # access changelogs with root
2642         changelog_dump || error "failed to dump changelogs"
2643         changelog_clear 0 || error "failed to clear changelogs"
2644
2645         # put clients in non-admin nodemap
2646         nodemap_test_setup
2647         stack_trap nodemap_test_cleanup EXIT
2648         for i in $(seq 0 $((num_clients-1))); do
2649                 do_facet mgs $LCTL nodemap_modify --name c${i} \
2650                          --property admin --value 0
2651         done
2652         for i in $(seq 0 $((num_clients-1))); do
2653                 wait_nm_sync c${i} admin_nodemap
2654         done
2655
2656         # access with mapped root
2657         changelog_dump && error "dump changelogs should have failed"
2658         changelog_clear 0 && error "clear changelogs should have failed"
2659
2660         exit 0
2661 }
2662 run_test 35 "Check permissions when accessing changelogs"
2663
2664 setup_for_enc_tests() {
2665         # remount client with test_dummy_encryption option
2666         if is_mounted $MOUNT; then
2667                 umount_client $MOUNT || error "umount $MOUNT failed"
2668         fi
2669         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption ||
2670                 error "mount with '-o test_dummy_encryption' failed"
2671
2672         # this directory will be encrypted, because of dummy mode
2673         mkdir $DIR/$tdir
2674 }
2675
2676 cleanup_for_enc_tests() {
2677         # remount client normally
2678         if is_mounted $MOUNT; then
2679                 umount_client $MOUNT || error "umount $MOUNT failed"
2680         fi
2681         mount_client $MOUNT ${MOUNT_OPTS} ||
2682                 error "remount failed"
2683
2684         if is_mounted $MOUNT2; then
2685                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
2686         fi
2687         if [ "$MOUNT_2" ]; then
2688                 mount_client $MOUNT2 ${MOUNT_OPTS} ||
2689                         error "remount failed"
2690         fi
2691 }
2692
2693 cleanup_nodemap_after_enc_tests() {
2694         do_facet mgs $LCTL nodemap_modify --name default \
2695                 --property forbid_encryption --value 1
2696         wait_nm_sync default forbid_encryption
2697         do_facet mgs $LCTL nodemap_activate 0
2698         wait_nm_sync active
2699 }
2700
2701 test_36() {
2702         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2703                 skip "client encryption not supported"
2704
2705         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2706                 skip "need dummy encryption support"
2707
2708         stack_trap cleanup_for_enc_tests EXIT
2709
2710         # first make sure it is possible to enable encryption
2711         # when nodemap is not active
2712         setup_for_enc_tests
2713         rmdir $DIR/$tdir
2714         umount_client $MOUNT || error "umount $MOUNT failed (1)"
2715
2716         # then activate nodemap, and retry
2717         # should succeed as encryption is not forbidden on default nodemap
2718         # by default
2719         stack_trap cleanup_nodemap_after_enc_tests EXIT
2720         do_facet mgs $LCTL nodemap_activate 1
2721         wait_nm_sync active
2722         forbid=$(do_facet mgs lctl get_param -n nodemap.default.forbid_encryption)
2723         [ $forbid -eq 0 ] || error "wrong default value for forbid_encryption"
2724         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption ||
2725                 error "mount '-o test_dummy_encryption' failed with default"
2726         umount_client $MOUNT || error "umount $MOUNT failed (2)"
2727
2728         # then forbid encryption, and retry
2729         do_facet mgs $LCTL nodemap_modify --name default \
2730                 --property forbid_encryption --value 1
2731         wait_nm_sync default forbid_encryption
2732         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption &&
2733                 error "mount '-o test_dummy_encryption' should have failed"
2734         return 0
2735 }
2736 run_test 36 "control if clients can use encryption"
2737
2738 test_37() {
2739         local testfile=$DIR/$tdir/$tfile
2740         local tmpfile=$TMP/abc
2741         local objdump=$TMP/objdump
2742         local objid
2743
2744         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2745                 skip "client encryption not supported"
2746
2747         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2748                 skip "need dummy encryption support"
2749
2750         [ "$ost1_FSTYPE" = ldiskfs ] || skip "ldiskfs only test (using debugfs)"
2751
2752         stack_trap cleanup_for_enc_tests EXIT
2753         setup_for_enc_tests
2754
2755         # write a few bytes in file
2756         echo "abc" > $tmpfile
2757         $LFS setstripe -c1 -i0 $testfile
2758         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
2759         do_facet ost1 "sync; sync"
2760
2761         # check that content on ost is encrypted
2762         objid=$($LFS getstripe $testfile | awk '/obdidx/{getline; print $2}')
2763         do_facet ost1 "$DEBUGFS -c -R 'cat O/0/d$(($objid % 32))/$objid' \
2764                  $(ostdevname 1)" > $objdump
2765         cmp -s $objdump $tmpfile &&
2766                 error "file $testfile is not encrypted on ost"
2767
2768         # check that in-memory representation of file is correct
2769         cmp -bl ${tmpfile} ${testfile} ||
2770                 error "file $testfile is corrupted in memory"
2771
2772         cancel_lru_locks osc ; cancel_lru_locks mdc
2773
2774         # check that file read from server is correct
2775         cmp -bl ${tmpfile} ${testfile} ||
2776                 error "file $testfile is corrupted on server"
2777
2778         rm -f $tmpfile $objdump
2779 }
2780 run_test 37 "simple encrypted file"
2781
2782 test_38() {
2783         local testfile=$DIR/$tdir/$tfile
2784         local tmpfile=$TMP/abc
2785         local objid
2786         local blksz
2787         local srvsz=0
2788         local filesz
2789         local bsize
2790         local pagesz=$(getconf PAGE_SIZE)
2791
2792         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2793                 skip "client encryption not supported"
2794
2795         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2796                 skip "need dummy encryption support"
2797
2798         stack_trap cleanup_for_enc_tests EXIT
2799         setup_for_enc_tests
2800
2801         # get block size on ost
2802         blksz=$($LCTL get_param osc.$FSNAME*.import |
2803                 awk '/grant_block_size:/ { print $2; exit; }')
2804         # write a few bytes in file at offset $blksz
2805         echo "abc" > $tmpfile
2806         $LFS setstripe -c1 -i0 $testfile
2807         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$blksz \
2808                 oflag=seek_bytes conv=fsync
2809
2810         blksz=$(($blksz > $pagesz ? $blksz : $pagesz))
2811         # check that in-memory representation of file is correct
2812         bsize=$(stat --format=%B $testfile)
2813         filesz=$(stat --format=%b $testfile)
2814         filesz=$((filesz*bsize))
2815         [ $filesz -le $blksz ] ||
2816                 error "file $testfile is $filesz long in memory"
2817
2818         cancel_lru_locks osc ; cancel_lru_locks mdc
2819
2820         # check that file read from server is correct
2821         bsize=$(stat --format=%B $testfile)
2822         filesz=$(stat --format=%b $testfile)
2823         filesz=$((filesz*bsize))
2824         [ $filesz -le $blksz ] ||
2825                 error "file $testfile is $filesz long on server"
2826
2827         rm -f $tmpfile
2828 }
2829 run_test 38 "encrypted file with hole"
2830
2831 test_39() {
2832         local testfile=$DIR/$tdir/$tfile
2833         local tmpfile=$TMP/abc
2834
2835         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2836                 skip "client encryption not supported"
2837
2838         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2839                 skip "need dummy encryption support"
2840
2841         stack_trap cleanup_for_enc_tests EXIT
2842         setup_for_enc_tests
2843
2844         # write a few bytes in file
2845         echo "abc" > $tmpfile
2846         $LFS setstripe -c1 -i0 $testfile
2847         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
2848
2849         # write a few more bytes in the same page
2850         dd if=$tmpfile of=$testfile bs=4 count=1 seek=1024 oflag=seek_bytes \
2851                 conv=fsync,notrunc
2852
2853         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=1024 oflag=seek_bytes \
2854                 conv=fsync,notrunc
2855
2856         # check that in-memory representation of file is correct
2857         cmp -bl $tmpfile $testfile ||
2858                 error "file $testfile is corrupted in memory"
2859
2860         cancel_lru_locks osc ; cancel_lru_locks mdc
2861
2862         # check that file read from server is correct
2863         cmp -bl $tmpfile $testfile ||
2864                 error "file $testfile is corrupted on server"
2865
2866         rm -f $tmpfile
2867 }
2868 run_test 39 "rewrite data in already encrypted page"
2869
2870 test_40() {
2871         local testfile=$DIR/$tdir/$tfile
2872         local tmpfile=$TMP/abc
2873         local tmpfile2=$TMP/abc2
2874         local seek
2875
2876         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2877                 skip "client encryption not supported"
2878
2879         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2880                 skip "need dummy encryption support"
2881
2882         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2883
2884         stack_trap cleanup_for_enc_tests EXIT
2885         setup_for_enc_tests
2886
2887         # write a few bytes in file
2888         echo "abc" > $tmpfile
2889         $LFS setstripe -c1 -i0 $testfile
2890         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
2891
2892         # check that in-memory representation of file is correct
2893         cmp -bl $tmpfile $testfile ||
2894                 error "file $testfile is corrupted in memory (1)"
2895
2896         cancel_lru_locks osc ; cancel_lru_locks mdc
2897
2898         # check that file read from server is correct
2899         cmp -bl $tmpfile $testfile ||
2900                 error "file $testfile is corrupted on server (1)"
2901
2902         # write a few other bytes in same page
2903         dd if=$tmpfile of=$testfile bs=4 count=1 seek=256 oflag=seek_bytes \
2904                 conv=fsync,notrunc
2905
2906         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=256 oflag=seek_bytes \
2907                 conv=fsync,notrunc
2908
2909         # check that in-memory representation of file is correct
2910         cmp -bl $tmpfile $testfile ||
2911                 error "file $testfile is corrupted in memory (2)"
2912
2913         cancel_lru_locks osc ; cancel_lru_locks mdc
2914
2915         # check that file read from server is correct
2916         cmp -bl $tmpfile $testfile ||
2917                 error "file $testfile is corrupted on server (2)"
2918
2919         rm -f $testfile $tmpfile
2920         cancel_lru_locks osc ; cancel_lru_locks mdc
2921
2922         # write a few bytes in file, at end of first page
2923         echo "abc" > $tmpfile
2924         $LFS setstripe -c1 -i0 $testfile
2925         seek=$(getconf PAGESIZE)
2926         seek=$((seek - 4))
2927         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2928                 conv=fsync,notrunc
2929
2930         # write a few other bytes at beginning of first page
2931         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
2932
2933         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2934                 conv=fsync,notrunc
2935
2936         # check that in-memory representation of file is correct
2937         cmp -bl $tmpfile $testfile ||
2938                 error "file $testfile is corrupted in memory (3)"
2939
2940         cancel_lru_locks osc ; cancel_lru_locks mdc
2941
2942         # check that file read from server is correct
2943         cmp -bl $tmpfile $testfile ||
2944                 error "file $testfile is corrupted on server (3)"
2945
2946         rm -f $testfile $tmpfile
2947         cancel_lru_locks osc ; cancel_lru_locks mdc
2948
2949         # write a few bytes in file, at beginning of second page
2950         echo "abc" > $tmpfile
2951         $LFS setstripe -c1 -i0 $testfile
2952         seek=$(getconf PAGESIZE)
2953         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2954                 conv=fsync,notrunc
2955         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
2956                 conv=fsync,notrunc
2957
2958         # write a few other bytes at end of first page
2959         seek=$((seek - 4))
2960         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2961                 conv=fsync,notrunc
2962         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
2963                 conv=fsync,notrunc
2964
2965         # check that in-memory representation of file is correct
2966         cmp -bl $tmpfile2 $testfile ||
2967                 error "file $testfile is corrupted in memory (4)"
2968
2969         cancel_lru_locks osc ; cancel_lru_locks mdc
2970
2971         # check that file read from server is correct
2972         cmp -bl $tmpfile2 $testfile ||
2973                 error "file $testfile is corrupted on server (4)"
2974
2975         rm -f $testfile $tmpfile $tmpfile2
2976         cancel_lru_locks osc ; cancel_lru_locks mdc
2977
2978         # write a few bytes in file, at beginning of first stripe
2979         echo "abc" > $tmpfile
2980         $LFS setstripe -S 256k -c2 $testfile
2981         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
2982
2983         # write a few other bytes, at beginning of second stripe
2984         dd if=$tmpfile of=$testfile bs=4 count=1 seek=262144 oflag=seek_bytes \
2985                 conv=fsync,notrunc
2986         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=262144 oflag=seek_bytes \
2987                 conv=fsync,notrunc
2988
2989         # check that in-memory representation of file is correct
2990         cmp -bl $tmpfile $testfile ||
2991                 error "file $testfile is corrupted in memory (5)"
2992
2993         cancel_lru_locks osc ; cancel_lru_locks mdc
2994
2995         # check that file read from server is correct
2996         cmp -bl $tmpfile $testfile ||
2997                 error "file $testfile is corrupted on server (5)"
2998
2999         rm -f $tmpfile
3000 }
3001 run_test 40 "exercise size of encrypted file"
3002
3003 test_41() {
3004         local testfile=$DIR/$tdir/$tfile
3005         local tmpfile=$TMP/abc
3006         local tmpfile2=$TMP/abc2
3007         local seek
3008
3009         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3010                 skip "client encryption not supported"
3011
3012         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3013                 skip "need dummy encryption support"
3014
3015         stack_trap cleanup_for_enc_tests EXIT
3016         setup_for_enc_tests
3017
3018         echo "abc" > $tmpfile
3019         seek=$(getconf PAGESIZE)
3020         seek=$((seek - 204))
3021         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3022                 conv=fsync
3023         seek=$(getconf PAGESIZE)
3024         seek=$((seek + 1092))
3025         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3026                 conv=fsync,notrunc
3027
3028         # write a few bytes in file
3029         $LFS setstripe -c1 -i0 -S 256k $testfile
3030         seek=$(getconf PAGESIZE)
3031         seek=$((seek - 204))
3032         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3033         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3034         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3035                 conv=fsync &
3036
3037         sleep 5
3038         # write a few other bytes, at a different offset
3039         seek=$(getconf PAGESIZE)
3040         seek=$((seek + 1092))
3041         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3042                 conv=fsync,notrunc &
3043         wait
3044         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3045
3046         # check that in-memory representation of file is correct
3047         cmp -bl $tmpfile2 $testfile ||
3048                 error "file $testfile is corrupted in memory (1)"
3049
3050         cancel_lru_locks osc ; cancel_lru_locks mdc
3051
3052         # check that file read from server is correct
3053         cmp -bl $tmpfile2 $testfile ||
3054                 error "file $testfile is corrupted on server (1)"
3055
3056         rm -f $tmpfile $tmpfile2
3057 }
3058 run_test 41 "test race on encrypted file size (1)"
3059
3060 test_42() {
3061         local testfile=$DIR/$tdir/$tfile
3062         local testfile2=$DIR2/$tdir/$tfile
3063         local tmpfile=$TMP/abc
3064         local tmpfile2=$TMP/abc2
3065         local pagesz=$(getconf PAGESIZE)
3066         local seek
3067
3068         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3069                 skip "client encryption not supported"
3070
3071         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3072                 skip "need dummy encryption support"
3073
3074         stack_trap cleanup_for_enc_tests EXIT
3075         setup_for_enc_tests
3076
3077         if is_mounted $MOUNT2; then
3078                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3079         fi
3080         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
3081                 error "mount2 with '-o test_dummy_encryption' failed"
3082
3083         # create file by writting one whole page
3084         $LFS setstripe -c1 -i0 -S 256k $testfile
3085         dd if=/dev/zero of=$testfile bs=$pagesz count=1 conv=fsync
3086
3087         # read file from 2nd mount point
3088         cat $testfile2 > /dev/null
3089
3090         echo "abc" > $tmpfile
3091         dd if=/dev/zero of=$tmpfile2 bs=$pagesz count=1 conv=fsync
3092         seek=$((2*pagesz - 204))
3093         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3094                 conv=fsync,notrunc
3095         seek=$((2*pagesz + 1092))
3096         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3097                 conv=fsync,notrunc
3098
3099         # write a few bytes in file from 1st mount point
3100         seek=$((2*pagesz - 204))
3101         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3102         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3103         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3104                 conv=fsync,notrunc &
3105
3106         sleep 5
3107         # write a few other bytes, at a different offset from 2nd mount point
3108         seek=$((2*pagesz + 1092))
3109         dd if=$tmpfile of=$testfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3110                 conv=fsync,notrunc &
3111         wait
3112         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3113
3114         # check that in-memory representation of file is correct
3115         cmp -bl $tmpfile2 $testfile ||
3116                 error "file $testfile is corrupted in memory (1)"
3117
3118         # check that in-memory representation of file is correct
3119         cmp -bl $tmpfile2 $testfile2 ||
3120                 error "file $testfile is corrupted in memory (2)"
3121
3122         cancel_lru_locks osc ; cancel_lru_locks mdc
3123
3124         # check that file read from server is correct
3125         cmp -bl $tmpfile2 $testfile ||
3126                 error "file $testfile is corrupted on server (1)"
3127
3128         rm -f $tmpfile $tmpfile2
3129 }
3130 run_test 42 "test race on encrypted file size (2)"
3131
3132 test_43() {
3133         local testfile=$DIR/$tdir/$tfile
3134         local testfile2=$DIR2/$tdir/$tfile
3135         local tmpfile=$TMP/abc
3136         local tmpfile2=$TMP/abc2
3137         local resfile=$TMP/res
3138         local pagesz=$(getconf PAGESIZE)
3139         local seek
3140
3141         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3142                 skip "client encryption not supported"
3143
3144         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3145                 skip "need dummy encryption support"
3146
3147         stack_trap cleanup_for_enc_tests EXIT
3148         setup_for_enc_tests
3149
3150         if is_mounted $MOUNT2; then
3151                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3152         fi
3153         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
3154                 error "mount2 with '-o test_dummy_encryption' failed"
3155
3156         # create file
3157         tr '\0' '1' < /dev/zero |
3158                 dd of=$tmpfile bs=1 count=$pagesz conv=fsync
3159         $LFS setstripe -c1 -i0 -S 256k $testfile
3160         cp $tmpfile $testfile
3161
3162         # read file from 2nd mount point
3163         cat $testfile2 > /dev/null
3164
3165         # write a few bytes in file from 1st mount point
3166         echo "abc" > $tmpfile2
3167         seek=$((2*pagesz - 204))
3168         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3169         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3170         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3171                 conv=fsync,notrunc &
3172
3173         sleep 5
3174         # read file from 2nd mount point
3175         dd if=$testfile2 of=$resfile bs=$pagesz count=1 conv=fsync,notrunc
3176         cmp -bl $tmpfile $resfile ||
3177                 error "file $testfile is corrupted in memory (1)"
3178
3179         wait
3180         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3181
3182         # check that in-memory representation of file is correct
3183         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3184                 conv=fsync,notrunc
3185         cmp -bl $tmpfile $testfile2 ||
3186                 error "file $testfile is corrupted in memory (2)"
3187
3188         cancel_lru_locks osc ; cancel_lru_locks mdc
3189
3190         # check that file read from server is correct
3191         cmp -bl $tmpfile $testfile ||
3192                 error "file $testfile is corrupted on server (1)"
3193
3194         rm -f $tmpfile $tmpfile2
3195 }
3196 run_test 43 "test race on encrypted file size (3)"
3197
3198 test_44() {
3199         local testfile=$DIR/$tdir/$tfile
3200         local tmpfile=$TMP/abc
3201         local resfile=$TMP/resfile
3202         local respage
3203
3204         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3205                 skip "client encryption not supported"
3206
3207         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3208                 skip "need dummy encryption support"
3209
3210         which vmtouch || skip "This test needs vmtouch utility"
3211
3212         # Direct I/O is now supported on encrypted files.
3213
3214         stack_trap cleanup_for_enc_tests EXIT
3215         setup_for_enc_tests
3216
3217         $LFS setstripe -c1 -i0 $testfile
3218         dd if=/dev/urandom of=$tmpfile bs=8192 count=1 conv=fsync
3219         dd if=$tmpfile of=$testfile bs=8192 count=1 oflag=direct ||
3220                 error "could not write to file with O_DIRECT (1)"
3221
3222         respage=$(vmtouch $testfile | awk '/Resident\ Pages:/ {print $3}')
3223         [ "$respage" == "0/2" ] ||
3224                 error "write to enc file fell back to buffered IO"
3225
3226         cancel_lru_locks
3227
3228         dd if=$testfile of=$resfile bs=8192 count=1 iflag=direct ||
3229                 error "could not read from file with O_DIRECT (1)"
3230
3231         respage=$(vmtouch $testfile | awk '/Resident\ Pages:/ {print $3}')
3232         [ "$respage" == "0/2" ] ||
3233                 error "read from enc file fell back to buffered IO"
3234
3235         cmp -bl $tmpfile $resfile ||
3236                 error "file $testfile is corrupted (1)"
3237
3238         rm -f $resfile
3239
3240         $TRUNCATE $tmpfile 4096
3241         dd if=$tmpfile of=$testfile bs=4096 count=1 seek=13 oflag=direct ||
3242                 error "could not write to file with O_DIRECT (2)"
3243
3244         cancel_lru_locks
3245
3246         dd if=$testfile of=$resfile bs=4096 count=1 skip=13 iflag=direct ||
3247                 error "could not read from file with O_DIRECT (2)"
3248         cmp -bl $tmpfile $resfile ||
3249                 error "file $testfile is corrupted (2)"
3250
3251         rm -f $testfile $resfile
3252         $LFS setstripe -c1 -i0 $testfile
3253
3254         $TRUNCATE $tmpfile 2043
3255         cp $tmpfile $testfile
3256
3257         cancel_lru_locks
3258
3259         dd if=$testfile of=$resfile bs=4096 count=1 iflag=direct ||
3260                 error "could not read from file with O_DIRECT (3)"
3261         cmp -bl $tmpfile $resfile ||
3262                 error "file $testfile is corrupted (3)"
3263
3264         rm -f $tmpfile $resfile
3265 }
3266 run_test 44 "encrypted file access semantics: direct IO"
3267
3268 test_45() {
3269         local testfile=$DIR/$tdir/$tfile
3270         local tmpfile=$TMP/junk
3271
3272         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3273                 skip "client encryption not supported"
3274
3275         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3276                 skip "need dummy encryption support"
3277
3278         stack_trap cleanup_for_enc_tests EXIT
3279         setup_for_enc_tests
3280
3281         $LFS setstripe -c1 -i0 $testfile
3282         dd if=/dev/zero of=$testfile bs=512K count=1
3283         $MULTIOP $testfile OSMRUc || error "$MULTIOP $testfile failed (1)"
3284         $MULTIOP $testfile OSMWUc || error "$MULTIOP $testfile failed (2)"
3285
3286         dd if=/dev/zero of=$tmpfile bs=512K count=1
3287         $MULTIOP $tmpfile OSMWUc || error "$MULTIOP $tmpfile failed"
3288         $MMAP_CAT $tmpfile > ${tmpfile}2
3289
3290         cancel_lru_locks osc ; cancel_lru_locks mdc
3291
3292         $MMAP_CAT $testfile > ${testfile}2
3293         cmp -bl ${tmpfile}2 ${testfile}2 ||
3294                 error "file $testfile is corrupted"
3295
3296         rm -f $tmpfile ${tmpfile}2
3297 }
3298 run_test 45 "encrypted file access semantics: MMAP"
3299
3300 test_46() {
3301         local testdir=$DIR/$tdir/mydir
3302         local testfile=$testdir/myfile
3303         local lsfile=$TMP/lsfile
3304         local scrambleddir
3305         local scrambledfile
3306
3307         local testfile2=$DIR/$tdir/${tfile}.2
3308         local tmpfile=$DIR/junk
3309
3310         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3311                 skip "client encryption not supported"
3312
3313         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3314                 skip "need dummy encryption support"
3315
3316         stack_trap cleanup_for_enc_tests EXIT
3317         setup_for_enc_tests
3318
3319         touch $DIR/onefile
3320         touch $DIR/$tdir/$tfile
3321         mkdir $testdir
3322         echo test > $testfile
3323         sync ; echo 3 > /proc/sys/vm/drop_caches
3324
3325         # remove fscrypt key from keyring
3326         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3327         keyctl reap
3328
3329         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
3330         ls -1 $scrambleddir > $lsfile || error "ls $testdir failed"
3331
3332         scrambledfile=$scrambleddir/$(head -n 1 $lsfile)
3333         stat $scrambledfile || error "stat $scrambledfile failed"
3334         rm -f $lsfile
3335
3336         cat $scrambledfile && error "cat $scrambledfile should have failed"
3337
3338         touch $scrambleddir/otherfile &&
3339                 error "touch otherfile should have failed"
3340         ls $scrambleddir/otherfile && error "otherfile should not exist"
3341         mkdir $scrambleddir/otherdir &&
3342                 error "mkdir otherdir should have failed"
3343         ls -d $scrambleddir/otherdir && error "otherdir should not exist"
3344
3345         rm -f $scrambledfile || error "rm $scrambledfile failed"
3346         rmdir $scrambleddir || error "rmdir $scrambleddir failed"
3347
3348         rm -f $DIR/onefile
3349 }
3350 run_test 46 "encrypted file access semantics without key"
3351
3352 test_47() {
3353         local testfile=$DIR/$tdir/$tfile
3354         local testfile2=$DIR/$tdir/${tfile}.2
3355         local tmpfile=$DIR/junk
3356         local scrambleddir
3357         local scrambledfile
3358
3359         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3360                 skip "client encryption not supported"
3361
3362         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3363                 skip "need dummy encryption support"
3364
3365         stack_trap cleanup_for_enc_tests EXIT
3366         setup_for_enc_tests
3367
3368         dd if=/dev/zero of=$tmpfile bs=512K count=1
3369         mrename $tmpfile $testfile &&
3370                 error "rename from unencrypted to encrypted dir should fail"
3371
3372         ln $tmpfile $testfile &&
3373                 error "link from unencrypted to encrypted dir should fail"
3374
3375         cp $tmpfile $testfile ||
3376                 error "cp from unencrypted to encrypted dir should succeed"
3377         rm -f $tmpfile
3378
3379         mrename $testfile $testfile2 ||
3380                 error "rename from within encrypted dir should succeed"
3381
3382         ln $testfile2 $testfile ||
3383                 error "link from within encrypted dir should succeed"
3384         rm -f $testfile
3385
3386         ln $testfile2 $tmpfile ||
3387                 error "link from encrypted to unencrypted dir should succeed"
3388         rm -f $tmpfile
3389
3390         mrename $testfile2 $tmpfile ||
3391                 error "rename from encrypted to unencrypted dir should succeed"
3392
3393         dd if=/dev/zero of=$testfile bs=512K count=1
3394         mkdir $DIR/$tdir/mydir
3395         sync ; echo 3 > /proc/sys/vm/drop_caches
3396
3397         # remove fscrypt key from keyring
3398         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3399         keyctl reap
3400
3401         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
3402         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
3403         ln $scrambledfile $scrambleddir/linkfile &&
3404                 error "ln linkfile should have failed"
3405         mrename $scrambledfile $DIR/onefile2 &&
3406                 error "mrename from $scrambledfile should have failed"
3407         touch $DIR/onefile
3408         mrename $DIR/onefile $scrambleddir/otherfile &&
3409                 error "mrename to $scrambleddir should have failed"
3410
3411         rm -f $tmpfile $DIR/onefile
3412 }
3413 run_test 47 "encrypted file access semantics: rename/link"
3414
3415 test_48a() {
3416         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
3417         local testfile=$DIR/$tdir/$tfile
3418         local tmpfile=$TMP/111
3419         local tmpfile2=$TMP/abc
3420         local pagesz=$(getconf PAGESIZE)
3421         local sz
3422         local seek
3423         local scrambledfile
3424
3425         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3426                 skip "client encryption not supported"
3427
3428         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3429                 skip "need dummy encryption support"
3430
3431         stack_trap cleanup_for_enc_tests EXIT
3432         setup_for_enc_tests
3433
3434         # create file, 4 x PAGE_SIZE long
3435         tr '\0' '1' < /dev/zero |
3436                 dd of=$tmpfile bs=1 count=4x$pagesz conv=fsync
3437         $LFS setstripe -c1 -i0 $testfile
3438         cp $tmpfile $testfile
3439         echo "abc" > $tmpfile2
3440
3441         # decrease size: truncate to PAGE_SIZE
3442         $TRUNCATE $tmpfile $pagesz
3443         $TRUNCATE $testfile $pagesz
3444         cancel_lru_locks osc ; cancel_lru_locks mdc
3445         cmp -bl $tmpfile $testfile ||
3446                 error "file $testfile is corrupted (1)"
3447
3448         # increase size: truncate to 2 x PAGE_SIZE
3449         sz=$((pagesz*2))
3450         $TRUNCATE $tmpfile $sz
3451         $TRUNCATE $testfile $sz
3452         cancel_lru_locks osc ; cancel_lru_locks mdc
3453         cmp -bl $tmpfile $testfile ||
3454                 error "file $testfile is corrupted (2)"
3455
3456         # write in 2nd page
3457         seek=$((pagesz+100))
3458         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3459                 conv=fsync,notrunc
3460         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3461                 conv=fsync,notrunc
3462         cancel_lru_locks osc ; cancel_lru_locks mdc
3463         cmp -bl $tmpfile $testfile ||
3464                 error "file $testfile is corrupted (3)"
3465
3466         # truncate to PAGE_SIZE / 2
3467         sz=$((pagesz/2))
3468         $TRUNCATE $tmpfile $sz
3469         $TRUNCATE $testfile $sz
3470         cancel_lru_locks osc ; cancel_lru_locks mdc
3471         cmp -bl $tmpfile $testfile ||
3472                 error "file $testfile is corrupted (4)"
3473
3474         # lockless truncate should be turned into regular truncate for enc file
3475         save_lustre_params client "osc.*.lockless_truncate" > $save
3476         # restore lockless_truncate default values on exit
3477         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
3478         cancel_lru_locks osc ; cancel_lru_locks mdc
3479         lctl set_param -n osc.*.lockless_truncate 1
3480         cancel_lru_locks osc
3481         clear_stats osc.*.osc_stats
3482         $TRUNCATE $testfile 8000000 || error "truncate failed (1)"
3483         [ $(calc_stats osc.*.osc_stats lockless_truncate) -eq 0 ] ||
3484                 error "lockless truncate should be turned into regular truncate"
3485         lctl set_param -n osc.*.lockless_truncate 0
3486
3487         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
3488         sz=$((sz-7))
3489         $TRUNCATE $tmpfile $sz
3490         $TRUNCATE $testfile $sz
3491         cancel_lru_locks osc ; cancel_lru_locks mdc
3492         cmp -bl $tmpfile $testfile ||
3493                 error "file $testfile is corrupted (5)"
3494
3495         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
3496         sz=$((sz+18))
3497         $TRUNCATE $tmpfile $sz
3498         $TRUNCATE $testfile $sz
3499         cancel_lru_locks osc ; cancel_lru_locks mdc
3500         cmp -bl $tmpfile $testfile ||
3501                 error "file $testfile is corrupted (6)"
3502
3503         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
3504         sz=$((sz+pagesz+30))
3505         $TRUNCATE $tmpfile $sz
3506         $TRUNCATE $testfile $sz
3507         cancel_lru_locks osc ; cancel_lru_locks mdc
3508         cmp -bl $tmpfile $testfile ||
3509                 error "file $testfile is corrupted (7)"
3510
3511         sync ; echo 3 > /proc/sys/vm/drop_caches
3512
3513         # remove fscrypt key from keyring
3514         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3515         keyctl reap
3516
3517         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
3518         $TRUNCATE $scrambledfile 0 &&
3519                 error "truncate $scrambledfile should have failed without key"
3520
3521         rm -f $tmpfile $tmpfile2
3522 }
3523 run_test 48a "encrypted file access semantics: truncate"
3524
3525 cleanup_for_enc_tests_othercli() {
3526         local othercli=$1
3527
3528         # remount othercli normally
3529         zconf_umount $othercli $MOUNT ||
3530                 error "umount $othercli $MOUNT failed"
3531         zconf_mount $othercli $MOUNT ||
3532                 error "remount $othercli $MOUNT failed"
3533 }
3534
3535 test_48b() {
3536         local othercli
3537
3538         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3539                 skip "client encryption not supported"
3540
3541         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3542                 skip "need dummy encryption support"
3543
3544         [ "$num_clients" -ge 2 ] || skip "Need at least 2 clients"
3545
3546         if [ "$HOSTNAME" == ${clients_arr[0]} ]; then
3547                 othercli=${clients_arr[1]}
3548         else
3549                 othercli=${clients_arr[0]}
3550         fi
3551
3552         stack_trap cleanup_for_enc_tests EXIT
3553         stack_trap "cleanup_for_enc_tests_othercli $othercli" EXIT
3554         setup_for_enc_tests
3555         zconf_umount $othercli $MOUNT ||
3556                 error "umount $othercli $MOUNT failed"
3557
3558         cp /bin/sleep $DIR/$tdir/
3559         cancel_lru_locks osc ; cancel_lru_locks mdc
3560         $DIR/$tdir/sleep 30 &
3561         # mount and IOs must be done in the same shell session, otherwise
3562         # encryption key in session keyring is missing
3563         do_node $othercli "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3564                            $MGSNID:/$FSNAME $MOUNT && \
3565                            $TRUNCATE $DIR/$tdir/sleep 7"
3566         wait || error "wait error"
3567         cmp --silent /bin/sleep $DIR/$tdir/sleep ||
3568                 error "/bin/sleep and $DIR/$tdir/sleep differ"
3569 }
3570 run_test 48b "encrypted file: concurrent truncate"
3571
3572 trace_cmd() {
3573         local cmd="$@"
3574         local xattr_name="security.c"
3575
3576         cancel_lru_locks osc ; cancel_lru_locks mdc
3577         $LCTL set_param debug=+info
3578         $LCTL clear
3579
3580         echo $cmd
3581         eval $cmd
3582
3583         $LCTL dk | grep -E "get xattr '${xattr_name}'|get xattrs"
3584         [ $? -ne 0 ] || error "get xattr event was triggered"
3585 }
3586
3587 test_49() {
3588         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3589                 skip "client encryption not supported"
3590
3591         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3592                 skip "need dummy encryption support"
3593
3594         stack_trap cleanup_for_enc_tests EXIT
3595         setup_for_enc_tests
3596
3597         local dirname=$DIR/$tdir/subdir
3598
3599         mkdir $dirname
3600
3601         trace_cmd stat $dirname
3602         trace_cmd touch $dirname/f1
3603         trace_cmd stat $dirname/f1
3604         trace_cmd cat $dirname/f1
3605         dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
3606         trace_cmd $TRUNCATE $dirname/f1 10240
3607         trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
3608         trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
3609         trace_cmd $LFS setdirstripe -i 1 $dirname/d2
3610         trace_cmd $LFS migrate -m 0 $dirname/d2
3611
3612         $LFS setdirstripe -i 1 -c 1 $dirname/d3
3613         dirname=$dirname/d3/subdir
3614         mkdir $dirname
3615
3616         trace_cmd stat $dirname
3617         trace_cmd touch $dirname/f1
3618         trace_cmd stat $dirname/f1
3619         trace_cmd cat $dirname/f1
3620         dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
3621         trace_cmd $TRUNCATE $dirname/f1 10240
3622         trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
3623         trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
3624 }
3625 run_test 49 "Avoid getxattr for encryption context"
3626
3627 test_50() {
3628         local testfile=$DIR/$tdir/$tfile
3629         local tmpfile=$TMP/abc
3630         local pagesz=$(getconf PAGESIZE)
3631         local sz
3632
3633         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3634                 skip "client encryption not supported"
3635
3636         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3637                 skip "need dummy encryption support"
3638
3639         stack_trap cleanup_for_enc_tests EXIT
3640         setup_for_enc_tests
3641
3642         # write small file, data on MDT only
3643         tr '\0' '1' < /dev/zero |
3644             dd of=$tmpfile bs=1 count=5000 conv=fsync
3645         $LFS setstripe -E 1M -L mdt -E EOF $testfile
3646         cp $tmpfile $testfile
3647
3648         # check that in-memory representation of file is correct
3649         cmp -bl $tmpfile $testfile ||
3650                 error "file $testfile is corrupted in memory"
3651
3652         cancel_lru_locks osc ; cancel_lru_locks mdc
3653
3654         # check that file read from server is correct
3655         cmp -bl $tmpfile $testfile ||
3656                 error "file $testfile is corrupted on server"
3657
3658         # decrease size: truncate to PAGE_SIZE
3659         $TRUNCATE $tmpfile $pagesz
3660         $TRUNCATE $testfile $pagesz
3661         cancel_lru_locks osc ; cancel_lru_locks mdc
3662         cmp -bl $tmpfile $testfile ||
3663                 error "file $testfile is corrupted (1)"
3664
3665         # increase size: truncate to 2 x PAGE_SIZE
3666         sz=$((pagesz*2))
3667         $TRUNCATE $tmpfile $sz
3668         $TRUNCATE $testfile $sz
3669         cancel_lru_locks osc ; cancel_lru_locks mdc
3670         cmp -bl $tmpfile $testfile ||
3671                 error "file $testfile is corrupted (2)"
3672
3673         # truncate to PAGE_SIZE / 2
3674         sz=$((pagesz/2))
3675         $TRUNCATE $tmpfile $sz
3676         $TRUNCATE $testfile $sz
3677         cancel_lru_locks osc ; cancel_lru_locks mdc
3678         cmp -bl $tmpfile $testfile ||
3679                 error "file $testfile is corrupted (3)"
3680
3681         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
3682         sz=$((sz-7))
3683         $TRUNCATE $tmpfile $sz
3684         $TRUNCATE $testfile $sz
3685         cancel_lru_locks osc ; cancel_lru_locks mdc
3686         cmp -bl $tmpfile $testfile ||
3687                 error "file $testfile is corrupted (4)"
3688
3689         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
3690         sz=$((sz+18))
3691         $TRUNCATE $tmpfile $sz
3692         $TRUNCATE $testfile $sz
3693         cancel_lru_locks osc ; cancel_lru_locks mdc
3694         cmp -bl $tmpfile $testfile ||
3695                 error "file $testfile is corrupted (5)"
3696
3697         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
3698         sz=$((sz+pagesz+30))
3699         $TRUNCATE $tmpfile $sz
3700         $TRUNCATE $testfile $sz
3701         cancel_lru_locks osc ; cancel_lru_locks mdc
3702         cmp -bl $tmpfile $testfile ||
3703                 error "file $testfile is corrupted (6)"
3704
3705         rm -f $testfile
3706         cancel_lru_locks osc ; cancel_lru_locks mdc
3707
3708         # write hole in file, data spread on MDT and OST
3709         tr '\0' '2' < /dev/zero |
3710             dd of=$tmpfile bs=1 count=1539 seek=1539074 conv=fsync,notrunc
3711         $LFS setstripe -E 1M -L mdt -E EOF $testfile
3712         cp --sparse=always $tmpfile $testfile
3713
3714         # check that in-memory representation of file is correct
3715         cmp -bl $tmpfile $testfile ||
3716                 error "file $testfile is corrupted in memory"
3717
3718         cancel_lru_locks osc ; cancel_lru_locks mdc
3719
3720         # check that file read from server is correct
3721         cmp -bl $tmpfile $testfile ||
3722                 error "file $testfile is corrupted on server"
3723
3724         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
3725         # inside OST part of data
3726         sz=$((1024*1024+13))
3727         $TRUNCATE $tmpfile $sz
3728         $TRUNCATE $testfile $sz
3729         cancel_lru_locks osc ; cancel_lru_locks mdc
3730         cmp -bl $tmpfile $testfile ||
3731                 error "file $testfile is corrupted (7)"
3732
3733         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
3734         # inside MDT part of data
3735         sz=7
3736         $TRUNCATE $tmpfile $sz
3737         $TRUNCATE $testfile $sz
3738         cancel_lru_locks osc ; cancel_lru_locks mdc
3739         cmp -bl $tmpfile $testfile ||
3740                 error "file $testfile is corrupted (8)"
3741
3742         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
3743         # inside MDT part of data
3744         sz=$((1024*1024-13))
3745         $TRUNCATE $tmpfile $sz
3746         $TRUNCATE $testfile $sz
3747         cancel_lru_locks osc ; cancel_lru_locks mdc
3748         cmp -bl $tmpfile $testfile ||
3749                 error "file $testfile is corrupted (9)"
3750
3751         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
3752         # inside OST part of data
3753         sz=$((1024*1024+7))
3754         $TRUNCATE $tmpfile $sz
3755         $TRUNCATE $testfile $sz
3756         cancel_lru_locks osc ; cancel_lru_locks mdc
3757         cmp -bl $tmpfile $testfile ||
3758                 error "file $testfile is corrupted (10)"
3759
3760         rm -f $tmpfile
3761 }
3762 run_test 50 "DoM encrypted file"
3763
3764 test_51() {
3765         [ "$MDS1_VERSION" -gt $(version_code 2.13.53) ] ||
3766                 skip "Need MDS version at least 2.13.53"
3767
3768         mkdir $DIR/$tdir || error "mkdir $tdir"
3769
3770         touch $DIR/$tdir/$tfile || error "touch $tfile"
3771         cp $(which chown) $DIR/$tdir || error "cp chown"
3772         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile &&
3773                 error "chown $tfile should fail"
3774         setcap 'CAP_CHOWN=ep' $DIR/$tdir/chown || error "setcap CAP_CHOWN"
3775         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile ||
3776                 error "chown $tfile"
3777         rm $DIR/$tdir/$tfile || error "rm $tfile"
3778
3779         touch $DIR/$tdir/$tfile || error "touch $tfile"
3780         cp $(which touch) $DIR/$tdir || error "cp touch"
3781         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile &&
3782                 error "touch should fail"
3783         setcap 'CAP_FOWNER=ep' $DIR/$tdir/touch || error "setcap CAP_FOWNER"
3784         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile ||
3785                 error "touch $tfile"
3786         rm $DIR/$tdir/$tfile || error "rm $tfile"
3787
3788         local cap
3789         for cap in "CAP_DAC_OVERRIDE" "CAP_DAC_READ_SEARCH"; do
3790                 touch $DIR/$tdir/$tfile || error "touch $tfile"
3791                 chmod 600 $DIR/$tdir/$tfile || error "chmod $tfile"
3792                 cp $(which cat) $DIR/$tdir || error "cp cat"
3793                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile &&
3794                         error "cat should fail"
3795                 setcap $cap=ep $DIR/$tdir/cat || error "setcap $cap"
3796                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile ||
3797                         error "cat $tfile"
3798                 rm $DIR/$tdir/$tfile || error "rm $tfile"
3799         done
3800 }
3801 run_test 51 "FS capabilities ==============="
3802
3803 test_52() {
3804         local testfile=$DIR/$tdir/$tfile
3805         local tmpfile=$TMP/$tfile
3806         local mirror1=$TMP/$tfile.mirror1
3807         local mirror2=$TMP/$tfile.mirror2
3808
3809         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3810                 skip "client encryption not supported"
3811
3812         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3813                 skip "need dummy encryption support"
3814
3815         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
3816
3817         stack_trap cleanup_for_enc_tests EXIT
3818         setup_for_enc_tests
3819
3820         dd if=/dev/urandom of=$tmpfile bs=5000 count=1 conv=fsync
3821
3822         $LFS mirror create -N -i0 -N -i1 $testfile ||
3823                 error "could not create mirror"
3824
3825         dd if=$tmpfile of=$testfile bs=5000 count=1 conv=fsync ||
3826                 error "could not write to $testfile"
3827
3828         $LFS mirror resync $testfile ||
3829                 error "could not resync mirror"
3830
3831         $LFS mirror verify -v $testfile ||
3832                 error "verify mirror failed"
3833
3834         $LFS mirror read -N 1 -o $mirror1 $testfile ||
3835                 error "could not read from mirror 1"
3836
3837         cmp -bl $tmpfile $mirror1 ||
3838                 error "mirror 1 is corrupted"
3839
3840         $LFS mirror read -N 2 -o $mirror2 $testfile ||
3841                 error "could not read from mirror 2"
3842
3843         cmp -bl $tmpfile $mirror2 ||
3844                 error "mirror 2 is corrupted"
3845
3846         tr '\0' '2' < /dev/zero |
3847             dd of=$tmpfile bs=1 count=9000 conv=fsync
3848
3849         $LFS mirror write -N 1 -i $tmpfile $testfile ||
3850                 error "could not write to mirror 1"
3851
3852         $LFS mirror verify -v $testfile &&
3853                 error "mirrors should be different"
3854
3855         rm -f $tmpfile $mirror1 $mirror2
3856 }
3857 run_test 52 "Mirrored encrypted file"
3858
3859 test_53() {
3860         local testfile=$DIR/$tdir/$tfile
3861         local testfile2=$DIR2/$tdir/$tfile
3862         local tmpfile=$TMP/$tfile.tmp
3863         local resfile=$TMP/$tfile.res
3864         local pagesz
3865         local filemd5
3866
3867         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3868                 skip "client encryption not supported"
3869
3870         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3871                 skip "need dummy encryption support"
3872
3873         pagesz=$(getconf PAGESIZE)
3874         [[ $pagesz == 65536 ]] || skip "Need 64K PAGE_SIZE client"
3875
3876         do_node $mds1_HOST \
3877                 "mount.lustre --help |& grep -q 'test_dummy_encryption:'" ||
3878                         skip "need dummy encryption support on MDS client mount"
3879
3880         # this test is probably useless now, but may turn out to be useful when
3881         # Lustre supports servers with PAGE_SIZE != 4KB
3882         pagesz=$(do_node $mds1_HOST getconf PAGESIZE)
3883         [[ $pagesz == 4096 ]] || skip "Need 4K PAGE_SIZE MDS client"
3884
3885         stack_trap cleanup_for_enc_tests EXIT
3886         stack_trap "zconf_umount $mds1_HOST $MOUNT2" EXIT
3887         setup_for_enc_tests
3888
3889         $LFS setstripe -c1 -i0 $testfile
3890
3891         # write from 1st client
3892         cat /dev/urandom | tr -dc 'a-zA-Z0-9' |
3893                 dd of=$tmpfile bs=$((pagesz+3)) count=2 conv=fsync
3894         dd if=$tmpfile of=$testfile bs=$((pagesz+3)) count=2 conv=fsync ||
3895                 error "could not write to $testfile (1)"
3896
3897         # read from 2nd client
3898         # mount and IOs must be done in the same shell session, otherwise
3899         # encryption key in session keyring is missing
3900         do_node $mds1_HOST "mkdir -p $MOUNT2"
3901         do_node $mds1_HOST \
3902                 "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3903                  $MGSNID:/$FSNAME $MOUNT2 && \
3904                  dd if=$testfile2 of=$resfile bs=$((pagesz+3)) count=2" ||
3905                 error "could not read from $testfile2 (1)"
3906
3907         # compare
3908         filemd5=$(do_node $mds1_HOST md5sum $resfile | awk '{print $1}')
3909         [ $filemd5 = $(md5sum $tmpfile | awk '{print $1}') ] ||
3910                 error "file is corrupted (1)"
3911         do_node $mds1_HOST rm -f $resfile
3912         cancel_lru_locks
3913
3914         # truncate from 2nd client
3915         $TRUNCATE $tmpfile $((pagesz+3))
3916         zconf_umount $mds1_HOST $MOUNT2 ||
3917                 error "umount $mds1_HOST $MOUNT2 failed (1)"
3918         do_node $mds1_HOST "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3919                            $MGSNID:/$FSNAME $MOUNT2 && \
3920                            $TRUNCATE $testfile2 $((pagesz+3))" ||
3921                 error "could not truncate $testfile2 (1)"
3922
3923         # compare
3924         cmp -bl $tmpfile $testfile ||
3925                 error "file is corrupted (2)"
3926         rm -f $tmpfile $testfile
3927         cancel_lru_locks
3928         zconf_umount $mds1_HOST $MOUNT2 ||
3929                 error "umount $mds1_HOST $MOUNT2 failed (2)"
3930
3931         # do conversly
3932         do_node $mds1_HOST \
3933               dd if=/dev/urandom of=$tmpfile bs=$((pagesz+3)) count=2 conv=fsync
3934         # write from 2nd client
3935         do_node $mds1_HOST \
3936            "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3937             $MGSNID:/$FSNAME $MOUNT2 && \
3938             dd if=$tmpfile of=$testfile2 bs=$((pagesz+3)) count=2 conv=fsync" ||
3939                 error "could not write to $testfile2 (2)"
3940
3941         # read from 1st client
3942         dd if=$testfile of=$resfile bs=$((pagesz+3)) count=2 ||
3943                 error "could not read from $testfile (2)"
3944
3945         # compare
3946         filemd5=$(do_node $mds1_HOST md5sum -b $tmpfile | awk '{print $1}')
3947         [ $filemd5 = $(md5sum -b $resfile | awk '{print $1}') ] ||
3948                 error "file is corrupted (3)"
3949         rm -f $resfile
3950         cancel_lru_locks
3951
3952         # truncate from 1st client
3953         do_node $mds1_HOST "$TRUNCATE $tmpfile $((pagesz+3))"
3954         $TRUNCATE $testfile $((pagesz+3)) ||
3955                 error "could not truncate $testfile (2)"
3956
3957         # compare
3958         zconf_umount $mds1_HOST $MOUNT2 ||
3959                 error "umount $mds1_HOST $MOUNT2 failed (3)"
3960         do_node $mds1_HOST "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3961                            $MGSNID:/$FSNAME $MOUNT2 && \
3962                            cmp -bl $tmpfile $testfile2" ||
3963                 error "file is corrupted (4)"
3964
3965         do_node $mds1_HOST rm -f $tmpfile
3966         rm -f $tmpfile
3967 }
3968 run_test 53 "Mixed PAGE_SIZE clients"
3969
3970 log "cleanup: ======================================================"
3971
3972 sec_unsetup() {
3973         for num in $(seq $MDSCOUNT); do
3974                 if [ "${identity_old[$num]}" = 1 ]; then
3975                         switch_identity $num false || identity_old[$num]=$?
3976                 fi
3977         done
3978
3979         $RUNAS_CMD -u $ID0 ls $DIR
3980         $RUNAS_CMD -u $ID1 ls $DIR
3981 }
3982 sec_unsetup
3983
3984 complete $SECONDS
3985 check_and_cleanup_lustre
3986 exit_status