Subversion Repositories HelenOS

Rev

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

Rev 4327 Rev 4581
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_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_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_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 < 0 || 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_OPEN call; the path
470
     * will need to arrive in another call.
441
     * 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
   
480
    /*
451
    /*
481
     * Make sure that we are called with exactly one of L_FILE and
452
     * Make sure that we are called with exactly one of L_FILE and
482
     * L_DIRECTORY.
453
     * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
483
     */
454
     */
484
    if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
455
    if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
485
        (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
456
        ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
-
 
457
        ((lflag & L_OPEN) != 0)) {
486
        ipc_answer_0(rid, EINVAL);
458
        ipc_answer_0(rid, EINVAL);
487
        return;
459
        return;
488
    }
460
    }
489
 
461
   
490
    if (oflag & O_CREAT)
462
    if (oflag & O_CREAT)
491
        lflag |= L_CREATE;
463
        lflag |= L_CREATE;
492
    if (oflag & O_EXCL)
464
    if (oflag & O_EXCL)
493
        lflag |= L_EXCLUSIVE;
465
        lflag |= L_EXCLUSIVE;
494
 
466
   
495
    ipc_callid_t callid;
467
    ipc_callid_t callid;
496
 
-
 
497
    if (!ipc_data_write_receive(&callid, &len)) {
468
    if (!ipc_data_write_receive(&callid, &len)) {
498
        ipc_answer_0(callid, EINVAL);
469
        ipc_answer_0(callid, EINVAL);
499
        ipc_answer_0(rid, EINVAL);
470
        ipc_answer_0(rid, EINVAL);
500
        return;
471
        return;
501
    }
472
    }
-
 
473
   
502
    char *path = malloc(len + 1);
474
    char *path = malloc(len + 1);
503
    if (!path) {
475
    if (!path) {
504
        ipc_answer_0(callid, ENOMEM);
476
        ipc_answer_0(callid, ENOMEM);
505
        ipc_answer_0(rid, ENOMEM);
477
        ipc_answer_0(rid, ENOMEM);
506
        return;
478
        return;
507
    }
479
    }
-
 
480
   
508
    int rc;
481
    int rc;
509
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
482
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
510
        ipc_answer_0(rid, rc);
483
        ipc_answer_0(rid, rc);
511
        free(path);
484
        free(path);
512
        return;
485
        return;
513
    }
486
    }
514
    path[len] = '\0';
487
    path[len] = '\0';
515
   
488
   
516
    /*
489
    /*
517
     * Avoid the race condition in which the file can be deleted before we
490
     * 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
491
     * find/create-and-lock the VFS node corresponding to the looked-up
519
     * triplet.
492
     * triplet.
520
     */
493
     */
521
    if (lflag & L_CREATE)
494
    if (lflag & L_CREATE)
522
        rwlock_write_lock(&namespace_rwlock);
495
        fibril_rwlock_write_lock(&namespace_rwlock);
523
    else
496
    else
524
        rwlock_read_lock(&namespace_rwlock);
497
        fibril_rwlock_read_lock(&namespace_rwlock);
525
 
498
   
526
    /* The path is now populated and we can call vfs_lookup_internal(). */
499
    /* The path is now populated and we can call vfs_lookup_internal(). */
527
    vfs_lookup_res_t lr;
500
    vfs_lookup_res_t lr;
528
    rc = vfs_lookup_internal(path, lflag, &lr, NULL);
501
    rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
529
    if (rc) {
502
    if (rc != EOK) {
530
        if (lflag & L_CREATE)
503
        if (lflag & L_CREATE)
531
            rwlock_write_unlock(&namespace_rwlock);
504
            fibril_rwlock_write_unlock(&namespace_rwlock);
532
        else
505
        else
533
            rwlock_read_unlock(&namespace_rwlock);
506
            fibril_rwlock_read_unlock(&namespace_rwlock);
534
        ipc_answer_0(rid, rc);
507
        ipc_answer_0(rid, rc);
535
        free(path);
508
        free(path);
536
        return;
509
        return;
537
    }
510
    }
538
 
511
   
539
    /* Path is no longer needed. */
512
    /* Path is no longer needed. */
540
    free(path);
513
    free(path);
541
 
514
   
542
    vfs_node_t *node = vfs_node_get(&lr);
515
    vfs_node_t *node = vfs_node_get(&lr);
543
    if (lflag & L_CREATE)
516
    if (lflag & L_CREATE)
544
        rwlock_write_unlock(&namespace_rwlock);
517
        fibril_rwlock_write_unlock(&namespace_rwlock);
545
    else
518
    else
546
        rwlock_read_unlock(&namespace_rwlock);
519
        fibril_rwlock_read_unlock(&namespace_rwlock);
547
 
520
   
548
    /* Truncate the file if requested and if necessary. */
521
    /* Truncate the file if requested and if necessary. */
549
    if (oflag & O_TRUNC) {
522
    if (oflag & O_TRUNC) {
550
        rwlock_write_lock(&node->contents_rwlock);
523
        fibril_rwlock_write_lock(&node->contents_rwlock);
551
        if (node->size) {
524
        if (node->size) {
552
            rc = vfs_truncate_internal(node->fs_handle,
525
            rc = vfs_truncate_internal(node->fs_handle,
553
                node->dev_handle, node->index, 0);
526
                node->dev_handle, node->index, 0);
554
            if (rc) {
527
            if (rc) {
555
                rwlock_write_unlock(&node->contents_rwlock);
528
                fibril_rwlock_write_unlock(&node->contents_rwlock);
556
                vfs_node_put(node);
529
                vfs_node_put(node);
557
                ipc_answer_0(rid, rc);
530
                ipc_answer_0(rid, rc);
558
                return;
531
                return;
559
            }
532
            }
560
            node->size = 0;
533
            node->size = 0;
561
        }
534
        }
562
        rwlock_write_unlock(&node->contents_rwlock);
535
        fibril_rwlock_write_unlock(&node->contents_rwlock);
563
    }
536
    }
564
 
537
   
