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