implement write

Also fix the permission checking function.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>