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