Subversion Repositories HelenOS

Rev

Rev 2678 | Rev 2686 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2678 Rev 2685
1
/*
1
/*
2
 * Copyright (c) 2007 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    tmpfs_ops.c
34
 * @file    tmpfs_ops.c
35
 * @brief   Implementation of VFS operations for the TMPFS file system
35
 * @brief   Implementation of VFS operations for the TMPFS file system
36
 *      server.
36
 *      server.
37
 */
37
 */
38
 
38
 
39
#include "tmpfs.h"
39
#include "tmpfs.h"
40
#include "../../vfs/vfs.h"
40
#include "../../vfs/vfs.h"
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <async.h>
42
#include <async.h>
43
#include <errno.h>
43
#include <errno.h>
44
#include <atomic.h>
44
#include <atomic.h>
45
#include <stdlib.h>
45
#include <stdlib.h>
46
#include <string.h>
46
#include <string.h>
47
#include <stdio.h>
47
#include <stdio.h>
48
#include <sys/types.h>
48
#include <sys/types.h>
49
#include <libadt/hash_table.h>
49
#include <libadt/hash_table.h>
50
#include <as.h>
50
#include <as.h>
51
 
51
 
52
#define min(a, b)       ((a) < (b) ? (a) : (b))
52
#define min(a, b)       ((a) < (b) ? (a) : (b))
53
#define max(a, b)       ((a) > (b) ? (a) : (b))
53
#define max(a, b)       ((a) > (b) ? (a) : (b))
54
 
54
 
55
#define PLB_GET_CHAR(i)     (tmpfs_reg.plb_ro[(i) % PLB_SIZE])
55
#define PLB_GET_CHAR(i)     (tmpfs_reg.plb_ro[(i) % PLB_SIZE])
56
 
56
 
57
#define DENTRIES_BUCKETS    256
57
#define DENTRIES_BUCKETS    256
58
 
58
 
59
/*
59
/*
60
 * Hash table of all directory entries.
60
 * Hash table of all directory entries.
61
 */
61
 */
62
hash_table_t dentries;
62
hash_table_t dentries;
63
 
63
 
64
static hash_index_t dentries_hash(unsigned long *key)
64
static hash_index_t dentries_hash(unsigned long *key)
65
{
65
{
66
    return *key % DENTRIES_BUCKETS;
66
    return *key % DENTRIES_BUCKETS;
67
}
67
}
68
 
68
 
69
static int dentries_compare(unsigned long *key, hash_count_t keys,
69
static int dentries_compare(unsigned long *key, hash_count_t keys,
70
    link_t *item)
70
    link_t *item)
71
{
71
{
72
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
72
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
73
        dh_link);
73
        dh_link);
74
    return dentry->index == *key;
74
    return dentry->index == *key;
75
}
75
}
76
 
76
 
77
static void dentries_remove_callback(link_t *item)
77
static void dentries_remove_callback(link_t *item)
78
{
78
{
79
}
79
}
80
 
80
 
81
/** TMPFS dentries hash table operations. */
81
/** TMPFS dentries hash table operations. */
82
hash_table_operations_t dentries_ops = {
82
hash_table_operations_t dentries_ops = {
83
    .hash = dentries_hash,
83
    .hash = dentries_hash,
84
    .compare = dentries_compare,
84
    .compare = dentries_compare,
85
    .remove_callback = dentries_remove_callback
85
    .remove_callback = dentries_remove_callback
86
};
86
};
87
 
87
 
88
unsigned tmpfs_next_index = 1;
88
unsigned tmpfs_next_index = 1;
89
 
89
 
90
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
90
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
91
{
91
{
92
    dentry->index = 0;
92
    dentry->index = 0;
93
    dentry->parent = NULL;
93
    dentry->parent = NULL;
94
    dentry->sibling = NULL;
94
    dentry->sibling = NULL;
95
    dentry->child = NULL;
95
    dentry->child = NULL;
96
    dentry->name = NULL;
96
    dentry->name = NULL;
97
    dentry->type = TMPFS_NONE;
97
    dentry->type = TMPFS_NONE;
98
    dentry->size = 0;
98
    dentry->size = 0;
99
    dentry->data = NULL;
99
    dentry->data = NULL;
100
    link_initialize(&dentry->dh_link);
100
    link_initialize(&dentry->dh_link);
101
}
101
}
102
 
102
 
