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_NAME "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_PATH "/opt/extendedsd"
99 #define EXT4_NAME "ext4"
100 #define LUKS_NAME "crypto_LUKS"
101 #define EXTENDEDSD_NAME "extendedsd"
103 /* Minimum value of block id */
104 #define BLOCK_ID_MIN 10
105 /* For 2.4 Backward Compatibility */
106 #define EXT_PRIMARY_SD_FIXID 1
108 /* Maximum number of thread */
111 #define SPEEDCHECK 16
113 #define PKGDIR_BUS_NAME "org.tizen.pkgdir_tool"
114 #define PKGDIR_PATH "/org/tizen/pkgdir_tool"
115 #define PKGDIR_INTERFACE "org.tizen.pkgdir_tool"
117 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
119 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;
158 enum block_dev_operation op;
159 struct block_device *bdev;
163 static struct block_conf {
165 bool extendedinternal;
166 } block_conf[BLOCK_EXTENDEDSD_DEV + 1];
168 static struct manage_thread {
169 dd_list *th_node_list; /* List of devnode which thread dealt with. Only main thread access */
170 dd_list *block_dev_list; /* Use thread mutex */
172 pthread_mutex_t mutex;
174 int num_dev; /* Number of devices which thread holds. Only main thread access */
175 int op_len; /* Number of operation of thread. Use thread mutex */
176 int thread_id; /* Never changed */
178 } th_manager[THREAD_MAX];
180 static dd_list *fs_head;
181 static dd_list *block_ops_list;
184 static fd_handler_h phandler;
185 static bool block_control = false;
186 static bool block_boot = false;
187 static pthread_mutex_t pipe_mutex = PTHREAD_MUTEX_INITIALIZER;
189 /* Assume there is only one physical internal storage */
190 static int dev_internal = -1;
191 static char dev_internal_scsi = '\0';
192 static char dev_internal_emul = '\0';
194 static int block_start(void *data);
195 static int block_stop(void *data);
197 static int add_operation(struct block_device *bdev,
198 enum block_dev_operation operation,
199 dbus_method_reply_handle_h reply_handle, void *data);
200 static void remove_operation(struct block_device *bdev);
201 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
202 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op);
203 static int change_mount_point(struct block_device *bdev, const char *mount_point);
205 static void uevent_block_handler(struct udev_device *dev);
206 static struct uevent_handler uh = {
207 .subsystem = BLOCK_SUBSYSTEM,
208 .uevent_func = uevent_block_handler,
211 static void __CONSTRUCTOR__ smack_check(void)
216 fp = fopen("/proc/filesystems", "r");
220 while (fgets(buf, sizeof(buf), fp) != NULL) {
221 if (strstr(buf, "smackfs")) {
230 void add_fs(const struct block_fs_ops *fs)
232 DD_LIST_APPEND(fs_head, (void *)fs);
235 void remove_fs(const struct block_fs_ops *fs)
237 DD_LIST_REMOVE(fs_head, (void *)fs);
240 const struct block_fs_ops *find_fs(enum block_fs_type type)
242 struct block_fs_ops *fs;
245 DD_LIST_FOREACH(fs_head, elem, fs) {
246 if (fs->type == type)
252 void add_block_dev(const struct block_dev_ops *ops)
254 DD_LIST_APPEND(block_ops_list, (void *)ops);
257 void remove_block_dev(const struct block_dev_ops *ops)
259 DD_LIST_REMOVE(block_ops_list, (void *)ops);
262 static void broadcast_block_info(enum block_dev_operation op,
263 struct block_data *data, int result)
265 struct block_dev_ops *ops;
268 if (data->primary != true)
271 DD_LIST_FOREACH(block_ops_list, elem, ops) {
272 int data_block_type = (data->block_type == BLOCK_EXTENDEDSD_DEV)
273 ? BLOCK_MMC_DEV : data->block_type;
275 if (ops->block_type != data_block_type)
277 // TODO What happend on extended internal storage case?
278 if (op == BLOCK_DEV_MOUNT) {
279 ops->mounted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
280 } else if (op == BLOCK_DEV_UNMOUNT) {
281 ops->unmounted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
282 } else if (op == BLOCK_DEV_FORMAT) {
283 ops->formatted(data, result, data->block_type == BLOCK_EXTENDEDSD_DEV);
284 } else if (op == BLOCK_DEV_INSERT)
286 else if (op == BLOCK_DEV_REMOVE)
291 // Called by MainThread - Insert
292 static int block_get_new_id(void)
294 static int id = BLOCK_ID_MIN;
295 struct block_device *bdev;
300 for (i = 0 ; i < INT_MAX ; i++) {
302 for (j = 0; j < THREAD_MAX; j++) {
303 pthread_mutex_lock(&(th_manager[j].mutex));
304 DD_LIST_FOREACH(th_manager[j].block_dev_list, elem, bdev) {
305 if (bdev->data->id == id) {
310 pthread_mutex_unlock(&(th_manager[j].mutex));
325 static void remove_file(int id, bool extendedsd)
327 char file_name[PATH_LEN];
334 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
336 snprintf(file_name, sizeof(file_name), EXTERNAL_STORAGE_PATH"/%d", id);
338 ret = remove(file_name);
340 _E("Fail to remove %s. errno: %d", file_name, errno);
343 static void create_file(int id, char *mount_point, bool extendedsd)
346 char file_name[PATH_LEN];
352 snprintf(file_name, PATH_LEN, EXTENDED_INTERNAL_PATH"/%d", id);
354 snprintf(file_name, PATH_LEN, EXTERNAL_STORAGE_PATH"/%d", id);
356 fp = fopen(file_name, "w+");
358 fprintf(fp, "%s", mount_point);
361 _E("Fail to open %s", file_name);
364 static void signal_device_blocked(struct block_device *bdev)
366 struct block_data *data;
368 char str_block_type[32];
369 char str_readonly[32];
371 char str_primary[32];
377 if (!bdev || !bdev->data)
383 /* Broadcast outside with BlockManager iface */
384 snprintf(str_block_type, sizeof(str_block_type),
385 "%d", data->block_type);
386 arr[0] = str_block_type;
387 arr[1] = (data->devnode ? data->devnode : str_null);
388 arr[2] = (data->syspath ? data->syspath : str_null);
389 arr[3] = (data->fs_usage ? data->fs_usage : str_null);
390 arr[4] = (data->fs_type ? data->fs_type : str_null);
391 arr[5] = (data->fs_version ? data->fs_version : str_null);
392 arr[6] = (data->fs_uuid_enc ? data->fs_uuid_enc : str_null);
393 snprintf(str_readonly, sizeof(str_readonly),
394 "%d", data->readonly);
395 arr[7] = str_readonly;
396 arr[8] = (data->mount_point ? data->mount_point : str_null);
397 snprintf(str_state, sizeof(str_state),
400 snprintf(str_primary, sizeof(str_primary),
401 "%d", data->primary);
402 arr[10] = str_primary;
403 snprintf(str_flags, sizeof(str_flags), "%d", flags);
405 snprintf(str_id, sizeof(str_id), "%d", data->id);
408 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
409 STORAGED_INTERFACE_BLOCK_MANAGER,
410 BLOCK_DEVICE_BLOCKED,
411 "issssssisibii", arr);
414 static void signal_device_changed(struct block_device *bdev,
415 enum block_dev_operation op)
417 struct block_data *data;
419 char str_block_type[32];
420 char str_readonly[32];
422 char str_primary[32];
428 if (!bdev || !bdev->data)
434 case BLOCK_DEV_MOUNT:
435 BLOCK_GET_MOUNT_FLAGS(data, flags);
437 case BLOCK_DEV_UNMOUNT:
438 BLOCK_GET_UNMOUNT_FLAGS(data, flags);
440 case BLOCK_DEV_FORMAT:
441 BLOCK_GET_FORMAT_FLAGS(data, flags);
448 /* Broadcast outside with BlockManager iface */
449 snprintf(str_block_type, sizeof(str_block_type),
450 "%d", data->block_type);
451 arr[0] = str_block_type;
452 arr[1] = (data->devnode ? data->devnode : str_null);
453 arr[2] = (data->syspath ? data->syspath : str_null);
454 arr[3] = (data->fs_usage ? data->fs_usage : str_null);
455 arr[4] = (data->fs_type ? data->fs_type : str_null);
456 arr[5] = (data->fs_version ? data->fs_version : str_null);
457 arr[6] = (data->fs_uuid_enc ? data->fs_uuid_enc : str_null);
458 snprintf(str_readonly, sizeof(str_readonly),
459 "%d", data->readonly);
460 arr[7] = str_readonly;
461 arr[8] = (data->mount_point ? data->mount_point : str_null);
462 snprintf(str_state, sizeof(str_state),
465 snprintf(str_primary, sizeof(str_primary),
466 "%d", data->primary);
467 arr[10] = str_primary;
468 snprintf(str_flags, sizeof(str_flags), "%d", flags);
470 snprintf(str_id, sizeof(str_id), "%d", data->id);
473 if (op == BLOCK_DEV_INSERT)
474 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
475 STORAGED_INTERFACE_BLOCK_MANAGER,
477 "issssssisibii", arr);
478 else if (op == BLOCK_DEV_REMOVE)
479 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
480 STORAGED_INTERFACE_BLOCK_MANAGER,
481 BLOCK_DEVICE_REMOVED,
482 "issssssisibii", arr);
484 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
485 STORAGED_INTERFACE_BLOCK_MANAGER,
486 BLOCK_DEVICE_CHANGED,
487 "issssssisibii", arr);
488 broadcast_dbus_signal(STORAGED_PATH_BLOCK_MANAGER,
489 STORAGED_INTERFACE_BLOCK_MANAGER,
490 BLOCK_DEVICE_CHANGED_2,
491 "issssssisibi", arr);
495 static int get_mmc_mount_node(char *devnode, char *node, size_t len)
497 char *name = devnode;
498 int dev = -1, part = -1;
499 char emul[32] = { 0, };
506 sscanf(name, "mmcblk%dp%d", &dev, &part);
509 snprintf(node, len, "%s%c", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1);
511 snprintf(node, len, "%s%c%d", BLOCK_MMC_NODE_PREFIX, dev + 'A' - 1, part);
516 sscanf(name, "vd%31s", emul);
519 for (i = 0 ; i < strlen(emul) ; i++)
520 emul[i] = toupper(emul[i]);
521 snprintf(node, len, "%s%s", BLOCK_MMC_NODE_PREFIX, emul);
525 static int get_scsi_mount_node(char *devnode, char *node, size_t len)
533 snprintf(dev, sizeof(dev), "%s", devnode);
535 if (!strstr(dev, "sd"))
539 name += strlen("sd");
541 for (i = 0 ; i < strlen(name) ; i++)
542 name[i] = toupper(name[i]);
543 snprintf(node, len, "%s%s", BLOCK_SCSI_NODE_PREFIX, name);
548 static char *generate_mount_path(struct block_data *data)
551 char *name, node[64];
554 if (!data || !data->devnode || !data->fs_usage || (strcmp(data->fs_usage, FILESYSTEM_NAME) && strncmp(data->fs_usage, "crypto", strlen("crypto"))))
557 name = strrchr(data->devnode, '/');
562 switch (data->block_type) {
564 ret = get_mmc_mount_node(name, node, sizeof(node));
567 ret = get_scsi_mount_node(name, node, sizeof(node));
569 case BLOCK_EXTENDEDSD_DEV:
570 return strdup(EXTENDEDSD_MOUNT_PATH);
572 _E("Invalid block type (%d)", data->block_type);
578 str = tzplatform_mkpath(TZ_SYS_MEDIA, node);
584 _E("Invalid devnode (%s)", data->devnode ? data->devnode : "NULL");
588 static bool check_primary_partition(const char *devnode)
590 struct block_fs_ops *fs;
593 const char *filesystem = NULL;
603 if (fnmatch(MMC_LINK_PATH, devnode, 0) &&
604 fnmatch(MMC_PATH, devnode, 0) &&
605 fnmatch(SCSI_PATH, devnode, 0) &&
606 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
609 temp = strrchr(devnode, '/');
612 if (fnmatch("/"SCSI_PARTITION_PATH, temp, 0) &&
613 fnmatch("/"MMC_PARTITION_PATH, temp, 0))
616 /* Emulator support only one partition */
620 snprintf(str, sizeof(str), "%s", devnode);
625 for (i = 1; i <= 9; ++i) {
626 snprintf(str2, sizeof(str2), "%s%d", str, i);
627 if (access(str2, R_OK) != 0)
630 probe = blkid_new_probe_from_filename(str2);
633 if (blkid_do_probe(probe) != 0)
636 ret = blkid_probe_lookup_value(probe, "TYPE", &filesystem, &fs_len);
638 blkid_free_probe(probe);
641 DD_LIST_FOREACH(fs_head, elem, fs) {
642 if (!strncmp(fs->name, filesystem, fs_len)) {
647 blkid_free_probe(probe);
653 if (found && !strncmp(devnode, str2, strlen(str2) + 1))
659 /* Whole data in struct block_data should be freed. */
660 static struct block_data *make_block_data(const char *devnode,
662 const char *fs_usage,
664 const char *fs_version,
665 const char *fs_uuid_enc,
666 const char *readonly)
668 struct block_data *data;
670 /* devnode is unique value so it should exist. */
675 _I("Unknown fs type");
677 data = calloc(1, sizeof(struct block_data));
679 _E("calloc() failed");
683 data->devnode = strdup(devnode);
685 data->syspath = strdup(syspath);
687 data->fs_usage = strdup(fs_usage);
689 data->fs_type = strdup(fs_type);
691 data->fs_version = strdup(fs_version);
693 data->fs_uuid_enc = strdup(fs_uuid_enc);
695 data->readonly = atoi(readonly);
696 data->primary = check_primary_partition(devnode);
698 /* TODO should we know block dev type? */
699 if (!fnmatch(MMC_LINK_PATH, devnode, 0))
700 data->block_type = BLOCK_MMC_DEV;
701 else if (!fnmatch(MMC_PATH, devnode, 0))
702 data->block_type = BLOCK_MMC_DEV;
703 else if (!fnmatch(SCSI_PATH, devnode, 0))
704 data->block_type = BLOCK_SCSI_DEV;
705 else if (!fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0))
706 data->block_type = BLOCK_EXTENDEDSD_DEV;
708 data->block_type = -1;
710 data->mount_point = generate_mount_path(data);
711 BLOCK_FLAG_CLEAR_ALL(data);
713 /* for 2.4 backward compatibility */
714 // What if storage id 1 is existed? (multi sdcard case)
715 if (data->primary == true && data->block_type == BLOCK_MMC_DEV &&
716 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM_NAME))
717 data->id = EXT_PRIMARY_SD_FIXID;
719 data->id = block_get_new_id();
724 static void free_block_data(struct block_data *data)
730 free(data->fs_usage);
732 free(data->fs_version);
733 free(data->fs_uuid_enc);
734 free(data->mount_point);
738 static int update_block_data(struct block_data *data,
739 const char *fs_usage,
741 const char *fs_version,
742 const char *fs_uuid_enc,
743 const char *readonly,
744 bool mount_point_updated)
749 free(data->fs_usage);
750 data->fs_usage = NULL;
752 data->fs_usage = strdup(fs_usage);
755 data->fs_type = NULL;
757 data->fs_type = strdup(fs_type);
759 free(data->fs_version);
760 data->fs_version = NULL;
762 data->fs_version = strdup(fs_version);
764 free(data->fs_uuid_enc);
765 data->fs_uuid_enc = NULL;
767 data->fs_uuid_enc = strdup(fs_uuid_enc);
769 /* generate_mount_path function should be invoked
770 * after fs_uuid_enc is updated */
771 if (!mount_point_updated) {
772 free(data->mount_point);
773 data->mount_point = generate_mount_path(data);
776 data->readonly = false;
778 data->readonly = atoi(readonly);
780 BLOCK_FLAG_MOUNT_CLEAR(data);
785 static struct block_device *make_block_device(struct block_data *data)
787 struct block_device *bdev;
792 bdev = calloc(1, sizeof(struct block_device));
797 bdev->thread_id = -1;
798 bdev->removed = false;
799 bdev->on_private_op = REQ_NORMAL;
800 bdev->private_pid = 0;
801 bdev->mount_point_updated = false;
806 // Called by MainThread - Remove DevNode
807 static void free_block_device(struct block_device *bdev)
810 struct operation_queue *op;
816 thread_id = bdev->thread_id;
817 if (thread_id < 0 || thread_id >= THREAD_MAX)
820 pthread_mutex_lock(&(th_manager[thread_id].mutex));
822 th_manager[thread_id].num_dev--;
823 DD_LIST_REMOVE(th_manager[thread_id].block_dev_list, bdev);
824 free_block_data(bdev->data);
826 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
828 th_manager[thread_id].op_len--;
829 DD_LIST_REMOVE(bdev->op_queue, op);
832 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
837 // Called By MainThread - Remove Device
838 static struct block_device *find_block_device(const char *devnode)
840 struct block_device *bdev;
845 len = strlen(devnode) + 1;
846 for (i = 0; i < THREAD_MAX; i++) {
847 pthread_mutex_lock(&(th_manager[i].mutex));
848 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
849 if (bdev->data && !bdev->removed &&
850 !strncmp(bdev->data->devnode, devnode, len)) {
851 pthread_mutex_unlock(&(th_manager[i].mutex));
855 pthread_mutex_unlock(&(th_manager[i].mutex));
861 // Called By MainThread - Remove Device
862 static struct block_device *find_block_device_path(const char *mount_point)
864 struct block_device *bdev;
869 len = strlen(mount_point) + 1;
870 for (i = 0; i < THREAD_MAX; i++) {
871 pthread_mutex_lock(&(th_manager[i].mutex));
872 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
873 if (bdev->data && !bdev->removed &&
874 (bdev->data->mount_point != NULL && !strncmp(bdev->data->mount_point, mount_point, len))) {
875 pthread_mutex_unlock(&(th_manager[i].mutex));
879 pthread_mutex_unlock(&(th_manager[i].mutex));
885 // Called By MainThread - Mount,Unmount,Format,GetInfo
886 static struct block_device *find_block_device_by_id(int id)
888 struct block_device *bdev;
892 for (i = 0; i < THREAD_MAX; i++) {
893 pthread_mutex_lock(&(th_manager[i].mutex));
894 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
899 if (bdev->data->id == id) {
900 pthread_mutex_unlock(&(th_manager[i].mutex));
904 pthread_mutex_unlock(&(th_manager[i].mutex));
910 static const char *get_operation_char(enum block_dev_operation op)
913 case BLOCK_DEV_MOUNT:
916 case BLOCK_DEV_UNMOUNT:
919 case BLOCK_DEV_FORMAT:
922 case BLOCK_DEV_INSERT:
925 case BLOCK_DEV_REMOVE:
928 case BLOCK_LUKS_CLOSE:
932 _E("invalid operation (%d)", op);
936 static void create_external_apps_directory(void)
940 ret = call_dbus_method_async(PKGDIR_BUS_NAME, PKGDIR_PATH,
941 PKGDIR_INTERFACE, "CreateExternalDirsForAllPkgs",
942 NULL, NULL, NULL, DBUS_TIMEOUT_USE_DEFAULT, NULL);
944 _E("Fail to create external directory");
947 static int pipe_trigger(enum block_dev_operation op,
948 struct block_device *bdev, int result)
950 struct pipe_data pdata = { op, bdev, result };
953 _D("op : %s, bdev : %p, result : %d",
954 get_operation_char(pdata.op),
955 pdata.bdev, pdata.result);
957 // Multi thread should not write at the same time
958 pthread_mutex_lock(&pipe_mutex);
959 n = write(pfds[1], &pdata, sizeof(struct pipe_data));
960 pthread_mutex_unlock(&pipe_mutex);
962 return (n != sizeof(struct pipe_data)) ? -EPERM : 0;
965 static bool pipe_cb(int fd, void *data)
967 struct pipe_data pdata = {0,};
972 n = read(fd, &pdata, sizeof(pdata));
973 if (n != sizeof(pdata) || !pdata.bdev) {
974 _E("fail to read struct pipe data");
978 _I("op : %s, bdev : %p, result : %d",
979 get_operation_char(pdata.op),
980 pdata.bdev, pdata.result);
982 if (pdata.op == BLOCK_DEV_MOUNT && pdata.result < 0) {
983 if (pdata.bdev->data->state == BLOCK_UNMOUNT) {
984 ret = change_mount_point(pdata.bdev, "");
985 /* Modify /run/external-storage/id file */
987 if (pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV)
988 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, true);
990 create_file(pdata.bdev->data->id, pdata.bdev->data->mount_point, false);
995 if (pdata.op == BLOCK_DEV_MOUNT &&
996 pdata.bdev->data->state == BLOCK_MOUNT &&
997 pdata.bdev->data->block_type == BLOCK_MMC_DEV &&
998 pdata.bdev->data->primary)
999 create_external_apps_directory();
1000 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1001 /* Remove file for block device /run/xxxxxx/id */
1002 remove_file(pdata.bdev->data->id, pdata.bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
1005 /* Broadcast to mmc and usb storage module */
1006 broadcast_block_info(pdata.op, pdata.bdev->data, pdata.result);
1008 /* Broadcast outside with Block iface */
1009 if (pdata.bdev->on_private_op == REQ_NORMAL)
1010 signal_device_changed(pdata.bdev, pdata.op);
1011 else if (pdata.bdev->on_private_op == REQ_PRIVATE) {
1012 if (pdata.op == BLOCK_DEV_UNMOUNT) {
1013 pdata.bdev->on_private_op = REQ_NORMAL;
1014 _D("Private operation state: %d", pdata.bdev->on_private_op);
1017 if (pdata.op == BLOCK_DEV_MOUNT) {
1018 pdata.bdev->on_private_op = REQ_PRIVATE;
1019 _D("Private operation state: %d", pdata.bdev->on_private_op);
1023 if (pdata.op == BLOCK_DEV_REMOVE) {
1024 thread_id = pdata.bdev->thread_id;
1025 if (thread_id < 0 || thread_id >= THREAD_MAX)
1027 free_block_device(pdata.bdev);
1034 static int pipe_init(void)
1038 ret = pipe2(pfds, O_CLOEXEC);
1042 ret = add_fd_read_handler(pfds[0], pipe_cb,
1043 NULL, NULL, &phandler);
1045 _E("Failed to add pipe handler (%d)", ret);
1052 static void pipe_exit(void)
1055 remove_fd_read_handler(&phandler);
1065 static int mmc_check_and_unmount(const char *path)
1073 while (mount_check(path)) {
1077 if (retry > UNMOUNT_RETRY)
1084 static bool check_rw_mount(const char *szPath)
1086 struct statvfs mount_stat;
1088 if (!statvfs(szPath, &mount_stat)) {
1089 if ((mount_stat.f_flag & ST_RDONLY) == ST_RDONLY)
1095 static int retrieve_udev_device(struct block_data *data, bool mount_point_updated)
1098 struct udev_device *dev;
1105 for (wait = 0; wait < 10; wait++) {
1108 _E("fail to create udev library context");
1112 dev = udev_device_new_from_syspath(udev, data->syspath);
1114 _E("fail to create new udev device");
1119 if (!udev_device_get_property_value(dev, "ID_FS_TYPE"))
1124 udev_device_unref(dev);
1128 r = update_block_data(data,
1129 udev_device_get_property_value(dev, "ID_FS_USAGE"),
1130 udev_device_get_property_value(dev, "ID_FS_TYPE"),
1131 udev_device_get_property_value(dev, "ID_FS_VERSION"),
1132 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
1133 udev_device_get_sysattr_value(dev, "ro"),
1134 mount_point_updated);
1136 _E("fail to update block data for %s", data->devnode);
1138 udev_device_unref(dev);
1143 static int block_mount(struct block_data *data)
1145 struct block_fs_ops *fs;
1150 if (!data || !data->devnode || !data->mount_point)
1153 /* check existing mounted */
1154 if (mount_check(data->mount_point))
1157 /* create mount point */
1158 if (access(data->mount_point, R_OK) != 0) {
1159 if (mkdir(data->mount_point, 0755) < 0)
1163 /* check matched file system */
1164 if (!data->fs_usage ||
1165 strncmp(data->fs_usage, FILESYSTEM_NAME,
1166 sizeof(FILESYSTEM_NAME)) != 0) {
1171 if (!data->fs_type) {
1172 _E("There is no file system");
1173 BLOCK_FLAG_SET(data, FS_EMPTY);
1179 len = strlen(data->fs_type) + 1;
1180 DD_LIST_FOREACH(fs_head, elem, fs) {
1181 if (!strncmp(fs->name, data->fs_type, len))
1186 _E("Not supported file system (%s)", data->fs_type);
1187 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1192 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1193 r = fs->mount(false, data->devnode, data->mount_point);
1195 r = fs->mount(smack, data->devnode, data->mount_point);
1198 BLOCK_FLAG_SET(data, FS_BROKEN);
1203 r = check_rw_mount(data->mount_point);
1210 rmdir(data->mount_point);
1214 static int mount_start(struct block_device *bdev)
1216 struct block_data *data;
1224 _I("Mount Start : (%s -> %s)",
1225 data->devnode, data->mount_point);
1227 /* mount operation */
1228 r = block_mount(data);
1229 if (r != -EROFS && r < 0) {
1230 _E("fail to mount %s device : %d", data->devnode, r);
1235 data->readonly = true;
1236 BLOCK_FLAG_SET(data, MOUNT_READONLY);
1239 data->state = BLOCK_MOUNT;
1241 if (data->block_type == BLOCK_MMC_DEV) {
1242 /* app2ext_migrate_legacy_all has dbus method call to deviced */
1243 ret = app2ext_migrate_legacy_all();
1245 _E("app2ext failed");
1249 if (r < 0 && r != -EROFS)
1250 data->state = BLOCK_UNMOUNT;
1252 _I("%s result : %s, %d", __func__, data->devnode, r);
1254 if (pipe_trigger(BLOCK_DEV_MOUNT, bdev, r) < 0)
1255 _E("fail to trigger pipe");
1260 static int change_mount_point(struct block_device *bdev,
1261 const char *mount_point)
1263 struct block_data *data;
1269 free(data->mount_point);
1271 /* If the mount path already exists, the path cannot be used */
1273 access(mount_point, F_OK) != 0) {
1274 data->mount_point = strdup(mount_point);
1275 bdev->mount_point_updated = true;
1277 data->mount_point = generate_mount_path(data);
1278 bdev->mount_point_updated = false;
1284 static int mount_block_device(struct block_device *bdev)
1286 struct block_data *data;
1289 if (!bdev || !bdev->data)
1293 if (data->state == BLOCK_MOUNT) {
1294 _I("%s is already mounted", data->devnode);
1298 if (!block_conf[data->block_type].multimount &&
1300 _I("Not support multi mount by config info");
1304 r = mount_start(bdev);
1306 _E("Failed to mount (%s)", data->devnode);
1313 static int block_unmount(struct block_device *bdev,
1314 enum unmount_operation option)
1316 struct block_data *data;
1318 struct timespec time = {0,};
1320 if (!bdev || !bdev->data || !bdev->data->mount_point)
1325 if (bdev->on_private_op == REQ_NORMAL)
1326 signal_device_blocked(bdev);
1328 /* it must called before unmounting mmc */
1329 r = mmc_check_and_unmount(data->mount_point);
1332 if (option == UNMOUNT_NORMAL) {
1333 _I("Failed to unmount with normal option : %d", r);
1337 _I("Execute force unmount!");
1338 /* Force Unmount Scenario */
1343 * should unmount the below vconf key. */
1344 if ((data->block_type == BLOCK_MMC_DEV ||
1345 data->block_type == BLOCK_EXTENDEDSD_DEV) &&
1347 /* At first, notify to other app
1348 * who already access sdcard */
1349 _I("Notify to other app who already access sdcard");
1350 vconf_set_int(VCONFKEY_SYSMAN_MMC_STATUS,
1351 VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED);
1355 /* Second, kill app with SIGTERM */
1356 _I("Kill app with SIGTERM");
1357 terminate_process(data->mount_point, false);
1360 /* Last time, kill app with SIGKILL */
1361 _I("Kill app with SIGKILL");
1362 terminate_process(data->mount_point, true);
1365 if (umount2(data->mount_point, MNT_DETACH) != 0) {
1366 _I("Failed to unmount with lazy option : %d",
1373 /* it takes some seconds til other app completely clean up */
1374 time.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
1375 nanosleep(&time, NULL);
1377 print_open_files(data->mount_point);
1379 r = mmc_check_and_unmount(data->mount_point);
1381 _D("Success to unmount (%s)", data->mount_point);
1387 data->state = BLOCK_UNMOUNT;
1389 if (rmdir(data->mount_point) < 0)
1390 _E("fail to remove %s directory", data->mount_point);
1395 static int unmount_block_device(struct block_device *bdev,
1396 enum unmount_operation option)
1398 struct block_data *data;
1401 if (!bdev || !bdev->data)
1405 if (data->state == BLOCK_UNMOUNT) {
1406 _I("%s is already unmounted", data->devnode);
1407 r = mmc_check_and_unmount(data->mount_point);
1409 _E("The path was existed, but could not delete it(%s)",
1414 _I("Unmount Start : (%s -> %s)",
1415 data->devnode, data->mount_point);
1417 r = block_unmount(bdev, option);
1419 _E("fail to unmount %s device : %d", data->devnode, r);
1423 BLOCK_FLAG_MOUNT_CLEAR(data);
1426 _I("%s result : %s, %d", __func__, data->devnode, r);
1428 if (pipe_trigger(BLOCK_DEV_UNMOUNT, bdev, r) < 0)
1429 _E("fail to trigger pipe");
1434 static int block_format(struct block_data *data,
1435 const char *fs_type, bool mount_point_updated)
1437 const struct block_fs_ops *fs;
1443 if (!data || !data->devnode || !data->mount_point)
1446 if (data->block_type == BLOCK_EXTENDEDSD_DEV)
1452 fstype = data->fs_type;
1458 len = strlen(fstype);
1459 DD_LIST_FOREACH(fs_head, elem, fs) {
1460 if (!strncmp(fs->name, fstype, len))
1465 BLOCK_FLAG_SET(data, FS_NOT_SUPPORTED);
1466 _E("not supported file system(%s)", fstype);
1470 _I("format path : %s", data->devnode);
1471 fs->check(data->devnode);
1472 r = fs->format(data->devnode);
1474 _E("fail to format block data for %s", data->devnode);
1478 /* need to update the partition data.
1479 * It can be changed in doing format. */
1480 retrieve_udev_device(data, mount_point_updated);
1485 static int format_block_device(struct block_device *bdev,
1486 const char *fs_type,
1487 enum unmount_operation option)
1489 struct block_data *data;
1497 _I("Format Start : (%s -> %s)",
1498 data->devnode, data->mount_point);
1500 if (data->state == BLOCK_MOUNT) {
1501 r = block_unmount(bdev, option);
1503 _E("fail to unmount %s device : %d", data->devnode, r);
1508 r = block_format(data, fs_type, bdev->mount_point_updated);
1510 _E("fail to format %s device : %d", data->devnode, r);
1513 _I("%s result : %s, %d", __func__, data->devnode, r);
1515 r = pipe_trigger(BLOCK_DEV_FORMAT, bdev, r);
1517 _E("fail to trigger pipe");
1522 static struct format_data *get_format_data(
1523 const char *fs_type, enum unmount_operation option)
1525 struct format_data *fdata;
1527 fdata = (struct format_data *)malloc(sizeof(struct format_data));
1529 _E("fail to allocate format data");
1534 fdata->fs_type = strdup(fs_type);
1536 fdata->fs_type = NULL;
1537 fdata->option = option;
1542 static void release_format_data(struct format_data *data)
1546 free(data->fs_type);
1551 // Called by BlockThread - Real Mount Op
1552 static int block_mount_device(struct block_device *bdev, void *data)
1561 thread_id = bdev->thread_id;
1562 if (thread_id < 0 || thread_id >= THREAD_MAX)
1564 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1565 l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
1566 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1568 _E("(%d) does not exist in the device list", bdev->data->devnode);
1572 /* mount automatically */
1573 ret = mount_block_device(bdev);
1575 _E("fail to mount block device for %s", bdev->data->devnode);
1580 // Called by BlockThread - Real Format Op
1581 static int block_format_device(struct block_device *bdev, void *data)
1586 struct format_data *fdata = (struct format_data *)data;
1588 if (!bdev || !fdata) {
1593 thread_id = bdev->thread_id;
1594 if (thread_id < 0 || thread_id >= THREAD_MAX)
1596 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1597 l = DD_LIST_FIND(th_manager[thread_id].block_dev_list, bdev);
1598 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1600 _E("(%d) does not exist in the device list", bdev->data->devnode);
1605 ret = format_block_device(bdev, fdata->fs_type, fdata->option);
1607 _E("fail to mount block device for %s", bdev->data->devnode);
1610 release_format_data(fdata);
1615 // Called by BlockThread - Real Unmount Op
1616 static int block_unmount_device(struct block_device *bdev, void *data)
1619 long option = (long)data;
1624 ret = unmount_block_device(bdev, option);
1626 _E("Failed to unmount block device (%s)", bdev->data->devnode);
1633 /* Called by BlockThread - Remove Operation
1634 Direct Call at BlockThread
1635 Previously this function was called by MainThread. However, it will increase complexity.
1636 Need thread lock before to call remove_operation
1638 static void remove_operation(struct block_device *bdev)
1640 struct operation_queue *op;
1646 thread_id = bdev->thread_id;
1647 if (thread_id < 0 || thread_id >= THREAD_MAX)
1650 DD_LIST_FOREACH_SAFE(bdev->op_queue, l, next, op) {
1652 _D("Remove operation (%s, %s)",
1653 get_operation_char(op->op),
1654 bdev->data->devnode);
1656 DD_LIST_REMOVE(bdev->op_queue, op);
1662 static void block_send_dbus_reply(dbus_method_reply_handle_h reply_handle, int result)
1669 rep = make_dbus_reply_message_simple(reply_handle, result);
1670 reply_dbus_method_result(reply_handle, rep);
1673 // Called by BlockThread
1674 static void check_removed(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1676 struct operation_queue *temp;
1689 thread_id = bdev->thread_id;
1690 if (thread_id < 0 || thread_id >= THREAD_MAX)
1693 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1695 DD_LIST_FOREACH(*queue, l, temp) {
1696 if (temp->op == BLOCK_DEV_REMOVE) {
1701 th_manager[thread_id].op_len--;
1702 block_send_dbus_reply((*op)->reply_handle, 0);
1705 remove_operation(bdev);
1706 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1709 // Called by BlockThread
1710 static bool check_unmount(struct block_device *bdev, dd_list **queue, struct operation_queue **op)
1712 struct operation_queue *temp;
1715 bool unmounted = false;
1726 thread_id = bdev->thread_id;
1727 if (thread_id < 0 || thread_id >= THREAD_MAX)
1730 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1731 DD_LIST_FOREACH(*queue, l, temp) {
1732 if (temp->op == BLOCK_DEV_UNMOUNT) {
1734 _D("Operation queue has unmount operation");
1738 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1743 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1745 DD_LIST_FOREACH(*queue, l, temp) {
1746 if (temp->op == BLOCK_DEV_UNMOUNT) {
1751 th_manager[thread_id].op_len--;
1752 block_send_dbus_reply((*op)->reply_handle, 0);
1755 remove_operation(bdev);
1756 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1761 // Called by BlockThread
1762 static void trigger_operation(struct block_device *bdev, dd_list *queue, struct operation_queue *op)
1766 char devnode[PATH_MAX];
1767 enum block_dev_operation operation;
1768 bool unmounted = false;
1775 thread_id = bdev->thread_id;
1776 if (thread_id < 0 || thread_id >= THREAD_MAX)
1779 snprintf(devnode, sizeof(devnode), "%s", bdev->data->devnode);
1789 _D("Thread id %d Trigger operation (%s, %s)", thread_id,
1790 get_operation_char(operation), devnode);
1793 if (operation == BLOCK_DEV_INSERT && bdev->removed) {
1794 check_removed(bdev, &queue, &op);
1796 _D("Trigger operation again (%s, %s)",
1797 get_operation_char(operation), devnode);
1799 if (operation == BLOCK_DEV_MOUNT) {
1800 unmounted = check_unmount(bdev, &queue, &op);
1803 _D("Trigger operation again (%s, %s)",
1804 get_operation_char(operation), devnode);
1808 switch (operation) {
1809 case BLOCK_DEV_INSERT:
1811 case BLOCK_DEV_MOUNT:
1812 ret = block_mount_device(bdev, op->data);
1813 _D("Mount (%s) result:(%d)", devnode, ret);
1815 case BLOCK_DEV_FORMAT:
1816 ret = block_format_device(bdev, op->data);
1817 _D("Format (%s) result:(%d)", devnode, ret);
1819 case BLOCK_DEV_UNMOUNT:
1820 ret = block_unmount_device(bdev, op->data);
1821 _D("Unmount (%s) result:(%d)", devnode, ret);
1823 case BLOCK_DEV_REMOVE:
1826 case BLOCK_LUKS_CLOSE:
1827 ret = ode_luks_close_sync(EXTENDEDSD_NAME);
1829 _E("Failed on ode_luks_close(%s)", EXTENDEDSD_NAME);
1832 _E("Operation type is invalid (%d)", op->op);
1838 * during checking the queue length */
1839 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1842 th_manager[thread_id].op_len--;
1844 block_send_dbus_reply(op->reply_handle, ret);
1846 queue = bdev->op_queue;
1847 if (queue != NULL) {
1848 queue = DD_LIST_NEXT(queue);
1850 op = DD_LIST_NTH(queue, 0);
1856 remove_operation(bdev);
1858 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1861 if (operation == BLOCK_DEV_INSERT || operation == BLOCK_DEV_REMOVE) {
1862 if (pipe_trigger(operation, bdev, 0) < 0)
1863 _E("fail to trigger pipe");
1870 // Called by BlockThread
1871 static void *block_th_start(void *arg)
1873 struct block_device *temp;
1874 struct manage_thread *th = (struct manage_thread *)arg;
1875 struct operation_queue *op = NULL;
1877 dd_list *queue = NULL;
1882 thread_id = th->thread_id;
1883 if (thread_id < 0 || thread_id >= THREAD_MAX) {
1884 _E("Thread Number: %d", th->thread_id);
1889 pthread_mutex_lock(&(th_manager[thread_id].mutex));
1890 if (th_manager[thread_id].op_len == 0) {
1891 _D("Operation queue of thread is empty");
1892 pthread_cond_wait(&(th_manager[thread_id].cond), &(th_manager[thread_id].mutex));
1893 _D("Wake up %d", thread_id);
1896 DD_LIST_FOREACH(th_manager[thread_id].block_dev_list, elem, temp) {
1897 queue = temp->op_queue;
1899 op = DD_LIST_NTH(queue, 0);
1901 _D("Operation queue for device %s is Empty", temp->data->devnode);
1905 queue = DD_LIST_NEXT(queue);
1913 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
1915 if (op && !op->done)
1916 trigger_operation(temp, queue, op);
1921 // This function will be refactored later
1922 // Especially, we don't need to keep th_node_list.
1923 static int find_thread(char *devnode)
1930 int i, len, min, min_num;
1931 int dev_mmc = -1, part = -1, num;
1934 if (!fnmatch("*/"MMC_PARTITION_PATH, devnode, 0)) {
1935 sscanf(devnode, "/dev/mmcblk%dp%d", &dev_mmc, &part);
1942 snprintf(str, len, "/dev/mmcblk%d", dev_mmc);
1943 th_node = strdup(str);
1944 } else if (!fnmatch("*/"SCSI_PARTITION_PATH, devnode, 0)) {
1945 sscanf(devnode, "/dev/sd%c%d", &dev_scsi, &part);
1946 snprintf(str, SCSI_PARTITION_LENGTH, "/dev/sd%c", dev_scsi);
1947 th_node = strdup(str);
1949 th_node = strdup(devnode);
1951 len = strlen(th_node) + 1;
1954 for (i = 0; i < THREAD_MAX; i++) {
1955 DD_LIST_FOREACH(th_manager[i].th_node_list, elem, temp) {
1956 if (!strncmp(temp, th_node, len)) {
1961 if (th_manager[i].num_dev < min_num) {
1962 min_num = th_manager[i].num_dev;
1967 if (min >= 0 && min < THREAD_MAX) {
1968 DD_LIST_APPEND(th_manager[min].th_node_list, th_node);
1972 _E("Finding thread is failed");
1973 DD_LIST_APPEND(th_manager[0].th_node_list, th_node);
1977 /* Only Main thread is permmited */
1978 // Called by MainThread
1979 static int add_operation(struct block_device *bdev,
1980 enum block_dev_operation operation,
1981 dbus_method_reply_handle_h reply_handle, void *data)
1983 struct operation_queue *op;
1992 _I("Add operation (%s, %s)",
1993 get_operation_char(operation),
1994 bdev->data->devnode);
1996 thread_id = bdev->thread_id;
1997 if (thread_id < 0 || thread_id >= THREAD_MAX) {
1998 _E("Fail to find thread to add");
2002 op = (struct operation_queue *)malloc(sizeof(struct operation_queue));
2004 _E("malloc failed");
2010 op->reply_handle = reply_handle;
2013 * during adding queue and checking the queue length */
2014 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2016 /* Only modified between lock and unlock of mutex */
2019 start_th = th_manager[thread_id].start_th;
2020 DD_LIST_APPEND(bdev->op_queue, op);
2021 th_manager[thread_id].op_len++;
2023 if (th_manager[thread_id].op_len == 1 && !start_th)
2024 pthread_cond_signal(&(th_manager[thread_id].cond));
2026 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2029 /* Need to disble app2ext whenever unmounting mmc */
2030 if (op->op == BLOCK_DEV_UNMOUNT &&
2031 bdev->data->state == BLOCK_MOUNT &&
2032 bdev->data->block_type == BLOCK_MMC_DEV &&
2033 bdev->data->primary)
2034 if (app2ext_disable_all_external_pkgs() < 0)
2035 _E("app2ext_disable_all_external_pkgs() failed");
2039 _D("Start New thread for block device");
2040 th_manager[thread_id].start_th = false;
2041 ret = pthread_create(&(th_manager[thread_id].th), NULL, block_th_start, &th_manager[thread_id]);
2043 _E("fail to create thread for %s", bdev->data->devnode);
2047 pthread_detach(th_manager[thread_id].th);
2053 static bool disk_is_partitioned_by_kernel(struct udev_device *dev)
2056 struct dirent entry;
2058 const char *syspath;
2061 syspath = udev_device_get_syspath(dev);
2065 dp = opendir(syspath);
2067 _E("fail to open %s", syspath);
2071 /* TODO compare devname and d_name */
2072 while (readdir_r(dp, &entry, &dir) == 0 && dir != NULL) {
2073 if (!fnmatch(MMC_PARTITION_PATH, dir->d_name, 0) ||
2074 !fnmatch(SCSI_PARTITION_PATH, dir->d_name, 0)) {
2086 static bool check_partition(struct udev_device *dev)
2088 const char *devtype;
2089 const char *part_table_type;
2090 const char *fs_usage;
2093 /* only consider disk type, never partitions */
2094 devtype = udev_device_get_devtype(dev);
2098 if (strncmp(devtype, BLOCK_DEVTYPE_DISK,
2099 sizeof(BLOCK_DEVTYPE_DISK)) != 0)
2102 part_table_type = udev_device_get_property_value(dev,
2103 "ID_PART_TABLE_TYPE");
2104 if (part_table_type) {
2105 fs_usage = udev_device_get_property_value(dev,
2108 strncmp(fs_usage, FILESYSTEM_NAME, sizeof(FILESYSTEM_NAME)) == 0) {
2109 if (!disk_is_partitioned_by_kernel(dev))
2116 if (disk_is_partitioned_by_kernel(dev)) {
2125 // Called by MainThread
2126 static int add_block_device(struct udev_device *dev, const char *devnode, bool mapper)
2128 struct block_data *data;
2129 struct block_device *bdev;
2130 //char id_string[PATH_LEN];
2134 bool need_format = false;
2136 partition = check_partition(dev);
2138 /* if there is a partition, skip this request */
2139 _I("%s device has partitions, skip this time", devnode);
2143 if (mapper && !udev_device_get_property_value(dev, "ID_FS_TYPE")) {
2144 char syspath[128] = {0};
2147 r = rindex(udev_device_get_syspath(dev), '/');
2148 if (!r) return -ENODEV;
2150 snprintf(syspath, sizeof(syspath), "/sys/block%s", r);
2152 data = make_block_data(devnode,
2157 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2158 udev_device_get_sysattr_value(dev, "ro"));
2161 data = make_block_data(devnode,
2162 udev_device_get_syspath(dev),
2163 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2164 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2165 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2166 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2167 udev_device_get_sysattr_value(dev, "ro"));
2171 _E("fail to make block data for %s", devnode);
2175 if (!block_conf[data->block_type].multimount && !data->primary &&
2176 data->fs_usage && !strcmp(data->fs_usage, FILESYSTEM_NAME)) {
2177 _D("Not support multi mount by config info");
2178 free_block_data(data);
2182 bdev = make_block_device(data);
2184 _E("fail to make block device for %s", devnode);
2185 free_block_data(data);
2189 thread_id = find_thread(bdev->data->devnode);
2190 if (thread_id < 0 || thread_id >= THREAD_MAX) {
2191 _E("Fail to find thread to add");
2192 free_block_device(bdev);
2195 bdev->thread_id = thread_id;
2197 pthread_mutex_lock(&(th_manager[thread_id].mutex));
2198 th_manager[thread_id].num_dev++;
2199 DD_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
2200 pthread_mutex_unlock(&(th_manager[thread_id].mutex));
2203 struct format_data *fdata;
2205 fdata = get_format_data(NULL, UNMOUNT_FORCE);
2207 _E("Failed to get format data");
2211 ret = add_operation(bdev, BLOCK_DEV_FORMAT, NULL, (void *)fdata);
2213 _E("Failed to add operation (format %s)", bdev->data->devnode);
2214 release_format_data(fdata);
2218 if (!bdev->data->fs_type) {
2219 _E("Unformatted Storage");
2220 free_block_device(bdev);
2222 } else if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2223 // bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2224 bdev->data->primary = true;
2225 _D("Need to unlock encrypted sdcard");
2226 // ---- ODE UI launch ----
2227 ret = launch_system_app(POPUP_DEFAULT
2230 , "unlockextendedsd"
2234 , bdev->data->devnode
2238 _E("Failed to launch popup");
2240 ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
2242 _E("Failed to add operation (insert %s)", devnode);
2243 free_block_device(bdev);
2248 } else if (mapper && !strncmp(bdev->data->fs_type, EXT4_NAME, strlen(EXT4_NAME))) {
2249 bdev->data->block_type = BLOCK_EXTENDEDSD_DEV;
2250 ret = change_mount_point(bdev, EXTENDEDSD_MOUNT_PATH);
2253 free_block_device(bdev);
2258 ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
2260 _E("Failed to add operation (insert %s)", devnode);
2261 free_block_device(bdev);
2265 // Not a regular filesystem -> skip mounting
2266 if (!bdev->data->fs_usage || strcmp(bdev->data->fs_usage, FILESYSTEM_NAME)) {
2267 _I("Not a filesystem. Not mounting");
2271 /* Create file for block device /run/external-storage/id */
2272 create_file(bdev->data->id, bdev->data->mount_point, bdev->data->block_type == BLOCK_EXTENDEDSD_DEV);
2273 ret = add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL);
2275 _E("Failed to add operation (mount %s)", devnode);
2281 static int remove_block_device(struct udev_device *dev, const char *devnode)
2283 struct block_device *bdev;
2284 struct block_device *bdev_extended;
2287 bdev = find_block_device(devnode);
2289 _E("fail to find block data for %s", devnode);
2293 BLOCK_FLAG_SET(bdev->data, UNMOUNT_UNSAFE);
2295 bdev->removed = true;
2296 if (bdev->on_private_op != REQ_NORMAL) {
2297 bdev->on_private_op = REQ_NORMAL;
2298 _D("Private operation state: %d", bdev->on_private_op);
2301 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
2302 bdev_extended = find_block_device_path(EXTENDEDSD_MOUNT_PATH);
2304 if (bdev_extended) {
2305 BLOCK_FLAG_SET(bdev_extended->data, UNMOUNT_UNSAFE);
2307 bdev_extended->removed = true;
2308 if (bdev_extended->on_private_op != REQ_NORMAL) {
2309 bdev_extended->on_private_op = REQ_NORMAL;
2310 _D("Private operation state: %d", bdev_extended->on_private_op);
2313 ret = add_operation(bdev_extended, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2315 _E("Failed to add operation (unmount %s)", devnode);
2319 ret = add_operation(bdev_extended, BLOCK_DEV_REMOVE, NULL, NULL);
2321 _E("Failed to add operation (remove %s)", devnode);
2325 ret = add_operation(bdev_extended, BLOCK_LUKS_CLOSE, NULL, NULL);
2327 _E("Failed to add operation (luks_close %s)", devnode);
2331 _E("fail to find block data for extended sd card");
2334 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2336 _E("Failed to add operation (unmount %s)", devnode);
2340 ret = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2342 _E("Failed to add operation (remove %s)", devnode);
2349 static int get_internal_storage_number(void)
2351 struct libmnt_table *t = NULL;
2352 struct libmnt_fs *fs;
2355 int r = 0, dev_temp;
2357 if ((!is_emulator() && (dev_internal >= 0 || dev_internal_scsi != '\0')) ||
2358 (is_emulator() && dev_internal_emul != '\0'))
2361 t = mnt_new_table();
2365 r = mnt_table_parse_mtab(t, NULL);
2371 fs = mnt_table_find_target(t, ROOT_DIR, MNT_ITER_BACKWARD);
2377 temp = mnt_fs_get_srcpath(fs);
2381 name = strrchr(temp, '/');
2385 /* Boot from USB is not handled */
2386 if (!is_emulator()) {
2387 if (!fnmatch(MMC_PATH, temp, 0))
2388 sscanf(name, "mmcblk%d", &dev_internal);
2389 else if (!fnmatch(SCSI_PATH, temp, 0))
2390 sscanf(name, "sd%c", &dev_internal_scsi);
2392 if (!fnmatch(MMC_LINK_PATH, temp, 0))
2393 sscanf(name, "vd%c%d", &dev_internal_emul, &dev_temp);
2395 dev_internal_emul = '\0';
2403 static int check_external_storage(const char* devnode)
2405 char dev_scsi = '\0';
2408 int dev_num = -1, dev_temp;
2413 name = strrchr(devnode, '/');
2417 if (!is_emulator()) {
2418 if (!fnmatch(MMC_PATH, devnode, 0)) {
2419 sscanf(name, "mmcblk%d", &dev_num);
2420 if (dev_internal == dev_num) {
2421 _D("%s is internal storage", devnode);
2424 } else if (!fnmatch(SCSI_PATH, devnode, 0)) {
2425 sscanf(name, "sd%c", &dev_scsi);
2426 if (dev_internal_scsi == dev_scsi) {
2427 _D("%s is internal storage", devnode);
2432 if (!fnmatch(MMC_LINK_PATH, devnode, 0)) {
2433 sscanf(name, "vd%c%d", &emul, &dev_temp);
2434 if (dev_internal_emul == emul) {
2435 _D("%s is internal storage", devnode);
2444 static int check_already_handled(const char* devnode)
2446 struct block_device *bdev;
2447 struct block_data *data;
2451 for (i = 0; i < THREAD_MAX; i++) {
2452 pthread_mutex_lock(&(th_manager[i].mutex));
2453 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2459 if (!strncmp(data->devnode, devnode, strlen(devnode) + 1)) {
2460 pthread_mutex_unlock(&(th_manager[i].mutex));
2464 pthread_mutex_unlock(&(th_manager[i].mutex));
2470 static int block_init_from_udev_enumerate(void)
2473 struct udev_enumerate *enumerate;
2474 struct udev_list_entry *list_entry, *list_sub_entry;
2475 struct udev_device *dev;
2476 const char *syspath;
2477 const char *devnode;
2482 _E("fail to create udev library context");
2486 /* create a list of the devices in the 'usb' subsystem */
2487 enumerate = udev_enumerate_new(udev);
2489 _E("fail to create an enumeration context");
2493 udev_enumerate_add_match_subsystem(enumerate, BLOCK_SUBSYSTEM);
2494 udev_enumerate_add_match_property(enumerate,
2495 UDEV_DEVTYPE, BLOCK_DEVTYPE_DISK);
2496 udev_enumerate_add_match_property(enumerate,
2497 UDEV_DEVTYPE, BLOCK_DEVTYPE_PARTITION);
2498 udev_enumerate_scan_devices(enumerate);
2500 udev_list_entry_foreach(list_entry,
2501 udev_enumerate_get_list_entry(enumerate)) {
2502 syspath = udev_list_entry_get_name(list_entry);
2506 dev = udev_device_new_from_syspath(
2507 udev_enumerate_get_udev(enumerate),
2513 udev_list_entry_foreach(list_sub_entry,
2514 udev_device_get_devlinks_list_entry(dev)) {
2515 const char *devlink = udev_list_entry_get_name(list_sub_entry);
2516 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2523 devnode = udev_device_get_devnode(dev);
2525 udev_device_unref(dev);
2529 if (fnmatch(MMC_PATH, devnode, 0) &&
2530 fnmatch(SCSI_PATH, devnode, 0) &&
2531 fnmatch(EXTENDEDSD_NODE_PATH, devnode, 0)) {
2532 udev_device_unref(dev);
2537 r = check_external_storage(devnode);
2539 udev_device_unref(dev);
2543 r = check_already_handled(devnode);
2545 _I("%s is already handled", devnode);
2546 udev_device_unref(dev);
2550 _I("%s device add", devnode);
2551 add_block_device(dev, devnode, false);
2553 udev_device_unref(dev);
2556 udev_enumerate_unref(enumerate);
2561 // Called by MainThread
2562 static void show_block_device_list(void)
2564 struct block_device *bdev;
2565 struct block_data *data;
2569 for (i = 0; i < THREAD_MAX; i++) {
2570 pthread_mutex_lock(&(th_manager[i].mutex));
2571 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
2577 _D("%s:", data->devnode);
2578 _D("\tSyspath: %s", data->syspath);
2579 _D("\tBlock type: %d", data->block_type);
2580 _D("\tFs type: %s", data->fs_type);
2581 _D("\tFs usage: %s", data->fs_usage);
2582 _D("\tFs version: %s", data->fs_version);
2583 _D("\tFs uuid enc: %s", data->fs_uuid_enc);
2584 _D("\tReadonly: %s",
2585 (data->readonly ? "true" : "false"));
2586 _D("\tMount point: %s", data->mount_point);
2587 _D("\tMount state: %s",
2588 (data->state == BLOCK_MOUNT ?
2589 "mount" : "unmount"));
2591 (data->primary ? "true" : "false"));
2592 _D("\tID: %d", data->id);
2594 pthread_mutex_unlock(&(th_manager[i].mutex));
2598 // Called by MainThread
2599 static void remove_whole_block_device(void)
2601 struct block_device *bdev;
2607 for (i = 0; i < THREAD_MAX; i++) {
2609 pthread_mutex_lock(&(th_manager[i].mutex));
2610 DD_LIST_FOREACH_SAFE(th_manager[i].block_dev_list, elem, next, bdev) {
2611 if (bdev->removed == false)
2614 pthread_mutex_unlock(&(th_manager[i].mutex));
2616 if (bdev && bdev->removed == false) {
2617 bdev->removed = true;
2618 r = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2620 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2622 r = add_operation(bdev, BLOCK_DEV_REMOVE, NULL, NULL);
2624 _E("Failed to add operation (remove %s)", bdev->data->devnode);
2631 static void booting_done(const char *sender_name,
2632 const char *object_path, const char *interface_name,
2633 const char *signal_name, DBusMessage *msg,
2636 static int done = 0;
2644 /* register mmc uevent control routine */
2645 ret = register_udev_uevent_control(&uh);
2647 _E("fail to register block uevent : %d", ret);
2649 /* if there is the attached device, try to mount */
2650 block_init_from_udev_enumerate();
2652 block_control = true;
2656 static void block_poweroff(const char *sender_name,
2657 const char *object_path, const char *interface_name,
2658 const char *signal_name, DBusMessage *msg,
2661 static int status = 0;
2666 /* unregister mmc uevent control routine */
2667 unregister_udev_uevent_control(&uh);
2668 remove_whole_block_device();
2671 static void uevent_block_handler(struct udev_device *dev)
2673 const char *devnode = NULL;
2675 struct udev_list_entry *list_entry;
2677 bool mapper = false;
2679 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
2680 const char *devlink = udev_list_entry_get_name(list_entry);
2681 if (!fnmatch(MMC_LINK_PATH, devlink, 0)) {
2685 if (!fnmatch(EXTENDEDSD_NODE_PATH, devlink, 0)) {
2693 devnode = udev_device_get_devnode(dev);
2697 if (fnmatch(MMC_PATH, devnode, 0) &&
2698 fnmatch(SCSI_PATH, devnode, 0))
2702 r = check_external_storage(devnode);
2706 action = udev_device_get_action(dev);
2710 _I("%s device %s", devnode, action);
2711 if (!strncmp(action, UDEV_ADD, sizeof(UDEV_ADD)) ||
2712 (mapper && !strcmp(action, UDEV_CHANGE))) {
2713 r = check_already_handled(devnode);
2715 _I("%s is already handled", devnode);
2719 add_block_device(dev, devnode, mapper);
2720 } else if (!strncmp(action, UDEV_REMOVE, sizeof(UDEV_REMOVE))) {
2721 remove_block_device(dev, devnode);
2722 } else if (!strncmp(action, UDEV_CHANGE, sizeof(UDEV_CHANGE))) {
2723 struct block_device *bdev;
2724 bdev = find_block_device(devnode);
2726 _E("fail to find block data for %s", devnode);
2729 if (!udev_device_get_property_value(dev, "ID_FS_TYPE"))
2732 r = update_block_data(bdev->data,
2733 udev_device_get_property_value(dev, "ID_FS_USAGE"),
2734 udev_device_get_property_value(dev, "ID_FS_TYPE"),
2735 udev_device_get_property_value(dev, "ID_FS_VERSION"),
2736 udev_device_get_property_value(dev, "ID_FS_UUID_ENC"),
2737 udev_device_get_sysattr_value(dev, "ro"),
2740 _E("fail to update block data for %s", bdev->data->devnode);
2741 if (!strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
2742 _I("filesystem type is updated: crypto_LUKS");
2746 static DBusMessage *request_mount_block(dbus_method_reply_handle_h reply_handle,
2747 DBusMessage *msg, bool onprivate)
2749 struct block_device *bdev;
2754 if (!reply_handle || !msg)
2757 ret = dbus_message_get_args(msg, NULL,
2758 DBUS_TYPE_INT32, &id,
2759 DBUS_TYPE_STRING, &mount_point,
2764 bdev = find_block_device_by_id(id);
2766 _E("Failed to find (%d) in the device list", id);
2770 if (bdev->on_private_op != REQ_NORMAL) {
2775 if (bdev->data->state == BLOCK_MOUNT) {
2776 _I("%s is already mounted", bdev->data->devnode);
2782 bdev->on_private_op = REQ_PRIVATE;
2783 bdev->private_pid = get_dbus_method_sender_pid(reply_handle);
2784 _D("Private operation state: %d", bdev->on_private_op);
2786 if (bdev->on_private_op != REQ_NORMAL) {
2787 _E("Failed to process mount operation");
2793 /* if requester want to use a specific mount point */
2794 if (mount_point && strncmp(mount_point, "", 1) != 0) {
2795 ret = change_mount_point(bdev, mount_point);
2801 /* Create /run/external-storage/id file */
2802 create_file(bdev->data->id, bdev->data->mount_point, false);
2804 /* Create file for block device /run/external-storage/id */
2805 create_file(bdev->data->id, bdev->data->mount_point, false);
2808 ret = add_operation(bdev, BLOCK_DEV_MOUNT, reply_handle, NULL);
2810 _E("Failed to add operation (mount %s)", bdev->data->devnode);
2817 return make_dbus_reply_message_simple(reply_handle, ret);
2820 static DBusMessage *request_public_mount_block(dbus_method_reply_handle_h reply_handle,
2823 return request_mount_block(reply_handle, msg, false);
2826 static DBusMessage *request_private_mount_block(dbus_method_reply_handle_h reply_handle,
2829 return request_mount_block(reply_handle, msg, true);
2832 static DBusMessage *request_unmount_block(dbus_method_reply_handle_h reply_handle,
2833 DBusMessage *msg, bool onprivate)
2835 struct block_device *bdev;
2841 if (!reply_handle || !msg)
2844 ret = dbus_message_get_args(msg, NULL,
2845 DBUS_TYPE_INT32, &id,
2846 DBUS_TYPE_INT32, &option,
2851 bdev = find_block_device_by_id(id);
2853 _E("Failed to find (%d) in the device list", id);
2859 pid = get_dbus_method_sender_pid(reply_handle);
2860 if (bdev->on_private_op == REQ_NORMAL || (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid)) {
2861 _E("Failed to process private unmount operation");
2866 if (bdev->on_private_op != REQ_NORMAL) {
2867 _E("Failed to process unmount operation");
2873 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, reply_handle, (void *)option);
2875 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2879 if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV) {
2880 ret = add_operation(bdev, BLOCK_LUKS_CLOSE, NULL, NULL);
2882 _E("Failed to add operation (luks_close %s)", bdev->data->devnode);
2888 return make_dbus_reply_message_simple(reply_handle, ret);
2891 static DBusMessage *request_public_unmount_block(dbus_method_reply_handle_h reply_handle,
2894 return request_unmount_block(reply_handle, msg, false);
2897 static DBusMessage *request_private_unmount_block(dbus_method_reply_handle_h reply_handle,
2900 return request_unmount_block(reply_handle, msg, true);
2903 static DBusMessage *request_format_block(dbus_method_reply_handle_h reply_handle,
2906 struct block_device *bdev;
2907 struct format_data *fdata;
2914 if (!reply_handle || !msg)
2917 ret = dbus_message_get_args(msg, NULL,
2918 DBUS_TYPE_INT32, &id,
2919 DBUS_TYPE_INT32, &option,
2924 bdev = find_block_device_by_id(id);
2926 _E("Failed to find (%d) in the device list", id);
2930 pid = get_dbus_method_sender_pid(reply_handle);
2931 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
2932 _E("Failed to format on private state");
2937 fdata = get_format_data(NULL, option);
2939 _E("Failed to get format data");
2943 prev_state = bdev->data->state;
2944 if (prev_state == BLOCK_MOUNT) {
2945 if (bdev->on_private_op == REQ_PRIVATE) {
2946 bdev->on_private_op = REQ_PRIVATE_FORMAT;
2947 _D("Private operation state: %d", bdev->on_private_op);
2949 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
2951 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
2952 release_format_data(fdata);
2957 ret = add_operation(bdev, BLOCK_DEV_FORMAT, reply_handle, (void *)fdata);
2959 _E("Failed to add operation (format %s)", bdev->data->devnode);
2960 release_format_data(fdata);
2963 /* Maintain previous state of mount/unmount */
2964 if (prev_state == BLOCK_MOUNT) {
2965 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
2966 _E("Failed to add operation (mount %s)", bdev->data->devnode);
2974 return make_dbus_reply_message_simple(reply_handle, ret);
2977 static DBusMessage *request_format_block_type(dbus_method_reply_handle_h reply_handle,
2980 struct block_device *bdev;
2981 struct format_data *fdata;
2989 if (!reply_handle || !msg)
2992 ret = dbus_message_get_args(msg, NULL,
2993 DBUS_TYPE_INT32, &id,
2994 DBUS_TYPE_INT32, &option,
2995 DBUS_TYPE_STRING, &type,
3000 bdev = find_block_device_by_id(id);
3002 _E("Failed to find (%d) in the device list", id);
3006 pid = get_dbus_method_sender_pid(reply_handle);
3007 if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
3008 _E("Failed to format on private state");
3013 fdata = get_format_data(type, option);
3015 _E("Failed to get format data");
3019 prev_state = bdev->data->state;
3020 if (prev_state == BLOCK_MOUNT) {
3021 if (bdev->on_private_op == REQ_PRIVATE) {
3022 bdev->on_private_op = REQ_PRIVATE_FORMAT;
3023 _D("Private operation state: %d", bdev->on_private_op);
3025 ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
3027 _E("Failed to add operation (unmount %s)", bdev->data->devnode);
3028 release_format_data(fdata);
3033 ret = add_operation(bdev, BLOCK_DEV_FORMAT, reply_handle, (void *)fdata);
3035 _E("Failed to add operation (format %s)", bdev->data->devnode);
3036 release_format_data(fdata);
3039 /* Maintain previous state of mount/unmount */
3040 if (prev_state == BLOCK_MOUNT) {
3041 if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
3042 _E("Failed to add operation (mount %s)", bdev->data->devnode);
3050 return make_dbus_reply_message_simple(reply_handle, ret);
3053 static int add_device_to_iter(struct block_data *data, DBusMessageIter *piter)
3055 char *str_null = "";
3057 if (!data || !piter)
3060 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3061 &(data->block_type));
3062 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3063 data->devnode ? &(data->devnode) : &str_null);
3064 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3065 data->syspath ? &(data->syspath) : &str_null);
3066 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3067 data->fs_usage ? &(data->fs_usage) : &str_null);
3068 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3069 data->fs_type ? &(data->fs_type) : &str_null);
3070 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3071 data->fs_version ? &(data->fs_version) : &str_null);
3072 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3073 data->fs_uuid_enc ? &(data->fs_uuid_enc) : &str_null);
3074 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3076 dbus_message_iter_append_basic(piter, DBUS_TYPE_STRING,
3077 data->mount_point ? &(data->mount_point) : &str_null);
3078 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3080 dbus_message_iter_append_basic(piter, DBUS_TYPE_BOOLEAN,
3082 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3084 dbus_message_iter_append_basic(piter, DBUS_TYPE_INT32,
3091 static int add_device_to_struct_iter(struct block_data *data, DBusMessageIter *iter)
3093 DBusMessageIter piter;
3098 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3099 add_device_to_iter(data, &piter);
3100 dbus_message_iter_close_container(iter, &piter);
3105 static int add_device_to_iter_2(struct block_data *data, DBusMessageIter *iter)
3107 DBusMessageIter piter;
3108 char *str_null = "";
3113 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &piter);
3114 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3115 &(data->block_type));
3116 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3117 data->devnode ? &(data->devnode) : &str_null);
3118 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3119 data->syspath ? &(data->syspath) : &str_null);
3120 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3121 data->fs_usage ? &(data->fs_usage) : &str_null);
3122 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3123 data->fs_type ? &(data->fs_type) : &str_null);
3124 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3125 data->fs_version ? &(data->fs_version) : &str_null);
3126 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3127 data->fs_uuid_enc ? &(data->fs_uuid_enc) : &str_null);
3128 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3130 dbus_message_iter_append_basic(&piter, DBUS_TYPE_STRING,
3131 data->mount_point ? &(data->mount_point) : &str_null);
3132 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3134 dbus_message_iter_append_basic(&piter, DBUS_TYPE_BOOLEAN,
3136 dbus_message_iter_append_basic(&piter, DBUS_TYPE_INT32,
3138 dbus_message_iter_close_container(iter, &piter);
3143 static DBusMessage *request_get_device_info(dbus_method_reply_handle_h reply_handle,
3146 DBusMessageIter iter;
3148 struct block_device *bdev;
3149 struct block_data *data;
3152 if (!reply_handle || !msg)
3155 reply = make_dbus_reply_message(reply_handle);
3159 ret = dbus_message_get_args(msg, NULL,
3160 DBUS_TYPE_INT32, &id,
3165 bdev = find_block_device_by_id(id);
3172 dbus_message_iter_init_append(reply, &iter);
3173 add_device_to_iter(data, &iter);
3179 static DBusMessage *request_show_device_list(dbus_method_reply_handle_h reply_handle,
3182 show_block_device_list();
3183 return make_dbus_reply_message(reply_handle);
3186 // Called by MainThread
3187 static DBusMessage *request_get_device_list(dbus_method_reply_handle_h reply_handle,
3190 DBusMessageIter iter;
3191 DBusMessageIter aiter;
3193 struct block_device *bdev;
3194 struct block_data *data;
3201 reply = make_dbus_reply_message(reply_handle);
3203 ret = dbus_message_get_args(msg, NULL,
3204 DBUS_TYPE_STRING, &type,
3207 _E("Failed to get args");
3212 _E("Delivered type is NULL");
3216 _D("Block (%s) device list is requested", type);
3218 if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
3219 block_type = BLOCK_SCSI_DEV;
3220 else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
3221 block_type = BLOCK_MMC_DEV;
3222 else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
3225 _E("Invalid type (%s) is requested", type);
3229 dbus_message_iter_init_append(reply, &iter);
3230 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(issssssisibii)", &aiter);
3232 for (i = 0; i < THREAD_MAX; i++) {
3233 pthread_mutex_lock(&(th_manager[i].mutex));
3234 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3241 switch (block_type) {
3242 case BLOCK_SCSI_DEV:
3244 if (data->block_type != block_type)
3250 add_device_to_struct_iter(data, &aiter);
3252 pthread_mutex_unlock(&(th_manager[i].mutex));
3254 dbus_message_iter_close_container(&iter, &aiter);
3260 // Called by MainThread
3261 static DBusMessage *request_get_device_list_2(dbus_method_reply_handle_h reply_handle,
3264 DBusMessageIter iter;
3265 DBusMessageIter aiter;
3267 struct block_device *bdev;
3268 struct block_data *data;
3275 reply = make_dbus_reply_message(reply_handle);
3277 ret = dbus_message_get_args(msg, NULL,
3278 DBUS_TYPE_STRING, &type,
3281 _E("Failed to get args");
3286 _E("Delivered type is NULL");
3290 _D("Block (%s) device list is requested", type);
3292 if (!strncmp(type, BLOCK_TYPE_SCSI, sizeof(BLOCK_TYPE_SCSI)))
3293 block_type = BLOCK_SCSI_DEV;
3294 else if (!strncmp(type, BLOCK_TYPE_MMC, sizeof(BLOCK_TYPE_MMC)))
3295 block_type = BLOCK_MMC_DEV;
3296 else if (!strncmp(type, BLOCK_TYPE_ALL, sizeof(BLOCK_TYPE_ALL)))
3299 _E("Invalid type (%s) is requested", type);
3303 dbus_message_iter_init_append(reply, &iter);
3304 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(issssssisibi)", &aiter);
3306 for (i = 0; i < THREAD_MAX; i++) {
3307 pthread_mutex_lock(&(th_manager[i].mutex));
3308 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3315 switch (block_type) {
3316 case BLOCK_SCSI_DEV:
3318 if (data->block_type != block_type)
3325 add_device_to_iter_2(data, &aiter);
3327 pthread_mutex_unlock(&(th_manager[i].mutex));
3329 dbus_message_iter_close_container(&iter, &aiter);
3335 static DBusMessage *request_get_mmc_primary(dbus_method_reply_handle_h reply_handle,
3338 DBusMessageIter iter;
3340 struct block_device *bdev;
3341 struct block_data *data, nodata = {0,};
3346 if (!reply_handle || !msg)
3349 reply = make_dbus_reply_message(reply_handle);
3354 for (i = 0; i < THREAD_MAX; i++) {
3355 pthread_mutex_lock(&(th_manager[i].mutex));
3356 DD_LIST_FOREACH(th_manager[i].block_dev_list, elem, bdev) {
3362 if (data->block_type != BLOCK_MMC_DEV &&
3363 data->block_type != BLOCK_EXTENDEDSD_DEV)
3367 // Return mapper node(/dev/mapper/extendedsd) for primary mmc (not /dev/mmcblk1p1(ex))
3368 if (!strncmp(data->fs_type, LUKS_NAME, strlen(LUKS_NAME)))
3373 pthread_mutex_unlock(&(th_manager[i].mutex));
3378 dbus_message_iter_init_append(reply, &iter);
3380 add_device_to_iter(data, &iter);
3382 nodata.id = -ENODEV;
3383 add_device_to_iter(&nodata, &iter);
3390 static DBusMessage *request_check_speed(dbus_method_reply_handle_h reply_handle,
3393 struct timespec start_time, end_time;
3394 DBusMessageIter iter;
3396 struct block_device *bdev;
3397 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 << 20);
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 << 20);
3438 clock_gettime(CLOCK_REALTIME, &end_time);
3439 _I("end time %lu.%lu", end_time.tv_sec, end_time.tv_nsec);
3443 time_diff = end_time.tv_sec - start_time.tv_sec;
3444 if (time_diff > 0 && (SPEEDCHECK / time_diff < 4)) {
3451 _E("write() failed %d", errno);
3458 reply = dbus_message_new_method_return(msg);
3459 dbus_message_iter_init_append(reply, &iter);
3460 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
3465 static DBusMessage *request_control_block(dbus_method_reply_handle_h reply_handle,
3471 DBusMessageIter iter;
3473 ret = dbus_message_get_args(msg, NULL,
3474 DBUS_TYPE_INT32, &enable,
3482 _I("control block Enable");
3484 } else if (enable == 0) {
3485 _I("control block Disable");
3488 _E("control block : Wrong request by client");
3494 reply = dbus_message_new_method_return(msg);
3495 dbus_message_iter_init_append(reply, &iter);
3496 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
3501 static DBusMessage *request_getcontrol_block(dbus_method_reply_handle_h reply_handle,
3506 DBusMessageIter iter;
3508 _I("getcontrol block");
3510 is_enabled = block_control;
3512 reply = dbus_message_new_method_return(msg);
3513 dbus_message_iter_init_append(reply, &iter);
3514 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &is_enabled);
3520 Method name Method call format string Reply format string
3521 { "ShowDeviceList", NULL, NULL, request_show_device_list },
3522 { "GetDeviceList", "s", "a(issssssisibii)", request_get_device_list },
3523 { "GetDeviceList2", "s", "a(issssssisibi)", request_get_device_list_2 },
3524 { "Mount", "is", "i", request_public_mount_block },
3525 { "Unmoun, "ii", "i", request_public_unmount_block },
3526 { "Format", "ii", "i", request_format_block },
3527 { "GetDeviceInfo", "i", "(issssssisibii)", request_get_device_info },
3528 { "GetMmcPrimary", NULL, "(issssssisibii)" , request_get_mmc_primary },
3529 { "PrivateMount", "is", "i", request_private_mount_block },
3530 { "PrivateUnmount", "ii", "i", request_private_unmount_block },
3533 static const dbus_method_s manager_methods[] = {
3534 { "ShowDeviceList", NULL, request_show_device_list },
3535 { "GetDeviceList" , "s", request_get_device_list },
3536 { "GetDeviceList2", "s", request_get_device_list_2 },
3537 { "Mount", "is", request_public_mount_block },
3538 { "Unmount", "ii", request_public_unmount_block },
3539 { "Format", "ii", request_format_block },
3540 { "FormatwithType", "iis", request_format_block_type },
3541 { "GetDeviceInfo", "i", request_get_device_info },
3542 { "GetMmcPrimary" , NULL, request_get_mmc_primary },
3543 { "PrivateMount", "is", request_private_mount_block },
3544 { "PrivateUnmount", "ii", request_private_unmount_block },
3545 { "CheckSpeed", "i", request_check_speed },
3546 { "Control", "i", request_control_block },
3547 { "GetControl", "i", request_getcontrol_block },
3550 static dbus_interface_s block_interface = {
3551 .name = STORAGED_INTERFACE_BLOCK_MANAGER,
3552 .methods = manager_methods,
3553 .nr_methods = ARRAY_SIZE(manager_methods),
3556 static int load_config(struct parse_result *result, void *user_data)
3560 if (MATCH(result->section, "Block"))
3563 if (MATCH(result->section, "SCSI"))
3564 index = BLOCK_SCSI_DEV;
3565 else if (MATCH(result->section, "MMC"))
3566 index = BLOCK_MMC_DEV;
3567 else if (MATCH(result->section, "Mapper"))
3568 index = BLOCK_EXTENDEDSD_DEV;
3572 if (MATCH(result->name, "Multimount"))
3573 block_conf[index].multimount =
3574 (MATCH(result->value, "yes") ? true : false);
3575 if (MATCH(result->name, "ExtendedInternalStorage"))
3576 block_conf[index].extendedinternal =
3577 (MATCH(result->value, "yes") ? true : false);
3583 static int mount_root_path_tmpfs(void)
3588 root = tzplatform_getenv(TZ_SYS_MEDIA);
3592 if (access(root, F_OK) != 0)
3595 if (mount_check(root))
3598 ret = mount("tmpfs", root, "tmpfs", 0, "smackfsroot=System::Shared");
3601 _E("tmpfs mount failed (%d)", ret);
3608 #define mount_root_path_tmpfs() 0
3611 static void block_init(void *data)
3616 dbus_handle_h handle;
3621 ret = config_parse(BLOCK_CONF_FILE, load_config, NULL);
3623 _E("fail to load %s, Use default value", BLOCK_CONF_FILE);
3625 ret = mount_root_path_tmpfs();
3627 _E("Failed to mount tmpfs to root mount path (%d)", ret);
3629 ret = dbus_get_connection(&handle);
3631 _E("Failed to get dbus connection(%d)", ret);
3633 /* register block manager object and interface */
3634 ret = register_dbus_methods(handle,
3635 STORAGED_PATH_BLOCK_MANAGER, &block_interface,
3638 _E("Failed to register block interface and methods (%d)", ret);
3643 _E("fail to init pipe");
3645 /* System Session is loaded completely */
3646 register_dbus_signal(SYSTEMD_DBUS_PATH,
3647 SYSTEMD_DBUS_IFACE_MANAGER,
3648 SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED,
3649 booting_done, NULL, NULL);
3651 register_dbus_signal(DEVICED_PATH_POWEROFF,
3652 DEVICED_INTERFACE_POWEROFF,
3653 SIGNAL_POWEROFF_STATE,
3654 block_poweroff, NULL, NULL);
3656 for (i = 0; i < THREAD_MAX; i++) {
3657 th_manager[i].num_dev = 0;
3658 th_manager[i].op_len = 0;
3659 th_manager[i].start_th = true;
3660 th_manager[i].thread_id = i;
3661 pthread_mutex_init(&(th_manager[i].mutex), NULL);
3662 pthread_cond_init(&(th_manager[i].cond), NULL);
3665 ret = stat(EXTERNAL_STORAGE_PATH, &buf);
3667 ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
3669 _E("Failed to make directory: %d", errno);
3670 } else if (!S_ISDIR(buf.st_mode)) {
3671 ret = remove(EXTERNAL_STORAGE_PATH);
3673 _E("Fail to remove %s. errno: %d", EXTERNAL_STORAGE_PATH, errno);
3674 ret = mkdir(EXTERNAL_STORAGE_PATH, 0755);
3676 _E("Failed to make directory: %d", errno);
3678 ret = chmod(EXTERNAL_STORAGE_PATH, 0644);
3680 _E("Fail to change permissions of a file");
3683 ret = stat(EXTENDED_INTERNAL_PATH, &buf);
3685 ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
3687 _E("Failed to make directory: %d", errno);
3688 } else if (!S_ISDIR(buf.st_mode)) {
3689 ret = remove(EXTENDED_INTERNAL_PATH);
3691 _E("Fail to remove %s. errno: %d", EXTENDED_INTERNAL_PATH, errno);
3692 ret = mkdir(EXTENDED_INTERNAL_PATH, 0755);
3694 _E("Failed to make directory: %d", errno);
3696 ret = chmod(EXTENDED_INTERNAL_PATH, 0644);
3698 _E("Fail to change permissions of a file");
3701 ret = get_internal_storage_number();
3703 _E("Failed to get internal storage number");
3706 static void block_exit(void *data)
3708 dd_list *elem, *elem_next;
3714 /* unregister notifier for below each event */
3715 unregister_dbus_signal(SYSTEMD_DBUS_PATH,
3716 SYSTEMD_DBUS_IFACE_MANAGER,
3717 SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED,
3720 unregister_dbus_signal(DEVICED_PATH_POWEROFF,
3721 DEVICED_INTERFACE_POWEROFF,
3722 SIGNAL_POWEROFF_STATE, block_poweroff);
3724 /* unregister mmc uevent control routine */
3725 ret = unregister_udev_uevent_control(&uh);
3727 _E("fail to unregister block uevent : %d", ret);
3729 /* remove remaining blocks */
3730 remove_whole_block_device();
3732 for (i = 0; i < THREAD_MAX; i++) {
3733 if (!th_manager[i].start_th)
3734 pthread_cancel(th_manager[i].th);
3735 DD_LIST_FOREACH_SAFE(th_manager[i].th_node_list, elem, elem_next, temp) {
3736 DD_LIST_REMOVE(th_manager[i].th_node_list, temp);
3744 block_control = false;
3747 static int block_start(void *data)
3752 _E("Cannot be started. Booting is not ready");
3756 if (block_control) {
3757 _I("Already started");
3761 /* register mmc uevent control routine */
3762 ret = register_udev_uevent_control(&uh);
3764 _E("fail to register block uevent : %d", ret);
3766 block_init_from_udev_enumerate();
3768 block_control = true;
3774 static int block_stop(void *data)
3777 _E("Cannot be stopped. Booting is not ready");
3781 if (!block_control) {
3782 _I("Already stopped");
3786 /* unregister mmc uevent control routine */
3787 unregister_udev_uevent_control(&uh);
3789 /* remove the existing blocks */
3790 remove_whole_block_device();
3792 block_control = false;
3798 static storaged_module_interface block_module = {
3802 .start = block_start,
3806 __attribute__ ((visibility("default")))storaged_module_interface *
3807 storaged_get_module_interface(void)
3809 return &block_module;