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