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