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