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