summaryrefslogtreecommitdiffstats
path: root/sbin/camcontrol/timestamp.c
blob: 771c8f7d0131238e0f228cb716bedb27ae868b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*-
 * Copyright (c) 2016 Spectra Logic Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * Authors: Ken Merry           (Spectra Logic Corporation)
 *          Reid Linnemann      (Spectra Logic Corporation)
 *          Samuel Klopsch      (Spectra Logic Corporation)
 */
/*
 * SCSI tape drive timestamp support
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <time.h>
#include <locale.h>

#include <cam/cam.h>
#include <cam/cam_debug.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <camlib.h>
#include "camcontrol.h"

#define TIMESTAMP_REPORT 0
#define TIMESTAMP_SET    1
#define MIL              "milliseconds"
#define UTC              "utc"

static int set_restore_flags(struct cam_device *device, uint8_t *flags,
			     int set_flag, int task_attr, int retry_count,
			     int timeout);
static int report_timestamp(struct cam_device *device, uint64_t *ts,
			    int task_attr, int retry_count, int timeout);
static int set_timestamp(struct cam_device *device, char *format_string,
			 char *timestamp_string, int task_attr, int retry_count,
			 int timeout);

static int
set_restore_flags(struct cam_device *device, uint8_t *flags, int set_flag,
		  int task_attr, int retry_count, int timeout)
{
	unsigned long blk_desc_length, hdr_and_blk_length;
	int error = 0;
	struct scsi_control_ext_page *control_page = NULL;
	struct scsi_mode_header_10 *mode_hdr = NULL;
	struct scsi_mode_sense_10 *cdb = NULL;
	union ccb *ccb = NULL;
	unsigned long mode_buf_size = sizeof(struct scsi_mode_header_10) +
	    sizeof(struct scsi_mode_blk_desc) +
	    sizeof(struct scsi_control_ext_page);
	uint8_t mode_buf[mode_buf_size];

	ccb = cam_getccb(device);
	if (ccb == NULL) {
		warnx("%s: error allocating CCB", __func__);
		error = 1;
		goto bailout;
	}
	/*
	 * Get the control extension subpage, we'll send it back modified to
	 * enable SCSI control over the tape drive's timestamp
	 */
	scsi_mode_sense_len(&ccb->csio,
	    /*retries*/ retry_count,
	    /*cbfcnp*/ NULL,
	    /*tag_action*/ task_attr,
	    /*dbd*/ 0,
	    /*page_control*/ SMS_PAGE_CTRL_CURRENT,
	    /*page*/ SCEP_PAGE_CODE,
	    /*param_buf*/ &mode_buf[0],
	    /*param_len*/ mode_buf_size,
	    /*minimum_cmd_size*/ 10,
	    /*sense_len*/ SSD_FULL_SIZE,
	    /*timeout*/ timeout ? timeout : 5000);
	/*
	 * scsi_mode_sense_len does not have a subpage argument at the moment,
	 * so we have to manually set the subpage code before calling
	 * cam_send_ccb().
	 */
	cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
	cdb->subpage = SCEP_SUBPAGE_CODE;

	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
	if (retry_count > 0)
		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;

	error = cam_send_ccb(device, ccb);
	if (error != 0) {
		warn("error sending Mode Sense");
		goto bailout;
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_error_print(device, ccb, CAM_ESF_ALL,
				CAM_EPF_ALL, stderr);
		error = 1;
		goto bailout;
	}

	mode_hdr = (struct scsi_mode_header_10 *)&mode_buf[0];
	blk_desc_length = scsi_2btoul(mode_hdr->blk_desc_len);
	hdr_and_blk_length = sizeof(struct scsi_mode_header_10)+blk_desc_length;
	/*
	 * Create the control page at the correct point in the mode_buf, it
	 * starts after the header and the blk description.
	 */
	assert(hdr_and_blk_length <=
	    sizeof(mode_buf) - sizeof(struct scsi_control_ext_page));
	control_page = (struct scsi_control_ext_page *)&mode_buf
	    [hdr_and_blk_length];
	if (set_flag != 0) {
		*flags = control_page->flags;
		/*
		 * Set the SCSIP flag to enable SCSI to change the
		 * tape drive's timestamp.
		 */
		control_page->flags |= SCEP_SCSIP;
	} else {
		control_page->flags = *flags;
	}

	scsi_mode_select_len(&ccb->csio,
	    /*retries*/ retry_count,
	    /*cbfcnp*/ NULL,
	    /*tag_action*/ task_attr,
	    /*scsi_page_fmt*/ 1,
	    /*save_pages*/ 0,
	    /*param_buf*/ &mode_buf[0],
	    /*param_len*/ mode_buf_size,
	    /*minimum_cmd_size*/ 10,
	    /*sense_len*/ SSD_FULL_SIZE,
	    /*timeout*/ timeout ? timeout : 5000);

	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
	if (retry_count > 0)
		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;

	error = cam_send_ccb(device, ccb);
	if (error != 0) {
		warn("error sending Mode Select");
		goto bailout;
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_error_print(device, ccb, CAM_ESF_ALL,
				CAM_EPF_ALL, stderr);
		error = 1;
		goto bailout;
	}

