2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
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 General Public License for more details.
18 You should have received a copy of the GNU 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
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Definitions for using the Device functions in Bacula
30 * Tape and File storage access
42 #undef DCR /* used by Bacula */
44 /* Return values from wait_for_sysop() */
53 /* Arguments to open_dev() */
55 CREATE_READ_WRITE = 1,
70 /* Generic status bits returned from status_dev() */
71 #define BMT_TAPE (1<<0) /* is tape device */
72 #define BMT_EOF (1<<1) /* just read EOF */
73 #define BMT_BOT (1<<2) /* at beginning of tape */
74 #define BMT_EOT (1<<3) /* end of tape reached */
75 #define BMT_SM (1<<4) /* DDS setmark */
76 #define BMT_EOD (1<<5) /* DDS at end of data */
77 #define BMT_WR_PROT (1<<6) /* tape write protected */
78 #define BMT_ONLINE (1<<7) /* tape online */
79 #define BMT_DR_OPEN (1<<8) /* tape door open */
80 #define BMT_IM_REP_EN (1<<9) /* immediate report enabled */
83 /* Bits for device capabilities */
84 #define CAP_EOF (1<<0) /* has MTWEOF */
85 #define CAP_BSR (1<<1) /* has MTBSR */
86 #define CAP_BSF (1<<2) /* has MTBSF */
87 #define CAP_FSR (1<<3) /* has MTFSR */
88 #define CAP_FSF (1<<4) /* has MTFSF */
89 #define CAP_EOM (1<<5) /* has MTEOM */
90 #define CAP_REM (1<<6) /* is removable media */
91 #define CAP_RACCESS (1<<7) /* is random access device */
92 #define CAP_AUTOMOUNT (1<<8) /* Read device at start to see what is there */
93 #define CAP_LABEL (1<<9) /* Label blank tapes */
94 #define CAP_ANONVOLS (1<<10) /* Mount without knowing volume name */
95 #define CAP_ALWAYSOPEN (1<<11) /* always keep device open */
96 #define CAP_AUTOCHANGER (1<<12) /* AutoChanger */
97 #define CAP_OFFLINEUNMOUNT (1<<13) /* Offline before unmount */
98 #define CAP_STREAM (1<<14) /* Stream device */
99 #define CAP_BSFATEOM (1<<15) /* Backspace file at EOM */
100 #define CAP_FASTFSF (1<<16) /* Fast forward space file */
101 #define CAP_TWOEOF (1<<17) /* Write two eofs for EOM */
102 #define CAP_CLOSEONPOLL (1<<18) /* Close device on polling */
103 #define CAP_POSITIONBLOCKS (1<<19) /* Use block positioning */
104 #define CAP_MTIOCGET (1<<20) /* Basic support for fileno and blkno */
105 #define CAP_REQMOUNT (1<<21) /* Require mount to read files back (typically: DVD) */
106 #define CAP_CHECKLABELS (1<<22) /* Check for ANSI/IBM labels */
109 #define dev_state(dev, st_state) ((dev)->state & (st_state))
111 /* Device state bits */
112 #define ST_XXXXXX (1<<0) /* was ST_OPENED */
113 #define ST_XXXXX (1<<1) /* was ST_TAPE */
114 #define ST_XXXX (1<<2) /* was ST_FILE */
115 #define ST_XXX (1<<3) /* was ST_FIFO */
116 #define ST_XX (1<<4) /* was ST_DVD */
117 #define ST_X (1<<5) /* was ST_PROG */
119 #define ST_LABEL (1<<6) /* label found */
120 #define ST_MALLOC (1<<7) /* dev packet malloc'ed in init_dev() */
121 #define ST_APPEND (1<<8) /* ready for Bacula append */
122 #define ST_READ (1<<9) /* ready for Bacula read */
123 #define ST_EOT (1<<10) /* at end of tape */
124 #define ST_WEOT (1<<11) /* Got EOT on write */
125 #define ST_EOF (1<<12) /* Read EOF i.e. zero bytes */
126 #define ST_NEXTVOL (1<<13) /* Start writing on next volume */
127 #define ST_SHORT (1<<14) /* Short block read */
128 #define ST_MOUNTED (1<<15) /* the device is mounted to the mount point */
129 #define ST_MEDIA (1<<16) /* Media found in mounted device */
130 #define ST_OFFLINE (1<<17) /* set offline by operator */
131 #define ST_PART_SPOOLED (1<<18) /* spooling part */
132 #define ST_FREESPACE_OK (1<<19) /* Have valid freespace for DVD */
135 /* Volume Catalog Information structure definition */
136 struct VOLUME_CAT_INFO {
137 /* Media info for the current Volume */
138 uint32_t VolCatJobs; /* number of jobs on this Volume */
139 uint32_t VolCatFiles; /* Number of files */
140 uint32_t VolCatBlocks; /* Number of blocks */
141 uint64_t VolCatBytes; /* Number of bytes written */
142 uint32_t VolCatParts; /* Number of parts written */
143 uint32_t VolCatMounts; /* Number of mounts this volume */
144 uint32_t VolCatErrors; /* Number of errors this volume */
145 uint32_t VolCatWrites; /* Number of writes this volume */
146 uint32_t VolCatReads; /* Number of reads this volume */
147 uint64_t VolCatRBytes; /* Number of bytes read */
148 uint32_t VolCatRecycles; /* Number of recycles this volume */
149 uint32_t EndFile; /* Last file number */
150 uint32_t EndBlock; /* Last block number */
151 int32_t LabelType; /* Bacula/ANSI/IBM */
152 int32_t Slot; /* >0=Slot loaded, 0=nothing, -1=unknown */
153 uint32_t VolCatMaxJobs; /* Maximum Jobs to write to volume */
154 uint32_t VolCatMaxFiles; /* Maximum files to write to volume */
155 uint64_t VolCatMaxBytes; /* Max bytes to write to volume */
156 uint64_t VolCatCapacityBytes; /* capacity estimate */
157 btime_t VolReadTime; /* time spent reading */
158 btime_t VolWriteTime; /* time spent writing this Volume */
159 int64_t VolMediaId; /* MediaId */
160 utime_t VolFirstWritten; /* Time of first write */
161 utime_t VolLastWritten; /* Time of last write */
162 bool InChanger; /* Set if vol in current magazine */
163 char VolCatStatus[20]; /* Volume status */
164 char VolCatName[MAX_NAME_LENGTH]; /* Desired volume to mount */
168 class DEVRES; /* Device resource defined in stored_conf.h */
170 class DCR; /* forward reference */
171 class VOLRES; /* forward reference */
175 * Device structure definition. There is one of these for
176 * each physical device. Everything here is "global" to
177 * that device and effects all jobs using the device.
181 int m_fd; /* file descriptor */
182 int m_blocked; /* set if we must wait (i.e. change tape) */
183 int m_count; /* Mutex use count -- DEBUG only */
184 pthread_t m_pid; /* Thread that locked -- DEBUG only */
186 dlist *attached_dcrs; /* attached DCR list */
187 pthread_mutex_t m_mutex; /* access control */
188 pthread_mutex_t spool_mutex; /* mutex for updating spool_size */
189 pthread_cond_t wait; /* thread wait variable */
190 pthread_cond_t wait_next_vol; /* wait for tape to be mounted */
191 pthread_t no_wait_id; /* this thread must not wait */
192 int dev_prev_blocked; /* previous blocked state */
193 int num_waiting; /* number of threads waiting */
194 int num_writers; /* number of writing threads */
195 int reserved_device; /* number of device reservations */
196 int capabilities; /* capabilities mask */
197 int state; /* state mask */
198 int dev_errno; /* Our own errno */
199 int mode; /* read/write modes */
200 int openmode; /* parameter passed to open_dev (useful to reopen the device) */
201 int dev_type; /* device type */
202 bool autoselect; /* Autoselect in autochanger */
203 bool initiated; /* set when init_dev() called */
204 int label_type; /* Bacula/ANSI/IBM label types */
205 uint32_t drive_index; /* Autochanger drive index (base 0) */
206 int32_t Slot; /* Slot currently in drive (base 1) */
207 POOLMEM *dev_name; /* Physical device name */
208 POOLMEM *prt_name; /* Name used for display purposes */
209 char *errmsg; /* nicely edited error message */
210 uint32_t block_num; /* current block number base 0 */
211 uint32_t file; /* current file number base 0 */
212 uint64_t file_addr; /* Current file read/write address */
213 uint64_t file_size; /* Current file size */
214 uint32_t EndBlock; /* last block written */
215 uint32_t EndFile; /* last file written */
216 uint32_t min_block_size; /* min block size */
217 uint32_t max_block_size; /* max block size */
218 uint64_t max_volume_size; /* max bytes to put on one volume */
219 uint64_t max_file_size; /* max file size to put in one file on volume */
220 uint64_t volume_capacity; /* advisory capacity */
221 uint64_t max_spool_size; /* maximum spool file size */
222 uint64_t spool_size; /* current spool size for this device */
223 uint32_t max_rewind_wait; /* max secs to allow for rewind */
224 uint32_t max_open_wait; /* max secs to allow for open */
225 uint32_t max_open_vols; /* max simultaneous open volumes */
227 uint64_t max_part_size; /* max part size */
228 uint64_t part_size; /* current part size */
229 uint32_t part; /* current part number (starts at 0) */
230 uint64_t part_start; /* current part start address (relative to the whole volume) */
231 uint32_t num_dvd_parts; /* number of parts WRITTEN on the DVD */
232 /* state ST_FREESPACE_OK is set if free_space is valid */
233 uint64_t free_space; /* current free space on medium (without the current part) */
234 int free_space_errno; /* indicates errno getting freespace */
235 bool truncating; /* if set, we are currently truncating the DVD */
236 bool blank_dvd; /* if set, we have a blank DVD in the drive */
239 utime_t vol_poll_interval; /* interval between polling Vol mount */
240 DEVRES *device; /* pointer to Device Resource */
241 VOLRES *vol; /* Pointer to Volume reservation item */
242 btimer_t *tid; /* timer id */
244 VOLUME_CAT_INFO VolCatInfo; /* Volume Catalog Information */
245 VOLUME_LABEL VolHdr; /* Actual volume label */
246 char pool_name[MAX_NAME_LENGTH]; /* pool name */
247 char pool_type[MAX_NAME_LENGTH]; /* pool type */
249 /* Device wait times ***FIXME*** look at durations */
250 char BadVolName[MAX_NAME_LENGTH]; /* Last wrong Volume mounted */
251 bool poll; /* set to poll Volume */
259 btime_t last_timer; /* used by read/write/seek to get stats (usec) */
260 btime_t last_tick; /* contains last read/write time (usec) */
263 btime_t DevWriteTime;
264 uint64_t DevWriteBytes;
265 uint64_t DevReadBytes;
268 btime_t get_timer_count(); /* return the last timer interval (ms) */
270 int has_cap(int cap) const { return capabilities & cap; }
271 void clear_cap(int cap) { capabilities &= ~cap; }
272 void set_cap(int cap) { capabilities |= cap; }
273 int is_autochanger() const { return capabilities & CAP_AUTOCHANGER; }
274 int requires_mount() const { return capabilities & CAP_REQMOUNT; }
275 int is_removable() const { return capabilities & CAP_REM; }
276 int is_tape() const { return dev_type == B_TAPE_DEV; }
277 int is_file() const { return dev_type == B_FILE_DEV; }
278 int is_fifo() const { return dev_type == B_FIFO_DEV; }
279 int is_dvd() const { return dev_type == B_DVD_DEV; }
280 int is_prog() const { return dev_type == B_PROG_DEV; }
281 int is_open() const { return m_fd >= 0; }
282 int is_offline() const { return state & ST_OFFLINE; }
283 int is_labeled() const { return state & ST_LABEL; }
284 int is_mounted() const { return state & ST_MOUNTED; }
285 int is_unmountable() const { return (is_dvd() || (is_file() && is_removable())); }
286 int is_part_spooled() const { return state & ST_PART_SPOOLED; }
287 int have_media() const { return state & ST_MEDIA; }
288 int is_short_block() const { return state & ST_SHORT; }
289 int is_busy() const { return (state & ST_READ) || num_writers || reserved_device; }
290 int at_eof() const { return state & ST_EOF; }
291 int at_eot() const { return state & ST_EOT; }
292 int at_weot() const { return state & ST_WEOT; }
293 int can_append() const { return state & ST_APPEND; }
294 int is_freespace_ok() const { return state & ST_FREESPACE_OK; }
296 * can_write() is meant for checking at the end of a job to see
297 * if we still have a tape (perhaps not if at end of tape
298 * and the job is canceled).
300 int can_write() const { return is_open() && can_append() &&
301 is_labeled() && !at_weot(); }
302 int can_read() const { return state & ST_READ; }
303 bool can_steal_lock() const { return m_blocked &&
304 (m_blocked == BST_UNMOUNTED ||
305 m_blocked == BST_WAITING_FOR_SYSOP ||
306 m_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
307 bool waiting_for_mount() const { return
308 (m_blocked == BST_UNMOUNTED ||
309 m_blocked == BST_WAITING_FOR_SYSOP ||
310 m_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
311 const char *strerror() const;
312 const char *archive_name() const;
313 const char *name() const;
314 const char *print_name() const; /* Name for display purposes */
315 void set_ateof(); /* in dev.c */
316 void set_ateot(); /* in dev.c */
317 void set_eot() { state |= ST_EOT; };
318 void set_eof() { state |= ST_EOF; };
319 void set_append() { state |= ST_APPEND; };
320 void set_labeled() { state |= ST_LABEL; };
321 inline void set_read() { state |= ST_READ; };
322 void set_offline() { state |= ST_OFFLINE; };
323 void set_mounted() { state |= ST_MOUNTED; };
324 void set_media() { state |= ST_MEDIA; };
325 void set_short_block() { state |= ST_SHORT; };
326 void set_freespace_ok() { state |= ST_FREESPACE_OK; }
327 void set_part_spooled(int val) { if (val) state |= ST_PART_SPOOLED; \
328 else state &= ~ST_PART_SPOOLED;
330 void set_mounted(int val) { if (val) state |= ST_MOUNTED; \
331 else state &= ~ST_MOUNTED; };
332 void clear_append() { state &= ~ST_APPEND; };
333 void clear_read() { state &= ~ST_READ; };
334 void clear_labeled() { state &= ~ST_LABEL; };
335 void clear_offline() { state &= ~ST_OFFLINE; };
336 void clear_eot() { state &= ~ST_EOT; };
337 void clear_eof() { state &= ~ST_EOF; };
338 void clear_opened() { m_fd = -1; };
339 void clear_mounted() { state &= ~ST_MOUNTED; };
340 void clear_media() { state &= ~ST_MEDIA; };
341 void clear_short_block() { state &= ~ST_SHORT; };
342 void clear_freespace_ok() { state &= ~ST_FREESPACE_OK; };
343 char *bstrerror(void) { return errmsg; };
344 char *print_errmsg() { return errmsg; };
347 void clear_volhdr(); /* in dev.c */
348 void close(); /* in dev.c */
349 void close_part(DCR *dcr); /* in dev.c */
350 bool truncate(DCR *dcr); /* in dev.c */
351 int open(DCR *dcr, int mode); /* in dev.c */
352 void term(void); /* in dev.c */
353 ssize_t read(void *buf, size_t len); /* in dev.c */
354 ssize_t write(const void *buf, size_t len); /* in dev.c */
355 bool rewind(DCR *dcr); /* in dev.c */
356 bool mount(int timeout); /* in dev.c */
357 bool unmount(int timeout); /* in dev.c */
358 void edit_mount_codes(POOL_MEM &omsg, const char *imsg); /* in dev.c */
359 bool offline_or_rewind(); /* in dev.c */
360 bool offline(); /* in dev.c */
361 bool bsf(int count); /* in dev.c */
362 bool eod(DCR *dcr); /* in dev.c */
363 bool fsr(int num); /* in dev.c */
364 bool fsf(int num); /* in dev.c */
365 bool bsr(int num); /* in dev.c */
366 bool weof(int num); /* in dev.c */
367 void lock_door(); /* in dev.c */
368 void unlock_door(); /* in dev.c */
369 int32_t get_os_tape_file(); /* in dev.c */
370 bool scan_dir_for_volume(DCR *dcr); /* in scan.c */
371 bool reposition(DCR *dcr, uint32_t rfile, uint32_t rblock); /* in dev.c */
372 void clrerror(int func); /* in dev.c */
373 boffset_t lseek(DCR *dcr, boffset_t offset, int whence); /* in dev.c */
374 bool update_pos(DCR *dcr); /* in dev.c */
375 bool update_freespace(); /* in dvd.c */
377 uint32_t get_file() const { return file; };
378 uint32_t get_block_num() const { return block_num; };
379 int fd() const { return m_fd; };
382 * Locking and blocking calls
385 void _r_dlock(const char *, int); /* in lock.c */
386 void _r_dunlock(const char *, int); /* in lock.c */
387 void _dlock(const char *, int); /* in lock.c */
388 void _dunlock(const char *, int); /* in lock.c */
390 void r_dlock(); /* in lock.c */
391 void r_dunlock() { dunlock(); }
392 void dlock() { P(m_mutex); }
393 void dunlock() { V(m_mutex); }
395 void dblock(int why); /* in lock.c */
396 void dunblock(bool locked=false); /* in lock.c */
397 void set_blocked(int block) { m_blocked = block; };
398 int blocked() const { return m_blocked; };
399 bool is_blocked() const { return m_blocked != BST_NOT_BLOCKED; };
400 const char *print_blocked() const; /* in dev.c */
403 bool do_mount(int mount, int timeout); /* in dev.c */
404 void set_mode(int omode); /* in dev.c */
405 void open_tape_device(DCR *dcr, int omode); /* in dev.c */
406 void open_file_device(DCR *dcr, int omode); /* in dev.c */
407 void open_dvd_device(DCR *dcr, int omode); /* in dev.c */
410 inline const char *DEVICE::strerror() const { return errmsg; }
411 inline const char *DEVICE::archive_name() const { return dev_name; }
412 inline const char *DEVICE::print_name() const { return prt_name; }
415 * Device Context (or Control) Record.
416 * There is one of these records for each Job that is using
417 * the device. Items in this record are "local" to the Job and
418 * do not affect other Jobs. Note, a job can have multiple
419 * DCRs open, each pointing to a different device.
423 dlink dev_link; /* link to attach to dev */
424 JCR *jcr; /* pointer to JCR */
425 DEVICE *dev; /* pointer to device */
426 DEVRES *device; /* pointer to device resource */
427 DEV_BLOCK *block; /* pointer to block */
428 DEV_RECORD *rec; /* pointer to record */
429 pthread_t tid; /* Thread running this dcr */
430 int spool_fd; /* fd if spooling */
431 bool spool_data; /* set to spool data */
432 bool spooling; /* set when actually spooling */
433 bool despooling; /* set when despooling */
434 bool despool_wait; /* waiting for despooling */
435 bool m_dev_locked; /* set if dev already locked */
436 bool NewVol; /* set if new Volume mounted */
437 bool WroteVol; /* set if Volume written */
438 bool NewFile; /* set when EOF written */
439 bool reserved_device; /* set if reserve done */
440 bool any_volume; /* Any OK for dir_find_next... */
441 bool attached_to_dev; /* set when attached to dev */
442 bool volume_in_use; /* set in dir_find_next_appendable_volume() */
443 bool keep_dcr; /* do not free dcr in release_dcr */
444 uint32_t VolFirstIndex; /* First file index this Volume */
445 uint32_t VolLastIndex; /* Last file index this Volume */
446 uint32_t FileIndex; /* Current File Index */
447 uint32_t EndFile; /* End file written */
448 uint32_t StartFile; /* Start write file */
449 uint32_t StartBlock; /* Start write block */
450 uint32_t EndBlock; /* Ending block written */
451 int64_t VolMediaId; /* MediaId */
452 int64_t job_spool_size; /* Current job spool size */
453 int64_t max_job_spool_size; /* Max job spool size */
454 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
455 char pool_name[MAX_NAME_LENGTH]; /* pool name */
456 char pool_type[MAX_NAME_LENGTH]; /* pool type */
457 char media_type[MAX_NAME_LENGTH]; /* media type */
458 char dev_name[MAX_NAME_LENGTH]; /* dev name */
459 int Copy; /* identical copy number */
460 int Stripe; /* RAIT stripe */
461 VOLUME_CAT_INFO VolCatInfo; /* Catalog info for desired volume */
464 bool is_dev_locked() { return m_dev_locked; }
465 void dlock() { dev->dlock(); m_dev_locked = true; }
466 void dunlock() { dev->dunlock(); m_dev_locked = false;}
467 void dblock(int why) { dev->dblock(why); }
472 * Volume reservation class -- see reserve.c
477 char *vol_name; /* Volume name */
478 DEVICE *dev; /* Pointer to device to which we are attached */
479 bool released; /* set when the Volume can be released */
483 /* Get some definition of function to position
484 * to the end of the medium in MTEOM. System
485 * dependent. Arrgggg!