103
/*
103
/*
104
 * For now, we don't distinguish between different dev_handles/instances. All
104
 * For now, we don't distinguish between different dev_handles/instances. All
105
 * requests resolve to the only instance, rooted in the following variable.
105
 * requests resolve to the only instance, rooted in the following variable.
106
 */
106
 */
107
static tmpfs_dentry_t *root;
107
static tmpfs_dentry_t *root;
108
 
108
 
109
static bool tmpfs_init(void)
109
static bool tmpfs_init(void)
110
{
110
{
111
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
111
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
112
        return false;
112
        return false;
113
 
113
 
114
    root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
114
    root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
115
    if (!root)
115
    if (!root)
116
        return false;
116
        return false;
117
    tmpfs_dentry_initialize(root);
117
    tmpfs_dentry_initialize(root);
118
    root->index = tmpfs_next_index++;
118
    root->index = tmpfs_next_index++;
119
    root->name = "";
119
    root->name = "";
120
    root->type = TMPFS_DIRECTORY;
120
    root->type = TMPFS_DIRECTORY;
121
    hash_table_insert(&dentries, &root->index, &root->dh_link);
121
    hash_table_insert(&dentries, &root->index, &root->dh_link);
122
 
122
 
123
    /*
123
    /*
124
     * This is only for debugging. Once we can create files and directories
124
     * This is only for debugging. Once we can create files and directories
125
     * using VFS, we can get rid of this.
125
     * using VFS, we can get rid of this.
126
     */
126
     */
127
    tmpfs_dentry_t *d;
127
    tmpfs_dentry_t *d;
128
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
128
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
129
    if (!d) {
129
    if (!d) {
130
        free(root);
130
        free(root);
131
        root = NULL;
131
        root = NULL;
132
        return false;
132
        return false;
133
    }
133
    }
134
    tmpfs_dentry_initialize(d);
134
    tmpfs_dentry_initialize(d);
135
    d->index = tmpfs_next_index++;
135
    d->index = tmpfs_next_index++;
136
    root->child = d;
136
    root->child = d;
137
    d->parent = root;
137
    d->parent = root;
138
    d->type = TMPFS_DIRECTORY;
138
    d->type = TMPFS_DIRECTORY;
139
    d->name = "dir1";
139
    d->name = "dir1";
140
    hash_table_insert(&dentries, &d->index, &d->dh_link);
140
    hash_table_insert(&dentries, &d->index, &d->dh_link);
141
 
141
 
142
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
142
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
143
    if (!d) {
143
    if (!d) {
144
        free(root->child);
144
        free(root->child);
145
        free(root);
145
        free(root);
146
        root = NULL;
146
        root = NULL;
147
        return false;
147
        return false;
148
    }
148
    }
149
    tmpfs_dentry_initialize(d);
149
    tmpfs_dentry_initialize(d);
150
    d->index = tmpfs_next_index++;
150
    d->index = tmpfs_next_index++;
151
    root->child->sibling = d;
151
    root->child->sibling = d;
152
    d->parent = root;
152
    d->parent = root;
153
    d->type = TMPFS_DIRECTORY;
153
    d->type = TMPFS_DIRECTORY;
154
    d->name = "dir2";
154
    d->name = "dir2";
155
    hash_table_insert(&dentries, &d->index, &d->dh_link);
155
    hash_table_insert(&dentries, &d->index, &d->dh_link);
156
   
156
   
157
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
157
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
158
    if (!d) {
158
    if (!d) {
159
        free(root->child->sibling);
159
        free(root->child->sibling);
160
        free(root->child);
160
        free(root->child);
161
        free(root);
161
        free(root);
162
        root = NULL;
162
        root = NULL;
163
        return false;
163
        return false;
164
    }
164
    }
165
    tmpfs_dentry_initialize(d);
165
    tmpfs_dentry_initialize(d);
166
    d->index = tmpfs_next_index++;
166
    d->index = tmpfs_next_index++;
167
    root->child->child = d;
167
    root->child->child = d;
168
    d->parent = root->child;
168
    d->parent = root->child;
169
    d->type = TMPFS_FILE;
169
    d->type = TMPFS_FILE;
170
    d->name = "file1";
170
    d->name = "file1";
171
    d->data = "This is the contents of /dir1/file1.\n";
171
    d->data = "This is the contents of /dir1/file1.\n";
172
    d->size = strlen(d->data);
172
    d->size = strlen(d->data);
173
    hash_table_insert(&dentries, &d->index, &d->dh_link);
173
    hash_table_insert(&dentries, &d->index, &d->dh_link);
174
 
174
 
175
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
175
    d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
176
    if (!d) {
176
    if (!d) {
177
        free(root->child->sibling);
177
        free(root->child->sibling);
178
        free(root->child->child);
178
        free(root->child->child);
179
        free(root->child);
179
        free(root->child);
180
        free(root);
180
        free(root);
181
        root = NULL;
181
        root = NULL;
182
        return false;
182
        return false;
183
    }
183
    }
184
    tmpfs_dentry_initialize(d);
184
    tmpfs_dentry_initialize(d);
185
    d->index = tmpfs_next_index++;
185
    d->index = tmpfs_next_index++;
186
    root->child->sibling->child = d;
186
    root->child->sibling->child = d;
187
    d->parent = root->child->sibling;
187
    d->parent = root->child->sibling;
188
    d->type = TMPFS_FILE;
188
    d->type = TMPFS_FILE;
189
    d->name = "file2";
189
    d->name = "file2";
190
    d->data = "This is the contents of /dir2/file2.\n";
190
    d->data = "This is the contents of /dir2/file2.\n";
191
    d->size = strlen(d->data);
191
    d->size = strlen(d->data);
192
    hash_table_insert(&dentries, &d->index, &d->dh_link);
192
    hash_table_insert(&dentries, &d->index, &d->dh_link);
193
 
193
 
194
    return true;
194
    return true;
195
}
195
}
196
 
196
 
197
/** Compare one component of path to a directory entry.
197
/** Compare one component of path to a directory entry.
198
 *
198
 *
199
 * @param dentry    Directory entry to compare the path component with.
199
 * @param dentry    Directory entry to compare the path component with.
200
 * @param start     Index into PLB where the path component starts.
200
 * @param start     Index into PLB where the path component starts.
201
 * @param last      Index of the last character of the path in PLB.
201
 * @param last      Index of the last character of the path in PLB.
202
 *
202
 *
203
 * @return      Zero on failure or delta such that (index + delta) %
203
 * @return      Zero on failure or delta such that (index + delta) %
204
 *          PLB_SIZE points to the first unprocessed character in
204
 *          PLB_SIZE points to the first unprocessed character in
205
 *          PLB which comprises the path.
205
 *          PLB which comprises the path.
206
 */
206
 */
207
static unsigned match_path_component(tmpfs_dentry_t *dentry, unsigned start,
207
static unsigned match_path_component(tmpfs_dentry_t *dentry, unsigned start,
208
    unsigned last)
208
    unsigned last)
209
{
209
{
210
    int i, j;
210
    int i, j;
211
    size_t namelen;
211
    size_t namelen;
212
 
212
 
213
    namelen = strlen(dentry->name);
213
    namelen = strlen(dentry->name);
214
 
214
 
215
    if (last < start)
215
    if (last < start)
216
        last += PLB_SIZE;
216
        last += PLB_SIZE;
217
 
217
 
218
    for (i = 0, j = start; i < namelen && j <= last; i++, j++) {
218
    for (i = 0, j = start; i < namelen && j <= last; i++, j++) {
219
        if (dentry->name[i] != PLB_GET_CHAR(j))
219
        if (dentry->name[i] != PLB_GET_CHAR(j))
220
            return 0;
220
            return 0;
221
    }
221
    }
222
   
222
   
223
    if (i != namelen)
223
    if (i != namelen)
224
        return 0;
224
        return 0;
225
    if (j < last && PLB_GET_CHAR(j) != '/')
225
    if (j < last && PLB_GET_CHAR(j) != '/')
226
        return 0;
226
        return 0;
227
    if (j == last)
227
    if (j == last)
228
            return 0;
228
            return 0;
229
   
229
   
230
    return (j - start);
230
    return (j - start);
231
}
231
}
232
 
232
 
