Subversion Repositories HelenOS

Rev

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

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