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