Subversion Repositories shark

Rev

Rev 2 | 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 "fsconst.h"
13
#include "fs.h"
14
 
15
/* NB: alcune operazioni sui files vanno fatte in mutua esclusione */
16
 
17
/* to FIX:
18
 * -- fd == 0,1,2 then we must write on teminal
19
 *    (must be integrated better)
20
 */
21
#include <ll/i386/cons.h>
22
 
23
__ssize_t k_write(int fd, __ptr_t buf, __ssize_t nbytes)
24
{
25
  __pid_t pid;
26
  __ssize_t sz;
27
 
28
  call_to_fs();
29
 
30
  /* TO FIX! */  
31
  if (fd>=0&&fd<=2) {
32
    __ssize_t x;
33
    char *c;    
34
 
35
    //printk(KERN_INFO "writing on console with fd=%i",fd);
36
 
37
    /* WARNING buf is a pointer into user-space
38
     * we are using it without cast
39
     */
40
 
41
    /* WARNING cputc is called without mutex
42
     * lock
43
     */
44
 
45
    if (fd==0) return_from_fs(-EBADF);
46
 
47
    c=(char*)buf;
48
    x=nbytes;    
49
    while (x-->0) cputc(*c++);
50
 
51
    return_from_fs(nbytes);
52
  }
53
 
54
  /* don't try to use a bad file descriptor */
55
  pid=__get_pid();
56
  if (check_fd(pid,fd)) {
57
    printk(KERN_INFO "k_write: using bad file descriptor");
58
    return_from_fs(-EBADF);
59
  }
60
 
61
  /* don't try to write a directory */
62
  if (file_ptr(pid,fd)->f_flag_isdir) return_from_fs(-EBADF);
63
 
64
  /* don't try to write from not user memory */
65
  if (!__verify_write(buf,nbytes)) return_from_fs(-EVERIFY);
66
 
67
  /* if in read only mode (errno OK?)*/
68
  if (*fildesfflags_ptr(pid,fd)&O_RDONLY)
69
    return_from_fs(-EPERM);
70
 
71
  /* cancellation point */
72
  //fs_test_cancellation(-EINTR);
73
 
74
  /* acquire write lock on inode's file */
75
  __rwlock_wrlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
76
 
77
  if (*fildesfflags_ptr(pid,fd)&O_APPEND)
78
    if (file_ptr(pid,fd)->f_pos!=
79
        file_ptr(pid,fd)->f_dentry->d_inode->i_st.st_size) {
80
      __off_t off;
81
      off=file_ptr(pid,fd)->f_op->lseek(file_ptr(pid,fd),0,SEEK_END);
82
      if (off!=file_ptr(pid,fd)->f_dentry->d_inode->i_st.st_size) {
83
        sz=-ESPIPE;
84
        goto SKIP;
85
      }
86
    }
87
 
88
  /* WARNIG buf is a pointer into user-space */  
89
  sz=file_ptr(pid,fd)->f_op->write(file_ptr(pid,fd),buf,nbytes);
90
 
91
  /* release the lock */
92
SKIP:
93
  __rwlock_wrunlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
94
 
95
  return_from_fs(sz);
96
}