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

Last change on this file since 686 was 683, checked in by alain, 4 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 6.7 KB
Line 
1/*
2 * sys_opendir.c - Open an user accessible VFS directory.
3 *
4 * Author        Alain Greiner (2016,2017,2018,2019,2020)
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 <hal_vmm.h>
28#include <thread.h>
29#include <process.h>
30#include <user_dir.h>
31#include <printk.h>
32#include <errno.h>
33#include <vseg.h>
34#include <vfs.h>
35#include <syscalls.h>
36#include <shared_syscalls.h>
37
38///////////////////////////////////
39int sys_opendir ( char *  pathname,
40                  DIR  ** dirp )
41{
42    error_t        error;
43    xptr_t         root_inode_xp;          // extended pointer on path root inode
44    xptr_t         inode_xp;               // extended pointer on directory inode
45    vfs_inode_t  * inode_ptr;              // local pointer on directory inode
46    cxy_t          inode_cxy;              // directory inode cluster
47    uint32_t       inode_type;             // to check directory inode type
48    user_dir_t   * dir_ptr;                // local pointer on user_dir_t
49    vseg_t       * vseg;                   // for user space checking
50    intptr_t       ident;                  // dirent array pointer in user space                 
51    char          kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
52       
53        thread_t     * this    = CURRENT_THREAD;  // client thread
54        process_t    * process = this->process;   // client process
55
56#if DEBUG_SYS_OPENDIR || DEBUG_SYSCALLS_ERROR || CONFIG_INSTRUMENTATION_SYSCALLS
57uint64_t     tm_start = hal_get_cycles();
58#endif
59
60    // check DIR buffer in user space
61    error = vmm_get_vseg( process , (intptr_t)dirp, &vseg );
62
63        if( error )
64        {
65
66#if DEBUG_SYSCALLS_ERROR
67printk("\n[ERROR] in %s : thread[%x,%x] / DIR buffer %x unmapped / cycle %d\n",
68__FUNCTION__ , process->pid , this->trdid, dirp, (uint32_t)tm_start );
69#endif
70                this->errno = EINVAL;
71                return -1;
72        }       
73
74    // check pathname in user space
75    error = vmm_get_vseg( process , (intptr_t)pathname, &vseg );
76
77        if( error )
78        {
79
80#if DEBUG_SYSCALLS_ERROR
81printk("\n[ERROR] in %s : thread[%x,%x] / pathname %x unmapped / cycle %d\n",
82__FUNCTION__ , process->pid , this->trdid, pathname, (uint32_t)tm_start );
83#endif
84                this->errno = EINVAL;
85                return -1;
86        }       
87    // check pathname length
88    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
89    {
90
91#if DEBUG_SYSCALLS_ERROR
92printk("\n[ERROR] in %s / thread[%x,%x] : pathname too long / cycle %d\n",
93 __FUNCTION__ , process->pid , this->trdid, (uint32_t)tm_start );
94#endif
95        this->errno = ENFILE;
96        return -1;
97    }
98
99    // copy pathname in kernel space
100    hal_strcpy_from_uspace( XPTR( local_cxy , kbuf ),
101                            pathname,
102                            CONFIG_VFS_MAX_PATH_LENGTH );
103
104#if DEBUG_SYS_OPENDIR
105if( DEBUG_SYS_OPENDIR < (uint32_t)tm_start )
106printk("\n[%s] thread[%x,%x] enter for directory <%s> / cycle %d\n",
107__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_start );
108#endif
109
110    // compute root inode for pathname
111    if( kbuf[0] == '/' )                        // absolute path
112    {
113        // use extended pointer on VFS root inode
114        root_inode_xp = process->vfs_root_xp;
115    }
116    else                                        // relative path
117    {
118        // get cluster and local pointer on reference process
119        xptr_t      ref_xp  = process->ref_xp;
120        process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
121        cxy_t       ref_cxy = GET_CXY( ref_xp );
122
123        // use extended pointer on CWD inode
124        root_inode_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) );
125    }
126
127    // get extended pointer on directory inode
128    error = vfs_lookup( root_inode_xp,
129                        kbuf,
130                        0,
131                        &inode_xp,
132                        NULL );
133    if( error )
134        {
135
136#if DEBUG_SYSCALLS_ERROR
137printk("\n[ERROR] in %s : thread[%x,%x] / cannot found directory <%s> / cycle %d\n",
138__FUNCTION__ , process->pid , this->trdid , kbuf , (uint32_t)tm_start );
139#endif
140                this->errno = ENFILE;
141                return -1;
142        }
143   
144    // check inode type
145    inode_ptr  = GET_PTR( inode_xp );
146    inode_cxy  = GET_CXY( inode_xp );
147    inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) );
148
149    if( inode_type != FILE_TYPE_DIR )
150        {
151
152#if DEBUG_SYSCALLS_ERROR
153printk("\n[ERROR] in %s : thread[%x,%x] / <%s> is not a directory / cycle %d\n",
154__FUNCTION__ , process->pid , this->trdid , kbuf , (uint32_t)tm_start );
155#endif
156                this->errno = ENFILE;
157                return -1;
158        }
159   
160    // create an user_dir_t structure in cluster containing directory inode
161    // map it in the reference user process VMM (in a new ANON vseg)
162    // an get the local pointer on the created user_dir_t structure
163    if( inode_cxy == local_cxy )
164    {
165        dir_ptr = user_dir_create( inode_ptr,
166                                   process->ref_xp );
167    }
168    else
169    {
170        rpc_user_dir_create_client( inode_cxy,
171                                    inode_ptr,
172                                    process->ref_xp,
173                                    &dir_ptr );
174    }
175
176    if( dir_ptr == NULL )
177        {
178
179#if DEBUG_SYSCALLS_ERROR
180printk("\n[ERROR] in %s : thread[%x,%x] / cannot create user_dir for <%s> / cycle %d\n",
181__FUNCTION__ , process->pid , this->trdid , kbuf , (uint32_t)tm_start );
182#endif
183                this->errno = ENFILE;
184                return -1;
185        }
186   
187    // get ident from user_dir structure
188    ident = (intptr_t)hal_remote_lpt( XPTR( inode_cxy , &dir_ptr->ident ) );
189
190    // set ident value in user buffer
191    hal_copy_to_uspace( dirp,
192                        XPTR( local_cxy , &ident ),
193                        sizeof(intptr_t) );
194
195    hal_fence();
196
197#if (DEBUG_SYS_OPENDIR || CONFIG_INSTRUMENTATION_SYSCALLS)
198uint64_t     tm_end = hal_get_cycles();
199#endif
200
201#if DEBUG_SYS_OPENDIR
202if( DEBUG_SYS_OPENDIR < tm_end )
203printk("\n[%s] thread[%x,%x] exit for directory <%s> / cycle %d\n",
204__FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end );
205#endif
206 
207#if CONFIG_INSTRUMENTATION_SYSCALLS
208hal_atomic_add( &syscalls_cumul_cost[SYS_OPENDIR] , tm_end - tm_start );
209hal_atomic_add( &syscalls_occurences[SYS_OPENDIR] , 1 );
210#endif
211
212        return 0;
213
214}  // end sys_opendir()
Note: See TracBrowser for help on using the repository browser.