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;
35 /* helper function for derived state */
36 void setup_derived_state(struct inode *inode, perm_t perm,
37 userid_t userid, uid_t uid, bool under_android)
39 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
42 info->userid = userid;
44 info->under_android = under_android;
47 /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
48 void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry)
50 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
51 struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
52 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
55 /* By default, each inode inherits from its parent.
56 * the properties are maintained on its private fields
57 * because the inode attributes will be modified with that of
59 * The derived state will be updated on the last
60 * stage of each system call by fix_derived_permission(inode).
63 inherit_derived_state(parent->d_inode, dentry->d_inode);
65 /* Derive custom permissions based on parent and current node */
66 switch (parent_info->perm) {
68 /* Already inherited above */
71 /* Legacy internal layout places users at top level */
72 info->perm = PERM_ROOT;
73 info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10);
76 /* Assume masked off by default. */
77 if (!strcasecmp(newdentry->d_name.name, "Android")) {
78 /* App-specific directories inside; let anyone traverse */
79 info->perm = PERM_ANDROID;
80 info->under_android = true;
84 if (!strcasecmp(newdentry->d_name.name, "data")) {
85 /* App-specific directories inside; let anyone traverse */
86 info->perm = PERM_ANDROID_DATA;
87 } else if (!strcasecmp(newdentry->d_name.name, "obb")) {
88 /* App-specific directories inside; let anyone traverse */
89 info->perm = PERM_ANDROID_OBB;
90 /* Single OBB directory is always shared */
91 } else if (!strcasecmp(newdentry->d_name.name, "media")) {
92 /* App-specific directories inside; let anyone traverse */
93 info->perm = PERM_ANDROID_MEDIA;
96 case PERM_ANDROID_DATA:
97 case PERM_ANDROID_OBB:
98 case PERM_ANDROID_MEDIA:
99 appid = get_appid(sbi->pkgl_id, newdentry->d_name.name);
101 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
107 void get_derived_permission(struct dentry *parent, struct dentry *dentry)
109 get_derived_permission_new(parent, dentry, dentry);
112 void get_derive_permissions_recursive(struct dentry *parent) {
113 struct dentry *dentry;
114 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
115 if (dentry->d_inode) {
116 mutex_lock(&dentry->d_inode->i_mutex);
117 get_derived_permission(parent, dentry);
118 fix_derived_permission(dentry->d_inode);
119 get_derive_permissions_recursive(dentry);
120 mutex_unlock(&dentry->d_inode->i_mutex);
125 /* main function for updating derived permission */
126 inline void update_derived_permission_lock(struct dentry *dentry)
128 struct dentry *parent;
130 if(!dentry || !dentry->d_inode) {
131 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
135 * 1. need to check whether the dentry is updated or not
136 * 2. remove the root dentry update
138 mutex_lock(&dentry->d_inode->i_mutex);
139 if(IS_ROOT(dentry)) {
140 //setup_default_pre_root_state(dentry->d_inode);
142 parent = dget_parent(dentry);
144 get_derived_permission(parent, dentry);
148 fix_derived_permission(dentry->d_inode);
149 mutex_unlock(&dentry->d_inode->i_mutex);
152 int need_graft_path(struct dentry *dentry)
155 struct dentry *parent = dget_parent(dentry);
156 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
157 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
159 if(parent_info->perm == PERM_ANDROID &&
160 !strcasecmp(dentry->d_name.name, "obb")) {
162 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
163 if(!(sbi->options.multiuser == false
164 && parent_info->userid == 0)) {
172 int is_obbpath_invalid(struct dentry *dent)
175 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
176 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
177 char *path_buf, *obbpath_s;
179 /* check the base obbpath has been changed.
180 * this routine can check an uninitialized obb dentry as well.
181 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
182 spin_lock(&di->lock);
183 if(di->orig_path.dentry) {
184 if(!di->lower_path.dentry) {
187 path_get(&di->lower_path);
188 //lower_parent = lock_parent(lower_path->dentry);
190 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
193 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
195 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
196 if (d_unhashed(di->lower_path.dentry) ||
197 strcasecmp(sbi->obbpath_s, obbpath_s)) {
203 //unlock_dir(lower_parent);
204 path_put(&di->lower_path);
207 spin_unlock(&di->lock);
211 int is_base_obbpath(struct dentry *dentry)
214 struct dentry *parent = dget_parent(dentry);
215 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
216 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
218 spin_lock(&SDCARDFS_D(dentry)->lock);
219 if (sbi->options.multiuser) {
220 if(parent_info->perm == PERM_PRE_ROOT &&
221 !strcasecmp(dentry->d_name.name, "obb")) {
224 } else if (parent_info->perm == PERM_ANDROID &&
225 !strcasecmp(dentry->d_name.name, "obb")) {
228 spin_unlock(&SDCARDFS_D(dentry)->lock);
232 /* The lower_path will be stored to the dentry's orig_path
233 * and the base obbpath will be copyed to the lower_path variable.
234 * if an error returned, there's no change in the lower_path
235 * returns: -ERRNO if error (0: no error) */
236 int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
239 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
242 /* A local obb dentry must have its own orig_path to support rmdir
243 * and mkdir of itself. Usually, we expect that the sbi->obbpath
244 * is avaiable on this stage. */
245 sdcardfs_set_orig_path(dentry, lower_path);
247 err = kern_path(sbi->obbpath_s,
248 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
251 /* the obbpath base has been found */
252 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
253 pathcpy(lower_path, &obbpath);
255 /* if the sbi->obbpath is not available, we can optionally
256 * setup the lower_path with its orig_path.
257 * but, the current implementation just returns an error
258 * because the sdcard daemon also regards this case as
260 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");