2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
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.
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
21 * FIXME: Check for valid handle before dereferencing field or log error?
23 #define pv_field(handle, field) ((handle)->field)
25 char *pv_fmt_dup(const struct physical_volume *pv)
29 return dm_pool_strdup(pv->vg->vgmem, pv->fmt->name);
32 char *pv_name_dup(const struct physical_volume *pv)
34 return dm_pool_strdup(pv->vg->vgmem, dev_name(pv->dev));
38 * Gets/Sets for external LVM library
40 struct id pv_id(const struct physical_volume *pv)
42 return pv_field(pv, id);
45 char *pv_uuid_dup(const struct physical_volume *pv)
47 return id_format_and_copy(pv->vg->vgmem, &pv->id);
50 char *pv_tags_dup(const struct physical_volume *pv)
52 return tags_format_and_copy(pv->vg->vgmem, &pv->tags);
55 const struct format_type *pv_format_type(const struct physical_volume *pv)
57 return pv_field(pv, fmt);
60 struct id pv_vgid(const struct physical_volume *pv)
62 return pv_field(pv, vgid);
65 struct device *pv_dev(const struct physical_volume *pv)
67 return pv_field(pv, dev);
70 const char *pv_vg_name(const struct physical_volume *pv)
72 return pv_field(pv, vg_name);
75 const char *pv_dev_name(const struct physical_volume *pv)
77 return dev_name(pv_dev(pv));
80 uint64_t pv_size(const struct physical_volume *pv)
82 return pv_field(pv, size);
85 uint64_t pv_dev_size(const struct physical_volume *pv)
89 if (!dev_get_size(pv->dev, &size))
94 uint64_t pv_size_field(const struct physical_volume *pv)
101 size = (uint64_t) pv->pe_count * pv->pe_size;
105 uint64_t pv_free(const struct physical_volume *pv)
110 freespace = pv->size;
112 freespace = (uint64_t)
113 (pv->pe_count - pv->pe_alloc_count) * pv->pe_size;
117 uint64_t pv_status(const struct physical_volume *pv)
119 return pv_field(pv, status);
122 uint32_t pv_pe_size(const struct physical_volume *pv)
124 return pv_field(pv, pe_size);
127 uint64_t pv_pe_start(const struct physical_volume *pv)
129 return pv_field(pv, pe_start);
132 uint32_t pv_pe_count(const struct physical_volume *pv)
134 return pv_field(pv, pe_count);
137 uint32_t pv_pe_alloc_count(const struct physical_volume *pv)
139 return pv_field(pv, pe_alloc_count);
142 uint32_t pv_mda_count(const struct physical_volume *pv)
144 struct lvmcache_info *info;
146 info = info_from_pvid((const char *)&pv->id.uuid, 0);
147 return info ? dm_list_size(&info->mdas) : UINT64_C(0);
150 uint32_t pv_mda_used_count(const struct physical_volume *pv)
152 struct lvmcache_info *info;
153 struct metadata_area *mda;
154 uint32_t used_count=0;
156 info = info_from_pvid((const char *)&pv->id.uuid, 0);
159 dm_list_iterate_items(mda, &info->mdas) {
160 if (!mda_is_ignored(mda))
167 * is_orphan - Determine whether a pv is an orphan based on its vg_name
168 * @pv: handle to the physical volume
170 int is_orphan(const struct physical_volume *pv)
172 return is_orphan_vg(pv_field(pv, vg_name));
176 * is_pv - Determine whether a pv is a real pv or dummy one
177 * @pv: handle to device
179 int is_pv(const struct physical_volume *pv)
181 return (pv_field(pv, vg_name) ? 1 : 0);
184 int is_missing_pv(const struct physical_volume *pv)
186 return pv_field(pv, status) & MISSING_PV ? 1 : 0;
189 char *pv_attr_dup(struct dm_pool *mem, const struct physical_volume *pv)
193 if (!(repstr = dm_pool_zalloc(mem, 3))) {
194 log_error("dm_pool_alloc failed");
198 repstr[0] = (pv->status & ALLOCATABLE_PV) ? 'a' : '-';
199 repstr[1] = (pv->status & EXPORTED_VG) ? 'x' : '-';
203 uint64_t pv_mda_size(const struct physical_volume *pv)
205 struct lvmcache_info *info;
206 uint64_t min_mda_size = 0;
207 const char *pvid = (const char *)(&pv->id.uuid);
209 /* PVs could have 2 mdas of different sizes (rounding effect) */
210 if ((info = info_from_pvid(pvid, 0)))
211 min_mda_size = find_min_mda_size(&info->mdas);
215 uint64_t pv_mda_free(const struct physical_volume *pv)
217 struct lvmcache_info *info;
218 uint64_t freespace = UINT64_MAX, mda_free;
219 const char *pvid = (const char *)&pv->id.uuid;
220 struct metadata_area *mda;
222 if ((info = info_from_pvid(pvid, 0)))
223 dm_list_iterate_items(mda, &info->mdas) {
224 if (!mda->ops->mda_free_sectors)
226 mda_free = mda->ops->mda_free_sectors(mda);
227 if (mda_free < freespace)
228 freespace = mda_free;
231 if (freespace == UINT64_MAX)
232 freespace = UINT64_C(0);
236 uint64_t pv_used(const struct physical_volume *pv)
243 used = (uint64_t) pv->pe_alloc_count * pv->pe_size;
247 unsigned pv_mda_set_ignored(const struct physical_volume *pv, unsigned mda_ignored)
249 struct lvmcache_info *info;
250 struct metadata_area *mda, *vg_mda, *tmda;
251 struct dm_list *vg_mdas_in_use, *vg_mdas_ignored;
253 if (!(info = info_from_pvid((const char *)&pv->id.uuid, 0)))
257 dm_list_iterate_items(mda, &info->mdas)
258 mda_set_ignored(mda, mda_ignored);
263 * Do not allow disabling of the the last PV in a VG.
265 if (pv_mda_used_count(pv) == vg_mda_used_count(pv->vg)) {
266 log_error("Cannot disable all metadata areas in volume group %s.",
272 * Non-orphan case is more complex.
273 * If the PV's mdas are ignored, and we wish to un-ignore,
274 * we clear the bit and move them from the ignored mda list to the
275 * in_use list, ensuring the new state will get written to disk
276 * in the vg_write() path.
277 * If the PV's mdas are not ignored, and we are setting
278 * them to ignored, we set the bit but leave them on the in_use
279 * list, ensuring the new state will get written to disk in the
282 vg_mdas_in_use = &pv->vg->fid->metadata_areas_in_use;
283 vg_mdas_ignored = &pv->vg->fid->metadata_areas_ignored;
285 dm_list_iterate_items(mda, &info->mdas) {
286 if (mda_is_ignored(mda) && !mda_ignored)
287 /* Changing an ignored mda to one in_use requires moving it */
288 dm_list_iterate_items_safe(vg_mda, tmda, vg_mdas_ignored)
289 if (mda_locns_match(mda, vg_mda)) {
290 mda_set_ignored(vg_mda, mda_ignored);
291 dm_list_move(vg_mdas_in_use, &vg_mda->list);
294 dm_list_iterate_items_safe(vg_mda, tmda, vg_mdas_in_use)
295 if (mda_locns_match(mda, vg_mda))
296 /* Don't move mda: needs writing to disk. */
297 mda_set_ignored(vg_mda, mda_ignored);
299 mda_set_ignored(mda, mda_ignored);