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