Codebase list cyrus-imapd / lintian-fixes/main imap / partlist.c
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

partlist.c @lintian-fixes/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* partlist.c - Partition/backend selection functions
 *
 * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>


#include "libconfig.h"
#include "partlist.h"
#include "util.h"
#include "xmalloc.h"


#define FREE(var) if (var) { free(var); (var) = NULL; }

static void partlist_bump_action(partlist_t *part_list);
static int partlist_selectpart_index(partlist_t *part_list);
static void partlist_compute_quota(partlist_t *part_list);
static void partlist_fill(const char *key, const char *value, void *rock);
static void partition_filldata(partlist_t *part_list, int idx);

typedef struct partlist_conf {
    partlist_t  *part_list;
    const char  *key_prefix;
    size_t      excluded_count;
    const char  **excluded_item;
} partlist_conf_t;

static partlist_t *partlist_local = NULL;


EXPORTED partmode_t partlist_getmode(const char *mode)
{
    if (!strcmp(mode, "freespace-most")) {
        return PART_SELECT_MODE_FREESPACE_MOST;
    }
    else if (!strcmp(mode, "freespace-percent-most")) {
        return PART_SELECT_MODE_FREESPACE_PERCENT_MOST;
    }
    else if (!strcmp(mode, "freespace-percent-weighted")) {
        return PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED;
    }
    else if (!strcmp(mode, "freespace-percent-weighted-delta")) {
        return PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED_DELTA;
    }
    else {
        return PART_SELECT_MODE_RANDOM;
    }
}


EXPORTED void partlist_initialize(partlist_t *part_list, cb_part_filldata filldata,
                         const char *key_prefix, const char *key_value,
                         const char *excluded, partmode_t mode,
                         int soft_usage_limit, int reinit)
{
    partlist_conf_t part_list_conf;
    char *excluded_parts = NULL;

    srand(time(NULL));
    memset(&part_list_conf, 0, sizeof(part_list_conf));
    memset(part_list, 0, sizeof(partlist_t));

    part_list->mode = mode;
    part_list->filldata = (filldata ? filldata : partition_filldata);
    part_list->size = 0;
    part_list->soft_usage_limit = soft_usage_limit;
    part_list->reinit = reinit;

    part_list_conf.part_list = part_list;
    part_list_conf.key_prefix = key_prefix;

    if (excluded && (strlen(excluded) > 0)) {
        char *item = NULL;
        char *lasts;

        excluded_parts = xstrdup(excluded);
        item = strtok_r(excluded_parts, " ,\t", &lasts);
        while (item) {
            part_list_conf.excluded_item = (const char **)xrealloc(part_list_conf.excluded_item, (part_list_conf.excluded_count+1) * sizeof(const char *));
            part_list_conf.excluded_item[part_list_conf.excluded_count++] = item;
            item = strtok_r(NULL, " ,\t", &lasts);
        }
    }

    if (key_value) {
        char *items = xstrdup(key_value);
        char *item = NULL;
        char *lasts;

        item = strtok_r(items, " ,\t", &lasts);
        while (item) {
            partlist_fill(item, item, &part_list_conf);
            item = strtok_r(NULL, " ,\t", &lasts);
        }

        FREE(items);
    }
    else {
        config_foreachoverflowstring(partlist_fill, &part_list_conf);
    }

    /* excluded items no more necessary */
    FREE(excluded_parts);
    FREE(part_list_conf.excluded_item);

    partlist_compute_quota(part_list);
}


EXPORTED void partlist_free(partlist_t *part_list)
{
    int i;

    if (part_list == NULL) {
        return;
    }

    for (i = 0; i < part_list->size; i++) {
        FREE(part_list->items[i].item);
        FREE(part_list->items[i].value);
    }
    FREE(part_list->items);
    part_list->size = -1;
}


static void partlist_bump_action(partlist_t *part_list)
{
    if ((part_list->reinit > 0) && (part_list->reinit_counter++ >= part_list->reinit)) {
        partlist_compute_quota(part_list);
        part_list->reinit_counter = 1;
    }
}


