[1] | 1 | /* |
---|
[611] | 2 | * sys_opendir.c - Open a VFS directory. |
---|
[1] | 3 | * |
---|
[610] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[1] | 5 | * |
---|
[23] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 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 | * |
---|
[23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 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 |
---|
[23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[611] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[610] | 26 | #include <hal_uspace.h> |
---|
[407] | 27 | #include <thread.h> |
---|
| 28 | #include <process.h> |
---|
[611] | 29 | #include <remote_dir.h> |
---|
[407] | 30 | #include <printk.h> |
---|
| 31 | #include <errno.h> |
---|
[611] | 32 | #include <vseg.h> |
---|
[1] | 33 | #include <vfs.h> |
---|
[23] | 34 | #include <syscalls.h> |
---|
[407] | 35 | #include <shared_syscalls.h> |
---|
[1] | 36 | |
---|
| 37 | /////////////////////////////////// |
---|
[407] | 38 | int sys_opendir ( char * pathname, |
---|
| 39 | DIR ** dirp ) |
---|
[1] | 40 | { |
---|
[611] | 41 | error_t error; |
---|
| 42 | xptr_t root_inode_xp; // extended pointer on path root inode |
---|
| 43 | xptr_t inode_xp; // extended pointer on directory inode |
---|
| 44 | vfs_inode_t * inode_ptr; // local pointer on directory inode |
---|
| 45 | cxy_t inode_cxy; // directory inode cluster |
---|
| 46 | uint32_t inode_type; // to check directory inode type |
---|
| 47 | xptr_t dir_xp; // extended pointer on remote_dir_t |
---|
| 48 | remote_dir_t * dir_ptr; // local pointer on remote_dir_t |
---|
| 49 | cxy_t dir_cxy; // remote_dir_t cluster identifier |
---|
| 50 | vseg_t * vseg; // for user space checking |
---|
| 51 | intptr_t ident; // dirent array pointer in user space |
---|
[610] | 52 | char kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; |
---|
| 53 | |
---|
[611] | 54 | thread_t * this = CURRENT_THREAD; // client thread |
---|
| 55 | process_t * process = this->process; // client process |
---|
[610] | 56 | |
---|
| 57 | #if (DEBUG_SYS_OPENDIR || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 58 | uint64_t tm_start = hal_get_cycles(); |
---|
| 59 | #endif |
---|
| 60 | |
---|
| 61 | // check DIR buffer in user space |
---|
| 62 | error = vmm_get_vseg( process , (intptr_t)dirp, &vseg ); |
---|
| 63 | |
---|
| 64 | if( error ) |
---|
| 65 | { |
---|
| 66 | |
---|
| 67 | #if DEBUG_SYSCALLS_ERROR |
---|
| 68 | printk("\n[ERROR] in %s / thread[%x,%x] : DIR buffer %x unmapped\n", |
---|
| 69 | __FUNCTION__ , process->pid , this->trdid, dirp ); |
---|
| 70 | vmm_display( process , false ); |
---|
| 71 | #endif |
---|
| 72 | this->errno = EINVAL; |
---|
| 73 | return -1; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // check pathname length |
---|
| 77 | if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH ) |
---|
| 78 | { |
---|
| 79 | |
---|
| 80 | #if DEBUG_SYSCALLS_ERROR |
---|
| 81 | printk("\n[ERROR] in %s / thread[%x,%x] : pathname too long\n", |
---|
| 82 | __FUNCTION__ , process->pid , this->trdid ); |
---|
| 83 | #endif |
---|
| 84 | this->errno = ENFILE; |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // copy pathname in kernel space |
---|
| 89 | hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH ); |
---|
| 90 | |
---|
| 91 | #if DEBUG_SYS_OPENDIR |
---|
| 92 | if( DEBUG_SYS_OPENDIR < tm_start ) |
---|
| 93 | printk("\n[%s] thread[%x,%x] enter for directory <%s> / cycle %d\n", |
---|
| 94 | __FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_start ); |
---|
| 95 | #endif |
---|
| 96 | |
---|
[611] | 97 | // compute root inode for pathname |
---|
[610] | 98 | if( kbuf[0] == '/' ) // absolute path |
---|
| 99 | { |
---|
| 100 | // use extended pointer on VFS root inode |
---|
| 101 | root_inode_xp = process->vfs_root_xp; |
---|
| 102 | } |
---|
| 103 | else // relative path |
---|
| 104 | { |
---|
| 105 | // get cluster and local pointer on reference process |
---|
| 106 | xptr_t ref_xp = process->ref_xp; |
---|
| 107 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 108 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 109 | |
---|
| 110 | // use extended pointer on CWD inode |
---|
| 111 | root_inode_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) ); |
---|
| 112 | } |
---|
| 113 | |
---|
[611] | 114 | // get extended pointer on directory inode |
---|
| 115 | error = vfs_lookup( root_inode_xp, |
---|
| 116 | kbuf, |
---|
| 117 | 0, |
---|
| 118 | &inode_xp, |
---|
| 119 | NULL ); |
---|
[610] | 120 | if( error ) |
---|
| 121 | { |
---|
| 122 | |
---|
| 123 | #if DEBUG_SYSCALLS_ERROR |
---|
[611] | 124 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot found directory <%s>\n", |
---|
| 125 | __FUNCTION__ , process->pid , this->trdid , kbuf ); |
---|
[610] | 126 | #endif |
---|
| 127 | this->errno = ENFILE; |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | |
---|
[611] | 131 | // check inode type |
---|
| 132 | inode_ptr = GET_PTR( inode_xp ); |
---|
| 133 | inode_cxy = GET_CXY( inode_xp ); |
---|
| 134 | inode_type = hal_remote_l32( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
[610] | 135 | |
---|
[611] | 136 | if( inode_type != INODE_TYPE_DIR ) |
---|
| 137 | { |
---|
| 138 | |
---|
| 139 | #if DEBUG_SYSCALLS_ERROR |
---|
| 140 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot found directory <%s>\n", |
---|
| 141 | __FUNCTION__ , process->pid , this->trdid , kbuf ); |
---|
| 142 | #endif |
---|
| 143 | this->errno = ENFILE; |
---|
| 144 | return -1; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | // allocate, initialize, and register a new remote_dir_t structure |
---|
| 148 | // in the calling process reference cluster |
---|
| 149 | dir_xp = remote_dir_create( inode_xp ); |
---|
| 150 | dir_ptr = GET_PTR( dir_xp ); |
---|
| 151 | dir_cxy = GET_CXY( dir_xp ); |
---|
| 152 | |
---|
| 153 | if( dir_xp == XPTR_NULL ) |
---|
| 154 | { |
---|
| 155 | |
---|
| 156 | #if DEBUG_SYSCALLS_ERROR |
---|
| 157 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot create remote_dir for <%s>\n", |
---|
| 158 | __FUNCTION__ , process->pid , this->trdid , kbuf ); |
---|
| 159 | #endif |
---|
| 160 | this->errno = ENFILE; |
---|
| 161 | return -1; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // get ident from remote_dir structure |
---|
| 165 | ident = (intptr_t)hal_remote_lpt( XPTR( dir_cxy , &dir_ptr->ident ) ); |
---|
| 166 | |
---|
| 167 | // set ident value in user buffer |
---|
| 168 | hal_copy_to_uspace( dirp , &ident , sizeof(intptr_t) ); |
---|
| 169 | |
---|
[610] | 170 | hal_fence(); |
---|
| 171 | |
---|
| 172 | #if (DEBUG_SYS_OPENDIR || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 173 | uint64_t tm_end = hal_get_cycles(); |
---|
| 174 | #endif |
---|
| 175 | |
---|
| 176 | #if DEBUG_SYS_OPENDIR |
---|
| 177 | if( DEBUG_SYS_OPENDIR < tm_end ) |
---|
| 178 | printk("\n[%s] thread[%x,%x] exit for directory <%s> / cycle %d\n", |
---|
| 179 | __FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_end ); |
---|
| 180 | #endif |
---|
| 181 | |
---|
| 182 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 183 | hal_atomic_add( &syscalls_cumul_cost[SYS_OPENDIR] , tm_end - tm_start ); |
---|
| 184 | hal_atomic_add( &syscalls_occurences[SYS_OPENDIR] , 1 ); |
---|
| 185 | #endif |
---|
| 186 | |
---|
| 187 | return 0; |
---|
| 188 | |
---|
| 189 | } // end sys_opendir() |
---|