source: trunk/kernel/syscalls/sys_open.c @ 597

Last change on this file since 597 was 594, checked in by alain, 6 years ago

Fix various bugs in sys_stat() and sys_mmap() functions.
Improve debug in other functions.

File size: 3.7 KB
Line 
1/*
2 * sys_open.c - open 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_kernel_types.h>
26#include <hal_uspace.h>
27#include <errno.h>
28#include <thread.h>
29#include <printk.h>
30#include <vfs.h>
31#include <process.h>
32#include <remote_rwlock.h>
33
34#include <syscalls.h>
35
36///////////////////////////////////
37int sys_open ( char     * pathname,
38               uint32_t   flags,
39               uint32_t   mode )
40{
41    error_t        error;
42    xptr_t         file_xp;                          // extended pointer on vfs_file_t
43    uint32_t       file_id;                          // file descriptor index
44    char           kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
45
46    thread_t     * this     = CURRENT_THREAD;
47    process_t    * process  = this->process;
48
49#if (DEBUG_SYS_OPEN || CONFIG_INSTRUMENTATION_SYSCALLS)
50uint64_t     tm_start = hal_get_cycles();
51#endif
52
53    // check fd_array not full
54    if( process_fd_array_full() )
55    {
56
57#if DEBUG_SYSCALLS_ERROR
58printk("\n[ERROR] in %s : file descriptor array full for process %x\n",
59__FUNCTION__ , process->pid );
60#endif
61        this->errno = ENFILE;
62        return -1;
63    }
64
65    // check pathname length
66    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
67    {
68
69#if DEBUG_SYSCALLS_ERROR
70printk("\n[ERROR] in %s : pathname too long\n", __FUNCTION__ );
71#endif
72        this->errno = ENFILE;
73        return -1;
74    }
75
76    // copy pathname in kernel space
77    hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
78
79#if DEBUG_SYS_OPEN
80if( DEBUG_SYS_OPEN < tm_start )
81printk("\n[%s] thread[%x,%x] enter for <%s> / flags %x / cycle %d\n",
82__FUNCTION__, process->pid, this->trdid, kbuf, flags, (uint32_t)tm_start );
83#endif
84 
85    // get cluster and local pointer on reference process
86    xptr_t      ref_xp  = process->ref_xp;
87    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
88    cxy_t       ref_cxy = GET_CXY( ref_xp );
89
90    // get the cwd lock in read mode from reference process
91    remote_rwlock_rd_acquire( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
92
93    // call the relevant VFS function
94    error = vfs_open( process,
95                      kbuf,
96                      flags,
97                      mode,
98                      &file_xp,
99                      &file_id );
100
101    // release the cwd lock
102    remote_rwlock_rd_release( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
103
104    if( error )
105    {
106        printk("\n[ERROR] in %s : cannot create file descriptor for %s\n",
107        __FUNCTION__ , kbuf );
108        this->errno = ENFILE;
109        return -1;
110    }
111
112    hal_fence;
113
114#if (DEBUG_SYS_OPEN || CONFIG_INSTRUMENTATION_SYSCALLS)
115uint64_t     tm_end = hal_get_cycles();
116#endif
117
118#if DEBUG_SYS_OPEN
119if( DEBUG_SYS_OPEN < tm_start )
120printk("\n[%s] thread[%x,%x] exit for <%s> / cycle %d\n",
121__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end );
122#endif
123 
124#if CONFIG_INSTRUMENTATION_SYSCALLS
125hal_atomic_add( &syscalls_cumul_cost[SYS_OPEN] , tm_end - tm_start );
126hal_atomic_add( &syscalls_occurences[SYS_OPEN] , 1 );
127#endif
128
129    return file_id;
130}
Note: See TracBrowser for help on using the repository browser.