aboutsummaryrefslogtreecommitdiffstats
path: root/extras/mini-os/fs-front.c
blob: 59b240440b65b194e23f7414b7ebae66297f16c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
/******************************************************************************
 * fs-front.c
 * 
 * Frontend driver for FS split device driver.
 *
 * Copyright (c) 2007, Grzegorz Milos, <gm281@cam.ac.uk>.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

#undef NDEBUG
#include <stdint.h>
#include <mini-os/os.h>
#include <mini-os/list.h>
#include <mini-os/xmalloc.h>
#include <mini-os/xenbus.h>
#include <mini-os/gnttab.h>
#include <mini-os/events.h>
#include <xen/io/fsif.h>
#include <mini-os/fs.h>
#include <mini-os/sched.h>

#define preempt_disable()
#define preempt_enable()
#define cmpxchg(p,o,n) synch_cmpxchg(p,o,n)


#ifdef FS_DEBUG
#define DEBUG(_f, _a...) \
    printk("MINI_OS(file=fs-front.c, line=%d) " _f "\n", __LINE__, ## _a)
#else
#define DEBUG(_f, _a...)    ((void)0)
#endif


struct fs_request;
struct fs_import *fs_import;
void *alloc_buffer_page(struct fs_request *req, domid_t domid, grant_ref_t *gref);
void free_buffer_page(struct fs_request *req);

/******************************************************************************/
/*                      RING REQUEST/RESPONSES HANDLING                       */
/******************************************************************************/

struct fs_request
{
    void *private1;                        /* Specific to request type */
    void *private2;
    struct thread *thread;                 /* Thread blocked on this request */
    struct fsif_response shadow_rsp;       /* Response copy writen by the 
                                              interrupt handler */  
};

struct fs_rw_gnts
{
    /* TODO 16 bit? */
    int count;
    grant_ref_t grefs[FSIF_NR_READ_GNTS];  
    void *pages[FSIF_NR_READ_GNTS];  
};

/* Ring operations:
 * FSIF ring is used differently to Linux-like split devices. This stems from 
 * the fact that no I/O request queue is present. The use of some of the macros
 * defined in ring.h is not allowed, in particular:
 * RING_PUSH_REQUESTS_AND_CHECK_NOTIFY cannot be used.
 *
 * The protocol used for FSIF ring is described below:
 *
 * In order to reserve a request the frontend:
 * a) saves current frontend_ring->req_prod_pvt into a local variable
 * b) checks that there are free request using the local req_prod_pvt
 * c) tries to reserve the request using cmpxchg on frontend_ring->req_prod_pvt
 *    if cmpxchg fails, it means that someone reserved the request, start from
 *    a)
 * 
 * In order to commit a request to the shared ring:
 * a) cmpxchg shared_ring->req_prod from local req_prod_pvt to req_prod_pvt+1 
 *    Loop if unsuccessful.
 * NOTE: Request should be commited to the shared ring as quickly as possible,
 *       because otherwise other threads might busy loop trying to commit next
 *       requests. It also follows that preemption should be disabled, if
 *       possible, for the duration of the request construction.
 */

/* Number of free requests (for use on front side only). */
#define FS_RING_FREE_REQUESTS(_r, _req_prod_pvt)                         \
    (RING_SIZE(_r) - (_req_prod_pvt - (_r)->rsp_cons))



static RING_IDX reserve_fsif_request(struct fs_import *import)
{
    RING_IDX idx; 

    down(&import->reqs_sem);
    preempt_disable();
again:    
    /* We will attempt to reserve slot idx */
    idx = import->ring.req_prod_pvt;
    ASSERT (FS_RING_FREE_REQUESTS(&import->ring, idx));
    /* Attempt to reserve */
    if(cmpxchg(&import->ring.req_prod_pvt, idx, idx+1) != idx)
        goto again;

    return idx; 
}

static void commit_fsif_request(struct fs_import *import, RING_IDX idx)
{
    while(cmpxchg(&import->ring.sring->req_prod, idx, idx+1) != idx)
    {
        printk("Failed to commit a request: req_prod=%d, idx=%d\n",
                import->ring.sring->req_prod, idx);
    }
    preempt_enable();

    /* NOTE: we cannot do anything clever about rsp_event, to hold off
     * notifications, because we don't know if we are a single request (in which
     * case we have to notify always), or a part of a larger request group
     * (when, in some cases, notification isn't required) */
    notify_remote_via_evtchn(import->local_port);
}



