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 /* Flush data conditionally to the filesystem object. */ 112 113 static long _flush(file_t *file, offset_t position) 114 { 115 long err; 116 117 /* Where the position is outside the current region, re-map. */ 118 119 if ((position < file->start_pos) || (position >= file->end_pos)) 120 { 121 if (file->can_mmap) 122 { 123 if (file_mmap(file, position, file_span(file))) 124 return -L4_EIO; 125 } 126 127 /* Strict conditions for region navigation in pipes. */ 128 129 else if ((position != file->end_pos) || 130 (client_next_region(file) == NULL)) 131 return -L4_EIO; 132 } 133 134 /* Otherwise, flush any written data in the current region and update the 135 file size details. */ 136 137 else 138 { 139 err = client_flush(file); 140 141 if (err) 142 return err; 143 } 144 145 /* Update the current data offset. */ 146 147 file->data_current = position - file->start_pos; 148 return L4_EOK; 149 } 150 151 152 153 /* Flush data explicitly to the filesystem object. */ 154 155 long client_flush(file_t *file) 156 { 157 if (file == NULL) 158 return -L4_EINVAL; 159 160 /* Flush and retain most buffer settings. */ 161 162 return file_flush(file); 163 } 164 165 166 167 /* Map a memory region to a file. */ 168 169 void *client_mmap(file_t *file, offset_t position, offset_t length) 170 { 171 if ((file == NULL) || (file_mmap(file, position, length))) 172 return NULL; 173 174 return file->memory; 175 } 176 177 178 179 /* Obtain the current region of a pipe. */ 180 181 void *client_current_region(file_t *file) 182 { 183 if ((file == NULL) || (pipe_current(file))) 184 return NULL; 185 186 return file->memory; 187 } 188 189 190 191 /* Obtain the next region of a pipe. */ 192 193 void *client_next_region(file_t *file) 194 { 195 if ((file == NULL) || (pipe_next(file))) 196 return NULL; 197 198 return file->memory; 199 } 200 201 202 203 /* Read from the filesystem object into the buffer provided. */ 204 205 offset_t client_read(file_t *file, void *buf, offset_t count) 206 { 207 if (file == NULL) 208 return 0; 209 210 /* Map memory if none has been mapped so far. */ 211 212 if ((file->memory == NULL) && (client_mmap(file, client_tell(file), count) == NULL)) 213 return 0; 214 215 /* Amount available in the descriptor buffer already. */ 216 217 offset_t available = file_data_available(file); 218 offset_t to_transfer, total = 0; 219 220 while (count > 0) 221 { 222 /* If there is no data, try and obtain more data. */ 223 224 if (!available) 225 { 226 /* Flush any unwritten data, preparing to read from the file position at 227 the end of the data, and returning if no new data is available. */ 228 229 if (_flush(file, file_data_end_position(file))) 230 break; 231 232 available = file_data_available(file); 233 234 if (!available) 235 break; 236 } 237 238 /* Transfer data into the supplied buffer. */ 239 240 to_transfer = available <= count ? available : count; 241 242 file_data_read(file, (char *) buf, to_transfer); 243 244 /* Update counters. */ 245 246 available -= to_transfer; 247 248 count -= to_transfer; 249 total += to_transfer; 250 251 buf = ((char *) buf + to_transfer); 252 } 253 254 return total; 255 } 256 257 258 259 /* Ensure that the buffer can provide the needed data. */ 260 261 offset_t client_seek(file_t *file, offset_t offset, int whence) 262 { 263 if (file == NULL) 264 return 0; 265 266 offset_t position, current = file_data_current_position(file), change; 267 268 switch (whence) 269 { 270 case SEEK_SET: 271 position = offset; 272 break; 273 274 case SEEK_CUR: 275 position = current + offset; 276 break; 277 278 case SEEK_END: 279 position = file->size + offset; 280 break; 281 282 default: 283 /* NOTE: Set errno to EINVAL. */ 284 return -1; 285 } 286 287 /* Retain the current position if unchanged. */ 288 289 if (position == current) 290 return position; 291 292 /* Move forward in the file. */ 293 294 if (position > current) 295 { 296 change = position - current; 297 298 /* Move towards the end of available data. 299 Request new data if not enough is available. */ 300 301 if (change <= file_data_available(file)) 302 { 303 file->data_current += change; 304 return position; 305 } 306 } 307 308 /* Move backward in the file. */ 309 310 else 311 { 312 change = current - position; 313 314 /* Move towards the start of available data. 315 Request new data if moving beyond the start of the data. */ 316 317 if (change <= file->data_current) 318 { 319 file->data_current -= change; 320 return position; 321 } 322 } 323 324 /* Handle unwritten data and reset the buffer for reading. */ 325 326 _flush(file, position); 327 return position; 328 } 329 330 331 332 long client_tell(file_t *file) 333 { 334 if (file == NULL) 335 return -L4_EINVAL; 336 337 return file_data_current_position(file); 338 } 339 340 341 342 /* Write to the filesystem object from the buffer provided. */ 343 344 offset_t client_write(file_t *file, const void *buf, offset_t count) 345 { 346 if (file == NULL) 347 return 0; 348 349 /* Map memory if none has been mapped so far. */ 350 351 if ((file->memory == NULL) && (client_mmap(file, client_tell(file), count) == NULL)) 352 return 0; 353 354 /* Attempt to ensure that the file can accept the amount of data to be 355 written. This may not resize to the needed amount if a file has a fixed 356 size, but data will still be written to any available space. */ 357 358 offset_t needed_size = file_data_current_position(file) + count; 359 360 if (file->size < needed_size) 361 { 362 file_resize(file, needed_size); 363 364 if (file->size < needed_size) 365 count = file->size - file_data_current_position(file); 366 } 367 368 /* Space remaining in the descriptor buffer. */ 369 370 offset_t space = file_data_space(file); 371 offset_t to_transfer, total = 0; 372 373 while (count > 0) 374 { 375 /* If no space is available, try and send data, reset the buffer. */ 376 377 if (!space) 378 { 379 /* Flush any unwritten data and continue writing from the current data 380 position. */ 381 382 if (_flush(file, file_data_current_position(file))) 383 break; 384 385 space = file_data_space(file); 386 } 387 388 /* Transfer data into the supplied buffer. */ 389 390 to_transfer = space <= count ? space : count; 391 392 file_data_write(file, (char *) buf, to_transfer); 393 394 /* Update counters. */ 395 396 space -= to_transfer; 397 398 count -= to_transfer; 399 total += to_transfer; 400 401 buf = ((char *) buf + to_transfer); 402 } 403 404 return total; 405 } 406 407 // vim: tabstop=2 expandtab shiftwidth=2