2 * Definitions for using the Device functions in Bacula
3 * Tape and File storage access
11 Bacula® - The Network Backup Solution
13 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
15 The main author of Bacula is Kern Sibbald, with contributions from
16 many others, a complete list can be found in the file AUTHORS.
17 This program is Free Software; you can redistribute it and/or
18 modify it under the terms of version two of the GNU General Public
19 License as published by the Free Software Foundation plus additions
20 that are listed in the file LICENSE.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 Bacula® is a registered trademark of John Walker.
33 The licensor of Bacula is the Free Software Foundation Europe
34 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35 Switzerland, email:ftf@fsfeurope.org.
42 #undef DCR /* used by Bacula */
44 /* #define NEW_LOCK 1 */
46 #define new_lock_device(dev) _new_lock_device(__FILE__, __LINE__, (dev))
47 #define new_lock_device_state(dev,state) _new_lock_device(__FILE__, __LINE__, (dev), (state))
48 #define new_unlock_device(dev) _new_unlock_device(__FILE__, __LINE__, (dev))
50 #define lock_device(d) _lock_device(__FILE__, __LINE__, (d))
51 #define unlock_device(d) _unlock_device(__FILE__, __LINE__, (d))
52 #define block_device(d, s) _block_device(__FILE__, __LINE__, (d), s)
53 #define unblock_device(d) _unblock_device(__FILE__, __LINE__, (d))
54 #define steal_device_lock(d, p, s) _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
55 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
57 /* Return values from wait_for_sysop() */
66 /* Arguments to open_dev() */
68 CREATE_READ_WRITE = 1,
83 /* Generic status bits returned from status_dev() */
84 #define BMT_TAPE (1<<0) /* is tape device */
85 #define BMT_EOF (1<<1) /* just read EOF */
86 #define BMT_BOT (1<<2) /* at beginning of tape */
87 #define BMT_EOT (1<<3) /* end of tape reached */
88 #define BMT_SM (1<<4) /* DDS setmark */
89 #define BMT_EOD (1<<5) /* DDS at end of data */
90 #define BMT_WR_PROT (1<<6) /* tape write protected */
91 #define BMT_ONLINE (1<<7) /* tape online */
92 #define BMT_DR_OPEN (1<<8) /* tape door open */
93 #define BMT_IM_REP_EN (1<<9) /* immediate report enabled */
96 /* Bits for device capabilities */
97 #define CAP_EOF (1<<0) /* has MTWEOF */
98 #define CAP_BSR (1<<1) /* has MTBSR */
99 #define CAP_BSF (1<<2) /* has MTBSF */
100 #define CAP_FSR (1<<3) /* has MTFSR */
101 #define CAP_FSF (1<<4) /* has MTFSF */
102 #define CAP_EOM (1<<5) /* has MTEOM */
103 #define CAP_REM (1<<6) /* is removable media */
104 #define CAP_RACCESS (1<<7) /* is random access device */
105 #define CAP_AUTOMOUNT (1<<8) /* Read device at start to see what is there */
106 #define CAP_LABEL (1<<9) /* Label blank tapes */
107 #define CAP_ANONVOLS (1<<10) /* Mount without knowing volume name */
108 #define CAP_ALWAYSOPEN (1<<11) /* always keep device open */
109 #define CAP_AUTOCHANGER (1<<12) /* AutoChanger */
110 #define CAP_OFFLINEUNMOUNT (1<<13) /* Offline before unmount */
111 #define CAP_STREAM (1<<14) /* Stream device */
112 #define CAP_BSFATEOM (1<<15) /* Backspace file at EOM */
113 #define CAP_FASTFSF (1<<16) /* Fast forward space file */
114 #define CAP_TWOEOF (1<<17) /* Write two eofs for EOM */
115 #define CAP_CLOSEONPOLL (1<<18) /* Close device on polling */
116 #define CAP_POSITIONBLOCKS (1<<19) /* Use block positioning */
117 #define CAP_MTIOCGET (1<<20) /* Basic support for fileno and blkno */
118 #define CAP_REQMOUNT (1<<21) /* Require mount to read files back (typically: DVD) */
119 #define CAP_CHECKLABELS (1<<22) /* Check for ANSI/IBM labels */
122 #define dev_state(dev, st_state) ((dev)->state & (st_state))
124 /* Device state bits */
125 #define ST_XXXXXX (1<<0) /* was ST_OPENED */
126 #define ST_XXXXX (1<<1) /* was ST_TAPE */
127 #define ST_XXXX (1<<2) /* was ST_FILE */
128 #define ST_XXX (1<<3) /* was ST_FIFO */
129 #define ST_XX (1<<4) /* was ST_DVD */
130 #define ST_X (1<<5) /* was ST_PROG */
132 #define ST_LABEL (1<<6) /* label found */
133 #define ST_MALLOC (1<<7) /* dev packet malloc'ed in init_dev() */
134 #define ST_APPEND (1<<8) /* ready for Bacula append */
135 #define ST_READ (1<<9) /* ready for Bacula read */
136 #define ST_EOT (1<<10) /* at end of tape */
137 #define ST_WEOT (1<<11) /* Got EOT on write */
138 #define ST_EOF (1<<12) /* Read EOF i.e. zero bytes */
139 #define ST_NEXTVOL (1<<13) /* Start writing on next volume */
140 #define ST_SHORT (1<<14) /* Short block read */
141 #define ST_MOUNTED (1<<15) /* the device is mounted to the mount point */
142 #define ST_MEDIA (1<<16) /* Media found in mounted device */
143 #define ST_OFFLINE (1<<17) /* set offline by operator */
144 #define ST_PART_SPOOLED (1<<18) /* spooling part */
145 #define ST_FREESPACE_OK (1<<19) /* Have valid freespace for DVD */
147 /* dev_blocked states (mutually exclusive) */
149 BST_NOT_BLOCKED = 0, /* not blocked */
150 BST_UNMOUNTED, /* User unmounted device */
151 BST_WAITING_FOR_SYSOP, /* Waiting for operator to mount tape */
152 BST_DOING_ACQUIRE, /* Opening/validating/moving tape */
153 BST_WRITING_LABEL, /* Labeling a tape */
154 BST_UNMOUNTED_WAITING_FOR_SYSOP, /* User unmounted during wait for op */
155 BST_MOUNT /* Mount request */
158 /* Volume Catalog Information structure definition */
159 struct VOLUME_CAT_INFO {
160 /* Media info for the current Volume */
161 uint32_t VolCatJobs; /* number of jobs on this Volume */
162 uint32_t VolCatFiles; /* Number of files */
163 uint32_t VolCatBlocks; /* Number of blocks */
164 uint64_t VolCatBytes; /* Number of bytes written */
165 uint32_t VolCatParts; /* Number of parts written */
166 uint32_t VolCatMounts; /* Number of mounts this volume */
167 uint32_t VolCatErrors; /* Number of errors this volume */
168 uint32_t VolCatWrites; /* Number of writes this volume */
169 uint32_t VolCatReads; /* Number of reads this volume */
170 uint64_t VolCatRBytes; /* Number of bytes read */
171 uint32_t VolCatRecycles; /* Number of recycles this volume */
172 uint32_t EndFile; /* Last file number */
173 uint32_t EndBlock; /* Last block number */
174 int32_t LabelType; /* Bacula/ANSI/IBM */
175 int32_t Slot; /* >0=Slot loaded, 0=nothing, -1=unknown */
176 uint32_t VolCatMaxJobs; /* Maximum Jobs to write to volume */
177 uint32_t VolCatMaxFiles; /* Maximum files to write to volume */
178 uint64_t VolCatMaxBytes; /* Max bytes to write to volume */
179 uint64_t VolCatCapacityBytes; /* capacity estimate */
180 uint64_t VolReadTime; /* time spent reading */
181 uint64_t VolWriteTime; /* time spent writing this Volume */
182 int64_t VolMediaId; /* MediaId */
183 utime_t VolFirstWritten; /* Time of first write */
184 bool InChanger; /* Set if vol in current magazine */
185 char VolCatStatus[20]; /* Volume status */
186 char VolCatName[MAX_NAME_LENGTH]; /* Desired volume to mount */
190 typedef struct s_steal_lock {
191 pthread_t no_wait_id; /* id of no wait thread */
192 int dev_blocked; /* state */
193 int dev_prev_blocked; /* previous blocked state */
196 class DEVRES; /* Device resource defined in stored_conf.h */
198 class DCR; /* forward reference */
200 * Device structure definition. There is one of these for
201 * each physical device. Everything here is "global" to
202 * that device and effects all jobs using the device.
206 dlist *attached_dcrs; /* attached DCR list */
207 pthread_mutex_t mutex; /* access control */
208 pthread_mutex_t spool_mutex; /* mutex for updating spool_size */
209 pthread_cond_t wait; /* thread wait variable */
210 pthread_cond_t wait_next_vol; /* wait for tape to be mounted */
211 pthread_t no_wait_id; /* this thread must not wait */
212 int dev_blocked; /* set if we must wait (i.e. change tape) */
213 int dev_prev_blocked; /* previous blocked state */
214 int num_waiting; /* number of threads waiting */
215 int num_writers; /* number of writing threads */
216 int reserved_device; /* number of device reservations */
218 /* New access control in process of being implemented */
219 brwlock_t lock; /* New mutual exclusion lock */
221 int fd; /* file descriptor */
222 int capabilities; /* capabilities mask */
223 int state; /* state mask */
224 int dev_errno; /* Our own errno */
225 int mode; /* read/write modes */
226 int openmode; /* parameter passed to open_dev (useful to reopen the device) */
227 int dev_type; /* device type */
228 bool autoselect; /* Autoselect in autochanger */
229 int label_type; /* Bacula/ANSI/IBM label types */
230 uint32_t drive_index; /* Autochanger drive index (base 0) */
231 int32_t Slot; /* Slot currently in drive (base 1) */
232 POOLMEM *dev_name; /* Physical device name */
233 POOLMEM *prt_name; /* Name used for display purposes */
234 char *errmsg; /* nicely edited error message */
235 uint32_t block_num; /* current block number base 0 */
236 uint32_t file; /* current file number base 0 */
237 uint64_t file_addr; /* Current file read/write address */
238 uint64_t file_size; /* Current file size */
239 uint32_t EndBlock; /* last block written */
240 uint32_t EndFile; /* last file written */
241 uint32_t min_block_size; /* min block size */
242 uint32_t max_block_size; /* max block size */
243 uint64_t max_volume_size; /* max bytes to put on one volume */
244 uint64_t max_file_size; /* max file size to put in one file on volume */
245 uint64_t volume_capacity; /* advisory capacity */
246 uint64_t max_spool_size; /* maximum spool file size */
247 uint64_t spool_size; /* current spool size for this device */
248 uint32_t max_rewind_wait; /* max secs to allow for rewind */
249 uint32_t max_open_wait; /* max secs to allow for open */
250 uint32_t max_open_vols; /* max simultaneous open volumes */
252 uint64_t max_part_size; /* max part size */
253 uint64_t part_size; /* current part size */
254 uint32_t part; /* current part number (starts at 0) */
255 uint64_t part_start; /* current part start address (relative to the whole volume) */
256 uint32_t num_dvd_parts; /* number of parts WRITTEN on the DVD */
257 /* state ST_FREESPACE_OK is set if free_space is valid */
258 uint64_t free_space; /* current free space on medium (without the current part) */
259 int free_space_errno; /* indicates errno getting freespace */
260 bool truncating; /* if set, we are currently truncating the DVD */
261 bool blank_dvd; /* if set, we have a blank DVD in the drive */
264 utime_t vol_poll_interval; /* interval between polling Vol mount */
265 DEVRES *device; /* pointer to Device Resource */
266 btimer_t *tid; /* timer id */
268 VOLUME_CAT_INFO VolCatInfo; /* Volume Catalog Information */
269 VOLUME_LABEL VolHdr; /* Actual volume label */
270 char pool_name[MAX_NAME_LENGTH]; /* pool name */
271 char pool_type[MAX_NAME_LENGTH]; /* pool type */
273 /* Device wait times ***FIXME*** look at durations */
274 char BadVolName[MAX_NAME_LENGTH]; /* Last wrong Volume mounted */
275 bool poll; /* set to poll Volume */
283 uint64_t last_timer; /* used by read/write/seek to get stats (usec) */
284 int last_tick; /* contains last read/write time (usec) */
286 uint64_t DevReadTime;
287 uint64_t DevWriteTime;
288 uint64_t DevWriteBytes;
289 uint64_t DevReadBytes;
292 int get_timer_count(); /* return the last timer interval (ms) */
294 int has_cap(int cap) const { return capabilities & cap; }
295 void clear_cap(int cap) { capabilities &= ~cap; }
296 void set_cap(int cap) { capabilities |= cap; }
297 int is_autochanger() const { return capabilities & CAP_AUTOCHANGER; }
298 int requires_mount() const { return capabilities & CAP_REQMOUNT; }
299 int is_removable() const { return capabilities & CAP_REM; }
300 int is_tape() const { return dev_type == B_TAPE_DEV; }
301 int is_file() const { return dev_type == B_FILE_DEV; }
302 int is_fifo() const { return dev_type == B_FIFO_DEV; }
303 int is_dvd() const { return dev_type == B_DVD_DEV; }
304 int is_prog() const { return dev_type == B_PROG_DEV; }
305 int is_open() const { return fd >= 0; }
306 int is_offline() const { return state & ST_OFFLINE; }
307 int is_labeled() const { return state & ST_LABEL; }
308 int is_mounted() const { return state & ST_MOUNTED; }
309 int is_part_spooled() const { return state & ST_PART_SPOOLED; }
310 int have_media() const { return state & ST_MEDIA; }
311 int is_short_block() const { return state & ST_SHORT; }
312 int is_busy() const { return (state & ST_READ) || num_writers || reserved_device; }
313 int at_eof() const { return state & ST_EOF; }
314 int at_eot() const { return state & ST_EOT; }
315 int at_weot() const { return state & ST_WEOT; }
316 int can_append() const { return state & ST_APPEND; }
317 int is_freespace_ok() const { return state & ST_FREESPACE_OK; }
319 * can_write() is meant for checking at the end of a job to see
320 * if we still have a tape (perhaps not if at end of tape
321 * and the job is canceled).
323 int can_write() const { return is_open() && can_append() &&
324 is_labeled() && !at_weot(); }
325 int can_read() const { return state & ST_READ; }
326 bool can_steal_lock() const { return dev_blocked &&
327 (dev_blocked == BST_UNMOUNTED ||
328 dev_blocked == BST_WAITING_FOR_SYSOP ||
329 dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
330 bool waiting_for_mount() const { return
331 (dev_blocked == BST_UNMOUNTED ||
332 dev_blocked == BST_WAITING_FOR_SYSOP ||
333 dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
334 const char *strerror() const;
335 const char *archive_name() const;
336 const char *name() const;
337 const char *print_name() const; /* Name for display purposes */
338 void set_ateof(); /* in dev.c */
339 void set_ateot(); /* in dev.c */
340 void set_eot() { state |= ST_EOT; };
341 void set_eof() { state |= ST_EOF; };
342 void set_append() { state |= ST_APPEND; };
343 void set_labeled() { state |= ST_LABEL; };
344 inline void set_read() { state |= ST_READ; };
345 void set_offline() { state |= ST_OFFLINE; };
346 void set_mounted() { state |= ST_MOUNTED; };
347 void set_media() { state |= ST_MEDIA; };
348 void set_short_block() { state |= ST_SHORT; };
349 void set_freespace_ok() { state |= ST_FREESPACE_OK; }
350 void set_part_spooled(int val) { if (val) state |= ST_PART_SPOOLED; \
351 else state &= ~ST_PART_SPOOLED;
353 void set_mounted(int val) { if (val) state |= ST_MOUNTED; \
354 else state &= ~ST_MOUNTED; };
355 void clear_append() { state &= ~ST_APPEND; };
356 void clear_read() { state &= ~ST_READ; };
357 void clear_labeled() { state &= ~ST_LABEL; };
358 void clear_offline() { state &= ~ST_OFFLINE; };
359 void clear_eot() { state &= ~ST_EOT; };
360 void clear_eof() { state &= ~ST_EOF; };
361 void clear_opened() { fd = -1; };
362 void clear_mounted() { state &= ~ST_MOUNTED; };
363 void clear_media() { state &= ~ST_MEDIA; };
364 void clear_short_block() { state &= ~ST_SHORT; };
365 void clear_freespace_ok() { state &= ~ST_FREESPACE_OK; };
366 char *bstrerror(void) { return errmsg; };
367 char *print_errmsg() { return errmsg; };
369 void block(int why); /* in dev.c */
370 void unblock(); /* in dev.c */
371 void close(); /* in dev.c */
372 void close_part(DCR *dcr); /* in dev.c */
373 bool truncate(DCR *dcr); /* in dev.c */
374 int open(DCR *dcr, int mode); /* in dev.c */
375 void term(void); /* in dev.c */
376 ssize_t read(void *buf, size_t len); /* in dev.c */
377 ssize_t write(const void *buf, size_t len); /* in dev.c */
378 bool rewind(DCR *dcr); /* in dev.c */
379 bool mount(int timeout); /* in dev.c */
380 bool unmount(int timeout); /* in dev.c */
381 void edit_mount_codes(POOL_MEM &omsg, const char *imsg); /* in dev.c */
382 bool offline_or_rewind(); /* in dev.c */
383 bool offline(); /* in dev.c */
384 bool bsf(int count); /* in dev.c */
385 bool eod(DCR *dcr); /* in dev.c */
386 bool fsr(int num); /* in dev.c */
387 bool fsf(int num); /* in dev.c */
388 bool bsr(int num); /* in dev.c */
389 bool weof(int num); /* in dev.c */
390 void lock_door(); /* in dev.c */
391 void unlock_door(); /* in dev.c */
392 bool scan_dir_for_volume(DCR *dcr); /* in scan.c */
393 bool reposition(DCR *dcr, uint32_t rfile, uint32_t rblock); /* in dev.c */
394 void clrerror(int func); /* in dev.c */
395 boffset_t lseek(DCR *dcr, boffset_t offset, int whence); /* in dev.c */
396 bool update_pos(DCR *dcr); /* in dev.c */
397 bool update_freespace(); /* in dvd.c */
399 void set_blocked(int block) { dev_blocked = block; };
400 int get_blocked() const { return dev_blocked; };
401 uint32_t get_file() const { return file; };
402 uint32_t get_block() const { return block_num; };
403 const char *print_blocked() const; /* in dev.c */
404 bool is_blocked() const { return dev_blocked != BST_NOT_BLOCKED; };
407 bool do_mount(int mount, int timeout); /* in dev.c */
408 void set_mode(int omode); /* in dev.c */
409 void open_tape_device(DCR *dcr, int omode); /* in dev.c */
410 void open_file_device(DCR *dcr, int omode); /* in dev.c */
411 void open_dvd_device(DCR *dcr, int omode); /* in dev.c */
414 /* Note, these return int not bool! */
415 inline const char *DEVICE::strerror() const { return errmsg; }
416 inline const char *DEVICE::archive_name() const { return dev_name; }
417 inline const char *DEVICE::print_name() const { return prt_name; }
420 * Device Context (or Control) Record.
421 * There is one of these records for each Job that is using
422 * the device. Items in this record are "local" to the Job and
423 * do not affect other Jobs. Note, a job can have multiple
424 * DCRs open, each pointing to a different device.
428 dlink dev_link; /* link to attach to dev */
429 JCR *jcr; /* pointer to JCR */
430 DEVICE *dev; /* pointer to device */
431 DEVRES *device; /* pointer to device resource */
432 DEV_BLOCK *block; /* pointer to block */
433 DEV_RECORD *rec; /* pointer to record */
434 int spool_fd; /* fd if spooling */
435 bool spool_data; /* set to spool data */
436 bool spooling; /* set when actually spooling */
437 bool despooling; /* set when despooling */
438 bool despool_wait; /* waiting for despooling */
439 bool dev_locked; /* set if dev already locked */
440 bool NewVol; /* set if new Volume mounted */
441 bool WroteVol; /* set if Volume written */
442 bool NewFile; /* set when EOF written */
443 bool reserved_device; /* set if reserve done */
444 bool any_volume; /* Any OK for dir_find_next... */
445 bool attached_to_dev; /* set when attached to dev */
446 uint32_t VolFirstIndex; /* First file index this Volume */
447 uint32_t VolLastIndex; /* Last file index this Volume */
448 uint32_t FileIndex; /* Current File Index */
449 uint32_t EndFile; /* End file written */
450 uint32_t StartFile; /* Start write file */
451 uint32_t StartBlock; /* Start write block */
452 uint32_t EndBlock; /* Ending block written */
453 int64_t job_spool_size; /* Current job spool size */
454 int64_t max_job_spool_size; /* Max job spool size */
455 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
456 char pool_name[MAX_NAME_LENGTH]; /* pool name */
457 char pool_type[MAX_NAME_LENGTH]; /* pool type */
458 char media_type[MAX_NAME_LENGTH]; /* media type */
459 char dev_name[MAX_NAME_LENGTH]; /* dev name */
460 int Copy; /* identical copy number */
461 int Stripe; /* RAIT stripe */
462 VOLUME_CAT_INFO VolCatInfo; /* Catalog info for desired volume */
466 * Volume reservation class -- see reserve.c
477 /* Get some definition of function to position
478 * to the end of the medium in MTEOM. System
479 * dependent. Arrgggg!