Subversion Repositories HelenOS

Rev

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

Rev 3153 Rev 3674
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_lookup.c
34
 * @file    vfs_lookup.c
35
 * @brief
35
 * @brief
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 <string.h>
42
#include <string.h>
43
#include <stdarg.h>
43
#include <stdarg.h>
44
#include <bool.h>
44
#include <bool.h>
45
#include <futex.h>
45
#include <futex.h>
46
#include <libadt/list.h>
46
#include <libadt/list.h>
47
#include <vfs/canonify.h>
47
#include <vfs/canonify.h>
48
 
48
 
49
#define min(a, b)   ((a) < (b) ? (a) : (b))
49
#define min(a, b)   ((a) < (b) ? (a) : (b))
50
 
50
 
51
futex_t plb_futex = FUTEX_INITIALIZER;
51
futex_t plb_futex = FUTEX_INITIALIZER;
52
link_t plb_head;    /**< PLB entry ring buffer. */
52
link_t plb_head;    /**< PLB entry ring buffer. */
53
uint8_t *plb = NULL;
53
uint8_t *plb = NULL;
54
 
54
 
55
/** Perform a path lookup.
55
/** Perform a path lookup.
56
 *
56
 *
57
 * @param path      Path to be resolved; it must be a NULL-terminated
57
 * @param path      Path to be resolved; it must be a NULL-terminated
58
 *          string.
58
 *          string.
59
 * @param lflag     Flags to be used during lookup.
59
 * @param lflag     Flags to be used during lookup.
60
 * @param result    Empty structure where the lookup result will be stored.
60
 * @param result    Empty structure where the lookup result will be stored.
61
 *          Can be NULL.
61
 *          Can be NULL.
62
 * @param altroot   If non-empty, will be used instead of rootfs as the root
62
 * @param altroot   If non-empty, will be used instead of rootfs as the root
63
 *          of the whole VFS tree.
63
 *          of the whole VFS tree.
64
 *
64
 *
65
 * @return      EOK on success or an error code from errno.h.
65
 * @return      EOK on success or an error code from errno.h.
66
 */
66
 */
67
int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
67
int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
68
    vfs_pair_t *altroot, ...)
68
    vfs_pair_t *altroot, ...)
