Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/main.c |
| 3 | * |
| 4 | * Copyright (c) 2013 Samsung Electronics Co. Ltd |
| 5 | * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun, |
| 6 | * Sunghwan Yun, Sungjong Seo |
| 7 | * |
| 8 | * This program has been developed as a stackable file system based on |
| 9 | * the WrapFS which written by |
| 10 | * |
| 11 | * Copyright (c) 1998-2011 Erez Zadok |
| 12 | * Copyright (c) 2009 Shrikar Archak |
| 13 | * Copyright (c) 2003-2011 Stony Brook University |
| 14 | * Copyright (c) 2003-2011 The Research Foundation of SUNY |
| 15 | * |
| 16 | * This file is dual licensed. It may be redistributed and/or modified |
| 17 | * under the terms of the Apache 2.0 License OR version 2 of the GNU |
| 18 | * General Public License. |
| 19 | */ |
| 20 | |
| 21 | #include "sdcardfs.h" |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/parser.h> |
| 25 | |
| 26 | enum { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 27 | Opt_fsuid, |
| 28 | Opt_fsgid, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 29 | Opt_gid, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 30 | Opt_debug, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 31 | Opt_mask, |
Daniel Rosenberg | d64126c | 2017-03-16 19:33:35 -0700 | [diff] [blame] | 32 | Opt_multiuser, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 33 | Opt_userid, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 34 | Opt_reserved_mb, |
Daniel Rosenberg | 7d10e43 | 2017-07-19 17:25:07 -0700 | [diff] [blame] | 35 | Opt_gid_derivation, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 36 | Opt_err, |
| 37 | }; |
| 38 | |
| 39 | static const match_table_t sdcardfs_tokens = { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 40 | {Opt_fsuid, "fsuid=%u"}, |
| 41 | {Opt_fsgid, "fsgid=%u"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 42 | {Opt_gid, "gid=%u"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 43 | {Opt_debug, "debug"}, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 44 | {Opt_mask, "mask=%u"}, |
| 45 | {Opt_userid, "userid=%d"}, |
| 46 | {Opt_multiuser, "multiuser"}, |
Daniel Rosenberg | 7d10e43 | 2017-07-19 17:25:07 -0700 | [diff] [blame] | 47 | {Opt_gid_derivation, "derive_gid"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 48 | {Opt_reserved_mb, "reserved_mb=%u"}, |
| 49 | {Opt_err, NULL} |
| 50 | }; |
| 51 | |
| 52 | static int parse_options(struct super_block *sb, char *options, int silent, |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 53 | int *debug, struct sdcardfs_vfsmount_options *vfsopts, |
| 54 | struct sdcardfs_mount_options *opts) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 55 | { |
| 56 | char *p; |
| 57 | substring_t args[MAX_OPT_ARGS]; |
| 58 | int option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 59 | |
| 60 | /* by default, we use AID_MEDIA_RW as uid, gid */ |
| 61 | opts->fs_low_uid = AID_MEDIA_RW; |
| 62 | opts->fs_low_gid = AID_MEDIA_RW; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 63 | vfsopts->mask = 0; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 64 | opts->multiuser = false; |
| 65 | opts->fs_user_id = 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 66 | vfsopts->gid = 0; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 67 | /* by default, 0MB is reserved */ |
| 68 | opts->reserved_mb = 0; |
Daniel Rosenberg | 7d10e43 | 2017-07-19 17:25:07 -0700 | [diff] [blame] | 69 | /* by default, gid derivation is off */ |
| 70 | opts->gid_derivation = false; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 71 | |
| 72 | *debug = 0; |
| 73 | |
| 74 | if (!options) |
| 75 | return 0; |
| 76 | |
| 77 | while ((p = strsep(&options, ",")) != NULL) { |
| 78 | int token; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 79 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 80 | if (!*p) |
| 81 | continue; |
| 82 | |
| 83 | token = match_token(p, sdcardfs_tokens, args); |
| 84 | |
| 85 | switch (token) { |
| 86 | case Opt_debug: |
| 87 | *debug = 1; |
| 88 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 89 | case Opt_fsuid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 90 | if (match_int(&args[0], &option)) |
| 91 | return 0; |
| 92 | opts->fs_low_uid = option; |
| 93 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 94 | case Opt_fsgid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 95 | if (match_int(&args[0], &option)) |
| 96 | return 0; |
| 97 | opts->fs_low_gid = option; |
| 98 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 99 | case Opt_gid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 100 | if (match_int(&args[0], &option)) |
| 101 | return 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 102 | vfsopts->gid = option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 103 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 104 | case Opt_userid: |
| 105 | if (match_int(&args[0], &option)) |
| 106 | return 0; |
| 107 | opts->fs_user_id = option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 108 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 109 | case Opt_mask: |
| 110 | if (match_int(&args[0], &option)) |
| 111 | return 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 112 | vfsopts->mask = option; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 113 | break; |
| 114 | case Opt_multiuser: |
| 115 | opts->multiuser = true; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 116 | break; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 117 | case Opt_reserved_mb: |
| 118 | if (match_int(&args[0], &option)) |
| 119 | return 0; |
| 120 | opts->reserved_mb = option; |
| 121 | break; |
Daniel Rosenberg | 7d10e43 | 2017-07-19 17:25:07 -0700 | [diff] [blame] | 122 | case Opt_gid_derivation: |
| 123 | opts->gid_derivation = true; |
Daniel Rosenberg | 93babeb | 2017-09-08 17:20:06 -0700 | [diff] [blame] | 124 | break; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 125 | /* unknown option */ |
| 126 | default: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 127 | if (!silent) |
| 128 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 129 | return -EINVAL; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (*debug) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 134 | pr_info("sdcardfs : options - debug:%d\n", *debug); |
| 135 | pr_info("sdcardfs : options - uid:%d\n", |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 136 | opts->fs_low_uid); |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 137 | pr_info("sdcardfs : options - gid:%d\n", |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 138 | opts->fs_low_gid); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 144 | int parse_options_remount(struct super_block *sb, char *options, int silent, |
| 145 | struct sdcardfs_vfsmount_options *vfsopts) |
| 146 | { |
| 147 | char *p; |
| 148 | substring_t args[MAX_OPT_ARGS]; |
| 149 | int option; |
| 150 | int debug; |
| 151 | |
| 152 | if (!options) |
| 153 | return 0; |
| 154 | |
| 155 | while ((p = strsep(&options, ",")) != NULL) { |
| 156 | int token; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 157 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 158 | if (!*p) |
| 159 | continue; |
| 160 | |
| 161 | token = match_token(p, sdcardfs_tokens, args); |
| 162 | |
| 163 | switch (token) { |
| 164 | case Opt_debug: |
| 165 | debug = 1; |
| 166 | break; |
| 167 | case Opt_gid: |
| 168 | if (match_int(&args[0], &option)) |
| 169 | return 0; |
| 170 | vfsopts->gid = option; |
| 171 | |
| 172 | break; |
| 173 | case Opt_mask: |
| 174 | if (match_int(&args[0], &option)) |
| 175 | return 0; |
| 176 | vfsopts->mask = option; |
| 177 | break; |
| 178 | case Opt_multiuser: |
| 179 | case Opt_userid: |
| 180 | case Opt_fsuid: |
| 181 | case Opt_fsgid: |
| 182 | case Opt_reserved_mb: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 183 | pr_warn("Option \"%s\" can't be changed during remount\n", p); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 184 | break; |
| 185 | /* unknown option */ |
| 186 | default: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 187 | if (!silent) |
| 188 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 189 | return -EINVAL; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (debug) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 194 | pr_info("sdcardfs : options - debug:%d\n", debug); |
| 195 | pr_info("sdcardfs : options - gid:%d\n", vfsopts->gid); |
| 196 | pr_info("sdcardfs : options - mask:%d\n", vfsopts->mask); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 202 | #if 0 |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 203 | /* |
| 204 | * our custom d_alloc_root work-alike |
| 205 | * |
| 206 | * we can't use d_alloc_root if we want to use our own interpose function |
| 207 | * unchanged, so we simply call our own "fake" d_alloc_root |
| 208 | */ |
| 209 | static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb) |
| 210 | { |
| 211 | struct dentry *ret = NULL; |
| 212 | |
| 213 | if (sb) { |
| 214 | static const struct qstr name = { |
| 215 | .name = "/", |
| 216 | .len = 1 |
| 217 | }; |
| 218 | |
| 219 | ret = d_alloc(NULL, &name); |
| 220 | if (ret) { |
| 221 | d_set_d_op(ret, &sdcardfs_ci_dops); |
| 222 | ret->d_sb = sb; |
| 223 | ret->d_parent = ret; |
| 224 | } |
| 225 | } |
| 226 | return ret; |
| 227 | } |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 228 | #endif |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 229 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 230 | DEFINE_MUTEX(sdcardfs_super_list_lock); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock); |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 232 | LIST_HEAD(sdcardfs_super_list); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(sdcardfs_super_list); |
| 234 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 235 | /* |
| 236 | * There is no need to lock the sdcardfs_super_info's rwsem as there is no |
| 237 | * way anyone can have a reference to the superblock at this point in time. |
| 238 | */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 239 | static int sdcardfs_read_super(struct vfsmount *mnt, struct super_block *sb, |
| 240 | const char *dev_name, void *raw_data, int silent) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 241 | { |
| 242 | int err = 0; |
| 243 | int debug; |
| 244 | struct super_block *lower_sb; |
| 245 | struct path lower_path; |
| 246 | struct sdcardfs_sb_info *sb_info; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 247 | struct sdcardfs_vfsmount_options *mnt_opt = mnt->data; |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 248 | struct inode *inode; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 249 | |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 250 | pr_info("sdcardfs version 2.0\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 251 | |
| 252 | if (!dev_name) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 253 | pr_err("sdcardfs: read_super: missing dev_name argument\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 254 | err = -EINVAL; |
| 255 | goto out; |
| 256 | } |
| 257 | |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 258 | pr_info("sdcardfs: dev_name -> %s\n", dev_name); |
| 259 | pr_info("sdcardfs: options -> %s\n", (char *)raw_data); |
| 260 | pr_info("sdcardfs: mnt -> %p\n", mnt); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 261 | |
| 262 | /* parse lower path */ |
| 263 | err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
| 264 | &lower_path); |
| 265 | if (err) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 266 | pr_err("sdcardfs: error accessing lower directory '%s'\n", dev_name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 267 | goto out; |
| 268 | } |
| 269 | |
| 270 | /* allocate superblock private data */ |
| 271 | sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL); |
| 272 | if (!SDCARDFS_SB(sb)) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 273 | pr_crit("sdcardfs: read_super: out of memory\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 274 | err = -ENOMEM; |
| 275 | goto out_free; |
| 276 | } |
| 277 | |
| 278 | sb_info = sb->s_fs_info; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 279 | /* parse options */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 280 | err = parse_options(sb, raw_data, silent, &debug, mnt_opt, &sb_info->options); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 281 | if (err) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 282 | pr_err("sdcardfs: invalid options\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 283 | goto out_freesbi; |
| 284 | } |
| 285 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 286 | /* set the lower superblock field of upper superblock */ |
| 287 | lower_sb = lower_path.dentry->d_sb; |
| 288 | atomic_inc(&lower_sb->s_active); |
| 289 | sdcardfs_set_lower_super(sb, lower_sb); |
| 290 | |
| 291 | /* inherit maxbytes from lower file system */ |
| 292 | sb->s_maxbytes = lower_sb->s_maxbytes; |
| 293 | |
| 294 | /* |
| 295 | * Our c/m/atime granularity is 1 ns because we may stack on file |
| 296 | * systems whose granularity is as good. |
| 297 | */ |
| 298 | sb->s_time_gran = 1; |
| 299 | |
| 300 | sb->s_magic = SDCARDFS_SUPER_MAGIC; |
| 301 | sb->s_op = &sdcardfs_sops; |
| 302 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 303 | /* get a new inode and allocate our root dentry */ |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 304 | inode = sdcardfs_iget(sb, d_inode(lower_path.dentry), 0); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 305 | if (IS_ERR(inode)) { |
| 306 | err = PTR_ERR(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 307 | goto out_sput; |
| 308 | } |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 309 | sb->s_root = d_make_root(inode); |
| 310 | if (!sb->s_root) { |
| 311 | err = -ENOMEM; |
| 312 | goto out_iput; |
| 313 | } |
| 314 | d_set_d_op(sb->s_root, &sdcardfs_ci_dops); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 315 | |
| 316 | /* link the upper and lower dentries */ |
| 317 | sb->s_root->d_fsdata = NULL; |
| 318 | err = new_dentry_private_data(sb->s_root); |
| 319 | if (err) |
| 320 | goto out_freeroot; |
| 321 | |
| 322 | /* set the lower dentries for s_root */ |
| 323 | sdcardfs_set_lower_path(sb->s_root, &lower_path); |
| 324 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 325 | /* |
| 326 | * No need to call interpose because we already have a positive |
| 327 | * dentry, which was instantiated by d_make_root. Just need to |
| 328 | * d_rehash it. |
| 329 | */ |
| 330 | d_rehash(sb->s_root); |
| 331 | |
| 332 | /* setup permission policy */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 333 | sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL); |
| 334 | mutex_lock(&sdcardfs_super_list_lock); |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 335 | if (sb_info->options.multiuser) { |
| 336 | setup_derived_state(d_inode(sb->s_root), PERM_PRE_ROOT, |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 337 | sb_info->options.fs_user_id, AID_ROOT, |
| 338 | false, SDCARDFS_I(d_inode(sb->s_root))->data); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 339 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 340 | } else { |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 341 | setup_derived_state(d_inode(sb->s_root), PERM_ROOT, |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 342 | sb_info->options.fs_user_id, AID_ROOT, |
| 343 | false, SDCARDFS_I(d_inode(sb->s_root))->data); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 344 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 345 | } |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 346 | fixup_tmp_permissions(d_inode(sb->s_root)); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 347 | sb_info->sb = sb; |
| 348 | list_add(&sb_info->list, &sdcardfs_super_list); |
| 349 | mutex_unlock(&sdcardfs_super_list_lock); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 350 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 351 | if (!silent) |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 352 | pr_info("sdcardfs: mounted on top of %s type %s\n", |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 353 | dev_name, lower_sb->s_type->name); |
| 354 | goto out; /* all is well */ |
| 355 | |
| 356 | /* no longer needed: free_dentry_private_data(sb->s_root); */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 357 | out_freeroot: |
| 358 | dput(sb->s_root); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 359 | out_iput: |
| 360 | iput(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 361 | out_sput: |
| 362 | /* drop refs we took earlier */ |
| 363 | atomic_dec(&lower_sb->s_active); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 364 | out_freesbi: |
| 365 | kfree(SDCARDFS_SB(sb)); |
| 366 | sb->s_fs_info = NULL; |
| 367 | out_free: |
| 368 | path_put(&lower_path); |
| 369 | |
| 370 | out: |
| 371 | return err; |
| 372 | } |
| 373 | |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 374 | struct sdcardfs_mount_private { |
| 375 | struct vfsmount *mnt; |
| 376 | const char *dev_name; |
| 377 | void *raw_data; |
| 378 | }; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 379 | |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 380 | static int __sdcardfs_fill_super( |
| 381 | struct super_block *sb, |
| 382 | void *_priv, int silent) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 383 | { |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 384 | struct sdcardfs_mount_private *priv = _priv; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 385 | |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 386 | return sdcardfs_read_super(priv->mnt, |
| 387 | sb, priv->dev_name, priv->raw_data, silent); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 390 | static struct dentry *sdcardfs_mount(struct vfsmount *mnt, |
| 391 | struct file_system_type *fs_type, int flags, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 392 | const char *dev_name, void *raw_data) |
| 393 | { |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 394 | struct sdcardfs_mount_private priv = { |
| 395 | .mnt = mnt, |
| 396 | .dev_name = dev_name, |
| 397 | .raw_data = raw_data |
| 398 | }; |
| 399 | |
| 400 | return mount_nodev(fs_type, flags, |
| 401 | &priv, __sdcardfs_fill_super); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 404 | static struct dentry *sdcardfs_mount_wrn(struct file_system_type *fs_type, |
| 405 | int flags, const char *dev_name, void *raw_data) |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 406 | { |
| 407 | WARN(1, "sdcardfs does not support mount. Use mount2.\n"); |
| 408 | return ERR_PTR(-EINVAL); |
| 409 | } |
| 410 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 411 | void *sdcardfs_alloc_mnt_data(void) |
| 412 | { |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 413 | return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 416 | void sdcardfs_kill_sb(struct super_block *sb) |
| 417 | { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 418 | struct sdcardfs_sb_info *sbi; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 419 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 420 | if (sb->s_magic == SDCARDFS_SUPER_MAGIC) { |
| 421 | sbi = SDCARDFS_SB(sb); |
| 422 | mutex_lock(&sdcardfs_super_list_lock); |
| 423 | list_del(&sbi->list); |
| 424 | mutex_unlock(&sdcardfs_super_list_lock); |
| 425 | } |
Gao Xiang | b2ac1fd | 2017-05-09 12:30:56 +0800 | [diff] [blame] | 426 | kill_anon_super(sb); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 429 | static struct file_system_type sdcardfs_fs_type = { |
| 430 | .owner = THIS_MODULE, |
| 431 | .name = SDCARDFS_NAME, |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 432 | .mount = sdcardfs_mount_wrn, |
| 433 | .mount2 = sdcardfs_mount, |
| 434 | .alloc_mnt_data = sdcardfs_alloc_mnt_data, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 435 | .kill_sb = sdcardfs_kill_sb, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 436 | .fs_flags = 0, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 437 | }; |
Daniel Rosenberg | 35ba5f6 | 2017-03-09 20:59:18 -0800 | [diff] [blame] | 438 | MODULE_ALIAS_FS(SDCARDFS_NAME); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 439 | |
| 440 | static int __init init_sdcardfs_fs(void) |
| 441 | { |
| 442 | int err; |
| 443 | |
| 444 | pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n"); |
| 445 | |
| 446 | err = sdcardfs_init_inode_cache(); |
| 447 | if (err) |
| 448 | goto out; |
| 449 | err = sdcardfs_init_dentry_cache(); |
| 450 | if (err) |
| 451 | goto out; |
| 452 | err = packagelist_init(); |
| 453 | if (err) |
| 454 | goto out; |
| 455 | err = register_filesystem(&sdcardfs_fs_type); |
| 456 | out: |
| 457 | if (err) { |
| 458 | sdcardfs_destroy_inode_cache(); |
| 459 | sdcardfs_destroy_dentry_cache(); |
| 460 | packagelist_exit(); |
| 461 | } |
| 462 | return err; |
| 463 | } |
| 464 | |
| 465 | static void __exit exit_sdcardfs_fs(void) |
| 466 | { |
| 467 | sdcardfs_destroy_inode_cache(); |
| 468 | sdcardfs_destroy_dentry_cache(); |
| 469 | packagelist_exit(); |
| 470 | unregister_filesystem(&sdcardfs_fs_type); |
| 471 | pr_info("Completed sdcardfs module unload\n"); |
| 472 | } |
| 473 | |
Daniel Rosenberg | 75665df | 2017-03-09 20:56:05 -0800 | [diff] [blame] | 474 | /* Original wrapfs authors */ |
| 475 | MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University (http://www.fsl.cs.sunysb.edu/)"); |
| 476 | |
| 477 | /* Original sdcardfs authors */ |
| 478 | MODULE_AUTHOR("Woojoong Lee, Daeho Jeong, Kitae Lee, Yeongjin Gil System Memory Lab., Samsung Electronics"); |
| 479 | |
| 480 | /* Current maintainer */ |
| 481 | MODULE_AUTHOR("Daniel Rosenberg, Google"); |
| 482 | MODULE_DESCRIPTION("Sdcardfs " SDCARDFS_VERSION); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 483 | MODULE_LICENSE("GPL"); |
| 484 | |
| 485 | module_init(init_sdcardfs_fs); |
| 486 | module_exit(exit_sdcardfs_fs); |