Code

debian/patches: Removed bts498183-segfault-madvise.
[pkg-rrdtool.git] / debian / patches / bts499350-data-corruption
1 diff a/src/rrd_update.c b/src/rrd_update.c
2 --- a/src/rrd_update.c
3 +++ b/src/rrd_update.c
4 @@ -780,12 +780,10 @@ static int process_arg(
5      }
6      /* seek to the beginning of the rra's */
7      if (*rra_current != rra_begin) {
8 -#ifndef HAVE_MMAP
9          if (rrd_seek(rrd_file, rra_begin, SEEK_SET) != 0) {
10              rrd_set_error("seek error in rrd");
11              return -1;
12          }
13 -#endif
14          *rra_current = rra_begin;
15      }
16      rra_start = rra_begin;
17 @@ -1868,95 +1866,70 @@ static int write_to_rras(
18  {
19      unsigned long rra_idx;
20      unsigned long rra_start;
21 -    unsigned long rra_pos_tmp;  /* temporary byte pointer. */
22      time_t    rra_time = 0; /* time of update for a RRA */
23  
24 +    unsigned long ds_cnt = rrd->stat_head->ds_cnt;
25 +    
26      /* Ready to write to disk */
27      rra_start = rra_begin;
28 +
29      for (rra_idx = 0; rra_idx < rrd->stat_head->rra_cnt; rra_idx++) {
30 -        /* skip unless there's something to write */
31 -        if (rra_step_cnt[rra_idx]) {
32 -            /* write the first row */
33 +        rra_def_t *rra_def = &rrd->rra_def[rra_idx];
34 +        rra_ptr_t *rra_ptr = &rrd->rra_ptr[rra_idx];
35 +
36 +        /* for cdp_prep */
37 +        unsigned short scratch_idx;
38 +        unsigned long step_subtract;
39 +
40 +        for (scratch_idx = CDP_primary_val,
41 +                 step_subtract = 1;
42 +             rra_step_cnt[rra_idx] > 0;
43 +             rra_step_cnt[rra_idx]--,
44 +                 scratch_idx = CDP_secondary_val,
45 +                 step_subtract = 2) {
46 +
47 +            unsigned long rra_pos_new;
48  #ifdef DEBUG
49              fprintf(stderr, "  -- RRA Preseek %ld\n", rrd_file->pos);
50  #endif
51 -            rrd->rra_ptr[rra_idx].cur_row++;
52 -            if (rrd->rra_ptr[rra_idx].cur_row >=
53 -                rrd->rra_def[rra_idx].row_cnt)
54 -                rrd->rra_ptr[rra_idx].cur_row = 0;  /* wrap around */
55 -            /* position on the first row */
56 -            rra_pos_tmp = rra_start +
57 -                (rrd->stat_head->ds_cnt) * (rrd->rra_ptr[rra_idx].cur_row) *
58 -                sizeof(rrd_value_t);
59 -            if (rra_pos_tmp != *rra_current) {
60 -                if (rrd_seek(rrd_file, rra_pos_tmp, SEEK_SET) != 0) {
61 +            /* increment, with wrap-around */
62 +            if (++rra_ptr->cur_row >= rra_def->row_cnt)
63 +              rra_ptr->cur_row = 0;
64 +
65 +            /* we know what our position should be */
66 +            rra_pos_new = rra_start
67 +              + ds_cnt * rra_ptr->cur_row * sizeof(rrd_value_t);
68 +
69 +            /* re-seek if the position is wrong or we wrapped around */
70 +            if (rra_pos_new != *rra_current || rra_ptr->cur_row == 0) {
71 +                if (rrd_seek(rrd_file, rra_pos_new, SEEK_SET) != 0) {
72                      rrd_set_error("seek error in rrd");
73                      return -1;
74                  }
75 -                *rra_current = rra_pos_tmp;
76 +                *rra_current = rra_pos_new;
77              }
78  #ifdef DEBUG
79              fprintf(stderr, "  -- RRA Postseek %ld\n", rrd_file->pos);
80  #endif
81 -            if (!skip_update[rra_idx]) {
82 -                if (*pcdp_summary != NULL) {
83 -                    rra_time = (current_time - current_time
84 -                                % (rrd->rra_def[rra_idx].pdp_cnt *
85 -                                   rrd->stat_head->pdp_step))
86 -                        -
87 -                        ((rra_step_cnt[rra_idx] -
88 -                          1) * rrd->rra_def[rra_idx].pdp_cnt *
89 -                         rrd->stat_head->pdp_step);
90 -                }
91 -                if (write_RRA_row
92 -                    (rrd_file, rrd, rra_idx, rra_current, CDP_primary_val,
93 -                     pcdp_summary, rra_time) == -1)
94 -                    return -1;
95 -            }
96  
97 -            /* write other rows of the bulk update, if any */
98 -            for (; rra_step_cnt[rra_idx] > 1; rra_step_cnt[rra_idx]--) {
99 -                if (++rrd->rra_ptr[rra_idx].cur_row ==
100 -                    rrd->rra_def[rra_idx].row_cnt) {
101 -#ifdef DEBUG
102 -                    fprintf(stderr,
103 -                            "Wraparound for RRA %s, %lu updates left\n",
104 -                            rrd->rra_def[rra_idx].cf_nam,
105 -                            rra_step_cnt[rra_idx] - 1);
106 -#endif
107 -                    /* wrap */
108 -                    rrd->rra_ptr[rra_idx].cur_row = 0;
109 -                    /* seek back to beginning of current rra */
110 -                    if (rrd_seek(rrd_file, rra_start, SEEK_SET) != 0) {
111 -                        rrd_set_error("seek error in rrd");
112 -                        return -1;
113 -                    }
114 -#ifdef DEBUG
115 -                    fprintf(stderr, "  -- Wraparound Postseek %ld\n",
116 -                            rrd_file->pos);
117 -#endif
118 -                    *rra_current = rra_start;
119 -                }
120 -                if (!skip_update[rra_idx]) {
121 -                    if (*pcdp_summary != NULL) {
122 -                        rra_time = (current_time - current_time
123 -                                    % (rrd->rra_def[rra_idx].pdp_cnt *
124 -                                       rrd->stat_head->pdp_step))
125 -                            -
126 -                            ((rra_step_cnt[rra_idx] -
127 -                              2) * rrd->rra_def[rra_idx].pdp_cnt *
128 -                             rrd->stat_head->pdp_step);
129 -                    }
130 -                    if (write_RRA_row(rrd_file, rrd, rra_idx, rra_current,
131 -                                      CDP_secondary_val, pcdp_summary,
132 -                                      rra_time) == -1)
133 -                        return -1;
134 -                }
135 +            if (skip_update[rra_idx])
136 +                continue;
138 +            if (*pcdp_summary != NULL) {
139 +                unsigned long step_time = rra_def->pdp_cnt * rrd->stat_head->pdp_step;
141 +                rra_time = (current_time - current_time % step_time)
142 +                    - ((rra_step_cnt[rra_idx] - step_subtract) * step_time);
143              }
145 +            if (write_RRA_row
146 +                (rrd_file, rrd, rra_idx, rra_current, scratch_idx,
147 +                 pcdp_summary, rra_time) == -1)
148 +                return -1;
149          }
150 -        rra_start += rrd->rra_def[rra_idx].row_cnt * rrd->stat_head->ds_cnt *
151 -            sizeof(rrd_value_t);
152 -    }                   /* RRA LOOP */
154 +        rra_start += rra_def->row_cnt * ds_cnt * sizeof(rrd_value_t);
155 +    } /* RRA LOOP */
156  
157      return 0;
158  }