File: disconnect.c

package info (click to toggle)
libnbd 1.24.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,956 kB
  • sloc: ansic: 55,158; ml: 12,325; sh: 8,811; python: 4,757; makefile: 3,038; perl: 165; cpp: 24
file content (89 lines) | stat: -rw-r--r-- 2,742 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include <inttypes.h>
#include <assert.h>

#include "internal.h"

int
nbd_unlocked_shutdown (struct nbd_handle *h, uint32_t flags)
{
  /* If we are still in opt mode, end it gracefully. */
  if (nbd_internal_is_state_negotiating (get_next_state (h))) {
    if (nbd_unlocked_aio_opt_abort (h) == -1)
      return -1;
    goto wait;
  }

  /* If ABANDON_PENDING, abort any commands that have not yet had any
   * bytes sent to the server, so NBD_CMD_DISC becomes next in line.
   */
  if (flags & LIBNBD_SHUTDOWN_ABANDON_PENDING) {
    struct command **cmd = &h->cmds_to_issue;
    if (!nbd_internal_is_state_ready (get_next_state (h))) {
      assert (*cmd);
      h->cmds_to_issue_tail = *cmd;
      cmd = &(*cmd)->next;
    }
    nbd_internal_abort_commands (h, cmd);
  }

  if (!h->disconnect_request &&
      (nbd_internal_is_state_ready (get_next_state (h)) ||
       nbd_internal_is_state_processing (get_next_state (h)))) {
    if (nbd_unlocked_aio_disconnect (h, 0) == -1)
      return -1;
  }

 wait:
  while (!nbd_internal_is_state_closed (get_next_state (h)) &&
         !nbd_internal_is_state_dead (get_next_state (h))) {
    if (nbd_unlocked_poll (h, -1) == -1)
      return -1;
  }

  return 0;
}

int
nbd_unlocked_aio_disconnect (struct nbd_handle *h, uint32_t flags)
{
  int64_t id;

  id = nbd_internal_command_common (h, flags, NBD_CMD_DISC, 0, 0, 0,
                                    NULL, NULL);
  if (id == -1)
    return -1;
  h->disconnect_request = true;

  /* As the server does not reply to this command, it is left
   * in-flight until the cleanup performed when moving to CLOSED or
   * DEAD.  We don't return a handle to the user, and thus also
   * special case things so that the user cannot request the status of
   * this command during aio_[peek_]command_completed.
   */
  return 0;
}