Tizen 2.1 base
[external/device-mapper.git] / tools / pvdisplay.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 "tools.h"
17
18 static int _pvdisplay_single(struct cmd_context *cmd,
19                              struct volume_group *vg,
20                              struct physical_volume *pv, void *handle)
21 {
22         struct pv_list *pvl;
23         int ret = ECMD_PROCESSED;
24         uint64_t size;
25         struct volume_group *old_vg = vg;
26
27         const char *pv_name = pv_dev_name(pv);
28         const char *vg_name = NULL;
29
30         if (!is_orphan(pv) && !vg) {
31                 vg_name = pv_vg_name(pv);
32                 vg = vg_read(cmd, vg_name, (char *)&pv->vgid, 0);
33                 if (vg_read_error(vg)) {
34                         log_error("Skipping volume group %s", vg_name);
35                         free_vg(vg);
36                         /* FIXME If CLUSTERED should return ECMD_PROCESSED here */
37                         return ECMD_FAILED;
38                 }
39
40                 /*
41                  * Replace possibly incomplete PV structure with new one
42                  * allocated in vg_read_internal() path.
43                  */
44                  if (!(pvl = find_pv_in_vg(vg, pv_name))) {
45                          log_error("Unable to find \"%s\" in volume group \"%s\"",
46                                    pv_name, vg->name);
47                          ret = ECMD_FAILED;
48                          goto out;
49                  }
50
51                  pv = pvl->pv;
52         }
53
54         if (is_orphan(pv))
55                 size = pv_size(pv);
56         else
57                 size = (pv_pe_count(pv) - pv_pe_alloc_count(pv)) *
58                         pv_pe_size(pv);
59
60         if (arg_count(cmd, short_ARG)) {
61                 log_print("Device \"%s\" has a capacity of %s", pv_name,
62                           display_size(cmd, size));
63                 goto out;
64         }
65
66         if (pv_status(pv) & EXPORTED_VG)
67                 log_print("Physical volume \"%s\" of volume group \"%s\" "
68                           "is exported", pv_name, pv_vg_name(pv));
69
70         if (is_orphan(pv))
71                 log_print("\"%s\" is a new physical volume of \"%s\"",
72                           pv_name, display_size(cmd, size));
73
74         if (arg_count(cmd, colon_ARG)) {
75                 pvdisplay_colons(pv);
76                 goto out;
77         }
78
79         pvdisplay_full(cmd, pv, handle);
80
81         if (arg_count(cmd, maps_ARG))
82                 pvdisplay_segments(pv);
83
84 out:
85         if (vg_name)
86                 unlock_vg(cmd, vg_name);
87         if (!old_vg)
88                 free_vg(vg);
89
90         return ret;
91 }
92
93 int pvdisplay(struct cmd_context *cmd, int argc, char **argv)
94 {
95         if (arg_count(cmd, columns_ARG)) {
96                 if (arg_count(cmd, colon_ARG) || arg_count(cmd, maps_ARG) ||
97                     arg_count(cmd, short_ARG)) {
98                         log_error("Incompatible options selected");
99                         return EINVALID_CMD_LINE;
100                 }
101                 return pvs(cmd, argc, argv);
102         } else if (arg_count(cmd, aligned_ARG) ||
103                    arg_count(cmd, all_ARG) ||
104                    arg_count(cmd, noheadings_ARG) ||
105                    arg_count(cmd, options_ARG) ||
106                    arg_count(cmd, separator_ARG) ||
107                    arg_count(cmd, sort_ARG) || arg_count(cmd, unbuffered_ARG)) {
108                 log_error("Incompatible options selected");
109                 return EINVALID_CMD_LINE;
110         }
111
112         if (arg_count(cmd, colon_ARG) && arg_count(cmd, maps_ARG)) {
113                 log_error("Option -v not allowed with option -c");
114                 return EINVALID_CMD_LINE;
115         }
116
117         return process_each_pv(cmd, argc, argv, NULL, 0, 0, NULL,
118                                _pvdisplay_single);
119 }