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