blob: 0b3b22334e54a27c09eb4f3e6e3008c16701202a [file] [log] [blame]
Daniel Campello35c9e242015-07-20 16:23:50 -07001/*
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 */
24static 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
Daniel Rosenberga56a1052017-05-15 14:03:15 -070029 ci->data->perm = PERM_INHERIT;
30 ci->data->userid = pi->data->userid;
31 ci->data->d_uid = pi->data->d_uid;
32 ci->data->under_android = pi->data->under_android;
33 ci->data->under_cache = pi->data->under_cache;
34 ci->data->under_obb = pi->data->under_obb;
Daniel Campello35c9e242015-07-20 16:23:50 -070035}
36
37/* helper function for derived state */
Daniel Rosenberg5080d242016-05-18 16:57:10 -070038void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080039 uid_t uid)
Daniel Campello35c9e242015-07-20 16:23:50 -070040{
41 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
42
Daniel Rosenberga56a1052017-05-15 14:03:15 -070043 info->data->perm = perm;
44 info->data->userid = userid;
45 info->data->d_uid = uid;
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080046 info->data->under_android = false;
Daniel Rosenberga56a1052017-05-15 14:03:15 -070047 info->data->under_cache = false;
48 info->data->under_obb = false;
Daniel Campello35c9e242015-07-20 16:23:50 -070049}
50
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070051/* While renaming, there is a point where we want the path from dentry,
52 * but the name from newdentry
53 */
54void get_derived_permission_new(struct dentry *parent, struct dentry *dentry,
55 const struct qstr *name)
Daniel Campello35c9e242015-07-20 16:23:50 -070056{
Daniel Rosenberg63d20762016-12-01 14:36:29 -080057 struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080058 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
59 struct sdcardfs_inode_data *parent_data = parent_info->data;
Daniel Campello35c9e242015-07-20 16:23:50 -070060 appid_t appid;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070061 unsigned long user_num;
62 int err;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -080063 struct qstr q_Android = QSTR_LITERAL("Android");
64 struct qstr q_data = QSTR_LITERAL("data");
65 struct qstr q_obb = QSTR_LITERAL("obb");
66 struct qstr q_media = QSTR_LITERAL("media");
67 struct qstr q_cache = QSTR_LITERAL("cache");
Daniel Campello35c9e242015-07-20 16:23:50 -070068
69 /* By default, each inode inherits from its parent.
70 * the properties are maintained on its private fields
71 * because the inode attributes will be modified with that of
72 * its lower inode.
Daniel Rosenbergad905252017-01-25 13:48:45 -080073 * These values are used by our custom permission call instead
74 * of using the inode permissions.
Daniel Campello35c9e242015-07-20 16:23:50 -070075 */
76
Daniel Rosenberg63d20762016-12-01 14:36:29 -080077 inherit_derived_state(d_inode(parent), d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -070078
Daniel Rosenbergad905252017-01-25 13:48:45 -080079 /* Files don't get special labels */
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080080 if (!S_ISDIR(d_inode(dentry)->i_mode)) {
81 set_top(info, parent_info);
Daniel Rosenbergad905252017-01-25 13:48:45 -080082 return;
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080083 }
Daniel Campello35c9e242015-07-20 16:23:50 -070084 /* Derive custom permissions based on parent and current node */
Daniel Rosenberga56a1052017-05-15 14:03:15 -070085 switch (parent_data->perm) {
Daniel Rosenbergad905252017-01-25 13:48:45 -080086 case PERM_INHERIT:
87 case PERM_ANDROID_PACKAGE_CACHE:
Daniel Rosenberg47af77b2018-02-01 16:52:22 -080088 set_top(info, parent_info);
Daniel Rosenbergad905252017-01-25 13:48:45 -080089 break;
90 case PERM_PRE_ROOT:
91 /* Legacy internal layout places users at top level */
Daniel Rosenberga56a1052017-05-15 14:03:15 -070092 info->data->perm = PERM_ROOT;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070093 err = kstrtoul(name->name, 10, &user_num);
94 if (err)
Daniel Rosenberga56a1052017-05-15 14:03:15 -070095 info->data->userid = 0;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070096 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -070097 info->data->userid = user_num;
Daniel Rosenbergad905252017-01-25 13:48:45 -080098 break;
99 case PERM_ROOT:
100 /* Assume masked off by default. */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800101 if (qstr_case_eq(name, &q_Android)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800102 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700103 info->data->perm = PERM_ANDROID;
104 info->data->under_android = true;
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800105 } else {
106 set_top(info, parent_info);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800107 }
108 break;
109 case PERM_ANDROID:
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800110 if (qstr_case_eq(name, &q_data)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800111 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700112 info->data->perm = PERM_ANDROID_DATA;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800113 } else if (qstr_case_eq(name, &q_obb)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800114 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700115 info->data->perm = PERM_ANDROID_OBB;
116 info->data->under_obb = true;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800117 /* Single OBB directory is always shared */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800118 } else if (qstr_case_eq(name, &q_media)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800119 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700120 info->data->perm = PERM_ANDROID_MEDIA;
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800121 } else {
122 set_top(info, parent_info);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800123 }
124 break;
125 case PERM_ANDROID_OBB:
126 case PERM_ANDROID_DATA:
127 case PERM_ANDROID_MEDIA:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700128 info->data->perm = PERM_ANDROID_PACKAGE;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800129 appid = get_appid(name->name);
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700130 if (appid != 0 && !is_excluded(name->name, parent_data->userid))
131 info->data->d_uid =
132 multiuser_get_uid(parent_data->userid, appid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800133 break;
134 case PERM_ANDROID_PACKAGE:
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800135 if (qstr_case_eq(name, &q_cache)) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700136 info->data->perm = PERM_ANDROID_PACKAGE_CACHE;
137 info->data->under_cache = true;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800138 }
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800139 set_top(info, parent_info);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800140 break;
Daniel Campello35c9e242015-07-20 16:23:50 -0700141 }
142}
143
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800144void get_derived_permission(struct dentry *parent, struct dentry *dentry)
145{
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800146 get_derived_permission_new(parent, dentry, &dentry->d_name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800147}
148
149static appid_t get_type(const char *name)
150{
151 const char *ext = strrchr(name, '.');
152 appid_t id;
153
154 if (ext && ext[0]) {
155 ext = &ext[1];
156 id = get_ext_gid(ext);
157 return id?:AID_MEDIA_RW;
158 }
159 return AID_MEDIA_RW;
160}
161
162void fixup_lower_ownership(struct dentry *dentry, const char *name)
163{
164 struct path path;
165 struct inode *inode;
166 struct inode *delegated_inode = NULL;
167 int error;
168 struct sdcardfs_inode_info *info;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700169 struct sdcardfs_inode_data *info_d;
170 struct sdcardfs_inode_data *info_top;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800171 perm_t perm;
172 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
173 uid_t uid = sbi->options.fs_low_uid;
174 gid_t gid = sbi->options.fs_low_gid;
175 struct iattr newattrs;
176
Daniel Rosenberg7d10e432017-07-19 17:25:07 -0700177 if (!sbi->options.gid_derivation)
178 return;
179
Daniel Rosenbergad905252017-01-25 13:48:45 -0800180 info = SDCARDFS_I(d_inode(dentry));
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700181 info_d = info->data;
182 perm = info_d->perm;
183 if (info_d->under_obb) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800184 perm = PERM_ANDROID_OBB;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700185 } else if (info_d->under_cache) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800186 perm = PERM_ANDROID_PACKAGE_CACHE;
187 } else if (perm == PERM_INHERIT) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700188 info_top = top_data_get(info);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800189 perm = info_top->perm;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700190 data_put(info_top);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800191 }
192
193 switch (perm) {
194 case PERM_ROOT:
195 case PERM_ANDROID:
196 case PERM_ANDROID_DATA:
197 case PERM_ANDROID_MEDIA:
198 case PERM_ANDROID_PACKAGE:
199 case PERM_ANDROID_PACKAGE_CACHE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700200 uid = multiuser_get_uid(info_d->userid, uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800201 break;
202 case PERM_ANDROID_OBB:
203 uid = AID_MEDIA_OBB;
204 break;
205 case PERM_PRE_ROOT:
206 default:
207 break;
208 }
209 switch (perm) {
210 case PERM_ROOT:
211 case PERM_ANDROID:
212 case PERM_ANDROID_DATA:
213 case PERM_ANDROID_MEDIA:
214 if (S_ISDIR(d_inode(dentry)->i_mode))
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700215 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800216 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700217 gid = multiuser_get_uid(info_d->userid, get_type(name));
Daniel Rosenbergad905252017-01-25 13:48:45 -0800218 break;
219 case PERM_ANDROID_OBB:
220 gid = AID_MEDIA_OBB;
221 break;
222 case PERM_ANDROID_PACKAGE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700223 if (uid_is_app(info_d->d_uid))
224 gid = multiuser_get_ext_gid(info_d->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800225 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700226 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800227 break;
228 case PERM_ANDROID_PACKAGE_CACHE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700229 if (uid_is_app(info_d->d_uid))
230 gid = multiuser_get_ext_cache_gid(info_d->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800231 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700232 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800233 break;
234 case PERM_PRE_ROOT:
235 default:
236 break;
237 }
238
239 sdcardfs_get_lower_path(dentry, &path);
240 inode = d_inode(path.dentry);
241 if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) {
242retry_deleg:
243 newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
244 newattrs.ia_uid = make_kuid(current_user_ns(), uid);
245 newattrs.ia_gid = make_kgid(current_user_ns(), gid);
246 if (!S_ISDIR(inode->i_mode))
247 newattrs.ia_valid |=
248 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
249 inode_lock(inode);
250 error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
251 if (!error)
252 error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode);
253 inode_unlock(inode);
254 if (delegated_inode) {
255 error = break_deleg_wait(&delegated_inode);
256 if (!error)
257 goto retry_deleg;
258 }
259 if (error)
Daniel Rosenberg33efe542017-04-18 22:49:38 -0700260 pr_debug("sdcardfs: Failed to touch up lower fs gid/uid for %s\n", name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800261 }
Daniel Rosenbergf61bc5a2017-02-16 17:55:22 -0800262 sdcardfs_put_lower_path(dentry, &path);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800263}
264
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700265static int descendant_may_need_fixup(struct sdcardfs_inode_data *data,
266 struct limit_search *limit)
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800267{
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700268 if (data->perm == PERM_ROOT)
269 return (limit->flags & BY_USERID) ?
270 data->userid == limit->userid : 1;
271 if (data->perm == PERM_PRE_ROOT || data->perm == PERM_ANDROID)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700272 return 1;
273 return 0;
274}
275
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700276static int needs_fixup(perm_t perm)
277{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700278 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
279 || perm == PERM_ANDROID_MEDIA)
280 return 1;
281 return 0;
282}
283
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800284static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth)
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800285{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700286 struct dentry *child;
287 struct sdcardfs_inode_info *info;
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800288
289 /*
290 * All paths will terminate their recursion on hitting PERM_ANDROID_OBB,
291 * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of
292 * at most 3.
293 */
294 WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__);
295 spin_lock_nested(&dentry->d_lock, depth);
Daniel Rosenberg63d20762016-12-01 14:36:29 -0800296 if (!d_inode(dentry)) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800297 spin_unlock(&dentry->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700298 return;
299 }
300 info = SDCARDFS_I(d_inode(dentry));
301
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700302 if (needs_fixup(info->data->perm)) {
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800303 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800304 spin_lock_nested(&child->d_lock, depth + 1);
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800305 if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) {
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800306 if (d_inode(child)) {
307 get_derived_permission(dentry, child);
308 fixup_tmp_permissions(d_inode(child));
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800309 spin_unlock(&child->d_lock);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800310 break;
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800311 }
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800312 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800313 spin_unlock(&child->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700314 }
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700315 } else if (descendant_may_need_fixup(info->data, limit)) {
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700316 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700317 __fixup_perms_recursive(child, limit, depth + 1);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700318 }
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700319 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800320 spin_unlock(&dentry->d_lock);
321}
322
323void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit)
324{
325 __fixup_perms_recursive(dentry, limit, 0);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700326}
327
Daniel Campello35c9e242015-07-20 16:23:50 -0700328/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800329inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700330{
331 struct dentry *parent;
332
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700333 if (!dentry || !d_inode(dentry)) {
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700334 pr_err("sdcardfs: %s: invalid dentry\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700335 return;
336 }
337 /* FIXME:
338 * 1. need to check whether the dentry is updated or not
339 * 2. remove the root dentry update
340 */
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700341 if (!IS_ROOT(dentry)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700342 parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700343 if (parent) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700344 get_derived_permission(parent, dentry);
345 dput(parent);
346 }
347 }
Daniel Rosenberg90219272016-10-26 20:27:20 -0700348 fixup_tmp_permissions(d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -0700349}
350
351int need_graft_path(struct dentry *dentry)
352{
353 int ret = 0;
354 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700355 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700356 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800357 struct qstr obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700358
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700359 if (parent_info->data->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800360 qstr_case_eq(&dentry->d_name, &obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700361
362 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700363 if (!(sbi->options.multiuser == false
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700364 && parent_info->data->userid == 0)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700365 ret = 1;
366 }
367 }
368 dput(parent);
369 return ret;
370}
371
372int is_obbpath_invalid(struct dentry *dent)
373{
374 int ret = 0;
375 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
376 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
377 char *path_buf, *obbpath_s;
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800378 int need_put = 0;
379 struct path lower_path;
Daniel Campello35c9e242015-07-20 16:23:50 -0700380
381 /* check the base obbpath has been changed.
382 * this routine can check an uninitialized obb dentry as well.
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700383 * regarding the uninitialized obb, refer to the sdcardfs_mkdir()
384 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700385 spin_lock(&di->lock);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700386 if (di->orig_path.dentry) {
387 if (!di->lower_path.dentry) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700388 ret = 1;
389 } else {
390 path_get(&di->lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700391
392 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700393 if (!path_buf) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700394 ret = 1;
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700395 pr_err("sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700396 } else {
397 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
398 if (d_unhashed(di->lower_path.dentry) ||
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800399 !str_case_eq(sbi->obbpath_s, obbpath_s)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700400 ret = 1;
401 }
402 kfree(path_buf);
403 }
404
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800405 pathcpy(&lower_path, &di->lower_path);
406 need_put = 1;
Daniel Campello35c9e242015-07-20 16:23:50 -0700407 }
408 }
409 spin_unlock(&di->lock);
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800410 if (need_put)
411 path_put(&lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700412 return ret;
413}
414
415int is_base_obbpath(struct dentry *dentry)
416{
417 int ret = 0;
418 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700419 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700420 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800421 struct qstr q_obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700422
423 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800424 if (sbi->options.multiuser) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700425 if (parent_info->data->perm == PERM_PRE_ROOT &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800426 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800427 ret = 1;
428 }
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700429 } else if (parent_info->data->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800430 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700431 ret = 1;
432 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700433 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700434 return ret;
435}
436
437/* The lower_path will be stored to the dentry's orig_path
438 * and the base obbpath will be copyed to the lower_path variable.
439 * if an error returned, there's no change in the lower_path
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700440 * returns: -ERRNO if error (0: no error)
441 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700442int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
443{
444 int err = 0;
445 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
446 struct path obbpath;
447
448 /* A local obb dentry must have its own orig_path to support rmdir
449 * and mkdir of itself. Usually, we expect that the sbi->obbpath
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700450 * is avaiable on this stage.
451 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700452 sdcardfs_set_orig_path(dentry, lower_path);
453
454 err = kern_path(sbi->obbpath_s,
455 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
456
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700457 if (!err) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700458 /* the obbpath base has been found */
Daniel Campello35c9e242015-07-20 16:23:50 -0700459 pathcpy(lower_path, &obbpath);
460 } else {
461 /* if the sbi->obbpath is not available, we can optionally
462 * setup the lower_path with its orig_path.
463 * but, the current implementation just returns an error
464 * because the sdcard daemon also regards this case as
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700465 * a lookup fail.
466 */
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700467 pr_info("sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700468 }
469 return err;
470}
471
472