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 | ||
34dc77ad JK |
2483 | static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) |
2484 | { | |
2485 | struct inode *inode = file_inode(filp); | |
2486 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2487 | struct f2fs_gc_range range; | |
2488 | u64 end; | |
2489 | int ret; | |
2490 | ||
2491 | if (!capable(CAP_SYS_ADMIN)) | |
2492 | return -EPERM; | |
2493 | ||
2494 | if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg, | |
2495 | sizeof(range))) | |
2496 | return -EFAULT; | |
2497 | ||
2498 | if (f2fs_readonly(sbi->sb)) | |
2499 | return -EROFS; | |
2500 | ||
b82f6e34 | 2501 | end = range.start + range.len; |
fbbf7799 ST |
2502 | if (end < range.start || range.start < MAIN_BLKADDR(sbi) || |
2503 | end >= MAX_BLKADDR(sbi)) | |
b82f6e34 | 2504 | return -EINVAL; |
b82f6e34 | 2505 | |
34dc77ad JK |
2506 | ret = mnt_want_write_file(filp); |
2507 | if (ret) | |
2508 | return ret; | |
2509 | ||
34dc77ad JK |
2510 | do_more: |
2511 | if (!range.sync) { | |
fb24fea7 | 2512 | if (!down_write_trylock(&sbi->gc_lock)) { |
34dc77ad JK |
2513 | ret = -EBUSY; |
2514 | goto out; | |
2515 | } | |
2516 | } else { | |
fb24fea7 | 2517 | down_write(&sbi->gc_lock); |
34dc77ad JK |
2518 | } |
2519 | ||
2520 | ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start)); | |
97767500 QZ |
2521 | if (ret) { |
2522 | if (ret == -EBUSY) | |
2523 | ret = -EAGAIN; | |
2524 | goto out; | |
2525 | } | |
67b0e42b | 2526 | range.start += BLKS_PER_SEC(sbi); |
34dc77ad JK |
2527 | if (range.start <= end) |
2528 | goto do_more; | |
2529 | out: | |
2530 | mnt_drop_write_file(filp); | |
2531 | return ret; | |
2532 | } | |
2533 | ||
059c0648 | 2534 | static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) |
456b88e4 CY |
2535 | { |
2536 | struct inode *inode = file_inode(filp); | |
2537 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
7fb17fe4 | 2538 | int ret; |
456b88e4 CY |
2539 | |
2540 | if (!capable(CAP_SYS_ADMIN)) | |
2541 | return -EPERM; | |
2542 | ||
2543 | if (f2fs_readonly(sbi->sb)) | |
2544 | return -EROFS; | |
2545 | ||
4354994f | 2546 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { |
dcbb4c10 | 2547 | f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled."); |
4354994f DR |
2548 | return -EINVAL; |
2549 | } | |
2550 | ||
7fb17fe4 CY |
2551 | ret = mnt_want_write_file(filp); |
2552 | if (ret) | |
2553 | return ret; | |
2554 | ||
2555 | ret = f2fs_sync_fs(sbi->sb, 1); | |
2556 | ||
2557 | mnt_drop_write_file(filp); | |
2558 | return ret; | |
456b88e4 CY |
2559 | } |
2560 | ||
d323d005 CY |
2561 | static int f2fs_defragment_range(struct f2fs_sb_info *sbi, |
2562 | struct file *filp, | |
2563 | struct f2fs_defragment *range) | |
2564 | { | |
2565 | struct inode *inode = file_inode(filp); | |
f3d98e74 | 2566 | struct f2fs_map_blocks map = { .m_next_extent = NULL, |
f4f0b677 JZ |
2567 | .m_seg_type = NO_CHECK_TYPE , |
2568 | .m_may_create = false }; | |
1061fd48 | 2569 | struct extent_info ei = {0, 0, 0}; |
f3d98e74 | 2570 | pgoff_t pg_start, pg_end, next_pgofs; |
3519e3f9 | 2571 | unsigned int blk_per_seg = sbi->blocks_per_seg; |
d323d005 | 2572 | unsigned int total = 0, sec_num; |
d323d005 CY |
2573 | block_t blk_end = 0; |
2574 | bool fragmented = false; | |
2575 | int err; | |
2576 | ||
2577 | /* if in-place-update policy is enabled, don't waste time here */ | |
4d57b86d | 2578 | if (f2fs_should_update_inplace(inode, NULL)) |
d323d005 CY |
2579 | return -EINVAL; |
2580 | ||
09cbfeaf KS |
2581 | pg_start = range->start >> PAGE_SHIFT; |
2582 | pg_end = (range->start + range->len) >> PAGE_SHIFT; | |
d323d005 | 2583 | |
2c4db1a6 | 2584 | f2fs_balance_fs(sbi, true); |
d323d005 | 2585 | |
5955102c | 2586 | inode_lock(inode); |
d323d005 CY |
2587 | |
2588 | /* writeback all dirty pages in the range */ | |
2589 | err = filemap_write_and_wait_range(inode->i_mapping, range->start, | |
d8fe4f0e | 2590 | range->start + range->len - 1); |
d323d005 CY |
2591 | if (err) |
2592 | goto out; | |
2593 | ||
2594 | /* | |
2595 | * lookup mapping info in extent cache, skip defragmenting if physical | |
2596 | * block addresses are continuous. | |
2597 | */ | |
2598 | if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { | |
2599 | if (ei.fofs + ei.len >= pg_end) | |
2600 | goto out; | |
2601 | } | |
2602 | ||
2603 | map.m_lblk = pg_start; | |
f3d98e74 | 2604 | map.m_next_pgofs = &next_pgofs; |
d323d005 CY |
2605 | |
2606 | /* | |
2607 | * lookup mapping info in dnode page cache, skip defragmenting if all | |
2608 | * physical block addresses are continuous even if there are hole(s) | |
2609 | * in logical blocks. | |
2610 | */ | |
2611 | while (map.m_lblk < pg_end) { | |
a1c1e9b7 | 2612 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2613 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2614 | if (err) |
2615 | goto out; | |
2616 | ||
2617 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
f3d98e74 | 2618 | map.m_lblk = next_pgofs; |
d323d005 CY |
2619 | continue; |
2620 | } | |
2621 | ||
25a912e5 | 2622 | if (blk_end && blk_end != map.m_pblk) |
d323d005 | 2623 | fragmented = true; |
25a912e5 CY |
2624 | |
2625 | /* record total count of block that we're going to move */ | |
2626 | total += map.m_len; | |
2627 | ||
d323d005 CY |
2628 | blk_end = map.m_pblk + map.m_len; |
2629 | ||
2630 | map.m_lblk += map.m_len; | |
d323d005 CY |
2631 | } |
2632 | ||
d3a1a0e1 CY |
2633 | if (!fragmented) { |
2634 | total = 0; | |
d323d005 | 2635 | goto out; |
d3a1a0e1 | 2636 | } |
d323d005 | 2637 | |
f91108b8 | 2638 | sec_num = DIV_ROUND_UP(total, BLKS_PER_SEC(sbi)); |
d323d005 CY |
2639 | |
2640 | /* | |
2641 | * make sure there are enough free section for LFS allocation, this can | |
2642 | * avoid defragment running in SSR mode when free section are allocated | |
2643 | * intensively | |
2644 | */ | |
7f3037a5 | 2645 | if (has_not_enough_free_secs(sbi, 0, sec_num)) { |
d323d005 CY |
2646 | err = -EAGAIN; |
2647 | goto out; | |
2648 | } | |
2649 | ||
25a912e5 CY |
2650 | map.m_lblk = pg_start; |
2651 | map.m_len = pg_end - pg_start; | |
2652 | total = 0; | |
2653 | ||
d323d005 CY |
2654 | while (map.m_lblk < pg_end) { |
2655 | pgoff_t idx; | |
2656 | int cnt = 0; | |
2657 | ||
2658 | do_map: | |
a1c1e9b7 | 2659 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2660 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2661 | if (err) |
2662 | goto clear_out; | |
2663 | ||
2664 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
f3d98e74 | 2665 | map.m_lblk = next_pgofs; |
d3a1a0e1 | 2666 | goto check; |
d323d005 CY |
2667 | } |
2668 | ||
91942321 | 2669 | set_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 CY |
2670 | |
2671 | idx = map.m_lblk; | |
2672 | while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { | |
2673 | struct page *page; | |
2674 | ||
4d57b86d | 2675 | page = f2fs_get_lock_data_page(inode, idx, true); |
d323d005 CY |
2676 | if (IS_ERR(page)) { |
2677 | err = PTR_ERR(page); | |
2678 | goto clear_out; | |
2679 | } | |
2680 | ||
2681 | set_page_dirty(page); | |
2682 | f2fs_put_page(page, 1); | |
2683 | ||
2684 | idx++; | |
2685 | cnt++; | |
2686 | total++; | |
2687 | } | |
2688 | ||
2689 | map.m_lblk = idx; | |
d3a1a0e1 CY |
2690 | check: |
2691 | if (map.m_lblk < pg_end && cnt < blk_per_seg) | |
d323d005 CY |
2692 | goto do_map; |
2693 | ||
91942321 | 2694 | clear_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 CY |
2695 | |
2696 | err = filemap_fdatawrite(inode->i_mapping); | |
2697 | if (err) | |
2698 | goto out; | |
2699 | } | |
2700 | clear_out: | |
91942321 | 2701 | clear_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 | 2702 | out: |
5955102c | 2703 | inode_unlock(inode); |
d323d005 | 2704 | if (!err) |
09cbfeaf | 2705 | range->len = (u64)total << PAGE_SHIFT; |
d323d005 CY |
2706 | return err; |
2707 | } | |
2708 | ||
2709 | static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) | |
2710 | { | |
2711 | struct inode *inode = file_inode(filp); | |
2712 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2713 | struct f2fs_defragment range; | |
2714 | int err; | |
2715 | ||
2716 | if (!capable(CAP_SYS_ADMIN)) | |
2717 | return -EPERM; | |
2718 | ||
7eab0c0d | 2719 | if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode)) |
d323d005 CY |
2720 | return -EINVAL; |
2721 | ||
d7563861 KM |
2722 | if (f2fs_readonly(sbi->sb)) |
2723 | return -EROFS; | |
d323d005 CY |
2724 | |
2725 | if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, | |
d7563861 KM |
2726 | sizeof(range))) |
2727 | return -EFAULT; | |
d323d005 CY |
2728 | |
2729 | /* verify alignment of offset & size */ | |
d7563861 KM |
2730 | if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1)) |
2731 | return -EINVAL; | |
d323d005 | 2732 | |
1941d7bc | 2733 | if (unlikely((range.start + range.len) >> PAGE_SHIFT > |
d7563861 KM |
2734 | sbi->max_file_blocks)) |
2735 | return -EINVAL; | |
2736 | ||
2737 | err = mnt_want_write_file(filp); | |
2738 | if (err) | |
2739 | return err; | |
1941d7bc | 2740 | |
d323d005 | 2741 | err = f2fs_defragment_range(sbi, filp, &range); |
d7563861 KM |
2742 | mnt_drop_write_file(filp); |
2743 | ||
d0239e1b | 2744 | f2fs_update_time(sbi, REQ_TIME); |
d323d005 | 2745 | if (err < 0) |
d7563861 | 2746 | return err; |
d323d005 CY |
2747 | |
2748 | if (copy_to_user((struct f2fs_defragment __user *)arg, &range, | |
2749 | sizeof(range))) | |
d7563861 KM |
2750 | return -EFAULT; |
2751 | ||
2752 | return 0; | |
d323d005 CY |
2753 | } |
2754 | ||
4dd6f977 JK |
2755 | static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, |
2756 | struct file *file_out, loff_t pos_out, size_t len) | |
2757 | { | |
2758 | struct inode *src = file_inode(file_in); | |
2759 | struct inode *dst = file_inode(file_out); | |
2760 | struct f2fs_sb_info *sbi = F2FS_I_SB(src); | |
2761 | size_t olen = len, dst_max_i_size = 0; | |
2762 | size_t dst_osize; | |
2763 | int ret; | |
2764 | ||
2765 | if (file_in->f_path.mnt != file_out->f_path.mnt || | |
2766 | src->i_sb != dst->i_sb) | |
2767 | return -EXDEV; | |
2768 | ||
2769 | if (unlikely(f2fs_readonly(src->i_sb))) | |
2770 | return -EROFS; | |
2771 | ||
fe8494bf CY |
2772 | if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode)) |
2773 | return -EINVAL; | |
4dd6f977 | 2774 | |
62230e0d | 2775 | if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst)) |
4dd6f977 JK |
2776 | return -EOPNOTSUPP; |
2777 | ||
aad1383c DR |
2778 | if (pos_out < 0 || pos_in < 0) |
2779 | return -EINVAL; | |
2780 | ||
d95fd91c FL |
2781 | if (src == dst) { |
2782 | if (pos_in == pos_out) | |
2783 | return 0; | |
2784 | if (pos_out > pos_in && pos_out < pos_in + len) | |
2785 | return -EINVAL; | |
2786 | } | |
2787 | ||
4dd6f977 | 2788 | inode_lock(src); |
20a3d61d | 2789 | if (src != dst) { |
bb06664a CY |
2790 | ret = -EBUSY; |
2791 | if (!inode_trylock(dst)) | |
2792 | goto out; | |
20a3d61d | 2793 | } |
4dd6f977 JK |
2794 | |
2795 | ret = -EINVAL; | |
2796 | if (pos_in + len > src->i_size || pos_in + len < pos_in) | |
2797 | goto out_unlock; | |
2798 | if (len == 0) | |
2799 | olen = len = src->i_size - pos_in; | |
2800 | if (pos_in + len == src->i_size) | |
2801 | len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in; | |
2802 | if (len == 0) { | |
2803 | ret = 0; | |
2804 | goto out_unlock; | |
2805 | } | |
2806 | ||
2807 | dst_osize = dst->i_size; | |
2808 | if (pos_out + olen > dst->i_size) | |
2809 | dst_max_i_size = pos_out + olen; | |
2810 | ||
2811 | /* verify the end result is block aligned */ | |
2812 | if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) || | |
2813 | !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) || | |
2814 | !IS_ALIGNED(pos_out, F2FS_BLKSIZE)) | |
2815 | goto out_unlock; | |
2816 | ||
2817 | ret = f2fs_convert_inline_inode(src); | |
2818 | if (ret) | |
2819 | goto out_unlock; | |
2820 | ||
2821 | ret = f2fs_convert_inline_inode(dst); | |
2822 | if (ret) | |
2823 | goto out_unlock; | |
2824 | ||
2825 | /* write out all dirty pages from offset */ | |
2826 | ret = filemap_write_and_wait_range(src->i_mapping, | |
2827 | pos_in, pos_in + len); | |
2828 | if (ret) | |
2829 | goto out_unlock; | |
2830 | ||
2831 | ret = filemap_write_and_wait_range(dst->i_mapping, | |
2832 | pos_out, pos_out + len); | |
2833 | if (ret) | |
2834 | goto out_unlock; | |
2835 | ||
2836 | f2fs_balance_fs(sbi, true); | |
6f8d4455 JK |
2837 | |
2838 | down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); | |
2839 | if (src != dst) { | |
2840 | ret = -EBUSY; | |
2841 | if (!down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE])) | |
2842 | goto out_src; | |
2843 | } | |
2844 | ||
4dd6f977 | 2845 | f2fs_lock_op(sbi); |
61e4da11 FL |
2846 | ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS, |
2847 | pos_out >> F2FS_BLKSIZE_BITS, | |
2848 | len >> F2FS_BLKSIZE_BITS, false); | |
4dd6f977 JK |
2849 | |
2850 | if (!ret) { | |
2851 | if (dst_max_i_size) | |
2852 | f2fs_i_size_write(dst, dst_max_i_size); | |
2853 | else if (dst_osize != dst->i_size) | |
2854 | f2fs_i_size_write(dst, dst_osize); | |
2855 | } | |
2856 | f2fs_unlock_op(sbi); | |
6f8d4455 JK |
2857 | |
2858 | if (src != dst) | |
b2532c69 | 2859 | up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]); |
6f8d4455 JK |
2860 | out_src: |
2861 | up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]); | |
2862 | out_unlock: | |
2863 | if (src != dst) | |
4dd6f977 | 2864 | inode_unlock(dst); |
20a3d61d | 2865 | out: |
4dd6f977 JK |
2866 | inode_unlock(src); |
2867 | return ret; | |
2868 | } | |
2869 | ||
2870 | static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) | |
2871 | { | |
2872 | struct f2fs_move_range range; | |
2873 | struct fd dst; | |
2874 | int err; | |
2875 | ||
2876 | if (!(filp->f_mode & FMODE_READ) || | |
2877 | !(filp->f_mode & FMODE_WRITE)) | |
2878 | return -EBADF; | |
2879 | ||
2880 | if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, | |
2881 | sizeof(range))) | |
2882 | return -EFAULT; | |
2883 | ||
2884 | dst = fdget(range.dst_fd); | |
2885 | if (!dst.file) | |
2886 | return -EBADF; | |
2887 | ||
2888 | if (!(dst.file->f_mode & FMODE_WRITE)) { | |
2889 | err = -EBADF; | |
2890 | goto err_out; | |
2891 | } | |
2892 | ||
2893 | err = mnt_want_write_file(filp); | |
2894 | if (err) | |
2895 | goto err_out; | |
2896 | ||
2897 | err = f2fs_move_file_range(filp, range.pos_in, dst.file, | |
2898 | range.pos_out, range.len); | |
2899 | ||
2900 | mnt_drop_write_file(filp); | |
3cecfa5f KM |
2901 | if (err) |
2902 | goto err_out; | |
4dd6f977 JK |
2903 | |
2904 | if (copy_to_user((struct f2fs_move_range __user *)arg, | |
2905 | &range, sizeof(range))) | |
2906 | err = -EFAULT; | |
2907 | err_out: | |
2908 | fdput(dst); | |
2909 | return err; | |
2910 | } | |
2911 | ||
e066b83c JK |
2912 | static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) |
2913 | { | |
2914 | struct inode *inode = file_inode(filp); | |
2915 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2916 | struct sit_info *sm = SIT_I(sbi); | |
2917 | unsigned int start_segno = 0, end_segno = 0; | |
2918 | unsigned int dev_start_segno = 0, dev_end_segno = 0; | |
2919 | struct f2fs_flush_device range; | |
2920 | int ret; | |
2921 | ||
2922 | if (!capable(CAP_SYS_ADMIN)) | |
2923 | return -EPERM; | |
2924 | ||
2925 | if (f2fs_readonly(sbi->sb)) | |
2926 | return -EROFS; | |
2927 | ||
4354994f DR |
2928 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) |
2929 | return -EINVAL; | |
2930 | ||
e066b83c JK |
2931 | if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg, |
2932 | sizeof(range))) | |
2933 | return -EFAULT; | |
2934 | ||
0916878d | 2935 | if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num || |
2c70c5e3 | 2936 | __is_large_section(sbi)) { |
dcbb4c10 JP |
2937 | f2fs_warn(sbi, "Can't flush %u in %d for segs_per_sec %u != 1", |
2938 | range.dev_num, sbi->s_ndevs, sbi->segs_per_sec); | |
e066b83c JK |
2939 | return -EINVAL; |
2940 | } | |
2941 | ||
2942 | ret = mnt_want_write_file(filp); | |
2943 | if (ret) | |
2944 | return ret; | |
2945 | ||
2946 | if (range.dev_num != 0) | |
2947 | dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk); | |
2948 | dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk); | |
2949 | ||
2950 | start_segno = sm->last_victim[FLUSH_DEVICE]; | |
2951 | if (start_segno < dev_start_segno || start_segno >= dev_end_segno) | |
2952 | start_segno = dev_start_segno; | |
2953 | end_segno = min(start_segno + range.segments, dev_end_segno); | |
2954 | ||
2955 | while (start_segno < end_segno) { | |
fb24fea7 | 2956 | if (!down_write_trylock(&sbi->gc_lock)) { |
e066b83c JK |
2957 | ret = -EBUSY; |
2958 | goto out; | |
2959 | } | |
2960 | sm->last_victim[GC_CB] = end_segno + 1; | |
2961 | sm->last_victim[GC_GREEDY] = end_segno + 1; | |
2962 | sm->last_victim[ALLOC_NEXT] = end_segno + 1; | |
2963 | ret = f2fs_gc(sbi, true, true, start_segno); | |
2964 | if (ret == -EAGAIN) | |
2965 | ret = 0; | |
2966 | else if (ret < 0) | |
2967 | break; | |
2968 | start_segno++; | |
2969 | } | |
2970 | out: | |
2971 | mnt_drop_write_file(filp); | |
2972 | return ret; | |
2973 | } | |
2974 | ||
e65ef207 JK |
2975 | static int f2fs_ioc_get_features(struct file *filp, unsigned long arg) |
2976 | { | |
2977 | struct inode *inode = file_inode(filp); | |
2978 | u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature); | |
2979 | ||
2980 | /* Must validate to set it with SQLite behavior in Android. */ | |
2981 | sb_feature |= F2FS_FEATURE_ATOMIC_WRITE; | |
2982 | ||
2983 | return put_user(sb_feature, (u32 __user *)arg); | |
2984 | } | |
e066b83c | 2985 | |
2c1d0305 | 2986 | #ifdef CONFIG_QUOTA |
78130819 CY |
2987 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) |
2988 | { | |
2989 | struct dquot *transfer_to[MAXQUOTAS] = {}; | |
2990 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2991 | struct super_block *sb = sbi->sb; | |
2992 | int err = 0; | |
2993 | ||
2994 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); | |
2995 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
2996 | err = __dquot_transfer(inode, transfer_to); | |
2997 | if (err) | |
2998 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); | |
2999 | dqput(transfer_to[PRJQUOTA]); | |
3000 | } | |
3001 | return err; | |
3002 | } | |
3003 | ||
2c1d0305 CY |
3004 | static int f2fs_ioc_setproject(struct file *filp, __u32 projid) |
3005 | { | |
3006 | struct inode *inode = file_inode(filp); | |
3007 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3008 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2c1d0305 CY |
3009 | struct page *ipage; |
3010 | kprojid_t kprojid; | |
3011 | int err; | |
3012 | ||
7beb01f7 | 3013 | if (!f2fs_sb_has_project_quota(sbi)) { |
2c1d0305 CY |
3014 | if (projid != F2FS_DEF_PROJID) |
3015 | return -EOPNOTSUPP; | |
3016 | else | |
3017 | return 0; | |
3018 | } | |
3019 | ||
3020 | if (!f2fs_has_extra_attr(inode)) | |
3021 | return -EOPNOTSUPP; | |
3022 | ||
3023 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
3024 | ||
3025 | if (projid_eq(kprojid, F2FS_I(inode)->i_projid)) | |
3026 | return 0; | |
3027 | ||
2c1d0305 | 3028 | err = -EPERM; |
2c1d0305 CY |
3029 | /* Is it quota file? Do not allow user to mess with it */ |
3030 | if (IS_NOQUOTA(inode)) | |
c8e92757 | 3031 | return err; |
2c1d0305 | 3032 | |
4d57b86d | 3033 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
c8e92757 WS |
3034 | if (IS_ERR(ipage)) |
3035 | return PTR_ERR(ipage); | |
2c1d0305 CY |
3036 | |
3037 | if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize, | |
3038 | i_projid)) { | |
3039 | err = -EOVERFLOW; | |
3040 | f2fs_put_page(ipage, 1); | |
c8e92757 | 3041 | return err; |
2c1d0305 CY |
3042 | } |
3043 | f2fs_put_page(ipage, 1); | |
3044 | ||
c22aecd7 CY |
3045 | err = dquot_initialize(inode); |
3046 | if (err) | |
c8e92757 | 3047 | return err; |
2c1d0305 | 3048 | |
78130819 CY |
3049 | f2fs_lock_op(sbi); |
3050 | err = f2fs_transfer_project_quota(inode, kprojid); | |
3051 | if (err) | |
3052 | goto out_unlock; | |
2c1d0305 CY |
3053 | |
3054 | F2FS_I(inode)->i_projid = kprojid; | |
3055 | inode->i_ctime = current_time(inode); | |
2c1d0305 | 3056 | f2fs_mark_inode_dirty_sync(inode, true); |
78130819 CY |
3057 | out_unlock: |
3058 | f2fs_unlock_op(sbi); | |
2c1d0305 CY |
3059 | return err; |
3060 | } | |
3061 | #else | |
78130819 CY |
3062 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid) |
3063 | { | |
3064 | return 0; | |
3065 | } | |
3066 | ||
2c1d0305 CY |
3067 | static int f2fs_ioc_setproject(struct file *filp, __u32 projid) |
3068 | { | |
3069 | if (projid != F2FS_DEF_PROJID) | |
3070 | return -EOPNOTSUPP; | |
3071 | return 0; | |
3072 | } | |
3073 | #endif | |
3074 | ||
36098557 EB |
3075 | /* FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR support */ |
3076 | ||
3077 | /* | |
3078 | * To make a new on-disk f2fs i_flag gettable via FS_IOC_FSGETXATTR and settable | |
3079 | * via FS_IOC_FSSETXATTR, add an entry for it to f2fs_xflags_map[], and add its | |
3080 | * FS_XFLAG_* equivalent to F2FS_SUPPORTED_XFLAGS. | |
3081 | */ | |
3082 | ||
3083 | static const struct { | |
3084 | u32 iflag; | |
3085 | u32 xflag; | |
3086 | } f2fs_xflags_map[] = { | |
3087 | { F2FS_SYNC_FL, FS_XFLAG_SYNC }, | |
3088 | { F2FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE }, | |
3089 | { F2FS_APPEND_FL, FS_XFLAG_APPEND }, | |
3090 | { F2FS_NODUMP_FL, FS_XFLAG_NODUMP }, | |
3091 | { F2FS_NOATIME_FL, FS_XFLAG_NOATIME }, | |
3092 | { F2FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT }, | |
3093 | }; | |
3094 | ||
3095 | #define F2FS_SUPPORTED_XFLAGS ( \ | |
3096 | FS_XFLAG_SYNC | \ | |
3097 | FS_XFLAG_IMMUTABLE | \ | |
3098 | FS_XFLAG_APPEND | \ | |
3099 | FS_XFLAG_NODUMP | \ | |
3100 | FS_XFLAG_NOATIME | \ | |
3101 | FS_XFLAG_PROJINHERIT) | |
3102 | ||
3103 | /* Convert f2fs on-disk i_flags to FS_IOC_FS{GET,SET}XATTR flags */ | |
3104 | static inline u32 f2fs_iflags_to_xflags(u32 iflags) | |
3105 | { | |
3106 | u32 xflags = 0; | |
3107 | int i; | |
3108 | ||
3109 | for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++) | |
3110 | if (iflags & f2fs_xflags_map[i].iflag) | |
3111 | xflags |= f2fs_xflags_map[i].xflag; | |
3112 | ||
2c1d0305 CY |
3113 | return xflags; |
3114 | } | |
3115 | ||
36098557 EB |
3116 | /* Convert FS_IOC_FS{GET,SET}XATTR flags to f2fs on-disk i_flags */ |
3117 | static inline u32 f2fs_xflags_to_iflags(u32 xflags) | |
2c1d0305 | 3118 | { |
36098557 EB |
3119 | u32 iflags = 0; |
3120 | int i; | |
2c1d0305 | 3121 | |
36098557 EB |
3122 | for (i = 0; i < ARRAY_SIZE(f2fs_xflags_map); i++) |
3123 | if (xflags & f2fs_xflags_map[i].xflag) | |
3124 | iflags |= f2fs_xflags_map[i].iflag; | |
2c1d0305 CY |
3125 | |
3126 | return iflags; | |
3127 | } | |
3128 | ||
6fc93c4e | 3129 | static void f2fs_fill_fsxattr(struct inode *inode, struct fsxattr *fa) |
2c1d0305 | 3130 | { |
2c1d0305 | 3131 | struct f2fs_inode_info *fi = F2FS_I(inode); |
2c1d0305 | 3132 | |
6fc93c4e | 3133 | simple_fill_fsxattr(fa, f2fs_iflags_to_xflags(fi->i_flags)); |
2c1d0305 | 3134 | |
7beb01f7 | 3135 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode))) |
6fc93c4e | 3136 | fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid); |
2c1d0305 CY |
3137 | } |
3138 | ||
6fc93c4e | 3139 | static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg) |
c8e92757 | 3140 | { |
6fc93c4e EB |
3141 | struct inode *inode = file_inode(filp); |
3142 | struct fsxattr fa; | |
c8e92757 | 3143 | |
6fc93c4e | 3144 | f2fs_fill_fsxattr(inode, &fa); |
c8e92757 | 3145 | |
6fc93c4e EB |
3146 | if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa))) |
3147 | return -EFAULT; | |
c8e92757 WS |
3148 | return 0; |
3149 | } | |
3150 | ||
2c1d0305 CY |
3151 | static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg) |
3152 | { | |
3153 | struct inode *inode = file_inode(filp); | |
6fc93c4e | 3154 | struct fsxattr fa, old_fa; |
36098557 | 3155 | u32 iflags; |
2c1d0305 CY |
3156 | int err; |
3157 | ||
3158 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa))) | |
3159 | return -EFAULT; | |
3160 | ||
3161 | /* Make sure caller has proper permission */ | |
3162 | if (!inode_owner_or_capable(inode)) | |
3163 | return -EACCES; | |
3164 | ||
36098557 | 3165 | if (fa.fsx_xflags & ~F2FS_SUPPORTED_XFLAGS) |
2c1d0305 CY |
3166 | return -EOPNOTSUPP; |
3167 | ||
36098557 EB |
3168 | iflags = f2fs_xflags_to_iflags(fa.fsx_xflags); |
3169 | if (f2fs_mask_flags(inode->i_mode, iflags) != iflags) | |
2c1d0305 CY |
3170 | return -EOPNOTSUPP; |
3171 | ||
3172 | err = mnt_want_write_file(filp); | |
3173 | if (err) | |
3174 | return err; | |
3175 | ||
3176 | inode_lock(inode); | |
6fc93c4e EB |
3177 | |
3178 | f2fs_fill_fsxattr(inode, &old_fa); | |
3179 | err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); | |
c8e92757 WS |
3180 | if (err) |
3181 | goto out; | |
6fc93c4e | 3182 | |
36098557 EB |
3183 | err = f2fs_setflags_common(inode, iflags, |
3184 | f2fs_xflags_to_iflags(F2FS_SUPPORTED_XFLAGS)); | |
2c1d0305 | 3185 | if (err) |
c8e92757 | 3186 | goto out; |
2c1d0305 CY |
3187 | |
3188 | err = f2fs_ioc_setproject(filp, fa.fsx_projid); | |
c8e92757 WS |
3189 | out: |
3190 | inode_unlock(inode); | |
3191 | mnt_drop_write_file(filp); | |
3192 | return err; | |
2c1d0305 | 3193 | } |
e066b83c | 3194 | |
1ad71a27 JK |
3195 | int f2fs_pin_file_control(struct inode *inode, bool inc) |
3196 | { | |
3197 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3198 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3199 | ||
3200 | /* Use i_gc_failures for normal file as a risk signal. */ | |
3201 | if (inc) | |
2ef79ecb CY |
3202 | f2fs_i_gc_failures_write(inode, |
3203 | fi->i_gc_failures[GC_FAILURE_PIN] + 1); | |
1ad71a27 | 3204 | |
2ef79ecb | 3205 | if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) { |
dcbb4c10 JP |
3206 | f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials", |
3207 | __func__, inode->i_ino, | |
3208 | fi->i_gc_failures[GC_FAILURE_PIN]); | |
1ad71a27 JK |
3209 | clear_inode_flag(inode, FI_PIN_FILE); |
3210 | return -EAGAIN; | |
3211 | } | |
3212 | return 0; | |
3213 | } | |
3214 | ||
3215 | static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg) | |
3216 | { | |
3217 | struct inode *inode = file_inode(filp); | |
3218 | __u32 pin; | |
3219 | int ret = 0; | |
3220 | ||
1ad71a27 JK |
3221 | if (get_user(pin, (__u32 __user *)arg)) |
3222 | return -EFAULT; | |
3223 | ||
3224 | if (!S_ISREG(inode->i_mode)) | |
3225 | return -EINVAL; | |
3226 | ||
3227 | if (f2fs_readonly(F2FS_I_SB(inode)->sb)) | |
3228 | return -EROFS; | |
3229 | ||
3230 | ret = mnt_want_write_file(filp); | |
3231 | if (ret) | |
3232 | return ret; | |
3233 | ||
3234 | inode_lock(inode); | |
3235 | ||
4d57b86d | 3236 | if (f2fs_should_update_outplace(inode, NULL)) { |
bb9e3bb8 CY |
3237 | ret = -EINVAL; |
3238 | goto out; | |
3239 | } | |
3240 | ||
1ad71a27 JK |
3241 | if (!pin) { |
3242 | clear_inode_flag(inode, FI_PIN_FILE); | |
30933364 | 3243 | f2fs_i_gc_failures_write(inode, 0); |
1ad71a27 JK |
3244 | goto done; |
3245 | } | |
3246 | ||
3247 | if (f2fs_pin_file_control(inode, false)) { | |
3248 | ret = -EAGAIN; | |
3249 | goto out; | |
3250 | } | |
4c8ff709 | 3251 | |
1ad71a27 JK |
3252 | ret = f2fs_convert_inline_inode(inode); |
3253 | if (ret) | |
3254 | goto out; | |
3255 | ||
78134d03 | 3256 | if (!f2fs_disable_compressed_file(inode)) { |
4c8ff709 CY |
3257 | ret = -EOPNOTSUPP; |
3258 | goto out; | |
3259 | } | |
3260 | ||
1ad71a27 | 3261 | set_inode_flag(inode, FI_PIN_FILE); |
2ef79ecb | 3262 | ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; |
1ad71a27 JK |
3263 | done: |
3264 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | |
3265 | out: | |
3266 | inode_unlock(inode); | |
3267 | mnt_drop_write_file(filp); | |
3268 | return ret; | |
3269 | } | |
3270 | ||
3271 | static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg) | |
3272 | { | |
3273 | struct inode *inode = file_inode(filp); | |
3274 | __u32 pin = 0; | |
3275 | ||
3276 | if (is_inode_flag_set(inode, FI_PIN_FILE)) | |
2ef79ecb | 3277 | pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]; |
1ad71a27 JK |
3278 | return put_user(pin, (u32 __user *)arg); |
3279 | } | |
3280 | ||
c4020b2d CY |
3281 | int f2fs_precache_extents(struct inode *inode) |
3282 | { | |
3283 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
3284 | struct f2fs_map_blocks map; | |
3285 | pgoff_t m_next_extent; | |
3286 | loff_t end; | |
3287 | int err; | |
3288 | ||
3289 | if (is_inode_flag_set(inode, FI_NO_EXTENT)) | |
3290 | return -EOPNOTSUPP; | |
3291 | ||
3292 | map.m_lblk = 0; | |
3293 | map.m_next_pgofs = NULL; | |
3294 | map.m_next_extent = &m_next_extent; | |
3295 | map.m_seg_type = NO_CHECK_TYPE; | |
f4f0b677 | 3296 | map.m_may_create = false; |
c4020b2d CY |
3297 | end = F2FS_I_SB(inode)->max_file_blocks; |
3298 | ||
3299 | while (map.m_lblk < end) { | |
3300 | map.m_len = end - map.m_lblk; | |
3301 | ||
b2532c69 | 3302 | down_write(&fi->i_gc_rwsem[WRITE]); |
c4020b2d | 3303 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE); |
b2532c69 | 3304 | up_write(&fi->i_gc_rwsem[WRITE]); |
c4020b2d CY |
3305 | if (err) |
3306 | return err; | |
3307 | ||
3308 | map.m_lblk = m_next_extent; | |
3309 | } | |
3310 | ||
3311 | return err; | |
3312 | } | |
3313 | ||
3314 | static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg) | |
3315 | { | |
3316 | return f2fs_precache_extents(file_inode(filp)); | |
3317 | } | |
3318 | ||
04f0b2ea QS |
3319 | static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg) |
3320 | { | |
3321 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); | |
3322 | __u64 block_count; | |
04f0b2ea QS |
3323 | |
3324 | if (!capable(CAP_SYS_ADMIN)) | |
3325 | return -EPERM; | |
3326 | ||
3327 | if (f2fs_readonly(sbi->sb)) | |
3328 | return -EROFS; | |
3329 | ||
3330 | if (copy_from_user(&block_count, (void __user *)arg, | |
3331 | sizeof(block_count))) | |
3332 | return -EFAULT; | |
3333 | ||
b4b10061 | 3334 | return f2fs_resize_fs(sbi, block_count); |
04f0b2ea QS |
3335 | } |
3336 | ||
95ae251f EB |
3337 | static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg) |
3338 | { | |
3339 | struct inode *inode = file_inode(filp); | |
3340 | ||
3341 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | |
3342 | ||
3343 | if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) { | |
3344 | f2fs_warn(F2FS_I_SB(inode), | |
3345 | "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem.\n", | |
3346 | inode->i_ino); | |
3347 | return -EOPNOTSUPP; | |
3348 | } | |
3349 | ||
3350 | return fsverity_ioctl_enable(filp, (const void __user *)arg); | |
3351 | } | |
3352 | ||
3353 | static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg) | |
3354 | { | |
3355 | if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp)))) | |
3356 | return -EOPNOTSUPP; | |
3357 | ||
3358 | return fsverity_ioctl_measure(filp, (void __user *)arg); | |
3359 | } | |
3360 | ||
3357af8f | 3361 | static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg) |
4507847c CY |
3362 | { |
3363 | struct inode *inode = file_inode(filp); | |
3364 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3365 | char *vbuf; | |
3366 | int count; | |
3367 | int err = 0; | |
3368 | ||
3369 | vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL); | |
3370 | if (!vbuf) | |
3371 | return -ENOMEM; | |
3372 | ||
3373 | down_read(&sbi->sb_lock); | |
3374 | count = utf16s_to_utf8s(sbi->raw_super->volume_name, | |
3375 | ARRAY_SIZE(sbi->raw_super->volume_name), | |
3376 | UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME); | |
3377 | up_read(&sbi->sb_lock); | |
3378 | ||
3379 | if (copy_to_user((char __user *)arg, vbuf, | |
3380 | min(FSLABEL_MAX, count))) | |
3381 | err = -EFAULT; | |
3382 | ||
c8eb7024 | 3383 | kfree(vbuf); |
4507847c CY |
3384 | return err; |
3385 | } | |
3386 | ||
3357af8f | 3387 | static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg) |
4507847c CY |
3388 | { |
3389 | struct inode *inode = file_inode(filp); | |
3390 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3391 | char *vbuf; | |
3392 | int err = 0; | |
3393 | ||
3394 | if (!capable(CAP_SYS_ADMIN)) | |
3395 | return -EPERM; | |
3396 | ||
3397 | vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX); | |
3398 | if (IS_ERR(vbuf)) | |
3399 | return PTR_ERR(vbuf); | |
3400 | ||
3401 | err = mnt_want_write_file(filp); | |
3402 | if (err) | |
3403 | goto out; | |
3404 | ||
3405 | down_write(&sbi->sb_lock); | |
3406 | ||
3407 | memset(sbi->raw_super->volume_name, 0, | |
3408 | sizeof(sbi->raw_super->volume_name)); | |
3409 | utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN, | |
3410 | sbi->raw_super->volume_name, | |
3411 | ARRAY_SIZE(sbi->raw_super->volume_name)); | |
3412 | ||
3413 | err = f2fs_commit_super(sbi, false); | |
3414 | ||
3415 | up_write(&sbi->sb_lock); | |
3416 | ||
3417 | mnt_drop_write_file(filp); | |
3418 | out: | |
3419 | kfree(vbuf); | |
3420 | return err; | |
3421 | } | |
3422 | ||
439dfb10 CY |
3423 | static int f2fs_get_compress_blocks(struct file *filp, unsigned long arg) |
3424 | { | |
3425 | struct inode *inode = file_inode(filp); | |
3426 | __u64 blocks; | |
3427 | ||
3428 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3429 | return -EOPNOTSUPP; | |
3430 | ||
3431 | if (!f2fs_compressed_file(inode)) | |
3432 | return -EINVAL; | |
3433 | ||
c2759eba | 3434 | blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks); |
439dfb10 CY |
3435 | return put_user(blocks, (u64 __user *)arg); |
3436 | } | |
3437 | ||
ef8d563f CY |
3438 | static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count) |
3439 | { | |
3440 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
3441 | unsigned int released_blocks = 0; | |
3442 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size; | |
3443 | block_t blkaddr; | |
3444 | int i; | |
3445 | ||
3446 | for (i = 0; i < count; i++) { | |
3447 | blkaddr = data_blkaddr(dn->inode, dn->node_page, | |
3448 | dn->ofs_in_node + i); | |
3449 | ||
3450 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3451 | continue; | |
3452 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3453 | DATA_GENERIC_ENHANCE))) | |
3454 | return -EFSCORRUPTED; | |
3455 | } | |
3456 | ||
3457 | while (count) { | |
3458 | int compr_blocks = 0; | |
3459 | ||
3460 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { | |
3461 | blkaddr = f2fs_data_blkaddr(dn); | |
3462 | ||
3463 | if (i == 0) { | |
3464 | if (blkaddr == COMPRESS_ADDR) | |
3465 | continue; | |
3466 | dn->ofs_in_node += cluster_size; | |
3467 | goto next; | |
3468 | } | |
3469 | ||
3470 | if (__is_valid_data_blkaddr(blkaddr)) | |
3471 | compr_blocks++; | |
3472 | ||
3473 | if (blkaddr != NEW_ADDR) | |
3474 | continue; | |
3475 | ||
3476 | dn->data_blkaddr = NULL_ADDR; | |
3477 | f2fs_set_data_blkaddr(dn); | |
3478 | } | |
3479 | ||
3480 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false); | |
3481 | dec_valid_block_count(sbi, dn->inode, | |
3482 | cluster_size - compr_blocks); | |
3483 | ||
3484 | released_blocks += cluster_size - compr_blocks; | |
3485 | next: | |
3486 | count -= cluster_size; | |
3487 | } | |
3488 | ||
3489 | return released_blocks; | |
3490 | } | |
3491 | ||
3492 | static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) | |
3493 | { | |
3494 | struct inode *inode = file_inode(filp); | |
3495 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3496 | pgoff_t page_idx = 0, last_idx; | |
3497 | unsigned int released_blocks = 0; | |
3498 | int ret; | |
3499 | int writecount; | |
3500 | ||
3501 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3502 | return -EOPNOTSUPP; | |
3503 | ||
3504 | if (!f2fs_compressed_file(inode)) | |
3505 | return -EINVAL; | |
3506 | ||
3507 | if (f2fs_readonly(sbi->sb)) | |
3508 | return -EROFS; | |
3509 | ||
3510 | ret = mnt_want_write_file(filp); | |
3511 | if (ret) | |
3512 | return ret; | |
3513 | ||
3514 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
3515 | ||
3516 | inode_lock(inode); | |
3517 | ||
3518 | writecount = atomic_read(&inode->i_writecount); | |
8c8cf26a DJ |
3519 | if ((filp->f_mode & FMODE_WRITE && writecount != 1) || |
3520 | (!(filp->f_mode & FMODE_WRITE) && writecount)) { | |
ef8d563f CY |
3521 | ret = -EBUSY; |
3522 | goto out; | |
3523 | } | |
3524 | ||
3525 | if (IS_IMMUTABLE(inode)) { | |
3526 | ret = -EINVAL; | |
3527 | goto out; | |
3528 | } | |
3529 | ||
3530 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); | |
3531 | if (ret) | |
3532 | goto out; | |
3533 | ||
ef8d563f CY |
3534 | F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL; |
3535 | f2fs_set_inode_flags(inode); | |
3536 | inode->i_ctime = current_time(inode); | |
3537 | f2fs_mark_inode_dirty_sync(inode, true); | |
3538 | ||
c2759eba | 3539 | if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) |
567c4bf5 DJ |
3540 | goto out; |
3541 | ||
ef8d563f CY |
3542 | down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
3543 | down_write(&F2FS_I(inode)->i_mmap_sem); | |
3544 | ||
3545 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
3546 | ||
3547 | while (page_idx < last_idx) { | |
3548 | struct dnode_of_data dn; | |
3549 | pgoff_t end_offset, count; | |
3550 | ||
3551 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3552 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE); | |
3553 | if (ret) { | |
3554 | if (ret == -ENOENT) { | |
3555 | page_idx = f2fs_get_next_page_offset(&dn, | |
3556 | page_idx); | |
3557 | ret = 0; | |
3558 | continue; | |
3559 | } | |
3560 | break; | |
3561 | } | |
3562 | ||
3563 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3564 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); | |
4fec3fc0 | 3565 | count = round_up(count, F2FS_I(inode)->i_cluster_size); |
ef8d563f CY |
3566 | |
3567 | ret = release_compress_blocks(&dn, count); | |
3568 | ||
3569 | f2fs_put_dnode(&dn); | |
3570 | ||
3571 | if (ret < 0) | |
3572 | break; | |
3573 | ||
3574 | page_idx += count; | |
3575 | released_blocks += ret; | |
3576 | } | |
3577 | ||
3578 | up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
3579 | up_write(&F2FS_I(inode)->i_mmap_sem); | |
3580 | out: | |
3581 | inode_unlock(inode); | |
3582 | ||
3583 | mnt_drop_write_file(filp); | |
3584 | ||
3585 | if (ret >= 0) { | |
3586 | ret = put_user(released_blocks, (u64 __user *)arg); | |
c2759eba DJ |
3587 | } else if (released_blocks && |
3588 | atomic_read(&F2FS_I(inode)->i_compr_blocks)) { | |
ef8d563f CY |
3589 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
3590 | f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx " | |
c2759eba | 3591 | "iblocks=%llu, released=%u, compr_blocks=%u, " |
ef8d563f CY |
3592 | "run fsck to fix.", |
3593 | __func__, inode->i_ino, inode->i_blocks, | |
3594 | released_blocks, | |
c2759eba | 3595 | atomic_read(&F2FS_I(inode)->i_compr_blocks)); |
ef8d563f CY |
3596 | } |
3597 | ||
3598 | return ret; | |
3599 | } | |
3600 | ||
c75488fb CY |
3601 | static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) |
3602 | { | |
3603 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
3604 | unsigned int reserved_blocks = 0; | |
3605 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size; | |
3606 | block_t blkaddr; | |
3607 | int i; | |
3608 | ||
3609 | for (i = 0; i < count; i++) { | |
3610 | blkaddr = data_blkaddr(dn->inode, dn->node_page, | |
3611 | dn->ofs_in_node + i); | |
3612 | ||
3613 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3614 | continue; | |
3615 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3616 | DATA_GENERIC_ENHANCE))) | |
3617 | return -EFSCORRUPTED; | |
3618 | } | |
3619 | ||
3620 | while (count) { | |
3621 | int compr_blocks = 0; | |
3622 | blkcnt_t reserved; | |
3623 | int ret; | |
3624 | ||
3625 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { | |
3626 | blkaddr = f2fs_data_blkaddr(dn); | |
3627 | ||
3628 | if (i == 0) { | |
3629 | if (blkaddr == COMPRESS_ADDR) | |
3630 | continue; | |
3631 | dn->ofs_in_node += cluster_size; | |
3632 | goto next; | |
3633 | } | |
3634 | ||
3635 | if (__is_valid_data_blkaddr(blkaddr)) { | |
3636 | compr_blocks++; | |
3637 | continue; | |
3638 | } | |
3639 | ||
3640 | dn->data_blkaddr = NEW_ADDR; | |
3641 | f2fs_set_data_blkaddr(dn); | |
3642 | } | |
3643 | ||
3644 | reserved = cluster_size - compr_blocks; | |
3645 | ret = inc_valid_block_count(sbi, dn->inode, &reserved); | |
3646 | if (ret) | |
3647 | return ret; | |
3648 | ||
3649 | if (reserved != cluster_size - compr_blocks) | |
3650 | return -ENOSPC; | |
3651 | ||
3652 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); | |
3653 | ||
3654 | reserved_blocks += reserved; | |
3655 | next: | |
3656 | count -= cluster_size; | |
3657 | } | |
3658 | ||
3659 | return reserved_blocks; | |
3660 | } | |
3661 | ||
3662 | static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) | |
3663 | { | |
3664 | struct inode *inode = file_inode(filp); | |
3665 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3666 | pgoff_t page_idx = 0, last_idx; | |
3667 | unsigned int reserved_blocks = 0; | |
3668 | int ret; | |
3669 | ||
3670 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3671 | return -EOPNOTSUPP; | |
3672 | ||
3673 | if (!f2fs_compressed_file(inode)) | |
3674 | return -EINVAL; | |
3675 | ||
3676 | if (f2fs_readonly(sbi->sb)) | |
3677 | return -EROFS; | |
3678 | ||
3679 | ret = mnt_want_write_file(filp); | |
3680 | if (ret) | |
3681 | return ret; | |
3682 | ||
c2759eba | 3683 | if (atomic_read(&F2FS_I(inode)->i_compr_blocks)) |
c75488fb CY |
3684 | goto out; |
3685 | ||
3686 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
3687 | ||
3688 | inode_lock(inode); | |
3689 | ||
3690 | if (!IS_IMMUTABLE(inode)) { | |
3691 | ret = -EINVAL; | |
3692 | goto unlock_inode; | |
3693 | } | |
3694 | ||
3695 | down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
3696 | down_write(&F2FS_I(inode)->i_mmap_sem); | |
3697 | ||
3698 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); | |
3699 | ||
3700 | while (page_idx < last_idx) { | |
3701 | struct dnode_of_data dn; | |
3702 | pgoff_t end_offset, count; | |
3703 | ||
3704 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3705 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE); | |
3706 | if (ret) { | |
3707 | if (ret == -ENOENT) { | |
3708 | page_idx = f2fs_get_next_page_offset(&dn, | |
3709 | page_idx); | |
3710 | ret = 0; | |
3711 | continue; | |
3712 | } | |
3713 | break; | |
3714 | } | |
3715 | ||
3716 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3717 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); | |
4fec3fc0 | 3718 | count = round_up(count, F2FS_I(inode)->i_cluster_size); |
c75488fb CY |
3719 | |
3720 | ret = reserve_compress_blocks(&dn, count); | |
3721 | ||
3722 | f2fs_put_dnode(&dn); | |
3723 | ||
3724 | if (ret < 0) | |
3725 | break; | |
3726 | ||
3727 | page_idx += count; | |
3728 | reserved_blocks += ret; | |
3729 | } | |
3730 | ||
3731 | up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
3732 | up_write(&F2FS_I(inode)->i_mmap_sem); | |
3733 | ||
3734 | if (ret >= 0) { | |
3735 | F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL; | |
3736 | f2fs_set_inode_flags(inode); | |
3737 | inode->i_ctime = current_time(inode); | |
3738 | f2fs_mark_inode_dirty_sync(inode, true); | |
3739 | } | |
3740 | unlock_inode: | |
3741 | inode_unlock(inode); | |
3742 | out: | |
3743 | mnt_drop_write_file(filp); | |
3744 | ||
3745 | if (ret >= 0) { | |
3746 | ret = put_user(reserved_blocks, (u64 __user *)arg); | |
c2759eba DJ |
3747 | } else if (reserved_blocks && |
3748 | atomic_read(&F2FS_I(inode)->i_compr_blocks)) { | |
c75488fb CY |
3749 | set_sbi_flag(sbi, SBI_NEED_FSCK); |
3750 | f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx " | |
c2759eba | 3751 | "iblocks=%llu, reserved=%u, compr_blocks=%u, " |
c75488fb CY |
3752 | "run fsck to fix.", |
3753 | __func__, inode->i_ino, inode->i_blocks, | |
3754 | reserved_blocks, | |
c2759eba | 3755 | atomic_read(&F2FS_I(inode)->i_compr_blocks)); |
c75488fb CY |
3756 | } |
3757 | ||
3758 | return ret; | |
3759 | } | |
3760 | ||
9af84648 DJ |
3761 | static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode, |
3762 | pgoff_t off, block_t block, block_t len, u32 flags) | |
3763 | { | |
3764 | struct request_queue *q = bdev_get_queue(bdev); | |
3765 | sector_t sector = SECTOR_FROM_BLOCK(block); | |
3766 | sector_t nr_sects = SECTOR_FROM_BLOCK(len); | |
3767 | int ret = 0; | |
3768 | ||
3769 | if (!q) | |
3770 | return -ENXIO; | |
3771 | ||
3772 | if (flags & F2FS_TRIM_FILE_DISCARD) | |
3773 | ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS, | |
3774 | blk_queue_secure_erase(q) ? | |
3775 | BLKDEV_DISCARD_SECURE : 0); | |
3776 | ||
3777 | if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) { | |
3778 | if (IS_ENCRYPTED(inode)) | |
3779 | ret = fscrypt_zeroout_range(inode, off, block, len); | |
3780 | else | |
3781 | ret = blkdev_issue_zeroout(bdev, sector, nr_sects, | |
3782 | GFP_NOFS, 0); | |
3783 | } | |
3784 | ||
3785 | return ret; | |
3786 | } | |
3787 | ||
3788 | static int f2fs_sec_trim_file(struct file *filp, unsigned long arg) | |
3789 | { | |
3790 | struct inode *inode = file_inode(filp); | |
3791 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3792 | struct address_space *mapping = inode->i_mapping; | |
3793 | struct block_device *prev_bdev = NULL; | |
3794 | struct f2fs_sectrim_range range; | |
3795 | pgoff_t index, pg_end, prev_index = 0; | |
3796 | block_t prev_block = 0, len = 0; | |
3797 | loff_t end_addr; | |
3798 | bool to_end = false; | |
3799 | int ret = 0; | |
3800 | ||
3801 | if (!(filp->f_mode & FMODE_WRITE)) | |
3802 | return -EBADF; | |
3803 | ||
3804 | if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg, | |
3805 | sizeof(range))) | |
3806 | return -EFAULT; | |
3807 | ||
3808 | if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) || | |
3809 | !S_ISREG(inode->i_mode)) | |
3810 | return -EINVAL; | |
3811 | ||
3812 | if (((range.flags & F2FS_TRIM_FILE_DISCARD) && | |
3813 | !f2fs_hw_support_discard(sbi)) || | |
3814 | ((range.flags & F2FS_TRIM_FILE_ZEROOUT) && | |
3815 | IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi))) | |
3816 | return -EOPNOTSUPP; | |
3817 | ||
3818 | file_start_write(filp); | |
3819 | inode_lock(inode); | |
3820 | ||
3821 | if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) || | |
3822 | range.start >= inode->i_size) { | |
3823 | ret = -EINVAL; | |
3824 | goto err; | |
3825 | } | |
3826 | ||
3827 | if (range.len == 0) | |
3828 | goto err; | |
3829 | ||
3830 | if (inode->i_size - range.start > range.len) { | |
3831 | end_addr = range.start + range.len; | |
3832 | } else { | |
3833 | end_addr = range.len == (u64)-1 ? | |
3834 | sbi->sb->s_maxbytes : inode->i_size; | |
3835 | to_end = true; | |
3836 | } | |
3837 | ||
3838 | if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) || | |
3839 | (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) { | |
3840 | ret = -EINVAL; | |
3841 | goto err; | |
3842 | } | |
3843 | ||
3844 | index = F2FS_BYTES_TO_BLK(range.start); | |
3845 | pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE); | |
3846 | ||
3847 | ret = f2fs_convert_inline_inode(inode); | |
3848 | if (ret) | |
3849 | goto err; | |
3850 | ||
3851 | down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
3852 | down_write(&F2FS_I(inode)->i_mmap_sem); | |
3853 | ||
3854 | ret = filemap_write_and_wait_range(mapping, range.start, | |
3855 | to_end ? LLONG_MAX : end_addr - 1); | |
3856 | if (ret) | |
3857 | goto out; | |
3858 | ||
3859 | truncate_inode_pages_range(mapping, range.start, | |
3860 | to_end ? -1 : end_addr - 1); | |
3861 | ||
3862 | while (index < pg_end) { | |
3863 | struct dnode_of_data dn; | |
3864 | pgoff_t end_offset, count; | |
3865 | int i; | |
3866 | ||
3867 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3868 | ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); | |
3869 | if (ret) { | |
3870 | if (ret == -ENOENT) { | |
3871 | index = f2fs_get_next_page_offset(&dn, index); | |
3872 | continue; | |
3873 | } | |
3874 | goto out; | |
3875 | } | |
3876 | ||
3877 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); | |
3878 | count = min(end_offset - dn.ofs_in_node, pg_end - index); | |
3879 | for (i = 0; i < count; i++, index++, dn.ofs_in_node++) { | |
3880 | struct block_device *cur_bdev; | |
3881 | block_t blkaddr = f2fs_data_blkaddr(&dn); | |
3882 | ||
3883 | if (!__is_valid_data_blkaddr(blkaddr)) | |
3884 | continue; | |
3885 | ||
3886 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, | |
3887 | DATA_GENERIC_ENHANCE)) { | |
3888 | ret = -EFSCORRUPTED; | |
3889 | f2fs_put_dnode(&dn); | |
3890 | goto out; | |
3891 | } | |
3892 | ||
3893 | cur_bdev = f2fs_target_device(sbi, blkaddr, NULL); | |
3894 | if (f2fs_is_multi_device(sbi)) { | |
3895 | int di = f2fs_target_device_index(sbi, blkaddr); | |
3896 | ||
3897 | blkaddr -= FDEV(di).start_blk; | |
3898 | } | |
3899 | ||
3900 | if (len) { | |
3901 | if (prev_bdev == cur_bdev && | |
3902 | index == prev_index + len && | |
3903 | blkaddr == prev_block + len) { | |
3904 | len++; | |
3905 | } else { | |
3906 | ret = f2fs_secure_erase(prev_bdev, | |
3907 | inode, prev_index, prev_block, | |
3908 | len, range.flags); | |
3909 | if (ret) { | |
3910 | f2fs_put_dnode(&dn); | |
3911 | goto out; | |
3912 | } | |
3913 | ||
3914 | len = 0; | |
3915 | } | |
3916 | } | |
3917 | ||
3918 | if (!len) { | |
3919 | prev_bdev = cur_bdev; | |
3920 | prev_index = index; | |
3921 | prev_block = blkaddr; | |
3922 | len = 1; | |
3923 | } | |
3924 | } | |
3925 | ||
3926 | f2fs_put_dnode(&dn); | |
3927 | ||
3928 | if (fatal_signal_pending(current)) { | |
3929 | ret = -EINTR; | |
3930 | goto out; | |
3931 | } | |
3932 | cond_resched(); | |
3933 | } | |
3934 | ||
3935 | if (len) | |
3936 | ret = f2fs_secure_erase(prev_bdev, inode, prev_index, | |
3937 | prev_block, len, range.flags); | |
3938 | out: | |
3939 | up_write(&F2FS_I(inode)->i_mmap_sem); | |
3940 | up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); | |
3941 | err: | |
3942 | inode_unlock(inode); | |
3943 | file_end_write(filp); | |
3944 | ||
3945 | return ret; | |
3946 | } | |
3947 | ||
9e2a5f8c DJ |
3948 | static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg) |
3949 | { | |
3950 | struct inode *inode = file_inode(filp); | |
3951 | struct f2fs_comp_option option; | |
3952 | ||
3953 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) | |
3954 | return -EOPNOTSUPP; | |
3955 | ||
3956 | inode_lock_shared(inode); | |
3957 | ||
3958 | if (!f2fs_compressed_file(inode)) { | |
3959 | inode_unlock_shared(inode); | |
3960 | return -ENODATA; | |
3961 | } | |
3962 | ||
3963 | option.algorithm = F2FS_I(inode)->i_compress_algorithm; | |
3964 | option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size; | |
3965 | ||
3966 | inode_unlock_shared(inode); | |
3967 | ||
3968 | if (copy_to_user((struct f2fs_comp_option __user *)arg, &option, | |
3969 | sizeof(option))) | |
3970 | return -EFAULT; | |
3971 | ||
3972 | return 0; | |
3973 | } | |
3974 | ||
52656e6c JK |
3975 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
3976 | { | |
1f227a3e JK |
3977 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) |
3978 | return -EIO; | |
00e09c0b CY |
3979 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp)))) |
3980 | return -ENOSPC; | |
1f227a3e | 3981 | |
52656e6c | 3982 | switch (cmd) { |
3357af8f | 3983 | case FS_IOC_GETFLAGS: |
52656e6c | 3984 | return f2fs_ioc_getflags(filp, arg); |
3357af8f | 3985 | case FS_IOC_SETFLAGS: |
52656e6c | 3986 | return f2fs_ioc_setflags(filp, arg); |
3357af8f | 3987 | case FS_IOC_GETVERSION: |
d49f3e89 | 3988 | return f2fs_ioc_getversion(filp, arg); |
88b88a66 JK |
3989 | case F2FS_IOC_START_ATOMIC_WRITE: |
3990 | return f2fs_ioc_start_atomic_write(filp); | |
3991 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
3992 | return f2fs_ioc_commit_atomic_write(filp); | |
02a1335f JK |
3993 | case F2FS_IOC_START_VOLATILE_WRITE: |
3994 | return f2fs_ioc_start_volatile_write(filp); | |
1e84371f JK |
3995 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: |
3996 | return f2fs_ioc_release_volatile_write(filp); | |
3997 | case F2FS_IOC_ABORT_VOLATILE_WRITE: | |
3998 | return f2fs_ioc_abort_volatile_write(filp); | |
1abff93d JK |
3999 | case F2FS_IOC_SHUTDOWN: |
4000 | return f2fs_ioc_shutdown(filp, arg); | |
52656e6c JK |
4001 | case FITRIM: |
4002 | return f2fs_ioc_fitrim(filp, arg); | |
3357af8f | 4003 | case FS_IOC_SET_ENCRYPTION_POLICY: |
f424f664 | 4004 | return f2fs_ioc_set_encryption_policy(filp, arg); |
3357af8f | 4005 | case FS_IOC_GET_ENCRYPTION_POLICY: |
f424f664 | 4006 | return f2fs_ioc_get_encryption_policy(filp, arg); |
3357af8f | 4007 | case FS_IOC_GET_ENCRYPTION_PWSALT: |
f424f664 | 4008 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); |
8ce589c7 EB |
4009 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
4010 | return f2fs_ioc_get_encryption_policy_ex(filp, arg); | |
4011 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
4012 | return f2fs_ioc_add_encryption_key(filp, arg); | |
4013 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
4014 | return f2fs_ioc_remove_encryption_key(filp, arg); | |
4015 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
4016 | return f2fs_ioc_remove_encryption_key_all_users(filp, arg); | |
4017 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
4018 | return f2fs_ioc_get_encryption_key_status(filp, arg); | |
ee446e1a EB |
4019 | case FS_IOC_GET_ENCRYPTION_NONCE: |
4020 | return f2fs_ioc_get_encryption_nonce(filp, arg); | |
c1c1b583 CY |
4021 | case F2FS_IOC_GARBAGE_COLLECT: |
4022 | return f2fs_ioc_gc(filp, arg); | |
34dc77ad JK |
4023 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
4024 | return f2fs_ioc_gc_range(filp, arg); | |
456b88e4 | 4025 | case F2FS_IOC_WRITE_CHECKPOINT: |
059c0648 | 4026 | return f2fs_ioc_write_checkpoint(filp, arg); |
d323d005 CY |
4027 | case F2FS_IOC_DEFRAGMENT: |
4028 | return f2fs_ioc_defragment(filp, arg); | |
4dd6f977 JK |
4029 | case F2FS_IOC_MOVE_RANGE: |
4030 | return f2fs_ioc_move_range(filp, arg); | |
e066b83c JK |
4031 | case F2FS_IOC_FLUSH_DEVICE: |
4032 | return f2fs_ioc_flush_device(filp, arg); | |
e65ef207 JK |
4033 | case F2FS_IOC_GET_FEATURES: |
4034 | return f2fs_ioc_get_features(filp, arg); | |
3357af8f | 4035 | case FS_IOC_FSGETXATTR: |
2c1d0305 | 4036 | return f2fs_ioc_fsgetxattr(filp, arg); |
3357af8f | 4037 | case FS_IOC_FSSETXATTR: |
2c1d0305 | 4038 | return f2fs_ioc_fssetxattr(filp, arg); |
1ad71a27 JK |
4039 | case F2FS_IOC_GET_PIN_FILE: |
4040 | return f2fs_ioc_get_pin_file(filp, arg); | |
4041 | case F2FS_IOC_SET_PIN_FILE: | |
4042 | return f2fs_ioc_set_pin_file(filp, arg); | |
c4020b2d CY |
4043 | case F2FS_IOC_PRECACHE_EXTENTS: |
4044 | return f2fs_ioc_precache_extents(filp, arg); | |
04f0b2ea QS |
4045 | case F2FS_IOC_RESIZE_FS: |
4046 | return f2fs_ioc_resize_fs(filp, arg); | |
95ae251f EB |
4047 | case FS_IOC_ENABLE_VERITY: |
4048 | return f2fs_ioc_enable_verity(filp, arg); | |
4049 | case FS_IOC_MEASURE_VERITY: | |
4050 | return f2fs_ioc_measure_verity(filp, arg); | |
3357af8f EB |
4051 | case FS_IOC_GETFSLABEL: |
4052 | return f2fs_ioc_getfslabel(filp, arg); | |
4053 | case FS_IOC_SETFSLABEL: | |
4054 | return f2fs_ioc_setfslabel(filp, arg); | |
439dfb10 CY |
4055 | case F2FS_IOC_GET_COMPRESS_BLOCKS: |
4056 | return f2fs_get_compress_blocks(filp, arg); | |
ef8d563f CY |
4057 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: |
4058 | return f2fs_release_compress_blocks(filp, arg); | |
c75488fb CY |
4059 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: |
4060 | return f2fs_reserve_compress_blocks(filp, arg); | |
9af84648 DJ |
4061 | case F2FS_IOC_SEC_TRIM_FILE: |
4062 | return f2fs_sec_trim_file(filp, arg); | |
9e2a5f8c DJ |
4063 | case F2FS_IOC_GET_COMPRESS_OPTION: |
4064 | return f2fs_ioc_get_compress_option(filp, arg); | |
fbfa2cc5 JK |
4065 | default: |
4066 | return -ENOTTY; | |
4067 | } | |
4068 | } | |
4069 | ||
4c8ff709 CY |
4070 | static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
4071 | { | |
4072 | struct file *file = iocb->ki_filp; | |
4073 | struct inode *inode = file_inode(file); | |
8b83ac81 | 4074 | int ret; |
4c8ff709 CY |
4075 | |
4076 | if (!f2fs_is_compress_backend_ready(inode)) | |
4077 | return -EOPNOTSUPP; | |
4078 | ||
8b83ac81 CY |
4079 | ret = generic_file_read_iter(iocb, iter); |
4080 | ||
4081 | if (ret > 0) | |
4082 | f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret); | |
4083 | ||
4084 | return ret; | |
4c8ff709 CY |
4085 | } |
4086 | ||
fcc85a4d JK |
4087 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
4088 | { | |
b439b103 JK |
4089 | struct file *file = iocb->ki_filp; |
4090 | struct inode *inode = file_inode(file); | |
4091 | ssize_t ret; | |
fcc85a4d | 4092 | |
126ce721 CY |
4093 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) { |
4094 | ret = -EIO; | |
4095 | goto out; | |
4096 | } | |
1f227a3e | 4097 | |
7bd29358 CY |
4098 | if (!f2fs_is_compress_backend_ready(inode)) { |
4099 | ret = -EOPNOTSUPP; | |
4100 | goto out; | |
4101 | } | |
4c8ff709 | 4102 | |
cb8434f1 GR |
4103 | if (iocb->ki_flags & IOCB_NOWAIT) { |
4104 | if (!inode_trylock(inode)) { | |
126ce721 CY |
4105 | ret = -EAGAIN; |
4106 | goto out; | |
4107 | } | |
cb8434f1 | 4108 | } else { |
b91050a8 HL |
4109 | inode_lock(inode); |
4110 | } | |
4111 | ||
b439b103 JK |
4112 | ret = generic_write_checks(iocb, from); |
4113 | if (ret > 0) { | |
dc7a10dd JK |
4114 | bool preallocated = false; |
4115 | size_t target_size = 0; | |
dc91de78 | 4116 | int err; |
a7de6086 | 4117 | |
dc91de78 JK |
4118 | if (iov_iter_fault_in_readable(from, iov_iter_count(from))) |
4119 | set_inode_flag(inode, FI_NO_PREALLOC); | |
a7de6086 | 4120 | |
d5d5f0c0 CX |
4121 | if ((iocb->ki_flags & IOCB_NOWAIT)) { |
4122 | if (!f2fs_overwrite_io(inode, iocb->ki_pos, | |
b91050a8 | 4123 | iov_iter_count(from)) || |
d5d5f0c0 CX |
4124 | f2fs_has_inline_data(inode) || |
4125 | f2fs_force_buffered_io(inode, iocb, from)) { | |
4126 | clear_inode_flag(inode, FI_NO_PREALLOC); | |
4127 | inode_unlock(inode); | |
4128 | ret = -EAGAIN; | |
4129 | goto out; | |
4130 | } | |
47501f87 JK |
4131 | goto write; |
4132 | } | |
dc7a10dd | 4133 | |
47501f87 JK |
4134 | if (is_inode_flag_set(inode, FI_NO_PREALLOC)) |
4135 | goto write; | |
4136 | ||
4137 | if (iocb->ki_flags & IOCB_DIRECT) { | |
4138 | /* | |
4139 | * Convert inline data for Direct I/O before entering | |
4140 | * f2fs_direct_IO(). | |
4141 | */ | |
4142 | err = f2fs_convert_inline_inode(inode); | |
4143 | if (err) | |
4144 | goto out_err; | |
4145 | /* | |
4146 | * If force_buffere_io() is true, we have to allocate | |
4147 | * blocks all the time, since f2fs_direct_IO will fall | |
4148 | * back to buffered IO. | |
4149 | */ | |
4150 | if (!f2fs_force_buffered_io(inode, iocb, from) && | |
4151 | allow_outplace_dio(inode, iocb, from)) | |
4152 | goto write; | |
4153 | } | |
4154 | preallocated = true; | |
4155 | target_size = iocb->ki_pos + iov_iter_count(from); | |
4156 | ||
4157 | err = f2fs_preallocate_blocks(iocb, from); | |
4158 | if (err) { | |
4159 | out_err: | |
4160 | clear_inode_flag(inode, FI_NO_PREALLOC); | |
4161 | inode_unlock(inode); | |
4162 | ret = err; | |
4163 | goto out; | |
9dfa1baf | 4164 | } |
47501f87 | 4165 | write: |
a7de6086 | 4166 | ret = __generic_file_write_iter(iocb, from); |
dc91de78 | 4167 | clear_inode_flag(inode, FI_NO_PREALLOC); |
b0af6d49 | 4168 | |
dc7a10dd JK |
4169 | /* if we couldn't write data, we should deallocate blocks. */ |
4170 | if (preallocated && i_size_read(inode) < target_size) | |
4171 | f2fs_truncate(inode); | |
4172 | ||
b0af6d49 CY |
4173 | if (ret > 0) |
4174 | f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret); | |
b439b103 JK |
4175 | } |
4176 | inode_unlock(inode); | |
126ce721 CY |
4177 | out: |
4178 | trace_f2fs_file_write_iter(inode, iocb->ki_pos, | |
4179 | iov_iter_count(from), ret); | |
e2592217 CH |
4180 | if (ret > 0) |
4181 | ret = generic_write_sync(iocb, ret); | |
b439b103 | 4182 | return ret; |
fcc85a4d JK |
4183 | } |
4184 | ||
e9750824 NJ |
4185 | #ifdef CONFIG_COMPAT |
4186 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
4187 | { | |
4188 | switch (cmd) { | |
3357af8f EB |
4189 | case FS_IOC32_GETFLAGS: |
4190 | cmd = FS_IOC_GETFLAGS; | |
e9750824 | 4191 | break; |
3357af8f EB |
4192 | case FS_IOC32_SETFLAGS: |
4193 | cmd = FS_IOC_SETFLAGS; | |
e9750824 | 4194 | break; |
3357af8f EB |
4195 | case FS_IOC32_GETVERSION: |
4196 | cmd = FS_IOC_GETVERSION; | |
04ef4b62 CY |
4197 | break; |
4198 | case F2FS_IOC_START_ATOMIC_WRITE: | |
4199 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
4200 | case F2FS_IOC_START_VOLATILE_WRITE: | |
4201 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: | |
4202 | case F2FS_IOC_ABORT_VOLATILE_WRITE: | |
4203 | case F2FS_IOC_SHUTDOWN: | |
314999dc | 4204 | case FITRIM: |
3357af8f EB |
4205 | case FS_IOC_SET_ENCRYPTION_POLICY: |
4206 | case FS_IOC_GET_ENCRYPTION_PWSALT: | |
4207 | case FS_IOC_GET_ENCRYPTION_POLICY: | |
8ce589c7 EB |
4208 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
4209 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
4210 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
4211 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
4212 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
ee446e1a | 4213 | case FS_IOC_GET_ENCRYPTION_NONCE: |
04ef4b62 | 4214 | case F2FS_IOC_GARBAGE_COLLECT: |
34dc77ad | 4215 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
04ef4b62 CY |
4216 | case F2FS_IOC_WRITE_CHECKPOINT: |
4217 | case F2FS_IOC_DEFRAGMENT: | |
4dd6f977 | 4218 | case F2FS_IOC_MOVE_RANGE: |
e066b83c | 4219 | case F2FS_IOC_FLUSH_DEVICE: |
e65ef207 | 4220 | case F2FS_IOC_GET_FEATURES: |
3357af8f EB |
4221 | case FS_IOC_FSGETXATTR: |
4222 | case FS_IOC_FSSETXATTR: | |
1ad71a27 JK |
4223 | case F2FS_IOC_GET_PIN_FILE: |
4224 | case F2FS_IOC_SET_PIN_FILE: | |
c4020b2d | 4225 | case F2FS_IOC_PRECACHE_EXTENTS: |
04f0b2ea | 4226 | case F2FS_IOC_RESIZE_FS: |
95ae251f EB |
4227 | case FS_IOC_ENABLE_VERITY: |
4228 | case FS_IOC_MEASURE_VERITY: | |
3357af8f EB |
4229 | case FS_IOC_GETFSLABEL: |
4230 | case FS_IOC_SETFSLABEL: | |
439dfb10 | 4231 | case F2FS_IOC_GET_COMPRESS_BLOCKS: |
ef8d563f | 4232 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: |
c75488fb | 4233 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: |
9af84648 | 4234 | case F2FS_IOC_SEC_TRIM_FILE: |
9e2a5f8c | 4235 | case F2FS_IOC_GET_COMPRESS_OPTION: |
4dd6f977 | 4236 | break; |
e9750824 NJ |
4237 | default: |
4238 | return -ENOIOCTLCMD; | |
4239 | } | |
4240 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
4241 | } | |
4242 | #endif | |
4243 | ||
fbfa2cc5 | 4244 | const struct file_operations f2fs_file_operations = { |
267378d4 | 4245 | .llseek = f2fs_llseek, |
4c8ff709 | 4246 | .read_iter = f2fs_file_read_iter, |
fcc85a4d JK |
4247 | .write_iter = f2fs_file_write_iter, |
4248 | .open = f2fs_file_open, | |
12662234 | 4249 | .release = f2fs_release_file, |
fbfa2cc5 | 4250 | .mmap = f2fs_file_mmap, |
7a10f017 | 4251 | .flush = f2fs_file_flush, |
fbfa2cc5 JK |
4252 | .fsync = f2fs_sync_file, |
4253 | .fallocate = f2fs_fallocate, | |
4254 | .unlocked_ioctl = f2fs_ioctl, | |
e9750824 NJ |
4255 | #ifdef CONFIG_COMPAT |
4256 | .compat_ioctl = f2fs_compat_ioctl, | |
4257 | #endif | |
fbfa2cc5 | 4258 | .splice_read = generic_file_splice_read, |
8d020765 | 4259 | .splice_write = iter_file_splice_write, |
fbfa2cc5 | 4260 | }; |