Change speedcheck methods by using O_DIRECT read operations 92/171092/1
authorHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 26 Feb 2018 11:05:44 +0000 (20:05 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 26 Feb 2018 11:05:48 +0000 (20:05 +0900)
to prevent corruption in existing filesystem on MicroSD card.
Further work might be required to consider optimized zero-page reads in several MicroSD cards.

Change-Id: Ic26f9c3c5396bbad7b21683d1b08a72c6a8404a3
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/block/block.c

index 8c69605..a6267af 100644 (file)
 #define EXTENDEDSD_NAME                "extendedsd"
 
 /* Minimum value of block id */
-#define BLOCK_ID_MIN 10
+#define BLOCK_ID_MIN           10
 /* For 2.4 Backward Compatibility */
 #define EXT_PRIMARY_SD_FIXID   1
 
 /* Maximum number of thread */
-#define THREAD_MAX 5
+#define THREAD_MAX             5
 
-#define SPEEDCHECK 16
+#define SPEEDCHECK_SIZE                16
+#define SPEEDCHECK_CRITERION   4 /* MB/s */
 
 #define PKGDIR_BUS_NAME                "org.tizen.pkgdir_tool"
 #define PKGDIR_PATH            "/org/tizen/pkgdir_tool"
 #define PKGDIR_INTERFACE       "org.tizen.pkgdir_tool"
 
-#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
+#define ARRAY_SIZE(name)       (sizeof(name)/sizeof(name[0]))
 
 enum block_dev_operation {
        BLOCK_DEV_MOUNT,
@@ -3375,7 +3376,7 @@ static GVariant *request_check_speed(GDBusConnection *conn,
        struct timespec start_time, end_time;
        struct block_device *bdev;
        struct block_data *data;
-       char *buf;
+       char *buf = NULL;
        int ret;
        int result = 0;
        int id;
@@ -3396,36 +3397,35 @@ static GVariant *request_check_speed(GDBusConnection *conn,
        }
 
        _D("speed check: %s", data->devnode);
-       fd = open(data->devnode, O_RDWR | O_SYNC);
-       buf = calloc(1, SPEEDCHECK << 20);
-       if (!buf) {
-               _E("calloc() failed");
+       fd = open(data->devnode, O_RDONLY | O_DIRECT);
+       ret = posix_memalign((void**)&buf, 4096, SPEEDCHECK_SIZE << 20);
+       if (ret) {
+               _E("posix_memalign() failed");
                result = -1;
-               close(fd);
-               goto out;
+               goto out_close;
        }
+
        clock_gettime(CLOCK_REALTIME, &start_time);
        _I("start time: %lu.%lu", start_time.tv_sec, start_time.tv_nsec);
-       ret = write(fd, buf, SPEEDCHECK << 20);
+       ret = read(fd, buf, SPEEDCHECK_SIZE << 20);
        clock_gettime(CLOCK_REALTIME, &end_time);
        _I("end time %lu.%lu", end_time.tv_sec, end_time.tv_nsec);
 
        free(buf);
 
-       time_diff = end_time.tv_sec - start_time.tv_sec;
-       if (time_diff > 0 && (SPEEDCHECK / time_diff < 4)) {
+       if (ret < 0) {
+               _E("read() failed %d", errno);
                result = -1;
-               close(fd);
-               goto out;
+               goto out_close;
        }
 
-       if (ret < 0) {
-               _E("write() failed %d", errno);
+       time_diff = end_time.tv_sec - start_time.tv_sec;
+       if (time_diff > 0 && (SPEEDCHECK_SIZE / time_diff < SPEEDCHECK_CRITERION)) {
                result = -1;
-               close(fd);
-               goto out;
+               goto out_close;
        }
 
+out_close:
        close(fd);
 out:
        return g_variant_new("(i)", result);