-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAmZuzl4ACgkQONu9yGCS aT7+ohAAyRkTis6xeME1FWIJthCJl8FzUq9nfN+OccK3TwCbXyZKXlZK8lVz0T5U DvG1Tg+rR76+hIJghMIy2FEPRBA19WMq9a+Ms2ZyyggPrlgksFivO8N8YgnIxabb EJPN7pAzO+GA+vd8YeGeK1ldq8QUISlN35s+hkur1jeBctWRcpHeOTjIej/Qytmj ny1o7hCp179+SPygSEYEYtguACaQflhfBjOgAQ9DwWjP6vO2W9Vb16X4tiT0udHm ExPjOwxbEMN/7m9gKrnl6WcIROSOy55FnfcYZP+NRY4jBlANUgXF1ca9hAhcIKSv oOyeRN5S3FZAdxIHG9SFU9b6MPwJSeO5ETQyfiRGNFRkXOa2tKknNSsuACu8kSwx SKJIpcuW1DkortwsYFbilXdl6TrK6oCcEczV5qtludcRoDznfUGejb5e81v3yYkO no6ORvBJSBnEObY+gpexvxQp2Ae1+YkSLJaDzYWMC+JHBIgWTz2F2qJJkP6bRAUV QduFTdTenDnL7zW3DseZGJKotU95cUoKNAwa7wfboZeygHc2+KaUOchKcqI0P9dZ pS27RzcAJJ2uufujofyxOOhzFKw98WFurfNsMZTDBwHuqReoiRAS7pi0PeTMuqUv GC8V1eIKgeWdI+pdTZLXylziiM41IylLjU/hxCrsykb+EwFa5NY= =B1lK -----END PGP SIGNATURE----- Merge 5.10.219 into android12-5.10-lts Changes in 5.10.219 x86/tsc: Trust initial offset in architectural TSC-adjust MSRs tty: n_gsm: fix possible out-of-bounds in gsm0_receive() speakup: Fix sizeof() vs ARRAY_SIZE() bug ring-buffer: Fix a race between readers and resize checks net: smc91x: Fix m68k kernel compilation for ColdFire CPU nilfs2: fix unexpected freezing of nilfs_segctor_sync() nilfs2: fix potential hang in nilfs_detach_log_writer() ALSA: core: Fix NULL module pointer assignment at card init wifi: cfg80211: fix the order of arguments for trace events of the tx_rx_evt class net: usb: qmi_wwan: add Telit FN920C04 compositions drm/amd/display: Set color_mgmt_changed to true on unsuspend ASoC: rt5645: Fix the electric noise due to the CBJ contacts floating ASoC: dt-bindings: rt5645: add cbj sleeve gpio property regulator: vqmmc-ipq4019: fix module autoloading ASoC: rt715: add vendor clear control register ASoC: da7219-aad: fix usage of device_get_named_child_node() drm/amdkfd: Flush the process wq before creating a kfd_process nvme: find numa distance only if controller has valid numa id openpromfs: finish conversion to the new mount API crypto: bcm - Fix pointer arithmetic firmware: raspberrypi: Use correct device for DMA mappings ecryptfs: Fix buffer size for tag 66 packet nilfs2: fix out-of-range warning parisc: add missing export of __cmpxchg_u8() crypto: ccp - drop platform ifdef checks crypto: x86/nh-avx2 - add missing vzeroupper crypto: x86/sha256-avx2 - add missing vzeroupper s390/cio: fix tracepoint subchannel type field jffs2: prevent xattr node from overflowing the eraseblock soc: mediatek: cmdq: Fix typo of CMDQ_JUMP_RELATIVE null_blk: Fix missing mutex_destroy() at module removal md: fix resync softlockup when bitmap size is less than array size wifi: ath10k: poll service ready message before failing x86/boot: Ignore relocations in .notes sections in walk_relocs() too qed: avoid truncating work queue length scsi: ufs: qcom: Perform read back after writing reset bit scsi: ufs-qcom: Fix ufs RST_n spec violation scsi: ufs: qcom: Perform read back after writing REG_UFS_SYS1CLK_1US scsi: ufs: ufs-qcom: Fix the Qcom register name for offset 0xD0 scsi: ufs: ufs-qcom: Clear qunipro_g4_sel for HW version major 5 scsi: ufs: qcom: Perform read back after writing unipro mode scsi: ufs: qcom: Perform read back after writing CGC enable scsi: ufs: cdns-pltfrm: Perform read back after writing HCLKDIV scsi: ufs: core: Perform read back after disabling interrupts scsi: ufs: core: Perform read back after disabling UIC_COMMAND_COMPL irqchip/alpine-msi: Fix off-by-one in allocation error path irqchip/loongson-pch-msi: Fix off-by-one on allocation error path ACPI: disable -Wstringop-truncation gfs2: Fix "ignore unlock failures after withdraw" selftests/bpf: Fix umount cgroup2 error in test_sockmap cpufreq: Reorganize checks in cpufreq_offline() cpufreq: Split cpufreq_offline() cpufreq: Rearrange locking in cpufreq_remove_dev() cpufreq: exit() callback is optional net: export inet_lookup_reuseport and inet6_lookup_reuseport net: remove duplicate reuseport_lookup functions udp: Avoid call to compute_score on multiple sites scsi: libsas: Fix the failure of adding phy with zero-address to port scsi: hpsa: Fix allocation size for Scsi_Host private data x86/purgatory: Switch to the position-independent small code model wifi: ath10k: Fix an error code problem in ath10k_dbg_sta_write_peer_debug_trigger() wifi: ath10k: populate board data for WCN3990 tcp: avoid premature drops in tcp_add_backlog() net: give more chances to rcu in netdev_wait_allrefs_any() macintosh/via-macii: Fix "BUG: sleeping function called from invalid context" wifi: carl9170: add a proper sanity check for endpoints wifi: ar5523: enable proper endpoint verification sh: kprobes: Merge arch_copy_kprobe() into arch_prepare_kprobe() Revert "sh: Handle calling csum_partial with misaligned data" selftests/binderfs: use the Makefile's rules, not Make's implicit rules HID: intel-ish-hid: ipc: Add check for pci_alloc_irq_vectors scsi: bfa: Ensure the copied buf is NUL terminated scsi: qedf: Ensure the copied buf is NUL terminated wifi: mwl8k: initialize cmd->addr[] properly usb: aqc111: stop lying about skb->truesize net: usb: sr9700: stop lying about skb->truesize m68k: Fix spinlock race in kernel thread creation m68k: mac: Fix reboot hang on Mac IIci net: ipv6: fix wrong start position when receive hop-by-hop fragment eth: sungem: remove .ndo_poll_controller to avoid deadlocks net: ethernet: cortina: Locking fixes af_unix: Fix data races in unix_release_sock/unix_stream_sendmsg net: usb: smsc95xx: stop lying about skb->truesize net: openvswitch: fix overwriting ct original tuple for ICMPv6 ipv6: sr: add missing seg6_local_exit ipv6: sr: fix incorrect unregister order ipv6: sr: fix invalid unregister error path net/mlx5: Discard command completions in internal error drm/amd/display: Fix potential index out of bounds in color transformation function ASoC: soc-acpi: add helper to identify parent driver. ASoC: Intel: Disable route checks for Skylake boards mtd: rawnand: hynix: fixed typo fbdev: shmobile: fix snprintf truncation drm/meson: vclk: fix calculation of 59.94 fractional rates drm/mediatek: Add 0 size check to mtk_drm_gem_obj powerpc/fsl-soc: hide unused const variable fbdev: sisfb: hide unused variables media: ngene: Add dvb_ca_en50221_init return value check media: radio-shark2: Avoid led_names truncations drm: bridge: cdns-mhdp8546: Fix possible null pointer dereference fbdev: sh7760fb: allow modular build media: atomisp: ssh_css: Fix a null-pointer dereference in load_video_binaries drm/arm/malidp: fix a possible null pointer dereference drm: vc4: Fix possible null pointer dereference ASoC: tracing: Export SND_SOC_DAPM_DIR_OUT to its value drm/bridge: lt9611: Don't log an error when DSI host can't be found drm/bridge: tc358775: Don't log an error when DSI host can't be found drm/panel: simple: Add missing Innolux G121X1-L03 format, flags, connector drm/mipi-dsi: use correct return type for the DSC functions RDMA/hns: Refactor the hns_roce_buf allocation flow RDMA/hns: Create QP with selected QPN for bank load balance RDMA/hns: Fix incorrect symbol types RDMA/hns: Fix return value in hns_roce_map_mr_sg RDMA/hns: Use complete parentheses in macros RDMA/hns: Modify the print level of CQE error clk: qcom: mmcc-msm8998: fix venus clock issue x86/insn: Fix PUSH instruction in x86 instruction decoder opcode map ext4: avoid excessive credit estimate in ext4_tmpfile() sunrpc: removed redundant procp check ext4: simplify calculation of blkoff in ext4_mb_new_blocks_simple ext4: fix unit mismatch in ext4_mb_new_blocks_simple ext4: try all groups in ext4_mb_new_blocks_simple ext4: remove unused parameter from ext4_mb_new_blocks_simple() ext4: fix potential unnitialized variable SUNRPC: Fix gss_free_in_token_pages() selftests/kcmp: Make the test output consistent and clear selftests/kcmp: remove unused open mode RDMA/IPoIB: Fix format truncation compilation errors net: qrtr: fix null-ptr-deref in qrtr_ns_remove net: qrtr: ns: Fix module refcnt netrom: fix possible dead-lock in nr_rt_ioctl() af_packet: do not call packet_read_pending() from tpacket_destruct_skb() sched/fair: Allow disabling sched_balance_newidle with sched_relax_domain_level greybus: lights: check return of get_channel_from_mode f2fs: fix to wait on page writeback in __clone_blkaddrs() soundwire: cadence: fix invalid PDI offset dmaengine: idma64: Add check for dma_set_max_seg_size firmware: dmi-id: add a release callback function serial: max3100: Lock port->lock when calling uart_handle_cts_change() serial: max3100: Update uart_driver_registered on driver removal serial: max3100: Fix bitwise types greybus: arche-ctrl: move device table to its right location serial: sc16is7xx: add proper sched.h include for sched_set_fifo() f2fs: compress: support chksum f2fs: add compress_mode mount option f2fs: compress: clean up parameter of __f2fs_cluster_blocks() f2fs: compress: remove unneeded preallocation f2fs: introduce FI_COMPRESS_RELEASED instead of using IMMUTABLE bit f2fs: compress: fix to relocate check condition in f2fs_{release,reserve}_compress_blocks() f2fs: add cp_error check in f2fs_write_compressed_pages f2fs: fix to force keeping write barrier for strict fsync mode f2fs: do not allow partial truncation on pinned file f2fs: fix typos in comments f2fs: fix to relocate check condition in f2fs_fallocate() f2fs: fix to check pinfile flag in f2fs_move_file_range() iio: pressure: dps310: support negative temperature values fpga: region: change FPGA indirect article to an fpga: region: Rename dev to parent for parent device docs: driver-api: fpga: avoid using UTF-8 chars fpga: region: Use standard dev_release for class driver fpga: region: add owner module and take its refcount microblaze: Remove gcc flag for non existing early_printk.c file microblaze: Remove early printk call from cpuinfo-static.c usb: gadget: u_audio: Clear uac pointer when freed. stm class: Fix a double free in stm_register_device() ppdev: Remove usage of the deprecated ida_simple_xx() API ppdev: Add an error check in register_device extcon: max8997: select IRQ_DOMAIN instead of depending on it PCI/EDR: Align EDR_PORT_DPC_ENABLE_DSM with PCI Firmware r3.3 PCI/EDR: Align EDR_PORT_LOCATE_DSM with PCI Firmware r3.3 f2fs: compress: fix to cover {reserve,release}_compress_blocks() w/ cp_rwsem lock f2fs: fix to release node block count in error path of f2fs_new_node_page() f2fs: compress: don't allow unaligned truncation on released compress inode serial: sh-sci: protect invalidating RXDMA on shutdown libsubcmd: Fix parse-options memory leak s390/ipl: Fix incorrect initialization of len fields in nvme reipl block s390/ipl: Fix incorrect initialization of nvme dump block Input: ims-pcu - fix printf string overflow Input: ioc3kbd - convert to platform remove callback returning void Input: ioc3kbd - add device table mmc: sdhci_am654: Add tuning algorithm for delay chain mmc: sdhci_am654: Write ITAPDLY for DDR52 timing mmc: sdhci_am654: Drop lookup for deprecated ti,otap-del-sel mmc: sdhci_am654: Add OTAP/ITAP delay enable mmc: sdhci_am654: Add ITAPDLYSEL in sdhci_j721e_4bit_set_clock mmc: sdhci_am654: Fix ITAPDLY for HS400 timing Input: pm8xxx-vibrator - correct VIB_MAX_LEVELS calculation drm/msm/dpu: Always flush the slave INTF on the CTL um: Fix return value in ubd_init() um: Add winch to winch_handlers before registering winch IRQ um: vector: fix bpfflash parameter evaluation drm/bridge: tc358775: fix support for jeida-18 and jeida-24 media: stk1160: fix bounds checking in stk1160_copy_video() scsi: qla2xxx: Replace all non-returning strlcpy() with strscpy() media: flexcop-usb: clean up endpoint sanity checks media: flexcop-usb: fix sanity check of bNumEndpoints powerpc/pseries: Add failure related checks for h_get_mpp and h_get_ppp um: Fix the -Wmissing-prototypes warning for __switch_mm media: cec: cec-adap: always cancel work in cec_transmit_msg_fh media: cec: cec-api: add locking in cec_release() media: core headers: fix kernel-doc warnings media: cec: fix a deadlock situation media: cec: call enable_adap on s_log_addrs media: cec: abort if the current transmit was canceled media: cec: correctly pass on reply results media: cec: use call_op and check for !unregistered media: cec-adap.c: drop activate_cnt, use state info instead media: cec: core: avoid recursive cec_claim_log_addrs media: cec: core: avoid confusing "transmit timed out" message null_blk: Fix the WARNING: modpost: missing MODULE_DESCRIPTION() regulator: bd71828: Don't overwrite runtime voltages x86/kconfig: Select ARCH_WANT_FRAME_POINTERS again when UNWINDER_FRAME_POINTER=y nfc: nci: Fix uninit-value in nci_rx_work ASoC: tas2552: Add TX path for capturing AUDIO-OUT data sunrpc: fix NFSACL RPC retry on soft mount rpcrdma: fix handling for RDMA_CM_EVENT_DEVICE_REMOVAL ipv6: sr: fix memleak in seg6_hmac_init_algo params: lift param_set_uint_minmax to common code tcp: Fix shift-out-of-bounds in dctcp_update_alpha(). openvswitch: Set the skbuff pkt_type for proper pmtud support. arm64: asm-bug: Add .align 2 to the end of __BUG_ENTRY virtio: delete vq in vp_find_vqs_msix() when request_irq() fails net: fec: avoid lock evasion when reading pps_enable tls: fix missing memory barrier in tls_init nfc: nci: Fix kcov check in nci_rx_work() nfc: nci: Fix handling of zero-length payload packets in nci_rx_work() netfilter: nfnetlink_queue: acquire rcu_read_lock() in instance_destroy_rcu() netfilter: nft_payload: restore vlan q-in-q match support spi: Don't mark message DMA mapped when no transfer in it is nvmet: fix ns enable/disable possible hang net/mlx5e: Use rx_missed_errors instead of rx_dropped for reporting buffer exhaustion dma-buf/sw-sync: don't enable IRQ from sync_print_obj() bpf: Fix potential integer overflow in resolve_btfids enic: Validate length of nl attributes in enic_set_vf_port net: usb: smsc95xx: fix changing LED_SEL bit value updated from EEPROM bpf: Allow delete from sockmap/sockhash only if update is allowed net:fec: Add fec_enet_deinit() netfilter: tproxy: bail out if IP has been disabled on the device kconfig: fix comparison to constant symbols, 'm', 'n' spi: stm32: Don't warn about spurious interrupts ipvlan: Dont Use skb->sk in ipvlan_process_v{4,6}_outbound hwmon: (shtc1) Fix property misspelling ALSA: timer: Set lower bound of start tick time genirq/cpuhotplug, x86/vector: Prevent vector leak during CPU offline media: cec: core: add adap_nb_transmit_canceled() callback SUNRPC: Fix loop termination condition in gss_free_in_token_pages() binder: fix max_thread type inconsistency mmc: core: Do not force a retune before RPMB switch io_uring: fail NOP if non-zero op flags is passed in afs: Don't cross .backup mountpoint from backup volume nilfs2: fix use-after-free of timer for log writer thread vxlan: Fix regression when dropping packets due to invalid src addresses x86/mm: Remove broken vsyscall emulation code from the page fault code netfilter: nf_tables: restrict tunnel object to NFPROTO_NETDEV netfilter: nf_tables: Fix potential data-race in __nft_obj_type_get() f2fs: fix to do sanity check on i_xattr_nid in sanity_check_inode() media: lgdt3306a: Add a check against null-pointer-def drm/amdgpu: add error handle to avoid out-of-bounds ata: pata_legacy: make legacy_exit() work again ACPI: resource: Do IRQ override on TongFang GXxHRXx and GMxHGxx arm64: tegra: Correct Tegra132 I2C alias arm64: dts: qcom: qcs404: fix bluetooth device address md/raid5: fix deadlock that raid5d() wait for itself to clear MD_SB_CHANGE_PENDING wifi: rtl8xxxu: Fix the TX power of RTL8192CU, RTL8723AU wifi: rtlwifi: rtl8192de: Fix low speed with WPA3-SAE wifi: rtlwifi: rtl8192de: Fix endianness issue in RX path arm64: dts: hi3798cv200: fix the size of GICR media: mc: mark the media devnode as registered from the, start media: mxl5xx: Move xpt structures off stack media: v4l2-core: hold videodev_lock until dev reg, finishes mmc: core: Add mmc_gpiod_set_cd_config() function mmc: sdhci-acpi: Sort DMI quirks alphabetically mmc: sdhci-acpi: Fix Lenovo Yoga Tablet 2 Pro 1380 sdcard slot not working mmc: sdhci-acpi: Disable write protect detection on Toshiba WT10-A fbdev: savage: Handle err return when savagefb_check_var failed KVM: arm64: Allow AArch32 PSTATE.M to be restored as System mode crypto: ecrdsa - Fix module auto-load on add_key crypto: qat - Fix ADF_DEV_RESET_SYNC memory leak net/ipv6: Fix route deleting failure when metric equals 0 net/9p: fix uninit-value in p9_client_rpc() intel_th: pci: Add Meteor Lake-S CPU support sparc64: Fix number of online CPUs watchdog: rti_wdt: Set min_hw_heartbeat_ms to accommodate a safety margin kdb: Fix buffer overflow during tab-complete kdb: Use format-strings rather than '\0' injection in kdb_read() kdb: Fix console handling when editing and tab-completing commands kdb: Merge identical case statements in kdb_read() kdb: Use format-specifiers rather than memset() for padding in kdb_read() net: fix __dst_negative_advice() race sparc: move struct termio to asm/termios.h ext4: fix mb_cache_entry's e_refcnt leak in ext4_xattr_block_cache_find() s390/ap: Fix crash in AP internal function modify_bitmap() nfs: fix undefined behavior in nfs_block_bits() NFS: Fix READ_PLUS when server doesn't support OP_READ_PLUS scsi: ufs: ufs-qcom: Clear qunipro_g4_sel for HW major version > 5 f2fs: compress: fix compression chksum RDMA/hns: Use mutex instead of spinlock for ida allocation RDMA/hns: Fix CQ and QP cache affinity Linux 5.10.219 Change-Id: I0e21ff44d28df2a2802a9fb35f0959bb5ab528fc Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
1249 lines
31 KiB
C
1249 lines
31 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* f2fs extent cache support
|
|
*
|
|
* Copyright (c) 2015 Motorola Mobility
|
|
* Copyright (c) 2015 Samsung Electronics
|
|
* Authors: Jaegeuk Kim <jaegeuk@kernel.org>
|
|
* Chao Yu <chao2.yu@samsung.com>
|
|
*
|
|
* block_age-based extent cache added by:
|
|
* Copyright (c) 2022 xiaomi Co., Ltd.
|
|
* http://www.xiaomi.com/
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
|
#include <linux/f2fs_fs.h>
|
|
|
|
#include "f2fs.h"
|
|
#include "node.h"
|
|
#include <trace/events/f2fs.h>
|
|
|
|
static void __set_extent_info(struct extent_info *ei,
|
|
unsigned int fofs, unsigned int len,
|
|
block_t blk, bool keep_clen,
|
|
unsigned long age, unsigned long last_blocks,
|
|
enum extent_type type)
|
|
{
|
|
ei->fofs = fofs;
|
|
ei->len = len;
|
|
|
|
if (type == EX_READ) {
|
|
ei->blk = blk;
|
|
if (keep_clen)
|
|
return;
|
|
#ifdef CONFIG_F2FS_FS_COMPRESSION
|
|
ei->c_len = 0;
|
|
#endif
|
|
} else if (type == EX_BLOCK_AGE) {
|
|
ei->age = age;
|
|
ei->last_blocks = last_blocks;
|
|
}
|
|
}
|
|
|
|
static bool __init_may_extent_tree(struct inode *inode, enum extent_type type)
|
|
{
|
|
if (type == EX_READ)
|
|
return test_opt(F2FS_I_SB(inode), READ_EXTENT_CACHE) &&
|
|
S_ISREG(inode->i_mode);
|
|
if (type == EX_BLOCK_AGE)
|
|
return test_opt(F2FS_I_SB(inode), AGE_EXTENT_CACHE) &&
|
|
(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode));
|
|
return false;
|
|
}
|
|
|
|
static bool __may_extent_tree(struct inode *inode, enum extent_type type)
|
|
{
|
|
/*
|
|
* for recovered files during mount do not create extents
|
|
* if shrinker is not registered.
|
|
*/
|
|
if (list_empty(&F2FS_I_SB(inode)->s_list))
|
|
return false;
|
|
|
|
if (!__init_may_extent_tree(inode, type))
|
|
return false;
|
|
|
|
if (type == EX_READ) {
|
|
if (is_inode_flag_set(inode, FI_NO_EXTENT))
|
|
return false;
|
|
if (is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
|
|
!f2fs_sb_has_readonly(F2FS_I_SB(inode)))
|
|
return false;
|
|
} else if (type == EX_BLOCK_AGE) {
|
|
if (is_inode_flag_set(inode, FI_COMPRESSED_FILE))
|
|
return false;
|
|
if (file_is_cold(inode))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static void __try_update_largest_extent(struct extent_tree *et,
|
|
struct extent_node *en)
|
|
{
|
|
if (et->type != EX_READ)
|
|
return;
|
|
if (en->ei.len <= et->largest.len)
|
|
return;
|
|
|
|
et->largest = en->ei;
|
|
et->largest_updated = true;
|
|
}
|
|
|
|
static bool __is_extent_mergeable(struct extent_info *back,
|
|
struct extent_info *front, enum extent_type type)
|
|
{
|
|
if (type == EX_READ) {
|
|
#ifdef CONFIG_F2FS_FS_COMPRESSION
|
|
if (back->c_len && back->len != back->c_len)
|
|
return false;
|
|
if (front->c_len && front->len != front->c_len)
|
|
return false;
|
|
#endif
|
|
return (back->fofs + back->len == front->fofs &&
|
|
back->blk + back->len == front->blk);
|
|
} else if (type == EX_BLOCK_AGE) {
|
|
return (back->fofs + back->len == front->fofs &&
|
|
abs(back->age - front->age) <= SAME_AGE_REGION &&
|
|
abs(back->last_blocks - front->last_blocks) <=
|
|
SAME_AGE_REGION);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static bool __is_back_mergeable(struct extent_info *cur,
|
|
struct extent_info *back, enum extent_type type)
|
|
{
|
|
return __is_extent_mergeable(back, cur, type);
|
|
}
|
|
|
|
static bool __is_front_mergeable(struct extent_info *cur,
|
|
struct extent_info *front, enum extent_type type)
|
|
{
|
|
return __is_extent_mergeable(cur, front, type);
|
|
}
|
|
|
|
static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re,
|
|
unsigned int ofs)
|
|
{
|
|
if (cached_re) {
|
|
if (cached_re->ofs <= ofs &&
|
|
cached_re->ofs + cached_re->len > ofs) {
|
|
return cached_re;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static struct rb_entry *__lookup_rb_tree_slow(struct rb_root_cached *root,
|
|
unsigned int ofs)
|
|
{
|
|
struct rb_node *node = root->rb_root.rb_node;
|
|
struct rb_entry *re;
|
|
|
|
while (node) {
|
|
re = rb_entry(node, struct rb_entry, rb_node);
|
|
|
|
if (ofs < re->ofs)
|
|
node = node->rb_left;
|
|
else if (ofs >= re->ofs + re->len)
|
|
node = node->rb_right;
|
|
else
|
|
return re;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
|
|
struct rb_entry *cached_re, unsigned int ofs)
|
|
{
|
|
struct rb_entry *re;
|
|
|
|
re = __lookup_rb_tree_fast(cached_re, ofs);
|
|
if (!re)
|
|
return __lookup_rb_tree_slow(root, ofs);
|
|
|
|
return re;
|
|
}
|
|
|
|
struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
|
|
struct rb_root_cached *root,
|
|
struct rb_node **parent,
|
|
unsigned long long key, bool *leftmost)
|
|
{
|
|
struct rb_node **p = &root->rb_root.rb_node;
|
|
struct rb_entry *re;
|
|
|
|
while (*p) {
|
|
*parent = *p;
|
|
re = rb_entry(*parent, struct rb_entry, rb_node);
|
|
|
|
if (key < re->key) {
|
|
p = &(*p)->rb_left;
|
|
} else {
|
|
p = &(*p)->rb_right;
|
|
*leftmost = false;
|
|
}
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
|
|
struct rb_root_cached *root,
|
|
struct rb_node **parent,
|
|
unsigned int ofs, bool *leftmost)
|
|
{
|
|
struct rb_node **p = &root->rb_root.rb_node;
|
|
struct rb_entry *re;
|
|
|
|
while (*p) {
|
|
*parent = *p;
|
|
re = rb_entry(*parent, struct rb_entry, rb_node);
|
|
|
|
if (ofs < re->ofs) {
|
|
p = &(*p)->rb_left;
|
|
} else if (ofs >= re->ofs + re->len) {
|
|
p = &(*p)->rb_right;
|
|
*leftmost = false;
|
|
} else {
|
|
f2fs_bug_on(sbi, 1);
|
|
}
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
/*
|
|
* lookup rb entry in position of @ofs in rb-tree,
|
|
* if hit, return the entry, otherwise, return NULL
|
|
* @prev_ex: extent before ofs
|
|
* @next_ex: extent after ofs
|
|
* @insert_p: insert point for new extent at ofs
|
|
* in order to simplify the insertion after.
|
|
* tree must stay unchanged between lookup and insertion.
|
|
*/
|
|
struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
|
|
struct rb_entry *cached_re,
|
|
unsigned int ofs,
|
|
struct rb_entry **prev_entry,
|
|
struct rb_entry **next_entry,
|
|
struct rb_node ***insert_p,
|
|
struct rb_node **insert_parent,
|
|
bool force, bool *leftmost)
|
|
{
|
|
struct rb_node **pnode = &root->rb_root.rb_node;
|
|
struct rb_node *parent = NULL, *tmp_node;
|
|
struct rb_entry *re = cached_re;
|
|
|
|
*insert_p = NULL;
|
|
*insert_parent = NULL;
|
|
*prev_entry = NULL;
|
|
*next_entry = NULL;
|
|
|
|
if (RB_EMPTY_ROOT(&root->rb_root))
|
|
return NULL;
|
|
|
|
if (re) {
|
|
if (re->ofs <= ofs && re->ofs + re->len > ofs)
|
|
goto lookup_neighbors;
|
|
}
|
|
|
|
if (leftmost)
|
|
*leftmost = true;
|
|
|
|
while (*pnode) {
|
|
parent = *pnode;
|
|
re = rb_entry(*pnode, struct rb_entry, rb_node);
|
|
|
|
if (ofs < re->ofs) {
|
|
pnode = &(*pnode)->rb_left;
|
|
} else if (ofs >= re->ofs + re->len) {
|
|
pnode = &(*pnode)->rb_right;
|
|
if (leftmost)
|
|
*leftmost = false;
|
|
} else {
|
|
goto lookup_neighbors;
|
|
}
|
|
}
|
|
|
|
*insert_p = pnode;
|
|
*insert_parent = parent;
|
|
|
|
re = rb_entry(parent, struct rb_entry, rb_node);
|
|
tmp_node = parent;
|
|
if (parent && ofs > re->ofs)
|
|
tmp_node = rb_next(parent);
|
|
*next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
|
|
|
|
tmp_node = parent;
|
|
if (parent && ofs < re->ofs)
|
|
tmp_node = rb_prev(parent);
|
|
*prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
|
|
return NULL;
|
|
|
|
lookup_neighbors:
|
|
if (ofs == re->ofs || force) {
|
|
/* lookup prev node for merging backward later */
|
|
tmp_node = rb_prev(&re->rb_node);
|
|
*prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
|
|
}
|
|
if (ofs == re->ofs + re->len - 1 || force) {
|
|
/* lookup next node for merging frontward later */
|
|
tmp_node = rb_next(&re->rb_node);
|
|
*next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
|
|
}
|
|
return re;
|
|
}
|
|
|
|
bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
|
|
struct rb_root_cached *root, bool check_key)
|
|
{
|
|
#ifdef CONFIG_F2FS_CHECK_FS
|
|
struct rb_node *cur = rb_first_cached(root), *next;
|
|
struct rb_entry *cur_re, *next_re;
|
|
|
|
if (!cur)
|
|
return true;
|
|
|
|
while (cur) {
|
|
next = rb_next(cur);
|
|
if (!next)
|
|
return true;
|
|
|
|
cur_re = rb_entry(cur, struct rb_entry, rb_node);
|
|
next_re = rb_entry(next, struct rb_entry, rb_node);
|
|
|
|
if (check_key) {
|
|
if (cur_re->key > next_re->key) {
|
|
f2fs_info(sbi, "inconsistent rbtree, "
|
|
"cur(%llu) next(%llu)",
|
|
cur_re->key, next_re->key);
|
|
return false;
|
|
}
|
|
goto next;
|
|
}
|
|
|
|
if (cur_re->ofs + cur_re->len > next_re->ofs) {
|
|
f2fs_info(sbi, "inconsistent rbtree, cur(%u, %u) next(%u, %u)",
|
|
cur_re->ofs, cur_re->len,
|
|
next_re->ofs, next_re->len);
|
|
return false;
|
|
}
|
|
next:
|
|
cur = next;
|
|
}
|
|
#endif
|
|
return true;
|
|
}
|
|
|
|
static struct kmem_cache *extent_tree_slab;
|
|
static struct kmem_cache *extent_node_slab;
|
|
|
|
static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et, struct extent_info *ei,
|
|
struct rb_node *parent, struct rb_node **p,
|
|
bool leftmost)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[et->type];
|
|
struct extent_node *en;
|
|
|
|
en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
|
|
if (!en)
|
|
return NULL;
|
|
|
|
en->ei = *ei;
|
|
INIT_LIST_HEAD(&en->list);
|
|
en->et = et;
|
|
|
|
rb_link_node(&en->rb_node, parent, p);
|
|
rb_insert_color_cached(&en->rb_node, &et->root, leftmost);
|
|
atomic_inc(&et->node_cnt);
|
|
atomic_inc(&eti->total_ext_node);
|
|
return en;
|
|
}
|
|
|
|
static void __detach_extent_node(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et, struct extent_node *en)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[et->type];
|
|
|
|
rb_erase_cached(&en->rb_node, &et->root);
|
|
atomic_dec(&et->node_cnt);
|
|
atomic_dec(&eti->total_ext_node);
|
|
|
|
if (et->cached_en == en)
|
|
et->cached_en = NULL;
|
|
kmem_cache_free(extent_node_slab, en);
|
|
}
|
|
|
|
/*
|
|
* Flow to release an extent_node:
|
|
* 1. list_del_init
|
|
* 2. __detach_extent_node
|
|
* 3. kmem_cache_free.
|
|
*/
|
|
static void __release_extent_node(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et, struct extent_node *en)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[et->type];
|
|
|
|
spin_lock(&eti->extent_lock);
|
|
f2fs_bug_on(sbi, list_empty(&en->list));
|
|
list_del_init(&en->list);
|
|
spin_unlock(&eti->extent_lock);
|
|
|
|
__detach_extent_node(sbi, et, en);
|
|
}
|
|
|
|
static struct extent_tree *__grab_extent_tree(struct inode *inode,
|
|
enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree_info *eti = &sbi->extent_tree[type];
|
|
struct extent_tree *et;
|
|
nid_t ino = inode->i_ino;
|
|
|
|
mutex_lock(&eti->extent_tree_lock);
|
|
et = radix_tree_lookup(&eti->extent_tree_root, ino);
|
|
if (!et) {
|
|
et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
|
|
f2fs_radix_tree_insert(&eti->extent_tree_root, ino, et);
|
|
memset(et, 0, sizeof(struct extent_tree));
|
|
et->ino = ino;
|
|
et->type = type;
|
|
et->root = RB_ROOT_CACHED;
|
|
et->cached_en = NULL;
|
|
rwlock_init(&et->lock);
|
|
INIT_LIST_HEAD(&et->list);
|
|
atomic_set(&et->node_cnt, 0);
|
|
atomic_inc(&eti->total_ext_tree);
|
|
} else {
|
|
atomic_dec(&eti->total_zombie_tree);
|
|
list_del_init(&et->list);
|
|
}
|
|
mutex_unlock(&eti->extent_tree_lock);
|
|
|
|
/* never died until evict_inode */
|
|
F2FS_I(inode)->extent_tree[type] = et;
|
|
|
|
return et;
|
|
}
|
|
|
|
static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et)
|
|
{
|
|
struct rb_node *node, *next;
|
|
struct extent_node *en;
|
|
unsigned int count = atomic_read(&et->node_cnt);
|
|
|
|
node = rb_first_cached(&et->root);
|
|
while (node) {
|
|
next = rb_next(node);
|
|
en = rb_entry(node, struct extent_node, rb_node);
|
|
__release_extent_node(sbi, et, en);
|
|
node = next;
|
|
}
|
|
|
|
return count - atomic_read(&et->node_cnt);
|
|
}
|
|
|
|
static void __drop_largest_extent(struct extent_tree *et,
|
|
pgoff_t fofs, unsigned int len)
|
|
{
|
|
if (fofs < et->largest.fofs + et->largest.len &&
|
|
fofs + len > et->largest.fofs) {
|
|
et->largest.len = 0;
|
|
et->largest_updated = true;
|
|
}
|
|
}
|
|
|
|
void f2fs_init_read_extent_tree(struct inode *inode, struct page *ipage)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
|
|
struct f2fs_extent *i_ext = &F2FS_INODE(ipage)->i_ext;
|
|
struct extent_tree *et;
|
|
struct extent_node *en;
|
|
struct extent_info ei;
|
|
|
|
if (!__may_extent_tree(inode, EX_READ)) {
|
|
/* drop largest read extent */
|
|
if (i_ext && i_ext->len) {
|
|
f2fs_wait_on_page_writeback(ipage, NODE, true, true);
|
|
i_ext->len = 0;
|
|
set_page_dirty(ipage);
|
|
}
|
|
goto out;
|
|
}
|
|
|
|
et = __grab_extent_tree(inode, EX_READ);
|
|
|
|
if (!i_ext || !i_ext->len)
|
|
goto out;
|
|
|
|
get_read_extent_info(&ei, i_ext);
|
|
|
|
write_lock(&et->lock);
|
|
if (atomic_read(&et->node_cnt))
|
|
goto unlock_out;
|
|
|
|
en = __attach_extent_node(sbi, et, &ei, NULL,
|
|
&et->root.rb_root.rb_node, true);
|
|
if (en) {
|
|
et->largest = en->ei;
|
|
et->cached_en = en;
|
|
|
|
spin_lock(&eti->extent_lock);
|
|
list_add_tail(&en->list, &eti->extent_list);
|
|
spin_unlock(&eti->extent_lock);
|
|
}
|
|
unlock_out:
|
|
write_unlock(&et->lock);
|
|
out:
|
|
if (!F2FS_I(inode)->extent_tree[EX_READ])
|
|
set_inode_flag(inode, FI_NO_EXTENT);
|
|
}
|
|
|
|
void f2fs_init_age_extent_tree(struct inode *inode)
|
|
{
|
|
if (!__init_may_extent_tree(inode, EX_BLOCK_AGE))
|
|
return;
|
|
__grab_extent_tree(inode, EX_BLOCK_AGE);
|
|
}
|
|
|
|
void f2fs_init_extent_tree(struct inode *inode)
|
|
{
|
|
/* initialize read cache */
|
|
if (__init_may_extent_tree(inode, EX_READ))
|
|
__grab_extent_tree(inode, EX_READ);
|
|
|
|
/* initialize block age cache */
|
|
if (__init_may_extent_tree(inode, EX_BLOCK_AGE))
|
|
__grab_extent_tree(inode, EX_BLOCK_AGE);
|
|
}
|
|
|
|
static bool __lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
|
|
struct extent_info *ei, enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree_info *eti = &sbi->extent_tree[type];
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[type];
|
|
struct extent_node *en;
|
|
bool ret = false;
|
|
|
|
if (!et)
|
|
return false;
|
|
|
|
trace_f2fs_lookup_extent_tree_start(inode, pgofs, type);
|
|
|
|
read_lock(&et->lock);
|
|
|
|
if (type == EX_READ &&
|
|
et->largest.fofs <= pgofs &&
|
|
et->largest.fofs + et->largest.len > pgofs) {
|
|
*ei = et->largest;
|
|
ret = true;
|
|
stat_inc_largest_node_hit(sbi);
|
|
goto out;
|
|
}
|
|
|
|
en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root,
|
|
(struct rb_entry *)et->cached_en, pgofs);
|
|
if (!en)
|
|
goto out;
|
|
|
|
if (en == et->cached_en)
|
|
stat_inc_cached_node_hit(sbi, type);
|
|
else
|
|
stat_inc_rbtree_node_hit(sbi, type);
|
|
|
|
*ei = en->ei;
|
|
spin_lock(&eti->extent_lock);
|
|
if (!list_empty(&en->list)) {
|
|
list_move_tail(&en->list, &eti->extent_list);
|
|
et->cached_en = en;
|
|
}
|
|
spin_unlock(&eti->extent_lock);
|
|
ret = true;
|
|
out:
|
|
stat_inc_total_hit(sbi, type);
|
|
read_unlock(&et->lock);
|
|
|
|
if (type == EX_READ)
|
|
trace_f2fs_lookup_read_extent_tree_end(inode, pgofs, ei);
|
|
else if (type == EX_BLOCK_AGE)
|
|
trace_f2fs_lookup_age_extent_tree_end(inode, pgofs, ei);
|
|
return ret;
|
|
}
|
|
|
|
static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et, struct extent_info *ei,
|
|
struct extent_node *prev_ex,
|
|
struct extent_node *next_ex)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[et->type];
|
|
struct extent_node *en = NULL;
|
|
|
|
if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei, et->type)) {
|
|
prev_ex->ei.len += ei->len;
|
|
ei = &prev_ex->ei;
|
|
en = prev_ex;
|
|
}
|
|
|
|
if (next_ex && __is_front_mergeable(ei, &next_ex->ei, et->type)) {
|
|
next_ex->ei.fofs = ei->fofs;
|
|
next_ex->ei.len += ei->len;
|
|
if (et->type == EX_READ)
|
|
next_ex->ei.blk = ei->blk;
|
|
if (en)
|
|
__release_extent_node(sbi, et, prev_ex);
|
|
|
|
en = next_ex;
|
|
}
|
|
|
|
if (!en)
|
|
return NULL;
|
|
|
|
__try_update_largest_extent(et, en);
|
|
|
|
spin_lock(&eti->extent_lock);
|
|
if (!list_empty(&en->list)) {
|
|
list_move_tail(&en->list, &eti->extent_list);
|
|
et->cached_en = en;
|
|
}
|
|
spin_unlock(&eti->extent_lock);
|
|
return en;
|
|
}
|
|
|
|
static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
|
|
struct extent_tree *et, struct extent_info *ei,
|
|
struct rb_node **insert_p,
|
|
struct rb_node *insert_parent,
|
|
bool leftmost)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[et->type];
|
|
struct rb_node **p;
|
|
struct rb_node *parent = NULL;
|
|
struct extent_node *en = NULL;
|
|
|
|
if (insert_p && insert_parent) {
|
|
parent = insert_parent;
|
|
p = insert_p;
|
|
goto do_insert;
|
|
}
|
|
|
|
leftmost = true;
|
|
|
|
p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent,
|
|
ei->fofs, &leftmost);
|
|
do_insert:
|
|
en = __attach_extent_node(sbi, et, ei, parent, p, leftmost);
|
|
if (!en)
|
|
return NULL;
|
|
|
|
__try_update_largest_extent(et, en);
|
|
|
|
/* update in global extent list */
|
|
spin_lock(&eti->extent_lock);
|
|
list_add_tail(&en->list, &eti->extent_list);
|
|
et->cached_en = en;
|
|
spin_unlock(&eti->extent_lock);
|
|
return en;
|
|
}
|
|
|
|
static void __update_extent_tree_range(struct inode *inode,
|
|
struct extent_info *tei, enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[type];
|
|
struct extent_node *en = NULL, *en1 = NULL;
|
|
struct extent_node *prev_en = NULL, *next_en = NULL;
|
|
struct extent_info ei, dei, prev;
|
|
struct rb_node **insert_p = NULL, *insert_parent = NULL;
|
|
unsigned int fofs = tei->fofs, len = tei->len;
|
|
unsigned int end = fofs + len;
|
|
bool updated = false;
|
|
bool leftmost = false;
|
|
|
|
if (!et)
|
|
return;
|
|
|
|
if (type == EX_READ)
|
|
trace_f2fs_update_read_extent_tree_range(inode, fofs, len,
|
|
tei->blk, 0);
|
|
else if (type == EX_BLOCK_AGE)
|
|
trace_f2fs_update_age_extent_tree_range(inode, fofs, len,
|
|
tei->age, tei->last_blocks);
|
|
|
|
write_lock(&et->lock);
|
|
|
|
if (type == EX_READ) {
|
|
if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
|
|
write_unlock(&et->lock);
|
|
return;
|
|
}
|
|
|
|
prev = et->largest;
|
|
dei.len = 0;
|
|
|
|
/*
|
|
* drop largest extent before lookup, in case it's already
|
|
* been shrunk from extent tree
|
|
*/
|
|
__drop_largest_extent(et, fofs, len);
|
|
}
|
|
|
|
/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
|
|
en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
|
|
(struct rb_entry *)et->cached_en, fofs,
|
|
(struct rb_entry **)&prev_en,
|
|
(struct rb_entry **)&next_en,
|
|
&insert_p, &insert_parent, false,
|
|
&leftmost);
|
|
if (!en)
|
|
en = next_en;
|
|
|
|
/* 2. invalidate all extent nodes in range [fofs, fofs + len - 1] */
|
|
while (en && en->ei.fofs < end) {
|
|
unsigned int org_end;
|
|
int parts = 0; /* # of parts current extent split into */
|
|
|
|
next_en = en1 = NULL;
|
|
|
|
dei = en->ei;
|
|
org_end = dei.fofs + dei.len;
|
|
f2fs_bug_on(sbi, fofs >= org_end);
|
|
|
|
if (fofs > dei.fofs && (type != EX_READ ||
|
|
fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) {
|
|
en->ei.len = fofs - en->ei.fofs;
|
|
prev_en = en;
|
|
parts = 1;
|
|
}
|
|
|
|
if (end < org_end && (type != EX_READ ||
|
|
org_end - end >= F2FS_MIN_EXTENT_LEN)) {
|
|
if (parts) {
|
|
__set_extent_info(&ei,
|
|
end, org_end - end,
|
|
end - dei.fofs + dei.blk, false,
|
|
dei.age, dei.last_blocks,
|
|
type);
|
|
en1 = __insert_extent_tree(sbi, et, &ei,
|
|
NULL, NULL, true);
|
|
next_en = en1;
|
|
} else {
|
|
__set_extent_info(&en->ei,
|
|
end, en->ei.len - (end - dei.fofs),
|
|
en->ei.blk + (end - dei.fofs), true,
|
|
dei.age, dei.last_blocks,
|
|
type);
|
|
next_en = en;
|
|
}
|
|
parts++;
|
|
}
|
|
|
|
if (!next_en) {
|
|
struct rb_node *node = rb_next(&en->rb_node);
|
|
|
|
next_en = rb_entry_safe(node, struct extent_node,
|
|
rb_node);
|
|
}
|
|
|
|
if (parts)
|
|
__try_update_largest_extent(et, en);
|
|
else
|
|
__release_extent_node(sbi, et, en);
|
|
|
|
/*
|
|
* if original extent is split into zero or two parts, extent
|
|
* tree has been altered by deletion or insertion, therefore
|
|
* invalidate pointers regard to tree.
|
|
*/
|
|
if (parts != 1) {
|
|
insert_p = NULL;
|
|
insert_parent = NULL;
|
|
}
|
|
en = next_en;
|
|
}
|
|
|
|
if (type == EX_BLOCK_AGE)
|
|
goto update_age_extent_cache;
|
|
|
|
/* 3. update extent in read extent cache */
|
|
BUG_ON(type != EX_READ);
|
|
|
|
if (tei->blk) {
|
|
__set_extent_info(&ei, fofs, len, tei->blk, false,
|
|
0, 0, EX_READ);
|
|
if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
|
|
__insert_extent_tree(sbi, et, &ei,
|
|
insert_p, insert_parent, leftmost);
|
|
|
|
/* give up extent_cache, if split and small updates happen */
|
|
if (dei.len >= 1 &&
|
|
prev.len < F2FS_MIN_EXTENT_LEN &&
|
|
et->largest.len < F2FS_MIN_EXTENT_LEN) {
|
|
et->largest.len = 0;
|
|
et->largest_updated = true;
|
|
set_inode_flag(inode, FI_NO_EXTENT);
|
|
}
|
|
}
|
|
|
|
if (is_inode_flag_set(inode, FI_NO_EXTENT))
|
|
__free_extent_tree(sbi, et);
|
|
|
|
if (et->largest_updated) {
|
|
et->largest_updated = false;
|
|
updated = true;
|
|
}
|
|
goto out_read_extent_cache;
|
|
update_age_extent_cache:
|
|
if (!tei->last_blocks)
|
|
goto out_read_extent_cache;
|
|
|
|
__set_extent_info(&ei, fofs, len, 0, false,
|
|
tei->age, tei->last_blocks, EX_BLOCK_AGE);
|
|
if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
|
|
__insert_extent_tree(sbi, et, &ei,
|
|
insert_p, insert_parent, leftmost);
|
|
out_read_extent_cache:
|
|
write_unlock(&et->lock);
|
|
|
|
if (updated)
|
|
f2fs_mark_inode_dirty_sync(inode, true);
|
|
}
|
|
|
|
#ifdef CONFIG_F2FS_FS_COMPRESSION
|
|
void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
|
|
pgoff_t fofs, block_t blkaddr, unsigned int llen,
|
|
unsigned int c_len)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
|
|
struct extent_node *en = NULL;
|
|
struct extent_node *prev_en = NULL, *next_en = NULL;
|
|
struct extent_info ei;
|
|
struct rb_node **insert_p = NULL, *insert_parent = NULL;
|
|
bool leftmost = false;
|
|
|
|
trace_f2fs_update_read_extent_tree_range(inode, fofs, llen,
|
|
blkaddr, c_len);
|
|
|
|
/* it is safe here to check FI_NO_EXTENT w/o et->lock in ro image */
|
|
if (is_inode_flag_set(inode, FI_NO_EXTENT))
|
|
return;
|
|
|
|
write_lock(&et->lock);
|
|
|
|
en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
|
|
(struct rb_entry *)et->cached_en, fofs,
|
|
(struct rb_entry **)&prev_en,
|
|
(struct rb_entry **)&next_en,
|
|
&insert_p, &insert_parent, false,
|
|
&leftmost);
|
|
if (en)
|
|
goto unlock_out;
|
|
|
|
__set_extent_info(&ei, fofs, llen, blkaddr, true, 0, 0, EX_READ);
|
|
ei.c_len = c_len;
|
|
|
|
if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
|
|
__insert_extent_tree(sbi, et, &ei,
|
|
insert_p, insert_parent, leftmost);
|
|
unlock_out:
|
|
write_unlock(&et->lock);
|
|
}
|
|
#endif
|
|
|
|
static unsigned long long __calculate_block_age(struct f2fs_sb_info *sbi,
|
|
unsigned long long new,
|
|
unsigned long long old)
|
|
{
|
|
unsigned int rem_old, rem_new;
|
|
unsigned long long res;
|
|
unsigned int weight = sbi->last_age_weight;
|
|
|
|
res = div_u64_rem(new, 100, &rem_new) * (100 - weight)
|
|
+ div_u64_rem(old, 100, &rem_old) * weight;
|
|
|
|
if (rem_new)
|
|
res += rem_new * (100 - weight) / 100;
|
|
if (rem_old)
|
|
res += rem_old * weight / 100;
|
|
|
|
return res;
|
|
}
|
|
|
|
/* This returns a new age and allocated blocks in ei */
|
|
static int __get_new_block_age(struct inode *inode, struct extent_info *ei,
|
|
block_t blkaddr)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
loff_t f_size = i_size_read(inode);
|
|
unsigned long long cur_blocks =
|
|
atomic64_read(&sbi->allocated_data_blocks);
|
|
struct extent_info tei = *ei; /* only fofs and len are valid */
|
|
|
|
/*
|
|
* When I/O is not aligned to a PAGE_SIZE, update will happen to the last
|
|
* file block even in seq write. So don't record age for newly last file
|
|
* block here.
|
|
*/
|
|
if ((f_size >> PAGE_SHIFT) == ei->fofs && f_size & (PAGE_SIZE - 1) &&
|
|
blkaddr == NEW_ADDR)
|
|
return -EINVAL;
|
|
|
|
if (__lookup_extent_tree(inode, ei->fofs, &tei, EX_BLOCK_AGE)) {
|
|
unsigned long long cur_age;
|
|
|
|
if (cur_blocks >= tei.last_blocks)
|
|
cur_age = cur_blocks - tei.last_blocks;
|
|
else
|
|
/* allocated_data_blocks overflow */
|
|
cur_age = ULLONG_MAX - tei.last_blocks + cur_blocks;
|
|
|
|
if (tei.age)
|
|
ei->age = __calculate_block_age(sbi, cur_age, tei.age);
|
|
else
|
|
ei->age = cur_age;
|
|
ei->last_blocks = cur_blocks;
|
|
WARN_ON(ei->age > cur_blocks);
|
|
return 0;
|
|
}
|
|
|
|
f2fs_bug_on(sbi, blkaddr == NULL_ADDR);
|
|
|
|
/* the data block was allocated for the first time */
|
|
if (blkaddr == NEW_ADDR)
|
|
goto out;
|
|
|
|
if (__is_valid_data_blkaddr(blkaddr) &&
|
|
!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
|
|
f2fs_bug_on(sbi, 1);
|
|
return -EINVAL;
|
|
}
|
|
out:
|
|
/*
|
|
* init block age with zero, this can happen when the block age extent
|
|
* was reclaimed due to memory constraint or system reboot
|
|
*/
|
|
ei->age = 0;
|
|
ei->last_blocks = cur_blocks;
|
|
return 0;
|
|
}
|
|
|
|
static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type type)
|
|
{
|
|
struct extent_info ei = {};
|
|
|
|
if (!__may_extent_tree(dn->inode, type))
|
|
return;
|
|
|
|
ei.fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
|
|
dn->ofs_in_node;
|
|
ei.len = 1;
|
|
|
|
if (type == EX_READ) {
|
|
if (dn->data_blkaddr == NEW_ADDR)
|
|
ei.blk = NULL_ADDR;
|
|
else
|
|
ei.blk = dn->data_blkaddr;
|
|
} else if (type == EX_BLOCK_AGE) {
|
|
if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr))
|
|
return;
|
|
}
|
|
__update_extent_tree_range(dn->inode, &ei, type);
|
|
}
|
|
|
|
static unsigned int __shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink,
|
|
enum extent_type type)
|
|
{
|
|
struct extent_tree_info *eti = &sbi->extent_tree[type];
|
|
struct extent_tree *et, *next;
|
|
struct extent_node *en;
|
|
unsigned int node_cnt = 0, tree_cnt = 0;
|
|
int remained;
|
|
|
|
if (!atomic_read(&eti->total_zombie_tree))
|
|
goto free_node;
|
|
|
|
if (!mutex_trylock(&eti->extent_tree_lock))
|
|
goto out;
|
|
|
|
/* 1. remove unreferenced extent tree */
|
|
list_for_each_entry_safe(et, next, &eti->zombie_list, list) {
|
|
if (atomic_read(&et->node_cnt)) {
|
|
write_lock(&et->lock);
|
|
node_cnt += __free_extent_tree(sbi, et);
|
|
write_unlock(&et->lock);
|
|
}
|
|
f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
|
|
list_del_init(&et->list);
|
|
radix_tree_delete(&eti->extent_tree_root, et->ino);
|
|
kmem_cache_free(extent_tree_slab, et);
|
|
atomic_dec(&eti->total_ext_tree);
|
|
atomic_dec(&eti->total_zombie_tree);
|
|
tree_cnt++;
|
|
|
|
if (node_cnt + tree_cnt >= nr_shrink)
|
|
goto unlock_out;
|
|
cond_resched();
|
|
}
|
|
mutex_unlock(&eti->extent_tree_lock);
|
|
|
|
free_node:
|
|
/* 2. remove LRU extent entries */
|
|
if (!mutex_trylock(&eti->extent_tree_lock))
|
|
goto out;
|
|
|
|
remained = nr_shrink - (node_cnt + tree_cnt);
|
|
|
|
spin_lock(&eti->extent_lock);
|
|
for (; remained > 0; remained--) {
|
|
if (list_empty(&eti->extent_list))
|
|
break;
|
|
en = list_first_entry(&eti->extent_list,
|
|
struct extent_node, list);
|
|
et = en->et;
|
|
if (!write_trylock(&et->lock)) {
|
|
/* refresh this extent node's position in extent list */
|
|
list_move_tail(&en->list, &eti->extent_list);
|
|
continue;
|
|
}
|
|
|
|
list_del_init(&en->list);
|
|
spin_unlock(&eti->extent_lock);
|
|
|
|
__detach_extent_node(sbi, et, en);
|
|
|
|
write_unlock(&et->lock);
|
|
node_cnt++;
|
|
spin_lock(&eti->extent_lock);
|
|
}
|
|
spin_unlock(&eti->extent_lock);
|
|
|
|
unlock_out:
|
|
mutex_unlock(&eti->extent_tree_lock);
|
|
out:
|
|
trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt, type);
|
|
|
|
return node_cnt + tree_cnt;
|
|
}
|
|
|
|
/* read extent cache operations */
|
|
bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs,
|
|
struct extent_info *ei)
|
|
{
|
|
if (!__may_extent_tree(inode, EX_READ))
|
|
return false;
|
|
|
|
return __lookup_extent_tree(inode, pgofs, ei, EX_READ);
|
|
}
|
|
|
|
void f2fs_update_read_extent_cache(struct dnode_of_data *dn)
|
|
{
|
|
return __update_extent_cache(dn, EX_READ);
|
|
}
|
|
|
|
void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
|
|
pgoff_t fofs, block_t blkaddr, unsigned int len)
|
|
{
|
|
struct extent_info ei = {
|
|
.fofs = fofs,
|
|
.len = len,
|
|
.blk = blkaddr,
|
|
};
|
|
|
|
if (!__may_extent_tree(dn->inode, EX_READ))
|
|
return;
|
|
|
|
__update_extent_tree_range(dn->inode, &ei, EX_READ);
|
|
}
|
|
|
|
unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
|
|
{
|
|
if (!test_opt(sbi, READ_EXTENT_CACHE))
|
|
return 0;
|
|
|
|
return __shrink_extent_tree(sbi, nr_shrink, EX_READ);
|
|
}
|
|
|
|
/* block age extent cache operations */
|
|
bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs,
|
|
struct extent_info *ei)
|
|
{
|
|
if (!__may_extent_tree(inode, EX_BLOCK_AGE))
|
|
return false;
|
|
|
|
return __lookup_extent_tree(inode, pgofs, ei, EX_BLOCK_AGE);
|
|
}
|
|
|
|
void f2fs_update_age_extent_cache(struct dnode_of_data *dn)
|
|
{
|
|
return __update_extent_cache(dn, EX_BLOCK_AGE);
|
|
}
|
|
|
|
void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn,
|
|
pgoff_t fofs, unsigned int len)
|
|
{
|
|
struct extent_info ei = {
|
|
.fofs = fofs,
|
|
.len = len,
|
|
};
|
|
|
|
if (!__may_extent_tree(dn->inode, EX_BLOCK_AGE))
|
|
return;
|
|
|
|
__update_extent_tree_range(dn->inode, &ei, EX_BLOCK_AGE);
|
|
}
|
|
|
|
unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
|
|
{
|
|
if (!test_opt(sbi, AGE_EXTENT_CACHE))
|
|
return 0;
|
|
|
|
return __shrink_extent_tree(sbi, nr_shrink, EX_BLOCK_AGE);
|
|
}
|
|
|
|
static unsigned int __destroy_extent_node(struct inode *inode,
|
|
enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[type];
|
|
unsigned int node_cnt = 0;
|
|
|
|
if (!et || !atomic_read(&et->node_cnt))
|
|
return 0;
|
|
|
|
write_lock(&et->lock);
|
|
node_cnt = __free_extent_tree(sbi, et);
|
|
write_unlock(&et->lock);
|
|
|
|
return node_cnt;
|
|
}
|
|
|
|
void f2fs_destroy_extent_node(struct inode *inode)
|
|
{
|
|
__destroy_extent_node(inode, EX_READ);
|
|
__destroy_extent_node(inode, EX_BLOCK_AGE);
|
|
}
|
|
|
|
static void __drop_extent_tree(struct inode *inode, enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[type];
|
|
bool updated = false;
|
|
|
|
if (!__may_extent_tree(inode, type))
|
|
return;
|
|
|
|
write_lock(&et->lock);
|
|
set_inode_flag(inode, FI_NO_EXTENT);
|
|
__free_extent_tree(sbi, et);
|
|
if (type == EX_READ) {
|
|
set_inode_flag(inode, FI_NO_EXTENT);
|
|
if (et->largest.len) {
|
|
et->largest.len = 0;
|
|
updated = true;
|
|
}
|
|
}
|
|
write_unlock(&et->lock);
|
|
if (updated)
|
|
f2fs_mark_inode_dirty_sync(inode, true);
|
|
}
|
|
|
|
void f2fs_drop_extent_tree(struct inode *inode)
|
|
{
|
|
__drop_extent_tree(inode, EX_READ);
|
|
__drop_extent_tree(inode, EX_BLOCK_AGE);
|
|
}
|
|
|
|
static void __destroy_extent_tree(struct inode *inode, enum extent_type type)
|
|
{
|
|
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
|
struct extent_tree_info *eti = &sbi->extent_tree[type];
|
|
struct extent_tree *et = F2FS_I(inode)->extent_tree[type];
|
|
unsigned int node_cnt = 0;
|
|
|
|
if (!et)
|
|
return;
|
|
|
|
if (inode->i_nlink && !is_bad_inode(inode) &&
|
|
atomic_read(&et->node_cnt)) {
|
|
mutex_lock(&eti->extent_tree_lock);
|
|
list_add_tail(&et->list, &eti->zombie_list);
|
|
atomic_inc(&eti->total_zombie_tree);
|
|
mutex_unlock(&eti->extent_tree_lock);
|
|
return;
|
|
}
|
|
|
|
/* free all extent info belong to this extent tree */
|
|
node_cnt = __destroy_extent_node(inode, type);
|
|
|
|
/* delete extent tree entry in radix tree */
|
|
mutex_lock(&eti->extent_tree_lock);
|
|
f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
|
|
radix_tree_delete(&eti->extent_tree_root, inode->i_ino);
|
|
kmem_cache_free(extent_tree_slab, et);
|
|
atomic_dec(&eti->total_ext_tree);
|
|
mutex_unlock(&eti->extent_tree_lock);
|
|
|
|
F2FS_I(inode)->extent_tree[type] = NULL;
|
|
|
|
trace_f2fs_destroy_extent_tree(inode, node_cnt, type);
|
|
}
|
|
|
|
void f2fs_destroy_extent_tree(struct inode *inode)
|
|
{
|
|
__destroy_extent_tree(inode, EX_READ);
|
|
__destroy_extent_tree(inode, EX_BLOCK_AGE);
|
|
}
|
|
|
|
static void __init_extent_tree_info(struct extent_tree_info *eti)
|
|
{
|
|
INIT_RADIX_TREE(&eti->extent_tree_root, GFP_NOIO);
|
|
mutex_init(&eti->extent_tree_lock);
|
|
INIT_LIST_HEAD(&eti->extent_list);
|
|
spin_lock_init(&eti->extent_lock);
|
|
atomic_set(&eti->total_ext_tree, 0);
|
|
INIT_LIST_HEAD(&eti->zombie_list);
|
|
atomic_set(&eti->total_zombie_tree, 0);
|
|
atomic_set(&eti->total_ext_node, 0);
|
|
}
|
|
|
|
void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
|
|
{
|
|
__init_extent_tree_info(&sbi->extent_tree[EX_READ]);
|
|
__init_extent_tree_info(&sbi->extent_tree[EX_BLOCK_AGE]);
|
|
|
|
/* initialize for block age extents */
|
|
atomic64_set(&sbi->allocated_data_blocks, 0);
|
|
sbi->hot_data_age_threshold = DEF_HOT_DATA_AGE_THRESHOLD;
|
|
sbi->warm_data_age_threshold = DEF_WARM_DATA_AGE_THRESHOLD;
|
|
sbi->last_age_weight = LAST_AGE_WEIGHT;
|
|
}
|
|
|
|
int __init f2fs_create_extent_cache(void)
|
|
{
|
|
extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
|
|
sizeof(struct extent_tree));
|
|
if (!extent_tree_slab)
|
|
return -ENOMEM;
|
|
extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
|
|
sizeof(struct extent_node));
|
|
if (!extent_node_slab) {
|
|
kmem_cache_destroy(extent_tree_slab);
|
|
return -ENOMEM;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void f2fs_destroy_extent_cache(void)
|
|
{
|
|
kmem_cache_destroy(extent_node_slab);
|
|
kmem_cache_destroy(extent_tree_slab);
|
|
}
|