1 /* 2 * Filesystem client functions. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 27 #include "client.h" 28 29 30 31 /* Default size of pipe regions. */ 32 33 const offset_t DEFAULT_PIPE_SIZE = 4096; 34 35 36 37 /* Close a filesystem object. */ 38 39 void client_close(file_t *file) 40 { 41 if (file == NULL) 42 return; 43 44 file_close(file); 45 free(file); 46 } 47 48 49 50 /* Open a filesystem object. */ 51 52 file_t *client_open(const char *name, flags_t flags) 53 { 54 return client_open_device("server", name, flags); 55 } 56 57 /* Open a filesystem object via a named capability. */ 58 59 file_t *client_open_device(const char *cap, const char *name, flags_t flags) 60 { 61 file_t *file = (file_t *) malloc(sizeof(file_t)); 62 63 if (file == NULL) 64 return NULL; 65 66 l4_cap_idx_t server = l4re_env_get_cap(cap); 67 68 if (file_open(file, name, flags, server)) 69 { 70 free(file); 71 return NULL; 72 } 73 74 return file; 75 } 76 77 78 79 /* Open a pipe object. */ 80 81 long client_pipe(file_t **reader, file_t **writer) 82 { 83 *reader = (file_t *) malloc(sizeof(file_t)); 84 85 if (*reader == NULL) 86 return -L4_ENOMEM; 87 88 *writer = (file_t *) malloc(sizeof(file_t)); 89 90 if (*writer == NULL) 91 { 92 free(*reader); 93 return -L4_ENOMEM; 94 } 95 96 l4_cap_idx_t server = l4re_env_get_cap("pipes"); 97 98 long err = pipe_open(DEFAULT_PIPE_SIZE, *reader, *writer, server); 99 100 if (err) 101 { 102 free(*reader); 103 free(*writer); 104 } 105 106 return err; 107 } 108 109 110 111 /* Access the given position and synchronise state with the file object. */ 112 113 static long _access(file_t *file, offset_t position) 114 { 115 long err; 116 117 if (file->can_mmap) 118 { 119 /* Where the position is outside the current region, re-map. */ 120 121 if ((position < file->start_pos) || (position >= file->end_pos)) 122 { 123 if (file_mmap(file, position, file_span(file))) 124 return -L4_EIO; 125 } 126 127 /* Otherwise, flush any written data in the current region and update the 128 file size details. */ 129 130 else 131 { 132 err = client_flush(file); 133 if (err) 134 return err; 135 } 136 } 137 else 138 { 139 /* Strict conditions for region navigation in pipes. */ 140 141 if ((position < file->start_pos) || (position > file->end_pos)) 142 { 143 return -L4_EIO; 144 } 145 146 /* The next region is only available at the end of the mapped memory. */ 147 148 else if (position == file->end_pos) 149 { 150 if (client_next_region(file) == NULL) 151 return -L4_EIO; 152 } 153 154 /* Within the current pipe region, synchronise with the pipe object. */ 155 156 else 157 { 158 if (client_current_region(file) == NULL) 159 return -L4_EIO; 160 } 161 } 162 163 /* Update the current data offset. */ 164 165 if (file->has_size) 166 file->data_current = position - file->start_pos; 167 else 168 file->data_current = 0; 169 170 return L4_EOK; 171 } 172 173 174 175 /* Flush data explicitly to the filesystem object. */ 176 177 long client_flush(file_t *file) 178 { 179 if (file == NULL) 180 return -L4_EINVAL; 181 182 /* Flush and retain most buffer settings. */ 183 184 return file_flush(file); 185 } 186 187 188 189 /* Map a memory region to a file. */ 190 191 void *client_mmap(file_t *file, offset_t position, offset_t length) 192 { 193 if ((file == NULL) || (file_mmap(file, position, length))) 194 return NULL; 195 196 return file->memory; 197 } 198 199 200 201 /* Obtain the current region of a pipe. */ 202 203 void *client_current_region(file_t *file) 204 { 205 if ((file == NULL) || (pipe_current(file))) 206 return NULL; 207 208 return file->memory; 209 } 210 211 212 213 /* Obtain the next region of a pipe. */ 214 215 void *client_next_region(file_t *file) 216 { 217 if ((file == NULL) || (pipe_next(file))) 218 return NULL; 219 220 return file->memory; 221 } 222 223 224 225 /* Ensure that memory is mapped for accessing the given file, using the 226 indicated count as a region size hint. */ 227 228 static void *_map_memory(file_t *file, offset_t count) 229 { 230 if (file->memory == NULL) 231 { 232 if (file->can_mmap) 233 return client_mmap(file, client_tell(file), count); 234 else if (pipe_current(file)) 235 return NULL; 236 } 237 238 return file->memory; 239 } 240 241 242 243 /* Read from the filesystem object into the buffer provided. */ 244 245 offset_t client_read(file_t *file, void *buf, offset_t count) 246 { 247 if (file == NULL) 248 return 0; 249 250 /* Map memory if none has been mapped so far. */ 251 252 if (_map_memory(file, count) == NULL) 253 return 0; 254 255 /* Amount available in the descriptor buffer already. */ 256 257 offset_t available = file_data_available(file); 258 offset_t to_transfer, total = 0; 259 260 while (count > 0) 261 { 262 /* If there is no data, try and obtain more data. */ 263 264 if (!available) 265 { 266 /* Flush any unwritten data, preparing to read from the file position at 267 the end of the data, and returning if no new data is available. */ 268 269 if (_access(file, file_data_end_position(file))) 270 break; 271 272 available = file_data_available(file); 273 274 if (!available) 275 break; 276 } 277 278 /* Transfer data into the supplied buffer. */ 279 280 to_transfer = available <= count ? available : count; 281 282 file_data_read(file, (char *) buf, to_transfer); 283 284 /* Update counters. */ 285 286 available -= to_transfer; 287 288 count -= to_transfer; 289 total += to_transfer; 290 291 buf = ((char *) buf + to_transfer); 292 } 293 294 return total; 295 } 296 297 298 299 /* Ensure that the buffer can provide the needed data. */ 300 301 offset_t client_seek(file_t *file, offset_t offset, int whence) 302 { 303 if (file == NULL) 304 return 0; 305 306 offset_t position, current = file_data_current_position(file), change; 307 308 switch (whence) 309 { 310 case SEEK_SET: 311 position = offset; 312 break; 313 314 case SEEK_CUR: 315 position = current + offset; 316 break; 317 318 case SEEK_END: 319 position = file->size + offset; 320 break; 321 322 default: 323 /* NOTE: Set errno to EINVAL. */ 324 return -1; 325 } 326 327 /* Retain the current position if unchanged. */ 328 329 if (position == current) 330 return position; 331 332 /* Move forward in the file. */ 333 334 if (position > current) 335 { 336 change = position - current; 337 338 /* Move towards the end of available data. 339 Request new data if not enough is available. */ 340 341 if (change <= file_data_available(file)) 342 { 343 file->data_current += change; 344 return position; 345 } 346 } 347 348 /* Move backward in the file. */ 349 350 else 351 { 352 change = current - position; 353 354 /* Move towards the start of available data. 355 Request new data if moving beyond the start of the data. */ 356 357 if (change <= file->data_current) 358 { 359 file->data_current -= change; 360 return position; 361 } 362 } 363 364 /* Handle unwritten data and reset the buffer for reading. */ 365 366 _access(file, position); 367 return position; 368 } 369 370 371 372 long client_tell(file_t *file) 373 { 374 if (file == NULL) 375 return -L4_EINVAL; 376 377 return file_data_current_position(file); 378 } 379 380 381 382 /* Write to the filesystem object from the buffer provided. */ 383 384 offset_t client_write(file_t *file, const void *buf, offset_t count) 385 { 386 if (file == NULL) 387 return 0; 388 389 /* Map memory if none has been mapped so far. */ 390 391 if (_map_memory(file, count) == NULL) 392 return 0; 393 394 /* Attempt to ensure that the file can accept the amount of data to be 395 written. This may not resize to the needed amount if a file has a fixed 396 size, but data will still be written to any available space. */ 397 398 offset_t needed_size = file_data_current_position(file) + count; 399 400 if (file->has_size) 401 { 402 if (file->size < needed_size) 403 { 404 file_resize(file, needed_size); 405 406 if (file->size < needed_size) 407 count = file->size - file_data_current_position(file); 408 } 409 } 410 411 /* Space remaining in the descriptor buffer. */ 412 413 offset_t space = file_data_space(file); 414 offset_t to_transfer, total = 0; 415 416 while (count > 0) 417 { 418 /* If no space is available, try and send data, reset the buffer. */ 419 420 if (!space) 421 { 422 /* Flush any unwritten data and continue writing from the current data 423 position. */ 424 425 if (_access(file, file_data_current_position(file))) 426 break; 427 428 space = file_data_space(file); 429 } 430 431 /* Transfer data into the supplied buffer. */ 432 433 to_transfer = space <= count ? space : count; 434 435 file_data_write(file, (char *) buf, to_transfer); 436 437 /* Update counters. */ 438 439 space -= to_transfer; 440 441 count -= to_transfer; 442 total += to_transfer; 443 444 buf = ((char *) buf + to_transfer); 445 } 446 447 return total; 448 } 449 450 // vim: tabstop=2 expandtab shiftwidth=2