]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.h
kes Add a new close_part() class in the SD to save the device
[bacula/bacula] / bacula / src / stored / dev.h
1 /*
2  * Definitions for using the Device functions in Bacula
3  *  Tape and File storage access
4  *
5  * Kern Sibbald, MM
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2000-2006 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License
15    version 2 as amended with additional clauses defined in the
16    file LICENSE in the main source directory.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
21    the file LICENSE for additional details.
22
23  */
24
25
26 #ifndef __DEV_H
27 #define __DEV_H 1
28
29 #undef DCR                            /* used by Bacula */
30
31 /* #define NEW_LOCK 1 */
32
33 #define new_lock_device(dev)             _new_lock_device(__FILE__, __LINE__, (dev))
34 #define new_lock_device_state(dev,state) _new_lock_device(__FILE__, __LINE__, (dev), (state))
35 #define new_unlock_device(dev)           _new_unlock_device(__FILE__, __LINE__, (dev))
36
37 #define lock_device(d) _lock_device(__FILE__, __LINE__, (d))
38 #define unlock_device(d) _unlock_device(__FILE__, __LINE__, (d))
39 #define block_device(d, s) _block_device(__FILE__, __LINE__, (d), s)
40 #define unblock_device(d) _unblock_device(__FILE__, __LINE__, (d))
41 #define steal_device_lock(d, p, s) _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
42 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
43
44 /* Return values from wait_for_sysop() */
45 enum {
46    W_ERROR = 1,
47    W_TIMEOUT,
48    W_POLL,
49    W_MOUNT,
50    W_WAKE
51 };
52
53 /* Arguments to open_dev() */
54 enum {
55    CREATE_READ_WRITE = 1,
56    OPEN_READ_WRITE,
57    OPEN_READ_ONLY,
58    OPEN_WRITE_ONLY
59 };
60
61 /* Device types */
62 enum {
63    B_FILE_DEV = 1,
64    B_TAPE_DEV,
65    B_DVD_DEV,
66    B_FIFO_DEV,
67    B_PROG_DEV
68 };
69
70 /* Generic status bits returned from status_dev() */
71 #define BMT_TAPE           (1<<0)     /* is tape device */
72 #define BMT_EOF            (1<<1)     /* just read EOF */
73 #define BMT_BOT            (1<<2)     /* at beginning of tape */
74 #define BMT_EOT            (1<<3)     /* end of tape reached */
75 #define BMT_SM             (1<<4)     /* DDS setmark */
76 #define BMT_EOD            (1<<5)     /* DDS at end of data */
77 #define BMT_WR_PROT        (1<<6)     /* tape write protected */
78 #define BMT_ONLINE         (1<<7)     /* tape online */
79 #define BMT_DR_OPEN        (1<<8)     /* tape door open */
80 #define BMT_IM_REP_EN      (1<<9)     /* immediate report enabled */
81
82
83 /* Test capabilities */
84 #define dev_cap(dev, cap) ((dev)->capabilities & (cap))
85
86 /* Bits for device capabilities */
87 #define CAP_EOF            (1<<0)     /* has MTWEOF */
88 #define CAP_BSR            (1<<1)     /* has MTBSR */
89 #define CAP_BSF            (1<<2)     /* has MTBSF */
90 #define CAP_FSR            (1<<3)     /* has MTFSR */
91 #define CAP_FSF            (1<<4)     /* has MTFSF */
92 #define CAP_EOM            (1<<5)     /* has MTEOM */
93 #define CAP_REM            (1<<6)     /* is removable media */
94 #define CAP_RACCESS        (1<<7)     /* is random access device */
95 #define CAP_AUTOMOUNT      (1<<8)     /* Read device at start to see what is there */
96 #define CAP_LABEL          (1<<9)     /* Label blank tapes */
97 #define CAP_ANONVOLS       (1<<10)    /* Mount without knowing volume name */
98 #define CAP_ALWAYSOPEN     (1<<11)    /* always keep device open */
99 #define CAP_AUTOCHANGER    (1<<12)    /* AutoChanger */
100 #define CAP_OFFLINEUNMOUNT (1<<13)    /* Offline before unmount */
101 #define CAP_STREAM         (1<<14)    /* Stream device */
102 #define CAP_BSFATEOM       (1<<15)    /* Backspace file at EOM */
103 #define CAP_FASTFSF        (1<<16)    /* Fast forward space file */
104 #define CAP_TWOEOF         (1<<17)    /* Write two eofs for EOM */
105 #define CAP_CLOSEONPOLL    (1<<18)    /* Close device on polling */
106 #define CAP_POSITIONBLOCKS (1<<19)    /* Use block positioning */
107 #define CAP_MTIOCGET       (1<<20)    /* Basic support for fileno and blkno */
108 #define CAP_REQMOUNT       (1<<21)    /* Require mount to read files back (typically: DVD) */
109 #define CAP_CHECKLABELS    (1<<22)    /* Check for ANSI/IBM labels */
110
111 /* Test state */
112 #define dev_state(dev, st_state) ((dev)->state & (st_state))
113
114 /* Device state bits */
115 #define ST_XXXXXX          (1<<0)     /* was ST_OPENED */
116 #define ST_XXXXX           (1<<1)     /* was ST_TAPE */
117 #define ST_XXXX            (1<<2)     /* was ST_FILE */
118 #define ST_XXX             (1<<3)     /* was ST_FIFO */
119 #define ST_XX              (1<<4)     /* was ST_DVD */
120 #define ST_X               (1<<5)     /* was ST_PROG */
121
122 #define ST_LABEL           (1<<6)     /* label found */
123 #define ST_MALLOC          (1<<7)     /* dev packet malloc'ed in init_dev() */
124 #define ST_APPEND          (1<<8)     /* ready for Bacula append */
125 #define ST_READ            (1<<9)     /* ready for Bacula read */
126 #define ST_EOT             (1<<10)    /* at end of tape */
127 #define ST_WEOT            (1<<11)    /* Got EOT on write */
128 #define ST_EOF             (1<<12)    /* Read EOF i.e. zero bytes */
129 #define ST_NEXTVOL         (1<<13)    /* Start writing on next volume */
130 #define ST_SHORT           (1<<14)    /* Short block read */
131 #define ST_MOUNTED         (1<<15)    /* the device is mounted to the mount point */
132 #define ST_MEDIA           (1<<16)    /* Media found in mounted device */
133 #define ST_OFFLINE         (1<<17)    /* set offline by operator */
134 #define ST_PART_SPOOLED    (1<<18)    /* spooling part */
135 #define ST_FREESPACE_OK    (1<<19)    /* Have valid freespace for DVD */
136
137 /* dev_blocked states (mutually exclusive) */
138 enum {
139    BST_NOT_BLOCKED = 0,               /* not blocked */
140    BST_UNMOUNTED,                     /* User unmounted device */
141    BST_WAITING_FOR_SYSOP,             /* Waiting for operator to mount tape */
142    BST_DOING_ACQUIRE,                 /* Opening/validating/moving tape */
143    BST_WRITING_LABEL,                 /* Labeling a tape */
144    BST_UNMOUNTED_WAITING_FOR_SYSOP,   /* User unmounted during wait for op */
145    BST_MOUNT                          /* Mount request */
146 };
147
148 /* Volume Catalog Information structure definition */
149 struct VOLUME_CAT_INFO {
150    /* Media info for the current Volume */
151    uint32_t VolCatJobs;               /* number of jobs on this Volume */
152    uint32_t VolCatFiles;              /* Number of files */
153    uint32_t VolCatBlocks;             /* Number of blocks */
154    uint64_t VolCatBytes;              /* Number of bytes written */
155    uint32_t VolCatParts;              /* Number of parts written */
156    uint32_t VolCatMounts;             /* Number of mounts this volume */
157    uint32_t VolCatErrors;             /* Number of errors this volume */
158    uint32_t VolCatWrites;             /* Number of writes this volume */
159    uint32_t VolCatReads;              /* Number of reads this volume */
160    uint64_t VolCatRBytes;             /* Number of bytes read */
161    uint32_t VolCatRecycles;           /* Number of recycles this volume */
162    uint32_t EndFile;                  /* Last file number */
163    uint32_t EndBlock;                 /* Last block number */
164    int32_t  LabelType;                /* Bacula/ANSI/IBM */
165    int32_t  Slot;                     /* >0=Slot loaded, 0=nothing, -1=unknown */
166    bool     InChanger;                /* Set if vol in current magazine */
167    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
168    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
169    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
170    uint64_t VolCatCapacityBytes;      /* capacity estimate */
171    uint64_t VolReadTime;              /* time spent reading */
172    uint64_t VolWriteTime;             /* time spent writing this Volume */
173    char VolCatStatus[20];             /* Volume status */
174    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
175 };
176
177
178 typedef struct s_steal_lock {
179    pthread_t  no_wait_id;             /* id of no wait thread */
180    int        dev_blocked;            /* state */
181    int        dev_prev_blocked;       /* previous blocked state */
182 } bsteal_lock_t;
183
184 class DEVRES;                        /* Device resource defined in stored_conf.h */
185
186 class DCR; /* forward reference */
187 /*
188  * Device structure definition. There is one of these for
189  *  each physical device. Everything here is "global" to
190  *  that device and effects all jobs using the device.
191  */
192 class DEVICE {
193 public:
194    dlist *attached_dcrs;              /* attached DCR list */
195    pthread_mutex_t mutex;             /* access control */
196    pthread_mutex_t spool_mutex;       /* mutex for updating spool_size */
197    pthread_cond_t wait;               /* thread wait variable */
198    pthread_cond_t wait_next_vol;      /* wait for tape to be mounted */
199    pthread_t no_wait_id;              /* this thread must not wait */
200    int dev_blocked;                   /* set if we must wait (i.e. change tape) */
201    int dev_prev_blocked;              /* previous blocked state */
202    int num_waiting;                   /* number of threads waiting */
203    int num_writers;                   /* number of writing threads */
204    int reserved_device;               /* number of device reservations */
205
206    /* New access control in process of being implemented */
207    brwlock_t lock;                    /* New mutual exclusion lock */
208
209    int fd;                            /* file descriptor */
210    int capabilities;                  /* capabilities mask */
211    int state;                         /* state mask */
212    int dev_errno;                     /* Our own errno */
213    int mode;                          /* read/write modes */
214    int openmode;                      /* parameter passed to open_dev (useful to reopen the device) */
215    int dev_type;                      /* device type */
216    bool autoselect;                   /* Autoselect in autochanger */
217    int label_type;                    /* Bacula/ANSI/IBM label types */
218    uint32_t drive_index;              /* Autochanger drive index (base 0) */
219    int32_t  Slot;                     /* Slot currently in drive (base 1) */
220    POOLMEM *dev_name;                 /* Physical device name */
221    POOLMEM *prt_name;                 /* Name used for display purposes */
222    char *errmsg;                      /* nicely edited error message */
223    uint32_t block_num;                /* current block number base 0 */
224    uint32_t file;                     /* current file number base 0 */
225    uint64_t file_addr;                /* Current file read/write address */
226    uint64_t file_size;                /* Current file size */
227    uint32_t EndBlock;                 /* last block written */
228    uint32_t EndFile;                  /* last file written */
229    uint32_t min_block_size;           /* min block size */
230    uint32_t max_block_size;           /* max block size */
231    uint64_t max_volume_size;          /* max bytes to put on one volume */
232    uint64_t max_file_size;            /* max file size to put in one file on volume */
233    uint64_t volume_capacity;          /* advisory capacity */
234    uint64_t max_spool_size;           /* maximum spool file size */
235    uint64_t spool_size;               /* current spool size for this device */
236    uint32_t max_rewind_wait;          /* max secs to allow for rewind */
237    uint32_t max_open_wait;            /* max secs to allow for open */
238    uint32_t max_open_vols;            /* max simultaneous open volumes */
239    
240    uint64_t max_part_size;            /* max part size */
241    uint64_t part_size;                /* current part size */
242    uint32_t part;                     /* current part number (starts at 0) */
243    uint64_t part_start;               /* current part start address (relative to the whole volume) */
244    uint32_t num_dvd_parts;            /* number of parts WRITTEN on the DVD */
245    /* state ST_FREESPACE_OK is set if free_space is valid */
246    uint64_t free_space;               /* current free space on medium (without the current part) */
247    int free_space_errno;              /* indicates errno getting freespace */
248    bool truncating;                   /* if set, we are currently truncating the DVD */
249    bool truncated_dvd;                /* if set, we have a truncated DVD in the drive */
250    
251    
252    utime_t  vol_poll_interval;        /* interval between polling Vol mount */
253    DEVRES *device;                    /* pointer to Device Resource */
254    btimer_t *tid;                     /* timer id */
255
256    VOLUME_CAT_INFO VolCatInfo;        /* Volume Catalog Information */
257    VOLUME_LABEL VolHdr;               /* Actual volume label */
258    char pool_name[MAX_NAME_LENGTH];   /* pool name */
259    char pool_type[MAX_NAME_LENGTH];   /* pool type */
260
261    /* Device wait times ***FIXME*** look at durations */
262    char BadVolName[MAX_NAME_LENGTH];  /* Last wrong Volume mounted */
263    bool poll;                         /* set to poll Volume */
264    int min_wait;
265    int max_wait;
266    int max_num_wait;
267    int wait_sec;
268    int rem_wait_sec;
269    int num_wait;
270
271    /* Methods */
272    int has_cap(int cap) const { return capabilities & cap; }
273    int is_autochanger() const { return capabilities & CAP_AUTOCHANGER; }
274    int requires_mount() const { return capabilities & CAP_REQMOUNT; }
275    int is_removable() const { return capabilities & CAP_REM; }
276    int is_tape() const { return dev_type == B_TAPE_DEV; }
277    int is_file() const { return dev_type == B_FILE_DEV; }
278    int is_fifo() const { return dev_type == B_FIFO_DEV; }
279    int is_dvd() const  { return dev_type == B_DVD_DEV; }
280    int is_prog() const  { return dev_type == B_PROG_DEV; }
281    int is_open() const { return fd >= 0; }
282    int is_offline() const { return state & ST_OFFLINE; }
283    int is_labeled() const { return state & ST_LABEL; }
284    int is_mounted() const { return state & ST_MOUNTED; }
285    int is_part_spooled() const { return state & ST_PART_SPOOLED; }
286    int have_media() const { return state & ST_MEDIA; }
287    int is_short_block() const { return state & ST_SHORT; }
288    int is_busy() const { return (state & ST_READ) || num_writers || reserved_device; }
289    int at_eof() const { return state & ST_EOF; }
290    int at_eot() const { return state & ST_EOT; }
291    int at_weot() const { return state & ST_WEOT; }
292    int can_append() const { return state & ST_APPEND; }
293    int is_freespace_ok() const { return state & ST_FREESPACE_OK; }
294    /*
295     * can_write() is meant for checking at the end of a job to see
296     * if we still have a tape (perhaps not if at end of tape
297     * and the job is canceled).
298     */
299    int can_write() const { return is_open() && can_append() &&
300                             is_labeled() && !at_weot(); }
301    int can_read() const   { return state & ST_READ; }
302    bool can_steal_lock() const { return dev_blocked &&
303                     (dev_blocked == BST_UNMOUNTED ||
304                      dev_blocked == BST_WAITING_FOR_SYSOP ||
305                      dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
306    bool waiting_for_mount() const { return 
307                     (dev_blocked == BST_UNMOUNTED ||
308                      dev_blocked == BST_WAITING_FOR_SYSOP ||
309                      dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP); };
310    const char *strerror() const;
311    const char *archive_name() const;
312    const char *name() const;
313    const char *print_name() const;    /* Name for display purposes */
314    void set_ateof(); /* in dev.c */
315    void set_ateot(); /* in dev.c */
316    void set_eot() { state |= ST_EOT; };
317    void set_eof() { state |= ST_EOF; };
318    void set_append() { state |= ST_APPEND; };
319    void set_labeled() { state |= ST_LABEL; };
320    inline void set_read() { state |= ST_READ; };
321    void set_offline() { state |= ST_OFFLINE; };
322    void set_mounted() { state |= ST_MOUNTED; };
323    void set_media() { state |= ST_MEDIA; };
324    void set_short_block() { state |= ST_SHORT; };
325    void set_freespace_ok() { state |= ST_FREESPACE_OK; }
326    void set_part_spooled(int val) { if (val) state |= ST_PART_SPOOLED; \
327           else state &= ~ST_PART_SPOOLED; };
328    void set_mounted(int val) { if (val) state |= ST_MOUNTED; \
329           else state &= ~ST_MOUNTED; };
330    void clear_append() { state &= ~ST_APPEND; };
331    void clear_read() { state &= ~ST_READ; };
332    void clear_labeled() { state &= ~ST_LABEL; };
333    void clear_offline() { state &= ~ST_OFFLINE; };
334    void clear_eot() { state &= ~ST_EOT; };
335    void clear_eof() { state &= ~ST_EOF; };
336    void clear_opened() { fd = -1; };
337    void clear_mounted() { state &= ~ST_MOUNTED; };
338    void clear_media() { state &= ~ST_MEDIA; };
339    void clear_short_block() { state &= ~ST_SHORT; };
340    void clear_freespace_ok() { state &= ~ST_FREESPACE_OK; };
341    char *bstrerror(void) { return errmsg; };
342
343    void block(int why);          /* in dev.c */
344    void unblock();               /* in dev.c */
345    void close();                 /* in dev.c */
346    void close_part(DCR *dcr);    /* in dev.c */
347    bool truncate(DCR *dcr);      /* in dev.c */
348    int open(DCR *dcr, int mode); /* in dev.c */
349    void term(void);              /* in dev.c */
350    bool rewind(DCR *dcr);        /* in dev.c */
351    bool mount(int timeout);      /* in dev.c */
352    bool unmount(int timeout);    /* in dev.c */
353    void edit_mount_codes(POOL_MEM &omsg, const char *imsg); /* in dev.c */
354    bool offline_or_rewind();     /* in dev.c */
355    bool offline();               /* in dev.c */
356    bool bsf(int count);          /* in dev.c */
357    bool eod();                   /* in dev.c */
358    bool fsr(int num);            /* in dev.c */
359    bool fsf(int num);            /* in dev.c */
360    bool bsr(int num);            /* in dev.c */
361    bool weof(int num);           /* in dev.c */
362    bool scan_dir_for_volume(DCR *dcr); /* in scan.c */
363    bool reposition(uint32_t rfile, uint32_t rblock); /* in dev.c */
364    void clrerror(int func);     /* in dev.c */
365
366    void set_blocked(int block) { dev_blocked = block; };
367    int  get_blocked() const { return dev_blocked; };
368    const char *print_blocked() const; /* in dev.c */
369    bool is_blocked() const { return dev_blocked != BST_NOT_BLOCKED; };
370
371 private:
372    bool do_mount(int mount, int timeout);      /* in dev.c */
373    void set_mode(int omode);                   /* in dev.c */
374    void open_tape_device(DCR *dcr, int omode); /* in dev.c */
375    void open_file_device(DCR *dcr, int omode); /* in dev.c */
376    void open_dvd_device(DCR *dcr, int omode);  /* in dev.c */
377    void set_blocking();                        /* in dev.c */
378
379 };
380
381 /* Note, these return int not bool! */
382 inline const char *DEVICE::strerror() const { return errmsg; }
383 inline const char *DEVICE::archive_name() const { return dev_name; }
384 inline const char *DEVICE::print_name() const { return prt_name; }
385
386 /*
387  * Device Context (or Control) Record.
388  *  There is one of these records for each Job that is using
389  *  the device. Items in this record are "local" to the Job and
390  *  do not affect other Jobs. Note, a job can have multiple
391  *  DCRs open, each pointing to a different device. 
392  */
393 class DCR {
394 public:
395    dlink dev_link;                    /* link to attach to dev */
396    JCR *jcr;                          /* pointer to JCR */
397    DEVICE *dev;                       /* pointer to device */
398    DEVRES *device;                    /* pointer to device resource */
399    DEV_BLOCK *block;                  /* pointer to block */
400    DEV_RECORD *rec;                   /* pointer to record */
401    int spool_fd;                      /* fd if spooling */
402    bool spool_data;                   /* set to spool data */
403    bool spooling;                     /* set when actually spooling */
404    bool despooling;                   /* set when despooling */
405    bool despool_wait;                 /* waiting for despooling */
406    bool dev_locked;                   /* set if dev already locked */
407    bool NewVol;                       /* set if new Volume mounted */
408    bool WroteVol;                     /* set if Volume written */
409    bool NewFile;                      /* set when EOF written */
410    bool reserved_device;              /* set if reserve done */
411    bool any_volume;                   /* Any OK for dir_find_next... */
412    uint32_t VolFirstIndex;            /* First file index this Volume */
413    uint32_t VolLastIndex;             /* Last file index this Volume */
414    uint32_t FileIndex;                /* Current File Index */
415    uint32_t EndFile;                  /* End file written */
416    uint32_t StartFile;                /* Start write file */
417    uint32_t StartBlock;               /* Start write block */
418    uint32_t EndBlock;                 /* Ending block written */
419    int64_t job_spool_size;            /* Current job spool size */
420    int64_t max_job_spool_size;        /* Max job spool size */
421    char VolumeName[MAX_NAME_LENGTH];  /* Volume name */
422    char pool_name[MAX_NAME_LENGTH];   /* pool name */
423    char pool_type[MAX_NAME_LENGTH];   /* pool type */
424    char media_type[MAX_NAME_LENGTH];  /* media type */
425    char dev_name[MAX_NAME_LENGTH];    /* dev name */
426    int Copy;                          /* identical copy number */
427    int Stripe;                        /* RAIT stripe */
428    VOLUME_CAT_INFO VolCatInfo;        /* Catalog info for desired volume */
429 };
430
431 /*
432  * Volume reservation class -- see reserve.c
433  */
434 class VOLRES { 
435 public:
436    dlink link;
437    char *vol_name;
438    DEVICE *dev;
439    DCR *dcr;
440 };
441
442
443 /* Get some definition of function to position
444  *  to the end of the medium in MTEOM. System
445  *  dependent. Arrgggg!
446  */
447 #ifndef MTEOM
448 #ifdef  MTSEOD
449 #define MTEOM MTSEOD
450 #endif
451 #ifdef MTEOD
452 #undef MTEOM
453 #define MTEOM MTEOD
454 #endif
455 #endif
456
457 #endif