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