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