1 /*
2 * Copyright (c) 2004-2021 Douglas Gilbert.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the BSD_LICENSE file.
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <limits.h>
18 #include <getopt.h>
19 #define __STDC_FORMAT_MACROS 1
20 #include <inttypes.h>
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "sg_lib.h"
27 #include "sg_cmds_basic.h"
28 #include "sg_pt.h"
29 #include "sg_unaligned.h"
30 #include "sg_pr2serr.h"
31
32 /* A utility program for the Linux OS SCSI subsystem.
33 *
34 *
35 * This program issues the SCSI command SYNCHRONIZE CACHE(10 or 16) to the
36 * given device. This command is defined for SCSI "direct access" devices
37 * (e.g. disks).
38 */
39
40 static const char * version_str = "1.27 20211114";
41
42 #define SYNCHRONIZE_CACHE16_CMD 0x91
43 #define SYNCHRONIZE_CACHE16_CMDLEN 16
44 #define SENSE_BUFF_LEN 64
45 #define DEF_PT_TIMEOUT 60 /* 60 seconds */
46
47
48 static struct option long_options[] = {
49 {"16", no_argument, 0, 'S'},
50 {"count", required_argument, 0, 'c'},
51 {"group", required_argument, 0, 'g'},
52 {"help", no_argument, 0, 'h'},
53 {"immed", no_argument, 0, 'i'},
54 {"lba", required_argument, 0, 'l'},
55 {"sync-nv", no_argument, 0, 's'},
56 {"timeout", required_argument, 0, 't'},
57 {"verbose", no_argument, 0, 'v'},
58 {"version", no_argument, 0, 'V'},
59 {0, 0, 0, 0},
60 };
61
usage()62 static void usage()
63 {
64 pr2serr("Usage: sg_sync [--16] [--count=COUNT] [--group=GN] [--help] "
65 "[--immed]\n"
66 " [--lba=LBA] [--sync-nv] [--timeout=SECS] "
67 "[--verbose]\n"
68 " [--version] DEVICE\n"
69 " where:\n"
70 " --16|-S calls SYNCHRONIZE CACHE(16) (def: is "
71 "10 byte\n"
72 " variant)\n"
73 " --count=COUNT|-c COUNT number of blocks to sync (def: 0 "
74 "which\n"
75 " implies rest of device)\n"
76 " --group=GN|-g GN set group number field to GN (def: 0)\n"
77 " --help|-h print out usage message\n"
78 " --immed|-i command returns immediately when set "
79 "else wait\n"
80 " for 'sync' to complete\n"
81 " --lba=LBA|-l LBA logical block address to start sync "
82 "operation\n"
83 " from (def: 0)\n"
84 " --sync-nv|-s synchronize to non-volatile storage "
85 "(if distinct\n"
86 " from medium). Obsolete in sbc3r35d.\n"
87 " --timeout=SECS|-t SECS command timeout in seconds, only "
88 "active\n"
89 " if '--16' given (def: 60 seconds)\n"
90 " --verbose|-v increase verbosity\n"
91 " --version|-V print version string and exit\n\n"
92 "Performs a SCSI SYNCHRONIZE CACHE(10 or 16) command\n");
93 }
94
95 static int
sg_ll_sync_cache_16(int sg_fd,bool sync_nv,bool immed,int group,uint64_t lba,unsigned int num_lb,int to_secs,bool noisy,int verbose)96 sg_ll_sync_cache_16(int sg_fd, bool sync_nv, bool immed, int group,
97 uint64_t lba, unsigned int num_lb, int to_secs,
98 bool noisy, int verbose)
99 {
100 int res, ret, sense_cat;
101 uint8_t sc_cdb[SYNCHRONIZE_CACHE16_CMDLEN] =
102 {SYNCHRONIZE_CACHE16_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 0, 0, 0, 0, 0, 0};
104 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
105 struct sg_pt_base * ptvp;
106
107 if (sync_nv)
108 sc_cdb[1] |= 4; /* obsolete in sbc3r35d */
109 if (immed)
110 sc_cdb[1] |= 2;
111 sg_put_unaligned_be64(lba, sc_cdb + 2);
112 sc_cdb[14] = group & GRPNUM_MASK;
113 sg_put_unaligned_be32((uint32_t)num_lb, sc_cdb + 10);
114
115 if (verbose) {
116 char b[128];
117
118 pr2serr(" Synchronize cache(16) cdb: %s\n",
119 sg_get_command_str(sc_cdb, SYNCHRONIZE_CACHE16_CMDLEN, false,
120 sizeof(b), b));
121 }
122 ptvp = construct_scsi_pt_obj();
123 if (NULL == ptvp) {
124 pr2serr("synchronize cache(16): out of memory\n");
125 return -1;
126 }
127 set_scsi_pt_cdb(ptvp, sc_cdb, sizeof(sc_cdb));
128 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
129 res = do_scsi_pt(ptvp, sg_fd, to_secs, verbose);
130 ret = sg_cmds_process_resp(ptvp, "synchronize cache(16)", res,
131 noisy, verbose, &sense_cat);
132 if (-1 == ret) {
133 if (get_scsi_pt_transport_err(ptvp))
134 ret = SG_LIB_TRANSPORT_ERROR;
135 else
136 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
137 } else if (-2 == ret) {
138 switch (sense_cat) {
139 case SG_LIB_CAT_RECOVERED:
140 case SG_LIB_CAT_NO_SENSE:
141 ret = 0;
142 break;
143 default:
144 ret = sense_cat;
145 break;
146 }
147 } else
148 ret = 0;
149
150 destruct_scsi_pt_obj(ptvp);
151 return ret;
152 }
153
154
main(int argc,char * argv[])155 int main(int argc, char * argv[])
156 {
157 bool do_16 = false;
158 bool immed = false;
159 bool sync_nv = false;
160 bool verbose_given = false;
161 bool version_given = false;
162 int res, c;
163 int sg_fd = -1;
164 int group = 0;
165 int ret = 0;
166 int to_secs = DEF_PT_TIMEOUT;
167 int verbose = 0;
168 unsigned int num_lb = 0;
169 int64_t count = 0;
170 int64_t lba = 0;
171 const char * device_name = NULL;
172
173 while (1) {
174 int option_index = 0;
175
176 c = getopt_long(argc, argv, "c:g:hil:sSt:vV", long_options,
177 &option_index);
178 if (c == -1)
179 break;
180
181 switch (c) {
182 case 'c':
183 count = sg_get_llnum(optarg);
184 if ((count < 0) || (count > UINT_MAX)) {
185 pr2serr("bad argument to '--count'\n");
186 return SG_LIB_SYNTAX_ERROR;
187 }
188 num_lb = (unsigned int)count;
189 break;
190 case 'g':
191 group = sg_get_num(optarg);
192 if ((group < 0) || (group > 63)) {
193 pr2serr("bad argument to '--group'\n");
194 return SG_LIB_SYNTAX_ERROR;
195 }
196 break;
197 case 'h':
198 case '?':
199 usage();
200 return 0;
201 case 'i':
202 immed = true;
203 break;
204 case 'l':
205 lba = sg_get_llnum(optarg);
206 if (lba < 0) {
207 pr2serr("bad argument to '--lba'\n");
208 return SG_LIB_SYNTAX_ERROR;
209 }
210 break;
211 case 's':
212 sync_nv = true;
213 break;
214 case 'S':
215 do_16 = true;
216 break;
217 case 't':
218 to_secs = sg_get_num(optarg);
219 if (to_secs < 0) {
220 pr2serr("bad argument to '--timeout'\n");
221 return SG_LIB_SYNTAX_ERROR;
222 }
223 break;
224 case 'v':
225 verbose_given = true;
226 ++verbose;
227 break;
228 case 'V':
229 version_given = true;
230 break;
231 default:
232 pr2serr("unrecognised option code 0x%x ??\n", c);
233 usage();
234 return SG_LIB_SYNTAX_ERROR;
235 }
236 }
237 if (optind < argc) {
238 if (NULL == device_name) {
239 device_name = argv[optind];
240 ++optind;
241 }
242 if (optind < argc) {
243 for (; optind < argc; ++optind)
244 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
245 usage();
246 return SG_LIB_SYNTAX_ERROR;
247 }
248 }
249
250 #ifdef DEBUG
251 pr2serr("In DEBUG mode, ");
252 if (verbose_given && version_given) {
253 pr2serr("but override: '-vV' given, zero verbose and continue\n");
254 verbose_given = false;
255 version_given = false;
256 verbose = 0;
257 } else if (! verbose_given) {
258 pr2serr("set '-vv'\n");
259 verbose = 2;
260 } else
261 pr2serr("keep verbose=%d\n", verbose);
262 #else
263 if (verbose_given && version_given)
264 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
265 #endif
266 if (version_given) {
267 pr2serr("version: %s\n", version_str);
268 return 0;
269 }
270
271 if (NULL == device_name) {
272 pr2serr("Missing device name!\n\n");
273 usage();
274 return SG_LIB_SYNTAX_ERROR;
275 }
276 sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose);
277 if (sg_fd < 0) {
278 if (verbose)
279 pr2serr("open error: %s: %s\n", device_name,
280 safe_strerror(-sg_fd));
281 ret = sg_convert_errno(-sg_fd);
282 goto fini;
283 }
284
285 if (do_16)
286 res = sg_ll_sync_cache_16(sg_fd, sync_nv, immed, group, lba, num_lb,
287 to_secs, true, verbose);
288 else
289 res = sg_ll_sync_cache_10(sg_fd, sync_nv, immed, group,
290 (unsigned int)lba, num_lb, true, verbose);
291 ret = res;
292 if (res) {
293 char b[80];
294
295 sg_get_category_sense_str(res, sizeof(b), b, verbose);
296 pr2serr("Synchronize cache failed: %s\n", b);
297 }
298
299 fini:
300 if (sg_fd >= 0) {
301 res = sg_cmds_close_device(sg_fd);
302 if (res < 0) {
303 pr2serr("close error: %s\n", safe_strerror(-res));
304 if (0 == ret)
305 ret = sg_convert_errno(-res);
306 }
307 }
308 if (0 == verbose) {
309 if (! sg_if_can2stderr("sg_sync failed: ", ret))
310 pr2serr("Some error occurred, try again with '-v' "
311 "or '-vv' for more information\n");
312 }
313 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
314 }
315