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