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