233
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
233
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
234
{
234
{
235
    unsigned next = IPC_GET_ARG1(*request);
235
    unsigned next = IPC_GET_ARG1(*request);
236
    unsigned last = IPC_GET_ARG2(*request);
236
    unsigned last = IPC_GET_ARG2(*request);
237
    int dev_handle = IPC_GET_ARG3(*request);
237
    int dev_handle = IPC_GET_ARG3(*request);
238
 
238
 
239
    if (last < next)
239
    if (last < next)
240
        last += PLB_SIZE;
240
        last += PLB_SIZE;
241
 
241
 
242
    if (!root && !tmpfs_init()) {
242
    if (!root && !tmpfs_init()) {
243
        ipc_answer_0(rid, ENOMEM);
243
        ipc_answer_0(rid, ENOMEM);
244
        return;
244
        return;
245
    }
245
    }
246
 
246
 
247
    tmpfs_dentry_t *dtmp = root->child;
247
    tmpfs_dentry_t *dtmp = root->child;
248
    tmpfs_dentry_t *dcur = root;
248
    tmpfs_dentry_t *dcur = root;
249
 
249
 
250
    bool hit = true;
250
    bool hit = true;
251
   
251
   
252
    if (PLB_GET_CHAR(next) == '/')
252
    if (PLB_GET_CHAR(next) == '/')
253
        next++;     /* eat slash */
253
        next++;     /* eat slash */
254
   
254
   
255
    while (next <= last) {
255
    while (next <= last) {
256
        unsigned delta;
256
        unsigned delta;
257
        hit = false;
257
        hit = false;
258
        do {
258
        do {
259
            delta = match_path_component(dtmp, next, last);
259
            delta = match_path_component(dtmp, next, last);
260
            if (!delta) {
260
            if (!delta) {
261
                dtmp = dtmp->sibling;
261
                dtmp = dtmp->sibling;
262
            } else {
262
            } else {
263
                hit = true;
263
                hit = true;
264
                next += delta;
264
                next += delta;
265
                next++;     /* eat slash */
265
                next++;     /* eat slash */
266
                dcur = dtmp;
266
                dcur = dtmp;
267
                dtmp = dtmp->child;
267
                dtmp = dtmp->child;
268
            }  
268
            }  
269
        } while (delta == 0 && dtmp);
269
        } while (delta == 0 && dtmp);
270
        if (!hit) {
270
        if (!hit) {
271
            ipc_answer_3(rid, ENOENT, tmpfs_reg.fs_handle,
271
            ipc_answer_3(rid, ENOENT, tmpfs_reg.fs_handle,
272
                dev_handle, dcur->index);
272
                dev_handle, dcur->index);
273
            return;
273
            return;
274
        }
274
        }
275
    }
275
    }
276
 
276
 
277
    ipc_answer_3(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index);
277
    ipc_answer_3(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index);
278
}
278
}
279
 
279
 
280
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
280
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
281
{
281
{
282
    int dev_handle = IPC_GET_ARG1(*request);
282
    int dev_handle = IPC_GET_ARG1(*request);
283
    unsigned long index = IPC_GET_ARG2(*request);
283
    unsigned long index = IPC_GET_ARG2(*request);
284
    off_t pos = IPC_GET_ARG3(*request);
284
    off_t pos = IPC_GET_ARG3(*request);
285
 
285
 
286
    /*
286
    /*
287
     * Lookup the respective dentry.
287
     * Lookup the respective dentry.
288
     */
288
     */
289
    link_t *hlp;
289
    link_t *hlp;
290
    hlp = hash_table_find(&dentries, &index);
290
    hlp = hash_table_find(&dentries, &index);
291
    if (!hlp) {
291
    if (!hlp) {
292
        ipc_answer_0(rid, ENOENT);
292
        ipc_answer_0(rid, ENOENT);
293
        return;
293
        return;
294
    }
294
    }
295
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
295
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
296
        dh_link);
296
        dh_link);
297
 
297
 
298
    /*
298
    /*
299
     * Receive the read request.
299
     * Receive the read request.
300
     */
300
     */
301
    ipc_callid_t callid;
301
    ipc_callid_t callid;
302
    size_t len;
302
    size_t len;
303
    if (!ipc_data_read_receive(&callid, &len)) {
303
    if (!ipc_data_read_receive(&callid, &len)) {
304
        ipc_answer_0(callid, EINVAL);  
304
        ipc_answer_0(callid, EINVAL);  
305
        ipc_answer_0(rid, EINVAL);
305
        ipc_answer_0(rid, EINVAL);
306
        return;
306
        return;
307
    }
307
    }
308
 
308
 
309
    size_t bytes = max(0, min(dentry->size - pos, len));
309
    size_t bytes = max(0, min(dentry->size - pos, len));
310
    (void) ipc_data_read_finalize(callid, dentry->data + pos, bytes);
310
    (void) ipc_data_read_finalize(callid, dentry->data + pos, bytes);
311
 
311
 
312
    /*
312
    /*
313
     * Answer the VFS_READ call.
313
     * Answer the VFS_READ call.
314
     */
314
     */
315
    ipc_answer_1(rid, EOK, bytes);
315
    ipc_answer_1(rid, EOK, bytes);
316
}
316
}
317
 
317
 
318
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
318
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
319
{
319
{
320
    int dev_handle = IPC_GET_ARG1(*request);
320
    int dev_handle = IPC_GET_ARG1(*request);
321
    unsigned long index = IPC_GET_ARG2(*request);
321
    unsigned long index = IPC_GET_ARG2(*request);
322
    off_t pos = IPC_GET_ARG3(*request);
322
    off_t pos = IPC_GET_ARG3(*request);
323
 
323
 
324
    /*
324
    /*
325
     * Lookup the respective dentry.
325
     * Lookup the respective dentry.
326
     */
326
     */
327
    link_t *hlp;
327
    link_t *hlp;
328
    hlp = hash_table_find(&dentries, &index);
328
    hlp = hash_table_find(&dentries, &index);
329
    if (!hlp) {
329
    if (!hlp) {
330
        ipc_answer_0(rid, ENOENT);
330
        ipc_answer_0(rid, ENOENT);
331
        return;
331
        return;
332
    }
332
    }
333
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
333
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
334
        dh_link);
334
        dh_link);
335
 
335
 
336
    /*
336
    /*
337
     * Receive the write request.
337
     * Receive the write request.
338
     */
338
     */
339
    ipc_callid_t callid;
339
    ipc_callid_t callid;
340
    size_t len;
340
    size_t len;
341
    if (!ipc_data_write_receive(&callid, &len)) {
341
    if (!ipc_data_write_receive(&callid, &len)) {
342
        ipc_answer_0(callid, EINVAL);  
342
        ipc_answer_0(callid, EINVAL);  
343
        ipc_answer_0(rid, EINVAL);
343
        ipc_answer_0(rid, EINVAL);
344
        return;
344
        return;
345
    }
345
    }
346
 
346
 
347
    /*
347
    /*
348
     * Check whether the file needs to grow.
348
     * Check whether the file needs to grow.
349
     */
349
     */
350
    if (pos + len <= dentry->size) {
350
    if (pos + len <= dentry->size) {
351
        /* The file size is not changing. */
351
        /* The file size is not changing. */
352
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
352
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
353
        ipc_answer_1(rid, EOK, len);
353
        ipc_answer_1(rid, EOK, len);
354
        return;
354
        return;
355
    }
355
    }
356
    size_t delta = (pos + len) - dentry->size;
356
    size_t delta = (pos + len) - dentry->size;
357
    /*
357
    /*
358
     * At this point, we are deliberately extremely straightforward and
358
     * At this point, we are deliberately extremely straightforward and
359
     * simply realloc the contents of the file on every write that grows the
359
     * simply realloc the contents of the file on every write that grows the
360
     * file. In the end, the situation might not be as bad as it may look:
360
     * file. In the end, the situation might not be as bad as it may look:
361
     * our heap allocator can save us and just grow the block whenever
361
     * our heap allocator can save us and just grow the block whenever
362
     * possible.
362
     * possible.
363
     */
363
     */
364
    void *newdata = realloc(dentry->data, dentry->size + delta);
364
    void *newdata = realloc(dentry->data, dentry->size + delta);
365
    if (!newdata) {
365
    if (!newdata) {
366
        ipc_answer_0(callid, ENOMEM);
366
        ipc_answer_0(callid, ENOMEM);
367
        ipc_answer_1(rid, EOK, 0);
367
        ipc_answer_1(rid, EOK, 0);
368
        return;
368
        return;
369
    }
369
    }
-
 
370
    /* Clear any newly allocated memory in order to emulate gaps.  */
-
 
371
    memset(dentry->data + dentry->size, 0, delta);
370
    dentry->size += delta;
372
    dentry->size += delta;
371
    dentry->data = newdata;
373
    dentry->data = newdata;
372
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
374
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
373
    ipc_answer_1(rid, EOK, len);
375
    ipc_answer_1(rid, EOK, len);
374
}
376
}
375
 
377
 
376
/**
378
/**
377
 * @}
379
 * @}
378
 */
380
 */
379
 
381