Subversion Repositories HelenOS

Rev

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

Rev 4345 Rev 4347
1
/*
1
/*
2
 * Copyright (c) 2008 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 vfs_ops.c
34
 * @file vfs_ops.c
35
 * @brief Operations that VFS offers to its clients.
35
 * @brief Operations that VFS offers to its clients.
36
 */
36
 */
37
 
37
 
38
#include "vfs.h"
38
#include "vfs.h"
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <async.h>
40
#include <async.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <stdlib.h>
43
#include <stdlib.h>
44
#include <string.h>
44
#include <string.h>
45
#include <bool.h>
45
#include <bool.h>
46
#include <futex.h>
46
#include <futex.h>
47
#include <rwlock.h>
47
#include <rwlock.h>
48
#include <libadt/list.h>
48
#include <libadt/list.h>
49
#include <unistd.h>
49
#include <unistd.h>
50
#include <ctype.h>
50
#include <ctype.h>
51
#include <fcntl.h>
51
#include <fcntl.h>
52
#include <assert.h>
52
#include <assert.h>
53
#include <vfs/canonify.h>
53
#include <vfs/canonify.h>
54
 
54
 
55
/* Forward declarations of static functions. */
55
/* Forward declarations of static functions. */
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
56
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
57
 
57
 
58
/** Pending mount structure. */
58
/** Pending mount structure. */
59
typedef struct {
59
typedef struct {
60
    link_t link;
60
    link_t link;
61
    char *fs_name;            /**< File system name */
61
    char *fs_name;            /**< File system name */
62
    char *mp;                 /**< Mount point */
62
    char *mp;                 /**< Mount point */
63
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
63
    ipc_callid_t callid;      /**< Call ID waiting for the mount */
64
    ipc_callid_t rid;         /**< Request ID */
64
    ipc_callid_t rid;         /**< Request ID */
65
    dev_handle_t dev_handle;  /**< Device handle */
65
    dev_handle_t dev_handle;  /**< Device handle */
66
} pending_req_t;
66
} pending_req_t;
67
 
67
 
68
LIST_INITIALIZE(pending_req);
68
LIST_INITIALIZE(pending_req);
69
 
69
 
70
/**
70
/**
71
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
71
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
72
 * concurrent VFS operation which modifies the file system namespace.
72
 * concurrent VFS operation which modifies the file system namespace.
73
 */
73
 */
74
RWLOCK_INITIALIZE(namespace_rwlock);
74
RWLOCK_INITIALIZE(namespace_rwlock);
75
 
75
 
76
futex_t rootfs_futex = FUTEX_INITIALIZER;
76
futex_t rootfs_futex = FUTEX_INITIALIZER;
77
vfs_pair_t rootfs = {
77
vfs_pair_t rootfs = {
78
    .fs_handle = 0,
78
    .fs_handle = 0,
79
    .dev_handle = 0
79
    .dev_handle = 0
80
};
80
};
81
 
81
 
82
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
82
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
83
    fs_handle_t fs_handle, char *mp)
83
    fs_handle_t fs_handle, char *mp)
