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