File: flags.c

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (286 lines) | stat: -rw-r--r-- 7,761 bytes parent folder | download | duplicates (2)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* 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 <inttypes.h>
#include <errno.h>
#include <assert.h>

#include "internal.h"
#include "minmax.h"

/* Reset connection data.  Called after swapping export name, after
 * failed OPT_GO/OPT_INFO, or after successful OPT_STARTTLS.
 */
void
nbd_internal_reset_size_and_flags (struct nbd_handle *h)
{
  h->exportsize = 0;
  h->eflags = 0;
  h->block_minimum = 0;
  h->block_preferred = 0;
  h->block_maximum = 0;
  h->payload_maximum = 0;
  free (h->canonical_name);
  h->canonical_name = NULL;
  free (h->description);
  h->description = NULL;
}

/* Set the export size and eflags on the handle, validating them.
 * This is called from the state machine when either the newstyle or
 * oldstyle negotiation reaches the point that these are available.
 */
int
nbd_internal_set_size_and_flags (struct nbd_handle *h,
                                 uint64_t exportsize, uint16_t eflags)
{
  debug (h, "exportsize: %" PRIu64 " eflags: 0x%" PRIx16, exportsize, eflags);

  if (eflags == 0) {
    set_error (EINVAL, "handshake: invalid eflags == 0 from server");
    return -1;
  }

  if (eflags & NBD_FLAG_SEND_DF && !h->structured_replies) {
    debug (h, "server lacks structured replies, ignoring claim of df");
    eflags &= ~NBD_FLAG_SEND_DF;
  }

  if (eflags & NBD_FLAG_BLOCK_STATUS_PAYLOAD && !h->extended_headers) {
    debug (h, "server lacks extended headers, ignoring claim "
           "of block status payload");
    eflags &= ~NBD_FLAG_BLOCK_STATUS_PAYLOAD;
  }

  if (eflags & NBD_FLAG_SEND_FAST_ZERO &&
      !(eflags & NBD_FLAG_SEND_WRITE_ZEROES)) {
    debug (h, "server lacks write zeroes, ignoring claim of fast zero");
    eflags &= ~NBD_FLAG_SEND_FAST_ZERO;
  }

  if (h->request_meta &&
      (!h->structured_replies || h->request_meta_contexts.len == 0)) {
    assert (h->meta_contexts.len == 0);
    h->meta_valid = true;
  }

  h->exportsize = exportsize;
  h->eflags = eflags;
  return 0;
}

/* Set the block size constraints on the handle, validating them.
 * This is called from the state machine if newstyle negotiation encounters
 * a server advertisement.
 */
int
nbd_internal_set_block_size (struct nbd_handle *h, uint32_t min,
                             uint32_t pref, uint32_t max)
{
  debug (h, "server block size constraints: min: %u preferred: %u max: %u",
         min, pref, max);
  /* NBD spec recommends:
   *  min and pref are power of 2
   *  min <= MIN(pref, 64k)
   *  pref >= 512
   *  max >= MIN(MAX(pref, 1M), exportsize)
   *  max is either multiple of min or 0xffffffff
   *  exportsize is multiple of min
   * At the point of this call, we don't necessarily know exportsize yet;
   * but we ignore server advertisement if any other constraints are wrong.
   */
  if (!min || min > 64*1024 || min > pref || pref < 512 || pref > max)
    goto ignore;
  if ((min & (min - 1)) != 0 || (pref & (pref - 1)) != 0)
    goto ignore;
  if (max != 0xffffffffU && max % min != 0)
    goto ignore;

  h->block_minimum = min;
  h->block_preferred = pref;
  h->block_maximum = max;
  return 0;

 ignore:
  debug (h, "ignoring improper server size constraints");
  return 0; /* Use return -1 if we want to reject such servers */
}

/* Set the payload size constraint on the handle.
 * This is called from the state machine after all other information
 * about an export is available, regardless of oldstyle or newstyle.
 */
void
nbd_internal_set_payload (struct nbd_handle *h)
{
  /* The NBD protocol says we should not assume the server will allow
   * more than 32M payload if it did not advertise anything.  If it
   * advertised more than 64M, we still cap at the maximum buffer size
   * we are willing to allocate as a client; if it advertised smaller
   * than 1M (presumably because the export itself was small), the NBD
   * protocol says we can still send payloads that large.
   */
  if (h->block_maximum)
    h->payload_maximum = MIN (MAX_REQUEST_SIZE,
                              MAX (1024 * 1024, h->block_maximum));
  else
    h->payload_maximum = 32 * 1024 * 1024;
}

static int
get_flag (struct nbd_handle *h, uint16_t flag)
{
  if (h->eflags == 0) {
    set_error (EINVAL, "server has not returned export flags, "
               "you need to connect to the server first");
    return -1;
  }

  return (h->eflags & flag) != 0;
}

int
nbd_unlocked_is_read_only (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_READ_ONLY);
}

int
nbd_unlocked_can_flush (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_FLUSH);
}

int
nbd_unlocked_can_fua (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_FUA);
}

int
nbd_unlocked_is_rotational (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_ROTATIONAL);
}

int
nbd_unlocked_can_trim (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_TRIM);
}

int
nbd_unlocked_can_zero (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_WRITE_ZEROES);
}

int
nbd_unlocked_can_fast_zero (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_FAST_ZERO);
}

int
nbd_unlocked_can_df (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_DF);
}

int
nbd_unlocked_can_multi_conn (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_CAN_MULTI_CONN);
}

int
nbd_unlocked_can_cache (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_SEND_CACHE);
}

int
nbd_unlocked_can_block_status_payload (struct nbd_handle *h)
{
  return get_flag (h, NBD_FLAG_BLOCK_STATUS_PAYLOAD);
}

int
nbd_unlocked_can_meta_context (struct nbd_handle *h, const char *name)
{
  size_t i;

  if (h->request_meta_contexts.len && !h->meta_valid) {
    set_error (EINVAL, "need a successful server meta context request first");
    return -1;
  }

  for (i = 0; i < h->meta_contexts.len; ++i)
    if (strcmp (h->meta_contexts.ptr[i].name, name) == 0)
      return 1;
  return 0;
}

int64_t
nbd_unlocked_get_size (struct nbd_handle *h)
{
  /* exportsize is only valid when we've read both the eflags and the
   * exportsize.  See comment in lib/internal.h.
   */
  if (h->eflags == 0) {
    set_error (EINVAL, "server has not returned export size, "
               "you need to connect to the server first");
    return -1;
  }

  if (h->exportsize > INT64_MAX) {
    set_error (EOVERFLOW, "server claims size %" PRIu64
               " which does not fit in signed result", h->exportsize);
    return -1;
  }

  return h->exportsize;
}

int64_t
nbd_unlocked_get_block_size (struct nbd_handle *h, int type)
{
  if (h->eflags == 0) {
    set_error (EINVAL, "server has not returned export flags, "
               "you need to connect to the server first");
    return -1;
  }

  switch (type) {
  case LIBNBD_SIZE_MINIMUM:
    return h->block_minimum;
  case LIBNBD_SIZE_PREFERRED:
    return h->block_preferred;
  case LIBNBD_SIZE_MAXIMUM:
    return h->block_maximum;
  case LIBNBD_SIZE_PAYLOAD:
    return h->payload_maximum;
  }
  return 0;
}