Subversion Repositories HelenOS

Rev

Rev 4389 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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