blob: 66a97ef8d2615969a1be4f6ffe4c432c0252ae39 [file] [log] [blame]
Daniel Campello35c9e242015-07-20 16:23:50 -07001/*
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 Rosenberg497ac902016-02-03 21:08:21 -080045#include <linux/list.h>
Daniel Campello35c9e242015-07-20 16:23:50 -070046#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
Daniel Rosenberg90219272016-10-26 20:27:20 -070071
72/*
73 * Permissions are handled by our permission function.
74 * We don't want anyone who happens to look at our inode value to prematurely
75 * block access, so store more permissive values. These are probably never
76 * used.
77 */
78#define fixup_tmp_permissions(x) \
Daniel Campello35c9e242015-07-20 16:23:50 -070079 do { \
Daniel Campellod1d080c2015-07-20 16:27:37 -070080 (x)->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(x)->d_uid); \
Daniel Rosenberg90219272016-10-26 20:27:20 -070081 (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); \
82 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
Daniel Campello35c9e242015-07-20 16:23:50 -070083 } while (0)
84
85/* OVERRIDE_CRED() and REVERT_CRED()
86 * OVERRID_CRED()
87 * backup original task->cred
88 * and modifies task->cred->fsuid/fsgid to specified value.
89 * REVERT_CRED()
90 * restore original task->cred->fsuid/fsgid.
91 * These two macro should be used in pair, and OVERRIDE_CRED() should be
92 * placed at the beginning of a function, right after variable declaration.
93 */
94#define OVERRIDE_CRED(sdcardfs_sbi, saved_cred) \
95 saved_cred = override_fsids(sdcardfs_sbi); \
96 if (!saved_cred) { return -ENOMEM; }
97
98#define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred) \
99 saved_cred = override_fsids(sdcardfs_sbi); \
100 if (!saved_cred) { return ERR_PTR(-ENOMEM); }
101
102#define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
103
104#define DEBUG_CRED() \
105 printk("KAKJAGI: %s:%d fsuid %d fsgid %d\n", \
106 __FUNCTION__, __LINE__, \
107 (int)current->cred->fsuid, \
108 (int)current->cred->fsgid);
109
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800110/* Android 5.0 support */
Daniel Campello35c9e242015-07-20 16:23:50 -0700111
112/* Permission mode for a specific node. Controls how file permissions
113 * are derived for children nodes. */
114typedef enum {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800115 /* Nothing special; this node should just inherit from its parent. */
116 PERM_INHERIT,
117 /* This node is one level above a normal root; used for legacy layouts
118 * which use the first level to represent user_id. */
119 PERM_PRE_ROOT,
120 /* This node is "/" */
121 PERM_ROOT,
122 /* This node is "/Android" */
123 PERM_ANDROID,
124 /* This node is "/Android/data" */
125 PERM_ANDROID_DATA,
126 /* This node is "/Android/obb" */
127 PERM_ANDROID_OBB,
128 /* This node is "/Android/media" */
129 PERM_ANDROID_MEDIA,
Daniel Campello35c9e242015-07-20 16:23:50 -0700130} perm_t;
131
Daniel Campello35c9e242015-07-20 16:23:50 -0700132struct sdcardfs_sb_info;
133struct sdcardfs_mount_options;
134
135/* Do not directly use this function. Use OVERRIDE_CRED() instead. */
136const struct cred * override_fsids(struct sdcardfs_sb_info* sbi);
137/* Do not directly use this function, use REVERT_CRED() instead. */
138void revert_fsids(const struct cred * old_cred);
139
140/* operations vectors defined in specific files */
141extern const struct file_operations sdcardfs_main_fops;
142extern const struct file_operations sdcardfs_dir_fops;
143extern const struct inode_operations sdcardfs_main_iops;
144extern const struct inode_operations sdcardfs_dir_iops;
145extern const struct inode_operations sdcardfs_symlink_iops;
146extern const struct super_operations sdcardfs_sops;
147extern const struct dentry_operations sdcardfs_ci_dops;
148extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
149extern const struct vm_operations_struct sdcardfs_vm_ops;
150
151extern int sdcardfs_init_inode_cache(void);
152extern void sdcardfs_destroy_inode_cache(void);
153extern int sdcardfs_init_dentry_cache(void);
154extern void sdcardfs_destroy_dentry_cache(void);
155extern int new_dentry_private_data(struct dentry *dentry);
156extern void free_dentry_private_data(struct dentry *dentry);
157extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
Daniel Campellod1d080c2015-07-20 16:27:37 -0700158 unsigned int flags);
159extern struct inode *sdcardfs_iget(struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800160 struct inode *lower_inode, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700161extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800162 struct path *lower_path, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700163
164/* file private data */
165struct sdcardfs_file_info {
166 struct file *lower_file;
167 const struct vm_operations_struct *lower_vm_ops;
168};
169
170/* sdcardfs inode data in memory */
171struct sdcardfs_inode_info {
172 struct inode *lower_inode;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800173 /* state derived based on current position in hierachy */
Daniel Campello35c9e242015-07-20 16:23:50 -0700174 perm_t perm;
175 userid_t userid;
176 uid_t d_uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800177 bool under_android;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700178 /* top folder for ownership */
179 struct inode *top;
Daniel Campello35c9e242015-07-20 16:23:50 -0700180
181 struct inode vfs_inode;
182};
183
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800184
Daniel Campello35c9e242015-07-20 16:23:50 -0700185/* sdcardfs dentry data in memory */
186struct sdcardfs_dentry_info {
187 spinlock_t lock; /* protects lower_path */
188 struct path lower_path;
189 struct path orig_path;
190};
191
192struct sdcardfs_mount_options {
193 uid_t fs_low_uid;
194 gid_t fs_low_gid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800195 userid_t fs_user_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800196 bool multiuser;
Daniel Campello35c9e242015-07-20 16:23:50 -0700197 unsigned int reserved_mb;
198};
199
Daniel Rosenberg317e7702016-10-26 17:36:05 -0700200struct sdcardfs_vfsmount_options {
201 gid_t gid;
202 mode_t mask;
203};
204
205extern int parse_options_remount(struct super_block *sb, char *options, int silent,
206 struct sdcardfs_vfsmount_options *vfsopts);
207
Daniel Campello35c9e242015-07-20 16:23:50 -0700208/* sdcardfs super-block data in memory */
209struct sdcardfs_sb_info {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800210 struct super_block *sb;
Daniel Campello35c9e242015-07-20 16:23:50 -0700211 struct super_block *lower_sb;
212 /* derived perm policy : some of options have been added
213 * to sdcardfs_mount_options (Android 4.4 support) */
214 struct sdcardfs_mount_options options;
215 spinlock_t lock; /* protects obbpath */
216 char *obbpath_s;
217 struct path obbpath;
218 void *pkgl_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800219 struct list_head list;
Daniel Campello35c9e242015-07-20 16:23:50 -0700220};
221
222/*
223 * inode to private data
224 *
225 * Since we use containers and the struct inode is _inside_ the
226 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
227 * inode pointer), return a valid non-NULL pointer.
228 */
229static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
230{
231 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
232}
233
234/* dentry to private data */
235#define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
236
237/* superblock to private data */
238#define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
239
240/* file to private Data */
241#define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
242
243/* file to lower file */
244static inline struct file *sdcardfs_lower_file(const struct file *f)
245{
246 return SDCARDFS_F(f)->lower_file;
247}
248
249static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
250{
251 SDCARDFS_F(f)->lower_file = val;
252}
253
254/* inode to lower inode. */
255static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
256{
257 return SDCARDFS_I(i)->lower_inode;
258}
259
260static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
261{
262 SDCARDFS_I(i)->lower_inode = val;
263}
264
265/* superblock to lower superblock */
266static inline struct super_block *sdcardfs_lower_super(
267 const struct super_block *sb)
268{
269 return SDCARDFS_SB(sb)->lower_sb;
270}
271
272static inline void sdcardfs_set_lower_super(struct super_block *sb,
273 struct super_block *val)
274{
275 SDCARDFS_SB(sb)->lower_sb = val;
276}
277
278/* path based (dentry/mnt) macros */
279static inline void pathcpy(struct path *dst, const struct path *src)
280{
281 dst->dentry = src->dentry;
282 dst->mnt = src->mnt;
283}
284
285/* sdcardfs_get_pname functions calls path_get()
286 * therefore, the caller must call "proper" path_put functions
287 */
288#define SDCARDFS_DENT_FUNC(pname) \
289static inline void sdcardfs_get_##pname(const struct dentry *dent, \
290 struct path *pname) \
291{ \
292 spin_lock(&SDCARDFS_D(dent)->lock); \
293 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
294 path_get(pname); \
295 spin_unlock(&SDCARDFS_D(dent)->lock); \
296 return; \
297} \
298static inline void sdcardfs_put_##pname(const struct dentry *dent, \
299 struct path *pname) \
300{ \
301 path_put(pname); \
302 return; \
303} \
304static inline void sdcardfs_set_##pname(const struct dentry *dent, \
305 struct path *pname) \
306{ \
307 spin_lock(&SDCARDFS_D(dent)->lock); \
308 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
309 spin_unlock(&SDCARDFS_D(dent)->lock); \
310 return; \
311} \
312static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
313{ \
314 spin_lock(&SDCARDFS_D(dent)->lock); \
315 SDCARDFS_D(dent)->pname.dentry = NULL; \
316 SDCARDFS_D(dent)->pname.mnt = NULL; \
317 spin_unlock(&SDCARDFS_D(dent)->lock); \
318 return; \
319} \
320static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
321{ \
322 struct path pname; \
323 spin_lock(&SDCARDFS_D(dent)->lock); \
324 if(SDCARDFS_D(dent)->pname.dentry) { \
325 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
326 SDCARDFS_D(dent)->pname.dentry = NULL; \
327 SDCARDFS_D(dent)->pname.mnt = NULL; \
328 spin_unlock(&SDCARDFS_D(dent)->lock); \
329 path_put(&pname); \
330 } else \
331 spin_unlock(&SDCARDFS_D(dent)->lock); \
332 return; \
333}
334
335SDCARDFS_DENT_FUNC(lower_path)
336SDCARDFS_DENT_FUNC(orig_path)
337
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700338/* grab a refererence if we aren't linking to ourself */
339static inline void set_top(struct sdcardfs_inode_info *info, struct inode *top)
340{
341 struct inode *old_top = NULL;
342 BUG_ON(IS_ERR_OR_NULL(top));
343 if (info->top && info->top != &info->vfs_inode) {
344 old_top = info->top;
345 }
346 if (top != &info->vfs_inode)
347 igrab(top);
348 info->top = top;
349 iput(old_top);
350}
351
352static inline struct inode *grab_top(struct sdcardfs_inode_info *info)
353{
354 struct inode *top = info->top;
355 if (top) {
356 return igrab(top);
357 } else {
358 return NULL;
359 }
360}
361
362static inline void release_top(struct sdcardfs_inode_info *info)
363{
364 iput(info->top);
365}
366
Daniel Rosenberg90219272016-10-26 20:27:20 -0700367static inline int get_gid(struct vfsmount *mnt, struct sdcardfs_inode_info *info) {
368 struct sdcardfs_vfsmount_options *opts = mnt->data;
369
370 if (opts->gid == AID_SDCARD_RW) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800371 /* As an optimization, certain trusted system components only run
372 * as owner but operate across all users. Since we're now handing
373 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
374 * the user boundary enforcement for the default view. The UIDs
375 * assigned to app directories are still multiuser aware. */
376 return AID_SDCARD_RW;
377 } else {
Daniel Rosenberg90219272016-10-26 20:27:20 -0700378 return multiuser_get_uid(info->userid, opts->gid);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800379 }
380}
Daniel Rosenberg90219272016-10-26 20:27:20 -0700381static inline int get_mode(struct vfsmount *mnt, struct sdcardfs_inode_info *info) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800382 int owner_mode;
383 int filtered_mode;
Daniel Rosenberg90219272016-10-26 20:27:20 -0700384 struct sdcardfs_vfsmount_options *opts = mnt->data;
385 int visible_mode = 0775 & ~opts->mask;
386
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800387
388 if (info->perm == PERM_PRE_ROOT) {
389 /* Top of multi-user view should always be visible to ensure
390 * secondary users can traverse inside. */
391 visible_mode = 0711;
392 } else if (info->under_android) {
393 /* Block "other" access to Android directories, since only apps
394 * belonging to a specific user should be in there; we still
395 * leave +x open for the default view. */
Daniel Rosenberg90219272016-10-26 20:27:20 -0700396 if (opts->gid == AID_SDCARD_RW) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800397 visible_mode = visible_mode & ~0006;
398 } else {
399 visible_mode = visible_mode & ~0007;
400 }
401 }
402 owner_mode = info->lower_inode->i_mode & 0700;
403 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
404 return filtered_mode;
405}
406
Daniel Campello35c9e242015-07-20 16:23:50 -0700407static inline int has_graft_path(const struct dentry *dent)
408{
409 int ret = 0;
410
411 spin_lock(&SDCARDFS_D(dent)->lock);
412 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
413 ret = 1;
414 spin_unlock(&SDCARDFS_D(dent)->lock);
415
416 return ret;
417}
418
419static inline void sdcardfs_get_real_lower(const struct dentry *dent,
420 struct path *real_lower)
421{
422 /* in case of a local obb dentry
423 * the orig_path should be returned
424 */
425 if(has_graft_path(dent))
426 sdcardfs_get_orig_path(dent, real_lower);
427 else
428 sdcardfs_get_lower_path(dent, real_lower);
429}
430
431static inline void sdcardfs_put_real_lower(const struct dentry *dent,
432 struct path *real_lower)
433{
434 if(has_graft_path(dent))
435 sdcardfs_put_orig_path(dent, real_lower);
436 else
437 sdcardfs_put_lower_path(dent, real_lower);
438}
439
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800440extern struct mutex sdcardfs_super_list_lock;
441extern struct list_head sdcardfs_super_list;
442
Daniel Campello35c9e242015-07-20 16:23:50 -0700443/* for packagelist.c */
Daniel Rosenbergfbd34b62016-05-10 13:42:43 -0700444extern appid_t get_appid(const char *app_name);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800445extern int check_caller_access_to_name(struct inode *parent_node, const char* name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700446extern int open_flags_to_access_mode(int open_flags);
Daniel Campello35c9e242015-07-20 16:23:50 -0700447extern int packagelist_init(void);
448extern void packagelist_exit(void);
449
450/* for derived_perm.c */
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700451extern void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
452 uid_t uid, bool under_android, struct inode *top);
Daniel Campello35c9e242015-07-20 16:23:50 -0700453extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800454extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700455extern void fixup_top_recursive(struct dentry *parent);
456extern void fixup_perms_recursive(struct dentry *dentry, const char *name, size_t len);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800457
458extern void update_derived_permission_lock(struct dentry *dentry);
Daniel Campello35c9e242015-07-20 16:23:50 -0700459extern int need_graft_path(struct dentry *dentry);
460extern int is_base_obbpath(struct dentry *dentry);
461extern int is_obbpath_invalid(struct dentry *dentry);
462extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
463
464/* locking helpers */
465static inline struct dentry *lock_parent(struct dentry *dentry)
466{
467 struct dentry *dir = dget_parent(dentry);
Amit Pundirb47e1102016-06-01 21:53:20 +0530468 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
Daniel Campello35c9e242015-07-20 16:23:50 -0700469 return dir;
470}
471
472static inline void unlock_dir(struct dentry *dir)
473{
Amit Pundirb47e1102016-06-01 21:53:20 +0530474 inode_unlock(d_inode(dir));
Daniel Campello35c9e242015-07-20 16:23:50 -0700475 dput(dir);
476}
477
478static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
479{
480 int err;
481 struct dentry *dent;
482 struct iattr attrs;
Daniel Campellod1d080c2015-07-20 16:27:37 -0700483 struct path parent;
Daniel Campello35c9e242015-07-20 16:23:50 -0700484
Daniel Campellod1d080c2015-07-20 16:27:37 -0700485 dent = kern_path_locked(path_s, &parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700486 if (IS_ERR(dent)) {
487 err = PTR_ERR(dent);
488 if (err == -EEXIST)
489 err = 0;
490 goto out_unlock;
491 }
492
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700493 err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode);
Daniel Campello35c9e242015-07-20 16:23:50 -0700494 if (err) {
495 if (err == -EEXIST)
496 err = 0;
497 goto out_dput;
498 }
499
Daniel Campellod1d080c2015-07-20 16:27:37 -0700500 attrs.ia_uid = make_kuid(&init_user_ns, uid);
501 attrs.ia_gid = make_kgid(&init_user_ns, gid);
Daniel Campello35c9e242015-07-20 16:23:50 -0700502 attrs.ia_valid = ATTR_UID | ATTR_GID;
Amit Pundirb47e1102016-06-01 21:53:20 +0530503 inode_lock(d_inode(dent));
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700504 notify_change2(parent.mnt, dent, &attrs, NULL);
Amit Pundirb47e1102016-06-01 21:53:20 +0530505 inode_unlock(d_inode(dent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700506
507out_dput:
508 dput(dent);
509
510out_unlock:
511 /* parent dentry locked by lookup_create */
Amit Pundirb47e1102016-06-01 21:53:20 +0530512 inode_unlock(d_inode(parent.dentry));
Daniel Campellod1d080c2015-07-20 16:27:37 -0700513 path_put(&parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700514 return err;
515}
516
517/*
518 * Return 1, if a disk has enough free space, otherwise 0.
519 * We assume that any files can not be overwritten.
520 */
521static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
522{
523 int err;
524 struct path lower_path;
525 struct kstatfs statfs;
526 u64 avail;
527 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
528
529 if (sbi->options.reserved_mb) {
530 /* Get fs stat of lower filesystem. */
531 sdcardfs_get_lower_path(dentry, &lower_path);
532 err = vfs_statfs(&lower_path, &statfs);
533 sdcardfs_put_lower_path(dentry, &lower_path);
534
535 if (unlikely(err))
536 return 0;
537
538 /* Invalid statfs informations. */
539 if (unlikely(statfs.f_bsize == 0))
540 return 0;
541
542 /* if you are checking directory, set size to f_bsize. */
543 if (unlikely(dir))
544 size = statfs.f_bsize;
545
546 /* available size */
547 avail = statfs.f_bavail * statfs.f_bsize;
548
549 /* not enough space */
550 if ((u64)size > avail)
551 return 0;
552
553 /* enough space */
554 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
555 return 1;
556
557 return 0;
558 } else
559 return 1;
560}
561
Daniel Rosenberg90219272016-10-26 20:27:20 -0700562/*
563 * Copies attrs and maintains sdcardfs managed attrs
564 * Since our permission check handles all special permissions, set those to be open
565 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800566static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
567{
Daniel Rosenberg90219272016-10-26 20:27:20 -0700568 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
569 S_IROTH | S_IXOTH; /* 0775 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800570 dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->d_uid);
Daniel Rosenberg90219272016-10-26 20:27:20 -0700571 dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800572 dest->i_rdev = src->i_rdev;
573 dest->i_atime = src->i_atime;
574 dest->i_mtime = src->i_mtime;
575 dest->i_ctime = src->i_ctime;
576 dest->i_blkbits = src->i_blkbits;
577 dest->i_flags = src->i_flags;
578 set_nlink(dest, src->i_nlink);
579}
Daniel Campello35c9e242015-07-20 16:23:50 -0700580#endif /* not _SDCARDFS_H_ */