]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.h
- Make all files in working directory have .xxx at end.
[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
15    version 2 as amended with additional clauses defined in the
16    file LICENSE in the main source directory.
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 
21    the file LICENSE for additional details.
22
23  */
24
25
26 #ifndef __DEV_H
27 #define __DEV_H 1
28
29 #undef DCR                            /* used by Bacula */
30
31 /* #define NEW_LOCK 1 */
32
33 #define new_lock_device(dev)             _new_lock_device(__FILE__, __LINE__, (dev))
34 #define new_lock_device_state(dev,state) _new_lock_device(__FILE__, __LINE__, (dev), (state))
35 #define new_unlock_device(dev)           _new_unlock_device(__FILE__, __LINE__, (dev))
36
37 #define lock_device(d) _lock_device(__FILE__, __LINE__, (d))
38 #define unlock_device(d) _unlock_device(__FILE__, __LINE__, (d))
39 #define block_device(d, s) _block_device(__FILE__, __LINE__, (d), s)
40 #define unblock_device(d) _unblock_device(__FILE__, __LINE__, (d))
41 #define steal_device_lock(d, p, s) _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
42 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
43
44 /* Arguments to open_dev() */
45 enum {
46    CREATE_READ_WRITE = 1,
47    OPEN_READ_WRITE,
48    OPEN_READ_ONLY,
49    OPEN_WRITE_ONLY
50 };
51
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 */
63
64
65 /* Test capabilities */
66 #define dev_cap(dev, cap) ((dev)->capabilities & (cap))
67
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 */
88 #define CAP_POSITIONBLOCKS (1<<19)    /* Use block positioning */
89 #define CAP_MTIOCGET       (1<<20)    /* Basic support for fileno and blkno */
90 #define CAP_REQMOUNT       (1<<21)    /* Require mount to read files back (typically: DVD) */
91 #define CAP_CHECKLABELS    (1<<22)    /* Check for ANSI/IBM labels */
92
93 /* Test state */
94 #define dev_state(dev, st_state) ((dev)->state & (st_state))
95
96 /* Device state bits */
97 #define ST_OPENED          (1<<0)     /* set when device opened */
98 #define ST_TAPE            (1<<1)     /* is a tape device */
99 #define ST_FILE            (1<<2)     /* is a file device */
100 #define ST_FIFO            (1<<3)     /* is a fifo device */
101 #define ST_DVD             (1<<4)     /* is a DVD device */  
102 #define ST_PROG            (1<<5)     /* is a program device */
103 #define ST_LABEL           (1<<6)     /* label found */
104 #define ST_MALLOC          (1<<7)     /* dev packet malloc'ed in init_dev() */
105 #define ST_APPEND          (1<<8)     /* ready for Bacula append */
106 #define ST_READ            (1<<9)     /* ready for Bacula read */
107 #define ST_EOT             (1<<10)    /* at end of tape */
108 #define ST_WEOT            (1<<11)    /* Got EOT on write */
109 #define ST_EOF             (1<<12)    /* Read EOF i.e. zero bytes */
110 #define ST_NEXTVOL         (1<<13)    /* Start writing on next volume */
111 #define ST_SHORT           (1<<14)    /* Short block read */
112 #define ST_MOUNTED         (1<<15)    /* the device is mounted to the mount point */
113 #define ST_MEDIA           (1<<16)    /* Media found in mounted device */
114 #define ST_OFFLINE         (1<<17)    /* set offline by operator */
115 #define ST_PART_SPOOLED    (1<<18)    /* spooling part */
116
117 /* dev_blocked states (mutually exclusive) */
118 enum {
119    BST_NOT_BLOCKED = 0,               /* not blocked */
120    BST_UNMOUNTED,                     /* User unmounted device */
121    BST_WAITING_FOR_SYSOP,             /* Waiting for operator to mount tape */
122    BST_DOING_ACQUIRE,                 /* Opening/validating/moving tape */
123    BST_WRITING_LABEL,                  /* Labeling a tape */
124    BST_UNMOUNTED_WAITING_FOR_SYSOP,    /* Closed by user during mount request */
125    BST_MOUNT                           /* Mount request */
126 };
127
128 /* Volume Catalog Information structure definition */
129 struct VOLUME_CAT_INFO {
130    /* Media info for the current Volume */
131    uint32_t VolCatJobs;               /* number of jobs on this Volume */
132    uint32_t VolCatFiles;              /* Number of files */
133    uint32_t VolCatBlocks;             /* Number of blocks */
134    uint64_t VolCatBytes;              /* Number of bytes written */
135    uint32_t VolCatParts;              /* Number of parts written */
136    uint32_t VolCatMounts;             /* Number of mounts this volume */
137    uint32_t VolCatErrors;             /* Number of errors this volume */
138    uint32_t VolCatWrites;             /* Number of writes this volume */
139    uint32_t VolCatReads;              /* Number of reads this volume */
140    uint64_t VolCatRBytes;             /* Number of bytes read */
141    uint32_t VolCatRecycles;           /* Number of recycles this volume */
142    uint32_t EndFile;                  /* Last file number */
143    uint32_t EndBlock;                 /* Last block number */
144    int32_t  LabelType;                /* Bacula/ANSI/IBM */
145    int32_t  Slot;                     /* Slot in changer */
146    bool     InChanger;                /* Set if vol in current magazine */
147    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
148    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
149    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
150    uint64_t VolCatCapacityBytes;      /* capacity estimate */
151    uint64_t VolReadTime;              /* time spent reading */
152    uint64_t VolWriteTime;             /* time spent writing this Volume */
153    char VolCatStatus[20];             /* Volume status */
154    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
155 };
156
157
158 typedef struct s_steal_lock {
159    pthread_t  no_wait_id;             /* id of no wait thread */
160    int        dev_blocked;            /* state */
161    int        dev_prev_blocked;       /* previous blocked state */
162 } bsteal_lock_t;
163
164 struct DEVRES;                        /* Device resource defined in stored_conf.h */
165 int      weof_dev(DEVICE *dev, int num);
166 bool     rewind_dev(DEVICE *dev);
167
168 class DCR; /* forward reference */
169 /*
170  * Device structure definition. There is one of these for
171  *  each physical device. Everything here is "global" to
172  *  that device and effects all jobs using the device.
173  */
174 class DEVICE {
175 public:
176    dlist *attached_dcrs;              /* attached DCR list */
177    pthread_mutex_t mutex;             /* access control */
178    pthread_mutex_t spool_mutex;       /* mutex for updating spool_size */
179    pthread_cond_t wait;               /* thread wait variable */
180    pthread_cond_t wait_next_vol;      /* wait for tape to be mounted */
181    pthread_t no_wait_id;              /* this thread must not wait */
182    int dev_blocked;                   /* set if we must wait (i.e. change tape) */
183    int dev_prev_blocked;              /* previous blocked state */
184    int num_waiting;                   /* number of threads waiting */
185    int num_writers;                   /* number of writing threads */
186    int reserved_device;               /* number of device reservations */
187
188    /* New access control in process of being implemented */
189    brwlock_t lock;                    /* New mutual exclusion lock */
190
191    int use_count;                     /* usage count on this device 0 or 1 */
192    int fd;                            /* file descriptor */
193    int capabilities;                  /* capabilities mask */
194    int state;                         /* state mask */
195    int dev_errno;                     /* Our own errno */
196    int mode;                          /* read/write modes */
197    int openmode;                      /* parameter passed to open_dev (useful to reopen the device) */
198    bool autoselect;                   /* Autoselect in autochanger */
199    bool open_nowait;                  /* If set, don t wait on open */
200    int label_type;                    /* Bacula/ANSI/IBM label types */
201    uint32_t drive_index;              /* Autochanger drive index (base 0) */
202    int32_t  Slot;                     /* Slot currently in drive (base 1) */
203    POOLMEM *dev_name;                 /* Physical device name */
204    POOLMEM *prt_name;                 /* Name used for display purposes */
205    char *errmsg;                      /* nicely edited error message */
206    uint32_t block_num;                /* current block number base 0 */
207    uint32_t file;                     /* current file number base 0 */
208    uint64_t file_addr;                /* Current file read/write address */
209    uint64_t file_size;                /* Current file size */
210    uint32_t EndBlock;                 /* last block written */
211    uint32_t EndFile;                  /* last file written */
212    uint32_t min_block_size;           /* min block size */
213    uint32_t max_block_size;           /* max block size */
214    uint64_t max_volume_size;          /* max bytes to put on one volume */
215    uint64_t max_file_size;            /* max file size to put in one file on volume */
216    uint64_t volume_capacity;          /* advisory capacity */
217    uint64_t max_spool_size;           /* maximum spool file size */
218    uint64_t spool_size;               /* current spool size for this device */
219    uint32_t max_rewind_wait;          /* max secs to allow for rewind */
220    uint32_t max_open_wait;            /* max secs to allow for open */
221    uint32_t max_open_vols;            /* max simultaneous open volumes */
222    
223    uint64_t max_part_size;            /* max part size */
224    uint64_t part_size;                /* current part size */
225    uint32_t part;                     /* current part number */
226    uint64_t part_start;               /* current part start address (relative to the whole volume) */
227    uint32_t num_parts;                /* number of parts (total) */
228    uint64_t free_space;               /* current free space on medium (without the current part) */
229    int free_space_errno;              /* indicates:
230                                        * - free_space_errno == 0: ignore free_space.
231                                        * - free_space_errno < 0: an error occured. 
232                                        * - free_space_errno > 0: free_space is valid. */
233    
234    utime_t  vol_poll_interval;        /* interval between polling Vol mount */
235    DEVRES *device;                    /* pointer to Device Resource */
236    btimer_t *tid;                     /* timer id */
237
238    VOLUME_CAT_INFO VolCatInfo;        /* Volume Catalog Information */
239    VOLUME_LABEL VolHdr;               /* Actual volume label */
240    char pool_name[MAX_NAME_LENGTH];   /* pool name */
241    char pool_type[MAX_NAME_LENGTH];   /* pool type */
242
243    /* Device wait times ***FIXME*** look at durations */
244    char BadVolName[MAX_NAME_LENGTH];  /* Last wrong Volume mounted */
245    bool poll;                         /* set to poll Volume */
246    int min_wait;
247    int max_wait;
248    int max_num_wait;
249    int wait_sec;
250    int rem_wait_sec;
251    int num_wait;
252
253    /* Methods */
254    int is_autochanger() const { return capabilities & CAP_AUTOCHANGER; }
255    int requires_mount() const { return capabilities & CAP_REQMOUNT; }
256    int is_tape() const { return state & ST_TAPE; }
257    int is_file() const { return state & ST_FILE; }
258    int is_fifo() const { return state & ST_FIFO; }
259    int is_dvd() const  { return state & ST_DVD; }
260    int is_open() const { return state & ST_OPENED; }
261    int is_offline() const { return state & ST_OFFLINE; }
262    int is_labeled() const { return state & ST_LABEL; }
263    int is_mounted() const { return state & ST_MOUNTED; }
264    int is_part_spooled() const { return state & ST_PART_SPOOLED; }
265    int have_media() const { return state & ST_MEDIA; }
266    int is_short_block() const { return state & ST_SHORT; }
267    int is_busy() const { return (state & ST_READ) || num_writers || reserved_device; }
268    int at_eof() const { return state & ST_EOF; }
269    int at_eot() const { return state & ST_EOT; }
270    int at_weot() const { return state & ST_WEOT; }
271    int can_append() const { return state & ST_APPEND; }
272    /*
273     * can_write() is meant for checking at the end of a job to see
274     * if we still have a tape (perhaps not if at end of tape
275     * and the job is canceled).
276     */
277    int can_write() const { return is_open() && can_append() &&
278                             is_labeled() && !at_weot(); }
279    int can_read() const   { return state & ST_READ; }
280    bool can_steal_lock() const { return dev_blocked &&
281                     (dev_blocked == BST_UNMOUNTED ||
282                      dev_blocked == BST_WAITING_FOR_SYSOP ||
283                      dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
284    bool waiting_for_mount() const { return 
285                     (dev_blocked == BST_UNMOUNTED ||
286                      dev_blocked == BST_WAITING_FOR_SYSOP ||
287                      dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
288    bool weof() { return !weof_dev(this, 1); };
289    bool fsr(int num);   /* in dev.c */
290    bool fsf(int num);   /* in dev.c */
291    bool rewind() { return rewind_dev(this); };
292    const char *strerror() const;
293    const char *archive_name() const;
294    const char *name() const;
295    const char *print_name() const;    /* Name for display purposes */
296    void set_ateof(); /* in dev.c */
297    void set_ateot(); /* in dev.c */
298    void set_eot() { state |= ST_EOT; };
299    void set_eof() { state |= ST_EOF; };
300    void set_append() { state |= ST_APPEND; };
301    void set_labeled() { state |= ST_LABEL; };
302    void set_read() { state |= ST_READ; };
303    void set_offline() { state |= ST_OFFLINE; };
304    void set_opened() { state |= ST_OPENED; };
305    void set_mounted() { state |= ST_MOUNTED; };
306    void set_media() { state |= ST_MEDIA; };
307    void set_short_block() { state |= ST_SHORT; };
308    void set_part_spooled(int val) { if (val) state |= ST_PART_SPOOLED; \
309           else state &= ~ST_PART_SPOOLED; };
310    void set_mounted(int val) { if (val) state |= ST_MOUNTED; \
311           else state &= ~ST_MOUNTED; };
312    void clear_append() { state &= ~ST_APPEND; };
313    void clear_read() { state &= ~ST_READ; };
314    void clear_labeled() { state &= ~ST_LABEL; };
315    void clear_offline() { state &= ~ST_OFFLINE; };
316    void clear_eot() { state &= ~ST_EOT; };
317    void clear_eof() { state &= ~ST_EOF; };
318    void clear_opened() { state &= ~ST_OPENED; };
319    void clear_mounted() { state &= ~ST_MOUNTED; };
320    void clear_media() { state &= ~ST_MEDIA; };
321    void clear_short_block() { state &= ~ST_SHORT; };
322
323    void block(int why); /* in dev.c */
324    void unblock();      /* in dev.c */
325    void close();        /* in dev.c */
326    int open(DCR *dcr, int mode); /* in dev.c */
327
328
329    void set_blocked(int block) { dev_blocked = block; };
330    int  get_blocked() const { return dev_blocked; };
331    const char *print_blocked() const; /* in dev.c */
332    bool is_blocked() const { return dev_blocked != BST_NOT_BLOCKED; };
333
334 private:
335    void set_mode(int omode); /* in dev.c */
336    void open_tape_device(int omode); /* in dev.c */
337    void open_file_device(int omode); /* in dev.c */
338    void open_dvd_device(DCR *dcr, int omode); /* in dev.c */
339
340 };
341
342 /* Note, these return int not bool! */
343 inline const char *DEVICE::strerror() const { return errmsg; }
344 inline const char *DEVICE::archive_name() const { return dev_name; }
345 inline const char *DEVICE::print_name() const { return prt_name; }
346
347 /*
348  * Device Context (or Control) Record.
349  *  There is one of these records for each Job that is using
350  *  the device. Items in this record are "local" to the Job and
351  *  do not affect other Jobs. Note, a job can have multiple
352  *  DCRs open, each pointing to a different device. 
353  */
354 class DCR {
355 public:
356    dlink dev_link;                    /* link to attach to dev */
357    JCR *jcr;                          /* pointer to JCR */
358    DEVICE *dev;                       /* pointer to device */
359    DEVRES *device;                    /* pointer to device resource */
360    DEV_BLOCK *block;                  /* pointer to block */
361    DEV_RECORD *rec;                   /* pointer to record */
362    int spool_fd;                      /* fd if spooling */
363    bool spool_data;                   /* set to spool data */
364    bool spooling;                     /* set when actually spooling */
365    bool dev_locked;                   /* set if dev already locked */
366    bool NewVol;                       /* set if new Volume mounted */
367    bool WroteVol;                     /* set if Volume written */
368    bool NewFile;                      /* set when EOF written */
369    bool reserved_device;              /* set if reserve done */
370    uint32_t VolFirstIndex;            /* First file index this Volume */
371    uint32_t VolLastIndex;             /* Last file index this Volume */
372    uint32_t FileIndex;                /* Current File Index */
373    uint32_t EndFile;                  /* End file written */
374    uint32_t StartFile;                /* Start write file */
375    uint32_t StartBlock;               /* Start write block */
376    uint32_t EndBlock;                 /* Ending block written */
377    int64_t job_spool_size;            /* Current job spool size */
378    int64_t max_job_spool_size;        /* Max job spool size */
379    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
380    char pool_name[MAX_NAME_LENGTH];   /* pool name */
381    char pool_type[MAX_NAME_LENGTH];   /* pool type */
382    char media_type[MAX_NAME_LENGTH];  /* media type */
383    char dev_name[MAX_NAME_LENGTH];    /* dev name */
384    int Copy;                          /* identical copy number */
385    int Stripe;                        /* RAIT stripe */
386    VOLUME_CAT_INFO VolCatInfo;        /* Catalog info for desired volume */
387 };
388
389 /*
390  * Volume reservation class -- see reserve.c
391  */
392 class VOLRES { 
393 public:
394    dlink link;
395    char *vol_name;
396    DEVICE *dev;
397    DCR *dcr;
398 };
399
400
401 /* Get some definition of function to position
402  *  to the end of the medium in MTEOM. System
403  *  dependent. Arrgggg!
404  */
405 #ifndef MTEOM
406 #ifdef  MTSEOD
407 #define MTEOM MTSEOD
408 #endif
409 #ifdef MTEOD
410 #undef MTEOM
411 #define MTEOM MTEOD
412 #endif
413 #endif
414
415 #endif