Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 2 | * fs/f2fs/file.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/stat.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/writeback.h> |
Jaegeuk Kim | ae51fb3 | 2013-03-16 11:13:04 +0900 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 17 | #include <linux/falloc.h> |
| 18 | #include <linux/types.h> |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 19 | #include <linux/compat.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/mount.h> |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 22 | #include <linux/pagevec.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 23 | |
| 24 | #include "f2fs.h" |
| 25 | #include "node.h" |
| 26 | #include "segment.h" |
| 27 | #include "xattr.h" |
| 28 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 29 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 30 | |
| 31 | static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, |
| 32 | struct vm_fault *vmf) |
| 33 | { |
| 34 | struct page *page = vmf->page; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 35 | struct inode *inode = file_inode(vma->vm_file); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 36 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 37 | struct dnode_of_data dn; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 38 | int err; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 39 | |
| 40 | f2fs_balance_fs(sbi); |
| 41 | |
| 42 | sb_start_pagefault(inode->i_sb); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 43 | |
| 44 | f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 45 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 46 | /* block allocation */ |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 47 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 48 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 49 | err = f2fs_reserve_block(&dn, page->index); |
| 50 | if (err) { |
| 51 | f2fs_unlock_op(sbi); |
| 52 | goto out; |
| 53 | } |
| 54 | f2fs_put_dnode(&dn); |
| 55 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 56 | |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 57 | file_update_time(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 58 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 59 | if (unlikely(page->mapping != inode->i_mapping || |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 60 | page_offset(page) > i_size_read(inode) || |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 61 | !PageUptodate(page))) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 62 | unlock_page(page); |
| 63 | err = -EFAULT; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * check to see if the page is mapped already (no holes) |
| 69 | */ |
| 70 | if (PageMappedToDisk(page)) |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 71 | goto mapped; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 72 | |
| 73 | /* page is wholly or partially inside EOF */ |
| 74 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { |
| 75 | unsigned offset; |
| 76 | offset = i_size_read(inode) & ~PAGE_CACHE_MASK; |
| 77 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 78 | } |
| 79 | set_page_dirty(page); |
| 80 | SetPageUptodate(page); |
| 81 | |
Jaegeuk Kim | e943a10 | 2013-10-25 14:26:31 +0900 | [diff] [blame] | 82 | trace_f2fs_vm_page_mkwrite(page, DATA); |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 83 | mapped: |
| 84 | /* fill the page */ |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 85 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 86 | out: |
| 87 | sb_end_pagefault(inode->i_sb); |
| 88 | return block_page_mkwrite_return(err); |
| 89 | } |
| 90 | |
| 91 | static const struct vm_operations_struct f2fs_file_vm_ops = { |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 92 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 93 | .map_pages = filemap_map_pages, |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 94 | .page_mkwrite = f2fs_vm_page_mkwrite, |
| 95 | .remap_pages = generic_file_remap_pages, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 96 | }; |
| 97 | |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 98 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
| 99 | { |
| 100 | struct dentry *dentry; |
| 101 | |
| 102 | inode = igrab(inode); |
| 103 | dentry = d_find_any_alias(inode); |
| 104 | iput(inode); |
| 105 | if (!dentry) |
| 106 | return 0; |
| 107 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 108 | if (update_dent_inode(inode, &dentry->d_name)) { |
| 109 | dput(dentry); |
| 110 | return 0; |
| 111 | } |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 112 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 113 | *pino = parent_ino(dentry); |
| 114 | dput(dentry); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 118 | static inline bool need_do_checkpoint(struct inode *inode) |
| 119 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 120 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 121 | bool need_cp = false; |
| 122 | |
| 123 | if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) |
| 124 | need_cp = true; |
| 125 | else if (file_wrong_pino(inode)) |
| 126 | need_cp = true; |
| 127 | else if (!space_for_roll_forward(sbi)) |
| 128 | need_cp = true; |
| 129 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
| 130 | need_cp = true; |
| 131 | else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) |
| 132 | need_cp = true; |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame^] | 133 | else if (test_opt(sbi, FASTBOOT)) |
| 134 | need_cp = true; |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 135 | |
| 136 | return need_cp; |
| 137 | } |
| 138 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 139 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
| 140 | { |
| 141 | struct inode *inode = file->f_mapping->host; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 142 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 143 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 144 | nid_t ino = inode->i_ino; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 145 | int ret = 0; |
| 146 | bool need_cp = false; |
| 147 | struct writeback_control wbc = { |
Jaegeuk Kim | c81bf1c | 2014-03-03 11:28:40 +0900 | [diff] [blame] | 148 | .sync_mode = WB_SYNC_ALL, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 149 | .nr_to_write = LONG_MAX, |
| 150 | .for_reclaim = 0, |
| 151 | }; |
| 152 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 153 | if (unlikely(f2fs_readonly(inode->i_sb))) |
Namjae Jeon | 1fa95b0 | 2012-12-01 10:56:01 +0900 | [diff] [blame] | 154 | return 0; |
| 155 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 156 | trace_f2fs_sync_file_enter(inode); |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 157 | |
| 158 | /* if fdatasync is triggered, let's do in-place-update */ |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 159 | if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 160 | set_inode_flag(fi, FI_NEED_IPU); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 161 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 162 | clear_inode_flag(fi, FI_NEED_IPU); |
| 163 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 164 | if (ret) { |
| 165 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 166 | return ret; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 167 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 168 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 169 | /* |
| 170 | * if there is no written data, don't waste time to write recovery info. |
| 171 | */ |
| 172 | if (!is_inode_flag_set(fi, FI_APPEND_WRITE) && |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 173 | !exist_written_data(sbi, ino, APPEND_INO)) { |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 174 | struct page *i = find_get_page(NODE_MAPPING(sbi), ino); |
| 175 | |
| 176 | /* But we need to avoid that there are some inode updates */ |
| 177 | if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) { |
| 178 | f2fs_put_page(i, 0); |
| 179 | goto go_write; |
| 180 | } |
| 181 | f2fs_put_page(i, 0); |
| 182 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 183 | if (is_inode_flag_set(fi, FI_UPDATE_WRITE) || |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 184 | exist_written_data(sbi, ino, UPDATE_INO)) |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 185 | goto flush_out; |
| 186 | goto out; |
| 187 | } |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 188 | go_write: |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 189 | /* guarantee free sections for fsync */ |
| 190 | f2fs_balance_fs(sbi); |
| 191 | |
Jaegeuk Kim | e5d2385 | 2013-07-03 10:55:52 +0900 | [diff] [blame] | 192 | /* |
| 193 | * Both of fdatasync() and fsync() are able to be recovered from |
| 194 | * sudden-power-off. |
| 195 | */ |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 196 | down_read(&fi->i_sem); |
| 197 | need_cp = need_do_checkpoint(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 198 | up_read(&fi->i_sem); |
| 199 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 200 | if (need_cp) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 201 | nid_t pino; |
| 202 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 203 | /* all the dirty node pages should be flushed for POR */ |
| 204 | ret = f2fs_sync_fs(inode->i_sb, 1); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 205 | |
| 206 | down_write(&fi->i_sem); |
| 207 | F2FS_I(inode)->xattr_ver = 0; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 208 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
| 209 | get_parent_ino(inode, &pino)) { |
| 210 | F2FS_I(inode)->i_pino = pino; |
| 211 | file_got_pino(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 212 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 213 | mark_inode_dirty_sync(inode); |
| 214 | ret = f2fs_write_inode(inode, NULL); |
| 215 | if (ret) |
| 216 | goto out; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 217 | } else { |
| 218 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 219 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 220 | } else { |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 221 | sync_nodes: |
| 222 | sync_node_pages(sbi, ino, &wbc); |
| 223 | |
| 224 | if (need_inode_block_update(sbi, ino)) { |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 225 | mark_inode_dirty_sync(inode); |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 226 | ret = f2fs_write_inode(inode, NULL); |
| 227 | if (ret) |
| 228 | goto out; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 229 | goto sync_nodes; |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 230 | } |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 231 | |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 232 | ret = wait_on_node_pages_writeback(sbi, ino); |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 233 | if (ret) |
| 234 | goto out; |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 235 | |
| 236 | /* once recovery info is written, don't need to tack this */ |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 237 | remove_dirty_inode(sbi, ino, APPEND_INO); |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 238 | clear_inode_flag(fi, FI_APPEND_WRITE); |
| 239 | flush_out: |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 240 | remove_dirty_inode(sbi, ino, UPDATE_INO); |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 241 | clear_inode_flag(fi, FI_UPDATE_WRITE); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 242 | ret = f2fs_issue_flush(F2FS_I_SB(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 243 | } |
| 244 | out: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 245 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 246 | return ret; |
| 247 | } |
| 248 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 249 | static pgoff_t __get_first_dirty_index(struct address_space *mapping, |
| 250 | pgoff_t pgofs, int whence) |
| 251 | { |
| 252 | struct pagevec pvec; |
| 253 | int nr_pages; |
| 254 | |
| 255 | if (whence != SEEK_DATA) |
| 256 | return 0; |
| 257 | |
| 258 | /* find first dirty page index */ |
| 259 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 65b85cc | 2014-07-30 17:25:54 -0700 | [diff] [blame] | 260 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, |
| 261 | PAGECACHE_TAG_DIRTY, 1); |
| 262 | pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 263 | pagevec_release(&pvec); |
| 264 | return pgofs; |
| 265 | } |
| 266 | |
| 267 | static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, |
| 268 | int whence) |
| 269 | { |
| 270 | switch (whence) { |
| 271 | case SEEK_DATA: |
| 272 | if ((blkaddr == NEW_ADDR && dirty == pgofs) || |
| 273 | (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) |
| 274 | return true; |
| 275 | break; |
| 276 | case SEEK_HOLE: |
| 277 | if (blkaddr == NULL_ADDR) |
| 278 | return true; |
| 279 | break; |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 284 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
| 285 | { |
| 286 | struct inode *inode = file->f_mapping->host; |
| 287 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 288 | struct dnode_of_data dn; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 289 | pgoff_t pgofs, end_offset, dirty; |
| 290 | loff_t data_ofs = offset; |
| 291 | loff_t isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 292 | int err = 0; |
| 293 | |
| 294 | mutex_lock(&inode->i_mutex); |
| 295 | |
| 296 | isize = i_size_read(inode); |
| 297 | if (offset >= isize) |
| 298 | goto fail; |
| 299 | |
| 300 | /* handle inline data case */ |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 301 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 302 | if (whence == SEEK_HOLE) |
| 303 | data_ofs = isize; |
| 304 | goto found; |
| 305 | } |
| 306 | |
| 307 | pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT); |
| 308 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 309 | dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); |
| 310 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 311 | for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 312 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 313 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); |
| 314 | if (err && err != -ENOENT) { |
| 315 | goto fail; |
| 316 | } else if (err == -ENOENT) { |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 317 | /* direct node does not exists */ |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 318 | if (whence == SEEK_DATA) { |
| 319 | pgofs = PGOFS_OF_NEXT_DNODE(pgofs, |
| 320 | F2FS_I(inode)); |
| 321 | continue; |
| 322 | } else { |
| 323 | goto found; |
| 324 | } |
| 325 | } |
| 326 | |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 327 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 328 | |
| 329 | /* find data/hole in dnode block */ |
| 330 | for (; dn.ofs_in_node < end_offset; |
| 331 | dn.ofs_in_node++, pgofs++, |
| 332 | data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 333 | block_t blkaddr; |
| 334 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 335 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 336 | if (__found_offset(blkaddr, dirty, pgofs, whence)) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 337 | f2fs_put_dnode(&dn); |
| 338 | goto found; |
| 339 | } |
| 340 | } |
| 341 | f2fs_put_dnode(&dn); |
| 342 | } |
| 343 | |
| 344 | if (whence == SEEK_DATA) |
| 345 | goto fail; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 346 | found: |
Jaegeuk Kim | fe369bc | 2014-04-28 17:02:48 +0900 | [diff] [blame] | 347 | if (whence == SEEK_HOLE && data_ofs > isize) |
| 348 | data_ofs = isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 349 | mutex_unlock(&inode->i_mutex); |
| 350 | return vfs_setpos(file, data_ofs, maxbytes); |
| 351 | fail: |
| 352 | mutex_unlock(&inode->i_mutex); |
| 353 | return -ENXIO; |
| 354 | } |
| 355 | |
| 356 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) |
| 357 | { |
| 358 | struct inode *inode = file->f_mapping->host; |
| 359 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 360 | |
| 361 | switch (whence) { |
| 362 | case SEEK_SET: |
| 363 | case SEEK_CUR: |
| 364 | case SEEK_END: |
| 365 | return generic_file_llseek_size(file, offset, whence, |
| 366 | maxbytes, i_size_read(inode)); |
| 367 | case SEEK_DATA: |
| 368 | case SEEK_HOLE: |
Jaegeuk Kim | 0b4c5afde | 2014-09-08 10:59:43 -0700 | [diff] [blame] | 369 | if (offset < 0) |
| 370 | return -ENXIO; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 371 | return f2fs_seek_block(file, offset, whence); |
| 372 | } |
| 373 | |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 377 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 378 | { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 379 | struct inode *inode = file_inode(file); |
| 380 | |
| 381 | /* we don't need to use inline_data strictly */ |
| 382 | if (f2fs_has_inline_data(inode)) { |
| 383 | int err = f2fs_convert_inline_inode(inode); |
| 384 | if (err) |
| 385 | return err; |
| 386 | } |
| 387 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 388 | file_accessed(file); |
| 389 | vma->vm_ops = &f2fs_file_vm_ops; |
| 390 | return 0; |
| 391 | } |
| 392 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 393 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 394 | { |
| 395 | int nr_free = 0, ofs = dn->ofs_in_node; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 396 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 397 | struct f2fs_node *raw_node; |
| 398 | __le32 *addr; |
| 399 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 400 | raw_node = F2FS_NODE(dn->node_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 401 | addr = blkaddr_in_node(raw_node) + ofs; |
| 402 | |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 403 | for (; count > 0; count--, addr++, dn->ofs_in_node++) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 404 | block_t blkaddr = le32_to_cpu(*addr); |
| 405 | if (blkaddr == NULL_ADDR) |
| 406 | continue; |
| 407 | |
| 408 | update_extent_cache(NULL_ADDR, dn); |
| 409 | invalidate_blocks(sbi, blkaddr); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 410 | nr_free++; |
| 411 | } |
| 412 | if (nr_free) { |
Namjae Jeon | d7cc950 | 2013-06-08 21:25:40 +0900 | [diff] [blame] | 413 | dec_valid_block_count(sbi, dn->inode, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 414 | set_page_dirty(dn->node_page); |
| 415 | sync_inode_page(dn); |
| 416 | } |
| 417 | dn->ofs_in_node = ofs; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 418 | |
| 419 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
| 420 | dn->ofs_in_node, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 421 | return nr_free; |
| 422 | } |
| 423 | |
| 424 | void truncate_data_blocks(struct dnode_of_data *dn) |
| 425 | { |
| 426 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); |
| 427 | } |
| 428 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 429 | static int truncate_partial_data_page(struct inode *inode, u64 from) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 430 | { |
| 431 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 432 | struct page *page; |
| 433 | |
| 434 | if (!offset) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 435 | return 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 436 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 437 | page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 438 | if (IS_ERR(page)) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 439 | return 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 440 | |
| 441 | lock_page(page); |
Chao Yu | ca0a81b | 2014-06-12 13:31:50 +0800 | [diff] [blame] | 442 | if (unlikely(!PageUptodate(page) || |
| 443 | page->mapping != inode->i_mapping)) |
| 444 | goto out; |
| 445 | |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 446 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 447 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
| 448 | set_page_dirty(page); |
Chao Yu | ca0a81b | 2014-06-12 13:31:50 +0800 | [diff] [blame] | 449 | out: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 450 | f2fs_put_page(page, 1); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 451 | return 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 452 | } |
| 453 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 454 | int truncate_blocks(struct inode *inode, u64 from, bool lock) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 455 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 456 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 457 | unsigned int blocksize = inode->i_sb->s_blocksize; |
| 458 | struct dnode_of_data dn; |
| 459 | pgoff_t free_from; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 460 | int count = 0, err = 0; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 461 | struct page *ipage; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 462 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 463 | trace_f2fs_truncate_blocks_enter(inode, from); |
| 464 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 465 | free_from = (pgoff_t) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 466 | ((from + blocksize - 1) >> (sbi->log_blocksize)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 467 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 468 | if (lock) |
| 469 | f2fs_lock_op(sbi); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 470 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 471 | ipage = get_node_page(sbi, inode->i_ino); |
| 472 | if (IS_ERR(ipage)) { |
| 473 | err = PTR_ERR(ipage); |
| 474 | goto out; |
| 475 | } |
| 476 | |
| 477 | if (f2fs_has_inline_data(inode)) { |
| 478 | truncate_inline_data(ipage, from); |
| 479 | update_inode(inode, ipage); |
| 480 | f2fs_put_page(ipage, 1); |
| 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 485 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 486 | if (err) { |
| 487 | if (err == -ENOENT) |
| 488 | goto free_next; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 489 | goto out; |
Jaegeuk Kim | 1ce86bf | 2014-10-15 10:24:34 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 492 | count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 493 | |
| 494 | count -= dn.ofs_in_node; |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 495 | f2fs_bug_on(sbi, count < 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 496 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 497 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
| 498 | truncate_data_blocks_range(&dn, count); |
| 499 | free_from += count; |
| 500 | } |
| 501 | |
| 502 | f2fs_put_dnode(&dn); |
| 503 | free_next: |
| 504 | err = truncate_inode_blocks(inode, free_from); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 505 | |
| 506 | /* lastly zero out the first data page */ |
| 507 | if (!err) |
| 508 | err = truncate_partial_data_page(inode, from); |
| 509 | out: |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 510 | if (lock) |
| 511 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 512 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 513 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 514 | return err; |
| 515 | } |
| 516 | |
| 517 | void f2fs_truncate(struct inode *inode) |
| 518 | { |
| 519 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 520 | S_ISLNK(inode->i_mode))) |
| 521 | return; |
| 522 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 523 | trace_f2fs_truncate(inode); |
| 524 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 525 | if (!truncate_blocks(inode, i_size_read(inode), true)) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 526 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 527 | mark_inode_dirty(inode); |
| 528 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 529 | } |
| 530 | |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 531 | int f2fs_getattr(struct vfsmount *mnt, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 532 | struct dentry *dentry, struct kstat *stat) |
| 533 | { |
| 534 | struct inode *inode = dentry->d_inode; |
| 535 | generic_fillattr(inode, stat); |
| 536 | stat->blocks <<= 3; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 541 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) |
| 542 | { |
| 543 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 544 | unsigned int ia_valid = attr->ia_valid; |
| 545 | |
| 546 | if (ia_valid & ATTR_UID) |
| 547 | inode->i_uid = attr->ia_uid; |
| 548 | if (ia_valid & ATTR_GID) |
| 549 | inode->i_gid = attr->ia_gid; |
| 550 | if (ia_valid & ATTR_ATIME) |
| 551 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 552 | inode->i_sb->s_time_gran); |
| 553 | if (ia_valid & ATTR_MTIME) |
| 554 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 555 | inode->i_sb->s_time_gran); |
| 556 | if (ia_valid & ATTR_CTIME) |
| 557 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 558 | inode->i_sb->s_time_gran); |
| 559 | if (ia_valid & ATTR_MODE) { |
| 560 | umode_t mode = attr->ia_mode; |
| 561 | |
| 562 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 563 | mode &= ~S_ISGID; |
| 564 | set_acl_inode(fi, mode); |
| 565 | } |
| 566 | } |
| 567 | #else |
| 568 | #define __setattr_copy setattr_copy |
| 569 | #endif |
| 570 | |
| 571 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) |
| 572 | { |
| 573 | struct inode *inode = dentry->d_inode; |
| 574 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 575 | int err; |
| 576 | |
| 577 | err = inode_change_ok(inode, attr); |
| 578 | if (err) |
| 579 | return err; |
| 580 | |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame] | 581 | if (attr->ia_valid & ATTR_SIZE) { |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame] | 582 | if (attr->ia_size != i_size_read(inode)) { |
| 583 | truncate_setsize(inode, attr->ia_size); |
| 584 | f2fs_truncate(inode); |
| 585 | f2fs_balance_fs(F2FS_I_SB(inode)); |
| 586 | } else { |
| 587 | /* |
| 588 | * giving a chance to truncate blocks past EOF which |
| 589 | * are fallocated with FALLOC_FL_KEEP_SIZE. |
| 590 | */ |
| 591 | f2fs_truncate(inode); |
| 592 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | __setattr_copy(inode, attr); |
| 596 | |
| 597 | if (attr->ia_valid & ATTR_MODE) { |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 598 | err = posix_acl_chmod(inode, get_inode_mode(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 599 | if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 600 | inode->i_mode = fi->i_acl_mode; |
| 601 | clear_inode_flag(fi, FI_ACL_MODE); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | mark_inode_dirty(inode); |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | const struct inode_operations f2fs_file_inode_operations = { |
| 610 | .getattr = f2fs_getattr, |
| 611 | .setattr = f2fs_setattr, |
| 612 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 613 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 614 | #ifdef CONFIG_F2FS_FS_XATTR |
| 615 | .setxattr = generic_setxattr, |
| 616 | .getxattr = generic_getxattr, |
| 617 | .listxattr = f2fs_listxattr, |
| 618 | .removexattr = generic_removexattr, |
| 619 | #endif |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 620 | .fiemap = f2fs_fiemap, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | static void fill_zero(struct inode *inode, pgoff_t index, |
| 624 | loff_t start, loff_t len) |
| 625 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 626 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 627 | struct page *page; |
| 628 | |
| 629 | if (!len) |
| 630 | return; |
| 631 | |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 632 | f2fs_balance_fs(sbi); |
| 633 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 634 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 635 | page = get_new_data_page(inode, NULL, index, false); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 636 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 637 | |
| 638 | if (!IS_ERR(page)) { |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 639 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 640 | zero_user(page, start, len); |
| 641 | set_page_dirty(page); |
| 642 | f2fs_put_page(page, 1); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
| 647 | { |
| 648 | pgoff_t index; |
| 649 | int err; |
| 650 | |
| 651 | for (index = pg_start; index < pg_end; index++) { |
| 652 | struct dnode_of_data dn; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 653 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 654 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 655 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 656 | if (err) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 657 | if (err == -ENOENT) |
| 658 | continue; |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | if (dn.data_blkaddr != NULL_ADDR) |
| 663 | truncate_data_blocks_range(&dn, 1); |
| 664 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 665 | } |
| 666 | return 0; |
| 667 | } |
| 668 | |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 669 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 670 | { |
| 671 | pgoff_t pg_start, pg_end; |
| 672 | loff_t off_start, off_end; |
| 673 | int ret = 0; |
| 674 | |
Chao Yu | 14cecc5 | 2014-09-15 18:03:32 +0800 | [diff] [blame] | 675 | if (!S_ISREG(inode->i_mode)) |
| 676 | return -EOPNOTSUPP; |
| 677 | |
| 678 | /* skip punching hole beyond i_size */ |
| 679 | if (offset >= inode->i_size) |
| 680 | return ret; |
| 681 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 682 | if (f2fs_has_inline_data(inode)) { |
| 683 | ret = f2fs_convert_inline_inode(inode); |
| 684 | if (ret) |
| 685 | return ret; |
| 686 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 687 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 688 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 689 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 690 | |
| 691 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 692 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 693 | |
| 694 | if (pg_start == pg_end) { |
| 695 | fill_zero(inode, pg_start, off_start, |
| 696 | off_end - off_start); |
| 697 | } else { |
| 698 | if (off_start) |
| 699 | fill_zero(inode, pg_start++, off_start, |
| 700 | PAGE_CACHE_SIZE - off_start); |
| 701 | if (off_end) |
| 702 | fill_zero(inode, pg_end, 0, off_end); |
| 703 | |
| 704 | if (pg_start < pg_end) { |
| 705 | struct address_space *mapping = inode->i_mapping; |
| 706 | loff_t blk_start, blk_end; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 707 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jason Hrycay | 1127a3d | 2013-04-08 20:16:44 -0500 | [diff] [blame] | 708 | |
| 709 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 710 | |
| 711 | blk_start = pg_start << PAGE_CACHE_SHIFT; |
| 712 | blk_end = pg_end << PAGE_CACHE_SHIFT; |
| 713 | truncate_inode_pages_range(mapping, blk_start, |
| 714 | blk_end - 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 715 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 716 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 717 | ret = truncate_hole(inode, pg_start, pg_end); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 718 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | static int expand_inode_data(struct inode *inode, loff_t offset, |
| 726 | loff_t len, int mode) |
| 727 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 728 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 729 | pgoff_t index, pg_start, pg_end; |
| 730 | loff_t new_size = i_size_read(inode); |
| 731 | loff_t off_start, off_end; |
| 732 | int ret = 0; |
| 733 | |
Chao Yu | 497a093 | 2014-08-04 10:11:17 +0800 | [diff] [blame] | 734 | f2fs_balance_fs(sbi); |
| 735 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 736 | ret = inode_newsize_ok(inode, (len + offset)); |
| 737 | if (ret) |
| 738 | return ret; |
| 739 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 740 | if (f2fs_has_inline_data(inode)) { |
| 741 | ret = f2fs_convert_inline_inode(inode); |
| 742 | if (ret) |
| 743 | return ret; |
| 744 | } |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 745 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 746 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 747 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 748 | |
| 749 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 750 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 751 | |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 752 | f2fs_lock_op(sbi); |
| 753 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 754 | for (index = pg_start; index <= pg_end; index++) { |
| 755 | struct dnode_of_data dn; |
| 756 | |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 757 | if (index == pg_end && !off_end) |
| 758 | goto noalloc; |
| 759 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 760 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 761 | ret = f2fs_reserve_block(&dn, index); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 762 | if (ret) |
| 763 | break; |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 764 | noalloc: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 765 | if (pg_start == pg_end) |
| 766 | new_size = offset + len; |
| 767 | else if (index == pg_start && off_start) |
| 768 | new_size = (index + 1) << PAGE_CACHE_SHIFT; |
| 769 | else if (index == pg_end) |
| 770 | new_size = (index << PAGE_CACHE_SHIFT) + off_end; |
| 771 | else |
| 772 | new_size += PAGE_CACHE_SIZE; |
| 773 | } |
| 774 | |
| 775 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 776 | i_size_read(inode) < new_size) { |
| 777 | i_size_write(inode, new_size); |
| 778 | mark_inode_dirty(inode); |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 779 | update_inode_page(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 780 | } |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 781 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | static long f2fs_fallocate(struct file *file, int mode, |
| 787 | loff_t offset, loff_t len) |
| 788 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 789 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 790 | long ret; |
| 791 | |
| 792 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 793 | return -EOPNOTSUPP; |
| 794 | |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 795 | mutex_lock(&inode->i_mutex); |
| 796 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 797 | if (mode & FALLOC_FL_PUNCH_HOLE) |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 798 | ret = punch_hole(inode, offset, len); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 799 | else |
| 800 | ret = expand_inode_data(inode, offset, len, mode); |
| 801 | |
Namjae Jeon | 3af60a4 | 2012-12-30 14:52:37 +0900 | [diff] [blame] | 802 | if (!ret) { |
| 803 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 804 | mark_inode_dirty(inode); |
| 805 | } |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 806 | |
| 807 | mutex_unlock(&inode->i_mutex); |
| 808 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 809 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) |
| 814 | #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) |
| 815 | |
| 816 | static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) |
| 817 | { |
| 818 | if (S_ISDIR(mode)) |
| 819 | return flags; |
| 820 | else if (S_ISREG(mode)) |
| 821 | return flags & F2FS_REG_FLMASK; |
| 822 | else |
| 823 | return flags & F2FS_OTHER_FLMASK; |
| 824 | } |
| 825 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 826 | static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 827 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 828 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 829 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 830 | unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 831 | return put_user(flags, (int __user *)arg); |
| 832 | } |
| 833 | |
| 834 | static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) |
| 835 | { |
| 836 | struct inode *inode = file_inode(filp); |
| 837 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 838 | unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 839 | unsigned int oldflags; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 840 | int ret; |
| 841 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 842 | ret = mnt_want_write_file(filp); |
| 843 | if (ret) |
| 844 | return ret; |
| 845 | |
| 846 | if (!inode_owner_or_capable(inode)) { |
| 847 | ret = -EACCES; |
| 848 | goto out; |
| 849 | } |
| 850 | |
| 851 | if (get_user(flags, (int __user *)arg)) { |
| 852 | ret = -EFAULT; |
| 853 | goto out; |
| 854 | } |
| 855 | |
| 856 | flags = f2fs_mask_flags(inode->i_mode, flags); |
| 857 | |
| 858 | mutex_lock(&inode->i_mutex); |
| 859 | |
| 860 | oldflags = fi->i_flags; |
| 861 | |
| 862 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 863 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 864 | mutex_unlock(&inode->i_mutex); |
| 865 | ret = -EPERM; |
| 866 | goto out; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | flags = flags & FS_FL_USER_MODIFIABLE; |
| 871 | flags |= oldflags & ~FS_FL_USER_MODIFIABLE; |
| 872 | fi->i_flags = flags; |
| 873 | mutex_unlock(&inode->i_mutex); |
| 874 | |
| 875 | f2fs_set_inode_flags(inode); |
| 876 | inode->i_ctime = CURRENT_TIME; |
| 877 | mark_inode_dirty(inode); |
| 878 | out: |
| 879 | mnt_drop_write_file(filp); |
| 880 | return ret; |
| 881 | } |
| 882 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 883 | static int f2fs_ioc_start_atomic_write(struct file *filp) |
| 884 | { |
| 885 | struct inode *inode = file_inode(filp); |
| 886 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 887 | |
| 888 | if (!inode_owner_or_capable(inode)) |
| 889 | return -EACCES; |
| 890 | |
| 891 | f2fs_balance_fs(sbi); |
| 892 | |
| 893 | set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
| 894 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 895 | return f2fs_convert_inline_inode(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static int f2fs_ioc_commit_atomic_write(struct file *filp) |
| 899 | { |
| 900 | struct inode *inode = file_inode(filp); |
| 901 | int ret; |
| 902 | |
| 903 | if (!inode_owner_or_capable(inode)) |
| 904 | return -EACCES; |
| 905 | |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 906 | if (f2fs_is_volatile_file(inode)) |
| 907 | return 0; |
| 908 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 909 | ret = mnt_want_write_file(filp); |
| 910 | if (ret) |
| 911 | return ret; |
| 912 | |
| 913 | if (f2fs_is_atomic_file(inode)) |
| 914 | commit_inmem_pages(inode, false); |
| 915 | |
| 916 | ret = f2fs_sync_file(filp, 0, LONG_MAX, 0); |
| 917 | mnt_drop_write_file(filp); |
| 918 | return ret; |
| 919 | } |
| 920 | |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 921 | static int f2fs_ioc_start_volatile_write(struct file *filp) |
| 922 | { |
| 923 | struct inode *inode = file_inode(filp); |
| 924 | |
| 925 | if (!inode_owner_or_capable(inode)) |
| 926 | return -EACCES; |
| 927 | |
| 928 | set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 929 | |
| 930 | return f2fs_convert_inline_inode(inode); |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 933 | static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) |
| 934 | { |
| 935 | struct inode *inode = file_inode(filp); |
| 936 | struct super_block *sb = inode->i_sb; |
| 937 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
| 938 | struct fstrim_range range; |
| 939 | int ret; |
| 940 | |
| 941 | if (!capable(CAP_SYS_ADMIN)) |
| 942 | return -EPERM; |
| 943 | |
| 944 | if (!blk_queue_discard(q)) |
| 945 | return -EOPNOTSUPP; |
| 946 | |
| 947 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
| 948 | sizeof(range))) |
| 949 | return -EFAULT; |
| 950 | |
| 951 | range.minlen = max((unsigned int)range.minlen, |
| 952 | q->limits.discard_granularity); |
| 953 | ret = f2fs_trim_fs(F2FS_SB(sb), &range); |
| 954 | if (ret < 0) |
| 955 | return ret; |
| 956 | |
| 957 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
| 958 | sizeof(range))) |
| 959 | return -EFAULT; |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 964 | { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 965 | switch (cmd) { |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 966 | case F2FS_IOC_GETFLAGS: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 967 | return f2fs_ioc_getflags(filp, arg); |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 968 | case F2FS_IOC_SETFLAGS: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 969 | return f2fs_ioc_setflags(filp, arg); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 970 | case F2FS_IOC_START_ATOMIC_WRITE: |
| 971 | return f2fs_ioc_start_atomic_write(filp); |
| 972 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: |
| 973 | return f2fs_ioc_commit_atomic_write(filp); |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 974 | case F2FS_IOC_START_VOLATILE_WRITE: |
| 975 | return f2fs_ioc_start_volatile_write(filp); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 976 | case FITRIM: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 977 | return f2fs_ioc_fitrim(filp, arg); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 978 | default: |
| 979 | return -ENOTTY; |
| 980 | } |
| 981 | } |
| 982 | |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 983 | #ifdef CONFIG_COMPAT |
| 984 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 985 | { |
| 986 | switch (cmd) { |
| 987 | case F2FS_IOC32_GETFLAGS: |
| 988 | cmd = F2FS_IOC_GETFLAGS; |
| 989 | break; |
| 990 | case F2FS_IOC32_SETFLAGS: |
| 991 | cmd = F2FS_IOC_SETFLAGS; |
| 992 | break; |
| 993 | default: |
| 994 | return -ENOIOCTLCMD; |
| 995 | } |
| 996 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 997 | } |
| 998 | #endif |
| 999 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1000 | const struct file_operations f2fs_file_operations = { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 1001 | .llseek = f2fs_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 1002 | .read = new_sync_read, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 1003 | .write = new_sync_write, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 1004 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 1005 | .write_iter = generic_file_write_iter, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1006 | .open = generic_file_open, |
| 1007 | .mmap = f2fs_file_mmap, |
| 1008 | .fsync = f2fs_sync_file, |
| 1009 | .fallocate = f2fs_fallocate, |
| 1010 | .unlocked_ioctl = f2fs_ioctl, |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 1011 | #ifdef CONFIG_COMPAT |
| 1012 | .compat_ioctl = f2fs_compat_ioctl, |
| 1013 | #endif |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1014 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 1015 | .splice_write = iter_file_splice_write, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1016 | }; |