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