84
{
84
{
85
    /* Resolve the path to the mountpoint. */
85
    /* Resolve the path to the mountpoint. */
86
    vfs_lookup_res_t mp_res;
86
    vfs_lookup_res_t mp_res;
87
    vfs_node_t *mp_node = NULL;
87
    vfs_node_t *mp_node = NULL;
88
    int rc;
88
    int rc;
89
    int phone;
89
    int phone;
90
    futex_down(&rootfs_futex);
90
    futex_down(&rootfs_futex);
91
    if (rootfs.fs_handle) {
91
    if (rootfs.fs_handle) {
92
        /* We already have the root FS. */
92
        /* We already have the root FS. */
93
        rwlock_write_lock(&namespace_rwlock);
93
        rwlock_write_lock(&namespace_rwlock);
94
        if ((strlen(mp) == 1) && (mp[0] == '/')) {
94
        if ((strlen(mp) == 1) && (mp[0] == '/')) {
95
            /* Trying to mount root FS over root FS */
95
            /* Trying to mount root FS over root FS */
96
            rwlock_write_unlock(&namespace_rwlock);
96
            rwlock_write_unlock(&namespace_rwlock);
97
            futex_up(&rootfs_futex);
97
            futex_up(&rootfs_futex);
98
            ipc_answer_0(rid, EBUSY);
98
            ipc_answer_0(rid, EBUSY);
99
            return;
99
            return;
100
        }
100
        }
101
       
101
       
102
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
102
        rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
103
        if (rc != EOK) {
103
        if (rc != EOK) {
104
            /* The lookup failed for some reason. */
104
            /* The lookup failed for some reason. */
105
            rwlock_write_unlock(&namespace_rwlock);
105
            rwlock_write_unlock(&namespace_rwlock);
106
            futex_up(&rootfs_futex);
106
            futex_up(&rootfs_futex);
107
            ipc_answer_0(rid, rc);
107
            ipc_answer_0(rid, rc);
108
            return;
108
            return;
109
        }
109
        }
110
       
110
       
111
        mp_node = vfs_node_get(&mp_res);
111
        mp_node = vfs_node_get(&mp_res);
112
        if (!mp_node) {
112
        if (!mp_node) {
113
            rwlock_write_unlock(&namespace_rwlock);
113
            rwlock_write_unlock(&namespace_rwlock);
114
            futex_up(&rootfs_futex);
114
            futex_up(&rootfs_futex);
115
            ipc_answer_0(rid, ENOMEM);
115
            ipc_answer_0(rid, ENOMEM);
116
            return;
116
            return;
117
        }
117
        }
118
       
118
       
119
        /*
119
        /*
120
         * Now we hold a reference to mp_node.
120
         * Now we hold a reference to mp_node.
121
         * It will be dropped upon the corresponding VFS_UNMOUNT.
121
         * It will be dropped upon the corresponding VFS_UNMOUNT.
122
         * This prevents the mount point from being deleted.
122
         * This prevents the mount point from being deleted.
123
         */
123
         */
124
        rwlock_write_unlock(&namespace_rwlock);
124
        rwlock_write_unlock(&namespace_rwlock);
125
    } else {
125
    } else {
126
        /* We still don't have the root file system mounted. */
126
        /* We still don't have the root file system mounted. */
127
        if ((strlen(mp) == 1) && (mp[0] == '/')) {
127
        if ((strlen(mp) == 1) && (mp[0] == '/')) {
128
            vfs_lookup_res_t mr_res;
128
            vfs_lookup_res_t mr_res;
129
            vfs_node_t *mr_node;
129
            vfs_node_t *mr_node;
130
            ipcarg_t rindex;
130
            ipcarg_t rindex;
131
            ipcarg_t rsize;
131
            ipcarg_t rsize;
132
            ipcarg_t rlnkcnt;
132
            ipcarg_t rlnkcnt;
133
           
133
           
134
            /*
134
            /*
135
             * For this simple, but important case,
135
             * For this simple, but important case,
136
             * we are almost done.
136
             * we are almost done.
137
             */
137
             */
138
           
138
           
139
            /* Tell the mountee that it is being mounted. */
139
            /* Tell the mountee that it is being mounted. */
140
            phone = vfs_grab_phone(fs_handle);
140
            phone = vfs_grab_phone(fs_handle);
141
            rc = async_req_1_3(phone, VFS_MOUNTED,
141
            rc = async_req_1_3(phone, VFS_MOUNTED,
142
                (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
142
                (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
143
            vfs_release_phone(phone);
143
            vfs_release_phone(phone);
144
           
144
           
145
            if (rc != EOK) {
145
            if (rc != EOK) {
146
                futex_up(&rootfs_futex);
146
                futex_up(&rootfs_futex);
147
                ipc_answer_0(rid, rc);
147
                ipc_answer_0(rid, rc);
148
                return;
148
                return;
149
            }
149
            }
150
           
150
           
151
            mr_res.triplet.fs_handle = fs_handle;
151
            mr_res.triplet.fs_handle = fs_handle;
152
            mr_res.triplet.dev_handle = dev_handle;
152
            mr_res.triplet.dev_handle = dev_handle;
153
            mr_res.triplet.index = (fs_index_t) rindex;
153
            mr_res.triplet.index = (fs_index_t) rindex;
154
            mr_res.size = (size_t) rsize;
154
            mr_res.size = (size_t) rsize;
155
            mr_res.lnkcnt = (unsigned) rlnkcnt;
155
            mr_res.lnkcnt = (unsigned) rlnkcnt;
156
            mr_res.type = VFS_NODE_DIRECTORY;
156
            mr_res.type = VFS_NODE_DIRECTORY;
157
           
157
           
158
            rootfs.fs_handle = fs_handle;
158
            rootfs.fs_handle = fs_handle;
159
            rootfs.dev_handle = dev_handle;
159
            rootfs.dev_handle = dev_handle;
160
            futex_up(&rootfs_futex);
160
            futex_up(&rootfs_futex);
161
           
161
           
162
            /* Add reference to the mounted root. */
162
            /* Add reference to the mounted root. */
163
            mr_node = vfs_node_get(&mr_res);
163
            mr_node = vfs_node_get(&mr_res);
164
            assert(mr_node);
164
            assert(mr_node);
165
           
165
           
166
            ipc_answer_0(rid, rc);
166
            ipc_answer_0(rid, rc);
167
            return;
167
            return;
168
        } else {
168
        } else {
169
            /*
169
            /*
170
             * We can't resolve this without the root filesystem
170
             * We can't resolve this without the root filesystem
171
             * being mounted first.
171
             * being mounted first.
172
             */
172
             */
173
            futex_up(&rootfs_futex);
173
            futex_up(&rootfs_futex);
174
            ipc_answer_0(rid, ENOENT);
174
            ipc_answer_0(rid, ENOENT);
175
            return;
175
            return;
176
        }
176
        }
177
    }
177
    }
178
    futex_up(&rootfs_futex);
178
    futex_up(&rootfs_futex);
179
   
179
   
180
    /*
180
    /*
181
     * At this point, we have all necessary pieces: file system and device
181
     * At this point, we have all necessary pieces: file system and device
182
     * handles, and we know the mount point VFS node.
182
     * handles, and we know the mount point VFS node.
183
     */
183
     */
184
   
184
   
185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
185
    phone = vfs_grab_phone(mp_res.triplet.fs_handle);
186
    rc = async_req_4_0(phone, VFS_MOUNT,
186
    rc = async_req_4_0(phone, VFS_MOUNT,
187
        (ipcarg_t) mp_res.triplet.dev_handle,
187
        (ipcarg_t) mp_res.triplet.dev_handle,
188
        (ipcarg_t) mp_res.triplet.index,
188
        (ipcarg_t) mp_res.triplet.index,
189
        (ipcarg_t) fs_handle,
189
        (ipcarg_t) fs_handle,
190
        (ipcarg_t) dev_handle);
190
        (ipcarg_t) dev_handle);
191
    vfs_release_phone(phone);
191
    vfs_release_phone(phone);
192
   
192
   
193
    if (rc != EOK) {
193
    if (rc != EOK) {
194
        /* Mount failed, drop reference to mp_node. */
194
        /* Mount failed, drop reference to mp_node. */
195
        if (mp_node)
195
        if (mp_node)
196
            vfs_node_put(mp_node);
196
            vfs_node_put(mp_node);
197
    }
197
    }
198
   
198
   
199
    ipc_answer_0(rid, rc);
199
    ipc_answer_0(rid, rc);
200
}
200
}
201
 
201
 
202
/** Process pending mount requests */
202
/** Process pending mount requests */
203
void vfs_process_pending_mount()
203
void vfs_process_pending_mount()
204
{
204
{
205
    link_t *cur;
205
    link_t *cur;
206
   
206
   
207
loop:
207
loop:
208
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
208
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
209
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
209
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
210
       
210
       
211
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
211
        fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
212
        if (!fs_handle)
212
        if (!fs_handle)
213
            continue;
213
            continue;
214
       
214
       
215
        /* Acknowledge that we know fs_name. */
215
        /* Acknowledge that we know fs_name. */
216
        ipc_answer_0(pr->callid, EOK);
216
        ipc_answer_0(pr->callid, EOK);
217
       
217
       
218
        /* Do the mount */
218
        /* Do the mount */
219
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp);
219
        vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp);
220
       
220
       
221
        free(pr->fs_name);
221
        free(pr->fs_name);
222
        free(pr->mp);
222
        free(pr->mp);
223
        list_remove(cur);
223
        list_remove(cur);
224
        free(pr);
224
        free(pr);
225
        goto loop;
225
        goto loop;
226
    }
226
    }
227
}
227
}
228
 
228
 