565
    /*
538
    /*
566
     * Get ourselves a file descriptor and the corresponding vfs_file_t
539
     * Get ourselves a file descriptor and the corresponding vfs_file_t
567
     * structure.
540
     * structure.
568
     */
541
     */
569
    int fd = vfs_fd_alloc();
542
    int fd = vfs_fd_alloc();
570
    if (fd < 0) {
543
    if (fd < 0) {
571
        vfs_node_put(node);
544
        vfs_node_put(node);
572
        ipc_answer_0(rid, fd);
545
        ipc_answer_0(rid, fd);
573
        return;
546
        return;
574
    }
547
    }
575
    vfs_file_t *file = vfs_file_get(fd);
548
    vfs_file_t *file = vfs_file_get(fd);
576
    file->node = node;
549
    file->node = node;
577
    if (oflag & O_APPEND)
550
    if (oflag & O_APPEND)
578
        file->append = true;
551
        file->append = true;
579
 
552
   
580
    /*
553
    /*
581
     * The following increase in reference count is for the fact that the
554
     * 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.
555
     * 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
556
     * It is necessary so that the file will not disappear when
584
     * vfs_node_put() is called. The reference will be dropped by the
557
     * vfs_node_put() is called. The reference will be dropped by the
585
     * respective VFS_CLOSE.
558
     * respective VFS_CLOSE.
586
     */
559
     */
587
    vfs_node_addref(node);
560
    vfs_node_addref(node);
588
    vfs_node_put(node);
561
    vfs_node_put(node);
-
 
562
   
-
 
563
    /* Success! Return the new file descriptor to the client. */
-
 
564
    ipc_answer_1(rid, EOK, fd);
-
 
565
}
589
 
566
 
-
 
567
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
-
 
568
{
-
 
569
    // FIXME: check for sanity of the supplied fs, dev and index
-
 
570
   
-
 
571
    if (!vfs_files_init()) {
-
 
572
        ipc_answer_0(rid, ENOMEM);
-
 
573
        return;
-
 
574
    }
-
 
575
   
-
 
576
    /*
-
 
577
     * The interface is open_node(fs, dev, index, oflag).
-
 
578
     */
-
 
579
    vfs_lookup_res_t lr;
-
 
580
   
-
 
581
    lr.triplet.fs_handle = IPC_GET_ARG1(*request);
-
 
582
    lr.triplet.dev_handle = IPC_GET_ARG2(*request);
-
 
583
    lr.triplet.index = IPC_GET_ARG3(*request);
-
 
584
    int oflag = IPC_GET_ARG4(*request);
-
 
585
   
-
 
586
    fibril_rwlock_read_lock(&namespace_rwlock);
-
 
587
   
-
 
588
    int rc = vfs_open_node_internal(&lr);
-
 
589
    if (rc != EOK) {
-
 
590
        fibril_rwlock_read_unlock(&namespace_rwlock);
-
 
591
        ipc_answer_0(rid, rc);
-
 
592
        return;
-
 
593
    }
-
 
594
   
-
 
595
    vfs_node_t *node = vfs_node_get(&lr);
-
 
596
    fibril_rwlock_read_unlock(&namespace_rwlock);
-
 
597
   
-
 
598
    /* Truncate the file if requested and if necessary. */
-
 
599
    if (oflag & O_TRUNC) {
-
 
600
        fibril_rwlock_write_lock(&node->contents_rwlock);
-
 
601
        if (node->size) {
-
 
602
            rc = vfs_truncate_internal(node->fs_handle,
-
 
603
                node->dev_handle, node->index, 0);
-
 
604
            if (rc) {
-
 
605
                fibril_rwlock_write_unlock(&node->contents_rwlock);
-
 
606
                vfs_node_put(node);
-
 
607
                ipc_answer_0(rid, rc);
-
 
608
                return;
-
 
609
            }
-
 
610
            node->size = 0;
-
 
611
        }
-
 
612
        fibril_rwlock_write_unlock(&node->contents_rwlock);
-
 
613
    }
-
 
614
   
-
 
615
    /*
-
 
616
     * Get ourselves a file descriptor and the corresponding vfs_file_t
-
 
617
     * structure.
-
 
618
     */
-
 
619
    int fd = vfs_fd_alloc();
-
 
620
    if (fd < 0) {
-
 
621
        vfs_node_put(node);
-
 
622
        ipc_answer_0(rid, fd);
-
 
623
        return;
-
 
624
    }
-
 
625
    vfs_file_t *file = vfs_file_get(fd);
-
 
626
    file->node = node;
-
 
627
    if (oflag & O_APPEND)
-
 
628
        file->append = true;
-
 
629
   
-
 
630
    /*
-
 
631
     * The following increase in reference count is for the fact that the
-
 
632
     * file is being opened and that a file structure is pointing to it.
-
 
633
     * It is necessary so that the file will not disappear when
-
 
634
     * vfs_node_put() is called. The reference will be dropped by the
-
 
635
     * respective VFS_CLOSE.
-
 
636
     */
-
 
637
    vfs_node_addref(node);
-
 
638
    vfs_node_put(node);
-
 
639
   
590
    /* Success! Return the new file descriptor to the client. */
640
    /* Success! Return the new file descriptor to the client. */
591
    ipc_answer_1(rid, EOK, fd);
641
    ipc_answer_1(rid, EOK, fd);
592
}
642
}
593
 
643
 
594
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
644
void vfs_node(ipc_callid_t rid, ipc_call_t *request)
595
{
645
{
596
    int fd = IPC_GET_ARG1(*request);
646
    int fd = IPC_GET_ARG1(*request);
-
 
647
   
-
 
648
    /* Lookup the file structure corresponding to the file descriptor. */
-
 
649
    vfs_file_t *file = vfs_file_get(fd);
-
 
650
    if (!file) {
-
 
651
        ipc_answer_0(rid, ENOENT);
-
 
652
        return;
-
 
653
    }
-
 
654
   
-
 
655
    ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle,
-
 
656
        file->node->index);
-
 
657
}
-
 
658
 
-
 
659
void vfs_device(ipc_callid_t rid, ipc_call_t *request)
-
 