static inline void add_id_to_freelist(unsigned int id,unsigned short* freelist)
{
    unsigned int old_id, new_id;

again:    
    old_id = freelist[0];
    /* Note: temporal inconsistency, since freelist[0] can be changed by someone
     * else, but we are a sole owner of freelist[id + 1], it's OK. */
    freelist[id + 1] = old_id;
    new_id = id;
    if(cmpxchg(&freelist[0], old_id, new_id) != old_id)
    {
        printk("Cmpxchg on freelist add failed.\n");
        goto again;
    }
}

/* always call reserve_fsif_request(import) before this, to protect from
 * depletion. */
static inline unsigned short get_id_from_freelist(unsigned short* freelist)
{
    unsigned int old_id, new_id;

again:    
    old_id = freelist[0];
    new_id = freelist[old_id + 1];
    if(cmpxchg(&freelist[0], old_id, new_id) != old_id)
    {
        printk("Cmpxchg on freelist remove failed.\n");
        goto again;
    }
    
    return old_id;
}

/******************************************************************************/
/*                  END OF RING REQUEST/RESPONSES HANDLING                    */
/******************************************************************************/



/******************************************************************************/
/*                         INDIVIDUAL FILE OPERATIONS                         */
/******************************************************************************/
int fs_open(struct fs_import *import, char *file)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int fd;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_open call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref id=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s", file);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_OPEN;
    req->id = priv_req_id;
    req->u.fopen.gref = gref;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    fd = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following FD returned: %d\n", fd);
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);

    return fd;
} 

int fs_close(struct fs_import *import, int fd)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_close call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    fsr->thread = current;

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_CLOSE;
    req->id = priv_req_id;
    req->u.fclose.fd = fd;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("Close returned: %d\n", ret);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
}

ssize_t fs_read(struct fs_import *import, int fd, void *buf, 
                ssize_t len, ssize_t offset)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    struct fs_rw_gnts gnts;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    ssize_t ret;
    int i;

    if (!import)
        return -1;

    BUG_ON(len > PAGE_SIZE * FSIF_NR_READ_GNTS);

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_read call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_READ;
    req->id = priv_req_id;
    req->u.fread.fd = fd;
    req->u.fread.len = len;
    req->u.fread.offset = offset;


    ASSERT(len > 0);
    gnts.count = ((len - 1) / PAGE_SIZE) + 1; 
    for(i=0; i<gnts.count; i++)
    {
        gnts.pages[i] = (void *)alloc_page(); 
        gnts.grefs[i] = gnttab_grant_access(import->dom_id, 
                                            virt_to_mfn(gnts.pages[i]), 
                                            0); 
        memset(gnts.pages[i], 0, PAGE_SIZE);
        req->u.fread.grefs[i] = gnts.grefs[i];
    }
    fsr->thread = current;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (ssize_t)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following ret value returned %d\n", ret);
    if(ret > 0)
    {
        ssize_t to_copy = ret, current_copy;
        for(i=0; i<gnts.count; i++)
        {
            gnttab_end_access(gnts.grefs[i]);
            current_copy = to_copy > PAGE_SIZE ? PAGE_SIZE : to_copy;
            if(current_copy > 0)
                memcpy(buf, gnts.pages[i], current_copy); 
            to_copy -= current_copy; 
            buf = (char*) buf + current_copy;
            free_page(gnts.pages[i]);
        }
    }
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

