forked from intel/pmem-csi
/
namespace.go
481 lines (400 loc) · 12.1 KB
/
namespace.go
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package ndctl
//#cgo pkg-config: libndctl
//#include <string.h>
//#include <stdlib.h>
//#include <ndctl/libndctl.h>
//#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
//#include <ndctl/ndctl.h>
import "C"
import (
"encoding/json"
"fmt"
/* needed for nullify
"os"
"syscall"*/
"unsafe"
"github.com/google/uuid"
)
const (
//PmemNamespace pmem type namespace
PmemNamespace NamespaceType = "pmem"
//BlockNamespace block type namespace
BlockNamespace NamespaceType = "blk"
//IoNamespace io type namespace
IoNamespace NamespaceType = "io"
//UnknownType unknown namespace
UnknownType NamespaceType = "unknown"
)
const (
DaxMode NamespaceMode = "dax" //DevDax
FsdaxMode NamespaceMode = "fsdax" //Memory
RawMode NamespaceMode = "raw"
SectorMode NamespaceMode = "sector"
UnknownMode NamespaceMode = "unknown"
)
const (
MemoryMap MapLocation = "mem" // RAM
DeviceMap MapLocation = "dev" // Block Device
NoneMap MapLocation = "none"
)
//NamespaceType type to represent namespace type
type NamespaceType string
//NamespaceMode represents mode of the namespace
type NamespaceMode string
func (mode NamespaceMode) toCMode() C.enum_ndctl_namespace_mode {
switch mode {
case DaxMode:
return C.NDCTL_NS_MODE_DAX
case FsdaxMode:
return C.NDCTL_NS_MODE_FSDAX
case RawMode:
return C.NDCTL_NS_MODE_RAW
case SectorMode:
return C.NDCTL_NS_MODE_SAFE
}
return C.NDCTL_NS_MODE_UNKNOWN
}
type MapLocation string
func (loc MapLocation) toCPfnLocation() C.enum_ndctl_pfn_loc {
if loc == MemoryMap {
return C.NDCTL_PFN_LOC_RAM
} else if loc == DeviceMap {
return C.NDCTL_PFN_LOC_PMEM
}
return C.NDCTL_PFN_LOC_NONE
}
// Namespace go wrapper for ndctl_namespace
type Namespace C.struct_ndctl_namespace
//ID returns namespace id
func (ns *Namespace) ID() uint {
ndns := (*C.struct_ndctl_namespace)(ns)
return uint(C.ndctl_namespace_get_id(ndns))
}
//Name returns name of the namespace
func (ns *Namespace) Name() string {
ndns := (*C.struct_ndctl_namespace)(ns)
return C.GoString(C.ndctl_namespace_get_alt_name(ndns))
}
//DeviceName returns namespace device name
func (ns *Namespace) DeviceName() string {
ndns := (*C.struct_ndctl_namespace)(ns)
return C.GoString(C.ndctl_namespace_get_devname(ndns))
}
//BlockDeviceName return namespace block device name
func (ns *Namespace) BlockDeviceName() string {
ndns := (*C.struct_ndctl_namespace)(ns)
if DaxMode := C.ndctl_namespace_get_dax(ndns); DaxMode != nil {
/* Chardevice */
return ""
}
var dev *C.char
btt := C.ndctl_namespace_get_btt(ndns)
pfn := C.ndctl_namespace_get_pfn(ndns)
if btt != nil {
dev = C.ndctl_btt_get_block_device(btt)
} else if pfn != nil {
dev = C.ndctl_pfn_get_block_device(pfn)
} else {
dev = C.ndctl_namespace_get_block_device(ndns)
}
return C.GoString(dev)
}
//Size returns size of the namespace
func (ns *Namespace) Size() uint64 {
var size C.ulonglong
ndns := (*C.struct_ndctl_namespace)(ns)
mode := ns.Mode()
switch mode {
case FsdaxMode:
if pfn := C.ndctl_namespace_get_pfn(ndns); pfn != nil {
size = C.ndctl_pfn_get_size(pfn)
} else {
size = C.ndctl_namespace_get_size(ndns)
}
case DaxMode:
if DaxMode := C.ndctl_namespace_get_dax(ndns); DaxMode != nil {
size = C.ndctl_dax_get_size(DaxMode)
}
case SectorMode:
if btt := C.ndctl_namespace_get_btt(ndns); btt != nil {
size = C.ndctl_btt_get_size(btt)
}
case RawMode:
size = C.ndctl_namespace_get_size(ndns)
}
return uint64(size)
}
//Mode returns namespace mode
func (ns *Namespace) Mode() NamespaceMode {
ndns := (*C.struct_ndctl_namespace)(ns)
mode := C.ndctl_namespace_get_mode(ndns)
if mode == C.NDCTL_NS_MODE_DAX || mode == C.NDCTL_NS_MODE_DEVDAX {
return DaxMode
}
if mode == C.NDCTL_NS_MODE_MEMORY || mode == C.NDCTL_NS_MODE_FSDAX {
return FsdaxMode
}
if mode == C.NDCTL_NS_MODE_RAW {
return RawMode
}
if mode == C.NDCTL_NS_MODE_SAFE {
return SectorMode
}
return UnknownMode
}
//Type returns namespace type
func (ns *Namespace) Type() NamespaceType {
ndns := (*C.struct_ndctl_namespace)(ns)
switch C.ndctl_namespace_get_type(ndns) {
case C.ND_DEVICE_NAMESPACE_PMEM:
return PmemNamespace
case C.ND_DEVICE_NAMESPACE_BLK:
return BlockNamespace
case C.ND_DEVICE_NAMESPACE_IO:
return IoNamespace
}
return UnknownType
}
//Enabled return if namespace is enabled
func (ns *Namespace) Enabled() bool {
ndns := (*C.struct_ndctl_namespace)(ns)
return C.ndctl_namespace_is_enabled(ndns) == 1
}
//Active return if namespace is active
func (ns *Namespace) Active() bool {
ndns := (*C.struct_ndctl_namespace)(ns)
return bool(C.ndctl_namespace_is_active(ndns))
}
//UUID returns uuid of the namespace
func (ns *Namespace) UUID() uuid.UUID {
ndns := (*C.struct_ndctl_namespace)(ns)
var cuid C.uuid_t
btt := C.ndctl_namespace_get_btt(ndns)
DaxMode := C.ndctl_namespace_get_dax(ndns)
pfn := C.ndctl_namespace_get_pfn(ndns)
if btt != nil {
C.ndctl_btt_get_uuid(btt, &cuid[0])
} else if pfn != nil {
C.ndctl_pfn_get_uuid(pfn, &cuid[0])
} else if DaxMode != nil {
C.ndctl_dax_get_uuid(DaxMode, &cuid[0])
} else if C.ndctl_namespace_get_type(ndns) != C.ND_DEVICE_NAMESPACE_IO {
C.ndctl_namespace_get_uuid(ndns, &cuid[0])
}
uidbytes := C.GoBytes(unsafe.Pointer(&cuid[0]), C.sizeof_uuid_t)
_uuid, err := uuid.FromBytes(uidbytes)
if err != nil {
fmt.Printf("WARN: wrong uuid: %s", err.Error())
return uuid.UUID{}
}
return _uuid
}
//Location returns namespace mapping location
func (ns *Namespace) Location() MapLocation {
ndns := (*C.struct_ndctl_namespace)(ns)
locations := map[uint32]MapLocation{
C.NDCTL_PFN_LOC_NONE: NoneMap,
C.NDCTL_PFN_LOC_RAM: MemoryMap,
C.NDCTL_PFN_LOC_PMEM: DeviceMap,
}
mode := ns.Mode()
switch mode {
case FsdaxMode:
if pfn := C.ndctl_namespace_get_pfn(ndns); pfn != nil {
return locations[C.ndctl_pfn_get_location(pfn)]
}
return locations[C.NDCTL_PFN_LOC_RAM]
case DaxMode:
if dax := C.ndctl_namespace_get_dax(ndns); dax != nil {
return locations[C.ndctl_dax_get_location(dax)]
}
}
return locations[C.NDCTL_PFN_LOC_NONE]
}
//Region returns reference to Region that this namespace is part of
func (ns *Namespace) Region() *Region {
ndns := (*C.struct_ndctl_namespace)(ns)
return (*Region)(C.ndctl_namespace_get_region(ndns))
}
func (ns *Namespace) SetAltName(name string) error {
ndns := (*C.struct_ndctl_namespace)(ns)
if rc := C.ndctl_namespace_set_alt_name(ndns, C.CString(name)); rc != 0 {
return fmt.Errorf("Failed to set namespace name: %s", cErrorString(rc))
}
return nil
}
func (ns *Namespace) SetSize(size uint64) error {
ndns := (*C.struct_ndctl_namespace)(ns)
if rc := C.ndctl_namespace_set_size(ndns, C.ulonglong(size)); rc != 0 {
return fmt.Errorf("Failed to set namespace size: %s", cErrorString(rc))
}
return nil
}
func (ns *Namespace) SetUUID(uid uuid.UUID) error {
ndns := (*C.struct_ndctl_namespace)(ns)
if rc := C.ndctl_namespace_set_uuid(ndns, (*C.uchar)(&uid[0])); rc != 0 {
return fmt.Errorf("Failed to set namespace uid: %s", cErrorString(rc))
}
return nil
}
func (ns *Namespace) SetSectorSize(sectorSize uint64) error {
ndns := (*C.struct_ndctl_namespace)(ns)
if sectorSize == 0 {
sectorSize = 512
}
sSize := C.uint(sectorSize)
for num := C.ndctl_namespace_get_num_sector_sizes(ndns) - 1; num >= 0; num-- {
if C.uint(C.ndctl_namespace_get_supported_sector_size(ndns, num)) == sSize {
if rc := C.ndctl_namespace_set_sector_size(ndns, sSize); rc < 0 {
return fmt.Errorf("Failed to set namespace sector size: %s", cErrorString(rc))
}
return nil
}
}
return fmt.Errorf("Sector size %v not supported", sectorSize)
}
func (ns *Namespace) SetEnforceMode(mode NamespaceMode) error {
ndns := (*C.struct_ndctl_namespace)(ns)
if rc := C.ndctl_namespace_set_enforce_mode(ndns, mode.toCMode()); rc != 0 {
return fmt.Errorf("Failed to set enforce mode: %s", cErrorString(rc))
}
return nil
}
func (ns *Namespace) Enable() error {
ndns := (*C.struct_ndctl_namespace)(ns)
if rc := C.ndctl_namespace_enable(ndns); rc < 0 {
return fmt.Errorf("failed to enable namespace:%s", cErrorString(rc))
}
return nil
}
//MarshalJSON returns json encoding of namespace
func (ns *Namespace) MarshalJSON() ([]byte, error) {
props := map[string]interface{}{
"id": ns.ID(),
"dev": ns.DeviceName(),
"mode": ns.Mode(),
"size": ns.Size(),
"enabled": ns.Enabled(),
"uuid": ns.UUID(),
"name": ns.Name(),
}
if mode := ns.Mode(); mode != DaxMode {
props["blockdev"] = ns.BlockDeviceName()
}
if location := ns.Location(); location != "none" {
props["map"] = location
}
return json.Marshal(props)
}
func (ns *Namespace) setPfnSeed(loc MapLocation, align uint64) error {
var rc C.int
ndns := (*C.struct_ndctl_namespace)(ns)
ndr := (*C.struct_ndctl_region)(ns.Region())
pfn := C.ndctl_region_get_pfn_seed(ndr)
if pfn == nil {
return fmt.Errorf("pfn: no seed")
}
uid, _ := uuid.NewUUID()
if rc = C.ndctl_pfn_set_uuid(pfn, (*C.uchar)(&uid[0])); rc < 0 {
return fmt.Errorf("pfn: failed to set: %s", cErrorString(rc))
}
if rc = C.ndctl_pfn_set_location(pfn, loc.toCPfnLocation()); rc < 0 {
return fmt.Errorf("pfn: failed to set location")
}
if align != 0 && C.ndctl_pfn_has_align(pfn) == 1 {
if rc = C.ndctl_pfn_set_align(pfn, C.ulong(align)); rc < 0 {
return fmt.Errorf("pfn: failed to set alignment: %s", cErrorString(rc))
}
}
if rc = C.ndctl_pfn_set_namespace(pfn, ndns); rc < 0 {
return fmt.Errorf("pfn: failed to set namespace")
}
if rc = C.ndctl_pfn_enable(pfn); rc < 0 {
// reset pfn seed in failure case
C.ndctl_pfn_set_namespace(pfn, nil)
return fmt.Errorf("pfn: failed to enable")
}
return nil
}
func (ns *Namespace) setDaxSeed(loc MapLocation, align uint64) error {
var rc C.int
ndns := (*C.struct_ndctl_namespace)(ns)
ndr := (*C.struct_ndctl_region)(ns.Region())
dax := C.ndctl_region_get_dax_seed(ndr)
if dax == nil {
return fmt.Errorf("dax: no seed")
}
uid, _ := uuid.NewUUID()
if rc = C.ndctl_dax_set_uuid(dax, (*C.uchar)(&uid[0])); rc < 0 {
return fmt.Errorf("dax: failed to set uuid")
}
if rc = C.ndctl_dax_set_location(dax, loc.toCPfnLocation()); rc < 0 {
return fmt.Errorf("dax: failed to set dax location")
}
/* device-dax assumes 'align' attribute present */
if align != 0 {
if rc = C.ndctl_dax_set_align(dax, C.ulong(align)); rc < 0 {
return fmt.Errorf("dax: failed to set dax alignment")
}
}
if rc = C.ndctl_dax_set_namespace(dax, ndns); rc < 0 {
return fmt.Errorf("dax: failed to set namespace")
}
if rc = C.ndctl_dax_enable(dax); rc < 0 {
C.ndctl_dax_set_namespace(dax, nil)
return fmt.Errorf("dax: failed to enable")
}
return nil
}
func (ns *Namespace) setBttSeed(sectorSize uint64) error {
ndns := (*C.struct_ndctl_namespace)(ns)
ndr := (*C.struct_ndctl_region)(ns.Region())
btt := C.ndctl_region_get_btt_seed(ndr)
var rc C.int
if btt == nil {
return fmt.Errorf("btt: no seed")
}
uid, _ := uuid.NewUUID()
if rc = C.ndctl_btt_set_uuid(btt, (*C.uchar)(&uid[0])); rc < 0 {
return fmt.Errorf("btt: failed to set btt")
}
if rc = C.ndctl_btt_set_sector_size(btt, C.uint(sectorSize)); rc < 0 {
return fmt.Errorf("btt: failed to set sector size")
}
if rc = C.ndctl_btt_set_namespace(btt, ndns); rc < 0 {
return fmt.Errorf("btt: failed to set namespace")
}
if rc = C.ndctl_btt_enable(btt); rc < 0 {
C.ndctl_btt_set_namespace(btt, nil)
return fmt.Errorf("btt: failed to enable")
}
return nil
}
/* nullify commented out as unused, was not reliable
func (ns *Namespace) nullify() error {
ndns := (*C.struct_ndctl_namespace)(ns)
var rc C.int
var buf unsafe.Pointer
var err error
var file *os.File
C.ndctl_namespace_set_raw_mode(ndns, 1)
defer C.ndctl_namespace_set_raw_mode(ndns, 0)
if err = ns.Enable(); err != nil {
return err
}
if rc = C.posix_memalign(&buf, C.size_t(kib4), C.size_t(kib4)); rc < 0 {
return fmt.Errorf("memory error: %s", cErrorString(rc))
}
defer C.free(buf)
C.memset(buf, 0, C.size_t(kib4))
file, err = os.OpenFile("/dev/"+ns.BlockDeviceName(), os.O_RDWR|syscall.O_DIRECT|os.O_EXCL, os.ModeDevice)
if err != nil {
return err
}
defer file.Close()
bytes := *(*[]byte)(buf)
if _, err = file.Write(bytes); err != nil {
err = fmt.Errorf("failed to zero info block %s", err.Error())
}
return err
}*/