]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/verify.c
Start of Solaris bare metal recovery
[bacula/bacula] / bacula / src / dird / verify.c
1 /*
2  *
3  *   Bacula Director -- verify.c -- responsible for running file verification
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  * 
10  * Current implementation is Catalog verification only (i.e. no
11  *  verification versus tape).
12  *
13  *  Basic tasks done here:
14  *     Open DB
15  *     Open connection with File daemon and pass him commands
16  *       to do the verify.
17  *     When the File daemon sends the attributes, compare them to
18  *       what is in the DB.
19  *
20  *   Version $Id$
21  */
22
23 /*
24    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25
26    This program is free software; you can redistribute it and/or
27    modify it under the terms of the GNU General Public License as
28    published by the Free Software Foundation; either version 2 of
29    the License, or (at your option) any later version.
30
31    This program is distributed in the hope that it will be useful,
32    but WITHOUT ANY WARRANTY; without even the implied warranty of
33    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34    General Public License for more details.
35
36    You should have received a copy of the GNU General Public
37    License along with this program; if not, write to the Free
38    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39    MA 02111-1307, USA.
40
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45 #include "findlib/find.h"
46
47 /* Imported Global Variables */
48 extern int debug_level;
49
50 /* Commands sent to File daemon */
51 static char verifycmd[]    = "verify level=%s\n";
52 static char storaddr[]     = "storage address=%s port=%d\n";
53 static char sessioncmd[]   = "session %s %ld %ld %ld %ld %ld %ld\n";  
54
55 /* Responses received from File daemon */
56 static char OKverify[]    = "2000 OK verify\n";
57 static char OKstore[]     = "2000 OK storage\n";
58 static char OKsession[]   = "2000 OK session\n";
59
60 /* Forward referenced functions */
61 static void verify_cleanup(JCR *jcr, int TermCode);
62 static void prt_fname(JCR *jcr);
63 static int missing_handler(void *ctx, int num_fields, char **row);
64
65 /* 
66  * Do a verification of the specified files against the Catlaog
67  *    
68  *  Returns:  0 on failure
69  *            1 on success
70  */
71 int do_verify(JCR *jcr) 
72 {
73    char *level;
74    BSOCK   *fd;
75    JOB_DBR jr;
76    JobId_t JobId = 0;
77
78    if (!get_or_create_client_record(jcr)) {
79       goto bail_out;
80    }
81
82    Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
83
84    /* If we are doing a verify from the catalog,
85     * we must look up the time and date of the
86     * last full verify.
87     */
88    if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
89       memcpy(&jr, &(jcr->jr), sizeof(jr));
90       if (!db_find_last_jobid(jcr->db, &jr)) {
91          Jmsg(jcr, M_FATAL, 0, _(
92               "Unable to find JobId of previous InitCatalog Job.\n"
93               "Please run a Verify with Level=InitCatalog before\n"
94               "running the current Job.\n"));
95          goto bail_out;
96       }
97       JobId = jr.JobId;
98       Dmsg1(20, "Last full id=%d\n", JobId);
99    } 
100
101    jcr->jr.JobId = jcr->JobId;
102    jcr->jr.StartTime = jcr->start_time;
103    jcr->jr.Level = jcr->JobLevel;
104    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
105       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
106       goto bail_out;
107    }
108
109    if (!jcr->fname) {
110       jcr->fname = (char *) get_pool_memory(PM_FNAME);
111    }
112
113    jcr->jr.JobId = JobId;      /* save target JobId */
114
115    /* Print Job Start message */
116    Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
117       jcr->JobId, jcr->Job);
118
119    if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
120       memset(&jr, 0, sizeof(jr));
121       jr.JobId = JobId;
122       if (!db_get_job_record(jcr->db, &jr)) {
123          Jmsg(jcr, M_FATAL, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
124          goto bail_out;
125       }
126       if (jr.JobStatus != 'T') {
127          Jmsg(jcr, M_FATAL, 0, _("Last Job %d did not terminate normally. JobStatus=%c\n"),
128             JobId, jr.JobStatus);
129          goto bail_out;
130       }
131       Jmsg(jcr, M_INFO, 0, _("Verifying against JobId=%d Job=%s\n"),
132          JobId, jr.Job); 
133    }
134
135    /* 
136     * If we are verifing a Volume, we need the Storage
137     *   daemon, so open a connection, otherwise, just
138     *   create a dummy authorization key (passed to
139     *   File daemon but not used).
140     */
141    if (jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
142       /*
143        * Now find the Volumes we will need for the Verify 
144        */
145       jcr->VolumeName[0] = 0;
146       if (!db_get_job_volume_names(jcr->db, jr.JobId, &jcr->VolumeName) ||
147            jcr->VolumeName[0] == 0) {
148          Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for verify JobId=%d. %s"), 
149             jr.JobId, db_strerror(jcr->db));
150          goto bail_out;
151       }
152       Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
153       /*
154        * Start conversation with Storage daemon  
155        */
156       jcr->JobStatus = JS_Blocked;
157       if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
158          goto bail_out;
159       }
160       /*
161        * Now start a job with the Storage daemon
162        */
163       if (!start_storage_daemon_job(jcr)) {
164          goto bail_out;
165       }
166       /*
167        * Now start a Storage daemon message thread
168        */
169       if (!start_storage_daemon_message_thread(jcr)) {
170          goto bail_out;
171       }
172       Dmsg0(50, "Storage daemon connection OK\n");
173    } else {
174       jcr->sd_auth_key = bstrdup("dummy");    /* dummy Storage daemon key */
175    }
176    /*
177     * OK, now connect to the File daemon
178     *  and ask him for the files.
179     */
180    jcr->JobStatus = JS_Blocked;
181    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
182       goto bail_out;
183    }
184
185    jcr->JobStatus = JS_Running;
186    fd = jcr->file_bsock;
187
188    Dmsg0(30, ">filed: Send include list\n");
189    if (!send_include_list(jcr)) {
190       goto bail_out;
191    }
192
193    Dmsg0(30, ">filed: Send exclude list\n");
194    if (!send_exclude_list(jcr)) {
195       goto bail_out;
196    }
197
198    /* 
199     * Send Level command to File daemon, as well
200     *   as the Storage address if appropriate.
201     */
202    switch (jcr->JobLevel) {
203       case L_VERIFY_INIT:
204          level = "init";
205          break;
206       case L_VERIFY_CATALOG:
207          level = "catalog";
208          break;
209       case L_VERIFY_VOLUME_TO_CATALOG:
210          /* 
211           * send Storage daemon address to the File daemon
212           */
213          if (jcr->store->SDDport == 0) {
214             jcr->store->SDDport = jcr->store->SDport;
215          }
216          bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
217          if (!response(fd, OKstore, "Storage")) {
218             goto bail_out;
219          }
220          /*
221           * Pass the VolSessionId, VolSessionTime, Start and
222           * end File and Blocks on the session command.
223           */
224          bnet_fsend(fd, sessioncmd, 
225                    jcr->VolumeName,
226                    jr.VolSessionId, jr.VolSessionTime, 
227                    jr.StartFile, jr.EndFile, jr.StartBlock, 
228                    jr.EndBlock);
229          if (!response(fd, OKsession, "Session")) {
230             goto bail_out;
231          }
232          level = "volume";
233          break;
234       case L_VERIFY_DATA:
235          level = "data";
236          break;
237       default:
238          Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
239          goto bail_out;
240    }
241
242    /* 
243     * Send verify command/level to File daemon
244     */
245    bnet_fsend(fd, verifycmd, level);
246    if (!response(fd, OKverify, "Verify")) {
247       goto bail_out;
248    }
249
250    /*
251     * Now get data back from File daemon and
252     *  compare it to the catalog or store it in the
253     *  catalog depending on the run type.
254     */
255    /* Compare to catalog */
256    switch (jcr->JobLevel) { 
257    case L_VERIFY_CATALOG:
258       Dmsg0(10, "Verify level=catalog\n");
259       get_attributes_and_compare_to_catalog(jcr, JobId);
260       break;
261
262    case L_VERIFY_VOLUME_TO_CATALOG:
263       int stat;
264       Dmsg0(10, "Verify level=volume\n");
265       get_attributes_and_compare_to_catalog(jcr, JobId);
266       stat = jcr->JobStatus;
267       jcr->JobStatus = JS_WaitSD;
268       wait_for_storage_daemon_termination(jcr);
269       /* If we terminate normally, use SD term code, else, use ours */
270       if (stat == JS_Terminated) {
271          jcr->JobStatus = jcr->SDJobStatus;
272       } else {
273          jcr->JobStatus = stat;
274       }
275       break;
276
277    case L_VERIFY_INIT:
278       /* Build catalog */
279       Dmsg0(10, "Verify level=init\n");
280       get_attributes_and_put_in_catalog(jcr);
281       break;
282
283    default:
284       Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
285       goto bail_out;
286    }
287
288    verify_cleanup(jcr, jcr->JobStatus);
289    return 1;
290
291 bail_out:
292    verify_cleanup(jcr, JS_ErrorTerminated);
293    return 0;
294 }
295
296 /*
297  * Release resources allocated during backup.
298  *
299  */
300 static void verify_cleanup(JCR *jcr, int TermCode)
301 {
302    char sdt[50], edt[50];
303    char ec1[30];
304    char term_code[100];
305    char *term_msg;
306    int msg_type;
307    JobId_t JobId;
308
309    Dmsg0(100, "Enter verify_cleanup()\n");
310
311    JobId = jcr->jr.JobId;
312    jcr->JobStatus = TermCode;
313
314    update_job_end_record(jcr);
315
316    msg_type = M_INFO;                 /* by default INFO message */
317    switch (TermCode) {
318       case JS_Terminated:
319          term_msg = _("Verify OK");
320          break;
321       case JS_ErrorTerminated:
322          term_msg = _("*** Verify Error ***"); 
323          msg_type = M_ERROR;          /* Generate error message */
324          break;
325       case JS_Cancelled:
326          term_msg = _("Verify Cancelled");
327          break;
328       case JS_Differences:
329          term_msg = _("Verify Differences");
330          break;
331       default:
332          term_msg = term_code;
333          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
334          break;
335    }
336    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
337    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
338
339    Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
340 JobId:                  %d\n\
341 Job:                    %s\n\
342 FileSet:                %s\n\
343 Verify Level:           %s\n\
344 Client:                 %s\n\
345 Start time:             %s\n\
346 End time:               %s\n\
347 Files Examined:         %s\n\
348 Termination:            %s\n\n"),
349         edt,
350         jcr->jr.JobId,
351         jcr->jr.Job,
352         jcr->fileset->hdr.name,
353         level_to_str(jcr->JobLevel),
354         jcr->client->hdr.name,
355         sdt,
356         edt,
357         edit_uint64_with_commas(jcr->JobFiles, ec1),
358         term_msg);
359
360    Dmsg0(100, "Leave verify_cleanup()\n");
361    if (jcr->fname) {
362       free_memory(jcr->fname);
363       jcr->fname = NULL;
364    }
365 }
366
367 /*
368  * This routine is called only during a Verify
369  */
370 int get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
371 {
372    BSOCK   *fd;
373    int n, len;
374    FILE_DBR fdbr;
375    struct stat statf;                 /* file stat */
376    struct stat statc;                 /* catalog stat */
377    int stat = JS_Terminated;
378    char buf[MAXSTRING];
379    POOLMEM *fname = get_pool_memory(PM_MESSAGE);
380    int do_MD5 = FALSE;
381    long file_index = 0;
382
383    memset(&fdbr, 0, sizeof(FILE_DBR));
384    fd = jcr->file_bsock;
385    fdbr.JobId = JobId;
386    jcr->FileIndex = 0;
387    
388    Dmsg0(20, "bdird: waiting to receive file attributes\n");
389    /*
390     * Get Attributes and MD5 Signature from File daemon
391     * We expect:
392     *   FileIndex
393     *   Stream
394     *   Options or MD5
395     *   Filename
396     *   Attributes
397     *   Link name  ???
398     */
399    while ((n=bget_msg(fd, 0)) >= 0 && !job_cancelled(jcr)) {
400       int stream;
401       char *attr, *p, *fn;
402       char Opts_MD5[MAXSTRING];        /* Verify Opts or MD5 signature */
403
404       fname = check_pool_memory_size(fname, fd->msglen);
405       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
406       Dmsg1(400, "Atts+MD5=%s\n", fd->msg);
407       if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream, 
408             Opts_MD5)) != 3) {
409          Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
410  mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
411          goto bail_out;
412       }
413       p = fd->msg;
414       skip_nonspaces(&p);             /* skip FileIndex */
415       skip_spaces(&p);
416       skip_nonspaces(&p);             /* skip Stream */
417       skip_spaces(&p);
418       skip_nonspaces(&p);             /* skip Opts_MD5 */   
419       p++;                            /* skip space */
420       fn = fname;
421       while (*p != 0) {
422          *fn++ = *p++;                /* copy filename */
423       }
424       *fn = *p++;                     /* term filename and point to attribs */
425       attr = p;
426       /*
427        * Got attributes stream, decode it
428        */
429       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
430          Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
431          jcr->JobFiles++;
432          jcr->FileIndex = file_index;    /* remember attribute file_index */
433          decode_stat(attr, &statf);  /* decode file stat packet */
434          do_MD5 = FALSE;
435          jcr->fn_printed = FALSE;
436          strcpy(jcr->fname, fname);  /* move filename into JCR */
437
438          Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
439          Dmsg1(020, "dird<filed: attr=%s\n", attr);
440
441          /* 
442           * Find equivalent record in the database 
443           */
444          fdbr.FileId = 0;
445          if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
446             Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
447             Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
448             stat = JS_Differences;
449             continue;
450          } else {
451             /* 
452              * mark file record as visited by stuffing the
453              * current JobId, which is unique, into the MarkId field.
454              */
455             db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
456          }
457
458          Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname, 
459             file_index, Opts_MD5);
460          decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
461          /*
462           * Loop over options supplied by user and verify the
463           * fields he requests.
464           */
465          for (p=Opts_MD5; *p; p++) {
466             char ed1[30], ed2[30];
467             switch (*p) {
468             case 'i':                /* compare INODEs */
469                if (statc.st_ino != statf.st_ino) {
470                   prt_fname(jcr);
471                   Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %s File: %s\n"), 
472                      edit_uint64((uint64_t)statc.st_ino, ed1),
473                      edit_uint64((uint64_t)statf.st_ino, ed2));
474                   stat = JS_Differences;
475                }
476                break;
477             case 'p':                /* permissions bits */
478                if (statc.st_mode != statf.st_mode) {
479                   prt_fname(jcr);
480                   Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"), 
481                      (uint32_t)statc.st_mode, (uint32_t)statf.st_mode);
482                   stat = JS_Differences;
483                }
484                break;
485             case 'n':                /* number of links */
486                if (statc.st_nlink != statf.st_nlink) {
487                   prt_fname(jcr);
488                   Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"), 
489                      (uint32_t)statc.st_nlink, (uint32_t)statf.st_nlink);
490                   stat = JS_Differences;
491                }
492                break;
493             case 'u':                /* user id */
494                if (statc.st_uid != statf.st_uid) {
495                   prt_fname(jcr);
496                   Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %u File: %u\n"), 
497                      (uint32_t)statc.st_uid, (uint32_t)statf.st_uid);
498                   stat = JS_Differences;
499                }
500                break;
501             case 'g':                /* group id */
502                if (statc.st_gid != statf.st_gid) {
503                   prt_fname(jcr);
504                   Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %u File: %u\n"), 
505                      (uint32_t)statc.st_gid, (uint32_t)statf.st_gid);
506                   stat = JS_Differences;
507                }
508                break;
509             case 's':                /* size */
510                if (statc.st_size != statf.st_size) {
511                   prt_fname(jcr);
512                   Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %s File: %s\n"), 
513                      edit_uint64((uint64_t)statc.st_size, ed1),
514                      edit_uint64((uint64_t)statf.st_size, ed2));
515                   stat = JS_Differences;
516                }
517                break;
518             case 'a':                /* access time */
519                if (statc.st_atime != statf.st_atime) {
520                   prt_fname(jcr);
521                   Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
522                   stat = JS_Differences;
523                }
524                break;
525             case 'm':
526                if (statc.st_mtime != statf.st_mtime) {
527                   prt_fname(jcr);
528                   Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
529                   stat = JS_Differences;
530                }
531                break;
532             case 'c':                /* ctime */
533                if (statc.st_ctime != statf.st_ctime) {
534                   prt_fname(jcr);
535                   Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
536                   stat = JS_Differences;
537                }
538                break;
539             case 'd':                /* file size decrease */
540                if (statc.st_size > statf.st_size) {
541                   prt_fname(jcr);
542                   Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %s File: %s\n"), 
543                      edit_uint64((uint64_t)statc.st_size, ed1),
544                      edit_uint64((uint64_t)statf.st_size, ed2));
545                   stat = JS_Differences;
546                }
547                break;
548             case '5':                /* compare MD5 */
549                Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
550                do_MD5 = TRUE;
551                break;
552             case ':':
553             case 'V':
554             default:
555                break;
556             }
557          }
558       /*
559        * Got MD5 Signature from Storage daemon
560        *  It came across in the Opts_MD5 field.
561        */
562       } else if (stream == STREAM_MD5_SIGNATURE) {
563          Dmsg2(400, "stream=MD5 inx=%d MD5=%s\n", file_index, Opts_MD5);
564          /* 
565           * When ever we get an MD5 signature is MUST have been
566           * preceded by an attributes record, which sets attr_file_index
567           */
568          if (jcr->FileIndex != (uint32_t)file_index) {
569             Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
570                file_index, jcr->FileIndex);
571             goto bail_out;
572          } 
573          if (do_MD5) {
574             db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
575             if (strcmp(buf, fdbr.MD5) != 0) {
576                prt_fname(jcr);
577                if (debug_level >= 10) {
578                   Jmsg(jcr, M_INFO, 0, _("      MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
579                } else {
580                   Jmsg(jcr, M_INFO, 0, _("      MD5 differs.\n"));
581                }
582                stat = JS_Differences;
583             }
584             do_MD5 = FALSE;
585          }
586       }
587       jcr->JobFiles = file_index;
588    } 
589    if (is_bnet_error(fd)) {
590       Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
591                         n, strerror(errno));
592       goto bail_out;
593    }
594
595    /* Now find all the files that are missing -- i.e. all files in
596     *  the database where the MarkedId != current JobId
597     */
598    jcr->fn_printed = FALSE;
599    sprintf(buf, 
600 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
601 "WHERE File.JobId=%d "
602 "AND File.MarkedId!=%d AND File.PathId=Path.PathId "
603 "AND File.FilenameId=Filename.FilenameId", 
604       JobId, jcr->JobId);
605    /* missing_handler is called for each file found */
606    db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
607    if (jcr->fn_printed) {
608       stat = JS_Differences;
609    }
610    free_pool_memory(fname);
611    jcr->JobStatus = stat;
612    return 1;
613     
614 bail_out:
615    free_pool_memory(fname);
616    jcr->JobStatus = JS_ErrorTerminated;
617    return 0;
618 }
619
620 /*
621  * We are called here for each record that matches the above
622  *  SQL query -- that is for each file contained in the Catalog
623  *  that was not marked earlier. This means that the file in
624  *  question is a missing file (in the Catalog but on on Disk).
625  */
626 static int missing_handler(void *ctx, int num_fields, char **row)
627 {
628    JCR *jcr = (JCR *)ctx;
629
630    if (!jcr->fn_printed) {
631       Jmsg(jcr, M_INFO, 0, "\n");
632       Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
633       jcr->fn_printed = TRUE;
634    }
635    Jmsg(jcr, M_INFO, 0, "      %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
636    return 0;
637 }
638
639
640 /* 
641  * Print filename for verify
642  */
643 static void prt_fname(JCR *jcr)
644 {
645    if (!jcr->fn_printed) {
646       Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
647       jcr->fn_printed = TRUE;
648    }
649 }