[PATCH] fio: make sure bs is a multiple of 512
[disktools.git] / sgioread.c
1 /*
2  * small utility to read from atapi and scsi devices
3  */
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <getopt.h>
10 #include <sys/ioctl.h>
11 #include <sys/time.h>
12 #include <scsi/sg.h>
13 #include <linux/cdrom.h>
14
15 #define DISK    "/dev/sda"
16
17 #define MAX_XFER        (65535)
18
19 #define ALIGN(buf)      (char *) (((unsigned long) (buf) + 4095) & ~(4095))
20
21 static char device[128];
22 static char output[128];
23 static char rate_output[128];
24
25 static int block_size;
26 static unsigned int blocks;
27 static unsigned long nr_blocks;
28 static int write_output;
29 static unsigned long start_lba;
30 static int force_unaligned;
31 static int write_rate;
32
33 int parse_options(int argc, char *argv[])
34 {
35         int c;
36
37         strcpy(device, DISK);
38
39         for (;;) {
40                 c = getopt(argc, argv, "d:n:o:s:p:ur:");
41                 if (c == -1)
42                         break;
43
44                 switch (c) {
45                         case 'd':
46                                 strcpy(device, optarg);
47                                 break;
48                         case 'n':
49                                 blocks = strtol(optarg, NULL, 10);
50                                 break;
51                         case 'o':
52                                 strcpy(output, optarg);
53                                 write_output = 1;
54                                 break;
55                         case 's':
56                                 nr_blocks = strtol(optarg, NULL, 10);
57                                 break;
58                         case 'p':
59                                 start_lba = strtol(optarg, NULL, 10);
60                                 break;
61                         case 'u':
62                                 force_unaligned = 1;
63                                 break;
64                         case 'r':
65                                 strcpy(rate_output, optarg);
66                                 write_rate = 1;
67                                 break;
68                         default:
69                                 printf("%s: -d device [-s # blocks] [-n # blocks per cmd] [-o output file] [-p start lba] [-u force unaligned buffer]\n", argv[0]);
70                                 return -1;
71                 }
72         }
73
74         return 0;
75 }
76
77 int get_capacity(int fd, int *bs, unsigned long *lba)
78 {
79         struct sg_io_hdr *hdr = malloc(sizeof(*hdr));
80         unsigned char buf[8], cdb[16];
81
82         *bs = 0;
83
84         memset(hdr, 0, sizeof(*hdr));
85         memset(cdb, 0, sizeof(cdb));
86         memset(buf, 0, sizeof(buf));
87
88         hdr->interface_id = 'S';
89         hdr->cmd_len = sizeof(cdb);
90         hdr->dxfer_direction = SG_DXFER_FROM_DEV;
91         hdr->dxferp = buf;
92         hdr->dxfer_len = sizeof(buf);
93
94         hdr->cmdp = cdb;
95         hdr->cmdp[0] = GPCMD_READ_CDVD_CAPACITY;
96
97         if (ioctl(fd, SG_IO, hdr) < 0) {
98                 perror("read capacity\n");
99                 printf("sense key: %x\n", hdr->status);
100                 return -1;
101         }
102
103         *lba = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
104         if (!*lba)
105                 *lba = ~0UL;
106
107         *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
108         return 0;
109 }
110
111 unsigned long time_elapsed(struct timeval *start, struct timeval *end)
112 {
113         int seconds = end->tv_sec - start->tv_sec;
114         int useconds = end->tv_usec - start->tv_usec;
115
116         return (1000 * seconds) + (useconds / 1000);
117 }
118
119 void print_datarate(unsigned long sectors_since_last, int time_spent)
120 {
121         if (!time_spent)
122                 time_spent = 1;
123
124         printf("disk %s: data rate %luKib/sec\r", device, 1000 * (sectors_since_last * block_size / 1024) / time_spent);
125 }
126
127 void write_datarate(int fd, unsigned long lba, unsigned long sectors, int time)
128 {
129         char buffer[32];
130
131         if (fd == -1)
132                 return;
133
134         memset(buffer, 0, sizeof(buffer));
135         snprintf(buffer, sizeof(buffer), "%lu, %lu\n", lba, (sectors * block_size) / time);
136         write(fd, buffer, strlen(buffer));
137 }
138
139 int write_the_crap(int fd_out, char *buffer, int length)
140 {
141         int ret;
142
143         ret = write(fd_out, buffer, length);
144         if (ret != length)
145                 printf("\nwanted to write %u, wrote %d\n", length, ret);
146
147         return ret;
148 }
149
150 int main(int argc, char *argv[])
151 {
152         struct sg_io_hdr *hdr;
153         unsigned long lba, bytes;
154         char *buffer, *ptr;
155         int fd, fd_out, fd_rate;
156         unsigned long time_spent;
157         unsigned long sectors_since_last, end_lba;
158         struct timeval start_time, end_time;
159         unsigned char cdb[16];
160
161         if (parse_options(argc, argv))
162                 return 1;
163
164         fd = open(device, O_RDONLY | O_NONBLOCK);
165         if (fd == -1) {
166                 perror("open input");
167                 return 1;
168         }
169
170         fd_out = -1;
171         if (write_output) {
172                 fd_out = open(output, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
173                 if (fd_out == -1) {
174                         perror("open output");
175                         return 2;
176                 }
177         }
178
179         fd_rate = -1;
180         if (write_rate) {
181                 fd_rate = open(rate_output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
182                 if (fd_rate == -1) {
183                         perror("open rate output");
184                         return 3;
185                 }
186         }
187
188         if (get_capacity(fd, &block_size, &end_lba)) {
189                 printf("%s: can't retrieve capacity\n", device);
190                 return 2;
191         }
192
193         if (!blocks)
194                 blocks = MAX_XFER / block_size;
195
196         hdr = malloc(sizeof(*hdr));
197         memset(hdr, 0, sizeof(*hdr));
198
199         ptr = malloc(blocks * block_size + block_size - 1);
200         if (force_unaligned) {
201                 buffer = ptr;
202                 if (!(((unsigned long) buffer) & (block_size - 1)))
203                         buffer++;
204         } else
205                 buffer = ALIGN(ptr);
206
207         hdr->cmdp = cdb;
208         memset(hdr->cmdp, 0, sizeof(cdb));
209
210         hdr->interface_id = 'S';
211         hdr->cmd_len = sizeof(cdb);
212         hdr->dxfer_direction = SG_DXFER_FROM_DEV;
213         hdr->dxferp = buffer;
214         hdr->cmdp[0] = GPCMD_READ_10;
215
216         lba = start_lba;
217                 nr_blocks = end_lba - lba + 1;
218
219         printf("disk %s: reading LBA %lu -> %lu", device, start_lba, end_lba);
220         printf(" in chunks of %u %ub blocks\n", blocks, block_size);
221
222         bytes = 0;
223         time_spent = sectors_since_last = 0;
224         gettimeofday(&start_time, NULL);
225         do {
226                 if (blocks > nr_blocks)
227                         blocks = nr_blocks;
228
229                 hdr->dxfer_len = blocks * block_size;
230                 hdr->cmdp[2] = (lba >> 24) & 0xff;
231                 hdr->cmdp[3] = (lba >> 16) & 0xff;
232                 hdr->cmdp[4] = (lba >>  8) & 0xff;
233                 hdr->cmdp[5] = lba & 0xff;
234                 hdr->cmdp[7] = (blocks >> 8) & 0xff;
235                 hdr->cmdp[8] = blocks & 0xff;
236
237                 if (ioctl(fd, SG_IO, hdr) < 0) {
238                         int good_blocks = (hdr->dxfer_len - hdr->resid) / block_size;
239                         printf("sense key: %x\n", hdr->status);
240                         if (good_blocks) {
241                                 printf("\n%s: IO error at lba %lu\n", device, lba + good_blocks);
242                                 blocks = good_blocks;
243                         } else {
244                                 printf("\n%s: giving up at lba %lu\n", device, lba);
245                                 break;
246                         }
247                 }
248
249                 if (hdr->resid)
250                         printf("%s: residual %u\n", device, hdr->resid);
251
252                 gettimeofday(&end_time, NULL);
253
254                 nr_blocks -= blocks;
255
256                 sectors_since_last += blocks;
257                 time_spent = time_elapsed(&start_time, &end_time);
258                 if (time_spent >= 500) {
259                         print_datarate(sectors_since_last, time_spent);
260                         fflush(stdout);
261                         if (fd_rate != -1)
262                                 write_datarate(fd_rate, lba, sectors_since_last, time_spent);
263                         sectors_since_last = 0;
264                         gettimeofday(&start_time, NULL);
265                 }
266
267                 lba += blocks;
268                 bytes += blocks * block_size;
269
270                 if (fd_out != -1)
271                         write_the_crap(fd_out, buffer, blocks * block_size);
272
273         } while (lba <= end_lba);
274
275         print_datarate(sectors_since_last, time_spent);
276         printf("\n");
277         free(ptr);
278         close(fd);
279         return 0;
280 }