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