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