]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.h
- Corrected some typos in the make_xxx_tables.in files.
[bacula/bacula] / bacula / src / stored / dev.h
1 /*
2  * Definitions for using the Device functions in Bacula
3  *  Tape and File storage access
4  *
5  * Kern Sibbald, MM
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2000-2005 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30
31 #ifndef __DEV_H
32 #define __DEV_H 1
33
34 #undef DCR                            /* used by Bacula */
35
36 /* #define NEW_LOCK 1 */
37
38 #define new_lock_device(dev)             _new_lock_device(__FILE__, __LINE__, (dev))
39 #define new_lock_device_state(dev,state) _new_lock_device(__FILE__, __LINE__, (dev), (state))
40 #define new_unlock_device(dev)           _new_unlock_device(__FILE__, __LINE__, (dev))
41
42 #define lock_device(d) _lock_device(__FILE__, __LINE__, (d))
43 #define unlock_device(d) _unlock_device(__FILE__, __LINE__, (d))
44 #define block_device(d, s) _block_device(__FILE__, __LINE__, (d), s)
45 #define unblock_device(d) _unblock_device(__FILE__, __LINE__, (d))
46 #define steal_device_lock(d, p, s) _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
47 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
48
49 /* Arguments to open_dev() */
50 enum {
51    OPEN_READ_WRITE = 0,
52    OPEN_READ_ONLY,
53    OPEN_WRITE_ONLY
54 };
55
56 /* Generic status bits returned from status_dev() */
57 #define BMT_TAPE           (1<<0)     /* is tape device */
58 #define BMT_EOF            (1<<1)     /* just read EOF */
59 #define BMT_BOT            (1<<2)     /* at beginning of tape */
60 #define BMT_EOT            (1<<3)     /* end of tape reached */
61 #define BMT_SM             (1<<4)     /* DDS setmark */
62 #define BMT_EOD            (1<<5)     /* DDS at end of data */
63 #define BMT_WR_PROT        (1<<6)     /* tape write protected */
64 #define BMT_ONLINE         (1<<7)     /* tape online */
65 #define BMT_DR_OPEN        (1<<8)     /* tape door open */
66 #define BMT_IM_REP_EN      (1<<9)     /* immediate report enabled */
67
68
69 /* Test capabilities */
70 #define dev_cap(dev, cap) ((dev)->capabilities & (cap))
71
72 /* Bits for device capabilities */
73 #define CAP_EOF            (1<<0)     /* has MTWEOF */
74 #define CAP_BSR            (1<<1)     /* has MTBSR */
75 #define CAP_BSF            (1<<2)     /* has MTBSF */
76 #define CAP_FSR            (1<<3)     /* has MTFSR */
77 #define CAP_FSF            (1<<4)     /* has MTFSF */
78 #define CAP_EOM            (1<<5)     /* has MTEOM */
79 #define CAP_REM            (1<<6)     /* is removable media */
80 #define CAP_RACCESS        (1<<7)     /* is random access device */
81 #define CAP_AUTOMOUNT      (1<<8)     /* Read device at start to see what is there */
82 #define CAP_LABEL          (1<<9)     /* Label blank tapes */
83 #define CAP_ANONVOLS       (1<<10)    /* Mount without knowing volume name */
84 #define CAP_ALWAYSOPEN     (1<<11)    /* always keep device open */
85 #define CAP_AUTOCHANGER    (1<<12)    /* AutoChanger */
86 #define CAP_OFFLINEUNMOUNT (1<<13)    /* Offline before unmount */
87 #define CAP_STREAM         (1<<14)    /* Stream device */
88 #define CAP_BSFATEOM       (1<<15)    /* Backspace file at EOM */
89 #define CAP_FASTFSF        (1<<16)    /* Fast forward space file */
90 #define CAP_TWOEOF         (1<<17)    /* Write two eofs for EOM */
91 #define CAP_CLOSEONPOLL    (1<<18)    /* Close device on polling */
92 #define CAP_POSITIONBLOCKS (1<<19)    /* Use block positioning */
93 #define CAP_MTIOCGET       (1<<20)    /* Basic support for fileno and blkno */
94 #define CAP_REQMOUNT       (1<<21)    /* Require mount to read files back (typically: DVD) */
95 #define CAP_CHECKLABELS    (1<<22)    /* Check for ANSI/IBM labels */
96
97 /* Test state */
98 #define dev_state(dev, st_state) ((dev)->state & (st_state))
99
100 /* Device state bits */
101 #define ST_OPENED          (1<<0)     /* set when device opened */
102 #define ST_TAPE            (1<<1)     /* is a tape device */
103 #define ST_FILE            (1<<2)     /* is a file device */
104 #define ST_FIFO            (1<<3)     /* is a fifo device */
105 #define ST_DVD             (1<<4)     /* is a DVD device */  
106 #define ST_PROG            (1<<5)     /* is a program device */
107 #define ST_LABEL           (1<<6)     /* label found */
108 #define ST_MALLOC          (1<<7)     /* dev packet malloc'ed in init_dev() */
109 #define ST_APPEND          (1<<8)     /* ready for Bacula append */
110 #define ST_READ            (1<<9)     /* ready for Bacula read */
111 #define ST_EOT             (1<<10)    /* at end of tape */
112 #define ST_WEOT            (1<<11)    /* Got EOT on write */
113 #define ST_EOF             (1<<12)    /* Read EOF i.e. zero bytes */
114 #define ST_NEXTVOL         (1<<13)    /* Start writing on next volume */
115 #define ST_SHORT           (1<<14)    /* Short block read */
116 #define ST_MOUNTED         (1<<15)    /* the device is mounted to the mount point */
117 #define ST_OFFLINE         (1<<16)    /* set offline by operator */
118
119 /* dev_blocked states (mutually exclusive) */
120 enum {
121    BST_NOT_BLOCKED = 0,               /* not blocked */
122    BST_UNMOUNTED,                     /* User unmounted device */
123    BST_WAITING_FOR_SYSOP,             /* Waiting for operator to mount tape */
124    BST_DOING_ACQUIRE,                 /* Opening/validating/moving tape */
125    BST_WRITING_LABEL,                  /* Labeling a tape */
126    BST_UNMOUNTED_WAITING_FOR_SYSOP,    /* Closed by user during mount request */
127    BST_MOUNT                           /* Mount request */
128 };
129
130 /* Volume Catalog Information structure definition */
131 struct VOLUME_CAT_INFO {
132    /* Media info for the current Volume */
133    uint32_t VolCatJobs;               /* number of jobs on this Volume */
134    uint32_t VolCatFiles;              /* Number of files */
135    uint32_t VolCatBlocks;             /* Number of blocks */
136    uint64_t VolCatBytes;              /* Number of bytes written */
137    uint32_t VolCatParts;              /* Number of parts written */
138    uint32_t VolCatMounts;             /* Number of mounts this volume */
139    uint32_t VolCatErrors;             /* Number of errors this volume */
140    uint32_t VolCatWrites;             /* Number of writes this volume */
141    uint32_t VolCatReads;              /* Number of reads this volume */
142    uint64_t VolCatRBytes;             /* Number of bytes read */
143    uint32_t VolCatRecycles;           /* Number of recycles this volume */
144    uint32_t EndFile;                  /* Last file number */
145    uint32_t EndBlock;                 /* Last block number */
146    int32_t  LabelType;                /* Bacula/ANSI/IBM */
147    int32_t  Slot;                     /* Slot in changer */
148    bool     InChanger;                /* Set if vol in current magazine */
149    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
150    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
151    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
152    uint64_t VolCatCapacityBytes;      /* capacity estimate */
153    uint64_t VolReadTime;              /* time spent reading */
154    uint64_t VolWriteTime;             /* time spent writing this Volume */
155    char VolCatStatus[20];             /* Volume status */
156    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
157 };
158
159
160 typedef struct s_steal_lock {
161    pthread_t  no_wait_id;             /* id of no wait thread */
162    int        dev_blocked;            /* state */
163    int        dev_prev_blocked;       /* previous blocked state */
164 } bsteal_lock_t;
165
166 struct DEVRES;                        /* Device resource defined in stored_conf.h */
167
168 /*
169  * Device structure definition. There is one of these for
170  *  each physical device. Everything here is "global" to
171  *  that device and effects all jobs using the device.
172  */
173 class DEVICE {
174 public:
175    dlist *attached_dcrs;              /* attached DCR list */
176    pthread_mutex_t mutex;             /* access control */
177    pthread_mutex_t spool_mutex;       /* mutex for updating spool_size */
178    pthread_cond_t wait;               /* thread wait variable */
179    pthread_cond_t wait_next_vol;      /* wait for tape to be mounted */
180    pthread_t no_wait_id;              /* this thread must not wait */
181    int dev_blocked;                   /* set if we must wait (i.e. change tape) */
182    int dev_prev_blocked;              /* previous blocked state */
183    int num_waiting;                   /* number of threads waiting */
184    int num_writers;                   /* number of writing threads */
185    int reserved_device;               /* number of device reservations */
186
187    /* New access control in process of being implemented */
188    brwlock_t lock;                    /* New mutual exclusion lock */
189
190    int use_count;                     /* usage count on this device */
191    int fd;                            /* file descriptor */
192    int capabilities;                  /* capabilities mask */
193    int state;                         /* state mask */
194    int dev_errno;                     /* Our own errno */
195    int mode;                          /* read/write modes */
196    int openmode;                      /* parameter passed to open_dev (useful to reopen the device) */
197    int label_type;                    /* Bacula/ANSI/IBM label types */
198    uint32_t drive_index;              /* Autochanger drive index */
199    POOLMEM *dev_name;                 /* device name */
200    char *errmsg;                      /* nicely edited error message */
201    uint32_t block_num;                /* current block number base 0 */
202    uint32_t file;                     /* current file number base 0 */
203    uint64_t file_addr;                /* Current file read/write address */
204    uint64_t file_size;                /* Current file size */
205    uint32_t EndBlock;                 /* last block written */
206    uint32_t EndFile;                  /* last file written */
207    uint32_t min_block_size;           /* min block size */
208    uint32_t max_block_size;           /* max block size */
209    uint64_t max_volume_size;          /* max bytes to put on one volume */
210    uint64_t max_file_size;            /* max file size to put in one file on volume */
211    uint64_t volume_capacity;          /* advisory capacity */
212    uint64_t max_spool_size;           /* maximum spool file size */
213    uint64_t spool_size;               /* current spool size */
214    uint32_t max_rewind_wait;          /* max secs to allow for rewind */
215    uint32_t max_open_wait;            /* max secs to allow for open */
216    uint32_t max_open_vols;            /* max simultaneous open volumes */
217    
218    uint64_t max_part_size;            /* max part size */
219    uint64_t part_size;                /* current part size */
220    uint32_t part;                     /* current part number */
221    uint64_t part_start;               /* current part start address (relative to the whole volume) */
222    uint32_t num_parts;                /* number of parts (total) */
223    uint64_t free_space;               /* current free space on medium (without the current part) */
224    int free_space_errno;              /* indicates:
225                                        * - free_space_errno == 0: ignore free_space.
226                                        * - free_space_errno < 0: an error occured. 
227                                        * - free_space_errno > 0: free_space is valid. */
228    
229    utime_t  vol_poll_interval;        /* interval between polling Vol mount */
230    DEVRES *device;                    /* pointer to Device Resource */
231    btimer_t *tid;                     /* timer id */
232
233    VOLUME_CAT_INFO VolCatInfo;        /* Volume Catalog Information */
234    VOLUME_LABEL VolHdr;               /* Actual volume label */
235
236    /* Device wait times ***FIXME*** look at durations */
237    char BadVolName[MAX_NAME_LENGTH];  /* Last wrong Volume mounted */
238    bool poll;                         /* set to poll Volume */
239    int min_wait;
240    int max_wait;
241    int max_num_wait;
242    int wait_sec;
243    int rem_wait_sec;
244    int num_wait;
245
246    int is_tape() const;
247    int is_file() const;
248    int is_fifo() const;
249    int is_dvd() const;
250    int is_open() const;
251    int is_offline() const;
252    int is_labeled() const;
253    int is_busy() const;               /* either reading or writing */
254    int at_eof() const;
255    int at_eom() const;
256    int at_eot() const;
257    int can_append() const;
258    int can_read() const;
259    const char *strerror() const;
260    const char *archive_name() const;
261    void set_eof();
262    void set_eot();
263    void set_append();
264    void set_read();
265    void set_offline();
266    void clear_append();
267    void clear_read();
268    void clear_label();
269    void clear_offline();
270 };
271
272 /* Note, these return int not bool! */
273 inline int DEVICE::is_tape() const { return state & ST_TAPE; }
274 inline int DEVICE::is_file() const { return state & ST_FILE; }
275 inline int DEVICE::is_fifo() const { return state & ST_FIFO; }
276 inline int DEVICE::is_dvd()  const { return state & ST_DVD; }
277 inline int DEVICE::is_open() const { return state & ST_OPENED; }
278 inline int DEVICE::is_offline() const { return state & ST_OFFLINE; }
279 inline int DEVICE::is_labeled() const { return state & ST_LABEL; }
280 inline int DEVICE::is_busy() const { return state & ST_READ || num_writers; }
281 inline int DEVICE::at_eof() const { return state & ST_EOF; }
282 inline int DEVICE::at_eot() const { return state & ST_EOT; }
283 inline int DEVICE::can_append() const { return state & ST_APPEND; }
284 inline int DEVICE::can_read() const { return state & ST_READ; }
285 inline void DEVICE::set_append() { state |= ST_APPEND; }
286 inline void DEVICE::set_read() { state |= ST_READ; }
287 inline void DEVICE::set_offline() { state |= ST_OFFLINE; }
288 inline void DEVICE::clear_append() { state &= ~ST_APPEND; }
289 inline void DEVICE::clear_read() { state &= ~ST_READ; }
290 inline void DEVICE::clear_label() { state &= ~ST_LABEL; }
291 inline void DEVICE::clear_offline() { state &= ~ST_OFFLINE; }
292 inline const char *DEVICE::strerror() const { return errmsg; }
293 inline const char *DEVICE::archive_name() const { return dev_name; }
294
295 /*
296  * Device Context (or Control) Record.
297  *  There is one of these records for each Job that is using
298  *  the device. Items in this record are "local" to the Job and
299  *  do not affect other Jobs. Note, a job can have multiple
300  *  DCRs open, each pointing to a different device. 
301  */
302 class DCR {
303 public:
304    dlink dev_link;                    /* link to attach to dev */
305    JCR *jcr;                          /* pointer to JCR */
306    DEVICE *dev;                       /* pointer to device */
307    DEVRES *device;                    /* pointer to device resource */
308    DEV_BLOCK *block;                  /* pointer to block */
309    DEV_RECORD *rec;                   /* pointer to record */
310    int spool_fd;                      /* fd if spooling */
311    bool spool_data;                   /* set to spool data */
312    bool spooling;                     /* set when actually spooling */
313    bool dev_locked;                   /* set if dev already locked */
314    bool NewVol;                       /* set if new Volume mounted */
315    bool WroteVol;                     /* set if Volume written */
316    bool NewFile;                      /* set when EOF written */
317    bool reserved_device;              /* set if reserve done */
318    uint32_t VolFirstIndex;            /* First file index this Volume */
319    uint32_t VolLastIndex;             /* Last file index this Volume */
320    uint32_t FileIndex;                /* Current File Index */
321    uint32_t EndFile;                  /* End file written */
322    uint32_t StartFile;                /* Start write file */
323    uint32_t StartBlock;               /* Start write block */
324    uint32_t EndBlock;                 /* Ending block written */
325    int64_t spool_size;                /* Current spool size */
326    int64_t max_spool_size;            /* Max job spool size */
327    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
328    char pool_name[MAX_NAME_LENGTH];   /* pool name */
329    char pool_type[MAX_NAME_LENGTH];   /* pool type */
330    char media_type[MAX_NAME_LENGTH];  /* media type */
331    char dev_name[MAX_NAME_LENGTH];    /* dev name */
332    VOLUME_CAT_INFO VolCatInfo;        /* Catalog info for desired volume */
333 };
334
335
336 /* Get some definition of function to position
337  *  to the end of the medium in MTEOM. System
338  *  dependent. Arrgggg!
339  */
340 #ifndef MTEOM
341 #ifdef  MTSEOD
342 #define MTEOM MTSEOD
343 #endif
344 #ifdef MTEOD
345 #undef MTEOM
346 #define MTEOM MTEOD
347 #endif
348 #endif
349
350 #endif