Subversion Repositories shark

Rev

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