Subversion Repositories shark

Rev

Rev 684 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
 
1063 tullio 2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program; if not, write to the Free Software
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 *
17
 */
18
 
2 pj 19
#include <fs/types.h>
20
#include <fs/errno.h>
21
#include <fs/task.h>
22
#include <fs/maccess.h>
23
#include <fs/fcntl.h>
24
 
25
#include "file.h"
26
#include "inode.h"
27
#include "dentry.h"
28
#include "fileop.h"
29
#include "rwlock.h"
30
#include "fs.h"
31
 
32
#define EBOH EIO
33
 
34
/* temporary: for console i/o */
35
#include <ll/i386/cons.h>
36
static int __getch(int flag)
37
{
684 giacomo 38
 
39
        return 0;
40
 
2 pj 41
}
42
 
43
__ssize_t k_read(int fd, __ptr_t buf, __ssize_t nbytes)
44
{
45
  __pid_t pid;
46
  __ssize_t sz;
47
 
48
  call_to_fs();
49
 
50
  /* TO FIX! */  
51
  if (fd>=0&&fd<=2) {    
52
    __ssize_t x;
53
    int ret,ch;
54
    char *c;    
55
 
56
    //printk(KERN_INFO "reading from console");
57
 
58
    /* WARNIG buf is a pointer into user-space
59
     * we are using it without cast
60
     */
61
 
62
    /* WARNIG there is a blocking call...
63
     * the system can go down if it is blocked!
64
     */
65
 
66
    if (fd!=0) return_from_fs(-EBADF);
67
 
68
    c=(char*)buf;
69
    x=0;    
70
    while (x<nbytes) {
71
      ch=__getch(NON_BLOCK);
72
      if (ch!=-1) *c++=ch;
73
      else {
74
        if (x>0) break;
75
        release_sys_call();
76
        ch=__getch(BLOCK);
77
        ret=reacquire_sys_call();
78
        if (ret) return -ENOSYS;
79
        *c++=ch;
80
      }
81
      x++;
82
      cputc(ch);
83
    }
84
 
85
    return_from_fs(x);
86
  }
87
 
88
  /* don't try to use bad file descriptor */
89
  pid=__get_pid();
90
  if (check_fd(pid,fd)) {
91
    printk(KERN_INFO "k_read: using bad file descriptor");
92
    return_from_fs(-EBADF);
93
  }
94
 
95
  /* don't try to read a directory */
96
  if (file_ptr(pid,fd)->f_flag_isdir) return_from_fs(-EBADF);
97
 
98
  /* don't try to read into not user memory */
99
  if (!__verify_write(buf,nbytes)) return_from_fs(-EVERIFY);
100
 
101
  /* if in write only mode */
102
  if (*fildesfflags_ptr(pid,fd)&O_WRONLY)
103
    return_from_fs(-EBOH);
104
 
105
  /* cancellation point */
106
  //fs_test_cancellation(-EINTR);
107
 
108
  /* acquire read lock on inode's file */
109
  __rwlock_rdlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
110
 
111
  /* O_APPEND flags */
112
  if (*fildesfflags_ptr(pid,fd)&O_APPEND) {
113
    sz=0;
114
    goto SKIP;
115
  }
116
 
117
  /* WARNING buf is a pointer into user-space! */
118
  sz=file_ptr(pid,fd)->f_op->read(file_ptr(pid,fd),buf,nbytes);
119
 
120
  /* release the lock */
121
SKIP:
122
  __rwlock_rdunlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
123
 
124
  return_from_fs(sz);
125
}