blob: 5ac0b0bbb0ec1c10092e3b19bb272d24ad6bdd82 [file] [log] [blame]
Daniel Campello35c9e242015-07-20 16:23:50 -07001/*
2 * fs/sdcardfs/file.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#ifdef CONFIG_SDCARD_FS_FADV_NOACTIVE
23#include <linux/backing-dev.h>
24#endif
25
26static ssize_t sdcardfs_read(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos)
28{
29 int err;
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;
34#endif
35
36 lower_file = sdcardfs_lower_file(file);
37
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);
46 }
47 }
48#endif
49
50 err = vfs_read(lower_file, buf, count, ppos);
51 /* update our inode atime upon a successful lower read */
52 if (err >= 0)
Daniel Campellod1d080c2015-07-20 16:27:37 -070053 fsstack_copy_attr_atime(d_inode(dentry),
54 file_inode(lower_file));
Daniel Campello35c9e242015-07-20 16:23:50 -070055
56 return err;
57}
58
59static ssize_t sdcardfs_write(struct file *file, const char __user *buf,
60 size_t count, loff_t *ppos)
61{
Daniel Campellod1d080c2015-07-20 16:27:37 -070062 int err;
Daniel Campello35c9e242015-07-20 16:23:50 -070063 struct file *lower_file;
64 struct dentry *dentry = file->f_path.dentry;
65
66 /* check disk space */
67 if (!check_min_free_space(dentry, count, 0)) {
Daniel Rosenberg77ecf212017-03-16 17:46:13 -070068 pr_err("No minimum free space.\n");
Daniel Campello35c9e242015-07-20 16:23:50 -070069 return -ENOSPC;
70 }
71
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 */
75 if (err >= 0) {
Daniel Campellod1d080c2015-07-20 16:27:37 -070076 fsstack_copy_inode_size(d_inode(dentry),
77 file_inode(lower_file));
78 fsstack_copy_attr_times(d_inode(dentry),
79 file_inode(lower_file));
Daniel Campello35c9e242015-07-20 16:23:50 -070080 }
81
82 return err;
83}
84
Daniel Campellod1d080c2015-07-20 16:27:37 -070085static int sdcardfs_readdir(struct file *file, struct dir_context *ctx)
Daniel Campello35c9e242015-07-20 16:23:50 -070086{
Daniel Campellod1d080c2015-07-20 16:27:37 -070087 int err;
Daniel Campello35c9e242015-07-20 16:23:50 -070088 struct file *lower_file = NULL;
89 struct dentry *dentry = file->f_path.dentry;
90
91 lower_file = sdcardfs_lower_file(file);
92
93 lower_file->f_pos = file->f_pos;
Daniel Campellod1d080c2015-07-20 16:27:37 -070094 err = iterate_dir(lower_file, ctx);
Daniel Campello35c9e242015-07-20 16:23:50 -070095 file->f_pos = lower_file->f_pos;
96 if (err >= 0) /* copy the atime */
Daniel Campellod1d080c2015-07-20 16:27:37 -070097 fsstack_copy_attr_atime(d_inode(dentry),
98 file_inode(lower_file));
Daniel Campello35c9e242015-07-20 16:23:50 -070099 return err;
100}
101
102static long sdcardfs_unlocked_ioctl(struct file *file, unsigned int cmd,
103 unsigned long arg)
104{
105 long err = -ENOTTY;
106 struct file *lower_file;
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700107 const struct cred *saved_cred = NULL;
108 struct dentry *dentry = file->f_path.dentry;
109 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Campello35c9e242015-07-20 16:23:50 -0700110
111 lower_file = sdcardfs_lower_file(file);
112
113 /* XXX: use vfs_ioctl if/when VFS exports it */
114 if (!lower_file || !lower_file->f_op)
115 goto out;
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700116
117 /* save current_cred and override it */
118 OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(file_inode(file)));
119
Daniel Campello35c9e242015-07-20 16:23:50 -0700120 if (lower_file->f_op->unlocked_ioctl)
121 err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
122
Daniel Rosenbergbe35f032017-03-09 21:24:58 -0800123 /* some ioctls can change inode attributes (EXT2_IOC_SETFLAGS) */
124 if (!err)
125 sdcardfs_copy_and_fix_attrs(file_inode(file),
126 file_inode(lower_file));
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700127 REVERT_CRED(saved_cred);
Daniel Campello35c9e242015-07-20 16:23:50 -0700128out:
129 return err;
130}
131
132#ifdef CONFIG_COMPAT
133static long sdcardfs_compat_ioctl(struct file *file, unsigned int cmd,
134 unsigned long arg)
135{
136 long err = -ENOTTY;
137 struct file *lower_file;
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700138 const struct cred *saved_cred = NULL;
139 struct dentry *dentry = file->f_path.dentry;
140 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
Daniel Campello35c9e242015-07-20 16:23:50 -0700141
142 lower_file = sdcardfs_lower_file(file);
143
144 /* XXX: use vfs_ioctl if/when VFS exports it */
145 if (!lower_file || !lower_file->f_op)
146 goto out;
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700147
148 /* save current_cred and override it */
149 OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(file_inode(file)));
150
Daniel Campello35c9e242015-07-20 16:23:50 -0700151 if (lower_file->f_op->compat_ioctl)
152 err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
153
Jaegeuk Kimdb024842017-07-06 19:12:22 -0700154 REVERT_CRED(saved_cred);
Daniel Campello35c9e242015-07-20 16:23:50 -0700155out:
156 return err;
157}
158#endif
159
160static int sdcardfs_mmap(struct file *file, struct vm_area_struct *vma)
161{
162 int err = 0;
163 bool willwrite;
164 struct file *lower_file;
165 const struct vm_operations_struct *saved_vm_ops = NULL;
166
167 /* this might be deferred to mmap's writepage */
168 willwrite = ((vma->vm_flags | VM_SHARED | VM_WRITE) == vma->vm_flags);
169
170 /*
171 * File systems which do not implement ->writepage may use
172 * generic_file_readonly_mmap as their ->mmap op. If you call
173 * generic_file_readonly_mmap with VM_WRITE, you'd get an -EINVAL.
174 * But we cannot call the lower ->mmap op, so we can't tell that
175 * writeable mappings won't work. Therefore, our only choice is to
176 * check if the lower file system supports the ->writepage, and if
177 * not, return EINVAL (the same error that
178 * generic_file_readonly_mmap returns in that case).
179 */
180 lower_file = sdcardfs_lower_file(file);
181 if (willwrite && !lower_file->f_mapping->a_ops->writepage) {
182 err = -EINVAL;
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700183 pr_err("sdcardfs: lower file system does not support writeable mmap\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700184 goto out;
185 }
186
187 /*
188 * find and save lower vm_ops.
189 *
190 * XXX: the VFS should have a cleaner way of finding the lower vm_ops
191 */
192 if (!SDCARDFS_F(file)->lower_vm_ops) {
193 err = lower_file->f_op->mmap(lower_file, vma);
194 if (err) {
Daniel Rosenberg77ecf212017-03-16 17:46:13 -0700195 pr_err("sdcardfs: lower mmap failed %d\n", err);
Daniel Campello35c9e242015-07-20 16:23:50 -0700196 goto out;
197 }
198 saved_vm_ops = vma->vm_ops; /* save: came from lower ->mmap */
Daniel Campello35c9e242015-07-20 16:23:50 -0700199 }
200
201 /*
202 * Next 3 lines are all I need from generic_file_mmap. I definitely
203 * don't want its test for ->readpage which returns -ENOEXEC.
204 */
205 file_accessed(file);
206 vma->vm_ops = &sdcardfs_vm_ops;
Daniel Campello35c9e242015-07-20 16:23:50 -0700207
208 file->f_mapping->a_ops = &sdcardfs_aops; /* set our aops */
209 if (!SDCARDFS_F(file)->lower_vm_ops) /* save for our ->fault */
210 SDCARDFS_F(file)->lower_vm_ops = saved_vm_ops;
Daniel Rosenberg2d82e2e2017-04-10 20:54:30 -0700211 vma->vm_private_data = file;
212 get_file(lower_file);
213 vma->vm_file = lower_file;
Daniel Campello35c9e242015-07-20 16:23:50 -0700214
215out:
216 return err;
217}
218
219static int sdcardfs_open(struct inode *inode, struct file *file)
220{
221 int err = 0;
222 struct file *lower_file = NULL;
223 struct path lower_path;
224 struct dentry *dentry = file->f_path.dentry;
225 struct dentry *parent = dget_parent(dentry);
226 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
227 const struct cred *saved_cred = NULL;
Daniel Campello35c9e242015-07-20 16:23:50 -0700228
229 /* don't open unhashed/deleted files */
230 if (d_unhashed(dentry)) {
231 err = -ENOENT;
232 goto out_err;
233 }
234
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800235 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
Daniel Campello35c9e242015-07-20 16:23:50 -0700236 err = -EACCES;
237 goto out_err;
238 }
239
240 /* save current_cred and override it */
Daniel Rosenbergad905252017-01-25 13:48:45 -0800241 OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(inode));
Daniel Campello35c9e242015-07-20 16:23:50 -0700242
243 file->private_data =
244 kzalloc(sizeof(struct sdcardfs_file_info), GFP_KERNEL);
245 if (!SDCARDFS_F(file)) {
246 err = -ENOMEM;
247 goto out_revert_cred;
248 }
249
250 /* open lower object and link sdcardfs's file struct to lower's */
251 sdcardfs_get_lower_path(file->f_path.dentry, &lower_path);
Daniel Campellod1d080c2015-07-20 16:27:37 -0700252 lower_file = dentry_open(&lower_path, file->f_flags, current_cred());
253 path_put(&lower_path);
Daniel Campello35c9e242015-07-20 16:23:50 -0700254 if (IS_ERR(lower_file)) {
255 err = PTR_ERR(lower_file);
256 lower_file = sdcardfs_lower_file(file);
257 if (lower_file) {
258 sdcardfs_set_lower_file(file, NULL);
259 fput(lower_file); /* fput calls dput for lower_dentry */
260 }
261 } else {
262 sdcardfs_set_lower_file(file, lower_file);
263 }
264
265 if (err)
266 kfree(SDCARDFS_F(file));
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700267 else
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800268 sdcardfs_copy_and_fix_attrs(inode, sdcardfs_lower_inode(inode));
Daniel Campello35c9e242015-07-20 16:23:50 -0700269
270out_revert_cred:
271 REVERT_CRED(saved_cred);
272out_err:
273 dput(parent);
274 return err;
275}
276
277static int sdcardfs_flush(struct file *file, fl_owner_t id)
278{
279 int err = 0;
280 struct file *lower_file = NULL;
281
282 lower_file = sdcardfs_lower_file(file);
Daniel Campellod1d080c2015-07-20 16:27:37 -0700283 if (lower_file && lower_file->f_op && lower_file->f_op->flush) {
284 filemap_write_and_wait(file->f_mapping);
Daniel Campello35c9e242015-07-20 16:23:50 -0700285 err = lower_file->f_op->flush(lower_file, id);
Daniel Campellod1d080c2015-07-20 16:27:37 -0700286 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700287
288 return err;
289}
290
291/* release all lower object references & free the file info structure */
292static int sdcardfs_file_release(struct inode *inode, struct file *file)
293{
294 struct file *lower_file;
295
296 lower_file = sdcardfs_lower_file(file);
297 if (lower_file) {
298 sdcardfs_set_lower_file(file, NULL);
299 fput(lower_file);
300 }
301
302 kfree(SDCARDFS_F(file));
303 return 0;
304}
305
Daniel Campellod1d080c2015-07-20 16:27:37 -0700306static int sdcardfs_fsync(struct file *file, loff_t start, loff_t end,
307 int datasync)
Daniel Campello35c9e242015-07-20 16:23:50 -0700308{
309 int err;
310 struct file *lower_file;
311 struct path lower_path;
312 struct dentry *dentry = file->f_path.dentry;
313
Daniel Campellod1d080c2015-07-20 16:27:37 -0700314 err = __generic_file_fsync(file, start, end, datasync);
315 if (err)
316 goto out;
317
Daniel Campello35c9e242015-07-20 16:23:50 -0700318 lower_file = sdcardfs_lower_file(file);
319 sdcardfs_get_lower_path(dentry, &lower_path);
Daniel Campellod1d080c2015-07-20 16:27:37 -0700320 err = vfs_fsync_range(lower_file, start, end, datasync);
Daniel Campello35c9e242015-07-20 16:23:50 -0700321 sdcardfs_put_lower_path(dentry, &lower_path);
Daniel Campellod1d080c2015-07-20 16:27:37 -0700322out:
Daniel Campello35c9e242015-07-20 16:23:50 -0700323 return err;
324}
325
326static int sdcardfs_fasync(int fd, struct file *file, int flag)
327{
328 int err = 0;
329 struct file *lower_file = NULL;
330
331 lower_file = sdcardfs_lower_file(file);
332 if (lower_file->f_op && lower_file->f_op->fasync)
333 err = lower_file->f_op->fasync(fd, lower_file, flag);
334
335 return err;
336}
337
Daniel Rosenberg034f8642017-03-09 21:42:01 -0800338/*
339 * Sdcardfs cannot use generic_file_llseek as ->llseek, because it would
340 * only set the offset of the upper file. So we have to implement our
341 * own method to set both the upper and lower file offsets
342 * consistently.
343 */
344static loff_t sdcardfs_file_llseek(struct file *file, loff_t offset, int whence)
345{
346 int err;
347 struct file *lower_file;
348
349 err = generic_file_llseek(file, offset, whence);
350 if (err < 0)
351 goto out;
352
353 lower_file = sdcardfs_lower_file(file);
354 err = generic_file_llseek(lower_file, offset, whence);
355
356out:
357 return err;
358}
359
Daniel Rosenberga3349122017-03-09 21:48:09 -0800360/*
361 * Sdcardfs read_iter, redirect modified iocb to lower read_iter
362 */
363ssize_t sdcardfs_read_iter(struct kiocb *iocb, struct iov_iter *iter)
364{
365 int err;
366 struct file *file = iocb->ki_filp, *lower_file;
367
368 lower_file = sdcardfs_lower_file(file);
369 if (!lower_file->f_op->read_iter) {
370 err = -EINVAL;
371 goto out;
372 }
373
374 get_file(lower_file); /* prevent lower_file from being released */
375 iocb->ki_filp = lower_file;
376 err = lower_file->f_op->read_iter(iocb, iter);
Daniel Rosenberga3349122017-03-09 21:48:09 -0800377 iocb->ki_filp = file;
378 fput(lower_file);
Daniel Rosenberg4131f8c2017-04-24 16:10:21 -0700379 /* update upper inode atime as needed */
380 if (err >= 0 || err == -EIOCBQUEUED)
381 fsstack_copy_attr_atime(file->f_path.dentry->d_inode,
382 file_inode(lower_file));
Daniel Rosenberga3349122017-03-09 21:48:09 -0800383out:
384 return err;
385}
386
387/*
388 * Sdcardfs write_iter, redirect modified iocb to lower write_iter
389 */
390ssize_t sdcardfs_write_iter(struct kiocb *iocb, struct iov_iter *iter)
391{
392 int err;
393 struct file *file = iocb->ki_filp, *lower_file;
394
395 lower_file = sdcardfs_lower_file(file);
396 if (!lower_file->f_op->write_iter) {
397 err = -EINVAL;
398 goto out;
399 }
400
401 get_file(lower_file); /* prevent lower_file from being released */
402 iocb->ki_filp = lower_file;
403 err = lower_file->f_op->write_iter(iocb, iter);
404 iocb->ki_filp = file;
405 fput(lower_file);
Daniel Rosenberg4131f8c2017-04-24 16:10:21 -0700406 /* update upper inode times/sizes as needed */
407 if (err >= 0 || err == -EIOCBQUEUED) {
408 fsstack_copy_inode_size(file->f_path.dentry->d_inode,
409 file_inode(lower_file));
410 fsstack_copy_attr_times(file->f_path.dentry->d_inode,
411 file_inode(lower_file));
412 }
Daniel Rosenberga3349122017-03-09 21:48:09 -0800413out:
414 return err;
415}
Daniel Rosenberg034f8642017-03-09 21:42:01 -0800416
Daniel Campello35c9e242015-07-20 16:23:50 -0700417const struct file_operations sdcardfs_main_fops = {
418 .llseek = generic_file_llseek,
419 .read = sdcardfs_read,
420 .write = sdcardfs_write,
421 .unlocked_ioctl = sdcardfs_unlocked_ioctl,
422#ifdef CONFIG_COMPAT
423 .compat_ioctl = sdcardfs_compat_ioctl,
424#endif
425 .mmap = sdcardfs_mmap,
426 .open = sdcardfs_open,
427 .flush = sdcardfs_flush,
428 .release = sdcardfs_file_release,
429 .fsync = sdcardfs_fsync,
430 .fasync = sdcardfs_fasync,
Daniel Rosenberga3349122017-03-09 21:48:09 -0800431 .read_iter = sdcardfs_read_iter,
432 .write_iter = sdcardfs_write_iter,
Daniel Campello35c9e242015-07-20 16:23:50 -0700433};
434
435/* trimmed directory options */
436const struct file_operations sdcardfs_dir_fops = {
Daniel Rosenberg034f8642017-03-09 21:42:01 -0800437 .llseek = sdcardfs_file_llseek,
Daniel Campello35c9e242015-07-20 16:23:50 -0700438 .read = generic_read_dir,
Daniel Campellod1d080c2015-07-20 16:27:37 -0700439 .iterate = sdcardfs_readdir,
Daniel Campello35c9e242015-07-20 16:23:50 -0700440 .unlocked_ioctl = sdcardfs_unlocked_ioctl,
441#ifdef CONFIG_COMPAT
442 .compat_ioctl = sdcardfs_compat_ioctl,
443#endif
444 .open = sdcardfs_open,
445 .release = sdcardfs_file_release,
446 .flush = sdcardfs_flush,
447 .fsync = sdcardfs_fsync,
448 .fasync = sdcardfs_fasync,
449};