]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/jcr.h
d7b6b9e816efa01fc778f934da6896540c7c7b0b
[bacula/bacula] / bacula / src / jcr.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Bacula JCR Structure definition for Daemons and the Library
30  *  This definition consists of a "Global" definition common
31  *  to all daemons and used by the library routines, and a
32  *  daemon specific part that is enabled with #defines.
33  *
34  * Kern Sibbald, Nov MM
35  *
36  *   Version $Id$
37  */
38
39
40 #ifndef __JCR_H_
41 #define __JCR_H_ 1
42
43 /* Backup/Verify level code. These are stored in the DB */
44 #define L_FULL                   'F'  /* Full backup */
45 #define L_INCREMENTAL            'I'  /* since last backup */
46 #define L_DIFFERENTIAL           'D'  /* since last full backup */
47 #define L_SINCE                  'S'
48 #define L_VERIFY_CATALOG         'C'  /* verify from catalog */
49 #define L_VERIFY_INIT            'V'  /* verify save (init DB) */
50 #define L_VERIFY_VOLUME_TO_CATALOG 'O'  /* verify Volume to catalog entries */
51 #define L_VERIFY_DISK_TO_CATALOG 'd'  /* verify Disk attributes to catalog */
52 #define L_VERIFY_DATA            'A'  /* verify data on volume */
53 #define L_BASE                   'B'  /* Base level job */
54 #define L_NONE                   ' '  /* None, for Restore and Admin */
55
56
57 /* Job Types. These are stored in the DB */
58 #define JT_BACKUP                'B'  /* Backup Job */
59 #define JT_MIGRATED_JOB          'M'  /* A previous backup job that was migrated */
60 #define JT_VERIFY                'V'  /* Verify Job */
61 #define JT_RESTORE               'R'  /* Restore Job */
62 #define JT_CONSOLE               'c'  /* console program */
63 #define JT_SYSTEM                'I'  /* internal system "job" */
64 #define JT_ADMIN                 'D'  /* admin job */
65 #define JT_ARCHIVE               'A'  /* Archive Job */
66 #define JT_COPY                  'C'  /* Copy Job */
67 #define JT_MIGRATE               'g'  /* Migration Job */
68 #define JT_SCAN                  'S'  /* Scan Job */
69
70 /* Job Status. Some of these are stored in the DB */
71 #define JS_Created               'C'  /* created but not yet running */
72 #define JS_Running               'R'  /* running */
73 #define JS_Blocked               'B'  /* blocked */
74 #define JS_Terminated            'T'  /* terminated normally */
75 #define JS_ErrorTerminated       'E'  /* Job terminated in error */
76 #define JS_Error                 'e'  /* Non-fatal error */
77 #define JS_FatalError            'f'  /* Fatal error */
78 #define JS_Differences           'D'  /* Verify differences */
79 #define JS_Canceled              'A'  /* canceled by user */
80 #define JS_WaitFD                'F'  /* waiting on File daemon */
81 #define JS_WaitSD                'S'  /* waiting on the Storage daemon */
82 #define JS_WaitMedia             'm'  /* waiting for new media */
83 #define JS_WaitMount             'M'  /* waiting for Mount */
84 #define JS_WaitStoreRes          's'  /* Waiting for storage resource */
85 #define JS_WaitJobRes            'j'  /* Waiting for job resource */
86 #define JS_WaitClientRes         'c'  /* Waiting for Client resource */
87 #define JS_WaitMaxJobs           'd'  /* Waiting for maximum jobs */
88 #define JS_WaitStartTime         't'  /* Waiting for start time */
89 #define JS_WaitPriority          'p'  /* Waiting for higher priority jobs to finish */
90 #define JS_AttrDespooling        'a'  /* SD despooling attributes */
91 #define JS_AttrInserting         'i'  /* Doing batch insert file records */
92 #define JS_DataDespooling        'l'  /* Doing data despooling */
93 #define JS_DataCommitting        'L'  /* Committing data (last despool) */
94
95 /* Migration selection types */
96 enum {
97    MT_SMALLEST_VOL = 1,
98    MT_OLDEST_VOL,
99    MT_POOL_OCCUPANCY,
100    MT_POOL_TIME,
101    MT_CLIENT,
102    MT_VOLUME,
103    MT_JOB,
104    MT_SQLQUERY
105 };
106
107 #define job_canceled(jcr) \
108   (jcr->JobStatus == JS_Canceled || \
109    jcr->JobStatus == JS_ErrorTerminated || \
110    jcr->JobStatus == JS_FatalError)
111
112 #define foreach_jcr(jcr) \
113    for (jcr=jcr_walk_start(); jcr; (jcr=jcr_walk_next(jcr)) )
114
115 #define endeach_jcr(jcr) jcr_walk_end(jcr)
116
117 #define SD_APPEND 1
118 #define SD_READ   0
119
120 /* Forward referenced structures */
121 class JCR;
122 struct FF_PKT;
123 struct B_DB;
124 struct ATTR_DBR;
125
126 #ifdef FILE_DAEMON
127 struct CRYPTO_CTX {
128    bool pki_sign;                     /* Enable PKI Signatures? */
129    bool pki_encrypt;                  /* Enable PKI Encryption? */
130    DIGEST *digest;                    /* Last file's digest context */
131    X509_KEYPAIR *pki_keypair;         /* Encryption key pair */
132    alist *pki_signers;                /* Trusted Signers */
133    alist *pki_recipients;             /* Trusted Recipients */
134    CRYPTO_SESSION *pki_session;       /* PKE Public Keys + Symmetric Session Keys */
135    POOLMEM *pki_session_encoded;      /* Cached DER-encoded copy of pki_session */
136    int32_t pki_session_encoded_size;  /* Size of DER-encoded pki_session */
137    POOLMEM *crypto_buf;               /* Encryption/Decryption buffer */
138 };
139 #endif
140
141 typedef void (JCR_free_HANDLER)(JCR *jcr);
142
143 /* Job Control Record (JCR) */
144 class JCR {
145 private:
146    pthread_mutex_t mutex;             /* jcr mutex */
147    volatile int _use_count;           /* use count */
148 public:
149    void lock() {P(mutex); };
150    void unlock() {V(mutex); };
151    void inc_use_count(void) {lock(); _use_count++; unlock(); };
152    void dec_use_count(void) {lock(); _use_count--; unlock(); };
153    int  use_count() { return _use_count; };
154    void init_mutex(void) {pthread_mutex_init(&mutex, NULL); };
155    void destroy_mutex(void) {pthread_mutex_destroy(&mutex); };
156    bool is_job_canceled() {return job_canceled(this); };
157
158    /* Global part of JCR common to all daemons */
159    dlink link;                        /* JCR chain link */
160    pthread_t my_thread_id;            /* id of thread controlling jcr */
161    BSOCK *dir_bsock;                  /* Director bsock or NULL if we are him */
162    BSOCK *store_bsock;                /* Storage connection socket */
163    BSOCK *file_bsock;                 /* File daemon connection socket */
164    JCR_free_HANDLER *daemon_free_jcr; /* Local free routine */
165    dlist *msg_queue;                  /* Queued messages */
166    alist job_end_push;                /* Job end pushed calls */
167    bool dequeuing;                    /* dequeuing messages */
168    POOLMEM *VolumeName;               /* Volume name desired -- pool_memory */
169    POOLMEM *errmsg;                   /* edited error message */
170    char Job[MAX_NAME_LENGTH];         /* Unique name of this Job */
171    char event[MAX_NAME_LENGTH];       /* Current event */
172    uint32_t JobId;                    /* Director's JobId */
173    uint32_t VolSessionId;
174    uint32_t VolSessionTime;
175    uint32_t JobFiles;                 /* Number of files written, this job */
176    uint32_t JobErrors;                /* */
177    uint64_t JobBytes;                 /* Number of bytes processed this job */
178    uint64_t ReadBytes;                /* Bytes read -- before compression */
179    uint32_t Errors;                   /* Number of non-fatal errors */
180    volatile int JobStatus;            /* ready, running, blocked, terminated */
181    int JobType;                       /* backup, restore, verify ... */
182    int JobLevel;                      /* Job level */
183    int JobPriority;                   /* Job priority */
184    time_t sched_time;                 /* job schedule time, i.e. when it should start */
185    time_t start_time;                 /* when job actually started */
186    time_t run_time;                   /* used for computing speed */
187    time_t end_time;                   /* job end time */
188    POOLMEM *client_name;              /* client name */
189    POOLMEM *RestoreBootstrap;         /* Bootstrap file to restore */
190    POOLMEM *stime;                    /* start time for incremental/differential */
191    char *sd_auth_key;                 /* SD auth key */
192    MSGS *jcr_msgs;                    /* Copy of message resource -- actually used */
193    uint32_t ClientId;                 /* Client associated with Job */
194    char *where;                       /* prefix to restore files to */
195    char *RegexWhere;                  /* file relocation in restore */
196    alist *where_bregexp;              /* BREGEXP alist for path manipulation */
197    int cached_pnl;                    /* cached path length */
198    POOLMEM *cached_path;              /* cached path */
199    bool prefix_links;                 /* Prefix links with Where path */
200    bool gui;                          /* set if gui using console */
201    bool authenticated;                /* set when client authenticated */
202    void *Python_job;                  /* Python Job Object */
203    void *Python_events;               /* Python Events Object */
204
205    bool cached_attribute;             /* set if attribute is cached */
206    POOLMEM *attr;                     /* Attribute string from SD */
207    B_DB *db;                          /* database pointer */
208    B_DB *db_batch;                    /* database pointer for batch insert */
209    ATTR_DBR *ar;                      /* DB attribute record */
210    guid_list *id_list;                /* User/group id to name list */
211
212    void *plugin_ctx_list;             /* list of contexts for plugins */
213    void *plugin_ctx;                  /* current plugin context */
214    void *plugin;                      /* plugin instance */
215
216    /* Daemon specific part of JCR */
217    /* This should be empty in the library */
218
219 #ifdef DIRECTOR_DAEMON
220    /* Director Daemon specific data part of JCR */
221    pthread_t SD_msg_chan;             /* Message channel thread id */
222    pthread_cond_t term_wait;          /* Wait for job termination */
223    workq_ele_t *work_item;            /* Work queue item if scheduled */
224    volatile bool sd_msg_thread_done;  /* Set when Storage message thread terms */
225    BSOCK *ua;                         /* User agent */
226    JOB *job;                          /* Job resource */
227    JOB *verify_job;                   /* Job resource of verify previous job */
228    alist *rstorage;                   /* Read storage possibilities */
229    STORE *rstore;                     /* Selected read storage */
230    alist *wstorage;                   /* Write storage possibilities */
231    STORE *wstore;                     /* Selected write storage */
232    CLIENT *client;                    /* Client resource */
233    POOL *pool;                        /* Pool resource = write for migration */
234    POOL *rpool;                       /* Read pool. Used only in migration */
235    POOL *full_pool;                   /* Full backup pool resource */
236    POOL *inc_pool;                    /* Incremental backup pool resource */
237    POOL *diff_pool;                   /* Differential backup pool resource */
238    bool run_pool_override;
239    bool run_full_pool_override;
240    bool run_inc_pool_override;
241    bool run_diff_pool_override;
242    FILESET *fileset;                  /* FileSet resource */
243    CAT *catalog;                      /* Catalog resource */
244    MSGS *messages;                    /* Default message handler */
245    uint32_t SDJobFiles;               /* Number of files written, this job */
246    uint64_t SDJobBytes;               /* Number of bytes processed this job */
247    uint32_t SDErrors;                 /* Number of non-fatal errors */
248    volatile int SDJobStatus;          /* Storage Job Status */
249    volatile int FDJobStatus;          /* File daemon Job Status */
250    uint32_t ExpectedFiles;            /* Expected restore files */
251    uint32_t MediaId;                  /* DB record IDs associated with this job */
252    FileId_t FileId;                   /* Last file id inserted */
253    uint32_t FileIndex;                /* Last FileIndex processed */
254    POOLMEM *fname;                    /* name to put into catalog */
255    JOB_DBR jr;                        /* Job DB record for current job */
256    JOB_DBR previous_jr;               /* previous job database record */
257    JOB *previous_job;                 /* Job resource of migration previous job */
258    JCR *mig_jcr;                      /* JCR for migration/copy job */
259    char FSCreateTime[MAX_TIME_LENGTH]; /* FileSet CreateTime as returned from DB */
260    char since[MAX_TIME_LENGTH];       /* since time */
261    union {
262       JobId_t RestoreJobId;           /* Id specified by UA */
263       JobId_t MigrateJobId;
264    };
265    POOLMEM *client_uname;             /* client uname */
266    POOLMEM *pool_source;              /* Where pool came from */
267    POOLMEM *rpool_source;             /* Where migrate read pool came from */
268    POOLMEM *rstore_source;            /* Where read storage came from */
269    POOLMEM *wstore_source;            /* Where write storage came from */
270    int replace;                       /* Replace option */
271    int NumVols;                       /* Number of Volume used in pool */
272    int reschedule_count;              /* Number of times rescheduled */
273    int64_t spool_size;                /* Spool size for this job */
274    bool spool_data;                   /* Spool data in SD */
275    bool acquired_resource_locks;      /* set if resource locks acquired */
276    bool term_wait_inited;             /* Set when cond var inited */
277    bool fn_printed;                   /* printed filename */
278    bool write_part_after_job;         /* Write part after job in SD */
279    bool needs_sd;                     /* set if SD needed by Job */
280    bool cloned;                       /* set if cloned */
281    bool unlink_bsr;                   /* Unlink bsr file created */
282    bool VSS;                          /* VSS used by FD */
283    bool Encrypt;                      /* Encryption used by FD */
284 #endif /* DIRECTOR_DAEMON */
285
286
287 #ifdef FILE_DAEMON
288    /* File Daemon specific part of JCR */
289    uint32_t num_files_examined;       /* files examined this job */
290    POOLMEM *last_fname;               /* last file saved/verified */
291    POOLMEM *acl_text;                 /* text of ACL for backup */
292    int last_type;                     /* type of last file saved/verified */
293    /*********FIXME********* add missing files and files to be retried */
294    int incremental;                   /* set if incremental for SINCE */
295    time_t mtime;                      /* begin time for SINCE */
296    int listing;                       /* job listing in estimate */
297    long Ticket;                       /* Ticket */
298    char *big_buf;                     /* I/O buffer */
299    POOLMEM *compress_buf;             /* Compression buffer */
300    int32_t compress_buf_size;         /* Length of compression buffer */
301    void *pZLIB_compress_workset;      /* zlib compression session data */
302    int replace;                       /* Replace options */
303    int buf_size;                      /* length of buffer */
304    FF_PKT *ff;                        /* Find Files packet */
305    char stored_addr[MAX_NAME_LENGTH]; /* storage daemon address */
306    uint32_t StartFile;
307    uint32_t EndFile;
308    uint32_t StartBlock;
309    uint32_t EndBlock;
310    pthread_t heartbeat_id;            /* id of heartbeat thread */
311    BSOCK *hb_bsock;                   /* duped SD socket */
312    BSOCK *hb_dir_bsock;               /* duped DIR socket */
313    alist *RunScripts;                 /* Commands to run before and after job */
314    CRYPTO_CTX crypto;                 /* Crypto ctx */
315    DIRRES* director;                  /* Director resource */
316    bool VSS;                          /* VSS used by FD */
317 #endif /* FILE_DAEMON */
318
319
320 #ifdef STORAGE_DAEMON
321    /* Storage Daemon specific part of JCR */
322    JCR *next_dev;                     /* next JCR attached to device */
323    JCR *prev_dev;                     /* previous JCR attached to device */
324    char *dir_auth_key;                /* Dir auth key */
325    pthread_cond_t job_start_wait;     /* Wait for FD to start Job */
326    int type;
327    DCR *read_dcr;                     /* device context for reading */
328    DCR *dcr;                          /* device context record */
329    alist *dcrs;                       /* list of dcrs open */
330    POOLMEM *job_name;                 /* base Job name (not unique) */
331    POOLMEM *fileset_name;             /* FileSet */
332    POOLMEM *fileset_md5;              /* MD5 for FileSet */
333    VOL_LIST *VolList;                 /* list to read */
334    int32_t NumWriteVolumes;           /* number of volumes written */
335    int32_t NumReadVolumes;            /* total number of volumes to read */
336    int32_t CurReadVolume;             /* current read volume number */
337    int label_errors;                  /* count of label errors */
338    bool session_opened;
339    long Ticket;                       /* ticket for this job */
340    bool ignore_label_errors;          /* ignore Volume label errors */
341    bool spool_attributes;             /* set if spooling attributes */
342    bool no_attributes;                /* set if no attributes wanted */
343    int64_t spool_size;                /* Spool size for this job */
344    bool spool_data;                   /* set to spool data */
345    int CurVol;                        /* Current Volume count */
346    DIRRES* director;                  /* Director resource */
347    alist *write_store;                /* list of write storage devices sent by DIR */ 
348    alist *read_store;                 /* list of read devices sent by DIR */
349    alist *reserve_msgs;               /* reserve fail messages */
350    bool write_part_after_job;         /* Set to write part after job */
351    bool PreferMountedVols;            /* Prefer mounted vols rather than new */
352    
353    uint32_t FileId;                   /* Last file id inserted */
354
355    /* Parmaters for Open Read Session */
356    BSR *bsr;                          /* Bootstrap record -- has everything */
357    bool mount_next_volume;            /* set to cause next volume mount */
358    uint32_t read_VolSessionId;
359    uint32_t read_VolSessionTime;
360    uint32_t read_StartFile;
361    uint32_t read_EndFile;
362    uint32_t read_StartBlock;
363    uint32_t read_EndBlock;
364    /* Device wait times */
365    int min_wait;
366    int max_wait;
367    int max_num_wait;
368    int wait_sec;
369    int rem_wait_sec;
370    int num_wait;
371
372 #endif /* STORAGE_DAEMON */
373
374 };
375
376 /*
377  * Structure for all daemons that keeps some summary
378  *  info on the last job run.
379  */
380 struct s_last_job {
381    dlink link;
382    int Errors;                        /* FD/SD errors */
383    int JobType;
384    int JobStatus;
385    int JobLevel;
386    uint32_t JobId;
387    uint32_t VolSessionId;
388    uint32_t VolSessionTime;
389    uint32_t JobFiles;
390    uint64_t JobBytes;
391    time_t start_time;
392    time_t end_time;
393    char Job[MAX_NAME_LENGTH];
394 };
395
396 extern struct s_last_job last_job;
397 extern DLL_IMP_EXP dlist * last_jobs;
398
399
400 /* The following routines are found in lib/jcr.c */
401 extern bool init_jcr_subsystem(void);
402 extern JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr);
403 extern JCR *get_jcr_by_id(uint32_t JobId);
404 extern JCR *get_jcr_by_session(uint32_t SessionId, uint32_t SessionTime);
405 extern JCR *get_jcr_by_partial_name(char *Job);
406 extern JCR *get_jcr_by_full_name(char *Job);
407 extern JCR *get_next_jcr(JCR *jcr);
408 extern void set_jcr_job_status(JCR *jcr, int JobStatus);
409 extern int DLL_IMP_EXP num_jobs_run;
410
411 #ifdef DEBUG
412 extern void b_free_jcr(const char *file, int line, JCR *jcr);
413 #define free_jcr(jcr) b_free_jcr(__FILE__, __LINE__, (jcr))
414 #else
415 extern void free_jcr(JCR *jcr);
416 #endif
417
418 #endif /* __JCR_H_ */