From c0ad33c126300a51721a4f0ec8c0d757647e9cbe Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 19 Mar 2017 21:39:19 -0800 Subject: [PATCH] bcachefs-in-userspace improvements Got rid of the stupid shim file hack --- .bcachefs_revision | 2 +- .gitignore | 1 + Makefile | 40 ++++++++++-- bcachefs-userspace-shim.c | 111 --------------------------------- bcachefs.c | 3 +- cmd_debug.c | 2 - cmd_fsck.c | 7 ++- cmd_migrate.c | 1 - include/linux/kernel.h | 1 + include/linux/sysfs.h | 5 ++ libbcachefs.h | 10 --- libbcachefs/bcachefs.h | 4 +- libbcachefs/btree_cache.c | 1 + libbcachefs/btree_iter.c | 1 + libbcachefs/clock.c | 1 + libbcachefs/error.h | 58 ++++++++++++++--- libbcachefs/fs-gc.c | 1 + libbcachefs/fs.h | 1 + libbcachefs/io.h | 1 + libbcachefs/movinggc.c | 1 + libbcachefs/six.c | 3 + libbcachefs/super.c | 38 +++++++++--- libbcachefs/super.h | 9 --- libbcachefs/sysfs.c | 127 +++++++++++++++++++++++++++++++------- libbcachefs/sysfs.h | 116 ++++++++-------------------------- libbcachefs/util.c | 9 +-- libbcachefs/xattr.c | 1 + linux/kstrtox.c | 2 +- 28 files changed, 278 insertions(+), 279 deletions(-) delete mode 100644 bcachefs-userspace-shim.c diff --git a/.bcachefs_revision b/.bcachefs_revision index e3d90e2..398cb4a 100644 --- a/.bcachefs_revision +++ b/.bcachefs_revision @@ -1 +1 @@ -4fef29d8dad00d8e6192016631c5108e5e537c3c +217509029551f8b40024d7fcd6e311be1fb6410c diff --git a/.gitignore b/.gitignore index ecf040c..7357d55 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ bcachefs *.a tags cscope* +bcachefs-tools diff --git a/Makefile b/Makefile index dd2e669..7e00baa 100644 --- a/Makefile +++ b/Makefile @@ -10,12 +10,9 @@ CFLAGS+=-std=gnu99 -O2 -g -MMD -Wall \ -D_GNU_SOURCE \ -D_LGPL_SOURCE \ -DRCU_MEMBARRIER \ - -DNO_BCACHE_ACCOUNTING \ - -DNO_BCACHE_BLOCKDEV \ -DNO_BCACHE_CHARDEV \ -DNO_BCACHE_FS \ - -DNO_BCACHE_NOTIFY \ - -DNO_BCACHE_WRITEBACK \ + -DNO_BCACHE_SYSFS \ $(EXTRA_CFLAGS) LDFLAGS+=-O2 -g @@ -41,7 +38,6 @@ endif all: bcachefs SRCS=bcachefs.c \ - bcachefs-userspace-shim.c \ cmd_assemble.c \ cmd_debug.c \ cmd_device.c \ @@ -55,6 +51,40 @@ SRCS=bcachefs.c \ libbcachefs.c \ qcow2.c \ tools-util.c \ + libbcachefs/alloc.c \ + libbcachefs/bkey.c \ + libbcachefs/bkey_methods.c \ + libbcachefs/bset.c \ + libbcachefs/btree_cache.c \ + libbcachefs/btree_gc.c \ + libbcachefs/btree_io.c \ + libbcachefs/btree_iter.c \ + libbcachefs/btree_update.c \ + libbcachefs/buckets.c \ + libbcachefs/checksum.c \ + libbcachefs/clock.c \ + libbcachefs/compress.c \ + libbcachefs/debug.c \ + libbcachefs/dirent.c \ + libbcachefs/error.c \ + libbcachefs/extents.c \ + libbcachefs/fs-gc.c \ + libbcachefs/inode.c \ + libbcachefs/io.c \ + libbcachefs/journal.c \ + libbcachefs/keylist.c \ + libbcachefs/migrate.c \ + libbcachefs/move.c \ + libbcachefs/movinggc.c \ + libbcachefs/opts.c \ + libbcachefs/siphash.c \ + libbcachefs/six.c \ + libbcachefs/super.c \ + libbcachefs/super-io.c \ + libbcachefs/tier.c \ + libbcachefs/trace.c \ + libbcachefs/util.c \ + libbcachefs/xattr.c \ $(wildcard linux/*.c linux/*/*.c) \ $(wildcard ccan/*/*.c) diff --git a/bcachefs-userspace-shim.c b/bcachefs-userspace-shim.c deleted file mode 100644 index 9fd62cd..0000000 --- a/bcachefs-userspace-shim.c +++ /dev/null @@ -1,111 +0,0 @@ - -#include -#include - -#define bch2_fmt(_c, fmt) fmt "\n" - -#include "libbcachefs.h" -#include "tools-util.h" - -enum fsck_err_opts fsck_err_opt; - -/* Returns true if error should be fixed: */ - -/* XXX: flag if we ignore errors */ - -/* - * If it's an error that we can't ignore, and we're running non - * interactively - return true and have the error fixed so that we don't have to - * bail out and stop the fsck early, so that the user can see all the errors - * present: - */ -#define __fsck_err(c, _can_fix, _can_ignore, _nofix_msg, msg, ...) \ -({ \ - bool _fix = false; \ - \ - if (_can_fix) { \ - switch (fsck_err_opt) { \ - case FSCK_ERR_ASK: \ - printf(msg ": fix?", ##__VA_ARGS__); \ - _fix = ask_yn(); \ - \ - break; \ - case FSCK_ERR_YES: \ - bch_err(c, msg ", fixing", ##__VA_ARGS__); \ - _fix = true; \ - break; \ - case FSCK_ERR_NO: \ - bch_err(c, msg, ##__VA_ARGS__); \ - _fix = false; \ - break; \ - } \ - } else if (_can_ignore) { \ - bch_err(c, msg, ##__VA_ARGS__); \ - } \ - \ - if (_can_fix && !_can_ignore && fsck_err_opt == FSCK_ERR_NO) \ - _fix = true; \ - \ - if (!_fix && !_can_ignore) { \ - printf("Fatal filesystem inconsistency, halting\n"); \ - ret = BCH_FSCK_ERRORS_NOT_FIXED; \ - goto fsck_err; \ - } \ - \ - _fix; \ -}) - -//#include "acl.c" -#include "alloc.c" -#include "bkey.c" -#include "bkey_methods.c" -#include "bset.c" -#include "btree_cache.c" -#include "btree_gc.c" -#include "btree_io.c" -#include "btree_iter.c" -#include "btree_update.c" -#include "buckets.c" -//#include "chardev.c" -#include "checksum.c" -#include "clock.c" -#include "compress.c" -#include "debug.c" -#include "dirent.c" -#include "error.c" -#include "extents.c" -//#include "fs.c" -#include "fs-gc.c" -//#include "fs-io.c" -#include "inode.c" -#include "io.c" -#include "journal.c" -#include "keylist.c" -#include "migrate.c" -#include "move.c" -#include "movinggc.c" -#include "opts.c" -#include "siphash.c" -#include "six.c" -#include "super.c" -#include "super-io.c" -//#include "sysfs.c" -#include "tier.c" -#include "trace.c" -#include "util.c" -#include "xattr.c" - -#define SHIM_KTYPE(type) \ -struct kobj_type type ## _ktype = { .release = type ## _release, } - -static void bch2_fs_internal_release(struct kobject *k) {} - -static void bch2_fs_opts_dir_release(struct kobject *k) {} - -static void bch2_fs_time_stats_release(struct kobject *k) {} - -SHIM_KTYPE(bch2_dev); -SHIM_KTYPE(bch2_fs); -SHIM_KTYPE(bch2_fs_internal); -SHIM_KTYPE(bch2_fs_time_stats); -SHIM_KTYPE(bch2_fs_opts_dir); diff --git a/bcachefs.c b/bcachefs.c index 945d6df..9b6b057 100644 --- a/bcachefs.c +++ b/bcachefs.c @@ -160,6 +160,7 @@ int main(int argc, char *argv[]) if (!strcmp(cmd, "list")) return cmd_list(argc, argv); + printf("Unknown command %s\n", cmd); usage(); - return 0; + exit(EXIT_FAILURE); } diff --git a/cmd_debug.c b/cmd_debug.c index a8c534b..02f4b49 100644 --- a/cmd_debug.c +++ b/cmd_debug.c @@ -95,7 +95,6 @@ int cmd_dump(int argc, char *argv[]) opts.nochanges = true; opts.noreplay = true; opts.errors = BCH_ON_ERROR_CONTINUE; - fsck_err_opt = FSCK_ERR_NO; while ((opt = getopt(argc, argv, "o:fh")) != -1) switch (opt) { @@ -235,7 +234,6 @@ int cmd_list(int argc, char *argv[]) opts.nochanges = true; opts.norecovery = true; opts.errors = BCH_ON_ERROR_CONTINUE; - fsck_err_opt = FSCK_ERR_NO; while ((opt = getopt(argc, argv, "b:s:e:m:h")) != -1) switch (opt) { diff --git a/cmd_fsck.c b/cmd_fsck.c index c8a8df6..1775067 100644 --- a/cmd_fsck.c +++ b/cmd_fsck.c @@ -1,5 +1,6 @@ #include "cmds.h" +#include "error.h" #include "libbcachefs.h" #include "super.h" #include "tools-util.h" @@ -29,14 +30,14 @@ int cmd_fsck(int argc, char *argv[]) while ((opt = getopt(argc, argv, "pynfvh")) != -1) switch (opt) { case 'p': - fsck_err_opt = FSCK_ERR_YES; + opts.fix_errors = FSCK_ERR_YES; break; case 'y': - fsck_err_opt = FSCK_ERR_YES; + opts.fix_errors = FSCK_ERR_YES; break; case 'n': opts.nochanges = true; - fsck_err_opt = FSCK_ERR_NO; + opts.fix_errors = FSCK_ERR_NO; break; case 'f': /* force check, even if filesystem marked clean: */ diff --git a/cmd_migrate.c b/cmd_migrate.c index 62ef04c..2810dc2 100644 --- a/cmd_migrate.c +++ b/cmd_migrate.c @@ -760,7 +760,6 @@ int cmd_migrate(int argc, char *argv[]) printf("Migrate complete, running fsck:\n"); opts.nostart = false; opts.nochanges = true; - fsck_err_opt = FSCK_ERR_NO; err = bch2_fs_open(path, 1, opts, &c); if (err) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 741e0ba..dc778f1 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -2,6 +2,7 @@ #define __TOOLS_LINUX_KERNEL_H #include +#include #include #include #include diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 0be85b7..dde1692 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -17,6 +17,11 @@ struct attribute { .store = _store, \ } +struct sysfs_ops { + ssize_t (*show)(struct kobject *, struct attribute *, char *); + ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); +}; + static inline int sysfs_create_files(struct kobject *kobj, const struct attribute **attr) { diff --git a/libbcachefs.h b/libbcachefs.h index f9cf8fe..2e677ae 100644 --- a/libbcachefs.h +++ b/libbcachefs.h @@ -8,16 +8,6 @@ #include "tools-util.h" #include "vstructs.h" -struct cache_sb; - -enum fsck_err_opts { - FSCK_ERR_ASK, - FSCK_ERR_YES, - FSCK_ERR_NO, -}; - -extern enum fsck_err_opts fsck_err_opt; - struct format_opts { char *label; uuid_le uuid; diff --git a/libbcachefs/bcachefs.h b/libbcachefs/bcachefs.h index 6e08947..9bc6210 100644 --- a/libbcachefs/bcachefs.h +++ b/libbcachefs/bcachefs.h @@ -210,8 +210,10 @@ #define bch2_meta_write_fault(name) \ dynamic_fault("bcachefs:meta:write:" name) -#ifndef bch2_fmt +#ifdef __KERNEL__ #define bch2_fmt(_c, fmt) "bcachefs (%s): " fmt "\n", ((_c)->name) +#else +#define bch2_fmt(_c, fmt) fmt "\n" #endif #define bch_info(c, fmt, ...) \ diff --git a/libbcachefs/btree_cache.c b/libbcachefs/btree_cache.c index c4cc26f..e98d9c1 100644 --- a/libbcachefs/btree_cache.c +++ b/libbcachefs/btree_cache.c @@ -7,6 +7,7 @@ #include "debug.h" #include "extents.h" +#include #include #define DEF_BTREE_ID(kwd, val, name) name, diff --git a/libbcachefs/btree_iter.c b/libbcachefs/btree_iter.c index fb5c507..55303f0 100644 --- a/libbcachefs/btree_iter.c +++ b/libbcachefs/btree_iter.c @@ -7,6 +7,7 @@ #include "debug.h" #include "extents.h" +#include #include #define BTREE_ITER_NOT_END ((struct btree *) 1) diff --git a/libbcachefs/clock.c b/libbcachefs/clock.c index 3c3649f..68ac62b 100644 --- a/libbcachefs/clock.c +++ b/libbcachefs/clock.c @@ -3,6 +3,7 @@ #include #include +#include static inline bool io_timer_cmp(struct io_timer *l, struct io_timer *r) { diff --git a/libbcachefs/error.h b/libbcachefs/error.h index 83d3a62..5f81c34 100644 --- a/libbcachefs/error.h +++ b/libbcachefs/error.h @@ -5,6 +5,7 @@ struct bch_dev; struct bch_fs; +struct work_struct; /* * XXX: separate out errors that indicate on disk data is inconsistent, and flag @@ -98,28 +99,65 @@ enum { /* XXX: mark in superblock that filesystem contains errors, if we ignore: */ -#ifndef __fsck_err -#define __fsck_err(c, _can_fix, _can_ignore, _nofix_msg, msg, ...) \ +enum fsck_err_opts { + FSCK_ERR_NO, + FSCK_ERR_YES, + FSCK_ERR_ASK, +}; + +#ifdef __KERNEL__ +#define __fsck_err_should_fix(c, msg, ...) \ +({ \ + bool _fix = (c)->opts.fix_errors; \ + bch_err(c, msg ", %sfixing", ##__VA_ARGS__, _fix ? "" : "not ");\ + _fix; \ +}) +#else +#include "tools-util.h" + +#define __fsck_err_should_fix(c, msg, ...) \ ({ \ bool _fix = false; \ - \ - if (_can_fix && (c)->opts.fix_errors) { \ + switch ((c)->opts.fix_errors) { \ + case FSCK_ERR_ASK: \ + printf(msg ": fix?", ##__VA_ARGS__); \ + _fix = ask_yn(); \ + break; \ + case FSCK_ERR_YES: \ bch_err(c, msg ", fixing", ##__VA_ARGS__); \ - set_bit(BCH_FS_FSCK_FIXED_ERRORS, &(c)->flags); \ _fix = true; \ - } else if (_can_ignore && \ - (c)->opts.errors == BCH_ON_ERROR_CONTINUE) { \ - bch_err(c, msg " (ignoring)", ##__VA_ARGS__); \ + break; \ + case FSCK_ERR_NO: \ + bch_err(c, msg, ##__VA_ARGS__); \ + _fix = false; \ + break; \ + } \ + _fix; \ +}) +#endif + +#define __fsck_err(c, _can_fix, _can_ignore, _nofix_msg, msg, ...) \ +({ \ + bool _fix; \ + \ + if (_can_fix) { \ + _fix = __fsck_err_should_fix(c, msg, ##__VA_ARGS__); \ } else { \ bch_err(c, msg " ("_nofix_msg")", ##__VA_ARGS__); \ + _fix = false; \ + } \ + \ + if (_fix) \ + set_bit(BCH_FS_FSCK_FIXED_ERRORS, &(c)->flags); \ + \ + if (!_fix && !_can_ignore) { \ + bch_err(c, "Unable to continue, halting"); \ ret = BCH_FSCK_ERRORS_NOT_FIXED; \ goto fsck_err; \ } \ \ - BUG_ON(!_fix && !_can_ignore); \ _fix; \ }) -#endif #define __fsck_err_on(cond, c, _can_fix, _can_ignore, _nofix_msg, ...) \ ((cond) ? __fsck_err(c, _can_fix, _can_ignore, \ diff --git a/libbcachefs/fs-gc.c b/libbcachefs/fs-gc.c index 20f552d..1a03b7e 100644 --- a/libbcachefs/fs-gc.c +++ b/libbcachefs/fs-gc.c @@ -9,6 +9,7 @@ #include "keylist.h" #include "super.h" +#include /* struct qstr */ #include #define QSTR(n) { { { .len = strlen(n) } }, .name = n } diff --git a/libbcachefs/fs.h b/libbcachefs/fs.h index f7cad29..38a349d 100644 --- a/libbcachefs/fs.h +++ b/libbcachefs/fs.h @@ -4,6 +4,7 @@ #include "str_hash.h" #include +#include struct bch_inode_info { struct inode vfs_inode; diff --git a/libbcachefs/io.h b/libbcachefs/io.h index 253316a..fb6f300 100644 --- a/libbcachefs/io.h +++ b/libbcachefs/io.h @@ -1,6 +1,7 @@ #ifndef _BCACHE_IO_H #define _BCACHE_IO_H +#include #include "io_types.h" #define to_wbio(_bio) \ diff --git a/libbcachefs/movinggc.c b/libbcachefs/movinggc.c index 8804dbb..cc7d3f6 100644 --- a/libbcachefs/movinggc.c +++ b/libbcachefs/movinggc.c @@ -17,6 +17,7 @@ #include #include #include +#include #include /* Moving GC - IO loop */ diff --git a/libbcachefs/six.c b/libbcachefs/six.c index 1bb8bfc..3283785 100644 --- a/libbcachefs/six.c +++ b/libbcachefs/six.c @@ -1,4 +1,7 @@ +#include +#include +#include #include #include diff --git a/libbcachefs/super.c b/libbcachefs/super.c index 8aa5cc0..1977bdc 100644 --- a/libbcachefs/super.c +++ b/libbcachefs/super.c @@ -29,6 +29,7 @@ #include "movinggc.h" #include "super.h" #include "super-io.h" +#include "sysfs.h" #include "tier.h" #include @@ -49,12 +50,33 @@ MODULE_LICENSE("GPL"); MODULE_AUTHOR("Kent Overstreet "); -static const uuid_le invalid_uuid = { - .b = { - 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78, - 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99 - } -}; +#define KTYPE(type) \ +struct kobj_type type ## _ktype = { \ + .release = type ## _release, \ + .sysfs_ops = &type ## _sysfs_ops, \ + .default_attrs = type ## _files \ +} + +static void bch2_fs_release(struct kobject *); +static void bch2_dev_release(struct kobject *); + +static void bch2_fs_internal_release(struct kobject *k) +{ +} + +static void bch2_fs_opts_dir_release(struct kobject *k) +{ +} + +static void bch2_fs_time_stats_release(struct kobject *k) +{ +} + +static KTYPE(bch2_fs); +static KTYPE(bch2_fs_internal); +static KTYPE(bch2_fs_opts_dir); +static KTYPE(bch2_fs_time_stats); +static KTYPE(bch2_dev); static struct kset *bcachefs_kset; static LIST_HEAD(bch_fs_list); @@ -417,7 +439,7 @@ static void bch2_fs_offline(struct bch_fs *c) __bch2_fs_read_only(c); } -void bch2_fs_release(struct kobject *kobj) +static void bch2_fs_release(struct kobject *kobj) { struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); @@ -913,7 +935,7 @@ static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) /* Device startup/shutdown: */ -void bch2_dev_release(struct kobject *kobj) +static void bch2_dev_release(struct kobject *kobj) { struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); diff --git a/libbcachefs/super.h b/libbcachefs/super.h index 9442441..700344a 100644 --- a/libbcachefs/super.h +++ b/libbcachefs/super.h @@ -93,8 +93,6 @@ struct bch_fs *bch2_bdev_to_fs(struct block_device *); struct bch_fs *bch2_uuid_to_fs(uuid_le); int bch2_congested(struct bch_fs *, int); -void bch2_dev_release(struct kobject *); - bool bch2_dev_state_allowed(struct bch_fs *, struct bch_dev *, enum bch_member_state, int); int __bch2_dev_set_state(struct bch_fs *, struct bch_dev *, @@ -113,7 +111,6 @@ bool bch2_fs_emergency_read_only(struct bch_fs *); void bch2_fs_read_only(struct bch_fs *); const char *bch2_fs_read_write(struct bch_fs *); -void bch2_fs_release(struct kobject *); void bch2_fs_stop(struct bch_fs *); const char *bch2_fs_start(struct bch_fs *); @@ -121,10 +118,4 @@ const char *bch2_fs_open(char * const *, unsigned, struct bch_opts, struct bch_fs **); const char *bch2_fs_open_incremental(const char *path); -extern struct kobj_type bch2_fs_ktype; -extern struct kobj_type bch2_fs_internal_ktype; -extern struct kobj_type bch2_fs_time_stats_ktype; -extern struct kobj_type bch2_fs_opts_dir_ktype; -extern struct kobj_type bch2_dev_ktype; - #endif /* _BCACHE_SUPER_H */ diff --git a/libbcachefs/sysfs.c b/libbcachefs/sysfs.c index 11c6cdc..a0b9fae 100644 --- a/libbcachefs/sysfs.c +++ b/libbcachefs/sysfs.c @@ -25,6 +25,101 @@ #include #include +#include "util.h" + +#define SYSFS_OPS(type) \ +struct sysfs_ops type ## _sysfs_ops = { \ + .show = type ## _show, \ + .store = type ## _store \ +} + +#define SHOW(fn) \ +static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\ + char *buf) \ + +#define STORE(fn) \ +static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\ + const char *buf, size_t size) \ + +#define __sysfs_attribute(_name, _mode) \ + static struct attribute sysfs_##_name = \ + { .name = #_name, .mode = _mode } + +#define write_attribute(n) __sysfs_attribute(n, S_IWUSR) +#define read_attribute(n) __sysfs_attribute(n, S_IRUGO) +#define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR) + +#define sysfs_printf(file, fmt, ...) \ +do { \ + if (attr == &sysfs_ ## file) \ + return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \ +} while (0) + +#define sysfs_print(file, var) \ +do { \ + if (attr == &sysfs_ ## file) \ + return snprint(buf, PAGE_SIZE, var); \ +} while (0) + +#define sysfs_hprint(file, val) \ +do { \ + if (attr == &sysfs_ ## file) { \ + ssize_t ret = bch2_hprint(buf, val); \ + strcat(buf, "\n"); \ + return ret + 1; \ + } \ +} while (0) + +#define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var)) +#define var_print(_var) sysfs_print(_var, var(_var)) +#define var_hprint(_var) sysfs_hprint(_var, var(_var)) + +#define sysfs_strtoul(file, var) \ +do { \ + if (attr == &sysfs_ ## file) \ + return strtoul_safe(buf, var) ?: (ssize_t) size; \ +} while (0) + +#define sysfs_strtoul_clamp(file, var, min, max) \ +do { \ + if (attr == &sysfs_ ## file) \ + return strtoul_safe_clamp(buf, var, min, max) \ + ?: (ssize_t) size; \ +} while (0) + +#define strtoul_or_return(cp) \ +({ \ + unsigned long _v; \ + int _r = kstrtoul(cp, 10, &_v); \ + if (_r) \ + return _r; \ + _v; \ +}) + +#define strtoul_restrict_or_return(cp, min, max) \ +({ \ + unsigned long __v = 0; \ + int _r = strtoul_safe_restrict(cp, __v, min, max); \ + if (_r) \ + return _r; \ + __v; \ +}) + +#define strtoi_h_or_return(cp) \ +({ \ + u64 _v; \ + int _r = strtoi_h(cp, &_v); \ + if (_r) \ + return _r; \ + _v; \ +}) + +#define sysfs_hatoi(file, var) \ +do { \ + if (attr == &sysfs_ ## file) \ + return strtoi_h(buf, &var) ?: (ssize_t) size; \ +} while (0) + write_attribute(trigger_btree_coalesce); write_attribute(trigger_gc); write_attribute(prune_cache); @@ -461,8 +556,9 @@ STORE(bch2_fs) return size; } +SYSFS_OPS(bch2_fs); -static struct attribute *bch2_fs_files[] = { +struct attribute *bch2_fs_files[] = { &sysfs_journal_write_delay_ms, &sysfs_journal_reclaim_delay_ms, &sysfs_journal_entry_size_max, @@ -488,7 +584,6 @@ static struct attribute *bch2_fs_files[] = { &sysfs_journal_flush, NULL }; -KTYPE(bch2_fs); /* internal dir - just a wrapper */ @@ -503,12 +598,9 @@ STORE(bch2_fs_internal) struct bch_fs *c = container_of(kobj, struct bch_fs, internal); return bch2_fs_store(&c->kobj, attr, buf, size); } +SYSFS_OPS(bch2_fs_internal); -static void bch2_fs_internal_release(struct kobject *k) -{ -} - -static struct attribute *bch2_fs_internal_files[] = { +struct attribute *bch2_fs_internal_files[] = { &sysfs_journal_debug, &sysfs_alloc_debug, @@ -537,7 +629,6 @@ static struct attribute *bch2_fs_internal_files[] = { NULL }; -KTYPE(bch2_fs_internal); /* options */ @@ -582,12 +673,9 @@ STORE(bch2_fs_opts_dir) return size; } +SYSFS_OPS(bch2_fs_opts_dir); -static void bch2_fs_opts_dir_release(struct kobject *k) -{ -} - -static struct attribute *bch2_fs_opts_dir_files[] = { +struct attribute *bch2_fs_opts_dir_files[] = { #define BCH_OPT(_name, ...) \ &sysfs_opt_##_name, @@ -596,7 +684,6 @@ static struct attribute *bch2_fs_opts_dir_files[] = { NULL }; -KTYPE(bch2_fs_opts_dir); /* time stats */ @@ -624,12 +711,9 @@ STORE(bch2_fs_time_stats) return size; } +SYSFS_OPS(bch2_fs_time_stats); -static void bch2_fs_time_stats_release(struct kobject *k) -{ -} - -static struct attribute *bch2_fs_time_stats_files[] = { +struct attribute *bch2_fs_time_stats_files[] = { #define BCH_TIME_STAT(name, frequency_units, duration_units) \ sysfs_time_stats_attribute_list(name, frequency_units, duration_units) BCH_TIME_STATS() @@ -637,7 +721,6 @@ static struct attribute *bch2_fs_time_stats_files[] = { NULL }; -KTYPE(bch2_fs_time_stats); typedef unsigned (bucket_map_fn)(struct bch_dev *, struct bucket *, void *); @@ -894,8 +977,9 @@ STORE(bch2_dev) return size; } +SYSFS_OPS(bch2_dev); -static struct attribute *bch2_dev_files[] = { +struct attribute *bch2_dev_files[] = { &sysfs_uuid, &sysfs_bucket_size, &sysfs_bucket_size_bytes, @@ -932,4 +1016,3 @@ static struct attribute *bch2_dev_files[] = { sysfs_pd_controller_files(copy_gc), NULL }; -KTYPE(bch2_dev); diff --git a/libbcachefs/sysfs.h b/libbcachefs/sysfs.h index d1f17cf..c0b8034 100644 --- a/libbcachefs/sysfs.h +++ b/libbcachefs/sysfs.h @@ -1,103 +1,39 @@ #ifndef _BCACHE_SYSFS_H_ #define _BCACHE_SYSFS_H_ -#include "util.h" +#include -#define KTYPE(type) \ -struct kobj_type type ## _ktype = { \ - .release = type ## _release, \ - .sysfs_ops = &((const struct sysfs_ops) { \ - .show = type ## _show, \ - .store = type ## _store \ - }), \ - .default_attrs = type ## _files \ -} +#ifndef NO_BCACHE_SYSFS -#define SHOW(fn) \ -static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\ - char *buf) \ +struct attribute; +struct sysfs_ops; -#define STORE(fn) \ -static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\ - const char *buf, size_t size) \ +extern struct attribute *bch2_fs_files[]; +extern struct attribute *bch2_fs_internal_files[]; +extern struct attribute *bch2_fs_opts_dir_files[]; +extern struct attribute *bch2_fs_time_stats_files[]; +extern struct attribute *bch2_dev_files[]; -#define __sysfs_attribute(_name, _mode) \ - static struct attribute sysfs_##_name = \ - { .name = #_name, .mode = _mode } +extern struct sysfs_ops bch2_fs_sysfs_ops; +extern struct sysfs_ops bch2_fs_internal_sysfs_ops; +extern struct sysfs_ops bch2_fs_opts_dir_sysfs_ops; +extern struct sysfs_ops bch2_fs_time_stats_sysfs_ops; +extern struct sysfs_ops bch2_dev_sysfs_ops; -#define write_attribute(n) __sysfs_attribute(n, S_IWUSR) -#define read_attribute(n) __sysfs_attribute(n, S_IRUGO) -#define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR) +#else -#define sysfs_printf(file, fmt, ...) \ -do { \ - if (attr == &sysfs_ ## file) \ - return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \ -} while (0) +static struct attribute *bch2_fs_files[] = {}; +static struct attribute *bch2_fs_internal_files[] = {}; +static struct attribute *bch2_fs_opts_dir_files[] = {}; +static struct attribute *bch2_fs_time_stats_files[] = {}; +static struct attribute *bch2_dev_files[] = {}; -#define sysfs_print(file, var) \ -do { \ - if (attr == &sysfs_ ## file) \ - return snprint(buf, PAGE_SIZE, var); \ -} while (0) +static const struct sysfs_ops bch2_fs_sysfs_ops; +static const struct sysfs_ops bch2_fs_internal_sysfs_ops; +static const struct sysfs_ops bch2_fs_opts_dir_sysfs_ops; +static const struct sysfs_ops bch2_fs_time_stats_sysfs_ops; +static const struct sysfs_ops bch2_dev_sysfs_ops; -#define sysfs_hprint(file, val) \ -do { \ - if (attr == &sysfs_ ## file) { \ - ssize_t ret = bch2_hprint(buf, val); \ - strcat(buf, "\n"); \ - return ret + 1; \ - } \ -} while (0) - -#define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var)) -#define var_print(_var) sysfs_print(_var, var(_var)) -#define var_hprint(_var) sysfs_hprint(_var, var(_var)) - -#define sysfs_strtoul(file, var) \ -do { \ - if (attr == &sysfs_ ## file) \ - return strtoul_safe(buf, var) ?: (ssize_t) size; \ -} while (0) - -#define sysfs_strtoul_clamp(file, var, min, max) \ -do { \ - if (attr == &sysfs_ ## file) \ - return strtoul_safe_clamp(buf, var, min, max) \ - ?: (ssize_t) size; \ -} while (0) - -#define strtoul_or_return(cp) \ -({ \ - unsigned long _v; \ - int _r = kstrtoul(cp, 10, &_v); \ - if (_r) \ - return _r; \ - _v; \ -}) - -#define strtoul_restrict_or_return(cp, min, max) \ -({ \ - unsigned long __v = 0; \ - int _r = strtoul_safe_restrict(cp, __v, min, max); \ - if (_r) \ - return _r; \ - __v; \ -}) - -#define strtoi_h_or_return(cp) \ -({ \ - u64 _v; \ - int _r = strtoi_h(cp, &_v); \ - if (_r) \ - return _r; \ - _v; \ -}) - -#define sysfs_hatoi(file, var) \ -do { \ - if (attr == &sysfs_ ## file) \ - return strtoi_h(buf, &var) ?: (ssize_t) size; \ -} while (0) +#endif #endif /* _BCACHE_SYSFS_H_ */ diff --git a/libbcachefs/util.c b/libbcachefs/util.c index e4cd631..f2e6ec4 100644 --- a/libbcachefs/util.c +++ b/libbcachefs/util.c @@ -9,14 +9,15 @@ #include #include #include -#include +#include +#include +#include +#include #include #include +#include #include -#include -#include - #include "util.h" #define simple_strtoint(c, end, base) simple_strtol(c, end, base) diff --git a/libbcachefs/xattr.c b/libbcachefs/xattr.c index 62a0889..488d536 100644 --- a/libbcachefs/xattr.c +++ b/libbcachefs/xattr.c @@ -7,6 +7,7 @@ #include "str_hash.h" #include "xattr.h" +#include #include #include diff --git a/linux/kstrtox.c b/linux/kstrtox.c index af6b222..1937265 100644 --- a/linux/kstrtox.c +++ b/linux/kstrtox.c @@ -11,8 +11,8 @@ * * If -E is returned, result is not touched. */ +#include #include -#include #include #include #include -- 2.39.2