660
{
-
 
661
    int fd = IPC_GET_ARG1(*request);
-
 
662
   
-
 
663
    /* Lookup the file structure corresponding to the file descriptor. */
-
 
664
    vfs_file_t *file = vfs_file_get(fd);
-
 
665
    if (!file) {
-
 
666
        ipc_answer_0(rid, ENOENT);
-
 
667
        return;
-
 
668
    }
-
 
669
   
-
 
670
    /*
-
 
671
     * Lock the open file structure so that no other thread can manipulate
-
 
672
     * the same open file at a time.
-
 
673
     */
-
 
674
    fibril_mutex_lock(&file->lock);
-
 
675
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
676
   
-
 
677
    /* Make a VFS_DEVICE request at the destination FS server. */
-
 
678
    aid_t msg;
-
 
679
    ipc_call_t answer;
-
 
680
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
681
        file->node->dev_handle, file->node->index, &answer);
-
 
682
 
-
 
683
    /* Wait for reply from the FS server. */
-
 
684
    ipcarg_t rc;
-
 
685
    async_wait_for(msg, &rc);
-
 
686
 
-
 
687
    vfs_release_phone(fs_phone);
-
 
688
    fibril_mutex_unlock(&file->lock);
-
 
689
   
-
 
690
    ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
-
 
691
}
-
 
692
 
-
 
693
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
-
 
694
{
-
 
695
    int fd = IPC_GET_ARG1(*request);
-
 
696
   
-
 
697
    /* Lookup the file structure corresponding to the file descriptor. */
597
    int rc = vfs_fd_free(fd);
698
    vfs_file_t *file = vfs_file_get(fd);
-
 
699
    if (!file) {
-
 
700
        ipc_answer_0(rid, ENOENT);
-
 
701
        return;
-
 
702
    }
-
 
703
   
-
 
704
    /*
-
 
705
     * Lock the open file structure so that no other thread can manipulate
-
 
706
     * the same open file at a time.
-
 
707
     */
-
 
708
    fibril_mutex_lock(&file->lock);
-
 
709
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
710
   
-
 
711
    /* Make a VFS_SYMC request at the destination FS server. */
-
 
712
    aid_t msg;
-
 
713
    ipc_call_t answer;
-
 
714
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
715
        file->node->dev_handle, file->node->index, &answer);
-
 
716
 
-
 
717
    /* Wait for reply from the FS server. */
-
 
718
    ipcarg_t rc;
-
 
719
    async_wait_for(msg, &rc);
-
 
720
   
-
 
721
    vfs_release_phone(fs_phone);
-
 
722
    fibril_mutex_unlock(&file->lock);
-
 
723
   
598
    ipc_answer_0(rid, rc);
724
    ipc_answer_0(rid, rc);
599
}
725
}
600
 
726
 
-
 
727
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
-
 
728
{
-
 
729
    int fd = IPC_GET_ARG1(*request);
-
 
730
   
-
 
731
    /* Lookup the file structure corresponding to the file descriptor. */
-
 
732
    vfs_file_t *file = vfs_file_get(fd);
-
 
733
    if (!file) {
-
 
734
        ipc_answer_0(rid, ENOENT);
-
 
735
        return;
-
 
736
    }
-
 
737
   
-
 
738
    /*
-
 
739
     * Lock the open file structure so that no other thread can manipulate
-
 
740
     * the same open file at a time.
-
 
741
     */
-
 
742
    fibril_mutex_lock(&file->lock);
-
 
743
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
-
 
744
   
-
 
745
    /* Make a VFS_CLOSE request at the destination FS server. */
-
 
746
    aid_t msg;
-
 
747
    ipc_call_t answer;
-
 
748
    msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
-
 
749
        file->node->dev_handle, file->node->index, &answer);
-
 
750
 
-
 
751
    /* Wait for reply from the FS server. */
-
 
752
    ipcarg_t rc;
-
 
753
    async_wait_for(msg, &rc);
-
 
754
 
-
 
755
    vfs_release_phone(fs_phone);
-
 
756
    fibril_mutex_unlock(&file->lock);
-
 
757
   
-
 
758
    int retval = IPC_GET_ARG1(answer);
-
 
759
    if (retval != EOK)
-
 
760
        ipc_answer_0(rid, retval);
-
 
761
   
-
 
762
    retval = vfs_fd_free(fd);
-
 
763
    ipc_answer_0(rid, retval);
-
 
764
}
-
 
765
 
