4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/mount.h>
28 #include <sys/statvfs.h>
32 #include <sys/statfs.h>
39 #include <tzplatform_config.h>
40 #include <app2ext_interface.h>
42 #include <blkid/blkid.h>
45 #include <glib/gstdio.h>
47 #include <libsyscommon/dbus-system.h>
50 #include "config-parser.h"
51 #include "module-intf.h"
55 #include "fd_handler.h"
58 #include "storaged_common.h"
61 * TODO Assume root device is always mmcblk0*.
63 #define MMC_PATH "*/mmcblk[0-9]*"
64 #define MMC_PARTITION_PATH "mmcblk[0-9]p[0-9]"
65 /* Emulator send devlink for sdcard as \*\/sdcard\/\* */
66 #define MMC_LINK_PATH "*/sdcard/*"
67 #define SCSI_PATH "*/sd[a-z]*"
68 #define SCSI_PARTITION_PATH "sd[a-z][0-9]"
69 #define SCSI_PARTITION_LENGTH 9
70 #define EXTENDEDSD_NODE_PATH "/dev/mapper/extendedsd"
72 #define FILESYSTEM_NAME "filesystem"
74 #define DEV_PREFIX "/dev/"
77 #define UNMOUNT_RETRY 5
78 #define TIMEOUT_MAKE_OBJECT 500 /* milliseconds */
80 #define SIGNAL_POWEROFF_STATE "ChangeState"
81 #define METHOD_ADD_POWEROFF_WAIT "AddPowerOffWait"
82 #define METHOD_REMOVE_POWEROFF_WAIT "RemovePowerOffWait"
84 #define BLOCK_DEVICE_ADDED "DeviceAdded"
85 #define BLOCK_DEVICE_REMOVED "DeviceRemoved"
86 #define BLOCK_DEVICE_BLOCKED "DeviceBlocked"
87 #define BLOCK_DEVICE_CHANGED "DeviceChanged"
88 #define BLOCK_DEVICE_CHANGED_2 "DeviceChanged2"
90 #define BLOCK_TYPE_MMC "mmc"
91 #define BLOCK_TYPE_SCSI "scsi"
92 #define BLOCK_TYPE_ALL "all"
94 #define BLOCK_MMC_NODE_PREFIX "SDCard"
95 #define BLOCK_SCSI_NODE_PREFIX "USBDrive"
97 #define BLOCK_CONF_FILE "/etc/storaged/block.conf"
99 #define EXTERNAL_STORAGE_PATH "/run/storaged/external-storage"
100 #define EXTENDED_INTERNAL_PATH "/run/storaged/extended-internal-sd"
103 #define EXTENDEDSD_MOUNT_PATH "/opt/extendedsd"
105 #define VFAT_NAME "vfat"
106 #define EXFAT_NAME "exfat"
107 #define EXT4_NAME "ext4"
108 #define LUKS_NAME "crypto_LUKS"
109 #define EXTENDEDSD_NAME "extendedsd"
111 /* Minimum value of block id */
112 #define BLOCK_ID_MIN 10
113 /* For 2.4 Backward Compatibility */
114 #define EXT_PRIMARY_SD_FIXID 1
116 /* Maximum number of thread */
119 #define SPEEDCHECK_SIZE 16
120 #define SPEEDCHECK_CRITERION 4 /* MB/s */
122 #define PKGDIR_BUS_NAME "org.tizen.pkgdir_tool"
123 #define PKGDIR_PATH "/org/tizen/pkgdir_tool"
124 #define PKGDIR_INTERFACE "org.tizen.pkgdir_tool"
126 #define POPUP_KEY_CONTENT "_SYSPOPUP_CONTENT_"
127 #define VIEWTYPE_KEY "viewtype"
128 #define DEVPATH_KEY "dev_path"
129 #define MAPPING_NODE_KEY "mapping_node"
130 #define INSERT_SD_CARD "INSERT_SD_CARD"
132 #define MMC_POPUP_NOTI "SDcardNoti"
133 #define MMC_INSERTED "inserted"
134 #define MMC_REMOVED "removed"
136 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
138 #define FILE_NAME_LEN_MAX 255
140 enum block_dev_operation {
149 enum private_operation_state {
155 struct operation_queue {
156 enum block_dev_operation op;
157 GDBusMethodInvocation *invocation;
162 struct block_device {
163 struct block_data *data;
165 int thread_id; /* Current thread ID */
166 bool removed; /* True when device is physically removed but operation is not precessed yet */
167 enum private_operation_state on_private_op;
168 bool mount_point_updated;
173 struct block_device *bdev;
175 enum unmount_operation option;
179 enum block_dev_operation op;
180 struct block_device *bdev;
184 static struct block_conf {
186 bool extendedinternal;
187 } block_conf[BLOCK_EXTENDEDSD_DEV + 1];
189 static struct manage_thread {
190 dd_list *th_node_list; /* List of devnode which thread dealt with. Only main thread access */
191 dd_list *block_dev_list; /* Use thread mutex */
193 pthread_mutex_t mutex;
195 int num_dev; /* Number of devices which thread holds. Only main thread access */
196 int op_len; /* Number of operation of thread. Use thread mutex */
197 int thread_id; /* Never changed */
199 } th_manager[THREAD_MAX];
201 char mmc_default_path[][FILE_NAME_LEN_MAX + 1] = {
208 #define DIR_NUM ((int)(sizeof(mmc_default_path)/sizeof(mmc_default_path[0])))
210 static dd_list *fs_head;
211 static dd_list *block_ops_list;
214 static fd_handler_h phandler;
215 static bool block_control = false;
216 static bool block_boot = false;
217 static pthread_mutex_t pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
218 static bool add_poweroff_wait = false;
220 /* Assume there is only one physical internal storage */
221 static int dev_internal = -1;
222 static char dev_internal_scsi = '\0';
223 static char dev_internal_emul = '\0';
225 static int block_start(void *data);
226 static int block_stop(void *data);
228 static int add_operation(struct block_device *bdev,
229 enum block_dev_operation operation,
230 GDBusMethodInvocation *invocation, void *data);
231 static void remove_operation(struct block_device *bdev);
232 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
233 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
234 static int change_mount_point(struct block_device *bdev, const char *mount_point);
235 static void terminate_threads(void);
237 #define nullstr(x) (x ? x : "")
238 static GVariant *block_data_to_gvariant(struct block_data *data, int flags);
239 static GVariant *block_data_to_gvariant2(struct block_data *data, int flags);
241 #define block_send_dbus_reply(invocation, result) if (invocation) {g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", result)); }
243 static void uevent_block_handler(struct udev_device *dev);
244 static struct uevent_handler uh = {
245 .subsystem = BLOCK_SUBSYSTEM,
246 .uevent_func = uevent_block_handler,
249 static void __CONSTRUCTOR__ smack_check(void)
254 fp = fopen("/proc/filesystems", "r");
258 while (fgets(buf, sizeof(buf), fp) != NULL) {
259 if (strstr(buf, "smackfs")) {
268 void add_fs(const struct block_fs_ops *fs)
270 DD_LIST_APPEND(fs_head, (void *)fs);
273 void remove_fs(const struct block_fs_ops *fs)
275 DD_LIST_REMOVE(fs_head, (void *)fs);
278 const struct block_fs_ops *find_fs(enum block_fs_type type)
280 struct block_fs_ops *fs;
283 DD_LIST_FOREACH(fs_head, elem, fs) {
284 if (fs->type == type)
290 void add_block_dev(const struct block_dev_ops *ops)
292 DD_LIST_APPEND(block_ops_list, (void *)ops);
295 void remove_block_dev(const struct block_dev_ops *ops)
297 DD_LIST_REMOVE(block_ops_list, (void *)ops);
300 static void broadcast_block_info(enum block_dev_operation op,
301 struct block_data *data, int result)
303 struct block_dev_ops *ops;
306 if (data->primary != true)
309 DD_LIST_FOREACH(block_ops_list, elem, ops) {
310 int data_block_type = (data->block_type == BLOCK_EXTENDEDSD_DEV)
311 ? BLOCK_MMC_DEV : data->block_type;
313 if (ops->block_type != data_block_type)
315 // TODO What happend on extended internal storage case?
316 if (op == BLOCK_DEV_MOUNT) {
317 ops->mounted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
318 } else if (op == BLOCK_DEV_UNMOUNT) {
319 ops->unmounted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
320 } else if (op == BLOCK_DEV_FORMAT) {
321 ops->formatted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
322 } else if (op == BLOCK_DEV_INSERT)
324 else if (op == BLOCK_DEV_REMOVE)
329 // Called by MainThread - Insert
330 static int block_get_new_id(void)
332 static int id = BLOCK_ID_MIN;
333 struct block_device *bdev;
338 for (i = 0 ; i < INT_MAX ; i++) {
340 for (j = 0; j < THREAD_MAX; j++) {
341 pthread_mutex_lock(&(th_manager[j].mutex));
342 DD_LIST_FOREACH(th_manager[j].block_dev_list, elem, bdev) {
343 if (bdev->data->id == id) {
348 pthread_mutex_unlock(&(th_manager[j].mutex));
363 static void remove_file(int id, bool extendedsd)
365 char file_name[PATH_LEN];
372 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
374 snprintf(file_name, sizeof(file_name), EXTERNAL_STORAGE_PATH"/%d", id);
376 ret = remove(file_name);
378 _E("Failed to remove '%s': %d", file_name, errno);
381 static void create_file(int id, char *mount_point, bool extendedsd)
384 char file_name[PATH_LEN];
390 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
392 snprintf(file_name, PATH_LEN, EXTERNAL_STORAGE_PATH"/%d", id);
394 fp = fopen(file_name, "w+");
396 fprintf(fp, "%s", mount_point);
399 _E("Failed to open '%s'.", file_name);
402 static void broadcast_device_blocked(struct block_device *bdev)
404 struct block_data *data;
407 if (!bdev || !bdev->data)
412 ret = dbus_handle_emit_dbus_signal(NULL,
413 STORAGED_PATH_BLOCK_MANAGER,
414 STORAGED_INTERFACE_BLOCK_MANAGER,
415 BLOCK_DEVICE_BLOCKED,
416 block_data_to_gvariant(data, 0));
418 _E("Failed to send dbus signal");
421 static void broadcast_device_changed(struct block_device *bdev,
422 enum block_dev_operation op)
424 struct block_data *data;
425 GVariant *param = NULL;
426 const char *signal_name = NULL;
430 if (!bdev || !bdev->data) {
431 _E("Failed to broadcast device changed signal. op(%d)", op);
437 /* set flags and signal name */
439 case BLOCK_DEV_MOUNT:
440 BLOCK_GET_MOUNT_FLAGS(data, flags);
441 signal_name = BLOCK_DEVICE_CHANGED;
443 case BLOCK_DEV_UNMOUNT:
444 BLOCK_GET_UNMOUNT_FLAGS(data, flags);
445 signal_name = BLOCK_DEVICE_CHANGED;
447 case BLOCK_DEV_FORMAT:
448 BLOCK_GET_FORMAT_FLAGS(data, flags);
449 signal_name = BLOCK_DEVICE_CHANGED;
451 case BLOCK_DEV_INSERT:
453 signal_name = BLOCK_DEVICE_ADDED;
455 case BLOCK_DEV_REMOVE:
457 signal_name = BLOCK_DEVICE_REMOVED;
461 _E("Failed to broadcast device changed signal. op(%d)", op);
465 /* Broadcast outside with BlockManager iface */
466 param = block_data_to_gvariant(data, flags);
468 ret = dbus_handle_emit_dbus_signal(NULL,
469 STORAGED_PATH_BLOCK_MANAGER,
470 STORAGED_INTERFACE_BLOCK_MANAGER,
474 _E("Failed to broadcast device changed signal. op(%d)", op);
476 /* broadcast one more signal for mount, unmount, format */
478 case BLOCK_DEV_MOUNT:
479 case BLOCK_DEV_UNMOUNT:
480 case BLOCK_DEV_FORMAT:
481 ret = dbus_handle_emit_dbus_signal(NULL,
482 STORAGED_PATH_BLOCK_MANAGER,
483 STORAGED_INTERFACE_BLOCK_MANAGER,
484 BLOCK_DEVICE_CHANGED_2,
485 block_data_to_gvariant2(data, flags));
487 _E("Failed to broadcast device changed signal. op(%d)", op);
494 static int get_mmc_mount_node(char *devnode, char *node, size_t len)
496 char *name = devnode;
497 int dev = -1, part = -1;
498 char emul[32] = { 0, };
505 sscanf(name, "mmcblk%dp%d", &dev, &part);
508 snprintf(node, len, "%s%c", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1);
510 snprintf(node, len, "%s%c%d", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1, part);
515 sscanf(name, "vd%31s", emul);
518 for (i = 0 ; i < strlen(emul) ; i++)
519 emul[i] = toupper(emul[i]);
520 snprintf(node, len, "%s%s", BLOCK_MMC_NODE_PREFIX, emul);
524 static int get_scsi_mount_node(char *devnode, char *node, size_t len)
532 snprintf(dev, sizeof(dev), "%s", devnode);
534 if (!strstr(dev, "sd"))
538 name += strlen("sd");
540 for (i = 0 ; i < strlen(name) ; i++)
541 name[i] = toupper(name[i]);
542 snprintf(node, len, "%s%s", BLOCK_SCSI_NODE_PREFIX, name);
547 static char *generate_mount_path(struct block_data *data)
550 char *name, node[64];
553 if (!data || !data->devnode || !data->fs_usage || (strcmp(data->fs_usage, FILESYSTEM_NAME) && strncmp(data->fs_usage, "crypto", strlen("crypto"))))
556 name = strrchr(data->devnode, '/');
561 switch (data->block_type) {
563 ret = get_mmc_mount_node(name, node, sizeof(node));
566 ret = get_scsi_mount_node(name, node, sizeof(node));
568 case BLOCK_EXTENDEDSD_DEV:
569 return strdup(EXTENDEDSD_MOUNT_PATH);
571 _E("Invalid block type(%d).", data->block_type);
577 str = tzplatform_mkpath(TZ_SYS_MEDIA, node);
583 _E("Invalid devnode(%s).", data->devnode ? data->devnode : "NULL");
587 static bool check_primary_partition(const char *devnode)
589 struct block_fs_ops *fs;
592 const char *filesystem = NULL;
602 if (fnmatch(MMC_LINK_PATH, devnode, 0) &&
603 fnmatch(MMC_PATH, devnode, 0) &&
604 fnmatch(SCSI_PATH, devnode, 0) &&
605 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
608 temp = strrchr(devnode, '/');
611 if (fnmatch("/"SCSI_PARTITION_PATH, temp, 0) &&
612 fnmatch("/"MMC_PARTITION_PATH, temp, 0))
615 /* Emulator support only one partition */
619 snprintf(str, sizeof(str), "%s", devnode);
624 for (i = 1; i <= 9; ++i) {
625 snprintf(str2, sizeof(str2), "%s%d", str, i);
626 if (access(str2, R_OK) != 0)
629 probe = blkid_new_probe_from_filename(str2);
632 if (blkid_do_probe(probe) != 0)
635 ret = blkid_probe_lookup_value(probe, "TYPE", &filesystem, &fs_len);
637 blkid_free_probe(probe);
640 DD_LIST_FOREACH(fs_head, elem, fs) {
641 if (!strncmp(fs->name, filesystem, fs_len)) {
646 blkid_free_probe(probe);
652 if (found && !strncmp(devnode, str2, strlen(str2) + 1))
658 /* Whole data in struct block_data should be freed. */
659 static struct block_data *make_block_data(const char *devnode,
661 const char *fs_usage,
663 const char *fs_version,
664 const char *fs_uuid_enc,
665 const char *readonly)
667 struct block_data *data;
669 /* devnode is unique value so it should exist. */
674 _I("Unknown fs type.");
676 data = calloc(1, sizeof(struct block_data));
678 _E("Failed to calloc().");
682 data->devnode = strdup(devnode);
684 data->syspath = strdup(syspath);
686 data->fs_usage = strdup(fs_usage);
688 data->fs_type = strdup(fs_type);
690 data->fs_version = strdup(fs_version);
692 data->fs_uuid_enc = strdup(fs_uuid_enc);
694 data->readonly = atoi(readonly);
695 data->primary = check_primary_partition(devnode);
697 /* TODO should we know block dev type? */
698 if (!fnmatch(MMC_LINK_PATH, devnode, 0))
699 data->block_type = BLOCK_MMC_DEV;
700 else if (!fnmatch(MMC_PATH, devnode, 0))
701 data->block_type = BLOCK_MMC_DEV;
702 else if (!fnmatch(SCSI_PATH, devnode, 0))
703 data->block_type = BLOCK_SCSI_DEV;
704 else if (!fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
705 data->block_type = BLOCK_EXTENDEDSD_DEV;
707 data->block_type = -1;
709 data->mount_point = generate_mount_path(data);
710 BLOCK_FLAG_CLEAR_ALL(data);
712 /* for 2.4 backward compatibility */
713 // What if storage id 1 is existed? (multi sdcard case)
714 if (data->primary == true && data->block_type == BLOCK_MMC_DEV &&
715 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM_NAME))
716 data->id = EXT_PRIMARY_SD_FIXID;
718 data->id = block_get_new_id();
723 static void free_block_data(struct block_data *data)
729 free(data->fs_usage);
731 free(data->fs_version);
732 free(data->fs_uuid_enc);
733 free(data->mount_point);
737 static int update_block_data(struct block_data *data,
738 const char *fs_usage,
740 const char *fs_version,
741 const char *fs_uuid_enc,
742 const char *readonly,
743 bool mount_point_updated)
748 free(data->fs_usage);
749 data->fs_usage = NULL;
751 data->fs_usage = strdup(fs_usage);
754 data->fs_type = NULL;
756 data->fs_type = strdup(fs_type);
758 free(data->fs_version);
759 data->fs_version = NULL;
761 data->fs_version = strdup(fs_version);
763 free(data->fs_uuid_enc);
764 data->fs_uuid_enc = NULL;
766 data->fs_uuid_enc = strdup(fs_uuid_enc);
768 /* generate_mount_path function should be invoked
769 * after fs_uuid_enc is updated */
770 if (!mount_point_updated) {
771 free(data->mount_point);
772 data->mount_point = generate_mount_path(data);
775 data->readonly = false;
777 data->readonly = atoi(readonly);
779 BLOCK_FLAG_MOUNT_CLEAR(data);
784 static struct block_device *make_block_device(struct block_data *data)
786 struct block_device *bdev;
791 bdev = calloc(1, sizeof(struct block_device));
796 bdev->thread_id = -1;
797 bdev->removed = false;
798 bdev->on_private_op = REQ_NORMAL;
799 bdev->private_pid = 0;
800 bdev->mount_point_updated = false;
805 // Called by MainThread - Remove DevNode
806 static void free_block_device(struct block_device *bdev)
809 struct operation_queue *op;
815 thread_id = bdev->thread_id;
816 if (thread_id < 0 || thread_id >= THREAD_MAX)
819 pthread_mutex_lock(&(th_manager[thread_id].mutex));
821 th_manager[thread_id].num_dev--;
822 DD_LIST_REMOVE(th_manager[thread_id].block_dev_list, bdev);
823 free_block_data(bdev->data);
825 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
827 th_manager[thread_id].op_len--;
828 DD_LIST_REMOVE(bdev->op_queue, op);
831 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
836 // Called By MainThread - Remove Device
837 static struct block_device *find_block_device(const char *devnode)
839 struct block_device *bdev;
844 len = strlen(devnode) + 1;
845 for (i = 0; i < THREAD_MAX; i++) {
846 pthread_mutex_lock(&(th_manager[i].mutex));
847 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
848 if (bdev->data && !bdev->removed &&
849 !strncmp(bdev->data->devnode, devnode, len)) {
850 pthread_mutex_unlock(&(th_manager[i].mutex));
854 pthread_mutex_unlock(&(th_manager[i].mutex));
860 // Called By MainThread - Remove Device
861 static struct block_device *find_block_device_path(const char *mount_point)
863 struct block_device *bdev;
868 len = strlen(mount_point) + 1;
869 for (i = 0; i < THREAD_MAX; i++) {
870 pthread_mutex_lock(&(th_manager[i].mutex));
871 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
872 if (bdev->data && !bdev->removed &&
873 (bdev->data->mount_point != NULL && !strncmp(bdev->data->mount_point, mount_point, len))) {
874 pthread_mutex_unlock(&(th_manager[i].mutex));
878 pthread_mutex_unlock(&(th_manager[i].mutex));
884 // Called By MainThread - Mount,Unmount,Format,GetInfo
885 static struct block_device *find_block_device_by_id(int id)
887 struct block_device *bdev;
891 for (i = 0; i < THREAD_MAX; i++) {
892 pthread_mutex_lock(&(th_manager[i].mutex));
893 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
898 if (bdev->data->id == id) {
899 pthread_mutex_unlock(&(th_manager[i].mutex));
903 pthread_mutex_unlock(&(th_manager[i].mutex));
909 static const char *get_operation_char(enum block_dev_operation op)
912 case BLOCK_DEV_MOUNT:
915 case BLOCK_DEV_UNMOUNT:
918 case BLOCK_DEV_FORMAT:
921 case BLOCK_DEV_INSERT:
924 case BLOCK_DEV_REMOVE:
927 case BLOCK_LUKS_CLOSE:
931 _E("Invalid operation(%d).", op);
935 void mmc_make_default_path(const char *mount_path)
939 char mmc_path[FILE_NAME_LEN_MAX + 1] = {0, };
941 for (i = 0; i < DIR_NUM; ++i) {
942 snprintf(mmc_path, sizeof(mmc_path), "%s/%s", mount_path, mmc_default_path[i]);
943 if (!g_file_test(mmc_path, G_FILE_TEST_IS_DIR)) {
944 _D("Path(%s) did not exist.", mmc_path);
945 ret = mkdir(mmc_path, 0777);
947 _E("Failed to mkdir: %d", errno);
949 /*this fuction for emulator*/
950 /*at the first time, the directroies are made permission 755*/
951 ret = chmod(mmc_path, 0777);
953 _E("Failed to chmod: %d", errno);
955 ret = chown(mmc_path, 0, 10001);
957 _E("Failed to chown: %d", errno);
962 static void create_external_apps_directory(void)
966 ret = dbus_handle_method_async(PKGDIR_BUS_NAME, PKGDIR_PATH,
967 PKGDIR_INTERFACE, "CreateExternalDirsForAllPkgs", NULL, NULL);
969 _E("Failed to create external directory.");
972 static int pipe_trigger(enum block_dev_operation op,
973 struct block_device *bdev, int result)
975 struct pipe_data pdata = { op, bdev, result };
978 _D("op=%s bdev=%p result=%d",
979 get_operation_char(pdata.op),
980 pdata.bdev, pdata.result);
982 // Multi thread should not write at the same time
983 pthread_mutex_lock(&pipe_mutex);
984 n = write(pfds[1], &pdata, sizeof(struct pipe_data));
985 pthread_mutex_unlock(&pipe_mutex);
987 return (n != sizeof(struct pipe_data)) ? -EPERM : 0;
990 static bool pipe_cb(int fd, void *data)
992 struct pipe_data pdata = {0,};
997 n = read(fd, &pdata, sizeof(pdata));
998 if (n != sizeof(pdata) || !pdata.bdev) {
999 _E("Failed to read struct pipe data.");
1003 _I("op=%s bdev=%p result=%d",
1004 get_operation_char(pdata.op),
1005 pdata.bdev, pdata.result);
1007 if (pdata.op == BLOCK_LUKS_CLOSE)
1010 if (pdata.op == BLOCK_DEV_MOUNT && pdata.result < 0) {
1011 if (pdata.bdev->data->state == BLOCK_UNMOUNT) {
1012 ret = change_mount_point(pdata.bdev, "");
1013 /* Modify /run/external-storage/id file */
1015 if (pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV)
1016 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, true);
1018 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, false);
1023 if (pdata.op == BLOCK_DEV_MOUNT &&
1024 pdata.bdev->data->state == BLOCK_MOUNT &&
1025 pdata.bdev->data->block_type == BLOCK_MMC_DEV &&
1026 pdata.bdev->data->primary) {
1027 create_external_apps_directory();
1028 mmc_make_default_path(pdata.bdev->data->mount_point);
1030 ret = dbus_handle_method_sync_var(POPUP_BUS_NAME,
1032 POPUP_INTERFACE_NOTI,
1034 g_variant_new("(s)", MMC_INSERTED));
1036 _E("Failed to popup: %d", ret);
1038 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1039 /* Remove file for block device /run/xxxxxx/id */
1040 remove_file(pdata.bdev->data->id, pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
1042 if (pdata.bdev->data->block_type == BLOCK_MMC_DEV &&
1043 pdata.bdev->data->primary &&
1044 BLOCK_IS_FLAG_SET(pdata.bdev->data, UNMOUNT_UNSAFE)) {
1046 ret = dbus_handle_method_sync_var(POPUP_BUS_NAME,
1048 POPUP_INTERFACE_NOTI,
1050 g_variant_new("(s)", MMC_REMOVED));
1052 _E("Failed to popup: %d", ret);
1056 /* Broadcast to mmc and usb storage module */
1057 broadcast_block_info(pdata.op, pdata.bdev->data, pdata.result);
1059 /* Broadcast outside with Block iface */
1060 if (pdata.bdev->on_private_op == REQ_NORMAL)
1061 broadcast_device_changed(pdata.bdev, pdata.op);
1062 else if (pdata.bdev->on_private_op == REQ_PRIVATE) {
1063 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1064 pdata.bdev->on_private_op = REQ_NORMAL;
1065 _D("Private operation state(%d).", pdata.bdev->on_private_op);
1068 if (pdata.op == BLOCK_DEV_MOUNT) {
1069 pdata.bdev->on_private_op = REQ_PRIVATE;
1070 _D("Private operation state(%d).", pdata.bdev->on_private_op);
1074 if (pdata.op == BLOCK_DEV_REMOVE) {
1075 thread_id = pdata.bdev->thread_id;
1076 if (thread_id < 0 || thread_id >= THREAD_MAX)
1078 free_block_device(pdata.bdev);
1085 static int pipe_init(void)
1089 ret = pipe2(pfds, O_CLOEXEC);
1093 ret = add_fd_read_handler(pfds[0], pipe_cb,
1094 NULL, NULL, &phandler);
1096 _E("Failed to add pipe handler: %d", ret);
1103 static void pipe_exit(void)
1106 remove_fd_read_handler(&phandler);
1116 static int mmc_check_and_unmount(const char *path)
1124 while (mount_check(path)) {
1128 if (retry > UNMOUNT_RETRY)
1135 static bool check_rw_mount(const char *szPath)
1137 struct statvfs mount_stat;
1139 if (!statvfs(szPath, &mount_stat)) {
1140 if ((mount_stat.f_flag & ST_RDONLY) == ST_RDONLY)
1146 static int retrieve_udev_device(struct block_data *data, bool mount_point_updated)
1149 struct udev_device *dev;
1150 const char *fs_type;
1151 const char *fs_usage;
1158 for (wait = 0; wait < 10; wait++) {
1161 _E("Failed to create udev library context.");
1165 dev = udev_device_new_from_syspath(udev, data->syspath);
1167 _E("Failed to create new udev device.");
1172 fs_type = udev_device_get_property_value(dev, "ID_FS_TYPE");
1173 fs_usage = udev_device_get_property_value(dev, "ID_FS_USAGE");
1174 /* fs_usage for crpto_LUKS is crypto */
1175 if (!fs_type || (strncmp(fs_type, VFAT_NAME, strlen(VFAT_NAME)) && strncmp(fs_type, EXT4_NAME, strlen(EXT4_NAME))))
1177 else if (!fs_usage || strncmp(FILESYSTEM_NAME, fs_usage, strlen(FILESYSTEM_NAME)))
1182 udev_device_unref(dev);
1186 r = update_block_data(data,
1187 udev_device_get_property_value(dev, "ID_FS_USAGE"),
1188 udev_device_get_property_value(dev, "ID_FS_TYPE"),
1189 udev_device_get_property_value(dev, "ID_FS_VERSION"),
1190 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
1191 udev_device_get_sysattr_value(dev, "ro"),
1192 mount_point_updated);
1194 _E("Failed to update block data for %s.", data->devnode);
1196 udev_device_unref(dev);
1201 static int block_mount(struct block_data *data)
1203 struct block_fs_ops *fs;
1208 if (!data || !data->devnode || !data->mount_point)
1211 /* check existing mounted */
1212 if (mount_check(data->mount_point))
1215 /* create mount point */
1216 if (access(data->mount_point, R_OK) != 0) {
1217 if (mkdir(data->mount_point, 0755) < 0)
1221 /* check matched file system */
1222 if (!data->fs_usage ||
1223 strncmp(data->fs_usage, FILESYSTEM_NAME,
1224 sizeof(FILESYSTEM_NAME)) != 0) {
1229 if (!data->fs_type) {
1230 _E("There is no file system.");
1231 BLOCK_FLAG_SET(data, FS_EMPTY);
1237 len = strlen(data->fs_type) + 1;
1238 DD_LIST_FOREACH(fs_head, elem, fs) {
1239 if (!strncmp(fs->name, data->fs_type, len))
1244 _E("Not supported file system(%s).", data->fs_type);
1245 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1250 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1251 r = fs->mount(false, data->devnode, data->mount_point);
1253 r = fs->mount(smack, data->devnode, data->mount_point);
1256 BLOCK_FLAG_SET(data, FS_BROKEN);
1261 r = check_rw_mount(data->mount_point);
1268 rmdir(data->mount_point);
1272 static int mount_start(struct block_device *bdev)
1274 struct block_data *data;
1282 _I("Mount Start (%s -> %s).",
1283 data->devnode, data->mount_point);
1285 /* mount operation */
1286 r = block_mount(data);
1287 if (r != -EROFS && r < 0) {
1288 _E("Failed to mount device(%s): %d", data->devnode, r);
1293 data->readonly = true;
1294 BLOCK_FLAG_SET(data, MOUNT_READONLY);
1297 data->state = BLOCK_MOUNT;
1299 if (data->block_type == BLOCK_MMC_DEV) {
1300 /* app2ext_migrate_legacy_all has dbus method call to deviced */
1301 ret = app2ext_migrate_legacy_all();
1303 _E("Failed to app2ext.");
1307 if (r < 0 && r != -EROFS)
1308 data->state = BLOCK_UNMOUNT;
1310 _I("%s result=%s: %d", __func__, data->devnode, r);
1312 if (pipe_trigger(BLOCK_DEV_MOUNT, bdev, r) < 0)
1313 _E("Failed to trigger pipe.");
1318 static int change_mount_point(struct block_device *bdev,
1319 const char *mount_point)
1321 struct block_data *data;
1327 free(data->mount_point);
1329 /* If the mount path already exists, the path cannot be used */
1331 access(mount_point, F_OK) != 0) {
1332 data->mount_point = strdup(mount_point);
1333 bdev->mount_point_updated = true;
1335 data->mount_point = generate_mount_path(data);
1336 bdev->mount_point_updated = false;
1342 static int mount_block_device(struct block_device *bdev)
1344 struct block_data *data;
1347 if (!bdev || !bdev->data)
1351 if (data->state == BLOCK_MOUNT) {
1352 _I("%s is already mounted.", data->devnode);
1356 if (!block_conf[data->block_type].multimount &&
1358 _I("Not support multi mount by config info.");
1362 r = mount_start(bdev);
1364 _E("Failed to mount %s.", data->devnode);
1371 static int block_unmount(struct block_device *bdev,
1372 enum unmount_operation option)
1374 struct block_data *data;
1376 struct timespec time = {0,};
1378 if (!bdev || !bdev->data || !bdev->data->mount_point)
1383 if (bdev->on_private_op == REQ_NORMAL)
1384 broadcast_device_blocked(bdev);
1386 /* it must called before unmounting mmc */
1387 r = mmc_check_and_unmount(data->mount_point);
1390 if (option == UNMOUNT_NORMAL) {
1391 _I("Failed to unmount with normal option: %d", r);
1395 _I("Execute force unmount.");
1396 /* Force Unmount Scenario */
1399 * should unmount the below vconf key. */
1400 if ((data->block_type == BLOCK_MMC_DEV ||
1401 data->block_type == BLOCK_EXTENDEDSD_DEV) &&
1403 /* At first, notify to other app
1404 * who already access sdcard */
1405 _I("Notify to other app who already access sdcard.");
1406 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
1407 VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
1411 if (umount2(data->mount_point, MNT_DETACH) != 0) {
1412 _I("Failed to unmount with lazy option: %m");
1416 time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
1418 while (retry++ < UNMOUNT_RETRY) {
1419 _I("Kill app with SIGTERM.");
1420 terminate_process(data->devnode, false);
1421 nanosleep(&time, NULL);
1423 _I("Kill app with SIGKILL.");
1424 terminate_process(data->devnode, true);
1425 nanosleep(&time, NULL);
1427 if (!is_in_use_partition(data->devnode))
1433 data->state = BLOCK_UNMOUNT;
1435 if (rmdir(data->mount_point) < 0)
1436 _E("Failed to remove '%s' directory.", data->mount_point);
1441 static int unmount_block_device(struct block_device *bdev,
1442 enum unmount_operation option)
1444 struct block_data *data;
1447 if (!bdev || !bdev->data)
1451 if (data->state == BLOCK_UNMOUNT) {
1452 _I("%s is already unmounted.", data->devnode);
1453 r = mmc_check_and_unmount(data->mount_point);
1455 _E("The path was existed, but could not delete it(%s).",
1460 _I("Unmount Start. '%s' -> '%s'.",
1461 data->devnode, data->mount_point);
1463 r = block_unmount(bdev, option);
1465 _E("Failed to unmount %s device: %d", data->devnode, r);
1469 BLOCK_FLAG_MOUNT_CLEAR(data);
1472 _I("%s result=%s: %d", __func__, data->devnode, r);
1474 if (pipe_trigger(BLOCK_DEV_UNMOUNT, bdev, r) < 0)
1475 _E("Failed to trigger pipe.");
1480 static int block_format(struct block_data *data,
1481 const char *fs_type, bool mount_point_updated)
1483 const struct block_fs_ops *fs;
1489 if (!data || !data->devnode || !data->mount_point)
1492 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1498 fstype = data->fs_type;
1502 if (!strcmp(fstype, EXFAT_NAME))
1506 len = strlen(fstype);
1507 DD_LIST_FOREACH(fs_head, elem, fs) {
1508 if (!strncmp(fs->name, fstype, len))
1512 if (!fs || !fs->format) {
1513 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1514 _E("Not supported file system(%s).", fstype);
1518 _I("Format path=%s", data->devnode);
1519 r = fs->format(data->devnode);
1521 _E("Failed to format block data for %s.", data->devnode);
1525 /* need to update the partition data.
1526 * It can be changed in doing format. */
1527 retrieve_udev_device(data, mount_point_updated);
1532 static int format_block_device(struct block_device *bdev,
1533 const char *fs_type,
1534 enum unmount_operation option)
1536 struct block_data *data;
1544 _I("Format Start. '%s' -> '%s'.",
1545 data->devnode, data->mount_point);
1547 if (data->state == BLOCK_MOUNT) {
1548 r = block_unmount(bdev, option);
1550 _E("Failed to unmount %s device: %d", data->devnode, r);
1555 r = block_format(data, fs_type, bdev->mount_point_updated);
1557 _E("Failed to format %s device: %d", data->devnode, r);
1560 _I("%s result=%s: %d", __func__, data->devnode, r);
1562 r = pipe_trigger(BLOCK_DEV_FORMAT, bdev, r);
1564 _E("Failed to trigger pipe.");
1569 static struct format_data *get_format_data(
1570 const char *fs_type, enum unmount_operation option)
1572 struct format_data *fdata;
1574 fdata = (struct format_data *)malloc(sizeof(struct format_data));
1576 _E("Failed to allocate format data.");
1581 fdata->fs_type = strdup(fs_type);
1583 fdata->fs_type = NULL;
1584 fdata->option = option;
1589 static void release_format_data(struct format_data *data)
1593 free(data->fs_type);
1598 // Called by BlockThread - Real Mount Op
1599 static int block_mount_device(struct block_device *bdev, void *data)
1608 thread_id = bdev->thread_id;
1609 if (thread_id < 0 || thread_id >= THREAD_MAX)
1611 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1612 l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
1613 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1615 _E("'%s' does not exist in the device list.", bdev->data->devnode);
1619 /* mount automatically */
1620 ret = mount_block_device(bdev);
1622 _E("Failed to mount block device for %s.", bdev->data->devnode);
1627 // Called by BlockThread - Real Format Op
1628 static int block_format_device(struct block_device *bdev, void *data)
1633 struct format_data *fdata = (struct format_data *)data;
1635 if (!bdev || !fdata) {
1640 thread_id = bdev->thread_id;
1641 if (thread_id < 0 || thread_id >= THREAD_MAX)
1643 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1644 l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
1645 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1647 _E("'%s' does not exist in the device list.", bdev->data->devnode);
1652 ret = format_block_device(bdev, fdata->fs_type, fdata->option);
1654 _E("Failed to format block device for %s.", bdev->data->devnode);
1657 release_format_data(fdata);
1662 // Called by BlockThread - Real Unmount Op
1663 static int block_unmount_device(struct block_device *bdev, void *data)
1666 long option = (long)data;
1671 ret = unmount_block_device(bdev, option);
1673 _E("Failed to unmount block device(%s).", bdev->data->devnode);
1680 /* Called by BlockThread - Remove Operation
1681 Direct Call at BlockThread
1682 Previously this function was called by MainThread. However, it will increase complexity.
1683 Need thread lock before to call remove_operation
1685 static void remove_operation(struct block_device *bdev)
1687 struct operation_queue *op;
1693 thread_id = bdev->thread_id;
1694 if (thread_id < 0 || thread_id >= THREAD_MAX)
1697 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
1699 _D("Remove operation(%s, %s).",
1700 get_operation_char(op->op),
1701 bdev->data->devnode);
1703 DD_LIST_REMOVE(bdev->op_queue, op);
1709 // Called by BlockThread
1710 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1712 struct operation_queue *temp;
1725 thread_id = bdev->thread_id;
1726 if (thread_id < 0 || thread_id >= THREAD_MAX)
1729 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1731 DD_LIST_FOREACH(*queue, l, temp) {
1732 if (temp->op == BLOCK_DEV_REMOVE) {
1737 th_manager[thread_id].op_len--;
1738 block_send_dbus_reply((*op)->invocation, 0);
1741 remove_operation(bdev);
1742 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1745 // Called by BlockThread
1746 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1748 struct operation_queue *temp;
1751 bool unmounted = false;
1762 thread_id = bdev->thread_id;
1763 if (thread_id < 0 || thread_id >= THREAD_MAX)
1766 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1767 DD_LIST_FOREACH(*queue, l, temp) {
1768 if (temp->op == BLOCK_DEV_UNMOUNT) {
1770 _D("Operation queue has unmount operation.");
1774 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1779 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1781 DD_LIST_FOREACH(*queue, l, temp) {
1782 if (temp->op == BLOCK_DEV_UNMOUNT) {
1787 th_manager[thread_id].op_len--;
1788 block_send_dbus_reply((*op)->invocation, 0);
1791 remove_operation(bdev);
1792 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1797 // Called by BlockThread
1798 static void trigger_operation(struct block_device *bdev, dd_list *queue, struct operation_queue *op)
1802 char devnode[PATH_MAX];
1803 enum block_dev_operation operation;
1804 bool unmounted = false;
1811 thread_id = bdev->thread_id;
1812 if (thread_id < 0 || thread_id >= THREAD_MAX)
1815 snprintf(devnode, sizeof(devnode), "%s", bdev->data->devnode);
1825 _D("Thread id(%d) Trigger operation(%s, %s)", thread_id,
1826 get_operation_char(operation), devnode);
1829 if (operation == BLOCK_DEV_INSERT && bdev->removed) {
1830 check_removed(bdev, &queue, &op);
1832 _D("Trigger operation again(%s, %s).",
1833 get_operation_char(operation), devnode);
1835 if (operation == BLOCK_DEV_MOUNT) {
1836 unmounted = check_unmount(bdev, &queue, &op);
1839 _D("Trigger operation again(%s, %s).",
1840 get_operation_char(operation), devnode);
1844 switch (operation) {
1845 case BLOCK_DEV_INSERT:
1847 case BLOCK_DEV_MOUNT:
1848 ret = block_mount_device(bdev, op->data);
1849 _D("Mount '%s': %d", devnode, ret);
1851 case BLOCK_DEV_FORMAT:
1852 ret = block_format_device(bdev, op->data);
1853 _D("Format '%s': %d", devnode, ret);
1855 case BLOCK_DEV_UNMOUNT:
1856 ret = block_unmount_device(bdev, op->data);
1857 _D("Unmount '%s': %d", devnode, ret);
1859 case BLOCK_DEV_REMOVE:
1862 case BLOCK_LUKS_CLOSE:
1863 ret = ode_luks_close_sync(EXTENDEDSD_NAME);
1865 _E("Failed on ode_luks_close(%s).", EXTENDEDSD_NAME);
1868 _E("Operation type(%d) is invalid.", op->op);
1874 * during checking the queue length */
1875 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1878 th_manager[thread_id].op_len--;
1880 block_send_dbus_reply(op->invocation, ret);
1882 queue = bdev->op_queue;
1883 if (queue != NULL) {
1884 queue = DD_LIST_NEXT(queue);
1886 op = DD_LIST_NTH(queue, 0);
1892 remove_operation(bdev);
1894 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1897 if (operation == BLOCK_DEV_INSERT || operation == BLOCK_DEV_REMOVE || operation == BLOCK_LUKS_CLOSE) {
1898 if (pipe_trigger(operation, bdev, 0) < 0)
1899 _E("Failed to trigger pipe.");
1906 // Called by BlockThread
1907 static void *block_th_start(void *arg)
1909 struct block_device *temp;
1910 struct manage_thread *th = (struct manage_thread *)arg;
1911 struct operation_queue *op = NULL;
1913 dd_list *queue = NULL;
1918 thread_id = th->thread_id;
1919 if (thread_id < 0 || thread_id >= THREAD_MAX) {
1920 _E("Thread Number=%d.", th->thread_id);
1925 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1926 if (th_manager[thread_id].op_len == 0) {
1927 _D("Operation queue of thread is empty.");
1928 pthread_cond_wait(&(th_manager[thread_id].cond), &(th_manager[thread_id].mutex));
1929 _D("Wake up thread=%d.", thread_id);
1932 DD_LIST_FOREACH(th_manager[thread_id].block_dev_list, elem, temp) {
1933 queue = temp->op_queue;
1935 op = DD_LIST_NTH(queue, 0);
1937 _D("Operation queue for device %s is Empty.", temp->data->devnode);
1941 queue = DD_LIST_NEXT(queue);
1949 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1951 if (op && !op->done)
1952 trigger_operation(temp, queue, op);
1957 // This function will be refactored later
1958 // Especially, we don't need to keep th_node_list.
1959 static int find_thread(char *devnode)
1966 int i, len, min, min_num;
1967 int dev_mmc = -1, part = -1, num;
1970 if (!fnmatch("*/"MMC_PARTITION_PATH, devnode, 0)) {
1971 sscanf(devnode, "/dev/mmcblk%dp%d", &dev_mmc, &part);
1978 snprintf(str, len, "/dev/mmcblk%d", dev_mmc);
1979 th_node = strdup(str);
1980 } else if (!fnmatch("*/"SCSI_PARTITION_PATH, devnode, 0)) {
1981 sscanf(devnode, "/dev/sd%c%d", &dev_scsi, &part);
1982 snprintf(str, SCSI_PARTITION_LENGTH, "/dev/sd%c", dev_scsi);
1983 th_node = strdup(str);
1985 th_node = strdup(devnode);
1987 len = strlen(th_node) + 1;
1990 for (i = 0; i < THREAD_MAX; i++) {
1991 DD_LIST_FOREACH(th_manager[i].th_node_list, elem, temp) {
1992 if (!strncmp(temp, th_node, len)) {
1997 if (th_manager[i].num_dev < min_num) {
1998 min_num = th_manager[i].num_dev;
2003 if (min >= 0 && min < THREAD_MAX) {
2004 DD_LIST_APPEND(th_manager[min].th_node_list, th_node);
2008 _E("Failed to find thread.");
2009 DD_LIST_APPEND(th_manager[0].th_node_list, th_node);
2013 /* Only Main thread is permmited */
2014 // Called by MainThread
2015 static int add_operation(struct block_device *bdev,
2016 enum block_dev_operation operation,
2017 GDBusMethodInvocation *invocation, void *data)
2019 struct operation_queue *op;
2028 _I("Add operation(%s, %s).",
2029 get_operation_char(operation),
2030 bdev->data->devnode);
2032 thread_id = bdev->thread_id;
2033 if (thread_id < 0 || thread_id >= THREAD_MAX) {
2034 _E("Failed to find thread to add.");
2038 op = (struct operation_queue *)malloc(sizeof(struct operation_queue));
2040 _E("Failed to malloc.");
2046 op->invocation = invocation;
2048 /* Need to disble app2ext whenever unmounting mmc */
2049 /* app2ext_disable_all_external_pkgs inside a critical section need to be avoided. */
2050 if (operation == BLOCK_DEV_UNMOUNT &&
2051 bdev->data->state == BLOCK_MOUNT &&
2052 bdev->data->block_type == BLOCK_MMC_DEV &&
2053 bdev->data->primary)
2054 if (app2ext_disable_all_external_pkgs() < 0)
2055 _E("Failed to app2ext_disable_all_external_pkgs().");
2058 * during adding queue and checking the queue length */
2059 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2061 /* Only modified between lock and unlock of mutex */
2064 start_th = th_manager[thread_id].start_th;
2065 DD_LIST_APPEND(bdev->op_queue, op);
2066 th_manager[thread_id].op_len++;
2068 if (th_manager[thread_id].op_len == 1 && start_th)
2069 pthread_cond_signal(&(th_manager[thread_id].cond));
2071 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2075 _D("Start new thread for block device.");
2076 th_manager[thread_id].start_th = true;
2077 ret = pthread_create(&(th_manager[thread_id].th), NULL, block_th_start, &th_manager[thread_id]);
2079 _E("Failed to create thread for %s.", bdev->data->devnode);
2083 pthread_detach(th_manager[thread_id].th);
2089 static bool disk_is_partitioned_by_kernel(struct udev_device *dev)
2092 struct dirent entry;
2094 const char *syspath;
2097 syspath = udev_device_get_syspath(dev);
2101 dp = opendir(syspath);
2103 _E("Failed to open '%s'.", syspath);
2107 /* TODO compare devname and d_name */
2108 while (readdir_r(dp, &entry, &dir) == 0 && dir != NULL) {
2109 if (!fnmatch(MMC_PARTITION_PATH, dir->d_name, 0) ||
2110 !fnmatch(SCSI_PARTITION_PATH, dir->d_name, 0)) {
2122 static bool check_partition(struct udev_device *dev)
2124 const char *devtype;
2125 const char *part_table_type;
2126 const char *fs_usage;
2129 /* only consider disk type, never partitions */
2130 devtype = udev_device_get_devtype(dev);
2134 if (strncmp(devtype, BLOCK_DEVTYPE_DISK,
2135 sizeof(BLOCK_DEVTYPE_DISK)) != 0)
2138 part_table_type = udev_device_get_property_value(dev,
2139 "ID_PART_TABLE_TYPE");
2140 if (part_table_type) {
2141 fs_usage = udev_device_get_property_value(dev,
2144 strncmp(fs_usage, FILESYSTEM_NAME, sizeof(FILESYSTEM_NAME)) == 0) {
2145 if (!disk_is_partitioned_by_kernel(dev))
2152 if (disk_is_partitioned_by_kernel(dev)) {
2161 // Called by MainThread
2162 static int add_block_device(struct udev_device *dev, const char *devnode, bool mapper)
2164 struct block_data *data;
2165 struct block_device *bdev;
2166 //char id_string[PATH_LEN];
2170 bool need_format = false;
2172 partition = check_partition(dev);
2174 /* if there is a partition, skip this request */
2175 _I("%s device has partitions, skip this time.", devnode);
2179 if (mapper && !udev_device_get_property_value(dev, "ID_FS_TYPE")) {
2180 char syspath[128] = {0};
2183 r = rindex(udev_device_get_syspath(dev), '/');
2184 if (!r) return -ENODEV;
2186 snprintf(syspath, sizeof(syspath), "/sys/block%s", r);
2188 data = make_block_data(devnode,
2193 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2194 udev_device_get_sysattr_value(dev, "ro"));
2197 data = make_block_data(devnode,
2198 udev_device_get_syspath(dev),
2199 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2200 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2201 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2202 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2203 udev_device_get_sysattr_value(dev, "ro"));
2207 _E("Failed to make block data for %s.", devnode);
2211 if (!block_conf[data->block_type].multimount && !data->primary &&
2212 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM_NAME)) {
2213 _D("Not support multi mount by config info.");
2214 free_block_data(data);
2218 if (!block_control) {
2219 if (!mapper && strncmp(data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2220 _D("Block module is disabled.");
2221 free_block_data(data);
2227 bdev = make_block_device(data);
2229 _E("Failed to make block device for %s.", devnode);
2230 free_block_data(data);
2234 thread_id = find_thread(bdev->data->devnode);
2235 if (thread_id < 0 || thread_id >= THREAD_MAX) {
2236 _E("Failed to find thread to add.");
2237 free_block_device(bdev);
2240 bdev->thread_id = thread_id;
2242 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2243 th_manager[thread_id].num_dev++;
2244 DD_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
2245 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2248 struct format_data *fdata;
2250 fdata = get_format_data(NULL, UNMOUNT_FORCE);
2252 _E("Failed to get format data.");
2256 ret = add_operation(bdev, BLOCK_DEV_FORMAT, NULL, (void *)fdata);
2258 _E("Failed to add operation(format, %s).", bdev->data->devnode);
2259 release_format_data(fdata);
2263 if (!bdev->data->fs_type) {
2264 _E("Unformatted Storage.");
2265 free_block_device(bdev);
2267 } else if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2268 // bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2269 bdev->data->primary = true;
2270 _D("Need to unlock encrypted sdcard.");
2271 // ---- ODE UI launch ----
2272 ret = launch_system_app(POPUP_DEFAULT
2275 , "unlockextendedsd"
2279 , bdev->data->devnode
2283 _E("Failed to launch popup.");
2285 ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
2287 _E("Failed to add operation(insert, %s).", devnode);
2288 free_block_device(bdev);
2293 } else if (mapper && !strncmp(bdev->data->fs_type, EXT4_NAME, strlen(EXT4_NAME))) {
2294 bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2295 ret = change_mount_point(bdev, EXTENDEDSD_MOUNT_PATH);
2298 free_block_device(bdev);
2303 ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
2305 _E("Failed to add operation(insert, %s).", devnode);
2306 free_block_device(bdev);
2310 // Not a regular filesystem -> skip mounting
2311 if (!bdev->data->fs_usage || strcmp(bdev->data->fs_usage, FILESYSTEM_NAME)) {
2312 _I("Not a filesystem. Not mounting.");
2316 /* Create file for block device /run/external-storage/id */
2317 create_file(bdev->data->id, bdev->data->mount_point, bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
2318 ret = add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL);
2320 _E("Failed to add operation(mount, %s).", devnode);
2326 static int remove_block_device(struct udev_device *dev, const char *devnode)
2328 struct block_device *bdev;
2329 struct block_device *bdev_extended;
2332 bdev = find_block_device(devnode);
2334 _E("Failed to find block data for %s.", devnode);
2338 BLOCK_FLAG_SET(bdev->data, UNMOUNT_UNSAFE);
2340 bdev->removed = true;
2341 if (bdev->on_private_op != REQ_NORMAL) {
2342 bdev->on_private_op = REQ_NORMAL;
2343 _D("Private operation state(%d).", bdev->on_private_op);
2346 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2347 bdev_extended = find_block_device_path(EXTENDEDSD_MOUNT_PATH);
2349 if (bdev_extended) {
2350 BLOCK_FLAG_SET(bdev_extended->data, UNMOUNT_UNSAFE);
2352 bdev_extended->removed = true;
2353 if (bdev_extended->on_private_op != REQ_NORMAL) {
2354 bdev_extended->on_private_op = REQ_NORMAL;
2355 _D("Private operation state(%d).", bdev_extended->on_private_op);
2358 ret = add_operation(bdev_extended, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2360 _E("Failed to add operation(unmount, %s).", devnode);
2364 ret = add_operation(bdev_extended, BLOCK_LUKS_CLOSE, NULL, NULL);
2366 _E("Failed to add operation(luks_close, %s).", devnode);
2370 ret = add_operation(bdev_extended, BLOCK_DEV_REMOVE, NULL, NULL);
2372 _E("Failed to add operation(remove, %s).", devnode);
2376 _E("Failed to find block data for extended sd card.");
2379 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2381 _E("Failed to add operation(unmount, %s).", devnode);
2385 ret = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2387 _E("Failed to add operation(remove, %s).", devnode);
2394 static int get_internal_storage_number(void)
2396 struct libmnt_table *t = NULL;
2397 struct libmnt_fs *fs;
2400 int r = 0, dev_temp;
2402 if ((!is_emulator() && (dev_internal >= 0 || dev_internal_scsi != '\0')) ||
2403 (is_emulator() && dev_internal_emul != '\0'))
2406 t = mnt_new_table();
2410 r = mnt_table_parse_mtab(t, NULL);
2416 fs = mnt_table_find_target(t, ROOT_DIR, MNT_ITER_BACKWARD);
2422 temp = mnt_fs_get_srcpath(fs);
2426 name = strrchr(temp, '/');
2430 /* Boot from USB is not handled */
2431 if (!is_emulator()) {
2432 if (!fnmatch(MMC_PATH, temp, 0))
2433 sscanf(name, "mmcblk%d", &dev_internal);
2434 else if (!fnmatch(SCSI_PATH, temp, 0))
2435 sscanf(name, "sd%c", &dev_internal_scsi);
2437 if (!fnmatch(MMC_LINK_PATH, temp, 0))
2438 sscanf(name, "vd%c%d", &dev_internal_emul, &dev_temp);
2440 dev_internal_emul = '\0';
2448 static int check_external_storage(const char* devnode)
2450 char dev_scsi = '\0';
2453 int dev_num = -1, dev_temp;
2458 name = strrchr(devnode, '/');
2462 if (!is_emulator()) {
2463 if (!fnmatch(MMC_PATH, devnode, 0)) {
2464 sscanf(name, "mmcblk%d", &dev_num);
2465 if (dev_internal == dev_num) {
2466 _D("%s is internal storage.", devnode);
2469 } else if (!fnmatch(SCSI_PATH, devnode, 0)) {
2470 sscanf(name, "sd%c", &dev_scsi);
2471 if (dev_internal_scsi == dev_scsi) {
2472 _D("%s is internal storage.", devnode);
2477 if (!fnmatch(MMC_LINK_PATH, devnode, 0)) {
2478 sscanf(name, "vd%c%d", &emul, &dev_temp);
2479 if (dev_internal_emul == emul) {
2480 _D("%s is internal storage.", devnode);
2489 static int check_already_handled(const char* devnode)
2491 struct block_device *bdev;
2492 struct block_data *data;
2496 for (i = 0; i < THREAD_MAX; i++) {
2497 pthread_mutex_lock(&(th_manager[i].mutex));
2498 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2504 if (!strncmp(data->devnode, devnode, strlen(devnode) + 1)) {
2505 pthread_mutex_unlock(&(th_manager[i].mutex));
2509 pthread_mutex_unlock(&(th_manager[i].mutex));
2515 static int block_init_from_udev_enumerate(void)
2518 struct udev_enumerate *enumerate;
2519 struct udev_list_entry *list_entry, *list_sub_entry;
2520 struct udev_device *dev;
2521 const char *syspath;
2522 const char *devnode;
2527 _E("Failed to create udev library context.");
2531 /* create a list of the devices in the 'usb' subsystem */
2532 enumerate = udev_enumerate_new(udev);
2534 _E("Failed to create an enumeration context.");
2538 udev_enumerate_add_match_subsystem(enumerate, BLOCK_SUBSYSTEM);
2539 udev_enumerate_add_match_property(enumerate,
2540 UDEV_DEVTYPE, BLOCK_DEVTYPE_DISK);
2541 udev_enumerate_add_match_property(enumerate,
2542 UDEV_DEVTYPE, BLOCK_DEVTYPE_PARTITION);
2543 udev_enumerate_scan_devices(enumerate);
2545 udev_list_entry_foreach(list_entry,
2546 udev_enumerate_get_list_entry(enumerate)) {
2547 syspath = udev_list_entry_get_name(list_entry);
2551 dev = udev_device_new_from_syspath(
2552 udev_enumerate_get_udev(enumerate),
2558 udev_list_entry_foreach(list_sub_entry,
2559 udev_device_get_devlinks_list_entry(dev)) {
2560 const char *devlink = udev_list_entry_get_name(list_sub_entry);
2561 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2568 devnode = udev_device_get_devnode(dev);
2570 udev_device_unref(dev);
2574 if (fnmatch(MMC_PATH, devnode, 0) &&
2575 fnmatch(SCSI_PATH, devnode, 0) &&
2576 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0)) {
2577 udev_device_unref(dev);
2582 r = check_external_storage(devnode);
2584 udev_device_unref(dev);
2588 r = check_already_handled(devnode);
2590 _I("%s is already handled.", devnode);
2591 udev_device_unref(dev);
2595 _I("%s device add.", devnode);
2596 add_block_device(dev, devnode, false);
2598 udev_device_unref(dev);
2601 udev_enumerate_unref(enumerate);
2606 // Called by MainThread
2607 static void show_block_device_list(void)
2609 struct block_device *bdev;
2610 struct block_data *data;
2614 for (i = 0; i < THREAD_MAX; i++) {
2615 pthread_mutex_lock(&(th_manager[i].mutex));
2616 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2622 _D("%s:", data->devnode);
2623 _D("\tSyspath=%s", data->syspath);
2624 _D("\tBlock type=%d", data->block_type);
2625 _D("\tFs type=%s", data->fs_type);
2626 _D("\tFs usage=%s", data->fs_usage);
2627 _D("\tFs version=%s", data->fs_version);
2628 _D("\tFs uuid enc=%s", data->fs_uuid_enc);
2630 (data->readonly ? "true" : "false"));
2631 _D("\tMount point=%s", data->mount_point);
2632 _D("\tMount state=%s",
2633 (data->state == BLOCK_MOUNT ?
2634 "mount" : "unmount"));
2636 (data->primary ? "true" : "false"));
2637 _D("\tID=%d", data->id);
2639 pthread_mutex_unlock(&(th_manager[i].mutex));
2643 // Called by MainThread
2644 static void remove_whole_block_device(void)
2646 struct block_device *bdev;
2652 for (i = 0; i < THREAD_MAX; i++) {
2654 pthread_mutex_lock(&(th_manager[i].mutex));
2655 DD_LIST_FOREACH_SAFE(th_manager[i].block_dev_list, elem, next, bdev) {
2656 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
2657 !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
2659 if (bdev->removed == false)
2662 pthread_mutex_unlock(&(th_manager[i].mutex));
2664 if (bdev && bdev->removed == false) {
2665 bdev->removed = true;
2666 r = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2668 _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
2670 r = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2672 _E("Failed to add operation(remove, %s).", bdev->data->devnode);
2679 static void booting_done(void)
2681 static int done = 0;
2687 _I("Booting done.");
2689 /* register mmc uevent control routine */
2690 ret = register_udev_uevent_control(&uh);
2692 _E("Failed to register block uevent: %d", ret);
2694 block_control = true;
2695 /* if there is the attached device, try to mount */
2696 block_init_from_udev_enumerate();
2698 ret = dbus_handle_method_sync(DEVICED_BUS_NAME,
2699 DEVICED_PATH_POWEROFF,
2700 DEVICED_INTERFACE_POWEROFF,
2701 METHOD_ADD_POWEROFF_WAIT,
2704 _E("Failed to call "METHOD_ADD_POWEROFF_WAIT" method.");
2706 add_poweroff_wait = true;
2711 static void block_poweroff(GDBusConnection *conn,
2712 const gchar *sender,
2719 static int status = 0;
2726 /* unregister mmc uevent control routine */
2727 unregister_udev_uevent_control(&uh);
2728 remove_whole_block_device();
2729 terminate_threads();
2731 if (add_poweroff_wait) {
2732 ret = dbus_handle_method_sync(DEVICED_BUS_NAME,
2733 DEVICED_PATH_POWEROFF,
2734 DEVICED_INTERFACE_POWEROFF,
2735 METHOD_REMOVE_POWEROFF_WAIT,
2738 _E("Failed to call "METHOD_REMOVE_POWEROFF_WAIT" method.");
2740 add_poweroff_wait = false;
2744 static void uevent_block_handler(struct udev_device *dev)
2746 const char *devnode = NULL;
2748 struct udev_list_entry *list_entry;
2750 bool mapper = false;
2752 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
2753 const char *devlink = udev_list_entry_get_name(list_entry);
2754 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2758 if (!fnmatch(EXTENDEDSD_NODE_PATH, devlink, 0)) {
2766 devnode = udev_device_get_devnode(dev);
2770 if (fnmatch(MMC_PATH, devnode, 0) &&
2771 fnmatch(SCSI_PATH, devnode, 0))
2775 r = check_external_storage(devnode);
2779 action = udev_device_get_action(dev);
2783 _I("%s device %s.", devnode, action);
2784 if (!strncmp(action, UDEV_ADD, sizeof(UDEV_ADD)) ||
2785 (mapper && !strcmp(action, UDEV_CHANGE))) {
2786 r = check_already_handled(devnode);
2788 _I("%s is already handled.", devnode);
2792 add_block_device(dev, devnode, mapper);
2793 } else if (!strncmp(action, UDEV_REMOVE, sizeof(UDEV_REMOVE))) {
2794 remove_block_device(dev, devnode);
2795 } else if (!strncmp(action, UDEV_CHANGE, sizeof(UDEV_CHANGE))) {
2796 struct block_device *bdev;
2797 bdev = find_block_device(devnode);
2799 _E("Failed to find block data for %s.", devnode);
2802 if (!udev_device_get_property_value(dev, "ID_FS_TYPE"))
2805 r = update_block_data(bdev->data,
2806 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2807 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2808 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2809 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2810 udev_device_get_sysattr_value(dev, "ro"),
2813 _E("Failed to update block data for %s.", bdev->data->devnode);
2814 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
2815 _I("Filesystem type(crypto_LUKS) is updated.");
2816 if (bdev->data->fs_usage)
2817 _I("fs_usage=%s", bdev->data->fs_usage);
2821 static GVariant *request_mount_block(GDBusConnection *conn,
2822 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2823 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data, bool onprivate)
2825 struct block_device *bdev;
2826 char *mount_point = NULL;
2830 if (!block_control) {
2831 _D("Block module is disabled.");
2836 g_variant_get(param, "(is)", &id, &mount_point);
2838 bdev = find_block_device_by_id(id);
2840 _E("Failed to find (%d) in the device list.", id);
2845 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
2846 !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2847 _D("Mount dbus request for extended internal storage is blocked.");
2852 if (bdev->on_private_op != REQ_NORMAL) {
2857 if (bdev->data->state == BLOCK_MOUNT) {
2858 _I("%s is already mounted.", bdev->data->devnode);
2864 bdev->on_private_op = REQ_PRIVATE;
2865 bdev->private_pid = dbus_handle_get_sender_pid(NULL, sender);
2866 _D("Private operation state(%d). pid=%d.", bdev->on_private_op, bdev->private_pid);
2868 if (bdev->on_private_op != REQ_NORMAL) {
2869 _E("Failed to process mount operation.");
2875 /* if requester want to use a specific mount point */
2876 if (mount_point && strncmp(mount_point, "", 1) != 0) {
2877 ret = change_mount_point(bdev, mount_point);
2883 /* Create /run/external-storage/id file */
2884 create_file(bdev->data->id, bdev->data->mount_point, false);
2886 /* Create file for block device /run/external-storage/id */
2887 create_file(bdev->data->id, bdev->data->mount_point, false);
2890 ret = add_operation(bdev, BLOCK_DEV_MOUNT, invocation, NULL);
2892 _E("Failed to add operation(mount, %s).", bdev->data->devnode);
2896 g_free(mount_point);
2900 g_free(mount_point);
2901 return g_variant_new("(i)", ret);
2904 static GVariant *request_public_mount_block(GDBusConnection *conn,
2905 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2906 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
2908 return request_mount_block(conn, sender, path, iface, name, param, invocation, user_data, false);
2911 static GVariant *request_private_mount_block(GDBusConnection *conn,
2912 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2913 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
2915 return request_mount_block(conn, sender, path, iface, name, param, invocation, user_data, true);
2918 static GVariant *request_unmount_block(GDBusConnection *conn,
2919 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2920 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data, bool onprivate)
2922 struct block_device *bdev;
2928 if (!block_control) {
2929 _D("Block module is disabled.");
2934 g_variant_get(param, "(ii)", &id, &option);
2936 bdev = find_block_device_by_id(id);
2938 _E("Failed to find (%d) in the device list.", id);
2943 /* Unmount dbus call is needed when app proceeds extended internal -> portable storage */
2944 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2945 _D("Unmount dbus request for extended internal storage is blocked.");
2951 pid = dbus_handle_get_sender_pid(NULL, sender);
2952 if (bdev->on_private_op == REQ_NORMAL || (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid)) {
2953 _E("Failed to process private unmount operation pid=%d private_pid=%d.", pid, bdev->private_pid);
2958 if (bdev->on_private_op != REQ_NORMAL) {
2959 _E("Failed to process unmount operation.");
2965 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, invocation, (void *)option);
2967 _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
2971 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV) {
2972 ret = add_operation(bdev, BLOCK_LUKS_CLOSE, NULL, NULL);
2974 _E("Failed to add operation(luks_close, %s).", bdev->data->devnode);
2980 return g_variant_new("(i)", ret);
2983 static GVariant *request_public_unmount_block(GDBusConnection *conn,
2984 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2985 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
2987 return request_unmount_block(conn, sender, path, iface, name, param, invocation, user_data, false);
2990 static GVariant *request_private_unmount_block(GDBusConnection *conn,
2991 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2992 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
2994 return request_unmount_block(conn, sender, path, iface, name, param, invocation, user_data, true);
2997 static GVariant *request_format_block(GDBusConnection *conn,
2998 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
2999 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3001 struct block_device *bdev;
3002 struct format_data *fdata;
3009 if (!block_control) {
3010 _D("Block module is disabled.");
3015 g_variant_get(param, "(ii)", &id, &option);
3017 bdev = find_block_device_by_id(id);
3019 _E("Failed to find (%d) in the device list.", id);
3023 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
3024 !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
3025 _D("Format dbus request for extended internal storage is blocked.");
3030 pid = dbus_handle_get_sender_pid(NULL, sender);
3031 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
3032 _E("Failed to format on private state.");
3037 fdata = get_format_data(NULL, option);
3039 _E("Failed to get format data.");
3043 prev_state = bdev->data->state;
3044 if (prev_state == BLOCK_MOUNT) {
3045 if (bdev->on_private_op == REQ_PRIVATE) {
3046 bdev->on_private_op = REQ_PRIVATE_FORMAT;
3047 _D("Private operation state(%d)", bdev->on_private_op);
3049 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
3051 _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
3052 release_format_data(fdata);
3057 ret = add_operation(bdev, BLOCK_DEV_FORMAT, invocation, (void *)fdata);
3059 _E("Failed to add operation(format, %s).", bdev->data->devnode);
3060 release_format_data(fdata);
3063 /* Maintain previous state of mount/unmount */
3064 if (prev_state == BLOCK_MOUNT) {
3065 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
3066 _E("Failed to add operation(mount, %s).", bdev->data->devnode);
3074 return g_variant_new("(i)", ret);
3077 static GVariant *request_format_block_type(GDBusConnection *conn,
3078 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3079 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3081 struct block_device *bdev;
3082 struct format_data *fdata;
3090 if (!block_control) {
3091 _D("Block module is disabled.");
3096 g_variant_get(param, "(iis)", &id, &option, &type);
3098 bdev = find_block_device_by_id(id);
3100 _E("Failed to find (%d) in the device list.", id);
3104 /* FormatwithType dbus call is needed when app proceeds extended internal -> portable storage */
3105 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV) {
3106 _D("FormatwithType dbus request for extended internal storage is blocked.");
3111 pid = dbus_handle_get_sender_pid(NULL, sender);
3112 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
3113 _E("Failed to format on private state.");
3118 fdata = get_format_data(type, option);
3120 _E("Failed to get format data.");
3124 prev_state = bdev->data->state;
3125 if (prev_state == BLOCK_MOUNT) {
3126 if (bdev->on_private_op == REQ_PRIVATE) {
3127 bdev->on_private_op = REQ_PRIVATE_FORMAT;
3128 _D("Private operation state(%d).", bdev->on_private_op);
3130 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
3132 _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
3133 release_format_data(fdata);
3138 ret = add_operation(bdev, BLOCK_DEV_FORMAT, invocation, (void *)fdata);
3140 _E("Failed to add operation(format, %s).", bdev->data->devnode);
3141 release_format_data(fdata);
3144 /* Maintain previous state of mount/unmount */
3145 if (prev_state == BLOCK_MOUNT) {
3146 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
3147 _E("Failed to add operation(mount, %s).", bdev->data->devnode);
3157 return g_variant_new("(i)", ret);
3160 static GVariant *block_data_to_gvariant(struct block_data *data, int flags)
3163 return dbus_handle_new_g_variant_tuple();
3165 return g_variant_new("(issssssisibii)",
3167 nullstr(data->devnode),
3168 nullstr(data->syspath),
3169 nullstr(data->fs_usage),
3170 nullstr(data->fs_type),
3171 nullstr(data->fs_version),
3172 nullstr(data->fs_uuid_enc),
3174 nullstr(data->mount_point),
3177 flags >= 0 ? flags : data->flags,
3181 static GVariant *block_data_to_gvariant2(struct block_data *data, int flags)
3184 return dbus_handle_new_g_variant_tuple();
3186 return g_variant_new("(issssssisibi)",
3188 nullstr(data->devnode),
3189 nullstr(data->syspath),
3190 nullstr(data->fs_usage),
3191 nullstr(data->fs_type),
3192 nullstr(data->fs_version),
3193 nullstr(data->fs_uuid_enc),
3195 nullstr(data->mount_point),
3198 flags >= 0 ? flags : data->flags);
3202 //static int add_device_to_struct_iter(struct block_data *data, DBusMessageIter *iter)
3204 // //DBusMessageIter piter;
3206 // //if (!data || !iter)
3207 // // return -EINVAL;
3209 // //dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3210 // //add_device_to_iter(data, &piter);
3211 // //dbus_message_iter_close_container(iter, &piter);
3216 //static int add_device_to_iter_2(struct block_data *data, DBusMessageIter *iter)
3218 // DBusMessageIter piter;
3219 // char *str_null = "";
3221 // if (!data || !iter)
3224 // //dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3225 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3226 // // &(data->block_type));
3227 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3228 // // data->devnode ? &(data->devnode) : &str_null);
3229 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3230 // // data->syspath ? &(data->syspath) : &str_null);
3231 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3232 // // data->fs_usage ? &(data->fs_usage) : &str_null);
3233 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3234 // // data->fs_type ? &(data->fs_type) : &str_null);
3235 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3236 // // data->fs_version ? &(data->fs_version) : &str_null);
3237 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3238 // // data->fs_uuid_enc ? &(data->fs_uuid_enc) : &str_null);
3239 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3240 // // &(data->readonly));
3241 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3242 // // data->mount_point ? &(data->mount_point) : &str_null);
3243 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3244 // // &(data->state));
3245 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_BOOLEAN,
3246 // // &(data->primary));
3247 // //dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3248 // // &(data->flags));
3249 // //dbus_message_iter_close_container(iter, &piter);
3254 static GVariant *request_get_device_info(GDBusConnection *conn,
3255 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3256 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3258 struct block_device *bdev = NULL;
3259 struct block_data *data = NULL;
3260 struct block_data nodata = {0,};
3263 g_variant_get(param, "(i)", &id);
3265 bdev = find_block_device_by_id(id);
3274 nodata.id = -ENODEV;
3278 return block_data_to_gvariant(data, -1);
3281 static GVariant *request_show_device_list(GDBusConnection *conn,
3282 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3283 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3285 show_block_device_list();
3286 return dbus_handle_new_g_variant_tuple();
3289 static enum block_device_type get_bdev_type_from_type_string(const char *type_str)
3292 return BLOCK_UNKNOWN_DEV;
3294 if (strcmp(type_str, BLOCK_TYPE_SCSI) == 0)
3295 return BLOCK_SCSI_DEV;
3296 if (strcmp(type_str, BLOCK_TYPE_MMC) == 0)
3297 return BLOCK_MMC_DEV;
3298 if (strcmp(type_str, BLOCK_TYPE_ALL) == 0)
3299 return BLOCK_ALL_DEV;
3301 return BLOCK_UNKNOWN_DEV;
3304 // Called by MainThread
3305 static GVariant *request_get_device_list(GDBusConnection *conn,
3306 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3307 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3309 GVariant *reply = NULL;
3310 struct block_device *bdev;
3311 struct block_data *data;
3314 enum block_device_type block_type;
3316 GVariantBuilder *builder = NULL;
3317 const char *error = NULL;
3319 g_variant_get(param, "(s)", &type);
3321 _D("Block (%s) device list is requested.", type);
3323 block_type = get_bdev_type_from_type_string(type);
3324 if (block_type == BLOCK_UNKNOWN_DEV) {
3325 _E("Invalid type (%s) is requested.", type);
3326 error = "Invalid type is requested";
3330 builder = g_variant_builder_new(G_VARIANT_TYPE("a(issssssisibii)"));
3332 for (i = 0; i < THREAD_MAX; i++) {
3333 pthread_mutex_lock(&(th_manager[i].mutex));
3334 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3335 if (!bdev->data || bdev->removed)
3340 if (block_type != BLOCK_ALL_DEV) {
3341 if (data->block_type != block_type)
3345 g_variant_builder_add(builder, "(issssssisibii)",
3347 nullstr(data->devnode),
3348 nullstr(data->syspath),
3349 nullstr(data->fs_usage),
3350 nullstr(data->fs_type),
3351 nullstr(data->fs_version),
3352 nullstr(data->fs_uuid_enc),
3354 nullstr(data->mount_point),
3360 pthread_mutex_unlock(&(th_manager[i].mutex));
3363 reply = g_variant_new("(a(issssssisibii))", builder);
3365 g_variant_builder_unref(builder);
3371 g_dbus_method_invocation_return_error(invocation,
3372 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
3377 // Called by MainThread
3378 static GVariant *request_get_device_list_2(GDBusConnection *conn,
3379 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3380 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3382 GVariant *reply = NULL;
3383 struct block_device *bdev;
3384 struct block_data *data;
3387 enum block_device_type block_type;
3389 GVariantBuilder *builder = NULL;
3390 const char *error = NULL;
3392 g_variant_get(param, "(s)", &type);
3394 _E("Delivered type is NULL.");
3395 error = "Delivered type is NULL";
3399 _D("Block (%s) device list is requested.", type);
3401 block_type = get_bdev_type_from_type_string(type);
3402 if (block_type == BLOCK_UNKNOWN_DEV) {
3403 _E("Invalid type (%s) is requested.", type);
3404 error = "Invalid type is requested";
3408 builder = g_variant_builder_new(G_VARIANT_TYPE("a(issssssisibi)"));
3410 for (i = 0; i < THREAD_MAX; i++) {
3411 pthread_mutex_lock(&(th_manager[i].mutex));
3412 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3413 if (!bdev->data || bdev->removed)
3418 if (block_type != BLOCK_ALL_DEV) {
3419 if (data->block_type != block_type)
3423 g_variant_builder_add(builder, "(issssssisibi)",
3425 nullstr(data->devnode),
3426 nullstr(data->syspath),
3427 nullstr(data->fs_usage),
3428 nullstr(data->fs_type),
3429 nullstr(data->fs_version),
3430 nullstr(data->fs_uuid_enc),
3432 nullstr(data->mount_point),
3437 pthread_mutex_unlock(&(th_manager[i].mutex));
3440 reply = g_variant_new("(a(issssssisibi))", builder);
3442 g_variant_builder_unref(builder);
3448 g_dbus_method_invocation_return_error(invocation,
3449 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
3454 static GVariant *request_get_mmc_primary(GDBusConnection *conn,
3455 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3456 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3458 struct block_device *bdev;
3459 struct block_data *data, nodata = {0,};
3464 for (i = 0; i < THREAD_MAX; i++) {
3465 pthread_mutex_lock(&(th_manager[i].mutex));
3466 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3472 if (data->block_type != BLOCK_MMC_DEV &&
3473 data->block_type != BLOCK_EXTENDEDSD_DEV)
3477 // Return mapper node(/dev/mapper/extendedsd) for primary mmc (not /dev/mmcblk1p1(ex))
3478 if (!strncmp(data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
3483 pthread_mutex_unlock(&(th_manager[i].mutex));
3489 nodata.id = -ENODEV;
3493 return block_data_to_gvariant(data, -1);
3496 static GVariant *request_check_speed(GDBusConnection *conn,
3497 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3498 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3500 struct timespec start_time, end_time;
3501 struct block_device *bdev;
3502 struct block_data *data;
3510 g_variant_get(param, "(i)", &id);
3512 bdev = find_block_device_by_id(id);
3523 _D("Speed check %s.", data->devnode);
3524 fd = open(data->devnode, O_RDONLY | O_DIRECT);
3526 _E("Failed to open fd(%s): %d", data->devnode, errno);
3530 ret = posix_memalign((void**)&buf, 4096, SPEEDCHECK_SIZE << 20);
3532 _E("Failed to posix_memalign().");
3537 clock_gettime(CLOCK_REALTIME, &start_time);
3538 _I("Start time=%lu.%lu", start_time.tv_sec, start_time.tv_nsec);
3539 ret = read(fd, buf, SPEEDCHECK_SIZE << 20);
3540 clock_gettime(CLOCK_REALTIME, &end_time);
3541 _I("End time=%lu.%lu", end_time.tv_sec, end_time.tv_nsec);
3546 _E("Failed to read(): %d", errno);
3551 time_diff = end_time.tv_sec - start_time.tv_sec;
3552 if (time_diff > 0 && (SPEEDCHECK_SIZE / time_diff < SPEEDCHECK_CRITERION)) {
3560 return g_variant_new("(i)", result);
3564 static GVariant *request_control_block(GDBusConnection *conn,
3565 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3566 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3571 g_variant_get(param, "(i)", &enable);
3575 _I("Control block disable.");
3580 _I("Control block enable.");
3585 _E("Control block. Wrong request by client.");
3590 return g_variant_new("(i)", result);
3593 static GVariant *request_getcontrol_block(GDBusConnection *conn,
3594 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
3595 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
3599 _I("Get control block.");
3601 result = block_control;
3603 return g_variant_new("(i)", result);
3607 Method name Method call format string Reply format string
3608 { "ShowDeviceList", NULL, NULL, request_show_device_list },
3609 { "GetDeviceList", "s", "a(issssssisibii)", request_get_device_list },
3610 { "GetDeviceList2", "s", "a(issssssisibi)", request_get_device_list_2 },
3611 { "Mount", "is", "i", request_public_mount_block },
3612 { "Unmoun, "ii", "i", request_public_unmount_block },
3613 { "Format", "ii", "i", request_format_block },
3614 { "GetDeviceInfo", "i", "(issssssisibii)", request_get_device_info },
3615 { "GetMmcPrimary", NULL, "(issssssisibii)" , request_get_mmc_primary },
3616 { "PrivateMount", "is", "i", request_private_mount_block },
3617 { "PrivateUnmount", "ii", "i", request_private_unmount_block },
3620 static const dbus_method_s manager_methods[] = {
3621 { "ShowDeviceList", NULL, NULL, request_show_device_list },
3622 { "GetDeviceList" , "s", "a(issssssisibii)", request_get_device_list },
3623 { "GetDeviceList2", "s", "a(issssssisibi)", request_get_device_list_2 },
3624 { "Mount", "is", "i", request_public_mount_block },
3625 { "Unmount", "ii", "i", request_public_unmount_block },
3626 { "Format", "ii", "i", request_format_block },
3627 { "FormatwithType", "iis", "i", request_format_block_type },
3628 { "GetDeviceInfo", "i", "issssssisibii", request_get_device_info },
3629 { "GetMmcPrimary", NULL, "issssssisibii", request_get_mmc_primary },
3630 { "PrivateMount", "is", "i", request_private_mount_block },
3631 { "PrivateUnmount", "ii", "i", request_private_unmount_block },
3632 { "CheckSpeed", "i", "i", request_check_speed },
3633 { "Control", "i", "i", request_control_block },
3634 { "GetControl", NULL, "i", request_getcontrol_block },
3637 static const dbus_interface_u block_interface = {
3638 .name = STORAGED_INTERFACE_BLOCK_MANAGER,
3639 .methods = manager_methods,
3640 .nr_methods = ARRAY_SIZE(manager_methods),
3643 static int load_config(struct parse_result *result, void *user_data)
3647 if (MATCH(result->section, "Block"))
3650 if (MATCH(result->section, "SCSI"))
3651 index = BLOCK_SCSI_DEV;
3652 else if (MATCH(result->section, "MMC"))
3653 index = BLOCK_MMC_DEV;
3654 else if (MATCH(result->section, "Mapper"))
3655 index = BLOCK_EXTENDEDSD_DEV;
3659 if (MATCH(result->name, "Multimount"))
3660 block_conf[index].multimount =
3661 (MATCH(result->value, "yes") ? true : false);
3662 if (MATCH(result->name, "ExtendedInternalStorage"))
3663 block_conf[index].extendedinternal =
3664 (MATCH(result->value, "yes") ? true : false);
3670 static int mount_root_path_tmpfs(void)
3675 root = tzplatform_getenv(TZ_SYS_MEDIA);
3679 if (access(root, F_OK) != 0)
3682 if (mount_check(root))
3685 ret = mount("tmpfs", root, "tmpfs", 0, "smackfsroot=System::Shared");
3688 _E("Failed to mount tmpfs: %d", ret);
3695 #define mount_root_path_tmpfs() 0
3698 static guint id_block_poweroff;
3700 static void block_init(void *data)
3708 ret = config_parse(BLOCK_CONF_FILE, load_config, NULL);
3710 _E("Failed to load '%s'. Use default value.", BLOCK_CONF_FILE);
3712 ret = mount_root_path_tmpfs();
3714 _E("Failed to mount tmpfs to root mount path: %d", ret);
3716 /* register block manager object and interface */
3717 ret = dbus_handle_register_dbus_object(NULL, STORAGED_PATH_BLOCK_MANAGER, &block_interface);
3719 _E("Failed to register block interface and methods: %d", ret);
3724 _E("Failed to init pipe.");
3726 for (i = 0; i < THREAD_MAX; i++) {
3727 th_manager[i].num_dev = 0;
3728 th_manager[i].op_len = 0;
3729 th_manager[i].start_th = false;
3730 th_manager[i].thread_id = i;
3731 pthread_mutex_init(&(th_manager[i].mutex), NULL);
3732 pthread_cond_init(&(th_manager[i].cond), NULL);
3735 ret = remove_directory(EXTERNAL_STORAGE_PATH);
3737 _E("Failed to remove directory.");
3738 ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
3740 _E("Failed to make directory: %d", errno);
3742 ret = remove_directory(EXTENDED_INTERNAL_PATH);
3744 _E("Failed to remove directory.");
3745 ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
3747 _E("Failed to make directory: %d", errno);
3749 ret = get_internal_storage_number();
3751 _E("Failed to get internal storage number.");
3753 id_block_poweroff = subscribe_dbus_signal(NULL, DEVICED_PATH_POWEROFF,
3754 DEVICED_INTERFACE_POWEROFF,
3755 SIGNAL_POWEROFF_STATE,
3756 block_poweroff, NULL, NULL);
3760 static void terminate_threads(void)
3762 dd_list *elem, *elem_next;
3765 const int WAIT_TIME = 10;
3767 for (i = 0; i < THREAD_MAX; i++) {
3768 if (th_manager[i].start_th) {
3770 while ((th_manager[i].op_len != 0) && (count < WAIT_TIME)) {
3771 _I("Thread(%d) job is not finished. Wait a second.", th_manager[i].thread_id);
3775 pthread_cancel(th_manager[i].th);
3776 pthread_join(th_manager[i].th, NULL);
3778 DD_LIST_FOREACH_SAFE(th_manager[i].th_node_list, elem, elem_next, temp) {
3779 DD_LIST_REMOVE(th_manager[i].th_node_list, temp);
3785 static void block_exit(void *data)
3791 /* unregister notifier for below each event */
3792 unsubscribe_dbus_signal(NULL, id_block_poweroff);
3794 /* unregister mmc uevent control routine */
3795 ret = unregister_udev_uevent_control(&uh);
3797 _E("Failed to unregister block uevent: %d", ret);
3799 /* remove remaining blocks */
3800 remove_whole_block_device();
3802 terminate_threads();
3807 if (add_poweroff_wait) {
3808 ret = dbus_handle_method_sync(DEVICED_BUS_NAME,
3809 DEVICED_PATH_POWEROFF,
3810 DEVICED_INTERFACE_POWEROFF,
3811 METHOD_REMOVE_POWEROFF_WAIT,
3814 _E("Failed to call "METHOD_REMOVE_POWEROFF_WAIT" method.");
3816 add_poweroff_wait = false;
3819 block_control = false;
3822 static int block_start(void *data)
3825 _E("Cannot be started. Booting is not ready.");
3829 if (block_control) {
3830 _I("Already started.");
3834 block_control = true;
3836 block_init_from_udev_enumerate();
3842 static int block_stop(void *data)
3845 _E("Cannot be stopped. Booting is not ready.");
3849 if (!block_control) {
3850 _I("Already stopped.");
3854 /* remove the existing blocks */
3855 remove_whole_block_device();
3857 block_control = false;
3863 static storaged_module_interface block_module = {
3867 .start = block_start,
3871 __attribute__ ((visibility("default")))storaged_module_interface *
3872 storaged_get_module_interface(void)
3874 return &block_module;