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