<commit_before>/*
 * linux/fs/mbcache.c
 * (C) 2001-2002 <NAME>, <<EMAIL>>
 */

/*
 * Filesystem Meta Information Block Cache (mbcache)
 *
 * The mbcache caches blocks of block devices that need to be located
 * by their device/block number, as well as by other criteria (such
 * as the block's contents).
 *
 * There can only be one cache entry in a cache per device and block number.
 * Additional indexes need not be unique in this sense. The number of
 * additional indexes (=other criteria) can be hardwired at compile time
 * or specified at cache create time.
 *
 * Each cache entry is of fixed size. An entry may be `valid' or `invalid'
 * in the cache. A valid entry is in the main hash tables of the cache,
 * and may also be in the lru list. An invalid entry is not in any hashes
 * or lists.
 *
 * A valid cache entry is only in the lru list if no handles refer to it.
 * Invalid cache entries will be freed when the last handle to the cache
 * entry is released. Entries that cannot be freed immediately are put
 * back on the lru list.
 */

/*
 * Lock descriptions and usage:
 *
 * Each hash chain of both the block and index hash tables now contains
 * a built-in lock used to serialize accesses to the hash chain.
 *
 * Accesses to global data structures mb_cache_list and mb_cache_lru_list
 * are serialized via the global spinlock mb_cache_spinlock.
 *
 * Each mb_cache_entry contains a spinlock, e_entry_lock, to serialize
 * accesses to its local data, such as e_used and e_queued.
 *
 * Lock ordering:
 *
 * Each block hash chain's lock has the highest lock order, followed by an
 * index hash chain's lock, mb_cache_bg_lock (used to implement mb_cache_entry's
 * lock), and mb_cach_spinlock, with the lowest order.  While holding
 * either a block or index hash chain lock, a thread can acquire an
 * mc_cache_bg_lock, which in turn can also acquire mb_cache_spinlock.
 *
 * Synchronization:
 *
 * Since both mb_cache_entry_get and mb_cache_entry_find scan the block and
 * index hash chian, it needs to lock the corresponding hash chain.  For each
 * mb_cache_entry within the chain, it needs to lock the mb_cache_entry to
 * prevent either any simultaneous release or free on the entry and also
 * to serialize accesses to either the e_used or e_queued member of the entry.
 *
 * To avoid having a dangling reference to an already freed
 * mb_cache_entry, an mb_cache_entry is only freed when it is not on a
 * block hash chain and also no longer being referenced, both e_used,
 * and e_queued are 0's.  When an mb_cache_entry is explicitly freed it is
 * first removed from a block hash chain.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/hash.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/list_bl.h>
#include <linux/mbcache.h>
#include <linux/init.h>
#include <linux/blockgroup_lock.h>

#ifdef MB_CACHE_DEBUG
# define mb_debug(f...) do { \
		printk(KERN_DEBUG f); \
		printk("\n"); \
	} while (0)
#define mb_assert(c) do { if (!(c)) \
		printk(KERN_ERR "assertion " #c " failed\n"); \
	} while(0)
#else
# define mb_debug(f...) do { } while(0)
# define mb_assert(c) do { } while(0)
#endif
#define mb_error(f...) do { \
		printk(KERN_ERR f); \
		printk("\n"); \
	} while(0)

#define MB_CACHE_WRITER ((unsigned short)~0U >> 1)

#define MB_CACHE_ENTRY_LOCK_BITS	__builtin_log2(NR_BG_LOCKS)
#define	MB_CACHE_ENTRY_LOCK_INDEX(ce)			\
	(hash_long((unsigned long)ce, MB_CACHE_ENTRY_LOCK_BITS))

static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
static struct blockgroup_lock *mb_cache_bg_lock;
static struct kmem_cache *mb_cache_kmem_cache;

MODULE_AUTHOR("<NAME> <<EMAIL>>");<commit_msg>fs/mbcache: replace __builtin_log2() with ilog2()<commit_after>/*
 * linux/fs/mbcache.c
 * (C) 2001-2002 <NAME>, <<EMAIL>>
 */

