1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/file.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/buffer_head.h>
12 #include <linux/writeback.h>
13 #include <linux/blkdev.h>
14 #include <linux/falloc.h>
15 #include <linux/types.h>
16 #include <linux/compat.h>
17 #include <linux/uaccess.h>
18 #include <linux/mount.h>
19 #include <linux/pagevec.h>
20 #include <linux/uio.h>
21 #include <linux/uuid.h>
22 #include <linux/file.h>
23 #include <linux/nls.h>
24 #include <linux/sched/signal.h>
25 #include <linux/fileattr.h>
26 #include <linux/fadvise.h>
27 #include <linux/iomap.h>
28
29 #include "f2fs.h"
30 #include "node.h"
31 #include "segment.h"
32 #include "xattr.h"
33 #include "acl.h"
34 #include "gc.h"
35 #include "iostat.h"
36 #include <trace/events/f2fs.h>
37 #include <uapi/linux/f2fs.h>
38
f2fs_zero_post_eof_page(struct inode * inode,loff_t new_size)39 static void f2fs_zero_post_eof_page(struct inode *inode, loff_t new_size)
40 {
41 loff_t old_size = i_size_read(inode);
42
43 if (old_size >= new_size)
44 return;
45
46 /* zero or drop pages only in range of [old_size, new_size] */
47 truncate_pagecache(inode, old_size);
48 }
49
f2fs_filemap_fault(struct vm_fault * vmf)50 static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
51 {
52 struct inode *inode = file_inode(vmf->vma->vm_file);
53 vm_flags_t flags = vmf->vma->vm_flags;
54 vm_fault_t ret;
55
56 ret = filemap_fault(vmf);
57 if (ret & VM_FAULT_LOCKED)
58 f2fs_update_iostat(F2FS_I_SB(inode), inode,
59 APP_MAPPED_READ_IO, F2FS_BLKSIZE);
60
61 trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
62
63 return ret;
64 }
65
f2fs_vm_page_mkwrite(struct vm_fault * vmf)66 static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
67 {
68 struct folio *folio = page_folio(vmf->page);
69 struct inode *inode = file_inode(vmf->vma->vm_file);
70 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
71 struct dnode_of_data dn;
72 bool need_alloc = !f2fs_is_pinned_file(inode);
73 int err = 0;
74 vm_fault_t ret;
75
76 if (unlikely(IS_IMMUTABLE(inode)))
77 return VM_FAULT_SIGBUS;
78
79 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
80 err = -EIO;
81 goto out;
82 }
83
84 if (unlikely(f2fs_cp_error(sbi))) {
85 err = -EIO;
86 goto out;
87 }
88
89 if (!f2fs_is_checkpoint_ready(sbi)) {
90 err = -ENOSPC;
91 goto out;
92 }
93
94 err = f2fs_convert_inline_inode(inode);
95 if (err)
96 goto out;
97
98 #ifdef CONFIG_F2FS_FS_COMPRESSION
99 if (f2fs_compressed_file(inode)) {
100 int ret = f2fs_is_compressed_cluster(inode, folio->index);
101
102 if (ret < 0) {
103 err = ret;
104 goto out;
105 } else if (ret) {
106 need_alloc = false;
107 }
108 }
109 #endif
110 /* should do out of any locked page */
111 if (need_alloc)
112 f2fs_balance_fs(sbi, true);
113
114 sb_start_pagefault(inode->i_sb);
115
116 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
117
118 filemap_invalidate_lock(inode->i_mapping);
119 f2fs_zero_post_eof_page(inode, (folio->index + 1) << PAGE_SHIFT);
120 filemap_invalidate_unlock(inode->i_mapping);
121
122 file_update_time(vmf->vma->vm_file);
123 filemap_invalidate_lock_shared(inode->i_mapping);
124
125 folio_lock(folio);
126 if (unlikely(folio->mapping != inode->i_mapping ||
127 folio_pos(folio) > i_size_read(inode) ||
128 !folio_test_uptodate(folio))) {
129 folio_unlock(folio);
130 err = -EFAULT;
131 goto out_sem;
132 }
133
134 set_new_dnode(&dn, inode, NULL, NULL, 0);
135 if (need_alloc) {
136 /* block allocation */
137 err = f2fs_get_block_locked(&dn, folio->index);
138 } else {
139 err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
140 f2fs_put_dnode(&dn);
141 if (f2fs_is_pinned_file(inode) &&
142 !__is_valid_data_blkaddr(dn.data_blkaddr))
143 err = -EIO;
144 }
145
146 if (err) {
147 folio_unlock(folio);
148 goto out_sem;
149 }
150
151 f2fs_wait_on_page_writeback(folio_page(folio, 0), DATA, false, true);
152
153 /* wait for GCed page writeback via META_MAPPING */
154 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
155
156 /*
157 * check to see if the page is mapped already (no holes)
158 */
159 if (folio_test_mappedtodisk(folio))
160 goto out_sem;
161
162 /* page is wholly or partially inside EOF */
163 if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
164 i_size_read(inode)) {
165 loff_t offset;
166
167 offset = i_size_read(inode) & ~PAGE_MASK;
168 folio_zero_segment(folio, offset, folio_size(folio));
169 }
170 folio_mark_dirty(folio);
171
172 f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
173 f2fs_update_time(sbi, REQ_TIME);
174
175 out_sem:
176 filemap_invalidate_unlock_shared(inode->i_mapping);
177
178 sb_end_pagefault(inode->i_sb);
179 out:
180 ret = vmf_fs_error(err);
181
182 trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
183 return ret;
184 }
185
186 static const struct vm_operations_struct f2fs_file_vm_ops = {
187 .fault = f2fs_filemap_fault,
188 .map_pages = filemap_map_pages,
189 .page_mkwrite = f2fs_vm_page_mkwrite,
190 };
191
get_parent_ino(struct inode * inode,nid_t * pino)192 static int get_parent_ino(struct inode *inode, nid_t *pino)
193 {
194 struct dentry *dentry;
195
196 /*
197 * Make sure to get the non-deleted alias. The alias associated with
198 * the open file descriptor being fsync()'ed may be deleted already.
199 */
200 dentry = d_find_alias(inode);
201 if (!dentry)
202 return 0;
203
204 *pino = parent_ino(dentry);
205 dput(dentry);
206 return 1;
207 }
208
need_do_checkpoint(struct inode * inode)209 static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
210 {
211 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
212 enum cp_reason_type cp_reason = CP_NO_NEEDED;
213
214 if (!S_ISREG(inode->i_mode))
215 cp_reason = CP_NON_REGULAR;
216 else if (f2fs_compressed_file(inode))
217 cp_reason = CP_COMPRESSED;
218 else if (inode->i_nlink != 1)
219 cp_reason = CP_HARDLINK;
220 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
221 cp_reason = CP_SB_NEED_CP;
222 else if (file_wrong_pino(inode))
223 cp_reason = CP_WRONG_PINO;
224 else if (!f2fs_space_for_roll_forward(sbi))
225 cp_reason = CP_NO_SPC_ROLL;
226 else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
227 cp_reason = CP_NODE_NEED_CP;
228 else if (test_opt(sbi, FASTBOOT))
229 cp_reason = CP_FASTBOOT_MODE;
230 else if (F2FS_OPTION(sbi).active_logs == 2)
231 cp_reason = CP_SPEC_LOG_NUM;
232 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
233 f2fs_need_dentry_mark(sbi, inode->i_ino) &&
234 f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
235 TRANS_DIR_INO))
236 cp_reason = CP_RECOVER_DIR;
237 else if (f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
238 XATTR_DIR_INO))
239 cp_reason = CP_XATTR_DIR;
240
241 return cp_reason;
242 }
243
need_inode_page_update(struct f2fs_sb_info * sbi,nid_t ino)244 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
245 {
246 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
247 bool ret = false;
248 /* But we need to avoid that there are some inode updates */
249 if ((i && PageDirty(i)) || f2fs_need_inode_block_update(sbi, ino))
250 ret = true;
251 f2fs_put_page(i, 0);
252 return ret;
253 }
254
try_to_fix_pino(struct inode * inode)255 static void try_to_fix_pino(struct inode *inode)
256 {
257 struct f2fs_inode_info *fi = F2FS_I(inode);
258 nid_t pino;
259
260 f2fs_down_write(&fi->i_sem);
261 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
262 get_parent_ino(inode, &pino)) {
263 f2fs_i_pino_write(inode, pino);
264 file_got_pino(inode);
265 }
266 f2fs_up_write(&fi->i_sem);
267 }
268
f2fs_do_sync_file(struct file * file,loff_t start,loff_t end,int datasync,bool atomic)269 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
270 int datasync, bool atomic)
271 {
272 struct inode *inode = file->f_mapping->host;
273 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
274 nid_t ino = inode->i_ino;
275 int ret = 0;
276 enum cp_reason_type cp_reason = 0;
277 struct writeback_control wbc = {
278 .sync_mode = WB_SYNC_ALL,
279 .nr_to_write = LONG_MAX,
280 .for_reclaim = 0,
281 };
282 unsigned int seq_id = 0;
283
284 if (unlikely(f2fs_readonly(inode->i_sb)))
285 return 0;
286
287 trace_f2fs_sync_file_enter(inode);
288
289 if (S_ISDIR(inode->i_mode))
290 goto go_write;
291
292 /* if fdatasync is triggered, let's do in-place-update */
293 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
294 set_inode_flag(inode, FI_NEED_IPU);
295 ret = file_write_and_wait_range(file, start, end);
296 clear_inode_flag(inode, FI_NEED_IPU);
297
298 if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
299 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
300 return ret;
301 }
302
303 /* if the inode is dirty, let's recover all the time */
304 if (!f2fs_skip_inode_update(inode, datasync)) {
305 f2fs_write_inode(inode, NULL);
306 goto go_write;
307 }
308
309 /*
310 * if there is no written data, don't waste time to write recovery info.
311 */
312 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
313 !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
314
315 /* it may call write_inode just prior to fsync */
316 if (need_inode_page_update(sbi, ino))
317 goto go_write;
318
319 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
320 f2fs_exist_written_data(sbi, ino, UPDATE_INO))
321 goto flush_out;
322 goto out;
323 } else {
324 /*
325 * for OPU case, during fsync(), node can be persisted before
326 * data when lower device doesn't support write barrier, result
327 * in data corruption after SPO.
328 * So for strict fsync mode, force to use atomic write semantics
329 * to keep write order in between data/node and last node to
330 * avoid potential data corruption.
331 */
332 if (F2FS_OPTION(sbi).fsync_mode ==
333 FSYNC_MODE_STRICT && !atomic)
334 atomic = true;
335 }
336 go_write:
337 /*
338 * Both of fdatasync() and fsync() are able to be recovered from
339 * sudden-power-off.
340 */
341 f2fs_down_read(&F2FS_I(inode)->i_sem);
342 cp_reason = need_do_checkpoint(inode);
343 f2fs_up_read(&F2FS_I(inode)->i_sem);
344
345 if (cp_reason) {
346 /* all the dirty node pages should be flushed for POR */
347 ret = f2fs_sync_fs(inode->i_sb, 1);
348
349 /*
350 * We've secured consistency through sync_fs. Following pino
351 * will be used only for fsynced inodes after checkpoint.
352 */
353 try_to_fix_pino(inode);
354 clear_inode_flag(inode, FI_APPEND_WRITE);
355 clear_inode_flag(inode, FI_UPDATE_WRITE);
356 goto out;
357 }
358 sync_nodes:
359 atomic_inc(&sbi->wb_sync_req[NODE]);
360 ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
361 atomic_dec(&sbi->wb_sync_req[NODE]);
362 if (ret)
363 goto out;
364
365 /* if cp_error was enabled, we should avoid infinite loop */
366 if (unlikely(f2fs_cp_error(sbi))) {
367 ret = -EIO;
368 goto out;
369 }
370
371 if (f2fs_need_inode_block_update(sbi, ino)) {
372 f2fs_mark_inode_dirty_sync(inode, true);
373 f2fs_write_inode(inode, NULL);
374 goto sync_nodes;
375 }
376
377 /*
378 * If it's atomic_write, it's just fine to keep write ordering. So
379 * here we don't need to wait for node write completion, since we use
380 * node chain which serializes node blocks. If one of node writes are
381 * reordered, we can see simply broken chain, resulting in stopping
382 * roll-forward recovery. It means we'll recover all or none node blocks
383 * given fsync mark.
384 */
385 if (!atomic) {
386 ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
387 if (ret)
388 goto out;
389 }
390
391 /* once recovery info is written, don't need to tack this */
392 f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
393 clear_inode_flag(inode, FI_APPEND_WRITE);
394 flush_out:
395 if ((!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER) ||
396 (atomic && !test_opt(sbi, NOBARRIER) && f2fs_sb_has_blkzoned(sbi)))
397 ret = f2fs_issue_flush(sbi, inode->i_ino);
398 if (!ret) {
399 f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
400 clear_inode_flag(inode, FI_UPDATE_WRITE);
401 f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
402 }
403 f2fs_update_time(sbi, REQ_TIME);
404 out:
405 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
406 return ret;
407 }
408
f2fs_sync_file(struct file * file,loff_t start,loff_t end,int datasync)409 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
410 {
411 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
412 return -EIO;
413 return f2fs_do_sync_file(file, start, end, datasync, false);
414 }
415
__found_offset(struct address_space * mapping,block_t blkaddr,pgoff_t index,int whence)416 static bool __found_offset(struct address_space *mapping, block_t blkaddr,
417 pgoff_t index, int whence)
418 {
419 switch (whence) {
420 case SEEK_DATA:
421 if (__is_valid_data_blkaddr(blkaddr))
422 return true;
423 if (blkaddr == NEW_ADDR &&
424 xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
425 return true;
426 break;
427 case SEEK_HOLE:
428 if (blkaddr == NULL_ADDR)
429 return true;
430 break;
431 }
432 return false;
433 }
434
f2fs_seek_block(struct file * file,loff_t offset,int whence)435 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
436 {
437 struct inode *inode = file->f_mapping->host;
438 loff_t maxbytes = inode->i_sb->s_maxbytes;
439 struct dnode_of_data dn;
440 pgoff_t pgofs, end_offset;
441 loff_t data_ofs = offset;
442 loff_t isize;
443 int err = 0;
444
445 inode_lock(inode);
446
447 isize = i_size_read(inode);
448 if (offset >= isize)
449 goto fail;
450
451 /* handle inline data case */
452 if (f2fs_has_inline_data(inode)) {
453 if (whence == SEEK_HOLE) {
454 data_ofs = isize;
455 goto found;
456 } else if (whence == SEEK_DATA) {
457 data_ofs = offset;
458 goto found;
459 }
460 }
461
462 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
463
464 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
465 set_new_dnode(&dn, inode, NULL, NULL, 0);
466 err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
467 if (err && err != -ENOENT) {
468 goto fail;
469 } else if (err == -ENOENT) {
470 /* direct node does not exists */
471 if (whence == SEEK_DATA) {
472 pgofs = f2fs_get_next_page_offset(&dn, pgofs);
473 continue;
474 } else {
475 goto found;
476 }
477 }
478
479 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
480
481 /* find data/hole in dnode block */
482 for (; dn.ofs_in_node < end_offset;
483 dn.ofs_in_node++, pgofs++,
484 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
485 block_t blkaddr;
486
487 blkaddr = f2fs_data_blkaddr(&dn);
488
489 if (__is_valid_data_blkaddr(blkaddr) &&
490 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
491 blkaddr, DATA_GENERIC_ENHANCE)) {
492 f2fs_put_dnode(&dn);
493 goto fail;
494 }
495
496 if (__found_offset(file->f_mapping, blkaddr,
497 pgofs, whence)) {
498 f2fs_put_dnode(&dn);
499 goto found;
500 }
501 }
502 f2fs_put_dnode(&dn);
503 }
504
505 if (whence == SEEK_DATA)
506 goto fail;
507 found:
508 if (whence == SEEK_HOLE && data_ofs > isize)
509 data_ofs = isize;
510 inode_unlock(inode);
511 return vfs_setpos(file, data_ofs, maxbytes);
512 fail:
513 inode_unlock(inode);
514 return -ENXIO;
515 }
516
f2fs_llseek(struct file * file,loff_t offset,int whence)517 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
518 {
519 struct inode *inode = file->f_mapping->host;
520 loff_t maxbytes = inode->i_sb->s_maxbytes;
521
522 if (f2fs_compressed_file(inode))
523 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
524
525 switch (whence) {
526 case SEEK_SET:
527 case SEEK_CUR:
528 case SEEK_END:
529 return generic_file_llseek_size(file, offset, whence,
530 maxbytes, i_size_read(inode));
531 case SEEK_DATA:
532 case SEEK_HOLE:
533 if (offset < 0)
534 return -ENXIO;
535 return f2fs_seek_block(file, offset, whence);
536 }
537
538 return -EINVAL;
539 }
540
f2fs_file_mmap(struct file * file,struct vm_area_struct * vma)541 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
542 {
543 struct inode *inode = file_inode(file);
544
545 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
546 return -EIO;
547
548 if (!f2fs_is_compress_backend_ready(inode))
549 return -EOPNOTSUPP;
550
551 file_accessed(file);
552 vma->vm_ops = &f2fs_file_vm_ops;
553
554 f2fs_down_read(&F2FS_I(inode)->i_sem);
555 set_inode_flag(inode, FI_MMAP_FILE);
556 f2fs_up_read(&F2FS_I(inode)->i_sem);
557
558 return 0;
559 }
560
finish_preallocate_blocks(struct inode * inode)561 static int finish_preallocate_blocks(struct inode *inode)
562 {
563 int ret;
564
565 inode_lock(inode);
566 if (is_inode_flag_set(inode, FI_OPENED_FILE)) {
567 inode_unlock(inode);
568 return 0;
569 }
570
571 if (!file_should_truncate(inode)) {
572 set_inode_flag(inode, FI_OPENED_FILE);
573 inode_unlock(inode);
574 return 0;
575 }
576
577 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
578 filemap_invalidate_lock(inode->i_mapping);
579
580 truncate_setsize(inode, i_size_read(inode));
581 ret = f2fs_truncate(inode);
582
583 filemap_invalidate_unlock(inode->i_mapping);
584 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
585
586 if (!ret)
587 set_inode_flag(inode, FI_OPENED_FILE);
588
589 inode_unlock(inode);
590 if (ret)
591 return ret;
592
593 file_dont_truncate(inode);
594 return 0;
595 }
596
f2fs_file_open(struct inode * inode,struct file * filp)597 static int f2fs_file_open(struct inode *inode, struct file *filp)
598 {
599 int err = fscrypt_file_open(inode, filp);
600
601 if (err)
602 return err;
603
604 if (!f2fs_is_compress_backend_ready(inode))
605 return -EOPNOTSUPP;
606
607 err = fsverity_file_open(inode, filp);
608 if (err)
609 return err;
610
611 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
612 filp->f_mode |= FMODE_CAN_ODIRECT;
613
614 err = dquot_file_open(inode, filp);
615 if (err)
616 return err;
617
618 return finish_preallocate_blocks(inode);
619 }
620
f2fs_truncate_data_blocks_range(struct dnode_of_data * dn,int count)621 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
622 {
623 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
624 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
625 __le32 *addr;
626 bool compressed_cluster = false;
627 int cluster_index = 0, valid_blocks = 0;
628 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
629 bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
630
631 addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
632
633 /* Assumption: truncation starts with cluster */
634 for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
635 block_t blkaddr = le32_to_cpu(*addr);
636
637 if (f2fs_compressed_file(dn->inode) &&
638 !(cluster_index & (cluster_size - 1))) {
639 if (compressed_cluster)
640 f2fs_i_compr_blocks_update(dn->inode,
641 valid_blocks, false);
642 compressed_cluster = (blkaddr == COMPRESS_ADDR);
643 valid_blocks = 0;
644 }
645
646 if (blkaddr == NULL_ADDR)
647 continue;
648
649 f2fs_set_data_blkaddr(dn, NULL_ADDR);
650
651 if (__is_valid_data_blkaddr(blkaddr)) {
652 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
653 DATA_GENERIC_ENHANCE))
654 continue;
655 if (compressed_cluster)
656 valid_blocks++;
657 }
658
659 f2fs_invalidate_blocks(sbi, blkaddr);
660
661 if (!released || blkaddr != COMPRESS_ADDR)
662 nr_free++;
663 }
664
665 if (compressed_cluster)
666 f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
667
668 if (nr_free) {
669 pgoff_t fofs;
670 /*
671 * once we invalidate valid blkaddr in range [ofs, ofs + count],
672 * we will invalidate all blkaddr in the whole range.
673 */
674 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page),
675 dn->inode) + ofs;
676 f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
677 f2fs_update_age_extent_cache_range(dn, fofs, len);
678 dec_valid_block_count(sbi, dn->inode, nr_free);
679 }
680 dn->ofs_in_node = ofs;
681
682 f2fs_update_time(sbi, REQ_TIME);
683 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
684 dn->ofs_in_node, nr_free);
685 }
686
truncate_partial_data_page(struct inode * inode,u64 from,bool cache_only)687 static int truncate_partial_data_page(struct inode *inode, u64 from,
688 bool cache_only)
689 {
690 loff_t offset = from & (PAGE_SIZE - 1);
691 pgoff_t index = from >> PAGE_SHIFT;
692 struct address_space *mapping = inode->i_mapping;
693 struct page *page;
694
695 if (!offset && !cache_only)
696 return 0;
697
698 if (cache_only) {
699 page = find_lock_page(mapping, index);
700 if (page && PageUptodate(page))
701 goto truncate_out;
702 f2fs_put_page(page, 1);
703 return 0;
704 }
705
706 page = f2fs_get_lock_data_page(inode, index, true);
707 if (IS_ERR(page))
708 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
709 truncate_out:
710 f2fs_wait_on_page_writeback(page, DATA, true, true);
711 zero_user(page, offset, PAGE_SIZE - offset);
712
713 /* An encrypted inode should have a key and truncate the last page. */
714 f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
715 if (!cache_only)
716 set_page_dirty(page);
717 f2fs_put_page(page, 1);
718 return 0;
719 }
720
f2fs_do_truncate_blocks(struct inode * inode,u64 from,bool lock)721 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
722 {
723 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
724 struct dnode_of_data dn;
725 pgoff_t free_from;
726 int count = 0, err = 0;
727 struct page *ipage;
728 bool truncate_page = false;
729
730 trace_f2fs_truncate_blocks_enter(inode, from);
731
732 free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
733
734 if (free_from >= max_file_blocks(inode))
735 goto free_partial;
736
737 if (lock)
738 f2fs_lock_op(sbi);
739
740 ipage = f2fs_get_node_page(sbi, inode->i_ino);
741 if (IS_ERR(ipage)) {
742 err = PTR_ERR(ipage);
743 goto out;
744 }
745
746 if (f2fs_has_inline_data(inode)) {
747 f2fs_truncate_inline_inode(inode, ipage, from);
748 f2fs_put_page(ipage, 1);
749 truncate_page = true;
750 goto out;
751 }
752
753 set_new_dnode(&dn, inode, ipage, NULL, 0);
754 err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
755 if (err) {
756 if (err == -ENOENT)
757 goto free_next;
758 goto out;
759 }
760
761 count = ADDRS_PER_PAGE(dn.node_page, inode);
762
763 count -= dn.ofs_in_node;
764 f2fs_bug_on(sbi, count < 0);
765
766 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
767 f2fs_truncate_data_blocks_range(&dn, count);
768 free_from += count;
769 }
770
771 f2fs_put_dnode(&dn);
772 free_next:
773 err = f2fs_truncate_inode_blocks(inode, free_from);
774 out:
775 if (lock)
776 f2fs_unlock_op(sbi);
777 free_partial:
778 /* lastly zero out the first data page */
779 if (!err)
780 err = truncate_partial_data_page(inode, from, truncate_page);
781
782 trace_f2fs_truncate_blocks_exit(inode, err);
783 return err;
784 }
785
f2fs_truncate_blocks(struct inode * inode,u64 from,bool lock)786 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
787 {
788 u64 free_from = from;
789 int err;
790
791 #ifdef CONFIG_F2FS_FS_COMPRESSION
792 /*
793 * for compressed file, only support cluster size
794 * aligned truncation.
795 */
796 if (f2fs_compressed_file(inode))
797 free_from = round_up(from,
798 F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
799 #endif
800
801 err = f2fs_do_truncate_blocks(inode, free_from, lock);
802 if (err)
803 return err;
804
805 #ifdef CONFIG_F2FS_FS_COMPRESSION
806 /*
807 * For compressed file, after release compress blocks, don't allow write
808 * direct, but we should allow write direct after truncate to zero.
809 */
810 if (f2fs_compressed_file(inode) && !free_from
811 && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
812 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
813
814 if (from != free_from) {
815 err = f2fs_truncate_partial_cluster(inode, from, lock);
816 if (err)
817 return err;
818 }
819 #endif
820
821 return 0;
822 }
823
f2fs_truncate(struct inode * inode)824 int f2fs_truncate(struct inode *inode)
825 {
826 int err;
827
828 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
829 return -EIO;
830
831 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
832 S_ISLNK(inode->i_mode)))
833 return 0;
834
835 trace_f2fs_truncate(inode);
836
837 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
838 return -EIO;
839
840 err = f2fs_dquot_initialize(inode);
841 if (err)
842 return err;
843
844 /* we should check inline_data size */
845 if (!f2fs_may_inline_data(inode)) {
846 err = f2fs_convert_inline_inode(inode);
847 if (err)
848 return err;
849 }
850
851 err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
852 if (err)
853 return err;
854
855 inode->i_mtime = inode_set_ctime_current(inode);
856 f2fs_mark_inode_dirty_sync(inode, false);
857 return 0;
858 }
859
f2fs_force_buffered_io(struct inode * inode,int rw)860 static bool f2fs_force_buffered_io(struct inode *inode, int rw)
861 {
862 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
863
864 if (!fscrypt_dio_supported(inode))
865 return true;
866 if (fsverity_active(inode))
867 return true;
868 if (f2fs_compressed_file(inode))
869 return true;
870 /*
871 * only force direct read to use buffered IO, for direct write,
872 * it expects inline data conversion before committing IO.
873 */
874 if (f2fs_has_inline_data(inode) && rw == READ)
875 return true;
876
877 /* disallow direct IO if any of devices has unaligned blksize */
878 if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
879 return true;
880 /*
881 * for blkzoned device, fallback direct IO to buffered IO, so
882 * all IOs can be serialized by log-structured write.
883 */
884 if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE))
885 return true;
886 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
887 return true;
888
889 return false;
890 }
891
f2fs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)892 int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
893 struct kstat *stat, u32 request_mask, unsigned int query_flags)
894 {
895 struct inode *inode = d_inode(path->dentry);
896 struct f2fs_inode_info *fi = F2FS_I(inode);
897 struct f2fs_inode *ri = NULL;
898 unsigned int flags;
899
900 if (f2fs_has_extra_attr(inode) &&
901 f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
902 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
903 stat->result_mask |= STATX_BTIME;
904 stat->btime.tv_sec = fi->i_crtime.tv_sec;
905 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
906 }
907
908 /*
909 * Return the DIO alignment restrictions if requested. We only return
910 * this information when requested, since on encrypted files it might
911 * take a fair bit of work to get if the file wasn't opened recently.
912 *
913 * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
914 * cannot represent that, so in that case we report no DIO support.
915 */
916 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
917 unsigned int bsize = i_blocksize(inode);
918
919 stat->result_mask |= STATX_DIOALIGN;
920 if (!f2fs_force_buffered_io(inode, WRITE)) {
921 stat->dio_mem_align = bsize;
922 stat->dio_offset_align = bsize;
923 }
924 }
925
926 flags = fi->i_flags;
927 if (flags & F2FS_COMPR_FL)
928 stat->attributes |= STATX_ATTR_COMPRESSED;
929 if (flags & F2FS_APPEND_FL)
930 stat->attributes |= STATX_ATTR_APPEND;
931 if (IS_ENCRYPTED(inode))
932 stat->attributes |= STATX_ATTR_ENCRYPTED;
933 if (flags & F2FS_IMMUTABLE_FL)
934 stat->attributes |= STATX_ATTR_IMMUTABLE;
935 if (flags & F2FS_NODUMP_FL)
936 stat->attributes |= STATX_ATTR_NODUMP;
937 if (IS_VERITY(inode))
938 stat->attributes |= STATX_ATTR_VERITY;
939
940 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
941 STATX_ATTR_APPEND |
942 STATX_ATTR_ENCRYPTED |
943 STATX_ATTR_IMMUTABLE |
944 STATX_ATTR_NODUMP |
945 STATX_ATTR_VERITY);
946
947 generic_fillattr(idmap, request_mask, inode, stat);
948
949 /* we need to show initial sectors used for inline_data/dentries */
950 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
951 f2fs_has_inline_dentry(inode))
952 stat->blocks += (stat->size + 511) >> 9;
953
954 return 0;
955 }
956
957 #ifdef CONFIG_F2FS_FS_POSIX_ACL
__setattr_copy(struct mnt_idmap * idmap,struct inode * inode,const struct iattr * attr)958 static void __setattr_copy(struct mnt_idmap *idmap,
959 struct inode *inode, const struct iattr *attr)
960 {
961 unsigned int ia_valid = attr->ia_valid;
962
963 i_uid_update(idmap, attr, inode);
964 i_gid_update(idmap, attr, inode);
965 if (ia_valid & ATTR_ATIME)
966 inode->i_atime = attr->ia_atime;
967 if (ia_valid & ATTR_MTIME)
968 inode->i_mtime = attr->ia_mtime;
969 if (ia_valid & ATTR_CTIME)
970 inode_set_ctime_to_ts(inode, attr->ia_ctime);
971 if (ia_valid & ATTR_MODE) {
972 umode_t mode = attr->ia_mode;
973 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
974
975 if (!vfsgid_in_group_p(vfsgid) &&
976 !capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
977 mode &= ~S_ISGID;
978 set_acl_inode(inode, mode);
979 }
980 }
981 #else
982 #define __setattr_copy setattr_copy
983 #endif
984
f2fs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)985 int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
986 struct iattr *attr)
987 {
988 struct inode *inode = d_inode(dentry);
989 int err;
990
991 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
992 return -EIO;
993
994 if (unlikely(IS_IMMUTABLE(inode)))
995 return -EPERM;
996
997 if (unlikely(IS_APPEND(inode) &&
998 (attr->ia_valid & (ATTR_MODE | ATTR_UID |
999 ATTR_GID | ATTR_TIMES_SET))))
1000 return -EPERM;
1001
1002 if ((attr->ia_valid & ATTR_SIZE)) {
1003 if (!f2fs_is_compress_backend_ready(inode))
1004 return -EOPNOTSUPP;
1005 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) &&
1006 !IS_ALIGNED(attr->ia_size,
1007 F2FS_BLK_TO_BYTES(F2FS_I(inode)->i_cluster_size)))
1008 return -EINVAL;
1009 }
1010
1011 err = setattr_prepare(idmap, dentry, attr);
1012 if (err)
1013 return err;
1014
1015 err = fscrypt_prepare_setattr(dentry, attr);
1016 if (err)
1017 return err;
1018
1019 err = fsverity_prepare_setattr(dentry, attr);
1020 if (err)
1021 return err;
1022
1023 if (is_quota_modification(idmap, inode, attr)) {
1024 err = f2fs_dquot_initialize(inode);
1025 if (err)
1026 return err;
1027 }
1028 if (i_uid_needs_update(idmap, attr, inode) ||
1029 i_gid_needs_update(idmap, attr, inode)) {
1030 f2fs_lock_op(F2FS_I_SB(inode));
1031 err = dquot_transfer(idmap, inode, attr);
1032 if (err) {
1033 set_sbi_flag(F2FS_I_SB(inode),
1034 SBI_QUOTA_NEED_REPAIR);
1035 f2fs_unlock_op(F2FS_I_SB(inode));
1036 return err;
1037 }
1038 /*
1039 * update uid/gid under lock_op(), so that dquot and inode can
1040 * be updated atomically.
1041 */
1042 i_uid_update(idmap, attr, inode);
1043 i_gid_update(idmap, attr, inode);
1044 f2fs_mark_inode_dirty_sync(inode, true);
1045 f2fs_unlock_op(F2FS_I_SB(inode));
1046 }
1047
1048 if (attr->ia_valid & ATTR_SIZE) {
1049 loff_t old_size = i_size_read(inode);
1050
1051 if (attr->ia_size > MAX_INLINE_DATA(inode)) {
1052 /*
1053 * should convert inline inode before i_size_write to
1054 * keep smaller than inline_data size with inline flag.
1055 */
1056 err = f2fs_convert_inline_inode(inode);
1057 if (err)
1058 return err;
1059 }
1060
1061 /*
1062 * wait for inflight dio, blocks should be removed after
1063 * IO completion.
1064 */
1065 if (attr->ia_size < old_size)
1066 inode_dio_wait(inode);
1067
1068 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1069 filemap_invalidate_lock(inode->i_mapping);
1070
1071 if (attr->ia_size > old_size)
1072 f2fs_zero_post_eof_page(inode, attr->ia_size);
1073 truncate_setsize(inode, attr->ia_size);
1074
1075 if (attr->ia_size <= old_size)
1076 err = f2fs_truncate(inode);
1077 /*
1078 * do not trim all blocks after i_size if target size is
1079 * larger than i_size.
1080 */
1081 filemap_invalidate_unlock(inode->i_mapping);
1082 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1083 if (err)
1084 return err;
1085
1086 spin_lock(&F2FS_I(inode)->i_size_lock);
1087 inode->i_mtime = inode_set_ctime_current(inode);
1088 F2FS_I(inode)->last_disk_size = i_size_read(inode);
1089 spin_unlock(&F2FS_I(inode)->i_size_lock);
1090 }
1091
1092 __setattr_copy(idmap, inode, attr);
1093
1094 if (attr->ia_valid & ATTR_MODE) {
1095 err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
1096
1097 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
1098 if (!err)
1099 inode->i_mode = F2FS_I(inode)->i_acl_mode;
1100 clear_inode_flag(inode, FI_ACL_MODE);
1101 }
1102 }
1103
1104 /* file size may changed here */
1105 f2fs_mark_inode_dirty_sync(inode, true);
1106
1107 /* inode change will produce dirty node pages flushed by checkpoint */
1108 f2fs_balance_fs(F2FS_I_SB(inode), true);
1109
1110 return err;
1111 }
1112
1113 const struct inode_operations f2fs_file_inode_operations = {
1114 .getattr = f2fs_getattr,
1115 .setattr = f2fs_setattr,
1116 .get_inode_acl = f2fs_get_acl,
1117 .set_acl = f2fs_set_acl,
1118 .listxattr = f2fs_listxattr,
1119 .fiemap = f2fs_fiemap,
1120 .fileattr_get = f2fs_fileattr_get,
1121 .fileattr_set = f2fs_fileattr_set,
1122 };
1123
fill_zero(struct inode * inode,pgoff_t index,loff_t start,loff_t len)1124 static int fill_zero(struct inode *inode, pgoff_t index,
1125 loff_t start, loff_t len)
1126 {
1127 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1128 struct page *page;
1129
1130 if (!len)
1131 return 0;
1132
1133 f2fs_balance_fs(sbi, true);
1134
1135 f2fs_lock_op(sbi);
1136 page = f2fs_get_new_data_page(inode, NULL, index, false);
1137 f2fs_unlock_op(sbi);
1138
1139 if (IS_ERR(page))
1140 return PTR_ERR(page);
1141
1142 f2fs_wait_on_page_writeback(page, DATA, true, true);
1143 zero_user(page, start, len);
1144 set_page_dirty(page);
1145 f2fs_put_page(page, 1);
1146 return 0;
1147 }
1148
f2fs_truncate_hole(struct inode * inode,pgoff_t pg_start,pgoff_t pg_end)1149 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
1150 {
1151 int err;
1152
1153 while (pg_start < pg_end) {
1154 struct dnode_of_data dn;
1155 pgoff_t end_offset, count;
1156
1157 set_new_dnode(&dn, inode, NULL, NULL, 0);
1158 err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
1159 if (err) {
1160 if (err == -ENOENT) {
1161 pg_start = f2fs_get_next_page_offset(&dn,
1162 pg_start);
1163 continue;
1164 }
1165 return err;
1166 }
1167
1168 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1169 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
1170
1171 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
1172
1173 f2fs_truncate_data_blocks_range(&dn, count);
1174 f2fs_put_dnode(&dn);
1175
1176 pg_start += count;
1177 }
1178 return 0;
1179 }
1180
f2fs_punch_hole(struct inode * inode,loff_t offset,loff_t len)1181 static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1182 {
1183 pgoff_t pg_start, pg_end;
1184 loff_t off_start, off_end;
1185 int ret;
1186
1187 ret = f2fs_convert_inline_inode(inode);
1188 if (ret)
1189 return ret;
1190
1191 filemap_invalidate_lock(inode->i_mapping);
1192 f2fs_zero_post_eof_page(inode, offset + len);
1193 filemap_invalidate_unlock(inode->i_mapping);
1194
1195 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1196 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1197
1198 off_start = offset & (PAGE_SIZE - 1);
1199 off_end = (offset + len) & (PAGE_SIZE - 1);
1200
1201 if (pg_start == pg_end) {
1202 ret = fill_zero(inode, pg_start, off_start,
1203 off_end - off_start);
1204 if (ret)
1205 return ret;
1206 } else {
1207 if (off_start) {
1208 ret = fill_zero(inode, pg_start++, off_start,
1209 PAGE_SIZE - off_start);
1210 if (ret)
1211 return ret;
1212 }
1213 if (off_end) {
1214 ret = fill_zero(inode, pg_end, 0, off_end);
1215 if (ret)
1216 return ret;
1217 }
1218
1219 if (pg_start < pg_end) {
1220 loff_t blk_start, blk_end;
1221 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1222
1223 f2fs_balance_fs(sbi, true);
1224
1225 blk_start = (loff_t)pg_start << PAGE_SHIFT;
1226 blk_end = (loff_t)pg_end << PAGE_SHIFT;
1227
1228 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1229 filemap_invalidate_lock(inode->i_mapping);
1230
1231 truncate_pagecache_range(inode, blk_start, blk_end - 1);
1232
1233 f2fs_lock_op(sbi);
1234 ret = f2fs_truncate_hole(inode, pg_start, pg_end);
1235 f2fs_unlock_op(sbi);
1236
1237 filemap_invalidate_unlock(inode->i_mapping);
1238 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1239 }
1240 }
1241
1242 return ret;
1243 }
1244
__read_out_blkaddrs(struct inode * inode,block_t * blkaddr,int * do_replace,pgoff_t off,pgoff_t len)1245 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
1246 int *do_replace, pgoff_t off, pgoff_t len)
1247 {
1248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1249 struct dnode_of_data dn;
1250 int ret, done, i;
1251
1252 next_dnode:
1253 set_new_dnode(&dn, inode, NULL, NULL, 0);
1254 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
1255 if (ret && ret != -ENOENT) {
1256 return ret;
1257 } else if (ret == -ENOENT) {
1258 if (dn.max_level == 0)
1259 return -ENOENT;
1260 done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
1261 dn.ofs_in_node, len);
1262 blkaddr += done;
1263 do_replace += done;
1264 goto next;
1265 }
1266
1267 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
1268 dn.ofs_in_node, len);
1269 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
1270 *blkaddr = f2fs_data_blkaddr(&dn);
1271
1272 if (__is_valid_data_blkaddr(*blkaddr) &&
1273 !f2fs_is_valid_blkaddr(sbi, *blkaddr,
1274 DATA_GENERIC_ENHANCE)) {
1275 f2fs_put_dnode(&dn);
1276 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1277 return -EFSCORRUPTED;
1278 }
1279
1280 if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
1281
1282 if (f2fs_lfs_mode(sbi)) {
1283 f2fs_put_dnode(&dn);
1284 return -EOPNOTSUPP;
1285 }
1286
1287 /* do not invalidate this block address */
1288 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
1289 *do_replace = 1;
1290 }
1291 }
1292 f2fs_put_dnode(&dn);
1293 next:
1294 len -= done;
1295 off += done;
1296 if (len)
1297 goto next_dnode;
1298 return 0;
1299 }
1300
__roll_back_blkaddrs(struct inode * inode,block_t * blkaddr,int * do_replace,pgoff_t off,int len)1301 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
1302 int *do_replace, pgoff_t off, int len)
1303 {
1304 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1305 struct dnode_of_data dn;
1306 int ret, i;
1307
1308 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1309 if (*do_replace == 0)
1310 continue;
1311
1312 set_new_dnode(&dn, inode, NULL, NULL, 0);
1313 ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1314 if (ret) {
1315 dec_valid_block_count(sbi, inode, 1);
1316 f2fs_invalidate_blocks(sbi, *blkaddr);
1317 } else {
1318 f2fs_update_data_blkaddr(&dn, *blkaddr);
1319 }
1320 f2fs_put_dnode(&dn);
1321 }
1322 return 0;
1323 }
1324
__clone_blkaddrs(struct inode * src_inode,struct inode * dst_inode,block_t * blkaddr,int * do_replace,pgoff_t src,pgoff_t dst,pgoff_t len,bool full)1325 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1326 block_t *blkaddr, int *do_replace,
1327 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1328 {
1329 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1330 pgoff_t i = 0;
1331 int ret;
1332
1333 while (i < len) {
1334 if (blkaddr[i] == NULL_ADDR && !full) {
1335 i++;
1336 continue;
1337 }
1338
1339 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1340 struct dnode_of_data dn;
1341 struct node_info ni;
1342 size_t new_size;
1343 pgoff_t ilen;
1344
1345 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1346 ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1347 if (ret)
1348 return ret;
1349
1350 ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
1351 if (ret) {
1352 f2fs_put_dnode(&dn);
1353 return ret;
1354 }
1355
1356 ilen = min((pgoff_t)
1357 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1358 dn.ofs_in_node, len - i);
1359 do {
1360 dn.data_blkaddr = f2fs_data_blkaddr(&dn);
1361 f2fs_truncate_data_blocks_range(&dn, 1);
1362
1363 if (do_replace[i]) {
1364 f2fs_i_blocks_write(src_inode,
1365 1, false, false);
1366 f2fs_i_blocks_write(dst_inode,
1367 1, true, false);
1368 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1369 blkaddr[i], ni.version, true, false);
1370
1371 do_replace[i] = 0;
1372 }
1373 dn.ofs_in_node++;
1374 i++;
1375 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
1376 if (dst_inode->i_size < new_size)
1377 f2fs_i_size_write(dst_inode, new_size);
1378 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1379
1380 f2fs_put_dnode(&dn);
1381 } else {
1382 struct page *psrc, *pdst;
1383
1384 psrc = f2fs_get_lock_data_page(src_inode,
1385 src + i, true);
1386 if (IS_ERR(psrc))
1387 return PTR_ERR(psrc);
1388 pdst = f2fs_get_new_data_page(dst_inode, NULL, dst + i,
1389 true);
1390 if (IS_ERR(pdst)) {
1391 f2fs_put_page(psrc, 1);
1392 return PTR_ERR(pdst);
1393 }
1394
1395 f2fs_wait_on_page_writeback(pdst, DATA, true, true);
1396
1397 memcpy_page(pdst, 0, psrc, 0, PAGE_SIZE);
1398 set_page_dirty(pdst);
1399 set_page_private_gcing(pdst);
1400 f2fs_put_page(pdst, 1);
1401 f2fs_put_page(psrc, 1);
1402
1403 ret = f2fs_truncate_hole(src_inode,
1404 src + i, src + i + 1);
1405 if (ret)
1406 return ret;
1407 i++;
1408 }
1409 }
1410 return 0;
1411 }
1412
__exchange_data_block(struct inode * src_inode,struct inode * dst_inode,pgoff_t src,pgoff_t dst,pgoff_t len,bool full)1413 static int __exchange_data_block(struct inode *src_inode,
1414 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1415 pgoff_t len, bool full)
1416 {
1417 block_t *src_blkaddr;
1418 int *do_replace;
1419 pgoff_t olen;
1420 int ret;
1421
1422 while (len) {
1423 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
1424
1425 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1426 array_size(olen, sizeof(block_t)),
1427 GFP_NOFS);
1428 if (!src_blkaddr)
1429 return -ENOMEM;
1430
1431 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1432 array_size(olen, sizeof(int)),
1433 GFP_NOFS);
1434 if (!do_replace) {
1435 kvfree(src_blkaddr);
1436 return -ENOMEM;
1437 }
1438
1439 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1440 do_replace, src, olen);
1441 if (ret)
1442 goto roll_back;
1443
1444 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1445 do_replace, src, dst, olen, full);
1446 if (ret)
1447 goto roll_back;
1448
1449 src += olen;
1450 dst += olen;
1451 len -= olen;
1452
1453 kvfree(src_blkaddr);
1454 kvfree(do_replace);
1455 }
1456 return 0;
1457
1458 roll_back:
1459 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
1460 kvfree(src_blkaddr);
1461 kvfree(do_replace);
1462 return ret;
1463 }
1464
f2fs_do_collapse(struct inode * inode,loff_t offset,loff_t len)1465 static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
1466 {
1467 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1468 pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1469 pgoff_t start = offset >> PAGE_SHIFT;
1470 pgoff_t end = (offset + len) >> PAGE_SHIFT;
1471 int ret;
1472
1473 f2fs_balance_fs(sbi, true);
1474
1475 /* avoid gc operation during block exchange */
1476 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1477 filemap_invalidate_lock(inode->i_mapping);
1478
1479 f2fs_zero_post_eof_page(inode, offset + len);
1480
1481 f2fs_lock_op(sbi);
1482 f2fs_drop_extent_tree(inode);
1483 truncate_pagecache(inode, offset);
1484 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1485 f2fs_unlock_op(sbi);
1486
1487 filemap_invalidate_unlock(inode->i_mapping);
1488 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1489 return ret;
1490 }
1491
f2fs_collapse_range(struct inode * inode,loff_t offset,loff_t len)1492 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1493 {
1494 loff_t new_size;
1495 int ret;
1496
1497 if (offset + len >= i_size_read(inode))
1498 return -EINVAL;
1499
1500 /* collapse range should be aligned to block size of f2fs. */
1501 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1502 return -EINVAL;
1503
1504 ret = f2fs_convert_inline_inode(inode);
1505 if (ret)
1506 return ret;
1507
1508 /* write out all dirty pages from offset */
1509 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1510 if (ret)
1511 return ret;
1512
1513 ret = f2fs_do_collapse(inode, offset, len);
1514 if (ret)
1515 return ret;
1516
1517 /* write out all moved pages, if possible */
1518 filemap_invalidate_lock(inode->i_mapping);
1519 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1520 truncate_pagecache(inode, offset);
1521
1522 new_size = i_size_read(inode) - len;
1523 ret = f2fs_truncate_blocks(inode, new_size, true);
1524 filemap_invalidate_unlock(inode->i_mapping);
1525 if (!ret)
1526 f2fs_i_size_write(inode, new_size);
1527 return ret;
1528 }
1529
f2fs_do_zero_range(struct dnode_of_data * dn,pgoff_t start,pgoff_t end)1530 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1531 pgoff_t end)
1532 {
1533 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1534 pgoff_t index = start;
1535 unsigned int ofs_in_node = dn->ofs_in_node;
1536 blkcnt_t count = 0;
1537 int ret;
1538
1539 for (; index < end; index++, dn->ofs_in_node++) {
1540 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
1541 count++;
1542 }
1543
1544 dn->ofs_in_node = ofs_in_node;
1545 ret = f2fs_reserve_new_blocks(dn, count);
1546 if (ret)
1547 return ret;
1548
1549 dn->ofs_in_node = ofs_in_node;
1550 for (index = start; index < end; index++, dn->ofs_in_node++) {
1551 dn->data_blkaddr = f2fs_data_blkaddr(dn);
1552 /*
1553 * f2fs_reserve_new_blocks will not guarantee entire block
1554 * allocation.
1555 */
1556 if (dn->data_blkaddr == NULL_ADDR) {
1557 ret = -ENOSPC;
1558 break;
1559 }
1560
1561 if (dn->data_blkaddr == NEW_ADDR)
1562 continue;
1563
1564 if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
1565 DATA_GENERIC_ENHANCE)) {
1566 ret = -EFSCORRUPTED;
1567 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1568 break;
1569 }
1570
1571 f2fs_invalidate_blocks(sbi, dn->data_blkaddr);
1572 f2fs_set_data_blkaddr(dn, NEW_ADDR);
1573 }
1574
1575 f2fs_update_read_extent_cache_range(dn, start, 0, index - start);
1576 f2fs_update_age_extent_cache_range(dn, start, index - start);
1577
1578 return ret;
1579 }
1580
f2fs_zero_range(struct inode * inode,loff_t offset,loff_t len,int mode)1581 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1582 int mode)
1583 {
1584 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1585 struct address_space *mapping = inode->i_mapping;
1586 pgoff_t index, pg_start, pg_end;
1587 loff_t new_size = i_size_read(inode);
1588 loff_t off_start, off_end;
1589 int ret = 0;
1590
1591 ret = inode_newsize_ok(inode, (len + offset));
1592 if (ret)
1593 return ret;
1594
1595 ret = f2fs_convert_inline_inode(inode);
1596 if (ret)
1597 return ret;
1598
1599 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1600 if (ret)
1601 return ret;
1602
1603 filemap_invalidate_lock(mapping);
1604 f2fs_zero_post_eof_page(inode, offset + len);
1605 filemap_invalidate_unlock(mapping);
1606
1607 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1608 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1609
1610 off_start = offset & (PAGE_SIZE - 1);
1611 off_end = (offset + len) & (PAGE_SIZE - 1);
1612
1613 if (pg_start == pg_end) {
1614 ret = fill_zero(inode, pg_start, off_start,
1615 off_end - off_start);
1616 if (ret)
1617 return ret;
1618
1619 new_size = max_t(loff_t, new_size, offset + len);
1620 } else {
1621 if (off_start) {
1622 ret = fill_zero(inode, pg_start++, off_start,
1623 PAGE_SIZE - off_start);
1624 if (ret)
1625 return ret;
1626
1627 new_size = max_t(loff_t, new_size,
1628 (loff_t)pg_start << PAGE_SHIFT);
1629 }
1630
1631 for (index = pg_start; index < pg_end;) {
1632 struct dnode_of_data dn;
1633 unsigned int end_offset;
1634 pgoff_t end;
1635
1636 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1637 filemap_invalidate_lock(mapping);
1638
1639 truncate_pagecache_range(inode,
1640 (loff_t)index << PAGE_SHIFT,
1641 ((loff_t)pg_end << PAGE_SHIFT) - 1);
1642
1643 f2fs_lock_op(sbi);
1644
1645 set_new_dnode(&dn, inode, NULL, NULL, 0);
1646 ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
1647 if (ret) {
1648 f2fs_unlock_op(sbi);
1649 filemap_invalidate_unlock(mapping);
1650 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1651 goto out;
1652 }
1653
1654 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1655 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1656
1657 ret = f2fs_do_zero_range(&dn, index, end);
1658 f2fs_put_dnode(&dn);
1659
1660 f2fs_unlock_op(sbi);
1661 filemap_invalidate_unlock(mapping);
1662 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1663
1664 f2fs_balance_fs(sbi, dn.node_changed);
1665
1666 if (ret)
1667 goto out;
1668
1669 index = end;
1670 new_size = max_t(loff_t, new_size,
1671 (loff_t)index << PAGE_SHIFT);
1672 }
1673
1674 if (off_end) {
1675 ret = fill_zero(inode, pg_end, 0, off_end);
1676 if (ret)
1677 goto out;
1678
1679 new_size = max_t(loff_t, new_size, offset + len);
1680 }
1681 }
1682
1683 out:
1684 if (new_size > i_size_read(inode)) {
1685 if (mode & FALLOC_FL_KEEP_SIZE)
1686 file_set_keep_isize(inode);
1687 else
1688 f2fs_i_size_write(inode, new_size);
1689 }
1690 return ret;
1691 }
1692
f2fs_insert_range(struct inode * inode,loff_t offset,loff_t len)1693 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1694 {
1695 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1696 struct address_space *mapping = inode->i_mapping;
1697 pgoff_t nr, pg_start, pg_end, delta, idx;
1698 loff_t new_size;
1699 int ret = 0;
1700
1701 new_size = i_size_read(inode) + len;
1702 ret = inode_newsize_ok(inode, new_size);
1703 if (ret)
1704 return ret;
1705
1706 if (offset >= i_size_read(inode))
1707 return -EINVAL;
1708
1709 /* insert range should be aligned to block size of f2fs. */
1710 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1711 return -EINVAL;
1712
1713 ret = f2fs_convert_inline_inode(inode);
1714 if (ret)
1715 return ret;
1716
1717 f2fs_balance_fs(sbi, true);
1718
1719 filemap_invalidate_lock(mapping);
1720 ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
1721 filemap_invalidate_unlock(mapping);
1722 if (ret)
1723 return ret;
1724
1725 /* write out all dirty pages from offset */
1726 ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1727 if (ret)
1728 return ret;
1729
1730 pg_start = offset >> PAGE_SHIFT;
1731 pg_end = (offset + len) >> PAGE_SHIFT;
1732 delta = pg_end - pg_start;
1733 idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1734
1735 /* avoid gc operation during block exchange */
1736 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1737 filemap_invalidate_lock(mapping);
1738
1739 f2fs_zero_post_eof_page(inode, offset + len);
1740 truncate_pagecache(inode, offset);
1741
1742 while (!ret && idx > pg_start) {
1743 nr = idx - pg_start;
1744 if (nr > delta)
1745 nr = delta;
1746 idx -= nr;
1747
1748 f2fs_lock_op(sbi);
1749 f2fs_drop_extent_tree(inode);
1750
1751 ret = __exchange_data_block(inode, inode, idx,
1752 idx + delta, nr, false);
1753 f2fs_unlock_op(sbi);
1754 }
1755 filemap_invalidate_unlock(mapping);
1756 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
1757
1758 /* write out all moved pages, if possible */
1759 filemap_invalidate_lock(mapping);
1760 filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
1761 truncate_pagecache(inode, offset);
1762 filemap_invalidate_unlock(mapping);
1763
1764 if (!ret)
1765 f2fs_i_size_write(inode, new_size);
1766 return ret;
1767 }
1768
f2fs_expand_inode_data(struct inode * inode,loff_t offset,loff_t len,int mode)1769 static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
1770 loff_t len, int mode)
1771 {
1772 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1773 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1774 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
1775 .m_may_create = true };
1776 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
1777 .init_gc_type = FG_GC,
1778 .should_migrate_blocks = false,
1779 .err_gc_skipped = true,
1780 .nr_free_secs = 0 };
1781 pgoff_t pg_start, pg_end;
1782 loff_t new_size;
1783 loff_t off_end;
1784 block_t expanded = 0;
1785 int err;
1786
1787 err = inode_newsize_ok(inode, (len + offset));
1788 if (err)
1789 return err;
1790
1791 err = f2fs_convert_inline_inode(inode);
1792 if (err)
1793 return err;
1794
1795 filemap_invalidate_lock(inode->i_mapping);
1796 f2fs_zero_post_eof_page(inode, offset + len);
1797 filemap_invalidate_unlock(inode->i_mapping);
1798
1799 f2fs_balance_fs(sbi, true);
1800
1801 pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
1802 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1803 off_end = (offset + len) & (PAGE_SIZE - 1);
1804
1805 map.m_lblk = pg_start;
1806 map.m_len = pg_end - pg_start;
1807 if (off_end)
1808 map.m_len++;
1809
1810 if (!map.m_len)
1811 return 0;
1812
1813 if (f2fs_is_pinned_file(inode)) {
1814 block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
1815 block_t sec_len = roundup(map.m_len, sec_blks);
1816
1817 map.m_len = sec_blks;
1818 next_alloc:
1819 if (has_not_enough_free_secs(sbi, 0,
1820 GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
1821 f2fs_down_write(&sbi->gc_lock);
1822 stat_inc_gc_call_count(sbi, FOREGROUND);
1823 err = f2fs_gc(sbi, &gc_control);
1824 if (err && err != -ENODATA)
1825 goto out_err;
1826 }
1827
1828 f2fs_down_write(&sbi->pin_sem);
1829
1830 err = f2fs_allocate_pinning_section(sbi);
1831 if (err) {
1832 f2fs_up_write(&sbi->pin_sem);
1833 goto out_err;
1834 }
1835
1836 map.m_seg_type = CURSEG_COLD_DATA_PINNED;
1837 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
1838 file_dont_truncate(inode);
1839
1840 f2fs_up_write(&sbi->pin_sem);
1841
1842 expanded += map.m_len;
1843 sec_len -= map.m_len;
1844 map.m_lblk += map.m_len;
1845 if (!err && sec_len)
1846 goto next_alloc;
1847
1848 map.m_len = expanded;
1849 } else {
1850 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
1851 expanded = map.m_len;
1852 }
1853 out_err:
1854 if (err) {
1855 pgoff_t last_off;
1856
1857 if (!expanded)
1858 return err;
1859
1860 last_off = pg_start + expanded - 1;
1861
1862 /* update new size to the failed position */
1863 new_size = (last_off == pg_end) ? offset + len :
1864 (loff_t)(last_off + 1) << PAGE_SHIFT;
1865 } else {
1866 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1867 }
1868
1869 if (new_size > i_size_read(inode)) {
1870 if (mode & FALLOC_FL_KEEP_SIZE)
1871 file_set_keep_isize(inode);
1872 else
1873 f2fs_i_size_write(inode, new_size);
1874 }
1875
1876 return err;
1877 }
1878
f2fs_fallocate(struct file * file,int mode,loff_t offset,loff_t len)1879 static long f2fs_fallocate(struct file *file, int mode,
1880 loff_t offset, loff_t len)
1881 {
1882 struct inode *inode = file_inode(file);
1883 long ret = 0;
1884
1885 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1886 return -EIO;
1887 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
1888 return -ENOSPC;
1889 if (!f2fs_is_compress_backend_ready(inode))
1890 return -EOPNOTSUPP;
1891
1892 /* f2fs only support ->fallocate for regular file */
1893 if (!S_ISREG(inode->i_mode))
1894 return -EINVAL;
1895
1896 if (IS_ENCRYPTED(inode) &&
1897 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1898 return -EOPNOTSUPP;
1899
1900 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1901 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1902 FALLOC_FL_INSERT_RANGE))
1903 return -EOPNOTSUPP;
1904
1905 inode_lock(inode);
1906
1907 /*
1908 * Pinned file should not support partial truncation since the block
1909 * can be used by applications.
1910 */
1911 if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
1912 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1913 FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
1914 ret = -EOPNOTSUPP;
1915 goto out;
1916 }
1917
1918 ret = file_modified(file);
1919 if (ret)
1920 goto out;
1921
1922 /*
1923 * wait for inflight dio, blocks should be removed after IO
1924 * completion.
1925 */
1926 inode_dio_wait(inode);
1927
1928 if (mode & FALLOC_FL_PUNCH_HOLE) {
1929 if (offset >= inode->i_size)
1930 goto out;
1931
1932 ret = f2fs_punch_hole(inode, offset, len);
1933 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1934 ret = f2fs_collapse_range(inode, offset, len);
1935 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1936 ret = f2fs_zero_range(inode, offset, len, mode);
1937 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1938 ret = f2fs_insert_range(inode, offset, len);
1939 } else {
1940 ret = f2fs_expand_inode_data(inode, offset, len, mode);
1941 }
1942
1943 if (!ret) {
1944 inode->i_mtime = inode_set_ctime_current(inode);
1945 f2fs_mark_inode_dirty_sync(inode, false);
1946 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1947 }
1948
1949 out:
1950 inode_unlock(inode);
1951
1952 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1953 return ret;
1954 }
1955
f2fs_release_file(struct inode * inode,struct file * filp)1956 static int f2fs_release_file(struct inode *inode, struct file *filp)
1957 {
1958 /*
1959 * f2fs_release_file is called at every close calls. So we should
1960 * not drop any inmemory pages by close called by other process.
1961 */
1962 if (!(filp->f_mode & FMODE_WRITE) ||
1963 atomic_read(&inode->i_writecount) != 1)
1964 return 0;
1965
1966 inode_lock(inode);
1967 f2fs_abort_atomic_write(inode, true);
1968 inode_unlock(inode);
1969
1970 return 0;
1971 }
1972
f2fs_file_flush(struct file * file,fl_owner_t id)1973 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1974 {
1975 struct inode *inode = file_inode(file);
1976
1977 /*
1978 * If the process doing a transaction is crashed, we should do
1979 * roll-back. Otherwise, other reader/write can see corrupted database
1980 * until all the writers close its file. Since this should be done
1981 * before dropping file lock, it needs to do in ->flush.
1982 */
1983 if (F2FS_I(inode)->atomic_write_task == current &&
1984 (current->flags & PF_EXITING)) {
1985 inode_lock(inode);
1986 f2fs_abort_atomic_write(inode, true);
1987 inode_unlock(inode);
1988 }
1989
1990 return 0;
1991 }
1992
f2fs_setflags_common(struct inode * inode,u32 iflags,u32 mask)1993 static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
1994 {
1995 struct f2fs_inode_info *fi = F2FS_I(inode);
1996 u32 masked_flags = fi->i_flags & mask;
1997
1998 /* mask can be shrunk by flags_valid selector */
1999 iflags &= mask;
2000
2001 /* Is it quota file? Do not allow user to mess with it */
2002 if (IS_NOQUOTA(inode))
2003 return -EPERM;
2004
2005 if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
2006 if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
2007 return -EOPNOTSUPP;
2008 if (!f2fs_empty_dir(inode))
2009 return -ENOTEMPTY;
2010 }
2011
2012 if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
2013 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
2014 return -EOPNOTSUPP;
2015 if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
2016 return -EINVAL;
2017 }
2018
2019 if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
2020 if (masked_flags & F2FS_COMPR_FL) {
2021 if (!f2fs_disable_compressed_file(inode))
2022 return -EINVAL;
2023 } else {
2024 /* try to convert inline_data to support compression */
2025 int err = f2fs_convert_inline_inode(inode);
2026 if (err)
2027 return err;
2028
2029 f2fs_down_write(&F2FS_I(inode)->i_sem);
2030 if (!f2fs_may_compress(inode) ||
2031 (S_ISREG(inode->i_mode) &&
2032 F2FS_HAS_BLOCKS(inode))) {
2033 f2fs_up_write(&F2FS_I(inode)->i_sem);
2034 return -EINVAL;
2035 }
2036 err = set_compress_context(inode);
2037 f2fs_up_write(&F2FS_I(inode)->i_sem);
2038
2039 if (err)
2040 return err;
2041 }
2042 }
2043
2044 fi->i_flags = iflags | (fi->i_flags & ~mask);
2045 f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
2046 (fi->i_flags & F2FS_NOCOMP_FL));
2047
2048 if (fi->i_flags & F2FS_PROJINHERIT_FL)
2049 set_inode_flag(inode, FI_PROJ_INHERIT);
2050 else
2051 clear_inode_flag(inode, FI_PROJ_INHERIT);
2052
2053 inode_set_ctime_current(inode);
2054 f2fs_set_inode_flags(inode);
2055 f2fs_mark_inode_dirty_sync(inode, true);
2056 return 0;
2057 }
2058
2059 /* FS_IOC_[GS]ETFLAGS and FS_IOC_FS[GS]ETXATTR support */
2060
2061 /*
2062 * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
2063 * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
2064 * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
2065 * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
2066 *
2067 * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
2068 * FS_IOC_FSSETXATTR is done by the VFS.
2069 */
2070
2071 static const struct {
2072 u32 iflag;
2073 u32 fsflag;
2074 } f2fs_fsflags_map[] = {
2075 { F2FS_COMPR_FL, FS_COMPR_FL },
2076 { F2FS_SYNC_FL, FS_SYNC_FL },
2077 { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
2078 { F2FS_APPEND_FL, FS_APPEND_FL },
2079 { F2FS_NODUMP_FL, FS_NODUMP_FL },
2080 { F2FS_NOATIME_FL, FS_NOATIME_FL },
2081 { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
2082 { F2FS_INDEX_FL, FS_INDEX_FL },
2083 { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
2084 { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
2085 { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
2086 };
2087
2088 #define F2FS_GETTABLE_FS_FL ( \
2089 FS_COMPR_FL | \
2090 FS_SYNC_FL | \
2091 FS_IMMUTABLE_FL | \
2092 FS_APPEND_FL | \
2093 FS_NODUMP_FL | \
2094 FS_NOATIME_FL | \
2095 FS_NOCOMP_FL | \
2096 FS_INDEX_FL | \
2097 FS_DIRSYNC_FL | \
2098 FS_PROJINHERIT_FL | \
2099 FS_ENCRYPT_FL | \
2100 FS_INLINE_DATA_FL | \
2101 FS_NOCOW_FL | \
2102 FS_VERITY_FL | \
2103 FS_CASEFOLD_FL)
2104
2105 #define F2FS_SETTABLE_FS_FL ( \
2106 FS_COMPR_FL | \
2107 FS_SYNC_FL | \
2108 FS_IMMUTABLE_FL | \
2109 FS_APPEND_FL | \
2110 FS_NODUMP_FL | \
2111 FS_NOATIME_FL | \
2112 FS_NOCOMP_FL | \
2113 FS_DIRSYNC_FL | \
2114 FS_PROJINHERIT_FL | \
2115 FS_CASEFOLD_FL)
2116
2117 /* Convert f2fs on-disk i_flags to FS_IOC_{GET,SET}FLAGS flags */
f2fs_iflags_to_fsflags(u32 iflags)2118 static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
2119 {
2120 u32 fsflags = 0;
2121 int i;
2122
2123 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2124 if (iflags & f2fs_fsflags_map[i].iflag)
2125 fsflags |= f2fs_fsflags_map[i].fsflag;
2126
2127 return fsflags;
2128 }
2129
2130 /* Convert FS_IOC_{GET,SET}FLAGS flags to f2fs on-disk i_flags */
f2fs_fsflags_to_iflags(u32 fsflags)2131 static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
2132 {
2133 u32 iflags = 0;
2134 int i;
2135
2136 for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
2137 if (fsflags & f2fs_fsflags_map[i].fsflag)
2138 iflags |= f2fs_fsflags_map[i].iflag;
2139
2140 return iflags;
2141 }
2142
f2fs_ioc_getversion(struct file * filp,unsigned long arg)2143 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
2144 {
2145 struct inode *inode = file_inode(filp);
2146
2147 return put_user(inode->i_generation, (int __user *)arg);
2148 }
2149
f2fs_ioc_start_atomic_write(struct file * filp,bool truncate)2150 static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
2151 {
2152 struct inode *inode = file_inode(filp);
2153 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2154 struct f2fs_inode_info *fi = F2FS_I(inode);
2155 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2156 loff_t isize;
2157 int ret;
2158
2159 if (!(filp->f_mode & FMODE_WRITE))
2160 return -EBADF;
2161
2162 if (!inode_owner_or_capable(idmap, inode))
2163 return -EACCES;
2164
2165 if (!S_ISREG(inode->i_mode))
2166 return -EINVAL;
2167
2168 if (filp->f_flags & O_DIRECT)
2169 return -EINVAL;
2170
2171 ret = mnt_want_write_file(filp);
2172 if (ret)
2173 return ret;
2174
2175 inode_lock(inode);
2176
2177 if (!f2fs_disable_compressed_file(inode)) {
2178 ret = -EINVAL;
2179 goto out;
2180 }
2181
2182 if (f2fs_is_atomic_file(inode))
2183 goto out;
2184
2185 ret = f2fs_convert_inline_inode(inode);
2186 if (ret)
2187 goto out;
2188
2189 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
2190
2191 /*
2192 * Should wait end_io to count F2FS_WB_CP_DATA correctly by
2193 * f2fs_is_atomic_file.
2194 */
2195 if (get_dirty_pages(inode))
2196 f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%lu, npages=%u",
2197 inode->i_ino, get_dirty_pages(inode));
2198 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
2199 if (ret) {
2200 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2201 goto out;
2202 }
2203
2204 /* Check if the inode already has a COW inode */
2205 if (fi->cow_inode == NULL) {
2206 /* Create a COW inode for atomic write */
2207 struct dentry *dentry = file_dentry(filp);
2208 struct inode *dir = d_inode(dentry->d_parent);
2209
2210 ret = f2fs_get_tmpfile(idmap, dir, &fi->cow_inode);
2211 if (ret) {
2212 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2213 goto out;
2214 }
2215
2216 set_inode_flag(fi->cow_inode, FI_COW_FILE);
2217 clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
2218
2219 /* Set the COW inode's atomic_inode to the atomic inode */
2220 F2FS_I(fi->cow_inode)->atomic_inode = inode;
2221 } else {
2222 /* Reuse the already created COW inode */
2223 f2fs_bug_on(sbi, get_dirty_pages(fi->cow_inode));
2224
2225 invalidate_mapping_pages(fi->cow_inode->i_mapping, 0, -1);
2226
2227 ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
2228 if (ret) {
2229 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2230 goto out;
2231 }
2232 }
2233
2234 f2fs_write_inode(inode, NULL);
2235
2236 stat_inc_atomic_inode(inode);
2237
2238 set_inode_flag(inode, FI_ATOMIC_FILE);
2239
2240 isize = i_size_read(inode);
2241 fi->original_i_size = isize;
2242 if (truncate) {
2243 set_inode_flag(inode, FI_ATOMIC_REPLACE);
2244 truncate_inode_pages_final(inode->i_mapping);
2245 f2fs_i_size_write(inode, 0);
2246 isize = 0;
2247 }
2248 f2fs_i_size_write(fi->cow_inode, isize);
2249
2250 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
2251
2252 f2fs_update_time(sbi, REQ_TIME);
2253 fi->atomic_write_task = current;
2254 stat_update_max_atomic_write(inode);
2255 fi->atomic_write_cnt = 0;
2256 out:
2257 inode_unlock(inode);
2258 mnt_drop_write_file(filp);
2259 return ret;
2260 }
2261
f2fs_ioc_commit_atomic_write(struct file * filp)2262 static int f2fs_ioc_commit_atomic_write(struct file *filp)
2263 {
2264 struct inode *inode = file_inode(filp);
2265 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2266 int ret;
2267
2268 if (!(filp->f_mode & FMODE_WRITE))
2269 return -EBADF;
2270
2271 if (!inode_owner_or_capable(idmap, inode))
2272 return -EACCES;
2273
2274 ret = mnt_want_write_file(filp);
2275 if (ret)
2276 return ret;
2277
2278 f2fs_balance_fs(F2FS_I_SB(inode), true);
2279
2280 inode_lock(inode);
2281
2282 if (f2fs_is_atomic_file(inode)) {
2283 ret = f2fs_commit_atomic_write(inode);
2284 if (!ret)
2285 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
2286
2287 f2fs_abort_atomic_write(inode, ret);
2288 } else {
2289 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
2290 }
2291
2292 inode_unlock(inode);
2293 mnt_drop_write_file(filp);
2294 return ret;
2295 }
2296
f2fs_ioc_abort_atomic_write(struct file * filp)2297 static int f2fs_ioc_abort_atomic_write(struct file *filp)
2298 {
2299 struct inode *inode = file_inode(filp);
2300 struct mnt_idmap *idmap = file_mnt_idmap(filp);
2301 int ret;
2302
2303 if (!(filp->f_mode & FMODE_WRITE))
2304 return -EBADF;
2305
2306 if (!inode_owner_or_capable(idmap, inode))
2307 return -EACCES;
2308
2309 ret = mnt_want_write_file(filp);
2310 if (ret)
2311 return ret;
2312
2313 inode_lock(inode);
2314
2315 f2fs_abort_atomic_write(inode, true);
2316
2317 inode_unlock(inode);
2318
2319 mnt_drop_write_file(filp);
2320 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2321 return ret;
2322 }
2323
f2fs_do_shutdown(struct f2fs_sb_info * sbi,unsigned int flag,bool readonly,bool need_lock)2324 int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
2325 bool readonly, bool need_lock)
2326 {
2327 struct super_block *sb = sbi->sb;
2328 int ret = 0;
2329
2330 switch (flag) {
2331 case F2FS_GOING_DOWN_FULLSYNC:
2332 ret = freeze_bdev(sb->s_bdev);
2333 if (ret)
2334 goto out;
2335 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2336 thaw_bdev(sb->s_bdev);
2337 break;
2338 case F2FS_GOING_DOWN_METASYNC:
2339 /* do checkpoint only */
2340 ret = f2fs_sync_fs(sb, 1);
2341 if (ret)
2342 goto out;
2343 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2344 break;
2345 case F2FS_GOING_DOWN_NOSYNC:
2346 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2347 break;
2348 case F2FS_GOING_DOWN_METAFLUSH:
2349 f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
2350 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
2351 break;
2352 case F2FS_GOING_DOWN_NEED_FSCK:
2353 set_sbi_flag(sbi, SBI_NEED_FSCK);
2354 set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
2355 set_sbi_flag(sbi, SBI_IS_DIRTY);
2356 /* do checkpoint only */
2357 ret = f2fs_sync_fs(sb, 1);
2358 goto out;
2359 default:
2360 ret = -EINVAL;
2361 goto out;
2362 }
2363
2364 if (readonly)
2365 goto out;
2366
2367 /*
2368 * grab sb->s_umount to avoid racing w/ remount() and other shutdown
2369 * paths.
2370 */
2371 if (need_lock)
2372 down_write(&sbi->sb->s_umount);
2373
2374 f2fs_stop_gc_thread(sbi);
2375 f2fs_stop_discard_thread(sbi);
2376
2377 f2fs_drop_discard_cmd(sbi);
2378 clear_opt(sbi, DISCARD);
2379
2380 if (need_lock)
2381 up_write(&sbi->sb->s_umount);
2382
2383 f2fs_update_time(sbi, REQ_TIME);
2384 out:
2385
2386 trace_f2fs_shutdown(sbi, flag, ret);
2387
2388 return ret;
2389 }
2390
f2fs_ioc_shutdown(struct file * filp,unsigned long arg)2391 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
2392 {
2393 struct inode *inode = file_inode(filp);
2394 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2395 __u32 in;
2396 int ret;
2397 bool need_drop = false, readonly = false;
2398
2399 if (!capable(CAP_SYS_ADMIN))
2400 return -EPERM;
2401
2402 if (get_user(in, (__u32 __user *)arg))
2403 return -EFAULT;
2404
2405 if (in != F2FS_GOING_DOWN_FULLSYNC) {
2406 ret = mnt_want_write_file(filp);
2407 if (ret) {
2408 if (ret != -EROFS)
2409 return ret;
2410
2411 /* fallback to nosync shutdown for readonly fs */
2412 in = F2FS_GOING_DOWN_NOSYNC;
2413 readonly = true;
2414 } else {
2415 need_drop = true;
2416 }
2417 }
2418
2419 ret = f2fs_do_shutdown(sbi, in, readonly, true);
2420
2421 if (need_drop)
2422 mnt_drop_write_file(filp);
2423
2424 return ret;
2425 }
2426
f2fs_ioc_fitrim(struct file * filp,unsigned long arg)2427 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
2428 {
2429 struct inode *inode = file_inode(filp);
2430 struct super_block *sb = inode->i_sb;
2431 struct fstrim_range range;
2432 int ret;
2433
2434 if (!capable(CAP_SYS_ADMIN))
2435 return -EPERM;
2436
2437 if (!f2fs_hw_support_discard(F2FS_SB(sb)))
2438 return -EOPNOTSUPP;
2439
2440 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
2441 sizeof(range)))
2442 return -EFAULT;
2443
2444 ret = mnt_want_write_file(filp);
2445 if (ret)
2446 return ret;
2447
2448 range.minlen = max((unsigned int)range.minlen,
2449 bdev_discard_granularity(sb->s_bdev));
2450 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
2451 mnt_drop_write_file(filp);
2452 if (ret < 0)
2453 return ret;
2454
2455 if (copy_to_user((struct fstrim_range __user *)arg, &range,
2456 sizeof(range)))
2457 return -EFAULT;
2458 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2459 return 0;
2460 }
2461
uuid_is_nonzero(__u8 u[16])2462 static bool uuid_is_nonzero(__u8 u[16])
2463 {
2464 int i;
2465
2466 for (i = 0; i < 16; i++)
2467 if (u[i])
2468 return true;
2469 return false;
2470 }
2471
f2fs_ioc_set_encryption_policy(struct file * filp,unsigned long arg)2472 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
2473 {
2474 struct inode *inode = file_inode(filp);
2475
2476 if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
2477 return -EOPNOTSUPP;
2478
2479 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2480
2481 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
2482 }
2483
f2fs_ioc_get_encryption_policy(struct file * filp,unsigned long arg)2484 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
2485 {
2486 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2487 return -EOPNOTSUPP;
2488 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
2489 }
2490
f2fs_ioc_get_encryption_pwsalt(struct file * filp,unsigned long arg)2491 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
2492 {
2493 struct inode *inode = file_inode(filp);
2494 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2495 u8 encrypt_pw_salt[16];
2496 int err;
2497
2498 if (!f2fs_sb_has_encrypt(sbi))
2499 return -EOPNOTSUPP;
2500
2501 err = mnt_want_write_file(filp);
2502 if (err)
2503 return err;
2504
2505 f2fs_down_write(&sbi->sb_lock);
2506
2507 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
2508 goto got_it;
2509
2510 /* update superblock with uuid */
2511 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
2512
2513 err = f2fs_commit_super(sbi, false);
2514 if (err) {
2515 /* undo new data */
2516 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
2517 goto out_err;
2518 }
2519 got_it:
2520 memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
2521 out_err:
2522 f2fs_up_write(&sbi->sb_lock);
2523 mnt_drop_write_file(filp);
2524
2525 if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
2526 err = -EFAULT;
2527
2528 return err;
2529 }
2530
f2fs_ioc_get_encryption_policy_ex(struct file * filp,unsigned long arg)2531 static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
2532 unsigned long arg)
2533 {
2534 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2535 return -EOPNOTSUPP;
2536
2537 return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
2538 }
2539
f2fs_ioc_add_encryption_key(struct file * filp,unsigned long arg)2540 static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
2541 {
2542 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2543 return -EOPNOTSUPP;
2544
2545 return fscrypt_ioctl_add_key(filp, (void __user *)arg);
2546 }
2547
f2fs_ioc_remove_encryption_key(struct file * filp,unsigned long arg)2548 static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
2549 {
2550 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2551 return -EOPNOTSUPP;
2552
2553 return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
2554 }
2555
f2fs_ioc_remove_encryption_key_all_users(struct file * filp,unsigned long arg)2556 static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
2557 unsigned long arg)
2558 {
2559 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2560 return -EOPNOTSUPP;
2561
2562 return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
2563 }
2564
f2fs_ioc_get_encryption_key_status(struct file * filp,unsigned long arg)2565 static int f2fs_ioc_get_encryption_key_status(struct file *filp,
2566 unsigned long arg)
2567 {
2568 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2569 return -EOPNOTSUPP;
2570
2571 return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
2572 }
2573
f2fs_ioc_get_encryption_nonce(struct file * filp,unsigned long arg)2574 static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
2575 {
2576 if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
2577 return -EOPNOTSUPP;
2578
2579 return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
2580 }
2581
f2fs_ioc_gc(struct file * filp,unsigned long arg)2582 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
2583 {
2584 struct inode *inode = file_inode(filp);
2585 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2586 struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
2587 .no_bg_gc = false,
2588 .should_migrate_blocks = false,
2589 .nr_free_secs = 0 };
2590 __u32 sync;
2591 int ret;
2592
2593 if (!capable(CAP_SYS_ADMIN))
2594 return -EPERM;
2595
2596 if (get_user(sync, (__u32 __user *)arg))
2597 return -EFAULT;
2598
2599 if (f2fs_readonly(sbi->sb))
2600 return -EROFS;
2601
2602 ret = mnt_want_write_file(filp);
2603 if (ret)
2604 return ret;
2605
2606 if (!sync) {
2607 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2608 ret = -EBUSY;
2609 goto out;
2610 }
2611 } else {
2612 f2fs_down_write(&sbi->gc_lock);
2613 }
2614
2615 gc_control.init_gc_type = sync ? FG_GC : BG_GC;
2616 gc_control.err_gc_skipped = sync;
2617 stat_inc_gc_call_count(sbi, FOREGROUND);
2618 ret = f2fs_gc(sbi, &gc_control);
2619 out:
2620 mnt_drop_write_file(filp);
2621 return ret;
2622 }
2623
__f2fs_ioc_gc_range(struct file * filp,struct f2fs_gc_range * range)2624 static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
2625 {
2626 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
2627 struct f2fs_gc_control gc_control = {
2628 .init_gc_type = range->sync ? FG_GC : BG_GC,
2629 .no_bg_gc = false,
2630 .should_migrate_blocks = false,
2631 .err_gc_skipped = range->sync,
2632 .nr_free_secs = 0 };
2633 u64 end;
2634 int ret;
2635
2636 if (!capable(CAP_SYS_ADMIN))
2637 return -EPERM;
2638 if (f2fs_readonly(sbi->sb))
2639 return -EROFS;
2640
2641 end = range->start + range->len;
2642 if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
2643 end >= MAX_BLKADDR(sbi))
2644 return -EINVAL;
2645
2646 ret = mnt_want_write_file(filp);
2647 if (ret)
2648 return ret;
2649
2650 do_more:
2651 if (!range->sync) {
2652 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2653 ret = -EBUSY;
2654 goto out;
2655 }
2656 } else {
2657 f2fs_down_write(&sbi->gc_lock);
2658 }
2659
2660 gc_control.victim_segno = GET_SEGNO(sbi, range->start);
2661 stat_inc_gc_call_count(sbi, FOREGROUND);
2662 ret = f2fs_gc(sbi, &gc_control);
2663 if (ret) {
2664 if (ret == -EBUSY)
2665 ret = -EAGAIN;
2666 goto out;
2667 }
2668 range->start += CAP_BLKS_PER_SEC(sbi);
2669 if (range->start <= end)
2670 goto do_more;
2671 out:
2672 mnt_drop_write_file(filp);
2673 return ret;
2674 }
2675
f2fs_ioc_gc_range(struct file * filp,unsigned long arg)2676 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2677 {
2678 struct f2fs_gc_range range;
2679
2680 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2681 sizeof(range)))
2682 return -EFAULT;
2683 return __f2fs_ioc_gc_range(filp, &range);
2684 }
2685
f2fs_ioc_write_checkpoint(struct file * filp)2686 static int f2fs_ioc_write_checkpoint(struct file *filp)
2687 {
2688 struct inode *inode = file_inode(filp);
2689 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2690 int ret;
2691
2692 if (!capable(CAP_SYS_ADMIN))
2693 return -EPERM;
2694
2695 if (f2fs_readonly(sbi->sb))
2696 return -EROFS;
2697
2698 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2699 f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
2700 return -EINVAL;
2701 }
2702
2703 ret = mnt_want_write_file(filp);
2704 if (ret)
2705 return ret;
2706
2707 ret = f2fs_sync_fs(sbi->sb, 1);
2708
2709 mnt_drop_write_file(filp);
2710 return ret;
2711 }
2712
f2fs_defragment_range(struct f2fs_sb_info * sbi,struct file * filp,struct f2fs_defragment * range)2713 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2714 struct file *filp,
2715 struct f2fs_defragment *range)
2716 {
2717 struct inode *inode = file_inode(filp);
2718 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2719 .m_seg_type = NO_CHECK_TYPE,
2720 .m_may_create = false };
2721 struct extent_info ei = {};
2722 pgoff_t pg_start, pg_end, next_pgofs;
2723 unsigned int total = 0, sec_num;
2724 block_t blk_end = 0;
2725 bool fragmented = false;
2726 int err;
2727
2728 pg_start = range->start >> PAGE_SHIFT;
2729 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2730
2731 f2fs_balance_fs(sbi, true);
2732
2733 inode_lock(inode);
2734
2735 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) ||
2736 f2fs_is_atomic_file(inode)) {
2737 err = -EINVAL;
2738 goto unlock_out;
2739 }
2740
2741 /* if in-place-update policy is enabled, don't waste time here */
2742 set_inode_flag(inode, FI_OPU_WRITE);
2743 if (f2fs_should_update_inplace(inode, NULL)) {
2744 err = -EINVAL;
2745 goto out;
2746 }
2747
2748 /* writeback all dirty pages in the range */
2749 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2750 range->start + range->len - 1);
2751 if (err)
2752 goto out;
2753
2754 /*
2755 * lookup mapping info in extent cache, skip defragmenting if physical
2756 * block addresses are continuous.
2757 */
2758 if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
2759 if ((pgoff_t)ei.fofs + ei.len >= pg_end)
2760 goto out;
2761 }
2762
2763 map.m_lblk = pg_start;
2764 map.m_next_pgofs = &next_pgofs;
2765
2766 /*
2767 * lookup mapping info in dnode page cache, skip defragmenting if all
2768 * physical block addresses are continuous even if there are hole(s)
2769 * in logical blocks.
2770 */
2771 while (map.m_lblk < pg_end) {
2772 map.m_len = pg_end - map.m_lblk;
2773 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2774 if (err)
2775 goto out;
2776
2777 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2778 map.m_lblk = next_pgofs;
2779 continue;
2780 }
2781
2782 if (blk_end && blk_end != map.m_pblk)
2783 fragmented = true;
2784
2785 /* record total count of block that we're going to move */
2786 total += map.m_len;
2787
2788 blk_end = map.m_pblk + map.m_len;
2789
2790 map.m_lblk += map.m_len;
2791 }
2792
2793 if (!fragmented) {
2794 total = 0;
2795 goto out;
2796 }
2797
2798 sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
2799
2800 /*
2801 * make sure there are enough free section for LFS allocation, this can
2802 * avoid defragment running in SSR mode when free section are allocated
2803 * intensively
2804 */
2805 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2806 err = -EAGAIN;
2807 goto out;
2808 }
2809
2810 map.m_lblk = pg_start;
2811 map.m_len = pg_end - pg_start;
2812 total = 0;
2813
2814 while (map.m_lblk < pg_end) {
2815 pgoff_t idx;
2816 int cnt = 0;
2817
2818 do_map:
2819 map.m_len = pg_end - map.m_lblk;
2820 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
2821 if (err)
2822 goto clear_out;
2823
2824 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2825 map.m_lblk = next_pgofs;
2826 goto check;
2827 }
2828
2829 set_inode_flag(inode, FI_SKIP_WRITES);
2830
2831 idx = map.m_lblk;
2832 while (idx < map.m_lblk + map.m_len &&
2833 cnt < BLKS_PER_SEG(sbi)) {
2834 struct page *page;
2835
2836 page = f2fs_get_lock_data_page(inode, idx, true);
2837 if (IS_ERR(page)) {
2838 err = PTR_ERR(page);
2839 goto clear_out;
2840 }
2841
2842 f2fs_wait_on_page_writeback(page, DATA, true, true);
2843
2844 set_page_dirty(page);
2845 set_page_private_gcing(page);
2846 f2fs_put_page(page, 1);
2847
2848 idx++;
2849 cnt++;
2850 total++;
2851 }
2852
2853 map.m_lblk = idx;
2854 check:
2855 if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
2856 goto do_map;
2857
2858 clear_inode_flag(inode, FI_SKIP_WRITES);
2859
2860 err = filemap_fdatawrite(inode->i_mapping);
2861 if (err)
2862 goto out;
2863 }
2864 clear_out:
2865 clear_inode_flag(inode, FI_SKIP_WRITES);
2866 out:
2867 clear_inode_flag(inode, FI_OPU_WRITE);
2868 unlock_out:
2869 inode_unlock(inode);
2870 if (!err)
2871 range->len = (u64)total << PAGE_SHIFT;
2872 return err;
2873 }
2874
f2fs_ioc_defragment(struct file * filp,unsigned long arg)2875 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2876 {
2877 struct inode *inode = file_inode(filp);
2878 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2879 struct f2fs_defragment range;
2880 int err;
2881
2882 if (!capable(CAP_SYS_ADMIN))
2883 return -EPERM;
2884
2885 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2886 return -EINVAL;
2887
2888 if (f2fs_readonly(sbi->sb))
2889 return -EROFS;
2890
2891 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2892 sizeof(range)))
2893 return -EFAULT;
2894
2895 /* verify alignment of offset & size */
2896 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2897 return -EINVAL;
2898
2899 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2900 max_file_blocks(inode)))
2901 return -EINVAL;
2902
2903 err = mnt_want_write_file(filp);
2904 if (err)
2905 return err;
2906
2907 err = f2fs_defragment_range(sbi, filp, &range);
2908 mnt_drop_write_file(filp);
2909
2910 f2fs_update_time(sbi, REQ_TIME);
2911 if (err < 0)
2912 return err;
2913
2914 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2915 sizeof(range)))
2916 return -EFAULT;
2917
2918 return 0;
2919 }
2920
f2fs_move_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,size_t len)2921 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2922 struct file *file_out, loff_t pos_out, size_t len)
2923 {
2924 struct inode *src = file_inode(file_in);
2925 struct inode *dst = file_inode(file_out);
2926 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2927 size_t olen = len, dst_max_i_size = 0;
2928 size_t dst_osize;
2929 int ret;
2930
2931 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2932 src->i_sb != dst->i_sb)
2933 return -EXDEV;
2934
2935 if (unlikely(f2fs_readonly(src->i_sb)))
2936 return -EROFS;
2937
2938 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2939 return -EINVAL;
2940
2941 if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst))
2942 return -EOPNOTSUPP;
2943
2944 if (pos_out < 0 || pos_in < 0)
2945 return -EINVAL;
2946
2947 if (src == dst) {
2948 if (pos_in == pos_out)
2949 return 0;
2950 if (pos_out > pos_in && pos_out < pos_in + len)
2951 return -EINVAL;
2952 }
2953
2954 inode_lock(src);
2955 if (src != dst) {
2956 ret = -EBUSY;
2957 if (!inode_trylock(dst))
2958 goto out;
2959 }
2960
2961 if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
2962 f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
2963 ret = -EOPNOTSUPP;
2964 goto out_unlock;
2965 }
2966
2967 if (f2fs_is_atomic_file(src) || f2fs_is_atomic_file(dst)) {
2968 ret = -EINVAL;
2969 goto out_unlock;
2970 }
2971
2972 ret = -EINVAL;
2973 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2974 goto out_unlock;
2975 if (len == 0)
2976 olen = len = src->i_size - pos_in;
2977 if (pos_in + len == src->i_size)
2978 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2979 if (len == 0) {
2980 ret = 0;
2981 goto out_unlock;
2982 }
2983
2984 dst_osize = dst->i_size;
2985 if (pos_out + olen > dst->i_size)
2986 dst_max_i_size = pos_out + olen;
2987
2988 /* verify the end result is block aligned */
2989 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2990 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2991 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2992 goto out_unlock;
2993
2994 ret = f2fs_convert_inline_inode(src);
2995 if (ret)
2996 goto out_unlock;
2997
2998 ret = f2fs_convert_inline_inode(dst);
2999 if (ret)
3000 goto out_unlock;
3001
3002 /* write out all dirty pages from offset */
3003 ret = filemap_write_and_wait_range(src->i_mapping,
3004 pos_in, pos_in + len);
3005 if (ret)
3006 goto out_unlock;
3007
3008 ret = filemap_write_and_wait_range(dst->i_mapping,
3009 pos_out, pos_out + len);
3010 if (ret)
3011 goto out_unlock;
3012
3013 f2fs_balance_fs(sbi, true);
3014
3015 f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3016 if (src != dst) {
3017 ret = -EBUSY;
3018 if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
3019 goto out_src;
3020 }
3021
3022 f2fs_lock_op(sbi);
3023 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
3024 pos_out >> F2FS_BLKSIZE_BITS,
3025 len >> F2FS_BLKSIZE_BITS, false);
3026
3027 if (!ret) {
3028 if (dst_max_i_size)
3029 f2fs_i_size_write(dst, dst_max_i_size);
3030 else if (dst_osize != dst->i_size)
3031 f2fs_i_size_write(dst, dst_osize);
3032 }
3033 f2fs_unlock_op(sbi);
3034
3035 if (src != dst)
3036 f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
3037 out_src:
3038 f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
3039 if (ret)
3040 goto out_unlock;
3041
3042 src->i_mtime = inode_set_ctime_current(src);
3043 f2fs_mark_inode_dirty_sync(src, false);
3044 if (src != dst) {
3045 dst->i_mtime = inode_set_ctime_current(dst);
3046 f2fs_mark_inode_dirty_sync(dst, false);
3047 }
3048 f2fs_update_time(sbi, REQ_TIME);
3049
3050 out_unlock:
3051 if (src != dst)
3052 inode_unlock(dst);
3053 out:
3054 inode_unlock(src);
3055 return ret;
3056 }
3057
__f2fs_ioc_move_range(struct file * filp,struct f2fs_move_range * range)3058 static int __f2fs_ioc_move_range(struct file *filp,
3059 struct f2fs_move_range *range)
3060 {
3061 struct fd dst;
3062 int err;
3063
3064 if (!(filp->f_mode & FMODE_READ) ||
3065 !(filp->f_mode & FMODE_WRITE))
3066 return -EBADF;
3067
3068 dst = fdget(range->dst_fd);
3069 if (!dst.file)
3070 return -EBADF;
3071
3072 if (!(dst.file->f_mode & FMODE_WRITE)) {
3073 err = -EBADF;
3074 goto err_out;
3075 }
3076
3077 err = mnt_want_write_file(filp);
3078 if (err)
3079 goto err_out;
3080
3081 err = f2fs_move_file_range(filp, range->pos_in, dst.file,
3082 range->pos_out, range->len);
3083
3084 mnt_drop_write_file(filp);
3085 err_out:
3086 fdput(dst);
3087 return err;
3088 }
3089
f2fs_ioc_move_range(struct file * filp,unsigned long arg)3090 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
3091 {
3092 struct f2fs_move_range range;
3093
3094 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
3095 sizeof(range)))
3096 return -EFAULT;
3097 return __f2fs_ioc_move_range(filp, &range);
3098 }
3099
f2fs_ioc_flush_device(struct file * filp,unsigned long arg)3100 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
3101 {
3102 struct inode *inode = file_inode(filp);
3103 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3104 struct sit_info *sm = SIT_I(sbi);
3105 unsigned int start_segno = 0, end_segno = 0;
3106 unsigned int dev_start_segno = 0, dev_end_segno = 0;
3107 struct f2fs_flush_device range;
3108 struct f2fs_gc_control gc_control = {
3109 .init_gc_type = FG_GC,
3110 .should_migrate_blocks = true,
3111 .err_gc_skipped = true,
3112 .nr_free_secs = 0 };
3113 int ret;
3114
3115 if (!capable(CAP_SYS_ADMIN))
3116 return -EPERM;
3117
3118 if (f2fs_readonly(sbi->sb))
3119 return -EROFS;
3120
3121 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
3122 return -EINVAL;
3123
3124 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
3125 sizeof(range)))
3126 return -EFAULT;
3127
3128 if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
3129 __is_large_section(sbi)) {
3130 f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
3131 range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
3132 return -EINVAL;
3133 }
3134
3135 ret = mnt_want_write_file(filp);
3136 if (ret)
3137 return ret;
3138
3139 if (range.dev_num != 0)
3140 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
3141 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
3142
3143 start_segno = sm->last_victim[FLUSH_DEVICE];
3144 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
3145 start_segno = dev_start_segno;
3146 end_segno = min(start_segno + range.segments, dev_end_segno);
3147
3148 while (start_segno < end_segno) {
3149 if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
3150 ret = -EBUSY;
3151 goto out;
3152 }
3153 sm->last_victim[GC_CB] = end_segno + 1;
3154 sm->last_victim[GC_GREEDY] = end_segno + 1;
3155 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
3156
3157 gc_control.victim_segno = start_segno;
3158 stat_inc_gc_call_count(sbi, FOREGROUND);
3159 ret = f2fs_gc(sbi, &gc_control);
3160 if (ret == -EAGAIN)
3161 ret = 0;
3162 else if (ret < 0)
3163 break;
3164 start_segno++;
3165 }
3166 out:
3167 mnt_drop_write_file(filp);
3168 return ret;
3169 }
3170
f2fs_ioc_get_features(struct file * filp,unsigned long arg)3171 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
3172 {
3173 struct inode *inode = file_inode(filp);
3174 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
3175
3176 /* Must validate to set it with SQLite behavior in Android. */
3177 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
3178
3179 return put_user(sb_feature, (u32 __user *)arg);
3180 }
3181
3182 #ifdef CONFIG_QUOTA
f2fs_transfer_project_quota(struct inode * inode,kprojid_t kprojid)3183 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3184 {
3185 struct dquot *transfer_to[MAXQUOTAS] = {};
3186 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3187 struct super_block *sb = sbi->sb;
3188 int err;
3189
3190 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
3191 if (IS_ERR(transfer_to[PRJQUOTA]))
3192 return PTR_ERR(transfer_to[PRJQUOTA]);
3193
3194 err = __dquot_transfer(inode, transfer_to);
3195 if (err)
3196 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
3197 dqput(transfer_to[PRJQUOTA]);
3198 return err;
3199 }
3200
f2fs_ioc_setproject(struct inode * inode,__u32 projid)3201 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3202 {
3203 struct f2fs_inode_info *fi = F2FS_I(inode);
3204 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3205 struct f2fs_inode *ri = NULL;
3206 kprojid_t kprojid;
3207 int err;
3208
3209 if (!f2fs_sb_has_project_quota(sbi)) {
3210 if (projid != F2FS_DEF_PROJID)
3211 return -EOPNOTSUPP;
3212 else
3213 return 0;
3214 }
3215
3216 if (!f2fs_has_extra_attr(inode))
3217 return -EOPNOTSUPP;
3218
3219 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
3220
3221 if (projid_eq(kprojid, fi->i_projid))
3222 return 0;
3223
3224 err = -EPERM;
3225 /* Is it quota file? Do not allow user to mess with it */
3226 if (IS_NOQUOTA(inode))
3227 return err;
3228
3229 if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
3230 return -EOVERFLOW;
3231
3232 err = f2fs_dquot_initialize(inode);
3233 if (err)
3234 return err;
3235
3236 f2fs_lock_op(sbi);
3237 err = f2fs_transfer_project_quota(inode, kprojid);
3238 if (err)
3239 goto out_unlock;
3240
3241 fi->i_projid = kprojid;
3242 inode_set_ctime_current(inode);
3243 f2fs_mark_inode_dirty_sync(inode, true);
3244 out_unlock:
3245 f2fs_unlock_op(sbi);
3246 return err;
3247 }
3248 #else
f2fs_transfer_project_quota(struct inode * inode,kprojid_t kprojid)3249 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
3250 {
3251 return 0;
3252 }
3253
f2fs_ioc_setproject(struct inode * inode,__u32 projid)3254 static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
3255 {
3256 if (projid != F2FS_DEF_PROJID)
3257 return -EOPNOTSUPP;
3258 return 0;
3259 }
3260 #endif
3261
f2fs_fileattr_get(struct dentry * dentry,struct fileattr * fa)3262 int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3263 {
3264 struct inode *inode = d_inode(dentry);
3265 struct f2fs_inode_info *fi = F2FS_I(inode);
3266 u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
3267
3268 if (IS_ENCRYPTED(inode))
3269 fsflags |= FS_ENCRYPT_FL;
3270 if (IS_VERITY(inode))
3271 fsflags |= FS_VERITY_FL;
3272 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
3273 fsflags |= FS_INLINE_DATA_FL;
3274 if (is_inode_flag_set(inode, FI_PIN_FILE))
3275 fsflags |= FS_NOCOW_FL;
3276
3277 fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
3278
3279 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
3280 fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
3281
3282 return 0;
3283 }
3284
f2fs_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct fileattr * fa)3285 int f2fs_fileattr_set(struct mnt_idmap *idmap,
3286 struct dentry *dentry, struct fileattr *fa)
3287 {
3288 struct inode *inode = d_inode(dentry);
3289 u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
3290 u32 iflags;
3291 int err;
3292
3293 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
3294 return -EIO;
3295 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
3296 return -ENOSPC;
3297 if (fsflags & ~F2FS_GETTABLE_FS_FL)
3298 return -EOPNOTSUPP;
3299 fsflags &= F2FS_SETTABLE_FS_FL;
3300 if (!fa->flags_valid)
3301 mask &= FS_COMMON_FL;
3302
3303 iflags = f2fs_fsflags_to_iflags(fsflags);
3304 if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
3305 return -EOPNOTSUPP;
3306
3307 err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
3308 if (!err)
3309 err = f2fs_ioc_setproject(inode, fa->fsx_projid);
3310
3311 return err;
3312 }
3313
f2fs_pin_file_control(struct inode * inode,bool inc)3314 int f2fs_pin_file_control(struct inode *inode, bool inc)
3315 {
3316 struct f2fs_inode_info *fi = F2FS_I(inode);
3317 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3318
3319 /* Use i_gc_failures for normal file as a risk signal. */
3320 if (inc)
3321 f2fs_i_gc_failures_write(inode,
3322 fi->i_gc_failures[GC_FAILURE_PIN] + 1);
3323
3324 if (fi->i_gc_failures[GC_FAILURE_PIN] > sbi->gc_pin_file_threshold) {
3325 f2fs_warn(sbi, "%s: Enable GC = ino %lx after %x GC trials",
3326 __func__, inode->i_ino,
3327 fi->i_gc_failures[GC_FAILURE_PIN]);
3328 clear_inode_flag(inode, FI_PIN_FILE);
3329 return -EAGAIN;
3330 }
3331 return 0;
3332 }
3333
f2fs_ioc_set_pin_file(struct file * filp,unsigned long arg)3334 static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
3335 {
3336 struct inode *inode = file_inode(filp);
3337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3338 __u32 pin;
3339 int ret = 0;
3340
3341 if (get_user(pin, (__u32 __user *)arg))
3342 return -EFAULT;
3343
3344 if (!S_ISREG(inode->i_mode))
3345 return -EINVAL;
3346
3347 if (f2fs_readonly(sbi->sb))
3348 return -EROFS;
3349
3350 ret = mnt_want_write_file(filp);
3351 if (ret)
3352 return ret;
3353
3354 inode_lock(inode);
3355
3356 if (f2fs_is_atomic_file(inode)) {
3357 ret = -EINVAL;
3358 goto out;
3359 }
3360
3361 if (!pin) {
3362 clear_inode_flag(inode, FI_PIN_FILE);
3363 f2fs_i_gc_failures_write(inode, 0);
3364 goto done;
3365 } else if (f2fs_is_pinned_file(inode)) {
3366 goto done;
3367 }
3368
3369 if (F2FS_HAS_BLOCKS(inode)) {
3370 ret = -EFBIG;
3371 goto out;
3372 }
3373
3374 /* Let's allow file pinning on zoned device. */
3375 if (!f2fs_sb_has_blkzoned(sbi) &&
3376 f2fs_should_update_outplace(inode, NULL)) {
3377 ret = -EINVAL;
3378 goto out;
3379 }
3380
3381 if (f2fs_pin_file_control(inode, false)) {
3382 ret = -EAGAIN;
3383 goto out;
3384 }
3385
3386 ret = f2fs_convert_inline_inode(inode);
3387 if (ret)
3388 goto out;
3389
3390 if (!f2fs_disable_compressed_file(inode)) {
3391 ret = -EOPNOTSUPP;
3392 goto out;
3393 }
3394
3395 set_inode_flag(inode, FI_PIN_FILE);
3396 ret = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3397 done:
3398 f2fs_update_time(sbi, REQ_TIME);
3399 out:
3400 inode_unlock(inode);
3401 mnt_drop_write_file(filp);
3402 return ret;
3403 }
3404
f2fs_ioc_get_pin_file(struct file * filp,unsigned long arg)3405 static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
3406 {
3407 struct inode *inode = file_inode(filp);
3408 __u32 pin = 0;
3409
3410 if (is_inode_flag_set(inode, FI_PIN_FILE))
3411 pin = F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN];
3412 return put_user(pin, (u32 __user *)arg);
3413 }
3414
f2fs_precache_extents(struct inode * inode)3415 int f2fs_precache_extents(struct inode *inode)
3416 {
3417 struct f2fs_inode_info *fi = F2FS_I(inode);
3418 struct f2fs_map_blocks map;
3419 pgoff_t m_next_extent;
3420 loff_t end;
3421 int err;
3422
3423 if (is_inode_flag_set(inode, FI_NO_EXTENT))
3424 return -EOPNOTSUPP;
3425
3426 map.m_lblk = 0;
3427 map.m_pblk = 0;
3428 map.m_next_pgofs = NULL;
3429 map.m_next_extent = &m_next_extent;
3430 map.m_seg_type = NO_CHECK_TYPE;
3431 map.m_may_create = false;
3432 end = max_file_blocks(inode);
3433
3434 while (map.m_lblk < end) {
3435 map.m_len = end - map.m_lblk;
3436
3437 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
3438 err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
3439 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
3440 if (err)
3441 return err;
3442
3443 map.m_lblk = m_next_extent;
3444 }
3445
3446 return 0;
3447 }
3448
f2fs_ioc_precache_extents(struct file * filp)3449 static int f2fs_ioc_precache_extents(struct file *filp)
3450 {
3451 return f2fs_precache_extents(file_inode(filp));
3452 }
3453
f2fs_ioc_resize_fs(struct file * filp,unsigned long arg)3454 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3455 {
3456 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3457 __u64 block_count;
3458
3459 if (!capable(CAP_SYS_ADMIN))
3460 return -EPERM;
3461
3462 if (f2fs_readonly(sbi->sb))
3463 return -EROFS;
3464
3465 if (copy_from_user(&block_count, (void __user *)arg,
3466 sizeof(block_count)))
3467 return -EFAULT;
3468
3469 return f2fs_resize_fs(filp, block_count);
3470 }
3471
f2fs_has_feature_verity(struct file * filp)3472 static inline int f2fs_has_feature_verity(struct file *filp)
3473 {
3474 struct inode *inode = file_inode(filp);
3475
3476 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3477
3478 if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
3479 f2fs_warn(F2FS_I_SB(inode),
3480 "Can't enable fs-verity on inode %lu: the verity feature is not enabled on this filesystem",
3481 inode->i_ino);
3482 return -EOPNOTSUPP;
3483 }
3484 return 0;
3485 }
3486
f2fs_ioc_enable_verity(struct file * filp,unsigned long arg)3487 static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
3488 {
3489 int err = f2fs_has_feature_verity(filp);
3490
3491 if (err)
3492 return err;
3493
3494 return fsverity_ioctl_enable(filp, (const void __user *)arg);
3495 }
3496
f2fs_ioc_enable_code_sign(struct file * filp,unsigned long arg)3497 static int f2fs_ioc_enable_code_sign(struct file *filp, unsigned long arg)
3498 {
3499 int err = f2fs_has_feature_verity(filp);
3500
3501 if (err)
3502 return err;
3503
3504 return fsverity_ioctl_enable_code_sign(filp, (const void __user *)arg);
3505 }
3506
f2fs_ioc_measure_verity(struct file * filp,unsigned long arg)3507 static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
3508 {
3509 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3510 return -EOPNOTSUPP;
3511
3512 return fsverity_ioctl_measure(filp, (void __user *)arg);
3513 }
3514
f2fs_ioc_read_verity_metadata(struct file * filp,unsigned long arg)3515 static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
3516 {
3517 if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
3518 return -EOPNOTSUPP;
3519
3520 return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
3521 }
3522
f2fs_ioc_getfslabel(struct file * filp,unsigned long arg)3523 static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
3524 {
3525 struct inode *inode = file_inode(filp);
3526 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3527 char *vbuf;
3528 int count;
3529 int err = 0;
3530
3531 vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
3532 if (!vbuf)
3533 return -ENOMEM;
3534
3535 f2fs_down_read(&sbi->sb_lock);
3536 count = utf16s_to_utf8s(sbi->raw_super->volume_name,
3537 ARRAY_SIZE(sbi->raw_super->volume_name),
3538 UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
3539 f2fs_up_read(&sbi->sb_lock);
3540
3541 if (copy_to_user((char __user *)arg, vbuf,
3542 min(FSLABEL_MAX, count)))
3543 err = -EFAULT;
3544
3545 kfree(vbuf);
3546 return err;
3547 }
3548
f2fs_ioc_setfslabel(struct file * filp,unsigned long arg)3549 static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
3550 {
3551 struct inode *inode = file_inode(filp);
3552 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3553 char *vbuf;
3554 int err = 0;
3555
3556 if (!capable(CAP_SYS_ADMIN))
3557 return -EPERM;
3558
3559 vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
3560 if (IS_ERR(vbuf))
3561 return PTR_ERR(vbuf);
3562
3563 err = mnt_want_write_file(filp);
3564 if (err)
3565 goto out;
3566
3567 f2fs_down_write(&sbi->sb_lock);
3568
3569 memset(sbi->raw_super->volume_name, 0,
3570 sizeof(sbi->raw_super->volume_name));
3571 utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
3572 sbi->raw_super->volume_name,
3573 ARRAY_SIZE(sbi->raw_super->volume_name));
3574
3575 err = f2fs_commit_super(sbi, false);
3576
3577 f2fs_up_write(&sbi->sb_lock);
3578
3579 mnt_drop_write_file(filp);
3580 out:
3581 kfree(vbuf);
3582 return err;
3583 }
3584
f2fs_get_compress_blocks(struct inode * inode,__u64 * blocks)3585 static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
3586 {
3587 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
3588 return -EOPNOTSUPP;
3589
3590 if (!f2fs_compressed_file(inode))
3591 return -EINVAL;
3592
3593 *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
3594
3595 return 0;
3596 }
3597
f2fs_ioc_get_compress_blocks(struct file * filp,unsigned long arg)3598 static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
3599 {
3600 struct inode *inode = file_inode(filp);
3601 __u64 blocks;
3602 int ret;
3603
3604 ret = f2fs_get_compress_blocks(inode, &blocks);
3605 if (ret < 0)
3606 return ret;
3607
3608 return put_user(blocks, (u64 __user *)arg);
3609 }
3610
release_compress_blocks(struct dnode_of_data * dn,pgoff_t count)3611 static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
3612 {
3613 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3614 unsigned int released_blocks = 0;
3615 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3616 block_t blkaddr;
3617 int i;
3618
3619 for (i = 0; i < count; i++) {
3620 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3621 dn->ofs_in_node + i);
3622
3623 if (!__is_valid_data_blkaddr(blkaddr))
3624 continue;
3625 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3626 DATA_GENERIC_ENHANCE))) {
3627 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3628 return -EFSCORRUPTED;
3629 }
3630 }
3631
3632 while (count) {
3633 int compr_blocks = 0;
3634
3635 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3636 blkaddr = f2fs_data_blkaddr(dn);
3637
3638 if (i == 0) {
3639 if (blkaddr == COMPRESS_ADDR)
3640 continue;
3641 dn->ofs_in_node += cluster_size;
3642 goto next;
3643 }
3644
3645 if (__is_valid_data_blkaddr(blkaddr))
3646 compr_blocks++;
3647
3648 if (blkaddr != NEW_ADDR)
3649 continue;
3650
3651 f2fs_set_data_blkaddr(dn, NULL_ADDR);
3652 }
3653
3654 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
3655 dec_valid_block_count(sbi, dn->inode,
3656 cluster_size - compr_blocks);
3657
3658 released_blocks += cluster_size - compr_blocks;
3659 next:
3660 count -= cluster_size;
3661 }
3662
3663 return released_blocks;
3664 }
3665
f2fs_release_compress_blocks(struct file * filp,unsigned long arg)3666 static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
3667 {
3668 struct inode *inode = file_inode(filp);
3669 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3670 pgoff_t page_idx = 0, last_idx;
3671 unsigned int released_blocks = 0;
3672 int ret;
3673 int writecount;
3674
3675 if (!f2fs_sb_has_compression(sbi))
3676 return -EOPNOTSUPP;
3677
3678 if (f2fs_readonly(sbi->sb))
3679 return -EROFS;
3680
3681 ret = mnt_want_write_file(filp);
3682 if (ret)
3683 return ret;
3684
3685 f2fs_balance_fs(sbi, true);
3686
3687 inode_lock(inode);
3688
3689 writecount = atomic_read(&inode->i_writecount);
3690 if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
3691 (!(filp->f_mode & FMODE_WRITE) && writecount)) {
3692 ret = -EBUSY;
3693 goto out;
3694 }
3695
3696 if (!f2fs_compressed_file(inode) ||
3697 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3698 ret = -EINVAL;
3699 goto out;
3700 }
3701
3702 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
3703 if (ret)
3704 goto out;
3705
3706 if (!atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3707 ret = -EPERM;
3708 goto out;
3709 }
3710
3711 set_inode_flag(inode, FI_COMPRESS_RELEASED);
3712 inode_set_ctime_current(inode);
3713 f2fs_mark_inode_dirty_sync(inode, true);
3714
3715 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3716 filemap_invalidate_lock(inode->i_mapping);
3717
3718 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3719
3720 while (page_idx < last_idx) {
3721 struct dnode_of_data dn;
3722 pgoff_t end_offset, count;
3723
3724 f2fs_lock_op(sbi);
3725
3726 set_new_dnode(&dn, inode, NULL, NULL, 0);
3727 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3728 if (ret) {
3729 f2fs_unlock_op(sbi);
3730 if (ret == -ENOENT) {
3731 page_idx = f2fs_get_next_page_offset(&dn,
3732 page_idx);
3733 ret = 0;
3734 continue;
3735 }
3736 break;
3737 }
3738
3739 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3740 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3741 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3742
3743 ret = release_compress_blocks(&dn, count);
3744
3745 f2fs_put_dnode(&dn);
3746
3747 f2fs_unlock_op(sbi);
3748
3749 if (ret < 0)
3750 break;
3751
3752 page_idx += count;
3753 released_blocks += ret;
3754 }
3755
3756 filemap_invalidate_unlock(inode->i_mapping);
3757 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3758 out:
3759 inode_unlock(inode);
3760
3761 mnt_drop_write_file(filp);
3762
3763 if (ret >= 0) {
3764 ret = put_user(released_blocks, (u64 __user *)arg);
3765 } else if (released_blocks &&
3766 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3767 set_sbi_flag(sbi, SBI_NEED_FSCK);
3768 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3769 "iblocks=%llu, released=%u, compr_blocks=%u, "
3770 "run fsck to fix.",
3771 __func__, inode->i_ino, inode->i_blocks,
3772 released_blocks,
3773 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3774 }
3775
3776 return ret;
3777 }
3778
reserve_compress_blocks(struct dnode_of_data * dn,pgoff_t count,unsigned int * reserved_blocks)3779 static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
3780 unsigned int *reserved_blocks)
3781 {
3782 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
3783 int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
3784 block_t blkaddr;
3785 int i;
3786
3787 for (i = 0; i < count; i++) {
3788 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3789 dn->ofs_in_node + i);
3790
3791 if (!__is_valid_data_blkaddr(blkaddr))
3792 continue;
3793 if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
3794 DATA_GENERIC_ENHANCE))) {
3795 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
3796 return -EFSCORRUPTED;
3797 }
3798 }
3799
3800 while (count) {
3801 int compr_blocks = 0;
3802 blkcnt_t reserved = 0;
3803 blkcnt_t to_reserved;
3804 int ret;
3805
3806 for (i = 0; i < cluster_size; i++) {
3807 blkaddr = data_blkaddr(dn->inode, dn->node_page,
3808 dn->ofs_in_node + i);
3809
3810 if (i == 0) {
3811 if (blkaddr != COMPRESS_ADDR) {
3812 dn->ofs_in_node += cluster_size;
3813 goto next;
3814 }
3815 continue;
3816 }
3817
3818 /*
3819 * compressed cluster was not released due to it
3820 * fails in release_compress_blocks(), so NEW_ADDR
3821 * is a possible case.
3822 */
3823 if (blkaddr == NEW_ADDR) {
3824 reserved++;
3825 continue;
3826 }
3827 if (__is_valid_data_blkaddr(blkaddr)) {
3828 compr_blocks++;
3829 continue;
3830 }
3831 }
3832
3833 to_reserved = cluster_size - compr_blocks - reserved;
3834
3835 /* for the case all blocks in cluster were reserved */
3836 if (reserved && to_reserved == 1) {
3837 dn->ofs_in_node += cluster_size;
3838 goto next;
3839 }
3840
3841 ret = inc_valid_block_count(sbi, dn->inode,
3842 &to_reserved, false);
3843 if (unlikely(ret))
3844 return ret;
3845
3846 for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
3847 if (f2fs_data_blkaddr(dn) == NULL_ADDR)
3848 f2fs_set_data_blkaddr(dn, NEW_ADDR);
3849 }
3850
3851 f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
3852
3853 *reserved_blocks += to_reserved;
3854 next:
3855 count -= cluster_size;
3856 }
3857
3858 return 0;
3859 }
3860
f2fs_reserve_compress_blocks(struct file * filp,unsigned long arg)3861 static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
3862 {
3863 struct inode *inode = file_inode(filp);
3864 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3865 pgoff_t page_idx = 0, last_idx;
3866 unsigned int reserved_blocks = 0;
3867 int ret;
3868
3869 if (!f2fs_sb_has_compression(sbi))
3870 return -EOPNOTSUPP;
3871
3872 if (f2fs_readonly(sbi->sb))
3873 return -EROFS;
3874
3875 ret = mnt_want_write_file(filp);
3876 if (ret)
3877 return ret;
3878
3879 f2fs_balance_fs(sbi, true);
3880
3881 inode_lock(inode);
3882
3883 if (!f2fs_compressed_file(inode) ||
3884 !is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
3885 ret = -EINVAL;
3886 goto unlock_inode;
3887 }
3888
3889 if (atomic_read(&F2FS_I(inode)->i_compr_blocks))
3890 goto unlock_inode;
3891
3892 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3893 filemap_invalidate_lock(inode->i_mapping);
3894
3895 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3896
3897 while (page_idx < last_idx) {
3898 struct dnode_of_data dn;
3899 pgoff_t end_offset, count;
3900
3901 f2fs_lock_op(sbi);
3902
3903 set_new_dnode(&dn, inode, NULL, NULL, 0);
3904 ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
3905 if (ret) {
3906 f2fs_unlock_op(sbi);
3907 if (ret == -ENOENT) {
3908 page_idx = f2fs_get_next_page_offset(&dn,
3909 page_idx);
3910 ret = 0;
3911 continue;
3912 }
3913 break;
3914 }
3915
3916 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
3917 count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
3918 count = round_up(count, F2FS_I(inode)->i_cluster_size);
3919
3920 ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
3921
3922 f2fs_put_dnode(&dn);
3923
3924 f2fs_unlock_op(sbi);
3925
3926 if (ret < 0)
3927 break;
3928
3929 page_idx += count;
3930 }
3931
3932 filemap_invalidate_unlock(inode->i_mapping);
3933 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3934
3935 if (!ret) {
3936 clear_inode_flag(inode, FI_COMPRESS_RELEASED);
3937 inode_set_ctime_current(inode);
3938 f2fs_mark_inode_dirty_sync(inode, true);
3939 }
3940 unlock_inode:
3941 inode_unlock(inode);
3942 mnt_drop_write_file(filp);
3943
3944 if (!ret) {
3945 ret = put_user(reserved_blocks, (u64 __user *)arg);
3946 } else if (reserved_blocks &&
3947 atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
3948 set_sbi_flag(sbi, SBI_NEED_FSCK);
3949 f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
3950 "iblocks=%llu, reserved=%u, compr_blocks=%u, "
3951 "run fsck to fix.",
3952 __func__, inode->i_ino, inode->i_blocks,
3953 reserved_blocks,
3954 atomic_read(&F2FS_I(inode)->i_compr_blocks));
3955 }
3956
3957 return ret;
3958 }
3959
f2fs_secure_erase(struct block_device * bdev,struct inode * inode,pgoff_t off,block_t block,block_t len,u32 flags)3960 static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
3961 pgoff_t off, block_t block, block_t len, u32 flags)
3962 {
3963 sector_t sector = SECTOR_FROM_BLOCK(block);
3964 sector_t nr_sects = SECTOR_FROM_BLOCK(len);
3965 int ret = 0;
3966
3967 if (flags & F2FS_TRIM_FILE_DISCARD) {
3968 if (bdev_max_secure_erase_sectors(bdev))
3969 ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
3970 GFP_NOFS);
3971 else
3972 ret = blkdev_issue_discard(bdev, sector, nr_sects,
3973 GFP_NOFS);
3974 }
3975
3976 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
3977 if (IS_ENCRYPTED(inode))
3978 ret = fscrypt_zeroout_range(inode, off, block, len);
3979 else
3980 ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
3981 GFP_NOFS, 0);
3982 }
3983
3984 return ret;
3985 }
3986
f2fs_sec_trim_file(struct file * filp,unsigned long arg)3987 static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
3988 {
3989 struct inode *inode = file_inode(filp);
3990 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3991 struct address_space *mapping = inode->i_mapping;
3992 struct block_device *prev_bdev = NULL;
3993 struct f2fs_sectrim_range range;
3994 pgoff_t index, pg_end, prev_index = 0;
3995 block_t prev_block = 0, len = 0;
3996 loff_t end_addr;
3997 bool to_end = false;
3998 int ret = 0;
3999
4000 if (!(filp->f_mode & FMODE_WRITE))
4001 return -EBADF;
4002
4003 if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
4004 sizeof(range)))
4005 return -EFAULT;
4006
4007 if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
4008 !S_ISREG(inode->i_mode))
4009 return -EINVAL;
4010
4011 if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
4012 !f2fs_hw_support_discard(sbi)) ||
4013 ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
4014 IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
4015 return -EOPNOTSUPP;
4016
4017 file_start_write(filp);
4018 inode_lock(inode);
4019
4020 if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
4021 range.start >= inode->i_size) {
4022 ret = -EINVAL;
4023 goto err;
4024 }
4025
4026 if (range.len == 0)
4027 goto err;
4028
4029 if (inode->i_size - range.start > range.len) {
4030 end_addr = range.start + range.len;
4031 } else {
4032 end_addr = range.len == (u64)-1 ?
4033 sbi->sb->s_maxbytes : inode->i_size;
4034 to_end = true;
4035 }
4036
4037 if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
4038 (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
4039 ret = -EINVAL;
4040 goto err;
4041 }
4042
4043 index = F2FS_BYTES_TO_BLK(range.start);
4044 pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
4045
4046 ret = f2fs_convert_inline_inode(inode);
4047 if (ret)
4048 goto err;
4049
4050 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4051 filemap_invalidate_lock(mapping);
4052
4053 ret = filemap_write_and_wait_range(mapping, range.start,
4054 to_end ? LLONG_MAX : end_addr - 1);
4055 if (ret)
4056 goto out;
4057
4058 truncate_inode_pages_range(mapping, range.start,
4059 to_end ? -1 : end_addr - 1);
4060
4061 while (index < pg_end) {
4062 struct dnode_of_data dn;
4063 pgoff_t end_offset, count;
4064 int i;
4065
4066 set_new_dnode(&dn, inode, NULL, NULL, 0);
4067 ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
4068 if (ret) {
4069 if (ret == -ENOENT) {
4070 index = f2fs_get_next_page_offset(&dn, index);
4071 continue;
4072 }
4073 goto out;
4074 }
4075
4076 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
4077 count = min(end_offset - dn.ofs_in_node, pg_end - index);
4078 for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
4079 struct block_device *cur_bdev;
4080 block_t blkaddr = f2fs_data_blkaddr(&dn);
4081
4082 if (!__is_valid_data_blkaddr(blkaddr))
4083 continue;
4084
4085 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
4086 DATA_GENERIC_ENHANCE)) {
4087 ret = -EFSCORRUPTED;
4088 f2fs_put_dnode(&dn);
4089 f2fs_handle_error(sbi,
4090 ERROR_INVALID_BLKADDR);
4091 goto out;
4092 }
4093
4094 cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
4095 if (f2fs_is_multi_device(sbi)) {
4096 int di = f2fs_target_device_index(sbi, blkaddr);
4097
4098 blkaddr -= FDEV(di).start_blk;
4099 }
4100
4101 if (len) {
4102 if (prev_bdev == cur_bdev &&
4103 index == prev_index + len &&
4104 blkaddr == prev_block + len) {
4105 len++;
4106 } else {
4107 ret = f2fs_secure_erase(prev_bdev,
4108 inode, prev_index, prev_block,
4109 len, range.flags);
4110 if (ret) {
4111 f2fs_put_dnode(&dn);
4112 goto out;
4113 }
4114
4115 len = 0;
4116 }
4117 }
4118
4119 if (!len) {
4120 prev_bdev = cur_bdev;
4121 prev_index = index;
4122 prev_block = blkaddr;
4123 len = 1;
4124 }
4125 }
4126
4127 f2fs_put_dnode(&dn);
4128
4129 if (fatal_signal_pending(current)) {
4130 ret = -EINTR;
4131 goto out;
4132 }
4133 cond_resched();
4134 }
4135
4136 if (len)
4137 ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
4138 prev_block, len, range.flags);
4139 out:
4140 filemap_invalidate_unlock(mapping);
4141 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4142 err:
4143 inode_unlock(inode);
4144 file_end_write(filp);
4145
4146 return ret;
4147 }
4148
f2fs_ioc_get_compress_option(struct file * filp,unsigned long arg)4149 static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
4150 {
4151 struct inode *inode = file_inode(filp);
4152 struct f2fs_comp_option option;
4153
4154 if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
4155 return -EOPNOTSUPP;
4156
4157 inode_lock_shared(inode);
4158
4159 if (!f2fs_compressed_file(inode)) {
4160 inode_unlock_shared(inode);
4161 return -ENODATA;
4162 }
4163
4164 option.algorithm = F2FS_I(inode)->i_compress_algorithm;
4165 option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
4166
4167 inode_unlock_shared(inode);
4168
4169 if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
4170 sizeof(option)))
4171 return -EFAULT;
4172
4173 return 0;
4174 }
4175
f2fs_ioc_set_compress_option(struct file * filp,unsigned long arg)4176 static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
4177 {
4178 struct inode *inode = file_inode(filp);
4179 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4180 struct f2fs_comp_option option;
4181 int ret = 0;
4182
4183 if (!f2fs_sb_has_compression(sbi))
4184 return -EOPNOTSUPP;
4185
4186 if (!(filp->f_mode & FMODE_WRITE))
4187 return -EBADF;
4188
4189 if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
4190 sizeof(option)))
4191 return -EFAULT;
4192
4193 if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
4194 option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
4195 option.algorithm >= COMPRESS_MAX)
4196 return -EINVAL;
4197
4198 file_start_write(filp);
4199 inode_lock(inode);
4200
4201 f2fs_down_write(&F2FS_I(inode)->i_sem);
4202 if (!f2fs_compressed_file(inode)) {
4203 ret = -EINVAL;
4204 goto out;
4205 }
4206
4207 if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
4208 ret = -EBUSY;
4209 goto out;
4210 }
4211
4212 if (F2FS_HAS_BLOCKS(inode)) {
4213 ret = -EFBIG;
4214 goto out;
4215 }
4216
4217 F2FS_I(inode)->i_compress_algorithm = option.algorithm;
4218 F2FS_I(inode)->i_log_cluster_size = option.log_cluster_size;
4219 F2FS_I(inode)->i_cluster_size = BIT(option.log_cluster_size);
4220 /* Set default level */
4221 if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD)
4222 F2FS_I(inode)->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
4223 else
4224 F2FS_I(inode)->i_compress_level = 0;
4225 /* Adjust mount option level */
4226 if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
4227 F2FS_OPTION(sbi).compress_level)
4228 F2FS_I(inode)->i_compress_level = F2FS_OPTION(sbi).compress_level;
4229 f2fs_mark_inode_dirty_sync(inode, true);
4230
4231 if (!f2fs_is_compress_backend_ready(inode))
4232 f2fs_warn(sbi, "compression algorithm is successfully set, "
4233 "but current kernel doesn't support this algorithm.");
4234 out:
4235 f2fs_up_write(&F2FS_I(inode)->i_sem);
4236 inode_unlock(inode);
4237 file_end_write(filp);
4238
4239 return ret;
4240 }
4241
redirty_blocks(struct inode * inode,pgoff_t page_idx,int len)4242 static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
4243 {
4244 DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
4245 struct address_space *mapping = inode->i_mapping;
4246 struct page *page;
4247 pgoff_t redirty_idx = page_idx;
4248 int i, page_len = 0, ret = 0;
4249
4250 page_cache_ra_unbounded(&ractl, len, 0);
4251
4252 for (i = 0; i < len; i++, page_idx++) {
4253 page = read_cache_page(mapping, page_idx, NULL, NULL);
4254 if (IS_ERR(page)) {
4255 ret = PTR_ERR(page);
4256 break;
4257 }
4258 page_len++;
4259 }
4260
4261 for (i = 0; i < page_len; i++, redirty_idx++) {
4262 page = find_lock_page(mapping, redirty_idx);
4263
4264 /* It will never fail, when page has pinned above */
4265 f2fs_bug_on(F2FS_I_SB(inode), !page);
4266
4267 f2fs_wait_on_page_writeback(page, DATA, true, true);
4268
4269 set_page_dirty(page);
4270 set_page_private_gcing(page);
4271 f2fs_put_page(page, 1);
4272 f2fs_put_page(page, 0);
4273 }
4274
4275 return ret;
4276 }
4277
f2fs_ioc_decompress_file(struct file * filp)4278 static int f2fs_ioc_decompress_file(struct file *filp)
4279 {
4280 struct inode *inode = file_inode(filp);
4281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4282 struct f2fs_inode_info *fi = F2FS_I(inode);
4283 pgoff_t page_idx = 0, last_idx, cluster_idx;
4284 int ret;
4285
4286 if (!f2fs_sb_has_compression(sbi) ||
4287 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4288 return -EOPNOTSUPP;
4289
4290 if (!(filp->f_mode & FMODE_WRITE))
4291 return -EBADF;
4292
4293 f2fs_balance_fs(sbi, true);
4294
4295 file_start_write(filp);
4296 inode_lock(inode);
4297
4298 if (!f2fs_is_compress_backend_ready(inode)) {
4299 ret = -EOPNOTSUPP;
4300 goto out;
4301 }
4302
4303 if (!f2fs_compressed_file(inode) ||
4304 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4305 ret = -EINVAL;
4306 goto out;
4307 }
4308
4309 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4310 if (ret)
4311 goto out;
4312
4313 if (!atomic_read(&fi->i_compr_blocks))
4314 goto out;
4315
4316 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4317 last_idx >>= fi->i_log_cluster_size;
4318
4319 for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4320 page_idx = cluster_idx << fi->i_log_cluster_size;
4321
4322 if (!f2fs_is_compressed_cluster(inode, page_idx))
4323 continue;
4324
4325 ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4326 if (ret < 0)
4327 break;
4328
4329 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4330 ret = filemap_fdatawrite(inode->i_mapping);
4331 if (ret < 0)
4332 break;
4333 }
4334
4335 cond_resched();
4336 if (fatal_signal_pending(current)) {
4337 ret = -EINTR;
4338 break;
4339 }
4340 }
4341
4342 if (!ret)
4343 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4344 LLONG_MAX);
4345
4346 if (ret)
4347 f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
4348 __func__, ret);
4349 out:
4350 inode_unlock(inode);
4351 file_end_write(filp);
4352
4353 return ret;
4354 }
4355
f2fs_ioc_compress_file(struct file * filp)4356 static int f2fs_ioc_compress_file(struct file *filp)
4357 {
4358 struct inode *inode = file_inode(filp);
4359 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4360 struct f2fs_inode_info *fi = F2FS_I(inode);
4361 pgoff_t page_idx = 0, last_idx, cluster_idx;
4362 int ret;
4363
4364 if (!f2fs_sb_has_compression(sbi) ||
4365 F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
4366 return -EOPNOTSUPP;
4367
4368 if (!(filp->f_mode & FMODE_WRITE))
4369 return -EBADF;
4370
4371 f2fs_balance_fs(sbi, true);
4372
4373 file_start_write(filp);
4374 inode_lock(inode);
4375
4376 if (!f2fs_is_compress_backend_ready(inode)) {
4377 ret = -EOPNOTSUPP;
4378 goto out;
4379 }
4380
4381 if (!f2fs_compressed_file(inode) ||
4382 is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
4383 ret = -EINVAL;
4384 goto out;
4385 }
4386
4387 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
4388 if (ret)
4389 goto out;
4390
4391 set_inode_flag(inode, FI_ENABLE_COMPRESS);
4392
4393 last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
4394 last_idx >>= fi->i_log_cluster_size;
4395
4396 for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
4397 page_idx = cluster_idx << fi->i_log_cluster_size;
4398
4399 if (f2fs_is_sparse_cluster(inode, page_idx))
4400 continue;
4401
4402 ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
4403 if (ret < 0)
4404 break;
4405
4406 if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
4407 ret = filemap_fdatawrite(inode->i_mapping);
4408 if (ret < 0)
4409 break;
4410 }
4411
4412 cond_resched();
4413 if (fatal_signal_pending(current)) {
4414 ret = -EINTR;
4415 break;
4416 }
4417 }
4418
4419 if (!ret)
4420 ret = filemap_write_and_wait_range(inode->i_mapping, 0,
4421 LLONG_MAX);
4422
4423 clear_inode_flag(inode, FI_ENABLE_COMPRESS);
4424
4425 if (ret)
4426 f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
4427 __func__, ret);
4428 out:
4429 inode_unlock(inode);
4430 file_end_write(filp);
4431
4432 return ret;
4433 }
4434
__f2fs_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)4435 static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4436 {
4437 switch (cmd) {
4438 case FS_IOC_GETVERSION:
4439 return f2fs_ioc_getversion(filp, arg);
4440 case F2FS_IOC_START_ATOMIC_WRITE:
4441 return f2fs_ioc_start_atomic_write(filp, false);
4442 case F2FS_IOC_START_ATOMIC_REPLACE:
4443 return f2fs_ioc_start_atomic_write(filp, true);
4444 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
4445 return f2fs_ioc_commit_atomic_write(filp);
4446 case F2FS_IOC_ABORT_ATOMIC_WRITE:
4447 return f2fs_ioc_abort_atomic_write(filp);
4448 case F2FS_IOC_START_VOLATILE_WRITE:
4449 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
4450 return -EOPNOTSUPP;
4451 case F2FS_IOC_SHUTDOWN:
4452 return f2fs_ioc_shutdown(filp, arg);
4453 case FITRIM:
4454 return f2fs_ioc_fitrim(filp, arg);
4455 case FS_IOC_SET_ENCRYPTION_POLICY:
4456 return f2fs_ioc_set_encryption_policy(filp, arg);
4457 case FS_IOC_GET_ENCRYPTION_POLICY:
4458 return f2fs_ioc_get_encryption_policy(filp, arg);
4459 case FS_IOC_GET_ENCRYPTION_PWSALT:
4460 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
4461 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
4462 return f2fs_ioc_get_encryption_policy_ex(filp, arg);
4463 case FS_IOC_ADD_ENCRYPTION_KEY:
4464 return f2fs_ioc_add_encryption_key(filp, arg);
4465 case FS_IOC_REMOVE_ENCRYPTION_KEY:
4466 return f2fs_ioc_remove_encryption_key(filp, arg);
4467 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4468 return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
4469 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
4470 return f2fs_ioc_get_encryption_key_status(filp, arg);
4471 case FS_IOC_GET_ENCRYPTION_NONCE:
4472 return f2fs_ioc_get_encryption_nonce(filp, arg);
4473 case F2FS_IOC_GARBAGE_COLLECT:
4474 return f2fs_ioc_gc(filp, arg);
4475 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
4476 return f2fs_ioc_gc_range(filp, arg);
4477 case F2FS_IOC_WRITE_CHECKPOINT:
4478 return f2fs_ioc_write_checkpoint(filp);
4479 case F2FS_IOC_DEFRAGMENT:
4480 return f2fs_ioc_defragment(filp, arg);
4481 case F2FS_IOC_MOVE_RANGE:
4482 return f2fs_ioc_move_range(filp, arg);
4483 case F2FS_IOC_FLUSH_DEVICE:
4484 return f2fs_ioc_flush_device(filp, arg);
4485 case F2FS_IOC_GET_FEATURES:
4486 return f2fs_ioc_get_features(filp, arg);
4487 case F2FS_IOC_GET_PIN_FILE:
4488 return f2fs_ioc_get_pin_file(filp, arg);
4489 case F2FS_IOC_SET_PIN_FILE:
4490 return f2fs_ioc_set_pin_file(filp, arg);
4491 case F2FS_IOC_PRECACHE_EXTENTS:
4492 return f2fs_ioc_precache_extents(filp);
4493 case F2FS_IOC_RESIZE_FS:
4494 return f2fs_ioc_resize_fs(filp, arg);
4495 case FS_IOC_ENABLE_VERITY:
4496 return f2fs_ioc_enable_verity(filp, arg);
4497 case FS_IOC_ENABLE_CODE_SIGN:
4498 return f2fs_ioc_enable_code_sign(filp, arg);
4499 case FS_IOC_MEASURE_VERITY:
4500 return f2fs_ioc_measure_verity(filp, arg);
4501 case FS_IOC_READ_VERITY_METADATA:
4502 return f2fs_ioc_read_verity_metadata(filp, arg);
4503 case FS_IOC_GETFSLABEL:
4504 return f2fs_ioc_getfslabel(filp, arg);
4505 case FS_IOC_SETFSLABEL:
4506 return f2fs_ioc_setfslabel(filp, arg);
4507 case F2FS_IOC_GET_COMPRESS_BLOCKS:
4508 return f2fs_ioc_get_compress_blocks(filp, arg);
4509 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
4510 return f2fs_release_compress_blocks(filp, arg);
4511 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
4512 return f2fs_reserve_compress_blocks(filp, arg);
4513 case F2FS_IOC_SEC_TRIM_FILE:
4514 return f2fs_sec_trim_file(filp, arg);
4515 case F2FS_IOC_GET_COMPRESS_OPTION:
4516 return f2fs_ioc_get_compress_option(filp, arg);
4517 case F2FS_IOC_SET_COMPRESS_OPTION:
4518 return f2fs_ioc_set_compress_option(filp, arg);
4519 case F2FS_IOC_DECOMPRESS_FILE:
4520 return f2fs_ioc_decompress_file(filp);
4521 case F2FS_IOC_COMPRESS_FILE:
4522 return f2fs_ioc_compress_file(filp);
4523 default:
4524 return -ENOTTY;
4525 }
4526 }
4527
f2fs_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)4528 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4529 {
4530 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
4531 return -EIO;
4532 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
4533 return -ENOSPC;
4534
4535 return __f2fs_ioctl(filp, cmd, arg);
4536 }
4537
4538 /*
4539 * Return %true if the given read or write request should use direct I/O, or
4540 * %false if it should use buffered I/O.
4541 */
f2fs_should_use_dio(struct inode * inode,struct kiocb * iocb,struct iov_iter * iter)4542 static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
4543 struct iov_iter *iter)
4544 {
4545 unsigned int align;
4546
4547 if (!(iocb->ki_flags & IOCB_DIRECT))
4548 return false;
4549
4550 if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
4551 return false;
4552
4553 /*
4554 * Direct I/O not aligned to the disk's logical_block_size will be
4555 * attempted, but will fail with -EINVAL.
4556 *
4557 * f2fs additionally requires that direct I/O be aligned to the
4558 * filesystem block size, which is often a stricter requirement.
4559 * However, f2fs traditionally falls back to buffered I/O on requests
4560 * that are logical_block_size-aligned but not fs-block aligned.
4561 *
4562 * The below logic implements this behavior.
4563 */
4564 align = iocb->ki_pos | iov_iter_alignment(iter);
4565 if (!IS_ALIGNED(align, i_blocksize(inode)) &&
4566 IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
4567 return false;
4568
4569 return true;
4570 }
4571
f2fs_dio_read_end_io(struct kiocb * iocb,ssize_t size,int error,unsigned int flags)4572 static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
4573 unsigned int flags)
4574 {
4575 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4576
4577 dec_page_count(sbi, F2FS_DIO_READ);
4578 if (error)
4579 return error;
4580 f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
4581 return 0;
4582 }
4583
4584 static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
4585 .end_io = f2fs_dio_read_end_io,
4586 };
4587
f2fs_dio_read_iter(struct kiocb * iocb,struct iov_iter * to)4588 static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
4589 {
4590 struct file *file = iocb->ki_filp;
4591 struct inode *inode = file_inode(file);
4592 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4593 struct f2fs_inode_info *fi = F2FS_I(inode);
4594 const loff_t pos = iocb->ki_pos;
4595 const size_t count = iov_iter_count(to);
4596 struct iomap_dio *dio;
4597 ssize_t ret;
4598
4599 if (count == 0)
4600 return 0; /* skip atime update */
4601
4602 trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
4603
4604 if (iocb->ki_flags & IOCB_NOWAIT) {
4605 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4606 ret = -EAGAIN;
4607 goto out;
4608 }
4609 } else {
4610 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4611 }
4612
4613 /*
4614 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4615 * the higher-level function iomap_dio_rw() in order to ensure that the
4616 * F2FS_DIO_READ counter will be decremented correctly in all cases.
4617 */
4618 inc_page_count(sbi, F2FS_DIO_READ);
4619 dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
4620 &f2fs_iomap_dio_read_ops, 0, NULL, 0);
4621 if (IS_ERR_OR_NULL(dio)) {
4622 ret = PTR_ERR_OR_ZERO(dio);
4623 if (ret != -EIOCBQUEUED)
4624 dec_page_count(sbi, F2FS_DIO_READ);
4625 } else {
4626 ret = iomap_dio_complete(dio);
4627 }
4628
4629 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4630
4631 file_accessed(file);
4632 out:
4633 trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
4634 return ret;
4635 }
4636
f2fs_trace_rw_file_path(struct file * file,loff_t pos,size_t count,int rw)4637 static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
4638 int rw)
4639 {
4640 struct inode *inode = file_inode(file);
4641 char *buf, *path;
4642
4643 buf = f2fs_getname(F2FS_I_SB(inode));
4644 if (!buf)
4645 return;
4646 path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
4647 if (IS_ERR(path))
4648 goto free_buf;
4649 if (rw == WRITE)
4650 trace_f2fs_datawrite_start(inode, pos, count,
4651 current->pid, path, current->comm);
4652 else
4653 trace_f2fs_dataread_start(inode, pos, count,
4654 current->pid, path, current->comm);
4655 free_buf:
4656 f2fs_putname(buf);
4657 }
4658
f2fs_file_read_iter(struct kiocb * iocb,struct iov_iter * to)4659 static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
4660 {
4661 struct inode *inode = file_inode(iocb->ki_filp);
4662 const loff_t pos = iocb->ki_pos;
4663 ssize_t ret;
4664
4665 if (!f2fs_is_compress_backend_ready(inode))
4666 return -EOPNOTSUPP;
4667
4668 if (trace_f2fs_dataread_start_enabled())
4669 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
4670 iov_iter_count(to), READ);
4671
4672 /* In LFS mode, if there is inflight dio, wait for its completion */
4673 if (f2fs_lfs_mode(F2FS_I_SB(inode)))
4674 inode_dio_wait(inode);
4675
4676 if (f2fs_should_use_dio(inode, iocb, to)) {
4677 ret = f2fs_dio_read_iter(iocb, to);
4678 } else {
4679 ret = filemap_read(iocb, to, 0);
4680 if (ret > 0)
4681 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4682 APP_BUFFERED_READ_IO, ret);
4683 }
4684 if (trace_f2fs_dataread_end_enabled())
4685 trace_f2fs_dataread_end(inode, pos, ret);
4686 return ret;
4687 }
4688
f2fs_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)4689 static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
4690 struct pipe_inode_info *pipe,
4691 size_t len, unsigned int flags)
4692 {
4693 struct inode *inode = file_inode(in);
4694 const loff_t pos = *ppos;
4695 ssize_t ret;
4696
4697 if (!f2fs_is_compress_backend_ready(inode))
4698 return -EOPNOTSUPP;
4699
4700 if (trace_f2fs_dataread_start_enabled())
4701 f2fs_trace_rw_file_path(in, pos, len, READ);
4702
4703 ret = filemap_splice_read(in, ppos, pipe, len, flags);
4704 if (ret > 0)
4705 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4706 APP_BUFFERED_READ_IO, ret);
4707
4708 if (trace_f2fs_dataread_end_enabled())
4709 trace_f2fs_dataread_end(inode, pos, ret);
4710 return ret;
4711 }
4712
f2fs_write_checks(struct kiocb * iocb,struct iov_iter * from)4713 static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
4714 {
4715 struct file *file = iocb->ki_filp;
4716 struct inode *inode = file_inode(file);
4717 ssize_t count;
4718 int err;
4719
4720 if (IS_IMMUTABLE(inode))
4721 return -EPERM;
4722
4723 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
4724 return -EPERM;
4725
4726 count = generic_write_checks(iocb, from);
4727 if (count <= 0)
4728 return count;
4729
4730 err = file_modified(file);
4731 if (err)
4732 return err;
4733
4734 filemap_invalidate_lock(inode->i_mapping);
4735 f2fs_zero_post_eof_page(inode, iocb->ki_pos + iov_iter_count(from));
4736 filemap_invalidate_unlock(inode->i_mapping);
4737 return count;
4738 }
4739
4740 /*
4741 * Preallocate blocks for a write request, if it is possible and helpful to do
4742 * so. Returns a positive number if blocks may have been preallocated, 0 if no
4743 * blocks were preallocated, or a negative errno value if something went
4744 * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4745 * requested blocks (not just some of them) have been allocated.
4746 */
f2fs_preallocate_blocks(struct kiocb * iocb,struct iov_iter * iter,bool dio)4747 static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
4748 bool dio)
4749 {
4750 struct inode *inode = file_inode(iocb->ki_filp);
4751 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4752 const loff_t pos = iocb->ki_pos;
4753 const size_t count = iov_iter_count(iter);
4754 struct f2fs_map_blocks map = {};
4755 int flag;
4756 int ret;
4757
4758 /* If it will be an out-of-place direct write, don't bother. */
4759 if (dio && f2fs_lfs_mode(sbi))
4760 return 0;
4761 /*
4762 * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
4763 * buffered IO, if DIO meets any holes.
4764 */
4765 if (dio && i_size_read(inode) &&
4766 (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
4767 return 0;
4768
4769 /* No-wait I/O can't allocate blocks. */
4770 if (iocb->ki_flags & IOCB_NOWAIT)
4771 return 0;
4772
4773 /* If it will be a short write, don't bother. */
4774 if (fault_in_iov_iter_readable(iter, count))
4775 return 0;
4776
4777 if (f2fs_has_inline_data(inode)) {
4778 /* If the data will fit inline, don't bother. */
4779 if (pos + count <= MAX_INLINE_DATA(inode))
4780 return 0;
4781 ret = f2fs_convert_inline_inode(inode);
4782 if (ret)
4783 return ret;
4784 }
4785
4786 /* Do not preallocate blocks that will be written partially in 4KB. */
4787 map.m_lblk = F2FS_BLK_ALIGN(pos);
4788 map.m_len = F2FS_BYTES_TO_BLK(pos + count);
4789 if (map.m_len > map.m_lblk)
4790 map.m_len -= map.m_lblk;
4791 else
4792 map.m_len = 0;
4793 map.m_may_create = true;
4794 if (dio) {
4795 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4796 flag = F2FS_GET_BLOCK_PRE_DIO;
4797 } else {
4798 map.m_seg_type = NO_CHECK_TYPE;
4799 flag = F2FS_GET_BLOCK_PRE_AIO;
4800 }
4801
4802 ret = f2fs_map_blocks(inode, &map, flag);
4803 /* -ENOSPC|-EDQUOT are fine to report the number of allocated blocks. */
4804 if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
4805 return ret;
4806 if (ret == 0)
4807 set_inode_flag(inode, FI_PREALLOCATED_ALL);
4808 return map.m_len;
4809 }
4810
f2fs_buffered_write_iter(struct kiocb * iocb,struct iov_iter * from)4811 static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
4812 struct iov_iter *from)
4813 {
4814 struct file *file = iocb->ki_filp;
4815 struct inode *inode = file_inode(file);
4816 ssize_t ret;
4817
4818 if (iocb->ki_flags & IOCB_NOWAIT)
4819 return -EOPNOTSUPP;
4820
4821 ret = generic_perform_write(iocb, from);
4822
4823 if (ret > 0) {
4824 f2fs_update_iostat(F2FS_I_SB(inode), inode,
4825 APP_BUFFERED_IO, ret);
4826 }
4827 return ret;
4828 }
4829
f2fs_dio_write_end_io(struct kiocb * iocb,ssize_t size,int error,unsigned int flags)4830 static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
4831 unsigned int flags)
4832 {
4833 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
4834
4835 dec_page_count(sbi, F2FS_DIO_WRITE);
4836 if (error)
4837 return error;
4838 f2fs_update_time(sbi, REQ_TIME);
4839 f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
4840 return 0;
4841 }
4842
4843 static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
4844 .end_io = f2fs_dio_write_end_io,
4845 };
4846
f2fs_flush_buffered_write(struct address_space * mapping,loff_t start_pos,loff_t end_pos)4847 static void f2fs_flush_buffered_write(struct address_space *mapping,
4848 loff_t start_pos, loff_t end_pos)
4849 {
4850 int ret;
4851
4852 ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
4853 if (ret < 0)
4854 return;
4855 invalidate_mapping_pages(mapping,
4856 start_pos >> PAGE_SHIFT,
4857 end_pos >> PAGE_SHIFT);
4858 }
4859
f2fs_dio_write_iter(struct kiocb * iocb,struct iov_iter * from,bool * may_need_sync)4860 static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
4861 bool *may_need_sync)
4862 {
4863 struct file *file = iocb->ki_filp;
4864 struct inode *inode = file_inode(file);
4865 struct f2fs_inode_info *fi = F2FS_I(inode);
4866 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4867 const bool do_opu = f2fs_lfs_mode(sbi);
4868 const loff_t pos = iocb->ki_pos;
4869 const ssize_t count = iov_iter_count(from);
4870 unsigned int dio_flags;
4871 struct iomap_dio *dio;
4872 ssize_t ret;
4873
4874 trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
4875
4876 if (iocb->ki_flags & IOCB_NOWAIT) {
4877 /* f2fs_convert_inline_inode() and block allocation can block */
4878 if (f2fs_has_inline_data(inode) ||
4879 !f2fs_overwrite_io(inode, pos, count)) {
4880 ret = -EAGAIN;
4881 goto out;
4882 }
4883
4884 if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
4885 ret = -EAGAIN;
4886 goto out;
4887 }
4888 if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
4889 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4890 ret = -EAGAIN;
4891 goto out;
4892 }
4893 } else {
4894 ret = f2fs_convert_inline_inode(inode);
4895 if (ret)
4896 goto out;
4897
4898 f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
4899 if (do_opu)
4900 f2fs_down_read(&fi->i_gc_rwsem[READ]);
4901 }
4902
4903 /*
4904 * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
4905 * the higher-level function iomap_dio_rw() in order to ensure that the
4906 * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
4907 */
4908 inc_page_count(sbi, F2FS_DIO_WRITE);
4909 dio_flags = 0;
4910 if (pos + count > inode->i_size)
4911 dio_flags |= IOMAP_DIO_FORCE_WAIT;
4912 dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
4913 &f2fs_iomap_dio_write_ops, dio_flags, NULL, 0);
4914 if (IS_ERR_OR_NULL(dio)) {
4915 ret = PTR_ERR_OR_ZERO(dio);
4916 if (ret == -ENOTBLK)
4917 ret = 0;
4918 if (ret != -EIOCBQUEUED)
4919 dec_page_count(sbi, F2FS_DIO_WRITE);
4920 } else {
4921 ret = iomap_dio_complete(dio);
4922 }
4923
4924 if (do_opu)
4925 f2fs_up_read(&fi->i_gc_rwsem[READ]);
4926 f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
4927
4928 if (ret < 0)
4929 goto out;
4930 if (pos + ret > inode->i_size)
4931 f2fs_i_size_write(inode, pos + ret);
4932 if (!do_opu)
4933 set_inode_flag(inode, FI_UPDATE_WRITE);
4934
4935 if (iov_iter_count(from)) {
4936 ssize_t ret2;
4937 loff_t bufio_start_pos = iocb->ki_pos;
4938
4939 /*
4940 * The direct write was partial, so we need to fall back to a
4941 * buffered write for the remainder.
4942 */
4943
4944 ret2 = f2fs_buffered_write_iter(iocb, from);
4945 if (iov_iter_count(from))
4946 f2fs_write_failed(inode, iocb->ki_pos);
4947 if (ret2 < 0)
4948 goto out;
4949
4950 /*
4951 * Ensure that the pagecache pages are written to disk and
4952 * invalidated to preserve the expected O_DIRECT semantics.
4953 */
4954 if (ret2 > 0) {
4955 loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
4956
4957 ret += ret2;
4958
4959 f2fs_flush_buffered_write(file->f_mapping,
4960 bufio_start_pos,
4961 bufio_end_pos);
4962 }
4963 } else {
4964 /* iomap_dio_rw() already handled the generic_write_sync(). */
4965 *may_need_sync = false;
4966 }
4967 out:
4968 trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
4969 return ret;
4970 }
4971
f2fs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)4972 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4973 {
4974 struct inode *inode = file_inode(iocb->ki_filp);
4975 const loff_t orig_pos = iocb->ki_pos;
4976 const size_t orig_count = iov_iter_count(from);
4977 loff_t target_size;
4978 bool dio;
4979 bool may_need_sync = true;
4980 int preallocated;
4981 const loff_t pos = iocb->ki_pos;
4982 const ssize_t count = iov_iter_count(from);
4983 ssize_t ret;
4984
4985 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
4986 ret = -EIO;
4987 goto out;
4988 }
4989
4990 if (!f2fs_is_compress_backend_ready(inode)) {
4991 ret = -EOPNOTSUPP;
4992 goto out;
4993 }
4994
4995 if (iocb->ki_flags & IOCB_NOWAIT) {
4996 if (!inode_trylock(inode)) {
4997 ret = -EAGAIN;
4998 goto out;
4999 }
5000 } else {
5001 inode_lock(inode);
5002 }
5003
5004 if (f2fs_is_pinned_file(inode) &&
5005 !f2fs_overwrite_io(inode, pos, count)) {
5006 ret = -EIO;
5007 goto out_unlock;
5008 }
5009
5010 ret = f2fs_write_checks(iocb, from);
5011 if (ret <= 0)
5012 goto out_unlock;
5013
5014 /* Determine whether we will do a direct write or a buffered write. */
5015 dio = f2fs_should_use_dio(inode, iocb, from);
5016
5017 /* Possibly preallocate the blocks for the write. */
5018 target_size = iocb->ki_pos + iov_iter_count(from);
5019 preallocated = f2fs_preallocate_blocks(iocb, from, dio);
5020 if (preallocated < 0) {
5021 ret = preallocated;
5022 } else {
5023 if (trace_f2fs_datawrite_start_enabled())
5024 f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
5025 orig_count, WRITE);
5026
5027 /* Do the actual write. */
5028 ret = dio ?
5029 f2fs_dio_write_iter(iocb, from, &may_need_sync) :
5030 f2fs_buffered_write_iter(iocb, from);
5031
5032 if (trace_f2fs_datawrite_end_enabled())
5033 trace_f2fs_datawrite_end(inode, orig_pos, ret);
5034 }
5035
5036 /* Don't leave any preallocated blocks around past i_size. */
5037 if (preallocated && i_size_read(inode) < target_size) {
5038 f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5039 filemap_invalidate_lock(inode->i_mapping);
5040 if (!f2fs_truncate(inode))
5041 file_dont_truncate(inode);
5042 filemap_invalidate_unlock(inode->i_mapping);
5043 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
5044 } else {
5045 file_dont_truncate(inode);
5046 }
5047
5048 clear_inode_flag(inode, FI_PREALLOCATED_ALL);
5049 out_unlock:
5050 inode_unlock(inode);
5051 out:
5052 trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
5053
5054 if (ret > 0 && may_need_sync)
5055 ret = generic_write_sync(iocb, ret);
5056
5057 /* If buffered IO was forced, flush and drop the data from
5058 * the page cache to preserve O_DIRECT semantics
5059 */
5060 if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
5061 f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
5062 orig_pos,
5063 orig_pos + ret - 1);
5064
5065 return ret;
5066 }
5067
f2fs_file_fadvise(struct file * filp,loff_t offset,loff_t len,int advice)5068 static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
5069 int advice)
5070 {
5071 struct address_space *mapping;
5072 struct backing_dev_info *bdi;
5073 struct inode *inode = file_inode(filp);
5074 int err;
5075
5076 if (advice == POSIX_FADV_SEQUENTIAL) {
5077 if (S_ISFIFO(inode->i_mode))
5078 return -ESPIPE;
5079
5080 mapping = filp->f_mapping;
5081 if (!mapping || len < 0)
5082 return -EINVAL;
5083
5084 bdi = inode_to_bdi(mapping->host);
5085 filp->f_ra.ra_pages = bdi->ra_pages *
5086 F2FS_I_SB(inode)->seq_file_ra_mul;
5087 spin_lock(&filp->f_lock);
5088 filp->f_mode &= ~FMODE_RANDOM;
5089 spin_unlock(&filp->f_lock);
5090 return 0;
5091 }
5092
5093 err = generic_fadvise(filp, offset, len, advice);
5094 if (!err && advice == POSIX_FADV_DONTNEED &&
5095 test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
5096 f2fs_compressed_file(inode))
5097 f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
5098
5099 return err;
5100 }
5101
5102 #ifdef CONFIG_COMPAT
5103 struct compat_f2fs_gc_range {
5104 u32 sync;
5105 compat_u64 start;
5106 compat_u64 len;
5107 };
5108 #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
5109 struct compat_f2fs_gc_range)
5110
f2fs_compat_ioc_gc_range(struct file * file,unsigned long arg)5111 static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
5112 {
5113 struct compat_f2fs_gc_range __user *urange;
5114 struct f2fs_gc_range range;
5115 int err;
5116
5117 urange = compat_ptr(arg);
5118 err = get_user(range.sync, &urange->sync);
5119 err |= get_user(range.start, &urange->start);
5120 err |= get_user(range.len, &urange->len);
5121 if (err)
5122 return -EFAULT;
5123
5124 return __f2fs_ioc_gc_range(file, &range);
5125 }
5126
5127 struct compat_f2fs_move_range {
5128 u32 dst_fd;
5129 compat_u64 pos_in;
5130 compat_u64 pos_out;
5131 compat_u64 len;
5132 };
5133 #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
5134 struct compat_f2fs_move_range)
5135
f2fs_compat_ioc_move_range(struct file * file,unsigned long arg)5136 static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
5137 {
5138 struct compat_f2fs_move_range __user *urange;
5139 struct f2fs_move_range range;
5140 int err;
5141
5142 urange = compat_ptr(arg);
5143 err = get_user(range.dst_fd, &urange->dst_fd);
5144 err |= get_user(range.pos_in, &urange->pos_in);
5145 err |= get_user(range.pos_out, &urange->pos_out);
5146 err |= get_user(range.len, &urange->len);
5147 if (err)
5148 return -EFAULT;
5149
5150 return __f2fs_ioc_move_range(file, &range);
5151 }
5152
f2fs_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)5153 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5154 {
5155 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
5156 return -EIO;
5157 if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
5158 return -ENOSPC;
5159
5160 switch (cmd) {
5161 case FS_IOC32_GETVERSION:
5162 cmd = FS_IOC_GETVERSION;
5163 break;
5164 case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
5165 return f2fs_compat_ioc_gc_range(file, arg);
5166 case F2FS_IOC32_MOVE_RANGE:
5167 return f2fs_compat_ioc_move_range(file, arg);
5168 case F2FS_IOC_START_ATOMIC_WRITE:
5169 case F2FS_IOC_START_ATOMIC_REPLACE:
5170 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
5171 case F2FS_IOC_START_VOLATILE_WRITE:
5172 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
5173 case F2FS_IOC_ABORT_ATOMIC_WRITE:
5174 case F2FS_IOC_SHUTDOWN:
5175 case FITRIM:
5176 case FS_IOC_SET_ENCRYPTION_POLICY:
5177 case FS_IOC_GET_ENCRYPTION_PWSALT:
5178 case FS_IOC_GET_ENCRYPTION_POLICY:
5179 case FS_IOC_GET_ENCRYPTION_POLICY_EX:
5180 case FS_IOC_ADD_ENCRYPTION_KEY:
5181 case FS_IOC_REMOVE_ENCRYPTION_KEY:
5182 case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
5183 case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
5184 case FS_IOC_GET_ENCRYPTION_NONCE:
5185 case F2FS_IOC_GARBAGE_COLLECT:
5186 case F2FS_IOC_WRITE_CHECKPOINT:
5187 case F2FS_IOC_DEFRAGMENT:
5188 case F2FS_IOC_FLUSH_DEVICE:
5189 case F2FS_IOC_GET_FEATURES:
5190 case F2FS_IOC_GET_PIN_FILE:
5191 case F2FS_IOC_SET_PIN_FILE:
5192 case F2FS_IOC_PRECACHE_EXTENTS:
5193 case F2FS_IOC_RESIZE_FS:
5194 case FS_IOC_ENABLE_VERITY:
5195 case FS_IOC_ENABLE_CODE_SIGN:
5196 case FS_IOC_MEASURE_VERITY:
5197 case FS_IOC_READ_VERITY_METADATA:
5198 case FS_IOC_GETFSLABEL:
5199 case FS_IOC_SETFSLABEL:
5200 case F2FS_IOC_GET_COMPRESS_BLOCKS:
5201 case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
5202 case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
5203 case F2FS_IOC_SEC_TRIM_FILE:
5204 case F2FS_IOC_GET_COMPRESS_OPTION:
5205 case F2FS_IOC_SET_COMPRESS_OPTION:
5206 case F2FS_IOC_DECOMPRESS_FILE:
5207 case F2FS_IOC_COMPRESS_FILE:
5208 break;
5209 default:
5210 return -ENOIOCTLCMD;
5211 }
5212 return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5213 }
5214 #endif
5215
5216 const struct file_operations f2fs_file_operations = {
5217 .llseek = f2fs_llseek,
5218 .read_iter = f2fs_file_read_iter,
5219 .write_iter = f2fs_file_write_iter,
5220 .iopoll = iocb_bio_iopoll,
5221 .open = f2fs_file_open,
5222 .release = f2fs_release_file,
5223 .mmap = f2fs_file_mmap,
5224 .flush = f2fs_file_flush,
5225 .fsync = f2fs_sync_file,
5226 .fallocate = f2fs_fallocate,
5227 .unlocked_ioctl = f2fs_ioctl,
5228 #ifdef CONFIG_COMPAT
5229 .compat_ioctl = f2fs_compat_ioctl,
5230 #endif
5231 .splice_read = f2fs_file_splice_read,
5232 .splice_write = iter_file_splice_write,
5233 .fadvise = f2fs_file_fadvise,
5234 };
5235