Home | History | Annotate | Line # | Download | only in include
metadata.h revision 1.1.1.2
      1 /*	$NetBSD: metadata.h,v 1.1.1.2 2009/02/18 11:16:47 haad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
      5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
      6  *
      7  * This file is part of LVM2.
      8  *
      9  * This copyrighted material is made available to anyone wishing to use,
     10  * modify, copy, or redistribute it subject to the terms and conditions
     11  * of the GNU Lesser General Public License v.2.1.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program; if not, write to the Free Software Foundation,
     15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     16  */
     17 
     18 /*
     19  * This is the in core representation of a volume group and its
     20  * associated physical and logical volumes.
     21  */
     22 
     23 #ifndef _LVM_METADATA_H
     24 #define _LVM_METADATA_H
     25 
     26 #include "ctype.h"
     27 #include "dev-cache.h"
     28 #include "lvm-string.h"
     29 #include "metadata-exported.h"
     30 
     31 //#define MAX_STRIPES 128U
     32 //#define SECTOR_SHIFT 9L
     33 #define SECTOR_SIZE ( 1L << SECTOR_SHIFT )
     34 //#define STRIPE_SIZE_MIN ( (unsigned) lvm_getpagesize() >> SECTOR_SHIFT)	/* PAGESIZE in sectors */
     35 //#define STRIPE_SIZE_MAX ( 512L * 1024L >> SECTOR_SHIFT)	/* 512 KB in sectors */
     36 //#define STRIPE_SIZE_LIMIT ((UINT_MAX >> 2) + 1)
     37 //#define PV_MIN_SIZE ( 512L * 1024L >> SECTOR_SHIFT)	/* 512 KB in sectors */
     38 //#define MAX_RESTRICTED_LVS 255	/* Used by FMT_RESTRICTED_LVIDS */
     39 #define MIRROR_LOG_OFFSET	2	/* sectors */
     40 
     41 /*
     42  * Ceiling(n / sz)
     43  */
     44 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
     45 
     46 /*
     47  * Ceiling(n / size) * size
     48  */
     49 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
     50 
     51 
     52 /* Various flags */
     53 /* Note that the bits no longer necessarily correspond to LVM1 disk format */
     54 
     55 //#define PARTIAL_VG		0x00000001U	/* VG */
     56 //#define EXPORTED_VG          	0x00000002U	/* VG PV */
     57 //#define RESIZEABLE_VG        	0x00000004U	/* VG */
     58 
     59 /* May any free extents on this PV be used or must they be left free? */
     60 //#define ALLOCATABLE_PV         	0x00000008U	/* PV */
     61 
     62 #define SPINDOWN_LV          	0x00000010U	/* LV */
     63 #define BADBLOCK_ON       	0x00000020U	/* LV */
     64 //#define VISIBLE_LV		0x00000040U	/* LV */
     65 //#define FIXED_MINOR		0x00000080U	/* LV */
     66 /* FIXME Remove when metadata restructuring is completed */
     67 //#define SNAPSHOT		0x00001000U	/* LV - internal use only */
     68 //#define PVMOVE			0x00002000U	/* VG LV SEG */
     69 //#define LOCKED			0x00004000U	/* LV */
     70 //#define MIRRORED		0x00008000U	/* LV - internal use only */
     71 #define VIRTUAL			0x00010000U	/* LV - internal use only */
     72 //#define MIRROR_LOG		0x00020000U	/* LV */
     73 //#define MIRROR_IMAGE		0x00040000U	/* LV */
     74 //#define MIRROR_NOTSYNCED	0x00080000U	/* LV */
     75 #define ACTIVATE_EXCL		0x00100000U	/* LV - internal use only */
     76 #define PRECOMMITTED		0x00200000U	/* VG - internal use only */
     77 //#define CONVERTING		0x00400000U	/* LV */
     78 
     79 //#define MISSING_PV		0x00800000U	/* PV */
     80 //#define PARTIAL_LV		0x01000000U	/* LV - derived flag, not
     81 //						   written out in metadata*/
     82 
     83 #define POSTORDER_FLAG		0x02000000U /* Not real flags, reserved for  */
     84 #define POSTORDER_OPEN_FLAG	0x04000000U /* temporary use inside vg_read. */
     85 
     86 //#define LVM_READ              	0x00000100U	/* LV VG */
     87 //#define LVM_WRITE             	0x00000200U	/* LV VG */
     88 //#define CLUSTERED         	0x00000400U	/* VG */
     89 #define SHARED            	0x00000800U	/* VG */
     90 
     91 /* Format features flags */
     92 //#define FMT_SEGMENTS		0x00000001U	/* Arbitrary segment params? */
     93 //#define FMT_MDAS		0x00000002U	/* Proper metadata areas? */
     94 //#define FMT_TAGS		0x00000004U	/* Tagging? */
     95 //#define FMT_UNLIMITED_VOLS	0x00000008U	/* Unlimited PVs/LVs? */
     96 //#define FMT_RESTRICTED_LVIDS	0x00000010U	/* LVID <= 255 */
     97 //#define FMT_ORPHAN_ALLOCATABLE	0x00000020U	/* Orphan PV allocatable? */
     98 #define FMT_PRECOMMIT		0x00000040U	/* Supports pre-commit? */
     99 //#define FMT_RESIZE_PV		0x00000080U	/* Supports pvresize? */
    100 //#define FMT_UNLIMITED_STRIPESIZE 0x00000100U	/* Unlimited stripe size? */
    101 
    102 struct metadata_area;
    103 
    104 /* Per-format per-metadata area operations */
    105 struct metadata_area_ops {
    106 	struct volume_group *(*vg_read) (struct format_instance * fi,
    107 					 const char *vg_name,
    108 					 struct metadata_area * mda);
    109 	struct volume_group *(*vg_read_precommit) (struct format_instance * fi,
    110 					 const char *vg_name,
    111 					 struct metadata_area * mda);
    112 	/*
    113 	 * Write out complete VG metadata.  You must ensure internal
    114 	 * consistency before calling. eg. PEs can't refer to PVs not
    115 	 * part of the VG.
    116 	 *
    117 	 * It is also the responsibility of the caller to ensure external
    118 	 * consistency, eg by calling pv_write() if removing PVs from
    119 	 * a VG or calling vg_write() a second time if splitting a VG
    120 	 * into two.
    121 	 *
    122 	 * vg_write() should not read or write from any PVs not included
    123 	 * in the volume_group structure it is handed.
    124 	 * (format1 currently breaks this rule.)
    125 	 */
    126 	int (*vg_write) (struct format_instance * fid, struct volume_group * vg,
    127 			 struct metadata_area * mda);
    128 	int (*vg_precommit) (struct format_instance * fid,
    129 			     struct volume_group * vg,
    130 			     struct metadata_area * mda);
    131 	int (*vg_commit) (struct format_instance * fid,
    132 			  struct volume_group * vg, struct metadata_area * mda);
    133 	int (*vg_revert) (struct format_instance * fid,
    134 			  struct volume_group * vg, struct metadata_area * mda);
    135 	int (*vg_remove) (struct format_instance * fi, struct volume_group * vg,
    136 			  struct metadata_area * mda);
    137 
    138 	/*
    139 	 * Returns number of free sectors in given metadata area.
    140 	 */
    141 	uint64_t (*mda_free_sectors) (struct metadata_area *mda);
    142 
    143 	/*
    144 	 * Returns number of total sectors in given metadata area.
    145 	 */
    146 	uint64_t (*mda_total_sectors) (struct metadata_area *mda);
    147 
    148 	/*
    149 	 * Check if metadata area belongs to vg
    150 	 */
    151 	int (*mda_in_vg) (struct format_instance * fi,
    152 			    struct volume_group * vg, struct metadata_area *mda);
    153 	/*
    154 	 * Analyze a metadata area on a PV.
    155 	 */
    156 	int (*pv_analyze_mda) (const struct format_type * fmt,
    157 			       struct metadata_area *mda);
    158 
    159 };
    160 
    161 struct metadata_area {
    162 	struct dm_list list;
    163 	struct metadata_area_ops *ops;
    164 	void *metadata_locn;
    165 };
    166 
    167 #define seg_pvseg(seg, s)	(seg)->areas[(s)].u.pv.pvseg
    168 #define seg_dev(seg, s)		(seg)->areas[(s)].u.pv.pvseg->pv->dev
    169 #define seg_pe(seg, s)		(seg)->areas[(s)].u.pv.pvseg->pe
    170 #define seg_le(seg, s)		(seg)->areas[(s)].u.lv.le
    171 
    172 struct name_list {
    173 	struct dm_list list;
    174 	char *name;
    175 };
    176 
    177 struct mda_list {
    178 	struct dm_list list;
    179 	struct device_area mda;
    180 };
    181 
    182 struct peg_list {
    183 	struct dm_list list;
    184 	struct pv_segment *peg;
    185 };
    186 
    187 struct seg_list {
    188 	struct dm_list list;
    189 	unsigned count;
    190 	struct lv_segment *seg;
    191 };
    192 
    193 /*
    194  * Ownership of objects passes to caller.
    195  */
    196 struct format_handler {
    197 	/*
    198 	 * Scan any metadata areas that aren't referenced in PV labels
    199 	 */
    200 	int (*scan) (const struct format_type * fmt);
    201 
    202 	/*
    203 	 * Return PV with given path.
    204 	 */
    205 	int (*pv_read) (const struct format_type * fmt, const char *pv_name,
    206 			struct physical_volume * pv, struct dm_list * mdas);
    207 
    208 	/*
    209 	 * Tweak an already filled out a pv ready for importing into a
    210 	 * vg.  eg. pe_count is format specific.
    211 	 */
    212 	int (*pv_setup) (const struct format_type * fmt,
    213 			 uint64_t pe_start, uint32_t extent_count,
    214 			 uint32_t extent_size,
    215 			 int pvmetadatacopies,
    216 			 uint64_t pvmetadatasize, struct dm_list * mdas,
    217 			 struct physical_volume * pv, struct volume_group * vg);
    218 
    219 	/*
    220 	 * Write a PV structure to disk. Fails if the PV is in a VG ie
    221 	 * pv->vg_name must be a valid orphan VG name
    222 	 */
    223 	int (*pv_write) (const struct format_type * fmt,
    224 			 struct physical_volume * pv, struct dm_list * mdas,
    225 			 int64_t label_sector);
    226 
    227 	/*
    228 	 * Tweak an already filled out a lv eg, check there
    229 	 * aren't too many extents.
    230 	 */
    231 	int (*lv_setup) (struct format_instance * fi,
    232 			 struct logical_volume * lv);
    233 
    234 	/*
    235 	 * Tweak an already filled out vg.  eg, max_pv is format
    236 	 * specific.
    237 	 */
    238 	int (*vg_setup) (struct format_instance * fi, struct volume_group * vg);
    239 
    240 	/*
    241 	 * Check whether particular segment type is supported.
    242 	 */
    243 	int (*segtype_supported) (struct format_instance *fid,
    244 				  const struct segment_type *segtype);
    245 
    246 	/*
    247 	 * Create format instance with a particular metadata area
    248 	 */
    249 	struct format_instance *(*create_instance) (const struct format_type *
    250 						    fmt, const char *vgname,
    251 						    const char *vgid,
    252 						    void *context);
    253 
    254 	/*
    255 	 * Destructor for format instance
    256 	 */
    257 	void (*destroy_instance) (struct format_instance * fid);
    258 
    259 	/*
    260 	 * Destructor for format type
    261 	 */
    262 	void (*destroy) (const struct format_type * fmt);
    263 };
    264 
    265 /*
    266  * Utility functions
    267  */
    268 unsigned long pe_align(struct physical_volume *pv);
    269 int vg_validate(struct volume_group *vg);
    270 
    271 int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv);
    272 
    273 /* Manipulate PV structures */
    274 int pv_add(struct volume_group *vg, struct physical_volume *pv);
    275 int pv_remove(struct volume_group *vg, struct physical_volume *pv);
    276 struct physical_volume *pv_find(struct volume_group *vg, const char *pv_name);
    277 
    278 /* Find a PV within a given VG */
    279 int get_pv_from_vg_by_id(const struct format_type *fmt, const char *vg_name,
    280 			 const char *vgid, const char *pvid,
    281 			 struct physical_volume *pv);
    282 
    283 struct lv_list *find_lv_in_vg_by_lvid(struct volume_group *vg,
    284 				      const union lvid *lvid);
    285 
    286 struct lv_list *find_lv_in_lv_list(const struct dm_list *ll,
    287 				   const struct logical_volume *lv);
    288 
    289 /* Return the VG that contains a given LV (based on path given in lv_name) */
    290 /* or environment var */
    291 struct volume_group *find_vg_with_lv(const char *lv_name);
    292 
    293 /* Find LV with given lvid (used during activation) */
    294 struct logical_volume *lv_from_lvid(struct cmd_context *cmd,
    295 				    const char *lvid_s,
    296 				    unsigned precommitted);
    297 
    298 /* FIXME Merge these functions with ones above */
    299 struct physical_volume *find_pv(struct volume_group *vg, struct device *dev);
    300 
    301 struct pv_list *find_pv_in_pv_list(const struct dm_list *pl,
    302 				   const struct physical_volume *pv);
    303 
    304 /* Find LV segment containing given LE */
    305 struct lv_segment *find_seg_by_le(const struct logical_volume *lv, uint32_t le);
    306 
    307 /* Find PV segment containing given LE */
    308 struct pv_segment *find_peg_by_pe(const struct physical_volume *pv, uint32_t pe);
    309 
    310 /*
    311  * Remove a dev_dir if present.
    312  */
    313 const char *strip_dir(const char *vg_name, const char *dir);
    314 
    315 /*
    316  * Checks that an lv has no gaps or overlapping segments.
    317  * Set complete_vg to perform additional VG level checks.
    318  */
    319 int check_lv_segments(struct logical_volume *lv, int complete_vg);
    320 
    321 /*
    322  * Sometimes (eg, after an lvextend), it is possible to merge two
    323  * adjacent segments into a single segment.  This function trys
    324  * to merge as many segments as possible.
    325  */
    326 int lv_merge_segments(struct logical_volume *lv);
    327 
    328 /*
    329  * Ensure there's a segment boundary at a given LE, splitting if necessary
    330  */
    331 int lv_split_segment(struct logical_volume *lv, uint32_t le);
    332 
    333 /*
    334  * Add/remove upward link from underlying LV to the segment using it
    335  * FIXME: ridiculously long name
    336  */
    337 int add_seg_to_segs_using_this_lv(struct logical_volume *lv, struct lv_segment *seg);
    338 int remove_seg_from_segs_using_this_lv(struct logical_volume *lv, struct lv_segment *seg);
    339 struct lv_segment *get_only_segment_using_this_lv(struct logical_volume *lv);
    340 
    341 /*
    342  * Count LVs that are visible from user's perspective.
    343  */
    344 unsigned displayable_lvs_in_vg(const struct volume_group *vg);
    345 
    346 /*
    347  * For internal metadata caching.
    348  */
    349 int export_vg_to_buffer(struct volume_group *vg, char **buf);
    350 struct volume_group *import_vg_from_buffer(char *buf,
    351 					   struct format_instance *fid);
    352 
    353 /*
    354  * Mirroring functions
    355  */
    356 
    357 /*
    358  * Given mirror image or mirror log segment, find corresponding mirror segment
    359  */
    360 int fixup_imported_mirrors(struct volume_group *vg);
    361 
    362 /*
    363  * Begin skeleton for external LVM library
    364  */
    365 struct id pv_id(const pv_t *pv);
    366 const struct format_type *pv_format_type(const pv_t *pv);
    367 struct id pv_vgid(const pv_t *pv);
    368 
    369 pv_t *pv_by_path(struct cmd_context *cmd, const char *pv_name);
    370 int add_pv_to_vg(struct volume_group *vg, const char *pv_name,
    371 		 struct physical_volume *pv);
    372 
    373 #endif
    374