Fix for empty app-mounted directories (CifsManager, etc.) in Android 4.2

470 posts
Thanks Meter: 834
By mkasick, Retired Recognized Developer on 18th January 2013, 10:32 PM
Post Reply Email Thread
Android 4.2 breaks Dalvik-apps that mount file systems to be shared with other apps. This includes CifsManager, Mount Manager, essentially anything that mounts cifs shares, FUSE file sytems, etc. The symptom is that the mounted contents appear fine to app that peforms the mount operation (assuming the app itself provides the ability to browse the contents), but every other app only sees an empty directory at the mount point.

It turns out that this problem is a side-effect of the approach used to implement multi-user storage in Android 4.2. I've explained the problem in detail in the commit log for a Gerrit issue we're reviewing for CyanogenMod 10.1 that addresses the problem:
Originally Posted by Zygote patch commit message

Zygote: Restrict slave mountspace so Dalvik apps can mount system-wide volumes

Android 4.2 implements multi-user storage using per-process mount namespaces. Originally, everything under "/" (the entire filesystem hierarchy) is marked as a recursive-slave mountspace for all zygote instances. This is done so that user-storage sandbox mounts under /storage/emulated are hidden from other apps and users.

Unfortunately this means that any Dalvik app (actually, any program whose clone/fork ancestry includes a Dalvik zygote, which is everything except services spawned directly from init) cannot mount system-wide volumes. Thus, apps like CifsManager are effectively broken in Android 4.2, since its cifs mounts are only visible to the CifsManager app itself. All other apps see empty mountpoints instead of the mounted volume. Furthermore, Linux provides no provision for a process to "escape" a recursive-slave mountspace in versions prior to Linux 3.8 (setns syscall).

Here, we restrict the slave mountspace to /storage (and, due to a possible kernel bug, /mnt/shell/emulated) so that Dalvik apps can mount system-wide volumes elsewhere (with appropriate permission, as in earlier versions of Android), while retaining full multi-user storage compatibility.

This change requires that a tmpfs volume is mounted as /storage in init.rc. If this volume is unavailable, then Zygote falls back to the previous behavior of marking the entire filesystem hierarchy as slave. It also implicitly requires that EMULATED_STORAGE_TARGET is path-prefixed by (part of the subhierarchy of) ANDROID_STORAGE, which is the typical case.

Ideally, any 4.2 ROM can provide support for CifsManager by applying a change to Dalvik, and a second change to the boot ramdisk's init.rc:
Alternatively, ROMs that can't/prefer not to use a source-build Dalvik (libdvm), I've also provided a kernel patch that implements a similar workaround within the kernel. It also requires the above init.rc modification:
With either of the above two fixes, CifsManager et al. should work when using a mountpoint outside /storage (and /mnt/shell/emulated). I'd recommend using "/mnt/cifs" or something similar. Attempting to mount inside /storage retains the previous behavior where the mount can not be seen by other apps.

Note that ROMs only need one of the above two fixes, although they are compatible with each other and will function correctly if both are present. The Dalvik approach is preferred over the kernel workaround where feasible. Attached are the three patches referenced in the issues/commits.
Attached Files
File Type: diff dalvik.diff - [Click for QR Code] (5.9 KB, 1796 views)
File Type: diff initrc.diff - [Click for QR Code] (1.1 KB, 1253 views)
File Type: diff kernel.diff - [Click for QR Code] (5.3 KB, 1021 views)
The Following 44 Users Say Thank You to mkasick For This Useful Post: [ View ] Gift mkasick Ad-Free
19th January 2013, 11:18 AM |#2  
Thanks Meter: 7
So is it possible, for a noob like me, to get it working wihtout learning all that coding stuff etc.?
19th January 2013, 11:59 AM |#3  
vorcers's Avatar
Senior Member
Thanks Meter: 127
I wanted to know if it really works - and it works!

I created a Nexus 7 wifi kernel and shared it. Thank you so much!
The Following User Says Thank You to vorcers For This Useful Post: [ View ] Gift vorcers Ad-Free
19th January 2013, 01:41 PM |#4  
Senior Member
Flag Worcester
Thanks Meter: 36
Re: Fix for empty app-mounted directories (CifsManager, etc.) in Android 4.2
Can anyone noobproof this into a flash able zip?

PA is perfect for me except for no cifs.

Sent from my Nexus 7 using Tapatalk HD
19th January 2013, 02:23 PM |#5  
timduru's Avatar
Senior Member
Thanks Meter: 18,013
Donate to Me
dalvik patch works great on TF101
I've integrated it into TeamEOS4 rom.
many thanks !
The Following 2 Users Say Thank You to timduru For This Useful Post: [ View ] Gift timduru Ad-Free
20th January 2013, 10:33 AM |#6  
Senior Member
Thanks Meter: 2,462
It would be really awesome if a fix or hack to this issue (empty directories if app is not launched before mounting) could be found without requiring a kernel patch.

I tried the debuggerd method supposedly used by StickMount and found that it didn't work: app still sees empty mount if its process is started after mount.

Btw, app sees mounts if its process is started before because mounts are propagated in that case. They are just not inherited on process creation due to how zygote spawn app processes and initializes their mounts.

I never understood how hacking debuggerd could work to fix this issue as this is not much different to me than running a shell script invoking adb shell in loopback mode (requires root), since debuggerd and adbd run with identical priviledges:

setprop service.adb.tcp.port 5555
stop adbd
start adbd
adb connect localhost
adb shell mount ....
I've found a workaround that involves hijaking the obb directory by mounting whatever you want over /mnt/shell/emulated/obb. This directory is systematically bind mounted by the Zygote process in <external storage root>/Android/obb (see source code) when it spawns an app's process

Unfortunately this hack is not very usable because it has the side effect that any app expecting its data in the obb directory will not find it. And you can only mount one filesystem here.

Conclusion: with the new 4.2 multiuser support, Google made miserable all apps dealing with mounting.
The Following 2 Users Say Thank You to bubbleguuum For This Useful Post: [ View ] Gift bubbleguuum Ad-Free
20th January 2013, 02:29 PM |#7  
pjsports's Avatar
Thanks Meter: 515
kernel patch works on acer picasso
many thanks !
21st January 2013, 07:42 AM |#8  
ayysir's Avatar
Senior Member
Flag Bronx, NY
Thanks Meter: 6,232
Donate to Me
Added the patch on ramdisk and kernel level,

will compile and try this out!! thanks man!
21st January 2013, 08:04 AM |#9  
xdadevnube's Avatar
Senior Member
Thanks Meter: 157
Just found this thread. This is seriously sweet! I had to roll back to 4.1.2 ROMs on the Nexus 7 because of the CIFS issue. Thanks for all the hard work creating this fix.
21st January 2013, 11:03 AM |#10  
munhoz's Avatar
Senior Member
Thanks Meter: 60
Seems like it's Merged on CM 10.1, im right ? its says "Merged" on github. It happened today. Maybe on tomorrow nightly
21st January 2013, 12:37 PM |#11  
Senior Member
Flag Lisboa
Thanks Meter: 19
Originally Posted by vanzano

Seems like it's Merged on CM 10.1, im right ? its says "Merged" on github. It happened today. Maybe on tomorrow nightly

I do hope so, as I have spent a few weeks already trying to find some "workaround" for this issue... If not maybe I'll try to merge it myself...
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes