import source from lvm2 2.02.79
[external/device-mapper.git] / lib / metadata / metadata.h
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
3  * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
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.
10  *
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
14  */
15
16 /*
17  * This is the in core representation of a volume group and its
18  * associated physical and logical volumes.
19  */
20
21 #ifndef _LVM_METADATA_H
22 #define _LVM_METADATA_H
23
24 #include "ctype.h"
25 #include "dev-cache.h"
26 #include "lvm-string.h"
27 #include "metadata-exported.h"
28
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 */
39
40 /*
41  * Ceiling(n / sz)
42  */
43 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
44
45 /*
46  * Ceiling(n / size) * size
47  */
48 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
49
50
51 /* Various flags */
52 /* Note that the bits no longer necessarily correspond to LVM1 disk format */
53
54 //#define PARTIAL_VG            0x00000001U     /* VG */
55 //#define EXPORTED_VG           0x00000002U     /* VG PV */
56 //#define RESIZEABLE_VG         0x00000004U     /* VG */
57
58 /* May any free extents on this PV be used or must they be left free? */
59 //#define ALLOCATABLE_PV                0x00000008U     /* PV */
60
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 */
77
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*/
82
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 */
86
87 //#define LVM_READ                      0x00000100U     /* LV VG */
88 //#define LVM_WRITE                     0x00000200U     /* LV VG */
89 //#define CLUSTERED             0x00000400U     /* VG */
90 #define SHARED                  0x00000800U     /* VG */
91
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? */
102
103 struct metadata_area;
104
105 /* Per-format per-metadata area operations */
106 struct metadata_area_ops {
107         struct volume_group *(*vg_read) (struct format_instance * fi,
108                                          const char *vg_name,
109                                          struct metadata_area * mda);
110         struct volume_group *(*vg_read_precommit) (struct format_instance * fi,
111                                          const char *vg_name,
112                                          struct metadata_area * mda);
113         /*
114          * Write out complete VG metadata.  You must ensure internal
115          * consistency before calling. eg. PEs can't refer to PVs not
116          * part of the VG.
117          *
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
121          * into two.
122          *
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.)
126          */
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);
138
139         /*
140          * Per location copy constructor.
141          */
142         void *(*mda_metadata_locn_copy) (struct dm_pool *mem, void *metadata_locn);
143
144         /*
145          * Per location description for logging.
146          */
147         const char *(*mda_metadata_locn_name) (void *metadata_locn);
148         uint64_t (*mda_metadata_locn_offset) (void *metadata_locn);
149
150         /*
151          * Returns number of free sectors in given metadata area.
152          */
153         uint64_t (*mda_free_sectors) (struct metadata_area *mda);
154
155         /*
156          * Returns number of total sectors in given metadata area.
157          */
158         uint64_t (*mda_total_sectors) (struct metadata_area *mda);
159
160         /*
161          * Check if metadata area belongs to vg
162          */
163         int (*mda_in_vg) (struct format_instance * fi,
164                             struct volume_group * vg, struct metadata_area *mda);
165         /*
166          * Analyze a metadata area on a PV.
167          */
168         int (*pv_analyze_mda) (const struct format_type * fmt,
169                                struct metadata_area *mda);
170
171         /*
172          * Do these two metadata_area structures match with respect to
173          * their underlying location?
174          */
175         unsigned (*mda_locns_match)(struct metadata_area *mda1,
176                                     struct metadata_area *mda2);
177 };
178
179 #define MDA_IGNORED 0x00000001
180
181 struct metadata_area {
182         struct dm_list list;
183         struct metadata_area_ops *ops;
184         void *metadata_locn;
185         uint32_t status;
186 };
187 struct metadata_area *mda_copy(struct dm_pool *mem,
188                                struct metadata_area *mda);
189
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);
196
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
201
202 struct name_list {
203         struct dm_list list;
204         char *name;
205 };
206
207 struct mda_list {
208         struct dm_list list;
209         struct device_area mda;
210 };
211
212 struct peg_list {
213         struct dm_list list;
214         struct pv_segment *peg;
215 };
216
217 struct seg_list {
218         struct dm_list list;
219         unsigned count;
220         struct lv_segment *seg;
221 };
222
223 /*
224  * Ownership of objects passes to caller.
225  */
226 struct format_handler {
227         /*
228          * Scan any metadata areas that aren't referenced in PV labels
229          */
230         int (*scan) (const struct format_type * fmt, const char *vgname);
231
232         /*
233          * Return PV with given path.
234          */
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);
238
239         /*
240          * Tweak an already filled out a pv ready for importing into a
241          * vg.  eg. pe_count is format specific.
242          */
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);
250
251         /*
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
254          */
255         int (*pv_write) (const struct format_type * fmt,
256                          struct physical_volume * pv, struct dm_list * mdas,
257                          int64_t label_sector);
258
259         /*
260          * Tweak an already filled out a lv eg, check there
261          * aren't too many extents.
262          */
263         int (*lv_setup) (struct format_instance * fi,
264                          struct logical_volume * lv);
265
266         /*
267          * Tweak an already filled out vg.  eg, max_pv is format
268          * specific.
269          */
270         int (*vg_setup) (struct format_instance * fi, struct volume_group * vg);
271
272         /*
273          * Check whether particular segment type is supported.
274          */
275         int (*segtype_supported) (struct format_instance *fid,
276                                   const struct segment_type *segtype);
277
278         /*
279          * Create format instance with a particular metadata area
280          */
281         struct format_instance *(*create_instance) (const struct format_type *
282                                                     fmt, const char *vgname,
283                                                     const char *vgid,
284                                                     void *context);
285
286         /*
287          * Destructor for format instance
288          */
289         void (*destroy_instance) (struct format_instance * fid);
290
291         /*
292          * Destructor for format type
293          */
294         void (*destroy) (struct format_type * fmt);
295 };
296
297 /*
298  * Utility functions
299  */
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);
304
305 int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv);
306
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);
311
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);
316
317 struct lv_list *find_lv_in_vg_by_lvid(struct volume_group *vg,
318                                       const union lvid *lvid);
319
320 struct lv_list *find_lv_in_lv_list(const struct dm_list *ll,
321                                    const struct logical_volume *lv);
322
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);
326
327 /* Find LV with given lvid (used during activation) */
328 struct logical_volume *lv_from_lvid(struct cmd_context *cmd,
329                                     const char *lvid_s,
330                                     unsigned precommitted);
331
332 /* FIXME Merge these functions with ones above */
333 struct physical_volume *find_pv(struct volume_group *vg, struct device *dev);
334
335 struct pv_list *find_pv_in_pv_list(const struct dm_list *pl,
336                                    const struct physical_volume *pv);
337
338 /* Find LV segment containing given LE */
339 struct lv_segment *find_seg_by_le(const struct logical_volume *lv, uint32_t le);
340
341 /*
342  * Remove a dev_dir if present.
343  */
344 const char *strip_dir(const char *vg_name, const char *dir);
345
346 struct logical_volume *alloc_lv(struct dm_pool *mem);
347
348 /*
349  * Checks that an lv has no gaps or overlapping segments.
350  * Set complete_vg to perform additional VG level checks.
351  */
352 int check_lv_segments(struct logical_volume *lv, int complete_vg);
353
354
355 /*
356  * Checks that a replicator segment is correct.
357  */
358 int check_replicator_segment(const struct lv_segment *replicator_seg);
359
360 /*
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.
364  */
365 int lv_merge_segments(struct logical_volume *lv);
366
367 /*
368  * Ensure there's a segment boundary at a given LE, splitting if necessary
369  */
370 int lv_split_segment(struct logical_volume *lv, uint32_t le);
371
372 /*
373  * Add/remove upward link from underlying LV to the segment using it
374  * FIXME: ridiculously long name
375  */
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);
379
380 /*
381  * Calculate readahead from underlying PV devices
382  */
383 void lv_calculate_readahead(const struct logical_volume *lv, uint32_t *read_ahead);
384
385 /*
386  * For internal metadata caching.
387  */
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);
391
392 /*
393  * Mirroring functions
394  */
395
396 /*
397  * Given mirror image or mirror log segment, find corresponding mirror segment 
398  */
399 int fixup_imported_mirrors(struct volume_group *vg);
400
401 /*
402  * Begin skeleton for external LVM library
403  */
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);
407
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);
413
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);
416
417 #endif