Changes in 4.19.307
PCI: mediatek: Clear interrupt status before dispatching handler
include/linux/units.h: add helpers for kelvin to/from Celsius conversion
units: Add Watt units
units: change from 'L' to 'UL'
units: add the HZ macros
serial: sc16is7xx: set safe default SPI clock frequency
driver core: add device probe log helper
spi: introduce SPI_MODE_X_MASK macro
serial: sc16is7xx: add check for unsupported SPI modes during probe
ext4: allow for the last group to be marked as trimmed
crypto: api - Disallow identical driver names
PM: hibernate: Enforce ordering during image compression/decompression
hwrng: core - Fix page fault dead lock on mmap-ed hwrng
rpmsg: virtio: Free driver_override when rpmsg_remove()
parisc/firmware: Fix F-extend for PDC addresses
nouveau/vmm: don't set addr on the fail path to avoid warning
block: Remove special-casing of compound pages
powerpc: Use always instead of always-y in for crtsavres.o
x86/CPU/AMD: Fix disabling XSAVES on AMD family 0x17 due to erratum
driver core: Annotate dev_err_probe() with __must_check
Revert "driver core: Annotate dev_err_probe() with __must_check"
driver code: print symbolic error code
drivers: core: fix kernel-doc markup for dev_err_probe()
net/smc: fix illegal rmb_desc access in SMC-D connection dump
vlan: skip nested type that is not IFLA_VLAN_QOS_MAPPING
llc: make llc_ui_sendmsg() more robust against bonding changes
llc: Drop support for ETH_P_TR_802_2.
net/rds: Fix UBSAN: array-index-out-of-bounds in rds_cmsg_recv
tracing: Ensure visibility when inserting an element into tracing_map
tcp: Add memory barrier to tcp_push()
netlink: fix potential sleeping issue in mqueue_flush_file
net/mlx5: Use kfree(ft->g) in arfs_create_groups()
net/mlx5e: fix a double-free in arfs_create_groups
netfilter: nf_tables: restrict anonymous set and map names to 16 bytes
fjes: fix memleaks in fjes_hw_setup
net: fec: fix the unhandled context fault from smmu
btrfs: don't warn if discard range is not aligned to sector
btrfs: defrag: reject unknown flags of btrfs_ioctl_defrag_range_args
netfilter: nf_tables: reject QUEUE/DROP verdict parameters
gpiolib: acpi: Ignore touchpad wakeup on GPD G1619-04
drm: Don't unref the same fb many times by mistake due to deadlock handling
drm/bridge: nxp-ptn3460: fix i2c_master_send() error checking
drm/bridge: nxp-ptn3460: simplify some error checking
drm/exynos: gsc: minor fix for loop iteration in gsc_runtime_resume
gpio: eic-sprd: Clear interrupt after set the interrupt type
mips: Call lose_fpu(0) before initializing fcr31 in mips_set_personality_nan
tick/sched: Preserve number of idle sleeps across CPU hotplug events
x86/entry/ia32: Ensure s32 is sign extended to s64
net/sched: cbs: Fix not adding cbs instance to list
powerpc/mm: Fix null-pointer dereference in pgtable_cache_add
powerpc: Fix build error due to is_valid_bugaddr()
powerpc/mm: Fix build failures due to arch_reserved_kernel_pages()
powerpc/lib: Validate size for vector operations
audit: Send netlink ACK before setting connection in auditd_set
ACPI: video: Add quirk for the Colorful X15 AT 23 Laptop
PNP: ACPI: fix fortify warning
ACPI: extlog: fix NULL pointer dereference check
FS:JFS:UBSAN:array-index-out-of-bounds in dbAdjTree
UBSAN: array-index-out-of-bounds in dtSplitRoot
jfs: fix slab-out-of-bounds Read in dtSearch
jfs: fix array-index-out-of-bounds in dbAdjTree
jfs: fix uaf in jfs_evict_inode
pstore/ram: Fix crash when setting number of cpus to an odd number
crypto: stm32/crc32 - fix parsing list of devices
afs: fix the usage of read_seqbegin_or_lock() in afs_find_server*()
rxrpc_find_service_conn_rcu: fix the usage of read_seqbegin_or_lock()
jfs: fix array-index-out-of-bounds in diNewExt
s390/ptrace: handle setting of fpc register correctly
KVM: s390: fix setting of fpc register
SUNRPC: Fix a suspicious RCU usage warning
ext4: fix inconsistent between segment fstrim and full fstrim
ext4: unify the type of flexbg_size to unsigned int
ext4: remove unnecessary check from alloc_flex_gd()
ext4: avoid online resizing failures due to oversized flex bg
scsi: lpfc: Fix possible file string name overflow when updating firmware
PCI: Add no PM reset quirk for NVIDIA Spectrum devices
bonding: return -ENOMEM instead of BUG in alb_upper_dev_walk
ARM: dts: imx7s: Fix lcdif compatible
ARM: dts: imx7s: Fix nand-controller #size-cells
wifi: ath9k: Fix potential array-index-out-of-bounds read in ath9k_htc_txstatus()
bpf: Add map and need_defer parameters to .map_fd_put_ptr()
scsi: libfc: Don't schedule abort twice
scsi: libfc: Fix up timeout error in fc_fcp_rec_error()
ARM: dts: rockchip: fix rk3036 hdmi ports node
ARM: dts: imx25/27-eukrea: Fix RTC node name
ARM: dts: imx: Use flash@0,0 pattern
ARM: dts: imx27: Fix sram node
ARM: dts: imx1: Fix sram node
ARM: dts: imx27-apf27dev: Fix LED name
ARM: dts: imx23-sansa: Use preferred i2c-gpios properties
ARM: dts: imx23/28: Fix the DMA controller node name
md: Whenassemble the array, consult the superblock of the freshest device
wifi: rtl8xxxu: Add additional USB IDs for RTL8192EU devices
wifi: rtlwifi: rtl8723{be,ae}: using calculate_bit_shift()
wifi: cfg80211: free beacon_ies when overridden from hidden BSS
f2fs: fix to check return value of f2fs_reserve_new_block()
ASoC: doc: Fix undefined SND_SOC_DAPM_NOPM argument
fast_dput(): handle underflows gracefully
RDMA/IPoIB: Fix error code return in ipoib_mcast_join
drm/drm_file: fix use of uninitialized variable
drm/framebuffer: Fix use of uninitialized variable
drm/mipi-dsi: Fix detach call without attach
media: stk1160: Fixed high volume of stk1160_dbg messages
media: rockchip: rga: fix swizzling for RGB formats
PCI: add INTEL_HDA_ARL to pci_ids.h
ALSA: hda: Intel: add HDA_ARL PCI ID support
drm/exynos: Call drm_atomic_helper_shutdown() at shutdown/unbind time
IB/ipoib: Fix mcast list locking
media: ddbridge: fix an error code problem in ddb_probe
drm/msm/dpu: Ratelimit framedone timeout msgs
clk: hi3620: Fix memory leak in hi3620_mmc_clk_init()
clk: mmp: pxa168: Fix memory leak in pxa168_clk_init()
drm/amdgpu: Let KFD sync with VM fences
drm/amdgpu: Drop 'fence' check in 'to_amdgpu_amdkfd_fence()'
leds: trigger: panic: Don't register panic notifier if creating the trigger failed
um: Fix naming clash between UML and scheduler
um: Don't use vfprintf() for os_info()
um: net: Fix return type of uml_net_start_xmit()
mfd: ti_am335x_tscadc: Fix TI SoC dependencies
PCI: Only override AMD USB controller if required
usb: hub: Replace hardcoded quirk value with BIT() macro
libsubcmd: Fix memory leak in uniq()
virtio_net: Fix "‘%d’ directive writing between 1 and 11 bytes into a region of size 10" warnings
blk-mq: fix IO hang from sbitmap wakeup race
ceph: fix deadlock or deadcode of misusing dget()
drm/amdgpu: Release 'adev->pm.fw' before return in 'amdgpu_device_need_post()'
wifi: cfg80211: fix RCU dereference in __cfg80211_bss_update
scsi: isci: Fix an error code problem in isci_io_request_build()
net: remove unneeded break
ixgbe: Remove non-inclusive language
ixgbe: Refactor returning internal error codes
ixgbe: Refactor overtemp event handling
ixgbe: Fix an error handling path in ixgbe_read_iosf_sb_reg_x550()
ipv6: Ensure natural alignment of const ipv6 loopback and router addresses
llc: call sock_orphan() at release time
netfilter: nf_log: replace BUG_ON by WARN_ON_ONCE when putting logger
net: ipv4: fix a memleak in ip_setup_cork
af_unix: fix lockdep positive in sk_diag_dump_icons()
net: sysfs: Fix /sys/class/net/<iface> path
HID: apple: Add support for the 2021 Magic Keyboard
HID: apple: Swap the Fn and Left Control keys on Apple keyboards
HID: apple: Add 2021 magic keyboard FN key mapping
bonding: remove print in bond_verify_device_path
dmaengine: fix is_slave_direction() return false when DMA_DEV_TO_DEV
phy: ti: phy-omap-usb2: Fix NULL pointer dereference for SRP
atm: idt77252: fix a memleak in open_card_ubr0
hwmon: (aspeed-pwm-tacho) mutex for tach reading
hwmon: (coretemp) Fix out-of-bounds memory access
hwmon: (coretemp) Fix bogus core_id to attr name mapping
inet: read sk->sk_family once in inet_recv_error()
rxrpc: Fix response to PING RESPONSE ACKs to a dead call
tipc: Check the bearer type before calling tipc_udp_nl_bearer_add()
ppp_async: limit MRU to 64K
netfilter: nft_compat: reject unused compat flag
netfilter: nft_compat: restrict match/target protocol to u16
net/af_iucv: clean up a try_then_request_module()
USB: serial: qcserial: add new usb-id for Dell Wireless DW5826e
USB: serial: option: add Fibocom FM101-GL variant
USB: serial: cp210x: add ID for IMST iM871A-USB
Input: atkbd - skip ATKBD_CMD_SETLEDS when skipping ATKBD_CMD_GETID
vhost: use kzalloc() instead of kmalloc() followed by memset()
hrtimer: Report offline hrtimer enqueue
btrfs: forbid creating subvol qgroups
btrfs: send: return EOPNOTSUPP on unknown flags
spi: ppc4xx: Drop write-only variable
ASoC: rt5645: Fix deadlock in rt5645_jack_detect_work()
Documentation: net-sysfs: describe missing statistics
net: sysfs: Fix /sys/class/net/<iface> path for statistics
MIPS: Add 'memory' clobber to csum_ipv6_magic() inline assembler
i40e: Fix waiting for queues of all VSIs to be disabled
tracing/trigger: Fix to return error if failed to alloc snapshot
mm/writeback: fix possible divide-by-zero in wb_dirty_limits(), again
HID: wacom: generic: Avoid reporting a serial of '0' to userspace
HID: wacom: Do not register input devices until after hid_hw_start
USB: hub: check for alternate port before enabling A_ALT_HNP_SUPPORT
usb: f_mass_storage: forbid async queue when shutdown happen
scsi: Revert "scsi: fcoe: Fix potential deadlock on &fip->ctlr_lock"
firewire: core: correct documentation of fw_csr_string() kernel API
nfc: nci: free rx_data_reassembly skb on NCI device cleanup
xen-netback: properly sync TX responses
binder: signal epoll threads of self-work
ext4: fix double-free of blocks due to wrong extents moved_len
staging: iio: ad5933: fix type mismatch regression
ring-buffer: Clean ring_buffer_poll_wait() error return
serial: max310x: set default value when reading clock ready bit
serial: max310x: improve crystal stable clock detection
x86/Kconfig: Transmeta Crusoe is CPU family 5, not 6
x86/mm/ident_map: Use gbpages only where full GB page should be mapped.
ALSA: hda/conexant: Add quirk for SWS JS201D
nilfs2: fix data corruption in dsync block recovery for small block sizes
nilfs2: fix hang in nilfs_lookup_dirty_data_buffers()
nfp: use correct macro for LengthSelect in BAR config
irqchip/irq-brcmstb-l2: Add write memory barrier before exit
pmdomain: core: Move the unused cleanup to a _sync initcall
Revert "md/raid5: Wait for MD_SB_CHANGE_PENDING in raid5d"
sched/membarrier: reduce the ability to hammer on sys_membarrier
nilfs2: fix potential bug in end_buffer_async_write
lsm: new security_file_ioctl_compat() hook
netfilter: nf_tables: fix pointer math issue in nft_byteorder_eval()
Linux 4.19.307
Change-Id: Ib05aec445afe9920e2502bcfce1c52db76e27139
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
698 lines
21 KiB
C
698 lines
21 KiB
C
// SPDX-License-Identifier: LGPL-2.1
|
|
/*
|
|
* Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
|
|
* Written by Takashi Sato <t-sato@yk.jp.nec.com>
|
|
* Akira Fujita <a-fujita@rs.jp.nec.com>
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
|
#include <linux/quotaops.h>
|
|
#include <linux/slab.h>
|
|
#include "ext4_jbd2.h"
|
|
#include "ext4.h"
|
|
#include "ext4_extents.h"
|
|
|
|
/**
|
|
* get_ext_path - Find an extent path for designated logical block number.
|
|
*
|
|
* @inode: an inode which is searched
|
|
* @lblock: logical block number to find an extent path
|
|
* @path: pointer to an extent path pointer (for output)
|
|
*
|
|
* ext4_find_extent wrapper. Return 0 on success, or a negative error value
|
|
* on failure.
|
|
*/
|
|
static inline int
|
|
get_ext_path(struct inode *inode, ext4_lblk_t lblock,
|
|
struct ext4_ext_path **ppath)
|
|
{
|
|
struct ext4_ext_path *path;
|
|
|
|
path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
|
|
if (IS_ERR(path))
|
|
return PTR_ERR(path);
|
|
if (path[ext_depth(inode)].p_ext == NULL) {
|
|
ext4_ext_drop_refs(path);
|
|
kfree(path);
|
|
*ppath = NULL;
|
|
return -ENODATA;
|
|
}
|
|
*ppath = path;
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* ext4_double_down_write_data_sem - Acquire two inodes' write lock
|
|
* of i_data_sem
|
|
*
|
|
* Acquire write lock of i_data_sem of the two inodes
|
|
*/
|
|
void
|
|
ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
|
|
{
|
|
if (first < second) {
|
|
down_write(&EXT4_I(first)->i_data_sem);
|
|
down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER);
|
|
} else {
|
|
down_write(&EXT4_I(second)->i_data_sem);
|
|
down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER);
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
|
|
*
|
|
* @orig_inode: original inode structure to be released its lock first
|
|
* @donor_inode: donor inode structure to be released its lock second
|
|
* Release write lock of i_data_sem of two inodes (orig and donor).
|
|
*/
|
|
void
|
|
ext4_double_up_write_data_sem(struct inode *orig_inode,
|
|
struct inode *donor_inode)
|
|
{
|
|
up_write(&EXT4_I(orig_inode)->i_data_sem);
|
|
up_write(&EXT4_I(donor_inode)->i_data_sem);
|
|
}
|
|
|
|
/**
|
|
* mext_check_coverage - Check that all extents in range has the same type
|
|
*
|
|
* @inode: inode in question
|
|
* @from: block offset of inode
|
|
* @count: block count to be checked
|
|
* @unwritten: extents expected to be unwritten
|
|
* @err: pointer to save error value
|
|
*
|
|
* Return 1 if all extents in range has expected type, and zero otherwise.
|
|
*/
|
|
static int
|
|
mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
|
|
int unwritten, int *err)
|
|
{
|
|
struct ext4_ext_path *path = NULL;
|
|
struct ext4_extent *ext;
|
|
int ret = 0;
|
|
ext4_lblk_t last = from + count;
|
|
while (from < last) {
|
|
*err = get_ext_path(inode, from, &path);
|
|
if (*err)
|
|
goto out;
|
|
ext = path[ext_depth(inode)].p_ext;
|
|
if (unwritten != ext4_ext_is_unwritten(ext))
|
|
goto out;
|
|
from += ext4_ext_get_actual_len(ext);
|
|
ext4_ext_drop_refs(path);
|
|
}
|
|
ret = 1;
|
|
out:
|
|
ext4_ext_drop_refs(path);
|
|
kfree(path);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
|
|
*
|
|
* @inode1: the inode structure
|
|
* @inode2: the inode structure
|
|
* @index1: page index
|
|
* @index2: page index
|
|
* @page: result page vector
|
|
*
|
|
* Grab two locked pages for inode's by inode order
|
|
*/
|
|
static int
|
|
mext_page_double_lock(struct inode *inode1, struct inode *inode2,
|
|
pgoff_t index1, pgoff_t index2, struct page *page[2])
|
|
{
|
|
struct address_space *mapping[2];
|
|
unsigned fl = AOP_FLAG_NOFS;
|
|
|
|
BUG_ON(!inode1 || !inode2);
|
|
if (inode1 < inode2) {
|
|
mapping[0] = inode1->i_mapping;
|
|
mapping[1] = inode2->i_mapping;
|
|
} else {
|
|
swap(index1, index2);
|
|
mapping[0] = inode2->i_mapping;
|
|
mapping[1] = inode1->i_mapping;
|
|
}
|
|
|
|
page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
|
|
if (!page[0])
|
|
return -ENOMEM;
|
|
|
|
page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
|
|
if (!page[1]) {
|
|
unlock_page(page[0]);
|
|
put_page(page[0]);
|
|
return -ENOMEM;
|
|
}
|
|
/*
|
|
* grab_cache_page_write_begin() may not wait on page's writeback if
|
|
* BDI not demand that. But it is reasonable to be very conservative
|
|
* here and explicitly wait on page's writeback
|
|
*/
|
|
wait_on_page_writeback(page[0]);
|
|
wait_on_page_writeback(page[1]);
|
|
if (inode1 > inode2)
|
|
swap(page[0], page[1]);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Force page buffers uptodate w/o dropping page's lock */
|
|
static int
|
|
mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
|
|
{
|
|
struct inode *inode = page->mapping->host;
|
|
sector_t block;
|
|
struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
|
|
unsigned int blocksize, block_start, block_end;
|
|
int i, err, nr = 0, partial = 0;
|
|
BUG_ON(!PageLocked(page));
|
|
BUG_ON(PageWriteback(page));
|
|
|
|
if (PageUptodate(page))
|
|
return 0;
|
|
|
|
blocksize = i_blocksize(inode);
|
|
if (!page_has_buffers(page))
|
|
create_empty_buffers(page, blocksize, 0);
|
|
|
|
head = page_buffers(page);
|
|
block = (sector_t)page->index << (PAGE_SHIFT - inode->i_blkbits);
|
|
for (bh = head, block_start = 0; bh != head || !block_start;
|
|
block++, block_start = block_end, bh = bh->b_this_page) {
|
|
block_end = block_start + blocksize;
|
|
if (block_end <= from || block_start >= to) {
|
|
if (!buffer_uptodate(bh))
|
|
partial = 1;
|
|
continue;
|
|
}
|
|
if (buffer_uptodate(bh))
|
|
continue;
|
|
if (!buffer_mapped(bh)) {
|
|
err = ext4_get_block(inode, block, bh, 0);
|
|
if (err) {
|
|
SetPageError(page);
|
|
return err;
|
|
}
|
|
if (!buffer_mapped(bh)) {
|
|
zero_user(page, block_start, blocksize);
|
|
set_buffer_uptodate(bh);
|
|
continue;
|
|
}
|
|
}
|
|
BUG_ON(nr >= MAX_BUF_PER_PAGE);
|
|
arr[nr++] = bh;
|
|
}
|
|
/* No io required */
|
|
if (!nr)
|
|
goto out;
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
bh = arr[i];
|
|
if (!bh_uptodate_or_lock(bh)) {
|
|
err = bh_submit_read(bh);
|
|
if (err)
|
|
return err;
|
|
}
|
|
}
|
|
out:
|
|
if (!partial)
|
|
SetPageUptodate(page);
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* move_extent_per_page - Move extent data per page
|
|
*
|
|
* @o_filp: file structure of original file
|
|
* @donor_inode: donor inode
|
|
* @orig_page_offset: page index on original file
|
|
* @donor_page_offset: page index on donor file
|
|
* @data_offset_in_page: block index where data swapping starts
|
|
* @block_len_in_page: the number of blocks to be swapped
|
|
* @unwritten: orig extent is unwritten or not
|
|
* @err: pointer to save return value
|
|
*
|
|
* Save the data in original inode blocks and replace original inode extents
|
|
* with donor inode extents by calling ext4_swap_extents().
|
|
* Finally, write out the saved data in new original inode blocks. Return
|
|
* replaced block count.
|
|
*/
|
|
static int
|
|
move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
|
|
pgoff_t orig_page_offset, pgoff_t donor_page_offset,
|
|
int data_offset_in_page,
|
|
int block_len_in_page, int unwritten, int *err)
|
|
{
|
|
struct inode *orig_inode = file_inode(o_filp);
|
|
struct page *pagep[2] = {NULL, NULL};
|
|
handle_t *handle;
|
|
ext4_lblk_t orig_blk_offset, donor_blk_offset;
|
|
unsigned long blocksize = orig_inode->i_sb->s_blocksize;
|
|
unsigned int tmp_data_size, data_size, replaced_size;
|
|
int i, err2, jblocks, retries = 0;
|
|
int replaced_count = 0;
|
|
int from = data_offset_in_page << orig_inode->i_blkbits;
|
|
int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
|
|
struct super_block *sb = orig_inode->i_sb;
|
|
struct buffer_head *bh = NULL;
|
|
|
|
/*
|
|
* It needs twice the amount of ordinary journal buffers because
|
|
* inode and donor_inode may change each different metadata blocks.
|
|
*/
|
|
again:
|
|
*err = 0;
|
|
jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
|
|
handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
|
|
if (IS_ERR(handle)) {
|
|
*err = PTR_ERR(handle);
|
|
return 0;
|
|
}
|
|
|
|
orig_blk_offset = orig_page_offset * blocks_per_page +
|
|
data_offset_in_page;
|
|
|
|
donor_blk_offset = donor_page_offset * blocks_per_page +
|
|
data_offset_in_page;
|
|
|
|
/* Calculate data_size */
|
|
if ((orig_blk_offset + block_len_in_page - 1) ==
|
|
((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
|
|
/* Replace the last block */
|
|
tmp_data_size = orig_inode->i_size & (blocksize - 1);
|
|
/*
|
|
* If data_size equal zero, it shows data_size is multiples of
|
|
* blocksize. So we set appropriate value.
|
|
*/
|
|
if (tmp_data_size == 0)
|
|
tmp_data_size = blocksize;
|
|
|
|
data_size = tmp_data_size +
|
|
((block_len_in_page - 1) << orig_inode->i_blkbits);
|
|
} else
|
|
data_size = block_len_in_page << orig_inode->i_blkbits;
|
|
|
|
replaced_size = data_size;
|
|
|
|
*err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
|
|
donor_page_offset, pagep);
|
|
if (unlikely(*err < 0))
|
|
goto stop_journal;
|
|
/*
|
|
* If orig extent was unwritten it can become initialized
|
|
* at any time after i_data_sem was dropped, in order to
|
|
* serialize with delalloc we have recheck extent while we
|
|
* hold page's lock, if it is still the case data copy is not
|
|
* necessary, just swap data blocks between orig and donor.
|
|
*/
|
|
if (unwritten) {
|
|
ext4_double_down_write_data_sem(orig_inode, donor_inode);
|
|
/* If any of extents in range became initialized we have to
|
|
* fallback to data copying */
|
|
unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
|
|
block_len_in_page, 1, err);
|
|
if (*err)
|
|
goto drop_data_sem;
|
|
|
|
unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
|
|
block_len_in_page, 1, err);
|
|
if (*err)
|
|
goto drop_data_sem;
|
|
|
|
if (!unwritten) {
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
goto data_copy;
|
|
}
|
|
if ((page_has_private(pagep[0]) &&
|
|
!try_to_release_page(pagep[0], 0)) ||
|
|
(page_has_private(pagep[1]) &&
|
|
!try_to_release_page(pagep[1], 0))) {
|
|
*err = -EBUSY;
|
|
goto drop_data_sem;
|
|
}
|
|
replaced_count = ext4_swap_extents(handle, orig_inode,
|
|
donor_inode, orig_blk_offset,
|
|
donor_blk_offset,
|
|
block_len_in_page, 1, err);
|
|
drop_data_sem:
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
goto unlock_pages;
|
|
}
|
|
data_copy:
|
|
*err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
|
|
if (*err)
|
|
goto unlock_pages;
|
|
|
|
/* At this point all buffers in range are uptodate, old mapping layout
|
|
* is no longer required, try to drop it now. */
|
|
if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
|
|
(page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
|
|
*err = -EBUSY;
|
|
goto unlock_pages;
|
|
}
|
|
ext4_double_down_write_data_sem(orig_inode, donor_inode);
|
|
replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
|
|
orig_blk_offset, donor_blk_offset,
|
|
block_len_in_page, 1, err);
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
if (*err) {
|
|
if (replaced_count) {
|
|
block_len_in_page = replaced_count;
|
|
replaced_size =
|
|
block_len_in_page << orig_inode->i_blkbits;
|
|
} else
|
|
goto unlock_pages;
|
|
}
|
|
/* Perform all necessary steps similar write_begin()/write_end()
|
|
* but keeping in mind that i_size will not change */
|
|
if (!page_has_buffers(pagep[0]))
|
|
create_empty_buffers(pagep[0], 1 << orig_inode->i_blkbits, 0);
|
|
bh = page_buffers(pagep[0]);
|
|
for (i = 0; i < data_offset_in_page; i++)
|
|
bh = bh->b_this_page;
|
|
for (i = 0; i < block_len_in_page; i++) {
|
|
*err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
|
|
if (*err < 0)
|
|
break;
|
|
bh = bh->b_this_page;
|
|
}
|
|
if (!*err)
|
|
*err = block_commit_write(pagep[0], from, from + replaced_size);
|
|
|
|
if (unlikely(*err < 0))
|
|
goto repair_branches;
|
|
|
|
/* Even in case of data=writeback it is reasonable to pin
|
|
* inode to transaction, to prevent unexpected data loss */
|
|
*err = ext4_jbd2_inode_add_write(handle, orig_inode,
|
|
(loff_t)orig_page_offset << PAGE_SHIFT, replaced_size);
|
|
|
|
unlock_pages:
|
|
unlock_page(pagep[0]);
|
|
put_page(pagep[0]);
|
|
unlock_page(pagep[1]);
|
|
put_page(pagep[1]);
|
|
stop_journal:
|
|
ext4_journal_stop(handle);
|
|
if (*err == -ENOSPC &&
|
|
ext4_should_retry_alloc(sb, &retries))
|
|
goto again;
|
|
/* Buffer was busy because probably is pinned to journal transaction,
|
|
* force transaction commit may help to free it. */
|
|
if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
|
|
jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
|
|
goto again;
|
|
return replaced_count;
|
|
|
|
repair_branches:
|
|
/*
|
|
* This should never ever happen!
|
|
* Extents are swapped already, but we are not able to copy data.
|
|
* Try to swap extents to it's original places
|
|
*/
|
|
ext4_double_down_write_data_sem(orig_inode, donor_inode);
|
|
replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
|
|
orig_blk_offset, donor_blk_offset,
|
|
block_len_in_page, 0, &err2);
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
if (replaced_count != block_len_in_page) {
|
|
EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
|
|
"Unable to copy data block,"
|
|
" data will be lost.");
|
|
*err = -EIO;
|
|
}
|
|
replaced_count = 0;
|
|
goto unlock_pages;
|
|
}
|
|
|
|
/**
|
|
* mext_check_arguments - Check whether move extent can be done
|
|
*
|
|
* @orig_inode: original inode
|
|
* @donor_inode: donor inode
|
|
* @orig_start: logical start offset in block for orig
|
|
* @donor_start: logical start offset in block for donor
|
|
* @len: the number of blocks to be moved
|
|
*
|
|
* Check the arguments of ext4_move_extents() whether the files can be
|
|
* exchanged with each other.
|
|
* Return 0 on success, or a negative error value on failure.
|
|
*/
|
|
static int
|
|
mext_check_arguments(struct inode *orig_inode,
|
|
struct inode *donor_inode, __u64 orig_start,
|
|
__u64 donor_start, __u64 *len)
|
|
{
|
|
__u64 orig_eof, donor_eof;
|
|
unsigned int blkbits = orig_inode->i_blkbits;
|
|
unsigned int blocksize = 1 << blkbits;
|
|
|
|
orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
|
|
donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
|
|
|
|
|
|
if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
|
|
ext4_debug("ext4 move extent: suid or sgid is set"
|
|
" to donor file [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
|
|
return -EPERM;
|
|
|
|
/* Ext4 move extent does not support swapfile */
|
|
if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
|
|
ext4_debug("ext4 move extent: The argument files should "
|
|
"not be swapfile [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EBUSY;
|
|
}
|
|
|
|
if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) {
|
|
ext4_debug("ext4 move extent: The argument files should "
|
|
"not be quota files [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EBUSY;
|
|
}
|
|
|
|
/* Ext4 move extent supports only extent based file */
|
|
if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
|
|
ext4_debug("ext4 move extent: orig file is not extents "
|
|
"based file [ino:orig %lu]\n", orig_inode->i_ino);
|
|
return -EOPNOTSUPP;
|
|
} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
|
|
ext4_debug("ext4 move extent: donor file is not extents "
|
|
"based file [ino:donor %lu]\n", donor_inode->i_ino);
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
|
|
ext4_debug("ext4 move extent: File size is 0 byte\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* Start offset should be same */
|
|
if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
|
|
(donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
|
|
ext4_debug("ext4 move extent: orig and donor's start "
|
|
"offsets are not aligned [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if ((orig_start >= EXT_MAX_BLOCKS) ||
|
|
(donor_start >= EXT_MAX_BLOCKS) ||
|
|
(*len > EXT_MAX_BLOCKS) ||
|
|
(donor_start + *len >= EXT_MAX_BLOCKS) ||
|
|
(orig_start + *len >= EXT_MAX_BLOCKS)) {
|
|
ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
|
|
"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
if (orig_eof <= orig_start)
|
|
*len = 0;
|
|
else if (orig_eof < orig_start + *len - 1)
|
|
*len = orig_eof - orig_start;
|
|
if (donor_eof <= donor_start)
|
|
*len = 0;
|
|
else if (donor_eof < donor_start + *len - 1)
|
|
*len = donor_eof - donor_start;
|
|
if (!*len) {
|
|
ext4_debug("ext4 move extent: len should not be 0 "
|
|
"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
|
|
donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* ext4_move_extents - Exchange the specified range of a file
|
|
*
|
|
* @o_filp: file structure of the original file
|
|
* @d_filp: file structure of the donor file
|
|
* @orig_blk: start offset in block for orig
|
|
* @donor_blk: start offset in block for donor
|
|
* @len: the number of blocks to be moved
|
|
* @moved_len: moved block length
|
|
*
|
|
* This function returns 0 and moved block length is set in moved_len
|
|
* if succeed, otherwise returns error value.
|
|
*
|
|
*/
|
|
int
|
|
ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
|
|
__u64 donor_blk, __u64 len, __u64 *moved_len)
|
|
{
|
|
struct inode *orig_inode = file_inode(o_filp);
|
|
struct inode *donor_inode = file_inode(d_filp);
|
|
struct ext4_ext_path *path = NULL;
|
|
int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
|
|
ext4_lblk_t o_end, o_start = orig_blk;
|
|
ext4_lblk_t d_start = donor_blk;
|
|
int ret;
|
|
|
|
if (orig_inode->i_sb != donor_inode->i_sb) {
|
|
ext4_debug("ext4 move extent: The argument files "
|
|
"should be in same FS [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* orig and donor should be different inodes */
|
|
if (orig_inode == donor_inode) {
|
|
ext4_debug("ext4 move extent: The argument files should not "
|
|
"be same inode [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* Regular file check */
|
|
if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
|
|
ext4_debug("ext4 move extent: The argument files should be "
|
|
"regular file [ino:orig %lu, donor %lu]\n",
|
|
orig_inode->i_ino, donor_inode->i_ino);
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* TODO: it's not obvious how to swap blocks for inodes with full
|
|
journaling enabled */
|
|
if (ext4_should_journal_data(orig_inode) ||
|
|
ext4_should_journal_data(donor_inode)) {
|
|
ext4_msg(orig_inode->i_sb, KERN_ERR,
|
|
"Online defrag not supported with data journaling");
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) {
|
|
ext4_msg(orig_inode->i_sb, KERN_ERR,
|
|
"Online defrag not supported for encrypted files");
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
/* Protect orig and donor inodes against a truncate */
|
|
lock_two_nondirectories(orig_inode, donor_inode);
|
|
|
|
/* Wait for all existing dio workers */
|
|
inode_dio_wait(orig_inode);
|
|
inode_dio_wait(donor_inode);
|
|
|
|
/* Protect extent tree against block allocations via delalloc */
|
|
ext4_double_down_write_data_sem(orig_inode, donor_inode);
|
|
/* Check the filesystem environment whether move_extent can be done */
|
|
ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
|
|
donor_blk, &len);
|
|
if (ret)
|
|
goto out;
|
|
o_end = o_start + len;
|
|
|
|
*moved_len = 0;
|
|
while (o_start < o_end) {
|
|
struct ext4_extent *ex;
|
|
ext4_lblk_t cur_blk, next_blk;
|
|
pgoff_t orig_page_index, donor_page_index;
|
|
int offset_in_page;
|
|
int unwritten, cur_len;
|
|
|
|
ret = get_ext_path(orig_inode, o_start, &path);
|
|
if (ret)
|
|
goto out;
|
|
ex = path[path->p_depth].p_ext;
|
|
next_blk = ext4_ext_next_allocated_block(path);
|
|
cur_blk = le32_to_cpu(ex->ee_block);
|
|
cur_len = ext4_ext_get_actual_len(ex);
|
|
/* Check hole before the start pos */
|
|
if (cur_blk + cur_len - 1 < o_start) {
|
|
if (next_blk == EXT_MAX_BLOCKS) {
|
|
o_start = o_end;
|
|
ret = -ENODATA;
|
|
goto out;
|
|
}
|
|
d_start += next_blk - o_start;
|
|
o_start = next_blk;
|
|
continue;
|
|
/* Check hole after the start pos */
|
|
} else if (cur_blk > o_start) {
|
|
/* Skip hole */
|
|
d_start += cur_blk - o_start;
|
|
o_start = cur_blk;
|
|
/* Extent inside requested range ?*/
|
|
if (cur_blk >= o_end)
|
|
goto out;
|
|
} else { /* in_range(o_start, o_blk, o_len) */
|
|
cur_len += cur_blk - o_start;
|
|
}
|
|
unwritten = ext4_ext_is_unwritten(ex);
|
|
if (o_end - o_start < cur_len)
|
|
cur_len = o_end - o_start;
|
|
|
|
orig_page_index = o_start >> (PAGE_SHIFT -
|
|
orig_inode->i_blkbits);
|
|
donor_page_index = d_start >> (PAGE_SHIFT -
|
|
donor_inode->i_blkbits);
|
|
offset_in_page = o_start % blocks_per_page;
|
|
if (cur_len > blocks_per_page- offset_in_page)
|
|
cur_len = blocks_per_page - offset_in_page;
|
|
/*
|
|
* Up semaphore to avoid following problems:
|
|
* a. transaction deadlock among ext4_journal_start,
|
|
* ->write_begin via pagefault, and jbd2_journal_commit
|
|
* b. racing with ->readpage, ->write_begin, and ext4_get_block
|
|
* in move_extent_per_page
|
|
*/
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
/* Swap original branches with new branches */
|
|
*moved_len += move_extent_per_page(o_filp, donor_inode,
|
|
orig_page_index, donor_page_index,
|
|
offset_in_page, cur_len,
|
|
unwritten, &ret);
|
|
ext4_double_down_write_data_sem(orig_inode, donor_inode);
|
|
if (ret < 0)
|
|
break;
|
|
o_start += cur_len;
|
|
d_start += cur_len;
|
|
}
|
|
|
|
out:
|
|
if (*moved_len) {
|
|
ext4_discard_preallocations(orig_inode);
|
|
ext4_discard_preallocations(donor_inode);
|
|
}
|
|
|
|
ext4_ext_drop_refs(path);
|
|
kfree(path);
|
|
ext4_double_up_write_data_sem(orig_inode, donor_inode);
|
|
unlock_two_nondirectories(orig_inode, donor_inode);
|
|
|
|
return ret;
|
|
}
|