]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/camcontrol/depop.c
unbound: Import upstream 0ee44ef3 when ENOBUFS is returned
[FreeBSD/FreeBSD.git] / sbin / camcontrol / depop.c
1 /*-
2  * Copyright (c) 2021 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions, and the following disclaimer,
9  *    without modification.
10  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
11  *    substantially similar to the "NO WARRANTY" disclaimer below
12  *    ("Disclaimer") and any redistribution must be conditioned upon
13  *    including a substantially similar Disclaimer requirement for further
14  *    binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGES.
28  *
29  */
30 /*
31  * SCSI disk depop (head depopulation) support
32  *
33  * The standard defines 'storage elements' as the generic way of referring to a
34  * disk drive head. Each storage element has an identifier and an active status.
35  * The health of an element can be queried. Active elements may be removed from
36  * service with a REMOVE ELEMENT AND TRUNCATE (RET) command. Inactive element
37  * may be returned to service with a RESTORE ELEMENTS AND REBUILD (RER)
38  * command. GET PHYSICAL ELEMENT STATUS (GPES) will return a list of elements,
39  * their health, whether they are in service, how much capacity the element is
40  * used for, etc.
41  *
42  * When a depop operation starts, the drive becomes format corrupt. No normal
43  * I/O can be done to the drive and a limited number of CDBs will
44  * succeed. Status can be obtained by either a TEST UNIT READY or a GPES
45  * command. A drive reset will not stop a depop operation, but a power cycle
46  * will. A failed depop operation will be reported when the next TEST UNIT READY
47  * is sent to the drive. Drives that are format corrupt after an interrupted
48  * operation need to have that operation repeated.
49  *
50  * 'depop' provides a wrapper around all these functions.
51  */
52
53 #include <sys/cdefs.h>
54 #include <sys/types.h>
55
56 #include <err.h>
57 #include <inttypes.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include <cam/cam.h>
64 #include <cam/cam_debug.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68 #include <camlib.h>
69 #include <scsi_wrap.h>
70 #include "camcontrol.h"
71
72 enum depop_action {
73         DEPOP_NONE,
74         DEPOP_LIST,
75         DEPOP_RESTORE,
76         DEPOP_REMOVE,
77 };
78
79 static int
80 depop_list(struct cam_device *device, int task_attr, int retry_count,
81     int timeout, int verbosemode __unused)
82 {
83         int error = 0;
84         uint32_t dtors;
85         struct scsi_get_physical_element_hdr *hdr;
86         struct scsi_get_physical_element_descriptor *dtor_ptr;
87
88         hdr = scsi_wrap_get_physical_element_status(device, task_attr, retry_count, timeout,
89             SCSI_GPES_FILTER_ALL | SCSI_GPES_REPORT_TYPE_PHYS, 1);
90         if (hdr == NULL)
91                 errx(1, "scsi_wrap_get_physical_element_status returned an error");
92
93         /*
94          * OK, we have the data, not report it out.
95          */
96         dtor_ptr = (struct scsi_get_physical_element_descriptor *)(hdr + 1);
97         dtors = scsi_4btoul(hdr->num_descriptors);
98         printf("Elem ID    * Health Capacity\n");
99         for (uint32_t i = 0; i < dtors; i++) {
100                 uint32_t id = scsi_4btoul(dtor_ptr[i].element_identifier);
101                 uint8_t ralwd = dtor_ptr[i].ralwd;
102                 uint8_t type = dtor_ptr[i].physical_element_type;
103                 uint8_t health = dtor_ptr[i].physical_element_health;
104                 uint64_t cap = scsi_8btou64(dtor_ptr[i].capacity);
105                 if (type != GPED_TYPE_STORAGE)
106                         printf("0x%08x -- type unknown %d\n", id, type);
107                 else
108                         printf("0x%08x %c 0x%02x   %jd\n", id, ralwd ? '*' : ' ', health, cap);
109         }
110         printf("* -- Element can be restored\n");
111
112         free(hdr);
113         return (error);
114 }
115
116 static int
117 depop_remove(struct cam_device *device, int task_attr, int retry_count,
118     int timeout, int verbosemode __unused, uint32_t elem, uint64_t capacity)
119 {
120         union ccb *ccb;
121         int error = 0;
122
123         ccb = cam_getccb(device);
124         if (ccb == NULL) {
125                 warnx("Can't allocate ccb");
126                 return (1);
127         }
128         scsi_remove_element_and_truncate(&ccb->csio,
129             retry_count,
130             NULL,
131             task_attr,
132             capacity,
133             elem,
134             SSD_FULL_SIZE,
135             timeout);
136         /* Disable freezing the device queue */
137         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
138         if (cam_send_ccb(device, ccb) < 0) {
139                 warn("error sending GET PHYSICAL ELEMENT STATUS command");
140                 error = 1;
141                 goto out;
142         }
143
144         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
145                 cam_error_print(device, ccb, CAM_ESF_ALL,
146                                 CAM_EPF_ALL, stderr);
147                 error = 1;
148         }
149
150 out:
151         cam_freeccb(ccb);
152         return (error);
153 }
154
155 static int
156 depop_restore(struct cam_device *device, int task_attr, int retry_count,
157     int timeout, int verbosemode __unused)
158 {
159         union ccb *ccb;
160         int error = 0;
161
162         ccb = cam_getccb(device);
163         if (ccb == NULL) {
164                 warnx("Can't allocate ccb");
165                 return (1);
166         }
167         scsi_restore_elements_and_rebuild(&ccb->csio,
168             retry_count,
169             NULL,
170             task_attr,
171             SSD_FULL_SIZE,
172             timeout);
173
174         /* Disable freezing the device queue */
175         ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
176         if (cam_send_ccb(device, ccb) < 0) {
177                 warn("error sending GET PHYSICAL ELEMENT STATUS command");
178                 error = 1;
179                 goto out;
180         }
181
182         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
183                 cam_error_print(device, ccb, CAM_ESF_ALL,
184                                 CAM_EPF_ALL, stderr);
185                 error = 1;
186         }
187
188 out:
189         cam_freeccb(ccb);
190         return (error);
191 }
192
193 #define MUST_BE_NONE() \
194         if (action != DEPOP_NONE) { \
195                 warnx("Use only one of -d, -l, or -r"); \
196                 error = 1; \
197                 goto bailout; \
198         }
199
200 int
201 depop(struct cam_device *device, int argc, char **argv, char *combinedopt,
202     int task_attr, int retry_count, int timeout, int verbosemode)
203 {
204         int c;
205         int action = DEPOP_NONE;
206         char *endptr;
207         int error = 0;
208         uint32_t elem = 0;
209         uint64_t capacity = 0;
210
211         while ((c = getopt(argc, argv, combinedopt)) != -1) {
212                 switch (c) {
213                 case 'c':
214                         capacity = strtoumax(optarg, &endptr, 0);
215                         if (*endptr != '\0') {
216                                 warnx("Invalid capacity: %s", optarg);
217                                 error = 1;
218                                 goto bailout;
219                         }
220                         break;
221                 case 'e':
222                         elem = strtoul(optarg, &endptr, 0);
223                         if (*endptr != '\0') {
224                                 warnx("Invalid element: %s", optarg);
225                                 error = 1;
226                                 goto bailout;
227                         }
228                         break;
229                 case 'd':
230                         MUST_BE_NONE();
231                         action = DEPOP_REMOVE;
232                         break;
233                 case 'l':
234                         MUST_BE_NONE();
235                         action  = DEPOP_LIST;
236                         break;
237                 case 'r':
238                         MUST_BE_NONE();
239                         action  = DEPOP_RESTORE;
240                         break;
241                 default:
242                         break;
243                 }
244         }
245
246         /*
247          * Compute a sane timeout if none given. 5 seconds for the list command
248          * and whatever the block device characteristics VPD says for other
249          * depop commands. If there's no value in that field, default to 1
250          * day. Experience has shown that these operations take the better part
251          * of a day to complete, so a 1 day timeout default seems appropriate.
252          */
253         if (timeout == 0 && action != DEPOP_NONE) {
254                 if (action == DEPOP_LIST) {
255                         timeout = 5 * 1000;
256                 } else {
257                         struct scsi_vpd_block_device_characteristics *bdc;
258
259                         timeout = 24 * 60 * 60 * 1000;  /* 1 day */
260                         bdc = scsi_wrap_vpd_block_device_characteristics(device);
261                         if (bdc != NULL) {
262                                 timeout = scsi_4btoul(bdc->depopulation_time);
263                         }
264                         free(bdc);
265                 }
266         }
267
268         switch (action) {
269         case DEPOP_NONE:
270                 warnx("Must specify one of -d, -l, or -r");
271                 error = 1;
272                 break;
273         case DEPOP_REMOVE:
274                 if (elem == 0 && capacity == 0) {
275                         warnx("Must specify at least one of -e and/or -c");
276                         error = 1;
277                         break;
278                 }
279                 error = depop_remove(device, task_attr, retry_count, timeout,
280                     verbosemode, elem, capacity);
281                 break;
282         case DEPOP_RESTORE:
283                 error = depop_restore(device, task_attr, retry_count, timeout,
284                     verbosemode);
285                 break;
286         case DEPOP_LIST:
287                 error = depop_list(device, task_attr, retry_count, timeout,
288                     verbosemode);
289                 break;
290         }
291
292 bailout:
293
294         return (error);
295 }