static int partlist_getavailable(partlist_t *part_list)
{
    int count = 0;
    int idx;

    for (idx = 0; idx < part_list->size; idx++) {
        if (part_list->items[idx].quota != 0.) {
            count++;
        }
    }

    /* When nothing available, refresh quotas after a while */
    if (!count) {
        partlist_bump_action(part_list);
    }

    return count;
}


static const char *partlist_select_item(partlist_t *part_list)
{
    int idx = partlist_selectpart_index(part_list);

    return (idx == -1 ? NULL : part_list->items[idx].item);
}


EXPORTED const char *partlist_select_value(partlist_t *part_list)
{
    int idx = partlist_selectpart_index(part_list);

    return (idx == -1 ? NULL : part_list->items[idx].value);
}

EXPORTED int partlist_foreach(partlist_t *part_list,
                              partlist_foreach_cb proc,
                              void *rock)
{
    int i, r = -1;

    for (i = 0; i < part_list->size; i++) {
        r = proc(&part_list->items[i], rock);

        if (r) break;
    }

    return r;
}

static int partlist_selectpart_index(partlist_t *part_list)
{
    int i;
    int do_random = 0;
    int ret = -1;
    int available;

    if (part_list->size <= 0) {
        return -1;
    }

    available = partlist_getavailable(part_list);
    if (!available) {
        return -1;
    }

    partlist_bump_action(part_list);

    if ((part_list->mode == PART_SELECT_MODE_RANDOM) || part_list->force_random) {
        do_random = 1;
    }
    else {
        double sq = 0.;
        double val = 0.;
        uint64_t max_available = 0;

        switch (part_list->mode) {
        case PART_SELECT_MODE_FREESPACE_MOST:
            for (i = 0; i < part_list->size; i++) {
                if (part_list->items[i].quota == 0.) {
                    continue;
                }

                if (part_list->items[i].available > max_available) {
                    ret = i;
                    max_available = part_list->items[i].available;
                }
            }
            break;

        case PART_SELECT_MODE_FREESPACE_PERCENT_MOST:
            for (i = 0 ;i < part_list->size; i++) {
                if (part_list->items[i].quota == 0.) {
                    continue;
                }

                if (part_list->items[i].quota > val) {
                    ret = i;
                    val = part_list->items[i].quota;
                }
            }
            break;

        case PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED:
        case PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED_DELTA:
            /* random in [0,100[ */
            val = 100. * ((double)rand() / (RAND_MAX + 1.));

            for (i = 0; i < part_list->size; i++) {
                sq += part_list->items[i].quota;
                ret = i;
                if (val < sq) {
                    break;
                }
            }

            /* sanity check: make sure we did not pick an unwanted entry
               and get last wanted one (or -1 if all items are unwanted) */
            while ((part_list->items[ret].quota == 0.) && (--ret >= 0))
                ;
            break;

        default:
            /* sanity check */
            do_random = 1;
            break;
        }
    }

    if (do_random) {
        i = rand() % available;
        ret = 0;
        while (i--) {
            while (part_list->items[++ret].quota == 0.)
                ;
        }
    }

    return ret;
}


