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