Storage System of Android-Vold and MountService analysis (I), voldmountservice

Source: Internet
Author: User

Storage System of Android-Vold and MountService analysis (I), voldmountservice

Android storage system (1)

After reading the knowledge of the Vold storage module for a long time, I also got rid of the Android source code for a period of time and found the function calls involved in the Android storage system, and the Socket transmission between the Kernel and the upper layer is really a headache. In addition to the principle of the entire architecture, we also need to read the source code repeatedly, which is really depressing.

In addition to being depressed, I plan to sort out the posts and references I have read and post them for my reference. please correct me if there are any mistakes, let's talk to each other. Next we will go to the topic.

The storage system of Android is mainly composed of MountService in the SystemServer process and VolumeManager In the Vold process.

They manage system storage devices and perform various operations, such as mount, unmount, and format.

Figure 1 architecture of the Android Storage System

Figure 2 schematic diagram of the Android Storage System

[Important component analysis]

1. NetlinkManager

The full name is NetlinkManager. cpp in the Android 4.x Source Code Location/system/vold/NetlinkManager. cpp.

This class receives event messages from the kernel by referencing the onEvent () method in the NetlinkHandler class. NetlinkHandler is located in/system/vold/NetlinkHandler. cpp.

2. VolumeManager

The full name is VolumeManager. cpp in the Android 4.x Source Code Location/system/vold/VolumeManager. cpp. This class is mainly used to receive event messages processed by NetlinkManager.

Because SD is mounted here, messages processed by NetlinkManager are divided into five types: block, switch, usb_composite, battery, and power_supply.

The SD card mounting event is block.

3. DirectVolume

Located in/system/vold/DirectVolume. cpp. This class is a tool class that is mainly responsible for further processing of incoming events.

Block events can be divided into four types: Add, Removed, Change, and Noaction.

4. Volume

Located in/system/vold/Volume. cpp. This class is the main class responsible for SD card mounting. Volume. cpp is mainly responsible for checking the SD card format and mounting the SD card with compound requirements, and passing messages mounted to the Message SD card to nativedaemonconnevia Socket.

5. CommandListener

This class is located in/system/vold/CommandListener. cpp and communicates with nativedaemonconnethrough vold socket.

6. NativeDaemonConnector

This class is located in frameworks/base/services/java/com. android. server/NativeDaemonConnector. java. This class is used to receive SD card Mount messages from Volume. cpp and send them up.

7. MountService

It is located in frameworks/base/services/java/com. android. server/MountService. java.

MountService is a service class that provides system services for managing and querying external storage devices. When the status of an external storage device changes, the device sends a notification to the upper-layer application. This is a very important class in the Android system.

8. StorageManaer

It is located in frameworks/base/core/java/andriod/OS/storage/StorageManager. java.

As mentioned in the description of this class, this class is the interface of the system storage service. In system settings, there are Storage related items, and Setting also registers the listener for this class.

StorageManager registers its listener to MountService. Therefore, this class is mainly used by upper-layer applications to obtain the SD card status.

 

[SD card mounting process]

1. the Kernel sends the SD card to insert the uevent message.

2. NetlinkHandler: onEvent () receives and parses the uevent sent by the kernel.

3. VolumeManager: handleBlockEvent () processes the events that have been processed in step 2.

4. Call DirectVolume: handleBlockEvent ().

Note the following highlights in this method:

(1) The program first traverses the mPath container to find whether the sysfs_path corresponding to the event exists in the mPath container;

(2) There are four processing methods for actions in the event: Add, Removed, Change, and Noaction.

 

 

 

5. Call the DirectVolume: handleDiskAdded () method after the previous step. The disk insert Message is broadcasted in this method.

6. SocketListener: runListener () receives messages broadcast by DirectVolume: handleDiskAdded. This method is used to obtain the data in the event through Socket.

7. Call FrameworkListener: onDataAvailable () to process the received message content.

8. FrameworkListener: dispatchCommand () This method is used to distribute commands.

9. In the FrameworkListener: dispatchCommand () method, call the corresponding command through the runCommand () method.

10. Run Command () in/system/vold/CommandListener. cpp. In this class, we can find this method: CommandListener: VolumeCmd: runCommand (). Literally, this method is used to parse the Volume distribution command. In this method, the "mount" function is executed: vm> mountVolume (arg [2]).

11. mountVolume (arg [2]) is implemented in VolumeManager: mountVolume (). In this method, v> mountVol () is called ().

12. The mountVol () method is implemented in Volume: mountVol (). This function is a real Mount function. (In this method, subsequent processing is performed in this method. During the Mount process, the corresponding message is sent to the upper layer through the setState () function ).

13. setState (Volume: Checking) broadcast to the upper layer. Check the SD card to prepare for mounting.

14. Fat: check () whether the SD card is in the FAT format.

15. Fat: mount the SD card to doMount.

So far, the SD mounting has been completed. Next we should send the message after the SD card is mounted to the upper layer, which is also mentioned in section 13, messages are sent to the upper layer during mounting and check.

 

16. The MountService constructor will enable the listening thread to listen to socket information from vold.

Thread thread = new Thread (mConnector, VOLD_TAG) starting thread. start () Success ();

17. mConnector is the object of NativeDaemonConnector. nativedaemonconneinherits Runnable and Override the run method. Use a while (true) call in the run MethodListenToSocket() Method to implement real-time monitoring.

18. In ListenToSocket (), first establish the Socket Server that communicates with Vold, and then call the onDaemonConnected () method in MountService.

19. The onDaemonConnected () method is defined in the INativeDaemonConnectorCallbacks interface. MountService implements this interface and Override the onDaemonConnected () method. This method enables a thread to update the status of an external storage device. The main method for updating the status is also implemented.

20. Return to ListenToSocket and use inputStream to obtain the event passed by Vold. The event is stored in the queue.

21. These events are then taken out in onDaemonConnected () through the queue's "queue. take ()" method. The updatePublicVolumeState () method is called based on different events. In this method, the updateExteralState () method in packageManagerService is called to update the status of the storage device.

22. Update is implemented through the packageHelper. getMountService (). finishMediaUpdate () method.

23. In the updatePublicVolumeState () method, the following code is executed after the update:

Bl. mListener. onStorageStateChanged () Listener ();

In the Android source code/packages/apps/Settings/src/com. android. settings. deviceinfo/Memory. in java code, the anonymous internal class of StorageEventListener is implemented, and the onStorageStateChanged () method is Override. Therefore, after the onStorageStateChanged () method is called in updatePublicVolumeState (), Memory. java will also receive the message. After receiving the information in Memory. java, it will be updated on the Setting interface, and the SD card status will be updated in system settings-storage. As a result, the SD card is mounted from the underlying layer to the upper layer.

 

In the next post, I will analyze the source code of the Vold module and the MountService, including the main function, NetlinkManager, NetlinkHandler, block-type uevent processing, MountService command processing, VolumeManager, nativedaemonconneand other source code, we will meet you soon. Thank you for your support. Thank you for your comments and corrections!

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.