229
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
229
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
230
{
230
{
231
    /*
231
    /*
232
     * We expect the library to do the device-name to device-handle
232
     * We expect the library to do the device-name to device-handle
233
     * translation for us, thus the device handle will arrive as ARG1
233
     * translation for us, thus the device handle will arrive as ARG1
234
     * in the request.
234
     * in the request.
235
     */
235
     */
236
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
236
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
237
   
237
   
238
    /*
238
    /*
239
     * Mount flags are passed as ARG2.
239
     * Mount flags are passed as ARG2.
240
     */
240
     */
241
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
241
    unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
242
   
242
   
243
    /*
243
    /*
244
     * For now, don't make use of ARG3, but it can be used to
244
     * For now, don't make use of ARG3, but it can be used to
245
     * carry mount options in the future.
245
     * carry mount options in the future.
246
     */
246
     */
247
   
247
   
248
    /* We want the client to send us the mount point. */
248
    /* We want the client to send us the mount point. */
249
    ipc_callid_t callid;
249
    ipc_callid_t callid;
250
    size_t size;
250
    size_t size;
251
    if (!ipc_data_write_receive(&callid, &size)) {
251
    if (!ipc_data_write_receive(&callid, &size)) {
252
        ipc_answer_0(callid, EINVAL);
252
        ipc_answer_0(callid, EINVAL);
253
        ipc_answer_0(rid, EINVAL);
253
        ipc_answer_0(rid, EINVAL);
254
        return;
254
        return;
255
    }
255
    }
256
   
256
   
257
    /* Check whether size is reasonable wrt. the mount point. */
257
    /* Check whether size is reasonable wrt. the mount point. */
258
    if ((size < 1) || (size > MAX_PATH_LEN)) {
258
    if ((size < 1) || (size > MAX_PATH_LEN)) {
259
        ipc_answer_0(callid, EINVAL);
259
        ipc_answer_0(callid, EINVAL);
260
        ipc_answer_0(rid, EINVAL);
260
        ipc_answer_0(rid, EINVAL);
261
        return;
261
        return;
262
    }
262
    }
263
   
263
   
264
    /* Allocate buffer for the mount point data being received. */
264
    /* Allocate buffer for the mount point data being received. */
265
    char *mp = malloc(size + 1);
265
    char *mp = malloc(size + 1);
266
    if (!mp) {
266
    if (!mp) {
267
        ipc_answer_0(callid, ENOMEM);
267
        ipc_answer_0(callid, ENOMEM);
268
        ipc_answer_0(rid, ENOMEM);
268
        ipc_answer_0(rid, ENOMEM);
269
        return;
269
        return;
270
    }
270
    }
271
   
271
   
272
    /* Deliver the mount point. */
272
    /* Deliver the mount point. */
273
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
273
    ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
274
    if (retval != EOK) {
274
    if (retval != EOK) {
275
        ipc_answer_0(rid, EREFUSED);
275
        ipc_answer_0(rid, EREFUSED);
276
        free(mp);
276
        free(mp);
277
        return;
277
        return;
278
    }
278
    }
279
    mp[size] = '\0';
279
    mp[size] = '\0';
280
   
280
   
281
    /*
281
    /*
282
     * Now, we expect the client to send us data with the name of the file
282
     * Now, we expect the client to send us data with the name of the file
283
     * system.
283
     * system.
284
     */
284
     */
285
    if (!ipc_data_write_receive(&callid, &size)) {
285
    if (!ipc_data_write_receive(&callid, &size)) {
286
        ipc_answer_0(callid, EINVAL);
286
        ipc_answer_0(callid, EINVAL);
287
        ipc_answer_0(rid, EINVAL);
287
        ipc_answer_0(rid, EINVAL);
288
        free(mp);
288
        free(mp);
289
        return;
289
        return;
290
    }
290
    }
291
   
291
   
292
    /*
292
    /*
293
     * Don't receive more than is necessary for storing a full file system
293
     * Don't receive more than is necessary for storing a full file system
294
     * name.
294
     * name.
295
     */
295
     */
296
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
296
    if ((size < 1) || (size > FS_NAME_MAXLEN)) {
297
        ipc_answer_0(callid, EINVAL);
297
        ipc_answer_0(callid, EINVAL);
298
        ipc_answer_0(rid, EINVAL);
298
        ipc_answer_0(rid, EINVAL);
299
        free(mp);
299
        free(mp);
300
        return;
300
        return;
301
    }
301
    }
302
   
302
   
303
    /*
303
    /*
304
     * Allocate buffer for file system name.
304
     * Allocate buffer for file system name.
305
     */
305
     */
306
    char *fs_name = (char *) malloc(size + 1);
306
    char *fs_name = (char *) malloc(size + 1);
307
    if (fs_name == NULL) {
307
    if (fs_name == NULL) {
308
        ipc_answer_0(callid, ENOMEM);
308
        ipc_answer_0(callid, ENOMEM);
309
        ipc_answer_0(rid, EREFUSED);
309
        ipc_answer_0(rid, EREFUSED);
310
        free(mp);
310
        free(mp);
311
        return;
311
        return;
312
    }
312
    }
313
   
313
   
314
    /* Deliver the file system name. */
314
    /* Deliver the file system name. */
315
    retval = ipc_data_write_finalize(callid, fs_name, size);
315
    retval = ipc_data_write_finalize(callid, fs_name, size);
316
    if (retval != EOK) {
316
    if (retval != EOK) {
317
        ipc_answer_0(rid, EREFUSED);
317
        ipc_answer_0(rid, EREFUSED);
318
        free(mp);
318
        free(mp);
319
        free(fs_name);
319
        free(fs_name);
320
        return;
320
        return;
321
    }
321
    }
322
    fs_name[size] = '\0';
322
    fs_name[size] = '\0';
323
   
323
   
324
    /*
324
    /*
325
     * Check if we know a file system with the same name as is in fs_name.
325
     * Check if we know a file system with the same name as is in fs_name.
326
     * This will also give us its file system handle.
326
     * This will also give us its file system handle.
327
     */
327
     */
328
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
328
    fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
329
    if (!fs_handle) {
329
    if (!fs_handle) {
330
        if (flags & IPC_FLAG_BLOCKING) {
330
        if (flags & IPC_FLAG_BLOCKING) {
331
            /* Blocking mount, add to pending list */
331
            /* Blocking mount, add to pending list */
332
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
332
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
333
            if (!pr) {
333
            if (!pr) {
334
                ipc_answer_0(callid, ENOMEM);
334
                ipc_answer_0(callid, ENOMEM);
335
                ipc_answer_0(rid, ENOMEM);
335
                ipc_answer_0(rid, ENOMEM);
336
                free(mp);
336
                free(mp);
337
                free(fs_name);
337
                free(fs_name);
338
                return;
338
                return;
339
            }
339
            }
340
           
340
           
341
            pr->fs_name = fs_name;
341
            pr->fs_name = fs_name;
342
            pr->mp = mp;
342
            pr->mp = mp;
343
            pr->callid = callid;
343
            pr->callid = callid;
344
            pr->rid = rid;
344
            pr->rid = rid;
345
            pr->dev_handle = dev_handle;
345
            pr->dev_handle = dev_handle;
-
 
346
            link_initialize(&pr->link);
346
            list_append(&pr->link, &pending_req);
347
            list_append(&pr->link, &pending_req);
347
            return;
348
            return;
348
        }
349
        }
349
       
350
       
350
        ipc_answer_0(callid, ENOENT);
351
        ipc_answer_0(callid, ENOENT);
351
        ipc_answer_0(rid, ENOENT);
352
        ipc_answer_0(rid, ENOENT);
352
        free(mp);
353
        free(mp);
353
        free(fs_name);
354
        free(fs_name);
354
        return;
355
        return;
355
    }
356
    }
356
   
357
   
357
    /* Acknowledge that we know fs_name. */
358
    /* Acknowledge that we know fs_name. */
358
    ipc_answer_0(callid, EOK);
359
    ipc_answer_0(callid, EOK);
359
   
360
   
360
    /* Do the mount */
361
    /* Do the mount */
361
    vfs_mount_internal(rid, dev_handle, fs_handle, mp);
362
    vfs_mount_internal(rid, dev_handle, fs_handle, mp);
362
    free(mp);
363
    free(mp);
363
    free(fs_name);
364
    free(fs_name);
364
}
365
}
365
 
366
 
366
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
367
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
367
{
368
{
368
    if (!vfs_files_init()) {
369
    if (!vfs_files_init()) {
369
        ipc_answer_0(rid, ENOMEM);
370
        ipc_answer_0(rid, ENOMEM);
370
        return;
371
        return;
371
    }
372
    }
372
 
373
 
373
    /*
374
    /*
374
     * The POSIX interface is open(path, oflag, mode).
375
     * The POSIX interface is open(path, oflag, mode).
375
     * We can receive oflags and mode along with the VFS_OPEN call; the path
376
     * We can receive oflags and mode along with the VFS_OPEN call; the path
376
     * will need to arrive in another call.
377
     * will need to arrive in another call.
377
     *
378
     *
378
     * We also receive one private, non-POSIX set of flags called lflag
379
     * We also receive one private, non-POSIX set of flags called lflag
379
     * used to pass information to vfs_lookup_internal().
380
     * used to pass information to vfs_lookup_internal().
380
     */
381
     */
381
    int lflag = IPC_GET_ARG1(*request);
382
    int lflag = IPC_GET_ARG1(*request);
382
    int oflag = IPC_GET_ARG2(*request);
383
    int oflag = IPC_GET_ARG2(*request);
383
    int mode = IPC_GET_ARG3(*request);
384
    int mode = IPC_GET_ARG3(*request);
384
    size_t len;
385
    size_t len;
385
 
386
 
386
    /*
387
    /*
387
     * Make sure that we are called with exactly one of L_FILE and
388
     * Make sure that we are called with exactly one of L_FILE and
388
     * L_DIRECTORY.
389
     * L_DIRECTORY.
389
     */
390
     */
390
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
391
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
391
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
392
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
392
        ipc_answer_0(rid, EINVAL);
393
        ipc_answer_0(rid, EINVAL);
393
        return;
394
        return;
394
    }
395
    }