static void partlist_compute_quota(partlist_t *part_list)
{
    int i;
    int j;
    unsigned long id;
    double percent_available;
    double quota_total = 0;
    double quota_min = 100.;
    double quota_min_limit = 100.;
    double soft_quota_limit = 100. - part_list->soft_usage_limit;
    int soft_quota_limit_use = 0;
    partmode_t mode = part_list->mode;

    part_list->force_random = 0;

    if (mode == PART_SELECT_MODE_RANDOM) {
        /* No need to check items usage */
        for (i = 0; i < part_list->size; i++) {
            part_list->items[i].quota = 50.0;
        }

        return;
    }

    for (i = 0; i < part_list->size; i++) {
        part_list->filldata(part_list, i);

        if ((mode == PART_SELECT_MODE_FREESPACE_MOST) || (mode == PART_SELECT_MODE_FREESPACE_PERCENT_MOST)) {
            id = part_list->items[i].id;
            for (j = i-1; j >= 0; j--) {
                if (id == part_list->items[j].id) {
                    /* duplicate id, keep only the first of its kind */
                    part_list->items[i].quota = 0.;
                    break;
                }
            }
            if (j >= 0) {
                /* duplicate id, skip */
                continue;
            }
        }
        /* else: other modes does not need id de-duplication */

        percent_available = -1.;
        if (part_list->items[i].total > 0) {
            percent_available = (part_list->items[i].available * (double)100. / part_list->items[i].total);
        }

        /* ensure we got a consistent value */
        if ((percent_available<0.) || (percent_available>100.)) {
            /* fallback to random mode */
            part_list->force_random = 1;
            break;
        }

        part_list->items[i].quota = percent_available;
        /* Note: beware floating-point precision between variables stored in
         * memory and CPU registers. From now on, do not use percent_available.
         */

        if (part_list->items[i].quota < quota_min) {
            quota_min = part_list->items[i].quota;
        }

        /* check free space against limit */
        if (part_list->items[i].quota <= soft_quota_limit) {
            /* entry below limit, will not be taken into account (unless all
               entries are below limit) */
            continue;
        }
        /* at least one entry is ok, quota limit can be applied */
        soft_quota_limit_use = 1;
        if (part_list->items[i].quota < quota_min_limit) {
            quota_min_limit = part_list->items[i].quota;
        }
    }

    if (soft_quota_limit_use) {
        quota_min = quota_min_limit;
    }

    for (i = 0; i < part_list->size; i++) {
        if (part_list->force_random) {
            part_list->items[i].quota = 50.0;
        }
        else if (soft_quota_limit_use && (part_list->items[i].quota <= soft_quota_limit)) {
            /* entry is below limit, make sure not to select it */
            part_list->items[i].quota = 0.;
        }
        else if (mode == PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED_DELTA) {
            /* Note: according to previous tests, current item quota shall be
             * >= quota_min. Even with differences in floating-point precision
             * between variables stored in memory and CPU registers, the former
             * would be slightly under the latter, which would not matter since
             * we are about to add .5.
             */

            /* the goal is to reach the level of the most used volume */
            part_list->items[i].quota -= quota_min;
            /* but prevent the most used one to starve */
            part_list->items[i].quota += .5;

            /* Sanity check */
            if (part_list->items[i].quota < 0) {
                part_list->items[i].quota = 0.;
            }
        }
    }

    if (part_list->force_random) {
        /* nothing else to do */
        return;
    }

    if ((mode == PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED) || (mode == PART_SELECT_MODE_FREESPACE_PERCENT_WEIGHTED_DELTA)) {
        /* normalize */
        for (i = 0; i < part_list->size; i++) {
            quota_total += part_list->items[i].quota;
        }
        if (quota_total != 0) {
            for (i = 0; i < part_list->size; i++) {
                part_list->items[i].quota = (part_list->items[i].quota * 100.) / quota_total;
            }
        }
    }
}


static void partlist_fill(const char *key, const char *value, void *rock)
{
    partlist_conf_t *part_list_conf = (partlist_conf_t *)rock;
    partlist_t *part_list = part_list_conf->part_list;
    size_t key_prefix_len = (part_list_conf->key_prefix ? strlen(part_list_conf->key_prefix) : 0);
    unsigned i;

    if (key_prefix_len) {
        if ((strncmp(part_list_conf->key_prefix, key, key_prefix_len) != 0) || (strlen(key) <= key_prefix_len)) {
            return;
        }
    }

    for (i = 0; i < part_list_conf->excluded_count; i++) {
        if (!strcmp(key+key_prefix_len, (part_list_conf->excluded_item)[i])) {
            return;
        }
    }

    part_list->items = (partitem_t *)xrealloc(part_list->items, (part_list->size+1) * sizeof(partitem_t));
    memset(&part_list->items[part_list->size], 0, sizeof(partitem_t));
    part_list->items[part_list->size].item = xstrdup(key + key_prefix_len);
    part_list->items[part_list->size].value = xstrdup(value);
    /* item usage data will be filled later */

    part_list->size++;
}


/**
 * \brief Fills partition data.
 *
 * @param inout part_list   items list structure
 * @param in    idx         item index
 */
