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