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