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