static void partition_filldata(partlist_t *part_list, int idx)
{
    partitem_t *item = &part_list->items[idx];
    struct statvfs stat;

    item->id = 0;
    item->available = 0;
    item->total = 0;
    item->quota = 0.;

    if (statvfs(item->value, &stat)) {
        /* statvfs error */
        int error = 1;

        if (errno == ENOENT) {
            /* try to create path */
            if ((cyrus_mkdir(item->value, 0755) == -1) || (mkdir(item->value, 0755) == -1)) {
                syslog(LOG_ERR, "IOERROR: creating %s: %m", item->value);
                return;
            }
            else {
                error = statvfs(item->value, &stat) ? 1 : 0;
            }
        }

        if (error) {
            syslog(LOG_ERR, "IOERROR: statvfs[%s]: %m", item->value);
            return;
        }
    }

    if (stat.f_blocks <= 0) {
        /* error retrieving statvfs info */
        syslog(LOG_ERR, "IOERROR: statvfs[%s]: non-positive number of blocks", item->value);
        return;
    }

    item->id = stat.f_fsid;
    item->available = (uint64_t)(stat.f_bavail * (stat.f_frsize / 1024.));
    item->total = (uint64_t)(stat.f_blocks * (stat.f_frsize / 1024.));
}


static void partlist_local_init(void)
{
    if (partlist_local) {
        /* already done */
        return;
    }

    partlist_local = xzmalloc(sizeof(partlist_t));
    partlist_initialize(
        partlist_local,
        NULL,
        "partition-",
        NULL,
        config_getstring(IMAPOPT_PARTITION_SELECT_EXCLUDE),
        partlist_getmode(config_getstring(IMAPOPT_PARTITION_SELECT_MODE)),
        config_getint(IMAPOPT_PARTITION_SELECT_SOFT_USAGE_LIMIT),
        config_getint(IMAPOPT_PARTITION_SELECT_USAGE_REINIT)
    );
}


HIDDEN const char *partlist_local_select(void)
{
    /* lazy loading */
    if (!partlist_local) {
        partlist_local_init();
    }

    return (char *)partlist_select_item(partlist_local);
}


HIDDEN const char *partlist_local_find_freespace_most(int percent, uint64_t *available,
                                               uint64_t *total, uint64_t *tavailable,
                                               uint64_t *ttotal)
{
    const char *item = NULL;
    unsigned long id;
    uint64_t available_tmp;
    uint64_t total_tmp;
    double percent_available;
    uint64_t available_max = 0;
    double percent_available_max = 0.;
    int i;
    int j;

    /* lazy loading */
    if (!partlist_local) {
        partlist_local_init();
    }

    if (available) *available = 0;
    if (total) *total = 0;
    if (tavailable) *tavailable = 0;
    if (ttotal) *ttotal = 0;

    partlist_bump_action(partlist_local);

    for (i = 0; i < partlist_local->size; i++) {
        if (partlist_local->items[i].quota == 0.) {
            continue;
        }

        id = partlist_local->items[i].id;
        for (j = i-1; j >= 0; j--) {
            if (id == partlist_local->items[j].id) {
                /* duplicate id */
                break;
            }
        }
        if (j >= 0) {
            /* duplicate id, skip */
            continue;
        }

        available_tmp = partlist_local->items[i].available;
        total_tmp = partlist_local->items[i].total;

        if (tavailable) *tavailable += available_tmp;
        if (ttotal) *ttotal += total_tmp;

        if (percent) {
            percent_available = 0.;
            if (total_tmp > 0) {
                percent_available = (available_tmp * (double)100. / total_tmp);
            }
            if ((percent_available > percent_available_max) && (percent_available <= 100.)) {
                percent_available_max = percent_available;
                item = partlist_local->items[i].item;
                if (available) *available = available_tmp;
                if (total) *total = total_tmp;
            }
        }
        else {
            if (available_tmp > available_max) {
                available_max = available_tmp;
                item = partlist_local->items[i].item;
                if (available) *available = available_tmp;
                if (total) *total = total_tmp;
            }
        }
    }

    return item;
}


EXPORTED void partlist_local_done(void)
{
    if (partlist_local) {
        partlist_free(partlist_local);
        free(partlist_local);
        partlist_local = NULL;
    }
}