Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/sdcardfs.h |
| 3 | * |
| 4 | * The sdcardfs v2.0 |
| 5 | * This file system replaces the sdcard daemon on Android |
| 6 | * On version 2.0, some of the daemon functions have been ported |
| 7 | * to support the multi-user concepts of Android 4.4 |
| 8 | * |
| 9 | * Copyright (c) 2013 Samsung Electronics Co. Ltd |
| 10 | * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun, |
| 11 | * Sunghwan Yun, Sungjong Seo |
| 12 | * |
| 13 | * This program has been developed as a stackable file system based on |
| 14 | * the WrapFS which written by |
| 15 | * |
| 16 | * Copyright (c) 1998-2011 Erez Zadok |
| 17 | * Copyright (c) 2009 Shrikar Archak |
| 18 | * Copyright (c) 2003-2011 Stony Brook University |
| 19 | * Copyright (c) 2003-2011 The Research Foundation of SUNY |
| 20 | * |
| 21 | * This file is dual licensed. It may be redistributed and/or modified |
| 22 | * under the terms of the Apache 2.0 License OR version 2 of the GNU |
| 23 | * General Public License. |
| 24 | */ |
| 25 | |
| 26 | #ifndef _SDCARDFS_H_ |
| 27 | #define _SDCARDFS_H_ |
| 28 | |
| 29 | #include <linux/dcache.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/fs.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/mount.h> |
| 34 | #include <linux/namei.h> |
| 35 | #include <linux/seq_file.h> |
| 36 | #include <linux/statfs.h> |
| 37 | #include <linux/fs_stack.h> |
| 38 | #include <linux/magic.h> |
| 39 | #include <linux/uaccess.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/sched.h> |
| 42 | #include <linux/types.h> |
| 43 | #include <linux/security.h> |
| 44 | #include <linux/string.h> |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 45 | #include <linux/list.h> |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 46 | #include "multiuser.h" |
| 47 | |
| 48 | /* the file system name */ |
| 49 | #define SDCARDFS_NAME "sdcardfs" |
| 50 | |
| 51 | /* sdcardfs root inode number */ |
| 52 | #define SDCARDFS_ROOT_INO 1 |
| 53 | |
| 54 | /* useful for tracking code reachability */ |
| 55 | #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__) |
| 56 | |
| 57 | #define SDCARDFS_DIRENT_SIZE 256 |
| 58 | |
| 59 | /* temporary static uid settings for development */ |
| 60 | #define AID_ROOT 0 /* uid for accessing /mnt/sdcard & extSdcard */ |
| 61 | #define AID_MEDIA_RW 1023 /* internal media storage write access */ |
| 62 | |
| 63 | #define AID_SDCARD_RW 1015 /* external storage write access */ |
| 64 | #define AID_SDCARD_R 1028 /* external storage read access */ |
| 65 | #define AID_SDCARD_PICS 1033 /* external storage photos access */ |
| 66 | #define AID_SDCARD_AV 1034 /* external storage audio/video access */ |
| 67 | #define AID_SDCARD_ALL 1035 /* access all users external storage */ |
| 68 | |
| 69 | #define AID_PACKAGE_INFO 1027 |
| 70 | |
| 71 | #define fix_derived_permission(x) \ |
| 72 | do { \ |
| 73 | (x)->i_uid = SDCARDFS_I(x)->d_uid; \ |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 74 | (x)->i_gid = get_gid(SDCARDFS_I(x)); \ |
| 75 | (x)->i_mode = ((x)->i_mode & S_IFMT) | get_mode(SDCARDFS_I(x));\ |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 76 | } while (0) |
| 77 | |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 78 | /* Android 5.0 support */ |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 79 | |
| 80 | /* Permission mode for a specific node. Controls how file permissions |
| 81 | * are derived for children nodes. */ |
| 82 | typedef enum { |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 83 | /* Nothing special; this node should just inherit from its parent. */ |
| 84 | PERM_INHERIT, |
| 85 | /* This node is one level above a normal root; used for legacy layouts |
| 86 | * which use the first level to represent user_id. */ |
| 87 | PERM_PRE_ROOT, |
| 88 | /* This node is "/" */ |
| 89 | PERM_ROOT, |
| 90 | /* This node is "/Android" */ |
| 91 | PERM_ANDROID, |
| 92 | /* This node is "/Android/data" */ |
| 93 | PERM_ANDROID_DATA, |
| 94 | /* This node is "/Android/obb" */ |
| 95 | PERM_ANDROID_OBB, |
| 96 | /* This node is "/Android/media" */ |
| 97 | PERM_ANDROID_MEDIA, |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 98 | } perm_t; |
| 99 | |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 100 | struct sdcardfs_sb_info; |
| 101 | struct sdcardfs_mount_options; |
| 102 | |
| 103 | /* Do not directly use this function. Use OVERRIDE_CRED() instead. */ |
| 104 | const struct cred * override_fsids(struct sdcardfs_sb_info* sbi); |
| 105 | /* Do not directly use this function, use REVERT_CRED() instead. */ |
| 106 | void revert_fsids(const struct cred * old_cred); |
| 107 | |
| 108 | /* operations vectors defined in specific files */ |
| 109 | extern const struct file_operations sdcardfs_main_fops; |
| 110 | extern const struct file_operations sdcardfs_dir_fops; |
| 111 | extern const struct inode_operations sdcardfs_main_iops; |
| 112 | extern const struct inode_operations sdcardfs_dir_iops; |
| 113 | extern const struct inode_operations sdcardfs_symlink_iops; |
| 114 | extern const struct super_operations sdcardfs_sops; |
| 115 | extern const struct dentry_operations sdcardfs_ci_dops; |
| 116 | extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops; |
| 117 | extern const struct vm_operations_struct sdcardfs_vm_ops; |
| 118 | |
| 119 | extern int sdcardfs_init_inode_cache(void); |
| 120 | extern void sdcardfs_destroy_inode_cache(void); |
| 121 | extern int sdcardfs_init_dentry_cache(void); |
| 122 | extern void sdcardfs_destroy_dentry_cache(void); |
| 123 | extern int new_dentry_private_data(struct dentry *dentry); |
| 124 | extern void free_dentry_private_data(struct dentry *dentry); |
| 125 | extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry, |
| 126 | struct nameidata *nd); |
fluxi | 41b26d8 | 2016-07-29 18:52:13 +0200 | [diff] [blame] | 127 | extern struct inode *sdcardfs_iget(struct super_block *sb, |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 128 | struct inode *lower_inode, userid_t id); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 129 | extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb, |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 130 | struct path *lower_path, userid_t id); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 131 | |
| 132 | /* file private data */ |
| 133 | struct sdcardfs_file_info { |
| 134 | struct file *lower_file; |
| 135 | const struct vm_operations_struct *lower_vm_ops; |
| 136 | }; |
| 137 | |
| 138 | /* sdcardfs inode data in memory */ |
| 139 | struct sdcardfs_inode_info { |
| 140 | struct inode *lower_inode; |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 141 | /* state derived based on current position in hierachy */ |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 142 | perm_t perm; |
| 143 | userid_t userid; |
| 144 | uid_t d_uid; |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 145 | bool under_android; |
Daniel Rosenberg | b07ed40 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 146 | /* top folder for ownership */ |
| 147 | struct inode *top; |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 148 | |
| 149 | struct inode vfs_inode; |
| 150 | }; |
| 151 | |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 152 | |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 153 | /* sdcardfs dentry data in memory */ |
| 154 | struct sdcardfs_dentry_info { |
| 155 | spinlock_t lock; /* protects lower_path */ |
| 156 | struct path lower_path; |
| 157 | struct path orig_path; |
| 158 | }; |
| 159 | |
| 160 | struct sdcardfs_mount_options { |
| 161 | uid_t fs_low_uid; |
| 162 | gid_t fs_low_gid; |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 163 | userid_t fs_user_id; |
| 164 | gid_t gid; |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 165 | mode_t mask; |
| 166 | bool multiuser; |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 167 | unsigned int reserved_mb; |
| 168 | }; |
| 169 | |
| 170 | /* sdcardfs super-block data in memory */ |
| 171 | struct sdcardfs_sb_info { |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 172 | struct super_block *sb; |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 173 | struct super_block *lower_sb; |
| 174 | /* derived perm policy : some of options have been added |
| 175 | * to sdcardfs_mount_options (Android 4.4 support) */ |
| 176 | struct sdcardfs_mount_options options; |
| 177 | spinlock_t lock; /* protects obbpath */ |
| 178 | char *obbpath_s; |
| 179 | struct path obbpath; |
| 180 | void *pkgl_id; |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 181 | struct list_head list; |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * inode to private data |
| 186 | * |
| 187 | * Since we use containers and the struct inode is _inside_ the |
| 188 | * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL |
| 189 | * inode pointer), return a valid non-NULL pointer. |
| 190 | */ |
| 191 | static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode) |
| 192 | { |
| 193 | return container_of(inode, struct sdcardfs_inode_info, vfs_inode); |
| 194 | } |
| 195 | |
| 196 | /* dentry to private data */ |
| 197 | #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata) |
| 198 | |
| 199 | /* superblock to private data */ |
| 200 | #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info) |
| 201 | |
| 202 | /* file to private Data */ |
| 203 | #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data)) |
| 204 | |
| 205 | /* file to lower file */ |
| 206 | static inline struct file *sdcardfs_lower_file(const struct file *f) |
| 207 | { |
| 208 | return SDCARDFS_F(f)->lower_file; |
| 209 | } |
| 210 | |
| 211 | static inline void sdcardfs_set_lower_file(struct file *f, struct file *val) |
| 212 | { |
| 213 | SDCARDFS_F(f)->lower_file = val; |
| 214 | } |
| 215 | |
| 216 | /* inode to lower inode. */ |
| 217 | static inline struct inode *sdcardfs_lower_inode(const struct inode *i) |
| 218 | { |
| 219 | return SDCARDFS_I(i)->lower_inode; |
| 220 | } |
| 221 | |
| 222 | static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val) |
| 223 | { |
| 224 | SDCARDFS_I(i)->lower_inode = val; |
| 225 | } |
| 226 | |
| 227 | /* superblock to lower superblock */ |
| 228 | static inline struct super_block *sdcardfs_lower_super( |
| 229 | const struct super_block *sb) |
| 230 | { |
| 231 | return SDCARDFS_SB(sb)->lower_sb; |
| 232 | } |
| 233 | |
| 234 | static inline void sdcardfs_set_lower_super(struct super_block *sb, |
| 235 | struct super_block *val) |
| 236 | { |
| 237 | SDCARDFS_SB(sb)->lower_sb = val; |
| 238 | } |
| 239 | |
| 240 | /* path based (dentry/mnt) macros */ |
| 241 | static inline void pathcpy(struct path *dst, const struct path *src) |
| 242 | { |
| 243 | dst->dentry = src->dentry; |
| 244 | dst->mnt = src->mnt; |
| 245 | } |
| 246 | |
| 247 | /* sdcardfs_get_pname functions calls path_get() |
| 248 | * therefore, the caller must call "proper" path_put functions |
| 249 | */ |
| 250 | #define SDCARDFS_DENT_FUNC(pname) \ |
| 251 | static inline void sdcardfs_get_##pname(const struct dentry *dent, \ |
| 252 | struct path *pname) \ |
| 253 | { \ |
| 254 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 255 | pathcpy(pname, &SDCARDFS_D(dent)->pname); \ |
| 256 | path_get(pname); \ |
| 257 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 258 | return; \ |
| 259 | } \ |
| 260 | static inline void sdcardfs_put_##pname(const struct dentry *dent, \ |
| 261 | struct path *pname) \ |
| 262 | { \ |
| 263 | path_put(pname); \ |
| 264 | return; \ |
| 265 | } \ |
| 266 | static inline void sdcardfs_set_##pname(const struct dentry *dent, \ |
| 267 | struct path *pname) \ |
| 268 | { \ |
| 269 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 270 | pathcpy(&SDCARDFS_D(dent)->pname, pname); \ |
| 271 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 272 | return; \ |
| 273 | } \ |
| 274 | static inline void sdcardfs_reset_##pname(const struct dentry *dent) \ |
| 275 | { \ |
| 276 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 277 | SDCARDFS_D(dent)->pname.dentry = NULL; \ |
| 278 | SDCARDFS_D(dent)->pname.mnt = NULL; \ |
| 279 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 280 | return; \ |
| 281 | } \ |
| 282 | static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \ |
| 283 | { \ |
| 284 | struct path pname; \ |
| 285 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 286 | if(SDCARDFS_D(dent)->pname.dentry) { \ |
| 287 | pathcpy(&pname, &SDCARDFS_D(dent)->pname); \ |
| 288 | SDCARDFS_D(dent)->pname.dentry = NULL; \ |
| 289 | SDCARDFS_D(dent)->pname.mnt = NULL; \ |
| 290 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 291 | path_put(&pname); \ |
| 292 | } else \ |
| 293 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 294 | return; \ |
| 295 | } |
| 296 | |
| 297 | SDCARDFS_DENT_FUNC(lower_path) |
| 298 | SDCARDFS_DENT_FUNC(orig_path) |
| 299 | |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 300 | static inline int get_gid(struct sdcardfs_inode_info *info) { |
| 301 | struct sdcardfs_sb_info *sb_info = SDCARDFS_SB(info->vfs_inode.i_sb); |
| 302 | if (sb_info->options.gid == AID_SDCARD_RW) { |
| 303 | /* As an optimization, certain trusted system components only run |
| 304 | * as owner but operate across all users. Since we're now handing |
| 305 | * out the sdcard_rw GID only to trusted apps, we're okay relaxing |
| 306 | * the user boundary enforcement for the default view. The UIDs |
| 307 | * assigned to app directories are still multiuser aware. */ |
| 308 | return AID_SDCARD_RW; |
| 309 | } else { |
| 310 | return multiuser_get_uid(info->userid, sb_info->options.gid); |
| 311 | } |
| 312 | } |
| 313 | static inline int get_mode(struct sdcardfs_inode_info *info) { |
| 314 | int owner_mode; |
| 315 | int filtered_mode; |
| 316 | struct sdcardfs_sb_info *sb_info = SDCARDFS_SB(info->vfs_inode.i_sb); |
| 317 | int visible_mode = 0775 & ~sb_info->options.mask; |
| 318 | |
| 319 | if (info->perm == PERM_PRE_ROOT) { |
| 320 | /* Top of multi-user view should always be visible to ensure |
| 321 | * secondary users can traverse inside. */ |
| 322 | visible_mode = 0711; |
| 323 | } else if (info->under_android) { |
| 324 | /* Block "other" access to Android directories, since only apps |
| 325 | * belonging to a specific user should be in there; we still |
| 326 | * leave +x open for the default view. */ |
| 327 | if (sb_info->options.gid == AID_SDCARD_RW) { |
| 328 | visible_mode = visible_mode & ~0006; |
| 329 | } else { |
| 330 | visible_mode = visible_mode & ~0007; |
| 331 | } |
| 332 | } |
| 333 | owner_mode = info->lower_inode->i_mode & 0700; |
| 334 | filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); |
| 335 | return filtered_mode; |
| 336 | } |
| 337 | |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 338 | static inline int has_graft_path(const struct dentry *dent) |
| 339 | { |
| 340 | int ret = 0; |
| 341 | |
| 342 | spin_lock(&SDCARDFS_D(dent)->lock); |
| 343 | if (SDCARDFS_D(dent)->orig_path.dentry != NULL) |
| 344 | ret = 1; |
| 345 | spin_unlock(&SDCARDFS_D(dent)->lock); |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static inline void sdcardfs_get_real_lower(const struct dentry *dent, |
| 351 | struct path *real_lower) |
| 352 | { |
| 353 | /* in case of a local obb dentry |
| 354 | * the orig_path should be returned |
| 355 | */ |
| 356 | if(has_graft_path(dent)) |
| 357 | sdcardfs_get_orig_path(dent, real_lower); |
| 358 | else |
| 359 | sdcardfs_get_lower_path(dent, real_lower); |
| 360 | } |
| 361 | |
| 362 | static inline void sdcardfs_put_real_lower(const struct dentry *dent, |
| 363 | struct path *real_lower) |
| 364 | { |
| 365 | if(has_graft_path(dent)) |
| 366 | sdcardfs_put_orig_path(dent, real_lower); |
| 367 | else |
| 368 | sdcardfs_put_lower_path(dent, real_lower); |
| 369 | } |
| 370 | |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 371 | extern struct mutex sdcardfs_super_list_lock; |
| 372 | extern struct list_head sdcardfs_super_list; |
| 373 | |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 374 | /* for packagelist.c */ |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 375 | extern appid_t get_appid(void *pkgl_id, const char *app_name); |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 376 | extern int check_caller_access_to_name(struct inode *parent_node, const char* name); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 377 | extern int open_flags_to_access_mode(int open_flags); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 378 | extern int packagelist_init(void); |
| 379 | extern void packagelist_exit(void); |
| 380 | |
| 381 | /* for derived_perm.c */ |
Daniel Rosenberg | b07ed40 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 382 | extern void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid, |
| 383 | uid_t uid, bool under_android, struct inode *top); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 384 | extern void get_derived_permission(struct dentry *parent, struct dentry *dentry); |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 385 | extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry); |
Daniel Rosenberg | b07ed40 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 386 | extern void fixup_top_recursive(struct dentry *parent); |
| 387 | extern void fixup_perms_recursive(struct dentry *dentry, const char *name, size_t len); |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 388 | |
| 389 | extern void update_derived_permission_lock(struct dentry *dentry); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 390 | extern int need_graft_path(struct dentry *dentry); |
| 391 | extern int is_base_obbpath(struct dentry *dentry); |
| 392 | extern int is_obbpath_invalid(struct dentry *dentry); |
| 393 | extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path); |
| 394 | |
| 395 | /* locking helpers */ |
| 396 | static inline struct dentry *lock_parent(struct dentry *dentry) |
| 397 | { |
| 398 | struct dentry *dir = dget_parent(dentry); |
| 399 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 400 | return dir; |
| 401 | } |
| 402 | |
| 403 | static inline void unlock_dir(struct dentry *dir) |
| 404 | { |
| 405 | mutex_unlock(&dir->d_inode->i_mutex); |
| 406 | dput(dir); |
| 407 | } |
| 408 | |
| 409 | static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode) |
| 410 | { |
| 411 | int err; |
| 412 | struct dentry *dent; |
| 413 | struct iattr attrs; |
fluxi | 41b26d8 | 2016-07-29 18:52:13 +0200 | [diff] [blame] | 414 | struct path parent; |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 415 | |
fluxi | 41b26d8 | 2016-07-29 18:52:13 +0200 | [diff] [blame] | 416 | dent = kern_path_locked(path_s, &parent); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 417 | if (IS_ERR(dent)) { |
| 418 | err = PTR_ERR(dent); |
| 419 | if (err == -EEXIST) |
| 420 | err = 0; |
| 421 | goto out_unlock; |
| 422 | } |
| 423 | |
fluxi | 41b26d8 | 2016-07-29 18:52:13 +0200 | [diff] [blame] | 424 | err = vfs_mkdir(parent.dentry->d_inode, dent, mode); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 425 | if (err) { |
| 426 | if (err == -EEXIST) |
| 427 | err = 0; |
| 428 | goto out_dput; |
| 429 | } |
| 430 | |
| 431 | attrs.ia_uid = uid; |
| 432 | attrs.ia_gid = gid; |
| 433 | attrs.ia_valid = ATTR_UID | ATTR_GID; |
| 434 | mutex_lock(&dent->d_inode->i_mutex); |
| 435 | notify_change(dent, &attrs); |
| 436 | mutex_unlock(&dent->d_inode->i_mutex); |
| 437 | |
| 438 | out_dput: |
| 439 | dput(dent); |
| 440 | |
| 441 | out_unlock: |
| 442 | /* parent dentry locked by lookup_create */ |
fluxi | 41b26d8 | 2016-07-29 18:52:13 +0200 | [diff] [blame] | 443 | mutex_unlock(&parent.dentry->d_inode->i_mutex); |
| 444 | path_put(&parent); |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 445 | |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 446 | return err; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Return 1, if a disk has enough free space, otherwise 0. |
| 451 | * We assume that any files can not be overwritten. |
| 452 | */ |
| 453 | static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir) |
| 454 | { |
| 455 | int err; |
| 456 | struct path lower_path; |
| 457 | struct kstatfs statfs; |
| 458 | u64 avail; |
| 459 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 460 | |
| 461 | if (sbi->options.reserved_mb) { |
| 462 | /* Get fs stat of lower filesystem. */ |
| 463 | sdcardfs_get_lower_path(dentry, &lower_path); |
| 464 | err = vfs_statfs(&lower_path, &statfs); |
| 465 | sdcardfs_put_lower_path(dentry, &lower_path); |
| 466 | |
| 467 | if (unlikely(err)) |
| 468 | return 0; |
| 469 | |
| 470 | /* Invalid statfs informations. */ |
| 471 | if (unlikely(statfs.f_bsize == 0)) |
| 472 | return 0; |
| 473 | |
| 474 | /* if you are checking directory, set size to f_bsize. */ |
| 475 | if (unlikely(dir)) |
| 476 | size = statfs.f_bsize; |
| 477 | |
| 478 | /* available size */ |
| 479 | avail = statfs.f_bavail * statfs.f_bsize; |
| 480 | |
| 481 | /* not enough space */ |
| 482 | if ((u64)size > avail) |
| 483 | return 0; |
| 484 | |
| 485 | /* enough space */ |
| 486 | if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024)) |
| 487 | return 1; |
| 488 | |
| 489 | return 0; |
| 490 | } else |
| 491 | return 1; |
| 492 | } |
| 493 | |
Daniel Rosenberg | 95e1721 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 494 | /* Copies attrs and maintains sdcardfs managed attrs */ |
| 495 | static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src) |
| 496 | { |
| 497 | dest->i_mode = (src->i_mode & S_IFMT) | get_mode(SDCARDFS_I(dest)); |
| 498 | dest->i_uid = SDCARDFS_I(dest)->d_uid; |
| 499 | dest->i_gid = get_gid(SDCARDFS_I(dest)); |
| 500 | dest->i_rdev = src->i_rdev; |
| 501 | dest->i_atime = src->i_atime; |
| 502 | dest->i_mtime = src->i_mtime; |
| 503 | dest->i_ctime = src->i_ctime; |
| 504 | dest->i_blkbits = src->i_blkbits; |
| 505 | dest->i_flags = src->i_flags; |
| 506 | set_nlink(dest, src->i_nlink); |
| 507 | } |
Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 508 | #endif /* not _SDCARDFS_H_ */ |