69
{
69
{
70
    vfs_pair_t *root;
70
    vfs_pair_t *root;
71
 
71
 
72
    if (altroot)
72
    if (altroot)
73
        root = altroot;
73
        root = altroot;
74
    else
74
    else
75
        root = &rootfs;
75
        root = &rootfs;
76
 
76
 
77
    if (!root->fs_handle)
77
    if (!root->fs_handle)
78
        return ENOENT;
78
        return ENOENT;
79
   
79
   
80
    size_t len;
80
    size_t len;
81
    path = canonify(path, &len);
81
    path = canonify(path, &len);
82
    if (!path)
82
    if (!path)
83
        return EINVAL;
83
        return EINVAL;
84
   
84
   
85
    fs_index_t index = 0;
85
    fs_index_t index = 0;
86
    if (lflag & L_LINK) {
86
    if (lflag & L_LINK) {
87
        va_list ap;
87
        va_list ap;
88
 
88
 
89
        va_start(ap, altroot);
89
        va_start(ap, altroot);
90
        index = va_arg(ap, fs_index_t);
90
        index = va_arg(ap, fs_index_t);
91
        va_end(ap);
91
        va_end(ap);
92
    }
92
    }
93
   
93
   
94
    futex_down(&plb_futex);
94
    futex_down(&plb_futex);
95
 
95
 
96
    plb_entry_t entry;
96
    plb_entry_t entry;
97
    link_initialize(&entry.plb_link);
97
    link_initialize(&entry.plb_link);
98
    entry.len = len;
98
    entry.len = len;
99
 
99
 
100
    off_t first;    /* the first free index */
100
    off_t first;    /* the first free index */
101
    off_t last; /* the last free index */
101
    off_t last; /* the last free index */
102
 
102
 
103
    if (list_empty(&plb_head)) {
103
    if (list_empty(&plb_head)) {
104
        first = 0;
104
        first = 0;
105
        last = PLB_SIZE - 1;
105
        last = PLB_SIZE - 1;
106
    } else {
106
    } else {
107
        plb_entry_t *oldest = list_get_instance(plb_head.next,
107
        plb_entry_t *oldest = list_get_instance(plb_head.next,
108
            plb_entry_t, plb_link);
108
            plb_entry_t, plb_link);
109
        plb_entry_t *newest = list_get_instance(plb_head.prev,
109
        plb_entry_t *newest = list_get_instance(plb_head.prev,
110
            plb_entry_t, plb_link);
110
            plb_entry_t, plb_link);
111
 
111
 
112
        first = (newest->index + newest->len) % PLB_SIZE;
112
        first = (newest->index + newest->len) % PLB_SIZE;
113
        last = (oldest->index - 1) % PLB_SIZE;
113
        last = (oldest->index - 1) % PLB_SIZE;
114
    }
114
    }
115
 
115
 
116
    if (first <= last) {
116
    if (first <= last) {
117
        if ((last - first) + 1 < len) {
117
        if ((last - first) + 1 < len) {
118
            /*
118
            /*
119
             * The buffer cannot absorb the path.
119
             * The buffer cannot absorb the path.
120
             */
120
             */
121
            futex_up(&plb_futex);
121
            futex_up(&plb_futex);
122
            return ELIMIT;
122
            return ELIMIT;
123
        }
123
        }
124
    } else {
124
    } else {
125
        if (PLB_SIZE - ((first - last) + 1) < len) {
125
        if (PLB_SIZE - ((first - last) + 1) < len) {
126
            /*
126
            /*
127
             * The buffer cannot absorb the path.
127
             * The buffer cannot absorb the path.
128
             */
128
             */
129
            futex_up(&plb_futex);
129
            futex_up(&plb_futex);
130
            return ELIMIT;
130
            return ELIMIT;
131
        }
131
        }
132
    }
132
    }
133
 
133
 
134
    /*
134
    /*
135
     * We know the first free index in PLB and we also know that there is
135
     * We know the first free index in PLB and we also know that there is
136
     * enough space in the buffer to hold our path.
136
     * enough space in the buffer to hold our path.
137
     */
137
     */
138
 
138
 
139
    entry.index = first;
139
    entry.index = first;
140
    entry.len = len;
140
    entry.len = len;
141
 
141
 
142
    /*
142
    /*
143
     * Claim PLB space by inserting the entry into the PLB entry ring
143
     * Claim PLB space by inserting the entry into the PLB entry ring
144
     * buffer.
144
     * buffer.
145
     */
145
     */
146
    list_append(&entry.plb_link, &plb_head);
146
    list_append(&entry.plb_link, &plb_head);
147
   
147
   
148
    futex_up(&plb_futex);
148
    futex_up(&plb_futex);
149
 
149
 
150
    /*
150
    /*
151
     * Copy the path into PLB.
151
     * Copy the path into PLB.
152
     */
152
     */
153
    size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
153
    size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
154
    size_t cnt2 = len - cnt1;
154
    size_t cnt2 = len - cnt1;
155
   
155
   
156
    memcpy(&plb[first], path, cnt1);
156
    memcpy(&plb[first], path, cnt1);
157
    memcpy(plb, &path[cnt1], cnt2);
157
    memcpy(plb, &path[cnt1], cnt2);
158
 
158
 
159
    ipc_call_t answer;
159
    ipc_call_t answer;
160
    int phone = vfs_grab_phone(root->fs_handle);
160
    int phone = vfs_grab_phone(root->fs_handle);
161
    aid_t req = async_send_5(phone, VFS_LOOKUP, (ipcarg_t) first,
161
    aid_t req = async_send_5(phone, VFS_LOOKUP, (ipcarg_t) first,
162
        (ipcarg_t) (first + len - 1) % PLB_SIZE,
162
        (ipcarg_t) (first + len - 1) % PLB_SIZE,
163
        (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, (ipcarg_t) index,
163
        (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, (ipcarg_t) index,
164
        &answer);
164
        &answer);
165
    vfs_release_phone(phone);
165
    vfs_release_phone(phone);
166
 
166
 
167
    ipcarg_t rc;
167
    ipcarg_t rc;
168
    async_wait_for(req, &rc);
168
    async_wait_for(req, &rc);
169
 
169
 
170
    futex_down(&plb_futex);
170
    futex_down(&plb_futex);
171
    list_remove(&entry.plb_link);
171
    list_remove(&entry.plb_link);
172
    /*
172
    /*
173
     * Erasing the path from PLB will come handy for debugging purposes.
173
     * Erasing the path from PLB will come handy for debugging purposes.
174
     */
174
     */
175
    memset(&plb[first], 0, cnt1);
175
    memset(&plb[first], 0, cnt1);
176
    memset(plb, 0, cnt2);
176
    memset(plb, 0, cnt2);
177
    futex_up(&plb_futex);
177
    futex_up(&plb_futex);
178
 
178
 
179
    if ((rc == EOK) && result) {
179
    if ((rc == EOK) && result) {
180
        result->triplet.fs_handle = (fs_handle_t) IPC_GET_ARG1(answer);
180
        result->triplet.fs_handle = (fs_handle_t) IPC_GET_ARG1(answer);
181
        result->triplet.dev_handle = (dev_handle_t) IPC_GET_ARG2(answer);
181
        result->triplet.dev_handle = (dev_handle_t) IPC_GET_ARG2(answer);
182
        result->triplet.index = (fs_index_t) IPC_GET_ARG3(answer);
182
        result->triplet.index = (fs_index_t) IPC_GET_ARG3(answer);
183
        result->size = (size_t) IPC_GET_ARG4(answer);
183
        result->size = (size_t) IPC_GET_ARG4(answer);
184
        result->lnkcnt = (unsigned) IPC_GET_ARG5(answer);
184
        result->lnkcnt = (unsigned) IPC_GET_ARG5(answer);
-
 
185
        if (lflag & L_FILE)
-
 
186
            result->type = VFS_NODE_FILE;
-
 
187
        else if (lflag & L_DIRECTORY)
-
 
188
            result->type = VFS_NODE_DIRECTORY;
-
 
189
        else
-
 
190
            result->type = VFS_NODE_UNKNOWN;
185
    }
191
    }
186
 
192
 
187
    return rc;
193
    return rc;
188
}
194
}
189
 
195
 
190
/**
196
/**
191
 * @}
197
 * @}
192
 */
198
 */
193
 
199