return ret;
}
+/*
+ * Parse the value of the btrfs.stripe_alloc_allow_rmw property, a word list
+ * ("nodatacow", "prealloc", "fsync"), into its mask. Words are separated by
+ * comma, space or colon. An empty value yields an empty mask. Returns
+ * -EINVAL on any unknown word.
+ */
+int btrfs_parse_stripe_rmw(const char *value, size_t len, u32 *mask)
+{
+ u32 m = 0;
+ size_t i = 0;
+
+ while (i < len) {
+ size_t j = i;
+
+ while (j < len && value[j] != ',' && value[j] != ' ' &&
+ value[j] != ':')
+ j++;
+ if (j - i == strlen("nodatacow") &&
+ !strncmp(value + i, "nodatacow", j - i))
+ m |= BTRFS_STRIPE_RMW_NODATACOW;
+ else if (j - i == strlen("prealloc") &&
+ !strncmp(value + i, "prealloc", j - i))
+ m |= BTRFS_STRIPE_RMW_PREALLOC;
+ else if (j - i == strlen("fsync") &&
+ !strncmp(value + i, "fsync", j - i))
+ m |= BTRFS_STRIPE_RMW_FSYNC;
+ else if (j != i)
+ return -EINVAL;
+ i = j + 1;
+ }
+ *mask = m;
+ return 0;
+}
+
+/*
+ * Does every committed extent in [start, start + len) belong solely to
+ * @ino in @root_id -- exactly one reference, a plain data ref, count 1?
+ * Any metadata block, shared reference, or foreign owner fails. Caller
+ * limits the range to full stripes; uncommitted allocations need not be
+ * visible because nothing can allocate into a partially used stripe (the
+ * fully-free claim rule), so a foreign extent can only appear here after
+ * the whole stripe frees and commits -- which the extent tree shows.
+ */
+static bool stripe_extents_owned_by(struct btrfs_fs_info *fs_info,
+ u64 root_id, u64 ino, u64 start, u64 len)
+{
+ struct btrfs_root *extent_root = btrfs_extent_root(fs_info, start);
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ bool ret = true;
+ int err;
+
+ if (!extent_root)
+ return false;
+ path = btrfs_alloc_path();
+ if (!path)
+ return false;
+
+ key.objectid = start;
+ key.type = BTRFS_EXTENT_ITEM_KEY;
+ key.offset = 0;
+ err = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
+ if (err < 0) {
+ ret = false;
+ goto out;
+ }
+ /* An extent starting before @start may still overlap it. */
+ err = btrfs_previous_extent_item(extent_root, path, 0);
+ if (err < 0) {
+ ret = false;
+ goto out;
+ }
+ if (err > 0) {
+ btrfs_release_path(path);
+ key.objectid = start;
+ key.type = BTRFS_EXTENT_ITEM_KEY;
+ key.offset = 0;
+ err = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
+ if (err < 0) {
+ ret = false;
+ goto out;
+ }
+ }
+
+ while (true) {
+ struct extent_buffer *leaf = path->nodes[0];
+ int slot = path->slots[0];
+ struct btrfs_extent_item *ei;
+ struct btrfs_extent_inline_ref *iref;
+ struct btrfs_extent_data_ref *dref;
+ int type;
+
+ if (slot >= btrfs_header_nritems(leaf)) {
+ err = btrfs_next_leaf(extent_root, path);
+ if (err < 0) {
+ ret = false;
+ goto out;
+ }
+ if (err > 0)
+ break;
+ continue;
+ }
+ btrfs_item_key_to_cpu(leaf, &key, slot);
+ if (key.objectid >= start + len)
+ break;
+ if (key.type == BTRFS_METADATA_ITEM_KEY) {
+ /*
+ * key.offset is the level, not a length; a tree
+ * block spans nodesize bytes. One from before the
+ * range (e.g. where the pre-range positioning landed
+ * on a metadata block group's last item) is harmless.
+ */
+ if (key.objectid + fs_info->nodesize > start) {
+ ret = false;
+ goto out;
+ }
+ path->slots[0]++;
+ continue;
+ }
+ if (key.type != BTRFS_EXTENT_ITEM_KEY ||
+ key.objectid + key.offset <= start) {
+ path->slots[0]++;
+ continue;
+ }
+
+ ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
+ if (btrfs_extent_refs(leaf, ei) != 1 ||
+ !(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
+ ret = false;
+ goto out;
+ }
+ /*
+ * With a keyed (non-inline) backref the item ends after the
+ * header, and reading an inline ref would read the next item.
+ * Treat it as not owned, the conservative answer.
+ */
+ if (btrfs_item_size(leaf, slot) < sizeof(*ei) +
+ btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) {
+ ret = false;
+ goto out;
+ }
+ iref = (struct btrfs_extent_inline_ref *)(ei + 1);
+ type = btrfs_get_extent_inline_ref_type(leaf, iref,
+ BTRFS_REF_TYPE_DATA);
+ if (type != BTRFS_EXTENT_DATA_REF_KEY) {
+ ret = false;
+ goto out;
+ }
+ dref = (struct btrfs_extent_data_ref *)(&iref->offset);
+ if (btrfs_extent_data_ref_root(leaf, dref) != root_id ||
+ btrfs_extent_data_ref_objectid(leaf, dref) != ino ||
+ btrfs_extent_data_ref_count(leaf, dref) != 1) {
+ ret = false;
+ goto out;
+ }
+ path->slots[0]++;
+ }
+out:
+ btrfs_free_path(path);
+ return ret;
+}
+
+/*
+ * May [start, start + num_bytes) be written in place under stripe-exclusive
+ * allocation? Only when stripe_alloc_allow_rmw includes @which and every
+ * full stripe the range touches is isolated to @inode: an in-place write
+ * RMWs parity, so a crash can tear every extent sharing the stripe, and
+ * isolation confines that to the file that opted out of COW. Fast path:
+ * the stripe is in one of the inode's own NOCOW runs. Slow path: the
+ * committed extent tree proves sole ownership. Extents that fail stay
+ * COWed, and their rewrite lands in the inode's NOCOW run, so the next
+ * overwrite passes.
+ */
+bool btrfs_stripe_nocow_writable(struct btrfs_inode *inode, u64 start,
+ u64 num_bytes, u32 which)
+{
+ struct btrfs_fs_info *fs_info = inode->root->fs_info;
+ struct btrfs_block_group *bg;
+ const u64 root_id = btrfs_root_id(inode->root);
+ const u64 ino = btrfs_ino(inode);
+ u64 fsl;
+ u64 cur;
+ u64 end;
+ bool ret = true;
+
+ if (!(btrfs_stripe_allow_rmw(fs_info) & which))
+ return false;
+ bg = btrfs_lookup_block_group(fs_info, start);
+ if (!bg)
+ return false;
+ if (!btrfs_is_stripe_alloc_bg(bg)) {
+ /* Not a raid56 data group: nothing forces COW here anyway. */
+ btrfs_put_block_group(bg);
+ return true;
+ }
+
+ fsl = bg->full_stripe_len;
+ cur = bg->start + round_down(start - bg->start, fsl);
+ end = start + num_bytes;
+ while (cur < end) {
+ struct btrfs_open_stripe_run *run;
+ unsigned long flags;
+ int verdict = 0; /* 0 = unknown, 1 = ok, -1 = foreign */
+
+ spin_lock_irqsave(&bg->stripe_run_lock, flags);
+ list_for_each_entry(run, &bg->open_stripe_runs, list) {
+ if (cur < run->start || cur >= run->end)
+ continue;
+ if (run->class == BTRFS_STRIPE_RUN_NOCOW &&
+ run->owner == ino)
+ verdict = 1;
+ else
+ verdict = -1;
+ break;
+ }
+ spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+
+ if (verdict == 0 &&
+ !stripe_extents_owned_by(fs_info, root_id, ino, cur, fsl))
+ verdict = -1;
+ if (verdict < 0) {
+ ret = false;
+ break;
+ }
+ cur += fsl;
+ }
+ btrfs_put_block_group(bg);
+ return ret;
+}
+
/*
* Lower an allocation ceiling to the end of the last live sector at or above
* @floor. Sectors between it and the frontier were allocated and abandoned:
* the commit drain already waits for and everything above it is dead space
* padding may fill.
*/
+bool btrfs_stripe_in_open_run(struct btrfs_fs_info *fs_info, u64 logical)
+{
+ return btrfs_stripe_open_run_class(fs_info, logical, NULL);
+}
+
/*
* The part of [*start, *start + *len) that @run owns and tracks in its
* written map: inside [run->start, run->end) -- the map itself reaches
return ret;
}
-bool btrfs_stripe_in_open_run(struct btrfs_fs_info *fs_info, u64 logical)
+bool btrfs_stripe_open_run_class(struct btrfs_fs_info *fs_info, u64 logical,
+ enum btrfs_stripe_run_class *class)
{
struct btrfs_block_group *bg;
struct btrfs_open_stripe_run *run;
spin_lock_irqsave(&bg->stripe_run_lock, flags);
list_for_each_entry(run, &bg->open_stripe_runs, list) {
if (logical >= run->start && logical < run->end) {
+ if (class)
+ *class = run->class;
ret = true;
break;
}
* counter must be rescanned at commit (stripe_alloc only).
*/
BLOCK_GROUP_FLAG_STRIPE_UNUSABLE_DIRTY,
+ /*
+ * The group has hosted NOCOW-class stripe runs. When
+ * stripe_alloc_allow_rmw permits it, isolated nodatacow/prealloc
+ * extents are overwritten in place after their runs drain, so the
+ * write-hole debug check skips such groups (sticky, debug only).
+ */
+ BLOCK_GROUP_FLAG_STRIPE_NOCOW_USED,
/*
* A RAID5/6 group with a single data stripe, which is mapped as
* RAID1/RAID1C3 (its chunk map's type differs from on_disk_type). Its
u64 len);
bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
u64 stripe_start, u64 stripe_len);
+bool btrfs_stripe_open_run_class(struct btrfs_fs_info *fs_info, u64 logical,
+ enum btrfs_stripe_run_class *class);
u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
u64 nstripes);
bool btrfs_stripe_run_strands(struct btrfs_block_group *bg, u64 stripe);
u64 stripe_len, unsigned long *dead,
unsigned long *unwritten,
struct btrfs_stripe_pad_info *info);
+bool btrfs_stripe_nocow_writable(struct btrfs_inode *inode, u64 start,
+ u64 num_bytes, u32 which);
+int btrfs_parse_stripe_rmw(const char *value, size_t len, u32 *mask);
int btrfs_stripe_alloc_check_support(struct btrfs_fs_info *fs_info);
int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info);
void btrfs_disable_stripe_alloc(struct btrfs_fs_info *fs_info);