395
 
396
 
396
    if (oflag & O_CREAT)
397
    if (oflag & O_CREAT)
397
        lflag |= L_CREATE;
398
        lflag |= L_CREATE;
398
    if (oflag & O_EXCL)
399
    if (oflag & O_EXCL)
399
        lflag |= L_EXCLUSIVE;
400
        lflag |= L_EXCLUSIVE;
400
 
401
 
401
    ipc_callid_t callid;
402
    ipc_callid_t callid;
402
 
403
 
403
    if (!ipc_data_write_receive(&callid, &len)) {
404
    if (!ipc_data_write_receive(&callid, &len)) {
404
        ipc_answer_0(callid, EINVAL);
405
        ipc_answer_0(callid, EINVAL);
405
        ipc_answer_0(rid, EINVAL);
406
        ipc_answer_0(rid, EINVAL);
406
        return;
407
        return;
407
    }
408
    }
408
    char *path = malloc(len + 1);
409
    char *path = malloc(len + 1);
409
    if (!path) {
410
    if (!path) {
410
        ipc_answer_0(callid, ENOMEM);
411
        ipc_answer_0(callid, ENOMEM);
411
        ipc_answer_0(rid, ENOMEM);
412
        ipc_answer_0(rid, ENOMEM);
412
        return;
413
        return;
413
    }
414
    }
414
    int rc;
415
    int rc;
415
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
416
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
416
        ipc_answer_0(rid, rc);
417
        ipc_answer_0(rid, rc);
417
        free(path);
418
        free(path);
418
        return;
419
        return;
419
    }
420
    }
420
    path[len] = '\0';
421
    path[len] = '\0';
421
   
422
   
422
    /*
423
    /*
423
     * Avoid the race condition in which the file can be deleted before we
424
     * Avoid the race condition in which the file can be deleted before we
424
     * find/create-and-lock the VFS node corresponding to the looked-up
425
     * find/create-and-lock the VFS node corresponding to the looked-up
425
     * triplet.
426
     * triplet.
426
     */
427
     */
427
    if (lflag & L_CREATE)
428
    if (lflag & L_CREATE)
428
        rwlock_write_lock(&namespace_rwlock);
429
        rwlock_write_lock(&namespace_rwlock);
429
    else
430
    else
430
        rwlock_read_lock(&namespace_rwlock);
431
        rwlock_read_lock(&namespace_rwlock);
431
 
432
 
432
    /* The path is now populated and we can call vfs_lookup_internal(). */
433
    /* The path is now populated and we can call vfs_lookup_internal(). */
433
    vfs_lookup_res_t lr;
434
    vfs_lookup_res_t lr;
434
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
435
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
435
    if (rc) {
436
    if (rc) {
436
        if (lflag & L_CREATE)
437
        if (lflag & L_CREATE)
437
            rwlock_write_unlock(&namespace_rwlock);
438
            rwlock_write_unlock(&namespace_rwlock);
438
        else
439
        else
439
            rwlock_read_unlock(&namespace_rwlock);
440
            rwlock_read_unlock(&namespace_rwlock);
440
        ipc_answer_0(rid, rc);
441
        ipc_answer_0(rid, rc);
441
        free(path);
442
        free(path);
442
        return;
443
        return;
443
    }
444
    }
444
 
445
 
445
    /* Path is no longer needed. */
446
    /* Path is no longer needed. */
446
    free(path);
447
    free(path);
447
 
448
 
448
    vfs_node_t *node = vfs_node_get(&lr);
449
    vfs_node_t *node = vfs_node_get(&lr);
449
    if (lflag & L_CREATE)
450
    if (lflag & L_CREATE)
450
        rwlock_write_unlock(&namespace_rwlock);
451
        rwlock_write_unlock(&namespace_rwlock);
451
    else
452
    else
452
        rwlock_read_unlock(&namespace_rwlock);
453
        rwlock_read_unlock(&namespace_rwlock);
453
 
454
 
454
    /* Truncate the file if requested and if necessary. */
455
    /* Truncate the file if requested and if necessary. */
455
    if (oflag & O_TRUNC) {
456
    if (oflag & O_TRUNC) {
456
        rwlock_write_lock(&node->contents_rwlock);
457
        rwlock_write_lock(&node->contents_rwlock);
457
        if (node->size) {
458
        if (node->size) {
458
            rc = vfs_truncate_internal(node->fs_handle,
459
            rc = vfs_truncate_internal(node->fs_handle,
459
                node->dev_handle, node->index, 0);
460
                node->dev_handle, node->index, 0);
460
            if (rc) {
461
            if (rc) {
461
                rwlock_write_unlock(&node->contents_rwlock);
462
                rwlock_write_unlock(&node->contents_rwlock);
462
                vfs_node_put(node);
463
                vfs_node_put(node);
463
                ipc_answer_0(rid, rc);
464
                ipc_answer_0(rid, rc);
464
                return;
465
                return;
465
            }
466
            }
466
            node->size = 0;
467
            node->size = 0;
467
        }
468
        }
468
        rwlock_write_unlock(&node->contents_rwlock);
469
        rwlock_write_unlock(&node->contents_rwlock);
469
    }
470
    }
470
 
471
 
471
    /*
472
    /*
472
     * Get ourselves a file descriptor and the corresponding vfs_file_t
473
     * Get ourselves a file descriptor and the corresponding vfs_file_t
473
     * structure.
474
     * structure.
474
     */
475
     */
475
    int fd = vfs_fd_alloc();
476
    int fd = vfs_fd_alloc();
476
    if (fd < 0) {
477
    if (fd < 0) {
477
        vfs_node_put(node);
478
        vfs_node_put(node);
478
        ipc_answer_0(rid, fd);
479
        ipc_answer_0(rid, fd);
479
        return;
480
        return;
480
    }
481
    }
481
    vfs_file_t *file = vfs_file_get(fd);
482
    vfs_file_t *file = vfs_file_get(fd);
482
    file->node = node;
483
    file->node = node;
483
    if (oflag & O_APPEND)
484
    if (oflag & O_APPEND)
484
        file->append = true;
485
        file->append = true;
485
 
486
 
486
    /*
487
    /*
487
     * The following increase in reference count is for the fact that the
488
     * The following increase in reference count is for the fact that the
488
     * file is being opened and that a file structure is pointing to it.
489
     * file is being opened and that a file structure is pointing to it.
489
     * It is necessary so that the file will not disappear when
490
     * It is necessary so that the file will not disappear when
490
     * vfs_node_put() is called. The reference will be dropped by the
491
     * vfs_node_put() is called. The reference will be dropped by the
491
     * respective VFS_CLOSE.
492
     * respective VFS_CLOSE.
492
     */
493
     */
493
    vfs_node_addref(node);
494
    vfs_node_addref(node);
494
    vfs_node_put(node);
495
    vfs_node_put(node);
495
 
496
 
496
    /* Success! Return the new file descriptor to the client. */
497
    /* Success! Return the new file descriptor to the client. */
497
    ipc_answer_1(rid, EOK, fd);
