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