blob: 29645276c7340a475452a894e26c5e504bd8f10b [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
29 ci->perm = PERM_INHERIT;
30 ci->userid = pi->userid;
31 ci->d_uid = pi->d_uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080032 ci->under_android = pi->under_android;
Daniel Rosenbergad905252017-01-25 13:48:45 -080033 ci->under_cache = pi->under_cache;
34 ci->under_obb = pi->under_obb;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070035 set_top(ci, pi->top);
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 Rosenberg5e024f62017-03-16 17:42:58 -070040 uid_t uid, bool under_android,
41 struct inode *top)
Daniel Campello35c9e242015-07-20 16:23:50 -070042{
43 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
44
45 info->perm = perm;
46 info->userid = userid;
47 info->d_uid = uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080048 info->under_android = under_android;
Daniel Rosenbergad905252017-01-25 13:48:45 -080049 info->under_cache = false;
50 info->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 Rosenberg5e024f62017-03-16 17:42:58 -070061 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -070062 appid_t appid;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070063 unsigned long user_num;
64 int err;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -080065 struct qstr q_Android = QSTR_LITERAL("Android");
66 struct qstr q_data = QSTR_LITERAL("data");
67 struct qstr q_obb = QSTR_LITERAL("obb");
68 struct qstr q_media = QSTR_LITERAL("media");
69 struct qstr q_cache = QSTR_LITERAL("cache");
Daniel Campello35c9e242015-07-20 16:23:50 -070070
71 /* By default, each inode inherits from its parent.
72 * the properties are maintained on its private fields
73 * because the inode attributes will be modified with that of
74 * its lower inode.
Daniel Rosenbergad905252017-01-25 13:48:45 -080075 * These values are used by our custom permission call instead
76 * of using the inode permissions.
Daniel Campello35c9e242015-07-20 16:23:50 -070077 */
78
Daniel Rosenberg63d20762016-12-01 14:36:29 -080079 inherit_derived_state(d_inode(parent), d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -070080
Daniel Rosenbergad905252017-01-25 13:48:45 -080081 /* Files don't get special labels */
82 if (!S_ISDIR(d_inode(dentry)->i_mode))
83 return;
Daniel Campello35c9e242015-07-20 16:23:50 -070084 /* Derive custom permissions based on parent and current node */
85 switch (parent_info->perm) {
Daniel Rosenbergad905252017-01-25 13:48:45 -080086 case PERM_INHERIT:
87 case PERM_ANDROID_PACKAGE_CACHE:
88 /* Already inherited above */
89 break;
90 case PERM_PRE_ROOT:
91 /* Legacy internal layout places users at top level */
92 info->perm = PERM_ROOT;
Daniel Rosenberg3c42d402017-03-16 19:32:59 -070093 err = kstrtoul(name->name, 10, &user_num);
94 if (err)
95 info->userid = 0;
96 else
97 info->userid = user_num;
Daniel Rosenbergad905252017-01-25 13:48:45 -080098 set_top(info, &info->vfs_inode);
99 break;
100 case PERM_ROOT:
101 /* Assume masked off by default. */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800102 if (qstr_case_eq(name, &q_Android)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800103 /* App-specific directories inside; let anyone traverse */
104 info->perm = PERM_ANDROID;
105 info->under_android = true;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700106 set_top(info, &info->vfs_inode);
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 */
112 info->perm = PERM_ANDROID_DATA;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700113 set_top(info, &info->vfs_inode);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800114 } else if (qstr_case_eq(name, &q_obb)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800115 /* App-specific directories inside; let anyone traverse */
116 info->perm = PERM_ANDROID_OBB;
117 info->under_obb = true;
118 set_top(info, &info->vfs_inode);
119 /* Single OBB directory is always shared */
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800120 } else if (qstr_case_eq(name, &q_media)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800121 /* App-specific directories inside; let anyone traverse */
122 info->perm = PERM_ANDROID_MEDIA;
123 set_top(info, &info->vfs_inode);
124 }
125 break;
126 case PERM_ANDROID_OBB:
127 case PERM_ANDROID_DATA:
128 case PERM_ANDROID_MEDIA:
129 info->perm = PERM_ANDROID_PACKAGE;
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800130 appid = get_appid(name->name);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700131 if (appid != 0 && !is_excluded(name->name, parent_info->userid))
Daniel Rosenbergad905252017-01-25 13:48:45 -0800132 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800133 set_top(info, &info->vfs_inode);
134 break;
135 case PERM_ANDROID_PACKAGE:
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800136 if (qstr_case_eq(name, &q_cache)) {
Daniel Rosenbergad905252017-01-25 13:48:45 -0800137 info->perm = PERM_ANDROID_PACKAGE_CACHE;
138 info->under_cache = true;
139 }
140 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;
169 struct sdcardfs_inode_info *info_top;
170 perm_t perm;
171 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
172 uid_t uid = sbi->options.fs_low_uid;
173 gid_t gid = sbi->options.fs_low_gid;
174 struct iattr newattrs;
175
176 info = SDCARDFS_I(d_inode(dentry));
177 perm = info->perm;
178 if (info->under_obb) {
179 perm = PERM_ANDROID_OBB;
180 } else if (info->under_cache) {
181 perm = PERM_ANDROID_PACKAGE_CACHE;
182 } else if (perm == PERM_INHERIT) {
183 info_top = SDCARDFS_I(grab_top(info));
184 perm = info_top->perm;
185 release_top(info);
186 }
187
188 switch (perm) {
189 case PERM_ROOT:
190 case PERM_ANDROID:
191 case PERM_ANDROID_DATA:
192 case PERM_ANDROID_MEDIA:
193 case PERM_ANDROID_PACKAGE:
194 case PERM_ANDROID_PACKAGE_CACHE:
195 uid = multiuser_get_uid(info->userid, uid);
196 break;
197 case PERM_ANDROID_OBB:
198 uid = AID_MEDIA_OBB;
199 break;
200 case PERM_PRE_ROOT:
201 default:
202 break;
203 }
204 switch (perm) {
205 case PERM_ROOT:
206 case PERM_ANDROID:
207 case PERM_ANDROID_DATA:
208 case PERM_ANDROID_MEDIA:
209 if (S_ISDIR(d_inode(dentry)->i_mode))
210 gid = multiuser_get_uid(info->userid, AID_MEDIA_RW);
211 else
212 gid = multiuser_get_uid(info->userid, get_type(name));
213 break;
214 case PERM_ANDROID_OBB:
215 gid = AID_MEDIA_OBB;
216 break;
217 case PERM_ANDROID_PACKAGE:
218 if (info->d_uid != 0)
Daniel Rosenberg1ec14332017-03-13 15:34:03 -0700219 gid = multiuser_get_ext_gid(info->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800220 else
221 gid = multiuser_get_uid(info->userid, uid);
222 break;
223 case PERM_ANDROID_PACKAGE_CACHE:
224 if (info->d_uid != 0)
Daniel Rosenberg366b8b82017-04-17 17:11:38 -0700225 gid = multiuser_get_ext_cache_gid(info->d_uid);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800226 else
227 gid = multiuser_get_uid(info->userid, uid);
228 break;
229 case PERM_PRE_ROOT:
230 default:
231 break;
232 }
233
234 sdcardfs_get_lower_path(dentry, &path);
235 inode = d_inode(path.dentry);
236 if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) {
237retry_deleg:
238 newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
239 newattrs.ia_uid = make_kuid(current_user_ns(), uid);
240 newattrs.ia_gid = make_kgid(current_user_ns(), gid);
241 if (!S_ISDIR(inode->i_mode))
242 newattrs.ia_valid |=
243 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
244 inode_lock(inode);
245 error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
246 if (!error)
247 error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode);
248 inode_unlock(inode);
249 if (delegated_inode) {
250 error = break_deleg_wait(&delegated_inode);
251 if (!error)
252 goto retry_deleg;
253 }
254 if (error)
Daniel Rosenberg33efe542017-04-18 22:49:38 -0700255 pr_debug("sdcardfs: Failed to touch up lower fs gid/uid for %s\n", name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800256 }
Daniel Rosenbergf61bc5a2017-02-16 17:55:22 -0800257 sdcardfs_put_lower_path(dentry, &path);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800258}
259
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800260static int descendant_may_need_fixup(struct sdcardfs_inode_info *info, struct limit_search *limit)
261{
262 if (info->perm == PERM_ROOT)
263 return (limit->flags & BY_USERID)?info->userid == limit->userid:1;
264 if (info->perm == PERM_PRE_ROOT || info->perm == PERM_ANDROID)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700265 return 1;
266 return 0;
267}
268
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700269static int needs_fixup(perm_t perm)
270{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700271 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
272 || perm == PERM_ANDROID_MEDIA)
273 return 1;
274 return 0;
275}
276
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800277static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth)
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800278{
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700279 struct dentry *child;
280 struct sdcardfs_inode_info *info;
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800281
282 /*
283 * All paths will terminate their recursion on hitting PERM_ANDROID_OBB,
284 * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of
285 * at most 3.
286 */
287 WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__);
288 spin_lock_nested(&dentry->d_lock, depth);
Daniel Rosenberg63d20762016-12-01 14:36:29 -0800289 if (!d_inode(dentry)) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800290 spin_unlock(&dentry->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700291 return;
292 }
293 info = SDCARDFS_I(d_inode(dentry));
294
295 if (needs_fixup(info->perm)) {
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800296 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800297 spin_lock_nested(&child->d_lock, depth + 1);
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800298 if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) {
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800299 if (d_inode(child)) {
300 get_derived_permission(dentry, child);
301 fixup_tmp_permissions(d_inode(child));
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800302 spin_unlock(&child->d_lock);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800303 break;
Daniel Rosenberg3adfc032016-12-27 12:36:29 -0800304 }
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800305 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800306 spin_unlock(&child->d_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700307 }
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700308 } else if (descendant_may_need_fixup(info, limit)) {
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700309 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700310 __fixup_perms_recursive(child, limit, depth + 1);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700311 }
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700312 }
Daniel Rosenbergfb4d1912017-03-02 15:11:27 -0800313 spin_unlock(&dentry->d_lock);
314}
315
316void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit)
317{
318 __fixup_perms_recursive(dentry, limit, 0);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700319}
320
Daniel Campello35c9e242015-07-20 16:23:50 -0700321/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800322inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700323{
324 struct dentry *parent;
325
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700326 if (!dentry || !d_inode(dentry)) {
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700327 pr_err("sdcardfs: %s: invalid dentry\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700328 return;
329 }
330 /* FIXME:
331 * 1. need to check whether the dentry is updated or not
332 * 2. remove the root dentry update
333 */
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700334 if (!IS_ROOT(dentry)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700335 parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700336 if (parent) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700337 get_derived_permission(parent, dentry);
338 dput(parent);
339 }
340 }
Daniel Rosenberg90219272016-10-26 20:27:20 -0700341 fixup_tmp_permissions(d_inode(dentry));
Daniel Campello35c9e242015-07-20 16:23:50 -0700342}
343
344int need_graft_path(struct dentry *dentry)
345{
346 int ret = 0;
347 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700348 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700349 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800350 struct qstr obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700351
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700352 if (parent_info->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800353 qstr_case_eq(&dentry->d_name, &obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700354
355 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700356 if (!(sbi->options.multiuser == false
Daniel Campello35c9e242015-07-20 16:23:50 -0700357 && parent_info->userid == 0)) {
358 ret = 1;
359 }
360 }
361 dput(parent);
362 return ret;
363}
364
365int is_obbpath_invalid(struct dentry *dent)
366{
367 int ret = 0;
368 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
369 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
370 char *path_buf, *obbpath_s;
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800371 int need_put = 0;
372 struct path lower_path;
Daniel Campello35c9e242015-07-20 16:23:50 -0700373
374 /* check the base obbpath has been changed.
375 * this routine can check an uninitialized obb dentry as well.
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700376 * regarding the uninitialized obb, refer to the sdcardfs_mkdir()
377 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700378 spin_lock(&di->lock);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700379 if (di->orig_path.dentry) {
380 if (!di->lower_path.dentry) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700381 ret = 1;
382 } else {
383 path_get(&di->lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700384
385 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700386 if (!path_buf) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700387 ret = 1;
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700388 pr_err("sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700389 } else {
390 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
391 if (d_unhashed(di->lower_path.dentry) ||
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800392 !str_case_eq(sbi->obbpath_s, obbpath_s)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700393 ret = 1;
394 }
395 kfree(path_buf);
396 }
397
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800398 pathcpy(&lower_path, &di->lower_path);
399 need_put = 1;
Daniel Campello35c9e242015-07-20 16:23:50 -0700400 }
401 }
402 spin_unlock(&di->lock);
Daniel Rosenberge2538da2017-03-10 13:54:30 -0800403 if (need_put)
404 path_put(&lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700405 return ret;
406}
407
408int is_base_obbpath(struct dentry *dentry)
409{
410 int ret = 0;
411 struct dentry *parent = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700412 struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700413 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800414 struct qstr q_obb = QSTR_LITERAL("obb");
Daniel Campello35c9e242015-07-20 16:23:50 -0700415
416 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800417 if (sbi->options.multiuser) {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700418 if (parent_info->perm == PERM_PRE_ROOT &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800419 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800420 ret = 1;
421 }
422 } else if (parent_info->perm == PERM_ANDROID &&
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800423 qstr_case_eq(&dentry->d_name, &q_obb)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700424 ret = 1;
425 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700426 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700427 return ret;
428}
429
430/* The lower_path will be stored to the dentry's orig_path
431 * and the base obbpath will be copyed to the lower_path variable.
432 * if an error returned, there's no change in the lower_path
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700433 * returns: -ERRNO if error (0: no error)
434 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700435int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
436{
437 int err = 0;
438 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
439 struct path obbpath;
440
441 /* A local obb dentry must have its own orig_path to support rmdir
442 * and mkdir of itself. Usually, we expect that the sbi->obbpath
Daniel Rosenbergd64126c2017-03-16 19:33:35 -0700443 * is avaiable on this stage.
444 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700445 sdcardfs_set_orig_path(dentry, lower_path);
446
447 err = kern_path(sbi->obbpath_s,
448 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
449
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700450 if (!err) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700451 /* the obbpath base has been found */
Daniel Campello35c9e242015-07-20 16:23:50 -0700452 pathcpy(lower_path, &obbpath);
453 } else {
454 /* if the sbi->obbpath is not available, we can optionally
455 * setup the lower_path with its orig_path.
456 * but, the current implementation just returns an error
457 * because the sdcard daemon also regards this case as
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700458 * a lookup fail.
459 */
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700460 pr_info("sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700461 }
462 return err;
463}
464
465