|
|
|
/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
|
|
|
|
|
|
|
|
/* Copyright (c) 2004 - 2006 Derek Foreman, Ben Jansens
|
|
|
|
Copyright (c) 2006 - 2010 Thomas Schmitt <scdbackup@gmx.net>
|
|
|
|
Provided under GPL version 2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* <<< ts A91112 : experiments to get better speed with USB
|
|
|
|
#define Libburn_sgio_as_growisofS 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This is the main operating system dependent SCSI part of libburn. It implements
|
|
|
|
the transport level aspects of SCSI control and command i/o.
|
|
|
|
|
|
|
|
Present implementation: GNU/Linux SCSI Generic (sg)
|
|
|
|
|
|
|
|
|
|
|
|
PORTING:
|
|
|
|
|
|
|
|
Porting libburn typically will consist of adding a new operating system case
|
|
|
|
to the following switcher files:
|
|
|
|
os.h Operating system specific libburn definitions and declarations.
|
|
|
|
sg.c Operating system dependent transport level modules.
|
|
|
|
and of deriving the following system specific files from existing examples:
|
|
|
|
os-*.h Included by os.h. You will need some general system knowledge
|
|
|
|
about signals and knowledge about the storage object needs of your
|
|
|
|
transport level module sg-*.c.
|
|
|
|
|
|
|
|
sg-*.c This source module. You will need special system knowledge about
|
|
|
|
how to detect all potentially available drives, how to open them,
|
|
|
|
eventually how to exclusively reserve them, how to perform
|
|
|
|
SCSI transactions, how to inquire the (pseudo-)SCSI driver.
|
|
|
|
You will not need to care about CD burning, MMC or other high-level
|
|
|
|
SCSI aspects.
|
|
|
|
|
|
|
|
Said sg-*.c operations are defined by a public function interface, which has
|
|
|
|
to be implemented in a way that provides libburn with the desired services:
|
|
|
|
|
|
|
|
sg_id_string() returns an id string of the SCSI transport adapter.
|
|
|
|
It may be called before initialization but then may
|
|
|
|
return only a preliminary id.
|
|
|
|
|
|
|
|
sg_initialize() performs global initialization of the SCSI transport
|
|
|
|
adapter and eventually needed operating system
|
|
|
|
facilities. Checks for compatibility of supporting
|
|
|
|
software components.
|
|
|
|
|
|
|
|
sg_shutdown() performs global finalizations and releases golbally
|
|
|
|
aquired resources.
|
|
|
|
|
|
|
|
sg_give_next_adr() iterates over the set of potentially useful drive
|
|
|
|
address strings.
|
|
|
|
|
|
|
|
scsi_enumerate_drives() brings all available, not-whitelist-banned, and
|
|
|
|
accessible drives into libburn's list of drives.
|
|
|
|
|
|
|
|
sg_dispose_drive() finalizes adapter specifics of struct burn_drive
|
|
|
|
on destruction. Releases resources which were aquired
|
|
|
|
underneath scsi_enumerate_drives().
|
|
|
|
|
|
|
|
sg_drive_is_open() tells wether libburn has the given drive in use.
|
|
|
|
|
|
|
|
sg_grab() opens the drive for SCSI commands and ensures
|
|
|
|
undisturbed access.
|
|
|
|
|
|
|
|
sg_release() closes a drive opened by sg_grab()
|
|
|
|
|
|
|
|
sg_issue_command() sends a SCSI command to the drive, receives reply,
|
|
|
|
and evaluates wether the command succeeded or shall
|
|
|
|
be retried or finally failed.
|
|
|
|
|
|
|
|
sg_obtain_scsi_adr() tries to obtain SCSI address parameters.
|
|
|
|
|
|
|
|
|
|
|
|
burn_os_is_2k_seekrw() tells whether the given path leads to a file object
|
|
|
|
that can be used in 2 kB granularity by lseek(2) and
|
|
|
|
read(2), and possibly write(2) if not read-only.
|
|
|
|
E.g. a USB stick or a hard disk.
|
|
|
|
|
|
|
|
burn_os_stdio_capacity() estimates the emulated media space of stdio-drives.
|
|
|
|
|
|
|
|
burn_os_open_track_src() opens a disk file in a way that allows best
|
|
|
|
throughput with file reading and/or SCSI write command
|
|
|
|
transmission.
|
|
|
|
|
|
|
|
burn_os_alloc_buffer() allocates a memory area that is suitable for file
|
|
|
|
descriptors issued by burn_os_open_track_src().
|
|
|
|
The buffer size may be rounded up for alignment
|
|
|
|
reasons.
|
|
|
|
|
|
|
|
burn_os_free_buffer() delete a buffer obtained by burn_os_alloc_buffer().
|
|
|
|
|
|
|
|
|
|
|
|
Porting hints are marked by the text "PORTING:".
|
|
|
|
Send feedback to libburn-hackers@pykix.org .
|
|
|
|
|
|
|
|
Hint: You should also look into sg-freebsd-port.c, which is a younger and
|
|
|
|
in some aspects more straightforward implementation of this interface.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/** PORTING : ------- OS dependent headers and definitions ------ */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef Libburn_read_o_direcT
|
|
|
|
# ifndef _GNU_SOURCE
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
# endif
|
|
|
|
#endif /* Libburn_read_o_direcT */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <linux/hdreg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <scsi/scsi.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
|
|
|
|
/* for ioctl(BLKGETSIZE) */
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
/* for mmap() */
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <scsi/sg.h>
|
|
|
|
/* Values within sg_io_hdr_t indicating success after ioctl(SG_IO) : */
|
|
|
|
/* .host_status : from http://tldp.org/HOWTO/SCSI-Generic-HOWTO/x291.html */
|
|
|
|
#define Libburn_sg_host_oK 0
|
|
|
|
/* .driver_status : from http://tldp.org/HOWTO/SCSI-Generic-HOWTO/x322.html */
|
|
|
|
#define Libburn_sg_driver_oK 0
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A61211 : to eventually recognize CD devices on /dev/sr* */
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/cdrom.h>
|
|
|
|
|
|
|
|
|
|
|
|
/** Indication of the Linux kernel this software is running on */
|
|
|
|
/* -1 = not evaluated , 0 = unrecognizable , 1 = 2.4 , 2 = 2.6 */
|
|
|
|
static int sg_kernel_age = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/** PORTING : Device file families for bus scanning and drive access.
|
|
|
|
Both device families must support the following ioctls:
|
|
|
|
SG_IO,
|
|
|
|
SG_GET_SCSI_ID
|
|
|
|
SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
SCSI_IOCTL_GET_IDLUN
|
|
|
|
as well as mutual exclusively locking with open(O_EXCL).
|
|
|
|
If a device family is left empty, then it will not be used.
|
|
|
|
|
|
|
|
To avoid misunderstandings: both families are used via identical
|
|
|
|
transport methods as soon as a device file is accepted as CD drive
|
|
|
|
by the family specific function <family>_enumerate().
|
|
|
|
One difference remains throughout usage: Host,Channel,Id,Lun and Bus
|
|
|
|
address parameters of ATA devices are considered invalid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Set this to 1 in order to get on stderr messages from sg_enumerate() */
|
|
|
|
static int linux_sg_enumerate_debug = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* The device file family to use for (emulated) generic SCSI transport.
|
|
|
|
This must be a printf formatter with one single placeholder for int
|
|
|
|
in the range of 0 to 31 . The resulting addresses must provide SCSI
|
|
|
|
address parameters Host, Channel, Id, Lun and also Bus.
|
|
|
|
E.g.: "/dev/sg%d"
|
|
|
|
sr%d is supposed to map only CD-ROM style devices. Additionally a test
|
|
|
|
with ioctl(CDROM_DRIVE_STATUS) is made to assert that it is such a drive,
|
|
|
|
|
|
|
|
This initial setting may be overridden in sg_select_device_family() by
|
|
|
|
settings made via burn_preset_device_open().
|
|
|
|
*/
|
|
|
|
static char linux_sg_device_family[80] = {"/dev/sg%d"};
|
|
|
|
|
|
|
|
/* Set this to 1 if you want the default linux_sg_device_family chosen
|
|
|
|
depending on kernel release: sg for <2.6 , sr for >=2.6
|
|
|
|
*/
|
|
|
|
static int linux_sg_auto_family = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* Set this to 1 in order to accept any TYPE_* (see scsi/scsi.h) */
|
|
|
|
/* But try with 0 first. There is hope via CDROM_DRIVE_STATUS. */
|
|
|
|
/* !!! DO NOT SET TO 1 UNLESS YOU PROTECTED ALL INDISPENSIBLE DEVICES
|
|
|
|
chmod -rw !!! */
|
|
|
|
static int linux_sg_accept_any_type = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* The device file family to use for SCSI transport over ATA.
|
|
|
|
This must be a printf formatter with one single placeholder for a
|
|
|
|
_single_ char in the range of 'a' to 'z'. This placeholder _must_ be
|
|
|
|
at the end of the formatter string.
|
|
|
|
E.g. "/dev/hd%c"
|
|
|
|
*/
|
|
|
|
static char linux_ata_device_family[80] = {"/dev/hd%c"};
|
|
|
|
|
|
|
|
/* Set this to 1 in order to get on stderr messages from ata_enumerate()
|
|
|
|
*/
|
|
|
|
static int linux_ata_enumerate_verbous = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* The waiting time before eventually retrying a failed SCSI command.
|
|
|
|
Before each retry wait Libburn_sg_linux_retry_incR longer than with
|
|
|
|
the previous one.
|
|
|
|
*/
|
|
|
|
#define Libburn_sg_linux_retry_usleeP 100000
|
|
|
|
#define Libburn_sg_linux_retry_incR 100000
|
|
|
|
|
|
|
|
|
|
|
|
/** PORTING : ------ libburn portable headers and definitions ----- */
|
|
|
|
|
|
|
|
#include "libburn.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "drive.h"
|
|
|
|
#include "sg.h"
|
|
|
|
#include "spc.h"
|
|
|
|
#include "mmc.h"
|
|
|
|
#include "sbc.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "toc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include "libdax_msgs.h"
|
|
|
|
extern struct libdax_msgs *libdax_messenger;
|
|
|
|
|
|
|
|
/* ts A51221 */
|
|
|
|
int burn_drive_is_banned(char *device_address);
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* PORTING: Private definitions. Port only if needed by public functions. */
|
|
|
|
/* (Public functions are listed below) */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
static void enumerate_common(char *fname, int bus_no, int host_no,
|
|
|
|
int channel_no, int target_no, int lun_no);
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60813 : storage objects are in libburn/init.c
|
|
|
|
whether to use O_EXCL with open(2) of devices
|
|
|
|
whether to use fcntl(,F_SETLK,) after open(2) of devices
|
|
|
|
what device family to use : 0=default, 1=sr, 2=scd, (3=st), 4=sg
|
|
|
|
whether to use O_NOBLOCK with open(2) on devices
|
|
|
|
whether to take O_EXCL rejection as fatal error
|
|
|
|
*/
|
|
|
|
extern int burn_sg_open_o_excl;
|
|
|
|
extern int burn_sg_fcntl_f_setlk;
|
|
|
|
extern int burn_sg_use_family;
|
|
|
|
extern int burn_sg_open_o_nonblock;
|
|
|
|
extern int burn_sg_open_abort_busy;
|
|
|
|
|
|
|
|
/* ts A91111 :
|
|
|
|
whether to log SCSI commands:
|
|
|
|
bit0= log in /tmp/libburn_sg_command_log
|
|
|
|
bit1= log to stderr
|
|
|
|
bit2= flush every line
|
|
|
|
*/
|
|
|
|
extern int burn_sg_log_scsi;
|
|
|
|
|
|
|
|
/* ts A60821
|
|
|
|
debug: for tracing calls which might use open drive fds
|
|
|
|
or for catching SCSI usage of emulated drives. */
|
|
|
|
int mmc_function_spy(struct burn_drive *d, char * text);
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* PORTING: Private functions. Port only if needed by public functions */
|
|
|
|
/* (Public functions are listed below) */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
/* ts A70413 */
|
|
|
|
/* This finds out wether the software is running on kernel >= 2.6
|
|
|
|
*/
|
|
|
|
static void sg_evaluate_kernel(void)
|
|
|
|
{
|
|
|
|
struct utsname buf;
|
|
|
|
if (sg_kernel_age >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sg_kernel_age = 0;
|
|
|
|
if (uname(&buf) == -1)
|
|
|
|
return;
|
|
|
|
sg_kernel_age = 1;
|
|
|
|
if (strcmp(buf.release, "2.6") >= 0)
|
|
|
|
sg_kernel_age = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A70314 */
|
|
|
|
/* This installs the device file family if one was chosen explicitely
|
|
|
|
by burn_preset_device_open()
|
|
|
|
*/
|
|
|
|
static void sg_select_device_family(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* >>> ??? do we need a mutex here ? */
|
|
|
|
/* >>> (It might be concurrent but is supposed to have always
|
|
|
|
the same effect. Any race condition should be harmless.) */
|
|
|
|
|
|
|
|
if (burn_sg_use_family == 1)
|
|
|
|
strcpy(linux_sg_device_family, "/dev/sr%d");
|
|
|
|
else if (burn_sg_use_family == 2)
|
|
|
|
strcpy(linux_sg_device_family, "/dev/scd%d");
|
|
|
|
else if (burn_sg_use_family == 3)
|
|
|
|
strcpy(linux_sg_device_family, "/dev/st%d");
|
|
|
|
else if (burn_sg_use_family == 4)
|
|
|
|
strcpy(linux_sg_device_family, "/dev/sg%d");
|
|
|
|
else if (linux_sg_auto_family) {
|
|
|
|
sg_evaluate_kernel();
|
|
|
|
if (sg_kernel_age >= 2)
|
|
|
|
strcpy(linux_sg_device_family, "/dev/sr%d");
|
|
|
|
else
|
|
|
|
strcpy(linux_sg_device_family, "/dev/sg%d");
|
|
|
|
linux_sg_auto_family = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A80701 */
|
|
|
|
/* This cares for the case that no /dev/srNN but only /dev/scdNN exists.
|
|
|
|
A theoretical case which has its complement in SuSE 10.2 having
|
|
|
|
/dev/sr but not /dev/scd.
|
|
|
|
*/
|
|
|
|
static int sg_exchange_scd_for_sr(char *fname, int flag)
|
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
char scd[17], msg[160];
|
|
|
|
|
|
|
|
if (burn_sg_use_family != 0 || strncmp(fname, "/dev/sr", 7)!=0 ||
|
|
|
|
strlen(fname)>9 || strlen(fname)<8)
|
|
|
|
return 2;
|
|
|
|
if (fname[7] < '0' || fname[7] > '9')
|
|
|
|
return 2;
|
|
|
|
if (fname [8] != 0 && (fname[7] < '0' || fname[7] > '9'))
|
|
|
|
return 2;
|
|
|
|
if (stat(fname, &stbuf) != -1)
|
|
|
|
return 2;
|
|
|
|
strcpy(scd, "/dev/scd");
|
|
|
|
strcpy(scd + 8, fname + 7);
|
|
|
|
if (stat(scd, &stbuf) == -1)
|
|
|
|
return 2;
|
|
|
|
sprintf(msg, "%s substitutes for non-existent %s", scd, fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
|
|
|
|
LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
|
|
|
|
strcpy(fname, scd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int sgio_test(int fd)
|
|
|
|
{
|
|
|
|
unsigned char test_ops[] = { 0, 0, 0, 0, 0, 0 };
|
|
|
|
sg_io_hdr_t s;
|
|
|
|
|
|
|
|
memset(&s, 0, sizeof(sg_io_hdr_t));
|
|
|
|
s.interface_id = 'S';
|
|
|
|
s.dxfer_direction = SG_DXFER_NONE;
|
|
|
|
s.cmd_len = 6;
|
|
|
|
s.cmdp = test_ops;
|
|
|
|
s.timeout = 12345;
|
|
|
|
return ioctl(fd, SG_IO, &s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60924 */
|
|
|
|
static int sg_handle_busy_device(char *fname, int os_errno)
|
|
|
|
{
|
|
|
|
char msg[4096];
|
|
|
|
struct stat stbuf;
|
|
|
|
int looks_like_hd= 0, fd, ret;
|
|
|
|
|
|
|
|
/* ts A80713 :
|
|
|
|
check existence of /dev/hdX1 as hint for hard disk rather than CD
|
|
|
|
Hint by Giulio Orsero: check /proc/ide/hdX/media for "disk"
|
|
|
|
*/
|
|
|
|
if (strncmp(fname, "/dev/hd", 7)==0) {
|
|
|
|
sprintf(msg, "%s1", fname);
|
|
|
|
if (stat(msg, &stbuf) != -1)
|
|
|
|
looks_like_hd= 1;
|
|
|
|
sprintf(msg, "/proc/ide/hd%c/media", fname[7]);
|
|
|
|
fd = open(msg, O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
ret = read(fd, msg, 10);
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
|
|
|
msg[ret]= 0;
|
|
|
|
close(fd);
|
|
|
|
if (strncmp(msg, "disk\n", 5) == 0 ||
|
|
|
|
strcmp(msg, "disk") == 0)
|
|
|
|
looks_like_hd= 2;
|
|
|
|
else if (strncmp(msg, "cdrom\n", 6) == 0 ||
|
|
|
|
strcmp(msg, "cdrom") == 0)
|
|
|
|
looks_like_hd= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ts A60814 : i saw no way to do this more nicely */
|
|
|
|
if (burn_sg_open_abort_busy) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn: FATAL : Application triggered abort on busy device '%s'\n",
|
|
|
|
fname);
|
|
|
|
|
|
|
|
/* ts A61007 */
|
|
|
|
abort();
|
|
|
|
/* a ssert("drive busy" == "non fatal"); */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ts A60924 : now reporting to libdax_msgs */
|
|
|
|
if (looks_like_hd == 2) { /* is surely hard disk */
|
|
|
|
;
|
|
|
|
} else if (looks_like_hd) {
|
|
|
|
sprintf(msg, "Could not examine busy device '%s'", fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x0002015a,
|
|
|
|
LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_LOW,
|
|
|
|
msg, os_errno, 0);
|
|
|
|
sprintf(msg,
|
|
|
|
"Busy '%s' seems to be a hard disk, as '%s1' exists. But better check.",
|
|
|
|
fname, fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x0002015b,
|
|
|
|
LIBDAX_MSGS_SEV_HINT, LIBDAX_MSGS_PRIO_LOW,
|
|
|
|
msg, 0, 0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sprintf(msg, "Cannot open busy device '%s'", fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020001,
|
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_LOW,
|
|
|
|
msg, os_errno, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60925 : ticket 74 */
|
|
|
|
static int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry)
|
|
|
|
{
|
|
|
|
int ret, os_errno, sevno= LIBDAX_MSGS_SEV_DEBUG;
|
|
|
|
char msg[4096+100];
|
|
|
|
|
|
|
|
if(*fd < 0)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
#ifdef CDROM_MEDIA_CHANGED_disabled_because_not_helpful
|
|
|
|
#ifdef CDSL_CURRENT
|
|
|
|
/* ts A80217 : wondering whether the os knows about our activities */
|
|
|
|
ret = ioctl(*fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
|
|
|
|
sprintf(msg, "ioctl(CDROM_MEDIA_CHANGED) == %d", ret);
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
|
|
|
|
LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
|
|
|
|
|
|
|
|
#ifdef BLKFLSBUF_disabled_because_not_helpful
|
|
|
|
ret = ioctl(*fd, BLKFLSBUF, 0);
|
|
|
|
sprintf(msg, "ioctl(BLKFLSBUF) == %d", ret);
|
|
|
|
os_errno = 0;
|
|
|
|
if(ret == -1)
|
|
|
|
os_errno = errno;
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
|
|
|
|
LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno,0);
|
|
|
|
#endif /* BLKFLSBUF */
|
|
|
|
|
|
|
|
#endif /* CDSL_CURRENT */
|
|
|
|
#endif /* CDROM_MEDIA_CHANGED */
|
|
|
|
|
|
|
|
ret = close(*fd);
|
|
|
|
*fd = -1337;
|
|
|
|
if(ret != -1) {
|
|
|
|
/* ts A70409 : DDLP-B */
|
|
|
|
/* >>> release single lock on fname */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
os_errno= errno;
|
|
|
|
|
|
|
|
sprintf(msg, "Encountered error when closing drive '%s'", fname);
|
|
|
|
if (sorry)
|
|
|
|
sevno = LIBDAX_MSGS_SEV_SORRY;
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno, 0x00020002,
|
|
|
|
sevno, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A70401 :
|
|
|
|
fcntl() has the unappealing property to work only after open().
|
|
|
|
So libburn will by default use open(O_EXCL) first and afterwards
|
|
|
|
as second assertion will use fcntl(F_SETLK). One lock more should not harm.
|
|
|
|
*/
|
|
|
|
static int sg_fcntl_lock(int *fd, char *fd_name, int l_type, int verbous)
|
|
|
|
{
|
|
|
|
struct flock lockthing;
|
|
|
|
char msg[81];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!burn_sg_fcntl_f_setlk)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
memset(&lockthing, 0, sizeof(lockthing));
|
|
|
|
lockthing.l_type = l_type;
|
|
|
|
lockthing.l_whence = SEEK_SET;
|
|
|
|
lockthing.l_start = 0;
|
|
|
|
lockthing.l_len = 0;
|
|
|
|
/*
|
|
|
|
fprintf(stderr,"LIBBURN_EXPERIMENTAL: fcntl(%d, F_SETLK, %s)\n",
|
|
|
|
*fd, l_type == F_WRLCK ? "F_WRLCK" : "F_RDLCK");
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = fcntl(*fd, F_SETLK, &lockthing);
|
|
|
|
if (ret == -1) {
|
|
|
|
if (verbous) {
|
|
|
|
sprintf(msg, "Device busy. Failed to fcntl-lock '%s'",
|
|
|
|
fd_name);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020008,
|
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
msg, errno, 0);
|
|
|
|
}
|
|
|
|
close(*fd);
|
|
|
|
*fd = -1;
|
|
|
|
|
|
|
|
/* ts A70409 : DDLP-B */
|
|
|
|
/* >>> release single lock on fd_name */
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
static int sg_open_drive_fd(char *fname, int scan_mode)
|
|
|
|
{
|
|
|
|
int open_mode = O_RDWR, fd, tries= 0;
|
|
|
|
char msg[81];
|
|
|
|
|
|
|
|
/* ts A70409 : DDLP-B */
|
|
|
|
/* >>> obtain single lock on fname */
|
|
|
|
|
|
|
|
/* ts A60813 - A60927
|
|
|
|
O_EXCL with devices is a non-POSIX feature
|
|
|
|
of Linux kernels. Possibly introduced 2002.
|
|
|
|
Mentioned in "The Linux SCSI Generic (sg) HOWTO" */
|
|
|
|
if(burn_sg_open_o_excl)
|
|
|
|
open_mode |= O_EXCL;
|
|
|
|
/* ts A60813
|
|
|
|
O_NONBLOCK was already hardcoded in ata_ but not in sg_.
|
|
|
|
There must be some reason for this. So O_NONBLOCK is
|
|
|
|
default mode for both now. Disable on own risk.
|
|
|
|
ts A70411
|
|
|
|
Switched to O_NDELAY for LKML statement 2007/4/11/141 by Alan Cox:
|
|
|
|
"open() has side effects. The CD layer allows you to open
|
|
|
|
with O_NDELAY if you want to avoid them."
|
|
|
|
*/
|
|
|
|
if(burn_sg_open_o_nonblock)
|
|
|
|
open_mode |= O_NDELAY;
|
|
|
|
|
|
|
|
/* <<< debugging
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn: experimental: o_excl= %d , o_nonblock= %d, abort_on_busy= %d\n",
|
|
|
|
burn_sg_open_o_excl,burn_sg_open_o_nonblock,burn_sg_open_abort_busy);
|
|
|
|
fprintf(stderr,
|
|
|
|
"libburn: experimental: O_EXCL= %d , O_NDELAY= %d\n",
|
|
|
|
!!(open_mode&O_EXCL),!!(open_mode&O_NDELAY));
|
|
|
|
*/
|
|
|
|
|
|
|
|
try_open:;
|
|
|
|
fd = open(fname, open_mode);
|
|
|
|
if (fd == -1) {
|
|
|
|
/* <<< debugging
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn: experimental: fname= %s , errno= %d\n",
|
|
|
|
fname,errno);
|
|
|
|
*/
|
|
|
|
if (errno == EBUSY) {
|
|
|
|
tries++;
|
|
|
|
|
|
|
|
/* <<< debugging
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn_DEBUG: EBUSY , tries= %d\n", tries);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tries < 4) {
|
|
|
|
usleep(2000000);
|
|
|
|
goto try_open;
|
|
|
|
}
|
|
|
|
sg_handle_busy_device(fname, errno);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (scan_mode)
|
|
|
|
return -1;
|
|
|
|
sprintf(msg, "Failed to open device '%s'",fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020005,
|
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
msg, errno, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sg_fcntl_lock(&fd, fname, F_WRLCK, 1);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
static int sg_release_siblings(int sibling_fds[],
|
|
|
|
char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
|
|
|
|
int *sibling_count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char msg[81];
|
|
|
|
|
|
|
|
for(i= 0; i < *sibling_count; i++)
|
|
|
|
sg_close_drive_fd(sibling_fnames[i], -1, &(sibling_fds[i]), 0);
|
|
|
|
if(*sibling_count > 0) {
|
|
|
|
sprintf(msg, "Closed %d O_EXCL scsi siblings", *sibling_count);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020007,
|
|
|
|
LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH, msg, 0,0);
|
|
|
|
}
|
|
|
|
*sibling_count = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
static int sg_close_drive(struct burn_drive *d)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!burn_drive_is_open(d))
|
|
|
|
return 0;
|
|
|
|
sg_release_siblings(d->sibling_fds, d->sibling_fnames,
|
|
|
|
&(d->sibling_count));
|
|
|
|
ret = sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
static int sg_open_scsi_siblings(char *path, int driveno,
|
|
|
|
int sibling_fds[],
|
|
|
|
char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
|
|
|
|
int *sibling_count,
|
|
|
|
int host_no, int channel_no, int id_no, int lun_no)
|
|
|
|
{
|
|
|
|
int tld, i, ret, fd, i_bus_no = -1;
|
|
|
|
int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
|
|
|
|
char msg[161], fname[81];
|
|
|
|
struct stat stbuf;
|
|
|
|
dev_t last_rdev = 0, path_rdev;
|
|
|
|
|
|
|
|
static char tldev[][81]= {"/dev/sr%d", "/dev/scd%d", "/dev/sg%d", ""};
|
|
|
|
/* ts A70609: removed "/dev/st%d" */
|
|
|
|
|
|
|
|
if(stat(path, &stbuf) == -1)
|
|
|
|
return 0;
|
|
|
|
path_rdev = stbuf.st_rdev;
|
|
|
|
|
|
|
|
sg_select_device_family();
|
|
|
|
if (linux_sg_device_family[0] == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(host_no < 0 || id_no < 0 || channel_no < 0 || lun_no < 0)
|
|
|
|
return(2);
|
|
|
|
if(*sibling_count > 0)
|
|
|
|
sg_release_siblings(sibling_fds, sibling_fnames,
|
|
|
|
sibling_count);
|
|
|
|
|
|
|
|
for (tld = 0; tldev[tld][0] != 0; tld++) {
|
|
|
|
if (strcmp(tldev[tld], linux_sg_device_family)==0)
|
|
|
|
continue;
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
sprintf(fname, tldev[tld], i);
|
|
|
|
if(stat(fname, &stbuf) == -1)
|
|
|
|
continue;
|
|
|
|
if (path_rdev == stbuf.st_rdev)
|
|
|
|
continue;
|
|
|
|
if (*sibling_count > 0 && last_rdev == stbuf.st_rdev)
|
|
|
|
continue;
|
|
|
|
ret = sg_obtain_scsi_adr(fname, &i_bus_no, &i_host_no,
|
|
|
|
&i_channel_no, &i_target_no, &i_lun_no);
|
|
|
|
if (ret <= 0)
|
|
|
|
continue;
|
|
|
|
if (i_host_no != host_no || i_channel_no != channel_no)
|
|
|
|
continue;
|
|
|
|
if (i_target_no != id_no || i_lun_no != lun_no)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fd = sg_open_drive_fd(fname, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (*sibling_count>=BURN_OS_SG_MAX_SIBLINGS) {
|
|
|
|
sprintf(msg, "Too many scsi siblings of '%s'",
|
|
|
|
path);
|
|
|
|
libdax_msgs_submit(libdax_messenger,
|
|
|
|
driveno, 0x00020006,
|
|
|
|
LIBDAX_MSGS_SEV_FATAL,
|
|
|
|
LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
sprintf(msg, "Opened O_EXCL scsi sibling '%s' of '%s'",
|
|
|
|
fname, path);
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno,
|
|
|
|
0x00020004,
|
|
|
|
LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
msg, 0, 0);
|
|
|
|
sibling_fds[*sibling_count] = fd;
|
|
|
|
strcpy(sibling_fnames[*sibling_count], fname);
|
|
|
|
(*sibling_count)++;
|
|
|
|
last_rdev= stbuf.st_rdev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
failed:;
|
|
|
|
sg_release_siblings(sibling_fds, sibling_fnames, sibling_count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define Libburn_drive_new_deaL 1
|
|
|
|
|
|
|
|
#ifdef Libburn_drive_new_deaL
|
|
|
|
|
|
|
|
/* ts A80731 */
|
|
|
|
static int is_ata_drive(char *fname)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct hd_driveid tm;
|
|
|
|
|
|
|
|
fd = sg_open_drive_fd(fname, 1);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (linux_ata_enumerate_verbous)
|
|
|
|
fprintf(stderr,"open failed, errno=%d '%s'\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
ioctl(fd, HDIO_GET_IDENTITY, &tm);
|
|
|
|
|
|
|
|
/* not atapi */
|
|
|
|
if (!(tm.config & 0x8000) || (tm.config & 0x4000)) {
|
|
|
|
if (linux_ata_enumerate_verbous)
|
|
|
|
fprintf(stderr, "not marked as ATAPI\n");
|
|
|
|
sg_close_drive_fd(fname, -1, &fd, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if SG_IO fails on an atapi device, we should stop trying to
|
|
|
|
use hd* devices */
|
|
|
|
if (sgio_test(fd) == -1) {
|
|
|
|
if (linux_ata_enumerate_verbous)
|
|
|
|
fprintf(stderr,
|
|
|
|
"FATAL: sgio_test() failed: errno=%d '%s'\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
sg_close_drive_fd(fname, -1, &fd, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0) {
|
|
|
|
if (linux_ata_enumerate_verbous)
|
|
|
|
fprintf(stderr,
|
|
|
|
"cannot close properly, errno=%d '%s'\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int is_scsi_drive(char *fname, int *bus_no, int *host_no,
|
|
|
|
int *channel_no, int *target_no, int *lun_no)
|
|
|
|
{
|
|
|
|
int fd, sid_ret = 0, ret;
|
|
|
|
struct sg_scsi_id sid;
|
|
|
|
int sibling_fds[BURN_OS_SG_MAX_SIBLINGS], sibling_count= 0;
|
|
|
|
char sibling_fnames[BURN_OS_SG_MAX_SIBLINGS][BURN_OS_SG_MAX_NAMELEN];
|
|
|
|
|
|
|
|
fd = sg_open_drive_fd(fname, 1);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (linux_sg_enumerate_debug)
|
|
|
|
fprintf(stderr, "open failed, errno=%d '%s'\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sid_ret = ioctl(fd, SG_GET_SCSI_ID, &sid);
|
|
|
|
if (sid_ret == -1) {
|
|
|
|
sid.scsi_id = -1; /* mark SCSI address as invalid */
|
|
|
|
if(linux_sg_enumerate_debug)
|
|
|
|
fprintf(stderr,
|
|
|
|
"ioctl(SG_GET_SCSI_ID) failed, errno=%d '%s' , ",
|
|
|
|
errno, strerror(errno));
|
|
|
|
|
|
|
|
if (sgio_test(fd) == -1) {
|
|
|
|
if (linux_sg_enumerate_debug)
|
|
|
|
fprintf(stderr,
|
|
|
|
"FATAL: sgio_test() failed: errno=%d '%s'",
|
|
|
|
errno, strerror(errno));
|
|
|
|
|
|
|
|
sg_close_drive_fd(fname, -1, &fd, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CDROM_DRIVE_STATUS
|
|
|
|
/* http://developer.osdl.org/dev/robustmutexes/
|
|
|
|
src/fusyn.hg/Documentation/ioctl/cdrom.txt */
|
|
|
|
sid_ret = ioctl(fd, CDROM_DRIVE_STATUS, 0);
|
|
|
|
if(linux_sg_enumerate_debug)
|
|
|
|
fprintf(stderr,
|
|
|
|
"ioctl(CDROM_DRIVE_STATUS) = %d , ",
|
|
|
|
sid_ret);
|
|
|
|
if (sid_ret != -1 && sid_ret != CDS_NO_INFO)
|
|
|
|
sid.scsi_type = TYPE_ROM;
|
|
|
|
else
|
|
|
|
sid_ret = -1;
|
|
|
|
#endif /* CDROM_DRIVE_STATUS */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
/* Hearsay A61005 */
|
|
|
|
if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, bus_no) == -1)
|
|
|
|
*bus_no = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sg_close_drive_fd(fname, -1, &fd,
|
|
|
|
sid.scsi_type == TYPE_ROM ) <= 0) {
|
|
|
|
if (linux_sg_enumerate_debug)
|
|
|
|
fprintf(stderr,
|
|
|
|
"cannot close properly, errno=%d '%s'\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( (sid_ret == -1 || sid.scsi_type != TYPE_ROM)
|
|
|
|
&& !linux_sg_accept_any_type) {
|
|
|