xref: /aosp_15_r20/external/sg3_utils/testing/sg_take_snap.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /* A utility program originally written for the Linux OS SCSI subsystem.
2  *  Copyright (C) 2021 D. Gilbert
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2, or (at your option)
6  *  any later version.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  *
10  * This program is experimental. It allows the SG_CTL_FLAGM_SNAP_DEV
11  * variant of ioctl(SG_SET_GET_EXTENDED) to be called. This assumes
12  * a Linux sg driver whose version number > 4.00.30 .
13  */
14 
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <getopt.h>
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #ifndef HAVE_LINUX_SG_V4_HDR
33 /* Kernel uapi header contain __user decorations on user space pointers
34  * to indicate they are unsafe in the kernel space. However glibc takes
35  * all those __user decorations out from headers in /usr/include/linux .
36  * So to stop compile errors when directly importing include/uapi/scsi/sg.h
37  * undef __user before doing that include. */
38 #define __user
39 
40 /* Want to block the original sg.h header from also being included. That
41  * causes lots of multiple definition errors. This will only work if this
42  * header is included _before_ the original sg.h header.  */
43 #define _SCSI_GENERIC_H         /* original kernel header guard */
44 #define _SCSI_SG_H              /* glibc header guard */
45 
46 #include "uapi_sg.h"    /* local copy of include/uapi/scsi/sg.h */
47 
48 #else
49 #define __user
50 #endif  /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
51 
52 #include "sg_lib.h"
53 #include "sg_pr2serr.h"
54 
55 
56 #define ME "sg_take_snap: "
57 
58 static const char * version_str = "1.01 20210403";
59 
60 #define SG_TAKE_MAX_DEVS 16
61 
62 static const char *dev_arr[SG_TAKE_MAX_DEVS];
63 static int next_vacant_dev_idx = 0;
64 
65 static struct option long_options[] = {
66         {"clear", no_argument, 0, 'c'},
67         {"help", no_argument, 0, 'h'},
68         {"verbose", no_argument, 0, 'v'},
69         {"version", no_argument, 0, 'V'},
70         {0, 0, 0, 0},
71 };
72 
73 static void
usage(void)74 usage(void)
75 {
76     pr2serr("Usage: sg_take_snap [--clear] [--help] [--verbose] [--version] "
77             "DEVICE*\n"
78             "  where:\n"
79             "    --clear|-c      set 'clear_first' flag; otherwise appends\n"
80             "    --help|-h       print usage information then exit\n"
81             "    --verbose|-v    increase the level of verbosity\n"
82             "    --version|-V    print version number then exit\n\n"
83             "Use ioctl(SG_SET_GET_EXTENDED(SG_CTL_FLAGM_SNAP_DEV)) to take "
84             "snap .\nThe output is placed in /sys/kernel/debug/scsi_generic/"
85             "snapped and needs\nroot permissions to read. Requires a Linux "
86             "sg driver version > 4.00.30 .\nOne or more DEVICEs can be "
87             "given. Note: sending the ioctl to do this\ncreates some "
88             "'noise' in the output\n"
89            );
90 }
91 
92 
main(int argc,char * argv[])93 int main(int argc, char * argv[])
94 {
95     bool clear_first = false;
96     int c, k, sg_fd, res;
97     int ret = 0;
98     int verbose = 0;
99     const char * device_name = NULL;
100     struct sg_extended_info sei;
101     struct sg_extended_info * seip;
102 
103     while (1) {
104         int option_index = 0;
105 
106         c = getopt_long(argc, argv, "chvV", long_options, &option_index);
107         if (c == -1)
108             break;
109 
110         switch (c) {
111         case 'c':
112             clear_first = true;
113             break;
114         case 'h':
115             usage();
116             return 0;
117         case 'v':
118             ++verbose;
119             break;
120         case 'V':
121             pr2serr(ME "version: %s\n", version_str);
122             return 0;
123         default:
124             pr2serr("unrecognised option code 0x%x ??\n", c);
125             usage();
126             return SG_LIB_SYNTAX_ERROR;
127         }
128     }
129 
130     if (optind < argc) {
131         for (; optind < argc; ++optind) {
132             if (next_vacant_dev_idx < SG_TAKE_MAX_DEVS) {
133                 dev_arr[next_vacant_dev_idx] = argv[optind];
134                 ++next_vacant_dev_idx;
135             } else if (next_vacant_dev_idx == SG_TAKE_MAX_DEVS) {
136                 pr2serr("Maximum of %d DEVICEs on command line\n",
137                         next_vacant_dev_idx);
138                 usage();
139                 return SG_LIB_SYNTAX_ERROR;
140             } else {
141                 pr2serr("something is wrong ...\n");
142                 return SG_LIB_SYNTAX_ERROR;
143             }
144         }
145     }
146     if (NULL == dev_arr[0]) {
147         pr2serr("Need at least one DEVICE name. Use '--help' to see "
148                 "usage.\n");
149         return SG_LIB_SYNTAX_ERROR;
150     }
151 
152     for (k = 0; k < next_vacant_dev_idx; ++k) {
153         device_name = dev_arr[k];
154         sg_fd = open(device_name, O_RDWR | O_NONBLOCK);
155         if (sg_fd < 0) {
156             int err = errno;
157 
158             ret = sg_convert_errno(err);
159             pr2serr(ME "open error: %s: ", device_name);
160             perror("");
161             sg_fd = -1;
162             goto fini;
163         }
164         if (0 == k) {
165             int t;
166 
167             res = ioctl(sg_fd, SG_GET_VERSION_NUM, &t);
168             if ((res < 0) || (t < 30000)) {
169                 pr2serr("sg driver prior to 3.0.00\n");
170                 ret = SG_LIB_FILE_ERROR;
171                 goto fini;
172             }
173             if (verbose) {
174                 pr2serr("sg driver version: %d.%02d.%02d\n",
175                         t / 10000, (t % 10000) / 100, t % 100);
176             }
177             if (t < 40000) {
178                 pr2serr("Warning: sg driver prior to 4.0.00\n");
179                 ret = SG_LIB_FILE_ERROR;
180                 goto fini;
181             } else if (t < 40045) {
182                 pr2serr("Warning: sg driver prior to 4.0.45\n");
183                 ret = SG_LIB_FILE_ERROR;
184                 goto fini;
185             }
186         }
187 
188         seip = &sei;
189         memset(seip, 0, sizeof(*seip));
190         seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
191         seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS;
192         seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_SNAP_DEV;
193         if (clear_first)    /* ... else 0 (due to memset) --> append */
194             seip->ctl_flags |= SG_CTL_FLAGM_SNAP_DEV;
195         if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
196             pr2serr("ioctl(SG_SET_GET_EXTENDED(SG_CTL_FLAGM_SNAP_DEV)), %s "
197                     "failed errno=%d %s\n", device_name, errno,
198                     strerror(errno));
199             ret = SG_LIB_FILE_ERROR;
200             goto fini;
201         }
202         if (verbose)
203             pr2serr("ioctl(%s, SG_SET_GET_EXTENDED(SG_CTL_FLAGM_SNAP_DEV)) "
204                     "ok\n", device_name);
205         res = close(sg_fd);
206         sg_fd = -1;
207         if (res < 0) {
208             pr2serr("close errno=%d on %s\n", errno, device_name);
209             ret = res;
210             goto fini;
211         }
212     }
213 
214 fini:
215     if (sg_fd >= 0) {
216         res = close(sg_fd);
217         if (res < 0) {
218             res = sg_convert_errno(errno);
219             perror(ME "close error");
220             if (0 == ret)
221                 ret = res;
222         }
223     }
224     return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
225 }
226