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