Subversion Repositories HelenOS

Rev

Rev 4509 | Rev 4539 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4509 Rev 4518
Line 43... Line 43...
43
#include <stdio.h>
43
#include <stdio.h>
44
#include <stdlib.h>
44
#include <stdlib.h>
45
#include <string.h>
45
#include <string.h>
46
#include <ctype.h>
46
#include <ctype.h>
47
#include <bool.h>
47
#include <bool.h>
48
#include <futex.h>
48
#include <fibril_sync.h>
49
#include <adt/list.h>
49
#include <adt/list.h>
50
#include <as.h>
50
#include <as.h>
51
#include <assert.h>
51
#include <assert.h>
52
#include <atomic.h>
52
#include <atomic.h>
53
#include "vfs.h"
53
#include "vfs.h"
54
 
54
 
55
atomic_t fs_head_futex = FUTEX_INITIALIZER;
55
FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
56
link_t fs_head;
56
link_t fs_head;
57
 
57
 
58
atomic_t fs_handle_next = {
58
atomic_t fs_handle_next = {
59
    .count = 1
59
    .count = 1
60
};
60
};
Line 157... Line 157...
157
        ipc_answer_0(callid, ENOMEM);
157
        ipc_answer_0(callid, ENOMEM);
158
        ipc_answer_0(rid, ENOMEM);
158
        ipc_answer_0(rid, ENOMEM);
159
        return;
159
        return;
160
    }
160
    }
161
    link_initialize(&fs_info->fs_link);
161
    link_initialize(&fs_info->fs_link);
162
    futex_initialize(&fs_info->phone_futex, 1);
162
    fibril_mutex_initialize(&fs_info->phone_lock);
163
       
163
       
164
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
164
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
165
    if (rc != EOK) {
165
    if (rc != EOK) {
166
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
166
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
167
            rc);
167
            rc);
Line 178... Line 178...
178
        ipc_answer_0(callid, EINVAL);
178
        ipc_answer_0(callid, EINVAL);
179
        ipc_answer_0(rid, EINVAL);
179
        ipc_answer_0(rid, EINVAL);
180
        return;
180
        return;
181
    }
181
    }
182
       
182
       
183
    futex_down(&fs_head_futex);
183
    fibril_mutex_lock(&fs_head_lock);
184
    fibril_inc_sercount();
-
 
185
 
184
 
186
    /*
185
    /*
187
     * Check for duplicit registrations.
186
     * Check for duplicit registrations.
188
     */
187
     */
189
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
188
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
190
        /*
189
        /*
191
         * We already register a fs like this.
190
         * We already register a fs like this.
192
         */
191
         */
193
        dprintf("FS is already registered.\n");
192
        dprintf("FS is already registered.\n");
194
        fibril_dec_sercount();
-
 
195
        futex_up(&fs_head_futex);
193
        fibril_mutex_unlock(&fs_head_lock);
196
        free(fs_info);
194
        free(fs_info);
197
        ipc_answer_0(callid, EEXISTS);
195
        ipc_answer_0(callid, EEXISTS);
198
        ipc_answer_0(rid, EEXISTS);
196
        ipc_answer_0(rid, EEXISTS);
199
        return;
197
        return;
200
    }
198
    }
Line 212... Line 210...
212
     */
210
     */
213
    callid = async_get_call(&call);
211
    callid = async_get_call(&call);
214
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
212
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
215
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
213
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
216
        list_remove(&fs_info->fs_link);
214
        list_remove(&fs_info->fs_link);
217
        fibril_dec_sercount();
-
 
218
        futex_up(&fs_head_futex);
215
        fibril_mutex_unlock(&fs_head_lock);
219
        free(fs_info);
216
        free(fs_info);
220
        ipc_answer_0(callid, EINVAL);
217
        ipc_answer_0(callid, EINVAL);
221
        ipc_answer_0(rid, EINVAL);
218
        ipc_answer_0(rid, EINVAL);
222
        return;
219
        return;
223
    }
220
    }
Line 231... Line 228...
231
     */
228
     */
232
 
229
 