498
    ipc_answer_1(rid, EOK, fd);
498
}
499
}
499
 
500
 
500
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
501
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
501
{
502
{
502
    int fd = IPC_GET_ARG1(*request);
503
    int fd = IPC_GET_ARG1(*request);
503
    int rc = vfs_fd_free(fd);
504
    int rc = vfs_fd_free(fd);
504
    ipc_answer_0(rid, rc);
505
    ipc_answer_0(rid, rc);
505
}
506
}
506
 
507
 
507
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
508
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
508
{
509
{
509
 
510
 
510
    /*
511
    /*
511
     * The following code strongly depends on the fact that the files data
512
     * The following code strongly depends on the fact that the files data
512
     * structure can be only accessed by a single fibril and all file
513
     * structure can be only accessed by a single fibril and all file
513
     * operations are serialized (i.e. the reads and writes cannot
514
     * operations are serialized (i.e. the reads and writes cannot
514
     * interleave and a file cannot be closed while it is being read).
515
     * interleave and a file cannot be closed while it is being read).
515
     *
516
     *
516
     * Additional synchronization needs to be added once the table of
517
     * Additional synchronization needs to be added once the table of
517
     * open files supports parallel access!
518
     * open files supports parallel access!
518
     */
519
     */
519
 
520
 
520
    int fd = IPC_GET_ARG1(*request);
521
    int fd = IPC_GET_ARG1(*request);
521
   
522
   
522
    /* Lookup the file structure corresponding to the file descriptor. */
523
    /* Lookup the file structure corresponding to the file descriptor. */
523
    vfs_file_t *file = vfs_file_get(fd);
524
    vfs_file_t *file = vfs_file_get(fd);
524
    if (!file) {
525
    if (!file) {
525
        ipc_answer_0(rid, ENOENT);
526
        ipc_answer_0(rid, ENOENT);
526
        return;
527
        return;
527
    }
528
    }
528
   
529
   
529
    /*
530
    /*
530
     * Now we need to receive a call with client's
531
     * Now we need to receive a call with client's
531
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
532
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
532
     */
533
     */
533
    ipc_callid_t callid;
534
    ipc_callid_t callid;
534
    int res;
535
    int res;
535
    if (read)
536
    if (read)
536
        res = ipc_data_read_receive(&callid, NULL);
537
        res = ipc_data_read_receive(&callid, NULL);
537
    else
538
    else
538
        res = ipc_data_write_receive(&callid, NULL);
539
        res = ipc_data_write_receive(&callid, NULL);
539
    if (!res) {
540
    if (!res) {
540
        ipc_answer_0(callid, EINVAL);
541
        ipc_answer_0(callid, EINVAL);
541
        ipc_answer_0(rid, EINVAL);
542
        ipc_answer_0(rid, EINVAL);
542
        return;
543
        return;
543
    }
544
    }
544
   
545
   
545
    /*
546
    /*
546
     * Lock the open file structure so that no other thread can manipulate
547
     * Lock the open file structure so that no other thread can manipulate
547
     * the same open file at a time.
548
     * the same open file at a time.
548
     */
549
     */
549
    futex_down(&file->lock);
550
    futex_down(&file->lock);
550
 
551
 
551
    /*
552
    /*
552
     * Lock the file's node so that no other client can read/write to it at
553
     * Lock the file's node so that no other client can read/write to it at
553
     * the same time.
554
     * the same time.
554
     */
555
     */
555
    if (read)
556
    if (read)
556
        rwlock_read_lock(&file->node->contents_rwlock);
557
        rwlock_read_lock(&file->node->contents_rwlock);
557
    else
558
    else
558
        rwlock_write_lock(&file->node->contents_rwlock);
559
        rwlock_write_lock(&file->node->contents_rwlock);
559
 
560
 
560
    if (file->node->type == VFS_NODE_DIRECTORY) {
561
    if (file->node->type == VFS_NODE_DIRECTORY) {
561
        /*
562
        /*
562
         * Make sure that no one is modifying the namespace
563
         * Make sure that no one is modifying the namespace
563
         * while we are in readdir().
564
         * while we are in readdir().
564
         */
565
         */
565
        assert(read);
566
        assert(read);
566
        rwlock_read_lock(&namespace_rwlock);
567
        rwlock_read_lock(&namespace_rwlock);
567
    }
568
    }
568
   
569
   
569
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
570
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
570
   
571
   
571
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
572
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
572
    aid_t msg;
573
    aid_t msg;
573
    ipc_call_t answer;
574
    ipc_call_t answer;
574
    if (!read && file->append)
575
    if (!read && file->append)
575
        file->pos = file->node->size;
576
        file->pos = file->node->size;
576
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
577
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
577
        file->node->dev_handle, file->node->index, file->pos, &answer);
578
        file->node->dev_handle, file->node->index, file->pos, &answer);
578
   
579
   
579
    /*
580
    /*
580
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
581
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
581
     * destination FS server. The call will be routed as if sent by
582
     * destination FS server. The call will be routed as if sent by
582
     * ourselves. Note that call arguments are immutable in this case so we
583
     * ourselves. Note that call arguments are immutable in this case so we
583
     * don't have to bother.
584
     * don't have to bother.
584
     */
585
     */
585
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
586
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
586
   
587
   
587
    vfs_release_phone(fs_phone);
588
    vfs_release_phone(fs_phone);
588
   
589
   
589
    /* Wait for reply from the FS server. */
590
    /* Wait for reply from the FS server. */
590
    ipcarg_t rc;
591
    ipcarg_t rc;
591
    async_wait_for(msg, &rc);
592
    async_wait_for(msg, &rc);
592
    size_t bytes = IPC_GET_ARG1(answer);
593
    size_t bytes = IPC_GET_ARG1(answer);
593
 
594
 
594
    if (file->node->type == VFS_NODE_DIRECTORY)
595
    if (file->node->type == VFS_NODE_DIRECTORY)
595
        rwlock_read_unlock(&namespace_rwlock);
596
        rwlock_read_unlock(&namespace_rwlock);
596
   
597
   
597
    /* Unlock the VFS node. */
598
    /* Unlock the VFS node. */
598
    if (read)
599
    if (read)
599
        rwlock_read_unlock(&file->node->contents_rwlock);
600
        rwlock_read_unlock(&file->node->contents_rwlock);
600
    else {
601
    else {
601
        /* Update the cached version of node's size. */
602
        /* Update the cached version of node's size. */
602
        if (rc == EOK)
603
        if (rc == EOK)
603
            file->node->size = IPC_GET_ARG2(answer);
604
            file->node->size = IPC_GET_ARG2(answer);
604
        rwlock_write_unlock(&file->node->contents_rwlock);
605
        rwlock_write_unlock(&file->node->contents_rwlock);
605
    }
606
    }
606
   
607
   
607
    /* Update the position pointer and unlock the open file. */
608
    /* Update the position pointer and unlock the open file. */
608
    if (rc == EOK)
609
    if (rc == EOK)
609
        file->pos += bytes;
610
        file->pos += bytes;
610
    futex_up(&file->lock);
611
    futex_up(&file->lock);
611
   
612
   
612
    /*
613
    /*
613
     * FS server's reply is the final result of the whole operation we
614
     * FS server's reply is the final result of the whole operation we
614
     * return to the client.
615
     * return to the client.
615
     */
616
     */
616
    ipc_answer_1(rid, rc, bytes);
617
    ipc_answer_1(rid, rc, bytes);
617
}
618
}
618
 
619
 
