1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2012 Fusion-io All rights reserved.
4 * Copyright (C) 2012 Intel Corp. All rights reserved.
10 #include <linux/workqueue.h>
15 BTRFS_RBIO_READ_REBUILD,
16 BTRFS_RBIO_PARITY_SCRUB,
19 struct btrfs_raid_bio {
20 struct btrfs_io_context *bioc;
23 * While we're doing RMW on a stripe we put it into a hash table so we
24 * can lock the stripe and merge more rbios into it.
26 struct list_head hash_list;
28 /* LRU list for the stripe cache */
29 struct list_head stripe_cache;
31 /* For scheduling work in the helper threads */
32 struct work_struct work;
35 * bio_list and bio_list_lock are used to add more bios into the stripe
36 * in hopes of avoiding the full RMW
38 struct bio_list bio_list;
39 spinlock_t bio_list_lock;
42 * Also protected by the bio_list_lock, the plug list is used by the
43 * plugging code to collect partial bios while plugged. The stripe
44 * locking code also uses it to hand off the stripe lock to the next
47 struct list_head plug_list;
49 /* Flags that tell us if it is safe to merge with this bio. */
53 * Set if we're doing a parity rebuild for a read from higher up, which
54 * is handled differently from a parity rebuild as part of RMW.
56 enum btrfs_rbio_ops operation;
58 /* How many pages there are for the full stripe including P/Q */
61 /* How many sectors there are for the full stripe including P/Q */
64 /* Number of data stripes (no p/q) */
67 /* Number of all stripes (including P/Q) */
70 /* How many pages there are for each stripe */
73 /* How many sectors there are for each stripe */
76 /* Stripe number that we're scrubbing */
80 * Size of all the bios in the bio_list. This helps us decide if the
81 * rbio maps to a full stripe or not.
87 atomic_t stripes_pending;
89 wait_queue_head_t io_wait;
91 /* Bitmap to record which horizontal stripe has data */
92 unsigned long dbitmap;
94 /* Allocated with stripe_nsectors-many bits for finish_*() calls */
95 unsigned long finish_pbitmap;
98 * These are two arrays of pointers. We allocate the rbio big enough
99 * to hold them both and setup their locations when the rbio is
104 * Pointers to pages that we allocated for reading/writing stripes
105 * directly from the disk (including P/Q).
107 struct page **stripe_pages;
109 /* Pointers to the sectors in the bio_list, for faster lookup */
110 struct sector_ptr *bio_sectors;
113 * For subpage support, we need to map each sector to above
116 struct sector_ptr *stripe_sectors;
118 /* Allocated with real_stripes-many pointers for finish_*() calls */
119 void **finish_pointers;
122 * The bitmap recording where IO errors happened.
123 * Each bit is corresponding to one sector in either bio_sectors[] or
124 * stripe_sectors[] array.
126 * The reason we don't use another bit in sector_ptr is, we have two
127 * arrays of sectors, and a lot of IO can use sectors in both arrays.
128 * Thus making it much harder to iterate.
130 unsigned long *error_bitmap;
133 * Checksum buffer if the rbio is for data. The buffer should cover
134 * all data sectors (excluding P/Q sectors).
139 * Each bit represents if the corresponding sector has data csum found.
140 * Should only cover data sectors (excluding P/Q sectors).
142 unsigned long *csum_bitmap;
146 * For trace event usage only. Records useful debug info for each bio submitted
147 * by RAID56 to each physical device.
149 * No matter signed or not, (-1) is always the one indicating we can not grab
150 * the proper stripe number.
152 struct raid56_bio_trace_info {
155 /* The offset inside the stripe. (<= STRIPE_LEN) */
160 * 0 is the first data stripe, and nr_data for P stripe,
161 * nr_data + 1 for Q stripe.
162 * >= real_stripes for
167 static inline int nr_data_stripes(const struct map_lookup *map)
169 return map->num_stripes - btrfs_nr_parity_stripes(map->type);
172 static inline int nr_bioc_data_stripes(const struct btrfs_io_context *bioc)
174 return bioc->num_stripes - btrfs_nr_parity_stripes(bioc->map_type);
177 #define RAID5_P_STRIPE ((u64)-2)
178 #define RAID6_Q_STRIPE ((u64)-1)
180 #define is_parity_stripe(x) (((x) == RAID5_P_STRIPE) || \
181 ((x) == RAID6_Q_STRIPE))
185 void raid56_parity_recover(struct bio *bio, struct btrfs_io_context *bioc,
187 void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc);
189 struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
190 struct btrfs_io_context *bioc,
191 struct btrfs_device *scrub_dev,
192 unsigned long *dbitmap, int stripe_nsectors);
193 void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
195 void raid56_parity_cache_data_pages(struct btrfs_raid_bio *rbio,
196 struct page **data_pages, u64 data_logical);
198 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info);
199 void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);