Subversion Repositories HelenOS

Rev

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

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