source: trunk/kernel/syscalls/sys_write.c @ 143

Last change on this file since 143 was 124, checked in by max@…, 7 years ago

rename hal_wbflush->hal_fence

File size: 3.6 KB
Line 
1/*
2 * sys_write.c - write bytes to a file
3 *
4 * Author        Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_uspace.h>
27#include <hal_special.h>
28#include <errno.h>
29#include <vfs.h>
30#include <thread.h>
31#include <printk.h>
32#include <process.h>
33
34/* TODO: user page(s) need to be locked  [AG] */
35
36//////////////////////////////////
37int sys_write( uint32_t   file_id,
38               void     * buf,
39               uint32_t   count )
40{
41    error_t      error;
42    paddr_t      paddr;
43    char         kbuf[CONFIG_VFS_KBUF_SIZE];
44        xptr_t       file_xp;                       // remote file extended pointer
45    uint32_t     nbytes;                        // number of bytes in one iteration
46
47        thread_t   * this = CURRENT_THREAD;
48        process_t  * process = this->process;
49 
50    // check file_id argument
51        if( file_id >= CONFIG_PROCESS_FILE_MAX_NR )
52        {
53        printk("\n[ERROR] in %s : illegal file descriptor index\n", __FUNCTION__ );
54        this->errno = EBADFD;
55                return -1;
56        }
57
58    // check user buffer in user space
59    error = vmm_v2p_translate( false , buf , &paddr );
60
61    if ( error )
62    {
63        printk("\n[ERROR] in %s : user buffer unmapped = %x\n",
64               __FUNCTION__ , (intptr_t)buf );
65                this->errno = EINVAL;
66                return -1;
67    }
68
69    // get extended pointer on remote file descriptor
70    file_xp = process_fd_get_xptr( process , file_id );
71
72    if( file_xp == XPTR_NULL )
73    {
74        printk("\n[ERROR] in %s : undefined file descriptor index = %d\n",
75               __FUNCTION__ , file_id );
76                this->errno = EBADFD;
77                return -1;
78    }
79
80    // get file descriptor cluster and local pointer
81    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
82    cxy_t        file_cxy = GET_CXY( file_xp );
83
84    // check file writable
85    uint32_t attr = hal_remote_lw( XPTR( file_cxy , &file_ptr->attr ) );
86    if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
87        {
88        printk("\n[ERROR] in %s : file %d not writable\n",
89               __FUNCTION__ , file_id );
90                this->errno = EBADFD;
91                return -1;
92        }
93   
94    // transfer at most CONFIG_VFS_KBUF_SIZE bytes per iteration
95    while( count )
96    {
97        if( count <= CONFIG_VFS_KBUF_SIZE ) 
98        {
99            nbytes = count;
100            count  = 0;
101        }
102        else
103        {
104            nbytes = CONFIG_VFS_KBUF_SIZE;
105            count  = count - CONFIG_VFS_KBUF_SIZE;
106        }
107
108        // copy user buffer to kernel buffer
109        hal_copy_to_uspace( buf , kbuf , nbytes );
110
111        // transfer nbytes from kernel buffer
112        error = vfs_move( false,               // write => (to_buffer = false)
113                          file_xp,
114                          kbuf , 
115                          nbytes );
116
117        if( error )
118        {
119            printk("\n[ERROR] in %s cannot read data from file %d\n",
120                   __FUNCTION__ , file_id );
121            this->errno = error;
122            return -1;
123        }
124    }
125
126    hal_fence();
127
128        return 0;
129
130}  // end sys_write()
Note: See TracBrowser for help on using the repository browser.