File: fs.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (353 lines) | stat: -rw-r--r-- 8,599 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/uio.h>
#include <unistd.h>

#include "fs.h"

/* Minimum physical block size for direct I/O that we expect to detect. */
#define MIN_BLOCK_SIZE 512

/* Maximum physical block size for direct I/O that we expect to detect. */
#define MAX_BLOCK_SIZE (1024 * 1024) /* 1M */

#define SYS_BLOCK_PATH "/sys/dev/block/%d:%d"

/* Histgram resolution when the underlying block driver is NVMe. */
#define RESOLUTION_NVME 500        /* buckets are 0.5 microseconds apart */
#define BUCKETS_NVME 20 * 1000 * 2 /* buckets up to 20,000 microseconds */

/* Histgram resolution when the underlying block driver is nullb. */
#define RESOLUTION_NULLB 50     /* buckets are 50 nanoseconds apart */
#define BUCKETS_NULLB 1000 * 20 /* buckets up to 1,000 microseconds */

/* Histgram resolution when the underlying block driver is unspecified. */
#define RESOLUTION_GENERIC 1000   /* buckets are 1 microsecond apart */
#define BUCKETS_GENERIC 20 * 1000 /* buckets up to 20,000 microseconds */

/* Read an integer value from the file /sys/dev/block/<maj>:<min>/name . */
static int readBlockDeviceInfo(unsigned maj,
                               unsigned min,
                               const char *name,
                               unsigned *value)
{
    char path[1024];
    char buf[256];
    FILE *file;
    int rv;
    sprintf(path, SYS_BLOCK_PATH "/%s", maj, min, name);

    file = fopen(path, "r");
    if (file == NULL) {
        /* Special case null_blk devices which don't create a "start" file. */
        if (strcmp(name, "start") == 0 && errno == ENOENT) {
            *value = 0;
            return 0;
        }

        fprintf(stderr, "fopen device info at %s: %s\n", path, strerror(errno));
        return -1;
    }

    rv = (int)fread(buf, sizeof buf, 1, file);
    if (rv < 0) {
        fprintf(stderr, "fread device info at %s: %s\n", path, strerror(errno));
        return -1;
    }

    *value = (unsigned)atoi(buf);

    fclose(file);

    return 0;
}

/* Read the start sector of the given block device. */
static int readBlockDeviceStart(unsigned maj, unsigned min, unsigned *start)
{
    return readBlockDeviceInfo(maj, min, "start", start);
}

/* Read the size in sectors of the given block device. */
static int readBlockDeviceSize(unsigned maj, unsigned min, unsigned *size)
{
    return readBlockDeviceInfo(maj, min, "size", size);
}

/* Check if a block device has power-loss protection via write-through.
 *
 * https://stackoverflow.com/questions/72353566/when-should-i-use-req-op-flush-in-a-kernel-blockdev-driver-do-req-op-flush-bio
 */
static int readBlockDeviceWriteCache(const char *path, bool *write_through)
{
    char buf[256];
    FILE *file;
    int rv;

    file = fopen(path, "r");
    if (file == NULL) {
        fprintf(stderr, "fopen write_cache at %s: %s\n", path, strerror(errno));
        return -1;
    }

    rv = (int)fread(buf, sizeof buf, 1, file);
    if (rv < 0) {
        fprintf(stderr, "fread write_cache at %s: %s\n", path, strerror(errno));
        return -1;
    }

    fclose(file);

    *write_through = strcmp(buf, "write through\n") == 0;

    return 0;
}

