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