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