Subversion Repositories HelenOS

Rev

Rev 4545 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4545 Rev 4557
Line 42... Line 42...
42
#include <unistd.h>
42
#include <unistd.h>
43
#include <ipc/ipc.h>
43
#include <ipc/ipc.h>
44
#include <ipc/bd.h>
44
#include <ipc/bd.h>
45
#include <async.h>
45
#include <async.h>
46
#include <as.h>
46
#include <as.h>
47
#include <futex.h>
47
#include <fibril_sync.h>
48
#include <devmap.h>
48
#include <devmap.h>
49
#include <sys/types.h>
49
#include <sys/types.h>
50
#include <errno.h>
50
#include <errno.h>
51
#include <bool.h>
51
#include <bool.h>
52
 
52
 
Line 54... Line 54...
54
 
54
 
55
static size_t comm_size;
55
static size_t comm_size;
56
static FILE *img;
56
static FILE *img;
57
 
57
 
58
static dev_handle_t dev_handle;
58
static dev_handle_t dev_handle;
59
static atomic_t dev_futex = FUTEX_INITIALIZER;
59
static fibril_mutex_t dev_lock;
60
 
60
 
61
static int file_bd_init(const char *fname);
61
static int file_bd_init(const char *fname);
62
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
62
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
63
static int file_bd_read(off_t blk_idx, size_t size, void *buf);
63
static int file_bd_read(off_t blk_idx, size_t size, void *buf);
64
static int file_bd_write(off_t blk_idx, size_t size, void *buf);
64
static int file_bd_write(off_t blk_idx, size_t size, void *buf);
Line 104... Line 104...
104
 
104
 
105
    img = fopen(fname, "rb+");
105
    img = fopen(fname, "rb+");
106
    if (img == NULL)
106
    if (img == NULL)
107
        return EINVAL;
107
        return EINVAL;
108
 
108
 
-
 
109
    fibril_mutex_initialize(&dev_lock);
-
 
110
 
109
    return EOK;
111
    return EOK;
110
}
112
}
111
 
113
 
112
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
114
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
113
{
115
{
Line 167... Line 169...
167
 
169
 
168
static int file_bd_read(off_t blk_idx, size_t size, void *buf)
170
static int file_bd_read(off_t blk_idx, size_t size, void *buf)
169
{
171
{
170
    size_t n_rd;
172
    size_t n_rd;
171
 
173
 
172
    printf("file_bd_read\n");
-
 
173
    futex_down(&dev_futex);
174
    fibril_mutex_lock(&dev_lock);
174
 
175
 
175
    printf("seek\n");
-
 
176
    fseek(img, blk_idx * size, SEEK_SET);
176
    fseek(img, blk_idx * size, SEEK_SET);
177
    printf("read\n");
-
 
178
    n_rd = fread(buf, 1, size, img);
177
    n_rd = fread(buf, 1, size, img);
179
    printf("done\n");
-
 
180
 
-
 
181
    printf("done\n");
-
 
182
 
178
 
183
    if (ferror(img)) {
179
    if (ferror(img)) {
184
        futex_up(&dev_futex);
180
        fibril_mutex_unlock(&dev_lock);
185
        return EIO; /* Read error */
181
        return EIO; /* Read error */
186
    }
182
    }
187
 
183
 
188
    futex_up(&dev_futex);
184
    fibril_mutex_unlock(&dev_lock);
189
 
185
 
190
    if (n_rd < size)
186
    if (n_rd < size)
191
        return EINVAL;  /* Read beyond end of disk */
187
        return EINVAL;  /* Read beyond end of disk */
192
 
188
 
193
    return EOK;
189
    return EOK;
Line 195... Line 191...
195
 
191
 
196
static int file_bd_write(off_t blk_idx, size_t size, void *buf)
192
static int file_bd_write(off_t blk_idx, size_t size, void *buf)
197
{
193
{
198
    size_t n_wr;
194
    size_t n_wr;
199
 
195
 
200
    futex_down(&dev_futex);
196
    fibril_mutex_lock(&dev_lock);
201
 
197
 
202
    fseek(img, blk_idx * size, SEEK_SET);
198
    fseek(img, blk_idx * size, SEEK_SET);
203
    n_wr = fread(buf, 1, size, img);
199
    n_wr = fread(buf, 1, size, img);
204
 
200
 
205
    if (ferror(img) || n_wr < size) {
201
    if (ferror(img) || n_wr < size) {
206
        futex_up(&dev_futex);
202
        fibril_mutex_unlock(&dev_lock);
207
        return EIO; /* Write error */
203
        return EIO; /* Write error */
208
    }
204
    }
209
 
205
 
210
    futex_up(&dev_futex);
206
    fibril_mutex_unlock(&dev_lock);
211
 
207
 
212
    return EOK;
208
    return EOK;
213
}
209
}
214
 
210
 
215
/**
211
/**