~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext4/move_extent.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
 18 #include <linux/slab.h>
 19 #include "ext4_jbd2.h"
 20 #include "ext4.h"
 21 #include "ext4_extents.h"
 22 
 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  *
 30  * ext4_find_extent wrapper. Return 0 on success, or a negative error value
 31  * on failure.
 32  */
 33 static inline int
 34 get_ext_path(struct inode *inode, ext4_lblk_t lblock,
 35                 struct ext4_ext_path **ppath)
 36 {
 37         struct ext4_ext_path *path;
 38 
 39         path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
 40         if (IS_ERR(path))
 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;
 50 }
 51 
 52 /**
 53  * ext4_double_down_write_data_sem - Acquire two inodes' write lock
 54  *                                   of i_data_sem
 55  *
 56  * Acquire write lock of i_data_sem of the two inodes
 57  */
 58 void
 59 ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
 60 {
 61         if (first < second) {
 62                 down_write(&EXT4_I(first)->i_data_sem);
 63                 down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER);
 64         } else {
 65                 down_write(&EXT4_I(second)->i_data_sem);
 66                 down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER);
 67 
 68         }
 69 }
 70 
 71 /**
 72  * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
 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
 76  * Release write lock of i_data_sem of two inodes (orig and donor).
 77  */
 78 void
 79 ext4_double_up_write_data_sem(struct inode *orig_inode,
 80                               struct inode *donor_inode)
 81 {
 82         up_write(&EXT4_I(orig_inode)->i_data_sem);
 83         up_write(&EXT4_I(donor_inode)->i_data_sem);
 84 }
 85 
 86 /**
 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
 92  * @unwritten:          extents expected to be unwritten
 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,
 99                     int unwritten, int *err)
100 {
101         struct ext4_ext_path *path = NULL;
102         struct ext4_extent *ext;
103         int ret = 0;
104         ext4_lblk_t last = from + count;
105         while (from < last) {
106                 *err = get_ext_path(inode, from, &path);
107                 if (*err)
108                         goto out;
109                 ext = path[ext_depth(inode)].p_ext;
110                 if (unwritten != ext4_ext_is_unwritten(ext))
111                         goto out;
112                 from += ext4_ext_get_actual_len(ext);
113                 ext4_ext_drop_refs(path);
114         }
115         ret = 1;
116 out:
117         ext4_ext_drop_refs(path);
118         kfree(path);
119         return ret;
120 }
121 
122 /**
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
127  * @index1:     page index
128  * @index2:     page index
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,
135                       pgoff_t index1, pgoff_t index2, struct page *page[2])
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 {
145                 pgoff_t tmp = index1;
146                 index1 = index2;
147                 index2 = tmp;
148                 mapping[0] = inode2->i_mapping;
149                 mapping[1] = inode1->i_mapping;
150         }
151 
152         page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
153         if (!page[0])
154                 return -ENOMEM;
155 
156         page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
157         if (!page[1]) {
158                 unlock_page(page[0]);
159                 page_cache_release(page[0]);
160                 return -ENOMEM;
161         }
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]);
169         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 */
179 static int
180 mext_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)) {
210                         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);
217                                 set_buffer_uptodate(bh);
218                                 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         }
236 out:
237         if (!partial)
238                 SetPageUptodate(page);
239         return 0;
240 }
241 
242 /**
243  * 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
248  * @donor_page_offset:          page index on donor file
249  * @data_offset_in_page:        block index where data swapping starts
250  * @block_len_in_page:          the number of blocks to be swapped
251  * @unwritten:                  orig extent is unwritten or not
252  * @err:                        pointer to save return value
253  *
254  * Save the data in original inode blocks and replace original inode extents
255  * with donor inode extents by calling ext4_swap_extents().
256  * Finally, write out the saved data in new original inode blocks. Return
257  * replaced block count.
258  */
259 static int
260 move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
261                      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)
264 {
265         struct inode *orig_inode = file_inode(o_filp);
266         struct page *pagep[2] = {NULL, NULL};
267         handle_t *handle;
268         ext4_lblk_t orig_blk_offset, donor_blk_offset;
269         unsigned long blocksize = orig_inode->i_sb->s_blocksize;
270         unsigned int w_flags = 0;
271         unsigned int tmp_data_size, data_size, replaced_size;
272         int i, err2, jblocks, retries = 0;
273         int replaced_count = 0;
274         int from = data_offset_in_page << orig_inode->i_blkbits;
275         int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
276         struct super_block *sb = orig_inode->i_sb;
277         struct buffer_head *bh = NULL;
278 
279         /*
280          * It needs twice the amount of ordinary journal buffers because
281          * inode and donor_inode may change each different metadata blocks.
282          */
283 again:
284         *err = 0;
285         jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
286         handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
287         if (IS_ERR(handle)) {
288                 *err = PTR_ERR(handle);
289                 return 0;
290         }
291 
292         if (segment_eq(get_fs(), KERNEL_DS))
293                 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
294 
295         orig_blk_offset = orig_page_offset * blocks_per_page +
296                 data_offset_in_page;
297 
298         donor_blk_offset = donor_page_offset * blocks_per_page +
299                 data_offset_in_page;
300 
301         /* Calculate data_size */
302         if ((orig_blk_offset + block_len_in_page - 1) ==
303             ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
304                 /* Replace the last block */
305                 tmp_data_size = orig_inode->i_size & (blocksize - 1);
306                 /*
307                  * If data_size equal zero, it shows data_size is multiples of
308                  * blocksize. So we set appropriate value.
309                  */
310                 if (tmp_data_size == 0)
311                         tmp_data_size = blocksize;
312 
313                 data_size = tmp_data_size +
314                         ((block_len_in_page - 1) << orig_inode->i_blkbits);
315         } else
316                 data_size = block_len_in_page << orig_inode->i_blkbits;
317 
318         replaced_size = data_size;
319 
320         *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
321                                      donor_page_offset, pagep);
322         if (unlikely(*err < 0))
323                 goto stop_journal;
324         /*
325          * If orig extent was unwritten it can become initialized
326          * at any time after i_data_sem was dropped, in order to
327          * serialize with delalloc we have recheck extent while we
328          * hold page's lock, if it is still the case data copy is not
329          * necessary, just swap data blocks between orig and donor.
330          */
331         if (unwritten) {
332                 ext4_double_down_write_data_sem(orig_inode, donor_inode);
333                 /* If any of extents in range became initialized we have to
334                  * fallback to data copying */
335                 unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
336                                                 block_len_in_page, 1, err);
337                 if (*err)
338                         goto drop_data_sem;
339 
340                 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
341                                                  block_len_in_page, 1, err);
342                 if (*err)
343                         goto drop_data_sem;
344 
345                 if (!unwritten) {
346                         ext4_double_up_write_data_sem(orig_inode, donor_inode);
347                         goto data_copy;
348                 }
349                 if ((page_has_private(pagep[0]) &&
350                      !try_to_release_page(pagep[0], 0)) ||
351                     (page_has_private(pagep[1]) &&
352                      !try_to_release_page(pagep[1], 0))) {
353                         *err = -EBUSY;
354                         goto drop_data_sem;
355                 }
356                 replaced_count = ext4_swap_extents(handle, orig_inode,
357                                                    donor_inode, orig_blk_offset,
358                                                    donor_blk_offset,
359                                                    block_len_in_page, 1, err);
360         drop_data_sem:
361                 ext4_double_up_write_data_sem(orig_inode, donor_inode);
362                 goto unlock_pages;
363         }
364 data_copy:
365         *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
366         if (*err)
367                 goto unlock_pages;
368 
369         /* At this point all buffers in range are uptodate, old mapping layout
370          * is no longer required, try to drop it now. */
371         if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
372             (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
373                 *err = -EBUSY;
374                 goto unlock_pages;
375         }
376         ext4_double_down_write_data_sem(orig_inode, donor_inode);
377         replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
378                                                orig_blk_offset, donor_blk_offset,
379                                            block_len_in_page, 1, err);
380         ext4_double_up_write_data_sem(orig_inode, donor_inode);
381         if (*err) {
382                 if (replaced_count) {
383                         block_len_in_page = replaced_count;
384                         replaced_size =
385                                 block_len_in_page << orig_inode->i_blkbits;
386                 } else
387                         goto unlock_pages;
388         }
389         /* Perform all necessary steps similar write_begin()/write_end()
390          * but keeping in mind that i_size will not change */
391         if (!page_has_buffers(pagep[0]))
392                 create_empty_buffers(pagep[0], 1 << orig_inode->i_blkbits, 0);
393         bh = page_buffers(pagep[0]);
394         for (i = 0; i < data_offset_in_page; i++)
395                 bh = bh->b_this_page;
396         for (i = 0; i < block_len_in_page; i++) {
397                 *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
398                 if (*err < 0)
399                         break;
400                 bh = bh->b_this_page;
401         }
402         if (!*err)
403                 *err = block_commit_write(pagep[0], from, from + replaced_size);
404 
405         if (unlikely(*err < 0))
406                 goto repair_branches;
407 
408         /* Even in case of data=writeback it is reasonable to pin
409          * inode to transaction, to prevent unexpected data loss */
410         *err = ext4_jbd2_file_inode(handle, orig_inode);
411 
412 unlock_pages:
413         unlock_page(pagep[0]);
414         page_cache_release(pagep[0]);
415         unlock_page(pagep[1]);
416         page_cache_release(pagep[1]);
417 stop_journal:
418         ext4_journal_stop(handle);
419         if (*err == -ENOSPC &&
420             ext4_should_retry_alloc(sb, &retries))
421                 goto again;
422         /* Buffer was busy because probably is pinned to journal transaction,
423          * force transaction commit may help to free it. */
424         if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
425             jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
426                 goto again;
427         return replaced_count;
428 
429 repair_branches:
430         /*
431          * This should never ever happen!
432          * Extents are swapped already, but we are not able to copy data.
433          * Try to swap extents to it's original places
434          */
435         ext4_double_down_write_data_sem(orig_inode, donor_inode);
436         replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
437                                                orig_blk_offset, donor_blk_offset,
438                                            block_len_in_page, 0, &err2);
439         ext4_double_up_write_data_sem(orig_inode, donor_inode);
440         if (replaced_count != block_len_in_page) {
441                 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
442                                        "Unable to copy data block,"
443                                        " data will be lost.");
444                 *err = -EIO;
445         }
446         replaced_count = 0;
447         goto unlock_pages;
448 }
449 
450 /**
451  * mext_check_arguments - Check whether move extent can be done
452  *
453  * @orig_inode:         original inode
454  * @donor_inode:        donor inode
455  * @orig_start:         logical start offset in block for orig
456  * @donor_start:        logical start offset in block for donor
457  * @len:                the number of blocks to be moved
458  *
459  * Check the arguments of ext4_move_extents() whether the files can be
460  * exchanged with each other.
461  * Return 0 on success, or a negative error value on failure.
462  */
463 static int
464 mext_check_arguments(struct inode *orig_inode,
465                      struct inode *donor_inode, __u64 orig_start,
466                      __u64 donor_start, __u64 *len)
467 {
468         __u64 orig_eof, donor_eof;
469         unsigned int blkbits = orig_inode->i_blkbits;
470         unsigned int blocksize = 1 << blkbits;
471 
472         orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
473         donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
474 
475 
476         if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
477                 ext4_debug("ext4 move extent: suid or sgid is set"
478                            " to donor file [ino:orig %lu, donor %lu]\n",
479                            orig_inode->i_ino, donor_inode->i_ino);
480                 return -EINVAL;
481         }
482 
483         if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
484                 return -EPERM;
485 
486         /* Ext4 move extent does not support swapfile */
487         if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
488                 ext4_debug("ext4 move extent: The argument files should "
489                         "not be swapfile [ino:orig %lu, donor %lu]\n",
490                         orig_inode->i_ino, donor_inode->i_ino);
491                 return -EBUSY;
492         }
493 
494         if (IS_NOQUOTA(orig_inode) || IS_NOQUOTA(donor_inode)) {
495                 ext4_debug("ext4 move extent: The argument files should "
496                         "not be quota files [ino:orig %lu, donor %lu]\n",
497                         orig_inode->i_ino, donor_inode->i_ino);
498                 return -EBUSY;
499         }
500 
501         /* Ext4 move extent supports only extent based file */
502         if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
503                 ext4_debug("ext4 move extent: orig file is not extents "
504                         "based file [ino:orig %lu]\n", orig_inode->i_ino);
505                 return -EOPNOTSUPP;
506         } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
507                 ext4_debug("ext4 move extent: donor file is not extents "
508                         "based file [ino:donor %lu]\n", donor_inode->i_ino);
509                 return -EOPNOTSUPP;
510         }
511 
512         if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
513                 ext4_debug("ext4 move extent: File size is 0 byte\n");
514                 return -EINVAL;
515         }
516 
517         /* Start offset should be same */
518         if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
519             (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
520                 ext4_debug("ext4 move extent: orig and donor's start "
521                         "offset are not alligned [ino:orig %lu, donor %lu]\n",
522                         orig_inode->i_ino, donor_inode->i_ino);
523                 return -EINVAL;
524         }
525 
526         if ((orig_start >= EXT_MAX_BLOCKS) ||
527             (donor_start >= EXT_MAX_BLOCKS) ||
528             (*len > EXT_MAX_BLOCKS) ||
529             (donor_start + *len >= EXT_MAX_BLOCKS) ||
530             (orig_start + *len >= EXT_MAX_BLOCKS))  {
531                 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
532                         "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
533                         orig_inode->i_ino, donor_inode->i_ino);
534                 return -EINVAL;
535         }
536         if (orig_eof <= orig_start)
537                 *len = 0;
538         else if (orig_eof < orig_start + *len - 1)
539                 *len = orig_eof - orig_start;
540         if (donor_eof <= donor_start)
541                 *len = 0;
542         else if (donor_eof < donor_start + *len - 1)
543                 *len = donor_eof - donor_start;
544         if (!*len) {
545                 ext4_debug("ext4 move extent: len should not be 0 "
546                         "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
547                         donor_inode->i_ino);
548                 return -EINVAL;
549         }
550 
551         return 0;
552 }
553 
554 /**
555  * ext4_move_extents - Exchange the specified range of a file
556  *
557  * @o_filp:             file structure of the original file
558  * @d_filp:             file structure of the donor file
559  * @orig_blk:           start offset in block for orig
560  * @donor_blk:          start offset in block for donor
561  * @len:                the number of blocks to be moved
562  * @moved_len:          moved block length
563  *
564  * This function returns 0 and moved block length is set in moved_len
565  * if succeed, otherwise returns error value.
566  *
567  */
568 int
569 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
570                   __u64 donor_blk, __u64 len, __u64 *moved_len)
571 {
572         struct inode *orig_inode = file_inode(o_filp);
573         struct inode *donor_inode = file_inode(d_filp);
574         struct ext4_ext_path *path = NULL;
575         int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
576         ext4_lblk_t o_end, o_start = orig_blk;
577         ext4_lblk_t d_start = donor_blk;
578         int ret;
579 
580         if (orig_inode->i_sb != donor_inode->i_sb) {
581                 ext4_debug("ext4 move extent: The argument files "
582                         "should be in same FS [ino:orig %lu, donor %lu]\n",
583                         orig_inode->i_ino, donor_inode->i_ino);
584                 return -EINVAL;
585         }
586 
587         /* orig and donor should be different inodes */
588         if (orig_inode == donor_inode) {
589                 ext4_debug("ext4 move extent: The argument files should not "
590                         "be same inode [ino:orig %lu, donor %lu]\n",
591                         orig_inode->i_ino, donor_inode->i_ino);
592                 return -EINVAL;
593         }
594 
595         /* Regular file check */
596         if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
597                 ext4_debug("ext4 move extent: The argument files should be "
598                         "regular file [ino:orig %lu, donor %lu]\n",
599                         orig_inode->i_ino, donor_inode->i_ino);
600                 return -EINVAL;
601         }
602         /* TODO: This is non obvious task to swap blocks for inodes with full
603            jornaling enabled */
604         if (ext4_should_journal_data(orig_inode) ||
605             ext4_should_journal_data(donor_inode)) {
606                 return -EINVAL;
607         }
608         /* Protect orig and donor inodes against a truncate */
609         lock_two_nondirectories(orig_inode, donor_inode);
610 
611         /* Wait for all existing dio workers */
612         ext4_inode_block_unlocked_dio(orig_inode);
613         ext4_inode_block_unlocked_dio(donor_inode);
614         inode_dio_wait(orig_inode);
615         inode_dio_wait(donor_inode);
616 
617         /* Protect extent tree against block allocations via delalloc */
618         ext4_double_down_write_data_sem(orig_inode, donor_inode);
619         /* Check the filesystem environment whether move_extent can be done */
620         ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
621                                     donor_blk, &len);
622         if (ret)
623                 goto out;
624         o_end = o_start + len;
625 
626         while (o_start < o_end) {
627                 struct ext4_extent *ex;
628                 ext4_lblk_t cur_blk, next_blk;
629                 pgoff_t orig_page_index, donor_page_index;
630                 int offset_in_page;
631                 int unwritten, cur_len;
632 
633                 ret = get_ext_path(orig_inode, o_start, &path);
634                 if (ret)
635                         goto out;
636                 ex = path[path->p_depth].p_ext;
637                 next_blk = ext4_ext_next_allocated_block(path);
638                 cur_blk = le32_to_cpu(ex->ee_block);
639                 cur_len = ext4_ext_get_actual_len(ex);
640                 /* Check hole before the start pos */
641                 if (cur_blk + cur_len - 1 < o_start) {
642                         if (next_blk == EXT_MAX_BLOCKS) {
643                                 o_start = o_end;
644                                 ret = -ENODATA;
645                                 goto out;
646                         }
647                         d_start += next_blk - o_start;
648                         o_start = next_blk;
649                         continue;
650                 /* Check hole after the start pos */
651                 } else if (cur_blk > o_start) {
652                         /* Skip hole */
653                         d_start += cur_blk - o_start;
654                         o_start = cur_blk;
655                         /* Extent inside requested range ?*/
656                         if (cur_blk >= o_end)
657                                 goto out;
658                 } else { /* in_range(o_start, o_blk, o_len) */
659                         cur_len += cur_blk - o_start;
660                 }
661                 unwritten = ext4_ext_is_unwritten(ex);
662                 if (o_end - o_start < cur_len)
663                         cur_len = o_end - o_start;
664 
665                 orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
666                                                orig_inode->i_blkbits);
667                 donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
668                                                donor_inode->i_blkbits);
669                 offset_in_page = o_start % blocks_per_page;
670                 if (cur_len > blocks_per_page- offset_in_page)
671                         cur_len = blocks_per_page - offset_in_page;
672                 /*
673                  * Up semaphore to avoid following problems:
674                  * a. transaction deadlock among ext4_journal_start,
675                  *    ->write_begin via pagefault, and jbd2_journal_commit
676                  * b. racing with ->readpage, ->write_begin, and ext4_get_block
677                  *    in move_extent_per_page
678                  */
679                 ext4_double_up_write_data_sem(orig_inode, donor_inode);
680                 /* Swap original branches with new branches */
681                 move_extent_per_page(o_filp, donor_inode,
682                                      orig_page_index, donor_page_index,
683                                      offset_in_page, cur_len,
684                                      unwritten, &ret);
685                 ext4_double_down_write_data_sem(orig_inode, donor_inode);
686                 if (ret < 0)
687                         break;
688                 o_start += cur_len;
689                 d_start += cur_len;
690         }
691         *moved_len = o_start - orig_blk;
692         if (*moved_len > len)
693                 *moved_len = len;
694 
695 out:
696         if (*moved_len) {
697                 ext4_discard_preallocations(orig_inode);
698                 ext4_discard_preallocations(donor_inode);
699         }
700 
701         ext4_ext_drop_refs(path);
702         kfree(path);
703         ext4_double_up_write_data_sem(orig_inode, donor_inode);
704         ext4_inode_resume_unlocked_dio(orig_inode);
705         ext4_inode_resume_unlocked_dio(donor_inode);
706         unlock_two_nondirectories(orig_inode, donor_inode);
707 
708         return ret;
709 }
710 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp