Rev 4528 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4528 | Rev 4555 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Jakub Jermar |
2 | * Copyright (c) 2007 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_file.c |
34 | * @file vfs_file.c |
35 | * @brief Various operations on files have their home in this file. |
35 | * @brief Various operations on files have their home in this file. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <errno.h> |
38 | #include <errno.h> |
39 | #include <stdlib.h> |
39 | #include <stdlib.h> |
40 | #include <string.h> |
40 | #include <string.h> |
41 | #include <assert.h> |
41 | #include <assert.h> |
42 | #include <bool.h> |
42 | #include <bool.h> |
43 | #include <fibril.h> |
43 | #include <fibril.h> |
44 | #include <fibril_sync.h> |
44 | #include <fibril_sync.h> |
45 | #include "vfs.h" |
45 | #include "vfs.h" |
46 | 46 | ||
47 | /** |
47 | /** |
48 | * This is a per-connection table of open files. |
48 | * This is a per-connection table of open files. |
49 | * Our assumption is that each client opens only one connection and therefore |
49 | * Our assumption is that each client opens only one connection and therefore |
50 | * there is one table of open files per task. However, this may not be the case |
50 | * there is one table of open files per task. However, this may not be the case |
51 | * and the client can open more connections to VFS. In that case, there will be |
51 | * and the client can open more connections to VFS. In that case, there will be |
52 | * several tables and several file handle name spaces per task. Besides of this, |
52 | * several tables and several file handle name spaces per task. Besides of this, |
53 | * the functionality will stay unchanged. So unless the client knows what it is |
53 | * the functionality will stay unchanged. So unless the client knows what it is |
54 | * doing, it should open one connection to VFS only. |
54 | * doing, it should open one connection to VFS only. |
55 | * |
55 | * |
56 | * Allocation of the open files table is deferred until the client makes the |
56 | * Allocation of the open files table is deferred until the client makes the |
57 | * first VFS_OPEN operation. |
57 | * first VFS_OPEN operation. |
58 | * |
58 | * |
59 | * This resource being per-connection and, in the first place, per-fibril, we |
59 | * This resource being per-connection and, in the first place, per-fibril, we |
60 | * don't need to protect it by a futex. |
60 | * don't need to protect it by a mutex. |
61 | */ |
61 | */ |
62 | fibril_local vfs_file_t **files = NULL; |
62 | fibril_local vfs_file_t **files = NULL; |
63 | 63 | ||
64 | /** Initialize the table of open files. */ |
64 | /** Initialize the table of open files. */ |
65 | bool vfs_files_init(void) |
65 | bool vfs_files_init(void) |
66 | { |
66 | { |
67 | if (!files) { |
67 | if (!files) { |
68 | files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
68 | files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
69 | if (!files) |
69 | if (!files) |
70 | return false; |
70 | return false; |
71 | memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
71 | memset(files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *)); |
72 | } |
72 | } |
73 | return true; |
73 | return true; |
74 | } |
74 | } |
75 | 75 | ||
76 | /** Allocate a file descriptor. |
76 | /** Allocate a file descriptor. |
77 | * |
77 | * |
78 | * @return First available file descriptor or a negative error |
78 | * @return First available file descriptor or a negative error |
79 | * code. |
79 | * code. |
80 | */ |
80 | */ |
81 | int vfs_fd_alloc(void) |
81 | int vfs_fd_alloc(void) |
82 | { |
82 | { |
83 | if (!vfs_files_init()) |
83 | if (!vfs_files_init()) |
84 | return ENOMEM; |
84 | return ENOMEM; |
85 | 85 | ||
86 | unsigned int i; |
86 | unsigned int i; |
87 | for (i = 0; i < MAX_OPEN_FILES; i++) { |
87 | for (i = 0; i < MAX_OPEN_FILES; i++) { |
88 | if (!files[i]) { |
88 | if (!files[i]) { |
89 | files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
89 | files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
90 | if (!files[i]) |
90 | if (!files[i]) |
91 | return ENOMEM; |
91 | return ENOMEM; |
92 | 92 | ||
93 | memset(files[i], 0, sizeof(vfs_file_t)); |
93 | memset(files[i], 0, sizeof(vfs_file_t)); |
94 | fibril_mutex_initialize(&files[i]->lock); |
94 | fibril_mutex_initialize(&files[i]->lock); |
95 | vfs_file_addref(files[i]); |
95 | vfs_file_addref(files[i]); |
96 | return (int) i; |
96 | return (int) i; |
97 | } |
97 | } |
98 | } |
98 | } |
99 | 99 | ||
100 | return EMFILE; |
100 | return EMFILE; |
101 | } |
101 | } |
102 | 102 | ||
103 | /** Release file descriptor. |
103 | /** Release file descriptor. |
104 | * |
104 | * |
105 | * @param fd File descriptor being released. |
105 | * @param fd File descriptor being released. |
106 | * |
106 | * |
107 | * @return EOK on success or EBADF if fd is an invalid file |
107 | * @return EOK on success or EBADF if fd is an invalid file |
108 | * descriptor. |
108 | * descriptor. |
109 | */ |
109 | */ |
110 | int vfs_fd_free(int fd) |
110 | int vfs_fd_free(int fd) |
111 | { |
111 | { |
112 | if (!vfs_files_init()) |
112 | if (!vfs_files_init()) |
113 | return ENOMEM; |
113 | return ENOMEM; |
114 | 114 | ||
115 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
115 | if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
116 | return EBADF; |
116 | return EBADF; |
117 | 117 | ||
118 | vfs_file_delref(files[fd]); |
118 | vfs_file_delref(files[fd]); |
119 | files[fd] = NULL; |
119 | files[fd] = NULL; |
120 | 120 | ||
121 | return EOK; |
121 | return EOK; |
122 | } |
122 | } |
123 | 123 | ||
124 | /** Increment reference count of VFS file structure. |
124 | /** Increment reference count of VFS file structure. |
125 | * |
125 | * |
126 | * @param file File structure that will have reference count |
126 | * @param file File structure that will have reference count |
127 | * incremented. |
127 | * incremented. |
128 | */ |
128 | */ |
129 | void vfs_file_addref(vfs_file_t *file) |
129 | void vfs_file_addref(vfs_file_t *file) |
130 | { |
130 | { |
131 | /* |
131 | /* |
132 | * File structures are per-connection, so no-one, except the current |
132 | * File structures are per-connection, so no-one, except the current |
133 | * fibril, should have a reference to them. This is the reason we don't |
133 | * fibril, should have a reference to them. This is the reason we don't |
134 | * do any synchronization here. |
134 | * do any synchronization here. |
135 | */ |
135 | */ |
136 | file->refcnt++; |
136 | file->refcnt++; |
137 | } |
137 | } |
138 | 138 | ||
139 | /** Decrement reference count of VFS file structure. |
139 | /** Decrement reference count of VFS file structure. |
140 | * |
140 | * |
141 | * @param file File structure that will have reference count |
141 | * @param file File structure that will have reference count |
142 | * decremented. |
142 | * decremented. |
143 | */ |
143 | */ |
144 | void vfs_file_delref(vfs_file_t *file) |
144 | void vfs_file_delref(vfs_file_t *file) |
145 | { |
145 | { |
146 | if (file->refcnt-- == 1) { |
146 | if (file->refcnt-- == 1) { |
147 | /* |
147 | /* |
148 | * Lost the last reference to a file, need to drop our reference |
148 | * Lost the last reference to a file, need to drop our reference |
149 | * to the underlying VFS node. |
149 | * to the underlying VFS node. |
150 | */ |
150 | */ |
151 | vfs_node_delref(file->node); |
151 | vfs_node_delref(file->node); |
152 | free(file); |
152 | free(file); |
153 | } |
153 | } |
154 | } |
154 | } |
155 | 155 | ||
156 | /** Find VFS file structure for a given file descriptor. |
156 | /** Find VFS file structure for a given file descriptor. |
157 | * |
157 | * |
158 | * @param fd File descriptor. |
158 | * @param fd File descriptor. |
159 | * |
159 | * |
160 | * @return VFS file structure corresponding to fd. |
160 | * @return VFS file structure corresponding to fd. |
161 | */ |
161 | */ |
162 | vfs_file_t *vfs_file_get(int fd) |
162 | vfs_file_t *vfs_file_get(int fd) |
163 | { |
163 | { |
164 | if (!vfs_files_init()) |
164 | if (!vfs_files_init()) |
165 | return NULL; |
165 | return NULL; |
166 | 166 | ||
167 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
167 | if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
168 | return files[fd]; |
168 | return files[fd]; |
169 | 169 | ||
170 | return NULL; |
170 | return NULL; |
171 | } |
171 | } |
172 | 172 | ||
173 | /** |
173 | /** |
174 | * @} |
174 | * @} |
175 | */ |
175 | */ |
176 | 176 |