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