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