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