Subversion Repositories HelenOS

Rev

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

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