/*
 * Filesystem Meta Information Block Cache (mbcache)
 *
 * The mbcache caches blocks of block devices that need to be located
 * by their device/block number, as well as by other criteria (such
 * as the block's contents).
 *
 * There can only be one cache entry in a cache per device and block number.
 * Additional indexes need not be unique in this sense. The number of
 * additional indexes (=other criteria) can be hardwired at compile time
 * or specified at cache create time.
 *
 * Each cache entry is of fixed size. An entry may be `valid' or `invalid'
 * in the cache. A valid entry is in the main hash tables of the cache,
 * and may also be in the lru list. An invalid entry is not in any hashes
 * or lists.
 *
 * A valid cache entry is only in the lru list if no handles refer to it.
 * Invalid cache entries will be freed when the last handle to the cache
 * entry is released. Entries that cannot be freed immediately are put
 * back on the lru list.
 */

/*
 * Lock descriptions and usage:
 *
 * Each hash chain of both the block and index hash tables now contains
 * a built-in lock used to serialize accesses to the hash chain.
 *
 * Accesses to global data structures mb_cache_list and mb_cache_lru_list
 * are serialized via the global spinlock mb_cache_spinlock.
 *
 * Each mb_cache_entry contains a spinlock, e_entry_lock, to serialize
 * accesses to its local data, such as e_used and e_queued.
 *
 * Lock ordering:
 *
 * Each block hash chain's lock has the highest lock order, followed by an
 * index hash chain's lock, mb_cache_bg_lock (used to implement mb_cache_entry's
 * lock), and mb_cach_spinlock, with the lowest order.  While holding
 * either a block or index hash chain lock, a thread can acquire an
 * mc_cache_bg_lock, which in turn can also acquire mb_cache_spinlock.
 *
 * Synchronization:
 *
 * Since both mb_cache_entry_get and mb_cache_entry_find scan the block and
 * index hash chian, it needs to lock the corresponding hash chain.  For each
 * mb_cache_entry within the chain, it needs to lock the mb_cache_entry to
 * prevent either any simultaneous release or free on the entry and also
 * to serialize accesses to either the e_used or e_queued member of the entry.
 *
 * To avoid having a dangling reference to an already freed
 * mb_cache_entry, an mb_cache_entry is only freed when it is not on a
 * block hash chain and also no longer being referenced, both e_used,
 * and e_queued are 0's.  When an mb_cache_entry is explicitly freed it is
 * first removed from a block hash chain.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/hash.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/list_bl.h>
#include <linux/mbcache.h>
#include <linux/init.h>
#include <linux/blockgroup_lock.h>
#include <linux/log2.h>

#ifdef MB_CACHE_DEBUG
# define mb_debug(f...) do { \
		printk(KERN_DEBUG f); \
		printk("\n"); \
	} while (0)
#define mb_assert(c) do { if (!(c)) \
		printk(KERN_ERR "assertion " #c " failed\n"); \
	} while(0)
#else
# define mb_debug(f...) do { } while(0)
# define mb_assert(c) do { } while(0)
#endif
#define mb_error(f...) do { \
		printk(KERN_ERR f); \
		printk("\n"); \
	} while(0)

#define MB_CACHE_WRITER ((unsigned short)~0U >> 1)

#define MB_CACHE_ENTRY_LOCK_BITS	ilog2(NR_BG_LOCKS)
#define	MB_CACHE_ENTRY_LOCK_INDEX(ce)			\
	(hash_long((unsigned long)ce, MB_CACHE_ENTRY_LOCK_BITS))

static DECLARE_WAIT_QUEUE_HEAD(mb_cache_queue);
static struct blockgroup_lock *mb_cache_bg_lock;
static struct kmem_cache *mb_cache_kmem_cache;

MODULE_AUTHOR("<NAME> <<EMAIL>>");
{'fraction_non_alphanumeric': 0.08819975835682642, 'fraction_numerical': 0.004967109679151564, 'mean_word_length': 4.08879781420765, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3567434', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2353, 'n_words': 975}