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