mirror of
https://github.com/rd-stuffs/msm-4.14.git
synced 2025-02-20 11:45:48 +08:00
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAltj7TUACgkQONu9yGCS aT6EYRAAvqCTCQmQfcduTp5ua9v3EatAhiCALUSS0SYwDNS0bbqRWaP7U/crvTs4 tJu2JY3bGFTvLSUHQyt/+0uwu4IKaMyerXCXdV0OcScpASsjbA18dbRY7zJEZjRt g6seezpE9CCnWbPxh3SraIeRfxoITUCH6Gjz0NTJvKwbLiK3XgsgkItOZxBbf1ib Qnu+9wLtbU90plBlY0ctgKpAa1UPJd3B2h7YGW4FfArEkDJ8M+loQygzd35rQfw/ s7R36nt1aaT5k+biQTxiWdpTmOL3fd1mC2OG4mBInc/u7qUtAFikBXbpQWfKochy fSxjC+GMTWWoYmqdV7bnWuc/GgmmxXR3nBv6LZhR58i0NfMjmzU1kJhnN4Ea5xA8 4qOZcd9UFLq1Yn44skgN9dm0jDzJaSDhWe4/XX9v7I1YqbDqC1mEr0i2yLtj5FXZ kTpcrieDBBzoY+GNZZnfWgmzg4V5W+1AM0k7PT7AoMbAJf57YF0rRoidlPE2sMQo UEhIXTHjK/jACOFNNdFc+DsJjJz0nDV4FxZJ8Y6EGF6uJERnspAxbL8AheAl+NS1 MSuNzl/CVbpJ3C/GSgAj5aZxb4BYDdS/crCjvbgojAavfFaqWYCIj6ULPJPYYFfu +qLPqIS7ctqXmnJ0tXA8okUrniGIUInZyhBsSee0IGp7tFfii5A= =H+1i -----END PGP SIGNATURE----- Merge 4.14.60 into android-4.14-p Changes in 4.14.60 fork: unconditionally clear stack on fork i2c: core: decrease reference count of device node in i2c_unregister_device RDMA/core: Avoid that ib_drain_qp() triggers an out-of-bounds stack access drivers/infiniband/core/verbs.c: fix build with gcc-4.4.4 IB/srpt: Fix an out-of-bounds stack access in srpt_zerolength_write() drivers/infiniband/ulp/srpt/ib_srpt.c: fix build with gcc-4.4.4 spi: spi-s3c64xx: Fix system resume support Input: elan_i2c - add ACPI ID for lenovo ideapad 330 Input: i8042 - add Lenovo LaVie Z to the i8042 reset list Input: elan_i2c - add another ACPI ID for Lenovo Ideapad 330-15AST kvm, mm: account shadow page tables to kmemcg delayacct: fix crash in delayacct_blkio_end() after delayacct init failure tracing: Fix double free of event_trigger_data tracing: Fix possible double free in event_enable_trigger_func() kthread, tracing: Don't expose half-written comm when creating kthreads tracing/kprobes: Fix trace_probe flags on enable_trace_kprobe() failure tracing: Quiet gcc warning about maybe unused link variable arm64: fix vmemmap BUILD_BUG_ON() triggering on !vmemmap setups mlxsw: spectrum_switchdev: Fix port_vlan refcounting kcov: ensure irq code sees a valid area xen/netfront: raise max number of slots in xennet_get_responses() hv_netvsc: fix network namespace issues with VF support skip LAYOUTRETURN if layout is invalid ALSA: emu10k1: add error handling for snd_ctl_add ALSA: fm801: add error handling for snd_ctl_add NFSv4.1: Fix the client behaviour on NFS4ERR_SEQ_FALSE_RETRY nfsd: fix potential use-after-free in nfsd4_decode_getdeviceinfo vfio: platform: Fix reset module leak in error path vfio/mdev: Check globally for duplicate devices vfio/type1: Fix task tracking for QEMU vCPU hotplug kernel/hung_task.c: show all hung tasks before panic mm: /proc/pid/pagemap: hide swap entries from unprivileged users mm: vmalloc: avoid racy handling of debugobjects in vunmap mm/slub.c: add __printf verification to slab_err() rtc: ensure rtc_set_alarm fails when alarms are not supported perf tools: Fix pmu events parsing rule netfilter: ipset: forbid family for hash:mac sets netfilter: ipset: List timing out entries with "timeout 1" instead of zero irqchip/ls-scfg-msi: Map MSIs in the iommu watchdog: da9063: Fix updating timeout value printk: drop in_nmi check from printk_safe_flush_on_panic() bpf, arm32: fix inconsistent naming about emit_a32_lsr_{r64,i64} ceph: fix alignment of rasize e1000e: Ignore TSYNCRXCTL when getting I219 clock attributes infiniband: fix a possible use-after-free bug powerpc/lib: Adjust .balign inside string functions for PPC32 powerpc/64s: Add barrier_nospec powerpc/eeh: Fix use-after-release of EEH driver hvc_opal: don't set tb_ticks_per_usec in udbg_init_opal_common() powerpc/64s: Fix compiler store ordering to SLB shadow area RDMA/mad: Convert BUG_ONs to error flows lightnvm: pblk: warn in case of corrupted write buffer netfilter: nf_tables: check msg_type before nft_trans_set(trans) pnfs: Don't release the sequence slot until we've processed layoutget on open disable loading f2fs module on PAGE_SIZE > 4KB f2fs: fix error path of move_data_page f2fs: fix to don't trigger writeback during recovery f2fs: fix to wait page writeback during revoking atomic write f2fs: Fix deadlock in shutdown ioctl f2fs: fix to detect failure of dquot_initialize f2fs: fix race in between GC and atomic open block, bfq: remove wrong lock in bfq_requests_merged usbip: usbip_detach: Fix memory, udev context and udev leak usbip: dynamically allocate idev by nports found in sysfs perf/x86/intel/uncore: Correct fixed counter index check in generic code perf/x86/intel/uncore: Correct fixed counter index check for NHM selftests/intel_pstate: Improve test, minor fixes selftests: memfd: return Kselftest Skip code for skipped tests selftests: intel_pstate: return Kselftest Skip code for skipped tests PCI: Fix devm_pci_alloc_host_bridge() memory leak btrfs: balance dirty metadata pages in btrfs_finish_ordered_io iwlwifi: pcie: fix race in Rx buffer allocator Bluetooth: hci_qca: Fix "Sleep inside atomic section" warning Bluetooth: btusb: Add a new Realtek 8723DE ID 2ff8:b011 ASoC: dpcm: fix BE dai not hw_free and shutdown mfd: cros_ec: Fail early if we cannot identify the EC mwifiex: handle race during mwifiex_usb_disconnect wlcore: sdio: check for valid platform device data before suspend net: hns3: Fixes the init of the VALID BD info in the descriptor media: tw686x: Fix incorrect vb2_mem_ops GFP flags media: videobuf2-core: don't call memop 'finish' when queueing Btrfs: don't return ino to ino cache if inode item removal fails Btrfs: don't BUG_ON() in btrfs_truncate_inode_items() btrfs: add barriers to btrfs_sync_log before log_commit_wait wakeups btrfs: qgroup: Finish rescan when hit the last leaf of extent tree x86/microcode: Make the late update update_lock a raw lock for RT PM / wakeup: Make s2idle_lock a RAW_SPINLOCK PCI: Prevent sysfs disable of device while driver is attached nvme-rdma: stop admin queue before freeing it nvme-pci: Fix AER reset handling ath: Add regulatory mapping for FCC3_ETSIC ath: Add regulatory mapping for ETSI8_WORLD ath: Add regulatory mapping for APL13_WORLD ath: Add regulatory mapping for APL2_FCCA ath: Add regulatory mapping for Uganda ath: Add regulatory mapping for Tanzania ath: Add regulatory mapping for Serbia ath: Add regulatory mapping for Bermuda ath: Add regulatory mapping for Bahamas powerpc/32: Add a missing include header powerpc/chrp/time: Make some functions static, add missing header include powerpc/powermac: Add missing prototype for note_bootable_part() powerpc/powermac: Mark variable x as unused powerpc: Add __printf verification to prom_printf spi: sh-msiof: Fix setting SIRMDR1.SYNCAC to match SITMDR1.SYNCAC powerpc/8xx: fix invalid register expression in head_8xx.S pinctrl: at91-pio4: add missing of_node_put bpf: powerpc64: pad function address loads with NOPs PCI: pciehp: Request control of native hotplug only if supported net: dsa: qca8k: Add support for QCA8334 switch mwifiex: correct histogram data with appropriate index ima: based on policy verify firmware signatures (pre-allocated buffer) drivers/perf: arm-ccn: don't log to dmesg in event_init spi: Add missing pm_runtime_put_noidle() after failed get net: hns3: Fix the missing client list node initialization fscrypt: use unbound workqueue for decryption scsi: ufs: ufshcd: fix possible unclocked register access scsi: ufs: fix exception event handling scsi: zfcp: assert that the ERP lock is held when tracing a recovery trigger drm/nouveau/fifo/gk104-: poll for runlist update completion Bluetooth: btusb: add ID for LiteOn 04ca:301a rtc: tps6586x: fix possible race condition rtc: vr41xx: fix possible race condition rtc: tps65910: fix possible race condition ALSA: emu10k1: Rate-limit error messages about page errors regulator: pfuze100: add .is_enable() for pfuze100_swb_regulator_ops md/raid1: add error handling of read error from FailFast device md: fix NULL dereference of mddev->pers in remove_and_add_spares() ixgbevf: fix MAC address changes through ixgbevf_set_mac() media: smiapp: fix timeout checking in smiapp_read_nvm net: ethernet: ti: cpsw-phy-sel: check bus_find_device() ret value ALSA: usb-audio: Apply rate limit to warning messages in URB complete callback media: atomisp: ov2680: don't declare unused vars arm64: cmpwait: Clear event register before arming exclusive monitor HID: hid-plantronics: Re-resend Update to map button for PTT products arm64: dts: renesas: salvator-common: use audio-graph-card for Sound drm/radeon: fix mode_valid's return type drm/amdgpu: Remove VRAM from shared bo domains. powerpc/embedded6xx/hlwd-pic: Prevent interrupts from being handled by Starlet HID: i2c-hid: check if device is there before really probing EDAC, altera: Fix ARM64 build warning ARM: dts: stih407-pinctrl: Fix complain about IRQ_TYPE_NONE usage ARM: dts: emev2: Add missing interrupt-affinity to PMU node ARM: dts: sh73a0: Add missing interrupt-affinity to PMU node nvmem: properly handle returned value nvmem_reg_read i40e: free the skb after clearing the bitlock tty: Fix data race in tty_insert_flip_string_fixed_flag dma-iommu: Fix compilation when !CONFIG_IOMMU_DMA net: phy: phylink: Release link GPIO media: rcar_jpu: Add missing clk_disable_unprepare() on error in jpu_open() libata: Fix command retry decision ACPI / LPSS: Only call pwm_add_table() for Bay Trail PWM if PMIC HRV is 2 media: media-device: fix ioctl function types media: saa7164: Fix driver name in debug output mtd: rawnand: fsl_ifc: fix FSL NAND driver to read all ONFI parameter pages brcmfmac: Add support for bcm43364 wireless chipset s390/cpum_sf: Add data entry sizes to sampling trailer entry perf: fix invalid bit in diagnostic entry bnxt_en: Check unsupported speeds in bnxt_update_link() on PF only. scsi: 3w-9xxx: fix a missing-check bug scsi: 3w-xxxx: fix a missing-check bug scsi: megaraid: silence a static checker bug scsi: hisi_sas: config ATA de-reset as an constrained command for v3 hw scsi: qedf: Set the UNLOADING flag when removing a vport staging: lustre: o2iblnd: fix race at kiblnd_connect_peer staging: lustre: o2iblnd: Fix FastReg map/unmap for MLX5 thermal: exynos: fix setting rising_threshold for Exynos5433 bpf: fix references to free_bpf_prog_info() in comments f2fs: avoid fsync() failure caused by EAGAIN in writepage() media: siano: get rid of __le32/__le16 cast warnings drm/atomic: Handling the case when setting old crtc for plane ALSA: hda/ca0132: fix build failure when a local macro is defined mmc: dw_mmc: update actual clock for mmc debugfs mmc: pwrseq: Use kmalloc_array instead of stack VLA dt-bindings: pinctrl: meson: add support for the Meson8m2 SoC spi: meson-spicc: Fix error handling in meson_spicc_probe() net: hns3: Fixes the out of bounds access in hclge_map_tqp dt-bindings: net: meson-dwmac: new compatible name for AXG SoC backlight: pwm_bl: Don't use GPIOF_* with gpiod_get_direction stop_machine: Use raw spinlocks delayacct: Use raw_spinlocks memory: tegra: Do not handle spurious interrupts memory: tegra: Apply interrupts mask per SoC nvme: lightnvm: add granby support arm64: defconfig: Enable Rockchip io-domain driver igb: Fix queue selection on MAC filters on i210 drm/gma500: fix psb_intel_lvds_mode_valid()'s return type ipconfig: Correctly initialise ic_nameservers rsi: Fix 'invalid vdd' warning in mmc rsi: fix nommu_map_sg overflow kernel panic audit: allow not equal op for audit by executable staging: vchiq_core: Fix missing semaphore release in error case staging: lustre: llite: correct removexattr detection staging: lustre: ldlm: free resource when ldlm_lock_create() fails. serial: core: Make sure compiler barfs for 16-byte earlycon names soc: imx: gpcv2: Do not pass static memory as platform data microblaze: Fix simpleImage format generation usb: hub: Don't wait for connect state at resume for powered-off ports crypto: authencesn - don't leak pointers to authenc keys crypto: authenc - don't leak pointers to authenc keys media: omap3isp: fix unbalanced dma_iommu_mapping regulator: Don't return or expect -errno from of_map_mode() scsi: scsi_dh: replace too broad "TP9" string with the exact models scsi: megaraid_sas: Increase timeout by 1 sec for non-RAID fastpath IOs scsi: cxlflash: Synchronize reset and remove ops scsi: cxlflash: Avoid clobbering context control register value media: atomisp: compat32: fix __user annotations media: si470x: fix __be16 annotations ASoC: topology: Fix bclk and fsync inversion in set_link_hw_format() ASoC: topology: Add missing clock gating parameter when parsing hw_configs drm: Add DP PSR2 sink enable bit drm/atomic-helper: Drop plane->fb references only for drm_atomic_helper_shutdown() drm/dp/mst: Fix off-by-one typo when dump payload table block: bio_iov_iter_get_pages: fix size of last iovec blkdev: __blkdev_direct_IO_simple: fix leak in error case block: reset bi_iter.bi_done after splitting bio random: mix rdrand with entropy sent in from userspace squashfs: be more careful about metadata corruption ext4: fix inline data updates with checksums enabled ext4: check for allocation block validity with block group locked ext4: fix check to prevent initializing reserved inodes PCI: pciehp: Assume NoCompl+ for Thunderbolt ports PCI: xgene: Remove leftover pci_scan_child_bus() call ovl: Sync upper dirty data when syncing overlayfs usb: gadget: udc: renesas_usb3: should remove debugfs RDMA/uverbs: Protect from attempts to create flows on unsupported QP net: dsa: qca8k: Force CPU port to its highest bandwidth net: dsa: qca8k: Enable RXMAC when bringing up a port net: dsa: qca8k: Add QCA8334 binding documentation net: dsa: qca8k: Allow overwriting CPU port setting ipv4: remove BUG_ON() from fib_compute_spec_dst net: ena: Fix use of uninitialized DMA address bits field net: fix amd-xgbe flow-control issue net: lan78xx: fix rx handling before first packet is send net: mdio-mux: bcm-iproc: fix wrong getter and setter pair NET: stmmac: align DMA stuff to largest cache line length tcp_bbr: fix bw probing to raise in-flight data for very small BDPs xen-netfront: wait xenbus state change when load module manually netlink: Do not subscribe to non-existent groups netlink: Don't shift with UB on nlk->ngroups tcp: do not force quickack when receiving out-of-order packets tcp: add max_quickacks param to tcp_incr_quickack and tcp_enter_quickack_mode tcp: do not aggressively quick ack after ECN events tcp: refactor tcp_ecn_check_ce to remove sk type cast tcp: add one more quick ack after after ECN events Linux 4.14.60 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2016 lines
49 KiB
C
2016 lines
49 KiB
C
/*
|
|
* Copyright (c) 2012 Taobao.
|
|
* Written by Tao Ma <boyu.mt@taobao.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of version 2.1 of the GNU Lesser General Public License
|
|
* as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
#include <linux/fiemap.h>
|
|
|
|
#include "ext4_jbd2.h"
|
|
#include "ext4.h"
|
|
#include "xattr.h"
|
|
#include "truncate.h"
|
|
#include <trace/events/android_fs.h>
|
|
|
|
#define EXT4_XATTR_SYSTEM_DATA "data"
|
|
#define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
|
|
#define EXT4_INLINE_DOTDOT_OFFSET 2
|
|
#define EXT4_INLINE_DOTDOT_SIZE 4
|
|
|
|
static int ext4_get_inline_size(struct inode *inode)
|
|
{
|
|
if (EXT4_I(inode)->i_inline_off)
|
|
return EXT4_I(inode)->i_inline_size;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int get_max_inline_xattr_value_size(struct inode *inode,
|
|
struct ext4_iloc *iloc)
|
|
{
|
|
struct ext4_xattr_ibody_header *header;
|
|
struct ext4_xattr_entry *entry;
|
|
struct ext4_inode *raw_inode;
|
|
int free, min_offs;
|
|
|
|
min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
|
|
EXT4_GOOD_OLD_INODE_SIZE -
|
|
EXT4_I(inode)->i_extra_isize -
|
|
sizeof(struct ext4_xattr_ibody_header);
|
|
|
|
/*
|
|
* We need to subtract another sizeof(__u32) since an in-inode xattr
|
|
* needs an empty 4 bytes to indicate the gap between the xattr entry
|
|
* and the name/value pair.
|
|
*/
|
|
if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
|
|
return EXT4_XATTR_SIZE(min_offs -
|
|
EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
|
|
EXT4_XATTR_ROUND - sizeof(__u32));
|
|
|
|
raw_inode = ext4_raw_inode(iloc);
|
|
header = IHDR(inode, raw_inode);
|
|
entry = IFIRST(header);
|
|
|
|
/* Compute min_offs. */
|
|
for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
|
|
if (!entry->e_value_inum && entry->e_value_size) {
|
|
size_t offs = le16_to_cpu(entry->e_value_offs);
|
|
if (offs < min_offs)
|
|
min_offs = offs;
|
|
}
|
|
}
|
|
free = min_offs -
|
|
((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
|
|
|
|
if (EXT4_I(inode)->i_inline_off) {
|
|
entry = (struct ext4_xattr_entry *)
|
|
((void *)raw_inode + EXT4_I(inode)->i_inline_off);
|
|
|
|
free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
|
|
goto out;
|
|
}
|
|
|
|
free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
|
|
|
|
if (free > EXT4_XATTR_ROUND)
|
|
free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
|
|
else
|
|
free = 0;
|
|
|
|
out:
|
|
return free;
|
|
}
|
|
|
|
/*
|
|
* Get the maximum size we now can store in an inode.
|
|
* If we can't find the space for a xattr entry, don't use the space
|
|
* of the extents since we have no space to indicate the inline data.
|
|
*/
|
|
int ext4_get_max_inline_size(struct inode *inode)
|
|
{
|
|
int error, max_inline_size;
|
|
struct ext4_iloc iloc;
|
|
|
|
if (EXT4_I(inode)->i_extra_isize == 0)
|
|
return 0;
|
|
|
|
error = ext4_get_inode_loc(inode, &iloc);
|
|
if (error) {
|
|
ext4_error_inode(inode, __func__, __LINE__, 0,
|
|
"can't get inode location %lu",
|
|
inode->i_ino);
|
|
return 0;
|
|
}
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
|
|
brelse(iloc.bh);
|
|
|
|
if (!max_inline_size)
|
|
return 0;
|
|
|
|
return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
|
|
}
|
|
|
|
/*
|
|
* this function does not take xattr_sem, which is OK because it is
|
|
* currently only used in a code path coming form ext4_iget, before
|
|
* the new inode has been unlocked
|
|
*/
|
|
int ext4_find_inline_data_nolock(struct inode *inode)
|
|
{
|
|
struct ext4_xattr_ibody_find is = {
|
|
.s = { .not_found = -ENODATA, },
|
|
};
|
|
struct ext4_xattr_info i = {
|
|
.name_index = EXT4_XATTR_INDEX_SYSTEM,
|
|
.name = EXT4_XATTR_SYSTEM_DATA,
|
|
};
|
|
int error;
|
|
|
|
if (EXT4_I(inode)->i_extra_isize == 0)
|
|
return 0;
|
|
|
|
error = ext4_get_inode_loc(inode, &is.iloc);
|
|
if (error)
|
|
return error;
|
|
|
|
error = ext4_xattr_ibody_find(inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
if (!is.s.not_found) {
|
|
if (is.s.here->e_value_inum) {
|
|
EXT4_ERROR_INODE(inode, "inline data xattr refers "
|
|
"to an external xattr inode");
|
|
error = -EFSCORRUPTED;
|
|
goto out;
|
|
}
|
|
EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
|
|
(void *)ext4_raw_inode(&is.iloc));
|
|
EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
|
|
le32_to_cpu(is.s.here->e_value_size);
|
|
ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
}
|
|
out:
|
|
brelse(is.iloc.bh);
|
|
return error;
|
|
}
|
|
|
|
static int ext4_read_inline_data(struct inode *inode, void *buffer,
|
|
unsigned int len,
|
|
struct ext4_iloc *iloc)
|
|
{
|
|
struct ext4_xattr_entry *entry;
|
|
struct ext4_xattr_ibody_header *header;
|
|
int cp_len = 0;
|
|
struct ext4_inode *raw_inode;
|
|
|
|
if (!len)
|
|
return 0;
|
|
|
|
BUG_ON(len > EXT4_I(inode)->i_inline_size);
|
|
|
|
cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
|
|
len : EXT4_MIN_INLINE_DATA_SIZE;
|
|
|
|
raw_inode = ext4_raw_inode(iloc);
|
|
memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
|
|
|
|
len -= cp_len;
|
|
buffer += cp_len;
|
|
|
|
if (!len)
|
|
goto out;
|
|
|
|
header = IHDR(inode, raw_inode);
|
|
entry = (struct ext4_xattr_entry *)((void *)raw_inode +
|
|
EXT4_I(inode)->i_inline_off);
|
|
len = min_t(unsigned int, len,
|
|
(unsigned int)le32_to_cpu(entry->e_value_size));
|
|
|
|
memcpy(buffer,
|
|
(void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
|
|
cp_len += len;
|
|
|
|
out:
|
|
return cp_len;
|
|
}
|
|
|
|
/*
|
|
* write the buffer to the inline inode.
|
|
* If 'create' is set, we don't need to do the extra copy in the xattr
|
|
* value since it is already handled by ext4_xattr_ibody_inline_set.
|
|
* That saves us one memcpy.
|
|
*/
|
|
static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
|
|
void *buffer, loff_t pos, unsigned int len)
|
|
{
|
|
struct ext4_xattr_entry *entry;
|
|
struct ext4_xattr_ibody_header *header;
|
|
struct ext4_inode *raw_inode;
|
|
int cp_len = 0;
|
|
|
|
if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
|
|
return;
|
|
|
|
BUG_ON(!EXT4_I(inode)->i_inline_off);
|
|
BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
|
|
|
|
raw_inode = ext4_raw_inode(iloc);
|
|
buffer += pos;
|
|
|
|
if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
|
|
cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
|
|
EXT4_MIN_INLINE_DATA_SIZE - pos : len;
|
|
memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
|
|
|
|
len -= cp_len;
|
|
buffer += cp_len;
|
|
pos += cp_len;
|
|
}
|
|
|
|
if (!len)
|
|
return;
|
|
|
|
pos -= EXT4_MIN_INLINE_DATA_SIZE;
|
|
header = IHDR(inode, raw_inode);
|
|
entry = (struct ext4_xattr_entry *)((void *)raw_inode +
|
|
EXT4_I(inode)->i_inline_off);
|
|
|
|
memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
|
|
buffer, len);
|
|
}
|
|
|
|
static int ext4_create_inline_data(handle_t *handle,
|
|
struct inode *inode, unsigned len)
|
|
{
|
|
int error;
|
|
void *value = NULL;
|
|
struct ext4_xattr_ibody_find is = {
|
|
.s = { .not_found = -ENODATA, },
|
|
};
|
|
struct ext4_xattr_info i = {
|
|
.name_index = EXT4_XATTR_INDEX_SYSTEM,
|
|
.name = EXT4_XATTR_SYSTEM_DATA,
|
|
};
|
|
|
|
error = ext4_get_inode_loc(inode, &is.iloc);
|
|
if (error)
|
|
return error;
|
|
|
|
BUFFER_TRACE(is.iloc.bh, "get_write_access");
|
|
error = ext4_journal_get_write_access(handle, is.iloc.bh);
|
|
if (error)
|
|
goto out;
|
|
|
|
if (len > EXT4_MIN_INLINE_DATA_SIZE) {
|
|
value = EXT4_ZERO_XATTR_VALUE;
|
|
len -= EXT4_MIN_INLINE_DATA_SIZE;
|
|
} else {
|
|
value = "";
|
|
len = 0;
|
|
}
|
|
|
|
/* Insert the the xttr entry. */
|
|
i.value = value;
|
|
i.value_len = len;
|
|
|
|
error = ext4_xattr_ibody_find(inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
BUG_ON(!is.s.not_found);
|
|
|
|
error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
|
|
if (error) {
|
|
if (error == -ENOSPC)
|
|
ext4_clear_inode_state(inode,
|
|
EXT4_STATE_MAY_INLINE_DATA);
|
|
goto out;
|
|
}
|
|
|
|
memset((void *)ext4_raw_inode(&is.iloc)->i_block,
|
|
0, EXT4_MIN_INLINE_DATA_SIZE);
|
|
|
|
EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
|
|
(void *)ext4_raw_inode(&is.iloc));
|
|
EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
|
|
ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
|
|
ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
|
|
get_bh(is.iloc.bh);
|
|
error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
|
|
|
|
out:
|
|
brelse(is.iloc.bh);
|
|
return error;
|
|
}
|
|
|
|
static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
|
|
unsigned int len)
|
|
{
|
|
int error;
|
|
void *value = NULL;
|
|
struct ext4_xattr_ibody_find is = {
|
|
.s = { .not_found = -ENODATA, },
|
|
};
|
|
struct ext4_xattr_info i = {
|
|
.name_index = EXT4_XATTR_INDEX_SYSTEM,
|
|
.name = EXT4_XATTR_SYSTEM_DATA,
|
|
};
|
|
|
|
/* If the old space is ok, write the data directly. */
|
|
if (len <= EXT4_I(inode)->i_inline_size)
|
|
return 0;
|
|
|
|
error = ext4_get_inode_loc(inode, &is.iloc);
|
|
if (error)
|
|
return error;
|
|
|
|
error = ext4_xattr_ibody_find(inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
BUG_ON(is.s.not_found);
|
|
|
|
len -= EXT4_MIN_INLINE_DATA_SIZE;
|
|
value = kzalloc(len, GFP_NOFS);
|
|
if (!value) {
|
|
error = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
|
|
value, len);
|
|
if (error == -ENODATA)
|
|
goto out;
|
|
|
|
BUFFER_TRACE(is.iloc.bh, "get_write_access");
|
|
error = ext4_journal_get_write_access(handle, is.iloc.bh);
|
|
if (error)
|
|
goto out;
|
|
|
|
/* Update the xttr entry. */
|
|
i.value = value;
|
|
i.value_len = len;
|
|
|
|
error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
|
|
(void *)ext4_raw_inode(&is.iloc));
|
|
EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
|
|
le32_to_cpu(is.s.here->e_value_size);
|
|
ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
get_bh(is.iloc.bh);
|
|
error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
|
|
|
|
out:
|
|
kfree(value);
|
|
brelse(is.iloc.bh);
|
|
return error;
|
|
}
|
|
|
|
static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
|
|
unsigned int len)
|
|
{
|
|
int ret, size, no_expand;
|
|
struct ext4_inode_info *ei = EXT4_I(inode);
|
|
|
|
if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
|
|
return -ENOSPC;
|
|
|
|
size = ext4_get_max_inline_size(inode);
|
|
if (size < len)
|
|
return -ENOSPC;
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
|
|
if (ei->i_inline_off)
|
|
ret = ext4_update_inline_data(handle, inode, len);
|
|
else
|
|
ret = ext4_create_inline_data(handle, inode, len);
|
|
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
return ret;
|
|
}
|
|
|
|
static int ext4_destroy_inline_data_nolock(handle_t *handle,
|
|
struct inode *inode)
|
|
{
|
|
struct ext4_inode_info *ei = EXT4_I(inode);
|
|
struct ext4_xattr_ibody_find is = {
|
|
.s = { .not_found = 0, },
|
|
};
|
|
struct ext4_xattr_info i = {
|
|
.name_index = EXT4_XATTR_INDEX_SYSTEM,
|
|
.name = EXT4_XATTR_SYSTEM_DATA,
|
|
.value = NULL,
|
|
.value_len = 0,
|
|
};
|
|
int error;
|
|
|
|
if (!ei->i_inline_off)
|
|
return 0;
|
|
|
|
error = ext4_get_inode_loc(inode, &is.iloc);
|
|
if (error)
|
|
return error;
|
|
|
|
error = ext4_xattr_ibody_find(inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
BUFFER_TRACE(is.iloc.bh, "get_write_access");
|
|
error = ext4_journal_get_write_access(handle, is.iloc.bh);
|
|
if (error)
|
|
goto out;
|
|
|
|
error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
|
|
if (error)
|
|
goto out;
|
|
|
|
memset((void *)ext4_raw_inode(&is.iloc)->i_block,
|
|
0, EXT4_MIN_INLINE_DATA_SIZE);
|
|
memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
|
|
|
|
if (ext4_has_feature_extents(inode->i_sb)) {
|
|
if (S_ISDIR(inode->i_mode) ||
|
|
S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
|
|
ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
|
|
ext4_ext_tree_init(handle, inode);
|
|
}
|
|
}
|
|
ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
|
|
|
|
get_bh(is.iloc.bh);
|
|
error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
|
|
|
|
EXT4_I(inode)->i_inline_off = 0;
|
|
EXT4_I(inode)->i_inline_size = 0;
|
|
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
out:
|
|
brelse(is.iloc.bh);
|
|
if (error == -ENODATA)
|
|
error = 0;
|
|
return error;
|
|
}
|
|
|
|
static int ext4_read_inline_page(struct inode *inode, struct page *page)
|
|
{
|
|
void *kaddr;
|
|
int ret = 0;
|
|
size_t len;
|
|
struct ext4_iloc iloc;
|
|
|
|
BUG_ON(!PageLocked(page));
|
|
BUG_ON(!ext4_has_inline_data(inode));
|
|
BUG_ON(page->index);
|
|
|
|
if (!EXT4_I(inode)->i_inline_off) {
|
|
ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
|
|
inode->i_ino);
|
|
goto out;
|
|
}
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
goto out;
|
|
|
|
len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
|
|
kaddr = kmap_atomic(page);
|
|
ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
|
|
flush_dcache_page(page);
|
|
kunmap_atomic(kaddr);
|
|
zero_user_segment(page, len, PAGE_SIZE);
|
|
SetPageUptodate(page);
|
|
brelse(iloc.bh);
|
|
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
int ext4_readpage_inline(struct inode *inode, struct page *page)
|
|
{
|
|
int ret = 0;
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
return -EAGAIN;
|
|
}
|
|
|
|
if (trace_android_fs_dataread_start_enabled()) {
|
|
char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
|
|
|
|
path = android_fstrace_get_pathname(pathbuf,
|
|
MAX_TRACE_PATHBUF_LEN,
|
|
inode);
|
|
trace_android_fs_dataread_start(inode, page_offset(page),
|
|
PAGE_SIZE, current->pid,
|
|
path, current->comm);
|
|
}
|
|
|
|
/*
|
|
* Current inline data can only exist in the 1st page,
|
|
* So for all the other pages, just set them uptodate.
|
|
*/
|
|
if (!page->index)
|
|
ret = ext4_read_inline_page(inode, page);
|
|
else if (!PageUptodate(page)) {
|
|
zero_user_segment(page, 0, PAGE_SIZE);
|
|
SetPageUptodate(page);
|
|
}
|
|
|
|
trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE);
|
|
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
|
|
unlock_page(page);
|
|
return ret >= 0 ? 0 : ret;
|
|
}
|
|
|
|
static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
|
|
struct inode *inode,
|
|
unsigned flags)
|
|
{
|
|
int ret, needed_blocks, no_expand;
|
|
handle_t *handle = NULL;
|
|
int retries = 0, sem_held = 0;
|
|
struct page *page = NULL;
|
|
unsigned from, to;
|
|
struct ext4_iloc iloc;
|
|
|
|
if (!ext4_has_inline_data(inode)) {
|
|
/*
|
|
* clear the flag so that no new write
|
|
* will trap here again.
|
|
*/
|
|
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
return 0;
|
|
}
|
|
|
|
needed_blocks = ext4_writepage_trans_blocks(inode);
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
retry:
|
|
handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
|
|
if (IS_ERR(handle)) {
|
|
ret = PTR_ERR(handle);
|
|
handle = NULL;
|
|
goto out;
|
|
}
|
|
|
|
/* We cannot recurse into the filesystem as the transaction is already
|
|
* started */
|
|
flags |= AOP_FLAG_NOFS;
|
|
|
|
page = grab_cache_page_write_begin(mapping, 0, flags);
|
|
if (!page) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
sem_held = 1;
|
|
/* If some one has already done this for us, just exit. */
|
|
if (!ext4_has_inline_data(inode)) {
|
|
ret = 0;
|
|
goto out;
|
|
}
|
|
|
|
from = 0;
|
|
to = ext4_get_inline_size(inode);
|
|
if (!PageUptodate(page)) {
|
|
ret = ext4_read_inline_page(inode, page);
|
|
if (ret < 0)
|
|
goto out;
|
|
}
|
|
|
|
ret = ext4_destroy_inline_data_nolock(handle, inode);
|
|
if (ret)
|
|
goto out;
|
|
|
|
if (ext4_should_dioread_nolock(inode)) {
|
|
ret = __block_write_begin(page, from, to,
|
|
ext4_get_block_unwritten);
|
|
} else
|
|
ret = __block_write_begin(page, from, to, ext4_get_block);
|
|
|
|
if (!ret && ext4_should_journal_data(inode)) {
|
|
ret = ext4_walk_page_buffers(handle, page_buffers(page),
|
|
from, to, NULL,
|
|
do_journal_get_write_access);
|
|
}
|
|
|
|
if (ret) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
page = NULL;
|
|
ext4_orphan_add(handle, inode);
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
sem_held = 0;
|
|
ext4_journal_stop(handle);
|
|
handle = NULL;
|
|
ext4_truncate_failed_write(inode);
|
|
/*
|
|
* If truncate failed early the inode might
|
|
* still be on the orphan list; we need to
|
|
* make sure the inode is removed from the
|
|
* orphan list in that case.
|
|
*/
|
|
if (inode->i_nlink)
|
|
ext4_orphan_del(NULL, inode);
|
|
}
|
|
|
|
if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
|
|
goto retry;
|
|
|
|
if (page)
|
|
block_commit_write(page, from, to);
|
|
out:
|
|
if (page) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
}
|
|
if (sem_held)
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
if (handle)
|
|
ext4_journal_stop(handle);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Try to write data in the inode.
|
|
* If the inode has inline data, check whether the new write can be
|
|
* in the inode also. If not, create the page the handle, move the data
|
|
* to the page make it update and let the later codes create extent for it.
|
|
*/
|
|
int ext4_try_to_write_inline_data(struct address_space *mapping,
|
|
struct inode *inode,
|
|
loff_t pos, unsigned len,
|
|
unsigned flags,
|
|
struct page **pagep)
|
|
{
|
|
int ret;
|
|
handle_t *handle;
|
|
struct page *page;
|
|
struct ext4_iloc iloc;
|
|
|
|
if (pos + len > ext4_get_max_inline_size(inode))
|
|
goto convert;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
/*
|
|
* The possible write could happen in the inode,
|
|
* so try to reserve the space in inode first.
|
|
*/
|
|
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
|
|
if (IS_ERR(handle)) {
|
|
ret = PTR_ERR(handle);
|
|
handle = NULL;
|
|
goto out;
|
|
}
|
|
|
|
ret = ext4_prepare_inline_data(handle, inode, pos + len);
|
|
if (ret && ret != -ENOSPC)
|
|
goto out;
|
|
|
|
/* We don't have space in inline inode, so convert it to extent. */
|
|
if (ret == -ENOSPC) {
|
|
ext4_journal_stop(handle);
|
|
brelse(iloc.bh);
|
|
goto convert;
|
|
}
|
|
|
|
ret = ext4_journal_get_write_access(handle, iloc.bh);
|
|
if (ret)
|
|
goto out;
|
|
|
|
flags |= AOP_FLAG_NOFS;
|
|
|
|
page = grab_cache_page_write_begin(mapping, 0, flags);
|
|
if (!page) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
*pagep = page;
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
ret = 0;
|
|
unlock_page(page);
|
|
put_page(page);
|
|
goto out_up_read;
|
|
}
|
|
|
|
if (!PageUptodate(page)) {
|
|
ret = ext4_read_inline_page(inode, page);
|
|
if (ret < 0)
|
|
goto out_up_read;
|
|
}
|
|
|
|
ret = 1;
|
|
handle = NULL;
|
|
out_up_read:
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
out:
|
|
if (handle && (ret != 1))
|
|
ext4_journal_stop(handle);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
convert:
|
|
return ext4_convert_inline_data_to_extent(mapping,
|
|
inode, flags);
|
|
}
|
|
|
|
int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
|
|
unsigned copied, struct page *page)
|
|
{
|
|
int ret, no_expand;
|
|
void *kaddr;
|
|
struct ext4_iloc iloc;
|
|
|
|
if (unlikely(copied < len)) {
|
|
if (!PageUptodate(page)) {
|
|
copied = 0;
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret) {
|
|
ext4_std_error(inode->i_sb, ret);
|
|
copied = 0;
|
|
goto out;
|
|
}
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
BUG_ON(!ext4_has_inline_data(inode));
|
|
|
|
kaddr = kmap_atomic(page);
|
|
ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
|
|
kunmap_atomic(kaddr);
|
|
SetPageUptodate(page);
|
|
/* clear page dirty so that writepages wouldn't work for us. */
|
|
ClearPageDirty(page);
|
|
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
brelse(iloc.bh);
|
|
mark_inode_dirty(inode);
|
|
out:
|
|
return copied;
|
|
}
|
|
|
|
struct buffer_head *
|
|
ext4_journalled_write_inline_data(struct inode *inode,
|
|
unsigned len,
|
|
struct page *page)
|
|
{
|
|
int ret, no_expand;
|
|
void *kaddr;
|
|
struct ext4_iloc iloc;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret) {
|
|
ext4_std_error(inode->i_sb, ret);
|
|
return NULL;
|
|
}
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
kaddr = kmap_atomic(page);
|
|
ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
|
|
kunmap_atomic(kaddr);
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
|
|
return iloc.bh;
|
|
}
|
|
|
|
/*
|
|
* Try to make the page cache and handle ready for the inline data case.
|
|
* We can call this function in 2 cases:
|
|
* 1. The inode is created and the first write exceeds inline size. We can
|
|
* clear the inode state safely.
|
|
* 2. The inode has inline data, then we need to read the data, make it
|
|
* update and dirty so that ext4_da_writepages can handle it. We don't
|
|
* need to start the journal since the file's metatdata isn't changed now.
|
|
*/
|
|
static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
|
|
struct inode *inode,
|
|
unsigned flags,
|
|
void **fsdata)
|
|
{
|
|
int ret = 0, inline_size;
|
|
struct page *page;
|
|
|
|
page = grab_cache_page_write_begin(mapping, 0, flags);
|
|
if (!page)
|
|
return -ENOMEM;
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
goto out;
|
|
}
|
|
|
|
inline_size = ext4_get_inline_size(inode);
|
|
|
|
if (!PageUptodate(page)) {
|
|
ret = ext4_read_inline_page(inode, page);
|
|
if (ret < 0)
|
|
goto out;
|
|
}
|
|
|
|
ret = __block_write_begin(page, 0, inline_size,
|
|
ext4_da_get_block_prep);
|
|
if (ret) {
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
unlock_page(page);
|
|
put_page(page);
|
|
ext4_truncate_failed_write(inode);
|
|
return ret;
|
|
}
|
|
|
|
SetPageDirty(page);
|
|
SetPageUptodate(page);
|
|
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
*fsdata = (void *)CONVERT_INLINE_DATA;
|
|
|
|
out:
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
if (page) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Prepare the write for the inline data.
|
|
* If the the data can be written into the inode, we just read
|
|
* the page and make it uptodate, and start the journal.
|
|
* Otherwise read the page, makes it dirty so that it can be
|
|
* handle in writepages(the i_disksize update is left to the
|
|
* normal ext4_da_write_end).
|
|
*/
|
|
int ext4_da_write_inline_data_begin(struct address_space *mapping,
|
|
struct inode *inode,
|
|
loff_t pos, unsigned len,
|
|
unsigned flags,
|
|
struct page **pagep,
|
|
void **fsdata)
|
|
{
|
|
int ret, inline_size;
|
|
handle_t *handle;
|
|
struct page *page;
|
|
struct ext4_iloc iloc;
|
|
int retries;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
retry_journal:
|
|
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
|
|
if (IS_ERR(handle)) {
|
|
ret = PTR_ERR(handle);
|
|
goto out;
|
|
}
|
|
|
|
inline_size = ext4_get_max_inline_size(inode);
|
|
|
|
ret = -ENOSPC;
|
|
if (inline_size >= pos + len) {
|
|
ret = ext4_prepare_inline_data(handle, inode, pos + len);
|
|
if (ret && ret != -ENOSPC)
|
|
goto out_journal;
|
|
}
|
|
|
|
/*
|
|
* We cannot recurse into the filesystem as the transaction
|
|
* is already started.
|
|
*/
|
|
flags |= AOP_FLAG_NOFS;
|
|
|
|
if (ret == -ENOSPC) {
|
|
ext4_journal_stop(handle);
|
|
ret = ext4_da_convert_inline_data_to_extent(mapping,
|
|
inode,
|
|
flags,
|
|
fsdata);
|
|
if (ret == -ENOSPC &&
|
|
ext4_should_retry_alloc(inode->i_sb, &retries))
|
|
goto retry_journal;
|
|
goto out;
|
|
}
|
|
|
|
page = grab_cache_page_write_begin(mapping, 0, flags);
|
|
if (!page) {
|
|
ret = -ENOMEM;
|
|
goto out_journal;
|
|
}
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
ret = 0;
|
|
goto out_release_page;
|
|
}
|
|
|
|
if (!PageUptodate(page)) {
|
|
ret = ext4_read_inline_page(inode, page);
|
|
if (ret < 0)
|
|
goto out_release_page;
|
|
}
|
|
ret = ext4_journal_get_write_access(handle, iloc.bh);
|
|
if (ret)
|
|
goto out_release_page;
|
|
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
*pagep = page;
|
|
brelse(iloc.bh);
|
|
return 1;
|
|
out_release_page:
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
unlock_page(page);
|
|
put_page(page);
|
|
out_journal:
|
|
ext4_journal_stop(handle);
|
|
out:
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
|
|
unsigned len, unsigned copied,
|
|
struct page *page)
|
|
{
|
|
int ret;
|
|
|
|
ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
|
|
if (ret < 0) {
|
|
unlock_page(page);
|
|
put_page(page);
|
|
return ret;
|
|
}
|
|
copied = ret;
|
|
|
|
/*
|
|
* No need to use i_size_read() here, the i_size
|
|
* cannot change under us because we hold i_mutex.
|
|
*
|
|
* But it's important to update i_size while still holding page lock:
|
|
* page writeout could otherwise come in and zero beyond i_size.
|
|
*/
|
|
if (pos+copied > inode->i_size)
|
|
i_size_write(inode, pos+copied);
|
|
unlock_page(page);
|
|
put_page(page);
|
|
|
|
/*
|
|
* Don't mark the inode dirty under page lock. First, it unnecessarily
|
|
* makes the holding time of page lock longer. Second, it forces lock
|
|
* ordering of page lock and transaction start for journaling
|
|
* filesystems.
|
|
*/
|
|
mark_inode_dirty(inode);
|
|
|
|
return copied;
|
|
}
|
|
|
|
#ifdef INLINE_DIR_DEBUG
|
|
void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
|
|
void *inline_start, int inline_size)
|
|
{
|
|
int offset;
|
|
unsigned short de_len;
|
|
struct ext4_dir_entry_2 *de = inline_start;
|
|
void *dlimit = inline_start + inline_size;
|
|
|
|
trace_printk("inode %lu\n", dir->i_ino);
|
|
offset = 0;
|
|
while ((void *)de < dlimit) {
|
|
de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
|
|
trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
|
|
offset, de_len, de->name_len, de->name,
|
|
de->name_len, le32_to_cpu(de->inode));
|
|
if (ext4_check_dir_entry(dir, NULL, de, bh,
|
|
inline_start, inline_size, offset))
|
|
BUG();
|
|
|
|
offset += de_len;
|
|
de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
|
|
}
|
|
}
|
|
#else
|
|
#define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
|
|
#endif
|
|
|
|
/*
|
|
* Add a new entry into a inline dir.
|
|
* It will return -ENOSPC if no space is available, and -EIO
|
|
* and -EEXIST if directory entry already exists.
|
|
*/
|
|
static int ext4_add_dirent_to_inline(handle_t *handle,
|
|
struct ext4_filename *fname,
|
|
struct inode *dir,
|
|
struct inode *inode,
|
|
struct ext4_iloc *iloc,
|
|
void *inline_start, int inline_size)
|
|
{
|
|
int err;
|
|
struct ext4_dir_entry_2 *de;
|
|
|
|
err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
|
|
inline_size, fname, &de);
|
|
if (err)
|
|
return err;
|
|
|
|
BUFFER_TRACE(iloc->bh, "get_write_access");
|
|
err = ext4_journal_get_write_access(handle, iloc->bh);
|
|
if (err)
|
|
return err;
|
|
ext4_insert_dentry(inode, de, inline_size, fname);
|
|
|
|
ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
|
|
|
|
/*
|
|
* XXX shouldn't update any times until successful
|
|
* completion of syscall, but too many callers depend
|
|
* on this.
|
|
*
|
|
* XXX similarly, too many callers depend on
|
|
* ext4_new_inode() setting the times, but error
|
|
* recovery deletes the inode, so the worst that can
|
|
* happen is that the times are slightly out of date
|
|
* and/or different from the directory change time.
|
|
*/
|
|
dir->i_mtime = dir->i_ctime = current_time(dir);
|
|
ext4_update_dx_flag(dir);
|
|
dir->i_version++;
|
|
return 1;
|
|
}
|
|
|
|
static void *ext4_get_inline_xattr_pos(struct inode *inode,
|
|
struct ext4_iloc *iloc)
|
|
{
|
|
struct ext4_xattr_entry *entry;
|
|
struct ext4_xattr_ibody_header *header;
|
|
|
|
BUG_ON(!EXT4_I(inode)->i_inline_off);
|
|
|
|
header = IHDR(inode, ext4_raw_inode(iloc));
|
|
entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
|
|
EXT4_I(inode)->i_inline_off);
|
|
|
|
return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
|
|
}
|
|
|
|
/* Set the final de to cover the whole block. */
|
|
static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
|
|
{
|
|
struct ext4_dir_entry_2 *de, *prev_de;
|
|
void *limit;
|
|
int de_len;
|
|
|
|
de = (struct ext4_dir_entry_2 *)de_buf;
|
|
if (old_size) {
|
|
limit = de_buf + old_size;
|
|
do {
|
|
prev_de = de;
|
|
de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
|
|
de_buf += de_len;
|
|
de = (struct ext4_dir_entry_2 *)de_buf;
|
|
} while (de_buf < limit);
|
|
|
|
prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
|
|
old_size, new_size);
|
|
} else {
|
|
/* this is just created, so create an empty entry. */
|
|
de->inode = 0;
|
|
de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
|
|
}
|
|
}
|
|
|
|
static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
|
|
struct ext4_iloc *iloc)
|
|
{
|
|
int ret;
|
|
int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
|
|
int new_size = get_max_inline_xattr_value_size(dir, iloc);
|
|
|
|
if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
|
|
return -ENOSPC;
|
|
|
|
ret = ext4_update_inline_data(handle, dir,
|
|
new_size + EXT4_MIN_INLINE_DATA_SIZE);
|
|
if (ret)
|
|
return ret;
|
|
|
|
ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
|
|
EXT4_I(dir)->i_inline_size -
|
|
EXT4_MIN_INLINE_DATA_SIZE);
|
|
dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
|
|
return 0;
|
|
}
|
|
|
|
static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
|
|
struct ext4_iloc *iloc,
|
|
void *buf, int inline_size)
|
|
{
|
|
ext4_create_inline_data(handle, inode, inline_size);
|
|
ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
|
|
ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
}
|
|
|
|
static int ext4_finish_convert_inline_dir(handle_t *handle,
|
|
struct inode *inode,
|
|
struct buffer_head *dir_block,
|
|
void *buf,
|
|
int inline_size)
|
|
{
|
|
int err, csum_size = 0, header_size = 0;
|
|
struct ext4_dir_entry_2 *de;
|
|
struct ext4_dir_entry_tail *t;
|
|
void *target = dir_block->b_data;
|
|
|
|
/*
|
|
* First create "." and ".." and then copy the dir information
|
|
* back to the block.
|
|
*/
|
|
de = (struct ext4_dir_entry_2 *)target;
|
|
de = ext4_init_dot_dotdot(inode, de,
|
|
inode->i_sb->s_blocksize, csum_size,
|
|
le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
|
|
header_size = (void *)de - target;
|
|
|
|
memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
|
|
inline_size - EXT4_INLINE_DOTDOT_SIZE);
|
|
|
|
if (ext4_has_metadata_csum(inode->i_sb))
|
|
csum_size = sizeof(struct ext4_dir_entry_tail);
|
|
|
|
inode->i_size = inode->i_sb->s_blocksize;
|
|
i_size_write(inode, inode->i_sb->s_blocksize);
|
|
EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
|
|
ext4_update_final_de(dir_block->b_data,
|
|
inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
|
|
inode->i_sb->s_blocksize - csum_size);
|
|
|
|
if (csum_size) {
|
|
t = EXT4_DIRENT_TAIL(dir_block->b_data,
|
|
inode->i_sb->s_blocksize);
|
|
initialize_dirent_tail(t, inode->i_sb->s_blocksize);
|
|
}
|
|
set_buffer_uptodate(dir_block);
|
|
err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
|
|
if (err)
|
|
return err;
|
|
set_buffer_verified(dir_block);
|
|
return ext4_mark_inode_dirty(handle, inode);
|
|
}
|
|
|
|
static int ext4_convert_inline_data_nolock(handle_t *handle,
|
|
struct inode *inode,
|
|
struct ext4_iloc *iloc)
|
|
{
|
|
int error;
|
|
void *buf = NULL;
|
|
struct buffer_head *data_bh = NULL;
|
|
struct ext4_map_blocks map;
|
|
int inline_size;
|
|
|
|
inline_size = ext4_get_inline_size(inode);
|
|
buf = kmalloc(inline_size, GFP_NOFS);
|
|
if (!buf) {
|
|
error = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
error = ext4_read_inline_data(inode, buf, inline_size, iloc);
|
|
if (error < 0)
|
|
goto out;
|
|
|
|
/*
|
|
* Make sure the inline directory entries pass checks before we try to
|
|
* convert them, so that we avoid touching stuff that needs fsck.
|
|
*/
|
|
if (S_ISDIR(inode->i_mode)) {
|
|
error = ext4_check_all_de(inode, iloc->bh,
|
|
buf + EXT4_INLINE_DOTDOT_SIZE,
|
|
inline_size - EXT4_INLINE_DOTDOT_SIZE);
|
|
if (error)
|
|
goto out;
|
|
}
|
|
|
|
error = ext4_destroy_inline_data_nolock(handle, inode);
|
|
if (error)
|
|
goto out;
|
|
|
|
map.m_lblk = 0;
|
|
map.m_len = 1;
|
|
map.m_flags = 0;
|
|
error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
|
|
if (error < 0)
|
|
goto out_restore;
|
|
if (!(map.m_flags & EXT4_MAP_MAPPED)) {
|
|
error = -EIO;
|
|
goto out_restore;
|
|
}
|
|
|
|
data_bh = sb_getblk(inode->i_sb, map.m_pblk);
|
|
if (!data_bh) {
|
|
error = -ENOMEM;
|
|
goto out_restore;
|
|
}
|
|
|
|
lock_buffer(data_bh);
|
|
error = ext4_journal_get_create_access(handle, data_bh);
|
|
if (error) {
|
|
unlock_buffer(data_bh);
|
|
error = -EIO;
|
|
goto out_restore;
|
|
}
|
|
memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
|
|
|
|
if (!S_ISDIR(inode->i_mode)) {
|
|
memcpy(data_bh->b_data, buf, inline_size);
|
|
set_buffer_uptodate(data_bh);
|
|
error = ext4_handle_dirty_metadata(handle,
|
|
inode, data_bh);
|
|
} else {
|
|
error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
|
|
buf, inline_size);
|
|
}
|
|
|
|
unlock_buffer(data_bh);
|
|
out_restore:
|
|
if (error)
|
|
ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
|
|
|
|
out:
|
|
brelse(data_bh);
|
|
kfree(buf);
|
|
return error;
|
|
}
|
|
|
|
/*
|
|
* Try to add the new entry to the inline data.
|
|
* If succeeds, return 0. If not, extended the inline dir and copied data to
|
|
* the new created block.
|
|
*/
|
|
int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
|
|
struct inode *dir, struct inode *inode)
|
|
{
|
|
int ret, inline_size, no_expand;
|
|
void *inline_start;
|
|
struct ext4_iloc iloc;
|
|
|
|
ret = ext4_get_inode_loc(dir, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
ext4_write_lock_xattr(dir, &no_expand);
|
|
if (!ext4_has_inline_data(dir))
|
|
goto out;
|
|
|
|
inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
|
|
EXT4_INLINE_DOTDOT_SIZE;
|
|
inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
|
|
|
|
ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
|
|
inline_start, inline_size);
|
|
if (ret != -ENOSPC)
|
|
goto out;
|
|
|
|
/* check whether it can be inserted to inline xattr space. */
|
|
inline_size = EXT4_I(dir)->i_inline_size -
|
|
EXT4_MIN_INLINE_DATA_SIZE;
|
|
if (!inline_size) {
|
|
/* Try to use the xattr space.*/
|
|
ret = ext4_update_inline_dir(handle, dir, &iloc);
|
|
if (ret && ret != -ENOSPC)
|
|
goto out;
|
|
|
|
inline_size = EXT4_I(dir)->i_inline_size -
|
|
EXT4_MIN_INLINE_DATA_SIZE;
|
|
}
|
|
|
|
if (inline_size) {
|
|
inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
|
|
|
|
ret = ext4_add_dirent_to_inline(handle, fname, dir,
|
|
inode, &iloc, inline_start,
|
|
inline_size);
|
|
|
|
if (ret != -ENOSPC)
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* The inline space is filled up, so create a new block for it.
|
|
* As the extent tree will be created, we have to save the inline
|
|
* dir first.
|
|
*/
|
|
ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
|
|
|
|
out:
|
|
ext4_write_unlock_xattr(dir, &no_expand);
|
|
ext4_mark_inode_dirty(handle, dir);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* This function fills a red-black tree with information from an
|
|
* inlined dir. It returns the number directory entries loaded
|
|
* into the tree. If there is an error it is returned in err.
|
|
*/
|
|
int htree_inlinedir_to_tree(struct file *dir_file,
|
|
struct inode *dir, ext4_lblk_t block,
|
|
struct dx_hash_info *hinfo,
|
|
__u32 start_hash, __u32 start_minor_hash,
|
|
int *has_inline_data)
|
|
{
|
|
int err = 0, count = 0;
|
|
unsigned int parent_ino;
|
|
int pos;
|
|
struct ext4_dir_entry_2 *de;
|
|
struct inode *inode = file_inode(dir_file);
|
|
int ret, inline_size = 0;
|
|
struct ext4_iloc iloc;
|
|
void *dir_buf = NULL;
|
|
struct ext4_dir_entry_2 fake;
|
|
struct fscrypt_str tmp_str;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
*has_inline_data = 0;
|
|
goto out;
|
|
}
|
|
|
|
inline_size = ext4_get_inline_size(inode);
|
|
dir_buf = kmalloc(inline_size, GFP_NOFS);
|
|
if (!dir_buf) {
|
|
ret = -ENOMEM;
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
goto out;
|
|
}
|
|
|
|
ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
pos = 0;
|
|
parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
|
|
while (pos < inline_size) {
|
|
/*
|
|
* As inlined dir doesn't store any information about '.' and
|
|
* only the inode number of '..' is stored, we have to handle
|
|
* them differently.
|
|
*/
|
|
if (pos == 0) {
|
|
fake.inode = cpu_to_le32(inode->i_ino);
|
|
fake.name_len = 1;
|
|
strcpy(fake.name, ".");
|
|
fake.rec_len = ext4_rec_len_to_disk(
|
|
EXT4_DIR_REC_LEN(fake.name_len),
|
|
inline_size);
|
|
ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
|
|
de = &fake;
|
|
pos = EXT4_INLINE_DOTDOT_OFFSET;
|
|
} else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
|
|
fake.inode = cpu_to_le32(parent_ino);
|
|
fake.name_len = 2;
|
|
strcpy(fake.name, "..");
|
|
fake.rec_len = ext4_rec_len_to_disk(
|
|
EXT4_DIR_REC_LEN(fake.name_len),
|
|
inline_size);
|
|
ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
|
|
de = &fake;
|
|
pos = EXT4_INLINE_DOTDOT_SIZE;
|
|
} else {
|
|
de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
|
|
pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
|
|
if (ext4_check_dir_entry(inode, dir_file, de,
|
|
iloc.bh, dir_buf,
|
|
inline_size, pos)) {
|
|
ret = count;
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
ext4fs_dirhash(de->name, de->name_len, hinfo);
|
|
if ((hinfo->hash < start_hash) ||
|
|
((hinfo->hash == start_hash) &&
|
|
(hinfo->minor_hash < start_minor_hash)))
|
|
continue;
|
|
if (de->inode == 0)
|
|
continue;
|
|
tmp_str.name = de->name;
|
|
tmp_str.len = de->name_len;
|
|
err = ext4_htree_store_dirent(dir_file, hinfo->hash,
|
|
hinfo->minor_hash, de, &tmp_str);
|
|
if (err) {
|
|
count = err;
|
|
goto out;
|
|
}
|
|
count++;
|
|
}
|
|
ret = count;
|
|
out:
|
|
kfree(dir_buf);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* So this function is called when the volume is mkfsed with
|
|
* dir_index disabled. In order to keep f_pos persistent
|
|
* after we convert from an inlined dir to a blocked based,
|
|
* we just pretend that we are a normal dir and return the
|
|
* offset as if '.' and '..' really take place.
|
|
*
|
|
*/
|
|
int ext4_read_inline_dir(struct file *file,
|
|
struct dir_context *ctx,
|
|
int *has_inline_data)
|
|
{
|
|
unsigned int offset, parent_ino;
|
|
int i;
|
|
struct ext4_dir_entry_2 *de;
|
|
struct super_block *sb;
|
|
struct inode *inode = file_inode(file);
|
|
int ret, inline_size = 0;
|
|
struct ext4_iloc iloc;
|
|
void *dir_buf = NULL;
|
|
int dotdot_offset, dotdot_size, extra_offset, extra_size;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
*has_inline_data = 0;
|
|
goto out;
|
|
}
|
|
|
|
inline_size = ext4_get_inline_size(inode);
|
|
dir_buf = kmalloc(inline_size, GFP_NOFS);
|
|
if (!dir_buf) {
|
|
ret = -ENOMEM;
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
goto out;
|
|
}
|
|
|
|
ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
ret = 0;
|
|
sb = inode->i_sb;
|
|
parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
|
|
offset = ctx->pos;
|
|
|
|
/*
|
|
* dotdot_offset and dotdot_size is the real offset and
|
|
* size for ".." and "." if the dir is block based while
|
|
* the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
|
|
* So we will use extra_offset and extra_size to indicate them
|
|
* during the inline dir iteration.
|
|
*/
|
|
dotdot_offset = EXT4_DIR_REC_LEN(1);
|
|
dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
|
|
extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
|
|
extra_size = extra_offset + inline_size;
|
|
|
|
/*
|
|
* If the version has changed since the last call to
|
|
* readdir(2), then we might be pointing to an invalid
|
|
* dirent right now. Scan from the start of the inline
|
|
* dir to make sure.
|
|
*/
|
|
if (file->f_version != inode->i_version) {
|
|
for (i = 0; i < extra_size && i < offset;) {
|
|
/*
|
|
* "." is with offset 0 and
|
|
* ".." is dotdot_offset.
|
|
*/
|
|
if (!i) {
|
|
i = dotdot_offset;
|
|
continue;
|
|
} else if (i == dotdot_offset) {
|
|
i = dotdot_size;
|
|
continue;
|
|
}
|
|
/* for other entry, the real offset in
|
|
* the buf has to be tuned accordingly.
|
|
*/
|
|
de = (struct ext4_dir_entry_2 *)
|
|
(dir_buf + i - extra_offset);
|
|
/* It's too expensive to do a full
|
|
* dirent test each time round this
|
|
* loop, but we do have to test at
|
|
* least that it is non-zero. A
|
|
* failure will be detected in the
|
|
* dirent test below. */
|
|
if (ext4_rec_len_from_disk(de->rec_len, extra_size)
|
|
< EXT4_DIR_REC_LEN(1))
|
|
break;
|
|
i += ext4_rec_len_from_disk(de->rec_len,
|
|
extra_size);
|
|
}
|
|
offset = i;
|
|
ctx->pos = offset;
|
|
file->f_version = inode->i_version;
|
|
}
|
|
|
|
while (ctx->pos < extra_size) {
|
|
if (ctx->pos == 0) {
|
|
if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
|
|
goto out;
|
|
ctx->pos = dotdot_offset;
|
|
continue;
|
|
}
|
|
|
|
if (ctx->pos == dotdot_offset) {
|
|
if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
|
|
goto out;
|
|
ctx->pos = dotdot_size;
|
|
continue;
|
|
}
|
|
|
|
de = (struct ext4_dir_entry_2 *)
|
|
(dir_buf + ctx->pos - extra_offset);
|
|
if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
|
|
extra_size, ctx->pos))
|
|
goto out;
|
|
if (le32_to_cpu(de->inode)) {
|
|
if (!dir_emit(ctx, de->name, de->name_len,
|
|
le32_to_cpu(de->inode),
|
|
get_dtype(sb, de->file_type)))
|
|
goto out;
|
|
}
|
|
ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
|
|
}
|
|
out:
|
|
kfree(dir_buf);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
|
|
struct ext4_dir_entry_2 **parent_de,
|
|
int *retval)
|
|
{
|
|
struct ext4_iloc iloc;
|
|
|
|
*retval = ext4_get_inode_loc(inode, &iloc);
|
|
if (*retval)
|
|
return NULL;
|
|
|
|
*parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
|
|
|
|
return iloc.bh;
|
|
}
|
|
|
|
/*
|
|
* Try to create the inline data for the new dir.
|
|
* If it succeeds, return 0, otherwise return the error.
|
|
* In case of ENOSPC, the caller should create the normal disk layout dir.
|
|
*/
|
|
int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
|
|
struct inode *inode)
|
|
{
|
|
int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
|
|
struct ext4_iloc iloc;
|
|
struct ext4_dir_entry_2 *de;
|
|
|
|
ret = ext4_get_inode_loc(inode, &iloc);
|
|
if (ret)
|
|
return ret;
|
|
|
|
ret = ext4_prepare_inline_data(handle, inode, inline_size);
|
|
if (ret)
|
|
goto out;
|
|
|
|
/*
|
|
* For inline dir, we only save the inode information for the ".."
|
|
* and create a fake dentry to cover the left space.
|
|
*/
|
|
de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
|
|
de->inode = cpu_to_le32(parent->i_ino);
|
|
de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
|
|
de->inode = 0;
|
|
de->rec_len = ext4_rec_len_to_disk(
|
|
inline_size - EXT4_INLINE_DOTDOT_SIZE,
|
|
inline_size);
|
|
set_nlink(inode, 2);
|
|
inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
|
|
out:
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
struct buffer_head *ext4_find_inline_entry(struct inode *dir,
|
|
struct ext4_filename *fname,
|
|
struct ext4_dir_entry_2 **res_dir,
|
|
int *has_inline_data)
|
|
{
|
|
int ret;
|
|
struct ext4_iloc iloc;
|
|
void *inline_start;
|
|
int inline_size;
|
|
|
|
if (ext4_get_inode_loc(dir, &iloc))
|
|
return NULL;
|
|
|
|
down_read(&EXT4_I(dir)->xattr_sem);
|
|
if (!ext4_has_inline_data(dir)) {
|
|
*has_inline_data = 0;
|
|
goto out;
|
|
}
|
|
|
|
inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
|
|
EXT4_INLINE_DOTDOT_SIZE;
|
|
inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
|
|
ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
|
|
dir, fname, 0, res_dir);
|
|
if (ret == 1)
|
|
goto out_find;
|
|
if (ret < 0)
|
|
goto out;
|
|
|
|
if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
|
|
goto out;
|
|
|
|
inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
|
|
inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
|
|
|
|
ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
|
|
dir, fname, 0, res_dir);
|
|
if (ret == 1)
|
|
goto out_find;
|
|
|
|
out:
|
|
brelse(iloc.bh);
|
|
iloc.bh = NULL;
|
|
out_find:
|
|
up_read(&EXT4_I(dir)->xattr_sem);
|
|
return iloc.bh;
|
|
}
|
|
|
|
int ext4_delete_inline_entry(handle_t *handle,
|
|
struct inode *dir,
|
|
struct ext4_dir_entry_2 *de_del,
|
|
struct buffer_head *bh,
|
|
int *has_inline_data)
|
|
{
|
|
int err, inline_size, no_expand;
|
|
struct ext4_iloc iloc;
|
|
void *inline_start;
|
|
|
|
err = ext4_get_inode_loc(dir, &iloc);
|
|
if (err)
|
|
return err;
|
|
|
|
ext4_write_lock_xattr(dir, &no_expand);
|
|
if (!ext4_has_inline_data(dir)) {
|
|
*has_inline_data = 0;
|
|
goto out;
|
|
}
|
|
|
|
if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
|
|
EXT4_MIN_INLINE_DATA_SIZE) {
|
|
inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
|
|
EXT4_INLINE_DOTDOT_SIZE;
|
|
inline_size = EXT4_MIN_INLINE_DATA_SIZE -
|
|
EXT4_INLINE_DOTDOT_SIZE;
|
|
} else {
|
|
inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
|
|
inline_size = ext4_get_inline_size(dir) -
|
|
EXT4_MIN_INLINE_DATA_SIZE;
|
|
}
|
|
|
|
BUFFER_TRACE(bh, "get_write_access");
|
|
err = ext4_journal_get_write_access(handle, bh);
|
|
if (err)
|
|
goto out;
|
|
|
|
err = ext4_generic_delete_entry(handle, dir, de_del, bh,
|
|
inline_start, inline_size, 0);
|
|
if (err)
|
|
goto out;
|
|
|
|
ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
|
|
out:
|
|
ext4_write_unlock_xattr(dir, &no_expand);
|
|
if (likely(err == 0))
|
|
err = ext4_mark_inode_dirty(handle, dir);
|
|
brelse(iloc.bh);
|
|
if (err != -ENOENT)
|
|
ext4_std_error(dir->i_sb, err);
|
|
return err;
|
|
}
|
|
|
|
/*
|
|
* Get the inline dentry at offset.
|
|
*/
|
|
static inline struct ext4_dir_entry_2 *
|
|
ext4_get_inline_entry(struct inode *inode,
|
|
struct ext4_iloc *iloc,
|
|
unsigned int offset,
|
|
void **inline_start,
|
|
int *inline_size)
|
|
{
|
|
void *inline_pos;
|
|
|
|
BUG_ON(offset > ext4_get_inline_size(inode));
|
|
|
|
if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
|
|
inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
|
|
*inline_size = EXT4_MIN_INLINE_DATA_SIZE;
|
|
} else {
|
|
inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
|
|
offset -= EXT4_MIN_INLINE_DATA_SIZE;
|
|
*inline_size = ext4_get_inline_size(inode) -
|
|
EXT4_MIN_INLINE_DATA_SIZE;
|
|
}
|
|
|
|
if (inline_start)
|
|
*inline_start = inline_pos;
|
|
return (struct ext4_dir_entry_2 *)(inline_pos + offset);
|
|
}
|
|
|
|
bool empty_inline_dir(struct inode *dir, int *has_inline_data)
|
|
{
|
|
int err, inline_size;
|
|
struct ext4_iloc iloc;
|
|
void *inline_pos;
|
|
unsigned int offset;
|
|
struct ext4_dir_entry_2 *de;
|
|
bool ret = true;
|
|
|
|
err = ext4_get_inode_loc(dir, &iloc);
|
|
if (err) {
|
|
EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
|
|
err, dir->i_ino);
|
|
return true;
|
|
}
|
|
|
|
down_read(&EXT4_I(dir)->xattr_sem);
|
|
if (!ext4_has_inline_data(dir)) {
|
|
*has_inline_data = 0;
|
|
goto out;
|
|
}
|
|
|
|
de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
|
|
if (!le32_to_cpu(de->inode)) {
|
|
ext4_warning(dir->i_sb,
|
|
"bad inline directory (dir #%lu) - no `..'",
|
|
dir->i_ino);
|
|
ret = true;
|
|
goto out;
|
|
}
|
|
|
|
offset = EXT4_INLINE_DOTDOT_SIZE;
|
|
while (offset < dir->i_size) {
|
|
de = ext4_get_inline_entry(dir, &iloc, offset,
|
|
&inline_pos, &inline_size);
|
|
if (ext4_check_dir_entry(dir, NULL, de,
|
|
iloc.bh, inline_pos,
|
|
inline_size, offset)) {
|
|
ext4_warning(dir->i_sb,
|
|
"bad inline directory (dir #%lu) - "
|
|
"inode %u, rec_len %u, name_len %d"
|
|
"inline size %d",
|
|
dir->i_ino, le32_to_cpu(de->inode),
|
|
le16_to_cpu(de->rec_len), de->name_len,
|
|
inline_size);
|
|
ret = true;
|
|
goto out;
|
|
}
|
|
if (le32_to_cpu(de->inode)) {
|
|
ret = false;
|
|
goto out;
|
|
}
|
|
offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
|
|
}
|
|
|
|
out:
|
|
up_read(&EXT4_I(dir)->xattr_sem);
|
|
brelse(iloc.bh);
|
|
return ret;
|
|
}
|
|
|
|
int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
|
|
{
|
|
int ret, no_expand;
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
ret = ext4_destroy_inline_data_nolock(handle, inode);
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int ext4_inline_data_fiemap(struct inode *inode,
|
|
struct fiemap_extent_info *fieinfo,
|
|
int *has_inline, __u64 start, __u64 len)
|
|
{
|
|
__u64 physical = 0;
|
|
__u64 inline_len;
|
|
__u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
|
|
FIEMAP_EXTENT_LAST;
|
|
int error = 0;
|
|
struct ext4_iloc iloc;
|
|
|
|
down_read(&EXT4_I(inode)->xattr_sem);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
*has_inline = 0;
|
|
goto out;
|
|
}
|
|
inline_len = min_t(size_t, ext4_get_inline_size(inode),
|
|
i_size_read(inode));
|
|
if (start >= inline_len)
|
|
goto out;
|
|
if (start + len < inline_len)
|
|
inline_len = start + len;
|
|
inline_len -= start;
|
|
|
|
error = ext4_get_inode_loc(inode, &iloc);
|
|
if (error)
|
|
goto out;
|
|
|
|
physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
|
|
physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
|
|
physical += offsetof(struct ext4_inode, i_block);
|
|
|
|
if (physical)
|
|
error = fiemap_fill_next_extent(fieinfo, start, physical,
|
|
inline_len, flags);
|
|
brelse(iloc.bh);
|
|
out:
|
|
up_read(&EXT4_I(inode)->xattr_sem);
|
|
return (error < 0 ? error : 0);
|
|
}
|
|
|
|
int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
|
|
{
|
|
handle_t *handle;
|
|
int inline_size, value_len, needed_blocks, no_expand, err = 0;
|
|
size_t i_size;
|
|
void *value = NULL;
|
|
struct ext4_xattr_ibody_find is = {
|
|
.s = { .not_found = -ENODATA, },
|
|
};
|
|
struct ext4_xattr_info i = {
|
|
.name_index = EXT4_XATTR_INDEX_SYSTEM,
|
|
.name = EXT4_XATTR_SYSTEM_DATA,
|
|
};
|
|
|
|
|
|
needed_blocks = ext4_writepage_trans_blocks(inode);
|
|
handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
|
|
if (IS_ERR(handle))
|
|
return PTR_ERR(handle);
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
if (!ext4_has_inline_data(inode)) {
|
|
*has_inline = 0;
|
|
ext4_journal_stop(handle);
|
|
return 0;
|
|
}
|
|
|
|
if ((err = ext4_orphan_add(handle, inode)) != 0)
|
|
goto out;
|
|
|
|
if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
|
|
goto out;
|
|
|
|
down_write(&EXT4_I(inode)->i_data_sem);
|
|
i_size = inode->i_size;
|
|
inline_size = ext4_get_inline_size(inode);
|
|
EXT4_I(inode)->i_disksize = i_size;
|
|
|
|
if (i_size < inline_size) {
|
|
/* Clear the content in the xattr space. */
|
|
if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
|
|
if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
|
|
goto out_error;
|
|
|
|
BUG_ON(is.s.not_found);
|
|
|
|
value_len = le32_to_cpu(is.s.here->e_value_size);
|
|
value = kmalloc(value_len, GFP_NOFS);
|
|
if (!value) {
|
|
err = -ENOMEM;
|
|
goto out_error;
|
|
}
|
|
|
|
err = ext4_xattr_ibody_get(inode, i.name_index,
|
|
i.name, value, value_len);
|
|
if (err <= 0)
|
|
goto out_error;
|
|
|
|
i.value = value;
|
|
i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
|
|
i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
|
|
err = ext4_xattr_ibody_inline_set(handle, inode,
|
|
&i, &is);
|
|
if (err)
|
|
goto out_error;
|
|
}
|
|
|
|
/* Clear the content within i_blocks. */
|
|
if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
|
|
void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
|
|
memset(p + i_size, 0,
|
|
EXT4_MIN_INLINE_DATA_SIZE - i_size);
|
|
}
|
|
|
|
EXT4_I(inode)->i_inline_size = i_size <
|
|
EXT4_MIN_INLINE_DATA_SIZE ?
|
|
EXT4_MIN_INLINE_DATA_SIZE : i_size;
|
|
}
|
|
|
|
out_error:
|
|
up_write(&EXT4_I(inode)->i_data_sem);
|
|
out:
|
|
brelse(is.iloc.bh);
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
kfree(value);
|
|
if (inode->i_nlink)
|
|
ext4_orphan_del(handle, inode);
|
|
|
|
if (err == 0) {
|
|
inode->i_mtime = inode->i_ctime = current_time(inode);
|
|
err = ext4_mark_inode_dirty(handle, inode);
|
|
if (IS_SYNC(inode))
|
|
ext4_handle_sync(handle);
|
|
}
|
|
ext4_journal_stop(handle);
|
|
return err;
|
|
}
|
|
|
|
int ext4_convert_inline_data(struct inode *inode)
|
|
{
|
|
int error, needed_blocks, no_expand;
|
|
handle_t *handle;
|
|
struct ext4_iloc iloc;
|
|
|
|
if (!ext4_has_inline_data(inode)) {
|
|
ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
|
|
return 0;
|
|
}
|
|
|
|
needed_blocks = ext4_writepage_trans_blocks(inode);
|
|
|
|
iloc.bh = NULL;
|
|
error = ext4_get_inode_loc(inode, &iloc);
|
|
if (error)
|
|
return error;
|
|
|
|
handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
|
|
if (IS_ERR(handle)) {
|
|
error = PTR_ERR(handle);
|
|
goto out_free;
|
|
}
|
|
|
|
ext4_write_lock_xattr(inode, &no_expand);
|
|
if (ext4_has_inline_data(inode))
|
|
error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
|
|
ext4_write_unlock_xattr(inode, &no_expand);
|
|
ext4_journal_stop(handle);
|
|
out_free:
|
|
brelse(iloc.bh);
|
|
return error;
|
|
}
|