+/*
+ * This allows a given thread to recursively call lock_reservations.
+ * It must, of course, call unlock_... the same number of times.
+ */
+void init_reservations_lock()
+{
+ int errstat;
+ if ((errstat=rwl_init(&reservation_lock)) != 0) {
+ berrno be;
+ Emsg1(M_ABORT, 0, _("Unable to initialize reservation lock. ERR=%s\n"),
+ be.bstrerror(errstat));
+ }
+
+ if ((errstat=rwl_init(&vol_list_lock)) != 0) {
+ berrno be;
+ Emsg1(M_ABORT, 0, _("Unable to initialize volume list lock. ERR=%s\n"),
+ be.bstrerror(errstat));
+ }
+}
+
+void term_reservations_lock()
+{
+ rwl_destroy(&reservation_lock);
+ rwl_destroy(&vol_list_lock);
+}
+
+int reservations_lock_count = 0;
+
+/* This applies to a drive and to Volumes */
+void _lock_reservations()
+{
+ int errstat;
+ reservations_lock_count++;
+ if ((errstat=rwl_writelock(&reservation_lock)) != 0) {
+ berrno be;
+ Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+void _unlock_reservations()
+{
+ int errstat;
+ reservations_lock_count--;
+ if ((errstat=rwl_writeunlock(&reservation_lock)) != 0) {
+ berrno be;
+ Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+int vol_list_lock_count = 0;
+
+/*
+ * This allows a given thread to recursively call to lock_volumes()
+ */
+void _lock_volumes()
+{
+ int errstat;
+ vol_list_lock_count++;
+ if ((errstat=rwl_writelock(&vol_list_lock)) != 0) {
+ berrno be;
+ Emsg2(M_ABORT, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+void _unlock_volumes()
+{
+ int errstat;
+ vol_list_lock_count--;
+ if ((errstat=rwl_writeunlock(&vol_list_lock)) != 0) {
+ berrno be;
+ Emsg2(M_ABORT, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
+ errstat, be.bstrerror(errstat));
+ }
+}
+
+
+/*
+ * List Volumes -- this should be moved to status.c
+ */
+enum {
+ debug_lock = true,
+ debug_nolock = false
+};
+
+void debug_list_volumes(const char *imsg)
+{
+ VOLRES *vol;
+ POOL_MEM msg(PM_MESSAGE);
+
+ lock_volumes();
+ foreach_dlist(vol, vol_list) {
+ if (vol->dev) {
+ Mmsg(msg, "List %s: %s in_use=%d on device %s\n", imsg,
+ vol->vol_name, vol->is_in_use(), vol->dev->print_name());
+ } else {
+ Mmsg(msg, "List %s: %s in_use=%d no dev\n", imsg, vol->vol_name,
+ vol->is_in_use());
+ }
+ Dmsg1(dbglvl, "%s", msg.c_str());
+ }
+
+ unlock_volumes();
+}
+
+
+/*
+ * List Volumes -- this should be moved to status.c
+ */
+void list_volumes(void sendit(const char *msg, int len, void *sarg), void *arg)
+{
+ VOLRES *vol;
+ POOL_MEM msg(PM_MESSAGE);
+ int len;
+
+ lock_volumes();
+ foreach_dlist(vol, vol_list) {
+ DEVICE *dev = vol->dev;
+ if (dev) {
+ len = Mmsg(msg, "%s on device %s\n", vol->vol_name, dev->print_name());
+ sendit(msg.c_str(), len, arg);
+ len = Mmsg(msg, " Reader=%d writers=%d devres=%d volinuse=%d\n",
+ dev->can_read()?1:0, dev->num_writers, dev->num_reserved(),
+ vol->is_in_use());
+ sendit(msg.c_str(), len, arg);
+ } else {
+ len = Mmsg(msg, "%s no device. volinuse= %d\n", vol->vol_name,
+ vol->is_in_use());
+ sendit(msg.c_str(), len, arg);
+ }
+ }
+ unlock_volumes();
+}
+
+/*
+ * Create a Volume item to put in the Volume list
+ * Ensure that the device points to it.
+ */
+static VOLRES *new_vol_item(DCR *dcr, const char *VolumeName)
+{
+ VOLRES *vol;
+ vol = (VOLRES *)malloc(sizeof(VOLRES));
+ memset(vol, 0, sizeof(VOLRES));
+ vol->vol_name = bstrdup(VolumeName);
+ vol->dev = dcr->dev;
+ Dmsg3(dbglvl, "new Vol=%s at %p dev=%s\n",
+ VolumeName, vol->vol_name, vol->dev->print_name());
+ return vol;
+}
+
+static void free_vol_item(VOLRES *vol)
+{
+ DEVICE *dev = NULL;
+
+ free(vol->vol_name);
+ if (vol->dev) {
+ dev = vol->dev;
+ }
+ free(vol);
+ if (dev) {
+ dev->vol = NULL;
+ }
+}