ANDROID: sdcardfs: fix itnull.cocci warnings
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / derived_perm.c
1 /*
2  * fs/sdcardfs/derived_perm.c
3  *
4  * Copyright (c) 2013 Samsung Electronics Co. Ltd
5  *   Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6  *               Sunghwan Yun, Sungjong Seo
7  *
8  * This program has been developed as a stackable file system based on
9  * the WrapFS which written by
10  *
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
15  *
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.
19  */
20
21 #include "sdcardfs.h"
22
23 /* copy derived state from parent inode */
24 static void inherit_derived_state(struct inode *parent, struct inode *child)
25 {
26         struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
27         struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
28
29         ci->perm = PERM_INHERIT;
30         ci->userid = pi->userid;
31         ci->d_uid = pi->d_uid;
32         ci->under_android = pi->under_android;
33 }
34
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)
38 {
39         struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
40
41         info->perm = perm;
42         info->userid = userid;
43         info->d_uid = uid;
44         info->under_android = under_android;
45 }
46
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)
49 {
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);
53         appid_t appid;
54
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
58          * its lower inode.
59          * The derived state will be updated on the last
60          * stage of each system call by fix_derived_permission(inode).
61          */
62
63         inherit_derived_state(parent->d_inode, dentry->d_inode);
64
65         /* Derive custom permissions based on parent and current node */
66         switch (parent_info->perm) {
67                 case PERM_INHERIT:
68                         /* Already inherited above */
69                         break;
70                 case PERM_PRE_ROOT:
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);
74                         break;
75                 case PERM_ROOT:
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;
81                         }
82                         break;
83                 case PERM_ANDROID:
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;
94                         }
95                         break;
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);
100                         if (appid != 0) {
101                                 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
102                         }
103                         break;
104         }
105 }
106
107 void get_derived_permission(struct dentry *parent, struct dentry *dentry)
108 {
109         get_derived_permission_new(parent, dentry, dentry);
110 }
111
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);
121                 }
122         }
123 }
124
125 /* main function for updating derived permission */
126 inline void update_derived_permission_lock(struct dentry *dentry)
127 {
128         struct dentry *parent;
129
130         if(!dentry || !dentry->d_inode) {
131                 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
132                 return;
133         }
134         /* FIXME:
135          * 1. need to check whether the dentry is updated or not
136          * 2. remove the root dentry update
137          */
138         mutex_lock(&dentry->d_inode->i_mutex);
139         if(IS_ROOT(dentry)) {
140                 //setup_default_pre_root_state(dentry->d_inode);
141         } else {
142                 parent = dget_parent(dentry);
143                 if(parent) {
144                         get_derived_permission(parent, dentry);
145                         dput(parent);
146                 }
147         }
148         fix_derived_permission(dentry->d_inode);
149         mutex_unlock(&dentry->d_inode->i_mutex);
150 }
151
152 int need_graft_path(struct dentry *dentry)
153 {
154         int ret = 0;
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);
158
159         if(parent_info->perm == PERM_ANDROID &&
160                         !strcasecmp(dentry->d_name.name, "obb")) {
161
162                 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
163                 if(!(sbi->options.multiuser == false
164                                 && parent_info->userid == 0)) {
165                         ret = 1;
166                 }
167         }
168         dput(parent);
169         return ret;
170 }
171
172 int is_obbpath_invalid(struct dentry *dent)
173 {
174         int ret = 0;
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;
178
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) {
185                         ret = 1;
186                 } else {
187                         path_get(&di->lower_path);
188                         //lower_parent = lock_parent(lower_path->dentry);
189
190                         path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
191                         if(!path_buf) {
192                                 ret = 1;
193                                 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
194                         } else {
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)) {
198                                         ret = 1;
199                                 }
200                                 kfree(path_buf);
201                         }
202
203                         //unlock_dir(lower_parent);
204                         path_put(&di->lower_path);
205                 }
206         }
207         spin_unlock(&di->lock);
208         return ret;
209 }
210
211 int is_base_obbpath(struct dentry *dentry)
212 {
213         int ret = 0;
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);
217
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")) {
222                         ret = 1;
223                 }
224         } else  if (parent_info->perm == PERM_ANDROID &&
225                         !strcasecmp(dentry->d_name.name, "obb")) {
226                 ret = 1;
227         }
228         spin_unlock(&SDCARDFS_D(dentry)->lock);
229         return ret;
230 }
231
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)
237 {
238         int err = 0;
239         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
240         struct path obbpath;
241
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);
246
247         err = kern_path(sbi->obbpath_s,
248                         LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
249
250         if(!err) {
251                 /* the obbpath base has been found */
252                 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
253                 pathcpy(lower_path, &obbpath);
254         } else {
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
259                  * a lookup fail. */
260                 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");
261         }
262         return err;
263 }
264
265