Whamcloud - gitweb
LU-12043 llite: make sure readahead cover current read
[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 # bug number for skipped test:
15 ALWAYS_EXCEPT=""
16 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
17
18 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
19
20 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
21
22 . $LUSTRE/tests/test-framework.sh
23 init_test_env $@
24 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
25 init_logging
26
27 MULTIOP=${MULTIOP:-multiop}
28 OPENFILE=${OPENFILE:-openfile}
29 MMAP_CAT=${MMAP_CAT:-mmap_cat}
30 MOUNT_2=${MOUNT_2:-"yes"}
31 FAIL_ON_ERROR=false
32
33 # script only handles up to 10 MDTs (because of MDT_PREFIX)
34 [ $MDSCOUNT -gt 9 ] &&
35         error "script cannot handle more than 9 MDTs, please fix" && exit
36
37 check_and_setup_lustre
38
39 if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.52) ]]; then
40         skip_env "Need MDS version at least 2.12.52" && exit
41 fi
42
43 # $RUNAS_ID may get set incorrectly somewhere else
44 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
45         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!" && exit
46 fi
47 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
48 if getent group nobody; then
49         GROUP=nobody
50 elif getent group nogroup; then
51         GROUP=nogroup
52 else
53         error "No generic nobody group"
54 fi
55
56 build_test_filter
57
58 # if there is no CLIENT1 defined, some tests can be ran on localhost
59 CLIENT1=${CLIENT1:-$HOSTNAME}
60 # if CLIENT2 doesn't exist then use CLIENT1 instead
61 # All tests should use CLIENT2 with MOUNT2 only therefore it will work if
62 # $CLIENT2 == CLIENT1
63 # Exception is the test which need two separate nodes
64 CLIENT2=${CLIENT2:-$CLIENT1}
65
66 check_file_size()
67 {
68         local client="$1"
69         local fpath="$2"
70         local expected_size="$3"
71
72         size=$(do_facet $client stat "--printf=%s" $fpath)
73         [[ $size == "$expected_size" ]] || error \
74                 "expected $fpath size: $expected_size got: $size"
75 }
76
77 check_lpcc_sizes()
78 {
79         local client="$1"
80         local lpcc_fpath="$2"
81         local lustre_fpath="$3"
82         local expected_size="$4"
83
84         check_file_size $client $lpcc_fpath $expected_size
85         check_file_size $client $lustre_fpath $expected_size
86 }
87
88 check_file_data()
89 {
90         local client="$1"
91         local path="$2"
92         local expected_data="$3"
93
94         path_data=$(do_facet $client cat $path)
95         [[ "x$path_data" == "x$expected_data" ]] || error \
96                 "expected $path: $expected_data, got: $path_data"
97 }
98
99 check_lpcc_data()
100 {
101         local client="$1"
102         local lpcc_fpath="$2"
103         local lustre_fpath="$3"
104         local expected_data="$4"
105
106         check_file_data  "$client" "$lpcc_fpath" "$expected_data"
107         check_file_data  "$client" "$lustre_fpath" "$expected_data"
108 }
109
110 lpcc_fid2path()
111 {
112         local hsm_root="$1"
113         local lustre_path="$2"
114         local fid=$(path2fid $lustre_path)
115
116         local -a f_seq
117         local -a f_oid
118         local -a f_ver
119
120         f_seq=$(echo $fid | awk -F ':' '{print $1}')
121         f_oid=$(echo $fid | awk -F ':' '{print $2}')
122         f_ver=$(echo $fid | awk -F ':' '{print $3}')
123
124         printf "%s/%04x/%04x/%04x/%04x/%04x/%04x/%s" \
125                 $hsm_root $(($f_oid & 0xFFFF)) \
126                 $(($f_oid >> 16 & 0xFFFF)) \
127                 $(($f_seq & 0xFFFF)) \
128                 $(($f_seq >> 16 & 0xFFFF)) \
129                 $(($f_seq >> 32 & 0xFFFF)) \
130                 $(($f_seq >> 48 & 0xFFFF)) $fid
131 }
132
133 check_lpcc_state()
134 {
135         local lustre_path="$1"
136         local expected_state="$2"
137         local facet=${3:-$SINGLEAGT}
138         local myRUNAS="$4"
139         local state=$(do_facet $facet $myRUNAS $LFS pcc state $lustre_path |
140                         awk -F 'type: ' '{print $2}' | awk -F ',' '{print $1}')
141
142         [[ "x$state" == "x$expected_state" ]] || error \
143                 "$lustre_path expected pcc state: $expected_state, but got: $state"
144 }
145
146 # initiate variables
147 init_agt_vars
148
149 # populate MDT device array
150 get_mdt_devices
151
152 # cleanup from previous bad setup
153 kill_copytools
154
155 # for recovery tests, coordinator needs to be started at mount
156 # so force it
157 # the lustre conf must be without hsm on (like for sanity.sh)
158 echo "Set HSM on and start"
159 cdt_set_mount_state enabled
160 cdt_check_state enabled
161
162 echo "Set sanity-hsm HSM policy"
163 cdt_set_sanity_policy
164
165 # finished requests are quickly removed from list
166 set_hsm_param grace_delay 10
167
168 cleanup_pcc_mapping() {
169         local facet=${1:-$SINGLEAGT}
170
171         do_facet $facet $LCTL pcc clear $MOUNT
172 }
173
174 setup_pcc_mapping() {
175         local facet=${1:-$SINGLEAGT}
176         local hsm_root=${hsm_root:-$(hsm_root "$facet")}
177         local param="$2"
178
179         [ -z "$param" ] && param="projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
180         stack_trap "cleanup_pcc_mapping $facet" EXIT
181         do_facet $facet $LCTL pcc add $MOUNT $hsm_root -p $param
182 }
183
184 lpcc_rw_test() {
185         local restore="$1"
186         local project="$2"
187         local project_id=100
188         local agt_facet=$SINGLEAGT
189         local hsm_root=$(hsm_root)
190         local file=$DIR/$tdir/$tfile
191         local -a state
192         local -a lpcc_path
193         local -a size
194
195         $project && enable_project_quota
196
197         do_facet $SINGLEAGT rm -rf $hsm_root
198         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
199
200         is_project_quota_supported || project=false
201
202         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
203         setup_pcc_mapping
204         $project && lfs project -sp $project_id $DIR/$tdir
205
206         do_facet $SINGLEAGT "echo -n attach_origin > $file"
207         if ! $project; then
208                 check_lpcc_state $file "none"
209                 do_facet $SINGLEAGT $LFS pcc attach -i \
210                         $HSM_ARCHIVE_NUMBER $file ||
211                         error "pcc attach $file failed"
212         fi
213
214         check_lpcc_state $file "readwrite"
215         # HSM released exists archived status
216         check_hsm_flags $file "0x0000000d"
217         lpcc_path=$(lpcc_fid2path $hsm_root $file)
218         check_lpcc_data $SINGLEAGT $lpcc_path $file "attach_origin"
219
220         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=7654321 count=1
221         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 7654321
222
223         do_facet $SINGLEAGT $TRUNCATE $file 1234567 ||
224                 error "truncate failed"
225         check_lpcc_sizes $SINGLEAGT $lpcc_path $file 1234567
226         check_lpcc_state $file "readwrite"
227
228         do_facet $SINGLEAGT "echo -n file_data > $file"
229         check_lpcc_state $file "readwrite"
230         # HSM released exists archived status
231         check_hsm_flags $file "0x0000000d"
232         check_lpcc_data $SINGLEAGT $lpcc_path $file "file_data"
233
234         echo "Restore testing..."
235         if [ $CLIENTCOUNT -lt 2 -o $restore ]; then
236                 $LFS hsm_restore $file || error \
237                         "failed to restore $file"
238                 wait_request_state $(path2fid $file) RESTORE SUCCEED
239         else
240                 path_data=$(do_node $CLIENT2 cat $file)
241                 [[ "x$path_data" == "xfile_data" ]] || error \
242                         "expected file_data, got: $path_data"
243         fi
244
245         check_lpcc_state $file "none"
246         # HSM exists archived status
247         check_hsm_flags $file "0x00000009"
248
249         echo -n "new_data" > $file
250         check_lpcc_state $file "none"
251         # HSM exists dirty archived status
252         check_hsm_flags $file "0x0000000b"
253         check_file_data $SINGLEAGT $file "new_data"
254
255         echo "Attach and detach testing"
256         rm -f $file
257         do_facet $SINGLEAGT "echo -n new_data2 > $file"
258         if ! $project; then
259                 check_lpcc_state $file "none"
260                 do_facet $SINGLEAGT $LFS pcc attach -i \
261                         $HSM_ARCHIVE_NUMBER $file ||
262                         error "PCC attach $file failed"
263         fi
264         check_lpcc_state $file "readwrite"
265         # HSM released exists archived status
266         check_hsm_flags $file "0x0000000d"
267         do_facet $SINGLEAGT "echo -n attach_detach > $file"
268         echo "Start to detach the $file"
269         do_facet $SINGLEAGT $LFS pcc detach $file ||
270                 error "PCC detach $file failed"
271         wait_request_state $(path2fid $file) REMOVE SUCCEED
272
273         check_lpcc_state $file "none"
274         # The file is removed from PCC
275         check_hsm_flags $file "0x00000000"
276         check_file_data $SINGLEAGT $file "attach_detach"
277 }
278
279 test_1a() {
280         lpcc_rw_test true false
281 }
282 run_test 1a "Test manual lfs pcc attach with manual HSM restore"
283
284 test_1b() {
285         lpcc_rw_test false false
286 }
287 run_test 1b "Test manual lfs pcc attach with restore on remote access"
288
289 test_1c() {
290         lpcc_rw_test true true
291 }
292 run_test 1c "Test automated attach using Project ID with manual HSM restore"
293
294 test_1d() {
295         lpcc_rw_test false true
296 }
297 run_test 1d "Test Project ID with remote access"
298
299 test_1e() {
300         local file=$DIR/$tdir/$tfile
301         local hsm_root=$(hsm_root)
302         local -a lpcc_path
303
304         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
305         setup_pcc_mapping
306         $LCTL pcc list $MOUNT
307         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
308         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
309
310         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
311                 error "failed to dd write to $file"
312         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
313                 $file || error "failed to attach file $file"
314         check_lpcc_state $file "readwrite"
315         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
316                 error "failed to dd read from $file"
317         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
318                 error "failed to truncate $file"
319         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
320                 error "failed to truncate $file"
321         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
322                 error "failed to dd write to $file"
323         check_lpcc_state $file "readwrite"
324
325         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
326                 error "failed to detach file $file"
327         check_lpcc_state $file "none"
328
329         # non-root user is forbidden to access PCC file directly
330         lpcc_path=$(lpcc_fid2path $hsm_root $file)
331         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
332                 error "non-root user can touch access PCC file $lpcc_path"
333         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
334                 count=1 && error "non-root user can read PCC file $lpcc_path"
335         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
336                 count=1 && error "non-root user can write PCC file $lpcc_path"
337
338         local perm=$(do_facet $SINGLEAGT stat -c %a $lpcc_path)
339
340         [[ $perm == "0" ]] || error "PCC file permission ($perm) is not zero"
341
342         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
343                 $file || error "failed to attach file $file"
344         check_lpcc_state $file "readwrite"
345
346         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
347                 error "failed to detach file $file"
348         check_lpcc_state $file "none"
349         wait_request_state $(path2fid $file) REMOVE SUCCEED
350 }
351 run_test 1e "Test RW-PCC with non-root user"
352
353 test_1f() {
354         local project_id=100
355         local agt_facet=$SINGLEAGT
356         local hsm_root=$(hsm_root)
357         local file=$DIR/$tdir/$tfile
358
359         ! is_project_quota_supported &&
360                 skip "project quota is not supported"
361
362         enable_project_quota
363         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
364         setup_pcc_mapping
365         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
366         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
367         $LFS project -sp $project_id $DIR/$tdir ||
368                 error "failed to set project for $DIR/$tdir"
369
370         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
371                 error "failed to dd write to $file"
372
373         check_lpcc_state $file "readwrite"
374         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
375                 error "failed to dd read from $file"
376         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
377                 error "failed to truncate $file"
378         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
379                 error "failed to truncate $file"
380         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
381                 error "failed to dd write from $file"
382         check_lpcc_state $file "readwrite"
383
384         # non-root user is forbidden to access PCC file directly
385         lpcc_path=$(lpcc_fid2path $hsm_root $file)
386         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
387                 error "non-root user can touch access PCC file $lpcc_path"
388         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
389                 count=1 && error "non-root user can read PCC file $lpcc_path"
390         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
391                 count=1 && error "non-root user can write PCC file $lpcc_path"
392
393         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
394                 error "failed to detach file $file"
395         check_lpcc_state $file "none"
396         wait_request_state $(path2fid $file) REMOVE SUCCEED
397 }
398 run_test 1f "Test auto RW-PCC cache with non-root user"
399
400 test_1g() {
401         local file=$DIR/$tfile
402
403         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
404         setup_pcc_mapping
405
406         dd if=/dev/zero of=$file bs=1024 count=1 ||
407                 error "failed to dd write to $file"
408         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
409                 error "non-root user can dd write to $file"
410         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
411                 error "failed to attach file $file"
412         check_lpcc_state $file "readwrite"
413         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
414                 error "non-root user can dd write to $file"
415         chmod 777 $file || error "chmod 777 $file failed"
416         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
417                 error "non-root user cannot write $file with permission (777)"
418
419         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file &&
420                 error "non-root user or non owner can detach $file"
421         chown $RUNAS_ID $file || error "chown $RUNAS_ID $file failed"
422         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
423                 error "failed to detach file $file"
424         check_lpcc_state $file "none"
425         wait_request_state $(path2fid $file) REMOVE SUCCEED
426         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
427                 error "non-root user cannot read to $file with permisson (777)"
428 }
429 run_test 1g "General permission test for RW-PCC"
430
431 #
432 # When a process created a LPCC file and holding the open,
433 # another process on the same client should be able to open the file.
434 #
435 test_2a() {
436         local project_id=100
437         local agt_facet=$SINGLEAGT
438         local hsm_root=$(hsm_root)
439         local agt_host=$(facet_active_host $SINGLEAGT)
440
441         ! is_project_quota_supported &&
442                 skip "project quota is not supported" && return
443
444         enable_project_quota
445         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
446         setup_pcc_mapping
447         file=$DIR/$tdir/multiop
448         mkdir -p $DIR/$tdir
449         rm -f $file
450
451         do_facet $SINGLEAGT $LFS project -sp $project_id $DIR/$tdir ||
452                 error "failed to set project quota"
453         rmultiop_start $agt_host $file O_c || error "open $file failed"
454         # HSM released exists archived status
455         check_hsm_flags $file "0x0000000d"
456         do_facet $SINGLEAGT "echo -n multiopen_data > $file" ||
457                 error "failed to echo multiopen_data to $file"
458
459         lpcc_path=$(lpcc_fid2path $hsm_root $file)
460         do_facet $SINGLEAGT ls -l $lpcc_path ||
461                 error "failed to ls $lpcc_path"
462         check_lpcc_data $SINGLEAGT $lpcc_path $file "multiopen_data"
463         # HSM released exists archived status
464         check_hsm_flags $file "0x0000000d"
465
466         rmultiop_stop $agt_host || error "close $file failed"
467 }
468 run_test 2a "Test multi open when creating"
469
470 get_remote_client() {
471         current_id=$(do_facet $SINGLEAGT hostname)
472         for client in ${CLIENTS//,/ }
473         do
474                 r_id=$(do_node $client hostname)
475                 if [ $r_id != $current_id ]; then
476                         echo $client
477                         return
478                 fi
479         done
480 }
481
482 #
483 # When a process created a LPCC file and holding the open, another
484 # process on the different client should be able to open the file
485 # and perform IO on the file.
486 #
487 test_2b() {
488         local agt_facet=$SINGLEAGT
489         local hsm_root=$(hsm_root)
490         local agt_host=$(facet_active_host $SINGLEAGT)
491
492         needclients 2 || return 0
493
494         remote_client=$(get_remote_client)
495
496         enable_project_quota
497         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
498         setup_pcc_mapping
499         file=$DIR/$tdir/multiop
500         mkdir -p $DIR/$tdir
501         rm -f $file
502
503         do_facet $SINGLEAGT "echo -n file_data > $file"
504         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
505                 $file || error "PCC attach $file failed"
506         check_lpcc_state $file "readwrite"
507
508         rmultiop_start $agt_host $file O_c || error "open $file failed"
509
510         do_node $remote_client "echo -n multiopen_data > $file"
511
512         # PCC cached file should be automatically detached
513         check_lpcc_state $file "none"
514
515         check_file_data $SINGLEAGT $file "multiopen_data"
516         rmultiop_stop $agt_host || error "close $file failed"
517         check_file_data $SINGLEAGT $file "multiopen_data"
518
519         do_node $remote_client cat $file || error \
520                 "cat $file on remote client failed"
521         do_node $remote_client echo -n "multiopen_data" > $file \
522                 || error "write $file on remote client failed"
523 }
524 run_test 2b "Test multi remote open when creating"
525
526 test_2c() {
527         local agt_host=$(facet_active_host $SINGLEAGT)
528         local file=$DIR/$tdir/$tfile
529         local file2=$DIR2/$tdir/$tfile
530
531         enable_project_quota
532         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
533         setup_pcc_mapping
534         mkdir -p $DIR/$tdir
535         rm -f $file
536
537         do_facet $SINGLEAGT "echo -n file_data > $file"
538         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
539                 $file || error "PCC attach $file failed"
540         check_lpcc_state $file "readwrite"
541
542         rmultiop_start $agt_host $file O_c || error "open $file failed"
543
544         echo -n multiopen_data > $file2
545
546         # PCC cached file should be automatically detached
547         check_lpcc_state $file "none"
548
549         check_file_data $SINGLEAGT $file "multiopen_data"
550         rmultiop_stop $agt_host || error "close $file failed"
551         check_file_data $SINGLEAGT $file "multiopen_data"
552
553         cat $file2 || error "cat $file on mount $MOUNT2 failed"
554         echo -n "multiopen_data" > $file2 ||
555                 error "write $file on mount $MOUNT2 failed"
556 }
557 run_test 2c "Test multi open on different mount points when creating"
558
559 test_3a() {
560         local file=$DIR/$tdir/$tfile
561
562         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
563         setup_pcc_mapping
564
565         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
566         dd if=/dev/zero of=$file bs=1024 count=1 ||
567                 error "failed to dd write to $file"
568
569         echo "Start to attach/detach the file: $file"
570         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
571                 error "failed to attach file $file"
572         check_lpcc_state $file "readwrite"
573         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
574                 error "failed to detach file $file"
575         check_lpcc_state $file "none"
576
577         echo "Repeat to attach/detach the same file: $file"
578         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
579                 error "failed to attach file $file"
580         check_lpcc_state $file "readwrite"
581         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
582                 error "failed to detach file $file"
583         check_lpcc_state $file "none"
584 }
585 run_test 3a "Repeat attach/detach operations"
586
587 test_3b() {
588         local n
589         local file=$DIR/$tdir/$tfile
590
591         needclients 3 || return 0
592
593         # Start all of the copytools and setup PCC
594         for n in $(seq $AGTCOUNT); do
595                 copytool setup -f agt$n -a $n -m $MOUNT
596                 setup_pcc_mapping agt$n "projid={100}\ rwid=$n"
597         done
598
599         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
600         dd if=/dev/zero of=$file bs=1024 count=1 ||
601                 error "failed to dd write to $file"
602
603         echo "Start to attach/detach $file on $agt1_HOST"
604         do_facet agt1 $LFS pcc attach -i 1 $file ||
605                 error "failed to attach file $file"
606         check_lpcc_state $file "readwrite" agt1
607         do_facet agt1 $LFS pcc detach -k $file ||
608                 error "failed to detach file $file"
609         check_lpcc_state $file "none" agt1
610
611         echo "Repeat to attach/detach $file on $agt2_HOST"
612         do_facet agt2 $LFS pcc attach -i 2 $file ||
613                 error "failed to attach file $file"
614         check_lpcc_state $file "readwrite" agt2
615         do_facet agt2 $LFS pcc detach -k $file ||
616                 error "failed to detach file $file"
617         check_lpcc_state $file "none" agt2
618
619         echo "Try attach on two agents"
620         do_facet agt1 $LFS pcc attach -i 1 $file ||
621                 error "failed to attach file $file"
622         check_lpcc_state $file "readwrite" agt1
623         do_facet agt2 $LFS pcc attach -i 2 $file ||
624                 error "failed to attach file $file"
625         check_lpcc_state $file "readwrite" agt2
626         # The later attach PCC agent should succeed,
627         # the former agent should be detached automatically.
628         check_lpcc_state $file "none" agt1
629         do_facet agt2 $LFS pcc detach -k $file ||
630                 error "failed to detach file $file"
631         check_lpcc_state $file "none" agt2
632 }
633 run_test 3b "Repeat attach/detach operations on multiple clients"
634
635 test_4() {
636         local project_id=100
637
638         ! is_project_quota_supported &&
639                 skip "project quota is not supported" && return
640
641         enable_project_quota
642         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
643         setup_pcc_mapping
644
645         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
646         lfs project -sp $project_id $DIR/$tdir ||
647                 error "lfs project -sp $project_id $DIR/$tdir failed"
648
649         # mmap_sanity tst7 failed on the local ext4 filesystem.
650         # It seems that Lustre filesystem does special process for tst 7.
651         # Thus, we exclude tst7 from the PCC testing.
652         $LUSTRE/tests/mmap_sanity -d $DIR/$tdir -m $DIR2/$tdir -e 7 ||
653                 error "mmap_sanity test failed"
654         sync; sleep 1; sync
655 }
656 run_test 4 "Auto cache test for mmap"
657
658 test_5() {
659         local file=$DIR/$tfile
660
661         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
662         setup_pcc_mapping
663
664         do_facet $SINGLEAGT "echo -n attach_mmap_data > $file" ||
665                 error "echo $file failed"
666
667         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
668                 error "failed to attach file $file"
669         check_lpcc_state $file "readwrite"
670
671         local content=$($MMAP_CAT $file)
672
673         [[ $content == "attach_mmap_data" ]] ||
674                 error "mmap cat data mismatch: $content"
675
676         $LFS hsm_restore $file || error "failed to restore $file"
677         wait_request_state $(path2fid $file) RESTORE SUCCEED
678         check_lpcc_state $file "none"
679
680         content=$($MMAP_CAT $file)
681         [[ $content == "attach_mmap_data" ]] ||
682                 error "mmap cat data mismatch: $content"
683 }
684 run_test 5 "Mmap & cat a RW-PCC cached file"
685
686 setup_loopdev() {
687         local facet=$1
688         local file=$2
689         local mntpt=$3
690         local size=${4:-50}
691
692         do_facet $facet mkdir -p $mntpt || error "mkdir -p $hsm_root failed"
693         stack_trap "do_facet $facet rm -rf $mntpt" EXIT
694         do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
695         stack_trap "do_facet $facet rm -f $file" EXIT
696         do_facet $facet mkfs.ext4 $file ||
697                 error "mkfs.ext4 $file failed"
698         do_facet $facet file $file
699         do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
700                 error "mount -o loop,usrquota,grpquota $file $mntpt failed"
701         stack_trap "do_facet $facet $UMOUNT $mntpt" EXIT
702 }
703
704 test_6() {
705         local loopfile="$TMP/$tfile"
706         local mntpt="/mnt/pcc.$tdir"
707         local hsm_root="$mntpt/$tdir"
708         local file=$DIR/$tfile
709         local content
710
711         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
712         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
713         setup_pcc_mapping
714
715         echo -n mmap_write_data > $file || error "echo write $file failed"
716         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
717                 error "failed to attach file $file"
718         check_lpcc_state $file "readwrite"
719
720         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
721                 error "could not mmap $file"
722         check_lpcc_state $file "readwrite"
723         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
724         # After mmap write via multiop, the first character of each page
725         # increases with 1.
726         [[ $content == "nmap_write_data" ]] ||
727                 error "mmap write data mismatch: $content"
728         check_lpcc_state $file "readwrite"
729
730         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
731                 error "failed to detach file $file"
732
733         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
734         [[ $content == "nmap_write_data" ]] ||
735                 error "mmap write data mismatch: $content"
736 }
737 run_test 6 "Test mmap write on RW-PCC "
738
739 test_7a() {
740         local loopfile="$TMP/$tfile"
741         local mntpt="/mnt/pcc.$tdir"
742         local hsm_root="$mntpt/$tdir"
743         local file=$DIR/$tfile
744         local content
745
746         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
747         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
748         setup_pcc_mapping
749
750         echo "QQQQQ" > $file
751         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
752                 error "failed to attach file $file"
753         check_lpcc_state $file "readwrite"
754         check_file_data $SINGLEAGT $file "QQQQQ"
755         # define OBD_FAIL_LLITE_PCC_DETACH_MKWRITE      0x1412
756         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1412
757         # HSM released exists archived status
758         check_hsm_flags $file "0x0000000d"
759
760         # multiop mmap write increase the first character of each page with 1
761         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
762                 error "mmap write $file failed"
763         check_lpcc_state $file "none"
764         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
765         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
766 }
767 run_test 7a "Fake file detached between fault() and page_mkwrite() for RW-PCC"
768
769 test_7b() {
770         local loopfile="$TMP/$tfile"
771         local mntpt="/mnt/pcc.$tdir"
772         local hsm_root="$mntpt/$tdir"
773         local file=$DIR/$tfile
774         local content
775         local pid
776
777         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
778         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
779         setup_pcc_mapping
780
781         echo "QQQQQ" > $file
782         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
783                 error "failed to attach file $file"
784         check_lpcc_state $file "readwrite"
785         check_file_data $SINGLEAGT $file "QQQQQ"
786         # define OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE       0x1413
787         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1413 fail_val=20
788         # HSM released exists archived status
789         check_hsm_flags $file "0x0000000d"
790
791         # multiop mmap write increases the first character of each page with 1
792         do_facet $SINGLEAGT $MULTIOP $file OSMWUc &
793         pid=$!
794
795         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
796                 error "failed to detach file $file"
797
798         wait $pid || error "multiop mmap write failed"
799         check_lpcc_state $file "none"
800         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
801         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
802 }
803 run_test 7b "Test the race with concurrent mkwrite and detach"
804
805 test_8() {
806         local file=$DIR/$tfile
807
808         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
809         setup_pcc_mapping
810
811         echo "QQQQQ" > $file
812         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
813                 error "failed to attach file $file"
814         check_lpcc_state $file "readwrite"
815         check_file_data $SINGLEAGT $file "QQQQQ"
816
817         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
818         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
819         do_facet $SINGLEAGT "echo -n ENOSPC_write > $file"
820         # Above write will return -ENOSPC failure and retry the IO on normal
821         # IO path. It will restore the HSM released file.
822         check_lpcc_state $file "none"
823         check_file_data $SINGLEAGT $file "ENOSPC_write"
824 }
825 run_test 8 "Test fake -ENOSPC tolerance for RW-PCC"
826
827 test_9() {
828         local loopfile="$TMP/$tfile"
829         local mntpt="/mnt/pcc.9a"
830         local hsm_root="$mntpt/$tdir"
831         local file=$DIR/$tfile
832
833         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
834
835         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
836         setup_pcc_mapping
837         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
838
839         touch $file || error "touch $file failed"
840         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
841                 error "fail to attach $file"
842         check_lpcc_state $file "readwrite"
843         # write 60M data, it is larger than the capacity of PCC backend
844         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1M count=60 ||
845                 error "fail to dd write $file"
846         check_lpcc_state $file "none"
847         check_file_size $SINGLEAGT $file 62914560
848 }
849 run_test 9 "Test -ENOSPC tolerance on loop PCC device for RW-PCC"
850
851 test_usrgrp_quota() {
852         local loopfile="$TMP/$tfile"
853         local mntpt="/mnt/pcc.$tdir"
854         local hsm_root="$mntpt/$tdir"
855         local ug=$1
856         local id=$RUNAS_ID
857
858         [[ $ug == "g" ]] && id=$RUNAS_GID
859
860         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
861         do_facet $SINGLEAGT quotacheck -c$ug $mntpt ||
862                 error "quotacheck -c$ug $mntpt failed"
863         do_facet $SINGLEAGT quotaon -$ug $mntpt ||
864                 error "quotaon -$ug $mntpt failed"
865         do_facet $SINGLEAGT setquota -$ug $id 0 20480 0 0 $mntpt ||
866                 error "setquota -$ug $id on $mntpt failed"
867         do_facet $SINGLEAGT repquota -${ug}vs $mntpt
868
869         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
870         setup_pcc_mapping
871         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
872
873         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
874
875         local file1=$DIR/$tdir/${ug}quotaA
876         local file2=$DIR/$tdir/${ug}quotaB
877
878         dd if=/dev/zero of=$file1 bs=1M count=15 ||
879                 error "dd write $file1 failed"
880         dd if=/dev/zero of=$file2 bs=1M count=15 ||
881                 error "dd write $file2 failed"
882         chown $RUNAS_ID:$RUNAS_GID $file1 ||
883                 error "chown $RUNAS_ID:$RUNAS_GID $file1 failed"
884         chown $RUNAS_ID:$RUNAS_GID $file2 ||
885                 error "chown $RUNAS_ID:$RUNAS_GID $file2 failed"
886         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
887                 $file1 || error "attach $file1 failed"
888         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
889                 $file2 && error "attach $file2 should fail due to quota limit"
890         check_lpcc_state $file1 "readwrite"
891         check_lpcc_state $file2 "none"
892
893         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file1 bs=1M count=30 ||
894                 error "dd write $file1 failed"
895         # -EDQUOT error should be tolerated via fallback to normal Lustre path.
896         check_lpcc_state $file1 "none"
897         do_facet $SINGLEAGT $LFS pcc detach -k $file1 ||
898                 error "failed to detach file $file"
899         rm $file1 $file2
900 }
901
902 test_10a() {
903         test_usrgrp_quota "u"
904 }
905 run_test 10a "Test RW-PCC with user quota on loop PCC device"
906
907 test_10b() {
908         test_usrgrp_quota "g"
909 }
910 run_test 10b "Test RW-PCC with group quota on loop PCC device"
911
912 test_11() {
913         local loopfile="$TMP/$tfile"
914         local mntpt="/mnt/pcc.$tdir"
915         local hsm_root="$mntpt/$tdir"
916         local file=$DIR/$tfile
917         local -a lpcc_path
918         local lpcc_dir
919
920         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
921         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
922         setup_pcc_mapping
923
924         do_facet $SINGLEAGT "echo -n QQQQQ > $file"
925         lpcc_path=$(lpcc_fid2path $hsm_root $file)
926         lpcc_dir=$(dirname $lpcc_path)
927         echo "Lustre file: $file LPCC dir: $lpcc_dir"
928         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
929                 error "mkdir -p $lpcc_dir failed"
930         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
931                 error "failed to attach $file"
932         check_lpcc_state $file "readwrite"
933         check_file_data $SINGLEAGT $file "QQQQQ"
934         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
935                 error "failed to detach $file"
936         rm $file || error "rm $file failed"
937
938         # The parent directory of the PCC file is immutable
939         do_facet $SINGLEAGT "echo -n immutable_dir > $file"
940         lpcc_path=$(lpcc_fid2path $hsm_root $file)
941         lpcc_dir=$(dirname $lpcc_path)
942         echo "Lustre file: $file LPCC dir: $lpcc_dir"
943         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
944                 error "mkdir -p $lpcc_dir failed"
945         do_facet $SINGLEAGT chattr +i $lpcc_dir ||
946                 error "chattr +i $lpcc_dir failed"
947         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
948                 error "attach $file with immutable directory should be failed"
949         do_facet $SINGLEAGT chattr -i $lpcc_dir ||
950                 error "chattr -i $lpcc_dir failed"
951         rm $file || error "rm $file failed"
952
953         # The PCC file path is set to a directory
954         do_facet $SINGLEAGT "echo -n pcc_file_path_is_dir > $file"
955         lpcc_path=$(lpcc_fid2path $hsm_root $file)
956         do_facet $SINGLEAGT mkdir -p $lpcc_path ||
957                 error "mkdir -p $lpcc_path failed"
958         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
959                 error "attach $file should fail as PCC path is a directory"
960         rm $file || error "rm $file failed"
961 }
962 run_test 11 "Test attach fault injection with simulated PCC file path"
963
964 test_12() {
965         local file=$DIR/$tfile
966         local hsm_root=$(hsm_root)
967         local -a lpcc_path
968         local pid
969
970         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
971         setup_pcc_mapping
972
973         echo  -n race_rw_attach_hsmremove > $file
974         lpcc_path=$(lpcc_fid2path $hsm_root $file)
975         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
976                 error "attach $file failed"
977         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
978                 error "detach $file failed"
979         # HSM released exists archived status
980         check_hsm_flags $file "0x0000000d"
981         # define OBD_FAIL_LLITE_PCC_ATTACH_PAUSE        0x1414
982         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1414 fail_val=20
983         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &
984         pid=$!
985         $LFS hsm_state $file
986         sleep 3
987         wait_request_state $(path2fid $file) RESTORE SUCCEED
988         $LFS hsm_remove $file || error "hsm remove $file failed"
989         wait $pid && error "RW-PCC attach $file should fail"
990         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
991                 error "RW-PCC cached file '$lpcc_path' should be removed"
992
993         return 0
994 }
995 run_test 12 "RW-PCC attach races with concurrent HSM remove"
996
997 test_rule_id() {
998         local idstr="${1}id"
999         local rule="${idstr}={$2}"
1000         local myRUNAS="$3"
1001         local file=$DIR/$tdir/$tfile
1002
1003         setup_pcc_mapping $SINGLEAGT "$rule\ rwid=$HSM_ARCHIVE_NUMBER"
1004         $LCTL pcc list $MOUNT
1005
1006         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1007         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1008
1009         rm -f $file || error "rm $file failed"
1010         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1011                 error "failed to dd write to $file"
1012         check_lpcc_state $file "readwrite"
1013         do_facet $SINGLEAGT $myRUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
1014                 error "failed to dd read from $file"
1015         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 256 ||
1016                 error "failed to truncate $file"
1017         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 2048 ||
1018                 error "failed to truncate $file"
1019         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1020                 error "failed to dd write from $file"
1021         check_lpcc_state $file "readwrite"
1022
1023         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1024                 error "failed to detach file $file"
1025         check_lpcc_state $file "none"
1026 }
1027
1028 test_13a() {
1029         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1030         test_rule_id "u" "500" "runas -u 500"
1031         test_rule_id "g" "500" "runas -u 500 -g 500"
1032 }
1033 run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
1034
1035 test_13b() {
1036         local file
1037
1038         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1039         setup_pcc_mapping $SINGLEAGT \
1040                 "fname={*.h5\ suffix.*\ Mid*dle}\ rwid=$HSM_ARCHIVE_NUMBER"
1041         $LCTL pcc list $MOUNT
1042
1043         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1044         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1045
1046         file=$DIR/$tdir/prefix.h5
1047         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1048                 error "failed to dd write to $file"
1049         check_lpcc_state $file "readwrite"
1050         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1051                 error "failed to detach file $file"
1052         check_lpcc_state $file "none"
1053         rm $file || error "rm $file failed"
1054
1055         file=$DIR/$tdir/suffix.doc
1056         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1057                 error "failed to dd write to $file"
1058         check_lpcc_state $file "readwrite"
1059         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1060                 error "failed to detach file $file"
1061         check_lpcc_state $file "none"
1062         rm $file || error "rm $file failed"
1063
1064         file=$DIR/$tdir/MidPADdle
1065         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1066                 error "failed to dd write to $file"
1067         check_lpcc_state $file "readwrite"
1068         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
1069                 error "failed to detach file $file"
1070         check_lpcc_state $file "none"
1071         rm $file || error "rm $file failed"
1072
1073         file=$DIR/$tdir/Midpad
1074         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1075                 error "failed to dd write to $file"
1076         check_lpcc_state $file "none"
1077         rm $file || error "rm $file failed"
1078 }
1079 run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
1080
1081 test_13c() {
1082         local file
1083         local myRUNAS
1084
1085         ! is_project_quota_supported &&
1086                 echo "Skip project quota is not supported" && return 0
1087
1088         enable_project_quota
1089         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1090         setup_pcc_mapping $SINGLEAGT \
1091                 "projid={100\ 200}\&fname={*.h5},uid={500}\&gid={1000}\ rwid=$HSM_ARCHIVE_NUMBER"
1092         $LCTL pcc list $MOUNT
1093         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1094         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1095
1096         mkdir -p $DIR/$tdir/proj || error "mkdir $DIR/$tdir/proj failed"
1097         mkdir -p $DIR/$tdir/proj2 || error "mkdir $DIR/$tdir/proj2 failed"
1098         $LFS project -sp 100 $DIR/$tdir/proj ||
1099                 error "failed to set project for $DIR/$tdir/proj"
1100         $LFS project -sp 200 $DIR/$tdir/proj2 ||
1101                 error "failed to set project for $DIR/$tdir/proj2"
1102
1103         file=$DIR/$tdir/proj/notcache
1104         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1105                 error "failed to dd write to $file"
1106         check_lpcc_state $file "none"
1107         rm $file || error "rm $file failed"
1108
1109         file=$DIR/$tdir/proj/autocache.h5
1110         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1111                 error "failed to dd write to $file"
1112         check_lpcc_state $file "readwrite"
1113         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1114                 error "failed to detach $file"
1115         rm $file || error "rm $file failed"
1116
1117         file=$DIR/$tdir/proj2/notcache
1118         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1119                 error "failed to dd write to $file"
1120         check_lpcc_state $file "none"
1121         rm $file || error "rm $file failed"
1122
1123         file=$DIR/$tdir/proj2/autocache.h5
1124         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1125                 error "failed to dd write to $file"
1126         check_lpcc_state $file "readwrite"
1127         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1128                 error "failed to detach $file"
1129         rm $file || error "rm $file failed"
1130
1131         file=$DIR/$tdir/ugidcache
1132         myRUNAS="runas -u 500 -g 1000"
1133         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1134                 error "failed to dd write to $file"
1135         check_lpcc_state $file "readwrite"
1136         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1137                 error "failed to detach $file"
1138         rm $file || error "rm $file failed"
1139 }
1140 run_test 13c "Check auto RW-PCC create caching for UID/GID/ProjID/fname rule"
1141
1142 test_14() {
1143         local file=$DIR/$tdir/$tfile
1144
1145         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1146         setup_pcc_mapping
1147
1148         mkdir -p $DIR/$tdir || error "mkdir -p $DIR/$tdir failed"
1149         do_facet $SINGLEAGT "echo -n autodetach_data > $file"
1150         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1151                 $file || error "PCC attach $file failed"
1152         check_lpcc_state $file "readwrite"
1153
1154         # Revoke the layout lock, the PCC-cached file will be
1155         # detached automatically.
1156         do_facet $SINGLEAGT $LCTL \
1157                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1158         check_file_data $SINGLEAGT $file "autodetach_data"
1159         check_lpcc_state $file "none"
1160 }
1161 run_test 14 "Revocation of the layout lock should detach the file automatically"
1162
1163 test_15() {
1164         local loopfile="$TMP/$tfile"
1165         local mntpt="/mnt/pcc.$tdir"
1166         local hsm_root="$mntpt/$tdir"
1167         local file=$DIR/$tdir/$tfile
1168
1169         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1170         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1171         setup_pcc_mapping $SINGLEAGT \
1172                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1"
1173
1174         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1175         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
1176
1177         echo "Check open attach for non-root user"
1178         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1179                 error "failed to dd write to $file"
1180         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1181                 $file || error "failed to attach file $file"
1182         do_facet $SINGLEAGT $RUNAS $LFS pcc state $file
1183         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1184         # Revoke the layout lock, the PCC-cached file will be
1185         # detached automatically.
1186         do_facet $SINGLEAGT $LCTL \
1187                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1188         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1189         # Detach the file but keep the cache , as the file layout generation
1190         # is not changed, so the file is still valid cached in PCC, and can
1191         # be reused from PCC cache directly.
1192         do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
1193                 error "PCC detach $file failed"
1194         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1195         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
1196                 error "PCC detach $file failed"
1197         rm $file || error "rm $file failed"
1198
1199         echo "check open attach for root user"
1200         do_facet $SINGLEAGT "echo -n autoattach_data > $file"
1201         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1202                 $file || error "PCC attach $file failed"
1203         check_lpcc_state $file "readwrite"
1204
1205         # Revoke the layout lock, the PCC-cached file will be
1206         # detached automatically.
1207         do_facet $SINGLEAGT $LCTL \
1208                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1209         check_file_data $SINGLEAGT $file "autoattach_data"
1210         check_lpcc_state $file "readwrite"
1211
1212         # Detach the file with -k option, as the file layout generation
1213         # is not changed, so the file is still valid cached in PCC,
1214         # and can be reused from PCC cache directly.
1215         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1216                 error "PCC detach $file failed"
1217         check_lpcc_state $file "readwrite"
1218         # HSM released exists archived status
1219         check_hsm_flags $file "0x0000000d"
1220         check_file_data $SINGLEAGT $file "autoattach_data"
1221
1222         # HSM restore the PCC cached file, the layout generation
1223         # was changed, so the file can not be auto attached.
1224         $LFS hsm_restore $file || error "failed to restore $file"
1225         wait_request_state $(path2fid $file) RESTORE SUCCEED
1226         check_lpcc_state $file "none"
1227         # HSM exists archived status
1228         check_hsm_flags $file "0x00000009"
1229
1230 }
1231 run_test 15 "Test auto attach at open when file is still valid cached"
1232
1233 test_16() {
1234         local loopfile="$TMP/$tfile"
1235         local mntpt="/mnt/pcc.$tdir"
1236         local hsm_root="$mntpt/$tdir"
1237         local file=$DIR/$tfile
1238         local -a lpcc_path
1239
1240         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1241         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1242         setup_pcc_mapping $SINGLEAGT \
1243                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1"
1244
1245         do_facet $SINGLEAGT "echo -n detach_data > $file"
1246         lpcc_path=$(lpcc_fid2path $hsm_root $file)
1247         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1248                 $file || error "PCC attach $file failed"
1249         check_lpcc_state $file "readwrite"
1250         # HSM released exists archived status
1251         check_hsm_flags $file "0x0000000d"
1252
1253         echo "Test for reusing valid PCC cache"
1254         # Valid PCC cache can be reused
1255         do_facet $SINGLEAGT $LFS pcc detach -k $file ||
1256                 error "PCC detach $file failed"
1257         check_lpcc_state $file "readwrite"
1258         # HSM released exists archived status
1259         check_hsm_flags $file "0x0000000d"
1260
1261         echo "Test for the default detach"
1262         # Permanent detach by default, it will remove the PCC copy
1263         do_facet $SINGLEAGT $LFS pcc detach $file ||
1264                 error "PCC detach $file failed"
1265         wait_request_state $(path2fid $file) REMOVE SUCCEED
1266         check_lpcc_state $file "none"
1267         # File is removed from PCC backend
1268         check_hsm_flags $file "0x00000000"
1269         do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
1270                 error "RW-PCC cached file '$lpcc_path' should be removed"
1271
1272         return 0
1273 }
1274 run_test 16 "Test detach with different options"
1275
1276 complete $SECONDS
1277 check_and_cleanup_lustre
1278 exit_status