2 * Definitions for using the Device functions in Bacula
3 * Tape and File storage access
9 Copyright (C) 2000-2004 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* #define NEW_LOCK 1 */
34 #define new_lock_device(dev) _new_lock_device(__FILE__, __LINE__, (dev))
35 #define new_lock_device_state(dev,state) _new_lock_device(__FILE__, __LINE__, (dev), (state))
36 #define new_unlock_device(dev) _new_unlock_device(__FILE__, __LINE__, (dev))
38 #define lock_device(d) _lock_device(__FILE__, __LINE__, (d))
39 #define unlock_device(d) _unlock_device(__FILE__, __LINE__, (d))
40 #define block_device(d, s) _block_device(__FILE__, __LINE__, (d), s)
41 #define unblock_device(d) _unblock_device(__FILE__, __LINE__, (d))
42 #define steal_device_lock(d, p, s) _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
43 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
45 /* Arguments to open_dev() */
52 /* Generic status bits returned from status_dev() */
53 #define BMT_TAPE (1<<0) /* is tape device */
54 #define BMT_EOF (1<<1) /* just read EOF */
55 #define BMT_BOT (1<<2) /* at beginning of tape */
56 #define BMT_EOT (1<<3) /* end of tape reached */
57 #define BMT_SM (1<<4) /* DDS setmark */
58 #define BMT_EOD (1<<5) /* DDS at end of data */
59 #define BMT_WR_PROT (1<<6) /* tape write protected */
60 #define BMT_ONLINE (1<<7) /* tape online */
61 #define BMT_DR_OPEN (1<<8) /* tape door open */
62 #define BMT_IM_REP_EN (1<<9) /* immediate report enabled */
65 /* Test capabilities */
66 #define dev_cap(dev, cap) ((dev)->capabilities & (cap))
68 /* Bits for device capabilities */
69 #define CAP_EOF (1<<0) /* has MTWEOF */
70 #define CAP_BSR (1<<1) /* has MTBSR */
71 #define CAP_BSF (1<<2) /* has MTBSF */
72 #define CAP_FSR (1<<3) /* has MTFSR */
73 #define CAP_FSF (1<<4) /* has MTFSF */
74 #define CAP_EOM (1<<5) /* has MTEOM */
75 #define CAP_REM (1<<6) /* is removable media */
76 #define CAP_RACCESS (1<<7) /* is random access device */
77 #define CAP_AUTOMOUNT (1<<8) /* Read device at start to see what is there */
78 #define CAP_LABEL (1<<9) /* Label blank tapes */
79 #define CAP_ANONVOLS (1<<10) /* Mount without knowing volume name */
80 #define CAP_ALWAYSOPEN (1<<11) /* always keep device open */
81 #define CAP_AUTOCHANGER (1<<12) /* AutoChanger */
82 #define CAP_OFFLINEUNMOUNT (1<<13) /* Offline before unmount */
83 #define CAP_STREAM (1<<14) /* Stream device */
84 #define CAP_BSFATEOM (1<<15) /* Backspace file at EOM */
85 #define CAP_FASTFSF (1<<16) /* Fast forward space file */
86 #define CAP_TWOEOF (1<<17) /* Write two eofs for EOM */
87 #define CAP_CLOSEONPOLL (1<<18) /* Close device on polling */
90 #define dev_state(dev, st_state) ((dev)->state & (st_state))
92 /* Device state bits */
93 #define ST_OPENED (1<<0) /* set when device opened */
94 #define ST_TAPE (1<<1) /* is a tape device */
95 #define ST_FILE (1<<2) /* is a file device */
96 #define ST_FIFO (1<<3) /* is a fifo device */
97 #define ST_PROG (1<<4) /* is a program device */
98 #define ST_LABEL (1<<5) /* label found */
99 #define ST_MALLOC (1<<6) /* dev packet malloc'ed in init_dev() */
100 #define ST_APPEND (1<<7) /* ready for Bacula append */
101 #define ST_READ (1<<8) /* ready for Bacula read */
102 #define ST_EOT (1<<9) /* at end of tape */
103 #define ST_WEOT (1<<10) /* Got EOT on write */
104 #define ST_EOF (1<<11) /* Read EOF i.e. zero bytes */
105 #define ST_NEXTVOL (1<<12) /* Start writing on next volume */
106 #define ST_SHORT (1<<13) /* Short block read */
108 /* dev_blocked states (mutually exclusive) */
110 BST_NOT_BLOCKED = 0, /* not blocked */
111 BST_UNMOUNTED, /* User unmounted device */
112 BST_WAITING_FOR_SYSOP, /* Waiting for operator to mount tape */
113 BST_DOING_ACQUIRE, /* Opening/validating/moving tape */
114 BST_WRITING_LABEL, /* Labeling a tape */
115 BST_UNMOUNTED_WAITING_FOR_SYSOP, /* Closed by user during mount request */
116 BST_MOUNT /* Mount request */
119 /* Volume Catalog Information structure definition */
120 struct VOLUME_CAT_INFO {
121 /* Media info for the current Volume */
122 uint32_t VolCatJobs; /* number of jobs on this Volume */
123 uint32_t VolCatFiles; /* Number of files */
124 uint32_t VolCatBlocks; /* Number of blocks */
125 uint64_t VolCatBytes; /* Number of bytes written */
126 uint32_t VolCatMounts; /* Number of mounts this volume */
127 uint32_t VolCatErrors; /* Number of errors this volume */
128 uint32_t VolCatWrites; /* Number of writes this volume */
129 uint32_t VolCatReads; /* Number of reads this volume */
130 uint64_t VolCatRBytes; /* Number of bytes read */
131 uint32_t VolCatRecycles; /* Number of recycles this volume */
132 int32_t Slot; /* Slot in changer */
133 bool InChanger; /* Set if vol in current magazine */
134 uint32_t VolCatMaxJobs; /* Maximum Jobs to write to volume */
135 uint32_t VolCatMaxFiles; /* Maximum files to write to volume */
136 uint64_t VolCatMaxBytes; /* Max bytes to write to volume */
137 uint64_t VolCatCapacityBytes; /* capacity estimate */
138 uint64_t VolReadTime; /* time spent reading */
139 uint64_t VolWriteTime; /* time spent writing this Volume */
140 char VolCatStatus[20]; /* Volume status */
141 char VolCatName[MAX_NAME_LENGTH]; /* Desired volume to mount */
145 typedef struct s_steal_lock {
146 pthread_t no_wait_id; /* id of no wait thread */
147 int dev_blocked; /* state */
148 int dev_prev_blocked; /* previous blocked state */
151 struct DEVRES; /* Device resource defined in stored_conf.h */
154 * Device structure definition. There is one of these for
155 * each physical device. Everything here is "global" to
156 * that device and effects all jobs using the device.
160 DEVICE *next; /* pointer to next open device */
161 DEVICE *prev; /* pointer to prev open device */
162 JCR *attached_jcrs; /* attached JCR list */
163 dlist *attached_dcrs; /* attached DCR list */
164 pthread_mutex_t mutex; /* access control */
165 pthread_mutex_t spool_mutex; /* mutex for updating spool_size */
166 pthread_cond_t wait; /* thread wait variable */
167 pthread_cond_t wait_next_vol; /* wait for tape to be mounted */
168 pthread_t no_wait_id; /* this thread must not wait */
169 int dev_blocked; /* set if we must wait (i.e. change tape) */
170 int dev_prev_blocked; /* previous blocked state */
171 int num_waiting; /* number of threads waiting */
172 int num_writers; /* number of writing threads */
173 int use_count; /* usage count on this device */
174 int fd; /* file descriptor */
175 int capabilities; /* capabilities mask */
176 int state; /* state mask */
177 int dev_errno; /* Our own errno */
178 int mode; /* read/write modes */
179 uint32_t drive_index; /* Autochanger drive index */
180 POOLMEM *dev_name; /* device name */
181 char *errmsg; /* nicely edited error message */
182 uint32_t block_num; /* current block number base 0 */
183 uint32_t file; /* current file number base 0 */
184 uint64_t file_addr; /* Current file read/write address */
185 uint64_t file_size; /* Current file size */
186 uint32_t EndBlock; /* last block written */
187 uint32_t EndFile; /* last file written */
188 uint32_t min_block_size; /* min block size */
189 uint32_t max_block_size; /* max block size */
190 uint64_t max_volume_size; /* max bytes to put on one volume */
191 uint64_t max_file_size; /* max file size to put in one file on volume */
192 uint64_t volume_capacity; /* advisory capacity */
193 uint64_t max_spool_size; /* maximum spool file size */
194 uint64_t spool_size; /* curren spool size */
195 uint32_t max_rewind_wait; /* max secs to allow for rewind */
196 uint32_t max_open_wait; /* max secs to allow for open */
197 uint32_t max_open_vols; /* max simultaneous open volumes */
198 utime_t vol_poll_interval; /* interval between polling Vol mount */
199 DEVRES *device; /* pointer to Device Resource */
200 btimer_t *tid; /* timer id */
202 VOLUME_CAT_INFO VolCatInfo; /* Volume Catalog Information */
203 VOLUME_LABEL VolHdr; /* Actual volume label */
205 /* Device wait times ***FIXME*** look at durations */
206 char BadVolName[MAX_NAME_LENGTH]; /* Last wrong Volume mounted */
207 bool poll; /* set to poll Volume */
217 * Device Context (or Control) Record.
218 * There is one of these records for each Job that is using
219 * the device. Items in this record are "local" to the Job and
220 * do not affect other Jobs.
223 dlink dev_link; /* link to attach to dev */
224 JCR *jcr; /* pointer to JCR */
225 DEVICE *dev; /* pointer to device */
226 DEV_BLOCK *block; /* pointer to block */
227 DEV_RECORD *record; /* pointer to record */
228 bool spool_data; /* set to spool data */
229 bool spooling; /* set when actually spooling */
230 bool dev_locked; /* set if dev already locked */
231 int spool_fd; /* fd if spooling */
232 bool NewVol; /* set if new Volume mounted */
233 bool WroteVol; /* set if Volume written */
234 bool NewFile; /* set when EOF written */
235 uint32_t VolFirstIndex; /* First file index this Volume */
236 uint32_t VolLastIndex; /* Last file index this Volume */
237 uint32_t FileIndex; /* Current File Index */
238 uint32_t EndFile; /* End file written */
239 uint32_t StartFile; /* Start write file */
240 uint32_t StartBlock; /* Start write block */
241 uint32_t EndBlock; /* Ending block written */
242 uint64_t spool_size; /* Current spool size */
243 uint64_t max_spool_size; /* Max job spool size */
244 char VolumeName[MAX_NAME_LENGTH]; /* Volume name */
245 VOLUME_CAT_INFO VolCatInfo; /* Catalog info for desired volume */
249 /* Get some definition of function to position
250 * to the end of the medium in MTEOM. System
251 * dependent. Arrgggg!