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