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