ssize_t fs_write(struct fs_import *import, int fd, void *buf, 
                 ssize_t len, ssize_t offset)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    struct fs_rw_gnts gnts;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    ssize_t ret, to_copy;
    int i;

    if (!import)
        return -1;

    BUG_ON(len > PAGE_SIZE * FSIF_NR_WRITE_GNTS);

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_read call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_WRITE;
    req->id = priv_req_id;
    req->u.fwrite.fd = fd;
    req->u.fwrite.len = len;
    req->u.fwrite.offset = offset;

    ASSERT(len > 0);
    gnts.count = ((len - 1) / PAGE_SIZE) + 1; 
    to_copy = len;
    for(i=0; i<gnts.count; i++)
    {
        int current_copy = (to_copy > PAGE_SIZE ? PAGE_SIZE : to_copy);
        gnts.pages[i] = (void *)alloc_page(); 
        gnts.grefs[i] = gnttab_grant_access(import->dom_id, 
                                            virt_to_mfn(gnts.pages[i]), 
                                            0); 
        memcpy(gnts.pages[i], buf, current_copy);
        if(current_copy < PAGE_SIZE)
            memset((char *)gnts.pages[i] + current_copy, 
                    0, 
                    PAGE_SIZE - current_copy); 
        req->u.fwrite.grefs[i] = gnts.grefs[i];
        to_copy -= current_copy; 
        buf = (char*) buf + current_copy;
    }
    fsr->thread = current;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (ssize_t)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following ret value returned %d\n", ret);
    for(i=0; i<gnts.count; i++)
    {
        gnttab_end_access(gnts.grefs[i]);
        free_page(gnts.pages[i]);
    }
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

int fs_stat(struct fs_import *import, 
            int fd, 
            struct fsif_stat_response *stat)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_stat call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    fsr->thread = current;

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_STAT;
    req->id = priv_req_id;
    req->u.fstat.fd   = fd;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("Following ret from fstat: %d\n", ret);
    memcpy(stat, 
           &fsr->shadow_rsp.u.fstat, 
           sizeof(struct fsif_stat_response));
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

int fs_truncate(struct fs_import *import, 
                int fd, 
                int64_t length)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_truncate call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    fsr->thread = current;

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_TRUNCATE;
    req->id = priv_req_id;
    req->u.ftruncate.fd = fd;
    req->u.ftruncate.length = length;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("Following ret from ftruncate: %d\n", ret);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

int fs_remove(struct fs_import *import, char *file)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_open call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s", file);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_REMOVE;
    req->id = priv_req_id;
    req->u.fremove.gref = gref;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following ret: %d\n", ret);
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
}


int fs_rename(struct fs_import *import, 
              char *old_file_name, 
              char *new_file_name)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;
    char old_header[] = "old: ";
    char new_header[] = "new: ";

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_open call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s%s%c%s%s", 
            old_header, old_file_name, '\0', new_header, new_file_name);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_RENAME;
    req->id = priv_req_id;
    req->u.frename.gref = gref;
    req->u.frename.old_name_offset = strlen(old_header);
    req->u.frename.new_name_offset = strlen(old_header) +
                                     strlen(old_file_name) +
                                     strlen(new_header) +
                                     1 /* Accouning for the additional 
                                          end of string character */;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following ret: %d\n", ret);
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
}

int fs_create(struct fs_import *import, char *name, 
              int8_t directory, int32_t mode)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_create call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s", name);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_CREATE;
    req->id = priv_req_id;
    req->u.fcreate.gref = gref;
    req->u.fcreate.directory = directory;
    req->u.fcreate.mode = mode;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following ret: %d\n", ret);
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

char** fs_list(struct fs_import *import, char *name, 
               int32_t offset, int32_t *nr_files, int *has_more)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    char **files, *current_file;
    int i;

    if (!import)
        return NULL;

    DEBUG("Different masks: NR_FILES=(%llx, %d), ERROR=(%llx, %d), HAS_MORE(%llx, %d)\n",
            NR_FILES_MASK, NR_FILES_SHIFT, ERROR_MASK, ERROR_SHIFT, HAS_MORE_FLAG, HAS_MORE_SHIFT);

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_list call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s", name);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_DIR_LIST;
    req->id = priv_req_id;
    req->u.flist.gref = gref;
    req->u.flist.offset = offset;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    *nr_files = (fsr->shadow_rsp.u.ret_val & NR_FILES_MASK) >> NR_FILES_SHIFT;
    files = NULL;
    if(*nr_files <= 0) goto exit;
    files = malloc(sizeof(char*) * (*nr_files));
    current_file = buffer; 
    for(i=0; i<*nr_files; i++)
    {
        files[i] = strdup(current_file); 
        current_file += strlen(current_file) + 1;
    }
    if(has_more != NULL)
        *has_more = fsr->shadow_rsp.u.ret_val & HAS_MORE_FLAG;
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);
exit:
    return files;
} 