601
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
766
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
602
{
767
{
603
 
768
 
604
    /*
769
    /*
605
     * The following code strongly depends on the fact that the files data
770
     * 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
771
     * structure can be only accessed by a single fibril and all file
607
     * operations are serialized (i.e. the reads and writes cannot
772
     * operations are serialized (i.e. the reads and writes cannot
608
     * interleave and a file cannot be closed while it is being read).
773
     * interleave and a file cannot be closed while it is being read).
609
     *
774
     *
610
     * Additional synchronization needs to be added once the table of
775
     * Additional synchronization needs to be added once the table of
611
     * open files supports parallel access!
776
     * open files supports parallel access!
612
     */
777
     */
613
 
778
 
614
    int fd = IPC_GET_ARG1(*request);
779
    int fd = IPC_GET_ARG1(*request);
615
   
780
   
616
    /* Lookup the file structure corresponding to the file descriptor. */
781
    /* Lookup the file structure corresponding to the file descriptor. */
617
    vfs_file_t *file = vfs_file_get(fd);
782
    vfs_file_t *file = vfs_file_get(fd);
618
    if (!file) {
783
    if (!file) {
619
        ipc_answer_0(rid, ENOENT);
784
        ipc_answer_0(rid, ENOENT);
620
        return;
785
        return;
621
    }
786
    }
622
   
787
   
623
    /*
788
    /*
624
     * Now we need to receive a call with client's
789
     * Now we need to receive a call with client's
625
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
790
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
626
     */
791
     */
627
    ipc_callid_t callid;
792
    ipc_callid_t callid;
628
    int res;
793
    int res;
629
    if (read)
794
    if (read)
630
        res = ipc_data_read_receive(&callid, NULL);
795
        res = ipc_data_read_receive(&callid, NULL);
631
    else
796
    else
632
        res = ipc_data_write_receive(&callid, NULL);
797
        res = ipc_data_write_receive(&callid, NULL);
633
    if (!res) {
798
    if (!res) {
634
        ipc_answer_0(callid, EINVAL);
799
        ipc_answer_0(callid, EINVAL);
635
        ipc_answer_0(rid, EINVAL);
800
        ipc_answer_0(rid, EINVAL);
636
        return;
801
        return;
637
    }
802
    }
638
   
803
   
639
    /*
804
    /*
640
     * Lock the open file structure so that no other thread can manipulate
805
     * Lock the open file structure so that no other thread can manipulate
641
     * the same open file at a time.
806
     * the same open file at a time.
642
     */
807
     */
643
    futex_down(&file->lock);
808
    fibril_mutex_lock(&file->lock);
644
 
809
 
645
    /*
810
    /*
646
     * Lock the file's node so that no other client can read/write to it at
811
     * Lock the file's node so that no other client can read/write to it at
647
     * the same time.
812
     * the same time.
648
     */
813
     */
649
    if (read)
814
    if (read)
650
        rwlock_read_lock(&file->node->contents_rwlock);
815
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
651
    else
816
    else
652
        rwlock_write_lock(&file->node->contents_rwlock);
817
        fibril_rwlock_write_lock(&file->node->contents_rwlock);
653
 
818
 
654
    if (file->node->type == VFS_NODE_DIRECTORY) {
819
    if (file->node->type == VFS_NODE_DIRECTORY) {
655
        /*
820
        /*
656
         * Make sure that no one is modifying the namespace
821
         * Make sure that no one is modifying the namespace
657
         * while we are in readdir().
822
         * while we are in readdir().
658
         */
823
         */
659
        assert(read);
824
        assert(read);
660
        rwlock_read_lock(&namespace_rwlock);
825
        fibril_rwlock_read_lock(&namespace_rwlock);
661
    }
826
    }
662
   
827
   
663
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
828
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
664
   
829
   
665
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
830
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
666
    aid_t msg;
831
    aid_t msg;
667
    ipc_call_t answer;
832
    ipc_call_t answer;
668
    if (!read && file->append)
833
    if (!read && file->append)
669
        file->pos = file->node->size;
834
        file->pos = file->node->size;
670
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
835
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
671
        file->node->dev_handle, file->node->index, file->pos, &answer);
836
        file->node->dev_handle, file->node->index, file->pos, &answer);
672
   
837
   
673
    /*
838
    /*
674
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
839
     * 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
840
     * 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
841
     * ourselves. Note that call arguments are immutable in this case so we
677
     * don't have to bother.
842
     * don't have to bother.
678
     */
843
     */
679
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
844
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
680
   
-
 
681
    vfs_release_phone(fs_phone);
-
 
682
   
845
 
683
    /* Wait for reply from the FS server. */
846
    /* Wait for reply from the FS server. */
684
    ipcarg_t rc;
847
    ipcarg_t rc;
685
    async_wait_for(msg, &rc);
848
    async_wait_for(msg, &rc);
-
 
849
   
-
 
850
    vfs_release_phone(fs_phone);
-
 
851
   
686
    size_t bytes = IPC_GET_ARG1(answer);
852
    size_t bytes = IPC_GET_ARG1(answer);
687
 
853
 
688
    if (file->node->type == VFS_NODE_DIRECTORY)
854
    if (file->node->type == VFS_NODE_DIRECTORY)
689
        rwlock_read_unlock(&namespace_rwlock);
855
        fibril_rwlock_read_unlock(&namespace_rwlock);
690
   
856
   
691
    /* Unlock the VFS node. */
857
    /* Unlock the VFS node. */
692
    if (read)
858
    if (read)
693
        rwlock_read_unlock(&file->node->contents_rwlock);
859
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
694
    else {
860
    else {
695
        /* Update the cached version of node's size. */
861
        /* Update the cached version of node's size. */
696
        if (rc == EOK)
862
        if (rc == EOK)
697
            file->node->size = IPC_GET_ARG2(answer);
863
            file->node->size = IPC_GET_ARG2(answer);
698
        rwlock_write_unlock(&file->node->contents_rwlock);
864
        fibril_rwlock_write_unlock(&file->node->contents_rwlock);
699
    }
865
    }
700
   
866
   
701
    /* Update the position pointer and unlock the open file. */
867
    /* Update the position pointer and unlock the open file. */
702
    if (rc == EOK)
868
    if (rc == EOK)
703
        file->pos += bytes;
869
        file->pos += bytes;
704
    futex_up(&file->lock);
870
    fibril_mutex_unlock(&file->lock);
705
   
871
   
706
    /*
872
    /*
707
     * FS server's reply is the final result of the whole operation we
873
     * FS server's reply is the final result of the whole operation we
708
     * return to the client.
874
     * return to the client.
709
     */
875
     */
710
    ipc_answer_1(rid, rc, bytes);
876
    ipc_answer_1(rid, rc, bytes);
711
}
877
}
712
 
878
 
713
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
879
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
714
{
880
{
715
    vfs_rdwr(rid, request, true);
881
    vfs_rdwr(rid, request, true);
716
}
882
}
717
 
883
 
718
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
884
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
719
{
885
{
720
    vfs_rdwr(rid, request, false);
886
    vfs_rdwr(rid, request, false);
721
}
887
}
722
 
888
 
