blob: fffaad4701cf160bc6c90ba658676e4a1c7dd749 [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;
35 set_top(ci, pi->top_data);
Daniel Campello35c9e242015-07-20 16:23:50 -070036}
37
38/* helper function for derived state */
Daniel Rosenberg5080d242016-05-18 16:57:10 -070039void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
Daniel Rosenberga56a1052017-05-15 14:03:15 -070040 uid_t uid, bool under_android,
41 struct sdcardfs_inode_data *top)
Daniel Campello35c9e242015-07-20 16:23:50 -070042{
43 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
44
Daniel Rosenberga56a1052017-05-15 14:03:15 -070045 info->data->perm = perm;
46 info->data->userid = userid;
47 info->data->d_uid = uid;
48 info->data->under_android = under_android;
49 info->data->under_cache = false;
50 info->data->under_obb = false;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070051 set_top(info, top);
Daniel Campello35c9e242015-07-20 16:23:50 -070052}
53
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070054/* While renaming, there is a point where we want the path from dentry,
55 * but the name from newdentry
56 */
57void get_derived_permission_new(struct dentry *parent, struct dentry *dentry,
58 const struct qstr *name)
Daniel Campello35c9e242015-07-20 16:23:50 -070059{
Daniel Rosenberg63d20762016-12-01 14:36:29 -080060 struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
Daniel Rosenberga56a1052017-05-15 14:03:15 -070061 struct sdcardfs_inode_data *parent_data =
62 SDCARDFS_I(d_inode(parent))->data;
Daniel Campello35c9e242015-07-20 16:23:50 -070063 appid_t appid;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070064 unsigned long user_num;
65 int err;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -080066 struct qstr q_Android = QSTR_LITERAL("Android");
67 struct qstr q_data = QSTR_LITERAL("data");
68 struct qstr q_obb = QSTR_LITERAL("obb");
69 struct qstr q_media = QSTR_LITERAL("media");
70 struct qstr q_cache = QSTR_LITERAL("cache");
Daniel Campello35c9e242015-07-20 16:23:50 -070071
72 /* By default, each inode inherits from its parent.
73 * the properties are maintained on its private fields
74 * because the inode attributes will be modified with that of
75 * its lower inode.
Daniel Rosenbergad905252017-01-25 13:48:45 -080076 * These values are used by our custom permission call instead
77 * of using the inode permissions.
Daniel Campello35c9e242015-07-20 16:23:50 -070078 */
79
Daniel Rosenberg63d20762016-12-01 14:36:29 -080080 inherit_derived_state(d_inode(parent), d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -070081
Daniel Rosenbergad905252017-01-25 13:48:45 -080082 /* Files don't get special labels */
83 if (!S_ISDIR(d_inode(dentry)->i_mode))
84 return;
Daniel Campello35c9e242015-07-20 16:23:50 -070085 /* Derive custom permissions based on parent and current node */
Daniel Rosenberga56a1052017-05-15 14:03:15 -070086 switch (parent_data->perm) {
Daniel Rosenbergad905252017-01-25 13:48:45 -080087 case PERM_INHERIT:
88 case PERM_ANDROID_PACKAGE_CACHE:
89 /* Already inherited above */
90 break;
91 case PERM_PRE_ROOT:
92 /* Legacy internal layout places users at top level */
Daniel Rosenberga56a1052017-05-15 14:03:15 -070093 info->data->perm = PERM_ROOT;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070094 err = kstrtoul(name->name, 10, &user_num);
95 if (err)
Daniel Rosenberga56a1052017-05-15 14:03:15 -070096 info->data->userid = 0;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070097 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -070098 info->data->userid = user_num;
99 set_top(info, info->data);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800100 break;
101 case PERM_ROOT:
102 /* Assume masked off by default. */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800103 if (qstr_case_eq(name, &q_Android)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800104 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700105 info->data->perm = PERM_ANDROID;
106 info->data->under_android = true;
107 set_top(info, info->data);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800108 }
109 break;
110 case PERM_ANDROID:
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800111 if (qstr_case_eq(name, &q_data)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800112 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700113 info->data->perm = PERM_ANDROID_DATA;
114 set_top(info, info->data);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800115 } else if (qstr_case_eq(name, &q_obb)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800116 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700117 info->data->perm = PERM_ANDROID_OBB;
118 info->data->under_obb = true;
119 set_top(info, info->data);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800120 /* Single OBB directory is always shared */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800121 } else if (qstr_case_eq(name, &q_media)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800122 /* App-specific directories inside; let anyone traverse */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700123 info->data->perm = PERM_ANDROID_MEDIA;
124 set_top(info, info->data);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800125 }
126 break;
127 case PERM_ANDROID_OBB:
128 case PERM_ANDROID_DATA:
129 case PERM_ANDROID_MEDIA:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700130 info->data->perm = PERM_ANDROID_PACKAGE;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800131 appid = get_appid(name->name);
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700132 if (appid != 0 && !is_excluded(name->name, parent_data->userid))
133 info->data->d_uid =
134 multiuser_get_uid(parent_data->userid, appid);
135 set_top(info, info->data);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800136 break;
137 case PERM_ANDROID_PACKAGE:
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800138 if (qstr_case_eq(name, &q_cache)) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700139 info->data->perm = PERM_ANDROID_PACKAGE_CACHE;
140 info->data->under_cache = true;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800141 }
142 break;
Daniel Campello35c9e242015-07-20 16:23:50 -0700143 }
144}
145
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800146void get_derived_permission(struct dentry *parent, struct dentry *dentry)
147{
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800148 get_derived_permission_new(parent, dentry, &dentry->d_name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800149}
150
151static appid_t get_type(const char *name)
152{
153 const char *ext = strrchr(name, '.');
154 appid_t id;
155
156 if (ext && ext[0]) {
157 ext = &ext[1];
158 id = get_ext_gid(ext);
159 return id?:AID_MEDIA_RW;
160 }
161 return AID_MEDIA_RW;
162}
163
164void fixup_lower_ownership(struct dentry *dentry, const char *name)
165{
166 struct path path;
167 struct inode *inode;
168 struct inode *delegated_inode = NULL;
169 int error;
170 struct sdcardfs_inode_info *info;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700171 struct sdcardfs_inode_data *info_d;
172 struct sdcardfs_inode_data *info_top;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800173 perm_t perm;
174 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
175 uid_t uid = sbi->options.fs_low_uid;
176 gid_t gid = sbi->options.fs_low_gid;
177 struct iattr newattrs;
178
Daniel Rosenberg7d10e432017-07-19 17:25:07 -0700179 if (!sbi->options.gid_derivation)
180 return;
181
Daniel Rosenbergad905252017-01-25 13:48:45 -0800182 info = SDCARDFS_I(d_inode(dentry));
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700183 info_d = info->data;
184 perm = info_d->perm;
185 if (info_d->under_obb) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800186 perm = PERM_ANDROID_OBB;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700187 } else if (info_d->under_cache) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800188 perm = PERM_ANDROID_PACKAGE_CACHE;
189 } else if (perm == PERM_INHERIT) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700190 info_top = top_data_get(info);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800191 perm = info_top->perm;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700192 data_put(info_top);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800193 }
194
195 switch (perm) {
196 case PERM_ROOT:
197 case PERM_ANDROID:
198 case PERM_ANDROID_DATA:
199 case PERM_ANDROID_MEDIA:
200 case PERM_ANDROID_PACKAGE:
201 case PERM_ANDROID_PACKAGE_CACHE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700202 uid = multiuser_get_uid(info_d->userid, uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800203 break;
204 case PERM_ANDROID_OBB:
205 uid = AID_MEDIA_OBB;
206 break;
207 case PERM_PRE_ROOT:
208 default:
209 break;
210 }
211 switch (perm) {
212 case PERM_ROOT:
213 case PERM_ANDROID:
214 case PERM_ANDROID_DATA:
215 case PERM_ANDROID_MEDIA:
216 if (S_ISDIR(d_inode(dentry)->i_mode))
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700217 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800218 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700219 gid = multiuser_get_uid(info_d->userid, get_type(name));
Daniel Rosenbergad905252017-01-25 13:48:45 -0800220 break;
221 case PERM_ANDROID_OBB:
222 gid = AID_MEDIA_OBB;
223 break;
224 case PERM_ANDROID_PACKAGE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700225 if (uid_is_app(info_d->d_uid))
226 gid = multiuser_get_ext_gid(info_d->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800227 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700228 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800229 break;
230 case PERM_ANDROID_PACKAGE_CACHE:
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700231 if (uid_is_app(info_d->d_uid))
232 gid = multiuser_get_ext_cache_gid(info_d->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800233 else
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700234 gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800235 break;
236 case PERM_PRE_ROOT:
237 default:
238 break;
239 }
240
241 sdcardfs_get_lower_path(dentry, &path);
242 inode = d_inode(path.dentry);
243 if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) {
244retry_deleg:
245 newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
246 newattrs.ia_uid = make_kuid(current_user_ns(), uid);
247 newattrs.ia_gid = make_kgid(current_user_ns(), gid);
248 if (!S_ISDIR(inode->i_mode))
249 newattrs.ia_valid |=
250 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
251 inode_lock(inode);
252 error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
253 if (!error)
254 error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode);
255 inode_unlock(inode);
256 if (delegated_inode) {
257 error = break_deleg_wait(&delegated_inode);
258 if (!error)
259 goto retry_deleg;
260 }
261 if (error)
Daniel Rosenberg33efe542017-04-18 22:49:38 -0700262 pr_debug("sdcardfs: Failed to touch up lower fs gid/uid for %s\n", name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800263 }
Daniel Rosenbergf61bc5a2017-02-16 17:55:22 -0800264 sdcardfs_put_lower_path(dentry, &path);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800265}
266
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700267static int descendant_may_need_fixup(struct sdcardfs_inode_data *data,
268 struct limit_search *limit)
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800269{
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700270 if (data->perm == PERM_ROOT)
271 return (limit->flags & BY_USERID) ?
272 data->userid == limit->userid : 1;
273 if (data->perm == PERM_PRE_ROOT || data->perm == PERM_ANDROID)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700274 return 1;
275 return 0;
276}
277
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700278static int needs_fixup(perm_t perm)
279{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700280 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
281 || perm == PERM_ANDROID_MEDIA)
282 return 1;
283 return 0;
284}
285
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800286static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth)
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800287{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700288 struct dentry *child;
289 struct sdcardfs_inode_info *info;
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800290
291 /*
292 * All paths will terminate their recursion on hitting PERM_ANDROID_OBB,
293 * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of
294 * at most 3.
295 */
296 WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__);
297 spin_lock_nested(&dentry->d_lock, depth);
Daniel Rosenberg63d20762016-12-01 14:36:29 -0800298 if (!d_inode(dentry)) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800299 spin_unlock(&dentry->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700300 return;
301 }
302 info = SDCARDFS_I(d_inode(dentry));
303
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700304 if (needs_fixup(info->data->perm)) {
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800305 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800306 spin_lock_nested(&child->d_lock, depth + 1);
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800307 if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) {
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800308 if (d_inode(child)) {
309 get_derived_permission(dentry, child);
310 fixup_tmp_permissions(d_inode(child));
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800311 spin_unlock(&child->d_lock);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800312 break;
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800313 }
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800314 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800315 spin_unlock(&child->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700316 }
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700317 } else if (descendant_may_need_fixup(info->data, limit)) {
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700318 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700319 __fixup_perms_recursive(child, limit, depth + 1);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700320 }
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700321 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800322 spin_unlock(&dentry->d_lock);
323}
324
325void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit)
326{
327 __fixup_perms_recursive(dentry, limit, 0);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700328}
329
Daniel Campello35c9e242015-07-20 16:23:50 -0700330/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800331inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700332{
333 struct dentry *parent;
334
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700335 if (!dentry || !d_inode(dentry)) {
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700336 pr_err("sdcardfs: %s: invalid dentry\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700337 return;
338 }
339 /* FIXME:
340 * 1. need to check whether the dentry is updated or not
341 * 2. remove the root dentry update
342 */
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700343 if (!IS_ROOT(dentry)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700344 parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700345 if (parent) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700346 get_derived_permission(parent, dentry);
347 dput(parent);
348 }
349 }
Daniel Rosenberg90219272016-10-26 20:27:20 -0700350 fixup_tmp_permissions(d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -0700351}
352
353int need_graft_path(struct dentry *dentry)
354{
355 int ret = 0;
356 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700357 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700358 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800359 struct qstr obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700360
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700361 if (parent_info->data->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800362 qstr_case_eq(&dentry->d_name, &obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700363
364 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700365 if (!(sbi->options.multiuser == false
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700366 && parent_info->data->userid == 0)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700367 ret = 1;
368 }
369 }
370 dput(parent);
371 return ret;
372}
373
374int is_obbpath_invalid(struct dentry *dent)
375{
376 int ret = 0;
377 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
378 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
379 char *path_buf, *obbpath_s;
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800380 int need_put = 0;
381 struct path lower_path;
Daniel Campello35c9e242015-07-20 16:23:50 -0700382
383 /* check the base obbpath has been changed.
384 * this routine can check an uninitialized obb dentry as well.
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700385 * regarding the uninitialized obb, refer to the sdcardfs_mkdir()
386 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700387 spin_lock(&di->lock);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700388 if (di->orig_path.dentry) {
389 if (!di->lower_path.dentry) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700390 ret = 1;
391 } else {
392 path_get(&di->lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700393
394 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700395 if (!path_buf) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700396 ret = 1;
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700397 pr_err("sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700398 } else {
399 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
400 if (d_unhashed(di->lower_path.dentry) ||
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800401 !str_case_eq(sbi->obbpath_s, obbpath_s)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700402 ret = 1;
403 }
404 kfree(path_buf);
405 }
406
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800407 pathcpy(&lower_path, &di->lower_path);
408 need_put = 1;
Daniel Campello35c9e242015-07-20 16:23:50 -0700409 }
410 }
411 spin_unlock(&di->lock);
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800412 if (need_put)
413 path_put(&lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700414 return ret;
415}
416
417int is_base_obbpath(struct dentry *dentry)
418{
419 int ret = 0;
420 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700421 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700422 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800423 struct qstr q_obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700424
425 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800426 if (sbi->options.multiuser) {
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700427 if (parent_info->data->perm == PERM_PRE_ROOT &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800428 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800429 ret = 1;
430 }
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700431 } else if (parent_info->data->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800432 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700433 ret = 1;
434 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700435 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700436 return ret;
437}
438
439/* The lower_path will be stored to the dentry's orig_path
440 * and the base obbpath will be copyed to the lower_path variable.
441 * if an error returned, there's no change in the lower_path
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700442 * returns: -ERRNO if error (0: no error)
443 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700444int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
445{
446 int err = 0;
447 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
448 struct path obbpath;
449
450 /* A local obb dentry must have its own orig_path to support rmdir
451 * and mkdir of itself. Usually, we expect that the sbi->obbpath
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700452 * is avaiable on this stage.
453 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700454 sdcardfs_set_orig_path(dentry, lower_path);
455
456 err = kern_path(sbi->obbpath_s,
457 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
458
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700459 if (!err) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700460 /* the obbpath base has been found */
Daniel Campello35c9e242015-07-20 16:23:50 -0700461 pathcpy(lower_path, &obbpath);
462 } else {
463 /* if the sbi->obbpath is not available, we can optionally
464 * setup the lower_path with its orig_path.
465 * but, the current implementation just returns an error
466 * because the sdcard daemon also regards this case as
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700467 * a lookup fail.
468 */
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700469 pr_info("sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700470 }
471 return err;
472}
473
474