4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
22 #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
23 #include <linux/backing-dev.h>
26 static ssize_t sdcardfs_read(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos)
30 struct file *lower_file;
31 struct dentry *dentry = file->f_path.dentry;
32 #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
33 struct backing_dev_info *bdi;
36 lower_file = sdcardfs_lower_file(file);
38 #ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
39 if (file->f_mode & FMODE_NOACTIVE) {
40 if (!(lower_file->f_mode & FMODE_NOACTIVE)) {
41 bdi = lower_file->f_mapping->backing_dev_info;
42 lower_file->f_ra.ra_pages = bdi->ra_pages * 2;
43 spin_lock(&lower_file->f_lock);
44 lower_file->f_mode |= FMODE_NOACTIVE;
45 spin_unlock(&lower_file->f_lock);
50 err = vfs_read(lower_file, buf, count, ppos);
51 /* update our inode atime upon a successful lower read */
53 fsstack_copy_attr_atime(dentry->d_inode,
54 lower_file->f_path.dentry->d_inode);
59 static ssize_t sdcardfs_write(struct file *file, const char __user *buf,
60 size_t count, loff_t *ppos)
63 struct file *lower_file;
64 struct dentry *dentry = file->f_path.dentry;
66 /* check disk space */
67 if (!check_min_free_space(dentry, count, 0)) {
68 printk(KERN_INFO "No minimum free space.\n");
72 lower_file = sdcardfs_lower_file(file);
73 err = vfs_write(lower_file, buf, count, ppos);
74 /* update our inode times+sizes upon a successful lower write */
76 fsstack_copy_inode_size(dentry->d_inode,
77 lower_file->f_path.dentry->d_inode);
78 fsstack_copy_attr_times(dentry->d_inode,
79 lower_file->f_path.dentry->d_inode);
85 static int sdcardfs_readdir(struct file *file, void *dirent, filldir_t filldir)
88 struct file *lower_file = NULL;
89 struct dentry *dentry = file->f_path.dentry;
91 lower_file = sdcardfs_lower_file(file);
93 lower_file->f_pos = file->f_pos;
94 err = vfs_readdir(lower_file, filldir, dirent);
95 file->f_pos = lower_file->f_pos;
96 if (err >= 0) /* copy the atime */
97 fsstack_copy_attr_atime(dentry->d_inode,
98 lower_file->f_path.dentry->d_inode);
102 static long sdcardfs_unlocked_ioctl(struct file *file, unsigned int cmd,
106 struct file *lower_file;
108 lower_file = sdcardfs_lower_file(file);
110 /* XXX: use vfs_ioctl if/when VFS exports it */
111 if (!lower_file || !lower_file->f_op)
113 if (lower_file->f_op->unlocked_ioctl)
114 err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
121 static long sdcardfs_compat_ioctl(struct file *file, unsigned int cmd,
125 struct file *lower_file;
127 lower_file = sdcardfs_lower_file(file);
129 /* XXX: use vfs_ioctl if/when VFS exports it */
130 if (!lower_file || !lower_file->f_op)
132 if (lower_file->f_op->compat_ioctl)
133 err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
140 static int sdcardfs_mmap(struct file *file, struct vm_area_struct *vma)
144 struct file *lower_file;
145 const struct vm_operations_struct *saved_vm_ops = NULL;
147 /* this might be deferred to mmap's writepage */
148 willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
151 * File systems which do not implement ->writepage may use
152 * generic_file_readonly_mmap as their ->mmap op. If you call
153 * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
154 * But we cannot call the lower ->mmap op, so we can't tell that
155 * writeable mappings won't work. Therefore, our only choice is to
156 * check if the lower file system supports the ->writepage, and if
157 * not, return EINVAL (the same error that
158 * generic_file_readonly_mmap returns in that case).
160 lower_file = sdcardfs_lower_file(file);
161 if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
163 printk(KERN_ERR "sdcardfs: lower file system does not "
164 "support writeable mmap\n");
169 * find and save lower vm_ops.
171 * XXX: the VFS should have a cleaner way of finding the lower vm_ops
173 if (!SDCARDFS_F(file)->lower_vm_ops) {
174 err = lower_file->f_op->mmap(lower_file, vma);
176 printk(KERN_ERR "sdcardfs: lower mmap failed %d\n", err);
179 saved_vm_ops = vma->vm_ops; /* save: came from lower ->mmap */
180 err = do_munmap(current->mm, vma->vm_start,
181 vma->vm_end - vma->vm_start);
183 printk(KERN_ERR "sdcardfs: do_munmap failed %d\n", err);
189 * Next 3 lines are all I need from generic_file_mmap. I definitely
190 * don't want its test for ->readpage which returns -ENOEXEC.
193 vma->vm_ops = &sdcardfs_vm_ops;
194 vma->vm_flags |= VM_CAN_NONLINEAR;
196 file->f_mapping->a_ops = &sdcardfs_aops; /* set our aops */
197 if (!SDCARDFS_F(file)->lower_vm_ops) /* save for our ->fault */
198 SDCARDFS_F(file)->lower_vm_ops = saved_vm_ops;
204 static int sdcardfs_open(struct inode *inode, struct file *file)
207 struct file *lower_file = NULL;
208 struct path lower_path;
209 struct dentry *dentry = file->f_path.dentry;
210 struct dentry *parent = dget_parent(dentry);
211 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
212 const struct cred *saved_cred = NULL;
215 /* don't open unhashed/deleted files */
216 if (d_unhashed(dentry)) {
221 has_rw = get_caller_has_rw_locked(sbi->pkgl_id, sbi->options.derive);
223 if(!check_caller_access_to_name(parent->d_inode, dentry->d_name.name,
225 open_flags_to_access_mode(file->f_flags), has_rw)) {
226 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
227 " dentry: %s, task:%s\n",
228 __func__, dentry->d_name.name, current->comm);
233 /* save current_cred and override it */
234 OVERRIDE_CRED(sbi, saved_cred);
237 kzalloc(sizeof(struct sdcardfs_file_info), GFP_KERNEL);
238 if (!SDCARDFS_F(file)) {
240 goto out_revert_cred;
243 /* open lower object and link sdcardfs's file struct to lower's */
244 sdcardfs_get_lower_path(file->f_path.dentry, &lower_path);
245 lower_file = dentry_open(lower_path.dentry, lower_path.mnt,
246 file->f_flags, current_cred());
247 if (IS_ERR(lower_file)) {
248 err = PTR_ERR(lower_file);
249 lower_file = sdcardfs_lower_file(file);
251 sdcardfs_set_lower_file(file, NULL);
252 fput(lower_file); /* fput calls dput for lower_dentry */
255 sdcardfs_set_lower_file(file, lower_file);
259 kfree(SDCARDFS_F(file));
261 fsstack_copy_attr_all(inode, sdcardfs_lower_inode(inode));
262 fix_derived_permission(inode);
266 REVERT_CRED(saved_cred);
272 static int sdcardfs_flush(struct file *file, fl_owner_t id)
275 struct file *lower_file = NULL;
277 lower_file = sdcardfs_lower_file(file);
278 if (lower_file && lower_file->f_op && lower_file->f_op->flush)
279 err = lower_file->f_op->flush(lower_file, id);
284 /* release all lower object references & free the file info structure */
285 static int sdcardfs_file_release(struct inode *inode, struct file *file)
287 struct file *lower_file;
289 lower_file = sdcardfs_lower_file(file);
291 sdcardfs_set_lower_file(file, NULL);
295 kfree(SDCARDFS_F(file));
300 sdcardfs_fsync(struct file *file, int datasync)
303 struct file *lower_file;
304 struct path lower_path;
305 struct dentry *dentry = file->f_path.dentry;
307 lower_file = sdcardfs_lower_file(file);
308 sdcardfs_get_lower_path(dentry, &lower_path);
309 err = vfs_fsync(lower_file, datasync);
310 sdcardfs_put_lower_path(dentry, &lower_path);
315 static int sdcardfs_fasync(int fd, struct file *file, int flag)
318 struct file *lower_file = NULL;
320 lower_file = sdcardfs_lower_file(file);
321 if (lower_file->f_op && lower_file->f_op->fasync)
322 err = lower_file->f_op->fasync(fd, lower_file, flag);
327 const struct file_operations sdcardfs_main_fops = {
328 .llseek = generic_file_llseek,
329 .read = sdcardfs_read,
330 .write = sdcardfs_write,
331 .unlocked_ioctl = sdcardfs_unlocked_ioctl,
333 .compat_ioctl = sdcardfs_compat_ioctl,
335 .mmap = sdcardfs_mmap,
336 .open = sdcardfs_open,
337 .flush = sdcardfs_flush,
338 .release = sdcardfs_file_release,
339 .fsync = sdcardfs_fsync,
340 .fasync = sdcardfs_fasync,
343 /* trimmed directory options */
344 const struct file_operations sdcardfs_dir_fops = {
345 .llseek = generic_file_llseek,
346 .read = generic_read_dir,
347 .readdir = sdcardfs_readdir,
348 .unlocked_ioctl = sdcardfs_unlocked_ioctl,
350 .compat_ioctl = sdcardfs_compat_ioctl,
352 .open = sdcardfs_open,
353 .release = sdcardfs_file_release,
354 .flush = sdcardfs_flush,
355 .fsync = sdcardfs_fsync,
356 .fasync = sdcardfs_fasync,