^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * fs/sdcardfs/inode.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) #include <linux/fs_struct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct sdcardfs_inode_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) cred = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (sbi->options.gid_derivation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (data->under_obb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) uid = AID_MEDIA_OBB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) uid = multiuser_get_uid(data->userid, sbi->options.fs_low_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) uid = sbi->options.fs_low_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) cred->fsuid = make_kuid(&init_user_ns, uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) old_cred = override_creds(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void revert_fsids(const struct cred *old_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const struct cred *cur_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) cur_cred = current->cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) put_cred(cur_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) umode_t mode, bool want_excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct vfsmount *lower_dentry_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct dentry *lower_parent_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct fs_struct *saved_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct fs_struct *copied_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!check_caller_access_to_name(dir, &dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto out_eacces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) SDCARDFS_I(dir)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) lower_dentry_mnt = lower_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) lower_parent_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (d_is_positive(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* set last 16bytes of mode field to 0664 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mode = (mode & S_IFMT) | 00664;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* temporarily change umask for lower fs write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) saved_fs = current->fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) copied_fs = copy_fs_struct(current->fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!copied_fs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) copied_fs->umask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) task_lock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) current->fs = copied_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) task_unlock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) SDCARDFS_I(dir)->data->userid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) fixup_lower_ownership(dentry, dentry->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) task_lock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) current->fs = saved_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) task_unlock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) free_fs_struct(copied_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unlock_dir(lower_parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) out_eacces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct vfsmount *lower_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!check_caller_access_to_name(dir, &dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) goto out_eacces;
^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) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) SDCARDFS_I(dir)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) lower_mnt = lower_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) dget(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Note: unlinking on top of NFS can cause silly-renamed files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * Trying to delete such files results in EBUSY from NFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * below. Silly-renamed files will get deleted by NFS later on, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * we just need to detect them here and treat such EBUSY errors as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * if the upper file was successfully deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) fsstack_copy_attr_times(dir, lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) fsstack_copy_inode_size(dir, lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) set_nlink(d_inode(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) d_inode(dentry)->i_ctime = dir->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dput(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) out_eacces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int touch(char *abs_path, mode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (IS_ERR(filp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (PTR_ERR(filp) == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pr_err("sdcardfs: failed to open(%s): %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) abs_path, PTR_ERR(filp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return PTR_ERR(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) filp_close(filp, current->files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int make_nomedia_in_obb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct vfsmount *lower_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct dentry *lower_parent_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct dentry *parent_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct sdcardfs_inode_data *pd = SDCARDFS_I(dir)->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int touch_err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct fs_struct *saved_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct fs_struct *copied_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct qstr q_obb = QSTR_LITERAL("obb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct qstr q_data = QSTR_LITERAL("data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!check_caller_access_to_name(dir, &dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto out_eacces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) SDCARDFS_I(dir)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* check disk space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) parent_dentry = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (!check_min_free_space(parent_dentry, 0, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pr_err("sdcardfs: No minimum free space.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dput(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) goto out_revert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) dput(parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* the lower_dentry is negative here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) lower_mnt = lower_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) lower_parent_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* set last 16bytes of mode field to 0775 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) mode = (mode & S_IFMT) | 00775;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* temporarily change umask for lower fs write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) saved_fs = current->fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) copied_fs = copy_fs_struct(current->fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!copied_fs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unlock_dir(lower_parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) copied_fs->umask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) task_lock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) current->fs = copied_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) task_unlock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) unlock_dir(lower_parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* if it is a local obb dentry, setup it with the base obbpath */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (need_graft_path(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err = setup_obb_dentry(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* if the sbi->obbpath is not available, the lower_path won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * changed by setup_obb_dentry() but the lower path is saved to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * its orig_path. this dentry will be revalidated later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * but now, the lower_path should be NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) sdcardfs_put_reset_lower_path(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* the newly created lower path which saved to its orig_path or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * the lower_path is the base obbpath.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * therefore, an additional path_get is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) path_get(&lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) make_nomedia_in_obb = 1;
^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) err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unlock_dir(lower_parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* update number of links on parent directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fixup_lower_ownership(dentry, dentry->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unlock_dir(lower_parent_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) && (pd->perm == PERM_ANDROID) && (pd->userid == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) make_nomedia_in_obb = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* When creating /Android/data and /Android/obb, mark them as .nomedia */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (make_nomedia_in_obb ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ((pd->perm == PERM_ANDROID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) && (qstr_case_eq(&dentry->d_name, &q_data)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) saved_cred = override_fsids(sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) SDCARDFS_I(d_inode(dentry))->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!saved_cred) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) pr_err("sdcardfs: failed to set up .nomedia in %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) lower_path.dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) -ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) set_fs_pwd(current->fs, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) touch_err = touch(".nomedia", 0664);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (touch_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) lower_path.dentry->d_name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) touch_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) goto out;
^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) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) task_lock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) current->fs = saved_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) task_unlock(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) free_fs_struct(copied_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) out_revert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) out_eacces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct vfsmount *lower_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!check_caller_access_to_name(dir, &dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) goto out_eacces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) SDCARDFS_I(dir)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * the dentry on the original path should be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) sdcardfs_get_real_lower(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) lower_mnt = lower_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (d_inode(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) clear_nlink(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) sdcardfs_put_real_lower(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) out_eacces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * The locking rules in sdcardfs_rename are complex. We could use a simpler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * superblock-level name-space lock for renames and copy-ups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct dentry *lower_old_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct dentry *lower_new_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct dentry *lower_old_dir_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct dentry *lower_new_dir_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct vfsmount *lower_mnt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct dentry *trap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct path lower_old_path, lower_new_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!check_caller_access_to_name(old_dir, &old_dentry->d_name) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) !check_caller_access_to_name(new_dir, &new_dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out_eacces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) saved_cred = override_fsids(SDCARDFS_SB(old_dir->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) SDCARDFS_I(new_dir)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) sdcardfs_get_real_lower(old_dentry, &lower_old_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) sdcardfs_get_lower_path(new_dentry, &lower_new_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) lower_old_dentry = lower_old_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) lower_new_dentry = lower_new_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) lower_mnt = lower_old_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) lower_old_dir_dentry = dget_parent(lower_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) lower_new_dir_dentry = dget_parent(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /* source should not be ancestor of target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (trap == lower_old_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /* target should not be ancestor of source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (trap == lower_new_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) err = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) err = vfs_rename2(lower_mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) d_inode(lower_old_dir_dentry), lower_old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) d_inode(lower_new_dir_dentry), lower_new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* Copy attrs from lower dir, but i_uid/i_gid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (new_dir != old_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) fixup_tmp_permissions(d_inode(old_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) d_invalidate(old_dentry); /* Can't fixup ownership recursively :( */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dput(lower_old_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) dput(lower_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) sdcardfs_put_real_lower(old_dentry, &lower_old_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) sdcardfs_put_lower_path(new_dentry, &lower_new_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) out_eacces:
^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) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /* XXX readlink does not requires overriding credential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!d_inode(lower_dentry)->i_op ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) !d_inode(lower_dentry)->i_op->readlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) buf, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) int len = PAGE_SIZE, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* This is freed by the put_link method assuming a successful call. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) buf = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) buf = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* read the symlink, and then we will follow it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) old_fs = get_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) set_fs(KERNEL_DS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) err = sdcardfs_readlink(dentry, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) set_fs(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) buf = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) buf[err] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return *cookie = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static int sdcardfs_permission_wrn(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) void copy_attrs(struct inode *dest, const struct inode *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) dest->i_mode = src->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) dest->i_uid = src->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) dest->i_gid = src->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) dest->i_rdev = src->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) dest->i_atime = src->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) dest->i_mtime = src->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) dest->i_ctime = src->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) dest->i_blkbits = src->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dest->i_flags = src->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) #ifdef CONFIG_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) dest->i_acl = src->i_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) #ifdef CONFIG_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) dest->i_security = src->i_security;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct inode tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (IS_ERR(mnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return PTR_ERR(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (!top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * Permission check on sdcardfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Calling process should have AID_SDCARD_RW permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * Since generic_permission only needs i_mode, i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * i_gid, and i_sb, we can create a fake inode to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * this information down in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * The underlying code may attempt to take locks in some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * cases for features we're not using, but if that changes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * locks must be dealt with to avoid undefined behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) copy_attrs(&tmp, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, inode->i_sb, top));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) tmp.i_mode = (inode->i_mode & S_IFMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) | get_mode(mnt, SDCARDFS_I(inode), top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) data_put(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) tmp.i_sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (IS_POSIXACL(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) pr_warn("%s: This may be undefined behavior...\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) err = generic_permission(&tmp, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct vfsmount *lower_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct inode *lower_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct iattr lower_ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct inode tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct dentry tmp_d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct sdcardfs_inode_data *top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) const struct cred *saved_cred = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) top = top_data_get(SDCARDFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (!top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * Permission check on sdcardfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * Calling process should have AID_SDCARD_RW permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * Since generic_permission only needs i_mode, i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * i_gid, and i_sb, we can create a fake inode to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * this information down in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * The underlying code may attempt to take locks in some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * cases for features we're not using, but if that changes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * locks must be dealt with to avoid undefined behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) copy_attrs(&tmp, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, dentry->d_sb, top));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) tmp.i_mode = (inode->i_mode & S_IFMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) | get_mode(mnt, SDCARDFS_I(inode), top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) tmp.i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) data_put(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) tmp.i_sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) tmp_d.d_inode = &tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * Check if user has permission to change dentry. We don't check if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * this user can change the lower inode: that should happen when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * calling notify_change on the lower inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /* prepare our own lower struct iattr (with the lower file) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) memcpy(&lower_ia, ia, sizeof(lower_ia));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) /* Allow touch updating timestamps. A previous permission check ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * we have write access. Changes to mode, owner, and group are ignored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) ia->ia_valid |= ATTR_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) err = setattr_prepare(&tmp_d, ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /* check the Android group ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) /* save current_cred and override it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) saved_cred = override_fsids(SDCARDFS_SB(dentry->d_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) SDCARDFS_I(inode)->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (!saved_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) lower_dentry = lower_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) lower_mnt = lower_path.mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) lower_inode = sdcardfs_lower_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (ia->ia_valid & ATTR_FILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * If shrinking, first truncate upper level to cancel writing dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * pages beyond the new eof; and also if its' maxbytes is more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * limiting (fail with -EFBIG before making any change to the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * level). There is no need to vmtruncate the upper level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * afterwards in the other cases: we fsstack_copy_inode_size from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * the lower level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (ia->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) err = inode_newsize_ok(&tmp, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) truncate_setsize(inode, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * mode change is for clearing setuid/setgid bits. Allow lower fs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * to interpret this in its own way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) lower_ia.ia_valid &= ~ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* notify the (possibly copied-up) lower inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * Note: we use d_inode(lower_dentry), because lower_inode may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * unlinked (no inode->i_sb and i_ino==0. This happens if someone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * tries to open(), unlink(), then ftruncate() a file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) inode_lock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) inode_unlock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /* get attributes from the lower inode and update derived permissions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) sdcardfs_copy_and_fix_attrs(inode, lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * Not running fsstack_copy_inode_size(inode, lower_inode), because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * VFS should update our inode size, and notify_change on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * lower_inode should update its size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) revert_fsids(saved_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static int sdcardfs_fillattr(struct vfsmount *mnt, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) struct kstat *lower_stat, struct kstat *stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct sdcardfs_inode_data *top = top_data_get(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) stat->dev = inode->i_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) stat->ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) stat->mode = (inode->i_mode & S_IFMT) | get_mode(mnt, info, top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) stat->nlink = inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) stat->uid = make_kuid(&init_user_ns, top->d_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) stat->gid = make_kgid(&init_user_ns, get_gid(mnt, sb, top));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) stat->rdev = inode->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) stat->size = lower_stat->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) stat->atime = lower_stat->atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) stat->mtime = lower_stat->mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) stat->ctime = lower_stat->ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) stat->blksize = lower_stat->blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) stat->blocks = lower_stat->blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) data_put(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static int sdcardfs_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct vfsmount *mnt = path->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct kstat lower_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct path lower_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) sdcardfs_get_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) err = vfs_getattr(&lower_path, &lower_stat, request_mask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) sdcardfs_copy_and_fix_attrs(d_inode(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) d_inode(lower_path.dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) err = sdcardfs_fillattr(mnt, d_inode(dentry), &lower_stat, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) sdcardfs_put_lower_path(dentry, &lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) const struct inode_operations sdcardfs_symlink_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) .permission2 = sdcardfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) .setattr2 = sdcardfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) /* XXX Following operations are implemented,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * but FUSE(sdcard) or FAT does not support them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * These methods are *NOT* perfectly tested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) .readlink = sdcardfs_readlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) .follow_link = sdcardfs_follow_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) .put_link = kfree_put_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) const struct inode_operations sdcardfs_dir_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) .create = sdcardfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) .lookup = sdcardfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) .permission = sdcardfs_permission_wrn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) .permission2 = sdcardfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) .unlink = sdcardfs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .mkdir = sdcardfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .rmdir = sdcardfs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .rename = sdcardfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) .setattr = sdcardfs_setattr_wrn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) .setattr2 = sdcardfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) .getattr = sdcardfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) const struct inode_operations sdcardfs_main_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) .permission = sdcardfs_permission_wrn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) .permission2 = sdcardfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) .setattr = sdcardfs_setattr_wrn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) .setattr2 = sdcardfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) .getattr = sdcardfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) };