2 * fs/sdcardfs/derived_perm.c
4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
11 * Copyright (c) 1998-2011 Erez Zadok
12 * Copyright (c) 2009 Shrikar Archak
13 * Copyright (c) 2003-2011 Stony Brook University
14 * Copyright (c) 2003-2011 The Research Foundation of SUNY
16 * This file is dual licensed. It may be redistributed and/or modified
17 * under the terms of the Apache 2.0 License OR version 2 of the GNU
18 * General Public License.
23 /* copy derived state from parent inode */
24 static void inherit_derived_state(struct inode *parent, struct inode *child)
26 struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
27 struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
29 ci->perm = PERM_INHERIT;
30 ci->userid = pi->userid;
31 ci->d_uid = pi->d_uid;
32 ci->under_android = pi->under_android;
36 /* helper function for derived state */
37 void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
38 uid_t uid, bool under_android, struct inode *top)
40 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
43 info->userid = userid;
45 info->under_android = under_android;
49 /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
50 void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry)
52 struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
53 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
56 /* By default, each inode inherits from its parent.
57 * the properties are maintained on its private fields
58 * because the inode attributes will be modified with that of
60 * The derived state will be updated on the last
61 * stage of each system call by fix_derived_permission(inode).
64 inherit_derived_state(d_inode(parent), d_inode(dentry));
66 /* Derive custom permissions based on parent and current node */
67 switch (parent_info->perm) {
69 /* Already inherited above */
72 /* Legacy internal layout places users at top level */
73 info->perm = PERM_ROOT;
74 info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10);
75 set_top(info, &info->vfs_inode);
78 /* Assume masked off by default. */
79 if (!strcasecmp(newdentry->d_name.name, "Android")) {
80 /* App-specific directories inside; let anyone traverse */
81 info->perm = PERM_ANDROID;
82 info->under_android = true;
83 set_top(info, &info->vfs_inode);
87 if (!strcasecmp(newdentry->d_name.name, "data")) {
88 /* App-specific directories inside; let anyone traverse */
89 info->perm = PERM_ANDROID_DATA;
90 set_top(info, &info->vfs_inode);
91 } else if (!strcasecmp(newdentry->d_name.name, "obb")) {
92 /* App-specific directories inside; let anyone traverse */
93 info->perm = PERM_ANDROID_OBB;
94 set_top(info, &info->vfs_inode);
95 /* Single OBB directory is always shared */
96 } else if (!strcasecmp(newdentry->d_name.name, "media")) {
97 /* App-specific directories inside; let anyone traverse */
98 info->perm = PERM_ANDROID_MEDIA;
99 set_top(info, &info->vfs_inode);
102 case PERM_ANDROID_DATA:
103 case PERM_ANDROID_OBB:
104 case PERM_ANDROID_MEDIA:
105 appid = get_appid(newdentry->d_name.name);
107 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
109 set_top(info, &info->vfs_inode);
114 void get_derived_permission(struct dentry *parent, struct dentry *dentry)
116 get_derived_permission_new(parent, dentry, dentry);
119 static int descendant_may_need_fixup(perm_t perm) {
120 if (perm == PERM_PRE_ROOT || perm == PERM_ROOT || perm == PERM_ANDROID)
125 static int needs_fixup(perm_t perm) {
126 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
127 || perm == PERM_ANDROID_MEDIA)
132 void fixup_perms_recursive(struct dentry *dentry, const char* name, size_t len) {
133 struct dentry *child;
134 struct sdcardfs_inode_info *info;
137 if (!d_inode(dentry)) {
141 info = SDCARDFS_I(d_inode(dentry));
143 if (needs_fixup(info->perm)) {
144 spin_lock(&dentry->d_lock);
145 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
147 if (!strncasecmp(child->d_name.name, name, len)) {
148 if (d_inode(child)) {
149 get_derived_permission(dentry, child);
150 fixup_tmp_permissions(d_inode(child));
157 spin_unlock(&dentry->d_lock);
158 } else if (descendant_may_need_fixup(info->perm)) {
159 spin_lock(&dentry->d_lock);
160 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
161 fixup_perms_recursive(child, name, len);
163 spin_unlock(&dentry->d_lock);
168 void fixup_top_recursive(struct dentry *parent) {
169 struct dentry *dentry;
170 struct sdcardfs_inode_info *info;
171 if (!d_inode(parent))
173 info = SDCARDFS_I(d_inode(parent));
174 spin_lock(&parent->d_lock);
175 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
176 if (d_inode(dentry)) {
177 if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) {
178 get_derived_permission(parent, dentry);
179 fixup_tmp_permissions(d_inode(dentry));
180 fixup_top_recursive(dentry);
184 spin_unlock(&parent->d_lock);
187 /* main function for updating derived permission */
188 inline void update_derived_permission_lock(struct dentry *dentry)
190 struct dentry *parent;
192 if(!dentry || !d_inode(dentry)) {
193 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
197 * 1. need to check whether the dentry is updated or not
198 * 2. remove the root dentry update
200 if(IS_ROOT(dentry)) {
201 //setup_default_pre_root_state(d_inode(dentry));
203 parent = dget_parent(dentry);
205 get_derived_permission(parent, dentry);
209 fixup_tmp_permissions(d_inode(dentry));
212 int need_graft_path(struct dentry *dentry)
215 struct dentry *parent = dget_parent(dentry);
216 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
217 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
219 if(parent_info->perm == PERM_ANDROID &&
220 !strcasecmp(dentry->d_name.name, "obb")) {
222 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
223 if(!(sbi->options.multiuser == false
224 && parent_info->userid == 0)) {
232 int is_obbpath_invalid(struct dentry *dent)
235 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
236 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
237 char *path_buf, *obbpath_s;
239 /* check the base obbpath has been changed.
240 * this routine can check an uninitialized obb dentry as well.
241 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
242 spin_lock(&di->lock);
243 if(di->orig_path.dentry) {
244 if(!di->lower_path.dentry) {
247 path_get(&di->lower_path);
248 //lower_parent = lock_parent(lower_path->dentry);
250 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
253 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
255 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
256 if (d_unhashed(di->lower_path.dentry) ||
257 strcasecmp(sbi->obbpath_s, obbpath_s)) {
263 //unlock_dir(lower_parent);
264 path_put(&di->lower_path);
267 spin_unlock(&di->lock);
271 int is_base_obbpath(struct dentry *dentry)
274 struct dentry *parent = dget_parent(dentry);
275 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
276 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
278 spin_lock(&SDCARDFS_D(dentry)->lock);
279 if (sbi->options.multiuser) {
280 if(parent_info->perm == PERM_PRE_ROOT &&
281 !strcasecmp(dentry->d_name.name, "obb")) {
284 } else if (parent_info->perm == PERM_ANDROID &&
285 !strcasecmp(dentry->d_name.name, "obb")) {
288 spin_unlock(&SDCARDFS_D(dentry)->lock);
292 /* The lower_path will be stored to the dentry's orig_path
293 * and the base obbpath will be copyed to the lower_path variable.
294 * if an error returned, there's no change in the lower_path
295 * returns: -ERRNO if error (0: no error) */
296 int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
299 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
302 /* A local obb dentry must have its own orig_path to support rmdir
303 * and mkdir of itself. Usually, we expect that the sbi->obbpath
304 * is avaiable on this stage. */
305 sdcardfs_set_orig_path(dentry, lower_path);
307 err = kern_path(sbi->obbpath_s,
308 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
311 /* the obbpath base has been found */
312 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
313 pathcpy(lower_path, &obbpath);
315 /* if the sbi->obbpath is not available, we can optionally
316 * setup the lower_path with its orig_path.
317 * but, the current implementation just returns an error
318 * because the sdcard daemon also regards this case as
320 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");