int fs_chmod(struct fs_import *import, int fd, int32_t mode)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_chmod call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    fsr->thread = current;

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_CHMOD;
    req->id = priv_req_id;
    req->u.fchmod.fd = fd;
    req->u.fchmod.mode = mode;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following returned: %d\n", ret);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

int64_t fs_space(struct fs_import *import, char *location)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    grant_ref_t gref;
    void *buffer;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int64_t ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_space is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    buffer = alloc_buffer_page(fsr, import->dom_id, &gref);
    DEBUG("gref=%d\n", gref);
    fsr->thread = current;
    sprintf(buffer, "%s", location);

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FS_SPACE;
    req->id = priv_req_id;
    req->u.fspace.gref = gref;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int64_t)fsr->shadow_rsp.u.ret_val;
    DEBUG("The following returned: %lld\n", ret);
    free_buffer_page(fsr);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
} 

int fs_sync(struct fs_import *import, int fd)
{
    struct fs_request *fsr;
    unsigned short priv_req_id;
    RING_IDX back_req_id; 
    struct fsif_request *req;
    int ret;

    if (!import)
        return -1;

    /* Prepare request for the backend */
    back_req_id = reserve_fsif_request(import);
    DEBUG("Backend request id=%d\n", back_req_id);

    /* Prepare our private request structure */
    priv_req_id = get_id_from_freelist(import->freelist);
    DEBUG("Request id for fs_sync call is: %d\n", priv_req_id);
    fsr = &import->requests[priv_req_id];
    fsr->thread = current;

    req = RING_GET_REQUEST(&import->ring, back_req_id);
    req->type = REQ_FILE_SYNC;
    req->id = priv_req_id;
    req->u.fsync.fd = fd;

    /* Set blocked flag before commiting the request, thus avoiding missed
     * response race */
    block(current);
    commit_fsif_request(import, back_req_id);
    schedule();
    
    /* Read the response */
    ret = (int)fsr->shadow_rsp.u.ret_val;
    DEBUG("Close returned: %d\n", ret);
    add_id_to_freelist(priv_req_id, import->freelist);

    return ret;
}


/******************************************************************************/
/*                       END OF INDIVIDUAL FILE OPERATIONS                    */
/******************************************************************************/

void *alloc_buffer_page(struct fs_request *req, domid_t domid, grant_ref_t *gref)
{
    void *page;

    page = (void *)alloc_page(); 
    *gref = gnttab_grant_access(domid, virt_to_mfn(page), 0); 
    req->private1 = page;
    req->private2 = (void *)(uintptr_t)(*gref);

    return page;
}

void free_buffer_page(struct fs_request *req)
{
    gnttab_end_access((grant_ref_t)(uintptr_t)req->private2);
    free_page(req->private1);
}

static void fsfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
    struct fs_import *import = (struct fs_import*)data;
    static int in_irq = 0;
    RING_IDX cons, rp;
    int more;

    /* Check for non-reentrance */
    BUG_ON(in_irq);
    in_irq = 1;

    DEBUG("Event from import [%d:%d].\n", import->dom_id, import->export_id);
moretodo:   
    rp = import->ring.sring->rsp_prod;
    rmb(); /* Ensure we see queued responses up to 'rp'. */
    cons = import->ring.rsp_cons;
    while (cons != rp)
    {
        struct fsif_response *rsp;
        struct fs_request *req;

        rsp = RING_GET_RESPONSE(&import->ring, cons); 
        DEBUG("Response at idx=%d to request id=%d, ret_val=%lx\n", 
            cons, rsp->id, rsp->u.ret_val);
        req = &import->requests[rsp->id];
        memcpy(&req->shadow_rsp, rsp, sizeof(struct fsif_response));
        DEBUG("Waking up: %s\n", req->thread->name);
        wake(req->thread);

        cons++;
        up(&import->reqs_sem);
    }

    import->ring.rsp_cons = rp;
    RING_FINAL_CHECK_FOR_RESPONSES(&import->ring, more);
    if(more) goto moretodo;
    
    in_irq = 0;
}

