Subversion Repositories shark

Rev

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

Rev Author Line No. Line
2 pj 1
 
2
#include <fs/types.h>
3
#include <fs/errno.h>
4
#include <fs/task.h>
5
#include <fs/maccess.h>
6
#include <fs/fcntl.h>
7
 
8
#include "file.h"
9
#include "inode.h"
10
#include "dentry.h"
11
#include "fileop.h"
12
#include "rwlock.h"
13
#include "fs.h"
14
 
15
#define EBOH EIO
16
 
17
/* temporary: for console i/o */
18
#include <ll/i386/cons.h>
19
static int __getch(int flag)
20
{
684 giacomo 21
 
22
        return 0;
23
 
2 pj 24
}
25
 
26
__ssize_t k_read(int fd, __ptr_t buf, __ssize_t nbytes)
27
{
28
  __pid_t pid;
29
  __ssize_t sz;
30
 
31
  call_to_fs();
32
 
33
  /* TO FIX! */  
34
  if (fd>=0&&fd<=2) {    
35
    __ssize_t x;
36
    int ret,ch;
37
    char *c;    
38
 
39
    //printk(KERN_INFO "reading from console");
40
 
41
    /* WARNIG buf is a pointer into user-space
42
     * we are using it without cast
43
     */
44
 
45
    /* WARNIG there is a blocking call...
46
     * the system can go down if it is blocked!
47
     */
48
 
49
    if (fd!=0) return_from_fs(-EBADF);
50
 
51
    c=(char*)buf;
52
    x=0;    
53
    while (x<nbytes) {
54
      ch=__getch(NON_BLOCK);
55
      if (ch!=-1) *c++=ch;
56
      else {
57
        if (x>0) break;
58
        release_sys_call();
59
        ch=__getch(BLOCK);
60
        ret=reacquire_sys_call();
61
        if (ret) return -ENOSYS;
62
        *c++=ch;
63
      }
64
      x++;
65
      cputc(ch);
66
    }
67
 
68
    return_from_fs(x);
69
  }
70
 
71
  /* don't try to use bad file descriptor */
72
  pid=__get_pid();
73
  if (check_fd(pid,fd)) {
74
    printk(KERN_INFO "k_read: using bad file descriptor");
75
    return_from_fs(-EBADF);
76
  }
77
 
78
  /* don't try to read a directory */
79
  if (file_ptr(pid,fd)->f_flag_isdir) return_from_fs(-EBADF);
80
 
81
  /* don't try to read into not user memory */
82
  if (!__verify_write(buf,nbytes)) return_from_fs(-EVERIFY);
83
 
84
  /* if in write only mode */
85
  if (*fildesfflags_ptr(pid,fd)&O_WRONLY)
86
    return_from_fs(-EBOH);
87
 
88
  /* cancellation point */
89
  //fs_test_cancellation(-EINTR);
90
 
91
  /* acquire read lock on inode's file */
92
  __rwlock_rdlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
93
 
94
  /* O_APPEND flags */
95
  if (*fildesfflags_ptr(pid,fd)&O_APPEND) {
96
    sz=0;
97
    goto SKIP;
98
  }
99
 
100
  /* WARNING buf is a pointer into user-space! */
101
  sz=file_ptr(pid,fd)->f_op->read(file_ptr(pid,fd),buf,nbytes);
102
 
103
  /* release the lock */
104
SKIP:
105
  __rwlock_rdunlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
106
 
107
  return_from_fs(sz);
108
}