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