]> sourceware.org Git - lvm2.git/blobdiff - tools/pvscan.c
Reinstate accidentally-deleted line.
[lvm2.git] / tools / pvscan.c
index 93d5c32871109590133ef190bfe4fc62bbe6dd12..cca079c8ef7f043d8ac87e784fd6b787aeb744f0 100644 (file)
 /*
- * Copyright (C) 2001 Sistina Software
+ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
+ * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  *
- * LVM is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
+ * This file is part of LVM2.
  *
- * LVM is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with LVM; see the file COPYING.  If not, write to
- * the Free Software Foundation, 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
  *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "tools.h"
 
-void pvscan_display_single(struct physical_volume *pv);
+#include "lvmetad.h"
+#include "lvmcache.h"
 
 int pv_max_name_len = 0;
 int vg_max_name_len = 0;
 
-int pvscan(int argc, char **argv)
+static void _pvscan_display_single(struct cmd_context *cmd,
+                                  struct physical_volume *pv,
+                                  void *handle __attribute__((unused)))
+{
+       char uuid[64] __attribute__((aligned(8)));
+       unsigned vg_name_len = 0;
+
+       char pv_tmp_name[NAME_LEN] = { 0, };
+       char vg_tmp_name[NAME_LEN] = { 0, };
+       char vg_name_this[NAME_LEN] = { 0, };
+
+       /* short listing? */
+       if (arg_count(cmd, short_ARG) > 0) {
+               log_print("%s", pv_dev_name(pv));
+               return;
+       }
+
+       if (arg_count(cmd, verbose_ARG) > 1) {
+               /* FIXME As per pv_display! Drop through for now. */
+               /* pv_show(pv); */
+
+               /* FIXME - Moved to Volume Group structure */
+               /* log_print("System Id             %s", pv->vg->system_id); */
+
+               /* log_print(" "); */
+               /* return; */
+       }
+
+       memset(pv_tmp_name, 0, sizeof(pv_tmp_name));
+
+       vg_name_len = strlen(pv_vg_name(pv)) + 1;
+
+       if (arg_count(cmd, uuid_ARG)) {
+               if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
+                       stack;
+                       return;
+               }
+
+               sprintf(pv_tmp_name, "%-*s with UUID %s",
+                       pv_max_name_len - 2, pv_dev_name(pv), uuid);
+       } else {
+               sprintf(pv_tmp_name, "%s", pv_dev_name(pv));
+       }
+
+       if (is_orphan(pv)) {
+               log_print("PV %-*s    %-*s %s [%s]",
+                         pv_max_name_len, pv_tmp_name,
+                         vg_max_name_len, " ",
+                         pv->fmt ? pv->fmt->name : "    ",
+                         display_size(cmd, pv_size(pv)));
+               return;
+       }
+
+       if (pv_status(pv) & EXPORTED_VG) {
+               strncpy(vg_name_this, pv_vg_name(pv), vg_name_len);
+               log_print("PV %-*s  is in exported VG %s "
+                         "[%s / %s free]",
+                         pv_max_name_len, pv_tmp_name,
+                         vg_name_this,
+                         display_size(cmd, (uint64_t) pv_pe_count(pv) *
+                                      pv_pe_size(pv)),
+                         display_size(cmd, (uint64_t) (pv_pe_count(pv) -
+                                               pv_pe_alloc_count(pv))
+                                      * pv_pe_size(pv)));
+               return;
+       }
+
+       sprintf(vg_tmp_name, "%s", pv_vg_name(pv));
+       log_print("PV %-*s VG %-*s %s [%s / %s free]", pv_max_name_len,
+                 pv_tmp_name, vg_max_name_len, vg_tmp_name,
+                 pv->fmt ? pv->fmt->name : "    ",
+                 display_size(cmd, (uint64_t) pv_pe_count(pv) *
+                                              pv_pe_size(pv)),
+                 display_size(cmd, (uint64_t) (pv_pe_count(pv) -
+                                               pv_pe_alloc_count(pv)) *
+                                          pv_pe_size(pv)));
+}
+
+static int _pvscan_lvmetad(struct cmd_context *cmd, int argc, char **argv)
+{
+       int ret = ECMD_PROCESSED;
+
+       if (!argc) {
+               log_error("List of Physical Volumes to be cached by the lvmetad daemon required.");
+               return EINVALID_CMD_LINE;
+       }
+
+       if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_READ)) {
+               log_error("Unable to obtain global lock.");
+               return ECMD_FAILED;
+       }
+
+       log_verbose("Using physical volume(s) on command line");
+       while (argc--) {
+               if (!pvscan_lvmetad_single(cmd, *argv++)) {
+                       ret = ECMD_FAILED;
+                       break;
+               }
+               if (sigint_caught())
+                       break;
+       }
+
+       unlock_vg(cmd, VG_GLOBAL);
+
+       return ret;
+}
+
+int pvscan(struct cmd_context *cmd, int argc, char **argv)
 {
        int new_pvs_found = 0;
        int pvs_found = 0;
-       char *s1, *s2, *s3;
 
-       struct list *pvs;
-       struct list *pvh;
+       struct dm_list *pvslist;
        struct pv_list *pvl;
        struct physical_volume *pv;
 
@@ -43,38 +144,49 @@ int pvscan(int argc, char **argv)
        pv_max_name_len = 0;
        vg_max_name_len = 0;
 
-       if (arg_count(novolumegroup_ARG) && arg_count(exported_ARG)) {
+       if (arg_count(cmd, cache_ARG))
+               return _pvscan_lvmetad(cmd, argc, argv);
+
+       if (arg_count(cmd, novolumegroup_ARG) && arg_count(cmd, exported_ARG)) {
                log_error("Options -e and -n are incompatible");
                return EINVALID_CMD_LINE;
        }
 
-       if (arg_count(exported_ARG) || arg_count(novolumegroup_ARG))
-               log_print("WARNING: only considering physical volumes %s",
-                         arg_count(exported_ARG) ?
+       if (arg_count(cmd, exported_ARG) || arg_count(cmd, novolumegroup_ARG))
+               log_warn("WARNING: only considering physical volumes %s",
+                         arg_count(cmd, exported_ARG) ?
                          "of exported volume group(s)" : "in no volume group");
 
-       log_verbose("Wiping cache of LVM-capable devices");
-       persistent_filter_wipe(ios->filter);
+       if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE)) {
+               log_error("Unable to obtain global lock.");
+               return ECMD_FAILED;
+       }
+
+       persistent_filter_wipe(cmd->filter);
+       lvmcache_destroy(cmd, 1);
 
        log_verbose("Walking through all physical volumes");
-       if (!(pvs = ios->get_pvs(ios)))
+       if (!(pvslist = get_pvs(cmd))) {
+               unlock_vg(cmd, VG_GLOBAL);
+               stack;
                return ECMD_FAILED;
+       }
 
        /* eliminate exported/new if required */
-       list_iterate(pvh, pvs) {
-               pvl = list_item(pvh, struct pv_list);
-               pv = &pvl->pv;
+       dm_list_iterate_items(pvl, pvslist) {
+               pv = pvl->pv;
 
-               if ((arg_count(exported_ARG) && !(pv->status & EXPORTED_VG))
-                   || (arg_count(novolumegroup_ARG) && (*pv->vg_name))) {
-                       list_del(&pvl->list);
+               if ((arg_count(cmd, exported_ARG)
+                    && !(pv_status(pv) & EXPORTED_VG))
+                   || (arg_count(cmd, novolumegroup_ARG) && (!is_orphan(pv)))) {
+                       dm_list_del(&pvl->list);
                        continue;
                }
 
                /* Also check for MD use? */
 /*******
                if (MAJOR(pv_create_kdev_t(pv[p]->pv_name)) != MD_MAJOR) {
-                       log_print
+                       log_warn
                            ("WARNING: physical volume \"%s\" belongs to a meta device",
                             pv[p]->pv_name);
                }
@@ -83,129 +195,47 @@ int pvscan(int argc, char **argv)
 ********/
                pvs_found++;
 
-
-               if (!*pv->vg_name) {
+               if (is_orphan(pv)) {
                        new_pvs_found++;
-                       size_new += pv->size;
-                       size_total += pv->size;
+                       size_new += pv_size(pv);
+                       size_total += pv_size(pv);
                } else
-                       size_total += (pv->pe_count - pv->pe_allocated)
-                                     * pv->pe_size;
+                       size_total += (uint64_t) pv_pe_count(pv) * pv_pe_size(pv);
        }
 
        /* find maximum pv name length */
        pv_max_name_len = vg_max_name_len = 0;
-       list_iterate(pvh, pvs) {
-               pv = &list_item(pvh, struct pv_list)->pv;
-               len = strlen(dev_name(pv->dev));
+       dm_list_iterate_items(pvl, pvslist) {
+               pv = pvl->pv;
+               len = strlen(pv_dev_name(pv));
                if (pv_max_name_len < len)
                        pv_max_name_len = len;
-               len = strlen(pv->vg_name);
+               len = strlen(pv_vg_name(pv));
                if (vg_max_name_len < len)
                        vg_max_name_len = len;
        }
        pv_max_name_len += 2;
        vg_max_name_len += 2;
 
-       list_iterate(pvh, pvs)
-               pvscan_display_single(&list_item(pvh, struct pv_list)->pv);
+       dm_list_iterate_items(pvl, pvslist) {
+               _pvscan_display_single(cmd, pvl->pv, NULL);
+               free_pv_fid(pvl->pv);
+       }
 
        if (!pvs_found) {
                log_print("No matching physical volumes found");
-               return 0;
+               unlock_vg(cmd, VG_GLOBAL);
+               return ECMD_PROCESSED;
        }
 
        log_print("Total: %d [%s] / in use: %d [%s] / in no VG: %d [%s]",
                  pvs_found,
-                 (s1 = display_size(size_total / 2, SIZE_SHORT)),
+                 display_size(cmd, size_total),
                  pvs_found - new_pvs_found,
-                 (s2 =
-                  display_size((size_total - size_new) / 2, SIZE_SHORT)),
-                 new_pvs_found, (s3 = display_size(size_new / 2, SIZE_SHORT)));
-       dbg_free(s1);
-       dbg_free(s2);
-       dbg_free(s3);
-
-       return 0;
-}
-
-void pvscan_display_single(struct physical_volume *pv)
-{
+                 display_size(cmd, (size_total - size_new)),
+                 new_pvs_found, display_size(cmd, size_new));
 
-       int vg_name_len = 0;
-       const char *active_str;
-
-       char *s1, *s2;
-
-       char pv_tmp_name[NAME_LEN] = { 0, };
-       char vg_tmp_name[NAME_LEN] = { 0, };
-       char vg_name_this[NAME_LEN] = { 0, };
-
-       /* short listing? */
-       if (arg_count(short_ARG) > 0) {
-               log_print("%s", dev_name(pv->dev));
-               return;
-       }
-
-       if (arg_count(verbose_ARG) > 1) {
-               /* FIXME As per pv_display! Drop through for now. */
-               /* pv_show(pv); */
-
-               log_print("System Id             %s", pv->exported);
-
-               /* log_print(" "); */
-               /* return; */
-       }
-
-       memset(pv_tmp_name, 0, sizeof (pv_tmp_name));
-
-       active_str = (pv->status & ACTIVE) ? "ACTIVE  " : "Inactive";
-
-       vg_name_len = strlen(pv->vg_name) - sizeof (EXPORTED_TAG) + 1;
-
-       if (arg_count(uuid_ARG)) {
-               sprintf(pv_tmp_name,
-                       "%-*s with UUID %s",
-                       pv_max_name_len - 2,
-                       dev_name(pv->dev), display_uuid(pv->id.uuid));
-       } else {
-               sprintf(pv_tmp_name, "%s", dev_name(pv->dev));
-       }
-
-       if (!*pv->vg_name) {
-               log_print("%s PV %-*s is in no VG %-*s [%s]", active_str,
-                         pv_max_name_len, pv_tmp_name,
-                         vg_max_name_len - 6, " ",
-                         (s1 = display_size(pv->size / 2, SIZE_SHORT)));
-               dbg_free(s1);
-               return;
-       }
-
-       if (strcmp(&pv->vg_name[vg_name_len], EXPORTED_TAG) == 0) {
-               strncpy(vg_name_this, pv->vg_name, vg_name_len);
-               log_print("%s PV %-*s  is in EXPORTED VG %s [%s / %s free]",
-                         active_str, pv_max_name_len, pv_tmp_name,
-                         vg_name_this, (s1 =
-                                        display_size(pv->pe_count *
-                                                     pv->pe_size / 2,
-                                                     SIZE_SHORT)),
-                         (s2 = display_size((pv->pe_count - pv->pe_allocated)
-                                            * pv->pe_size / 2, SIZE_SHORT)));
-               dbg_free(s1);
-               dbg_free(s2);
-               return;
-       }
+       unlock_vg(cmd, VG_GLOBAL);
 
-       sprintf(vg_tmp_name, "%s", pv->vg_name);
-       log_print
-           ("%s PV %-*s of VG %-*s [%s / %s free]", active_str, pv_max_name_len,
-            pv_tmp_name, vg_max_name_len, vg_tmp_name,
-            (s1 = display_size(pv->pe_count * pv->pe_size / 2, SIZE_SHORT)),
-            (s2 =
-             display_size((pv->pe_count - pv->pe_allocated) * pv->pe_size / 2,
-                          SIZE_SHORT)));
-       dbg_free(s1);
-       dbg_free(s2);
-
-       return;
+       return ECMD_PROCESSED;
 }
This page took 0.031183 seconds and 5 git commands to generate.