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