tape.c 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * TAPE support
 *
 * Copyright 2006 Hans Leidekker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29 30 31 32
 */

#include "config.h"
#include "wine/port.h"

#include <stdarg.h>
#include <stdio.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_MTIO_H
#include <sys/mtio.h>
#endif

33 34 35
#if !defined(MTCOMPRESSION) && defined(MTCOMP)
#define MTCOMPRESSION MTCOMP
#endif
36 37 38
#if !defined(MTSETBLK) && defined(MTSETBSIZ)
#define MTSETBLK MTSETBSIZ
#endif
39 40 41 42 43 44
#if !defined(MTSETBLK) && defined(MTSRSZ)
#define MTSETBLK MTSRSZ
#endif
#ifndef MT_ST_BLKSIZE_MASK
#define MT_ST_BLKSIZE_MASK 0xffffff
#endif
45

46 47 48 49 50
/* Darwin 7.9.0 has MTSETBSIZ instead of MTSETBLK */
#if !defined(MTSETBLK) && defined(MTSETBSIZ)
#define MTSETBLK MTSETBSIZ
#endif

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
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "winioctl.h"
#include "ddk/ntddtape.h"
#include "ntdll_misc.h"
#include "wine/server.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(tape);

static const char *io2str( DWORD io )
{
    switch (io)
    {
#define X(x)    case (x): return #x;
    X(IOCTL_TAPE_CHECK_VERIFY);
    X(IOCTL_TAPE_CREATE_PARTITION);
    X(IOCTL_TAPE_ERASE);
    X(IOCTL_TAPE_FIND_NEW_DEVICES);
    X(IOCTL_TAPE_GET_DRIVE_PARAMS);
    X(IOCTL_TAPE_GET_MEDIA_PARAMS);
    X(IOCTL_TAPE_GET_POSITION);
    X(IOCTL_TAPE_GET_STATUS);
    X(IOCTL_TAPE_PREPARE);
    X(IOCTL_TAPE_SET_DRIVE_PARAMS);
    X(IOCTL_TAPE_SET_MEDIA_PARAMS);
    X(IOCTL_TAPE_SET_POSITION);
    X(IOCTL_TAPE_WRITE_MARKS);
#undef X
84
    default: { static char tmp[32]; sprintf(tmp, "IOCTL_TAPE_%d\n", io); return tmp; }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    }
}

/******************************************************************
 *      TAPE_GetStatus
 */
static NTSTATUS TAPE_GetStatus( int error )
{
    if (!error) return STATUS_SUCCESS;
    return FILE_GetNtStatus();
}

/******************************************************************
 *      TAPE_CreatePartition
 */
100
static NTSTATUS TAPE_CreatePartition( int fd, const TAPE_CREATE_PARTITION *data )
101 102 103 104
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

105
    TRACE( "fd: %d method: 0x%08x count: 0x%08x size: 0x%08x\n",
106 107 108 109 110 111 112 113 114 115
           fd, data->Method, data->Count, data->Size );

    if (data->Count > 1)
    {
        WARN( "Creating more than 1 partition is not supported\n" );
        return STATUS_INVALID_PARAMETER;
    }

    switch (data->Method)
    {
116
#ifdef MTMKPART
117 118 119 120 121 122 123 124 125
    case TAPE_FIXED_PARTITIONS:
    case TAPE_SELECT_PARTITIONS:
        cmd.mt_op = MTMKPART;
        cmd.mt_count = 0;
        break;
    case TAPE_INITIATOR_PARTITIONS:
        cmd.mt_op = MTMKPART;
        cmd.mt_count = data->Size;
        break;
126
#endif
127
    default:
128
        ERR( "Unhandled method: 0x%08x\n", data->Method );
129 130 131 132 133 134 135 136 137 138 139 140 141
        return STATUS_INVALID_PARAMETER;
    }

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_Erase
 */
