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