Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | static 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 53 | fsstack_copy_attr_atime(d_inode(dentry), |
| 54 | file_inode(lower_file)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 55 | |
| 56 | return err; |
| 57 | } |
| 58 | |
| 59 | static ssize_t sdcardfs_write(struct file *file, const char __user *buf, |
| 60 | size_t count, loff_t *ppos) |
| 61 | { |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 62 | int err; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 63 | 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 Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 68 | pr_err("No minimum free space.\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 69 | 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 76 | 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return err; |
| 83 | } |
| 84 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 85 | static int sdcardfs_readdir(struct file *file, struct dir_context *ctx) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 86 | { |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 87 | int err; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 88 | 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 94 | err = iterate_dir(lower_file, ctx); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 95 | file->f_pos = lower_file->f_pos; |
| 96 | if (err >= 0) /* copy the atime */ |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 97 | fsstack_copy_attr_atime(d_inode(dentry), |
| 98 | file_inode(lower_file)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 99 | return err; |
| 100 | } |
| 101 | |
| 102 | static 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 Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 107 | 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 110 | |
| 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 Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 116 | |
| 117 | /* save current_cred and override it */ |
| 118 | OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(file_inode(file))); |
| 119 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 120 | if (lower_file->f_op->unlocked_ioctl) |
| 121 | err = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg); |
| 122 | |
Daniel Rosenberg | be35f03 | 2017-03-09 21:24:58 -0800 | [diff] [blame] | 123 | /* 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 Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 127 | REVERT_CRED(saved_cred); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 128 | out: |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | #ifdef CONFIG_COMPAT |
| 133 | static 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 Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 138 | 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 141 | |
| 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 Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 147 | |
| 148 | /* save current_cred and override it */ |
| 149 | OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(file_inode(file))); |
| 150 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 151 | if (lower_file->f_op->compat_ioctl) |
| 152 | err = lower_file->f_op->compat_ioctl(lower_file, cmd, arg); |
| 153 | |
Jaegeuk Kim | db02484 | 2017-07-06 19:12:22 -0700 | [diff] [blame] | 154 | REVERT_CRED(saved_cred); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 155 | out: |
| 156 | return err; |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | static 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 Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 183 | pr_err("sdcardfs: lower file system does not support writeable mmap\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 184 | 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 Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 195 | pr_err("sdcardfs: lower mmap failed %d\n", err); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 196 | goto out; |
| 197 | } |
| 198 | saved_vm_ops = vma->vm_ops; /* save: came from lower ->mmap */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 199 | } |
| 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 207 | |
| 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 Rosenberg | 2d82e2e | 2017-04-10 20:54:30 -0700 | [diff] [blame] | 211 | vma->vm_private_data = file; |
| 212 | get_file(lower_file); |
| 213 | vma->vm_file = lower_file; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 214 | |
| 215 | out: |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | static 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 228 | |
| 229 | /* don't open unhashed/deleted files */ |
| 230 | if (d_unhashed(dentry)) { |
| 231 | err = -ENOENT; |
| 232 | goto out_err; |
| 233 | } |
| 234 | |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 235 | if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 236 | err = -EACCES; |
| 237 | goto out_err; |
| 238 | } |
| 239 | |
| 240 | /* save current_cred and override it */ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 241 | OVERRIDE_CRED(sbi, saved_cred, SDCARDFS_I(inode)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 242 | |
| 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 252 | lower_file = dentry_open(&lower_path, file->f_flags, current_cred()); |
| 253 | path_put(&lower_path); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 254 | 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 Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 267 | else |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 268 | sdcardfs_copy_and_fix_attrs(inode, sdcardfs_lower_inode(inode)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 269 | |
| 270 | out_revert_cred: |
| 271 | REVERT_CRED(saved_cred); |
| 272 | out_err: |
| 273 | dput(parent); |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | static 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 283 | if (lower_file && lower_file->f_op && lower_file->f_op->flush) { |
| 284 | filemap_write_and_wait(file->f_mapping); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 285 | err = lower_file->f_op->flush(lower_file, id); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 286 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 287 | |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | /* release all lower object references & free the file info structure */ |
| 292 | static 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 Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 306 | static int sdcardfs_fsync(struct file *file, loff_t start, loff_t end, |
| 307 | int datasync) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 308 | { |
| 309 | int err; |
| 310 | struct file *lower_file; |
| 311 | struct path lower_path; |
| 312 | struct dentry *dentry = file->f_path.dentry; |
| 313 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 314 | err = __generic_file_fsync(file, start, end, datasync); |
| 315 | if (err) |
| 316 | goto out; |
| 317 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 318 | lower_file = sdcardfs_lower_file(file); |
| 319 | sdcardfs_get_lower_path(dentry, &lower_path); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 320 | err = vfs_fsync_range(lower_file, start, end, datasync); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 321 | sdcardfs_put_lower_path(dentry, &lower_path); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 322 | out: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 323 | return err; |
| 324 | } |
| 325 | |
| 326 | static 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 Rosenberg | 034f864 | 2017-03-09 21:42:01 -0800 | [diff] [blame] | 338 | /* |
| 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 | */ |
| 344 | static 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 | |
| 356 | out: |
| 357 | return err; |
| 358 | } |
| 359 | |
Daniel Rosenberg | a334912 | 2017-03-09 21:48:09 -0800 | [diff] [blame] | 360 | /* |
| 361 | * Sdcardfs read_iter, redirect modified iocb to lower read_iter |
| 362 | */ |
| 363 | ssize_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 Rosenberg | a334912 | 2017-03-09 21:48:09 -0800 | [diff] [blame] | 377 | iocb->ki_filp = file; |
| 378 | fput(lower_file); |
Daniel Rosenberg | 4131f8c | 2017-04-24 16:10:21 -0700 | [diff] [blame] | 379 | /* 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 Rosenberg | a334912 | 2017-03-09 21:48:09 -0800 | [diff] [blame] | 383 | out: |
| 384 | return err; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Sdcardfs write_iter, redirect modified iocb to lower write_iter |
| 389 | */ |
| 390 | ssize_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 Rosenberg | 4131f8c | 2017-04-24 16:10:21 -0700 | [diff] [blame] | 406 | /* 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 Rosenberg | a334912 | 2017-03-09 21:48:09 -0800 | [diff] [blame] | 413 | out: |
| 414 | return err; |
| 415 | } |
Daniel Rosenberg | 034f864 | 2017-03-09 21:42:01 -0800 | [diff] [blame] | 416 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 417 | const 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 Rosenberg | a334912 | 2017-03-09 21:48:09 -0800 | [diff] [blame] | 431 | .read_iter = sdcardfs_read_iter, |
| 432 | .write_iter = sdcardfs_write_iter, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | /* trimmed directory options */ |
| 436 | const struct file_operations sdcardfs_dir_fops = { |
Daniel Rosenberg | 034f864 | 2017-03-09 21:42:01 -0800 | [diff] [blame] | 437 | .llseek = sdcardfs_file_llseek, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 438 | .read = generic_read_dir, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 439 | .iterate = sdcardfs_readdir, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 440 | .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 | }; |