1 | /* |
---|
2 | * vfs.c - Virtual File System implementation. |
---|
3 | * |
---|
4 | * Author Mohamed Lamine Karaoui (2015) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | |
---|
26 | #include <kernel_config.h> |
---|
27 | #include <hal_types.h> |
---|
28 | #include <hal_atomic.h> |
---|
29 | #include <hal_special.h> |
---|
30 | #include <readlock.h> |
---|
31 | #include <spinlock.h> |
---|
32 | #include <printk.h> |
---|
33 | #include <list.h> |
---|
34 | #include <xlist.h> |
---|
35 | #include <slist.h> |
---|
36 | #include <xhtab.h> |
---|
37 | #include <rpc.h> |
---|
38 | #include <errno.h> |
---|
39 | #include <kmem.h> |
---|
40 | #include <mapper.h> |
---|
41 | #include <thread.h> |
---|
42 | #include <process.h> |
---|
43 | #include <vfs.h> |
---|
44 | #include <fatfs.h> |
---|
45 | #include <ramfs.h> |
---|
46 | #include <devfs.h> |
---|
47 | #include <syscalls.h> |
---|
48 | |
---|
49 | |
---|
50 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
51 | // Extern variables |
---|
52 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
53 | |
---|
54 | extern vfs_ctx_t fs_context[FS_TYPES_NR]; // allocated in kernel_init.c |
---|
55 | |
---|
56 | |
---|
57 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
58 | // Context related functions |
---|
59 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
60 | |
---|
61 | //////////////////////////////////////// |
---|
62 | void vfs_ctx_init( vfs_fs_type_t type, |
---|
63 | uint32_t attr, |
---|
64 | uint32_t total_clusters, |
---|
65 | uint32_t cluster_size, |
---|
66 | xptr_t vfs_root_xp, |
---|
67 | void * extend ) |
---|
68 | { |
---|
69 | vfs_ctx_t * vfs_ctx = &fs_context[type]; |
---|
70 | |
---|
71 | vfs_ctx->type = type; |
---|
72 | vfs_ctx->attr = attr; |
---|
73 | vfs_ctx->total_clusters = total_clusters; |
---|
74 | vfs_ctx->cluster_size = cluster_size; |
---|
75 | vfs_ctx->vfs_root_xp = vfs_root_xp; |
---|
76 | vfs_ctx->extend = extend; |
---|
77 | |
---|
78 | spinlock_init( &vfs_ctx->lock ); |
---|
79 | |
---|
80 | bitmap_init( vfs_ctx->bitmap , BITMAP_SIZE(CONFIG_VFS_MAX_INODES) ); |
---|
81 | } |
---|
82 | |
---|
83 | //////////////////////////////////////////// |
---|
84 | error_t vfs_ctx_inum_alloc( vfs_ctx_t * ctx, |
---|
85 | uint32_t * inum ) |
---|
86 | { |
---|
87 | // get lock on inum allocator |
---|
88 | spinlock_lock( &ctx->lock ); |
---|
89 | |
---|
90 | // get lid from local inum allocator |
---|
91 | uint32_t lid = bitmap_ffc( ctx->bitmap , CONFIG_VFS_MAX_INODES ); |
---|
92 | |
---|
93 | if( lid == -1 ) // no more free slot => error |
---|
94 | { |
---|
95 | // release lock |
---|
96 | spinlock_unlock( &ctx->lock ); |
---|
97 | |
---|
98 | // return error |
---|
99 | return 1; |
---|
100 | } |
---|
101 | else // found => return inum |
---|
102 | { |
---|
103 | // set slot allocated |
---|
104 | bitmap_set( ctx->bitmap , lid ); |
---|
105 | |
---|
106 | // release lock |
---|
107 | spinlock_unlock( &ctx->lock ); |
---|
108 | |
---|
109 | // return inum |
---|
110 | *inum = (((uint32_t)local_cxy) << 16) | (lid & 0xFFFF); |
---|
111 | return 0; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | //////////////////////////////////////////// |
---|
116 | void vfs_ctx_inum_release( vfs_ctx_t * ctx, |
---|
117 | uint32_t inum ) |
---|
118 | { |
---|
119 | bitmap_clear( ctx->bitmap , inum & 0xFFFF ); |
---|
120 | } |
---|
121 | |
---|
122 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
123 | // Inode related functions |
---|
124 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
125 | |
---|
126 | char * vfs_inode_type_str( uint32_t type ) |
---|
127 | { |
---|
128 | if ( type == INODE_TYPE_FILE ) return "FILE"; |
---|
129 | else if( type == INODE_TYPE_DIR ) return "DIR "; |
---|
130 | else if( type == INODE_TYPE_FIFO ) return "FIFO"; |
---|
131 | else if( type == INODE_TYPE_PIPE ) return "PIPE"; |
---|
132 | else if( type == INODE_TYPE_SOCK ) return "SOCK"; |
---|
133 | else if( type == INODE_TYPE_DEV ) return "DEV "; |
---|
134 | else if( type == INODE_TYPE_SYML ) return "SYML"; |
---|
135 | else return "undefined"; |
---|
136 | } |
---|
137 | |
---|
138 | ////////////////////////////////////////////////////// |
---|
139 | error_t vfs_inode_create( xptr_t dentry_xp, |
---|
140 | vfs_fs_type_t fs_type, |
---|
141 | vfs_inode_type_t inode_type, |
---|
142 | void * extend, |
---|
143 | uint32_t attr, |
---|
144 | uint32_t rights, |
---|
145 | uid_t uid, |
---|
146 | gid_t gid, |
---|
147 | xptr_t * inode_xp ) |
---|
148 | { |
---|
149 | mapper_t * mapper; // associated mapper( to be allocated) |
---|
150 | vfs_inode_t * inode; // inode descriptor (to be allocated) |
---|
151 | uint32_t inum; // inode identifier (to be allocated) |
---|
152 | vfs_ctx_t * ctx; // file system context |
---|
153 | kmem_req_t req; // request to kernel memory allocator |
---|
154 | error_t error; |
---|
155 | |
---|
156 | vfs_dmsg("\n[INFO] %s : enter / local_cxy = %x / parent_xp = %l\n", |
---|
157 | __FUNCTION__ , local_cxy , dentry_xp ); |
---|
158 | |
---|
159 | // check fs type and get pointer on context |
---|
160 | if ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; |
---|
161 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; |
---|
162 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; |
---|
163 | else |
---|
164 | { |
---|
165 | ctx = NULL; |
---|
166 | printk("\n[PANIC] in %s : illegal file system type = %d\n", __FUNCTION__ , fs_type ); |
---|
167 | hal_core_sleep(); |
---|
168 | } |
---|
169 | |
---|
170 | // allocate inum |
---|
171 | error = vfs_ctx_inum_alloc( ctx , &inum ); |
---|
172 | |
---|
173 | if( error ) |
---|
174 | { |
---|
175 | printk("\n[ERROR] in %s : cannot allocate inum\n", __FUNCTION__ ); |
---|
176 | return ENOMEM; |
---|
177 | } |
---|
178 | |
---|
179 | // allocate memory for mapper |
---|
180 | mapper = mapper_create( fs_type ); |
---|
181 | |
---|
182 | if( mapper == NULL ) |
---|
183 | { |
---|
184 | printk("\n[ERROR] in %s : cannot allocate mapper\n", __FUNCTION__ ); |
---|
185 | vfs_ctx_inum_release( ctx , inum ); |
---|
186 | return ENOMEM; |
---|
187 | } |
---|
188 | |
---|
189 | // allocate memory for VFS inode descriptor |
---|
190 | req.type = KMEM_VFS_INODE; |
---|
191 | req.size = sizeof(vfs_inode_t); |
---|
192 | req.flags = AF_KERNEL | AF_ZERO; |
---|
193 | inode = (vfs_inode_t *)kmem_alloc( &req ); |
---|
194 | |
---|
195 | if( inode == NULL ) |
---|
196 | { |
---|
197 | printk("\n[ERROR] in %s : cannot allocate inode descriptor\n", __FUNCTION__ ); |
---|
198 | vfs_ctx_inum_release( ctx , inum ); |
---|
199 | mapper_destroy( mapper ); |
---|
200 | return ENOMEM; |
---|
201 | } |
---|
202 | |
---|
203 | // initialize inode descriptor |
---|
204 | inode->gc = 0; |
---|
205 | inode->type = inode_type; |
---|
206 | inode->inum = inum; |
---|
207 | inode->attr = attr; |
---|
208 | inode->rights = rights; |
---|
209 | inode->uid = uid; |
---|
210 | inode->gid = gid; |
---|
211 | inode->refcount = 0; |
---|
212 | inode->parent_xp = dentry_xp; |
---|
213 | inode->ctx = ctx; |
---|
214 | inode->mapper = mapper; |
---|
215 | inode->extend = extend; |
---|
216 | |
---|
217 | // initialise inode field in mapper |
---|
218 | mapper->inode = inode; |
---|
219 | |
---|
220 | // initialise threads waiting queue |
---|
221 | xlist_root_init( XPTR( local_cxy , &inode->wait_root ) ); |
---|
222 | |
---|
223 | // initialize dentries hash table |
---|
224 | xhtab_init( &inode->children , XHTAB_DENTRY_TYPE ); |
---|
225 | |
---|
226 | // initialize inode locks |
---|
227 | remote_rwlock_init( XPTR( local_cxy , &inode->data_lock ) ); |
---|
228 | remote_spinlock_init( XPTR( local_cxy , &inode->main_lock ) ); |
---|
229 | |
---|
230 | vfs_dmsg("\n[INFO] %s : exit / child_xp = %l / parent_xp = %l\n", |
---|
231 | __FUNCTION__ , XPTR( local_cxy , inode ) , dentry_xp ); |
---|
232 | |
---|
233 | // return extended pointer on inode |
---|
234 | *inode_xp = XPTR( local_cxy , inode ); |
---|
235 | return 0; |
---|
236 | |
---|
237 | } // end vfs_inode_create() |
---|
238 | |
---|
239 | ///////////////////////////////////////////// |
---|
240 | void vfs_inode_destroy( vfs_inode_t * inode ) |
---|
241 | { |
---|
242 | if( inode->refcount ) |
---|
243 | { |
---|
244 | printk("\n[PANIC] in %s : inode refcount non zero\n", __FUNCTION__ ); |
---|
245 | hal_core_sleep(); |
---|
246 | } |
---|
247 | |
---|
248 | // release memory allocated for mapper |
---|
249 | mapper_destroy( inode->mapper ); |
---|
250 | |
---|
251 | // release memory allocate for inode descriptor |
---|
252 | kmem_req_t req; |
---|
253 | req.ptr = inode; |
---|
254 | req.type = KMEM_VFS_INODE; |
---|
255 | kmem_free( &req ); |
---|
256 | |
---|
257 | } // end vfs_inode_destroy() |
---|
258 | |
---|
259 | ///////////////////////////////////////////// |
---|
260 | error_t vfs_inode_load( vfs_inode_t * parent, |
---|
261 | char * name, |
---|
262 | xptr_t child_xp ) |
---|
263 | { |
---|
264 | vfs_dmsg("\n[INFO] %s : enter for child <%s>\n", |
---|
265 | __FUNCTION__ , name ); |
---|
266 | |
---|
267 | error_t error = 0; |
---|
268 | |
---|
269 | assert( (parent != NULL) , __FUNCTION__ , "parent pointer is NULL\n"); |
---|
270 | |
---|
271 | assert( (child_xp != XPTR_NULL) , __FUNCTION__ , "child pointer is NULL\n"); |
---|
272 | |
---|
273 | // get parent inode FS type |
---|
274 | vfs_fs_type_t fs_type = parent->ctx->type; |
---|
275 | |
---|
276 | // call relevant FS function |
---|
277 | if( fs_type == FS_TYPE_FATFS ) |
---|
278 | { |
---|
279 | error = fatfs_inode_load( parent , name , child_xp ); |
---|
280 | } |
---|
281 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
282 | { |
---|
283 | assert( false , __FUNCTION__ , "should not be called for RAMFS\n" ); |
---|
284 | } |
---|
285 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
286 | { |
---|
287 | assert( false , __FUNCTION__ , "should not be called for DEVFS\n" ); |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | assert( false , __FUNCTION__ , "undefined file system type\n" ); |
---|
292 | } |
---|
293 | |
---|
294 | vfs_dmsg("\n[INFO] %s : exit for child <%s>\n", |
---|
295 | __FUNCTION__ , name ); |
---|
296 | |
---|
297 | return error; |
---|
298 | |
---|
299 | } // end vfs_load_inode() |
---|
300 | |
---|
301 | //////////////////////////////////////////// |
---|
302 | void vfs_inode_remote_up( xptr_t inode_xp ) |
---|
303 | { |
---|
304 | // get inode cluster and local pointer |
---|
305 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
306 | vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
307 | |
---|
308 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , 1 ); |
---|
309 | } |
---|
310 | |
---|
311 | ////////////////////////////////////////////// |
---|
312 | void vfs_inode_remote_down( xptr_t inode_xp ) |
---|
313 | { |
---|
314 | // get inode cluster and local pointer |
---|
315 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
316 | vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
317 | |
---|
318 | hal_remote_atomic_add( XPTR( inode_cxy , &inode_ptr->refcount ) , -1 ); |
---|
319 | } |
---|
320 | |
---|
321 | ////////////////////////////////////////////// |
---|
322 | uint32_t vfs_inode_get_size( xptr_t inode_xp ) |
---|
323 | { |
---|
324 | // get inode cluster and local pointer |
---|
325 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
326 | vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
327 | |
---|
328 | // get size |
---|
329 | remote_rwlock_rd_lock( XPTR( cxy , &ptr->data_lock ) ); |
---|
330 | uint32_t size = hal_remote_lw( XPTR( cxy , &ptr->size ) ); |
---|
331 | remote_rwlock_rd_unlock( XPTR( cxy , &ptr->data_lock ) ); |
---|
332 | return size; |
---|
333 | } |
---|
334 | |
---|
335 | //////////////////////////////////////////// |
---|
336 | void vfs_inode_set_size( xptr_t inode_xp, |
---|
337 | uint32_t size ) |
---|
338 | { |
---|
339 | // get inode cluster and local pointer |
---|
340 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
341 | vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
342 | |
---|
343 | // set size |
---|
344 | remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) ); |
---|
345 | hal_remote_sw( XPTR( cxy , &ptr->size ) , size ); |
---|
346 | remote_rwlock_wr_unlock( XPTR( cxy , &ptr->data_lock ) ); |
---|
347 | } |
---|
348 | |
---|
349 | //////////////////////////////////////// |
---|
350 | void vfs_inode_unlock( xptr_t inode_xp ) |
---|
351 | { |
---|
352 | // get inode cluster and local pointer |
---|
353 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
354 | vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
355 | |
---|
356 | // release the main lock |
---|
357 | remote_spinlock_unlock( XPTR( cxy , &ptr->main_lock ) ); |
---|
358 | } |
---|
359 | |
---|
360 | ////////////////////////////////////// |
---|
361 | void vfs_inode_lock( xptr_t inode_xp ) |
---|
362 | { |
---|
363 | // get inode cluster and local pointer |
---|
364 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
365 | vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
366 | |
---|
367 | // get the main lock |
---|
368 | remote_spinlock_lock( XPTR( cxy , &ptr->main_lock ) ); |
---|
369 | } |
---|
370 | |
---|
371 | ///////////////////////////////////////// |
---|
372 | xptr_t vfs_inode_owner( xptr_t inode_xp ) |
---|
373 | { |
---|
374 | // get inode cluster and local pointer |
---|
375 | cxy_t cxy = GET_CXY( inode_xp ); |
---|
376 | vfs_inode_t * ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
377 | |
---|
378 | // get the main lock |
---|
379 | return remote_spinlock_owner( XPTR( cxy , &ptr->main_lock ) ); |
---|
380 | } |
---|
381 | |
---|
382 | ///////////////////////////////////////// |
---|
383 | void vfs_inode_display( xptr_t inode_xp ) |
---|
384 | { |
---|
385 | cxy_t inode_cxy; |
---|
386 | vfs_inode_t * inode_ptr; |
---|
387 | xptr_t dentry_xp; |
---|
388 | cxy_t dentry_cxy; |
---|
389 | vfs_dentry_t * dentry_ptr; |
---|
390 | |
---|
391 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
392 | |
---|
393 | // get inode cluster and local pointer |
---|
394 | inode_cxy = GET_CXY( inode_xp ); |
---|
395 | inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
396 | |
---|
397 | // get parent dentry |
---|
398 | dentry_xp = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); |
---|
399 | |
---|
400 | // get local copy of name |
---|
401 | if( dentry_xp == XPTR_NULL ) // it is the VFS root |
---|
402 | { |
---|
403 | strcpy( name , "/" ); |
---|
404 | } |
---|
405 | else // not the VFS root |
---|
406 | { |
---|
407 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
408 | dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
409 | |
---|
410 | hal_remote_strcpy( XPTR( local_cxy , name ) , |
---|
411 | XPTR( dentry_cxy , &dentry_ptr->name ) ); |
---|
412 | } |
---|
413 | |
---|
414 | // display inode header |
---|
415 | printk("\n*** inode <%s> / inode_xp = %l / dentry_xp = %l ***\n", |
---|
416 | name , inode_xp , dentry_xp ); |
---|
417 | |
---|
418 | // display children from xhtab |
---|
419 | xhtab_display( XPTR( inode_cxy , &inode_ptr->children ) ); |
---|
420 | |
---|
421 | } // end vfs_inode_display() |
---|
422 | |
---|
423 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
424 | // Dentry related functions |
---|
425 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
426 | |
---|
427 | /////////////////////////////////////////////////// |
---|
428 | error_t vfs_dentry_create( vfs_fs_type_t fs_type, |
---|
429 | char * name, |
---|
430 | vfs_inode_t * parent, |
---|
431 | xptr_t * dentry_xp ) |
---|
432 | { |
---|
433 | vfs_ctx_t * ctx; // context descriptor |
---|
434 | vfs_dentry_t * dentry; // dentry descriptor (to be allocated) |
---|
435 | kmem_req_t req; // request to kernel memory allocator |
---|
436 | |
---|
437 | vfs_dmsg("\n[INFO] %s : enter for %s / parent inode = %x / cycle = %d\n", |
---|
438 | __FUNCTION__ , name , parent , hal_time_stamp() ); |
---|
439 | |
---|
440 | // get pointer on context |
---|
441 | if ( fs_type == FS_TYPE_FATFS ) ctx = &fs_context[FS_TYPE_FATFS]; |
---|
442 | else if( fs_type == FS_TYPE_RAMFS ) ctx = &fs_context[FS_TYPE_RAMFS]; |
---|
443 | else if( fs_type == FS_TYPE_DEVFS ) ctx = &fs_context[FS_TYPE_DEVFS]; |
---|
444 | else |
---|
445 | { |
---|
446 | ctx = NULL; |
---|
447 | printk("\n[PANIC] in %s : undefined file system type\n", __FUNCTION__ ); |
---|
448 | hal_core_sleep(); |
---|
449 | } |
---|
450 | |
---|
451 | // get name length |
---|
452 | uint32_t length = strlen( name ); |
---|
453 | |
---|
454 | if( length >= CONFIG_VFS_MAX_NAME_LENGTH ) |
---|
455 | { |
---|
456 | printk("\n[ERROR] in %s : name too long\n", __FUNCTION__ ); |
---|
457 | return EINVAL; |
---|
458 | } |
---|
459 | |
---|
460 | // allocate memory for dentry descriptor |
---|
461 | req.type = KMEM_VFS_DENTRY; |
---|
462 | req.size = sizeof(vfs_dentry_t); |
---|
463 | req.flags = AF_KERNEL | AF_ZERO; |
---|
464 | dentry = (vfs_dentry_t *)kmem_alloc( &req ); |
---|
465 | |
---|
466 | if( dentry == NULL ) |
---|
467 | { |
---|
468 | printk("\n[ERROR] in %s : cannot allocate dentry descriptor\n", __FUNCTION__ ); |
---|
469 | return ENOMEM; |
---|
470 | } |
---|
471 | |
---|
472 | // initialize dentry descriptor |
---|
473 | |
---|
474 | dentry->ctx = ctx; |
---|
475 | dentry->length = length; |
---|
476 | dentry->parent = parent; |
---|
477 | strcpy( dentry->name , name ); |
---|
478 | |
---|
479 | // register dentry in hash table rooted in parent inode |
---|
480 | xhtab_insert( XPTR( local_cxy , &parent->children ), |
---|
481 | name, |
---|
482 | XPTR( local_cxy , &dentry->list ) ); |
---|
483 | |
---|
484 | // return extended pointer on dentry |
---|
485 | *dentry_xp = XPTR( local_cxy , dentry ); |
---|
486 | |
---|
487 | vfs_dmsg("\n[INFO] %s : exit for %s / cycle = %d\n", |
---|
488 | __FUNCTION__ , name , hal_time_stamp() ); |
---|
489 | |
---|
490 | return 0; |
---|
491 | |
---|
492 | } // end vfs_dentry_create() |
---|
493 | |
---|
494 | //////////////////////////////////////////////// |
---|
495 | void vfs_dentry_destroy( vfs_dentry_t * dentry ) |
---|
496 | { |
---|
497 | if( dentry->refcount ) |
---|
498 | { |
---|
499 | printk("\n[PANIC] in %s : dentry refcount non zero\n", __FUNCTION__ ); |
---|
500 | hal_core_sleep(); |
---|
501 | } |
---|
502 | |
---|
503 | kmem_req_t req; |
---|
504 | req.ptr = dentry; |
---|
505 | req.type = KMEM_VFS_DENTRY; |
---|
506 | kmem_free( &req ); |
---|
507 | } |
---|
508 | |
---|
509 | |
---|
510 | |
---|
511 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
512 | // File descriptor related functions |
---|
513 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
514 | |
---|
515 | ///////////////////////////////////////////// |
---|
516 | error_t vfs_file_create( vfs_inode_t * inode, |
---|
517 | uint32_t attr, |
---|
518 | xptr_t * file_xp ) |
---|
519 | { |
---|
520 | vfs_file_t * file; |
---|
521 | kmem_req_t req; |
---|
522 | |
---|
523 | // allocate memory for new file descriptor |
---|
524 | req.type = KMEM_VFS_FILE; |
---|
525 | req.size = sizeof(vfs_file_t); |
---|
526 | req.flags = AF_KERNEL | AF_ZERO; |
---|
527 | file = (vfs_file_t *)kmem_alloc( &req ); |
---|
528 | |
---|
529 | if( file == NULL ) return ENOMEM; |
---|
530 | |
---|
531 | // initializes new file descriptor |
---|
532 | file->gc = 0; |
---|
533 | file->type = inode->type; |
---|
534 | file->attr = attr; |
---|
535 | file->offset = 0; |
---|
536 | file->refcount = 0; |
---|
537 | file->inode = inode; |
---|
538 | file->ctx = inode->ctx; |
---|
539 | file->mapper = inode->mapper; |
---|
540 | |
---|
541 | remote_rwlock_init( XPTR( local_cxy , &file->lock ) ); |
---|
542 | |
---|
543 | *file_xp = XPTR( local_cxy , file ); |
---|
544 | return 0; |
---|
545 | |
---|
546 | } // end vfs_file_create() |
---|
547 | |
---|
548 | /////////////////////////////////////////// |
---|
549 | void vfs_file_destroy( vfs_file_t * file ) |
---|
550 | { |
---|
551 | if( file->refcount ) |
---|
552 | { |
---|
553 | printk("\n[PANIC] in %s : file refcount non zero\n", __FUNCTION__ ); |
---|
554 | hal_core_sleep(); |
---|
555 | } |
---|
556 | |
---|
557 | kmem_req_t req; |
---|
558 | req.ptr = file; |
---|
559 | req.type = KMEM_VFS_FILE; |
---|
560 | kmem_free( &req ); |
---|
561 | |
---|
562 | } // end vfs_file_destroy() |
---|
563 | |
---|
564 | |
---|
565 | //////////////////////////////////////// |
---|
566 | void vfs_file_count_up( xptr_t file_xp ) |
---|
567 | { |
---|
568 | // get file cluster and local pointer |
---|
569 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
570 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
571 | |
---|
572 | // atomically increment count |
---|
573 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , 1 ); |
---|
574 | } |
---|
575 | |
---|
576 | ////////////////////////////////////////// |
---|
577 | void vfs_file_count_down( xptr_t file_xp ) |
---|
578 | { |
---|
579 | // get file cluster and local pointer |
---|
580 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
581 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
582 | |
---|
583 | // atomically decrement count |
---|
584 | hal_remote_atomic_add( XPTR( file_cxy , &file_ptr->refcount ) , -1 ); |
---|
585 | } |
---|
586 | |
---|
587 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
588 | // File access related functions |
---|
589 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
590 | |
---|
591 | //////////////////////////////////// |
---|
592 | error_t vfs_open( xptr_t cwd_xp, |
---|
593 | char * path, |
---|
594 | uint32_t flags, |
---|
595 | uint32_t mode, |
---|
596 | xptr_t * new_file_xp, |
---|
597 | uint32_t * new_file_id ) |
---|
598 | { |
---|
599 | error_t error; |
---|
600 | xptr_t inode_xp; // extended pointer on target inode |
---|
601 | cxy_t inode_cxy; // inode cluster identifier |
---|
602 | vfs_inode_t * inode_ptr; // inode local pointer |
---|
603 | uint32_t file_attr; // file descriptor attributes |
---|
604 | uint32_t lookup_mode; // lookup working mode |
---|
605 | xptr_t file_xp; // extended pointer on created file descriptor |
---|
606 | uint32_t file_id; // created file descriptor index in reference fd_array |
---|
607 | |
---|
608 | vfs_dmsg("\n[INFO] %s : enters for <%s> at cycle %d\n", |
---|
609 | __FUNCTION__ , path , (uint32_t)hal_time_stamp() ); |
---|
610 | |
---|
611 | // compute lookup working mode |
---|
612 | lookup_mode = VFS_LOOKUP_OPEN; |
---|
613 | if( (flags & O_DIR ) ) lookup_mode |= VFS_LOOKUP_DIR; |
---|
614 | if( (flags & O_CREAT ) ) lookup_mode |= VFS_LOOKUP_CREATE; |
---|
615 | if( (flags & O_EXCL ) ) lookup_mode |= VFS_LOOKUP_EXCL; |
---|
616 | |
---|
617 | // compute attributes for the created file |
---|
618 | file_attr = 0; |
---|
619 | if( (flags & O_RDONLY ) == 0 ) file_attr |= FD_ATTR_READ_ENABLE; |
---|
620 | if( (flags & O_WRONLY ) == 0 ) file_attr |= FD_ATTR_WRITE_ENABLE; |
---|
621 | if( (flags & O_SYNC ) ) file_attr |= FD_ATTR_SYNC; |
---|
622 | if( (flags & O_APPEND ) ) file_attr |= FD_ATTR_APPEND; |
---|
623 | if( (flags & O_CLOEXEC) ) file_attr |= FD_ATTR_CLOSE_EXEC; |
---|
624 | |
---|
625 | // get extended pointer on target inode |
---|
626 | error = vfs_lookup( cwd_xp , path , lookup_mode , &inode_xp ); |
---|
627 | |
---|
628 | vfs_dmsg("\n[INFO] %s : get inode_xp = %l for <%s> at cycle %d\n", |
---|
629 | __FUNCTION__ , inode_xp , path , hal_get_cycles() ); |
---|
630 | |
---|
631 | if( error ) return error; |
---|
632 | |
---|
633 | // get target inode cluster and local pointer |
---|
634 | inode_cxy = GET_CXY( inode_xp ); |
---|
635 | inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
636 | |
---|
637 | // create a new file descriptor in cluster containing inode |
---|
638 | if( inode_cxy == local_cxy ) // target cluster is local |
---|
639 | { |
---|
640 | error = vfs_file_create( inode_ptr , file_attr , &file_xp ); |
---|
641 | } |
---|
642 | else // target cluster is remote |
---|
643 | { |
---|
644 | rpc_vfs_file_create_client( inode_cxy , inode_ptr , file_attr , &file_xp , &error ); |
---|
645 | } |
---|
646 | |
---|
647 | if( error ) return error; |
---|
648 | |
---|
649 | // allocate and register a new file descriptor index in reference cluster fd_array |
---|
650 | error = process_fd_register( file_xp , &file_id ); |
---|
651 | |
---|
652 | if( error ) return error; |
---|
653 | |
---|
654 | vfs_dmsg("\n[INFO] %s : exit for <%s> / file_id = %d / file_xp = %l / at cycle %d\n", |
---|
655 | __FUNCTION__ , path , file_id , file_xp , hal_get_cycles() ); |
---|
656 | |
---|
657 | // success |
---|
658 | *new_file_xp = file_xp; |
---|
659 | *new_file_id = file_id; |
---|
660 | return 0; |
---|
661 | |
---|
662 | } // end vfs_open() |
---|
663 | |
---|
664 | ////////////////////////////////////////// |
---|
665 | error_t vfs_user_move( bool_t to_buffer, |
---|
666 | xptr_t file_xp, |
---|
667 | void * buffer, |
---|
668 | uint32_t size ) |
---|
669 | { |
---|
670 | assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" ); |
---|
671 | |
---|
672 | cxy_t file_cxy; // remote file descriptor cluster |
---|
673 | vfs_file_t * file_ptr; // remote file descriptor local pointer |
---|
674 | vfs_inode_type_t inode_type; |
---|
675 | uint32_t file_offset; // current offset in file |
---|
676 | mapper_t * mapper; |
---|
677 | error_t error; |
---|
678 | |
---|
679 | // get cluster and local pointer on remote file descriptor |
---|
680 | file_cxy = GET_CXY( file_xp ); |
---|
681 | file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
682 | |
---|
683 | // get inode type from remote file descriptor |
---|
684 | inode_type = hal_remote_lw( XPTR( file_cxy , &file_ptr->type ) ); |
---|
685 | |
---|
686 | // action depends on inode type |
---|
687 | if( inode_type == INODE_TYPE_FILE ) |
---|
688 | { |
---|
689 | // get mapper pointer and file offset from file descriptor |
---|
690 | file_offset = hal_remote_lw( XPTR( file_cxy , &file_ptr->offset ) ); |
---|
691 | mapper = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->mapper ) ); |
---|
692 | |
---|
693 | // move data between mapper and buffer |
---|
694 | if( file_cxy == local_cxy ) |
---|
695 | { |
---|
696 | error = mapper_move_user( mapper, |
---|
697 | to_buffer, |
---|
698 | file_offset, |
---|
699 | buffer, |
---|
700 | size ); |
---|
701 | } |
---|
702 | else |
---|
703 | { |
---|
704 | rpc_mapper_move_buffer_client( file_cxy, |
---|
705 | mapper, |
---|
706 | to_buffer, |
---|
707 | true, // user buffer |
---|
708 | file_offset, |
---|
709 | (uint64_t)(intptr_t)buffer, |
---|
710 | size, |
---|
711 | &error ); |
---|
712 | } |
---|
713 | |
---|
714 | if( error ) return -1; |
---|
715 | else return size; |
---|
716 | } |
---|
717 | else |
---|
718 | { |
---|
719 | printk("\n[ERROR] in %s : inode is not a file", __FUNCTION__ ); |
---|
720 | return -1; |
---|
721 | } |
---|
722 | } // end vfs_user_move() |
---|
723 | |
---|
724 | ////////////////////////////////////// |
---|
725 | error_t vfs_lseek( xptr_t file_xp, |
---|
726 | uint32_t offset, |
---|
727 | uint32_t whence, |
---|
728 | uint32_t * new_offset ) |
---|
729 | { |
---|
730 | xptr_t offset_xp; |
---|
731 | xptr_t lock_xp; |
---|
732 | cxy_t file_cxy; |
---|
733 | vfs_file_t * file_ptr; |
---|
734 | vfs_inode_t * inode_ptr; |
---|
735 | uint32_t new; |
---|
736 | |
---|
737 | assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" ); |
---|
738 | |
---|
739 | // get cluster and local pointer on remote file descriptor |
---|
740 | file_cxy = GET_CXY( file_xp ); |
---|
741 | file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
742 | |
---|
743 | // build extended pointers on lock and offset |
---|
744 | offset_xp = XPTR( file_cxy , &file_ptr->offset ); |
---|
745 | lock_xp = XPTR( file_cxy , &file_ptr->lock ); |
---|
746 | |
---|
747 | // take file descriptor lock |
---|
748 | remote_rwlock_wr_lock( lock_xp ); |
---|
749 | |
---|
750 | if ( whence == SEEK_CUR ) // new = current + offset |
---|
751 | { |
---|
752 | new = hal_remote_lw( offset_xp ) + offset; |
---|
753 | } |
---|
754 | else if ( whence == SEEK_SET ) // new = offset |
---|
755 | { |
---|
756 | new = offset; |
---|
757 | } |
---|
758 | else if ( whence == SEEK_END ) // new = size + offset |
---|
759 | { |
---|
760 | // get local pointer on remote inode |
---|
761 | inode_ptr = (vfs_inode_t *)hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) ); |
---|
762 | |
---|
763 | new = hal_remote_lw( XPTR( file_cxy , &inode_ptr->size ) ) + offset; |
---|
764 | } |
---|
765 | else |
---|
766 | { |
---|
767 | printk("\n[ERROR] in %s : illegal whence value\n", __FUNCTION__ ); |
---|
768 | remote_rwlock_wr_unlock( lock_xp ); |
---|
769 | return -1; |
---|
770 | } |
---|
771 | |
---|
772 | // set new offset |
---|
773 | hal_remote_sw( offset_xp , new ); |
---|
774 | |
---|
775 | // release file descriptor lock |
---|
776 | remote_rwlock_wr_unlock( lock_xp ); |
---|
777 | |
---|
778 | // success |
---|
779 | if ( new_offset != NULL ) |
---|
780 | *new_offset = new; |
---|
781 | return 0; |
---|
782 | |
---|
783 | } // vfs_lseek() |
---|
784 | |
---|
785 | /////////////////////////////////// |
---|
786 | error_t vfs_close( xptr_t file_xp, |
---|
787 | uint32_t file_id ) |
---|
788 | { |
---|
789 | assert( (file_xp != XPTR_NULL) , __FUNCTION__ , "file_xp == XPTR_NULL" ); |
---|
790 | |
---|
791 | assert( (file_id < CONFIG_PROCESS_FILE_MAX_NR) , __FUNCTION__ , "illegal file_id" ); |
---|
792 | |
---|
793 | thread_t * this = CURRENT_THREAD; |
---|
794 | process_t * process = this->process; |
---|
795 | |
---|
796 | // get cluster and local pointer on remote file descriptor |
---|
797 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
798 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
799 | |
---|
800 | // get local pointer on local cluster manager |
---|
801 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
802 | |
---|
803 | // get owner process cluster and lpid |
---|
804 | cxy_t owner_cxy = CXY_FROM_PID( process->pid ); |
---|
805 | lpid_t lpid = LPID_FROM_PID( process->pid ); |
---|
806 | |
---|
807 | // get extended pointers on copies root and lock |
---|
808 | xptr_t root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] ); |
---|
809 | xptr_t lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] ); |
---|
810 | |
---|
811 | // take the lock protecting the copies |
---|
812 | remote_spinlock_lock( lock_xp ); |
---|
813 | |
---|
814 | // 1) loop on the process descriptor copies to cancel all fd_array[file_id] entries |
---|
815 | xptr_t iter_xp; |
---|
816 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
817 | { |
---|
818 | xptr_t process_xp = XLIST_ELEMENT( iter_xp , process_t , copies_list ); |
---|
819 | cxy_t process_cxy = GET_CXY( process_xp ); |
---|
820 | process_t * process_ptr = (process_t *)GET_PTR( process_xp ); |
---|
821 | |
---|
822 | xptr_t lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); |
---|
823 | xptr_t entry_xp = XPTR( process_cxy , &process_ptr->fd_array.array[file_id] ); |
---|
824 | |
---|
825 | // lock is required for atomic write of a 64 bits word |
---|
826 | remote_rwlock_wr_lock( lock_xp ); |
---|
827 | hal_remote_swd( entry_xp , XPTR_NULL ); |
---|
828 | remote_rwlock_wr_unlock( lock_xp ); |
---|
829 | |
---|
830 | hal_fence(); |
---|
831 | } |
---|
832 | |
---|
833 | // 2) release memory allocated to file descriptor in remote cluster |
---|
834 | if( file_cxy == local_cxy ) // file cluster is local |
---|
835 | { |
---|
836 | vfs_file_destroy( file_ptr ); |
---|
837 | } |
---|
838 | else // file cluster is local |
---|
839 | { |
---|
840 | rpc_vfs_file_destroy_client( file_cxy , file_ptr ); |
---|
841 | } |
---|
842 | |
---|
843 | return 0; |
---|
844 | |
---|
845 | } // end vfs_close() |
---|
846 | |
---|
847 | //////////////////////////////////// |
---|
848 | error_t vfs_unlink( xptr_t cwd_xp, |
---|
849 | char * path ) |
---|
850 | { |
---|
851 | printk("\n[PANIC] %s non implemented\n", __FUNCTION__ ); |
---|
852 | hal_core_sleep(); |
---|
853 | return 0; |
---|
854 | } // vfs_unlink() |
---|
855 | |
---|
856 | /////////////////////////////////////// |
---|
857 | error_t vfs_stat( xptr_t file_xp, |
---|
858 | vfs_stat_t * k_stat ) |
---|
859 | { |
---|
860 | printk("\n[PANIC] %s non implemented\n", __FUNCTION__ ); |
---|
861 | hal_core_sleep(); |
---|
862 | return 0; |
---|
863 | } |
---|
864 | |
---|
865 | //////////////////////////////////////////// |
---|
866 | error_t vfs_readdir( xptr_t file_xp, |
---|
867 | vfs_dirent_t * k_dirent ) |
---|
868 | { |
---|
869 | printk("\n[PANIC] %s non implemented\n", __FUNCTION__ ); |
---|
870 | hal_core_sleep(); |
---|
871 | return 0; |
---|
872 | } |
---|
873 | |
---|
874 | ////////////////////////////////////// |
---|
875 | error_t vfs_mkdir( xptr_t file_xp, |
---|
876 | char * path, |
---|
877 | uint32_t mode ) |
---|
878 | { |
---|
879 | printk("\n[PANIC] %s non implemented\n", __FUNCTION__ ); |
---|
880 | hal_core_sleep(); |
---|
881 | return 0; |
---|
882 | } |
---|
883 | |
---|
884 | //////////////////////////////////// |
---|
885 | error_t vfs_rmdir( xptr_t file_xp, |
---|
886 | char * path ) |
---|
887 | { |
---|
888 | printk("\n[PANIC] %s non implemented\n", __FUNCTION__ ); |
---|
889 | hal_core_sleep(); |
---|
890 | return 0; |
---|
891 | } |
---|
892 | |
---|
893 | /////////////////////////////////// |
---|
894 | error_t vfs_chdir( xptr_t cwd_xp, |
---|
895 | char * path ) |
---|
896 | { |
---|
897 | error_t error; |
---|
898 | xptr_t inode_xp; // extended pointer on target inode |
---|
899 | cxy_t inode_cxy; // target inode cluster identifier |
---|
900 | vfs_inode_t * inode_ptr; // target inode local pointer |
---|
901 | uint32_t mode; // lookup working mode |
---|
902 | vfs_inode_type_t inode_type; // target inode type |
---|
903 | |
---|
904 | // set lookup working mode |
---|
905 | mode = 0; |
---|
906 | |
---|
907 | // get extended pointer on target inode |
---|
908 | error = vfs_lookup( cwd_xp , path , mode , &inode_xp ); |
---|
909 | |
---|
910 | if( error ) return error; |
---|
911 | |
---|
912 | // get inode cluster and local pointer |
---|
913 | inode_cxy = GET_CXY( inode_xp ); |
---|
914 | inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
915 | |
---|
916 | // get inode type from remote file |
---|
917 | inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
918 | |
---|
919 | if( inode_type != INODE_TYPE_DIR ) |
---|
920 | { |
---|
921 | CURRENT_THREAD->errno = ENOTDIR; |
---|
922 | return -1; |
---|
923 | } |
---|
924 | |
---|
925 | printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ ); |
---|
926 | hal_core_sleep(); |
---|
927 | return 0; |
---|
928 | } |
---|
929 | |
---|
930 | /////////////////////////////////// |
---|
931 | error_t vfs_chmod( xptr_t cwd_xp, |
---|
932 | char * path, |
---|
933 | uint32_t rights ) |
---|
934 | { |
---|
935 | error_t error; |
---|
936 | xptr_t inode_xp; // extended pointer on target inode |
---|
937 | cxy_t inode_cxy; // inode cluster identifier |
---|
938 | vfs_inode_t * inode_ptr; // inode local pointer |
---|
939 | uint32_t mode; // lookup working mode |
---|
940 | vfs_inode_type_t inode_type; // target inode type |
---|
941 | |
---|
942 | // set lookup working mode |
---|
943 | mode = 0; |
---|
944 | |
---|
945 | // get extended pointer on target inode |
---|
946 | error = vfs_lookup( cwd_xp , path , mode , &inode_xp ); |
---|
947 | |
---|
948 | if( error ) return error; |
---|
949 | |
---|
950 | // get inode cluster and local pointer |
---|
951 | inode_cxy = GET_CXY( inode_xp ); |
---|
952 | inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
953 | |
---|
954 | // get inode type from remote inode |
---|
955 | inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
956 | |
---|
957 | |
---|
958 | printk("\n[PANIC] %s non fully implemented\n", __FUNCTION__ ); |
---|
959 | hal_core_sleep(); |
---|
960 | return 0; |
---|
961 | } |
---|
962 | |
---|
963 | /////////////////////////////////// |
---|
964 | error_t vfs_mkfifo( xptr_t cwd_xp, |
---|
965 | char * path, |
---|
966 | uint32_t rights ) |
---|
967 | { |
---|
968 | printk("\n[PANIC] in %s : not implemented yet\n", __FUNCTION__ ); |
---|
969 | hal_core_sleep(); |
---|
970 | return 0; |
---|
971 | } |
---|
972 | |
---|
973 | |
---|
974 | |
---|
975 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
976 | // Inode Tree functions |
---|
977 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
978 | |
---|
979 | ///////////////////////////////// |
---|
980 | cxy_t vfs_cluster_random_select() |
---|
981 | { |
---|
982 | uint32_t x_size = LOCAL_CLUSTER->x_size; |
---|
983 | uint32_t y_size = LOCAL_CLUSTER->y_size; |
---|
984 | uint32_t y_width = LOCAL_CLUSTER->y_width; |
---|
985 | uint32_t index = ( hal_get_cycles() + hal_get_gid() ) % (x_size * y_size); |
---|
986 | uint32_t x = index / y_size; |
---|
987 | uint32_t y = index % y_size; |
---|
988 | |
---|
989 | return (x<<y_width) + y; |
---|
990 | } |
---|
991 | |
---|
992 | |
---|
993 | ////////////////////////////////////////////////////////////////////////// |
---|
994 | // This static function is called by the vfs_display() function. |
---|
995 | ////////////////////////////////////////////////////////////////////////// |
---|
996 | static void vfs_recursive_display( xptr_t inode_xp, |
---|
997 | xptr_t name_xp, |
---|
998 | xptr_t dentry_xp, |
---|
999 | uint32_t indent ) |
---|
1000 | { |
---|
1001 | cxy_t inode_cxy; |
---|
1002 | vfs_inode_t * inode_ptr; |
---|
1003 | vfs_inode_type_t inode_type; |
---|
1004 | xptr_t children_xp; // extended pointer on children xhtab |
---|
1005 | |
---|
1006 | xptr_t child_dentry_xp; |
---|
1007 | cxy_t child_dentry_cxy; |
---|
1008 | vfs_dentry_t * child_dentry_ptr; |
---|
1009 | xptr_t child_inode_xp; |
---|
1010 | xptr_t child_dentry_name_xp; |
---|
1011 | |
---|
1012 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; |
---|
1013 | |
---|
1014 | char * indent_str[] = { "", // level 0 |
---|
1015 | " ", // level 1 |
---|
1016 | " ", // level 2 |
---|
1017 | " ", // level 3 |
---|
1018 | " ", // level 4 |
---|
1019 | " ", // level 5 |
---|
1020 | " ", // level 6 |
---|
1021 | " ", // level 7 |
---|
1022 | " ", // level 8 |
---|
1023 | " ", // level 9 |
---|
1024 | " ", // level 10 |
---|
1025 | " ", // level 11 |
---|
1026 | " ", // level 12 |
---|
1027 | " ", // level 13 |
---|
1028 | " ", // level 14 |
---|
1029 | " " }; // level 15 |
---|
1030 | |
---|
1031 | assert( (inode_xp != XPTR_NULL) , __FUNCTION__ , "inode_xp cannot be NULL\n" ); |
---|
1032 | assert( (name_xp != XPTR_NULL) , __FUNCTION__ , "name_xp cannot be NULL\n" ); |
---|
1033 | assert( (indent < 16) , __FUNCTION__ , "depth cannot be larger than 15\n" ); |
---|
1034 | |
---|
1035 | // get inode cluster and local pointer |
---|
1036 | inode_cxy = GET_CXY( inode_xp ); |
---|
1037 | inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
1038 | |
---|
1039 | // get inode type |
---|
1040 | inode_type = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->type ) ); |
---|
1041 | |
---|
1042 | // make a local copy of node name |
---|
1043 | hal_remote_strcpy( XPTR( local_cxy , name ) , name_xp ); |
---|
1044 | |
---|
1045 | // display inode |
---|
1046 | printk("%s%s <%s> inode_xp = %l / dentry_xp = %l\n", |
---|
1047 | indent_str[indent], vfs_inode_type_str( inode_type ), |
---|
1048 | name , inode_xp , dentry_xp ); |
---|
1049 | |
---|
1050 | // scan directory entries |
---|
1051 | if( inode_type == INODE_TYPE_DIR ) |
---|
1052 | { |
---|
1053 | // get extended pointer on directory entries xhtab |
---|
1054 | children_xp = XPTR( inode_cxy , &inode_ptr->children ); |
---|
1055 | |
---|
1056 | // get xhtab lock |
---|
1057 | xhtab_read_lock( children_xp ); |
---|
1058 | |
---|
1059 | // get first dentry from xhtab |
---|
1060 | child_dentry_xp = xhtab_get_first( children_xp ); |
---|
1061 | |
---|
1062 | while( child_dentry_xp != XPTR_NULL ) |
---|
1063 | { |
---|
1064 | // get dentry cluster and local pointer |
---|
1065 | child_dentry_cxy = GET_CXY( child_dentry_xp ); |
---|
1066 | child_dentry_ptr = (vfs_dentry_t *)GET_PTR( child_dentry_xp ); |
---|
1067 | |
---|
1068 | // get extended pointer on child inode |
---|
1069 | child_inode_xp = hal_remote_lwd( XPTR( child_dentry_cxy, |
---|
1070 | &child_dentry_ptr->child_xp ) ); |
---|
1071 | |
---|
1072 | // get extended pointer on dentry name |
---|
1073 | child_dentry_name_xp = XPTR( child_dentry_cxy , &child_dentry_ptr->name ); |
---|
1074 | |
---|
1075 | // recursive call on child inode |
---|
1076 | vfs_recursive_display( child_inode_xp, |
---|
1077 | child_dentry_name_xp, |
---|
1078 | child_dentry_xp, |
---|
1079 | indent+1 ); |
---|
1080 | |
---|
1081 | // get next dentry |
---|
1082 | child_dentry_xp = xhtab_get_next( children_xp ); |
---|
1083 | } |
---|
1084 | |
---|
1085 | // release xhtab lock |
---|
1086 | xhtab_read_unlock( children_xp ); |
---|
1087 | } |
---|
1088 | } // end vfs_recursive_display() |
---|
1089 | |
---|
1090 | /////////////////////////////////// |
---|
1091 | void vfs_display( xptr_t inode_xp ) |
---|
1092 | { |
---|
1093 | xptr_t name_xp; |
---|
1094 | xptr_t dentry_xp; |
---|
1095 | cxy_t dentry_cxy; |
---|
1096 | vfs_dentry_t * dentry_ptr; |
---|
1097 | |
---|
1098 | // get target inode cluster and local pointer |
---|
1099 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
1100 | vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
1101 | |
---|
1102 | // get extended pointer on associated dentry |
---|
1103 | dentry_xp = hal_remote_lwd( XPTR( inode_cxy , &inode_ptr->parent_xp ) ); |
---|
1104 | |
---|
1105 | // check if target inode is the File System root |
---|
1106 | if( dentry_xp == XPTR_NULL ) |
---|
1107 | { |
---|
1108 | // build extended pointer on root name |
---|
1109 | name_xp = XPTR( local_cxy , "/" ); |
---|
1110 | } |
---|
1111 | else |
---|
1112 | { |
---|
1113 | // get dentry cluster and local pointer |
---|
1114 | dentry_cxy = GET_CXY( dentry_xp ); |
---|
1115 | dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
1116 | |
---|
1117 | // get extended pointer on dentry name |
---|
1118 | name_xp = XPTR( dentry_cxy , &dentry_ptr->name ); |
---|
1119 | } |
---|
1120 | |
---|
1121 | // print header |
---|
1122 | printk("\n*** VFS ***\n"); |
---|
1123 | |
---|
1124 | // call recursive function |
---|
1125 | vfs_recursive_display( inode_xp , name_xp , dentry_xp , 0 ); |
---|
1126 | |
---|
1127 | } // end vfs_display() |
---|
1128 | |
---|
1129 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1130 | // This function is used by the vfs_lookup() function. |
---|
1131 | // It takes an extended pointer on a remote inode (parent directory inode), |
---|
1132 | // and check access_rights violation for the calling thread. |
---|
1133 | // It can be used by any thread running in any cluster. |
---|
1134 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1135 | // @ inode_xp : extended pointer on inode. |
---|
1136 | // @ client_uid : client thread user ID |
---|
1137 | // @ client_gid : client thread group ID |
---|
1138 | // @ return true if access rights are violated. |
---|
1139 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1140 | bool_t vfs_access_denied( xptr_t inode_xp, |
---|
1141 | uint32_t client_uid, |
---|
1142 | uint32_t client_gid ) |
---|
1143 | { |
---|
1144 | // get found inode cluster and local pointer |
---|
1145 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
1146 | vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
1147 | |
---|
1148 | // get inode access mode, UID, and GID |
---|
1149 | // TODO uint32_t mode = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->mode ) ); |
---|
1150 | uid_t uid = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->uid ) ); |
---|
1151 | gid_t gid = hal_remote_lw( XPTR( inode_cxy , &inode_ptr->gid ) ); |
---|
1152 | |
---|
1153 | // FIXME : me must use mode |
---|
1154 | if( (uid == client_uid) || (gid == client_gid) ) return false; |
---|
1155 | else return true; |
---|
1156 | } |
---|
1157 | |
---|
1158 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1159 | // This static function is used by the vfs_lookup() function. |
---|
1160 | // It takes an extended pointer on a remote parent directory inode, a directory |
---|
1161 | // entry name, and returns an extended pointer on the child inode. |
---|
1162 | // It can be used by any thread running in any cluster. |
---|
1163 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1164 | // @ parent_xp : extended pointer on parent inode in remote cluster. |
---|
1165 | // @ name : dentry name |
---|
1166 | // @ child_xp : [out] buffer for extended pointer on child inode. |
---|
1167 | // @ return true if success / return false if not found. |
---|
1168 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1169 | static bool_t vfs_get_child( xptr_t parent_xp, |
---|
1170 | char * name, |
---|
1171 | xptr_t * child_xp ) |
---|
1172 | { |
---|
1173 | xptr_t xhtab_xp; // extended pointer on hash table containing children dentries |
---|
1174 | xptr_t dentry_xp; // extended pointer on children dentry |
---|
1175 | |
---|
1176 | // get parent inode cluster and local pointer |
---|
1177 | cxy_t parent_cxy = GET_CXY( parent_xp ); |
---|
1178 | vfs_inode_t * parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp ); |
---|
1179 | |
---|
1180 | // get extended pointer on hash table of children directory entries |
---|
1181 | xhtab_xp = XPTR( parent_cxy , &parent_ptr->children ); |
---|
1182 | |
---|
1183 | // search extended pointer on matching dentry |
---|
1184 | dentry_xp = xhtab_lookup( xhtab_xp , name ); |
---|
1185 | |
---|
1186 | if( dentry_xp == XPTR_NULL ) return false; |
---|
1187 | |
---|
1188 | // get dentry cluster and local pointer |
---|
1189 | cxy_t dentry_cxy = GET_CXY( dentry_xp ); |
---|
1190 | vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
1191 | |
---|
1192 | // return child inode |
---|
1193 | *child_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) ); |
---|
1194 | return true; |
---|
1195 | |
---|
1196 | } // end vfs_get_child() |
---|
1197 | |
---|
1198 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1199 | // This static function is used by the vfs_lookup() function. |
---|
1200 | // It takes the <current> pointer on a buffer containing a complete pathname, and return |
---|
1201 | // in the <name> buffer, allocated by the caller, a single name in the path. |
---|
1202 | // It return also in the <next> pointer the next character to analyse in the path. |
---|
1203 | // Finally it returns a <last> boolean, that is true when the returned <name> is the |
---|
1204 | // last name in the path. The names are supposed to be separated by one or several '/' |
---|
1205 | // characters, that are not written in the <name> buffer. |
---|
1206 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1207 | // @ current : pointer on first character to analyse in buffer containing the path. |
---|
1208 | // @ name : [out] pointer on buffer allocated by the caller for the returned name. |
---|
1209 | // @ next : [out] pointer on next character to analyse in buffer containing the path. |
---|
1210 | // @ last : [out] true if the returned name is the last (NUL character found). |
---|
1211 | // @ return 0 if success / return EINVAL if string empty (first chracter is NUL). |
---|
1212 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1213 | static error_t vfs_get_name_from_path( char * current, |
---|
1214 | char * name, |
---|
1215 | char ** next, |
---|
1216 | bool_t * last ) |
---|
1217 | { |
---|
1218 | char * ptr = current; |
---|
1219 | |
---|
1220 | // skip leading '/' characters |
---|
1221 | while( *ptr == '/' ) ptr++; |
---|
1222 | |
---|
1223 | // return EINVAL if string empty |
---|
1224 | if( *ptr == 0 ) return EINVAL; |
---|
1225 | |
---|
1226 | // copy all characters in name until NUL or '/' |
---|
1227 | while( (*ptr != 0) && (*ptr !='/') ) *(name++) = *(ptr++); |
---|
1228 | |
---|
1229 | // set NUL terminating character in name buffer |
---|
1230 | *(name++) = 0; |
---|
1231 | |
---|
1232 | // return last an next |
---|
1233 | if( *ptr == 0 ) // last found character is NUL => last name in path |
---|
1234 | { |
---|
1235 | *last = true; |
---|
1236 | } |
---|
1237 | else // last found character is '/' => skip it |
---|
1238 | { |
---|
1239 | *last = false; |
---|
1240 | *next = ptr + 1; |
---|
1241 | } |
---|
1242 | |
---|
1243 | return 0; |
---|
1244 | |
---|
1245 | } // end vfs_get name_from_path() |
---|
1246 | |
---|
1247 | ////////////////////////////////////////////// |
---|
1248 | error_t vfs_lookup( xptr_t cwd_xp, |
---|
1249 | char * pathname, |
---|
1250 | uint32_t mode, |
---|
1251 | xptr_t * inode_xp ) |
---|
1252 | { |
---|
1253 | char name[CONFIG_VFS_MAX_NAME_LENGTH]; // one name in path |
---|
1254 | |
---|
1255 | xptr_t parent_xp; // extended pointer on parent inode |
---|
1256 | cxy_t parent_cxy; // cluster for parent inode |
---|
1257 | vfs_inode_t * parent_ptr; // local pointer on parent inode |
---|
1258 | xptr_t child_xp; // extended pointer on child inode |
---|
1259 | cxy_t child_cxy; // cluster for child inode |
---|
1260 | vfs_inode_t * child_ptr; // local pointer on child inode |
---|
1261 | vfs_inode_type_t child_type; // child inode type |
---|
1262 | vfs_fs_type_t fs_type; // File system type |
---|
1263 | vfs_ctx_t * ctx_ptr; // local pointer on FS context |
---|
1264 | char * current; // current pointer on path |
---|
1265 | char * next; // next value for current pointer |
---|
1266 | bool_t last; // true when the name is the last in path |
---|
1267 | bool_t found; // true when a child has been found |
---|
1268 | thread_t * this; // pointer on calling thread descriptor |
---|
1269 | process_t * process; // pointer on calling process descriptor |
---|
1270 | error_t error; |
---|
1271 | |
---|
1272 | vfs_dmsg("\n[INFO] %s : enters for <%s> at cycle %d\n", |
---|
1273 | __FUNCTION__ , pathname , (uint32_t)hal_time_stamp() ); |
---|
1274 | |
---|
1275 | this = CURRENT_THREAD; |
---|
1276 | process = this->process; |
---|
1277 | |
---|
1278 | // get extended pointer on first inode to search |
---|
1279 | if( pathname[0] == '/' ) parent_xp = process->vfs_root_xp; |
---|
1280 | else parent_xp = cwd_xp; |
---|
1281 | |
---|
1282 | // initialise other loop variables |
---|
1283 | current = pathname; |
---|
1284 | next = NULL; |
---|
1285 | last = false; |
---|
1286 | child_xp = XPTR_NULL; |
---|
1287 | |
---|
1288 | // take lock on parent inode |
---|
1289 | vfs_inode_lock( parent_xp ); |
---|
1290 | |
---|
1291 | // load from device if one intermediate node not found |
---|
1292 | // exit while loop when last name found (i.e. last == true) |
---|
1293 | do |
---|
1294 | { |
---|
1295 | // get one name from path, and the "last" flag |
---|
1296 | vfs_get_name_from_path( current , name , &next , &last ); |
---|
1297 | |
---|
1298 | vfs_dmsg("\n[INFO] %s : looking for <%s> / last = %d\n", |
---|
1299 | __FUNCTION__ , name , last ); |
---|
1300 | |
---|
1301 | // search a child dentry matching name in parent inode |
---|
1302 | found = vfs_get_child( parent_xp, |
---|
1303 | name, |
---|
1304 | &child_xp ); |
---|
1305 | |
---|
1306 | // if a child inode is not found in the inode tree: |
---|
1307 | // - we create the missing inode/dentry couple in the inode tree, |
---|
1308 | // - we scan the parent mapper to complete the child inode (type and extension), |
---|
1309 | // - we return an error if child not found on device. |
---|
1310 | // - if the missing child is a directory, we load the child mapper from device |
---|
1311 | |
---|
1312 | // for the last name, the behaviour depends on the "mode" argument: |
---|
1313 | |
---|
1314 | if (found == false ) // child node not found in inode tree |
---|
1315 | { |
---|
1316 | vfs_dmsg("\n[INFO] %s : <%s> not found, try to load it\n", |
---|
1317 | __FUNCTION__ , name ); |
---|
1318 | |
---|
1319 | // release lock on parent inode |
---|
1320 | vfs_inode_unlock( parent_xp ); |
---|
1321 | |
---|
1322 | // get parent inode FS type |
---|
1323 | parent_cxy = GET_CXY( parent_xp ); |
---|
1324 | parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp ); |
---|
1325 | |
---|
1326 | ctx_ptr = (vfs_ctx_t *)hal_remote_lpt( XPTR( parent_cxy , |
---|
1327 | &parent_ptr->ctx ) ); |
---|
1328 | fs_type = hal_remote_lw( XPTR( parent_cxy , &ctx_ptr->type ) ); |
---|
1329 | |
---|
1330 | // select a cluster for missing inode |
---|
1331 | child_cxy = vfs_cluster_random_select(); |
---|
1332 | |
---|
1333 | // insert a new child dentry/inode in parent inode |
---|
1334 | error = vfs_add_child_in_parent( child_cxy, |
---|
1335 | INODE_TYPE_DIR, |
---|
1336 | fs_type, |
---|
1337 | parent_xp, |
---|
1338 | name, |
---|
1339 | NULL, // fs_type_specific inode extend |
---|
1340 | &child_xp ); |
---|
1341 | if( error ) |
---|
1342 | { |
---|
1343 | printk("\n[ERROR] in %s : no memory for inode %s in path %s\n", |
---|
1344 | __FUNCTION__ , name , pathname ); |
---|
1345 | return ENOMEM; |
---|
1346 | } |
---|
1347 | |
---|
1348 | // scan parent mapper to complete the missing inode |
---|
1349 | if( parent_cxy == local_cxy ) |
---|
1350 | { |
---|
1351 | error = vfs_inode_load( parent_ptr, |
---|
1352 | name, |
---|
1353 | child_xp ); |
---|
1354 | } |
---|
1355 | else |
---|
1356 | { |
---|
1357 | rpc_vfs_inode_load_client( parent_cxy, |
---|
1358 | parent_ptr, |
---|
1359 | name, |
---|
1360 | child_xp, |
---|
1361 | &error ); |
---|
1362 | } |
---|
1363 | |
---|
1364 | if ( error ) |
---|
1365 | { |
---|
1366 | printk("\n[ERROR] in %s : node %s not found in path %s\n", |
---|
1367 | __FUNCTION__ , name , pathname ); |
---|
1368 | return ENOENT; |
---|
1369 | } |
---|
1370 | |
---|
1371 | // get child inode type |
---|
1372 | child_ptr = (vfs_inode_t *)GET_PTR( child_xp ); |
---|
1373 | child_type = hal_remote_lw( XPTR( child_cxy , &child_ptr->type ) ); |
---|
1374 | |
---|
1375 | // load child mapper from device if it is a directory |
---|
1376 | if( child_type == INODE_TYPE_DIR ) |
---|
1377 | { |
---|
1378 | if( child_cxy == local_cxy ) |
---|
1379 | { |
---|
1380 | error = vfs_mapper_load_all( child_ptr ); |
---|
1381 | } |
---|
1382 | else |
---|
1383 | { |
---|
1384 | rpc_vfs_mapper_load_all_client( child_cxy, |
---|
1385 | child_ptr, |
---|
1386 | &error ); |
---|
1387 | } |
---|
1388 | |
---|
1389 | if ( error ) |
---|
1390 | { |
---|
1391 | printk("\n[ERROR] in %s : cannot access device for node %s in path %s\n", |
---|
1392 | __FUNCTION__ , name , pathname ); |
---|
1393 | return EIO; |
---|
1394 | } |
---|
1395 | } |
---|
1396 | |
---|
1397 | // TODO handle lookup mode here [AG] |
---|
1398 | |
---|
1399 | // take lock on parent inode |
---|
1400 | vfs_inode_lock( parent_xp ); |
---|
1401 | } |
---|
1402 | |
---|
1403 | vfs_dmsg("\n[INFO] %s : found <%s> / parent = %l / child = %l / last = %d\n", |
---|
1404 | __FUNCTION__ , name , parent_xp , child_xp , last ); |
---|
1405 | |
---|
1406 | // TODO check access rights |
---|
1407 | // error = vfs_access_denied( child_xp, |
---|
1408 | // client_uid, |
---|
1409 | // client_gid ); |
---|
1410 | // if( error ) |
---|
1411 | // { |
---|
1412 | // printk("\n[ERROR] in %s : permission denied for %s\n", __FUNCTION__ , name ); |
---|
1413 | // return EACCES; |
---|
1414 | // } |
---|
1415 | |
---|
1416 | // take lock on child inode and release lock on parent |
---|
1417 | vfs_inode_lock( child_xp ); |
---|
1418 | vfs_inode_unlock( parent_xp ); |
---|
1419 | |
---|
1420 | // update loop variables |
---|
1421 | parent_xp = child_xp; |
---|
1422 | current = next; |
---|
1423 | } |
---|
1424 | while( last == false ); |
---|
1425 | |
---|
1426 | // release lock |
---|
1427 | vfs_inode_unlock( parent_xp ); |
---|
1428 | |
---|
1429 | vfs_dmsg("\n[INFO] in %s : exit <%s> found / inode = %l\n", |
---|
1430 | __FUNCTION__ , pathname , child_xp ); |
---|
1431 | |
---|
1432 | // return searched pointer |
---|
1433 | *inode_xp = child_xp; |
---|
1434 | |
---|
1435 | return 0; |
---|
1436 | |
---|
1437 | } // end vfs_lookup() |
---|
1438 | |
---|
1439 | //////////////////////////////////////////// |
---|
1440 | error_t vfs_get_path( xptr_t searched_xp, |
---|
1441 | char * buffer, |
---|
1442 | uint32_t max_size ) |
---|
1443 | { |
---|
1444 | xptr_t dentry_xp; // extended pointer on current dentry |
---|
1445 | char * name; // local pointer on current dentry name |
---|
1446 | uint32_t length; // length of current dentry name |
---|
1447 | uint32_t count; // number of characters written in buffer |
---|
1448 | uint32_t index; // slot index in buffer |
---|
1449 | xptr_t inode_xp; // extended pointer on |
---|
1450 | |
---|
1451 | // implementation note: |
---|
1452 | // we use two variables "index" and "count" because the buffer |
---|
1453 | // is actually written in decreasing index order (from leaf to root) |
---|
1454 | // TODO : handle conflict with a concurrent rename |
---|
1455 | // FIXME : handle synchro in the loop ... [AG] |
---|
1456 | |
---|
1457 | // set the NUL character in buffer / initialise buffer index and count |
---|
1458 | buffer[max_size - 1] = 0; |
---|
1459 | count = 1; |
---|
1460 | index = max_size - 2; |
---|
1461 | |
---|
1462 | // initialize current inode |
---|
1463 | inode_xp = searched_xp; |
---|
1464 | |
---|
1465 | // exit when root inode found (i.e. dentry_xp == XPTR_NULL) |
---|
1466 | do |
---|
1467 | { |
---|
1468 | // get inode cluster and local pointer |
---|
1469 | cxy_t inode_cxy = GET_CXY( inode_xp ); |
---|
1470 | vfs_inode_t * inode_ptr = (vfs_inode_t *)GET_PTR( inode_xp ); |
---|
1471 | |
---|
1472 | // get extended pointer on parent dentry |
---|
1473 | dentry_xp = (xptr_t)hal_remote_lwd( XPTR( inode_cxy , inode_ptr->parent_xp ) ); |
---|
1474 | |
---|
1475 | // get dentry cluster and local pointer |
---|
1476 | cxy_t dentry_cxy = GET_CXY( dentry_xp ); |
---|
1477 | vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
1478 | |
---|
1479 | // get dentry name length and pointer |
---|
1480 | length = hal_remote_lw( XPTR( dentry_cxy , &dentry_ptr->length ) ); |
---|
1481 | name = (char *)hal_remote_lpt( XPTR( dentry_cxy , &dentry_ptr->name ) ); |
---|
1482 | |
---|
1483 | // update index and count |
---|
1484 | index -= (length + 1); |
---|
1485 | count += (length + 1); |
---|
1486 | |
---|
1487 | // check buffer overflow |
---|
1488 | if( count >= max_size ) |
---|
1489 | { |
---|
1490 | printk("\n[ERROR] in %s : kernel buffer too small\n", __FUNCTION__ ); |
---|
1491 | return EINVAL; |
---|
1492 | } |
---|
1493 | |
---|
1494 | // update pathname |
---|
1495 | hal_remote_memcpy( XPTR( local_cxy , &buffer[index + 1] ) , |
---|
1496 | XPTR( dentry_cxy , name ) , length ); |
---|
1497 | buffer[index] = '/'; |
---|
1498 | |
---|
1499 | // get extended pointer on next inode |
---|
1500 | inode_xp = (xptr_t)hal_remote_lwd( XPTR( dentry_cxy , dentry_ptr->parent ) ); |
---|
1501 | } |
---|
1502 | while( (dentry_xp != XPTR_NULL) ); |
---|
1503 | |
---|
1504 | return 0; |
---|
1505 | |
---|
1506 | } // end vfs_get_path() |
---|
1507 | |
---|
1508 | |
---|
1509 | ////////////////////////////////////////////////////////////// |
---|
1510 | error_t vfs_add_child_in_parent( cxy_t child_cxy, |
---|
1511 | vfs_inode_type_t inode_type, |
---|
1512 | vfs_fs_type_t fs_type, |
---|
1513 | xptr_t parent_xp, |
---|
1514 | char * name, |
---|
1515 | void * extend, |
---|
1516 | xptr_t * child_xp ) |
---|
1517 | { |
---|
1518 | error_t error; |
---|
1519 | xptr_t dentry_xp; // extended pointer on created dentry |
---|
1520 | xptr_t inode_xp; // extended pointer on created inode |
---|
1521 | cxy_t parent_cxy; // parent inode cluster identifier |
---|
1522 | vfs_inode_t * parent_ptr; // parent inode local pointer |
---|
1523 | |
---|
1524 | // get parent inode cluster and local pointer |
---|
1525 | parent_cxy = GET_CXY( parent_xp ); |
---|
1526 | parent_ptr = (vfs_inode_t *)GET_PTR( parent_xp ); |
---|
1527 | |
---|
1528 | vfs_dmsg("\n[INFO] %s : enter in cluster %x for %s / child_cxy = %x / parent_xp = %l\n", |
---|
1529 | __FUNCTION__ , local_cxy , name , child_cxy , parent_xp ); |
---|
1530 | |
---|
1531 | // 1. create dentry |
---|
1532 | if( parent_cxy == local_cxy ) // parent cluster is the local cluster |
---|
1533 | { |
---|
1534 | error = vfs_dentry_create( fs_type, |
---|
1535 | name, |
---|
1536 | parent_ptr, |
---|
1537 | &dentry_xp ); |
---|
1538 | |
---|
1539 | vfs_dmsg("\n[INFO] %s : dentry created in local cluster %x\n", |
---|
1540 | __FUNCTION__ , local_cxy ); |
---|
1541 | } |
---|
1542 | else // parent cluster is remote |
---|
1543 | { |
---|
1544 | rpc_vfs_dentry_create_client( parent_cxy, |
---|
1545 | fs_type, |
---|
1546 | name, |
---|
1547 | parent_ptr, |
---|
1548 | &dentry_xp, |
---|
1549 | &error ); |
---|
1550 | |
---|
1551 | vfs_dmsg("\n[INFO] %s : dentry created in remote cluster %x\n", |
---|
1552 | __FUNCTION__ , parent_cxy ); |
---|
1553 | } |
---|
1554 | |
---|
1555 | if( error ) |
---|
1556 | { |
---|
1557 | printk("\n[ERROR] in %s : cannot create dentry in cluster %x\n", |
---|
1558 | __FUNCTION__ , parent_cxy ); |
---|
1559 | return ENOMEM; |
---|
1560 | } |
---|
1561 | |
---|
1562 | // 2. create child inode TODO : define attr / mode / uid / gid |
---|
1563 | uint32_t attr = 0; |
---|
1564 | uint32_t mode = 0; |
---|
1565 | uint32_t uid = 0; |
---|
1566 | uint32_t gid = 0; |
---|
1567 | |
---|
1568 | if( child_cxy == local_cxy ) // child cluster is the local cluster |
---|
1569 | { |
---|
1570 | error = vfs_inode_create( dentry_xp, |
---|
1571 | fs_type, |
---|
1572 | inode_type, |
---|
1573 | extend, |
---|
1574 | attr, |
---|
1575 | mode, |
---|
1576 | uid, |
---|
1577 | gid, |
---|
1578 | &inode_xp ); |
---|
1579 | |
---|
1580 | vfs_dmsg("\n[INFO] %s : inode created in local cluster %x\n", |
---|
1581 | __FUNCTION__ , local_cxy ); |
---|
1582 | } |
---|
1583 | else // child cluster is remote |
---|
1584 | { |
---|
1585 | rpc_vfs_inode_create_client( child_cxy, |
---|
1586 | dentry_xp, |
---|
1587 | fs_type, |
---|
1588 | inode_type, |
---|
1589 | extend, |
---|
1590 | attr, |
---|
1591 | mode, |
---|
1592 | uid, |
---|
1593 | gid, |
---|
1594 | &inode_xp, |
---|
1595 | &error ); |
---|
1596 | |
---|
1597 | vfs_dmsg("\n[INFO] %s : inodecreated in remote cluster %x\n", |
---|
1598 | __FUNCTION__ , child_cxy ); |
---|
1599 | } |
---|
1600 | |
---|
1601 | if( error ) |
---|
1602 | { |
---|
1603 | printk("\n[ERROR] in %s : cannot create inode in cluster %x\n", |
---|
1604 | __FUNCTION__ , child_cxy ); |
---|
1605 | |
---|
1606 | vfs_dentry_t * dentry = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
1607 | if( parent_cxy == local_cxy ) vfs_dentry_destroy( dentry ); |
---|
1608 | else rpc_vfs_dentry_destroy_client( parent_cxy , dentry ); |
---|
1609 | return ENOMEM; |
---|
1610 | } |
---|
1611 | |
---|
1612 | // 3. update extended pointer on inode in dentry |
---|
1613 | cxy_t dentry_cxy = GET_CXY( dentry_xp ); |
---|
1614 | vfs_dentry_t * dentry_ptr = (vfs_dentry_t *)GET_PTR( dentry_xp ); |
---|
1615 | hal_remote_swd( XPTR( dentry_cxy , &dentry_ptr->child_xp ) , inode_xp ); |
---|
1616 | |
---|
1617 | vfs_dmsg("\n[INFO] %s : exit in cluster %x for %s\n", |
---|
1618 | __FUNCTION__ , local_cxy , name ); |
---|
1619 | |
---|
1620 | // success : return extended pointer on child inode |
---|
1621 | *child_xp = inode_xp; |
---|
1622 | return 0; |
---|
1623 | |
---|
1624 | } // end vfs_add_child_in_parent() |
---|
1625 | |
---|
1626 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1627 | // Mapper related functions |
---|
1628 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
1629 | |
---|
1630 | //////////////////////////////////////////// |
---|
1631 | error_t vfs_mapper_move_page( page_t * page, |
---|
1632 | bool_t to_mapper ) |
---|
1633 | { |
---|
1634 | error_t error = 0; |
---|
1635 | |
---|
1636 | assert( (page != NULL) , __FUNCTION__ , "page pointer is NULL\n" ); |
---|
1637 | |
---|
1638 | mapper_t * mapper = page->mapper; |
---|
1639 | |
---|
1640 | |
---|
1641 | assert( (mapper != NULL) , __FUNCTION__ , "no mapper for page\n" ); |
---|
1642 | |
---|
1643 | vfs_dmsg("\n[INFO] %s : enters for page = %d in mapper = %x\n", |
---|
1644 | __FUNCTION__ , page->index , mapper ); |
---|
1645 | |
---|
1646 | // get FS type |
---|
1647 | vfs_fs_type_t fs_type = mapper->type; |
---|
1648 | |
---|
1649 | // call relevant FS function |
---|
1650 | if( fs_type == FS_TYPE_FATFS ) |
---|
1651 | { |
---|
1652 | rwlock_wr_lock( &mapper->lock ); |
---|
1653 | error = fatfs_mapper_move_page( page , to_mapper ); |
---|
1654 | rwlock_wr_unlock( &mapper->lock ); |
---|
1655 | } |
---|
1656 | else if( fs_type == FS_TYPE_RAMFS ) |
---|
1657 | { |
---|
1658 | assert( false , __FUNCTION__ , "should not be called for RAMFS\n" ); |
---|
1659 | } |
---|
1660 | else if( fs_type == FS_TYPE_DEVFS ) |
---|
1661 | { |
---|
1662 | assert( false , __FUNCTION__ , "should not be called for DEVFS\n" ); |
---|
1663 | } |
---|
1664 | else |
---|
1665 | { |
---|
1666 | assert( false , __FUNCTION__ , "undefined file system type\n" ); |
---|
1667 | } |
---|
1668 | |
---|
1669 | vfs_dmsg("\n[INFO] %s : exit for page = %d in mapper = %x\n", |
---|
1670 | __FUNCTION__ , page->index , mapper ); |
---|
1671 | |
---|
1672 | return error; |
---|
1673 | |
---|
1674 | } // end vfs_move_page() |
---|
1675 | |
---|
1676 | ////////////////////////////////////////////////// |
---|
1677 | error_t vfs_mapper_load_all( vfs_inode_t * inode ) |
---|
1678 | { |
---|
1679 | assert( (inode != NULL) , __FUNCTION__ , "inode pointer is NULL\n" ); |
---|
1680 | |
---|
1681 | uint32_t index; |
---|
1682 | page_t * page; |
---|
1683 | |
---|
1684 | mapper_t * mapper = inode->mapper; |
---|
1685 | uint32_t size = inode->size; |
---|
1686 | |
---|
1687 | assert( (mapper != NULL) , __FUNCTION__ , "mapper pointer is NULL\n" ); |
---|
1688 | |
---|
1689 | uint32_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
1690 | if( (size & CONFIG_PPM_PAGE_MASK) || (size == 0) ) npages++; |
---|
1691 | |
---|
1692 | // loop on pages |
---|
1693 | for( index = 0 ; index < npages ; index ++ ) |
---|
1694 | { |
---|
1695 | // this function allocates the missing page in mapper, |
---|
1696 | // and call the vfs_mapper_move_page() to load the page from device |
---|
1697 | page = mapper_get_page( mapper , index ); |
---|
1698 | |
---|
1699 | if( page == NULL ) return EIO; |
---|
1700 | } |
---|
1701 | |
---|
1702 | return 0; |
---|
1703 | |
---|
1704 | } // end vfs_mapper_load_all() |
---|
1705 | |
---|