Commit | Line | Data |
---|---|---|
7c1a000d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0a8165d7 | 2 | /* |
fbfa2cc5 JK |
3 | * fs/f2fs/file.c |
4 | * | |
5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com/ | |
fbfa2cc5 JK |
7 | */ |
8 | #include <linux/fs.h> | |
9 | #include <linux/f2fs_fs.h> | |
10 | #include <linux/stat.h> | |
11 | #include <linux/buffer_head.h> | |
12 | #include <linux/writeback.h> | |
ae51fb31 | 13 | #include <linux/blkdev.h> |
fbfa2cc5 JK |
14 | #include <linux/falloc.h> |
15 | #include <linux/types.h> | |
e9750824 | 16 | #include <linux/compat.h> |
fbfa2cc5 JK |
17 | #include <linux/uaccess.h> |
18 | #include <linux/mount.h> | |
7f7670fe | 19 | #include <linux/pagevec.h> |
dc91de78 | 20 | #include <linux/uio.h> |
8da4b8c4 | 21 | #include <linux/uuid.h> |
4dd6f977 | 22 | #include <linux/file.h> |
4507847c | 23 | #include <linux/nls.h> |
9af84648 | 24 | #include <linux/sched/signal.h> |
9b1bb01c | 25 | #include <linux/fileattr.h> |
0f6b56ec | 26 | #include <linux/fadvise.h> |
a1e09b03 | 27 | #include <linux/iomap.h> |
fbfa2cc5 JK |
28 | |
29 | #include "f2fs.h" | |
30 | #include "node.h" | |
31 | #include "segment.h" | |
32 | #include "xattr.h" | |
33 | #include "acl.h" | |
c1c1b583 | 34 | #include "gc.h" |
52118743 | 35 | #include "iostat.h" |
a2a4a7e4 | 36 | #include <trace/events/f2fs.h> |
fa4320ce | 37 | #include <uapi/linux/f2fs.h> |
fbfa2cc5 | 38 | |
ea4d479b | 39 | static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf) |
5a3a2d83 QS |
40 | { |
41 | struct inode *inode = file_inode(vmf->vma->vm_file); | |
ea4d479b | 42 | vm_fault_t ret; |
5a3a2d83 | 43 | |
ea4d479b | 44 | ret = filemap_fault(vmf); |
8b83ac81 CY |
45 | if (!ret) |
46 | f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO, | |
47 | F2FS_BLKSIZE); | |
48 | ||
d7648343 CY |
49 | trace_f2fs_filemap_fault(inode, vmf->pgoff, (unsigned long)ret); |
50 | ||
ea4d479b | 51 | return ret; |
5a3a2d83 QS |
52 | } |
53 | ||
ea4d479b | 54 | static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) |
fbfa2cc5 JK |
55 | { |
56 | struct page *page = vmf->page; | |
11bac800 | 57 | struct inode *inode = file_inode(vmf->vma->vm_file); |
4081363f | 58 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
bdf03299 | 59 | struct dnode_of_data dn; |
4c8ff709 CY |
60 | bool need_alloc = true; |
61 | int err = 0; | |
fbfa2cc5 | 62 | |
e0fcd015 CY |
63 | if (unlikely(IS_IMMUTABLE(inode))) |
64 | return VM_FAULT_SIGBUS; | |
65 | ||
c6140415 JK |
66 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) |
67 | return VM_FAULT_SIGBUS; | |
68 | ||
1f227a3e JK |
69 | if (unlikely(f2fs_cp_error(sbi))) { |
70 | err = -EIO; | |
71 | goto err; | |
72 | } | |
73 | ||
00e09c0b CY |
74 | if (!f2fs_is_checkpoint_ready(sbi)) { |
75 | err = -ENOSPC; | |
955ebcd3 | 76 | goto err; |
00e09c0b | 77 | } |
1f227a3e | 78 | |
c8e43d55 CY |
79 | err = f2fs_convert_inline_inode(inode); |
80 | if (err) | |
81 | goto err; | |
82 | ||
4c8ff709 CY |
83 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
84 | if (f2fs_compressed_file(inode)) { | |
85 | int ret = f2fs_is_compressed_cluster(inode, page->index); | |
86 | ||
87 | if (ret < 0) { | |
88 | err = ret; | |
89 | goto err; | |
90 | } else if (ret) { | |
4c8ff709 CY |
91 | need_alloc = false; |
92 | } | |
93 | } | |
94 | #endif | |
bdf03299 | 95 | /* should do out of any locked page */ |
4c8ff709 CY |
96 | if (need_alloc) |
97 | f2fs_balance_fs(sbi, true); | |
bdf03299 | 98 | |
fbfa2cc5 | 99 | sb_start_pagefault(inode->i_sb); |
b3d208f9 JK |
100 | |
101 | f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); | |
b067ba1f | 102 | |
11bac800 | 103 | file_update_time(vmf->vma->vm_file); |
edc6d01b | 104 | filemap_invalidate_lock_shared(inode->i_mapping); |
fbfa2cc5 | 105 | lock_page(page); |
6bacf52f | 106 | if (unlikely(page->mapping != inode->i_mapping || |
9851e6e1 | 107 | page_offset(page) > i_size_read(inode) || |
6bacf52f | 108 | !PageUptodate(page))) { |
fbfa2cc5 JK |
109 | unlock_page(page); |
110 | err = -EFAULT; | |
5a3a2d83 | 111 | goto out_sem; |
fbfa2cc5 JK |
112 | } |
113 | ||
4c8ff709 CY |
114 | if (need_alloc) { |
115 | /* block allocation */ | |
0ef81833 | 116 | f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); |
4c8ff709 CY |
117 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
118 | err = f2fs_get_block(&dn, page->index); | |
0ef81833 | 119 | f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); |
39a86958 CY |
120 | } |
121 | ||
06c7540f CY |
122 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
123 | if (!need_alloc) { | |
124 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
125 | err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); | |
126 | f2fs_put_dnode(&dn); | |
127 | } | |
128 | #endif | |
129 | if (err) { | |
130 | unlock_page(page); | |
131 | goto out_sem; | |
39a86958 CY |
132 | } |
133 | ||
bae0ee7a | 134 | f2fs_wait_on_page_writeback(page, DATA, false, true); |
39a86958 CY |
135 | |
136 | /* wait for GCed page writeback via META_MAPPING */ | |
137 | f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); | |
138 | ||
fbfa2cc5 JK |
139 | /* |
140 | * check to see if the page is mapped already (no holes) | |
141 | */ | |
142 | if (PageMappedToDisk(page)) | |
39a86958 | 143 | goto out_sem; |
fbfa2cc5 JK |
144 | |
145 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 146 | if (((loff_t)(page->index + 1) << PAGE_SHIFT) > |
9edcdabf | 147 | i_size_read(inode)) { |
193bea1d | 148 | loff_t offset; |
f11e98bd | 149 | |
09cbfeaf KS |
150 | offset = i_size_read(inode) & ~PAGE_MASK; |
151 | zero_user_segment(page, offset, PAGE_SIZE); | |
fbfa2cc5 JK |
152 | } |
153 | set_page_dirty(page); | |
237c0790 JK |
154 | if (!PageUptodate(page)) |
155 | SetPageUptodate(page); | |
fbfa2cc5 | 156 | |
b0af6d49 | 157 | f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE); |
c75f2feb | 158 | f2fs_update_time(sbi, REQ_TIME); |
b0af6d49 | 159 | |
e943a10d | 160 | trace_f2fs_vm_page_mkwrite(page, DATA); |
5a3a2d83 | 161 | out_sem: |
edc6d01b | 162 | filemap_invalidate_unlock_shared(inode->i_mapping); |
39a86958 | 163 | |
fbfa2cc5 | 164 | sb_end_pagefault(inode->i_sb); |
1f227a3e | 165 | err: |
fbfa2cc5 JK |
166 | return block_page_mkwrite_return(err); |
167 | } | |
168 | ||
169 | static const struct vm_operations_struct f2fs_file_vm_ops = { | |
5a3a2d83 | 170 | .fault = f2fs_filemap_fault, |
f1820361 | 171 | .map_pages = filemap_map_pages, |
692bb55d | 172 | .page_mkwrite = f2fs_vm_page_mkwrite, |
fbfa2cc5 JK |
173 | }; |
174 | ||
354a3399 JK |
175 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
176 | { | |
177 | struct dentry *dentry; | |
178 | ||
84c9c2de EB |
179 | /* |
180 | * Make sure to get the non-deleted alias. The alias associated with | |
181 | * the open file descriptor being fsync()'ed may be deleted already. | |
182 | */ | |
183 | dentry = d_find_alias(inode); | |
354a3399 JK |
184 | if (!dentry) |
185 | return 0; | |
186 | ||
f0947e5c JK |
187 | *pino = parent_ino(dentry); |
188 | dput(dentry); | |
354a3399 JK |
189 | return 1; |
190 | } | |
191 | ||
a5fd5050 | 192 | static inline enum cp_reason_type need_do_checkpoint(struct inode *inode) |
9d1589ef | 193 | { |
4081363f | 194 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
a5fd5050 | 195 | enum cp_reason_type cp_reason = CP_NO_NEEDED; |
9d1589ef | 196 | |
a5fd5050 CY |
197 | if (!S_ISREG(inode->i_mode)) |
198 | cp_reason = CP_NON_REGULAR; | |
4c8ff709 CY |
199 | else if (f2fs_compressed_file(inode)) |
200 | cp_reason = CP_COMPRESSED; | |
a5fd5050 CY |
201 | else if (inode->i_nlink != 1) |
202 | cp_reason = CP_HARDLINK; | |
bbf156f7 | 203 | else if (is_sbi_flag_set(sbi, SBI_NEED_CP)) |
a5fd5050 | 204 | cp_reason = CP_SB_NEED_CP; |
9d1589ef | 205 | else if (file_wrong_pino(inode)) |
a5fd5050 | 206 | cp_reason = CP_WRONG_PINO; |
4d57b86d | 207 | else if (!f2fs_space_for_roll_forward(sbi)) |
a5fd5050 | 208 | cp_reason = CP_NO_SPC_ROLL; |
4d57b86d | 209 | else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
a5fd5050 | 210 | cp_reason = CP_NODE_NEED_CP; |
d5053a34 | 211 | else if (test_opt(sbi, FASTBOOT)) |
a5fd5050 | 212 | cp_reason = CP_FASTBOOT_MODE; |
63189b78 | 213 | else if (F2FS_OPTION(sbi).active_logs == 2) |
a5fd5050 | 214 | cp_reason = CP_SPEC_LOG_NUM; |
63189b78 | 215 | else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT && |
4d57b86d CY |
216 | f2fs_need_dentry_mark(sbi, inode->i_ino) && |
217 | f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino, | |
218 | TRANS_DIR_INO)) | |
0a007b97 | 219 | cp_reason = CP_RECOVER_DIR; |
9d1589ef | 220 | |
a5fd5050 | 221 | return cp_reason; |
9d1589ef CY |
222 | } |
223 | ||
9c7bb702 CL |
224 | static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) |
225 | { | |
226 | struct page *i = find_get_page(NODE_MAPPING(sbi), ino); | |
227 | bool ret = false; | |
228 | /* But we need to avoid that there are some inode updates */ | |
4d57b86d | 229 | if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino)) |
9c7bb702 CL |
230 | ret = true; |
231 | f2fs_put_page(i, 0); | |
232 | return ret; | |
233 | } | |
234 | ||
51455b19 CL |
235 | static void try_to_fix_pino(struct inode *inode) |
236 | { | |
237 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
238 | nid_t pino; | |
239 | ||
e4544b63 | 240 | f2fs_down_write(&fi->i_sem); |
51455b19 CL |
241 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
242 | get_parent_ino(inode, &pino)) { | |
205b9822 | 243 | f2fs_i_pino_write(inode, pino); |
51455b19 | 244 | file_got_pino(inode); |
51455b19 | 245 | } |
e4544b63 | 246 | f2fs_up_write(&fi->i_sem); |
51455b19 CL |
247 | } |
248 | ||
608514de JK |
249 | static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end, |
250 | int datasync, bool atomic) | |
fbfa2cc5 JK |
251 | { |
252 | struct inode *inode = file->f_mapping->host; | |
4081363f | 253 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
2403c155 | 254 | nid_t ino = inode->i_ino; |
fbfa2cc5 | 255 | int ret = 0; |
a5fd5050 | 256 | enum cp_reason_type cp_reason = 0; |
fbfa2cc5 | 257 | struct writeback_control wbc = { |
c81bf1c8 | 258 | .sync_mode = WB_SYNC_ALL, |
fbfa2cc5 JK |
259 | .nr_to_write = LONG_MAX, |
260 | .for_reclaim = 0, | |
261 | }; | |
50fa53ec | 262 | unsigned int seq_id = 0; |
fbfa2cc5 | 263 | |
dddd3d65 | 264 | if (unlikely(f2fs_readonly(inode->i_sb))) |
1fa95b0b NJ |
265 | return 0; |
266 | ||
a2a4a7e4 | 267 | trace_f2fs_sync_file_enter(inode); |
ea1aa12c | 268 | |
b61ac5b7 YH |
269 | if (S_ISDIR(inode->i_mode)) |
270 | goto go_write; | |
271 | ||
ea1aa12c | 272 | /* if fdatasync is triggered, let's do in-place-update */ |
c46a155b | 273 | if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) |
91942321 | 274 | set_inode_flag(inode, FI_NEED_IPU); |
3b49c9a1 | 275 | ret = file_write_and_wait_range(file, start, end); |
91942321 | 276 | clear_inode_flag(inode, FI_NEED_IPU); |
c1ce1b02 | 277 | |
dddd3d65 | 278 | if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) { |
a5fd5050 | 279 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); |
fbfa2cc5 | 280 | return ret; |
a2a4a7e4 | 281 | } |
fbfa2cc5 | 282 | |
9c7bb702 | 283 | /* if the inode is dirty, let's recover all the time */ |
281518c6 | 284 | if (!f2fs_skip_inode_update(inode, datasync)) { |
2286c020 | 285 | f2fs_write_inode(inode, NULL); |
9c7bb702 CL |
286 | goto go_write; |
287 | } | |
288 | ||
6d99ba41 JK |
289 | /* |
290 | * if there is no written data, don't waste time to write recovery info. | |
291 | */ | |
91942321 | 292 | if (!is_inode_flag_set(inode, FI_APPEND_WRITE) && |
4d57b86d | 293 | !f2fs_exist_written_data(sbi, ino, APPEND_INO)) { |
19c9c466 | 294 | |
9c7bb702 CL |
295 | /* it may call write_inode just prior to fsync */ |
296 | if (need_inode_page_update(sbi, ino)) | |
19c9c466 | 297 | goto go_write; |
19c9c466 | 298 | |
91942321 | 299 | if (is_inode_flag_set(inode, FI_UPDATE_WRITE) || |
4d57b86d | 300 | f2fs_exist_written_data(sbi, ino, UPDATE_INO)) |
6d99ba41 JK |
301 | goto flush_out; |
302 | goto out; | |
27879915 CY |
303 | } else { |
304 | /* | |
305 | * for OPU case, during fsync(), node can be persisted before | |
306 | * data when lower device doesn't support write barrier, result | |
307 | * in data corruption after SPO. | |
308 | * So for strict fsync mode, force to use atomic write sematics | |
309 | * to keep write order in between data/node and last node to | |
310 | * avoid potential data corruption. | |
311 | */ | |
312 | if (F2FS_OPTION(sbi).fsync_mode == | |
313 | FSYNC_MODE_STRICT && !atomic) | |
314 | atomic = true; | |
6d99ba41 | 315 | } |
19c9c466 | 316 | go_write: |
e5d2385e JK |
317 | /* |
318 | * Both of fdatasync() and fsync() are able to be recovered from | |
319 | * sudden-power-off. | |
320 | */ | |
e4544b63 | 321 | f2fs_down_read(&F2FS_I(inode)->i_sem); |
a5fd5050 | 322 | cp_reason = need_do_checkpoint(inode); |
e4544b63 | 323 | f2fs_up_read(&F2FS_I(inode)->i_sem); |
d928bfbf | 324 | |
a5fd5050 | 325 | if (cp_reason) { |
fbfa2cc5 JK |
326 | /* all the dirty node pages should be flushed for POR */ |
327 | ret = f2fs_sync_fs(inode->i_sb, 1); | |
d928bfbf | 328 | |
51455b19 CL |
329 | /* |
330 | * We've secured consistency through sync_fs. Following pino | |
331 | * will be used only for fsynced inodes after checkpoint. | |
332 | */ | |
333 | try_to_fix_pino(inode); | |
91942321 JK |
334 | clear_inode_flag(inode, FI_APPEND_WRITE); |
335 | clear_inode_flag(inode, FI_UPDATE_WRITE); | |
51455b19 CL |
336 | goto out; |
337 | } | |
88bd02c9 | 338 | sync_nodes: |
c29fd0c0 | 339 | atomic_inc(&sbi->wb_sync_req[NODE]); |
50fa53ec | 340 | ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id); |
c29fd0c0 | 341 | atomic_dec(&sbi->wb_sync_req[NODE]); |
c267ec15 JK |
342 | if (ret) |
343 | goto out; | |
51455b19 | 344 | |
871f599f | 345 | /* if cp_error was enabled, we should avoid infinite loop */ |
6d5a1495 CY |
346 | if (unlikely(f2fs_cp_error(sbi))) { |
347 | ret = -EIO; | |
871f599f | 348 | goto out; |
6d5a1495 | 349 | } |
871f599f | 350 | |
4d57b86d | 351 | if (f2fs_need_inode_block_update(sbi, ino)) { |
7c45729a | 352 | f2fs_mark_inode_dirty_sync(inode, true); |
51455b19 CL |
353 | f2fs_write_inode(inode, NULL); |
354 | goto sync_nodes; | |
fbfa2cc5 | 355 | } |
51455b19 | 356 | |
b6a245eb JK |
357 | /* |
358 | * If it's atomic_write, it's just fine to keep write ordering. So | |
359 | * here we don't need to wait for node write completion, since we use | |
360 | * node chain which serializes node blocks. If one of node writes are | |
361 | * reordered, we can see simply broken chain, resulting in stopping | |
362 | * roll-forward recovery. It means we'll recover all or none node blocks | |
363 | * given fsync mark. | |
364 | */ | |
365 | if (!atomic) { | |
50fa53ec | 366 | ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id); |
b6a245eb JK |
367 | if (ret) |
368 | goto out; | |
369 | } | |
51455b19 CL |
370 | |
371 | /* once recovery info is written, don't need to tack this */ | |
4d57b86d | 372 | f2fs_remove_ino_entry(sbi, ino, APPEND_INO); |
91942321 | 373 | clear_inode_flag(inode, FI_APPEND_WRITE); |
51455b19 | 374 | flush_out: |
c550e25b JK |
375 | if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) || |
376 | (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi))) | |
39d787be | 377 | ret = f2fs_issue_flush(sbi, inode->i_ino); |
3f06252f | 378 | if (!ret) { |
4d57b86d | 379 | f2fs_remove_ino_entry(sbi, ino, UPDATE_INO); |
3f06252f | 380 | clear_inode_flag(inode, FI_UPDATE_WRITE); |
4d57b86d | 381 | f2fs_remove_ino_entry(sbi, ino, FLUSH_INO); |
3f06252f | 382 | } |
d0239e1b | 383 | f2fs_update_time(sbi, REQ_TIME); |
fbfa2cc5 | 384 | out: |
a5fd5050 | 385 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); |
fbfa2cc5 JK |
386 | return ret; |
387 | } | |
388 | ||
608514de JK |
389 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
390 | { | |
1f227a3e JK |
391 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file))))) |
392 | return -EIO; | |
608514de JK |
393 | return f2fs_do_sync_file(file, start, end, datasync, false); |
394 | } | |
395 | ||
4cb03fec MWO |
396 | static bool __found_offset(struct address_space *mapping, block_t blkaddr, |
397 | pgoff_t index, int whence) | |
7f7670fe JK |
398 | { |
399 | switch (whence) { | |
400 | case SEEK_DATA: | |
4cb03fec MWO |
401 | if (__is_valid_data_blkaddr(blkaddr)) |
402 | return true; | |
403 | if (blkaddr == NEW_ADDR && | |
404 | xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY)) | |
7f7670fe JK |
405 | return true; |
406 | break; | |
407 | case SEEK_HOLE: | |
408 | if (blkaddr == NULL_ADDR) | |
409 | return true; | |
410 | break; | |
411 | } | |
412 | return false; | |
413 | } | |
414 | ||
267378d4 CY |
415 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
416 | { | |
417 | struct inode *inode = file->f_mapping->host; | |
418 | loff_t maxbytes = inode->i_sb->s_maxbytes; | |
419 | struct dnode_of_data dn; | |
4cb03fec | 420 | pgoff_t pgofs, end_offset; |
7f7670fe JK |
421 | loff_t data_ofs = offset; |
422 | loff_t isize; | |
267378d4 CY |
423 | int err = 0; |
424 | ||
5955102c | 425 | inode_lock(inode); |
267378d4 CY |
426 | |
427 | isize = i_size_read(inode); | |
428 | if (offset >= isize) | |
429 | goto fail; | |
430 | ||
431 | /* handle inline data case */ | |
7a6e59d7 CY |
432 | if (f2fs_has_inline_data(inode)) { |
433 | if (whence == SEEK_HOLE) { | |
434 | data_ofs = isize; | |
435 | goto found; | |
436 | } else if (whence == SEEK_DATA) { | |
437 | data_ofs = offset; | |
438 | goto found; | |
439 | } | |
267378d4 CY |
440 | } |
441 | ||
09cbfeaf | 442 | pgofs = (pgoff_t)(offset >> PAGE_SHIFT); |
267378d4 | 443 | |
09cbfeaf | 444 | for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) { |
267378d4 | 445 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 446 | err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE); |
267378d4 CY |
447 | if (err && err != -ENOENT) { |
448 | goto fail; | |
449 | } else if (err == -ENOENT) { | |
e1c42045 | 450 | /* direct node does not exists */ |
267378d4 | 451 | if (whence == SEEK_DATA) { |
4d57b86d | 452 | pgofs = f2fs_get_next_page_offset(&dn, pgofs); |
267378d4 CY |
453 | continue; |
454 | } else { | |
455 | goto found; | |
456 | } | |
457 | } | |
458 | ||
81ca7350 | 459 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
267378d4 CY |
460 | |
461 | /* find data/hole in dnode block */ | |
462 | for (; dn.ofs_in_node < end_offset; | |
463 | dn.ofs_in_node++, pgofs++, | |
09cbfeaf | 464 | data_ofs = (loff_t)pgofs << PAGE_SHIFT) { |
267378d4 | 465 | block_t blkaddr; |
f11e98bd | 466 | |
a2ced1ce | 467 | blkaddr = f2fs_data_blkaddr(&dn); |
267378d4 | 468 | |
c9b60788 CY |
469 | if (__is_valid_data_blkaddr(blkaddr) && |
470 | !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), | |
93770ab7 | 471 | blkaddr, DATA_GENERIC_ENHANCE)) { |
c9b60788 CY |
472 | f2fs_put_dnode(&dn); |
473 | goto fail; | |
474 | } | |
475 | ||
4cb03fec | 476 | if (__found_offset(file->f_mapping, blkaddr, |
e1da7872 | 477 | pgofs, whence)) { |
267378d4 CY |
478 | f2fs_put_dnode(&dn); |
479 | goto found; | |
480 | } | |
481 | } | |
482 | f2fs_put_dnode(&dn); | |
483 | } | |
484 | ||
485 | if (whence == SEEK_DATA) | |
486 | goto fail; | |
267378d4 | 487 | found: |
fe369bc8 JK |
488 | if (whence == SEEK_HOLE && data_ofs > isize) |
489 | data_ofs = isize; | |
5955102c | 490 | inode_unlock(inode); |
267378d4 CY |
491 | return vfs_setpos(file, data_ofs, maxbytes); |
492 | fail: | |
5955102c | 493 | inode_unlock(inode); |
267378d4 CY |
494 | return -ENXIO; |
495 | } | |
496 | ||
497 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) | |
498 | { | |
499 | struct inode *inode = file->f_mapping->host; | |
500 | loff_t maxbytes = inode->i_sb->s_maxbytes; | |
501 | ||
6d1451bf CX |
502 | if (f2fs_compressed_file(inode)) |
503 | maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS; | |
504 | ||
267378d4 CY |
505 | switch (whence) { |
506 | case SEEK_SET: | |
507 | case SEEK_CUR: | |
508 | case SEEK_END: | |
509 | return generic_file_llseek_size(file, offset, whence, | |
510 | maxbytes, i_size_read(inode)); | |
511 | case SEEK_DATA: | |
512 | case SEEK_HOLE: | |
0b4c5afd JK |
513 | if (offset < 0) |
514 | return -ENXIO; | |
267378d4 CY |
515 | return f2fs_seek_block(file, offset, whence); |
516 | } | |
517 | ||
518 | return -EINVAL; | |
519 | } | |
520 | ||
fbfa2cc5 JK |
521 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
522 | { | |
b3d208f9 JK |
523 | struct inode *inode = file_inode(file); |
524 | ||
1f227a3e JK |
525 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
526 | return -EIO; | |
527 | ||
4c8ff709 CY |
528 | if (!f2fs_is_compress_backend_ready(inode)) |
529 | return -EOPNOTSUPP; | |
530 | ||
fbfa2cc5 JK |
531 | file_accessed(file); |
532 | vma->vm_ops = &f2fs_file_vm_ops; | |
4c8ff709 | 533 | set_inode_flag(inode, FI_MMAP_FILE); |
fbfa2cc5 JK |
534 | return 0; |
535 | } | |
536 | ||
fcc85a4d JK |
537 | static int f2fs_file_open(struct inode *inode, struct file *filp) |
538 | { | |
2e168c82 | 539 | int err = fscrypt_file_open(inode, filp); |
fcc85a4d | 540 | |
95ae251f EB |
541 | if (err) |
542 | return err; | |
543 | ||
4c8ff709 CY |
544 | if (!f2fs_is_compress_backend_ready(inode)) |
545 | return -EOPNOTSUPP; | |
546 | ||
95ae251f | 547 | err = fsverity_file_open(inode, filp); |
2e168c82 EB |
548 | if (err) |
549 | return err; | |
b91050a8 HL |
550 | |
551 | filp->f_mode |= FMODE_NOWAIT; | |
552 | ||
0abd675e | 553 | return dquot_file_open(inode, filp); |
fcc85a4d JK |
554 | } |
555 | ||
4d57b86d | 556 | void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
fbfa2cc5 | 557 | { |
4081363f | 558 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
fbfa2cc5 | 559 | struct f2fs_node *raw_node; |
19b2c30d | 560 | int nr_free = 0, ofs = dn->ofs_in_node, len = count; |
fbfa2cc5 | 561 | __le32 *addr; |
7a2af766 | 562 | int base = 0; |
4c8ff709 CY |
563 | bool compressed_cluster = false; |
564 | int cluster_index = 0, valid_blocks = 0; | |
565 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size; | |
c2759eba | 566 | bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks); |
7a2af766 CY |
567 | |
568 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) | |
569 | base = get_extra_isize(dn->inode); | |
fbfa2cc5 | 570 | |
45590710 | 571 | raw_node = F2FS_NODE(dn->node_page); |
7a2af766 | 572 | addr = blkaddr_in_node(raw_node) + base + ofs; |
fbfa2cc5 | 573 | |
4c8ff709 CY |
574 | /* Assumption: truncateion starts with cluster */ |
575 | for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) { | |
fbfa2cc5 | 576 | block_t blkaddr = le32_to_cpu(*addr); |
f11e98bd | 577 | |
4c8ff709 CY |
578 | if (f2fs_compressed_file(dn->inode) && |
579 | !(cluster_index & (cluster_size - 1))) { | |
580 | if (compressed_cluster) | |
581 | f2fs_i_compr_blocks_update(dn->inode, | |
582 | valid_blocks, false); | |
583 | compressed_cluster = (blkaddr == COMPRESS_ADDR); | |
584 | valid_blocks = 0; | |
585 | } | |
586 | ||
fbfa2cc5 JK |
587 | if (blkaddr == NULL_ADDR) |
588 | continue; | |
589 | ||
e1509cf2 | 590 | dn->data_blkaddr = NULL_ADDR; |
4d57b86d | 591 | f2fs_set_data_blkaddr(dn); |
c9b60788 | 592 | |
4c8ff709 CY |
593 | if (__is_valid_data_blkaddr(blkaddr)) { |
594 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
93770ab7 | 595 | DATA_GENERIC_ENHANCE)) |
4c8ff709 CY |
596 | continue; |
597 | if (compressed_cluster) | |
598 | valid_blocks++; | |
599 | } | |
c9b60788 | 600 | |
3c6c2beb | 601 | if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) |
91942321 | 602 | clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN); |
4c8ff709 CY |
603 | |
604 | f2fs_invalidate_blocks(sbi, blkaddr); | |
ef8d563f CY |
605 | |
606 | if (!released || blkaddr != COMPRESS_ADDR) | |
607 | nr_free++; | |
fbfa2cc5 | 608 | } |
19b2c30d | 609 | |
4c8ff709 CY |
610 | if (compressed_cluster) |
611 | f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false); | |
612 | ||
fbfa2cc5 | 613 | if (nr_free) { |
19b2c30d CY |
614 | pgoff_t fofs; |
615 | /* | |
616 | * once we invalidate valid blkaddr in range [ofs, ofs + count], | |
617 | * we will invalidate all blkaddr in the whole range. | |
618 | */ | |
4d57b86d | 619 | fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), |
81ca7350 | 620 | dn->inode) + ofs; |
19b2c30d | 621 | f2fs_update_extent_cache_range(dn, fofs, 0, len); |
d7cc950b | 622 | dec_valid_block_count(sbi, dn->inode, nr_free); |
fbfa2cc5 JK |
623 | } |
624 | dn->ofs_in_node = ofs; | |
51dd6249 | 625 | |
d0239e1b | 626 | f2fs_update_time(sbi, REQ_TIME); |
51dd6249 NJ |
627 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
628 | dn->ofs_in_node, nr_free); | |
fbfa2cc5 JK |
629 | } |
630 | ||
4d57b86d | 631 | void f2fs_truncate_data_blocks(struct dnode_of_data *dn) |
fbfa2cc5 | 632 | { |
d02a6e61 | 633 | f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode)); |
fbfa2cc5 JK |
634 | } |
635 | ||
0bfcfcca | 636 | static int truncate_partial_data_page(struct inode *inode, u64 from, |
43f3eae1 | 637 | bool cache_only) |
fbfa2cc5 | 638 | { |
193bea1d | 639 | loff_t offset = from & (PAGE_SIZE - 1); |
09cbfeaf | 640 | pgoff_t index = from >> PAGE_SHIFT; |
43f3eae1 | 641 | struct address_space *mapping = inode->i_mapping; |
fbfa2cc5 JK |
642 | struct page *page; |
643 | ||
43f3eae1 | 644 | if (!offset && !cache_only) |
b3d208f9 | 645 | return 0; |
fbfa2cc5 | 646 | |
43f3eae1 | 647 | if (cache_only) { |
34b5d5c2 | 648 | page = find_lock_page(mapping, index); |
43f3eae1 JK |
649 | if (page && PageUptodate(page)) |
650 | goto truncate_out; | |
651 | f2fs_put_page(page, 1); | |
b3d208f9 | 652 | return 0; |
43f3eae1 | 653 | } |
fbfa2cc5 | 654 | |
4d57b86d | 655 | page = f2fs_get_lock_data_page(inode, index, true); |
43f3eae1 | 656 | if (IS_ERR(page)) |
a78aaa2c | 657 | return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page); |
43f3eae1 | 658 | truncate_out: |
bae0ee7a | 659 | f2fs_wait_on_page_writeback(page, DATA, true, true); |
09cbfeaf | 660 | zero_user(page, offset, PAGE_SIZE - offset); |
a9bcf9bc JK |
661 | |
662 | /* An encrypted inode should have a key and truncate the last page. */ | |
62230e0d | 663 | f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode)); |
a9bcf9bc | 664 | if (!cache_only) |
0bfcfcca | 665 | set_page_dirty(page); |
fbfa2cc5 | 666 | f2fs_put_page(page, 1); |
b3d208f9 | 667 | return 0; |
fbfa2cc5 JK |
668 | } |
669 | ||
3265d3db | 670 | int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock) |
fbfa2cc5 | 671 | { |
4081363f | 672 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
fbfa2cc5 JK |
673 | struct dnode_of_data dn; |
674 | pgoff_t free_from; | |
9ffe0fb5 | 675 | int count = 0, err = 0; |
b3d208f9 | 676 | struct page *ipage; |
0bfcfcca | 677 | bool truncate_page = false; |
fbfa2cc5 | 678 | |
51dd6249 NJ |
679 | trace_f2fs_truncate_blocks_enter(inode, from); |
680 | ||
df033caf | 681 | free_from = (pgoff_t)F2FS_BLK_ALIGN(from); |
fbfa2cc5 | 682 | |
6d1451bf | 683 | if (free_from >= max_file_blocks(inode)) |
09210c97 CY |
684 | goto free_partial; |
685 | ||
764aa3e9 | 686 | if (lock) |
c42d28ce | 687 | f2fs_lock_op(sbi); |
9ffe0fb5 | 688 | |
4d57b86d | 689 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
b3d208f9 JK |
690 | if (IS_ERR(ipage)) { |
691 | err = PTR_ERR(ipage); | |
692 | goto out; | |
693 | } | |
694 | ||
695 | if (f2fs_has_inline_data(inode)) { | |
4d57b86d | 696 | f2fs_truncate_inline_inode(inode, ipage, from); |
b3d208f9 | 697 | f2fs_put_page(ipage, 1); |
0bfcfcca | 698 | truncate_page = true; |
b3d208f9 JK |
699 | goto out; |
700 | } | |
701 | ||
702 | set_new_dnode(&dn, inode, ipage, NULL, 0); | |
4d57b86d | 703 | err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA); |
fbfa2cc5 JK |
704 | if (err) { |
705 | if (err == -ENOENT) | |
706 | goto free_next; | |
b3d208f9 | 707 | goto out; |
1ce86bf6 JK |
708 | } |
709 | ||
81ca7350 | 710 | count = ADDRS_PER_PAGE(dn.node_page, inode); |
fbfa2cc5 JK |
711 | |
712 | count -= dn.ofs_in_node; | |
9850cf4a | 713 | f2fs_bug_on(sbi, count < 0); |
39936837 | 714 | |
fbfa2cc5 | 715 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
4d57b86d | 716 | f2fs_truncate_data_blocks_range(&dn, count); |
fbfa2cc5 JK |
717 | free_from += count; |
718 | } | |
719 | ||
720 | f2fs_put_dnode(&dn); | |
721 | free_next: | |
4d57b86d | 722 | err = f2fs_truncate_inode_blocks(inode, free_from); |
764d2c80 JK |
723 | out: |
724 | if (lock) | |
c42d28ce | 725 | f2fs_unlock_op(sbi); |
09210c97 | 726 | free_partial: |
b3d208f9 JK |
727 | /* lastly zero out the first data page */ |
728 | if (!err) | |
0bfcfcca | 729 | err = truncate_partial_data_page(inode, from, truncate_page); |
fbfa2cc5 | 730 | |
51dd6249 | 731 | trace_f2fs_truncate_blocks_exit(inode, err); |
fbfa2cc5 JK |
732 | return err; |
733 | } | |
734 | ||
4c8ff709 CY |
735 | int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock) |
736 | { | |
737 | u64 free_from = from; | |
3265d3db | 738 | int err; |
4c8ff709 | 739 | |
3265d3db | 740 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
4c8ff709 CY |
741 | /* |
742 | * for compressed file, only support cluster size | |
743 | * aligned truncation. | |
744 | */ | |
4fec3fc0 CY |
745 | if (f2fs_compressed_file(inode)) |
746 | free_from = round_up(from, | |
747 | F2FS_I(inode)->i_cluster_size << PAGE_SHIFT); | |
3265d3db CY |
748 | #endif |
749 | ||
750 | err = f2fs_do_truncate_blocks(inode, free_from, lock); | |
751 | if (err) | |
752 | return err; | |
753 | ||
754 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
4a4fc043 FC |
755 | /* |
756 | * For compressed file, after release compress blocks, don't allow write | |
757 | * direct, but we should allow write direct after truncate to zero. | |
758 | */ | |
759 | if (f2fs_compressed_file(inode) && !free_from | |
760 | && is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) | |
761 | clear_inode_flag(inode, FI_COMPRESS_RELEASED); | |
762 | ||
17d7648d | 763 | if (from != free_from) { |
3265d3db | 764 | err = f2fs_truncate_partial_cluster(inode, from, lock); |
17d7648d CY |
765 | if (err) |
766 | return err; | |
767 | } | |
3265d3db | 768 | #endif |
4c8ff709 | 769 | |
17d7648d | 770 | return 0; |
4c8ff709 CY |
771 | } |
772 | ||
9a449e9c | 773 | int f2fs_truncate(struct inode *inode) |
fbfa2cc5 | 774 | { |
b0154891 CY |
775 | int err; |
776 | ||
1f227a3e JK |
777 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
778 | return -EIO; | |
779 | ||
fbfa2cc5 JK |
780 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
781 | S_ISLNK(inode->i_mode))) | |
b0154891 | 782 | return 0; |
fbfa2cc5 | 783 | |
51dd6249 NJ |
784 | trace_f2fs_truncate(inode); |
785 | ||
14b44d23 | 786 | if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) { |
c45d6002 | 787 | f2fs_show_injection_info(F2FS_I_SB(inode), FAULT_TRUNCATE); |
14b44d23 JK |
788 | return -EIO; |
789 | } | |
7fa750a1 | 790 | |
10a26878 | 791 | err = f2fs_dquot_initialize(inode); |
25fb04db YC |
792 | if (err) |
793 | return err; | |
794 | ||
92dffd01 | 795 | /* we should check inline_data size */ |
b9d777b8 | 796 | if (!f2fs_may_inline_data(inode)) { |
b0154891 CY |
797 | err = f2fs_convert_inline_inode(inode); |
798 | if (err) | |
799 | return err; | |
92dffd01 JK |
800 | } |
801 | ||
c42d28ce | 802 | err = f2fs_truncate_blocks(inode, i_size_read(inode), true); |
b0154891 CY |
803 | if (err) |
804 | return err; | |
805 | ||
078cd827 | 806 | inode->i_mtime = inode->i_ctime = current_time(inode); |
7c45729a | 807 | f2fs_mark_inode_dirty_sync(inode, false); |
b0154891 | 808 | return 0; |
fbfa2cc5 JK |
809 | } |
810 | ||
549c7297 CB |
811 | int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path, |
812 | struct kstat *stat, u32 request_mask, unsigned int query_flags) | |
fbfa2cc5 | 813 | { |
a528d35e | 814 | struct inode *inode = d_inode(path->dentry); |
1c6d8ee4 | 815 | struct f2fs_inode_info *fi = F2FS_I(inode); |
d13732cc | 816 | struct f2fs_inode *ri = NULL; |
1c6d8ee4 CY |
817 | unsigned int flags; |
818 | ||
1c1d35df | 819 | if (f2fs_has_extra_attr(inode) && |
7beb01f7 | 820 | f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) && |
1c1d35df CY |
821 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) { |
822 | stat->result_mask |= STATX_BTIME; | |
823 | stat->btime.tv_sec = fi->i_crtime.tv_sec; | |
824 | stat->btime.tv_nsec = fi->i_crtime.tv_nsec; | |
825 | } | |
826 | ||
36098557 | 827 | flags = fi->i_flags; |
fd26725f CY |
828 | if (flags & F2FS_COMPR_FL) |
829 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
59c84408 | 830 | if (flags & F2FS_APPEND_FL) |
1c6d8ee4 | 831 | stat->attributes |= STATX_ATTR_APPEND; |
62230e0d | 832 | if (IS_ENCRYPTED(inode)) |
1c6d8ee4 | 833 | stat->attributes |= STATX_ATTR_ENCRYPTED; |
59c84408 | 834 | if (flags & F2FS_IMMUTABLE_FL) |
1c6d8ee4 | 835 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
59c84408 | 836 | if (flags & F2FS_NODUMP_FL) |
1c6d8ee4 | 837 | stat->attributes |= STATX_ATTR_NODUMP; |
924e3194 EB |
838 | if (IS_VERITY(inode)) |
839 | stat->attributes |= STATX_ATTR_VERITY; | |
1c6d8ee4 | 840 | |
fd26725f CY |
841 | stat->attributes_mask |= (STATX_ATTR_COMPRESSED | |
842 | STATX_ATTR_APPEND | | |
1c6d8ee4 CY |
843 | STATX_ATTR_ENCRYPTED | |
844 | STATX_ATTR_IMMUTABLE | | |
924e3194 EB |
845 | STATX_ATTR_NODUMP | |
846 | STATX_ATTR_VERITY); | |
1c6d8ee4 | 847 | |
984fc4e7 | 848 | generic_fillattr(mnt_userns, inode, stat); |
5b4267d1 JK |
849 | |
850 | /* we need to show initial sectors used for inline_data/dentries */ | |
851 | if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) || | |
852 | f2fs_has_inline_dentry(inode)) | |
853 | stat->blocks += (stat->size + 511) >> 9; | |
854 | ||
fbfa2cc5 JK |
855 | return 0; |
856 | } | |
857 | ||
858 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
e65ce2a5 CB |
859 | static void __setattr_copy(struct user_namespace *mnt_userns, |
860 | struct inode *inode, const struct iattr *attr) | |
fbfa2cc5 | 861 | { |
fbfa2cc5 JK |
862 | unsigned int ia_valid = attr->ia_valid; |
863 | ||
864 | if (ia_valid & ATTR_UID) | |
865 | inode->i_uid = attr->ia_uid; | |
866 | if (ia_valid & ATTR_GID) | |
867 | inode->i_gid = attr->ia_gid; | |
eb31e2f6 AG |
868 | if (ia_valid & ATTR_ATIME) |
869 | inode->i_atime = attr->ia_atime; | |
870 | if (ia_valid & ATTR_MTIME) | |
871 | inode->i_mtime = attr->ia_mtime; | |
872 | if (ia_valid & ATTR_CTIME) | |
873 | inode->i_ctime = attr->ia_ctime; | |
fbfa2cc5 JK |
874 | if (ia_valid & ATTR_MODE) { |
875 | umode_t mode = attr->ia_mode; | |
2f221d6f | 876 | kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); |
fbfa2cc5 | 877 | |
7d6beb71 | 878 | if (!in_group_p(kgid) && !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) |
fbfa2cc5 | 879 | mode &= ~S_ISGID; |
91942321 | 880 | set_acl_inode(inode, mode); |
fbfa2cc5 JK |
881 | } |
882 | } | |
883 | #else | |
884 | #define __setattr_copy setattr_copy | |
885 | #endif | |
886 | ||
549c7297 CB |
887 | int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
888 | struct iattr *attr) | |
fbfa2cc5 | 889 | { |
2b0143b5 | 890 | struct inode *inode = d_inode(dentry); |
fbfa2cc5 JK |
891 | int err; |
892 | ||
1f227a3e JK |
893 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
894 | return -EIO; | |
895 | ||
e0fcd015 CY |
896 | if (unlikely(IS_IMMUTABLE(inode))) |
897 | return -EPERM; | |
898 | ||
899 | if (unlikely(IS_APPEND(inode) && | |
900 | (attr->ia_valid & (ATTR_MODE | ATTR_UID | | |
901 | ATTR_GID | ATTR_TIMES_SET)))) | |
902 | return -EPERM; | |
903 | ||
4c8ff709 CY |
904 | if ((attr->ia_valid & ATTR_SIZE) && |
905 | !f2fs_is_compress_backend_ready(inode)) | |
906 | return -EOPNOTSUPP; | |
907 | ||
984fc4e7 | 908 | err = setattr_prepare(mnt_userns, dentry, attr); |
fbfa2cc5 JK |
909 | if (err) |
910 | return err; | |
911 | ||
20bb2479 EB |
912 | err = fscrypt_prepare_setattr(dentry, attr); |
913 | if (err) | |
914 | return err; | |
915 | ||
95ae251f EB |
916 | err = fsverity_prepare_setattr(dentry, attr); |
917 | if (err) | |
918 | return err; | |
919 | ||
0abd675e | 920 | if (is_quota_modification(inode, attr)) { |
10a26878 | 921 | err = f2fs_dquot_initialize(inode); |
0abd675e CY |
922 | if (err) |
923 | return err; | |
924 | } | |
925 | if ((attr->ia_valid & ATTR_UID && | |
926 | !uid_eq(attr->ia_uid, inode->i_uid)) || | |
927 | (attr->ia_valid & ATTR_GID && | |
928 | !gid_eq(attr->ia_gid, inode->i_gid))) { | |
af033b2a | 929 | f2fs_lock_op(F2FS_I_SB(inode)); |
0abd675e | 930 | err = dquot_transfer(inode, attr); |
af033b2a CY |
931 | if (err) { |
932 | set_sbi_flag(F2FS_I_SB(inode), | |
933 | SBI_QUOTA_NEED_REPAIR); | |
934 | f2fs_unlock_op(F2FS_I_SB(inode)); | |
0abd675e | 935 | return err; |
af033b2a CY |
936 | } |
937 | /* | |
938 | * update uid/gid under lock_op(), so that dquot and inode can | |
939 | * be updated atomically. | |
940 | */ | |
941 | if (attr->ia_valid & ATTR_UID) | |
942 | inode->i_uid = attr->ia_uid; | |
943 | if (attr->ia_valid & ATTR_GID) | |
944 | inode->i_gid = attr->ia_gid; | |
945 | f2fs_mark_inode_dirty_sync(inode, true); | |
946 | f2fs_unlock_op(F2FS_I_SB(inode)); | |
0abd675e CY |
947 | } |
948 | ||
09db6a2e | 949 | if (attr->ia_valid & ATTR_SIZE) { |
cfb9a34d JK |
950 | loff_t old_size = i_size_read(inode); |
951 | ||
952 | if (attr->ia_size > MAX_INLINE_DATA(inode)) { | |
953 | /* | |
954 | * should convert inline inode before i_size_write to | |
955 | * keep smaller than inline_data size with inline flag. | |
956 | */ | |
957 | err = f2fs_convert_inline_inode(inode); | |
958 | if (err) | |
959 | return err; | |
960 | } | |
a33c1502 | 961 | |
e4544b63 | 962 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 963 | filemap_invalidate_lock(inode->i_mapping); |
a33c1502 CY |
964 | |
965 | truncate_setsize(inode, attr->ia_size); | |
966 | ||
cfb9a34d | 967 | if (attr->ia_size <= old_size) |
9a449e9c | 968 | err = f2fs_truncate(inode); |
a33c1502 CY |
969 | /* |
970 | * do not trim all blocks after i_size if target size is | |
971 | * larger than i_size. | |
972 | */ | |
edc6d01b | 973 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 974 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
a33c1502 CY |
975 | if (err) |
976 | return err; | |
0cab80ee | 977 | |
c10c9820 | 978 | spin_lock(&F2FS_I(inode)->i_size_lock); |
cfb9a34d | 979 | inode->i_mtime = inode->i_ctime = current_time(inode); |
a0d00fad | 980 | F2FS_I(inode)->last_disk_size = i_size_read(inode); |
c10c9820 | 981 | spin_unlock(&F2FS_I(inode)->i_size_lock); |
fbfa2cc5 JK |
982 | } |
983 | ||
984fc4e7 | 984 | __setattr_copy(mnt_userns, inode, attr); |
fbfa2cc5 JK |
985 | |
986 | if (attr->ia_valid & ATTR_MODE) { | |
984fc4e7 | 987 | err = posix_acl_chmod(mnt_userns, inode, f2fs_get_inode_mode(inode)); |
17232e83 CY |
988 | |
989 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { | |
990 | if (!err) | |
991 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
91942321 | 992 | clear_inode_flag(inode, FI_ACL_MODE); |
fbfa2cc5 JK |
993 | } |
994 | } | |
995 | ||
c0ed4405 | 996 | /* file size may changed here */ |
ca597bdd | 997 | f2fs_mark_inode_dirty_sync(inode, true); |
15d04354 JK |
998 | |
999 | /* inode change will produce dirty node pages flushed by checkpoint */ | |
1000 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
1001 | ||
fbfa2cc5 JK |
1002 | return err; |
1003 | } | |
1004 | ||
1005 | const struct inode_operations f2fs_file_inode_operations = { | |
1006 | .getattr = f2fs_getattr, | |
1007 | .setattr = f2fs_setattr, | |
1008 | .get_acl = f2fs_get_acl, | |
a6dda0e6 | 1009 | .set_acl = f2fs_set_acl, |
fbfa2cc5 | 1010 | .listxattr = f2fs_listxattr, |
9ab70134 | 1011 | .fiemap = f2fs_fiemap, |
9b1bb01c MS |
1012 | .fileattr_get = f2fs_fileattr_get, |
1013 | .fileattr_set = f2fs_fileattr_set, | |
fbfa2cc5 JK |
1014 | }; |
1015 | ||
6394328a | 1016 | static int fill_zero(struct inode *inode, pgoff_t index, |
fbfa2cc5 JK |
1017 | loff_t start, loff_t len) |
1018 | { | |
4081363f | 1019 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
fbfa2cc5 JK |
1020 | struct page *page; |
1021 | ||
1022 | if (!len) | |
6394328a | 1023 | return 0; |
fbfa2cc5 | 1024 | |
2c4db1a6 | 1025 | f2fs_balance_fs(sbi, true); |
bd43df02 | 1026 | |
e479556b | 1027 | f2fs_lock_op(sbi); |
4d57b86d | 1028 | page = f2fs_get_new_data_page(inode, NULL, index, false); |
e479556b | 1029 | f2fs_unlock_op(sbi); |
fbfa2cc5 | 1030 | |
6394328a CY |
1031 | if (IS_ERR(page)) |
1032 | return PTR_ERR(page); | |
1033 | ||
bae0ee7a | 1034 | f2fs_wait_on_page_writeback(page, DATA, true, true); |
6394328a CY |
1035 | zero_user(page, start, len); |
1036 | set_page_dirty(page); | |
1037 | f2fs_put_page(page, 1); | |
1038 | return 0; | |
fbfa2cc5 JK |
1039 | } |
1040 | ||
4d57b86d | 1041 | int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
fbfa2cc5 | 1042 | { |
fbfa2cc5 JK |
1043 | int err; |
1044 | ||
ea58711e | 1045 | while (pg_start < pg_end) { |
fbfa2cc5 | 1046 | struct dnode_of_data dn; |
ea58711e | 1047 | pgoff_t end_offset, count; |
9eaeba70 | 1048 | |
fbfa2cc5 | 1049 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 1050 | err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE); |
fbfa2cc5 | 1051 | if (err) { |
ea58711e | 1052 | if (err == -ENOENT) { |
4d57b86d CY |
1053 | pg_start = f2fs_get_next_page_offset(&dn, |
1054 | pg_start); | |
fbfa2cc5 | 1055 | continue; |
ea58711e | 1056 | } |
fbfa2cc5 JK |
1057 | return err; |
1058 | } | |
1059 | ||
81ca7350 | 1060 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
ea58711e CY |
1061 | count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); |
1062 | ||
1063 | f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); | |
1064 | ||
4d57b86d | 1065 | f2fs_truncate_data_blocks_range(&dn, count); |
fbfa2cc5 | 1066 | f2fs_put_dnode(&dn); |
ea58711e CY |
1067 | |
1068 | pg_start += count; | |
fbfa2cc5 JK |
1069 | } |
1070 | return 0; | |
1071 | } | |
1072 | ||
a66c7b2f | 1073 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len) |
fbfa2cc5 JK |
1074 | { |
1075 | pgoff_t pg_start, pg_end; | |
1076 | loff_t off_start, off_end; | |
b9d777b8 | 1077 | int ret; |
fbfa2cc5 | 1078 | |
b9d777b8 JK |
1079 | ret = f2fs_convert_inline_inode(inode); |
1080 | if (ret) | |
1081 | return ret; | |
9ffe0fb5 | 1082 | |
09cbfeaf KS |
1083 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; |
1084 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; | |
fbfa2cc5 | 1085 | |
09cbfeaf KS |
1086 | off_start = offset & (PAGE_SIZE - 1); |
1087 | off_end = (offset + len) & (PAGE_SIZE - 1); | |
fbfa2cc5 JK |
1088 | |
1089 | if (pg_start == pg_end) { | |
6394328a | 1090 | ret = fill_zero(inode, pg_start, off_start, |
fbfa2cc5 | 1091 | off_end - off_start); |
6394328a CY |
1092 | if (ret) |
1093 | return ret; | |
fbfa2cc5 | 1094 | } else { |
6394328a CY |
1095 | if (off_start) { |
1096 | ret = fill_zero(inode, pg_start++, off_start, | |
09cbfeaf | 1097 | PAGE_SIZE - off_start); |
6394328a CY |
1098 | if (ret) |
1099 | return ret; | |
1100 | } | |
1101 | if (off_end) { | |
1102 | ret = fill_zero(inode, pg_end, 0, off_end); | |
1103 | if (ret) | |
1104 | return ret; | |
1105 | } | |
fbfa2cc5 JK |
1106 | |
1107 | if (pg_start < pg_end) { | |
fbfa2cc5 | 1108 | loff_t blk_start, blk_end; |
4081363f | 1109 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
1127a3d4 | 1110 | |
2c4db1a6 | 1111 | f2fs_balance_fs(sbi, true); |
fbfa2cc5 | 1112 | |
09cbfeaf KS |
1113 | blk_start = (loff_t)pg_start << PAGE_SHIFT; |
1114 | blk_end = (loff_t)pg_end << PAGE_SHIFT; | |
a33c1502 | 1115 | |
e4544b63 | 1116 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
6abaa83c | 1117 | filemap_invalidate_lock(inode->i_mapping); |
a33c1502 | 1118 | |
c8dc3047 | 1119 | truncate_pagecache_range(inode, blk_start, blk_end - 1); |
39936837 | 1120 | |
e479556b | 1121 | f2fs_lock_op(sbi); |
4d57b86d | 1122 | ret = f2fs_truncate_hole(inode, pg_start, pg_end); |
e479556b | 1123 | f2fs_unlock_op(sbi); |
a33c1502 | 1124 | |
6abaa83c | 1125 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 1126 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
fbfa2cc5 JK |
1127 | } |
1128 | } | |
1129 | ||
fbfa2cc5 JK |
1130 | return ret; |
1131 | } | |
1132 | ||
0a2aa8fb JK |
1133 | static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr, |
1134 | int *do_replace, pgoff_t off, pgoff_t len) | |
b4ace337 CY |
1135 | { |
1136 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1137 | struct dnode_of_data dn; | |
0a2aa8fb | 1138 | int ret, done, i; |
ecbaa406 | 1139 | |
0a2aa8fb | 1140 | next_dnode: |
6e2c64ad | 1141 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 1142 | ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA); |
6e2c64ad JK |
1143 | if (ret && ret != -ENOENT) { |
1144 | return ret; | |
1145 | } else if (ret == -ENOENT) { | |
0a2aa8fb JK |
1146 | if (dn.max_level == 0) |
1147 | return -ENOENT; | |
4c8ff709 CY |
1148 | done = min((pgoff_t)ADDRS_PER_BLOCK(inode) - |
1149 | dn.ofs_in_node, len); | |
0a2aa8fb JK |
1150 | blkaddr += done; |
1151 | do_replace += done; | |
1152 | goto next; | |
1153 | } | |
1154 | ||
1155 | done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) - | |
1156 | dn.ofs_in_node, len); | |
1157 | for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) { | |
a2ced1ce | 1158 | *blkaddr = f2fs_data_blkaddr(&dn); |
93770ab7 CY |
1159 | |
1160 | if (__is_valid_data_blkaddr(*blkaddr) && | |
1161 | !f2fs_is_valid_blkaddr(sbi, *blkaddr, | |
1162 | DATA_GENERIC_ENHANCE)) { | |
1163 | f2fs_put_dnode(&dn); | |
10f966bb | 1164 | return -EFSCORRUPTED; |
93770ab7 CY |
1165 | } |
1166 | ||
4d57b86d | 1167 | if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) { |
0a2aa8fb | 1168 | |
b0332a0f | 1169 | if (f2fs_lfs_mode(sbi)) { |
0a2aa8fb | 1170 | f2fs_put_dnode(&dn); |
fd114ab2 | 1171 | return -EOPNOTSUPP; |
0a2aa8fb JK |
1172 | } |
1173 | ||
6e2c64ad | 1174 | /* do not invalidate this block address */ |
f28b3434 | 1175 | f2fs_update_data_blkaddr(&dn, NULL_ADDR); |
0a2aa8fb | 1176 | *do_replace = 1; |
b4ace337 | 1177 | } |
6e2c64ad | 1178 | } |
0a2aa8fb JK |
1179 | f2fs_put_dnode(&dn); |
1180 | next: | |
1181 | len -= done; | |
1182 | off += done; | |
1183 | if (len) | |
1184 | goto next_dnode; | |
1185 | return 0; | |
1186 | } | |
b4ace337 | 1187 | |
0a2aa8fb JK |
1188 | static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr, |
1189 | int *do_replace, pgoff_t off, int len) | |
1190 | { | |
1191 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1192 | struct dnode_of_data dn; | |
1193 | int ret, i; | |
b4ace337 | 1194 | |
0a2aa8fb JK |
1195 | for (i = 0; i < len; i++, do_replace++, blkaddr++) { |
1196 | if (*do_replace == 0) | |
1197 | continue; | |
b4ace337 | 1198 | |
0a2aa8fb | 1199 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 1200 | ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA); |
0a2aa8fb JK |
1201 | if (ret) { |
1202 | dec_valid_block_count(sbi, inode, 1); | |
4d57b86d | 1203 | f2fs_invalidate_blocks(sbi, *blkaddr); |
0a2aa8fb JK |
1204 | } else { |
1205 | f2fs_update_data_blkaddr(&dn, *blkaddr); | |
36abef4e | 1206 | } |
0a2aa8fb JK |
1207 | f2fs_put_dnode(&dn); |
1208 | } | |
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode, | |
1213 | block_t *blkaddr, int *do_replace, | |
1214 | pgoff_t src, pgoff_t dst, pgoff_t len, bool full) | |
1215 | { | |
1216 | struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode); | |
1217 | pgoff_t i = 0; | |
1218 | int ret; | |
36abef4e | 1219 | |
0a2aa8fb JK |
1220 | while (i < len) { |
1221 | if (blkaddr[i] == NULL_ADDR && !full) { | |
1222 | i++; | |
1223 | continue; | |
6e2c64ad | 1224 | } |
b4ace337 | 1225 | |
0a2aa8fb JK |
1226 | if (do_replace[i] || blkaddr[i] == NULL_ADDR) { |
1227 | struct dnode_of_data dn; | |
1228 | struct node_info ni; | |
1229 | size_t new_size; | |
1230 | pgoff_t ilen; | |
b4ace337 | 1231 | |
0a2aa8fb | 1232 | set_new_dnode(&dn, dst_inode, NULL, NULL, 0); |
4d57b86d | 1233 | ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE); |
0a2aa8fb JK |
1234 | if (ret) |
1235 | return ret; | |
b4ace337 | 1236 | |
a9419b63 | 1237 | ret = f2fs_get_node_info(sbi, dn.nid, &ni, false); |
7735730d CY |
1238 | if (ret) { |
1239 | f2fs_put_dnode(&dn); | |
1240 | return ret; | |
1241 | } | |
1242 | ||
0a2aa8fb JK |
1243 | ilen = min((pgoff_t) |
1244 | ADDRS_PER_PAGE(dn.node_page, dst_inode) - | |
1245 | dn.ofs_in_node, len - i); | |
1246 | do { | |
a2ced1ce | 1247 | dn.data_blkaddr = f2fs_data_blkaddr(&dn); |
4d57b86d | 1248 | f2fs_truncate_data_blocks_range(&dn, 1); |
0a2aa8fb JK |
1249 | |
1250 | if (do_replace[i]) { | |
1251 | f2fs_i_blocks_write(src_inode, | |
0abd675e | 1252 | 1, false, false); |
0a2aa8fb | 1253 | f2fs_i_blocks_write(dst_inode, |
0abd675e | 1254 | 1, true, false); |
0a2aa8fb JK |
1255 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, |
1256 | blkaddr[i], ni.version, true, false); | |
1257 | ||
1258 | do_replace[i] = 0; | |
1259 | } | |
1260 | dn.ofs_in_node++; | |
1261 | i++; | |
1f0d5c91 | 1262 | new_size = (loff_t)(dst + i) << PAGE_SHIFT; |
0a2aa8fb JK |
1263 | if (dst_inode->i_size < new_size) |
1264 | f2fs_i_size_write(dst_inode, new_size); | |
e87f7329 | 1265 | } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR)); |
6e2c64ad | 1266 | |
0a2aa8fb JK |
1267 | f2fs_put_dnode(&dn); |
1268 | } else { | |
1269 | struct page *psrc, *pdst; | |
1270 | ||
4d57b86d CY |
1271 | psrc = f2fs_get_lock_data_page(src_inode, |
1272 | src + i, true); | |
0a2aa8fb JK |
1273 | if (IS_ERR(psrc)) |
1274 | return PTR_ERR(psrc); | |
4d57b86d | 1275 | pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i, |
0a2aa8fb JK |
1276 | true); |
1277 | if (IS_ERR(pdst)) { | |
1278 | f2fs_put_page(psrc, 1); | |
1279 | return PTR_ERR(pdst); | |
1280 | } | |
1281 | f2fs_copy_page(psrc, pdst); | |
1282 | set_page_dirty(pdst); | |
1283 | f2fs_put_page(pdst, 1); | |
6e2c64ad | 1284 | f2fs_put_page(psrc, 1); |
b4ace337 | 1285 | |
4d57b86d CY |
1286 | ret = f2fs_truncate_hole(src_inode, |
1287 | src + i, src + i + 1); | |
0a2aa8fb JK |
1288 | if (ret) |
1289 | return ret; | |
1290 | i++; | |
1291 | } | |
6e2c64ad JK |
1292 | } |
1293 | return 0; | |
0a2aa8fb | 1294 | } |
b4ace337 | 1295 | |
0a2aa8fb JK |
1296 | static int __exchange_data_block(struct inode *src_inode, |
1297 | struct inode *dst_inode, pgoff_t src, pgoff_t dst, | |
363cad7f | 1298 | pgoff_t len, bool full) |
0a2aa8fb JK |
1299 | { |
1300 | block_t *src_blkaddr; | |
1301 | int *do_replace; | |
363cad7f | 1302 | pgoff_t olen; |
0a2aa8fb JK |
1303 | int ret; |
1304 | ||
363cad7f | 1305 | while (len) { |
d02a6e61 | 1306 | olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len); |
0a2aa8fb | 1307 | |
628b3d14 | 1308 | src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode), |
9d2a789c | 1309 | array_size(olen, sizeof(block_t)), |
4f4460c0 | 1310 | GFP_NOFS); |
363cad7f JK |
1311 | if (!src_blkaddr) |
1312 | return -ENOMEM; | |
0a2aa8fb | 1313 | |
628b3d14 | 1314 | do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode), |
9d2a789c | 1315 | array_size(olen, sizeof(int)), |
4f4460c0 | 1316 | GFP_NOFS); |
363cad7f JK |
1317 | if (!do_replace) { |
1318 | kvfree(src_blkaddr); | |
1319 | return -ENOMEM; | |
1320 | } | |
0a2aa8fb | 1321 | |
363cad7f JK |
1322 | ret = __read_out_blkaddrs(src_inode, src_blkaddr, |
1323 | do_replace, src, olen); | |
1324 | if (ret) | |
1325 | goto roll_back; | |
0a2aa8fb | 1326 | |
363cad7f JK |
1327 | ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr, |
1328 | do_replace, src, dst, olen, full); | |
1329 | if (ret) | |
1330 | goto roll_back; | |
1331 | ||
1332 | src += olen; | |
1333 | dst += olen; | |
1334 | len -= olen; | |
1335 | ||
1336 | kvfree(src_blkaddr); | |
1337 | kvfree(do_replace); | |
1338 | } | |
0a2aa8fb JK |
1339 | return 0; |
1340 | ||
1341 | roll_back: | |
9fd62605 | 1342 | __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen); |
0a2aa8fb JK |
1343 | kvfree(src_blkaddr); |
1344 | kvfree(do_replace); | |
6e2c64ad JK |
1345 | return ret; |
1346 | } | |
b4ace337 | 1347 | |
6f8d4455 | 1348 | static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len) |
6e2c64ad JK |
1349 | { |
1350 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
f91108b8 | 1351 | pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
6f8d4455 JK |
1352 | pgoff_t start = offset >> PAGE_SHIFT; |
1353 | pgoff_t end = (offset + len) >> PAGE_SHIFT; | |
0a2aa8fb | 1354 | int ret; |
6e2c64ad | 1355 | |
0a2aa8fb | 1356 | f2fs_balance_fs(sbi, true); |
5f281fab | 1357 | |
6f8d4455 | 1358 | /* avoid gc operation during block exchange */ |
e4544b63 | 1359 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 1360 | filemap_invalidate_lock(inode->i_mapping); |
5f281fab | 1361 | |
6f8d4455 JK |
1362 | f2fs_lock_op(sbi); |
1363 | f2fs_drop_extent_tree(inode); | |
1364 | truncate_pagecache(inode, offset); | |
0a2aa8fb JK |
1365 | ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true); |
1366 | f2fs_unlock_op(sbi); | |
6f8d4455 | 1367 | |
edc6d01b | 1368 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 1369 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
b4ace337 CY |
1370 | return ret; |
1371 | } | |
1372 | ||
1373 | static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) | |
1374 | { | |
b4ace337 CY |
1375 | loff_t new_size; |
1376 | int ret; | |
1377 | ||
b4ace337 CY |
1378 | if (offset + len >= i_size_read(inode)) |
1379 | return -EINVAL; | |
1380 | ||
1381 | /* collapse range should be aligned to block size of f2fs. */ | |
1382 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) | |
1383 | return -EINVAL; | |
1384 | ||
b9d777b8 JK |
1385 | ret = f2fs_convert_inline_inode(inode); |
1386 | if (ret) | |
1387 | return ret; | |
97a7b2c2 | 1388 | |
b4ace337 CY |
1389 | /* write out all dirty pages from offset */ |
1390 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); | |
1391 | if (ret) | |
6f8d4455 | 1392 | return ret; |
b4ace337 | 1393 | |
6f8d4455 | 1394 | ret = f2fs_do_collapse(inode, offset, len); |
b4ace337 | 1395 | if (ret) |
6f8d4455 | 1396 | return ret; |
b4ace337 | 1397 | |
6e2c64ad | 1398 | /* write out all moved pages, if possible */ |
edc6d01b | 1399 | filemap_invalidate_lock(inode->i_mapping); |
6e2c64ad JK |
1400 | filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); |
1401 | truncate_pagecache(inode, offset); | |
1402 | ||
b4ace337 | 1403 | new_size = i_size_read(inode) - len; |
c42d28ce | 1404 | ret = f2fs_truncate_blocks(inode, new_size, true); |
edc6d01b | 1405 | filemap_invalidate_unlock(inode->i_mapping); |
b4ace337 | 1406 | if (!ret) |
fc9581c8 | 1407 | f2fs_i_size_write(inode, new_size); |
b4ace337 CY |
1408 | return ret; |
1409 | } | |
1410 | ||
6e961949 CY |
1411 | static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, |
1412 | pgoff_t end) | |
1413 | { | |
1414 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
1415 | pgoff_t index = start; | |
1416 | unsigned int ofs_in_node = dn->ofs_in_node; | |
1417 | blkcnt_t count = 0; | |
1418 | int ret; | |
1419 | ||
1420 | for (; index < end; index++, dn->ofs_in_node++) { | |
a2ced1ce | 1421 | if (f2fs_data_blkaddr(dn) == NULL_ADDR) |
6e961949 CY |
1422 | count++; |
1423 | } | |
1424 | ||
1425 | dn->ofs_in_node = ofs_in_node; | |
4d57b86d | 1426 | ret = f2fs_reserve_new_blocks(dn, count); |
6e961949 CY |
1427 | if (ret) |
1428 | return ret; | |
1429 | ||
1430 | dn->ofs_in_node = ofs_in_node; | |
1431 | for (index = start; index < end; index++, dn->ofs_in_node++) { | |
a2ced1ce | 1432 | dn->data_blkaddr = f2fs_data_blkaddr(dn); |
6e961949 | 1433 | /* |
4d57b86d | 1434 | * f2fs_reserve_new_blocks will not guarantee entire block |
6e961949 CY |
1435 | * allocation. |
1436 | */ | |
1437 | if (dn->data_blkaddr == NULL_ADDR) { | |
1438 | ret = -ENOSPC; | |
1439 | break; | |
1440 | } | |
25f82362 CY |
1441 | |
1442 | if (dn->data_blkaddr == NEW_ADDR) | |
1443 | continue; | |
1444 | ||
1445 | if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr, | |
1446 | DATA_GENERIC_ENHANCE)) { | |
1447 | ret = -EFSCORRUPTED; | |
1448 | break; | |
6e961949 | 1449 | } |
25f82362 CY |
1450 | |
1451 | f2fs_invalidate_blocks(sbi, dn->data_blkaddr); | |
1452 | dn->data_blkaddr = NEW_ADDR; | |
1453 | f2fs_set_data_blkaddr(dn); | |
6e961949 CY |
1454 | } |
1455 | ||
1456 | f2fs_update_extent_cache_range(dn, start, 0, index - start); | |
1457 | ||
1458 | return ret; | |
1459 | } | |
1460 | ||
75cd4e09 CY |
1461 | static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, |
1462 | int mode) | |
1463 | { | |
1464 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1465 | struct address_space *mapping = inode->i_mapping; | |
1466 | pgoff_t index, pg_start, pg_end; | |
1467 | loff_t new_size = i_size_read(inode); | |
1468 | loff_t off_start, off_end; | |
1469 | int ret = 0; | |
1470 | ||
75cd4e09 CY |
1471 | ret = inode_newsize_ok(inode, (len + offset)); |
1472 | if (ret) | |
1473 | return ret; | |
1474 | ||
b9d777b8 JK |
1475 | ret = f2fs_convert_inline_inode(inode); |
1476 | if (ret) | |
1477 | return ret; | |
75cd4e09 CY |
1478 | |
1479 | ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); | |
1480 | if (ret) | |
6f8d4455 | 1481 | return ret; |
75cd4e09 | 1482 | |
09cbfeaf KS |
1483 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; |
1484 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; | |
75cd4e09 | 1485 | |
09cbfeaf KS |
1486 | off_start = offset & (PAGE_SIZE - 1); |
1487 | off_end = (offset + len) & (PAGE_SIZE - 1); | |
75cd4e09 CY |
1488 | |
1489 | if (pg_start == pg_end) { | |
6394328a CY |
1490 | ret = fill_zero(inode, pg_start, off_start, |
1491 | off_end - off_start); | |
1492 | if (ret) | |
6f8d4455 | 1493 | return ret; |
6394328a | 1494 | |
75cd4e09 CY |
1495 | new_size = max_t(loff_t, new_size, offset + len); |
1496 | } else { | |
1497 | if (off_start) { | |
6394328a | 1498 | ret = fill_zero(inode, pg_start++, off_start, |
09cbfeaf | 1499 | PAGE_SIZE - off_start); |
6394328a | 1500 | if (ret) |
6f8d4455 | 1501 | return ret; |
6394328a | 1502 | |
75cd4e09 | 1503 | new_size = max_t(loff_t, new_size, |
09cbfeaf | 1504 | (loff_t)pg_start << PAGE_SHIFT); |
75cd4e09 CY |
1505 | } |
1506 | ||
6e961949 | 1507 | for (index = pg_start; index < pg_end;) { |
75cd4e09 | 1508 | struct dnode_of_data dn; |
6e961949 CY |
1509 | unsigned int end_offset; |
1510 | pgoff_t end; | |
75cd4e09 | 1511 | |
e4544b63 | 1512 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 1513 | filemap_invalidate_lock(mapping); |
c7079853 CY |
1514 | |
1515 | truncate_pagecache_range(inode, | |
1516 | (loff_t)index << PAGE_SHIFT, | |
1517 | ((loff_t)pg_end << PAGE_SHIFT) - 1); | |
1518 | ||
75cd4e09 CY |
1519 | f2fs_lock_op(sbi); |
1520 | ||
6e961949 | 1521 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 1522 | ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE); |
75cd4e09 CY |
1523 | if (ret) { |
1524 | f2fs_unlock_op(sbi); | |
edc6d01b | 1525 | filemap_invalidate_unlock(mapping); |
e4544b63 | 1526 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
75cd4e09 CY |
1527 | goto out; |
1528 | } | |
1529 | ||
6e961949 CY |
1530 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
1531 | end = min(pg_end, end_offset - dn.ofs_in_node + index); | |
1532 | ||
1533 | ret = f2fs_do_zero_range(&dn, index, end); | |
75cd4e09 | 1534 | f2fs_put_dnode(&dn); |
c7079853 | 1535 | |
75cd4e09 | 1536 | f2fs_unlock_op(sbi); |
edc6d01b | 1537 | filemap_invalidate_unlock(mapping); |
e4544b63 | 1538 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
9434fcde CY |
1539 | |
1540 | f2fs_balance_fs(sbi, dn.node_changed); | |
1541 | ||
6e961949 CY |
1542 | if (ret) |
1543 | goto out; | |
75cd4e09 | 1544 | |
6e961949 | 1545 | index = end; |
75cd4e09 | 1546 | new_size = max_t(loff_t, new_size, |
6e961949 | 1547 | (loff_t)index << PAGE_SHIFT); |
75cd4e09 CY |
1548 | } |
1549 | ||
1550 | if (off_end) { | |
6394328a CY |
1551 | ret = fill_zero(inode, pg_end, 0, off_end); |
1552 | if (ret) | |
1553 | goto out; | |
1554 | ||
75cd4e09 CY |
1555 | new_size = max_t(loff_t, new_size, offset + len); |
1556 | } | |
1557 | } | |
1558 | ||
1559 | out: | |
17cd07ae CY |
1560 | if (new_size > i_size_read(inode)) { |
1561 | if (mode & FALLOC_FL_KEEP_SIZE) | |
1562 | file_set_keep_isize(inode); | |
1563 | else | |
1564 | f2fs_i_size_write(inode, new_size); | |
1565 | } | |
75cd4e09 CY |
1566 | return ret; |
1567 | } | |
1568 | ||
f62185d0 CY |
1569 | static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) |
1570 | { | |
1571 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
edc6d01b | 1572 | struct address_space *mapping = inode->i_mapping; |
0a2aa8fb | 1573 | pgoff_t nr, pg_start, pg_end, delta, idx; |
f62185d0 | 1574 | loff_t new_size; |
6e2c64ad | 1575 | int ret = 0; |
f62185d0 | 1576 | |
f62185d0 | 1577 | new_size = i_size_read(inode) + len; |
46e82fb1 KM |
1578 | ret = inode_newsize_ok(inode, new_size); |
1579 | if (ret) | |
1580 | return ret; | |
f62185d0 CY |
1581 | |
1582 | if (offset >= i_size_read(inode)) | |
1583 | return -EINVAL; | |
1584 | ||
1585 | /* insert range should be aligned to block size of f2fs. */ | |
1586 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) | |
1587 | return -EINVAL; | |
1588 | ||
b9d777b8 JK |
1589 | ret = f2fs_convert_inline_inode(inode); |
1590 | if (ret) | |
1591 | return ret; | |
97a7b2c2 | 1592 | |
2c4db1a6 | 1593 | f2fs_balance_fs(sbi, true); |
2a340760 | 1594 | |
edc6d01b | 1595 | filemap_invalidate_lock(mapping); |
c42d28ce | 1596 | ret = f2fs_truncate_blocks(inode, i_size_read(inode), true); |
edc6d01b | 1597 | filemap_invalidate_unlock(mapping); |
f62185d0 | 1598 | if (ret) |
6f8d4455 | 1599 | return ret; |
f62185d0 CY |
1600 | |
1601 | /* write out all dirty pages from offset */ | |
edc6d01b | 1602 | ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX); |
f62185d0 | 1603 | if (ret) |
6f8d4455 | 1604 | return ret; |
f62185d0 | 1605 | |
09cbfeaf KS |
1606 | pg_start = offset >> PAGE_SHIFT; |
1607 | pg_end = (offset + len) >> PAGE_SHIFT; | |
f62185d0 | 1608 | delta = pg_end - pg_start; |
f91108b8 | 1609 | idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
0a2aa8fb | 1610 | |
6f8d4455 | 1611 | /* avoid gc operation during block exchange */ |
e4544b63 | 1612 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 1613 | filemap_invalidate_lock(mapping); |
6f8d4455 JK |
1614 | truncate_pagecache(inode, offset); |
1615 | ||
0a2aa8fb JK |
1616 | while (!ret && idx > pg_start) { |
1617 | nr = idx - pg_start; | |
1618 | if (nr > delta) | |
1619 | nr = delta; | |
1620 | idx -= nr; | |
f62185d0 | 1621 | |
f62185d0 | 1622 | f2fs_lock_op(sbi); |
5f281fab JK |
1623 | f2fs_drop_extent_tree(inode); |
1624 | ||
0a2aa8fb JK |
1625 | ret = __exchange_data_block(inode, inode, idx, |
1626 | idx + delta, nr, false); | |
f62185d0 CY |
1627 | f2fs_unlock_op(sbi); |
1628 | } | |
edc6d01b | 1629 | filemap_invalidate_unlock(mapping); |
e4544b63 | 1630 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
f62185d0 | 1631 | |
6e2c64ad | 1632 | /* write out all moved pages, if possible */ |
edc6d01b JK |
1633 | filemap_invalidate_lock(mapping); |
1634 | filemap_write_and_wait_range(mapping, offset, LLONG_MAX); | |
6e2c64ad | 1635 | truncate_pagecache(inode, offset); |
edc6d01b | 1636 | filemap_invalidate_unlock(mapping); |
6e2c64ad JK |
1637 | |
1638 | if (!ret) | |
fc9581c8 | 1639 | f2fs_i_size_write(inode, new_size); |
f62185d0 CY |
1640 | return ret; |
1641 | } | |
1642 | ||
fbfa2cc5 JK |
1643 | static int expand_inode_data(struct inode *inode, loff_t offset, |
1644 | loff_t len, int mode) | |
1645 | { | |
4081363f | 1646 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
d5097be5 | 1647 | struct f2fs_map_blocks map = { .m_next_pgofs = NULL, |
f9d6d059 CY |
1648 | .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE, |
1649 | .m_may_create = true }; | |
d147ea4a JK |
1650 | struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO, |
1651 | .init_gc_type = FG_GC, | |
1652 | .should_migrate_blocks = false, | |
c81d5bae JK |
1653 | .err_gc_skipped = true, |
1654 | .nr_free_secs = 0 }; | |
88f2cfc5 | 1655 | pgoff_t pg_start, pg_end; |
fbfa2cc5 | 1656 | loff_t new_size = i_size_read(inode); |
e12dd7bd | 1657 | loff_t off_end; |
88f2cfc5 | 1658 | block_t expanded = 0; |
a7de6086 | 1659 | int err; |
fbfa2cc5 | 1660 | |
a7de6086 JK |
1661 | err = inode_newsize_ok(inode, (len + offset)); |
1662 | if (err) | |
1663 | return err; | |
fbfa2cc5 | 1664 | |
a7de6086 JK |
1665 | err = f2fs_convert_inline_inode(inode); |
1666 | if (err) | |
1667 | return err; | |
9e09fc85 | 1668 | |
2c4db1a6 | 1669 | f2fs_balance_fs(sbi, true); |
2a340760 | 1670 | |
88f2cfc5 | 1671 | pg_start = ((unsigned long long)offset) >> PAGE_SHIFT; |
e12dd7bd | 1672 | pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT; |
09cbfeaf | 1673 | off_end = (offset + len) & (PAGE_SIZE - 1); |
fbfa2cc5 | 1674 | |
88f2cfc5 CY |
1675 | map.m_lblk = pg_start; |
1676 | map.m_len = pg_end - pg_start; | |
e12dd7bd JK |
1677 | if (off_end) |
1678 | map.m_len++; | |
ead43275 | 1679 | |
f5a53edc JK |
1680 | if (!map.m_len) |
1681 | return 0; | |
1682 | ||
1683 | if (f2fs_is_pinned_file(inode)) { | |
e1175f02 CY |
1684 | block_t sec_blks = BLKS_PER_SEC(sbi); |
1685 | block_t sec_len = roundup(map.m_len, sec_blks); | |
f5a53edc | 1686 | |
e1175f02 | 1687 | map.m_len = sec_blks; |
f5a53edc JK |
1688 | next_alloc: |
1689 | if (has_not_enough_free_secs(sbi, 0, | |
1690 | GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) { | |
e4544b63 | 1691 | f2fs_down_write(&sbi->gc_lock); |
d147ea4a | 1692 | err = f2fs_gc(sbi, &gc_control); |
2e42b7f8 | 1693 | if (err && err != -ENODATA) |
f5a53edc JK |
1694 | goto out_err; |
1695 | } | |
1696 | ||
e4544b63 | 1697 | f2fs_down_write(&sbi->pin_sem); |
fd612648 DJ |
1698 | |
1699 | f2fs_lock_op(sbi); | |
509f1010 | 1700 | f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false); |
fd612648 DJ |
1701 | f2fs_unlock_op(sbi); |
1702 | ||
d0b9e42a | 1703 | map.m_seg_type = CURSEG_COLD_DATA_PINNED; |
f5a53edc | 1704 | err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO); |
d4dd19ec | 1705 | file_dont_truncate(inode); |
d0b9e42a | 1706 | |
e4544b63 | 1707 | f2fs_up_write(&sbi->pin_sem); |
cad3836f | 1708 | |
88f2cfc5 | 1709 | expanded += map.m_len; |
e1175f02 | 1710 | sec_len -= map.m_len; |
f5a53edc | 1711 | map.m_lblk += map.m_len; |
e1175f02 | 1712 | if (!err && sec_len) |
f5a53edc JK |
1713 | goto next_alloc; |
1714 | ||
88f2cfc5 | 1715 | map.m_len = expanded; |
f5a53edc JK |
1716 | } else { |
1717 | err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); | |
88f2cfc5 | 1718 | expanded = map.m_len; |
f5a53edc JK |
1719 | } |
1720 | out_err: | |
a7de6086 | 1721 | if (err) { |
e12dd7bd | 1722 | pgoff_t last_off; |
fbfa2cc5 | 1723 | |
88f2cfc5 | 1724 | if (!expanded) |
a7de6086 | 1725 | return err; |
98397ff3 | 1726 | |
88f2cfc5 | 1727 | last_off = pg_start + expanded - 1; |
e12dd7bd JK |
1728 | |
1729 | /* update new size to the failed position */ | |
1061fd48 | 1730 | new_size = (last_off == pg_end) ? offset + len : |
e12dd7bd JK |
1731 | (loff_t)(last_off + 1) << PAGE_SHIFT; |
1732 | } else { | |
1733 | new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end; | |
fbfa2cc5 JK |
1734 | } |
1735 | ||
e8ed90a6 CY |
1736 | if (new_size > i_size_read(inode)) { |
1737 | if (mode & FALLOC_FL_KEEP_SIZE) | |
1738 | file_set_keep_isize(inode); | |
1739 | else | |
1740 | f2fs_i_size_write(inode, new_size); | |
1741 | } | |
fbfa2cc5 | 1742 | |
a7de6086 | 1743 | return err; |
fbfa2cc5 JK |
1744 | } |
1745 | ||
1746 | static long f2fs_fallocate(struct file *file, int mode, | |
1747 | loff_t offset, loff_t len) | |
1748 | { | |
6131ffaa | 1749 | struct inode *inode = file_inode(file); |
587c0a42 | 1750 | long ret = 0; |
fbfa2cc5 | 1751 | |
1f227a3e JK |
1752 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
1753 | return -EIO; | |
00e09c0b CY |
1754 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode))) |
1755 | return -ENOSPC; | |
4c8ff709 CY |
1756 | if (!f2fs_is_compress_backend_ready(inode)) |
1757 | return -EOPNOTSUPP; | |
1f227a3e | 1758 | |
c998012b CY |
1759 | /* f2fs only support ->fallocate for regular file */ |
1760 | if (!S_ISREG(inode->i_mode)) | |
1761 | return -EINVAL; | |
1762 | ||
62230e0d | 1763 | if (IS_ENCRYPTED(inode) && |
f62185d0 | 1764 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
fcc85a4d JK |
1765 | return -EOPNOTSUPP; |
1766 | ||
5fed0be8 JK |
1767 | /* |
1768 | * Pinned file should not support partial trucation since the block | |
1769 | * can be used by applications. | |
1770 | */ | |
1771 | if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) && | |
4c8ff709 CY |
1772 | (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | |
1773 | FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) | |
1774 | return -EOPNOTSUPP; | |
1775 | ||
b4ace337 | 1776 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
f62185d0 CY |
1777 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
1778 | FALLOC_FL_INSERT_RANGE)) | |
fbfa2cc5 JK |
1779 | return -EOPNOTSUPP; |
1780 | ||
5955102c | 1781 | inode_lock(inode); |
3375f696 | 1782 | |
587c0a42 TY |
1783 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
1784 | if (offset >= inode->i_size) | |
1785 | goto out; | |
1786 | ||
a66c7b2f | 1787 | ret = punch_hole(inode, offset, len); |
b4ace337 CY |
1788 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
1789 | ret = f2fs_collapse_range(inode, offset, len); | |
75cd4e09 CY |
1790 | } else if (mode & FALLOC_FL_ZERO_RANGE) { |
1791 | ret = f2fs_zero_range(inode, offset, len, mode); | |
f62185d0 CY |
1792 | } else if (mode & FALLOC_FL_INSERT_RANGE) { |
1793 | ret = f2fs_insert_range(inode, offset, len); | |
b4ace337 | 1794 | } else { |
fbfa2cc5 | 1795 | ret = expand_inode_data(inode, offset, len, mode); |
b4ace337 | 1796 | } |
fbfa2cc5 | 1797 | |
3af60a49 | 1798 | if (!ret) { |
078cd827 | 1799 | inode->i_mtime = inode->i_ctime = current_time(inode); |
7c45729a | 1800 | f2fs_mark_inode_dirty_sync(inode, false); |
d0239e1b | 1801 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
3af60a49 | 1802 | } |
3375f696 | 1803 | |
587c0a42 | 1804 | out: |
5955102c | 1805 | inode_unlock(inode); |
3375f696 | 1806 | |
c01e2853 | 1807 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
fbfa2cc5 JK |
1808 | return ret; |
1809 | } | |
1810 | ||
1e84371f JK |
1811 | static int f2fs_release_file(struct inode *inode, struct file *filp) |
1812 | { | |
de5307e4 JK |
1813 | /* |
1814 | * f2fs_relase_file is called at every close calls. So we should | |
1815 | * not drop any inmemory pages by close called by other process. | |
1816 | */ | |
1817 | if (!(filp->f_mode & FMODE_WRITE) || | |
1818 | atomic_read(&inode->i_writecount) != 1) | |
1819 | return 0; | |
1820 | ||
1e84371f | 1821 | if (f2fs_is_atomic_file(inode)) |
3db1de0e | 1822 | f2fs_abort_atomic_write(inode, true); |
1e84371f JK |
1823 | return 0; |
1824 | } | |
1825 | ||
7a10f017 | 1826 | static int f2fs_file_flush(struct file *file, fl_owner_t id) |
fbfa2cc5 | 1827 | { |
7a10f017 JK |
1828 | struct inode *inode = file_inode(file); |
1829 | ||
1830 | /* | |
1831 | * If the process doing a transaction is crashed, we should do | |
1832 | * roll-back. Otherwise, other reader/write can see corrupted database | |
1833 | * until all the writers close its file. Since this should be done | |
1834 | * before dropping file lock, it needs to do in ->flush. | |
1835 | */ | |
1836 | if (f2fs_is_atomic_file(inode) && | |
3db1de0e DJ |
1837 | F2FS_I(inode)->atomic_write_task == current) |
1838 | f2fs_abort_atomic_write(inode, true); | |
7a10f017 | 1839 | return 0; |
fbfa2cc5 JK |
1840 | } |
1841 | ||
36098557 | 1842 | static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask) |
2c1d0305 CY |
1843 | { |
1844 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
99eabb91 JK |
1845 | u32 masked_flags = fi->i_flags & mask; |
1846 | ||
a7531039 JK |
1847 | /* mask can be shrunk by flags_valid selector */ |
1848 | iflags &= mask; | |
2c1d0305 CY |
1849 | |
1850 | /* Is it quota file? Do not allow user to mess with it */ | |
1851 | if (IS_NOQUOTA(inode)) | |
1852 | return -EPERM; | |
1853 | ||
99eabb91 | 1854 | if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) { |
2c2eb7a3 DR |
1855 | if (!f2fs_sb_has_casefold(F2FS_I_SB(inode))) |
1856 | return -EOPNOTSUPP; | |
1857 | if (!f2fs_empty_dir(inode)) | |
1858 | return -ENOTEMPTY; | |
1859 | } | |
1860 | ||
4c8ff709 CY |
1861 | if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) { |
1862 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
1863 | return -EOPNOTSUPP; | |
1864 | if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL)) | |
1865 | return -EINVAL; | |
1866 | } | |
1867 | ||
99eabb91 | 1868 | if ((iflags ^ masked_flags) & F2FS_COMPR_FL) { |
aa576970 | 1869 | if (masked_flags & F2FS_COMPR_FL) { |
78134d03 | 1870 | if (!f2fs_disable_compressed_file(inode)) |
2536ac68 CY |
1871 | return -EINVAL; |
1872 | } | |
4c8ff709 CY |
1873 | if (iflags & F2FS_NOCOMP_FL) |
1874 | return -EINVAL; | |
1875 | if (iflags & F2FS_COMPR_FL) { | |
4c8ff709 CY |
1876 | if (!f2fs_may_compress(inode)) |
1877 | return -EINVAL; | |
519a5a2f CY |
1878 | if (S_ISREG(inode->i_mode) && inode->i_size) |
1879 | return -EINVAL; | |
4c8ff709 CY |
1880 | |
1881 | set_compress_context(inode); | |
1882 | } | |
1883 | } | |
99eabb91 JK |
1884 | if ((iflags ^ masked_flags) & F2FS_NOCOMP_FL) { |
1885 | if (masked_flags & F2FS_COMPR_FL) | |
4c8ff709 CY |
1886 | return -EINVAL; |
1887 | } | |
1888 | ||
d5e5efa2 | 1889 | fi->i_flags = iflags | (fi->i_flags & ~mask); |
4c8ff709 CY |
1890 | f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) && |
1891 | (fi->i_flags & F2FS_NOCOMP_FL)); | |
2c1d0305 | 1892 | |
59c84408 | 1893 | if (fi->i_flags & F2FS_PROJINHERIT_FL) |
2c1d0305 CY |
1894 | set_inode_flag(inode, FI_PROJ_INHERIT); |
1895 | else | |
1896 | clear_inode_flag(inode, FI_PROJ_INHERIT); | |
1897 | ||
1898 | inode->i_ctime = current_time(inode); | |
1899 | f2fs_set_inode_flags(inode); | |
b32e0190 | 1900 | f2fs_mark_inode_dirty_sync(inode, true); |
2c1d0305 CY |
1901 | return 0; |
1902 | } | |
1903 | ||
9b1bb01c | 1904 | /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */ |
36098557 EB |
1905 | |
1906 | /* | |
1907 | * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry | |
1908 | * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to | |
1909 | * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add | |
1910 | * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL. | |
9b1bb01c MS |
1911 | * |
1912 | * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and | |
1913 | * FS_IOC_FSSETXATTR is done by the VFS. | |
36098557 EB |
1914 | */ |
1915 | ||
1916 | static const struct { | |
1917 | u32 iflag; | |
1918 | u32 fsflag; | |
1919 | } f2fs_fsflags_map[] = { | |
4c8ff709 | 1920 | { F2FS_COMPR_FL, FS_COMPR_FL }, |
36098557 EB |
1921 | { F2FS_SYNC_FL, FS_SYNC_FL }, |
1922 | { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL }, | |
1923 | { F2FS_APPEND_FL, FS_APPEND_FL }, | |
1924 | { F2FS_NODUMP_FL, FS_NODUMP_FL }, | |
1925 | { F2FS_NOATIME_FL, FS_NOATIME_FL }, | |
4c8ff709 | 1926 | { F2FS_NOCOMP_FL, FS_NOCOMP_FL }, |
36098557 EB |
1927 | { F2FS_INDEX_FL, FS_INDEX_FL }, |
1928 | { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL }, | |
1929 | { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL }, | |
2c2eb7a3 | 1930 | { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL }, |
36098557 EB |
1931 | }; |
1932 | ||
1933 | #define F2FS_GETTABLE_FS_FL ( \ | |
4c8ff709 | 1934 | FS_COMPR_FL | \ |
36098557 EB |
1935 | FS_SYNC_FL | \ |
1936 | FS_IMMUTABLE_FL | \ | |
1937 | FS_APPEND_FL | \ | |
1938 | FS_NODUMP_FL | \ | |
1939 | FS_NOATIME_FL | \ | |
4c8ff709 | 1940 | FS_NOCOMP_FL | \ |
36098557 EB |
1941 | FS_INDEX_FL | \ |
1942 | FS_DIRSYNC_FL | \ | |
1943 | FS_PROJINHERIT_FL | \ | |
1944 | FS_ENCRYPT_FL | \ | |
1945 | FS_INLINE_DATA_FL | \ | |
95ae251f | 1946 | FS_NOCOW_FL | \ |
fbc246a1 | 1947 | FS_VERITY_FL | \ |
2c2eb7a3 | 1948 | FS_CASEFOLD_FL) |
36098557 EB |
1949 | |
1950 | #define F2FS_SETTABLE_FS_FL ( \ | |
4c8ff709 | 1951 | FS_COMPR_FL | \ |
36098557 EB |
1952 | FS_SYNC_FL | \ |
1953 | FS_IMMUTABLE_FL | \ | |
1954 | FS_APPEND_FL | \ | |
1955 | FS_NODUMP_FL | \ | |
1956 | FS_NOATIME_FL | \ | |
4c8ff709 | 1957 | FS_NOCOMP_FL | \ |
36098557 | 1958 | FS_DIRSYNC_FL | \ |
2c2eb7a3 DR |
1959 | FS_PROJINHERIT_FL | \ |
1960 | FS_CASEFOLD_FL) | |
36098557 EB |
1961 | |
1962 | /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */ | |
1963 | static inline u32 f2fs_iflags_to_fsflags(u32 iflags) | |
1964 | { | |
1965 | u32 fsflags = 0; | |
1966 | int i; | |
1967 | ||
1968 | for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++) | |
1969 | if (iflags & f2fs_fsflags_map[i].iflag) | |
1970 | fsflags |= f2fs_fsflags_map[i].fsflag; | |
1971 | ||
1972 | return fsflags; | |
1973 | } | |
1974 | ||
1975 | /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */ | |
1976 | static inline u32 f2fs_fsflags_to_iflags(u32 fsflags) | |
1977 | { | |
1978 | u32 iflags = 0; | |
1979 | int i; | |
1980 | ||
1981 | for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++) | |
1982 | if (fsflags & f2fs_fsflags_map[i].fsflag) | |
1983 | iflags |= f2fs_fsflags_map[i].iflag; | |
1984 | ||
1985 | return iflags; | |
1986 | } | |
1987 | ||
d49f3e89 CY |
1988 | static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) |
1989 | { | |
1990 | struct inode *inode = file_inode(filp); | |
1991 | ||
1992 | return put_user(inode->i_generation, (int __user *)arg); | |
1993 | } | |
1994 | ||
88b88a66 JK |
1995 | static int f2fs_ioc_start_atomic_write(struct file *filp) |
1996 | { | |
1997 | struct inode *inode = file_inode(filp); | |
984fc4e7 | 1998 | struct user_namespace *mnt_userns = file_mnt_user_ns(filp); |
743b620c JK |
1999 | struct f2fs_inode_info *fi = F2FS_I(inode); |
2000 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3db1de0e | 2001 | struct inode *pinode; |
f4c9c743 | 2002 | int ret; |
88b88a66 | 2003 | |
984fc4e7 | 2004 | if (!inode_owner_or_capable(mnt_userns, inode)) |
88b88a66 JK |
2005 | return -EACCES; |
2006 | ||
e811898c JK |
2007 | if (!S_ISREG(inode->i_mode)) |
2008 | return -EINVAL; | |
2009 | ||
038d0698 CY |
2010 | if (filp->f_flags & O_DIRECT) |
2011 | return -EINVAL; | |
2012 | ||
7fb17fe4 CY |
2013 | ret = mnt_want_write_file(filp); |
2014 | if (ret) | |
2015 | return ret; | |
2016 | ||
0fac558b CY |
2017 | inode_lock(inode); |
2018 | ||
9b56adcf FC |
2019 | if (!f2fs_disable_compressed_file(inode)) { |
2020 | ret = -EINVAL; | |
2021 | goto out; | |
2022 | } | |
4c8ff709 | 2023 | |
3db1de0e | 2024 | if (f2fs_is_atomic_file(inode)) |
7fb17fe4 | 2025 | goto out; |
88b88a66 | 2026 | |
f4c9c743 CY |
2027 | ret = f2fs_convert_inline_inode(inode); |
2028 | if (ret) | |
7fb17fe4 | 2029 | goto out; |
88b88a66 | 2030 | |
e4544b63 | 2031 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
6f8d4455 | 2032 | |
31867b23 JK |
2033 | /* |
2034 | * Should wait end_io to count F2FS_WB_CP_DATA correctly by | |
2035 | * f2fs_is_atomic_file. | |
2036 | */ | |
2037 | if (get_dirty_pages(inode)) | |
dcbb4c10 JP |
2038 | f2fs_warn(F2FS_I_SB(inode), "Unexpected flush for atomic writes: ino=%lu, npages=%u", |
2039 | inode->i_ino, get_dirty_pages(inode)); | |
c27753d6 | 2040 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); |
6f8d4455 | 2041 | if (ret) { |
e4544b63 | 2042 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
684ca7e5 | 2043 | goto out; |
6f8d4455 | 2044 | } |
31867b23 | 2045 | |
3db1de0e DJ |
2046 | /* Create a COW inode for atomic write */ |
2047 | pinode = f2fs_iget(inode->i_sb, fi->i_pino); | |
2048 | if (IS_ERR(pinode)) { | |
2049 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
2050 | ret = PTR_ERR(pinode); | |
2051 | goto out; | |
2052 | } | |
2053 | ||
2054 | ret = f2fs_get_tmpfile(mnt_userns, pinode, &fi->cow_inode); | |
2055 | iput(pinode); | |
2056 | if (ret) { | |
2057 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
2058 | goto out; | |
2059 | } | |
2060 | f2fs_i_size_write(fi->cow_inode, i_size_read(inode)); | |
2061 | ||
743b620c | 2062 | spin_lock(&sbi->inode_lock[ATOMIC_FILE]); |
677017d1 | 2063 | sbi->atomic_files++; |
743b620c JK |
2064 | spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); |
2065 | ||
054afda9 | 2066 | set_inode_flag(inode, FI_ATOMIC_FILE); |
3db1de0e DJ |
2067 | set_inode_flag(fi->cow_inode, FI_ATOMIC_FILE); |
2068 | clear_inode_flag(fi->cow_inode, FI_INLINE_DATA); | |
e4544b63 | 2069 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
684ca7e5 | 2070 | |
6f8d4455 | 2071 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
3db1de0e | 2072 | F2FS_I(inode)->atomic_write_task = current; |
26a28a0c | 2073 | stat_update_max_atomic_write(inode); |
684ca7e5 | 2074 | out: |
0fac558b | 2075 | inode_unlock(inode); |
7fb17fe4 | 2076 | mnt_drop_write_file(filp); |
c27753d6 | 2077 | return ret; |
88b88a66 JK |
2078 | } |
2079 | ||
2080 | static int f2fs_ioc_commit_atomic_write(struct file *filp) | |
2081 | { | |
2082 | struct inode *inode = file_inode(filp); | |
984fc4e7 | 2083 | struct user_namespace *mnt_userns = file_mnt_user_ns(filp); |
88b88a66 JK |
2084 | int ret; |
2085 | ||
984fc4e7 | 2086 | if (!inode_owner_or_capable(mnt_userns, inode)) |
88b88a66 JK |
2087 | return -EACCES; |
2088 | ||
2089 | ret = mnt_want_write_file(filp); | |
2090 | if (ret) | |
2091 | return ret; | |
2092 | ||
6f8d4455 | 2093 | f2fs_balance_fs(F2FS_I_SB(inode), true); |
0fac558b | 2094 | |
6f8d4455 | 2095 | inode_lock(inode); |
1dc0f899 | 2096 | |
6282adbf | 2097 | if (f2fs_is_atomic_file(inode)) { |
3db1de0e | 2098 | ret = f2fs_commit_atomic_write(inode); |
5fe45743 | 2099 | if (ret) |
7bc155fe | 2100 | goto unlock_out; |
5fe45743 | 2101 | |
26a28a0c | 2102 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); |
743b620c | 2103 | if (!ret) |
3db1de0e | 2104 | f2fs_abort_atomic_write(inode, false); |
26a28a0c | 2105 | } else { |
774e1b78 | 2106 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false); |
6282adbf | 2107 | } |
7bc155fe | 2108 | unlock_out: |
0fac558b | 2109 | inode_unlock(inode); |
88b88a66 JK |
2110 | mnt_drop_write_file(filp); |
2111 | return ret; | |
2112 | } | |
2113 | ||
1abff93d JK |
2114 | static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) |
2115 | { | |
2116 | struct inode *inode = file_inode(filp); | |
2117 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2118 | struct super_block *sb = sbi->sb; | |
2119 | __u32 in; | |
2a96d8ad | 2120 | int ret = 0; |
1abff93d JK |
2121 | |
2122 | if (!capable(CAP_SYS_ADMIN)) | |
2123 | return -EPERM; | |
2124 | ||
2125 | if (get_user(in, (__u32 __user *)arg)) | |
2126 | return -EFAULT; | |
2127 | ||
60b2b4ee ST |
2128 | if (in != F2FS_GOING_DOWN_FULLSYNC) { |
2129 | ret = mnt_want_write_file(filp); | |
8626441f CY |
2130 | if (ret) { |
2131 | if (ret == -EROFS) { | |
2132 | ret = 0; | |
2133 | f2fs_stop_checkpoint(sbi, false); | |
2134 | set_sbi_flag(sbi, SBI_IS_SHUTDOWN); | |
2135 | trace_f2fs_shutdown(sbi, in, ret); | |
2136 | } | |
60b2b4ee | 2137 | return ret; |
8626441f | 2138 | } |
60b2b4ee | 2139 | } |
7fb17fe4 | 2140 | |
1abff93d JK |
2141 | switch (in) { |
2142 | case F2FS_GOING_DOWN_FULLSYNC: | |
040f04bd CH |
2143 | ret = freeze_bdev(sb->s_bdev); |
2144 | if (ret) | |
d027c484 | 2145 | goto out; |
040f04bd CH |
2146 | f2fs_stop_checkpoint(sbi, false); |
2147 | set_sbi_flag(sbi, SBI_IS_SHUTDOWN); | |
2148 | thaw_bdev(sb->s_bdev); | |
1abff93d JK |
2149 | break; |
2150 | case F2FS_GOING_DOWN_METASYNC: | |
2151 | /* do checkpoint only */ | |
d027c484 CY |
2152 | ret = f2fs_sync_fs(sb, 1); |
2153 | if (ret) | |
2154 | goto out; | |
38f91ca8 | 2155 | f2fs_stop_checkpoint(sbi, false); |
83a3bfdb | 2156 | set_sbi_flag(sbi, SBI_IS_SHUTDOWN); |
1abff93d JK |
2157 | break; |
2158 | case F2FS_GOING_DOWN_NOSYNC: | |
38f91ca8 | 2159 | f2fs_stop_checkpoint(sbi, false); |
83a3bfdb | 2160 | set_sbi_flag(sbi, SBI_IS_SHUTDOWN); |
1abff93d | 2161 | break; |
c912a829 | 2162 | case F2FS_GOING_DOWN_METAFLUSH: |
4d57b86d | 2163 | f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO); |
38f91ca8 | 2164 | f2fs_stop_checkpoint(sbi, false); |
83a3bfdb | 2165 | set_sbi_flag(sbi, SBI_IS_SHUTDOWN); |
c912a829 | 2166 | break; |
0cd6d9b0 JK |
2167 | case F2FS_GOING_DOWN_NEED_FSCK: |
2168 | set_sbi_flag(sbi, SBI_NEED_FSCK); | |
db610a64 JK |
2169 | set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); |
2170 | set_sbi_flag(sbi, SBI_IS_DIRTY); | |
0cd6d9b0 JK |
2171 | /* do checkpoint only */ |
2172 | ret = f2fs_sync_fs(sb, 1); | |
db610a64 | 2173 | goto out; |
1abff93d | 2174 | default: |
7fb17fe4 CY |
2175 | ret = -EINVAL; |
2176 | goto out; | |
1abff93d | 2177 | } |
7950e9ac | 2178 | |
4d57b86d CY |
2179 | f2fs_stop_gc_thread(sbi); |
2180 | f2fs_stop_discard_thread(sbi); | |
7950e9ac | 2181 | |
4d57b86d | 2182 | f2fs_drop_discard_cmd(sbi); |
7950e9ac CY |
2183 | clear_opt(sbi, DISCARD); |
2184 | ||
d0239e1b | 2185 | f2fs_update_time(sbi, REQ_TIME); |
7fb17fe4 | 2186 | out: |
60b2b4ee ST |
2187 | if (in != F2FS_GOING_DOWN_FULLSYNC) |
2188 | mnt_drop_write_file(filp); | |
559e87c4 CY |
2189 | |
2190 | trace_f2fs_shutdown(sbi, in, ret); | |
2191 | ||
7fb17fe4 | 2192 | return ret; |
1abff93d JK |
2193 | } |
2194 | ||
52656e6c JK |
2195 | static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) |
2196 | { | |
2197 | struct inode *inode = file_inode(filp); | |
2198 | struct super_block *sb = inode->i_sb; | |
2199 | struct request_queue *q = bdev_get_queue(sb->s_bdev); | |
2200 | struct fstrim_range range; | |
2201 | int ret; | |
4b2fecc8 | 2202 | |
52656e6c JK |
2203 | if (!capable(CAP_SYS_ADMIN)) |
2204 | return -EPERM; | |
4b2fecc8 | 2205 | |
7d20c8ab | 2206 | if (!f2fs_hw_support_discard(F2FS_SB(sb))) |
52656e6c | 2207 | return -EOPNOTSUPP; |
4b2fecc8 | 2208 | |
52656e6c JK |
2209 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
2210 | sizeof(range))) | |
2211 | return -EFAULT; | |
4b2fecc8 | 2212 | |
7fb17fe4 CY |
2213 | ret = mnt_want_write_file(filp); |
2214 | if (ret) | |
2215 | return ret; | |
2216 | ||
52656e6c JK |
2217 | range.minlen = max((unsigned int)range.minlen, |
2218 | q->limits.discard_granularity); | |
2219 | ret = f2fs_trim_fs(F2FS_SB(sb), &range); | |
7fb17fe4 | 2220 | mnt_drop_write_file(filp); |
52656e6c JK |
2221 | if (ret < 0) |
2222 | return ret; | |
4b2fecc8 | 2223 | |
52656e6c JK |
2224 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
2225 | sizeof(range))) | |
2226 | return -EFAULT; | |
d0239e1b | 2227 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
52656e6c JK |
2228 | return 0; |
2229 | } | |
2230 | ||
f424f664 JK |
2231 | static bool uuid_is_nonzero(__u8 u[16]) |
2232 | { | |
2233 | int i; | |
2234 | ||
2235 | for (i = 0; i < 16; i++) | |
2236 | if (u[i]) | |
2237 | return true; | |
2238 | return false; | |
2239 | } | |
2240 | ||
2241 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) | |
2242 | { | |
f424f664 JK |
2243 | struct inode *inode = file_inode(filp); |
2244 | ||
7beb01f7 | 2245 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode))) |
ead710b7 CY |
2246 | return -EOPNOTSUPP; |
2247 | ||
d0239e1b | 2248 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
7fb17fe4 | 2249 | |
db717d8e | 2250 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
f424f664 JK |
2251 | } |
2252 | ||
2253 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) | |
2254 | { | |
7beb01f7 | 2255 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) |
ead710b7 | 2256 | return -EOPNOTSUPP; |
db717d8e | 2257 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
f424f664 JK |
2258 | } |
2259 | ||
2260 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) | |
2261 | { | |
2262 | struct inode *inode = file_inode(filp); | |
2263 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2264 | int err; | |
2265 | ||
7beb01f7 | 2266 | if (!f2fs_sb_has_encrypt(sbi)) |
f424f664 JK |
2267 | return -EOPNOTSUPP; |
2268 | ||
f424f664 JK |
2269 | err = mnt_want_write_file(filp); |
2270 | if (err) | |
2271 | return err; | |
2272 | ||
e4544b63 | 2273 | f2fs_down_write(&sbi->sb_lock); |
d0d3f1b3 CY |
2274 | |
2275 | if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) | |
2276 | goto got_it; | |
2277 | ||
f424f664 JK |
2278 | /* update superblock with uuid */ |
2279 | generate_random_uuid(sbi->raw_super->encrypt_pw_salt); | |
2280 | ||
c5bda1c8 | 2281 | err = f2fs_commit_super(sbi, false); |
f424f664 JK |
2282 | if (err) { |
2283 | /* undo new data */ | |
2284 | memset(sbi->raw_super->encrypt_pw_salt, 0, 16); | |
d0d3f1b3 | 2285 | goto out_err; |
f424f664 JK |
2286 | } |
2287 | got_it: | |
2288 | if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, | |
2289 | 16)) | |
d0d3f1b3 CY |
2290 | err = -EFAULT; |
2291 | out_err: | |
e4544b63 | 2292 | f2fs_up_write(&sbi->sb_lock); |
d0d3f1b3 CY |
2293 | mnt_drop_write_file(filp); |
2294 | return err; | |
f424f664 JK |
2295 | } |
2296 | ||
8ce589c7 EB |
2297 | static int f2fs_ioc_get_encryption_policy_ex(struct file *filp, |
2298 | unsigned long arg) | |
2299 | { | |
2300 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2301 | return -EOPNOTSUPP; | |
2302 | ||
2303 | return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); | |
2304 | } | |
2305 | ||
2306 | static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg) | |
2307 | { | |
2308 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2309 | return -EOPNOTSUPP; | |
2310 | ||
2311 | return fscrypt_ioctl_add_key(filp, (void __user *)arg); | |
2312 | } | |
2313 | ||
2314 | static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg) | |
2315 | { | |
2316 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2317 | return -EOPNOTSUPP; | |
2318 | ||
2319 | return fscrypt_ioctl_remove_key(filp, (void __user *)arg); | |
2320 | } | |
2321 | ||
2322 | static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp, | |
2323 | unsigned long arg) | |
2324 | { | |
2325 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2326 | return -EOPNOTSUPP; | |
2327 | ||
2328 | return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg); | |
2329 | } | |
2330 | ||
2331 | static int f2fs_ioc_get_encryption_key_status(struct file *filp, | |
2332 | unsigned long arg) | |
2333 | { | |
2334 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2335 | return -EOPNOTSUPP; | |
2336 | ||
2337 | return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); | |
2338 | } | |
2339 | ||
ee446e1a EB |
2340 | static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg) |
2341 | { | |
2342 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | |
2343 | return -EOPNOTSUPP; | |
2344 | ||
2345 | return fscrypt_ioctl_get_nonce(filp, (void __user *)arg); | |
2346 | } | |
2347 | ||
c1c1b583 CY |
2348 | static int f2fs_ioc_gc(struct file *filp, unsigned long arg) |
2349 | { | |
2350 | struct inode *inode = file_inode(filp); | |
2351 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
d147ea4a JK |
2352 | struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO, |
2353 | .no_bg_gc = false, | |
c81d5bae JK |
2354 | .should_migrate_blocks = false, |
2355 | .nr_free_secs = 0 }; | |
d530d4d8 | 2356 | __u32 sync; |
7fb17fe4 | 2357 | int ret; |
c1c1b583 CY |
2358 | |
2359 | if (!capable(CAP_SYS_ADMIN)) | |
2360 | return -EPERM; | |
2361 | ||
d530d4d8 | 2362 | if (get_user(sync, (__u32 __user *)arg)) |
c1c1b583 CY |
2363 | return -EFAULT; |
2364 | ||
d530d4d8 CY |
2365 | if (f2fs_readonly(sbi->sb)) |
2366 | return -EROFS; | |
c1c1b583 | 2367 | |
7fb17fe4 CY |
2368 | ret = mnt_want_write_file(filp); |
2369 | if (ret) | |
2370 | return ret; | |
2371 | ||
d530d4d8 | 2372 | if (!sync) { |
e4544b63 | 2373 | if (!f2fs_down_write_trylock(&sbi->gc_lock)) { |
7fb17fe4 CY |
2374 | ret = -EBUSY; |
2375 | goto out; | |
2376 | } | |
d530d4d8 | 2377 | } else { |
e4544b63 | 2378 | f2fs_down_write(&sbi->gc_lock); |
c1c1b583 CY |
2379 | } |
2380 | ||
d147ea4a JK |
2381 | gc_control.init_gc_type = sync ? FG_GC : BG_GC; |
2382 | gc_control.err_gc_skipped = sync; | |
2383 | ret = f2fs_gc(sbi, &gc_control); | |
7fb17fe4 CY |
2384 | out: |
2385 | mnt_drop_write_file(filp); | |
2386 | return ret; | |
c1c1b583 CY |
2387 | } |
2388 | ||
34178b1b | 2389 | static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range) |
34dc77ad | 2390 | { |
34178b1b | 2391 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); |
d147ea4a JK |
2392 | struct f2fs_gc_control gc_control = { |
2393 | .init_gc_type = range->sync ? FG_GC : BG_GC, | |
2394 | .no_bg_gc = false, | |
2395 | .should_migrate_blocks = false, | |
c81d5bae JK |
2396 | .err_gc_skipped = range->sync, |
2397 | .nr_free_secs = 0 }; | |
34dc77ad JK |
2398 | u64 end; |
2399 | int ret; | |
2400 | ||
2401 | if (!capable(CAP_SYS_ADMIN)) | |
2402 | return -EPERM; | |
34dc77ad JK |
2403 | if (f2fs_readonly(sbi->sb)) |
2404 | return -EROFS; | |
2405 | ||
34178b1b CY |
2406 | end = range->start + range->len; |
2407 | if (end < range->start || range->start < MAIN_BLKADDR(sbi) || | |
fbbf7799 | 2408 | end >= MAX_BLKADDR(sbi)) |
b82f6e34 | 2409 | return -EINVAL; |
b82f6e34 | 2410 | |
34dc77ad JK |
2411 | ret = mnt_want_write_file(filp); |
2412 | if (ret) | |
2413 | return ret; | |
2414 | ||
34dc77ad | 2415 | do_more: |
34178b1b | 2416 | if (!range->sync) { |
e4544b63 | 2417 | if (!f2fs_down_write_trylock(&sbi->gc_lock)) { |
34dc77ad JK |
2418 | ret = -EBUSY; |
2419 | goto out; | |
2420 | } | |
2421 | } else { | |
e4544b63 | 2422 | f2fs_down_write(&sbi->gc_lock); |
34dc77ad JK |
2423 | } |
2424 | ||
d147ea4a JK |
2425 | gc_control.victim_segno = GET_SEGNO(sbi, range->start); |
2426 | ret = f2fs_gc(sbi, &gc_control); | |
97767500 QZ |
2427 | if (ret) { |
2428 | if (ret == -EBUSY) | |
2429 | ret = -EAGAIN; | |
2430 | goto out; | |
2431 | } | |
34178b1b CY |
2432 | range->start += BLKS_PER_SEC(sbi); |
2433 | if (range->start <= end) | |
34dc77ad JK |
2434 | goto do_more; |
2435 | out: | |
2436 | mnt_drop_write_file(filp); | |
2437 | return ret; | |
2438 | } | |
2439 | ||
34178b1b CY |
2440 | static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) |
2441 | { | |
2442 | struct f2fs_gc_range range; | |
2443 | ||
2444 | if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg, | |
2445 | sizeof(range))) | |
2446 | return -EFAULT; | |
2447 | return __f2fs_ioc_gc_range(filp, &range); | |
2448 | } | |
2449 | ||
059c0648 | 2450 | static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) |
456b88e4 CY |
2451 | { |
2452 | struct inode *inode = file_inode(filp); | |
2453 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
7fb17fe4 | 2454 | int ret; |
456b88e4 CY |
2455 | |
2456 | if (!capable(CAP_SYS_ADMIN)) | |
2457 | return -EPERM; | |
2458 | ||
2459 | if (f2fs_readonly(sbi->sb)) | |
2460 | return -EROFS; | |
2461 | ||
4354994f | 2462 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { |
dcbb4c10 | 2463 | f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled."); |
4354994f DR |
2464 | return -EINVAL; |
2465 | } | |
2466 | ||
7fb17fe4 CY |
2467 | ret = mnt_want_write_file(filp); |
2468 | if (ret) | |
2469 | return ret; | |
2470 | ||
2471 | ret = f2fs_sync_fs(sbi->sb, 1); | |
2472 | ||
2473 | mnt_drop_write_file(filp); | |
2474 | return ret; | |
456b88e4 CY |
2475 | } |
2476 | ||
d323d005 CY |
2477 | static int f2fs_defragment_range(struct f2fs_sb_info *sbi, |
2478 | struct file *filp, | |
2479 | struct f2fs_defragment *range) | |
2480 | { | |
2481 | struct inode *inode = file_inode(filp); | |
f3d98e74 | 2482 | struct f2fs_map_blocks map = { .m_next_extent = NULL, |
5f029c04 | 2483 | .m_seg_type = NO_CHECK_TYPE, |
f4f0b677 | 2484 | .m_may_create = false }; |
1061fd48 | 2485 | struct extent_info ei = {0, 0, 0}; |
f3d98e74 | 2486 | pgoff_t pg_start, pg_end, next_pgofs; |
3519e3f9 | 2487 | unsigned int blk_per_seg = sbi->blocks_per_seg; |
d323d005 | 2488 | unsigned int total = 0, sec_num; |
d323d005 CY |
2489 | block_t blk_end = 0; |
2490 | bool fragmented = false; | |
2491 | int err; | |
2492 | ||
09cbfeaf KS |
2493 | pg_start = range->start >> PAGE_SHIFT; |
2494 | pg_end = (range->start + range->len) >> PAGE_SHIFT; | |
d323d005 | 2495 | |
2c4db1a6 | 2496 | f2fs_balance_fs(sbi, true); |
d323d005 | 2497 | |
5955102c | 2498 | inode_lock(inode); |
d323d005 | 2499 | |
1018a546 CY |
2500 | /* if in-place-update policy is enabled, don't waste time here */ |
2501 | set_inode_flag(inode, FI_OPU_WRITE); | |
2502 | if (f2fs_should_update_inplace(inode, NULL)) { | |
2503 | err = -EINVAL; | |
2504 | goto out; | |
2505 | } | |
2506 | ||
d323d005 CY |
2507 | /* writeback all dirty pages in the range */ |
2508 | err = filemap_write_and_wait_range(inode->i_mapping, range->start, | |
d8fe4f0e | 2509 | range->start + range->len - 1); |
d323d005 CY |
2510 | if (err) |
2511 | goto out; | |
2512 | ||
2513 | /* | |
2514 | * lookup mapping info in extent cache, skip defragmenting if physical | |
2515 | * block addresses are continuous. | |
2516 | */ | |
2517 | if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { | |
2518 | if (ei.fofs + ei.len >= pg_end) | |
2519 | goto out; | |
2520 | } | |
2521 | ||
2522 | map.m_lblk = pg_start; | |
f3d98e74 | 2523 | map.m_next_pgofs = &next_pgofs; |
d323d005 CY |
2524 | |
2525 | /* | |
2526 | * lookup mapping info in dnode page cache, skip defragmenting if all | |
2527 | * physical block addresses are continuous even if there are hole(s) | |
2528 | * in logical blocks. | |
2529 | */ | |
2530 | while (map.m_lblk < pg_end) { | |
a1c1e9b7 | 2531 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2532 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2533 | if (err) |
2534 | goto out; | |
2535 | ||
2536 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
f3d98e74 | 2537 | map.m_lblk = next_pgofs; |
d323d005 CY |
2538 | continue; |
2539 | } | |
2540 | ||
25a912e5 | 2541 | if (blk_end && blk_end != map.m_pblk) |
d323d005 | 2542 | fragmented = true; |
25a912e5 CY |
2543 | |
2544 | /* record total count of block that we're going to move */ | |
2545 | total += map.m_len; | |
2546 | ||
d323d005 CY |
2547 | blk_end = map.m_pblk + map.m_len; |
2548 | ||
2549 | map.m_lblk += map.m_len; | |
d323d005 CY |
2550 | } |
2551 | ||
d3a1a0e1 CY |
2552 | if (!fragmented) { |
2553 | total = 0; | |
d323d005 | 2554 | goto out; |
d3a1a0e1 | 2555 | } |
d323d005 | 2556 | |
f91108b8 | 2557 | sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi)); |
d323d005 CY |
2558 | |
2559 | /* | |
2560 | * make sure there are enough free section for LFS allocation, this can | |
2561 | * avoid defragment running in SSR mode when free section are allocated | |
2562 | * intensively | |
2563 | */ | |
7f3037a5 | 2564 | if (has_not_enough_free_secs(sbi, 0, sec_num)) { |
d323d005 CY |
2565 | err = -EAGAIN; |
2566 | goto out; | |
2567 | } | |
2568 | ||
25a912e5 CY |
2569 | map.m_lblk = pg_start; |
2570 | map.m_len = pg_end - pg_start; | |
2571 | total = 0; | |
2572 | ||
d323d005 CY |
2573 | while (map.m_lblk < pg_end) { |
2574 | pgoff_t idx; | |
2575 | int cnt = 0; | |
2576 | ||
2577 | do_map: | |
a1c1e9b7 | 2578 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2579 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2580 | if (err) |
2581 | goto clear_out; | |
2582 | ||
2583 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
f3d98e74 | 2584 | map.m_lblk = next_pgofs; |
d3a1a0e1 | 2585 | goto check; |
d323d005 CY |
2586 | } |
2587 | ||
1018a546 | 2588 | set_inode_flag(inode, FI_SKIP_WRITES); |
d323d005 CY |
2589 | |
2590 | idx = map.m_lblk; | |
2591 | while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { | |
2592 | struct page *page; | |
2593 | ||
4d57b86d | 2594 | page = f2fs_get_lock_data_page(inode, idx, true); |
d323d005 CY |
2595 | if (IS_ERR(page)) { |
2596 | err = PTR_ERR(page); | |
2597 | goto clear_out; | |
2598 | } | |
2599 | ||
2600 | set_page_dirty(page); | |
2601 | f2fs_put_page(page, 1); | |
2602 | ||
2603 | idx++; | |
2604 | cnt++; | |
2605 | total++; | |
2606 | } | |
2607 | ||
2608 | map.m_lblk = idx; | |
d3a1a0e1 CY |
2609 | check: |
2610 | if (map.m_lblk < pg_end && cnt < blk_per_seg) | |
d323d005 CY |
2611 | goto do_map; |
2612 | ||
1018a546 | 2613 | clear_inode_flag(inode, FI_SKIP_WRITES); |
d323d005 CY |
2614 | |
2615 | err = filemap_fdatawrite(inode->i_mapping); | |
2616 | if (err) | |
2617 | goto out; | |
2618 | } | |
2619 | clear_out: | |
1018a546 | 2620 | clear_inode_flag(inode, FI_SKIP_WRITES); |
d323d005 | 2621 | out: |
1018a546 | 2622 | clear_inode_flag(inode, FI_OPU_WRITE); |
5955102c | 2623 | inode_unlock(inode); |
d323d005 | 2624 | if (!err) |
09cbfeaf | 2625 | range->len = (u64)total << PAGE_SHIFT; |
d323d005 CY |
2626 | return err; |
2627 | } | |
2628 | ||
2629 | static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) | |
2630 | { | |
2631 | struct inode *inode = file_inode(filp); | |
2632 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2633 | struct f2fs_defragment range; | |
2634 | int err; | |
2635 | ||
2636 | if (!capable(CAP_SYS_ADMIN)) | |
2637 | return -EPERM; | |
2638 | ||
7eab0c0d | 2639 | if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode)) |
d323d005 CY |
2640 | return -EINVAL; |
2641 | ||
d7563861 KM |
2642 | if (f2fs_readonly(sbi->sb)) |
2643 | return -EROFS; | |
d323d005 CY |
2644 | |
2645 | if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, | |
d7563861 KM |
2646 | sizeof(range))) |
2647 | return -EFAULT; | |
d323d005 CY |
2648 | |
2649 | /* verify alignment of offset & size */ | |
d7563861 KM |
2650 | if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1)) |
2651 | return -EINVAL; | |
d323d005 | 2652 | |
1941d7bc | 2653 | if (unlikely((range.start + range.len) >> PAGE_SHIFT > |
6d1451bf | 2654 | max_file_blocks(inode))) |
d7563861 KM |
2655 | return -EINVAL; |
2656 | ||
2657 | err = mnt_want_write_file(filp); | |
2658 | if (err) | |
2659 | return err; | |
1941d7bc | 2660 | |
d323d005 | 2661 | err = f2fs_defragment_range(sbi, filp, &range); |
d7563861 KM |
2662 | mnt_drop_write_file(filp); |
2663 | ||
d0239e1b | 2664 | f2fs_update_time(sbi, REQ_TIME); |
d323d005 | 2665 | if (err < 0) |
d7563861 | 2666 | return err; |
d323d005 CY |
2667 | |
2668 | if (copy_to_user((struct f2fs_defragment __user *)arg, &range, | |
2669 | sizeof(range))) | |
d7563861 KM |
2670 | return -EFAULT; |
2671 | ||
2672 | return 0; | |
d323d005 CY |
2673 | } |
2674 | ||
4dd6f977 JK |
2675 | static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, |
2676 | struct file *file_out, loff_t pos_out, size_t len) | |
2677 | { | |
2678 | struct inode *src = file_inode(file_in); | |
2679 | struct inode *dst = file_inode(file_out); | |
2680 | struct f2fs_sb_info *sbi = F2FS_I_SB(src); | |
2681 | size_t olen = len, dst_max_i_size = 0; | |
2682 | size_t dst_osize; | |
2683 | int ret; | |
2684 | ||
2685 | if (file_in->f_path.mnt != file_out->f_path.mnt || | |
2686 | src->i_sb != dst->i_sb) | |
2687 | return -EXDEV; | |
2688 | ||
2689 | if (unlikely(f2fs_readonly(src->i_sb))) | |
2690 | return -EROFS; | |
2691 | ||
fe8494bf CY |
2692 | if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode)) |
2693 | return -EINVAL; | |
4dd6f977 | 2694 | |
62230e0d | 2695 | if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst)) |
4dd6f977 JK |
2696 | return -EOPNOTSUPP; |
2697 | ||
aad1383c DR |
2698 | if (pos_out < 0 || pos_in < 0) |
2699 | return -EINVAL; | |
2700 | ||
d95fd91c FL |
2701 | if (src == dst) { |
2702 | if (pos_in == pos_out) | |
2703 | return 0; | |
2704 | if (pos_out > pos_in && pos_out < pos_in + len) | |
2705 | return -EINVAL; | |
2706 | } | |
2707 | ||
4dd6f977 | 2708 | inode_lock(src); |
20a3d61d | 2709 | if (src != dst) { |
bb06664a CY |
2710 | ret = -EBUSY; |
2711 | if (!inode_trylock(dst)) | |
2712 | goto out; | |
20a3d61d | 2713 | } |
4dd6f977 JK |
2714 | |
2715 | ret = -EINVAL; | |
2716 | if (pos_in + len > src->i_size || pos_in + len < pos_in) | |
2717 | goto out_unlock; | |
2718 | if (len == 0) | |
2719 | olen = len = src->i_size - pos_in; | |
2720 | if (pos_in + len == src->i_size) | |
2721 | len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in; | |
2722 | if (len == 0) { | |
2723 | ret = 0; | |
2724 | goto out_unlock; | |
2725 | } | |
2726 | ||
2727 | dst_osize = dst->i_size; | |
2728 | if (pos_out + olen > dst->i_size) | |
2729 | dst_max_i_size = pos_out + olen; | |
2730 | ||
2731 | /* verify the end result is block aligned */ | |
2732 | if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) || | |
2733 | !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) || | |
2734 | !IS_ALIGNED(pos_out, F2FS_BLKSIZE)) | |
2735 | goto out_unlock; | |
2736 | ||
2737 | ret = f2fs_convert_inline_inode(src); | |
2738 | if (ret) | |
2739 | goto out_unlock; | |
2740 | ||
2741 | ret = f2fs_convert_inline_inode(dst); | |
2742 | if (ret) | |
2743 | goto out_unlock; | |
2744 | ||
2745 | /* write out all dirty pages from offset */ | |
2746 | ret = filemap_write_and_wait_range(src->i_mapping, | |
2747 | pos_in, pos_in + len); | |
2748 | if (ret) | |
2749 | goto out_unlock; | |
2750 | ||
2751 | ret = filemap_write_and_wait_range(dst->i_mapping, | |
2752 | pos_out, pos_out + len); | |
2753 | if (ret) | |
2754 | goto out_unlock; | |
2755 | ||
2756 | f2fs_balance_fs(sbi, true); | |
6f8d4455 | 2757 | |
e4544b63 | 2758 | f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); |
6f8d4455 JK |
2759 | if (src != dst) { |
2760 | ret = -EBUSY; | |
e4544b63 | 2761 | if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE])) |
6f8d4455 JK |
2762 | goto out_src; |
2763 | } | |
2764 | ||
4dd6f977 | 2765 | f2fs_lock_op(sbi); |
61e4da11 FL |
2766 | ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS, |
2767 | pos_out >> F2FS_BLKSIZE_BITS, | |
2768 | len >> F2FS_BLKSIZE_BITS, false); | |
4dd6f977 JK |
2769 | |
2770 | if (!ret) { | |
2771 | if (dst_max_i_size) | |
2772 | f2fs_i_size_write(dst, dst_max_i_size); | |
2773 | else if (dst_osize != dst->i_size) | |
2774 | f2fs_i_size_write(dst, dst_osize); | |
2775 | } | |
2776 | f2fs_unlock_op(sbi); | |
6f8d4455 JK |
2777 | |
2778 | if (src != dst) | |
e4544b63 | 2779 | f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]); |
6f8d4455 | 2780 | out_src: |
e4544b63 | 2781 | f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); |
6f8d4455 JK |
2782 | out_unlock: |
2783 | if (src != dst) | |
4dd6f977 | 2784 | inode_unlock(dst); |
20a3d61d | 2785 | out: |
4dd6f977 JK |
2786 | inode_unlock(src); |
2787 | return ret; | |
2788 | } | |
2789 | ||
34178b1b CY |
2790 | static int __f2fs_ioc_move_range(struct file *filp, |
2791 | struct f2fs_move_range *range) | |
4dd6f977 | 2792 | { |
4dd6f977 JK |
2793 | struct fd dst; |
2794 | int err; | |
2795 | ||
2796 | if (!(filp->f_mode & FMODE_READ) || | |
2797 | !(filp->f_mode & FMODE_WRITE)) | |
2798 | return -EBADF; | |
2799 | ||
34178b1b | 2800 | dst = fdget(range->dst_fd); |
4dd6f977 JK |
2801 | if (!dst.file) |
2802 | return -EBADF; | |
2803 | ||
2804 | if (!(dst.file->f_mode & FMODE_WRITE)) { | |
2805 | err = -EBADF; | |
2806 | goto err_out; | |
2807 | } | |
2808 | ||
2809 | err = mnt_want_write_file(filp); | |
2810 | if (err) | |
2811 | goto err_out; | |
2812 | ||
34178b1b CY |
2813 | err = f2fs_move_file_range(filp, range->pos_in, dst.file, |
2814 | range->pos_out, range->len); | |
4dd6f977 JK |
2815 | |
2816 | mnt_drop_write_file(filp); | |
4dd6f977 JK |
2817 | err_out: |
2818 | fdput(dst); | |
2819 | return err; | |
2820 | } | |
2821 | ||
34178b1b CY |
2822 | static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) |
2823 | { | |
2824 | struct f2fs_move_range range; | |
2825 | ||
2826 | if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, | |
2827 | sizeof(range))) | |
2828 | return -EFAULT; | |
2829 | return __f2fs_ioc_move_range(filp, &range); | |
2830 | } | |
2831 | ||
e066b83c JK |
2832 | static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) |
2833 | { | |
2834 | struct inode *inode = file_inode(filp); | |
2835 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2836 | struct sit_info *sm = SIT_I(sbi); | |
2837 | unsigned int start_segno = 0, end_segno = 0; | |
2838 | unsigned int dev_start_segno = 0, dev_end_segno = 0; | |
2839 | struct f2fs_flush_device range; | |
d147ea4a JK |
2840 | struct f2fs_gc_control gc_control = { |
2841 | .init_gc_type = FG_GC, | |
2842 | .should_migrate_blocks = true, | |
c81d5bae JK |
2843 | .err_gc_skipped = true, |
2844 | .nr_free_secs = 0 }; | |
e066b83c JK |
2845 | int ret; |
2846 | ||
2847 | if (!capable(CAP_SYS_ADMIN)) | |
2848 | return -EPERM; | |
2849 | ||
2850 | if (f2fs_readonly(sbi->sb)) | |
2851 | return -EROFS; | |
2852 | ||
4354994f DR |
2853 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
2854 | return -EINVAL; | |
2855 | ||
e066b83c JK |
2856 | if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg, |
2857 | sizeof(range))) | |
2858 | return -EFAULT; | |
2859 | ||
0916878d | 2860 | if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num || |
2c70c5e3 | 2861 | __is_large_section(sbi)) { |
dcbb4c10 JP |
2862 | f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1", |
2863 | range.dev_num, sbi->s_ndevs, sbi->segs_per_sec); | |
e066b83c JK |
2864 | return -EINVAL; |
2865 | } | |
2866 | ||
2867 | ret = mnt_want_write_file(filp); | |
2868 | if (ret) | |
2869 | return ret; | |
2870 | ||
2871 | if (range.dev_num != 0) | |
2872 | dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk); | |
2873 | dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk); | |
2874 | ||
2875 | start_segno = sm->last_victim[FLUSH_DEVICE]; | |
2876 | if (start_segno < dev_start_segno || start_segno >= dev_end_segno) | |
2877 | start_segno = dev_start_segno; | |
2878 | end_segno = min(start_segno + range.segments, dev_end_segno); | |
2879 | ||
2880 | while (start_segno < end_segno) { | |
e4544b63 | 2881 | if (!f2fs_down_write_trylock(&sbi->gc_lock)) { |
e066b83c JK |
2882 | ret = -EBUSY; |
2883 | goto out; | |
2884 | } | |
2885 | sm->last_victim[GC_CB] = end_segno + 1; | |
2886 | sm->last_victim[GC_GREEDY] = end_segno + 1; | |
2887 | sm->last_victim[ALLOC_NEXT] = end_segno + 1; | |
d147ea4a JK |
2888 | |
2889 | gc_control.victim_segno = start_segno; | |
2890 | ret = f2fs_gc(sbi, &gc_control); | |
e066b83c JK |
2891 | if (ret == -EAGAIN) |
2892 | ret = 0; | |
2893 | else if (ret < 0) | |
2894 | break; | |
2895 | start_segno++; | |
2896 | } | |
2897 | out: | |
2898 | mnt_drop_write_file(filp); | |
2899 | return ret; | |
2900 | } | |
2901 | ||
e65ef207 JK |
2902 | static int f2fs_ioc_get_features(struct file *filp, unsigned long arg) |
2903 | { | |
2904 | struct inode *inode = file_inode(filp); | |
2905 | u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature); | |
2906 | ||
2907 | /* Must validate to set it with SQLite behavior in Android. */ | |
2908 | sb_feature |= F2FS_FEATURE_ATOMIC_WRITE; | |
2909 | ||
2910 | return put_user(sb_feature, (u32 __user *)arg); | |
2911 | } | |
e066b83c | 2912 | |
2c1d0305 | 2913 | #ifdef CONFIG_QUOTA |
78130819 CY |
2914 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) |
2915 | { | |
2916 | struct dquot *transfer_to[MAXQUOTAS] = {}; | |
2917 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2918 | struct super_block *sb = sbi->sb; | |
2919 | int err = 0; | |
2920 | ||
2921 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); | |
2922 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
2923 | err = __dquot_transfer(inode, transfer_to); | |
2924 | if (err) | |
2925 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); | |
2926 | dqput(transfer_to[PRJQUOTA]); | |
2927 | } | |
2928 | return err; | |
2929 | } | |
2930 | ||
9b1bb01c | 2931 | static int f2fs_ioc_setproject(struct inode *inode, __u32 projid) |
2c1d0305 | 2932 | { |
2c1d0305 CY |
2933 | struct f2fs_inode_info *fi = F2FS_I(inode); |
2934 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
d13732cc | 2935 | struct f2fs_inode *ri = NULL; |
2c1d0305 CY |
2936 | kprojid_t kprojid; |
2937 | int err; | |
2938 | ||
7beb01f7 | 2939 | if (!f2fs_sb_has_project_quota(sbi)) { |
2c1d0305 CY |
2940 | if (projid != F2FS_DEF_PROJID) |
2941 | return -EOPNOTSUPP; | |
2942 | else | |
2943 | return 0; | |
2944 | } | |
2945 | ||
2946 | if (!f2fs_has_extra_attr(inode)) | |
2947 | return -EOPNOTSUPP; | |
2948 | ||
2949 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
2950 | ||
2951 | if (projid_eq(kprojid, F2FS_I(inode)->i_projid)) | |
2952 | return 0; | |
2953 | ||
2c1d0305 | 2954 | err = -EPERM; |
2c1d0305 CY |
2955 | /* Is it quota file? Do not allow user to mess with it */ |
2956 | if (IS_NOQUOTA(inode)) | |
c8e92757 | 2957 | return err; |
2c1d0305 | 2958 | |
d13732cc JY |
2959 | if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) |
2960 | return -EOVERFLOW; | |
2c1d0305 | 2961 | |
10a26878 | 2962 | err = f2fs_dquot_initialize(inode); |
c22aecd7 | 2963 | if (err) |
c8e92757 | 2964 | return err; |
2c1d0305 | 2965 | |
78130819 CY |
2966 | f2fs_lock_op(sbi); |
2967 | err = f2fs_transfer_project_quota(inode, kprojid); | |
2968 | if (err) | |
2969 | goto out_unlock; | |
2c1d0305 CY |
2970 | |
2971 | F2FS_I(inode)->i_projid = kprojid; | |
2972 | inode->i_ctime = current_time(inode); | |
2c1d0305 | 2973 | f2fs_mark_inode_dirty_sync(inode, true); |
78130819 CY |
2974 | out_unlock: |
2975 | f2fs_unlock_op(sbi); | |
2c1d0305 CY |
2976 | return err; |
2977 | } | |
2978 | #else | |
78130819 CY |
2979 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) |
2980 | { | |
2981 | return 0; | |
2982 | } | |
2983 | ||
9b1bb01c | 2984 | static int f2fs_ioc_setproject(struct inode *inode, __u32 projid) |
2c1d0305 CY |
2985 | { |
2986 | if (projid != F2FS_DEF_PROJID) | |
2987 | return -EOPNOTSUPP; | |
2988 | return 0; | |
2989 | } | |
2990 | #endif | |
2991 | ||
9b1bb01c | 2992 | int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
2c1d0305 | 2993 | { |
9b1bb01c | 2994 | struct inode *inode = d_inode(dentry); |
2c1d0305 | 2995 | struct f2fs_inode_info *fi = F2FS_I(inode); |
9b1bb01c | 2996 | u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags); |
2c1d0305 | 2997 | |
9b1bb01c MS |
2998 | if (IS_ENCRYPTED(inode)) |
2999 | fsflags |= FS_ENCRYPT_FL; | |
3000 | if (IS_VERITY(inode)) | |
3001 | fsflags |= FS_VERITY_FL; | |
3002 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) | |
3003 | fsflags |= FS_INLINE_DATA_FL; | |
3004 | if (is_inode_flag_set(inode, FI_PIN_FILE)) | |
3005 | fsflags |= FS_NOCOW_FL; | |
3006 | ||
3007 | fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL); | |
2c1d0305 | 3008 | |
7beb01f7 | 3009 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode))) |
6fc93c4e | 3010 | fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid); |
2c1d0305 | 3011 | |
c8e92757 WS |
3012 | return 0; |
3013 | } | |
3014 | ||
9b1bb01c MS |
3015 | int f2fs_fileattr_set(struct user_namespace *mnt_userns, |
3016 | struct dentry *dentry, struct fileattr *fa) | |
2c1d0305 | 3017 | { |
9b1bb01c MS |
3018 | struct inode *inode = d_inode(dentry); |
3019 | u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL; | |
36098557 | 3020 | u32 iflags; |
2c1d0305 CY |
3021 | int err; |
3022 | ||
9b1bb01c MS |
3023 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
3024 | return -EIO; | |
3025 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode))) | |
3026 | return -ENOSPC; | |
3027 | if (fsflags & ~F2FS_GETTABLE_FS_FL) | |
2c1d0305 | 3028 | return -EOPNOTSUPP; |
9b1bb01c MS |
3029 | fsflags &= F2FS_SETTABLE_FS_FL; |
3030 | if (!fa->flags_valid) | |
3031 | mask &= FS_COMMON_FL; | |
2c1d0305 | 3032 | |
9b1bb01c | 3033 | iflags = f2fs_fsflags_to_iflags(fsflags); |
36098557 | 3034 | if (f2fs_mask_flags(inode->i_mode, iflags) != iflags) |
2c1d0305 CY |
3035 | return -EOPNOTSUPP; |
3036 | ||
9b1bb01c MS |
3037 | err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask)); |
3038 | if (!err) | |
3039 | err = f2fs_ioc_setproject(inode, fa->fsx_projid); | |
2c1d0305 | 3040 | |
c8e92757 | 3041 | return err; |
2c1d0305 | 3042 | } |
e066b83c | 3043 | |
1ad71a27 JK |
3044 | int f2fs_pin_file_control(struct inode *inode, bool inc) |
3045 | { | |
3046 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3047 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3048 | ||
3049 | /* Use i_gc_failures for normal file as a risk signal. */ | |
3050 | if (inc) | |
2ef79ecb CY |
3051 | f2fs_i_gc_failures_write(inode, |
3052 | fi->i_gc_failures[GC_FAILURE_PIN] + 1); | |
1ad71a27 | 3053 | |
2ef79ecb | 3054 | if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) { |
dcbb4c10 JP |
3055 | f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials", |
3056 | __func__, inode->i_ino, | |
3057 | fi->i_gc_failures[GC_FAILURE_PIN]); | |
1ad71a27 JK |
3058 | clear_inode_flag(inode, FI_PIN_FILE); |
3059 | return -EAGAIN; | |
3060 | } | |
3061 | return 0; | |
3062 | } | |
3063 | ||
3064 | static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg) | |
3065 | { | |
3066 | struct inode *inode = file_inode(filp); | |
3067 | __u32 pin; | |
3068 | int ret = 0; | |
3069 | ||
1ad71a27 JK |
3070 | if (get_user(pin, (__u32 __user *)arg)) |
3071 | return -EFAULT; | |
3072 | ||
3073 | if (!S_ISREG(inode->i_mode)) | |
3074 | return -EINVAL; | |
3075 | ||
3076 | if (f2fs_readonly(F2FS_I_SB(inode)->sb)) | |
3077 | return -EROFS; | |
3078 | ||
3079 | ret = mnt_want_write_file(filp); | |
3080 | if (ret) | |
3081 | return ret; | |
3082 | ||
3083 | inode_lock(inode); | |
3084 | ||
3085 | if (!pin) { | |
3086 | clear_inode_flag(inode, FI_PIN_FILE); | |
30933364 | 3087 | f2fs_i_gc_failures_write(inode, 0); |
1ad71a27 JK |
3088 | goto done; |
3089 | } | |
3090 | ||
19bdba52 JK |
3091 | if (f2fs_should_update_outplace(inode, NULL)) { |
3092 | ret = -EINVAL; | |
3093 | goto out; | |
3094 | } | |
3095 | ||
1ad71a27 JK |
3096 | if (f2fs_pin_file_control(inode, false)) { |
3097 | ret = -EAGAIN; | |
3098 | goto out; | |
3099 | } | |
4c8ff709 | 3100 | |
1ad71a27 JK |
3101 | ret = f2fs_convert_inline_inode(inode); |
3102 | if (ret) | |
3103 | goto out; | |
3104 | ||
78134d03 | 3105 | if (!f2fs_disable_compressed_file(inode)) { |
4c8ff709 CY |
3106 | ret = -EOPNOTSUPP; |
3107 | goto out; | |
3108 | } | |
3109 | ||
1ad71a27 | 3110 | set_inode_flag(inode, FI_PIN_FILE); |
2ef79ecb | 3111 | ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; |
1ad71a27 JK |
3112 | done: |
3113 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | |
3114 | out: | |
3115 | inode_unlock(inode); | |
3116 | mnt_drop_write_file(filp); | |
3117 | return ret; | |
3118 | } | |
3119 | ||
3120 | static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg) | |
3121 | { | |
3122 | struct inode *inode = file_inode(filp); | |
3123 | __u32 pin = 0; | |
3124 | ||
3125 | if (is_inode_flag_set(inode, FI_PIN_FILE)) | |
2ef79ecb | 3126 | pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; |
1ad71a27 JK |
3127 | return put_user(pin, (u32 __user *)arg); |
3128 | } | |
3129 | ||
c4020b2d CY |
3130 | int f2fs_precache_extents(struct inode *inode) |
3131 | { | |
3132 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3133 | struct f2fs_map_blocks map; | |
3134 | pgoff_t m_next_extent; | |
3135 | loff_t end; | |
3136 | int err; | |
3137 | ||
3138 | if (is_inode_flag_set(inode, FI_NO_EXTENT)) | |
3139 | return -EOPNOTSUPP; | |
3140 | ||
3141 | map.m_lblk = 0; | |
3142 | map.m_next_pgofs = NULL; | |
3143 | map.m_next_extent = &m_next_extent; | |
3144 | map.m_seg_type = NO_CHECK_TYPE; | |
f4f0b677 | 3145 | map.m_may_create = false; |
6d1451bf | 3146 | end = max_file_blocks(inode); |
c4020b2d CY |
3147 | |
3148 | while (map.m_lblk < end) { | |
3149 | map.m_len = end - map.m_lblk; | |
3150 | ||
e4544b63 | 3151 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]); |
c4020b2d | 3152 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE); |
e4544b63 | 3153 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]); |
c4020b2d CY |
3154 | if (err) |
3155 | return err; | |
3156 | ||
3157 | map.m_lblk = m_next_extent; | |
3158 | } | |
3159 | ||
4f55dc2a | 3160 | return 0; |
c4020b2d CY |
3161 | } |
3162 | ||
3163 | static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg) | |
3164 | { | |
3165 | return f2fs_precache_extents(file_inode(filp)); | |
3166 | } | |
3167 | ||
04f0b2ea QS |
3168 | static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg) |
3169 | { | |
3170 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); | |
3171 | __u64 block_count; | |
04f0b2ea QS |
3172 | |
3173 | if (!capable(CAP_SYS_ADMIN)) | |
3174 | return -EPERM; | |
3175 | ||
3176 | if (f2fs_readonly(sbi->sb)) | |
3177 | return -EROFS; | |
3178 | ||
3179 | if (copy_from_user(&block_count, (void __user *)arg, | |
3180 | sizeof(block_count))) | |
3181 | return -EFAULT; | |
3182 | ||
b4b10061 | 3183 | return f2fs_resize_fs(sbi, block_count); |
04f0b2ea QS |
3184 | } |
3185 | ||
95ae251f EB |
3186 | static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg) |
3187 | { | |
3188 | struct inode *inode = file_inode(filp); | |
3189 | ||
3190 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | |
3191 | ||
3192 | if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) { | |
3193 | f2fs_warn(F2FS_I_SB(inode), | |
833dcd35 | 3194 | "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem", |
95ae251f EB |
3195 | inode->i_ino); |
3196 | return -EOPNOTSUPP; | |
3197 | } | |
3198 | ||
3199 | return fsverity_ioctl_enable(filp, (const void __user *)arg); | |
3200 | } | |
3201 | ||
3202 | static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg) | |
3203 | { | |
3204 | if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp)))) | |
3205 | return -EOPNOTSUPP; | |
3206 | ||
3207 | return fsverity_ioctl_measure(filp, (void __user *)arg); | |
3208 | } | |
3209 | ||
e17fe657 EB |
3210 | static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg) |
3211 | { | |
3212 | if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp)))) | |
3213 | return -EOPNOTSUPP; | |
3214 | ||
3215 | return fsverity_ioctl_read_metadata(filp, (const void __user *)arg); | |
3216 | } | |
3217 | ||
3357af8f | 3218 | static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg) |
4507847c CY |
3219 | { |
3220 | struct inode *inode = file_inode(filp); | |
3221 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3222 | char *vbuf; | |
3223 | int count; | |
3224 | int err = 0; | |
3225 | ||
3226 | vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL); | |
3227 | if (!vbuf) | |
3228 | return -ENOMEM; | |
3229 | ||
e4544b63 | 3230 | f2fs_down_read(&sbi->sb_lock); |
4507847c CY |
3231 | count = utf16s_to_utf8s(sbi->raw_super->volume_name, |
3232 | ARRAY_SIZE(sbi->raw_super->volume_name), | |
3233 | UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME); | |
e4544b63 | 3234 | f2fs_up_read(&sbi->sb_lock); |
4507847c CY |
3235 | |
3236 | if (copy_to_user((char __user *)arg, vbuf, | |
3237 | min(FSLABEL_MAX, count))) | |
3238 | err = -EFAULT; | |
3239 | ||
c8eb7024 | 3240 | kfree(vbuf); |
4507847c CY |
3241 | return err; |
3242 | } | |
3243 | ||
3357af8f | 3244 | static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg) |
4507847c CY |
3245 | { |
3246 | struct inode *inode = file_inode(filp); | |
3247 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3248 | char *vbuf; | |
3249 | int err = 0; | |
3250 | ||
3251 | if (!capable(CAP_SYS_ADMIN)) | |
3252 | return -EPERM; | |
3253 | ||
3254 | vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX); | |
3255 | if (IS_ERR(vbuf)) | |
3256 | return PTR_ERR(vbuf); | |
3257 | ||
3258 | err = mnt_want_write_file(filp); | |
3259 | if (err) | |
3260 | goto out; | |
3261 | ||
e4544b63 | 3262 | f2fs_down_write(&sbi->sb_lock); |
4507847c CY |
3263 | |
3264 | memset(sbi->raw_super->volume_name, 0, | |
3265 | sizeof(sbi->raw_super->volume_name)); | |
3266 | utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN, | |
3267 | sbi->raw_super->volume_name, | |
3268 | ARRAY_SIZE(sbi->raw_super->volume_name)); | |
3269 | ||
3270 | err = f2fs_commit_super(sbi, false); | |
3271 | ||
e4544b63 | 3272 | f2fs_up_write(&sbi->sb_lock); |
4507847c CY |
3273 | |
3274 | mnt_drop_write_file(filp); | |
3275 | out: | |
3276 | kfree(vbuf); | |
3277 | return err; | |
3278 | } | |
3279 | ||
439dfb10 CY |
3280 | static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg) |
3281 | { | |
3282 | struct inode *inode = file_inode(filp); | |
3283 | __u64 blocks; | |
3284 | ||
3285 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3286 | return -EOPNOTSUPP; | |
3287 | ||
3288 | if (!f2fs_compressed_file(inode)) | |
3289 | return -EINVAL; | |
3290 | ||
c2759eba | 3291 | blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks); |
439dfb10 CY |
3292 | return put_user(blocks, (u64 __user *)arg); |
3293 | } | |
3294 | ||
ef8d563f CY |
3295 | static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count) |
3296 | { | |
3297 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
3298 | unsigned int released_blocks = 0; | |
3299 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size; | |
3300 | block_t blkaddr; | |
3301 | int i; | |
3302 | ||
3303 | for (i = 0; i < count; i++) { | |
3304 | blkaddr = data_blkaddr(dn->inode, dn->node_page, | |
3305 | dn->ofs_in_node + i); | |
3306 | ||
3307 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3308 | continue; | |
3309 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3310 | DATA_GENERIC_ENHANCE))) | |
3311 | return -EFSCORRUPTED; | |
3312 | } | |
3313 | ||
3314 | while (count) { | |
3315 | int compr_blocks = 0; | |
3316 | ||
3317 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { | |
3318 | blkaddr = f2fs_data_blkaddr(dn); | |
3319 | ||
3320 | if (i == 0) { | |
3321 | if (blkaddr == COMPRESS_ADDR) | |
3322 | continue; | |
3323 | dn->ofs_in_node += cluster_size; | |
3324 | goto next; | |
3325 | } | |
3326 | ||
3327 | if (__is_valid_data_blkaddr(blkaddr)) | |
3328 | compr_blocks++; | |
3329 | ||
3330 | if (blkaddr != NEW_ADDR) | |
3331 | continue; | |
3332 | ||
3333 | dn->data_blkaddr = NULL_ADDR; | |
3334 | f2fs_set_data_blkaddr(dn); | |
3335 | } | |
3336 | ||
3337 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false); | |
3338 | dec_valid_block_count(sbi, dn->inode, | |
3339 | cluster_size - compr_blocks); | |
3340 | ||
3341 | released_blocks += cluster_size - compr_blocks; | |
3342 | next: | |
3343 | count -= cluster_size; | |
3344 | } | |
3345 | ||
3346 | return released_blocks; | |
3347 | } | |
3348 | ||
3349 | static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) | |
3350 | { | |
3351 | struct inode *inode = file_inode(filp); | |
3352 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3353 | pgoff_t page_idx = 0, last_idx; | |
3354 | unsigned int released_blocks = 0; | |
3355 | int ret; | |
3356 | int writecount; | |
3357 | ||
3358 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3359 | return -EOPNOTSUPP; | |
3360 | ||
3361 | if (!f2fs_compressed_file(inode)) | |
3362 | return -EINVAL; | |
3363 | ||
3364 | if (f2fs_readonly(sbi->sb)) | |
3365 | return -EROFS; | |
3366 | ||
3367 | ret = mnt_want_write_file(filp); | |
3368 | if (ret) | |
3369 | return ret; | |
3370 | ||
3371 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
3372 | ||
3373 | inode_lock(inode); | |
3374 | ||
3375 | writecount = atomic_read(&inode->i_writecount); | |
8c8cf26a DJ |
3376 | if ((filp->f_mode & FMODE_WRITE && writecount != 1) || |
3377 | (!(filp->f_mode & FMODE_WRITE) && writecount)) { | |
ef8d563f CY |
3378 | ret = -EBUSY; |
3379 | goto out; | |
3380 | } | |
3381 | ||
c6140415 | 3382 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) { |
ef8d563f CY |
3383 | ret = -EINVAL; |
3384 | goto out; | |
3385 | } | |
3386 | ||
3387 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); | |
3388 | if (ret) | |
3389 | goto out; | |
3390 | ||
c6140415 | 3391 | set_inode_flag(inode, FI_COMPRESS_RELEASED); |
ef8d563f CY |
3392 | inode->i_ctime = current_time(inode); |
3393 | f2fs_mark_inode_dirty_sync(inode, true); | |
3394 | ||
c2759eba | 3395 | if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) |
567c4bf5 DJ |
3396 | goto out; |
3397 | ||
e4544b63 | 3398 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 3399 | filemap_invalidate_lock(inode->i_mapping); |
ef8d563f CY |
3400 | |
3401 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
3402 | ||
3403 | while (page_idx < last_idx) { | |
3404 | struct dnode_of_data dn; | |
3405 | pgoff_t end_offset, count; | |
3406 | ||
3407 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3408 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE); | |
3409 | if (ret) { | |
3410 | if (ret == -ENOENT) { | |
3411 | page_idx = f2fs_get_next_page_offset(&dn, | |
3412 | page_idx); | |
3413 | ret = 0; | |
3414 | continue; | |
3415 | } | |
3416 | break; | |
3417 | } | |
3418 | ||
3419 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3420 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); | |
4fec3fc0 | 3421 | count = round_up(count, F2FS_I(inode)->i_cluster_size); |
ef8d563f CY |
3422 | |
3423 | ret = release_compress_blocks(&dn, count); | |
3424 | ||
3425 | f2fs_put_dnode(&dn); | |
3426 | ||
3427 | if (ret < 0) | |
3428 | break; | |
3429 | ||
3430 | page_idx += count; | |
3431 | released_blocks += ret; | |
3432 | } | |
3433 | ||
edc6d01b | 3434 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 3435 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
ef8d563f CY |
3436 | out: |
3437 | inode_unlock(inode); | |
3438 | ||
3439 | mnt_drop_write_file(filp); | |
3440 | ||
3441 | if (ret >= 0) { | |
3442 | ret = put_user(released_blocks, (u64 __user *)arg); | |
c2759eba DJ |
3443 | } else if (released_blocks && |
3444 | atomic_read(&F2FS_I(inode)->i_compr_blocks)) { | |
ef8d563f CY |
3445 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
3446 | f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx " | |
c2759eba | 3447 | "iblocks=%llu, released=%u, compr_blocks=%u, " |
ef8d563f CY |
3448 | "run fsck to fix.", |
3449 | __func__, inode->i_ino, inode->i_blocks, | |
3450 | released_blocks, | |
c2759eba | 3451 | atomic_read(&F2FS_I(inode)->i_compr_blocks)); |
ef8d563f CY |
3452 | } |
3453 | ||
3454 | return ret; | |
3455 | } | |
3456 | ||
c75488fb CY |
3457 | static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) |
3458 | { | |
3459 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
3460 | unsigned int reserved_blocks = 0; | |
3461 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size; | |
3462 | block_t blkaddr; | |
3463 | int i; | |
3464 | ||
3465 | for (i = 0; i < count; i++) { | |
3466 | blkaddr = data_blkaddr(dn->inode, dn->node_page, | |
3467 | dn->ofs_in_node + i); | |
3468 | ||
3469 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3470 | continue; | |
3471 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3472 | DATA_GENERIC_ENHANCE))) | |
3473 | return -EFSCORRUPTED; | |
3474 | } | |
3475 | ||
3476 | while (count) { | |
3477 | int compr_blocks = 0; | |
3478 | blkcnt_t reserved; | |
3479 | int ret; | |
3480 | ||
3481 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { | |
3482 | blkaddr = f2fs_data_blkaddr(dn); | |
3483 | ||
3484 | if (i == 0) { | |
3485 | if (blkaddr == COMPRESS_ADDR) | |
3486 | continue; | |
3487 | dn->ofs_in_node += cluster_size; | |
3488 | goto next; | |
3489 | } | |
3490 | ||
3491 | if (__is_valid_data_blkaddr(blkaddr)) { | |
3492 | compr_blocks++; | |
3493 | continue; | |
3494 | } | |
3495 | ||
3496 | dn->data_blkaddr = NEW_ADDR; | |
3497 | f2fs_set_data_blkaddr(dn); | |
3498 | } | |
3499 | ||
3500 | reserved = cluster_size - compr_blocks; | |
3501 | ret = inc_valid_block_count(sbi, dn->inode, &reserved); | |
3502 | if (ret) | |
3503 | return ret; | |
3504 | ||
3505 | if (reserved != cluster_size - compr_blocks) | |
3506 | return -ENOSPC; | |
3507 | ||
3508 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); | |
3509 | ||
3510 | reserved_blocks += reserved; | |
3511 | next: | |
3512 | count -= cluster_size; | |
3513 | } | |
3514 | ||
3515 | return reserved_blocks; | |
3516 | } | |
3517 | ||
3518 | static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) | |
3519 | { | |
3520 | struct inode *inode = file_inode(filp); | |
3521 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3522 | pgoff_t page_idx = 0, last_idx; | |
3523 | unsigned int reserved_blocks = 0; | |
3524 | int ret; | |
3525 | ||
3526 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3527 | return -EOPNOTSUPP; | |
3528 | ||
3529 | if (!f2fs_compressed_file(inode)) | |
3530 | return -EINVAL; | |
3531 | ||
3532 | if (f2fs_readonly(sbi->sb)) | |
3533 | return -EROFS; | |
3534 | ||
3535 | ret = mnt_want_write_file(filp); | |
3536 | if (ret) | |
3537 | return ret; | |
3538 | ||
c2759eba | 3539 | if (atomic_read(&F2FS_I(inode)->i_compr_blocks)) |
c75488fb CY |
3540 | goto out; |
3541 | ||
3542 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
3543 | ||
3544 | inode_lock(inode); | |
3545 | ||
c6140415 | 3546 | if (!is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) { |
c75488fb CY |
3547 | ret = -EINVAL; |
3548 | goto unlock_inode; | |
3549 | } | |
3550 | ||
e4544b63 | 3551 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 3552 | filemap_invalidate_lock(inode->i_mapping); |
c75488fb CY |
3553 | |
3554 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
3555 | ||
3556 | while (page_idx < last_idx) { | |
3557 | struct dnode_of_data dn; | |
3558 | pgoff_t end_offset, count; | |
3559 | ||
3560 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3561 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE); | |
3562 | if (ret) { | |
3563 | if (ret == -ENOENT) { | |
3564 | page_idx = f2fs_get_next_page_offset(&dn, | |
3565 | page_idx); | |
3566 | ret = 0; | |
3567 | continue; | |
3568 | } | |
3569 | break; | |
3570 | } | |
3571 | ||
3572 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3573 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); | |
4fec3fc0 | 3574 | count = round_up(count, F2FS_I(inode)->i_cluster_size); |
c75488fb CY |
3575 | |
3576 | ret = reserve_compress_blocks(&dn, count); | |
3577 | ||
3578 | f2fs_put_dnode(&dn); | |
3579 | ||
3580 | if (ret < 0) | |
3581 | break; | |
3582 | ||
3583 | page_idx += count; | |
3584 | reserved_blocks += ret; | |
3585 | } | |
3586 | ||
edc6d01b | 3587 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 3588 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
c75488fb CY |
3589 | |
3590 | if (ret >= 0) { | |
c6140415 | 3591 | clear_inode_flag(inode, FI_COMPRESS_RELEASED); |
c75488fb CY |
3592 | inode->i_ctime = current_time(inode); |
3593 | f2fs_mark_inode_dirty_sync(inode, true); | |
3594 | } | |
3595 | unlock_inode: | |
3596 | inode_unlock(inode); | |
3597 | out: | |
3598 | mnt_drop_write_file(filp); | |
3599 | ||
3600 | if (ret >= 0) { | |
3601 | ret = put_user(reserved_blocks, (u64 __user *)arg); | |
c2759eba DJ |
3602 | } else if (reserved_blocks && |
3603 | atomic_read(&F2FS_I(inode)->i_compr_blocks)) { | |
c75488fb CY |
3604 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
3605 | f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx " | |
c2759eba | 3606 | "iblocks=%llu, reserved=%u, compr_blocks=%u, " |
c75488fb CY |
3607 | "run fsck to fix.", |
3608 | __func__, inode->i_ino, inode->i_blocks, | |
3609 | reserved_blocks, | |
c2759eba | 3610 | atomic_read(&F2FS_I(inode)->i_compr_blocks)); |
c75488fb CY |
3611 | } |
3612 | ||
3613 | return ret; | |
3614 | } | |
3615 | ||
9af84648 DJ |
3616 | static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode, |
3617 | pgoff_t off, block_t block, block_t len, u32 flags) | |
3618 | { | |
3619 | struct request_queue *q = bdev_get_queue(bdev); | |
3620 | sector_t sector = SECTOR_FROM_BLOCK(block); | |
3621 | sector_t nr_sects = SECTOR_FROM_BLOCK(len); | |
3622 | int ret = 0; | |
3623 | ||
3624 | if (!q) | |
3625 | return -ENXIO; | |
3626 | ||
3627 | if (flags & F2FS_TRIM_FILE_DISCARD) | |
3628 | ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS, | |
3629 | blk_queue_secure_erase(q) ? | |
3630 | BLKDEV_DISCARD_SECURE : 0); | |
3631 | ||
3632 | if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) { | |
3633 | if (IS_ENCRYPTED(inode)) | |
3634 | ret = fscrypt_zeroout_range(inode, off, block, len); | |
3635 | else | |
3636 | ret = blkdev_issue_zeroout(bdev, sector, nr_sects, | |
3637 | GFP_NOFS, 0); | |
3638 | } | |
3639 | ||
3640 | return ret; | |
3641 | } | |
3642 | ||
3643 | static int f2fs_sec_trim_file(struct file *filp, unsigned long arg) | |
3644 | { | |
3645 | struct inode *inode = file_inode(filp); | |
3646 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3647 | struct address_space *mapping = inode->i_mapping; | |
3648 | struct block_device *prev_bdev = NULL; | |
3649 | struct f2fs_sectrim_range range; | |
3650 | pgoff_t index, pg_end, prev_index = 0; | |
3651 | block_t prev_block = 0, len = 0; | |
3652 | loff_t end_addr; | |
3653 | bool to_end = false; | |
3654 | int ret = 0; | |
3655 | ||
3656 | if (!(filp->f_mode & FMODE_WRITE)) | |
3657 | return -EBADF; | |
3658 | ||
3659 | if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg, | |
3660 | sizeof(range))) | |
3661 | return -EFAULT; | |
3662 | ||
3663 | if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) || | |
3664 | !S_ISREG(inode->i_mode)) | |
3665 | return -EINVAL; | |
3666 | ||
3667 | if (((range.flags & F2FS_TRIM_FILE_DISCARD) && | |
3668 | !f2fs_hw_support_discard(sbi)) || | |
3669 | ((range.flags & F2FS_TRIM_FILE_ZEROOUT) && | |
3670 | IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi))) | |
3671 | return -EOPNOTSUPP; | |
3672 | ||
3673 | file_start_write(filp); | |
3674 | inode_lock(inode); | |
3675 | ||
3676 | if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) || | |
3677 | range.start >= inode->i_size) { | |
3678 | ret = -EINVAL; | |
3679 | goto err; | |
3680 | } | |
3681 | ||
3682 | if (range.len == 0) | |
3683 | goto err; | |
3684 | ||
3685 | if (inode->i_size - range.start > range.len) { | |
3686 | end_addr = range.start + range.len; | |
3687 | } else { | |
3688 | end_addr = range.len == (u64)-1 ? | |
3689 | sbi->sb->s_maxbytes : inode->i_size; | |
3690 | to_end = true; | |
3691 | } | |
3692 | ||
3693 | if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) || | |
3694 | (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) { | |
3695 | ret = -EINVAL; | |
3696 | goto err; | |
3697 | } | |
3698 | ||
3699 | index = F2FS_BYTES_TO_BLK(range.start); | |
3700 | pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE); | |
3701 | ||
3702 | ret = f2fs_convert_inline_inode(inode); | |
3703 | if (ret) | |
3704 | goto err; | |
3705 | ||
e4544b63 | 3706 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 3707 | filemap_invalidate_lock(mapping); |
9af84648 DJ |
3708 | |
3709 | ret = filemap_write_and_wait_range(mapping, range.start, | |
3710 | to_end ? LLONG_MAX : end_addr - 1); | |
3711 | if (ret) | |
3712 | goto out; | |
3713 | ||
3714 | truncate_inode_pages_range(mapping, range.start, | |
3715 | to_end ? -1 : end_addr - 1); | |
3716 | ||
3717 | while (index < pg_end) { | |
3718 | struct dnode_of_data dn; | |
3719 | pgoff_t end_offset, count; | |
3720 | int i; | |
3721 | ||
3722 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3723 | ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); | |
3724 | if (ret) { | |
3725 | if (ret == -ENOENT) { | |
3726 | index = f2fs_get_next_page_offset(&dn, index); | |
3727 | continue; | |
3728 | } | |
3729 | goto out; | |
3730 | } | |
3731 | ||
3732 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3733 | count = min(end_offset - dn.ofs_in_node, pg_end - index); | |
3734 | for (i = 0; i < count; i++, index++, dn.ofs_in_node++) { | |
3735 | struct block_device *cur_bdev; | |
3736 | block_t blkaddr = f2fs_data_blkaddr(&dn); | |
3737 | ||
3738 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3739 | continue; | |
3740 | ||
3741 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3742 | DATA_GENERIC_ENHANCE)) { | |
3743 | ret = -EFSCORRUPTED; | |
3744 | f2fs_put_dnode(&dn); | |
3745 | goto out; | |
3746 | } | |
3747 | ||
3748 | cur_bdev = f2fs_target_device(sbi, blkaddr, NULL); | |
3749 | if (f2fs_is_multi_device(sbi)) { | |
3750 | int di = f2fs_target_device_index(sbi, blkaddr); | |
3751 | ||
3752 | blkaddr -= FDEV(di).start_blk; | |
3753 | } | |
3754 | ||
3755 | if (len) { | |
3756 | if (prev_bdev == cur_bdev && | |
3757 | index == prev_index + len && | |
3758 | blkaddr == prev_block + len) { | |
3759 | len++; | |
3760 | } else { | |
3761 | ret = f2fs_secure_erase(prev_bdev, | |
3762 | inode, prev_index, prev_block, | |
3763 | len, range.flags); | |
3764 | if (ret) { | |
3765 | f2fs_put_dnode(&dn); | |
3766 | goto out; | |
3767 | } | |
3768 | ||
3769 | len = 0; | |
3770 | } | |
3771 | } | |
3772 | ||
3773 | if (!len) { | |
3774 | prev_bdev = cur_bdev; | |
3775 | prev_index = index; | |
3776 | prev_block = blkaddr; | |
3777 | len = 1; | |
3778 | } | |
3779 | } | |
3780 | ||
3781 | f2fs_put_dnode(&dn); | |
3782 | ||
3783 | if (fatal_signal_pending(current)) { | |
3784 | ret = -EINTR; | |
3785 | goto out; | |
3786 | } | |
3787 | cond_resched(); | |
3788 | } | |
3789 | ||
3790 | if (len) | |
3791 | ret = f2fs_secure_erase(prev_bdev, inode, prev_index, | |
3792 | prev_block, len, range.flags); | |
3793 | out: | |
edc6d01b | 3794 | filemap_invalidate_unlock(mapping); |
e4544b63 | 3795 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
9af84648 DJ |
3796 | err: |
3797 | inode_unlock(inode); | |
3798 | file_end_write(filp); | |
3799 | ||
3800 | return ret; | |
3801 | } | |
3802 | ||
9e2a5f8c | 3803 | static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg) |
52656e6c | 3804 | { |
9e2a5f8c DJ |
3805 | struct inode *inode = file_inode(filp); |
3806 | struct f2fs_comp_option option; | |
3807 | ||
3808 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3809 | return -EOPNOTSUPP; | |
3810 | ||
3811 | inode_lock_shared(inode); | |
3812 | ||
3813 | if (!f2fs_compressed_file(inode)) { | |
3814 | inode_unlock_shared(inode); | |
3815 | return -ENODATA; | |
3816 | } | |
3817 | ||
3818 | option.algorithm = F2FS_I(inode)->i_compress_algorithm; | |
3819 | option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size; | |
3820 | ||
3821 | inode_unlock_shared(inode); | |
3822 | ||
3823 | if (copy_to_user((struct f2fs_comp_option __user *)arg, &option, | |
3824 | sizeof(option))) | |
3825 | return -EFAULT; | |
3826 | ||
3827 | return 0; | |
3828 | } | |
3829 | ||
e1e8debe DJ |
3830 | static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg) |
3831 | { | |
3832 | struct inode *inode = file_inode(filp); | |
3833 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3834 | struct f2fs_comp_option option; | |
3835 | int ret = 0; | |
1f227a3e | 3836 | |
e1e8debe DJ |
3837 | if (!f2fs_sb_has_compression(sbi)) |
3838 | return -EOPNOTSUPP; | |
3839 | ||
3840 | if (!(filp->f_mode & FMODE_WRITE)) | |
3841 | return -EBADF; | |
3842 | ||
3843 | if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg, | |
3844 | sizeof(option))) | |
3845 | return -EFAULT; | |
3846 | ||
3847 | if (!f2fs_compressed_file(inode) || | |
3848 | option.log_cluster_size < MIN_COMPRESS_LOG_SIZE || | |
3849 | option.log_cluster_size > MAX_COMPRESS_LOG_SIZE || | |
3850 | option.algorithm >= COMPRESS_MAX) | |
3851 | return -EINVAL; | |
3852 | ||
3853 | file_start_write(filp); | |
3854 | inode_lock(inode); | |
3855 | ||
3856 | if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) { | |
3857 | ret = -EBUSY; | |
3858 | goto out; | |
3859 | } | |
3860 | ||
3861 | if (inode->i_size != 0) { | |
3862 | ret = -EFBIG; | |
3863 | goto out; | |
3864 | } | |
3865 | ||
3866 | F2FS_I(inode)->i_compress_algorithm = option.algorithm; | |
3867 | F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size; | |
3868 | F2FS_I(inode)->i_cluster_size = 1 << option.log_cluster_size; | |
3869 | f2fs_mark_inode_dirty_sync(inode, true); | |
3870 | ||
3871 | if (!f2fs_is_compress_backend_ready(inode)) | |
3872 | f2fs_warn(sbi, "compression algorithm is successfully set, " | |
3873 | "but current kernel doesn't support this algorithm."); | |
3874 | out: | |
3875 | inode_unlock(inode); | |
3876 | file_end_write(filp); | |
3877 | ||
3878 | return ret; | |
3879 | } | |
3880 | ||
5fdb322f DJ |
3881 | static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len) |
3882 | { | |
fcd9ae4f | 3883 | DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx); |
5fdb322f DJ |
3884 | struct address_space *mapping = inode->i_mapping; |
3885 | struct page *page; | |
3886 | pgoff_t redirty_idx = page_idx; | |
3887 | int i, page_len = 0, ret = 0; | |
3888 | ||
3889 | page_cache_ra_unbounded(&ractl, len, 0); | |
3890 | ||
3891 | for (i = 0; i < len; i++, page_idx++) { | |
3892 | page = read_cache_page(mapping, page_idx, NULL, NULL); | |
3893 | if (IS_ERR(page)) { | |
3894 | ret = PTR_ERR(page); | |
3895 | break; | |
3896 | } | |
3897 | page_len++; | |
3898 | } | |
3899 | ||
3900 | for (i = 0; i < page_len; i++, redirty_idx++) { | |
3901 | page = find_lock_page(mapping, redirty_idx); | |
df0736d7 DJ |
3902 | if (!page) { |
3903 | ret = -ENOMEM; | |
3904 | break; | |
3905 | } | |
5fdb322f DJ |
3906 | set_page_dirty(page); |
3907 | f2fs_put_page(page, 1); | |
3908 | f2fs_put_page(page, 0); | |
3909 | } | |
3910 | ||
3911 | return ret; | |
3912 | } | |
3913 | ||
3914 | static int f2fs_ioc_decompress_file(struct file *filp, unsigned long arg) | |
3915 | { | |
3916 | struct inode *inode = file_inode(filp); | |
3917 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3918 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3919 | pgoff_t page_idx = 0, last_idx; | |
3920 | unsigned int blk_per_seg = sbi->blocks_per_seg; | |
3921 | int cluster_size = F2FS_I(inode)->i_cluster_size; | |
3922 | int count, ret; | |
3923 | ||
3924 | if (!f2fs_sb_has_compression(sbi) || | |
3925 | F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER) | |
3926 | return -EOPNOTSUPP; | |
3927 | ||
3928 | if (!(filp->f_mode & FMODE_WRITE)) | |
3929 | return -EBADF; | |
3930 | ||
3931 | if (!f2fs_compressed_file(inode)) | |
3932 | return -EINVAL; | |
3933 | ||
3934 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
3935 | ||
3936 | file_start_write(filp); | |
3937 | inode_lock(inode); | |
3938 | ||
3939 | if (!f2fs_is_compress_backend_ready(inode)) { | |
3940 | ret = -EOPNOTSUPP; | |
3941 | goto out; | |
3942 | } | |
3943 | ||
3944 | if (f2fs_is_mmap_file(inode)) { | |
3945 | ret = -EBUSY; | |
3946 | goto out; | |
3947 | } | |
3948 | ||
3949 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); | |
3950 | if (ret) | |
3951 | goto out; | |
3952 | ||
3953 | if (!atomic_read(&fi->i_compr_blocks)) | |
3954 | goto out; | |
3955 | ||
3956 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
3957 | ||
3958 | count = last_idx - page_idx; | |
3959 | while (count) { | |
3960 | int len = min(cluster_size, count); | |
3961 | ||
3962 | ret = redirty_blocks(inode, page_idx, len); | |
3963 | if (ret < 0) | |
3964 | break; | |
3965 | ||
3966 | if (get_dirty_pages(inode) >= blk_per_seg) | |
3967 | filemap_fdatawrite(inode->i_mapping); | |
3968 | ||
3969 | count -= len; | |
3970 | page_idx += len; | |
3971 | } | |
3972 | ||
3973 | if (!ret) | |
3974 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, | |
3975 | LLONG_MAX); | |
3976 | ||
3977 | if (ret) | |
833dcd35 JP |
3978 | f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.", |
3979 | __func__, ret); | |
5fdb322f DJ |
3980 | out: |
3981 | inode_unlock(inode); | |
3982 | file_end_write(filp); | |
3983 | ||
3984 | return ret; | |
3985 | } | |
3986 | ||
3987 | static int f2fs_ioc_compress_file(struct file *filp, unsigned long arg) | |
3988 | { | |
3989 | struct inode *inode = file_inode(filp); | |
3990 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3991 | pgoff_t page_idx = 0, last_idx; | |
3992 | unsigned int blk_per_seg = sbi->blocks_per_seg; | |
3993 | int cluster_size = F2FS_I(inode)->i_cluster_size; | |
3994 | int count, ret; | |
3995 | ||
3996 | if (!f2fs_sb_has_compression(sbi) || | |
3997 | F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER) | |
3998 | return -EOPNOTSUPP; | |
3999 | ||
4000 | if (!(filp->f_mode & FMODE_WRITE)) | |
4001 | return -EBADF; | |
4002 | ||
4003 | if (!f2fs_compressed_file(inode)) | |
4004 | return -EINVAL; | |
4005 | ||
4006 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
4007 | ||
4008 | file_start_write(filp); | |
4009 | inode_lock(inode); | |
4010 | ||
4011 | if (!f2fs_is_compress_backend_ready(inode)) { | |
4012 | ret = -EOPNOTSUPP; | |
4013 | goto out; | |
4014 | } | |
4015 | ||
4016 | if (f2fs_is_mmap_file(inode)) { | |
4017 | ret = -EBUSY; | |
4018 | goto out; | |
4019 | } | |
4020 | ||
4021 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); | |
4022 | if (ret) | |
4023 | goto out; | |
4024 | ||
4025 | set_inode_flag(inode, FI_ENABLE_COMPRESS); | |
4026 | ||
4027 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
4028 | ||
4029 | count = last_idx - page_idx; | |
4030 | while (count) { | |
4031 | int len = min(cluster_size, count); | |
4032 | ||
4033 | ret = redirty_blocks(inode, page_idx, len); | |
4034 | if (ret < 0) | |
4035 | break; | |
4036 | ||
4037 | if (get_dirty_pages(inode) >= blk_per_seg) | |
4038 | filemap_fdatawrite(inode->i_mapping); | |
4039 | ||
4040 | count -= len; | |
4041 | page_idx += len; | |
4042 | } | |
4043 | ||
4044 | if (!ret) | |
4045 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, | |
4046 | LLONG_MAX); | |
4047 | ||
4048 | clear_inode_flag(inode, FI_ENABLE_COMPRESS); | |
4049 | ||
4050 | if (ret) | |
833dcd35 JP |
4051 | f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.", |
4052 | __func__, ret); | |
5fdb322f DJ |
4053 | out: |
4054 | inode_unlock(inode); | |
4055 | file_end_write(filp); | |
4056 | ||
4057 | return ret; | |
4058 | } | |
4059 | ||
34178b1b | 4060 | static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
52656e6c JK |
4061 | { |
4062 | switch (cmd) { | |
3357af8f | 4063 | case FS_IOC_GETVERSION: |
d49f3e89 | 4064 | return f2fs_ioc_getversion(filp, arg); |
88b88a66 JK |
4065 | case F2FS_IOC_START_ATOMIC_WRITE: |
4066 | return f2fs_ioc_start_atomic_write(filp); | |
4067 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
4068 | return f2fs_ioc_commit_atomic_write(filp); | |
02a1335f | 4069 | case F2FS_IOC_START_VOLATILE_WRITE: |
1e84371f | 4070 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: |
1e84371f | 4071 | case F2FS_IOC_ABORT_VOLATILE_WRITE: |
7bc155fe | 4072 | return -EOPNOTSUPP; |
1abff93d JK |
4073 | case F2FS_IOC_SHUTDOWN: |
4074 | return f2fs_ioc_shutdown(filp, arg); | |
52656e6c JK |
4075 | case FITRIM: |
4076 | return f2fs_ioc_fitrim(filp, arg); | |
3357af8f | 4077 | case FS_IOC_SET_ENCRYPTION_POLICY: |
f424f664 | 4078 | return f2fs_ioc_set_encryption_policy(filp, arg); |
3357af8f | 4079 | case FS_IOC_GET_ENCRYPTION_POLICY: |
f424f664 | 4080 | return f2fs_ioc_get_encryption_policy(filp, arg); |
3357af8f | 4081 | case FS_IOC_GET_ENCRYPTION_PWSALT: |
f424f664 | 4082 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); |
8ce589c7 EB |
4083 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
4084 | return f2fs_ioc_get_encryption_policy_ex(filp, arg); | |
4085 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
4086 | return f2fs_ioc_add_encryption_key(filp, arg); | |
4087 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
4088 | return f2fs_ioc_remove_encryption_key(filp, arg); | |
4089 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
4090 | return f2fs_ioc_remove_encryption_key_all_users(filp, arg); | |
4091 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
4092 | return f2fs_ioc_get_encryption_key_status(filp, arg); | |
ee446e1a EB |
4093 | case FS_IOC_GET_ENCRYPTION_NONCE: |
4094 | return f2fs_ioc_get_encryption_nonce(filp, arg); | |
c1c1b583 CY |
4095 | case F2FS_IOC_GARBAGE_COLLECT: |
4096 | return f2fs_ioc_gc(filp, arg); | |
34dc77ad JK |
4097 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
4098 | return f2fs_ioc_gc_range(filp, arg); | |
456b88e4 | 4099 | case F2FS_IOC_WRITE_CHECKPOINT: |
059c0648 | 4100 | return f2fs_ioc_write_checkpoint(filp, arg); |
d323d005 CY |
4101 | case F2FS_IOC_DEFRAGMENT: |
4102 | return f2fs_ioc_defragment(filp, arg); | |
4dd6f977 JK |
4103 | case F2FS_IOC_MOVE_RANGE: |
4104 | return f2fs_ioc_move_range(filp, arg); | |
e066b83c JK |
4105 | case F2FS_IOC_FLUSH_DEVICE: |
4106 | return f2fs_ioc_flush_device(filp, arg); | |
e65ef207 JK |
4107 | case F2FS_IOC_GET_FEATURES: |
4108 | return f2fs_ioc_get_features(filp, arg); | |
1ad71a27 JK |
4109 | case F2FS_IOC_GET_PIN_FILE: |
4110 | return f2fs_ioc_get_pin_file(filp, arg); | |
4111 | case F2FS_IOC_SET_PIN_FILE: | |
4112 | return f2fs_ioc_set_pin_file(filp, arg); | |
c4020b2d CY |
4113 | case F2FS_IOC_PRECACHE_EXTENTS: |
4114 | return f2fs_ioc_precache_extents(filp, arg); | |
04f0b2ea QS |
4115 | case F2FS_IOC_RESIZE_FS: |
4116 | return f2fs_ioc_resize_fs(filp, arg); | |
95ae251f EB |
4117 | case FS_IOC_ENABLE_VERITY: |
4118 | return f2fs_ioc_enable_verity(filp, arg); | |
4119 | case FS_IOC_MEASURE_VERITY: | |
4120 | return f2fs_ioc_measure_verity(filp, arg); | |
e17fe657 EB |
4121 | case FS_IOC_READ_VERITY_METADATA: |
4122 | return f2fs_ioc_read_verity_metadata(filp, arg); | |
3357af8f EB |
4123 | case FS_IOC_GETFSLABEL: |
4124 | return f2fs_ioc_getfslabel(filp, arg); | |
4125 | case FS_IOC_SETFSLABEL: | |
4126 | return f2fs_ioc_setfslabel(filp, arg); | |
439dfb10 CY |
4127 | case F2FS_IOC_GET_COMPRESS_BLOCKS: |
4128 | return f2fs_get_compress_blocks(filp, arg); | |
ef8d563f CY |
4129 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: |
4130 | return f2fs_release_compress_blocks(filp, arg); | |
c75488fb CY |
4131 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: |
4132 | return f2fs_reserve_compress_blocks(filp, arg); | |
9af84648 DJ |
4133 | case F2FS_IOC_SEC_TRIM_FILE: |
4134 | return f2fs_sec_trim_file(filp, arg); | |
9e2a5f8c DJ |
4135 | case F2FS_IOC_GET_COMPRESS_OPTION: |
4136 | return f2fs_ioc_get_compress_option(filp, arg); | |
e1e8debe DJ |
4137 | case F2FS_IOC_SET_COMPRESS_OPTION: |
4138 | return f2fs_ioc_set_compress_option(filp, arg); | |
5fdb322f DJ |
4139 | case F2FS_IOC_DECOMPRESS_FILE: |
4140 | return f2fs_ioc_decompress_file(filp, arg); | |
4141 | case F2FS_IOC_COMPRESS_FILE: | |
4142 | return f2fs_ioc_compress_file(filp, arg); | |
fbfa2cc5 JK |
4143 | default: |
4144 | return -ENOTTY; | |
4145 | } | |
4146 | } | |
4147 | ||
34178b1b CY |
4148 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
4149 | { | |
4150 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) | |
4151 | return -EIO; | |
4152 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp)))) | |
4153 | return -ENOSPC; | |
4154 | ||
4155 | return __f2fs_ioctl(filp, cmd, arg); | |
4156 | } | |
4157 | ||
a1e09b03 EB |
4158 | /* |
4159 | * Return %true if the given read or write request should use direct I/O, or | |
4160 | * %false if it should use buffered I/O. | |
4161 | */ | |
4162 | static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb, | |
4163 | struct iov_iter *iter) | |
4164 | { | |
4165 | unsigned int align; | |
4166 | ||
4167 | if (!(iocb->ki_flags & IOCB_DIRECT)) | |
4168 | return false; | |
4169 | ||
4170 | if (f2fs_force_buffered_io(inode, iocb, iter)) | |
4171 | return false; | |
4172 | ||
4173 | /* | |
4174 | * Direct I/O not aligned to the disk's logical_block_size will be | |
4175 | * attempted, but will fail with -EINVAL. | |
4176 | * | |
4177 | * f2fs additionally requires that direct I/O be aligned to the | |
4178 | * filesystem block size, which is often a stricter requirement. | |
4179 | * However, f2fs traditionally falls back to buffered I/O on requests | |
4180 | * that are logical_block_size-aligned but not fs-block aligned. | |
4181 | * | |
4182 | * The below logic implements this behavior. | |
4183 | */ | |
4184 | align = iocb->ki_pos | iov_iter_alignment(iter); | |
4185 | if (!IS_ALIGNED(align, i_blocksize(inode)) && | |
4186 | IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev))) | |
4187 | return false; | |
4188 | ||
4189 | return true; | |
4190 | } | |
4191 | ||
4192 | static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error, | |
4193 | unsigned int flags) | |
4194 | { | |
4195 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp)); | |
4196 | ||
4197 | dec_page_count(sbi, F2FS_DIO_READ); | |
4198 | if (error) | |
4199 | return error; | |
4200 | f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, size); | |
4201 | return 0; | |
4202 | } | |
4203 | ||
4204 | static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = { | |
4205 | .end_io = f2fs_dio_read_end_io, | |
4206 | }; | |
4207 | ||
4208 | static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) | |
4c8ff709 CY |
4209 | { |
4210 | struct file *file = iocb->ki_filp; | |
4211 | struct inode *inode = file_inode(file); | |
a1e09b03 EB |
4212 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
4213 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
4214 | const loff_t pos = iocb->ki_pos; | |
4215 | const size_t count = iov_iter_count(to); | |
4216 | struct iomap_dio *dio; | |
4217 | ssize_t ret; | |
4218 | ||
4219 | if (count == 0) | |
4220 | return 0; /* skip atime update */ | |
4221 | ||
bd984c03 | 4222 | trace_f2fs_direct_IO_enter(inode, iocb, count, READ); |
a1e09b03 EB |
4223 | |
4224 | if (iocb->ki_flags & IOCB_NOWAIT) { | |
e4544b63 | 4225 | if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) { |
a1e09b03 EB |
4226 | ret = -EAGAIN; |
4227 | goto out; | |
4228 | } | |
4229 | } else { | |
e4544b63 | 4230 | f2fs_down_read(&fi->i_gc_rwsem[READ]); |
a1e09b03 EB |
4231 | } |
4232 | ||
4233 | /* | |
4234 | * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of | |
4235 | * the higher-level function iomap_dio_rw() in order to ensure that the | |
4236 | * F2FS_DIO_READ counter will be decremented correctly in all cases. | |
4237 | */ | |
4238 | inc_page_count(sbi, F2FS_DIO_READ); | |
4239 | dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops, | |
4240 | &f2fs_iomap_dio_read_ops, 0, 0); | |
4241 | if (IS_ERR_OR_NULL(dio)) { | |
4242 | ret = PTR_ERR_OR_ZERO(dio); | |
4243 | if (ret != -EIOCBQUEUED) | |
4244 | dec_page_count(sbi, F2FS_DIO_READ); | |
4245 | } else { | |
4246 | ret = iomap_dio_complete(dio); | |
4247 | } | |
4248 | ||
e4544b63 | 4249 | f2fs_up_read(&fi->i_gc_rwsem[READ]); |
a1e09b03 EB |
4250 | |
4251 | file_accessed(file); | |
4252 | out: | |
4253 | trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret); | |
4254 | return ret; | |
4255 | } | |
4256 | ||
4257 | static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) | |
4258 | { | |
4259 | struct inode *inode = file_inode(iocb->ki_filp); | |
c277f141 | 4260 | const loff_t pos = iocb->ki_pos; |
a1e09b03 | 4261 | ssize_t ret; |
4c8ff709 CY |
4262 | |
4263 | if (!f2fs_is_compress_backend_ready(inode)) | |
4264 | return -EOPNOTSUPP; | |
4265 | ||
c277f141 JK |
4266 | if (trace_f2fs_dataread_start_enabled()) { |
4267 | char *p = f2fs_kmalloc(F2FS_I_SB(inode), PATH_MAX, GFP_KERNEL); | |
4268 | char *path; | |
4269 | ||
4270 | if (!p) | |
4271 | goto skip_read_trace; | |
4272 | ||
4273 | path = dentry_path_raw(file_dentry(iocb->ki_filp), p, PATH_MAX); | |
4274 | if (IS_ERR(path)) { | |
4275 | kfree(p); | |
4276 | goto skip_read_trace; | |
4277 | } | |
8b83ac81 | 4278 | |
c277f141 JK |
4279 | trace_f2fs_dataread_start(inode, pos, iov_iter_count(to), |
4280 | current->pid, path, current->comm); | |
4281 | kfree(p); | |
4282 | } | |
4283 | skip_read_trace: | |
4284 | if (f2fs_should_use_dio(inode, iocb, to)) { | |
4285 | ret = f2fs_dio_read_iter(iocb, to); | |
4286 | } else { | |
4287 | ret = filemap_read(iocb, to, 0); | |
4288 | if (ret > 0) | |
4289 | f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_READ_IO, ret); | |
4290 | } | |
4291 | if (trace_f2fs_dataread_end_enabled()) | |
4292 | trace_f2fs_dataread_end(inode, pos, ret); | |
8b83ac81 | 4293 | return ret; |
4c8ff709 CY |
4294 | } |
4295 | ||
a1e09b03 EB |
4296 | static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from) |
4297 | { | |
4298 | struct file *file = iocb->ki_filp; | |
4299 | struct inode *inode = file_inode(file); | |
4300 | ssize_t count; | |
4301 | int err; | |
4302 | ||
4303 | if (IS_IMMUTABLE(inode)) | |
4304 | return -EPERM; | |
4305 | ||
4306 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) | |
4307 | return -EPERM; | |
4308 | ||
4309 | count = generic_write_checks(iocb, from); | |
4310 | if (count <= 0) | |
4311 | return count; | |
4312 | ||
4313 | err = file_modified(file); | |
4314 | if (err) | |
4315 | return err; | |
4316 | return count; | |
4317 | } | |
4318 | ||
3d697a4a EB |
4319 | /* |
4320 | * Preallocate blocks for a write request, if it is possible and helpful to do | |
4321 | * so. Returns a positive number if blocks may have been preallocated, 0 if no | |
4322 | * blocks were preallocated, or a negative errno value if something went | |
4323 | * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the | |
4324 | * requested blocks (not just some of them) have been allocated. | |
4325 | */ | |
a1e09b03 EB |
4326 | static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter, |
4327 | bool dio) | |
3d697a4a EB |
4328 | { |
4329 | struct inode *inode = file_inode(iocb->ki_filp); | |
4330 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
4331 | const loff_t pos = iocb->ki_pos; | |
4332 | const size_t count = iov_iter_count(iter); | |
4333 | struct f2fs_map_blocks map = {}; | |
3d697a4a EB |
4334 | int flag; |
4335 | int ret; | |
4336 | ||
4337 | /* If it will be an out-of-place direct write, don't bother. */ | |
4338 | if (dio && f2fs_lfs_mode(sbi)) | |
4339 | return 0; | |
d4dd19ec JK |
4340 | /* |
4341 | * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into | |
4342 | * buffered IO, if DIO meets any holes. | |
4343 | */ | |
4344 | if (dio && i_size_read(inode) && | |
4345 | (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode)))) | |
4346 | return 0; | |
3d697a4a EB |
4347 | |
4348 | /* No-wait I/O can't allocate blocks. */ | |
4349 | if (iocb->ki_flags & IOCB_NOWAIT) | |
4350 | return 0; | |
4351 | ||
4352 | /* If it will be a short write, don't bother. */ | |
4353 | if (fault_in_iov_iter_readable(iter, count)) | |
4354 | return 0; | |
4355 | ||
4356 | if (f2fs_has_inline_data(inode)) { | |
4357 | /* If the data will fit inline, don't bother. */ | |
4358 | if (pos + count <= MAX_INLINE_DATA(inode)) | |
4359 | return 0; | |
4360 | ret = f2fs_convert_inline_inode(inode); | |
4361 | if (ret) | |
4362 | return ret; | |
4363 | } | |
4364 | ||
4365 | /* Do not preallocate blocks that will be written partially in 4KB. */ | |
4366 | map.m_lblk = F2FS_BLK_ALIGN(pos); | |
4367 | map.m_len = F2FS_BYTES_TO_BLK(pos + count); | |
4368 | if (map.m_len > map.m_lblk) | |
4369 | map.m_len -= map.m_lblk; | |
4370 | else | |
4371 | map.m_len = 0; | |
4372 | map.m_may_create = true; | |
4373 | if (dio) { | |
4374 | map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint); | |
4375 | flag = F2FS_GET_BLOCK_PRE_DIO; | |
4376 | } else { | |
4377 | map.m_seg_type = NO_CHECK_TYPE; | |
4378 | flag = F2FS_GET_BLOCK_PRE_AIO; | |
4379 | } | |
4380 | ||
4381 | ret = f2fs_map_blocks(inode, &map, 1, flag); | |
d4dd19ec JK |
4382 | /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */ |
4383 | if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0)) | |
3d697a4a EB |
4384 | return ret; |
4385 | if (ret == 0) | |
4386 | set_inode_flag(inode, FI_PREALLOCATED_ALL); | |
4387 | return map.m_len; | |
4388 | } | |
4389 | ||
a1e09b03 EB |
4390 | static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb, |
4391 | struct iov_iter *from) | |
fcc85a4d | 4392 | { |
b439b103 JK |
4393 | struct file *file = iocb->ki_filp; |
4394 | struct inode *inode = file_inode(file); | |
a1e09b03 EB |
4395 | ssize_t ret; |
4396 | ||
4397 | if (iocb->ki_flags & IOCB_NOWAIT) | |
4398 | return -EOPNOTSUPP; | |
4399 | ||
4400 | current->backing_dev_info = inode_to_bdi(inode); | |
800ba295 | 4401 | ret = generic_perform_write(iocb, from); |
a1e09b03 EB |
4402 | current->backing_dev_info = NULL; |
4403 | ||
4404 | if (ret > 0) { | |
4405 | iocb->ki_pos += ret; | |
4406 | f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_IO, ret); | |
4407 | } | |
4408 | return ret; | |
4409 | } | |
4410 | ||
4411 | static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error, | |
4412 | unsigned int flags) | |
4413 | { | |
4414 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp)); | |
4415 | ||
4416 | dec_page_count(sbi, F2FS_DIO_WRITE); | |
4417 | if (error) | |
4418 | return error; | |
4419 | f2fs_update_iostat(sbi, APP_DIRECT_IO, size); | |
4420 | return 0; | |
4421 | } | |
4422 | ||
4423 | static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = { | |
4424 | .end_io = f2fs_dio_write_end_io, | |
4425 | }; | |
4426 | ||
4427 | static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from, | |
4428 | bool *may_need_sync) | |
4429 | { | |
4430 | struct file *file = iocb->ki_filp; | |
4431 | struct inode *inode = file_inode(file); | |
4432 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
4433 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
4434 | const bool do_opu = f2fs_lfs_mode(sbi); | |
a1e09b03 EB |
4435 | const loff_t pos = iocb->ki_pos; |
4436 | const ssize_t count = iov_iter_count(from); | |
a1e09b03 EB |
4437 | unsigned int dio_flags; |
4438 | struct iomap_dio *dio; | |
4439 | ssize_t ret; | |
4440 | ||
bd984c03 | 4441 | trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE); |
a1e09b03 EB |
4442 | |
4443 | if (iocb->ki_flags & IOCB_NOWAIT) { | |
4444 | /* f2fs_convert_inline_inode() and block allocation can block */ | |
4445 | if (f2fs_has_inline_data(inode) || | |
4446 | !f2fs_overwrite_io(inode, pos, count)) { | |
4447 | ret = -EAGAIN; | |
4448 | goto out; | |
4449 | } | |
4450 | ||
e4544b63 | 4451 | if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) { |
a1e09b03 EB |
4452 | ret = -EAGAIN; |
4453 | goto out; | |
4454 | } | |
e4544b63 TM |
4455 | if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) { |
4456 | f2fs_up_read(&fi->i_gc_rwsem[WRITE]); | |
a1e09b03 EB |
4457 | ret = -EAGAIN; |
4458 | goto out; | |
4459 | } | |
4460 | } else { | |
4461 | ret = f2fs_convert_inline_inode(inode); | |
4462 | if (ret) | |
4463 | goto out; | |
4464 | ||
e4544b63 | 4465 | f2fs_down_read(&fi->i_gc_rwsem[WRITE]); |
a1e09b03 | 4466 | if (do_opu) |
e4544b63 | 4467 | f2fs_down_read(&fi->i_gc_rwsem[READ]); |
a1e09b03 | 4468 | } |
a1e09b03 EB |
4469 | |
4470 | /* | |
4471 | * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of | |
4472 | * the higher-level function iomap_dio_rw() in order to ensure that the | |
4473 | * F2FS_DIO_WRITE counter will be decremented correctly in all cases. | |
4474 | */ | |
4475 | inc_page_count(sbi, F2FS_DIO_WRITE); | |
4476 | dio_flags = 0; | |
4477 | if (pos + count > inode->i_size) | |
4478 | dio_flags |= IOMAP_DIO_FORCE_WAIT; | |
4479 | dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops, | |
4480 | &f2fs_iomap_dio_write_ops, dio_flags, 0); | |
4481 | if (IS_ERR_OR_NULL(dio)) { | |
4482 | ret = PTR_ERR_OR_ZERO(dio); | |
4483 | if (ret == -ENOTBLK) | |
4484 | ret = 0; | |
4485 | if (ret != -EIOCBQUEUED) | |
4486 | dec_page_count(sbi, F2FS_DIO_WRITE); | |
4487 | } else { | |
4488 | ret = iomap_dio_complete(dio); | |
4489 | } | |
4490 | ||
a1e09b03 | 4491 | if (do_opu) |
e4544b63 TM |
4492 | f2fs_up_read(&fi->i_gc_rwsem[READ]); |
4493 | f2fs_up_read(&fi->i_gc_rwsem[WRITE]); | |
a1e09b03 EB |
4494 | |
4495 | if (ret < 0) | |
4496 | goto out; | |
4497 | if (pos + ret > inode->i_size) | |
4498 | f2fs_i_size_write(inode, pos + ret); | |
4499 | if (!do_opu) | |
4500 | set_inode_flag(inode, FI_UPDATE_WRITE); | |
4501 | ||
4502 | if (iov_iter_count(from)) { | |
4503 | ssize_t ret2; | |
4504 | loff_t bufio_start_pos = iocb->ki_pos; | |
4505 | ||
4506 | /* | |
4507 | * The direct write was partial, so we need to fall back to a | |
4508 | * buffered write for the remainder. | |
4509 | */ | |
4510 | ||
4511 | ret2 = f2fs_buffered_write_iter(iocb, from); | |
4512 | if (iov_iter_count(from)) | |
4513 | f2fs_write_failed(inode, iocb->ki_pos); | |
4514 | if (ret2 < 0) | |
4515 | goto out; | |
4516 | ||
4517 | /* | |
4518 | * Ensure that the pagecache pages are written to disk and | |
4519 | * invalidated to preserve the expected O_DIRECT semantics. | |
4520 | */ | |
4521 | if (ret2 > 0) { | |
4522 | loff_t bufio_end_pos = bufio_start_pos + ret2 - 1; | |
4523 | ||
4524 | ret += ret2; | |
4525 | ||
4526 | ret2 = filemap_write_and_wait_range(file->f_mapping, | |
4527 | bufio_start_pos, | |
4528 | bufio_end_pos); | |
4529 | if (ret2 < 0) | |
4530 | goto out; | |
4531 | invalidate_mapping_pages(file->f_mapping, | |
4532 | bufio_start_pos >> PAGE_SHIFT, | |
4533 | bufio_end_pos >> PAGE_SHIFT); | |
4534 | } | |
4535 | } else { | |
4536 | /* iomap_dio_rw() already handled the generic_write_sync(). */ | |
4537 | *may_need_sync = false; | |
4538 | } | |
4539 | out: | |
4540 | trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret); | |
4541 | return ret; | |
4542 | } | |
4543 | ||
4544 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
4545 | { | |
4546 | struct inode *inode = file_inode(iocb->ki_filp); | |
ccf7cf92 EB |
4547 | const loff_t orig_pos = iocb->ki_pos; |
4548 | const size_t orig_count = iov_iter_count(from); | |
3d697a4a | 4549 | loff_t target_size; |
a1e09b03 EB |
4550 | bool dio; |
4551 | bool may_need_sync = true; | |
3d697a4a | 4552 | int preallocated; |
b439b103 | 4553 | ssize_t ret; |
fcc85a4d | 4554 | |
126ce721 CY |
4555 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) { |
4556 | ret = -EIO; | |
4557 | goto out; | |
4558 | } | |
1f227a3e | 4559 | |
7bd29358 CY |
4560 | if (!f2fs_is_compress_backend_ready(inode)) { |
4561 | ret = -EOPNOTSUPP; | |
4562 | goto out; | |
4563 | } | |
4c8ff709 | 4564 | |
cb8434f1 GR |
4565 | if (iocb->ki_flags & IOCB_NOWAIT) { |
4566 | if (!inode_trylock(inode)) { | |
126ce721 CY |
4567 | ret = -EAGAIN; |
4568 | goto out; | |
4569 | } | |
cb8434f1 | 4570 | } else { |
b91050a8 HL |
4571 | inode_lock(inode); |
4572 | } | |
4573 | ||
a1e09b03 | 4574 | ret = f2fs_write_checks(iocb, from); |
b31bf0f9 EB |
4575 | if (ret <= 0) |
4576 | goto out_unlock; | |
4577 | ||
a1e09b03 EB |
4578 | /* Determine whether we will do a direct write or a buffered write. */ |
4579 | dio = f2fs_should_use_dio(inode, iocb, from); | |
3d697a4a | 4580 | |
b31bf0f9 EB |
4581 | /* Possibly preallocate the blocks for the write. */ |
4582 | target_size = iocb->ki_pos + iov_iter_count(from); | |
a1e09b03 | 4583 | preallocated = f2fs_preallocate_blocks(iocb, from, dio); |
c277f141 | 4584 | if (preallocated < 0) { |
b31bf0f9 | 4585 | ret = preallocated; |
c277f141 JK |
4586 | } else { |
4587 | if (trace_f2fs_datawrite_start_enabled()) { | |
4588 | char *p = f2fs_kmalloc(F2FS_I_SB(inode), | |
4589 | PATH_MAX, GFP_KERNEL); | |
4590 | char *path; | |
4591 | ||
4592 | if (!p) | |
4593 | goto skip_write_trace; | |
4594 | path = dentry_path_raw(file_dentry(iocb->ki_filp), | |
4595 | p, PATH_MAX); | |
4596 | if (IS_ERR(path)) { | |
4597 | kfree(p); | |
4598 | goto skip_write_trace; | |
4599 | } | |
4600 | trace_f2fs_datawrite_start(inode, orig_pos, orig_count, | |
4601 | current->pid, path, current->comm); | |
4602 | kfree(p); | |
4603 | } | |
4604 | skip_write_trace: | |
a1e09b03 EB |
4605 | /* Do the actual write. */ |
4606 | ret = dio ? | |
4607 | f2fs_dio_write_iter(iocb, from, &may_need_sync): | |
4608 | f2fs_buffered_write_iter(iocb, from); | |
dc7a10dd | 4609 | |
c277f141 JK |
4610 | if (trace_f2fs_datawrite_end_enabled()) |
4611 | trace_f2fs_datawrite_end(inode, orig_pos, ret); | |
4612 | } | |
4613 | ||
b31bf0f9 | 4614 | /* Don't leave any preallocated blocks around past i_size. */ |
d4dd19ec | 4615 | if (preallocated && i_size_read(inode) < target_size) { |
e4544b63 | 4616 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
b31bf0f9 | 4617 | filemap_invalidate_lock(inode->i_mapping); |
d4dd19ec JK |
4618 | if (!f2fs_truncate(inode)) |
4619 | file_dont_truncate(inode); | |
b31bf0f9 | 4620 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 4621 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
d4dd19ec JK |
4622 | } else { |
4623 | file_dont_truncate(inode); | |
b439b103 | 4624 | } |
b31bf0f9 EB |
4625 | |
4626 | clear_inode_flag(inode, FI_PREALLOCATED_ALL); | |
3d697a4a | 4627 | out_unlock: |
b439b103 | 4628 | inode_unlock(inode); |
126ce721 | 4629 | out: |
ccf7cf92 | 4630 | trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret); |
a1e09b03 | 4631 | if (ret > 0 && may_need_sync) |
e2592217 | 4632 | ret = generic_write_sync(iocb, ret); |
b439b103 | 4633 | return ret; |
fcc85a4d JK |
4634 | } |
4635 | ||
0f6b56ec DJ |
4636 | static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len, |
4637 | int advice) | |
4638 | { | |
0f6b56ec DJ |
4639 | struct address_space *mapping; |
4640 | struct backing_dev_info *bdi; | |
e64347ae FC |
4641 | struct inode *inode = file_inode(filp); |
4642 | int err; | |
0f6b56ec DJ |
4643 | |
4644 | if (advice == POSIX_FADV_SEQUENTIAL) { | |
0f6b56ec DJ |
4645 | if (S_ISFIFO(inode->i_mode)) |
4646 | return -ESPIPE; | |
4647 | ||
4648 | mapping = filp->f_mapping; | |
4649 | if (!mapping || len < 0) | |
4650 | return -EINVAL; | |
4651 | ||
4652 | bdi = inode_to_bdi(mapping->host); | |
4653 | filp->f_ra.ra_pages = bdi->ra_pages * | |
4654 | F2FS_I_SB(inode)->seq_file_ra_mul; | |
4655 | spin_lock(&filp->f_lock); | |
4656 | filp->f_mode &= ~FMODE_RANDOM; | |
4657 | spin_unlock(&filp->f_lock); | |
4658 | return 0; | |
4659 | } | |
4660 | ||
e64347ae FC |
4661 | err = generic_fadvise(filp, offset, len, advice); |
4662 | if (!err && advice == POSIX_FADV_DONTNEED && | |
4663 | test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) && | |
4664 | f2fs_compressed_file(inode)) | |
4665 | f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino); | |
4666 | ||
4667 | return err; | |
0f6b56ec DJ |
4668 | } |
4669 | ||
e9750824 | 4670 | #ifdef CONFIG_COMPAT |
34178b1b CY |
4671 | struct compat_f2fs_gc_range { |
4672 | u32 sync; | |
4673 | compat_u64 start; | |
4674 | compat_u64 len; | |
4675 | }; | |
4676 | #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\ | |
4677 | struct compat_f2fs_gc_range) | |
4678 | ||
4679 | static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg) | |
4680 | { | |
4681 | struct compat_f2fs_gc_range __user *urange; | |
4682 | struct f2fs_gc_range range; | |
4683 | int err; | |
4684 | ||
4685 | urange = compat_ptr(arg); | |
4686 | err = get_user(range.sync, &urange->sync); | |
4687 | err |= get_user(range.start, &urange->start); | |
4688 | err |= get_user(range.len, &urange->len); | |
4689 | if (err) | |
4690 | return -EFAULT; | |
4691 | ||
4692 | return __f2fs_ioc_gc_range(file, &range); | |
4693 | } | |
4694 | ||
4695 | struct compat_f2fs_move_range { | |
4696 | u32 dst_fd; | |
4697 | compat_u64 pos_in; | |
4698 | compat_u64 pos_out; | |
4699 | compat_u64 len; | |
4700 | }; | |
4701 | #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \ | |
4702 | struct compat_f2fs_move_range) | |
4703 | ||
4704 | static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg) | |
4705 | { | |
4706 | struct compat_f2fs_move_range __user *urange; | |
4707 | struct f2fs_move_range range; | |
4708 | int err; | |
4709 | ||
4710 | urange = compat_ptr(arg); | |
4711 | err = get_user(range.dst_fd, &urange->dst_fd); | |
4712 | err |= get_user(range.pos_in, &urange->pos_in); | |
4713 | err |= get_user(range.pos_out, &urange->pos_out); | |
4714 | err |= get_user(range.len, &urange->len); | |
4715 | if (err) | |
4716 | return -EFAULT; | |
4717 | ||
4718 | return __f2fs_ioc_move_range(file, &range); | |
4719 | } | |
4720 | ||
e9750824 NJ |
4721 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
4722 | { | |
34178b1b CY |
4723 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file))))) |
4724 | return -EIO; | |
4725 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file)))) | |
4726 | return -ENOSPC; | |
4727 | ||
e9750824 | 4728 | switch (cmd) { |
3357af8f EB |
4729 | case FS_IOC32_GETVERSION: |
4730 | cmd = FS_IOC_GETVERSION; | |
04ef4b62 | 4731 | break; |
34178b1b CY |
4732 | case F2FS_IOC32_GARBAGE_COLLECT_RANGE: |
4733 | return f2fs_compat_ioc_gc_range(file, arg); | |
4734 | case F2FS_IOC32_MOVE_RANGE: | |
4735 | return f2fs_compat_ioc_move_range(file, arg); | |
04ef4b62 CY |
4736 | case F2FS_IOC_START_ATOMIC_WRITE: |
4737 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
4738 | case F2FS_IOC_START_VOLATILE_WRITE: | |
4739 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: | |
4740 | case F2FS_IOC_ABORT_VOLATILE_WRITE: | |
4741 | case F2FS_IOC_SHUTDOWN: | |
314999dc | 4742 | case FITRIM: |
3357af8f EB |
4743 | case FS_IOC_SET_ENCRYPTION_POLICY: |
4744 | case FS_IOC_GET_ENCRYPTION_PWSALT: | |
4745 | case FS_IOC_GET_ENCRYPTION_POLICY: | |
8ce589c7 EB |
4746 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
4747 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
4748 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
4749 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
4750 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
ee446e1a | 4751 | case FS_IOC_GET_ENCRYPTION_NONCE: |
04ef4b62 CY |
4752 | case F2FS_IOC_GARBAGE_COLLECT: |
4753 | case F2FS_IOC_WRITE_CHECKPOINT: | |
4754 | case F2FS_IOC_DEFRAGMENT: | |
e066b83c | 4755 | case F2FS_IOC_FLUSH_DEVICE: |
e65ef207 | 4756 | case F2FS_IOC_GET_FEATURES: |
1ad71a27 JK |
4757 | case F2FS_IOC_GET_PIN_FILE: |
4758 | case F2FS_IOC_SET_PIN_FILE: | |
c4020b2d | 4759 | case F2FS_IOC_PRECACHE_EXTENTS: |
04f0b2ea | 4760 | case F2FS_IOC_RESIZE_FS: |
95ae251f EB |
4761 | case FS_IOC_ENABLE_VERITY: |
4762 | case FS_IOC_MEASURE_VERITY: | |
e17fe657 | 4763 | case FS_IOC_READ_VERITY_METADATA: |
3357af8f EB |
4764 | case FS_IOC_GETFSLABEL: |
4765 | case FS_IOC_SETFSLABEL: | |
439dfb10 | 4766 | case F2FS_IOC_GET_COMPRESS_BLOCKS: |
ef8d563f | 4767 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: |
c75488fb | 4768 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: |
9af84648 | 4769 | case F2FS_IOC_SEC_TRIM_FILE: |
9e2a5f8c | 4770 | case F2FS_IOC_GET_COMPRESS_OPTION: |
e1e8debe | 4771 | case F2FS_IOC_SET_COMPRESS_OPTION: |
5fdb322f DJ |
4772 | case F2FS_IOC_DECOMPRESS_FILE: |
4773 | case F2FS_IOC_COMPRESS_FILE: | |
4dd6f977 | 4774 | break; |
e9750824 NJ |
4775 | default: |
4776 | return -ENOIOCTLCMD; | |
4777 | } | |
34178b1b | 4778 | return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
e9750824 NJ |
4779 | } |
4780 | #endif | |
4781 | ||
fbfa2cc5 | 4782 | const struct file_operations f2fs_file_operations = { |
267378d4 | 4783 | .llseek = f2fs_llseek, |
4c8ff709 | 4784 | .read_iter = f2fs_file_read_iter, |
fcc85a4d JK |
4785 | .write_iter = f2fs_file_write_iter, |
4786 | .open = f2fs_file_open, | |
12662234 | 4787 | .release = f2fs_release_file, |
fbfa2cc5 | 4788 | .mmap = f2fs_file_mmap, |
7a10f017 | 4789 | .flush = f2fs_file_flush, |
fbfa2cc5 JK |
4790 | .fsync = f2fs_sync_file, |
4791 | .fallocate = f2fs_fallocate, | |
4792 | .unlocked_ioctl = f2fs_ioctl, | |
e9750824 NJ |
4793 | #ifdef CONFIG_COMPAT |
4794 | .compat_ioctl = f2fs_compat_ioctl, | |
4795 | #endif | |
fbfa2cc5 | 4796 | .splice_read = generic_file_splice_read, |
8d020765 | 4797 | .splice_write = iter_file_splice_write, |
0f6b56ec | 4798 | .fadvise = f2fs_file_fadvise, |
fbfa2cc5 | 4799 | }; |