blob: 370420bfae8d76aaf7f68d21cfb5f1c06cf7770c [file] [log] [blame]
Akira Fujita748de672009-06-17 19:24:03 -04001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Akira Fujita748de672009-06-17 19:24:03 -040019#include "ext4_jbd2.h"
Akira Fujita748de672009-06-17 19:24:03 -040020#include "ext4.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050021#include "ext4_extents.h"
Akira Fujita748de672009-06-17 19:24:03 -040022
Akira Fujitae8505972009-09-16 13:46:38 -040023/**
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'oed8a1a72014-09-01 14:43:09 -040030 * ext4_find_extent wrapper. Return 0 on success, or a negative error value
Akira Fujitae8505972009-09-16 13:46:38 -040031 * on failure.
32 */
33static inline int
34get_ext_path(struct inode *inode, ext4_lblk_t lblock,
Theodore Ts'o3bdf14b2014-09-01 14:42:09 -040035 struct ext4_ext_path **ppath)
Akira Fujitae8505972009-09-16 13:46:38 -040036{
Dmitry Monakhov0e401102013-03-18 11:40:19 -040037 struct ext4_ext_path *path;
Akira Fujitae8505972009-09-16 13:46:38 -040038
Theodore Ts'oed8a1a72014-09-01 14:43:09 -040039 path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
Dmitry Monakhov0e401102013-03-18 11:40:19 -040040 if (IS_ERR(path))
Theodore Ts'o3bdf14b2014-09-01 14:42:09 -040041 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 Fujitae8505972009-09-16 13:46:38 -040050}
Akira Fujita748de672009-06-17 19:24:03 -040051
52/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040053 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
54 * of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -040055 *
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -040056 * Acquire write lock of i_data_sem of the two inodes
Akira Fujita748de672009-06-17 19:24:03 -040057 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040058void
59ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
Akira Fujita748de672009-06-17 19:24:03 -040060{
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -040061 if (first < second) {
62 down_write(&EXT4_I(first)->i_data_sem);
63 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
64 } else {
65 down_write(&EXT4_I(second)->i_data_sem);
66 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
Akira Fujita748de672009-06-17 19:24:03 -040067
Akira Fujita748de672009-06-17 19:24:03 -040068 }
Akira Fujita748de672009-06-17 19:24:03 -040069}
70
71/**
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040072 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
Akira Fujita748de672009-06-17 19:24:03 -040073 *
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 Fujitafc04cb42009-11-23 07:24:43 -050076 * Release write lock of i_data_sem of two inodes (orig and donor).
Akira Fujita748de672009-06-17 19:24:03 -040077 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040078void
79ext4_double_up_write_data_sem(struct inode *orig_inode,
80 struct inode *donor_inode)
Akira Fujita748de672009-06-17 19:24:03 -040081{
Akira Fujita748de672009-06-17 19:24:03 -040082 up_write(&EXT4_I(orig_inode)->i_data_sem);
83 up_write(&EXT4_I(donor_inode)->i_data_sem);
84}
85
86/**
Dmitry Monakhov8c854472012-09-26 12:54:52 -040087 * 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 Czerner556615d2014-04-20 23:45:47 -040092 * @unwritten: extents expected to be unwritten
Dmitry Monakhov8c854472012-09-26 12:54:52 -040093 * @err: pointer to save error value
94 *
95 * Return 1 if all extents in range has expected type, and zero otherwise.
96 */
97static int
98mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
Lukas Czerner556615d2014-04-20 23:45:47 -040099 int unwritten, int *err)
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400100{
101 struct ext4_ext_path *path = NULL;
102 struct ext4_extent *ext;
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400103 int ret = 0;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400104 ext4_lblk_t last = from + count;
105 while (from < last) {
106 *err = get_ext_path(inode, from, &path);
107 if (*err)
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400108 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400109 ext = path[ext_depth(inode)].p_ext;
Lukas Czerner556615d2014-04-20 23:45:47 -0400110 if (unwritten != ext4_ext_is_unwritten(ext))
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400111 goto out;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400112 from += ext4_ext_get_actual_len(ext);
113 ext4_ext_drop_refs(path);
114 }
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400115 ret = 1;
116out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400117 ext4_ext_drop_refs(path);
118 kfree(path);
Dmitry Monakhov0e401102013-03-18 11:40:19 -0400119 return ret;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400120}
121
122/**
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400123 * 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 Wang65dd8322014-10-11 19:56:34 -0400127 * @index1: page index
128 * @index2: page index
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400129 * @page: result page vector
130 *
131 * Grab two locked pages for inode's by inode order
132 */
133static int
134mext_page_double_lock(struct inode *inode1, struct inode *inode2,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400135 pgoff_t index1, pgoff_t index2, struct page *page[2])
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400136{
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 Monakhovfcf6b1b72014-08-30 23:52:19 -0400145 pgoff_t tmp = index1;
146 index1 = index2;
147 index2 = tmp;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400148 mapping[0] = inode2->i_mapping;
149 mapping[1] = inode1->i_mapping;
150 }
151
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400152 page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400153 if (!page[0])
154 return -ENOMEM;
155
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400156 page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400157 if (!page[1]) {
158 unlock_page(page[0]);
159 page_cache_release(page[0]);
160 return -ENOMEM;
161 }
Dmitry Monakhov7e8b12c2013-04-11 23:24:58 -0400162 /*
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]);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400169 if (inode1 > inode2) {
170 struct page *tmp;
171 tmp = page[0];
172 page[0] = page[1];
173 page[1] = tmp;
174 }
175 return 0;
176}
177
178/* Force page buffers uptodate w/o dropping page's lock */
179static int
180mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
181{
182 struct inode *inode = page->mapping->host;
183 sector_t block;
184 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
185 unsigned int blocksize, block_start, block_end;
186 int i, err, nr = 0, partial = 0;
187 BUG_ON(!PageLocked(page));
188 BUG_ON(PageWriteback(page));
189
190 if (PageUptodate(page))
191 return 0;
192
193 blocksize = 1 << inode->i_blkbits;
194 if (!page_has_buffers(page))
195 create_empty_buffers(page, blocksize, 0);
196
197 head = page_buffers(page);
198 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
199 for (bh = head, block_start = 0; bh != head || !block_start;
200 block++, block_start = block_end, bh = bh->b_this_page) {
201 block_end = block_start + blocksize;
202 if (block_end <= from || block_start >= to) {
203 if (!buffer_uptodate(bh))
204 partial = 1;
205 continue;
206 }
207 if (buffer_uptodate(bh))
208 continue;
209 if (!buffer_mapped(bh)) {
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400210 err = ext4_get_block(inode, block, bh, 0);
211 if (err) {
212 SetPageError(page);
213 return err;
214 }
215 if (!buffer_mapped(bh)) {
216 zero_user(page, block_start, blocksize);
Dan Carpenterdf3a98b2014-02-17 20:46:40 -0500217 set_buffer_uptodate(bh);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400218 continue;
219 }
220 }
221 BUG_ON(nr >= MAX_BUF_PER_PAGE);
222 arr[nr++] = bh;
223 }
224 /* No io required */
225 if (!nr)
226 goto out;
227
228 for (i = 0; i < nr; i++) {
229 bh = arr[i];
230 if (!bh_uptodate_or_lock(bh)) {
231 err = bh_submit_read(bh);
232 if (err)
233 return err;
234 }
235 }
236out:
237 if (!partial)
238 SetPageUptodate(page);
239 return 0;
240}
241
242/**
Akira Fujita748de672009-06-17 19:24:03 -0400243 * move_extent_per_page - Move extent data per page
244 *
245 * @o_filp: file structure of original file
246 * @donor_inode: donor inode
247 * @orig_page_offset: page index on original file
Xiaoguang Wang65dd8322014-10-11 19:56:34 -0400248 * @donor_page_offset: page index on donor file
Akira Fujita748de672009-06-17 19:24:03 -0400249 * @data_offset_in_page: block index where data swapping starts
250 * @block_len_in_page: the number of blocks to be swapped
Lukas Czerner556615d2014-04-20 23:45:47 -0400251 * @unwritten: orig extent is unwritten or not
Akira Fujitaf868a482009-11-23 07:25:48 -0500252 * @err: pointer to save return value
Akira Fujita748de672009-06-17 19:24:03 -0400253 *
254 * Save the data in original inode blocks and replace original inode extents
Xiaoguang Wang65dd8322014-10-11 19:56:34 -0400255 * with donor inode extents by calling ext4_swap_extents().
Akira Fujitaf868a482009-11-23 07:25:48 -0500256 * Finally, write out the saved data in new original inode blocks. Return
257 * replaced block count.
Akira Fujita748de672009-06-17 19:24:03 -0400258 */
259static int
Akira Fujita44fc48f2009-09-05 23:12:41 -0400260move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400261 pgoff_t orig_page_offset, pgoff_t donor_page_offset,
262 int data_offset_in_page,
263 int block_len_in_page, int unwritten, int *err)
Akira Fujita748de672009-06-17 19:24:03 -0400264{
Al Viro496ad9a2013-01-23 17:07:38 -0500265 struct inode *orig_inode = file_inode(o_filp);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400266 struct page *pagep[2] = {NULL, NULL};
Akira Fujita748de672009-06-17 19:24:03 -0400267 handle_t *handle;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400268 ext4_lblk_t orig_blk_offset, donor_blk_offset;
Akira Fujita748de672009-06-17 19:24:03 -0400269 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
Akira Fujitaf868a482009-11-23 07:25:48 -0500270 unsigned int tmp_data_size, data_size, replaced_size;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400271 int err2, jblocks, retries = 0;
Akira Fujitaf868a482009-11-23 07:25:48 -0500272 int replaced_count = 0;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400273 int from = data_offset_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400274 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
Dmitry Monakhov88c6b612014-11-05 11:52:38 -0500275 struct super_block *sb = orig_inode->i_sb;
Akira Fujita748de672009-06-17 19:24:03 -0400276
277 /*
278 * It needs twice the amount of ordinary journal buffers because
279 * inode and donor_inode may change each different metadata blocks.
280 */
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400281again:
282 *err = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400283 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
Theodore Ts'o9924a922013-02-08 21:59:22 -0500284 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
Akira Fujita748de672009-06-17 19:24:03 -0400285 if (IS_ERR(handle)) {
Akira Fujitaf868a482009-11-23 07:25:48 -0500286 *err = PTR_ERR(handle);
287 return 0;
Akira Fujita748de672009-06-17 19:24:03 -0400288 }
289
Akira Fujita748de672009-06-17 19:24:03 -0400290 orig_blk_offset = orig_page_offset * blocks_per_page +
291 data_offset_in_page;
292
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400293 donor_blk_offset = donor_page_offset * blocks_per_page +
294 data_offset_in_page;
295
Akira Fujitaf868a482009-11-23 07:25:48 -0500296 /* Calculate data_size */
Akira Fujita748de672009-06-17 19:24:03 -0400297 if ((orig_blk_offset + block_len_in_page - 1) ==
298 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
299 /* Replace the last block */
Akira Fujitaf868a482009-11-23 07:25:48 -0500300 tmp_data_size = orig_inode->i_size & (blocksize - 1);
Akira Fujita748de672009-06-17 19:24:03 -0400301 /*
Akira Fujitaf868a482009-11-23 07:25:48 -0500302 * If data_size equal zero, it shows data_size is multiples of
Akira Fujita748de672009-06-17 19:24:03 -0400303 * blocksize. So we set appropriate value.
304 */
Akira Fujitaf868a482009-11-23 07:25:48 -0500305 if (tmp_data_size == 0)
306 tmp_data_size = blocksize;
Akira Fujita748de672009-06-17 19:24:03 -0400307
Akira Fujitaf868a482009-11-23 07:25:48 -0500308 data_size = tmp_data_size +
Akira Fujita748de672009-06-17 19:24:03 -0400309 ((block_len_in_page - 1) << orig_inode->i_blkbits);
Akira Fujitaf868a482009-11-23 07:25:48 -0500310 } else
311 data_size = block_len_in_page << orig_inode->i_blkbits;
Akira Fujita748de672009-06-17 19:24:03 -0400312
Akira Fujitaf868a482009-11-23 07:25:48 -0500313 replaced_size = data_size;
314
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400315 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400316 donor_page_offset, pagep);
Akira Fujitaf868a482009-11-23 07:25:48 -0500317 if (unlikely(*err < 0))
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400318 goto stop_journal;
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400319 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400320 * If orig extent was unwritten it can become initialized
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400321 * at any time after i_data_sem was dropped, in order to
322 * serialize with delalloc we have recheck extent while we
323 * hold page's lock, if it is still the case data copy is not
324 * necessary, just swap data blocks between orig and donor.
325 */
Lukas Czerner556615d2014-04-20 23:45:47 -0400326 if (unwritten) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400327 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400328 /* If any of extents in range became initialized we have to
329 * fallback to data copying */
Lukas Czerner556615d2014-04-20 23:45:47 -0400330 unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
331 block_len_in_page, 1, err);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400332 if (*err)
333 goto drop_data_sem;
Akira Fujita748de672009-06-17 19:24:03 -0400334
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400335 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
Lukas Czerner556615d2014-04-20 23:45:47 -0400336 block_len_in_page, 1, err);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400337 if (*err)
338 goto drop_data_sem;
339
Lukas Czerner556615d2014-04-20 23:45:47 -0400340 if (!unwritten) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400341 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400342 goto data_copy;
343 }
344 if ((page_has_private(pagep[0]) &&
345 !try_to_release_page(pagep[0], 0)) ||
346 (page_has_private(pagep[1]) &&
347 !try_to_release_page(pagep[1], 0))) {
348 *err = -EBUSY;
349 goto drop_data_sem;
350 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400351 replaced_count = ext4_swap_extents(handle, orig_inode,
352 donor_inode, orig_blk_offset,
353 donor_blk_offset,
354 block_len_in_page, 1, err);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400355 drop_data_sem:
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400356 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov8c854472012-09-26 12:54:52 -0400357 goto unlock_pages;
358 }
359data_copy:
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400360 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
361 if (*err)
362 goto unlock_pages;
363
364 /* At this point all buffers in range are uptodate, old mapping layout
365 * is no longer required, try to drop it now. */
366 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
367 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
368 *err = -EBUSY;
369 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -0400370 }
Dmitry Monakhov6e263142014-07-27 22:32:27 -0400371 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400372 replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
373 orig_blk_offset, donor_blk_offset,
374 block_len_in_page, 1, err);
Dmitry Monakhov6e263142014-07-27 22:32:27 -0400375 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400376 if (*err) {
Akira Fujitaf868a482009-11-23 07:25:48 -0500377 if (replaced_count) {
378 block_len_in_page = replaced_count;
379 replaced_size =
380 block_len_in_page << orig_inode->i_blkbits;
Akira Fujitaac48b0a2009-11-24 10:31:56 -0500381 } else
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400382 goto unlock_pages;
Akira Fujitaf868a482009-11-23 07:25:48 -0500383 }
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400384 /* Perform all necessary steps similar write_begin()/write_end()
385 * but keeping in mind that i_size will not change */
Dmitry Monakhov7e8b12c2013-04-11 23:24:58 -0400386 *err = __block_write_begin(pagep[0], from, replaced_size,
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400387 ext4_get_block);
388 if (!*err)
389 *err = block_commit_write(pagep[0], from, from + replaced_size);
Akira Fujita748de672009-06-17 19:24:03 -0400390
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400391 if (unlikely(*err < 0))
392 goto repair_branches;
Akira Fujita748de672009-06-17 19:24:03 -0400393
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400394 /* Even in case of data=writeback it is reasonable to pin
395 * inode to transaction, to prevent unexpected data loss */
396 *err = ext4_jbd2_file_inode(handle, orig_inode);
Akira Fujita748de672009-06-17 19:24:03 -0400397
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400398unlock_pages:
399 unlock_page(pagep[0]);
400 page_cache_release(pagep[0]);
401 unlock_page(pagep[1]);
402 page_cache_release(pagep[1]);
403stop_journal:
Akira Fujita748de672009-06-17 19:24:03 -0400404 ext4_journal_stop(handle);
Dmitry Monakhov88c6b612014-11-05 11:52:38 -0500405 if (*err == -ENOSPC &&
406 ext4_should_retry_alloc(sb, &retries))
407 goto again;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400408 /* Buffer was busy because probably is pinned to journal transaction,
409 * force transaction commit may help to free it. */
Dmitry Monakhov88c6b612014-11-05 11:52:38 -0500410 if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
411 jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400412 goto again;
Akira Fujitaf868a482009-11-23 07:25:48 -0500413 return replaced_count;
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400414
415repair_branches:
416 /*
417 * This should never ever happen!
418 * Extents are swapped already, but we are not able to copy data.
419 * Try to swap extents to it's original places
420 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400421 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400422 replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
423 orig_blk_offset, donor_blk_offset,
424 block_len_in_page, 0, &err2);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400425 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovbb557482012-09-26 12:52:07 -0400426 if (replaced_count != block_len_in_page) {
427 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
428 "Unable to copy data block,"
429 " data will be lost.");
430 *err = -EIO;
431 }
432 replaced_count = 0;
433 goto unlock_pages;
Akira Fujita748de672009-06-17 19:24:03 -0400434}
435
436/**
Akira Fujitac437b272010-03-04 00:39:24 -0500437 * mext_check_arguments - Check whether move extent can be done
Akira Fujita748de672009-06-17 19:24:03 -0400438 *
439 * @orig_inode: original inode
440 * @donor_inode: donor inode
441 * @orig_start: logical start offset in block for orig
442 * @donor_start: logical start offset in block for donor
443 * @len: the number of blocks to be moved
Akira Fujita748de672009-06-17 19:24:03 -0400444 *
445 * Check the arguments of ext4_move_extents() whether the files can be
446 * exchanged with each other.
447 * Return 0 on success, or a negative error value on failure.
448 */
449static int
450mext_check_arguments(struct inode *orig_inode,
Kazuya Mio446aaa62009-11-24 10:28:48 -0500451 struct inode *donor_inode, __u64 orig_start,
452 __u64 donor_start, __u64 *len)
Akira Fujita748de672009-06-17 19:24:03 -0400453{
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400454 __u64 orig_eof, donor_eof;
Akira Fujita70d5d3d2009-09-16 14:28:22 -0400455 unsigned int blkbits = orig_inode->i_blkbits;
456 unsigned int blocksize = 1 << blkbits;
457
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400458 orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
459 donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
460
461
Akira Fujita4a585792009-12-06 23:38:31 -0500462 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
463 ext4_debug("ext4 move extent: suid or sgid is set"
464 " to donor file [ino:orig %lu, donor %lu]\n",
465 orig_inode->i_ino, donor_inode->i_ino);
466 return -EINVAL;
467 }
468
Theodore Ts'o1f5a81e2010-06-02 22:04:39 -0400469 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
470 return -EPERM;
471
Akira Fujita748de672009-06-17 19:24:03 -0400472 /* Ext4 move extent does not support swapfile */
473 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
474 ext4_debug("ext4 move extent: The argument files should "
475 "not be swapfile [ino:orig %lu, donor %lu]\n",
476 orig_inode->i_ino, donor_inode->i_ino);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400477 return -EBUSY;
Akira Fujita748de672009-06-17 19:24:03 -0400478 }
479
Akira Fujita748de672009-06-17 19:24:03 -0400480 /* Ext4 move extent supports only extent based file */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400481 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -0400482 ext4_debug("ext4 move extent: orig file is not extents "
483 "based file [ino:orig %lu]\n", orig_inode->i_ino);
484 return -EOPNOTSUPP;
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400485 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
Akira Fujita748de672009-06-17 19:24:03 -0400486 ext4_debug("ext4 move extent: donor file is not extents "
487 "based file [ino:donor %lu]\n", donor_inode->i_ino);
488 return -EOPNOTSUPP;
489 }
490
491 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
492 ext4_debug("ext4 move extent: File size is 0 byte\n");
493 return -EINVAL;
494 }
495
496 /* Start offset should be same */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400497 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
498 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
Akira Fujita748de672009-06-17 19:24:03 -0400499 ext4_debug("ext4 move extent: orig and donor's start "
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400500 "offset are not alligned [ino:orig %lu, donor %lu]\n",
Akira Fujita748de672009-06-17 19:24:03 -0400501 orig_inode->i_ino, donor_inode->i_ino);
502 return -EINVAL;
503 }
504
Lukas Czernerf17722f2011-06-06 00:05:17 -0400505 if ((orig_start >= EXT_MAX_BLOCKS) ||
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400506 (donor_start >= EXT_MAX_BLOCKS) ||
Lukas Czernerf17722f2011-06-06 00:05:17 -0400507 (*len > EXT_MAX_BLOCKS) ||
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400508 (donor_start + *len >= EXT_MAX_BLOCKS) ||
Lukas Czernerf17722f2011-06-06 00:05:17 -0400509 (orig_start + *len >= EXT_MAX_BLOCKS)) {
Eric Sandeen0a80e982009-09-17 11:55:58 -0400510 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
Lukas Czernerf17722f2011-06-06 00:05:17 -0400511 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
Akira Fujita748de672009-06-17 19:24:03 -0400512 orig_inode->i_ino, donor_inode->i_ino);
513 return -EINVAL;
514 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400515 if (orig_eof < orig_start + *len - 1)
516 *len = orig_eof - orig_start;
517 if (donor_eof < donor_start + *len - 1)
518 *len = donor_eof - donor_start;
Akira Fujita748de672009-06-17 19:24:03 -0400519 if (!*len) {
Akira Fujita92c28152009-11-23 07:24:50 -0500520 ext4_debug("ext4 move extent: len should not be 0 "
Akira Fujita748de672009-06-17 19:24:03 -0400521 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
522 donor_inode->i_ino);
523 return -EINVAL;
524 }
525
526 return 0;
527}
528
529/**
Akira Fujita748de672009-06-17 19:24:03 -0400530 * ext4_move_extents - Exchange the specified range of a file
531 *
532 * @o_filp: file structure of the original file
533 * @d_filp: file structure of the donor file
Xiaoguang Wang65dd8322014-10-11 19:56:34 -0400534 * @orig_blk: start offset in block for orig
535 * @donor_blk: start offset in block for donor
Akira Fujita748de672009-06-17 19:24:03 -0400536 * @len: the number of blocks to be moved
537 * @moved_len: moved block length
538 *
539 * This function returns 0 and moved block length is set in moved_len
540 * if succeed, otherwise returns error value.
541 *
Akira Fujita748de672009-06-17 19:24:03 -0400542 */
543int
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400544ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
545 __u64 donor_blk, __u64 len, __u64 *moved_len)
Akira Fujita748de672009-06-17 19:24:03 -0400546{
Al Viro496ad9a2013-01-23 17:07:38 -0500547 struct inode *orig_inode = file_inode(o_filp);
548 struct inode *donor_inode = file_inode(d_filp);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400549 struct ext4_ext_path *path = NULL;
Akira Fujita748de672009-06-17 19:24:03 -0400550 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400551 ext4_lblk_t o_end, o_start = orig_blk;
552 ext4_lblk_t d_start = donor_blk;
553 int ret;
Akira Fujita748de672009-06-17 19:24:03 -0400554
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400555 if (orig_inode->i_sb != donor_inode->i_sb) {
556 ext4_debug("ext4 move extent: The argument files "
557 "should be in same FS [ino:orig %lu, donor %lu]\n",
558 orig_inode->i_ino, donor_inode->i_ino);
559 return -EINVAL;
560 }
561
562 /* orig and donor should be different inodes */
563 if (orig_inode == donor_inode) {
Theodore Ts'of3ce8062009-09-28 15:58:29 -0400564 ext4_debug("ext4 move extent: The argument files should not "
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400565 "be same inode [ino:orig %lu, donor %lu]\n",
Theodore Ts'of3ce8062009-09-28 15:58:29 -0400566 orig_inode->i_ino, donor_inode->i_ino);
567 return -EINVAL;
568 }
569
Akira Fujita7247c0c2010-03-04 00:34:58 -0500570 /* Regular file check */
571 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
572 ext4_debug("ext4 move extent: The argument files should be "
573 "regular file [ino:orig %lu, donor %lu]\n",
574 orig_inode->i_ino, donor_inode->i_ino);
575 return -EINVAL;
576 }
Dmitry Monakhovf0660552012-09-26 12:32:54 -0400577 /* TODO: This is non obvious task to swap blocks for inodes with full
578 jornaling enabled */
579 if (ext4_should_journal_data(orig_inode) ||
580 ext4_should_journal_data(donor_inode)) {
581 return -EINVAL;
582 }
Akira Fujitafc04cb42009-11-23 07:24:43 -0500583 /* Protect orig and donor inodes against a truncate */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400584 lock_two_nondirectories(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -0400585
Dmitry Monakhov17335dc2012-09-29 00:41:21 -0400586 /* Wait for all existing dio workers */
587 ext4_inode_block_unlocked_dio(orig_inode);
588 ext4_inode_block_unlocked_dio(donor_inode);
589 inode_dio_wait(orig_inode);
590 inode_dio_wait(donor_inode);
591
Akira Fujitafc04cb42009-11-23 07:24:43 -0500592 /* Protect extent tree against block allocations via delalloc */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400593 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -0400594 /* Check the filesystem environment whether move_extent can be done */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400595 ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
596 donor_blk, &len);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400597 if (ret)
Akira Fujita347fa6f2009-09-16 14:25:07 -0400598 goto out;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400599 o_end = o_start + len;
Akira Fujita748de672009-06-17 19:24:03 -0400600
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400601 while (o_start < o_end) {
602 struct ext4_extent *ex;
603 ext4_lblk_t cur_blk, next_blk;
604 pgoff_t orig_page_index, donor_page_index;
605 int offset_in_page;
606 int unwritten, cur_len;
Akira Fujita748de672009-06-17 19:24:03 -0400607
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400608 ret = get_ext_path(orig_inode, o_start, &path);
609 if (ret)
Akira Fujita748de672009-06-17 19:24:03 -0400610 goto out;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400611 ex = path[path->p_depth].p_ext;
612 next_blk = ext4_ext_next_allocated_block(path);
613 cur_blk = le32_to_cpu(ex->ee_block);
614 cur_len = ext4_ext_get_actual_len(ex);
615 /* Check hole before the start pos */
616 if (cur_blk + cur_len - 1 < o_start) {
617 if (next_blk == EXT_MAX_BLOCKS) {
618 o_start = o_end;
619 ret = -ENODATA;
620 goto out;
621 }
622 d_start += next_blk - o_start;
623 o_start = next_blk;
Theodore Ts'o3bdf14b2014-09-01 14:42:09 -0400624 continue;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400625 /* Check hole after the start pos */
626 } else if (cur_blk > o_start) {
627 /* Skip hole */
628 d_start += cur_blk - o_start;
629 o_start = cur_blk;
630 /* Extent inside requested range ?*/
631 if (cur_blk >= o_end)
632 goto out;
633 } else { /* in_range(o_start, o_blk, o_len) */
634 cur_len += cur_blk - o_start;
Akira Fujita748de672009-06-17 19:24:03 -0400635 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400636 unwritten = ext4_ext_is_unwritten(ex);
637 if (o_end - o_start < cur_len)
638 cur_len = o_end - o_start;
Akira Fujita748de672009-06-17 19:24:03 -0400639
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400640 orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
641 orig_inode->i_blkbits);
642 donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
643 donor_inode->i_blkbits);
644 offset_in_page = o_start % blocks_per_page;
645 if (cur_len > blocks_per_page- offset_in_page)
646 cur_len = blocks_per_page - offset_in_page;
Akira Fujitafc04cb42009-11-23 07:24:43 -0500647 /*
648 * Up semaphore to avoid following problems:
649 * a. transaction deadlock among ext4_journal_start,
650 * ->write_begin via pagefault, and jbd2_journal_commit
651 * b. racing with ->readpage, ->write_begin, and ext4_get_block
652 * in move_extent_per_page
653 */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400654 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400655 /* Swap original branches with new branches */
656 move_extent_per_page(o_filp, donor_inode,
657 orig_page_index, donor_page_index,
658 offset_in_page, cur_len,
659 unwritten, &ret);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400660 ext4_double_down_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400661 if (ret < 0)
Akira Fujitafc04cb42009-11-23 07:24:43 -0500662 break;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400663 o_start += cur_len;
664 d_start += cur_len;
Akira Fujita748de672009-06-17 19:24:03 -0400665 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400666 *moved_len = o_start - orig_blk;
667 if (*moved_len > len)
668 *moved_len = len;
669
Akira Fujita748de672009-06-17 19:24:03 -0400670out:
Akira Fujita94d7c162009-11-24 10:19:57 -0500671 if (*moved_len) {
672 ext4_discard_preallocations(orig_inode);
673 ext4_discard_preallocations(donor_inode);
674 }
675
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400676 ext4_ext_drop_refs(path);
677 kfree(path);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400678 ext4_double_up_write_data_sem(orig_inode, donor_inode);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -0400679 ext4_inode_resume_unlocked_dio(orig_inode);
680 ext4_inode_resume_unlocked_dio(donor_inode);
J. Bruce Fields375e2892012-04-18 15:16:33 -0400681 unlock_two_nondirectories(orig_inode, donor_inode);
Akira Fujita748de672009-06-17 19:24:03 -0400682
Dmitry Monakhov03bd8b92012-09-26 12:32:19 -0400683 return ret;
Akira Fujita748de672009-06-17 19:24:03 -0400684}