233
    if (!ipc_share_in_receive(&callid, &size)) {
230
    if (!ipc_share_in_receive(&callid, &size)) {
234
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
231
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
235
        list_remove(&fs_info->fs_link);
232
        list_remove(&fs_info->fs_link);
236
        fibril_dec_sercount();
-
 
237
        futex_up(&fs_head_futex);
233
        fibril_mutex_unlock(&fs_head_lock);
238
        ipc_hangup(fs_info->phone);
234
        ipc_hangup(fs_info->phone);
239
        free(fs_info);
235
        free(fs_info);
240
        ipc_answer_0(callid, EINVAL);
236
        ipc_answer_0(callid, EINVAL);
241
        ipc_answer_0(rid, EINVAL);
237
        ipc_answer_0(rid, EINVAL);
242
        return;
238
        return;
Line 246... Line 242...
246
     * We can only send the client address space area PLB_SIZE bytes long.
242
     * We can only send the client address space area PLB_SIZE bytes long.
247
     */
243
     */
248
    if (size != PLB_SIZE) {
244
    if (size != PLB_SIZE) {
249
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
245
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
250
        list_remove(&fs_info->fs_link);
246
        list_remove(&fs_info->fs_link);
251
        fibril_dec_sercount();
-
 
252
        futex_up(&fs_head_futex);
247
        fibril_mutex_unlock(&fs_head_lock);
253
        ipc_hangup(fs_info->phone);
248
        ipc_hangup(fs_info->phone);
254
        free(fs_info);
249
        free(fs_info);
255
        ipc_answer_0(callid, EINVAL);
250
        ipc_answer_0(callid, EINVAL);
256
        ipc_answer_0(rid, EINVAL);
251
        ipc_answer_0(rid, EINVAL);
257
        return;
252
        return;
Line 271... Line 266...
271
     * system a global file system handle.
266
     * system a global file system handle.
272
     */
267
     */
273
    fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
268
    fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
274
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
269
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
275
   
270
   
276
    fibril_dec_sercount();
-
 
277
    futex_up(&fs_head_futex);
271
    fibril_mutex_unlock(&fs_head_lock);
278
   
272
   
279
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
273
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
280
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
274
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
281
}
275
}
282
 
276
 
Line 295... Line 289...
295
     * open any additional phones (even though that itself would be pretty
289
     * open any additional phones (even though that itself would be pretty
296
     * straightforward; housekeeping multiple open phones to a FS task would
290
     * straightforward; housekeeping multiple open phones to a FS task would
297
     * be more demanding). Instead, we simply take the respective
291
     * be more demanding). Instead, we simply take the respective
298
     * phone_futex and keep it until vfs_release_phone().
292
     * phone_futex and keep it until vfs_release_phone().
299
     */
293
     */
300
    futex_down(&fs_head_futex);
294
    fibril_mutex_lock(&fs_head_lock);
301
    link_t *cur;
295
    link_t *cur;
302
    fs_info_t *fs;
296
    fs_info_t *fs;
303
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
297
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
304
        fs = list_get_instance(cur, fs_info_t, fs_link);
298
        fs = list_get_instance(cur, fs_info_t, fs_link);
305
        if (fs->fs_handle == handle) {
299
        if (fs->fs_handle == handle) {
306
            futex_up(&fs_head_futex);
300
            fibril_mutex_unlock(&fs_head_lock);
307
            /*
-
 
308
             * For now, take the futex unconditionally.
-
 
309
             * Oh yeah, serialization rocks.
-
 
310
             * It will be up'ed in vfs_release_phone().
-
 
311
             */
-
 
312
            futex_down(&fs->phone_futex);
301
            fibril_mutex_lock(&fs->phone_lock);
313
            /*
-
 
314
             * Avoid deadlock with other fibrils in the same thread
-
 
315
             * by disabling fibril preemption.
-
 
316
             */
-
 
317
            fibril_inc_sercount();
-
 
318
            return fs->phone;
302
            return fs->phone;
319
        }
303
        }
320
    }
304
    }
321
    futex_up(&fs_head_futex);
305
    fibril_mutex_unlock(&fs_head_lock);
322
    return 0;
306
    return 0;
323
}
307
}
324
 
308
 
325
/** Tell VFS that the phone is in use for any request.
309
/** Tell VFS that the phone is in use for any request.
326
 *
310
 *
Line 328... Line 312...
328
 */
312
 */
329
void vfs_release_phone(int phone)
313
void vfs_release_phone(int phone)
330
{
314
{
331
    bool found = false;
315
    bool found = false;
332
 
316
 
333
    /*
-
 
334
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
-
 
335
     */
-
 
336
    fibril_dec_sercount();
-
 
337
   
-
 
338
    futex_down(&fs_head_futex);
317
    fibril_mutex_lock(&fs_head_lock);
339
    link_t *cur;
318
    link_t *cur;
340
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
319
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
341
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
320
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
342
        if (fs->phone == phone) {
321
        if (fs->phone == phone) {
343
            found = true;
322
            found = true;
344
            futex_up(&fs_head_futex);
323
            fibril_mutex_unlock(&fs_head_lock);
345
            futex_up(&fs->phone_futex);
324
            fibril_mutex_unlock(&fs->phone_lock);
346
            return;
325
            return;
347
        }
326
        }
348
    }
327
    }
349
    futex_up(&fs_head_futex);
328
    fibril_mutex_unlock(&fs_head_lock);
350
 
329
 
351
    /*
330
    /*
352
     * Not good to get here.
331
     * Not good to get here.
353
     */
332
     */
354
    assert(found == true);
333
    assert(found == true);
355
}
334
}
356
 
335
 
357
/** Convert file system name to its handle.
336
/** Convert file system name to its handle.
358
 *
337
 *
359
 * @param name      File system name.
338
 * @param name      File system name.
360
 * @param lock      If true, the function will down and up the
339
 * @param lock      If true, the function will lock and unlock the
361
 *          fs_head_futex.
340
 *          fs_head_lock.
362
 *
341
 *
363
 * @return      File system handle or zero if file system not found.
342
 * @return      File system handle or zero if file system not found.
364
 */
343
 */
365
fs_handle_t fs_name_to_handle(char *name, bool lock)
344
fs_handle_t fs_name_to_handle(char *name, bool lock)
366
{
345
{
367
    int handle = 0;
346
    int handle = 0;
368
   
347
   
369
    if (lock)
348
    if (lock)
370
        futex_down(&fs_head_futex);
349
        fibril_mutex_lock(&fs_head_lock);
371
    link_t *cur;
350
    link_t *cur;
372
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
351
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
373
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
352
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
374
        if (str_cmp(fs->vfs_info.name, name) == 0) {
353
        if (str_cmp(fs->vfs_info.name, name) == 0) {
375
            handle = fs->fs_handle;
354
            handle = fs->fs_handle;
376
            break;
355
            break;
377
        }
356
        }
378
    }
357
    }
379
    if (lock)
358
    if (lock)
380
        futex_up(&fs_head_futex);
359
        fibril_mutex_unlock(&fs_head_lock);
381
    return handle;
360
    return handle;
382
}
361
}
383
 
362
 
384
/**
363
/**
385
 * @}
364
 * @}