Rev 2700 | Rev 2730 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2700 | Rev 2707 | ||
---|---|---|---|
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 <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <async.h> |
39 | #include <async.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <string.h> |
41 | #include <string.h> |
42 | #include <bool.h> |
42 | #include <bool.h> |
43 | #include <futex.h> |
43 | #include <futex.h> |
44 | #include <libadt/list.h> |
44 | #include <libadt/list.h> |
45 | #include <atomic.h> |
45 | #include <atomic.h> |
46 | #include "vfs.h" |
46 | #include "vfs.h" |
47 | 47 | ||
48 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
48 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
49 | 49 | ||
50 | atomic_t plb_futex = FUTEX_INITIALIZER; |
50 | atomic_t plb_futex = FUTEX_INITIALIZER; |
51 | link_t plb_head; /**< PLB entry ring buffer. */ |
51 | link_t plb_head; /**< PLB entry ring buffer. */ |
52 | uint8_t *plb = NULL; |
52 | uint8_t *plb = NULL; |
53 | 53 | ||
54 | /** Perform a path lookup. |
54 | /** Perform a path lookup. |
55 | * |
55 | * |
56 | * @param path Path to be resolved; it needn't be an ASCIIZ string. |
56 | * @param path Path to be resolved; it needn't be an ASCIIZ string. |
57 | * @param len Number of path characters pointed by path. |
57 | * @param len Number of path characters pointed by path. |
58 | * @param lflag Flags to be used during lookup. |
58 | * @param lflag Flags to be used during lookup. |
59 | * @param result Empty structure where the lookup result will be stored. |
59 | * @param result Empty structure where the lookup result will be stored. |
- | 60 | * Can be NULL. |
|
60 | * @param altroot If non-empty, will be used instead of rootfs as the root |
61 | * @param altroot If non-empty, will be used instead of rootfs as the root |
61 | * of the whole VFS tree. |
62 | * of the whole VFS tree. |
62 | * |
63 | * |
63 | * @return EOK on success or an error code from errno.h. |
64 | * @return EOK on success or an error code from errno.h. |
64 | */ |
65 | */ |
65 | int vfs_lookup_internal(char *path, size_t len, int lflag, |
66 | int vfs_lookup_internal(char *path, size_t len, int lflag, |
66 | vfs_lookup_res_t *result, vfs_pair_t *altroot) |
67 | vfs_lookup_res_t *result, vfs_pair_t *altroot) |
67 | { |
68 | { |
68 | vfs_pair_t *root; |
69 | vfs_pair_t *root; |
69 | 70 | ||
70 | if (!len) |
71 | if (!len) |
71 | return EINVAL; |
72 | return EINVAL; |
72 | 73 | ||
73 | if (altroot) |
74 | if (altroot) |
74 | root = altroot; |
75 | root = altroot; |
75 | else |
76 | else |
76 | root = (vfs_pair_t *) &rootfs; |
77 | root = (vfs_pair_t *) &rootfs; |
77 | 78 | ||
78 | if (!root->fs_handle) |
79 | if (!root->fs_handle) |
79 | return ENOENT; |
80 | return ENOENT; |
80 | 81 | ||
81 | futex_down(&plb_futex); |
82 | futex_down(&plb_futex); |
82 | 83 | ||
83 | plb_entry_t entry; |
84 | plb_entry_t entry; |
84 | link_initialize(&entry.plb_link); |
85 | link_initialize(&entry.plb_link); |
85 | entry.len = len; |
86 | entry.len = len; |
86 | 87 | ||
87 | off_t first; /* the first free index */ |
88 | off_t first; /* the first free index */ |
88 | off_t last; /* the last free index */ |
89 | off_t last; /* the last free index */ |
89 | 90 | ||
90 | if (list_empty(&plb_head)) { |
91 | if (list_empty(&plb_head)) { |
91 | first = 0; |
92 | first = 0; |
92 | last = PLB_SIZE - 1; |
93 | last = PLB_SIZE - 1; |
93 | } else { |
94 | } else { |
94 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
95 | plb_entry_t *oldest = list_get_instance(plb_head.next, |
95 | plb_entry_t, plb_link); |
96 | plb_entry_t, plb_link); |
96 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
97 | plb_entry_t *newest = list_get_instance(plb_head.prev, |
97 | plb_entry_t, plb_link); |
98 | plb_entry_t, plb_link); |
98 | 99 | ||
99 | first = (newest->index + newest->len) % PLB_SIZE; |
100 | first = (newest->index + newest->len) % PLB_SIZE; |
100 | last = (oldest->index - 1) % PLB_SIZE; |
101 | last = (oldest->index - 1) % PLB_SIZE; |
101 | } |
102 | } |
102 | 103 | ||
103 | if (first <= last) { |
104 | if (first <= last) { |
104 | if ((last - first) + 1 < len) { |
105 | if ((last - first) + 1 < len) { |
105 | /* |
106 | /* |
106 | * The buffer cannot absorb the path. |
107 | * The buffer cannot absorb the path. |
107 | */ |
108 | */ |
108 | futex_up(&plb_futex); |
109 | futex_up(&plb_futex); |
109 | return ELIMIT; |
110 | return ELIMIT; |
110 | } |
111 | } |
111 | } else { |
112 | } else { |
112 | if (PLB_SIZE - ((first - last) + 1) < len) { |
113 | if (PLB_SIZE - ((first - last) + 1) < len) { |
113 | /* |
114 | /* |
114 | * The buffer cannot absorb the path. |
115 | * The buffer cannot absorb the path. |
115 | */ |
116 | */ |
116 | futex_up(&plb_futex); |
117 | futex_up(&plb_futex); |
117 | return ELIMIT; |
118 | return ELIMIT; |
118 | } |
119 | } |
119 | } |
120 | } |
120 | 121 | ||
121 | /* |
122 | /* |
122 | * We know the first free index in PLB and we also know that there is |
123 | * We know the first free index in PLB and we also know that there is |
123 | * enough space in the buffer to hold our path. |
124 | * enough space in the buffer to hold our path. |
124 | */ |
125 | */ |
125 | 126 | ||
126 | entry.index = first; |
127 | entry.index = first; |
127 | entry.len = len; |
128 | entry.len = len; |
128 | 129 | ||
129 | /* |
130 | /* |
130 | * Claim PLB space by inserting the entry into the PLB entry ring |
131 | * Claim PLB space by inserting the entry into the PLB entry ring |
131 | * buffer. |
132 | * buffer. |
132 | */ |
133 | */ |
133 | list_append(&entry.plb_link, &plb_head); |
134 | list_append(&entry.plb_link, &plb_head); |
134 | 135 | ||
135 | futex_up(&plb_futex); |
136 | futex_up(&plb_futex); |
136 | 137 | ||
137 | /* |
138 | /* |
138 | * Copy the path into PLB. |
139 | * Copy the path into PLB. |
139 | */ |
140 | */ |
140 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
141 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1); |
141 | size_t cnt2 = len - cnt1; |
142 | size_t cnt2 = len - cnt1; |
142 | 143 | ||
143 | memcpy(&plb[first], path, cnt1); |
144 | memcpy(&plb[first], path, cnt1); |
144 | memcpy(plb, &path[cnt1], cnt2); |
145 | memcpy(plb, &path[cnt1], cnt2); |
145 | 146 | ||
146 | ipc_call_t answer; |
147 | ipc_call_t answer; |
147 | int phone = vfs_grab_phone(root->fs_handle); |
148 | int phone = vfs_grab_phone(root->fs_handle); |
148 | aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first, |
149 | aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first, |
149 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
150 | (ipcarg_t) (first + len - 1) % PLB_SIZE, |
150 | (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer); |
151 | (ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer); |
151 | vfs_release_phone(phone); |
152 | vfs_release_phone(phone); |
152 | 153 | ||
153 | ipcarg_t rc; |
154 | ipcarg_t rc; |
154 | async_wait_for(req, &rc); |
155 | async_wait_for(req, &rc); |
155 | 156 | ||
156 | futex_down(&plb_futex); |
157 | futex_down(&plb_futex); |
157 | list_remove(&entry.plb_link); |
158 | list_remove(&entry.plb_link); |
158 | /* |
159 | /* |
159 | * Erasing the path from PLB will come handy for debugging purposes. |
160 | * Erasing the path from PLB will come handy for debugging purposes. |
160 | */ |
161 | */ |
161 | memset(&plb[first], 0, cnt1); |
162 | memset(&plb[first], 0, cnt1); |
162 | memset(plb, 0, cnt2); |
163 | memset(plb, 0, cnt2); |
163 | futex_up(&plb_futex); |
164 | futex_up(&plb_futex); |
164 | 165 | ||
165 | if (rc == EOK) { |
166 | if ((rc == EOK) && result) { |
166 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer); |
167 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer); |
167 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer); |
168 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer); |
168 | result->triplet.index = (int) IPC_GET_ARG3(answer); |
169 | result->triplet.index = (int) IPC_GET_ARG3(answer); |
169 | result->size = (size_t) IPC_GET_ARG4(answer); |
170 | result->size = (size_t) IPC_GET_ARG4(answer); |
170 | } |
171 | } |
171 | 172 | ||
172 | return rc; |
173 | return rc; |
173 | } |
174 | } |
174 | 175 | ||
175 | /** |
176 | /** |
176 | * @} |
177 | * @} |
177 | */ |
178 | */ |
178 | 179 |