619
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
620
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
620
{
621
{
621
    vfs_rdwr(rid, request, true);
622
    vfs_rdwr(rid, request, true);
622
}
623
}
623
 
624
 
624
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
625
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
625
{
626
{
626
    vfs_rdwr(rid, request, false);
627
    vfs_rdwr(rid, request, false);
627
}
628
}
628
 
629
 
629
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
630
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
630
{
631
{
631
    int fd = (int) IPC_GET_ARG1(*request);
632
    int fd = (int) IPC_GET_ARG1(*request);
632
    off_t off = (off_t) IPC_GET_ARG2(*request);
633
    off_t off = (off_t) IPC_GET_ARG2(*request);
633
    int whence = (int) IPC_GET_ARG3(*request);
634
    int whence = (int) IPC_GET_ARG3(*request);
634
 
635
 
635
 
636
 
636
    /* Lookup the file structure corresponding to the file descriptor. */
637
    /* Lookup the file structure corresponding to the file descriptor. */
637
    vfs_file_t *file = vfs_file_get(fd);
638
    vfs_file_t *file = vfs_file_get(fd);
638
    if (!file) {
639
    if (!file) {
639
        ipc_answer_0(rid, ENOENT);
640
        ipc_answer_0(rid, ENOENT);
640
        return;
641
        return;
641
    }
642
    }
642
 
643
 
643
    off_t newpos;
644
    off_t newpos;
644
    futex_down(&file->lock);
645
    futex_down(&file->lock);
645
    if (whence == SEEK_SET) {
646
    if (whence == SEEK_SET) {
646
        file->pos = off;
647
        file->pos = off;
647
        futex_up(&file->lock);
648
        futex_up(&file->lock);
648
        ipc_answer_1(rid, EOK, off);
649
        ipc_answer_1(rid, EOK, off);
649
        return;
650
        return;
650
    }
651
    }
651
    if (whence == SEEK_CUR) {
652
    if (whence == SEEK_CUR) {
652
        if (file->pos + off < file->pos) {
653
        if (file->pos + off < file->pos) {
653
            futex_up(&file->lock);
654
            futex_up(&file->lock);
654
            ipc_answer_0(rid, EOVERFLOW);
655
            ipc_answer_0(rid, EOVERFLOW);
655
            return;
656
            return;
656
        }
657
        }
657
        file->pos += off;
658
        file->pos += off;
658
        newpos = file->pos;
659
        newpos = file->pos;
659
        futex_up(&file->lock);
660
        futex_up(&file->lock);
660
        ipc_answer_1(rid, EOK, newpos);
661
        ipc_answer_1(rid, EOK, newpos);
661
        return;
662
        return;
662
    }
663
    }
663
    if (whence == SEEK_END) {
664
    if (whence == SEEK_END) {
664
        rwlock_read_lock(&file->node->contents_rwlock);
665
        rwlock_read_lock(&file->node->contents_rwlock);
665
        size_t size = file->node->size;
666
        size_t size = file->node->size;
666
        rwlock_read_unlock(&file->node->contents_rwlock);
667
        rwlock_read_unlock(&file->node->contents_rwlock);
667
        if (size + off < size) {
668
        if (size + off < size) {
668
            futex_up(&file->lock);
669
            futex_up(&file->lock);
669
            ipc_answer_0(rid, EOVERFLOW);
670
            ipc_answer_0(rid, EOVERFLOW);
670
            return;
671
            return;
671
        }
672
        }
672
        newpos = size + off;
673
        newpos = size + off;
673
        futex_up(&file->lock);
674
        futex_up(&file->lock);
674
        ipc_answer_1(rid, EOK, newpos);
675
        ipc_answer_1(rid, EOK, newpos);
675
        return;
676
        return;
676
    }
677
    }
677
    futex_up(&file->lock);
678
    futex_up(&file->lock);
678
    ipc_answer_0(rid, EINVAL);
679
    ipc_answer_0(rid, EINVAL);
679
}
680
}
680
 
681
 
681
int
682
int
682
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
683
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
683
    fs_index_t index, size_t size)
684
    fs_index_t index, size_t size)
684
{
685
{
685
    ipcarg_t rc;
686
    ipcarg_t rc;
686
    int fs_phone;
687
    int fs_phone;
687
   
688
   
688
    fs_phone = vfs_grab_phone(fs_handle);
689
    fs_phone = vfs_grab_phone(fs_handle);
689
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
690
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
690
        (ipcarg_t)index, (ipcarg_t)size);
691
        (ipcarg_t)index, (ipcarg_t)size);
691
    vfs_release_phone(fs_phone);
692
    vfs_release_phone(fs_phone);
692
    return (int)rc;
693
    return (int)rc;
693
}
694
}
694
 
695
 
695
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
696
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
696
{
697
{
697
    int fd = IPC_GET_ARG1(*request);
698
    int fd = IPC_GET_ARG1(*request);
698
    size_t size = IPC_GET_ARG2(*request);
699
    size_t size = IPC_GET_ARG2(*request);
699
    int rc;
700
    int rc;
700
 
701
 
701
    vfs_file_t *file = vfs_file_get(fd);
702
    vfs_file_t *file = vfs_file_get(fd);
702
    if (!file) {
703
    if (!file) {
703
        ipc_answer_0(rid, ENOENT);
704
        ipc_answer_0(rid, ENOENT);
704
        return;
705
        return;
705
    }
706
    }
706
    futex_down(&file->lock);
707
    futex_down(&file->lock);
707
 
708
 
708
    rwlock_write_lock(&file->node->contents_rwlock);
709
    rwlock_write_lock(&file->node->contents_rwlock);
709
    rc = vfs_truncate_internal(file->node->fs_handle,
710
    rc = vfs_truncate_internal(file->node->fs_handle,
710
        file->node->dev_handle, file->node->index, size);
711
        file->node->dev_handle, file->node->index, size);
711
    if (rc == EOK)
712
    if (rc == EOK)
712
        file->node->size = size;
713
        file->node->size = size;
713
    rwlock_write_unlock(&file->node->contents_rwlock);
714
    rwlock_write_unlock(&file->node->contents_rwlock);
714
 
715
 
715
    futex_up(&file->lock);
716
    futex_up(&file->lock);
716
    ipc_answer_0(rid, (ipcarg_t)rc);
717
    ipc_answer_0(rid, (ipcarg_t)rc);
717
}
718
}
718
 
719
 
719
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
720
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
720
{
721
{
721
    int mode = IPC_GET_ARG1(*request);
722
    int mode = IPC_GET_ARG1(*request);
722
 
723
 
723
    size_t len;
724
    size_t len;
724
    ipc_callid_t callid;
725
    ipc_callid_t callid;
725
 
726
 
726
    if (!ipc_data_write_receive(&callid, &len)) {
727
    if (!ipc_data_write_receive(&callid, &len)) {
727
        ipc_answer_0(callid, EINVAL);
728
        ipc_answer_0(callid, EINVAL);
728
        ipc_answer_0(rid, EINVAL);
729
        ipc_answer_0(rid, EINVAL);
729
        return;
730
        return;
730
    }
731
    }
731
    char *path = malloc(len + 1);
732
    char *path = malloc(len + 1);
732
    if (!path) {
733
    if (!path) {
733
        ipc_answer_0(callid, ENOMEM);
734
        ipc_answer_0(callid, ENOMEM);
734
        ipc_answer_0(rid, ENOMEM);
735
        ipc_answer_0(rid, ENOMEM);
735
        return;
736
        return;
736
    }
737
    }
737
    int rc;
738
    int rc;
738
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
739
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
739
        ipc_answer_0(rid, rc);
740
        ipc_answer_0(rid, rc);
740
        free(path);
741
        free(path);
741
        return;
742
        return;
742
    }
743
    }
