Daniel Campello | 35c9e24 | 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 | |
| 29 | /* final actions when unmounting a file system */ |
| 30 | static void sdcardfs_put_super(struct super_block *sb) |
| 31 | { |
| 32 | struct sdcardfs_sb_info *spd; |
| 33 | struct super_block *s; |
| 34 | |
| 35 | spd = SDCARDFS_SB(sb); |
| 36 | if (!spd) |
| 37 | return; |
| 38 | |
| 39 | if(spd->obbpath_s) { |
| 40 | kfree(spd->obbpath_s); |
| 41 | path_put(&spd->obbpath); |
| 42 | } |
| 43 | |
| 44 | /* decrement lower super references */ |
| 45 | s = sdcardfs_lower_super(sb); |
| 46 | sdcardfs_set_lower_super(sb, NULL); |
| 47 | atomic_dec(&s->s_active); |
| 48 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 49 | kfree(spd); |
| 50 | sb->s_fs_info = NULL; |
| 51 | } |
| 52 | |
| 53 | static int sdcardfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 54 | { |
| 55 | int err; |
| 56 | struct path lower_path; |
| 57 | u32 min_blocks; |
| 58 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 59 | |
| 60 | sdcardfs_get_lower_path(dentry, &lower_path); |
| 61 | err = vfs_statfs(&lower_path, buf); |
| 62 | sdcardfs_put_lower_path(dentry, &lower_path); |
| 63 | |
| 64 | if (sbi->options.reserved_mb) { |
| 65 | /* Invalid statfs informations. */ |
| 66 | if (buf->f_bsize == 0) { |
| 67 | printk(KERN_ERR "Returned block size is zero.\n"); |
| 68 | return -EINVAL; |
| 69 | } |
| 70 | |
| 71 | min_blocks = ((sbi->options.reserved_mb * 1024 * 1024)/buf->f_bsize); |
| 72 | buf->f_blocks -= min_blocks; |
| 73 | |
| 74 | if (buf->f_bavail > min_blocks) |
| 75 | buf->f_bavail -= min_blocks; |
| 76 | else |
| 77 | buf->f_bavail = 0; |
| 78 | |
| 79 | /* Make reserved blocks invisiable to media storage */ |
| 80 | buf->f_bfree = buf->f_bavail; |
| 81 | } |
| 82 | |
| 83 | /* set return buf to our f/s to avoid confusing user-level utils */ |
| 84 | buf->f_type = SDCARDFS_SUPER_MAGIC; |
| 85 | |
| 86 | return err; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * @flags: numeric mount options |
| 91 | * @options: mount options string |
| 92 | */ |
| 93 | static int sdcardfs_remount_fs(struct super_block *sb, int *flags, char *options) |
| 94 | { |
| 95 | int err = 0; |
| 96 | |
| 97 | /* |
| 98 | * The VFS will take care of "ro" and "rw" flags among others. We |
| 99 | * can safely accept a few flags (RDONLY, MANDLOCK), and honor |
| 100 | * SILENT, but anything else left over is an error. |
| 101 | */ |
| 102 | if ((*flags & ~(MS_RDONLY | MS_MANDLOCK | MS_SILENT)) != 0) { |
| 103 | printk(KERN_ERR |
| 104 | "sdcardfs: remount flags 0x%x unsupported\n", *flags); |
| 105 | err = -EINVAL; |
| 106 | } |
| 107 | |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | /* |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 112 | * @mnt: mount point we are remounting |
| 113 | * @sb: superblock we are remounting |
| 114 | * @flags: numeric mount options |
| 115 | * @options: mount options string |
| 116 | */ |
| 117 | static int sdcardfs_remount_fs2(struct vfsmount *mnt, struct super_block *sb, |
| 118 | int *flags, char *options) |
| 119 | { |
| 120 | int err = 0; |
| 121 | |
| 122 | /* |
| 123 | * The VFS will take care of "ro" and "rw" flags among others. We |
| 124 | * can safely accept a few flags (RDONLY, MANDLOCK), and honor |
| 125 | * SILENT, but anything else left over is an error. |
| 126 | */ |
| 127 | if ((*flags & ~(MS_RDONLY | MS_MANDLOCK | MS_SILENT | MS_REMOUNT)) != 0) { |
| 128 | printk(KERN_ERR |
| 129 | "sdcardfs: remount flags 0x%x unsupported\n", *flags); |
| 130 | err = -EINVAL; |
| 131 | } |
| 132 | printk(KERN_INFO "Remount options were %s for vfsmnt %p.\n", options, mnt); |
| 133 | err = parse_options_remount(sb, options, *flags & ~MS_SILENT, mnt->data); |
| 134 | |
| 135 | |
| 136 | return err; |
| 137 | } |
| 138 | |
| 139 | static void* sdcardfs_clone_mnt_data(void *data) { |
| 140 | struct sdcardfs_vfsmount_options* opt = kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL); |
| 141 | struct sdcardfs_vfsmount_options* old = data; |
| 142 | if(!opt) return NULL; |
| 143 | opt->gid = old->gid; |
| 144 | opt->mask = old->mask; |
| 145 | return opt; |
| 146 | } |
| 147 | |
| 148 | static void sdcardfs_copy_mnt_data(void *data, void *newdata) { |
| 149 | struct sdcardfs_vfsmount_options* old = data; |
| 150 | struct sdcardfs_vfsmount_options* new = newdata; |
| 151 | old->gid = new->gid; |
| 152 | old->mask = new->mask; |
| 153 | } |
| 154 | |
| 155 | /* |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 156 | * Called by iput() when the inode reference count reached zero |
| 157 | * and the inode is not hashed anywhere. Used to clear anything |
| 158 | * that needs to be, before the inode is completely destroyed and put |
| 159 | * on the inode free list. |
| 160 | */ |
| 161 | static void sdcardfs_evict_inode(struct inode *inode) |
| 162 | { |
| 163 | struct inode *lower_inode; |
| 164 | |
| 165 | truncate_inode_pages(&inode->i_data, 0); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 166 | clear_inode(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 167 | /* |
| 168 | * Decrement a reference to a lower_inode, which was incremented |
| 169 | * by our read_inode when it was created initially. |
| 170 | */ |
| 171 | lower_inode = sdcardfs_lower_inode(inode); |
| 172 | sdcardfs_set_lower_inode(inode, NULL); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 173 | set_top(SDCARDFS_I(inode), inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 174 | iput(lower_inode); |
| 175 | } |
| 176 | |
| 177 | static struct inode *sdcardfs_alloc_inode(struct super_block *sb) |
| 178 | { |
| 179 | struct sdcardfs_inode_info *i; |
| 180 | |
| 181 | i = kmem_cache_alloc(sdcardfs_inode_cachep, GFP_KERNEL); |
| 182 | if (!i) |
| 183 | return NULL; |
| 184 | |
| 185 | /* memset everything up to the inode to 0 */ |
| 186 | memset(i, 0, offsetof(struct sdcardfs_inode_info, vfs_inode)); |
| 187 | |
| 188 | i->vfs_inode.i_version = 1; |
| 189 | return &i->vfs_inode; |
| 190 | } |
| 191 | |
| 192 | static void sdcardfs_destroy_inode(struct inode *inode) |
| 193 | { |
| 194 | kmem_cache_free(sdcardfs_inode_cachep, SDCARDFS_I(inode)); |
| 195 | } |
| 196 | |
| 197 | /* sdcardfs inode cache constructor */ |
| 198 | static void init_once(void *obj) |
| 199 | { |
| 200 | struct sdcardfs_inode_info *i = obj; |
| 201 | |
| 202 | inode_init_once(&i->vfs_inode); |
| 203 | } |
| 204 | |
| 205 | int sdcardfs_init_inode_cache(void) |
| 206 | { |
| 207 | int err = 0; |
| 208 | |
| 209 | sdcardfs_inode_cachep = |
| 210 | kmem_cache_create("sdcardfs_inode_cache", |
| 211 | sizeof(struct sdcardfs_inode_info), 0, |
| 212 | SLAB_RECLAIM_ACCOUNT, init_once); |
| 213 | if (!sdcardfs_inode_cachep) |
| 214 | err = -ENOMEM; |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | /* sdcardfs inode cache destructor */ |
| 219 | void sdcardfs_destroy_inode_cache(void) |
| 220 | { |
| 221 | if (sdcardfs_inode_cachep) |
| 222 | kmem_cache_destroy(sdcardfs_inode_cachep); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Used only in nfs, to kill any pending RPC tasks, so that subsequent |
| 227 | * code can actually succeed and won't leave tasks that need handling. |
| 228 | */ |
| 229 | static void sdcardfs_umount_begin(struct super_block *sb) |
| 230 | { |
| 231 | struct super_block *lower_sb; |
| 232 | |
| 233 | lower_sb = sdcardfs_lower_super(sb); |
| 234 | if (lower_sb && lower_sb->s_op && lower_sb->s_op->umount_begin) |
| 235 | lower_sb->s_op->umount_begin(lower_sb); |
| 236 | } |
| 237 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 238 | static int sdcardfs_show_options(struct vfsmount *mnt, struct seq_file *m, struct dentry *root) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 239 | { |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 240 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(root->d_sb); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 241 | struct sdcardfs_mount_options *opts = &sbi->options; |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 242 | struct sdcardfs_vfsmount_options *vfsopts = mnt->data; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 243 | |
| 244 | if (opts->fs_low_uid != 0) |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 245 | seq_printf(m, ",fsuid=%u", opts->fs_low_uid); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 246 | if (opts->fs_low_gid != 0) |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 247 | seq_printf(m, ",fsgid=%u", opts->fs_low_gid); |
| 248 | if (vfsopts->gid != 0) |
| 249 | seq_printf(m, ",gid=%u", vfsopts->gid); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 250 | if (opts->multiuser) |
| 251 | seq_printf(m, ",multiuser"); |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 252 | if (vfsopts->mask) |
| 253 | seq_printf(m, ",mask=%u", vfsopts->mask); |
| 254 | if (opts->fs_user_id) |
| 255 | seq_printf(m, ",userid=%u", opts->fs_user_id); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 256 | if (opts->reserved_mb != 0) |
| 257 | seq_printf(m, ",reserved=%uMB", opts->reserved_mb); |
| 258 | |
| 259 | return 0; |
| 260 | }; |
| 261 | |
| 262 | const struct super_operations sdcardfs_sops = { |
| 263 | .put_super = sdcardfs_put_super, |
| 264 | .statfs = sdcardfs_statfs, |
| 265 | .remount_fs = sdcardfs_remount_fs, |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 266 | .remount_fs2 = sdcardfs_remount_fs2, |
| 267 | .clone_mnt_data = sdcardfs_clone_mnt_data, |
| 268 | .copy_mnt_data = sdcardfs_copy_mnt_data, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 269 | .evict_inode = sdcardfs_evict_inode, |
| 270 | .umount_begin = sdcardfs_umount_begin, |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 271 | .show_options2 = sdcardfs_show_options, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 272 | .alloc_inode = sdcardfs_alloc_inode, |
| 273 | .destroy_inode = sdcardfs_destroy_inode, |
| 274 | .drop_inode = generic_delete_inode, |
| 275 | }; |