Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*-------------------------------------------------------------------------
 *
 * cfs_io.c
 *    Raw file I/O primitives shared by every CFS module (see cfs_io.h).
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 2026, Diasoft
 *
 * src/backend/storage/cfs/cfs_io.c
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include <sys/stat.h>
#include <unistd.h>
 
#include "common/file_utils.h"
#include "port/pg_iovec.h"
#include "storage/cfs/cfs_guc.h"
#include "storage/cfs/cfs_io.h"
 
/*
 * cfs_pwrite_all — write 'len' bytes to 'fd' at 'offset', retrying short
 * writes.  Returns CFS_STORAGE_OK or CFS_STORAGE_ERROR.  Used both by the
 * storage layer and by the compaction layer (cfs_compact.c).
 */
int
cfs_pwrite_all(int fd, const void *buf, size_t len, off_t offset)
{
    const char *p = (const char *) buf;
    size_t      done = 0;
 
    /*
     * Debug fault injection: simulate a pwrite() failure so the test suite
     * can exercise the write-path error handling.  No-op in production
     * (cfs_debug_fail_pwrite defaults to 0).
     */
    if (cfs_debug_fail_pwrite > 0)
    {
        cfs_debug_fail_pwrite--;
        errno = EIO;
        ereport(WARNING,
                (errcode_for_file_access(),
                 errmsg("CFS: pwrite failed: %m")));
        return CFS_STORAGE_ERROR;
    }
 
    while (done < len)
    {
        ssize_t     n;
 
        n = pg_pwrite(fd, p + done, len - done, offset + done);
        if (n < 0)
        {
            if (errno == EINTR)
                continue;
            ereport(WARNING,
                    (errcode_for_file_access(),
                     errmsg("CFS: pwrite failed: %m")));
            return CFS_STORAGE_ERROR;
        }
        if (n == 0)
        {
            ereport(WARNING,
                    (errcode_for_file_access(),
                     errmsg("CFS: pwrite wrote zero bytes")));
            return CFS_STORAGE_ERROR;
        }
        done += n;
    }
    return CFS_STORAGE_OK;
}
 
/*
 * cfs_pwritev_all — the scatter/gather form of cfs_pwrite_all(): write
 * 'iovcnt' iovecs to 'fd' at 'offset' via pg_pwritev_with_retry (the
 * portable port in pg_iovec.h, never a raw pwritev), retrying short
 * writes and EINTR, with the same cfs_debug_fail_pwrite fault injection
 * and WARNING-on-failure contract as cfs_pwrite_all.  A partial record
 * left by a failed call is the caller's torn tail, exactly like a
 * cfs_pwrite_all failure.
 *
 * A zero-length transfer is refused the same way cfs_pwrite_all() reports
 * "wrote zero bytes": the caller always sends a record header, so a zero
 * result can only come from a broken retry helper below us (upstream
 * pg_pwritev_with_retry() has no zero-progress check of its own; a
 * pwritev() returning 0 with a nonzero count violates POSIX and would
 * spin inside it before we could ever observe it).
 */
int
cfs_pwritev_all(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
    size_t      total = 0;
    ssize_t     n;
    int         i;
 
    /*
     * Debug fault injection: simulate a pwrite() failure so the test suite
     * can exercise the write-path error handling.  No-op in production
     * (cfs_debug_fail_pwrite defaults to 0).
     */
    if (cfs_debug_fail_pwrite > 0)
    {
        cfs_debug_fail_pwrite--;
        errno = EIO;
        ereport(WARNING,
                (errcode_for_file_access(),
                 errmsg("CFS: pwrite failed: %m")));
        return CFS_STORAGE_ERROR;
    }
 
    /*
     * A record always has a header: refuse an empty transfer up front with
     * the same "wrote zero bytes" diagnostic cfs_pwrite_all() would give,
     * instead of delegating a zero-length write to the retry helper.
     */
    for (i = 0; i < iovcnt; i++)
        total += iov[i].iov_len;
    if (total == 0)
    {
        ereport(WARNING,
                (errcode_for_file_access(),
                 errmsg("CFS: pwrite wrote zero bytes")));
        return CFS_STORAGE_ERROR;
    }
 
    do
    {
        n = pg_pwritev_with_retry(fd, iov, iovcnt, offset);
    } while (n < 0 && errno == EINTR);
 
    if (n < 0)
    {
        ereport(WARNING,
                (errcode_for_file_access(),
                 errmsg("CFS: pwrite failed: %m")));
        return CFS_STORAGE_ERROR;
    }
    if (n == 0)
    {
        /*
         * Unreachable with a nonzero total and a healthy retry helper;
         * kept so the vector path can never lose the zero-bytes
         * diagnostic that the byte form reports.
         */
        ereport(WARNING,
                (errcode_for_file_access(),
                 errmsg("CFS: pwrite wrote zero bytes")));
        return CFS_STORAGE_ERROR;
    }
    return CFS_STORAGE_OK;
}
 
/*
 * cfs_pread_all — read exactly 'len' bytes from 'fd' at 'offset' into buf.
 * Returns CFS_STORAGE_OK, CFS_STORAGE_NOENT on unexpected EOF, or
 * CFS_STORAGE_ERROR.  Also used by the shared map pool to load .cfsmap
 * segments.
 */
int
cfs_pread_all(int fd, void *buf, size_t len, off_t offset)
{
    char       *p = (char *) buf;
    size_t      done = 0;
 
    while (done < len)
    {
        ssize_t     n;
 
        n = pg_pread(fd, p + done, len - done, offset + done);
        if (n < 0)
        {
            if (errno == EINTR)
                continue;
            ereport(WARNING,
                    (errcode_for_file_access(),
                     errmsg("CFS: pread failed: %m")));
            return CFS_STORAGE_ERROR;
        }
        if (n == 0)
            return CFS_STORAGE_NOENT;   /* unexpected EOF */
        done += n;
    }
    return CFS_STORAGE_OK;
}
 
/*
 * Return the current size of an open file descriptor, or -1.
 */
off_t
cfs_fd_size(int fd)
{
    struct stat st;
 
    if (fstat(fd, &st) != 0)
        return -1;
    return st.st_size;
}