forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl_storage_rpc.go
392 lines (330 loc) · 12.5 KB
/
ctl_storage_rpc.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
//
// (C) Copyright 2019 Intel Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package server
import (
"fmt"
"strings"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/daos-stack/daos/src/control/common/proto"
ctlpb "github.com/daos-stack/daos/src/control/common/proto/ctl"
"github.com/daos-stack/daos/src/control/fault"
"github.com/daos-stack/daos/src/control/logging"
"github.com/daos-stack/daos/src/control/server/storage"
"github.com/daos-stack/daos/src/control/server/storage/bdev"
"github.com/daos-stack/daos/src/control/server/storage/scm"
)
const (
msgFormatErr = "failure formatting storage, check RPC response for details"
msgNvmeFormatSkip = "NVMe format skipped on instance %d as SCM format did not complete"
)
// newState creates, populates and returns ResponseState in addition
// to logging any err.
func newState(log logging.Logger, status ctlpb.ResponseStatus, errMsg string, infoMsg string,
contextMsg string) *ctlpb.ResponseState {
state := &ctlpb.ResponseState{
Status: status, Error: errMsg, Info: infoMsg,
}
if errMsg != "" {
log.Error(contextMsg + ": " + errMsg)
}
return state
}
func (c *StorageControlService) doNvmePrepare(req *ctlpb.PrepareNvmeReq) (resp *ctlpb.PrepareNvmeResp) {
resp = &ctlpb.PrepareNvmeResp{}
msg := "Storage Prepare NVMe"
_, err := c.NvmePrepare(bdev.PrepareRequest{
HugePageCount: int(req.GetNrhugepages()),
TargetUser: req.GetTargetuser(),
PCIWhitelist: req.GetPciwhitelist(),
ResetOnly: req.GetReset_(),
})
if err != nil {
resp.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_NVME, err.Error(), "", msg)
return
}
resp.State = newState(c.log, ctlpb.ResponseStatus_CTL_SUCCESS, "", "", msg)
return
}
func (c *StorageControlService) doScmPrepare(pbReq *ctlpb.PrepareScmReq) (pbResp *ctlpb.PrepareScmResp) {
pbResp = &ctlpb.PrepareScmResp{}
msg := "Storage Prepare SCM"
scmState, err := c.GetScmState()
if err != nil {
pbResp.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM, err.Error(), "", msg)
return
}
c.log.Debugf("SCM state before prep: %s", scmState)
resp, err := c.ScmPrepare(scm.PrepareRequest{Reset: pbReq.Reset_})
if err != nil {
pbResp.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM, err.Error(), "", msg)
return
}
info := ""
if resp.RebootRequired {
info = scm.MsgScmRebootRequired
}
pbResp.Namespaces = make(proto.ScmNamespaces, 0, len(resp.Namespaces))
if err := (*proto.ScmNamespaces)(&pbResp.Namespaces).FromNative(resp.Namespaces); err != nil {
pbResp.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM, err.Error(), "", msg)
return
}
pbResp.State = newState(c.log, ctlpb.ResponseStatus_CTL_SUCCESS, "", info, msg)
return
}
// StoragePrepare configures SSDs for user specific access with SPDK and
// groups SCM modules in AppDirect/interleaved mode as kernel "pmem" devices.
func (c *StorageControlService) StoragePrepare(ctx context.Context, req *ctlpb.StoragePrepareReq) (
*ctlpb.StoragePrepareResp, error) {
c.log.Debug("received StoragePrepare RPC; proceeding to instance storage preparation")
resp := &ctlpb.StoragePrepareResp{}
if req.Nvme != nil {
resp.Nvme = c.doNvmePrepare(req.Nvme)
}
if req.Scm != nil {
resp.Scm = c.doScmPrepare(req.Scm)
}
return resp, nil
}
// StorageScan discovers non-volatile storage hardware on node.
func (c *StorageControlService) StorageScan(ctx context.Context, req *ctlpb.StorageScanReq) (*ctlpb.StorageScanResp, error) {
c.log.Debug("received StorageScan RPC")
msg := "Storage Scan "
resp := new(ctlpb.StorageScanResp)
bsr, err := c.bdev.Scan(bdev.ScanRequest{})
if err != nil {
resp.Nvme = &ctlpb.ScanNvmeResp{
State: newState(c.log, ctlpb.ResponseStatus_CTL_ERR_NVME, err.Error(), "", msg+"NVMe"),
}
} else {
pbCtrlrs := make(proto.NvmeControllers, 0, len(bsr.Controllers))
if err := pbCtrlrs.FromNative(bsr.Controllers); err != nil {
c.log.Errorf("failed to cleanly convert %#v to protobuf: %s", bsr.Controllers, err)
}
resp.Nvme = &ctlpb.ScanNvmeResp{
State: newState(c.log, ctlpb.ResponseStatus_CTL_SUCCESS, "", "", msg+"NVMe"),
Ctrlrs: pbCtrlrs,
}
}
ssr, err := c.scm.Scan(scm.ScanRequest{})
if err != nil {
resp.Scm = &ctlpb.ScanScmResp{
State: newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM, err.Error(), "", msg+"SCM"),
}
} else {
msg += fmt.Sprintf("SCM (%s)", ssr.State)
resp.Scm = &ctlpb.ScanScmResp{
State: newState(c.log, ctlpb.ResponseStatus_CTL_SUCCESS, "", "", msg),
}
if len(ssr.Namespaces) > 0 {
resp.Scm.Namespaces = make(proto.ScmNamespaces, 0, len(ssr.Namespaces))
err := (*proto.ScmNamespaces)(&resp.Scm.Namespaces).FromNative(ssr.Namespaces)
if err != nil {
resp.Scm.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM,
err.Error(), "", msg+"SCM")
}
} else {
resp.Scm.Modules = make(proto.ScmModules, 0, len(ssr.Modules))
err := (*proto.ScmModules)(&resp.Scm.Modules).FromNative(ssr.Modules)
if err != nil {
resp.Scm.State = newState(c.log, ctlpb.ResponseStatus_CTL_ERR_SCM,
err.Error(), "", msg+"SCM")
}
}
}
c.log.Debug("responding to StorageScan RPC")
return resp, nil
}
// newMntRet creates and populates NVMe ctrlr result and logs error through newState.
func newMntRet(log logging.Logger, op, mntPoint string, status ctlpb.ResponseStatus, errMsg, infoMsg string) *ctlpb.ScmMountResult {
if mntPoint == "" {
mntPoint = "<nil>"
}
return &ctlpb.ScmMountResult{
Mntpoint: mntPoint,
State: newState(log, status, errMsg, infoMsg, "scm mount "+op),
}
}
// newCret creates and populates NVMe controller result and logs error
func newCret(log logging.Logger, op, pciAddr string, status ctlpb.ResponseStatus, errMsg, infoMsg string) *ctlpb.NvmeControllerResult {
if pciAddr == "" {
pciAddr = "<nil>"
}
return &ctlpb.NvmeControllerResult{
Pciaddr: pciAddr,
State: newState(log, status, errMsg, infoMsg, "nvme controller "+op),
}
}
func (c *ControlService) scmFormat(scmCfg storage.ScmConfig, reformat bool) (*ctlpb.ScmMountResult, error) {
var eMsg, iMsg string
status := ctlpb.ResponseStatus_CTL_SUCCESS
req, err := scm.CreateFormatRequest(scmCfg, reformat)
if err != nil {
return nil, errors.Wrap(err, "generate format request")
}
scmStr := fmt.Sprintf("SCM (%s:%s)", scmCfg.Class, scmCfg.MountPoint)
c.log.Infof("Starting format of %s", scmStr)
res, err := c.scm.Format(*req)
if err != nil {
eMsg = err.Error()
iMsg = fault.ShowResolutionFor(err)
status = ctlpb.ResponseStatus_CTL_ERR_SCM
} else if !res.Formatted {
err = scm.FaultUnknown
eMsg = errors.WithMessage(err, "is still unformatted").Error()
iMsg = fault.ShowResolutionFor(err)
status = ctlpb.ResponseStatus_CTL_ERR_SCM
}
if err != nil {
c.log.Errorf(" format of %s failed: %s", scmStr, err)
}
c.log.Infof("Finished format of %s", scmStr)
return newMntRet(c.log, "format", scmCfg.MountPoint, status, eMsg, iMsg), nil
}
// doFormat performs format on storage subsystems, populates response results
// in storage subsystem routines and broadcasts (closes channel) if successful.
func (c *ControlService) doFormat(i *IOServerInstance, reformat bool, resp *ctlpb.StorageFormatResp) error {
needsSuperblock := true
needsScmFormat := reformat
skipNvmeResult := newCret(c.log, "format", "", ctlpb.ResponseStatus_CTL_SUCCESS, "",
fmt.Sprintf(msgNvmeFormatSkip, i.Index()))
c.log.Infof("formatting storage for %s instance %d (reformat: %t)",
DataPlaneName, i.Index(), reformat)
scmConfig := i.scmConfig()
// If not reformatting, check if SCM is already formatted.
if !reformat {
var err error
needsScmFormat, err = i.NeedsScmFormat()
if err != nil {
return errors.Wrap(err, "unable to check storage formatting")
}
if !needsScmFormat {
err = scm.FaultFormatNoReformat
resp.Mrets = append(resp.Mrets,
newMntRet(c.log, "format", scmConfig.MountPoint,
ctlpb.ResponseStatus_CTL_ERR_SCM, err.Error(),
fault.ShowResolutionFor(err)))
if len(i.bdevConfig().DeviceList) > 0 {
resp.Crets = append(resp.Crets, skipNvmeResult)
}
return nil // don't continue if formatted and no reformat opt
}
}
// When SCM format is required, format and populate response with result.
if needsScmFormat {
results := proto.ScmMountResults{}
result, err := c.scmFormat(scmConfig, true)
if err != nil {
return errors.Wrap(err, "scm format") // return unexpected errors
}
results = append(results, result)
resp.Mrets = results
if results.HasErrors() {
c.log.Error(msgFormatErr)
if len(i.bdevConfig().DeviceList) > 0 {
resp.Crets = append(resp.Crets, skipNvmeResult)
}
return nil // don't continue if we can't format SCM
}
} else {
var err error
// If SCM was already formatted, verify if superblock exists.
needsSuperblock, err = i.NeedsSuperblock()
if err != nil {
return errors.Wrap(err, "unable to check instance superblock")
}
}
results := proto.NvmeControllerResults{} // init actual NVMe format results
// If no superblock exists, populate NVMe response with format results.
if needsSuperblock {
bdevConfig := i.bdevConfig()
// A config with SCM and no block devices is valid.
if len(bdevConfig.DeviceList) > 0 {
bdevListStr := strings.Join(bdevConfig.DeviceList, ",")
c.log.Infof("Starting format of %s block devices (%s)", bdevConfig.Class, bdevListStr)
res, err := c.bdev.Format(bdev.FormatRequest{
Class: bdevConfig.Class,
DeviceList: bdevConfig.DeviceList,
})
if err != nil {
return err
}
for dev, status := range res.DeviceResponses {
var errMsg, infoMsg string
ctlpbStatus := ctlpb.ResponseStatus_CTL_SUCCESS
if status.Error != nil {
ctlpbStatus = ctlpb.ResponseStatus_CTL_ERR_NVME
errMsg = status.Error.Error()
c.log.Errorf(" format of %s device %s failed: %s", bdevConfig.Class, dev, errMsg)
if fault.HasResolution(status.Error) {
infoMsg = fault.ShowResolutionFor(status.Error)
}
}
results = append(results,
newCret(c.log, "format", dev, ctlpbStatus, errMsg, infoMsg))
}
c.log.Infof("Finished format of %s block devices (%s)", bdevConfig.Class, bdevListStr)
}
}
resp.Crets = results // overwrite with actual results
if results.HasErrors() {
c.log.Error(msgFormatErr)
} else {
i.NotifyStorageReady()
}
return nil
}
// StorageFormat delegates to Storage implementation's Format methods to prepare
// storage for use by DAOS data plane.
//
// Errors returned will stop other servers from formatting, non-fatal errors
// specific to particular device should be reported within resp results instead.
//
// Send response containing multiple results of format operations on scm mounts
// and nvme controllers.
func (c *ControlService) StorageFormat(req *ctlpb.StorageFormatReq, stream ctlpb.MgmtCtl_StorageFormatServer) error {
resp := new(ctlpb.StorageFormatResp)
c.log.Debugf("received StorageFormat RPC %v; proceeding to instance storage format", req)
// TODO: We may want to ease this restriction at some point, but having this
// here for now should help to cut down on shenanigans which might result
// in data loss.
if c.harness.IsStarted() {
return errors.New("cannot format storage with running I/O server instances")
}
// temporary scaffolding
for _, i := range c.harness.Instances() {
if err := c.doFormat(i, req.Reformat, resp); err != nil {
return errors.WithMessage(err, "formatting storage")
}
}
if resp.Crets == nil {
// indicate that NVMe not yet formatted
resp.Crets = proto.NvmeControllerResults{
newCret(c.log, "format", "", ctlpb.ResponseStatus_CTL_ERR_NVME, msgBdevScmNotReady, ""),
}
}
if err := stream.Send(resp); err != nil {
return errors.WithMessagef(err, "sending response (%+v)", resp)
}
return nil
}