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