4 * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU General Public License v.2.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define _FILE_OFFSET_BITS 64
21 #include <configure.h>
23 #include <sys/types.h>
24 #include <sys/utsname.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
39 #include <libdevmapper.h>
42 #include "lvm-types.h"
44 #include "clvmd-comms.h"
46 #include "lvm-functions.h"
49 #include "toolcontext.h"
51 #include "lvm-logging.h"
52 #include "lvm-globals.h"
58 static struct cmd_context
*cmd
= NULL
;
59 static struct dm_hash_table
*lv_hash
= NULL
;
60 static pthread_mutex_t lv_hash_lock
;
61 static pthread_mutex_t lvm_lock
;
62 static char last_error
[1024];
63 static int suspended
= 0;
70 static const char *decode_locking_cmd(unsigned char cmdl
)
77 switch (cmdl
& LCK_TYPE_MASK
) {
101 switch (cmdl
& LCK_SCOPE_MASK
) {
113 switch (cmdl
& LCK_MASK
) {
114 case LCK_LV_EXCLUSIVE
& LCK_MASK
:
115 command
= "LCK_LV_EXCLUSIVE";
117 case LCK_LV_SUSPEND
& LCK_MASK
:
118 command
= "LCK_LV_SUSPEND";
120 case LCK_LV_RESUME
& LCK_MASK
:
121 command
= "LCK_LV_RESUME";
123 case LCK_LV_ACTIVATE
& LCK_MASK
:
124 command
= "LCK_LV_ACTIVATE";
126 case LCK_LV_DEACTIVATE
& LCK_MASK
:
127 command
= "LCK_LV_DEACTIVATE";
134 sprintf(buf
, "0x%x %s (%s|%s%s%s%s%s%s)", cmdl
, command
, type
, scope
,
135 cmdl
& LCK_NONBLOCK
? "|NONBLOCK" : "",
136 cmdl
& LCK_HOLD
? "|HOLD" : "",
137 cmdl
& LCK_LOCAL
? "|LOCAL" : "",
138 cmdl
& LCK_CLUSTER_VG
? "|CLUSTER_VG" : "",
139 cmdl
& LCK_CACHE
? "|CACHE" : "");
144 static const char *decode_flags(unsigned char flags
)
146 static char buf
[128];
148 sprintf(buf
, "0x%x (%s%s%s%s)", flags
,
149 flags
& LCK_PARTIAL_MODE
? "PARTIAL_MODE " : "",
150 flags
& LCK_MIRROR_NOSYNC_MODE
? "MIRROR_NOSYNC " : "",
151 flags
& LCK_DMEVENTD_MONITOR_MODE
? "DMEVENTD_MONITOR " : "",
152 flags
& LCK_CONVERT
? "CONVERT " : "");
157 char *get_last_lvm_error()
163 * Hash lock info helpers
165 static struct lv_info
*lookup_info(const char *resource
)
169 pthread_mutex_lock(&lv_hash_lock
);
170 lvi
= dm_hash_lookup(lv_hash
, resource
);
171 pthread_mutex_unlock(&lv_hash_lock
);
176 static void insert_info(const char *resource
, struct lv_info
*lvi
)
178 pthread_mutex_lock(&lv_hash_lock
);
179 dm_hash_insert(lv_hash
, resource
, lvi
);
180 pthread_mutex_unlock(&lv_hash_lock
);
183 static void remove_info(const char *resource
)
185 pthread_mutex_lock(&lv_hash_lock
);
186 dm_hash_remove(lv_hash
, resource
);
187 pthread_mutex_unlock(&lv_hash_lock
);
191 * Return the mode a lock is currently held at (or -1 if not held)
193 static int get_current_lock(char *resource
)
197 if ((lvi
= lookup_info(resource
)))
198 return lvi
->lock_mode
;
206 /* Create hash table for keeping LV locks & status */
207 lv_hash
= dm_hash_create(100);
208 pthread_mutex_init(&lv_hash_lock
, NULL
);
209 pthread_mutex_init(&lvm_lock
, NULL
);
212 /* Called at shutdown to tidy the lockspace */
213 void destroy_lvhash()
215 struct dm_hash_node
*v
;
220 pthread_mutex_lock(&lv_hash_lock
);
222 dm_hash_iterate(v
, lv_hash
) {
223 lvi
= dm_hash_get_data(lv_hash
, v
);
224 resource
= dm_hash_get_key(lv_hash
, v
);
226 if ((status
= sync_unlock(resource
, lvi
->lock_id
)))
227 DEBUGLOG("unlock_all. unlock failed(%d): %s\n",
228 status
, strerror(errno
));
232 dm_hash_destroy(lv_hash
);
235 pthread_mutex_unlock(&lv_hash_lock
);
238 /* Gets a real lock and keeps the info in the hash table */
239 int hold_lock(char *resource
, int mode
, int flags
)
245 /* Mask off invalid options */
246 flags
&= LKF_NOQUEUE
| LKF_CONVERT
;
248 lvi
= lookup_info(resource
);
250 /* Only allow explicit conversions */
251 if (lvi
&& !(flags
& LKF_CONVERT
)) {
256 /* Already exists - convert it */
258 sync_lock(resource
, mode
, flags
, &lvi
->lock_id
);
261 lvi
->lock_mode
= mode
;
264 DEBUGLOG("hold_lock. convert to %d failed: %s\n", mode
,
269 lvi
= malloc(sizeof(struct lv_info
));
273 lvi
->lock_mode
= mode
;
274 status
= sync_lock(resource
, mode
, flags
, &lvi
->lock_id
);
278 DEBUGLOG("hold_lock. lock at %d failed: %s\n", mode
,
281 insert_info(resource
, lvi
);
288 /* Unlock and remove it from the hash table */
289 int hold_unlock(char *resource
)
295 if (!(lvi
= lookup_info(resource
))) {
296 DEBUGLOG("hold_unlock, lock not already held\n");
300 status
= sync_unlock(resource
, lvi
->lock_id
);
303 remove_info(resource
);
306 DEBUGLOG("hold_unlock. unlock failed(%d): %s\n", status
,
314 /* Watch the return codes here.
315 liblvm API functions return 1(true) for success, 0(false) for failure and don't set errno.
316 libdlm API functions return 0 for success, -1 for failure and do set errno.
317 These functions here return 0 for success or >0 for failure (where the retcode is errno)
320 /* Activate LV exclusive or non-exclusive */
321 static int do_activate_lv(char *resource
, unsigned char lock_flags
, int mode
)
329 /* Is it already open ? */
330 oldmode
= get_current_lock(resource
);
331 if (oldmode
== mode
) {
332 return 0; /* Nothing to do */
335 /* Does the config file want us to activate this LV ? */
336 if (!lv_activation_filter(cmd
, resource
, &activate_lv
))
340 return 0; /* Success, we did nothing! */
342 /* Do we need to activate exclusively? */
343 if ((activate_lv
== 2) || (mode
== LKM_EXMODE
)) {
348 /* Try to get the lock if it's a clustered volume group */
349 if (lock_flags
& LCK_CLUSTER_VG
) {
350 status
= hold_lock(resource
, mode
, LKF_NOQUEUE
| (lock_flags
& LCK_CONVERT
?LKF_CONVERT
:0));
352 /* Return an LVM-sensible error for this.
353 * Forcing EIO makes the upper level return this text
354 * rather than the strerror text for EAGAIN.
356 if (errno
== EAGAIN
) {
357 sprintf(last_error
, "Volume is busy on another node");
364 /* If it's suspended then resume it */
365 if (!lv_info_by_lvid(cmd
, resource
, &lvi
, 0, 0))
369 if (!lv_resume(cmd
, resource
))
372 /* Now activate it */
373 if (!lv_activate(cmd
, resource
, exclusive
))
379 /* Resume the LV if it was active */
380 static int do_resume_lv(char *resource
)
385 oldmode
= get_current_lock(resource
);
387 DEBUGLOG("do_resume_lv, lock not already held\n");
388 return 0; /* We don't need to do anything */
391 if (!lv_resume_if_active(cmd
, resource
))
397 /* Suspend the device if active */
398 static int do_suspend_lv(char *resource
)
404 oldmode
= get_current_lock(resource
);
406 DEBUGLOG("do_suspend_lv, lock held at %d\n", oldmode
);
407 return 0; /* Not active, so it's OK */
410 /* Only suspend it if it exists */
411 if (!lv_info_by_lvid(cmd
, resource
, &lvi
, 0, 0))
415 if (!lv_suspend_if_active(cmd
, resource
)) {
422 static int do_deactivate_lv(char *resource
, unsigned char lock_flags
)
428 oldmode
= get_current_lock(resource
);
429 if (oldmode
== -1 && (lock_flags
& LCK_CLUSTER_VG
)) {
430 DEBUGLOG("do_deactivate_lock, lock not already held\n");
431 return 0; /* We don't need to do anything */
434 if (!lv_deactivate(cmd
, resource
))
437 if (lock_flags
& LCK_CLUSTER_VG
) {
438 status
= hold_unlock(resource
);
446 const char *do_lock_query(char *resource
)
449 const char *type
= NULL
;
451 mode
= get_current_lock(resource
);
453 case LKM_NLMODE
: type
= "NL"; break;
454 case LKM_CRMODE
: type
= "CR"; break;
455 case LKM_CWMODE
: type
= "CW"; break;
456 case LKM_PRMODE
: type
= "PR"; break;
457 case LKM_PWMODE
: type
= "PW"; break;
458 case LKM_EXMODE
: type
= "EX"; break;
461 DEBUGLOG("do_lock_query: resource '%s', mode %i (%s)\n", resource
, mode
, type
?: "?");
466 /* This is the LOCK_LV part that happens on all nodes in the cluster -
467 it is responsible for the interaction with device-mapper and LVM */
468 int do_lock_lv(unsigned char command
, unsigned char lock_flags
, char *resource
)
472 DEBUGLOG("do_lock_lv: resource '%s', cmd = %s, flags = %s\n",
473 resource
, decode_locking_cmd(command
), decode_flags(lock_flags
));
475 if (!cmd
->config_valid
|| config_files_changed(cmd
)) {
476 /* Reinitialise various settings inc. logging, filters */
477 if (do_refresh_cache()) {
478 log_error("Updated config file invalid. Aborting.");
483 pthread_mutex_lock(&lvm_lock
);
484 if (lock_flags
& LCK_MIRROR_NOSYNC_MODE
)
485 init_mirror_in_sync(1);
487 if (!(lock_flags
& LCK_DMEVENTD_MONITOR_MODE
))
488 init_dmeventd_monitor(0);
490 cmd
->partial_activation
= (lock_flags
& LCK_PARTIAL_MODE
) ? 1 : 0;
492 switch (command
& LCK_MASK
) {
493 case LCK_LV_EXCLUSIVE
:
494 status
= do_activate_lv(resource
, lock_flags
, LKM_EXMODE
);
498 status
= do_suspend_lv(resource
);
504 case LCK_LV_RESUME
: /* if active */
505 status
= do_resume_lv(resource
);
510 case LCK_LV_ACTIVATE
:
511 status
= do_activate_lv(resource
, lock_flags
, LKM_CRMODE
);
514 case LCK_LV_DEACTIVATE
:
515 status
= do_deactivate_lv(resource
, lock_flags
);
519 DEBUGLOG("Invalid LV command 0x%x\n", command
);
524 if (lock_flags
& LCK_MIRROR_NOSYNC_MODE
)
525 init_mirror_in_sync(0);
527 if (!(lock_flags
& LCK_DMEVENTD_MONITOR_MODE
))
528 init_dmeventd_monitor(DEFAULT_DMEVENTD_MONITOR
);
530 cmd
->partial_activation
= 0;
532 /* clean the pool for another command */
533 dm_pool_empty(cmd
->mem
);
534 pthread_mutex_unlock(&lvm_lock
);
536 DEBUGLOG("Command return is %d\n", status
);
540 /* Functions to do on the local node only BEFORE the cluster-wide stuff above happens */
541 int pre_lock_lv(unsigned char command
, unsigned char lock_flags
, char *resource
)
543 /* Nearly all the stuff happens cluster-wide. Apart from SUSPEND. Here we get the
544 lock out on this node (because we are the node modifying the metadata)
545 before suspending cluster-wide.
547 if (command
== LCK_LV_SUSPEND
) {
548 DEBUGLOG("pre_lock_lv: resource '%s', cmd = %s, flags = %s\n",
549 resource
, decode_locking_cmd(command
), decode_flags(lock_flags
));
551 if (hold_lock(resource
, LKM_PWMODE
, LKF_NOQUEUE
| (lock_flags
& LCK_CONVERT
?LKF_CONVERT
:0)))
557 /* Functions to do on the local node only AFTER the cluster-wide stuff above happens */
558 int post_lock_lv(unsigned char command
, unsigned char lock_flags
,
563 /* Opposite of above, done on resume after a metadata update */
564 if (command
== LCK_LV_RESUME
) {
568 ("post_lock_lv: resource '%s', cmd = %s, flags = %s\n",
569 resource
, decode_locking_cmd(command
), decode_flags(lock_flags
));
571 /* If the lock state is PW then restore it to what it was */
572 oldmode
= get_current_lock(resource
);
573 if (oldmode
== LKM_PWMODE
) {
576 pthread_mutex_lock(&lvm_lock
);
577 status
= lv_info_by_lvid(cmd
, resource
, &lvi
, 0, 0);
578 pthread_mutex_unlock(&lvm_lock
);
583 if (hold_lock(resource
, LKM_CRMODE
, lock_flags
& LCK_CONVERT
?LKF_CONVERT
:0))
586 if (hold_unlock(resource
))
594 /* Check if a VG is in use by LVM1 so we don't stomp on it */
595 int do_check_lvm1(const char *vgname
)
599 status
= check_lvm1_vg_inactive(cmd
, vgname
);
601 return status
== 1 ? 0 : EBUSY
;
604 int do_refresh_cache()
606 DEBUGLOG("Refreshing context\n");
607 log_notice("Refreshing context");
609 pthread_mutex_lock(&lvm_lock
);
611 if (!refresh_toolcontext(cmd
)) {
612 pthread_mutex_unlock(&lvm_lock
);
616 init_full_scan_done(0);
617 lvmcache_label_scan(cmd
, 2);
618 dm_pool_empty(cmd
->mem
);
620 pthread_mutex_unlock(&lvm_lock
);
626 /* Only called at gulm startup. Drop any leftover VG or P_orphan locks
627 that might be hanging around if we died for any reason
629 static void drop_vg_locks()
635 ("lvm pvs --config 'log{command_names=0 prefix=\"\"}' --nolocking --noheadings -o vg_name", "r");
637 sync_unlock("P_" VG_ORPHANS
, LCK_EXCL
);
638 sync_unlock("P_" VG_GLOBAL
, LCK_EXCL
);
643 while (fgets(line
, sizeof(line
), vgs
)) {
647 if (line
[strlen(line
)-1] == '\n')
648 line
[strlen(line
)-1] = '\0';
650 vgstart
= line
+ strspn(line
, " ");
651 vgend
= vgstart
+ strcspn(vgstart
, " ");
654 if (strncmp(vgstart
, "WARNING:", 8) == 0)
657 sprintf(vg
, "V_%s", vgstart
);
658 sync_unlock(vg
, LCK_EXCL
);
662 DEBUGLOG("vgs fclose failed: %s\n", strerror(errno
));
666 * Drop lvmcache metadata
668 void drop_metadata(const char *vgname
)
670 DEBUGLOG("Dropping metadata for VG %s\n", vgname
);
671 pthread_mutex_lock(&lvm_lock
);
672 lvmcache_drop_metadata(vgname
);
673 pthread_mutex_unlock(&lvm_lock
);
677 * Ideally, clvmd should be started before any LVs are active
678 * but this may not be the case...
679 * I suppose this also comes in handy if clvmd crashes, not that it would!
681 static void *get_initial_state()
683 char lv
[64], vg
[64], flags
[25], vg_flags
[25];
688 ("lvm lvs --config 'log{command_names=0 prefix=\"\"}' --nolocking --noheadings -o vg_uuid,lv_uuid,lv_attr,vg_attr",
694 while (fgets(line
, sizeof(line
), lvs
)) {
695 if (sscanf(line
, "%s %s %s %s\n", vg
, lv
, flags
, vg_flags
) == 4) {
697 /* States: s:suspended a:active S:dropped snapshot I:invalid snapshot */
698 if (strlen(vg
) == 38 && /* is is a valid UUID ? */
699 (flags
[4] == 'a' || flags
[4] == 's') && /* is it active or suspended? */
700 vg_flags
[5] == 'c') { /* is it clustered ? */
701 /* Convert hyphen-separated UUIDs into one */
702 memcpy(&uuid
[0], &vg
[0], 6);
703 memcpy(&uuid
[6], &vg
[7], 4);
704 memcpy(&uuid
[10], &vg
[12], 4);
705 memcpy(&uuid
[14], &vg
[17], 4);
706 memcpy(&uuid
[18], &vg
[22], 4);
707 memcpy(&uuid
[22], &vg
[27], 4);
708 memcpy(&uuid
[26], &vg
[32], 6);
709 memcpy(&uuid
[32], &lv
[0], 6);
710 memcpy(&uuid
[38], &lv
[7], 4);
711 memcpy(&uuid
[42], &lv
[12], 4);
712 memcpy(&uuid
[46], &lv
[17], 4);
713 memcpy(&uuid
[50], &lv
[22], 4);
714 memcpy(&uuid
[54], &lv
[27], 4);
715 memcpy(&uuid
[58], &lv
[32], 6);
718 DEBUGLOG("getting initial lock for %s\n", uuid
);
719 hold_lock(uuid
, LKM_CRMODE
, LKF_NOQUEUE
);
724 DEBUGLOG("lvs fclose failed: %s\n", strerror(errno
));
728 static void lvm2_log_fn(int level
, const char *file
, int line
, int dm_errno
,
732 /* Send messages to the normal LVM2 logging system too,
733 so we get debug output when it's asked for.
734 We need to NULL the function ptr otherwise it will just call
737 print_log(level
, file
, line
, dm_errno
, "%s", message
);
738 init_log_fn(lvm2_log_fn
);
741 * Ignore non-error messages, but store the latest one for returning
744 if (level
!= _LOG_ERR
&& level
!= _LOG_FATAL
)
747 strncpy(last_error
, message
, sizeof(last_error
));
748 last_error
[sizeof(last_error
)-1] = '\0';
751 /* This checks some basic cluster-LVM configuration stuff */
752 static void check_config()
756 locking_type
= find_config_tree_int(cmd
, "global/locking_type", 1);
758 if (locking_type
== 3) /* compiled-in cluster support */
761 if (locking_type
== 2) { /* External library, check name */
764 libname
= find_config_tree_str(cmd
, "global/locking_library",
766 if (strstr(libname
, "liblvm2clusterlock.so"))
769 log_error("Incorrect LVM locking library specified in lvm.conf, cluster operations may not work.");
772 log_error("locking_type not set correctly in lvm.conf, cluster operations will not work.");
775 /* Backups up the LVM metadata if it's changed */
776 void lvm_do_backup(const char *vgname
)
778 struct volume_group
* vg
;
781 DEBUGLOG("Triggering backup of VG metadata for %s. suspended=%d\n", vgname
, suspended
);
783 pthread_mutex_lock(&lvm_lock
);
785 vg
= vg_read_internal(cmd
, vgname
, NULL
/*vgid*/, &consistent
);
787 if (vg
&& consistent
)
788 check_current_backup(vg
);
790 log_error("Error backing up metadata, can't find VG for group %s", vgname
);
793 dm_pool_empty(cmd
->mem
);
795 pthread_mutex_unlock(&lvm_lock
);
798 /* Called to initialise the LVM context of the daemon */
799 int init_lvm(int using_gulm
)
801 if (!(cmd
= create_toolcontext(1, NULL
))) {
802 log_error("Failed to allocate command context");
806 if (stored_errno()) {
807 destroy_toolcontext(cmd
);
811 /* Use LOG_DAEMON for syslog messages instead of LOG_USER */
812 init_syslog(LOG_DAEMON
);
813 openlog("clvmd", LOG_PID
, LOG_DAEMON
);
814 cmd
->cmd_line
= "clvmd";
816 /* Check lvm.conf is setup for cluster-LVM */
819 /* Remove any non-LV locks that may have been left around */
825 /* Trap log messages so we can pass them back to the user */
826 init_log_fn(lvm2_log_fn
);
831 void destroy_lvm(void)
834 destroy_toolcontext(cmd
);