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