source: trunk/kernel/syscalls/sys_opendir.c @ 610

Last change on this file since 610 was 610, checked in by alain, 3 years ago

Fix several bugs in VFS to support the following
ksh commandis : cp, mv, rm, mkdir, cd, pwd

File size: 4.0 KB
Line 
1/*
2 * sys_opendir.c - open a directory.
3 *
4 * Author        Alain Greiner (2016,2017,2018)
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 <hal_kernel_types.h>
25#include <hal_uspace.h>
26#include <thread.h>
27#include <process.h>
28#include <printk.h>
29#include <errno.h>
30#include <vfs.h>
31#include <syscalls.h>
32#include <shared_syscalls.h>
33
34///////////////////////////////////
35int sys_opendir ( char *  pathname,
36                  DIR  ** dirp )
37{
38    error_t       error;
39    vseg_t      * vseg;                   // for user space checking
40    xptr_t        root_inode_xp;          // extended pointer on path root inode
41
42    char          kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
43       
44        thread_t  * this    = CURRENT_THREAD;
45        process_t * process = this->process;
46
47#if (DEBUG_SYS_OPENDIR || CONFIG_INSTRUMENTATION_SYSCALLS)
48uint64_t     tm_start = hal_get_cycles();
49#endif
50
51    // check DIR buffer in user space
52    error = vmm_get_vseg( process , (intptr_t)dirp, &vseg );
53
54        if( error )
55        {
56
57#if DEBUG_SYSCALLS_ERROR
58printk("\n[ERROR] in %s / thread[%x,%x] : DIR buffer %x unmapped\n",
59__FUNCTION__ , process->pid , this->trdid, dirp );
60vmm_display( process , false );
61#endif
62                this->errno = EINVAL;
63                return -1;
64        }       
65
66    // check pathname length
67    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
68    {
69
70#if DEBUG_SYSCALLS_ERROR
71printk("\n[ERROR] in %s / thread[%x,%x] : pathname too long\n",
72 __FUNCTION__ , process->pid , this->trdid );
73#endif
74        this->errno = ENFILE;
75        return -1;
76    }
77
78    // copy pathname in kernel space
79    hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
80
81#if DEBUG_SYS_OPENDIR
82if( DEBUG_SYS_OPENDIR < tm_start )
83printk("\n[%s] thread[%x,%x] enter for directory <%s> / cycle %d\n",
84__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_start );
85#endif
86
87    // compute root inode for path
88    if( kbuf[0] == '/' )                        // absolute path
89    {
90        // use extended pointer on VFS root inode
91        root_inode_xp = process->vfs_root_xp;
92    }
93    else                                        // relative path
94    {
95        // get cluster and local pointer on reference process
96        xptr_t      ref_xp  = process->ref_xp;
97        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
98        cxy_t       ref_cxy = GET_CXY( ref_xp );
99
100        // use extended pointer on CWD inode
101        root_inode_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) );
102    }
103
104/*
105    // call the relevant VFS function ???
106    error = vfs_opendir( root_inode_xp,
107                         kbuf );
108    if( error )
109        {
110
111#if DEBUG_SYSCALLS_ERROR
112printk("\n[ERROR] in %s / thread[%x,%x] : cannot open directory <%s>\n",
113__FUNCTION__ , process->pid , this->trdid , pathname );
114#endif
115                this->errno = ENFILE;
116                return -1;
117        }
118   
119    // copy to user space ???
120*/
121
122    hal_fence();
123
124#if (DEBUG_SYS_OPENDIR || CONFIG_INSTRUMENTATION_SYSCALLS)
125uint64_t     tm_end = hal_get_cycles();
126#endif
127
128#if DEBUG_SYS_OPENDIR
129if( DEBUG_SYS_OPENDIR < tm_end )
130printk("\n[%s] thread[%x,%x] exit for directory <%s> / cycle %d\n",
131__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end );
132#endif
133 
134#if CONFIG_INSTRUMENTATION_SYSCALLS
135hal_atomic_add( &syscalls_cumul_cost[SYS_OPENDIR] , tm_end - tm_start );
136hal_atomic_add( &syscalls_occurences[SYS_OPENDIR] , 1 );
137#endif
138
139        return 0;
140
141}  // end sys_opendir()
Note: See TracBrowser for help on using the repository browser.