blob: bcc6cce47d6671a9cde016ba10afa7acb6a156a4 [file] [log] [blame]
Clay Murphyf9d451e2014-10-21 18:11:12 -07001page.title=External Storage
Robert Ly35f2fda2013-01-29 16:27:05 -08002@jd:body
3
4<!--
Heidi von Markham1e7b8b72015-03-09 10:13:48 -07005 Copyright 2015 The Android Open Source Project
Robert Ly35f2fda2013-01-29 16:27:05 -08006
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
Ken Sumrall93c0b9c2013-04-16 15:43:27 -070019
Heidi von Markham1e7b8b72015-03-09 10:13:48 -070020<img style="float: right; margin: 0px 15px 15px 0px;" src="images/ape_fwk_hal_extstor.png" alt="Android external storage HAL icon" width="175" />
Jeff Sharkey790c02d2013-10-18 13:57:33 -070021
22<p>Android supports devices with external storage, which is defined to be a
23case-insensitive filesystem with immutable POSIX permission classes and
24modes. External storage can be provided by physical media (such as an SD
25card), or by exposing a portion of internal storage through an emulation
26layer. Devices may contain multiple instances of external storage.</p>
27
28<p>Access to external storage is protected by various Android
29permissions. Starting in Android 1.0, write access is protected with the
30<code>WRITE_EXTERNAL_STORAGE</code> permission. Starting in Android 4.1,
31read access is protected with the <code>READ_EXTERNAL_STORAGE</code>
32permission.</p>
33
34<p>Starting in Android 4.4, the owner, group and modes of files on external
35storage devices are now synthesized based on directory structure. This
36enables apps to manage their package-specific directories on external
37storage without requiring they hold the broad
38<code>WRITE_EXTERNAL_STORAGE</code> permission. For example, the app with
39package name <code>com.example.foo</code> can now freely access
40<code>Android/data/com.example.foo/</code> on external storage devices with
41no permissions. These synthesized permissions are accomplished by wrapping
42raw storage devices in a FUSE daemon.</p>
43
44<p>Since external storage offers minimal protection for stored data, system
45code should not store sensitive data on external storage. Specifically,
46configuration and log files should only be stored on internal storage where
47they can be effectively protected.</p>
48
49
50<h2 id="multiple-external-storage-devices">Multiple external storage devices</h2>
51
52<p>Starting in Android 4.4, multiple external storage devices are surfaced
53to developers through <code>Context.getExternalFilesDirs()</code>,
54<code>Context.getExternalCacheDirs()</code>, and
55<code>Context.getObbDirs()</code>.</p>
56
57</p>External storage devices surfaced through these APIs must be a
58semi-permanent part of the device (such as an SD card slot in a battery
59compartment). Developers expect data stored in these locations to be
60available over long periods of time. For this reason, transient storage
61devices (such as USB mass storage drives) should not be surfaced through
62these APIs.</p>
63
64<p>The <code>WRITE_EXTERNAL_STORAGE</code> permission must only grant write
65access to the primary external storage on a device. Apps must not be
66allowed to write to secondary external storage devices, except in their
67package-specific directories as allowed by synthesized
68permissions. Restricting writes in this way ensures the system can clean
69up files when applications are uninstalled.</p>
70
71
Robert Ly35f2fda2013-01-29 16:27:05 -080072<h2 id="multi-user-external-storage">Multi-user external storage</h2>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070073
Robert Ly35f2fda2013-01-29 16:27:05 -080074<p>Starting in Android 4.2, devices can support multiple users, and external
75storage must meet the following constraints:</p>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070076
Robert Ly35f2fda2013-01-29 16:27:05 -080077<ul>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070078<li>Each user must have their own isolated primary external storage, and
79must not have access to the primary external storage of other users.</li>
80<li>The <code>/sdcard</code> path must resolve to the correct user-specific
81primary external storage based on the user a process is running as.</li>
82<li>Storage for large OBB files in the <code>Android/obb</code> directory
83may be shared between multiple users as an optimization.</li>
84<li>Secondary external storage must not be writable by apps, except in
85package-specific directories as allowed by synthesized permissions.</li>
Robert Ly35f2fda2013-01-29 16:27:05 -080086</ul>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070087
Robert Ly35f2fda2013-01-29 16:27:05 -080088<p>The default platform implementation of this feature leverages Linux kernel
Jeff Sharkey790c02d2013-10-18 13:57:33 -070089namespaces to create isolated mount tables for each Zygote-forked process,
90and then uses bind mounts to offer the correct user-specific primary external
Robert Ly35f2fda2013-01-29 16:27:05 -080091storage into that private namespace.</p>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070092
93<p>At boot, the system mounts a single emulated external storage FUSE daemon
94at <code>EMULATED_STORAGE_SOURCE</code>, which is hidden from apps. After
95the Zygote forks, it bind mounts the appropriate user-specific subdirectory
96from under the FUSE daemon to <code>EMULATED_STORAGE_TARGET</code> so that
97external storage paths resolve correctly for the app. Because an app lacks
98accessible mount points for other users' storage, they can only access
99storage for the user it was started as.</p>
100
101<p>This implementation also uses the shared subtree kernel feature to
102propagate mount events from the default root namespace into app namespaces,
103which ensures that features like ASEC containers and OBB mounting continue
104working correctly. It does this by mounting the rootfs as shared, and then
105remounting it as slave after each Zygote namespace is created.</p>