bailout:
	if (ccb != NULL)
		cam_freeccb(ccb);

	return error;
}

static int
report_timestamp(struct cam_device *device, uint64_t *ts, int task_attr,
		 int retry_count, int timeout)
{
	int error = 0;
	struct scsi_report_timestamp_data *report_buf = malloc(
		sizeof(struct scsi_report_timestamp_data));
	uint8_t temp_timestamp[8];
	uint32_t report_buf_size = sizeof(
	    struct scsi_report_timestamp_data);
	union ccb *ccb = NULL;

	ccb = cam_getccb(device);
	if (ccb == NULL) {
		warnx("%s: error allocating CCB", __func__);
		error = 1;
		goto bailout;
	}

	scsi_report_timestamp(&ccb->csio,
	    /*retries*/ retry_count,
	    /*cbfcnp*/ NULL,
	    /*tag_action*/ task_attr,
	    /*pdf*/ 0,
	    /*buf*/ report_buf,
	    /*buf_len*/ report_buf_size,
	    /*sense_len*/ SSD_FULL_SIZE,
	    /*timeout*/ timeout ? timeout : 5000);

	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
	if (retry_count > 0)
		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;

	error = cam_send_ccb(device, ccb);
	if (error != 0) {
		warn("error sending Report Timestamp");
		goto bailout;
	}
	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_error_print(device, ccb, CAM_ESF_ALL,
				CAM_EPF_ALL, stderr);
		error = 1;
		goto bailout;
	}

	bzero(temp_timestamp, sizeof(temp_timestamp));
	memcpy(&temp_timestamp[2], &report_buf->timestamp, 6);

	*ts = scsi_8btou64(temp_timestamp);

bailout:
	if (ccb != NULL)
		cam_freeccb(ccb);
	free(report_buf);

	return error;
}

static int
set_timestamp(struct cam_device *device, char *format_string,
	      char *timestamp_string, int task_attr, int retry_count,
	      int timeout)
{
	struct scsi_set_timestamp_parameters ts_p;
	time_t time_value;
	struct tm time_struct;
	uint8_t flags = 0;
	int error = 0;
	uint64_t ts = 0;
	union ccb *ccb = NULL;
	int do_restore_flags = 0;

	error = set_restore_flags(device, &flags, /*set_flag*/ 1, task_attr,
				  retry_count, timeout);
	if (error != 0)
		goto bailout;

	do_restore_flags = 1;

	ccb = cam_getccb(device);
	if (ccb == NULL) {
		warnx("%s: error allocating CCB", __func__);
		error = 1;
		goto bailout;
	}

	if (strcmp(format_string, UTC) == 0) {
		time(&time_value);
		ts = (uint64_t) time_value;
	} else {
		bzero(&time_struct, sizeof(struct tm));
		strptime(timestamp_string, format_string, &time_struct);
		time_value = mktime(&time_struct);
		ts = (uint64_t) time_value;
	}
	/* Convert time from seconds to milliseconds */
	ts *= 1000;
	scsi_create_timestamp(ts_p.timestamp, ts);

	scsi_set_timestamp(&ccb->csio,
	    /*retries*/ retry_count,
	    /*cbfcnp*/ NULL,
	    /*tag_action*/ task_attr,
	    /*buf*/ &ts_p,
	    /*buf_len*/ sizeof(ts_p),
	    /*sense_len*/ SSD_FULL_SIZE,
	    /*timeout*/ timeout ? timeout : 5000);

	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
	if (retry_count > 0)
		ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;

	error = cam_send_ccb(device, ccb);
	if (error != 0) {
		warn("error sending Set Timestamp");
		goto bailout;
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_error_print(device, ccb, CAM_ESF_ALL,
				CAM_EPF_ALL, stderr);
		error = 1;
		goto bailout;
	}

	printf("Timestamp set to %ju\n", (uintmax_t)ts);

bailout:
	if (do_restore_flags != 0)
		error = set_restore_flags(device, &flags, /*set_flag*/ 0,
					  task_attr, retry_count, timeout);
	if (ccb != NULL)
		cam_freeccb(ccb);

	return error;
}