static void alloc_request_table(struct fs_import *import)
{
    struct fs_request *requests;
    int i;

    BUG_ON(import->nr_entries <= 0);
    printk("Allocating request array for import %d, nr_entries = %d.\n",
            import->import_id, import->nr_entries);
    requests = xmalloc_array(struct fs_request, import->nr_entries);
    import->freelist = xmalloc_array(unsigned short, import->nr_entries + 1);
    memset(import->freelist, 0, sizeof(unsigned short) * (import->nr_entries + 1));
    for(i=0; i<import->nr_entries; i++)
        add_id_to_freelist(i, import->freelist);
    import->requests = requests;
}


/******************************************************************************/
/*                                FS TESTS                                    */
/******************************************************************************/


void test_fs_import(void *data)
{
    struct fs_import *import = (struct fs_import *)data; 
    int ret, fd, i, repeat_count;
    int32_t nr_files;
    char buffer[1024];
    ssize_t offset;
    char **files;
    long ret64;
    struct fsif_stat_response stat;
    
    repeat_count = 10; 
    /* Sleep for 1s and then try to open a file */
    msleep(1000);
again:
    ret = fs_create(import, "mini-os-created-directory", 1, 0777);
    printk("Directory create: %d\n", ret);

    sprintf(buffer, "mini-os-created-directory/mini-os-created-file-%d", 
            repeat_count);
    ret = fs_create(import, buffer, 0, 0666);
    printk("File create: %d\n", ret);

    fd = fs_open(import, buffer);
    printk("File descriptor: %d\n", fd);
    if(fd < 0) return;

    offset = 0;
    for(i=0; i<10; i++)
    {
        sprintf(buffer, "Current time is: %lld\n", NOW());
        ret = fs_write(import, fd, buffer, strlen(buffer), offset);
        printk("Writen current time (%d)\n", ret);
        if(ret < 0)
            return;
        offset += ret;
    }
    ret = fs_stat(import, fd, &stat);
    printk("Ret after stat: %d\n", ret);
    printk(" st_mode=%o\n", stat.stat_mode);
    printk(" st_uid =%d\n", stat.stat_uid);
    printk(" st_gid =%d\n", stat.stat_gid);
    printk(" st_size=%ld\n", stat.stat_size);
    printk(" st_atime=%ld\n", stat.stat_atime);
    printk(" st_mtime=%ld\n", stat.stat_mtime);
    printk(" st_ctime=%ld\n", stat.stat_ctime);
 
    ret = fs_close(import, fd);
    printk("Closed fd: %d, ret=%d\n", fd, ret);
   
    printk("Listing files in /\n");
    files = fs_list(import, "/", 0, &nr_files, NULL); 
    for(i=0; i<nr_files; i++)
        printk(" files[%d] = %s\n", i, files[i]);

    ret64 = fs_space(import, "/");
    printk("Free space: %lld (=%lld Mb)\n", ret64, (ret64 >> 20));
    repeat_count--;
    if(repeat_count > 0)
        goto again;
    
}

#if 0
//    char *content = (char *)alloc_page();
    int fd, ret;
//    int read;
    char write_string[] = "\"test data written from minios\"";
    struct fsif_stat_response stat;
    char **files;
    int32_t nr_files, i;
    int64_t ret64;


    fd = fs_open(import, "test-export-file");
//    read = fs_read(import, fd, content, PAGE_SIZE, 0);
//    printk("Read: %d bytes\n", read); 
//    content[read] = '\0';
//    printk("Value: %s\n", content);
    ret = fs_write(import, fd, write_string, strlen(write_string), 0);
    printk("Ret after write: %d\n", ret);
    ret = fs_stat(import, fd, &stat);
    printk("Ret after stat: %d\n", ret);
    printk(" st_mode=%o\n", stat.stat_mode);
    printk(" st_uid =%d\n", stat.stat_uid);
    printk(" st_gid =%d\n", stat.stat_gid);
    printk(" st_size=%ld\n", stat.stat_size);
    printk(" st_atime=%ld\n", stat.stat_atime);
    printk(" st_mtime=%ld\n", stat.stat_mtime);
    printk(" st_ctime=%ld\n", stat.stat_ctime);
    ret = fs_truncate(import, fd, 30);
    printk("Ret after truncate: %d\n", ret);
    ret = fs_remove(import, "test-to-remove/test-file");
    printk("Ret after remove: %d\n", ret);
    ret = fs_remove(import, "test-to-remove");
    printk("Ret after remove: %d\n", ret);
    ret = fs_chmod(import, fd, 0700);
    printk("Ret after chmod: %d\n", ret);
    ret = fs_sync(import, fd);
    printk("Ret after sync: %d\n", ret);
    ret = fs_close(import, fd);
    //ret = fs_rename(import, "test-export-file", "renamed-test-export-file");
    //printk("Ret after rename: %d\n", ret);
    ret = fs_create(import, "created-dir", 1, 0777);
    printk("Ret after dir create: %d\n", ret);
    ret = fs_create(import, "created-dir/created-file", 0, 0777);
    printk("Ret after file create: %d\n", ret);
    files = fs_list(import, "/", 15, &nr_files, NULL); 
    for(i=0; i<nr_files; i++)
        printk(" files[%d] = %s\n", i, files[i]);
    ret64 = fs_space(import, "created-dir");
    printk("Ret after space: %lld\n", ret64);

