From 99e384733f651b379ea3fffb5a79368fbf8d01c0 Mon Sep 17 00:00:00 2001 From: nyyu Date: Sat, 19 Oct 2024 21:26:41 +0200 Subject: [PATCH] feat: bcachefs init --- Makefile | 12 ++++ PKGBUILD | 42 ++++++++++++ compat.h | 151 ++++++++++++++++++++++++++++++++++++++++++ dkms.conf | 6 ++ fix-kernel-6.11.patch | 70 ++++++++++++++++++++ 5 files changed, 281 insertions(+) create mode 100644 Makefile create mode 100644 PKGBUILD create mode 100644 compat.h create mode 100644 dkms.conf create mode 100644 fix-kernel-6.11.patch diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..83fde0d --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +ifneq (${KERNELRELEASE},) +ccflags-y := -I$(src)/include +obj-m += src/fs/bcachefs/ +else +KDIR ?= /lib/modules/`uname -r`/build + +default: + $(MAKE) -C $(KDIR) M=$$PWD modules + +clean: + $(MAKE) -C $(KDIR) M=$$PWD clean +endif diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..527dc3b --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,42 @@ +pkgname=bcachefs +pkgver=20241025 +pkgrel=1 +arch=('x86_64') +depends=('dkms') +license=('GPL2') +options=('!strip') +provides=("BCACHEFS-MODULE=$pkgver") +source=('dkms.conf' 'Makefile' 'compat.h' 'fix-kernel-6.11.patch') +sha256sums=('a5e55374ea5966e8304b6436dc1512588a898974b3b0f9acb189342323f826cf' + 'd7fa983a3a03a68974ffbc5c96a0c9c7978e2b4001c2305896ba8085c772aa43' + '1f3cdd4f85b0a47c80e930c238840de9f6505c1f8c3e3d1ae6333bebb6ebaa73' + 'eaed621c99e1af0bba60f4530c4e2d9e29505648b8ff4d23a5d02e9fea90c220') + +pkgver() { + cd bcachefs + git --no-pager log -1 --pretty='format:%cd' --date='format:%Y%m%d' +} + +prepare() { + sed -i -e "s/@PACKAGE_VERSION@/$pkgver/g" "$srcdir/dkms.conf" + + rm -rf bcachefs + git clone -n --depth=1 --filter=tree:0 https://github.com/koverstreet/bcachefs.git -b bcachefs-for-upstream + cd bcachefs + git sparse-checkout set --no-cone fs/bcachefs + git checkout + git apply "$srcdir"/*.patch + for i in fs/bcachefs/{fs.c,rcu_pending.c,six.c,journal.c}; do + sed -i '/#include[^\n]*/,$!b;//{x;//p;g};//!H;$!d;x;s//&\n#include "compat.h"/' $i + done + cp ../compat.h "$srcdir"/bcachefs/fs/bcachefs/ +} + +package() { + install -Dm644 "$srcdir/Makefile" "$pkgdir/usr/src/$pkgname-$pkgver/Makefile" + install -Dm644 "$srcdir/dkms.conf" "$pkgdir/usr/src/$pkgname-$pkgver/dkms.conf" + install -d -Dm755 "$pkgdir/usr/src/$pkgname-$pkgver/src/fs/bcachefs" + install -m644 -t "$pkgdir/usr/src/$pkgname-$pkgver/src/fs/bcachefs" "$srcdir/$pkgname/fs/bcachefs"/* + sed -i 's/$(CONFIG_BCACHEFS_FS)/m/g' "$pkgdir/usr/src/$pkgname-$pkgver/src/fs/bcachefs/Makefile" + sed -i "s%^#define TRACE_INCLUDE_PATH \\.\\./\\.\\./fs/bcachefs\$%#define TRACE_INCLUDE_PATH /usr/src/$pkgname-$pkgver/src/fs/bcachefs%" "$pkgdir/usr/src/$pkgname-$pkgver/src/fs/bcachefs/trace.h" +} diff --git a/compat.h b/compat.h new file mode 100644 index 0000000..614ebf6 --- /dev/null +++ b/compat.h @@ -0,0 +1,151 @@ +#include + +#define inode_state_wait_address(inode, bit) ((char *)&(inode)->i_state + (bit)) + + +static inline void inode_wake_up_bit(struct inode *inode, u32 bit) +{ + /* Caller is responsible for correct memory barriers. */ + wake_up_var(inode_state_wait_address(inode, bit)); +} + +static inline struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *wqe, + struct inode *inode, u32 bit) +{ + void *bit_address; + + bit_address = inode_state_wait_address(inode, bit); + init_wait_var_entry(wqe, bit_address, 0); + return __var_waitqueue(bit_address); +} + +#define genradix_ptr_alloc_preallocated_inlined(_radix, _idx, _new_node, _gfp)\ + (__genradix_cast(_radix) \ + (__genradix_ptr_inlined(&(_radix)->tree, \ + __genradix_idx_to_offset(_radix, _idx)) ?: \ + __genradix_ptr_alloc(&(_radix)->tree, \ + __genradix_idx_to_offset(_radix, _idx), \ + _gfp))) + +#define GENRADIX_NODE_SHIFT 9 +#define GENRADIX_NODE_SIZE (1U << GENRADIX_NODE_SHIFT) + +#define GENRADIX_ARY (GENRADIX_NODE_SIZE / sizeof(struct genradix_node *)) +#define GENRADIX_ARY_SHIFT ilog2(GENRADIX_ARY) + +#define GENRADIX_MAX_DEPTH \ + DIV_ROUND_UP(BITS_PER_LONG - GENRADIX_NODE_SHIFT, GENRADIX_ARY_SHIFT) + +#define GENRADIX_DEPTH_MASK \ + ((unsigned long) (roundup_pow_of_two(GENRADIX_MAX_DEPTH + 1) - 1)) + +struct genradix_root; + +static inline struct genradix_node *genradix_alloc_node(gfp_t gfp_mask) +{ + return kzalloc(GENRADIX_NODE_SIZE, gfp_mask); +} + +static inline void genradix_free_node(struct genradix_node *node) +{ + kfree(node); +} + +static inline int genradix_depth_shift(unsigned depth) +{ + return GENRADIX_NODE_SHIFT + GENRADIX_ARY_SHIFT * depth; +} + +static inline struct genradix_node *genradix_root_to_node(struct genradix_root *r) +{ + return (void *) ((unsigned long) r & ~GENRADIX_DEPTH_MASK); +} + + +static inline unsigned genradix_root_to_depth(struct genradix_root *r) +{ + return (unsigned long) r & GENRADIX_DEPTH_MASK; +} + +struct genradix_node { + union { + /* Interior node: */ + struct genradix_node *children[GENRADIX_ARY]; + + /* Leaf: */ + u8 data[GENRADIX_NODE_SIZE]; + }; +}; + +static inline void *__genradix_ptr_inlined(struct __genradix *radix, size_t offset) +{ + struct genradix_root *r = READ_ONCE(radix->root); + struct genradix_node *n = genradix_root_to_node(r); + unsigned level = genradix_root_to_depth(r); + unsigned shift = genradix_depth_shift(level); + + if (unlikely(ilog2(offset) >= genradix_depth_shift(level))) + return NULL; + + while (n && shift > GENRADIX_NODE_SHIFT) { + shift -= GENRADIX_ARY_SHIFT; + n = n->children[offset >> shift]; + offset &= (1UL << shift) - 1; + } + + return n ? &n->data[offset] : NULL; +} + +static inline bool rt_or_dl_prio(int prio) +{ + return unlikely(prio < MAX_RT_PRIO); +} + +static inline bool rt_or_dl_task(struct task_struct *p) +{ + return rt_or_dl_prio(p->prio); +} + +#ifdef _BCACHEFS_FS_H +inline void __iget(struct inode *inode) +{ + atomic_inc(&inode->i_count); +} +#endif + + +#define __closure_wait_event_timeout(waitlist, _cond, _until) \ +({ \ + struct closure cl; \ + long _t; \ + \ + closure_init_stack(&cl); \ + \ + while (1) { \ + closure_wait(waitlist, &cl); \ + if (_cond) { \ + _t = max_t(long, 1L, _until - jiffies); \ + break; \ + } \ + _t = max_t(long, 0L, _until - jiffies); \ + if (!_t) \ + break; \ + closure_sync_timeout(&cl, _t); \ + } \ + closure_wake_up(waitlist); \ + closure_sync(&cl); \ + _t; \ +}) + +/* + * Returns 0 if timeout expired, remaining time in jiffies (at least 1) if + * condition became true + */ +#define closure_wait_event_timeout(waitlist, _cond, _timeout) \ +({ \ + unsigned long _until = jiffies + _timeout; \ + (_cond) \ + ? max_t(long, 1L, _until - jiffies) \ + : __closure_wait_event_timeout(waitlist, _cond, _until);\ +}) + diff --git a/dkms.conf b/dkms.conf new file mode 100644 index 0000000..0de2c99 --- /dev/null +++ b/dkms.conf @@ -0,0 +1,6 @@ +PACKAGE_NAME="bcachefs" +PACKAGE_VERSION="@PACKAGE_VERSION@" +BUILT_MODULE_NAME[0]="bcachefs" +BUILT_MODULE_LOCATION[0]="src/fs/bcachefs" +DEST_MODULE_LOCATION[0]="/kernel/fs/bcachefs" +AUTOINSTALL="yes" diff --git a/fix-kernel-6.11.patch b/fix-kernel-6.11.patch new file mode 100644 index 0000000..a3a8b70 --- /dev/null +++ b/fix-kernel-6.11.patch @@ -0,0 +1,70 @@ +From ed055b79a4503094656f19455a847fc9ae2fa799 Mon Sep 17 00:00:00 2001 +From: nyyu +Date: Sun, 6 Oct 2024 18:15:55 +0200 +Subject: [PATCH] fix: kernel 6.11 + +--- + fs/bcachefs/fs-io-buffered.c | 8 +++++--- + fs/bcachefs/fs-io-buffered.h | 4 ++-- + 2 files changed, 7 insertions(+), 5 deletions(-) + +diff --git a/fs/bcachefs/fs-io-buffered.c b/fs/bcachefs/fs-io-buffered.c +index 48a1ab9..2f96017 100644 +--- a/fs/bcachefs/fs-io-buffered.c ++++ b/fs/bcachefs/fs-io-buffered.c +@@ -648,7 +648,7 @@ int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc + + int bch2_write_begin(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, +- struct folio **foliop, void **fsdata) ++ struct page **pagep, void **fsdata) + { + struct bch_inode_info *inode = to_bch_ei(mapping->host); + struct bch_fs *c = inode->v.i_sb->s_fs_info; +@@ -717,11 +717,12 @@ out: + goto err; + } + +- *foliop = folio; ++ *pagep = &folio->page; + return 0; + err: + folio_unlock(folio); + folio_put(folio); ++ *pagep = NULL; + err_unlock: + bch2_pagecache_add_put(inode); + kfree(res); +@@ -731,11 +732,12 @@ err_unlock: + + int bch2_write_end(struct file *file, struct address_space *mapping, + loff_t pos, unsigned len, unsigned copied, +- struct folio *folio, void *fsdata) ++ struct page *page, void *fsdata) + { + struct bch_inode_info *inode = to_bch_ei(mapping->host); + struct bch_fs *c = inode->v.i_sb->s_fs_info; + struct bch2_folio_reservation *res = fsdata; ++ struct folio *folio = page_folio(page); + unsigned offset = pos - folio_pos(folio); + + lockdep_assert_held(&inode->v.i_rwsem); +diff --git a/fs/bcachefs/fs-io-buffered.h b/fs/bcachefs/fs-io-buffered.h +index 3207ebb..40ce76c 100644 +--- a/fs/bcachefs/fs-io-buffered.h ++++ b/fs/bcachefs/fs-io-buffered.h +@@ -11,9 +11,9 @@ int bch2_writepages(struct address_space *, struct writeback_control *); + void bch2_readahead(struct readahead_control *); + + int bch2_write_begin(struct file *, struct address_space *, loff_t pos, +- unsigned len, struct folio **, void **); ++ unsigned len, struct page **, void **); + int bch2_write_end(struct file *, struct address_space *, loff_t, +- unsigned len, unsigned copied, struct folio *, void *); ++ unsigned len, unsigned copied, struct page *, void *); + + ssize_t bch2_write_iter(struct kiocb *, struct iov_iter *); + +-- +2.46.2 +