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