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