Whamcloud - gitweb
LU-17871 ldlm: FLOCK ownlocks may be not set
[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=v2
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 if [[ -r /etc/redhat-release ]]; then
58         rhel_version=$(sed -e 's/[^0-9.]*//g' /etc/redhat-release)
59         if (( $(version_code $rhel_version) >= $(version_code 9.3.0) )); then
60                 always_except EX-8739 6 7a 7b 23    # PCC-RW
61                 always_except LU-17289 102          # fio io_uring
62                 always_except LU-17781 33           # inconsistent LSOM
63         fi
64 fi
65
66 build_test_filter
67
68 # if there is no CLIENT1 defined, some tests can be ran on localhost
69 CLIENT1=${CLIENT1:-$HOSTNAME}
70 # if CLIENT2 doesn't exist then use CLIENT1 instead
71 # All tests should use CLIENT2 with MOUNT2 only therefore it will work if
72 # $CLIENT2 == CLIENT1
73 # Exception is the test which need two separate nodes
74 CLIENT2=${CLIENT2:-$CLIENT1}
75
76 check_file_size()
77 {
78         local client="$1"
79         local fpath="$2"
80         local expected_size="$3"
81
82         size=$(do_facet $client stat "--printf=%s" $fpath)
83         [[ $size == "$expected_size" ]] || error \
84                 "expected $fpath size: $expected_size got: $size"
85 }
86
87 check_lpcc_sizes()
88 {
89         local client="$1"
90         local lpcc_fpath="$2"
91         local lustre_fpath="$3"
92         local expected_size="$4"
93
94         check_file_size $client $lpcc_fpath $expected_size
95         check_file_size $client $lustre_fpath $expected_size
96 }
97
98 check_file_data()
99 {
100         local client="$1"
101         local path="$2"
102         local expected_data="$3"
103         local pid=$4
104
105         # if $pid is set, then run command within namespace for that process
106         path_data=$(do_facet $client ${pid:+nsenter -t $pid -U -m} cat $path)
107         [[ "x$path_data" == "x$expected_data" ]] ||
108                 error "expected $path: $expected_data, got: $path_data"
109 }
110
111 check_lpcc_data()
112 {
113         local client="$1"
114         local lpcc_fpath="$2"
115         local lustre_fpath="$3"
116         local expected_data="$4"
117
118         check_file_data  "$client" "$lpcc_fpath" "$expected_data"
119         check_file_data  "$client" "$lustre_fpath" "$expected_data"
120 }
121
122 lpcc_fid2path()
123 {
124         local hsm_root="$1"
125         local lustre_path="$2"
126         local fid=$(path2fid $lustre_path)
127
128         local seq=$(echo $fid | awk -F ':' '{print $1}')
129         local oid=$(echo $fid | awk -F ':' '{print $2}')
130         local ver=$(echo $fid | awk -F ':' '{print $3}')
131
132         case "$HSMTOOL_ARCHIVE_FORMAT" in
133                 v1)
134                         printf "%s/%04x/%04x/%04x/%04x/%04x/%04x/%s" \
135                                 $hsm_root $((oid & 0xFFFF)) \
136                                 $((oid >> 16 & 0xFFFF)) \
137                                 $((seq & 0xFFFF)) \
138                                 $((seq >> 16 & 0xFFFF)) \
139                                 $((seq >> 32 & 0xFFFF)) \
140                                 $((seq >> 48 & 0xFFFF)) $fid
141                         ;;
142                 v2)
143                         printf "%s/%04x/%s" $hsm_root $(((oid ^ seq) & 0xFFFF)) $fid
144                         ;;
145         esac
146 }
147
148 check_lpcc_state()
149 {
150         local lustre_path="$1"
151         local expected_state="$2"
152         local facet=${3:-$SINGLEAGT}
153         local myRUNAS="$4"
154         local state=$(do_facet $facet $myRUNAS $LFS pcc state $lustre_path |
155                         awk -F 'type: ' '{print $2}' | awk -F ',' '{print $1}')
156
157         [[ "x$state" == "x$expected_state" ]] || error \
158                 "$lustre_path expected pcc state: $expected_state, but got: $state"
159 }
160
161 # initiate variables
162 init_agt_vars
163
164 # populate MDT device array
165 get_mdt_devices
166
167 # cleanup from previous bad setup
168 kill_copytools
169
170 # for recovery tests, coordinator needs to be started at mount
171 # so force it
172 # the lustre conf must be without hsm on (like for sanity.sh)
173 echo "Set HSM on and start"
174 cdt_set_mount_state enabled
175 cdt_check_state enabled
176
177 echo "Set sanity-hsm HSM policy"
178 cdt_set_sanity_policy
179
180 # finished requests are quickly removed from list
181 set_hsm_param grace_delay 10
182
183 cleanup_pcc_mapping() {
184         local facet=${1:-$SINGLEAGT}
185
186         echo "Cleanup PCC backend on $MOUNT"
187         do_facet $facet $LCTL pcc clear $MOUNT
188 }
189
190 setup_pcc_mapping() {
191         local facet=${1:-$SINGLEAGT}
192         local hsm_root=${hsm_root:-$(hsm_root "$facet")}
193         local param="$2"
194
195         [ -z "$param" ] && param="projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
196         stack_trap "cleanup_pcc_mapping $facet" EXIT
197         do_facet $facet $LCTL pcc add $MOUNT $hsm_root -p "$param" ||
198                 error "Setup PCC backend $hsm_root on $MOUNT failed"
199 }
200
201 umount_loopdev() {
202         local facet=$1
203         local mntpt=$2
204         local rc
205
206         do_facet $facet lsof $mntpt || true
207         do_facet $facet $UMOUNT $mntpt
208         rc=$?
209         return $rc
210 }
211
212 setup_loopdev() {
213         local facet=$1
214         local file=$2
215         local mntpt=$3
216         local size=${4:-50}
217
218         do_facet $facet mkdir -p $mntpt || error "mkdir -p $mntpt failed"
219         stack_trap "do_facet $facet rm -rf $mntpt" EXIT
220         do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
221         stack_trap "do_facet $facet rm -f $file" EXIT
222         do_facet $facet mkfs.ext4 $file ||
223                 error "mkfs.ext4 $file failed"
224         do_facet $facet file $file
225         do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
226                 error "mount -o loop,usrquota,grpquota $file $mntpt failed"
227         stack_trap "umount_loopdev $facet $mntpt" EXIT
228 }
229
230 lpcc_rw_test() {
231         local restore="$1"
232         local project="$2"
233         local project_id=100
234         local agt_facet=$SINGLEAGT
235         local loopfile="$TMP/$tfile"
236         local mntpt="/mnt/pcc.$tdir"
237         local hsm_root="$mntpt/$tdir"
238         local file=$DIR/$tdir/$tfile
239         local -a state
240         local -a lpcc_path
241         local -a size
242
243         $project && enable_project_quota
244
245         do_facet $SINGLEAGT rm -rf $hsm_root
246         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
247         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
248
249         is_project_quota_supported || project=false
250
251         do_facet $SINGLEAGT $LFS mkdir -i0 -c1 $DIR/$tdir
252         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
253         $project && lfs project -sp $project_id $DIR2/$tdir
254
255         do_facet $SINGLEAGT "echo -n attach_origin > $file"
256         if ! $project; then
257                 check_lpcc_state $file "none"
258                 do_facet $SINGLEAGT $LFS pcc attach -w \
259                         -i $HSM_ARCHIVE_NUMBER $file ||
260                         error "pcc attach $file failed"
261         fi
262
263         check_lpcc_state $file "readwrite"
264         # HSM released exists archived status
265         check_hsm_flags $file "0x0000000d"
266         lpcc_path=$(lpcc_fid2path $hsm_root $file)
267         check_lpcc_data $SINGLEAGT $lpcc_path $file "attach_origin"
268
269         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=7654321 count=1
270         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 7654321
271
272         do_facet $SINGLEAGT $TRUNCATE $file 1234567 ||
273                 error "truncate failed"
274         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 1234567
275         check_lpcc_state $file "readwrite"
276
277         do_facet $SINGLEAGT "echo -n file_data > $file"
278         check_lpcc_state $file "readwrite"
279         # HSM released exists archived status
280         check_hsm_flags $file "0x0000000d"
281         check_lpcc_data $SINGLEAGT $lpcc_path $file "file_data"
282
283         echo "Restore testing..."
284         if [ $CLIENTCOUNT -lt 2 -o $restore ]; then
285                 $LFS hsm_restore $file || error \
286                         "failed to restore $file"
287                 wait_request_state $(path2fid $file) RESTORE SUCCEED
288         else
289                 path_data=$(do_node $CLIENT2 cat $file)
290                 [[ "x$path_data" == "xfile_data" ]] || error \
291                         "expected file_data, got: $path_data"
292         fi
293
294         check_lpcc_state $file "none"
295         # HSM exists archived status
296         check_hsm_flags $file "0x00000009"
297
298         echo -n "new_data" > $file
299         check_lpcc_state $file "none"
300         # HSM exists dirty archived status
301         check_hsm_flags $file "0x0000000b"
302         check_file_data $SINGLEAGT $file "new_data"
303
304         echo "Attach and detach testing"
305         rm -f $file
306         do_facet $SINGLEAGT "echo -n new_data2 > $file"
307         if ! $project; then
308                 check_lpcc_state $file "none"
309                 do_facet $SINGLEAGT $LFS pcc attach -w \
310                         -i $HSM_ARCHIVE_NUMBER $file ||
311                         error "PCC attach $file failed"
312         fi
313         check_lpcc_state $file "readwrite"
314         # HSM released exists archived status
315         check_hsm_flags $file "0x0000000d"
316         do_facet $SINGLEAGT "echo -n attach_detach > $file"
317         echo "Start to detach the $file"
318         do_facet $SINGLEAGT $LFS pcc detach $file ||
319                 error "PCC detach $file failed"
320         wait_request_state $(path2fid $file) REMOVE SUCCEED
321
322         check_lpcc_state $file "none"
323         # The file is removed from PCC
324         check_hsm_flags $file "0x00000000"
325         check_file_data $SINGLEAGT $file "attach_detach"
326 }
327
328 test_1a() {
329         lpcc_rw_test true false
330 }
331 run_test 1a "Test manual lfs pcc attach with manual HSM restore"
332
333 test_1b() {
334         lpcc_rw_test false false
335 }
336 run_test 1b "Test manual lfs pcc attach with restore on remote access"
337
338 test_1c() {
339         lpcc_rw_test true true
340 }
341 run_test 1c "Test automated attach using Project ID with manual HSM restore"
342
343 test_1d() {
344         lpcc_rw_test false true
345 }
346 run_test 1d "Test Project ID with remote access"
347
348 test_1e() {
349         local file=$DIR/$tdir/$tfile
350         local hsm_root=$(hsm_root)
351         local -a lpcc_path
352
353         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
354         setup_pcc_mapping $SINGLEAGT \
355                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
356         $LCTL pcc list $MOUNT
357         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
358         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
359
360         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
361                 error "failed to dd write to $file"
362         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
363                 $file || error "failed to attach file $file"
364         check_lpcc_state $file "readwrite"
365         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
366                 error "failed to dd read from $file"
367         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
368                 error "failed to truncate $file"
369         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
370                 error "failed to truncate $file"
371         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
372                 error "failed to dd write to $file"
373         check_lpcc_state $file "readwrite"
374
375         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
376                 error "failed to detach file $file"
377         check_lpcc_state $file "none"
378
379         # non-root user is forbidden to access PCC file directly
380         lpcc_path=$(lpcc_fid2path $hsm_root $file)
381         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
382                 error "non-root user can touch access PCC file $lpcc_path"
383         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
384                 count=1 && error "non-root user can read PCC file $lpcc_path"
385         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
386                 count=1 && error "non-root user can write PCC file $lpcc_path"
387
388         local perm=$(do_facet $SINGLEAGT stat -c %a $lpcc_path)
389
390         [[ $perm == "0" ]] || error "PCC file permission ($perm) is not zero"
391
392         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
393                 $file || error "failed to attach file $file"
394         check_lpcc_state $file "readwrite"
395
396         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
397                 error "failed to detach file $file"
398         wait_request_state $(path2fid $file) REMOVE SUCCEED
399         check_lpcc_state $file "none"
400 }
401 run_test 1e "Test RW-PCC with non-root user"
402
403 test_1f() {
404         local project_id=100
405         local agt_facet=$SINGLEAGT
406         local loopfile="$TMP/$tfile"
407         local mntpt="/mnt/pcc.$tdir"
408         local hsm_root="$mntpt/$tdir"
409         local file=$DIR/$tdir/$tfile
410
411         ! is_project_quota_supported &&
412                 skip "project quota is not supported"
413
414         enable_project_quota
415         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
416         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
417         setup_pcc_mapping $SINGLEAGT \
418                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=0\ stat_attach=0\ pccrw=1"
419
420         do_facet $SINGLEAGT $LFS mkdir -i0 -c1 $DIR/$tdir
421         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
422         $LFS project -sp $project_id $DIR/$tdir ||
423                 error "failed to set project for $DIR/$tdir"
424
425         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
426                 error "failed to dd write to $file"
427
428         check_lpcc_state $file "readwrite"
429         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
430                 error "failed to dd read from $file"
431         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
432                 error "failed to truncate $file"
433         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
434                 error "failed to truncate $file"
435         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=256 count=1 ||
436                 error "failed to dd write from $file"
437         check_lpcc_state $file "readwrite"
438
439         # non-root user is forbidden to access PCC file directly
440         lpcc_path=$(lpcc_fid2path $hsm_root $file)
441         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
442                 error "non-root user can touch access PCC file $lpcc_path"
443         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
444                 count=1 && error "non-root user can read PCC file $lpcc_path"
445         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
446                 count=1 && error "non-root user can write PCC file $lpcc_path"
447
448         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
449                 error "failed to detach file $file"
450         wait_request_state $(path2fid $file) REMOVE SUCCEED
451         check_lpcc_state $file "none"
452 }
453 run_test 1f "Test auto RW-PCC cache with non-root user"
454
455 test_1g() {
456         local loopfile="$TMP/$tfile"
457         local mntpt="/mnt/pcc.$tdir"
458         local hsm_root="$mntpt/$tdir"
459         local file=$DIR/$tfile
460
461         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
462         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
463         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
464
465         dd if=/dev/zero of=$file bs=1024 count=1 ||
466                 error "failed to dd write to $file"
467         chmod 600 $file || error "chmod 600 $file failed"
468         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
469                 error "non-root user can dd write $file"
470         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 &&
471                 error "non-root user can dd read $file"
472         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
473                 error "failed to attach file $file"
474         check_lpcc_state $file "readwrite"
475         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
476                 error "non-root user can dd write to $file"
477         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 &&
478                 error "non-root user can dd read $file"
479         chmod 777 $DIR2/$tfile || error "chmod 777 $DIR2/$tfile failed"
480         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
481                 error "non-root user cannot write $file with permission (777)"
482
483         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file &&
484                 error "non-root user or non owner can detach $file"
485         chown $RUNAS_ID $file || error "chown $RUNAS_ID $file failed"
486         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
487                 error "failed to detach file $file"
488         wait_request_state $(path2fid $file) REMOVE SUCCEED
489         check_lpcc_state $file "none"
490         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
491                 error "non-root user cannot read to $file with permisson (777)"
492 }
493 run_test 1g "General permission test for RW-PCC"
494
495 #
496 # When a process created a LPCC file and holding the open,
497 # another process on the same client should be able to open the file.
498 #
499 test_2a() {
500         local project_id=100
501         local agt_facet=$SINGLEAGT
502         local loopfile="$TMP/$tfile"
503         local mntpt="/mnt/pcc.$tdir"
504         local hsm_root="$mntpt/$tdir"
505         local agt_host=$(facet_active_host $SINGLEAGT)
506
507         ! is_project_quota_supported &&
508                 skip "project quota is not supported" && return
509
510         enable_project_quota
511         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
512         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
513         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
514         file=$DIR/$tdir/multiop
515         $LFS mkdir -i -1 -c $MDSCOUNT $DIR/$tdir
516         rm -f $file
517
518         do_facet $SINGLEAGT $LFS project -sp $project_id $DIR/$tdir ||
519                 error "failed to set project quota"
520         rmultiop_start $agt_host $file O_c || error "open $file failed"
521         # HSM released exists archived status
522         check_hsm_flags $file "0x0000000d"
523         do_facet $SINGLEAGT "echo -n multiopen_data > $file" ||
524                 error "failed to echo multiopen_data to $file"
525
526         lpcc_path=$(lpcc_fid2path $hsm_root $file)
527         do_facet $SINGLEAGT ls -l $lpcc_path ||
528                 error "failed to ls $lpcc_path"
529         check_lpcc_data $SINGLEAGT $lpcc_path $file "multiopen_data"
530         # HSM released exists archived status
531         check_hsm_flags $file "0x0000000d"
532
533         do_facet $SINGLEAGT $LFS pcc detach $file ||
534                 error "failed to detach $file"
535         rmultiop_stop $agt_host || error "close $file failed"
536 }
537 run_test 2a "Test multi open when creating"
538
539 get_remote_client() {
540         current_id=$(do_facet $SINGLEAGT hostname)
541         for client in ${CLIENTS//,/ }
542         do
543                 r_id=$(do_node $client hostname)
544                 if [ $r_id != $current_id ]; then
545                         echo $client
546                         return
547                 fi
548         done
549 }
550
551 #
552 # When a process created a LPCC file and holding the open, another
553 # process on the different client should be able to open the file
554 # and perform IO on the file.
555 #
556 test_2b() {
557         local agt_facet=$SINGLEAGT
558         local agt_host=$(facet_active_host $SINGLEAGT)
559         local loopfile="$TMP/$tfile"
560         local mntpt="/mnt/pcc.$tdir"
561         local hsm_root="$mntpt/$tdir"
562
563         needclients 2 || return 0
564
565         remote_client=$(get_remote_client)
566
567         enable_project_quota
568         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
569         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
570         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
571         file=$DIR/$tdir/multiop
572         mkdir -p $DIR/$tdir
573         rm -f $file
574
575         do_facet $SINGLEAGT "echo -n file_data > $file"
576         do_facet $SINGLEAGT lfs pcc attach -w -i $HSM_ARCHIVE_NUMBER \
577                 $file || error "PCC attach $file failed"
578         check_lpcc_state $file "readwrite"
579
580         rmultiop_start $agt_host $file O_c || error "open $file failed"
581
582         do_node $remote_client "echo -n multiopen_data > $file"
583
584         # PCC cached file should be automatically detached
585         check_lpcc_state $file "none"
586
587         check_file_data $SINGLEAGT $file "multiopen_data"
588         rmultiop_stop $agt_host || error "close $file failed"
589         check_file_data $SINGLEAGT $file "multiopen_data"
590
591         do_node $remote_client cat $file || error \
592                 "cat $file on remote client failed"
593         do_node $remote_client echo -n "multiopen_data" > $file \
594                 || error "write $file on remote client failed"
595 }
596 run_test 2b "Test multi remote open when creating"
597
598 test_2c() {
599         local agt_host=$(facet_active_host $SINGLEAGT)
600         local loopfile="$TMP/$tfile"
601         local mntpt="/mnt/pcc.$tdir"
602         local hsm_root="$mntpt/$tdir"
603         local file=$DIR/$tdir/$tfile
604         local file2=$DIR2/$tdir/$tfile
605
606         enable_project_quota
607         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
608         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
609         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
610         mkdir -p $DIR/$tdir
611         rm -f $file
612
613         do_facet $SINGLEAGT "echo -n file_data > $file"
614         do_facet $SINGLEAGT lfs pcc attach -w -i $HSM_ARCHIVE_NUMBER \
615                 $file || error "PCC attach $file failed"
616         check_lpcc_state $file "readwrite"
617
618         rmultiop_start $agt_host $file O_c || error "open $file failed"
619
620         echo -n multiopen_data > $file2
621
622         # PCC cached file should be automatically detached
623         check_lpcc_state $file "none"
624
625         check_file_data $SINGLEAGT $file "multiopen_data"
626         rmultiop_stop $agt_host || error "close $file failed"
627         check_file_data $SINGLEAGT $file "multiopen_data"
628
629         cat $file2 || error "cat $file on mount $MOUNT2 failed"
630         echo -n "multiopen_data" > $file2 ||
631                 error "write $file on mount $MOUNT2 failed"
632 }
633 run_test 2c "Test multi open on different mount points when creating"
634
635 test_3a() {
636         local file=$DIR/$tdir/$tfile
637         local file2=$DIR2/$tdir/$tfile
638
639         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
640                 skip "Server does not support PCC-RO"
641
642         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
643         setup_pcc_mapping $SINGLEAGT \
644                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1\ pccro=1"
645
646         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
647         dd if=/dev/zero of=$file2 bs=1024 count=1 ||
648                 error "failed to dd write to $file"
649
650         echo "Start to RW-PCC attach/detach the file: $file"
651         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
652                 error "failed to attach file $file"
653         check_lpcc_state $file "readwrite"
654         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
655                 error "failed to detach file $file"
656         check_lpcc_state $file "none"
657
658         echo "Repeat to RW-PCC attach/detach the same file: $file"
659         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
660                 error "failed to attach file $file"
661         check_lpcc_state $file "readwrite"
662         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
663                 error "failed to detach file $file"
664         check_lpcc_state $file "none"
665
666         rm -f $file || error "failed to remove $file"
667         echo "pccro_data" > $file
668
669         echo "Start to RO-PCC attach/detach the file: $file"
670         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
671                 error "failed to attach file $file"
672         check_lpcc_state $file "readonly"
673         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
674                 error "failed to detach file $file"
675         check_lpcc_state $file "none"
676
677         echo "Repeat to RO-PCC attach/detach the same file: $file"
678         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
679                 error "failed to attach file $file"
680         check_lpcc_state $file "readonly"
681         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
682                 error "failed to detach file $file"
683         check_lpcc_state $file "none"
684 }
685 run_test 3a "Repeat attach/detach operations"
686
687 test_3b() {
688         local n
689         local file=$DIR/$tdir/$tfile
690
691         needclients 3 || return 0
692
693         # Start all of the copytools and setup PCC
694         for n in $(seq $AGTCOUNT); do
695                 copytool setup -f agt$n -a $n -m $MOUNT -h $(hsm_root agt$n)
696                 setup_pcc_mapping agt$n "projid={100}\ rwid=$n\ auto_attach=0\ pccrw=1\ pccro=1"
697         done
698
699         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
700         dd if=/dev/zero of=$file bs=1024 count=1 ||
701                 error "failed to dd write to $file"
702
703         echo "Start to RW-PCC attach/detach $file on $agt1_HOST"
704         do_facet agt1 $LFS pcc attach -w -i 1 $file ||
705                 error "failed to attach file $file"
706         check_lpcc_state $file "readwrite" agt1
707         do_facet agt1 $LFS pcc detach -k $file ||
708                 error "failed to detach file $file"
709         check_lpcc_state $file "none" agt1
710
711         echo "Repeat to RW-PCC attach/detach $file on $agt2_HOST"
712         do_facet agt2 $LFS pcc attach -w -i 2 $file ||
713                 error "failed to attach file $file"
714         check_lpcc_state $file "readwrite" agt2
715         do_facet agt2 $LFS pcc detach -k $file ||
716                 error "failed to detach file $file"
717         check_lpcc_state $file "none" agt2
718
719         echo "Try RW-PCC attach on two agents"
720         do_facet agt1 $LFS pcc attach -w -i 1 $file ||
721                 error "failed to attach file $file"
722         check_lpcc_state $file "readwrite" agt1
723         do_facet agt2 $LFS pcc attach -w -i 2 $file ||
724                 error "failed to attach file $file"
725         check_lpcc_state $file "readwrite" agt2
726         # The later attach PCC agent should succeed,
727         # the former agent should be detached automatically.
728         check_lpcc_state $file "none" agt1
729         do_facet agt2 $LFS pcc detach -k $file ||
730                 error "failed to detach file $file"
731         check_lpcc_state $file "none" agt2
732
733         echo "Start to RO-PCC attach/detach $file on $agt1_HOST"
734         do_facet agt1 $LFS pcc attach -r -i 1 $file ||
735                 error "failed to attach file $file"
736         check_lpcc_state $file "readonly" agt1
737         do_facet agt1 $LFS pcc detach -k $file ||
738                 error "failed to detach file $file"
739         check_lpcc_state $file "none" agt1
740
741         echo "Repeat to RO-PCC attach/detach $file on $agt2_HOST"
742         do_facet agt2 $LFS pcc attach -r -i 2 $file ||
743                 error "failed to attach file $file"
744         check_lpcc_state $file "readonly" agt2
745         do_facet agt2 $LFS pcc detach -k $file ||
746                 error "failed to detach file $file"
747         check_lpcc_state $file "none" agt2
748
749         echo "Try RO-PCC attach on two agents"
750         do_facet agt1 $LFS pcc attach -r -i 1 $file ||
751                 error "failed to attach file $file"
752         check_lpcc_state $file "readonly" agt1
753         do_facet agt2 $LFS pcc attach -r -i 2 $file ||
754                 error "failed to attach file $file"
755         check_lpcc_state $file "readonly" agt2
756         check_lpcc_state $file "readonly" agt1
757         do_facet agt2 $LFS pcc detach -k $file ||
758                 error "failed to detach file $file"
759         check_lpcc_state $file "none" agt2
760         do_facet agt1 $LFS pcc detach -k $file ||
761                 error "failed to detach file $file"
762         check_lpcc_state $file "none" agt1
763 }
764 run_test 3b "Repeat attach/detach operations on multiple clients"
765
766 test_4() {
767         local project_id=100
768         local loopfile="$TMP/$tfile"
769         local mntpt="/mnt/pcc.$tdir"
770         local hsm_root="$mntpt/$tdir"
771         local excepts="-e 6 -e 7 -e 8 -e 9"
772
773         ! is_project_quota_supported &&
774                 skip "project quota is not supported" && return
775
776         enable_project_quota
777         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
778         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
779         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
780
781         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
782         lfs project -sp $project_id $DIR/$tdir ||
783                 error "lfs project -sp $project_id $DIR/$tdir failed"
784
785         # 1. mmap_sanity tst7 failed on the local ext4 filesystem.
786         #    It seems that Lustre filesystem does special process for tst 7.
787         # 2. There is a mmap problem for PCC when multiple clients read/write
788         #    on a shared mmapped file for mmap_sanity tst 6.
789         # 3. Current CentOS8 kernel does not strictly obey POSIX syntax for
790         #    mmap() within the maping but beyond current end of the underlying
791         #    files: It does not send SIGBUS signals to the process.
792         # 4. For negative file offset, sanity_mmap also failed on 48 bits
793         #    ldiksfs backend due to too large offset: "Value too large for
794         #    defined data type".
795         # mmap_sanity tst7/tst8/tst9 all failed on Lustre and local ext4.
796         # Thus, we exclude sanity tst6/tst7/tst8/tst9 from the PCC testing.
797         $LUSTRE/tests/mmap_sanity -d $DIR/$tdir -m $DIR2/$tdir $excepts ||
798                 error "mmap_sanity test failed"
799         sync; sleep 1; sync
800
801         # Revoke the layout lock, the PCC-cached file will be
802         # detached automatically.
803         do_facet $SINGLEAGT $LCTL \
804                 set_param ldlm.namespaces.*mdc*.lru_size=clear
805         rm -rf $DIR/$tdir || error "failed to remove $DIR/$tdir"
806 }
807 run_test 4 "Auto cache test for mmap"
808
809 test_5() {
810         local file=$DIR/$tfile
811         local loopfile="$TMP/$tfile"
812         local mntpt="/mnt/pcc.$tdir"
813         local hsm_root="$mntpt/$tdir"
814
815         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
816         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
817         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
818
819         do_facet $SINGLEAGT "echo -n attach_mmap_data > $file" ||
820                 error "echo $file failed"
821
822         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
823                 error "failed to attach file $file"
824         check_lpcc_state $file "readwrite"
825
826         local content=$($MMAP_CAT $file)
827
828         [[ $content == "attach_mmap_data" ]] ||
829                 error "mmap cat data mismatch: $content"
830
831         $LFS hsm_restore $file || error "failed to restore $file"
832         wait_request_state $(path2fid $file) RESTORE SUCCEED
833         check_lpcc_state $file "none"
834
835         content=$($MMAP_CAT $file)
836         [[ $content == "attach_mmap_data" ]] ||
837                 error "mmap cat data mismatch: $content"
838 }
839 run_test 5 "Mmap & cat a RW-PCC cached file"
840
841 test_6() {
842         local loopfile="$TMP/$tfile"
843         local mntpt="/mnt/pcc.$tdir"
844         local hsm_root="$mntpt/$tdir"
845         local file=$DIR/$tfile
846         local content
847
848         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
849         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
850         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
851
852         echo -n mmap_write_data > $file || error "echo write $file failed"
853         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
854                 error "failed to attach file $file"
855         check_lpcc_state $file "readwrite"
856
857         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
858                 error "could not mmap $file"
859         check_lpcc_state $file "readwrite"
860         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
861         # After mmap write via multiop, the first character of each page
862         # increases with 1.
863         [[ $content == "nmap_write_data" ]] ||
864                 error "mmap write data mismatch: $content"
865         check_lpcc_state $file "readwrite"
866
867         do_facet $SINGLEAGT $LFS pcc detach $file ||
868                 error "failed to detach file $file"
869         wait_request_state $(path2fid $file) REMOVE SUCCEED
870
871         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
872         [[ $content == "nmap_write_data" ]] ||
873                 error "mmap write data mismatch: $content"
874 }
875 run_test 6 "Test mmap write on RW-PCC "
876
877 test_7a() {
878         local loopfile="$TMP/$tfile"
879         local mntpt="/mnt/pcc.$tdir"
880         local hsm_root="$mntpt/$tdir"
881         local file=$DIR/$tfile
882         local content
883
884         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
885         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
886         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
887
888         echo "QQQQQ" > $file
889         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
890                 error "failed to attach file $file"
891         check_lpcc_state $file "readwrite"
892         check_file_data $SINGLEAGT $file "QQQQQ"
893         # define OBD_FAIL_LLITE_PCC_DETACH_MKWRITE      0x1412
894         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1412
895         # HSM released exists archived status
896         check_hsm_flags $file "0x0000000d"
897
898         # multiop mmap write increase the first character of each page with 1
899         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
900                 error "mmap write $file failed"
901         check_lpcc_state $file "none"
902         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
903         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
904 }
905 run_test 7a "Fake file detached between fault() and page_mkwrite() for RW-PCC"
906
907 test_7b() {
908         local loopfile="$TMP/$tfile"
909         local mntpt="/mnt/pcc.$tdir"
910         local hsm_root="$mntpt/$tdir"
911         local file=$DIR/$tfile
912         local content
913         local pid
914
915         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
916         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
917         setup_pcc_mapping $SINGLEAGT \
918                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
919
920         echo "QQQQQ" > $file
921         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
922                 error "failed to attach file $file"
923         check_lpcc_state $file "readwrite"
924         check_file_data $SINGLEAGT $file "QQQQQ"
925         # define OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE       0x1413
926         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1413 fail_val=20
927         # HSM released exists archived status
928         check_hsm_flags $file "0x0000000d"
929
930         # multiop mmap write increases the first character of each page with 1
931         do_facet $SINGLEAGT $MULTIOP $file OSMWUc &
932         pid=$!
933
934         sleep 3
935         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
936                 error "failed to detach file $file"
937
938         wait $pid || error "multiop mmap write failed"
939         check_lpcc_state $file "none"
940         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
941         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
942 }
943 run_test 7b "Test the race with concurrent mkwrite and detach"
944
945 test_8() {
946         local loopfile="$TMP/$tfile"
947         local mntpt="/mnt/pcc.$tdir"
948         local hsm_root="$mntpt/$tdir"
949         local file=$DIR/$tfile
950
951         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
952         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
953         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
954
955         echo "QQQQQ" > $file
956         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
957                 error "failed to attach file $file"
958         check_lpcc_state $file "readwrite"
959         check_file_data $SINGLEAGT $file "QQQQQ"
960
961         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
962         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
963         do_facet $SINGLEAGT "echo -n ENOSPC_write > $file"
964         # Above write will return -ENOSPC failure and retry the IO on normal
965         # IO path. It will restore the HSM released file.
966         check_lpcc_state $file "none"
967         check_file_data $SINGLEAGT $file "ENOSPC_write"
968 }
969 run_test 8 "Test fake -ENOSPC tolerance for RW-PCC"
970
971 test_9() {
972         local loopfile="$TMP/$tfile"
973         local mntpt="/mnt/pcc.9a"
974         local hsm_root="$mntpt/$tdir"
975         local file=$DIR/$tfile
976
977         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
978
979         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
980         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
981         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
982
983         touch $file || error "touch $file failed"
984         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
985                 error "fail to attach $file"
986         check_lpcc_state $file "readwrite"
987         # write 60M data, it is larger than the capacity of PCC backend
988         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1M count=60 ||
989                 error "fail to dd write $file"
990         check_lpcc_state $file "none"
991         check_file_size $SINGLEAGT $file 62914560
992 }
993 run_test 9 "Test -ENOSPC tolerance on loop PCC device for RW-PCC"
994
995 test_usrgrp_quota() {
996         local loopfile="$TMP/$tfile"
997         local mntpt="/mnt/pcc.$tdir"
998         local hsm_root="$mntpt/$tdir"
999         local state="readonly"
1000         local mode="pccro"
1001         local ug=$1
1002         local rw=$2
1003         local id=$RUNAS_ID
1004
1005         [[ $ug == "g" ]] && id=$RUNAS_GID
1006         [[ -z $rw ]] || {
1007                 state="readwrite"
1008                 mode="pccrw"
1009         }
1010
1011         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1012         do_facet $SINGLEAGT quotacheck -c$ug $mntpt ||
1013                 error "quotacheck -c$ug $mntpt failed"
1014         do_facet $SINGLEAGT quotaon -$ug $mntpt ||
1015                 error "quotaon -$ug $mntpt failed"
1016         do_facet $SINGLEAGT setquota -$ug $id 0 20480 0 0 $mntpt ||
1017                 error "setquota -$ug $id on $mntpt failed"
1018         do_facet $SINGLEAGT repquota -${ug}vs $mntpt
1019
1020         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
1021         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ $mode=1"
1022         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1023
1024         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1025
1026         local file1=$DIR/$tdir/${ug}quotaA
1027         local file2=$DIR/$tdir/${ug}quotaB
1028
1029         dd if=/dev/zero of=$file1 bs=1M count=15 ||
1030                 error "dd write $file1 failed"
1031         dd if=/dev/zero of=$file2 bs=1M count=15 ||
1032                 error "dd write $file2 failed"
1033         chown $RUNAS_ID:$RUNAS_GID $file1 ||
1034                 error "chown $RUNAS_ID:$RUNAS_GID $file1 failed"
1035         chown $RUNAS_ID:$RUNAS_GID $file2 ||
1036                 error "chown $RUNAS_ID:$RUNAS_GID $file2 failed"
1037         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $rw \
1038                 $file1 || error "attach $file1 failed"
1039         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $rw \
1040                 $file2 && error "attach $file2 should fail due to quota limit"
1041         check_lpcc_state $file1 $state
1042         check_lpcc_state $file2 "none"
1043
1044         if [[ -z $rw ]]; then
1045                 do_facet $SINGLEAGT $LFS pcc detach $file1 ||
1046                         error "detach $file1 failed"
1047                 return 0
1048         fi
1049
1050         echo "Test -EDQUOT error tolerance for RW-PCC"
1051         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file1 bs=1M count=30 ||
1052                 error "dd write $file1 failed"
1053         # -EDQUOT error should be tolerated via fallback to normal Lustre path.
1054         check_lpcc_state $file1 "none"
1055 }
1056
1057 test_10a() {
1058         test_usrgrp_quota "u" "-w"
1059 }
1060 run_test 10a "Test RW-PCC with user quota on loop PCC device"
1061
1062 test_10b() {
1063         test_usrgrp_quota "g" "-w"
1064 }
1065 run_test 10b "Test RW-PCC with group quota on loop PCC device"
1066
1067 test_10c() {
1068         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1069                 skip "Server does not support PCC-RO"
1070
1071         test_usrgrp_quota "u"
1072 }
1073 run_test 10c "Test RO-PCC with user quota on loop PCC device"
1074
1075 test_10d() {
1076         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1077                 skip "Server does not support PCC-RO"
1078
1079         test_usrgrp_quota "g"
1080 }
1081 run_test 10d "Test RO-PCC with group quota on loop PCC device"
1082
1083 test_usrgrp_edquot() {
1084         local loopfile="$TMP/$tfile"
1085         local mntpt="/mnt/pcc.$tdir"
1086         local hsm_root="$mntpt/$tdir"
1087         local file=$DIR/$tfile
1088         local id=$RUNAS_ID
1089         local ug=$1
1090
1091         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1092                 skip "Server does not support PCC-RO"
1093
1094         [[ $ug == "g" ]] && id=$RUNAS_GID
1095         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1096         do_facet $SINGLEAGT quotacheck -c$ug $mntpt ||
1097                 error "quotacheck -c$ug $mntpt failed"
1098         do_facet $SINGLEAGT quotaon -$ug $mntpt ||
1099                 error "quotaon -$ug $mntpt failed"
1100         do_facet $SINGLEAGT setquota -$ug $id 0 4096 0 0 $mntpt ||
1101                 error "setquota -$ug $id on $mntpt failed"
1102         do_facet $SINGLEAGT repquota -${ug}vs $mntpt
1103         do_facet $SINGLEAGT mkdir $hsm_root || error "mkdir $hsm_root failed"
1104         setup_pcc_mapping $SINGLEAGT \
1105                 "${ug}id={$id}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
1106         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1107
1108         dd if=/dev/zero of=$file bs=1M count=2 ||
1109                 error "dd write $file failed"
1110         chown $RUNAS_ID:$RUNAS_GID $file ||
1111                 error "chown $RUNAS_ID:$RUNAS_GID $file failed"
1112         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1M count=2 ||
1113                 error "dd read $file failed"
1114         check_lpcc_state $file "readonly"
1115         $LFS getstripe -v $file
1116         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1M count=5 ||
1117                 error "dd write $file failed"
1118         check_lpcc_state $file "none"
1119         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1M count=5 ||
1120                 error "dd read $file failed"
1121         do_facet $SINGLEAGT $LFS pcc state $file
1122         $LFS getstripe -v $file
1123         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1124                 error "PCC-RO attach $file failed"
1125
1126         do_facet $SINGLEAGT $LFS pcc detach $file || error "detach $file failed"
1127 }
1128
1129 test_10e() {
1130         test_usrgrp_edquot "u"
1131 }
1132 run_test 10e "Tolerate -EDQUOT failure when auto PCC-RO attach with user quota"
1133
1134 test_10f() {
1135         test_usrgrp_edquot "g"
1136 }
1137 run_test 10f "Tolerate -EDQUOT failure when auto PCC-RO attach with group quota"
1138
1139 test_11() {
1140         local loopfile="$TMP/$tfile"
1141         local mntpt="/mnt/pcc.$tdir"
1142         local hsm_root="$mntpt/$tdir"
1143         local file=$DIR/$tfile
1144         local -a lpcc_path
1145         local lpcc_dir
1146
1147         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1148         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1149         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
1150
1151         do_facet $SINGLEAGT "echo -n QQQQQ > $file"
1152         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1153         lpcc_dir=$(dirname $lpcc_path)
1154         echo "Lustre file: $file LPCC dir: $lpcc_dir"
1155         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
1156                 error "mkdir -p $lpcc_dir failed"
1157         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1158                 error "failed to attach $file"
1159         check_lpcc_state $file "readwrite"
1160         check_file_data $SINGLEAGT $file "QQQQQ"
1161         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1162                 error "failed to detach $file"
1163         rm $file || error "rm $file failed"
1164
1165         # The parent directory of the PCC file is immutable
1166         do_facet $SINGLEAGT "echo -n immutable_dir > $file"
1167         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1168         lpcc_dir=$(dirname $lpcc_path)
1169         echo "Lustre file: $file LPCC dir: $lpcc_dir"
1170         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
1171                 error "mkdir -p $lpcc_dir failed"
1172         do_facet $SINGLEAGT chattr +i $lpcc_dir ||
1173                 error "chattr +i $lpcc_dir failed"
1174         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file &&
1175                 error "attach $file with immutable directory should be failed"
1176         do_facet $SINGLEAGT chattr -i $lpcc_dir ||
1177                 error "chattr -i $lpcc_dir failed"
1178         rm $file || error "rm $file failed"
1179
1180         # The PCC file path is set to a directory
1181         do_facet $SINGLEAGT "echo -n pcc_file_path_is_dir > $file"
1182         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1183         do_facet $SINGLEAGT mkdir -p $lpcc_path ||
1184                 error "mkdir -p $lpcc_path failed"
1185         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file &&
1186                 error "attach $file should fail as PCC path is a directory"
1187         rm $file || error "rm $file failed"
1188 }
1189 run_test 11 "Test attach fault injection with simulated PCC file path"
1190
1191 test_12() {
1192         local file=$DIR/$tfile
1193         local hsm_root=$(hsm_root)
1194         local -a lpcc_path
1195         local pid
1196
1197         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1198         setup_pcc_mapping $SINGLEAGT \
1199                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
1200
1201         echo  -n race_rw_attach_hsmremove > $file
1202         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1203         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1204                 error "attach $file failed"
1205         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1206                 error "detach $file failed"
1207         # HSM released exists archived status
1208         check_hsm_flags $file "0x0000000d"
1209         # define OBD_FAIL_LLITE_PCC_ATTACH_PAUSE        0x1414
1210         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1414 fail_val=20
1211         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file &
1212         pid=$!
1213         $LFS hsm_state $file
1214         sleep 3
1215         wait_request_state $(path2fid $file) RESTORE SUCCEED
1216         $LFS hsm_remove $file || error "hsm remove $file failed"
1217         wait $pid
1218         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1219                 error "RW-PCC cached file '$lpcc_path' should be removed"
1220
1221         return 0
1222 }
1223 run_test 12 "RW-PCC attach races with concurrent HSM remove"
1224
1225 test_rule_id() {
1226         local idstr="${1}id"
1227         local rule="${idstr}={$2}"
1228         local myRUNAS="$3"
1229         local file=$DIR/$tdir/$tfile
1230
1231         setup_pcc_mapping $SINGLEAGT \
1232                 "$rule\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
1233         $LCTL pcc list $MOUNT
1234
1235         do_facet $SINGLEAGT $LFS mkdir -i 0 $DIR/$tdir
1236         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1237
1238         rm -f $file || error "rm $file failed"
1239         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1240                 error "failed to dd write to $file"
1241         check_lpcc_state $file "readwrite"
1242         do_facet $SINGLEAGT $myRUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
1243                 error "failed to dd read from $file"
1244         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 256 ||
1245                 error "failed to truncate $file"
1246         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 2048 ||
1247                 error "failed to truncate $file"
1248         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1249                 error "failed to dd write from $file"
1250         check_lpcc_state $file "readwrite"
1251
1252         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1253                 error "failed to detach file $file"
1254         wait_request_state $(path2fid $file) REMOVE SUCCEED
1255         check_lpcc_state $file "none"
1256
1257         cleanup_pcc_mapping
1258 }
1259
1260 test_13a() {
1261         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1262         test_rule_id "u" "$RUNAS_ID" "runas -u $RUNAS_ID"
1263         test_rule_id "g" "$RUNAS_GID" "runas -u $RUNAS_ID -g $RUNAS_GID"
1264 }
1265 run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
1266
1267 test_13b() {
1268         local file
1269
1270         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1271         setup_pcc_mapping $SINGLEAGT \
1272                 "fname={*.h5\ suffix.*\ Mid*dle}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
1273         $LCTL pcc list $MOUNT
1274
1275         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1276         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1277
1278         file=$DIR/$tdir/prefix.h5
1279         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1280                 error "failed to dd write to $file"
1281         check_lpcc_state $file "readwrite"
1282         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1283                 error "failed to detach file $file"
1284         check_lpcc_state $file "none"
1285         rm $file || error "rm $file failed"
1286
1287         file=$DIR/$tdir/suffix.doc
1288         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1289                 error "failed to dd write to $file"
1290         check_lpcc_state $file "readwrite"
1291         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1292                 error "failed to detach file $file"
1293         check_lpcc_state $file "none"
1294         rm $file || error "rm $file failed"
1295
1296         file=$DIR/$tdir/MidPADdle
1297         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1298                 error "failed to dd write to $file"
1299         check_lpcc_state $file "readwrite"
1300         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1301                 error "failed to detach file $file"
1302         check_lpcc_state $file "none"
1303         rm $file || error "rm $file failed"
1304
1305         file=$DIR/$tdir/Midpad
1306         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1307                 error "failed to dd write to $file"
1308         check_lpcc_state $file "none"
1309         rm $file || error "rm $file failed"
1310 }
1311 run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
1312
1313 test_13c() {
1314         local file
1315         local myRUNAS
1316         local loopfile="$TMP/$tfile"
1317         local mntpt="/mnt/pcc.$tdir"
1318         local hsm_root="$mntpt/$tdir"
1319
1320         ! is_project_quota_supported &&
1321                 echo "Skip project quota is not supported" && return 0
1322
1323         enable_project_quota
1324         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1325         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1326         setup_pcc_mapping $SINGLEAGT \
1327                 "projid={100\ 200}\&fname={*.h5},uid={$RUNAS_ID}\&gid={$RUNAS_GID}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
1328         $LCTL pcc list $MOUNT
1329         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1330         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1331
1332         mkdir -p $DIR/$tdir/proj || error "mkdir $DIR/$tdir/proj failed"
1333         mkdir -p $DIR/$tdir/proj2 || error "mkdir $DIR/$tdir/proj2 failed"
1334         $LFS project -sp 100 $DIR/$tdir/proj ||
1335                 error "failed to set project for $DIR/$tdir/proj"
1336         $LFS project -sp 200 $DIR/$tdir/proj2 ||
1337                 error "failed to set project for $DIR/$tdir/proj2"
1338
1339         file=$DIR/$tdir/proj/notcache
1340         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1341                 error "failed to dd write to $file"
1342         check_lpcc_state $file "none"
1343         rm $file || error "rm $file failed"
1344
1345         file=$DIR/$tdir/proj/autocache.h5
1346         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1347                 error "failed to dd write to $file"
1348         check_lpcc_state $file "readwrite"
1349         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1350                 error "failed to detach $file"
1351         rm $file || error "rm $file failed"
1352
1353         file=$DIR/$tdir/proj2/notcache
1354         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1355                 error "failed to dd write to $file"
1356         check_lpcc_state $file "none"
1357         rm $file || error "rm $file failed"
1358
1359         file=$DIR/$tdir/proj2/autocache.h5
1360         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1361                 error "failed to dd write to $file"
1362         check_lpcc_state $file "readwrite"
1363         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1364                 error "failed to detach $file"
1365         rm $file || error "rm $file failed"
1366
1367         file=$DIR/$tdir/ugidcache
1368         myRUNAS="runas -u $RUNAS_ID -g $RUNAS_GID"
1369         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1370                 error "failed to dd write to $file"
1371         check_lpcc_state $file "readwrite"
1372         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1373                 error "failed to detach $file"
1374         rm $file || error "rm $file failed"
1375 }
1376 run_test 13c "Check auto RW-PCC create caching for UID/GID/ProjID/fname rule"
1377
1378 test_14() {
1379         local file=$DIR/$tdir/$tfile
1380
1381         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1382                 skip "Server does not support PCC-RO"
1383
1384         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1385         setup_pcc_mapping $SINGLEAGT \
1386                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1\ pccro=1"
1387
1388         mkdir -p $DIR/$tdir || error "mkdir -p $DIR/$tdir failed"
1389         do_facet $SINGLEAGT "echo -n autodetach_data > $file"
1390         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
1391                 $file || error "PCC attach $file failed"
1392         check_lpcc_state $file "readwrite"
1393
1394         # Revoke the layout lock, the PCC-cached file will be
1395         # detached automatically.
1396         do_facet $SINGLEAGT $LCTL \
1397                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1398         check_file_data $SINGLEAGT $file "autodetach_data"
1399         check_lpcc_state $file "none"
1400
1401         rm $file || error "rm $file failed"
1402         do_facet $SINGLEAGT "echo -n ro_autodetach_data > $file"
1403         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1404                 error "PCC attach $file failed"
1405         check_lpcc_state $file "readonly"
1406
1407         # Revoke the layout lock, the PCC-cached file will be
1408         # detached automatically.
1409         do_facet $SINGLEAGT $LCTL \
1410                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1411         check_file_data $SINGLEAGT $file "ro_autodetach_data"
1412         check_lpcc_state $file "none"
1413 }
1414 run_test 14 "Revocation of the layout lock should detach the file automatically"
1415
1416 test_15() {
1417         local loopfile="$TMP/$tfile"
1418         local mntpt="/mnt/pcc.$tdir"
1419         local hsm_root="$mntpt/$tdir"
1420         local file=$DIR/$tdir/$tfile
1421
1422         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1423                 skip "Server does not support PCC-RO"
1424
1425         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1426         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1427         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1\ pccro=1"
1428
1429         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1430         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
1431
1432         echo "Verify open attach for non-root user"
1433         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1434                 error "failed to dd write to $file"
1435         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
1436                 $file || error "failed to attach file $file"
1437         do_facet $SINGLEAGT $RUNAS $LFS pcc state $file
1438         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1439         # Revoke the layout lock, the PCC-cached file will be
1440         # detached automatically.
1441         do_facet $SINGLEAGT $LCTL \
1442                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1443         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1444         # Detach the file but keep the cache , as the file layout generation
1445         # is not changed, so the file is still valid cached in PCC, and can
1446         # be reused from PCC cache directly.
1447         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
1448                 error "PCC detach $file failed"
1449         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1450         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
1451                 error "PCC detach $file failed"
1452         rm $file || error "rm $file failed"
1453
1454         echo "Verify auto attach at open for RW-PCC"
1455         do_facet $SINGLEAGT "echo -n autoattach_data > $file"
1456         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
1457                 $file || error "RW-PCC attach $file failed"
1458         check_lpcc_state $file "readwrite"
1459
1460         # Revoke the layout lock, the PCC-cached file will be
1461         # detached automatically.
1462         do_facet $SINGLEAGT $LCTL \
1463                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1464         check_file_data $SINGLEAGT $file "autoattach_data"
1465         check_lpcc_state $file "readwrite"
1466
1467         # Detach the file with -k option, as the file layout generation
1468         # is not changed, so the file is still valid cached in PCC,
1469         # and can be reused from PCC cache directly.
1470         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1471                 error "RW-PCC detach $file failed"
1472         check_lpcc_state $file "readwrite"
1473         # HSM released exists archived status
1474         check_hsm_flags $file "0x0000000d"
1475         check_file_data $SINGLEAGT $file "autoattach_data"
1476
1477         # HSM restore the PCC cached file, the layout generation
1478         # was changed, so the file can not be auto attached.
1479         $LFS hsm_restore $file || error "failed to restore $file"
1480         wait_request_state $(path2fid $file) RESTORE SUCCEED
1481         check_lpcc_state $file "none"
1482         # HSM exists archived status
1483         check_hsm_flags $file "0x00000009"
1484
1485         echo "Verify auto attach at open for RO-PCC"
1486         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
1487                 error "RO-PCC attach $file failed"
1488         check_lpcc_state $file "readonly"
1489
1490         # Revoke the layout lock, the PCC-cached file will be
1491         # detached automatically.
1492         do_facet $SINGLEAGT $LCTL \
1493                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1494         check_file_data $SINGLEAGT $file "autoattach_data"
1495         check_lpcc_state $file "readonly"
1496
1497         # Detach the file with "-k" option, as the file layout generation
1498         # is not changed, so the file is still valid cached in PCC,
1499         # and can be reused from PCC cache directly.
1500         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1501                 error "RO-PCC detach $file failed"
1502         check_lpcc_state $file "readonly"
1503         check_file_data $SINGLEAGT $file "autoattach_data"
1504         do_facet $SINGLEAGT $LFS pcc detach $file ||
1505                 error "RO-PCC detach $file failed"
1506 }
1507 run_test 15 "Test auto attach at open when file is still valid cached"
1508
1509 test_16() {
1510         local loopfile="$TMP/$tfile"
1511         local mntpt="/mnt/pcc.$tdir"
1512         local hsm_root="$mntpt/$tdir"
1513         local file=$DIR/$tfile
1514         local -a lpcc_path
1515
1516         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1517                 skip "Server does not support PCC-RO"
1518
1519         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1520         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1521         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1\ pccro=1"
1522
1523         echo "Test detach for RW-PCC"
1524         do_facet $SINGLEAGT "echo -n detach_data > $file"
1525         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1526         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
1527                 $file || error "RW-PCC attach $file failed"
1528         check_lpcc_state $file "readwrite"
1529         # HSM released exists archived status
1530         check_hsm_flags $file "0x0000000d"
1531
1532         echo "Test for reusing valid PCC cache"
1533         # Valid PCC cache can be reused
1534         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1535                 error "PCC detach $file failed"
1536         check_lpcc_state $file "readwrite"
1537         # HSM released exists archived status
1538         check_hsm_flags $file "0x0000000d"
1539
1540         echo "Test for the default detach"
1541         # Permanent detach by default, it will remove the PCC copy
1542         do_facet $SINGLEAGT $LFS pcc detach $file ||
1543                 error "RW-PCC detach $file failed"
1544         wait_request_state $(path2fid $file) REMOVE SUCCEED
1545         check_lpcc_state $file "none"
1546         # File is removed from PCC backend
1547         check_hsm_flags $file "0x00000000"
1548         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1549                 error "RW-PCC cached file '$lpcc_path' should be removed"
1550
1551         echo "Test detach for RO-PCC"
1552         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
1553                 error "RO-PCC attach $file failed"
1554         check_lpcc_state $file "readonly"
1555
1556         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1557                 error "RO-PCC detach $file failed"
1558         check_lpcc_state $file "readonly"
1559
1560         do_facet $SINGLEAGT $LFS pcc detach $file ||
1561                 error "RO-PCC detach $file failed"
1562         check_lpcc_state $file "none"
1563         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1564                 error "RO-PCC cached file '$lpcc_path' should be removed"
1565
1566         return 0
1567 }
1568 run_test 16 "Test detach with different options"
1569
1570 test_17() {
1571         local agt_host=$(facet_active_host $SINGLEAGT)
1572         local loopfile="$TMP/$tfile"
1573         local mntpt="/mnt/pcc.$tdir"
1574         local hsm_root="$mntpt/$tdir"
1575         local file=$DIR/$tfile
1576
1577         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1578         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1579         setup_pcc_mapping $SINGLEAGT \
1580                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=0\ stat_attach=0\ pccrw=1"
1581
1582         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1583
1584         do_facet $SINGLEAGT "echo -n layout_refresh_data > $file"
1585         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1586                 error "PCC attach $file failed"
1587         check_lpcc_state $file "readwrite"
1588
1589         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1590                 error "PCC detach $file failed"
1591         check_lpcc_state $file "none"
1592
1593         # Truncate should attach the file into PCC automatically
1594         # as the PCC copy is still valid.
1595         echo "Verify auto attach during IO for truncate"
1596         do_facet $SINGLEAGT $TRUNCATE $file 4 || error "truncate $file failed"
1597         check_lpcc_state $file "readwrite"
1598
1599         echo "Verify auto attach during IO for read/write"
1600         rmultiop_start $agt_host $file O_r || error "open $file failed"
1601         sleep 3
1602
1603         # Revoke the layout lock, the PCC-cached file will be
1604         # detached automatically.
1605         do_facet $SINGLEAGT $LCTL \
1606                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1607
1608         check_lpcc_state $file "none"
1609         rmultiop_stop $agt_host || error "close $file failed"
1610         sleep 3
1611         check_lpcc_state $file "readwrite"
1612
1613         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1614                 error "PCC detach $file failed"
1615         check_lpcc_state $file "none"
1616 }
1617 run_test 17 "Test auto attach for layout refresh"
1618
1619 test_18() {
1620         local agt_host=$(facet_active_host $SINGLEAGT)
1621         local loopfile="$TMP/$tfile"
1622         local mntpt="/mnt/pcc.$tdir"
1623         local hsm_root="$mntpt/$tdir"
1624         local file=$DIR/$tfile
1625         local oldmd5
1626         local newmd5
1627
1628         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1629         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1630         setup_pcc_mapping $SINGLEAGT \
1631                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
1632
1633         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
1634         do_facet $SINGLEAGT dd if=/dev/urandom of=$file bs=1M count=4 ||
1635                 error "failed to write $file"
1636         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1637                 error "failed to attach $file"
1638         do_facet $SINGLEAGT $LFS pcc state $file
1639         check_lpcc_state $file "readwrite"
1640         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1641                 error "failed to detach $file"
1642         do_facet $SINGLEAGT $LFS pcc state $file
1643         $CHECKSTAT -s 4194304 $file
1644         dd if=/dev/zero of=$DIR2/$tfile seek=1k bs=1k count=1 ||
1645                 error "failed to write $DIR2/$tfile"
1646         oldmd5=$(md5sum $DIR2/$tfile | awk '{print $1}')
1647         $CHECKSTAT -s 1049600 $DIR2/$tfile || error "$DIR2/$tfile size wrong"
1648
1649         local lpcc_path=$(lpcc_fid2path $hsm_root $file)
1650
1651         do_facet $SINGLEAGT $LFS pcc state $file
1652         check_file_size $SINGLEAGT $lpcc_path 4194304
1653         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1654                 error "failed to attach $file"
1655         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 1049600
1656         newmd5=$(do_facet $SINGLEAGT md5sum $file | awk '{print $1}')
1657         [ "$oldmd5" == "$newmd5" ] || error "md5sum differ: $oldmd5 != $newmd5"
1658         do_facet $SINGLEAGT $LFS pcc detach $file ||
1659                 error "failed to detach $file"
1660 }
1661 run_test 18 "Verify size correctness after re-attach the file"
1662
1663 test_19() {
1664         local agt_host=$(facet_active_host $SINGLEAGT)
1665         local loopfile="$TMP/$tfile"
1666         local mntpt="/mnt/pcc.$tdir"
1667         local hsm_root="$mntpt/$tdir"
1668         local file=$DIR/$tfile
1669
1670         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1671         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1672         setup_pcc_mapping $SINGLEAGT \
1673                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
1674
1675         do_facet $SINGLEAGT "echo -n QQQQQ > $file" || error "echo $file failed"
1676         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1677         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1678                 error "Failed to attach $file"
1679         check_lpcc_state $file "readwrite"
1680         check_lpcc_sizes $SINGLEAGT $file $lpcc_path 5
1681         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1682                 error "Failed to detach $file"
1683         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1684                 error "Failed to attach $file"
1685         check_lpcc_sizes $SINGLEAGT $file $lpcc_path 5
1686         do_facet $SINGLEAGT $LFS pcc detach --keep $file ||
1687                 error "Failed to detach $file"
1688 }
1689 run_test 19 "Verify the file re-attach works as expected"
1690
1691 test_20() {
1692         local agt_host=$(facet_active_host $SINGLEAGT)
1693         local loopfile="$TMP/$tfile"
1694         local mntpt="/mnt/pcc.$tdir"
1695         local hsm_root="$mntpt/$tdir"
1696         local file=$DIR/$tfile
1697
1698         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1699         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1700         setup_pcc_mapping $SINGLEAGT \
1701                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
1702
1703         do_facet $SINGLEAGT "echo -n QQQQQ > $file" ||
1704                 error "echo $file failed"
1705         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
1706                 error "Failed to attach $file"
1707         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1708         check_lpcc_state $file "readwrite"
1709         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1710         do_facet $SINGLEAGT "echo 3 > /proc/sys/vm/drop_caches"
1711         check_lpcc_state $file "readwrite"
1712         do_facet $SINGLEAGT $LFS pcc detach $file ||
1713                 error "Failed to detach $file"
1714 }
1715 run_test 20 "Auto attach works after the inode was once evicted from cache"
1716
1717 test_21a() {
1718         local loopfile="$TMP/$tfile"
1719         local mntpt="/mnt/pcc.$tdir"
1720         local hsm_root="$mntpt/$tdir"
1721         local file=$DIR/$tfile
1722
1723         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1724                 skip "Server does not support PCC-RO"
1725
1726         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1727         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1728         setup_pcc_mapping $SINGLEAGT \
1729                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1730
1731         do_facet $SINGLEAGT "echo -n pccro_as_mirror_layout > $file"
1732         echo "Plain layout info before PCC-RO attach '$file':"
1733         $LFS getstripe -v $file
1734         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1735                 error "RW-PCC attach $file failed"
1736         check_lpcc_state $file "readonly"
1737         echo -e "\nFLR layout info after PCC-RO attach '$file':"
1738         $LFS getstripe -v $file
1739         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1740                 error "failed to detach file $file"
1741         check_lpcc_state $file "none"
1742         echo -e "\nFLR layout info after PCC-RO detach '$file':"
1743         $LFS getstripe -v $file
1744
1745         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1746                 error "failed to attach file $file"
1747         check_lpcc_state $file "readonly"
1748         echo -e "\nFLR layout info after RO-PCC attach $file again:"
1749         $LFS getstripe -v $file
1750         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1751                 error "failed to detach file $file"
1752         check_lpcc_state $file "none"
1753         echo -e "\nFLR layout info after RO-PCC detach '$file' again:"
1754         $LFS getstripe -v $file
1755 }
1756 run_test 21a "PCC-RO storing as a plain HSM mirror component for plain layout"
1757
1758 test_21b() {
1759         local loopfile="$TMP/$tfile"
1760         local mntpt="/mnt/pcc.$tdir"
1761         local hsm_root="$mntpt/$tdir"
1762         local file=$DIR/$tfile
1763
1764         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1765                 skip "Server does not support PCC-RO"
1766
1767         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1768         do_facet $SINGLEAGT mkdir -p $hsm_root ||
1769                 error "failed to mkdir $hsm_root"
1770         setup_pcc_mapping $SINGLEAGT \
1771                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1772
1773         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1  $file ||
1774                 error "create mirrored file $file failed"
1775         #do_facet $SINGLEAGT "echo -n pccro_as_mirror_layout > $file"
1776         echo "FLR layout before PCC-RO attach '$file':"
1777         $LFS getstripe -v $file
1778         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1779                 error "failed to attach file $file"
1780         check_lpcc_state $file "readonly"
1781         echo -e "\nFLR layout after PCC-RO attach '$file':"
1782         $LFS getstripe -v $file
1783         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1784                 error "failed to detach file $file"
1785         check_lpcc_state $file "none"
1786         echo -e "\nFLR layout info after PCC-RO detach '$file':"
1787         $LFS getstripe -v $file
1788
1789         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1790                 error "failed to attach file $file"
1791         check_lpcc_state $file "readonly"
1792         echo -e "\nFLR layout after PCC-RO attach '$file' again:"
1793         $LFS getstripe -v $file
1794         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1795                 error "failed to detach file $file"
1796         check_lpcc_state $file "none"
1797         echo -e "\nFLR layout info after PCC-RO detach '$file':"
1798         $LFS getstripe -v $file
1799 }
1800 run_test 21b "PCC-RO stroing as a plain HSM mirror component for FLR layouts"
1801
1802 test_21c() {
1803         local loopfile="$TMP/$tfile"
1804         local mntpt="/mnt/pcc.$tdir"
1805         local hsm_root="$mntpt/$tdir"
1806         local file=$DIR/$tfile
1807         local fid
1808
1809         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1810                 skip "Server does not support PCC-RO"
1811
1812         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1813         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1814         setup_pcc_mapping $SINGLEAGT \
1815                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1816
1817         do_facet $SINGLEAGT "echo -n pccro_hsm_release > $file"
1818         fid=$(path2fid $file)
1819         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $file ||
1820                 error "Archive $file failed"
1821         wait_request_state $fid ARCHIVE SUCCEED
1822         $LFS hsm_state $file
1823
1824         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
1825                 error "RO-PCC attach $file failed"
1826         # HSM exists archived status
1827         check_hsm_flags $file "0x00000009"
1828         check_lpcc_state $file "readonly"
1829         check_file_data $SINGLEAGT $file "pccro_hsm_release"
1830
1831         $LFS hsm_release $file || error "HSM released $file failed"
1832         $LFS getstripe $file
1833         $LFS hsm_state $file
1834         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1835                 error "failed to detach $file"
1836         check_lpcc_state $file "none"
1837
1838         unlink $file || error "unlink $file failed"
1839 }
1840 run_test 21c "Verify HSM release works storing PCC-RO as HSM mirror component"
1841
1842 test_21d() {
1843         local loopfile="$TMP/$tfile"
1844         local mntpt="/mnt/pcc.$tdir"
1845         local hsm_root="$mntpt/$tdir"
1846         local file=$DIR/$tfile
1847
1848         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1849                 skip "Server does not support PCC-RO"
1850
1851         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1852         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1853         setup_pcc_mapping
1854
1855         echo "pccro_init_data" > $file
1856         $LFS getstripe $file
1857         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1858                 error "failed to PCC-RO attach file $file"
1859         check_lpcc_state $file "readonly"
1860         echo "PCC-RO attach '$file':"
1861         $LFS getstripe -v $file
1862
1863         echo "Write invalidated PCC-RO cache:"
1864         echo -n "write_mod_data" > $file
1865         check_lpcc_state $file "none"
1866         $LFS getstripe -v $file
1867         check_file_data $SINGLEAGT $file "write_mod_data"
1868 }
1869 run_test 21d "Write should invalidate PCC-RO caching"
1870
1871 test_21e() {
1872         local loopfile="$TMP/$tfile"
1873         local mntpt="/mnt/pcc.$tdir"
1874         local hsm_root="$mntpt/$tdir"
1875         local file=$DIR/$tfile
1876
1877         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1878                 skip "Server does not support PCC-RO"
1879
1880         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1881         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1882         setup_pcc_mapping
1883
1884         echo "pccro_init_data" > $file
1885         $LFS getstripe $file
1886         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1887                 error "failed to PCC-RO attach file $file"
1888         check_lpcc_state $file "readonly"
1889         echo "PCC-RO attach '$file':"
1890         $LFS getstripe -v $file
1891
1892         echo "Trucate invalidate PCC-RO file '$file':"
1893         $TRUNCATE $file 256 || error "failed to truncate $file"
1894         $LFS getstripe -v $file
1895         check_lpcc_state $file "none"
1896         check_file_size $SINGLEAGT $file 256
1897 }
1898 run_test 21e "Truncate should invalidate PCC-RO caching"
1899
1900 test_21f() {
1901         local loopfile="$TMP/$tfile"
1902         local mntpt="/mnt/pcc.$tdir"
1903         local hsm_root="$mntpt/$tdir"
1904         local file=$DIR/$tfile
1905
1906         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1907                 skip "Server does not support PCC-RO"
1908
1909         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1910         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1911         setup_pcc_mapping
1912
1913         echo "pccro_mmap_data" > $file
1914         $LFS getstripe $file
1915         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1916                 error "failed to PCC-RO attach file $file"
1917         check_lpcc_state $file "readonly"
1918         echo "PCC-RO attach '$file':"
1919         $LFS getstripe -v $file
1920
1921         echo "Mmap write invalidate PCC-RO caching:"
1922         # Mmap write will invalidate the RO-PCC cache
1923         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
1924                 error "mmap write $file failed"
1925         check_lpcc_state $file "none"
1926         $LFS getstripe -v $file
1927         # After mmap-write by MULTIOP, the first character of the content
1928         # will be increased with 1.
1929         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
1930         [[ $content == "qccro_mmap_data" ]] ||
1931                 error "mmap_cat data mismatch: $content"
1932 }
1933 run_test 21f "mmap write should invalidate PCC-RO caching"
1934
1935 test_21g() {
1936         local loopfile="$TMP/$tfile"
1937         local mntpt="/mnt/pcc.$tdir"
1938         local hsm_root="$mntpt/$tdir"
1939         local file=$DIR/$tfile
1940
1941         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1942                 skip "Server does not support PCC-RO"
1943
1944         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1945         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1946         setup_pcc_mapping $SINGLEAGT \
1947                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1948
1949         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1  $file ||
1950                 error "create mirrored file '$file' failed"
1951         do_facet $SINGLEAGT "echo -n pccro_as_mirror_layout > $file"
1952         echo "FLR layout before PCC-RO attach '$file':"
1953         $LFS getstripe -v $file
1954         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1955                 error "failed to PCC-RO attach '$file'"
1956         echo "FLR layout after PCC-RO attach '$file':"
1957         $LFS getstripe -v $file
1958         echo "Layout after Write invalidate '$file':"
1959         echo -n pccro_write_invalidate_mirror > $file
1960         $LFS getstripe -v $file
1961 }
1962 run_test 21g "PCC-RO for file under FLR write pending state"
1963
1964 test_21h() {
1965         local loopfile="$TMP/$tfile"
1966         local mntpt="/mnt/pcc.$tdir"
1967         local hsm_root="$mntpt/$tdir"
1968         local file=$DIR/$tfile
1969
1970         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
1971                 skip "Server does not support PCC-RO"
1972
1973         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1974         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1975         setup_pcc_mapping $SINGLEAGT \
1976                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
1977
1978         $LFS mirror create -N -S 4M -c 2 -N -S 1M -c -1  $file ||
1979                 error "create mirrored file $file failed"
1980         #do_facet $SINGLEAGT "echo -n pccro_as_mirror_layout > $file"
1981         echo "FLR layout before PCC-RO attach '$file':"
1982         $LFS getstripe -v $file
1983         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1984                 error "failed to attach file $file"
1985         check_lpcc_state $file "readonly"
1986         echo -e "\nFLR layout after PCC-RO attach '$file':"
1987         $LFS getstripe -v $file
1988
1989         $LFS mirror extend -N -S 8M -c -1 $file ||
1990                 error "mirror extend $file failed"
1991         echo -e "\nFLR layout after extend a mirror:"
1992         $LFS getstripe -v $file
1993         $LFS pcc state $file
1994         check_lpcc_state $file "none"
1995
1996         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
1997                 error "failed to attach file $file"
1998         check_lpcc_state $file "readonly"
1999         echo -e "\nFLR layout after PCC-RO attach '$file' again:"
2000         $LFS getstripe -v $file
2001         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2002                 error "failed to detach file $file"
2003         check_lpcc_state $file "none"
2004 }
2005 run_test 21h "Extend mirror once file was PCC-RO cached"
2006
2007 test_21i() {
2008         local loopfile="$TMP/$tfile"
2009         local mntpt="/mnt/pcc.$tdir"
2010         local hsm_root="$mntpt/$tdir"
2011         local file=$DIR/$tfile
2012         local file2=$DIR2/$tfile
2013         local fid
2014
2015         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2016                 skip "Server does not support PCC-RO"
2017
2018         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2019         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2020         setup_pcc_mapping $SINGLEAGT \
2021                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccro=1\ pccrw=1"
2022
2023         do_facet $SINGLEAGT "echo -n hsm_release_pcc_file > $file"
2024         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2025                 error "RW-PCC attach $file failed"
2026         check_lpcc_state $file "readwrite"
2027         # HSM released exists archived status
2028         check_hsm_flags $file "0x0000000d"
2029
2030         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2031                 error "RW-PCC detach $file failed"
2032         check_lpcc_state $file "none"
2033         # HSM released exists archived status
2034         check_hsm_flags $file "0x0000000d"
2035
2036         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2037                 error "failed to PCC-RO attach $file"
2038
2039         $LFS hsm_state $file
2040         $LFS hsm_release $file || error "HSM released $file failed"
2041         echo "Layout after HSM release $file:"
2042         $LFS getstripe -v $file
2043         echo "PCC state $file:"
2044         $LFS pcc state $file
2045         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
2046                 error "RO-PCC attach $file failed"
2047         echo "Layout after PCC-RO attach $file again:"
2048         $LFS getstripe -v $file
2049         echo "PCC state:"
2050         $LFS pcc state $file
2051
2052         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2053                 error "RW-PCC detach $file failed"
2054         check_lpcc_state $file "none"
2055 }
2056 run_test 21i "HSM release increase layout gen, should invalidate PCC-RO cache"
2057
2058 test_22() {
2059         local loopfile="$TMP/$tfile"
2060         local mntpt="/mnt/pcc.$tdir"
2061         local hsm_root="$mntpt/$tdir"
2062         local file=$DIR/$tfile
2063         local file2=$DIR2/$tfile
2064         local fid
2065
2066         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2067                 skip "Server does not support PCC-RO"
2068
2069         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2070         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2071         setup_pcc_mapping $SINGLEAGT \
2072                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1\ pccro=1"
2073
2074         do_facet $SINGLEAGT "echo -n roattach_data > $file"
2075
2076         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2077                 error "RW-PCC attach $file failed"
2078         check_lpcc_state $file "readwrite"
2079         # HSM released exists archived status
2080         check_hsm_flags $file "0x0000000d"
2081
2082         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2083                 error "RW-PCC detach $file failed"
2084         check_lpcc_state $file "none"
2085         # HSM released exists archived status
2086         check_hsm_flags $file "0x0000000d"
2087
2088         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
2089                 error "RO-PCC attach $file failed"
2090         echo "Layout after PCC-RO attach $file:"
2091         $LFS getstripe -v $file
2092         # HSM exists archived status
2093         check_hsm_flags $file "0x00000009"
2094         check_lpcc_state $file "readonly"
2095         check_file_data $SINGLEAGT $file "roattach_data"
2096
2097         $LFS hsm_release $file || error "HSM released $file failed"
2098         echo "Layout after HSM release $file:"
2099         $LFS getstripe -v $file
2100         # HSM released exists archived status
2101         check_hsm_flags $file "0x0000000d"
2102         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
2103                 error "RO-PCC attach $file failed"
2104         echo "Layout after PCC-RO attach $file again:"
2105         $LFS getstripe -v $file
2106         check_lpcc_state $file "readonly"
2107         check_file_data $SINGLEAGT $file "roattach_data"
2108         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2109                 error "failed to detach $file"
2110         echo "Layout after PCC-RO detach $file:"
2111         $LFS getstripe -v $file
2112         rm -f $file2 || error "rm -f $file failed"
2113         do_facet $SINGLEAGT "echo -n roattach_data2 > $file"
2114         fid=$(path2fid $file)
2115         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $file ||
2116                 error "Archive $file failed"
2117         wait_request_state $fid ARCHIVE SUCCEED
2118         $LFS hsm_release $file || error "HSM released $file failed"
2119         # HSM released exists archived status
2120         check_hsm_flags $file "0x0000000d"
2121         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER -r $file ||
2122                 error "RO-PCC attach $file failed"
2123         check_lpcc_state $file "readonly"
2124         check_file_data $SINGLEAGT $file "roattach_data2"
2125         do_facet $SINGLEAGT $LFS pcc detach $file ||
2126                 error "RO-PCC detach $file failed"
2127 }
2128 run_test 22 "Test RO-PCC attach for the HSM released file"
2129
2130 test_23() {
2131         local loopfile="$TMP/$tfile"
2132         local mntpt="/mnt/pcc.$tdir"
2133         local hsm_root="$mntpt/$tdir"
2134         local file=$DIR/$tfile
2135         local -a lpcc_path
2136
2137         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2138                 skip "Server does not support PCC-RO"
2139
2140         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2141         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2142         setup_pcc_mapping
2143
2144         echo "pccro_data" > $file
2145         lpcc_path=$(lpcc_fid2path $hsm_root $file)
2146
2147         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2148                 error "failed to RO-PCC attach file $file"
2149         check_lpcc_state $file "readonly"
2150         check_lpcc_data $SINGLEAGT $lpcc_path $file "pccro_data"
2151
2152         local content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
2153
2154         [[ $content == "pccro_data" ]] ||
2155                 error "mmap_cat data mismatch: $content"
2156         check_lpcc_state $file "readonly"
2157
2158         echo -n "write_mod_data" > $file
2159         echo "Write should invalidate the RO-PCC cache:"
2160         $LFS getstripe -v $file
2161         check_lpcc_state $file "none"
2162         check_file_data $SINGLEAGT $file "write_mod_data"
2163
2164         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2165                 error "failed to RO-PCC attach file $file"
2166         check_lpcc_state $file "readonly"
2167         echo "PCC-RO attach '$file' again:"
2168         $LFS getstripe -v $file
2169
2170         echo "Truncate invalidate the RO-PCC cache:"
2171         $TRUNCATE $file 256 || error "failed to truncate $file"
2172         $LFS getstripe -v $file
2173         echo "Finish trucate operation"
2174         check_lpcc_state $file "none"
2175         check_file_size $SINGLEAGT $file 256
2176
2177         echo "Mmap write invalidates RO-PCC caching"
2178         echo -n mmap_write_data > $file || error "echo write $file failed"
2179         $LFS getstripe -v $file
2180         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2181                 error "failed to RO-PCC attach file $file"
2182         check_lpcc_state $file "readonly"
2183         echo "PCC-RO attach '$file' again:"
2184         $LFS getstripe -v $file
2185         echo "Mmap write $file via multiop"
2186         # Mmap write will invalidate the RO-PCC cache
2187         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
2188                 error "mmap write $file failed"
2189         check_lpcc_state $file "none"
2190         $LFS getstripe -v $file
2191         # After mmap-write by MULTIOP, the first character of the content
2192         # increases 1.
2193         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
2194         [[ $content == "nmap_write_data" ]] ||
2195                 error "mmap_cat data mismatch: $content"
2196 }
2197 run_test 23 "Test write/truncate/mmap-write invalidating RO-PCC caching"
2198
2199 test_24a() {
2200         local loopfile="$TMP/$tfile"
2201         local mntpt="/mnt/pcc.$tdir"
2202         local hsm_root="$mntpt/$tdir"
2203         local file=$DIR/$tdir/$tfile
2204         local -a lpcc_path
2205
2206         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2207                 skip "Server does not support PCC-RO"
2208
2209         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2210         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2211         setup_pcc_mapping $SINGLEAGT \
2212                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
2213         $LCTL pcc list $MOUNT
2214         mkdir -p $DIR/$tdir
2215         chmod 777 $DIR/$tdir
2216
2217         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
2218                 error "failed to dd write to $file"
2219         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER \
2220                 $file || error "failed to attach file $file"
2221         check_lpcc_state $file "readonly"
2222         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
2223                 error "failed to dd read from $file"
2224         check_lpcc_state $file "readonly"
2225
2226         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
2227                 error "failed to detach file $file"
2228         check_lpcc_state $file "none"
2229
2230         # non-root user is forbidden to access PCC file directly
2231         lpcc_path=$(lpcc_fid2path $hsm_root $file)
2232         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
2233                 error "non-root user can touch access PCC file $lpcc_path"
2234         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
2235                 count=1 && error "non-root user can read PCC file $lpcc_path"
2236         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
2237                 count=1 && error "non-root user can write PCC file $lpcc_path"
2238
2239         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER \
2240                 $file || error "failed to attach file $file"
2241         check_lpcc_state $file "readonly"
2242
2243         # Test RO-PCC detach as non-root user
2244         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
2245                 error "failed to detach file $file"
2246         check_lpcc_state $file "none"
2247         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
2248                 error "RO-PCC cached file '$lpcc_path' should be removed"
2249
2250         return 0
2251 }
2252 run_test 24a "Test RO-PCC with non-root user"
2253
2254 test_24b() {
2255         local loopfile="$TMP/$tfile"
2256         local mntpt="/mnt/pcc.$tdir"
2257         local hsm_root="$mntpt/$tdir"
2258         local file=$DIR/$tdir/$tfile
2259
2260         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2261                 skip "Server does not support PCC-RO"
2262
2263         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2264         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2265         setup_pcc_mapping
2266
2267         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
2268         dd if=/dev/zero of=$file bs=1024 count=1 ||
2269                 error "failed to dd write $file"
2270         chmod 600 $file || error "chmod 600 $file failed"
2271         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
2272                 error "non-root user can dd write $file"
2273         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 &&
2274                 error "non-root user can dd read $file"
2275         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2276                 error "failed to attach file $file"
2277         check_lpcc_state $file "readonly"
2278         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
2279                 error "non-root user can dd write $file"
2280         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 &&
2281                 error "non-root user can dd read $file"
2282         chmod 777 $file || error "chmod 777 $file failed"
2283         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
2284                 error "non-root user cannot read $file with permission (777)"
2285         check_lpcc_state $file "readonly"
2286
2287         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file &&
2288                 error "non-root user or non owner can detach $file"
2289         chown $RUNAS_ID $file || error "chown $RUNAS_ID $file failed"
2290         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
2291                 error "failed to detach file $file"
2292         check_lpcc_state $file "none"
2293         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
2294                 error "non-root user cannot read $file with permission (777)"
2295 }
2296 run_test 24b "General permission test for RO-PCC"
2297
2298 test_25() {
2299         local loopfile="$TMP/$tfile"
2300         local mntpt="/mnt/pcc.$tdir"
2301         local hsm_root="$mntpt/$tdir"
2302         local file=$DIR/$tdir/$tfile
2303         local content
2304
2305         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2306                 skip "Server does not support PCC-RO"
2307
2308         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2309         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2310         setup_pcc_mapping
2311
2312         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
2313
2314         echo "ro_fake_mmap_cat_err" > $file
2315         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2316                 error "failed to attach RO-PCC file $file"
2317         check_lpcc_state $file "readonly"
2318         check_file_data $SINGLEAGT $file "ro_fake_mmap_cat_err"
2319
2320         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
2321         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
2322         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
2323         [[ $content == "ro_fake_mmap_cat_err" ]] ||
2324                 error "failed to fall back to Lustre I/O path for mmap-read"
2325         # Above mmap read will return VM_FAULT_SIGBUS failure and
2326         # retry the IO on normal IO path.
2327         check_lpcc_state $file "readonly"
2328         check_file_data $SINGLEAGT $file "ro_fake_mmap_cat_err"
2329
2330         do_facet $SINGLEAGT $LFS pcc detach $file ||
2331                 error "failed to detach RO-PCC file $file"
2332         check_lpcc_state $file "none"
2333
2334         do_facet $SINGLEAGT $LCTL set_param fail_loc=0
2335         echo "ro_fake_cat_err" > $file
2336         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2337                 error "failed to attach RO-PCC file $file"
2338         check_lpcc_state $file "readonly"
2339         check_file_data $SINGLEAGT $file "ro_fake_cat_err"
2340
2341         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
2342         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
2343         # Fake read I/O will return -EIO failure and
2344         # retry the IO on normal IO path.
2345         check_file_data $SINGLEAGT $file "ro_fake_cat_err"
2346         check_lpcc_state $file "readonly"
2347
2348         do_facet $SINGLEAGT $LFS pcc detach $file ||
2349                 error "failed to detach RO-PCC file $file"
2350         check_lpcc_state $file "none"
2351 }
2352 run_test 25 "Tolerate fake read failure for RO-PCC"
2353
2354 test_26() {
2355         local agt_host=$(facet_active_host $SINGLEAGT)
2356         local loopfile="$TMP/$tfile"
2357         local mntpt="/mnt/pcc.$tdir"
2358         local hsm_root="$mntpt/$tdir"
2359         local file=$DIR/$tfile
2360
2361         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2362                 skip "Server does not support PCC-RO"
2363
2364         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2365         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER" -h "$hsm_root"
2366         setup_pcc_mapping $SINGLEAGT \
2367                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1\ pccro=1"
2368
2369         echo -n attach_keep_open > $file
2370         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2371                 error "attach $file failed"
2372         check_lpcc_state $file "readonly"
2373         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2374         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2375                 error "detach $file failed"
2376         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2377                 error "attach $file failed"
2378         check_lpcc_state $file "readonly"
2379         check_file_data $SINGLEAGT $file "attach_keep_open"
2380         check_lpcc_state $file "readonly"
2381         do_facet $SINGLEAGT $LFS pcc detach $file ||
2382                 error "detach $file failed"
2383         rmultiop_stop $agt_host || error "multiop $file close failed"
2384
2385         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2386                 error "attach $file failed"
2387         check_lpcc_state $file "readonly"
2388         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2389         do_facet $SINGLEAGT $LFS pcc detach $file ||
2390                 error "detach $file failed"
2391         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2392                 error "attach $file failed"
2393         check_lpcc_state $file "readonly"
2394         check_file_data $SINGLEAGT $file "attach_keep_open"
2395         check_lpcc_state $file "readonly"
2396         do_facet $SINGLEAGT $LFS pcc detach $file ||
2397                 error "detach $file failed"
2398         rmultiop_stop $agt_host || error "multiop $file close failed"
2399
2400         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2401                 error "attach $file failed"
2402         check_lpcc_state $file "readonly"
2403         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2404         do_facet $SINGLEAGT $LFS pcc detach $file ||
2405                 error "detach $file failed"
2406         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2407                 error "attach $file failed"
2408         check_lpcc_state $file "readonly"
2409         check_file_data $SINGLEAGT $file "attach_keep_open"
2410         check_lpcc_state $file "readonly"
2411         rmultiop_stop $agt_host || error "multiop $file close failed"
2412         do_facet $SINGLEAGT $LFS pcc detach $file ||
2413                 error "detach $file failed"
2414
2415         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2416                 error "attach $file failed"
2417         check_lpcc_state $file "readwrite"
2418         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2419         do_facet $SINGLEAGT $LFS pcc detach $file ||
2420                 error "detach $file failed"
2421         wait_request_state $(path2fid $file) REMOVE SUCCEED
2422         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2423                 error "attach $file failed"
2424         check_lpcc_state $file "readonly"
2425         check_file_data $SINGLEAGT $file "attach_keep_open"
2426         check_lpcc_state $file "readonly"
2427         rmultiop_stop $agt_host || error "multiop $file close failed"
2428         check_lpcc_state $file "readonly"
2429         do_facet $SINGLEAGT $LFS pcc detach $file ||
2430                 error "detach $file failed"
2431
2432         rm $file || error "rm $file failed"
2433         echo -n attach_keep_open > $file
2434         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2435                 error "attach $file failed"
2436         check_lpcc_state $file "readwrite"
2437         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2438         do_facet $SINGLEAGT $LFS pcc detach $file ||
2439                 error "detach $file failed"
2440         wait_request_state $(path2fid $file) REMOVE SUCCEED
2441         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2442                 error "attach $file failed"
2443         check_lpcc_state $file "readonly"
2444         check_file_data $SINGLEAGT $file "attach_keep_open"
2445         check_lpcc_state $file "readonly"
2446         do_facet $SINGLEAGT $LFS pcc detach $file ||
2447                 error "detach $file failed"
2448         rmultiop_stop $agt_host || error "multiop $file close failed"
2449         check_lpcc_state $file "none"
2450 }
2451 run_test 26 "Repeat the attach/detach when the file has multiple openers"
2452
2453 test_27() {
2454         local agt_host=$(facet_active_host $SINGLEAGT)
2455         local loopfile="$TMP/$tfile"
2456         local mntpt="/mnt/pcc.$tdir"
2457         local hsm_root="$mntpt/$tdir"
2458         local file=$DIR/$tfile
2459
2460         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2461                 skip "Server does not support PCC-RO"
2462
2463         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2464         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER" -h "$hsm_root"
2465         setup_pcc_mapping $SINGLEAGT \
2466                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1\ pccrw=1\ pccro=1"
2467
2468         echo -n auto_attach_multi_open > $file
2469         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2470                 error "attach $file failed"
2471         check_lpcc_state $file "readwrite"
2472         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2473         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2474                 error "detach $file failed"
2475         check_lpcc_state $file "readwrite"
2476         check_file_data $SINGLEAGT $file "auto_attach_multi_open"
2477         check_lpcc_state $file "readwrite"
2478         do_facet $SINGLEAGT $LFS pcc detach $file ||
2479                 error "detach $file failed"
2480         wait_request_state $(path2fid $file) REMOVE SUCCEED
2481         check_lpcc_state $file "none"
2482         rmultiop_stop $agt_host || error "multiop $file close failed"
2483
2484         rm $file || error "rm $file failed"
2485         echo -n auto_attach_multi_open > $file
2486         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2487                 error "attach $file failed"
2488         check_lpcc_state $file "readwrite"
2489         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2490         do_facet $SINGLEAGT $LCTL \
2491                 set_param ldlm.namespaces.*mdc*.lru_size=clear
2492         check_lpcc_state $file "readwrite"
2493         check_file_data $SINGLEAGT $file "auto_attach_multi_open"
2494         check_lpcc_state $file "readwrite"
2495         do_facet $SINGLEAGT $LFS pcc detach $file ||
2496                 error "detach $file failed"
2497         wait_request_state $(path2fid $file) REMOVE SUCCEED
2498         check_lpcc_state $file "none"
2499         rmultiop_stop $agt_host || error "multiop $file close failed"
2500
2501         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2502                 error "attach $file failed"
2503         check_lpcc_state $file "readonly"
2504         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2505         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2506                 error "detach $file failed"
2507         check_lpcc_state $file "readonly"
2508         check_file_data $SINGLEAGT $file "auto_attach_multi_open"
2509         check_lpcc_state $file "readonly"
2510         do_facet $SINGLEAGT $LFS pcc detach $file ||
2511                 error "detach $file failed"
2512         check_lpcc_state $file "none"
2513         rmultiop_stop $agt_host || error "multiop $file close failed"
2514
2515         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2516                 error "attach $file failed"
2517         check_lpcc_state $file "readonly"
2518         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2519         do_facet $SINGLEAGT $LCTL \
2520                 set_param ldlm.namespaces.*mdc*.lru_size=clear
2521         check_lpcc_state $file "readonly"
2522         check_file_data $SINGLEAGT $file "auto_attach_multi_open"
2523         check_lpcc_state $file "readonly"
2524         do_facet $SINGLEAGT $LFS pcc detach $file ||
2525                 error "detach $file failed"
2526         check_lpcc_state $file "none"
2527         rmultiop_stop $agt_host || error "multiop $file close failed"
2528 }
2529 run_test 27 "Auto attach at open when the file has multiple openers"
2530
2531 test_28() {
2532         local agt_host=$(facet_active_host $SINGLEAGT)
2533         local loopfile="$TMP/$tfile"
2534         local mntpt="/mnt/pcc.$tdir"
2535         local hsm_root="$mntpt/$tdir"
2536         local file=$DIR/$tfile
2537         local file2=$DIR2/$tfile
2538         local multipid
2539
2540         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2541         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER" -h "$hsm_root"
2542         setup_pcc_mapping $SINGLEAGT \
2543                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1"
2544
2545         echo -n rw_attach_hasopen_fail > $file
2546         rmultiop_start $agt_host $file O_c || error "multiop $file failed"
2547         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file &&
2548                 error "attach $file should fail"
2549         rmultiop_stop $agt_host || error "multiop $file close failed"
2550         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2551                 error "attach $file should fail"
2552         check_lpcc_state $file "readwrite"
2553         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2554                 error "detach $file failed"
2555         check_lpcc_state $file "none"
2556
2557         multiop_bg_pause $file2 O_c || error "multiop $file2 failed"
2558         multipid=$!
2559         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file &&
2560                 error "attach $file should fail"
2561         kill -USR1 $multipid
2562         wait $multipid || error "multiop $file2 close failed"
2563         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2564                 error "failed to attach $file"
2565         check_lpcc_state $file "readwrite"
2566         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2567                 error "detach $file failed"
2568         check_lpcc_state $file "none"
2569 }
2570 run_test 28 "RW-PCC attach should fail when the file has cluster-wide openers"
2571
2572 test_29a() {
2573         local project_id=100
2574         local agt_facet=$SINGLEAGT
2575         local loopfile="$TMP/$tfile"
2576         local mntpt="/mnt/pcc.$tdir"
2577         local hsm_root="$mntpt/$tdir"
2578         local file=$DIR/$tdir/$tfile
2579         local file2=$DIR2/$tdir/$tfile
2580
2581         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2582                 skip "Server does not support PCC-RO"
2583
2584         is_project_quota_supported || skip "project quota is not supported"
2585
2586         enable_project_quota
2587         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2588         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2589         setup_pcc_mapping $SINGLEAGT \
2590                 "projid={$project_id}\ rwid=$HSM_ARCHIVE_NUMBER\ pccro=1"
2591         $LCTL pcc list $MOUNT
2592
2593         do_facet $SINGLEAGT mkdir -p $DIR/$tdir ||
2594                 error "mkdir $DIR/$tdir failed"
2595         do_facet $SINGLEAGT "echo -n ro_uptodate > $file" ||
2596                 error "failed to write $file"
2597         check_lpcc_state $file "none"
2598         $LFS project -sp $project_id $file ||
2599                 error "failed to set project for $file"
2600         $LFS project -d $file
2601         check_lpcc_state $file "readonly"
2602         check_file_data $SINGLEAGT $file "ro_uptodate"
2603
2604         echo -n Update_ro_data > $file2
2605         check_lpcc_state $file "readonly"
2606         check_file_data $SINGLEAGT $file "Update_ro_data"
2607
2608         do_facet $SINGLEAGT $LFS pcc detach $file ||
2609                 error "failed to detach $file"
2610 }
2611 run_test 29a "Auto readonly caching on RO-PCC backend for O_RDONLY open"
2612
2613 test_29b() {
2614         local loopfile="$TMP/$tfile"
2615         local mntpt="/mnt/pcc.$tdir"
2616         local hsm_root="$mntpt/$tdir"
2617         local file=$DIR/myfile.dat
2618
2619         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2620                 skip "Server does not support PCC-RO"
2621
2622         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2623         do_facet $SINGLEAGT mkdir $hsm_root || error "mkdir $hsm_root failed"
2624         setup_pcc_mapping $SINGLEAGT \
2625                 "fname={*.dat}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
2626         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2627
2628         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=4k count=1 ||
2629                 error "Write $file failed"
2630         do_facet $SINGLEAGT dd if=$file of=/dev/null bs=4k count=1 ||
2631                 error "Read $file failed"
2632         do_facet $SINGLEAGT $LFS pcc state $file
2633         check_lpcc_state $file "readonly"
2634         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=4k count=1 ||
2635                 error "Write $file failed"
2636         sysctl vm.drop_caches=3
2637         do_facet $SINGLEAGT dd if=$file of=/dev/null bs=4k count=1 ||
2638                 error "Read $file failed"
2639         do_facet $SINGLEAGT $LFS pcc state $file
2640         check_lpcc_state $file "readonly"
2641
2642         do_facet $SINGLEAGT $LFS pcc detach $file || error "detach $file failed"
2643 }
2644 run_test 29b "Auto PCC-RO attach in atomic_open"
2645
2646 test_30() {
2647         local loopfile="$TMP/$tfile"
2648         local mntpt="/mnt/pcc.$tdir"
2649         local hsm_root="$mntpt/$tdir"
2650         local file
2651
2652         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2653                 skip "Server does not support PCC-RO"
2654
2655         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2656         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2657         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1\ pccro=1"
2658
2659         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
2660
2661         file=$DIR/$tdir/rwattach
2662         echo -n backend_del_attach > $file
2663         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2664                 error "RW-PCC attach $file failed"
2665
2666         file=$DIR/$tdir/rwattachrm
2667         echo -n backend_del_attach_rm > $file
2668         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2669                 error "RW-PCC attach $file failed"
2670         rm $file || error "rm $file failed"
2671
2672         file=$DIR/$tdir/roattach
2673         echo -n backend_del_roattach_rm > $file
2674         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2675                 error "RO-PCC attach $file failed"
2676
2677         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2678         do_facet $SINGLEAGT $LCTL pcc del -v -v -v -v $MOUNT $hsm_root ||
2679                 error "lctl pcc del $MOUNT $hsm_root failed"
2680 }
2681 run_test 30 "Test lctl pcc del command"
2682
2683 test_31() {
2684         local loopfile="$TMP/$tfile"
2685         local mntpt="/mnt/pcc.$tdir"
2686         local hsm_root="$mntpt/$tdir"
2687         local -a lpcc_path1
2688         local -a lpcc_path2
2689         local -a lpcc_path3
2690         local file
2691
2692         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2693                 skip "Server does not support PCC-RO"
2694
2695         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2696         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2697         setup_pcc_mapping $SINGLEAGT \
2698                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0\ pccrw=1\ pccro=1"
2699
2700         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
2701
2702         file=$DIR/$tdir/rwattach
2703         echo -n backend_del_attach > $file
2704         lpcc_path1=$(lpcc_fid2path $hsm_root $file)
2705         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2706                 error "RW-PCC attach $file failed"
2707         check_lpcc_state $file "readwrite"
2708         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2709                 error "RW-PCC detach $file failed"
2710         check_lpcc_state $file "none"
2711
2712         file=$DIR/$tdir/rwattachrm
2713         echo -n backend_del_attach_rm > $file
2714         lpcc_path2=$(lpcc_fid2path $hsm_root $file)
2715         do_facet $SINGLEAGT $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER $file ||
2716                 error "RW-PCC attach $file failed"
2717         check_lpcc_state $file "readwrite"
2718         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2719                 error "RW-PCC detach $file failed"
2720         check_lpcc_state $file "none"
2721         rm $file || error "rm $file failed"
2722
2723         file=$DIR/$tdir/roattach
2724         echo -n backend_del_roattach_rm > $file
2725         lpcc_path3=$(lpcc_fid2path $hsm_root $file "readonly")
2726         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2727                 error "RO-PCC attach $file failed"
2728         check_lpcc_state $file "readonly"
2729         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2730                 error "RO-PCC detach $file failed"
2731         check_lpcc_state $file "none"
2732
2733         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2734         do_facet $SINGLEAGT $LCTL pcc del -v -v -v -v -k $MOUNT $hsm_root ||
2735                 error "lctl pcc del -k $MOUNT $hsm_root failed"
2736
2737         do_facet $SINGLEAGT "[ -f $lpcc_path1 ]" ||
2738                 error "PCC copy $lpcc_path1 should retain"
2739         do_facet $SINGLEAGT "[ -f $lpcc_path2 ]" ||
2740                 error "PCC copy $lpcc_path1 should retain"
2741         do_facet $SINGLEAGT "[ -f $lpcc_path3 ]" ||
2742                 error "PCC copy $lpcc_path1 should retain"
2743 }
2744 run_test 31 "Test lctl pcc del command with --keep option"
2745
2746 test_32() {
2747         local agt_host=$(facet_active_host $SINGLEAGT)
2748         local loopfile="$TMP/$tfile"
2749         local mntpt="/mnt/pcc.$tdir"
2750         local hsm_root="$mntpt/$tdir"
2751         local file=$DIR/$tfile
2752         local -a lpcc_path
2753
2754         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2755                 skip "Server does not support PCC-RO"
2756
2757         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2758         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2759         setup_pcc_mapping $SINGLEAGT \
2760                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ auto_attach=0"
2761
2762         do_facet $SINGLEAGT echo -n roattach_removed > $file
2763         lpcc_path=$(lpcc_fid2path $hsm_root $file "readonly")
2764         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2765                 error "RO-PCC attach $file failed"
2766         rmultiop_start $agt_host $file o_rc || error "multiop $file failed"
2767         sleep 3
2768         do_facet $SINGLEAGT rm $lpcc_path || error "rm $lpcc_path failed"
2769         rmultiop_stop $agt_host || error "multiop $file read failed"
2770         check_lpcc_state $file "readonly"
2771
2772         local content=$(do_facet $SINGLEAGT cat $file)
2773         [[ $content == "roattach_removed" ]] || error "data mismatch: $content"
2774         check_lpcc_state $file "readonly"
2775         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2776                 error "RO-PCC detach $file failed"
2777         check_lpcc_state $file "none"
2778
2779         do_facet $SINGLEAGT $LFS pcc attach -r -i $HSM_ARCHIVE_NUMBER $file ||
2780                 error "RO-PCC attach $file failed"
2781         do_facet $SINGLEAGT rm $lpcc_path || error "rm $lpcc_path failed"
2782         check_lpcc_state $file "readonly"
2783         content=$(do_facet $SINGLEAGT cat $file)
2784         [[ $content == "roattach_removed" ]] || error "data mismatch: $content"
2785         check_lpcc_state $file "readonly"
2786         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2787                 error "RO-PCC detach $file failed"
2788         check_lpcc_state $file "none"
2789 }
2790 run_test 32 "Test for RO-PCC when PCC copy is deleted"
2791
2792 test_33() {
2793         local loopfile="$TMP/$tfile"
2794         local mntpt="/mnt/pcc.$tdir"
2795         local hsm_root="$mntpt/$tdir"
2796         local file=$DIR/myfile.doc
2797         local file2=$DIR2/myfile.doc
2798
2799         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2800                 skip "Server does not support PCC-RO"
2801
2802         stack_trap "restore_opencache" EXIT
2803         disable_opencache
2804
2805         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2806         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2807
2808         setup_pcc_mapping $SINGLEAGT \
2809                 "fname={*.doc}\&size\<{1M}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
2810         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2811         touch $file || error "touch $file failed"
2812         $TRUNCATE $file $((1048576 * 2)) || error "Truncate $file failed"
2813         check_lpcc_state $file "none"
2814         do_facet $SINGLEAGT $LFS pcc state $file
2815         $TRUNCATE $file $((1048576 / 2)) || error "Truncate $file failed"
2816         do_facet $SINGLEAGT $LFS pcc state $file
2817         check_lpcc_state $file "readonly"
2818         cleanup_pcc_mapping
2819
2820         setup_pcc_mapping $SINGLEAGT \
2821                 "fname={*.doc}\&size\<{5M}\&size\>{3M}\ roid=5\ pccro=1"
2822         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2823         check_lpcc_state $file "none"
2824         $TRUNCATE $file2 $((1048576 * 6)) || error "Truncate $file2 failed"
2825         check_lpcc_state $file "none"
2826         $TRUNCATE $file2 $((1048576 * 4)) || error "Truncate $file2 failed"
2827         check_lpcc_state $file "readonly"
2828         cleanup_pcc_mapping
2829
2830         setup_pcc_mapping $SINGLEAGT \
2831                 "fname={*.doc}\&size={5M\ 3M}\ roid=5\ pccro=1"
2832         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2833         do_facet $SINGLEAGT $MULTIOP $file oc ||
2834                 error "failed to readonly open $file"
2835         check_lpcc_state $file "none"
2836         $TRUNCATE $file $((1048576 * 5)) || error "Truncate $file failed"
2837         do_facet $SINGLEAGT $MULTIOP $file oc ||
2838                 error "failed to readonly open $file"
2839         check_lpcc_state $file "readonly"
2840         do_facet $SINGLEAGT $LFS pcc detach $file ||
2841                 error "failed to detach $file"
2842         $TRUNCATE $file $((1048576 * 4)) || error "Truncate $file failed"
2843         do_facet $SINGLEAGT $MULTIOP $file oc ||
2844                 error "failed to readonly open $file"
2845         check_lpcc_state $file "none"
2846         $TRUNCATE $file $((1048576 * 3)) || error "Truncate $file failed"
2847         do_facet $SINGLEAGT $MULTIOP $file oc ||
2848                 error "failed to readonly open $file"
2849         check_lpcc_state $file "readonly"
2850         cleanup_pcc_mapping
2851 }
2852 run_test 33 "Cache rule with comparator (>, =, <) for file size"
2853
2854 test_34() {
2855         local loopfile="$TMP/$tfile"
2856         local mntpt="/mnt/pcc.$tdir"
2857         local hsm_root="$mntpt/$tdir"
2858         local file=$DIR/$tfile
2859
2860         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2861                 skip "Server does not support PCC-RO"
2862
2863         ! is_project_quota_supported &&
2864                 skip "project quota is not supported"
2865
2866         enable_project_quota
2867         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2868         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2869
2870         setup_pcc_mapping $SINGLEAGT \
2871                 "projid\>{100}\ roid=5\ ropcc=1"
2872         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2873         do_facet $SINGLEAGT "echo -n QQQQQ > $file" ||
2874                 error "failed to write $file"
2875         check_lpcc_state $file "none"
2876         $LFS project -p 99 $file || error "failed to set project for $file"
2877         $LFS project -d $file
2878         check_lpcc_state $file "none"
2879         $LFS project -p 101 $file || error "failed to set project for $file"
2880         $LFS project -d $file
2881         check_lpcc_state $file "readonly"
2882         cleanup_pcc_mapping
2883
2884         setup_pcc_mapping $SINGLEAGT \
2885                 "projid\<{100}\ roid=5\ ropcc=1"
2886         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2887         check_lpcc_state $file "none"
2888         $LFS project -p 102 $file || error "failed to set project for $file"
2889         $LFS project -d $file
2890         check_lpcc_state $file "none"
2891         $LFS project -p 99 $file || error "failed to set project for $file"
2892         $LFS project -d $file
2893         check_lpcc_state $file "readonly"
2894         cleanup_pcc_mapping
2895
2896         setup_pcc_mapping $SINGLEAGT \
2897                 "projid\<{120}\&projid\>{110}\ roid=5\ ropcc=1"
2898         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2899         check_lpcc_state $file "none"
2900         $LFS project -p 105 $file || error "failed to set project for $file"
2901         $LFS project -d $file
2902         check_lpcc_state $file "none"
2903         $LFS project -p 121 $file || error "failed to set project for $file"
2904         $LFS project -d $file
2905         check_lpcc_state $file "none"
2906         $LFS project -p 115 $file || error "failed to set project for $file"
2907         $LFS project -d $file
2908         check_lpcc_state $file "readonly"
2909         cleanup_pcc_mapping
2910 }
2911 run_test 34 "Cache rule with comparator (>, <) for Project ID range"
2912
2913 test_36_base() {
2914         local loopfile="$TMP/$tfile"
2915         local mntpt="/mnt/pcc.$tdir"
2916         local hsm_root="$mntpt/$tdir"
2917         local file=$DIR/$tfile
2918         local -a lpcc_path
2919         local state="readonly"
2920         local rw="$1"
2921
2922         [[ -z $rw ]] || state="readwrite"
2923         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2924         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
2925         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1\ pccro=1"
2926
2927         echo -n backend_clear_verify > $file
2928         lpcc_path=$(lpcc_fid2path $hsm_root $file)
2929         do_facet $SINGLEAGT $LFS pcc attach $rw -i $HSM_ARCHIVE_NUMBER $file ||
2930                 error "PCC attach $ro $file failed"
2931         check_lpcc_state $file "$state"
2932         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
2933                 error "PCC detach -k $file failed"
2934         do_facet $SINGLEAGT "[ -f $lpcc_path1 ]" ||
2935                 error "PCC copy $lpcc_path should retain"
2936         do_facet $SINGLEAGT $LCTL pcc clear -v $MOUNT ||
2937                 error "lctl pcc clear -v $MOUNT failed"
2938         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
2939                 error "PCC copy $lpcc_path should be removed"
2940         rm $file || error "rm $file failed"
2941 }
2942
2943 test_36a() {
2944         test_36_base "-w"
2945 }
2946 run_test 36a "Stale RW-PCC copy should be deleted after remove the PCC backend"
2947
2948 test_36b() {
2949         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2950                 skip "Server does not support PCC-RO"
2951
2952         test_36_base
2953 }
2954 run_test 36b "Stale RO-PCC copy should be deleted after remove the PCC backend"
2955
2956 test_41() {
2957         local loopfile="$TMP/$tfile"
2958         local mntpt="/mnt/pcc.$tdir"
2959         local hsm_root="$mntpt/$tdir"
2960         local file=$DIR/$tfile
2961
2962         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
2963                 skip "Server does not support PCC-RO"
2964
2965         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
2966         do_facet $SINGLEAGT mkdir $hsm_root || error "mkdir $hsm_root failed"
2967         setup_pcc_mapping $SINGLEAGT \
2968                 "mtime\>{1m}\ roid=$HSM_ARCHIVE_NUMBER\ ropcc=1"
2969         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
2970
2971         echo "pcc_ro_data" > $file || error "echo $file failed"
2972         do_facet $SINGLEAGT cat $file || error "cat $file failed"
2973         check_lpcc_state $file "none"
2974
2975         local mtime=$(date -d "2min ago" +%s)
2976
2977         do_facet $SINGLEAGT touch -m -d @$mtime $file ||
2978                 error "failed to change mtime for $file $mtime"
2979         do_facet $SINGLEAGT cat $file || error "cat $file failed"
2980         check_lpcc_state $file "readonly"
2981 }
2982 run_test 41 "Test mtime rule for PCC-RO open attach with O_RDONLY mode"
2983
2984 #test 101: containers and PCC
2985 #LU-15170: Test mount namespaces with PCC
2986 #This tests the cases where the PCC mount is not present in the container by
2987 #creating a mount namespace without the PCC mount in it (this is probably the
2988 #standard config for most containers)
2989 test_101a() {
2990         local loopfile="$TMP/$tfile"
2991         local mntpt="/mnt/pcc.$tdir"
2992         local hsm_root="$mntpt/$tdir"
2993         local file=$DIR/$tdir/$tfile
2994
2995         # Some kernels such as RHEL7 default to 0 user namespaces
2996         local maxuserns=$(do_facet $SINGLEAGT cat /proc/sys/user/max_user_namespaces)
2997         do_facet $SINGLEAGT "echo 10 > /proc/sys/user/max_user_namespaces"
2998         stack_trap "do_facet $SINGLEAGT 'echo $maxuserns > /proc/sys/user/max_user_namespaces'"
2999
3000         echo "creating user namespace for $RUNAS_ID"
3001         # Create a mount and user namespace with this command, and leave the
3002         # process running so we can do the rest of our steps
3003         local start=$SECONDS
3004         local PID=$(do_facet $SINGLEAGT \
3005                     "$RUNAS unshare -Um sleep 600 &>/dev/null & echo \\\$!")
3006         local elapsed=$((SECONDS - start))
3007         local count=0
3008
3009         do_facet $SINGLEAGT ps auxww | grep sleep
3010         echo "Created NS: child (sleep) pid=$PID in $elapsed seconds"
3011         [[ -n "$PID" ]] || error "remote sleep start failed"
3012         stack_trap "do_facet $SINGLEAGT kill -9 $PID" EXIT
3013         (( elapsed < 300 )) || error "remote sleep took $elapsed sec to start"
3014
3015         # Map 'RUNAS' to root in the namespace, so it has rights to do whatever
3016         # This is handled by '-r' in unshare in newer versions
3017         do_facet $SINGLEAGT $RUNAS newuidmap $PID 0 $RUNAS_ID 1 ||
3018                 error "could not map uid $RUNAS_ID to root in namespace"
3019         do_facet $SINGLEAGT $RUNAS newgidmap $PID 0 $RUNAS_GID 1 ||
3020                 error "could not map gid $RUNAS_GID to root in namespace"
3021
3022         # Create PCC after creating namespace; namespace will not have PCC
3023         # mount
3024         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
3025
3026         # Create a temp file inside the PCC mount to verify mount namespace
3027         do_facet $SINGLEAGT touch $mntpt/$tfile.tmp
3028         stack_trap "do_facet $SINGLEAGT rm -f $mntpt/$tfile.tmp" EXIT
3029         echo "Check for temp file in PCC mount"
3030         do_facet $SINGLEAGT test -f $mntpt/$tfile.tmp ||
3031                 error "Should see $mntpt/$tfile.tmp"
3032         echo "Check for temp file in PCC mount from inside namespace"
3033         do_facet $SINGLEAGT nsenter -t $PID -U -m test -f $mntpt/$tfile.tmp &&
3034                 error "Should not see $mntpt/$tfile.tmp from namespace"
3035         rm -f $mntpt/$tfile.tmp
3036
3037         # Finish PCC setup
3038         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
3039         setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ pccrw=1"
3040
3041         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3042         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
3043
3044         echo "Verify open attach from inside mount namespace"
3045         do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
3046                 error "failed to dd write to $file"
3047         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach -w \
3048                 -i $HSM_ARCHIVE_NUMBER $file || error "cannot attach $file"
3049         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc state $file
3050
3051         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
3052         # Revoke the layout lock, the PCC-cached file will be
3053         # detached automatically.
3054         do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
3055         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
3056         # Detach the file but keep the cache, as the file layout generation
3057         # is not changed, so the file is still valid cached in PCC, and can
3058         # be reused from PCC cache directly.
3059         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach -k $file ||
3060                 error "PCC detach $file failed"
3061         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
3062         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach $file ||
3063                 error "PCC detach $file failed"
3064         do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
3065                 error "failed to dd write to $file"
3066         rm -f $file || error "rm $file failed"
3067
3068         echo "Verify auto attach at open from inside NS for RW-PCC"
3069         # nsenter has strange behavior with echo, which means we have to place
3070         # this in a script so we can use sh, otherwise it doesn't execute echo
3071         # in the namespace
3072         # NB: using /bin/echo instead of the shell built in does not help
3073         echo "echo -n autoattach_data > $file" > $DIR/$tdir/$tfile.shell
3074         # File is owned by root, make it accessible to RUNAS user
3075         chmod a+rw $DIR/$tdir/$tfile.shell
3076         stack_trap 'rm -f $DIR/$tdir/$tfile.shell' EXIT
3077         do_facet $SINGLEAGT nsenter -t $PID -U -m "bash $DIR/$tdir/$tfile.shell"
3078         do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach -w -i $HSM_ARCHIVE_NUMBER \
3079                 $file || error "RW-PCC attach $file failed"
3080         check_lpcc_state $file "readwrite"
3081
3082         # Revoke the layout lock, the PCC-cached file will be
3083         # detached automatically.
3084         do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
3085         check_file_data $SINGLEAGT $file "autoattach_data" $PID
3086         check_lpcc_state $file "readwrite"
3087
3088         # Detach the file with -k option, as the file layout generation
3089         # is not changed, so the file is still valid cached in PCC,
3090         # and can be reused from PCC cache directly.
3091         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
3092                 error "RW-PCC detach $file failed"
3093         check_lpcc_state $file "readwrite"
3094         # HSM released exists archived status
3095         check_hsm_flags $file "0x0000000d"
3096         check_file_data $SINGLEAGT $file "autoattach_data" $PID
3097
3098         # HSM restore the PCC cached file, the layout generation
3099         # was changed, so the file can not be auto attached.
3100         $LFS hsm_restore $file || error "failed to restore $file"
3101         wait_request_state $(path2fid $file) RESTORE SUCCEED
3102         check_lpcc_state $file "none"
3103         # HSM exists archived status
3104         check_hsm_flags $file "0x00000009"
3105 }
3106 run_test 101a "Test auto attach in mount namespace (simulated container)"
3107
3108 test_102() {
3109         grep -q io_uring_setup /proc/kallsyms ||
3110                 skip "Client OS does not support io_uring I/O engine"
3111         io_uring_probe || skip "kernel does not support io_uring fully"
3112
3113         $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
3114                 skip "Server does not support PCC-RO"
3115
3116         which fio || skip_env "no fio installed"
3117         fio --enghelp | grep -q io_uring ||
3118                 skip_env "fio does not support io_uring I/O engine"
3119
3120         local loopfile="$TMP/$tfile"
3121         local mntpt="/mnt/pcc.$tdir"
3122         local hsm_root="$mntpt/$tdir"
3123         local file=$DIR/$tfile
3124
3125         setup_loopdev client $loopfile $mntpt 60
3126         mkdir $hsm_root || error "mkdir $hsm_root failed"
3127         setup_pcc_mapping client \
3128                 "projid={0}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
3129         do_facet $SINGLEAGT $LCTL set_param llite.*.pcc_async_threshold=0
3130
3131         local ioengine="io_uring"
3132         local numjobs=2
3133         local size=10M
3134
3135         do_facet $SINGLEAGT fio --name=seqwrite --ioengine=$ioengine    \
3136                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
3137                 --iodepth=64 --size=$size --filename=$file --rw=write ||
3138                 error "fio seqwrite $file failed"
3139
3140         # Read the file will trigger the buffered read from Lustre OSTs and
3141         # write to PCC copy as @pcc_async_threshold is set with 0.
3142         do_facet $SINGLEAGT fio --name=seqread --ioengine=$ioengine     \
3143                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
3144                 --iodepth=64 --size=$size --filename=$file --rw=read ||
3145                 error "fio seqread $file failed"
3146 }
3147 run_test 102 "PCC-RO should not hange for io_uring I/O engine"
3148
3149 complete_test $SECONDS
3150 check_and_cleanup_lustre
3151 exit_status