Tizen 2.1 base
[external/device-mapper.git] / lib / format1 / format1.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2007 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 #include "lib.h"
17 #include "disk-rep.h"
18 #include "limits.h"
19 #include "display.h"
20 #include "toolcontext.h"
21 #include "lvm1-label.h"
22 #include "format1.h"
23 #include "segtype.h"
24 #include "pv_alloc.h"
25
26 /* VG consistency checks */
27 static int _check_vgs(struct dm_list *pvs, struct volume_group *vg)
28 {
29         struct dm_list *pvh, *t;
30         struct disk_list *dl = NULL;
31         struct disk_list *first = NULL;
32
33         uint32_t pv_count = 0;
34         uint32_t exported = 0;
35         int first_time = 1;
36
37         /*
38          * If there are exported and unexported PVs, ignore exported ones.
39          * This means an active VG won't be affected if disks are inserted
40          * bearing an exported VG with the same name.
41          */
42         dm_list_iterate_items(dl, pvs) {
43                 if (first_time) {
44                         exported = dl->pvd.pv_status & VG_EXPORTED;
45                         first_time = 0;
46                         continue;
47                 }
48
49                 if (exported != (dl->pvd.pv_status & VG_EXPORTED)) {
50                         /* Remove exported PVs */
51                         dm_list_iterate_safe(pvh, t, pvs) {
52                                 dl = dm_list_item(pvh, struct disk_list);
53                                 if (dl->pvd.pv_status & VG_EXPORTED)
54                                         dm_list_del(pvh);
55                         }
56                         break;
57                 }
58         }
59
60         /* Remove any PVs with VG structs that differ from the first */
61         dm_list_iterate_safe(pvh, t, pvs) {
62                 dl = dm_list_item(pvh, struct disk_list);
63
64                 if (!first)
65                         first = dl;
66
67                 else if (memcmp(&first->vgd, &dl->vgd, sizeof(first->vgd))) {
68                         log_error("VG data differs between PVs %s and %s",
69                                   dev_name(first->dev), dev_name(dl->dev));
70                         log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32
71                                   "  %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
72                                   PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
73                                   " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
74                                   PRIu32 " %" PRIu32 " %" PRIu32,
75                                   dev_name(first->dev), first->vgd.vg_uuid,
76                                   first->vgd.vg_name_dummy,
77                                   first->vgd.vg_number, first->vgd.vg_access,
78                                   first->vgd.vg_status, first->vgd.lv_max,
79                                   first->vgd.lv_cur, first->vgd.lv_open,
80                                   first->vgd.pv_max, first->vgd.pv_cur,
81                                   first->vgd.pv_act, first->vgd.dummy,
82                                   first->vgd.vgda, first->vgd.pe_size,
83                                   first->vgd.pe_total, first->vgd.pe_allocated,
84                                   first->vgd.pvg_total);
85                         log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32
86                                   "  %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
87                                   PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
88                                   " %" PRIu32 " %" PRIu32 " %" PRIu32 " %"
89                                   PRIu32 " %" PRIu32 " %" PRIu32,
90                                   dev_name(dl->dev), dl->vgd.vg_uuid,
91                                   dl->vgd.vg_name_dummy, dl->vgd.vg_number,
92                                   dl->vgd.vg_access, dl->vgd.vg_status,
93                                   dl->vgd.lv_max, dl->vgd.lv_cur,
94                                   dl->vgd.lv_open, dl->vgd.pv_max,
95                                   dl->vgd.pv_cur, dl->vgd.pv_act, dl->vgd.dummy,
96                                   dl->vgd.vgda, dl->vgd.pe_size,
97                                   dl->vgd.pe_total, dl->vgd.pe_allocated,
98                                   dl->vgd.pvg_total);
99                         dm_list_del(pvh);
100                         return 0;
101                 }
102                 pv_count++;
103         }
104
105         /* On entry to fn, list known to be non-empty */
106         if (pv_count != first->vgd.pv_cur) {
107                 log_error("%d PV(s) found for VG %s: expected %d",
108                           pv_count, first->pvd.vg_name, first->vgd.pv_cur);
109                 vg->status |= PARTIAL_VG;
110         }
111
112         return 1;
113 }
114
115 static int _fix_partial_vg(struct volume_group *vg, struct dm_list *pvs)
116 {
117         uint32_t extent_count = 0;
118         struct disk_list *dl;
119         struct dm_list *pvh;
120         struct pv_list *pvl;
121         struct lv_list *ll;
122         struct lv_segment *seg;
123
124         /*
125          * FIXME: code should remap missing segments to error segment.
126          * Also current mapping code allocates 1 segment per missing extent.
127          * For now bail out completely - allocated structures are not complete
128          */
129         dm_list_iterate_items(ll, &vg->lvs)
130                 dm_list_iterate_items(seg, &ll->lv->segments) {
131
132                         /* area_count is always 1 here, s == 0 */
133                         if (seg_type(seg, 0) != AREA_PV)
134                                 continue;
135
136                         if (seg_pv(seg, 0))
137                                 continue;
138
139                         log_error("Partial mode support for missing lvm1 PVs and "
140                                   "partially available LVs is currently not implemented.");
141                         return 0;
142         }
143
144         dm_list_iterate(pvh, pvs) {
145                 dl = dm_list_item(pvh, struct disk_list);
146                 extent_count += dl->pvd.pe_total;
147         }
148
149         /* FIXME: move this to one place to pv_manip */
150         if (!(pvl = dm_pool_zalloc(vg->vgmem, sizeof(*pvl))) ||
151             !(pvl->pv = dm_pool_zalloc(vg->vgmem, sizeof(*pvl->pv))))
152                 return_0;
153
154         /* Use vg uuid with replaced first chars to "missing" as missing PV UUID */
155         memcpy(&pvl->pv->id.uuid, vg->id.uuid, sizeof(pvl->pv->id.uuid));
156         memcpy(&pvl->pv->id.uuid, "missing", 7);
157
158         if (!(pvl->pv->vg_name = dm_pool_strdup(vg->vgmem, vg->name)))
159                 goto_out;
160         memcpy(&pvl->pv->vgid, &vg->id, sizeof(vg->id));
161         pvl->pv->status |= MISSING_PV;
162         dm_list_init(&pvl->pv->tags);
163         dm_list_init(&pvl->pv->segments);
164
165         pvl->pv->pe_size = vg->extent_size;
166         pvl->pv->pe_count = vg->extent_count - extent_count;
167         if (!alloc_pv_segment_whole_pv(vg->vgmem, pvl->pv))
168                 goto_out;
169
170         add_pvl_to_vgs(vg, pvl);
171         log_debug("%s: partial VG, allocated missing PV using %d extents.",
172                   vg->name, pvl->pv->pe_count);
173
174         return 1;
175 out:
176         dm_pool_free(vg->vgmem, pvl);
177         return 0;
178 }
179
180 static struct volume_group *_build_vg(struct format_instance *fid,
181                                       struct dm_list *pvs,
182                                       struct dm_pool *mem)
183 {
184         struct volume_group *vg = dm_pool_zalloc(mem, sizeof(*vg));
185         struct disk_list *dl;
186
187         if (!vg)
188                 goto_bad;
189
190         if (dm_list_empty(pvs))
191                 goto_bad;
192
193         vg->cmd = fid->fmt->cmd;
194         vg->vgmem = mem;
195         vg->fid = fid;
196         vg->seqno = 0;
197         dm_list_init(&vg->pvs);
198         dm_list_init(&vg->lvs);
199         dm_list_init(&vg->tags);
200         dm_list_init(&vg->removed_pvs);
201
202         if (!_check_vgs(pvs, vg))
203                 goto_bad;
204
205         dl = dm_list_item(pvs->n, struct disk_list);
206
207         if (!import_vg(mem, vg, dl))
208                 goto_bad;
209
210         if (!import_pvs(fid->fmt, mem, vg, pvs))
211                 goto_bad;
212
213         if (!import_lvs(mem, vg, pvs))
214                 goto_bad;
215
216         if (!import_extents(fid->fmt->cmd, vg, pvs))
217                 goto_bad;
218
219         if (!import_snapshots(mem, vg, pvs))
220                 goto_bad;
221
222         /* Fix extents counts by adding missing PV if partial VG */
223         if ((vg->status & PARTIAL_VG) && !_fix_partial_vg(vg, pvs))
224                 goto_bad;
225
226         return vg;
227
228       bad:
229         dm_pool_free(mem, vg);
230         return NULL;
231 }
232
233 static struct volume_group *_format1_vg_read(struct format_instance *fid,
234                                      const char *vg_name,
235                                      struct metadata_area *mda __attribute__((unused)))
236 {
237         struct dm_pool *mem = dm_pool_create("lvm1 vg_read", VG_MEMPOOL_CHUNK);
238         struct dm_list pvs;
239         struct volume_group *vg = NULL;
240         dm_list_init(&pvs);
241
242         if (!mem)
243                 return_NULL;
244
245         /* Strip dev_dir if present */
246         vg_name = strip_dir(vg_name, fid->fmt->cmd->dev_dir);
247
248         if (!read_pvs_in_vg
249             (fid->fmt, vg_name, fid->fmt->cmd->filter, mem, &pvs))
250                 goto_bad;
251
252         if (!(vg = _build_vg(fid, &pvs, mem)))
253                 goto_bad;
254
255         return vg;
256 bad:
257         dm_pool_destroy(mem);
258         return NULL;
259 }
260
261 static struct disk_list *_flatten_pv(struct format_instance *fid,
262                                      struct dm_pool *mem, struct volume_group *vg,
263                                      struct physical_volume *pv,
264                                      const char *dev_dir)
265 {
266         struct disk_list *dl = dm_pool_alloc(mem, sizeof(*dl));
267
268         if (!dl)
269                 return_NULL;
270
271         dl->mem = mem;
272         dl->dev = pv->dev;
273
274         dm_list_init(&dl->uuids);
275         dm_list_init(&dl->lvds);
276
277         if (!export_pv(fid->fmt->cmd, mem, vg, &dl->pvd, pv) ||
278             !export_vg(&dl->vgd, vg) ||
279             !export_uuids(dl, vg) ||
280             !export_lvs(dl, vg, pv, dev_dir) || !calculate_layout(dl)) {
281                 dm_pool_free(mem, dl);
282                 return_NULL;
283         }
284
285         return dl;
286 }
287
288 static int _flatten_vg(struct format_instance *fid, struct dm_pool *mem,
289                        struct volume_group *vg,
290                        struct dm_list *pvds, const char *dev_dir,
291                        struct dev_filter *filter)
292 {
293         struct pv_list *pvl;
294         struct disk_list *data;
295
296         dm_list_iterate_items(pvl, &vg->pvs) {
297                 if (!(data = _flatten_pv(fid, mem, vg, pvl->pv, dev_dir)))
298                         return_0;
299
300                 dm_list_add(pvds, &data->list);
301         }
302
303         export_numbers(pvds, vg);
304         export_pv_act(pvds);
305
306         if (!export_vg_number(fid, pvds, vg->name, filter))
307                 return_0;
308
309         return 1;
310 }
311
312 static int _format1_vg_write(struct format_instance *fid, struct volume_group *vg,
313                      struct metadata_area *mda __attribute__((unused)))
314 {
315         struct dm_pool *mem = dm_pool_create("lvm1 vg_write", VG_MEMPOOL_CHUNK);
316         struct dm_list pvds;
317         int r = 0;
318
319         if (!mem)
320                 return_0;
321
322         dm_list_init(&pvds);
323
324         r = (_flatten_vg(fid, mem, vg, &pvds, fid->fmt->cmd->dev_dir,
325                          fid->fmt->cmd->filter) &&
326              write_disks(fid->fmt, &pvds));
327
328         lvmcache_update_vg(vg, 0);
329         dm_pool_destroy(mem);
330         return r;
331 }
332
333 static int _format1_pv_read(const struct format_type *fmt, const char *pv_name,
334                     struct physical_volume *pv, struct dm_list *mdas __attribute__((unused)),
335                     int scan_label_only __attribute__((unused)))
336 {
337         struct dm_pool *mem = dm_pool_create("lvm1 pv_read", 1024);
338         struct disk_list *dl;
339         struct device *dev;
340         int r = 0;
341
342         log_very_verbose("Reading physical volume data %s from disk", pv_name);
343
344         if (!mem)
345                 return_0;
346
347         if (!(dev = dev_cache_get(pv_name, fmt->cmd->filter)))
348                 goto_out;
349
350         if (!(dl = read_disk(fmt, dev, mem, NULL)))
351                 goto_out;
352
353         if (!import_pv(fmt, fmt->cmd->mem, dl->dev, NULL, pv, &dl->pvd, &dl->vgd))
354                 goto_out;
355
356         pv->fmt = fmt;
357
358         r = 1;
359
360       out:
361         dm_pool_destroy(mem);
362         return r;
363 }
364
365 static int _format1_pv_setup(const struct format_type *fmt,
366                              uint64_t pe_start, uint32_t extent_count,
367                              uint32_t extent_size,
368                              unsigned long data_alignment __attribute__((unused)),
369                              unsigned long data_alignment_offset __attribute__((unused)),
370                              int pvmetadatacopies __attribute__((unused)),
371                              uint64_t pvmetadatasize __attribute__((unused)),
372                              unsigned metadataignore __attribute__((unused)),
373                              struct dm_list *mdas __attribute__((unused)),
374                              struct physical_volume *pv, struct volume_group *vg __attribute__((unused)))
375 {
376         if (pv->size > MAX_PV_SIZE)
377                 pv->size--;
378         if (pv->size > MAX_PV_SIZE) {
379                 log_error("Physical volumes cannot be bigger than %s",
380                           display_size(fmt->cmd, (uint64_t) MAX_PV_SIZE));
381                 return 0;
382         }
383
384         /* Nothing more to do if extent size isn't provided */
385         if (!extent_size)
386                 return 1;
387
388         /*
389          * This works out pe_start and pe_count.
390          */
391         if (!calculate_extent_count(pv, extent_size, extent_count, pe_start))
392                 return_0;
393
394         /* Retain existing extent locations exactly */
395         if (((pe_start || extent_count) && (pe_start != pv->pe_start)) ||
396             (extent_count && (extent_count != pv->pe_count))) {
397                 log_error("Metadata would overwrite physical extents");
398                 return 0;
399         }
400
401         return 1;
402 }
403
404 static int _format1_lv_setup(struct format_instance *fid, struct logical_volume *lv)
405 {
406         uint64_t max_size = UINT_MAX;
407
408         if (!*lv->lvid.s)
409                 lvid_from_lvnum(&lv->lvid, &lv->vg->id, find_free_lvnum(lv));
410
411         if (lv->le_count > MAX_LE_TOTAL) {
412                 log_error("logical volumes cannot contain more than "
413                           "%d extents.", MAX_LE_TOTAL);
414                 return 0;
415         }
416         if (lv->size > max_size) {
417                 log_error("logical volumes cannot be larger than %s",
418                           display_size(fid->fmt->cmd, max_size));
419                 return 0;
420         }
421
422         return 1;
423 }
424
425 static int _format1_pv_write(const struct format_type *fmt, struct physical_volume *pv,
426                      struct dm_list *mdas __attribute__((unused)), int64_t sector __attribute__((unused)))
427 {
428         struct dm_pool *mem;
429         struct disk_list *dl;
430         struct dm_list pvs;
431         struct lvmcache_info *info;
432
433         if (!(info = lvmcache_add(fmt->labeller, (char *) &pv->id, pv->dev,
434                                   pv->vg_name, NULL, 0)))
435                 return_0;
436
437         info->device_size = pv->size << SECTOR_SHIFT;
438         info->fmt = fmt;
439
440         dm_list_init(&info->mdas);
441
442         dm_list_init(&pvs);
443
444         /* Ensure any residual PE structure is gone */
445         pv->pe_size = pv->pe_count = 0;
446         pv->pe_start = LVM1_PE_ALIGN;
447
448         if (!(mem = dm_pool_create("lvm1 pv_write", 1024)))
449                 return_0;
450
451         if (!(dl = dm_pool_alloc(mem, sizeof(*dl))))
452                 goto_bad;
453
454         dl->mem = mem;
455         dl->dev = pv->dev;
456
457         if (!export_pv(fmt->cmd, mem, NULL, &dl->pvd, pv))
458                 goto_bad;
459
460         /* must be set to be able to zero gap after PV structure in
461            dev_write in order to make other disk tools happy */
462         dl->pvd.pv_on_disk.base = METADATA_BASE;
463         dl->pvd.pv_on_disk.size = PV_SIZE;
464         dl->pvd.pe_on_disk.base = LVM1_PE_ALIGN << SECTOR_SHIFT;
465
466         dm_list_add(&pvs, &dl->list);
467         if (!write_disks(fmt, &pvs))
468                 goto_bad;
469
470         dm_pool_destroy(mem);
471         return 1;
472
473       bad:
474         dm_pool_destroy(mem);
475         return 0;
476 }
477
478 static int _format1_vg_setup(struct format_instance *fid, struct volume_group *vg)
479 {
480         /* just check max_pv and max_lv */
481         if (!vg->max_lv || vg->max_lv >= MAX_LV)
482                 vg->max_lv = MAX_LV - 1;
483
484         if (!vg->max_pv || vg->max_pv >= MAX_PV)
485                 vg->max_pv = MAX_PV - 1;
486
487         if (vg->extent_size > MAX_PE_SIZE || vg->extent_size < MIN_PE_SIZE) {
488                 log_error("Extent size must be between %s and %s",
489                           display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE),
490                           display_size(fid->fmt->cmd, (uint64_t) MAX_PE_SIZE));
491
492                 return 0;
493         }
494
495         if (vg->extent_size % MIN_PE_SIZE) {
496                 log_error("Extent size must be multiple of %s",
497                           display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE));
498                 return 0;
499         }
500
501         /* Redundant? */
502         if (vg->extent_size & (vg->extent_size - 1)) {
503                 log_error("Extent size must be power of 2");
504                 return 0;
505         }
506
507         return 1;
508 }
509
510 static int _format1_segtype_supported(struct format_instance *fid __attribute__((unused)),
511                                       const struct segment_type *segtype)
512 {
513         if (!(segtype->flags & SEG_FORMAT1_SUPPORT))
514                 return_0;
515
516         return 1;
517 }
518
519 static struct metadata_area_ops _metadata_format1_ops = {
520         .vg_read = _format1_vg_read,
521         .vg_write = _format1_vg_write,
522 };
523
524 static struct format_instance *_format1_create_instance(const struct format_type *fmt,
525                                                 const char *vgname __attribute__((unused)),
526                                                 const char *vgid __attribute__((unused)),
527                                                 void *private __attribute__((unused)))
528 {
529         struct format_instance *fid;
530         struct metadata_area *mda;
531
532         if (!(fid = dm_pool_alloc(fmt->cmd->mem, sizeof(*fid))))
533                 return_NULL;
534
535         fid->fmt = fmt;
536         dm_list_init(&fid->metadata_areas_in_use);
537         dm_list_init(&fid->metadata_areas_ignored);
538
539         /* Define a NULL metadata area */
540         if (!(mda = dm_pool_zalloc(fmt->cmd->mem, sizeof(*mda)))) {
541                 dm_pool_free(fmt->cmd->mem, fid);
542                 return_NULL;
543         }
544
545         mda->ops = &_metadata_format1_ops;
546         mda->metadata_locn = NULL;
547         mda->status = 0;
548         dm_list_add(&fid->metadata_areas_in_use, &mda->list);
549
550         return fid;
551 }
552
553 static void _format1_destroy_instance(struct format_instance *fid __attribute__((unused)))
554 {
555 }
556
557 static void _format1_destroy(struct format_type *fmt)
558 {
559         dm_free(fmt);
560 }
561
562 static struct format_handler _format1_ops = {
563         .pv_read = _format1_pv_read,
564         .pv_setup = _format1_pv_setup,
565         .pv_write = _format1_pv_write,
566         .lv_setup = _format1_lv_setup,
567         .vg_setup = _format1_vg_setup,
568         .segtype_supported = _format1_segtype_supported,
569         .create_instance = _format1_create_instance,
570         .destroy_instance = _format1_destroy_instance,
571         .destroy = _format1_destroy,
572 };
573
574 #ifdef LVM1_INTERNAL
575 struct format_type *init_lvm1_format(struct cmd_context *cmd)
576 #else                           /* Shared */
577 struct format_type *init_format(struct cmd_context *cmd);
578 struct format_type *init_format(struct cmd_context *cmd)
579 #endif
580 {
581         struct format_type *fmt = dm_malloc(sizeof(*fmt));
582
583         if (!fmt)
584                 return_NULL;
585
586         fmt->cmd = cmd;
587         fmt->ops = &_format1_ops;
588         fmt->name = FMT_LVM1_NAME;
589         fmt->alias = NULL;
590         fmt->orphan_vg_name = FMT_LVM1_ORPHAN_VG_NAME;
591         fmt->features = FMT_RESTRICTED_LVIDS | FMT_ORPHAN_ALLOCATABLE |
592                         FMT_RESTRICTED_READAHEAD;
593         fmt->private = NULL;
594
595         if (!(fmt->labeller = lvm1_labeller_create(fmt))) {
596                 log_error("Couldn't create lvm1 label handler.");
597                 return NULL;
598         }
599
600         if (!(label_register_handler(FMT_LVM1_NAME, fmt->labeller))) {
601                 log_error("Couldn't register lvm1 label handler.");
602                 return NULL;
603         }
604
605         log_very_verbose("Initialised format: %s", fmt->name);
606
607         return fmt;
608 }