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