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