#endif


/******************************************************************************/
/*                            END OF FS TESTS                                 */
/******************************************************************************/

static int init_fs_import(struct fs_import *import)
{    
    char *err;
    xenbus_transaction_t xbt;
    char nodename[1024], r_nodename[1024], token[128], *message = NULL;
    struct fsif_sring *sring;
    int i, retry = 0;
    domid_t self_id;
    xenbus_event_queue events = NULL;

    printk("Initialising FS fortend to backend dom %d\n", import->dom_id);
    /* Allocate page for the shared ring */
    sring = (struct fsif_sring*) alloc_pages(FSIF_RING_SIZE_ORDER);
    memset(sring, 0, PAGE_SIZE * FSIF_RING_SIZE_PAGES);

    /* Init the shared ring */
    SHARED_RING_INIT(sring);
    ASSERT(FSIF_NR_READ_GNTS == FSIF_NR_WRITE_GNTS);

    /* Init private frontend ring */
    FRONT_RING_INIT(&import->ring, sring, PAGE_SIZE * FSIF_RING_SIZE_PAGES);
    import->nr_entries = import->ring.nr_ents;

    /* Allocate table of requests */
    alloc_request_table(import);
    init_SEMAPHORE(&import->reqs_sem, import->nr_entries);

    /* Grant access to the shared ring */
    for(i=0; i<FSIF_RING_SIZE_PAGES; i++) 
        import->gnt_refs[i] = 
            gnttab_grant_access(import->dom_id, 
                                virt_to_mfn((char *)sring + i * PAGE_SIZE), 
                                0);
   
    /* Allocate event channel */ 
    BUG_ON(evtchn_alloc_unbound(import->dom_id, 
                                fsfront_handler, 
                                //ANY_CPU, 
                                import, 
                                &import->local_port));
    unmask_evtchn(import->local_port);

    
    self_id = xenbus_get_self_id(); 
    /* Write the frontend info to a node in our Xenbus */
    sprintf(nodename, "/local/domain/%d/device/vfs/%d", 
                        self_id, import->import_id);

again:    
    err = xenbus_transaction_start(&xbt);
    if (err) {
        printk("starting transaction\n");
    }
    
    err = xenbus_printf(xbt, 
                        nodename, 
                        "ring-size",
                        "%u",
                        FSIF_RING_SIZE_PAGES);
    if (err) {
        message = "writing ring-size";
        goto abort_transaction;
    }
    
    for(i=0; i<FSIF_RING_SIZE_PAGES; i++)
    {
        sprintf(r_nodename, "ring-ref-%d", i);
        err = xenbus_printf(xbt, 
                            nodename, 
                            r_nodename,
                            "%u",
                            import->gnt_refs[i]);
        if (err) {
            message = "writing ring-refs";
            goto abort_transaction;
        }
    }

    err = xenbus_printf(xbt, 
                        nodename,
                        "event-channel", 
                        "%u", 
                        import->local_port);
    if (err) {
        message = "writing event-channel";
        goto abort_transaction;
    }

    err = xenbus_printf(xbt, nodename, "state", STATE_READY, 0xdeadbeef);

    
    err = xenbus_transaction_end(xbt, 0, &retry);
    if (retry) {
            goto again;
        printk("completing transaction\n");
    }

    /* Now, when our node is prepared we write request in the exporting domain
     * */
    printk("Our own id is %d\n", self_id);
    sprintf(r_nodename, 
            "/local/domain/%d/backend/vfs/exports/requests/%d/%d/frontend", 
            import->dom_id, self_id, import->export_id);
    BUG_ON(xenbus_write(XBT_NIL, r_nodename, nodename));

    goto done;

abort_transaction:
    xenbus_transaction_end(xbt, 1, &retry);

done:

#define WAIT_PERIOD 10   /* Wait period in ms */    
#define MAX_WAIT    10   /* Max number of WAIT_PERIODs */
    import->backend = NULL;
    sprintf(r_nodename, "%s/backend", nodename);
   
    for(retry = MAX_WAIT; retry > 0; retry--)
    { 
        xenbus_read(XBT_NIL, r_nodename, &import->backend);
        if(import->backend)
        {
            printk("Backend found at %s\n", import->backend);
            break;
        }
	msleep(WAIT_PERIOD);
    }        
    
    if(!import->backend)
    {
        printk("No backend available.\n");
        /* TODO - cleanup datastructures/xenbus */
        return 0;
    }
    sprintf(r_nodename, "%s/state", import->backend);
    sprintf(token, "fs-front-%d", import->import_id);
    /* The token will not be unique if multiple imports are inited */
    xenbus_watch_path_token(XBT_NIL, r_nodename, r_nodename, &events);
    xenbus_wait_for_value(r_nodename, STATE_READY, &events);
    xenbus_unwatch_path_token(XBT_NIL, r_nodename, r_nodename);
    printk("Backend ready.\n");
   
    //create_thread("fs-tester", test_fs_import, import); 

    return 1;
}

