Changeset 4c53333 in mainline for uspace/srv/bd/file_bd/file_bd.c
- Timestamp:
- 2013-07-11T08:21:10Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 64e63ce1
- Parents:
- 80445cf (diff), c8bb1633 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
uspace/srv/bd/file_bd/file_bd.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/bd/file_bd/file_bd.c
r80445cf r4c53333 41 41 #include <stdio.h> 42 42 #include <unistd.h> 43 #include <ipc/bd.h>44 43 #include <async.h> 45 44 #include <as.h> 45 #include <bd_srv.h> 46 46 #include <fibril_synch.h> 47 47 #include <loc.h> … … 49 49 #include <sys/typefmt.h> 50 50 #include <errno.h> 51 #include < bool.h>51 #include <stdbool.h> 52 52 #include <task.h> 53 53 #include <macros.h> … … 62 62 63 63 static service_id_t service_id; 64 static bd_srvs_t bd_srvs; 64 65 static fibril_mutex_t dev_lock; 65 66 … … 67 68 static int file_bd_init(const char *fname); 68 69 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *); 69 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf); 70 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf); 70 71 static int file_bd_open(bd_srvs_t *, bd_srv_t *); 72 static int file_bd_close(bd_srv_t *); 73 static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t); 74 static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t); 75 static int file_bd_get_block_size(bd_srv_t *, size_t *); 76 static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *); 77 78 static bd_ops_t file_bd_ops = { 79 .open = file_bd_open, 80 .close = file_bd_close, 81 .read_blocks = file_bd_read_blocks, 82 .write_blocks = file_bd_write_blocks, 83 .get_block_size = file_bd_get_block_size, 84 .get_num_blocks = file_bd_get_num_blocks 85 }; 71 86 72 87 int main(int argc, char **argv) … … 119 134 rc = loc_service_register(device_name, &service_id); 120 135 if (rc != EOK) { 121 printf( NAME ": Unable to register device '%s'.\n",122 device_name);136 printf("%s: Unable to register device '%s'.\n", 137 NAME, device_name); 123 138 return rc; 124 139 } 125 126 printf( NAME ": Accepting connections\n");140 141 printf("%s: Accepting connections\n", NAME); 127 142 task_retval(0); 128 143 async_manager(); 129 144 130 145 /* Not reached */ 131 146 return 0; … … 139 154 static int file_bd_init(const char *fname) 140 155 { 141 int rc;142 long img_size;156 bd_srvs_init(&bd_srvs); 157 bd_srvs.ops = &file_bd_ops; 143 158 144 159 async_set_client_connection(file_bd_connection); 145 rc = loc_server_register(NAME);146 if (rc < 0) {147 printf( NAME ": Unable to register driver.\n");160 int rc = loc_server_register(NAME); 161 if (rc != EOK) { 162 printf("%s: Unable to register driver.\n", NAME); 148 163 return rc; 149 164 } 150 165 151 166 img = fopen(fname, "rb+"); 152 167 if (img == NULL) 153 168 return EINVAL; 154 169 155 170 if (fseek(img, 0, SEEK_END) != 0) { 156 171 fclose(img); 157 172 return EIO; 158 173 } 159 160 img_size = ftell(img);174 175 off64_t img_size = ftell(img); 161 176 if (img_size < 0) { 162 177 fclose(img); 163 178 return EIO; 164 179 } 165 180 166 181 num_blocks = img_size / block_size; 167 182 168 183 fibril_mutex_initialize(&dev_lock); 169 184 170 185 return EOK; 171 186 } … … 173 188 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg) 174 189 { 175 void *fs_va = NULL; 176 ipc_callid_t callid; 177 ipc_call_t call; 178 sysarg_t method; 179 size_t comm_size; 180 unsigned int flags; 181 int retval; 182 uint64_t ba; 183 size_t cnt; 184 185 /* Answer the IPC_M_CONNECT_ME_TO call. */ 186 async_answer_0(iid, EOK); 187 188 if (!async_share_out_receive(&callid, &comm_size, &flags)) { 189 async_answer_0(callid, EHANGUP); 190 return; 191 } 192 193 (void) async_share_out_finalize(callid, &fs_va); 194 if (fs_va == (void *) -1) { 195 async_answer_0(callid, EHANGUP); 196 return; 197 } 198 199 while (true) { 200 callid = async_get_call(&call); 201 method = IPC_GET_IMETHOD(call); 202 203 if (!method) { 204 /* The other side has hung up. */ 205 async_answer_0(callid, EOK); 206 return; 207 } 208 209 switch (method) { 210 case BD_READ_BLOCKS: 211 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 212 IPC_GET_ARG2(call)); 213 cnt = IPC_GET_ARG3(call); 214 if (cnt * block_size > comm_size) { 215 retval = ELIMIT; 216 break; 217 } 218 retval = file_bd_read_blocks(ba, cnt, fs_va); 219 break; 220 case BD_WRITE_BLOCKS: 221 ba = MERGE_LOUP32(IPC_GET_ARG1(call), 222 IPC_GET_ARG2(call)); 223 cnt = IPC_GET_ARG3(call); 224 if (cnt * block_size > comm_size) { 225 retval = ELIMIT; 226 break; 227 } 228 retval = file_bd_write_blocks(ba, cnt, fs_va); 229 break; 230 case BD_GET_BLOCK_SIZE: 231 async_answer_1(callid, EOK, block_size); 232 continue; 233 case BD_GET_NUM_BLOCKS: 234 async_answer_2(callid, EOK, LOWER32(num_blocks), 235 UPPER32(num_blocks)); 236 continue; 237 default: 238 retval = EINVAL; 239 break; 240 } 241 async_answer_0(callid, retval); 242 } 190 bd_conn(iid, icall, &bd_srvs); 191 } 192 193 /** Open device. */ 194 static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd) 195 { 196 return EOK; 197 } 198 199 /** Close device. */ 200 static int file_bd_close(bd_srv_t *bd) 201 { 202 return EOK; 243 203 } 244 204 245 205 /** Read blocks from the device. */ 246 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf) 206 static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf, 207 size_t size) 247 208 { 248 209 size_t n_rd; 249 210 int rc; 211 212 if (size < cnt * block_size) 213 return EINVAL; 250 214 251 215 /* Check whether access is within device address bounds. */ … … 282 246 283 247 /** Write blocks to the device. */ 284 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf) 248 static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, 249 const void *buf, size_t size) 285 250 { 286 251 size_t n_wr; 287 252 int rc; 253 254 if (size < cnt * block_size) 255 return EINVAL; 288 256 289 257 /* Check whether access is within device address bounds. */ … … 321 289 } 322 290 291 /** Get device block size. */ 292 static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize) 293 { 294 *rsize = block_size; 295 return EOK; 296 } 297 298 /** Get number of blocks on device. */ 299 static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb) 300 { 301 *rnb = num_blocks; 302 return EOK; 303 } 304 323 305 /** 324 306 * @}
Note:
See TracChangeset
for help on using the changeset viewer.
