source: trunk/kernel/libk/user_dir.c @ 617

Last change on this file since 617 was 614, checked in by alain, 6 years ago

1) introduce a dev_ioc_sync_write() function in IOC API,

to improve the DEVFS synchronous update.

2) fix a big bug in both the user_dir_create() and user_dir_destroy()

functions: add an extended pointer on the reference client process
in the function's arguments.

File size: 17.6 KB
RevLine 
[613]1/*
2 * user_dir.c - kernel DIR related operations implementation.
3 *
4 * Authors   Alain   Greiner (2016,2017,2018)
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 <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_irqmask.h>
27#include <hal_remote.h>
28#include <thread.h>
29#include <xlist.h>
30#include <scheduler.h>
31#include <remote_queuelock.h>
32#include <user_dir.h>
33
34
35/////////////////////////////////////////////
36xptr_t user_dir_from_ident( intptr_t  ident )
37{
38    // get pointer on local process_descriptor
39    process_t * process = CURRENT_THREAD->process;
40
41    // get pointers on reference process
42    xptr_t      ref_xp  = process->ref_xp;
43    cxy_t       ref_cxy = GET_CXY( ref_xp );
44    process_t * ref_ptr = GET_PTR( ref_xp );
45
46    // get extended pointers on open directories list and lock 
47    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->dir_root );
48    xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->dir_lock );
49
50    // get lock protecting open directories list
51    remote_queuelock_acquire( lock_xp );
52 
53    // scan reference process dir list
54    xptr_t           iter_xp;
55    xptr_t           dir_xp;
56    cxy_t            dir_cxy;
57    user_dir_t     * dir_ptr;
58    intptr_t         current;
59    bool_t           found = false;
60           
61    XLIST_FOREACH( root_xp , iter_xp )
62    {
63        dir_xp  = XLIST_ELEMENT( iter_xp , user_dir_t , list );
64        dir_cxy = GET_CXY( dir_xp );
65        dir_ptr = GET_PTR( dir_xp );
66        current = (intptr_t)hal_remote_lpt( XPTR( dir_cxy , &dir_ptr->ident ) );   
67        if( ident == current )
68        {
69            found = true;
70            break;
71        }
72    }
73
74    // relese lock protecting open directories list
75    remote_queuelock_release( lock_xp );
76 
77    if( found == false )  return XPTR_NULL;
78    else                  return dir_xp;
79
80}  // end user_dir_from_ident()
81
[614]82//////////////////////////////////////////////////
83user_dir_t * user_dir_create( vfs_inode_t * inode,
84                              xptr_t        ref_xp )
[613]85{ 
86    user_dir_t    * dir;               // local pointer on created user_dir_t
87    vseg_t        * vseg;              // local pointer on dirent array vseg
88    uint32_t        vseg_size;         // size of vseg in bytes
89    process_t     * ref_ptr;           // local pointer on reference process
90    cxy_t           ref_cxy;           // reference process cluster identifier
[614]91    pid_t           ref_pid;           // reference process PID
[613]92    xptr_t          gpt_xp;            // extended pointer on reference process GPT
93    uint32_t        gpt_attributes;    // attributes for all mapped gpt entries
94    uint32_t        dirents_per_page;  // number of dirent descriptors per page
95    xptr_t          page_xp;           // extended pointer on page descriptor 
96    page_t        * page;              // local pointer on page descriptor
97    xptr_t          base_xp;           // extended pointer on physical page base
98    struct dirent * base;              // local pointer on physical page base
99    uint32_t        total_dirents;     // total number of dirents in dirent array
100    uint32_t        total_pages;       // total number of pages for dirent array
101    vpn_t           vpn;               // first page in dirent array vseg
102    ppn_t           ppn;               // ppn of currently allocated physical page
103    uint32_t        entries;           // number of dirent actually comied in one page
104    uint32_t        first_entry;       // index of first dentry to copy in dirent array
105    bool_t          done;              // last entry found and copied when true
106    list_entry_t    root;              // root of temporary list of allocated pages
107    uint32_t        page_id;           // page index in list of physical pages
108    kmem_req_t      req;               // kmem request descriptor
109    error_t         error;
110
[614]111    // get cluster, local pointer, and pid of reference user process
112    ref_cxy = GET_CXY( ref_xp );
113    ref_ptr = GET_PTR( ref_xp );
114    ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
[613]115
116#if DEBUG_USER_DIR
117uint32_t cycle = (uint32_t)hal_get_cycles();
118thread_t * this = CURRENT_THREAD;
119if( cycle > DEBUG_USER_DIR )
[614]120printk("\n[%s] thread[%x,%x] enter for inode (%x,%x) and process %x / cycle %d\n",
121__FUNCTION__, this->process->pid, this->trdid, local_cxy, inode, ref_pid, cycle );
[613]122#endif
123
124// check dirent size
125assert( ( sizeof(struct dirent) == 64), "sizeof(dirent) != 64\n");
126
127    // compute number of dirent per page
128    dirents_per_page = CONFIG_PPM_PAGE_SIZE >> 6;
129   
130    // initialise temporary list of pages
131    list_root_init( &root );
132
133    // allocate memory for a local user_dir descriptor
134    req.type  = KMEM_DIR;
135    req.flags = AF_ZERO;
136    dir       = kmem_alloc( &req );
137
138    if( dir == NULL )
139    {
140        printk("\n[ERROR] in %s : cannot allocate user_dir_t in cluster %x\n",
141        __FUNCTION__, local_cxy );
142        return NULL;
143    }
144
145    // Build an initialize the dirent array as a list of physical pages.
146    // For each iteration in this while loop:
147    // - allocate one physical 4 Kbytes (64 dirent slots)
148    // - call the relevant FS specific function to scan the directory mapper,
149    //   and copy up to 64 entries in the page.
150    // - register the page in a temporary list using the embedded page list_entry
151    // - exit when the last entry has been found (done == true).
152
153    // initialize loops variables
154    done          = false;
155    total_dirents = 0;
156    total_pages   = 0;
157    first_entry   = 0;
158
159    while( done == false )  // loop on physical pages
160    {
161        // allocate one physical page
162        req.type  = KMEM_PAGE;
163        req.size  = 0;
164        req.flags = AF_ZERO;
165        page      = kmem_alloc( &req );
166
167        if( page == NULL )
168        {
169            printk("\n[ERROR] in %s : cannot allocate page in cluster %x\n",
170            __FUNCTION__, ref_cxy );
171            goto user_dir_create_failure;
172        }
173
174        // get pointer on page base (array of dirents)
175        page_xp  = XPTR( local_cxy , page );
176        base_xp  = ppm_page2base( page_xp );
177        base     = GET_PTR( base_xp );
178
179        // call the relevant FS specific function to copy up to 64 dirents in page
180        error = vfs_fs_get_user_dir( inode,
181                                     base,
182                                     dirents_per_page,
183                                     first_entry,
184                                     false,        // don't create missing inodes
185                                     &entries,
186                                     &done );
187        if( error )
188        {
189            printk("\n[ERROR] in %s : cannot initialise dirent array in cluster %x\n",
190            __FUNCTION__, ref_cxy );
191            goto user_dir_create_failure;
192        }
193
194        // increment number of written dirents
195        total_dirents += entries;
196
197        // register page in temporary list
198        list_add_last( &root , &page->list ); 
199        total_pages++; 
200
201        // set first_entry for next iteration
202        first_entry = total_dirents;
203
204    } // end while
205       
[614]206#if DEBUG_USER_DIR
207if( cycle > DEBUG_USER_DIR )
208printk("\n[%s] thread[%x,%x] initialised dirent array / %d entries\n",
209__FUNCTION__, this->process->pid, this->trdid, total_dirents, cycle );
210#endif
211
[613]212    // compute required vseg size for a 64 bytes dirent
213    vseg_size = total_dirents << 6;
214
215    // create an ANON vseg and register it in reference process VSL
216    if( local_cxy == ref_cxy )
217    {
[614]218        vseg = vmm_create_vseg( ref_ptr,
[613]219                                VSEG_TYPE_ANON,
220                                0,                      // vseg base (unused)
221                                vseg_size,
222                                0,                      // file offset (unused)
223                                0,                      // file_size (unused)
224                                XPTR_NULL,              // mapper (unused)
[614]225                                local_cxy );
[613]226    }
227    else
228    {
229        rpc_vmm_create_vseg_client( ref_cxy,
230                                    ref_ptr,
231                                    VSEG_TYPE_ANON,
232                                    0,                     // vseg base (unused)
233                                    vseg_size,
234                                    0,                     // file offset (unused)
235                                    0,                     // file size (unused)
236                                    XPTR_NULL,             // mapper (unused)
[614]237                                    local_cxy,
[613]238                                    &vseg ); 
239    }
[614]240
[613]241    if( vseg == NULL )
242    {
[614]243        printk("\n[ERROR] in %s : cannot create vseg for user_dir in cluster %x\n",
[613]244        __FUNCTION__, ref_cxy);
245        goto user_dir_create_failure;
246    }
247
[614]248#if DEBUG_USER_DIR
[613]249if( cycle > DEBUG_USER_DIR )
250printk("\n[%s] thread[%x,%x] allocated vseg ANON / base %x / size %x\n",
[614]251__FUNCTION__, this->process->pid, this->trdid, vseg->min, vseg->max - vseg->min );
[613]252#endif
253
254// check vseg size
255assert( (total_pages == hal_remote_l32( XPTR( ref_cxy , &vseg->vpn_size ) ) ),
256"unconsistent vseg size for dirent array" );
257
258    // build extended pointer on reference process GPT, PTE attributes and ppn
259    gpt_xp         = XPTR( ref_cxy , &ref_ptr->vmm.gpt );
260    gpt_attributes = GPT_MAPPED   |
261                     GPT_SMALL    |
262                     GPT_READABLE |
263                     GPT_CACHABLE |
264                     GPT_USER     ;
265
266    // get first vpn from vseg descriptor
267    vpn = hal_remote_l32( XPTR( ref_cxy , &vseg->vpn_base ) );
268
269    // scan the list of allocated physical pages to map
270    // all physical pages in the in the reference process GPT
271    page_id = 0;
272    while( list_is_empty( &root ) == false )
273    {
274        // get pointer on first page descriptor
275        page = LIST_FIRST( &root , page_t , list );
276
277        // compute ppn
278        ppn = ppm_page2ppn( XPTR( local_cxy , page ) );
279       
280        error = hal_gpt_set_pte( gpt_xp,
281                                 vpn + page_id,
282                                 gpt_attributes,
283                                 ppn );
284        if( error )
285        {
286            printk("\n[ERROR] in %s : cannot map vpn %x in GPT\n",
287            __FUNCTION__, (vpn + page_id) );
288            // use the non blocking RPC to delete the remote vseg
289            rpc_desc_t     desc;
290            desc.index     = RPC_VMM_DELETE_VSEG;
291            desc.responses = 1;
292            desc.thread    = CURRENT_THREAD;
293            desc.lid       = CURRENT_THREAD->core->lid;
294            desc.blocking  = true;
[614]295            desc.args[0]   = ref_pid;
[613]296            desc.args[1]   = vpn << CONFIG_PPM_PAGE_SHIFT;
297            rpc_vmm_delete_vseg_client( ref_cxy , &desc );
298            // release the user_dir descriptor
299            req.type = KMEM_DIR;
300            req.ptr  = dir;
301            kmem_free( &req );
302            return NULL;
303        }
304
[614]305#if DEBUG_USER_DIR
[613]306if( cycle > DEBUG_USER_DIR )
307printk("\n[%s] thread[%x,%x] mapped vpn %x to ppn %x\n",
[614]308__FUNCTION__, this->process->pid, this->trdid, vpn + page_id, ppn );
[613]309#endif
310
311        // remove the page from temporary list
312        list_unlink( &page->list );
313
314        page_id++;
315
316    }  // end map loop
317
318// check number of pages
319assert( (page_id == total_pages) , "unconsistent pages number\n" );
320
321    // initialise user_dir_t structure
322    dir->current = 0;
323    dir->entries = total_dirents;
324    dir->ident   = (intptr_t)(vpn << CONFIG_PPM_PAGE_SHIFT);
325
326    // build extended pointers on root and lock of user_dir xlist in ref process
327    xptr_t root_xp  = XPTR( ref_cxy , &ref_ptr->dir_root );
328    xptr_t lock_xp  = XPTR( ref_cxy , &ref_ptr->dir_lock );
329
330    // build extended pointer on list field in user_dir structure
331    xptr_t entry_xp = XPTR( local_cxy , &dir->list );
332
333    // get lock protecting open directories list
334    remote_queuelock_acquire( lock_xp );
335
336    // register user_dir_t in reference process 
337    xlist_add_first( root_xp , entry_xp );
338
339    // release lock protecting  open directorie list
340    remote_queuelock_release( lock_xp );
341
342#if DEBUG_USER_DIR
343cycle = (uint32_t)hal_get_cycles();
344if( cycle > DEBUG_USER_DIR )
345printk("\n[%s] thread[%x,%x] created user_dir (%x,%x) / %d entries / cycle %d\n",
[614]346__FUNCTION__, this->process->pid, this->trdid, local_cxy, dir, total_dirents, cycle );
[613]347#endif
348
349    return dir;
350
351user_dir_create_failure:
352
353    // release local user_dir_t structure
354    req.type = KMEM_DIR;
355    req.ptr  = dir;
356    kmem_free( &req );
357
358    // release local physical pages
359    while( list_is_empty( &root ) == false )
360    {
361        page = LIST_FIRST( &root , page_t , list );
362        req.type  = KMEM_PAGE;
363        req.ptr   = page;
364        kmem_free( &req );
365    }
366
367    return NULL;
368
369}  // end user_dir_create()
370
[614]371////////////////////////////////////////
372void user_dir_destroy( user_dir_t * dir,
373                       xptr_t       ref_xp )
[613]374{
[614]375    thread_t     * this;       // local pointer on calling thread
376    process_t    * process;    // local pointer on calling process
[613]377    cluster_t    * cluster;    // local pointer on local cluster
378    intptr_t       ident;      // user pointer on dirent array
[614]379    xptr_t         ref_pid;    // reference process PID
[613]380    cxy_t          ref_cxy;    // reference process cluster identifier
381    process_t    * ref_ptr;    // local pointer on reference process
382    xptr_t         root_xp;    // root of xlist
383    xptr_t         lock_xp;    // extended pointer on lock protecting xlist
384    xptr_t         iter_xp;    // iteratot in xlist
385    reg_t          save_sr;    // for critical section
386    cxy_t          owner_cxy;  // owner process cluster
387    lpid_t         lpid;       // process local index
388    rpc_desc_t     rpc;        // rpc descriptor
389     
[614]390    // get pointers on calling process & thread
[613]391    this    = CURRENT_THREAD;
392    process = this->process;
393    cluster = LOCAL_CLUSTER;
394
[614]395    // get cluster, local pointer, and PID of reference user process
396    ref_cxy = GET_CXY( ref_xp );
397    ref_ptr = GET_PTR( ref_xp );
398    ref_pid = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->pid ) );
399
[613]400#if DEBUG_USER_DIR
401uint32_t cycle = (uint32_t)hal_get_cycles();
402if( cycle > DEBUG_USER_DIR )
[614]403printk("\n[%s] thread[%x,%x] enter for user_dir (%x,%x) and process %x / cycle %d\n",
404__FUNCTION__, process->pid, this->trdid, local_cxy, dir, ref_pid, cycle );
[613]405#endif
406
407    // get user pointer on dirent array
408    ident = dir->ident;
409
410    // build extended pointer on lock protecting open directories list
411    lock_xp = XPTR( ref_cxy , &ref_ptr->dir_lock );
412
413    // get lock protecting open directories list
414    remote_queuelock_acquire( lock_xp );
415
416    // remove dir from reference process xlist
417    xlist_unlink( XPTR( local_cxy , &dir->list ) );
418
419    // release lock protecting open directories list
420    remote_queuelock_release( lock_xp );
421
422    // To delete all copies of the vseg containing the dirent array, the client thread
423    // send parallel RPCs to all clusters containing a client process copy (including
424    // the local cluster). It blocks and deschedules when all RPCs have been sent,
425    // to wait all RPC responses, and will be unblocked by the last RPC server thread.
426    // It allocates a - shared - RPC descriptor in the stack,  because all parallel
427    // server threads use the same input arguments, and the same response field.
428
429    // get owner cluster identifier and process lpid
[614]430    owner_cxy = CXY_FROM_PID( ref_pid );
431    lpid      = LPID_FROM_PID( ref_pid );
[613]432
433    // get root of list of copies and lock from owner cluster
434    root_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_root[lpid] );
435    lock_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
436
437    // mask IRQs
438    hal_disable_irq( &save_sr);
439
440    // client thread blocks itself
441    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC );
442
443    // initialize RPC descriptor shared fields
444    rpc.responses = 0;
445    rpc.blocking  = false;
446    rpc.index     = RPC_VMM_DELETE_VSEG;
447    rpc.thread    = this;
448    rpc.lid       = this->core->lid;
[614]449    rpc.args[0]   = ref_pid;
[613]450    rpc.args[1]   = ident;
451
452    // take the lock protecting process copies
453    remote_queuelock_acquire( lock_xp );
454
455    // scan list of process copies
456    XLIST_FOREACH( root_xp , iter_xp )
457    {
458        // get extended pointer and cluster of process
459        xptr_t      process_xp  = XLIST_ELEMENT( iter_xp , process_t , copies_list );
460        cxy_t       process_cxy = GET_CXY( process_xp );
461
462        // atomically increment responses counter
463        hal_atomic_add( (void *)&rpc.responses , 1 );
464
465        // call RPC 
466        rpc_vmm_delete_vseg_client( process_cxy , &rpc );
467
468    }  // end list of copies
469
470    // release the lock protecting process copies
471    remote_queuelock_release( lock_xp );
472
473    // client thread deschedule
474    sched_yield("blocked on rpc_vmm_unmap_vseg");
475 
476    // restore IRQs
477    hal_restore_irq( save_sr);
478
479    // release local user_dir_t structure
480    kmem_req_t  req;
481    req.type = KMEM_DIR;
482    req.ptr  = dir;
483    kmem_free( &req );
484
485#if DEBUG_USER_DIR
486cycle = (uint32_t)hal_get_cycles();
487if( cycle > DEBUG_USER_DIR )
488printk("\n[%s] thread[%x,%x] deleted user_dir (%x,%x) / cycle %d\n",
489__FUNCTION__, process->pid, this->trdid, local_cxy, dir, cycle );
490#endif
491
492}  // end user_dir_destroy()
Note: See TracBrowser for help on using the repository browser.