blob: 894e85b9a6eac005ad726f254c28dd63d264405e [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-->
Clay Murphy1d5f8fe2015-05-18 16:44:07 -070019<div id="qv-wrapper">
20 <div id="qv">
21 <h2>In this document</h2>
22 <ol id="auto-toc">
23 </ol>
24 </div>
25</div>
Ken Sumrall93c0b9c2013-04-16 15:43:27 -070026
Heidi von Markhamb493fb62015-03-25 12:35:11 -070027<img style="float: right; margin: 0px 15px 15px 15px;" src="images/ape_fwk_hal_extstor.png" alt="Android external storage HAL icon"/>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070028
29<p>Android supports devices with external storage, which is defined to be a
30case-insensitive filesystem with immutable POSIX permission classes and
31modes. External storage can be provided by physical media (such as an SD
32card), or by exposing a portion of internal storage through an emulation
33layer. Devices may contain multiple instances of external storage.</p>
34
35<p>Access to external storage is protected by various Android
36permissions. Starting in Android 1.0, write access is protected with the
37<code>WRITE_EXTERNAL_STORAGE</code> permission. Starting in Android 4.1,
38read access is protected with the <code>READ_EXTERNAL_STORAGE</code>
39permission.</p>
40
41<p>Starting in Android 4.4, the owner, group and modes of files on external
42storage devices are now synthesized based on directory structure. This
43enables apps to manage their package-specific directories on external
44storage without requiring they hold the broad
45<code>WRITE_EXTERNAL_STORAGE</code> permission. For example, the app with
46package name <code>com.example.foo</code> can now freely access
47<code>Android/data/com.example.foo/</code> on external storage devices with
48no permissions. These synthesized permissions are accomplished by wrapping
49raw storage devices in a FUSE daemon.</p>
50
51<p>Since external storage offers minimal protection for stored data, system
52code should not store sensitive data on external storage. Specifically,
53configuration and log files should only be stored on internal storage where
54they can be effectively protected.</p>
55
56
57<h2 id="multiple-external-storage-devices">Multiple external storage devices</h2>
58
59<p>Starting in Android 4.4, multiple external storage devices are surfaced
60to developers through <code>Context.getExternalFilesDirs()</code>,
61<code>Context.getExternalCacheDirs()</code>, and
62<code>Context.getObbDirs()</code>.</p>
63
64</p>External storage devices surfaced through these APIs must be a
65semi-permanent part of the device (such as an SD card slot in a battery
66compartment). Developers expect data stored in these locations to be
67available over long periods of time. For this reason, transient storage
68devices (such as USB mass storage drives) should not be surfaced through
69these APIs.</p>
70
71<p>The <code>WRITE_EXTERNAL_STORAGE</code> permission must only grant write
72access to the primary external storage on a device. Apps must not be
73allowed to write to secondary external storage devices, except in their
74package-specific directories as allowed by synthesized
75permissions. Restricting writes in this way ensures the system can clean
76up files when applications are uninstalled.</p>
77
78
Robert Ly35f2fda2013-01-29 16:27:05 -080079<h2 id="multi-user-external-storage">Multi-user external storage</h2>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070080
Robert Ly35f2fda2013-01-29 16:27:05 -080081<p>Starting in Android 4.2, devices can support multiple users, and external
82storage must meet the following constraints:</p>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070083
Robert Ly35f2fda2013-01-29 16:27:05 -080084<ul>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070085<li>Each user must have their own isolated primary external storage, and
86must not have access to the primary external storage of other users.</li>
87<li>The <code>/sdcard</code> path must resolve to the correct user-specific
88primary external storage based on the user a process is running as.</li>
89<li>Storage for large OBB files in the <code>Android/obb</code> directory
90may be shared between multiple users as an optimization.</li>
91<li>Secondary external storage must not be writable by apps, except in
92package-specific directories as allowed by synthesized permissions.</li>
Robert Ly35f2fda2013-01-29 16:27:05 -080093</ul>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070094
Robert Ly35f2fda2013-01-29 16:27:05 -080095<p>The default platform implementation of this feature leverages Linux kernel
Jeff Sharkey790c02d2013-10-18 13:57:33 -070096namespaces to create isolated mount tables for each Zygote-forked process,
97and then uses bind mounts to offer the correct user-specific primary external
Robert Ly35f2fda2013-01-29 16:27:05 -080098storage into that private namespace.</p>
Jeff Sharkey790c02d2013-10-18 13:57:33 -070099
100<p>At boot, the system mounts a single emulated external storage FUSE daemon
101at <code>EMULATED_STORAGE_SOURCE</code>, which is hidden from apps. After
102the Zygote forks, it bind mounts the appropriate user-specific subdirectory
103from under the FUSE daemon to <code>EMULATED_STORAGE_TARGET</code> so that
104external storage paths resolve correctly for the app. Because an app lacks
105accessible mount points for other users' storage, they can only access
106storage for the user it was started as.</p>
107
108<p>This implementation also uses the shared subtree kernel feature to
109propagate mount events from the default root namespace into app namespaces,
110which ensures that features like ASEC containers and OBB mounting continue
111working correctly. It does this by mounting the rootfs as shared, and then
112remounting it as slave after each Zygote namespace is created.</p>