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