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