Whamcloud - gitweb
LU-12275 sec: O_DIRECT for encrypted file
[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
2791         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2792                 skip "client encryption not supported"
2793
2794         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2795                 skip "need dummy encryption support"
2796
2797         stack_trap cleanup_for_enc_tests EXIT
2798         setup_for_enc_tests
2799
2800         # get block size on ost
2801         blksz=$($LCTL get_param osc.$FSNAME*.import |
2802                 awk '/grant_block_size:/ { print $2; exit; }')
2803         # write a few bytes in file at offset $blksz
2804         echo "abc" > $tmpfile
2805         $LFS setstripe -c1 -i0 $testfile
2806         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$blksz \
2807                 oflag=seek_bytes conv=fsync
2808
2809         # check that in-memory representation of file is correct
2810         bsize=$(stat --format=%B $testfile)
2811         filesz=$(stat --format=%b $testfile)
2812         filesz=$((filesz*bsize))
2813         [ $filesz -le $blksz ] ||
2814                 error "file $testfile is $filesz long in memory"
2815
2816         cancel_lru_locks osc ; cancel_lru_locks mdc
2817
2818         # check that file read from server is correct
2819         bsize=$(stat --format=%B $testfile)
2820         filesz=$(stat --format=%b $testfile)
2821         filesz=$((filesz*bsize))
2822         [ $filesz -le $blksz ] ||
2823                 error "file $testfile is $filesz long on server"
2824
2825         rm -f $tmpfile
2826 }
2827 run_test 38 "encrypted file with hole"
2828
2829 test_39() {
2830         local testfile=$DIR/$tdir/$tfile
2831         local tmpfile=$TMP/abc
2832
2833         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2834                 skip "client encryption not supported"
2835
2836         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2837                 skip "need dummy encryption support"
2838
2839         stack_trap cleanup_for_enc_tests EXIT
2840         setup_for_enc_tests
2841
2842         # write a few bytes in file
2843         echo "abc" > $tmpfile
2844         $LFS setstripe -c1 -i0 $testfile
2845         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
2846
2847         # write a few more bytes in the same page
2848         dd if=$tmpfile of=$testfile bs=4 count=1 seek=1024 oflag=seek_bytes \
2849                 conv=fsync,notrunc
2850
2851         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=1024 oflag=seek_bytes \
2852                 conv=fsync,notrunc
2853
2854         # check that in-memory representation of file is correct
2855         cmp -bl $tmpfile $testfile ||
2856                 error "file $testfile is corrupted in memory"
2857
2858         cancel_lru_locks osc ; cancel_lru_locks mdc
2859
2860         # check that file read from server is correct
2861         cmp -bl $tmpfile $testfile ||
2862                 error "file $testfile is corrupted on server"
2863
2864         rm -f $tmpfile
2865 }
2866 run_test 39 "rewrite data in already encrypted page"
2867
2868 test_40() {
2869         local testfile=$DIR/$tdir/$tfile
2870         local tmpfile=$TMP/abc
2871         local tmpfile2=$TMP/abc2
2872         local seek
2873
2874         $LCTL get_param mdc.*.import | grep -q client_encryption ||
2875                 skip "client encryption not supported"
2876
2877         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
2878                 skip "need dummy encryption support"
2879
2880         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2881
2882         stack_trap cleanup_for_enc_tests EXIT
2883         setup_for_enc_tests
2884
2885         # write a few bytes in file
2886         echo "abc" > $tmpfile
2887         $LFS setstripe -c1 -i0 $testfile
2888         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
2889
2890         # check that in-memory representation of file is correct
2891         cmp -bl $tmpfile $testfile ||
2892                 error "file $testfile is corrupted in memory (1)"
2893
2894         cancel_lru_locks osc ; cancel_lru_locks mdc
2895
2896         # check that file read from server is correct
2897         cmp -bl $tmpfile $testfile ||
2898                 error "file $testfile is corrupted on server (1)"
2899
2900         # write a few other bytes in same page
2901         dd if=$tmpfile of=$testfile bs=4 count=1 seek=256 oflag=seek_bytes \
2902                 conv=fsync,notrunc
2903
2904         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=256 oflag=seek_bytes \
2905                 conv=fsync,notrunc
2906
2907         # check that in-memory representation of file is correct
2908         cmp -bl $tmpfile $testfile ||
2909                 error "file $testfile is corrupted in memory (2)"
2910
2911         cancel_lru_locks osc ; cancel_lru_locks mdc
2912
2913         # check that file read from server is correct
2914         cmp -bl $tmpfile $testfile ||
2915                 error "file $testfile is corrupted on server (2)"
2916
2917         rm -f $testfile $tmpfile
2918         cancel_lru_locks osc ; cancel_lru_locks mdc
2919
2920         # write a few bytes in file, at end of first page
2921         echo "abc" > $tmpfile
2922         $LFS setstripe -c1 -i0 $testfile
2923         seek=$(getconf PAGESIZE)
2924         seek=$((seek - 4))
2925         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2926                 conv=fsync,notrunc
2927
2928         # write a few other bytes at beginning of first page
2929         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
2930
2931         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2932                 conv=fsync,notrunc
2933
2934         # check that in-memory representation of file is correct
2935         cmp -bl $tmpfile $testfile ||
2936                 error "file $testfile is corrupted in memory (3)"
2937
2938         cancel_lru_locks osc ; cancel_lru_locks mdc
2939
2940         # check that file read from server is correct
2941         cmp -bl $tmpfile $testfile ||
2942                 error "file $testfile is corrupted on server (3)"
2943
2944         rm -f $testfile $tmpfile
2945         cancel_lru_locks osc ; cancel_lru_locks mdc
2946
2947         # write a few bytes in file, at beginning of second page
2948         echo "abc" > $tmpfile
2949         $LFS setstripe -c1 -i0 $testfile
2950         seek=$(getconf PAGESIZE)
2951         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2952                 conv=fsync,notrunc
2953         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
2954                 conv=fsync,notrunc
2955
2956         # write a few other bytes at end of first page
2957         seek=$((seek - 4))
2958         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
2959                 conv=fsync,notrunc
2960         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
2961                 conv=fsync,notrunc
2962
2963         # check that in-memory representation of file is correct
2964         cmp -bl $tmpfile2 $testfile ||
2965                 error "file $testfile is corrupted in memory (4)"
2966
2967         cancel_lru_locks osc ; cancel_lru_locks mdc
2968
2969         # check that file read from server is correct
2970         cmp -bl $tmpfile2 $testfile ||
2971                 error "file $testfile is corrupted on server (4)"
2972
2973         rm -f $testfile $tmpfile $tmpfile2
2974         cancel_lru_locks osc ; cancel_lru_locks mdc
2975
2976         # write a few bytes in file, at beginning of first stripe
2977         echo "abc" > $tmpfile
2978         $LFS setstripe -S 256k -c2 $testfile
2979         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
2980
2981         # write a few other bytes, at beginning of second stripe
2982         dd if=$tmpfile of=$testfile bs=4 count=1 seek=262144 oflag=seek_bytes \
2983                 conv=fsync,notrunc
2984         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=262144 oflag=seek_bytes \
2985                 conv=fsync,notrunc
2986
2987         # check that in-memory representation of file is correct
2988         cmp -bl $tmpfile $testfile ||
2989                 error "file $testfile is corrupted in memory (5)"
2990
2991         cancel_lru_locks osc ; cancel_lru_locks mdc
2992
2993         # check that file read from server is correct
2994         cmp -bl $tmpfile $testfile ||
2995                 error "file $testfile is corrupted on server (5)"
2996
2997         rm -f $tmpfile
2998 }
2999 run_test 40 "exercise size of encrypted file"
3000
3001 test_41() {
3002         local testfile=$DIR/$tdir/$tfile
3003         local tmpfile=$TMP/abc
3004         local tmpfile2=$TMP/abc2
3005         local seek
3006
3007         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3008                 skip "client encryption not supported"
3009
3010         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3011                 skip "need dummy encryption support"
3012
3013         stack_trap cleanup_for_enc_tests EXIT
3014         setup_for_enc_tests
3015
3016         echo "abc" > $tmpfile
3017         seek=$(getconf PAGESIZE)
3018         seek=$((seek - 204))
3019         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3020                 conv=fsync
3021         seek=$(getconf PAGESIZE)
3022         seek=$((seek + 1092))
3023         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3024                 conv=fsync,notrunc
3025
3026         # write a few bytes in file
3027         $LFS setstripe -c1 -i0 -S 256k $testfile
3028         seek=$(getconf PAGESIZE)
3029         seek=$((seek - 204))
3030         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3031         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3032         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3033                 conv=fsync &
3034
3035         sleep 5
3036         # write a few other bytes, at a different offset
3037         seek=$(getconf PAGESIZE)
3038         seek=$((seek + 1092))
3039         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3040                 conv=fsync,notrunc &
3041         wait
3042         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3043
3044         # check that in-memory representation of file is correct
3045         cmp -bl $tmpfile2 $testfile ||
3046                 error "file $testfile is corrupted in memory (1)"
3047
3048         cancel_lru_locks osc ; cancel_lru_locks mdc
3049
3050         # check that file read from server is correct
3051         cmp -bl $tmpfile2 $testfile ||
3052                 error "file $testfile is corrupted on server (1)"
3053
3054         rm -f $tmpfile $tmpfile2
3055 }
3056 run_test 41 "test race on encrypted file size (1)"
3057
3058 test_42() {
3059         local testfile=$DIR/$tdir/$tfile
3060         local testfile2=$DIR2/$tdir/$tfile
3061         local tmpfile=$TMP/abc
3062         local tmpfile2=$TMP/abc2
3063         local pagesz=$(getconf PAGESIZE)
3064         local seek
3065
3066         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3067                 skip "client encryption not supported"
3068
3069         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3070                 skip "need dummy encryption support"
3071
3072         stack_trap cleanup_for_enc_tests EXIT
3073         setup_for_enc_tests
3074
3075         if is_mounted $MOUNT2; then
3076                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3077         fi
3078         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
3079                 error "mount2 with '-o test_dummy_encryption' failed"
3080
3081         # create file by writting one whole page
3082         $LFS setstripe -c1 -i0 -S 256k $testfile
3083         dd if=/dev/zero of=$testfile bs=$pagesz count=1 conv=fsync
3084
3085         # read file from 2nd mount point
3086         cat $testfile2 > /dev/null
3087
3088         echo "abc" > $tmpfile
3089         dd if=/dev/zero of=$tmpfile2 bs=$pagesz count=1 conv=fsync
3090         seek=$((2*pagesz - 204))
3091         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3092                 conv=fsync,notrunc
3093         seek=$((2*pagesz + 1092))
3094         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3095                 conv=fsync,notrunc
3096
3097         # write a few bytes in file from 1st mount point
3098         seek=$((2*pagesz - 204))
3099         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3100         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3101         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3102                 conv=fsync,notrunc &
3103
3104         sleep 5
3105         # write a few other bytes, at a different offset from 2nd mount point
3106         seek=$((2*pagesz + 1092))
3107         dd if=$tmpfile of=$testfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3108                 conv=fsync,notrunc &
3109         wait
3110         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3111
3112         # check that in-memory representation of file is correct
3113         cmp -bl $tmpfile2 $testfile ||
3114                 error "file $testfile is corrupted in memory (1)"
3115
3116         # check that in-memory representation of file is correct
3117         cmp -bl $tmpfile2 $testfile2 ||
3118                 error "file $testfile is corrupted in memory (2)"
3119
3120         cancel_lru_locks osc ; cancel_lru_locks mdc
3121
3122         # check that file read from server is correct
3123         cmp -bl $tmpfile2 $testfile ||
3124                 error "file $testfile is corrupted on server (1)"
3125
3126         rm -f $tmpfile $tmpfile2
3127 }
3128 run_test 42 "test race on encrypted file size (2)"
3129
3130 test_43() {
3131         local testfile=$DIR/$tdir/$tfile
3132         local testfile2=$DIR2/$tdir/$tfile
3133         local tmpfile=$TMP/abc
3134         local tmpfile2=$TMP/abc2
3135         local resfile=$TMP/res
3136         local pagesz=$(getconf PAGESIZE)
3137         local seek
3138
3139         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3140                 skip "client encryption not supported"
3141
3142         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3143                 skip "need dummy encryption support"
3144
3145         stack_trap cleanup_for_enc_tests EXIT
3146         setup_for_enc_tests
3147
3148         if is_mounted $MOUNT2; then
3149                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3150         fi
3151         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
3152                 error "mount2 with '-o test_dummy_encryption' failed"
3153
3154         # create file
3155         tr '\0' '1' < /dev/zero |
3156                 dd of=$tmpfile bs=$pagesz count=1 conv=fsync
3157         $LFS setstripe -c1 -i0 -S 256k $testfile
3158         cp $tmpfile $testfile
3159
3160         # read file from 2nd mount point
3161         cat $testfile2 > /dev/null
3162
3163         # write a few bytes in file from 1st mount point
3164         echo "abc" > $tmpfile2
3165         seek=$((2*pagesz - 204))
3166         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
3167         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
3168         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3169                 conv=fsync,notrunc &
3170
3171         sleep 5
3172         # read file from 2nd mount point
3173         dd if=$testfile2 of=$resfile bs=$pagesz count=1 conv=fsync,notrunc
3174         cmp -bl $tmpfile $resfile ||
3175                 error "file $testfile is corrupted in memory (1)"
3176
3177         wait
3178         do_facet ost1 "$LCTL set_param fail_loc=0x0"
3179
3180         # check that in-memory representation of file is correct
3181         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3182                 conv=fsync,notrunc
3183         cmp -bl $tmpfile $testfile2 ||
3184                 error "file $testfile is corrupted in memory (2)"
3185
3186         cancel_lru_locks osc ; cancel_lru_locks mdc
3187
3188         # check that file read from server is correct
3189         cmp -bl $tmpfile $testfile ||
3190                 error "file $testfile is corrupted on server (1)"
3191
3192         rm -f $tmpfile $tmpfile2
3193 }
3194 run_test 43 "test race on encrypted file size (3)"
3195
3196 test_44() {
3197         local testfile=$DIR/$tdir/$tfile
3198         local tmpfile=$TMP/abc
3199         local resfile=$TMP/resfile
3200         local respage
3201
3202         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3203                 skip "client encryption not supported"
3204
3205         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3206                 skip "need dummy encryption support"
3207
3208         which vmtouch || skip "This test needs vmtouch utility"
3209
3210         # Direct I/O is now supported on encrypted files.
3211
3212         stack_trap cleanup_for_enc_tests EXIT
3213         setup_for_enc_tests
3214
3215         $LFS setstripe -c1 -i0 $testfile
3216         dd if=/dev/urandom of=$tmpfile bs=8192 count=1 conv=fsync
3217         dd if=$tmpfile of=$testfile bs=8192 count=1 oflag=direct ||
3218                 error "could not write to file with O_DIRECT (1)"
3219
3220         respage=$(vmtouch $testfile | awk '/Resident\ Pages:/ {print $3}')
3221         [ "$respage" == "0/2" ] ||
3222                 error "write to enc file fell back to buffered IO"
3223
3224         cancel_lru_locks
3225
3226         dd if=$testfile of=$resfile bs=8192 count=1 iflag=direct ||
3227                 error "could not read from file with O_DIRECT (1)"
3228
3229         respage=$(vmtouch $testfile | awk '/Resident\ Pages:/ {print $3}')
3230         [ "$respage" == "0/2" ] ||
3231                 error "read from enc file fell back to buffered IO"
3232
3233         cmp -bl $tmpfile $resfile ||
3234                 error "file $testfile is corrupted (1)"
3235
3236         rm -f $resfile
3237
3238         $TRUNCATE $tmpfile 4096
3239         dd if=$tmpfile of=$testfile bs=4096 count=1 seek=13 oflag=direct ||
3240                 error "could not write to file with O_DIRECT (2)"
3241
3242         cancel_lru_locks
3243
3244         dd if=$testfile of=$resfile bs=4096 count=1 skip=13 iflag=direct ||
3245                 error "could not read from file with O_DIRECT (2)"
3246         cmp -bl $tmpfile $resfile ||
3247                 error "file $testfile is corrupted (2)"
3248
3249         rm -f $testfile $resfile
3250         $LFS setstripe -c1 -i0 $testfile
3251
3252         $TRUNCATE $tmpfile 2043
3253         cp $tmpfile $testfile
3254
3255         cancel_lru_locks
3256
3257         dd if=$testfile of=$resfile bs=4096 count=1 iflag=direct ||
3258                 error "could not read from file with O_DIRECT (3)"
3259         cmp -bl $tmpfile $resfile ||
3260                 error "file $testfile is corrupted (3)"
3261
3262         rm -f $tmpfile $resfile
3263 }
3264 run_test 44 "encrypted file access semantics: direct IO"
3265
3266 test_45() {
3267         local testfile=$DIR/$tdir/$tfile
3268         local tmpfile=$TMP/junk
3269
3270         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3271                 skip "client encryption not supported"
3272
3273         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3274                 skip "need dummy encryption support"
3275
3276         stack_trap cleanup_for_enc_tests EXIT
3277         setup_for_enc_tests
3278
3279         $LFS setstripe -c1 -i0 $testfile
3280         dd if=/dev/zero of=$testfile bs=512K count=1
3281         $MULTIOP $testfile OSMRUc || error "$MULTIOP $testfile failed (1)"
3282         $MULTIOP $testfile OSMWUc || error "$MULTIOP $testfile failed (2)"
3283
3284         dd if=/dev/zero of=$tmpfile bs=512K count=1
3285         $MULTIOP $tmpfile OSMWUc || error "$MULTIOP $tmpfile failed"
3286         $MMAP_CAT $tmpfile > ${tmpfile}2
3287
3288         cancel_lru_locks osc ; cancel_lru_locks mdc
3289
3290         $MMAP_CAT $testfile > ${testfile}2
3291         cmp -bl ${tmpfile}2 ${testfile}2 ||
3292                 error "file $testfile is corrupted"
3293
3294         rm -f $tmpfile ${tmpfile}2
3295 }
3296 run_test 45 "encrypted file access semantics: MMAP"
3297
3298 test_46() {
3299         local testdir=$DIR/$tdir/mydir
3300         local testfile=$testdir/myfile
3301         local lsfile=$TMP/lsfile
3302         local scrambleddir
3303         local scrambledfile
3304
3305         local testfile2=$DIR/$tdir/${tfile}.2
3306         local tmpfile=$DIR/junk
3307
3308         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3309                 skip "client encryption not supported"
3310
3311         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3312                 skip "need dummy encryption support"
3313
3314         stack_trap cleanup_for_enc_tests EXIT
3315         setup_for_enc_tests
3316
3317         touch $DIR/onefile
3318         touch $DIR/$tdir/$tfile
3319         mkdir $testdir
3320         echo test > $testfile
3321         sync ; echo 3 > /proc/sys/vm/drop_caches
3322
3323         # remove fscrypt key from keyring
3324         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3325         keyctl reap
3326
3327         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
3328         ls -1 $scrambleddir > $lsfile || error "ls $testdir failed"
3329
3330         scrambledfile=$scrambleddir/$(head -n 1 $lsfile)
3331         stat $scrambledfile || error "stat $scrambledfile failed"
3332         rm -f $lsfile
3333
3334         cat $scrambledfile && error "cat $scrambledfile should have failed"
3335
3336         touch $scrambleddir/otherfile &&
3337                 error "touch otherfile should have failed"
3338         ls $scrambleddir/otherfile && error "otherfile should not exist"
3339         mkdir $scrambleddir/otherdir &&
3340                 error "mkdir otherdir should have failed"
3341         ls -d $scrambleddir/otherdir && error "otherdir should not exist"
3342
3343         rm -f $scrambledfile || error "rm $scrambledfile failed"
3344         rmdir $scrambleddir || error "rmdir $scrambleddir failed"
3345
3346         rm -f $DIR/onefile
3347 }
3348 run_test 46 "encrypted file access semantics without key"
3349
3350 test_47() {
3351         local testfile=$DIR/$tdir/$tfile
3352         local testfile2=$DIR/$tdir/${tfile}.2
3353         local tmpfile=$DIR/junk
3354         local scrambleddir
3355         local scrambledfile
3356
3357         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3358                 skip "client encryption not supported"
3359
3360         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3361                 skip "need dummy encryption support"
3362
3363         stack_trap cleanup_for_enc_tests EXIT
3364         setup_for_enc_tests
3365
3366         dd if=/dev/zero of=$tmpfile bs=512K count=1
3367         mrename $tmpfile $testfile &&
3368                 error "rename from unencrypted to encrypted dir should fail"
3369
3370         ln $tmpfile $testfile &&
3371                 error "link from unencrypted to encrypted dir should fail"
3372
3373         cp $tmpfile $testfile ||
3374                 error "cp from unencrypted to encrypted dir should succeed"
3375         rm -f $tmpfile
3376
3377         mrename $testfile $testfile2 ||
3378                 error "rename from within encrypted dir should succeed"
3379
3380         ln $testfile2 $testfile ||
3381                 error "link from within encrypted dir should succeed"
3382         rm -f $testfile
3383
3384         ln $testfile2 $tmpfile ||
3385                 error "link from encrypted to unencrypted dir should succeed"
3386         rm -f $tmpfile
3387
3388         mrename $testfile2 $tmpfile ||
3389                 error "rename from encrypted to unencrypted dir should succeed"
3390
3391         dd if=/dev/zero of=$testfile bs=512K count=1
3392         mkdir $DIR/$tdir/mydir
3393         sync ; echo 3 > /proc/sys/vm/drop_caches
3394
3395         # remove fscrypt key from keyring
3396         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3397         keyctl reap
3398
3399         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
3400         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
3401         ln $scrambledfile $scrambleddir/linkfile &&
3402                 error "ln linkfile should have failed"
3403         mrename $scrambledfile $DIR/onefile2 &&
3404                 error "mrename from $scrambledfile should have failed"
3405         touch $DIR/onefile
3406         mrename $DIR/onefile $scrambleddir/otherfile &&
3407                 error "mrename to $scrambleddir should have failed"
3408
3409         rm -f $tmpfile $DIR/onefile
3410 }
3411 run_test 47 "encrypted file access semantics: rename/link"
3412
3413 test_48a() {
3414         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
3415         local testfile=$DIR/$tdir/$tfile
3416         local tmpfile=$TMP/111
3417         local tmpfile2=$TMP/abc
3418         local pagesz=$(getconf PAGESIZE)
3419         local sz
3420         local seek
3421         local scrambledfile
3422
3423         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3424                 skip "client encryption not supported"
3425
3426         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3427                 skip "need dummy encryption support"
3428
3429         stack_trap cleanup_for_enc_tests EXIT
3430         setup_for_enc_tests
3431
3432         # create file, 4 x PAGE_SIZE long
3433         tr '\0' '1' < /dev/zero |
3434                 dd of=$tmpfile bs=4x$pagesz count=1 conv=fsync
3435         $LFS setstripe -c1 -i0 $testfile
3436         cp $tmpfile $testfile
3437         echo "abc" > $tmpfile2
3438
3439         # decrease size: truncate to PAGE_SIZE
3440         $TRUNCATE $tmpfile $pagesz
3441         $TRUNCATE $testfile $pagesz
3442         cancel_lru_locks osc ; cancel_lru_locks mdc
3443         cmp -bl $tmpfile $testfile ||
3444                 error "file $testfile is corrupted (1)"
3445
3446         # increase size: truncate to 2 x PAGE_SIZE
3447         sz=$((pagesz*2))
3448         $TRUNCATE $tmpfile $sz
3449         $TRUNCATE $testfile $sz
3450         cancel_lru_locks osc ; cancel_lru_locks mdc
3451         cmp -bl $tmpfile $testfile ||
3452                 error "file $testfile is corrupted (2)"
3453
3454         # write in 2nd page
3455         seek=$((pagesz+100))
3456         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3457                 conv=fsync,notrunc
3458         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3459                 conv=fsync,notrunc
3460         cancel_lru_locks osc ; cancel_lru_locks mdc
3461         cmp -bl $tmpfile $testfile ||
3462                 error "file $testfile is corrupted (3)"
3463
3464         # truncate to PAGE_SIZE / 2
3465         sz=$((pagesz/2))
3466         $TRUNCATE $tmpfile $sz
3467         $TRUNCATE $testfile $sz
3468         cancel_lru_locks osc ; cancel_lru_locks mdc
3469         cmp -bl $tmpfile $testfile ||
3470                 error "file $testfile is corrupted (4)"
3471
3472         # lockless truncate should be turned into regular truncate for enc file
3473         save_lustre_params client "osc.*.lockless_truncate" > $save
3474         # restore lockless_truncate default values on exit
3475         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
3476         cancel_lru_locks osc ; cancel_lru_locks mdc
3477         lctl set_param -n osc.*.lockless_truncate 1
3478         cancel_lru_locks osc
3479         clear_stats osc.*.osc_stats
3480         $TRUNCATE $testfile 8000000 || error "truncate failed (1)"
3481         [ $(calc_stats osc.*.osc_stats lockless_truncate) -eq 0 ] ||
3482                 error "lockless truncate should be turned into regular truncate"
3483         lctl set_param -n osc.*.lockless_truncate 0
3484
3485         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
3486         sz=$((sz-7))
3487         $TRUNCATE $tmpfile $sz
3488         $TRUNCATE $testfile $sz
3489         cancel_lru_locks osc ; cancel_lru_locks mdc
3490         cmp -bl $tmpfile $testfile ||
3491                 error "file $testfile is corrupted (5)"
3492
3493         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
3494         sz=$((sz+18))
3495         $TRUNCATE $tmpfile $sz
3496         $TRUNCATE $testfile $sz
3497         cancel_lru_locks osc ; cancel_lru_locks mdc
3498         cmp -bl $tmpfile $testfile ||
3499                 error "file $testfile is corrupted (6)"
3500
3501         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
3502         sz=$((sz+pagesz+30))
3503         $TRUNCATE $tmpfile $sz
3504         $TRUNCATE $testfile $sz
3505         cancel_lru_locks osc ; cancel_lru_locks mdc
3506         cmp -bl $tmpfile $testfile ||
3507                 error "file $testfile is corrupted (7)"
3508
3509         sync ; echo 3 > /proc/sys/vm/drop_caches
3510
3511         # remove fscrypt key from keyring
3512         keyctl revoke $(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3513         keyctl reap
3514
3515         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
3516         $TRUNCATE $scrambledfile 0 &&
3517                 error "truncate $scrambledfile should have failed without key"
3518
3519         rm -f $tmpfile $tmpfile2
3520 }
3521 run_test 48a "encrypted file access semantics: truncate"
3522
3523 cleanup_for_enc_tests_othercli() {
3524         local othercli=$1
3525
3526         # remount othercli normally
3527         zconf_umount $othercli $MOUNT ||
3528                 error "umount $othercli $MOUNT failed"
3529         zconf_mount $othercli $MOUNT ||
3530                 error "remount $othercli $MOUNT failed"
3531 }
3532
3533 test_48b() {
3534         local othercli
3535
3536         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3537                 skip "client encryption not supported"
3538
3539         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3540                 skip "need dummy encryption support"
3541
3542         [ "$num_clients" -ge 2 ] || skip "Need at least 2 clients"
3543
3544         if [ "$HOSTNAME" == ${clients_arr[0]} ]; then
3545                 othercli=${clients_arr[1]}
3546         else
3547                 othercli=${clients_arr[0]}
3548         fi
3549
3550         stack_trap cleanup_for_enc_tests EXIT
3551         stack_trap "cleanup_for_enc_tests_othercli $othercli" EXIT
3552         setup_for_enc_tests
3553         zconf_umount $othercli $MOUNT ||
3554                 error "umount $othercli $MOUNT failed"
3555
3556         cp /bin/sleep $DIR/$tdir/
3557         cancel_lru_locks osc ; cancel_lru_locks mdc
3558         $DIR/$tdir/sleep 30 &
3559         # mount and IOs must be done in the same shell session, otherwise
3560         # encryption key in session keyring is missing
3561         do_node $othercli "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
3562                            $MGSNID:/$FSNAME $MOUNT && \
3563                            $TRUNCATE $DIR/$tdir/sleep 7"
3564         wait || error "wait error"
3565         cmp --silent /bin/sleep $DIR/$tdir/sleep ||
3566                 error "/bin/sleep and $DIR/$tdir/sleep differ"
3567 }
3568 run_test 48b "encrypted file: concurrent truncate"
3569
3570 trace_cmd() {
3571         local cmd="$@"
3572         local xattr_name="security.c"
3573
3574         cancel_lru_locks osc ; cancel_lru_locks mdc
3575         $LCTL set_param debug=+info
3576         $LCTL clear
3577
3578         echo $cmd
3579         eval $cmd
3580
3581         $LCTL dk | grep -E "get xattr '${xattr_name}'|get xattrs"
3582         [ $? -ne 0 ] || error "get xattr event was triggered"
3583 }
3584
3585 test_49() {
3586         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3587                 skip "client encryption not supported"
3588
3589         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3590                 skip "need dummy encryption support"
3591
3592         stack_trap cleanup_for_enc_tests EXIT
3593         setup_for_enc_tests
3594
3595         local dirname=$DIR/$tdir/subdir
3596
3597         mkdir $dirname
3598
3599         trace_cmd stat $dirname
3600         trace_cmd touch $dirname/f1
3601         trace_cmd stat $dirname/f1
3602         trace_cmd cat $dirname/f1
3603         dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
3604         trace_cmd $TRUNCATE $dirname/f1 10240
3605         trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
3606         trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
3607         trace_cmd $LFS setdirstripe -i 1 $dirname/d2
3608         trace_cmd $LFS migrate -m 0 $dirname/d2
3609
3610         $LFS setdirstripe -i 1 -c 1 $dirname/d3
3611         dirname=$dirname/d3/subdir
3612         mkdir $dirname
3613
3614         trace_cmd stat $dirname
3615         trace_cmd touch $dirname/f1
3616         trace_cmd stat $dirname/f1
3617         trace_cmd cat $dirname/f1
3618         dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
3619         trace_cmd $TRUNCATE $dirname/f1 10240
3620         trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
3621         trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
3622 }
3623 run_test 49 "Avoid getxattr for encryption context"
3624
3625 test_50() {
3626         local testfile=$DIR/$tdir/$tfile
3627         local tmpfile=$TMP/abc
3628         local pagesz=$(getconf PAGESIZE)
3629         local sz
3630
3631         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3632                 skip "client encryption not supported"
3633
3634         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3635                 skip "need dummy encryption support"
3636
3637         stack_trap cleanup_for_enc_tests EXIT
3638         setup_for_enc_tests
3639
3640         # write small file, data on MDT only
3641         tr '\0' '1' < /dev/zero |
3642             dd of=$tmpfile bs=1 count=5000 conv=fsync
3643         $LFS setstripe -E 1M -L mdt -E EOF $testfile
3644         cp $tmpfile $testfile
3645
3646         # check that in-memory representation of file is correct
3647         cmp -bl $tmpfile $testfile ||
3648                 error "file $testfile is corrupted in memory"
3649
3650         cancel_lru_locks osc ; cancel_lru_locks mdc
3651
3652         # check that file read from server is correct
3653         cmp -bl $tmpfile $testfile ||
3654                 error "file $testfile is corrupted on server"
3655
3656         # decrease size: truncate to PAGE_SIZE
3657         $TRUNCATE $tmpfile $pagesz
3658         $TRUNCATE $testfile $pagesz
3659         cancel_lru_locks osc ; cancel_lru_locks mdc
3660         cmp -bl $tmpfile $testfile ||
3661                 error "file $testfile is corrupted (1)"
3662
3663         # increase size: truncate to 2 x PAGE_SIZE
3664         sz=$((pagesz*2))
3665         $TRUNCATE $tmpfile $sz
3666         $TRUNCATE $testfile $sz
3667         cancel_lru_locks osc ; cancel_lru_locks mdc
3668         cmp -bl $tmpfile $testfile ||
3669                 error "file $testfile is corrupted (2)"
3670
3671         # truncate to PAGE_SIZE / 2
3672         sz=$((pagesz/2))
3673         $TRUNCATE $tmpfile $sz
3674         $TRUNCATE $testfile $sz
3675         cancel_lru_locks osc ; cancel_lru_locks mdc
3676         cmp -bl $tmpfile $testfile ||
3677                 error "file $testfile is corrupted (3)"
3678
3679         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
3680         sz=$((sz-7))
3681         $TRUNCATE $tmpfile $sz
3682         $TRUNCATE $testfile $sz
3683         cancel_lru_locks osc ; cancel_lru_locks mdc
3684         cmp -bl $tmpfile $testfile ||
3685                 error "file $testfile is corrupted (4)"
3686
3687         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
3688         sz=$((sz+18))
3689         $TRUNCATE $tmpfile $sz
3690         $TRUNCATE $testfile $sz
3691         cancel_lru_locks osc ; cancel_lru_locks mdc
3692         cmp -bl $tmpfile $testfile ||
3693                 error "file $testfile is corrupted (5)"
3694
3695         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
3696         sz=$((sz+pagesz+30))
3697         $TRUNCATE $tmpfile $sz
3698         $TRUNCATE $testfile $sz
3699         cancel_lru_locks osc ; cancel_lru_locks mdc
3700         cmp -bl $tmpfile $testfile ||
3701                 error "file $testfile is corrupted (6)"
3702
3703         rm -f $testfile
3704         cancel_lru_locks osc ; cancel_lru_locks mdc
3705
3706         # write hole in file, data spread on MDT and OST
3707         tr '\0' '2' < /dev/zero |
3708             dd of=$tmpfile bs=1 count=1539 seek=1539074 conv=fsync,notrunc
3709         $LFS setstripe -E 1M -L mdt -E EOF $testfile
3710         cp --sparse=always $tmpfile $testfile
3711
3712         # check that in-memory representation of file is correct
3713         cmp -bl $tmpfile $testfile ||
3714                 error "file $testfile is corrupted in memory"
3715
3716         cancel_lru_locks osc ; cancel_lru_locks mdc
3717
3718         # check that file read from server is correct
3719         cmp -bl $tmpfile $testfile ||
3720                 error "file $testfile is corrupted on server"
3721
3722         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
3723         # inside OST part of data
3724         sz=$((1024*1024+13))
3725         $TRUNCATE $tmpfile $sz
3726         $TRUNCATE $testfile $sz
3727         cancel_lru_locks osc ; cancel_lru_locks mdc
3728         cmp -bl $tmpfile $testfile ||
3729                 error "file $testfile is corrupted (7)"
3730
3731         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
3732         # inside MDT part of data
3733         sz=7
3734         $TRUNCATE $tmpfile $sz
3735         $TRUNCATE $testfile $sz
3736         cancel_lru_locks osc ; cancel_lru_locks mdc
3737         cmp -bl $tmpfile $testfile ||
3738                 error "file $testfile is corrupted (8)"
3739
3740         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
3741         # inside MDT part of data
3742         sz=$((1024*1024-13))
3743         $TRUNCATE $tmpfile $sz
3744         $TRUNCATE $testfile $sz
3745         cancel_lru_locks osc ; cancel_lru_locks mdc
3746         cmp -bl $tmpfile $testfile ||
3747                 error "file $testfile is corrupted (9)"
3748
3749         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
3750         # inside OST part of data
3751         sz=$((1024*1024+7))
3752         $TRUNCATE $tmpfile $sz
3753         $TRUNCATE $testfile $sz
3754         cancel_lru_locks osc ; cancel_lru_locks mdc
3755         cmp -bl $tmpfile $testfile ||
3756                 error "file $testfile is corrupted (10)"
3757
3758         rm -f $tmpfile
3759 }
3760 run_test 50 "DoM encrypted file"
3761
3762 test_51() {
3763         [ "$MDS1_VERSION" -gt $(version_code 2.13.53) ] ||
3764                 skip "Need MDS version at least 2.13.53"
3765
3766         mkdir $DIR/$tdir || error "mkdir $tdir"
3767
3768         touch $DIR/$tdir/$tfile || error "touch $tfile"
3769         cp $(which chown) $DIR/$tdir || error "cp chown"
3770         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile &&
3771                 error "chown $tfile should fail"
3772         setcap 'CAP_CHOWN=ep' $DIR/$tdir/chown || error "setcap CAP_CHOWN"
3773         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile ||
3774                 error "chown $tfile"
3775         rm $DIR/$tdir/$tfile || error "rm $tfile"
3776
3777         touch $DIR/$tdir/$tfile || error "touch $tfile"
3778         cp $(which touch) $DIR/$tdir || error "cp touch"
3779         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile &&
3780                 error "touch should fail"
3781         setcap 'CAP_FOWNER=ep' $DIR/$tdir/touch || error "setcap CAP_FOWNER"
3782         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile ||
3783                 error "touch $tfile"
3784         rm $DIR/$tdir/$tfile || error "rm $tfile"
3785
3786         local cap
3787         for cap in "CAP_DAC_OVERRIDE" "CAP_DAC_READ_SEARCH"; do
3788                 touch $DIR/$tdir/$tfile || error "touch $tfile"
3789                 chmod 600 $DIR/$tdir/$tfile || error "chmod $tfile"
3790                 cp $(which cat) $DIR/$tdir || error "cp cat"
3791                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile &&
3792                         error "cat should fail"
3793                 setcap $cap=ep $DIR/$tdir/cat || error "setcap $cap"
3794                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile ||
3795                         error "cat $tfile"
3796                 rm $DIR/$tdir/$tfile || error "rm $tfile"
3797         done
3798 }
3799 run_test 51 "FS capabilities ==============="
3800
3801 test_52() {
3802         local testfile=$DIR/$tdir/$tfile
3803         local tmpfile=$TMP/$tfile
3804         local mirror1=$TMP/$tfile.mirror1
3805         local mirror2=$TMP/$tfile.mirror2
3806
3807         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3808                 skip "client encryption not supported"
3809
3810         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3811                 skip "need dummy encryption support"
3812
3813         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
3814
3815         stack_trap cleanup_for_enc_tests EXIT
3816         setup_for_enc_tests
3817
3818         dd if=/dev/urandom of=$tmpfile bs=5000 count=1 conv=fsync
3819
3820         $LFS mirror create -N -i0 -N -i1 $testfile ||
3821                 error "could not create mirror"
3822
3823         dd if=$tmpfile of=$testfile bs=5000 count=1 conv=fsync ||
3824                 error "could not write to $testfile"
3825
3826         $LFS mirror resync $testfile ||
3827                 error "could not resync mirror"
3828
3829         $LFS mirror verify -v $testfile ||
3830                 error "verify mirror failed"
3831
3832         $LFS mirror read -N 1 -o $mirror1 $testfile ||
3833                 error "could not read from mirror 1"
3834
3835         cmp -bl $tmpfile $mirror1 ||
3836                 error "mirror 1 is corrupted"
3837
3838         $LFS mirror read -N 2 -o $mirror2 $testfile ||
3839                 error "could not read from mirror 2"
3840
3841         cmp -bl $tmpfile $mirror2 ||
3842                 error "mirror 2 is corrupted"
3843
3844         tr '\0' '2' < /dev/zero |
3845             dd of=$tmpfile bs=9000 count=1 conv=fsync
3846
3847         $LFS mirror write -N 1 -i $tmpfile $testfile ||
3848                 error "could not write to mirror 1"
3849
3850         $LFS mirror verify -v $testfile &&
3851                 error "mirrors should be different"
3852
3853         rm -f $tmpfile $mirror1 $mirror2
3854 }
3855 run_test 52 "Mirrored encrypted file"
3856
3857 log "cleanup: ======================================================"
3858
3859 sec_unsetup() {
3860         for num in $(seq $MDSCOUNT); do
3861                 if [ "${identity_old[$num]}" = 1 ]; then
3862                         switch_identity $num false || identity_old[$num]=$?
3863                 fi
3864         done
3865
3866         $RUNAS_CMD -u $ID0 ls $DIR
3867         $RUNAS_CMD -u $ID1 ls $DIR
3868 }
3869 sec_unsetup
3870
3871 complete $SECONDS
3872 check_and_cleanup_lustre
3873 exit_status