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