723
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
889
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
724
{
890
{
725
    int fd = (int) IPC_GET_ARG1(*request);
891
    int fd = (int) IPC_GET_ARG1(*request);
726
    off_t off = (off_t) IPC_GET_ARG2(*request);
892
    off_t off = (off_t) IPC_GET_ARG2(*request);
727
    int whence = (int) IPC_GET_ARG3(*request);
893
    int whence = (int) IPC_GET_ARG3(*request);
728
 
894
 
729
 
895
 
730
    /* Lookup the file structure corresponding to the file descriptor. */
896
    /* Lookup the file structure corresponding to the file descriptor. */
731
    vfs_file_t *file = vfs_file_get(fd);
897
    vfs_file_t *file = vfs_file_get(fd);
732
    if (!file) {
898
    if (!file) {
733
        ipc_answer_0(rid, ENOENT);
899
        ipc_answer_0(rid, ENOENT);
734
        return;
900
        return;
735
    }
901
    }
736
 
902
 
737
    off_t newpos;
903
    off_t newpos;
738
    futex_down(&file->lock);
904
    fibril_mutex_lock(&file->lock);
739
    if (whence == SEEK_SET) {
905
    if (whence == SEEK_SET) {
740
        file->pos = off;
906
        file->pos = off;
741
        futex_up(&file->lock);
907
        fibril_mutex_unlock(&file->lock);
742
        ipc_answer_1(rid, EOK, off);
908
        ipc_answer_1(rid, EOK, off);
743
        return;
909
        return;
744
    }
910
    }
745
    if (whence == SEEK_CUR) {
911
    if (whence == SEEK_CUR) {
746
        if (file->pos + off < file->pos) {
912
        if (file->pos + off < file->pos) {
747
            futex_up(&file->lock);
913
            fibril_mutex_unlock(&file->lock);
748
            ipc_answer_0(rid, EOVERFLOW);
914
            ipc_answer_0(rid, EOVERFLOW);
749
            return;
915
            return;
750
        }
916
        }
751
        file->pos += off;
917
        file->pos += off;
752
        newpos = file->pos;
918
        newpos = file->pos;
753
        futex_up(&file->lock);
919
        fibril_mutex_unlock(&file->lock);
754
        ipc_answer_1(rid, EOK, newpos);
920
        ipc_answer_1(rid, EOK, newpos);
755
        return;
921
        return;
756
    }
922
    }
757
    if (whence == SEEK_END) {
923
    if (whence == SEEK_END) {
758
        rwlock_read_lock(&file->node->contents_rwlock);
924
        fibril_rwlock_read_lock(&file->node->contents_rwlock);
759
        size_t size = file->node->size;
925
        size_t size = file->node->size;
760
        rwlock_read_unlock(&file->node->contents_rwlock);
926
        fibril_rwlock_read_unlock(&file->node->contents_rwlock);
761
        if (size + off < size) {
927
        if (size + off < size) {
762
            futex_up(&file->lock);
928
            fibril_mutex_unlock(&file->lock);
763
            ipc_answer_0(rid, EOVERFLOW);
929
            ipc_answer_0(rid, EOVERFLOW);
764
            return;
930
            return;
765
        }
931
        }
766
        newpos = size + off;
932
        newpos = size + off;
767
        futex_up(&file->lock);
933
        fibril_mutex_unlock(&file->lock);
768
        ipc_answer_1(rid, EOK, newpos);
934
        ipc_answer_1(rid, EOK, newpos);
769
        return;
935
        return;
770
    }
936
    }
771
    futex_up(&file->lock);
937
    fibril_mutex_unlock(&file->lock);
772
    ipc_answer_0(rid, EINVAL);
938
    ipc_answer_0(rid, EINVAL);
773
}
939
}
774
 
940
 
775
int
941
int
776
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
942
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
777
    fs_index_t index, size_t size)
943
    fs_index_t index, size_t size)
778
{
944
{
779
    ipcarg_t rc;
945
    ipcarg_t rc;
780
    int fs_phone;
946
    int fs_phone;
781
   
947
   
782
    fs_phone = vfs_grab_phone(fs_handle);
948
    fs_phone = vfs_grab_phone(fs_handle);
783
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
949
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
784
        (ipcarg_t)index, (ipcarg_t)size);
950
        (ipcarg_t)index, (ipcarg_t)size);
785
    vfs_release_phone(fs_phone);
951
    vfs_release_phone(fs_phone);
786
    return (int)rc;
952
    return (int)rc;
787
}
953
}
788
 
954
 
789
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
955
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
790
{
956
{
791
    int fd = IPC_GET_ARG1(*request);
957
    int fd = IPC_GET_ARG1(*request);
792
    size_t size = IPC_GET_ARG2(*request);
958
    size_t size = IPC_GET_ARG2(*request);
793
    int rc;
959
    int rc;
794
 
960
 
795
    vfs_file_t *file = vfs_file_get(fd);
961
    vfs_file_t *file = vfs_file_get(fd);
796
    if (!file) {
962
    if (!file) {
797
        ipc_answer_0(rid, ENOENT);
963
        ipc_answer_0(rid, ENOENT);
798
        return;
964
        return;
799
    }
965
    }
800
    futex_down(&file->lock);
966
    fibril_mutex_lock(&file->lock);
801
 
967
 
802
    rwlock_write_lock(&file->node->contents_rwlock);
968
    fibril_rwlock_write_lock(&file->node->contents_rwlock);
803
    rc = vfs_truncate_internal(file->node->fs_handle,
969
    rc = vfs_truncate_internal(file->node->fs_handle,
804
        file->node->dev_handle, file->node->index, size);
970
        file->node->dev_handle, file->node->index, size);
805
    if (rc == EOK)
971
    if (rc == EOK)
806
        file->node->size = size;
972
        file->node->size = size;
807
    rwlock_write_unlock(&file->node->contents_rwlock);
973
    fibril_rwlock_write_unlock(&file->node->contents_rwlock);
808
 
974
 
809
    futex_up(&file->lock);
975
    fibril_mutex_unlock(&file->lock);
810
    ipc_answer_0(rid, (ipcarg_t)rc);
976
    ipc_answer_0(rid, (ipcarg_t)rc);
811
}
977
}
812
 
978
 
