/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _BCACHEFS_IOCTL_H2#define _BCACHEFS_IOCTL_H34#include <linux/uuid.h>5#include <asm/ioctl.h>6#include "bcachefs_format.h"7#include "bkey_types.h"89/*10* Flags common to multiple ioctls:11*/12#define BCH_FORCE_IF_DATA_LOST (1 << 0)13#define BCH_FORCE_IF_METADATA_LOST (1 << 1)14#define BCH_FORCE_IF_DATA_DEGRADED (1 << 2)15#define BCH_FORCE_IF_METADATA_DEGRADED (1 << 3)1617#define BCH_FORCE_IF_LOST \18(BCH_FORCE_IF_DATA_LOST| \19BCH_FORCE_IF_METADATA_LOST)20#define BCH_FORCE_IF_DEGRADED \21(BCH_FORCE_IF_DATA_DEGRADED| \22BCH_FORCE_IF_METADATA_DEGRADED)2324/*25* If cleared, ioctl that refer to a device pass it as a pointer to a pathname26* (e.g. /dev/sda1); if set, the dev field is the device's index within the27* filesystem:28*/29#define BCH_BY_INDEX (1 << 4)3031/*32* For BCH_IOCTL_READ_SUPER: get superblock of a specific device, not filesystem33* wide superblock:34*/35#define BCH_READ_DEV (1 << 5)3637/* global control dev: */3839/* These are currently broken, and probably unnecessary: */40#if 041#define BCH_IOCTL_ASSEMBLE _IOW(0xbc, 1, struct bch_ioctl_assemble)42#define BCH_IOCTL_INCREMENTAL _IOW(0xbc, 2, struct bch_ioctl_incremental)4344struct bch_ioctl_assemble {45__u32 flags;46__u32 nr_devs;47__u64 pad;48__u64 devs[];49};5051struct bch_ioctl_incremental {52__u32 flags;53__u64 pad;54__u64 dev;55};56#endif5758/* filesystem ioctls: */5960#define BCH_IOCTL_QUERY_UUID _IOR(0xbc, 1, struct bch_ioctl_query_uuid)6162/* These only make sense when we also have incremental assembly */63#if 064#define BCH_IOCTL_START _IOW(0xbc, 2, struct bch_ioctl_start)65#define BCH_IOCTL_STOP _IO(0xbc, 3)66#endif6768#define BCH_IOCTL_DISK_ADD _IOW(0xbc, 4, struct bch_ioctl_disk)69#define BCH_IOCTL_DISK_REMOVE _IOW(0xbc, 5, struct bch_ioctl_disk)70#define BCH_IOCTL_DISK_ONLINE _IOW(0xbc, 6, struct bch_ioctl_disk)71#define BCH_IOCTL_DISK_OFFLINE _IOW(0xbc, 7, struct bch_ioctl_disk)72#define BCH_IOCTL_DISK_SET_STATE _IOW(0xbc, 8, struct bch_ioctl_disk_set_state)73#define BCH_IOCTL_DATA _IOW(0xbc, 10, struct bch_ioctl_data)74#define BCH_IOCTL_FS_USAGE _IOWR(0xbc, 11, struct bch_ioctl_fs_usage)75#define BCH_IOCTL_DEV_USAGE _IOWR(0xbc, 11, struct bch_ioctl_dev_usage)76#define BCH_IOCTL_READ_SUPER _IOW(0xbc, 12, struct bch_ioctl_read_super)77#define BCH_IOCTL_DISK_GET_IDX _IOW(0xbc, 13, struct bch_ioctl_disk_get_idx)78#define BCH_IOCTL_DISK_RESIZE _IOW(0xbc, 14, struct bch_ioctl_disk_resize)79#define BCH_IOCTL_DISK_RESIZE_JOURNAL _IOW(0xbc,15, struct bch_ioctl_disk_resize_journal)8081#define BCH_IOCTL_SUBVOLUME_CREATE _IOW(0xbc, 16, struct bch_ioctl_subvolume)82#define BCH_IOCTL_SUBVOLUME_DESTROY _IOW(0xbc, 17, struct bch_ioctl_subvolume)8384#define BCH_IOCTL_DEV_USAGE_V2 _IOWR(0xbc, 18, struct bch_ioctl_dev_usage_v2)8586#define BCH_IOCTL_FSCK_OFFLINE _IOW(0xbc, 19, struct bch_ioctl_fsck_offline)87#define BCH_IOCTL_FSCK_ONLINE _IOW(0xbc, 20, struct bch_ioctl_fsck_online)88#define BCH_IOCTL_QUERY_ACCOUNTING _IOW(0xbc, 21, struct bch_ioctl_query_accounting)89#define BCH_IOCTL_QUERY_COUNTERS _IOW(0xbc, 21, struct bch_ioctl_query_counters)9091/* ioctl below act on a particular file, not the filesystem as a whole: */9293#define BCHFS_IOC_REINHERIT_ATTRS _IOR(0xbc, 64, const char __user *)9495/*96* BCH_IOCTL_QUERY_UUID: get filesystem UUID97*98* Returns user visible UUID, not internal UUID (which may not ever be changed);99* the filesystem's sysfs directory may be found under /sys/fs/bcachefs with100* this UUID.101*/102struct bch_ioctl_query_uuid {103__uuid_t uuid;104};105106#if 0107struct bch_ioctl_start {108__u32 flags;109__u32 pad;110};111#endif112113/*114* BCH_IOCTL_DISK_ADD: add a new device to an existing filesystem115*116* The specified device must not be open or in use. On success, the new device117* will be an online member of the filesystem just like any other member.118*119* The device must first be prepared by userspace by formatting with a bcachefs120* superblock, which is only used for passing in superblock options/parameters121* for that device (in struct bch_member). The new device's superblock should122* not claim to be a member of any existing filesystem - UUIDs on it will be123* ignored.124*/125126/*127* BCH_IOCTL_DISK_REMOVE: permanently remove a member device from a filesystem128*129* Any data present on @dev will be permanently deleted, and @dev will be130* removed from its slot in the filesystem's list of member devices. The device131* may be either offline or offline.132*133* Will fail removing @dev would leave us with insufficient read write devices134* or degraded/unavailable data, unless the approprate BCH_FORCE_IF_* flags are135* set.136*/137138/*139* BCH_IOCTL_DISK_ONLINE: given a disk that is already a member of a filesystem140* but is not open (e.g. because we started in degraded mode), bring it online141*142* all existing data on @dev will be available once the device is online,143* exactly as if @dev was present when the filesystem was first mounted144*/145146/*147* BCH_IOCTL_DISK_OFFLINE: offline a disk, causing the kernel to close that148* block device, without removing it from the filesystem (so it can be brought149* back online later)150*151* Data present on @dev will be unavailable while @dev is offline (unless152* replicated), but will still be intact and untouched if @dev is brought back153* online154*155* Will fail (similarly to BCH_IOCTL_DISK_SET_STATE) if offlining @dev would156* leave us with insufficient read write devices or degraded/unavailable data,157* unless the approprate BCH_FORCE_IF_* flags are set.158*/159160struct bch_ioctl_disk {161__u32 flags;162__u32 pad;163__u64 dev;164};165166/*167* BCH_IOCTL_DISK_SET_STATE: modify state of a member device of a filesystem168*169* @new_state - one of the bch_member_state states (rw, ro, failed,170* spare)171*172* Will refuse to change member state if we would then have insufficient devices173* to write to, or if it would result in degraded data (when @new_state is174* failed or spare) unless the appropriate BCH_FORCE_IF_* flags are set.175*/176struct bch_ioctl_disk_set_state {177__u32 flags;178__u8 new_state;179__u8 pad[3];180__u64 dev;181};182183#define BCH_DATA_OPS() \184x(scrub, 0) \185x(rereplicate, 1) \186x(migrate, 2) \187x(rewrite_old_nodes, 3) \188x(drop_extra_replicas, 4)189190enum bch_data_ops {191#define x(t, n) BCH_DATA_OP_##t = n,192BCH_DATA_OPS()193#undef x194BCH_DATA_OP_NR195};196197/*198* BCH_IOCTL_DATA: operations that walk and manipulate filesystem data (e.g.199* scrub, rereplicate, migrate).200*201* This ioctl kicks off a job in the background, and returns a file descriptor.202* Reading from the file descriptor returns a struct bch_ioctl_data_event,203* indicating current progress, and closing the file descriptor will stop the204* job. The file descriptor is O_CLOEXEC.205*/206struct bch_ioctl_data {207__u16 op;208__u8 start_btree;209__u8 end_btree;210__u32 flags;211212struct bpos start_pos;213struct bpos end_pos;214215union {216struct {217__u32 dev;218__u32 data_types;219} scrub;220struct {221__u32 dev;222__u32 pad;223} migrate;224struct {225__u64 pad[8];226};227};228} __packed __aligned(8);229230enum bch_data_event {231BCH_DATA_EVENT_PROGRESS = 0,232/* XXX: add an event for reporting errors */233BCH_DATA_EVENT_NR = 1,234};235236enum data_progress_data_type_special {237DATA_PROGRESS_DATA_TYPE_phys = 254,238DATA_PROGRESS_DATA_TYPE_done = 255,239};240241struct bch_ioctl_data_progress {242__u8 data_type;243__u8 btree_id;244__u8 pad[2];245struct bpos pos;246247__u64 sectors_done;248__u64 sectors_total;249__u64 sectors_error_corrected;250__u64 sectors_error_uncorrected;251} __packed __aligned(8);252253enum bch_ioctl_data_event_ret {254BCH_IOCTL_DATA_EVENT_RET_done = 1,255BCH_IOCTL_DATA_EVENT_RET_device_offline = 2,256};257258struct bch_ioctl_data_event {259__u8 type;260__u8 ret;261__u8 pad[6];262union {263struct bch_ioctl_data_progress p;264__u64 pad2[15];265};266} __packed __aligned(8);267268struct bch_replicas_usage {269__u64 sectors;270struct bch_replicas_entry_v1 r;271} __packed;272273static inline unsigned replicas_usage_bytes(struct bch_replicas_usage *u)274{275return offsetof(struct bch_replicas_usage, r) + replicas_entry_bytes(&u->r);276}277278static inline struct bch_replicas_usage *279replicas_usage_next(struct bch_replicas_usage *u)280{281return (void *) u + replicas_usage_bytes(u);282}283284/* Obsolete */285/*286* BCH_IOCTL_FS_USAGE: query filesystem disk space usage287*288* Returns disk space usage broken out by data type, number of replicas, and289* by component device290*291* @replica_entries_bytes - size, in bytes, allocated for replica usage entries292*293* On success, @replica_entries_bytes will be changed to indicate the number of294* bytes actually used.295*296* Returns -ERANGE if @replica_entries_bytes was too small297*/298struct bch_ioctl_fs_usage {299__u64 capacity;300__u64 used;301__u64 online_reserved;302__u64 persistent_reserved[BCH_REPLICAS_MAX];303304__u32 replica_entries_bytes;305__u32 pad;306307struct bch_replicas_usage replicas[];308};309310/* Obsolete */311/*312* BCH_IOCTL_DEV_USAGE: query device disk space usage313*314* Returns disk space usage broken out by data type - both by buckets and315* sectors.316*/317struct bch_ioctl_dev_usage {318__u64 dev;319__u32 flags;320__u8 state;321__u8 pad[7];322323__u32 bucket_size;324__u64 nr_buckets;325326__u64 buckets_ec;327328struct bch_ioctl_dev_usage_type {329__u64 buckets;330__u64 sectors;331__u64 fragmented;332} d[10];333};334335/* Obsolete */336struct bch_ioctl_dev_usage_v2 {337__u64 dev;338__u32 flags;339__u8 state;340__u8 nr_data_types;341__u8 pad[6];342343__u32 bucket_size;344__u64 nr_buckets;345346struct bch_ioctl_dev_usage_type d[];347};348349/*350* BCH_IOCTL_READ_SUPER: read filesystem superblock351*352* Equivalent to reading the superblock directly from the block device, except353* avoids racing with the kernel writing the superblock or having to figure out354* which block device to read355*356* @sb - buffer to read into357* @size - size of userspace allocated buffer358* @dev - device to read superblock for, if BCH_READ_DEV flag is359* specified360*361* Returns -ERANGE if buffer provided is too small362*/363struct bch_ioctl_read_super {364__u32 flags;365__u32 pad;366__u64 dev;367__u64 size;368__u64 sb;369};370371/*372* BCH_IOCTL_DISK_GET_IDX: give a path to a block device, query filesystem to373* determine if disk is a (online) member - if so, returns device's index374*375* Returns -ENOENT if not found376*/377struct bch_ioctl_disk_get_idx {378__u64 dev;379};380381/*382* BCH_IOCTL_DISK_RESIZE: resize filesystem on a device383*384* @dev - member to resize385* @nbuckets - new number of buckets386*/387struct bch_ioctl_disk_resize {388__u32 flags;389__u32 pad;390__u64 dev;391__u64 nbuckets;392};393394/*395* BCH_IOCTL_DISK_RESIZE_JOURNAL: resize journal on a device396*397* @dev - member to resize398* @nbuckets - new number of buckets399*/400struct bch_ioctl_disk_resize_journal {401__u32 flags;402__u32 pad;403__u64 dev;404__u64 nbuckets;405};406407struct bch_ioctl_subvolume {408__u32 flags;409__u32 dirfd;410__u16 mode;411__u16 pad[3];412__u64 dst_ptr;413__u64 src_ptr;414};415416#define BCH_SUBVOL_SNAPSHOT_CREATE (1U << 0)417#define BCH_SUBVOL_SNAPSHOT_RO (1U << 1)418419/*420* BCH_IOCTL_FSCK_OFFLINE: run fsck from the 'bcachefs fsck' userspace command,421* but with the kernel's implementation of fsck:422*/423struct bch_ioctl_fsck_offline {424__u64 flags;425__u64 opts; /* string */426__u64 nr_devs;427__u64 devs[] __counted_by(nr_devs);428};429430/*431* BCH_IOCTL_FSCK_ONLINE: run fsck from the 'bcachefs fsck' userspace command,432* but with the kernel's implementation of fsck:433*/434struct bch_ioctl_fsck_online {435__u64 flags;436__u64 opts; /* string */437};438439/*440* BCH_IOCTL_QUERY_ACCOUNTING: query filesystem disk accounting441*442* Returns disk space usage broken out by data type, number of replicas, and443* by component device444*445* @replica_entries_bytes - size, in bytes, allocated for replica usage entries446*447* On success, @replica_entries_bytes will be changed to indicate the number of448* bytes actually used.449*450* Returns -ERANGE if @replica_entries_bytes was too small451*/452struct bch_ioctl_query_accounting {453__u64 capacity;454__u64 used;455__u64 online_reserved;456457__u32 accounting_u64s; /* input parameter */458__u32 accounting_types_mask; /* input parameter */459460struct bkey_i_accounting accounting[];461};462463#define BCH_IOCTL_QUERY_COUNTERS_MOUNT (1 << 0)464465struct bch_ioctl_query_counters {466__u16 nr;467__u16 flags;468__u32 pad;469__u64 d[];470};471472#endif /* _BCACHEFS_IOCTL_H */473474475