]>
Commit | Line | Data |
---|---|---|
0a8165d7 | 1 | /* |
fbfa2cc5 JK |
2 | * fs/f2fs/file.c |
3 | * | |
4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
5 | * http://www.samsung.com/ | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | #include <linux/fs.h> | |
12 | #include <linux/f2fs_fs.h> | |
13 | #include <linux/stat.h> | |
14 | #include <linux/buffer_head.h> | |
15 | #include <linux/writeback.h> | |
ae51fb31 | 16 | #include <linux/blkdev.h> |
fbfa2cc5 JK |
17 | #include <linux/falloc.h> |
18 | #include <linux/types.h> | |
e9750824 | 19 | #include <linux/compat.h> |
fbfa2cc5 JK |
20 | #include <linux/uaccess.h> |
21 | #include <linux/mount.h> | |
7f7670fe | 22 | #include <linux/pagevec.h> |
dc91de78 | 23 | #include <linux/uio.h> |
8da4b8c4 | 24 | #include <linux/uuid.h> |
4dd6f977 | 25 | #include <linux/file.h> |
fbfa2cc5 JK |
26 | |
27 | #include "f2fs.h" | |
28 | #include "node.h" | |
29 | #include "segment.h" | |
30 | #include "xattr.h" | |
31 | #include "acl.h" | |
c1c1b583 | 32 | #include "gc.h" |
db9f7c1a | 33 | #include "trace.h" |
a2a4a7e4 | 34 | #include <trace/events/f2fs.h> |
fbfa2cc5 | 35 | |
5a3a2d83 QS |
36 | static int f2fs_filemap_fault(struct vm_fault *vmf) |
37 | { | |
38 | struct inode *inode = file_inode(vmf->vma->vm_file); | |
39 | int err; | |
40 | ||
41 | down_read(&F2FS_I(inode)->i_mmap_sem); | |
42 | err = filemap_fault(vmf); | |
43 | up_read(&F2FS_I(inode)->i_mmap_sem); | |
44 | ||
45 | return err; | |
46 | } | |
47 | ||
11bac800 | 48 | static int 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); |
fbfa2cc5 | 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 | ||
fbfa2cc5 | 61 | sb_start_pagefault(inode->i_sb); |
b3d208f9 JK |
62 | |
63 | f2fs_bug_on(sbi, f2fs_has_inline_data(inode)); | |
b067ba1f | 64 | |
fbfa2cc5 | 65 | /* block allocation */ |
e479556b | 66 | f2fs_lock_op(sbi); |
b3d208f9 | 67 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
b600965c | 68 | err = f2fs_reserve_block(&dn, page->index); |
9ba69cf9 JK |
69 | if (err) { |
70 | f2fs_unlock_op(sbi); | |
b600965c | 71 | goto out; |
9ba69cf9 JK |
72 | } |
73 | f2fs_put_dnode(&dn); | |
74 | f2fs_unlock_op(sbi); | |
fbfa2cc5 | 75 | |
2c4db1a6 | 76 | f2fs_balance_fs(sbi, dn.node_changed); |
2a340760 | 77 | |
11bac800 | 78 | file_update_time(vmf->vma->vm_file); |
5a3a2d83 | 79 | down_read(&F2FS_I(inode)->i_mmap_sem); |
fbfa2cc5 | 80 | lock_page(page); |
6bacf52f | 81 | if (unlikely(page->mapping != inode->i_mapping || |
9851e6e1 | 82 | page_offset(page) > i_size_read(inode) || |
6bacf52f | 83 | !PageUptodate(page))) { |
fbfa2cc5 JK |
84 | unlock_page(page); |
85 | err = -EFAULT; | |
5a3a2d83 | 86 | goto out_sem; |
fbfa2cc5 JK |
87 | } |
88 | ||
89 | /* | |
90 | * check to see if the page is mapped already (no holes) | |
91 | */ | |
92 | if (PageMappedToDisk(page)) | |
9851e6e1 | 93 | goto mapped; |
fbfa2cc5 JK |
94 | |
95 | /* page is wholly or partially inside EOF */ | |
09cbfeaf | 96 | if (((loff_t)(page->index + 1) << PAGE_SHIFT) > |
9edcdabf | 97 | i_size_read(inode)) { |
fbfa2cc5 | 98 | unsigned offset; |
09cbfeaf KS |
99 | offset = i_size_read(inode) & ~PAGE_MASK; |
100 | zero_user_segment(page, offset, PAGE_SIZE); | |
fbfa2cc5 JK |
101 | } |
102 | set_page_dirty(page); | |
237c0790 JK |
103 | if (!PageUptodate(page)) |
104 | SetPageUptodate(page); | |
fbfa2cc5 | 105 | |
b0af6d49 CY |
106 | f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE); |
107 | ||
e943a10d | 108 | trace_f2fs_vm_page_mkwrite(page, DATA); |
9851e6e1 NJ |
109 | mapped: |
110 | /* fill the page */ | |
fec1d657 | 111 | f2fs_wait_on_page_writeback(page, DATA, false); |
08b39fbd CY |
112 | |
113 | /* wait for GCed encrypted page writeback */ | |
1958593e | 114 | if (f2fs_encrypted_file(inode)) |
d4c759ee | 115 | f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr); |
08b39fbd | 116 | |
5a3a2d83 QS |
117 | out_sem: |
118 | up_read(&F2FS_I(inode)->i_mmap_sem); | |
fbfa2cc5 JK |
119 | out: |
120 | sb_end_pagefault(inode->i_sb); | |
d0239e1b | 121 | f2fs_update_time(sbi, REQ_TIME); |
1f227a3e | 122 | err: |
fbfa2cc5 JK |
123 | return block_page_mkwrite_return(err); |
124 | } | |
125 | ||
126 | static const struct vm_operations_struct f2fs_file_vm_ops = { | |
5a3a2d83 | 127 | .fault = f2fs_filemap_fault, |
f1820361 | 128 | .map_pages = filemap_map_pages, |
692bb55d | 129 | .page_mkwrite = f2fs_vm_page_mkwrite, |
fbfa2cc5 JK |
130 | }; |
131 | ||
354a3399 JK |
132 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
133 | { | |
134 | struct dentry *dentry; | |
135 | ||
136 | inode = igrab(inode); | |
137 | dentry = d_find_any_alias(inode); | |
138 | iput(inode); | |
139 | if (!dentry) | |
140 | return 0; | |
141 | ||
f0947e5c JK |
142 | *pino = parent_ino(dentry); |
143 | dput(dentry); | |
354a3399 JK |
144 | return 1; |
145 | } | |
146 | ||
a5fd5050 | 147 | static inline enum cp_reason_type need_do_checkpoint(struct inode *inode) |
9d1589ef | 148 | { |
4081363f | 149 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
a5fd5050 | 150 | enum cp_reason_type cp_reason = CP_NO_NEEDED; |
9d1589ef | 151 | |
a5fd5050 CY |
152 | if (!S_ISREG(inode->i_mode)) |
153 | cp_reason = CP_NON_REGULAR; | |
154 | else if (inode->i_nlink != 1) | |
155 | cp_reason = CP_HARDLINK; | |
bbf156f7 | 156 | else if (is_sbi_flag_set(sbi, SBI_NEED_CP)) |
a5fd5050 | 157 | cp_reason = CP_SB_NEED_CP; |
9d1589ef | 158 | else if (file_wrong_pino(inode)) |
a5fd5050 | 159 | cp_reason = CP_WRONG_PINO; |
9d1589ef | 160 | else if (!space_for_roll_forward(sbi)) |
a5fd5050 | 161 | cp_reason = CP_NO_SPC_ROLL; |
9d1589ef | 162 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
a5fd5050 | 163 | cp_reason = CP_NODE_NEED_CP; |
d5053a34 | 164 | else if (test_opt(sbi, FASTBOOT)) |
a5fd5050 | 165 | cp_reason = CP_FASTBOOT_MODE; |
a344b9fd | 166 | else if (sbi->active_logs == 2) |
a5fd5050 | 167 | cp_reason = CP_SPEC_LOG_NUM; |
9d1589ef | 168 | |
a5fd5050 | 169 | return cp_reason; |
9d1589ef CY |
170 | } |
171 | ||
9c7bb702 CL |
172 | static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino) |
173 | { | |
174 | struct page *i = find_get_page(NODE_MAPPING(sbi), ino); | |
175 | bool ret = false; | |
176 | /* But we need to avoid that there are some inode updates */ | |
177 | if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) | |
178 | ret = true; | |
179 | f2fs_put_page(i, 0); | |
180 | return ret; | |
181 | } | |
182 | ||
51455b19 CL |
183 | static void try_to_fix_pino(struct inode *inode) |
184 | { | |
185 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
186 | nid_t pino; | |
187 | ||
188 | down_write(&fi->i_sem); | |
51455b19 CL |
189 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
190 | get_parent_ino(inode, &pino)) { | |
205b9822 | 191 | f2fs_i_pino_write(inode, pino); |
51455b19 | 192 | file_got_pino(inode); |
51455b19 | 193 | } |
ee6d182f | 194 | up_write(&fi->i_sem); |
51455b19 CL |
195 | } |
196 | ||
608514de JK |
197 | static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end, |
198 | int datasync, bool atomic) | |
fbfa2cc5 JK |
199 | { |
200 | struct inode *inode = file->f_mapping->host; | |
4081363f | 201 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
2403c155 | 202 | nid_t ino = inode->i_ino; |
fbfa2cc5 | 203 | int ret = 0; |
a5fd5050 | 204 | enum cp_reason_type cp_reason = 0; |
fbfa2cc5 | 205 | struct writeback_control wbc = { |
c81bf1c8 | 206 | .sync_mode = WB_SYNC_ALL, |
fbfa2cc5 JK |
207 | .nr_to_write = LONG_MAX, |
208 | .for_reclaim = 0, | |
209 | }; | |
210 | ||
6bacf52f | 211 | if (unlikely(f2fs_readonly(inode->i_sb))) |
1fa95b0b NJ |
212 | return 0; |
213 | ||
a2a4a7e4 | 214 | trace_f2fs_sync_file_enter(inode); |
ea1aa12c JK |
215 | |
216 | /* if fdatasync is triggered, let's do in-place-update */ | |
c46a155b | 217 | if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) |
91942321 | 218 | set_inode_flag(inode, FI_NEED_IPU); |
3b49c9a1 | 219 | ret = file_write_and_wait_range(file, start, end); |
91942321 | 220 | clear_inode_flag(inode, FI_NEED_IPU); |
c1ce1b02 | 221 | |
a2a4a7e4 | 222 | if (ret) { |
a5fd5050 | 223 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); |
fbfa2cc5 | 224 | return ret; |
a2a4a7e4 | 225 | } |
fbfa2cc5 | 226 | |
9c7bb702 | 227 | /* if the inode is dirty, let's recover all the time */ |
281518c6 | 228 | if (!f2fs_skip_inode_update(inode, datasync)) { |
2286c020 | 229 | f2fs_write_inode(inode, NULL); |
9c7bb702 CL |
230 | goto go_write; |
231 | } | |
232 | ||
6d99ba41 JK |
233 | /* |
234 | * if there is no written data, don't waste time to write recovery info. | |
235 | */ | |
91942321 | 236 | if (!is_inode_flag_set(inode, FI_APPEND_WRITE) && |
2403c155 | 237 | !exist_written_data(sbi, ino, APPEND_INO)) { |
19c9c466 | 238 | |
9c7bb702 CL |
239 | /* it may call write_inode just prior to fsync */ |
240 | if (need_inode_page_update(sbi, ino)) | |
19c9c466 | 241 | goto go_write; |
19c9c466 | 242 | |
91942321 | 243 | if (is_inode_flag_set(inode, FI_UPDATE_WRITE) || |
2403c155 | 244 | exist_written_data(sbi, ino, UPDATE_INO)) |
6d99ba41 JK |
245 | goto flush_out; |
246 | goto out; | |
247 | } | |
19c9c466 | 248 | go_write: |
e5d2385e JK |
249 | /* |
250 | * Both of fdatasync() and fsync() are able to be recovered from | |
251 | * sudden-power-off. | |
252 | */ | |
91942321 | 253 | down_read(&F2FS_I(inode)->i_sem); |
a5fd5050 | 254 | cp_reason = need_do_checkpoint(inode); |
91942321 | 255 | up_read(&F2FS_I(inode)->i_sem); |
d928bfbf | 256 | |
a5fd5050 | 257 | if (cp_reason) { |
fbfa2cc5 JK |
258 | /* all the dirty node pages should be flushed for POR */ |
259 | ret = f2fs_sync_fs(inode->i_sb, 1); | |
d928bfbf | 260 | |
51455b19 CL |
261 | /* |
262 | * We've secured consistency through sync_fs. Following pino | |
263 | * will be used only for fsynced inodes after checkpoint. | |
264 | */ | |
265 | try_to_fix_pino(inode); | |
91942321 JK |
266 | clear_inode_flag(inode, FI_APPEND_WRITE); |
267 | clear_inode_flag(inode, FI_UPDATE_WRITE); | |
51455b19 CL |
268 | goto out; |
269 | } | |
88bd02c9 | 270 | sync_nodes: |
26de9b11 | 271 | ret = fsync_node_pages(sbi, inode, &wbc, atomic); |
c267ec15 JK |
272 | if (ret) |
273 | goto out; | |
51455b19 | 274 | |
871f599f | 275 | /* if cp_error was enabled, we should avoid infinite loop */ |
6d5a1495 CY |
276 | if (unlikely(f2fs_cp_error(sbi))) { |
277 | ret = -EIO; | |
871f599f | 278 | goto out; |
6d5a1495 | 279 | } |
871f599f | 280 | |
51455b19 | 281 | if (need_inode_block_update(sbi, ino)) { |
7c45729a | 282 | f2fs_mark_inode_dirty_sync(inode, true); |
51455b19 CL |
283 | f2fs_write_inode(inode, NULL); |
284 | goto sync_nodes; | |
fbfa2cc5 | 285 | } |
51455b19 | 286 | |
b6a245eb JK |
287 | /* |
288 | * If it's atomic_write, it's just fine to keep write ordering. So | |
289 | * here we don't need to wait for node write completion, since we use | |
290 | * node chain which serializes node blocks. If one of node writes are | |
291 | * reordered, we can see simply broken chain, resulting in stopping | |
292 | * roll-forward recovery. It means we'll recover all or none node blocks | |
293 | * given fsync mark. | |
294 | */ | |
295 | if (!atomic) { | |
296 | ret = wait_on_node_pages_writeback(sbi, ino); | |
297 | if (ret) | |
298 | goto out; | |
299 | } | |
51455b19 CL |
300 | |
301 | /* once recovery info is written, don't need to tack this */ | |
a49324f1 | 302 | remove_ino_entry(sbi, ino, APPEND_INO); |
91942321 | 303 | clear_inode_flag(inode, FI_APPEND_WRITE); |
51455b19 | 304 | flush_out: |
e7c75ab0 | 305 | if (!atomic) |
39d787be | 306 | ret = f2fs_issue_flush(sbi, inode->i_ino); |
3f06252f CY |
307 | if (!ret) { |
308 | remove_ino_entry(sbi, ino, UPDATE_INO); | |
309 | clear_inode_flag(inode, FI_UPDATE_WRITE); | |
39d787be | 310 | remove_ino_entry(sbi, ino, FLUSH_INO); |
3f06252f | 311 | } |
d0239e1b | 312 | f2fs_update_time(sbi, REQ_TIME); |
fbfa2cc5 | 313 | out: |
a5fd5050 | 314 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret); |
05ca3632 | 315 | f2fs_trace_ios(NULL, 1); |
fbfa2cc5 JK |
316 | return ret; |
317 | } | |
318 | ||
608514de JK |
319 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
320 | { | |
1f227a3e JK |
321 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file))))) |
322 | return -EIO; | |
608514de JK |
323 | return f2fs_do_sync_file(file, start, end, datasync, false); |
324 | } | |
325 | ||
7f7670fe JK |
326 | static pgoff_t __get_first_dirty_index(struct address_space *mapping, |
327 | pgoff_t pgofs, int whence) | |
328 | { | |
8faab642 | 329 | struct page *page; |
7f7670fe JK |
330 | int nr_pages; |
331 | ||
332 | if (whence != SEEK_DATA) | |
333 | return 0; | |
334 | ||
335 | /* find first dirty page index */ | |
8faab642 JK |
336 | nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY, |
337 | 1, &page); | |
338 | if (!nr_pages) | |
339 | return ULONG_MAX; | |
340 | pgofs = page->index; | |
341 | put_page(page); | |
7f7670fe JK |
342 | return pgofs; |
343 | } | |
344 | ||
345 | static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, | |
346 | int whence) | |
347 | { | |
348 | switch (whence) { | |
349 | case SEEK_DATA: | |
350 | if ((blkaddr == NEW_ADDR && dirty == pgofs) || | |
351 | (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) | |
352 | return true; | |
353 | break; | |
354 | case SEEK_HOLE: | |
355 | if (blkaddr == NULL_ADDR) | |
356 | return true; | |
357 | break; | |
358 | } | |
359 | return false; | |
360 | } | |
361 | ||
267378d4 CY |
362 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
363 | { | |
364 | struct inode *inode = file->f_mapping->host; | |
365 | loff_t maxbytes = inode->i_sb->s_maxbytes; | |
366 | struct dnode_of_data dn; | |
7f7670fe JK |
367 | pgoff_t pgofs, end_offset, dirty; |
368 | loff_t data_ofs = offset; | |
369 | loff_t isize; | |
267378d4 CY |
370 | int err = 0; |
371 | ||
5955102c | 372 | inode_lock(inode); |
267378d4 CY |
373 | |
374 | isize = i_size_read(inode); | |
375 | if (offset >= isize) | |
376 | goto fail; | |
377 | ||
378 | /* handle inline data case */ | |
622f28ae | 379 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
267378d4 CY |
380 | if (whence == SEEK_HOLE) |
381 | data_ofs = isize; | |
382 | goto found; | |
383 | } | |
384 | ||
09cbfeaf | 385 | pgofs = (pgoff_t)(offset >> PAGE_SHIFT); |
267378d4 | 386 | |
7f7670fe JK |
387 | dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); |
388 | ||
09cbfeaf | 389 | for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) { |
267378d4 | 390 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
ac6f1999 | 391 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE); |
267378d4 CY |
392 | if (err && err != -ENOENT) { |
393 | goto fail; | |
394 | } else if (err == -ENOENT) { | |
e1c42045 | 395 | /* direct node does not exists */ |
267378d4 | 396 | if (whence == SEEK_DATA) { |
3cf45747 | 397 | pgofs = get_next_page_offset(&dn, pgofs); |
267378d4 CY |
398 | continue; |
399 | } else { | |
400 | goto found; | |
401 | } | |
402 | } | |
403 | ||
81ca7350 | 404 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
267378d4 CY |
405 | |
406 | /* find data/hole in dnode block */ | |
407 | for (; dn.ofs_in_node < end_offset; | |
408 | dn.ofs_in_node++, pgofs++, | |
09cbfeaf | 409 | data_ofs = (loff_t)pgofs << PAGE_SHIFT) { |
267378d4 | 410 | block_t blkaddr; |
7a2af766 CY |
411 | blkaddr = datablock_addr(dn.inode, |
412 | dn.node_page, dn.ofs_in_node); | |
267378d4 | 413 | |
7f7670fe | 414 | if (__found_offset(blkaddr, dirty, pgofs, whence)) { |
267378d4 CY |
415 | f2fs_put_dnode(&dn); |
416 | goto found; | |
417 | } | |
418 | } | |
419 | f2fs_put_dnode(&dn); | |
420 | } | |
421 | ||
422 | if (whence == SEEK_DATA) | |
423 | goto fail; | |
267378d4 | 424 | found: |
fe369bc8 JK |
425 | if (whence == SEEK_HOLE && data_ofs > isize) |
426 | data_ofs = isize; | |
5955102c | 427 | inode_unlock(inode); |
267378d4 CY |
428 | return vfs_setpos(file, data_ofs, maxbytes); |
429 | fail: | |
5955102c | 430 | inode_unlock(inode); |
267378d4 CY |
431 | return -ENXIO; |
432 | } | |
433 | ||
434 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) | |
435 | { | |
436 | struct inode *inode = file->f_mapping->host; | |
437 | loff_t maxbytes = inode->i_sb->s_maxbytes; | |
438 | ||
439 | switch (whence) { | |
440 | case SEEK_SET: | |
441 | case SEEK_CUR: | |
442 | case SEEK_END: | |
443 | return generic_file_llseek_size(file, offset, whence, | |
444 | maxbytes, i_size_read(inode)); | |
445 | case SEEK_DATA: | |
446 | case SEEK_HOLE: | |
0b4c5afd JK |
447 | if (offset < 0) |
448 | return -ENXIO; | |
267378d4 CY |
449 | return f2fs_seek_block(file, offset, whence); |
450 | } | |
451 | ||
452 | return -EINVAL; | |
453 | } | |
454 | ||
fbfa2cc5 JK |
455 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
456 | { | |
b3d208f9 | 457 | struct inode *inode = file_inode(file); |
b9d777b8 | 458 | int err; |
b3d208f9 | 459 | |
1f227a3e JK |
460 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
461 | return -EIO; | |
462 | ||
b3d208f9 | 463 | /* we don't need to use inline_data strictly */ |
b9d777b8 JK |
464 | err = f2fs_convert_inline_inode(inode); |
465 | if (err) | |
466 | return err; | |
b3d208f9 | 467 | |
fbfa2cc5 JK |
468 | file_accessed(file); |
469 | vma->vm_ops = &f2fs_file_vm_ops; | |
470 | return 0; | |
471 | } | |
472 | ||
fcc85a4d JK |
473 | static int f2fs_file_open(struct inode *inode, struct file *filp) |
474 | { | |
2e168c82 | 475 | int err = fscrypt_file_open(inode, filp); |
fcc85a4d | 476 | |
2e168c82 EB |
477 | if (err) |
478 | return err; | |
0abd675e | 479 | return dquot_file_open(inode, filp); |
fcc85a4d JK |
480 | } |
481 | ||
b292dcab | 482 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
fbfa2cc5 | 483 | { |
4081363f | 484 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
fbfa2cc5 | 485 | struct f2fs_node *raw_node; |
19b2c30d | 486 | int nr_free = 0, ofs = dn->ofs_in_node, len = count; |
fbfa2cc5 | 487 | __le32 *addr; |
7a2af766 CY |
488 | int base = 0; |
489 | ||
490 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) | |
491 | base = get_extra_isize(dn->inode); | |
fbfa2cc5 | 492 | |
45590710 | 493 | raw_node = F2FS_NODE(dn->node_page); |
7a2af766 | 494 | addr = blkaddr_in_node(raw_node) + base + ofs; |
fbfa2cc5 | 495 | |
6c311ec6 | 496 | for (; count > 0; count--, addr++, dn->ofs_in_node++) { |
fbfa2cc5 JK |
497 | block_t blkaddr = le32_to_cpu(*addr); |
498 | if (blkaddr == NULL_ADDR) | |
499 | continue; | |
500 | ||
e1509cf2 | 501 | dn->data_blkaddr = NULL_ADDR; |
216a620a | 502 | set_data_blkaddr(dn); |
fbfa2cc5 | 503 | invalidate_blocks(sbi, blkaddr); |
3c6c2beb | 504 | if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page)) |
91942321 | 505 | clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN); |
fbfa2cc5 JK |
506 | nr_free++; |
507 | } | |
19b2c30d | 508 | |
fbfa2cc5 | 509 | if (nr_free) { |
19b2c30d CY |
510 | pgoff_t fofs; |
511 | /* | |
512 | * once we invalidate valid blkaddr in range [ofs, ofs + count], | |
513 | * we will invalidate all blkaddr in the whole range. | |
514 | */ | |
515 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), | |
81ca7350 | 516 | dn->inode) + ofs; |
19b2c30d | 517 | f2fs_update_extent_cache_range(dn, fofs, 0, len); |
d7cc950b | 518 | dec_valid_block_count(sbi, dn->inode, nr_free); |
fbfa2cc5 JK |
519 | } |
520 | dn->ofs_in_node = ofs; | |
51dd6249 | 521 | |
d0239e1b | 522 | f2fs_update_time(sbi, REQ_TIME); |
51dd6249 NJ |
523 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
524 | dn->ofs_in_node, nr_free); | |
fbfa2cc5 JK |
525 | return nr_free; |
526 | } | |
527 | ||
528 | void truncate_data_blocks(struct dnode_of_data *dn) | |
529 | { | |
530 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); | |
531 | } | |
532 | ||
0bfcfcca | 533 | static int truncate_partial_data_page(struct inode *inode, u64 from, |
43f3eae1 | 534 | bool cache_only) |
fbfa2cc5 | 535 | { |
09cbfeaf KS |
536 | unsigned offset = from & (PAGE_SIZE - 1); |
537 | pgoff_t index = from >> PAGE_SHIFT; | |
43f3eae1 | 538 | struct address_space *mapping = inode->i_mapping; |
fbfa2cc5 JK |
539 | struct page *page; |
540 | ||
43f3eae1 | 541 | if (!offset && !cache_only) |
b3d208f9 | 542 | return 0; |
fbfa2cc5 | 543 | |
43f3eae1 | 544 | if (cache_only) { |
34b5d5c2 | 545 | page = find_lock_page(mapping, index); |
43f3eae1 JK |
546 | if (page && PageUptodate(page)) |
547 | goto truncate_out; | |
548 | f2fs_put_page(page, 1); | |
b3d208f9 | 549 | return 0; |
43f3eae1 | 550 | } |
fbfa2cc5 | 551 | |
a56c7c6f | 552 | page = get_lock_data_page(inode, index, true); |
43f3eae1 | 553 | if (IS_ERR(page)) |
a78aaa2c | 554 | return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page); |
43f3eae1 | 555 | truncate_out: |
fec1d657 | 556 | f2fs_wait_on_page_writeback(page, DATA, true); |
09cbfeaf | 557 | zero_user(page, offset, PAGE_SIZE - offset); |
a9bcf9bc JK |
558 | |
559 | /* An encrypted inode should have a key and truncate the last page. */ | |
560 | f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode)); | |
561 | if (!cache_only) | |
0bfcfcca | 562 | set_page_dirty(page); |
fbfa2cc5 | 563 | f2fs_put_page(page, 1); |
b3d208f9 | 564 | return 0; |
fbfa2cc5 JK |
565 | } |
566 | ||
764aa3e9 | 567 | int truncate_blocks(struct inode *inode, u64 from, bool lock) |
fbfa2cc5 | 568 | { |
4081363f | 569 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
fbfa2cc5 JK |
570 | unsigned int blocksize = inode->i_sb->s_blocksize; |
571 | struct dnode_of_data dn; | |
572 | pgoff_t free_from; | |
9ffe0fb5 | 573 | int count = 0, err = 0; |
b3d208f9 | 574 | struct page *ipage; |
0bfcfcca | 575 | bool truncate_page = false; |
fbfa2cc5 | 576 | |
51dd6249 NJ |
577 | trace_f2fs_truncate_blocks_enter(inode, from); |
578 | ||
f7ef9b83 | 579 | free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1); |
fbfa2cc5 | 580 | |
09210c97 CY |
581 | if (free_from >= sbi->max_file_blocks) |
582 | goto free_partial; | |
583 | ||
764aa3e9 JK |
584 | if (lock) |
585 | f2fs_lock_op(sbi); | |
9ffe0fb5 | 586 | |
b3d208f9 JK |
587 | ipage = get_node_page(sbi, inode->i_ino); |
588 | if (IS_ERR(ipage)) { | |
589 | err = PTR_ERR(ipage); | |
590 | goto out; | |
591 | } | |
592 | ||
593 | if (f2fs_has_inline_data(inode)) { | |
bd4667cb | 594 | truncate_inline_inode(inode, ipage, from); |
b3d208f9 | 595 | f2fs_put_page(ipage, 1); |
0bfcfcca | 596 | truncate_page = true; |
b3d208f9 JK |
597 | goto out; |
598 | } | |
599 | ||
600 | set_new_dnode(&dn, inode, ipage, NULL, 0); | |
79344efb | 601 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA); |
fbfa2cc5 JK |
602 | if (err) { |
603 | if (err == -ENOENT) | |
604 | goto free_next; | |
b3d208f9 | 605 | goto out; |
1ce86bf6 JK |
606 | } |
607 | ||
81ca7350 | 608 | count = ADDRS_PER_PAGE(dn.node_page, inode); |
fbfa2cc5 JK |
609 | |
610 | count -= dn.ofs_in_node; | |
9850cf4a | 611 | f2fs_bug_on(sbi, count < 0); |
39936837 | 612 | |
fbfa2cc5 JK |
613 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
614 | truncate_data_blocks_range(&dn, count); | |
615 | free_from += count; | |
616 | } | |
617 | ||
618 | f2fs_put_dnode(&dn); | |
619 | free_next: | |
620 | err = truncate_inode_blocks(inode, free_from); | |
764d2c80 JK |
621 | out: |
622 | if (lock) | |
623 | f2fs_unlock_op(sbi); | |
09210c97 | 624 | free_partial: |
b3d208f9 JK |
625 | /* lastly zero out the first data page */ |
626 | if (!err) | |
0bfcfcca | 627 | err = truncate_partial_data_page(inode, from, truncate_page); |
fbfa2cc5 | 628 | |
51dd6249 | 629 | trace_f2fs_truncate_blocks_exit(inode, err); |
fbfa2cc5 JK |
630 | return err; |
631 | } | |
632 | ||
9a449e9c | 633 | int f2fs_truncate(struct inode *inode) |
fbfa2cc5 | 634 | { |
b0154891 CY |
635 | int err; |
636 | ||
1f227a3e JK |
637 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
638 | return -EIO; | |
639 | ||
fbfa2cc5 JK |
640 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
641 | S_ISLNK(inode->i_mode))) | |
b0154891 | 642 | return 0; |
fbfa2cc5 | 643 | |
51dd6249 NJ |
644 | trace_f2fs_truncate(inode); |
645 | ||
14b44d23 JK |
646 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
647 | if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) { | |
648 | f2fs_show_injection_info(FAULT_TRUNCATE); | |
649 | return -EIO; | |
650 | } | |
651 | #endif | |
92dffd01 | 652 | /* we should check inline_data size */ |
b9d777b8 | 653 | if (!f2fs_may_inline_data(inode)) { |
b0154891 CY |
654 | err = f2fs_convert_inline_inode(inode); |
655 | if (err) | |
656 | return err; | |
92dffd01 JK |
657 | } |
658 | ||
9a449e9c | 659 | err = truncate_blocks(inode, i_size_read(inode), true); |
b0154891 CY |
660 | if (err) |
661 | return err; | |
662 | ||
078cd827 | 663 | inode->i_mtime = inode->i_ctime = current_time(inode); |
7c45729a | 664 | f2fs_mark_inode_dirty_sync(inode, false); |
b0154891 | 665 | return 0; |
fbfa2cc5 JK |
666 | } |
667 | ||
a528d35e | 668 | int f2fs_getattr(const struct path *path, struct kstat *stat, |
1c6d8ee4 | 669 | u32 request_mask, unsigned int query_flags) |
fbfa2cc5 | 670 | { |
a528d35e | 671 | struct inode *inode = d_inode(path->dentry); |
1c6d8ee4 CY |
672 | struct f2fs_inode_info *fi = F2FS_I(inode); |
673 | unsigned int flags; | |
674 | ||
2c1d0305 | 675 | flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL); |
1c6d8ee4 CY |
676 | if (flags & FS_APPEND_FL) |
677 | stat->attributes |= STATX_ATTR_APPEND; | |
678 | if (flags & FS_COMPR_FL) | |
679 | stat->attributes |= STATX_ATTR_COMPRESSED; | |
680 | if (f2fs_encrypted_inode(inode)) | |
681 | stat->attributes |= STATX_ATTR_ENCRYPTED; | |
682 | if (flags & FS_IMMUTABLE_FL) | |
683 | stat->attributes |= STATX_ATTR_IMMUTABLE; | |
684 | if (flags & FS_NODUMP_FL) | |
685 | stat->attributes |= STATX_ATTR_NODUMP; | |
686 | ||
687 | stat->attributes_mask |= (STATX_ATTR_APPEND | | |
688 | STATX_ATTR_COMPRESSED | | |
689 | STATX_ATTR_ENCRYPTED | | |
690 | STATX_ATTR_IMMUTABLE | | |
691 | STATX_ATTR_NODUMP); | |
692 | ||
fbfa2cc5 | 693 | generic_fillattr(inode, stat); |
5b4267d1 JK |
694 | |
695 | /* we need to show initial sectors used for inline_data/dentries */ | |
696 | if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) || | |
697 | f2fs_has_inline_dentry(inode)) | |
698 | stat->blocks += (stat->size + 511) >> 9; | |
699 | ||
fbfa2cc5 JK |
700 | return 0; |
701 | } | |
702 | ||
703 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
704 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) | |
705 | { | |
fbfa2cc5 JK |
706 | unsigned int ia_valid = attr->ia_valid; |
707 | ||
708 | if (ia_valid & ATTR_UID) | |
709 | inode->i_uid = attr->ia_uid; | |
710 | if (ia_valid & ATTR_GID) | |
711 | inode->i_gid = attr->ia_gid; | |
712 | if (ia_valid & ATTR_ATIME) | |
713 | inode->i_atime = timespec_trunc(attr->ia_atime, | |
714 | inode->i_sb->s_time_gran); | |
715 | if (ia_valid & ATTR_MTIME) | |
716 | inode->i_mtime = timespec_trunc(attr->ia_mtime, | |
717 | inode->i_sb->s_time_gran); | |
718 | if (ia_valid & ATTR_CTIME) | |
719 | inode->i_ctime = timespec_trunc(attr->ia_ctime, | |
720 | inode->i_sb->s_time_gran); | |
721 | if (ia_valid & ATTR_MODE) { | |
722 | umode_t mode = attr->ia_mode; | |
723 | ||
724 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
725 | mode &= ~S_ISGID; | |
91942321 | 726 | set_acl_inode(inode, mode); |
fbfa2cc5 JK |
727 | } |
728 | } | |
729 | #else | |
730 | #define __setattr_copy setattr_copy | |
731 | #endif | |
732 | ||
733 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) | |
734 | { | |
2b0143b5 | 735 | struct inode *inode = d_inode(dentry); |
fbfa2cc5 | 736 | int err; |
c0ed4405 | 737 | bool size_changed = false; |
fbfa2cc5 | 738 | |
1f227a3e JK |
739 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
740 | return -EIO; | |
741 | ||
31051c85 | 742 | err = setattr_prepare(dentry, attr); |
fbfa2cc5 JK |
743 | if (err) |
744 | return err; | |
745 | ||
20bb2479 EB |
746 | err = fscrypt_prepare_setattr(dentry, attr); |
747 | if (err) | |
748 | return err; | |
749 | ||
0abd675e CY |
750 | if (is_quota_modification(inode, attr)) { |
751 | err = dquot_initialize(inode); | |
752 | if (err) | |
753 | return err; | |
754 | } | |
755 | if ((attr->ia_valid & ATTR_UID && | |
756 | !uid_eq(attr->ia_uid, inode->i_uid)) || | |
757 | (attr->ia_valid & ATTR_GID && | |
758 | !gid_eq(attr->ia_gid, inode->i_gid))) { | |
759 | err = dquot_transfer(inode, attr); | |
760 | if (err) | |
761 | return err; | |
762 | } | |
763 | ||
09db6a2e | 764 | if (attr->ia_valid & ATTR_SIZE) { |
3c454145 | 765 | if (attr->ia_size <= i_size_read(inode)) { |
5a3a2d83 | 766 | down_write(&F2FS_I(inode)->i_mmap_sem); |
09db6a2e | 767 | truncate_setsize(inode, attr->ia_size); |
9a449e9c | 768 | err = f2fs_truncate(inode); |
5a3a2d83 | 769 | up_write(&F2FS_I(inode)->i_mmap_sem); |
b0154891 CY |
770 | if (err) |
771 | return err; | |
09db6a2e CY |
772 | } else { |
773 | /* | |
3c454145 CY |
774 | * do not trim all blocks after i_size if target size is |
775 | * larger than i_size. | |
09db6a2e | 776 | */ |
5a3a2d83 | 777 | down_write(&F2FS_I(inode)->i_mmap_sem); |
3c454145 | 778 | truncate_setsize(inode, attr->ia_size); |
5a3a2d83 | 779 | up_write(&F2FS_I(inode)->i_mmap_sem); |
0cab80ee CY |
780 | |
781 | /* should convert inline inode here */ | |
b9d777b8 | 782 | if (!f2fs_may_inline_data(inode)) { |
0cab80ee CY |
783 | err = f2fs_convert_inline_inode(inode); |
784 | if (err) | |
785 | return err; | |
786 | } | |
078cd827 | 787 | inode->i_mtime = inode->i_ctime = current_time(inode); |
09db6a2e | 788 | } |
c0ed4405 | 789 | |
a0d00fad CY |
790 | down_write(&F2FS_I(inode)->i_sem); |
791 | F2FS_I(inode)->last_disk_size = i_size_read(inode); | |
792 | up_write(&F2FS_I(inode)->i_sem); | |
793 | ||
c0ed4405 | 794 | size_changed = true; |
fbfa2cc5 JK |
795 | } |
796 | ||
797 | __setattr_copy(inode, attr); | |
798 | ||
799 | if (attr->ia_valid & ATTR_MODE) { | |
a6dda0e6 | 800 | err = posix_acl_chmod(inode, get_inode_mode(inode)); |
91942321 JK |
801 | if (err || is_inode_flag_set(inode, FI_ACL_MODE)) { |
802 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
803 | clear_inode_flag(inode, FI_ACL_MODE); | |
fbfa2cc5 JK |
804 | } |
805 | } | |
806 | ||
c0ed4405 YH |
807 | /* file size may changed here */ |
808 | f2fs_mark_inode_dirty_sync(inode, size_changed); | |
15d04354 JK |
809 | |
810 | /* inode change will produce dirty node pages flushed by checkpoint */ | |
811 | f2fs_balance_fs(F2FS_I_SB(inode), true); | |
812 | ||
fbfa2cc5 JK |
813 | return err; |
814 | } | |
815 | ||
816 | const struct inode_operations f2fs_file_inode_operations = { | |
817 | .getattr = f2fs_getattr, | |
818 | .setattr = f2fs_setattr, | |
819 | .get_acl = f2fs_get_acl, | |
a6dda0e6 | 820 | .set_acl = f2fs_set_acl, |
fbfa2cc5 | 821 | #ifdef CONFIG_F2FS_FS_XATTR |
fbfa2cc5 | 822 | .listxattr = f2fs_listxattr, |
fbfa2cc5 | 823 | #endif |
9ab70134 | 824 | .fiemap = f2fs_fiemap, |
fbfa2cc5 JK |
825 | }; |
826 | ||
6394328a | 827 | static int fill_zero(struct inode *inode, pgoff_t index, |
fbfa2cc5 JK |
828 | loff_t start, loff_t len) |
829 | { | |
4081363f | 830 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
fbfa2cc5 JK |
831 | struct page *page; |
832 | ||
833 | if (!len) | |
6394328a | 834 | return 0; |
fbfa2cc5 | 835 | |
2c4db1a6 | 836 | f2fs_balance_fs(sbi, true); |
bd43df02 | 837 | |
e479556b | 838 | f2fs_lock_op(sbi); |
64aa7ed9 | 839 | page = get_new_data_page(inode, NULL, index, false); |
e479556b | 840 | f2fs_unlock_op(sbi); |
fbfa2cc5 | 841 | |
6394328a CY |
842 | if (IS_ERR(page)) |
843 | return PTR_ERR(page); | |
844 | ||
fec1d657 | 845 | f2fs_wait_on_page_writeback(page, DATA, true); |
6394328a CY |
846 | zero_user(page, start, len); |
847 | set_page_dirty(page); | |
848 | f2fs_put_page(page, 1); | |
849 | return 0; | |
fbfa2cc5 JK |
850 | } |
851 | ||
852 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) | |
853 | { | |
fbfa2cc5 JK |
854 | int err; |
855 | ||
ea58711e | 856 | while (pg_start < pg_end) { |
fbfa2cc5 | 857 | struct dnode_of_data dn; |
ea58711e | 858 | pgoff_t end_offset, count; |
9eaeba70 | 859 | |
fbfa2cc5 | 860 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
ea58711e | 861 | err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE); |
fbfa2cc5 | 862 | if (err) { |
ea58711e | 863 | if (err == -ENOENT) { |
48ab25f4 | 864 | pg_start = get_next_page_offset(&dn, pg_start); |
fbfa2cc5 | 865 | continue; |
ea58711e | 866 | } |
fbfa2cc5 JK |
867 | return err; |
868 | } | |
869 | ||
81ca7350 | 870 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
ea58711e CY |
871 | count = min(end_offset - dn.ofs_in_node, pg_end - pg_start); |
872 | ||
873 | f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset); | |
874 | ||
875 | truncate_data_blocks_range(&dn, count); | |
fbfa2cc5 | 876 | f2fs_put_dnode(&dn); |
ea58711e CY |
877 | |
878 | pg_start += count; | |
fbfa2cc5 JK |
879 | } |
880 | return 0; | |
881 | } | |
882 | ||
a66c7b2f | 883 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len) |
fbfa2cc5 JK |
884 | { |
885 | pgoff_t pg_start, pg_end; | |
886 | loff_t off_start, off_end; | |
b9d777b8 | 887 | int ret; |
fbfa2cc5 | 888 | |
b9d777b8 JK |
889 | ret = f2fs_convert_inline_inode(inode); |
890 | if (ret) | |
891 | return ret; | |
9ffe0fb5 | 892 | |
09cbfeaf KS |
893 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; |
894 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; | |
fbfa2cc5 | 895 | |
09cbfeaf KS |
896 | off_start = offset & (PAGE_SIZE - 1); |
897 | off_end = (offset + len) & (PAGE_SIZE - 1); | |
fbfa2cc5 JK |
898 | |
899 | if (pg_start == pg_end) { | |
6394328a | 900 | ret = fill_zero(inode, pg_start, off_start, |
fbfa2cc5 | 901 | off_end - off_start); |
6394328a CY |
902 | if (ret) |
903 | return ret; | |
fbfa2cc5 | 904 | } else { |
6394328a CY |
905 | if (off_start) { |
906 | ret = fill_zero(inode, pg_start++, off_start, | |
09cbfeaf | 907 | PAGE_SIZE - off_start); |
6394328a CY |
908 | if (ret) |
909 | return ret; | |
910 | } | |
911 | if (off_end) { | |
912 | ret = fill_zero(inode, pg_end, 0, off_end); | |
913 | if (ret) | |
914 | return ret; | |
915 | } | |
fbfa2cc5 JK |
916 | |
917 | if (pg_start < pg_end) { | |
918 | struct address_space *mapping = inode->i_mapping; | |
919 | loff_t blk_start, blk_end; | |
4081363f | 920 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
1127a3d4 | 921 | |
2c4db1a6 | 922 | f2fs_balance_fs(sbi, true); |
fbfa2cc5 | 923 | |
09cbfeaf KS |
924 | blk_start = (loff_t)pg_start << PAGE_SHIFT; |
925 | blk_end = (loff_t)pg_end << PAGE_SHIFT; | |
5a3a2d83 | 926 | down_write(&F2FS_I(inode)->i_mmap_sem); |
fbfa2cc5 JK |
927 | truncate_inode_pages_range(mapping, blk_start, |
928 | blk_end - 1); | |
39936837 | 929 | |
e479556b | 930 | f2fs_lock_op(sbi); |
fbfa2cc5 | 931 | ret = truncate_hole(inode, pg_start, pg_end); |
e479556b | 932 | f2fs_unlock_op(sbi); |
5a3a2d83 | 933 | up_write(&F2FS_I(inode)->i_mmap_sem); |
fbfa2cc5 JK |
934 | } |
935 | } | |
936 | ||
fbfa2cc5 JK |
937 | return ret; |
938 | } | |
939 | ||
0a2aa8fb JK |
940 | static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr, |
941 | int *do_replace, pgoff_t off, pgoff_t len) | |
b4ace337 CY |
942 | { |
943 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
944 | struct dnode_of_data dn; | |
0a2aa8fb | 945 | int ret, done, i; |
ecbaa406 | 946 | |
0a2aa8fb | 947 | next_dnode: |
6e2c64ad | 948 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
0a2aa8fb | 949 | ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA); |
6e2c64ad JK |
950 | if (ret && ret != -ENOENT) { |
951 | return ret; | |
952 | } else if (ret == -ENOENT) { | |
0a2aa8fb JK |
953 | if (dn.max_level == 0) |
954 | return -ENOENT; | |
955 | done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len); | |
956 | blkaddr += done; | |
957 | do_replace += done; | |
958 | goto next; | |
959 | } | |
960 | ||
961 | done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) - | |
962 | dn.ofs_in_node, len); | |
963 | for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) { | |
7a2af766 CY |
964 | *blkaddr = datablock_addr(dn.inode, |
965 | dn.node_page, dn.ofs_in_node); | |
0a2aa8fb JK |
966 | if (!is_checkpointed_data(sbi, *blkaddr)) { |
967 | ||
968 | if (test_opt(sbi, LFS)) { | |
969 | f2fs_put_dnode(&dn); | |
970 | return -ENOTSUPP; | |
971 | } | |
972 | ||
6e2c64ad | 973 | /* do not invalidate this block address */ |
f28b3434 | 974 | f2fs_update_data_blkaddr(&dn, NULL_ADDR); |
0a2aa8fb | 975 | *do_replace = 1; |
b4ace337 | 976 | } |
6e2c64ad | 977 | } |
0a2aa8fb JK |
978 | f2fs_put_dnode(&dn); |
979 | next: | |
980 | len -= done; | |
981 | off += done; | |
982 | if (len) | |
983 | goto next_dnode; | |
984 | return 0; | |
985 | } | |
b4ace337 | 986 | |
0a2aa8fb JK |
987 | static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr, |
988 | int *do_replace, pgoff_t off, int len) | |
989 | { | |
990 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
991 | struct dnode_of_data dn; | |
992 | int ret, i; | |
b4ace337 | 993 | |
0a2aa8fb JK |
994 | for (i = 0; i < len; i++, do_replace++, blkaddr++) { |
995 | if (*do_replace == 0) | |
996 | continue; | |
b4ace337 | 997 | |
0a2aa8fb JK |
998 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
999 | ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA); | |
1000 | if (ret) { | |
1001 | dec_valid_block_count(sbi, inode, 1); | |
1002 | invalidate_blocks(sbi, *blkaddr); | |
1003 | } else { | |
1004 | f2fs_update_data_blkaddr(&dn, *blkaddr); | |
36abef4e | 1005 | } |
0a2aa8fb JK |
1006 | f2fs_put_dnode(&dn); |
1007 | } | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode, | |
1012 | block_t *blkaddr, int *do_replace, | |
1013 | pgoff_t src, pgoff_t dst, pgoff_t len, bool full) | |
1014 | { | |
1015 | struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode); | |
1016 | pgoff_t i = 0; | |
1017 | int ret; | |
36abef4e | 1018 | |
0a2aa8fb JK |
1019 | while (i < len) { |
1020 | if (blkaddr[i] == NULL_ADDR && !full) { | |
1021 | i++; | |
1022 | continue; | |
6e2c64ad | 1023 | } |
b4ace337 | 1024 | |
0a2aa8fb JK |
1025 | if (do_replace[i] || blkaddr[i] == NULL_ADDR) { |
1026 | struct dnode_of_data dn; | |
1027 | struct node_info ni; | |
1028 | size_t new_size; | |
1029 | pgoff_t ilen; | |
b4ace337 | 1030 | |
0a2aa8fb JK |
1031 | set_new_dnode(&dn, dst_inode, NULL, NULL, 0); |
1032 | ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE); | |
1033 | if (ret) | |
1034 | return ret; | |
b4ace337 | 1035 | |
0a2aa8fb JK |
1036 | get_node_info(sbi, dn.nid, &ni); |
1037 | ilen = min((pgoff_t) | |
1038 | ADDRS_PER_PAGE(dn.node_page, dst_inode) - | |
1039 | dn.ofs_in_node, len - i); | |
1040 | do { | |
7a2af766 CY |
1041 | dn.data_blkaddr = datablock_addr(dn.inode, |
1042 | dn.node_page, dn.ofs_in_node); | |
0a2aa8fb JK |
1043 | truncate_data_blocks_range(&dn, 1); |
1044 | ||
1045 | if (do_replace[i]) { | |
1046 | f2fs_i_blocks_write(src_inode, | |
0abd675e | 1047 | 1, false, false); |
0a2aa8fb | 1048 | f2fs_i_blocks_write(dst_inode, |
0abd675e | 1049 | 1, true, false); |
0a2aa8fb JK |
1050 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr, |
1051 | blkaddr[i], ni.version, true, false); | |
1052 | ||
1053 | do_replace[i] = 0; | |
1054 | } | |
1055 | dn.ofs_in_node++; | |
1056 | i++; | |
1057 | new_size = (dst + i) << PAGE_SHIFT; | |
1058 | if (dst_inode->i_size < new_size) | |
1059 | f2fs_i_size_write(dst_inode, new_size); | |
e87f7329 | 1060 | } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR)); |
6e2c64ad | 1061 | |
0a2aa8fb JK |
1062 | f2fs_put_dnode(&dn); |
1063 | } else { | |
1064 | struct page *psrc, *pdst; | |
1065 | ||
1066 | psrc = get_lock_data_page(src_inode, src + i, true); | |
1067 | if (IS_ERR(psrc)) | |
1068 | return PTR_ERR(psrc); | |
1069 | pdst = get_new_data_page(dst_inode, NULL, dst + i, | |
1070 | true); | |
1071 | if (IS_ERR(pdst)) { | |
1072 | f2fs_put_page(psrc, 1); | |
1073 | return PTR_ERR(pdst); | |
1074 | } | |
1075 | f2fs_copy_page(psrc, pdst); | |
1076 | set_page_dirty(pdst); | |
1077 | f2fs_put_page(pdst, 1); | |
6e2c64ad | 1078 | f2fs_put_page(psrc, 1); |
b4ace337 | 1079 | |
0a2aa8fb JK |
1080 | ret = truncate_hole(src_inode, src + i, src + i + 1); |
1081 | if (ret) | |
1082 | return ret; | |
1083 | i++; | |
1084 | } | |
6e2c64ad JK |
1085 | } |
1086 | return 0; | |
0a2aa8fb | 1087 | } |
b4ace337 | 1088 | |
0a2aa8fb JK |
1089 | static int __exchange_data_block(struct inode *src_inode, |
1090 | struct inode *dst_inode, pgoff_t src, pgoff_t dst, | |
363cad7f | 1091 | pgoff_t len, bool full) |
0a2aa8fb JK |
1092 | { |
1093 | block_t *src_blkaddr; | |
1094 | int *do_replace; | |
363cad7f | 1095 | pgoff_t olen; |
0a2aa8fb JK |
1096 | int ret; |
1097 | ||
363cad7f JK |
1098 | while (len) { |
1099 | olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len); | |
0a2aa8fb | 1100 | |
628b3d14 CY |
1101 | src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode), |
1102 | sizeof(block_t) * olen, GFP_KERNEL); | |
363cad7f JK |
1103 | if (!src_blkaddr) |
1104 | return -ENOMEM; | |
0a2aa8fb | 1105 | |
628b3d14 CY |
1106 | do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode), |
1107 | sizeof(int) * olen, GFP_KERNEL); | |
363cad7f JK |
1108 | if (!do_replace) { |
1109 | kvfree(src_blkaddr); | |
1110 | return -ENOMEM; | |
1111 | } | |
0a2aa8fb | 1112 | |
363cad7f JK |
1113 | ret = __read_out_blkaddrs(src_inode, src_blkaddr, |
1114 | do_replace, src, olen); | |
1115 | if (ret) | |
1116 | goto roll_back; | |
0a2aa8fb | 1117 | |
363cad7f JK |
1118 | ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr, |
1119 | do_replace, src, dst, olen, full); | |
1120 | if (ret) | |
1121 | goto roll_back; | |
1122 | ||
1123 | src += olen; | |
1124 | dst += olen; | |
1125 | len -= olen; | |
1126 | ||
1127 | kvfree(src_blkaddr); | |
1128 | kvfree(do_replace); | |
1129 | } | |
0a2aa8fb JK |
1130 | return 0; |
1131 | ||
1132 | roll_back: | |
1133 | __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len); | |
1134 | kvfree(src_blkaddr); | |
1135 | kvfree(do_replace); | |
6e2c64ad JK |
1136 | return ret; |
1137 | } | |
b4ace337 | 1138 | |
6e2c64ad JK |
1139 | static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end) |
1140 | { | |
1141 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1142 | pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; | |
0a2aa8fb | 1143 | int ret; |
6e2c64ad | 1144 | |
0a2aa8fb JK |
1145 | f2fs_balance_fs(sbi, true); |
1146 | f2fs_lock_op(sbi); | |
5f281fab JK |
1147 | |
1148 | f2fs_drop_extent_tree(inode); | |
1149 | ||
0a2aa8fb JK |
1150 | ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true); |
1151 | f2fs_unlock_op(sbi); | |
b4ace337 CY |
1152 | return ret; |
1153 | } | |
1154 | ||
1155 | static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len) | |
1156 | { | |
1157 | pgoff_t pg_start, pg_end; | |
1158 | loff_t new_size; | |
1159 | int ret; | |
1160 | ||
b4ace337 CY |
1161 | if (offset + len >= i_size_read(inode)) |
1162 | return -EINVAL; | |
1163 | ||
1164 | /* collapse range should be aligned to block size of f2fs. */ | |
1165 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) | |
1166 | return -EINVAL; | |
1167 | ||
b9d777b8 JK |
1168 | ret = f2fs_convert_inline_inode(inode); |
1169 | if (ret) | |
1170 | return ret; | |
97a7b2c2 | 1171 | |
09cbfeaf KS |
1172 | pg_start = offset >> PAGE_SHIFT; |
1173 | pg_end = (offset + len) >> PAGE_SHIFT; | |
b4ace337 | 1174 | |
21020812 CY |
1175 | /* avoid gc operation during block exchange */ |
1176 | down_write(&F2FS_I(inode)->dio_rwsem[WRITE]); | |
1177 | ||
5a3a2d83 | 1178 | down_write(&F2FS_I(inode)->i_mmap_sem); |
b4ace337 CY |
1179 | /* write out all dirty pages from offset */ |
1180 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); | |
1181 | if (ret) | |
21020812 | 1182 | goto out_unlock; |
bb06664a | 1183 | |
b4ace337 CY |
1184 | truncate_pagecache(inode, offset); |
1185 | ||
1186 | ret = f2fs_do_collapse(inode, pg_start, pg_end); | |
1187 | if (ret) | |
bb06664a | 1188 | goto out_unlock; |
b4ace337 | 1189 | |
6e2c64ad JK |
1190 | /* write out all moved pages, if possible */ |
1191 | filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); | |
1192 | truncate_pagecache(inode, offset); | |
1193 | ||
b4ace337 | 1194 | new_size = i_size_read(inode) - len; |
6e2c64ad | 1195 | truncate_pagecache(inode, new_size); |
b4ace337 CY |
1196 | |
1197 | ret = truncate_blocks(inode, new_size, true); | |
1198 | if (!ret) | |
fc9581c8 | 1199 | f2fs_i_size_write(inode, new_size); |
bb06664a | 1200 | out_unlock: |
5a3a2d83 | 1201 | up_write(&F2FS_I(inode)->i_mmap_sem); |
21020812 | 1202 | up_write(&F2FS_I(inode)->dio_rwsem[WRITE]); |
b4ace337 CY |
1203 | return ret; |
1204 | } | |
1205 | ||
6e961949 CY |
1206 | static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, |
1207 | pgoff_t end) | |
1208 | { | |
1209 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
1210 | pgoff_t index = start; | |
1211 | unsigned int ofs_in_node = dn->ofs_in_node; | |
1212 | blkcnt_t count = 0; | |
1213 | int ret; | |
1214 | ||
1215 | for (; index < end; index++, dn->ofs_in_node++) { | |
7a2af766 CY |
1216 | if (datablock_addr(dn->inode, dn->node_page, |
1217 | dn->ofs_in_node) == NULL_ADDR) | |
6e961949 CY |
1218 | count++; |
1219 | } | |
1220 | ||
1221 | dn->ofs_in_node = ofs_in_node; | |
1222 | ret = reserve_new_blocks(dn, count); | |
1223 | if (ret) | |
1224 | return ret; | |
1225 | ||
1226 | dn->ofs_in_node = ofs_in_node; | |
1227 | for (index = start; index < end; index++, dn->ofs_in_node++) { | |
7a2af766 CY |
1228 | dn->data_blkaddr = datablock_addr(dn->inode, |
1229 | dn->node_page, dn->ofs_in_node); | |
6e961949 CY |
1230 | /* |
1231 | * reserve_new_blocks will not guarantee entire block | |
1232 | * allocation. | |
1233 | */ | |
1234 | if (dn->data_blkaddr == NULL_ADDR) { | |
1235 | ret = -ENOSPC; | |
1236 | break; | |
1237 | } | |
1238 | if (dn->data_blkaddr != NEW_ADDR) { | |
1239 | invalidate_blocks(sbi, dn->data_blkaddr); | |
1240 | dn->data_blkaddr = NEW_ADDR; | |
1241 | set_data_blkaddr(dn); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | f2fs_update_extent_cache_range(dn, start, 0, index - start); | |
1246 | ||
1247 | return ret; | |
1248 | } | |
1249 | ||
75cd4e09 CY |
1250 | static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len, |
1251 | int mode) | |
1252 | { | |
1253 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1254 | struct address_space *mapping = inode->i_mapping; | |
1255 | pgoff_t index, pg_start, pg_end; | |
1256 | loff_t new_size = i_size_read(inode); | |
1257 | loff_t off_start, off_end; | |
1258 | int ret = 0; | |
1259 | ||
75cd4e09 CY |
1260 | ret = inode_newsize_ok(inode, (len + offset)); |
1261 | if (ret) | |
1262 | return ret; | |
1263 | ||
b9d777b8 JK |
1264 | ret = f2fs_convert_inline_inode(inode); |
1265 | if (ret) | |
1266 | return ret; | |
75cd4e09 | 1267 | |
5a3a2d83 | 1268 | down_write(&F2FS_I(inode)->i_mmap_sem); |
75cd4e09 CY |
1269 | ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1); |
1270 | if (ret) | |
5a3a2d83 | 1271 | goto out_sem; |
75cd4e09 CY |
1272 | |
1273 | truncate_pagecache_range(inode, offset, offset + len - 1); | |
1274 | ||
09cbfeaf KS |
1275 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT; |
1276 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT; | |
75cd4e09 | 1277 | |
09cbfeaf KS |
1278 | off_start = offset & (PAGE_SIZE - 1); |
1279 | off_end = (offset + len) & (PAGE_SIZE - 1); | |
75cd4e09 CY |
1280 | |
1281 | if (pg_start == pg_end) { | |
6394328a CY |
1282 | ret = fill_zero(inode, pg_start, off_start, |
1283 | off_end - off_start); | |
1284 | if (ret) | |
5a3a2d83 | 1285 | goto out_sem; |
6394328a | 1286 | |
75cd4e09 CY |
1287 | new_size = max_t(loff_t, new_size, offset + len); |
1288 | } else { | |
1289 | if (off_start) { | |
6394328a | 1290 | ret = fill_zero(inode, pg_start++, off_start, |
09cbfeaf | 1291 | PAGE_SIZE - off_start); |
6394328a | 1292 | if (ret) |
5a3a2d83 | 1293 | goto out_sem; |
6394328a | 1294 | |
75cd4e09 | 1295 | new_size = max_t(loff_t, new_size, |
09cbfeaf | 1296 | (loff_t)pg_start << PAGE_SHIFT); |
75cd4e09 CY |
1297 | } |
1298 | ||
6e961949 | 1299 | for (index = pg_start; index < pg_end;) { |
75cd4e09 | 1300 | struct dnode_of_data dn; |
6e961949 CY |
1301 | unsigned int end_offset; |
1302 | pgoff_t end; | |
75cd4e09 CY |
1303 | |
1304 | f2fs_lock_op(sbi); | |
1305 | ||
6e961949 CY |
1306 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
1307 | ret = get_dnode_of_data(&dn, index, ALLOC_NODE); | |
75cd4e09 CY |
1308 | if (ret) { |
1309 | f2fs_unlock_op(sbi); | |
1310 | goto out; | |
1311 | } | |
1312 | ||
6e961949 CY |
1313 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
1314 | end = min(pg_end, end_offset - dn.ofs_in_node + index); | |
1315 | ||
1316 | ret = f2fs_do_zero_range(&dn, index, end); | |
75cd4e09 CY |
1317 | f2fs_put_dnode(&dn); |
1318 | f2fs_unlock_op(sbi); | |
9434fcde CY |
1319 | |
1320 | f2fs_balance_fs(sbi, dn.node_changed); | |
1321 | ||
6e961949 CY |
1322 | if (ret) |
1323 | goto out; | |
75cd4e09 | 1324 | |
6e961949 | 1325 | index = end; |
75cd4e09 | 1326 | new_size = max_t(loff_t, new_size, |
6e961949 | 1327 | (loff_t)index << PAGE_SHIFT); |
75cd4e09 CY |
1328 | } |
1329 | ||
1330 | if (off_end) { | |
6394328a CY |
1331 | ret = fill_zero(inode, pg_end, 0, off_end); |
1332 | if (ret) | |
1333 | goto out; | |
1334 | ||
75cd4e09 CY |
1335 | new_size = max_t(loff_t, new_size, offset + len); |
1336 | } | |
1337 | } | |
1338 | ||
1339 | out: | |
ee6d182f | 1340 | if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) |
fc9581c8 | 1341 | f2fs_i_size_write(inode, new_size); |
5a3a2d83 QS |
1342 | out_sem: |
1343 | up_write(&F2FS_I(inode)->i_mmap_sem); | |
75cd4e09 CY |
1344 | |
1345 | return ret; | |
1346 | } | |
1347 | ||
f62185d0 CY |
1348 | static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) |
1349 | { | |
1350 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
0a2aa8fb | 1351 | pgoff_t nr, pg_start, pg_end, delta, idx; |
f62185d0 | 1352 | loff_t new_size; |
6e2c64ad | 1353 | int ret = 0; |
f62185d0 | 1354 | |
f62185d0 | 1355 | new_size = i_size_read(inode) + len; |
46e82fb1 KM |
1356 | ret = inode_newsize_ok(inode, new_size); |
1357 | if (ret) | |
1358 | return ret; | |
f62185d0 CY |
1359 | |
1360 | if (offset >= i_size_read(inode)) | |
1361 | return -EINVAL; | |
1362 | ||
1363 | /* insert range should be aligned to block size of f2fs. */ | |
1364 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1)) | |
1365 | return -EINVAL; | |
1366 | ||
b9d777b8 JK |
1367 | ret = f2fs_convert_inline_inode(inode); |
1368 | if (ret) | |
1369 | return ret; | |
97a7b2c2 | 1370 | |
2c4db1a6 | 1371 | f2fs_balance_fs(sbi, true); |
2a340760 | 1372 | |
21020812 CY |
1373 | /* avoid gc operation during block exchange */ |
1374 | down_write(&F2FS_I(inode)->dio_rwsem[WRITE]); | |
1375 | ||
5a3a2d83 | 1376 | down_write(&F2FS_I(inode)->i_mmap_sem); |
f62185d0 CY |
1377 | ret = truncate_blocks(inode, i_size_read(inode), true); |
1378 | if (ret) | |
5a3a2d83 | 1379 | goto out; |
f62185d0 CY |
1380 | |
1381 | /* write out all dirty pages from offset */ | |
1382 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); | |
1383 | if (ret) | |
5a3a2d83 | 1384 | goto out; |
f62185d0 CY |
1385 | |
1386 | truncate_pagecache(inode, offset); | |
1387 | ||
09cbfeaf KS |
1388 | pg_start = offset >> PAGE_SHIFT; |
1389 | pg_end = (offset + len) >> PAGE_SHIFT; | |
f62185d0 | 1390 | delta = pg_end - pg_start; |
0a2aa8fb JK |
1391 | idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE; |
1392 | ||
1393 | while (!ret && idx > pg_start) { | |
1394 | nr = idx - pg_start; | |
1395 | if (nr > delta) | |
1396 | nr = delta; | |
1397 | idx -= nr; | |
f62185d0 | 1398 | |
f62185d0 | 1399 | f2fs_lock_op(sbi); |
5f281fab JK |
1400 | f2fs_drop_extent_tree(inode); |
1401 | ||
0a2aa8fb JK |
1402 | ret = __exchange_data_block(inode, inode, idx, |
1403 | idx + delta, nr, false); | |
f62185d0 CY |
1404 | f2fs_unlock_op(sbi); |
1405 | } | |
1406 | ||
6e2c64ad JK |
1407 | /* write out all moved pages, if possible */ |
1408 | filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX); | |
1409 | truncate_pagecache(inode, offset); | |
1410 | ||
1411 | if (!ret) | |
fc9581c8 | 1412 | f2fs_i_size_write(inode, new_size); |
5a3a2d83 QS |
1413 | out: |
1414 | up_write(&F2FS_I(inode)->i_mmap_sem); | |
21020812 | 1415 | up_write(&F2FS_I(inode)->dio_rwsem[WRITE]); |
f62185d0 CY |
1416 | return ret; |
1417 | } | |
1418 | ||
fbfa2cc5 JK |
1419 | static int expand_inode_data(struct inode *inode, loff_t offset, |
1420 | loff_t len, int mode) | |
1421 | { | |
4081363f | 1422 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
d5097be5 HL |
1423 | struct f2fs_map_blocks map = { .m_next_pgofs = NULL, |
1424 | .m_seg_type = NO_CHECK_TYPE }; | |
e12dd7bd | 1425 | pgoff_t pg_end; |
fbfa2cc5 | 1426 | loff_t new_size = i_size_read(inode); |
e12dd7bd | 1427 | loff_t off_end; |
a7de6086 | 1428 | int err; |
fbfa2cc5 | 1429 | |
a7de6086 JK |
1430 | err = inode_newsize_ok(inode, (len + offset)); |
1431 | if (err) | |
1432 | return err; | |
fbfa2cc5 | 1433 | |
a7de6086 JK |
1434 | err = f2fs_convert_inline_inode(inode); |
1435 | if (err) | |
1436 | return err; | |
9e09fc85 | 1437 | |
2c4db1a6 | 1438 | f2fs_balance_fs(sbi, true); |
2a340760 | 1439 | |
e12dd7bd | 1440 | pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT; |
09cbfeaf | 1441 | off_end = (offset + len) & (PAGE_SIZE - 1); |
fbfa2cc5 | 1442 | |
e12dd7bd JK |
1443 | map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT; |
1444 | map.m_len = pg_end - map.m_lblk; | |
1445 | if (off_end) | |
1446 | map.m_len++; | |
ead43275 | 1447 | |
a7de6086 JK |
1448 | err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); |
1449 | if (err) { | |
e12dd7bd | 1450 | pgoff_t last_off; |
fbfa2cc5 | 1451 | |
e12dd7bd | 1452 | if (!map.m_len) |
a7de6086 | 1453 | return err; |
98397ff3 | 1454 | |
e12dd7bd JK |
1455 | last_off = map.m_lblk + map.m_len - 1; |
1456 | ||
1457 | /* update new size to the failed position */ | |
1458 | new_size = (last_off == pg_end) ? offset + len: | |
1459 | (loff_t)(last_off + 1) << PAGE_SHIFT; | |
1460 | } else { | |
1461 | new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end; | |
fbfa2cc5 JK |
1462 | } |
1463 | ||
e8ed90a6 CY |
1464 | if (new_size > i_size_read(inode)) { |
1465 | if (mode & FALLOC_FL_KEEP_SIZE) | |
1466 | file_set_keep_isize(inode); | |
1467 | else | |
1468 | f2fs_i_size_write(inode, new_size); | |
1469 | } | |
fbfa2cc5 | 1470 | |
a7de6086 | 1471 | return err; |
fbfa2cc5 JK |
1472 | } |
1473 | ||
1474 | static long f2fs_fallocate(struct file *file, int mode, | |
1475 | loff_t offset, loff_t len) | |
1476 | { | |
6131ffaa | 1477 | struct inode *inode = file_inode(file); |
587c0a42 | 1478 | long ret = 0; |
fbfa2cc5 | 1479 | |
1f227a3e JK |
1480 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
1481 | return -EIO; | |
1482 | ||
c998012b CY |
1483 | /* f2fs only support ->fallocate for regular file */ |
1484 | if (!S_ISREG(inode->i_mode)) | |
1485 | return -EINVAL; | |
1486 | ||
f62185d0 CY |
1487 | if (f2fs_encrypted_inode(inode) && |
1488 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) | |
fcc85a4d JK |
1489 | return -EOPNOTSUPP; |
1490 | ||
b4ace337 | 1491 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
f62185d0 CY |
1492 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
1493 | FALLOC_FL_INSERT_RANGE)) | |
fbfa2cc5 JK |
1494 | return -EOPNOTSUPP; |
1495 | ||
5955102c | 1496 | inode_lock(inode); |
3375f696 | 1497 | |
587c0a42 TY |
1498 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
1499 | if (offset >= inode->i_size) | |
1500 | goto out; | |
1501 | ||
a66c7b2f | 1502 | ret = punch_hole(inode, offset, len); |
b4ace337 CY |
1503 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
1504 | ret = f2fs_collapse_range(inode, offset, len); | |
75cd4e09 CY |
1505 | } else if (mode & FALLOC_FL_ZERO_RANGE) { |
1506 | ret = f2fs_zero_range(inode, offset, len, mode); | |
f62185d0 CY |
1507 | } else if (mode & FALLOC_FL_INSERT_RANGE) { |
1508 | ret = f2fs_insert_range(inode, offset, len); | |
b4ace337 | 1509 | } else { |
fbfa2cc5 | 1510 | ret = expand_inode_data(inode, offset, len, mode); |
b4ace337 | 1511 | } |
fbfa2cc5 | 1512 | |
3af60a49 | 1513 | if (!ret) { |
078cd827 | 1514 | inode->i_mtime = inode->i_ctime = current_time(inode); |
7c45729a | 1515 | f2fs_mark_inode_dirty_sync(inode, false); |
d0239e1b | 1516 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
3af60a49 | 1517 | } |
3375f696 | 1518 | |
587c0a42 | 1519 | out: |
5955102c | 1520 | inode_unlock(inode); |
3375f696 | 1521 | |
c01e2853 | 1522 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
fbfa2cc5 JK |
1523 | return ret; |
1524 | } | |
1525 | ||
1e84371f JK |
1526 | static int f2fs_release_file(struct inode *inode, struct file *filp) |
1527 | { | |
de5307e4 JK |
1528 | /* |
1529 | * f2fs_relase_file is called at every close calls. So we should | |
1530 | * not drop any inmemory pages by close called by other process. | |
1531 | */ | |
1532 | if (!(filp->f_mode & FMODE_WRITE) || | |
1533 | atomic_read(&inode->i_writecount) != 1) | |
1534 | return 0; | |
1535 | ||
1e84371f JK |
1536 | /* some remained atomic pages should discarded */ |
1537 | if (f2fs_is_atomic_file(inode)) | |
29b96b54 | 1538 | drop_inmem_pages(inode); |
1e84371f | 1539 | if (f2fs_is_volatile_file(inode)) { |
91942321 | 1540 | clear_inode_flag(inode, FI_VOLATILE_FILE); |
648d50ba | 1541 | stat_dec_volatile_write(inode); |
91942321 | 1542 | set_inode_flag(inode, FI_DROP_CACHE); |
1e84371f | 1543 | filemap_fdatawrite(inode->i_mapping); |
91942321 | 1544 | clear_inode_flag(inode, FI_DROP_CACHE); |
1e84371f JK |
1545 | } |
1546 | return 0; | |
1547 | } | |
1548 | ||
7a10f017 | 1549 | static int f2fs_file_flush(struct file *file, fl_owner_t id) |
fbfa2cc5 | 1550 | { |
7a10f017 JK |
1551 | struct inode *inode = file_inode(file); |
1552 | ||
1553 | /* | |
1554 | * If the process doing a transaction is crashed, we should do | |
1555 | * roll-back. Otherwise, other reader/write can see corrupted database | |
1556 | * until all the writers close its file. Since this should be done | |
1557 | * before dropping file lock, it needs to do in ->flush. | |
1558 | */ | |
1559 | if (f2fs_is_atomic_file(inode) && | |
1560 | F2FS_I(inode)->inmem_task == current) | |
1561 | drop_inmem_pages(inode); | |
1562 | return 0; | |
fbfa2cc5 JK |
1563 | } |
1564 | ||
52656e6c | 1565 | static int f2fs_ioc_getflags(struct file *filp, unsigned long arg) |
fbfa2cc5 | 1566 | { |
6131ffaa | 1567 | struct inode *inode = file_inode(filp); |
fbfa2cc5 | 1568 | struct f2fs_inode_info *fi = F2FS_I(inode); |
2c1d0305 CY |
1569 | unsigned int flags = fi->i_flags & |
1570 | (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL); | |
52656e6c JK |
1571 | return put_user(flags, (int __user *)arg); |
1572 | } | |
fbfa2cc5 | 1573 | |
2c1d0305 CY |
1574 | static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags) |
1575 | { | |
1576 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
1577 | unsigned int oldflags; | |
1578 | ||
1579 | /* Is it quota file? Do not allow user to mess with it */ | |
1580 | if (IS_NOQUOTA(inode)) | |
1581 | return -EPERM; | |
1582 | ||
1583 | flags = f2fs_mask_flags(inode->i_mode, flags); | |
1584 | ||
1585 | oldflags = fi->i_flags; | |
1586 | ||
1587 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) | |
1588 | if (!capable(CAP_LINUX_IMMUTABLE)) | |
1589 | return -EPERM; | |
1590 | ||
1591 | flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL); | |
1592 | flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL); | |
1593 | fi->i_flags = flags; | |
1594 | ||
1595 | if (fi->i_flags & FS_PROJINHERIT_FL) | |
1596 | set_inode_flag(inode, FI_PROJ_INHERIT); | |
1597 | else | |
1598 | clear_inode_flag(inode, FI_PROJ_INHERIT); | |
1599 | ||
1600 | inode->i_ctime = current_time(inode); | |
1601 | f2fs_set_inode_flags(inode); | |
1602 | f2fs_mark_inode_dirty_sync(inode, false); | |
1603 | return 0; | |
1604 | } | |
1605 | ||
52656e6c JK |
1606 | static int f2fs_ioc_setflags(struct file *filp, unsigned long arg) |
1607 | { | |
1608 | struct inode *inode = file_inode(filp); | |
69494229 | 1609 | unsigned int flags; |
52656e6c | 1610 | int ret; |
fbfa2cc5 | 1611 | |
7fb17fe4 CY |
1612 | if (!inode_owner_or_capable(inode)) |
1613 | return -EACCES; | |
1614 | ||
1615 | if (get_user(flags, (int __user *)arg)) | |
1616 | return -EFAULT; | |
1617 | ||
52656e6c JK |
1618 | ret = mnt_want_write_file(filp); |
1619 | if (ret) | |
1620 | return ret; | |
fbfa2cc5 | 1621 | |
5955102c | 1622 | inode_lock(inode); |
fbfa2cc5 | 1623 | |
2c1d0305 | 1624 | ret = __f2fs_ioc_setflags(inode, flags); |
fbfa2cc5 | 1625 | |
a72d4b97 | 1626 | inode_unlock(inode); |
52656e6c JK |
1627 | mnt_drop_write_file(filp); |
1628 | return ret; | |
1629 | } | |
4b2fecc8 | 1630 | |
d49f3e89 CY |
1631 | static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) |
1632 | { | |
1633 | struct inode *inode = file_inode(filp); | |
1634 | ||
1635 | return put_user(inode->i_generation, (int __user *)arg); | |
1636 | } | |
1637 | ||
88b88a66 JK |
1638 | static int f2fs_ioc_start_atomic_write(struct file *filp) |
1639 | { | |
1640 | struct inode *inode = file_inode(filp); | |
f4c9c743 | 1641 | int ret; |
88b88a66 JK |
1642 | |
1643 | if (!inode_owner_or_capable(inode)) | |
1644 | return -EACCES; | |
1645 | ||
e811898c JK |
1646 | if (!S_ISREG(inode->i_mode)) |
1647 | return -EINVAL; | |
1648 | ||
7fb17fe4 CY |
1649 | ret = mnt_want_write_file(filp); |
1650 | if (ret) | |
1651 | return ret; | |
1652 | ||
0fac558b CY |
1653 | inode_lock(inode); |
1654 | ||
1e84371f | 1655 | if (f2fs_is_atomic_file(inode)) |
7fb17fe4 | 1656 | goto out; |
88b88a66 | 1657 | |
f4c9c743 CY |
1658 | ret = f2fs_convert_inline_inode(inode); |
1659 | if (ret) | |
7fb17fe4 | 1660 | goto out; |
88b88a66 | 1661 | |
91942321 | 1662 | set_inode_flag(inode, FI_ATOMIC_FILE); |
6c3acd97 | 1663 | set_inode_flag(inode, FI_HOT_DATA); |
d0239e1b JK |
1664 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
1665 | ||
c27753d6 | 1666 | if (!get_dirty_pages(inode)) |
684ca7e5 | 1667 | goto inc_stat; |
c27753d6 JK |
1668 | |
1669 | f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING, | |
204706c7 | 1670 | "Unexpected flush for atomic writes: ino=%lu, npages=%u", |
c27753d6 JK |
1671 | inode->i_ino, get_dirty_pages(inode)); |
1672 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); | |
684ca7e5 | 1673 | if (ret) { |
91942321 | 1674 | clear_inode_flag(inode, FI_ATOMIC_FILE); |
84a23fbe | 1675 | clear_inode_flag(inode, FI_HOT_DATA); |
684ca7e5 KM |
1676 | goto out; |
1677 | } | |
1678 | ||
1679 | inc_stat: | |
7a10f017 | 1680 | F2FS_I(inode)->inmem_task = current; |
26a28a0c JK |
1681 | stat_inc_atomic_write(inode); |
1682 | stat_update_max_atomic_write(inode); | |
684ca7e5 | 1683 | out: |
0fac558b | 1684 | inode_unlock(inode); |
7fb17fe4 | 1685 | mnt_drop_write_file(filp); |
c27753d6 | 1686 | return ret; |
88b88a66 JK |
1687 | } |
1688 | ||
1689 | static int f2fs_ioc_commit_atomic_write(struct file *filp) | |
1690 | { | |
1691 | struct inode *inode = file_inode(filp); | |
1692 | int ret; | |
1693 | ||
1694 | if (!inode_owner_or_capable(inode)) | |
1695 | return -EACCES; | |
1696 | ||
1697 | ret = mnt_want_write_file(filp); | |
1698 | if (ret) | |
1699 | return ret; | |
1700 | ||
0fac558b CY |
1701 | inode_lock(inode); |
1702 | ||
7fb17fe4 CY |
1703 | if (f2fs_is_volatile_file(inode)) |
1704 | goto err_out; | |
1705 | ||
6282adbf | 1706 | if (f2fs_is_atomic_file(inode)) { |
29b96b54 | 1707 | ret = commit_inmem_pages(inode); |
5fe45743 | 1708 | if (ret) |
edb27dee | 1709 | goto err_out; |
5fe45743 | 1710 | |
26a28a0c | 1711 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); |
5fe45743 CY |
1712 | if (!ret) { |
1713 | clear_inode_flag(inode, FI_ATOMIC_FILE); | |
84a23fbe | 1714 | clear_inode_flag(inode, FI_HOT_DATA); |
5fe45743 | 1715 | stat_dec_atomic_write(inode); |
447135a8 | 1716 | } |
26a28a0c | 1717 | } else { |
774e1b78 | 1718 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false); |
6282adbf | 1719 | } |
edb27dee | 1720 | err_out: |
0fac558b | 1721 | inode_unlock(inode); |
88b88a66 JK |
1722 | mnt_drop_write_file(filp); |
1723 | return ret; | |
1724 | } | |
1725 | ||
02a1335f JK |
1726 | static int f2fs_ioc_start_volatile_write(struct file *filp) |
1727 | { | |
1728 | struct inode *inode = file_inode(filp); | |
f4c9c743 | 1729 | int ret; |
02a1335f JK |
1730 | |
1731 | if (!inode_owner_or_capable(inode)) | |
1732 | return -EACCES; | |
1733 | ||
8ff0971f CY |
1734 | if (!S_ISREG(inode->i_mode)) |
1735 | return -EINVAL; | |
1736 | ||
7fb17fe4 CY |
1737 | ret = mnt_want_write_file(filp); |
1738 | if (ret) | |
1739 | return ret; | |
1740 | ||
0fac558b CY |
1741 | inode_lock(inode); |
1742 | ||
1e84371f | 1743 | if (f2fs_is_volatile_file(inode)) |
7fb17fe4 | 1744 | goto out; |
1e84371f | 1745 | |
f4c9c743 CY |
1746 | ret = f2fs_convert_inline_inode(inode); |
1747 | if (ret) | |
7fb17fe4 | 1748 | goto out; |
b3d208f9 | 1749 | |
648d50ba CY |
1750 | stat_inc_volatile_write(inode); |
1751 | stat_update_max_volatile_write(inode); | |
1752 | ||
91942321 | 1753 | set_inode_flag(inode, FI_VOLATILE_FILE); |
d0239e1b | 1754 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
7fb17fe4 | 1755 | out: |
0fac558b | 1756 | inode_unlock(inode); |
7fb17fe4 CY |
1757 | mnt_drop_write_file(filp); |
1758 | return ret; | |
02a1335f JK |
1759 | } |
1760 | ||
1e84371f JK |
1761 | static int f2fs_ioc_release_volatile_write(struct file *filp) |
1762 | { | |
1763 | struct inode *inode = file_inode(filp); | |
7fb17fe4 | 1764 | int ret; |
1e84371f JK |
1765 | |
1766 | if (!inode_owner_or_capable(inode)) | |
1767 | return -EACCES; | |
1768 | ||
7fb17fe4 CY |
1769 | ret = mnt_want_write_file(filp); |
1770 | if (ret) | |
1771 | return ret; | |
1772 | ||
0fac558b CY |
1773 | inode_lock(inode); |
1774 | ||
1e84371f | 1775 | if (!f2fs_is_volatile_file(inode)) |
7fb17fe4 | 1776 | goto out; |
1e84371f | 1777 | |
7fb17fe4 CY |
1778 | if (!f2fs_is_first_block_written(inode)) { |
1779 | ret = truncate_partial_data_page(inode, 0, true); | |
1780 | goto out; | |
1781 | } | |
3c6c2beb | 1782 | |
7fb17fe4 CY |
1783 | ret = punch_hole(inode, 0, F2FS_BLKSIZE); |
1784 | out: | |
0fac558b | 1785 | inode_unlock(inode); |
7fb17fe4 CY |
1786 | mnt_drop_write_file(filp); |
1787 | return ret; | |
1e84371f JK |
1788 | } |
1789 | ||
1790 | static int f2fs_ioc_abort_volatile_write(struct file *filp) | |
1791 | { | |
1792 | struct inode *inode = file_inode(filp); | |
1793 | int ret; | |
1794 | ||
1795 | if (!inode_owner_or_capable(inode)) | |
1796 | return -EACCES; | |
1797 | ||
1798 | ret = mnt_want_write_file(filp); | |
1799 | if (ret) | |
1800 | return ret; | |
1801 | ||
0fac558b CY |
1802 | inode_lock(inode); |
1803 | ||
26dc3d44 | 1804 | if (f2fs_is_atomic_file(inode)) |
29b96b54 | 1805 | drop_inmem_pages(inode); |
732d5648 | 1806 | if (f2fs_is_volatile_file(inode)) { |
91942321 | 1807 | clear_inode_flag(inode, FI_VOLATILE_FILE); |
648d50ba | 1808 | stat_dec_volatile_write(inode); |
608514de | 1809 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true); |
732d5648 | 1810 | } |
de6a8ec9 | 1811 | |
0fac558b CY |
1812 | inode_unlock(inode); |
1813 | ||
1e84371f | 1814 | mnt_drop_write_file(filp); |
d0239e1b | 1815 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
1e84371f JK |
1816 | return ret; |
1817 | } | |
1818 | ||
1abff93d JK |
1819 | static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) |
1820 | { | |
1821 | struct inode *inode = file_inode(filp); | |
1822 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1823 | struct super_block *sb = sbi->sb; | |
1824 | __u32 in; | |
7fb17fe4 | 1825 | int ret; |
1abff93d JK |
1826 | |
1827 | if (!capable(CAP_SYS_ADMIN)) | |
1828 | return -EPERM; | |
1829 | ||
1830 | if (get_user(in, (__u32 __user *)arg)) | |
1831 | return -EFAULT; | |
1832 | ||
7fb17fe4 CY |
1833 | ret = mnt_want_write_file(filp); |
1834 | if (ret) | |
1835 | return ret; | |
1836 | ||
1abff93d JK |
1837 | switch (in) { |
1838 | case F2FS_GOING_DOWN_FULLSYNC: | |
1839 | sb = freeze_bdev(sb->s_bdev); | |
1840 | if (sb && !IS_ERR(sb)) { | |
38f91ca8 | 1841 | f2fs_stop_checkpoint(sbi, false); |
1abff93d JK |
1842 | thaw_bdev(sb->s_bdev, sb); |
1843 | } | |
1844 | break; | |
1845 | case F2FS_GOING_DOWN_METASYNC: | |
1846 | /* do checkpoint only */ | |
1847 | f2fs_sync_fs(sb, 1); | |
38f91ca8 | 1848 | f2fs_stop_checkpoint(sbi, false); |
1abff93d JK |
1849 | break; |
1850 | case F2FS_GOING_DOWN_NOSYNC: | |
38f91ca8 | 1851 | f2fs_stop_checkpoint(sbi, false); |
1abff93d | 1852 | break; |
c912a829 | 1853 | case F2FS_GOING_DOWN_METAFLUSH: |
b0af6d49 | 1854 | sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO); |
38f91ca8 | 1855 | f2fs_stop_checkpoint(sbi, false); |
c912a829 | 1856 | break; |
1abff93d | 1857 | default: |
7fb17fe4 CY |
1858 | ret = -EINVAL; |
1859 | goto out; | |
1abff93d | 1860 | } |
d0239e1b | 1861 | f2fs_update_time(sbi, REQ_TIME); |
7fb17fe4 CY |
1862 | out: |
1863 | mnt_drop_write_file(filp); | |
1864 | return ret; | |
1abff93d JK |
1865 | } |
1866 | ||
52656e6c JK |
1867 | static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg) |
1868 | { | |
1869 | struct inode *inode = file_inode(filp); | |
1870 | struct super_block *sb = inode->i_sb; | |
1871 | struct request_queue *q = bdev_get_queue(sb->s_bdev); | |
1872 | struct fstrim_range range; | |
1873 | int ret; | |
4b2fecc8 | 1874 | |
52656e6c JK |
1875 | if (!capable(CAP_SYS_ADMIN)) |
1876 | return -EPERM; | |
4b2fecc8 | 1877 | |
52656e6c JK |
1878 | if (!blk_queue_discard(q)) |
1879 | return -EOPNOTSUPP; | |
4b2fecc8 | 1880 | |
52656e6c JK |
1881 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
1882 | sizeof(range))) | |
1883 | return -EFAULT; | |
4b2fecc8 | 1884 | |
7fb17fe4 CY |
1885 | ret = mnt_want_write_file(filp); |
1886 | if (ret) | |
1887 | return ret; | |
1888 | ||
52656e6c JK |
1889 | range.minlen = max((unsigned int)range.minlen, |
1890 | q->limits.discard_granularity); | |
1891 | ret = f2fs_trim_fs(F2FS_SB(sb), &range); | |
7fb17fe4 | 1892 | mnt_drop_write_file(filp); |
52656e6c JK |
1893 | if (ret < 0) |
1894 | return ret; | |
4b2fecc8 | 1895 | |
52656e6c JK |
1896 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
1897 | sizeof(range))) | |
1898 | return -EFAULT; | |
d0239e1b | 1899 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
52656e6c JK |
1900 | return 0; |
1901 | } | |
1902 | ||
f424f664 JK |
1903 | static bool uuid_is_nonzero(__u8 u[16]) |
1904 | { | |
1905 | int i; | |
1906 | ||
1907 | for (i = 0; i < 16; i++) | |
1908 | if (u[i]) | |
1909 | return true; | |
1910 | return false; | |
1911 | } | |
1912 | ||
1913 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) | |
1914 | { | |
f424f664 JK |
1915 | struct inode *inode = file_inode(filp); |
1916 | ||
ead710b7 CY |
1917 | if (!f2fs_sb_has_crypto(inode->i_sb)) |
1918 | return -EOPNOTSUPP; | |
1919 | ||
d0239e1b | 1920 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
7fb17fe4 | 1921 | |
db717d8e | 1922 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
f424f664 JK |
1923 | } |
1924 | ||
1925 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) | |
1926 | { | |
ead710b7 CY |
1927 | if (!f2fs_sb_has_crypto(file_inode(filp)->i_sb)) |
1928 | return -EOPNOTSUPP; | |
db717d8e | 1929 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
f424f664 JK |
1930 | } |
1931 | ||
1932 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) | |
1933 | { | |
1934 | struct inode *inode = file_inode(filp); | |
1935 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1936 | int err; | |
1937 | ||
1938 | if (!f2fs_sb_has_crypto(inode->i_sb)) | |
1939 | return -EOPNOTSUPP; | |
1940 | ||
1941 | if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt)) | |
1942 | goto got_it; | |
1943 | ||
1944 | err = mnt_want_write_file(filp); | |
1945 | if (err) | |
1946 | return err; | |
1947 | ||
1948 | /* update superblock with uuid */ | |
1949 | generate_random_uuid(sbi->raw_super->encrypt_pw_salt); | |
1950 | ||
c5bda1c8 | 1951 | err = f2fs_commit_super(sbi, false); |
f424f664 JK |
1952 | if (err) { |
1953 | /* undo new data */ | |
1954 | memset(sbi->raw_super->encrypt_pw_salt, 0, 16); | |
e8240f65 | 1955 | mnt_drop_write_file(filp); |
f424f664 JK |
1956 | return err; |
1957 | } | |
e8240f65 | 1958 | mnt_drop_write_file(filp); |
f424f664 JK |
1959 | got_it: |
1960 | if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, | |
1961 | 16)) | |
1962 | return -EFAULT; | |
1963 | return 0; | |
1964 | } | |
1965 | ||
c1c1b583 CY |
1966 | static int f2fs_ioc_gc(struct file *filp, unsigned long arg) |
1967 | { | |
1968 | struct inode *inode = file_inode(filp); | |
1969 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
d530d4d8 | 1970 | __u32 sync; |
7fb17fe4 | 1971 | int ret; |
c1c1b583 CY |
1972 | |
1973 | if (!capable(CAP_SYS_ADMIN)) | |
1974 | return -EPERM; | |
1975 | ||
d530d4d8 | 1976 | if (get_user(sync, (__u32 __user *)arg)) |
c1c1b583 CY |
1977 | return -EFAULT; |
1978 | ||
d530d4d8 CY |
1979 | if (f2fs_readonly(sbi->sb)) |
1980 | return -EROFS; | |
c1c1b583 | 1981 | |
7fb17fe4 CY |
1982 | ret = mnt_want_write_file(filp); |
1983 | if (ret) | |
1984 | return ret; | |
1985 | ||
d530d4d8 | 1986 | if (!sync) { |
7fb17fe4 CY |
1987 | if (!mutex_trylock(&sbi->gc_mutex)) { |
1988 | ret = -EBUSY; | |
1989 | goto out; | |
1990 | } | |
d530d4d8 CY |
1991 | } else { |
1992 | mutex_lock(&sbi->gc_mutex); | |
c1c1b583 CY |
1993 | } |
1994 | ||
e066b83c | 1995 | ret = f2fs_gc(sbi, sync, true, NULL_SEGNO); |
7fb17fe4 CY |
1996 | out: |
1997 | mnt_drop_write_file(filp); | |
1998 | return ret; | |
c1c1b583 CY |
1999 | } |
2000 | ||
34dc77ad JK |
2001 | static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg) |
2002 | { | |
2003 | struct inode *inode = file_inode(filp); | |
2004 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2005 | struct f2fs_gc_range range; | |
2006 | u64 end; | |
2007 | int ret; | |
2008 | ||
2009 | if (!capable(CAP_SYS_ADMIN)) | |
2010 | return -EPERM; | |
2011 | ||
2012 | if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg, | |
2013 | sizeof(range))) | |
2014 | return -EFAULT; | |
2015 | ||
2016 | if (f2fs_readonly(sbi->sb)) | |
2017 | return -EROFS; | |
2018 | ||
2019 | ret = mnt_want_write_file(filp); | |
2020 | if (ret) | |
2021 | return ret; | |
2022 | ||
2023 | end = range.start + range.len; | |
2024 | if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi)) | |
2025 | return -EINVAL; | |
2026 | do_more: | |
2027 | if (!range.sync) { | |
2028 | if (!mutex_trylock(&sbi->gc_mutex)) { | |
2029 | ret = -EBUSY; | |
2030 | goto out; | |
2031 | } | |
2032 | } else { | |
2033 | mutex_lock(&sbi->gc_mutex); | |
2034 | } | |
2035 | ||
2036 | ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start)); | |
2037 | range.start += sbi->blocks_per_seg; | |
2038 | if (range.start <= end) | |
2039 | goto do_more; | |
2040 | out: | |
2041 | mnt_drop_write_file(filp); | |
2042 | return ret; | |
2043 | } | |
2044 | ||
456b88e4 CY |
2045 | static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg) |
2046 | { | |
2047 | struct inode *inode = file_inode(filp); | |
2048 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
7fb17fe4 | 2049 | int ret; |
456b88e4 CY |
2050 | |
2051 | if (!capable(CAP_SYS_ADMIN)) | |
2052 | return -EPERM; | |
2053 | ||
2054 | if (f2fs_readonly(sbi->sb)) | |
2055 | return -EROFS; | |
2056 | ||
7fb17fe4 CY |
2057 | ret = mnt_want_write_file(filp); |
2058 | if (ret) | |
2059 | return ret; | |
2060 | ||
2061 | ret = f2fs_sync_fs(sbi->sb, 1); | |
2062 | ||
2063 | mnt_drop_write_file(filp); | |
2064 | return ret; | |
456b88e4 CY |
2065 | } |
2066 | ||
d323d005 CY |
2067 | static int f2fs_defragment_range(struct f2fs_sb_info *sbi, |
2068 | struct file *filp, | |
2069 | struct f2fs_defragment *range) | |
2070 | { | |
2071 | struct inode *inode = file_inode(filp); | |
d5097be5 HL |
2072 | struct f2fs_map_blocks map = { .m_next_pgofs = NULL, |
2073 | .m_seg_type = NO_CHECK_TYPE }; | |
e15882b6 | 2074 | struct extent_info ei = {0,0,0}; |
d323d005 | 2075 | pgoff_t pg_start, pg_end; |
3519e3f9 | 2076 | unsigned int blk_per_seg = sbi->blocks_per_seg; |
d323d005 | 2077 | unsigned int total = 0, sec_num; |
d323d005 CY |
2078 | block_t blk_end = 0; |
2079 | bool fragmented = false; | |
2080 | int err; | |
2081 | ||
2082 | /* if in-place-update policy is enabled, don't waste time here */ | |
7eab0c0d | 2083 | if (need_inplace_update_policy(inode, NULL)) |
d323d005 CY |
2084 | return -EINVAL; |
2085 | ||
09cbfeaf KS |
2086 | pg_start = range->start >> PAGE_SHIFT; |
2087 | pg_end = (range->start + range->len) >> PAGE_SHIFT; | |
d323d005 | 2088 | |
2c4db1a6 | 2089 | f2fs_balance_fs(sbi, true); |
d323d005 | 2090 | |
5955102c | 2091 | inode_lock(inode); |
d323d005 CY |
2092 | |
2093 | /* writeback all dirty pages in the range */ | |
2094 | err = filemap_write_and_wait_range(inode->i_mapping, range->start, | |
d8fe4f0e | 2095 | range->start + range->len - 1); |
d323d005 CY |
2096 | if (err) |
2097 | goto out; | |
2098 | ||
2099 | /* | |
2100 | * lookup mapping info in extent cache, skip defragmenting if physical | |
2101 | * block addresses are continuous. | |
2102 | */ | |
2103 | if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) { | |
2104 | if (ei.fofs + ei.len >= pg_end) | |
2105 | goto out; | |
2106 | } | |
2107 | ||
2108 | map.m_lblk = pg_start; | |
d323d005 CY |
2109 | |
2110 | /* | |
2111 | * lookup mapping info in dnode page cache, skip defragmenting if all | |
2112 | * physical block addresses are continuous even if there are hole(s) | |
2113 | * in logical blocks. | |
2114 | */ | |
2115 | while (map.m_lblk < pg_end) { | |
a1c1e9b7 | 2116 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2117 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2118 | if (err) |
2119 | goto out; | |
2120 | ||
2121 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
2122 | map.m_lblk++; | |
d323d005 CY |
2123 | continue; |
2124 | } | |
2125 | ||
2126 | if (blk_end && blk_end != map.m_pblk) { | |
2127 | fragmented = true; | |
2128 | break; | |
2129 | } | |
2130 | blk_end = map.m_pblk + map.m_len; | |
2131 | ||
2132 | map.m_lblk += map.m_len; | |
d323d005 CY |
2133 | } |
2134 | ||
2135 | if (!fragmented) | |
2136 | goto out; | |
2137 | ||
2138 | map.m_lblk = pg_start; | |
2139 | map.m_len = pg_end - pg_start; | |
2140 | ||
4ddb1a4d | 2141 | sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi); |
d323d005 CY |
2142 | |
2143 | /* | |
2144 | * make sure there are enough free section for LFS allocation, this can | |
2145 | * avoid defragment running in SSR mode when free section are allocated | |
2146 | * intensively | |
2147 | */ | |
7f3037a5 | 2148 | if (has_not_enough_free_secs(sbi, 0, sec_num)) { |
d323d005 CY |
2149 | err = -EAGAIN; |
2150 | goto out; | |
2151 | } | |
2152 | ||
2153 | while (map.m_lblk < pg_end) { | |
2154 | pgoff_t idx; | |
2155 | int cnt = 0; | |
2156 | ||
2157 | do_map: | |
a1c1e9b7 | 2158 | map.m_len = pg_end - map.m_lblk; |
f2220c7f | 2159 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
d323d005 CY |
2160 | if (err) |
2161 | goto clear_out; | |
2162 | ||
2163 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { | |
2164 | map.m_lblk++; | |
2165 | continue; | |
2166 | } | |
2167 | ||
91942321 | 2168 | set_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 CY |
2169 | |
2170 | idx = map.m_lblk; | |
2171 | while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) { | |
2172 | struct page *page; | |
2173 | ||
2174 | page = get_lock_data_page(inode, idx, true); | |
2175 | if (IS_ERR(page)) { | |
2176 | err = PTR_ERR(page); | |
2177 | goto clear_out; | |
2178 | } | |
2179 | ||
2180 | set_page_dirty(page); | |
2181 | f2fs_put_page(page, 1); | |
2182 | ||
2183 | idx++; | |
2184 | cnt++; | |
2185 | total++; | |
2186 | } | |
2187 | ||
2188 | map.m_lblk = idx; | |
d323d005 CY |
2189 | |
2190 | if (idx < pg_end && cnt < blk_per_seg) | |
2191 | goto do_map; | |
2192 | ||
91942321 | 2193 | clear_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 CY |
2194 | |
2195 | err = filemap_fdatawrite(inode->i_mapping); | |
2196 | if (err) | |
2197 | goto out; | |
2198 | } | |
2199 | clear_out: | |
91942321 | 2200 | clear_inode_flag(inode, FI_DO_DEFRAG); |
d323d005 | 2201 | out: |
5955102c | 2202 | inode_unlock(inode); |
d323d005 | 2203 | if (!err) |
09cbfeaf | 2204 | range->len = (u64)total << PAGE_SHIFT; |
d323d005 CY |
2205 | return err; |
2206 | } | |
2207 | ||
2208 | static int f2fs_ioc_defragment(struct file *filp, unsigned long arg) | |
2209 | { | |
2210 | struct inode *inode = file_inode(filp); | |
2211 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2212 | struct f2fs_defragment range; | |
2213 | int err; | |
2214 | ||
2215 | if (!capable(CAP_SYS_ADMIN)) | |
2216 | return -EPERM; | |
2217 | ||
7eab0c0d | 2218 | if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode)) |
d323d005 CY |
2219 | return -EINVAL; |
2220 | ||
d7563861 KM |
2221 | if (f2fs_readonly(sbi->sb)) |
2222 | return -EROFS; | |
d323d005 CY |
2223 | |
2224 | if (copy_from_user(&range, (struct f2fs_defragment __user *)arg, | |
d7563861 KM |
2225 | sizeof(range))) |
2226 | return -EFAULT; | |
d323d005 CY |
2227 | |
2228 | /* verify alignment of offset & size */ | |
d7563861 KM |
2229 | if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1)) |
2230 | return -EINVAL; | |
d323d005 | 2231 | |
1941d7bc | 2232 | if (unlikely((range.start + range.len) >> PAGE_SHIFT > |
d7563861 KM |
2233 | sbi->max_file_blocks)) |
2234 | return -EINVAL; | |
2235 | ||
2236 | err = mnt_want_write_file(filp); | |
2237 | if (err) | |
2238 | return err; | |
1941d7bc | 2239 | |
d323d005 | 2240 | err = f2fs_defragment_range(sbi, filp, &range); |
d7563861 KM |
2241 | mnt_drop_write_file(filp); |
2242 | ||
d0239e1b | 2243 | f2fs_update_time(sbi, REQ_TIME); |
d323d005 | 2244 | if (err < 0) |
d7563861 | 2245 | return err; |
d323d005 CY |
2246 | |
2247 | if (copy_to_user((struct f2fs_defragment __user *)arg, &range, | |
2248 | sizeof(range))) | |
d7563861 KM |
2249 | return -EFAULT; |
2250 | ||
2251 | return 0; | |
d323d005 CY |
2252 | } |
2253 | ||
4dd6f977 JK |
2254 | static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, |
2255 | struct file *file_out, loff_t pos_out, size_t len) | |
2256 | { | |
2257 | struct inode *src = file_inode(file_in); | |
2258 | struct inode *dst = file_inode(file_out); | |
2259 | struct f2fs_sb_info *sbi = F2FS_I_SB(src); | |
2260 | size_t olen = len, dst_max_i_size = 0; | |
2261 | size_t dst_osize; | |
2262 | int ret; | |
2263 | ||
2264 | if (file_in->f_path.mnt != file_out->f_path.mnt || | |
2265 | src->i_sb != dst->i_sb) | |
2266 | return -EXDEV; | |
2267 | ||
2268 | if (unlikely(f2fs_readonly(src->i_sb))) | |
2269 | return -EROFS; | |
2270 | ||
fe8494bf CY |
2271 | if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode)) |
2272 | return -EINVAL; | |
4dd6f977 JK |
2273 | |
2274 | if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst)) | |
2275 | return -EOPNOTSUPP; | |
2276 | ||
d95fd91c FL |
2277 | if (src == dst) { |
2278 | if (pos_in == pos_out) | |
2279 | return 0; | |
2280 | if (pos_out > pos_in && pos_out < pos_in + len) | |
2281 | return -EINVAL; | |
2282 | } | |
2283 | ||
4dd6f977 | 2284 | inode_lock(src); |
bb06664a | 2285 | down_write(&F2FS_I(src)->dio_rwsem[WRITE]); |
20a3d61d | 2286 | if (src != dst) { |
bb06664a CY |
2287 | ret = -EBUSY; |
2288 | if (!inode_trylock(dst)) | |
2289 | goto out; | |
2290 | if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) { | |
2291 | inode_unlock(dst); | |
20a3d61d CY |
2292 | goto out; |
2293 | } | |
2294 | } | |
4dd6f977 JK |
2295 | |
2296 | ret = -EINVAL; | |
2297 | if (pos_in + len > src->i_size || pos_in + len < pos_in) | |
2298 | goto out_unlock; | |
2299 | if (len == 0) | |
2300 | olen = len = src->i_size - pos_in; | |
2301 | if (pos_in + len == src->i_size) | |
2302 | len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in; | |
2303 | if (len == 0) { | |
2304 | ret = 0; | |
2305 | goto out_unlock; | |
2306 | } | |
2307 | ||
2308 | dst_osize = dst->i_size; | |
2309 | if (pos_out + olen > dst->i_size) | |
2310 | dst_max_i_size = pos_out + olen; | |
2311 | ||
2312 | /* verify the end result is block aligned */ | |
2313 | if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) || | |
2314 | !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) || | |
2315 | !IS_ALIGNED(pos_out, F2FS_BLKSIZE)) | |
2316 | goto out_unlock; | |
2317 | ||
2318 | ret = f2fs_convert_inline_inode(src); | |
2319 | if (ret) | |
2320 | goto out_unlock; | |
2321 | ||
2322 | ret = f2fs_convert_inline_inode(dst); | |
2323 | if (ret) | |
2324 | goto out_unlock; | |
2325 | ||
2326 | /* write out all dirty pages from offset */ | |
2327 | ret = filemap_write_and_wait_range(src->i_mapping, | |
2328 | pos_in, pos_in + len); | |
2329 | if (ret) | |
2330 | goto out_unlock; | |
2331 | ||
2332 | ret = filemap_write_and_wait_range(dst->i_mapping, | |
2333 | pos_out, pos_out + len); | |
2334 | if (ret) | |
2335 | goto out_unlock; | |
2336 | ||
2337 | f2fs_balance_fs(sbi, true); | |
2338 | f2fs_lock_op(sbi); | |
61e4da11 FL |
2339 | ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS, |
2340 | pos_out >> F2FS_BLKSIZE_BITS, | |
2341 | len >> F2FS_BLKSIZE_BITS, false); | |
4dd6f977 JK |
2342 | |
2343 | if (!ret) { | |
2344 | if (dst_max_i_size) | |
2345 | f2fs_i_size_write(dst, dst_max_i_size); | |
2346 | else if (dst_osize != dst->i_size) | |
2347 | f2fs_i_size_write(dst, dst_osize); | |
2348 | } | |
2349 | f2fs_unlock_op(sbi); | |
2350 | out_unlock: | |
bb06664a CY |
2351 | if (src != dst) { |
2352 | up_write(&F2FS_I(dst)->dio_rwsem[WRITE]); | |
4dd6f977 | 2353 | inode_unlock(dst); |
bb06664a | 2354 | } |
20a3d61d | 2355 | out: |
bb06664a | 2356 | up_write(&F2FS_I(src)->dio_rwsem[WRITE]); |
4dd6f977 JK |
2357 | inode_unlock(src); |
2358 | return ret; | |
2359 | } | |
2360 | ||
2361 | static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) | |
2362 | { | |
2363 | struct f2fs_move_range range; | |
2364 | struct fd dst; | |
2365 | int err; | |
2366 | ||
2367 | if (!(filp->f_mode & FMODE_READ) || | |
2368 | !(filp->f_mode & FMODE_WRITE)) | |
2369 | return -EBADF; | |
2370 | ||
2371 | if (copy_from_user(&range, (struct f2fs_move_range __user *)arg, | |
2372 | sizeof(range))) | |
2373 | return -EFAULT; | |
2374 | ||
2375 | dst = fdget(range.dst_fd); | |
2376 | if (!dst.file) | |
2377 | return -EBADF; | |
2378 | ||
2379 | if (!(dst.file->f_mode & FMODE_WRITE)) { | |
2380 | err = -EBADF; | |
2381 | goto err_out; | |
2382 | } | |
2383 | ||
2384 | err = mnt_want_write_file(filp); | |
2385 | if (err) | |
2386 | goto err_out; | |
2387 | ||
2388 | err = f2fs_move_file_range(filp, range.pos_in, dst.file, | |
2389 | range.pos_out, range.len); | |
2390 | ||
2391 | mnt_drop_write_file(filp); | |
3cecfa5f KM |
2392 | if (err) |
2393 | goto err_out; | |
4dd6f977 JK |
2394 | |
2395 | if (copy_to_user((struct f2fs_move_range __user *)arg, | |
2396 | &range, sizeof(range))) | |
2397 | err = -EFAULT; | |
2398 | err_out: | |
2399 | fdput(dst); | |
2400 | return err; | |
2401 | } | |
2402 | ||
e066b83c JK |
2403 | static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) |
2404 | { | |
2405 | struct inode *inode = file_inode(filp); | |
2406 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2407 | struct sit_info *sm = SIT_I(sbi); | |
2408 | unsigned int start_segno = 0, end_segno = 0; | |
2409 | unsigned int dev_start_segno = 0, dev_end_segno = 0; | |
2410 | struct f2fs_flush_device range; | |
2411 | int ret; | |
2412 | ||
2413 | if (!capable(CAP_SYS_ADMIN)) | |
2414 | return -EPERM; | |
2415 | ||
2416 | if (f2fs_readonly(sbi->sb)) | |
2417 | return -EROFS; | |
2418 | ||
2419 | if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg, | |
2420 | sizeof(range))) | |
2421 | return -EFAULT; | |
2422 | ||
2423 | if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num || | |
2424 | sbi->segs_per_sec != 1) { | |
2425 | f2fs_msg(sbi->sb, KERN_WARNING, | |
2426 | "Can't flush %u in %d for segs_per_sec %u != 1\n", | |
2427 | range.dev_num, sbi->s_ndevs, | |
2428 | sbi->segs_per_sec); | |
2429 | return -EINVAL; | |
2430 | } | |
2431 | ||
2432 | ret = mnt_want_write_file(filp); | |
2433 | if (ret) | |
2434 | return ret; | |
2435 | ||
2436 | if (range.dev_num != 0) | |
2437 | dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk); | |
2438 | dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk); | |
2439 | ||
2440 | start_segno = sm->last_victim[FLUSH_DEVICE]; | |
2441 | if (start_segno < dev_start_segno || start_segno >= dev_end_segno) | |
2442 | start_segno = dev_start_segno; | |
2443 | end_segno = min(start_segno + range.segments, dev_end_segno); | |
2444 | ||
2445 | while (start_segno < end_segno) { | |
2446 | if (!mutex_trylock(&sbi->gc_mutex)) { | |
2447 | ret = -EBUSY; | |
2448 | goto out; | |
2449 | } | |
2450 | sm->last_victim[GC_CB] = end_segno + 1; | |
2451 | sm->last_victim[GC_GREEDY] = end_segno + 1; | |
2452 | sm->last_victim[ALLOC_NEXT] = end_segno + 1; | |
2453 | ret = f2fs_gc(sbi, true, true, start_segno); | |
2454 | if (ret == -EAGAIN) | |
2455 | ret = 0; | |
2456 | else if (ret < 0) | |
2457 | break; | |
2458 | start_segno++; | |
2459 | } | |
2460 | out: | |
2461 | mnt_drop_write_file(filp); | |
2462 | return ret; | |
2463 | } | |
2464 | ||
e65ef207 JK |
2465 | static int f2fs_ioc_get_features(struct file *filp, unsigned long arg) |
2466 | { | |
2467 | struct inode *inode = file_inode(filp); | |
2468 | u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature); | |
2469 | ||
2470 | /* Must validate to set it with SQLite behavior in Android. */ | |
2471 | sb_feature |= F2FS_FEATURE_ATOMIC_WRITE; | |
2472 | ||
2473 | return put_user(sb_feature, (u32 __user *)arg); | |
2474 | } | |
e066b83c | 2475 | |
2c1d0305 CY |
2476 | #ifdef CONFIG_QUOTA |
2477 | static int f2fs_ioc_setproject(struct file *filp, __u32 projid) | |
2478 | { | |
2479 | struct inode *inode = file_inode(filp); | |
2480 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
2481 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2482 | struct super_block *sb = sbi->sb; | |
2483 | struct dquot *transfer_to[MAXQUOTAS] = {}; | |
2484 | struct page *ipage; | |
2485 | kprojid_t kprojid; | |
2486 | int err; | |
2487 | ||
2488 | if (!f2fs_sb_has_project_quota(sb)) { | |
2489 | if (projid != F2FS_DEF_PROJID) | |
2490 | return -EOPNOTSUPP; | |
2491 | else | |
2492 | return 0; | |
2493 | } | |
2494 | ||
2495 | if (!f2fs_has_extra_attr(inode)) | |
2496 | return -EOPNOTSUPP; | |
2497 | ||
2498 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
2499 | ||
2500 | if (projid_eq(kprojid, F2FS_I(inode)->i_projid)) | |
2501 | return 0; | |
2502 | ||
2503 | err = mnt_want_write_file(filp); | |
2504 | if (err) | |
2505 | return err; | |
2506 | ||
2507 | err = -EPERM; | |
2508 | inode_lock(inode); | |
2509 | ||
2510 | /* Is it quota file? Do not allow user to mess with it */ | |
2511 | if (IS_NOQUOTA(inode)) | |
2512 | goto out_unlock; | |
2513 | ||
2514 | ipage = get_node_page(sbi, inode->i_ino); | |
2515 | if (IS_ERR(ipage)) { | |
2516 | err = PTR_ERR(ipage); | |
2517 | goto out_unlock; | |
2518 | } | |
2519 | ||
2520 | if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize, | |
2521 | i_projid)) { | |
2522 | err = -EOVERFLOW; | |
2523 | f2fs_put_page(ipage, 1); | |
2524 | goto out_unlock; | |
2525 | } | |
2526 | f2fs_put_page(ipage, 1); | |
2527 | ||
2528 | dquot_initialize(inode); | |
2529 | ||
2530 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); | |
2531 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
2532 | err = __dquot_transfer(inode, transfer_to); | |
2533 | dqput(transfer_to[PRJQUOTA]); | |
2534 | if (err) | |
2535 | goto out_dirty; | |
2536 | } | |
2537 | ||
2538 | F2FS_I(inode)->i_projid = kprojid; | |
2539 | inode->i_ctime = current_time(inode); | |
2540 | out_dirty: | |
2541 | f2fs_mark_inode_dirty_sync(inode, true); | |
2542 | out_unlock: | |
2543 | inode_unlock(inode); | |
2544 | mnt_drop_write_file(filp); | |
2545 | return err; | |
2546 | } | |
2547 | #else | |
2548 | static int f2fs_ioc_setproject(struct file *filp, __u32 projid) | |
2549 | { | |
2550 | if (projid != F2FS_DEF_PROJID) | |
2551 | return -EOPNOTSUPP; | |
2552 | return 0; | |
2553 | } | |
2554 | #endif | |
2555 | ||
2556 | /* Transfer internal flags to xflags */ | |
2557 | static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags) | |
2558 | { | |
2559 | __u32 xflags = 0; | |
2560 | ||
2561 | if (iflags & FS_SYNC_FL) | |
2562 | xflags |= FS_XFLAG_SYNC; | |
2563 | if (iflags & FS_IMMUTABLE_FL) | |
2564 | xflags |= FS_XFLAG_IMMUTABLE; | |
2565 | if (iflags & FS_APPEND_FL) | |
2566 | xflags |= FS_XFLAG_APPEND; | |
2567 | if (iflags & FS_NODUMP_FL) | |
2568 | xflags |= FS_XFLAG_NODUMP; | |
2569 | if (iflags & FS_NOATIME_FL) | |
2570 | xflags |= FS_XFLAG_NOATIME; | |
2571 | if (iflags & FS_PROJINHERIT_FL) | |
2572 | xflags |= FS_XFLAG_PROJINHERIT; | |
2573 | return xflags; | |
2574 | } | |
2575 | ||
2576 | #define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \ | |
2577 | FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \ | |
2578 | FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT) | |
2579 | ||
2580 | /* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */ | |
2581 | #define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \ | |
2582 | FS_IMMUTABLE_FL | \ | |
2583 | FS_APPEND_FL | \ | |
2584 | FS_NODUMP_FL | \ | |
2585 | FS_NOATIME_FL | \ | |
2586 | FS_PROJINHERIT_FL) | |
2587 | ||
2588 | /* Transfer xflags flags to internal */ | |
2589 | static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags) | |
2590 | { | |
2591 | unsigned long iflags = 0; | |
2592 | ||
2593 | if (xflags & FS_XFLAG_SYNC) | |
2594 | iflags |= FS_SYNC_FL; | |
2595 | if (xflags & FS_XFLAG_IMMUTABLE) | |
2596 | iflags |= FS_IMMUTABLE_FL; | |
2597 | if (xflags & FS_XFLAG_APPEND) | |
2598 | iflags |= FS_APPEND_FL; | |
2599 | if (xflags & FS_XFLAG_NODUMP) | |
2600 | iflags |= FS_NODUMP_FL; | |
2601 | if (xflags & FS_XFLAG_NOATIME) | |
2602 | iflags |= FS_NOATIME_FL; | |
2603 | if (xflags & FS_XFLAG_PROJINHERIT) | |
2604 | iflags |= FS_PROJINHERIT_FL; | |
2605 | ||
2606 | return iflags; | |
2607 | } | |
2608 | ||
2609 | static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg) | |
2610 | { | |
2611 | struct inode *inode = file_inode(filp); | |
2612 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
2613 | struct fsxattr fa; | |
2614 | ||
2615 | memset(&fa, 0, sizeof(struct fsxattr)); | |
2616 | fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags & | |
2617 | (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL)); | |
2618 | ||
2619 | if (f2fs_sb_has_project_quota(inode->i_sb)) | |
2620 | fa.fsx_projid = (__u32)from_kprojid(&init_user_ns, | |
2621 | fi->i_projid); | |
2622 | ||
2623 | if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa))) | |
2624 | return -EFAULT; | |
2625 | return 0; | |
2626 | } | |
2627 | ||
2628 | static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg) | |
2629 | { | |
2630 | struct inode *inode = file_inode(filp); | |
2631 | struct f2fs_inode_info *fi = F2FS_I(inode); | |
2632 | struct fsxattr fa; | |
2633 | unsigned int flags; | |
2634 | int err; | |
2635 | ||
2636 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa))) | |
2637 | return -EFAULT; | |
2638 | ||
2639 | /* Make sure caller has proper permission */ | |
2640 | if (!inode_owner_or_capable(inode)) | |
2641 | return -EACCES; | |
2642 | ||
2643 | if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS) | |
2644 | return -EOPNOTSUPP; | |
2645 | ||
2646 | flags = f2fs_xflags_to_iflags(fa.fsx_xflags); | |
2647 | if (f2fs_mask_flags(inode->i_mode, flags) != flags) | |
2648 | return -EOPNOTSUPP; | |
2649 | ||
2650 | err = mnt_want_write_file(filp); | |
2651 | if (err) | |
2652 | return err; | |
2653 | ||
2654 | inode_lock(inode); | |
2655 | flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) | | |
2656 | (flags & F2FS_FL_XFLAG_VISIBLE); | |
2657 | err = __f2fs_ioc_setflags(inode, flags); | |
2658 | inode_unlock(inode); | |
2659 | mnt_drop_write_file(filp); | |
2660 | if (err) | |
2661 | return err; | |
2662 | ||
2663 | err = f2fs_ioc_setproject(filp, fa.fsx_projid); | |
2664 | if (err) | |
2665 | return err; | |
2666 | ||
2667 | return 0; | |
2668 | } | |
e066b83c | 2669 | |
52656e6c JK |
2670 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
2671 | { | |
1f227a3e JK |
2672 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) |
2673 | return -EIO; | |
2674 | ||
52656e6c JK |
2675 | switch (cmd) { |
2676 | case F2FS_IOC_GETFLAGS: | |
2677 | return f2fs_ioc_getflags(filp, arg); | |
2678 | case F2FS_IOC_SETFLAGS: | |
2679 | return f2fs_ioc_setflags(filp, arg); | |
d49f3e89 CY |
2680 | case F2FS_IOC_GETVERSION: |
2681 | return f2fs_ioc_getversion(filp, arg); | |
88b88a66 JK |
2682 | case F2FS_IOC_START_ATOMIC_WRITE: |
2683 | return f2fs_ioc_start_atomic_write(filp); | |
2684 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
2685 | return f2fs_ioc_commit_atomic_write(filp); | |
02a1335f JK |
2686 | case F2FS_IOC_START_VOLATILE_WRITE: |
2687 | return f2fs_ioc_start_volatile_write(filp); | |
1e84371f JK |
2688 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: |
2689 | return f2fs_ioc_release_volatile_write(filp); | |
2690 | case F2FS_IOC_ABORT_VOLATILE_WRITE: | |
2691 | return f2fs_ioc_abort_volatile_write(filp); | |
1abff93d JK |
2692 | case F2FS_IOC_SHUTDOWN: |
2693 | return f2fs_ioc_shutdown(filp, arg); | |
52656e6c JK |
2694 | case FITRIM: |
2695 | return f2fs_ioc_fitrim(filp, arg); | |
f424f664 JK |
2696 | case F2FS_IOC_SET_ENCRYPTION_POLICY: |
2697 | return f2fs_ioc_set_encryption_policy(filp, arg); | |
2698 | case F2FS_IOC_GET_ENCRYPTION_POLICY: | |
2699 | return f2fs_ioc_get_encryption_policy(filp, arg); | |
2700 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: | |
2701 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); | |
c1c1b583 CY |
2702 | case F2FS_IOC_GARBAGE_COLLECT: |
2703 | return f2fs_ioc_gc(filp, arg); | |
34dc77ad JK |
2704 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
2705 | return f2fs_ioc_gc_range(filp, arg); | |
456b88e4 CY |
2706 | case F2FS_IOC_WRITE_CHECKPOINT: |
2707 | return f2fs_ioc_write_checkpoint(filp, arg); | |
d323d005 CY |
2708 | case F2FS_IOC_DEFRAGMENT: |
2709 | return f2fs_ioc_defragment(filp, arg); | |
4dd6f977 JK |
2710 | case F2FS_IOC_MOVE_RANGE: |
2711 | return f2fs_ioc_move_range(filp, arg); | |
e066b83c JK |
2712 | case F2FS_IOC_FLUSH_DEVICE: |
2713 | return f2fs_ioc_flush_device(filp, arg); | |
e65ef207 JK |
2714 | case F2FS_IOC_GET_FEATURES: |
2715 | return f2fs_ioc_get_features(filp, arg); | |
2c1d0305 CY |
2716 | case F2FS_IOC_FSGETXATTR: |
2717 | return f2fs_ioc_fsgetxattr(filp, arg); | |
2718 | case F2FS_IOC_FSSETXATTR: | |
2719 | return f2fs_ioc_fssetxattr(filp, arg); | |
fbfa2cc5 JK |
2720 | default: |
2721 | return -ENOTTY; | |
2722 | } | |
2723 | } | |
2724 | ||
fcc85a4d JK |
2725 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
2726 | { | |
b439b103 JK |
2727 | struct file *file = iocb->ki_filp; |
2728 | struct inode *inode = file_inode(file); | |
9dfa1baf | 2729 | struct blk_plug plug; |
b439b103 | 2730 | ssize_t ret; |
fcc85a4d | 2731 | |
1f227a3e JK |
2732 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) |
2733 | return -EIO; | |
2734 | ||
b439b103 JK |
2735 | inode_lock(inode); |
2736 | ret = generic_write_checks(iocb, from); | |
2737 | if (ret > 0) { | |
dc91de78 | 2738 | int err; |
a7de6086 | 2739 | |
dc91de78 JK |
2740 | if (iov_iter_fault_in_readable(from, iov_iter_count(from))) |
2741 | set_inode_flag(inode, FI_NO_PREALLOC); | |
a7de6086 | 2742 | |
dc91de78 | 2743 | err = f2fs_preallocate_blocks(iocb, from); |
a7de6086 | 2744 | if (err) { |
28cfafb7 | 2745 | clear_inode_flag(inode, FI_NO_PREALLOC); |
a7de6086 JK |
2746 | inode_unlock(inode); |
2747 | return err; | |
9dfa1baf | 2748 | } |
a7de6086 JK |
2749 | blk_start_plug(&plug); |
2750 | ret = __generic_file_write_iter(iocb, from); | |
2751 | blk_finish_plug(&plug); | |
dc91de78 | 2752 | clear_inode_flag(inode, FI_NO_PREALLOC); |
b0af6d49 CY |
2753 | |
2754 | if (ret > 0) | |
2755 | f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret); | |
b439b103 JK |
2756 | } |
2757 | inode_unlock(inode); | |
2758 | ||
e2592217 CH |
2759 | if (ret > 0) |
2760 | ret = generic_write_sync(iocb, ret); | |
b439b103 | 2761 | return ret; |
fcc85a4d JK |
2762 | } |
2763 | ||
e9750824 NJ |
2764 | #ifdef CONFIG_COMPAT |
2765 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
2766 | { | |
2767 | switch (cmd) { | |
2768 | case F2FS_IOC32_GETFLAGS: | |
2769 | cmd = F2FS_IOC_GETFLAGS; | |
2770 | break; | |
2771 | case F2FS_IOC32_SETFLAGS: | |
2772 | cmd = F2FS_IOC_SETFLAGS; | |
2773 | break; | |
04ef4b62 CY |
2774 | case F2FS_IOC32_GETVERSION: |
2775 | cmd = F2FS_IOC_GETVERSION; | |
2776 | break; | |
2777 | case F2FS_IOC_START_ATOMIC_WRITE: | |
2778 | case F2FS_IOC_COMMIT_ATOMIC_WRITE: | |
2779 | case F2FS_IOC_START_VOLATILE_WRITE: | |
2780 | case F2FS_IOC_RELEASE_VOLATILE_WRITE: | |
2781 | case F2FS_IOC_ABORT_VOLATILE_WRITE: | |
2782 | case F2FS_IOC_SHUTDOWN: | |
2783 | case F2FS_IOC_SET_ENCRYPTION_POLICY: | |
2784 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: | |
2785 | case F2FS_IOC_GET_ENCRYPTION_POLICY: | |
2786 | case F2FS_IOC_GARBAGE_COLLECT: | |
34dc77ad | 2787 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
04ef4b62 CY |
2788 | case F2FS_IOC_WRITE_CHECKPOINT: |
2789 | case F2FS_IOC_DEFRAGMENT: | |
4dd6f977 | 2790 | case F2FS_IOC_MOVE_RANGE: |
e066b83c | 2791 | case F2FS_IOC_FLUSH_DEVICE: |
e65ef207 | 2792 | case F2FS_IOC_GET_FEATURES: |
2c1d0305 CY |
2793 | case F2FS_IOC_FSGETXATTR: |
2794 | case F2FS_IOC_FSSETXATTR: | |
4dd6f977 | 2795 | break; |
e9750824 NJ |
2796 | default: |
2797 | return -ENOIOCTLCMD; | |
2798 | } | |
2799 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
2800 | } | |
2801 | #endif | |
2802 | ||
fbfa2cc5 | 2803 | const struct file_operations f2fs_file_operations = { |
267378d4 | 2804 | .llseek = f2fs_llseek, |
aad4f8bb | 2805 | .read_iter = generic_file_read_iter, |
fcc85a4d JK |
2806 | .write_iter = f2fs_file_write_iter, |
2807 | .open = f2fs_file_open, | |
12662234 | 2808 | .release = f2fs_release_file, |
fbfa2cc5 | 2809 | .mmap = f2fs_file_mmap, |
7a10f017 | 2810 | .flush = f2fs_file_flush, |
fbfa2cc5 JK |
2811 | .fsync = f2fs_sync_file, |
2812 | .fallocate = f2fs_fallocate, | |
2813 | .unlocked_ioctl = f2fs_ioctl, | |
e9750824 NJ |
2814 | #ifdef CONFIG_COMPAT |
2815 | .compat_ioctl = f2fs_compat_ioctl, | |
2816 | #endif | |
fbfa2cc5 | 2817 | .splice_read = generic_file_splice_read, |
8d020765 | 2818 | .splice_write = iter_file_splice_write, |
fbfa2cc5 | 2819 | }; |