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