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
|
/*-
* Copyright (c) 2008, 2009 Yahoo!, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <err.h>
#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mfiutil.h"
MFI_TABLE(top, volume);
const char *
mfi_ldstate(enum mfi_ld_state state)
{
static char buf[16];
switch (state) {
case MFI_LD_STATE_OFFLINE:
return ("OFFLINE");
case MFI_LD_STATE_PARTIALLY_DEGRADED:
return ("PARTIALLY DEGRADED");
case MFI_LD_STATE_DEGRADED:
return ("DEGRADED");
case MFI_LD_STATE_OPTIMAL:
return ("OPTIMAL");
default:
sprintf(buf, "LSTATE 0x%02x", state);
return (buf);
}
}
void
mbox_store_ldref(uint8_t *mbox, union mfi_ld_ref *ref)
{
mbox[0] = ref->v.target_id;
mbox[1] = ref->v.reserved;
mbox[2] = ref->v.seq & 0xff;
mbox[3] = ref->v.seq >> 8;
}
int
mfi_ld_get_list(int fd, struct mfi_ld_list *list, uint8_t *statusp)
{
return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_LIST, list,
sizeof(struct mfi_ld_list), NULL, 0, statusp));
}
int
mfi_ld_get_info(int fd, uint8_t target_id, struct mfi_ld_info *info,
uint8_t *statusp)
{
uint8_t mbox[1];
mbox[0] = target_id;
return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_INFO, info,
sizeof(struct mfi_ld_info), mbox, 1, statusp));
}
static int
mfi_ld_get_props(int fd, uint8_t target_id, struct mfi_ld_props *props)
{
uint8_t mbox[1];
mbox[0] = target_id;
return (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_PROP, props,
sizeof(struct mfi_ld_props), mbox, 1, NULL));
}
static int
mfi_ld_set_props(int fd, struct mfi_ld_props *props)
{
uint8_t mbox[4];
mbox_store_ldref(mbox, &props->ld);
return (mfi_dcmd_command(fd, MFI_DCMD_LD_SET_PROP, props,
sizeof(struct mfi_ld_props), mbox, 4, NULL));
}
static int
update_cache_policy(int fd, struct mfi_ld_props *props, uint8_t new_policy,
uint8_t mask)
{
uint8_t changes, policy;
policy = (props->default_cache_policy & ~mask) | new_policy;
if (policy == props->default_cache_policy)
return (0);
changes = policy ^ props->default_cache_policy;
if (changes & MR_LD_CACHE_ALLOW_WRITE_CACHE)
printf("%s caching of I/O writes\n",
policy & MR_LD_CACHE_ALLOW_WRITE_CACHE ? "Enabling" :
"Disabling");
if (changes & MR_LD_CACHE_ALLOW_READ_CACHE)
printf("%s caching of I/O reads\n",
policy & MR_LD_CACHE_ALLOW_READ_CACHE ? "Enabling" :
"Disabling");
if (changes & MR_LD_CACHE_WRITE_BACK)
printf("Setting write cache policy to %s\n",
policy & MR_LD_CACHE_WRITE_BACK ? "write-back" :
"write-through");
if (changes & (MR_LD_CACHE_READ_AHEAD | MR_LD_CACHE_READ_ADAPTIVE))
printf("Setting read ahead policy to %s\n",
policy & MR_LD_CACHE_READ_AHEAD ?
(policy & MR_LD_CACHE_READ_ADAPTIVE ?
"adaptive" : "always") : "none");
props->default_cache_policy = policy;
if (mfi_ld_set_props(fd, props) < 0) {
warn("Failed to set volume properties");
return (errno);
}
return (0);
}
static int
volume_cache(int ac, char **av)
{
struct mfi_ld_props props;
int error, fd;
uint8_t target_id, policy;
if (ac < 2) {
warnx("cache: volume required");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
warn("mfi_open");
return (errno);
}
if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
warn("Invalid volume: %s", av[1]);
return (errno);
}
if (mfi_ld_get_props(fd, target_id, &props) < 0) {
warn("Failed to fetch volume properties");
return (errno);
}
if (ac == 2) {
printf("mfi%u volume %s cache settings:\n", mfi_unit,
mfi_volume_name(fd, target_id));
printf(" I/O caching: ");
switch (props.default_cache_policy &
(MR_LD_CACHE_ALLOW_WRITE_CACHE |
MR_LD_CACHE_ALLOW_READ_CACHE)) {
case 0:
printf("disabled\n");
break;
case MR_LD_CACHE_ALLOW_WRITE_CACHE:
printf("writes\n");
break;
case MR_LD_CACHE_ALLOW_READ_CACHE:
printf("reads\n");
break;
case MR_LD_CACHE_ALLOW_WRITE_CACHE |
MR_LD_CACHE_ALLOW_READ_CACHE:
printf("writes and reads\n");
break;
}
printf(" write caching: %s\n",
props.default_cache_policy & MR_LD_CACHE_WRITE_BACK ?
"write-back" : "write-through");
printf(" read ahead: %s\n",
props.default_cache_policy & MR_LD_CACHE_READ_AHEAD ?
(props.default_cache_policy & MR_LD_CACHE_READ_ADAPTIVE ?
"adaptive" : "always") : "none");
printf("drive write cache: ");
switch (props.disk_cache_policy) {
case MR_PD_CACHE_UNCHANGED:
printf("default\n");
break;
case MR_PD_CACHE_ENABLE:
printf("enabled\n");
break;
case MR_PD_CACHE_DISABLE:
printf("disabled\n");
break;
default:
printf("??? %d\n", props.disk_cache_policy);
break;
}
if (props.default_cache_policy != props.current_cache_policy)
printf("Cache Disabled Due to Dead Battery\n");
error = 0;
} else {
if (strcmp(av[2], "all") == 0 || strcmp(av[2], "enable") == 0)
error = update_cache_policy(fd, &props,
MR_LD_CACHE_ALLOW_READ_CACHE |
MR_LD_CACHE_ALLOW_WRITE_CACHE,
MR_LD_CACHE_ALLOW_READ_CACHE |
MR_LD_CACHE_ALLOW_WRITE_CACHE);
else if (strcmp(av[2], "none") == 0 ||
strcmp(av[2], "disable") == 0)
error = update_cache_policy(fd, &props, 0,
MR_LD_CACHE_ALLOW_READ_CACHE |
MR_LD_CACHE_ALLOW_WRITE_CACHE);
else if (strcmp(av[2], "reads") == 0)
error = update_cache_policy(fd, &props,
MR_LD_CACHE_ALLOW_READ_CACHE,
MR_LD_CACHE_ALLOW_READ_CACHE |
MR_LD_CACHE_ALLOW_WRITE_CACHE);
else if (strcmp(av[2], "writes") == 0)
error = update_cache_policy(fd, &props,
MR_LD_CACHE_ALLOW_WRITE_CACHE,
MR_LD_CACHE_ALLOW_READ_CACHE |
MR_LD_CACHE_ALLOW_WRITE_CACHE);
else if (strcmp(av[2], "write-back") == 0)
error = update_cache_policy(fd, &props,
MR_LD_CACHE_WRITE_BACK,
MR_LD_CACHE_WRITE_BACK);
else if (strcmp(av[2], "write-through") == 0)
error = update_cache_policy(fd, &props, 0,
MR_LD_CACHE_WRITE_BACK);
else if (strcmp(av[2], "read-ahead") == 0) {
if (ac < 4) {
warnx("cache: read-ahead setting required");
return (EINVAL);
}
if (strcmp(av[3], "none") == 0)
policy = 0;
else if (strcmp(av[3], "always") == 0)
policy = MR_LD_CACHE_READ_AHEAD;
else if (strcmp(av[3], "adaptive") == 0)
policy = MR_LD_CACHE_READ_AHEAD |
MR_LD_CACHE_READ_ADAPTIVE;
else {
warnx("cache: invalid read-ahead setting");
return (EINVAL);
}
error = update_cache_policy(fd, &props, policy,
MR_LD_CACHE_READ_AHEAD |
MR_LD_CACHE_READ_ADAPTIVE);
} else if (strcmp(av[2], "write-cache") == 0) {
if (ac < 4) {
warnx("cache: write-cache setting required");
return (EINVAL);
}
if (strcmp(av[3], "enable") == 0)
policy = MR_PD_CACHE_ENABLE;
else if (strcmp(av[3], "disable") == 0)
policy = MR_PD_CACHE_DISABLE;
else if (strcmp(av[3], "default") == 0)
policy = MR_PD_CACHE_UNCHANGED;
else {
warnx("cache: invalid write-cache setting");
return (EINVAL);
}
error = 0;
if (policy != props.disk_cache_policy) {
switch (policy) {
case MR_PD_CACHE_ENABLE:
printf("Enabling write-cache on physical drives\n");
break;
case MR_PD_CACHE_DISABLE:
printf("Disabling write-cache on physical drives\n");
break;
case MR_PD_CACHE_UNCHANGED:
printf("Using default write-cache setting on physical drives\n");
break;
}
props.disk_cache_policy = policy;
if (mfi_ld_set_props(fd, &props) < 0) {
warn("Failed to set volume properties");
error = errno;
}
}
} else {
warnx("cache: Invalid command");
return (EINVAL);
}
}
close(fd);
return (error);
}
MFI_COMMAND(top, cache, volume_cache);
static int
volume_name(int ac, char **av)
{
struct mfi_ld_props props;
int fd;
uint8_t target_id;
if (ac != 3) {
warnx("name: volume and name required");
return (EINVAL);
}
if (strlen(av[2]) >= sizeof(props.name)) {
warnx("name: new name is too long");
return (ENOSPC);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
warn("mfi_open");
return (errno);
}
if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
warn("Invalid volume: %s", av[1]);
return (errno);
}
if (mfi_ld_get_props(fd, target_id, &props) < 0) {
warn("Failed to fetch volume properties");
return (errno);
}
printf("mfi%u volume %s name changed from \"%s\" to \"%s\"\n", mfi_unit,
mfi_volume_name(fd, target_id), props.name, av[2]);
bzero(props.name, sizeof(props.name));
strcpy(props.name, av[2]);
if (mfi_ld_set_props(fd, &props) < 0) {
warn("Failed to set volume properties");
return (errno);
}
close(fd);
return (0);
}
MFI_COMMAND(top, name, volume_name);
static int
volume_progress(int ac, char **av)
{
struct mfi_ld_info info;
int fd;
uint8_t target_id;
if (ac != 2) {
warnx("volume progress: %s", ac > 2 ? "extra arguments" :
"volume required");
return (EINVAL);
}
fd = mfi_open(mfi_unit);
if (fd < 0) {
warn("mfi_open");
return (errno);
}
if (mfi_lookup_volume(fd, av[1], &target_id) < 0) {
warn("Invalid volume: %s", av[1]);
return (errno);
}
/* Get the info for this drive. */
if (mfi_ld_get_info(fd, target_id, &info, NULL) < 0) {
warn("Failed to fetch info for volume %s",
mfi_volume_name(fd, target_id));
return (errno);
}
/* Display any of the active events. */
if (info.progress.active & MFI_LD_PROGRESS_CC)
mfi_display_progress("Consistency Check", &info.progress.cc);
if (info.progress.active & MFI_LD_PROGRESS_BGI)
mfi_display_progress("Background Init", &info.progress.bgi);
if (info.progress.active & MFI_LD_PROGRESS_FGI)
mfi_display_progress("Foreground Init", &info.progress.fgi);
if (info.progress.active & MFI_LD_PROGRESS_RECON)
mfi_display_progress("Reconstruction", &info.progress.recon);
if ((info.progress.active & (MFI_LD_PROGRESS_CC | MFI_LD_PROGRESS_BGI |
MFI_LD_PROGRESS_FGI | MFI_LD_PROGRESS_RECON)) == 0)
printf("No activity in progress for volume %s.\n",
mfi_volume_name(fd, target_id));
close(fd);
return (0);
}
MFI_COMMAND(volume, progress, volume_progress);
|