142
static NTSTATUS TAPE_Erase( int fd, const TAPE_ERASE *data )
143 144 145 146
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

147
    TRACE( "fd: %d type: 0x%08x immediate: 0x%02x\n",
148 149 150 151 152 153 154 155 156 157 158 159 160
           fd, data->Type, data->Immediate );

    switch (data->Type)
    {
    case TAPE_ERASE_LONG:
        cmd.mt_op = MTERASE;
        cmd.mt_count = 1;
        break;
    case TAPE_ERASE_SHORT:
        cmd.mt_op = MTERASE;
        cmd.mt_count = 0;
        break;
    default:
161
        ERR( "Unhandled type: 0x%08x\n", data->Type );
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
        return STATUS_INVALID_PARAMETER;
    }

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_GetDriveParams
 */
static NTSTATUS TAPE_GetDriveParams( int fd, TAPE_GET_DRIVE_PARAMETERS *data )
{
#ifdef HAVE_SYS_MTIO_H
    struct mtget get;
    NTSTATUS status;

    TRACE( "fd: %d\n", fd );

    memset( data, 0, sizeof(TAPE_GET_DRIVE_PARAMETERS) );

    status = TAPE_GetStatus( ioctl( fd, MTIOCGET, &get ) );
    if (status != STATUS_SUCCESS)
        return status;

    data->ECC = FALSE;
    data->Compression = FALSE;
    data->DataPadding = FALSE;
    data->ReportSetmarks = FALSE;
193 194 195
#ifdef HAVE_STRUCT_MTGET_MT_BLKSIZ
    data->DefaultBlockSize = get.mt_blksiz;
#else
196
    data->DefaultBlockSize = get.mt_dsreg & MT_ST_BLKSIZE_MASK;
197
#endif
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
    data->MaximumBlockSize = data->DefaultBlockSize;
    data->MinimumBlockSize = data->DefaultBlockSize;
    data->MaximumPartitionCount = 1;

    return status;
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_GetMediaParams
 */
static NTSTATUS TAPE_GetMediaParams( int fd, TAPE_GET_MEDIA_PARAMETERS *data )
{
#ifdef HAVE_SYS_MTIO_H
    struct mtget get;
    NTSTATUS status;

    TRACE( "fd: %d\n", fd );

    memset( data, 0, sizeof(TAPE_GET_MEDIA_PARAMETERS) );

    status = TAPE_GetStatus( ioctl( fd, MTIOCGET, &get ) );
    if (status != STATUS_SUCCESS)
        return status;

    data->Capacity.u.LowPart = 1024 * 1024 * 1024;
    data->Remaining.u.LowPart = 1024 * 1024 * 1024;
228 229 230
#ifdef HAVE_STRUCT_MTGET_MT_BLKSIZ
    data->BlockSize = get.mt_blksiz;
#else
231
    data->BlockSize = get.mt_dsreg & MT_ST_BLKSIZE_MASK;
232
#endif
233
    data->PartitionCount = 1;
234
#ifdef HAVE_STRUCT_MTGET_MT_GSTAT
235
    data->WriteProtected = (GMT_WR_PROT(get.mt_gstat) != 0);
236 237 238
#else
    data->WriteProtected = 0;
#endif
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    return status;
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_GetPosition
 */
static NTSTATUS TAPE_GetPosition( int fd, ULONG type, TAPE_GET_POSITION *data )
{
#ifdef HAVE_SYS_MTIO_H
    struct mtget get;
254
#ifndef HAVE_STRUCT_MTGET_MT_BLKNO
255
    struct mtpos pos;
256
#endif
257 258
    NTSTATUS status;

259
    TRACE( "fd: %d type: 0x%08x\n", fd, type );
260 261 262 263 264 265 266

    memset( data, 0, sizeof(TAPE_GET_POSITION) );

    status = TAPE_GetStatus( ioctl( fd, MTIOCGET, &get ) );
    if (status != STATUS_SUCCESS)
        return status;

267
#ifndef HAVE_STRUCT_MTGET_MT_BLKNO
268 269 270
    status = TAPE_GetStatus( ioctl( fd, MTIOCPOS, &pos ) );
    if (status != STATUS_SUCCESS)
        return status;
271
#endif
272 273 274 275 276 277

    switch (type)
    {
    case TAPE_ABSOLUTE_BLOCK:
        data->Type = type;
        data->Partition = get.mt_resid;
278 279 280
#ifdef HAVE_STRUCT_MTGET_MT_BLKNO
        data->OffsetLow = get.mt_blkno;
#else
281
        data->OffsetLow = pos.mt_blkno;
282
#endif
283
        break;
284 285 286 287 288
    case TAPE_LOGICAL_BLOCK:
    case TAPE_PSEUDO_LOGICAL_BLOCK:
        WARN( "Positioning type not supported\n" );
        break;
    default:
289
        ERR( "Unhandled type: 0x%08x\n", type );
290 291 292 293 294 295 296 297 298 299 300 301 302
        return STATUS_INVALID_PARAMETER;
    }

    return status;
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_Prepare
 */
303
static NTSTATUS TAPE_Prepare( int fd, const TAPE_PREPARE *data )
304 305 306 307
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

308
    TRACE( "fd: %d type: 0x%08x immediate: 0x%02x\n",
309 310 311 312
           fd, data->Operation, data->Immediate );

    switch (data->Operation)
    {
313
#ifdef MTLOAD
314 315 316
    case TAPE_LOAD:
        cmd.mt_op = MTLOAD;
        break;
317 318
#endif
#ifdef MTUNLOAD
319 320 321
    case TAPE_UNLOAD:
        cmd.mt_op = MTUNLOAD;
        break;
322 323
#endif
#ifdef MTRETEN
324 325 326
    case TAPE_TENSION:
        cmd.mt_op = MTRETEN;
        break;
327 328
#endif
#ifdef MTLOCK
329 330 331
    case TAPE_LOCK:
        cmd.mt_op = MTLOCK;
        break;
332 333
#endif
#ifdef MTUNLOCK
334 335 336
    case TAPE_UNLOCK:
        cmd.mt_op = MTUNLOCK;
        break;
337
#endif
338 339 340 341
    case TAPE_FORMAT:
        /* Native ignores this if the drive doesn't support it */
        return STATUS_SUCCESS;
    default:
342
        ERR( "Unhandled operation: 0x%08x\n", data->Operation );
343 344 345 346 347 348 349 350 351 352 353 354 355
        return STATUS_INVALID_PARAMETER;
    }

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_SetDriveParams
 */
356
static NTSTATUS TAPE_SetDriveParams( int fd, const TAPE_SET_DRIVE_PARAMETERS *data )
357
{
358
#if defined(HAVE_SYS_MTIO_H) && defined(MTCOMPRESSION)
359 360 361 362
    struct mtop cmd;

    TRACE( "fd: %d ECC: 0x%02x, compression: 0x%02x padding: 0x%02x\n",
            fd, data->ECC, data->Compression, data->DataPadding );
363
    TRACE( "setmarks: 0x%02x zonesize: 0x%08x\n",
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
           data->ReportSetmarks, data->EOTWarningZoneSize );

    if (data->ECC || data->DataPadding || data->ReportSetmarks ||
        data->EOTWarningZoneSize ) WARN( "Setting not supported\n" );

    cmd.mt_op = MTCOMPRESSION;
    cmd.mt_count = data->Compression;

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}
        
/******************************************************************
 *      TAPE_SetMediaParams
 */
382
static NTSTATUS TAPE_SetMediaParams( int fd, const TAPE_SET_MEDIA_PARAMETERS *data )
383 384 385 386
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

387
    TRACE( "fd: %d blocksize: 0x%08x\n", fd, data->BlockSize );
388 389 390 391 392 393 394 395 396 397 398 399 400 401
    
    cmd.mt_op = MTSETBLK;
    cmd.mt_count = data->BlockSize;

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}
        
/******************************************************************
 *      TAPE_SetPosition
 */
402
static NTSTATUS TAPE_SetPosition( int fd, const TAPE_SET_POSITION *data )
403 404 405 406
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

407
    TRACE( "fd: %d method: 0x%08x partition: 0x%08x offset: 0x%08x immediate: 0x%02x\n",
408 409 410 411 412 413 414 415 416 417
           fd, data->Method, data->Partition, data->Offset.u.LowPart, data->Immediate );

    if (data->Offset.u.HighPart > 0)
        return STATUS_INVALID_PARAMETER;

    switch (data->Method)
    {
    case TAPE_REWIND:
        cmd.mt_op = MTREW;
        break;
418
#ifdef MTSEEK
419 420 421 422
    case TAPE_ABSOLUTE_BLOCK:
        cmd.mt_op = MTSEEK;
        cmd.mt_count = data->Offset.u.LowPart;
        break;
423 424
#endif
#ifdef MTEOM
425 426 427
    case TAPE_SPACE_END_OF_DATA:
        cmd.mt_op = MTEOM;
        break;
428
#endif
429 430 431 432 433 434 435 436 437
    case TAPE_SPACE_FILEMARKS:
        if (data->Offset.u.LowPart >= 0) {
            cmd.mt_op = MTFSF;
            cmd.mt_count = data->Offset.u.LowPart;
        }
        else {
            cmd.mt_op = MTBSF;
            cmd.mt_count = -data->Offset.u.LowPart;
        }
438
        break;
439
#if defined(MTFSS) && defined(MTBSS)
440 441 442 443 444 445 446 447 448
    case TAPE_SPACE_SETMARKS:
        if (data->Offset.u.LowPart >= 0) {
            cmd.mt_op = MTFSS;
            cmd.mt_count = data->Offset.u.LowPart;
        }
        else {
            cmd.mt_op = MTBSS;
            cmd.mt_count = -data->Offset.u.LowPart;
        }
449
        break;
450
#endif
451 452 453 454 455 456 457 458
    case TAPE_LOGICAL_BLOCK:
    case TAPE_PSEUDO_LOGICAL_BLOCK:
    case TAPE_SPACE_RELATIVE_BLOCKS:
    case TAPE_SPACE_SEQUENTIAL_FMKS:
    case TAPE_SPACE_SEQUENTIAL_SMKS:
        WARN( "Positioning method not supported\n" );
        return STATUS_INVALID_PARAMETER;
    default:
459
        ERR( "Unhandled method: 0x%08x\n", data->Method );
460 461 462 463 464 465 466 467 468 469 470 471 472
        return STATUS_INVALID_PARAMETER;
    }

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *      TAPE_WriteMarks
 */
473
static NTSTATUS TAPE_WriteMarks( int fd, const TAPE_WRITE_MARKS *data )
474 475 476 477
{
#ifdef HAVE_SYS_MTIO_H
    struct mtop cmd;

478
    TRACE( "fd: %d type: 0x%08x count: 0x%08x immediate: 0x%02x\n",
479 480 481 482
           fd, data->Type, data->Count, data->Immediate );

    switch (data->Type)
    {
483
#ifdef MTWSM
484 485 486 487
    case TAPE_SETMARKS:
        cmd.mt_op = MTWSM;
        cmd.mt_count = data->Count;
        break;
488
#endif
489 490 491 492 493 494 495
    case TAPE_FILEMARKS:
    case TAPE_SHORT_FILEMARKS:
    case TAPE_LONG_FILEMARKS:
        cmd.mt_op = MTWEOF;
        cmd.mt_count = data->Count;
        break;
    default:
496
        ERR( "Unhandled type: 0x%08x\n", data->Type );
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        return STATUS_INVALID_PARAMETER;
    }

    return TAPE_GetStatus( ioctl( fd, MTIOCTOP, &cmd ) );
#else
    FIXME( "Not implemented.\n" );
    return STATUS_NOT_SUPPORTED;
#endif
}

/******************************************************************
 *		TAPE_DeviceIoControl
 *
 * SEE ALSO
 *   NtDeviceIoControl.
 */
NTSTATUS TAPE_DeviceIoControl( HANDLE device, HANDLE event,
    PIO_APC_ROUTINE apc, PVOID apc_user, PIO_STATUS_BLOCK io_status,
    ULONG io_control, LPVOID in_buffer, DWORD in_size,
    LPVOID out_buffer, DWORD out_size )
{
    DWORD sz = 0;
    NTSTATUS status = STATUS_INVALID_PARAMETER;
520
    int fd, needs_close;
521

522
    TRACE( "%p %s %p %d %p %d %p\n", device, io2str(io_control),
523 524 525 526
           in_buffer, in_size, out_buffer, out_size, io_status );

    io_status->Information = 0;

527
    if ((status = server_get_unix_fd( device, 0, &fd, &needs_close, NULL, NULL )))
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
        goto error;

    switch (io_control)
    {
    case IOCTL_TAPE_CREATE_PARTITION:
        status = TAPE_CreatePartition( fd, (TAPE_CREATE_PARTITION *)in_buffer );
        break;
    case IOCTL_TAPE_ERASE:
        status = TAPE_Erase( fd, (TAPE_ERASE *)in_buffer );
        break;
    case IOCTL_TAPE_GET_DRIVE_PARAMS:
        status = TAPE_GetDriveParams( fd, (TAPE_GET_DRIVE_PARAMETERS *)out_buffer );
        break;
    case IOCTL_TAPE_GET_MEDIA_PARAMS:
        status = TAPE_GetMediaParams( fd, (TAPE_GET_MEDIA_PARAMETERS *)out_buffer );
        break;
544
    case IOCTL_TAPE_GET_POSITION:
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        status = TAPE_GetPosition( fd, ((TAPE_GET_POSITION *)in_buffer)->Type,
                                   (TAPE_GET_POSITION *)out_buffer );
        break;
    case IOCTL_TAPE_GET_STATUS:
        status = FILE_GetNtStatus();
        break;
    case IOCTL_TAPE_PREPARE:
        status = TAPE_Prepare( fd, (TAPE_PREPARE *)in_buffer );
        break;
    case IOCTL_TAPE_SET_DRIVE_PARAMS:
        status = TAPE_SetDriveParams( fd, (TAPE_SET_DRIVE_PARAMETERS *)in_buffer );
        break;
    case IOCTL_TAPE_SET_MEDIA_PARAMS:
        status = TAPE_SetMediaParams( fd, (TAPE_SET_MEDIA_PARAMETERS *)in_buffer );
        break;
560
    case IOCTL_TAPE_SET_POSITION:
561 562 563 564 565 566 567 568 569 570
        status = TAPE_SetPosition( fd, (TAPE_SET_POSITION *)in_buffer );
        break;
    case IOCTL_TAPE_WRITE_MARKS:
        status = TAPE_WriteMarks( fd, (TAPE_WRITE_MARKS *)in_buffer );
        break;

    case IOCTL_TAPE_CHECK_VERIFY:
    case IOCTL_TAPE_FIND_NEW_DEVICES:
        break;
    default:
571
        FIXME( "Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
572 573 574 575 576
               io_control, io_control >> 16, (io_control >> 14) & 3,
               (io_control >> 2) & 0xfff, io_control & 3 );
        break;
    }

577
    if (needs_close) close( fd );
578 579 580 581 582 583 584

error:
    io_status->u.Status = status;
    io_status->Information = sz;
    if (event) NtSetEvent( event, NULL );
    return status;
}