1#!/system/bin/sh
2
3#
4# Copyright (C) 2019 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# This script will run as an pre-checkpointing cleanup for mounting f2fs
20# with checkpoint=disable, so that the first mount after the reboot will
21# be faster. It is unnecessary to run if the device does not use userdata
22# checkpointing on F2FS.
23
24# TARGET_SLOT="${1}"
25STATUS_FD="${2}"
26DIRTY_SEGMENTS_THRESHOLD=100
27
28SLEEP=5
29TIME=0
30MAX_TIME=3600
31
32NAME=`while read dev dir type opt; do
33  if [ /data = ${dir} -a f2fs = ${type} ]; then
34    real_dev=$(realpath $dev)
35    echo ${real_dev##*/}
36    break
37  fi
38done < /proc/mounts`
39if [ -z "${NAME}" ]; then
40  exit 0
41fi
42log -pi -t checkpoint_gc Turning on GC for ${NAME}
43read OLD_SLEEP < /sys/fs/f2fs/${NAME}/gc_urgent_sleep_time || exit 1
44echo 50 > /sys/fs/f2fs/${NAME}/gc_urgent_sleep_time || exit 1
45echo 1 > /sys/fs/f2fs/${NAME}/gc_urgent || exit 1
46
47read DIRTY_SEGMENTS_START < /sys/fs/f2fs/${NAME}/dirty_segments
48DIRTY_SEGMENTS=${DIRTY_SEGMENTS_START}
49TODO_SEGMENTS=$((${DIRTY_SEGMENTS_START}-${DIRTY_SEGMENTS_THRESHOLD}))
50while [ ${DIRTY_SEGMENTS} -gt ${DIRTY_SEGMENTS_THRESHOLD} ]; do
51  log -pi -t checkpoint_gc dirty segments:${DIRTY_SEGMENTS} \(threshold:${DIRTY_SEGMENTS_THRESHOLD}\)
52  PROGRESS=`echo "(${DIRTY_SEGMENTS_START}-${DIRTY_SEGMENTS})/${TODO_SEGMENTS}"|bc -l`
53  if [[ $PROGRESS == -* ]]; then
54      PROGRESS=0
55  fi
56  print -u${STATUS_FD} "global_progress ${PROGRESS}"
57  read DIRTY_SEGMENTS < /sys/fs/f2fs/${NAME}/dirty_segments
58  sleep ${SLEEP}
59  TIME=$((${TIME}+${SLEEP}))
60  if [ ${TIME} -gt ${MAX_TIME} ]; then
61    break
62  fi
63  # In case someone turns it off behind our back
64  echo 1 > /sys/fs/f2fs/${NAME}/gc_urgent
65done
66
67log -pi -t checkpoint_gc Turning off GC for ${NAME}
68echo 0 > /sys/fs/f2fs/${NAME}/gc_urgent
69echo ${OLD_SLEEP} > /sys/fs/f2fs/${NAME}/gc_urgent_sleep_time
70sync
71
72print -u${STATUS_FD} "global_progress 1.0"
73exit 0
74