xref: /openbmc/linux/fs/btrfs/volumes.h (revision 387125fc)
10b86a832SChris Mason /*
20b86a832SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
30b86a832SChris Mason  *
40b86a832SChris Mason  * This program is free software; you can redistribute it and/or
50b86a832SChris Mason  * modify it under the terms of the GNU General Public
60b86a832SChris Mason  * License v2 as published by the Free Software Foundation.
70b86a832SChris Mason  *
80b86a832SChris Mason  * This program is distributed in the hope that it will be useful,
90b86a832SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
100b86a832SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
110b86a832SChris Mason  * General Public License for more details.
120b86a832SChris Mason  *
130b86a832SChris Mason  * You should have received a copy of the GNU General Public
140b86a832SChris Mason  * License along with this program; if not, write to the
150b86a832SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
160b86a832SChris Mason  * Boston, MA 021110-1307, USA.
170b86a832SChris Mason  */
180b86a832SChris Mason 
190b86a832SChris Mason #ifndef __BTRFS_VOLUMES_
200b86a832SChris Mason #define __BTRFS_VOLUMES_
218790d502SChris Mason 
22cea9e445SChris Mason #include <linux/bio.h>
23b2117a39SMiao Xie #include <linux/sort.h>
248b712842SChris Mason #include "async-thread.h"
25cea9e445SChris Mason 
26b2117a39SMiao Xie #define BTRFS_STRIPE_LEN	(64 * 1024)
27b2117a39SMiao Xie 
28f2984462SChris Mason struct buffer_head;
29ffbd517dSChris Mason struct btrfs_pending_bios {
30ffbd517dSChris Mason 	struct bio *head;
31ffbd517dSChris Mason 	struct bio *tail;
32ffbd517dSChris Mason };
33ffbd517dSChris Mason 
340b86a832SChris Mason struct btrfs_device {
350b86a832SChris Mason 	struct list_head dev_list;
36b3075717SChris Mason 	struct list_head dev_alloc_list;
372b82032cSYan Zheng 	struct btrfs_fs_devices *fs_devices;
380b86a832SChris Mason 	struct btrfs_root *dev_root;
39ffbd517dSChris Mason 
40ffbd517dSChris Mason 	/* regular prio bios */
41ffbd517dSChris Mason 	struct btrfs_pending_bios pending_bios;
42ffbd517dSChris Mason 	/* WRITE_SYNC bios */
43ffbd517dSChris Mason 	struct btrfs_pending_bios pending_sync_bios;
44ffbd517dSChris Mason 
458b712842SChris Mason 	int running_pending;
46dfe25020SChris Mason 	u64 generation;
47b3075717SChris Mason 
482b82032cSYan Zheng 	int writeable;
49dfe25020SChris Mason 	int in_fs_metadata;
50cd02dca5SChris Mason 	int missing;
51d5e2003cSJosef Bacik 	int can_discard;
52b3075717SChris Mason 
538790d502SChris Mason 	spinlock_t io_lock;
540b86a832SChris Mason 
550b86a832SChris Mason 	struct block_device *bdev;
560b86a832SChris Mason 
57d4d77629STejun Heo 	/* the mode sent to blkdev_get */
5815916de8SChris Mason 	fmode_t mode;
5915916de8SChris Mason 
608a4b83ccSChris Mason 	char *name;
618a4b83ccSChris Mason 
620b86a832SChris Mason 	/* the internal btrfs device id */
630b86a832SChris Mason 	u64 devid;
640b86a832SChris Mason 
650b86a832SChris Mason 	/* size of the device */
660b86a832SChris Mason 	u64 total_bytes;
670b86a832SChris Mason 
68d6397baeSChris Ball 	/* size of the disk */
69d6397baeSChris Ball 	u64 disk_total_bytes;
70d6397baeSChris Ball 
710b86a832SChris Mason 	/* bytes used */
720b86a832SChris Mason 	u64 bytes_used;
730b86a832SChris Mason 
740b86a832SChris Mason 	/* optimal io alignment for this device */
750b86a832SChris Mason 	u32 io_align;
760b86a832SChris Mason 
770b86a832SChris Mason 	/* optimal io width for this device */
780b86a832SChris Mason 	u32 io_width;
790b86a832SChris Mason 
800b86a832SChris Mason 	/* minimal io size for this device */
810b86a832SChris Mason 	u32 sector_size;
820b86a832SChris Mason 
830b86a832SChris Mason 	/* type and info about this device */
840b86a832SChris Mason 	u64 type;
850b86a832SChris Mason 
860b86a832SChris Mason 	/* physical drive uuid (or lvm uuid) */
87e17cade2SChris Mason 	u8 uuid[BTRFS_UUID_SIZE];
888b712842SChris Mason 
89a2de733cSArne Jansen 	/* per-device scrub information */
90a2de733cSArne Jansen 	struct scrub_dev *scrub_device;
91a2de733cSArne Jansen 
928b712842SChris Mason 	struct btrfs_work work;
931f78160cSXiao Guangrong 	struct rcu_head rcu;
941f78160cSXiao Guangrong 	struct work_struct rcu_work;
9590519d66SArne Jansen 
9690519d66SArne Jansen 	/* readahead state */
9790519d66SArne Jansen 	spinlock_t reada_lock;
9890519d66SArne Jansen 	atomic_t reada_in_flight;
9990519d66SArne Jansen 	u64 reada_next;
10090519d66SArne Jansen 	struct reada_zone *reada_curr_zone;
10190519d66SArne Jansen 	struct radix_tree_root reada_zones;
10290519d66SArne Jansen 	struct radix_tree_root reada_extents;
103387125fcSChris Mason 
104387125fcSChris Mason 	/* for sending down flush barriers */
105387125fcSChris Mason 	struct bio *flush_bio;
106387125fcSChris Mason 	struct completion flush_wait;
107387125fcSChris Mason 	int nobarriers;
108387125fcSChris Mason 
1090b86a832SChris Mason };
1100b86a832SChris Mason 
1118a4b83ccSChris Mason struct btrfs_fs_devices {
1128a4b83ccSChris Mason 	u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
1138a4b83ccSChris Mason 
114d4a78947SWu Fengguang 	/* the device with this id has the most recent copy of the super */
1158a4b83ccSChris Mason 	u64 latest_devid;
1168a4b83ccSChris Mason 	u64 latest_trans;
1178a4b83ccSChris Mason 	u64 num_devices;
118a0af469bSChris Mason 	u64 open_devices;
1192b82032cSYan Zheng 	u64 rw_devices;
120cd02dca5SChris Mason 	u64 missing_devices;
1212b82032cSYan Zheng 	u64 total_rw_bytes;
122d5e2003cSJosef Bacik 	u64 num_can_discard;
1238a4b83ccSChris Mason 	struct block_device *latest_bdev;
124e5e9a520SChris Mason 
125e5e9a520SChris Mason 	/* all of the devices in the FS, protected by a mutex
126e5e9a520SChris Mason 	 * so we can safely walk it to write out the supers without
127e5e9a520SChris Mason 	 * worrying about add/remove by the multi-device code
128e5e9a520SChris Mason 	 */
129e5e9a520SChris Mason 	struct mutex device_list_mutex;
1308a4b83ccSChris Mason 	struct list_head devices;
131b3075717SChris Mason 
132b3075717SChris Mason 	/* devices not currently being allocated */
133b3075717SChris Mason 	struct list_head alloc_list;
1348a4b83ccSChris Mason 	struct list_head list;
1352b82032cSYan Zheng 
1362b82032cSYan Zheng 	struct btrfs_fs_devices *seed;
1372b82032cSYan Zheng 	int seeding;
1382b82032cSYan Zheng 
1392b82032cSYan Zheng 	int opened;
140c289811cSChris Mason 
141c289811cSChris Mason 	/* set when we find or add a device that doesn't have the
142c289811cSChris Mason 	 * nonrot flag set
143c289811cSChris Mason 	 */
144c289811cSChris Mason 	int rotating;
1458a4b83ccSChris Mason };
1468a4b83ccSChris Mason 
147cea9e445SChris Mason struct btrfs_bio_stripe {
148cea9e445SChris Mason 	struct btrfs_device *dev;
149cea9e445SChris Mason 	u64 physical;
150fce3bb9aSLi Dongyang 	u64 length; /* only used for discard mappings */
151cea9e445SChris Mason };
152cea9e445SChris Mason 
153a1d3c478SJan Schmidt struct btrfs_bio;
154a1d3c478SJan Schmidt typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
155a1d3c478SJan Schmidt 
156a1d3c478SJan Schmidt struct btrfs_bio {
157cea9e445SChris Mason 	atomic_t stripes_pending;
158cea9e445SChris Mason 	bio_end_io_t *end_io;
1597d2b4daaSChris Mason 	struct bio *orig_bio;
160cea9e445SChris Mason 	void *private;
161a236aed1SChris Mason 	atomic_t error;
162a236aed1SChris Mason 	int max_errors;
163cea9e445SChris Mason 	int num_stripes;
164a1d3c478SJan Schmidt 	int mirror_num;
165cea9e445SChris Mason 	struct btrfs_bio_stripe stripes[];
166cea9e445SChris Mason };
167cea9e445SChris Mason 
168b2117a39SMiao Xie struct btrfs_device_info {
169b2117a39SMiao Xie 	struct btrfs_device *dev;
170b2117a39SMiao Xie 	u64 dev_offset;
171b2117a39SMiao Xie 	u64 max_avail;
17273c5de00SArne Jansen 	u64 total_avail;
173b2117a39SMiao Xie };
174b2117a39SMiao Xie 
1751abe9b8aSliubo struct map_lookup {
1761abe9b8aSliubo 	u64 type;
1771abe9b8aSliubo 	int io_align;
1781abe9b8aSliubo 	int io_width;
1791abe9b8aSliubo 	int stripe_len;
1801abe9b8aSliubo 	int sector_size;
1811abe9b8aSliubo 	int num_stripes;
1821abe9b8aSliubo 	int sub_stripes;
1831abe9b8aSliubo 	struct btrfs_bio_stripe stripes[];
1841abe9b8aSliubo };
1851abe9b8aSliubo 
186a2de733cSArne Jansen #define map_lookup_size(n) (sizeof(struct map_lookup) + \
187a2de733cSArne Jansen 			    (sizeof(struct btrfs_bio_stripe) * (n)))
188a2de733cSArne Jansen 
1896d07bcecSMiao Xie int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1906d07bcecSMiao Xie 				   u64 end, u64 *length);
1916d07bcecSMiao Xie 
192a1d3c478SJan Schmidt #define btrfs_bio_size(n) (sizeof(struct btrfs_bio) + \
193cea9e445SChris Mason 			    (sizeof(struct btrfs_bio_stripe) * (n)))
194cea9e445SChris Mason 
1950b86a832SChris Mason int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1960b86a832SChris Mason 			   struct btrfs_device *device,
197e17cade2SChris Mason 			   u64 chunk_tree, u64 chunk_objectid,
1982b82032cSYan Zheng 			   u64 chunk_offset, u64 start, u64 num_bytes);
199cea9e445SChris Mason int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
200cea9e445SChris Mason 		    u64 logical, u64 *length,
201a1d3c478SJan Schmidt 		    struct btrfs_bio **bbio_ret, int mirror_num);
202a512bbf8SYan Zheng int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
203a512bbf8SYan Zheng 		     u64 chunk_start, u64 physical, u64 devid,
204a512bbf8SYan Zheng 		     u64 **logical, int *naddrs, int *stripe_len);
205e4404d6eSYan Zheng int btrfs_read_sys_array(struct btrfs_root *root);
2060b86a832SChris Mason int btrfs_read_chunk_tree(struct btrfs_root *root);
2070b86a832SChris Mason int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2082b82032cSYan Zheng 		      struct btrfs_root *extent_root, u64 type);
2090b86a832SChris Mason void btrfs_mapping_init(struct btrfs_mapping_tree *tree);
2100b86a832SChris Mason void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree);
211f188591eSChris Mason int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2128b712842SChris Mason 		  int mirror_num, int async_submit);
2138a4b83ccSChris Mason int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
21497288f2cSChristoph Hellwig 		       fmode_t flags, void *holder);
21597288f2cSChristoph Hellwig int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
2168a4b83ccSChris Mason 			  struct btrfs_fs_devices **fs_devices_ret);
2178a4b83ccSChris Mason int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
218dfe25020SChris Mason int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices);
2198a4b83ccSChris Mason int btrfs_add_device(struct btrfs_trans_handle *trans,
2208a4b83ccSChris Mason 		     struct btrfs_root *root,
2218a4b83ccSChris Mason 		     struct btrfs_device *device);
222a061fc8dSChris Mason int btrfs_rm_device(struct btrfs_root *root, char *device_path);
2238a4b83ccSChris Mason int btrfs_cleanup_fs_uuids(void);
224f188591eSChris Mason int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len);
2258f18cf13SChris Mason int btrfs_grow_device(struct btrfs_trans_handle *trans,
2268f18cf13SChris Mason 		      struct btrfs_device *device, u64 new_size);
2278f18cf13SChris Mason struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2282b82032cSYan Zheng 				       u8 *uuid, u8 *fsid);
2298f18cf13SChris Mason int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
230788f20ebSChris Mason int btrfs_init_new_device(struct btrfs_root *root, char *path);
231ec44a35cSChris Mason int btrfs_balance(struct btrfs_root *dev_root);
2322b82032cSYan Zheng int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset);
233ba1bf481SJosef Bacik int find_free_dev_extent(struct btrfs_trans_handle *trans,
234ba1bf481SJosef Bacik 			 struct btrfs_device *device, u64 num_bytes,
235ba1bf481SJosef Bacik 			 u64 *start, u64 *max_avail);
2360b86a832SChris Mason #endif
237