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