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