Whamcloud - gitweb
9ce8b47b61df3e5b552aa5c99c63e5a24923d335
[fs/lustre-release.git] / lustre / tests / sanity-pcc.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 # exit on error
7 set -e
8 set +o monitor
9
10 SRCDIR=$(dirname $0)
11 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin
12
13 ONLY=${ONLY:-"$*"}
14 ALWAYS_EXCEPT="$SANITY_PCC_EXCEPT "
15 # bug number for skipped test:
16 ALWAYS_EXCEPT+=""
17 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
18
19 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
20 HSMTOOL_ARCHIVE_FORMAT=v1
21
22 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
23
24 . $LUSTRE/tests/test-framework.sh
25 init_test_env $@
26 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
27 init_logging
28
29 MULTIOP=${MULTIOP:-multiop}
30 OPENFILE=${OPENFILE:-openfile}
31 MOUNT_2=${MOUNT_2:-"yes"}
32 FAIL_ON_ERROR=false
33
34 # script only handles up to 10 MDTs (because of MDT_PREFIX)
35 [ $MDSCOUNT -gt 9 ] &&
36         error "script cannot handle more than 9 MDTs, please fix" && exit
37
38 check_and_setup_lustre
39
40 if [[ "$MDS1_VERSION" -lt $(version_code 2.12.52) ]]; then
41         skip "Need MDS version at least 2.12.52"
42 fi
43
44 # $RUNAS_ID may get set incorrectly somewhere else
45 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
46         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!" && exit
47 fi
48 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
49 if getent group nobody; then
50         GROUP=nobody
51 elif getent group nogroup; then
52         GROUP=nogroup
53 else
54         error "No generic nobody group"
55 fi
56
57 build_test_filter
58
59 # if there is no CLIENT1 defined, some tests can be ran on localhost
60 CLIENT1=${CLIENT1:-$HOSTNAME}
61 # if CLIENT2 doesn't exist then use CLIENT1 instead
62 # All tests should use CLIENT2 with MOUNT2 only therefore it will work if
63 # $CLIENT2 == CLIENT1
64 # Exception is the test which need two separate nodes
65 CLIENT2=${CLIENT2:-$CLIENT1}
66
67 check_file_size()
68 {
69         local client="$1"
70         local fpath="$2"
71         local expected_size="$3"
72
73         size=$(do_facet $client stat "--printf=%s" $fpath)
74         [[ $size == "$expected_size" ]] || error \
75                 "expected $fpath size: $expected_size got: $size"
76 }
77
78 check_lpcc_sizes()
79 {
80         local client="$1"
81         local lpcc_fpath="$2"
82         local lustre_fpath="$3"
83         local expected_size="$4"
84
85         check_file_size $client $lpcc_fpath $expected_size
86         check_file_size $client $lustre_fpath $expected_size
87 }
88
89 check_file_data()
90 {
91         local client="$1"
92         local path="$2"
93         local expected_data="$3"
94         local pid=$4
95
96         # if $pid is set, then run command within namespace for that process
97         path_data=$(do_facet $client ${pid:+nsenter -t $pid -U -m} cat $path)
98         [[ "x$path_data" == "x$expected_data" ]] ||
99                 error "expected $path: $expected_data, got: $path_data"
100 }
101
102 check_lpcc_data()
103 {
104         local client="$1"
105         local lpcc_fpath="$2"
106         local lustre_fpath="$3"
107         local expected_data="$4"
108
109         check_file_data  "$client" "$lpcc_fpath" "$expected_data"
110         check_file_data  "$client" "$lustre_fpath" "$expected_data"
111 }
112
113 lpcc_fid2path()
114 {
115         local hsm_root="$1"
116         local lustre_path="$2"
117         local fid=$(path2fid $lustre_path)
118
119         local seq=$(echo $fid | awk -F ':' '{print $1}')
120         local oid=$(echo $fid | awk -F ':' '{print $2}')
121         local ver=$(echo $fid | awk -F ':' '{print $3}')
122
123         case "$HSMTOOL_ARCHIVE_FORMAT" in
124                 v1)
125                         printf "%s/%04x/%04x/%04x/%04x/%04x/%04x/%s" \
126                                 $hsm_root $((oid & 0xFFFF)) \
127                                 $((oid >> 16 & 0xFFFF)) \
128                                 $((seq & 0xFFFF)) \
129                                 $((seq >> 16 & 0xFFFF)) \
130                                 $((seq >> 32 & 0xFFFF)) \
131                                 $((seq >> 48 & 0xFFFF)) $fid
132                         ;;
133                 v2)
134                         printf "%s/%04x/%s" $hsm_root $(((oid ^ seq) & 0xFFFF)) $fid
135                         ;;
136         esac
137 }
138
139 check_lpcc_state()
140 {
141         local lustre_path="$1"
142         local expected_state="$2"
143         local facet=${3:-$SINGLEAGT}
144         local myRUNAS="$4"
145         local state=$(do_facet $facet $myRUNAS $LFS pcc state $lustre_path |
146                         awk -F 'type: ' '{print $2}' | awk -F ',' '{print $1}')
147
148         [[ "x$state" == "x$expected_state" ]] || error \
149                 "$lustre_path expected pcc state: $expected_state, but got: $state"
150 }
151
152 # initiate variables
153 init_agt_vars
154
155 # populate MDT device array
156 get_mdt_devices
157
158 # cleanup from previous bad setup
159 kill_copytools
160
161 # for recovery tests, coordinator needs to be started at mount
162 # so force it
163 # the lustre conf must be without hsm on (like for sanity.sh)
164 echo "Set HSM on and start"
165 cdt_set_mount_state enabled
166 cdt_check_state enabled
167
168 echo "Set sanity-hsm HSM policy"
169 cdt_set_sanity_policy
170
171 # finished requests are quickly removed from list
172 set_hsm_param grace_delay 10
173
174 cleanup_pcc_mapping() {
175         local facet=${1:-$SINGLEAGT}
176
177         do_facet $facet $LCTL pcc clear $MOUNT
178 }
179
180 setup_pcc_mapping() {
181         local facet=${1:-$SINGLEAGT}
182         local hsm_root=${hsm_root:-$(hsm_root "$facet")}
183         local param="$2"
184
185         [ -z "$param" ] && param="projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
186         stack_trap "cleanup_pcc_mapping $facet" EXIT
187         do_facet $facet $LCTL pcc add $MOUNT $hsm_root -p $param
188 }
189
190 setup_loopdev() {
191         local facet=$1
192         local file=$2
193         local mntpt=$3
194         local size=${4:-50}
195
196         do_facet $facet mkdir -p $mntpt || error "mkdir -p $hsm_root failed"
197         stack_trap "do_facet $facet rm -rf $mntpt" EXIT
198         do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
199         stack_trap "do_facet $facet rm -f $file" EXIT
200         do_facet $facet mkfs.ext4 $file ||
201                 error "mkfs.ext4 $file failed"
202         do_facet $facet file $file
203         do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
204                 error "mount -o loop,usrquota,grpquota $file $mntpt failed"
205         stack_trap "do_facet $facet $UMOUNT $mntpt" EXIT
206 }
207
208 lpcc_rw_test() {
209         local restore="$1"
210         local project="$2"
211         local project_id=100
212         local agt_facet=$SINGLEAGT
213         local loopfile="$TMP/$tfile"
214         local mntpt="/mnt/pcc.$tdir"
215         local hsm_root="$mntpt/$tdir"
216         local file=$DIR/$tdir/$tfile
217         local -a state
218         local -a lpcc_path
219         local -a size
220
221         $project && enable_project_quota
222
223         do_facet $SINGLEAGT rm -rf $hsm_root
224         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
225         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
226
227         is_project_quota_supported || project=false
228
229         do_facet $SINGLEAGT $LFS mkdir -i0 -c1 $DIR/$tdir
230         setup_pcc_mapping
231         $project && lfs project -sp $project_id $DIR/$tdir
232
233         do_facet $SINGLEAGT "echo -n attach_origin > $file"
234         if ! $project; then
235                 check_lpcc_state $file "none"
236                 do_facet $SINGLEAGT $LFS pcc attach -i \
237                         $HSM_ARCHIVE_NUMBER $file ||
238                         error "pcc attach $file failed"
239         fi
240
241         check_lpcc_state $file "readwrite"
242         # HSM released exists archived status
243         check_hsm_flags $file "0x0000000d"
244         lpcc_path=$(lpcc_fid2path $hsm_root $file)
245         check_lpcc_data $SINGLEAGT $lpcc_path $file "attach_origin"
246
247         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=7654321 count=1
248         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 7654321
249
250         do_facet $SINGLEAGT $TRUNCATE $file 1234567 ||
251                 error "truncate failed"
252         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 1234567
253         check_lpcc_state $file "readwrite"
254
255         do_facet $SINGLEAGT "echo -n file_data > $file"
256         check_lpcc_state $file "readwrite"
257         # HSM released exists archived status
258         check_hsm_flags $file "0x0000000d"
259         check_lpcc_data $SINGLEAGT $lpcc_path $file "file_data"
260
261         echo "Restore testing..."
262         if [ $CLIENTCOUNT -lt 2 -o $restore ]; then
263                 $LFS hsm_restore $file || error \
264                         "failed to restore $file"
265                 wait_request_state $(path2fid $file) RESTORE SUCCEED
266         else
267                 path_data=$(do_node $CLIENT2 cat $file)
268                 [[ "x$path_data" == "xfile_data" ]] || error \
269                         "expected file_data, got: $path_data"
270         fi
271
272         check_lpcc_state $file "none"
273         # HSM exists archived status
274         check_hsm_flags $file "0x00000009"
275
276         echo -n "new_data" > $file
277         check_lpcc_state $file "none"
278         # HSM exists dirty archived status
279         check_hsm_flags $file "0x0000000b"
280         check_file_data $SINGLEAGT $file "new_data"
281
282         echo "Attach and detach testing"
283         rm -f $file
284         do_facet $SINGLEAGT "echo -n new_data2 > $file"
285         if ! $project; then
286                 check_lpcc_state $file "none"
287                 do_facet $SINGLEAGT $LFS pcc attach -i \
288                         $HSM_ARCHIVE_NUMBER $file ||
289                         error "PCC attach $file failed"
290         fi
291         check_lpcc_state $file "readwrite"
292         # HSM released exists archived status
293         check_hsm_flags $file "0x0000000d"
294         do_facet $SINGLEAGT "echo -n attach_detach > $file"
295         echo "Start to detach the $file"
296         do_facet $SINGLEAGT $LFS pcc detach $file ||
297                 error "PCC detach $file failed"
298         wait_request_state $(path2fid $file) REMOVE SUCCEED
299
300         check_lpcc_state $file "none"
301         # The file is removed from PCC
302         check_hsm_flags $file "0x00000000"
303         check_file_data $SINGLEAGT $file "attach_detach"
304 }
305
306 test_1a() {
307         lpcc_rw_test true false
308 }
309 run_test 1a "Test manual lfs pcc attach with manual HSM restore"
310
311 test_1b() {
312         lpcc_rw_test false false
313 }
314 run_test 1b "Test manual lfs pcc attach with restore on remote access"
315
316 test_1c() {
317         lpcc_rw_test true true
318 }
319 run_test 1c "Test automated attach using Project ID with manual HSM restore"
320
321 test_1d() {
322         lpcc_rw_test false true
323 }
324 run_test 1d "Test Project ID with remote access"
325
326 test_1e() {
327         local file=$DIR/$tdir/$tfile
328         local hsm_root=$(hsm_root)
329         local -a lpcc_path
330
331         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
332         setup_pcc_mapping $SINGLEAGT \
333                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
334         $LCTL pcc list $MOUNT
335         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
336         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
337
338         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
339                 error "failed to dd write to $file"
340         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
341                 $file || error "failed to attach file $file"
342         check_lpcc_state $file "readwrite"
343         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
344                 error "failed to dd read from $file"
345         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
346                 error "failed to truncate $file"
347         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
348                 error "failed to truncate $file"
349         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
350                 error "failed to dd write to $file"
351         check_lpcc_state $file "readwrite"
352
353         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
354                 error "failed to detach file $file"
355         check_lpcc_state $file "none"
356
357         # non-root user is forbidden to access PCC file directly
358         lpcc_path=$(lpcc_fid2path $hsm_root $file)
359         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
360                 error "non-root user can touch access PCC file $lpcc_path"
361         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
362                 count=1 && error "non-root user can read PCC file $lpcc_path"
363         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
364                 count=1 && error "non-root user can write PCC file $lpcc_path"
365
366         local perm=$(do_facet $SINGLEAGT stat -c %a $lpcc_path)
367
368         [[ $perm == "0" ]] || error "PCC file permission ($perm) is not zero"
369
370         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
371                 $file || error "failed to attach file $file"
372         check_lpcc_state $file "readwrite"
373
374         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
375                 error "failed to detach file $file"
376         check_lpcc_state $file "none"
377         wait_request_state $(path2fid $file) REMOVE SUCCEED
378 }
379 run_test 1e "Test RW-PCC with non-root user"
380
381 test_1f() {
382         local project_id=100
383         local agt_facet=$SINGLEAGT
384         local loopfile="$TMP/$tfile"
385         local mntpt="/mnt/pcc.$tdir"
386         local hsm_root="$mntpt/$tdir"
387         local file=$DIR/$tdir/$tfile
388
389         ! is_project_quota_supported &&
390                 skip "project quota is not supported"
391
392         enable_project_quota
393         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
394         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
395         setup_pcc_mapping $SINGLEAGT \
396                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=0\ stat_attach=0"
397
398         do_facet $SINGLEAGT $LFS mkdir -i0 -c1 $DIR/$tdir
399         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
400         $LFS project -sp $project_id $DIR/$tdir ||
401                 error "failed to set project for $DIR/$tdir"
402
403         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
404                 error "failed to dd write to $file"
405
406         check_lpcc_state $file "readwrite"
407         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
408                 error "failed to dd read from $file"
409         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
410                 error "failed to truncate $file"
411         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
412                 error "failed to truncate $file"
413         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=256 count=1 ||
414                 error "failed to dd write from $file"
415         check_lpcc_state $file "readwrite"
416
417         # non-root user is forbidden to access PCC file directly
418         lpcc_path=$(lpcc_fid2path $hsm_root $file)
419         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
420                 error "non-root user can touch access PCC file $lpcc_path"
421         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
422                 count=1 && error "non-root user can read PCC file $lpcc_path"
423         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
424                 count=1 && error "non-root user can write PCC file $lpcc_path"
425
426         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
427                 error "failed to detach file $file"
428         wait_request_state $(path2fid $file) REMOVE SUCCEED
429         check_lpcc_state $file "none"
430 }
431 run_test 1f "Test auto RW-PCC cache with non-root user"
432
433 test_1g() {
434         local loopfile="$TMP/$tfile"
435         local mntpt="/mnt/pcc.$tdir"
436         local hsm_root="$mntpt/$tdir"
437         local file=$DIR/$tfile
438
439         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
440         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
441         setup_pcc_mapping
442
443         dd if=/dev/zero of=$file bs=1024 count=1 ||
444                 error "failed to dd write to $file"
445         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
446                 error "non-root user can dd write to $file"
447         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
448                 error "failed to attach file $file"
449         check_lpcc_state $file "readwrite"
450         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
451                 error "non-root user can dd write to $file"
452         chmod 777 $file || error "chmod 777 $file failed"
453         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
454                 error "non-root user cannot write $file with permission (777)"
455
456         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file &&
457                 error "non-root user or non owner can detach $file"
458         chown $RUNAS_ID $file || error "chown $RUNAS_ID $file failed"
459         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
460                 error "failed to detach file $file"
461         check_lpcc_state $file "none"
462         wait_request_state $(path2fid $file) REMOVE SUCCEED
463         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
464                 error "non-root user cannot read to $file with permisson (777)"
465 }
466 run_test 1g "General permission test for RW-PCC"
467
468 #
469 # When a process created a LPCC file and holding the open,
470 # another process on the same client should be able to open the file.
471 #
472 test_2a() {
473         local project_id=100
474         local agt_facet=$SINGLEAGT
475         local loopfile="$TMP/$tfile"
476         local mntpt="/mnt/pcc.$tdir"
477         local hsm_root="$mntpt/$tdir"
478         local agt_host=$(facet_active_host $SINGLEAGT)
479
480         ! is_project_quota_supported &&
481                 skip "project quota is not supported" && return
482
483         enable_project_quota
484         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
485         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
486         setup_pcc_mapping
487         file=$DIR/$tdir/multiop
488         $LFS mkdir -i -1 -c $MDSCOUNT $DIR/$tdir
489         rm -f $file
490
491         do_facet $SINGLEAGT $LFS project -sp $project_id $DIR/$tdir ||
492                 error "failed to set project quota"
493         rmultiop_start $agt_host $file O_c || error "open $file failed"
494         # HSM released exists archived status
495         check_hsm_flags $file "0x0000000d"
496         do_facet $SINGLEAGT "echo -n multiopen_data > $file" ||
497                 error "failed to echo multiopen_data to $file"
498
499         lpcc_path=$(lpcc_fid2path $hsm_root $file)
500         do_facet $SINGLEAGT ls -l $lpcc_path ||
501                 error "failed to ls $lpcc_path"
502         check_lpcc_data $SINGLEAGT $lpcc_path $file "multiopen_data"
503         # HSM released exists archived status
504         check_hsm_flags $file "0x0000000d"
505
506         do_facet $SINGLEAGT $LFS pcc detach $file ||
507                 error "failed to detach $file"
508         rmultiop_stop $agt_host || error "close $file failed"
509 }
510 run_test 2a "Test multi open when creating"
511
512 get_remote_client() {
513         current_id=$(do_facet $SINGLEAGT hostname)
514         for client in ${CLIENTS//,/ }
515         do
516                 r_id=$(do_node $client hostname)
517                 if [ $r_id != $current_id ]; then
518                         echo $client
519                         return
520                 fi
521         done
522 }
523
524 #
525 # When a process created a LPCC file and holding the open, another
526 # process on the different client should be able to open the file
527 # and perform IO on the file.
528 #
529 test_2b() {
530         local agt_facet=$SINGLEAGT
531         local agt_host=$(facet_active_host $SINGLEAGT)
532         local loopfile="$TMP/$tfile"
533         local mntpt="/mnt/pcc.$tdir"
534         local hsm_root="$mntpt/$tdir"
535
536         needclients 2 || return 0
537
538         remote_client=$(get_remote_client)
539
540         enable_project_quota
541         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
542         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
543         setup_pcc_mapping
544         file=$DIR/$tdir/multiop
545         mkdir -p $DIR/$tdir
546         rm -f $file
547
548         do_facet $SINGLEAGT "echo -n file_data > $file"
549         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
550                 $file || error "PCC attach $file failed"
551         check_lpcc_state $file "readwrite"
552
553         rmultiop_start $agt_host $file O_c || error "open $file failed"
554
555         do_node $remote_client "echo -n multiopen_data > $file"
556
557         # PCC cached file should be automatically detached
558         check_lpcc_state $file "none"
559
560         check_file_data $SINGLEAGT $file "multiopen_data"
561         rmultiop_stop $agt_host || error "close $file failed"
562         check_file_data $SINGLEAGT $file "multiopen_data"
563
564         do_node $remote_client cat $file || error \
565                 "cat $file on remote client failed"
566         do_node $remote_client echo -n "multiopen_data" > $file \
567                 || error "write $file on remote client failed"
568 }
569 run_test 2b "Test multi remote open when creating"
570
571 test_2c() {
572         local agt_host=$(facet_active_host $SINGLEAGT)
573         local loopfile="$TMP/$tfile"
574         local mntpt="/mnt/pcc.$tdir"
575         local hsm_root="$mntpt/$tdir"
576         local file=$DIR/$tdir/$tfile
577         local file2=$DIR2/$tdir/$tfile
578
579         enable_project_quota
580         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
581         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
582         setup_pcc_mapping
583         mkdir -p $DIR/$tdir
584         rm -f $file
585
586         do_facet $SINGLEAGT "echo -n file_data > $file"
587         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
588                 $file || error "PCC attach $file failed"
589         check_lpcc_state $file "readwrite"
590
591         rmultiop_start $agt_host $file O_c || error "open $file failed"
592
593         echo -n multiopen_data > $file2
594
595         # PCC cached file should be automatically detached
596         check_lpcc_state $file "none"
597
598         check_file_data $SINGLEAGT $file "multiopen_data"
599         rmultiop_stop $agt_host || error "close $file failed"
600         check_file_data $SINGLEAGT $file "multiopen_data"
601
602         cat $file2 || error "cat $file on mount $MOUNT2 failed"
603         echo -n "multiopen_data" > $file2 ||
604                 error "write $file on mount $MOUNT2 failed"
605 }
606 run_test 2c "Test multi open on different mount points when creating"
607
608 test_3a() {
609         local file=$DIR/$tdir/$tfile
610
611         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
612         setup_pcc_mapping $SINGLEAGT \
613                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
614
615         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
616         dd if=/dev/zero of=$file bs=1024 count=1 ||
617                 error "failed to dd write to $file"
618
619         echo "Start to attach/detach the file: $file"
620         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
621                 error "failed to attach file $file"
622         check_lpcc_state $file "readwrite"
623         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
624                 error "failed to detach file $file"
625         check_lpcc_state $file "none"
626
627         echo "Repeat to attach/detach the same file: $file"
628         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
629                 error "failed to attach file $file"
630         check_lpcc_state $file "readwrite"
631         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
632                 error "failed to detach file $file"
633         check_lpcc_state $file "none"
634 }
635 run_test 3a "Repeat attach/detach operations"
636
637 test_3b() {
638         local n
639         local file=$DIR/$tdir/$tfile
640
641         needclients 3 || return 0
642
643         # Start all of the copytools and setup PCC
644         for n in $(seq $AGTCOUNT); do
645                 copytool setup -f agt$n -a $n -m $MOUNT
646                 setup_pcc_mapping agt$n "projid={100}\ rwid=$n\ auto_attach=0"
647         done
648
649         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
650         dd if=/dev/zero of=$file bs=1024 count=1 ||
651                 error "failed to dd write to $file"
652
653         echo "Start to attach/detach $file on $agt1_HOST"
654         do_facet agt1 $LFS pcc attach -i 1 $file ||
655                 error "failed to attach file $file"
656         check_lpcc_state $file "readwrite" agt1
657         do_facet agt1 $LFS pcc detach -k $file ||
658                 error "failed to detach file $file"
659         check_lpcc_state $file "none" agt1
660
661         echo "Repeat to attach/detach $file on $agt2_HOST"
662         do_facet agt2 $LFS pcc attach -i 2 $file ||
663                 error "failed to attach file $file"
664         check_lpcc_state $file "readwrite" agt2
665         do_facet agt2 $LFS pcc detach -k $file ||
666                 error "failed to detach file $file"
667         check_lpcc_state $file "none" agt2
668
669         echo "Try attach on two agents"
670         do_facet agt1 $LFS pcc attach -i 1 $file ||
671                 error "failed to attach file $file"
672         check_lpcc_state $file "readwrite" agt1
673         do_facet agt2 $LFS pcc attach -i 2 $file ||
674                 error "failed to attach file $file"
675         check_lpcc_state $file "readwrite" agt2
676         # The later attach PCC agent should succeed,
677         # the former agent should be detached automatically.
678         check_lpcc_state $file "none" agt1
679         do_facet agt2 $LFS pcc detach -k $file ||
680                 error "failed to detach file $file"
681         check_lpcc_state $file "none" agt2
682 }
683 run_test 3b "Repeat attach/detach operations on multiple clients"
684
685 test_4() {
686         local project_id=100
687         local loopfile="$TMP/$tfile"
688         local mntpt="/mnt/pcc.$tdir"
689         local hsm_root="$mntpt/$tdir"
690         local excepts="-e 6 -e 7 -e 8 -e 9"
691
692         ! is_project_quota_supported &&
693                 skip "project quota is not supported" && return
694
695         enable_project_quota
696         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
697         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
698         setup_pcc_mapping
699
700         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
701         lfs project -sp $project_id $DIR/$tdir ||
702                 error "lfs project -sp $project_id $DIR/$tdir failed"
703
704         # 1. mmap_sanity tst7 failed on the local ext4 filesystem.
705         #    It seems that Lustre filesystem does special process for tst 7.
706         # 2. There is a mmap problem for PCC when multiple clients read/write
707         #    on a shared mmapped file for mmap_sanity tst 6.
708         # 3. Current CentOS8 kernel does not strictly obey POSIX syntax for
709         #    mmap() within the maping but beyond current end of the underlying
710         #    files: It does not send SIGBUS signals to the process.
711         # 4. For negative file offset, sanity_mmap also failed on 48 bits
712         #    ldiksfs backend due to too large offset: "Value too large for
713         #    defined data type".
714         # mmap_sanity tst7/tst8/tst9 all failed on Lustre and local ext4.
715         # Thus, we exclude sanity tst6/tst7/tst8/tst9 from the PCC testing.
716         $LUSTRE/tests/mmap_sanity -d $DIR/$tdir -m $DIR2/$tdir $excepts ||
717                 error "mmap_sanity test failed"
718         sync; sleep 1; sync
719
720         # Revoke the layout lock, the PCC-cached file will be
721         # detached automatically.
722         do_facet $SINGLEAGT $LCTL \
723                 set_param ldlm.namespaces.*mdc*.lru_size=clear
724         rm -rf $DIR/$tdir || error "failed to remove $DIR/$tdir"
725 }
726 run_test 4 "Auto cache test for mmap"
727
728 test_5() {
729         local file=$DIR/$tfile
730         local loopfile="$TMP/$tfile"
731         local mntpt="/mnt/pcc.$tdir"
732         local hsm_root="$mntpt/$tdir"
733
734         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
735         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
736         setup_pcc_mapping
737
738         do_facet $SINGLEAGT "echo -n attach_mmap_data > $file" ||
739                 error "echo $file failed"
740
741         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
742                 error "failed to attach file $file"
743         check_lpcc_state $file "readwrite"
744
745         local content=$($MMAP_CAT $file)
746
747         [[ $content == "attach_mmap_data" ]] ||
748                 error "mmap cat data mismatch: $content"
749
750         $LFS hsm_restore $file || error "failed to restore $file"
751         wait_request_state $(path2fid $file) RESTORE SUCCEED
752         check_lpcc_state $file "none"
753
754         content=$($MMAP_CAT $file)
755         [[ $content == "attach_mmap_data" ]] ||
756                 error "mmap cat data mismatch: $content"
757 }
758 run_test 5 "Mmap & cat a RW-PCC cached file"
759
760 test_6() {
761         local loopfile="$TMP/$tfile"
762         local mntpt="/mnt/pcc.$tdir"
763         local hsm_root="$mntpt/$tdir"
764         local file=$DIR/$tfile
765         local content
766
767         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
768         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
769         setup_pcc_mapping
770
771         echo -n mmap_write_data > $file || error "echo write $file failed"
772         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
773                 error "failed to attach file $file"
774         check_lpcc_state $file "readwrite"
775
776         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
777                 error "could not mmap $file"
778         check_lpcc_state $file "readwrite"
779         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
780         # After mmap write via multiop, the first character of each page
781         # increases with 1.
782         [[ $content == "nmap_write_data" ]] ||
783                 error "mmap write data mismatch: $content"
784         check_lpcc_state $file "readwrite"
785
786         do_facet $SINGLEAGT $LFS pcc detach $file ||
787                 error "failed to detach file $file"
788         wait_request_state $(path2fid $file) REMOVE SUCCEED
789
790         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
791         [[ $content == "nmap_write_data" ]] ||
792                 error "mmap write data mismatch: $content"
793 }
794 run_test 6 "Test mmap write on RW-PCC "
795
796 test_7a() {
797         local loopfile="$TMP/$tfile"
798         local mntpt="/mnt/pcc.$tdir"
799         local hsm_root="$mntpt/$tdir"
800         local file=$DIR/$tfile
801         local content
802
803         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
804         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
805         setup_pcc_mapping
806
807         echo "QQQQQ" > $file
808         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
809                 error "failed to attach file $file"
810         check_lpcc_state $file "readwrite"
811         check_file_data $SINGLEAGT $file "QQQQQ"
812         # define OBD_FAIL_LLITE_PCC_DETACH_MKWRITE      0x1412
813         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1412
814         # HSM released exists archived status
815         check_hsm_flags $file "0x0000000d"
816
817         # multiop mmap write increase the first character of each page with 1
818         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
819                 error "mmap write $file failed"
820         check_lpcc_state $file "none"
821         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
822         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
823 }
824 run_test 7a "Fake file detached between fault() and page_mkwrite() for RW-PCC"
825
826 test_7b() {
827         local loopfile="$TMP/$tfile"
828         local mntpt="/mnt/pcc.$tdir"
829         local hsm_root="$mntpt/$tdir"
830         local file=$DIR/$tfile
831         local content
832         local pid
833
834         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
835         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
836         setup_pcc_mapping $SINGLEAGT \
837                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
838
839         echo "QQQQQ" > $file
840         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
841                 error "failed to attach file $file"
842         check_lpcc_state $file "readwrite"
843         check_file_data $SINGLEAGT $file "QQQQQ"
844         # define OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE       0x1413
845         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1413 fail_val=20
846         # HSM released exists archived status
847         check_hsm_flags $file "0x0000000d"
848
849         # multiop mmap write increases the first character of each page with 1
850         do_facet $SINGLEAGT $MULTIOP $file OSMWUc &
851         pid=$!
852
853         sleep 3
854         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
855                 error "failed to detach file $file"
856
857         wait $pid || error "multiop mmap write failed"
858         check_lpcc_state $file "none"
859         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
860         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
861 }
862 run_test 7b "Test the race with concurrent mkwrite and detach"
863
864 test_8() {
865         local loopfile="$TMP/$tfile"
866         local mntpt="/mnt/pcc.$tdir"
867         local hsm_root="$mntpt/$tdir"
868         local file=$DIR/$tfile
869
870         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
871         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
872         setup_pcc_mapping
873
874         echo "QQQQQ" > $file
875         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
876                 error "failed to attach file $file"
877         check_lpcc_state $file "readwrite"
878         check_file_data $SINGLEAGT $file "QQQQQ"
879
880         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
881         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
882         do_facet $SINGLEAGT "echo -n ENOSPC_write > $file"
883         # Above write will return -ENOSPC failure and retry the IO on normal
884         # IO path. It will restore the HSM released file.
885         check_lpcc_state $file "none"
886         check_file_data $SINGLEAGT $file "ENOSPC_write"
887 }
888 run_test 8 "Test fake -ENOSPC tolerance for RW-PCC"
889
890 test_9() {
891         local loopfile="$TMP/$tfile"
892         local mntpt="/mnt/pcc.9a"
893         local hsm_root="$mntpt/$tdir"
894         local file=$DIR/$tfile
895
896         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
897
898         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
899         setup_pcc_mapping
900         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
901
902         touch $file || error "touch $file failed"
903         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
904                 error "fail to attach $file"
905         check_lpcc_state $file "readwrite"
906         # write 60M data, it is larger than the capacity of PCC backend
907         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1M count=60 ||
908                 error "fail to dd write $file"
909         check_lpcc_state $file "none"
910         check_file_size $SINGLEAGT $file 62914560
911 }
912 run_test 9 "Test -ENOSPC tolerance on loop PCC device for RW-PCC"
913
914 test_usrgrp_quota() {
915         local loopfile="$TMP/$tfile"
916         local mntpt="/mnt/pcc.$tdir"
917         local hsm_root="$mntpt/$tdir"
918         local ug=$1
919         local id=$RUNAS_ID
920
921         [[ $ug == "g" ]] && id=$RUNAS_GID
922
923         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
924         do_facet $SINGLEAGT quotacheck -c$ug $mntpt ||
925                 error "quotacheck -c$ug $mntpt failed"
926         do_facet $SINGLEAGT quotaon -$ug $mntpt ||
927                 error "quotaon -$ug $mntpt failed"
928         do_facet $SINGLEAGT setquota -$ug $id 0 20480 0 0 $mntpt ||
929                 error "setquota -$ug $id on $mntpt failed"
930         do_facet $SINGLEAGT repquota -${ug}vs $mntpt
931
932         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
933         setup_pcc_mapping
934         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
935
936         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
937
938         local file1=$DIR/$tdir/${ug}quotaA
939         local file2=$DIR/$tdir/${ug}quotaB
940
941         dd if=/dev/zero of=$file1 bs=1M count=15 ||
942                 error "dd write $file1 failed"
943         dd if=/dev/zero of=$file2 bs=1M count=15 ||
944                 error "dd write $file2 failed"
945         chown $RUNAS_ID:$RUNAS_GID $file1 ||
946                 error "chown $RUNAS_ID:$RUNAS_GID $file1 failed"
947         chown $RUNAS_ID:$RUNAS_GID $file2 ||
948                 error "chown $RUNAS_ID:$RUNAS_GID $file2 failed"
949         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
950                 $file1 || error "attach $file1 failed"
951         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
952                 $file2 && error "attach $file2 should fail due to quota limit"
953         check_lpcc_state $file1 "readwrite"
954         check_lpcc_state $file2 "none"
955
956         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file1 bs=1M count=30 ||
957                 error "dd write $file1 failed"
958         # -EDQUOT error should be tolerated via fallback to normal Lustre path.
959         check_lpcc_state $file1 "none"
960         do_facet $SINGLEAGT $LFS pcc detach -k $file1 ||
961                 error "failed to detach file $file"
962         rm $file1 $file2
963 }
964
965 test_10a() {
966         test_usrgrp_quota "u"
967 }
968 run_test 10a "Test RW-PCC with user quota on loop PCC device"
969
970 test_10b() {
971         test_usrgrp_quota "g"
972 }
973 run_test 10b "Test RW-PCC with group quota on loop PCC device"
974
975 test_11() {
976         local loopfile="$TMP/$tfile"
977         local mntpt="/mnt/pcc.$tdir"
978         local hsm_root="$mntpt/$tdir"
979         local file=$DIR/$tfile
980         local -a lpcc_path
981         local lpcc_dir
982
983         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
984         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
985         setup_pcc_mapping
986
987         do_facet $SINGLEAGT "echo -n QQQQQ > $file"
988         lpcc_path=$(lpcc_fid2path $hsm_root $file)
989         lpcc_dir=$(dirname $lpcc_path)
990         echo "Lustre file: $file LPCC dir: $lpcc_dir"
991         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
992                 error "mkdir -p $lpcc_dir failed"
993         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
994                 error "failed to attach $file"
995         check_lpcc_state $file "readwrite"
996         check_file_data $SINGLEAGT $file "QQQQQ"
997         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
998                 error "failed to detach $file"
999         rm $file || error "rm $file failed"
1000
1001         # The parent directory of the PCC file is immutable
1002         do_facet $SINGLEAGT "echo -n immutable_dir > $file"
1003         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1004         lpcc_dir=$(dirname $lpcc_path)
1005         echo "Lustre file: $file LPCC dir: $lpcc_dir"
1006         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
1007                 error "mkdir -p $lpcc_dir failed"
1008         do_facet $SINGLEAGT chattr +i $lpcc_dir ||
1009                 error "chattr +i $lpcc_dir failed"
1010         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
1011                 error "attach $file with immutable directory should be failed"
1012         do_facet $SINGLEAGT chattr -i $lpcc_dir ||
1013                 error "chattr -i $lpcc_dir failed"
1014         rm $file || error "rm $file failed"
1015
1016         # The PCC file path is set to a directory
1017         do_facet $SINGLEAGT "echo -n pcc_file_path_is_dir > $file"
1018         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1019         do_facet $SINGLEAGT mkdir -p $lpcc_path ||
1020                 error "mkdir -p $lpcc_path failed"
1021         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
1022                 error "attach $file should fail as PCC path is a directory"
1023         rm $file || error "rm $file failed"
1024 }
1025 run_test 11 "Test attach fault injection with simulated PCC file path"
1026
1027 test_12() {
1028         local file=$DIR/$tfile
1029         local hsm_root=$(hsm_root)
1030         local -a lpcc_path
1031         local pid
1032
1033         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1034         setup_pcc_mapping $SINGLEAGT \
1035                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1036
1037         echo  -n race_rw_attach_hsmremove > $file
1038         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1039         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1040                 error "attach $file failed"
1041         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1042                 error "detach $file failed"
1043         # HSM released exists archived status
1044         check_hsm_flags $file "0x0000000d"
1045         # define OBD_FAIL_LLITE_PCC_ATTACH_PAUSE        0x1414
1046         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1414 fail_val=20
1047         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &
1048         pid=$!
1049         $LFS hsm_state $file
1050         sleep 3
1051         wait_request_state $(path2fid $file) RESTORE SUCCEED
1052         $LFS hsm_remove $file || error "hsm remove $file failed"
1053         wait $pid
1054         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1055                 error "RW-PCC cached file '$lpcc_path' should be removed"
1056
1057         return 0
1058 }
1059 run_test 12 "RW-PCC attach races with concurrent HSM remove"
1060
1061 test_rule_id() {
1062         local idstr="${1}id"
1063         local rule="${idstr}={$2}"
1064         local myRUNAS="$3"
1065         local file=$DIR/$tdir/$tfile
1066
1067         setup_pcc_mapping $SINGLEAGT \
1068                 "$rule\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1069         $LCTL pcc list $MOUNT
1070
1071         do_facet $SINGLEAGT $LFS mkdir -i 0 $DIR/$tdir
1072         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1073
1074         rm -f $file || error "rm $file failed"
1075         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1076                 error "failed to dd write to $file"
1077         check_lpcc_state $file "readwrite"
1078         do_facet $SINGLEAGT $myRUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
1079                 error "failed to dd read from $file"
1080         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 256 ||
1081                 error "failed to truncate $file"
1082         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 2048 ||
1083                 error "failed to truncate $file"
1084         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1085                 error "failed to dd write from $file"
1086         check_lpcc_state $file "readwrite"
1087
1088         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1089                 error "failed to detach file $file"
1090         wait_request_state $(path2fid $file) REMOVE SUCCEED
1091         check_lpcc_state $file "none"
1092
1093         cleanup_pcc_mapping
1094 }
1095
1096 test_13a() {
1097         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1098         test_rule_id "u" "500" "runas -u 500"
1099         test_rule_id "g" "500" "runas -u 500 -g 500"
1100 }
1101 run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
1102
1103 test_13b() {
1104         local file
1105
1106         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1107         setup_pcc_mapping $SINGLEAGT \
1108                 "fname={*.h5\ suffix.*\ Mid*dle}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1109         $LCTL pcc list $MOUNT
1110
1111         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1112         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1113
1114         file=$DIR/$tdir/prefix.h5
1115         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1116                 error "failed to dd write to $file"
1117         check_lpcc_state $file "readwrite"
1118         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1119                 error "failed to detach file $file"
1120         check_lpcc_state $file "none"
1121         rm $file || error "rm $file failed"
1122
1123         file=$DIR/$tdir/suffix.doc
1124         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1125                 error "failed to dd write to $file"
1126         check_lpcc_state $file "readwrite"
1127         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1128                 error "failed to detach file $file"
1129         check_lpcc_state $file "none"
1130         rm $file || error "rm $file failed"
1131
1132         file=$DIR/$tdir/MidPADdle
1133         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1134                 error "failed to dd write to $file"
1135         check_lpcc_state $file "readwrite"
1136         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1137                 error "failed to detach file $file"
1138         check_lpcc_state $file "none"
1139         rm $file || error "rm $file failed"
1140
1141         file=$DIR/$tdir/Midpad
1142         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1143                 error "failed to dd write to $file"
1144         check_lpcc_state $file "none"
1145         rm $file || error "rm $file failed"
1146 }
1147 run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
1148
1149 test_13c() {
1150         local file
1151         local myRUNAS
1152         local loopfile="$TMP/$tfile"
1153         local mntpt="/mnt/pcc.$tdir"
1154         local hsm_root="$mntpt/$tdir"
1155
1156         ! is_project_quota_supported &&
1157                 echo "Skip project quota is not supported" && return 0
1158
1159         enable_project_quota
1160         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1161         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1162         setup_pcc_mapping $SINGLEAGT \
1163                 "projid={100\ 200}\&fname={*.h5},uid={500}\&gid={1000}\ rwid=$HSM_ARCHIVE_NUMBER"
1164         $LCTL pcc list $MOUNT
1165         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1166         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1167
1168         mkdir -p $DIR/$tdir/proj || error "mkdir $DIR/$tdir/proj failed"
1169         mkdir -p $DIR/$tdir/proj2 || error "mkdir $DIR/$tdir/proj2 failed"
1170         $LFS project -sp 100 $DIR/$tdir/proj ||
1171                 error "failed to set project for $DIR/$tdir/proj"
1172         $LFS project -sp 200 $DIR/$tdir/proj2 ||
1173                 error "failed to set project for $DIR/$tdir/proj2"
1174
1175         file=$DIR/$tdir/proj/notcache
1176         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1177                 error "failed to dd write to $file"
1178         check_lpcc_state $file "none"
1179         rm $file || error "rm $file failed"
1180
1181         file=$DIR/$tdir/proj/autocache.h5
1182         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1183                 error "failed to dd write to $file"
1184         check_lpcc_state $file "readwrite"
1185         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1186                 error "failed to detach $file"
1187         rm $file || error "rm $file failed"
1188
1189         file=$DIR/$tdir/proj2/notcache
1190         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1191                 error "failed to dd write to $file"
1192         check_lpcc_state $file "none"
1193         rm $file || error "rm $file failed"
1194
1195         file=$DIR/$tdir/proj2/autocache.h5
1196         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1197                 error "failed to dd write to $file"
1198         check_lpcc_state $file "readwrite"
1199         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1200                 error "failed to detach $file"
1201         rm $file || error "rm $file failed"
1202
1203         file=$DIR/$tdir/ugidcache
1204         myRUNAS="runas -u 500 -g 1000"
1205         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1206                 error "failed to dd write to $file"
1207         check_lpcc_state $file "readwrite"
1208         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1209                 error "failed to detach $file"
1210         rm $file || error "rm $file failed"
1211 }
1212 run_test 13c "Check auto RW-PCC create caching for UID/GID/ProjID/fname rule"
1213
1214 test_14() {
1215         local file=$DIR/$tdir/$tfile
1216
1217         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1218         setup_pcc_mapping $SINGLEAGT \
1219                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1220
1221         mkdir -p $DIR/$tdir || error "mkdir -p $DIR/$tdir failed"
1222         do_facet $SINGLEAGT "echo -n autodetach_data > $file"
1223         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1224                 $file || error "PCC attach $file failed"
1225         check_lpcc_state $file "readwrite"
1226
1227         # Revoke the layout lock, the PCC-cached file will be
1228         # detached automatically.
1229         do_facet $SINGLEAGT $LCTL \
1230                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1231         check_file_data $SINGLEAGT $file "autodetach_data"
1232         check_lpcc_state $file "none"
1233 }
1234 run_test 14 "Revocation of the layout lock should detach the file automatically"
1235
1236 test_15() {
1237         local loopfile="$TMP/$tfile"
1238         local mntpt="/mnt/pcc.$tdir"
1239         local hsm_root="$mntpt/$tdir"
1240         local file=$DIR/$tdir/$tfile
1241
1242         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1243         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1244         setup_pcc_mapping
1245
1246         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1247         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
1248
1249         echo "Check open attach for non-root user"
1250         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1251                 error "failed to dd write to $file"
1252         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1253                 $file || error "failed to attach file $file"
1254         do_facet $SINGLEAGT $RUNAS $LFS pcc state $file
1255         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1256         # Revoke the layout lock, the PCC-cached file will be
1257         # detached automatically.
1258         do_facet $SINGLEAGT $LCTL \
1259                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1260         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1261         # Detach the file but keep the cache , as the file layout generation
1262         # is not changed, so the file is still valid cached in PCC, and can
1263         # be reused from PCC cache directly.
1264         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
1265                 error "PCC detach $file failed"
1266         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1267         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
1268                 error "PCC detach $file failed"
1269         rm $file || error "rm $file failed"
1270
1271         echo "check open attach for root user"
1272         do_facet $SINGLEAGT "echo -n autoattach_data > $file"
1273         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1274                 $file || error "PCC attach $file failed"
1275         check_lpcc_state $file "readwrite"
1276
1277         # Revoke the layout lock, the PCC-cached file will be
1278         # detached automatically.
1279         do_facet $SINGLEAGT $LCTL \
1280                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1281         check_file_data $SINGLEAGT $file "autoattach_data"
1282         check_lpcc_state $file "readwrite"
1283
1284         # Detach the file with -k option, as the file layout generation
1285         # is not changed, so the file is still valid cached in PCC,
1286         # and can be reused from PCC cache directly.
1287         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1288                 error "PCC detach $file failed"
1289         check_lpcc_state $file "readwrite"
1290         # HSM released exists archived status
1291         check_hsm_flags $file "0x0000000d"
1292         check_file_data $SINGLEAGT $file "autoattach_data"
1293
1294         # HSM restore the PCC cached file, the layout generation
1295         # was changed, so the file can not be auto attached.
1296         $LFS hsm_restore $file || error "failed to restore $file"
1297         wait_request_state $(path2fid $file) RESTORE SUCCEED
1298         check_lpcc_state $file "none"
1299         # HSM exists archived status
1300         check_hsm_flags $file "0x00000009"
1301
1302 }
1303 run_test 15 "Test auto attach at open when file is still valid cached"
1304
1305 test_16() {
1306         local loopfile="$TMP/$tfile"
1307         local mntpt="/mnt/pcc.$tdir"
1308         local hsm_root="$mntpt/$tdir"
1309         local file=$DIR/$tfile
1310         local -a lpcc_path
1311
1312         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1313         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1314         setup_pcc_mapping
1315
1316         do_facet $SINGLEAGT "echo -n detach_data > $file"
1317         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1318         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1319                 $file || error "PCC attach $file failed"
1320         check_lpcc_state $file "readwrite"
1321         # HSM released exists archived status
1322         check_hsm_flags $file "0x0000000d"
1323
1324         echo "Test for reusing valid PCC cache"
1325         # Valid PCC cache can be reused
1326         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1327                 error "PCC detach $file failed"
1328         check_lpcc_state $file "readwrite"
1329         # HSM released exists archived status
1330         check_hsm_flags $file "0x0000000d"
1331
1332         echo "Test for the default detach"
1333         # Permanent detach by default, it will remove the PCC copy
1334         do_facet $SINGLEAGT $LFS pcc detach $file ||
1335                 error "PCC detach $file failed"
1336         wait_request_state $(path2fid $file) REMOVE SUCCEED
1337         check_lpcc_state $file "none"
1338         # File is removed from PCC backend
1339         check_hsm_flags $file "0x00000000"
1340         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1341                 error "RW-PCC cached file '$lpcc_path' should be removed"
1342
1343         return 0
1344 }
1345 run_test 16 "Test detach with different options"
1346
1347 test_17() {
1348         local agt_host=$(facet_active_host $SINGLEAGT)
1349         local loopfile="$TMP/$tfile"
1350         local mntpt="/mnt/pcc.$tdir"
1351         local hsm_root="$mntpt/$tdir"
1352         local file=$DIR/$tfile
1353
1354         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1355         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1356         setup_pcc_mapping $SINGLEAGT \
1357                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=0\ stat_attach=0"
1358
1359         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1360
1361         do_facet $SINGLEAGT "echo -n layout_refresh_data > $file"
1362         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1363                 error "PCC attach $file failed"
1364         check_lpcc_state $file "readwrite"
1365
1366         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1367                 error "PCC detach $file failed"
1368         check_lpcc_state $file "none"
1369
1370         # Truncate should attach the file into PCC automatically
1371         # as the PCC copy is still valid.
1372         echo "Verify auto attach during IO for truncate"
1373         do_facet $SINGLEAGT $TRUNCATE $file 4 || error "truncate $file failed"
1374         check_lpcc_state $file "readwrite"
1375
1376         echo "Verify auto attach during IO for read/write"
1377         rmultiop_start $agt_host $file O_r || error "open $file failed"
1378         sleep 3
1379
1380         # Revoke the layout lock, the PCC-cached file will be
1381         # detached automatically.
1382         do_facet $SINGLEAGT $LCTL \
1383                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1384
1385         check_lpcc_state $file "none"
1386         rmultiop_stop $agt_host || error "close $file failed"
1387         sleep 3
1388         check_lpcc_state $file "readwrite"
1389
1390         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1391                 error "PCC detach $file failed"
1392         check_lpcc_state $file "none"
1393 }
1394 run_test 17 "Test auto attach for layout refresh"
1395
1396 test_18() {
1397         local agt_host=$(facet_active_host $SINGLEAGT)
1398         local loopfile="$TMP/$tfile"
1399         local mntpt="/mnt/pcc.$tdir"
1400         local hsm_root="$mntpt/$tdir"
1401         local file=$DIR/$tfile
1402         local oldmd5
1403         local newmd5
1404
1405         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1406         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1407         setup_pcc_mapping $SINGLEAGT \
1408                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
1409
1410         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1411         do_facet $SINGLEAGT dd if=/dev/urandom of=$file bs=1M count=4 ||
1412                 error "failed to write $file"
1413         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1414                 error "failed to attach $file"
1415         do_facet $SINGLEAGT $LFS pcc state $file
1416         check_lpcc_state $file "readwrite"
1417         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1418                 error "failed to detach $file"
1419         do_facet $SINGLEAGT $LFS pcc state $file
1420         $CHECKSTAT -s 4194304 $file
1421         dd if=/dev/zero of=$DIR2/$tfile seek=1k bs=1k count=1 ||
1422                 error "failed to write $DIR2/$tfile"
1423         oldmd5=$(md5sum $DIR2/$tfile | awk '{print $1}')
1424         $CHECKSTAT -s 1049600 $DIR2/$tfile || error "$DIR2/$tfile size wrong"
1425
1426         local lpcc_path=$(lpcc_fid2path $hsm_root $file)
1427
1428         do_facet $SINGLEAGT $LFS pcc state $file
1429         check_file_size $SINGLEAGT $lpcc_path 4194304
1430         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1431                 error "failed to attach $file"
1432         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 1049600
1433         newmd5=$(do_facet $SINGLEAGT md5sum $file | awk '{print $1}')
1434         [ "$oldmd5" == "$newmd5" ] || error "md5sum differ: $oldmd5 != $newmd5"
1435         do_facet $SINGLEAGT $LFS pcc detach $file ||
1436                 error "failed to detach $file"
1437 }
1438 run_test 18 "Verify size correctness after re-attach the file"
1439
1440 test_19() {
1441         local agt_host=$(facet_active_host $SINGLEAGT)
1442         local loopfile="$TMP/$tfile"
1443         local mntpt="/mnt/pcc.$tdir"
1444         local hsm_root="$mntpt/$tdir"
1445         local file=$DIR/$tfile
1446
1447         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1448         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1449         setup_pcc_mapping $SINGLEAGT \
1450                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1451
1452         do_facet $SINGLEAGT "echo -n QQQQQ > $file" || error "echo $file failed"
1453         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1454         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1455                 error "Failed to attach $file"
1456         check_lpcc_state $file "readwrite"
1457         check_lpcc_sizes $SINGLEAGT $file $lpcc_path 5
1458         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1459                 error "Failed to detach $file"
1460         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1461                 error "Failed to attach $file"
1462         check_lpcc_sizes $SINGLEAGT $file $lpcc_path 5
1463         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1464                 error "Failed to detach $file"
1465 }
1466 run_test 19 "Verify the file re-attach works as expected"
1467
1468 test_20() {
1469         local agt_host=$(facet_active_host $SINGLEAGT)
1470         local loopfile="$TMP/$tfile"
1471         local mntpt="/mnt/pcc.$tdir"
1472         local hsm_root="$mntpt/$tdir"
1473         local file=$DIR/$tfile
1474
1475         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1476         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1477         setup_pcc_mapping $SINGLEAGT \
1478                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
1479
1480         do_facet $SINGLEAGT "echo -n QQQQQ > $file" ||
1481                 error "echo $file failed"
1482         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
1483                 error "Failed to attach $file"
1484         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1485         check_lpcc_state $file "readwrite"
1486         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1487         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1488         check_lpcc_state $file "readwrite"
1489         do_facet $SINGLEAGT $LFS pcc detach $file ||
1490                 error "Failed to detach $file"
1491 }
1492 run_test 20 "Auto attach works after the inode was once evicted from cache"
1493
1494 #test 101: containers and PCC
1495 #LU-15170: Test mount namespaces with PCC
1496 #This tests the cases where the PCC mount is not present in the container by
1497 #creating a mount namespace without the PCC mount in it (this is probably the
1498 #standard config for most containers)
1499 test_101a() {
1500         local loopfile="$TMP/$tfile"
1501         local mntpt="/mnt/pcc.$tdir"
1502         local hsm_root="$mntpt/$tdir"
1503         local file=$DIR/$tdir/$tfile
1504
1505         # Some kernels such as RHEL7 default to 0 user namespaces
1506         local maxuserns=$(do_facet $SINGLEAGT cat /proc/sys/user/max_user_namespaces)
1507         do_facet $SINGLEAGT "echo 10 > /proc/sys/user/max_user_namespaces"
1508         stack_trap "do_facet $SINGLEAGT 'echo $maxuserns > /proc/sys/user/max_user_namespaces'"
1509
1510         echo "creating user namespace for $RUNAS_ID"
1511         # Create a mount and user namespace with this command, and leave the
1512         # process running so we can do the rest of our steps
1513         do_facet $SINGLEAGT $RUNAS unshare -Um sleep 600 &
1514         # Let the child start...
1515         sleep 2
1516         # Get the sleep PID so we can find its namespace and kill it later
1517         PID=$(do_facet $SINGLEAGT pgrep sleep)
1518         stack_trap "do_facet $SINGLEAGT kill -9 $PID" EXIT
1519         echo "Created NS: child (sleep) pid $PID"
1520         # Map 'RUNAS' to root in the namespace, so it has rights to do whatever
1521         # This is handled by '-r' in unshare in newer versions
1522         do_facet $SINGLEAGT $RUNAS newuidmap $PID 0 $RUNAS_ID 1 ||
1523                 error "could not map uid $RUNAS_ID to root in namespace"
1524         do_facet $SINGLEAGT $RUNAS newgidmap $PID 0 $RUNAS_GID 1 ||
1525                 error "could not map gid $RUNAS_GID to root in namespace"
1526
1527         # Create PCC after creating namespace; namespace will not have PCC
1528         # mount
1529         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1530
1531         # Create a temp file inside the PCC mount to verify mount namespace
1532         do_facet $SINGLEAGT touch $mntpt/$tfile.tmp
1533         stack_trap "do_facet $SINGLEAGT rm -f $mntpt/$tfile.tmp" EXIT
1534         echo "Check for temp file in PCC mount"
1535         do_facet $SINGLEAGT test -f $mntpt/$tfile.tmp ||
1536                 error "Should see $mntpt/$tfile.tmp"
1537         echo "Check for temp file in PCC mount from inside namespace"
1538         do_facet $SINGLEAGT nsenter -t $PID -U -m test -f $mntpt/$tfile.tmp &&
1539                 error "Should not see $mntpt/$tfile.tmp from namespace"
1540         rm -f $mntpt/$tfile.tmp
1541
1542         # Finish PCC setup
1543         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1544         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
1545
1546         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1547         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
1548
1549         echo "Verify open attach from inside mount namespace"
1550         do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
1551                 error "failed to dd write to $file"
1552         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach \
1553                 -i $HSM_ARCHIVE_NUMBER $file || error "cannot attach $file"
1554         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc state $file
1555
1556         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1557         # Revoke the layout lock, the PCC-cached file will be
1558         # detached automatically.
1559         do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
1560         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1561         # Detach the file but keep the cache, as the file layout generation
1562         # is not changed, so the file is still valid cached in PCC, and can
1563         # be reused from PCC cache directly.
1564         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach -k $file ||
1565                 error "PCC detach $file failed"
1566         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1567         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach $file ||
1568                 error "PCC detach $file failed"
1569         do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
1570                 error "failed to dd write to $file"
1571         rm -f $file || error "rm $file failed"
1572
1573         echo "Verify auto attach at open from inside NS for RW-PCC"
1574         # nsenter has strange behavior with echo, which means we have to place
1575         # this in a script so we can use sh, otherwise it doesn't execute echo
1576         # in the namespace
1577         # NB: using /bin/echo instead of the shell built in does not help
1578         echo "echo -n autoattach_data > $file" > $DIR/$tdir/$tfile.shell
1579         # File is owned by root, make it accessible to RUNAS user
1580         chmod a+rw $DIR/$tdir/$tfile.shell
1581         stack_trap 'rm -f $DIR/$tdir/$tfile.shell' EXIT
1582         do_facet $SINGLEAGT nsenter -t $PID -U -m "sh $DIR/$tdir/$tfile.shell"
1583         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1584                 $file || error "RW-PCC attach $file failed"
1585         check_lpcc_state $file "readwrite"
1586
1587         # Revoke the layout lock, the PCC-cached file will be
1588         # detached automatically.
1589         do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
1590         check_file_data $SINGLEAGT $file "autoattach_data" $PID
1591         check_lpcc_state $file "readwrite"
1592
1593         # Detach the file with -k option, as the file layout generation
1594         # is not changed, so the file is still valid cached in PCC,
1595         # and can be reused from PCC cache directly.
1596         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1597                 error "RW-PCC detach $file failed"
1598         check_lpcc_state $file "readwrite"
1599         # HSM released exists archived status
1600         check_hsm_flags $file "0x0000000d"
1601         check_file_data $SINGLEAGT $file "autoattach_data" $PID
1602
1603         # HSM restore the PCC cached file, the layout generation
1604         # was changed, so the file can not be auto attached.
1605         $LFS hsm_restore $file || error "failed to restore $file"
1606         wait_request_state $(path2fid $file) RESTORE SUCCEED
1607         check_lpcc_state $file "none"
1608         # HSM exists archived status
1609         check_hsm_flags $file "0x00000009"
1610 }
1611 run_test 101a "Test auto attach in mount namespace (simulated container)"
1612
1613 complete $SECONDS
1614 check_and_cleanup_lustre
1615 exit_status