int
timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
	  int task_attr, int retry_count, int timeout, int verbosemode __unused)
{
	int c;
	uint64_t ts = 0;
	char *format_string = NULL;
	char *timestamp_string = NULL;
	int action = -1;
	int error = 0;
	int single_arg = 0;
	int do_utc = 0;

	while ((c = getopt(argc, argv, combinedopt)) != -1) {
		switch (c) {
		case 'r': {
			if (action != -1) {
				warnx("Use only one -r or only one -s");
				error =1;
				goto bailout;
			}
			action = TIMESTAMP_REPORT;
			break;
		}
		case 's': {
			if (action != -1) {
				warnx("Use only one -r or only one -s");
				error = 1;
				goto bailout;
			}
			action = TIMESTAMP_SET;
			break;
		}
		case 'f': {
			single_arg++;
			free(format_string);
			format_string = strdup(optarg);
			if (format_string == NULL) {
				warn("Error allocating memory for format "
				   "argument");
				error = 1;
				goto bailout;
			}
			break;
		}
		case 'm': {
			single_arg++;
			free(format_string);
			format_string = strdup(MIL);
			if (format_string == NULL) {
				warn("Error allocating memory");
				error = 1;
				goto bailout;
			}
			break;
		}
		case 'U': {
			do_utc = 1;
			break;
		}
		case 'T':
			free(timestamp_string);
			timestamp_string = strdup(optarg);
			if (timestamp_string == NULL) {
				warn("Error allocating memory for format "
				   "argument");
				error = 1;
				goto bailout;
			}
			break;
		default:
			break;
		}
	}

	if (action == -1) {
		warnx("Must specify an action, either -r or -s");
		error = 1;
		goto bailout;
	}

	if (single_arg > 1) {
		warnx("Select only one: %s",
		    (action == TIMESTAMP_REPORT) ?
		    "-f format or -m for the -r flag" : 
		    "-f format -T time or -U for the -s flag");
		error = 1;
		goto bailout;
	}

	if (action == TIMESTAMP_SET) {
		if ((format_string == NULL)
		 && (do_utc == 0)) {
			warnx("Must specify either -f format or -U for "
			    "setting the timestamp");
			error = 1;
		} else if ((format_string != NULL)
			&& (do_utc != 0)) {
			warnx("Must specify only one of -f or -U to set "
			    "the timestamp");
			error = 1;
		} else if ((format_string != NULL)
			&& (strcmp(format_string, MIL) == 0)) {
			warnx("-m is not allowed for setting the "
			    "timestamp");
			error = 1;
		} else if ((do_utc == 0)
			&& (timestamp_string == NULL)) {
			warnx("Must specify the time (-T) to set as the "
			    "timestamp");
			error = 1;
		}
		if (error != 0)
			goto bailout;
	} else if (action == TIMESTAMP_REPORT) {
		if (format_string == NULL) {
			format_string = strdup("%c %Z");
			if (format_string == NULL) {
				warn("Error allocating memory for format "
				    "string");
				error = 1;
				goto bailout;
			}
		}
	}

	if (action == TIMESTAMP_REPORT) {
		error = report_timestamp(device, &ts, task_attr, retry_count,
		    timeout);
		if (error != 0) {
			goto bailout;
		} else if (strcmp(format_string, MIL) == 0) {
			printf("Timestamp in milliseconds: %ju\n",
			    (uintmax_t)ts);
		} else {
			char temp_timestamp_string[100];
			time_t time_var = ts / 1000;
			const struct tm *restrict cur_time;

			setlocale(LC_TIME, "");
			if (do_utc != 0)
				cur_time = gmtime(&time_var);
			else
				cur_time = localtime(&time_var);

			strftime(temp_timestamp_string,
			    sizeof(temp_timestamp_string), format_string,
			    cur_time);
			printf("Formatted timestamp: %s\n",
			    temp_timestamp_string);
		}
	} else if (action == TIMESTAMP_SET) {
		if (do_utc != 0) {
			format_string = strdup(UTC);
			if (format_string == NULL) {
				warn("Error allocating memory for format "
				    "string");
				error = 1;
				goto bailout;
			}
		}

		error = set_timestamp(device, format_string, timestamp_string,
		    task_attr, retry_count, timeout);
	}

bailout:
	free(format_string);
	free(timestamp_string);

	return (error);
}
OpenPOWER on IntegriCloud