int FsFileInfo(const char *path, struct FsFileInfo *info)
{
    struct stat st;
    unsigned maj;
    unsigned min;
    unsigned dev_size;
    char block[1024];
    char link[1024];
    char parent[8192]; /* TODO: suppress -Werror=format-overflow */
    const char *name;
    int rv;

    rv = stat(path, &st);
    if (rv != 0) {
        perror("stat");
        return -1;
    }

    if ((st.st_mode & S_IFMT) == S_IFBLK) {
        info->type = FS_TYPE_DEVICE;
        maj = major(st.st_rdev);
        min = minor(st.st_rdev);
    } else {
        info->type = FS_TYPE_REGULAR;
        maj = major(st.st_dev);
        min = minor(st.st_dev);
    }

    sprintf(block, SYS_BLOCK_PATH, maj, min);

    memset(link, 0, sizeof link);
    rv = (int)readlink(block, link, sizeof link);

    if (rv < 0) {
        if (errno != ENOENT) {
            return -1;
        }
        info->driver = FS_DRIVER_GENERIC;
        goto out;
    }

    rv = readBlockDeviceStart(maj, min, &info->block_dev_start);
    if (rv != 0) {
        return -1;
    }
    rv = readBlockDeviceSize(maj, min, &dev_size);
    if (rv != 0) {
        return -1;
    }
    info->block_dev_end = info->block_dev_start + dev_size;

    name = basename(link);

    if (strcmp(name, "nullb0") == 0) {
        info->driver = FS_DRIVER_NULLB;
        sprintf(parent, "%s/%s/queue/write_cache", dirname(block), link);
    } else if (strncmp(name, "nvme", strlen("nvme")) == 0) {
        info->driver = FS_DRIVER_NVME;
        sprintf(parent, "%s/%s/queue/write_cache", dirname(block),
                dirname(link));
    } else {
        info->driver = FS_DRIVER_GENERIC;
        info->block_dev_write_through = false;
        goto out;
    }

    rv = readBlockDeviceWriteCache(parent, &info->block_dev_write_through);
    if (rv != 0) {
        return rv;
    }

out:
    switch (info->driver) {
        case FS_DRIVER_NVME:
            info->buckets = BUCKETS_NVME;
            info->resolution = RESOLUTION_NVME;
            break;
        case FS_DRIVER_NULLB:
            info->buckets = BUCKETS_NULLB;
            info->resolution = RESOLUTION_NULLB;
            break;
        case FS_DRIVER_GENERIC:
            info->buckets = BUCKETS_GENERIC;
            info->resolution = RESOLUTION_GENERIC;
            break;
        default:
            assert(0);
            break;
    }

    return 0;
}

static char *makeTempTemplate(const char *dir)
{
    char *path;
    path = malloc(strlen(dir) + strlen("/bench-XXXXXX") + 1);
    assert(path != NULL);
    sprintf(path, "%s/bench-XXXXXX", dir);
    return path;
}

int FsCreateTempFile(const char *dir, size_t size, char **path, int *fd)
{
    int flags = O_WRONLY | O_CREAT | O_EXCL | O_DIRECT;
    void *buf;
    int dirfd;
    int rv;

    *path = makeTempTemplate(dir);
    *fd = mkostemp(*path, flags);
    if (*fd == -1) {
        printf("mstemp '%s': %s\n", *path, strerror(errno));
        return -1;
    }

    rv = posix_fallocate(*fd, 0, (off_t)size);
    if (rv != 0) {
        errno = rv;
        printf("posix_fallocate: %s\n", strerror(errno));
        unlink(*path);
        return -1;
    }

    buf = aligned_alloc(size, size);
    assert(buf != NULL);

    rv = (int)pwrite(*fd, buf, size, 0);

    free(buf);

    if (rv != (int)size) {
        printf("pwrite '%s': %d\n", *path, rv);
        return -1;
    }

    /* Sync the file and its directory. */
    rv = fsync(*fd);
    assert(rv == 0);

    dirfd = open(dir, O_RDONLY | O_DIRECTORY);
    assert(dirfd != -1);

    rv = fsync(dirfd);
    assert(rv == 0);

    close(dirfd);

    return 0;
}

int FsRemoveTempFile(char *path, int fd)
{
    int rv;

    rv = close(fd);
    if (rv != 0) {
        printf("close '%s': %s\n", path, strerror(errno));
        goto out;
    }
    rv = unlink(path);
    if (rv != 0) {
        printf("unlink '%s': %s\n", path, strerror(errno));
        goto out;
    }

out:
    free(path);
    return rv;
}

int FsCreateTempDir(const char *dir, char **path)
{
    *path = makeTempTemplate(dir);
    if (*path == NULL) {
        return -1;
    }
    *path = mkdtemp(*path);
    if (*path == NULL) {
        return -1;
    }
    return 0;
}

/* Wrapper around remove(), compatible with ntfw. */
static int dirRemoveFn(const char *path,
                       const struct stat *sbuf,
                       int type,
                       struct FTW *ftwb)
{
    (void)sbuf;
    (void)type;
    (void)ftwb;
    return remove(path);
}

int FsRemoveTempDir(char *path)
{
    int rv;
    rv = nftw(path, dirRemoveFn, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
    if (rv != 0) {
        return -1;
    }
    free(path);
    return 0;
}

int FsOpenBlockDevice(const char *dir, int *fd)
{
    *fd = open(dir, O_WRONLY | O_DIRECT);
    if (*fd == -1) {
        printf("open '%s': %s\n", dir, strerror(errno));
        return -1;
    }
    return 0;
}

int FsFileExists(const char *dir, const char *name, bool *exists)
{
    char *path;
    struct stat st;
    int rv;

    path = malloc(strlen(dir) + strlen(name) + 1 + 1);
    assert(path != NULL);
    sprintf(path, "%s/%s", dir, name);

    rv = stat(path, &st);

    free(path);

    if (rv != 0) {
        if (errno != ENOENT) {
            return -1;
        }
        *exists = false;
    } else {
        *exists = true;
    }

    return 0;
}