[1] | 1 | /* |
---|
[23] | 2 | * sys_stat.c - Return statistics on a file or directory. |
---|
[1] | 3 | * |
---|
[440] | 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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_uspace.h> |
---|
| 26 | #include <hal_special.h> |
---|
[1] | 27 | #include <errno.h> |
---|
| 28 | #include <thread.h> |
---|
[23] | 29 | #include <printk.h> |
---|
[1] | 30 | #include <vfs.h> |
---|
[23] | 31 | #include <vmm.h> |
---|
| 32 | #include <process.h> |
---|
[1] | 33 | |
---|
[506] | 34 | #include <syscalls.h> |
---|
| 35 | |
---|
[407] | 36 | ///////////////////////////////////// |
---|
[566] | 37 | int sys_stat( char * pathname, |
---|
[407] | 38 | struct stat * u_stat ) |
---|
[1] | 39 | { |
---|
[407] | 40 | error_t error; |
---|
[594] | 41 | vseg_t * vseg; // for user space checking |
---|
| 42 | struct stat k_stat; // in kernel space |
---|
| 43 | xptr_t inode_xp; // extended pointer on target inode |
---|
| 44 | |
---|
[407] | 45 | char kbuf[CONFIG_VFS_MAX_PATH_LENGTH]; |
---|
[23] | 46 | |
---|
| 47 | thread_t * this = CURRENT_THREAD; |
---|
| 48 | process_t * process = this->process; |
---|
[1] | 49 | |
---|
[594] | 50 | #if (DEBUG_SYS_STAT || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 51 | uint64_t tm_start = hal_get_cycles(); |
---|
| 52 | #endif |
---|
| 53 | |
---|
[23] | 54 | // check stat structure in user space |
---|
[440] | 55 | error = vmm_get_vseg( process , (intptr_t)u_stat , &vseg ); |
---|
[1] | 56 | |
---|
[23] | 57 | if( error ) |
---|
[1] | 58 | { |
---|
[440] | 59 | |
---|
[509] | 60 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 61 | printk("\n[ERROR] in %s / thread[%x,%x] : stat structure unmapped\n", |
---|
| 62 | __FUNCTION__ , process->pid , this->trdid ); |
---|
[440] | 63 | vmm_display( process , false ); |
---|
| 64 | #endif |
---|
[23] | 65 | this->errno = EINVAL; |
---|
[1] | 66 | return -1; |
---|
[23] | 67 | } |
---|
[1] | 68 | |
---|
[407] | 69 | // check pathname length |
---|
| 70 | if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH ) |
---|
| 71 | { |
---|
[594] | 72 | |
---|
| 73 | #if DEBUG_SYSCALLS_ERROR |
---|
| 74 | printk("\n[ERROR] in %s / thread[%x,%x] : pathname too long\n", |
---|
| 75 | __FUNCTION__ , process->pid , this->trdid ); |
---|
| 76 | #endif |
---|
[407] | 77 | this->errno = ENFILE; |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // copy pathname in kernel space |
---|
| 82 | hal_strcpy_from_uspace( kbuf , pathname , CONFIG_VFS_MAX_PATH_LENGTH ); |
---|
| 83 | |
---|
[594] | 84 | #if DEBUG_SYS_STAT |
---|
| 85 | if( DEBUG_SYS_STAT < tm_start ) |
---|
| 86 | printk("\n[%s] thread[%x,%x] enter for file <%s> / cycle %d\n", |
---|
| 87 | __FUNCTION__, process->pid, this->trdid, kbuf, (uint32_t)tm_start ); |
---|
| 88 | #endif |
---|
| 89 | |
---|
[407] | 90 | // get cluster and local pointer on reference process |
---|
| 91 | xptr_t ref_xp = process->ref_xp; |
---|
| 92 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 93 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 94 | |
---|
| 95 | // get extended pointer on cwd inode |
---|
[566] | 96 | xptr_t cwd_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) ); |
---|
[407] | 97 | |
---|
| 98 | // get the cwd lock in read mode from reference process |
---|
[566] | 99 | remote_rwlock_rd_acquire( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); |
---|
[407] | 100 | |
---|
[23] | 101 | // get extended pointer on remote file descriptor |
---|
[407] | 102 | error = vfs_lookup( cwd_xp, |
---|
[594] | 103 | kbuf, |
---|
[407] | 104 | 0, |
---|
[594] | 105 | &inode_xp ); |
---|
[23] | 106 | |
---|
[407] | 107 | // release the cwd lock |
---|
[566] | 108 | remote_rwlock_rd_release( XPTR( ref_cxy , &ref_ptr->cwd_lock ) ); |
---|
[407] | 109 | |
---|
| 110 | if( error ) |
---|
[23] | 111 | { |
---|
[594] | 112 | |
---|
| 113 | #if DEBUG_SYSCALLS_ERROR |
---|
| 114 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot found file <%s>\n", |
---|
| 115 | __FUNCTION__ , process->pid , this->trdid , pathname ); |
---|
| 116 | #endif |
---|
| 117 | this->errno = ENFILE; |
---|
[23] | 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
[594] | 121 | #if (DEBUG_SYS_STAT & 1) |
---|
| 122 | if( DEBUG_SYS_STAT < tm_start ) |
---|
| 123 | printk("\n[%s] thread[%x,%x] got inode %x in cluster %x for <%s>\n", |
---|
| 124 | __FUNCTION__, process->pid, this->trdid, GET_PTR(inode_xp), GET_CXY(inode_xp), kbuf ); |
---|
| 125 | #endif |
---|
| 126 | |
---|
[407] | 127 | // call VFS function to get stat info |
---|
[594] | 128 | error = vfs_stat( inode_xp, |
---|
[23] | 129 | &k_stat ); |
---|
| 130 | if( error ) |
---|
[1] | 131 | { |
---|
[594] | 132 | |
---|
| 133 | #if DEBUG_SYSCALLS_ERROR |
---|
| 134 | printk("\n[ERROR] in %s / thread[%x,%x] : cannot get stats for inode <%s>\n", |
---|
| 135 | __FUNCTION__ , process->pid , this->trdid , pathname ); |
---|
| 136 | #endif |
---|
| 137 | this->errno = ENFILE; |
---|
[1] | 138 | return -1; |
---|
| 139 | } |
---|
[23] | 140 | |
---|
[594] | 141 | #if (DEBUG_SYS_STAT & 1) |
---|
| 142 | if( DEBUG_SYS_STAT < tm_start ) |
---|
| 143 | printk("\n[%s] thread[%x,%x] set kstat : inum %d / size %d / mode %d\n", |
---|
| 144 | __FUNCTION__, process->pid, this->trdid, k_stat.st_ino, k_stat.st_size, k_stat.st_mode ); |
---|
| 145 | #endif |
---|
| 146 | |
---|
[407] | 147 | // copy k_stat to u_stat |
---|
| 148 | hal_copy_to_uspace( u_stat , &k_stat , sizeof(struct stat) ); |
---|
[1] | 149 | |
---|
[124] | 150 | hal_fence(); |
---|
[23] | 151 | |
---|
[594] | 152 | #if (DEBUG_SYS_STAT || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 153 | uint64_t tm_end = hal_get_cycles(); |
---|
| 154 | #endif |
---|
| 155 | |
---|
| 156 | #if DEBUG_SYS_STAT |
---|
| 157 | if( DEBUG_SYS_STAT < tm_end ) |
---|
[604] | 158 | printk("\n[%s] thread[%x,%x] exit for file <%s> / size %d / mode %d / cycle %d\n", |
---|
| 159 | __FUNCTION__, process->pid, this->trdid, kbuf, |
---|
| 160 | k_stat.st_size, k_stat.st_mode, (uint32_t)tm_end ); |
---|
[594] | 161 | #endif |
---|
| 162 | |
---|
| 163 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 164 | hal_atomic_add( &syscalls_cumul_cost[SYS_STAT] , tm_end - tm_start ); |
---|
| 165 | hal_atomic_add( &syscalls_occurences[SYS_STAT] , 1 ); |
---|
| 166 | #endif |
---|
| 167 | |
---|
[1] | 168 | return 0; |
---|
[23] | 169 | |
---|
| 170 | } // end sys_stat() |
---|
| 171 | |
---|