Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *               Gerardo Lamastra <gerardo@sssup.it>
6
 *
7
 * Authors     : Massimiliano Giorgi <massy@hartik.sssup.it>
8
 * (see authors.txt for full list of hartik's authors)
9
 *
10
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
11
 *
12
 * http://www.sssup.it
13
 * http://retis.sssup.it
14
 * http://hartik.sssup.it
15
 */
16
 
17
/*
18
 * Copyright (C) 1999 Massimiliano Giorgi
19
 *
20
 * This program is free software; you can redistribute it and/or modify
21
 * it under the terms of the GNU General Public License as published by
22
 * the Free Software Foundation; either version 2 of the License, or
23
 * (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
 *
34
 */
35
 
36
/*
37
 * CVS :        $Id: file.c,v 1.1.1.1 2002-03-29 14:12:50 pj Exp $
38
 *
39
 * File:        $File$
40
 * Revision:    $Revision: 1.1.1.1 $
41
 * Last update: $Date: 2002-03-29 14:12:50 $
42
 */
43
 
44
#include <fs/fsconf.h>
45
#include <fs/util.h>
46
#include <fs/types.h>
47
#include <fs/fcntl.h>
48
#include <fs/stat.h>
49
#include <fs/errno.h>
50
#include <fs/maccess.h>
51
#include <fs/limits.h>
52
#include <fs/task.h>
53
 
54
#include "mutex.h"
55
#include "fs.h"
56
#include "file.h"
57
#include "fsconst.h"
58
#include "dentry.h"
59
#include "fileop.h"
60
#include "inode.h"
61
#include "inodeop.h"
62
 
63
#include "debug.h"
64
 
65
/* OSKit stdio need this */
66
int getdtablesize(void)
67
{
68
  return MAXOPENFILES;
69
}
70
 
71
/*-----------*/
72
 
73
//#if !defined GLOB_DESC_TABLE
74
//#if !defined LOC_DESC_TABLE
75
//#error "GLOB_DESC_TABLE or LOC_DESC_TABLE must be defined! (see fsconfig.h)"
76
//#endif
77
//#else
78
//#if defined LOC_DESC_TABLE
79
//#error "GLOB_DESC_TABLE and LOC_DESC_TABLE are defined! (see fsconfig.h)"
80
//#endif
81
//#endif
82
 
83
/*-----------*/
84
 
85
//static __fs_mutex_t mutex;
86
static __fs_mutex_t mutexreq;
87
 
88
static struct file *freelist;
89
static struct file filetab[MAXSYSFILE];
90
 
91
//#ifdef GLOB_DESC_TABLE
92
//#include "fileg.inc"
93
//#else
94
//#include "filel.inc"
95
//#endif
96
 
97
struct file_descriptors desctable[MAXIMUMPROCESS];
98
 
99
int get_fd(__pid_t pid, struct file *f)
100
{
101
  int i;
102
  lock_desctable(pid);
103
  for (i=0;i<MAXOPENFILES;i++)
104
    if (desctable[pid].fd_file[i]==NULL) {
105
      desctable[pid].fd_file[i]=f;
106
      desctable[pid].fd_file[i]->f_count=1;
107
      desctable[pid].fd_flags[i]=0;
108
      desctable[pid].fd_fflags[i]=0;
109
      unlock_desctable(pid);
110
      return i;
111
    }
112
  unlock_desctable(pid);
113
  return -1;
114
}
115
 
116
int __duplicate_fd(__pid_t pid, int fd, int low)
117
{
118
  while (low<MAXOPENFILES) {
119
    if (desctable[pid].fd_file[low]==NULL) {
120
      desctable[pid].fd_file[low]=desctable[pid].fd_file[fd];
121
      desctable[pid].fd_file[fd]->f_count++;
122
      return low;
123
    }
124
    low++;
125
  }
126
  return -1;
127
}
128
 
129
 
130
int __isvalid_fd(__pid_t pid, int fd)
131
{
132
  return desctable[pid].fd_file[fd]!=NULL;
133
}
134
 
135
 
136
struct file *free_fd(__pid_t pid, int fd)
137
{
138
  struct file *ret;
139
  lock_desctable(pid);
140
  ret=desctable[pid].fd_file[fd];
141
  desctable[pid].fd_file[fd]=NULL;  
142
  unlock_desctable(pid);
143
  return ret;  
144
}
145
 
146
static int subfile_init(void)
147
{
148
  int i,j;
149
  for (j=0;j<MAXIMUMPROCESS;j++) {
150
 
151
    strcpy(desctable[j].fd_cwd,ROOTDIRNAME);
152
    desctable[j].fd_cwden=get_root_dentry();
153
    get_root_dentry()->d_lock++;
154
    desctable[j].fd_umask=__DEFUMASK;
155
 
156
    /* TO FIX : 0,1,2 are reserved file descriptors */
157
    /*
158
    desctable[j].fd_file[0]=(void*)(-1);
159
    desctable[j].fd_flags[0]=0;
160
    desctable[j].fd_fflags[0]=0;
161
    desctable[j].fd_file[1]=(void*)(-1);
162
    desctable[j].fd_flags[1]=0;
163
    desctable[j].fd_fflags[1]=0;
164
    desctable[j].fd_file[2]=(void*)(-1);
165
    desctable[j].fd_flags[2]=0;
166
    desctable[j].fd_fflags[2]=0;
167
    */
168
    for (i=0;i<MAXOPENFILES;i++) {
169
      desctable[j].fd_file[i]=NULL;
170
      desctable[j].fd_flags[i]=0;
171
      desctable[j].fd_fflags[i]=0;
172
    }
173
 
174
 
175
    init_mutex(j);
176
  }
177
 
178
  return 0;
179
}
180
 
181
/*-----------*/
182
 
183
/*
184
 * DANGER: must be called AFTER the mounting of the root device!
185
 * because the subfile_init() use get_root_dentry()
186
 */
187
 
188
int file_init(void)
189
{
190
  int i;
191
  //__fs_mutex_init(&mutex);
192
  __fs_mutex_init(&mutexreq);
193
  freelist=filetab;
194
  for (i=0;i<MAXSYSFILE;i++) {
195
    memset(filetab+i,0,sizeof(struct file));
196
    filetab[i].f_next=filetab+i+1;
197
    filetab[i].f_count=0;
198
  }
199
  filetab[MAXSYSFILE-1].f_next=NULL;
200
 
201
  return subfile_init();  
202
}
203
 
204
/**/
205
 
206
struct file *__get_file(void)
207
{
208
  struct file *f;
209
  if (freelist==NULL) return NULL;    
210
  f=freelist;
211
  freelist=f->f_next;
212
  _assert(f->f_count==0);
213
  f->f_count=1;
214
  return f;
215
}
216
 
217
/* get a file struct from the free queue */
218
struct file *get_file(struct inode *ptr)
219
{
220
  /* NB: ptr is not used yet */
221
  struct file *f;
222
  __fs_mutex_lock(&mutexreq);
223
  //cprintf("Û{");
224
  f=__get_file();
225
  __fs_mutex_unlock(&mutexreq);
226
  //cprintf("Û}");
227
  return f;
228
}
229
 
230
/**/
231
 
232
static void __free_file(struct file *f)
233
{
234
  _assert(f->f_count==1);
235
  f->f_count=0;
236
  f->f_next=freelist;
237
  freelist=f;
238
}
239
 
240
/* insert file struct into the free queue */
241
void free_file(struct file *f)
242
{
243
  __fs_mutex_lock(&mutexreq);
244
  //cprintf("Û{");
245
  __free_file(f);
246
  __fs_mutex_unlock(&mutexreq);
247
  //cprintf("Û}");
248
}
249
 
250
/* enumerate all files */
251
void enums_file(void (*func)(struct file *))
252
{
253
  int i;
254
  __fs_mutex_lock(&mutexreq);
255
  //cprintf("Û{");
256
  for (i=0;i<MAXSYSFILE;i++)
257
    if (filetab[i].f_count!=0) func(filetab+i);
258
  __fs_mutex_unlock(&mutexreq);
259
  //cprintf("Û}");
260
}
261
 
262
 
263
/*
264
 *
265
 * DUMMY file operations
266
 *
267
 */
268
 
269
__off_t dummy_lseek(struct file *f, __off_t off, int whence)
270
{
271
  return -ENOSYS;
272
}
273
 
274
__ssize_t dummy_read(struct file *f, char *p, __ssize_t d)
275
{
276
  return -ENOSYS;
277
}
278
 
279
__ssize_t dummy_write(struct file *f, char *p, __ssize_t d)
280
{
281
  return -ENOSYS;
282
}
283
 
284
int dummy_readdir(struct file *f, void *e)
285
{
286
  return -ENOSYS;
287
}
288
 
289
int dummy_open(struct inode *i, struct file *f)
290
{
291
  return -ENOSYS;
292
}
293
 
294
int dummy_close(struct inode *i, struct file *f)
295
{
296
  return -ENOSYS;
297
}
298
 
299
/*
300
 *
301
 * for function inlines
302
 *
303
 *
304
 */
305
 
306
#ifdef MULTITHREAD
307
 
308
void lock_desctable(__pid_t pid)
309
{
310
  __mutex_lock(&desctable[pid].fd_mutex);
311
}
312
 
313
void unlock_desctable(__pid_t pid)
314
{
315
  __mutex_unlock(&desctable[pid].fd_mutex);
316
}
317
 
318
void init_mutex(__pid_t pid)
319
{
320
  __mutex_init(&desctable[pid].fd_mutex,fsmutexattr);
321
}
322
 
323
#else
324
 
325
void lock_desctable(__pid_t pid)
326
{}
327
 
328
void unlock_desctable(__pid_t pid)
329
{}
330
 
331
void init_mutex(__pid_t pid)
332
{}
333
 
334
#endif
335
 
336
__mode_t *umask_ptr(__pid_t pid)
337
{
338
  return &desctable[pid].fd_umask;
339
}
340
 
341
char *cwd_ptr(__pid_t pid)
342
{
343
  return desctable[pid].fd_cwd;
344
}
345
 
346
struct dentry *cwden_ptr(__pid_t pid)
347
{
348
  return desctable[pid].fd_cwden;
349
}
350
 
351
struct file *file_ptr(__pid_t pid, int i)
352
{
353
  return desctable[pid].fd_file[i];
354
}
355
 
356
 
357
 
358
 
359
 
360