]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.h
4824ef2670f8d92d0f49643915d070ac8fda0661
[bacula/bacula] / bacula / src / stored / dev.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
5
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 three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
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.
17
18    You should have received a copy of the GNU Affero 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
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
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.
27 */
28 /*
29  * Definitions for using the Device functions in Bacula
30  *  Tape and File storage access
31  *
32  * Kern Sibbald, MM
33  *
34  */
35
36 /*
37  * Some details of how volume and device reservations work
38  *
39  * class VOLRES:
40  *   set_in_use()     volume being used on current drive
41  *   clear_in_use()   no longer being used.  Can be re-used or moved.
42  *   set_swapping()   set volume being moved to another drive
43  *   is_swapping()    volume is being moved to another drive
44  *   clear_swapping() volume normal
45  *
46  * class DEVICE:
47  *   set_load()       set to load volume
48  *   needs_load()     volume must be loaded (i.e. set_load done)
49  *   clear_load()     load done.
50  *   set_unload()     set to unload volume
51  *   needs_unload()    volume must be unloaded
52  *   clear_unload()   volume unloaded
53  *
54  *    reservations are temporary until the drive is acquired
55  *   inc_reserved()   increments num of reservations
56  *   dec_reserved()   decrements num of reservations
57  *   num_reserved()   number of reservations
58  *
59  * class DCR:
60  *   set_reserved()   sets local reserve flag and calls dev->inc_reserved()
61  *   clear_reserved() clears local reserve flag and calls dev->dec_reserved()
62  *   is_reserved()    returns local reserved flag
63  *   unreserve_device()  much more complete unreservation
64  *
65  */
66
67 #ifndef __DEV_H
68 #define __DEV_H 1
69
70 #undef DCR                            /* used by Bacula */
71
72 /* Return values from wait_for_sysop() */
73 enum {
74    W_ERROR = 1,
75    W_TIMEOUT,
76    W_POLL,
77    W_MOUNT,
78    W_WAKE
79 };
80
81 /* Arguments to open_dev() */
82 enum {
83    CREATE_READ_WRITE = 1,
84    OPEN_READ_WRITE,
85    OPEN_READ_ONLY,
86    OPEN_WRITE_ONLY
87 };
88
89 /* Device types */
90 enum {
91    B_FILE_DEV = 1,
92    B_TAPE_DEV,
93    B_DVD_DEV,
94    B_FIFO_DEV,
95    B_VTAPE_DEV,                       /* change to B_TAPE_DEV after init */
96    B_FTP_DEV,
97    B_VTL_DEV
98 };
99
100 /* Generic status bits returned from status_dev() */
101 #define BMT_TAPE           (1<<0)     /* is tape device */
102 #define BMT_EOF            (1<<1)     /* just read EOF */
103 #define BMT_BOT            (1<<2)     /* at beginning of tape */
104 #define BMT_EOT            (1<<3)     /* end of tape reached */
105 #define BMT_SM             (1<<4)     /* DDS setmark */
106 #define BMT_EOD            (1<<5)     /* DDS at end of data */
107 #define BMT_WR_PROT        (1<<6)     /* tape write protected */
108 #define BMT_ONLINE         (1<<7)     /* tape online */
109 #define BMT_DR_OPEN        (1<<8)     /* tape door open */
110 #define BMT_IM_REP_EN      (1<<9)     /* immediate report enabled */
111
112
113 /* Bits for device capabilities */
114 #define CAP_EOF            (1<<0)     /* has MTWEOF */
115 #define CAP_BSR            (1<<1)     /* has MTBSR */
116 #define CAP_BSF            (1<<2)     /* has MTBSF */
117 #define CAP_FSR            (1<<3)     /* has MTFSR */
118 #define CAP_FSF            (1<<4)     /* has MTFSF */
119 #define CAP_EOM            (1<<5)     /* has MTEOM */
120 #define CAP_REM            (1<<6)     /* is removable media */
121 #define CAP_RACCESS        (1<<7)     /* is random access device */
122 #define CAP_AUTOMOUNT      (1<<8)     /* Read device at start to see what is there */
123 #define CAP_LABEL          (1<<9)     /* Label blank tapes */
124 #define CAP_ANONVOLS       (1<<10)    /* Mount without knowing volume name */
125 #define CAP_ALWAYSOPEN     (1<<11)    /* always keep device open */
126 #define CAP_AUTOCHANGER    (1<<12)    /* AutoChanger */
127 #define CAP_OFFLINEUNMOUNT (1<<13)    /* Offline before unmount */
128 #define CAP_STREAM         (1<<14)    /* Stream device */
129 #define CAP_BSFATEOM       (1<<15)    /* Backspace file at EOM */
130 #define CAP_FASTFSF        (1<<16)    /* Fast forward space file */
131 #define CAP_TWOEOF         (1<<17)    /* Write two eofs for EOM */
132 #define CAP_CLOSEONPOLL    (1<<18)    /* Close device on polling */
133 #define CAP_POSITIONBLOCKS (1<<19)    /* Use block positioning */
134 #define CAP_MTIOCGET       (1<<20)    /* Basic support for fileno and blkno */
135 #define CAP_REQMOUNT       (1<<21)    /* Require mount/unmount */
136 #define CAP_CHECKLABELS    (1<<22)    /* Check for ANSI/IBM labels */
137 #define CAP_BLOCKCHECKSUM  (1<<23)    /* Create/test block checksum */
138
139 /* Test state */
140 #define dev_state(dev, st_state) ((dev)->state & (st_state))
141
142 /* Device state bits */
143 #define ST_XXXXXX          (1<<0)     /* was ST_OPENED */
144 #define ST_XXXXX           (1<<1)     /* was ST_TAPE */
145 #define ST_XXXX            (1<<2)     /* was ST_FILE */
146 #define ST_XXX             (1<<3)     /* was ST_FIFO */
147 #define ST_XX              (1<<4)     /* was ST_DVD */
148 #define ST_X               (1<<5)     /* was ST_PROG */
149
150 #define ST_LABEL           (1<<6)     /* label found */
151 #define ST_MALLOC          (1<<7)     /* dev packet malloc'ed in init_dev() */
152 #define ST_APPEND          (1<<8)     /* ready for Bacula append */
153 #define ST_READ            (1<<9)     /* ready for Bacula read */
154 #define ST_EOT             (1<<10)    /* at end of tape */
155 #define ST_WEOT            (1<<11)    /* Got EOT on write */
156 #define ST_EOF             (1<<12)    /* Read EOF i.e. zero bytes */
157 #define ST_NEXTVOL         (1<<13)    /* Start writing on next volume */
158 #define ST_SHORT           (1<<14)    /* Short block read */
159 #define ST_MOUNTED         (1<<15)    /* the device is mounted to the mount point */
160 #define ST_MEDIA           (1<<16)    /* Media found in mounted device */
161 #define ST_OFFLINE         (1<<17)    /* set offline by operator */
162 #define ST_PART_SPOOLED    (1<<18)    /* spooling part */
163 #define ST_FREESPACE_OK    (1<<19)    /* Have valid freespace for DVD */
164
165
166 /* Volume Catalog Information structure definition */
167 struct VOLUME_CAT_INFO {
168    /* Media info for the current Volume */
169    uint32_t VolCatJobs;               /* number of jobs on this Volume */
170    uint32_t VolCatFiles;              /* Number of files */
171    uint32_t VolCatBlocks;             /* Number of blocks */
172    uint64_t VolCatBytes;              /* Number of bytes written */
173    uint32_t VolCatParts;              /* Number of parts written */
174    uint32_t VolCatMounts;             /* Number of mounts this volume */
175    uint32_t VolCatErrors;             /* Number of errors this volume */
176    uint32_t VolCatWrites;             /* Number of writes this volume */
177    uint32_t VolCatReads;              /* Number of reads this volume */
178    uint64_t VolCatRBytes;             /* Number of bytes read */
179    uint32_t VolCatRecycles;           /* Number of recycles this volume */
180    uint32_t EndFile;                  /* Last file number */
181    uint32_t EndBlock;                 /* Last block number */
182    int32_t  LabelType;                /* Bacula/ANSI/IBM */
183    int32_t  Slot;                     /* >0=Slot loaded, 0=nothing, -1=unknown */
184    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
185    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
186    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
187    uint64_t VolCatCapacityBytes;      /* capacity estimate */
188    btime_t  VolReadTime;              /* time spent reading */
189    btime_t  VolWriteTime;             /* time spent writing this Volume */
190    int64_t  VolMediaId;               /* MediaId */
191    utime_t  VolFirstWritten;          /* Time of first write */
192    utime_t  VolLastWritten;           /* Time of last write */
193    bool     InChanger;                /* Set if vol in current magazine */
194    bool     is_valid;                 /* set if this data is valid */
195    char VolCatStatus[20];             /* Volume status */
196    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
197 };
198
199 class DEVRES;                        /* Device resource defined in stored_conf.h */
200 class DCR; /* forward reference */
201 class VOLRES; /* forward reference */
202
203 /*
204  * Device structure definition. There is one of these for
205  *  each physical device. Everything here is "global" to
206  *  that device and effects all jobs using the device.
207  */
208 class DEVICE: public SMARTALLOC {
209 protected:
210    int m_fd;                          /* file descriptor */
211 private:
212    int m_blocked;                     /* set if we must wait (i.e. change tape) */
213    int m_count;                       /* Mutex use count -- DEBUG only */
214    int m_num_reserved;                /* counter of device reservations */
215    int32_t m_slot;                    /* slot loaded in drive or -1 if none */ 
216    pthread_t m_pid;                   /* Thread that locked -- DEBUG only */
217    bool m_unload;                     /* set when Volume must be unloaded */
218    bool m_load;                       /* set when Volume must be loaded */
219
220 public:
221    DEVICE() {};
222    virtual ~DEVICE() {};
223    DEVICE * volatile swap_dev;        /* Swap vol from this device */
224    dlist *attached_dcrs;              /* attached DCR list */
225    bthread_mutex_t m_mutex;           /* access control */
226    bthread_mutex_t spool_mutex;       /* mutex for updating spool_size */
227    bthread_mutex_t acquire_mutex;     /* mutex for acquire code */
228    pthread_mutex_t read_acquire_mutex; /* mutex for acquire read code */
229    pthread_cond_t wait;               /* thread wait variable */
230    pthread_cond_t wait_next_vol;      /* wait for tape to be mounted */
231    pthread_t no_wait_id;              /* this thread must not wait */
232    int dev_prev_blocked;              /* previous blocked state */
233    int num_waiting;                   /* number of threads waiting */
234    int num_writers;                   /* number of writing threads */
235    int capabilities;                  /* capabilities mask */
236    int state;                         /* state mask */
237    int dev_errno;                     /* Our own errno */
238    int mode;                          /* read/write modes */
239    int openmode;                      /* parameter passed to open_dev (useful to reopen the device) */
240    int dev_type;                      /* device type */
241    bool autoselect;                   /* Autoselect in autochanger */
242    bool initiated;                    /* set when init_dev() called */
243    int label_type;                    /* Bacula/ANSI/IBM label types */
244    uint32_t drive_index;              /* Autochanger drive index (base 0) */
245    POOLMEM *dev_name;                 /* Physical device name */
246    POOLMEM *prt_name;                 /* Name used for display purposes */
247    char *errmsg;                      /* nicely edited error message */
248    uint32_t block_num;                /* current block number base 0 */
249    uint32_t LastBlock;                /* last DEV_BLOCK number written to Volume */
250    uint32_t file;                     /* current file number base 0 */
251    uint64_t file_addr;                /* Current file read/write address */
252    uint64_t file_size;                /* Current file size */
253    uint32_t EndBlock;                 /* last block written */
254    uint32_t EndFile;                  /* last file written */
255    uint32_t min_block_size;           /* min block size */
256    uint32_t max_block_size;           /* max block size */
257    uint32_t max_concurrent_jobs;      /* maximum simultaneous jobs this drive */
258    uint64_t max_volume_size;          /* max bytes to put on one volume */
259    uint64_t max_file_size;            /* max file size to put in one file on volume */
260    uint64_t volume_capacity;          /* advisory capacity */
261    uint64_t max_spool_size;           /* maximum spool file size */
262    uint64_t spool_size;               /* current spool size for this device */
263    uint32_t max_rewind_wait;          /* max secs to allow for rewind */
264    uint32_t max_open_wait;            /* max secs to allow for open */
265    uint32_t max_open_vols;            /* max simultaneous open volumes */
266    
267    uint64_t max_part_size;            /* max part size */
268    uint64_t part_size;                /* current part size */
269    uint32_t part;                     /* current part number (starts at 0) */
270    uint64_t part_start;               /* current part start address (relative to the whole volume) */
271    uint32_t num_dvd_parts;            /* number of parts WRITTEN on the DVD */
272    /* state ST_FREESPACE_OK is set if free_space is valid */
273    uint64_t free_space;               /* current free space on medium (without the current part) */
274    int free_space_errno;              /* indicates errno getting freespace */
275    bool truncating;                   /* if set, we are currently truncating the DVD */
276    bool blank_dvd;                    /* if set, we have a blank DVD in the drive */
277    
278    
279    utime_t  vol_poll_interval;        /* interval between polling Vol mount */
280    DEVRES *device;                    /* pointer to Device Resource */
281    VOLRES *vol;                       /* Pointer to Volume reservation item */
282    btimer_t *tid;                     /* timer id */
283
284    VOLUME_CAT_INFO VolCatInfo;        /* Volume Catalog Information */
285    VOLUME_LABEL VolHdr;               /* Actual volume label */
286    char pool_name[MAX_NAME_LENGTH];   /* pool name */
287    char pool_type[MAX_NAME_LENGTH];   /* pool type */
288
289    char UnloadVolName[MAX_NAME_LENGTH];  /* Last wrong Volume mounted */
290    bool poll;                         /* set to poll Volume */
291    /* Device wait times ***FIXME*** look at durations */
292    int min_wait;
293    int max_wait;
294    int max_num_wait;
295    int wait_sec;
296    int rem_wait_sec;
297    int num_wait;
298
299    btime_t last_timer;        /* used by read/write/seek to get stats (usec) */
300    btime_t last_tick;         /* contains last read/write time (usec) */
301
302    btime_t  DevReadTime;
303    btime_t  DevWriteTime;
304    uint64_t DevWriteBytes;
305    uint64_t DevReadBytes;
306
307    /* Methods */
308    btime_t get_timer_count();         /* return the last timer interval (ms) */
309
310    int has_cap(int cap) const { return capabilities & cap; }
311    void clear_cap(int cap) { capabilities &= ~cap; }
312    void set_cap(int cap) { capabilities |= cap; }
313    bool do_checksum() const { return (capabilities & CAP_BLOCKCHECKSUM) != 0; }
314    int is_autochanger() const { return capabilities & CAP_AUTOCHANGER; }
315    int requires_mount() const { return capabilities & CAP_REQMOUNT; }
316    int is_removable() const { return capabilities & CAP_REM; }
317    int is_tape() const { return (dev_type == B_TAPE_DEV || 
318                                  dev_type == B_VTAPE_DEV); }
319    int is_ftp() const { return dev_type == B_FTP_DEV; }
320    int is_file() const { return (dev_type == B_FILE_DEV); }
321    int is_fifo() const { return dev_type == B_FIFO_DEV; }
322    int is_dvd() const  { return dev_type == B_DVD_DEV; }
323    int is_vtl() const  { return dev_type == B_VTL_DEV; }
324    int is_vtape() const  { return dev_type == B_VTAPE_DEV; }
325    int is_open() const { return m_fd >= 0; }
326    int is_offline() const { return state & ST_OFFLINE; }
327    int is_labeled() const { return state & ST_LABEL; }
328    int is_mounted() const { return state & ST_MOUNTED; }
329    int is_unmountable() const { return (is_dvd() || (is_file() && is_removable())); }
330    int num_reserved() const { return m_num_reserved; };
331    int is_part_spooled() const { return state & ST_PART_SPOOLED; }
332    int have_media() const { return state & ST_MEDIA; }
333    int is_short_block() const { return state & ST_SHORT; }
334    int is_busy() const { return (state & ST_READ) || num_writers || num_reserved(); }
335    int at_eof() const { return state & ST_EOF; }
336    int at_eot() const { return state & ST_EOT; }
337    int at_weot() const { return state & ST_WEOT; }
338    int can_append() const { return state & ST_APPEND; }
339    int is_freespace_ok() const { return state & ST_FREESPACE_OK; }
340    /*
341     * can_write() is meant for checking at the end of a job to see
342     * if we still have a tape (perhaps not if at end of tape
343     * and the job is canceled).
344     */
345    int can_write() const { return is_open() && can_append() &&
346                             is_labeled() && !at_weot(); }
347    int can_read() const   { return state & ST_READ; }
348    bool can_steal_lock() const { return m_blocked &&
349                     (m_blocked == BST_UNMOUNTED ||
350                      m_blocked == BST_WAITING_FOR_SYSOP ||
351                      m_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
352    bool waiting_for_mount() const { return 
353                     (m_blocked == BST_UNMOUNTED ||
354                      m_blocked == BST_WAITING_FOR_SYSOP ||
355                      m_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
356    bool must_unload() const { return m_unload; };
357    bool must_load() const { return m_load; };
358    const char *strerror() const;
359    const char *archive_name() const;
360    const char *name() const;
361    const char *print_name() const;    /* Name for display purposes */
362    void set_ateof(); /* in dev.c */
363    void set_ateot(); /* in dev.c */
364    void set_eot() { state |= ST_EOT; };
365    void set_eof() { state |= ST_EOF; };
366    void set_append() { state |= ST_APPEND; };
367    void set_labeled() { state |= ST_LABEL; };
368    inline void set_read() { state |= ST_READ; };
369    void set_offline() { state |= ST_OFFLINE; };
370    void set_mounted() { state |= ST_MOUNTED; };
371    void set_media() { state |= ST_MEDIA; };
372    void set_short_block() { state |= ST_SHORT; };
373    void set_freespace_ok() { state |= ST_FREESPACE_OK; }
374    void set_part_spooled(int val) { if (val) state |= ST_PART_SPOOLED; \
375           else state &= ~ST_PART_SPOOLED;
376    };
377    bool is_volume_to_unload() const { \
378       return m_unload && strcmp(VolHdr.VolumeName, UnloadVolName) == 0; };
379    void set_load() { m_load = true; };
380    void inc_reserved() { m_num_reserved++; }
381    void set_mounted(int val) { if (val) state |= ST_MOUNTED; \
382           else state &= ~ST_MOUNTED; };
383    void dec_reserved() { m_num_reserved--; ASSERT(m_num_reserved>=0); };
384    void clear_append() { state &= ~ST_APPEND; };
385    void clear_read() { state &= ~ST_READ; };
386    void clear_labeled() { state &= ~ST_LABEL; };
387    void clear_offline() { state &= ~ST_OFFLINE; };
388    void clear_eot() { state &= ~ST_EOT; };
389    void clear_eof() { state &= ~ST_EOF; };
390    void clear_opened() { m_fd = -1; };
391    void clear_mounted() { state &= ~ST_MOUNTED; };
392    void clear_media() { state &= ~ST_MEDIA; };
393    void clear_short_block() { state &= ~ST_SHORT; };
394    void clear_freespace_ok() { state &= ~ST_FREESPACE_OK; };
395    void clear_unload() { m_unload = false; UnloadVolName[0] = 0; };
396    void clear_load() { m_load = false; };
397    char *bstrerror(void) { return errmsg; };
398    char *print_errmsg() { return errmsg; };
399    int32_t get_slot() const { return m_slot; };
400    void setVolCatInfo(bool valid) { VolCatInfo.is_valid = valid; };
401    bool haveVolCatInfo() const { return VolCatInfo.is_valid; };
402    void setVolCatName(const char *name) {
403      bstrncpy(VolCatInfo.VolCatName, name, sizeof(VolCatInfo.VolCatName));
404      setVolCatInfo(false);
405    };
406    char *getVolCatName() { return VolCatInfo.VolCatName; };
407
408    void set_unload();            /* in dev.c */
409    void clear_volhdr();          /* in dev.c */
410    void close();                 /* in dev.c */
411    void close_part(DCR *dcr);    /* in dev.c */
412    bool open(DCR *dcr, int mode); /* in dev.c */
413    void term(void);              /* in dev.c */
414    ssize_t read(void *buf, size_t len); /* in dev.c */
415    ssize_t write(const void *buf, size_t len);  /* in dev.c */
416    bool mount(int timeout);      /* in dev.c */
417    bool unmount(int timeout);    /* in dev.c */
418    void edit_mount_codes(POOL_MEM &omsg, const char *imsg); /* in dev.c */
419    bool offline_or_rewind();     /* in dev.c */
420    bool offline();               /* in dev.c */
421    bool bsf(int count);          /* in dev.c */
422    bool eod(DCR *dcr);           /* in dev.c */
423    bool fsr(int num);            /* in dev.c */
424    bool fsf(int num);            /* in dev.c */
425    bool bsr(int num);            /* in dev.c */
426    bool weof(int num);           /* in dev.c */
427    void lock_door();             /* in dev.c */
428    void unlock_door();           /* in dev.c */
429    int32_t get_os_tape_file();   /* in dev.c */
430    bool scan_dir_for_volume(DCR *dcr); /* in scan.c */
431    bool reposition(DCR *dcr, uint32_t rfile, uint32_t rblock); /* in dev.c */
432    void clrerror(int func);      /* in dev.c */
433    void set_slot(int32_t slot);  /* in dev.c */
434    void clear_slot();            /* in dev.c */
435
436
437    bool update_freespace();      /* in dvd.c */
438
439    uint32_t get_file() const { return file; };
440    uint32_t get_block_num() const { return block_num; };
441    int fd() const { return m_fd; };
442
443    /* low level operations */
444    virtual int d_ioctl(int fd, ioctl_req_t request, char *mt_com=NULL);
445    virtual int d_open(const char *pathname, int flags);
446    virtual int d_close(int fd);
447    virtual ssize_t d_read(int fd, void *buffer, size_t count);
448    virtual ssize_t d_write(int fd, const void *buffer, size_t count);
449    virtual boffset_t lseek(DCR *dcr, boffset_t offset, int whence);
450    virtual bool update_pos(DCR *dcr);
451    virtual bool rewind(DCR *dcr);
452    virtual bool truncate(DCR *dcr);
453    virtual void open_device(DCR *dcr, int omode);
454    /* 
455     * Locking and blocking calls
456     */
457 #ifdef  SD_DEBUG_LOCK
458    void dbg_rLock(const char *, int, bool locked=false);    /* in lock.c */
459    void dbg_rUnlock(const char *, int);                     /* in lock.c */
460    void dbg_Lock(const char *, int);                        /* in lock.c */
461    void dbg_Unlock(const char *, int);                      /* in lock.c */
462    void dbg_Lock_acquire(const char *, int);                /* in lock.c */
463    void dbg_Unlock_acquire(const char *, int);              /* in lock.c */
464    void dbg_Lock_read_acquire(const char *, int);           /* in lock.c */
465    void dbg_Unlock_read_acquire(const char *, int);         /* in lock.c */
466 #else
467    void rLock(bool locked=false);         /* in lock.c */
468    void rUnlock();                        /* in lock.c */
469    void Lock();                           /* in lock.c */
470    void Unlock();                         /* in lock.c */
471    void Lock_acquire();                   /* in lock.c */
472    void Unlock_acquire();                 /* in lock.c */
473    void Lock_read_acquire();              /* in lock.c */
474    void Unlock_read_acquire();            /* in lock.c */
475    void Lock_VolCatInfo();                /* in lock.c */
476    void Unlock_VolCatInfo();              /* in lock.c */
477 #endif
478    int init_mutex();                      /* in lock.c */
479    int init_acquire_mutex();              /* in lock.c */
480    int init_read_acquire_mutex();         /* in lock.c */
481    int init_volcat_mutex();               /* in lock.c */
482    int init_adata_mutex();                /* in lock.c */
483    void set_mutex_priorities();           /* in lock.c */
484    int next_vol_timedwait(const struct timespec *timeout);  /* in lock.c */
485    void dblock(int why);                  /* in lock.c */
486    void dunblock(bool locked=false);      /* in lock.c */
487    bool is_device_unmounted();            /* in lock.c */
488    void set_blocked(int block) { m_blocked = block; };
489    int blocked() const { return m_blocked; };
490    bool is_blocked() const { return m_blocked != BST_NOT_BLOCKED; };
491    const char *print_blocked() const;     /* in dev.c */
492
493 private:
494    bool do_tape_mount(int mount, int dotimeout);  /* in dev.c */
495    bool do_file_mount(int mount, int dotimeout);  /* in dev.c */
496    void set_mode(int omode);                      /* in dev.c */
497    void open_tape_device(DCR *dcr, int omode);    /* in dev.c */
498    void open_file_device(DCR *dcr, int omode);    /* in dev.c */
499 };
500
501 inline const char *DEVICE::strerror() const { return errmsg; }
502 inline const char *DEVICE::archive_name() const { return dev_name; }
503 inline const char *DEVICE::print_name() const { return prt_name; }
504
505
506 #define CHECK_BLOCK_NUMBERS    true
507 #define NO_BLOCK_NUMBER_CHECK  false
508
509 /*
510  * Device Context (or Control) Record.
511  *  There is one of these records for each Job that is using
512  *  the device. Items in this record are "local" to the Job and
513  *  do not affect other Jobs. Note, a job can have multiple
514  *  DCRs open, each pointing to a different device. 
515  * Normally, there is only one JCR thread per DCR. However, the
516  *  big and important exception to this is when a Job is being
517  *  canceled. At that time, there may be two threads using the 
518  *  same DCR. Consequently, when creating/attaching/detaching
519  *  and freeing the DCR we must lock it (m_mutex).
520  */
521 class DCR {
522 private:
523    bool m_dev_locked;                 /* set if dev already locked */
524    int m_dev_lock;                    /* non-zero if rLock already called */
525    bool m_reserved;                   /* set if reserved device */
526    bool m_found_in_use;               /* set if a volume found in use */
527
528 public:
529    dlink dev_link;                    /* link to attach to dev */
530    JCR *jcr;                          /* pointer to JCR */
531    bthread_mutex_t m_mutex;           /* access control */
532    pthread_mutex_t r_mutex;           /* rLock pre-mutex */
533    DEVICE * volatile dev;             /* pointer to device */
534    DEVRES *device;                    /* pointer to device resource */
535    DEV_BLOCK *block;                  /* pointer to block */
536    DEV_RECORD *rec;                   /* pointer to record */
537    pthread_t tid;                     /* Thread running this dcr */
538    int spool_fd;                      /* fd if spooling */
539    bool spool_data;                   /* set to spool data */
540    bool spooling;                     /* set when actually spooling */
541    bool despooling;                   /* set when despooling */
542    bool despool_wait;                 /* waiting for despooling */
543    bool NewVol;                       /* set if new Volume mounted */
544    bool WroteVol;                     /* set if Volume written */
545    bool NewFile;                      /* set when EOF written */
546    bool reserved_volume;              /* set if we reserved a volume */
547    bool any_volume;                   /* Any OK for dir_find_next... */
548    bool attached_to_dev;              /* set when attached to dev */
549    bool keep_dcr;                     /* do not free dcr in release_dcr */
550    uint32_t VolFirstIndex;            /* First file index this Volume */
551    uint32_t VolLastIndex;             /* Last file index this Volume */
552    uint32_t FileIndex;                /* Current File Index */
553    uint32_t EndFile;                  /* End file written */
554    uint32_t StartFile;                /* Start write file */
555    uint32_t StartBlock;               /* Start write block */
556    uint32_t EndBlock;                 /* Ending block written */
557    int64_t  VolMediaId;               /* MediaId */
558    int64_t job_spool_size;            /* Current job spool size */
559    int64_t max_job_spool_size;        /* Max job spool size */
560    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
561    char pool_name[MAX_NAME_LENGTH];   /* pool name */
562    char pool_type[MAX_NAME_LENGTH];   /* pool type */
563    char media_type[MAX_NAME_LENGTH];  /* media type */
564    char dev_name[MAX_NAME_LENGTH];    /* dev name */
565    int Copy;                          /* identical copy number */
566    int Stripe;                        /* RAIT stripe */
567    VOLUME_CAT_INFO VolCatInfo;        /* Catalog info for desired volume */
568
569    /* Methods */
570    void set_dev(DEVICE *ndev) { dev = ndev; }; 
571    void inc_dev_lock() { m_dev_lock++; };
572    void dec_dev_lock() { m_dev_lock--; };
573    bool found_in_use() const { return m_found_in_use; };
574    void set_found_in_use() { m_found_in_use = true; };
575    void clear_found_in_use() { m_found_in_use = false; };
576    bool is_reserved() const { return m_reserved; };
577    bool is_dev_locked() { return m_dev_locked; }
578    void setVolCatInfo(bool valid) { VolCatInfo.is_valid = valid; };
579    bool haveVolCatInfo() const { return VolCatInfo.is_valid; };
580    void setVolCatName(const char *name) {
581      bstrncpy(VolCatInfo.VolCatName, name, sizeof(VolCatInfo.VolCatName));
582      setVolCatInfo(false);
583    };
584    char *getVolCatName() { return VolCatInfo.VolCatName; };
585
586    /* Methods in lock.c */
587    void dblock(int why) { dev->dblock(why); }
588 #ifdef  SD_DEBUG_LOCK
589    void dbg_mLock(const char *, int, bool locked);    /* in lock.c */
590    void dbg_mUnlock(const char *, int);               /* in lock.c */
591 #else
592    void mLock(bool locked);
593    void mUnlock();
594 #endif
595
596    /* Methods in record.c */
597    bool write_record(DEV_RECORD *rec);
598
599    /* Methods in reserve.c */
600    void clear_reserved();
601    void set_reserved();
602    void unreserve_device();
603
604    /* Methods in vol_mgr.c */
605    bool can_i_use_volume();
606    bool can_i_write_volume();
607
608    /* Methods in mount.c */
609    bool mount_next_write_volume();
610    bool mount_next_read_volume();
611    void mark_volume_in_error();
612    void mark_volume_not_inchanger();
613    int try_autolabel(bool opened);
614    bool find_a_volume();
615    bool is_suitable_volume_mounted();
616    bool is_eod_valid();
617    int check_volume_label(bool &ask, bool &autochanger);
618    void release_volume();
619    void do_swapping(bool is_writing);
620    bool do_unload();
621    bool do_load(bool is_writing);
622    bool is_tape_position_ok();
623
624    /* Methods in block.c */
625    bool write_block_to_device();
626    bool write_block_to_dev();
627    bool read_block_from_device(bool check_block_numbers);
628    bool read_block_from_dev(bool check_block_numbers);
629
630    /* Methods in label.c */
631    bool rewrite_volume_label(bool recycle);
632    
633 };
634
635 /*
636  * Volume reservation class -- see reserve.c
637  */
638 class VOLRES { 
639    bool m_swapping;                   /* set when swapping to another drive */
640    bool m_in_use;                     /* set when volume reserved or in use */
641    int32_t m_slot;                    /* slot of swapping volume */
642    uint32_t m_JobId;                  /* JobId for read volumes */
643 public:
644    dlink link;
645    char *vol_name;                    /* Volume name */
646    DEVICE *dev;                       /* Pointer to device to which we are attached */
647
648    bool is_swapping() const { return m_swapping; };
649    void set_swapping() { m_swapping = true; };
650    void clear_swapping() { m_swapping = false; };
651    bool is_in_use() const { return m_in_use; };
652    void set_in_use() { m_in_use = true; };
653    void clear_in_use() { m_in_use = false; };
654    void set_slot(int32_t slot) { m_slot = slot; };
655    void clear_slot() { m_slot = -1; };
656    int32_t get_slot() const { return m_slot; };
657    uint32_t get_jobid() const { return m_JobId; };
658    void set_jobid(uint32_t JobId) { m_JobId = JobId; };
659 };
660
661
662 /* Get some definition of function to position
663  *  to the end of the medium in MTEOM. System
664  *  dependent. Arrgggg!
665  */
666 #ifndef MTEOM
667 #ifdef  MTSEOD
668 #define MTEOM MTSEOD
669 #endif
670 #ifdef MTEOD
671 #undef MTEOM
672 #define MTEOM MTEOD
673 #endif
674 #endif
675
676 #endif