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