Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * fs/sdcardfs/derived_perm.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2013 Samsung Electronics Co. Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *               Sunghwan Yun, Sungjong Seo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * This program has been developed as a stackable file system based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * the WrapFS which written by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Copyright (c) 1998-2011 Erez Zadok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Copyright (c) 2009     Shrikar Archak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Copyright (c) 2003-2011 Stony Brook University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Copyright (c) 2003-2011 The Research Foundation of SUNY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This file is dual licensed.  It may be redistributed and/or modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * under the terms of the Apache 2.0 License OR version 2 of the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * General Public License.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "sdcardfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* copy derived state from parent inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static void inherit_derived_state(struct inode *parent, struct inode *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	ci->data->perm = PERM_INHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ci->data->userid = pi->data->userid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	ci->data->d_uid = pi->data->d_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	ci->data->under_android = pi->data->under_android;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	ci->data->under_cache = pi->data->under_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	ci->data->under_obb = pi->data->under_obb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* helper function for derived state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 					uid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	info->data->perm = perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	info->data->userid = userid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	info->data->d_uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	info->data->under_android = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	info->data->under_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	info->data->under_obb = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /* While renaming, there is a point where we want the path from dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * but the name from newdentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) void get_derived_permission_new(struct dentry *parent, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct sdcardfs_inode_data *parent_data = parent_info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	appid_t appid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned long user_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct qstr q_Android = QSTR_LITERAL("Android");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct qstr q_data = QSTR_LITERAL("data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct qstr q_sandbox = QSTR_LITERAL("sandbox");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct qstr q_obb = QSTR_LITERAL("obb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct qstr q_media = QSTR_LITERAL("media");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct qstr q_cache = QSTR_LITERAL("cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* By default, each inode inherits from its parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 * the properties are maintained on its private fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 * because the inode attributes will be modified with that of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 * its lower inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	 * These values are used by our custom permission call instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	 * of using the inode permissions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	inherit_derived_state(d_inode(parent), d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Files don't get special labels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!S_ISDIR(d_inode(dentry)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		set_top(info, parent_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* Derive custom permissions based on parent and current node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	switch (parent_data->perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	case PERM_INHERIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	case PERM_ANDROID_PACKAGE_CACHE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		set_top(info, parent_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	case PERM_PRE_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		/* Legacy internal layout places users at top level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		info->data->perm = PERM_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		err = kstrtoul(name->name, 10, &user_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			info->data->userid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			info->data->userid = user_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	case PERM_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		/* Assume masked off by default. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (qstr_case_eq(name, &q_Android)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			/* App-specific directories inside; let anyone traverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			info->data->perm = PERM_ANDROID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			info->data->under_android = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			set_top(info, parent_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	case PERM_ANDROID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (qstr_case_eq(name, &q_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			/* App-specific directories inside; let anyone traverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			info->data->perm = PERM_ANDROID_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		} else if (qstr_case_eq(name, &q_sandbox)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			/* App-specific directories inside; let anyone traverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			info->data->perm = PERM_ANDROID_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		} else if (qstr_case_eq(name, &q_obb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			/* App-specific directories inside; let anyone traverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			info->data->perm = PERM_ANDROID_OBB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			info->data->under_obb = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			/* Single OBB directory is always shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		} else if (qstr_case_eq(name, &q_media)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			/* App-specific directories inside; let anyone traverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			info->data->perm = PERM_ANDROID_MEDIA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			set_top(info, parent_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	case PERM_ANDROID_OBB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	case PERM_ANDROID_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	case PERM_ANDROID_MEDIA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		info->data->perm = PERM_ANDROID_PACKAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		appid = get_appid(name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (appid != 0 && !is_excluded(name->name, parent_data->userid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			info->data->d_uid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 				multiuser_get_uid(parent_data->userid, appid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	case PERM_ANDROID_PACKAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		if (qstr_case_eq(name, &q_cache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			info->data->perm = PERM_ANDROID_PACKAGE_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			info->data->under_cache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		set_top(info, parent_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void get_derived_permission(struct dentry *parent, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	get_derived_permission_new(parent, dentry, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static appid_t get_type(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	const char *ext = strrchr(name, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	appid_t id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (ext && ext[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		ext = &ext[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		id = get_ext_gid(ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return id?:AID_MEDIA_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return AID_MEDIA_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void fixup_lower_ownership(struct dentry *dentry, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct inode *delegated_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct sdcardfs_inode_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct sdcardfs_inode_data *info_d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct sdcardfs_inode_data *info_top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	perm_t perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	uid_t uid = sbi->options.fs_low_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	gid_t gid = sbi->options.fs_low_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct iattr newattrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (!sbi->options.gid_derivation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	info = SDCARDFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	info_d = info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	perm = info_d->perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (info_d->under_obb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		perm = PERM_ANDROID_OBB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	} else if (info_d->under_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		perm = PERM_ANDROID_PACKAGE_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	} else if (perm == PERM_INHERIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		info_top = top_data_get(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		perm = info_top->perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		data_put(info_top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	switch (perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	case PERM_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	case PERM_ANDROID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	case PERM_ANDROID_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	case PERM_ANDROID_MEDIA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	case PERM_ANDROID_PACKAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	case PERM_ANDROID_PACKAGE_CACHE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		uid = multiuser_get_uid(info_d->userid, uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	case PERM_ANDROID_OBB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		uid = AID_MEDIA_OBB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	case PERM_PRE_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	switch (perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	case PERM_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	case PERM_ANDROID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	case PERM_ANDROID_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	case PERM_ANDROID_MEDIA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (S_ISDIR(d_inode(dentry)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			gid = multiuser_get_uid(info_d->userid, get_type(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	case PERM_ANDROID_OBB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		gid = AID_MEDIA_OBB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	case PERM_ANDROID_PACKAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (uid_is_app(info_d->d_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			gid = multiuser_get_ext_gid(info_d->d_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	case PERM_ANDROID_PACKAGE_CACHE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (uid_is_app(info_d->d_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			gid = multiuser_get_ext_cache_gid(info_d->d_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			gid = multiuser_get_uid(info_d->userid, AID_MEDIA_RW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	case PERM_PRE_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	sdcardfs_get_lower_path(dentry, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	inode = d_inode(path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) retry_deleg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		newattrs.ia_uid = make_kuid(current_user_ns(), uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		newattrs.ia_gid = make_kgid(current_user_ns(), gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (!S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			newattrs.ia_valid |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (delegated_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			error = break_deleg_wait(&delegated_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				goto retry_deleg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			pr_debug("sdcardfs: Failed to touch up lower fs gid/uid for %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	sdcardfs_put_lower_path(dentry, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int descendant_may_need_fixup(struct sdcardfs_inode_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		struct limit_search *limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (data->perm == PERM_ROOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return (limit->flags & BY_USERID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				data->userid == limit->userid : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (data->perm == PERM_PRE_ROOT || data->perm == PERM_ANDROID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int needs_fixup(perm_t perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			|| perm == PERM_ANDROID_MEDIA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct dentry *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct sdcardfs_inode_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 * All paths will terminate their recursion on hitting PERM_ANDROID_OBB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * at most 3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	spin_lock_nested(&dentry->d_lock, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (!d_inode(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	info = SDCARDFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (needs_fixup(info->data->perm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		list_for_each_entry(child, &dentry->d_subdirs, d_child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			spin_lock_nested(&child->d_lock, depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				if (d_inode(child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 					get_derived_permission(dentry, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					fixup_tmp_permissions(d_inode(child));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 					spin_unlock(&child->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			spin_unlock(&child->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	} else if (descendant_may_need_fixup(info->data, limit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		list_for_each_entry(child, &dentry->d_subdirs, d_child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			__fixup_perms_recursive(child, limit, depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	__fixup_perms_recursive(dentry, limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* main function for updating derived permission */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) inline void update_derived_permission_lock(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (!dentry || !d_inode(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		pr_err("sdcardfs: %s: invalid dentry\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* FIXME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * 1. need to check whether the dentry is updated or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * 2. remove the root dentry update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (!IS_ROOT(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			get_derived_permission(parent, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	fixup_tmp_permissions(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int need_graft_path(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct dentry *parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct qstr obb = QSTR_LITERAL("obb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (!sbi->options.unshared_obb &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			parent_info->data->perm == PERM_ANDROID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			qstr_case_eq(&dentry->d_name, &obb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		/* /Android/obb is the base obbpath of DERIVED_UNIFIED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (!(sbi->options.multiuser == false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 				&& parent_info->data->userid == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int is_obbpath_invalid(struct dentry *dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	char *path_buf, *obbpath_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	int need_put = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/* check the base obbpath has been changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * this routine can check an uninitialized obb dentry as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * regarding the uninitialized obb, refer to the sdcardfs_mkdir()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	spin_lock(&di->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (di->orig_path.dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (!di->lower_path.dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			path_get(&di->lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			if (!path_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 				ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 				pr_err("sdcardfs: fail to allocate path_buf in %s.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 				if (d_unhashed(di->lower_path.dentry) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 					!str_case_eq(sbi->obbpath_s, obbpath_s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 					ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 				kfree(path_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			pathcpy(&lower_path, &di->lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			need_put = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	spin_unlock(&di->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (need_put)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		path_put(&lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) int is_base_obbpath(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct dentry *parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct sdcardfs_inode_info *parent_info = SDCARDFS_I(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct qstr q_obb = QSTR_LITERAL("obb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	spin_lock(&SDCARDFS_D(dentry)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (sbi->options.multiuser) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		if (parent_info->data->perm == PERM_PRE_ROOT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 				qstr_case_eq(&dentry->d_name, &q_obb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	} else  if (parent_info->data->perm == PERM_ANDROID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			qstr_case_eq(&dentry->d_name, &q_obb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	spin_unlock(&SDCARDFS_D(dentry)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* The lower_path will be stored to the dentry's orig_path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  * and the base obbpath will be copyed to the lower_path variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  * if an error returned, there's no change in the lower_path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  * returns: -ERRNO if error (0: no error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct path obbpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	/* A local obb dentry must have its own orig_path to support rmdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * and mkdir of itself. Usually, we expect that the sbi->obbpath
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 * is avaiable on this stage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	sdcardfs_set_orig_path(dentry, lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	err = kern_path(sbi->obbpath_s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		/* the obbpath base has been found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		pathcpy(lower_path, &obbpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		/* if the sbi->obbpath is not available, we can optionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		 * setup the lower_path with its orig_path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		 * but, the current implementation just returns an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		 * because the sdcard daemon also regards this case as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		 * a lookup fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		pr_info("sdcardfs: the sbi->obbpath is not available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)