Subversion Repositories HelenOS

Rev

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

Rev 4537 Rev 4668
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
#include <task.h>
52
 
53
 
53
#define NAME "file_bd"
54
#define NAME "file_bd"
54
 
55
 
55
static size_t comm_size;
56
static size_t comm_size;
56
static FILE *img;
57
static FILE *img;
57
 
58
 
58
static dev_handle_t dev_handle;
59
static dev_handle_t dev_handle;
59
static atomic_t dev_futex = FUTEX_INITIALIZER;
60
static fibril_mutex_t dev_lock;
60
 
61
 
61
static int file_bd_init(const char *fname);
62
static int file_bd_init(const char *fname);
62
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
63
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
63
static int file_bd_read(off_t blk_idx, off_t size, void *buf);
64
static int file_bd_read(off_t blk_idx, size_t size, void *buf);
64
static int file_bd_write(off_t blk_idx, off_t size, void *buf);
65
static int file_bd_write(off_t blk_idx, size_t size, void *buf);
65
 
66
 
66
int main(int argc, char **argv)
67
int main(int argc, char **argv)
67
{
68
{
68
    int rc;
69
    int rc;
69
 
70
 
Line 84... Line 85...
84
            argv[2]);
85
            argv[2]);
85
        return rc;
86
        return rc;
86
    }
87
    }
87
 
88
 
88
    printf(NAME ": Accepting connections\n");
89
    printf(NAME ": Accepting connections\n");
-
 
90
    task_retval(0);
89
    async_manager();
91
    async_manager();
90
 
92
 
91
    /* Not reached */
93
    /* Not reached */
92
    return 0;
94
    return 0;
93
}
95
}
Line 104... Line 106...
104
 
106
 
105
    img = fopen(fname, "rb+");
107
    img = fopen(fname, "rb+");
106
    if (img == NULL)
108
    if (img == NULL)
107
        return EINVAL;
109
        return EINVAL;
108
 
110
 
-
 
111
    fibril_mutex_initialize(&dev_lock);
-
 
112
 
109
    return EOK;
113
    return EOK;
110
}
114
}
111
 
115
 
112
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
116
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
113
{
117
{
Line 116... Line 120...
116
    ipc_call_t call;
120
    ipc_call_t call;
117
    ipcarg_t method;
121
    ipcarg_t method;
118
    int flags;
122
    int flags;
119
    int retval;
123
    int retval;
120
    off_t idx;
124
    off_t idx;
121
    off_t size;
125
    size_t size;
122
 
126
 
123
    /* Answer the IPC_M_CONNECT_ME_TO call. */
127
    /* Answer the IPC_M_CONNECT_ME_TO call. */
124
    ipc_answer_0(iid, EOK);
128
    ipc_answer_0(iid, EOK);
125
 
129
 
126
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
130
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
Line 163... Line 167...
163
        }
167
        }
164
        ipc_answer_0(callid, retval);
168
        ipc_answer_0(callid, retval);
165
    }
169
    }
166
}
170
}
167
 
171
 
168
static int file_bd_read(off_t blk_idx, off_t size, void *buf)
172
static int file_bd_read(off_t blk_idx, size_t size, void *buf)
169
{
173
{
170
    size_t n_rd;
174
    size_t n_rd;
171
 
175
 
172
    printf("file_bd_read\n");
-
 
173
    futex_down(&dev_futex);
176
    fibril_mutex_lock(&dev_lock);
174
 
177
 
175
    printf("seek\n");
-
 
176
    fseek(img, blk_idx * size, SEEK_SET);
178
    fseek(img, blk_idx * size, SEEK_SET);
177
    printf("read\n");
-
 
178
    n_rd = fread(buf, 1, size, img);
179
    n_rd = fread(buf, 1, size, img);
179
    printf("done\n");
-
 
180
 
-
 
181
    printf("done\n");
-
 
182
 
180
 
183
    if (ferror(img)) {
181
    if (ferror(img)) {
184
        futex_up(&dev_futex);
182
        fibril_mutex_unlock(&dev_lock);
185
        return EIO; /* Read error */
183
        return EIO; /* Read error */
186
    }
184
    }
187
 
185
 
188
    futex_up(&dev_futex);
186
    fibril_mutex_unlock(&dev_lock);
189
 
187
 
190
    if (n_rd < size)
188
    if (n_rd < size)
191
        return EINVAL;  /* Read beyond end of disk */
189
        return EINVAL;  /* Read beyond end of disk */
192
 
190
 
193
    return EOK;
191
    return EOK;
194
}
192
}
195
 
193
 
196
static int file_bd_write(off_t blk_idx, off_t size, void *buf)
194
static int file_bd_write(off_t blk_idx, size_t size, void *buf)
197
{
195
{
198
    size_t n_wr;
196
    size_t n_wr;
199
 
197
 
200
    futex_down(&dev_futex);
198
    fibril_mutex_lock(&dev_lock);
201
 
199
 
202
    fseek(img, blk_idx * size, SEEK_SET);
200
    fseek(img, blk_idx * size, SEEK_SET);
203
    n_wr = fread(buf, 1, size, img);
201
    n_wr = fread(buf, 1, size, img);
204
 
202
 
205
    if (ferror(img) || n_wr < size) {
203
    if (ferror(img) || n_wr < size) {
206
        futex_up(&dev_futex);
204
        fibril_mutex_unlock(&dev_lock);
207
        return EIO; /* Write error */
205
        return EIO; /* Write error */
208
    }
206
    }
209
 
207
 
210
    futex_up(&dev_futex);
208
    fibril_mutex_unlock(&dev_lock);
211
 
209
 
212
    return EOK;
210
    return EOK;
213
}
211
}
214
 
212
 
215
/**
213
/**