1 /*
2  * Copyright (c) 2004-2015 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 
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <limits.h>
14 #include <getopt.h>
15 
16 #include "sg_lib.h"
17 #include "sg_cmds_basic.h"
18 #include "sg_pt.h"
19 #include "sg_unaligned.h"
20 #include "sg_pr2serr.h"
21 
22 /* A utility program for the Linux OS SCSI subsystem.
23  *
24  *
25  * This program issues the SCSI command SYNCHRONIZE CACHE(10 or 16) to the
26  * given device. This command is defined for SCSI "direct access" devices
27  * (e.g. disks).
28  */
29 
30 static const char * version_str = "1.14 20151219";
31 
32 #define SYNCHRONIZE_CACHE16_CMD     0x91
33 #define SYNCHRONIZE_CACHE16_CMDLEN  16
34 #define SENSE_BUFF_LEN  64
35 #define DEF_PT_TIMEOUT  60       /* 60 seconds */
36 
37 
38 static struct option long_options[] = {
39         {"16", no_argument, 0, 'S'},
40         {"count", required_argument, 0, 'c'},
41         {"group", required_argument, 0, 'g'},
42         {"help", no_argument, 0, 'h'},
43         {"immed", no_argument, 0, 'i'},
44         {"lba", required_argument, 0, 'l'},
45         {"sync-nv", no_argument, 0, 's'},
46         {"timeout", required_argument, 0, 't'},
47         {"verbose", no_argument, 0, 'v'},
48         {"version", no_argument, 0, 'V'},
49         {0, 0, 0, 0},
50 };
51 
usage()52 static void usage()
53 {
54     pr2serr("Usage: sg_sync    [--16] [--count=COUNT] [--group=GN] [--help] "
55             "[--immed]\n"
56             "                  [--lba=LBA] [--sync-nv] [--timeout=SECS] "
57             "[--verbose]\n"
58             "                  [--version] DEVICE\n"
59             "  where:\n"
60             "    --16|-S             calls SYNCHRONIZE CACHE(16) (def: is "
61             "10 byte\n"
62             "                        variant)\n"
63             "    --count=COUNT|-c COUNT    number of blocks to sync (def: 0 "
64             "which\n"
65             "                              implies rest of device)\n"
66             "    --group=GN|-g GN    set group number field to GN (def: 0)\n"
67             "    --help|-h           print out usage message\n"
68             "    --immed|-i          command returns immediately when set "
69             "else wait\n"
70             "                        for 'sync' to complete\n"
71             "    --lba=LBA|-l LBA    logical block address to start sync "
72             "operation\n"
73             "                        from (def: 0)\n"
74             "    --sync-nv|-s        synchronize to non-volatile storage "
75             "(if distinct\n"
76             "                        from medium). Obsolete in sbc3r35d.\n"
77             "    --timeout=SECS|-t SECS    command timeout in seconds, only "
78             "active\n"
79             "                              if '--16' given (def: 60 seconds)\n"
80             "    --verbose|-v        increase verbosity\n"
81             "    --version|-V        print version string and exit\n\n"
82             "Performs a SCSI SYNCHRONIZE CACHE(10 or 16) command\n");
83 }
84 
85 static int
ll_sync_cache_16(int sg_fd,int sync_nv,int immed,int group,uint64_t lba,unsigned int num_lb,int to_secs,int noisy,int verbose)86 ll_sync_cache_16(int sg_fd, int sync_nv, int immed, int group,
87                  uint64_t lba, unsigned int num_lb, int to_secs,
88                  int noisy, int verbose)
89 {
90     int res, ret, k, sense_cat;
91     unsigned char scCmdBlk[SYNCHRONIZE_CACHE16_CMDLEN] =
92                 {SYNCHRONIZE_CACHE16_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93                  0, 0, 0, 0, 0, 0};
94     unsigned char sense_b[SENSE_BUFF_LEN];
95     struct sg_pt_base * ptvp;
96 
97     if (sync_nv)
98         scCmdBlk[1] |= 4;       /* obsolete in sbc3r35d */
99     if (immed)
100         scCmdBlk[1] |= 2;
101     sg_put_unaligned_be64(lba, scCmdBlk + 2);
102     scCmdBlk[14] = group & 0x1f;
103     sg_put_unaligned_be32((uint32_t)num_lb, scCmdBlk + 10);
104 
105     if (verbose) {
106         pr2serr("    synchronize cache(16) cdb: ");
107         for (k = 0; k < SYNCHRONIZE_CACHE16_CMDLEN; ++k)
108             pr2serr("%02x ", scCmdBlk[k]);
109         pr2serr("\n");
110     }
111     ptvp = construct_scsi_pt_obj();
112     if (NULL == ptvp) {
113         pr2serr("synchronize cache(16): out of memory\n");
114         return -1;
115     }
116     set_scsi_pt_cdb(ptvp, scCmdBlk, sizeof(scCmdBlk));
117     set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
118     res = do_scsi_pt(ptvp, sg_fd, to_secs, verbose);
119     ret = sg_cmds_process_resp(ptvp, "synchronize cache(16)", res, 0,
120                                sense_b, noisy, verbose, &sense_cat);
121     if (-1 == ret)
122         ;
123     else if (-2 == ret) {
124         switch (sense_cat) {
125         case SG_LIB_CAT_RECOVERED:
126         case SG_LIB_CAT_NO_SENSE:
127             ret = 0;
128             break;
129         default:
130             ret = sense_cat;
131             break;
132         }
133     } else
134         ret = 0;
135 
136     destruct_scsi_pt_obj(ptvp);
137     return ret;
138 }
139 
140 
main(int argc,char * argv[])141 int main(int argc, char * argv[])
142 {
143     int sg_fd, res, c;
144     int64_t count = 0;
145     unsigned int num_lb = 0;
146     int do_16 = 0;
147     int group = 0;
148     int64_t lba = 0;
149     int immed = 0;
150     int sync_nv = 0;
151     int to_secs = DEF_PT_TIMEOUT;
152     int verbose = 0;
153     const char * device_name = NULL;
154     int ret = 0;
155 
156     while (1) {
157         int option_index = 0;
158 
159         c = getopt_long(argc, argv, "c:g:hil:sSt:vV", long_options,
160                         &option_index);
161         if (c == -1)
162             break;
163 
164         switch (c) {
165         case 'c':
166             count = sg_get_llnum(optarg);
167             if ((count < 0) || (count > UINT_MAX)) {
168                 pr2serr("bad argument to '--count'\n");
169                 return SG_LIB_SYNTAX_ERROR;
170             }
171             num_lb = (unsigned int)count;
172             break;
173         case 'g':
174             group = sg_get_num(optarg);
175             if ((group < 0) || (group > 31)) {
176                 pr2serr("bad argument to '--group'\n");
177                 return SG_LIB_SYNTAX_ERROR;
178             }
179             break;
180         case 'h':
181         case '?':
182             usage();
183             return 0;
184         case 'i':
185             immed = 1;
186             break;
187         case 'l':
188             lba = sg_get_llnum(optarg);
189             if (lba < 0) {
190                 pr2serr("bad argument to '--lba'\n");
191                 return SG_LIB_SYNTAX_ERROR;
192             }
193             break;
194         case 's':
195             sync_nv = 1;
196             break;
197         case 'S':
198             do_16 = 1;
199             break;
200         case 't':
201             to_secs = sg_get_num(optarg);
202             if (to_secs < 0) {
203                 pr2serr("bad argument to '--timeout'\n");
204                 return SG_LIB_SYNTAX_ERROR;
205             }
206             break;
207         case 'v':
208             ++verbose;
209             break;
210         case 'V':
211             pr2serr("version: %s\n", version_str);
212             return 0;
213         default:
214             pr2serr("unrecognised option code 0x%x ??\n", c);
215             usage();
216             return SG_LIB_SYNTAX_ERROR;
217         }
218     }
219     if (optind < argc) {
220         if (NULL == device_name) {
221             device_name = argv[optind];
222             ++optind;
223         }
224         if (optind < argc) {
225             for (; optind < argc; ++optind)
226                 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
227             usage();
228             return SG_LIB_SYNTAX_ERROR;
229         }
230     }
231 
232     if (NULL == device_name) {
233         pr2serr("missing device name!\n");
234         usage();
235         return SG_LIB_SYNTAX_ERROR;
236     }
237     sg_fd = sg_cmds_open_device(device_name, 0 /* rw */, verbose);
238     if (sg_fd < 0) {
239         pr2serr("open error: %s: %s\n", device_name,
240                 safe_strerror(-sg_fd));
241         return SG_LIB_FILE_ERROR;
242     }
243 
244     if (do_16)
245         res = ll_sync_cache_16(sg_fd, sync_nv, immed, group, lba, num_lb,
246                                to_secs, 1, verbose);
247     else
248         res = sg_ll_sync_cache_10(sg_fd, sync_nv, immed, group,
249                                   (unsigned int)lba, num_lb, 1, verbose);
250     ret = res;
251     if (res) {
252         char b[80];
253 
254         sg_get_category_sense_str(res, sizeof(b), b, verbose);
255         pr2serr("Synchronize cache failed: %s\n", b);
256     }
257 
258     res = sg_cmds_close_device(sg_fd);
259     if (res < 0) {
260         pr2serr("close error: %s\n", safe_strerror(-res));
261         if (0 == ret)
262             return SG_LIB_FILE_ERROR;
263     }
264     return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
265 }
266