static void add_export(struct minios_list_head *exports, unsigned int domid)
{
    char node[1024], **exports_list = NULL, *ret_msg;
    int j = 0;
    static int import_id = 0;

    sprintf(node, "/local/domain/%d/backend/vfs/exports", domid);
    ret_msg = xenbus_ls(XBT_NIL, node, &exports_list);
    if (ret_msg && strcmp(ret_msg, "ENOENT"))
        printk("couldn't read %s: %s\n", node, ret_msg);
    while(exports_list && exports_list[j])
    {
        struct fs_import *import; 
        int export_id = -1;
        
        sscanf(exports_list[j], "%d", &export_id);
        if(export_id >= 0)
        {
            import = xmalloc(struct fs_import);
            import->dom_id = domid;
            import->export_id = export_id;
            import->import_id = import_id++;
            MINIOS_INIT_LIST_HEAD(&import->list);
            minios_list_add(&import->list, exports);
        }
        free(exports_list[j]);
        j++;
    }
    if(exports_list)
        free(exports_list);
    if(ret_msg)
        free(ret_msg);
}

#if 0
static struct minios_list_head* probe_exports(void)
{
    struct minios_list_head *exports;
    char **node_list = NULL, *msg = NULL;
    int i = 0;

    exports = xmalloc(struct minios_list_head);
    MINIOS_INIT_LIST_HEAD(exports);
    
    msg = xenbus_ls(XBT_NIL, "/local/domain", &node_list);
    if(msg)
    {
        printk("Could not list VFS exports (%s).\n", msg);
        goto exit;
    }

    while(node_list[i])
    {
        add_export(exports, atoi(node_list[i]));
        free(node_list[i]);
        i++;
    } 

exit:    
    if(msg)
        free(msg);
    if(node_list)
        free(node_list);
    return exports;
}
#endif

MINIOS_LIST_HEAD(exports);

void init_fs_frontend(void)
{
    struct minios_list_head *entry;
    struct fs_import *import = NULL;
    printk("Initing FS frontend(s).\n");

    add_export(&exports, 0);
    minios_list_for_each(entry, &exports)
    {
        import = minios_list_entry(entry, struct fs_import, list);
        printk("FS export [dom=%d, id=%d] found\n", 
                import->dom_id, import->export_id);
        if (init_fs_import(import) != 0) {
            fs_import = import;
            break;
        }
    }

    if (!fs_import)
	printk("No FS import\n");
}

/* TODO: shutdown */