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