summaryrefslogtreecommitdiff
path: root/fs/pipe.cc
blob: f7708f5cccef31ad43b874b6305fff5ea0dd308d (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * pipe.cc — Pipe implementation
 *
 * Implements unidirectional byte-stream pipes with blocking I/O.
 * Each pipe has a 4KB circular buffer.  Reads block when empty,
 * writes block when full (unless O_NONBLOCK).
 *
 * When all writers close, read returns 0 (EOF).
 * When all readers close, write returns -EPIPE.
 */

#include <lume/pipe.h>
#include <lume/file.h>
#include <lume/fdtable.h>
#include <lume/errno.h>
#include <lume/slab.h>
#include <lume/fs_types.h>
#include <lume/klog.h>
#include <lume/sched.h>

/* ================================================================== */
/*  pipe_inode_info lifecycle                                          */
/* ================================================================== */

pipe_inode_info* alloc_pipe_info()
{
    auto* pi = new pipe_inode_info{};
    if (!pi) return nullptr;

    pi->rd_pos = 0;
    pi->wr_pos = 0;
    pi->len = 0;
    pi->rd_wait.init("pipe_rd");
    pi->wr_wait.init("pipe_wr");
    pi->readers.store(0);
    pi->writers.store(0);
    pi->refcount.store(1);
    return pi;
}

void free_pipe_info(pipe_inode_info* pipe)
{
    delete pipe;
}

static void pipe_get(pipe_inode_info* pi)
{
    pi->refcount.fetch_add(1);
}

static void pipe_put(pipe_inode_info* pi)
{
    if (pi->refcount.fetch_sub(1) == 1) {
        free_pipe_info(pi);
    }
}

/* ================================================================== */
/*  pipe_read                                                         */
/* ================================================================== */

static int64 pipe_read(file* fp, char* buf, uint64 count, int64* /*pos*/)
{
    pipe_inode_info* pi = static_cast<pipe_inode_info*>(fp->private_data);
    if (!pi) return -EBADF;

    while (true) {
        pi->lock.acquire();

        if (pi->len > 0) {
            /* Data available — copy out */
            int64 to_read = static_cast<int64>(count);
            if (to_read > pi->len) to_read = pi->len;

            for (int64 i = 0; i < to_read; i++) {
                buf[i] = pi->buf[pi->rd_pos];
                pi->rd_pos = (pi->rd_pos + 1) % PIPE_BUF_SIZE;
            }
            pi->len -= static_cast<int>(to_read);

            pi->lock.release();

            /* Wake any blocked writers */
            pi->wr_wait.wake_all();
            return to_read;
        }

        /* Buffer empty */
        if (pi->writers.load() == 0) {
            /* No writers left — EOF */
            pi->lock.release();
            return 0;
        }

        if (fp->f_flags & O_NONBLOCK) {
            pi->lock.release();
            return -EAGAIN;
        }

        pi->lock.release();

        /* Sleep until data or writer closes */
        pi->rd_wait.sleep();
    }
}

/* ================================================================== */
/*  pipe_write                                                        */
/* ================================================================== */

static int64 pipe_write(file* fp, const char* buf, uint64 count, int64* /*pos*/)
{
    pipe_inode_info* pi = static_cast<pipe_inode_info*>(fp->private_data);
    if (!pi) return -EBADF;

    if (pi->readers.load() == 0) {
        return -EPIPE;  /* Broken pipe */
    }

    int64 written = 0;

    while (static_cast<uint64>(written) < count) {
        pi->lock.acquire();

        if (pi->len < PIPE_BUF_SIZE) {
            /* Space available — copy in */
            int avail = PIPE_BUF_SIZE - pi->len;
            int64 remaining = static_cast<int64>(count) - written;
            int to_write = avail < static_cast<int>(remaining)
                               ? avail
                               : static_cast<int>(remaining);

            for (int i = 0; i < to_write; i++) {
                pi->buf[pi->wr_pos] = buf[written + i];
                pi->wr_pos = (pi->wr_pos + 1) % PIPE_BUF_SIZE;
            }
            pi->len += to_write;
            written += to_write;

            pi->lock.release();

            /* Wake any blocked readers */
            pi->rd_wait.wake_all();
            continue;
        }

        /* Buffer full */
        if (pi->readers.load() == 0) {
            pi->lock.release();
            return written > 0 ? written : -EPIPE;
        }

        if (fp->f_flags & O_NONBLOCK) {
            pi->lock.release();
            return written > 0 ? written : -EAGAIN;
        }

        pi->lock.release();

        /* Sleep until space or reader closes */
        pi->wr_wait.sleep();
    }

    return written;
}

/* ================================================================== */
/*  pipe_release — called on last fput of a pipe fd                   */
/* ================================================================== */

static int pipe_read_release(inode* /*ip*/, file* fp)
{
    pipe_inode_info* pi = static_cast<pipe_inode_info*>(fp->private_data);
    if (pi) {
        pi->readers.fetch_sub(1);
        /* Wake writers so they see readers==0 and get EPIPE */
        pi->wr_wait.wake_all();
        pipe_put(pi);
    }
    return 0;
}

static int pipe_write_release(inode* /*ip*/, file* fp)
{
    pipe_inode_info* pi = static_cast<pipe_inode_info*>(fp->private_data);
    if (pi) {
        pi->writers.fetch_sub(1);
        /* Wake readers so they see writers==0 and get EOF */
        pi->rd_wait.wake_all();
        pipe_put(pi);
    }
    return 0;
}

/* ================================================================== */
/*  file_operations tables                                            */
/* ================================================================== */

static file_operations pipe_read_fops;
static file_operations pipe_write_fops;
static bool pipe_fops_initialized = false;

static void pipe_fops_init()
{
    if (pipe_fops_initialized) return;

    pipe_read_fops = {};
    pipe_read_fops.read    = pipe_read;
    pipe_read_fops.release = pipe_read_release;

    pipe_write_fops = {};
    pipe_write_fops.write   = pipe_write;
    pipe_write_fops.release = pipe_write_release;

    pipe_fops_initialized = true;
}

/* ================================================================== */
/*  do_pipe2 — create a pipe, install two fds                         */
/* ================================================================== */

int do_pipe2(files_struct* files, int fds[2], int flags)
{
    pipe_fops_init();

    pipe_inode_info* pi = alloc_pipe_info();
    if (!pi) return -ENOMEM;

    /* Create read-end file */
    file* rd_fp = alloc_empty_file(O_RDONLY | (flags & O_NONBLOCK));
    if (!rd_fp) {
        free_pipe_info(pi);
        return -ENFILE;
    }
    rd_fp->f_op = &pipe_read_fops;
    rd_fp->private_data = pi;

    /* Create write-end file */
    file* wr_fp = alloc_empty_file(O_WRONLY | (flags & O_NONBLOCK));
    if (!wr_fp) {
        fput(rd_fp);
        free_pipe_info(pi);
        return -ENFILE;
    }
    wr_fp->f_op = &pipe_write_fops;
    wr_fp->private_data = pi;

    /* Bump pipe refcount for the two files */
    pipe_get(pi);  /* wr_fp gets second ref */
    pi->readers.store(1);
    pi->writers.store(1);

    /* Allocate fds */
    int rd_fd = alloc_fd(files, 0);
    if (rd_fd < 0) {
        fput(rd_fp);
        fput(wr_fp);
        return rd_fd;
    }

    int wr_fd = alloc_fd(files, 0);
    if (wr_fd < 0) {
        close_fd(files, rd_fd);
        fput(wr_fp);
        return wr_fd;
    }

    fd_install(files, rd_fd, rd_fp);
    fd_install(files, wr_fd, wr_fp);

    fds[0] = rd_fd;
    fds[1] = wr_fd;

    return 0;
}