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