Whamcloud - gitweb
LU-10092 pcc: auto attach during open for valid cache
[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         check_lpcc_state $file "none"
272         # HSM released exists archived status
273         check_hsm_flags $file "0x0000000d"
274         check_file_data $SINGLEAGT $file "attach_detach"
275 }
276
277 test_1a() {
278         lpcc_rw_test true false
279 }
280 run_test 1a "Test manual lfs pcc attach with manual HSM restore"
281
282 test_1b() {
283         lpcc_rw_test false false
284 }
285 run_test 1b "Test manual lfs pcc attach with restore on remote access"
286
287 test_1c() {
288         lpcc_rw_test true true
289 }
290 run_test 1c "Test automated attach using Project ID with manual HSM restore"
291
292 test_1d() {
293         lpcc_rw_test false true
294 }
295 run_test 1d "Test Project ID with remote access"
296
297 test_1e() {
298         local file=$DIR/$tdir/$tfile
299         local hsm_root=$(hsm_root)
300         local -a lpcc_path
301
302         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
303         setup_pcc_mapping
304         $LCTL pcc list $MOUNT
305         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
306         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
307
308         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
309                 error "failed to dd write to $file"
310         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
311                 $file || error "failed to attach file $file"
312         check_lpcc_state $file "readwrite"
313         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
314                 error "failed to dd read from $file"
315         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
316                 error "failed to truncate $file"
317         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
318                 error "failed to truncate $file"
319         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
320                 error "failed to dd write to $file"
321         check_lpcc_state $file "readwrite"
322
323         # non-root user is forbidden to access PCC file directly
324         lpcc_path=$(lpcc_fid2path $hsm_root $file)
325         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
326                 error "non-root user can touch access PCC file $lpcc_path"
327         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
328                 count=1 && error "non-root user can read PCC file $lpcc_path"
329         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
330                 count=1 && error "non-root user can write PCC file $lpcc_path"
331
332         local perm=$(do_facet $SINGLEAGT stat -c %a $lpcc_path)
333
334         [[ $perm == "0" ]] || error "PCC file permission ($perm) is not zero"
335
336         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
337                 error "failed to detach file $file"
338         check_lpcc_state $file "none"
339 }
340 run_test 1e "Test RW-PCC with non-root user"
341
342 test_1f() {
343         local project_id=100
344         local agt_facet=$SINGLEAGT
345         local hsm_root=$(hsm_root)
346         local file=$DIR/$tdir/$tfile
347
348         ! is_project_quota_supported &&
349                 skip "project quota is not supported"
350
351         enable_project_quota
352         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
353         setup_pcc_mapping
354         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
355         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
356         $LFS project -sp $project_id $DIR/$tdir ||
357                 error "failed to set project for $DIR/$tdir"
358
359         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
360                 error "failed to dd write to $file"
361
362         check_lpcc_state $file "readwrite"
363         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
364                 error "failed to dd read from $file"
365         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 256 ||
366                 error "failed to truncate $file"
367         do_facet $SINGLEAGT $RUNAS $TRUNCATE $file 2048 ||
368                 error "failed to truncate $file"
369         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
370                 error "failed to dd write from $file"
371         check_lpcc_state $file "readwrite"
372
373         # non-root user is forbidden to access PCC file directly
374         lpcc_path=$(lpcc_fid2path $hsm_root $file)
375         do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
376                 error "non-root user can touch access PCC file $lpcc_path"
377         do_facet $SINGLEAGT $RUNAS dd if=$lpcc_path of=/dev/null bs=1024 \
378                 count=1 && error "non-root user can read PCC file $lpcc_path"
379         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$lpcc_path bs=1024 \
380                 count=1 && error "non-root user can write PCC file $lpcc_path"
381
382         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
383                 error "failed to detach file $file"
384         check_lpcc_state $file "none"
385 }
386 run_test 1f "Test auto RW-PCC cache with non-root user"
387
388 test_1g() {
389         local file=$DIR/$tfile
390
391         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
392         setup_pcc_mapping
393
394         dd if=/dev/zero of=$file bs=1024 count=1 ||
395                 error "failed to dd write to $file"
396         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
397                 error "non-root user can dd write to $file"
398         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
399                 error "failed to attach file $file"
400         check_lpcc_state $file "readwrite"
401         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
402                 error "non-root user can dd write to $file"
403         chmod 777 $file || error "chmod 777 $file failed"
404         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
405                 error "non-root user cannot write $file with permission (777)"
406
407         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file &&
408                 error "non-root user or non owner can detach $file"
409         chown $RUNAS_ID $file || error "chown $RUNAS_ID $file failed"
410         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
411                 error "failed to detach file $file"
412         check_lpcc_state $file "none"
413         do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
414                 error "non-root user cannot read to $file with permisson (777)"
415 }
416 run_test 1g "General permission test for RW-PCC"
417
418 #
419 # When a process created a LPCC file and holding the open,
420 # another process on the same client should be able to open the file.
421 #
422 test_2a() {
423         local project_id=100
424         local agt_facet=$SINGLEAGT
425         local hsm_root=$(hsm_root)
426         local agt_host=$(facet_active_host $SINGLEAGT)
427
428         ! is_project_quota_supported &&
429                 skip "project quota is not supported" && return
430
431         enable_project_quota
432         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
433         setup_pcc_mapping
434         file=$DIR/$tdir/multiop
435         mkdir -p $DIR/$tdir
436         rm -f $file
437
438         do_facet $SINGLEAGT $LFS project -sp $project_id $DIR/$tdir ||
439                 error "failed to set project quota"
440         rmultiop_start $agt_host $file O_c || error "open $file failed"
441         # HSM released exists archived status
442         check_hsm_flags $file "0x0000000d"
443         do_facet $SINGLEAGT "echo -n multiopen_data > $file" ||
444                 error "failed to echo multiopen_data to $file"
445
446         lpcc_path=$(lpcc_fid2path $hsm_root $file)
447         do_facet $SINGLEAGT ls -l $lpcc_path ||
448                 error "failed to ls $lpcc_path"
449         check_lpcc_data $SINGLEAGT $lpcc_path $file "multiopen_data"
450         # HSM released exists archived status
451         check_hsm_flags $file "0x0000000d"
452
453         rmultiop_stop $agt_host || error "close $file failed"
454 }
455 run_test 2a "Test multi open when creating"
456
457 get_remote_client() {
458         current_id=$(do_facet $SINGLEAGT hostname)
459         for client in ${CLIENTS//,/ }
460         do
461                 r_id=$(do_node $client hostname)
462                 if [ $r_id != $current_id ]; then
463                         echo $client
464                         return
465                 fi
466         done
467 }
468
469 #
470 # When a process created a LPCC file and holding the open, another
471 # process on the different client should be able to open the file
472 # and perform IO on the file.
473 #
474 test_2b() {
475         local agt_facet=$SINGLEAGT
476         local hsm_root=$(hsm_root)
477         local agt_host=$(facet_active_host $SINGLEAGT)
478
479         needclients 2 || return 0
480
481         remote_client=$(get_remote_client)
482
483         enable_project_quota
484         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
485         setup_pcc_mapping
486         file=$DIR/$tdir/multiop
487         mkdir -p $DIR/$tdir
488         rm -f $file
489
490         do_facet $SINGLEAGT "echo -n file_data > $file"
491         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
492                 $file || error "PCC attach $file failed"
493         check_lpcc_state $file "readwrite"
494
495         rmultiop_start $agt_host $file O_c || error "open $file failed"
496
497         do_node $remote_client "echo -n multiopen_data > $file"
498
499         # PCC cached file should be automatically detached
500         check_lpcc_state $file "none"
501
502         check_file_data $SINGLEAGT $file "multiopen_data"
503         rmultiop_stop $agt_host || error "close $file failed"
504         check_file_data $SINGLEAGT $file "multiopen_data"
505
506         do_node $remote_client cat $file || error \
507                 "cat $file on remote client failed"
508         do_node $remote_client echo -n "multiopen_data" > $file \
509                 || error "write $file on remote client failed"
510 }
511 run_test 2b "Test multi remote open when creating"
512
513 test_2c() {
514         local agt_host=$(facet_active_host $SINGLEAGT)
515         local file=$DIR/$tdir/$tfile
516         local file2=$DIR2/$tdir/$tfile
517
518         enable_project_quota
519         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
520         setup_pcc_mapping
521         mkdir -p $DIR/$tdir
522         rm -f $file
523
524         do_facet $SINGLEAGT "echo -n file_data > $file"
525         do_facet $SINGLEAGT lfs pcc attach -i $HSM_ARCHIVE_NUMBER \
526                 $file || error "PCC attach $file failed"
527         check_lpcc_state $file "readwrite"
528
529         rmultiop_start $agt_host $file O_c || error "open $file failed"
530
531         echo -n multiopen_data > $file2
532
533         # PCC cached file should be automatically detached
534         check_lpcc_state $file "none"
535
536         check_file_data $SINGLEAGT $file "multiopen_data"
537         rmultiop_stop $agt_host || error "close $file failed"
538         check_file_data $SINGLEAGT $file "multiopen_data"
539
540         cat $file2 || error "cat $file on mount $MOUNT2 failed"
541         echo -n "multiopen_data" > $file2 ||
542                 error "write $file on mount $MOUNT2 failed"
543 }
544 run_test 2c "Test multi open on different mount points when creating"
545
546 test_3a() {
547         local file=$DIR/$tdir/$tfile
548
549         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
550         setup_pcc_mapping
551
552         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
553         dd if=/dev/zero of=$file bs=1024 count=1 ||
554                 error "failed to dd write to $file"
555
556         echo "Start to attach/detach the file: $file"
557         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
558                 error "failed to attach file $file"
559         check_lpcc_state $file "readwrite"
560         do_facet $SINGLEAGT $LFS pcc detach $file ||
561                 error "failed to detach file $file"
562         check_lpcc_state $file "none"
563
564         echo "Repeat to attach/detach the same file: $file"
565         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
566                 error "failed to attach file $file"
567         check_lpcc_state $file "readwrite"
568         do_facet $SINGLEAGT $LFS pcc detach $file ||
569                 error "failed to detach file $file"
570         check_lpcc_state $file "none"
571 }
572 run_test 3a "Repeat attach/detach operations"
573
574 test_3b() {
575         local n
576         local file=$DIR/$tdir/$tfile
577
578         needclients 3 || return 0
579
580         # Start all of the copytools and setup PCC
581         for n in $(seq $AGTCOUNT); do
582                 copytool setup -f agt$n -a $n -m $MOUNT
583                 setup_pcc_mapping agt$n "projid={100}\ rwid=$n"
584         done
585
586         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
587         dd if=/dev/zero of=$file bs=1024 count=1 ||
588                 error "failed to dd write to $file"
589
590         echo "Start to attach/detach $file on $agt1_HOST"
591         do_facet agt1 $LFS pcc attach -i 1 $file ||
592                 error "failed to attach file $file"
593         check_lpcc_state $file "readwrite" agt1
594         do_facet agt1 $LFS pcc detach $file ||
595                 error "failed to detach file $file"
596         check_lpcc_state $file "none" agt1
597
598         echo "Repeat to attach/detach $file on $agt2_HOST"
599         do_facet agt2 $LFS pcc attach -i 2 $file ||
600                 error "failed to attach file $file"
601         check_lpcc_state $file "readwrite" agt2
602         do_facet agt2 $LFS pcc detach $file ||
603                 error "failed to detach file $file"
604         check_lpcc_state $file "none" agt2
605
606         echo "Try attach on two agents"
607         do_facet agt1 $LFS pcc attach -i 1 $file ||
608                 error "failed to attach file $file"
609         check_lpcc_state $file "readwrite" agt1
610         do_facet agt2 $LFS pcc attach -i 2 $file ||
611                 error "failed to attach file $file"
612         check_lpcc_state $file "readwrite" agt2
613         # The later attach PCC agent should succeed,
614         # the former agent should be detached automatically.
615         check_lpcc_state $file "none" agt1
616         do_facet agt2 $LFS pcc detach $file ||
617                 error "failed to detach file $file"
618         check_lpcc_state $file "none" agt2
619 }
620 run_test 3b "Repeat attach/detach operations on multiple clients"
621
622 test_4() {
623         local project_id=100
624
625         ! is_project_quota_supported &&
626                 skip "project quota is not supported" && return
627
628         enable_project_quota
629         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
630         setup_pcc_mapping
631
632         mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
633         lfs project -sp $project_id $DIR/$tdir ||
634                 error "lfs project -sp $project_id $DIR/$tdir failed"
635
636         # mmap_sanity tst7 failed on the local ext4 filesystem.
637         # It seems that Lustre filesystem does special process for tst 7.
638         # Thus, we exclude tst7 from the PCC testing.
639         $LUSTRE/tests/mmap_sanity -d $DIR/$tdir -m $DIR2/$tdir -e 7 ||
640                 error "mmap_sanity test failed"
641         sync; sleep 1; sync
642 }
643 run_test 4 "Auto cache test for mmap"
644
645 test_5() {
646         local file=$DIR/$tfile
647
648         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
649         setup_pcc_mapping
650
651         do_facet $SINGLEAGT "echo -n attach_mmap_data > $file" ||
652                 error "echo $file failed"
653
654         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
655                 error "failed to attach file $file"
656         check_lpcc_state $file "readwrite"
657
658         local content=$($MMAP_CAT $file)
659
660         [[ $content == "attach_mmap_data" ]] ||
661                 error "mmap cat data mismatch: $content"
662
663         $LFS hsm_restore $file || error "failed to restore $file"
664         wait_request_state $(path2fid $file) RESTORE SUCCEED
665         check_lpcc_state $file "none"
666
667         content=$($MMAP_CAT $file)
668         [[ $content == "attach_mmap_data" ]] ||
669                 error "mmap cat data mismatch: $content"
670 }
671 run_test 5 "Mmap & cat a RW-PCC cached file"
672
673 setup_loopdev() {
674         local facet=$1
675         local file=$2
676         local mntpt=$3
677         local size=${4:-50}
678
679         do_facet $facet mkdir -p $mntpt || error "mkdir -p $hsm_root failed"
680         stack_trap "do_facet $facet rm -rf $mntpt" EXIT
681         do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
682         stack_trap "do_facet $facet rm -f $file" EXIT
683         do_facet $facet mkfs.ext4 $file ||
684                 error "mkfs.ext4 $file failed"
685         do_facet $facet file $file
686         do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
687                 error "mount -o loop,usrquota,grpquota $file $mntpt failed"
688         stack_trap "do_facet $facet $UMOUNT $mntpt" EXIT
689 }
690
691 test_6() {
692         local loopfile="$TMP/$tfile"
693         local mntpt="/mnt/pcc.$tdir"
694         local hsm_root="$mntpt/$tdir"
695         local file=$DIR/$tfile
696         local content
697
698         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
699         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
700         setup_pcc_mapping
701
702         echo -n mmap_write_data > $file || error "echo write $file failed"
703         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
704                 error "failed to attach file $file"
705         check_lpcc_state $file "readwrite"
706
707         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
708                 error "could not mmap $file"
709         check_lpcc_state $file "readwrite"
710         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
711         # After mmap write via multiop, the first character of each page
712         # increases with 1.
713         [[ $content == "nmap_write_data" ]] ||
714                 error "mmap write data mismatch: $content"
715         check_lpcc_state $file "readwrite"
716
717         do_facet $SINGLEAGT $LFS pcc detach $file ||
718                 error "failed to detach file $file"
719
720         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
721         [[ $content == "nmap_write_data" ]] ||
722                 error "mmap write data mismatch: $content"
723 }
724 run_test 6 "Test mmap write on RW-PCC "
725
726 test_7a() {
727         local loopfile="$TMP/$tfile"
728         local mntpt="/mnt/pcc.$tdir"
729         local hsm_root="$mntpt/$tdir"
730         local file=$DIR/$tfile
731         local content
732
733         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
734         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
735         setup_pcc_mapping
736
737         echo "QQQQQ" > $file
738         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
739                 error "failed to attach file $file"
740         check_lpcc_state $file "readwrite"
741         check_file_data $SINGLEAGT $file "QQQQQ"
742         # define OBD_FAIL_LLITE_PCC_DETACH_MKWRITE      0x1412
743         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1412
744         # HSM released exists archived status
745         check_hsm_flags $file "0x0000000d"
746
747         # multiop mmap write increase the first character of each page with 1
748         do_facet $SINGLEAGT $MULTIOP $file OSMWUc ||
749                 error "mmap write $file failed"
750         check_lpcc_state $file "none"
751         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
752         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
753 }
754 run_test 7a "Fake file detached between fault() and page_mkwrite() for RW-PCC"
755
756 test_7b() {
757         local loopfile="$TMP/$tfile"
758         local mntpt="/mnt/pcc.$tdir"
759         local hsm_root="$mntpt/$tdir"
760         local file=$DIR/$tfile
761         local content
762         local pid
763
764         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
765         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
766         setup_pcc_mapping
767
768         echo "QQQQQ" > $file
769         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
770                 error "failed to attach file $file"
771         check_lpcc_state $file "readwrite"
772         check_file_data $SINGLEAGT $file "QQQQQ"
773         # define OBD_FAIL_LLITE_PCC_MKWRITE_PAUSE       0x1413
774         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1413 fail_val=20
775         # HSM released exists archived status
776         check_hsm_flags $file "0x0000000d"
777
778         # multiop mmap write increase the first character of each page with 1
779         do_facet $SINGLEAGT $MULTIOP $file OSMWUc &
780         pid=$!
781
782         do_facet $SINGLEAGT $LFS pcc detach $file ||
783                 error "failed to detach file $file"
784
785         wait $pid || error "multiop mmap write failed"
786         check_lpcc_state $file "none"
787         content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
788         [[ $content == "RQQQQ" ]] || error "data mismatch: $content"
789 }
790 run_test 7b "Test the race with concurrent mkwrite and detach"
791
792 test_8() {
793         local file=$DIR/$tfile
794
795         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
796         setup_pcc_mapping
797
798         echo "QQQQQ" > $file
799         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
800                 error "failed to attach file $file"
801         check_lpcc_state $file "readwrite"
802         check_file_data $SINGLEAGT $file "QQQQQ"
803
804         # define OBD_FAIL_LLITE_PCC_FAKE_ERROR  0x1411
805         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1411
806         do_facet $SINGLEAGT "echo -n ENOSPC_write > $file"
807         # Above write will return -ENOSPC failure and retry the IO on normal
808         # IO path. It will restore the HSM released file.
809         check_lpcc_state $file "none"
810         check_file_data $SINGLEAGT $file "ENOSPC_write"
811 }
812 run_test 8 "Test fake -ENOSPC tolerance for RW-PCC"
813
814 test_9() {
815         local loopfile="$TMP/$tfile"
816         local mntpt="/mnt/pcc.9a"
817         local hsm_root="$mntpt/$tdir"
818         local file=$DIR/$tfile
819
820         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
821
822         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
823         setup_pcc_mapping
824         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
825
826         touch $file || error "touch $file failed"
827         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
828                 error "fail to attach $file"
829         check_lpcc_state $file "readwrite"
830         # write 60M data, it is larger than the capacity of PCC backend
831         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1M count=60 ||
832                 error "fail to dd write $file"
833         check_lpcc_state $file "none"
834         check_file_size $SINGLEAGT $file 62914560
835 }
836 run_test 9 "Test -ENOSPC tolerance on loop PCC device for RW-PCC"
837
838 test_usrgrp_quota() {
839         local loopfile="$TMP/$tfile"
840         local mntpt="/mnt/pcc.$tdir"
841         local hsm_root="$mntpt/$tdir"
842         local ug=$1
843         local id=$RUNAS_ID
844
845         [[ $ug == "g" ]] && id=$RUNAS_GID
846
847         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
848         do_facet $SINGLEAGT quotacheck -c$ug $mntpt ||
849                 error "quotacheck -c$ug $mntpt failed"
850         do_facet $SINGLEAGT quotaon -$ug $mntpt ||
851                 error "quotaon -$ug $mntpt failed"
852         do_facet $SINGLEAGT setquota -$ug $id 0 20480 0 0 $mntpt ||
853                 error "setquota -$ug $id on $mntpt failed"
854         do_facet $SINGLEAGT repquota -${ug}vs $mntpt
855
856         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMVER" -h "$hsm_root"
857         setup_pcc_mapping
858         do_facet $SINGLEAGT $LCTL pcc list $MOUNT
859
860         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
861
862         local file1=$DIR/$tdir/${ug}quotaA
863         local file2=$DIR/$tdir/${ug}quotaB
864
865         dd if=/dev/zero of=$file1 bs=1M count=15 ||
866                 error "dd write $file1 failed"
867         dd if=/dev/zero of=$file2 bs=1M count=15 ||
868                 error "dd write $file2 failed"
869         chown $RUNAS_ID:$RUNAS_GID $file1 ||
870                 error "chown $RUNAS_ID:$RUNAS_GID $file1 failed"
871         chown $RUNAS_ID:$RUNAS_GID $file2 ||
872                 error "chown $RUNAS_ID:$RUNAS_GID $file2 failed"
873         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
874                 $file1 || error "attach $file1 failed"
875         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
876                 $file2 && error "attach $file2 should fail due to quota limit"
877         check_lpcc_state $file1 "readwrite"
878         check_lpcc_state $file2 "none"
879
880         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file1 bs=1M count=30 ||
881                 error "dd write $file1 failed"
882         # -EDQUOT error should be tolerated via fallback to normal Lustre path.
883         check_lpcc_state $file1 "none"
884         do_facet $SINGLEAGT $LFS pcc detach $file1 ||
885                 error "failed to detach file $file"
886         rm $file1 $file2
887 }
888
889 test_10a() {
890         test_usrgrp_quota "u"
891 }
892 run_test 10a "Test RW-PCC with user quota on loop PCC device"
893
894 test_10b() {
895         test_usrgrp_quota "g"
896 }
897 run_test 10b "Test RW-PCC with group quota on loop PCC device"
898
899 test_11() {
900         local loopfile="$TMP/$tfile"
901         local mntpt="/mnt/pcc.$tdir"
902         local hsm_root="$mntpt/$tdir"
903         local file=$DIR/$tfile
904         local -a lpcc_path
905         local lpcc_dir
906
907         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
908         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
909         setup_pcc_mapping
910
911         do_facet $SINGLEAGT "echo -n QQQQQ > $file"
912         lpcc_path=$(lpcc_fid2path $hsm_root $file)
913         lpcc_dir=$(dirname $lpcc_path)
914         echo "Lustre file: $file LPCC dir: $lpcc_dir"
915         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
916                 error "mkdir -p $lpcc_dir failed"
917         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
918                 error "failed to attach $file"
919         check_lpcc_state $file "readwrite"
920         check_file_data $SINGLEAGT $file "QQQQQ"
921         do_facet $SINGLEAGT $LFS pcc detach $file ||
922                 error "failed to detach $file"
923         rm $file || error "rm $file failed"
924
925         # The parent directory of the PCC file is immutable
926         do_facet $SINGLEAGT "echo -n immutable_dir > $file"
927         lpcc_path=$(lpcc_fid2path $hsm_root $file)
928         lpcc_dir=$(dirname $lpcc_path)
929         echo "Lustre file: $file LPCC dir: $lpcc_dir"
930         do_facet $SINGLEAGT mkdir -p $lpcc_dir ||
931                 error "mkdir -p $lpcc_dir failed"
932         do_facet $SINGLEAGT chattr +i $lpcc_dir ||
933                 error "chattr +i $lpcc_dir failed"
934         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
935                 error "attach $file with immutable directory should be failed"
936         do_facet $SINGLEAGT chattr -i $lpcc_dir ||
937                 error "chattr -i $lpcc_dir failed"
938         rm $file || error "rm $file failed"
939
940         # The PCC file path is set to a directory
941         do_facet $SINGLEAGT "echo -n pcc_file_path_is_dir > $file"
942         lpcc_path=$(lpcc_fid2path $hsm_root $file)
943         do_facet $SINGLEAGT mkdir -p $lpcc_path ||
944                 error "mkdir -p $lpcc_path failed"
945         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &&
946                 error "attach $file should fail as PCC path is a directory"
947         rm $file || error "rm $file failed"
948 }
949 run_test 11 "Test attach fault injection with simulated PCC file path"
950
951 test_12() {
952         local file=$DIR/$tfile
953         local pid
954
955         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
956         setup_pcc_mapping
957
958         echo  -n race_rw_attach_hsmremove > $file
959         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
960                 error "attach $file failed"
961         do_facet $SINGLEAGT $LFS pcc detach $file || error "detach $file failed"
962         # HSM released exists archived status
963         check_hsm_flags $file "0x0000000d"
964         # define OBD_FAIL_LLITE_PCC_ATTACH_PAUSE        0x1414
965         do_facet $SINGLEAGT $LCTL set_param fail_loc=0x1414 fail_val=20
966         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file &
967         pid=$!
968         $LFS hsm_state $file
969         sleep 3
970         wait_request_state $(path2fid $file) RESTORE SUCCEED
971         $LFS hsm_remove $file || error "hsm remove $file failed"
972         wait $pid && error "RW-PCC attach $file should fail"
973
974         return 0
975 }
976 run_test 12 "RW-PCC attach races with concurrent HSM remove"
977
978 test_rule_id() {
979         local idstr="${1}id"
980         local rule="${idstr}={$2}"
981         local myRUNAS="$3"
982         local file=$DIR/$tdir/$tfile
983
984         setup_pcc_mapping $SINGLEAGT "$rule\ rwid=$HSM_ARCHIVE_NUMBER"
985         $LCTL pcc list $MOUNT
986
987         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
988         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
989
990         rm -f $file || error "rm $file failed"
991         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
992                 error "failed to dd write to $file"
993         check_lpcc_state $file "readwrite"
994         do_facet $SINGLEAGT $myRUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
995                 error "failed to dd read from $file"
996         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 256 ||
997                 error "failed to truncate $file"
998         do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 2048 ||
999                 error "failed to truncate $file"
1000         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1001                 error "failed to dd write from $file"
1002         check_lpcc_state $file "readwrite"
1003
1004         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1005                 error "failed to detach file $file"
1006         check_lpcc_state $file "none"
1007 }
1008
1009 test_13a() {
1010         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1011         test_rule_id "u" "500" "runas -u 500"
1012         test_rule_id "g" "500" "runas -u 500 -g 500"
1013 }
1014 run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
1015
1016 test_13b() {
1017         local file
1018
1019         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1020         setup_pcc_mapping $SINGLEAGT \
1021                 "fname={*.h5\ suffix.*\ Mid*dle}\ rwid=$HSM_ARCHIVE_NUMBER"
1022         $LCTL pcc list $MOUNT
1023
1024         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1025         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1026
1027         file=$DIR/$tdir/prefix.h5
1028         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1029                 error "failed to dd write to $file"
1030         check_lpcc_state $file "readwrite"
1031         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1032                 error "failed to detach file $file"
1033         check_lpcc_state $file "none"
1034         rm $file || error "rm $file failed"
1035
1036         file=$DIR/$tdir/suffix.doc
1037         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1038                 error "failed to dd write to $file"
1039         check_lpcc_state $file "readwrite"
1040         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1041                 error "failed to detach file $file"
1042         check_lpcc_state $file "none"
1043         rm $file || error "rm $file failed"
1044
1045         file=$DIR/$tdir/MidPADdle
1046         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1047                 error "failed to dd write to $file"
1048         check_lpcc_state $file "readwrite"
1049         do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
1050                 error "failed to detach file $file"
1051         check_lpcc_state $file "none"
1052         rm $file || error "rm $file failed"
1053
1054         file=$DIR/$tdir/Midpad
1055         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1056                 error "failed to dd write to $file"
1057         check_lpcc_state $file "none"
1058         rm $file || error "rm $file failed"
1059 }
1060 run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
1061
1062 test_13c() {
1063         local file
1064         local myRUNAS
1065
1066         ! is_project_quota_supported &&
1067                 echo "Skip project quota is not supported" && return 0
1068
1069         enable_project_quota
1070         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1071         setup_pcc_mapping $SINGLEAGT \
1072                 "projid={100\ 200}\&fname={*.h5},uid={500}\&gid={1000}\ rwid=$HSM_ARCHIVE_NUMBER"
1073         $LCTL pcc list $MOUNT
1074         do_facet $SINGLEAGT mkdir -p $DIR/$tdir
1075         chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
1076
1077         mkdir -p $DIR/$tdir/proj || error "mkdir $DIR/$tdir/proj failed"
1078         mkdir -p $DIR/$tdir/proj2 || error "mkdir $DIR/$tdir/proj2 failed"
1079         $LFS project -sp 100 $DIR/$tdir/proj ||
1080                 error "failed to set project for $DIR/$tdir/proj"
1081         $LFS project -sp 200 $DIR/$tdir/proj2 ||
1082                 error "failed to set project for $DIR/$tdir/proj2"
1083
1084         file=$DIR/$tdir/proj/notcache
1085         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1086                 error "failed to dd write to $file"
1087         check_lpcc_state $file "none"
1088         rm $file || error "rm $file failed"
1089
1090         file=$DIR/$tdir/proj/autocache.h5
1091         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1092                 error "failed to dd write to $file"
1093         check_lpcc_state $file "readwrite"
1094         do_facet $SINGLEAGT $LFS pcc detach $file ||
1095                 error "failed to detach $file"
1096         rm $file || error "rm $file failed"
1097
1098         file=$DIR/$tdir/proj2/notcache
1099         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1100                 error "failed to dd write to $file"
1101         check_lpcc_state $file "none"
1102         rm $file || error "rm $file failed"
1103
1104         file=$DIR/$tdir/proj2/autocache.h5
1105         do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
1106                 error "failed to dd write to $file"
1107         check_lpcc_state $file "readwrite"
1108         do_facet $SINGLEAGT $LFS pcc detach $file ||
1109                 error "failed to detach $file"
1110         rm $file || error "rm $file failed"
1111
1112         file=$DIR/$tdir/ugidcache
1113         myRUNAS="runas -u 500 -g 1000"
1114         do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1115                 error "failed to dd write to $file"
1116         check_lpcc_state $file "readwrite"
1117         do_facet $SINGLEAGT $LFS pcc detach $file ||
1118                 error "failed to detach $file"
1119         rm $file || error "rm $file failed"
1120 }
1121 run_test 13c "Check auto RW-PCC create caching for UID/GID/ProjID/fname rule"
1122
1123 test_14() {
1124         local file=$DIR/$tdir/$tfile
1125
1126         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1127         setup_pcc_mapping
1128
1129         mkdir -p $DIR/$tdir || error "mkdir -p $DIR/$tdir failed"
1130         do_facet $SINGLEAGT "echo -n autodetach_data > $file"
1131         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1132                 $file || error "PCC attach $file failed"
1133         check_lpcc_state $file "readwrite"
1134
1135         # Revoke the layout lock, the PCC-cached file will be
1136         # detached automatically.
1137         do_facet $SINGLEAGT $LCTL \
1138                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1139         check_file_data $SINGLEAGT $file "autodetach_data"
1140         check_lpcc_state $file "none"
1141 }
1142 run_test 14 "Revocation of the layout lock should detach the file automatically"
1143
1144 test_15() {
1145         local loopfile="$TMP/$tfile"
1146         local mntpt="/mnt/pcc.$tdir"
1147         local hsm_root="$mntpt/$tdir"
1148         local file=$DIR/$tdir/$tfile
1149
1150         setup_loopdev $SINGLEAGT $loopfile $mntpt 50
1151         copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
1152         setup_pcc_mapping $SINGLEAGT \
1153                 "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1"
1154
1155         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
1156         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
1157
1158         echo "Check open attach for non-root user"
1159         do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
1160                 error "failed to dd write to $file"
1161         do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1162                 $file || error "failed to attach file $file"
1163         do_facet $SINGLEAGT $RUNAS $LFS pcc state $file
1164         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1165         # Revoke the layout lock, the PCC-cached file will be
1166         # detached automatically.
1167         do_facet $SINGLEAGT $LCTL \
1168                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1169         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1170         # Detach the file directly, as the file layout generation
1171         # is not changed, so the file is still valid cached in PCC,
1172         # and can be reused from PCC cache directly.
1173         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
1174                 error "PCC detach $file failed"
1175         check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
1176         do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
1177                 error "PCC detach $file failed"
1178         rm $file || error "rm $file failed"
1179
1180         echo "check open attach for root user"
1181         do_facet $SINGLEAGT "echo -n autoattach_data > $file"
1182         do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
1183                 $file || error "PCC attach $file failed"
1184         check_lpcc_state $file "readwrite"
1185
1186         # Revoke the layout lock, the PCC-cached file will be
1187         # detached automatically.
1188         do_facet $SINGLEAGT $LCTL \
1189                 set_param ldlm.namespaces.*mdc*.lru_size=clear
1190         check_file_data $SINGLEAGT $file "autoattach_data"
1191         check_lpcc_state $file "readwrite"
1192
1193         # Detach the file directly, as the file layout generation
1194         # is not changed, so the file is still valid cached in PCC,
1195         # and can be reused from PCC cache directly.
1196         do_facet $SINGLEAGT $LFS pcc detach $file ||
1197                 error "PCC detach $file failed"
1198         check_lpcc_state $file "readwrite"
1199         # HSM released exists archived status
1200         check_hsm_flags $file "0x0000000d"
1201         check_file_data $SINGLEAGT $file "autoattach_data"
1202
1203         $LFS hsm_restore $file || error "failed to restore $file"
1204         wait_request_state $(path2fid $file) RESTORE SUCCEED
1205         check_lpcc_state $file "none"
1206 }
1207 run_test 15 "Test auto attach at open when file is still valid cached"
1208
1209 complete $SECONDS
1210 check_and_cleanup_lustre
1211 exit_status