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