813
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
979
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
814
{
980
{
815
    int mode = IPC_GET_ARG1(*request);
981
    int mode = IPC_GET_ARG1(*request);
816
 
982
 
817
    size_t len;
983
    size_t len;
818
    ipc_callid_t callid;
984
    ipc_callid_t callid;
819
 
985
 
820
    if (!ipc_data_write_receive(&callid, &len)) {
986
    if (!ipc_data_write_receive(&callid, &len)) {
821
        ipc_answer_0(callid, EINVAL);
987
        ipc_answer_0(callid, EINVAL);
822
        ipc_answer_0(rid, EINVAL);
988
        ipc_answer_0(rid, EINVAL);
823
        return;
989
        return;
824
    }
990
    }
825
    char *path = malloc(len + 1);
991
    char *path = malloc(len + 1);
826
    if (!path) {
992
    if (!path) {
827
        ipc_answer_0(callid, ENOMEM);
993
        ipc_answer_0(callid, ENOMEM);
828
        ipc_answer_0(rid, ENOMEM);
994
        ipc_answer_0(rid, ENOMEM);
829
        return;
995
        return;
830
    }
996
    }
831
    int rc;
997
    int rc;
832
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
998
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
833
        ipc_answer_0(rid, rc);
999
        ipc_answer_0(rid, rc);
834
        free(path);
1000
        free(path);
835
        return;
1001
        return;
836
    }
1002
    }
837
    path[len] = '\0';
1003
    path[len] = '\0';
838
   
1004
   
839
    rwlock_write_lock(&namespace_rwlock);
1005
    fibril_rwlock_write_lock(&namespace_rwlock);
840
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
1006
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
841
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
1007
    rc = vfs_lookup_internal(path, lflag, NULL, NULL);
842
    rwlock_write_unlock(&namespace_rwlock);
1008
    fibril_rwlock_write_unlock(&namespace_rwlock);
843
    free(path);
1009
    free(path);
844
    ipc_answer_0(rid, rc);
1010
    ipc_answer_0(rid, rc);
845
}
1011
}
846
 
1012
 
847
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1013
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
848
{
1014
{
849
    int lflag = IPC_GET_ARG1(*request);
1015
    int lflag = IPC_GET_ARG1(*request);
850
 
1016
 
851
    size_t len;
1017
    size_t len;
852
    ipc_callid_t callid;
1018
    ipc_callid_t callid;
853
 
1019
 
854
    if (!ipc_data_write_receive(&callid, &len)) {
1020
    if (!ipc_data_write_receive(&callid, &len)) {
855
        ipc_answer_0(callid, EINVAL);
1021
        ipc_answer_0(callid, EINVAL);
856
        ipc_answer_0(rid, EINVAL);
1022
        ipc_answer_0(rid, EINVAL);
857
        return;
1023
        return;
858
    }
1024
    }
859
    char *path = malloc(len + 1);
1025
    char *path = malloc(len + 1);
860
    if (!path) {
1026
    if (!path) {
861
        ipc_answer_0(callid, ENOMEM);
1027
        ipc_answer_0(callid, ENOMEM);
862
        ipc_answer_0(rid, ENOMEM);
1028
        ipc_answer_0(rid, ENOMEM);
863
        return;
1029
        return;
864
    }
1030
    }
865
    int rc;
1031
    int rc;
866
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
1032
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
867
        ipc_answer_0(rid, rc);
1033
        ipc_answer_0(rid, rc);
868
        free(path);
1034
        free(path);
869
        return;
1035
        return;
870
    }
1036
    }
871
    path[len] = '\0';
1037
    path[len] = '\0';
872
   
1038
   
873
    rwlock_write_lock(&namespace_rwlock);
1039
    fibril_rwlock_write_lock(&namespace_rwlock);
874
    lflag &= L_DIRECTORY;   /* sanitize lflag */
1040
    lflag &= L_DIRECTORY;   /* sanitize lflag */
875
    vfs_lookup_res_t lr;
1041
    vfs_lookup_res_t lr;
876
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
1042
    rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
877
    free(path);
1043
    free(path);
878
    if (rc != EOK) {
1044
    if (rc != EOK) {
879
        rwlock_write_unlock(&namespace_rwlock);
1045
        fibril_rwlock_write_unlock(&namespace_rwlock);
880
        ipc_answer_0(rid, rc);
1046
        ipc_answer_0(rid, rc);
881
        return;
1047
        return;
882
    }
1048
    }
883
 
1049
 
884
    /*
1050
    /*
885
     * The name has already been unlinked by vfs_lookup_internal().
1051
     * 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
1052
     * 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.
1053
     * VFS_DESTROY'ed after the last reference to it is dropped.
888
     */
1054
     */
889
    vfs_node_t *node = vfs_node_get(&lr);
1055
    vfs_node_t *node = vfs_node_get(&lr);
890
    futex_down(&nodes_futex);
1056
    fibril_mutex_lock(&nodes_mutex);
891
    node->lnkcnt--;
1057
    node->lnkcnt--;
892
    futex_up(&nodes_futex);
1058
    fibril_mutex_unlock(&nodes_mutex);
893
    rwlock_write_unlock(&namespace_rwlock);
1059
    fibril_rwlock_write_unlock(&namespace_rwlock);
894
    vfs_node_put(node);
1060
    vfs_node_put(node);
895
    ipc_answer_0(rid, EOK);
1061
    ipc_answer_0(rid, EOK);
896
}
1062
}
897
 
1063
 
