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