1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2016 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Kern Sibbald, MM
25  */
26 /**
27  * @file
28  * Record, and label definitions for Bareos media data format.
29  */
30 
31 #ifndef BAREOS_STORED_RECORD_H_
32 #define BAREOS_STORED_RECORD_H_ 1
33 
34 namespace storagedaemon {
35 
36 /**
37  * Return codes from read_device_volume_label()
38  */
39 enum {
40    VOL_NOT_READ = 1,                      /**< Volume label not read */
41    VOL_OK,                                /**< volume name OK */
42    VOL_NO_LABEL,                          /**< volume not labeled */
43    VOL_IO_ERROR,                          /**< volume I/O error */
44    VOL_NAME_ERROR,                        /**< Volume name mismatch */
45    VOL_CREATE_ERROR,                      /**< Error creating label */
46    VOL_VERSION_ERROR,                     /**< Bareos version error */
47    VOL_LABEL_ERROR,                       /**< Bad label type */
48    VOL_NO_MEDIA                           /**< Hard error -- no media present */
49 };
50 
51 enum rec_state {
52    st_none,                               /**< No state */
53    st_header,                             /**< Write header */
54    st_header_cont,
55    st_data
56 };
57 
58 
59 /*  See block.h for RECHDR_LENGTH */
60 
61 /*
62  * This is the Media structure for a record header.
63  *  NB: when it is written it is serialized.
64 
65    uint32_t VolSessionId;
66    uint32_t VolSessionTime;
67 
68  * The above 8 bytes are only written in a BB01 block, BB02
69  *  and later blocks contain these values in the block header
70  *  rather than the record header.
71 
72    int32_t  FileIndex;
73    int32_t  Stream;
74    uint32_t data_len;
75 
76  */
77 
78 /**
79  * Record state bit definitions
80  */
81 enum {
82    REC_NO_HEADER = 0,                 /**< No header read */
83    REC_PARTIAL_RECORD = 1,            /**< Returning partial record */
84    REC_BLOCK_EMPTY = 2,               /**< Not enough data in block */
85    REC_NO_MATCH = 3,                  /**< No match on continuation data */
86    REC_CONTINUATION = 4,              /**< Continuation record found */
87    REC_ISTAPE = 5                     /**< Set if device is tape */
88 };
89 
90 /*
91  * Keep this set to the last entry in the enum.
92  */
93 #define REC_STATE_MAX REC_ISTAPE
94 
95 /*
96  * Make sure you have enough bits to store all above bit fields.
97  */
98 #define REC_STATE_BYTES NbytesForBits(REC_STATE_MAX + 1)
99 
100 #define IsPartialRecord(r) (BitIsSet(REC_PARTIAL_RECORD, (r)->state_bits))
101 #define IsBlockEmpty(r) (BitIsSet(REC_BLOCK_EMPTY, (r)->state_bits))
102 
103 /*
104  * DeviceRecord for reading and writing records.
105  * It consists of a Record Header, and the Record Data
106  *
107  * This is the memory structure for the record header.
108  */
109 struct BootStrapRecord;                           /* satisfy forward reference */
110 struct DeviceRecord {
111    dlink link;                        /**< link for chaining in read_record.c */
112    /**<
113     * File and Block are always returned during reading and writing records.
114     */
115    uint32_t File;                     /**< File number */
116    uint32_t Block;                    /**< Block number */
117    uint32_t VolSessionId;             /**< Sequential id within this session */
118    uint32_t VolSessionTime;           /**< Session start time */
119    int32_t FileIndex;                 /**< Sequential file number */
120    int32_t Stream;                    /**< Full Stream number with high bits */
121    int32_t maskedStream;              /**< Masked Stream without high bits */
122    uint32_t data_len;                 /**< Current record length */
123    uint32_t remainder;                /**< Remaining bytes to read/write */
124    char state_bits[REC_STATE_BYTES];  /**< State bits */
125    rec_state state;                   /**< State of WriteRecordToBlock */
126    BootStrapRecord *bsr;                          /**< Pointer to bsr that matched */
127    POOLMEM *data;                     /**< Record data. This MUST be a memory pool item */
128    int32_t match_stat;                /**< BootStrapRecord match status */
129    uint32_t last_VolSessionId;        /**< Used in sequencing FI for Vbackup */
130    uint32_t last_VolSessionTime;
131    int32_t last_FileIndex;
132    int32_t last_Stream;               /**< Used in SD-SD replication */
133    bool own_mempool;                  /**< Do we own the POOLMEM pointed to in data ? */
134 };
135 
136 /*
137  * Values for LabelType that are put into the FileIndex field
138  * Note, these values are negative to distinguish them
139  * from user records where the FileIndex is forced positive.
140  */
141 #define PRE_LABEL   -1                /**< Vol label on unwritten tape */
142 #define VOL_LABEL   -2                /**< Volume label first file */
143 #define EOM_LABEL   -3                /**< Writen at end of tape */
144 #define SOS_LABEL   -4                /**< Start of Session */
145 #define EOS_LABEL   -5                /**< End of Session */
146 #define EOT_LABEL   -6                /**< End of physical tape (2 eofs) */
147 #define SOB_LABEL   -7                /**< Start of object -- file/directory */
148 #define EOB_LABEL   -8                /**< End of object (after all streams) */
149 
150 /*
151  * Volume Label Record.  This is the in-memory definition. The
152  * tape definition is defined in the serialization code itself
153  * ser_volume_label() and UnserVolumeLabel() and is slightly different.
154  */
155 struct Volume_Label {
156   /*
157    * The first items in this structure are saved
158    * in the Device buffer, but are not actually written
159    * to the tape.
160    */
161   int32_t LabelType;                  /**< This is written in header only */
162   uint32_t LabelSize;                 /**< length of serialized label */
163   /*
164    * The items below this line are stored on
165    * the tape
166    */
167   char Id[32];                        /**< Bareos Immortal ... */
168 
169   uint32_t VerNum;                    /**< Label version number */
170 
171   /* VerNum <= 10 */
172   float64_t label_date;               /**< Date tape labeled */
173   float64_t label_time;               /**< Time tape labeled */
174 
175   /* VerNum >= 11 */
176   btime_t   label_btime;              /**< tdate tape labeled */
177   btime_t   write_btime;              /**< tdate tape written */
178 
179   /* Unused with VerNum >= 11 */
180   float64_t write_date;               /**< Date this label written */
181   float64_t write_time;               /**< Time this label written */
182 
183   char VolumeName[MAX_NAME_LENGTH];   /**< Volume name */
184   char PrevVolumeName[MAX_NAME_LENGTH]; /**< Previous Volume Name */
185   char PoolName[MAX_NAME_LENGTH];     /**< Pool name */
186   char PoolType[MAX_NAME_LENGTH];     /**< Pool type */
187   char MediaType[MAX_NAME_LENGTH];    /**< Type of this media */
188 
189   char HostName[MAX_NAME_LENGTH];     /**< Host name of writing computer */
190   char LabelProg[50];                 /**< Label program name */
191   char ProgVersion[50];               /**< Program version */
192   char ProgDate[50];                  /**< Program build date/time */
193 
194 };
195 
196 #define SER_LENGTH_Volume_Label 1024   /**< max serialised length of volume label */
197 #define SER_LENGTH_Session_Label 1024  /**< max serialised length of session label */
198 
199 typedef struct Volume_Label VOLUME_LABEL;
200 
201 /**
202  * Session Start/End Label
203  *  This record is at the beginning and end of each session
204  */
205 struct Session_Label {
206   char Id[32];                        /**< Bareos Immortal ... */
207 
208   uint32_t VerNum;                    /**< Label version number */
209 
210   uint32_t JobId;                     /**< Job id */
211   uint32_t VolumeIndex;               /**< Sequence no of volume for this job */
212 
213   /* VerNum >= 11 */
214   btime_t   write_btime;              /**< Tdate this label written */
215 
216   /* VerNum < 11 */
217   float64_t write_date;               /**< Date this label written */
218 
219   /* Unused VerNum >= 11 */
220   float64_t write_time;               /**< Time this label written */
221 
222   char PoolName[MAX_NAME_LENGTH];     /**< Pool name */
223   char PoolType[MAX_NAME_LENGTH];     /**< Pool type */
224   char JobName[MAX_NAME_LENGTH];      /**< base Job name */
225   char ClientName[MAX_NAME_LENGTH];
226   char Job[MAX_NAME_LENGTH];          /**< Unique name of this Job */
227   char FileSetName[MAX_NAME_LENGTH];
228   char FileSetMD5[MAX_NAME_LENGTH];
229   uint32_t JobType;
230   uint32_t JobLevel;
231   /* The remainder are part of EOS label only */
232   uint32_t JobFiles;
233   uint64_t JobBytes;
234   uint32_t StartBlock;
235   uint32_t EndBlock;
236   uint32_t StartFile;
237   uint32_t EndFile;
238   uint32_t JobErrors;
239   uint32_t JobStatus;                 /**< Job status */
240 
241 };
242 typedef struct Session_Label SESSION_LABEL;
243 
244 #define SERIAL_BUFSIZE 1024           /**< Volume serialisation buffer size */
245 
246 /**
247  * Read context used to keep track of what is processed or not.
248  */
249 struct Read_Context {
250    DeviceRecord *rec;                   /**< Record currently being processed */
251    dlist *recs;                       /**< Linked list of record packets open */
252    SESSION_LABEL sessrec;             /**< Start Of Session record info */
253    uint32_t records_processed;        /**< Number of records processed from this block */
254    int32_t lastFileIndex;             /**< Last File Index processed */
255 };
256 
257 struct DelayedDataStream {
258    int32_t stream;                     /**< stream less new bits */
259    char *content;                      /**< stream data */
260    uint32_t content_length;            /**< stream length */
261 };
262 
263 #define READ_NO_FILEINDEX -999999
264 
265 #include "lib/mem_pool.h"
266 
267 
268 class DeviceControlRecord; /* Forward Reference */
269 struct DeviceBlock;         /* Forward Reference */
270 
271 const char *FI_to_ascii(char *buf, int fi);
272 const char *stream_to_ascii(char *buf, int stream, int fi);
273 const char *record_to_str(PoolMem &resultbuffer, JobControlRecord *jcr, const DeviceRecord *rec);
274 void DumpRecord(const char *tag, const DeviceRecord *rec);
275 bool WriteRecordToBlock(DeviceControlRecord *dcr, DeviceRecord *rec);
276 bool CanWriteRecordToBlock(DeviceBlock *block, const DeviceRecord *rec);
277 bool ReadRecordFromBlock(DeviceControlRecord *dcr, DeviceRecord *rec);
278 DeviceRecord *new_record(bool with_data = true);
279 void EmptyRecord(DeviceRecord *rec);
280 void CopyRecordState(DeviceRecord *dst, DeviceRecord *src);
281 void FreeRecord(DeviceRecord *rec);
282 uint64_t GetRecordAddress(const DeviceRecord *rec);
283 
284 } /* namespace storagedaemon */
285 
286 #endif
287