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