743
    path[len] = '\0';
744
    path[len] = '\0';
744
   
745
   
745
    rwlock_write_lock(&namespace_rwlock);
746
    rwlock_write_lock(&namespace_rwlock);
746
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
747
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
747
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
748
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
748
    rwlock_write_unlock(&namespace_rwlock);
749
    rwlock_write_unlock(&namespace_rwlock);
749
    free(path);
750
    free(path);
750
    ipc_answer_0(rid, rc);
751
    ipc_answer_0(rid, rc);
751
}
752
}
752
 
753
 
753
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
754
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
754
{
755
{
755
    int lflag = IPC_GET_ARG1(*request);
756
    int lflag = IPC_GET_ARG1(*request);
756
 
757
 
757
    size_t len;
758
    size_t len;
758
    ipc_callid_t callid;
759
    ipc_callid_t callid;
759
 
760
 
760
    if (!ipc_data_write_receive(&callid, &len)) {
761
    if (!ipc_data_write_receive(&callid, &len)) {
761
        ipc_answer_0(callid, EINVAL);
762
        ipc_answer_0(callid, EINVAL);
762
        ipc_answer_0(rid, EINVAL);
763
        ipc_answer_0(rid, EINVAL);
763
        return;
764
        return;
764
    }
765
    }
765
    char *path = malloc(len + 1);
766
    char *path = malloc(len + 1);
766
    if (!path) {
767
    if (!path) {
767
        ipc_answer_0(callid, ENOMEM);
768
        ipc_answer_0(callid, ENOMEM);
768
        ipc_answer_0(rid, ENOMEM);
769
        ipc_answer_0(rid, ENOMEM);
769
        return;
770
        return;
770
    }
771
    }
771
    int rc;
772
    int rc;
772
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
773
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
773
        ipc_answer_0(rid, rc);
774
        ipc_answer_0(rid, rc);
774
        free(path);
775
        free(path);
775
        return;
776
        return;
776
    }
777
    }
777
    path[len] = '\0';
778
    path[len] = '\0';
778
   
779
   
779
    rwlock_write_lock(&namespace_rwlock);
780
    rwlock_write_lock(&namespace_rwlock);
780
    lflag &= L_DIRECTORY;   /* sanitize lflag */
781
    lflag &= L_DIRECTORY;   /* sanitize lflag */
781
    vfs_lookup_res_t lr;
782
    vfs_lookup_res_t lr;
782
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
783
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
783
    free(path);
784
    free(path);
784
    if (rc != EOK) {
785
    if (rc != EOK) {
785
        rwlock_write_unlock(&namespace_rwlock);
786
        rwlock_write_unlock(&namespace_rwlock);
786
        ipc_answer_0(rid, rc);
787
        ipc_answer_0(rid, rc);
787
        return;
788
        return;
788
    }
789
    }
789
 
790
 
790
    /*
791
    /*
791
     * The name has already been unlinked by vfs_lookup_internal().
792
     * The name has already been unlinked by vfs_lookup_internal().
792
     * We have to get and put the VFS node to ensure that it is
793
     * We have to get and put the VFS node to ensure that it is
793
     * VFS_DESTROY'ed after the last reference to it is dropped.
794
     * VFS_DESTROY'ed after the last reference to it is dropped.
794
     */
795
     */
795
    vfs_node_t *node = vfs_node_get(&lr);
796
    vfs_node_t *node = vfs_node_get(&lr);
796
    futex_down(&nodes_futex);
797
    futex_down(&nodes_futex);
797
    node->lnkcnt--;
798
    node->lnkcnt--;
798
    futex_up(&nodes_futex);
799
    futex_up(&nodes_futex);
799
    rwlock_write_unlock(&namespace_rwlock);
800
    rwlock_write_unlock(&namespace_rwlock);
800
    vfs_node_put(node);
801
    vfs_node_put(node);
801
    ipc_answer_0(rid, EOK);
802
    ipc_answer_0(rid, EOK);
802
}
803
}
803
 
804
 
804
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
805
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
805
{
806
{
806
    size_t len;
807
    size_t len;
807
    ipc_callid_t callid;
808
    ipc_callid_t callid;
808
    int rc;
809
    int rc;
809
 
810
 
810
    /* Retrieve the old path. */
811
    /* Retrieve the old path. */
811
    if (!ipc_data_write_receive(&callid, &len)) {
812
    if (!ipc_data_write_receive(&callid, &len)) {
812
        ipc_answer_0(callid, EINVAL);
813
        ipc_answer_0(callid, EINVAL);
813
        ipc_answer_0(rid, EINVAL);
814
        ipc_answer_0(rid, EINVAL);
814
        return;
815
        return;
815
    }
816
    }
816
    char *old = malloc(len + 1);
817
    char *old = malloc(len + 1);
817
    if (!old) {
818
    if (!old) {
818
        ipc_answer_0(callid, ENOMEM);
819
        ipc_answer_0(callid, ENOMEM);
819
        ipc_answer_0(rid, ENOMEM);
820
        ipc_answer_0(rid, ENOMEM);
820
        return;
821
        return;
821
    }
822
    }
822
    if ((rc = ipc_data_write_finalize(callid, old, len))) {
823
    if ((rc = ipc_data_write_finalize(callid, old, len))) {
823
        ipc_answer_0(rid, rc);
824
        ipc_answer_0(rid, rc);
824
        free(old);
825
        free(old);
825
        return;
826
        return;
826
    }
827
    }
827
    old[len] = '\0';
828
    old[len] = '\0';
828
   
829
   
829
    /* Retrieve the new path. */
830
    /* Retrieve the new path. */
830
    if (!ipc_data_write_receive(&callid, &len)) {
831
    if (!ipc_data_write_receive(&callid, &len)) {
831
        ipc_answer_0(callid, EINVAL);
832
        ipc_answer_0(callid, EINVAL);
832
        ipc_answer_0(rid, EINVAL);
833
        ipc_answer_0(rid, EINVAL);
833
        free(old);
834
        free(old);
834
        return;
835
        return;
835
    }
836
    }
836
    char *new = malloc(len + 1);
837
    char *new = malloc(len + 1);
837
    if (!new) {
838
    if (!new) {
838
        ipc_answer_0(callid, ENOMEM);
839
        ipc_answer_0(callid, ENOMEM);
839
        ipc_answer_0(rid, ENOMEM);
840
        ipc_answer_0(rid, ENOMEM);
840
        free(old);
841
        free(old);
841
        return;
842
        return;
842
    }
843
    }
843
    if ((rc = ipc_data_write_finalize(callid, new, len))) {
844
    if ((rc = ipc_data_write_finalize(callid, new, len))) {
844
        ipc_answer_0(rid, rc);
845
        ipc_answer_0(rid, rc);
845
        free(old);
846
        free(old);
846
        free(new);
847
        free(new);
847
        return;
848
        return;
848
    }
849
    }
849
    new[len] = '\0';
850
    new[len] = '\0';
850
 
851
 
851
    char *oldc = canonify(old, &len);
852
    char *oldc = canonify(old, &len);
852
    char *newc = canonify(new, NULL);
853
    char *newc = canonify(new, NULL);
853
    if (!oldc || !newc) {
854
    if (!oldc || !newc) {
854
        ipc_answer_0(rid, EINVAL);
855
        ipc_answer_0(rid, EINVAL);
855
        free(old);
856
        free(old);
856
        free(new);
857
        free(new);
857
        return;
858
        return;
858
    }
859
    }
859
    if (!strncmp(newc, oldc, len)) {
860
    if (!strncmp(newc, oldc, len)) {
860
        /* oldc is a prefix of newc */
861
        /* oldc is a prefix of newc */
861
        ipc_answer_0(rid, EINVAL);
862
        ipc_answer_0(rid, EINVAL);
862
        free(old);
863
        free(old);
863
        free(new);
864
        free(new);
864
        return;
865
        return;
865
    }
866
    }
866
   
867
   
867
    vfs_lookup_res_t old_lr;
868
    vfs_lookup_res_t old_lr;
868
    vfs_lookup_res_t new_lr;
869
    vfs_lookup_res_t new_lr;
869
    vfs_lookup_res_t new_par_lr;
870
    vfs_lookup_res_t new_par_lr;
870
    rwlock_write_lock(&namespace_rwlock);
871
    rwlock_write_lock(&namespace_rwlock);
871
    /* Lookup the node belonging to the old file name. */
872
    /* Lookup the node belonging to the old file name. */
872
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
873
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
873
    if (rc != EOK) {
874
    if (rc != EOK) {
874
        rwlock_write_unlock(&namespace_rwlock);
875
        rwlock_write_unlock(&namespace_rwlock);
875
        ipc_answer_0(rid, rc);
876
        ipc_answer_0(rid, rc);
876
        free(old);
877
        free(old);
877
        free(new);
878
        free(new);
878
        return;
879
        return;
879
    }
880
    }
880
    vfs_node_t *old_node = vfs_node_get(&old_lr);
881
    vfs_node_t *old_node = vfs_node_get(&old_lr);
881
    if (!old_node) {
882
    if (!old_node) {
882
        rwlock_write_unlock(&namespace_rwlock);
883
        rwlock_write_unlock(&namespace_rwlock);
883
        ipc_answer_0(rid, ENOMEM);
884
        ipc_answer_0(rid, ENOMEM);
884
        free(old);
885
        free(old);
885
        free(new);
886
        free(new);
886
        return;
887
        return;
887
    }
888
    }
888
    /* Lookup parent of the new file name. */
889
    /* Lookup parent of the new file name. */
889
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
890
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
890
    if (rc != EOK) {
891
    if (rc != EOK) {
891
        rwlock_write_unlock(&namespace_rwlock);
892
        rwlock_write_unlock(&namespace_rwlock);
892
        ipc_answer_0(rid, rc);
893
        ipc_answer_0(rid, rc);
893
        free(old);
894
        free(old);
894
        free(new);
895
        free(new);
895
        return;
896
        return;
896
    }
897
    }
897
    /* Check whether linking to the same file system instance. */
898
    /* Check whether linking to the same file system instance. */
898
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
899
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
899
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
900
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
900
        rwlock_write_unlock(&namespace_rwlock);
901
        rwlock_write_unlock(&namespace_rwlock);
901
        ipc_answer_0(rid, EXDEV);   /* different file systems */
902
        ipc_answer_0(rid, EXDEV);   /* different file systems */
902
        free(old);
903
        free(old);
903
        free(new);
904
        free(new);
904
        return;
905
        return;
905
    }
906
    }
