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>
46 #include "config-parser.h"
47 #include "module-intf.h"
52 #include "fd_handler.h"
57 * TODO Assume root device is always mmcblk0*.
59 #define MMC_PATH "*/mmcblk[0-9]*"
60 #define MMC_PARTITION_PATH "mmcblk[0-9]p[0-9]"
61 /* Emulator send devlink for sdcard as \*\/sdcard\/\* */
62 #define MMC_LINK_PATH "*/sdcard/*"
63 #define SCSI_PATH "*/sd[a-z]*"
64 #define SCSI_PARTITION_PATH "sd[a-z][0-9]"
65 #define SCSI_PARTITION_LENGTH 9
66 #define EXTENDEDSD_NODE_PATH "/dev/mapper/extendedsd*"
68 #define FILESYSTEM "filesystem"
70 #define DEV_PREFIX "/dev/"
73 #define UNMOUNT_RETRY 5
74 #define TIMEOUT_MAKE_OBJECT 500 /* milliseconds */
76 #define SIGNAL_POWEROFF_STATE "ChangeState"
78 #define BLOCK_DEVICE_ADDED "DeviceAdded"
79 #define BLOCK_DEVICE_REMOVED "DeviceRemoved"
80 #define BLOCK_DEVICE_BLOCKED "DeviceBlocked"
81 #define BLOCK_DEVICE_CHANGED "DeviceChanged"
82 #define BLOCK_DEVICE_CHANGED_2 "DeviceChanged2"
84 #define BLOCK_TYPE_MMC "mmc"
85 #define BLOCK_TYPE_SCSI "scsi"
86 #define BLOCK_TYPE_ALL "all"
88 #define BLOCK_MMC_NODE_PREFIX "SDCard"
89 #define BLOCK_SCSI_NODE_PREFIX "USBDrive"
91 #define BLOCK_CONF_FILE "/etc/storaged/block.conf"
93 #define EXTERNAL_STORAGE_PATH "/run/external-storage"
94 #define EXTENDED_INTERNAL_PATH "/run/extended-internal-sd"
97 #define EXTENDEDSD_MOUNT_POINT "/opt/extendedsd"
98 #define EXTENDEDSD_STRING "ExtendedInternalSD"
100 #define EXT4_NAME "ext4"
101 #define LUKS_NAME "crypto_LUKS"
102 #define EXTENDEDSD_NAME "extendedsd"
104 /* Minimum value of block id */
105 #define BLOCK_ID_MIN 10
106 /* For 2.4 Backward Compatibility */
107 #define EXT_PRIMARY_SD_FIXID 1
109 /* Maximum number of thread */
112 #define SPEEDCHECK 16
114 #define PKGDIR_BUS_NAME "org.tizen.pkgdir_tool"
115 #define PKGDIR_PATH "/org/tizen/pkgdir_tool"
116 #define PKGDIR_INTERFACE "org.tizen.pkgdir_tool"
118 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
120 enum block_dev_operation {
128 enum private_operation_state {
134 struct operation_queue {
135 enum block_dev_operation op;
136 dbus_method_reply_handle_h reply_handle;
141 struct block_device {
142 struct block_data *data;
144 int thread_id; /* Current thread ID */
145 bool removed; /* True when device is physically removed but operation is not precessed yet */
146 enum private_operation_state on_private_op;
147 bool mount_point_updated;
152 struct block_device *bdev;
154 enum unmount_operation option;
159 enum block_dev_operation op;
160 struct block_device *bdev;
164 static struct block_conf {
166 bool extendedinternal;
167 } block_conf[BLOCK_EXTENDEDSD_DEV + 1];
169 static struct manage_thread {
170 dd_list *th_node_list; /* List of devnode which thread dealt with. Only main thread access */
171 dd_list *block_dev_list; /* Use thread mutex */
173 pthread_mutex_t mutex;
175 int num_dev; /* Number of devices which thread holds. Only main thread access */
176 int op_len; /* Number of operation of thread. Use thread mutex */
177 int thread_id; /* Never changed */
179 } th_manager[THREAD_MAX];
181 static dd_list *fs_head;
182 static dd_list *block_ops_list;
185 static fd_handler_h phandler;
186 static bool block_control = false;
187 static bool block_boot = false;
188 static pthread_mutex_t pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
190 /* Assume there is only one physical internal storage */
191 static int dev_internal = -1;
192 static char dev_internal_scsi = '\0';
193 static char dev_internal_emul = '\0';
195 static int add_operation(struct block_device *bdev,
196 enum block_dev_operation operation,
197 dbus_method_reply_handle_h reply_handle, void *data);
198 static void remove_operation(struct block_device *bdev);
199 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
200 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
201 static int change_mount_point(struct block_device *bdev, const char *mount_point);
203 static void uevent_block_handler(struct udev_device *dev);
204 static struct uevent_handler uh = {
205 .subsystem = BLOCK_SUBSYSTEM,
206 .uevent_func = uevent_block_handler,
209 static void __CONSTRUCTOR__ smack_check(void)
214 fp = fopen("/proc/filesystems", "r");
218 while (fgets(buf, sizeof(buf), fp) != NULL) {
219 if (strstr(buf, "smackfs")) {
228 void add_fs(const struct block_fs_ops *fs)
230 DD_LIST_APPEND(fs_head, (void *)fs);
233 void remove_fs(const struct block_fs_ops *fs)
235 DD_LIST_REMOVE(fs_head, (void *)fs);
238 const struct block_fs_ops *find_fs(enum block_fs_type type)
240 struct block_fs_ops *fs;
243 DD_LIST_FOREACH(fs_head, elem, fs) {
244 if (fs->type == type)
250 void add_block_dev(const struct block_dev_ops *ops)
252 DD_LIST_APPEND(block_ops_list, (void *)ops);
255 void remove_block_dev(const struct block_dev_ops *ops)
257 DD_LIST_REMOVE(block_ops_list, (void *)ops);
260 static void broadcast_block_info(enum block_dev_operation op,
261 struct block_data *data, int result)
263 struct block_dev_ops *ops;
266 if (data->primary != true)
269 DD_LIST_FOREACH(block_ops_list, elem, ops) {
270 if (ops->block_type != data->block_type)
272 // TODO What happend on extended internal storage case?
273 if (op == BLOCK_DEV_MOUNT) {
274 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
275 ops->mounted(data, result, true);
277 ops->mounted(data, result, false);
278 } else if (op == BLOCK_DEV_UNMOUNT) {
279 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
280 ops->unmounted(data, result, true);
282 ops->unmounted(data, result, false);
283 } else if (op == BLOCK_DEV_FORMAT) {
284 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
285 ops->formatted(data, result, true);
287 ops->formatted(data, result, false);
288 } else if (op == BLOCK_DEV_INSERT)
290 else if (op == BLOCK_DEV_REMOVE)
295 // Called by MainThread - Insert
296 static int block_get_new_id(void)
298 static int id = BLOCK_ID_MIN;
299 struct block_device *bdev;
304 for (i = 0 ; i < INT_MAX ; i++) {
306 for (j = 0; j < THREAD_MAX; j++) {
307 pthread_mutex_lock(&(th_manager[j].mutex));
308 DD_LIST_FOREACH(th_manager[j].block_dev_list, elem, bdev) {
309 if (bdev->data->id == id) {
314 pthread_mutex_unlock(&(th_manager[j].mutex));
329 static void remove_file(int id, bool extendedsd)
331 char file_name[PATH_LEN];
338 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
340 snprintf(file_name, sizeof(file_name), EXTERNAL_STORAGE_PATH"/%d", id);
342 ret = remove(file_name);
344 _E("Fail to remove %s. errno: %d", file_name, errno);
347 static void create_file(int id, char *mount_point, bool extendedsd)
350 char file_name[PATH_LEN];
356 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
358 snprintf(file_name, PATH_LEN, EXTERNAL_STORAGE_PATH"/%d", id);
360 fp = fopen(file_name, "w+");
362 fprintf(fp, "%s", mount_point);
365 _E("Fail to open %s", file_name);
368 static void signal_device_blocked(struct block_device *bdev)
370 struct block_data *data;
372 char str_block_type[32];
373 char str_readonly[32];
375 char str_primary[32];
381 if (!bdev || !bdev->data)
387 /* Broadcast outside with BlockManager iface */
388 snprintf(str_block_type, sizeof(str_block_type),
389 "%d", data->block_type);
390 arr[0] = str_block_type;
391 arr[1] = (data->devnode ? data->devnode : str_null);
392 arr[2] = (data->syspath ? data->syspath : str_null);
393 arr[3] = (data->fs_usage ? data->fs_usage : str_null);
394 arr[4] = (data->fs_type ? data->fs_type : str_null);
395 arr[5] = (data->fs_version ? data->fs_version : str_null);
396 arr[6] = (data->fs_uuid_enc ? data->fs_uuid_enc : str_null);
397 snprintf(str_readonly, sizeof(str_readonly),
398 "%d", data->readonly);
399 arr[7] = str_readonly;
400 arr[8] = (data->mount_point ? data->mount_point : str_null);
401 snprintf(str_state, sizeof(str_state),
404 snprintf(str_primary, sizeof(str_primary),
405 "%d", data->primary);
406 arr[10] = str_primary;
407 snprintf(str_flags, sizeof(str_flags), "%d", flags);
409 snprintf(str_id, sizeof(str_id), "%d", data->id);
412 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
413 STORAGED_INTERFACE_BLOCK_MANAGER,
414 BLOCK_DEVICE_BLOCKED,
415 "issssssisibii", arr);
418 static void signal_device_changed(struct block_device *bdev,
419 enum block_dev_operation op)
421 struct block_data *data;
423 char str_block_type[32];
424 char str_readonly[32];
426 char str_primary[32];
432 if (!bdev || !bdev->data)
438 case BLOCK_DEV_MOUNT:
439 BLOCK_GET_MOUNT_FLAGS(data, flags);
441 case BLOCK_DEV_UNMOUNT:
442 BLOCK_GET_UNMOUNT_FLAGS(data, flags);
444 case BLOCK_DEV_FORMAT:
445 BLOCK_GET_FORMAT_FLAGS(data, flags);
452 /* Broadcast outside with BlockManager iface */
453 snprintf(str_block_type, sizeof(str_block_type),
454 "%d", data->block_type);
455 arr[0] = str_block_type;
456 arr[1] = (data->devnode ? data->devnode : str_null);
457 arr[2] = (data->syspath ? data->syspath : str_null);
458 arr[3] = (data->fs_usage ? data->fs_usage : str_null);
459 arr[4] = (data->fs_type ? data->fs_type : str_null);
460 arr[5] = (data->fs_version ? data->fs_version : str_null);
461 arr[6] = (data->fs_uuid_enc ? data->fs_uuid_enc : str_null);
462 snprintf(str_readonly, sizeof(str_readonly),
463 "%d", data->readonly);
464 arr[7] = str_readonly;
465 arr[8] = (data->mount_point ? data->mount_point : str_null);
466 snprintf(str_state, sizeof(str_state),
469 snprintf(str_primary, sizeof(str_primary),
470 "%d", data->primary);
471 arr[10] = str_primary;
472 snprintf(str_flags, sizeof(str_flags), "%d", flags);
474 snprintf(str_id, sizeof(str_id), "%d", data->id);
477 if (op == BLOCK_DEV_INSERT)
478 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
479 STORAGED_INTERFACE_BLOCK_MANAGER,
481 "issssssisibii", arr);
482 else if (op == BLOCK_DEV_REMOVE)
483 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
484 STORAGED_INTERFACE_BLOCK_MANAGER,
485 BLOCK_DEVICE_REMOVED,
486 "issssssisibii", arr);
488 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
489 STORAGED_INTERFACE_BLOCK_MANAGER,
490 BLOCK_DEVICE_CHANGED,
491 "issssssisibii", arr);
492 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
493 STORAGED_INTERFACE_BLOCK_MANAGER,
494 BLOCK_DEVICE_CHANGED_2,
495 "issssssisibi", arr);
499 static int get_mmc_mount_node(char *devnode, char *node, size_t len)
501 char *name = devnode;
502 int dev = -1, part = -1;
503 char emul[32] = { 0, };
510 sscanf(name, "mmcblk%dp%d", &dev, &part);
513 snprintf(node, len, "%s%c", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1);
515 snprintf(node, len, "%s%c%d", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1, part);
520 sscanf(name, "vd%31s", emul);
523 for (i = 0 ; i < strlen(emul) ; i++)
524 emul[i] = toupper(emul[i]);
525 snprintf(node, len, "%s%s", BLOCK_MMC_NODE_PREFIX, emul);
529 static int get_scsi_mount_node(char *devnode, char *node, size_t len)
537 snprintf(dev, sizeof(dev), "%s", devnode);
539 if (!strstr(dev, "sd"))
543 name += strlen("sd");
545 for (i = 0 ; i < strlen(name) ; i++)
546 name[i] = toupper(name[i]);
547 snprintf(node, len, "%s%s", BLOCK_SCSI_NODE_PREFIX, name);
552 static char *generate_mount_path(struct block_data *data)
555 char *name, node[64];
558 if (!data || !data->devnode || !data->fs_usage || strcmp(data->fs_usage, FILESYSTEM))
561 name = strrchr(data->devnode, '/');
566 switch (data->block_type) {
568 ret = get_mmc_mount_node(name, node, sizeof(node));
571 ret = get_scsi_mount_node(name, node, sizeof(node));
573 case BLOCK_EXTENDEDSD_DEV:
574 return strdup(EXTENDEDSD_MOUNT_POINT);
576 _E("Invalid block type (%d)", data->block_type);
582 str = tzplatform_mkpath(TZ_SYS_MEDIA, node);
588 _E("Invalid devnode (%s)", data->devnode ? data->devnode : "NULL");
592 static bool check_primary_partition(const char *devnode)
594 struct block_fs_ops *fs;
597 const char *filesystem = NULL;
607 if (fnmatch(MMC_LINK_PATH, devnode, 0) &&
608 fnmatch(MMC_PATH, devnode, 0) &&
609 fnmatch(SCSI_PATH, devnode, 0) &&
610 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
613 temp = strrchr(devnode, '/');
616 if (fnmatch("/"SCSI_PARTITION_PATH, temp, 0) &&
617 fnmatch("/"MMC_PARTITION_PATH, temp, 0))
620 /* Emulator support only one partition */
624 snprintf(str, sizeof(str), "%s", devnode);
629 for (i = 1; i <= 9; ++i) {
630 snprintf(str2, sizeof(str2), "%s%d", str, i);
631 if (access(str2, R_OK) != 0)
634 probe = blkid_new_probe_from_filename(str2);
637 if (blkid_do_probe(probe) != 0)
640 ret = blkid_probe_lookup_value(probe, "TYPE", &filesystem, &fs_len);
642 blkid_free_probe(probe);
645 DD_LIST_FOREACH(fs_head, elem, fs) {
646 if (!strncmp(fs->name, filesystem, fs_len)) {
651 blkid_free_probe(probe);
657 if (found && !strncmp(devnode, str2, strlen(str2) + 1))
663 /* Whole data in struct block_data should be freed. */
664 static struct block_data *make_block_data(const char *devnode,
666 const char *fs_usage,
668 const char *fs_version,
669 const char *fs_uuid_enc,
670 const char *readonly)
672 struct block_data *data;
674 /* devnode is unique value so it should exist. */
679 _I("Unknown fs type");
681 data = calloc(1, sizeof(struct block_data));
683 _E("calloc() failed");
687 data->devnode = strdup(devnode);
689 data->syspath = strdup(syspath);
691 data->fs_usage = strdup(fs_usage);
693 data->fs_type = strdup(fs_type);
695 data->fs_version = strdup(fs_version);
697 data->fs_uuid_enc = strdup(fs_uuid_enc);
699 data->readonly = atoi(readonly);
700 data->primary = check_primary_partition(devnode);
702 /* TODO should we know block dev type? */
703 if (!fnmatch(MMC_LINK_PATH, devnode, 0))
704 data->block_type = BLOCK_MMC_DEV;
705 else if (!fnmatch(MMC_PATH, devnode, 0))
706 data->block_type = BLOCK_MMC_DEV;
707 else if (!fnmatch(SCSI_PATH, devnode, 0))
708 data->block_type = BLOCK_SCSI_DEV;
709 else if (!fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
710 data->block_type = BLOCK_EXTENDEDSD_DEV;
712 data->block_type = -1;
714 data->mount_point = generate_mount_path(data);
715 BLOCK_FLAG_CLEAR_ALL(data);
717 /* for 2.4 backward compatibility */
718 // What if storage id 1 is existed? (multi sdcard case)
719 if (data->primary == true && data->block_type == BLOCK_MMC_DEV &&
720 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM))
721 data->id = EXT_PRIMARY_SD_FIXID;
723 data->id = block_get_new_id();
728 static void free_block_data(struct block_data *data)
734 free(data->fs_usage);
736 free(data->fs_version);
737 free(data->fs_uuid_enc);
738 free(data->mount_point);
742 static int update_block_data(struct block_data *data,
743 const char *fs_usage,
745 const char *fs_version,
746 const char *fs_uuid_enc,
747 const char *readonly,
748 bool mount_point_updated)
753 free(data->fs_usage);
754 data->fs_usage = NULL;
756 data->fs_usage = strdup(fs_usage);
759 data->fs_type = NULL;
761 data->fs_type = strdup(fs_type);
763 free(data->fs_version);
764 data->fs_version = NULL;
766 data->fs_version = strdup(fs_version);
768 free(data->fs_uuid_enc);
769 data->fs_uuid_enc = NULL;
771 data->fs_uuid_enc = strdup(fs_uuid_enc);
773 /* generate_mount_path function should be invoked
774 * after fs_uuid_enc is updated */
775 if (!mount_point_updated) {
776 free(data->mount_point);
777 data->mount_point = generate_mount_path(data);
780 data->readonly = false;
782 data->readonly = atoi(readonly);
784 BLOCK_FLAG_MOUNT_CLEAR(data);
789 static struct block_device *make_block_device(struct block_data *data)
791 struct block_device *bdev;
796 bdev = calloc(1, sizeof(struct block_device));
801 bdev->thread_id = -1;
802 bdev->removed = false;
803 bdev->on_private_op = REQ_NORMAL;
804 bdev->private_pid = 0;
805 bdev->mount_point_updated = false;
810 // Called by MainThread - Remove DevNode
811 static void free_block_device(struct block_device *bdev)
814 struct operation_queue *op;
820 thread_id = bdev->thread_id;
821 if (thread_id < 0 || thread_id >= THREAD_MAX)
824 pthread_mutex_lock(&(th_manager[thread_id].mutex));
826 th_manager[thread_id].num_dev--;
827 DD_LIST_REMOVE(th_manager[thread_id].block_dev_list, bdev);
828 free_block_data(bdev->data);
830 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
832 th_manager[thread_id].op_len--;
833 DD_LIST_REMOVE(bdev->op_queue, op);
836 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
841 // Called By MainThread - Remove Device
842 static struct block_device *find_block_device(const char *devnode)
844 struct block_device *bdev;
849 len = strlen(devnode) + 1;
850 for (i = 0; i < THREAD_MAX; i++) {
851 pthread_mutex_lock(&(th_manager[i].mutex));
852 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
853 if (bdev->data && !bdev->removed &&
854 !strncmp(bdev->data->devnode, devnode, len)) {
855 pthread_mutex_unlock(&(th_manager[i].mutex));
859 pthread_mutex_unlock(&(th_manager[i].mutex));
865 // Called By MainThread - Remove Device
866 static struct block_device *find_block_device_path(const char *mount_point)
868 struct block_device *bdev;
873 len = strlen(mount_point) + 1;
874 for (i = 0; i < THREAD_MAX; i++) {
875 pthread_mutex_lock(&(th_manager[i].mutex));
876 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
877 if (bdev->data && !bdev->removed &&
878 !strncmp(bdev->data->mount_point, mount_point, len)) {
879 pthread_mutex_unlock(&(th_manager[i].mutex));
883 pthread_mutex_unlock(&(th_manager[i].mutex));
889 // Called By MainThread - Mount,Unmount,Format,GetInfo
890 static struct block_device *find_block_device_by_id(int id)
892 struct block_device *bdev;
896 for (i = 0; i < THREAD_MAX; i++) {
897 pthread_mutex_lock(&(th_manager[i].mutex));
898 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
903 if (bdev->data->id == id) {
904 pthread_mutex_unlock(&(th_manager[i].mutex));
908 pthread_mutex_unlock(&(th_manager[i].mutex));
914 static char *get_operation_char(enum block_dev_operation op,
915 char *name, unsigned int len)
917 char *str = "unknown";
923 case BLOCK_DEV_MOUNT:
926 case BLOCK_DEV_UNMOUNT:
929 case BLOCK_DEV_FORMAT:
932 case BLOCK_DEV_INSERT:
935 case BLOCK_DEV_REMOVE:
939 _E("invalid operation (%d)", op);
943 snprintf(name, len, "%s", str);
947 static void create_external_apps_directory(void)
951 ret = call_dbus_method_async(PKGDIR_BUS_NAME, PKGDIR_PATH,
952 PKGDIR_INTERFACE, "CreateExternalDirsForAllPkgs",
953 NULL, NULL, NULL, DBUS_TIMEOUT_USE_DEFAULT, NULL);
955 _E("Fail to create external directory");
958 static int pipe_trigger(enum block_dev_operation op,
959 struct block_device *bdev, int result)
961 struct pipe_data pdata = { op, bdev, result };
965 _D("op : %s, bdev : %p, result : %d",
966 get_operation_char(pdata.op, name, sizeof(name)),
967 pdata.bdev, pdata.result);
969 // Multi thread should not write at the same time
970 pthread_mutex_lock(&pipe_mutex);
971 n = write(pfds[1], &pdata, sizeof(struct pipe_data));
972 pthread_mutex_unlock(&pipe_mutex);
974 return (n != sizeof(struct pipe_data)) ? -EPERM : 0;
977 static bool pipe_cb(int fd, void *data)
979 struct pipe_data pdata = {0,};
985 n = read(fd, &pdata, sizeof(pdata));
986 if (n != sizeof(pdata) || !pdata.bdev) {
987 _E("fail to read struct pipe data");
991 _I("op : %s, bdev : %p, result : %d",
992 get_operation_char(pdata.op, name, sizeof(name)),
993 pdata.bdev, pdata.result);
995 if (pdata.op == BLOCK_DEV_MOUNT && pdata.result < 0) {
996 if (pdata.bdev->data->state == BLOCK_UNMOUNT) {
997 ret = change_mount_point(pdata.bdev, "");
998 /* Modify /run/external-storage/id file */
1000 if (pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV)
1001 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, true);
1003 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, false);
1008 if (pdata.op == BLOCK_DEV_MOUNT &&
1009 pdata.bdev->data->state == BLOCK_MOUNT &&
1010 pdata.bdev->data->block_type == BLOCK_MMC_DEV &&
1011 pdata.bdev->data->primary)
1012 create_external_apps_directory();
1013 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1014 /* Remove file for block device /run/xxxxxx/id */
1015 remove_file(pdata.bdev->data->id, pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
1018 /* Broadcast to mmc and usb storage module */
1019 broadcast_block_info(pdata.op, pdata.bdev->data, pdata.result);
1021 /* Broadcast outside with Block iface */
1022 if (pdata.bdev->on_private_op == REQ_NORMAL)
1023 signal_device_changed(pdata.bdev, pdata.op);
1024 else if (pdata.bdev->on_private_op == REQ_PRIVATE) {
1025 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1026 pdata.bdev->on_private_op = REQ_NORMAL;
1027 _D("Private operation state: %d", pdata.bdev->on_private_op);
1030 if (pdata.op == BLOCK_DEV_MOUNT) {
1031 pdata.bdev->on_private_op = REQ_PRIVATE;
1032 _D("Private operation state: %d", pdata.bdev->on_private_op);
1036 if (pdata.op == BLOCK_DEV_REMOVE) {
1037 thread_id = pdata.bdev->thread_id;
1038 if (thread_id < 0 || thread_id >= THREAD_MAX)
1040 free_block_device(pdata.bdev);
1047 static int pipe_init(void)
1051 ret = pipe2(pfds, O_CLOEXEC);
1055 ret = add_fd_read_handler(pfds[0], pipe_cb,
1056 NULL, NULL, &phandler);
1058 _E("Failed to add pipe handler (%d)", ret);
1065 static void pipe_exit(void)
1068 remove_fd_read_handler(&phandler);
1078 static int mmc_check_and_unmount(const char *path)
1086 while (mount_check(path)) {
1090 if (retry > UNMOUNT_RETRY)
1097 static bool check_rw_mount(const char *szPath)
1099 struct statvfs mount_stat;
1101 if (!statvfs(szPath, &mount_stat)) {
1102 if ((mount_stat.f_flag & ST_RDONLY) == ST_RDONLY)
1108 static int retrieve_udev_device(struct block_data *data, bool mount_point_updated)
1111 struct udev_device *dev;
1118 for (wait = 0; wait < 10; wait++) {
1121 _E("fail to create udev library context");
1125 dev = udev_device_new_from_syspath(udev, data->syspath);
1127 _E("fail to create new udev device");
1132 if (!udev_device_get_property_value(dev, "ID_FS_TYPE"))
1137 udev_device_unref(dev);
1141 r = update_block_data(data,
1142 udev_device_get_property_value(dev, "ID_FS_USAGE"),
1143 udev_device_get_property_value(dev, "ID_FS_TYPE"),
1144 udev_device_get_property_value(dev, "ID_FS_VERSION"),
1145 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
1146 udev_device_get_sysattr_value(dev, "ro"),
1147 mount_point_updated);
1149 _E("fail to update block data for %s", data->devnode);
1151 udev_device_unref(dev);
1156 static int block_mount(struct block_data *data)
1158 struct block_fs_ops *fs;
1163 if (!data || !data->devnode || !data->mount_point)
1166 /* check existing mounted */
1167 if (mount_check(data->mount_point))
1170 /* create mount point */
1171 if (access(data->mount_point, R_OK) != 0) {
1172 if (mkdir(data->mount_point, 0755) < 0)
1176 /* check matched file system */
1177 if (!data->fs_usage ||
1178 strncmp(data->fs_usage, FILESYSTEM,
1179 sizeof(FILESYSTEM)) != 0) {
1184 if (!data->fs_type) {
1185 _E("There is no file system");
1186 BLOCK_FLAG_SET(data, FS_EMPTY);
1192 len = strlen(data->fs_type) + 1;
1193 DD_LIST_FOREACH(fs_head, elem, fs) {
1194 if (!strncmp(fs->name, data->fs_type, len))
1199 _E("Not supported file system (%s)", data->fs_type);
1200 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1205 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1206 r = fs->mount(false, data->devnode, data->mount_point);
1208 r = fs->mount(smack, data->devnode, data->mount_point);
1211 BLOCK_FLAG_SET(data, FS_BROKEN);
1216 r = check_rw_mount(data->mount_point);
1223 rmdir(data->mount_point);
1227 static int mount_start(struct block_device *bdev)
1229 struct block_data *data;
1237 _I("Mount Start : (%s -> %s)",
1238 data->devnode, data->mount_point);
1240 /* mount operation */
1241 r = block_mount(data);
1242 if (r != -EROFS && r < 0) {
1243 _E("fail to mount %s device : %d", data->devnode, r);
1248 data->readonly = true;
1249 BLOCK_FLAG_SET(data, MOUNT_READONLY);
1252 data->state = BLOCK_MOUNT;
1254 if (data->block_type == BLOCK_MMC_DEV) {
1255 /* app2ext_migrate_legacy_all has dbus method call to deviced */
1256 ret = app2ext_migrate_legacy_all();
1258 _E("app2ext failed");
1262 if (r < 0 && r != -EROFS)
1263 data->state = BLOCK_UNMOUNT;
1265 _I("%s result : %s, %d", __func__, data->devnode, r);
1267 if (pipe_trigger(BLOCK_DEV_MOUNT, bdev, r) < 0)
1268 _E("fail to trigger pipe");
1273 static int change_mount_point(struct block_device *bdev,
1274 const char *mount_point)
1276 struct block_data *data;
1282 free(data->mount_point);
1284 /* If the mount path already exists, the path cannot be used */
1286 access(mount_point, F_OK) != 0) {
1287 data->mount_point = strdup(mount_point);
1288 bdev->mount_point_updated = true;
1290 data->mount_point = generate_mount_path(data);
1291 bdev->mount_point_updated = false;
1297 static int mount_block_device(struct block_device *bdev)
1299 struct block_data *data;
1302 if (!bdev || !bdev->data)
1306 if (data->state == BLOCK_MOUNT) {
1307 _I("%s is already mounted", data->devnode);
1311 if (!block_conf[data->block_type].multimount &&
1313 _I("Not support multi mount by config info");
1317 r = mount_start(bdev);
1319 _E("Failed to mount (%s)", data->devnode);
1326 static int block_unmount(struct block_device *bdev,
1327 enum unmount_operation option)
1329 struct block_data *data;
1331 struct timespec time = {0,};
1333 if (!bdev || !bdev->data || !bdev->data->mount_point)
1338 if (bdev->on_private_op == REQ_NORMAL)
1339 signal_device_blocked(bdev);
1341 /* it must called before unmounting mmc */
1342 r = mmc_check_and_unmount(data->mount_point);
1345 if (option == UNMOUNT_NORMAL) {
1346 _I("Failed to unmount with normal option : %d", r);
1350 _I("Execute force unmount!");
1351 /* Force Unmount Scenario */
1356 * should unmount the below vconf key. */
1357 if ((data->block_type == BLOCK_MMC_DEV ||
1358 data->block_type == BLOCK_EXTENDEDSD_DEV) &&
1360 /* At first, notify to other app
1361 * who already access sdcard */
1362 _I("Notify to other app who already access sdcard");
1363 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
1364 VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
1368 /* Second, kill app with SIGTERM */
1369 _I("Kill app with SIGTERM");
1370 terminate_process(data->mount_point, false);
1373 /* Last time, kill app with SIGKILL */
1374 _I("Kill app with SIGKILL");
1375 terminate_process(data->mount_point, true);
1378 if (umount2(data->mount_point, MNT_DETACH) != 0) {
1379 _I("Failed to unmount with lazy option : %d",
1386 /* it takes some seconds til other app completely clean up */
1387 time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
1388 nanosleep(&time, NULL);
1390 print_open_files(data->mount_point);
1392 r = mmc_check_and_unmount(data->mount_point);
1394 _D("Success to unmount (%s)", data->mount_point);
1400 data->state = BLOCK_UNMOUNT;
1402 if (rmdir(data->mount_point) < 0)
1403 _E("fail to remove %s directory", data->mount_point);
1408 static int unmount_block_device(struct block_device *bdev,
1409 enum unmount_operation option)
1411 struct block_data *data;
1414 if (!bdev || !bdev->data)
1418 if (data->state == BLOCK_UNMOUNT) {
1419 _I("%s is already unmounted", data->devnode);
1420 r = mmc_check_and_unmount(data->mount_point);
1422 _E("The path was existed, but could not delete it(%s)",
1427 _I("Unmount Start : (%s -> %s)",
1428 data->devnode, data->mount_point);
1430 r = block_unmount(bdev, option);
1432 _E("fail to unmount %s device : %d", data->devnode, r);
1436 BLOCK_FLAG_MOUNT_CLEAR(data);
1439 _I("%s result : %s, %d", __func__, data->devnode, r);
1441 if (pipe_trigger(BLOCK_DEV_UNMOUNT, bdev, r) < 0)
1442 _E("fail to trigger pipe");
1447 static int block_format(struct block_data *data,
1448 const char *fs_type, bool mount_point_updated, bool udev_update)
1450 const struct block_fs_ops *fs;
1456 if (!data || !data->devnode || !data->mount_point)
1459 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1465 fstype = data->fs_type;
1471 len = strlen(fstype);
1472 DD_LIST_FOREACH(fs_head, elem, fs) {
1473 if (!strncmp(fs->name, fstype, len))
1478 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1479 _E("not supported file system(%s)", fstype);
1483 _I("format path : %s", data->devnode);
1484 fs->check(data->devnode);
1485 r = fs->format(data->devnode);
1487 _E("fail to format block data for %s", data->devnode);
1491 /* need to update the partition data.
1492 * It can be changed in doing format. */
1494 retrieve_udev_device(data, mount_point_updated);
1499 static int format_block_device(struct block_device *bdev,
1500 const char *fs_type,
1501 enum unmount_operation option, bool udev_update)
1503 struct block_data *data;
1511 _I("Format Start : (%s -> %s)",
1512 data->devnode, data->mount_point);
1514 if (data->state == BLOCK_MOUNT) {
1515 r = block_unmount(bdev, option);
1517 _E("fail to unmount %s device : %d", data->devnode, r);
1522 r = block_format(data, fs_type, bdev->mount_point_updated, udev_update);
1524 _E("fail to format %s device : %d", data->devnode, r);
1527 _I("%s result : %s, %d", __func__, data->devnode, r);
1529 r = pipe_trigger(BLOCK_DEV_FORMAT, bdev, r);
1531 _E("fail to trigger pipe");
1536 static struct format_data *get_format_data(
1537 const char *fs_type, enum unmount_operation option, bool udev_update)
1539 struct format_data *fdata;
1541 fdata = (struct format_data *)malloc(sizeof(struct format_data));
1543 _E("fail to allocate format data");
1548 fdata->fs_type = strdup(fs_type);
1550 fdata->fs_type = NULL;
1551 fdata->option = option;
1552 fdata->udev_update = udev_update;
1557 static void release_format_data(struct format_data *data)
1561 free(data->fs_type);
1566 // Called by BlockThread - Real Mount Op
1567 static int block_mount_device(struct block_device *bdev, void *data)
1576 thread_id = bdev->thread_id;
1577 if (thread_id < 0 || thread_id >= THREAD_MAX)
1579 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1580 l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
1581 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1583 _E("(%d) does not exist in the device list", bdev->data->devnode);
1587 /* mount automatically */
1588 ret = mount_block_device(bdev);
1590 _E("fail to mount block device for %s", bdev->data->devnode);
1595 // Called by BlockThread - Real Format Op
1596 static int block_format_device(struct block_device *bdev, void *data)
1601 struct format_data *fdata = (struct format_data *)data;
1603 if (!bdev || !fdata) {
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("(%d) does not exist in the device list", bdev->data->devnode);
1620 ret = format_block_device(bdev, fdata->fs_type, fdata->option, fdata->udev_update);
1622 _E("fail to mount block device for %s", bdev->data->devnode);
1625 release_format_data(fdata);
1630 // Called by BlockThread - Real Unmount Op
1631 static int block_unmount_device(struct block_device *bdev, void *data)
1634 long option = (long)data;
1639 ret = unmount_block_device(bdev, option);
1641 _E("Failed to unmount block device (%s)", bdev->data->devnode);
1648 /* Called by BlockThread - Remove Operation
1649 Direct Call at BlockThread
1650 Previously this function was called by MainThread. However, it will increase complexity.
1651 Need thread lock before to call remove_operation
1653 static void remove_operation(struct block_device *bdev)
1655 struct operation_queue *op;
1662 thread_id = bdev->thread_id;
1663 if (thread_id < 0 || thread_id >= THREAD_MAX)
1666 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
1668 _D("Remove operation (%s, %s)",
1669 get_operation_char(op->op, name, sizeof(name)),
1670 bdev->data->devnode);
1672 DD_LIST_REMOVE(bdev->op_queue, op);
1678 static void block_send_dbus_reply(dbus_method_reply_handle_h reply_handle, int result)
1685 rep = make_dbus_reply_message_simple(reply_handle, result);
1686 reply_dbus_method_result(reply_handle, rep);
1689 // Called by BlockThread
1690 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1692 struct operation_queue *temp;
1705 thread_id = bdev->thread_id;
1706 if (thread_id < 0 || thread_id >= THREAD_MAX)
1709 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1711 DD_LIST_FOREACH(*queue, l, temp) {
1712 if (temp->op == BLOCK_DEV_REMOVE) {
1717 th_manager[thread_id].op_len--;
1718 block_send_dbus_reply((*op)->reply_handle, 0);
1721 remove_operation(bdev);
1722 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1725 // Called by BlockThread
1726 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1728 struct operation_queue *temp;
1731 bool unmounted = false;
1742 thread_id = bdev->thread_id;
1743 if (thread_id < 0 || thread_id >= THREAD_MAX)
1746 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1747 DD_LIST_FOREACH(*queue, l, temp) {
1748 if (temp->op == BLOCK_DEV_UNMOUNT) {
1750 _D("Operation queue has unmount operation");
1754 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1759 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1761 DD_LIST_FOREACH(*queue, l, temp) {
1762 if (temp->op == BLOCK_DEV_UNMOUNT) {
1767 th_manager[thread_id].op_len--;
1768 block_send_dbus_reply((*op)->reply_handle, 0);
1771 remove_operation(bdev);
1772 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1777 // Called by BlockThread
1778 static void trigger_operation(struct block_device *bdev, dd_list *queue, struct operation_queue *op)
1782 char devnode[PATH_MAX];
1784 enum block_dev_operation operation;
1785 bool unmounted = false;
1792 thread_id = bdev->thread_id;
1793 if (thread_id < 0 || thread_id >= THREAD_MAX)
1796 snprintf(devnode, sizeof(devnode), "%s", bdev->data->devnode);
1806 _D("Thread id %d Trigger operation (%s, %s)", thread_id,
1807 get_operation_char(operation, name, sizeof(name)), devnode);
1810 if (operation == BLOCK_DEV_INSERT && bdev->removed) {
1811 check_removed(bdev, &queue, &op);
1813 _D("Trigger operation again (%s, %s)",
1814 get_operation_char(operation, name, sizeof(name)), devnode);
1816 if (operation == BLOCK_DEV_MOUNT) {
1817 unmounted = check_unmount(bdev, &queue, &op);
1820 _D("Trigger operation again (%s, %s)",
1821 get_operation_char(operation, name, sizeof(name)), devnode);
1825 switch (operation) {
1826 case BLOCK_DEV_INSERT:
1828 case BLOCK_DEV_MOUNT:
1829 ret = block_mount_device(bdev, op->data);
1830 _D("Mount (%s) result:(%d)", devnode, ret);
1832 case BLOCK_DEV_FORMAT:
1833 ret = block_format_device(bdev, op->data);
1834 _D("Format (%s) result:(%d)", devnode, ret);
1836 case BLOCK_DEV_UNMOUNT:
1837 ret = block_unmount_device(bdev, op->data);
1838 _D("Unmount (%s) result:(%d)", devnode, ret);
1840 case BLOCK_DEV_REMOVE:
1844 _E("Operation type is invalid (%d)", op->op);
1850 * during checking the queue length */
1851 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1854 th_manager[thread_id].op_len--;
1856 block_send_dbus_reply(op->reply_handle, ret);
1858 queue = bdev->op_queue;
1859 if (queue != NULL) {
1860 queue = DD_LIST_NEXT(queue);
1862 op = DD_LIST_NTH(queue, 0);
1868 remove_operation(bdev);
1870 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1873 if (operation == BLOCK_DEV_INSERT || operation == BLOCK_DEV_REMOVE) {
1874 if (pipe_trigger(operation, bdev, 0) < 0)
1875 _E("fail to trigger pipe");
1882 // Called by BlockThread
1883 static void *block_th_start(void *arg)
1885 struct block_device *temp;
1886 struct manage_thread *th = (struct manage_thread *)arg;
1887 struct operation_queue *op = NULL;
1889 dd_list *queue = NULL;
1894 thread_id = th->thread_id;
1895 if (thread_id < 0 || thread_id >= THREAD_MAX) {
1896 _E("Thread Number: %d", th->thread_id);
1901 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1902 if (th_manager[thread_id].op_len == 0) {
1903 _D("Operation queue of thread is empty");
1904 pthread_cond_wait(&(th_manager[thread_id].cond), &(th_manager[thread_id].mutex));
1905 _D("Wake up %d", thread_id);
1908 DD_LIST_FOREACH(th_manager[thread_id].block_dev_list, elem, temp) {
1909 queue = temp->op_queue;
1911 op = DD_LIST_NTH(queue, 0);
1913 _D("Operation queue for device %s is Empty", temp->data->devnode);
1917 queue = DD_LIST_NEXT(queue);
1925 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1927 if (op && !op->done)
1928 trigger_operation(temp, queue, op);
1933 // This function will be refactored later
1934 // Especially, we don't need to keep th_node_list.
1935 static int find_thread(char *devnode)
1942 int i, len, min, min_num;
1943 int dev_mmc = -1, part = -1, num;
1946 if (!fnmatch("*/"MMC_PARTITION_PATH, devnode, 0)) {
1947 sscanf(devnode, "/dev/mmcblk%dp%d", &dev_mmc, &part);
1954 snprintf(str, len, "/dev/mmcblk%d", dev_mmc);
1955 th_node = strdup(str);
1956 } else if (!fnmatch("*/"SCSI_PARTITION_PATH, devnode, 0)) {
1957 sscanf(devnode, "/dev/sd%c%d", &dev_scsi, &part);
1958 snprintf(str, SCSI_PARTITION_LENGTH, "/dev/sd%c", dev_scsi);
1959 th_node = strdup(str);
1961 th_node = strdup(devnode);
1963 len = strlen(th_node) + 1;
1966 for (i = 0; i < THREAD_MAX; i++) {
1967 DD_LIST_FOREACH(th_manager[i].th_node_list, elem, temp) {
1968 if (!strncmp(temp, th_node, len)) {
1973 if (th_manager[i].num_dev < min_num) {
1974 min_num = th_manager[i].num_dev;
1979 if (min >= 0 && min < THREAD_MAX) {
1980 DD_LIST_APPEND(th_manager[min].th_node_list, th_node);
1984 _E("Finding thread is failed");
1985 DD_LIST_APPEND(th_manager[0].th_node_list, th_node);
1989 /* Only Main thread is permmited */
1990 // Called by MainThread
1991 static int add_operation(struct block_device *bdev,
1992 enum block_dev_operation operation,
1993 dbus_method_reply_handle_h reply_handle, void *data)
1995 struct operation_queue *op;
2005 _I("Add operation (%s, %s)",
2006 get_operation_char(operation, name, sizeof(name)),
2007 bdev->data->devnode);
2009 thread_id = bdev->thread_id;
2010 if (thread_id < 0 || thread_id >= THREAD_MAX) {
2011 _E("Fail to find thread to add");
2015 op = (struct operation_queue *)malloc(sizeof(struct operation_queue));
2017 _E("malloc failed");
2023 op->reply_handle = reply_handle;
2026 * during adding queue and checking the queue length */
2027 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2029 /* Only modified between lock and unlock of mutex */
2032 start_th = th_manager[thread_id].start_th;
2033 DD_LIST_APPEND(bdev->op_queue, op);
2034 th_manager[thread_id].op_len++;
2036 if (th_manager[thread_id].op_len == 1 && !start_th)
2037 pthread_cond_signal(&(th_manager[thread_id].cond));
2039 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2042 /* Need to disble app2ext whenever unmounting mmc */
2043 if (op->op == BLOCK_DEV_UNMOUNT &&
2044 bdev->data->state == BLOCK_MOUNT &&
2045 bdev->data->block_type == BLOCK_MMC_DEV &&
2046 bdev->data->primary)
2047 if (app2ext_disable_all_external_pkgs() < 0)
2048 _E("app2ext_disable_all_external_pkgs() failed");
2052 _D("Start New thread for block device");
2053 th_manager[thread_id].start_th = false;
2054 ret = pthread_create(&(th_manager[thread_id].th), NULL, block_th_start, &th_manager[thread_id]);
2056 _E("fail to create thread for %s", bdev->data->devnode);
2060 pthread_detach(th_manager[thread_id].th);
2066 static bool disk_is_partitioned_by_kernel(struct udev_device *dev)
2069 struct dirent entry;
2071 const char *syspath;
2074 syspath = udev_device_get_syspath(dev);
2078 dp = opendir(syspath);
2080 _E("fail to open %s", syspath);
2084 /* TODO compare devname and d_name */
2085 while (readdir_r(dp, &entry, &dir) == 0 && dir != NULL) {
2086 if (!fnmatch(MMC_PARTITION_PATH, dir->d_name, 0) ||
2087 !fnmatch(SCSI_PARTITION_PATH, dir->d_name, 0)) {
2099 static bool check_partition(struct udev_device *dev)
2101 const char *devtype;
2102 const char *part_table_type;
2103 const char *fs_usage;
2106 /* only consider disk type, never partitions */
2107 devtype = udev_device_get_devtype(dev);
2111 if (strncmp(devtype, BLOCK_DEVTYPE_DISK,
2112 sizeof(BLOCK_DEVTYPE_DISK)) != 0)
2115 part_table_type = udev_device_get_property_value(dev,
2116 "ID_PART_TABLE_TYPE");
2117 if (part_table_type) {
2118 fs_usage = udev_device_get_property_value(dev,
2121 strncmp(fs_usage, FILESYSTEM, sizeof(FILESYSTEM)) == 0) {
2122 if (!disk_is_partitioned_by_kernel(dev))
2129 if (disk_is_partitioned_by_kernel(dev)) {
2138 // Called by MainThread
2139 static int add_block_device(struct udev_device *dev, const char *devnode, bool mapper)
2141 struct block_data *data;
2142 struct block_device *bdev;
2143 //char id_string[PATH_LEN];
2147 bool need_format = false;
2149 partition = check_partition(dev);
2151 /* if there is a partition, skip this request */
2152 _I("%s device has partitions, skip this time", devnode);
2156 if (mapper && !udev_device_get_property_value(dev, "ID_FS_TYPE")) {
2157 char syspath[128] = {0};
2158 sprintf(syspath, "/sys/block%s", rindex(udev_device_get_syspath(dev), '/'));
2159 data = make_block_data(devnode,
2164 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2165 udev_device_get_sysattr_value(dev, "ro"));
2168 data = make_block_data(devnode,
2169 udev_device_get_syspath(dev),
2170 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2171 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2172 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2173 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2174 udev_device_get_sysattr_value(dev, "ro"));
2178 _E("fail to make block data for %s", devnode);
2182 if (!block_conf[data->block_type].multimount && !data->primary &&
2183 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM)) {
2184 _D("Not support multi mount by config info");
2185 free_block_data(data);
2189 bdev = make_block_device(data);
2191 _E("fail to make block device for %s", devnode);
2192 free_block_data(data);
2196 thread_id = find_thread(bdev->data->devnode);
2197 if (thread_id < 0 || thread_id >= THREAD_MAX) {
2198 _E("Fail to find thread to add");
2199 free_block_device(bdev);
2202 bdev->thread_id = thread_id;
2204 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2205 th_manager[thread_id].num_dev++;
2206 DD_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
2207 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2210 struct format_data *fdata;
2212 fdata = get_format_data(NULL, 1, true);
2214 _E("Failed to get format data");
2218 ret = add_operation(bdev, BLOCK_DEV_FORMAT, NULL, (void *)fdata);
2220 _E("Failed to add operation (format %s)", bdev->data->devnode);
2221 release_format_data(fdata);
2225 if (!bdev->data->fs_type) {
2226 _E("Unformatted Storage");
2227 free_block_device(bdev);
2229 } else if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2230 bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2231 _D("Need to unlock encrypted sdcard");
2232 // ---- ODE UI launch ----
2233 ret = launch_system_app(POPUP_DEFAULT
2236 , "unlockextendedsd"
2240 , bdev->data->devnode
2244 _E("Failed to launch popup");
2247 } else if (mapper && !strncmp(bdev->data->fs_type, EXT4_NAME, strlen(EXT4_NAME))) {
2248 bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2249 ret = change_mount_point(bdev, EXTENDEDSD_MOUNT_POINT);
2252 free_block_device(bdev);
2257 ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
2259 _E("Failed to add operation (insert %s)", devnode);
2260 free_block_device(bdev);
2264 // Not a regular filesystem -> skip mounting
2265 if (!bdev->data->fs_usage || strcmp(bdev->data->fs_usage, FILESYSTEM)) {
2266 _I("Not a filesystem. Not mounting");
2270 /* Create file for block device /run/external-storage/id */
2271 create_file(bdev->data->id, bdev->data->mount_point, bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
2272 ret = add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL);
2274 _E("Failed to add operation (mount %s)", devnode);
2280 static int remove_block_device(struct udev_device *dev, const char *devnode)
2282 struct block_device *bdev;
2283 struct block_device *bdev_extended;
2286 bdev = find_block_device(devnode);
2288 _E("fail to find block data for %s", devnode);
2292 BLOCK_FLAG_SET(bdev->data, UNMOUNT_UNSAFE);
2294 bdev->removed = true;
2295 if (bdev->on_private_op != REQ_NORMAL) {
2296 bdev->on_private_op = REQ_NORMAL;
2297 _D("Private operation state: %d", bdev->on_private_op);
2300 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2301 bdev_extended = find_block_device_path(EXTENDEDSD_MOUNT_POINT);
2302 if (bdev_extended) {
2303 BLOCK_FLAG_SET(bdev_extended->data, UNMOUNT_UNSAFE);
2305 bdev_extended->removed = true;
2306 if (bdev_extended->on_private_op != REQ_NORMAL) {
2307 bdev_extended->on_private_op = REQ_NORMAL;
2308 _D("Private operation state: %d", bdev_extended->on_private_op);
2311 ret = add_operation(bdev_extended, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2313 _E("Failed to add operation (unmount %s)", devnode);
2317 ret = add_operation(bdev_extended, BLOCK_DEV_REMOVE, NULL, NULL);
2319 _E("Failed to add operation (remove %s)", devnode);
2323 ret = ode_luks_close(EXTENDEDSD_NAME);
2325 _E("Failed on ode_luks_close(%s)", EXTENDEDSD_NAME);
2329 _E("fail to find block data for extended sd card");
2332 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2334 _E("Failed to add operation (unmount %s)", devnode);
2338 ret = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2340 _E("Failed to add operation (remove %s)", devnode);
2347 static int get_internal_storage_number(void)
2349 struct libmnt_table *t = NULL;
2350 struct libmnt_fs *fs;
2353 int r = 0, dev_temp;
2355 if ((!is_emulator() && (dev_internal >= 0 || dev_internal_scsi != '\0')) ||
2356 (is_emulator() && dev_internal_emul != '\0'))
2359 t = mnt_new_table();
2363 r = mnt_table_parse_mtab(t, NULL);
2369 fs = mnt_table_find_target(t, ROOT_DIR, MNT_ITER_BACKWARD);
2375 temp = mnt_fs_get_srcpath(fs);
2379 name = strrchr(temp, '/');
2383 /* Boot from USB is not handled */
2384 if (!is_emulator()) {
2385 if (!fnmatch(MMC_PATH, temp, 0))
2386 sscanf(name, "mmcblk%d", &dev_internal);
2387 else if (!fnmatch(SCSI_PATH, temp, 0))
2388 sscanf(name, "sd%c", &dev_internal_scsi);
2390 if (!fnmatch(MMC_LINK_PATH, temp, 0))
2391 sscanf(name, "vd%c%d", &dev_internal_emul, &dev_temp);
2393 dev_internal_emul = '\0';
2401 static int check_external_storage(const char* devnode)
2403 char dev_scsi = '\0';
2406 int dev_num = -1, dev_temp;
2411 name = strrchr(devnode, '/');
2415 if (!is_emulator()) {
2416 if (!fnmatch(MMC_PATH, devnode, 0)) {
2417 sscanf(name, "mmcblk%d", &dev_num);
2418 if (dev_internal == dev_num) {
2419 _D("%s is internal storage", devnode);
2422 } else if (!fnmatch(SCSI_PATH, devnode, 0)) {
2423 sscanf(name, "sd%c", &dev_scsi);
2424 if (dev_internal_scsi == dev_scsi) {
2425 _D("%s is internal storage", devnode);
2430 if (!fnmatch(MMC_LINK_PATH, devnode, 0)) {
2431 sscanf(name, "vd%c%d", &emul, &dev_temp);
2432 if (dev_internal_emul == emul) {
2433 _D("%s is internal storage", devnode);
2442 static int check_already_handled(const char* devnode)
2444 struct block_device *bdev;
2445 struct block_data *data;
2449 for (i = 0; i < THREAD_MAX; i++) {
2450 pthread_mutex_lock(&(th_manager[i].mutex));
2451 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2457 if (!strncmp(data->devnode, devnode, strlen(devnode) + 1)) {
2458 pthread_mutex_unlock(&(th_manager[i].mutex));
2462 pthread_mutex_unlock(&(th_manager[i].mutex));
2468 static int block_init_from_udev_enumerate(void)
2471 struct udev_enumerate *enumerate;
2472 struct udev_list_entry *list_entry, *list_sub_entry;
2473 struct udev_device *dev;
2474 const char *syspath;
2475 const char *devnode;
2480 _E("fail to create udev library context");
2484 /* create a list of the devices in the 'usb' subsystem */
2485 enumerate = udev_enumerate_new(udev);
2487 _E("fail to create an enumeration context");
2491 if ((dev_internal < 0 && !is_emulator() && dev_internal_scsi == '\0') ||
2492 (is_emulator() && dev_internal_emul == '\0')) {
2493 r = get_internal_storage_number();
2498 udev_enumerate_add_match_subsystem(enumerate, BLOCK_SUBSYSTEM);
2499 udev_enumerate_add_match_property(enumerate,
2500 UDEV_DEVTYPE, BLOCK_DEVTYPE_DISK);
2501 udev_enumerate_add_match_property(enumerate,
2502 UDEV_DEVTYPE, BLOCK_DEVTYPE_PARTITION);
2503 udev_enumerate_scan_devices(enumerate);
2505 udev_list_entry_foreach(list_entry,
2506 udev_enumerate_get_list_entry(enumerate)) {
2507 syspath = udev_list_entry_get_name(list_entry);
2511 dev = udev_device_new_from_syspath(
2512 udev_enumerate_get_udev(enumerate),
2518 udev_list_entry_foreach(list_sub_entry,
2519 udev_device_get_devlinks_list_entry(dev)) {
2520 const char *devlink = udev_list_entry_get_name(list_sub_entry);
2521 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2528 devnode = udev_device_get_devnode(dev);
2532 if (fnmatch(MMC_PATH, devnode, 0) &&
2533 fnmatch(SCSI_PATH, devnode, 0) &&
2534 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
2538 r = check_external_storage(devnode);
2542 r = check_already_handled(devnode);
2544 _I("%s is already handled", devnode);
2548 _I("%s device add", devnode);
2549 add_block_device(dev, devnode, false);
2551 udev_device_unref(dev);
2554 udev_enumerate_unref(enumerate);
2559 // Called by MainThread
2560 static void show_block_device_list(void)
2562 struct block_device *bdev;
2563 struct block_data *data;
2567 for (i = 0; i < THREAD_MAX; i++) {
2568 pthread_mutex_lock(&(th_manager[i].mutex));
2569 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2575 _D("%s:", data->devnode);
2576 _D("\tSyspath: %s", data->syspath);
2577 _D("\tBlock type: %d", data->block_type);
2578 _D("\tFs type: %s", data->fs_type);
2579 _D("\tFs usage: %s", data->fs_usage);
2580 _D("\tFs version: %s", data->fs_version);
2581 _D("\tFs uuid enc: %s", data->fs_uuid_enc);
2582 _D("\tReadonly: %s",
2583 (data->readonly ? "true" : "false"));
2584 _D("\tMount point: %s", data->mount_point);
2585 _D("\tMount state: %s",
2586 (data->state == BLOCK_MOUNT ?
2587 "mount" : "unmount"));
2589 (data->primary ? "true" : "false"));
2590 _D("\tID: %d", data->id);
2592 pthread_mutex_unlock(&(th_manager[i].mutex));
2596 // Called by MainThread
2597 static void remove_whole_block_device(void)
2599 struct block_device *bdev;
2605 for (i = 0; i < THREAD_MAX; i++) {
2607 pthread_mutex_lock(&(th_manager[i].mutex));
2608 DD_LIST_FOREACH_SAFE(th_manager[i].block_dev_list, elem, next, bdev) {
2609 if (bdev->removed == false)
2612 pthread_mutex_unlock(&(th_manager[i].mutex));
2614 if (bdev && bdev->removed == false) {
2615 bdev->removed = true;
2616 r = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_NORMAL);
2618 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2620 r = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2622 _E("Failed to add operation (remove %s)", bdev->data->devnode);
2629 static void booting_done(const char *sender_name,
2630 const char *object_path, const char *interface_name,
2631 const char *signal_name, DBusMessage *msg,
2634 static int done = 0;
2639 /* if there is the attached device, try to mount */
2640 block_init_from_udev_enumerate();
2641 block_control = true;
2645 static void block_poweroff(const char *sender_name,
2646 const char *object_path, const char *interface_name,
2647 const char *signal_name, DBusMessage *msg,
2650 static int status = 0;
2655 /* unregister mmc uevent control routine */
2656 unregister_udev_uevent_control(&uh);
2657 remove_whole_block_device();
2660 static void uevent_block_handler(struct udev_device *dev)
2662 const char *devnode = NULL;
2664 struct udev_list_entry *list_entry;
2666 bool mapper = false;
2668 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
2669 const char *devlink = udev_list_entry_get_name(list_entry);
2670 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2674 if (!fnmatch(EXTENDEDSD_NODE_PATH, devlink, 0)) {
2682 devnode = udev_device_get_devnode(dev);
2686 if (fnmatch(MMC_PATH, devnode, 0) &&
2687 fnmatch(SCSI_PATH, devnode, 0))
2691 r = check_external_storage(devnode);
2695 action = udev_device_get_action(dev);
2699 _I("%s device %s", devnode, action);
2700 if (!strncmp(action, UDEV_ADD, sizeof(UDEV_ADD)) ||
2701 (mapper && !strcmp(action, UDEV_CHANGE))) {
2702 r = check_already_handled(devnode);
2704 _I("%s is already handled", devnode);
2708 add_block_device(dev, devnode, mapper);
2709 } else if (!strncmp(action, UDEV_REMOVE, sizeof(UDEV_REMOVE))) {
2710 remove_block_device(dev, devnode);
2711 } else if (!strncmp(action, UDEV_CHANGE, sizeof(UDEV_CHANGE))) {
2712 struct block_device *bdev;
2713 bdev = find_block_device(devnode);
2715 _E("fail to find block data for %s", devnode);
2718 if (!udev_device_get_property_value(dev, "ID_FS_TYPE"))
2721 r = update_block_data(bdev->data,
2722 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2723 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2724 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2725 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2726 udev_device_get_sysattr_value(dev, "ro"),
2729 _E("fail to update block data for %s", bdev->data->devnode);
2733 static DBusMessage *request_mount_block(dbus_method_reply_handle_h reply_handle,
2734 DBusMessage *msg, bool onprivate)
2736 struct block_device *bdev;
2741 if (!reply_handle || !msg)
2744 ret = dbus_message_get_args(msg, NULL,
2745 DBUS_TYPE_INT32, &id,
2746 DBUS_TYPE_STRING, &mount_point,
2751 bdev = find_block_device_by_id(id);
2753 _E("Failed to find (%d) in the device list", id);
2757 if (bdev->on_private_op != REQ_NORMAL) {
2762 if (bdev->data->state == BLOCK_MOUNT) {
2763 _I("%s is already mounted", bdev->data->devnode);
2769 bdev->on_private_op = REQ_PRIVATE;
2770 bdev->private_pid = get_dbus_method_sender_pid(reply_handle);
2771 _D("Private operation state: %d", bdev->on_private_op);
2773 if (bdev->on_private_op != REQ_NORMAL) {
2774 _E("Failed to process mount operation");
2780 /* if requester want to use a specific mount point */
2782 !strncmp(mount_point, EXTENDEDSD_STRING, strlen(EXTENDEDSD_STRING) + 1) != 0) {
2783 if (!block_conf[bdev->data->block_type].extendedinternal ||
2784 !bdev->data->primary) {
2785 _E("Not support extended internal storage");
2789 bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2790 ret = change_mount_point(bdev, EXTENDEDSD_MOUNT_POINT);
2796 create_file(bdev->data->id, bdev->data->mount_point, true);
2798 } else if (mount_point && strncmp(mount_point, "", 1) != 0) {
2799 ret = change_mount_point(bdev, mount_point);
2805 /* Create /run/external-storage/id file */
2806 create_file(bdev->data->id, bdev->data->mount_point, false);
2808 /* Create file for block device /run/external-storage/id */
2809 create_file(bdev->data->id, bdev->data->mount_point, false);
2812 ret = add_operation(bdev, BLOCK_DEV_MOUNT, reply_handle, NULL);
2814 _E("Failed to add operation (mount %s)", bdev->data->devnode);
2821 return make_dbus_reply_message_simple(reply_handle, ret);
2824 static DBusMessage *request_public_mount_block(dbus_method_reply_handle_h reply_handle,
2827 return request_mount_block(reply_handle, msg, false);
2830 static DBusMessage *request_private_mount_block(dbus_method_reply_handle_h reply_handle,
2833 return request_mount_block(reply_handle, msg, true);
2836 static DBusMessage *request_unmount_block(dbus_method_reply_handle_h reply_handle,
2837 DBusMessage *msg, bool onprivate)
2839 struct block_device *bdev;
2845 if (!reply_handle || !msg)
2848 ret = dbus_message_get_args(msg, NULL,
2849 DBUS_TYPE_INT32, &id,
2850 DBUS_TYPE_INT32, &option,
2855 bdev = find_block_device_by_id(id);
2857 _E("Failed to find (%d) in the device list", id);
2862 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV) {
2863 _I("Impossible to request unmount extended internal sdcard");
2869 pid = get_dbus_method_sender_pid(reply_handle);
2870 if (bdev->on_private_op == REQ_NORMAL || (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid)) {
2871 _E("Failed to process private unmount operation");
2876 if (bdev->on_private_op != REQ_NORMAL) {
2877 _E("Failed to process unmount operation");
2883 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, reply_handle, (void *)option);
2885 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2892 return make_dbus_reply_message_simple(reply_handle, ret);
2895 static DBusMessage *request_public_unmount_block(dbus_method_reply_handle_h reply_handle,
2898 return request_unmount_block(reply_handle, msg, false);
2901 static DBusMessage *request_private_unmount_block(dbus_method_reply_handle_h reply_handle,
2904 return request_unmount_block(reply_handle, msg, true);
2907 static DBusMessage *request_format_block(dbus_method_reply_handle_h reply_handle,
2910 struct block_device *bdev;
2911 struct format_data *fdata;
2918 if (!reply_handle || !msg)
2921 ret = dbus_message_get_args(msg, NULL,
2922 DBUS_TYPE_INT32, &id,
2923 DBUS_TYPE_INT32, &option,
2928 bdev = find_block_device_by_id(id);
2930 _E("Failed to find (%d) in the device list", id);
2934 pid = get_dbus_method_sender_pid(reply_handle);
2935 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
2936 _E("Failed to format on private state");
2941 fdata = get_format_data(NULL, option, true);
2943 _E("Failed to get format data");
2947 prev_state = bdev->data->state;
2948 if (prev_state == BLOCK_MOUNT) {
2949 if (bdev->on_private_op == REQ_PRIVATE) {
2950 bdev->on_private_op = REQ_PRIVATE_FORMAT;
2951 _D("Private operation state: %d", bdev->on_private_op);
2953 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2955 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2956 release_format_data(fdata);
2961 ret = add_operation(bdev, BLOCK_DEV_FORMAT, reply_handle, (void *)fdata);
2963 _E("Failed to add operation (format %s)", bdev->data->devnode);
2964 release_format_data(fdata);
2967 /* Maintain previous state of mount/unmount */
2968 if (prev_state == BLOCK_MOUNT) {
2969 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
2970 _E("Failed to add operation (mount %s)", bdev->data->devnode);
2978 return make_dbus_reply_message_simple(reply_handle, ret);
2981 static DBusMessage *request_format_block_type(dbus_method_reply_handle_h reply_handle,
2984 struct block_device *bdev;
2985 struct format_data *fdata;
2993 if (!reply_handle || !msg)
2996 ret = dbus_message_get_args(msg, NULL,
2997 DBUS_TYPE_INT32, &id,
2998 DBUS_TYPE_INT32, &option,
2999 DBUS_TYPE_STRING, &type,
3004 bdev = find_block_device_by_id(id);
3006 _E("Failed to find (%d) in the device list", id);
3010 pid = get_dbus_method_sender_pid(reply_handle);
3011 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
3012 _E("Failed to format on private state");
3017 fdata = get_format_data(type, option, true);
3019 _E("Failed to get format data");
3023 prev_state = bdev->data->state;
3024 if (prev_state == BLOCK_MOUNT) {
3025 if (bdev->on_private_op == REQ_PRIVATE) {
3026 bdev->on_private_op = REQ_PRIVATE_FORMAT;
3027 _D("Private operation state: %d", bdev->on_private_op);
3029 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
3031 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
3032 release_format_data(fdata);
3037 ret = add_operation(bdev, BLOCK_DEV_FORMAT, reply_handle, (void *)fdata);
3039 _E("Failed to add operation (format %s)", bdev->data->devnode);
3040 release_format_data(fdata);
3043 /* Maintain previous state of mount/unmount */
3044 if (prev_state == BLOCK_MOUNT) {
3045 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
3046 _E("Failed to add operation (mount %s)", bdev->data->devnode);
3054 return make_dbus_reply_message_simple(reply_handle, ret);
3057 static int add_device_to_iter(struct block_data *data, DBusMessageIter *piter)
3059 char *str_null = "";
3061 if (!data || !piter)
3064 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3065 &(data->block_type));
3066 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3067 data->devnode ? &(data->devnode) : &str_null);
3068 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3069 data->syspath ? &(data->syspath) : &str_null);
3070 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3071 data->fs_usage ? &(data->fs_usage) : &str_null);
3072 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3073 data->fs_type ? &(data->fs_type) : &str_null);
3074 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3075 data->fs_version ? &(data->fs_version) : &str_null);
3076 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3077 data->fs_uuid_enc ? &(data->fs_uuid_enc) : &str_null);
3078 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3080 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3081 data->mount_point ? &(data->mount_point) : &str_null);
3082 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3084 dbus_message_iter_append_basic(piter, DBUS_TYPE_BOOLEAN,
3086 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3088 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3095 static int add_device_to_struct_iter(struct block_data *data, DBusMessageIter *iter)
3097 DBusMessageIter piter;
3102 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3103 add_device_to_iter(data, &piter);
3104 dbus_message_iter_close_container(iter, &piter);
3109 static int add_device_to_iter_2(struct block_data *data, DBusMessageIter *iter)
3111 DBusMessageIter piter;
3112 char *str_null = "";
3117 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3118 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3119 &(data->block_type));
3120 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3121 data->devnode ? &(data->devnode) : &str_null);
3122 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3123 data->syspath ? &(data->syspath) : &str_null);
3124 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3125 data->fs_usage ? &(data->fs_usage) : &str_null);
3126 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3127 data->fs_type ? &(data->fs_type) : &str_null);
3128 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3129 data->fs_version ? &(data->fs_version) : &str_null);
3130 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3131 data->fs_uuid_enc ? &(data->fs_uuid_enc) : &str_null);
3132 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3134 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3135 data->mount_point ? &(data->mount_point) : &str_null);
3136 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3138 dbus_message_iter_append_basic(&piter, DBUS_TYPE_BOOLEAN,
3140 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3142 dbus_message_iter_close_container(iter, &piter);
3147 static DBusMessage *request_get_device_info(dbus_method_reply_handle_h reply_handle,
3150 DBusMessageIter iter;
3152 struct block_device *bdev;
3153 struct block_data *data;
3156 if (!reply_handle || !msg)
3159 reply = make_dbus_reply_message(reply_handle);
3163 ret = dbus_message_get_args(msg, NULL,
3164 DBUS_TYPE_INT32, &id,
3169 bdev = find_block_device_by_id(id);
3176 dbus_message_iter_init_append(reply, &iter);
3177 add_device_to_iter(data, &iter);
3183 static DBusMessage *request_show_device_list(dbus_method_reply_handle_h reply_handle,
3186 show_block_device_list();
3187 return make_dbus_reply_message(reply_handle);
3190 // Called by MainThread
3191 static DBusMessage *request_get_device_list(dbus_method_reply_handle_h reply_handle,
3194 DBusMessageIter iter;
3195 DBusMessageIter aiter;
3197 struct block_device *bdev;
3198 struct block_data *data;
3205 reply = make_dbus_reply_message(reply_handle);
3207 ret = dbus_message_get_args(msg, NULL,
3208 DBUS_TYPE_STRING, &type,
3211 _E("Failed to get args");
3216 _E("Delivered type is NULL");
3220 _D("Block (%s) device list is requested", type);
3222 if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
3223 block_type = BLOCK_SCSI_DEV;
3224 else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
3225 block_type = BLOCK_MMC_DEV;
3226 else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
3229 _E("Invalid type (%s) is requested", type);
3233 dbus_message_iter_init_append(reply, &iter);
3234 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(issssssisibii)", &aiter);
3236 for (i = 0; i < THREAD_MAX; i++) {
3237 pthread_mutex_lock(&(th_manager[i].mutex));
3238 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3245 switch (block_type) {
3246 case BLOCK_SCSI_DEV:
3248 if (data->block_type != block_type)
3254 add_device_to_struct_iter(data, &aiter);
3256 pthread_mutex_unlock(&(th_manager[i].mutex));
3258 dbus_message_iter_close_container(&iter, &aiter);
3264 // Called by MainThread
3265 static DBusMessage *request_get_device_list_2(dbus_method_reply_handle_h reply_handle,
3268 DBusMessageIter iter;
3269 DBusMessageIter aiter;
3271 struct block_device *bdev;
3272 struct block_data *data;
3279 reply = make_dbus_reply_message(reply_handle);
3281 ret = dbus_message_get_args(msg, NULL,
3282 DBUS_TYPE_STRING, &type,
3285 _E("Failed to get args");
3290 _E("Delivered type is NULL");
3294 _D("Block (%s) device list is requested", type);
3296 if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
3297 block_type = BLOCK_SCSI_DEV;
3298 else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
3299 block_type = BLOCK_MMC_DEV;
3300 else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
3303 _E("Invalid type (%s) is requested", type);
3307 dbus_message_iter_init_append(reply, &iter);
3308 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(issssssisibi)", &aiter);
3310 for (i = 0; i < THREAD_MAX; i++) {
3311 pthread_mutex_lock(&(th_manager[i].mutex));
3312 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3319 switch (block_type) {
3320 case BLOCK_SCSI_DEV:
3322 if (data->block_type != block_type)
3329 add_device_to_iter_2(data, &aiter);
3331 pthread_mutex_unlock(&(th_manager[i].mutex));
3333 dbus_message_iter_close_container(&iter, &aiter);
3339 static DBusMessage *request_get_mmc_primary(dbus_method_reply_handle_h reply_handle,
3342 DBusMessageIter iter;
3344 struct block_device *bdev;
3345 struct block_data *data, nodata = {0,};
3350 if (!reply_handle || !msg)
3353 reply = make_dbus_reply_message(reply_handle);
3358 for (i = 0; i < THREAD_MAX; i++) {
3359 pthread_mutex_lock(&(th_manager[i].mutex));
3360 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3366 if (data->block_type != BLOCK_MMC_DEV &&
3367 data->block_type != BLOCK_EXTENDEDSD_DEV)
3374 pthread_mutex_unlock(&(th_manager[i].mutex));
3379 dbus_message_iter_init_append(reply, &iter);
3381 add_device_to_iter(data, &iter);
3383 nodata.id = -ENODEV;
3384 add_device_to_iter(&nodata, &iter);
3391 static DBusMessage *request_check_speed(dbus_method_reply_handle_h reply_handle,
3394 struct timespec start_time, end_time;
3395 DBusMessageIter iter;
3397 struct block_device *bdev;
3398 struct block_data *data;
3404 if (!reply_handle || !msg)
3407 ret = dbus_message_get_args(msg, NULL,
3408 DBUS_TYPE_INT32, &id,
3415 bdev = find_block_device_by_id(id);
3426 _D("speed check: %s", data->devnode);
3427 fd = open(data->devnode, O_RDWR | O_SYNC);
3428 buf = calloc(1, SPEEDCHECK * 1024 * 1024);
3430 _E("calloc() failed");
3435 clock_gettime(CLOCK_REALTIME, &start_time);
3436 _I("start time: %lu.%lu", start_time.tv_sec, start_time.tv_nsec);
3437 ret = write(fd, buf, SPEEDCHECK * 1024 * 1024);
3438 clock_gettime(CLOCK_REALTIME, &end_time);
3439 _I("end time %lu.%lu", end_time.tv_sec, end_time.tv_nsec);
3443 if (SPEEDCHECK / (end_time.tv_sec - start_time.tv_sec) < 4) {
3450 _E("write() failed %d", errno);
3457 reply = dbus_message_new_method_return(msg);
3458 dbus_message_iter_init_append(reply, &iter);
3459 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
3464 Method name Method call format string Reply format string
3465 { "ShowDeviceList", NULL, NULL, request_show_device_list },
3466 { "GetDeviceList", "s", "a(issssssisibii)", request_get_device_list },
3467 { "GetDeviceList2", "s", "a(issssssisibi)", request_get_device_list_2 },
3468 { "Mount", "is", "i", request_public_mount_block },
3469 { "Unmoun, "ii", "i", request_public_unmount_block },
3470 { "Format", "ii", "i", request_format_block },
3471 { "GetDeviceInfo", "i", "(issssssisibii)", request_get_device_info },
3472 { "GetMmcPrimary", NULL, "(issssssisibii)" , request_get_mmc_primary },
3473 { "PrivateMount", "is", "i", request_private_mount_block },
3474 { "PrivateUnmount", "ii", "i", request_private_unmount_block },
3477 static const dbus_method_s manager_methods[] = {
3478 { "ShowDeviceList", NULL, request_show_device_list },
3479 { "GetDeviceList" , "s", request_get_device_list },
3480 { "GetDeviceList2", "s", request_get_device_list_2 },
3481 { "Mount", "is", request_public_mount_block },
3482 { "Unmount", "ii", request_public_unmount_block },
3483 { "Format", "ii", request_format_block },
3484 { "FormatwithType", "iis", request_format_block_type },
3485 { "GetDeviceInfo", "i", request_get_device_info },
3486 { "GetMmcPrimary" , NULL, request_get_mmc_primary },
3487 { "PrivateMount", "is", request_private_mount_block },
3488 { "PrivateUnmount", "ii", request_private_unmount_block },
3489 { "CheckSpeed", "i", request_check_speed },
3492 static dbus_interface_s block_interface = {
3493 .name = STORAGED_INTERFACE_BLOCK_MANAGER,
3494 .methods = manager_methods,
3495 .nr_methods = ARRAY_SIZE(manager_methods),
3498 static int load_config(struct parse_result *result, void *user_data)
3502 if (MATCH(result->section, "Block"))
3505 if (MATCH(result->section, "SCSI"))
3506 index = BLOCK_SCSI_DEV;
3507 else if (MATCH(result->section, "MMC"))
3508 index = BLOCK_MMC_DEV;
3509 else if (MATCH(result->section, "Mapper"))
3510 index = BLOCK_EXTENDEDSD_DEV;
3514 if (MATCH(result->name, "Multimount"))
3515 block_conf[index].multimount =
3516 (MATCH(result->value, "yes") ? true : false);
3517 if (MATCH(result->name, "ExtendedInternalStorage"))
3518 block_conf[index].extendedinternal =
3519 (MATCH(result->value, "yes") ? true : false);
3525 static int mount_root_path_tmpfs(void)
3530 root = tzplatform_getenv(TZ_SYS_MEDIA);
3534 if (access(root, F_OK) != 0)
3537 if (mount_check(root))
3540 ret = mount("tmpfs", root, "tmpfs", 0, "smackfsroot=System::Shared");
3543 _E("tmpfs mount failed (%d)", ret);
3550 #define mount_root_path_tmpfs() 0
3553 static void block_init(void *data)
3558 dbus_handle_h handle;
3563 ret = config_parse(BLOCK_CONF_FILE, load_config, NULL);
3565 _E("fail to load %s, Use default value", BLOCK_CONF_FILE);
3567 ret = mount_root_path_tmpfs();
3569 _E("Failed to mount tmpfs to root mount path (%d)", ret);
3571 ret = dbus_get_connection(&handle);
3573 _E("Failed to get dbus connection(%d)", ret);
3575 /* register block manager object and interface */
3576 ret = register_dbus_methods(handle,
3577 STORAGED_PATH_BLOCK_MANAGER, &block_interface,
3580 _E("Failed to register block interface and methods (%d)", ret);
3585 _E("fail to init pipe");
3587 /* register mmc uevent control routine */
3588 ret = register_udev_uevent_control(&uh);
3590 _E("fail to register block uevent : %d", ret);
3592 /* System Session is loaded completely */
3593 register_dbus_signal(SYSTEMD_DBUS_PATH,
3594 SYSTEMD_DBUS_IFACE_MANAGER,
3595 SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED,
3596 booting_done, NULL, NULL);
3598 register_dbus_signal(DEVICED_PATH_POWEROFF,
3599 DEVICED_INTERFACE_POWEROFF,
3600 SIGNAL_POWEROFF_STATE,
3601 block_poweroff, NULL, NULL);
3603 for (i = 0; i < THREAD_MAX; i++) {
3604 th_manager[i].num_dev = 0;
3605 th_manager[i].op_len = 0;
3606 th_manager[i].start_th = true;
3607 th_manager[i].thread_id = i;
3608 pthread_mutex_init(&(th_manager[i].mutex), NULL);
3609 pthread_cond_init(&(th_manager[i].cond), NULL);
3612 ret = stat(EXTERNAL_STORAGE_PATH, &buf);
3614 ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
3616 _E("Failed to make directory: %d", errno);
3617 } else if (!S_ISDIR(buf.st_mode)) {
3618 ret = remove(EXTERNAL_STORAGE_PATH);
3620 _E("Fail to remove %s. errno: %d", EXTERNAL_STORAGE_PATH, errno);
3621 ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
3623 _E("Failed to make directory: %d", errno);
3625 ret = chmod(EXTERNAL_STORAGE_PATH, 0644);
3627 _E("Fail to change permissions of a file");
3630 ret = stat(EXTENDED_INTERNAL_PATH, &buf);
3632 ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
3634 _E("Failed to make directory: %d", errno);
3635 } else if (!S_ISDIR(buf.st_mode)) {
3636 ret = remove(EXTENDED_INTERNAL_PATH);
3638 _E("Fail to remove %s. errno: %d", EXTENDED_INTERNAL_PATH, errno);
3639 ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
3641 _E("Failed to make directory: %d", errno);
3643 ret = chmod(EXTENDED_INTERNAL_PATH, 0644);
3645 _E("Fail to change permissions of a file");
3649 static void block_exit(void *data)
3651 dd_list *elem, *elem_next;
3657 /* unregister notifier for below each event */
3658 unregister_dbus_signal(SYSTEMD_DBUS_PATH,
3659 SYSTEMD_DBUS_IFACE_MANAGER,
3660 SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED,
3663 unregister_dbus_signal(DEVICED_PATH_POWEROFF,
3664 DEVICED_INTERFACE_POWEROFF,
3665 SIGNAL_POWEROFF_STATE, block_poweroff);
3670 /* unregister mmc uevent control routine */
3671 ret = unregister_udev_uevent_control(&uh);
3673 _E("fail to unregister block uevent : %d", ret);
3675 /* remove remaining blocks */
3676 remove_whole_block_device();
3678 for (i = 0; i < THREAD_MAX; i++) {
3679 if (!th_manager[i].start_th)
3680 pthread_cancel(th_manager[i].th);
3681 DD_LIST_FOREACH_SAFE(th_manager[i].th_node_list, elem, elem_next, temp) {
3682 DD_LIST_REMOVE(th_manager[i].th_node_list, temp);
3687 block_control = false;
3690 static int block_start(void *data)
3695 _E("Cannot be started. Booting is not ready");
3699 if (block_control) {
3700 _I("Already started");
3704 /* register mmc uevent control routine */
3705 ret = register_udev_uevent_control(&uh);
3707 _E("fail to register block uevent : %d", ret);
3709 block_init_from_udev_enumerate();
3711 block_control = true;
3717 static int block_stop(void *data)
3720 _E("Cannot be stopped. Booting is not ready");
3724 if (!block_control) {
3725 _I("Already stopped");
3729 /* unregister mmc uevent control routine */
3730 unregister_udev_uevent_control(&uh);
3732 /* remove the existing blocks */
3733 remove_whole_block_device();
3735 block_control = false;
3741 static storaged_module_interface block_module = {
3745 .start = block_start,
3749 __attribute__ ((visibility("default")))storaged_module_interface *
3750 storaged_get_module_interface(void)
3752 return &block_module;