Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / tools / pvresize.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4  * Copyright (C) 2005 Zak Kipling. All rights reserved.
5  *
6  * This file is part of LVM2.
7  *
8  * This copyrighted material is made available to anyone wishing to use,
9  * modify, copy, or redistribute it subject to the terms and conditions
10  * of the GNU Lesser General Public License v.2.1.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 #include "tools.h"
18
19 struct pvresize_params {
20         uint64_t new_size;
21
22         unsigned done;
23         unsigned total;
24 };
25
26 static int _pv_resize_single(struct cmd_context *cmd,
27                              struct volume_group *vg,
28                              struct physical_volume *pv,
29                              const uint64_t new_size)
30 {
31         struct pv_list *pvl;
32         uint64_t size = 0;
33         uint32_t new_pe_count = 0;
34         int r = 0;
35         struct dm_list mdas;
36         const char *pv_name = pv_dev_name(pv);
37         const char *vg_name = pv_vg_name(pv);
38         struct lvmcache_info *info;
39         int mda_count = 0;
40         struct volume_group *old_vg = vg;
41
42         dm_list_init(&mdas);
43
44         if (is_orphan_vg(vg_name)) {
45                 if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
46                         log_error("Can't get lock for orphans");
47                         return 0;
48                 }
49
50                 if (!(pv = pv_read(cmd, pv_name, &mdas, NULL, 1, 0))) {
51                         unlock_vg(cmd, vg_name);
52                         log_error("Unable to read PV \"%s\"", pv_name);
53                         return 0;
54                 }
55
56                 mda_count = dm_list_size(&mdas);
57         } else {
58                 vg = vg_read_for_update(cmd, vg_name, NULL, 0);
59
60                 if (vg_read_error(vg)) {
61                         free_vg(vg);
62                         log_error("Unable to read volume group \"%s\".",
63                                   vg_name);
64                         return 0;
65                 }
66
67                 if (!(pvl = find_pv_in_vg(vg, pv_name))) {
68                         log_error("Unable to find \"%s\" in volume group \"%s\"",
69                                   pv_name, vg->name);
70                         goto out;
71                 }
72
73                 pv = pvl->pv;
74
75                 if (!(info = info_from_pvid(pv->dev->pvid, 0))) {
76                         log_error("Can't get info for PV %s in volume group %s",
77                                   pv_name, vg->name);
78                         goto out;
79                 }
80
81                 mda_count = dm_list_size(&info->mdas);
82
83                 if (!archive(vg))
84                         goto out;
85         }
86
87         /* FIXME Create function to test compatibility properly */
88         if (mda_count > 1) {
89                 log_error("%s: too many metadata areas for pvresize", pv_name);
90                 goto out;
91         }
92
93         if (!(pv->fmt->features & FMT_RESIZE_PV)) {
94                 log_error("Physical volume %s format does not support resizing.",
95                           pv_name);
96                 goto out;
97         }
98
99         /* Get new size */
100         if (!dev_get_size(pv_dev(pv), &size)) {
101                 log_error("%s: Couldn't get size.", pv_name);
102                 goto out;
103         }
104
105         if (new_size) {
106                 if (new_size > size)
107                         log_warn("WARNING: %s: Overriding real size. "
108                                   "You could lose data.", pv_name);
109                 log_verbose("%s: Pretending size is %" PRIu64 " not %" PRIu64
110                             " sectors.", pv_name, new_size, pv_size(pv));
111                 size = new_size;
112         }
113
114         if (size < PV_MIN_SIZE) {
115                 log_error("%s: Size must exceed minimum of %ld sectors.",
116                           pv_name, PV_MIN_SIZE);
117                 goto out;
118         }
119
120         if (size < pv_pe_start(pv)) {
121                 log_error("%s: Size must exceed physical extent start of "
122                           "%" PRIu64 " sectors.", pv_name, pv_pe_start(pv));
123                 goto out;
124         }
125
126         pv->size = size;
127
128         if (vg) {
129                 pv->size -= pv_pe_start(pv);
130                 new_pe_count = pv_size(pv) / vg->extent_size;
131
132                 if (!new_pe_count) {
133                         log_error("%s: Size must leave space for at "
134                                   "least one physical extent of "
135                                   "%" PRIu32 " sectors.", pv_name,
136                                   pv_pe_size(pv));
137                         goto out;
138                 }
139
140                 if (!pv_resize(pv, vg, new_pe_count))
141                         goto_out;
142         }
143
144         log_verbose("Resizing volume \"%s\" to %" PRIu64 " sectors.",
145                     pv_name, pv_size(pv));
146
147         log_verbose("Updating physical volume \"%s\"", pv_name);
148         if (!is_orphan_vg(vg_name)) {
149                 if (!vg_write(vg) || !vg_commit(vg)) {
150                         log_error("Failed to store physical volume \"%s\" in "
151                                   "volume group \"%s\"", pv_name, vg_name);
152                         goto out;
153                 }
154                 backup(vg);
155         } else if (!(pv_write(cmd, pv, NULL, INT64_C(-1)))) {
156                 log_error("Failed to store physical volume \"%s\"",
157                           pv_name);
158                 goto out;
159         }
160
161         log_print("Physical volume \"%s\" changed", pv_name);
162         r = 1;
163
164 out:
165         unlock_vg(cmd, vg_name);
166         if (!old_vg)
167                 free_vg(vg);
168         return r;
169 }
170
171 static int _pvresize_single(struct cmd_context *cmd,
172                             struct volume_group *vg,
173                             struct physical_volume *pv,
174                             void *handle)
175 {
176         struct pvresize_params *params = (struct pvresize_params *) handle;
177
178         params->total++;
179
180         if (!_pv_resize_single(cmd, vg, pv, params->new_size)) {
181                 stack;
182                 return ECMD_FAILED;
183         }
184         
185         params->done++;
186
187         return ECMD_PROCESSED;
188 }
189
190 int pvresize(struct cmd_context *cmd, int argc, char **argv)
191 {
192         struct pvresize_params params;
193         int ret;
194
195         if (!argc) {
196                 log_error("Please supply physical volume(s)");
197                 return EINVALID_CMD_LINE;
198         }
199
200         if (arg_sign_value(cmd, physicalvolumesize_ARG, 0) == SIGN_MINUS) {
201                 log_error("Physical volume size may not be negative");
202                 return 0;
203         }
204
205         params.new_size = arg_uint64_value(cmd, physicalvolumesize_ARG,
206                                            UINT64_C(0));
207
208         params.done = 0;
209         params.total = 0;
210
211         ret = process_each_pv(cmd, argc, argv, NULL, READ_FOR_UPDATE, 0, &params,
212                               _pvresize_single);
213
214         log_print("%d physical volume(s) resized / %d physical volume(s) "
215                   "not resized", params.done, params.total - params.done);
216
217         return ret;
218 }