Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/super.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 | |
| 23 | /* |
| 24 | * The inode cache is used with alloc_inode for both our inode info and the |
| 25 | * vfs inode. |
| 26 | */ |
| 27 | static struct kmem_cache *sdcardfs_inode_cachep; |
| 28 | |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 29 | /* |
| 30 | * To support the top references, we must track some data separately. |
| 31 | * An sdcardfs_inode_info always has a reference to its data, and once set up, |
| 32 | * also has a reference to its top. The top may be itself, in which case it |
| 33 | * holds two references to its data. When top is changed, it takes a ref to the |
| 34 | * new data and then drops the ref to the old data. |
| 35 | */ |
| 36 | static struct kmem_cache *sdcardfs_inode_data_cachep; |
| 37 | |
| 38 | void data_release(struct kref *ref) |
| 39 | { |
| 40 | struct sdcardfs_inode_data *data = |
| 41 | container_of(ref, struct sdcardfs_inode_data, refcount); |
| 42 | |
| 43 | kmem_cache_free(sdcardfs_inode_data_cachep, data); |
| 44 | } |
| 45 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 46 | /* final actions when unmounting a file system */ |
| 47 | static void sdcardfs_put_super(struct super_block *sb) |
| 48 | { |
| 49 | struct sdcardfs_sb_info *spd; |
| 50 | struct super_block *s; |
| 51 | |
| 52 | spd = SDCARDFS_SB(sb); |
| 53 | if (!spd) |
| 54 | return; |
| 55 | |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 56 | if (spd->obbpath_s) { |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 57 | kfree(spd->obbpath_s); |
| 58 | path_put(&spd->obbpath); |
| 59 | } |
| 60 | |
| 61 | /* decrement lower super references */ |
| 62 | s = sdcardfs_lower_super(sb); |
| 63 | sdcardfs_set_lower_super(sb, NULL); |
| 64 | atomic_dec(&s->s_active); |
| 65 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 66 | kfree(spd); |
| 67 | sb->s_fs_info = NULL; |
| 68 | } |
| 69 | |
| 70 | static int sdcardfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 71 | { |
| 72 | int err; |
| 73 | struct path lower_path; |
| 74 | u32 min_blocks; |
| 75 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 76 | |
| 77 | sdcardfs_get_lower_path(dentry, &lower_path); |
| 78 | err = vfs_statfs(&lower_path, buf); |
| 79 | sdcardfs_put_lower_path(dentry, &lower_path); |
| 80 | |
| 81 | if (sbi->options.reserved_mb) { |
| 82 | /* Invalid statfs informations. */ |
| 83 | if (buf->f_bsize == 0) { |
Daniel Rosenberg | 8cfe3bf | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 84 | pr_err("Returned block size is zero.\n"); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | min_blocks = ((sbi->options.reserved_mb * 1024 * 1024)/buf->f_bsize); |
| 89 | buf->f_blocks -= min_blocks; |
| 90 | |
| 91 | if (buf->f_bavail > min_blocks) |
| 92 | buf->f_bavail -= min_blocks; |
| 93 | else |
| 94 | buf->f_bavail = 0; |
| 95 | |
| 96 | /* Make reserved blocks invisiable to media storage */ |
| 97 | buf->f_bfree = buf->f_bavail; |
| 98 | } |
| 99 | |
| 100 | /* set return buf to our f/s to avoid confusing user-level utils */ |
| 101 | buf->f_type = SDCARDFS_SUPER_MAGIC; |
| 102 | |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * @flags: numeric mount options |
| 108 | * @options: mount options string |
| 109 | */ |
| 110 | static int sdcardfs_remount_fs(struct super_block *sb, int *flags, char *options) |
| 111 | { |
| 112 | int err = 0; |
| 113 | |
| 114 | /* |
| 115 | * The VFS will take care of "ro" and "rw" flags among others. We |
| 116 | * can safely accept a few flags (RDONLY, MANDLOCK), and honor |
| 117 | * SILENT, but anything else left over is an error. |
| 118 | */ |
| 119 | if ((*flags & ~(MS_RDONLY | MS_MANDLOCK | MS_SILENT)) != 0) { |
Daniel Rosenberg | 8cfe3bf | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 120 | pr_err("sdcardfs: remount flags 0x%x unsupported\n", *flags); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 121 | err = -EINVAL; |
| 122 | } |
| 123 | |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | /* |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 128 | * @mnt: mount point we are remounting |
| 129 | * @sb: superblock we are remounting |
| 130 | * @flags: numeric mount options |
| 131 | * @options: mount options string |
| 132 | */ |
| 133 | static int sdcardfs_remount_fs2(struct vfsmount *mnt, struct super_block *sb, |
| 134 | int *flags, char *options) |
| 135 | { |
| 136 | int err = 0; |
| 137 | |
| 138 | /* |
| 139 | * The VFS will take care of "ro" and "rw" flags among others. We |
| 140 | * can safely accept a few flags (RDONLY, MANDLOCK), and honor |
| 141 | * SILENT, but anything else left over is an error. |
| 142 | */ |
| 143 | if ((*flags & ~(MS_RDONLY | MS_MANDLOCK | MS_SILENT | MS_REMOUNT)) != 0) { |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 144 | pr_err("sdcardfs: remount flags 0x%x unsupported\n", *flags); |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 145 | err = -EINVAL; |
| 146 | } |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 147 | pr_info("Remount options were %s for vfsmnt %p.\n", options, mnt); |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 148 | err = parse_options_remount(sb, options, *flags & ~MS_SILENT, mnt->data); |
| 149 | |
| 150 | |
| 151 | return err; |
| 152 | } |
| 153 | |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 154 | static void *sdcardfs_clone_mnt_data(void *data) |
| 155 | { |
| 156 | struct sdcardfs_vfsmount_options *opt = kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL); |
| 157 | struct sdcardfs_vfsmount_options *old = data; |
| 158 | |
| 159 | if (!opt) |
| 160 | return NULL; |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 161 | opt->gid = old->gid; |
| 162 | opt->mask = old->mask; |
| 163 | return opt; |
| 164 | } |
| 165 | |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 166 | static void sdcardfs_copy_mnt_data(void *data, void *newdata) |
| 167 | { |
| 168 | struct sdcardfs_vfsmount_options *old = data; |
| 169 | struct sdcardfs_vfsmount_options *new = newdata; |
| 170 | |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 171 | old->gid = new->gid; |
| 172 | old->mask = new->mask; |
| 173 | } |
| 174 | |
| 175 | /* |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 176 | * Called by iput() when the inode reference count reached zero |
| 177 | * and the inode is not hashed anywhere. Used to clear anything |
| 178 | * that needs to be, before the inode is completely destroyed and put |
| 179 | * on the inode free list. |
| 180 | */ |
| 181 | static void sdcardfs_evict_inode(struct inode *inode) |
| 182 | { |
| 183 | struct inode *lower_inode; |
| 184 | |
| 185 | truncate_inode_pages(&inode->i_data, 0); |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 186 | set_top(SDCARDFS_I(inode), NULL); |
Daniel Campello | df58a30 | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 187 | clear_inode(inode); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 188 | /* |
| 189 | * Decrement a reference to a lower_inode, which was incremented |
| 190 | * by our read_inode when it was created initially. |
| 191 | */ |
| 192 | lower_inode = sdcardfs_lower_inode(inode); |
| 193 | sdcardfs_set_lower_inode(inode, NULL); |
| 194 | iput(lower_inode); |
| 195 | } |
| 196 | |
| 197 | static struct inode *sdcardfs_alloc_inode(struct super_block *sb) |
| 198 | { |
| 199 | struct sdcardfs_inode_info *i; |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 200 | struct sdcardfs_inode_data *d; |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 201 | |
| 202 | i = kmem_cache_alloc(sdcardfs_inode_cachep, GFP_KERNEL); |
| 203 | if (!i) |
| 204 | return NULL; |
| 205 | |
| 206 | /* memset everything up to the inode to 0 */ |
| 207 | memset(i, 0, offsetof(struct sdcardfs_inode_info, vfs_inode)); |
| 208 | |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 209 | d = kmem_cache_alloc(sdcardfs_inode_data_cachep, |
| 210 | GFP_KERNEL | __GFP_ZERO); |
| 211 | if (!d) { |
| 212 | kmem_cache_free(sdcardfs_inode_cachep, i); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | i->data = d; |
| 217 | kref_init(&d->refcount); |
Daniel Rosenberg | 108bd1b | 2018-02-01 16:52:22 -0800 | [diff] [blame] | 218 | i->top_data = d; |
| 219 | spin_lock_init(&i->top_lock); |
| 220 | kref_get(&d->refcount); |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 221 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 222 | i->vfs_inode.i_version = 1; |
| 223 | return &i->vfs_inode; |
| 224 | } |
| 225 | |
Gao Xiang | 25a818e | 2017-05-10 23:01:15 +0800 | [diff] [blame] | 226 | static void i_callback(struct rcu_head *head) |
| 227 | { |
| 228 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 229 | |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 230 | release_own_data(SDCARDFS_I(inode)); |
Gao Xiang | 25a818e | 2017-05-10 23:01:15 +0800 | [diff] [blame] | 231 | kmem_cache_free(sdcardfs_inode_cachep, SDCARDFS_I(inode)); |
| 232 | } |
| 233 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 234 | static void sdcardfs_destroy_inode(struct inode *inode) |
| 235 | { |
Gao Xiang | 25a818e | 2017-05-10 23:01:15 +0800 | [diff] [blame] | 236 | call_rcu(&inode->i_rcu, i_callback); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* sdcardfs inode cache constructor */ |
| 240 | static void init_once(void *obj) |
| 241 | { |
| 242 | struct sdcardfs_inode_info *i = obj; |
| 243 | |
| 244 | inode_init_once(&i->vfs_inode); |
| 245 | } |
| 246 | |
| 247 | int sdcardfs_init_inode_cache(void) |
| 248 | { |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 249 | sdcardfs_inode_cachep = |
| 250 | kmem_cache_create("sdcardfs_inode_cache", |
| 251 | sizeof(struct sdcardfs_inode_info), 0, |
| 252 | SLAB_RECLAIM_ACCOUNT, init_once); |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 253 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 254 | if (!sdcardfs_inode_cachep) |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 255 | return -ENOMEM; |
| 256 | |
| 257 | sdcardfs_inode_data_cachep = |
| 258 | kmem_cache_create("sdcardfs_inode_data_cache", |
| 259 | sizeof(struct sdcardfs_inode_data), 0, |
| 260 | SLAB_RECLAIM_ACCOUNT, NULL); |
| 261 | if (!sdcardfs_inode_data_cachep) { |
| 262 | kmem_cache_destroy(sdcardfs_inode_cachep); |
| 263 | return -ENOMEM; |
| 264 | } |
| 265 | |
| 266 | return 0; |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* sdcardfs inode cache destructor */ |
| 270 | void sdcardfs_destroy_inode_cache(void) |
| 271 | { |
Daniel Rosenberg | 638eed3 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 272 | kmem_cache_destroy(sdcardfs_inode_data_cachep); |
Daniel Rosenberg | ffe322f | 2017-03-21 16:29:13 -0700 | [diff] [blame] | 273 | kmem_cache_destroy(sdcardfs_inode_cachep); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Used only in nfs, to kill any pending RPC tasks, so that subsequent |
| 278 | * code can actually succeed and won't leave tasks that need handling. |
| 279 | */ |
| 280 | static void sdcardfs_umount_begin(struct super_block *sb) |
| 281 | { |
| 282 | struct super_block *lower_sb; |
| 283 | |
| 284 | lower_sb = sdcardfs_lower_super(sb); |
| 285 | if (lower_sb && lower_sb->s_op && lower_sb->s_op->umount_begin) |
| 286 | lower_sb->s_op->umount_begin(lower_sb); |
| 287 | } |
| 288 | |
Daniel Rosenberg | 6043c64e | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 289 | static int sdcardfs_show_options(struct vfsmount *mnt, struct seq_file *m, |
| 290 | struct dentry *root) |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 291 | { |
Daniel Campello | df58a30 | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 292 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(root->d_sb); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 293 | struct sdcardfs_mount_options *opts = &sbi->options; |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 294 | struct sdcardfs_vfsmount_options *vfsopts = mnt->data; |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 295 | |
| 296 | if (opts->fs_low_uid != 0) |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 297 | seq_printf(m, ",fsuid=%u", opts->fs_low_uid); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 298 | if (opts->fs_low_gid != 0) |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 299 | seq_printf(m, ",fsgid=%u", opts->fs_low_gid); |
| 300 | if (vfsopts->gid != 0) |
| 301 | seq_printf(m, ",gid=%u", vfsopts->gid); |
Daniel Rosenberg | 7fc95a9 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 302 | if (opts->multiuser) |
Daniel Rosenberg | e265634 | 2017-03-21 17:27:40 -0700 | [diff] [blame] | 303 | seq_puts(m, ",multiuser"); |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 304 | if (vfsopts->mask) |
| 305 | seq_printf(m, ",mask=%u", vfsopts->mask); |
| 306 | if (opts->fs_user_id) |
| 307 | seq_printf(m, ",userid=%u", opts->fs_user_id); |
Daniel Rosenberg | 53a0882 | 2017-07-19 17:25:07 -0700 | [diff] [blame] | 308 | if (opts->gid_derivation) |
| 309 | seq_puts(m, ",derive_gid"); |
Daniel Rosenberg | 6c72cab | 2018-01-02 14:44:49 -0800 | [diff] [blame] | 310 | if (opts->default_normal) |
| 311 | seq_puts(m, ",default_normal"); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 312 | if (opts->reserved_mb != 0) |
| 313 | seq_printf(m, ",reserved=%uMB", opts->reserved_mb); |
Daniel Rosenberg | 12064f3 | 2018-07-06 16:24:27 -0700 | [diff] [blame] | 314 | if (opts->nocache) |
| 315 | seq_printf(m, ",nocache"); |
Daniel Rosenberg | fe1e70b | 2019-05-21 14:56:39 -0700 | [diff] [blame] | 316 | if (opts->unshared_obb) |
| 317 | seq_printf(m, ",unshared_obb"); |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | }; |
| 321 | |
Eric Biggers | 3e5dc4e | 2019-10-09 14:35:49 -0700 | [diff] [blame] | 322 | int sdcardfs_on_fscrypt_key_removed(struct notifier_block *nb, |
| 323 | unsigned long action, void *data) |
| 324 | { |
| 325 | struct sdcardfs_sb_info *sbi = container_of(nb, struct sdcardfs_sb_info, |
| 326 | fscrypt_nb); |
| 327 | |
| 328 | /* |
| 329 | * Evict any unused sdcardfs dentries (and hence any unused sdcardfs |
| 330 | * inodes, since sdcardfs doesn't cache unpinned inodes by themselves) |
| 331 | * so that the lower filesystem's encrypted inodes can be evicted. |
| 332 | * This is needed to make the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl |
| 333 | * properly "lock" the files underneath the sdcardfs mount. |
| 334 | */ |
| 335 | shrink_dcache_sb(sbi->sb); |
| 336 | return NOTIFY_OK; |
| 337 | } |
| 338 | |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 339 | const struct super_operations sdcardfs_sops = { |
| 340 | .put_super = sdcardfs_put_super, |
| 341 | .statfs = sdcardfs_statfs, |
| 342 | .remount_fs = sdcardfs_remount_fs, |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 343 | .remount_fs2 = sdcardfs_remount_fs2, |
| 344 | .clone_mnt_data = sdcardfs_clone_mnt_data, |
| 345 | .copy_mnt_data = sdcardfs_copy_mnt_data, |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 346 | .evict_inode = sdcardfs_evict_inode, |
| 347 | .umount_begin = sdcardfs_umount_begin, |
Daniel Rosenberg | 8246a78 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 348 | .show_options2 = sdcardfs_show_options, |
Daniel Campello | 819e203 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 349 | .alloc_inode = sdcardfs_alloc_inode, |
| 350 | .destroy_inode = sdcardfs_destroy_inode, |
| 351 | .drop_inode = generic_delete_inode, |
| 352 | }; |