Changes in 4.9.215 x86/vdso: Use RDPID in preference to LSL when available KVM: x86: emulate RDPID ALSA: hda: Use scnprintf() for printing texts for sysfs/procfs ecryptfs: fix a memory leak bug in parse_tag_1_packet() ecryptfs: fix a memory leak bug in ecryptfs_init_messaging() ALSA: usb-audio: Apply sample rate quirk for Audioengine D1 ext4: don't assume that mmp_nodename/bdevname have NUL ext4: fix checksum errors with indexed dirs ext4: improve explanation of a mount failure caused by a misconfigured kernel Btrfs: fix race between using extent maps and merging them btrfs: log message when rw remount is attempted with unclean tree-log perf/x86/amd: Add missing L2 misses event spec to AMD Family 17h's event map padata: Remove broken queue flushing s390/time: Fix clk type in get_tod_clock perf/x86/intel: Fix inaccurate period in context switch for auto-reload hwmon: (pmbus/ltc2978) Fix PMBus polling of MFR_COMMON definitions. jbd2: move the clearing of b_modified flag to the journal_unmap_buffer() jbd2: do not clear the BH_Mapped flag when forgetting a metadata buffer btrfs: print message when tree-log replay starts scsi: qla2xxx: fix a potential NULL pointer dereference Revert "KVM: VMX: Add non-canonical check on writes to RTIT address MSRs" drm/gma500: Fixup fbdev stolen size usage evaluation cpu/hotplug, stop_machine: Fix stop_machine vs hotplug order brcmfmac: Fix use after free in brcmf_sdio_readframes() gianfar: Fix TX timestamping with a stacked DSA driver pinctrl: sh-pfc: sh7264: Fix CAN function GPIOs pxa168fb: Fix the function used to release some memory in an error handling path media: i2c: mt9v032: fix enum mbus codes and frame sizes powerpc/powernv/iov: Ensure the pdn for VFs always contains a valid PE number gpio: gpio-grgpio: fix possible sleep-in-atomic-context bugs in grgpio_irq_map/unmap() media: sti: bdisp: fix a possible sleep-in-atomic-context bug in bdisp_device_run() pinctrl: baytrail: Do not clear IRQ flags on direct-irq enabled pins efi/x86: Map the entire EFI vendor string before copying it MIPS: Loongson: Fix potential NULL dereference in loongson3_platform_init() sparc: Add .exit.data section. uio: fix a sleep-in-atomic-context bug in uio_dmem_genirq_irqcontrol() usb: gadget: udc: fix possible sleep-in-atomic-context bugs in gr_probe() jbd2: clear JBD2_ABORT flag before journal_reset to update log tail info when load journal x86/sysfb: Fix check for bad VRAM size tracing: Fix tracing_stat return values in error handling paths tracing: Fix very unlikely race of registering two stat tracers ext4, jbd2: ensure panic when aborting with zero errno kconfig: fix broken dependency in randconfig-generated .config clk: qcom: rcg2: Don't crash if our parent can't be found; return an error drm/amdgpu: remove 4 set but not used variable in amdgpu_atombios_get_connector_info_from_object_table regulator: rk808: Lower log level on optional GPIOs being not available net/wan/fsl_ucc_hdlc: reject muram offsets above 64K PCI/IOV: Fix memory leak in pci_iov_add_virtfn() NFC: port100: Convert cpu_to_le16(le16_to_cpu(E1) + E2) to use le16_add_cpu(). media: v4l2-device.h: Explicitly compare grp{id,mask} to zero in v4l2_device macros reiserfs: Fix spurious unlock in reiserfs_fill_super() error handling ALSA: usx2y: Adjust indentation in snd_usX2Y_hwdep_dsp_status b43legacy: Fix -Wcast-function-type ipw2x00: Fix -Wcast-function-type iwlegacy: Fix -Wcast-function-type rtlwifi: rtl_pci: Fix -Wcast-function-type orinoco: avoid assertion in case of NULL pointer ACPICA: Disassembler: create buffer fields in ACPI_PARSE_LOAD_PASS1 scsi: aic7xxx: Adjust indentation in ahc_find_syncrate drm/mediatek: handle events when enabling/disabling crtc ARM: dts: r8a7779: Add device node for ARM global timer x86/vdso: Provide missing include file PM / devfreq: rk3399_dmc: Add COMPILE_TEST and HAVE_ARM_SMCCC dependency pinctrl: sh-pfc: sh7269: Fix CAN function GPIOs RDMA/rxe: Fix error type of mmap_offset ALSA: sh: Fix compile warning wrt const tools lib api fs: Fix gcc9 stringop-truncation compilation error usbip: Fix unsafe unaligned pointer usage udf: Fix free space reporting for metadata and virtual partitions soc/tegra: fuse: Correct straps' address for older Tegra124 device trees rcu: Use WRITE_ONCE() for assignments to ->pprev for hlist_nulls Input: edt-ft5x06 - work around first register access error wan: ixp4xx_hss: fix compile-testing on 64-bit ASoC: atmel: fix build error with CONFIG_SND_ATMEL_SOC_DMA=m tty: synclinkmp: Adjust indentation in several functions tty: synclink_gt: Adjust indentation in several functions driver core: platform: Prevent resouce overflow from causing infinite loops driver core: Print device when resources present in really_probe() vme: bridges: reduce stack usage drm/nouveau/gr/gk20a,gm200-: add terminators to method lists read from fw drm/nouveau: Fix copy-paste error in nouveau_fence_wait_uevent_handler drm/vmwgfx: prevent memory leak in vmw_cmdbuf_res_add usb: musb: omap2430: Get rid of musb .set_vbus for omap2430 glue iommu/arm-smmu-v3: Use WRITE_ONCE() when changing validity of an STE scsi: iscsi: Don't destroy session if there are outstanding connections arm64: fix alternatives with LLVM's integrated assembler pwm: omap-dmtimer: Remove PWM chip in .remove before making it unfunctional cmd64x: potential buffer overflow in cmd64x_program_timings() ide: serverworks: potential overflow in svwks_set_pio_mode() remoteproc: Initialize rproc_class before use x86/decoder: Add TEST opcode to Group3-2 s390/ftrace: generate traced function stack frame driver core: platform: fix u32 greater or equal to zero comparison ALSA: hda - Add docking station support for Lenovo Thinkpad T420s powerpc/sriov: Remove VF eeh_dev state when disabling SR-IOV jbd2: switch to use jbd2_journal_abort() when failed to submit the commit record ARM: 8951/1: Fix Kexec compilation issue. hostap: Adjust indentation in prism2_hostapd_add_sta iwlegacy: ensure loop counter addr does not wrap and cause an infinite loop cifs: fix NULL dereference in match_prepath irqchip/gic-v3: Only provision redistributors that are enabled in ACPI drm/nouveau/disp/nv50-: prevent oops when no channel method map provided ftrace: fpid_next() should increase position index trigger_next should increase position index radeon: insert 10ms sleep in dce5_crtc_load_lut ocfs2: fix a NULL pointer dereference when call ocfs2_update_inode_fsync_trans() lib/scatterlist.c: adjust indentation in __sg_alloc_table reiserfs: prevent NULL pointer dereference in reiserfs_insert_item() bcache: explicity type cast in bset_bkey_last() irqchip/gic-v3-its: Reference to its_invall_cmd descriptor when building INVALL iwlwifi: mvm: Fix thermal zone registration microblaze: Prevent the overflow of the start brd: check and limit max_part par help_next should increase position index selinux: ensure we cleanup the internal AVC counters on error in avc_update() enic: prevent waking up stopped tx queues over watchdog reset net/sched: matchall: add missing validation of TCA_MATCHALL_FLAGS net/sched: flower: add missing validation of TCA_FLOWER_FLAGS floppy: check FDC index for errors before assigning it vt: selection, handle pending signals in paste_selection staging: android: ashmem: Disallow ashmem memory from being remapped staging: vt6656: fix sign of rx_dbm to bb_pre_ed_rssi. xhci: Force Maximum Packet size for Full-speed bulk devices to valid range. usb: uas: fix a plug & unplug racing USB: Fix novation SourceControl XL after suspend USB: hub: Don't record a connect-change event during reset-resume staging: rtl8188eu: Fix potential security hole staging: rtl8188eu: Fix potential overuse of kernel memory x86/mce/amd: Publish the bank pointer only after setup has succeeded x86/mce/amd: Fix kobject lifetime tty/serial: atmel: manage shutdown in case of RS485 or ISO7816 mode tty: serial: imx: setup the correct sg entry for tx dma Revert "ipc,sem: remove uneeded sem_undo_list lock usage in exit_sem()" xhci: apply XHCI_PME_STUCK_QUIRK to Intel Comet Lake platforms KVM: x86: don't notify userspace IOAPIC on edge-triggered interrupt EOI VT_RESIZEX: get rid of field-by-field copyin vt: vt_ioctl: fix race in VT_RESIZEX lib/stackdepot.c: fix global out-of-bounds in stack_slabs KVM: nVMX: Don't emulate instructions in guest mode netfilter: xt_bpf: add overflow checks ext4: fix a data race in EXT4_I(inode)->i_disksize ext4: add cond_resched() to __ext4_find_entry() ext4: fix mount failure with quota configured as module ext4: rename s_journal_flag_rwsem to s_writepages_rwsem ext4: fix race between writepages and enabling EXT4_EXTENTS_FL KVM: nVMX: Refactor IO bitmap checks into helper function KVM: nVMX: Check IO instruction VM-exit conditions KVM: apic: avoid calculating pending eoi from an uninitialized val Btrfs: fix btrfs_wait_ordered_range() so that it waits for all ordered extents scsi: Revert "RDMA/isert: Fix a recently introduced regression related to logout" scsi: Revert "target: iscsi: Wait for all commands to finish before freeing a session" usb: gadget: composite: Fix bMaxPower for SuperSpeedPlus staging: greybus: use after free in gb_audio_manager_remove_all() ecryptfs: replace BUG_ON with error handling code ALSA: rawmidi: Avoid bit fields for state flags ALSA: seq: Avoid concurrent access to queue flags ALSA: seq: Fix concurrent access to queue current tick/time netfilter: xt_hashlimit: limit the max size of hashtable ata: ahci: Add shutdown to freeze hardware resources of ahci xen: Enable interrupts when calling _cond_resched() s390/mm: Explicitly compare PAGE_DEFAULT_KEY against zero in storage_key_init_range Linux 4.9.215 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I4c663321dde48cd2a324e59acb70c99f75f9344e
574 lines
18 KiB
C
574 lines
18 KiB
C
#ifndef _BCACHE_BSET_H
|
|
#define _BCACHE_BSET_H
|
|
|
|
#include <linux/bcache.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/types.h>
|
|
|
|
#include "util.h" /* for time_stats */
|
|
|
|
/*
|
|
* BKEYS:
|
|
*
|
|
* A bkey contains a key, a size field, a variable number of pointers, and some
|
|
* ancillary flag bits.
|
|
*
|
|
* We use two different functions for validating bkeys, bch_ptr_invalid and
|
|
* bch_ptr_bad().
|
|
*
|
|
* bch_ptr_invalid() primarily filters out keys and pointers that would be
|
|
* invalid due to some sort of bug, whereas bch_ptr_bad() filters out keys and
|
|
* pointer that occur in normal practice but don't point to real data.
|
|
*
|
|
* The one exception to the rule that ptr_invalid() filters out invalid keys is
|
|
* that it also filters out keys of size 0 - these are keys that have been
|
|
* completely overwritten. It'd be safe to delete these in memory while leaving
|
|
* them on disk, just unnecessary work - so we filter them out when resorting
|
|
* instead.
|
|
*
|
|
* We can't filter out stale keys when we're resorting, because garbage
|
|
* collection needs to find them to ensure bucket gens don't wrap around -
|
|
* unless we're rewriting the btree node those stale keys still exist on disk.
|
|
*
|
|
* We also implement functions here for removing some number of sectors from the
|
|
* front or the back of a bkey - this is mainly used for fixing overlapping
|
|
* extents, by removing the overlapping sectors from the older key.
|
|
*
|
|
* BSETS:
|
|
*
|
|
* A bset is an array of bkeys laid out contiguously in memory in sorted order,
|
|
* along with a header. A btree node is made up of a number of these, written at
|
|
* different times.
|
|
*
|
|
* There could be many of them on disk, but we never allow there to be more than
|
|
* 4 in memory - we lazily resort as needed.
|
|
*
|
|
* We implement code here for creating and maintaining auxiliary search trees
|
|
* (described below) for searching an individial bset, and on top of that we
|
|
* implement a btree iterator.
|
|
*
|
|
* BTREE ITERATOR:
|
|
*
|
|
* Most of the code in bcache doesn't care about an individual bset - it needs
|
|
* to search entire btree nodes and iterate over them in sorted order.
|
|
*
|
|
* The btree iterator code serves both functions; it iterates through the keys
|
|
* in a btree node in sorted order, starting from either keys after a specific
|
|
* point (if you pass it a search key) or the start of the btree node.
|
|
*
|
|
* AUXILIARY SEARCH TREES:
|
|
*
|
|
* Since keys are variable length, we can't use a binary search on a bset - we
|
|
* wouldn't be able to find the start of the next key. But binary searches are
|
|
* slow anyways, due to terrible cache behaviour; bcache originally used binary
|
|
* searches and that code topped out at under 50k lookups/second.
|
|
*
|
|
* So we need to construct some sort of lookup table. Since we only insert keys
|
|
* into the last (unwritten) set, most of the keys within a given btree node are
|
|
* usually in sets that are mostly constant. We use two different types of
|
|
* lookup tables to take advantage of this.
|
|
*
|
|
* Both lookup tables share in common that they don't index every key in the
|
|
* set; they index one key every BSET_CACHELINE bytes, and then a linear search
|
|
* is used for the rest.
|
|
*
|
|
* For sets that have been written to disk and are no longer being inserted
|
|
* into, we construct a binary search tree in an array - traversing a binary
|
|
* search tree in an array gives excellent locality of reference and is very
|
|
* fast, since both children of any node are adjacent to each other in memory
|
|
* (and their grandchildren, and great grandchildren...) - this means
|
|
* prefetching can be used to great effect.
|
|
*
|
|
* It's quite useful performance wise to keep these nodes small - not just
|
|
* because they're more likely to be in L2, but also because we can prefetch
|
|
* more nodes on a single cacheline and thus prefetch more iterations in advance
|
|
* when traversing this tree.
|
|
*
|
|
* Nodes in the auxiliary search tree must contain both a key to compare against
|
|
* (we don't want to fetch the key from the set, that would defeat the purpose),
|
|
* and a pointer to the key. We use a few tricks to compress both of these.
|
|
*
|
|
* To compress the pointer, we take advantage of the fact that one node in the
|
|
* search tree corresponds to precisely BSET_CACHELINE bytes in the set. We have
|
|
* a function (to_inorder()) that takes the index of a node in a binary tree and
|
|
* returns what its index would be in an inorder traversal, so we only have to
|
|
* store the low bits of the offset.
|
|
*
|
|
* The key is 84 bits (KEY_DEV + key->key, the offset on the device). To
|
|
* compress that, we take advantage of the fact that when we're traversing the
|
|
* search tree at every iteration we know that both our search key and the key
|
|
* we're looking for lie within some range - bounded by our previous
|
|
* comparisons. (We special case the start of a search so that this is true even
|
|
* at the root of the tree).
|
|
*
|
|
* So we know the key we're looking for is between a and b, and a and b don't
|
|
* differ higher than bit 50, we don't need to check anything higher than bit
|
|
* 50.
|
|
*
|
|
* We don't usually need the rest of the bits, either; we only need enough bits
|
|
* to partition the key range we're currently checking. Consider key n - the
|
|
* key our auxiliary search tree node corresponds to, and key p, the key
|
|
* immediately preceding n. The lowest bit we need to store in the auxiliary
|
|
* search tree is the highest bit that differs between n and p.
|
|
*
|
|
* Note that this could be bit 0 - we might sometimes need all 80 bits to do the
|
|
* comparison. But we'd really like our nodes in the auxiliary search tree to be
|
|
* of fixed size.
|
|
*
|
|
* The solution is to make them fixed size, and when we're constructing a node
|
|
* check if p and n differed in the bits we needed them to. If they don't we
|
|
* flag that node, and when doing lookups we fallback to comparing against the
|
|
* real key. As long as this doesn't happen to often (and it seems to reliably
|
|
* happen a bit less than 1% of the time), we win - even on failures, that key
|
|
* is then more likely to be in cache than if we were doing binary searches all
|
|
* the way, since we're touching so much less memory.
|
|
*
|
|
* The keys in the auxiliary search tree are stored in (software) floating
|
|
* point, with an exponent and a mantissa. The exponent needs to be big enough
|
|
* to address all the bits in the original key, but the number of bits in the
|
|
* mantissa is somewhat arbitrary; more bits just gets us fewer failures.
|
|
*
|
|
* We need 7 bits for the exponent and 3 bits for the key's offset (since keys
|
|
* are 8 byte aligned); using 22 bits for the mantissa means a node is 4 bytes.
|
|
* We need one node per 128 bytes in the btree node, which means the auxiliary
|
|
* search trees take up 3% as much memory as the btree itself.
|
|
*
|
|
* Constructing these auxiliary search trees is moderately expensive, and we
|
|
* don't want to be constantly rebuilding the search tree for the last set
|
|
* whenever we insert another key into it. For the unwritten set, we use a much
|
|
* simpler lookup table - it's just a flat array, so index i in the lookup table
|
|
* corresponds to the i range of BSET_CACHELINE bytes in the set. Indexing
|
|
* within each byte range works the same as with the auxiliary search trees.
|
|
*
|
|
* These are much easier to keep up to date when we insert a key - we do it
|
|
* somewhat lazily; when we shift a key up we usually just increment the pointer
|
|
* to it, only when it would overflow do we go to the trouble of finding the
|
|
* first key in that range of bytes again.
|
|
*/
|
|
|
|
struct btree_keys;
|
|
struct btree_iter;
|
|
struct btree_iter_set;
|
|
struct bkey_float;
|
|
|
|
#define MAX_BSETS 4U
|
|
|
|
struct bset_tree {
|
|
/*
|
|
* We construct a binary tree in an array as if the array
|
|
* started at 1, so that things line up on the same cachelines
|
|
* better: see comments in bset.c at cacheline_to_bkey() for
|
|
* details
|
|
*/
|
|
|
|
/* size of the binary tree and prev array */
|
|
unsigned size;
|
|
|
|
/* function of size - precalculated for to_inorder() */
|
|
unsigned extra;
|
|
|
|
/* copy of the last key in the set */
|
|
struct bkey end;
|
|
struct bkey_float *tree;
|
|
|
|
/*
|
|
* The nodes in the bset tree point to specific keys - this
|
|
* array holds the sizes of the previous key.
|
|
*
|
|
* Conceptually it's a member of struct bkey_float, but we want
|
|
* to keep bkey_float to 4 bytes and prev isn't used in the fast
|
|
* path.
|
|
*/
|
|
uint8_t *prev;
|
|
|
|
/* The actual btree node, with pointers to each sorted set */
|
|
struct bset *data;
|
|
};
|
|
|
|
struct btree_keys_ops {
|
|
bool (*sort_cmp)(struct btree_iter_set,
|
|
struct btree_iter_set);
|
|
struct bkey *(*sort_fixup)(struct btree_iter *, struct bkey *);
|
|
bool (*insert_fixup)(struct btree_keys *, struct bkey *,
|
|
struct btree_iter *, struct bkey *);
|
|
bool (*key_invalid)(struct btree_keys *,
|
|
const struct bkey *);
|
|
bool (*key_bad)(struct btree_keys *, const struct bkey *);
|
|
bool (*key_merge)(struct btree_keys *,
|
|
struct bkey *, struct bkey *);
|
|
void (*key_to_text)(char *, size_t, const struct bkey *);
|
|
void (*key_dump)(struct btree_keys *, const struct bkey *);
|
|
|
|
/*
|
|
* Only used for deciding whether to use START_KEY(k) or just the key
|
|
* itself in a couple places
|
|
*/
|
|
bool is_extents;
|
|
};
|
|
|
|
struct btree_keys {
|
|
const struct btree_keys_ops *ops;
|
|
uint8_t page_order;
|
|
uint8_t nsets;
|
|
unsigned last_set_unwritten:1;
|
|
bool *expensive_debug_checks;
|
|
|
|
/*
|
|
* Sets of sorted keys - the real btree node - plus a binary search tree
|
|
*
|
|
* set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
|
|
* to the memory we have allocated for this btree node. Additionally,
|
|
* set[0]->data points to the entire btree node as it exists on disk.
|
|
*/
|
|
struct bset_tree set[MAX_BSETS];
|
|
};
|
|
|
|
static inline struct bset_tree *bset_tree_last(struct btree_keys *b)
|
|
{
|
|
return b->set + b->nsets;
|
|
}
|
|
|
|
static inline bool bset_written(struct btree_keys *b, struct bset_tree *t)
|
|
{
|
|
return t <= b->set + b->nsets - b->last_set_unwritten;
|
|
}
|
|
|
|
static inline bool bkey_written(struct btree_keys *b, struct bkey *k)
|
|
{
|
|
return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
|
|
}
|
|
|
|
static inline unsigned bset_byte_offset(struct btree_keys *b, struct bset *i)
|
|
{
|
|
return ((size_t) i) - ((size_t) b->set->data);
|
|
}
|
|
|
|
static inline unsigned bset_sector_offset(struct btree_keys *b, struct bset *i)
|
|
{
|
|
return bset_byte_offset(b, i) >> 9;
|
|
}
|
|
|
|
#define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t))
|
|
#define set_bytes(i) __set_bytes(i, i->keys)
|
|
|
|
#define __set_blocks(i, k, block_bytes) \
|
|
DIV_ROUND_UP(__set_bytes(i, k), block_bytes)
|
|
#define set_blocks(i, block_bytes) \
|
|
__set_blocks(i, (i)->keys, block_bytes)
|
|
|
|
static inline size_t bch_btree_keys_u64s_remaining(struct btree_keys *b)
|
|
{
|
|
struct bset_tree *t = bset_tree_last(b);
|
|
|
|
BUG_ON((PAGE_SIZE << b->page_order) <
|
|
(bset_byte_offset(b, t->data) + set_bytes(t->data)));
|
|
|
|
if (!b->last_set_unwritten)
|
|
return 0;
|
|
|
|
return ((PAGE_SIZE << b->page_order) -
|
|
(bset_byte_offset(b, t->data) + set_bytes(t->data))) /
|
|
sizeof(u64);
|
|
}
|
|
|
|
static inline struct bset *bset_next_set(struct btree_keys *b,
|
|
unsigned block_bytes)
|
|
{
|
|
struct bset *i = bset_tree_last(b)->data;
|
|
|
|
return ((void *) i) + roundup(set_bytes(i), block_bytes);
|
|
}
|
|
|
|
void bch_btree_keys_free(struct btree_keys *);
|
|
int bch_btree_keys_alloc(struct btree_keys *, unsigned, gfp_t);
|
|
void bch_btree_keys_init(struct btree_keys *, const struct btree_keys_ops *,
|
|
bool *);
|
|
|
|
void bch_bset_init_next(struct btree_keys *, struct bset *, uint64_t);
|
|
void bch_bset_build_written_tree(struct btree_keys *);
|
|
void bch_bset_fix_invalidated_key(struct btree_keys *, struct bkey *);
|
|
bool bch_bkey_try_merge(struct btree_keys *, struct bkey *, struct bkey *);
|
|
void bch_bset_insert(struct btree_keys *, struct bkey *, struct bkey *);
|
|
unsigned bch_btree_insert_key(struct btree_keys *, struct bkey *,
|
|
struct bkey *);
|
|
|
|
enum {
|
|
BTREE_INSERT_STATUS_NO_INSERT = 0,
|
|
BTREE_INSERT_STATUS_INSERT,
|
|
BTREE_INSERT_STATUS_BACK_MERGE,
|
|
BTREE_INSERT_STATUS_OVERWROTE,
|
|
BTREE_INSERT_STATUS_FRONT_MERGE,
|
|
};
|
|
|
|
/* Btree key iteration */
|
|
|
|
struct btree_iter {
|
|
size_t size, used;
|
|
#ifdef CONFIG_BCACHE_DEBUG
|
|
struct btree_keys *b;
|
|
#endif
|
|
struct btree_iter_set {
|
|
struct bkey *k, *end;
|
|
} data[MAX_BSETS];
|
|
};
|
|
|
|
typedef bool (*ptr_filter_fn)(struct btree_keys *, const struct bkey *);
|
|
|
|
struct bkey *bch_btree_iter_next(struct btree_iter *);
|
|
struct bkey *bch_btree_iter_next_filter(struct btree_iter *,
|
|
struct btree_keys *, ptr_filter_fn);
|
|
|
|
void bch_btree_iter_push(struct btree_iter *, struct bkey *, struct bkey *);
|
|
struct bkey *bch_btree_iter_init(struct btree_keys *, struct btree_iter *,
|
|
struct bkey *);
|
|
|
|
struct bkey *__bch_bset_search(struct btree_keys *, struct bset_tree *,
|
|
const struct bkey *);
|
|
|
|
/*
|
|
* Returns the first key that is strictly greater than search
|
|
*/
|
|
static inline struct bkey *bch_bset_search(struct btree_keys *b,
|
|
struct bset_tree *t,
|
|
const struct bkey *search)
|
|
{
|
|
return search ? __bch_bset_search(b, t, search) : t->data->start;
|
|
}
|
|
|
|
#define for_each_key_filter(b, k, iter, filter) \
|
|
for (bch_btree_iter_init((b), (iter), NULL); \
|
|
((k) = bch_btree_iter_next_filter((iter), (b), filter));)
|
|
|
|
#define for_each_key(b, k, iter) \
|
|
for (bch_btree_iter_init((b), (iter), NULL); \
|
|
((k) = bch_btree_iter_next(iter));)
|
|
|
|
/* Sorting */
|
|
|
|
struct bset_sort_state {
|
|
mempool_t *pool;
|
|
|
|
unsigned page_order;
|
|
unsigned crit_factor;
|
|
|
|
struct time_stats time;
|
|
};
|
|
|
|
void bch_bset_sort_state_free(struct bset_sort_state *);
|
|
int bch_bset_sort_state_init(struct bset_sort_state *, unsigned);
|
|
void bch_btree_sort_lazy(struct btree_keys *, struct bset_sort_state *);
|
|
void bch_btree_sort_into(struct btree_keys *, struct btree_keys *,
|
|
struct bset_sort_state *);
|
|
void bch_btree_sort_and_fix_extents(struct btree_keys *, struct btree_iter *,
|
|
struct bset_sort_state *);
|
|
void bch_btree_sort_partial(struct btree_keys *, unsigned,
|
|
struct bset_sort_state *);
|
|
|
|
static inline void bch_btree_sort(struct btree_keys *b,
|
|
struct bset_sort_state *state)
|
|
{
|
|
bch_btree_sort_partial(b, 0, state);
|
|
}
|
|
|
|
struct bset_stats {
|
|
size_t sets_written, sets_unwritten;
|
|
size_t bytes_written, bytes_unwritten;
|
|
size_t floats, failed;
|
|
};
|
|
|
|
void bch_btree_keys_stats(struct btree_keys *, struct bset_stats *);
|
|
|
|
/* Bkey utility code */
|
|
|
|
#define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, \
|
|
(unsigned int)(i)->keys)
|
|
|
|
static inline struct bkey *bset_bkey_idx(struct bset *i, unsigned idx)
|
|
{
|
|
return bkey_idx(i->start, idx);
|
|
}
|
|
|
|
static inline void bkey_init(struct bkey *k)
|
|
{
|
|
*k = ZERO_KEY;
|
|
}
|
|
|
|
static __always_inline int64_t bkey_cmp(const struct bkey *l,
|
|
const struct bkey *r)
|
|
{
|
|
return unlikely(KEY_INODE(l) != KEY_INODE(r))
|
|
? (int64_t) KEY_INODE(l) - (int64_t) KEY_INODE(r)
|
|
: (int64_t) KEY_OFFSET(l) - (int64_t) KEY_OFFSET(r);
|
|
}
|
|
|
|
void bch_bkey_copy_single_ptr(struct bkey *, const struct bkey *,
|
|
unsigned);
|
|
bool __bch_cut_front(const struct bkey *, struct bkey *);
|
|
bool __bch_cut_back(const struct bkey *, struct bkey *);
|
|
|
|
static inline bool bch_cut_front(const struct bkey *where, struct bkey *k)
|
|
{
|
|
BUG_ON(bkey_cmp(where, k) > 0);
|
|
return __bch_cut_front(where, k);
|
|
}
|
|
|
|
static inline bool bch_cut_back(const struct bkey *where, struct bkey *k)
|
|
{
|
|
BUG_ON(bkey_cmp(where, &START_KEY(k)) < 0);
|
|
return __bch_cut_back(where, k);
|
|
}
|
|
|
|
/*
|
|
* Pointer '*preceding_key_p' points to a memory object to store preceding
|
|
* key of k. If the preceding key does not exist, set '*preceding_key_p' to
|
|
* NULL. So the caller of preceding_key() needs to take care of memory
|
|
* which '*preceding_key_p' pointed to before calling preceding_key().
|
|
* Currently the only caller of preceding_key() is bch_btree_insert_key(),
|
|
* and it points to an on-stack variable, so the memory release is handled
|
|
* by stackframe itself.
|
|
*/
|
|
static inline void preceding_key(struct bkey *k, struct bkey **preceding_key_p)
|
|
{
|
|
if (KEY_INODE(k) || KEY_OFFSET(k)) {
|
|
(**preceding_key_p) = KEY(KEY_INODE(k), KEY_OFFSET(k), 0);
|
|
if (!(*preceding_key_p)->low)
|
|
(*preceding_key_p)->high--;
|
|
(*preceding_key_p)->low--;
|
|
} else {
|
|
(*preceding_key_p) = NULL;
|
|
}
|
|
}
|
|
|
|
static inline bool bch_ptr_invalid(struct btree_keys *b, const struct bkey *k)
|
|
{
|
|
return b->ops->key_invalid(b, k);
|
|
}
|
|
|
|
static inline bool bch_ptr_bad(struct btree_keys *b, const struct bkey *k)
|
|
{
|
|
return b->ops->key_bad(b, k);
|
|
}
|
|
|
|
static inline void bch_bkey_to_text(struct btree_keys *b, char *buf,
|
|
size_t size, const struct bkey *k)
|
|
{
|
|
return b->ops->key_to_text(buf, size, k);
|
|
}
|
|
|
|
static inline bool bch_bkey_equal_header(const struct bkey *l,
|
|
const struct bkey *r)
|
|
{
|
|
return (KEY_DIRTY(l) == KEY_DIRTY(r) &&
|
|
KEY_PTRS(l) == KEY_PTRS(r) &&
|
|
KEY_CSUM(l) == KEY_CSUM(r));
|
|
}
|
|
|
|
/* Keylists */
|
|
|
|
struct keylist {
|
|
union {
|
|
struct bkey *keys;
|
|
uint64_t *keys_p;
|
|
};
|
|
union {
|
|
struct bkey *top;
|
|
uint64_t *top_p;
|
|
};
|
|
|
|
/* Enough room for btree_split's keys without realloc */
|
|
#define KEYLIST_INLINE 16
|
|
uint64_t inline_keys[KEYLIST_INLINE];
|
|
};
|
|
|
|
static inline void bch_keylist_init(struct keylist *l)
|
|
{
|
|
l->top_p = l->keys_p = l->inline_keys;
|
|
}
|
|
|
|
static inline void bch_keylist_init_single(struct keylist *l, struct bkey *k)
|
|
{
|
|
l->keys = k;
|
|
l->top = bkey_next(k);
|
|
}
|
|
|
|
static inline void bch_keylist_push(struct keylist *l)
|
|
{
|
|
l->top = bkey_next(l->top);
|
|
}
|
|
|
|
static inline void bch_keylist_add(struct keylist *l, struct bkey *k)
|
|
{
|
|
bkey_copy(l->top, k);
|
|
bch_keylist_push(l);
|
|
}
|
|
|
|
static inline bool bch_keylist_empty(struct keylist *l)
|
|
{
|
|
return l->top == l->keys;
|
|
}
|
|
|
|
static inline void bch_keylist_reset(struct keylist *l)
|
|
{
|
|
l->top = l->keys;
|
|
}
|
|
|
|
static inline void bch_keylist_free(struct keylist *l)
|
|
{
|
|
if (l->keys_p != l->inline_keys)
|
|
kfree(l->keys_p);
|
|
}
|
|
|
|
static inline size_t bch_keylist_nkeys(struct keylist *l)
|
|
{
|
|
return l->top_p - l->keys_p;
|
|
}
|
|
|
|
static inline size_t bch_keylist_bytes(struct keylist *l)
|
|
{
|
|
return bch_keylist_nkeys(l) * sizeof(uint64_t);
|
|
}
|
|
|
|
struct bkey *bch_keylist_pop(struct keylist *);
|
|
void bch_keylist_pop_front(struct keylist *);
|
|
int __bch_keylist_realloc(struct keylist *, unsigned);
|
|
|
|
/* Debug stuff */
|
|
|
|
#ifdef CONFIG_BCACHE_DEBUG
|
|
|
|
int __bch_count_data(struct btree_keys *);
|
|
void __bch_check_keys(struct btree_keys *, const char *, ...);
|
|
void bch_dump_bset(struct btree_keys *, struct bset *, unsigned);
|
|
void bch_dump_bucket(struct btree_keys *);
|
|
|
|
#else
|
|
|
|
static inline int __bch_count_data(struct btree_keys *b) { return -1; }
|
|
static inline void __bch_check_keys(struct btree_keys *b, const char *fmt, ...) {}
|
|
static inline void bch_dump_bucket(struct btree_keys *b) {}
|
|
void bch_dump_bset(struct btree_keys *, struct bset *, unsigned);
|
|
|
|
#endif
|
|
|
|
static inline bool btree_keys_expensive_checks(struct btree_keys *b)
|
|
{
|
|
#ifdef CONFIG_BCACHE_DEBUG
|
|
return *b->expensive_debug_checks;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
static inline int bch_count_data(struct btree_keys *b)
|
|
{
|
|
return btree_keys_expensive_checks(b) ? __bch_count_data(b) : -1;
|
|
}
|
|
|
|
#define bch_check_keys(b, ...) \
|
|
do { \
|
|
if (btree_keys_expensive_checks(b)) \
|
|
__bch_check_keys(b, __VA_ARGS__); \
|
|
} while (0)
|
|
|
|
#endif
|