Subversion Repositories HelenOS

Rev

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

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