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