^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef BTRFS_ORDERED_DATA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define BTRFS_ORDERED_DATA_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /* one of these per inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct btrfs_ordered_inode_tree {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct rb_root tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct rb_node *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct btrfs_ordered_sum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* bytenr is the start of this extent on disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * this is the length in bytes covered by the sums array below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* last field is a variable length array of csums */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u8 sums[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * bits for the flags field:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * BTRFS_ORDERED_IO_DONE is set when all of the blocks are written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * It is used to make sure metadata is inserted into the tree only once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * per extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * BTRFS_ORDERED_COMPLETE is set when the extent is removed from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * rbtree, just before waking any waiters. It is used to indicate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * IO is done and any metadata is inserted into the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* set when all the pages are written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) BTRFS_ORDERED_IO_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* set when removed from the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) BTRFS_ORDERED_COMPLETE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* set when we want to write in place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) BTRFS_ORDERED_NOCOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* writing a zlib compressed extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) BTRFS_ORDERED_COMPRESSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* set when writing to preallocated extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) BTRFS_ORDERED_PREALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* set when we're doing DIO with this extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) BTRFS_ORDERED_DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* We had an io error when writing this out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) BTRFS_ORDERED_IOERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Set when we have to truncate an extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) BTRFS_ORDERED_TRUNCATED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Regular IO for COW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) BTRFS_ORDERED_REGULAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Used during fsync to track already logged extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) BTRFS_ORDERED_LOGGED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* We have already logged all the csums of the ordered extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) BTRFS_ORDERED_LOGGED_CSUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* We wait for this extent to complete in the current transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) BTRFS_ORDERED_PENDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct btrfs_ordered_extent {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* logical offset in the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u64 file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * These fields directly correspond to the same fields in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * btrfs_file_extent_item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u64 disk_bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u64 disk_num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* number of bytes that still need writing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) u64 bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * the end of the ordered extent which is behind it but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * didn't update disk_i_size. Please see the comment of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * btrfs_ordered_update_i_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u64 outstanding_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * If we get truncated we need to adjust the file extent we enter for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * this ordered extent so that we do not expose stale data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) u64 truncated_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* flags (described above) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* compression algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Qgroup reserved space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int qgroup_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* reference count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) refcount_t refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* the inode we belong to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* list of checksums for insertion when the extent io is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* used for fast fsyncs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct list_head log_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* used to wait for the BTRFS_ORDERED_COMPLETE bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* our friendly rbtree entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct rb_node rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* a per root list of all the pending ordered extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct list_head root_extent_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct btrfs_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct completion completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct btrfs_work flush_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct list_head work_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * calculates the total size you need to allocate for an ordered sum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * structure spanning 'bytes' in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline int btrfs_ordered_sum_size(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int num_sectors = (int)DIV_ROUND_UP(bytes, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return sizeof(struct btrfs_ordered_sum) + num_sectors * csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) spin_lock_init(&t->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) t->tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) t->last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct btrfs_ordered_extent *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct btrfs_ordered_extent **cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u64 file_offset, u64 io_size, int uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct btrfs_ordered_extent **cached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u64 *file_offset, u64 io_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u64 disk_bytenr, u64 num_bytes, u64 disk_num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int btrfs_add_ordered_extent_dio(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u64 disk_bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u64 disk_num_bytes, int type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int btrfs_add_ordered_extent_compress(struct btrfs_inode *inode, u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) u64 disk_bytenr, u64 num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) u64 disk_num_bytes, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct btrfs_ordered_sum *sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) u64 file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry, int wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct btrfs_ordered_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u64 file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) u64 len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct list_head *list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int btrfs_find_ordered_sum(struct btrfs_inode *inode, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) u64 disk_bytenr, u8 *sum, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) const u64 range_start, const u64 range_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) const u64 range_start, const u64 range_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct extent_state **cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int __init ordered_data_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) void __cold ordered_data_exit(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #endif