• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/fs_context.h>
22 
23 #include "sdcardfs.h"
24 
25 /*
26  * The inode cache is used with alloc_inode for both our inode info and the
27  * vfs inode.
28  */
29 static struct kmem_cache *sdcardfs_inode_cachep;
30 
31 /*
32  * To support the top references, we must track some data separately.
33  * An sdcardfs_inode_info always has a reference to its data, and once set up,
34  * also has a reference to its top. The top may be itself, in which case it
35  * holds two references to its data. When top is changed, it takes a ref to the
36  * new data and then drops the ref to the old data.
37  */
38 static struct kmem_cache *sdcardfs_inode_data_cachep;
39 
data_release(struct kref * ref)40 void data_release(struct kref *ref)
41 {
42 	struct sdcardfs_inode_data *data =
43 		container_of(ref, struct sdcardfs_inode_data, refcount);
44 
45 	kmem_cache_free(sdcardfs_inode_data_cachep, data);
46 }
47 
48 /* final actions when unmounting a file system */
sdcardfs_put_super(struct super_block * sb)49 static void sdcardfs_put_super(struct super_block *sb)
50 {
51 	struct sdcardfs_sb_info *spd;
52 	struct super_block *s;
53 
54 	spd = SDCARDFS_SB(sb);
55 	if (!spd)
56 		return;
57 
58 	if (spd->obbpath_s) {
59 		kfree(spd->obbpath_s);
60 		path_put(&spd->obbpath);
61 	}
62 
63 	/* decrement lower super references */
64 	s = sdcardfs_lower_super(sb);
65 	sdcardfs_set_lower_super(sb, NULL);
66 	atomic_dec(&s->s_active);
67 
68 	kfree(spd);
69 	sb->s_fs_info = NULL;
70 }
71 
sdcardfs_statfs(struct dentry * dentry,struct kstatfs * buf)72 static int sdcardfs_statfs(struct dentry *dentry, struct kstatfs *buf)
73 {
74 	int err;
75 	struct path lower_path;
76 	u32 min_blocks;
77 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
78 
79 	sdcardfs_get_lower_path(dentry, &lower_path);
80 	err = vfs_statfs(&lower_path, buf);
81 	sdcardfs_put_lower_path(dentry, &lower_path);
82 
83 	if (sbi->options.reserved_mb) {
84 		/* Invalid statfs informations. */
85 		if (buf->f_bsize == 0) {
86 			pr_err("Returned block size is zero.\n");
87 			return -EINVAL;
88 		}
89 
90 		min_blocks = ((sbi->options.reserved_mb * 1024 * 1024)/buf->f_bsize);
91 		buf->f_blocks -= min_blocks;
92 
93 		if (buf->f_bavail > min_blocks)
94 			buf->f_bavail -= min_blocks;
95 		else
96 			buf->f_bavail = 0;
97 
98 		/* Make reserved blocks invisiable to media storage */
99 		buf->f_bfree = buf->f_bavail;
100 	}
101 
102 	/* set return buf to our f/s to avoid confusing user-level utils */
103 	buf->f_type = SDCARDFS_SUPER_MAGIC;
104 
105 	return err;
106 }
107 
sdcardfs_clone_mnt_data(void * data)108 static void *sdcardfs_clone_mnt_data(void *data)
109 {
110 	struct sdcardfs_vfsmount_options *opt = kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL);
111 	struct sdcardfs_vfsmount_options *old = data;
112 
113 	if (!opt)
114 		return NULL;
115 	opt->gid = old->gid;
116 	opt->mask = old->mask;
117 	return opt;
118 }
119 
sdcardfs_copy_mnt_data(void * data,void * newdata)120 static void sdcardfs_copy_mnt_data(void *data, void *newdata)
121 {
122 	struct sdcardfs_vfsmount_options *old = data;
123 	struct sdcardfs_vfsmount_options *new = newdata;
124 
125 	old->gid = new->gid;
126 	old->mask = new->mask;
127 }
128 
sdcardfs_update_mnt_data(void * data,struct fs_context * fc)129 static void sdcardfs_update_mnt_data(void *data, struct fs_context *fc)
130 {
131 	struct sdcardfs_vfsmount_options *opts = data;
132 	struct sdcardfs_context_options *fcopts = fc->fs_private;
133 
134 	opts->gid = fcopts->vfsopts.gid;
135 	opts->mask = fcopts->vfsopts.mask;
136 }
137 
138 /*
139  * Called by iput() when the inode reference count reached zero
140  * and the inode is not hashed anywhere.  Used to clear anything
141  * that needs to be, before the inode is completely destroyed and put
142  * on the inode free list.
143  */
sdcardfs_evict_inode(struct inode * inode)144 static void sdcardfs_evict_inode(struct inode *inode)
145 {
146 	struct inode *lower_inode;
147 
148 	truncate_inode_pages(&inode->i_data, 0);
149 	set_top(SDCARDFS_I(inode), NULL);
150 	clear_inode(inode);
151 	/*
152 	 * Decrement a reference to a lower_inode, which was incremented
153 	 * by our read_inode when it was created initially.
154 	 */
155 	lower_inode = sdcardfs_lower_inode(inode);
156 	sdcardfs_set_lower_inode(inode, NULL);
157 	iput(lower_inode);
158 }
159 
sdcardfs_alloc_inode(struct super_block * sb)160 static struct inode *sdcardfs_alloc_inode(struct super_block *sb)
161 {
162 	struct sdcardfs_inode_info *i;
163 	struct sdcardfs_inode_data *d;
164 
165 	i = kmem_cache_alloc(sdcardfs_inode_cachep, GFP_KERNEL);
166 	if (!i)
167 		return NULL;
168 
169 	/* memset everything up to the inode to 0 */
170 	memset(i, 0, offsetof(struct sdcardfs_inode_info, vfs_inode));
171 
172 	d = kmem_cache_alloc(sdcardfs_inode_data_cachep,
173 					GFP_KERNEL | __GFP_ZERO);
174 	if (!d) {
175 		kmem_cache_free(sdcardfs_inode_cachep, i);
176 		return NULL;
177 	}
178 
179 	i->data = d;
180 	kref_init(&d->refcount);
181 	i->top_data = d;
182 	spin_lock_init(&i->top_lock);
183 	kref_get(&d->refcount);
184 
185 	inode_set_iversion(&i->vfs_inode, 1);
186 	return &i->vfs_inode;
187 }
188 
i_callback(struct rcu_head * head)189 static void i_callback(struct rcu_head *head)
190 {
191 	struct inode *inode = container_of(head, struct inode, i_rcu);
192 
193 	release_own_data(SDCARDFS_I(inode));
194 	kmem_cache_free(sdcardfs_inode_cachep, SDCARDFS_I(inode));
195 }
196 
sdcardfs_destroy_inode(struct inode * inode)197 static void sdcardfs_destroy_inode(struct inode *inode)
198 {
199 	call_rcu(&inode->i_rcu, i_callback);
200 }
201 
202 /* sdcardfs inode cache constructor */
init_once(void * obj)203 static void init_once(void *obj)
204 {
205 	struct sdcardfs_inode_info *i = obj;
206 
207 	inode_init_once(&i->vfs_inode);
208 }
209 
sdcardfs_init_inode_cache(void)210 int sdcardfs_init_inode_cache(void)
211 {
212 	sdcardfs_inode_cachep =
213 		kmem_cache_create("sdcardfs_inode_cache",
214 				  sizeof(struct sdcardfs_inode_info), 0,
215 				  SLAB_RECLAIM_ACCOUNT, init_once);
216 
217 	if (!sdcardfs_inode_cachep)
218 		return -ENOMEM;
219 
220 	sdcardfs_inode_data_cachep =
221 		kmem_cache_create("sdcardfs_inode_data_cache",
222 				  sizeof(struct sdcardfs_inode_data), 0,
223 				  SLAB_RECLAIM_ACCOUNT, NULL);
224 	if (!sdcardfs_inode_data_cachep) {
225 		kmem_cache_destroy(sdcardfs_inode_cachep);
226 		return -ENOMEM;
227 	}
228 
229 	return 0;
230 }
231 
232 /* sdcardfs inode cache destructor */
sdcardfs_destroy_inode_cache(void)233 void sdcardfs_destroy_inode_cache(void)
234 {
235 	kmem_cache_destroy(sdcardfs_inode_data_cachep);
236 	kmem_cache_destroy(sdcardfs_inode_cachep);
237 }
238 
239 /*
240  * Used only in nfs, to kill any pending RPC tasks, so that subsequent
241  * code can actually succeed and won't leave tasks that need handling.
242  */
sdcardfs_umount_begin(struct super_block * sb)243 static void sdcardfs_umount_begin(struct super_block *sb)
244 {
245 	struct super_block *lower_sb;
246 
247 	lower_sb = sdcardfs_lower_super(sb);
248 	if (lower_sb && lower_sb->s_op && lower_sb->s_op->umount_begin)
249 		lower_sb->s_op->umount_begin(lower_sb);
250 }
251 
sdcardfs_show_options(struct vfsmount * mnt,struct seq_file * m,struct dentry * root)252 static int sdcardfs_show_options(struct vfsmount *mnt, struct seq_file *m,
253 			struct dentry *root)
254 {
255 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(root->d_sb);
256 	struct sdcardfs_mount_options *opts = &sbi->options;
257 	struct sdcardfs_vfsmount_options *vfsopts = mnt->data;
258 
259 	if (opts->fs_low_uid != 0)
260 		seq_printf(m, ",fsuid=%u", opts->fs_low_uid);
261 	if (opts->fs_low_gid != 0)
262 		seq_printf(m, ",fsgid=%u", opts->fs_low_gid);
263 	if (vfsopts->gid != 0)
264 		seq_printf(m, ",gid=%u", vfsopts->gid);
265 	if (opts->multiuser)
266 		seq_puts(m, ",multiuser");
267 	if (vfsopts->mask)
268 		seq_printf(m, ",mask=%u", vfsopts->mask);
269 	if (opts->fs_user_id)
270 		seq_printf(m, ",userid=%u", opts->fs_user_id);
271 	if (opts->gid_derivation)
272 		seq_puts(m, ",derive_gid");
273 	if (opts->default_normal)
274 		seq_puts(m, ",default_normal");
275 	if (opts->reserved_mb != 0)
276 		seq_printf(m, ",reserved=%uMB", opts->reserved_mb);
277 	if (opts->nocache)
278 		seq_printf(m, ",nocache");
279 	if (opts->unshared_obb)
280 		seq_printf(m, ",unshared_obb");
281 
282 	return 0;
283 };
284 
sdcardfs_on_fscrypt_key_removed(struct notifier_block * nb,unsigned long action,void * data)285 int sdcardfs_on_fscrypt_key_removed(struct notifier_block *nb,
286 				    unsigned long action, void *data)
287 {
288 	struct sdcardfs_sb_info *sbi = container_of(nb, struct sdcardfs_sb_info,
289 						    fscrypt_nb);
290 
291 	/*
292 	 * Evict any unused sdcardfs dentries (and hence any unused sdcardfs
293 	 * inodes, since sdcardfs doesn't cache unpinned inodes by themselves)
294 	 * so that the lower filesystem's encrypted inodes can be evicted.
295 	 * This is needed to make the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl
296 	 * properly "lock" the files underneath the sdcardfs mount.
297 	 */
298 	shrink_dcache_sb(sbi->sb);
299 	return NOTIFY_OK;
300 }
301 
302 const struct super_operations sdcardfs_sops = {
303 	.put_super	= sdcardfs_put_super,
304 	.statfs		= sdcardfs_statfs,
305 	.clone_mnt_data	= sdcardfs_clone_mnt_data,
306 	.copy_mnt_data	= sdcardfs_copy_mnt_data,
307 	.update_mnt_data = sdcardfs_update_mnt_data,
308 	.evict_inode	= sdcardfs_evict_inode,
309 	.umount_begin	= sdcardfs_umount_begin,
310 	.show_options2	= sdcardfs_show_options,
311 	.alloc_inode	= sdcardfs_alloc_inode,
312 	.destroy_inode	= sdcardfs_destroy_inode,
313 	.drop_inode	= generic_delete_inode,
314 };
315