/*
*/
/*
* BSD 3 Clause License
*
* Copyright (c) 2007, The Storage Networking Industry Association.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* - Neither the name of The Storage Networking Industry Association (SNIA)
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* 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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
*/
/* Copyright (c) 2007, The Storage Networking Industry Association. */
/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
/* Copyright 2014 Nexenta Systems, Inc. All rights reserved. */
#include <sys/socketvar.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ndmpd_common.h"
#include "ndmpd.h"
/*
* Maximum mover record size
*/
tlm_buffer_t *buf);
tlm_buffer_t *buf);
static int set_socket_nonblock(int sock);
/*
* Set non-blocking mode for socket.
*/
static int
{
int flags;
if (flags < 0)
return (0);
}
/*
* ************************************************************************
* NDMP V2 HANDLERS
* ************************************************************************
*/
/*
* ndmpd_mover_get_state_v2
*
* This handler handles the mover_get_state request.
* Status information for the mover state machine is returned.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending tape_get_state reply");
}
/*
* ndmpd_mover_listen_v2
*
* This handler handles mover_listen requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
"sending mover_listen reply");
return;
}
} else {
"sending mover_listen reply");
return;
}
}
/*
* ndmp window should always set by client during restore
*/
/* Set the default window. */
"sending mover_listen reply");
}
/*
* ndmpd_mover_continue_v2
*
* This handler handles mover_continue requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending mover_continue reply");
return;
}
"sending mover_continue reply");
}
/*
* ndmpd_mover_abort_v2
*
* This handler handles mover_abort requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending mover_abort reply");
return;
}
"sending mover_abort reply");
}
/*
* ndmpd_mover_stop_v2
*
* This handler handles mover_stop requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending mover_stop reply");
return;
}
"sending mover_stop reply");
(void) ndmpd_mover_init(session);
(void) ndmp_lbr_init(session);
}
/*
* ndmpd_mover_set_window_v2
*
* This handler handles mover_set_window requests.
*
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
/*
* The NDMPv2 specification states that "a window can be set only
* when in the listen or paused state."
*
* See the comment in ndmpd_mover_set_window_v3 regarding the reason for
* allowing it in the idle state as well.
*/
} else {
} else {
}
}
"sending mover_set_window reply");
}
/*
* ndmpd_mover_read_v2
*
* This handler handles mover_read requests. If the requested offset is
* outside of the current window, the mover is paused and a notify_mover_paused
* request is sent notifying the client that a seek is required. If the
* requested offest is within the window but not within the current record,
* then the tape is positioned to the record containing the requested offest.
* The requested amount of data is then read from the tape device and written
* to the data connection.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
int err;
"sending mover_read reply");
return;
}
"sending mover_read reply");
return;
}
if (err < 0) {
return;
}
/*
* Just return if we are waiting for the NDMP client to
* complete the seek.
*/
if (err == 1)
return;
/*
* Start the mover for restore in the 3-way backups.
*/
if (start_mover_for_restore(session) < 0)
}
/*
* ndmpd_mover_close_v2
*
* This handler handles mover_close requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending mover_close reply");
return;
}
}
/*
* ndmpd_mover_set_record_size_v2
*
* This handler handles mover_set_record_size requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
"sending mover_set_record_size reply");
}
/*
* ************************************************************************
* NDMP V3 HANDLERS
* ************************************************************************
*/
/*
* ndmpd_mover_get_state_v3
*
* This handler handles the ndmp_mover_get_state_request.
* Status information for the mover state machine is returned.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending ndmp_mover_get_state reply");
}
/*
* ndmpd_mover_listen_v3
*
* This handler handles ndmp_mover_listen_requests.
* and accept data connections initiated by a remote
* data server.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
"Invalid mover state to process listen request");
"Invalid data state to process listen request");
}
"error sending ndmp_mover_listen reply");
return;
}
case NDMP_ADDR_LOCAL:
break;
case NDMP_ADDR_TCP:
break;
}
break;
default:
}
}
"error sending ndmp_mover_listen reply");
}
/*
* ndmpd_mover_continue_v3
*
* This handler handles ndmp_mover_continue_requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
int ret;
"sending mover_continue reply");
return;
}
"sending mover_continue reply");
return;
}
/*
* Restore the file handler if the mover is remote to the data
* server and the handler was removed pending the continuation of a
* seek request. The handler is removed in mover_data_write().
*/
/*
* If we are here, it means that we needed DMA interference
* for seek. We should be on the right window, so we do not
* need the DMA interference anymore.
* We do another seek inside the Window to move to the
* exact position on the tape.
* If the resore is running without DAR the pause reason should
* not be seek.
*/
if (ret < 0) {
return;
}
if (!ret) {
HC_MOVER, mover_data_write_v3) < 0)
} else {
/*
* This should not happen because we should be in the
* right window. This means that DMA does not follow
* the V3 spec.
*/
return;
}
}
/* The tape has been likely exchanged, reset tape block counter */
"sending mover_continue reply");
}
/*
* ndmpd_mover_abort_v3
*
* This handler handles mover_abort requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending mover_abort reply");
return;
}
"sending mover_abort reply");
}
/*
* ndmpd_mover_set_window_v3
*
* This handler handles mover_set_window requests.
*
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
/*
* Note: The spec says that the window can be set only in the listen
* and paused states. We let this happen when mover is in the idle
* state as well. I can't rememebr which NDMP client (net_backup 4.5
* or net_worker 6.1.1) forced us to do this!
*/
else
} else
}
"sending mover_set_window_v3 reply");
return;
}
/*
* We have to update the position for DAR. DAR needs this
* information to position to the right index on tape,
* especially when we span the tapes.
*/
#ifdef NO_POSITION_CHANGE
/*
* Do not change the mover position if we are reading from
* the tape. In this way, we can use the position+window_length
* to know how much we can write to a tape before pausing with
* EOW reason.
*/
#endif /* NO_POSITION_CHANGE */
"sending mover_set_window_v3 reply");
}
/*
* ndmpd_mover_read_v3
*
* This handler handles ndmp_mover_read_requests.
* If the requested offset is outside of the current window, the mover
* is paused and a notify_mover_paused request is sent notifying the
* client that a seek is required. If the requested offest is within
* the window but not within the current record, then the tape is
* positioned to the record containing the requested offest. The requested
* amount of data is then read from the tape device and written to the
* data connection.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
int err;
} else {
}
"sending ndmp_mover_read_reply");
return;
if (err < 0) {
return;
}
/*
* Just return if we are waiting for the DMA to complete the seek.
*/
if (err == 1)
return;
/*
* Setup a handler function that will be called when
* data can be written to the data connection without blocking.
*/
mover_data_write_v3) < 0) {
return;
}
}
/*
* ndmpd_mover_set_record_size_v3
*
* This handler handles mover_set_record_size requests.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
char *cp;
"Invalid argument %d, should be > 0 and <= %d",
} else {
}
"sending mover_set_record_size reply");
}
/*
* ndmpd_mover_connect_v3
* Request handler. Connects the mover to either a local
* or remote data server.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
} else
"sending ndmp_mover_connect reply");
return;
}
case NDMP_ADDR_LOCAL:
/*
* Verify that the data server is listening for a
* local connection.
*/
"Data server is not in local listen state");
} else
break;
case NDMP_ADDR_TCP:
break;
default:
}
}
"sending ndmp_mover_connect reply");
}
/*
* ************************************************************************
* NDMP V4 HANDLERS
* ************************************************************************
*/
/*
* ndmpd_mover_get_state_v4
*
* This handler handles the ndmp_mover_get_state_request.
* Status information for the mover state machine is returned.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
/*ARGSUSED*/
void
{
"sending ndmp_mover_get_state reply");
}
/*
* ndmpd_mover_listen_v4
*
* This handler handles ndmp_mover_listen_requests.
* and accept data connections initiated by a remote
* data server.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
"Invalid mover state to process listen request");
"Invalid data state to process listen request");
}
"error sending ndmp_mover_listen reply");
return;
}
case NDMP_ADDR_LOCAL:
break;
case NDMP_ADDR_TCP:
break;
}
ndmp_malloc(sizeof (ndmp_tcp_addr_v4));
/* For compatibility with V3 */
break;
default:
}
}
"error sending ndmp_mover_listen reply");
}
/*
* ndmpd_mover_connect_v4
* Request handler. Connects the mover to either a local
* or remote data server.
*
* Parameters:
* connection (input) - connection handle.
* body (input) - request message body.
*
* Returns:
* void
*/
void
{
} else
"sending ndmp_mover_connect reply");
return;
}
case NDMP_ADDR_LOCAL:
/*
* Verify that the data server is listening for a
* local connection.
*/
"Data server is not in local listen state");
} else
break;
case NDMP_ADDR_TCP:
break;
default:
}
}
"sending ndmp_mover_connect reply");
}
/*
* ************************************************************************
* LOCALS
* ************************************************************************
*/
/*
* ndmpd_local_write
*
* Writes data to the mover.
* Buffers and write data to the tape device.
* A full tape record is buffered before being written.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - data length.
*
* Returns:
* 0 - data successfully written.
* -1 - error.
*/
int
{
ssize_t n;
/*
* A length of 0 indicates that any buffered data should be
* flushed to tape.
*/
if (length == 0) {
return (0);
(void) memset(
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
return (0);
}
/* Break the data into records. */
/*
* Determine if data needs to be buffered or
* can be written directly from user supplied location.
* We can fast path the write if there is no pending
* buffered data and there is at least a full record's worth
* of data to be written.
*/
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
count += n;
continue;
}
/* Buffer the data */
(void) memcpy(
/* Write the buffer if its full */
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
}
}
return (0);
}
/*
* ndmpd_remote_write
*
* Writes data to the remote mover.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - data length.
*
* Returns:
* 0 - data successfully written.
* -1 - error.
*/
int
{
ssize_t n;
return (-1);
return (-1);
}
count += n;
}
return (0);
}
/*
* ndmpd_local_read
*
* Reads data from the local tape device.
* Full tape records are read and buffered.
*
* Parameters:
* session (input) - session pointer.
* data (input) - location to store data.
* length (input) - data length.
*
* Returns:
* 0 - data successfully read.
* -1 - error.
* 1 - session terminated or operation aborted.
*/
int
{
ssize_t n;
/*
* Automatically increase the seek window if necessary.
* This is needed in the event the module attempts to read
* past a seek window set via a prior call to ndmpd_seek() or
* the module has not issued a seek. If no seek was issued then
* pretend that a seek was issued to read the entire tape.
*/
/* ndmpd_seek() never called? */
} else {
}
}
/*
* Read as many records as necessary to satisfy the request.
*/
/*
* If the end of the mover window has been reached,
* then notify the client that a new data window is needed.
*/
(void *) &pause_request, 0) < 0) {
"Sending notify_mover_paused request");
return (-1);
}
/*
* Wait until the state is changed by
* an abort or continue request.
*/
if (ndmp_wait_for_mover(session) != 0)
return (1);
}
/*
* Prevent reading past the end of the window.
*/
if (len >
/*
* Copy from the data buffer first.
*/
/*
* Limit the copy to the amount of data in the buffer.
*/
continue;
}
/*
* Determine if data needs to be buffered or
* can be read directly to user supplied location.
* We can fast path the read if at least a full record
* needs to be read and there is no seek pending.
* This is done to eliminate a buffer copy.
*/
if (n <= 0) {
if (n == TAPE_NO_WRITER_ERR)
return (1);
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (n == 0) ? (1) : (-1);
}
count += n;
continue;
}
/* Read the next record into the buffer. */
if (n <= 0) {
if (n == TAPE_NO_WRITER_ERR)
return (1);
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (n == 0) ? (1) : (-1);
}
/*
* Discard data if the current data stream position is
* prior to the seek position. This is necessary if a seek
* request set the seek pointer to a position that is not a
* record boundary. The seek request handler can only position
* to the start of a record.
*/
}
}
return (0);
}
/*
* ndmpd_remote_read
*
* Reads data from the remote mover.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - data length.
*
* Returns:
* 0 - data successfully read.
* -1 - error.
* 1 - session terminated or operation aborted.
*/
int
{
ssize_t n;
/*
* If the end of the seek window has been reached then
* send an ndmp_read request to the client.
* The NDMP client will then send a mover_data_read request to
* the remote mover and the mover will send more data.
* This condition can occur if the module attempts to read past
* a seek window set via a prior call to ndmpd_seek() or
* the module has not issued a seek. If no seek was issued then
* pretend that a seek was issued to read the entire tape.
*/
/* ndmpd_seek() never called? */
} else {
}
(void *) &request, 0) < 0) {
"Sending notify_data_read request");
return (-1);
}
}
return (1);
/*
* If the module called ndmpd_seek() prior to reading all of the
* data that the remote mover was requested to send, then the
* excess data from the seek has to be discardd.
*/
n = discard_data(session,
if (n < 0)
return (-1);
continue;
}
/*
* Don't attempt to read more data than the remote is sending.
*/
len)) < 0) {
return (-1);
}
/* read returns 0 if the connection was closed */
if (n == 0)
return (-1);
count += n;
}
return (0);
}
/* *** ndmpd internal functions ***************************************** */
/*
* ndmpd_mover_init
*
* Initialize mover specific session variables.
* Don't initialize variables such as record_size that need to
* persist across data operations. A client may open a connection and
* do multiple backups after setting the record_size.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0 - success.
* -1 - error.
*/
int
{
return (-1);
sizeof (ndmp_addr_v3));
}
return (0);
}
/*
* ndmpd_mover_shut_down
*
* Shutdown the mover. It closes all the sockets.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* void
*/
void
{
return;
(void) ndmpd_remove_file_handler(session,
}
(void) ndmpd_remove_file_handler(session,
}
}
/*
* ndmpd_mover_cleanup
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* void
*/
void
{
}
/*
* ndmpd_mover_connect
* Create a connection to the specified mover.
*
* Parameters:
* session (input) - session pointer
*
* Returns:
* error code.
*/
{
/*
* If the address type is TCP but both the address and
* the port number are zero, we have to use a different
* socket than the mover socket. This can happen when
* using NDMP disk to disk copy (AKA D2D copy).
* The NDMPCopy client will send a zero address to
* direct the server to use the mover socket as the
* data socket to receive the recovery data.
*/
return (NDMP_NO_ERR);
}
return (NDMP_IO_ERR);
}
sizeof (sin)) < 0) {
return (NDMP_IO_ERR);
}
} else {
"Not in active state mover"
" state = %d or Invalid mover sock=%d",
return (NDMP_ILLEGAL_STATE_ERR);
}
"session: 0x%x setting data sock fd: %d to be"
}
return (NDMP_NO_ERR);
}
/* Local mover connection. */
return (NDMP_ILLEGAL_STATE_ERR);
}
return (NDMP_DEV_NOT_OPEN_ERR);
}
if (mover_mode == NDMP_MOVER_MODE_READ &&
return (NDMP_WRITE_PROTECT_ERR);
}
return (NDMP_NO_ERR);
}
/*
* ndmpd_mover_seek
*
* Seek to the requested data stream position.
* If the requested offset is outside of the current window,
* the mover is paused and a notify_mover_paused request is sent
* notifying the client that a seek is required.
* If the requested offest is within the window but not within the
* current record, then the tape is positioned to the record containing
* the requested offest.
* The requested amount of data is then read from the tape device and
* written to the data connection.
*
* Parameters:
* session (input) - session pointer.
* offset (input) - data stream position to seek to.
* length (input) - amount of data that will be read.
*
* Returns:
* 1 - seek pending completion by the NDMP client.
* 0 - seek successfully completed.
* -1 - error.
*/
int
{
int ctlcmd;
int ctlcnt;
/*
* If the requested position is outside of the window,
* notify the client that a seek is required.
*/
(void *) &pause_request, 0) < 0) {
"Sending notify_mover_paused request");
return (-1);
}
return (1);
}
/*
* Determine the data stream position of the first byte in the
* data buffer.
*/
/*
* Determine the data stream position of the next byte that
* will be read from tape.
*/
/*
* Check if requested position is for data that has been read and is
* in the buffer.
*/
return (0);
}
ctlcmd = 0;
/* Need to seek backward. */
/* Need to seek forward. */
}
/* Reposition the tape if necessary. */
if (ctlcmd) {
}
return (0);
}
/* ** static functions ************************************************** */
/*
* create_listen_socket_v2
*
* Creates a socket for listening for accepting data connections.
*
* Parameters:
* session (input) - session pointer.
* addr (output) - location to store address of socket.
* port (output) - location to store port of socket.
*
* Returns:
* 0 - success.
* -1 - error.
*/
static int
{
return (-1);
/*
* Add a file handler for the listen socket.
* ndmpd_select will call accept_connection when a
* connection is ready to be accepted.
*/
accept_connection) < 0) {
return (-1);
}
return (0);
}
/*
* accept_connection
*
* Accept a data connection from a data server.
* Called by ndmpd_select when a connection is pending on
* the mover listen socket.
*
* Parameters:
* cookie (input) - session pointer.
* fd (input) - file descriptor.
* mode (input) - select mode.
*
* Returns:
* void.
*/
/*ARGSUSED*/
static void
{
int from_len;
&from_len);
return;
}
if (start_mover_for_backup(session) < 0) {
return;
}
} else {
}
}
/*
* tape_read
*
* Reads a data record from tape. Detects and handles EOT conditions.
*
* Parameters:
* session (input) - session pointer.
* data (input) - location to read data to.
*
* Returns:
* 0 - operation aborted.
* -1 - tape read error.
* otherwise - number of bytes read.
*/
static int
{
ssize_t n;
int err;
for (; ; ) {
if (n < 0) {
return (TAPE_READ_ERR);
}
if (n == 0) {
if (!is_writer_running(session))
return (TAPE_NO_WRITER_ERR);
/*
* End of media reached.
* Notify client and wait for the client to
* either abort the data operation or continue the
* operation after changing the tape.
*/
"End of tape reached. Load next tape");
"End of tape reached. Load next tape");
/* Operation aborted or connection terminated? */
if (err < 0) {
/*
* K.L. Go back one record if it is read
* but not used.
*/
(void) ndmp_mtioctl(
}
return (0);
}
/* Retry the read from the new tape. */
continue;
}
/* Change to pass Veritas Netbackup prequal test. */
data += n;
count -= n;
if (count <= 0) {
return (n);
}
}
}
/*
* change_tape
*
* Send a notify_pause request (protocol version 1) or
* notify_mover_pause request (protocol version 2) to the
* NDMP client to inform
* the client that a tape volume change is required.
* or continued.
*
* Parameters:
* client_data (input) - session pointer.
*
* Returns:
* 0 - operation has been continued.
* -1 - operation has been aborted.
*/
static int
{
else
(void *) &request, 0) < 0) {
"Sending notify_mover_paused request");
return (-1);
}
/*
* Wait for until the state is changed by
* an abort or continue request.
*/
return (ndmp_wait_for_mover(session));
}
/*
* discard_data
*
* Read and discard data from the data connection.
* Called when a module has called ndmpd_seek() prior to
* reading all of the data from the previous seek.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* number of bytes read and discarded.
* -1 - error.
*/
static int
{
int n;
char *addr;
return (-1);
/* Read and discard the data. */
if (n < 0) {
return (-1);
}
return (n);
}
/*
* mover_tape_read_one_buf
*
* Read one buffer from the tape. This is used by mover_tape_reader
*
* Parameters:
* session (input) - session pointer.
* buf (input) - buffer read
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
{
int n;
/*
* If the end of the mover window has been reached,
* then notify the client that a seek is needed.
* Remove the file handler to prevent this function from
* being called. The handler will be reinstalled in
* ndmpd_mover_continue.
*/
(void *) &pause_request, 0) < 0) {
"Sending notify_mover_paused request");
}
return (TAPE_READ_ERR);
}
if (n <= 0) {
if (n < 0)
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (TAPE_READ_ERR);
}
/*
* Discard data if the current data stream position is
* prior to the seek position. This is necessary if a seek
* request set the seek pointer to a position that is not a
* record boundary. The seek request handler can only position
* to the start of a record.
*/
return (0);
}
/*
* mover_tape_reader
*
* Mover tape reader thread. It is launched when the mover is started
* for restore.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
int
{
int rv;
return (-1);
}
cmds->tcs_reader_count++;
/*
* Let our parent thread know that we are running.
*/
/*
* The buffer is still full, wait for the consumer
* thread to use it.
*/
} else {
/*
* If there was an error while reading, such as
* end of stream.
*/
if (rv < 0) {
break;
}
/*
* Can we do more buffering?
*/
if (is_buffer_erroneous(buf)) {
"Exiting, errno: %d, eot: %d, eof: %d",
break;
}
(void) tlm_buffer_advance_in_idx(bufs);
}
}
/* If the consumer is waiting for us, wake it up. */
/*
* Clean up.
*/
cmds->tcs_reader_count--;
return (0);
}
/*
* mover_socket_write_one_buf
*
* Write one buffer to the network socket. This is used by mover_socket_writer
*
* Parameters:
* session (input) - session pointer.
* buf (input) - buffer read
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
{
int n;
/* Write the data to the data connection. */
errno = 0;
if (n < 0) {
return (-1);
}
/*
* If the read limit has been reached,
* then remove the file handler to prevent this
* function from getting called. The next mover_read request
* will reinstall the handler.
*/
(void) ndmpd_remove_file_handler(session,
return (-1);
}
return (0);
}
/*
* mover_socket_writer
*
* Mover's socket writer thread. This thread sends the read buffer
* from the tape to the data server through the network socket.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
int
{
return (-1);
}
cmds->tcs_writer_count++;
/*
* Let our parent thread know that we are running.
*/
"mover_socket_write_one_buf() < 0");
break;
}
(void) tlm_buffer_advance_out_idx(bufs);
} else {
/* No more data is coming, time to exit */
break;
}
/*
* The buffer is not full, wait for the producer
* thread to fill it.
*/
}
}
/* If the producer is waiting for us, wake it up. */
/*
* Clean up.
*/
cmds->tcs_writer_count--;
return (0);
}
/*
* start_mover_for_restore
*
* Creates the mover tape reader and network writer threads for
* the mover to perform the 3-way restore.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
{
long xfer_size;
int rc;
return (-1);
}
return (-1);
/*
* We intentionnally don't wait for the threads to start since the
* reply of the request (which resulted in calling this function)
* must be sent to the client before probable errors are sent
* to the client.
*/
if (rc == 0) {
} else {
return (-1);
}
if (rc == 0) {
} else {
return (-1);
}
return (0);
}
/*
* mover_socket_read_one_buf
*
* Read one buffer from the network socket for the mover. This is used
* by mover_socket_reader
*
* Parameters:
* session (input) - session pointer.
* buf (input) - buffer read
* read_size (input) - size to be read
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
long read_size)
{
int n, index;
long toread;
errno = 0;
toread);
if (n == 0) {
break;
} else if (n > 0) {
index += n;
toread -= n;
} else {
buf->tb_buffer_size = 0;
return (-1);
}
}
if (index > 0) {
if (read_size > 0)
} else {
buf->tb_buffer_size = 0;
}
" errno: %d, size: %d, data: 0x%x",
return (0);
}
/*
* mover_socket_reader
*
* Mover socket reader thread. This is used when reading data from the
* network socket for performing remote backups.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
int
{
static int nr = 0;
return (-1);
}
cmds->tcs_reader_count++;
/*
* Let our parent thread know that we are running.
*/
/*
* The buffer is still full, wait for the consumer
* thread to use it.
*/
} else {
/*
* Can we do more buffering?
*/
if (is_buffer_erroneous(buf)) {
"Exiting, errno: %d, eot: %d, eof: %d",
break;
}
(void) tlm_buffer_advance_in_idx(bufs);
}
}
/* If the consumer is waiting for us, wake it up. */
/*
* Clean up.
*/
cmds->tcs_reader_count--;
return (0);
}
/*
* mover_tape_writer_one_buf
*
* Write one buffer for the mover to the local tape device. This is
* used by mover_tape_writer thread.
*
* Parameters:
* session (input) - session pointer.
* buf (input) - buffer read
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
{
int n;
" errno: %d, size: %d, data: 0x%x",
if (n <= 0) {
return (-1);
}
return (0);
}
/*
* mover_tape_writer
*
* Mover tape writer thread. This is used for performing remote backups
* in a 3-way configuration. It writes the data from network socket to
* the locally attached tape device.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
int
{
int bidx;
static int nw = 0;
return (-1);
}
cmds->tcs_writer_count++;
/*
* Let our parent thread know that we are running.
*/
"mover_tape_write_one_buf() failed");
break;
}
(void) tlm_buffer_advance_out_idx(bufs);
} else {
/* No more data is coming, time to exit */
break;
}
/*
* The buffer is not full, wait for the producer
* thread to fill it.
*/
}
}
} else {
}
/* If the producer is waiting for us, wake it up. */
/*
* Clean up.
*/
cmds->tcs_writer_count--;
return (0);
}
/*
* start_mover_for_backup
*
* Starts a remote backup by running socket reader and tape
* writer threads. The mover runs a remote backup in a 3-way backup
* configuration.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: on success
* -1: otherwise
*/
static int
{
int rc;
return (-1);
}
return (-1);
/*
* We intentionally don't wait for the threads to start since the
* reply of the request (which resulted in calling this function)
* must be sent to the client before probable errors are sent
* to the client.
*/
if (rc == 0) {
} else {
return (-1);
}
if (rc == 0) {
} else {
return (-1);
}
return (0);
}
/*
* is_writer_running
*
* Find out if the writer thread has started or not.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: not started
* non-zero: started
* Note: non-zero is also returned if the backup type is
* neither TAR nor DUMP. I.e. the is_writer_running()
* check does not apply in this case and things should
* appear successful.
*/
static boolean_t
{
return (1);
rv = 0;
rv = 0;
else
return (rv);
}
/*
* is_writer_running_v3
*
* Find out if the writer thread has started or not.
*
* Parameters:
* session (input) - session pointer.
*
* Returns:
* 0: not started
* non-zero: started
* Note: non-zero is also returned if the backup type is
* neither TAR nor DUMP. I.e. the is_writer_running()
* check does not apply in this case and things should
* appear successful.
*/
static boolean_t
{
return (1);
rv = 0;
rv = 1;
rv = 0;
else
return (rv);
}
/*
* ndmpd_mover_error_send
*
* This function sends the notify message to the client.
*
* Parameters:
* session (input) - session pointer.
* reason (input) - halt reason.
*
* Returns:
* Error code
*/
int
{
}
/*
* ndmpd_mover_error_send_v4
*
* This function sends the notify message to the client.
*
* Parameters:
* session (input) - session pointer.
* reason (input) - halt reason.
*
* Returns:
* Error code
*/
int
{
}
/*
* ndmpd_mover_error
*
* This function is called when an unrecoverable mover error
* has been detected. A notify message is sent to the client and the
* mover is placed into the halted state.
*
* Parameters:
* session (input) - session pointer.
* reason (input) - halt reason.
*
* Returns:
* void.
*/
void
{
return;
"Error sending notify_mover_halted request");
} else {
/* No media error in V3 */
if (reason == NDMP_MOVER_HALT_MEDIA_ERROR)
"Error sending notify_mover_halted request");
}
(void) ndmpd_remove_file_handler(session,
}
(void) ndmpd_remove_file_handler(session,
}
}
/*
* mover_pause_v3
*
* Send an ndmp_notify_mover_paused request to the
* NDMP client to inform the client that its attention is required.
* or continued.
*
* Parameters:
* client_data (input) - session pointer.
* reason (input) - pause reason.
*
* Returns:
* 0 - operation has been continued.
* -1 - operation has been aborted.
*/
static int
{
int rv;
rv = 0;
NDMP_NO_ERR, (void *)&request, 0) < 0) {
"Error sending notify_mover_paused_request");
return (-1);
}
/*
* 3-way operations are single-thread. The same thread
* should process the messages.
*
* 2-way operations are multi-thread. The main thread
* processes the messages. We just need to wait and
* see if the mover state changes or the operation aborts.
*/
/*
* Process messages until the state is changed by
* an abort, continue, or close request .
*/
for (; ; ) {
return (-1);
return (-1);
case NDMP_MOVER_STATE_ACTIVE:
return (0);
case NDMP_MOVER_STATE_PAUSED:
continue;
default:
return (-1);
}
}
} else {
} else {
rv = -1;
}
}
return (rv);
}
/*
* mover_tape_write_v3
*
* Writes a data record to tape. Detects and handles EOT conditions.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - length of data to be written.
*
* Returns:
* 0 - operation aborted by client.
* -1 - error.
* otherwise - number of bytes written.
*/
static int
{
ssize_t n;
while (count > 0) {
/*
* Enforce mover window on write.
*/
/* Operation aborted or connection terminated */
return (-1);
}
if (n < 0) {
return (-1);
} else if (n > 0) {
count -= n;
data += n;
}
/* EOM handling */
if (count > 0) {
&mtstatus);
"mover record %d, file #%d, block #%d)", n,
/*
* Notify the client to either abort the operation
* or change the tape.
*/
"End of tape reached. Load next tape");
/* Operation aborted or connection terminated */
return (-1);
}
}
return (length);
}
/*
* mover_tape_flush_v3
*
* Writes all remaining buffered data to tape. A partial record is
* padded out to a full record with zeros.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - length of data to be written.
*
* Returns:
* -1 - error.
* otherwise - number of bytes written.
*/
static int
{
int n;
return (0);
ns_mover.md_w_index], 0,
if (n < 0) {
return (-1);
}
return (n);
}
/*
* ndmpd_local_write_v3
*
* Buffers and writes data to the tape device.
* A full tape record is buffered before being written.
*
* Parameters:
* session (input) - session pointer.
* data (input) - data to be written.
* length (input) - data length.
*
* Returns:
* 0 - data successfully written.
* -1 - error.
*/
int
{
ssize_t n;
return (-1);
}
/*
* A length of 0 indicates that any buffered data should be
* flushed to tape.
*/
if (length == 0) {
return (0);
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
return (0);
}
/* Break the data into records. */
/*
* Determine if data needs to be buffered or
* can be written directly from user supplied location.
* We can fast path the write if there is no pending
* buffered data and there is at least a full records worth
* of data to be written.
*/
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
count += n;
continue;
}
/* Buffer the data */
/* Write the buffer if its full */
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return (-1);
}
}
}
return (0);
}
/*
* mover_data_read_v3
*
* Reads backup data from the data connection and writes the
* received data to the tape device.
*
* Parameters:
* cookie (input) - session pointer.
* fd (input) - file descriptor.
* mode (input) - select mode.
*
* Returns:
* void.
*/
/*ARGSUSED*/
static void
{
int n;
/*
* Since this function is only called when select believes data
* is available to be read, a return of zero indicates the
* connection has been closed.
*/
if (n <= 0) {
if (n == 0) {
} else {
/* Socket is non-blocking, perhaps there are no data */
return;
}
}
/* Save the index since mover_tape_flush_v3 resets it. */
/* Flush any buffered data to tape. */
if (mover_tape_flush_v3(session) > 0) {
}
return;
}
if (n <= 0) {
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return;
}
}
}
/*
* mover_tape_read_v3
*
* Reads a data record from tape. Detects and handles EOT conditions.
*
* Parameters:
* session (input) - session pointer.
* data (input) - location to read data to.
*
* Returns:
* 0 - operation aborted.
* TAPE_READ_ERR - tape read IO error.
* TAPE_NO_WRITER_ERR - no writer is running during tape read
* otherwise - number of bytes read.
*/
static int
{
int pause_reason;
ssize_t n;
int err;
int count;
while (count > 0) {
if (n < 0) {
/*
* If at beginning of file and read fails with EIO,
* then it's repeated attempt to read at EOT.
*/
"End of tape reached. Load next tape");
}
/*
* According to NDMPv4 spec preferred error code when
* trying to read from blank tape is NDMP_EOM_ERR.
*/
"Blank tape detected, returning EOM");
"Blank tape. Load another tape");
} else {
return (TAPE_READ_ERR);
}
} else if (n > 0) {
data += n;
count -= n;
} else {
if (!is_writer_running_v3(session))
return (TAPE_NO_WRITER_ERR);
/*
* End of file or media reached. Notify client and
* wait for the client to either abort the data
* operation or continue the operation after changing
* the tape.
*/
if (tape_is_at_bof(session)) {
++ndmp_log_msg_id, "End of medium reached");
} else {
/* reposition the tape to BOT side of FM */
++ndmp_log_msg_id, "End of file reached.");
}
}
if (pause_reason != NDMP_MOVER_PAUSE_NA) {
/* Operation aborted or connection terminated? */
if (err < 0) {
return (0);
}
/* Retry the read from new location */
}
}
}
/*
* mover_data_write_v3
*
* Reads backup data from the tape device and writes the
* data to the data connection.
* This function is called by ndmpd_select when the data connection
* is ready for more data to be written.
*
* Parameters:
* cookie (input) - session pointer.
* fd (input) - file descriptor.
* mode (input) - select mode.
*
* Returns:
* void.
*/
/*ARGSUSED*/
static void
{
int n;
/*
* If the end of the mover window has been reached,
* then notify the client that a seek is needed.
* Remove the file handler to prevent this function from
* being called. The handler will be reinstalled in
* ndmpd_mover_continue.
*/
(void *)&pause_request, 0) < 0) {
"Sending notify_mover_paused request");
}
return;
}
/*
* Read more data into the tape buffer if the buffer is empty.
*/
"read %u bytes from tape", n);
if (n <= 0) {
ndmpd_mover_error(session, (n == 0 ?
return;
}
/*
* Discard data if the current data stream position is
* prior to the seek position. This is necessary if a seek
* request set the seek pointer to a position that is not a
* record boundary. The seek request handler can only position
* to the start of a record.
*/
}
}
/*
* The limit on the total amount of data to be sent can be
* dictated by either the end of the mover window or the end of the
* seek window.
* First determine which window applies and then determine if the
* send length needs to be less than a full record to avoid
* exceeding the window.
*/
else
/*
* Now limit the length to the amount of data in the buffer.
*/
/*
* Write the data to the data connection.
*/
if (n < 0) {
/* Socket is non-blocking, perhaps the write queue is full */
return;
}
return;
}
"wrote %u of %u bytes to data connection position %llu r_index %lu",
/*
* If all data in the buffer has been written,
* zero the buffer indices. The next call to this function
* will read more data from the tape device into the buffer.
*/
}
/*
* If the read limit has been reached,
* then remove the file handler to prevent this
* function from getting called. The next mover_read request
* will reinstall the handler.
*/
}
/*
* accept_connection_v3
*
* Accept a data connection from a data server.
* Called by ndmpd_select when a connection is pending on
* the mover listen socket.
*
* Parameters:
* cookie (input) - session pointer.
* fd (input) - file descriptor.
* mode (input) - select mode.
*
* Returns:
* void.
*/
/*ARGSUSED*/
static void
{
int from_len;
&from_len);
return;
}
/*
* Save the peer address.
*/
/* Set the parameter of the new socket */
/*
* non-blocking, otherwise ndmpd would be unable to process other
* incoming requests.
*/
"on socket: %m");
return;
}
HC_MOVER, mover_data_read_v3) < 0) {
return;
}
} else {
}
}
/*
* create_listen_socket_v3
*
* Creates a socket for listening for accepting data connections.
*
* Parameters:
* session (input) - session pointer.
* addr (output) - location to store address of socket.
* port (output) - location to store port of socket.
*
* Returns:
* 0 - success.
* -1 - error.
*/
static int
{
return (-1);
/*
* Add a file handler for the listen socket.
* ndmpd_select will call accept_connection when a
* connection is ready to be accepted.
*/
accept_connection_v3) < 0) {
return (-1);
}
return (0);
}
/*
* mover_connect_sock
*
* Connect the mover to the specified address
*
* Parameters:
* session (input) - session pointer.
* mode (input) - mover mode.
* addr (output) - location to store address of socket.
* port (output) - location to store port of socket.
*
* Returns:
* error code.
*/
static ndmp_error
{
int sock;
if (sock < 0)
return (NDMP_CONNECT_ERR);
/*
* non-blocking, otherwise ndmpd would be unable to process other
* incoming requests.
*/
if (!set_socket_nonblock(sock)) {
"on socket: %m");
return (NDMP_CONNECT_ERR);
}
if (mode == NDMP_MOVER_MODE_READ) {
return (NDMP_CONNECT_ERR);
}
}
return (NDMP_NO_ERR);
}
/*
* ndmpd_local_read_v3
*
* Reads data from the local tape device.
* Full tape records are read and buffered.
*
* Parameters:
* session (input) - session pointer.
* data (input) - location to store data.
* length (input) - data length.
*
* Returns:
* 1 - no read error but no writer running
* 0 - data successfully read.
* -1 - error.
*/
int
{
ssize_t n;
count = 0;
return (-1);
}
/*
* Automatically increase the seek window if necessary.
* This is needed in the event the module attempts to read
* past a seek window set via a prior call to ndmpd_seek() or
* the module has not issued a seek. If no seek was issued then
* pretend that a seek was issued to read the entire tape.
*/
/* ndmpd_seek() never called? */
} else {
}
}
/*
* Read as many records as necessary to satisfy the request.
*/
/*
* If the end of the mover window has been reached,
* then notify the client that a new data window is needed.
*/
if (mover_pause_v3(session,
NDMP_MOVER_PAUSE_SEEK) < 0) {
return (-1);
}
continue;
}
/*
* Prevent reading past the end of the window.
*/
/*
* Copy from the data buffer first.
*/
/*
* Limit the copy to the amount of data in the buffer.
*/
continue;
}
/*
* Determine if data needs to be buffered or
* can be read directly to user supplied location.
* We can fast path the read if at least a full record
* needs to be read and there is no seek pending.
* This is done to eliminate a buffer copy.
*/
if (n <= 0) {
if (n == TAPE_NO_WRITER_ERR)
return (1);
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return ((n == 0) ? 1 : -1);
}
count += n;
continue;
}
/* Read the next record into the buffer. */
if (n <= 0) {
if (n == TAPE_NO_WRITER_ERR)
return (1);
(n == 0 ? NDMP_MOVER_HALT_ABORTED :
return ((n == 0) ? 1 : -1);
}
/*
* Discard data if the current data stream position is
* prior to the seek position. This is necessary if a seek
* request set the seek pointer to a position that is not a
* record boundary. The seek request handler can only position
* to the start of a record.
*/
}
}
return (0);
}