898
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1064
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
899
{
1065
{
900
    size_t olen, nlen;
1066
    size_t olen, nlen;
901
    ipc_callid_t callid;
1067
    ipc_callid_t callid;
902
    int rc;
1068
    int rc;
903
 
1069
 
904
    /* Retrieve the old path. */
1070
    /* Retrieve the old path. */
905
    if (!ipc_data_write_receive(&callid, &olen)) {
1071
    if (!ipc_data_write_receive(&callid, &olen)) {
906
        ipc_answer_0(callid, EINVAL);
1072
        ipc_answer_0(callid, EINVAL);
907
        ipc_answer_0(rid, EINVAL);
1073
        ipc_answer_0(rid, EINVAL);
908
        return;
1074
        return;
909
    }
1075
    }
910
    char *old = malloc(olen + 1);
1076
    char *old = malloc(olen + 1);
911
    if (!old) {
1077
    if (!old) {
912
        ipc_answer_0(callid, ENOMEM);
1078
        ipc_answer_0(callid, ENOMEM);
913
        ipc_answer_0(rid, ENOMEM);
1079
        ipc_answer_0(rid, ENOMEM);
914
        return;
1080
        return;
915
    }
1081
    }
916
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
1082
    if ((rc = ipc_data_write_finalize(callid, old, olen))) {
917
        ipc_answer_0(rid, rc);
1083
        ipc_answer_0(rid, rc);
918
        free(old);
1084
        free(old);
919
        return;
1085
        return;
920
    }
1086
    }
921
    old[olen] = '\0';
1087
    old[olen] = '\0';
922
   
1088
   
923
    /* Retrieve the new path. */
1089
    /* Retrieve the new path. */
924
    if (!ipc_data_write_receive(&callid, &nlen)) {
1090
    if (!ipc_data_write_receive(&callid, &nlen)) {
925
        ipc_answer_0(callid, EINVAL);
1091
        ipc_answer_0(callid, EINVAL);
926
        ipc_answer_0(rid, EINVAL);
1092
        ipc_answer_0(rid, EINVAL);
927
        free(old);
1093
        free(old);
928
        return;
1094
        return;
929
    }
1095
    }
930
    char *new = malloc(nlen + 1);
1096
    char *new = malloc(nlen + 1);
931
    if (!new) {
1097
    if (!new) {
932
        ipc_answer_0(callid, ENOMEM);
1098
        ipc_answer_0(callid, ENOMEM);
933
        ipc_answer_0(rid, ENOMEM);
1099
        ipc_answer_0(rid, ENOMEM);
934
        free(old);
1100
        free(old);
935
        return;
1101
        return;
936
    }
1102
    }
937
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
1103
    if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
938
        ipc_answer_0(rid, rc);
1104
        ipc_answer_0(rid, rc);
939
        free(old);
1105
        free(old);
940
        free(new);
1106
        free(new);
941
        return;
1107
        return;
942
    }
1108
    }
943
    new[nlen] = '\0';
1109
    new[nlen] = '\0';
944
 
1110
 
945
    char *oldc = canonify(old, &olen);
1111
    char *oldc = canonify(old, &olen);
946
    char *newc = canonify(new, &nlen);
1112
    char *newc = canonify(new, &nlen);
947
    if (!oldc || !newc) {
1113
    if (!oldc || !newc) {
948
        ipc_answer_0(rid, EINVAL);
1114
        ipc_answer_0(rid, EINVAL);
949
        free(old);
1115
        free(old);
950
        free(new);
1116
        free(new);
951
        return;
1117
        return;
952
    }
1118
    }
953
    oldc[olen] = '\0';
1119
    oldc[olen] = '\0';
954
    newc[nlen] = '\0';
1120
    newc[nlen] = '\0';
955
    if (!str_lcmp(newc, oldc, str_length(oldc))) {
1121
    if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
-
 
1122
        ((newc[str_length(oldc)] == '/') ||
-
 
1123
        (str_length(oldc) == 1) ||
-
 
1124
        (str_length(oldc) == str_length(newc)))) {
-
 
1125
            /*
956
        /* oldc is a prefix of newc */
1126
         * oldc is a prefix of newc and either
-
 
1127
         * - newc continues with a / where oldc ends, or
-
 
1128
         * - oldc was / itself, or
-
 
1129
         * - oldc and newc are equal.
-
 
1130
         */
957
        ipc_answer_0(rid, EINVAL);
1131
        ipc_answer_0(rid, EINVAL);
958
        free(old);
1132
        free(old);
959
        free(new);
1133
        free(new);
960
        return;
1134
        return;
961
    }
1135
    }
962
   
1136
   
963
    vfs_lookup_res_t old_lr;
1137
    vfs_lookup_res_t old_lr;
964
    vfs_lookup_res_t new_lr;
1138
    vfs_lookup_res_t new_lr;
965
    vfs_lookup_res_t new_par_lr;
1139
    vfs_lookup_res_t new_par_lr;
966
    rwlock_write_lock(&namespace_rwlock);
1140
    fibril_rwlock_write_lock(&namespace_rwlock);
967
    /* Lookup the node belonging to the old file name. */
1141
    /* Lookup the node belonging to the old file name. */
968
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1142
    rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
969
    if (rc != EOK) {
1143
    if (rc != EOK) {
970
        rwlock_write_unlock(&namespace_rwlock);
1144
        fibril_rwlock_write_unlock(&namespace_rwlock);
971
        ipc_answer_0(rid, rc);
1145
        ipc_answer_0(rid, rc);
972
        free(old);
1146
        free(old);
973
        free(new);
1147
        free(new);
974
        return;
1148
        return;
975
    }
1149
    }
976
    vfs_node_t *old_node = vfs_node_get(&old_lr);
1150
    vfs_node_t *old_node = vfs_node_get(&old_lr);
977
    if (!old_node) {
1151
    if (!old_node) {
978
        rwlock_write_unlock(&namespace_rwlock);
1152
        fibril_rwlock_write_unlock(&namespace_rwlock);
979
        ipc_answer_0(rid, ENOMEM);
1153
        ipc_answer_0(rid, ENOMEM);
980
        free(old);
1154
        free(old);
981
        free(new);
1155
        free(new);
982
        return;
1156
        return;
983
    }
1157
    }
-
 
1158
    /* Determine the path to the parent of the node with the new name. */
-
 
1159
    char *parentc = str_dup(newc);
-
 
1160
    if (!parentc) {
-
 
1161
        fibril_rwlock_write_unlock(&namespace_rwlock);
-
 
1162
        ipc_answer_0(rid, rc);
-
 
1163
        free(old);
-
 
1164
        free(new);
-
 
1165
        return;
-
 
1166
    }
-
 
1167
    char *lastsl = str_rchr(parentc + 1, '/');
-
 
1168
    if (lastsl)
-
 
1169
        *lastsl = '\0';
-
 
1170
    else
-
 
