2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * This is the in core representation of a volume group and its
18 * associated physical and logical volumes.
21 #ifndef _LVM_METADATA_H
22 #define _LVM_METADATA_H
25 #include "dev-cache.h"
26 #include "lvm-string.h"
27 #include "metadata-exported.h"
29 //#define MAX_STRIPES 128U
30 //#define SECTOR_SHIFT 9L
31 //#define SECTOR_SIZE ( 1L << SECTOR_SHIFT )
32 //#define STRIPE_SIZE_MIN ( (unsigned) lvm_getpagesize() >> SECTOR_SHIFT) /* PAGESIZE in sectors */
33 //#define STRIPE_SIZE_MAX ( 512L * 1024L >> SECTOR_SHIFT) /* 512 KB in sectors */
34 //#define STRIPE_SIZE_LIMIT ((UINT_MAX >> 2) + 1)
35 //#define PV_MIN_SIZE ( 512L * 1024L >> SECTOR_SHIFT) /* 512 KB in sectors */
36 //#define MAX_RESTRICTED_LVS 255 /* Used by FMT_RESTRICTED_LVIDS */
37 #define MIRROR_LOG_OFFSET 2 /* sectors */
38 #define VG_MEMPOOL_CHUNK 10240 /* in bytes, hint only */
43 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
46 * Ceiling(n / size) * size
48 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
52 /* Note that the bits no longer necessarily correspond to LVM1 disk format */
54 //#define PARTIAL_VG 0x00000001U /* VG */
55 //#define EXPORTED_VG 0x00000002U /* VG PV */
56 //#define RESIZEABLE_VG 0x00000004U /* VG */
58 /* May any free extents on this PV be used or must they be left free? */
59 //#define ALLOCATABLE_PV 0x00000008U /* PV */
61 #define SPINDOWN_LV 0x00000010U /* LV */
62 #define BADBLOCK_ON 0x00000020U /* LV */
63 //#define VISIBLE_LV 0x00000040U /* LV */
64 //#define FIXED_MINOR 0x00000080U /* LV */
65 /* FIXME Remove when metadata restructuring is completed */
66 //#define SNAPSHOT 0x00001000U /* LV - internal use only */
67 //#define PVMOVE 0x00002000U /* VG LV SEG */
68 //#define LOCKED 0x00004000U /* LV */
69 //#define MIRRORED 0x00008000U /* LV - internal use only */
70 #define VIRTUAL 0x00010000U /* LV - internal use only */
71 //#define MIRROR_LOG 0x00020000U /* LV */
72 //#define MIRROR_IMAGE 0x00040000U /* LV */
73 //#define MIRROR_NOTSYNCED 0x00080000U /* LV */
74 #define ACTIVATE_EXCL 0x00100000U /* LV - internal use only */
75 #define PRECOMMITTED 0x00200000U /* VG - internal use only */
76 //#define CONVERTING 0x00400000U /* LV */
78 //#define MISSING_PV 0x00800000U /* PV */
79 #define INCONSISTENT_VG 0x00800000U /* VG - internal use only */
80 //#define PARTIAL_LV 0x01000000U /* LV - derived flag, not
81 // written out in metadata*/
83 #define POSTORDER_FLAG 0x02000000U /* Not real flags, reserved for */
84 #define POSTORDER_OPEN_FLAG 0x04000000U /* temporary use inside vg_read_internal. */
85 #define VIRTUAL_ORIGIN 0x08000000U /* LV - internal use only */
87 //#define LVM_READ 0x00000100U /* LV VG */
88 //#define LVM_WRITE 0x00000200U /* LV VG */
89 //#define CLUSTERED 0x00000400U /* VG */
90 #define SHARED 0x00000800U /* VG */
92 /* Format features flags */
93 //#define FMT_SEGMENTS 0x00000001U /* Arbitrary segment params? */
94 //#define FMT_MDAS 0x00000002U /* Proper metadata areas? */
95 //#define FMT_TAGS 0x00000004U /* Tagging? */
96 //#define FMT_UNLIMITED_VOLS 0x00000008U /* Unlimited PVs/LVs? */
97 //#define FMT_RESTRICTED_LVIDS 0x00000010U /* LVID <= 255 */
98 //#define FMT_ORPHAN_ALLOCATABLE 0x00000020U /* Orphan PV allocatable? */
99 #define FMT_PRECOMMIT 0x00000040U /* Supports pre-commit? */
100 //#define FMT_RESIZE_PV 0x00000080U /* Supports pvresize? */
101 //#define FMT_UNLIMITED_STRIPESIZE 0x00000100U /* Unlimited stripe size? */
103 struct metadata_area;
105 /* Per-format per-metadata area operations */
106 struct metadata_area_ops {
107 struct volume_group *(*vg_read) (struct format_instance * fi,
109 struct metadata_area * mda);
110 struct volume_group *(*vg_read_precommit) (struct format_instance * fi,
112 struct metadata_area * mda);
114 * Write out complete VG metadata. You must ensure internal
115 * consistency before calling. eg. PEs can't refer to PVs not
118 * It is also the responsibility of the caller to ensure external
119 * consistency, eg by calling pv_write() if removing PVs from
120 * a VG or calling vg_write() a second time if splitting a VG
123 * vg_write() should not read or write from any PVs not included
124 * in the volume_group structure it is handed.
125 * (format1 currently breaks this rule.)
127 int (*vg_write) (struct format_instance * fid, struct volume_group * vg,
128 struct metadata_area * mda);
129 int (*vg_precommit) (struct format_instance * fid,
130 struct volume_group * vg,
131 struct metadata_area * mda);
132 int (*vg_commit) (struct format_instance * fid,
133 struct volume_group * vg, struct metadata_area * mda);
134 int (*vg_revert) (struct format_instance * fid,
135 struct volume_group * vg, struct metadata_area * mda);
136 int (*vg_remove) (struct format_instance * fi, struct volume_group * vg,
137 struct metadata_area * mda);
140 * Per location copy constructor.
142 void *(*mda_metadata_locn_copy) (struct dm_pool *mem, void *metadata_locn);
145 * Per location description for logging.
147 const char *(*mda_metadata_locn_name) (void *metadata_locn);
148 uint64_t (*mda_metadata_locn_offset) (void *metadata_locn);
151 * Returns number of free sectors in given metadata area.
153 uint64_t (*mda_free_sectors) (struct metadata_area *mda);
156 * Returns number of total sectors in given metadata area.
158 uint64_t (*mda_total_sectors) (struct metadata_area *mda);
161 * Check if metadata area belongs to vg
163 int (*mda_in_vg) (struct format_instance * fi,
164 struct volume_group * vg, struct metadata_area *mda);
166 * Analyze a metadata area on a PV.
168 int (*pv_analyze_mda) (const struct format_type * fmt,
169 struct metadata_area *mda);
172 * Do these two metadata_area structures match with respect to
173 * their underlying location?
175 unsigned (*mda_locns_match)(struct metadata_area *mda1,
176 struct metadata_area *mda2);
179 #define MDA_IGNORED 0x00000001
181 struct metadata_area {
183 struct metadata_area_ops *ops;
187 struct metadata_area *mda_copy(struct dm_pool *mem,
188 struct metadata_area *mda);
190 unsigned mda_is_ignored(struct metadata_area *mda);
191 void mda_set_ignored(struct metadata_area *mda, unsigned ignored);
192 unsigned mda_locns_match(struct metadata_area *mda1, struct metadata_area *mda2);
193 void fid_add_mda(struct format_instance *fid, struct metadata_area *mda);
194 int fid_add_mdas(struct format_instance *fid, struct dm_list *mdas);
195 int mdas_empty_or_ignored(struct dm_list *mdas);
197 #define seg_pvseg(seg, s) (seg)->areas[(s)].u.pv.pvseg
198 #define seg_dev(seg, s) (seg)->areas[(s)].u.pv.pvseg->pv->dev
199 #define seg_pe(seg, s) (seg)->areas[(s)].u.pv.pvseg->pe
200 #define seg_le(seg, s) (seg)->areas[(s)].u.lv.le
209 struct device_area mda;
214 struct pv_segment *peg;
220 struct lv_segment *seg;
224 * Ownership of objects passes to caller.
226 struct format_handler {
228 * Scan any metadata areas that aren't referenced in PV labels
230 int (*scan) (const struct format_type * fmt, const char *vgname);
233 * Return PV with given path.
235 int (*pv_read) (const struct format_type * fmt, const char *pv_name,
236 struct physical_volume * pv, struct dm_list *mdas,
237 int scan_label_only);
240 * Tweak an already filled out a pv ready for importing into a
241 * vg. eg. pe_count is format specific.
243 int (*pv_setup) (const struct format_type * fmt,
244 uint64_t pe_start, uint32_t extent_count,
245 uint32_t extent_size, unsigned long data_alignment,
246 unsigned long data_alignment_offset,
247 int pvmetadatacopies, uint64_t pvmetadatasize,
248 unsigned metadataignore, struct dm_list * mdas,
249 struct physical_volume * pv, struct volume_group * vg);
252 * Write a PV structure to disk. Fails if the PV is in a VG ie
253 * pv->vg_name must be a valid orphan VG name
255 int (*pv_write) (const struct format_type * fmt,
256 struct physical_volume * pv, struct dm_list * mdas,
257 int64_t label_sector);
260 * Tweak an already filled out a lv eg, check there
261 * aren't too many extents.
263 int (*lv_setup) (struct format_instance * fi,
264 struct logical_volume * lv);
267 * Tweak an already filled out vg. eg, max_pv is format
270 int (*vg_setup) (struct format_instance * fi, struct volume_group * vg);
273 * Check whether particular segment type is supported.
275 int (*segtype_supported) (struct format_instance *fid,
276 const struct segment_type *segtype);
279 * Create format instance with a particular metadata area
281 struct format_instance *(*create_instance) (const struct format_type *
282 fmt, const char *vgname,
287 * Destructor for format instance
289 void (*destroy_instance) (struct format_instance * fid);
292 * Destructor for format type
294 void (*destroy) (struct format_type * fmt);
300 unsigned long set_pe_align(struct physical_volume *pv, unsigned long data_alignment);
301 unsigned long set_pe_align_offset(struct physical_volume *pv,
302 unsigned long data_alignment_offset);
303 int vg_validate(struct volume_group *vg);
305 int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv);
307 /* Manipulate PV structures */
308 int pv_add(struct volume_group *vg, struct physical_volume *pv);
309 int pv_remove(struct volume_group *vg, struct physical_volume *pv);
310 struct physical_volume *pv_find(struct volume_group *vg, const char *pv_name);
312 /* Find a PV within a given VG */
313 int get_pv_from_vg_by_id(const struct format_type *fmt, const char *vg_name,
314 const char *vgid, const char *pvid,
315 struct physical_volume *pv);
317 struct lv_list *find_lv_in_vg_by_lvid(struct volume_group *vg,
318 const union lvid *lvid);
320 struct lv_list *find_lv_in_lv_list(const struct dm_list *ll,
321 const struct logical_volume *lv);
323 /* Return the VG that contains a given LV (based on path given in lv_name) */
324 /* or environment var */
325 struct volume_group *find_vg_with_lv(const char *lv_name);
327 /* Find LV with given lvid (used during activation) */
328 struct logical_volume *lv_from_lvid(struct cmd_context *cmd,
330 unsigned precommitted);
332 /* FIXME Merge these functions with ones above */
333 struct physical_volume *find_pv(struct volume_group *vg, struct device *dev);
335 struct pv_list *find_pv_in_pv_list(const struct dm_list *pl,
336 const struct physical_volume *pv);
338 /* Find LV segment containing given LE */
339 struct lv_segment *find_seg_by_le(const struct logical_volume *lv, uint32_t le);
342 * Remove a dev_dir if present.
344 const char *strip_dir(const char *vg_name, const char *dir);
346 struct logical_volume *alloc_lv(struct dm_pool *mem);
349 * Checks that an lv has no gaps or overlapping segments.
350 * Set complete_vg to perform additional VG level checks.
352 int check_lv_segments(struct logical_volume *lv, int complete_vg);
356 * Checks that a replicator segment is correct.
358 int check_replicator_segment(const struct lv_segment *replicator_seg);
361 * Sometimes (eg, after an lvextend), it is possible to merge two
362 * adjacent segments into a single segment. This function trys
363 * to merge as many segments as possible.
365 int lv_merge_segments(struct logical_volume *lv);
368 * Ensure there's a segment boundary at a given LE, splitting if necessary
370 int lv_split_segment(struct logical_volume *lv, uint32_t le);
373 * Add/remove upward link from underlying LV to the segment using it
374 * FIXME: ridiculously long name
376 int add_seg_to_segs_using_this_lv(struct logical_volume *lv, struct lv_segment *seg);
377 int remove_seg_from_segs_using_this_lv(struct logical_volume *lv, struct lv_segment *seg);
378 struct lv_segment *get_only_segment_using_this_lv(struct logical_volume *lv);
381 * Calculate readahead from underlying PV devices
383 void lv_calculate_readahead(const struct logical_volume *lv, uint32_t *read_ahead);
386 * For internal metadata caching.
388 int export_vg_to_buffer(struct volume_group *vg, char **buf);
389 struct volume_group *import_vg_from_buffer(const char *buf,
390 struct format_instance *fid);
393 * Mirroring functions
397 * Given mirror image or mirror log segment, find corresponding mirror segment
399 int fixup_imported_mirrors(struct volume_group *vg);
402 * Begin skeleton for external LVM library
404 struct id pv_id(const struct physical_volume *pv);
405 const struct format_type *pv_format_type(const struct physical_volume *pv);
406 struct id pv_vgid(const struct physical_volume *pv);
408 struct physical_volume *pv_by_path(struct cmd_context *cmd, const char *pv_name);
409 int add_pv_to_vg(struct volume_group *vg, const char *pv_name,
410 struct physical_volume *pv);
411 int vg_mark_partial_lvs(struct volume_group *vg);
412 int is_mirror_image_removable(struct logical_volume *mimage_lv, void *baton);
414 uint64_t find_min_mda_size(struct dm_list *mdas);
415 char *tags_format_and_copy(struct dm_pool *mem, const struct dm_list *tags);