906
    /* Destroy the old link for the new name. */
907
    /* Destroy the old link for the new name. */
907
    vfs_node_t *new_node = NULL;
908
    vfs_node_t *new_node = NULL;
908
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
909
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
909
    switch (rc) {
910
    switch (rc) {
910
    case ENOENT:
911
    case ENOENT:
911
        /* simply not in our way */
912
        /* simply not in our way */
912
        break;
913
        break;
913
    case EOK:
914
    case EOK:
914
        new_node = vfs_node_get(&new_lr);
915
        new_node = vfs_node_get(&new_lr);
915
        if (!new_node) {
916
        if (!new_node) {
916
            rwlock_write_unlock(&namespace_rwlock);
917
            rwlock_write_unlock(&namespace_rwlock);
917
            ipc_answer_0(rid, ENOMEM);
918
            ipc_answer_0(rid, ENOMEM);
918
            free(old);
919
            free(old);
919
            free(new);
920
            free(new);
920
            return;
921
            return;
921
        }
922
        }
922
        futex_down(&nodes_futex);
923
        futex_down(&nodes_futex);
923
        new_node->lnkcnt--;
924
        new_node->lnkcnt--;
924
        futex_up(&nodes_futex);
925
        futex_up(&nodes_futex);
925
        break;
926
        break;
926
    default:
927
    default:
927
        rwlock_write_unlock(&namespace_rwlock);
928
        rwlock_write_unlock(&namespace_rwlock);
928
        ipc_answer_0(rid, ENOTEMPTY);
929
        ipc_answer_0(rid, ENOTEMPTY);
929
        free(old);
930
        free(old);
930
        free(new);
931
        free(new);
931
        return;
932
        return;
932
    }
933
    }
933
    /* Create the new link for the new name. */
934
    /* Create the new link for the new name. */
934
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
935
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
935
    if (rc != EOK) {
936
    if (rc != EOK) {
936
        rwlock_write_unlock(&namespace_rwlock);
937
        rwlock_write_unlock(&namespace_rwlock);
937
        if (new_node)
938
        if (new_node)
938
            vfs_node_put(new_node);
939
            vfs_node_put(new_node);
939
        ipc_answer_0(rid, rc);
940
        ipc_answer_0(rid, rc);
940
        free(old);
941
        free(old);
941
        free(new);
942
        free(new);
942
        return;
943
        return;
943
    }
944
    }
944
    futex_down(&nodes_futex);
945
    futex_down(&nodes_futex);
945
    old_node->lnkcnt++;
946
    old_node->lnkcnt++;
946
    futex_up(&nodes_futex);
947
    futex_up(&nodes_futex);
947
    /* Destroy the link for the old name. */
948
    /* Destroy the link for the old name. */
948
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
949
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
949
    if (rc != EOK) {
950
    if (rc != EOK) {
950
        rwlock_write_unlock(&namespace_rwlock);
951
        rwlock_write_unlock(&namespace_rwlock);
951
        vfs_node_put(old_node);
952
        vfs_node_put(old_node);
952
        if (new_node)
953
        if (new_node)
953
            vfs_node_put(new_node);
954
            vfs_node_put(new_node);
954
        ipc_answer_0(rid, rc);
955
        ipc_answer_0(rid, rc);
955
        free(old);
956
        free(old);
956
        free(new);
957
        free(new);
957
        return;
958
        return;
958
    }
959
    }
959
    futex_down(&nodes_futex);
960
    futex_down(&nodes_futex);
960
    old_node->lnkcnt--;
961
    old_node->lnkcnt--;
961
    futex_up(&nodes_futex);
962
    futex_up(&nodes_futex);
962
    rwlock_write_unlock(&namespace_rwlock);
963
    rwlock_write_unlock(&namespace_rwlock);
963
    vfs_node_put(old_node);
964
    vfs_node_put(old_node);
964
    if (new_node)
965
    if (new_node)
965
        vfs_node_put(new_node);
966
        vfs_node_put(new_node);
966
    free(old);
967
    free(old);
967
    free(new);
968
    free(new);
968
    ipc_answer_0(rid, EOK);
969
    ipc_answer_0(rid, EOK);
969
}
970
}
970
 
971
 
971
/**
972
/**
972
 * @}
973
 * @}
973
 */
974
 */
974
 
975