Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd. |
| 3 | * Written by Takashi Sato <t-sato@yk.jp.nec.com> |
| 4 | * Akira Fujita <a-fujita@rs.jp.nec.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2.1 of the GNU Lesser General Public License |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/quotaops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 19 | #include "ext4_jbd2.h" |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 20 | #include "ext4.h" |
Theodore Ts'o | 4a092d7 | 2012-11-28 13:03:30 -0500 | [diff] [blame] | 21 | #include "ext4_extents.h" |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 22 | |
Akira Fujita | e850597 | 2009-09-16 13:46:38 -0400 | [diff] [blame] | 23 | /** |
| 24 | * get_ext_path - Find an extent path for designated logical block number. |
| 25 | * |
| 26 | * @inode: an inode which is searched |
| 27 | * @lblock: logical block number to find an extent path |
| 28 | * @path: pointer to an extent path pointer (for output) |
| 29 | * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 30 | * ext4_find_extent wrapper. Return 0 on success, or a negative error value |
Akira Fujita | e850597 | 2009-09-16 13:46:38 -0400 | [diff] [blame] | 31 | * on failure. |
| 32 | */ |
| 33 | static inline int |
| 34 | get_ext_path(struct inode *inode, ext4_lblk_t lblock, |
Theodore Ts'o | 3bdf14b | 2014-09-01 14:42:09 -0400 | [diff] [blame] | 35 | struct ext4_ext_path **ppath) |
Akira Fujita | e850597 | 2009-09-16 13:46:38 -0400 | [diff] [blame] | 36 | { |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 37 | struct ext4_ext_path *path; |
Akira Fujita | e850597 | 2009-09-16 13:46:38 -0400 | [diff] [blame] | 38 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 39 | path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE); |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 40 | if (IS_ERR(path)) |
Theodore Ts'o | 3bdf14b | 2014-09-01 14:42:09 -0400 | [diff] [blame] | 41 | return PTR_ERR(path); |
| 42 | if (path[ext_depth(inode)].p_ext == NULL) { |
| 43 | ext4_ext_drop_refs(path); |
| 44 | kfree(path); |
| 45 | *ppath = NULL; |
| 46 | return -ENODATA; |
| 47 | } |
| 48 | *ppath = path; |
| 49 | return 0; |
Akira Fujita | e850597 | 2009-09-16 13:46:38 -0400 | [diff] [blame] | 50 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 51 | |
| 52 | /** |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 53 | * ext4_double_down_write_data_sem - Acquire two inodes' write lock |
| 54 | * of i_data_sem |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 55 | * |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 56 | * Acquire write lock of i_data_sem of the two inodes |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 57 | */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 58 | void |
| 59 | ext4_double_down_write_data_sem(struct inode *first, struct inode *second) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 60 | { |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 61 | if (first < second) { |
| 62 | down_write(&EXT4_I(first)->i_data_sem); |
Theodore Ts'o | daf647d | 2016-04-01 01:31:28 -0400 | [diff] [blame] | 63 | down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER); |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 64 | } else { |
| 65 | down_write(&EXT4_I(second)->i_data_sem); |
Theodore Ts'o | daf647d | 2016-04-01 01:31:28 -0400 | [diff] [blame] | 66 | down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 67 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 68 | } |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 72 | * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 73 | * |
| 74 | * @orig_inode: original inode structure to be released its lock first |
| 75 | * @donor_inode: donor inode structure to be released its lock second |
Akira Fujita | fc04cb4 | 2009-11-23 07:24:43 -0500 | [diff] [blame] | 76 | * Release write lock of i_data_sem of two inodes (orig and donor). |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 77 | */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 78 | void |
| 79 | ext4_double_up_write_data_sem(struct inode *orig_inode, |
| 80 | struct inode *donor_inode) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 81 | { |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 82 | up_write(&EXT4_I(orig_inode)->i_data_sem); |
| 83 | up_write(&EXT4_I(donor_inode)->i_data_sem); |
| 84 | } |
| 85 | |
| 86 | /** |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 87 | * mext_check_coverage - Check that all extents in range has the same type |
| 88 | * |
| 89 | * @inode: inode in question |
| 90 | * @from: block offset of inode |
| 91 | * @count: block count to be checked |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 92 | * @unwritten: extents expected to be unwritten |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 93 | * @err: pointer to save error value |
| 94 | * |
| 95 | * Return 1 if all extents in range has expected type, and zero otherwise. |
| 96 | */ |
| 97 | static int |
| 98 | mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 99 | int unwritten, int *err) |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 100 | { |
| 101 | struct ext4_ext_path *path = NULL; |
| 102 | struct ext4_extent *ext; |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 103 | int ret = 0; |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 104 | ext4_lblk_t last = from + count; |
| 105 | while (from < last) { |
| 106 | *err = get_ext_path(inode, from, &path); |
| 107 | if (*err) |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 108 | goto out; |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 109 | ext = path[ext_depth(inode)].p_ext; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 110 | if (unwritten != ext4_ext_is_unwritten(ext)) |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 111 | goto out; |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 112 | from += ext4_ext_get_actual_len(ext); |
| 113 | ext4_ext_drop_refs(path); |
| 114 | } |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 115 | ret = 1; |
| 116 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 117 | ext4_ext_drop_refs(path); |
| 118 | kfree(path); |
Dmitry Monakhov | 0e40110 | 2013-03-18 11:40:19 -0400 | [diff] [blame] | 119 | return ret; |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 123 | * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2 |
| 124 | * |
| 125 | * @inode1: the inode structure |
| 126 | * @inode2: the inode structure |
Xiaoguang Wang | 65dd832 | 2014-10-11 19:56:34 -0400 | [diff] [blame] | 127 | * @index1: page index |
| 128 | * @index2: page index |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 129 | * @page: result page vector |
| 130 | * |
| 131 | * Grab two locked pages for inode's by inode order |
| 132 | */ |
| 133 | static int |
| 134 | mext_page_double_lock(struct inode *inode1, struct inode *inode2, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 135 | pgoff_t index1, pgoff_t index2, struct page *page[2]) |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 136 | { |
| 137 | struct address_space *mapping[2]; |
| 138 | unsigned fl = AOP_FLAG_NOFS; |
| 139 | |
| 140 | BUG_ON(!inode1 || !inode2); |
| 141 | if (inode1 < inode2) { |
| 142 | mapping[0] = inode1->i_mapping; |
| 143 | mapping[1] = inode2->i_mapping; |
| 144 | } else { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 145 | pgoff_t tmp = index1; |
| 146 | index1 = index2; |
| 147 | index2 = tmp; |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 148 | mapping[0] = inode2->i_mapping; |
| 149 | mapping[1] = inode1->i_mapping; |
| 150 | } |
| 151 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 152 | page[0] = grab_cache_page_write_begin(mapping[0], index1, fl); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 153 | if (!page[0]) |
| 154 | return -ENOMEM; |
| 155 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 156 | page[1] = grab_cache_page_write_begin(mapping[1], index2, fl); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 157 | if (!page[1]) { |
| 158 | unlock_page(page[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 159 | put_page(page[0]); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 160 | return -ENOMEM; |
| 161 | } |
Dmitry Monakhov | 7e8b12c | 2013-04-11 23:24:58 -0400 | [diff] [blame] | 162 | /* |
| 163 | * grab_cache_page_write_begin() may not wait on page's writeback if |
| 164 | * BDI not demand that. But it is reasonable to be very conservative |
| 165 | * here and explicitly wait on page's writeback |
| 166 | */ |
| 167 | wait_on_page_writeback(page[0]); |
| 168 | wait_on_page_writeback(page[1]); |
Fabian Frederick | bf86546 | 2015-06-12 23:47:33 -0400 | [diff] [blame] | 169 | if (inode1 > inode2) |
| 170 | swap(page[0], page[1]); |
| 171 | |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* Force page buffers uptodate w/o dropping page's lock */ |
| 176 | static int |
| 177 | mext_page_mkuptodate(struct page *page, unsigned from, unsigned to) |
| 178 | { |
| 179 | struct inode *inode = page->mapping->host; |
| 180 | sector_t block; |
| 181 | struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; |
| 182 | unsigned int blocksize, block_start, block_end; |
| 183 | int i, err, nr = 0, partial = 0; |
| 184 | BUG_ON(!PageLocked(page)); |
| 185 | BUG_ON(PageWriteback(page)); |
| 186 | |
| 187 | if (PageUptodate(page)) |
| 188 | return 0; |
| 189 | |
| 190 | blocksize = 1 << inode->i_blkbits; |
| 191 | if (!page_has_buffers(page)) |
| 192 | create_empty_buffers(page, blocksize, 0); |
| 193 | |
| 194 | head = page_buffers(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 195 | block = (sector_t)page->index << (PAGE_SHIFT - inode->i_blkbits); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 196 | for (bh = head, block_start = 0; bh != head || !block_start; |
| 197 | block++, block_start = block_end, bh = bh->b_this_page) { |
| 198 | block_end = block_start + blocksize; |
| 199 | if (block_end <= from || block_start >= to) { |
| 200 | if (!buffer_uptodate(bh)) |
| 201 | partial = 1; |
| 202 | continue; |
| 203 | } |
| 204 | if (buffer_uptodate(bh)) |
| 205 | continue; |
| 206 | if (!buffer_mapped(bh)) { |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 207 | err = ext4_get_block(inode, block, bh, 0); |
| 208 | if (err) { |
| 209 | SetPageError(page); |
| 210 | return err; |
| 211 | } |
| 212 | if (!buffer_mapped(bh)) { |
| 213 | zero_user(page, block_start, blocksize); |
Dan Carpenter | df3a98b | 2014-02-17 20:46:40 -0500 | [diff] [blame] | 214 | set_buffer_uptodate(bh); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 215 | continue; |
| 216 | } |
| 217 | } |
| 218 | BUG_ON(nr >= MAX_BUF_PER_PAGE); |
| 219 | arr[nr++] = bh; |
| 220 | } |
| 221 | /* No io required */ |
| 222 | if (!nr) |
| 223 | goto out; |
| 224 | |
| 225 | for (i = 0; i < nr; i++) { |
| 226 | bh = arr[i]; |
| 227 | if (!bh_uptodate_or_lock(bh)) { |
| 228 | err = bh_submit_read(bh); |
| 229 | if (err) |
| 230 | return err; |
| 231 | } |
| 232 | } |
| 233 | out: |
| 234 | if (!partial) |
| 235 | SetPageUptodate(page); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /** |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 240 | * move_extent_per_page - Move extent data per page |
| 241 | * |
| 242 | * @o_filp: file structure of original file |
| 243 | * @donor_inode: donor inode |
| 244 | * @orig_page_offset: page index on original file |
Xiaoguang Wang | 65dd832 | 2014-10-11 19:56:34 -0400 | [diff] [blame] | 245 | * @donor_page_offset: page index on donor file |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 246 | * @data_offset_in_page: block index where data swapping starts |
| 247 | * @block_len_in_page: the number of blocks to be swapped |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 248 | * @unwritten: orig extent is unwritten or not |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 249 | * @err: pointer to save return value |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 250 | * |
| 251 | * Save the data in original inode blocks and replace original inode extents |
Xiaoguang Wang | 65dd832 | 2014-10-11 19:56:34 -0400 | [diff] [blame] | 252 | * with donor inode extents by calling ext4_swap_extents(). |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 253 | * Finally, write out the saved data in new original inode blocks. Return |
| 254 | * replaced block count. |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 255 | */ |
| 256 | static int |
Akira Fujita | 44fc48f | 2009-09-05 23:12:41 -0400 | [diff] [blame] | 257 | move_extent_per_page(struct file *o_filp, struct inode *donor_inode, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 258 | pgoff_t orig_page_offset, pgoff_t donor_page_offset, |
| 259 | int data_offset_in_page, |
| 260 | int block_len_in_page, int unwritten, int *err) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 261 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 262 | struct inode *orig_inode = file_inode(o_filp); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 263 | struct page *pagep[2] = {NULL, NULL}; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 264 | handle_t *handle; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 265 | ext4_lblk_t orig_blk_offset, donor_blk_offset; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 266 | unsigned long blocksize = orig_inode->i_sb->s_blocksize; |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 267 | unsigned int tmp_data_size, data_size, replaced_size; |
Eryu Guan | bcff248 | 2016-02-12 01:20:43 -0500 | [diff] [blame] | 268 | int i, err2, jblocks, retries = 0; |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 269 | int replaced_count = 0; |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 270 | int from = data_offset_in_page << orig_inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 271 | int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; |
Dmitry Monakhov | 88c6b61 | 2014-11-05 11:52:38 -0500 | [diff] [blame] | 272 | struct super_block *sb = orig_inode->i_sb; |
Eryu Guan | bcff248 | 2016-02-12 01:20:43 -0500 | [diff] [blame] | 273 | struct buffer_head *bh = NULL; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * It needs twice the amount of ordinary journal buffers because |
| 277 | * inode and donor_inode may change each different metadata blocks. |
| 278 | */ |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 279 | again: |
| 280 | *err = 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 281 | jblocks = ext4_writepage_trans_blocks(orig_inode) * 2; |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 282 | handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 283 | if (IS_ERR(handle)) { |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 284 | *err = PTR_ERR(handle); |
| 285 | return 0; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 288 | orig_blk_offset = orig_page_offset * blocks_per_page + |
| 289 | data_offset_in_page; |
| 290 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 291 | donor_blk_offset = donor_page_offset * blocks_per_page + |
| 292 | data_offset_in_page; |
| 293 | |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 294 | /* Calculate data_size */ |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 295 | if ((orig_blk_offset + block_len_in_page - 1) == |
| 296 | ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) { |
| 297 | /* Replace the last block */ |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 298 | tmp_data_size = orig_inode->i_size & (blocksize - 1); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 299 | /* |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 300 | * If data_size equal zero, it shows data_size is multiples of |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 301 | * blocksize. So we set appropriate value. |
| 302 | */ |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 303 | if (tmp_data_size == 0) |
| 304 | tmp_data_size = blocksize; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 305 | |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 306 | data_size = tmp_data_size + |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 307 | ((block_len_in_page - 1) << orig_inode->i_blkbits); |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 308 | } else |
| 309 | data_size = block_len_in_page << orig_inode->i_blkbits; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 310 | |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 311 | replaced_size = data_size; |
| 312 | |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 313 | *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 314 | donor_page_offset, pagep); |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 315 | if (unlikely(*err < 0)) |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 316 | goto stop_journal; |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 317 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 318 | * If orig extent was unwritten it can become initialized |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 319 | * at any time after i_data_sem was dropped, in order to |
| 320 | * serialize with delalloc we have recheck extent while we |
| 321 | * hold page's lock, if it is still the case data copy is not |
| 322 | * necessary, just swap data blocks between orig and donor. |
| 323 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 324 | if (unwritten) { |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 325 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 326 | /* If any of extents in range became initialized we have to |
| 327 | * fallback to data copying */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 328 | unwritten = mext_check_coverage(orig_inode, orig_blk_offset, |
| 329 | block_len_in_page, 1, err); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 330 | if (*err) |
| 331 | goto drop_data_sem; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 332 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 333 | unwritten &= mext_check_coverage(donor_inode, donor_blk_offset, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 334 | block_len_in_page, 1, err); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 335 | if (*err) |
| 336 | goto drop_data_sem; |
| 337 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 338 | if (!unwritten) { |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 339 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 340 | goto data_copy; |
| 341 | } |
| 342 | if ((page_has_private(pagep[0]) && |
| 343 | !try_to_release_page(pagep[0], 0)) || |
| 344 | (page_has_private(pagep[1]) && |
| 345 | !try_to_release_page(pagep[1], 0))) { |
| 346 | *err = -EBUSY; |
| 347 | goto drop_data_sem; |
| 348 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 349 | replaced_count = ext4_swap_extents(handle, orig_inode, |
| 350 | donor_inode, orig_blk_offset, |
| 351 | donor_blk_offset, |
| 352 | block_len_in_page, 1, err); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 353 | drop_data_sem: |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 354 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | 8c85447 | 2012-09-26 12:54:52 -0400 | [diff] [blame] | 355 | goto unlock_pages; |
| 356 | } |
| 357 | data_copy: |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 358 | *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size); |
| 359 | if (*err) |
| 360 | goto unlock_pages; |
| 361 | |
| 362 | /* At this point all buffers in range are uptodate, old mapping layout |
| 363 | * is no longer required, try to drop it now. */ |
| 364 | if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) || |
| 365 | (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) { |
| 366 | *err = -EBUSY; |
| 367 | goto unlock_pages; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 368 | } |
Dmitry Monakhov | 6e26314 | 2014-07-27 22:32:27 -0400 | [diff] [blame] | 369 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 370 | replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode, |
| 371 | orig_blk_offset, donor_blk_offset, |
| 372 | block_len_in_page, 1, err); |
Dmitry Monakhov | 6e26314 | 2014-07-27 22:32:27 -0400 | [diff] [blame] | 373 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 374 | if (*err) { |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 375 | if (replaced_count) { |
| 376 | block_len_in_page = replaced_count; |
| 377 | replaced_size = |
| 378 | block_len_in_page << orig_inode->i_blkbits; |
Akira Fujita | ac48b0a | 2009-11-24 10:31:56 -0500 | [diff] [blame] | 379 | } else |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 380 | goto unlock_pages; |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 381 | } |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 382 | /* Perform all necessary steps similar write_begin()/write_end() |
| 383 | * but keeping in mind that i_size will not change */ |
Eryu Guan | bcff248 | 2016-02-12 01:20:43 -0500 | [diff] [blame] | 384 | if (!page_has_buffers(pagep[0])) |
| 385 | create_empty_buffers(pagep[0], 1 << orig_inode->i_blkbits, 0); |
| 386 | bh = page_buffers(pagep[0]); |
| 387 | for (i = 0; i < data_offset_in_page; i++) |
| 388 | bh = bh->b_this_page; |
| 389 | for (i = 0; i < block_len_in_page; i++) { |
| 390 | *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0); |
| 391 | if (*err < 0) |
| 392 | break; |
Eryu Guan | 6ffe77b | 2016-02-21 18:38:44 -0500 | [diff] [blame] | 393 | bh = bh->b_this_page; |
Eryu Guan | bcff248 | 2016-02-12 01:20:43 -0500 | [diff] [blame] | 394 | } |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 395 | if (!*err) |
| 396 | *err = block_commit_write(pagep[0], from, from + replaced_size); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 397 | |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 398 | if (unlikely(*err < 0)) |
| 399 | goto repair_branches; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 400 | |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 401 | /* Even in case of data=writeback it is reasonable to pin |
| 402 | * inode to transaction, to prevent unexpected data loss */ |
Jan Kara | ee0876b | 2016-04-24 00:56:08 -0400 | [diff] [blame] | 403 | *err = ext4_jbd2_inode_add_write(handle, orig_inode); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 404 | |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 405 | unlock_pages: |
| 406 | unlock_page(pagep[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 407 | put_page(pagep[0]); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 408 | unlock_page(pagep[1]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 409 | put_page(pagep[1]); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 410 | stop_journal: |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 411 | ext4_journal_stop(handle); |
Dmitry Monakhov | 88c6b61 | 2014-11-05 11:52:38 -0500 | [diff] [blame] | 412 | if (*err == -ENOSPC && |
| 413 | ext4_should_retry_alloc(sb, &retries)) |
| 414 | goto again; |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 415 | /* Buffer was busy because probably is pinned to journal transaction, |
| 416 | * force transaction commit may help to free it. */ |
Dmitry Monakhov | 88c6b61 | 2014-11-05 11:52:38 -0500 | [diff] [blame] | 417 | if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal && |
| 418 | jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal)) |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 419 | goto again; |
Akira Fujita | f868a48 | 2009-11-23 07:25:48 -0500 | [diff] [blame] | 420 | return replaced_count; |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 421 | |
| 422 | repair_branches: |
| 423 | /* |
| 424 | * This should never ever happen! |
| 425 | * Extents are swapped already, but we are not able to copy data. |
| 426 | * Try to swap extents to it's original places |
| 427 | */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 428 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 429 | replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode, |
| 430 | orig_blk_offset, donor_blk_offset, |
| 431 | block_len_in_page, 0, &err2); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 432 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | bb55748 | 2012-09-26 12:52:07 -0400 | [diff] [blame] | 433 | if (replaced_count != block_len_in_page) { |
| 434 | EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset), |
| 435 | "Unable to copy data block," |
| 436 | " data will be lost."); |
| 437 | *err = -EIO; |
| 438 | } |
| 439 | replaced_count = 0; |
| 440 | goto unlock_pages; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Akira Fujita | c437b27 | 2010-03-04 00:39:24 -0500 | [diff] [blame] | 444 | * mext_check_arguments - Check whether move extent can be done |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 445 | * |
| 446 | * @orig_inode: original inode |
| 447 | * @donor_inode: donor inode |
| 448 | * @orig_start: logical start offset in block for orig |
| 449 | * @donor_start: logical start offset in block for donor |
| 450 | * @len: the number of blocks to be moved |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 451 | * |
| 452 | * Check the arguments of ext4_move_extents() whether the files can be |
| 453 | * exchanged with each other. |
| 454 | * Return 0 on success, or a negative error value on failure. |
| 455 | */ |
| 456 | static int |
| 457 | mext_check_arguments(struct inode *orig_inode, |
Kazuya Mio | 446aaa6 | 2009-11-24 10:28:48 -0500 | [diff] [blame] | 458 | struct inode *donor_inode, __u64 orig_start, |
| 459 | __u64 donor_start, __u64 *len) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 460 | { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 461 | __u64 orig_eof, donor_eof; |
Akira Fujita | 70d5d3d | 2009-09-16 14:28:22 -0400 | [diff] [blame] | 462 | unsigned int blkbits = orig_inode->i_blkbits; |
| 463 | unsigned int blocksize = 1 << blkbits; |
| 464 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 465 | orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits; |
| 466 | donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits; |
| 467 | |
| 468 | |
Akira Fujita | 4a58579 | 2009-12-06 23:38:31 -0500 | [diff] [blame] | 469 | if (donor_inode->i_mode & (S_ISUID|S_ISGID)) { |
| 470 | ext4_debug("ext4 move extent: suid or sgid is set" |
| 471 | " to donor file [ino:orig %lu, donor %lu]\n", |
| 472 | orig_inode->i_ino, donor_inode->i_ino); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
Theodore Ts'o | 1f5a81e | 2010-06-02 22:04:39 -0400 | [diff] [blame] | 476 | if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) |
| 477 | return -EPERM; |
| 478 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 479 | /* Ext4 move extent does not support swapfile */ |
| 480 | if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) { |
| 481 | ext4_debug("ext4 move extent: The argument files should " |
| 482 | "not be swapfile [ino:orig %lu, donor %lu]\n", |
| 483 | orig_inode->i_ino, donor_inode->i_ino); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 484 | return -EBUSY; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Theodore Ts'o | daf647d | 2016-04-01 01:31:28 -0400 | [diff] [blame] | 487 | if (IS_NOQUOTA(orig_inode) || IS_NOQUOTA(donor_inode)) { |
| 488 | ext4_debug("ext4 move extent: The argument files should " |
| 489 | "not be quota files [ino:orig %lu, donor %lu]\n", |
| 490 | orig_inode->i_ino, donor_inode->i_ino); |
| 491 | return -EBUSY; |
| 492 | } |
| 493 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 494 | /* Ext4 move extent supports only extent based file */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 495 | if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) { |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 496 | ext4_debug("ext4 move extent: orig file is not extents " |
| 497 | "based file [ino:orig %lu]\n", orig_inode->i_ino); |
| 498 | return -EOPNOTSUPP; |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 499 | } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 500 | ext4_debug("ext4 move extent: donor file is not extents " |
| 501 | "based file [ino:donor %lu]\n", donor_inode->i_ino); |
| 502 | return -EOPNOTSUPP; |
| 503 | } |
| 504 | |
| 505 | if ((!orig_inode->i_size) || (!donor_inode->i_size)) { |
| 506 | ext4_debug("ext4 move extent: File size is 0 byte\n"); |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
| 510 | /* Start offset should be same */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 511 | if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) != |
| 512 | (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) { |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 513 | ext4_debug("ext4 move extent: orig and donor's start " |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 514 | "offset are not alligned [ino:orig %lu, donor %lu]\n", |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 515 | orig_inode->i_ino, donor_inode->i_ino); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 519 | if ((orig_start >= EXT_MAX_BLOCKS) || |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 520 | (donor_start >= EXT_MAX_BLOCKS) || |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 521 | (*len > EXT_MAX_BLOCKS) || |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 522 | (donor_start + *len >= EXT_MAX_BLOCKS) || |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 523 | (orig_start + *len >= EXT_MAX_BLOCKS)) { |
Eric Sandeen | 0a80e98 | 2009-09-17 11:55:58 -0400 | [diff] [blame] | 524 | ext4_debug("ext4 move extent: Can't handle over [%u] blocks " |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 525 | "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS, |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 526 | orig_inode->i_ino, donor_inode->i_ino); |
| 527 | return -EINVAL; |
| 528 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 529 | if (orig_eof < orig_start + *len - 1) |
| 530 | *len = orig_eof - orig_start; |
| 531 | if (donor_eof < donor_start + *len - 1) |
| 532 | *len = donor_eof - donor_start; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 533 | if (!*len) { |
Akira Fujita | 92c2815 | 2009-11-23 07:24:50 -0500 | [diff] [blame] | 534 | ext4_debug("ext4 move extent: len should not be 0 " |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 535 | "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino, |
| 536 | donor_inode->i_ino); |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /** |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 544 | * ext4_move_extents - Exchange the specified range of a file |
| 545 | * |
| 546 | * @o_filp: file structure of the original file |
| 547 | * @d_filp: file structure of the donor file |
Xiaoguang Wang | 65dd832 | 2014-10-11 19:56:34 -0400 | [diff] [blame] | 548 | * @orig_blk: start offset in block for orig |
| 549 | * @donor_blk: start offset in block for donor |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 550 | * @len: the number of blocks to be moved |
| 551 | * @moved_len: moved block length |
| 552 | * |
| 553 | * This function returns 0 and moved block length is set in moved_len |
| 554 | * if succeed, otherwise returns error value. |
| 555 | * |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 556 | */ |
| 557 | int |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 558 | ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk, |
| 559 | __u64 donor_blk, __u64 len, __u64 *moved_len) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 560 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 561 | struct inode *orig_inode = file_inode(o_filp); |
| 562 | struct inode *donor_inode = file_inode(d_filp); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 563 | struct ext4_ext_path *path = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 564 | int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 565 | ext4_lblk_t o_end, o_start = orig_blk; |
| 566 | ext4_lblk_t d_start = donor_blk; |
| 567 | int ret; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 568 | |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 569 | if (orig_inode->i_sb != donor_inode->i_sb) { |
| 570 | ext4_debug("ext4 move extent: The argument files " |
| 571 | "should be in same FS [ino:orig %lu, donor %lu]\n", |
| 572 | orig_inode->i_ino, donor_inode->i_ino); |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
| 576 | /* orig and donor should be different inodes */ |
| 577 | if (orig_inode == donor_inode) { |
Theodore Ts'o | f3ce806 | 2009-09-28 15:58:29 -0400 | [diff] [blame] | 578 | ext4_debug("ext4 move extent: The argument files should not " |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 579 | "be same inode [ino:orig %lu, donor %lu]\n", |
Theodore Ts'o | f3ce806 | 2009-09-28 15:58:29 -0400 | [diff] [blame] | 580 | orig_inode->i_ino, donor_inode->i_ino); |
| 581 | return -EINVAL; |
| 582 | } |
| 583 | |
Akira Fujita | 7247c0c | 2010-03-04 00:34:58 -0500 | [diff] [blame] | 584 | /* Regular file check */ |
| 585 | if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) { |
| 586 | ext4_debug("ext4 move extent: The argument files should be " |
| 587 | "regular file [ino:orig %lu, donor %lu]\n", |
| 588 | orig_inode->i_ino, donor_inode->i_ino); |
| 589 | return -EINVAL; |
| 590 | } |
Eric Whitney | 04e2241 | 2015-06-21 21:38:03 -0400 | [diff] [blame] | 591 | |
| 592 | /* TODO: it's not obvious how to swap blocks for inodes with full |
| 593 | journaling enabled */ |
Dmitry Monakhov | f066055 | 2012-09-26 12:32:54 -0400 | [diff] [blame] | 594 | if (ext4_should_journal_data(orig_inode) || |
| 595 | ext4_should_journal_data(donor_inode)) { |
Eric Whitney | 04e2241 | 2015-06-21 21:38:03 -0400 | [diff] [blame] | 596 | ext4_msg(orig_inode->i_sb, KERN_ERR, |
| 597 | "Online defrag not supported with data journaling"); |
| 598 | return -EOPNOTSUPP; |
Dmitry Monakhov | f066055 | 2012-09-26 12:32:54 -0400 | [diff] [blame] | 599 | } |
Eric Whitney | 04e2241 | 2015-06-21 21:38:03 -0400 | [diff] [blame] | 600 | |
Akira Fujita | fc04cb4 | 2009-11-23 07:24:43 -0500 | [diff] [blame] | 601 | /* Protect orig and donor inodes against a truncate */ |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 602 | lock_two_nondirectories(orig_inode, donor_inode); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 603 | |
Dmitry Monakhov | 17335dc | 2012-09-29 00:41:21 -0400 | [diff] [blame] | 604 | /* Wait for all existing dio workers */ |
| 605 | ext4_inode_block_unlocked_dio(orig_inode); |
| 606 | ext4_inode_block_unlocked_dio(donor_inode); |
| 607 | inode_dio_wait(orig_inode); |
| 608 | inode_dio_wait(donor_inode); |
| 609 | |
Akira Fujita | fc04cb4 | 2009-11-23 07:24:43 -0500 | [diff] [blame] | 610 | /* Protect extent tree against block allocations via delalloc */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 611 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 612 | /* Check the filesystem environment whether move_extent can be done */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 613 | ret = mext_check_arguments(orig_inode, donor_inode, orig_blk, |
| 614 | donor_blk, &len); |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 615 | if (ret) |
Akira Fujita | 347fa6f | 2009-09-16 14:25:07 -0400 | [diff] [blame] | 616 | goto out; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 617 | o_end = o_start + len; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 618 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 619 | while (o_start < o_end) { |
| 620 | struct ext4_extent *ex; |
| 621 | ext4_lblk_t cur_blk, next_blk; |
| 622 | pgoff_t orig_page_index, donor_page_index; |
| 623 | int offset_in_page; |
| 624 | int unwritten, cur_len; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 625 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 626 | ret = get_ext_path(orig_inode, o_start, &path); |
| 627 | if (ret) |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 628 | goto out; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 629 | ex = path[path->p_depth].p_ext; |
| 630 | next_blk = ext4_ext_next_allocated_block(path); |
| 631 | cur_blk = le32_to_cpu(ex->ee_block); |
| 632 | cur_len = ext4_ext_get_actual_len(ex); |
| 633 | /* Check hole before the start pos */ |
| 634 | if (cur_blk + cur_len - 1 < o_start) { |
| 635 | if (next_blk == EXT_MAX_BLOCKS) { |
| 636 | o_start = o_end; |
| 637 | ret = -ENODATA; |
| 638 | goto out; |
| 639 | } |
| 640 | d_start += next_blk - o_start; |
| 641 | o_start = next_blk; |
Theodore Ts'o | 3bdf14b | 2014-09-01 14:42:09 -0400 | [diff] [blame] | 642 | continue; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 643 | /* Check hole after the start pos */ |
| 644 | } else if (cur_blk > o_start) { |
| 645 | /* Skip hole */ |
| 646 | d_start += cur_blk - o_start; |
| 647 | o_start = cur_blk; |
| 648 | /* Extent inside requested range ?*/ |
| 649 | if (cur_blk >= o_end) |
| 650 | goto out; |
| 651 | } else { /* in_range(o_start, o_blk, o_len) */ |
| 652 | cur_len += cur_blk - o_start; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 653 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 654 | unwritten = ext4_ext_is_unwritten(ex); |
| 655 | if (o_end - o_start < cur_len) |
| 656 | cur_len = o_end - o_start; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 657 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 658 | orig_page_index = o_start >> (PAGE_SHIFT - |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 659 | orig_inode->i_blkbits); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 660 | donor_page_index = d_start >> (PAGE_SHIFT - |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 661 | donor_inode->i_blkbits); |
| 662 | offset_in_page = o_start % blocks_per_page; |
| 663 | if (cur_len > blocks_per_page- offset_in_page) |
| 664 | cur_len = blocks_per_page - offset_in_page; |
Akira Fujita | fc04cb4 | 2009-11-23 07:24:43 -0500 | [diff] [blame] | 665 | /* |
| 666 | * Up semaphore to avoid following problems: |
| 667 | * a. transaction deadlock among ext4_journal_start, |
| 668 | * ->write_begin via pagefault, and jbd2_journal_commit |
| 669 | * b. racing with ->readpage, ->write_begin, and ext4_get_block |
| 670 | * in move_extent_per_page |
| 671 | */ |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 672 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 673 | /* Swap original branches with new branches */ |
| 674 | move_extent_per_page(o_filp, donor_inode, |
| 675 | orig_page_index, donor_page_index, |
| 676 | offset_in_page, cur_len, |
| 677 | unwritten, &ret); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 678 | ext4_double_down_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 679 | if (ret < 0) |
Akira Fujita | fc04cb4 | 2009-11-23 07:24:43 -0500 | [diff] [blame] | 680 | break; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 681 | o_start += cur_len; |
| 682 | d_start += cur_len; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 683 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 684 | *moved_len = o_start - orig_blk; |
| 685 | if (*moved_len > len) |
| 686 | *moved_len = len; |
| 687 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 688 | out: |
Akira Fujita | 94d7c16 | 2009-11-24 10:19:57 -0500 | [diff] [blame] | 689 | if (*moved_len) { |
| 690 | ext4_discard_preallocations(orig_inode); |
| 691 | ext4_discard_preallocations(donor_inode); |
| 692 | } |
| 693 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 694 | ext4_ext_drop_refs(path); |
| 695 | kfree(path); |
Dr. Tilmann Bubeck | 393d1d1 | 2013-04-08 12:54:05 -0400 | [diff] [blame] | 696 | ext4_double_up_write_data_sem(orig_inode, donor_inode); |
Dmitry Monakhov | 17335dc | 2012-09-29 00:41:21 -0400 | [diff] [blame] | 697 | ext4_inode_resume_unlocked_dio(orig_inode); |
| 698 | ext4_inode_resume_unlocked_dio(donor_inode); |
J. Bruce Fields | 375e289 | 2012-04-18 15:16:33 -0400 | [diff] [blame] | 699 | unlock_two_nondirectories(orig_inode, donor_inode); |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 700 | |
Dmitry Monakhov | 03bd8b9 | 2012-09-26 12:32:19 -0400 | [diff] [blame] | 701 | return ret; |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 702 | } |