blkdeactivate.sh: Add PATH environment variable into script
[platform/upstream/device-mapper.git] / lib / format_text / archiver.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14  */
15
16 #include "lib/misc/lib.h"
17 #include "lib/format_text/archiver.h"
18 #include "lib/format_text/format-text.h"
19 #include "lib/misc/lvm-string.h"
20 #include "lib/misc/lvm-signal.h"
21 #include "lib/cache/lvmcache.h"
22 #include "lib/mm/memlock.h"
23 #include "lib/commands/toolcontext.h"
24 #include "lib/locking/locking.h"
25
26 #include <unistd.h>
27
28 struct archive_params {
29         int enabled;
30         char *dir;
31         unsigned int keep_days;
32         unsigned int keep_number;
33 };
34
35 struct backup_params {
36         int enabled;
37         char *dir;
38         int suppress;
39 };
40
41 int archive_init(struct cmd_context *cmd, const char *dir,
42                  unsigned int keep_days, unsigned int keep_min,
43                  int enabled)
44 {
45         archive_exit(cmd);
46
47         if (!(cmd->archive_params = dm_pool_zalloc(cmd->libmem,
48                                                 sizeof(*cmd->archive_params)))) {
49                 log_error("archive_params alloc failed");
50                 return 0;
51         }
52
53         if (!*dir)
54                 return 1;
55
56         if (!(cmd->archive_params->dir = strdup(dir))) {
57                 log_error("Couldn't copy archive directory name.");
58                 return 0;
59         }
60
61         cmd->archive_params->keep_days = keep_days;
62         cmd->archive_params->keep_number = keep_min;
63         archive_enable(cmd, enabled);
64
65         return 1;
66 }
67
68 void archive_exit(struct cmd_context *cmd)
69 {
70         if (!cmd->archive_params)
71                 return;
72         free(cmd->archive_params->dir);
73         memset(cmd->archive_params, 0, sizeof(*cmd->archive_params));
74 }
75
76 void archive_enable(struct cmd_context *cmd, int flag)
77 {
78         cmd->archive_params->enabled = flag;
79 }
80
81 static char *_build_desc(struct dm_pool *mem, const char *line, int before)
82 {
83         size_t len = strlen(line) + 32;
84         char *buffer;
85
86         if (!(buffer = dm_pool_alloc(mem, len))) {
87                 log_error("Failed to allocate desc.");
88                 return NULL;
89         }
90
91         if (dm_snprintf(buffer, len, "Created %s executing '%s'",
92                         before ? "*before*" : "*after*", line) < 0) {
93                 log_error("Failed to build desc.");
94                 return NULL;
95         }
96
97         return buffer;
98 }
99
100 static int _archive(struct volume_group *vg, int compulsory)
101 {
102         char *desc;
103
104         if (vg_is_archived(vg))
105                 return 1; /* VG has been already archived */
106
107         /* Don't archive orphan VGs. */
108         if (is_orphan_vg(vg->name))
109                 return 1;
110
111         if (!vg->cmd->archive_params->enabled || !vg->cmd->archive_params->dir) {
112                 vg->status |= ARCHIVED_VG;
113                 return 1;
114         }
115
116         if (test_mode()) {
117                 vg->status |= ARCHIVED_VG;
118                 log_verbose("Test mode: Skipping archiving of volume group.");
119                 return 1;
120         }
121
122         if (!dm_create_dir(vg->cmd->archive_params->dir)) {
123                 if (compulsory)
124                         return_0;
125                 return 1;
126         }
127
128         /* Trap a read-only file system */
129         if ((access(vg->cmd->archive_params->dir, R_OK | W_OK | X_OK) == -1) &&
130             (errno == EROFS)) {
131                 if (compulsory) {
132                         log_error("Cannot archive volume group metadata for %s to read-only filesystem.",
133                                   vg->name);
134                         return 0;
135                 }
136                 return 1;
137         }
138
139         log_verbose("Archiving volume group \"%s\" metadata (seqno %u).", vg->name,
140                     vg->seqno);
141
142         if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 1)))
143                 return_0;
144
145         if (!archive_vg(vg, vg->cmd->archive_params->dir, desc,
146                         vg->cmd->archive_params->keep_days,
147                         vg->cmd->archive_params->keep_number))
148                 return_0;
149
150         vg->status |= ARCHIVED_VG;
151
152         return 1;
153 }
154
155 int archive(struct volume_group *vg)
156 {
157         int r;
158
159         sigint_allow();
160         r  = _archive(vg, 1);
161         sigint_restore();
162
163         return r;
164 }
165
166 int archive_display(struct cmd_context *cmd, const char *vg_name)
167 {
168         int r1, r2;
169
170         r1 = archive_list(cmd, cmd->archive_params->dir, vg_name);
171         r2 = backup_list(cmd, cmd->backup_params->dir, vg_name);
172
173         return r1 && r2;
174 }
175
176 int archive_display_file(struct cmd_context *cmd, const char *file)
177 {
178         int r;
179
180         r = archive_list_file(cmd, file);
181
182         return r;
183 }
184
185 int backup_init(struct cmd_context *cmd, const char *dir,
186                 int enabled)
187 {
188         backup_exit(cmd);
189
190         if (!(cmd->backup_params = dm_pool_zalloc(cmd->libmem,
191                                                sizeof(*cmd->backup_params)))) {
192                 log_error("backup_params alloc failed");
193                 return 0;
194         }
195
196         if (!*dir)
197                 return 1;
198
199         if (!(cmd->backup_params->dir = strdup(dir))) {
200                 log_error("Couldn't copy backup directory name.");
201                 return 0;
202         }
203         backup_enable(cmd, enabled);
204
205         return 1;
206 }
207
208 void backup_exit(struct cmd_context *cmd)
209 {
210         if (!cmd->backup_params)
211                 return;
212         free(cmd->backup_params->dir);
213         memset(cmd->backup_params, 0, sizeof(*cmd->backup_params));
214 }
215
216 void backup_enable(struct cmd_context *cmd, int flag)
217 {
218         cmd->backup_params->enabled = flag;
219 }
220
221 static int _backup(struct volume_group *vg)
222 {
223         char name[PATH_MAX];
224         char *desc;
225         int r;
226
227         if (!(desc = _build_desc(vg->cmd->mem, vg->cmd->cmd_line, 0)))
228                 return_0;
229
230         if (dm_snprintf(name, sizeof(name), "%s/%s",
231                          vg->cmd->backup_params->dir, vg->name) < 0) {
232                 log_error("Failed to generate volume group metadata backup "
233                           "filename.");
234                 return 0;
235         }
236
237         sigint_allow();
238         r = backup_to_file(name, desc, vg);
239         sigint_restore();
240
241         return r;
242 }
243
244 int backup_locally(struct volume_group *vg)
245 {
246         if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
247                 log_warn_suppress(vg->cmd->backup_params->suppress++,
248                                   "WARNING: This metadata update is NOT backed up.");
249                 return 1;
250         }
251
252         if (test_mode()) {
253                 log_verbose("Test mode: Skipping backup of volume group.");
254                 return 1;
255         }
256
257         if (!dm_create_dir(vg->cmd->backup_params->dir))
258                 return 0;
259
260         /* Trap a read-only file system */
261         if ((access(vg->cmd->backup_params->dir, R_OK | W_OK | X_OK) == -1) &&
262             (errno == EROFS)) {
263                 /* Will take a backup next time when FS is writable */
264                 log_debug("Skipping backup of volume group on read-only filesystem.");
265                 return 0;
266         }
267
268         if (!_backup(vg)) {
269                 log_error("Backup of volume group %s metadata failed.",
270                           vg->name);
271                 return 0;
272         }
273
274         return 1;
275 }
276
277 int backup(struct volume_group *vg)
278 {
279
280         /* Unlock memory if possible */
281         memlock_unlock(vg->cmd);
282
283         /* Don't back up orphan VGs. */
284         if (is_orphan_vg(vg->name))
285                 return 1;
286
287         return backup_locally(vg);
288 }
289
290 int backup_remove(struct cmd_context *cmd, const char *vg_name)
291 {
292         char path[PATH_MAX];
293
294         if (dm_snprintf(path, sizeof(path), "%s/%s",
295                          cmd->backup_params->dir, vg_name) < 0) {
296                 log_error("Failed to generate backup filename (for removal).");
297                 return 0;
298         }
299
300         /*
301          * Let this fail silently.
302          */
303         if (unlink(path))
304                 log_sys_debug("unlink", path);
305
306         return 1;
307 }
308
309 struct volume_group *backup_read_vg(struct cmd_context *cmd,
310                                     const char *vg_name, const char *file)
311 {
312         struct volume_group *vg = NULL;
313         struct format_instance *tf;
314         struct format_instance_ctx fic;
315         struct text_context tc = {.path_live = file,
316                                   .path_edit = NULL,
317                                   .desc = cmd->cmd_line};
318         struct metadata_area *mda;
319
320         fic.type = FMT_INSTANCE_PRIVATE_MDAS;
321         fic.context.private = &tc;
322         if (!(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, &fic))) {
323                 log_error("Couldn't create text format object.");
324                 return NULL;
325         }
326
327         dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
328                 if (!(vg = mda->ops->vg_read(cmd, tf, vg_name, mda, NULL, NULL)))
329                         stack;
330                 break;
331         }
332
333         if (vg)
334                 set_pv_devices(tf, vg);
335
336         if (!vg)
337                 tf->fmt->ops->destroy_instance(tf);
338
339         return vg;
340 }
341
342 static int _restore_vg_should_write_pv(struct physical_volume *pv, int do_pvcreate)
343 {
344         struct lvmcache_info *info;
345
346         if (do_pvcreate)
347                 return 1;
348
349         if (!(pv->fmt->features & FMT_PV_FLAGS))
350                 return 0;
351
352         if (!pv->dev) {
353                 log_error("Failed to find device for PV.");
354                 return -1;
355         }
356
357         if (!(info = lvmcache_info_from_pvid(pv->dev->pvid, pv->dev, 0))) {
358                 log_error("Failed to find cached info for PV %s.", pv_dev_name(pv));
359                 return -1;
360         }
361
362         /*
363          * We're restoring a VG and if the PV_EXT_USED
364          * flag is not set yet in PV, we need to set it now!
365          * This may happen if we have plain PVs without a VG
366          * and we're restoring former VG from backup on top
367          * of these PVs.
368          */
369         if (!(lvmcache_ext_flags(info) & PV_EXT_USED))
370                 return 1;
371
372         return 0;
373 }
374
375 /* ORPHAN and VG locks held before calling this */
376 int backup_restore_vg(struct cmd_context *cmd, struct volume_group *vg,
377                       int do_pvcreate, struct pv_create_args *pva)
378 {
379         struct dm_list new_pvs;
380         struct pv_list *pvl, *new_pvl;
381         struct physical_volume *existing_pv, *pv;
382         struct dm_list *pvs = &vg->pvs;
383         struct format_instance *fid;
384         struct format_instance_ctx fic;
385         int should_write_pv;
386         uint32_t tmp_extent_size;
387
388         /*
389          * FIXME: Check that the PVs referenced in the backup are
390          * not members of other existing VGs.
391          */
392
393         /* Prepare new PVs if needed. */
394         if (do_pvcreate) {
395                 dm_list_init(&new_pvs);
396
397                 dm_list_iterate_items(pvl, &vg->pvs) {
398                         existing_pv = pvl->pv;
399
400                         pva->id = existing_pv->id;
401                         pva->idp = &pva->id;
402                         pva->pe_start = pv_pe_start(existing_pv);
403                         pva->extent_count = pv_pe_count(existing_pv);
404                         pva->extent_size = pv_pe_size(existing_pv);
405                         /* pe_end = pv_pe_count(existing_pv) * pv_pe_size(existing_pv) + pe_start - 1 */
406
407                         if (!(pv = pv_create(cmd, pv_dev(existing_pv), pva))) {
408                                 log_error("Failed to setup physical volume \"%s\".",
409                                           pv_dev_name(existing_pv));
410                                 return 0;
411                         }
412                         pv->vg_name = vg->name;
413                         /* both are struct id */
414                         memcpy(&pv->vg_id, &vg->id, sizeof(struct id));
415
416                         if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
417                                 log_error("Failed to allocate PV list item for \"%s\".",
418                                           pv_dev_name(pvl->pv));
419                                 return 0;
420                         }
421
422                         new_pvl->pv = pv;
423                         dm_list_add(&new_pvs, &new_pvl->list);
424
425                         log_verbose("Set up physical volume for \"%s\" with " FMTu64
426                                     " available sectors.", pv_dev_name(pv), pv_size(pv));
427                 }
428
429                 pvs = &new_pvs;
430         }
431
432         /* Attempt to write out using currently active format */
433         fic.type = FMT_INSTANCE_AUX_MDAS;
434         fic.context.vg_ref.vg_name = vg->name;
435         fic.context.vg_ref.vg_id = NULL;
436         if (!(fid = cmd->fmt->ops->create_instance(cmd->fmt, &fic))) {
437                 log_error("Failed to allocate format instance.");
438                 return 0;
439         }
440
441         if (do_pvcreate) {
442                 log_verbose("Deleting existing metadata for VG %s.", vg->name);
443                 if (!vg_remove_mdas(vg)) {
444                         cmd->fmt->ops->destroy_instance(fid);
445                         log_error("Removal of existing metadata for VG %s failed.", vg->name);
446                         return 0;
447                 }
448         }
449
450         vg_set_fid(vg, fid);
451
452         /*
453          * Setting vg->old_name to a blank value will explicitly
454          * disable any attempt to check VG name in existing metadata.
455         */
456         if (!(vg->old_name = dm_pool_strdup(vg->vgmem, ""))) {
457                 log_error("Failed to duplicate empty name.");
458                 return 0;
459         }
460
461         /* Add any metadata areas on the PVs */
462         dm_list_iterate_items(pvl, pvs) {
463                 if ((should_write_pv = _restore_vg_should_write_pv(pvl->pv, do_pvcreate)) < 0)
464                         return_0;
465
466                 if (should_write_pv) {
467                         if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) {
468                                 log_error("Failed to allocate structure for scheduled "
469                                           "writing of PV '%s'.", pv_dev_name(pvl->pv));
470                                 return 0;
471                         }
472
473                         new_pvl->pv = pvl->pv;
474                         dm_list_add(&vg->pv_write_list, &new_pvl->list);
475                 }
476
477                 /* Add any metadata areas on the PV. */
478                 tmp_extent_size = vg->extent_size;
479                 vg->extent_size = 0;
480                 if (!vg->fid->fmt->ops->pv_setup(vg->fid->fmt, pvl->pv, vg)) {
481                         vg->extent_size = tmp_extent_size;
482                         log_error("Format-specific setup for %s failed.",
483                                   pv_dev_name(pvl->pv));
484                         return 0;
485                 }
486                 vg->extent_size = tmp_extent_size;
487         }
488
489         if (do_pvcreate) {
490                 dm_list_iterate_items(pvl, &vg->pv_write_list) {
491                         struct device *dev = pv_dev(pvl->pv);
492                         const char *pv_name = dev_name(dev);
493
494                         if (!label_remove(dev)) {
495                                 log_error("Failed to wipe existing label on %s", pv_name);
496                                 return 0;
497                         }
498
499                         log_verbose("Zeroing start of device %s", pv_name);
500
501                         if (!dev_write_zeros(dev, 0, 2048)) {
502                                 log_error("%s not wiped: aborting", pv_name);
503                                 return 0;
504                         }
505                 }
506         }
507
508         if (!vg_write(vg))
509                 return_0;
510
511         if (!vg_commit(vg))
512                 return_0;
513
514         return 1;
515 }
516
517 /* ORPHAN and VG locks held before calling this */
518 int backup_restore_from_file(struct cmd_context *cmd, const char *vg_name,
519                              const char *file, int force)
520 {
521         struct volume_group *vg;
522         int missing_pvs, r = 0;
523         const struct lv_list *lvl;
524
525         /*
526          * Read in the volume group from the text file.
527          */
528         if (!(vg = backup_read_vg(cmd, vg_name, file)))
529                 return_0;
530
531         /* FIXME: Restore support is missing for now */
532         dm_list_iterate_items(lvl, &vg->lvs) {
533                 if (lv_is_thin_type(lvl->lv)) {
534                         if (!force) {
535                                 log_error("Consider using option --force to restore "
536                                           "Volume Group %s with thin volumes.",
537                                           vg->name);
538                                 goto out;
539                         } else {
540                                 log_warn("WARNING: Forced restore of Volume Group "
541                                          "%s with thin volumes.", vg->name);
542                                 break;
543                         }
544                 }
545         }
546
547         missing_pvs = vg_missing_pv_count(vg);
548         if (missing_pvs == 0)
549                 r = backup_restore_vg(cmd, vg, 0, NULL);
550         else
551                 log_error("Cannot restore Volume Group %s with %i PVs "
552                           "marked as missing.", vg->name, missing_pvs);
553
554 out:
555         release_vg(vg);
556         return r;
557 }
558
559 int backup_restore(struct cmd_context *cmd, const char *vg_name, int force)
560 {
561         char path[PATH_MAX];
562
563         if (dm_snprintf(path, sizeof(path), "%s/%s",
564                          cmd->backup_params->dir, vg_name) < 0) {
565                 log_error("Failed to generate backup filename (for restore).");
566                 return 0;
567         }
568
569         return backup_restore_from_file(cmd, vg_name, path, force);
570 }
571
572 int backup_to_file(const char *file, const char *desc, struct volume_group *vg)
573 {
574         int r = 0;
575         struct format_instance *tf;
576         struct format_instance_ctx fic;
577         struct text_context tc = {.path_live = file,
578                                   .path_edit = NULL,
579                                   .desc = desc};
580         struct metadata_area *mda;
581         struct cmd_context *cmd;
582
583         cmd = vg->cmd;
584
585         log_verbose("Creating volume group backup \"%s\" (seqno %u).", file, vg->seqno);
586
587         fic.type = FMT_INSTANCE_PRIVATE_MDAS;
588         fic.context.private = &tc;
589         if (!(tf = cmd->fmt_backup->ops->create_instance(cmd->fmt_backup, &fic))) {
590                 log_error("Couldn't create backup object.");
591                 return 0;
592         }
593
594         if (dm_list_empty(&tf->metadata_areas_in_use)) {
595                 log_error(INTERNAL_ERROR "No in use metadata areas to write.");
596                 tf->fmt->ops->destroy_instance(tf);
597                 return 0;
598         }
599
600         /* Write and commit the metadata area */
601         dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
602                 if (!(r = mda->ops->vg_write(tf, vg, mda))) {
603                         stack;
604                         continue;
605                 }
606                 if (mda->ops->vg_commit &&
607                     !(r = mda->ops->vg_commit(tf, vg, mda))) {
608                         stack;
609                 }
610         }
611
612         tf->fmt->ops->destroy_instance(tf);
613         return r;
614 }
615
616 /*
617  * Update backup (and archive) if they're out-of-date or don't exist.
618  *
619  * This function is not supposed to log_error
620  * when the filesystem with archive/backup dir is read-only.
621  */
622 void check_current_backup(struct volume_group *vg)
623 {
624         char path[PATH_MAX];
625         struct volume_group *vg_backup;
626         int old_suppress;
627
628         if (!vg->cmd->backup_params->enabled || !vg->cmd->backup_params->dir) {
629                 if (!vg->cmd->backup_disabled) {
630                         log_debug("Skipping check for current backup, since backup is disabled.");
631                         vg->cmd->backup_disabled = 1;
632                 }
633                 return;
634         }
635
636         if (vg_is_exported(vg))
637                 return;
638
639         if (dm_snprintf(path, sizeof(path), "%s/%s",
640                         vg->cmd->backup_params->dir, vg->name) < 0) {
641                 log_warn("WARNING: Failed to generate backup pathname %s/%s.",
642                          vg->cmd->backup_params->dir, vg->name);
643                 return;
644         }
645
646         old_suppress = log_suppress(1);
647         /* Up-to-date backup exists? */
648         if ((vg_backup = backup_read_vg(vg->cmd, vg->name, path)) &&
649             (vg->seqno == vg_backup->seqno) &&
650             (id_equal(&vg->id, &vg_backup->id))) {
651                 log_suppress(old_suppress);
652                 release_vg(vg_backup);
653                 return;
654         }
655         log_suppress(old_suppress);
656
657         if (vg_backup) {
658                 if (!_archive(vg_backup, 0))
659                         stack;
660                 release_vg(vg_backup);
661         }
662         if (!_archive(vg, 0))
663                 stack;
664         if (!backup_locally(vg))
665                 stack;
666 }