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