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