Daniel Campello | 9fbe866 | 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 | 9fbe866 | 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 | /* |
| 112 | * Called by iput() when the inode reference count reached zero |
| 113 | * and the inode is not hashed anywhere. Used to clear anything |
| 114 | * that needs to be, before the inode is completely destroyed and put |
| 115 | * on the inode free list. |
| 116 | */ |
| 117 | static void sdcardfs_evict_inode(struct inode *inode) |
| 118 | { |
| 119 | struct inode *lower_inode; |
| 120 | |
| 121 | truncate_inode_pages(&inode->i_data, 0); |
Daniel Campello | e84c525 | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 122 | clear_inode(inode); |
Daniel Campello | 9fbe866 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Decrement a reference to a lower_inode, which was incremented |
| 125 | * by our read_inode when it was created initially. |
| 126 | */ |
| 127 | lower_inode = sdcardfs_lower_inode(inode); |
| 128 | sdcardfs_set_lower_inode(inode, NULL); |
| 129 | iput(lower_inode); |
| 130 | } |
| 131 | |
| 132 | static struct inode *sdcardfs_alloc_inode(struct super_block *sb) |
| 133 | { |
| 134 | struct sdcardfs_inode_info *i; |
| 135 | |
| 136 | i = kmem_cache_alloc(sdcardfs_inode_cachep, GFP_KERNEL); |
| 137 | if (!i) |
| 138 | return NULL; |
| 139 | |
| 140 | /* memset everything up to the inode to 0 */ |
| 141 | memset(i, 0, offsetof(struct sdcardfs_inode_info, vfs_inode)); |
| 142 | |
| 143 | i->vfs_inode.i_version = 1; |
| 144 | return &i->vfs_inode; |
| 145 | } |
| 146 | |
| 147 | static void sdcardfs_destroy_inode(struct inode *inode) |
| 148 | { |
| 149 | kmem_cache_free(sdcardfs_inode_cachep, SDCARDFS_I(inode)); |
| 150 | } |
| 151 | |
| 152 | /* sdcardfs inode cache constructor */ |
| 153 | static void init_once(void *obj) |
| 154 | { |
| 155 | struct sdcardfs_inode_info *i = obj; |
| 156 | |
| 157 | inode_init_once(&i->vfs_inode); |
| 158 | } |
| 159 | |
| 160 | int sdcardfs_init_inode_cache(void) |
| 161 | { |
| 162 | int err = 0; |
| 163 | |
| 164 | sdcardfs_inode_cachep = |
| 165 | kmem_cache_create("sdcardfs_inode_cache", |
| 166 | sizeof(struct sdcardfs_inode_info), 0, |
| 167 | SLAB_RECLAIM_ACCOUNT, init_once); |
| 168 | if (!sdcardfs_inode_cachep) |
| 169 | err = -ENOMEM; |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | /* sdcardfs inode cache destructor */ |
| 174 | void sdcardfs_destroy_inode_cache(void) |
| 175 | { |
| 176 | if (sdcardfs_inode_cachep) |
| 177 | kmem_cache_destroy(sdcardfs_inode_cachep); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Used only in nfs, to kill any pending RPC tasks, so that subsequent |
| 182 | * code can actually succeed and won't leave tasks that need handling. |
| 183 | */ |
| 184 | static void sdcardfs_umount_begin(struct super_block *sb) |
| 185 | { |
| 186 | struct super_block *lower_sb; |
| 187 | |
| 188 | lower_sb = sdcardfs_lower_super(sb); |
| 189 | if (lower_sb && lower_sb->s_op && lower_sb->s_op->umount_begin) |
| 190 | lower_sb->s_op->umount_begin(lower_sb); |
| 191 | } |
| 192 | |
Daniel Campello | e84c525 | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 193 | static int sdcardfs_show_options(struct seq_file *m, struct dentry *root) |
Daniel Campello | 9fbe866 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 194 | { |
Daniel Campello | e84c525 | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 195 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(root->d_sb); |
Daniel Campello | 9fbe866 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 196 | struct sdcardfs_mount_options *opts = &sbi->options; |
| 197 | |
| 198 | if (opts->fs_low_uid != 0) |
| 199 | seq_printf(m, ",uid=%u", opts->fs_low_uid); |
| 200 | if (opts->fs_low_gid != 0) |
| 201 | seq_printf(m, ",gid=%u", opts->fs_low_gid); |
| 202 | |
Daniel Rosenberg | 44f555a | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 203 | if (opts->multiuser) |
| 204 | seq_printf(m, ",multiuser"); |
Daniel Campello | 9fbe866 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 205 | |
| 206 | if (opts->reserved_mb != 0) |
| 207 | seq_printf(m, ",reserved=%uMB", opts->reserved_mb); |
| 208 | |
| 209 | return 0; |
| 210 | }; |
| 211 | |
| 212 | const struct super_operations sdcardfs_sops = { |
| 213 | .put_super = sdcardfs_put_super, |
| 214 | .statfs = sdcardfs_statfs, |
| 215 | .remount_fs = sdcardfs_remount_fs, |
| 216 | .evict_inode = sdcardfs_evict_inode, |
| 217 | .umount_begin = sdcardfs_umount_begin, |
| 218 | .show_options = sdcardfs_show_options, |
| 219 | .alloc_inode = sdcardfs_alloc_inode, |
| 220 | .destroy_inode = sdcardfs_destroy_inode, |
| 221 | .drop_inode = generic_delete_inode, |
| 222 | }; |