Whamcloud - gitweb
LU-17814 utils: implement real pfind
[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
19 [[ "$SLOW" == "no" ]] && EXCEPT_SLOW="26"
20
21 NODEMAP_TESTS=$(seq 7 26)
22
23 if ! check_versions; then
24         echo "It is NOT necessary to test nodemap under interoperation mode"
25         EXCEPT="$EXCEPT $NODEMAP_TESTS"
26 fi
27
28 build_test_filter
29
30 RUNAS_CMD=${RUNAS_CMD:-runas}
31
32 WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
33
34 CONFDIR=/etc/lustre
35 PERM_CONF=$CONFDIR/perm.conf
36 FAIL_ON_ERROR=false
37 HOSTNAME_CHECKSUM=$(hostname | sum | awk '{ print $1 }')
38 SUBNET_CHECKSUM=$(expr $HOSTNAME_CHECKSUM % 250 + 1)
39
40 require_dsh_mds || exit 0
41 require_dsh_ost || exit 0
42
43 clients=${CLIENTS//,/ }
44 num_clients=$(get_node_count ${clients})
45 clients_arr=($clients)
46
47 echo "was USER0=$(getent passwd | grep :${ID0:-500}:)"
48 echo "was USER1=$(getent passwd | grep :${ID1:-501}:)"
49
50 ID0=$(id -u $USER0)
51 ID1=$(id -u $USER1)
52
53 echo "now USER0=$USER0=$ID0:$(id -g $USER0), USER1=$USER1=$ID1:$(id -g $USER1)"
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=$(mdtname_from_index 0 $MOUNT)
97 [[ -z "$MDT" ]] && error "fail to get MDT0000 device name" && exit 1
98 do_facet $SINGLEMDS "mkdir -p $CONFDIR"
99 IDENTITY_FLUSH=mdt.$MDT.identity_flush
100
101 SAVE_PWD=$PWD
102
103 if (( $MDS1_VERSION >= $(version_code 2.16.51) )); then
104         nodemap_activate="nodemap activate"
105         nodemap_add="nodemap add"
106         nodemap_del="nodemap del"
107         nodemap_info="nodemap info"
108         nodemap_modify="nodemap modify"
109         nodemap_add_range="nodemap add_range"
110         nodemap_del_range="nodemap del_range"
111         nodemap_add_idmap="nodemap add_idmap"
112         nodemap_del_idmap="nodemap del_idmap"
113         nodemap_test_nid="nodemap test_nid"
114         nodemap_test_id="nodemap test_id"
115         nodemap_set_fileset="nodemap set_fileset"
116         nodemap_set_sepol="nodemap set_sepol"
117 else
118         nodemap_activate="nodemap_activate"
119         nodemap_add="nodemap_add"
120         nodemap_del="nodemap_del"
121         nodemap_info="nodemap_info"
122         nodemap_modify="nodemap_modify"
123         nodemap_add_range="nodemap_add_range"
124         nodemap_del_range="nodemap_del_range"
125         nodemap_add_idmap="nodemap_add_idmap"
126         nodemap_del_idmap="nodemap_del_idmap"
127         nodemap_test_nid="nodemap_test_nid"
128         nodemap_test_id="nodemap_test_id"
129         nodemap_set_fileset="nodemap_set_fileset"
130         nodemap_set_sepol="nodemap_set_sepol"
131 fi
132
133 sec_login() {
134         local user=$1
135         local group=$2
136
137         $GSS_KRB5 || return
138         if ! $RUNAS_CMD -u $user krb5_login.sh; then
139                 error "$user login kerberos failed."
140                 exit 1
141         fi
142
143         if ! $RUNAS_CMD -u $user -g $group ls $DIR > /dev/null 2>&1; then
144                 $RUNAS_CMD -u $user lfs flushctx -k
145                 $RUNAS_CMD -u $user krb5_login.sh
146                 if ! $RUNAS_CMD -u$user -g$group ls $DIR > /dev/null 2>&1; then
147                         error "init $user $group failed."
148                         exit 2
149                 fi
150         fi
151 }
152
153 declare -a identity_old
154
155 wait_ssk() {
156         # wait for SSK flavor to be applied if necessary
157         if $GSS_SK; then
158                 if $SK_S2S; then
159                         wait_flavor all2all $SK_FLAVOR
160                 else
161                         wait_flavor cli2mdt $SK_FLAVOR
162                         wait_flavor cli2ost $SK_FLAVOR
163                 fi
164         fi
165 }
166
167 sec_setup() {
168         for ((num = 1; num <= $MDSCOUNT; num++)); do
169                 switch_identity $num true || identity_old[$num]=$?
170         done
171
172         if ! $RUNAS_CMD -u $ID0 ls $DIR > /dev/null 2>&1; then
173                 sec_login $USER0 $USER0
174         fi
175
176         if ! $RUNAS_CMD -u $ID1 ls $DIR > /dev/null 2>&1; then
177                 sec_login $USER1 $USER1
178         fi
179         wait_ssk
180 }
181 sec_setup
182
183 # run as different user
184 test_0() {
185         umask 0022
186
187         chmod 0755 $DIR || error "chmod (1) Failed"
188         rm -rf $DIR/$tdir || error "rm (1) Failed"
189         mkdir -p $DIR/$tdir || error "mkdir (1) Failed"
190
191         # $DIR/$tdir owner changed to USER0(sanityusr)
192         chown $USER0 $DIR/$tdir || error "chown (2) Failed"
193         chmod 0755 $DIR/$tdir || error "chmod (2) Failed"
194
195         # Run as ID0 cmd must pass
196         $RUNAS_CMD -u $ID0 ls -ali $DIR || error "ls (1) Failed"
197         # Remove non-existing file f0
198         rm -f $DIR/f0 || error "rm (2) Failed"
199
200         # It is expected that this cmd should fail
201         # $DIR has only r-x rights for group and other
202         $RUNAS_CMD -u $ID0 touch $DIR/f0
203         (( $? == 0 )) && error "touch (1) should not pass"
204
205         # This must pass. $DIR/$tdir/ is owned by ID0/USER0
206         $RUNAS_CMD -u $ID0 touch $DIR/$tdir/f1 || error "touch (2) Failed"
207
208         # It is expected that this cmd should fail
209         # $tdir has rwxr-xr-x rights for $ID0
210         $RUNAS_CMD -u $ID1 touch $DIR/$tdir/f2
211         (( $? == 0 )) && error "touch (3) should not pass"
212
213         touch $DIR/$tdir/f3 || error "touch (4) Failed"
214         chown root $DIR/$tdir || error "chown (3) Failed"
215         chgrp $USER0 $DIR/$tdir || error "chgrp (1) Failed"
216         chmod 0775 $DIR/$tdir || error "chmod (3) Failed"
217
218         # Owner is root and group is USER0
219         $RUNAS_CMD -u $USER0 -g $USER0 touch $DIR/$tdir/f4 ||
220                 error "touch (5) Failed"
221
222         # It is expected that this cmd should fail
223         # $tdir has rwxrwxr-x rights for group sanityusr/ID0, ID1 will fail
224         $RUNAS_CMD -u $ID1 -g $ID1 touch $DIR/$tdir/f5
225         (( $? == 0 )) && error "touch (6) should not pass"
226
227         touch $DIR/$tdir/f6 || error "touch (7) Failed"
228         rm -rf $DIR/$tdir || error "rm (3) Failed"
229 }
230 run_test 0 "uid permission ============================="
231
232 # setuid/gid
233 test_1() {
234         [ $GSS_SUP = 0 ] && skip "without GSS support." && return
235
236         rm -rf $DIR/$tdir
237         mkdir_on_mdt0 $DIR/$tdir
238
239         chown $USER0 $DIR/$tdir || error "chown (1)"
240         $RUNAS_CMD -u $ID1 -v $ID0 touch $DIR/$tdir/f0 && error "touch (2)"
241         echo "enable uid $ID1 setuid"
242         do_facet $SINGLEMDS "echo '* $ID1 setuid' >> $PERM_CONF"
243         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
244         $RUNAS_CMD -u $ID1 -v $ID0 touch $DIR/$tdir/f1 || error "touch (3)"
245
246         chown root $DIR/$tdir || error "chown (4)"
247         chgrp $USER0 $DIR/$tdir || error "chgrp (5)"
248         chmod 0770 $DIR/$tdir || error "chmod (6)"
249         $RUNAS_CMD -u $ID1 -g $ID1 touch $DIR/$tdir/f2 && error "touch (7)"
250         $RUNAS_CMD -u$ID1 -g$ID1 -j$ID0 touch $DIR/$tdir/f3 && error "touch (8)"
251         echo "enable uid $ID1 setuid,setgid"
252         do_facet $SINGLEMDS "echo '* $ID1 setuid,setgid' > $PERM_CONF"
253         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
254         $RUNAS_CMD -u $ID1 -g $ID1 -j $ID0 touch $DIR/$tdir/f4 ||
255                 error "touch (9)"
256         $RUNAS_CMD -u $ID1 -v $ID0 -g $ID1 -j $ID0 touch $DIR/$tdir/f5 ||
257                 error "touch (10)"
258
259         rm -rf $DIR/$tdir
260
261         do_facet $SINGLEMDS "rm -f $PERM_CONF"
262         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
263 }
264 run_test 1 "setuid/gid ============================="
265
266 # bug 3285 - supplementary group should always succeed.
267 # NB: the supplementary groups are set for local client only,
268 # as for remote client, the groups of the specified uid on MDT
269 # will be obtained by upcall /usr/sbin/l_getidentity and used.
270 test_4() {
271         [[ "$MDS1_VERSION" -ge $(version_code 2.6.93) ]] ||
272         [[ "$MDS1_VERSION" -ge $(version_code 2.5.35) &&
273            "$MDS1_VERSION" -lt $(version_code 2.5.50) ]] ||
274                 skip "Need MDS version at least 2.6.93 or 2.5.35"
275
276         rm -rf $DIR/$tdir
277         mkdir_on_mdt0 -p $DIR/$tdir
278         chmod 0771 $DIR/$tdir
279         chgrp $ID0 $DIR/$tdir
280         $RUNAS_CMD -u $ID0 ls $DIR/$tdir || error "setgroups (1)"
281         do_facet $SINGLEMDS "echo '* $ID1 setgrp' > $PERM_CONF"
282         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
283         $RUNAS_CMD -u $ID1 -G1,2,$ID0 ls $DIR/$tdir ||
284                 error "setgroups (2)"
285         $RUNAS_CMD -u $ID1 -G1,2 ls $DIR/$tdir && error "setgroups (3)"
286         rm -rf $DIR/$tdir
287
288         do_facet $SINGLEMDS "rm -f $PERM_CONF"
289         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
290 }
291 run_test 4 "set supplementary group ==============="
292
293 create_nodemaps() {
294         local i
295         local rc
296
297         squash_id default ${NOBODY_UID:-65534} 0
298         wait_nm_sync default squash_uid '' inactive
299         squash_id default ${NOBODY_UID:-65534} 1
300         wait_nm_sync default squash_gid '' inactive
301         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
302                 local csum=${HOSTNAME_CHECKSUM}_${i}
303
304                 do_facet mgs $LCTL $nodemap_add $csum
305                 rc=$?
306                 if [ $rc -ne 0 ]; then
307                         echo "$nodemap_add $csum failed with $rc"
308                         return $rc
309                 fi
310
311                 wait_update_facet --verbose mgs \
312                         "$LCTL get_param nodemap.$csum.id 2>/dev/null | \
313                         grep -c $csum || true" 1 30 ||
314                     return 1
315         done
316         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
317                 local csum=${HOSTNAME_CHECKSUM}_${i}
318
319                 wait_nm_sync $csum id '' inactive
320         done
321         return 0
322 }
323
324 delete_nodemaps() {
325         local i
326
327         for ((i = 0; i < NODEMAP_COUNT; i++)); do
328                 local csum=${HOSTNAME_CHECKSUM}_${i}
329
330                 if ! do_facet mgs $LCTL $nodemap_del $csum; then
331                         error "$nodemap_del $csum failed with $?"
332                         return 3
333                 fi
334
335                 wait_update_facet --verbose mgs \
336                         "$LCTL get_param nodemap.$csum.id 2>/dev/null | \
337                         grep -c $csum || true" 0 30 ||
338                     return 1
339         done
340         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
341                 local csum=${HOSTNAME_CHECKSUM}_${i}
342
343                 wait_nm_sync $csum id '' inactive
344         done
345         return 0
346 }
347
348 add_range() {
349         local j
350         local cmd="$LCTL $nodemap_add_range"
351         local range
352         local rc=0
353
354         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
355                 range="$SUBNET_CHECKSUM.${2}.${j}.[1-253]@tcp"
356                 if ! do_facet mgs $cmd --name $1 --range $range; then
357                         rc=$((rc + 1))
358                 fi
359         done
360         return $rc
361 }
362
363 delete_range() {
364         local j
365         local cmd="$LCTL $nodemap_del_range"
366         local range
367         local rc=0
368
369         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
370                 range="$SUBNET_CHECKSUM.${2}.${j}.[1-253]@tcp"
371                 if ! do_facet mgs $cmd --name $1 --range $range; then
372                         rc=$((rc + 1))
373                 fi
374         done
375
376         return $rc
377 }
378
379 add_idmaps() {
380         local i
381         local cmd="$LCTL $nodemap_add_idmap"
382         local do_proj=true
383         local rc=0
384
385         (( $MDS1_VERSION >= $(version_code 2.14.52) )) || do_proj=false
386
387         echo "Start to add idmaps ..."
388         for ((i = 0; i < NODEMAP_COUNT; i++)); do
389                 local j
390
391                 for ((j = $ID0; j < NODEMAP_MAX_ID; j++)); do
392                         local csum=${HOSTNAME_CHECKSUM}_${i}
393                         local client_id=$j
394                         local fs_id=$((j + 1))
395
396                         if ! do_facet mgs $cmd --name $csum --idtype uid \
397                              --idmap $client_id:$fs_id; then
398                                 rc=$((rc + 1))
399                         fi
400                         if ! do_facet mgs $cmd --name $csum --idtype gid \
401                              --idmap $client_id:$fs_id; then
402                                 rc=$((rc + 1))
403                         fi
404                         if $do_proj; then
405                                 if ! do_facet mgs $cmd --name $csum \
406                                      --idtype projid --idmap \
407                                      $client_id:$fs_id; then
408                                         rc=$((rc + 1))
409                                 fi
410                         fi
411                 done
412         done
413
414         return $rc
415 }
416
417 add_root_idmaps() {
418         local i
419         local cmd="$LCTL $nodemap_add_idmap"
420         local rc=0
421
422         echo "Start to add root idmaps ..."
423         for ((i = 0; i < NODEMAP_COUNT; i++)); do
424                 local csum=${HOSTNAME_CHECKSUM}_${i}
425
426                 if ! do_facet mgs $cmd --name $csum --idtype uid \
427                      --idmap 0:1; then
428                         rc=$((rc + 1))
429                 fi
430                 if ! do_facet mgs $cmd --name $csum --idtype gid \
431                      --idmap 0:1; then
432                         rc=$((rc + 1))
433                 fi
434         done
435
436         return $rc
437 }
438
439 update_idmaps() { #LU-10040
440         [ "$MGS_VERSION" -lt $(version_code 2.10.55) ] &&
441                 skip "Need MGS >= 2.10.55"
442
443         local csum=${HOSTNAME_CHECKSUM}_0
444         local old_id_client=$ID0
445         local old_id_fs=$((ID0 + 1))
446         local new_id=$((ID0 + 100))
447         local tmp_id
448         local cmd
449         local rc=0
450
451         echo "Start to update idmaps ..."
452
453         #Inserting an existed idmap should return error
454         cmd="$LCTL $nodemap_add_idmap --name $csum --idtype uid"
455         if do_facet mgs \
456                 $cmd --idmap $old_id_client:$old_id_fs 2>/dev/null; then
457                 error "insert idmap {$old_id_client:$old_id_fs} " \
458                         "should return error"
459                 rc=$((rc + 1))
460                 return rc
461         fi
462
463         #Update id_fs and check it
464         if ! do_facet mgs $cmd --idmap $old_id_client:$new_id; then
465                 error "$cmd --idmap $old_id_client:$new_id failed"
466                 rc=$((rc + 1))
467                 return $rc
468         fi
469         tmp_id=$(do_facet mgs $LCTL get_param -n nodemap.$csum.idmap |
470                 awk '{ print $7 }' | sed -n '2p')
471         [ $tmp_id != $new_id ] && { error "new id_fs $tmp_id != $new_id"; \
472                 rc=$((rc + 1)); return $rc; }
473
474         #Update id_client and check it
475         if ! do_facet mgs $cmd --idmap $new_id:$new_id; then
476                 error "$cmd --idmap $new_id:$new_id failed"
477                 rc=$((rc + 1))
478                 return $rc
479         fi
480         tmp_id=$(do_facet mgs $LCTL get_param -n nodemap.$csum.idmap |
481                 awk '{ print $5 }' | sed -n "$((NODEMAP_ID_COUNT + 1)) p")
482         tmp_id=$(echo ${tmp_id%,*}) #e.g. "501,"->"501"
483         [ $tmp_id != $new_id ] && { error "new id_client $tmp_id != $new_id"; \
484                 rc=$((rc + 1)); return $rc; }
485
486         #Delete above updated idmap
487         cmd="$LCTL $nodemap_del_idmap --name $csum --idtype uid"
488         if ! do_facet mgs $cmd --idmap $new_id:$new_id; then
489                 error "$cmd --idmap $new_id:$new_id failed"
490                 rc=$((rc + 1))
491                 return $rc
492         fi
493
494         #restore the idmaps to make delete_idmaps work well
495         cmd="$LCTL $nodemap_add_idmap --name $csum --idtype uid"
496         if ! do_facet mgs $cmd --idmap $old_id_client:$old_id_fs; then
497                 error "$cmd --idmap $old_id_client:$old_id_fs failed"
498                 rc=$((rc + 1))
499                 return $rc
500         fi
501
502         return $rc
503 }
504
505 delete_idmaps() {
506         local i
507         local cmd="$LCTL $nodemap_del_idmap"
508         local do_proj=true
509         local rc=0
510
511         (( $MDS1_VERSION >= $(version_code 2.14.52) )) || do_proj=false
512
513         echo "Start to delete idmaps ..."
514         for ((i = 0; i < NODEMAP_COUNT; i++)); do
515                 local j
516
517                 for ((j = $ID0; j < NODEMAP_MAX_ID; j++)); do
518                         local csum=${HOSTNAME_CHECKSUM}_${i}
519                         local client_id=$j
520                         local fs_id=$((j + 1))
521
522                         if ! do_facet mgs $cmd --name $csum --idtype uid \
523                              --idmap $client_id:$fs_id; then
524                                 rc=$((rc + 1))
525                         fi
526                         if ! do_facet mgs $cmd --name $csum --idtype gid \
527                              --idmap $client_id:$fs_id; then
528                                 rc=$((rc + 1))
529                         fi
530                         if $do_proj; then
531                                 if ! do_facet mgs $cmd --name $csum \
532                                      --idtype projid --idmap \
533                                      $client_id:$fs_id; then
534                                         rc=$((rc + 1))
535                                 fi
536                         fi
537                 done
538         done
539
540         return $rc
541 }
542
543 delete_root_idmaps() {
544         local i
545         local cmd="$LCTL $nodemap_del_idmap"
546         local rc=0
547
548         echo "Start to delete root idmaps ..."
549         for ((i = 0; i < NODEMAP_COUNT; i++)); do
550                 local csum=${HOSTNAME_CHECKSUM}_${i}
551
552                 if ! do_facet mgs $cmd --name $csum --idtype uid \
553                      --idmap 0:1; then
554                         rc=$((rc + 1))
555                 fi
556                 if ! do_facet mgs $cmd --name $csum --idtype gid \
557                      --idmap 0:1; then
558                         rc=$((rc + 1))
559                 fi
560         done
561
562         return $rc
563 }
564
565 modify_flags() {
566         local i
567         local proc
568         local option
569         local cmd="$LCTL $nodemap_modify"
570         local rc=0
571
572         proc[0]="admin_nodemap"
573         proc[1]="trusted_nodemap"
574         option[0]="admin"
575         option[1]="trusted"
576
577         for ((idx = 0; idx < 2; idx++)); do
578                 if ! do_facet mgs $cmd --name $1 --property ${option[$idx]} \
579                      --value 1; then
580                         rc=$((rc + 1))
581                 fi
582
583                 if ! do_facet mgs $cmd --name $1 --property ${option[$idx]} \
584                      --value 0; then
585                         rc=$((rc + 1))
586                 fi
587         done
588
589         return $rc
590 }
591
592 squash_id() {
593         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
594                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
595
596         local cmd
597
598         cmd[0]="$LCTL $nodemap_modify --property squash_uid"
599         cmd[1]="$LCTL $nodemap_modify --property squash_gid"
600         cmd[2]="$LCTL $nodemap_modify --property squash_projid"
601
602         if ! do_facet mgs ${cmd[$3]} --name $1 --value $2; then
603                 return 1
604         fi
605 }
606
607 # ensure that the squash defaults are the expected defaults
608 squash_id default ${NOBODY_UID:-65534} 0
609 wait_nm_sync default squash_uid '' inactive
610 squash_id default ${NOBODY_UID:-65534} 1
611 wait_nm_sync default squash_gid '' inactive
612 if [ "$MDS1_VERSION" -ge $(version_code 2.14.50) ]; then
613         squash_id default ${NOBODY_UID:-65534} 2
614         wait_nm_sync default squash_projid '' inactive
615 fi
616
617 test_nid() {
618         local cmd
619
620         cmd="$LCTL $nodemap_test_nid"
621
622         nid=$(do_facet mgs $cmd $1)
623
624         if [ $nid == $2 ]; then
625                 return 0
626         fi
627
628         return 1
629 }
630
631 cleanup_active() {
632         # restore activation state
633         do_facet mgs $LCTL $nodemap_activate 0
634         wait_nm_sync active
635 }
636
637 test_idmap() {
638         local i
639         local cmd="$LCTL $nodemap_test_id"
640         local do_root_idmap=true
641         local rc=0
642
643         (( $MDS1_VERSION >= $(version_code 2.15.60) )) || do_root_idmap=false
644
645         echo "Start to test idmaps ..."
646         ## nodemap deactivated
647         if ! do_facet mgs $LCTL $nodemap_activate 0; then
648                 return 1
649         fi
650         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
651                 local j
652
653                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
654                         local nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
655                         local fs_id=$(do_facet mgs $cmd --nid $nid      \
656                                       --idtype uid --id $id)
657                         if [ $fs_id != $id ]; then
658                                 echo "expected $id, got $fs_id"
659                                 rc=$((rc + 1))
660                         fi
661                 done
662         done
663
664         ## nodemap activated
665         if ! do_facet mgs $LCTL $nodemap_activate 1; then
666                 return 2
667         fi
668
669         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
670                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
671                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
672                         fs_id=$(do_facet mgs $cmd --nid $nid    \
673                                 --idtype uid --id $id)
674                         expected_id=$((id + 1))
675                         if [ $fs_id != $expected_id ]; then
676                                 echo "expected $expected_id, got $fs_id"
677                                 rc=$((rc + 1))
678                         fi
679                 done
680         done
681
682         ## trust client ids
683         for ((i = 0; i < NODEMAP_COUNT; i++)); do
684                 local csum=${HOSTNAME_CHECKSUM}_${i}
685
686                 if ! do_facet mgs $LCTL $nodemap_modify --name $csum \
687                      --property trusted --value 1; then
688                         error "$nodemap_modify $csum failed with $?"
689                         return 3
690                 fi
691         done
692
693         for ((id = $ID0; id < NODEMAP_MAX_ID; id++)); do
694                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
695                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
696                         fs_id=$(do_facet mgs $cmd --nid $nid    \
697                                 --idtype uid --id $id)
698                         if [ $fs_id != $id ]; then
699                                 echo "expected $id, got $fs_id"
700                                 rc=$((rc + 1))
701                         fi
702                 done
703         done
704
705         ## ensure allow_root_access is enabled
706         for ((i = 0; i < NODEMAP_COUNT; i++)); do
707                 local csum=${HOSTNAME_CHECKSUM}_${i}
708
709                 if ! do_facet mgs $LCTL $nodemap_modify --name $csum    \
710                      --property admin --value 1; then
711                         error "$nodemap_modify $csum failed with $?"
712                         return 3
713                 fi
714         done
715
716         ## check that root allowed
717         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
718                 nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
719                 fs_id=$(do_facet mgs $cmd --nid $nid --idtype uid --id 0)
720                 if [ $fs_id != 0 ]; then
721                         echo "root allowed expected 0, got $fs_id"
722                         rc=$((rc + 1))
723                 fi
724         done
725
726         if $do_root_idmap; then
727                 ## add mapping for root
728                 add_root_idmaps
729
730                 ## check that root allowed
731                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
732                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
733                         fs_id=$(do_facet mgs $cmd --nid $nid \
734                                 --idtype uid --id 0)
735                         if [ $fs_id != 0 ]; then
736                                 echo "root allowed expected 0, got $fs_id"
737                                 rc=$((rc + 1))
738                         fi
739                 done
740
741                 ## delete mapping for root
742                 delete_root_idmaps
743         fi
744
745         ## ensure allow_root_access is disabled
746         for ((i = 0; i < NODEMAP_COUNT; i++)); do
747                 local csum=${HOSTNAME_CHECKSUM}_${i}
748
749                 if ! do_facet mgs $LCTL $nodemap_modify --name $csum    \
750                                 --property admin --value 0; then
751                         error "$nodemap_modify ${HOSTNAME_CHECKSUM}_${i} "
752                                 "failed with $rc"
753                         return 3
754                 fi
755         done
756
757         ## check that root is mapped to NOBODY_UID
758         for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
759                 nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
760                 fs_id=$(do_facet mgs $cmd --nid $nid --idtype uid --id 0)
761                 if [ $fs_id != ${NOBODY_UID:-65534} ]; then
762                       error "root squash expect ${NOBODY_UID:-65534} got $fs_id"
763                       rc=$((rc + 1))
764                 fi
765         done
766
767         if $do_root_idmap; then
768                 ## add mapping for root
769                 add_root_idmaps
770
771                 ## check root is mapped
772                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
773                         nid="$SUBNET_CHECKSUM.0.${j}.100@tcp"
774                         fs_id=$(do_facet mgs $cmd --nid $nid    \
775                                 --idtype uid --id 0)
776                         expected_id=1
777                         if [ $fs_id != $expected_id ]; then
778                                 echo "expected $expected_id, got $fs_id"
779                                 rc=$((rc + 1))
780                         fi
781                 done
782
783                 ## delete mapping for root
784                 delete_root_idmaps
785         fi
786
787         ## reset client trust to 0
788         for ((i = 0; i < NODEMAP_COUNT; i++)); do
789                 if ! do_facet mgs $LCTL $nodemap_modify         \
790                         --name ${HOSTNAME_CHECKSUM}_${i}        \
791                         --property trusted --value 0; then
792                         error "$nodemap_modify ${HOSTNAME_CHECKSUM}_${i} "
793                                 "failed with $rc"
794                         return 3
795                 fi
796         done
797
798         return $rc
799 }
800
801 test_7() {
802         local rc
803
804         remote_mgs_nodsh && skip "remote MGS with nodsh"
805         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
806                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
807
808         create_nodemaps
809         rc=$?
810         [[ $rc != 0 ]] && error "nodemap_add failed with $rc"
811
812         delete_nodemaps
813         rc=$?
814         [[ $rc != 0 ]] && error "nodemap_del failed with $rc"
815
816         return 0
817 }
818 run_test 7 "nodemap create and delete"
819
820 test_8() {
821         local rc
822
823         remote_mgs_nodsh && skip "remote MGS with nodsh"
824         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
825                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
826
827         # Set up nodemaps
828
829         create_nodemaps
830         rc=$?
831         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
832
833         # Try duplicates
834
835         create_nodemaps
836         rc=$?
837         [[ $rc == 0 ]] && error "duplicate nodemap_add allowed with $rc" &&
838         return 2
839
840         # Clean up
841         delete_nodemaps
842         rc=$?
843         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 3
844
845         return 0
846 }
847 run_test 8 "nodemap reject duplicates"
848
849 test_9() {
850         local i
851         local rc
852
853         remote_mgs_nodsh && skip "remote MGS with nodsh"
854         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
855                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
856
857         rc=0
858         create_nodemaps
859         rc=$?
860         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
861
862         rc=0
863         for ((i = 0; i < NODEMAP_COUNT; i++)); do
864                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
865                         rc=$((rc + 1))
866                 fi
867         done
868         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
869
870         rc=0
871         for ((i = 0; i < NODEMAP_COUNT; i++)); do
872                 if ! delete_range ${HOSTNAME_CHECKSUM}_${i} $i; then
873                         rc=$((rc + 1))
874                 fi
875         done
876         [[ $rc != 0 ]] && error "nodemap_del_range failed with $rc" && return 4
877
878         rc=0
879         delete_nodemaps
880         rc=$?
881         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
882
883         return 0
884 }
885 run_test 9 "nodemap range add"
886
887 test_10a() {
888         local rc
889
890         remote_mgs_nodsh && skip "remote MGS with nodsh"
891         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
892                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
893
894         rc=0
895         create_nodemaps
896         rc=$?
897         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
898
899         rc=0
900         for ((i = 0; i < NODEMAP_COUNT; i++)); do
901                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
902                         rc=$((rc + 1))
903                 fi
904         done
905         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
906
907         rc=0
908         for ((i = 0; i < NODEMAP_COUNT; i++)); do
909                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
910                         rc=$((rc + 1))
911                 fi
912         done
913         [[ $rc == 0 ]] && error "nodemap_add_range duplicate add with $rc" &&
914                 return 2
915
916
917         rc=0
918         for ((i = 0; i < NODEMAP_COUNT; i++)); do
919                 if ! delete_range ${HOSTNAME_CHECKSUM}_${i} $i; then
920                         rc=$((rc + 1))
921                 fi
922         done
923         [[ $rc != 0 ]] && error "nodemap_del_range failed with $rc" && return 4
924
925         delete_nodemaps
926         rc=$?
927         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 5
928
929         return 0
930 }
931 run_test 10a "nodemap reject duplicate ranges"
932
933 test_10b() {
934         [ "$MGS_VERSION" -lt $(version_code 2.10.53) ] &&
935                 skip "Need MGS >= 2.10.53"
936
937         local nm1="nodemap1"
938         local nm2="nodemap2"
939         local nids="192.168.19.[0-255]@o2ib20"
940
941         do_facet mgs $LCTL nodemap_del $nm1 2>/dev/null
942         do_facet mgs $LCTL nodemap_del $nm2 2>/dev/null
943
944         do_facet mgs $LCTL nodemap_add $nm1 || error "Add $nm1 failed"
945         do_facet mgs $LCTL nodemap_add $nm2 || error "Add $nm2 failed"
946         do_facet mgs $LCTL nodemap_add_range --name $nm1 --range $nids ||
947                 error "Add range $nids to $nm1 failed"
948         [ -n "$(do_facet mgs $LCTL get_param nodemap.$nm1.* |
949                 grep start_nid)" ] || error "No range was found"
950         do_facet mgs $LCTL nodemap_del_range --name $nm2 --range $nids &&
951                 error "Deleting range $nids from $nm2 should fail"
952         [ -n "$(do_facet mgs $LCTL get_param nodemap.$nm1.* |
953                 grep start_nid)" ] || error "Range $nids should be there"
954
955         do_facet mgs $LCTL nodemap_del $nm1 || error "Delete $nm1 failed"
956         do_facet mgs $LCTL nodemap_del $nm2 || error "Delete $nm2 failed"
957         return 0
958 }
959 run_test 10b "delete range from the correct nodemap"
960
961 test_10c() { #LU-8912
962         [ "$MGS_VERSION" -lt $(version_code 2.10.57) ] &&
963                 skip "Need MGS >= 2.10.57"
964
965         local nm="nodemap_lu8912"
966         local nid_range="10.210.[32-47].[0-255]@o2ib3"
967         local start_nid="10.210.32.0@o2ib3"
968         local end_nid="10.210.47.255@o2ib3"
969         local start_nid_found
970         local end_nid_found
971
972         do_facet mgs $LCTL nodemap_del $nm 2>/dev/null
973         do_facet mgs $LCTL nodemap_add $nm || error "Add $nm failed"
974         do_facet mgs $LCTL nodemap_add_range --name $nm --range $nid_range ||
975                 error "Add range $nid_range to $nm failed"
976
977         start_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
978                 awk -F '[,: ]' /start_nid/'{ print $9 }')
979         [ "$start_nid" == "$start_nid_found" ] ||
980                 error "start_nid: $start_nid_found != $start_nid"
981         end_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
982                 awk -F '[,: ]' /end_nid/'{ print $13 }')
983         [ "$end_nid" == "$end_nid_found" ] ||
984                 error "end_nid: $end_nid_found != $end_nid"
985
986         do_facet mgs $LCTL nodemap_del $nm || error "Delete $nm failed"
987         return 0
988 }
989 run_test 10c "verfify contiguous range support"
990
991 test_10d() { #LU-8913
992         [ "$MGS_VERSION" -lt $(version_code 2.10.59) ] &&
993                 skip "Need MGS >= 2.10.59"
994
995         local nm="nodemap_lu8913"
996         local nid_range="*@o2ib3"
997         local start_nid="0.0.0.0@o2ib3"
998         local end_nid="255.255.255.255@o2ib3"
999         local start_nid_found
1000         local end_nid_found
1001
1002         do_facet mgs $LCTL nodemap_del $nm 2>/dev/null
1003         do_facet mgs $LCTL nodemap_add $nm || error "Add $nm failed"
1004         do_facet mgs $LCTL nodemap_add_range --name $nm --range $nid_range ||
1005                 error "Add range $nid_range to $nm failed"
1006
1007         start_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
1008                 awk -F '[,: ]' /start_nid/'{ print $9 }')
1009         [ "$start_nid" == "$start_nid_found" ] ||
1010                 error "start_nid: $start_nid_found != $start_nid"
1011         end_nid_found=$(do_facet mgs $LCTL get_param nodemap.$nm.* |
1012                 awk -F '[,: ]' /end_nid/'{ print $13 }')
1013         [ "$end_nid" == "$end_nid_found" ] ||
1014                 error "end_nid: $end_nid_found != $end_nid"
1015
1016         do_facet mgs $LCTL nodemap_del $nm || error "Delete $nm failed"
1017         return 0
1018 }
1019 run_test 10d "verfify nodemap range format '*@<net>' support"
1020
1021 test_11() {
1022         local rc
1023
1024         remote_mgs_nodsh && skip "remote MGS with nodsh"
1025         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1026                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1027
1028         rc=0
1029         create_nodemaps
1030         rc=$?
1031         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1032
1033         rc=0
1034         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1035                 if ! modify_flags ${HOSTNAME_CHECKSUM}_${i}; then
1036                         rc=$((rc + 1))
1037                 fi
1038         done
1039         [[ $rc != 0 ]] && error "nodemap_modify with $rc" && return 2
1040
1041         rc=0
1042         delete_nodemaps
1043         rc=$?
1044         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 3
1045
1046         return 0
1047 }
1048 run_test 11 "nodemap modify"
1049
1050 test_12() {
1051         local rc
1052
1053         remote_mgs_nodsh && skip "remote MGS with nodsh"
1054         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1055                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1056
1057         rc=0
1058         create_nodemaps
1059         rc=$?
1060         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1061
1062         rc=0
1063         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1064                 if ! squash_id ${HOSTNAME_CHECKSUM}_${i} 88 0; then
1065                         rc=$((rc + 1))
1066                 fi
1067         done
1068         [[ $rc != 0 ]] && error "nodemap squash_uid with $rc" && return 2
1069
1070         rc=0
1071         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1072                 if ! squash_id ${HOSTNAME_CHECKSUM}_${i} 88 1; then
1073                         rc=$((rc + 1))
1074                 fi
1075         done
1076         [[ $rc != 0 ]] && error "nodemap squash_gid with $rc" && return 3
1077
1078         rc=0
1079         if (( $MDS1_VERSION >= $(version_code 2.14.52) )); then
1080                 for ((i = 0; i < NODEMAP_COUNT; i++)); do
1081                         if ! squash_id ${HOSTNAME_CHECKSUM}_${i} 88 2; then
1082                                 rc=$((rc + 1))
1083                         fi
1084                 done
1085         fi
1086         [[ $rc != 0 ]] && error "nodemap squash_projid with $rc" && return 5
1087
1088         rc=0
1089         delete_nodemaps
1090         rc=$?
1091         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
1092
1093         return 0
1094 }
1095 run_test 12 "nodemap set squash ids"
1096
1097 test_13() {
1098         local rc
1099
1100         remote_mgs_nodsh && skip "remote MGS with nodsh"
1101         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1102                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1103
1104         rc=0
1105         create_nodemaps
1106         rc=$?
1107         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1108
1109         rc=0
1110         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1111                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
1112                         rc=$((rc + 1))
1113                 fi
1114         done
1115         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
1116
1117         rc=0
1118         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1119                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
1120                         for k in $NODEMAP_IPADDR_LIST; do
1121                                 if ! test_nid $SUBNET_CHECKSUM.$i.$j.$k \
1122                                        ${HOSTNAME_CHECKSUM}_${i}; then
1123                                         rc=$((rc + 1))
1124                                 fi
1125                         done
1126                 done
1127         done
1128         [[ $rc != 0 ]] && error "nodemap_test_nid failed with $rc" && return 3
1129
1130         rc=0
1131         delete_nodemaps
1132         rc=$?
1133         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
1134
1135         return 0
1136 }
1137 run_test 13 "test nids"
1138
1139 test_14() {
1140         local rc
1141
1142         remote_mgs_nodsh && skip "remote MGS with nodsh"
1143         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1144                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1145
1146         rc=0
1147         create_nodemaps
1148         rc=$?
1149         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1150
1151         rc=0
1152         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1153                 for ((j = 0; j < NODEMAP_RANGE_COUNT; j++)); do
1154                         for k in $NODEMAP_IPADDR_LIST; do
1155                                 if ! test_nid $SUBNET_CHECKSUM.$i.$j.$k \
1156                                         default; then
1157                                         rc=$((rc + 1))
1158                                 fi
1159                         done
1160                 done
1161         done
1162         [[ $rc != 0 ]] && error "nodemap_test_nid failed with $rc" && return 3
1163
1164         rc=0
1165         delete_nodemaps
1166         rc=$?
1167         [[ $rc != 0 ]] && error "nodemap_del failed with $rc" && return 4
1168
1169         return 0
1170 }
1171 run_test 14 "test default nodemap nid lookup"
1172
1173 test_15() {
1174         local rc
1175
1176         remote_mgs_nodsh && skip "remote MGS with nodsh"
1177         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1178                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53"
1179
1180         rc=0
1181         create_nodemaps
1182         rc=$?
1183         [[ $rc != 0 ]] && error "nodemap_add failed with $rc" && return 1
1184
1185         for (( i = 0; i < NODEMAP_COUNT; i++ )); do
1186                 local csum=${HOSTNAME_CHECKSUM}_${i}
1187
1188                 if ! do_facet mgs $LCTL nodemap_modify --name $csum \
1189                                 --property admin --value 0; then
1190                         rc=$((rc + 1))
1191                 fi
1192                 if ! do_facet mgs $LCTL nodemap_modify --name $csum \
1193                                 --property trusted --value 0; then
1194                         rc=$((rc + 1))
1195                 fi
1196         done
1197         [[ $rc != 0 ]] && error "nodemap_modify failed with $rc" && return 1
1198
1199         rc=0
1200         for ((i = 0; i < NODEMAP_COUNT; i++)); do
1201                 if ! add_range ${HOSTNAME_CHECKSUM}_${i} $i; then
1202                         rc=$((rc + 1))
1203                 fi
1204         done
1205         [[ $rc != 0 ]] && error "nodemap_add_range failed with $rc" && return 2
1206
1207         rc=0
1208         add_idmaps
1209         rc=$?
1210         [[ $rc != 0 ]] && error "nodemap_add_idmap failed with $rc" && return 3
1211
1212         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
1213         if [[ "$activedefault" != "1" ]]; then
1214                 stack_trap cleanup_active EXIT
1215         fi
1216
1217         rc=0
1218         test_idmap
1219         rc=$?
1220         [[ $rc != 0 ]] && error "nodemap_test_id failed with $rc" && return 4
1221
1222         rc=0
1223         update_idmaps
1224         rc=$?
1225         [[ $rc != 0 ]] && error "update_idmaps failed with $rc" && return 5
1226
1227         rc=0
1228         delete_idmaps
1229         rc=$?
1230         [[ $rc != 0 ]] && error "nodemap_del_idmap failed with $rc" && return 6
1231
1232         rc=0
1233         delete_nodemaps
1234         rc=$?
1235         [[ $rc != 0 ]] && error "nodemap_delete failed with $rc" && return 7
1236
1237         return 0
1238 }
1239 run_test 15 "test id mapping"
1240
1241 create_fops_nodemaps() {
1242         local i=0
1243         local client
1244         for client in $clients; do
1245                 local client_ip=$(host_nids_address $client $NETTYPE)
1246                 local client_nid=$(h2nettype $client_ip)
1247
1248                 do_facet mgs $LCTL nodemap_add c${i} || return 1
1249                 do_facet mgs $LCTL nodemap_add_range    \
1250                         --name c${i} --range $client_nid || {
1251                         do_facet mgs $LCTL nodemap_del c${i}
1252                         return 1
1253                 }
1254                 for map in ${FOPS_IDMAPS[i]}; do
1255                         do_facet mgs $LCTL nodemap_add_idmap --name c${i} \
1256                                 --idtype uid --idmap ${map} || return 1
1257                         do_facet mgs $LCTL nodemap_add_idmap --name c${i} \
1258                                 --idtype gid --idmap ${map} || return 1
1259                 done
1260
1261                 wait_nm_sync c$i idmap
1262
1263                 i=$((i + 1))
1264         done
1265         return 0
1266 }
1267
1268 delete_fops_nodemaps() {
1269         local i=0
1270         local client
1271         for client in $clients; do
1272                 do_facet mgs $LCTL nodemap_del c${i} || return 1
1273                 i=$((i + 1))
1274         done
1275         return 0
1276 }
1277
1278 fops_mds_index=0
1279 nm_test_mkdir() {
1280         if [ $MDSCOUNT -le 1 ]; then
1281                 do_node ${clients_arr[0]} mkdir -p $DIR/$tdir
1282         else
1283                 # round-robin MDTs to test DNE nodemap support
1284                 [ ! -d $DIR ] && do_node ${clients_arr[0]} mkdir -p $DIR
1285                 do_node ${clients_arr[0]} $LFS setdirstripe -c 1 -i \
1286                         $((fops_mds_index % MDSCOUNT)) $DIR/$tdir
1287                 ((fops_mds_index++))
1288         fi
1289 }
1290
1291 # acl test directory needs to be initialized on a privileged client
1292 fops_test_setup() {
1293         local admin=$(do_facet mgs $LCTL get_param -n nodemap.c0.admin_nodemap)
1294         local trust=$(do_facet mgs $LCTL get_param -n \
1295                 nodemap.c0.trusted_nodemap)
1296
1297         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1298         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1299
1300         wait_nm_sync c0 admin_nodemap
1301         wait_nm_sync c0 trusted_nodemap
1302
1303         do_node ${clients_arr[0]} rm -rf $DIR/$tdir
1304         nm_test_mkdir
1305         do_node ${clients_arr[0]} chown $user $DIR/$tdir
1306
1307         do_facet mgs $LCTL nodemap_modify --name c0 \
1308                 --property admin --value $admin
1309         do_facet mgs $LCTL nodemap_modify --name c0 \
1310                 --property trusted --value $trust
1311
1312         # flush MDT locks to make sure they are reacquired before test
1313         do_node ${clients_arr[0]} $LCTL set_param \
1314                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1315
1316         wait_nm_sync c0 admin_nodemap
1317         wait_nm_sync c0 trusted_nodemap
1318 }
1319
1320 # fileset test directory needs to be initialized on a privileged client
1321 fileset_test_setup() {
1322         local nm=$1
1323         local modify_val=""
1324
1325         # exercise new nodemap_modify syntax if available
1326         (( $MGS_VERSION >= $(version_code 2.16.51) )) ||
1327                 modify_val=" --value"
1328
1329         if [[ -n $FILESET && -z $SKIP_FILESET ]]; then
1330                 cleanup_mount $MOUNT
1331                 FILESET="" zconf_mount_clients $CLIENTS $MOUNT
1332         fi
1333
1334         local admin=$(do_facet mgs $LCTL get_param -n \
1335                 nodemap.${nm}.admin_nodemap)
1336         local trust=$(do_facet mgs $LCTL get_param -n \
1337                 nodemap.${nm}.trusted_nodemap)
1338
1339         do_facet mgs $LCTL nodemap_modify --name $nm \
1340                 --property admin${modify_val}=1
1341         do_facet mgs $LCTL nodemap_modify --name $nm \
1342                 --property trusted${modify_val}=1
1343
1344         wait_nm_sync $nm trusted_nodemap
1345
1346         # create directory and populate it for subdir mount
1347         do_node ${clients_arr[0]} mkdir $MOUNT/$subdir ||
1348                 error "unable to create dir $MOUNT/$subdir"
1349         do_node ${clients_arr[0]} touch $MOUNT/$subdir/this_is_$subdir ||
1350                 error "unable to create file $MOUNT/$subdir/this_is_$subdir"
1351         do_node ${clients_arr[0]} mkdir $MOUNT/$subdir/$subsubdir ||
1352                 error "unable to create dir $MOUNT/$subdir/$subsubdir"
1353         do_node ${clients_arr[0]} touch \
1354                         $MOUNT/$subdir/$subsubdir/this_is_$subsubdir ||
1355                 error "unable to create file \
1356                         $MOUNT/$subdir/$subsubdir/this_is_$subsubdir"
1357
1358         do_facet mgs $LCTL nodemap_modify --name $nm \
1359                 --property admin${modify_val}=$admin
1360         do_facet mgs $LCTL nodemap_modify --name $nm \
1361                 --property trusted${modify_val}=$trust
1362
1363         # flush MDT locks to make sure they are reacquired before test
1364         do_node ${clients_arr[0]} $LCTL set_param \
1365                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1366
1367         wait_nm_sync $nm trusted_nodemap
1368 }
1369
1370 # fileset test directory needs to be initialized on a privileged client
1371 fileset_test_cleanup() {
1372         local nm=$1
1373         local admin=$(do_facet mgs $LCTL get_param -n \
1374                 nodemap.${nm}.admin_nodemap)
1375         local trust=$(do_facet mgs $LCTL get_param -n \
1376                 nodemap.${nm}.trusted_nodemap)
1377
1378         do_facet mgs $LCTL nodemap_modify --name $nm --property admin --value 1
1379         do_facet mgs $LCTL nodemap_modify --name $nm --property trusted \
1380                 --value 1
1381
1382         wait_nm_sync $nm admin_nodemap
1383         wait_nm_sync $nm trusted_nodemap
1384
1385         # cleanup directory created for subdir mount
1386         do_node ${clients_arr[0]} rm -rf $MOUNT/$subdir ||
1387                 error "unable to remove dir $MOUNT/$subdir"
1388
1389         do_facet mgs $LCTL nodemap_modify --name $nm \
1390                 --property admin --value $admin
1391         do_facet mgs $LCTL nodemap_modify --name $nm \
1392                 --property trusted --value $trust
1393
1394         # flush MDT locks to make sure they are reacquired before test
1395         do_node ${clients_arr[0]} $LCTL set_param \
1396                 ldlm.namespaces.$FSNAME-MDT*.lru_size=clear
1397
1398         wait_nm_sync $nm admin_nodemap
1399         wait_nm_sync $nm trusted_nodemap
1400         if [ -n "$FILESET" -a -z "$SKIP_FILESET" ]; then
1401                 cleanup_mount $MOUNT
1402                 zconf_mount_clients $CLIENTS $MOUNT
1403         fi
1404 }
1405
1406 do_create_delete() {
1407         local run_u=$1
1408         local key=$2
1409         local testfile=$DIR/$tdir/$tfile
1410         local rc=0
1411         local c=0 d=0
1412         local qused_new
1413         if $run_u touch $testfile >& /dev/null; then
1414                 c=1
1415                 $run_u rm $testfile && d=1
1416         fi >& /dev/null
1417
1418         local res="$c $d"
1419         local expected=$(get_cr_del_expected $key)
1420         [ "$res" != "$expected" ] &&
1421                 error "test $key, wanted $expected, got $res" && rc=$((rc + 1))
1422         return $rc
1423 }
1424
1425 nodemap_check_quota() {
1426         local run_u="$1"
1427         $run_u lfs quota -q $DIR | awk '{ print $2; exit; }'
1428 }
1429
1430 do_fops_quota_test() {
1431         local run_u=$1
1432         # fuzz quota used to account for possible indirect blocks, etc
1433         local quota_fuzz=$(fs_log_size)
1434         local qused_orig=$(nodemap_check_quota "$run_u")
1435         local qused_high=$((qused_orig + quota_fuzz))
1436         local qused_low=$((qused_orig - quota_fuzz))
1437         local testfile=$DIR/$tdir/$tfile
1438         $run_u dd if=/dev/zero of=$testfile oflag=sync bs=1M count=1 \
1439                 >& /dev/null || error "unable to write quota test file"
1440         sync; sync_all_data || true
1441
1442         local qused_new=$(nodemap_check_quota "$run_u")
1443         [ $((qused_new)) -lt $((qused_low + 1024)) -o \
1444           $((qused_new)) -gt $((qused_high + 1024)) ] &&
1445                 error "$qused_new != $qused_orig + 1M after write, " \
1446                       "fuzz is $quota_fuzz"
1447         $run_u rm $testfile || error "unable to remove quota test file"
1448         wait_delete_completed_mds
1449
1450         qused_new=$(nodemap_check_quota "$run_u")
1451         [ $((qused_new)) -lt $((qused_low)) \
1452                 -o $((qused_new)) -gt $((qused_high)) ] &&
1453                 error "quota not reclaimed, expect $qused_orig, " \
1454                       "got $qused_new, fuzz $quota_fuzz"
1455 }
1456
1457 get_fops_mapped_user() {
1458         local cli_user=$1
1459
1460         for ((i=0; i < ${#FOPS_IDMAPS[@]}; i++)); do
1461                 for map in ${FOPS_IDMAPS[i]}; do
1462                         if [ $(cut -d: -f1 <<< "$map") == $cli_user ]; then
1463                                 cut -d: -f2 <<< "$map"
1464                                 return
1465                         fi
1466                 done
1467         done
1468         echo -1
1469 }
1470
1471 get_cr_del_expected() {
1472         local -a key
1473         IFS=":" read -a key <<< "$1"
1474         local mapmode="${key[0]}"
1475         local mds_user="${key[1]}"
1476         local cluster="${key[2]}"
1477         local cli_user="${key[3]}"
1478         local mode="0${key[4]}"
1479         local SUCCESS="1 1"
1480         local FAILURE="0 0"
1481         local noadmin=0
1482         local mapped=0
1483         local other=0
1484
1485         [[ $mapmode == *mapped* ]] && mapped=1
1486         # only c1 is mapped in these test cases
1487         [[ $mapmode == mapped_trusted* ]] && [ "$cluster" == "c0" ] && mapped=0
1488         [[ $mapmode == *noadmin* ]] && noadmin=1
1489
1490         # o+wx works as long as the user isn't mapped
1491         if [ $((mode & 3)) -eq 3 ]; then
1492                 other=1
1493         fi
1494
1495         # if client user is root, check if root is squashed
1496         if [ "$cli_user" == "0" ]; then
1497                 # squash root succeed, if other bit is on
1498                 case $noadmin in
1499                         0) echo $SUCCESS;;
1500                         1) [ "$other" == "1" ] && echo $SUCCESS
1501                            [ "$other" == "0" ] && echo $FAILURE;;
1502                 esac
1503                 return
1504         fi
1505         if [ "$mapped" == "0" ]; then
1506                 [ "$other" == "1" ] && echo $SUCCESS
1507                 [ "$other" == "0" ] && echo $FAILURE
1508                 return
1509         fi
1510
1511         # if mapped user is mds user, check for u+wx
1512         mapped_user=$(get_fops_mapped_user $cli_user)
1513         [ "$mapped_user" == "-1" ] &&
1514                 error "unable to find mapping for client user $cli_user"
1515
1516         if [ "$mapped_user" == "$mds_user" -a \
1517              $(((mode & 0300) == 0300)) -eq 1 ]; then
1518                 echo $SUCCESS
1519                 return
1520         fi
1521         if [ "$mapped_user" != "$mds_user" -a "$other" == "1" ]; then
1522                 echo $SUCCESS
1523                 return
1524         fi
1525         echo $FAILURE
1526 }
1527
1528 test_fops_admin_cli_i=""
1529 test_fops_chmod_dir() {
1530         local current_cli_i=$1
1531         local perm_bits=$2
1532         local dir_to_chmod=$3
1533         local new_admin_cli_i=""
1534
1535         # do we need to set up a new admin client?
1536         [ "$current_cli_i" == "0" ] && [ "$test_fops_admin_cli_i" != "1" ] &&
1537                 new_admin_cli_i=1
1538         [ "$current_cli_i" != "0" ] && [ "$test_fops_admin_cli_i" != "0" ] &&
1539                 new_admin_cli_i=0
1540
1541         # if only one client, and non-admin, need to flip admin everytime
1542         if [ "$num_clients" == "1" ]; then
1543                 test_fops_admin_client=$clients
1544                 test_fops_admin_val=$(do_facet mgs $LCTL get_param -n \
1545                         nodemap.c0.admin_nodemap)
1546                 if [ "$test_fops_admin_val" != "1" ]; then
1547                         do_facet mgs $LCTL nodemap_modify \
1548                                 --name c0 \
1549                                 --property admin \
1550                                 --value 1
1551                         wait_nm_sync c0 admin_nodemap
1552                 fi
1553         elif [ "$new_admin_cli_i" != "" ]; then
1554                 # restore admin val to old admin client
1555                 if [ "$test_fops_admin_cli_i" != "" ] &&
1556                                 [ "$test_fops_admin_val" != "1" ]; then
1557                         do_facet mgs $LCTL nodemap_modify \
1558                                 --name c${test_fops_admin_cli_i} \
1559                                 --property admin \
1560                                 --value $test_fops_admin_val
1561                         wait_nm_sync c${test_fops_admin_cli_i} admin_nodemap
1562                 fi
1563
1564                 test_fops_admin_cli_i=$new_admin_cli_i
1565                 test_fops_admin_client=${clients_arr[$new_admin_cli_i]}
1566                 test_fops_admin_val=$(do_facet mgs $LCTL get_param -n \
1567                         nodemap.c${new_admin_cli_i}.admin_nodemap)
1568
1569                 if [ "$test_fops_admin_val" != "1" ]; then
1570                         do_facet mgs $LCTL nodemap_modify \
1571                                 --name c${new_admin_cli_i} \
1572                                 --property admin \
1573                                 --value 1
1574                         wait_nm_sync c${new_admin_cli_i} admin_nodemap
1575                 fi
1576         fi
1577
1578         do_node $test_fops_admin_client chmod $perm_bits $DIR/$tdir || return 1
1579
1580         # remove admin for single client if originally non-admin
1581         if [ "$num_clients" == "1" ] && [ "$test_fops_admin_val" != "1" ]; then
1582                 do_facet mgs $LCTL nodemap_modify --name c0 --property admin \
1583                         --value 0
1584                 wait_nm_sync c0 admin_nodemap
1585         fi
1586
1587         return 0
1588 }
1589
1590 test_fops() {
1591         local mapmode="$1"
1592         local single_client="$2"
1593         local client_user_list=([0]="0 $((IDBASE+3))"
1594                                 [1]="0 $((IDBASE+5))")
1595         local mds_users="-1 0"
1596         local mds_i
1597         local rc=0
1598         local perm_bit_list="3 $((0300))"
1599         # SLOW tests 000-007, 010-070, 100-700 (octal modes)
1600         if [ "$SLOW" == "yes" ]; then
1601                 perm_bit_list="0 $(seq 1 7) $(seq 8 8 63) $(seq 64 64 511) \
1602                                $((0303))"
1603                 client_user_list=([0]="0 $((IDBASE+3)) $((IDBASE+4))"
1604                                   [1]="0 $((IDBASE+5)) $((IDBASE+6))")
1605                 mds_users="-1 0 1 2"
1606         fi
1607
1608         # force single_client to speed up test
1609         [ "$SLOW" == "yes" ] ||
1610                 single_client=1
1611         # step through mds users. -1 means root
1612         for mds_i in $mds_users; do
1613                 local user=$((mds_i + IDBASE))
1614                 local client
1615                 local x
1616
1617                 [ "$mds_i" == "-1" ] && user=0
1618
1619                 echo mkdir -p $DIR/$tdir
1620                 fops_test_setup
1621                 local cli_i=0
1622                 for client in $clients; do
1623                         local u
1624                         for u in ${client_user_list[$cli_i]}; do
1625                                 local run_u="do_node $client \
1626                                              $RUNAS_CMD -u$u -g$u -G$u"
1627                                 for perm_bits in $perm_bit_list; do
1628                                         local mode=$(printf %03o $perm_bits)
1629                                         local key
1630                                         key="$mapmode:$user:c$cli_i:$u:$mode"
1631                                         test_fops_chmod_dir $cli_i $mode \
1632                                                 $DIR/$tdir ||
1633                                                         error cannot chmod $key
1634                                         do_create_delete "$run_u" "$key"
1635                                 done
1636
1637                                 # check quota
1638                                 test_fops_chmod_dir $cli_i 777 $DIR/$tdir ||
1639                                         error cannot chmod $key
1640                                 do_fops_quota_test "$run_u"
1641                         done
1642
1643                         cli_i=$((cli_i + 1))
1644                         [ "$single_client" == "1" ] && break
1645                 done
1646                 rm -rf $DIR/$tdir
1647         done
1648         return $rc
1649 }
1650
1651 nodemap_version_check () {
1652         remote_mgs_nodsh && skip "remote MGS with nodsh" && return 1
1653         [ "$MGS_VERSION" -lt $(version_code 2.5.53) ] &&
1654                 skip "No nodemap on $MGS_VERSION MGS < 2.5.53" &&
1655                 return 1
1656         return 0
1657 }
1658
1659 nodemap_test_setup() {
1660         local rc
1661         local active_nodemap=1
1662
1663         [ "$1" == "0" ] && active_nodemap=0
1664
1665         do_nodes $(comma_list $(all_mdts_nodes)) \
1666                 $LCTL set_param mdt.*.identity_upcall=NONE
1667
1668         rc=0
1669         create_fops_nodemaps
1670         rc=$?
1671         [[ $rc != 0 ]] && error "adding fops nodemaps failed $rc"
1672
1673         do_facet mgs $LCTL nodemap_activate $active_nodemap
1674         wait_nm_sync active
1675
1676         do_facet mgs $LCTL nodemap_modify --name default \
1677                 --property admin --value 1
1678         wait_nm_sync default admin_nodemap
1679         do_facet mgs $LCTL nodemap_modify --name default \
1680                 --property trusted --value 1
1681         wait_nm_sync default trusted_nodemap
1682 }
1683
1684 nodemap_test_cleanup() {
1685         trap 0
1686         delete_fops_nodemaps
1687         rc=$?
1688         [[ $rc != 0 ]] && error "removing fops nodemaps failed $rc"
1689
1690         do_facet mgs $LCTL nodemap_modify --name default \
1691                  --property admin --value 0
1692         wait_nm_sync default admin_nodemap
1693         do_facet mgs $LCTL nodemap_modify --name default \
1694                  --property trusted --value 0
1695         wait_nm_sync default trusted_nodemap
1696
1697         do_facet mgs $LCTL nodemap_activate 0
1698         wait_nm_sync active 0
1699
1700         export SK_UNIQUE_NM=false
1701         return 0
1702 }
1703
1704 nodemap_clients_admin_trusted() {
1705         local admin=$1
1706         local tr=$2
1707         local i=0
1708         for client in $clients; do
1709                 do_facet mgs $LCTL nodemap_modify --name c0 \
1710                         --property admin --value $admin
1711                 do_facet mgs $LCTL nodemap_modify --name c0 \
1712                         --property trusted --value $tr
1713                 i=$((i + 1))
1714         done
1715         wait_nm_sync c$((i - 1)) admin_nodemap
1716         wait_nm_sync c$((i - 1)) trusted_nodemap
1717 }
1718
1719 test_16() {
1720         nodemap_version_check || return 0
1721         nodemap_test_setup 0
1722
1723         trap nodemap_test_cleanup EXIT
1724         test_fops all_off
1725         nodemap_test_cleanup
1726 }
1727 run_test 16 "test nodemap all_off fileops"
1728
1729 test_17() {
1730         if $SHARED_KEY &&
1731         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1732                 skip "Need MDS >= 2.11.55"
1733         fi
1734         local check_proj=true
1735
1736         (( $MDS1_VERSION >= $(version_code 2.14.52) )) || check_proj=false
1737
1738         nodemap_version_check || return 0
1739         nodemap_test_setup
1740
1741         trap nodemap_test_cleanup EXIT
1742         nodemap_clients_admin_trusted 0 1
1743         test_fops trusted_noadmin 1
1744         if $check_proj; then
1745                 do_facet mgs $LCTL nodemap_modify --name c0 \
1746                         --property map_mode --value projid
1747                 wait_nm_sync c0 map_mode
1748         fi
1749         test_fops trusted_noadmin 1
1750         nodemap_test_cleanup
1751 }
1752 run_test 17 "test nodemap trusted_noadmin fileops"
1753
1754 test_18() {
1755         if $SHARED_KEY &&
1756         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1757                 skip "Need MDS >= 2.11.55"
1758         fi
1759
1760         nodemap_version_check || return 0
1761         nodemap_test_setup
1762
1763         trap nodemap_test_cleanup EXIT
1764         nodemap_clients_admin_trusted 0 0
1765         test_fops mapped_noadmin 1
1766         nodemap_test_cleanup
1767 }
1768 run_test 18 "test nodemap mapped_noadmin fileops"
1769
1770 test_19() {
1771         if $SHARED_KEY &&
1772         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1773                 skip "Need MDS >= 2.11.55"
1774         fi
1775
1776         nodemap_version_check || return 0
1777         nodemap_test_setup
1778
1779         trap nodemap_test_cleanup EXIT
1780         nodemap_clients_admin_trusted 1 1
1781         test_fops trusted_admin 1
1782         nodemap_test_cleanup
1783 }
1784 run_test 19 "test nodemap trusted_admin fileops"
1785
1786 test_20() {
1787         if $SHARED_KEY &&
1788         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1789                 skip "Need MDS >= 2.11.55"
1790         fi
1791
1792         nodemap_version_check || return 0
1793         nodemap_test_setup
1794
1795         trap nodemap_test_cleanup EXIT
1796         nodemap_clients_admin_trusted 1 0
1797         test_fops mapped_admin 1
1798         nodemap_test_cleanup
1799 }
1800 run_test 20 "test nodemap mapped_admin fileops"
1801
1802 test_21() {
1803         if $SHARED_KEY &&
1804         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1805                 skip "Need MDS >= 2.11.55"
1806         fi
1807
1808         nodemap_version_check || return 0
1809         nodemap_test_setup
1810
1811         trap nodemap_test_cleanup EXIT
1812         local x=1
1813         local i=0
1814         for client in $clients; do
1815                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1816                         --property admin --value 0
1817                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1818                         --property trusted --value $x
1819                 x=0
1820                 i=$((i + 1))
1821         done
1822         wait_nm_sync c$((i - 1)) trusted_nodemap
1823
1824         test_fops mapped_trusted_noadmin
1825         nodemap_test_cleanup
1826 }
1827 run_test 21 "test nodemap mapped_trusted_noadmin fileops"
1828
1829 test_22() {
1830         if $SHARED_KEY &&
1831         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ]; then
1832                 skip "Need MDS >= 2.11.55"
1833         fi
1834
1835         nodemap_version_check || return 0
1836         nodemap_test_setup
1837
1838         trap nodemap_test_cleanup EXIT
1839         local x=1
1840         local i=0
1841         for client in $clients; do
1842                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1843                         --property admin --value 1
1844                 do_facet mgs $LCTL nodemap_modify --name c${i} \
1845                         --property trusted --value $x
1846                 x=0
1847                 i=$((i + 1))
1848         done
1849         wait_nm_sync c$((i - 1)) trusted_nodemap
1850
1851         test_fops mapped_trusted_admin
1852         nodemap_test_cleanup
1853 }
1854 run_test 22 "test nodemap mapped_trusted_admin fileops"
1855
1856 # acl test directory needs to be initialized on a privileged client
1857 nodemap_acl_test_setup() {
1858         local admin=$(do_facet mgs $LCTL get_param -n \
1859                       nodemap.c0.admin_nodemap)
1860         local trust=$(do_facet mgs $LCTL get_param -n \
1861                       nodemap.c0.trusted_nodemap)
1862
1863         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1864         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1865
1866         wait_nm_sync c0 admin_nodemap
1867         wait_nm_sync c0 trusted_nodemap
1868
1869         do_node ${clients_arr[0]} rm -rf $DIR/$tdir
1870         nm_test_mkdir
1871         do_node ${clients_arr[0]} chmod a+rwx $DIR/$tdir ||
1872                 error unable to chmod a+rwx test dir $DIR/$tdir
1873
1874         do_facet mgs $LCTL nodemap_modify --name c0 \
1875                 --property admin --value $admin
1876         do_facet mgs $LCTL nodemap_modify --name c0 \
1877                 --property trusted --value $trust
1878
1879         wait_nm_sync c0 trusted_nodemap
1880 }
1881
1882 # returns 0 if the number of ACLs does not change on the second (mapped) client
1883 # after being set on the first client
1884 nodemap_acl_test() {
1885         local user="$1"
1886         local set_client="$2"
1887         local get_client="$3"
1888         local check_setfacl="$4"
1889         local setfacl_error=0
1890         local testfile=$DIR/$tdir/$tfile
1891         local RUNAS_USER="$RUNAS_CMD -u $user"
1892         local acl_count=0
1893         local acl_count_post=0
1894
1895         nodemap_acl_test_setup
1896         sleep 5
1897
1898         do_node $set_client $RUNAS_USER touch $testfile
1899         # remove from cache, otherwise ACLs will not be fetched from server
1900         do_rpc_nodes $set_client cancel_lru_locks
1901         do_node $set_client "sync ; echo 3 > /proc/sys/vm/drop_caches"
1902
1903         # ACL masks aren't filtered by nodemap code, so we ignore them
1904         acl_count=$(do_node $get_client getfacl $testfile | grep -v mask |
1905                 wc -l)
1906         # remove from cache, otherwise ACLs will not be fetched from server
1907         do_rpc_nodes $get_client cancel_lru_locks
1908         do_node $get_client "sync ; echo 3 > /proc/sys/vm/drop_caches"
1909         do_node $set_client $RUNAS_USER setfacl -m $user:rwx $testfile ||
1910                 setfacl_error=1
1911         # remove from cache, otherwise ACLs will not be fetched from server
1912         do_rpc_nodes $set_client cancel_lru_locks
1913         do_node $set_client "sync ; echo 3 > /proc/sys/vm/drop_caches"
1914
1915         # if check setfacl is set to 1, then it's supposed to error
1916         if [ "$check_setfacl" == "1" ]; then
1917                 [ "$setfacl_error" != "1" ] && return 1
1918                 return 0
1919         fi
1920         [ "$setfacl_error" == "1" ] && echo "WARNING: unable to setfacl"
1921
1922         acl_count_post=$(do_node $get_client getfacl $testfile | grep -v mask |
1923                 wc -l)
1924         # remove from cache, otherwise ACLs will not be fetched from server
1925         do_rpc_nodes $get_client cancel_lru_locks
1926         do_node $get_client "sync ; echo 3 > /proc/sys/vm/drop_caches"
1927         [ $acl_count -eq $acl_count_post ] && return 0
1928         return 1
1929 }
1930
1931 test_23a() {
1932         [ $num_clients -lt 2 ] && skip "Need 2 clients at least" && return
1933         nodemap_version_check || return 0
1934         nodemap_test_setup
1935
1936         trap nodemap_test_cleanup EXIT
1937         # 1 trusted cluster, 1 mapped cluster
1938         local unmapped_fs=$((IDBASE+0))
1939         local unmapped_c1=$((IDBASE+5))
1940         local mapped_fs=$((IDBASE+2))
1941         local mapped_c0=$((IDBASE+4))
1942         local mapped_c1=$((IDBASE+6))
1943
1944         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
1945         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 1
1946
1947         do_facet mgs $LCTL nodemap_modify --name c1 --property admin --value 0
1948         do_facet mgs $LCTL nodemap_modify --name c1 --property trusted --value 0
1949
1950         wait_nm_sync c1 trusted_nodemap
1951
1952         # setfacl on trusted cluster to unmapped user, verify it's not seen
1953         nodemap_acl_test $unmapped_fs ${clients_arr[0]} ${clients_arr[1]} ||
1954                 error "acl count (1)"
1955
1956         # setfacl on trusted cluster to mapped user, verify it's seen
1957         nodemap_acl_test $mapped_fs ${clients_arr[0]} ${clients_arr[1]} &&
1958                 error "acl count (2)"
1959
1960         # setfacl on mapped cluster to mapped user, verify it's seen
1961         nodemap_acl_test $mapped_c1 ${clients_arr[1]} ${clients_arr[0]} &&
1962                 error "acl count (3)"
1963
1964         # setfacl on mapped cluster to unmapped user, verify error
1965         nodemap_acl_test $unmapped_fs ${clients_arr[1]} ${clients_arr[0]} 1 ||
1966                 error "acl count (4)"
1967
1968         # 2 mapped clusters
1969         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 0
1970         do_facet mgs $LCTL nodemap_modify --name c0 --property trusted --value 0
1971
1972         wait_nm_sync c0 trusted_nodemap
1973
1974         # setfacl to mapped user on c1, also mapped to c0, verify it's seen
1975         nodemap_acl_test $mapped_c1 ${clients_arr[1]} ${clients_arr[0]} &&
1976                 error "acl count (5)"
1977
1978         # setfacl to mapped user on c1, not mapped to c0, verify not seen
1979         nodemap_acl_test $unmapped_c1 ${clients_arr[1]} ${clients_arr[0]} ||
1980                 error "acl count (6)"
1981
1982         nodemap_test_cleanup
1983 }
1984 run_test 23a "test mapped regular ACLs"
1985
1986 test_23b() { #LU-9929
1987         (( num_clients >= 2 )) || skip "Need 2 clients at least"
1988         (( $MGS_VERSION >= $(version_code 2.10.53) )) ||
1989                 skip "Need MGS >= 2.10.53"
1990
1991         stack_trap "export SK_UNIQUE_NM=$SK_UNIQUE_NM"
1992         export SK_UNIQUE_NM=true
1993         nodemap_test_setup
1994         stack_trap nodemap_test_cleanup EXIT
1995
1996         local testdir=$DIR/$tdir
1997         local fs_id=$((IDBASE+10))
1998         local unmapped_id
1999         local mapped_id
2000         local fs_user
2001
2002         do_facet mgs $LCTL nodemap_modify --name c0 --property admin --value 1
2003         wait_nm_sync c0 admin_nodemap
2004         do_facet mgs $LCTL nodemap_modify --name c1 --property admin --value 1
2005         wait_nm_sync c1 admin_nodemap
2006         do_facet mgs $LCTL nodemap_modify --name c1 --property trusted --value 1
2007         wait_nm_sync c1 trusted_nodemap
2008
2009         # Add idmap $ID0:$fs_id (500:60010)
2010         do_facet mgs $LCTL nodemap_add_idmap --name c0 --idtype gid \
2011                 --idmap $ID0:$fs_id ||
2012                 error "add idmap $ID0:$fs_id to nodemap c0 failed"
2013         wait_nm_sync c0 idmap
2014
2015         # set/getfacl default acl on client 1 (unmapped gid=500)
2016         do_node ${clients_arr[0]} rm -rf $testdir
2017         do_node ${clients_arr[0]} mkdir -p $testdir
2018         echo "$testdir ACLs after mkdir:"
2019         do_node ${clients_arr[0]} getfacl $testdir
2020         # Here, USER0=$(getent passwd | grep :$ID0:$ID0: | cut -d: -f1)
2021         do_node ${clients_arr[0]} setfacl -R -d -m group:$USER0:rwx $testdir ||
2022                 error "setfacl $testdir on ${clients_arr[0]} failed"
2023         do_node ${clients_arr[0]} "sync && stat $testdir > /dev/null"
2024         do_node ${clients_arr[0]} \
2025                 $LCTL set_param -t4 -n "ldlm.namespaces.*.lru_size=clear"
2026         echo "$testdir ACLs after setfacl, on ${clients_arr[0]}:"
2027         do_node ${clients_arr[0]} getfacl $testdir
2028         unmapped_id=$(do_node ${clients_arr[0]} getfacl $testdir |
2029                         grep -E "default:group:.+:rwx" | awk -F: '{print $3}')
2030         echo unmapped_id=$unmapped_id
2031         (( unmapped_id == USER0 )) ||
2032                 error "gid=$ID0 was not unmapped correctly on ${clients_arr[0]}"
2033
2034         # getfacl default acl on client 2 (mapped gid=60010)
2035         do_node ${clients_arr[1]} \
2036                 $LCTL set_param -t4 -n "ldlm.namespaces.*.lru_size=clear"
2037         do_node ${clients_arr[1]} "sync && stat $testdir > /dev/null"
2038         echo "$testdir ACLs after setfacl, on ${clients_arr[1]}:"
2039         do_node ${clients_arr[1]} getfacl $testdir
2040         mapped_id=$(do_node ${clients_arr[1]} getfacl $testdir |
2041                         grep -E "default:group:.+:rwx" | awk -F: '{print $3}')
2042         echo mapped_id=$mapped_id
2043         [[ -n "$mapped_id" ]] || error "mapped_id empty"
2044         fs_user=$(do_node ${clients_arr[1]} getent passwd |
2045                         grep :$fs_id:$fs_id: | cut -d: -f1)
2046         [[ -n "$fs_user" ]] || fs_user=$fs_id
2047         echo fs_user=$fs_user
2048         (( mapped_id == fs_id || mapped_id == fs_user )) ||
2049                 error "Should return user $fs_user id $fs_id on client2"
2050 }
2051 run_test 23b "test mapped default ACLs"
2052
2053 test_24() {
2054         nodemap_test_setup
2055
2056         trap nodemap_test_cleanup EXIT
2057         do_nodes $(comma_list $(all_server_nodes)) $LCTL get_param -R nodemap
2058
2059         nodemap_test_cleanup
2060 }
2061 run_test 24 "check nodemap proc files for LBUGs and Oopses"
2062
2063 test_25() {
2064         local tmpfile=$(mktemp)
2065         local tmpfile2=$(mktemp)
2066         local tmpfile3=$(mktemp)
2067         local tmpfile4=$(mktemp)
2068         local subdir=c0dir
2069         local client
2070
2071         nodemap_version_check || return 0
2072
2073         # stop clients for this test
2074         zconf_umount_clients $CLIENTS $MOUNT ||
2075             error "unable to umount clients $CLIENTS"
2076
2077         export SK_UNIQUE_NM=true
2078         nodemap_test_setup
2079
2080         # enable trusted/admin for setquota call in cleanup_and_setup_lustre()
2081         i=0
2082         for client in $clients; do
2083                 do_facet mgs $LCTL nodemap_modify --name c${i} \
2084                         --property admin --value 1
2085                 do_facet mgs $LCTL nodemap_modify --name c${i} \
2086                         --property trusted --value 1
2087                 ((i++))
2088         done
2089         wait_nm_sync c$((i - 1)) trusted_nodemap
2090
2091         trap nodemap_test_cleanup EXIT
2092
2093         # create a new, empty nodemap, and add fileset info to it
2094         do_facet mgs $LCTL nodemap_add test25 ||
2095                 error "unable to create nodemap $testname"
2096         do_facet mgs $LCTL set_param -P nodemap.$testname.fileset=/$subdir ||
2097                 error "unable to add fileset info to nodemap test25"
2098
2099         wait_nm_sync test25 id
2100
2101         do_facet mgs $LCTL $nodemap_info > $tmpfile
2102         do_facet mds $LCTL $nodemap_info > $tmpfile2
2103
2104         if ! $SHARED_KEY; then
2105                 # will conflict with SK's nodemaps
2106                 cleanup_and_setup_lustre
2107         fi
2108         # stop clients for this test
2109         zconf_umount_clients $CLIENTS $MOUNT ||
2110             error "unable to umount clients $CLIENTS"
2111
2112         do_facet mgs $LCTL $nodemap_info > $tmpfile3
2113         diff -q $tmpfile3 $tmpfile >& /dev/null ||
2114                 error "$nodemap_info diff on MGS after remount"
2115
2116         do_facet mds $LCTL $nodemap_info > $tmpfile4
2117         diff -q $tmpfile4 $tmpfile2 >& /dev/null ||
2118                 error "$nodemap_info diff on MDS after remount"
2119
2120         # cleanup nodemap
2121         do_facet mgs $LCTL nodemap_del test25 ||
2122             error "cannot delete nodemap test25 from config"
2123         nodemap_test_cleanup
2124         # restart clients previously stopped
2125         zconf_mount_clients $CLIENTS $MOUNT ||
2126             error "unable to mount clients $CLIENTS"
2127
2128         rm -f $tmpfile $tmpfile2
2129         export SK_UNIQUE_NM=false
2130 }
2131 run_test 25 "test save and reload nodemap config"
2132
2133 test_25a() {
2134         local nm="c0"
2135         local info_dump=$(mktemp)
2136         local param_dump=$(mktemp)
2137
2138         (( $MGS_VERSION >= $(version_code 2.16.52) )) ||
2139                 skip "Need MGS >= 2.16.52 for updated nodemap_info"
2140
2141         nodemap_test_setup
2142         stack_trap nodemap_test_cleanup EXIT
2143
2144         if $SHARED_KEY; then
2145                 export SK_UNIQUE_NM=true
2146         fi
2147
2148         # fill some more values on nodemap
2149         # We test only local here, so no wait_nm_sync required
2150         do_facet mgs $LCTL nodemap_add_offset --name $nm \
2151                 --offset 1000000 --limit 100000 ||
2152                 error "cannot set offset $nm"
2153         do_facet mgs $LCTL nodemap_set_fileset --name $nm \
2154                 --fileset "/somedir" ||
2155                 error "unable to add fileset info"
2156
2157         # full nodemap dump
2158         do_facet mgs $LCTL $nodemap_info > $info_dump ||
2159                 error "$nodemap_info failed"
2160         stack_trap "rm -f $info_dump" EXIT
2161         do_facet mgs $LCTL get_param -R nodemap > $param_dump
2162         stack_trap "rm -f $param_dump" EXIT
2163
2164         diff -q $info_dump $param_dump >& /dev/null ||
2165                 error "$nodemap_info differs from get_param output"
2166
2167         # nodemap dump for $nm
2168         do_facet mgs $LCTL $nodemap_info --name $nm > $info_dump ||
2169                 error "$nodemap_info failed"
2170         do_facet mgs $LCTL get_param -R nodemap.$nm > $param_dump
2171
2172         diff -q $info_dump $param_dump >& /dev/null ||
2173                 error "$nodemap_info differs from get_param output"
2174
2175         # nodemap dump for $nm and property fileset
2176         do_facet mgs $LCTL $nodemap_info --name $nm \
2177                 --property fileset > $info_dump ||
2178                 error "$nodemap_info failed"
2179         do_facet mgs $LCTL get_param nodemap.$nm.fileset > $param_dump
2180
2181         diff -q $info_dump $param_dump >& /dev/null ||
2182                 error "$nodemap_info differs from get_param output"
2183
2184         # cross nodemap dump for property ranges
2185         do_facet mgs $LCTL $nodemap_info --property ranges > $info_dump ||
2186                 error "$nodemap_info failed"
2187         do_facet mgs $LCTL get_param -R nodemap.*.ranges > $param_dump
2188
2189         # back to non-nodemap setup
2190         if $SHARED_KEY; then
2191                 export SK_UNIQUE_NM=false
2192         fi
2193 }
2194 run_test 25a "test nodemap info values"
2195
2196 test_26() {
2197         nodemap_version_check || return 0
2198
2199         local large_i=32000
2200
2201         do_facet mgs "seq -f 'c%g' $large_i | xargs -n1 $LCTL nodemap_add"
2202         wait_nm_sync c$large_i admin_nodemap
2203
2204         do_facet mgs "seq -f 'c%g' $large_i | xargs -n1 $LCTL nodemap_del"
2205         wait_nm_sync c$large_i admin_nodemap
2206 }
2207 run_test 26 "test transferring very large nodemap"
2208
2209 nodemap_exercise_fileset_cleanup() {
2210         # Already mounted clients are skipped in zconf_mount_clients()
2211         for client in "${clients_arr[@]}"; do
2212                 zconf_mount_clients $client $MOUNT $MOUNT_OPTS ||
2213                         error "unable to mount client $client"
2214         done
2215 }
2216
2217 nodemap_exercise_fileset() {
2218         local have_persistent_fset_cmd
2219         local check_proj=true
2220         local loop=0
2221         local nm="$1"
2222         local subdir="subdir_${nm}"
2223         local subsubdir="subsubdir_${nm}"
2224
2225         (( $MDS1_VERSION >= $(version_code 2.14.52) )) || check_proj=false
2226
2227         # when "have_persistent_fset_cmd" is true, "lctl nodemap_set_fileset"
2228         # is persistent, otherwise "lctl set_param -P" must be used
2229         have_persistent_fset_cmd=false
2230         if (( $MGS_VERSION >= $(version_code 2.16.51) )); then
2231                 have_persistent_fset_cmd=true
2232                 subdir="thisisaverylongsubdirtotestlongfilesetsandtotestmultiplefilesetfragmentsonthenodemapiam_${nm}"
2233         fi
2234
2235         # setup
2236         if [[ "$nm" == "default" ]]; then
2237                 do_facet mgs $LCTL nodemap_activate 1
2238                 wait_nm_sync active
2239                 do_facet mgs $LCTL nodemap_modify --name default \
2240                         --property admin --value 1
2241                 do_facet mgs $LCTL nodemap_modify --name default \
2242                         --property trusted --value 1
2243                 wait_nm_sync default trusted_nodemap
2244                 check_proj=false
2245         else
2246                 nodemap_test_setup
2247         fi
2248         if $SHARED_KEY; then
2249                 export SK_UNIQUE_NM=true
2250         else
2251                 # will conflict with SK's nodemaps
2252                 trap "fileset_test_cleanup $nm" EXIT
2253         fi
2254         fileset_test_setup "$nm"
2255
2256         # add fileset info to $nm nodemap
2257         if $have_persistent_fset_cmd; then
2258                 do_facet mgs $LCTL nodemap_set_fileset --name $nm \
2259                         --fileset "/${subdir}" ||
2260                         error "can't set fileset to $nm nodemap on MGS"
2261                 # check fileset is set on local mgs node
2262                 wait_update_facet mgs "$LCTL get_param nodemap.${nm}.fileset" \
2263                                 "nodemap.${nm}.fileset=/${subdir}" ||
2264                         error "fileset /${subdir} not set on $nm nodemap"
2265         else
2266                 if ! combined_mgs_mds; then
2267                         do_facet mgs $LCTL set_param \
2268                                 nodemap.${nm}.fileset=/${subdir} ||
2269                                 error "can't set fileset /${subdir} to $nm nodemap on MGS"
2270                 fi
2271                 do_facet mgs $LCTL set_param -P \
2272                         nodemap.${nm}.fileset=/${subdir} ||
2273                         error "can't set fileset /${subdir} to $nm nodemap on servers"
2274         fi
2275
2276         # check fileset is set on remote nodes
2277         wait_nm_sync $nm fileset "nodemap.${nm}.fileset=/${subdir}"
2278
2279         if $check_proj; then
2280                 do_facet mgs $LCTL nodemap_modify --name $nm \
2281                          --property admin --value 1
2282                 wait_nm_sync $nm admin_nodemap
2283                 do_facet mgs $LCTL nodemap_modify --name $nm \
2284                          --property trusted --value 0
2285                 wait_nm_sync $nm trusted_nodemap
2286                 do_facet mgs $LCTL nodemap_modify --name $nm \
2287                          --property map_mode --value projid
2288                 wait_nm_sync $nm map_mode
2289                 do_facet mgs $LCTL nodemap_add_idmap --name $nm \
2290                          --idtype projid --idmap 1:1
2291                 do_facet mgs $LCTL nodemap_modify --name $nm \
2292                          --property deny_unknown --value 1
2293                 wait_nm_sync $nm deny_unknown
2294         fi
2295
2296         # re-start all components to verify persistence of fileset after restart
2297         stopall || error "unable to stop"
2298         # Unload modules to fully reload nodemap IAM
2299         LOAD_MODULES_REMOTE=true unload_modules ||
2300                 error "unable to unload modules"
2301         LOAD_MODULES_REMOTE=true load_modules ||
2302                 error "unable to load modules"
2303         mountmgs || error "unable to start mgs"
2304         mountmds || error "unable to start mds"
2305         mountoss || error "unable to start oss"
2306
2307         stack_trap nodemap_exercise_fileset_cleanup EXIT
2308
2309         # mount a single client for fileset testing and remount
2310         # the remaining clients later.
2311         # set some generic fileset to trigger SSK code
2312         export FILESET=/
2313         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2314                 error "unable to remount client ${clients_arr[0]}"
2315         unset FILESET
2316
2317         # test mount point content
2318         do_node ${clients_arr[0]} test -f $MOUNT/this_is_$subdir ||
2319                 error "fileset not taken into account"
2320
2321         if $check_proj; then
2322                 do_node ${clients_arr[0]} $LFS setquota -p 1 -b 10000 -B 11000 \
2323                         -i 0 -I 0 $MOUNT || error "setquota -p 1 failed"
2324                 do_node ${clients_arr[0]} $LFS setquota -p 2 -b 10000 -B 11000 \
2325                         -i 0 -I 0 $MOUNT && error "setquota -p 2 should fail"
2326         fi
2327
2328         # re-mount client with sub-subdir
2329         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2330                 error "unable to umount client ${clients_arr[0]}"
2331         export FILESET=/$subsubdir
2332         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2333                 error "unable to remount client ${clients_arr[0]}"
2334         unset FILESET
2335
2336         # test mount point content
2337         do_node ${clients_arr[0]} test -f $MOUNT/this_is_$subsubdir ||
2338                 error "subdir of fileset not taken into account"
2339
2340         # remove fileset info from nodemap
2341         do_facet mgs $LCTL nodemap_set_fileset --name $nm --fileset clear ||
2342                 error "unable to delete fileset info on $nm nodemap"
2343         # check whether fileset was removed on mgs
2344         wait_update_facet mgs "$LCTL get_param nodemap.${nm}.fileset" \
2345                           "nodemap.${nm}.fileset=" ||
2346                 error "fileset info still not cleared on $nm nodemap"
2347         if ! $have_persistent_fset_cmd; then
2348                 do_facet mgs $LCTL set_param -P nodemap.${nm}.fileset=clear ||
2349                 error "unable to reset fileset info on $nm nodemap"
2350         fi
2351
2352         # check whether fileset was removed on remote nodes
2353         wait_nm_sync $nm fileset "nodemap.${nm}.fileset="
2354         if ! $have_persistent_fset_cmd; then
2355                 do_facet mgs $LCTL set_param -P -d nodemap.${nm}.fileset ||
2356                         error "unable to remove fileset rule on $nm nodemap"
2357         fi
2358
2359         # re-mount client
2360         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2361                 error "unable to umount client ${clients_arr[0]}"
2362         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
2363                 error "unable to remount client ${clients_arr[0]}"
2364
2365         # test mount point content
2366         if ! $(do_node ${clients_arr[0]} test -d $MOUNT/$subdir); then
2367                 ls $MOUNT
2368                 error "fileset not cleared on $nm nodemap"
2369         fi
2370
2371         # back to non-nodemap setup
2372         if $SHARED_KEY; then
2373                 export SK_UNIQUE_NM=false
2374                 zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2375                         error "unable to umount client ${clients_arr[0]}"
2376         fi
2377         fileset_test_cleanup "$nm"
2378         if [[ "$nm" == "default" ]]; then
2379                 do_facet mgs $LCTL nodemap_modify --name default \
2380                          --property admin --value 0
2381                 do_facet mgs $LCTL nodemap_modify --name default \
2382                          --property trusted --value 0
2383                 wait_nm_sync default trusted_nodemap
2384                 do_facet mgs $LCTL nodemap_activate 0
2385                 wait_nm_sync active 0
2386                 trap 0
2387                 export SK_UNIQUE_NM=false
2388         else
2389                 nodemap_test_cleanup
2390         fi
2391         # The fileset cleanup trap is reset during nodemap clean up.
2392         # Call fileset cleanup to restart all shut down clients
2393         nodemap_exercise_fileset_cleanup
2394 }
2395
2396 test_27a() {
2397         (( $MDS1_VERSION < $(version_code 2.11.50) )) &&
2398                 skip "Need MDS >= 2.11.50"
2399
2400         # if servers run on the same node, it is impossible to tell if they get
2401         # synced with the mgs, so this test needs to be skipped
2402         if [[ $(facet_active_host mgs) == $(facet_active_host mds) ]] &&
2403            [[ $(facet_active_host mgs) == $(facet_active_host ost1) ]]; then
2404                 skip "local mode not supported"
2405         fi
2406
2407         for nm in "default" "c0"; do
2408                 if [[ "$nm" == "default" && "$SHARED_KEY" == "true" ]]; then
2409                         echo "Skipping nodemap $nm with SHARED_KEY"
2410                         continue
2411                 fi
2412
2413                 echo "Exercising fileset for nodemap $nm"
2414                 nodemap_exercise_fileset "$nm"
2415         done
2416 }
2417 run_test 27a "test fileset in various nodemaps"
2418
2419 test_27aa() { #LU-17922
2420         local idmap
2421         local id=500
2422
2423         (( $MDS1_VERSION >= $(version_code v2_15_64-86-g8445f7b92f) )) ||
2424                 skip "need MDS >= 2.15.64.86 for nodemap range"
2425
2426         do_facet mgs $LCTL nodemap_add Test17922 ||
2427                 error "unable to add Test17922 as nodemap"
2428         stack_trap "do_facet mgs $LCTL nodemap_del Test17922 || true"
2429
2430         do_facet mgs $LCTL nodemap_add_idmap --name Test17922 \
2431                  --idtype uid --idmap 500-509:10000-10009 ||
2432                  error "unable to add idmap range 500-509:10000-10009"
2433
2434         idmap=$(do_facet mgs $LCTL get_param nodemap.Test17922.idmap | grep idtype)
2435         while IFS= read -r idmap; do
2436                 if (( $id <= 509 )); then
2437                         [[ "$idmap" == *"client_id: $id"* ]] ||
2438                                 error "could not find 'client_id: ${id}' inside of ${idmap}"
2439                 fi
2440                 ((id++))
2441         done < <(echo "$idmap")
2442
2443         do_facet mgs $LCTL nodemap_del_idmap --name Test17922 \
2444                  --idtype uid --idmap 505-509:10005 ||
2445                         error "cannot delete idmap range 505-509:10005"
2446
2447         id=500
2448         idmap=$(do_facet mgs $LCTL get_param nodemap.Test17922.idmap | grep idtype)
2449         while IFS= read -r idmap; do
2450                 if (( $id <= 504 )); then
2451                         [[ "$idmap" == *"client_id: $id"* ]] ||
2452                                 error "could not find 'client_id: ${id}' inside of ${idmap}"
2453                 else
2454                         [[ "$idmap" =~ "client_id: $id" ]] &&
2455                                 error "found 'client_id: $id' in $idmap"
2456                 fi
2457                 ((id++))
2458         done < <(echo "$idmap")
2459
2460         do_facet mgs $LCTL nodemap_del_idmap --name Test17922 \
2461                  --idtype uid --idmap 500-504:10000
2462
2463         #expected error, invalid secondary range supplied
2464         do_facet mgs $LCTL nodemap_add --name Test17922 \
2465                  --idtype uid --idmap 500-509:10000-10010 &&
2466                  error "Invalid range 10000-10010 was added"
2467
2468         (( $(do_facet mgs $LCTL get_param nodemap.Test17922.idmap |
2469                 grep -c idtype) == 0 )) ||
2470                 error "invalid range 10000-10010 supplied and passed"
2471
2472         do_facet mgs $LCTL nodemap_del Test17922 ||
2473                 error "failed to remove nodemap Test17922"
2474 }
2475 run_test 27aa "test nodemap idmap range"
2476
2477 test_27ab() { #LU-18109
2478         local offset_start=100000
2479         local offset_limit=200000
2480         local nid=1.1.1.1@tcp777
2481         local activedefault
2482         local nm1=Test18109
2483         local nm2=OffsetTest
2484         local squash=65534
2485         local id_start=500
2486         local expected
2487         local id=500
2488         local idmap
2489         local offset
2490
2491         (( MDS1_VERSION > $(version_code 2.16.50.170) )) ||
2492                 skip "need MDS > 2.16.50.170 for nodemap range offset"
2493
2494         do_facet mgs $LCTL nodemap_add $nm1 ||
2495                 error "unable to add $nm1 as nodemap"
2496         stack_trap "do_facet mgs $LCTL nodemap_del $nm1 || true"
2497
2498         do_facet mgs $LCTL nodemap_add $nm2 ||
2499                 error "unable to add $nm2 as nodemap"
2500         stack_trap "do_facet mgs $LCTL nodemap_del $nm2 || true"
2501
2502         do_facet mgs $LCTL nodemap_add_offset --name $nm1 \
2503                 --offset $offset_start --limit $offset_limit ||
2504                         error "cannot set offset $offset_start-$((offset_start+offset_limit-1)) for $nm1"
2505
2506         #expected error, invalid offset range supplied
2507         do_facet mgs $LCTL nodemap_add_offset --name $nm2 \
2508                 --offset $((offset_start+50000)) --limit 100000 &&
2509                         error "setting offset $((offset_start+50000))-249999 on $nm2 should fail"
2510
2511         do_facet mgs $LCTL nodemap_add_idmap --name $nm1 \
2512                  --idtype uid --idmap 500-509:0-9 ||
2513                  error "unable to add idmap range 500-509:0-9"
2514
2515         idmap=$(do_facet mgs $LCTL get_param nodemap.$nm1.idmap |
2516                 grep idtype)
2517         while IFS= read -r idmap; do
2518                 if (( $id <= 509 )); then
2519                         [[ "$idmap" == *"client_id: $id"* ]] ||
2520                                 error "could not find 'client_id: ${id}' inside of ${idmap}"
2521                 fi
2522                 ((id++))
2523         done < <(echo "$idmap")
2524
2525         do_facet mgs $LCTL nodemap_add_range --name $nm1 --range $nid ||
2526                 error "Add range $nid to $nm1 failed"
2527         do_facet mgs $LCTL nodemap_modify --name $nm1 \
2528                 --property admin --value 1 ||
2529                 error "Setting admin=1 on $nm1 failed"
2530         do_facet mgs $LCTL nodemap_modify --name $nm1 \
2531                 --property trusted --value 1 ||
2532                 error "Setting trusted=1 on $nm1 failed"
2533         do_facet mgs $LCTL nodemap_modify --name $nm1 \
2534                 --property squash_uid --value $squash ||
2535                 error "Setting squash_uid=$squash on $nm1 failed"
2536         do_facet mgs $LCTL nodemap_modify --name $nm1 \
2537                 --property squash_gid --value $squash ||
2538                 error "Setting squash_gid=$squash on $nm1 failed"
2539
2540         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
2541         if ((activedefault != 1)); then
2542                 do_facet mgs $LCTL nodemap_modify --name default \
2543                         --property trusted --value 1
2544                 do_facet mgs $LCTL nodemap_modify --name default \
2545                         --property admin --value 1
2546                 do_facet mgs $LCTL nodemap_activate 1
2547                 wait_nm_sync active
2548                 stack_trap cleanup_active EXIT
2549         fi
2550
2551         if (( MDS1_VERSION >= $(version_code 2.16.51.45) )); then
2552                 # with admin=1, we expect root to be offset
2553                 id=0
2554                 expected=$offset_start
2555                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2556                         --idtype uid --id $id)
2557                 ((idmap == expected)) ||
2558                         error "uid $id should be mapped to $expected"
2559                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2560                         --idtype gid --id $id)
2561                 ((idmap == expected)) ||
2562                         error "gid $id should be mapped to $expected"
2563                 # with trusted=1, we expect ids to be offset
2564                 id=$((id_start+1))
2565                 expected=$((offset_start+id_start+1))
2566                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2567                         --idtype uid --id $id)
2568                 ((idmap == expected)) ||
2569                         error "uid $id should be mapped to $expected"
2570                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2571                         --idtype gid --id $id)
2572                 ((idmap == expected)) ||
2573                         error "gid $id should be mapped to $expected"
2574
2575                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2576                         --property trusted --value 0 ||
2577                         error "Setting trusted=0 on $nm1 failed"
2578
2579                 # with trusted=0, we expect uid to be mapped+offset,
2580                 # gid to be squashed+offset
2581                 expected=$((offset_start+1))
2582                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2583                         --idtype uid --id $id)
2584                 ((idmap == expected)) ||
2585                         error "uid $id should be mapped to $expected"
2586                 expected=$((offset_start+squash))
2587                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2588                         --idtype gid --id $id)
2589                 ((idmap == expected)) ||
2590                         error "gid $id should be mapped to $expected"
2591
2592                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2593                         --property admin --value 0 ||
2594                         error "Setting admin=0 on $nm1 failed"
2595
2596                 # with admin=0, we expect root to be squashed+offset
2597                 id=0
2598                 expected=$((offset_start+squash))
2599                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2600                         --idtype uid --id $id)
2601                 ((idmap == expected)) ||
2602                         error "uid $id should be mapped to $expected"
2603                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2604                         --idtype gid --id $id)
2605                 ((idmap == expected)) ||
2606                         error "gid $id should be mapped to $expected"
2607
2608                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2609                         --property admin --value 1 ||
2610                         error "Setting admin=1 on $nm1 failed"
2611                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2612                         --property trusted --value 1 ||
2613                         error "Setting trusted=1 on $nm1 failed"
2614         fi
2615
2616         do_facet mgs $LCTL nodemap_del_idmap --name $nm1 \
2617                  --idtype uid --idmap 500-509:0 ||
2618                         error "cannot delete idmap range 500-509:0"
2619
2620         #expected error, invalid secondary range supplied
2621         do_facet mgs $LCTL nodemap_add_idmap --name $nm1 \
2622                  --idtype uid --idmap 500-509:200000-200010 &&
2623                  error "Invalid range 200000-200010 was supplied"
2624
2625         (( $(do_facet mgs $LCTL get_param nodemap.$nm1.idmap |
2626                 grep -c idtype) == 0 )) ||
2627                 error "invalid range 200000-200010 supplied and passed"
2628
2629         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2630                  grep start_uid)
2631         [[ "$offset" == *"start_uid: $offset_start"* ]] ||
2632                 error "expected start_uid of $offset_start not found before remounting"
2633
2634         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2635                  grep limit_uid)
2636         [[ "$offset" == *"limit_uid: $offset_limit"* ]] ||
2637                 error "expected limit_uid of $offset_limit not found before remounting"
2638
2639         if (( MDS1_VERSION >= $(version_code 2.16.51.45) )); then
2640                 # with admin=1, we expect root to be offset
2641                 id=0
2642                 expected=$offset_start
2643                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2644                         --idtype uid --id $id)
2645                 ((idmap == expected)) ||
2646                         error "uid $id should be mapped to $expected"
2647                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2648                         --idtype gid --id $id)
2649                 ((idmap == expected)) ||
2650                         error "gid $id should be mapped to $expected"
2651                 # with trusted=1, we expect ids to be offset
2652                 id=$((id_start+1))
2653                 expected=$((offset_start+id_start+1))
2654                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2655                         --idtype uid --id $id)
2656                 ((idmap == expected)) ||
2657                         error "uid $id should be mapped to $expected"
2658                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2659                         --idtype gid --id $id)
2660                 ((idmap == expected)) ||
2661                         error "gid $id should be mapped to $expected"
2662
2663                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2664                         --property trusted --value 0 ||
2665                         error "Setting trusted=0 on $nm1 failed"
2666
2667                 # with trusted=0, we expect uid to be squashed+offset
2668                 expected=$((offset_start+squash))
2669                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2670                         --idtype uid --id $id)
2671                 ((idmap == expected)) ||
2672                         error "uid $id should be mapped to $expected"
2673                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2674                         --idtype gid --id $id)
2675                 ((idmap == expected)) ||
2676                         error "gid $id should be mapped to $expected"
2677
2678                 do_facet mgs $LCTL nodemap_modify --name $nm1 \
2679                         --property admin --value 0 ||
2680                         error "Setting admin=0 on $nm1 failed"
2681
2682                 # with admin=0, we expect root to be squashed+offset
2683                 id=0
2684                 expected=$((offset_start+squash))
2685                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2686                         --idtype uid --id $id)
2687                 ((idmap == expected)) ||
2688                         error "uid $id should be mapped to $expected"
2689                 idmap=$(do_facet mgs $LCTL nodemap_test_id --nid $nid \
2690                         --idtype gid --id $id)
2691                 ((idmap == expected)) ||
2692                         error "gid $id should be mapped to $expected"
2693         fi
2694
2695         stopall || error "failed to unmount servers"
2696         setupall || error "failed to remount servers"
2697
2698         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2699                  grep start_uid)
2700         [[ "$offset" == *"start_uid: $offset_start"* ]] ||
2701                 error "expected start_uid of $offset_start not found after remounting"
2702
2703         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2704                  grep limit_uid)
2705         [[ "$offset" == *"limit_uid: $offset_limit"* ]] ||
2706                 error "expected limit_uid of $offset_limit not found after remounting"
2707
2708         do_facet mgs $LCTL nodemap_del_offset --name $nm1 ||
2709                 error "cannot del offset from $nm1"
2710
2711         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2712                  grep start_uid)
2713         [[ "$offset" == *"start_uid: 0"* ]] ||
2714                 error "expected start_uid 0, found $offset"
2715
2716         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2717                  grep limit_uid)
2718         [[ "$offset" == *"limit_uid: 0"* ]] ||
2719                 error "expected limit_uid 0, found $offset"
2720
2721         stopall || error "failed to unmount servers"
2722         setupall || error "failed to remount servers"
2723
2724         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2725                  grep start_uid)
2726         [[ "$offset" == *"start_uid: 0"* ]] ||
2727                 error "expected start_uid 0, found $offset after remounting"
2728
2729         offset=$(do_facet mgs $LCTL get_param nodemap.$nm1.offset |
2730                  grep limit_uid)
2731         [[ "$offset" == *"limit_uid: 0"* ]] ||
2732                 error "expected limit_uid 0, found $offset after remounting"
2733
2734         do_facet mgs $LCTL nodemap_del $nm1 ||
2735                 error "failed to remove nodemap $nm1"
2736
2737         do_facet mgs $LCTL nodemap_del $nm2 ||
2738                 error "failed to remove nodemap $nm2"
2739 }
2740 run_test 27ab "test nodemap idmap offset"
2741
2742 test_27b() { #LU-10703
2743         [ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
2744                 skip "Need MDS >= 2.11.50"
2745         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs"
2746
2747         # if servers run on the same node, it is impossible to tell if they get
2748         # synced with the mgs, so this test needs to be skipped
2749         if [ $(facet_active_host mgs) == $(facet_active_host mds) ] &&
2750            [ $(facet_active_host mgs) == $(facet_active_host ost1) ]; then
2751                 skip "local mode not supported"
2752         fi
2753
2754         nodemap_test_setup
2755         trap nodemap_test_cleanup EXIT
2756
2757         # Add the nodemaps and set their filesets
2758         for i in $(seq 1 $MDSCOUNT); do
2759                 do_facet mgs $LCTL nodemap_del nm$i 2>/dev/null
2760                 do_facet mgs $LCTL nodemap_add nm$i ||
2761                         error "add nodemap nm$i failed"
2762                 wait_nm_sync nm$i "" "" "-N"
2763
2764                 if ! combined_mgs_mds; then
2765                         do_facet mgs \
2766                                 $LCTL set_param nodemap.nm$i.fileset=/dir$i ||
2767                                 error "set nm$i.fileset=/dir$i failed on MGS"
2768                 fi
2769                 do_facet mgs $LCTL set_param -P nodemap.nm$i.fileset=/dir$i ||
2770                         error "set nm$i.fileset=/dir$i failed on servers"
2771                 wait_nm_sync nm$i fileset "nodemap.nm$i.fileset=/dir$i"
2772         done
2773
2774         # Check if all the filesets are correct
2775         for i in $(seq 1 $MDSCOUNT); do
2776                 fileset=$(do_facet mds$i \
2777                           $LCTL get_param -n nodemap.nm$i.fileset)
2778                 [ "$fileset" = "/dir$i" ] ||
2779                         error "nm$i.fileset $fileset != /dir$i on mds$i"
2780                 do_facet mgs $LCTL set_param -P -d nodemap.nm$i.fileset ||
2781                         error "unable to remove fileset rule for nm$i nodemap"
2782                 do_facet mgs $LCTL nodemap_del nm$i ||
2783                         error "delete nodemap nm$i failed"
2784         done
2785
2786         nodemap_test_cleanup
2787 }
2788 run_test 27b "The new nodemap won't clear the old nodemap's fileset"
2789
2790 test_28() {
2791         if ! $SHARED_KEY; then
2792                 skip "need shared key feature for this test" && return
2793         fi
2794         mkdir -p $DIR/$tdir || error "mkdir failed"
2795         touch $DIR/$tdir/$tdir.out || error "touch failed"
2796         if [ ! -f $DIR/$tdir/$tdir.out ]; then
2797                 error "read before rotation failed"
2798         fi
2799         # check srpc_contexts is valid YAML
2800         $LCTL get_param -n *.*.srpc_contexts 2>/dev/null | verify_yaml ||
2801                 error "srpc_contexts is not valid YAML"
2802         # store top key identity to ensure rotation has occurred
2803         SK_IDENTITY_OLD=$($LCTL get_param -n *.*.srpc_contexts 2>/dev/null |
2804                        head -n 1 | awk 'BEGIN{RS=", "} $1=="expire:"{print $2}')
2805         do_facet $SINGLEMDS lfs flushctx ||
2806                  error "could not run flushctx on $SINGLEMDS"
2807         sleep 5
2808         lfs flushctx || error "could not run flushctx on client"
2809         sleep 5
2810         # verify new key is in place
2811         SK_IDENTITY_NEW=$($LCTL get_param -n *.*.srpc_contexts 2>/dev/null |
2812                        head -n 1 | awk 'BEGIN{RS=", "} $1=="expire:"{print $2}')
2813         if [ $SK_IDENTITY_OLD == $SK_IDENTITY_NEW ]; then
2814                 error "key did not rotate correctly"
2815         fi
2816         if [ ! -f $DIR/$tdir/$tdir.out ]; then
2817                 error "read after rotation failed"
2818         fi
2819 }
2820 run_test 28 "check shared key rotation method"
2821
2822 test_29() {
2823         if ! $SHARED_KEY; then
2824                 skip "need shared key feature for this test" && return
2825         fi
2826         if [ $SK_FLAVOR != "ski" ] && [ $SK_FLAVOR != "skpi" ]; then
2827                 skip "test only valid if integrity is active"
2828         fi
2829         rm -r $DIR/$tdir
2830         mkdir $DIR/$tdir || error "mkdir"
2831         touch $DIR/$tdir/$tfile || error "touch"
2832         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2833                 error "unable to umount clients"
2834         do_node ${clients_arr[0]} "keyctl show |
2835                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2836         OLD_SK_PATH=$SK_PATH
2837         export SK_PATH=/dev/null
2838         if zconf_mount_clients ${clients_arr[0]} $MOUNT; then
2839                 export SK_PATH=$OLD_SK_PATH
2840                 do_node ${clients_arr[0]} "ls $DIR/$tdir/$tfile"
2841                 if [ $? -eq 0 ]; then
2842                         error "able to mount and read without key"
2843                 else
2844                         error "able to mount without key"
2845                 fi
2846         else
2847                 export SK_PATH=$OLD_SK_PATH
2848                 do_node ${clients_arr[0]} "keyctl show |
2849                         awk '/lustre/ { print \\\$1 }' |
2850                         xargs -IX keyctl unlink X"
2851         fi
2852         zconf_mount_clients ${clients_arr[0]} $MOUNT ||
2853                 error "unable to mount clients"
2854 }
2855 run_test 29 "check for missing shared key"
2856
2857 test_30() {
2858         if ! $SHARED_KEY; then
2859                 skip "need shared key feature for this test" && return
2860         fi
2861         if [ $SK_FLAVOR != "ski" ] && [ $SK_FLAVOR != "skpi" ]; then
2862                 skip "test only valid if integrity is active"
2863         fi
2864         mkdir -p $DIR/$tdir || error "mkdir failed"
2865         touch $DIR/$tdir/$tdir.out || error "touch failed"
2866         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2867                 error "unable to umount clients"
2868         # unload keys from ring
2869         do_node ${clients_arr[0]} "keyctl show |
2870                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2871         # generate key with bogus filesystem name
2872         do_node ${clients_arr[0]} "$LGSS_SK -w $SK_PATH/$FSNAME-bogus.key \
2873                 -f $FSNAME.bogus -t client -d /dev/urandom" ||
2874                 error "lgss_sk failed (1)"
2875         do_facet $SINGLEMDS lfs flushctx || error "could not run flushctx"
2876         OLD_SK_PATH=$SK_PATH
2877         export SK_PATH=$SK_PATH/$FSNAME-bogus.key
2878         if zconf_mount_clients ${clients_arr[0]} $MOUNT; then
2879                 SK_PATH=$OLD_SK_PATH
2880                 do_node ${clients_arr[0]} "ls $DIR/$tdir/$tdir.out"
2881                 if [ $? -eq 0 ]; then
2882                         error "mount and read file with invalid key"
2883                 else
2884                         error "mount with invalid key"
2885                 fi
2886         fi
2887         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
2888                 error "unable to umount clients"
2889         # unload keys from ring
2890         do_node ${clients_arr[0]} "keyctl show |
2891                 awk '/lustre/ { print \\\$1 }' | xargs -IX keyctl unlink X"
2892         rm -f $SK_PATH
2893         SK_PATH=$OLD_SK_PATH
2894         zconf_mount_clients ${clients_arr[0]} $MOUNT ||
2895                 error "unable to mount clients"
2896 }
2897 run_test 30 "check for invalid shared key"
2898
2899 basic_ios() {
2900         local flvr=$1
2901
2902         mkdir -p $DIR/$tdir/dir0 || error "mkdir $flvr"
2903         touch $DIR/$tdir/dir0/f0 || error "touch $flvr"
2904         ls $DIR/$tdir/dir0 || error "ls $flvr"
2905         dd if=/dev/zero of=$DIR/$tdir/dir0/f0 conv=fsync bs=1M count=10 \
2906                 >& /dev/null || error "dd $flvr"
2907         rm -f $DIR/$tdir/dir0/f0 || error "rm $flvr"
2908         rmdir $DIR/$tdir/dir0 || error "rmdir $flvr"
2909
2910         sync ; sync
2911         echo 3 > /proc/sys/vm/drop_caches
2912 }
2913
2914 cleanup_30b() {
2915         # restore clients' idle_timeout
2916         for c in ${clients//,/ }; do
2917                 param=IDLETIME_$(echo $c | cut -d'.' -f1 | sed s+-+_+g)
2918                 do_node $c "lctl set_param osc.*.idle_timeout=${!param}"
2919         done
2920 }
2921
2922 test_30b() {
2923         local save_flvr=$SK_FLAVOR
2924
2925         if ! $SHARED_KEY; then
2926                 skip "need shared key feature for this test"
2927         fi
2928
2929         # save clients' idle_timeout, and set all to 0 for this test,
2930         # as we do not want connections to go idle
2931         for c in ${clients//,/ }; do
2932                 param=IDLETIME_$(echo $c | cut -d'.' -f1 | sed s+-+_+g)
2933                 idle=$(do_node $c lctl get_param -n osc.*.idle_timeout |
2934                         head -n1)
2935                 eval export $param=\$idle
2936                 do_node $c lctl set_param osc.*.idle_timeout=0
2937         done
2938
2939         stack_trap cleanup_30b EXIT
2940         stack_trap restore_to_default_flavor EXIT
2941
2942         lfs mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
2943         lfs setstripe -c -1 $DIR/$tdir/fileA ||
2944                 error "setstripe $DIR/$tdir/fileA failed"
2945         echo 30b > $DIR/$tdir/fileA ||
2946                 error "wrtie to $DIR/$tdir/fileA failed"
2947
2948         for flvr in skn ska ski skpi; do
2949                 # set flavor
2950                 SK_FLAVOR=$flvr
2951                 restore_to_default_flavor || error "cannot set $flvr flavor"
2952                 SK_FLAVOR=$save_flvr
2953
2954                 basic_ios $flvr
2955         done
2956 }
2957 run_test 30b "basic test of all different SSK flavors"
2958
2959 cleanup_31() {
2960         local failover_mds1=$1
2961
2962         # unmount client
2963         zconf_umount $HOSTNAME $MOUNT || error "unable to umount client"
2964
2965         # necessary to do writeconf in order to de-register
2966         # @${NETTYPE}999 nid for targets
2967         KZPOOL=$KEEP_ZPOOL
2968         export KEEP_ZPOOL="true"
2969         stopall
2970         LOAD_MODULES_REMOTE=true unload_modules
2971         LOAD_MODULES_REMOTE=true load_modules
2972
2973         # restore mgsnid on targets
2974         for ((num = 1; num <= $MDSCOUNT; num++)); do
2975                 do_facet mds$num $TUNEFS --erase-param mgsnode \
2976                         $(mdsdevname $num)
2977                 do_facet mds$num $TUNEFS --mgsnode=$MGSNID $(mdsdevname $num)
2978         done
2979         for ((num = 1; num <= $OSTCOUNT; num++)); do
2980                 do_facet ost$num $TUNEFS --erase-param mgsnode \
2981                         $(ostdevname $num)
2982                 do_facet ost$num $TUNEFS --mgsnode=$MGSNID $(ostdevname $num)
2983         done
2984
2985         do_facet mds1 $TUNEFS --erase-param failover.node $(mdsdevname 1)
2986         if [ -n "$failover_mds1" ]; then
2987                 do_facet mds1 $TUNEFS \
2988                         --servicenode=$failover_mds1 $(mdsdevname 1)
2989         else
2990                 # If no service node previously existed, setting one in test_31
2991                 # added the no_primnode flag to the target. To remove everything
2992                 # and clear the flag, add a meaningless failnode and remove it.
2993                 do_facet mds1 $TUNEFS \
2994                         --failnode=$(do_facet mds1 $LCTL list_nids | head -1) \
2995                         $(mdsdevname 1)
2996                 do_facet mds1 $TUNEFS \
2997                         --erase-param failover.node $(mdsdevname 1)
2998         fi
2999
3000         export SK_MOUNTED=false
3001         writeconf_all
3002         setupall || echo 1
3003         export KEEP_ZPOOL="$KZPOOL"
3004 }
3005
3006 test_31() {
3007         local nid=$(lctl list_nids | grep ${NETTYPE} | head -n1)
3008         local addr=${nid%@*}
3009         local net=${nid#*@}
3010         local net2=${NETTYPE}999
3011         local mdsnid=$(do_facet mds1 $LCTL list_nids | head -1)
3012         local addr1=${mdsnid%@*}
3013         local nid2=${addr}@$net2
3014         local addr2 failover_mds1
3015         local all=$(comma_list $(all_nodes))
3016
3017         export LNETCTL=$(which lnetctl 2> /dev/null)
3018
3019         (( $MDS1_VERSION >= $(version_code 2.15.0) )) ||
3020                 skip "Need MDS >= 2.15.0"
3021         [ -z "$LNETCTL" ] && skip "without lnetctl support." && return
3022         local_mode && skip "in local mode."
3023
3024         if $SHARED_KEY; then
3025                 skip "Conflicting test with SSK"
3026         fi
3027
3028         if [[ $addr1 =~ ^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$ ]]; then
3029                 local tmp=$(printf "%x" $(((0x${addr1##*:} + 11) % 65536)))
3030
3031                 addr2=${addr1%:*}:${tmp}
3032         elif [[ $addr1 =~ ^([0-9]{1,3}\.){3,3}[0-9]{1,3}$ ]]; then
3033                 addr2=${addr1%.*}.$(((${addr1##*.} + 11) % 256))
3034         elif [[ $addr1 =~ ^[0-9]+$ ]]; then
3035                 addr2=$((addr1 + 11))
3036         fi
3037
3038         # build list of interface on nodes
3039         for node in ${all//,/ }; do
3040                 infname=inf_$(echo $node | cut -d'.' -f1 | sed s+-+_+g)
3041                 itf=$(do_node $node $LNETCTL net show --net $net |
3042                       awk 'BEGIN{inf=0} \
3043                       {if (inf==1) { print $2; exit; } fi} /interfaces/{inf=1}')
3044                 eval $infname=\$itf
3045         done
3046
3047         # backup MGSNID
3048         local mgsnid_orig=$MGSNID
3049         # compute new MGSNID
3050         local mgsnid_new=${MGSNID%@*}@$net2
3051         local tgts=$(tgts_nodes)
3052
3053         # save mds failover nids for restore at cleanup
3054         failover_mds1=$(do_facet mds1 $TUNEFS --dryrun $(mdsdevname 1))
3055         if [ -n "$failover_mds1" ]; then
3056                 failover_mds1=${failover_mds1##*Parameters:}
3057                 failover_mds1=${failover_mds1%%exiting*}
3058                 failover_mds1=$(echo $failover_mds1 | tr ' ' '\n' |
3059                                 grep failover.node | cut -d'=' -f2-)
3060         fi
3061         stack_trap "cleanup_31 $failover_mds1" EXIT
3062
3063         # umount client
3064         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
3065                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3066         fi
3067         if $(grep -q $MOUNT' ' /proc/mounts); then
3068                 umount_client $MOUNT || error "umount $MOUNT failed"
3069         fi
3070
3071         do_facet mgs "$LCTL set_param mgs.MGS.exports.clear=clear"
3072         do_nodes $tgts "$LCTL set_param *.${FSNAME}*.exports.clear=clear"
3073
3074         # check exports on servers are empty for client
3075         wait_update_facet_cond mgs \
3076                 "$LCTL get_param -N mgs.MGS.exports.* | grep $nid |
3077                 cut -d'.' -f4-" '!=' $nid
3078         for node in ${tgts//,/ }; do
3079                 wait_update_cond $node \
3080                         "$LCTL get_param -N *.${FSNAME}*.exports.* | grep $nid |
3081                         cut -d'.' -f4-" '!=' $nid
3082         done
3083         do_facet mgs "$LCTL get_param *.MGS*.exports.*.export"
3084         do_facet mgs "$LCTL get_param -n *.MGS*.exports.'$nid'.uuid 2>/dev/null|
3085                       grep -q -" && error "export on MGS should be empty"
3086         do_nodes $tgts "$LCTL get_param -n *.${FSNAME}*.exports.'$nid'.uuid \
3087                          2>/dev/null | grep -q -" &&
3088                 error "export on servers should be empty"
3089
3090         KZPOOL=$KEEP_ZPOOL
3091         export KEEP_ZPOOL="true"
3092         stopall || error "stopall failed"
3093         LOAD_MODULES_REMOTE=true unload_modules ||
3094                 error "Failed to unload modules"
3095
3096         # add network $net2 on all nodes
3097         do_rpc_nodes $all load_modules || error "unable to load modules on $all"
3098         for node in ${all//,/ }; do
3099                 do_node $node "$LNETCTL set discovery 0" ||
3100                         error "Failed to disable discovery on $node"
3101                 do_node $node "$LNETCTL lnet configure" ||
3102                         error "unable to configure lnet on node $node"
3103                 infname=inf_$(echo $node | cut -d'.' -f1 | sed s+-+_+g)
3104                 do_node $node "$LNETCTL net add --if ${!infname} --net $net2" ||
3105                         error "unable to configure NID on $net2 for node $node"
3106         done
3107
3108         LOAD_MODULES_REMOTE=true load_modules || error "failed to load modules"
3109
3110         # update MGSNID
3111         MGSNID=$mgsnid_orig,$mgsnid_new
3112         stack_trap "MGSNID=$mgsnid_orig" EXIT
3113
3114         # add mgsnid on @$net2 to targets
3115         for ((num = 1; num <= $MDSCOUNT; num++)); do
3116                 do_facet mds$num $TUNEFS --erase-param mgsnode \
3117                         $(mdsdevname $num)
3118                 do_facet mds$num $TUNEFS --mgsnode=$MGSNID $(mdsdevname $num)
3119         done
3120         for ((num = 1; num <= $OSTCOUNT; num++)); do
3121                 do_facet ost$num $TUNEFS --erase-param mgsnode \
3122                         $(ostdevname $num)
3123                 do_facet ost$num $TUNEFS --mgsnode=$MGSNID $(ostdevname $num)
3124         done
3125
3126         # necessary to do writeconf in order to register
3127         # new @$net2 nid for targets
3128         export SK_MOUNTED=false
3129         writeconf_all || error "writeconf failed"
3130
3131         nids="${addr1}@$net,${addr1}@$net2:${addr2}@$net,${addr2}@$net2"
3132         do_facet mds1 "$TUNEFS --servicenode="$nids" $(mdsdevname 1)" ||
3133                 error "tunefs failed"
3134
3135         setupall server_only || error "setupall failed"
3136         export KEEP_ZPOOL="$KZPOOL"
3137
3138         # on client, reconfigure LNet and turn LNet Dynamic Discovery off
3139         $LUSTRE_RMMOD || error "$LUSTRE_RMMOD failed (1)"
3140         load_modules || error "Failed to load modules"
3141         $LNETCTL set discovery 0 || error "Failed to disable discovery"
3142         $LNETCTL lnet configure ||
3143                 error "unable to configure lnet on client"
3144         infname=inf_$(echo $(hostname -s) | sed s+-+_+g)
3145         $LNETCTL net add --if ${!infname} --net $net2 ||
3146                 error "unable to configure NID on $net2 on client (1)"
3147
3148         # mount client with -o network=$net2 option
3149         mount_client $MOUNT ${MOUNT_OPTS},network=$net2 ||
3150                 error "unable to remount client"
3151
3152         # check export on MGS
3153         do_facet mgs "$LCTL get_param *.MGS*.exports.*.export"
3154         do_facet mgs "$LCTL get_param -n *.MGS*.exports.'$nid'.uuid 2>/dev/null|
3155                       grep -" &&
3156                 error "export for $nid on MGS should not exist"
3157
3158         do_facet mgs "$LCTL get_param -n *.MGS*.exports.'$nid2'.uuid"|grep - ||
3159                 error "export for $nid2 on MGS should exist"
3160
3161         # check {mdc,osc} imports
3162         $LCTL get_param mdc.${FSNAME}-*.import | grep current_connection |
3163                 grep $net2 ||
3164                 error "import for mdc should use ${addr1}@$net2"
3165         $LCTL get_param osc.${FSNAME}-*.import | grep current_connection |
3166                 grep $net2 ||
3167                 error "import for osc should use ${addr1}@$net2"
3168
3169         # no NIDs on other networks should be listed
3170         $LCTL get_param mdc.${FSNAME}-*.import | grep failover_nids |
3171             grep -w ".*@$net" &&
3172                 error "MDC import shouldn't have failnids at @$net"
3173
3174         # failover NIDs on net999 should be listed
3175         $LCTL get_param mdc.${FSNAME}-*.import | grep failover_nids |
3176             grep ${addr2}@$net2 ||
3177                 error "MDC import should have failnid ${addr2}@$net2"
3178
3179         # unmount client
3180         zconf_umount $HOSTNAME $MOUNT || error "unable to umount client"
3181
3182         do_facet mgs "$LCTL set_param mgs.MGS.exports.clear=clear"
3183         do_nodes $tgts "$LCTL set_param *.${FSNAME}*.exports.clear=clear"
3184
3185         wait_update_facet_cond mgs \
3186                 "$LCTL get_param -N mgs.MGS.exports.* | grep $nid2 |
3187                 cut -d'.' -f4-" '!=' $nid2
3188         for node in ${tgts//,/ }; do
3189                 wait_update_cond $node \
3190                         "$LCTL get_param -N *.${FSNAME}*.exports.* | grep $nid2|
3191                         cut -d'.' -f4-" '!=' $nid2
3192         done
3193         do_facet mgs "$LCTL get_param *.MGS*.exports.*.export"
3194
3195         # on client, configure LNet and turn LNet Dynamic Discovery on (default)
3196         $LUSTRE_RMMOD || error "$LUSTRE_RMMOD failed (2)"
3197         load_modules || error "Failed to load modules"
3198         $LNETCTL lnet configure || error "unable to configure lnet on client"
3199         infname=inf_$(echo $(hostname -s) | sed s+-+_+g)
3200         $LNETCTL net add --if ${!infname} --net $net2 ||
3201                 error "unable to configure NID on $net2 on client (2)"
3202
3203         # mount client with -o network=$net2 option:
3204         # should fail because of LNet Dynamic Discovery
3205         mount_client $MOUNT ${MOUNT_OPTS},network=$net2 &&
3206                 error "client mount with '-o network' option should be refused"
3207
3208         # remount with '-o network' server side option
3209         (( $MDS1_VERSION >= $(version_code 2.16.51) )) || return 0
3210
3211         KZPOOL=$KEEP_ZPOOL
3212         export KEEP_ZPOOL="true"
3213         stopall || error "stopall failed"
3214         mountmgs
3215         for ((num = 1; num <= $MDSCOUNT; num++)); do
3216                 start mds$num $(mdsdevname $num) $MDS_MOUNT_OPTS,network=$net2
3217         done
3218         for ((num = 1; num <= $OSTCOUNT; num++)); do
3219                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS,network=$net2
3220         done
3221         export KEEP_ZPOOL="$KZPOOL"
3222         sleep 5
3223
3224         # check exports on servers are empty for $net
3225         do_facet mgs "$LCTL get_param mgs.MGS.exports.*.export"
3226         wait_update_facet_cond mgs \
3227                 "$LCTL get_param -N mgs.MGS.exports.*.export | \
3228                  grep ${net}.export | cut -d'@' -f2-" '!=' ${net}.export
3229         do_nodes $tgts "$LCTL get_param *.${FSNAME}*.exports.*.export"
3230         for node in ${tgts//,/ }; do
3231                 wait_update_cond $node \
3232                         "$LCTL get_param -N *.${FSNAME}*.exports.*.export | \
3233                         grep ${net}.export | cut -d'@' -f2-" '!=' ${net}.export
3234         done
3235
3236         return 0
3237 }
3238 run_test 31 "client mount option '-o network'"
3239
3240 cleanup_32() {
3241         # umount client
3242         zconf_umount_clients ${clients_arr[0]} $MOUNT
3243
3244         # disable sk flavor enforcement on MGS
3245         set_rule _mgs any any null
3246
3247         # stop gss daemon on MGS
3248         send_sigint $mgs_HOST lsvcgssd
3249
3250         # re-start gss daemon on MDS if necessary
3251         if combined_mgs_mds ; then
3252                 start_gss_daemons $mds_HOST $LSVCGSSD "-vvv -s -m -o -z"
3253         fi
3254
3255         # restore MGS NIDs in key on MGS
3256         do_nodes $mgs_HOST "$LGSS_SK -g $MGSNID -m \
3257                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3258                 error "could not modify keyfile on MGS (3)"
3259
3260         # load modified key file on MGS
3261         do_nodes $mgs_HOST "$LGSS_SK -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3262                 error "could not load keyfile on MGS (3)"
3263
3264         # restore MGS NIDs in key on client
3265         do_nodes ${clients_arr[0]} "$LGSS_SK -g $MGSNID -m \
3266                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3267                 error "could not modify keyfile on client (3)"
3268
3269         # re-mount client
3270         MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
3271         mountcli
3272
3273         restore_to_default_flavor
3274 }
3275
3276 test_32() {
3277         local mgsnid2=$(host_nids_address $ost1_HOST $NETTYPE)@${MGSNID#*@}
3278         local mgsorig=$MGSNID
3279
3280         if ! $SHARED_KEY; then
3281                 skip "need shared key feature for this test"
3282         fi
3283
3284         stack_trap cleanup_32 EXIT
3285
3286         # restore to default null flavor
3287         save_flvr=$SK_FLAVOR
3288         SK_FLAVOR=null
3289         restore_to_default_flavor || error "cannot set null flavor"
3290         SK_FLAVOR=$save_flvr
3291
3292         # umount client
3293         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
3294                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3295         fi
3296         if $(grep -q $MOUNT' ' /proc/mounts); then
3297                 umount_client $MOUNT || error "umount $MOUNT failed"
3298         fi
3299
3300         # kill daemon on MGS to start afresh
3301         send_sigint $mgs_HOST lsvcgssd
3302
3303         # start gss daemon on MGS
3304         if combined_mgs_mds ; then
3305                 start_gss_daemons $mgs_HOST $LSVCGSSD "-vvv -s -g -m -o -z"
3306         else
3307                 start_gss_daemons $mgs_HOST $LSVCGSSD "-vvv -s -g"
3308         fi
3309
3310         # add mgs key type and MGS NIDs in key on MGS
3311         do_nodes $mgs_HOST "$LGSS_SK -t mgs,server -g $MGSNID -m \
3312                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3313                 error "could not modify keyfile on MGS (1)"
3314
3315         # load modified key file on MGS
3316         do_nodes $mgs_HOST "$LGSS_SK -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3317                 error "could not load keyfile on MGS (1)"
3318
3319         # add MGS NIDs in key on client
3320         do_nodes ${clients_arr[0]} "$LGSS_SK -g $MGSNID -m \
3321                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3322                 error "could not modify keyfile on client (1)"
3323
3324         # set perms for per-nodemap keys else permission denied
3325         do_nodes $(comma_list $(all_nodes)) \
3326                  "keyctl show | grep lustre | cut -c1-11 |
3327                                 sed -e 's/ //g;' |
3328                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
3329
3330         # re-mount client with mgssec=skn
3331         save_opts=$MOUNT_OPTS
3332         stack_trap "MOUNT_OPTS=$save_opts" EXIT
3333         if [ -z "$MOUNT_OPTS" ]; then
3334                 MOUNT_OPTS="-o mgssec=skn"
3335         else
3336                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
3337         fi
3338         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
3339                 error "mount ${clients_arr[0]} with mgssec=skn failed"
3340         MOUNT_OPTS=$save_opts
3341
3342         # umount client
3343         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
3344                 error "umount ${clients_arr[0]} failed"
3345
3346         # enforce ska flavor on MGS
3347         set_rule _mgs any any ska
3348
3349         # re-mount client without mgssec
3350         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS &&
3351                 error "mount ${clients_arr[0]} without mgssec should fail"
3352
3353         # re-mount client with mgssec=skn
3354         save_opts=$MOUNT_OPTS
3355         if [ -z "$MOUNT_OPTS" ]; then
3356                 MOUNT_OPTS="-o mgssec=skn"
3357         else
3358                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
3359         fi
3360         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS &&
3361                 error "mount ${clients_arr[0]} with mgssec=skn should fail"
3362         MOUNT_OPTS=$save_opts
3363
3364         # re-mount client with mgssec=ska
3365         save_opts=$MOUNT_OPTS
3366         if [ -z "$MOUNT_OPTS" ]; then
3367                 MOUNT_OPTS="-o mgssec=ska"
3368         else
3369                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=ska"
3370         fi
3371         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
3372                 error "mount ${clients_arr[0]} with mgssec=ska failed"
3373
3374         MGSNID=$mgsnid2:$mgsorig
3375         stack_trap "MGSNID=$mgsorig" EXIT
3376
3377         # umount client
3378         zconf_umount_clients ${clients_arr[0]} $MOUNT ||
3379                 error "umount ${clients_arr[0]} failed"
3380
3381         # add MGS NIDs in key on MGS
3382         do_nodes $mgs_HOST "$LGSS_SK -g ${MGSNID//:/,} -m \
3383                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3384                 error "could not modify keyfile on MGS (2)"
3385
3386         # load modified key file on MGS
3387         do_nodes $mgs_HOST "$LGSS_SK -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3388                 error "could not load keyfile on MGS (2)"
3389
3390         # add MGS NIDs in key on client
3391         do_nodes ${clients_arr[0]} "$LGSS_SK -g ${MGSNID//:/,} -m \
3392                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3393                 error "could not modify keyfile on client (2)"
3394
3395         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
3396                 error "mount ${clients_arr[0]} with alternate mgsnid failed"
3397 }
3398 run_test 32 "check for mgssec"
3399
3400 cleanup_33() {
3401         # disable sk flavor enforcement
3402         set_rule $FSNAME any cli2mdt null
3403         wait_flavor cli2mdt null
3404
3405         # umount client
3406         zconf_umount_clients ${clients_arr[0]} $MOUNT
3407
3408         # stop gss daemon on MGS
3409         send_sigint $mgs_HOST lsvcgssd
3410
3411         # re-start gss daemon on MDS if necessary
3412         if combined_mgs_mds ; then
3413                 start_gss_daemons $mds_HOST $LSVCGSSD "-vvv -s -m -o -z"
3414         fi
3415
3416         # re-mount client
3417         MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
3418         mountcli
3419
3420         restore_to_default_flavor
3421 }
3422
3423 test_33() {
3424         if ! $SHARED_KEY; then
3425                 skip "need shared key feature for this test"
3426         fi
3427
3428         stack_trap cleanup_33 EXIT
3429
3430         # restore to default null flavor
3431         save_flvr=$SK_FLAVOR
3432         SK_FLAVOR=null
3433         restore_to_default_flavor || error "cannot set null flavor"
3434         SK_FLAVOR=$save_flvr
3435
3436         # umount client
3437         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
3438                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3439         fi
3440         if $(grep -q $MOUNT' ' /proc/mounts); then
3441         umount_client $MOUNT || error "umount $MOUNT failed"
3442         fi
3443
3444         # kill daemon on MGS to start afresh
3445         send_sigint $mgs_HOST lsvcgssd
3446
3447         # start gss daemon on MGS
3448         if combined_mgs_mds ; then
3449                 start_gss_daemons $mgs_HOST $LSVCGSSD "-vvv -s -g -m -o -z"
3450         else
3451                 start_gss_daemons $mgs_HOST $LSVCGSSD "-vvv -s -g"
3452         fi
3453
3454         # add mgs key type and MGS NIDs in key on MGS
3455         do_nodes $mgs_HOST "$LGSS_SK -t mgs,server -g $MGSNID -m \
3456                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3457                 error "could not modify keyfile on MGS"
3458
3459         # load modified key file on MGS
3460         do_nodes $mgs_HOST "$LGSS_SK -l $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3461                 error "could not load keyfile on MGS"
3462
3463         # add MGS NIDs in key on client
3464         do_nodes ${clients_arr[0]} "$LGSS_SK -g $MGSNID -m \
3465                                 $SK_PATH/$FSNAME.key >/dev/null 2>&1" ||
3466                 error "could not modify keyfile on MGS"
3467
3468         # set perms for per-nodemap keys else permission denied
3469         do_nodes $(comma_list $(all_nodes)) \
3470                  "keyctl show | grep lustre | cut -c1-11 |
3471                                 sed -e 's/ //g;' |
3472                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
3473
3474         # re-mount client with mgssec=skn
3475         save_opts=$MOUNT_OPTS
3476         if [ -z "$MOUNT_OPTS" ]; then
3477                 MOUNT_OPTS="-o mgssec=skn"
3478         else
3479                 MOUNT_OPTS="$MOUNT_OPTS,mgssec=skn"
3480         fi
3481         zconf_mount_clients ${clients_arr[0]} $MOUNT $MOUNT_OPTS ||
3482                 error "mount ${clients_arr[0]} with mgssec=skn failed"
3483         MOUNT_OPTS=$save_opts
3484
3485         # enforce ska flavor for cli2mdt
3486         set_rule $FSNAME any cli2mdt ska
3487         wait_flavor cli2mdt ska
3488
3489         # check error message
3490         $LCTL dk | grep "faked source" &&
3491                 error "MGS connection srpc flags incorrect"
3492
3493         exit 0
3494 }
3495 run_test 33 "correct srpc flags for MGS connection"
3496
3497 cleanup_34_deny() {
3498         # restore deny_unknown
3499         do_facet mgs $LCTL nodemap_modify --name default \
3500                            --property deny_unknown --value $denydefault
3501         if [ $? -ne 0 ]; then
3502                 error_noexit "cannot reset deny_unknown on default nodemap"
3503                 return
3504         fi
3505
3506         wait_nm_sync default deny_unknown
3507 }
3508
3509 test_34() {
3510         local denynew
3511         local activedefault
3512
3513         [ $MGS_VERSION -lt $(version_code 2.12.51) ] &&
3514                 skip "deny_unknown on default nm not supported before 2.12.51"
3515
3516         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
3517
3518         if [[ "$activedefault" != "1" ]]; then
3519                 do_facet mgs $LCTL nodemap_activate 1
3520                 wait_nm_sync active
3521                 stack_trap cleanup_active EXIT
3522         fi
3523
3524         denydefault=$(do_facet mgs $LCTL get_param -n \
3525                       nodemap.default.deny_unknown)
3526         [ -z "$denydefault" ] &&
3527                 error "cannot get deny_unknown on default nodemap"
3528         if [ "$denydefault" -eq 0 ]; then
3529                 denynew=1;
3530         else
3531                 denynew=0;
3532         fi
3533
3534         do_facet mgs $LCTL nodemap_modify --name default \
3535                         --property deny_unknown --value $denynew ||
3536                 error "cannot set deny_unknown on default nodemap"
3537
3538         [ "$(do_facet mgs $LCTL get_param -n nodemap.default.deny_unknown)" \
3539                         -eq $denynew ] ||
3540                 error "setting deny_unknown on default nodemap did not work"
3541
3542         stack_trap cleanup_34_deny EXIT
3543
3544         wait_nm_sync default deny_unknown
3545 }
3546 run_test 34 "deny_unknown on default nodemap"
3547
3548 test_35() {
3549         (( $MDS1_VERSION >= $(version_code 2.13.50) )) ||
3550                 skip "Need MDS >= 2.13.50"
3551
3552         # activate changelogs
3553         changelog_register || error "changelog_register failed"
3554         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
3555         changelog_users $SINGLEMDS | grep -q $cl_user ||
3556                 error "User $cl_user not found in changelog_users"
3557         changelog_chmask ALL
3558
3559         # do some IOs
3560         mkdir $DIR/$tdir || error "failed to mkdir $tdir"
3561         touch $DIR/$tdir/$tfile || error "failed to touch $tfile"
3562
3563         # access changelogs with root
3564         changelog_dump || error "failed to dump changelogs"
3565         changelog_clear 0 || error "failed to clear changelogs"
3566
3567         # put clients in non-admin nodemap
3568         nodemap_test_setup
3569         stack_trap nodemap_test_cleanup EXIT
3570         for i in $(seq 0 $((num_clients-1))); do
3571                 do_facet mgs $LCTL nodemap_modify --name c${i} \
3572                          --property admin --value 0
3573         done
3574         for i in $(seq 0 $((num_clients-1))); do
3575                 wait_nm_sync c${i} admin_nodemap
3576         done
3577
3578         # access with mapped root
3579         changelog_dump && error "dump changelogs should have failed"
3580         changelog_clear 0 && error "clear changelogs should have failed"
3581
3582         exit 0
3583 }
3584 run_test 35 "Check permissions when accessing changelogs"
3585
3586 setup_dummy_key() {
3587         local mode='\x00\x00\x00\x00'
3588         local raw="$(printf ""\\\\x%02x"" {0..63})"
3589         local size
3590         local key
3591
3592         [[ $(lscpu) =~ Byte\ Order.*Little ]] && size='\x40\x00\x00\x00' ||
3593                 size='\x00\x00\x00\x40'
3594         key="${mode}${raw}${size}"
3595         echo -n -e "${key}" | keyctl padd logon fscrypt:4242424242424242 @s
3596 }
3597
3598 insert_enc_key() {
3599         cancel_lru_locks
3600         sync ; echo 3 > /proc/sys/vm/drop_caches
3601         setup_dummy_key
3602 }
3603
3604 remove_enc_key() {
3605         local dummy_key
3606
3607         $LCTL set_param -n ldlm.namespaces.*.lru_size=clear
3608         sync ; echo 3 > /proc/sys/vm/drop_caches
3609         dummy_key=$(keyctl show | awk '$7 ~ "^fscrypt:" {print $1}')
3610         if [ -n "$dummy_key" ]; then
3611                 keyctl revoke $dummy_key
3612                 keyctl reap
3613         fi
3614 }
3615
3616 remount_client_normally() {
3617         # remount client without dummy encryption key
3618         if is_mounted $MOUNT; then
3619                 umount_client $MOUNT || error "umount $MOUNT failed"
3620         fi
3621         mount_client $MOUNT ${MOUNT_OPTS} ||
3622                 error "remount failed"
3623
3624         if is_mounted $MOUNT2; then
3625                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
3626         fi
3627         if [ "$MOUNT_2" ]; then
3628                 mount_client $MOUNT2 ${MOUNT_OPTS} ||
3629                         error "remount failed"
3630         fi
3631
3632         remove_enc_key
3633         wait_ssk
3634 }
3635
3636 remount_client_dummykey() {
3637         insert_enc_key
3638
3639         # remount client with dummy encryption key
3640         if is_mounted $MOUNT; then
3641                 umount_client $MOUNT || error "umount $MOUNT failed"
3642         fi
3643         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption ||
3644                 error "remount failed"
3645
3646         wait_ssk
3647 }
3648
3649 setup_for_enc_tests() {
3650         # remount client with test_dummy_encryption option
3651         if is_mounted $MOUNT; then
3652                 umount_client $MOUNT || error "umount $MOUNT failed"
3653         fi
3654         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption ||
3655                 error "mount with '-o test_dummy_encryption' failed"
3656
3657         wait_ssk
3658
3659         # this directory will be encrypted, because of dummy mode
3660         mkdir $DIR/$tdir
3661 }
3662
3663 cleanup_for_enc_tests() {
3664         rm -rf $DIR/$tdir $*
3665
3666         remount_client_normally
3667 }
3668
3669 cleanup_nodemap_after_enc_tests() {
3670         umount_client $MOUNT || true
3671
3672         if (( MGS_VERSION >= $(version_code 2.13.55) )); then
3673                 do_facet mgs $LCTL nodemap_modify --name default \
3674                         --property forbid_encryption --value 0
3675                 if (( MGS_VERSION >= $(version_code 2.15.51) )); then
3676                         do_facet mgs $LCTL nodemap_modify --name default \
3677                                 --property readonly_mount --value 0
3678                 fi
3679         fi
3680         do_facet mgs $LCTL nodemap_modify --name default \
3681                 --property trusted --value 0
3682         do_facet mgs $LCTL nodemap_modify --name default \
3683                 --property admin --value 0
3684         do_facet mgs $LCTL nodemap_activate 0
3685
3686         if (( MGS_VERSION >= $(version_code 2.13.55) )); then
3687                 wait_nm_sync default forbid_encryption '' inactive
3688                 if (( MGS_VERSION >= $(version_code 2.15.51) )); then
3689                         wait_nm_sync default readonly_mount '' inactive
3690                 fi
3691         fi
3692         wait_nm_sync default trusted_nodemap '' inactive
3693         wait_nm_sync default admin_nodemap '' inactive
3694         wait_nm_sync active
3695
3696         mount_client $MOUNT ${MOUNT_OPTS} || error "re-mount failed"
3697         wait_ssk
3698 }
3699
3700 test_36() {
3701         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3702                 skip "client encryption not supported"
3703
3704         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3705                 skip "need dummy encryption support"
3706
3707         stack_trap cleanup_for_enc_tests EXIT
3708
3709         # first make sure it is possible to enable encryption
3710         # when nodemap is not active
3711         setup_for_enc_tests
3712         rmdir $DIR/$tdir
3713         umount_client $MOUNT || error "umount $MOUNT failed (1)"
3714
3715         # then activate nodemap, and retry
3716         # should succeed as encryption is not forbidden on default nodemap
3717         # by default
3718         stack_trap cleanup_nodemap_after_enc_tests EXIT
3719         do_facet mgs $LCTL nodemap_activate 1
3720         wait_nm_sync active
3721         forbid=$(do_facet mgs lctl get_param -n nodemap.default.forbid_encryption)
3722         [ $forbid -eq 0 ] || error "wrong default value for forbid_encryption"
3723         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption ||
3724                 error "mount '-o test_dummy_encryption' failed with default"
3725         umount_client $MOUNT || error "umount $MOUNT failed (2)"
3726
3727         # then forbid encryption, and retry
3728         do_facet mgs $LCTL nodemap_modify --name default \
3729                 --property forbid_encryption --value 1
3730         wait_nm_sync default forbid_encryption
3731         mount_client $MOUNT ${MOUNT_OPTS},test_dummy_encryption &&
3732                 error "mount '-o test_dummy_encryption' should have failed"
3733         return 0
3734 }
3735 run_test 36 "control if clients can use encryption"
3736
3737 test_37() {
3738         local testfile=$DIR/$tdir/$tfile
3739         local tmpfile=$TMP/abc
3740         local objdump=$TMP/objdump
3741
3742         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3743                 skip "client encryption not supported"
3744
3745         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3746                 skip "need dummy encryption support"
3747
3748         [ "$ost1_FSTYPE" = ldiskfs ] || skip "ldiskfs only test (using debugfs)"
3749
3750         stack_trap cleanup_for_enc_tests EXIT
3751         setup_for_enc_tests
3752
3753         # write a few bytes in file
3754         echo "abc" > $tmpfile
3755         $LFS setstripe -c1 -i0 $testfile
3756         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
3757         do_facet ost1 "sync; sync"
3758
3759         # check that content on ost is encrypted
3760         local fids=($($LFS getstripe $testfile | grep 0x))
3761         local fid="${fids[3]}:${fids[2]}:0"
3762         local objpath=$(ost_fid2_objpath ost1 $fid)
3763
3764         do_facet ost1 "$DEBUGFS -c -R 'cat $objpath' $(ostdevname 1)" > $objdump
3765         cmp -s $objdump $tmpfile &&
3766                 error "file $testfile is not encrypted on ost"
3767
3768         # check that in-memory representation of file is correct
3769         cmp -bl ${tmpfile} ${testfile} ||
3770                 error "file $testfile is corrupted in memory"
3771
3772         cancel_lru_locks osc ; cancel_lru_locks mdc
3773
3774         # check that file read from server is correct
3775         cmp -bl ${tmpfile} ${testfile} ||
3776                 error "file $testfile is corrupted on server"
3777
3778         rm -f $tmpfile $objdump
3779 }
3780 run_test 37 "simple encrypted file"
3781
3782 test_38() {
3783         local testfile=$DIR/$tdir/$tfile
3784         local tmpfile=$TMP/abc
3785         local blksz
3786         local filesz
3787         local bsize
3788         local pagesz=$(getconf PAGE_SIZE)
3789
3790         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3791                 skip "client encryption not supported"
3792
3793         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3794                 skip "need dummy encryption support"
3795
3796         stack_trap cleanup_for_enc_tests EXIT
3797         setup_for_enc_tests
3798
3799         # get block size on ost
3800         blksz=$($LCTL get_param osc.$FSNAME*.import |
3801                 awk '/grant_block_size:/ { print $2; exit; }')
3802         # write a few bytes in file at offset $blksz
3803         echo "abc" > $tmpfile
3804         $LFS setstripe -c1 -i0 $testfile
3805         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$blksz \
3806                 oflag=seek_bytes conv=fsync
3807
3808         blksz=$(($blksz > $pagesz ? $blksz : $pagesz))
3809         # check that in-memory representation of file is correct
3810         bsize=$(stat --format=%B $testfile)
3811         filesz=$(stat --format=%b $testfile)
3812         filesz=$((filesz*bsize))
3813         [ $filesz -le $blksz ] ||
3814                 error "file $testfile is $filesz long in memory"
3815
3816         cancel_lru_locks osc ; cancel_lru_locks mdc
3817
3818         # check that file read from server is correct
3819         bsize=$(stat --format=%B $testfile)
3820         filesz=$(stat --format=%b $testfile)
3821         filesz=$((filesz*bsize))
3822         [ $filesz -le $blksz ] ||
3823                 error "file $testfile is $filesz long on server"
3824
3825         rm -f $tmpfile
3826 }
3827 run_test 38 "encrypted file with hole"
3828
3829 test_39() {
3830         local testfile=$DIR/$tdir/$tfile
3831         local tmpfile=$TMP/abc
3832
3833         $LCTL get_param mdc.*.import | grep -q client_encryption ||
3834                 skip "client encryption not supported"
3835
3836         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
3837                 skip "need dummy encryption support"
3838
3839         stack_trap cleanup_for_enc_tests EXIT
3840         setup_for_enc_tests
3841
3842         # write a few bytes in file
3843         echo "abc" > $tmpfile
3844         $LFS setstripe -c1 -i0 $testfile
3845         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
3846
3847         # write a few more bytes in the same page
3848         dd if=$tmpfile of=$testfile bs=4 count=1 seek=1024 oflag=seek_bytes \
3849                 conv=fsync,notrunc
3850
3851         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=1024 oflag=seek_bytes \
3852                 conv=fsync,notrunc
3853
3854         # check that in-memory representation of file is correct
3855         cmp -bl $tmpfile $testfile ||
3856                 error "file $testfile is corrupted in memory"
3857
3858         cancel_lru_locks osc ; cancel_lru_locks mdc
3859
3860         # check that file read from server is correct
3861         cmp -bl $tmpfile $testfile ||
3862                 error "file $testfile is corrupted on server"
3863
3864         rm -f $tmpfile
3865 }
3866 run_test 39 "rewrite data in already encrypted page"
3867
3868 test_40() {
3869         local testfile=$DIR/$tdir/$tfile
3870         local tmpfile=$TMP/abc
3871         local tmpfile2=$TMP/abc2
3872         local seek
3873         local filesz
3874         #define LUSTRE_ENCRYPTION_UNIT_SIZE   (1 << 12)
3875         local UNIT_SIZE=$((1 << 12))
3876         local scrambledfile
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         # write a few bytes in file
3890         echo "abc" > $tmpfile
3891         $LFS setstripe -c1 -i0 $testfile
3892         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync
3893
3894         # check that in-memory representation of file is correct
3895         cmp -bl $tmpfile $testfile ||
3896                 error "file $testfile is corrupted in memory (1)"
3897
3898         cancel_lru_locks osc ; cancel_lru_locks mdc
3899
3900         # check that file read from server is correct
3901         cmp -bl $tmpfile $testfile ||
3902                 error "file $testfile is corrupted on server (1)"
3903
3904         # write a few other bytes in same page
3905         dd if=$tmpfile of=$testfile bs=4 count=1 seek=256 oflag=seek_bytes \
3906                 conv=fsync,notrunc
3907
3908         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=256 oflag=seek_bytes \
3909                 conv=fsync,notrunc
3910
3911         # check that in-memory representation of file is correct
3912         cmp -bl $tmpfile $testfile ||
3913                 error "file $testfile is corrupted in memory (2)"
3914
3915         cancel_lru_locks osc ; cancel_lru_locks mdc
3916
3917         # check that file read from server is correct
3918         cmp -bl $tmpfile $testfile ||
3919                 error "file $testfile is corrupted on server (2)"
3920
3921         rm -f $testfile $tmpfile
3922         cancel_lru_locks osc ; cancel_lru_locks mdc
3923
3924         # write a few bytes in file, at end of first page
3925         echo "abc" > $tmpfile
3926         $LFS setstripe -c1 -i0 $testfile
3927         seek=$(getconf PAGESIZE)
3928         seek=$((seek - 4))
3929         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3930                 conv=fsync,notrunc
3931
3932         # write a few other bytes at beginning of first page
3933         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
3934
3935         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3936                 conv=fsync,notrunc
3937
3938         # check that in-memory representation of file is correct
3939         cmp -bl $tmpfile $testfile ||
3940                 error "file $testfile is corrupted in memory (3)"
3941
3942         cancel_lru_locks osc ; cancel_lru_locks mdc
3943
3944         # check that file read from server is correct
3945         cmp -bl $tmpfile $testfile ||
3946                 error "file $testfile is corrupted on server (3)"
3947
3948         rm -f $testfile $tmpfile
3949         cancel_lru_locks osc ; cancel_lru_locks mdc
3950
3951         # write a few bytes in file, at beginning of second page
3952         echo "abc" > $tmpfile
3953         $LFS setstripe -c1 -i0 $testfile
3954         seek=$(getconf PAGESIZE)
3955         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3956                 conv=fsync,notrunc
3957         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3958                 conv=fsync,notrunc
3959
3960         # write a few other bytes at end of first page
3961         seek=$((seek - 4))
3962         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
3963                 conv=fsync,notrunc
3964         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
3965                 conv=fsync,notrunc
3966
3967         # check that in-memory representation of file is correct
3968         cmp -bl $tmpfile2 $testfile ||
3969                 error "file $testfile is corrupted in memory (4)"
3970
3971         cancel_lru_locks osc ; cancel_lru_locks mdc
3972
3973         # check that file read from server is correct
3974         cmp -bl $tmpfile2 $testfile ||
3975                 error "file $testfile is corrupted on server (4)"
3976
3977         rm -f $testfile $tmpfile $tmpfile2
3978         cancel_lru_locks osc ; cancel_lru_locks mdc
3979
3980         # write a few bytes in file, at beginning of first stripe
3981         echo "abc" > $tmpfile
3982         $LFS setstripe -S 256k -c2 $testfile
3983         dd if=$tmpfile of=$testfile bs=4 count=1 conv=fsync,notrunc
3984
3985         # write a few other bytes, at beginning of second stripe
3986         dd if=$tmpfile of=$testfile bs=4 count=1 seek=262144 oflag=seek_bytes \
3987                 conv=fsync,notrunc
3988         dd if=$tmpfile of=$tmpfile bs=4 count=1 seek=262144 oflag=seek_bytes \
3989                 conv=fsync,notrunc
3990
3991         # check that in-memory representation of file is correct
3992         cmp -bl $tmpfile $testfile ||
3993                 error "file $testfile is corrupted in memory (5)"
3994
3995         cancel_lru_locks osc ; cancel_lru_locks mdc
3996
3997         # check that file read from server is correct
3998         cmp -bl $tmpfile $testfile ||
3999                 error "file $testfile is corrupted on server (5)"
4000
4001         filesz=$(stat --format=%s $testfile)
4002         filesz=$(((filesz+UNIT_SIZE-1)/UNIT_SIZE * UNIT_SIZE))
4003
4004         # remount without dummy encryption key
4005         remount_client_normally
4006
4007         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type f)
4008         [ $(stat --format=%s $scrambledfile) -eq $filesz ] ||
4009                 error "file size without key should be rounded up"
4010
4011         rm -f $tmpfile
4012 }
4013 run_test 40 "exercise size of encrypted file"
4014
4015 test_41() {
4016         local testfile=$DIR/$tdir/$tfile
4017         local tmpfile=$TMP/abc
4018         local tmpfile2=$TMP/abc2
4019         local seek
4020
4021         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4022                 skip "client encryption not supported"
4023
4024         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4025                 skip "need dummy encryption support"
4026
4027         stack_trap cleanup_for_enc_tests EXIT
4028         setup_for_enc_tests
4029
4030         echo "abc" > $tmpfile
4031         seek=$(getconf PAGESIZE)
4032         seek=$((seek - 204))
4033         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
4034                 conv=fsync
4035         seek=$(getconf PAGESIZE)
4036         seek=$((seek + 1092))
4037         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
4038                 conv=fsync,notrunc
4039
4040         # write a few bytes in file
4041         $LFS setstripe -c1 -i0 -S 256k $testfile
4042         seek=$(getconf PAGESIZE)
4043         seek=$((seek - 204))
4044         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
4045         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
4046         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4047                 conv=fsync &
4048
4049         sleep 5
4050         # write a few other bytes, at a different offset
4051         seek=$(getconf PAGESIZE)
4052         seek=$((seek + 1092))
4053         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4054                 conv=fsync,notrunc &
4055         wait
4056         do_facet ost1 "$LCTL set_param fail_loc=0x0"
4057
4058         # check that in-memory representation of file is correct
4059         cmp -bl $tmpfile2 $testfile ||
4060                 error "file $testfile is corrupted in memory (1)"
4061
4062         cancel_lru_locks osc ; cancel_lru_locks mdc
4063
4064         # check that file read from server is correct
4065         cmp -bl $tmpfile2 $testfile ||
4066                 error "file $testfile is corrupted on server (1)"
4067
4068         rm -f $tmpfile $tmpfile2
4069 }
4070 run_test 41 "test race on encrypted file size (1)"
4071
4072 test_42() {
4073         local testfile=$DIR/$tdir/$tfile
4074         local testfile2=$DIR2/$tdir/$tfile
4075         local tmpfile=$TMP/abc
4076         local tmpfile2=$TMP/abc2
4077         local pagesz=$(getconf PAGESIZE)
4078         local seek
4079
4080         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4081                 skip "client encryption not supported"
4082
4083         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4084                 skip "need dummy encryption support"
4085
4086         stack_trap cleanup_for_enc_tests EXIT
4087         setup_for_enc_tests
4088
4089         if is_mounted $MOUNT2; then
4090                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
4091         fi
4092         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
4093                 error "mount2 with '-o test_dummy_encryption' failed"
4094
4095         # create file by writting one whole page
4096         $LFS setstripe -c1 -i0 -S 256k $testfile
4097         dd if=/dev/zero of=$testfile bs=$pagesz count=1 conv=fsync
4098
4099         # read file from 2nd mount point
4100         cat $testfile2 > /dev/null
4101
4102         echo "abc" > $tmpfile
4103         dd if=/dev/zero of=$tmpfile2 bs=$pagesz count=1 conv=fsync
4104         seek=$((2*pagesz - 204))
4105         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
4106                 conv=fsync,notrunc
4107         seek=$((2*pagesz + 1092))
4108         dd if=$tmpfile of=$tmpfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
4109                 conv=fsync,notrunc
4110
4111         # write a few bytes in file from 1st mount point
4112         seek=$((2*pagesz - 204))
4113         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
4114         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
4115         dd if=$tmpfile of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4116                 conv=fsync,notrunc &
4117
4118         sleep 5
4119         # write a few other bytes, at a different offset from 2nd mount point
4120         seek=$((2*pagesz + 1092))
4121         dd if=$tmpfile of=$testfile2 bs=4 count=1 seek=$seek oflag=seek_bytes \
4122                 conv=fsync,notrunc &
4123         wait
4124         do_facet ost1 "$LCTL set_param fail_loc=0x0"
4125
4126         # check that in-memory representation of file is correct
4127         cmp -bl $tmpfile2 $testfile ||
4128                 error "file $testfile is corrupted in memory (1)"
4129
4130         # check that in-memory representation of file is correct
4131         cmp -bl $tmpfile2 $testfile2 ||
4132                 error "file $testfile is corrupted in memory (2)"
4133
4134         cancel_lru_locks osc ; cancel_lru_locks mdc
4135
4136         # check that file read from server is correct
4137         cmp -bl $tmpfile2 $testfile ||
4138                 error "file $testfile is corrupted on server (1)"
4139
4140         rm -f $tmpfile $tmpfile2
4141 }
4142 run_test 42 "test race on encrypted file size (2)"
4143
4144 test_43() {
4145         local testfile=$DIR/$tdir/$tfile
4146         local testfile2=$DIR2/$tdir/$tfile
4147         local tmpfile=$TMP/abc
4148         local tmpfile2=$TMP/abc2
4149         local resfile=$TMP/res
4150         local pagesz=$(getconf PAGESIZE)
4151         local seek
4152
4153         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4154                 skip "client encryption not supported"
4155
4156         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4157                 skip "need dummy encryption support"
4158
4159         stack_trap cleanup_for_enc_tests EXIT
4160         setup_for_enc_tests
4161
4162         if is_mounted $MOUNT2; then
4163                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
4164         fi
4165         mount_client $MOUNT2 ${MOUNT_OPTS},test_dummy_encryption ||
4166                 error "mount2 with '-o test_dummy_encryption' failed"
4167
4168         # create file
4169         tr '\0' '1' < /dev/zero |
4170                 dd of=$tmpfile bs=1 count=$pagesz conv=fsync
4171         $LFS setstripe -c1 -i0 -S 256k $testfile
4172         cp $tmpfile $testfile
4173
4174         # read file from 2nd mount point
4175         cat $testfile2 > /dev/null
4176
4177         # write a few bytes in file from 1st mount point
4178         echo "abc" > $tmpfile2
4179         seek=$((2*pagesz - 204))
4180         #define OBD_FAIL_OST_WR_ATTR_DELAY       0x250
4181         do_facet ost1 "$LCTL set_param fail_loc=0x250 fail_val=15"
4182         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4183                 conv=fsync,notrunc &
4184
4185         sleep 5
4186         # read file from 2nd mount point
4187         dd if=$testfile2 of=$resfile bs=$pagesz count=1 conv=fsync,notrunc
4188         cmp -bl $tmpfile $resfile ||
4189                 error "file $testfile is corrupted in memory (1)"
4190
4191         wait
4192         do_facet ost1 "$LCTL set_param fail_loc=0x0"
4193
4194         # check that in-memory representation of file is correct
4195         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4196                 conv=fsync,notrunc
4197         cmp -bl $tmpfile $testfile2 ||
4198                 error "file $testfile is corrupted in memory (2)"
4199
4200         cancel_lru_locks osc ; cancel_lru_locks mdc
4201
4202         # check that file read from server is correct
4203         cmp -bl $tmpfile $testfile ||
4204                 error "file $testfile is corrupted on server (1)"
4205
4206         rm -f $tmpfile $tmpfile2
4207 }
4208 run_test 43 "test race on encrypted file size (3)"
4209
4210 test_44() {
4211         local testfile=$DIR/$tdir/$tfile
4212         local tmpfile=$TMP/abc
4213         local resfile=$TMP/resfile
4214         local pagesz=$(getconf PAGESIZE)
4215         local respage
4216
4217         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4218                 skip "client encryption not supported"
4219
4220         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4221                 skip "need dummy encryption support"
4222
4223         which vmtouch || skip "This test needs vmtouch utility"
4224
4225         # Direct I/O is now supported on encrypted files.
4226
4227         stack_trap cleanup_for_enc_tests EXIT
4228         setup_for_enc_tests
4229
4230         $LFS setstripe -c1 -i0 $testfile
4231         dd if=/dev/urandom of=$tmpfile bs=$pagesz count=2 conv=fsync
4232         dd if=$tmpfile of=$testfile bs=$pagesz count=2 oflag=direct ||
4233                 error "could not write to file with O_DIRECT (1)"
4234
4235         respage=$(vmtouch $testfile | awk '/Resident Pages:/ {print $3}')
4236         [ "$respage" == "0/2" ] ||
4237                 error "write to enc file fell back to buffered IO"
4238
4239         cancel_lru_locks
4240
4241         dd if=$testfile of=$resfile bs=$pagesz count=2 iflag=direct ||
4242                 error "could not read from file with O_DIRECT (1)"
4243
4244         respage=$(vmtouch $testfile | awk '/Resident Pages:/ {print $3}')
4245         [ "$respage" == "0/2" ] ||
4246                 error "read from enc file fell back to buffered IO"
4247
4248         cmp -bl $tmpfile $resfile ||
4249                 error "file $testfile is corrupted (1)"
4250
4251         rm -f $resfile
4252
4253         $TRUNCATE $tmpfile $pagesz
4254         dd if=$tmpfile of=$testfile bs=$pagesz count=1 seek=13 oflag=direct ||
4255                 error "could not write to file with O_DIRECT (2)"
4256
4257         cancel_lru_locks
4258
4259         dd if=$testfile of=$resfile bs=$pagesz count=1 skip=13 iflag=direct ||
4260                 error "could not read from file with O_DIRECT (2)"
4261         cmp -bl $tmpfile $resfile ||
4262                 error "file $testfile is corrupted (2)"
4263
4264         rm -f $testfile $resfile
4265         $LFS setstripe -c1 -i0 $testfile
4266
4267         $TRUNCATE $tmpfile $((pagesz/2 - 5))
4268         cp $tmpfile $testfile
4269
4270         cancel_lru_locks
4271
4272         dd if=$testfile of=$resfile bs=$pagesz count=1 iflag=direct ||
4273                 error "could not read from file with O_DIRECT (3)"
4274         cmp -bl $tmpfile $resfile ||
4275                 error "file $testfile is corrupted (3)"
4276
4277         rm -f $tmpfile $resfile $testfile
4278
4279         if [ $OSTCOUNT -ge 2 ]; then
4280                 dd if=/dev/urandom of=$tmpfile bs=$pagesz count=1 conv=fsync
4281                 $LFS setstripe -S 256k -c2 $testfile
4282
4283                 # write in file, at beginning of first stripe, buffered IO
4284                 dd if=$tmpfile of=$testfile bs=$pagesz count=1 \
4285                         conv=fsync,notrunc
4286
4287                 # write at beginning of second stripe, direct IO
4288                 dd if=$tmpfile of=$testfile bs=$pagesz count=1 seek=256k \
4289                         oflag=seek_bytes,direct conv=fsync,notrunc
4290
4291                 cancel_lru_locks
4292
4293                 # read at beginning of first stripe, direct IO
4294                 dd if=$testfile of=$resfile bs=$pagesz count=1 \
4295                         iflag=direct conv=fsync
4296
4297                 cmp -bl $tmpfile $resfile ||
4298                         error "file $testfile is corrupted (4)"
4299
4300                 # read at beginning of second stripe, buffered IO
4301                 dd if=$testfile of=$resfile bs=$pagesz count=1 skip=256k \
4302                         iflag=skip_bytes conv=fsync
4303
4304                 cmp -bl $tmpfile $resfile ||
4305                         error "file $testfile is corrupted (5)"
4306
4307                 rm -f $tmpfile $resfile
4308         fi
4309 }
4310 run_test 44 "encrypted file access semantics: direct IO"
4311
4312 test_45() {
4313         local testfile=$DIR/$tdir/$tfile
4314         local tmpfile=$TMP/junk
4315
4316         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4317                 skip "client encryption not supported"
4318
4319         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4320                 skip "need dummy encryption support"
4321
4322         stack_trap cleanup_for_enc_tests EXIT
4323         setup_for_enc_tests
4324
4325         $LFS setstripe -c1 -i0 $testfile
4326         dd if=/dev/zero of=$testfile bs=512K count=1
4327         $MULTIOP $testfile OSMRUc || error "$MULTIOP $testfile failed (1)"
4328         $MULTIOP $testfile OSMWUc || error "$MULTIOP $testfile failed (2)"
4329
4330         dd if=/dev/zero of=$tmpfile bs=512K count=1
4331         $MULTIOP $tmpfile OSMWUc || error "$MULTIOP $tmpfile failed"
4332         $MMAP_CAT $tmpfile > ${tmpfile}2
4333
4334         cancel_lru_locks
4335
4336         $MULTIOP $testfile OSMRUc
4337         $MMAP_CAT $testfile > ${testfile}2
4338         cmp -bl ${tmpfile}2 ${testfile}2 ||
4339                 error "file $testfile is corrupted"
4340
4341         rm -f $tmpfile ${tmpfile}2
4342 }
4343 run_test 45 "encrypted file access semantics: MMAP"
4344
4345 test_46() {
4346         local testdir=$DIR/$tdir/mydir
4347         local testfile=$testdir/myfile
4348         local testdir2=$DIR/$tdir/mydirwithaveryverylongnametotestcodebehaviour0
4349         local testfile2=$testdir/myfilewithaveryverylongnametotestcodebehaviour0
4350         # testdir3, testfile3, testhl3 and testsl3 names are 255 bytes long
4351         local testdir3=$testdir2/dir_abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz012345678
4352         local testfile3=$testdir2/file_abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01234567
4353         local testhl3=$testdir2/hl_abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
4354         local testsl3=$testdir2/sl_abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
4355         local lsfile=$TMP/lsfile
4356         local scrambleddir
4357         local scrambledfile
4358         local inum
4359
4360         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4361                 skip "client encryption not supported"
4362
4363         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4364                 skip "need dummy encryption support"
4365
4366         stack_trap cleanup_for_enc_tests EXIT
4367         setup_for_enc_tests
4368
4369         touch $DIR/$tdir/$tfile
4370         mkdir $testdir
4371         echo test > $testfile
4372         echo othertest > $testfile2
4373         if [[ $MDSCOUNT -gt 1 ]]; then
4374                 $LFS setdirstripe -c1 -i1 $testdir2
4375         else
4376                 mkdir $testdir2
4377         fi
4378         inum=$(stat -c %i $testdir2)
4379         if [ "$mds1_FSTYPE" = ldiskfs ]; then
4380                 # For now, restrict this part of the test to ldiskfs backend,
4381                 # as osd-zfs does not support 255 byte-long encrypted names.
4382                 mkdir $testdir3 || error "cannot mkdir $testdir3"
4383                 touch $testfile3 || error "cannot touch $testfile3"
4384                 ln $testfile3 $testhl3 || error "cannot ln $testhl3"
4385                 ln -s $testfile3 $testsl3 || error "cannot ln $testsl3"
4386         fi
4387         sync ; echo 3 > /proc/sys/vm/drop_caches
4388
4389         # remount without dummy encryption key
4390         remount_client_normally
4391
4392         # this is $testdir2
4393         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -inum $inum)
4394         stat $scrambleddir || error "stat $scrambleddir failed"
4395         if [ "$mds1_FSTYPE" = ldiskfs ]; then
4396                 stat $scrambleddir/* || error "cannot stat in $scrambleddir"
4397                 rm -rf $scrambleddir/* || error "cannot clean in $scrambleddir"
4398         fi
4399         rmdir $scrambleddir || error "rmdir $scrambleddir failed"
4400
4401         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
4402         ls -1 $scrambleddir > $lsfile || error "ls $testdir failed (1)"
4403
4404         scrambledfile=$scrambleddir/$(head -n 1 $lsfile)
4405         stat $scrambledfile || error "stat $scrambledfile failed (1)"
4406         rm -f $lsfile
4407
4408         cat $scrambledfile && error "cat $scrambledfile should have failed (1)"
4409         rm -f $scrambledfile || error "rm $scrambledfile failed (1)"
4410
4411         ls -1 $scrambleddir > $lsfile || error "ls $testdir failed (2)"
4412         scrambledfile=$scrambleddir/$(head -n 1 $lsfile)
4413         stat $scrambledfile || error "stat $scrambledfile failed (2)"
4414         rm -f $lsfile
4415         cat $scrambledfile && error "cat $scrambledfile should have failed (2)"
4416
4417         touch $scrambleddir/otherfile &&
4418                 error "touch otherfile should have failed"
4419         ls $scrambleddir/otherfile && error "otherfile should not exist"
4420         mkdir $scrambleddir/otherdir &&
4421                 error "mkdir otherdir should have failed"
4422         ls -d $scrambleddir/otherdir && error "otherdir should not exist"
4423
4424         ls -R $DIR
4425         rm -f $scrambledfile || error "rm $scrambledfile failed (2)"
4426         rmdir $scrambleddir || error "rmdir $scrambleddir failed"
4427         ls -R $DIR
4428 }
4429 run_test 46 "encrypted file access semantics without key"
4430
4431 test_47() {
4432         local testfile=$DIR/$tdir/$tfile
4433         local testfile2=$DIR/$tdir/${tfile}.2
4434         local tmpfile=$DIR/junk
4435         local name_enc=1
4436         local scrambleddir
4437         local scrambledfile
4438
4439         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4440                 skip "client encryption not supported"
4441
4442         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4443                 skip "need dummy encryption support"
4444
4445         $LCTL get_param mdc.*.connect_flags | grep -q name_encryption ||
4446                 name_enc=0
4447
4448         stack_trap cleanup_for_enc_tests EXIT
4449         setup_for_enc_tests
4450
4451         dd if=/dev/urandom of=$tmpfile bs=512K count=1
4452         mrename $tmpfile $testfile &&
4453                 error "rename from unencrypted to encrypted dir should fail"
4454
4455         ln $tmpfile $testfile &&
4456                 error "link from encrypted to unencrypted dir should fail"
4457
4458         cp $tmpfile $testfile ||
4459                 error "cp from unencrypted to encrypted dir should succeed"
4460         rm -f $tmpfile
4461
4462         mrename $testfile $testfile2 ||
4463                 error "rename from within encrypted dir should succeed"
4464
4465         ln $testfile2 $testfile ||
4466                 error "link from within encrypted dir should succeed"
4467         cmp -bl $testfile2 $testfile ||
4468                 error "cannot read from hard link (1.1)"
4469         echo a >> $testfile || error "cannot write to hard link (1)"
4470         cancel_lru_locks
4471         cmp -bl $testfile2 $testfile ||
4472                 error "cannot read from hard link (1.2)"
4473         rm -f $testfile
4474
4475         ln $testfile2 $tmpfile ||
4476                 error "link from unencrypted to encrypted dir should succeed"
4477         cancel_lru_locks
4478         cmp -bl $testfile2 $tmpfile ||
4479                 error "cannot read from hard link (2.1)"
4480         echo a >> $tmpfile || error "cannot write to hard link (2)"
4481         cancel_lru_locks
4482         cmp -bl $testfile2 $tmpfile ||
4483                 error "cannot read from hard link (2.2)"
4484         rm -f $tmpfile
4485
4486         if [ $name_enc -eq 1 ]; then
4487                 # check we are limited in the number of hard links
4488                 # we can create for encrypted files, to what can fit into LinkEA
4489                 for i in $(seq 1 160); do
4490                         ln $testfile2 ${testfile}_$i || break
4491                 done
4492                 [ $i -lt 160 ] || error "hard link $i should fail"
4493                 rm -f ${testfile}_*
4494         fi
4495
4496         mrename $testfile2 $tmpfile &&
4497                 error "rename from encrypted to unencrypted dir should fail"
4498         rm -f $testfile2
4499         dd if=/dev/urandom of=$tmpfile bs=512K count=1
4500
4501         dd if=/dev/urandom of=$testfile bs=512K count=1
4502         mkdir $DIR/$tdir/mydir
4503
4504         ln -s $testfile ${testfile}.sym ||
4505                 error "symlink from within encrypted dir should succeed"
4506         cancel_lru_locks
4507         cmp -bl $testfile ${testfile}.sym ||
4508                 error "cannot read from sym link (1.1)"
4509         echo a >> ${testfile}.sym || error "cannot write to sym link (1)"
4510         cancel_lru_locks
4511         cmp -bl $testfile ${testfile}.sym ||
4512                 error "cannot read from sym link (1.2)"
4513         [ $(stat -c %s ${testfile}.sym) -eq ${#testfile} ] ||
4514                 error "wrong symlink size (1)"
4515
4516         ln -s $tmpfile ${testfile}.sl ||
4517                 error "symlink from encrypted to unencrypted dir should succeed"
4518         cancel_lru_locks
4519         cmp -bl $tmpfile ${testfile}.sl ||
4520                 error "cannot read from sym link (2.1)"
4521         echo a >> ${testfile}.sl || error "cannot write to sym link (2)"
4522         cancel_lru_locks
4523         cmp -bl $tmpfile ${testfile}.sl ||
4524                 error "cannot read from sym link (2.2)"
4525         [ $(stat -c %s ${testfile}.sl) -eq ${#tmpfile} ] ||
4526                 error "wrong symlink size (2)"
4527         rm -f ${testfile}.sl
4528
4529         sync ; echo 3 > /proc/sys/vm/drop_caches
4530
4531         # remount without dummy encryption key
4532         remount_client_normally
4533
4534         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
4535         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
4536         scrambledlink=$(find $DIR/$tdir/ -maxdepth 1 -type l)
4537         ln $scrambledfile $scrambleddir/linkfile &&
4538                 error "ln linkfile should have failed"
4539         mrename $scrambledfile $DIR/onefile2 &&
4540                 error "mrename from $scrambledfile should have failed"
4541         touch $DIR/onefile
4542         mrename $DIR/onefile $scrambleddir/otherfile &&
4543                 error "mrename to $scrambleddir should have failed"
4544         readlink $scrambledlink ||
4545                 error "link should be read without key"
4546         [ $(stat -c %s $scrambledlink) -eq \
4547                         $(expr length "$(readlink $scrambledlink)") ] ||
4548                 error "wrong symlink size without key"
4549         if [ $name_enc -eq 1 ]; then
4550                 readlink -e $scrambledlink &&
4551                         error "link should not point to anywhere useful"
4552         fi
4553         ln -s $scrambledfile ${scrambledfile}.sym &&
4554                 error "symlink without key should fail (1)"
4555         ln -s $tmpfile ${scrambledfile}.sl &&
4556                 error "symlink without key should fail (2)"
4557
4558         rm -f $tmpfile $DIR/onefile
4559 }
4560 run_test 47 "encrypted file access semantics: rename/link"
4561
4562 test_48a() {
4563         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
4564         local testfile=$DIR/$tdir/$tfile
4565         local tmpfile=$TMP/111
4566         local tmpfile2=$TMP/abc
4567         local pagesz=$(getconf PAGESIZE)
4568         local sz
4569         local seek
4570         local scrambledfile
4571
4572         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4573                 skip "client encryption not supported"
4574
4575         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4576                 skip "need dummy encryption support"
4577
4578         stack_trap cleanup_for_enc_tests EXIT
4579         setup_for_enc_tests
4580
4581         # create file, 4 x PAGE_SIZE long
4582         tr '\0' '1' < /dev/zero |
4583                 dd of=$tmpfile bs=1 count=4x$pagesz conv=fsync
4584         $LFS setstripe -c1 -i0 $testfile
4585         cp $tmpfile $testfile
4586         echo "abc" > $tmpfile2
4587
4588         # decrease size: truncate to PAGE_SIZE
4589         $TRUNCATE $tmpfile $pagesz
4590         $TRUNCATE $testfile $pagesz
4591         cancel_lru_locks osc ; cancel_lru_locks mdc
4592         cmp -bl $tmpfile $testfile ||
4593                 error "file $testfile is corrupted (1)"
4594
4595         # increase size: truncate to 2 x PAGE_SIZE
4596         sz=$((pagesz*2))
4597         $TRUNCATE $tmpfile $sz
4598         $TRUNCATE $testfile $sz
4599         cancel_lru_locks osc ; cancel_lru_locks mdc
4600         cmp -bl $tmpfile $testfile ||
4601                 error "file $testfile is corrupted (2)"
4602
4603         # write in 2nd page
4604         seek=$((pagesz+100))
4605         dd if=$tmpfile2 of=$tmpfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4606                 conv=fsync,notrunc
4607         dd if=$tmpfile2 of=$testfile bs=4 count=1 seek=$seek oflag=seek_bytes \
4608                 conv=fsync,notrunc
4609         cancel_lru_locks osc ; cancel_lru_locks mdc
4610         cmp -bl $tmpfile $testfile ||
4611                 error "file $testfile is corrupted (3)"
4612
4613         # truncate to PAGE_SIZE / 2
4614         sz=$((pagesz/2))
4615         $TRUNCATE $tmpfile $sz
4616         $TRUNCATE $testfile $sz
4617         cancel_lru_locks osc ; cancel_lru_locks mdc
4618         cmp -bl $tmpfile $testfile ||
4619                 error "file $testfile is corrupted (4)"
4620
4621         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
4622         sz=$((sz-7))
4623         $TRUNCATE $tmpfile $sz
4624         $TRUNCATE $testfile $sz
4625         cancel_lru_locks osc ; cancel_lru_locks mdc
4626         cmp -bl $tmpfile $testfile ||
4627                 error "file $testfile is corrupted (5)"
4628
4629         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
4630         sz=$((sz+18))
4631         $TRUNCATE $tmpfile $sz
4632         $TRUNCATE $testfile $sz
4633         cancel_lru_locks osc ; cancel_lru_locks mdc
4634         cmp -bl $tmpfile $testfile ||
4635                 error "file $testfile is corrupted (6)"
4636
4637         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
4638         sz=$((sz+pagesz+30))
4639         $TRUNCATE $tmpfile $sz
4640         $TRUNCATE $testfile $sz
4641         cancel_lru_locks osc ; cancel_lru_locks mdc
4642         cmp -bl $tmpfile $testfile ||
4643                 error "file $testfile is corrupted (7)"
4644
4645         sync ; echo 3 > /proc/sys/vm/drop_caches
4646
4647         # remount without dummy encryption key
4648         remount_client_normally
4649
4650         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -type f)
4651         $TRUNCATE $scrambledfile 0 &&
4652                 error "truncate $scrambledfile should have failed without key"
4653
4654         rm -f $tmpfile $tmpfile2
4655 }
4656 run_test 48a "encrypted file access semantics: truncate"
4657
4658 cleanup_for_enc_tests_othercli() {
4659         local othercli=$1
4660
4661         # remount othercli normally
4662         zconf_umount $othercli $MOUNT ||
4663                 error "umount $othercli $MOUNT failed"
4664         zconf_mount $othercli $MOUNT ||
4665                 error "remount $othercli $MOUNT failed"
4666 }
4667
4668 test_48b() {
4669         local othercli
4670
4671         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4672                 skip "client encryption not supported"
4673
4674         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4675                 skip "need dummy encryption support"
4676
4677         [ "$num_clients" -ge 2 ] || skip "Need at least 2 clients"
4678
4679         if [ "$HOSTNAME" == ${clients_arr[0]} ]; then
4680                 othercli=${clients_arr[1]}
4681         else
4682                 othercli=${clients_arr[0]}
4683         fi
4684
4685         stack_trap cleanup_for_enc_tests EXIT
4686         stack_trap "cleanup_for_enc_tests_othercli $othercli" EXIT
4687         setup_for_enc_tests
4688         zconf_umount $othercli $MOUNT ||
4689                 error "umount $othercli $MOUNT failed"
4690
4691         cp /bin/sleep $DIR/$tdir/
4692         cancel_lru_locks osc ; cancel_lru_locks mdc
4693         $DIR/$tdir/sleep 30 &
4694         # mount and IOs must be done in the same shell session, otherwise
4695         # encryption key in session keyring is missing
4696         do_node $othercli "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
4697                            $MGSNID:/$FSNAME $MOUNT && \
4698                            $TRUNCATE $DIR/$tdir/sleep 7"
4699         wait || error "wait error"
4700         cmp --silent /bin/sleep $DIR/$tdir/sleep ||
4701                 error "/bin/sleep and $DIR/$tdir/sleep differ"
4702 }
4703 run_test 48b "encrypted file: concurrent truncate"
4704
4705 trace_cmd() {
4706         local cmd="$@"
4707
4708         cancel_lru_locks
4709         $LCTL set_param debug=+info
4710         $LCTL clear
4711
4712         echo $cmd
4713         eval $cmd
4714         [ $? -eq 0 ] || error "$cmd failed"
4715
4716         if [ -z "$MATCHING_STRING" ]; then
4717                 $LCTL dk | grep -E "get xattr 'encryption.c'|get xattrs"
4718         else
4719                 $LCTL dk | grep -E "$MATCHING_STRING"
4720         fi
4721         [ $? -ne 0 ] || error "get xattr event was triggered"
4722 }
4723
4724 test_49() {
4725         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4726                 skip "client encryption not supported"
4727
4728         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4729                 skip "need dummy encryption support"
4730
4731         stack_trap cleanup_for_enc_tests EXIT
4732         setup_for_enc_tests
4733
4734         local dirname=$DIR/$tdir/subdir
4735
4736         mkdir $dirname
4737
4738         trace_cmd stat $dirname
4739         trace_cmd echo a > $dirname/f1
4740         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4741         trace_cmd stat $dirname/f1
4742         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4743         trace_cmd cat $dirname/f1
4744         dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
4745         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4746         MATCHING_STRING="get xattr 'encryption.c'" \
4747                 trace_cmd $TRUNCATE $dirname/f1 10240
4748         trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
4749         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4750         trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
4751
4752         if [[ $MDSCOUNT -gt 1 ]]; then
4753                 trace_cmd $LFS setdirstripe -i 1 $dirname/d2
4754                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4755                 trace_cmd $LFS migrate -m 0 $dirname/d2
4756                 echo b > $dirname/d2/subf
4757                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4758                 if (( "$MDS1_VERSION" > $(version_code 2.14.54.54) )); then
4759                         # migrate a non-empty encrypted dir
4760                         trace_cmd $LFS migrate -m 1 $dirname/d2
4761                         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4762                         [ -f $dirname/d2/subf ] || error "migrate failed (1)"
4763                         [ $(cat $dirname/d2/subf) == "b" ] ||
4764                                 error "migrate failed (2)"
4765                 fi
4766
4767                 $LFS setdirstripe -i 1 -c 1 $dirname/d3
4768                 dirname=$dirname/d3/subdir
4769                 mkdir $dirname
4770                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4771                 trace_cmd stat $dirname
4772                 trace_cmd echo c > $dirname/f1
4773                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4774                 trace_cmd stat $dirname/f1
4775                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4776                 trace_cmd cat $dirname/f1
4777                 dd if=/dev/zero of=$dirname/f1 bs=1M count=10 conv=fsync
4778                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4779                 MATCHING_STRING="get xattr 'encryption.c'" \
4780                         trace_cmd $TRUNCATE $dirname/f1 10240
4781                 trace_cmd $LFS setstripe -E -1 -S 4M $dirname/f2
4782                 sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
4783                 trace_cmd $LFS migrate -E -1 -S 256K $dirname/f2
4784         else
4785                 skip_noexit "2nd part needs >= 2 MDTs"
4786         fi
4787 }
4788 run_test 49 "Avoid getxattr for encryption context"
4789
4790 test_50() {
4791         local testfile=$DIR/$tdir/$tfile
4792         local tmpfile=$TMP/abc
4793         local pagesz=$(getconf PAGESIZE)
4794         local sz
4795
4796         $LCTL get_param mdc.*.import | grep -q client_encryption ||
4797                 skip "client encryption not supported"
4798
4799         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
4800                 skip "need dummy encryption support"
4801
4802         stack_trap cleanup_for_enc_tests EXIT
4803         setup_for_enc_tests
4804
4805         # write small file, data on MDT only
4806         tr '\0' '1' < /dev/zero |
4807             dd of=$tmpfile bs=1 count=5000 conv=fsync
4808         $LFS setstripe -E 1M -L mdt -E EOF $testfile
4809         cp $tmpfile $testfile
4810
4811         # check that in-memory representation of file is correct
4812         cmp -bl $tmpfile $testfile ||
4813                 error "file $testfile is corrupted in memory"
4814
4815         remove_enc_key ; insert_enc_key
4816
4817         # check that file read from server is correct
4818         cmp -bl $tmpfile $testfile ||
4819                 error "file $testfile is corrupted on server"
4820
4821         # decrease size: truncate to PAGE_SIZE
4822         $TRUNCATE $tmpfile $pagesz
4823         $TRUNCATE $testfile $pagesz
4824         remove_enc_key ; insert_enc_key
4825         cmp -bl $tmpfile $testfile ||
4826                 error "file $testfile is corrupted (1)"
4827
4828         # increase size: truncate to 2 x PAGE_SIZE
4829         sz=$((pagesz*2))
4830         $TRUNCATE $tmpfile $sz
4831         $TRUNCATE $testfile $sz
4832         remove_enc_key ; insert_enc_key
4833         cmp -bl $tmpfile $testfile ||
4834                 error "file $testfile is corrupted (2)"
4835
4836         # truncate to PAGE_SIZE / 2
4837         sz=$((pagesz/2))
4838         $TRUNCATE $tmpfile $sz
4839         $TRUNCATE $testfile $sz
4840         remove_enc_key ; insert_enc_key
4841         cmp -bl $tmpfile $testfile ||
4842                 error "file $testfile is corrupted (3)"
4843
4844         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16
4845         sz=$((sz-7))
4846         $TRUNCATE $tmpfile $sz
4847         $TRUNCATE $testfile $sz
4848         remove_enc_key ; insert_enc_key
4849         cmp -bl $tmpfile $testfile ||
4850                 error "file $testfile is corrupted (4)"
4851
4852         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16
4853         sz=$((sz+18))
4854         $TRUNCATE $tmpfile $sz
4855         $TRUNCATE $testfile $sz
4856         remove_enc_key ; insert_enc_key
4857         cmp -bl $tmpfile $testfile ||
4858                 error "file $testfile is corrupted (5)"
4859
4860         # truncate to a larger, non-multiple of PAGE_SIZE, in a different page
4861         sz=$((sz+pagesz+30))
4862         $TRUNCATE $tmpfile $sz
4863         $TRUNCATE $testfile $sz
4864         remove_enc_key ; insert_enc_key
4865         cmp -bl $tmpfile $testfile ||
4866                 error "file $testfile is corrupted (6)"
4867
4868         rm -f $testfile
4869         remove_enc_key ; insert_enc_key
4870
4871         # write hole in file, data spread on MDT and OST
4872         tr '\0' '2' < /dev/zero |
4873             dd of=$tmpfile bs=1 count=1539 seek=1539074 conv=fsync,notrunc
4874         $LFS setstripe -E 1M -L mdt -E EOF $testfile
4875         cp --sparse=always $tmpfile $testfile
4876
4877         # check that in-memory representation of file is correct
4878         cmp -bl $tmpfile $testfile ||
4879                 error "file $testfile is corrupted in memory"
4880
4881         remove_enc_key ; insert_enc_key
4882
4883         # check that file read from server is correct
4884         cmp -bl $tmpfile $testfile ||
4885                 error "file $testfile is corrupted on server"
4886
4887         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
4888         # inside OST part of data
4889         sz=$((1024*1024+13))
4890         $TRUNCATE $tmpfile $sz
4891         $TRUNCATE $testfile $sz
4892         remove_enc_key ; insert_enc_key
4893         cmp -bl $tmpfile $testfile ||
4894                 error "file $testfile is corrupted (7)"
4895
4896         # truncate to a smaller, non-multiple of PAGE_SIZE, non-multiple of 16,
4897         # inside MDT part of data
4898         sz=7
4899         $TRUNCATE $tmpfile $sz
4900         $TRUNCATE $testfile $sz
4901         remove_enc_key ; insert_enc_key
4902         cmp -bl $tmpfile $testfile ||
4903                 error "file $testfile is corrupted (8)"
4904
4905         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
4906         # inside MDT part of data
4907         sz=$((1024*1024-13))
4908         $TRUNCATE $tmpfile $sz
4909         $TRUNCATE $testfile $sz
4910         remove_enc_key ; insert_enc_key
4911         cmp -bl $tmpfile $testfile ||
4912                 error "file $testfile is corrupted (9)"
4913
4914         # truncate to a larger, non-multiple of PAGE_SIZE, non-multiple of 16,
4915         # inside OST part of data
4916         sz=$((1024*1024+7))
4917         $TRUNCATE $tmpfile $sz
4918         $TRUNCATE $testfile $sz
4919         remove_enc_key ; insert_enc_key
4920         cmp -bl $tmpfile $testfile ||
4921                 error "file $testfile is corrupted (10)"
4922
4923         rm -f $tmpfile
4924 }
4925 run_test 50 "DoM encrypted file"
4926
4927 test_51() {
4928         (( "$MDS1_VERSION" >= $(version_code v2_13_55-38-gf05edf8e2b) )) ||
4929                 skip "Need MDS version at least 2.13.55.38"
4930
4931         mkdir $DIR/$tdir || error "mkdir $tdir"
4932         local mdts=$(comma_list $(mdts_nodes))
4933         local cap_param=mdt.*.enable_cap_mask
4934         local nm_param=nodemap.default.enable_cap_mask
4935         local val
4936
4937         old_cap=($(do_nodes $mdts $LCTL get_param -n $cap_param 2>/dev/null))
4938         if [[ -n "$old_cap" ]]; then
4939                 local new_cap="+cap_chown+cap_fowner+cap_dac_override+cap_dac_read_search"
4940
4941                 (( MDS1_VERSION >= $(version_code 2.15.63.14) )) ||
4942                 (( MDS1_VERSION < $(version_code 2.15.0) &&
4943                    MDS1_VERSION > $(version_code 2.14.0.135) )) ||
4944                         new_cap=0xf
4945                 echo "old_cap: $old_cap new_cap: $new_cap"
4946                 do_nodes $mdts $LCTL set_param $cap_param=$new_cap
4947                 stack_trap "do_nodes $mdts $LCTL set_param $cap_param=$old_cap"
4948         fi
4949
4950         touch $DIR/$tdir/$tfile || error "touch $tfile as root (1)"
4951         cp $(which chown) $DIR/$tdir || error "cp chown"
4952         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile &&
4953                 error "chown $tfile should fail (1)"
4954         setcap 'CAP_CHOWN=ep' $DIR/$tdir/chown || error "setcap CAP_CHOWN"
4955         $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile ||
4956                 error "chown $tfile as $ID0 (1)"
4957         rm $DIR/$tdir/$tfile || error "rm $tfile (1)"
4958
4959         touch $DIR/$tdir/$tfile || error "touch $tfile as root (2)"
4960         cp $(which touch) $DIR/$tdir || error "cp touch"
4961         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile &&
4962                 error "touch should fail"
4963         setcap 'CAP_FOWNER=ep' $DIR/$tdir/touch || error "setcap CAP_FOWNER"
4964         $RUNAS_CMD -u $ID0 $DIR/$tdir/touch $DIR/$tdir/$tfile ||
4965                 error "touch $tfile"
4966         rm $DIR/$tdir/$tfile || error "rm $tfile (2)"
4967
4968         local cap
4969         for cap in "CAP_DAC_OVERRIDE" "CAP_DAC_READ_SEARCH"; do
4970                 touch $DIR/$tdir/$tfile || error "touch $tfile as root (3)"
4971                 chmod 600 $DIR/$tdir/$tfile || error "chmod $tfile"
4972                 cp $(which cat) $DIR/$tdir || error "cp cat"
4973                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile &&
4974                         error "cat should fail"
4975                 setcap $cap=ep $DIR/$tdir/cat || error "setcap $cap"
4976                 $RUNAS_CMD -u $ID0 $DIR/$tdir/cat $DIR/$tdir/$tfile ||
4977                         error "cat $tfile"
4978                 rm $DIR/$tdir/$tfile || error "rm $tfile (3)"
4979         done
4980
4981         if (( "$MDS1_VERSION" >= $(version_code 2.16.55) )); then
4982                 val=$(do_facet mgs $LCTL get_param -n $nm_param)
4983                 [[ "$val" == "off" ]] ||
4984                         error "wrong default value $val for $nm_param"
4985
4986                 do_facet mgs $LCTL nodemap_modify --name default \
4987                         --property admin --value 1
4988                 do_facet mgs $LCTL nodemap_modify --name default \
4989                         --property trusted --value 1
4990                 wait_nm_sync default trusted_nodemap
4991
4992                 do_facet mgs $LCTL nodemap_activate 1
4993                 wait_nm_sync active 1
4994                 stack_trap cleanup_active EXIT
4995                 stack_trap "do_facet mgs $LCTL nodemap_modify --name default \
4996                         --property admin --value 0" EXIT
4997                 stack_trap "do_facet mgs $LCTL nodemap_modify --name default \
4998                         --property trusted --value 0" EXIT
4999                 stack_trap "do_facet mgs $LCTL nodemap_set_cap \
5000                         --name default --type off" EXIT
5001
5002                 # $DIR/$tdir/chown has CAP_CHOWN, so it should succeed with
5003                 # enable_cap_mask=off on nodemap
5004                 touch $DIR/$tdir/$tfile || error "touch $tfile as root (4)"
5005                 $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile ||
5006                         error "chown $tfile as $ID0 (2)"
5007                 rm $DIR/$tdir/$tfile || error "rm $tfile (4)"
5008
5009                 do_facet mgs $LCTL nodemap_set_cap --name default \
5010                    --type mask --caps cap_dac_read_search ||
5011                         error "nodemap_set_cap failed (1)"
5012                 wait_nm_sync default enable_cap_mask
5013
5014                 # $DIR/$tdir/chown should fail with
5015                 # enable_cap_mask=mask:cap_dac_read_search on nodemap
5016                 touch $DIR/$tdir/$tfile || error "touch $tfile as root (5)"
5017                 $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile &&
5018                         error "chown $tfile should fail (2)"
5019                 do_facet mgs $LCTL nodemap_set_cap --name default \
5020                    --type mask --caps +cap_chown ||
5021                         error "nodemap_set_cap failed (2)"
5022                 wait_nm_sync default enable_cap_mask
5023                 # $DIR/$tdir/chown should succeed with
5024                 # enable_cap_mask=mask:cap_chown,cap_dac_read_search
5025                 $RUNAS_CMD -u $ID0 $DIR/$tdir/chown $ID0 $DIR/$tdir/$tfile ||
5026                         error "chown $tfile as $ID0 (3)"
5027                 rm $DIR/$tdir/$tfile || error "rm $tfile (5)"
5028
5029                 # Test ability to raise caps on child nodemap
5030                 do_facet mgs $LCTL nodemap_modify --name default \
5031                    --property child_raise_privileges --value none ||
5032                         error "setting child_raise_privileges=none failed"
5033                 wait_nm_sync default child_raise_privileges
5034                 stack_trap "do_facet mgs $LCTL nodemap_modify --name default \
5035                             --property child_raise_privileges --value none" EXIT
5036                 do_facet mds1 $LCTL nodemap_add -d -p default nm_51 ||
5037                         error "cannot create nodemap nm_51 (1)"
5038                 stack_trap "do_facet mds1 $LCTL nodemap_del nm_51" EXIT
5039                 do_facet mds1 $LCTL nodemap_set_cap --name nm_51 \
5040                    --type mask --caps +cap_fowner &&
5041                         error "nodemap_set_cap +cap_fowner should fail"
5042                 do_facet mds1 $LCTL nodemap_set_cap --name nm_51 \
5043                    --type mask --caps -cap_chown ||
5044                         error "nodemap_set_cap -cap_chown failed"
5045                 do_facet mds1 $LCTL nodemap_set_cap --name nm_51 \
5046                    --type mask --caps +cap_chown ||
5047                         error "nodemap_set_cap +cap_chown failed"
5048                 do_facet mds1 $LCTL nodemap_del nm_51 ||
5049                         error "cannot delete nodemap nm_51"
5050                 do_facet mgs $LCTL nodemap_modify --name default \
5051                    --property child_raise_privileges --value caps ||
5052                         error "setting child_raise_privileges=caps failed"
5053                 wait_nm_sync default child_raise_privileges
5054                 do_facet mds1 $LCTL nodemap_add -d -p default nm_51 ||
5055                         error "cannot create nodemap nm_51 (2)"
5056                 do_facet mds1 $LCTL nodemap_set_cap --name nm_51 \
5057                    --type mask --caps +cap_fowner ||
5058                         error "nodemap_set_cap +cap_fowner failed"
5059         fi
5060 }
5061 run_test 51 "FS capabilities ==============="
5062
5063 test_52() {
5064         local testfile=$DIR/$tdir/$tfile
5065         local tmpfile=$TMP/$tfile
5066         local mirror1=$TMP/$tfile.mirror1
5067         local mirror2=$TMP/$tfile.mirror2
5068
5069         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5070                 skip "client encryption not supported"
5071
5072         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5073                 skip "need dummy encryption support"
5074
5075         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5076
5077         stack_trap "cleanup_for_enc_tests $tmpfile $mirror1 $mirror2" EXIT
5078         setup_for_enc_tests
5079
5080         dd if=/dev/urandom of=$tmpfile bs=5000 count=1 conv=fsync
5081
5082         $LFS mirror create -N -i0 -N -i1 $testfile ||
5083                 error "could not create mirror"
5084
5085         dd if=$tmpfile of=$testfile bs=5000 count=1 conv=fsync ||
5086                 error "could not write to $testfile"
5087
5088         $LFS mirror resync $testfile ||
5089                 error "could not resync mirror"
5090
5091         $LFS mirror verify -v $testfile ||
5092                 error "verify mirror failed"
5093
5094         $LFS mirror read -N 1 -o $mirror1 $testfile ||
5095                 error "could not read from mirror 1"
5096
5097         cmp -bl $tmpfile $mirror1 ||
5098                 error "mirror 1 is corrupted"
5099
5100         $LFS mirror read -N 2 -o $mirror2 $testfile ||
5101                 error "could not read from mirror 2"
5102
5103         cmp -bl $tmpfile $mirror2 ||
5104                 error "mirror 2 is corrupted"
5105
5106         tr '\0' '2' < /dev/zero |
5107             dd of=$tmpfile bs=1 count=9000 conv=fsync
5108
5109         $LFS mirror write -N 1 -i $tmpfile $testfile ||
5110                 error "could not write to mirror 1"
5111
5112         $LFS mirror verify -v $testfile &&
5113                 error "mirrors should be different"
5114
5115         rm -f $testfile $mirror1 $mirror2
5116
5117         $LFS setstripe -c1 -i0 $testfile
5118         dd if=$tmpfile of=$testfile bs=9000 count=1 conv=fsync ||
5119                 error "write to $testfile failed"
5120         $LFS getstripe $testfile
5121         cancel_lru_locks
5122
5123         $LFS migrate -i1 $testfile ||
5124                 error "migrate $testfile failed"
5125         $LFS getstripe $testfile
5126         stripe=$($LFS getstripe -i $testfile)
5127         [ $stripe -eq 1 ] || error "migrate file $testfile failed"
5128
5129         cancel_lru_locks
5130         cmp -bl $tmpfile $testfile ||
5131                 error "migrated file is corrupted"
5132
5133         $LFS mirror extend -N -i0 $testfile ||
5134                 error "mirror extend $testfile failed"
5135         $LFS getstripe $testfile
5136         mirror_count=$($LFS getstripe -N $testfile)
5137         [ $mirror_count -eq 2 ] ||
5138                 error "mirror extend file $testfile failed (1)"
5139         stripe=$($LFS getstripe --mirror-id=1 -i $testfile)
5140         [ $stripe -eq 1 ] || error "mirror extend file $testfile failed (2)"
5141         stripe=$($LFS getstripe --mirror-id=2 -i $testfile)
5142         [ $stripe -eq 0 ] || error "mirror extend file $testfile failed (3)"
5143
5144         cancel_lru_locks
5145         $LFS mirror verify -v $testfile ||
5146                 error "mirror verify failed"
5147         $LFS mirror read -N 1 -o $mirror1 $testfile ||
5148                 error "read from mirror 1 failed"
5149         cmp -bl $tmpfile $mirror1 ||
5150                 error "corruption of mirror 1"
5151         $LFS mirror read -N 2 -o $mirror2 $testfile ||
5152                 error "read from mirror 2 failed"
5153         cmp -bl $tmpfile $mirror2 ||
5154                 error "corruption of mirror 2"
5155
5156         $LFS mirror split --mirror-id 1 -f ${testfile}.mirror $testfile &&
5157                 error "mirror split -f should fail"
5158
5159         $LFS mirror split --mirror-id 1 $testfile &&
5160                 error "mirror split without -d should fail"
5161
5162         $LFS mirror split --mirror-id 1 -d $testfile ||
5163                 error "mirror split failed"
5164         $LFS getstripe $testfile
5165         mirror_count=$($LFS getstripe -N $testfile)
5166         [ $mirror_count -eq 1 ] ||
5167                 error "mirror split file $testfile failed (1)"
5168         stripe=$($LFS getstripe --mirror-id=1 -i $testfile)
5169         [ -z "$stripe" ] || error "mirror extend file $testfile failed (2)"
5170         stripe=$($LFS getstripe --mirror-id=2 -i $testfile)
5171         [ $stripe -eq 0 ] || error "mirror extend file $testfile failed (3)"
5172
5173         cancel_lru_locks
5174         cmp -bl $tmpfile $testfile ||
5175                 error "extended/split file is corrupted"
5176 }
5177 run_test 52 "Mirrored encrypted file"
5178
5179 test_53() {
5180         local testfile=$DIR/$tdir/$tfile
5181         local testfile2=$DIR2/$tdir/$tfile
5182         local tmpfile=$TMP/$tfile.tmp
5183         local resfile=$TMP/$tfile.res
5184         local pagesz
5185         local filemd5
5186
5187         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5188                 skip "client encryption not supported"
5189
5190         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5191                 skip "need dummy encryption support"
5192
5193         pagesz=$(getconf PAGESIZE)
5194         [[ $pagesz == 65536 ]] || skip "Need 64K PAGE_SIZE client"
5195
5196         do_node $mds1_HOST \
5197                 "mount.lustre --help |& grep -q 'test_dummy_encryption:'" ||
5198                         skip "need dummy encryption support on MDS client mount"
5199
5200         # this test is probably useless now, but may turn out to be useful when
5201         # Lustre supports servers with PAGE_SIZE != 4KB
5202         pagesz=$(do_node $mds1_HOST getconf PAGESIZE)
5203         [[ $pagesz == 4096 ]] || skip "Need 4K PAGE_SIZE MDS client"
5204
5205         stack_trap cleanup_for_enc_tests EXIT
5206         stack_trap "zconf_umount $mds1_HOST $MOUNT2" EXIT
5207         setup_for_enc_tests
5208
5209         $LFS setstripe -c1 -i0 $testfile
5210
5211         # write from 1st client
5212         cat /dev/urandom | tr -dc 'a-zA-Z0-9' |
5213                 dd of=$tmpfile bs=$((pagesz+3)) count=2 conv=fsync
5214         dd if=$tmpfile of=$testfile bs=$((pagesz+3)) count=2 conv=fsync ||
5215                 error "could not write to $testfile (1)"
5216
5217         # read from 2nd client
5218         # mount and IOs must be done in the same shell session, otherwise
5219         # encryption key in session keyring is missing
5220         do_node $mds1_HOST "mkdir -p $MOUNT2"
5221         do_node $mds1_HOST \
5222                 "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
5223                  $MGSNID:/$FSNAME $MOUNT2 && \
5224                  dd if=$testfile2 of=$resfile bs=$((pagesz+3)) count=2" ||
5225                 error "could not read from $testfile2 (1)"
5226
5227         # compare
5228         filemd5=$(do_node $mds1_HOST md5sum $resfile | awk '{print $1}')
5229         [ $filemd5 = $(md5sum $tmpfile | awk '{print $1}') ] ||
5230                 error "file is corrupted (1)"
5231         do_node $mds1_HOST rm -f $resfile
5232         cancel_lru_locks
5233
5234         # truncate from 2nd client
5235         $TRUNCATE $tmpfile $((pagesz+3))
5236         zconf_umount $mds1_HOST $MOUNT2 ||
5237                 error "umount $mds1_HOST $MOUNT2 failed (1)"
5238         do_node $mds1_HOST "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
5239                            $MGSNID:/$FSNAME $MOUNT2 && \
5240                            $TRUNCATE $testfile2 $((pagesz+3))" ||
5241                 error "could not truncate $testfile2 (1)"
5242
5243         # compare
5244         cmp -bl $tmpfile $testfile ||
5245                 error "file is corrupted (2)"
5246         rm -f $tmpfile $testfile
5247         cancel_lru_locks
5248         zconf_umount $mds1_HOST $MOUNT2 ||
5249                 error "umount $mds1_HOST $MOUNT2 failed (2)"
5250
5251         # do conversly
5252         do_node $mds1_HOST \
5253               dd if=/dev/urandom of=$tmpfile bs=$((pagesz+3)) count=2 conv=fsync
5254         # write from 2nd client
5255         do_node $mds1_HOST \
5256            "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
5257             $MGSNID:/$FSNAME $MOUNT2 && \
5258             dd if=$tmpfile of=$testfile2 bs=$((pagesz+3)) count=2 conv=fsync" ||
5259                 error "could not write to $testfile2 (2)"
5260
5261         # read from 1st client
5262         dd if=$testfile of=$resfile bs=$((pagesz+3)) count=2 ||
5263                 error "could not read from $testfile (2)"
5264
5265         # compare
5266         filemd5=$(do_node $mds1_HOST md5sum -b $tmpfile | awk '{print $1}')
5267         [ $filemd5 = $(md5sum -b $resfile | awk '{print $1}') ] ||
5268                 error "file is corrupted (3)"
5269         rm -f $resfile
5270         cancel_lru_locks
5271
5272         # truncate from 1st client
5273         do_node $mds1_HOST "$TRUNCATE $tmpfile $((pagesz+3))"
5274         $TRUNCATE $testfile $((pagesz+3)) ||
5275                 error "could not truncate $testfile (2)"
5276
5277         # compare
5278         zconf_umount $mds1_HOST $MOUNT2 ||
5279                 error "umount $mds1_HOST $MOUNT2 failed (3)"
5280         do_node $mds1_HOST "$MOUNT_CMD -o ${MOUNT_OPTS},test_dummy_encryption \
5281                            $MGSNID:/$FSNAME $MOUNT2 && \
5282                            cmp -bl $tmpfile $testfile2" ||
5283                 error "file is corrupted (4)"
5284
5285         do_node $mds1_HOST rm -f $tmpfile
5286         rm -f $tmpfile
5287 }
5288 run_test 53 "Mixed PAGE_SIZE clients"
5289
5290 test_54() {
5291         local testdir=$DIR/$tdir/$ID0
5292         local testdir2=$DIR2/$tdir/$ID0
5293         local testfile=$testdir/$tfile
5294         local testfile2=$testdir/${tfile}withveryverylongnametoexercisecode
5295         local testfile3=$testdir/_${tfile}
5296         local tmpfile=$TMP/${tfile}.tmp
5297         local resfile=$TMP/${tfile}.res
5298         local nameenc=""
5299         local fid1
5300         local fid2
5301
5302         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5303                 skip "client encryption not supported"
5304
5305         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5306                 skip "need dummy encryption support"
5307
5308         which fscrypt || skip "This test needs fscrypt userspace tool"
5309
5310         yes | fscrypt setup --force --verbose ||
5311                 error "fscrypt global setup failed"
5312         sed -i 's/\(.*\)policy_version\(.*\):\(.*\)\"[0-9]*\"\(.*\)/\1policy_version\2:\3"2"\4/' \
5313                 /etc/fscrypt.conf
5314         yes | fscrypt setup --verbose $MOUNT ||
5315                 error "fscrypt setup $MOUNT failed"
5316         mkdir -p $testdir
5317         chown -R $ID0:$ID0 $testdir
5318
5319         echo -e 'mypass\nmypass' | su - $USER0 -c "fscrypt encrypt --verbose \
5320                 --source=custom_passphrase --name=protector $testdir" ||
5321                 error "fscrypt encrypt failed"
5322
5323         echo -e 'mypass\nmypass' | su - $USER0 -c "fscrypt encrypt --verbose \
5324                 --source=custom_passphrase --name=protector2 $testdir" &&
5325                 error "second fscrypt encrypt should have failed"
5326
5327         mkdir -p ${testdir}2 || error "mkdir ${testdir}2 failed"
5328         touch ${testdir}2/f || error "mkdir ${testdir}2/f failed"
5329         cancel_lru_locks
5330
5331         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
5332                 --source=custom_passphrase --name=protector3 ${testdir}2 &&
5333                 error "fscrypt encrypt on non-empty dir should have failed"
5334
5335         $RUNAS dd if=/dev/urandom of=$testfile bs=127 count=1 conv=fsync ||
5336                 error "write to encrypted file $testfile failed"
5337         cp $testfile $tmpfile
5338         $RUNAS dd if=/dev/urandom of=$testfile2 bs=127 count=1 conv=fsync ||
5339                 error "write to encrypted file $testfile2 failed"
5340         $RUNAS dd if=/dev/urandom of=$testfile3 bs=127 count=1 conv=fsync ||
5341                 error "write to encrypted file $testfile3 failed"
5342         $RUNAS mkdir $testdir/subdir || error "mkdir subdir failed"
5343         $RUNAS touch $testdir/subdir/subfile || error "mkdir subdir failed"
5344
5345         $RUNAS fscrypt lock --verbose $testdir ||
5346                 error "fscrypt lock $testdir failed (1)"
5347
5348         $RUNAS ls -R $testdir || error "ls -R $testdir failed"
5349         local filecount=$($RUNAS find $testdir -type f | wc -l)
5350         [ $filecount -eq 4 ] || error "found $filecount files"
5351
5352         # check enable_filename_encryption default value
5353         # tunable only available for client built against embedded llcrypt
5354         $LCTL get_param mdc.*.connect_flags | grep -q name_encryption &&
5355           nameenc=$(lctl get_param -n llite.*.enable_filename_encryption |
5356                         head -n1)
5357         # If client is built against in-kernel fscrypt, it is not possible
5358         # to decide to encrypt file names or not: they are always encrypted.
5359         if [ -n "$nameenc" ]; then
5360                 [ $nameenc -eq 0 ] ||
5361                        error "enable_filename_encryption should be 0 by default"
5362
5363                 # $testfile, $testfile2 and $testfile3 should exist because
5364                 # names are not encrypted
5365                 [ -f $testfile ] ||
5366                       error "$testfile should exist because name not encrypted"
5367                 [ -f $testfile2 ] ||
5368                       error "$testfile2 should exist because name not encrypted"
5369                 [ -f $testfile3 ] ||
5370                       error "$testfile3 should exist because name not encrypted"
5371                 stat $testfile3
5372                 [ $? -eq 0 ] || error "cannot stat $testfile3 without key"
5373         fi
5374
5375         scrambledfiles=( $(find $testdir/ -maxdepth 1 -type f) )
5376         $RUNAS hexdump -C ${scrambledfiles[0]} &&
5377                 error "reading ${scrambledfiles[0]} should fail without key"
5378
5379         $RUNAS touch ${testfile}.nokey &&
5380                 error "touch ${testfile}.nokey should have failed without key"
5381
5382         echo mypass | $RUNAS fscrypt unlock --verbose $testdir ||
5383                 error "fscrypt unlock $testdir failed (1)"
5384
5385         $RUNAS cat $testfile > $resfile ||
5386                 error "reading $testfile failed"
5387
5388         cmp -bl $tmpfile $resfile || error "file read differs from file written"
5389         stat $testfile3
5390         [ $? -eq 0 ] || error "cannot stat $testfile3 with key"
5391
5392         $RUNAS fscrypt lock --verbose $testdir ||
5393                 error "fscrypt lock $testdir failed (2)"
5394
5395         $RUNAS hexdump -C ${scrambledfiles[1]} &&
5396                 error "reading ${scrambledfiles[1]} should fail without key"
5397
5398         # server local client incompatible with SSK keys installed
5399         if [ "$SHARED_KEY" != true ]; then
5400                 mount_mds_client
5401                 stack_trap umount_mds_client EXIT
5402                 do_facet $SINGLEMDS touch $DIR2/$tdir/newfile
5403                 mdsscrambledfile=$(do_facet $SINGLEMDS find $testdir2/ \
5404                                         -maxdepth 1 -type f | head -n1)
5405                 [ -n "$mdsscrambledfile" ] || error "could not find file"
5406                 do_facet $SINGLEMDS cat "$mdsscrambledfile" &&
5407                         error "reading $mdsscrambledfile should fail on MDS"
5408                 do_facet $SINGLEMDS "echo aaa >> \"$mdsscrambledfile\"" &&
5409                         error "writing $mdsscrambledfile should fail on MDS"
5410                 do_facet $SINGLEMDS $MULTIOP $testdir2/fileA m &&
5411                         error "creating $testdir2/fileA should fail on MDS"
5412                 do_facet $SINGLEMDS mkdir $testdir2/dirA &&
5413                         error "mkdir $testdir2/dirA should fail on MDS"
5414                 do_facet $SINGLEMDS ln -s $DIR2/$tdir/newfile $testdir2/sl1 &&
5415                         error "ln -s $testdir2/sl1 should fail on MDS"
5416                 do_facet $SINGLEMDS ln $DIR2/$tdir/newfile $testdir2/hl1 &&
5417                         error "ln $testdir2/hl1 should fail on MDS"
5418                 do_facet $SINGLEMDS mv "$mdsscrambledfile" $testdir2/fB &&
5419                         error "mv $mdsscrambledfile should fail on MDS"
5420                 do_facet $SINGLEMDS mrename "$mdsscrambledfile" $testdir2/fB &&
5421                         error "mrename $mdsscrambledfile should fail on MDS"
5422                 do_facet $SINGLEMDS rm -f $DIR2/$tdir/newfile
5423         fi
5424
5425         echo mypass | $RUNAS fscrypt unlock --verbose $testdir ||
5426                 error "fscrypt unlock $testdir failed (2)"
5427
5428         rm -rf $testdir/*
5429         $RUNAS fscrypt lock --verbose $testdir ||
5430                 error "fscrypt lock $testdir failed (3)"
5431
5432         rm -rf $tmpfile $resfile $testdir ${testdir}2 $MOUNT/.fscrypt
5433
5434         # remount client with subdirectory mount
5435         umount_client $MOUNT || error "umount $MOUNT failed (1)"
5436         export FILESET=/$tdir
5437         mount_client $MOUNT ${MOUNT_OPTS} || error "remount failed (1)"
5438         export FILESET=""
5439         wait_ssk
5440
5441         # setup encryption from inside this subdir mount
5442         # the .fscrypt directory is going to be created at the real fs root
5443         yes | fscrypt setup --verbose $MOUNT ||
5444                 error "fscrypt setup $MOUNT failed (2)"
5445         testdir=$MOUNT/vault
5446         mkdir $testdir
5447         chown -R $ID0:$ID0 $testdir
5448         fid1=$(path2fid $MOUNT/.fscrypt)
5449         echo "With FILESET $tdir, .fscrypt FID is $fid1"
5450
5451         # enable name encryption, only valid if built against embedded llcrypt
5452         if [ -n "$nameenc" ]; then
5453                 do_facet mgs $LCTL set_param -P \
5454                         llite.*.enable_filename_encryption=1
5455                 [ $? -eq 0 ] ||
5456                         error "set_param -P \
5457                                 llite.*.enable_filename_encryption failed"
5458
5459                 wait_update_facet --verbose client \
5460                         "$LCTL get_param -n llite.*.enable_filename_encryption \
5461                         | head -n1" 1 30 ||
5462                         error "enable_filename_encryption not set on client"
5463         fi
5464
5465         # encrypt 'vault' dir inside the subdir mount
5466         echo -e 'mypass\nmypass' | su - $USER0 -c "fscrypt encrypt --verbose \
5467                 --source=custom_passphrase --name=protector $testdir" ||
5468                 error "fscrypt encrypt failed"
5469
5470         echo abc > $tmpfile
5471         chmod 666 $tmpfile
5472         $RUNAS cp $tmpfile $testdir/encfile
5473
5474         $RUNAS fscrypt lock --verbose $testdir ||
5475                 error "fscrypt lock $testdir failed (4)"
5476
5477         # encfile should actually have its name encrypted
5478         if [ -n "$nameenc" ]; then
5479                 [ -f $testdir/encfile ] &&
5480                         error "encfile name should be encrypted"
5481         fi
5482         filecount=$(find $testdir -type f | wc -l)
5483         [ $filecount -eq 1 ] || error "found $filecount files instead of 1"
5484
5485         # remount client with encrypted dir as subdirectory mount
5486         umount_client $MOUNT || error "umount $MOUNT failed (2)"
5487         export FILESET=/$tdir/vault
5488         mount_client $MOUNT ${MOUNT_OPTS} || error "remount failed (2)"
5489         export FILESET=""
5490         wait_ssk
5491         ls -laR $MOUNT
5492         fid2=$(path2fid $MOUNT/.fscrypt)
5493         echo "With FILESET $tdir/vault, .fscrypt FID is $fid2"
5494         [ "$fid1" == "$fid2" ] || error "fid1 $fid1 != fid2 $fid2 (1)"
5495
5496         # all content seen by this mount is encrypted, but .fscrypt is virtually
5497         # presented, letting us call fscrypt lock/unlock
5498         echo mypass | $RUNAS fscrypt unlock --verbose $MOUNT ||
5499                 error "fscrypt unlock $MOUNT failed (3)"
5500
5501         ls -laR $MOUNT
5502         [ $(cat $MOUNT/encfile) == "abc" ] || error "cat encfile failed"
5503
5504         # remount client without subdir mount
5505         umount_client $MOUNT || error "umount $MOUNT failed (3)"
5506         mount_client $MOUNT ${MOUNT_OPTS} || error "remount failed (3)"
5507         wait_ssk
5508         ls -laR $MOUNT
5509         fid2=$(path2fid $MOUNT/.fscrypt)
5510         echo "Without FILESET, .fscrypt FID is $fid2"
5511         [ "$fid1" == "$fid2" ] || error "fid1 $fid1 != fid2 $fid2 (2)"
5512
5513         # because .fscrypt was actually created at the real root of the fs,
5514         # we can call fscrypt lock/unlock on the encrypted dir
5515         echo mypass | $RUNAS fscrypt unlock --verbose $DIR/$tdir/vault ||
5516                 error "fscrypt unlock $$DIR/$tdir/vault failed (4)"
5517
5518         ls -laR $MOUNT
5519         echo c >> $DIR/$tdir/vault/encfile || error "write to encfile failed"
5520
5521         rm -rf $DIR/$tdir/vault/*
5522         $RUNAS fscrypt lock --verbose $DIR/$tdir/vault ||
5523                 error "fscrypt lock $DIR/$tdir/vault failed (5)"
5524
5525         # disable name encryption, only valid if built against embedded llcrypt
5526         if [ -n "$nameenc" ]; then
5527                 do_facet mgs $LCTL set_param -P \
5528                         llite.*.enable_filename_encryption=0
5529                 [ $? -eq 0 ] ||
5530                         error "set_param -P \
5531                                 llite.*.enable_filename_encryption failed"
5532
5533                 wait_update_facet --verbose client \
5534                         "$LCTL get_param -n llite.*.enable_filename_encryption \
5535                         | head -n1" 0 30 ||
5536                         error "enable_filename_encryption not set back to default"
5537         fi
5538
5539         rm -rf $tmpfile $MOUNT/.fscrypt
5540 }
5541 run_test 54 "Encryption policies with fscrypt"
5542
5543 setup_local_client_nodemap() {
5544         local nm_name=${1:-"c0"}
5545         local nm_admin_val=${2:-0}
5546         local nm_trusted_val=${3:-0}
5547         local rc
5548
5549         if $SHARED_KEY; then
5550                 export SK_UNIQUE_NM=true
5551                 export FILESET="/"
5552         fi
5553
5554         do_facet mgs $LCTL nodemap_del $nm_name || true
5555         wait_nm_sync $nm_name id ''
5556
5557         do_facet mgs $LCTL nodemap_modify --name default \
5558                 --property admin --value 1
5559         do_facet mgs $LCTL nodemap_modify --name default \
5560                 --property trusted --value 1
5561         wait_nm_sync default trusted_nodemap
5562
5563         client_ip=$(host_nids_address $HOSTNAME $NETTYPE)
5564         client_nid=$(h2nettype $client_ip)
5565         do_facet mgs $LCTL nodemap_add $nm_name
5566         do_facet mgs $LCTL nodemap_add_range \
5567                 --name $nm_name --range $client_nid ||
5568                 error "Add range $client_nid to $nm_name failed rc = $?"
5569         do_facet mgs $LCTL nodemap_modify --name $nm_name \
5570                 --property admin --value $nm_admin_val
5571         do_facet mgs $LCTL nodemap_modify --name $nm_name \
5572                 --property trusted --value $nm_trusted_val
5573
5574         do_facet mgs $LCTL nodemap_activate 1
5575         wait_nm_sync active
5576 }
5577
5578 cleanup_local_client_nodemap() {
5579         local nm_name=${1:-"c0"}
5580
5581         do_facet mgs $LCTL nodemap_del $nm_name
5582         do_facet mgs $LCTL nodemap_modify --name default \
5583                 --property admin --value 0
5584         do_facet mgs $LCTL nodemap_modify --name default \
5585                 --property trusted --value 0
5586         wait_nm_sync default trusted_nodemap
5587
5588         do_facet mgs $LCTL nodemap_activate 0
5589         wait_nm_sync active 0
5590
5591         if $SHARED_KEY; then
5592                 unset FILESET
5593                 export SK_UNIQUE_NM=false
5594         fi
5595         if ! is_mounted $MOUNT; then
5596                 mount_client $MOUNT ${MOUNT_OPTS} || error "re-mount failed"
5597                 wait_ssk
5598         fi
5599 }
5600
5601 cleanup_local_client_nodemap_with_mounts() {
5602         # unmount client
5603         if is_mounted $MOUNT; then
5604                 umount_client $MOUNT || error "umount $MOUNT failed"
5605         fi
5606
5607         # reset and deactivate nodemaps, remount client
5608         cleanup_local_client_nodemap
5609
5610         # remount client on $MOUNT_2
5611         if [ "$MOUNT_2" ]; then
5612                 mount_client $MOUNT2 ${MOUNT_OPTS} || error "remount failed"
5613         fi
5614         wait_ssk
5615 }
5616
5617 test_55() {
5618         (( $MDS1_VERSION > $(version_code 2.12.6.2) )) ||
5619                 skip "Need MDS version at least 2.12.6.3"
5620
5621         local client_ip
5622         local client_nid
5623
5624         mkdir -p $DIR/$tdir/$USER0/testdir_groups
5625         chown root:$USER0 $DIR/$tdir/$USER0
5626         chmod 770 $DIR/$tdir/$USER0
5627         chmod g+s $DIR/$tdir/$USER0
5628         chown $USER0:$USER0 $DIR/$tdir/$USER0/testdir_groups
5629         chmod 770 $DIR/$tdir/$USER0/testdir_groups
5630         chmod g+s $DIR/$tdir/$USER0/testdir_groups
5631
5632         # unmount client completely
5633         umount_client $MOUNT || error "umount $MOUNT failed"
5634         if is_mounted $MOUNT2; then
5635                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
5636         fi
5637
5638         do_nodes $(comma_list $(all_mdts_nodes)) \
5639                 $LCTL set_param mdt.*.identity_upcall=NONE
5640
5641         stack_trap cleanup_local_client_nodemap_with_mounts EXIT
5642
5643         setup_local_client_nodemap "c0" 0 1
5644
5645         # remount client to take nodemap into account
5646         zconf_mount_clients $HOSTNAME $MOUNT $MOUNT_OPTS ||
5647                 error "remount failed"
5648         unset FILESET
5649         wait_ssk
5650
5651         euid_access $USER0 $DIR/$tdir/$USER0/testdir_groups/file
5652 }
5653 run_test 55 "access with seteuid"
5654
5655 test_56() {
5656         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
5657         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
5658
5659         local testfile=$DIR/$tdir/$tfile
5660
5661         [[ $(facet_fstype ost1) == zfs ]] && skip "skip ZFS backend"
5662
5663         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5664                 skip "client encryption not supported"
5665
5666         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5667                 skip "need dummy encryption support"
5668
5669         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5670
5671         stack_trap cleanup_for_enc_tests EXIT
5672         setup_for_enc_tests
5673
5674         $LFS setstripe -c1 $testfile
5675         dd if=/dev/urandom of=$testfile bs=1M count=3 conv=fsync
5676         filefrag -v $testfile || error "filefrag $testfile failed"
5677         (( $(filefrag -v $testfile | grep -c encrypted) >= 1 )) ||
5678                 error "filefrag $testfile does not show encrypted flag"
5679         (( $(filefrag -v $testfile | grep -c encoded) >= 1 )) ||
5680                 error "filefrag $testfile does not show encoded flag"
5681 }
5682 run_test 56 "FIEMAP on encrypted file"
5683
5684 test_57() {
5685         local testdir=$DIR/$tdir/mytestdir
5686         local testfile=$DIR/$tdir/$tfile
5687
5688         [[ $(facet_fstype ost1) == zfs ]] && skip "skip ZFS backend"
5689
5690         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5691                 skip "client encryption not supported"
5692
5693         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5694                 skip "need dummy encryption support"
5695
5696         mkdir $DIR/$tdir
5697         mkdir $testdir
5698         setfattr -n security.c -v myval $testdir &&
5699                 error "setting xattr on $testdir should have failed (1.1)"
5700         setfattr -n encryption.c -v myval $testdir &&
5701                 error "setting xattr on $testdir should have failed (1.2)"
5702         touch $testfile
5703         setfattr -n security.c -v myval $testfile &&
5704                 error "setting xattr on $testfile should have failed (1.1)"
5705         setfattr -n encryption.c -v myval $testfile &&
5706                 error "setting xattr on $testfile should have failed (1.2)"
5707
5708         rm -rf $DIR/$tdir
5709
5710         stack_trap cleanup_for_enc_tests EXIT
5711         setup_for_enc_tests
5712
5713         mkdir $testdir
5714         if [ $(getfattr -n security.c $testdir 2>&1 |
5715                grep -ci "Operation not permitted") -eq 0 ]; then
5716                 error "getting xattr on $testdir should have failed (1.1)"
5717         fi
5718         if [ $(getfattr -n encryption.c $testdir 2>&1 |
5719                grep -ci "Operation not supported") -eq 0 ]; then
5720                 error "getting xattr on $testdir should have failed (1.2)"
5721         fi
5722         getfattr -d -m - $testdir 2>&1 | grep security\.c &&
5723                 error "listing xattrs on $testdir should not expose security.c"
5724         getfattr -d -m - $testdir 2>&1 | grep encryption\.c &&
5725                error "listing xattrs on $testdir should not expose encryption.c"
5726         if [ $(setfattr -n security.c -v myval $testdir 2>&1 |
5727                grep -ci "Operation not permitted") -eq 0 ]; then
5728                 error "setting xattr on $testdir should have failed (2.1)"
5729         fi
5730         if [ $(setfattr -n encryption.c -v myval $testdir 2>&1 |
5731                grep -ci "Operation not supported") -eq 0 ]; then
5732                 error "setting xattr on $testdir should have failed (2.2)"
5733         fi
5734         touch $testfile
5735         if [ $(getfattr -n security.c $testfile 2>&1 |
5736                grep -ci "Operation not permitted") -eq 0 ]; then
5737                 error "getting xattr on $testfile should have failed (1.1)"
5738         fi
5739         if [ $(getfattr -n encryption.c $testfile 2>&1 |
5740                grep -ci "Operation not supported") -eq 0 ]; then
5741                 error "getting xattr on $testfile should have failed (1.2)"
5742         fi
5743         getfattr -d -m - $testfile 2>&1 | grep security\.c &&
5744                 error "listing xattrs on $testfile should not expose security.c"
5745         getfattr -d -m - $testfile 2>&1 | grep encryption\.c &&
5746               error "listing xattrs on $testfile should not expose encryption.c"
5747         if [ $(setfattr -n security.c -v myval $testfile 2>&1 |
5748                grep -ci "Operation not permitted") -eq 0 ]; then
5749                 error "setting xattr on $testfile should have failed (2.1)"
5750         fi
5751         if [ $(setfattr -n encryption.c -v myval $testfile 2>&1 |
5752                grep -ci "Operation not supported") -eq 0 ]; then
5753                 error "setting xattr on $testfile should have failed (2.2)"
5754         fi
5755         return 0
5756 }
5757 run_test 57 "security.c/encryption.c xattr protection"
5758
5759 test_58() {
5760         local testdir=$DIR/$tdir/mytestdir
5761         local testfile=$DIR/$tdir/$tfile
5762
5763         [[ $(facet_fstype ost1) == zfs ]] && skip "skip ZFS backend"
5764
5765         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5766                 skip "client encryption not supported"
5767
5768         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5769                 skip "need dummy encryption support"
5770
5771         stack_trap cleanup_for_enc_tests EXIT
5772         setup_for_enc_tests
5773
5774         touch $DIR/$tdir/$tfile
5775         mkdir $DIR/$tdir/subdir
5776
5777         cancel_lru_locks
5778         sync ; sync
5779         echo 3 > /proc/sys/vm/drop_caches
5780
5781         ll_decode_linkea $DIR/$tdir/$tfile || error "cannot read $tfile linkea"
5782         ll_decode_linkea $DIR/$tdir/subdir || error "cannot read subdir linkea"
5783
5784         for ((i = 0; i < 1000; i = $((i+1)))); do
5785                 mkdir -p $DIR/$tdir/d${i}
5786                 touch $DIR/$tdir/f${i}
5787                 createmany -m $DIR/$tdir/d${i}/f 5 > /dev/null
5788         done
5789
5790         cancel_lru_locks
5791         sync ; sync
5792         echo 3 > /proc/sys/vm/drop_caches
5793
5794         sleep 10
5795         ls -ailR $DIR/$tdir > /dev/null || error "fail to ls"
5796 }
5797 run_test 58 "access to enc file's xattrs"
5798
5799 verify_mirror() {
5800         local mirror1=$TMP/$tfile.mirror1
5801         local mirror2=$TMP/$tfile.mirror2
5802         local testfile=$1
5803         local reffile=$2
5804
5805         $LFS mirror verify -vvv $testfile ||
5806                 error "verifying mirror failed (1)"
5807         if [ $($LFS mirror verify -v $testfile 2>&1 |
5808                 grep -ci "only valid") -ne 0 ]; then
5809                 error "verifying mirror failed (2)"
5810         fi
5811
5812         $LFS mirror read -N 1 -o $mirror1 $testfile ||
5813                 error "read from mirror 1 failed"
5814         cmp -bl $reffile $mirror1 ||
5815                 error "corruption of mirror 1"
5816         $LFS mirror read -N 2 -o $mirror2 $testfile ||
5817                 error "read from mirror 2 failed"
5818         cmp -bl $reffile $mirror2 ||
5819                 error "corruption of mirror 2"
5820 }
5821
5822 test_59a() {
5823         local testfile=$DIR/$tdir/$tfile
5824         local tmpfile=$TMP/$tfile
5825         local mirror1=$TMP/$tfile.mirror1
5826         local mirror2=$TMP/$tfile.mirror2
5827         local scrambledfile
5828
5829         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5830                 skip "client encryption not supported"
5831
5832         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5833                 skip "need dummy encryption support"
5834
5835         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5836
5837         stack_trap "cleanup_for_enc_tests $tmpfile $mirror1 $mirror2" EXIT
5838         setup_for_enc_tests
5839
5840         dd if=/dev/urandom of=$tmpfile bs=5000 count=1 conv=fsync
5841
5842         $LFS mirror create -N -i0 -N -i1 $testfile ||
5843                 error "could not create mirror"
5844         dd if=$tmpfile of=$testfile bs=5000 count=1 conv=fsync ||
5845                 error "could not write to $testfile"
5846         $LFS getstripe $testfile
5847
5848         # remount without dummy encryption key
5849         remount_client_normally
5850
5851         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type f)
5852         $LFS mirror resync $scrambledfile ||
5853                 error "could not resync mirror"
5854
5855         $LFS mirror verify -vvv $scrambledfile ||
5856                 error "mirror verify failed (1)"
5857         if [ $($LFS mirror verify -v $scrambledfile 2>&1 |
5858                 grep -ci "only valid") -ne 0 ]; then
5859                 error "mirror verify failed (2)"
5860         fi
5861
5862         $LFS mirror read -N 1 -o $mirror1 $scrambledfile &&
5863                 error "read from mirror should fail"
5864
5865         # now, with the key
5866         remount_client_dummykey
5867         verify_mirror $testfile $tmpfile
5868 }
5869 run_test 59a "mirror resync of encrypted files without key"
5870
5871 test_59b() {
5872         local testfile=$DIR/$tdir/$tfile
5873         local tmpfile=$TMP/$tfile
5874         local mirror1=$TMP/$tfile.mirror1
5875         local mirror2=$TMP/$tfile.mirror2
5876         local scrambledfile
5877
5878         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5879                 skip "client encryption not supported"
5880
5881         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5882                 skip "need dummy encryption support"
5883
5884         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5885
5886         stack_trap "cleanup_for_enc_tests $tmpfile $mirror1 $mirror2" EXIT
5887         setup_for_enc_tests
5888
5889         tr '\0' '2' < /dev/zero |
5890                 dd of=$tmpfile bs=1 count=9000 conv=fsync
5891
5892         $LFS setstripe -c1 -i0 $testfile
5893         dd if=$tmpfile of=$testfile bs=9000 count=1 conv=fsync ||
5894                 error "write to $testfile failed"
5895         $LFS getstripe $testfile
5896
5897         # remount without dummy encryption key
5898         remount_client_normally
5899
5900         scrambledfile=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type f)
5901         $LFS migrate -i1 $scrambledfile ||
5902                 error "migrate $scrambledfile failed"
5903         $LFS getstripe $scrambledfile
5904         stripe=$($LFS getstripe -i $scrambledfile)
5905         [ $stripe -eq 1 ] || error "migrate file $scrambledfile failed"
5906         cancel_lru_locks
5907
5908         # now, with the key
5909         remount_client_dummykey
5910         cmp -bl $tmpfile $testfile ||
5911                 error "migrated file is corrupted"
5912
5913         # remount without dummy encryption key
5914         remount_client_normally
5915
5916         $LFS mirror extend -N -i0 $scrambledfile ||
5917                 error "mirror extend $scrambledfile failed (1)"
5918         $LFS getstripe $scrambledfile
5919         mirror_count=$($LFS getstripe -N $scrambledfile)
5920         [ $mirror_count -eq 2 ] ||
5921                 error "mirror extend file $scrambledfile failed (2)"
5922         stripe=$($LFS getstripe --mirror-id=1 -i $scrambledfile)
5923         [ $stripe -eq 1 ] ||
5924                 error "mirror extend file $scrambledfile failed (3)"
5925         stripe=$($LFS getstripe --mirror-id=2 -i $scrambledfile)
5926         [ $stripe -eq 0 ] ||
5927                 error "mirror extend file $scrambledfile failed (4)"
5928
5929         $LFS mirror verify -vvv $scrambledfile ||
5930                 error "mirror verify failed (1)"
5931         if [ $($LFS mirror verify -v $scrambledfile 2>&1 |
5932                 grep -ci "only valid") -ne 0 ]; then
5933                 error "mirror verify failed (2)"
5934         fi
5935
5936         # now, with the key
5937         remount_client_dummykey
5938         verify_mirror $testfile $tmpfile
5939
5940         # remount without dummy encryption key
5941         remount_client_normally
5942
5943         $LFS mirror split --mirror-id 1 -d $scrambledfile ||
5944                 error "mirror split file $scrambledfile failed (1)"
5945         $LFS getstripe $scrambledfile
5946         mirror_count=$($LFS getstripe -N $scrambledfile)
5947         [ $mirror_count -eq 1 ] ||
5948                 error "mirror split file $scrambledfile failed (2)"
5949         stripe=$($LFS getstripe --mirror-id=1 -i $scrambledfile)
5950         [ -z "$stripe" ] || error "mirror split file $scrambledfile failed (3)"
5951         stripe=$($LFS getstripe --mirror-id=2 -i $scrambledfile)
5952         [ $stripe -eq 0 ] || error "mirror split file $scrambledfile failed (4)"
5953
5954         # now, with the key
5955         remount_client_dummykey
5956         cancel_lru_locks
5957         cmp -bl $tmpfile $testfile ||
5958                 error "extended/split file is corrupted"
5959 }
5960 run_test 59b "migrate/extend/split of encrypted files without key"
5961
5962 test_59c() {
5963         local dirname=$DIR/$tdir/subdir
5964         local scrambleddir
5965
5966         $LCTL get_param mdc.*.import | grep -q client_encryption ||
5967                 skip "client encryption not supported"
5968
5969         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
5970                 skip "need dummy encryption support"
5971
5972         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
5973
5974         (( "$MDS1_VERSION" > $(version_code 2.14.54.54) )) ||
5975                 skip "MDT migration not supported with older server"
5976
5977         stack_trap cleanup_for_enc_tests EXIT
5978         setup_for_enc_tests
5979
5980         $LFS setdirstripe -i 0 $dirname
5981         echo b > $dirname/subf
5982
5983         # remount without dummy encryption key
5984         remount_client_normally
5985
5986         scrambleddir=$(find $DIR/$tdir/ -maxdepth 1 -mindepth 1 -type d)
5987
5988         # migrate a non-empty encrypted dir
5989         $LFS migrate -m 1 $scrambleddir ||
5990                 error "migrate $scrambleddir between MDTs failed (1)"
5991
5992         stripe=$($LFS getdirstripe -i $scrambleddir)
5993         [ $stripe -eq 1 ] ||
5994                 error "migrate $scrambleddir between MDTs failed (2)"
5995
5996         # now, with the key
5997         insert_enc_key
5998         [ -f $dirname/subf ] ||
5999             error "migrate $scrambleddir between MDTs failed (3)"
6000         [ $(cat $dirname/subf) == "b" ] ||
6001             error "migrate $scrambleddir between MDTs failed (4)"
6002 }
6003 run_test 59c "MDT migrate of encrypted files without key"
6004
6005 test_60() {
6006         local testdir=$DIR/$tdir/mytestdir
6007         local testfile=$DIR/$tdir/$tfile
6008
6009         (( $MDS1_VERSION > $(version_code 2.14.53) )) ||
6010                 skip "Need MDS version at least 2.14.53"
6011
6012         $LCTL get_param mdc.*.import | grep -q client_encryption ||
6013                 skip "client encryption not supported"
6014
6015         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
6016                 skip "need dummy encryption support"
6017
6018         stack_trap cleanup_for_enc_tests EXIT
6019         setup_for_enc_tests
6020
6021         echo a > $DIR/$tdir/file1
6022         mkdir $DIR/$tdir/subdir
6023         echo b > $DIR/$tdir/subdir/subfile1
6024
6025         remove_enc_key
6026         # unmount client completely
6027         umount_client $MOUNT || error "umount $MOUNT failed"
6028         if is_mounted $MOUNT2; then
6029                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
6030         fi
6031
6032         # remount client with subdirectory mount
6033         export FILESET=/$tdir
6034         mount_client $MOUNT ${MOUNT_OPTS} || error "remount failed"
6035         if [ "$MOUNT_2" ]; then
6036                 mount_client $MOUNT2 ${MOUNT_OPTS} || error "remount failed"
6037         fi
6038         wait_ssk
6039
6040         ls -Rl $DIR || error "ls -Rl $DIR failed (1)"
6041
6042         # now, with the key
6043         remount_client_dummykey
6044         export FILESET=""
6045
6046         ls -Rl $DIR || error "ls -Rl $DIR failed (2)"
6047         cat $DIR/file1 || error "cat $DIR/$tdir/file1 failed"
6048         cat $DIR/subdir/subfile1 ||
6049                 error "cat $DIR/$tdir/subdir/subfile1 failed"
6050 }
6051 run_test 60 "Subdirmount of encrypted dir"
6052
6053 test_61() {
6054         local testfile=$DIR/$tdir/$tfile
6055         local readonly
6056
6057         readonly=$(do_facet mgs \
6058                         lctl get_param -n nodemap.default.readonly_mount)
6059         [ -n "$readonly" ] ||
6060                 skip "Server does not have readonly_mount nodemap flag"
6061
6062         stack_trap cleanup_local_client_nodemap EXIT
6063         for idx in $(seq 1 $MDSCOUNT); do
6064                 wait_recovery_complete mds$idx
6065         done
6066         umount_client $MOUNT || error "umount $MOUNT failed (1)"
6067
6068         # Activate nodemap, and mount rw.
6069         # Should succeed as rw mount is not forbidden by default.
6070         setup_local_client_nodemap "c0" 1 1
6071         readonly=$(do_facet mgs \
6072                         lctl get_param -n nodemap.default.readonly_mount)
6073         [ $readonly -eq 0 ] ||
6074                 error "wrong default value for readonly_mount on default nodemap"
6075         readonly=$(do_facet mgs \
6076                         lctl get_param -n nodemap.c0.readonly_mount)
6077         [ $readonly -eq 0 ] ||
6078                 error "wrong default value for readonly_mount on nodemap c0"
6079
6080         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS},rw ||
6081                 error "mount '-o rw' failed with default"
6082         wait_ssk
6083         findmnt $MOUNT --output=options -n -f | grep -q "rw," ||
6084                 error "should be rw mount"
6085         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6086         echo a > $testfile || error "write $testfile failed"
6087         umount_client $MOUNT || error "umount $MOUNT failed (2)"
6088
6089         # Now enforce read-only, and retry.
6090         do_facet mgs $LCTL nodemap_modify --name c0 \
6091                 --property readonly_mount --value 1
6092         wait_nm_sync c0 readonly_mount
6093
6094         # mount without option should turn into ro
6095         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS} ||
6096                 error "mount failed (1)"
6097         findmnt $MOUNT --output=options -n -f | grep -q "ro," ||
6098                 error "mount should have been turned into ro"
6099         cat $testfile || error "read $testfile failed (1)"
6100         echo b > $testfile && error "write $testfile should fail (1)"
6101         umount_client $MOUNT || error "umount $MOUNT failed (3)"
6102
6103         # mount rw should turn into ro
6104         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS},rw ||
6105                 error "mount '-o rw' failed"
6106         findmnt $MOUNT --output=options -n -f | grep -q "ro," ||
6107                 error "mount rw should have been turned into ro"
6108         cat $testfile || error "read $testfile failed (2)"
6109         echo b > $testfile && error "write $testfile should fail (2)"
6110         umount_client $MOUNT || error "umount $MOUNT failed (4)"
6111
6112         # mount ro should work as expected
6113         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS},ro ||
6114                 error "mount '-o ro' failed"
6115         wait_ssk
6116         cat $testfile || error "read $testfile failed (3)"
6117         echo b > $testfile && error "write $testfile should fail (3)"
6118         umount_client $MOUNT || error "umount $MOUNT failed (5)"
6119
6120         # remount rw should not work
6121         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS} ||
6122                 error "mount failed (2)"
6123         mount_client $MOUNT remount,rw || error "remount failed"
6124         findmnt $MOUNT --output=options -n -f | grep -q "ro," ||
6125                 error "remount rw should have been turned into ro"
6126         cat $testfile || error "read $testfile failed (4)"
6127         echo b > $testfile && error "write $testfile should fail (4)"
6128         umount_client $MOUNT || error "umount $MOUNT failed (6)"
6129 }
6130 run_test 61 "Nodemap enforces read-only mount"
6131
6132 test_62() {
6133         local testdir=$DIR/$tdir/mytestdir
6134         local testfile=$DIR/$tdir/$tfile
6135
6136         [[ $(facet_fstype ost1) == zfs ]] && skip "skip ZFS backend"
6137
6138         (( $MDS1_VERSION > $(version_code 2.15.51) )) ||
6139                 skip "Need MDS version at least 2.15.51"
6140
6141         $LCTL get_param mdc.*.import | grep -q client_encryption ||
6142                 skip "client encryption not supported"
6143
6144         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
6145                 skip "need dummy encryption support"
6146
6147         stack_trap cleanup_for_enc_tests EXIT
6148         setup_for_enc_tests
6149
6150         lfs setstripe -c -1 $DIR/$tdir
6151         touch $DIR/$tdir/${tfile}_1 || error "touch ${tfile}_1 failed"
6152         dd if=/dev/zero of=$DIR/$tdir/${tfile}_2 bs=1 count=1 conv=fsync ||
6153                 error "dd ${tfile}_2 failed"
6154
6155         # unmount the Lustre filesystem
6156         stopall || error "stopping for e2fsck run"
6157
6158         # run e2fsck on the MDT and OST devices
6159         local mds_host=$(facet_active_host $SINGLEMDS)
6160         local ost_host=$(facet_active_host ost1)
6161         local mds_dev=$(mdsdevname ${SINGLEMDS//mds/})
6162         local ost_dev=$(ostdevname 1)
6163
6164         run_e2fsck $mds_host $mds_dev "-n"
6165         run_e2fsck $ost_host $ost_dev "-n"
6166
6167         # mount the Lustre filesystem
6168         setupall || error "remounting the filesystem failed"
6169 }
6170 run_test 62 "e2fsck with encrypted files"
6171
6172 create_files() {
6173         local path
6174
6175         for path in "${paths[@]}"; do
6176                 touch $path
6177         done
6178 }
6179
6180 build_fids() {
6181         local path
6182
6183         for path in "${paths[@]}"; do
6184                 fids+=("$(lfs path2fid $path)")
6185         done
6186 }
6187
6188 check_fids() {
6189         for fid in "${fids[@]}"; do
6190                 echo $fid
6191                 respath=$(lfs fid2path $MOUNT $fid)
6192                 echo -e "\t" $respath
6193                 ls -li $respath >/dev/null
6194                 [ $? -eq 0 ] || error "fid2path $fid failed"
6195         done
6196 }
6197
6198 test_63() {
6199         declare -a fids
6200         declare -a paths
6201         local vaultdir1=$DIR/$tdir/vault1==dir
6202         local vaultdir2=$DIR/$tdir/vault2==dir
6203         local longfname1="longfilenamewitha=inthemiddletotestbehaviorregardingthedigestedform"
6204         local longdname="longdirectorynamewitha=inthemiddletotestbehaviorregardingthedigestedform"
6205         local longfname2="$longdname/${longfname1}2"
6206
6207         (( $MDS1_VERSION > $(version_code 2.15.53) )) ||
6208                 skip "Need MDS version at least 2.15.53"
6209
6210         $LCTL get_param mdc.*.import | grep -q client_encryption ||
6211                 skip "client encryption not supported"
6212
6213         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
6214                 skip "need dummy encryption support"
6215
6216         which fscrypt || skip "This test needs fscrypt userspace tool"
6217
6218         yes | fscrypt setup --force --verbose ||
6219                 echo "fscrypt global setup already done"
6220         sed -i 's/\(.*\)policy_version\(.*\):\(.*\)\"[0-9]*\"\(.*\)/\1policy_version\2:\3"2"\4/' \
6221                 /etc/fscrypt.conf
6222         yes | fscrypt setup --verbose $MOUNT ||
6223                 echo "fscrypt setup $MOUNT already done"
6224
6225         # enable_filename_encryption tunable only available for client
6226         # built against embedded llcrypt. If client is built against in-kernel
6227         # fscrypt, file names are always encrypted.
6228         $LCTL get_param mdc.*.connect_flags | grep -q name_encryption &&
6229           nameenc=$(lctl get_param -n llite.*.enable_filename_encryption |
6230                         head -n1)
6231         if [ -n "$nameenc" ]; then
6232                 do_facet mgs $LCTL set_param -P \
6233                         llite.*.enable_filename_encryption=1
6234                 [ $? -eq 0 ] ||
6235                         error "set_param -P \
6236                                 llite.*.enable_filename_encryption=1 failed"
6237
6238                 wait_update_facet --verbose client \
6239                         "$LCTL get_param -n llite.*.enable_filename_encryption \
6240                         | head -n1" 1 30 ||
6241                         error "enable_filename_encryption not set on client"
6242         fi
6243
6244         mkdir -p $vaultdir1
6245         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
6246                 --source=custom_passphrase --name=protector_63_1 $vaultdir1 ||
6247                 error "fscrypt encrypt $vaultdir1 failed"
6248
6249         mkdir $vaultdir1/dirA
6250         mkdir $vaultdir1/$longdname
6251         paths=("$vaultdir1/fileA")
6252         paths+=("$vaultdir1/dirA/fileB")
6253         paths+=("$vaultdir1/$longfname1")
6254         paths+=("$vaultdir1/$longfname2")
6255         create_files
6256
6257         paths+=("$vaultdir1/dirA")
6258         paths+=("$vaultdir1/$longdname")
6259
6260         build_fids
6261         check_fids
6262
6263         fscrypt lock --verbose $vaultdir1 ||
6264                 error "fscrypt lock $vaultdir1 failed (1)"
6265
6266         check_fids
6267
6268         if [ -z "$nameenc" ]; then
6269                 echo "Rest of the test requires disabling name encryption"
6270                 exit 0
6271         fi
6272
6273         # disable name encryption
6274         do_facet mgs $LCTL set_param -P llite.*.enable_filename_encryption=0
6275         [ $? -eq 0 ] ||
6276                 error "set_param -P llite.*.enable_filename_encryption=0 failed"
6277
6278         wait_update_facet --verbose client \
6279                 "$LCTL get_param -n llite.*.enable_filename_encryption \
6280                 | head -n1" 0 30 ||
6281                 error "enable_filename_encryption not set back to default"
6282
6283         mkdir -p $vaultdir2
6284         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
6285                 --source=custom_passphrase --name=protector_63_2 $vaultdir2 ||
6286                 error "fscrypt encrypt $vaultdir2 failed"
6287
6288         mkdir $vaultdir2/dirA
6289         mkdir $vaultdir2/$longdname
6290         paths=()
6291         fids=()
6292         paths=("$vaultdir2/fileA")
6293         paths+=("$vaultdir2/dirA/fileB")
6294         paths+=("$vaultdir2/$longfname1")
6295         paths+=("$vaultdir2/$longfname2")
6296         create_files
6297
6298         paths+=("$vaultdir2/dirA")
6299         paths+=("$vaultdir2/$longdname")
6300
6301         build_fids
6302         check_fids
6303
6304         fscrypt lock --verbose $vaultdir2 ||
6305                 error "fscrypt lock $vaultdir2 failed (2)"
6306
6307         check_fids
6308
6309         rm -rf $MOUNT/.fscrypt
6310 }
6311 run_test 63 "fid2path with encrypted files"
6312
6313 test_64a() {
6314         local testfile=$DIR/$tdir/$tfile
6315         local srv_uc=""
6316         local local_admin=""
6317         local rbac
6318
6319         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6320                 skip "Need MDS >= 2.15.54 for role-based controls"
6321
6322         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6323                 srv_uc="server_upcall"
6324
6325         (( MDS1_VERSION >= $(version_code 2.16.52) )) &&
6326                 local_admin="local_admin"
6327
6328         stack_trap cleanup_local_client_nodemap EXIT
6329         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6330         setup_local_client_nodemap "c0" 1 1
6331
6332         # check default value for rbac is all
6333         rbac=$(do_facet mds $LCTL get_param -n nodemap.c0.rbac)
6334         for role in file_perms \
6335                     dne_ops \
6336                     quota_ops \
6337                     byfid_ops \
6338                     chlg_ops \
6339                     fscrypt_admin \
6340                     $srv_uc \
6341                     $local_admin \
6342                     ;
6343         do
6344                 [[ "$rbac" =~ "$role" ]] ||
6345                         error "role '$role' not in default '$rbac'"
6346         done
6347
6348         rbac="file_perms"
6349         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6350         do_facet mgs $LCTL nodemap_modify --name c0 \
6351                  --property rbac --value $rbac ||
6352                 error "setting rbac $rbac failed (1)"
6353         wait_nm_sync c0 rbac
6354         touch $testfile
6355         stack_trap "set +vx"
6356         set -vx
6357         chmod 777 $testfile || error "chmod failed"
6358         chown $TSTUSR:$TSTUSR $testfile || error "chown failed"
6359         chgrp $TSTUSR $testfile || error "chgrp failed"
6360         $LFS project -p 1000 $testfile || error "setting project failed"
6361         set +vx
6362         rm -f $testfile
6363         rbac="none"
6364         if [ -z "$srv_uc" ]; then
6365                 rbac="none"
6366         else
6367                 rbac="$srv_uc"
6368         fi
6369         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6370                 --value $rbac ||
6371                 error "setting rbac $rbac failed (2)"
6372         wait_nm_sync c0 rbac
6373         touch $testfile
6374         set -vx
6375         chmod 777 $testfile && error "chmod should fail"
6376         chown $TSTUSR:$TSTUSR $testfile && error "chown should fail"
6377         chgrp $TSTUSR $testfile && error "chgrp should fail"
6378         $LFS project -p 1000 $testfile && error "setting project should fail"
6379         set +vx
6380 }
6381 run_test 64a "Nodemap enforces file_perms RBAC roles"
6382
6383 test_64b() {
6384         local testdir=$DIR/$tdir/${tfile}.d
6385         local dir_restripe
6386         local srv_uc=""
6387         local rbac
6388
6389         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6390                 skip "Need MDS >= 2.15.54 for role-based controls"
6391
6392         (( MDSCOUNT >= 2 )) || skip "mdt count $MDSCOUNT, skipping dne_ops role"
6393
6394         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6395                 srv_uc="server_upcall"
6396
6397         stack_trap cleanup_local_client_nodemap EXIT
6398         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6399         setup_local_client_nodemap "c0" 1 1
6400
6401         dir_restripe=$(do_node $mds1_HOST \
6402                 "$LCTL get_param -n mdt.*MDT0000.enable_dir_restripe")
6403         [ -n "$dir_restripe" ] || dir_restripe=0
6404         do_nodes $(comma_list $(all_mdts_nodes)) \
6405                 $LCTL set_param mdt.*.enable_dir_restripe=1 ||
6406                         error "enabling dir_restripe failed"
6407         stack_trap "do_nodes $(comma_list $(all_mdts_nodes)) \
6408               $LCTL set_param mdt.*.enable_dir_restripe=$dir_restripe" EXIT
6409         rbac="dne_ops"
6410         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6411         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6412                  --value $rbac ||
6413                 error "setting rbac $rbac failed (1)"
6414         wait_nm_sync c0 rbac
6415         $LFS mkdir -i 0 ${testdir}_for_migr ||
6416                 error "$LFS mkdir ${testdir}_for_migr failed (1)"
6417         touch ${testdir}_for_migr/file001 ||
6418                 error "touch ${testdir}_for_migr/file001 failed (1)"
6419         $LFS mkdir -i 0 ${testdir}_mdt0 ||
6420                 error "$LFS mkdir ${testdir}_mdt0 failed (1)"
6421         $LFS mkdir -i 1 ${testdir}_mdt1 ||
6422                 error "$LFS mkdir ${testdir}_mdt1 failed (1)"
6423         set -vx
6424         $LFS mkdir -i 1 $testdir || error "$LFS mkdir failed (1)"
6425         rmdir $testdir
6426         $LFS mkdir -c 2 $testdir || error "$LFS mkdir failed (2)"
6427         rmdir $testdir
6428         mkdir $testdir
6429         $LFS setdirstripe -c 2 $testdir || error "$LFS setdirstripe failed"
6430         rmdir $testdir
6431         $LFS migrate -m 1 ${testdir}_for_migr || error "$LFS migrate failed"
6432         touch ${testdir}_mdt0/fileA || error "touch fileA failed (1)"
6433         mv ${testdir}_mdt0/fileA ${testdir}_mdt1/ || error "mv failed (1)"
6434         set +vx
6435         rm -rf ${testdir}*
6436         $LFS mkdir -i 0 ${testdir}_for_migr ||
6437                 error "$LFS mkdir ${testdir}_for_migr failed (2)"
6438         touch ${testdir}_for_migr/file001 ||
6439                 error "touch ${testdir}_for_migr/file001 failed (2)"
6440         $LFS mkdir -i 0 ${testdir}_mdt0 ||
6441                 error "$LFS mkdir ${testdir}_mdt0 failed (2)"
6442         $LFS mkdir -i 1 ${testdir}_mdt1 ||
6443                 error "$LFS mkdir ${testdir}_mdt1 failed (2)"
6444
6445         rbac="none"
6446         if [ -z "$srv_uc" ]; then
6447                 rbac="none"
6448         else
6449                 rbac="$srv_uc"
6450         fi
6451         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6452                 --value $rbac ||
6453                 error "setting rbac $rbac failed (2)"
6454         wait_nm_sync c0 rbac
6455         set -vx
6456         $LFS mkdir -i 1 $testdir && error "$LFS mkdir should fail (1)"
6457         $LFS mkdir -c 2 $testdir && error "$LFS mkdir should fail (2)"
6458         mkdir $testdir
6459         $LFS setdirstripe -c 2 $testdir && error "$LFS setdirstripe should fail"
6460         rmdir $testdir
6461         $LFS migrate -m 1 ${testdir}_for_migr &&
6462                 error "$LFS migrate should fail"
6463         touch ${testdir}_mdt0/fileA || error "touch fileA failed (2)"
6464         mv ${testdir}_mdt0/fileA ${testdir}_mdt1/ || error "mv failed (2)"
6465         set +vx
6466 }
6467 run_test 64b "Nodemap enforces dne_ops RBAC roles"
6468
6469 test_64c() {
6470         local srv_uc=""
6471         local rbac
6472
6473         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6474                 skip "Need MDS >= 2.15.54 for role-based controls"
6475
6476         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6477                 srv_uc="server_upcall"
6478
6479         stack_trap cleanup_local_client_nodemap EXIT
6480         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6481         setup_local_client_nodemap "c0" 1 1
6482
6483         rbac="quota_ops"
6484         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6485         do_facet mgs $LCTL nodemap_modify --name c0 \
6486                  --property rbac --value $rbac ||
6487                 error "setting rbac $rbac failed (1)"
6488         wait_nm_sync c0 rbac
6489         set -vx
6490         $LFS setquota -u $USER0 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT ||
6491                 error "lfs setquota -u failed"
6492         $LFS setquota -u $USER0 --delete $MOUNT
6493         $LFS setquota -g $USER0 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT ||
6494                 error "lfs setquota -g failed"
6495         $LFS setquota -g $USER0 --delete $MOUNT
6496         $LFS setquota -p 1000 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT ||
6497                 error "lfs setquota -p failed"
6498         $LFS setquota -p 1000 --delete $MOUNT
6499
6500         $LFS setquota -U -b 10G -B 11G -i 100K -I 105K $MOUNT ||
6501                 error "lfs setquota -U failed"
6502         $LFS setquota -U -b 0 -B 0 -i 0 -I 0 $MOUNT
6503         $LFS setquota -G -b 10G -B 11G -i 100K -I 105K $MOUNT ||
6504                 error "lfs setquota -G failed"
6505         $LFS setquota -G -b 0 -B 0 -i 0 -I 0 $MOUNT
6506         $LFS setquota -P -b 10G -B 11G -i 100K -I 105K $MOUNT ||
6507                 error "lfs setquota -P failed"
6508         $LFS setquota -P -b 0 -B 0 -i 0 -I 0 $MOUNT
6509         $LFS setquota -u $USER0 -D $MOUNT ||
6510                 error "lfs setquota -u -D failed"
6511         $LFS setquota -u $USER0 --delete $MOUNT
6512         $LFS setquota -g $USER0 -D $MOUNT ||
6513                 error "lfs setquota -g -D failed"
6514         $LFS setquota -g $USER0 --delete $MOUNT
6515         $LFS setquota -p 1000 -D $MOUNT ||
6516                 error "lfs setquota -p -D failed"
6517         $LFS setquota -p 1000 --delete $MOUNT
6518         set +vx
6519
6520         rbac="none"
6521         if [ -z "$srv_uc" ]; then
6522                 rbac="none"
6523         else
6524                 rbac="$srv_uc"
6525         fi
6526         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6527                 --value $rbac ||
6528                 error "setting rbac $rbac failed (2)"
6529         wait_nm_sync c0 rbac
6530
6531         set -vx
6532         $LFS setquota -u $USER0 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT &&
6533                 error "lfs setquota -u should fail"
6534         $LFS setquota -u $USER0 --delete $MOUNT
6535         $LFS setquota -g $USER0 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT &&
6536                 error "lfs setquota -g should fail"
6537         $LFS setquota -g $USER0 --delete $MOUNT
6538         $LFS setquota -p 1000 -b 307200 -B 309200 -i 10000 -I 11000 $MOUNT &&
6539                 error "lfs setquota -p should fail"
6540         $LFS setquota -p 1000 --delete $MOUNT
6541
6542         $LFS setquota -U -b 10G -B 11G -i 100K -I 105K $MOUNT &&
6543                 error "lfs setquota -U should fail"
6544         $LFS setquota -G -b 10G -B 11G -i 100K -I 105K $MOUNT &&
6545                 error "lfs setquota -G should fail"
6546         $LFS setquota -P -b 10G -B 11G -i 100K -I 105K $MOUNT &&
6547                 error "lfs setquota -P should fail"
6548         $LFS setquota -u $USER0 -D $MOUNT &&
6549                 error "lfs setquota -u -D should fail"
6550         $LFS setquota -u $USER0 --delete $MOUNT
6551         $LFS setquota -g $USER0 -D $MOUNT &&
6552                 error "lfs setquota -g -D should fail"
6553         $LFS setquota -g $USER0 --delete $MOUNT
6554         $LFS setquota -p 1000 -D $MOUNT &&
6555                 error "lfs setquota -p -D should fail"
6556         $LFS setquota -p 1000 --delete $MOUNT
6557         set +vx
6558 }
6559 run_test 64c "Nodemap enforces quota_ops RBAC roles"
6560
6561 test_64d() {
6562         local testfile=$DIR/$tdir/$tfile
6563         local srv_uc=""
6564         local rbac
6565         local fid
6566
6567         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6568                 skip "Need MDS >= 2.15.54 for role-based controls"
6569
6570         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6571                 srv_uc="server_upcall"
6572
6573         stack_trap cleanup_local_client_nodemap EXIT
6574         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6575         setup_local_client_nodemap "c0" 1 1
6576
6577         rbac="byfid_ops"
6578         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6579         do_facet mgs $LCTL nodemap_modify --name c0 \
6580                  --property rbac --value $rbac ||
6581                 error "setting rbac $rbac failed (1)"
6582         wait_nm_sync c0 rbac
6583
6584         touch $testfile
6585         fid=$(lfs path2fid $testfile)
6586         set -vx
6587         $LFS fid2path $MOUNT $fid || error "fid2path $fid failed (1)"
6588         cat $MOUNT/.lustre/fid/$fid || error "cat by fid failed"
6589         lfs rmfid $MOUNT $fid || error "lfs rmfid failed"
6590         set +vx
6591
6592         rbac="none"
6593         if [ -z "$srv_uc" ]; then
6594                 rbac="none"
6595         else
6596                 rbac="$srv_uc"
6597         fi
6598         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6599                 --value $rbac ||
6600                 error "setting rbac $rbac failed (2)"
6601         wait_nm_sync c0 rbac
6602
6603         touch $testfile
6604         fid=$(lfs path2fid $testfile)
6605         set -vx
6606         $LFS fid2path $MOUNT $fid || error "fid2path $fid failed (2)"
6607         cat $MOUNT/.lustre/fid/$fid && error "cat by fid should fail"
6608         lfs rmfid $MOUNT $fid && error "lfs rmfid should fail"
6609         set +vx
6610         rm -f $testfile
6611 }
6612 run_test 64d "Nodemap enforces byfid_ops RBAC roles"
6613
6614 test_64e() {
6615         local testfile=$DIR/$tdir/$tfile
6616         local testdir=$DIR/$tdir/${tfile}.d
6617         local srv_uc=""
6618         local rbac
6619
6620         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6621                 skip "Need MDS >= 2.15.54 for role-based controls"
6622
6623         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6624                 srv_uc="server_upcall"
6625
6626         stack_trap cleanup_local_client_nodemap EXIT
6627         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6628         setup_local_client_nodemap "c0" 1 1
6629
6630         # activate changelogs
6631         changelog_register || error "changelog_register failed"
6632         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
6633         changelog_users $SINGLEMDS | grep -q $cl_user ||
6634                 error "User $cl_user not found in changelog_users"
6635         changelog_chmask ALL
6636
6637         # do some IOs
6638         mkdir $testdir || error "failed to mkdir $testdir"
6639         touch $testfile || error "failed to touch $testfile"
6640
6641         rbac="chlg_ops"
6642         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6643         do_facet mgs $LCTL nodemap_modify --name c0 \
6644                  --property rbac --value $rbac ||
6645                 error "setting rbac $rbac failed (1)"
6646         wait_nm_sync c0 rbac
6647
6648         # access changelogs
6649         echo "changelogs dump"
6650         changelog_dump || error "failed to dump changelogs"
6651         echo "changelogs clear"
6652         changelog_clear 0 || error "failed to clear changelogs"
6653
6654         rm -rf $testdir $testfile || error "rm -rf $testdir $testfile failed"
6655
6656         rbac="none"
6657         if [ -z "$srv_uc" ]; then
6658                 rbac="none"
6659         else
6660                 rbac="$srv_uc"
6661         fi
6662         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6663                 --value $rbac ||
6664                 error "setting rbac $rbac failed (2)"
6665         wait_nm_sync c0 rbac
6666
6667         # do some IOs
6668         mkdir $testdir || error "failed to mkdir $testdir"
6669         touch $testfile || error "failed to touch $testfile"
6670
6671         # access changelogs
6672         echo "changelogs dump"
6673         changelog_dump && error "dump changelogs should fail"
6674         echo "changelogs clear"
6675         changelog_clear 0 && error "clear changelogs should fail"
6676         rm -rf $testdir $testfile
6677
6678         do_facet mgs $LCTL nodemap_modify --name c0 \
6679                 --property rbac --value all ||
6680                 error "setting rbac all failed (3)"
6681         wait_nm_sync c0 rbac
6682 }
6683 run_test 64e "Nodemap enforces chlg_ops RBAC roles"
6684
6685 test_64f() {
6686         local vaultdir=$DIR/$tdir/vault
6687         local cli_enc
6688         local policy
6689         local protector
6690         local srv_uc=""
6691         local rbac
6692
6693         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6694                 skip "Need MDS >= 2.15.54 for role-based controls"
6695
6696         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6697                 srv_uc="server_upcall"
6698
6699         cli_enc=$($LCTL get_param mdc.*.import | grep client_encryption)
6700         [ -n "$cli_enc" ] || skip "Need enc support, skip fscrypt_admin role"
6701         which fscrypt || skip "Need fscrypt, skip fscrypt_admin role"
6702
6703         stack_trap cleanup_local_client_nodemap EXIT
6704         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6705         echo "setup local client nodmap c0"
6706         setup_local_client_nodemap "c0" 1 1
6707
6708         yes | fscrypt setup --force --verbose ||
6709                 echo "fscrypt global setup already done"
6710         sed -i 's/\(.*\)policy_version\(.*\):\(.*\)\"[0-9]*\"\(.*\)/\1policy_version\2:\3"2"\4/' \
6711                 /etc/fscrypt.conf
6712         yes | fscrypt setup --verbose $MOUNT ||
6713                 echo "fscrypt setup $MOUNT already done"
6714         echo "fscrypt for mount $MOUNT is ready for use"
6715         stack_trap "rm -rf $MOUNT/.fscrypt"
6716
6717         # file_perms is required because fscrypt uses chmod/chown
6718         rbac="fscrypt_admin,file_perms"
6719         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6720         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6721                 --value $rbac ||
6722                 error "setting rbac $rbac failed (1)"
6723         echo "waiting for nodemap file_perms and fscrypt to be modified"
6724         wait_nm_sync c0 rbac
6725
6726         mkdir -p $vaultdir
6727         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
6728              --source=custom_passphrase --name=protector_64 $vaultdir ||
6729                 error "fscrypt encrypt $vaultdir failed"
6730         fscrypt lock $vaultdir || error "fscrypt lock $vaultdir failed (1)"
6731         echo "$vaultdir is locked away with encryption"
6732         policy=$(fscrypt status $vaultdir | awk '$1 == "Policy:"{print $2}')
6733         [ -n "$policy" ] || error "could not get enc policy"
6734         echo "fscrypt policy $policy is ready"
6735         protector=$(fscrypt status $vaultdir |
6736                   awk 'BEGIN {found=0} { if (found == 1) { print $1 }} \
6737                         $1 == "PROTECTOR" {found=1}')
6738         [ -n "$protector" ] || error "could not get enc protector"
6739         set +vx
6740
6741         cancel_lru_locks
6742         # file_perms is required because fscrypt uses chmod/chown
6743         rbac="file_perms"
6744         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6745         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6746                 --value $rbac ||
6747                 error "setting rbac $rbac failed (2)"
6748         wait_nm_sync c0 rbac
6749
6750         set -vx
6751         echo mypass | fscrypt unlock $vaultdir ||
6752                 error "fscrypt unlock $vaultdir failed"
6753         fscrypt lock $vaultdir || error "fscrypt lock $vaultdir failed (2)"
6754         fscrypt metadata destroy --protector=$MOUNT:$protector --force &&
6755                 error "destroy protector should fail"
6756         fscrypt metadata destroy --policy=$MOUNT:$policy --force &&
6757                 error "destroy policy should fail"
6758         mkdir -p ${vaultdir}2
6759         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
6760                 --source=custom_passphrase \
6761                 --name=protector_64bis ${vaultdir}2 &&
6762                         error "fscrypt encrypt ${vaultdir}2 should fail"
6763         set +vx
6764
6765         cancel_lru_locks
6766         do_facet mgs $LCTL nodemap_modify --name c0 \
6767                 --property rbac  --value all ||
6768                 error "setting rbac all failed (3)"
6769         wait_nm_sync c0 rbac
6770
6771         set -vx
6772         fscrypt metadata destroy --protector=$MOUNT:$protector --force ||
6773                 error "destroy protector failed"
6774         fscrypt metadata destroy --policy=$MOUNT:$policy --force ||
6775                 error "destroy policy failed"
6776         set +vx
6777
6778         rm -rf ${vaultdir}*
6779 }
6780 run_test 64f "Nodemap enforces fscrypt_admin RBAC roles"
6781
6782 test_64g() {
6783         local testfile=$DIR/$tdir/$tfile
6784
6785         (( MDS1_VERSION >= $(version_code 2.16.50) )) ||
6786                 skip "Need MDS >= 2.16.50 for role-based controls"
6787
6788         # Add groups, and client to new group, on client only.
6789         # Server is not aware.
6790         groupadd -g 5000 grptest64g1
6791         stack_trap "groupdel grptest64g1" EXIT
6792         groupadd -g 5001 grptest64g2
6793         stack_trap "groupdel grptest64g2" EXIT
6794         groupadd -g 5002 grptest64g3
6795         stack_trap "groupdel grptest64g3" EXIT
6796
6797         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6798         chmod 750 $DIR/$tdir
6799         chgrp grptest64g1 $DIR/$tdir
6800         echo hi > $DIR/$tdir/fileA
6801         chmod 640 $DIR/$tdir/fileA
6802         chgrp grptest64g3 $DIR/$tdir/fileA
6803         setfacl -m g:grptest64g2:r $DIR/$tdir/fileA
6804         setfacl -m g:grptest64g2:rwx $DIR/$tdir
6805         ls -lR $DIR/$tdir
6806
6807         setup_local_client_nodemap "c0" 1 1
6808         stack_trap cleanup_local_client_nodemap EXIT
6809
6810         # remove server_upcall from rbac roles,
6811         # to make this client use INTERNAL upcall
6812         do_facet mgs $LCTL nodemap_modify --name c0 \
6813                  --property rbac --value file_perms ||
6814                 error "setting rbac file_perms failed"
6815         wait_nm_sync c0 rbac
6816
6817         $RUNAS touch $DIR/$tdir/fileB &&
6818                 error "touch $DIR/$tdir/fileB should fail"
6819         do_nodes $(comma_list $(all_mdts_nodes)) \
6820                 $LCTL set_param mdt.*.identity_int_flush=$RUNAS_ID
6821         $RUNAS -G 5001 touch $DIR/$tdir/fileB ||
6822                 error "touch $DIR/$tdir/fileB failed"
6823         do_nodes $(comma_list $(all_mdts_nodes)) \
6824                 $LCTL set_param mdt.*.identity_int_flush=$RUNAS_ID
6825         $RUNAS -G 5000,5001 touch $DIR/$tdir/fileC ||
6826                 error "touch $DIR/$tdir/fileC failed"
6827         do_nodes $(comma_list $(all_mdts_nodes)) \
6828                 $LCTL set_param mdt.*.identity_int_flush=$RUNAS_ID
6829         $RUNAS cat $DIR/$tdir/fileA && error "cat $DIR/$tdir/fileA should fail"
6830         do_nodes $(comma_list $(all_mdts_nodes)) \
6831                 $LCTL set_param mdt.*.identity_int_flush=$RUNAS_ID
6832         $RUNAS -G 5000,5001 cat $DIR/$tdir/fileA ||
6833                 error "cat $DIR/$tdir/fileA failed"
6834 }
6835 run_test 64g "Nodemap enforces server_upcall RBAC role"
6836
6837 test_64h() {
6838         local testfile=$DIR/$tdir/$tfile
6839         local offset_start=100000
6840         local offset_limit=200000
6841         local projid=1001
6842         local srv_uc=""
6843         local rbac
6844         local fid
6845
6846         (( MDS1_VERSION >= $(version_code 2.15.54) )) ||
6847                 skip "Need MDS >= 2.15.54 for role-based controls"
6848
6849         (( MDS1_VERSION >= $(version_code 2.16.50) )) &&
6850                 srv_uc="server_upcall"
6851
6852         do_nodes $(comma_list $(all_mdts_nodes)) \
6853                 $LCTL set_param mdt.*.identity_upcall=NONE
6854
6855         stack_trap \
6856             "$LFS setquota -p $((projid+offset_start)) --delete $DIR/$tdir" EXIT
6857         stack_trap cleanup_local_client_nodemap EXIT
6858         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
6859         chmod 777 $DIR/$tdir
6860         $LFS project -p $((projid+offset_start)) -s $DIR/$tdir
6861         $LFS setquota -p $((projid+offset_start)) -b 1G -B 1G $DIR/$tdir
6862         $LFS project -d $DIR/$tdir
6863         $LFS quota -aph $DIR/$tdir
6864         setup_local_client_nodemap "c0" 1 1
6865
6866         # skip test if server does not support local_admin rbac role
6867         rbac=$(do_facet mds $LCTL get_param -n nodemap.c0.rbac)
6868         [[ "$rbac" =~ "local_admin" ]] ||
6869                 skip "server does not support 'local_admin' rbac role"
6870
6871         # Let's offset ids. Even root is offset.
6872         do_facet mgs $LCTL nodemap_add_offset --name c0 \
6873                 --offset $offset_start --limit $offset_limit ||
6874                         error "cannot set offset for c0"
6875
6876         rbac="file_perms,quota_ops"
6877         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6878         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6879                 --value $rbac ||
6880                 error "setting rbac $rbac failed (1)"
6881         wait_nm_sync c0 rbac
6882
6883         $RUNAS touch $testfile
6884
6885         # Without local_admin, root capabilities are dropped
6886         chmod o+x $testfile && error "root chmod should fail (1)"
6887         # and setquota/lfs project is not permitted
6888         $LFS setquota -p $projid -b 4G -B 4G $DIR/$tdir &&
6889                 error "setquota should fail (1)"
6890         $LFS project -p $((projid+1)) -s $DIR/$tdir &&
6891                 error "setting projid should fail (1)"
6892
6893         rbac="file_perms,quota_ops,local_admin"
6894         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6895         do_facet mgs $LCTL nodemap_modify --name c0 \
6896                  --property rbac --value $rbac ||
6897                 error "setting rbac $rbac failed (2)"
6898         wait_nm_sync c0 rbac
6899         # squash root by setting admin=0
6900         do_facet mgs $LCTL nodemap_modify --name c0 \
6901                  --property admin --value 0
6902         wait_nm_sync c0 admin_nodemap
6903
6904         # Even with local_admin, capabilities are dropped if root is squashed
6905         chmod o+x $testfile && error "root chmod should fail (2)"
6906         # and setquota/lfs project is not permitted
6907         $LFS setquota -p $projid -b 4G -B 4G $DIR/$tdir &&
6908                 error "setquota should fail (2)"
6909         $LFS project -p $((projid+1)) -s $DIR/$tdir &&
6910                 error "setting projid should fail (2)"
6911
6912         do_facet mgs $LCTL nodemap_modify --name c0 \
6913                  --property admin --value 1
6914         wait_nm_sync c0 admin_nodemap
6915
6916         #  with local_admin and admin=1, capabilities are kept
6917         chmod o+x $testfile || error "root chmod failed (1)"
6918         # and setquota/lfs project is permitted
6919         $LFS setquota -p $projid -b 4G -B 4G $DIR/$tdir ||
6920                 error "setquota failed (1)"
6921         $LFS project -p $((projid+1)) -s $DIR/$tdir ||
6922                 error "setting projid failed (1)"
6923
6924         # remove offset and local_admin but keep admin, so that root
6925         # on client is root on file system side
6926         do_facet mgs $LCTL nodemap_del_offset --name c0 ||
6927                 error "cannot del offset for c0"
6928         rbac="file_perms,quota_ops"
6929         [ -z "$srv_uc" ] || rbac="$rbac,$srv_uc"
6930         do_facet mgs $LCTL nodemap_modify --name c0 --property rbac \
6931                 --value $rbac ||
6932                 error "setting rbac $rbac failed (3)"
6933         wait_nm_sync c0 rbac
6934
6935         #  as root, capabilities are kept even without local_admin
6936         chmod g+x $testfile || error "root chmod failed (2)"
6937         # and setquota/lfs project is permitted
6938         $LFS setquota -p $((projid+offset_start)) -b 3G -B 3G $DIR/$tdir ||
6939                 error "setquota failed (2)"
6940         $LFS project -p $((projid+offset_start)) -s $DIR/$tdir ||
6941                 error "setting projid failed (2)"
6942 }
6943 run_test 64h "Nodemap enforces local_admin RBAC roles"
6944
6945 look_for_files() {
6946         local pattern=$1
6947         local neg=$2
6948         local path=$3
6949         local expected=$4
6950         local res
6951
6952         (( neg == 1 )) || neg=""
6953         $LFS find -type f ${neg:+"!"} --attrs $pattern $path > $TMP/res
6954         cat $TMP/res
6955         res=$(cat $TMP/res | wc -l)
6956         (( res == $expected )) ||
6957                 error "Find $pattern $path: found $res, expected $expected"
6958 }
6959
6960 test_65() {
6961         local dirbis=$DIR/${tdir}_bis
6962         local testfile=$DIR/$tdir/$tfile
6963         local res
6964
6965         $LCTL get_param mdc.*.import | grep -q client_encryption ||
6966                 skip "client encryption not supported"
6967
6968         mount.lustre --help |& grep -q "test_dummy_encryption:" ||
6969                 skip "need dummy encryption support"
6970
6971         # $dirbis is not going to be encrypted, as client
6972         # is not mounted with -o test_dummy_encryption yet
6973         mkdir $dirbis
6974         stack_trap "rm -rf $dirbis" EXIT
6975         touch $dirbis/$tfile.1
6976         touch $dirbis/$tfile.2
6977         chattr +i $dirbis/$tfile.2
6978         stack_trap "chattr -i $dirbis/$tfile.2" EXIT
6979
6980         stack_trap cleanup_for_enc_tests EXIT
6981         setup_for_enc_tests
6982
6983         # All files/dirs under $DIR/$tdir are encrypted
6984         touch $testfile.1
6985         touch $testfile.2
6986         chattr +i $testfile.2
6987         stack_trap "chattr -i $testfile.2" EXIT
6988
6989         $LFS find -printf "%p %LA\n" $dirbis/$tfile.1
6990         res=$($LFS find -printf "%LA" $dirbis/$tfile.1)
6991         [ "$res" == "---" ] ||
6992                 error "$dirbis/$tfile.1 should have no attr, showed $res (1)"
6993         $LFS find -printf "%p %La\n" $dirbis/$tfile.1
6994         res=$($LFS find -printf "%La" $dirbis/$tfile.1)
6995         [ "$res" == "---" ] ||
6996                 error "$dirbis/$tfile.1 should have no attr, showed $res (2)"
6997         $LFS find -printf "%p %LA\n" $dirbis/$tfile.2
6998         res=$($LFS find -printf "%LA" $dirbis/$tfile.2)
6999         [ "$res" == "Immutable" ] ||
7000                 error "$dirbis/$tfile.2 should be Immutable, showed $res"
7001         $LFS find -printf "%p %La\n" $dirbis/$tfile.2
7002         res=$($LFS find -printf "%La" $dirbis/$tfile.2)
7003         [ "$res" == "i" ] ||
7004                 error "$dirbis/$tfile.2 should be 'i', showed $res"
7005         $LFS find -printf "%p %LA\n" $testfile.1
7006         res=$($LFS find -printf "%LA" $testfile.1)
7007         [ "$res" == "Encrypted" ] ||
7008                 error "$testfile.1 should be Encrypted, showed $res"
7009         $LFS find -printf "%p %La\n" $testfile.1
7010         res=$($LFS find -printf "%La" $testfile.1)
7011         [ "$res" == "E" ] ||
7012                 error "$testfile.1 should be 'E', showed $res"
7013         $LFS find -printf "%p %LA\n" $testfile.2
7014         res=$($LFS find -printf "%LA" $testfile.2)
7015         [ "$res" == "Immutable,Encrypted" ] ||
7016                 error "$testfile.2 should be Immutable,Encrypted, showed $res"
7017         $LFS find -printf "%p %La\n" $testfile.2
7018         res=$($LFS find -printf "%La" $testfile.2)
7019         [ "$res" == "iE" ] ||
7020                 error "$testfile.2 should be 'iE', showed $res"
7021
7022         echo Expecting to find 2 encrypted files
7023         look_for_files Encrypted 0 "$DIR/${tdir}*" 2
7024         echo Expecting to find 2 encrypted files
7025         look_for_files E 0 "$DIR/${tdir}*" 2
7026
7027         echo Expecting to find 2 non-encrypted files
7028         look_for_files Encrypted 1 "$DIR/${tdir}*" 2
7029         echo Expecting to find 2 non-encrypted files
7030         look_for_files E 1 "$DIR/${tdir}*" 2
7031
7032         echo Expecting to find 1 encrypted+immutable file
7033         look_for_files "Encrypted,Immutable" 0 "$DIR/${tdir}*" 1
7034         echo Expecting to find 1 encrypted+immutable file
7035         look_for_files "Ei" 0 "$DIR/${tdir}*" 1
7036
7037         echo Expecting to find 1 encrypted+^immutable file
7038         look_for_files "Encrypted,^Immutable" 0 "$DIR/${tdir}*" 1
7039         echo Expecting to find 1 encrypted+^immutable file
7040         look_for_files "E^i" 0 "$DIR/${tdir}*" 1
7041
7042         echo Expecting to find 1 ^encrypted+immutable file
7043         look_for_files "^Encrypted,Immutable" 0 "$DIR/${tdir}*" 1
7044         echo Expecting to find 1 ^encrypted+immutable file
7045         look_for_files "^Ei" 0 "$DIR/${tdir}*" 1
7046
7047         echo Expecting to find 1 ^encrypted+^immutable file
7048         look_for_files "^Encrypted,^Immutable" 0 "$DIR/${tdir}*" 1
7049         echo Expecting to find 1 ^encrypted+^immutable file
7050         look_for_files "^E^i" 0 "$DIR/${tdir}*" 1
7051 }
7052 run_test 65 "lfs find -printf %La and --attrs support"
7053
7054 cleanup_68() {
7055         lctl set_param fail_loc=0 fail_val=0
7056         mount_client $MOUNT ${MOUNT_OPTS} || error "re-mount $MOUNT failed"
7057         if is_mounted $MOUNT2; then
7058                 mount_client $MOUNT2 ${MOUNT_OPTS} ||
7059                         error "re-mount $MOUNT2 failed"
7060         fi
7061 }
7062
7063 test_68() {
7064         stack_trap cleanup_68 EXIT
7065
7066         # unmount client completely
7067         umount_client $MOUNT || error "umount $MOUNT failed"
7068         if is_mounted $MOUNT2; then
7069                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
7070         fi
7071
7072         #define CFS_FAIL_ONCE|OBD_FAIL_PTLRPC_DROP_MGS    0x51d
7073         lctl set_param fail_loc=0x8000051d fail_val=20
7074
7075         zconf_mount_clients $HOSTNAME $MOUNT $MOUNT_OPTS ||
7076                 error "mount failed"
7077
7078         umount_client $MOUNT || error "re-umount $MOUNT failed"
7079 }
7080 run_test 68 "all config logs are processed"
7081
7082 test_69() {
7083         local mdt="$(mdtname_from_index 0 $MOUNT)"
7084         local param
7085         local orig
7086
7087         (( MDS1_VERSION >= $(version_code v2_15_61-210-g2153e86541) )) ||
7088                 skip "need MDS >= 2.15.61.210 for upcall sanity checking"
7089
7090         param="mdt.$mdt.identity_upcall"
7091         orig="$(do_facet mds1 "$LCTL get_param -n $param")"
7092         stack_trap "do_facet mds1 $LCTL set_param $param=$orig" EXIT
7093
7094         # identity_upcall accepts an absolute path to an executable,
7095         # or NONE (case insensitive)
7096         do_facet mds1 $LCTL set_param $param=/path/to/prog ||
7097                 error "set_param $param=/path/to/prog failed (1)"
7098         do_facet mds1 $LCTL set_param $param=prog &&
7099                 error "set_param $param=prog should fail (1)"
7100         do_facet mds1 $LCTL set_param $param=NONE ||
7101                 error "set_param $param=NONE failed (1)"
7102         do_facet mds1 $LCTL set_param $param=none ||
7103                 error "set_param $param=none failed (1)"
7104
7105         if $GSS; then
7106                 param="sptlrpc.gss.rsi_upcall"
7107                 orig="$(do_facet mds1 "$LCTL get_param -n $param")"
7108                 stack_trap "do_facet mds1 $LCTL set_param $param=$orig" EXIT
7109
7110                 # rsi_upcall only accepts an absolute path to an executable
7111                 do_facet mds1 $LCTL set_param $param=prog &&
7112                         error "set_param $param=prog should fail (2)"
7113                 do_facet mds1 $LCTL set_param $param=NONE &&
7114                         error "set_param $param=NONE should fail (2)"
7115                 do_facet mds1 $LCTL set_param $param=/path/to/prog ||
7116                         error "set_param $param=/path/to/prog failed (2)"
7117         fi
7118 }
7119 run_test 69 "check upcall incorrect values"
7120
7121 test_70() {
7122         local param_mgs=$(mktemp $TMP/$tfile-mgs.XXXXXX)
7123         local param_copy=$(mktemp $TMP/$tfile-copy.XXXXXX)
7124
7125         stack_trap "rm -f $param_mgs $param_copy" EXIT
7126
7127         (( $MDS1_VERSION > $(version_code 2.15.61) )) ||
7128                 skip "Need MDS version at least 2.15.61"
7129
7130         if ! $SHARED_KEY; then
7131                 skip "need shared key feature for this test"
7132         fi
7133
7134         [[ "$ost1_FSTYPE" == ldiskfs ]] ||
7135                 skip "ldiskfs only test (using debugfs)"
7136
7137         # unmount then remount the Lustre filesystem, to make sure llogs
7138         # are copied locally
7139         export SK_NO_KEY=false
7140         stopall || error "stopall failed"
7141         init_gss
7142         mountmgs || error "mountmgs failed"
7143         mountmds || error "mountmds failed"
7144         mountoss || error "mountoss failed"
7145         mountcli || error "mountcli failed"
7146         lfs df -h
7147         unset SK_NO_KEY
7148
7149         do_facet mgs "sync ; sync"
7150         do_facet mgs "$DEBUGFS -c -R 'ls CONFIGS/' $(mgsdevname)"
7151         do_facet mgs "$DEBUGFS -c -R 'dump CONFIGS/$FSNAME-sptlrpc $param_mgs' \
7152                 $(mgsdevname)"
7153         do_facet mgs "llog_reader $param_mgs" | grep -vE "SKIP|marker" |
7154                 grep "^#" > $param_mgs
7155         cat $param_mgs
7156
7157         if ! combined_mgs_mds; then
7158                 do_facet mds1 "sync ; sync"
7159                 do_facet mds1 "$DEBUGFS -c -R 'ls CONFIGS/' $(mdsdevname 1)"
7160                 do_facet mds1 "$DEBUGFS -c -R 'dump CONFIGS/$FSNAME-sptlrpc \
7161                         $param_copy' $(mdsdevname 1)"
7162                 do_facet mds1 "llog_reader $param_copy" |
7163                         grep -vE "SKIP|marker" | grep "^#" > $param_copy
7164                 cat $param_copy
7165                 cmp -bl $param_mgs $param_copy ||
7166                         error "sptlrpc llog differ in mds"
7167                 rm -f $param_copy
7168         fi
7169
7170         do_facet ost1 "sync ; sync"
7171         do_facet ost1 "$DEBUGFS -c -R 'ls CONFIGS/' $(ostdevname 1)"
7172         do_facet ost1 "$DEBUGFS -c -R 'dump CONFIGS/$FSNAME-sptlrpc \
7173                 $param_copy' $(ostdevname 1)"
7174         do_facet ost1 "llog_reader $param_copy" | grep -vE "SKIP|marker" |
7175                 grep "^#" > $param_copy
7176         cat -A $param_copy
7177         cmp -bl $param_mgs $param_copy ||
7178                 error "sptlrpc llog differ at ost1"
7179         rm -f $param_copy
7180
7181         do_facet ost2 "sync ; sync"
7182         do_facet ost2 "$DEBUGFS -c -R 'ls CONFIGS/' $(ostdevname 2)"
7183         do_facet ost2 "$DEBUGFS -c -R 'dump CONFIGS/$FSNAME-sptlrpc \
7184                 $param_copy' $(ostdevname 2)"
7185         do_facet ost2 "llog_reader $param_copy" | grep -vE "SKIP|marker" |
7186                 grep "^#" > $param_copy
7187         cat -A $param_copy
7188         cmp -bl $param_mgs $param_copy ||
7189                 error "sptlrpc llog differ at ost2"
7190 }
7191 run_test 70 "targets have local copy of sptlrpc llog"
7192
7193 test_71() {
7194         local vaultdir=$DIR/$tdir/vault
7195         local projid=101
7196         local res
7197
7198         (( $MDS1_VERSION >= $(version_code 2.15.63) )) ||
7199                 skip "Need MDS version at least 2.15.63"
7200
7201         [[ $($LCTL get_param mdc.*.import) =~ client_encryption ]] ||
7202                 skip "need encryption support"
7203         which fscrypt || skip_env "Need fscrypt"
7204
7205         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
7206
7207         yes | fscrypt setup --force --verbose ||
7208                 echo "fscrypt global setup already done"
7209         sed -i 's/\(.*\)policy_version\(.*\):\(.*\)\"[0-9]*\"\(.*\)/\1policy_version\2:\3"2"\4/' \
7210                 /etc/fscrypt.conf
7211         yes | fscrypt setup --verbose $MOUNT ||
7212                 echo "fscrypt setup $MOUNT already done"
7213         stack_trap "rm -rf $MOUNT/.fscrypt"
7214
7215         mkdir -p $vaultdir
7216         stack_trap "rm -rf $vaultdir"
7217         $LFS project -p $projid -s $vaultdir
7218         $LFS project -d $vaultdir
7219         res=$($LFS project -d $vaultdir | awk '{print $1}')
7220         [[ "$res" == "$projid" ]] ||
7221                 error "project id set to $res instead of $projid"
7222         res=$($LFS project -d $vaultdir | awk '{print $2}')
7223         [[ "$res" == "P" ]] ||
7224                 error "project id should have inherit flag (1)"
7225
7226         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
7227              --source=custom_passphrase --name=protector_71 $vaultdir ||
7228                 error "fscrypt encrypt $vaultdir failed"
7229
7230         $LFS project -d $vaultdir
7231         res=$($LFS project -d $vaultdir | awk '{print $1}')
7232         [[ "$res" == "$projid" ]] ||
7233                 error "project id changed to $res after enc"
7234         res=$($LFS project -d $vaultdir | awk '{print $2}')
7235         [[ "$res" == "P" ]] ||
7236                 error "project id should have inherit flag (2)"
7237
7238         touch $vaultdir/fileA || error "touch $vaultdir/fileA failed"
7239         $LFS project $vaultdir/fileA
7240         res=$($LFS project $vaultdir/fileA | awk '{print $1}')
7241         [[ "$res" == "$projid" ]] ||
7242                 error "project id on fileA is $res after enc"
7243
7244         mkdir $vaultdir/dirA || error "touch $vaultdir/dirA failed"
7245         $LFS project -d $vaultdir/dirA
7246         res=$($LFS project -d $vaultdir/dirA | awk '{print $1}')
7247         [[ "$res" == "$projid" ]] ||
7248                 error "project id on dirA is $res after enc"
7249         res=$($LFS project -d $vaultdir/dirA | awk '{print $2}')
7250         [[ "$res" == "P" ]] ||
7251                 error "project id should have inherit flag (3)"
7252 }
7253 run_test 71 "encryption does not remove project flag"
7254
7255 dyn_nm_helper() {
7256         local facet=$1
7257         local mgsnm=mgsnm
7258         local mgsnids=1.1.0.[1-100]@tcp
7259         local mgsnids2=1.0.0.[1-100]@tcp
7260         local mgsclid=600
7261         local mgsfsid=2000
7262         local nm=nm_test72
7263         local nids=1.1.1.[1-100]@tcp
7264         local startnid=1.1.1.1@tcp
7265         local endnid=1.1.1.100@tcp
7266         local subnids1=1.1.1.[2-50]@tcp
7267         local subnids2=1.1.1.[51-100]@tcp
7268         local subnids3=1.1.1.[2-25]@tcp
7269         local subnids4=1.1.1.[51-52]@tcp
7270         local subnids5=1.1.1.[26-60]@tcp
7271         local subnids6=1.1.1.[1-60]@tcp
7272         local clid=500
7273         local fsid=1000
7274         local properties="audit_mode deny_unknown forbid_encryption \
7275                           readonly_mount"
7276         local sepol="1:mls:31:40afb76d077c441b69af58cccaaa2ca63641ed6e21b0a887dc21a684f508b78f"
7277         local rbac_val
7278         local raise
7279         local val
7280
7281         activedefault=$(do_facet mgs $LCTL get_param -n nodemap.active)
7282         if [[ "$activedefault" != "1" ]]; then
7283                 do_facet mgs $LCTL nodemap_activate 1
7284                 wait_nm_sync active
7285                 stack_trap cleanup_active EXIT
7286         fi
7287
7288         do_facet mgs $LCTL nodemap_set_fileset --name default \
7289                 --fileset "/deffset" ||
7290                         error "setting fileset on default failed"
7291         raise=$(do_facet mgs $LCTL get_param -n \
7292                 nodemap.default.child_raise_privileges)
7293         if [[ -n "$raise" ]]; then
7294                 do_facet mgs $LCTL nodemap_modify --name default \
7295                     --property child_raise_privileges --value all ||
7296                        error "modify raise_privileges for default on MGS failed"
7297                 wait_nm_sync default child_raise_privileges
7298                 stack_trap "do_facet mgs $LCTL nodemap_modify --name default \
7299                         --property child_raise_privileges --value $raise" EXIT
7300         fi
7301
7302         do_facet mgs $LCTL nodemap_add $mgsnm ||
7303                 error "adding $mgsnm on MGS failed"
7304         stack_trap "do_facet mgs $LCTL nodemap_del $mgsnm" EXIT
7305         do_facet mgs $LCTL nodemap_add_range --name $mgsnm --range $mgsnids ||
7306                 error "add_range for $mgsnm on MGS failed"
7307         do_facet mgs $LCTL nodemap_add_idmap --name $mgsnm --idtype uid \
7308                 --idmap $mgsclid:$mgsfsid ||
7309                 error "add_idmap for $mgsnm on MGS failed"
7310         wait_nm_sync $mgsnm idmap
7311
7312         rbac_val=$(do_facet mgs $LCTL get_param -n nodemap.$mgsnm.rbac)
7313
7314         stack_trap "do_facet $facet $LCTL nodemap_del $nm || true" EXIT
7315         if [[ "$(facet_active_host mgs)" != \
7316                         "$(facet_active_host $facet)" ]]; then
7317                 do_facet $facet $LCTL nodemap_add $nm &&
7318                         error "static nodemap on server should fail"
7319         fi
7320         do_facet $facet $LCTL nodemap_add -d $nm &&
7321                 error "dynamic nodemap without parent should fail"
7322         do_facet $facet $LCTL nodemap_add -d -p default $nm ||
7323                 error "dynamic nodemap on server failed"
7324         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.id)
7325         if [[ -z "$val" || "$val" == "0" ]]; then
7326                 error "dynamic nodemap wrong id $val"
7327         fi
7328
7329         do_facet $facet $LCTL nodemap_add_range --name $nm --range $nids ||
7330                 error "dynamic add_range on server failed"
7331         val=$(do_facet $facet $LCTL get_param nodemap.$nm.ranges |
7332                 awk 'BEGIN{RS=", "} $1=="start_nid:"{print $2 ; exit}')
7333         [[ "$val" == "$startnid" ]] ||
7334                 error "dynamic nodemap wrong start nid range $val"
7335         val=$(do_facet $facet $LCTL get_param nodemap.$nm.ranges |
7336                 awk 'BEGIN{RS=", "} $1=="end_nid:"{print $2 ; exit}')
7337         [[ "$val" == "$endnid" ]] ||
7338                 error "dynamic nodemap wrong end nid range $val"
7339
7340         do_facet $facet $LCTL nodemap_add_idmap --name $nm --idtype uid \
7341                 --idmap $clid:$fsid ||
7342                         error "dynamic add_idmap on server failed"
7343         val=$(do_facet $facet $LCTL get_param nodemap.$nm.idmap |
7344                 awk 'BEGIN{RS=", "} $1=="client_id:"{print $2 ; exit}')
7345         (( val == clid )) || error "dynamic nodemap wrong client id $val"
7346         val=$(do_facet $facet $LCTL get_param nodemap.$nm.idmap |
7347                 awk 'BEGIN{RS=", "} $1=="fs_id:"{print $2 ; exit}')
7348         (( val == fsid )) || error "dynamic nodemap wrong fs id $val"
7349
7350         for prop in $properties; do
7351                 do_facet $facet $LCTL nodemap_modify --name $nm \
7352                         --property $prop --value 1 ||
7353                                 error "dynamic modify of $prop failed"
7354                 val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7355                 (( val == 1 )) || error "incorrect $prop $val"
7356         done
7357         prop=admin
7358         do_facet $facet $LCTL nodemap_modify --name $nm \
7359                 --property $prop --value 1 ||
7360                         error "dynamic modify of $prop failed"
7361         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.admin_nodemap)
7362         (( val == 1 )) || error "incorrect $prop $val"
7363         prop=trusted
7364         do_facet $facet $LCTL nodemap_modify --name $nm \
7365                 --property $prop --value 0 ||
7366                         error "dynamic modify of $prop failed"
7367         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.trusted_nodemap)
7368         (( val == 0 )) || error "incorrect $prop $val"
7369         prop=map_mode
7370         do_facet $facet $LCTL nodemap_modify --name $nm \
7371                 --property $prop --value uid ||
7372                         error "dynamic modify of $prop failed"
7373         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7374         [[ "$val" == "uid" ]] || error "incorrect $prop $val"
7375         prop=rbac
7376         do_facet $facet $LCTL nodemap_modify --name $nm \
7377                 --property $prop --value file_perms ||
7378                         error "dynamic modify of $prop failed"
7379         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7380         [[ "$val" == "file_perms" ]] || error "incorrect $prop $val"
7381         do_facet $facet $LCTL nodemap_modify --name $nm \
7382                 --property $prop --value all ||
7383                         error "dynamic modify of $prop failed"
7384         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7385         [[ "$val" == "$rbac_val" ]] || error "incorrect $prop $val"
7386         prop=squash_uid
7387         do_facet $facet $LCTL nodemap_modify --name $nm \
7388                 --property $prop --value 77 ||
7389                         error "dynamic modify of $prop failed"
7390         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7391         (( val == 77 )) || error "incorrect $prop $val"
7392         prop=squash_gid
7393         do_facet $facet $LCTL nodemap_modify --name $nm \
7394                 --property $prop --value 77 ||
7395                         error "dynamic modify of $prop failed"
7396         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7397         (( val == 77 )) || error "incorrect $prop $val"
7398         prop=squash_projid
7399         do_facet $facet $LCTL nodemap_modify --name $nm \
7400                 --property $prop --value 77 ||
7401                         error "dynamic modify of $prop failed"
7402         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7403         (( val == 77 )) || error "incorrect $prop $val"
7404         prop=fileset
7405         do_facet $facet $LCTL nodemap_set_fileset --name $nm \
7406                 --fileset "/tmp" &&
7407                         error "dynamic modify of $prop should fail"
7408         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7409         [[ "$val" == "/deffset" ]] || error "incorrect $prop $val"
7410         prop=sepol
7411         do_facet $facet $LCTL nodemap_set_sepol --name $nm \
7412                 --sepol $sepol ||
7413                         error "dynamic modify of $prop failed"
7414         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop)
7415         [[ "$val" == "$sepol" ]] || error "incorrect $prop $val"
7416
7417         prop=offset
7418         do_facet $facet $LCTL nodemap_add_offset --name $nm \
7419                 --offset 100000 --limit 200000 ||
7420                         error "dynamic modify of $prop failed"
7421         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop |
7422                 awk '$1 == "start_uid:" {print $2}' | sed s+,++)
7423         (( val == 100000 )) || error "incorrect $prop start_uid $val"
7424         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop |
7425                 awk '$1 == "limit_uid:" {print $2}' | sed s+,++)
7426         (( val == 200000 )) || error "incorrect $prop limit_uid $val"
7427         do_facet $facet $LCTL nodemap_del_offset --name $nm ||
7428                         error "dynamic del of $prop failed"
7429         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop |
7430                 awk '$1 == "start_uid:" {print $2}' | sed s+,++)
7431         (( val == 0 )) || error "incorrect $prop start_uid $val"
7432         val=$(do_facet $facet $LCTL get_param -n nodemap.$nm.$prop |
7433                 awk '$1 == "limit_uid:" {print $2}' | sed s+,++)
7434         (( val == 0 )) || error "incorrect $prop limit_uid $val"
7435
7436         val=$(do_facet $facet $LCTL nodemap_test_id --nid $startnid \
7437                 --idtype uid --id $clid)
7438         (( val == fsid )) || error "dynamic test_id on server failed"
7439
7440         do_facet $facet $LCTL nodemap_del_idmap --name $nm --idtype uid \
7441                 --idmap $clid:$fsid ||
7442                         error "dynamic del_idmap on server failed"
7443         val=$(do_facet $facet $LCTL get_param nodemap.$nm.idmap |
7444                 awk 'BEGIN{RS=", "} $1=="client_id:"{print $2 ; exit}')
7445         [[ -z "$val" ]] || error "idmap should be empty, got $val"
7446
7447         val=$(do_facet $facet $LCTL nodemap_test_nid $startnid)
7448         [[ "$val" == "$nm" ]] || error "dynamic test_nid on server failed"
7449
7450         do_facet $facet $LCTL nodemap_add -d -p $nm ${nm}_1 ||
7451                 error "nodemap add ${nm}_1 on server failed"
7452         stack_trap "do_facet $facet $LCTL nodemap_del ${nm}_1 || true" EXIT
7453         do_facet $facet $LCTL nodemap_add_range --name ${nm}_1 \
7454                 --range $subnids1 ||
7455                         error "add_range for ${nm}_1 failed"
7456         val=$(do_facet $facet $LCTL get_param -n nodemap.${nm}_1.parent)
7457         [[ "$val" == "$nm" ]] ||
7458                 error "parent of ${nm}_1 should be $nm, got $val"
7459
7460         do_facet $facet $LCTL nodemap_add -d -p $nm ${nm}_2 ||
7461                 error "nodemap add ${nm}_2 on server failed"
7462         stack_trap "do_facet $facet $LCTL nodemap_del ${nm}_2 || true" EXIT
7463         do_facet $facet $LCTL nodemap_add_range --name ${nm}_2 \
7464                 --range $subnids2 ||
7465                         error "add_range for ${nm}_2 failed"
7466         val=$(do_facet $facet $LCTL get_param -n nodemap.${nm}_2.parent)
7467         [[ "$val" == "$nm" ]] ||
7468                 error "parent of ${nm}_2 should be $nm, got $val"
7469
7470         do_facet $facet $LCTL nodemap_add -d -p ${nm}_1 ${nm}_3 ||
7471                 error "nodemap add ${nm}_3 on server failed"
7472         stack_trap "do_facet $facet $LCTL nodemap_del ${nm}_3 || true" EXIT
7473         do_facet $facet $LCTL nodemap_add_range --name ${nm}_3 \
7474                 --range $subnids4 &&
7475                        error "nodemap ${nm}_3 should not accept range $subnids4"
7476         do_facet $facet $LCTL nodemap_add_range --name ${nm}_3 \
7477                 --range $subnids5 &&
7478                        error "nodemap ${nm}_3 should not accept range $subnids5"
7479         do_facet $facet $LCTL nodemap_add_range --name ${nm}_3 \
7480                 --range $subnids6 &&
7481                        error "nodemap ${nm}_3 should not accept range $subnids6"
7482         do_facet $facet $LCTL nodemap_add_range --name ${nm}_3 \
7483                 --range $subnids3 ||
7484                         error "add_range $subnids3 for ${nm}_3 failed"
7485         val=$(do_facet $facet $LCTL get_param -n nodemap.${nm}_3.parent)
7486         [[ "$val" == "${nm}_1" ]] ||
7487                 error "parent of ${nm}_3 should be ${nm}_1, got $val"
7488         val=$(do_facet $facet $LCTL get_param -n nodemap.${nm}_3.squash_projid)
7489         (( val == 77 )) || error "squash_projid should be inherited, got $val"
7490
7491         do_facet $facet $LCTL nodemap_del_range --name $nm --range $nids ||
7492                 error "dynamic del_range on server failed"
7493         val=$(do_facet $facet $LCTL get_param nodemap.$nm.ranges |
7494                 awk 'BEGIN{RS=", "} $1=="start_nid:"{print $2 ; exit}')
7495         [[ -z "$val" ]] || error "nid range should be empty, got $val"
7496
7497         do_facet $facet $LCTL nodemap_del $nm ||
7498                 error "dynamic nodemap del on server failed"
7499         val=$(do_facet $facet $LCTL get_param nodemap.$nm.id)
7500         [[ -z "$val" ]] || error "nodemap should be gone, got $val"
7501
7502         if [[ "$(facet_active_host mgs)" != \
7503                         "$(facet_active_host $facet)" ]]; then
7504                 do_facet $facet $LCTL nodemap_add_range --name $mgsnm \
7505                         --range $mgsnids2 &&
7506                                 error "add_range $mgsnm on server should fail"
7507                 do_facet $facet $LCTL nodemap_del_range --name $mgsnm \
7508                         --range $mgsnids &&
7509                                 error "del_range $mgsnm on server should fail"
7510                 do_facet $facet $LCTL nodemap_add_idmap --name $mgsnm \
7511                         --idtype gid --idmap $mgsclid:$mgsfsid &&
7512                                 error "add_idmap $mgsnm on server should fail"
7513                 do_facet $facet $LCTL nodemap_del_idmap --name $mgsnm \
7514                         --idtype uid --idmap $mgsclid:$mgsfsid &&
7515                                 error "del_idmap $mgsnm on server should fail"
7516                 do_facet $facet $LCTL nodemap_modify --name $mgsnm \
7517                         --property squash_projid --value 77 &&
7518                                 error "modify $mgsnm on server should fail"
7519                 do_facet $facet $LCTL nodemap_del $mgsnm &&
7520                         error "nodemap del $mgsnm on server should fail"
7521         fi
7522         do_facet $facet $LCTL get_param -R 'nodemap.*'
7523 }
7524
7525 test_72a() {
7526         (( OST1_VERSION >= $(version_code 2.15.64) )) ||
7527                 skip "Need MDS >= 2.15.64 dynamic nodemaps"
7528
7529         [[ "$(facet_active_host mgs)" != "$(facet_active_host ost1)" ]] ||
7530                 skip "Need servers on different hosts"
7531
7532         dyn_nm_helper ost1
7533 }
7534 run_test 72a "dynamic nodemap properties on OSS"
7535
7536 test_72b() {
7537         (( MDS1_VERSION >= $(version_code 2.15.64) )) ||
7538                 skip "Need MDS >= 2.15.64 dynamic nodemaps"
7539
7540         dyn_nm_helper mds1
7541 }
7542 run_test 72b "dynamic nodemap properties on MDS"
7543
7544 test_72c() {
7545         local mgsnm=mgsnm
7546         local nm=nm_test72c
7547         local val
7548
7549         (( MDS1_VERSION >= $(version_code 2.16.52) )) ||
7550                 skip "Need MDS >= 2.16.52 dynamic nodemaps"
7551
7552         do_facet mgs $LCTL nodemap_add $mgsnm ||
7553                 error "adding $mgsnm on MGS failed"
7554         stack_trap "do_facet mgs $LCTL nodemap_del $mgsnm" EXIT
7555
7556         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7557                 --property child_raise_privileges --value trusted ||
7558                 error "modify raise_privileges for $mgsnm on MGS failed (1)"
7559         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7560                 --property admin --value 0 ||
7561                 error "modify admin for $mgsnm on MGS failed"
7562         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7563                 --property trusted --value 0 ||
7564                 error "modify trusted for $mgsnm on MGS failed"
7565         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7566                 --property deny_unknown --value 0 ||
7567                 error "modify deny_unknown for $mgsnm on MGS failed"
7568         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7569                 --property readonly_mount --value 0 ||
7570                 error "modify readonly_mount for $mgsnm on MGS failed"
7571         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7572                 --property rbac --value file_perms,quota_ops,byfid_ops ||
7573                 error "modify rbac for $mgsnm on MGS failed"
7574         wait_nm_sync $mgsnm rbac '' inactive
7575
7576         do_facet mds1 $LCTL nodemap_add -d -p $mgsnm $nm ||
7577                 error "dynamic nodemap on server failed (1)"
7578         stack_trap "do_facet mds1 $LCTL nodemap_del $nm || true" EXIT
7579         val=$(do_facet mds1 $LCTL get_param -n nodemap.$nm.id)
7580         if [[ -z "$val" || "$val" == "0" ]]; then
7581                 error "dynamic nodemap wrong id $val (1)"
7582         fi
7583         val=$(do_facet mds1 $LCTL get_param -n \
7584                 nodemap.$nm.child_raise_privileges)
7585         [[ $val == "trusted" ]] ||
7586                 error "dyn nodemap should inherit child_raise_privileges"
7587         do_facet mds1 $LCTL nodemap_modify --name $nm \
7588                 --property admin --value 1 &&
7589                 error "modify admin for $nm on mds1 should fail"
7590         do_facet mds1 $LCTL nodemap_modify --name $nm \
7591                 --property trusted --value 1 ||
7592                 error "modify trusted for $nm on mds1 failed"
7593         do_facet mds1 $LCTL nodemap_modify --name $nm \
7594                 --property deny_unknown --value 1 ||
7595                 error "modify deny_unknown for $nm on mds1 failed"
7596         do_facet mds1 $LCTL nodemap_modify --name $nm \
7597                 --property readonly_mount --value 1 ||
7598                 error "modify readonly_mount for $nm on mds1 failed"
7599         do_facet mds1 $LCTL nodemap_modify --name $nm \
7600                --property rbac --value file_perms,quota_ops,byfid_ops,dne_ops &&
7601                 error "modify rbac for $nm on mds1 should fail"
7602         do_facet mds1 $LCTL nodemap_modify --name $nm \
7603                 --property rbac --value file_perms ||
7604                 error "modify rbac for $nm on mds1 failed (1)"
7605         do_facet mds1 $LCTL nodemap_modify --name $nm \
7606                --property child_raise_privileges \
7607                --value trusted,admin &&
7608             error "modify nm.child_raise_privileges for $nm on mds1 should fail"
7609
7610         do_facet mds1 $LCTL nodemap_del $nm ||
7611                 error "failed to delete dynamic nodemap $nm"
7612
7613         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7614                 --property child_raise_privileges --value trusted,dne_ops ||
7615                 error "modify raise_privileges for $mgsnm on MGS failed (2)"
7616         wait_nm_sync $mgsnm child_raise_privileges '' inactive
7617
7618         do_facet mds1 $LCTL nodemap_add -d -p $mgsnm $nm ||
7619                 error "dynamic nodemap on server failed (2)"
7620         val=$(do_facet mds1 $LCTL get_param -n nodemap.$nm.id)
7621         if [[ -z "$val" || "$val" == "0" ]]; then
7622                 error "dynamic nodemap wrong id $val (2)"
7623         fi
7624         do_facet mds1 $LCTL nodemap_modify --name $nm \
7625                --property rbac --value file_perms,quota_ops,byfid_ops,dne_ops ||
7626                 error "modify rbac for $nm on mds1 failed (2)"
7627         do_facet mds1 $LCTL nodemap_modify --name $nm \
7628                --property child_raise_privileges \
7629                --value trusted ||
7630             error "modify nm.child_raise_privileges for $nm on mds1 failed (1)"
7631
7632         do_facet mds1 $LCTL nodemap_del $nm ||
7633                 error "failed to delete dynamic nodemap $nm"
7634
7635         do_facet mgs $LCTL nodemap_modify --name $mgsnm \
7636                 --property child_raise_privileges \
7637                 --value child_raise_privs,trusted,dne_ops ||
7638                 error "modify raise_privileges for $mgsnm on MGS failed (3)"
7639         wait_nm_sync $mgsnm child_raise_privileges '' inactive
7640
7641         do_facet mds1 $LCTL nodemap_add -d -p $mgsnm $nm ||
7642                 error "dynamic nodemap on server failed (3)"
7643         val=$(do_facet mds1 $LCTL get_param -n nodemap.$nm.id)
7644         if [[ -z "$val" || "$val" == "0" ]]; then
7645                 error "dynamic nodemap wrong id $val (3)"
7646         fi
7647         do_facet mds1 $LCTL nodemap_modify --name $nm \
7648                --property child_raise_privileges \
7649                --value child_raise_privs,trusted,dne_ops,admin ||
7650             error "modify nm.child_raise_privileges for $nm on mds1 failed (2)"
7651
7652         do_facet mds1 $LCTL get_param -R nodemap.*
7653 }
7654 run_test 72c "child_raise_privileges nodemap property"
7655
7656 test_73() {
7657         local vaultdir1=$DIR/$tdir/vault1
7658         local vaultdir2=$DIR/$tdir/vault2
7659         local shortfname="short=a"
7660         local longfname="longfilenamewitha=inthemiddletotestbehaviorregardingthedigestedform"
7661         local fid
7662         local digshort1
7663         local digshort2
7664         local diglong1
7665         local diglong2
7666
7667         (( $MDS1_VERSION >= $(version_code 2.16.50) )) ||
7668                 skip "Need MDS version at least 2.16.50"
7669
7670         [[ $($LCTL get_param mdc.*.import) =~ client_encryption ]] ||
7671                 skip "need encryption support"
7672         which fscrypt || skip_env "Need fscrypt"
7673
7674         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
7675
7676         yes | fscrypt setup --force --verbose ||
7677                 echo "fscrypt global setup already done"
7678         sed -i 's/\(.*\)policy_version\(.*\):\(.*\)\"[0-9]*\"\(.*\)/\1policy_version\2:\3"2"\4/' \
7679                 /etc/fscrypt.conf
7680         yes | fscrypt setup --verbose $MOUNT ||
7681                 echo "fscrypt setup $MOUNT already done"
7682         stack_trap "rm -rf $MOUNT/.fscrypt"
7683
7684         # enable_filename_encryption tunable only available for client
7685         # built against embedded llcrypt. If client is built against in-kernel
7686         # fscrypt, file names are always encrypted.
7687         $LCTL get_param mdc.*.connect_flags | grep -q name_encryption &&
7688           nameenc=$(lctl get_param -n llite.*.enable_filename_encryption |
7689                         head -n1)
7690
7691         # begin with non-encrypted names
7692         if [ -n "$nameenc" ] && (( nameenc != 0 )); then
7693                 $LCTL set_param llite.*.enable_filename_encryption=0
7694                 [ $? -eq 0 ] ||
7695                         error "set_param \
7696                                llite.*.enable_filename_encryption=1 failed"
7697         fi
7698
7699         mkdir -p $vaultdir1
7700         stack_trap "rm -rf $vaultdir1"
7701
7702         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
7703              --source=custom_passphrase --name=protector_73a $vaultdir1 ||
7704                 error "fscrypt encrypt $vaultdir1 failed"
7705
7706         # activate changelogs
7707         changelog_register || error "changelog_register failed"
7708         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
7709         changelog_users $SINGLEMDS | grep -q $cl_user ||
7710                 error "User $cl_user not found in changelog_users"
7711         changelog_chmask ALL
7712
7713         touch $vaultdir1/$shortfname ||
7714                 error "touch $vaultdir1/$shortfname failed"
7715         fid=$($LFS path2fid $vaultdir1/$shortfname)
7716         fid="${fid:1:-1}"
7717         fscrypt lock $vaultdir1 || error "fscrypt lock $vaultdir1 failed"
7718         digshort1=$($LFS fid2path $MOUNT $fid)
7719         digshort1=$(basename $digshort1)
7720         echo mypass | fscrypt unlock $vaultdir1 ||
7721                 error "fscrypt unlock $vaultdir1 failed"
7722         mrename $vaultdir1/$shortfname $vaultdir1/$longfname ||
7723                 error "mrename $vaultdir1/$shortfname failed"
7724         fscrypt lock $vaultdir1 || error "fscrypt lock $vaultdir1 failed"
7725         diglong1=$($LFS fid2path $MOUNT $fid)
7726         diglong1=$(basename $diglong1)
7727
7728         # access changelogs
7729         echo "changelogs dump"
7730         changelog_dump || error "failed to dump changelogs"
7731         digshort2=$(changelog_find -type CREAT -target-fid $fid |
7732                         awk '{print $12}')
7733         [[ $digshort1 == $digshort2 ]] ||
7734                 error "name $digshort2 in CREAT is not $digshort1"
7735         digshort2=$(changelog_find -type RENME -source-fid $fid |
7736                         awk '{print $15}')
7737         [[ $digshort1 == $digshort2 ]] ||
7738                 error "name $digshort2 in RENME is not $digshort1"
7739         diglong2=$(changelog_find -type RENME -source-fid $fid |
7740                         awk '{print $12}')
7741         [[ $diglong1 == $diglong2 ]] ||
7742                 error "name $diglong2 in RENME is not $diglong1"
7743
7744         echo "changelogs clear"
7745         changelog_clear 0 || error "failed to clear changelogs"
7746
7747         # now switch to encrypted names
7748         if [ -n "$nameenc" ] && (( nameenc != 1 )); then
7749                 $LCTL set_param llite.*.enable_filename_encryption=1
7750                 [ $? -eq 0 ] ||
7751                         error "set_param \
7752                                llite.*.enable_filename_encryption=1 failed"
7753                 stack_trap \
7754                         "$LCTL set_param llite.*.enable_filename_encryption=0"
7755         fi
7756
7757         $LFS mkdir -c1 -i $((MDSCOUNT-1)) $vaultdir2
7758         stack_trap "rm -rf $vaultdir2"
7759
7760         echo -e 'mypass\nmypass' | fscrypt encrypt --verbose \
7761              --source=custom_passphrase --name=protector_73b $vaultdir2 ||
7762                 error "fscrypt encrypt $vaultdir2 failed"
7763
7764         touch $vaultdir2/$shortfname ||
7765                 error "touch $vaultdir2/$shortfname failed"
7766         fid=$($LFS path2fid $vaultdir2/$shortfname)
7767         fid="${fid:1:-1}"
7768         fscrypt lock $vaultdir2 || error "fscrypt lock $vaultdir2 failed"
7769         digshort1=$($LFS fid2path $MOUNT $fid)
7770         digshort1=$(basename $digshort1)
7771         echo mypass | fscrypt unlock $vaultdir2 ||
7772                 error "fscrypt unlock $vaultdir2 failed"
7773         mrename $vaultdir2/$shortfname $vaultdir2/$longfname ||
7774                 error "mrename $vaultdir2/$shortfname failed"
7775         fscrypt lock $vaultdir2 || error "fscrypt lock $vaultdir2 failed"
7776         diglong1=$($LFS fid2path $MOUNT $fid)
7777         diglong1=$(basename $diglong1)
7778
7779         # access changelogs
7780         echo "changelogs dump"
7781         changelog_dump || error "failed to dump changelogs"
7782         digshort2=$(changelog_find -type CREAT -target-fid $fid |
7783                         awk '{print $12}')
7784         [[ $digshort1 == $digshort2 ]] ||
7785                 error "name $digshort2 in CREAT is not $digshort1"
7786         digshort2=$(changelog_find -type RENME -source-fid $fid |
7787                         awk '{print $15}')
7788         [[ $digshort1 == $digshort2 ]] ||
7789                 error "name $digshort2 in RENME is not $digshort1"
7790         diglong2=$(changelog_find -type RENME -source-fid $fid |
7791                         awk '{print $12}')
7792         [[ $diglong1 == $diglong2 ]] ||
7793                 error "name $diglong2 in RENME is not $diglong1"
7794 }
7795 run_test 73 "encrypted names in changelogs"
7796
7797 test_74() {
7798         local testfile="${DIR}/${tdir}/$tfile"
7799         local deny_mount
7800
7801         # check that deny_mount flag exists
7802         deny_mount=$(do_facet mgs \
7803                         $LCTL get_param -n nodemap.default.deny_mount)
7804         [[ -n "$deny_mount" ]] ||
7805                 skip "Server does not have the deny_mount nodemap flag"
7806
7807         stack_trap cleanup_local_client_nodemap EXIT
7808
7809         umount_client $MOUNT || error "umount $MOUNT failed (1)"
7810
7811         # setup privileged nodemap for c0
7812         setup_local_client_nodemap "c0" 1 1
7813
7814         # check default deny_mount flags
7815         (( $deny_mount == 0 )) ||
7816                 error "wrong default for deny_mount flag on default nodemap"
7817         deny_mount=$(do_facet mgs \
7818                         $LCTL get_param -n nodemap.c0.deny_mount)
7819         (( $deny_mount == 0 )) ||
7820                 error "wrong default value for deny_mount on nodemap c0"
7821
7822         # mount client with active nodemap
7823         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS} ||
7824                 error "re-mount failed (1)"
7825         wait_ssk
7826
7827         # simple access test
7828         $LFS mkdir -c 1 "${DIR}/$tdir" || error "mkdir ${DIR}/$tdir failed"
7829         $LFS setstripe -c 1 $testfile || error "setstripe $testfile failed"
7830         echo -n "a" > $testfile || error "(1) write $testfile failed"
7831
7832         # set deny_mount flag. Access should still work for existing clients
7833         do_facet mgs $LCTL nodemap_modify --name c0 \
7834                 --property deny_mount --value 1
7835         wait_nm_sync c0 deny_mount
7836         echo -n "b" >> $testfile || error "(2) write $testfile failed"
7837         cat $testfile > /dev/null || error "read $testfile failed"
7838         # unmount client
7839         umount_client $MOUNT || error "umount $MOUNT failed (2)"
7840
7841         # mount client should fail (nodemap is deny_mount)
7842         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS} &&
7843                 error "mount should have failed. deny_mount flag is not honored"
7844
7845         # set active flag for c0. Access should work again
7846         do_facet mgs $LCTL nodemap_modify --name c0 \
7847                 --property deny_mount --value 0
7848         wait_nm_sync c0 deny_mount
7849
7850         zconf_mount_clients $HOSTNAME $MOUNT ${MOUNT_OPTS} ||
7851                 error "re-mount failed (2)"
7852         wait_ssk
7853
7854         # check access
7855         echo -n "c" >> $testfile || error "(3) write $testfile failed"
7856         [[ $(cat $testfile) == "abc" ]] ||
7857                 error "read access test for $testfile failed"
7858 }
7859 run_test 74 "Set nodemap deny_mount flag"
7860
7861 check_ost_object_ids() {
7862         local file=$1
7863         local expected_uid=$2
7864         local expected_gid=$3
7865         local expected_projid=$4
7866         local objdump=$DIR/$tdir/objdump
7867
7868         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
7869
7870         # Get the OST object path. We assume the file has one stripe on ost1
7871         local fids=($($LFS getstripe $file | grep 0x))
7872         local fid="${fids[3]}:${fids[2]}:0"
7873         local objpath=$(ost_fid2_objpath ost1 $fid)
7874
7875         do_facet ost1 "$DEBUGFS -c -R 'stat $objpath' $(ostdevname 1)" |
7876                 grep "Project" > $objdump
7877         local obj_uid=$(awk '{print $2}' $objdump)
7878         local obj_gid=$(awk '{print $4}' $objdump)
7879         local obj_projid=$(awk '{print $6}' $objdump)
7880         echo "OST object ids and size for file '$file': $(cat $objdump)"
7881
7882         [[ "$obj_uid" == "$expected_uid" ]] ||
7883                 error "uid is not set to expected value $expected_uid"
7884         [[ "$obj_gid" == "$expected_gid" ]] ||
7885                 error "gid is not set to expected value $expected_gid"
7886         [[ "$obj_projid" == "$expected_projid" ]] ||
7887                 error "projid is not set to expected value $expected_projid"
7888 }
7889
7890 check_mdt_inode_ids() {
7891         local file=${1#${MOUNT}}
7892         local expected_uid=$2
7893         local expected_gid=$3
7894         local expected_projid=$4
7895         local objdump=$DIR/$tdir/objdump
7896
7897         if (( $MDSCOUNT != 1 )); then
7898                 echo "DNE not supported; checking IDs on MDT assumes a single MDT only"
7899                 return 0
7900         fi
7901
7902         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
7903
7904         do_facet mds1 "$DEBUGFS -c -R 'stat ROOT${file}' $(mdsdevname 1)" |
7905                 grep "Project" > $objdump
7906
7907         local obj_uid=$(awk '{print $2}' $objdump)
7908         local obj_gid=$(awk '{print $4}' $objdump)
7909         local obj_projid=$(awk '{print $6}' $objdump)
7910         echo "MDT inode ids and size for file '$file': $(cat $objdump)"
7911
7912         [[ "$obj_uid" == "$expected_uid" ]] ||
7913                 error "uid is not set to expected value $expected_uid"
7914         [[ "$obj_gid" == "$expected_gid" ]] ||
7915                 error "gid is not set to expected value $expected_gid"
7916         [[ "$obj_projid" == "$expected_projid" ]] ||
7917                 error "projid is not set to expected value $expected_projid"
7918 }
7919
7920 check_ids_sync() {
7921         sync
7922         # wait for asynchronous MDS-OST sync and force flush to OST
7923         sync_all_data
7924         wait_mds_ost_sync || error "wait_mds_ost_sync failed"
7925         # drop_caches to flush inode cache so ID updates from chown or
7926         # lfs project are visible through debugfs on the OST objects
7927         do_facet ost1 "sync; sync; echo 3 > /proc/sys/vm/drop_caches"
7928         # drop_caches to flush dentry cache so namespace updates from "mv"
7929         # operations are visible through debugfs on the MDT
7930         do_facet mds "sync; sync; echo 3 > /proc/sys/vm/drop_caches"
7931 }
7932
7933 test_75() {
7934         local testdir="${DIR}/${tdir}"
7935         local projdir="${testdir}/projdir"
7936         local tfile_write=${projdir}/${tfile}_write
7937         local tfile_trunc=${projdir}/${tfile}_trunc
7938         local tfile_creat=${projdir}/${tfile}_creat
7939         local tfile_falloc=${projdir}/${tfile}_falloc
7940         local tfile_write2=${testdir}/${tfile}_write2
7941         local testdir_projid=42
7942         local testfile_projid=43
7943         local have_ost_punch_ids=false
7944
7945         # prior to 2.16.53 OST_PUNCH did not set OST IDs
7946         (( $OST1_VERSION >= $(version_code 2.16.53) &&
7947                 $CLIENT_VERSION >= $(version_code 2.16.53) )) &&
7948                 have_ost_punch_ids=true
7949
7950         [[ "$ost1_FSTYPE" == ldiskfs ]] ||
7951                 skip "ldiskfs only test (using debugfs)"
7952
7953         # setup
7954         mkdir -p $projdir || error "mkdir $projdir failed"
7955         stack_trap "rm -rf $DIR/$tdir" EXIT
7956
7957         $LFS project -s -p $testdir_projid $projdir ||
7958                 error "lfs project failed"
7959         chown -R $USER0 $DIR/$tdir || error "chown Failed"
7960
7961         # setstripe is primarily used to force data being created on ost1
7962
7963         # OST_WRITE RPC (dd) - in projdir
7964         $RUNAS_CMD -u $ID0 $LFS setstripe -c 1 -i 0 $tfile_write ||
7965                 error "setstripe for file $tfile_write failed"
7966         $RUNAS_CMD -u $ID0 dd if=/dev/urandom of=$tfile_write bs=1M count=1 ||
7967                 error "dd for file $tfile_write failed"
7968
7969         # OST_WRITE RPC (dd) - not in projdir
7970         $RUNAS_CMD -u $ID0 $LFS setstripe -c 1 -i 0 $tfile_write2 ||
7971                 error "setstripe for file $tfile_write2 failed"
7972         $RUNAS_CMD -u $ID0 \
7973                 dd if=/dev/urandom of=$tfile_write2 bs=1M count=1 ||
7974                 error "dd for file $tfile_write2 failed"
7975
7976         if $have_ost_punch_ids; then
7977                 # OST_PUNCH RPC (truncate)
7978                 $RUNAS_CMD -u $ID0 $LFS setstripe -c 1 -i 0 $tfile_trunc ||
7979                         error "setstripe for file $tfile_trunc failed"
7980                 $RUNAS_CMD -u $ID0 $TRUNCATE $tfile_trunc 1048576 ||
7981                         error "truncate for file $tfile_trunc failed"
7982         fi
7983
7984         # LDLM_ENQUEUE RPC (IT_CREAT intent) (setstripe)
7985         $RUNAS_CMD -u $ID0 $LFS setstripe -c 1 -i 0 $tfile_creat ||
7986                 error "setstripe for file $tfile_creat failed"
7987
7988         # OST_FALLOCATE RPC (fallocate)
7989         $RUNAS_CMD -u $ID0 $LFS setstripe -c 1 -i 0 $tfile_falloc ||
7990                 error "setstripe for file $tfile_falloc failed"
7991         $RUNAS_CMD -u $ID0 fallocate -l 1M $tfile_falloc ||
7992                 error "fallocate for file $tfile_falloc failed"
7993
7994         check_ids_sync
7995
7996         # check IDs are set correctly
7997         check_mdt_inode_ids $tfile_write $ID0 $ID0 $testdir_projid
7998         check_ost_object_ids $tfile_write $ID0 $ID0 $testdir_projid
7999
8000         check_mdt_inode_ids $tfile_write2 $ID0 $ID0 0
8001         check_ost_object_ids $tfile_write2 $ID0 $ID0 0
8002
8003         if $have_ost_punch_ids; then
8004                 check_mdt_inode_ids $tfile_trunc $ID0 $ID0 $testdir_projid
8005                 check_ost_object_ids $tfile_trunc $ID0 $ID0 $testdir_projid
8006         fi
8007
8008         check_mdt_inode_ids $tfile_falloc $ID0 $ID0 $testdir_projid
8009         check_ost_object_ids $tfile_falloc $ID0 $ID0 $testdir_projid
8010
8011         check_mdt_inode_ids $tfile_creat $ID0 $ID0 $testdir_projid
8012
8013         # move file to projdir should set PROJID from directory
8014         # MDS_REINT RPC Client->MDS; OST_SETATTR RPC MDS->OST
8015         mv $tfile_write ${testdir}/ || error "mv $tfile_write failed"
8016         tfile_write=$testdir/${tfile}_write
8017
8018         # set explicit PROJID outside of projdir
8019         # MDS_REINT RPC Client->MDS; OST_SETATTR RPC MDS->OST
8020         $LFS project -p $testfile_projid $tfile_write2 ||
8021                 error "lfs project failed"
8022
8023         check_ids_sync
8024
8025         check_mdt_inode_ids $tfile_write $ID0 $ID0 $testdir_projid
8026         check_ost_object_ids $tfile_write $ID0 $ID0 $testdir_projid
8027
8028         check_mdt_inode_ids $tfile_write2 $ID0 $ID0 $testfile_projid
8029         check_ost_object_ids $tfile_write2 $ID0 $ID0 $testfile_projid
8030
8031         # move file to projdir should set new PROJID from directory
8032         # MDS_REINT RPC Client->MDS; OST_SETATTR RPC MDS->OST
8033         mv $tfile_write2 $projdir || error "mv $tfile_write2 failed"
8034         tfile_write2=$projdir/${tfile}_write2
8035
8036         # chown should set new UID/GID
8037         # MDS_REINT RPC Client->MDS; OST_SETATTR RPC MDS->OST
8038         chown $ID1:$ID1 $tfile_write || error "chown $tfile_write failed"
8039
8040         check_ids_sync
8041
8042         check_mdt_inode_ids $tfile_write2 $ID0 $ID0 $testdir_projid
8043         check_ost_object_ids $tfile_write2 $ID0 $ID0 $testdir_projid
8044
8045         check_mdt_inode_ids $tfile_write $ID1 $ID1 $testdir_projid
8046         check_ost_object_ids $tfile_write $ID1 $ID1 $testdir_projid
8047 }
8048 run_test 75 "check uid/gid/projid are set on OST and MDT for various RPCs"
8049
8050 setup_75a() {
8051         # Assumes that variables from test_75a are set
8052         # Setup c0 (trusted) and c1 (tenant) nodemaps used by the clients
8053         nodemap_test_setup
8054         trap cleanup_75a EXIT
8055
8056         # configure tentant nodemap
8057         do_facet mgs $LCTL nodemap_set_fileset --name $nm_tenant \
8058                 --fileset "/$fileset_nm" || error "Setting fileset failed"
8059         do_facet mgs $LCTL nodemap_add_offset --name $nm_tenant \
8060                 --offset $offset_start --limit $offset_limit ||
8061                 error "cannot set offset for $nm_tenant"
8062         do_facet mgs $LCTL nodemap_modify --name $nm_tenant \
8063                 --property map_mode=projid ||
8064                 error "cannot set offset for $nm_tenant"
8065
8066         # configure trusted nodemap
8067         do_facet mgs $LCTL nodemap_modify --name $nm_trusted \
8068                 --property admin --value 1 || error "Setting admin=1 failed"
8069         do_facet mgs $LCTL nodemap_modify --name $nm_trusted \
8070                 --property trusted --value 1 || error "Setting trusted=1 failed"
8071
8072         wait_nm_sync $nm_trusted trusted_nodemap
8073
8074         # create and set ownership for fileset dir of "nm_tenant"
8075         $run_as_trusted mkdir -p $fileset_subdir ||
8076                 error "mkdir $fileset_subdir failed"
8077         $run_as_trusted chown $((offset_start+ID0)) $fileset_subdir
8078
8079         # remount clients for nodemap changes to take effect.
8080         # This mounts the trusted nodemap (c0) and tenant nodemap (c1)
8081         export FILESET=/
8082         for client in "${clients_arr[@]}"; do
8083                 zconf_umount_clients $client $MOUNT ||
8084                         error "unable to umount client ${clients_arr[0]}"
8085                 zconf_mount_clients $client $MOUNT $MOUNT_OPTS ||
8086                         error "unable to umount client ${clients_arr[0]}"
8087         done
8088         unset FILESET
8089         wait_ssk
8090 }
8091
8092 setup_namespace_75a() {
8093         # Assumes that variables from test_75a are set
8094         setup_tfiles_75a() {
8095                 local tenant_file=$1
8096                 local tenant_dir=$2
8097                 local offset=$3
8098                 $run_as_trusted echo "abc" > ${fileset_subdir}/$tenant_file ||
8099                         error "echo $tenant_file failed"
8100                 $run_as_trusted mkdir -p ${fileset_subdir}/$tenant_dir ||
8101                         error "mkdir $tenant_dir failed"
8102                 $run_as_trusted chmod 777 ${fileset_subdir}/$tenant_file \
8103                         ${fileset_subdir}/$tenant_dir ||
8104                         error "chmod 777 $tenant_file and $tenant_dir failed"
8105                 $run_as_trusted chown \
8106                         $((offset+ID0)):$((offset+ID0)) \
8107                         ${fileset_subdir}/$tenant_file \
8108                         ${fileset_subdir}/$tenant_dir ||
8109                         error "chown $tenant_file and $tenant_dir failed"
8110         }
8111
8112         # setup testfiles and testdirectories. *_trusted files/dirs are
8113         # world-accessible, but become inaccessible once the id_check is enabled
8114         $run_as_trusted echo "abc" > ${fileset_subdir}/$tfile_trusted ||
8115                 error "echo $tfile_trusted failed"
8116         $run_as_trusted mkdir ${fileset_subdir}/$tdir_trusted ||
8117                 error "mkdir $tdir_trusted failed"
8118         $run_as_trusted chmod 777 ${fileset_subdir}/$tdir_trusted \
8119                 ${fileset_subdir}/$tfile_trusted ||
8120                 error "chmod 777 $tdir_trusted failed"
8121
8122         setup_tfiles_75a $tfile_tl $tdir_tl 100000
8123         setup_tfiles_75a $tfile_tenant $tdir_tenant $offset_start
8124         setup_tfiles_75a $tfile_tr $tdir_tr 300000
8125
8126         # DoM files
8127         $run_as_trusted $LFS setstripe -E 1M -L mdt \
8128                  ${fileset_subdir}/${tfile_trusted}_dom ||
8129                 error "setstripe ${tfile_trusted}_dom failed"
8130         $run_as_trusted chmod 777 ${fileset_subdir}/${tfile_trusted}_dom ||
8131                         error "chmod 777 ${tfile_trusted}_dom failed"
8132         $run_as_trusted $LFS setstripe -E 1M -L mdt \
8133                  ${fileset_subdir}/${tfile_tenant}_dom ||
8134                 error "setstripe ${tfile_tenant}_dom failed"
8135         $run_as_trusted chown \
8136                         $((offset_start+ID0)):$((offset_start+ID0)) \
8137                         ${fileset_subdir}/${tfile_tenant}_dom ||
8138                         error "chown ${tfile_tenant}_dom failed"
8139
8140         # create a file used in write tests
8141         $run_as_trusted echo "def" > ${fileset_subdir}/$tf_write ||
8142                 error "echo  $tf_write failed"
8143         $run_as_trusted chown \
8144                 $((offset_start+ID0)):$((offset_start+ID0)) \
8145                 ${fileset_subdir}/$tf_write ||
8146                 error "chown $tf_write failed"
8147 }
8148
8149 cleanup_75a() {
8150         do_nodes $(all_mdts_nodes) \
8151                 $LCTL set_param mdt.*.enable_resource_id_check=0 ||
8152                         error "disabling resource id check on MDTs failed"
8153
8154         do_nodes $(all_osts_nodes) \
8155                 $LCTL set_param obdfilter.*.enable_resource_id_check=0 ||
8156                         error "disabling resource id check on OSTs failed"
8157
8158         nodemap_test_cleanup
8159
8160         for client in "${clients_arr[@]}"; do
8161                 zconf_umount_clients $client $MOUNT ||
8162                         error "unable to umount client $client"
8163                 zconf_mount_clients $client $MOUNT $MOUNT_OPTS ||
8164                         error "unable to umount client $client"
8165         done
8166         wait_ssk
8167 }
8168
8169 test_75a() {
8170         local offset_start=200000
8171         local offset_limit=100000
8172         local nm_trusted="c0"
8173         local nm_tenant="c1"
8174         local fileset_nm="${tdir}/${nm_tenant}_dir"
8175         local fileset_subdir="${DIR}/${fileset_nm}"
8176         local tfile_trusted="testfile_trusted"
8177         local tfile_tenant="testfile_tenant"
8178         local tdir_trusted="testdir_trusted"
8179         local tdir_tenant="testdir_tenant"
8180         # *_tl and *_tr files/dirs are set up such that their fs_ids are to the
8181         # left and right of the tenant's offset range, respectively. This is to
8182         # exercise both cases of nodemap_map_id() when mapping FS to client IDs.
8183         local tfile_tl="testfile_tenant_left"
8184         local tdir_tl="testdir_tenant_left"
8185         local tfile_tr="testfile_tenant_right"
8186         local tdir_tr="testdir_tenant_right"
8187         local tf_write="testf_write"
8188         local tf="testfile"
8189         local client_trusted
8190         local run_as_tenant
8191         local out
8192
8193         # This test checks that the enable_resource_id_check flag works
8194         # correctly by having a tenant accessing squashed files.
8195         # Without this check, tenants are able to access such files
8196         # that have world-accessible permissions.
8197         # With the flag enabled, this is no longer possible.
8198
8199         # check that enable_resource_id_check flag exists
8200         do_facet mds $LCTL get_param -n mdt.*.enable_resource_id_check ||
8201                 skip "MDS does not have the enable_resource_id_check flag"
8202         do_facet ost $LCTL get_param -n obdfilter.*.enable_resource_id_check ||
8203                 skip "OSS does not have the enable_resource_id_check flag"
8204
8205         # need two clients to continue
8206         (( $CLIENTCOUNT >= 2 )) || skip "need at least two clients"
8207
8208         if $SHARED_KEY; then
8209                 skip "need non-shared key for this test"
8210         fi
8211
8212         # assign clients and helper routines
8213         client_trusted=${clients_arr[0]}
8214         client_tenant=${clients_arr[1]}
8215         run_as_tenant="do_node $client_tenant $RUNAS_CMD -u $ID0"
8216         run_as_trusted="do_node $client_trusted"
8217
8218         setup_75a
8219
8220         do_nodes $(all_mdts_nodes) \
8221                 $LCTL set_param mdt.*.enable_resource_id_check=0 ||
8222                         error "disabling resource id check on MDTs failed"
8223
8224         do_nodes $(all_osts_nodes) \
8225                 $LCTL set_param obdfilter.*.enable_resource_id_check=0 ||
8226                         error "disabling resource id check on OSTs failed"
8227
8228         report_client_view_75a() {
8229                 echo "Trusted view:"
8230                 $run_as_trusted ls -al $fileset_subdir
8231                 echo "------------------------------"
8232                 echo "Tenant view:"
8233                 $run_as_tenant ls -al $MOUNT
8234         }
8235
8236         75a_drop_tenant_cache() {
8237                 do_node $client_tenant \
8238                         "sync ; echo 3 > /proc/sys/vm/drop_caches"
8239         }
8240
8241         75a_op_test() {
8242                 local test_cmd="$run_as_tenant $1"
8243                 local test_success=${2:-true}
8244                 if $test_success; then
8245                         $test_cmd || error "$1 failed"
8246                 else
8247                         $test_cmd && error "$1 should've failed"
8248                 fi
8249         }
8250
8251         75a_read_test() {
8252                 local test_cmd="$run_as_tenant $1"
8253                 local test_success=${2:-true}
8254                 local expected=${3:-"def"}
8255                 local out
8256                 if $test_success; then
8257                         out=$($test_cmd) || error "$1 failed"
8258                         echo $out
8259                         [[ $out == $expected ]] ||
8260                                 error "read $expected for $1 incorrect"
8261                 else
8262                         $test_cmd && error "$1 should've failed"
8263                 fi
8264         }
8265
8266         75a_getxattr_test() {
8267                 local test_cmd="$run_as_tenant getfattr -n user.abc $1"
8268                 local test_success=${2:-true}
8269                 local expected=${3:-"\"def\""}
8270                 local out
8271                 if $test_success; then
8272                         out=$($test_cmd | awk -F'=' '/user.abc/ {print $2}') ||
8273                                 error "$1 failed"
8274                         echo $out
8275                         [[ $out == $expected ]] ||
8276                                 error "getxattr $expected for $1 incorrect"
8277                 else
8278                         $test_cmd && error "$1 should've failed"
8279                 fi
8280         }
8281
8282         # Setup testrun 1
8283         setup_namespace_75a
8284         report_client_view_75a
8285         # Testrun 1 begins (check disabled)
8286         # 1. write to files
8287         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_trusted" true
8288         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tl" true
8289         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tenant" true
8290         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tr" true
8291         75a_drop_tenant_cache
8292         # 2. read from files
8293         75a_read_test "cat ${MOUNT}/$tfile_trusted" true
8294         75a_read_test "cat ${MOUNT}/$tfile_tl" true
8295         75a_read_test "cat ${MOUNT}/$tfile_tenant" true
8296         75a_read_test "cat ${MOUNT}/$tfile_tr" true
8297         75a_drop_tenant_cache
8298         # 3. create files in various dirs
8299         75a_op_test "touch ${MOUNT}/${tdir_trusted}/$tf" true
8300         75a_op_test "touch ${MOUNT}/${tdir_tl}/$tf" true
8301         75a_op_test "touch ${MOUNT}/${tdir_tenant}/$tf" true
8302         75a_op_test "touch ${MOUNT}/${tdir_tr}/$tf" true
8303         # 4. soft and hard links
8304         75a_op_test "ln ${MOUNT}/$tfile_trusted \
8305                 ${MOUNT}/${tfile_trusted}_hlink" true
8306         75a_op_test "ln -s ${MOUNT}/$tfile_trusted \
8307                 ${MOUNT}/${tfile_trusted}_slink" true
8308         75a_op_test "ln ${MOUNT}/$tfile_tl \
8309                 ${MOUNT}/${tfile_tl}_hlink" true
8310         75a_op_test "ln -s ${MOUNT}/$tfile_tl \
8311                 ${MOUNT}/${tfile_tl}_slink" true
8312         75a_op_test "ln ${MOUNT}/$tfile_tenant \
8313                 ${MOUNT}/${tfile_tenant}_hlink" true
8314         75a_op_test "ln -s ${MOUNT}/$tfile_tenant \
8315                 ${MOUNT}/${tfile_tenant}_slink" true
8316         75a_op_test "ln ${MOUNT}/$tfile_tr \
8317                 ${MOUNT}/${tfile_tr}_hlink" true
8318         75a_op_test "ln -s ${MOUNT}/$tfile_tr \
8319                 ${MOUNT}/${tfile_tr}_slink" true
8320         75a_read_test "cat ${MOUNT}/${tfile_trusted}_hlink" true
8321         75a_read_test "cat ${MOUNT}/${tfile_trusted}_slink" true
8322         75a_read_test "cat ${MOUNT}/${tfile_tl}_hlink" true
8323         75a_read_test "cat ${MOUNT}/${tfile_tl}_slink" true
8324         75a_read_test "cat ${MOUNT}/${tfile_tenant}_hlink" true
8325         75a_read_test "cat ${MOUNT}/${tfile_tenant}_slink" true
8326         75a_read_test "cat ${MOUNT}/${tfile_tr}_hlink" true
8327         75a_read_test "cat ${MOUNT}/${tfile_tr}_slink" true
8328         75a_op_test "rm ${MOUNT}/*_hlink" true
8329         75a_op_test "rm ${MOUNT}/*_slink" true
8330         # 5. fallocate (zfs does not support pre-allocation via fallocate(2))
8331         if [[ "$ost1_FSTYPE" == "ldiskfs" ]]; then
8332                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_trusted" true
8333                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tl" true
8334                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tenant" true
8335                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tr" true
8336         fi
8337         # 6. truncate
8338         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_trusted 524288" true
8339         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tl 524288" true
8340         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tenant 524288" true
8341         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tr 524288" true
8342         # 7. rename files (and back)
8343         75a_op_test "mv ${MOUNT}/$tfile_trusted ${MOUNT}/${tfile_trusted}_" true
8344         75a_op_test "mv ${MOUNT}/${tfile_trusted}_ ${MOUNT}/$tfile_trusted" true
8345         75a_op_test "mv ${MOUNT}/$tfile_tl ${MOUNT}/${tfile_tl}_" true
8346         75a_op_test "mv ${MOUNT}/${tfile_tl}_ ${MOUNT}/$tfile_tl" true
8347         75a_op_test "mv ${MOUNT}/$tfile_tenant ${MOUNT}/${tfile_tenant}_" true
8348         75a_op_test "mv ${MOUNT}/${tfile_tenant}_ ${MOUNT}/$tfile_tenant" true
8349         75a_op_test "mv ${MOUNT}/$tfile_tr ${MOUNT}/${tfile_tr}_" true
8350         75a_op_test "mv ${MOUNT}/${tfile_tr}_ ${MOUNT}/$tfile_tr" true
8351         # 8. trigger setattr operation with "touch" (timestamp update)
8352         75a_op_test "touch ${MOUNT}/$tfile_trusted" true
8353         75a_op_test "touch ${MOUNT}/$tfile_tl" true
8354         75a_op_test "touch ${MOUNT}/$tfile_tenant" true
8355         75a_op_test "touch ${MOUNT}/$tfile_tr" true
8356         # 9. xattr, set and get
8357         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_trusted" true
8358         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tl" true
8359         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tenant" true
8360         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tr" true
8361         75a_getxattr_test ${MOUNT}/$tfile_trusted true
8362         75a_getxattr_test ${MOUNT}/$tfile_tl true
8363         75a_getxattr_test ${MOUNT}/$tfile_tenant true
8364         75a_getxattr_test ${MOUNT}/$tfile_tr true
8365         # 10. remove create files from tenant dirs
8366         75a_op_test "rm ${MOUNT}/${tdir_trusted}/$tf" true
8367         75a_op_test "rm ${MOUNT}/${tdir_tl}/$tf" true
8368         75a_op_test "rm ${MOUNT}/${tdir_tenant}/$tf" true
8369         75a_op_test "rm ${MOUNT}/${tdir_tr}/$tf" true
8370         # 11. remove all tenant dirs
8371         75a_op_test "rmdir ${MOUNT}/$tdir_trusted" true
8372         75a_op_test "rmdir ${MOUNT}/$tdir_tl" true
8373         75a_op_test "rmdir ${MOUNT}/$tdir_tenant" true
8374         75a_op_test "rmdir ${MOUNT}/$tdir_tr" true
8375         # 12. remove remaining tenant files from root
8376         75a_op_test "rm ${MOUNT}/$tfile_trusted" true
8377         75a_op_test "rm ${MOUNT}/$tfile_tl" true
8378         75a_op_test "rm ${MOUNT}/$tfile_tenant" true
8379         75a_op_test "rm ${MOUNT}/$tfile_tr" true
8380         # 13. Data on MDT cases
8381         if [[ "$mds1_FSTYPE" == "ldiskfs" ]]; then
8382                 75a_op_test "fallocate -l 1M ${MOUNT}/${tfile_trusted}_dom" true
8383         fi
8384         75a_op_test "$TRUNCATE ${MOUNT}/${tfile_trusted}_dom 524288" true
8385         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/${tfile_trusted}_dom" true
8386         75a_drop_tenant_cache
8387         75a_read_test "cat ${MOUNT}/${tfile_trusted}_dom" true
8388         75a_op_test "rm ${MOUNT}/${tfile_trusted}_dom" true
8389
8390         if [[ "$mds1_FSTYPE" == "ldiskfs" ]]; then
8391                 75a_op_test "fallocate -l 1M ${MOUNT}/${tfile_tenant}_dom" true
8392         fi
8393         75a_op_test "$TRUNCATE ${MOUNT}/${tfile_tenant}_dom 524288" true
8394         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/${tfile_tenant}_dom" true
8395         75a_drop_tenant_cache
8396         75a_read_test "cat ${MOUNT}/${tfile_tenant}_dom" true
8397         75a_op_test "rm ${MOUNT}/${tfile_tenant}_dom" true
8398
8399         report_client_view_75a
8400
8401         do_nodes $(all_mdts_nodes) \
8402                 $LCTL set_param mdt.*.enable_resource_id_check=1 ||
8403                         error "enabling resource id check on MDTs failed"
8404
8405         do_nodes $(all_osts_nodes) \
8406                 $LCTL set_param obdfilter.*.enable_resource_id_check=1 ||
8407                         error "enabling resource id check on OSTs failed"
8408
8409         # Setup testrun 2
8410         setup_namespace_75a
8411         report_client_view_75a
8412
8413         # Testrun 2 begins (check enabled)
8414         # 1. write to files
8415         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_trusted" false
8416         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tl" false
8417         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tenant" true
8418         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/$tfile_tr" false
8419         75a_drop_tenant_cache
8420         # 2. read from files
8421         75a_read_test "cat ${MOUNT}/$tfile_trusted" false
8422         75a_read_test "cat ${MOUNT}/$tfile_tl" false
8423         75a_read_test "cat ${MOUNT}/$tfile_tenant" true
8424         75a_read_test "cat ${MOUNT}/$tfile_tr" false
8425         75a_drop_tenant_cache
8426         # 3. create files
8427         75a_op_test "touch ${MOUNT}/${tdir_trusted}/$tf" false
8428         75a_op_test "touch ${MOUNT}/${tdir_tl}/$tf" false
8429         75a_op_test "touch ${MOUNT}/${tdir_tenant}/$tf" true
8430         75a_op_test "touch ${MOUNT}/${tdir_tr}/$tf" false
8431         # 4. soft and hard links (cannot create hard links but soft links)
8432         75a_op_test "ln ${MOUNT}/$tfile_trusted \
8433                 ${MOUNT}/${tfile_trusted}_hlink" false
8434         75a_op_test "ln -s ${MOUNT}/$tfile_trusted \
8435                 ${MOUNT}/${tfile_trusted}_slink" true
8436         75a_op_test "ln ${MOUNT}/$tfile_tl \
8437                 ${MOUNT}/${tfile_tl}_hlink" false
8438         75a_op_test "ln -s ${MOUNT}/$tfile_tl \
8439                 ${MOUNT}/${tfile_tl}_slink" true
8440         75a_op_test "ln ${MOUNT}/$tfile_tenant \
8441                 ${MOUNT}/${tfile_tenant}_hlink" true
8442         75a_op_test "ln -s ${MOUNT}/$tfile_tenant \
8443                 ${MOUNT}/${tfile_tenant}_slink" true
8444         75a_op_test "ln ${MOUNT}/$tfile_tr \
8445                 ${MOUNT}/${tfile_tr}_hlink" false
8446         75a_op_test "ln -s ${MOUNT}/$tfile_tr \
8447                 ${MOUNT}/${tfile_tr}_slink" true
8448         # can only read soft-links pointing to permitted files
8449         75a_read_test "cat ${MOUNT}/${tfile_trusted}_slink" false
8450         75a_read_test "cat ${MOUNT}/${tfile_tl}_slink" false
8451         75a_read_test "cat ${MOUNT}/${tfile_tenant}_slink" true
8452         75a_read_test "cat ${MOUNT}/${tfile_tr}_slink" false
8453         # can remove all links created by tenant
8454         75a_op_test "rm ${MOUNT}/${tfile_trusted}_slink" true
8455         75a_op_test "rm ${MOUNT}/${tfile_tl}_slink" true
8456         75a_op_test "rm ${MOUNT}/${tfile_tenant}_slink" true
8457         75a_op_test "rm ${MOUNT}/${tfile_tr}_slink" true
8458         75a_op_test "rm ${MOUNT}/${tfile_tenant}_hlink" true
8459         # 5. fallocate (only on ldiskfs, zfs does not support pre-allocation)
8460         if [[ "$ost1_FSTYPE" == "ldiskfs" ]]; then
8461                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_trusted" false
8462                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tl" false
8463                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tenant" true
8464                 75a_op_test "fallocate -l 1M ${MOUNT}/$tfile_tr" false
8465         fi
8466         # 6. truncate
8467         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_trusted 524288" false
8468         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tl 524288" false
8469         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tenant 524288" true
8470         75a_op_test "$TRUNCATE ${MOUNT}/$tfile_tr 524288" false
8471         # 7. rename files (and back)
8472         75a_op_test "mv ${MOUNT}/$tfile_trusted \
8473                 ${MOUNT}/${tfile_trusted}_" false
8474         75a_op_test "mv ${MOUNT}/$tfile_tl ${MOUNT}/${tfile_tl}_" false
8475         75a_op_test "mv ${MOUNT}/$tfile_tenant ${MOUNT}/${tfile_tenant}_" true
8476         75a_op_test "mv ${MOUNT}/${tfile_tenant}_ ${MOUNT}/$tfile_tenant" true
8477         75a_op_test "mv ${MOUNT}/$tfile_tr ${MOUNT}/${tfile_tr}_" false
8478         # 8. trigger setattr operation with "touch" (timestamp update)
8479         75a_op_test "touch ${MOUNT}/$tfile_trusted" false
8480         75a_op_test "touch ${MOUNT}/$tfile_tl" false
8481         75a_op_test "touch ${MOUNT}/$tfile_tenant" true
8482         75a_op_test "touch ${MOUNT}/$tfile_tr" false
8483         # 9. xattr, set and get
8484         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_trusted" false
8485         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tl" false
8486         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tenant" true
8487         75a_op_test "setfattr -n user.abc -v def ${MOUNT}/$tfile_tr" false
8488         75a_getxattr_test ${MOUNT}/$tfile_trusted false
8489         75a_getxattr_test ${MOUNT}/$tfile_tl false
8490         75a_getxattr_test ${MOUNT}/$tfile_tenant true
8491         75a_getxattr_test ${MOUNT}/$tfile_tr false
8492         # 10. remove create files from tenant dirs
8493         75a_op_test "rm ${MOUNT}/${tdir_tenant}/$tf" true
8494         # 11. attempt to remove all tenant dirs
8495         75a_op_test "rmdir ${MOUNT}/$tdir_trusted" false
8496         75a_op_test "rmdir ${MOUNT}/$tdir_tl" false
8497         75a_op_test "rmdir ${MOUNT}/$tdir_tenant" true
8498         75a_op_test "rmdir ${MOUNT}/$tdir_tr" false
8499         # 12. attempt to remove remaining tenant files from root
8500         75a_op_test "rm ${MOUNT}/$tfile_trusted" false
8501         75a_op_test "rm ${MOUNT}/$tfile_tl" false
8502         75a_op_test "rm ${MOUNT}/$tfile_tenant" true
8503         75a_op_test "rm ${MOUNT}/$tfile_tr" false
8504         # 13. Data on MDT cases
8505         if [[ "$mds1_FSTYPE" == "ldiskfs" ]]; then
8506                 75a_op_test "fallocate -l 1M ${MOUNT}/${tfile_trusted}_dom" \
8507                         false
8508         fi
8509         75a_op_test "$TRUNCATE ${MOUNT}/${tfile_trusted}_dom 524288" false
8510         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/${tfile_trusted}_dom" false
8511         75a_read_test "cat ${MOUNT}/${tfile_trusted}_dom" false
8512         75a_op_test "rm ${MOUNT}/${tfile_trusted}_dom" false
8513
8514         if [[ "$mds1_FSTYPE" == "ldiskfs" ]]; then
8515                 75a_op_test "fallocate -l 1M ${MOUNT}/${tfile_tenant}_dom" true
8516         fi
8517         75a_op_test "$TRUNCATE ${MOUNT}/${tfile_tenant}_dom 524288" true
8518         75a_op_test "cp ${MOUNT}/$tf_write ${MOUNT}/${tfile_tenant}_dom" true
8519         75a_drop_tenant_cache
8520         75a_read_test "cat ${MOUNT}/${tfile_tenant}_dom" true
8521         75a_op_test "rm ${MOUNT}/${tfile_tenant}_dom" true
8522
8523         report_client_view_75a
8524 }
8525 run_test 75a "test resource fs IDs against nodemap offset"
8526
8527 cleanup_76() {
8528         # unmount client
8529         if is_mounted $MOUNT; then
8530                 umount_client $MOUNT || error "umount $MOUNT failed"
8531         fi
8532
8533         # reset and deactivate nodemaps, remount client
8534         cleanup_local_client_nodemap
8535
8536         # remount client on $MOUNT_2
8537         if [ "$MOUNT_2" ]; then
8538                 mount_client $MOUNT2 ${MOUNT_OPTS} || error "remount failed"
8539         fi
8540         wait_ssk
8541 }
8542
8543 test_76() {
8544         local user=$(getent passwd $RUNAS_ID | cut -d: -f1)
8545         local grp=grptest76
8546         local grpid=5000
8547         local nm=c0
8548
8549         (( $MDS1_VERSION >= $(version_code 2.16.53) )) ||
8550                 skip "need MDS >= 2.16.53 for suppgroup mapping"
8551
8552         do_nodes $(comma_list $(all_mdts_nodes)) \
8553                 $LCTL set_param mdt.*.identity_upcall=NONE
8554
8555         # create a specific group and add it as a supplementary group for $USER0
8556         groupadd -g $grpid $grp
8557         stack_trap "groupdel $grp" EXIT
8558         usermod -aG $grp $user
8559         stack_trap "gpasswd -d $user $grp" EXIT
8560
8561         stack_trap cleanup_76 EXIT
8562
8563         # unmount client completely
8564         umount_client $MOUNT || error "umount $MOUNT failed"
8565         if is_mounted $MOUNT2; then
8566                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
8567         fi
8568
8569         # setup nodemap with offset
8570         setup_local_client_nodemap $nm 1 1
8571         do_facet mgs $LCTL nodemap_add_offset --name $nm \
8572                 --offset 100000 --limit 200000 ||
8573                         error "nodemap_add_offset failed"
8574         wait_nm_sync $nm offset
8575
8576         # remount client to take nodemap into account
8577         zconf_mount_clients $HOSTNAME $MOUNT $MOUNT_OPTS ||
8578                 error "remount failed"
8579         wait_ssk
8580
8581         # Create directory from client part of the nodemap, as root,
8582         # and set its group membership to $grpid.
8583         # This is going to be mapped on server side.
8584         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
8585         chgrp -v $grp $DIR/$tdir || error "chgrp $DIR/$tdir failed"
8586         chmod -v 0770 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8587         ls -ld $DIR/$tdir
8588         cancel_lru_locks
8589
8590         # access as $USER0, should work because it has $grpid as a supp group
8591         # and it is properly mapped on server side
8592         $RUNAS -G$grpid ls -l $DIR/$tdir ||
8593                 error "ls -l $DIR/$tdir as $user failed"
8594         $RUNAS -G$grpid touch $DIR/$tdir/fileA ||
8595                 error "touch $DIR/$tdir/fileA as $user failed"
8596 }
8597 run_test 76 "suppgroups and gid mapping"
8598
8599 test_77() {
8600         local squash=100
8601         local nm=c0
8602
8603         (( $MDS1_VERSION >= $(version_code 2.16.54) )) ||
8604                 skip "Need MDS version >= 2.16.54 for proper root offsetting"
8605
8606         do_nodes $(comma_list $(all_mdts_nodes)) \
8607                 $LCTL set_param mdt.*.identity_upcall=NONE
8608
8609         stack_trap cleanup_local_client_nodemap_with_mounts EXIT
8610
8611         # create dir before nodemap create
8612         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
8613
8614         # unmount client completely
8615         umount_client $MOUNT || error "umount $MOUNT failed"
8616         if is_mounted $MOUNT2; then
8617                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
8618         fi
8619
8620         # setup nodemap with offset
8621         setup_local_client_nodemap $nm 1 0
8622         do_facet mgs $LCTL nodemap_modify --name $nm \
8623                 --property squash_uid --value $squash ||
8624                 error "Setting squash_uid=$squash on $nm failed"
8625         do_facet mgs $LCTL nodemap_modify --name $nm \
8626                 --property squash_gid --value $squash ||
8627                 error "Setting squash_gid=$squash on $nm failed"
8628         do_facet mgs $LCTL nodemap_modify --name $nm \
8629                 --property squash_projid --value $squash ||
8630                 error "Setting squash_projid=$squash on $nm failed"
8631         do_facet mgs $LCTL nodemap_add_offset --name $nm \
8632                 --offset 100000 --limit 200000 ||
8633                         error "nodemap_add_offset failed"
8634         wait_nm_sync $nm offset
8635
8636         # remount client to take nodemap into account
8637         zconf_mount_clients $HOSTNAME $MOUNT $MOUNT_OPTS ||
8638                 error "remount failed"
8639         wait_ssk
8640
8641         # create a file as root...
8642         touch $DIR/$tdir/fileA
8643         # the owner:group ids read back should be 0:0
8644         ls -ln $DIR/$tdir/fileA
8645         [[ $(stat -c "%u:%g" $DIR/$tdir/fileA) == "0:0" ]] ||
8646                 error "bad owner/group for root file"
8647 }
8648 run_test 77 "root offsetting"
8649
8650 log "cleanup: ======================================================"
8651
8652 sec_unsetup() {
8653         for ((num = 1; num <= $MDSCOUNT; num++)); do
8654                 if [[ "${identity_old[$num]}" == 1 ]]; then
8655                         switch_identity $num false || identity_old[$num]=$?
8656                 fi
8657         done
8658
8659         $RUNAS_CMD -u $ID0 ls $DIR
8660         $RUNAS_CMD -u $ID1 ls $DIR
8661 }
8662 sec_unsetup
8663
8664 complete_test $SECONDS
8665 check_and_cleanup_lustre
8666 exit_status