1171
        parentc[1] = '\0';
984
    /* Lookup parent of the new file name. */
1172
    /* Lookup parent of the new file name. */
985
    rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
1173
    rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
-
 
1174
    free(parentc);  /* not needed anymore */
986
    if (rc != EOK) {
1175
    if (rc != EOK) {
987
        rwlock_write_unlock(&namespace_rwlock);
1176
        fibril_rwlock_write_unlock(&namespace_rwlock);
988
        ipc_answer_0(rid, rc);
1177
        ipc_answer_0(rid, rc);
989
        free(old);
1178
        free(old);
990
        free(new);
1179
        free(new);
991
        return;
1180
        return;
992
    }
1181
    }
993
    /* Check whether linking to the same file system instance. */
1182
    /* Check whether linking to the same file system instance. */
994
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1183
    if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
995
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
1184
        (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
996
        rwlock_write_unlock(&namespace_rwlock);
1185
        fibril_rwlock_write_unlock(&namespace_rwlock);
997
        ipc_answer_0(rid, EXDEV);   /* different file systems */
1186
        ipc_answer_0(rid, EXDEV);   /* different file systems */
998
        free(old);
1187
        free(old);
999
        free(new);
1188
        free(new);
1000
        return;
1189
        return;
1001
    }
1190
    }
1002
    /* Destroy the old link for the new name. */
1191
    /* Destroy the old link for the new name. */
1003
    vfs_node_t *new_node = NULL;
1192
    vfs_node_t *new_node = NULL;
1004
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1193
    rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1005
    switch (rc) {
1194
    switch (rc) {
1006
    case ENOENT:
1195
    case ENOENT:
1007
        /* simply not in our way */
1196
        /* simply not in our way */
1008
        break;
1197
        break;
1009
    case EOK:
1198
    case EOK:
1010
        new_node = vfs_node_get(&new_lr);
1199
        new_node = vfs_node_get(&new_lr);
1011
        if (!new_node) {
1200
        if (!new_node) {
1012
            rwlock_write_unlock(&namespace_rwlock);
1201
            fibril_rwlock_write_unlock(&namespace_rwlock);
1013
            ipc_answer_0(rid, ENOMEM);
1202
            ipc_answer_0(rid, ENOMEM);
1014
            free(old);
1203
            free(old);
1015
            free(new);
1204
            free(new);
1016
            return;
1205
            return;
1017
        }
1206
        }
1018
        futex_down(&nodes_futex);
1207
        fibril_mutex_lock(&nodes_mutex);
1019
        new_node->lnkcnt--;
1208
        new_node->lnkcnt--;
1020
        futex_up(&nodes_futex);
1209
        fibril_mutex_unlock(&nodes_mutex);
1021
        break;
1210
        break;
1022
    default:
1211
    default:
1023
        rwlock_write_unlock(&namespace_rwlock);
1212
        fibril_rwlock_write_unlock(&namespace_rwlock);
1024
        ipc_answer_0(rid, ENOTEMPTY);
1213
        ipc_answer_0(rid, ENOTEMPTY);
1025
        free(old);
1214
        free(old);
1026
        free(new);
1215
        free(new);
1027
        return;
1216
        return;
1028
    }
1217
    }
1029
    /* Create the new link for the new name. */
1218
    /* Create the new link for the new name. */
1030
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1219
    rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1031
    if (rc != EOK) {
1220
    if (rc != EOK) {
1032
        rwlock_write_unlock(&namespace_rwlock);
1221
        fibril_rwlock_write_unlock(&namespace_rwlock);
1033
        if (new_node)
1222
        if (new_node)
1034
            vfs_node_put(new_node);
1223
            vfs_node_put(new_node);
1035
        ipc_answer_0(rid, rc);
1224
        ipc_answer_0(rid, rc);
1036
        free(old);
1225
        free(old);
1037
        free(new);
1226
        free(new);
1038
        return;
1227
        return;
1039
    }
1228
    }
1040
    futex_down(&nodes_futex);
1229
    fibril_mutex_lock(&nodes_mutex);
1041
    old_node->lnkcnt++;
1230
    old_node->lnkcnt++;
1042
    futex_up(&nodes_futex);
1231
    fibril_mutex_unlock(&nodes_mutex);
1043
    /* Destroy the link for the old name. */
1232
    /* Destroy the link for the old name. */
1044
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1233
    rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1045
    if (rc != EOK) {
1234
    if (rc != EOK) {
1046
        rwlock_write_unlock(&namespace_rwlock);
1235
        fibril_rwlock_write_unlock(&namespace_rwlock);
1047
        vfs_node_put(old_node);
1236
        vfs_node_put(old_node);
1048
        if (new_node)
1237
        if (new_node)
1049
            vfs_node_put(new_node);
1238
            vfs_node_put(new_node);
1050
        ipc_answer_0(rid, rc);
1239
        ipc_answer_0(rid, rc);
1051
        free(old);
1240
        free(old);
1052
        free(new);
1241
        free(new);
1053
        return;
1242
        return;
1054
    }
1243
    }
1055
    futex_down(&nodes_futex);
1244
    fibril_mutex_lock(&nodes_mutex);
1056
    old_node->lnkcnt--;
1245
    old_node->lnkcnt--;
1057
    futex_up(&nodes_futex);
1246
    fibril_mutex_unlock(&nodes_mutex);
1058
    rwlock_write_unlock(&namespace_rwlock);
1247
    fibril_rwlock_write_unlock(&namespace_rwlock);
1059
    vfs_node_put(old_node);
1248
    vfs_node_put(old_node);
1060
    if (new_node)
1249
    if (new_node)
1061
        vfs_node_put(new_node);
1250
        vfs_node_put(new_node);
1062
    free(old);
1251
    free(old);
1063
    free(new);
1252
    free(new);
1064
    ipc_answer_0(rid, EOK);
1253
    ipc_answer_0(rid, EOK);
1065
}
1254
}
1066
 
1255
 
1067
/**
1256
/**
1068
 * @}
1257
 * @}
1069
 */
1258
 */
1070
 
1259