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 | f424f66 | 2015-04-20 15:19:06 -0700 | [diff] [blame] | 23 | #include <linux/random.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 24 | |
| 25 | #include "f2fs.h" |
| 26 | #include "node.h" |
| 27 | #include "segment.h" |
| 28 | #include "xattr.h" |
| 29 | #include "acl.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 30 | #include "trace.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 31 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 32 | |
| 33 | static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, |
| 34 | struct vm_fault *vmf) |
| 35 | { |
| 36 | struct page *page = vmf->page; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 37 | struct inode *inode = file_inode(vma->vm_file); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 38 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 39 | struct dnode_of_data dn; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 40 | int err; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 41 | |
| 42 | f2fs_balance_fs(sbi); |
| 43 | |
| 44 | sb_start_pagefault(inode->i_sb); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 45 | |
| 46 | f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 47 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 48 | /* block allocation */ |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 49 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 50 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 51 | err = f2fs_reserve_block(&dn, page->index); |
| 52 | if (err) { |
| 53 | f2fs_unlock_op(sbi); |
| 54 | goto out; |
| 55 | } |
| 56 | f2fs_put_dnode(&dn); |
| 57 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 58 | |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 59 | file_update_time(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 60 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 61 | if (unlikely(page->mapping != inode->i_mapping || |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 62 | page_offset(page) > i_size_read(inode) || |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 63 | !PageUptodate(page))) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 64 | unlock_page(page); |
| 65 | err = -EFAULT; |
| 66 | goto out; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * check to see if the page is mapped already (no holes) |
| 71 | */ |
| 72 | if (PageMappedToDisk(page)) |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 73 | goto mapped; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 74 | |
| 75 | /* page is wholly or partially inside EOF */ |
| 76 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { |
| 77 | unsigned offset; |
| 78 | offset = i_size_read(inode) & ~PAGE_CACHE_MASK; |
| 79 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 80 | } |
| 81 | set_page_dirty(page); |
| 82 | SetPageUptodate(page); |
| 83 | |
Jaegeuk Kim | e943a10 | 2013-10-25 14:26:31 +0900 | [diff] [blame] | 84 | trace_f2fs_vm_page_mkwrite(page, DATA); |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 85 | mapped: |
| 86 | /* fill the page */ |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 87 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 88 | out: |
| 89 | sb_end_pagefault(inode->i_sb); |
| 90 | return block_page_mkwrite_return(err); |
| 91 | } |
| 92 | |
| 93 | static const struct vm_operations_struct f2fs_file_vm_ops = { |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 94 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 95 | .map_pages = filemap_map_pages, |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 96 | .page_mkwrite = f2fs_vm_page_mkwrite, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 97 | }; |
| 98 | |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 99 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
| 100 | { |
| 101 | struct dentry *dentry; |
| 102 | |
| 103 | inode = igrab(inode); |
| 104 | dentry = d_find_any_alias(inode); |
| 105 | iput(inode); |
| 106 | if (!dentry) |
| 107 | return 0; |
| 108 | |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 109 | if (update_dent_inode(inode, inode, &dentry->d_name)) { |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 110 | dput(dentry); |
| 111 | return 0; |
| 112 | } |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 113 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 114 | *pino = parent_ino(dentry); |
| 115 | dput(dentry); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 119 | static inline bool need_do_checkpoint(struct inode *inode) |
| 120 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 121 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 122 | bool need_cp = false; |
| 123 | |
| 124 | if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) |
| 125 | need_cp = true; |
Jaegeuk Kim | e7d5545 | 2015-04-29 17:02:18 -0700 | [diff] [blame] | 126 | else if (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino)) |
| 127 | need_cp = true; |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 128 | else if (file_wrong_pino(inode)) |
| 129 | need_cp = true; |
| 130 | else if (!space_for_roll_forward(sbi)) |
| 131 | need_cp = true; |
| 132 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
| 133 | need_cp = true; |
| 134 | else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) |
| 135 | need_cp = true; |
Jaegeuk Kim | d5053a34 | 2014-10-30 22:47:03 -0700 | [diff] [blame] | 136 | else if (test_opt(sbi, FASTBOOT)) |
| 137 | need_cp = true; |
Jaegeuk Kim | a344b9f | 2014-11-05 20:05:53 -0800 | [diff] [blame] | 138 | else if (sbi->active_logs == 2) |
| 139 | need_cp = true; |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 140 | |
| 141 | return need_cp; |
| 142 | } |
| 143 | |
Changman Lee | 9c7bb70 | 2014-12-08 15:29:40 +0900 | [diff] [blame] | 144 | static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) |
| 145 | { |
| 146 | struct page *i = find_get_page(NODE_MAPPING(sbi), ino); |
| 147 | bool ret = false; |
| 148 | /* But we need to avoid that there are some inode updates */ |
| 149 | if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) |
| 150 | ret = true; |
| 151 | f2fs_put_page(i, 0); |
| 152 | return ret; |
| 153 | } |
| 154 | |
Changman Lee | 51455b1 | 2014-12-08 15:29:41 +0900 | [diff] [blame] | 155 | static void try_to_fix_pino(struct inode *inode) |
| 156 | { |
| 157 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 158 | nid_t pino; |
| 159 | |
| 160 | down_write(&fi->i_sem); |
| 161 | fi->xattr_ver = 0; |
| 162 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
| 163 | get_parent_ino(inode, &pino)) { |
| 164 | fi->i_pino = pino; |
| 165 | file_got_pino(inode); |
| 166 | up_write(&fi->i_sem); |
| 167 | |
| 168 | mark_inode_dirty_sync(inode); |
| 169 | f2fs_write_inode(inode, NULL); |
| 170 | } else { |
| 171 | up_write(&fi->i_sem); |
| 172 | } |
| 173 | } |
| 174 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 175 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
| 176 | { |
| 177 | struct inode *inode = file->f_mapping->host; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 178 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 179 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 180 | nid_t ino = inode->i_ino; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 181 | int ret = 0; |
| 182 | bool need_cp = false; |
| 183 | struct writeback_control wbc = { |
Jaegeuk Kim | c81bf1c | 2014-03-03 11:28:40 +0900 | [diff] [blame] | 184 | .sync_mode = WB_SYNC_ALL, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 185 | .nr_to_write = LONG_MAX, |
| 186 | .for_reclaim = 0, |
| 187 | }; |
| 188 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 189 | if (unlikely(f2fs_readonly(inode->i_sb))) |
Namjae Jeon | 1fa95b0 | 2012-12-01 10:56:01 +0900 | [diff] [blame] | 190 | return 0; |
| 191 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 192 | trace_f2fs_sync_file_enter(inode); |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 193 | |
| 194 | /* if fdatasync is triggered, let's do in-place-update */ |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 195 | if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 196 | set_inode_flag(fi, FI_NEED_IPU); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 197 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 198 | clear_inode_flag(fi, FI_NEED_IPU); |
| 199 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 200 | if (ret) { |
| 201 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 202 | return ret; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 203 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 204 | |
Changman Lee | 9c7bb70 | 2014-12-08 15:29:40 +0900 | [diff] [blame] | 205 | /* if the inode is dirty, let's recover all the time */ |
| 206 | if (!datasync && is_inode_flag_set(fi, FI_DIRTY_INODE)) { |
| 207 | update_inode_page(inode); |
| 208 | goto go_write; |
| 209 | } |
| 210 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 211 | /* |
| 212 | * if there is no written data, don't waste time to write recovery info. |
| 213 | */ |
| 214 | if (!is_inode_flag_set(fi, FI_APPEND_WRITE) && |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 215 | !exist_written_data(sbi, ino, APPEND_INO)) { |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 216 | |
Changman Lee | 9c7bb70 | 2014-12-08 15:29:40 +0900 | [diff] [blame] | 217 | /* it may call write_inode just prior to fsync */ |
| 218 | if (need_inode_page_update(sbi, ino)) |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 219 | goto go_write; |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 220 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 221 | if (is_inode_flag_set(fi, FI_UPDATE_WRITE) || |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 222 | exist_written_data(sbi, ino, UPDATE_INO)) |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 223 | goto flush_out; |
| 224 | goto out; |
| 225 | } |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 226 | go_write: |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 227 | /* guarantee free sections for fsync */ |
| 228 | f2fs_balance_fs(sbi); |
| 229 | |
Jaegeuk Kim | e5d2385 | 2013-07-03 10:55:52 +0900 | [diff] [blame] | 230 | /* |
| 231 | * Both of fdatasync() and fsync() are able to be recovered from |
| 232 | * sudden-power-off. |
| 233 | */ |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 234 | down_read(&fi->i_sem); |
| 235 | need_cp = need_do_checkpoint(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 236 | up_read(&fi->i_sem); |
| 237 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 238 | if (need_cp) { |
| 239 | /* all the dirty node pages should be flushed for POR */ |
| 240 | ret = f2fs_sync_fs(inode->i_sb, 1); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 241 | |
Changman Lee | 51455b1 | 2014-12-08 15:29:41 +0900 | [diff] [blame] | 242 | /* |
| 243 | * We've secured consistency through sync_fs. Following pino |
| 244 | * will be used only for fsynced inodes after checkpoint. |
| 245 | */ |
| 246 | try_to_fix_pino(inode); |
Jaegeuk Kim | cff2852 | 2015-03-02 10:48:56 -0800 | [diff] [blame] | 247 | clear_inode_flag(fi, FI_APPEND_WRITE); |
| 248 | clear_inode_flag(fi, FI_UPDATE_WRITE); |
Changman Lee | 51455b1 | 2014-12-08 15:29:41 +0900 | [diff] [blame] | 249 | goto out; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 250 | } |
Changman Lee | 51455b1 | 2014-12-08 15:29:41 +0900 | [diff] [blame] | 251 | sync_nodes: |
| 252 | sync_node_pages(sbi, ino, &wbc); |
| 253 | |
Jaegeuk Kim | 871f599 | 2015-01-09 16:27:17 -0800 | [diff] [blame] | 254 | /* if cp_error was enabled, we should avoid infinite loop */ |
| 255 | if (unlikely(f2fs_cp_error(sbi))) |
| 256 | goto out; |
| 257 | |
Changman Lee | 51455b1 | 2014-12-08 15:29:41 +0900 | [diff] [blame] | 258 | if (need_inode_block_update(sbi, ino)) { |
| 259 | mark_inode_dirty_sync(inode); |
| 260 | f2fs_write_inode(inode, NULL); |
| 261 | goto sync_nodes; |
| 262 | } |
| 263 | |
| 264 | ret = wait_on_node_pages_writeback(sbi, ino); |
| 265 | if (ret) |
| 266 | goto out; |
| 267 | |
| 268 | /* once recovery info is written, don't need to tack this */ |
| 269 | remove_dirty_inode(sbi, ino, APPEND_INO); |
| 270 | clear_inode_flag(fi, FI_APPEND_WRITE); |
| 271 | flush_out: |
| 272 | remove_dirty_inode(sbi, ino, UPDATE_INO); |
| 273 | clear_inode_flag(fi, FI_UPDATE_WRITE); |
| 274 | ret = f2fs_issue_flush(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 275 | out: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 276 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 277 | f2fs_trace_ios(NULL, 1); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 278 | return ret; |
| 279 | } |
| 280 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 281 | static pgoff_t __get_first_dirty_index(struct address_space *mapping, |
| 282 | pgoff_t pgofs, int whence) |
| 283 | { |
| 284 | struct pagevec pvec; |
| 285 | int nr_pages; |
| 286 | |
| 287 | if (whence != SEEK_DATA) |
| 288 | return 0; |
| 289 | |
| 290 | /* find first dirty page index */ |
| 291 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 65b85cc | 2014-07-30 17:25:54 -0700 | [diff] [blame] | 292 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, |
| 293 | PAGECACHE_TAG_DIRTY, 1); |
| 294 | pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 295 | pagevec_release(&pvec); |
| 296 | return pgofs; |
| 297 | } |
| 298 | |
| 299 | static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, |
| 300 | int whence) |
| 301 | { |
| 302 | switch (whence) { |
| 303 | case SEEK_DATA: |
| 304 | if ((blkaddr == NEW_ADDR && dirty == pgofs) || |
| 305 | (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) |
| 306 | return true; |
| 307 | break; |
| 308 | case SEEK_HOLE: |
| 309 | if (blkaddr == NULL_ADDR) |
| 310 | return true; |
| 311 | break; |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 316 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
| 317 | { |
| 318 | struct inode *inode = file->f_mapping->host; |
| 319 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 320 | struct dnode_of_data dn; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 321 | pgoff_t pgofs, end_offset, dirty; |
| 322 | loff_t data_ofs = offset; |
| 323 | loff_t isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 324 | int err = 0; |
| 325 | |
| 326 | mutex_lock(&inode->i_mutex); |
| 327 | |
| 328 | isize = i_size_read(inode); |
| 329 | if (offset >= isize) |
| 330 | goto fail; |
| 331 | |
| 332 | /* handle inline data case */ |
Chao Yu | 622f28a | 2014-09-24 18:19:10 +0800 | [diff] [blame] | 333 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 334 | if (whence == SEEK_HOLE) |
| 335 | data_ofs = isize; |
| 336 | goto found; |
| 337 | } |
| 338 | |
| 339 | pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT); |
| 340 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 341 | dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); |
| 342 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 343 | for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 344 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 345 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); |
| 346 | if (err && err != -ENOENT) { |
| 347 | goto fail; |
| 348 | } else if (err == -ENOENT) { |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 349 | /* direct node does not exists */ |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 350 | if (whence == SEEK_DATA) { |
| 351 | pgofs = PGOFS_OF_NEXT_DNODE(pgofs, |
| 352 | F2FS_I(inode)); |
| 353 | continue; |
| 354 | } else { |
| 355 | goto found; |
| 356 | } |
| 357 | } |
| 358 | |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 359 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 360 | |
| 361 | /* find data/hole in dnode block */ |
| 362 | for (; dn.ofs_in_node < end_offset; |
| 363 | dn.ofs_in_node++, pgofs++, |
Chao Yu | 2e02317 | 2015-02-09 11:23:58 +0800 | [diff] [blame] | 364 | data_ofs = (loff_t)pgofs << PAGE_CACHE_SHIFT) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 365 | block_t blkaddr; |
| 366 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 367 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 368 | if (__found_offset(blkaddr, dirty, pgofs, whence)) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 369 | f2fs_put_dnode(&dn); |
| 370 | goto found; |
| 371 | } |
| 372 | } |
| 373 | f2fs_put_dnode(&dn); |
| 374 | } |
| 375 | |
| 376 | if (whence == SEEK_DATA) |
| 377 | goto fail; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 378 | found: |
Jaegeuk Kim | fe369bc | 2014-04-28 17:02:48 +0900 | [diff] [blame] | 379 | if (whence == SEEK_HOLE && data_ofs > isize) |
| 380 | data_ofs = isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 381 | mutex_unlock(&inode->i_mutex); |
| 382 | return vfs_setpos(file, data_ofs, maxbytes); |
| 383 | fail: |
| 384 | mutex_unlock(&inode->i_mutex); |
| 385 | return -ENXIO; |
| 386 | } |
| 387 | |
| 388 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) |
| 389 | { |
| 390 | struct inode *inode = file->f_mapping->host; |
| 391 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 392 | |
| 393 | switch (whence) { |
| 394 | case SEEK_SET: |
| 395 | case SEEK_CUR: |
| 396 | case SEEK_END: |
| 397 | return generic_file_llseek_size(file, offset, whence, |
| 398 | maxbytes, i_size_read(inode)); |
| 399 | case SEEK_DATA: |
| 400 | case SEEK_HOLE: |
Jaegeuk Kim | 0b4c5afde | 2014-09-08 10:59:43 -0700 | [diff] [blame] | 401 | if (offset < 0) |
| 402 | return -ENXIO; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 403 | return f2fs_seek_block(file, offset, whence); |
| 404 | } |
| 405 | |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 409 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 410 | { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 411 | struct inode *inode = file_inode(file); |
| 412 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 413 | if (f2fs_encrypted_inode(inode)) { |
| 414 | int err = f2fs_get_encryption_info(inode); |
| 415 | if (err) |
| 416 | return 0; |
| 417 | } |
| 418 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 419 | /* we don't need to use inline_data strictly */ |
| 420 | if (f2fs_has_inline_data(inode)) { |
| 421 | int err = f2fs_convert_inline_inode(inode); |
| 422 | if (err) |
| 423 | return err; |
| 424 | } |
| 425 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 426 | file_accessed(file); |
| 427 | vma->vm_ops = &f2fs_file_vm_ops; |
| 428 | return 0; |
| 429 | } |
| 430 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 431 | static int f2fs_file_open(struct inode *inode, struct file *filp) |
| 432 | { |
| 433 | int ret = generic_file_open(inode, filp); |
| 434 | |
| 435 | if (!ret && f2fs_encrypted_inode(inode)) { |
| 436 | ret = f2fs_get_encryption_info(inode); |
| 437 | if (ret) |
| 438 | ret = -EACCES; |
| 439 | } |
| 440 | return ret; |
| 441 | } |
| 442 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 443 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 444 | { |
| 445 | int nr_free = 0, ofs = dn->ofs_in_node; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 446 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 447 | struct f2fs_node *raw_node; |
| 448 | __le32 *addr; |
| 449 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 450 | raw_node = F2FS_NODE(dn->node_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 451 | addr = blkaddr_in_node(raw_node) + ofs; |
| 452 | |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 453 | for (; count > 0; count--, addr++, dn->ofs_in_node++) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 454 | block_t blkaddr = le32_to_cpu(*addr); |
| 455 | if (blkaddr == NULL_ADDR) |
| 456 | continue; |
| 457 | |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 458 | dn->data_blkaddr = NULL_ADDR; |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 459 | set_data_blkaddr(dn); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 460 | f2fs_update_extent_cache(dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 461 | invalidate_blocks(sbi, blkaddr); |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 462 | if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) |
| 463 | clear_inode_flag(F2FS_I(dn->inode), |
| 464 | FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 465 | nr_free++; |
| 466 | } |
| 467 | if (nr_free) { |
Namjae Jeon | d7cc950 | 2013-06-08 21:25:40 +0900 | [diff] [blame] | 468 | dec_valid_block_count(sbi, dn->inode, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 469 | set_page_dirty(dn->node_page); |
| 470 | sync_inode_page(dn); |
| 471 | } |
| 472 | dn->ofs_in_node = ofs; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 473 | |
| 474 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
| 475 | dn->ofs_in_node, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 476 | return nr_free; |
| 477 | } |
| 478 | |
| 479 | void truncate_data_blocks(struct dnode_of_data *dn) |
| 480 | { |
| 481 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); |
| 482 | } |
| 483 | |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 484 | static int truncate_partial_data_page(struct inode *inode, u64 from, |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 485 | bool cache_only) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 486 | { |
| 487 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 488 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 489 | struct address_space *mapping = inode->i_mapping; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 490 | struct page *page; |
| 491 | |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 492 | if (!offset && !cache_only) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 493 | return 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 494 | |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 495 | if (cache_only) { |
| 496 | page = grab_cache_page(mapping, index); |
| 497 | if (page && PageUptodate(page)) |
| 498 | goto truncate_out; |
| 499 | f2fs_put_page(page, 1); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | page = get_lock_data_page(inode, index); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 504 | if (IS_ERR(page)) |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 505 | return 0; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 506 | truncate_out: |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 507 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 508 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 509 | if (!cache_only || !f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode)) |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 510 | set_page_dirty(page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 511 | f2fs_put_page(page, 1); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 512 | return 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 513 | } |
| 514 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 515 | int truncate_blocks(struct inode *inode, u64 from, bool lock) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 516 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 517 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 518 | unsigned int blocksize = inode->i_sb->s_blocksize; |
| 519 | struct dnode_of_data dn; |
| 520 | pgoff_t free_from; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 521 | int count = 0, err = 0; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 522 | struct page *ipage; |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 523 | bool truncate_page = false; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 524 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 525 | trace_f2fs_truncate_blocks_enter(inode, from); |
| 526 | |
Jaegeuk Kim | f7ef9b8 | 2015-02-09 12:02:44 -0800 | [diff] [blame] | 527 | free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 528 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 529 | if (lock) |
| 530 | f2fs_lock_op(sbi); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 531 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 532 | ipage = get_node_page(sbi, inode->i_ino); |
| 533 | if (IS_ERR(ipage)) { |
| 534 | err = PTR_ERR(ipage); |
| 535 | goto out; |
| 536 | } |
| 537 | |
| 538 | if (f2fs_has_inline_data(inode)) { |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 539 | if (truncate_inline_inode(ipage, from)) |
| 540 | set_page_dirty(ipage); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 541 | f2fs_put_page(ipage, 1); |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 542 | truncate_page = true; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 547 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 548 | if (err) { |
| 549 | if (err == -ENOENT) |
| 550 | goto free_next; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 551 | goto out; |
Jaegeuk Kim | 1ce86bf | 2014-10-15 10:24:34 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 554 | count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 555 | |
| 556 | count -= dn.ofs_in_node; |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 557 | f2fs_bug_on(sbi, count < 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 558 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 559 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
| 560 | truncate_data_blocks_range(&dn, count); |
| 561 | free_from += count; |
| 562 | } |
| 563 | |
| 564 | f2fs_put_dnode(&dn); |
| 565 | free_next: |
| 566 | err = truncate_inode_blocks(inode, free_from); |
Jaegeuk Kim | 764d2c8 | 2014-11-11 11:01:01 -0800 | [diff] [blame] | 567 | out: |
| 568 | if (lock) |
| 569 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 570 | |
| 571 | /* lastly zero out the first data page */ |
| 572 | if (!err) |
Chao Yu | 0bfcfcc | 2015-03-10 13:16:25 +0800 | [diff] [blame] | 573 | err = truncate_partial_data_page(inode, from, truncate_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 574 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 575 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 576 | return err; |
| 577 | } |
| 578 | |
| 579 | void f2fs_truncate(struct inode *inode) |
| 580 | { |
| 581 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 582 | S_ISLNK(inode->i_mode))) |
| 583 | return; |
| 584 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 585 | trace_f2fs_truncate(inode); |
| 586 | |
Jaegeuk Kim | 92dffd0 | 2014-11-11 14:10:01 -0800 | [diff] [blame] | 587 | /* we should check inline_data size */ |
Jaegeuk Kim | 01b960e | 2015-04-23 10:27:21 -0700 | [diff] [blame] | 588 | if (f2fs_has_inline_data(inode) && !f2fs_may_inline_data(inode)) { |
Jaegeuk Kim | 92dffd0 | 2014-11-11 14:10:01 -0800 | [diff] [blame] | 589 | if (f2fs_convert_inline_inode(inode)) |
| 590 | return; |
| 591 | } |
| 592 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 593 | if (!truncate_blocks(inode, i_size_read(inode), true)) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 594 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 595 | mark_inode_dirty(inode); |
| 596 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 597 | } |
| 598 | |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 599 | int f2fs_getattr(struct vfsmount *mnt, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 600 | struct dentry *dentry, struct kstat *stat) |
| 601 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 602 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 603 | generic_fillattr(inode, stat); |
| 604 | stat->blocks <<= 3; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 609 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) |
| 610 | { |
| 611 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 612 | unsigned int ia_valid = attr->ia_valid; |
| 613 | |
| 614 | if (ia_valid & ATTR_UID) |
| 615 | inode->i_uid = attr->ia_uid; |
| 616 | if (ia_valid & ATTR_GID) |
| 617 | inode->i_gid = attr->ia_gid; |
| 618 | if (ia_valid & ATTR_ATIME) |
| 619 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 620 | inode->i_sb->s_time_gran); |
| 621 | if (ia_valid & ATTR_MTIME) |
| 622 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 623 | inode->i_sb->s_time_gran); |
| 624 | if (ia_valid & ATTR_CTIME) |
| 625 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 626 | inode->i_sb->s_time_gran); |
| 627 | if (ia_valid & ATTR_MODE) { |
| 628 | umode_t mode = attr->ia_mode; |
| 629 | |
| 630 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 631 | mode &= ~S_ISGID; |
| 632 | set_acl_inode(fi, mode); |
| 633 | } |
| 634 | } |
| 635 | #else |
| 636 | #define __setattr_copy setattr_copy |
| 637 | #endif |
| 638 | |
| 639 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) |
| 640 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 641 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 642 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 643 | int err; |
| 644 | |
| 645 | err = inode_change_ok(inode, attr); |
| 646 | if (err) |
| 647 | return err; |
| 648 | |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame] | 649 | if (attr->ia_valid & ATTR_SIZE) { |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 650 | if (f2fs_encrypted_inode(inode) && |
| 651 | f2fs_get_encryption_info(inode)) |
| 652 | return -EACCES; |
| 653 | |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame] | 654 | if (attr->ia_size != i_size_read(inode)) { |
| 655 | truncate_setsize(inode, attr->ia_size); |
| 656 | f2fs_truncate(inode); |
| 657 | f2fs_balance_fs(F2FS_I_SB(inode)); |
| 658 | } else { |
| 659 | /* |
| 660 | * giving a chance to truncate blocks past EOF which |
| 661 | * are fallocated with FALLOC_FL_KEEP_SIZE. |
| 662 | */ |
| 663 | f2fs_truncate(inode); |
| 664 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | __setattr_copy(inode, attr); |
| 668 | |
| 669 | if (attr->ia_valid & ATTR_MODE) { |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 670 | err = posix_acl_chmod(inode, get_inode_mode(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 671 | if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 672 | inode->i_mode = fi->i_acl_mode; |
| 673 | clear_inode_flag(fi, FI_ACL_MODE); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | mark_inode_dirty(inode); |
| 678 | return err; |
| 679 | } |
| 680 | |
| 681 | const struct inode_operations f2fs_file_inode_operations = { |
| 682 | .getattr = f2fs_getattr, |
| 683 | .setattr = f2fs_setattr, |
| 684 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 685 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 686 | #ifdef CONFIG_F2FS_FS_XATTR |
| 687 | .setxattr = generic_setxattr, |
| 688 | .getxattr = generic_getxattr, |
| 689 | .listxattr = f2fs_listxattr, |
| 690 | .removexattr = generic_removexattr, |
| 691 | #endif |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 692 | .fiemap = f2fs_fiemap, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 693 | }; |
| 694 | |
| 695 | static void fill_zero(struct inode *inode, pgoff_t index, |
| 696 | loff_t start, loff_t len) |
| 697 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 698 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 699 | struct page *page; |
| 700 | |
| 701 | if (!len) |
| 702 | return; |
| 703 | |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 704 | f2fs_balance_fs(sbi); |
| 705 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 706 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 707 | page = get_new_data_page(inode, NULL, index, false); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 708 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 709 | |
| 710 | if (!IS_ERR(page)) { |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 711 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 712 | zero_user(page, start, len); |
| 713 | set_page_dirty(page); |
| 714 | f2fs_put_page(page, 1); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
| 719 | { |
| 720 | pgoff_t index; |
| 721 | int err; |
| 722 | |
| 723 | for (index = pg_start; index < pg_end; index++) { |
| 724 | struct dnode_of_data dn; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 725 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 726 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 727 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 728 | if (err) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 729 | if (err == -ENOENT) |
| 730 | continue; |
| 731 | return err; |
| 732 | } |
| 733 | |
| 734 | if (dn.data_blkaddr != NULL_ADDR) |
| 735 | truncate_data_blocks_range(&dn, 1); |
| 736 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 737 | } |
| 738 | return 0; |
| 739 | } |
| 740 | |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 741 | 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] | 742 | { |
| 743 | pgoff_t pg_start, pg_end; |
| 744 | loff_t off_start, off_end; |
| 745 | int ret = 0; |
| 746 | |
Chao Yu | 14cecc5 | 2014-09-15 18:03:32 +0800 | [diff] [blame] | 747 | if (!S_ISREG(inode->i_mode)) |
| 748 | return -EOPNOTSUPP; |
| 749 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 750 | if (f2fs_has_inline_data(inode)) { |
| 751 | ret = f2fs_convert_inline_inode(inode); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 755 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 756 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 757 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 758 | |
| 759 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 760 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 761 | |
| 762 | if (pg_start == pg_end) { |
| 763 | fill_zero(inode, pg_start, off_start, |
| 764 | off_end - off_start); |
| 765 | } else { |
| 766 | if (off_start) |
| 767 | fill_zero(inode, pg_start++, off_start, |
| 768 | PAGE_CACHE_SIZE - off_start); |
| 769 | if (off_end) |
| 770 | fill_zero(inode, pg_end, 0, off_end); |
| 771 | |
| 772 | if (pg_start < pg_end) { |
| 773 | struct address_space *mapping = inode->i_mapping; |
| 774 | loff_t blk_start, blk_end; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 775 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jason Hrycay | 1127a3d | 2013-04-08 20:16:44 -0500 | [diff] [blame] | 776 | |
| 777 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 778 | |
| 779 | blk_start = pg_start << PAGE_CACHE_SHIFT; |
| 780 | blk_end = pg_end << PAGE_CACHE_SHIFT; |
| 781 | truncate_inode_pages_range(mapping, blk_start, |
| 782 | blk_end - 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 783 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 784 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 785 | ret = truncate_hole(inode, pg_start, pg_end); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 786 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 793 | static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end) |
| 794 | { |
| 795 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 796 | struct dnode_of_data dn; |
| 797 | pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; |
| 798 | int ret = 0; |
| 799 | |
| 800 | f2fs_lock_op(sbi); |
| 801 | |
| 802 | for (; end < nrpages; start++, end++) { |
| 803 | block_t new_addr, old_addr; |
| 804 | |
| 805 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 806 | ret = get_dnode_of_data(&dn, end, LOOKUP_NODE_RA); |
| 807 | if (ret && ret != -ENOENT) { |
| 808 | goto out; |
| 809 | } else if (ret == -ENOENT) { |
| 810 | new_addr = NULL_ADDR; |
| 811 | } else { |
| 812 | new_addr = dn.data_blkaddr; |
| 813 | truncate_data_blocks_range(&dn, 1); |
| 814 | f2fs_put_dnode(&dn); |
| 815 | } |
| 816 | |
| 817 | if (new_addr == NULL_ADDR) { |
| 818 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 819 | ret = get_dnode_of_data(&dn, start, LOOKUP_NODE_RA); |
| 820 | if (ret && ret != -ENOENT) |
| 821 | goto out; |
| 822 | else if (ret == -ENOENT) |
| 823 | continue; |
| 824 | |
| 825 | if (dn.data_blkaddr == NULL_ADDR) { |
| 826 | f2fs_put_dnode(&dn); |
| 827 | continue; |
| 828 | } else { |
| 829 | truncate_data_blocks_range(&dn, 1); |
| 830 | } |
| 831 | |
| 832 | f2fs_put_dnode(&dn); |
| 833 | } else { |
| 834 | struct page *ipage; |
| 835 | |
| 836 | ipage = get_node_page(sbi, inode->i_ino); |
| 837 | if (IS_ERR(ipage)) { |
| 838 | ret = PTR_ERR(ipage); |
| 839 | goto out; |
| 840 | } |
| 841 | |
| 842 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
| 843 | ret = f2fs_reserve_block(&dn, start); |
| 844 | if (ret) |
| 845 | goto out; |
| 846 | |
| 847 | old_addr = dn.data_blkaddr; |
| 848 | if (old_addr != NEW_ADDR && new_addr == NEW_ADDR) { |
| 849 | dn.data_blkaddr = NULL_ADDR; |
| 850 | f2fs_update_extent_cache(&dn); |
| 851 | invalidate_blocks(sbi, old_addr); |
| 852 | |
| 853 | dn.data_blkaddr = new_addr; |
| 854 | set_data_blkaddr(&dn); |
| 855 | } else if (new_addr != NEW_ADDR) { |
| 856 | struct node_info ni; |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 857 | |
| 858 | get_node_info(sbi, dn.nid, &ni); |
Chao Yu | 528e345 | 2015-05-28 19:15:35 +0800 | [diff] [blame] | 859 | f2fs_replace_block(sbi, &dn, old_addr, new_addr, |
| 860 | ni.version, true); |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | f2fs_put_dnode(&dn); |
| 864 | } |
| 865 | } |
| 866 | ret = 0; |
| 867 | out: |
| 868 | f2fs_unlock_op(sbi); |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) |
| 873 | { |
| 874 | pgoff_t pg_start, pg_end; |
| 875 | loff_t new_size; |
| 876 | int ret; |
| 877 | |
| 878 | if (!S_ISREG(inode->i_mode)) |
| 879 | return -EINVAL; |
| 880 | |
| 881 | if (offset + len >= i_size_read(inode)) |
| 882 | return -EINVAL; |
| 883 | |
| 884 | /* collapse range should be aligned to block size of f2fs. */ |
| 885 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) |
| 886 | return -EINVAL; |
| 887 | |
| 888 | pg_start = offset >> PAGE_CACHE_SHIFT; |
| 889 | pg_end = (offset + len) >> PAGE_CACHE_SHIFT; |
| 890 | |
| 891 | /* write out all dirty pages from offset */ |
| 892 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); |
| 893 | if (ret) |
| 894 | return ret; |
| 895 | |
| 896 | truncate_pagecache(inode, offset); |
| 897 | |
| 898 | ret = f2fs_do_collapse(inode, pg_start, pg_end); |
| 899 | if (ret) |
| 900 | return ret; |
| 901 | |
| 902 | new_size = i_size_read(inode) - len; |
| 903 | |
| 904 | ret = truncate_blocks(inode, new_size, true); |
| 905 | if (!ret) |
| 906 | i_size_write(inode, new_size); |
| 907 | |
| 908 | return ret; |
| 909 | } |
| 910 | |
Chao Yu | 75cd4e0 | 2015-05-06 13:11:13 +0800 | [diff] [blame] | 911 | static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, |
| 912 | int mode) |
| 913 | { |
| 914 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 915 | struct address_space *mapping = inode->i_mapping; |
| 916 | pgoff_t index, pg_start, pg_end; |
| 917 | loff_t new_size = i_size_read(inode); |
| 918 | loff_t off_start, off_end; |
| 919 | int ret = 0; |
| 920 | |
| 921 | if (!S_ISREG(inode->i_mode)) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | ret = inode_newsize_ok(inode, (len + offset)); |
| 925 | if (ret) |
| 926 | return ret; |
| 927 | |
| 928 | f2fs_balance_fs(sbi); |
| 929 | |
| 930 | if (f2fs_has_inline_data(inode)) { |
| 931 | ret = f2fs_convert_inline_inode(inode); |
| 932 | if (ret) |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); |
| 937 | if (ret) |
| 938 | return ret; |
| 939 | |
| 940 | truncate_pagecache_range(inode, offset, offset + len - 1); |
| 941 | |
| 942 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 943 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 944 | |
| 945 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 946 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 947 | |
| 948 | if (pg_start == pg_end) { |
| 949 | fill_zero(inode, pg_start, off_start, off_end - off_start); |
| 950 | if (offset + len > new_size) |
| 951 | new_size = offset + len; |
| 952 | new_size = max_t(loff_t, new_size, offset + len); |
| 953 | } else { |
| 954 | if (off_start) { |
| 955 | fill_zero(inode, pg_start++, off_start, |
| 956 | PAGE_CACHE_SIZE - off_start); |
| 957 | new_size = max_t(loff_t, new_size, |
| 958 | pg_start << PAGE_CACHE_SHIFT); |
| 959 | } |
| 960 | |
| 961 | for (index = pg_start; index < pg_end; index++) { |
| 962 | struct dnode_of_data dn; |
| 963 | struct page *ipage; |
| 964 | |
| 965 | f2fs_lock_op(sbi); |
| 966 | |
| 967 | ipage = get_node_page(sbi, inode->i_ino); |
| 968 | if (IS_ERR(ipage)) { |
| 969 | ret = PTR_ERR(ipage); |
| 970 | f2fs_unlock_op(sbi); |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
| 975 | ret = f2fs_reserve_block(&dn, index); |
| 976 | if (ret) { |
| 977 | f2fs_unlock_op(sbi); |
| 978 | goto out; |
| 979 | } |
| 980 | |
| 981 | if (dn.data_blkaddr != NEW_ADDR) { |
| 982 | invalidate_blocks(sbi, dn.data_blkaddr); |
| 983 | |
| 984 | dn.data_blkaddr = NEW_ADDR; |
| 985 | set_data_blkaddr(&dn); |
| 986 | |
| 987 | dn.data_blkaddr = NULL_ADDR; |
| 988 | f2fs_update_extent_cache(&dn); |
| 989 | } |
| 990 | f2fs_put_dnode(&dn); |
| 991 | f2fs_unlock_op(sbi); |
| 992 | |
| 993 | new_size = max_t(loff_t, new_size, |
| 994 | (index + 1) << PAGE_CACHE_SHIFT); |
| 995 | } |
| 996 | |
| 997 | if (off_end) { |
| 998 | fill_zero(inode, pg_end, 0, off_end); |
| 999 | new_size = max_t(loff_t, new_size, offset + len); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | out: |
| 1004 | if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) { |
| 1005 | i_size_write(inode, new_size); |
| 1006 | mark_inode_dirty(inode); |
| 1007 | update_inode_page(inode); |
| 1008 | } |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Chao Yu | f62185d | 2015-05-28 19:16:57 +0800 | [diff] [blame^] | 1013 | static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) |
| 1014 | { |
| 1015 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1016 | pgoff_t pg_start, pg_end, delta, nrpages, idx; |
| 1017 | loff_t new_size; |
| 1018 | int ret; |
| 1019 | |
| 1020 | if (!S_ISREG(inode->i_mode)) |
| 1021 | return -EINVAL; |
| 1022 | |
| 1023 | new_size = i_size_read(inode) + len; |
| 1024 | if (new_size > inode->i_sb->s_maxbytes) |
| 1025 | return -EFBIG; |
| 1026 | |
| 1027 | if (offset >= i_size_read(inode)) |
| 1028 | return -EINVAL; |
| 1029 | |
| 1030 | /* insert range should be aligned to block size of f2fs. */ |
| 1031 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) |
| 1032 | return -EINVAL; |
| 1033 | |
| 1034 | f2fs_balance_fs(sbi); |
| 1035 | |
| 1036 | ret = truncate_blocks(inode, i_size_read(inode), true); |
| 1037 | if (ret) |
| 1038 | return ret; |
| 1039 | |
| 1040 | /* write out all dirty pages from offset */ |
| 1041 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); |
| 1042 | if (ret) |
| 1043 | return ret; |
| 1044 | |
| 1045 | truncate_pagecache(inode, offset); |
| 1046 | |
| 1047 | pg_start = offset >> PAGE_CACHE_SHIFT; |
| 1048 | pg_end = (offset + len) >> PAGE_CACHE_SHIFT; |
| 1049 | delta = pg_end - pg_start; |
| 1050 | nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; |
| 1051 | |
| 1052 | for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) { |
| 1053 | struct dnode_of_data dn; |
| 1054 | struct page *ipage; |
| 1055 | block_t new_addr, old_addr; |
| 1056 | |
| 1057 | f2fs_lock_op(sbi); |
| 1058 | |
| 1059 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1060 | ret = get_dnode_of_data(&dn, idx, LOOKUP_NODE_RA); |
| 1061 | if (ret && ret != -ENOENT) { |
| 1062 | goto out; |
| 1063 | } else if (ret == -ENOENT) { |
| 1064 | goto next; |
| 1065 | } else if (dn.data_blkaddr == NULL_ADDR) { |
| 1066 | f2fs_put_dnode(&dn); |
| 1067 | goto next; |
| 1068 | } else { |
| 1069 | new_addr = dn.data_blkaddr; |
| 1070 | truncate_data_blocks_range(&dn, 1); |
| 1071 | f2fs_put_dnode(&dn); |
| 1072 | } |
| 1073 | |
| 1074 | ipage = get_node_page(sbi, inode->i_ino); |
| 1075 | if (IS_ERR(ipage)) { |
| 1076 | ret = PTR_ERR(ipage); |
| 1077 | goto out; |
| 1078 | } |
| 1079 | |
| 1080 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
| 1081 | ret = f2fs_reserve_block(&dn, idx + delta); |
| 1082 | if (ret) |
| 1083 | goto out; |
| 1084 | |
| 1085 | old_addr = dn.data_blkaddr; |
| 1086 | f2fs_bug_on(sbi, old_addr != NEW_ADDR); |
| 1087 | |
| 1088 | if (new_addr != NEW_ADDR) { |
| 1089 | struct node_info ni; |
| 1090 | |
| 1091 | get_node_info(sbi, dn.nid, &ni); |
| 1092 | f2fs_replace_block(sbi, &dn, old_addr, new_addr, |
| 1093 | ni.version, true); |
| 1094 | } |
| 1095 | f2fs_put_dnode(&dn); |
| 1096 | next: |
| 1097 | f2fs_unlock_op(sbi); |
| 1098 | } |
| 1099 | |
| 1100 | i_size_write(inode, new_size); |
| 1101 | return 0; |
| 1102 | out: |
| 1103 | f2fs_unlock_op(sbi); |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1107 | static int expand_inode_data(struct inode *inode, loff_t offset, |
| 1108 | loff_t len, int mode) |
| 1109 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1110 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1111 | pgoff_t index, pg_start, pg_end; |
| 1112 | loff_t new_size = i_size_read(inode); |
| 1113 | loff_t off_start, off_end; |
| 1114 | int ret = 0; |
| 1115 | |
Chao Yu | 497a093 | 2014-08-04 10:11:17 +0800 | [diff] [blame] | 1116 | f2fs_balance_fs(sbi); |
| 1117 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1118 | ret = inode_newsize_ok(inode, (len + offset)); |
| 1119 | if (ret) |
| 1120 | return ret; |
| 1121 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1122 | if (f2fs_has_inline_data(inode)) { |
| 1123 | ret = f2fs_convert_inline_inode(inode); |
| 1124 | if (ret) |
| 1125 | return ret; |
| 1126 | } |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 1127 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1128 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 1129 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 1130 | |
| 1131 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 1132 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 1133 | |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 1134 | f2fs_lock_op(sbi); |
| 1135 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1136 | for (index = pg_start; index <= pg_end; index++) { |
| 1137 | struct dnode_of_data dn; |
| 1138 | |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 1139 | if (index == pg_end && !off_end) |
| 1140 | goto noalloc; |
| 1141 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1142 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 1143 | ret = f2fs_reserve_block(&dn, index); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 1144 | if (ret) |
| 1145 | break; |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 1146 | noalloc: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1147 | if (pg_start == pg_end) |
| 1148 | new_size = offset + len; |
| 1149 | else if (index == pg_start && off_start) |
| 1150 | new_size = (index + 1) << PAGE_CACHE_SHIFT; |
| 1151 | else if (index == pg_end) |
| 1152 | new_size = (index << PAGE_CACHE_SHIFT) + off_end; |
| 1153 | else |
| 1154 | new_size += PAGE_CACHE_SIZE; |
| 1155 | } |
| 1156 | |
| 1157 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 1158 | i_size_read(inode) < new_size) { |
| 1159 | i_size_write(inode, new_size); |
| 1160 | mark_inode_dirty(inode); |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 1161 | update_inode_page(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1162 | } |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 1163 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1164 | |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static long f2fs_fallocate(struct file *file, int mode, |
| 1169 | loff_t offset, loff_t len) |
| 1170 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1171 | struct inode *inode = file_inode(file); |
Taehee Yoo | 587c0a4 | 2015-04-21 15:59:12 +0900 | [diff] [blame] | 1172 | long ret = 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1173 | |
Chao Yu | f62185d | 2015-05-28 19:16:57 +0800 | [diff] [blame^] | 1174 | if (f2fs_encrypted_inode(inode) && |
| 1175 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 1176 | return -EOPNOTSUPP; |
| 1177 | |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 1178 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Chao Yu | f62185d | 2015-05-28 19:16:57 +0800 | [diff] [blame^] | 1179 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 1180 | FALLOC_FL_INSERT_RANGE)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1181 | return -EOPNOTSUPP; |
| 1182 | |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 1183 | mutex_lock(&inode->i_mutex); |
| 1184 | |
Taehee Yoo | 587c0a4 | 2015-04-21 15:59:12 +0900 | [diff] [blame] | 1185 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 1186 | if (offset >= inode->i_size) |
| 1187 | goto out; |
| 1188 | |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 1189 | ret = punch_hole(inode, offset, len); |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 1190 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
| 1191 | ret = f2fs_collapse_range(inode, offset, len); |
Chao Yu | 75cd4e0 | 2015-05-06 13:11:13 +0800 | [diff] [blame] | 1192 | } else if (mode & FALLOC_FL_ZERO_RANGE) { |
| 1193 | ret = f2fs_zero_range(inode, offset, len, mode); |
Chao Yu | f62185d | 2015-05-28 19:16:57 +0800 | [diff] [blame^] | 1194 | } else if (mode & FALLOC_FL_INSERT_RANGE) { |
| 1195 | ret = f2fs_insert_range(inode, offset, len); |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 1196 | } else { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1197 | ret = expand_inode_data(inode, offset, len, mode); |
Chao Yu | b4ace33 | 2015-05-06 13:09:46 +0800 | [diff] [blame] | 1198 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1199 | |
Namjae Jeon | 3af60a4 | 2012-12-30 14:52:37 +0900 | [diff] [blame] | 1200 | if (!ret) { |
| 1201 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 1202 | mark_inode_dirty(inode); |
| 1203 | } |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 1204 | |
Taehee Yoo | 587c0a4 | 2015-04-21 15:59:12 +0900 | [diff] [blame] | 1205 | out: |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 1206 | mutex_unlock(&inode->i_mutex); |
| 1207 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 1208 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1209 | return ret; |
| 1210 | } |
| 1211 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1212 | static int f2fs_release_file(struct inode *inode, struct file *filp) |
| 1213 | { |
| 1214 | /* some remained atomic pages should discarded */ |
| 1215 | if (f2fs_is_atomic_file(inode)) |
| 1216 | commit_inmem_pages(inode, true); |
| 1217 | if (f2fs_is_volatile_file(inode)) { |
| 1218 | set_inode_flag(F2FS_I(inode), FI_DROP_CACHE); |
| 1219 | filemap_fdatawrite(inode->i_mapping); |
| 1220 | clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE); |
| 1221 | } |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1225 | #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) |
| 1226 | #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) |
| 1227 | |
| 1228 | static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) |
| 1229 | { |
| 1230 | if (S_ISDIR(mode)) |
| 1231 | return flags; |
| 1232 | else if (S_ISREG(mode)) |
| 1233 | return flags & F2FS_REG_FLMASK; |
| 1234 | else |
| 1235 | return flags & F2FS_OTHER_FLMASK; |
| 1236 | } |
| 1237 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1238 | static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1239 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1240 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1241 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1242 | unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 1243 | return put_user(flags, (int __user *)arg); |
| 1244 | } |
| 1245 | |
| 1246 | static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) |
| 1247 | { |
| 1248 | struct inode *inode = file_inode(filp); |
| 1249 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 1250 | unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 1251 | unsigned int oldflags; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1252 | int ret; |
| 1253 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1254 | ret = mnt_want_write_file(filp); |
| 1255 | if (ret) |
| 1256 | return ret; |
| 1257 | |
| 1258 | if (!inode_owner_or_capable(inode)) { |
| 1259 | ret = -EACCES; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | |
| 1263 | if (get_user(flags, (int __user *)arg)) { |
| 1264 | ret = -EFAULT; |
| 1265 | goto out; |
| 1266 | } |
| 1267 | |
| 1268 | flags = f2fs_mask_flags(inode->i_mode, flags); |
| 1269 | |
| 1270 | mutex_lock(&inode->i_mutex); |
| 1271 | |
| 1272 | oldflags = fi->i_flags; |
| 1273 | |
| 1274 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 1275 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 1276 | mutex_unlock(&inode->i_mutex); |
| 1277 | ret = -EPERM; |
| 1278 | goto out; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | flags = flags & FS_FL_USER_MODIFIABLE; |
| 1283 | flags |= oldflags & ~FS_FL_USER_MODIFIABLE; |
| 1284 | fi->i_flags = flags; |
| 1285 | mutex_unlock(&inode->i_mutex); |
| 1286 | |
| 1287 | f2fs_set_inode_flags(inode); |
| 1288 | inode->i_ctime = CURRENT_TIME; |
| 1289 | mark_inode_dirty(inode); |
| 1290 | out: |
| 1291 | mnt_drop_write_file(filp); |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
Chao Yu | d49f3e8 | 2015-01-23 20:36:04 +0800 | [diff] [blame] | 1295 | static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) |
| 1296 | { |
| 1297 | struct inode *inode = file_inode(filp); |
| 1298 | |
| 1299 | return put_user(inode->i_generation, (int __user *)arg); |
| 1300 | } |
| 1301 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1302 | static int f2fs_ioc_start_atomic_write(struct file *filp) |
| 1303 | { |
| 1304 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1305 | |
| 1306 | if (!inode_owner_or_capable(inode)) |
| 1307 | return -EACCES; |
| 1308 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1309 | f2fs_balance_fs(F2FS_I_SB(inode)); |
| 1310 | |
| 1311 | if (f2fs_is_atomic_file(inode)) |
| 1312 | return 0; |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1313 | |
| 1314 | set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
| 1315 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1316 | return f2fs_convert_inline_inode(inode); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | static int f2fs_ioc_commit_atomic_write(struct file *filp) |
| 1320 | { |
| 1321 | struct inode *inode = file_inode(filp); |
| 1322 | int ret; |
| 1323 | |
| 1324 | if (!inode_owner_or_capable(inode)) |
| 1325 | return -EACCES; |
| 1326 | |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 1327 | if (f2fs_is_volatile_file(inode)) |
| 1328 | return 0; |
| 1329 | |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1330 | ret = mnt_want_write_file(filp); |
| 1331 | if (ret) |
| 1332 | return ret; |
| 1333 | |
| 1334 | if (f2fs_is_atomic_file(inode)) |
| 1335 | commit_inmem_pages(inode, false); |
| 1336 | |
| 1337 | ret = f2fs_sync_file(filp, 0, LONG_MAX, 0); |
| 1338 | mnt_drop_write_file(filp); |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1339 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1340 | return ret; |
| 1341 | } |
| 1342 | |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 1343 | static int f2fs_ioc_start_volatile_write(struct file *filp) |
| 1344 | { |
| 1345 | struct inode *inode = file_inode(filp); |
| 1346 | |
| 1347 | if (!inode_owner_or_capable(inode)) |
| 1348 | return -EACCES; |
| 1349 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1350 | if (f2fs_is_volatile_file(inode)) |
| 1351 | return 0; |
| 1352 | |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 1353 | set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1354 | |
| 1355 | return f2fs_convert_inline_inode(inode); |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1358 | static int f2fs_ioc_release_volatile_write(struct file *filp) |
| 1359 | { |
| 1360 | struct inode *inode = file_inode(filp); |
| 1361 | |
| 1362 | if (!inode_owner_or_capable(inode)) |
| 1363 | return -EACCES; |
| 1364 | |
| 1365 | if (!f2fs_is_volatile_file(inode)) |
| 1366 | return 0; |
| 1367 | |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 1368 | if (!f2fs_is_first_block_written(inode)) |
| 1369 | return truncate_partial_data_page(inode, 0, true); |
| 1370 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1371 | punch_hole(inode, 0, F2FS_BLKSIZE); |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | static int f2fs_ioc_abort_volatile_write(struct file *filp) |
| 1376 | { |
| 1377 | struct inode *inode = file_inode(filp); |
| 1378 | int ret; |
| 1379 | |
| 1380 | if (!inode_owner_or_capable(inode)) |
| 1381 | return -EACCES; |
| 1382 | |
| 1383 | ret = mnt_want_write_file(filp); |
| 1384 | if (ret) |
| 1385 | return ret; |
| 1386 | |
| 1387 | f2fs_balance_fs(F2FS_I_SB(inode)); |
| 1388 | |
| 1389 | if (f2fs_is_atomic_file(inode)) { |
| 1390 | commit_inmem_pages(inode, false); |
| 1391 | clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE); |
| 1392 | } |
| 1393 | |
| 1394 | if (f2fs_is_volatile_file(inode)) { |
| 1395 | clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); |
| 1396 | filemap_fdatawrite(inode->i_mapping); |
| 1397 | set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE); |
| 1398 | } |
| 1399 | mnt_drop_write_file(filp); |
| 1400 | return ret; |
| 1401 | } |
| 1402 | |
Jaegeuk Kim | 1abff93 | 2015-01-08 19:15:53 -0800 | [diff] [blame] | 1403 | static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) |
| 1404 | { |
| 1405 | struct inode *inode = file_inode(filp); |
| 1406 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1407 | struct super_block *sb = sbi->sb; |
| 1408 | __u32 in; |
| 1409 | |
| 1410 | if (!capable(CAP_SYS_ADMIN)) |
| 1411 | return -EPERM; |
| 1412 | |
| 1413 | if (get_user(in, (__u32 __user *)arg)) |
| 1414 | return -EFAULT; |
| 1415 | |
| 1416 | switch (in) { |
| 1417 | case F2FS_GOING_DOWN_FULLSYNC: |
| 1418 | sb = freeze_bdev(sb->s_bdev); |
| 1419 | if (sb && !IS_ERR(sb)) { |
| 1420 | f2fs_stop_checkpoint(sbi); |
| 1421 | thaw_bdev(sb->s_bdev, sb); |
| 1422 | } |
| 1423 | break; |
| 1424 | case F2FS_GOING_DOWN_METASYNC: |
| 1425 | /* do checkpoint only */ |
| 1426 | f2fs_sync_fs(sb, 1); |
| 1427 | f2fs_stop_checkpoint(sbi); |
| 1428 | break; |
| 1429 | case F2FS_GOING_DOWN_NOSYNC: |
| 1430 | f2fs_stop_checkpoint(sbi); |
| 1431 | break; |
| 1432 | default: |
| 1433 | return -EINVAL; |
| 1434 | } |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1438 | static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) |
| 1439 | { |
| 1440 | struct inode *inode = file_inode(filp); |
| 1441 | struct super_block *sb = inode->i_sb; |
| 1442 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
| 1443 | struct fstrim_range range; |
| 1444 | int ret; |
| 1445 | |
| 1446 | if (!capable(CAP_SYS_ADMIN)) |
| 1447 | return -EPERM; |
| 1448 | |
| 1449 | if (!blk_queue_discard(q)) |
| 1450 | return -EOPNOTSUPP; |
| 1451 | |
| 1452 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
| 1453 | sizeof(range))) |
| 1454 | return -EFAULT; |
| 1455 | |
| 1456 | range.minlen = max((unsigned int)range.minlen, |
| 1457 | q->limits.discard_granularity); |
| 1458 | ret = f2fs_trim_fs(F2FS_SB(sb), &range); |
| 1459 | if (ret < 0) |
| 1460 | return ret; |
| 1461 | |
| 1462 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
| 1463 | sizeof(range))) |
| 1464 | return -EFAULT; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Jaegeuk Kim | f424f66 | 2015-04-20 15:19:06 -0700 | [diff] [blame] | 1468 | static bool uuid_is_nonzero(__u8 u[16]) |
| 1469 | { |
| 1470 | int i; |
| 1471 | |
| 1472 | for (i = 0; i < 16; i++) |
| 1473 | if (u[i]) |
| 1474 | return true; |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
| 1478 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) |
| 1479 | { |
| 1480 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 1481 | struct f2fs_encryption_policy policy; |
| 1482 | struct inode *inode = file_inode(filp); |
| 1483 | |
| 1484 | if (copy_from_user(&policy, (struct f2fs_encryption_policy __user *)arg, |
| 1485 | sizeof(policy))) |
| 1486 | return -EFAULT; |
| 1487 | |
Jaegeuk Kim | f424f66 | 2015-04-20 15:19:06 -0700 | [diff] [blame] | 1488 | return f2fs_process_policy(&policy, inode); |
| 1489 | #else |
| 1490 | return -EOPNOTSUPP; |
| 1491 | #endif |
| 1492 | } |
| 1493 | |
| 1494 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) |
| 1495 | { |
| 1496 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 1497 | struct f2fs_encryption_policy policy; |
| 1498 | struct inode *inode = file_inode(filp); |
| 1499 | int err; |
| 1500 | |
| 1501 | err = f2fs_get_policy(inode, &policy); |
| 1502 | if (err) |
| 1503 | return err; |
| 1504 | |
| 1505 | if (copy_to_user((struct f2fs_encryption_policy __user *)arg, &policy, |
| 1506 | sizeof(policy))) |
| 1507 | return -EFAULT; |
| 1508 | return 0; |
| 1509 | #else |
| 1510 | return -EOPNOTSUPP; |
| 1511 | #endif |
| 1512 | } |
| 1513 | |
| 1514 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) |
| 1515 | { |
| 1516 | struct inode *inode = file_inode(filp); |
| 1517 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1518 | int err; |
| 1519 | |
| 1520 | if (!f2fs_sb_has_crypto(inode->i_sb)) |
| 1521 | return -EOPNOTSUPP; |
| 1522 | |
| 1523 | if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) |
| 1524 | goto got_it; |
| 1525 | |
| 1526 | err = mnt_want_write_file(filp); |
| 1527 | if (err) |
| 1528 | return err; |
| 1529 | |
| 1530 | /* update superblock with uuid */ |
| 1531 | generate_random_uuid(sbi->raw_super->encrypt_pw_salt); |
| 1532 | |
| 1533 | err = f2fs_commit_super(sbi); |
| 1534 | |
| 1535 | mnt_drop_write_file(filp); |
| 1536 | if (err) { |
| 1537 | /* undo new data */ |
| 1538 | memset(sbi->raw_super->encrypt_pw_salt, 0, 16); |
| 1539 | return err; |
| 1540 | } |
| 1541 | got_it: |
| 1542 | if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, |
| 1543 | 16)) |
| 1544 | return -EFAULT; |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1548 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1549 | { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1550 | switch (cmd) { |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 1551 | case F2FS_IOC_GETFLAGS: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1552 | return f2fs_ioc_getflags(filp, arg); |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 1553 | case F2FS_IOC_SETFLAGS: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1554 | return f2fs_ioc_setflags(filp, arg); |
Chao Yu | d49f3e8 | 2015-01-23 20:36:04 +0800 | [diff] [blame] | 1555 | case F2FS_IOC_GETVERSION: |
| 1556 | return f2fs_ioc_getversion(filp, arg); |
Jaegeuk Kim | 88b88a6 | 2014-10-06 17:39:50 -0700 | [diff] [blame] | 1557 | case F2FS_IOC_START_ATOMIC_WRITE: |
| 1558 | return f2fs_ioc_start_atomic_write(filp); |
| 1559 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: |
| 1560 | return f2fs_ioc_commit_atomic_write(filp); |
Jaegeuk Kim | 02a1335 | 2014-10-06 16:11:16 -0700 | [diff] [blame] | 1561 | case F2FS_IOC_START_VOLATILE_WRITE: |
| 1562 | return f2fs_ioc_start_volatile_write(filp); |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1563 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: |
| 1564 | return f2fs_ioc_release_volatile_write(filp); |
| 1565 | case F2FS_IOC_ABORT_VOLATILE_WRITE: |
| 1566 | return f2fs_ioc_abort_volatile_write(filp); |
Jaegeuk Kim | 1abff93 | 2015-01-08 19:15:53 -0800 | [diff] [blame] | 1567 | case F2FS_IOC_SHUTDOWN: |
| 1568 | return f2fs_ioc_shutdown(filp, arg); |
Jaegeuk Kim | 4b2fecc | 2014-09-20 22:06:39 -0700 | [diff] [blame] | 1569 | case FITRIM: |
Jaegeuk Kim | 52656e6 | 2014-09-24 15:37:02 -0700 | [diff] [blame] | 1570 | return f2fs_ioc_fitrim(filp, arg); |
Jaegeuk Kim | f424f66 | 2015-04-20 15:19:06 -0700 | [diff] [blame] | 1571 | case F2FS_IOC_SET_ENCRYPTION_POLICY: |
| 1572 | return f2fs_ioc_set_encryption_policy(filp, arg); |
| 1573 | case F2FS_IOC_GET_ENCRYPTION_POLICY: |
| 1574 | return f2fs_ioc_get_encryption_policy(filp, arg); |
| 1575 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: |
| 1576 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1577 | default: |
| 1578 | return -ENOTTY; |
| 1579 | } |
| 1580 | } |
| 1581 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 1582 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 1583 | { |
| 1584 | struct inode *inode = file_inode(iocb->ki_filp); |
| 1585 | |
| 1586 | if (f2fs_encrypted_inode(inode) && |
| 1587 | !f2fs_has_encryption_key(inode) && |
| 1588 | f2fs_get_encryption_info(inode)) |
| 1589 | return -EACCES; |
| 1590 | |
| 1591 | return generic_file_write_iter(iocb, from); |
| 1592 | } |
| 1593 | |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 1594 | #ifdef CONFIG_COMPAT |
| 1595 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1596 | { |
| 1597 | switch (cmd) { |
| 1598 | case F2FS_IOC32_GETFLAGS: |
| 1599 | cmd = F2FS_IOC_GETFLAGS; |
| 1600 | break; |
| 1601 | case F2FS_IOC32_SETFLAGS: |
| 1602 | cmd = F2FS_IOC_SETFLAGS; |
| 1603 | break; |
| 1604 | default: |
| 1605 | return -ENOIOCTLCMD; |
| 1606 | } |
| 1607 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1611 | const struct file_operations f2fs_file_operations = { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 1612 | .llseek = f2fs_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 1613 | .read_iter = generic_file_read_iter, |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 1614 | .write_iter = f2fs_file_write_iter, |
| 1615 | .open = f2fs_file_open, |
Jaegeuk Kim | 1266223 | 2014-12-05 14:37:37 -0800 | [diff] [blame] | 1616 | .release = f2fs_release_file, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1617 | .mmap = f2fs_file_mmap, |
| 1618 | .fsync = f2fs_sync_file, |
| 1619 | .fallocate = f2fs_fallocate, |
| 1620 | .unlocked_ioctl = f2fs_ioctl, |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 1621 | #ifdef CONFIG_COMPAT |
| 1622 | .compat_ioctl = f2fs_compat_ioctl, |
| 1623 | #endif |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1624 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 1625 | .splice_write = iter_file_splice_write, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 1626 | }; |