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, |
| 35 | Opt_err, |
| 36 | }; |
| 37 | |
| 38 | static const match_table_t sdcardfs_tokens = { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 39 | {Opt_fsuid, "fsuid=%u"}, |
| 40 | {Opt_fsgid, "fsgid=%u"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 41 | {Opt_gid, "gid=%u"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 42 | {Opt_debug, "debug"}, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 43 | {Opt_mask, "mask=%u"}, |
| 44 | {Opt_userid, "userid=%d"}, |
| 45 | {Opt_multiuser, "multiuser"}, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 46 | {Opt_reserved_mb, "reserved_mb=%u"}, |
| 47 | {Opt_err, NULL} |
| 48 | }; |
| 49 | |
| 50 | static int parse_options(struct super_block *sb, char *options, int silent, |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 51 | int *debug, struct sdcardfs_vfsmount_options *vfsopts, |
| 52 | struct sdcardfs_mount_options *opts) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 53 | { |
| 54 | char *p; |
| 55 | substring_t args[MAX_OPT_ARGS]; |
| 56 | int option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 57 | |
| 58 | /* by default, we use AID_MEDIA_RW as uid, gid */ |
| 59 | opts->fs_low_uid = AID_MEDIA_RW; |
| 60 | opts->fs_low_gid = AID_MEDIA_RW; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 61 | vfsopts->mask = 0; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 62 | opts->multiuser = false; |
| 63 | opts->fs_user_id = 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 64 | vfsopts->gid = 0; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 65 | /* by default, 0MB is reserved */ |
| 66 | opts->reserved_mb = 0; |
| 67 | |
| 68 | *debug = 0; |
| 69 | |
| 70 | if (!options) |
| 71 | return 0; |
| 72 | |
| 73 | while ((p = strsep(&options, ",")) != NULL) { |
| 74 | int token; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 75 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 76 | if (!*p) |
| 77 | continue; |
| 78 | |
| 79 | token = match_token(p, sdcardfs_tokens, args); |
| 80 | |
| 81 | switch (token) { |
| 82 | case Opt_debug: |
| 83 | *debug = 1; |
| 84 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 85 | case Opt_fsuid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 86 | if (match_int(&args[0], &option)) |
| 87 | return 0; |
| 88 | opts->fs_low_uid = option; |
| 89 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 90 | case Opt_fsgid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 91 | if (match_int(&args[0], &option)) |
| 92 | return 0; |
| 93 | opts->fs_low_gid = option; |
| 94 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 95 | case Opt_gid: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 96 | if (match_int(&args[0], &option)) |
| 97 | return 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 98 | vfsopts->gid = option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 99 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 100 | case Opt_userid: |
| 101 | if (match_int(&args[0], &option)) |
| 102 | return 0; |
| 103 | opts->fs_user_id = option; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 104 | break; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 105 | case Opt_mask: |
| 106 | if (match_int(&args[0], &option)) |
| 107 | return 0; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 108 | vfsopts->mask = option; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 109 | break; |
| 110 | case Opt_multiuser: |
| 111 | opts->multiuser = true; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 112 | break; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 113 | case Opt_reserved_mb: |
| 114 | if (match_int(&args[0], &option)) |
| 115 | return 0; |
| 116 | opts->reserved_mb = option; |
| 117 | break; |
| 118 | /* unknown option */ |
| 119 | default: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 120 | if (!silent) |
| 121 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (*debug) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 127 | pr_info("sdcardfs : options - debug:%d\n", *debug); |
| 128 | pr_info("sdcardfs : options - uid:%d\n", |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 129 | opts->fs_low_uid); |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 130 | pr_info("sdcardfs : options - gid:%d\n", |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 131 | opts->fs_low_gid); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 137 | int parse_options_remount(struct super_block *sb, char *options, int silent, |
| 138 | struct sdcardfs_vfsmount_options *vfsopts) |
| 139 | { |
| 140 | char *p; |
| 141 | substring_t args[MAX_OPT_ARGS]; |
| 142 | int option; |
| 143 | int debug; |
| 144 | |
| 145 | if (!options) |
| 146 | return 0; |
| 147 | |
| 148 | while ((p = strsep(&options, ",")) != NULL) { |
| 149 | int token; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 150 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 151 | if (!*p) |
| 152 | continue; |
| 153 | |
| 154 | token = match_token(p, sdcardfs_tokens, args); |
| 155 | |
| 156 | switch (token) { |
| 157 | case Opt_debug: |
| 158 | debug = 1; |
| 159 | break; |
| 160 | case Opt_gid: |
| 161 | if (match_int(&args[0], &option)) |
| 162 | return 0; |
| 163 | vfsopts->gid = option; |
| 164 | |
| 165 | break; |
| 166 | case Opt_mask: |
| 167 | if (match_int(&args[0], &option)) |
| 168 | return 0; |
| 169 | vfsopts->mask = option; |
| 170 | break; |
| 171 | case Opt_multiuser: |
| 172 | case Opt_userid: |
| 173 | case Opt_fsuid: |
| 174 | case Opt_fsgid: |
| 175 | case Opt_reserved_mb: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 176 | pr_warn("Option \"%s\" can't be changed during remount\n", p); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 177 | break; |
| 178 | /* unknown option */ |
| 179 | default: |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 180 | if (!silent) |
| 181 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 182 | return -EINVAL; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (debug) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 187 | pr_info("sdcardfs : options - debug:%d\n", debug); |
| 188 | pr_info("sdcardfs : options - gid:%d\n", vfsopts->gid); |
| 189 | pr_info("sdcardfs : options - mask:%d\n", vfsopts->mask); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 195 | #if 0 |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 196 | /* |
| 197 | * our custom d_alloc_root work-alike |
| 198 | * |
| 199 | * we can't use d_alloc_root if we want to use our own interpose function |
| 200 | * unchanged, so we simply call our own "fake" d_alloc_root |
| 201 | */ |
| 202 | static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb) |
| 203 | { |
| 204 | struct dentry *ret = NULL; |
| 205 | |
| 206 | if (sb) { |
| 207 | static const struct qstr name = { |
| 208 | .name = "/", |
| 209 | .len = 1 |
| 210 | }; |
| 211 | |
| 212 | ret = d_alloc(NULL, &name); |
| 213 | if (ret) { |
| 214 | d_set_d_op(ret, &sdcardfs_ci_dops); |
| 215 | ret->d_sb = sb; |
| 216 | ret->d_parent = ret; |
| 217 | } |
| 218 | } |
| 219 | return ret; |
| 220 | } |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 221 | #endif |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 222 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 223 | DEFINE_MUTEX(sdcardfs_super_list_lock); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 224 | EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock); |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 225 | LIST_HEAD(sdcardfs_super_list); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(sdcardfs_super_list); |
| 227 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 228 | /* |
| 229 | * There is no need to lock the sdcardfs_super_info's rwsem as there is no |
| 230 | * way anyone can have a reference to the superblock at this point in time. |
| 231 | */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 232 | static int sdcardfs_read_super(struct vfsmount *mnt, struct super_block *sb, |
| 233 | const char *dev_name, void *raw_data, int silent) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 234 | { |
| 235 | int err = 0; |
| 236 | int debug; |
| 237 | struct super_block *lower_sb; |
| 238 | struct path lower_path; |
| 239 | struct sdcardfs_sb_info *sb_info; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 240 | struct sdcardfs_vfsmount_options *mnt_opt = mnt->data; |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 241 | struct inode *inode; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 242 | |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 243 | pr_info("sdcardfs version 2.0\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 244 | |
| 245 | if (!dev_name) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 246 | pr_err("sdcardfs: read_super: missing dev_name argument\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 247 | err = -EINVAL; |
| 248 | goto out; |
| 249 | } |
| 250 | |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 251 | pr_info("sdcardfs: dev_name -> %s\n", dev_name); |
| 252 | pr_info("sdcardfs: options -> %s\n", (char *)raw_data); |
| 253 | pr_info("sdcardfs: mnt -> %p\n", mnt); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 254 | |
| 255 | /* parse lower path */ |
| 256 | err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
| 257 | &lower_path); |
| 258 | if (err) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 259 | pr_err("sdcardfs: error accessing lower directory '%s'\n", dev_name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 260 | goto out; |
| 261 | } |
| 262 | |
| 263 | /* allocate superblock private data */ |
| 264 | sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL); |
| 265 | if (!SDCARDFS_SB(sb)) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 266 | pr_crit("sdcardfs: read_super: out of memory\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 267 | err = -ENOMEM; |
| 268 | goto out_free; |
| 269 | } |
| 270 | |
| 271 | sb_info = sb->s_fs_info; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 272 | /* parse options */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 273 | 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] | 274 | if (err) { |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 275 | pr_err("sdcardfs: invalid options\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 276 | goto out_freesbi; |
| 277 | } |
| 278 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 279 | /* set the lower superblock field of upper superblock */ |
| 280 | lower_sb = lower_path.dentry->d_sb; |
| 281 | atomic_inc(&lower_sb->s_active); |
| 282 | sdcardfs_set_lower_super(sb, lower_sb); |
| 283 | |
| 284 | /* inherit maxbytes from lower file system */ |
| 285 | sb->s_maxbytes = lower_sb->s_maxbytes; |
| 286 | |
| 287 | /* |
| 288 | * Our c/m/atime granularity is 1 ns because we may stack on file |
| 289 | * systems whose granularity is as good. |
| 290 | */ |
| 291 | sb->s_time_gran = 1; |
| 292 | |
| 293 | sb->s_magic = SDCARDFS_SUPER_MAGIC; |
| 294 | sb->s_op = &sdcardfs_sops; |
| 295 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 296 | /* get a new inode and allocate our root dentry */ |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 297 | inode = sdcardfs_iget(sb, d_inode(lower_path.dentry), 0); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 298 | if (IS_ERR(inode)) { |
| 299 | err = PTR_ERR(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 300 | goto out_sput; |
| 301 | } |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 302 | sb->s_root = d_make_root(inode); |
| 303 | if (!sb->s_root) { |
| 304 | err = -ENOMEM; |
| 305 | goto out_iput; |
| 306 | } |
| 307 | d_set_d_op(sb->s_root, &sdcardfs_ci_dops); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 308 | |
| 309 | /* link the upper and lower dentries */ |
| 310 | sb->s_root->d_fsdata = NULL; |
| 311 | err = new_dentry_private_data(sb->s_root); |
| 312 | if (err) |
| 313 | goto out_freeroot; |
| 314 | |
| 315 | /* set the lower dentries for s_root */ |
| 316 | sdcardfs_set_lower_path(sb->s_root, &lower_path); |
| 317 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 318 | /* |
| 319 | * No need to call interpose because we already have a positive |
| 320 | * dentry, which was instantiated by d_make_root. Just need to |
| 321 | * d_rehash it. |
| 322 | */ |
| 323 | d_rehash(sb->s_root); |
| 324 | |
| 325 | /* setup permission policy */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 326 | sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL); |
| 327 | mutex_lock(&sdcardfs_super_list_lock); |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 328 | if (sb_info->options.multiuser) { |
| 329 | setup_derived_state(d_inode(sb->s_root), PERM_PRE_ROOT, |
| 330 | sb_info->options.fs_user_id, AID_ROOT, |
| 331 | false, d_inode(sb->s_root)); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 332 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 333 | } else { |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 334 | setup_derived_state(d_inode(sb->s_root), PERM_ROOT, |
| 335 | sb_info->options.fs_user_id, AID_ROOT, |
| 336 | false, d_inode(sb->s_root)); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 337 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 338 | } |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 339 | fixup_tmp_permissions(d_inode(sb->s_root)); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 340 | sb_info->sb = sb; |
| 341 | list_add(&sb_info->list, &sdcardfs_super_list); |
| 342 | mutex_unlock(&sdcardfs_super_list_lock); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 343 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 344 | if (!silent) |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 345 | pr_info("sdcardfs: mounted on top of %s type %s\n", |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 346 | dev_name, lower_sb->s_type->name); |
| 347 | goto out; /* all is well */ |
| 348 | |
| 349 | /* no longer needed: free_dentry_private_data(sb->s_root); */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 350 | out_freeroot: |
| 351 | dput(sb->s_root); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 352 | out_iput: |
| 353 | iput(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 354 | out_sput: |
| 355 | /* drop refs we took earlier */ |
| 356 | atomic_dec(&lower_sb->s_active); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 357 | out_freesbi: |
| 358 | kfree(SDCARDFS_SB(sb)); |
| 359 | sb->s_fs_info = NULL; |
| 360 | out_free: |
| 361 | path_put(&lower_path); |
| 362 | |
| 363 | out: |
| 364 | return err; |
| 365 | } |
| 366 | |
| 367 | /* A feature which supports mount_nodev() with options */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 368 | static struct dentry *mount_nodev_with_options(struct vfsmount *mnt, |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 369 | struct file_system_type *fs_type, int flags, |
| 370 | const char *dev_name, void *data, |
| 371 | int (*fill_super)(struct vfsmount *, struct super_block *, |
| 372 | const char *, void *, int)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 373 | |
| 374 | { |
| 375 | int error; |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 376 | struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 377 | |
| 378 | if (IS_ERR(s)) |
| 379 | return ERR_CAST(s); |
| 380 | |
| 381 | s->s_flags = flags; |
| 382 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 383 | error = fill_super(mnt, s, dev_name, data, flags & MS_SILENT ? 1 : 0); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 384 | if (error) { |
| 385 | deactivate_locked_super(s); |
| 386 | return ERR_PTR(error); |
| 387 | } |
| 388 | s->s_flags |= MS_ACTIVE; |
| 389 | return dget(s->s_root); |
| 390 | } |
| 391 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 392 | static struct dentry *sdcardfs_mount(struct vfsmount *mnt, |
| 393 | struct file_system_type *fs_type, int flags, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 394 | const char *dev_name, void *raw_data) |
| 395 | { |
| 396 | /* |
| 397 | * dev_name is a lower_path_name, |
| 398 | * raw_data is a option string. |
| 399 | */ |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 400 | return mount_nodev_with_options(mnt, fs_type, flags, dev_name, |
| 401 | raw_data, sdcardfs_read_super); |
| 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 | } |
| 426 | generic_shutdown_super(sb); |
| 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); |